text
stringlengths
216
518k
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: 2008๋…„๋„ โ€œ๋””์ง€ํ„ธ์ฝ˜ํ…์ธ (DC)๋Œ€์ƒโ€ ์‹œ์ƒ์‹ ๊ฐœ์ตœ ### ๋ณธ๋ฌธ: โ–ก ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€(์žฅ๊ด€ <NAME>)๋Š” 2008๋…„ ๋””์ง€ํ„ธ์ฝ˜ํ…์ธ  ๋ถ„์•ผ์—์„œ ๋‘๊ฐ์„ ๋‚˜ํƒ€๋‚ธ ์ตœ์šฐ์ˆ˜์ž‘ํ’ˆ์„ ์„ ์ •ํ•˜๊ณ , 12์›” 15์ผ(์›”) ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€ 5์ธต ๋…๋ฆฝ์˜ˆ์ˆ ์˜ํ™”๊ด€์—์„œ "2008๋…„ ๋””์ง€ํ„ธ์ฝ˜ํ…์ธ  ๋Œ€์ƒ" ์‹œ์ƒ์‹์„ ๊ฐœ์ตœํ–ˆ๋‹ค. ๋Œ€ํ†ต๋ น ์ƒ์— "<NAME> ๋””์ง€ํ„ธ์ฝ˜ํ…์ธ ", ๊ตญ๋ฌด์ด๋ฆฌ ์ƒ์— "์›น ๊ธฐ๋ฐ˜<NAME>์ƒ ํŽธ์ง‘๊ธฐ ํ”Œ๋ž˜์˜จ์— ", "์ดˆ๊ฐ๊ฐ ์ปคํ”Œ" ์ˆ˜์ƒ' 'โ–ก"2008๋…„ ๋””์ง€ํ„ธ์ฝ˜ํ…์ธ  ๋Œ€์ƒ"์€ 2007๋…„ 4๋ถ„๊ธฐ๋ถ€ํ„ฐ 2008๋…„ 3๋ถ„๊ธฐ๊นŒ์ง€์˜ ๋””์ง€ํ„ธ์ฝ˜ํ…์ธ  ๋Œ€์ƒ ์ˆ˜์ƒ์ž‘ํ’ˆ ์ค‘ ๊ฐ€์žฅ ์šฐ์ˆ˜ํ•œ ์ž‘ํ’ˆ์„ ์‹œ์ƒํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ์ด๋ฒˆ ์‹œ์ƒ์‹์—์„œ ์˜์˜ˆ์˜ ๋Œ€ํ†ต๋ น ์ƒ์—๋Š” (์ฃผ) ์•„์‚ฌ๋‹ฌ์˜ "์•„์‚ฌ๋‹ฌ ๋””์ง€ํ„ธ์ฝ˜ํ…์ธ ", ๊ตญ๋ฌด์ด๋ฆฌ ์ƒ์—๋Š” (์ฃผ)์—” ์—์ดํฌ์˜ "์›น ๊ธฐ๋ฐ˜<NAME>์ƒ ํŽธ์ง‘๊ธฐ ํ”Œ๋ž˜์˜จ์— ", ๊ทธ๋ฆฌ๊ณ  (์ฃผ) ํฌ๋กœ์Šค ํ•„๋ฆ„๊ณผ (์ฃผ) ์˜๋กœ ์—”ํ„ฐํ…Œ์ธ๋จผํŠธ๊ฐ€ ๊ณต๋™์ œ์ž‘ํ•œ "์ดˆ๊ฐ๊ฐ ์ปคํ”Œ"์ด ๊ฐ๊ฐ ์ˆ˜์ƒํ–ˆ๋‹ค. โ–ก๋””์ž์ธ ์†Œ์Šค, ์•„์ด์ฝ˜ ๋ฐ ์„ค๋ฃจ์…˜์„ ์ œ์ž‘. ํŒ๋งคํ•˜๋Š” (์ฃผ) ์•„์‚ฌ๋‹ฌ์˜ "์•„์‚ฌ๋‹ฌ ๋””์ง€ํ„ธ ์ฝ˜ํ…์ธ "๋Š” ํ™ˆํŽ˜์ด์ง€ ์ œ์ž‘, ์ถœํŒ, PPT, ๊ฒŒ์ž„, ๊ต์œก ๋“ฑ ๋‹ค์–‘ํ•œ ์ฝ˜ํ…์ธ  ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๋‹ค ์‰ฝ๊ณ  ํšจ๊ณผ์ ์œผ๋กœ ์ฝ˜ํ…์ธ ๋ฅผ ์ œ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•˜๋Š” ์˜จ๋ผ์ธ ์ •๋ณด ์ฝ˜ํ…์ธ ๋กœ ๋Œ€ํ†ต๋ น ์ƒ์„ ์ˆ˜์ƒํ–ˆ๋‹ค. ํŠนํžˆ ์˜ฌํ•ด์—๋Š” 50์—ฌ ๋ช…์˜ ๋””์ž์ด๋„ˆ์™€ ๊ฐœ๋ฐœ์ž๋ฅผ ํฌํ•จํ•œ ์ค‘๊ตญ ๋ฒ•์ธ์„ ์„ค๋ฆฝํ•˜๊ณ  ์ผ๋ณธ๊ณผ ์ฝ˜ํ…์ธ  ๊ณต๊ธ‰๊ณ„์•ฝ์„ ์ฒด๊ฒฐํ•˜๋Š” ๋“ฑ ํ•ด์™ธ์‹œ์žฅ ๊ฐœ์ฒ™์— ๋ฐ•์ฐจ๋ฅผ ๊ฐ€ํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋ฒˆ 2008๋…„ ๋””์ง€ํ„ธ์ฝ˜ํ…์ธ  ๋Œ€์ƒ์—์„œ๋Š” ์ œํ’ˆ์˜ ์šฐ์ˆ˜์„ฑ, ๊ธฐ์ˆ ๋ ฅ, ํ•ด์™ธ์‹œ์žฅ ์ง„์ถœ ๋“ฑ์—์„œ ์„ฑ๊ณต์ ์ธ ์‚ฌ๋ก€๋ฅผ ์„ ๋ณด์˜€๋‹ค๋Š” ํ‰๊ฐ€๋ฅผ ๋ฐ›์•˜์œผ๋ฉฐ, ํ–ฅํ›„ ๋Œ€๋งŒ๊ณผ ๋ถ๋ฏธ์‹œ์žฅ์—๋„ ๋™์ผํ•œ ์„œ๋น„์Šค๋ฅผ ์ƒ์šฉํ™”ํ•  ์˜ˆ์ •์ด๋‹ค.' ๊ตญ๋ฌด์ด๋ฆฌ ์ƒ์„ ์ˆ˜์ƒํ•œ ์›น ๊ธฐ๋ฐ˜<NAME>์ƒ ํŽธ์ง‘๊ธฐ ํ”Œ๋ž˜์˜จ์— ์€ ์›น ๊ธฐ๋ฐ˜ ์„œ๋ฒ„์‚ฌ์ด๋“œ ๊ธฐ์ˆ ๊ณผ ์›น ํ‘œ์ค€์„ ์ ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์›น์ƒ์—์„œ Active-X ๋“ฑ ๋ณ„๋„์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์›น์ƒ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์›น 2.0 ์„ค๋ฃจ์…˜์ด๋‹ค. ์ด ์ž‘ํ’ˆ์€ ํœด๋Œ€ํฐ, PDA, IPTV ๋“ฑ ๋‹ค์–‘ํ•œ ์ธํ„ฐ๋„ท ๋””๋ฐ”์ด์Šค์™€ ์—ฐ๋™์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์‚ฌ์šฉ์ž๊ฐ€ ์‰ฝ๊ฒŒ ๋ฉ€ํ‹ฐ๋ฏธ๋””์–ด UCC๋ฅผ ์ œ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์„ ์ œ๊ณตํ•œ๋‹ค. ํ˜„์žฌ SKT, KTF, G๋งˆ์ผ“ ๋“ฑ ๊ตญ๋‚ด ํ†ต์‹ ์‚ฌ์™€ ์—ฌ๋Ÿฌ ์—…์ฒด์— ์„ค๋ฃจ์…˜์„ ๊ณต๊ธ‰ํ•˜์—ฌ 7์–ต์—ฌ ์›์˜ ๋งค์ถœ์„ ์˜ฌ๋ ธ์œผ๋ฉฐ, ํ–ฅํ›„์—๋„ ๋†’์€ ์ˆ˜์ต์„ฑ์ด ๊ธฐ๋Œ€๋˜๊ณ  ์žˆ๋‹ค.' ๋˜ํ•œ ๊ตญ๋ฌด์ด๋ฆฌ ์ƒ์„ ์ˆ˜์ƒํ•œ (์ฃผ) ํฌ๋กœ์Šค ํ•„๋ฆ„, (์ฃผ) ์˜๋กœ ์—”ํ„ฐํ…Œ์ธ๋จผํŠธ์˜ "์ดˆ๊ฐ๊ฐ ์ปคํ”Œ"์€ ๋ฏธ์Šคํ„ฐ๋ฆฌ HD ๋””์ง€ํ„ธ ์˜ํ™”๋กœ, ์‹ค์‚ฌ์˜ํ™”์—์„œ ๊ตฌํ˜„ํ•˜๊ธฐ ํž˜๋“  ํŒํƒ€์ง€ ๋ถ€๋ถ„์— ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๊ฐ€๋ฏธํ•จ์œผ๋กœ์จ ์˜์ƒ ๊ตฌํ˜„์˜ ํ•œ๊ณ„๋ฅผ ๊ทน๋ณตํ•œ ์ ์ด ๋‹๋ณด์ธ๋‹ค. o ํŠนํžˆ, ์ €์˜ˆ์‚ฐ์œผ๋กœ ์ œ์ž‘๋˜์—ˆ์œผ๋‚˜ CJ CGV, CJ ๋ฏธ๋””์–ด ๋“ฑ์˜ ๋ฉ”์ด์ € ๋ฐฐ๊ธ‰ ๋ฐ ๋งˆ์ผ€ํŒ…ํŒ€์ด ๊ฒฐํ•ฉ๋˜์–ด ์†Œ์ž๋ณธ ์ƒ์—…์˜ํ™”์˜ ์ƒˆ๋กœ์šด ์‹œ์žฅ ์‹คํ—˜ ์ž‘ํ’ˆ์œผ๋กœ์จ ๋””์ง€ํ„ธ ์ƒ์˜๊ด€ ์ค‘์‹ฌ์˜ ๊ทน์žฅ ๊ฐœ๋ด‰ ํ›„ ๊ด€๊ฐ๋“ค๋กœ๋ถ€ํ„ฐ ์ข‹์€ ํ‰๊ฐ€๋ฅผ ๋ฐ›๊ณ  ์žˆ์œผ๋ฉฐ, ์ผ๋ณธ All Rights ์ˆ˜์ถœ ๊ณ„์•ฝ ์ฒด๊ฒฐ ๋“ฑ์˜ ์„ฑ๊ณผ๋ฅผ ๋ณด์ด๊ณ  ์žˆ๋‹ค.' 'โ–ก๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€๋Š” ๊ตญ๋‚ด ๋””์ง€ํ„ธ์ฝ˜ํ…์ธ  ๋ถ„์•ผ์˜ ์šฐ์ˆ˜ํ•œ ์ž‘ํ’ˆ์„ ๋ฐœ๊ตด, ํ™๋ณดโ€ค๋งˆ์ผ€ํŒ…๊ณผ ๊ตญ๋‚ด์™ธ ์‹œ์žฅ ์ง„์ถœ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ๋งค ๋ถ„๊ธฐ๋ณ„ ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€ ์žฅ๊ด€์ƒ์„ ์ˆ˜์—ฌํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ์ˆ˜์ƒ์—…์ฒด๋“ค์—๊ฒŒ๋Š” ์–ธ๋ก ํ™๋ณด ๋“ฑ์˜ ๋งˆ์ผ€ํŒ… ์ง€์›๊ณผ ํ•œ๊ตญ์†Œํ”„ํŠธ์›จ์–ด์ง„ํฅ์›์—์„œ ์ฃผ๊ด€ํ•˜๋Š” ๋””์ง€ํ„ธ์ฝ˜ํ…์ธ  ๊ด€๋ จ ์‚ฌ์—… ์ฐธ์—ฌ ์‹œ ๊ฐ€์‚ฐ์ ์ด ๋ถ€๊ณผ๋˜๋ฉฐ, ํ•ด์™ธ ์ „๋ฌธ๊ต์œก ์ง€์› ๋“ฑ ๋‹ค์–‘ํ•œ ํŠน์ „์ด ์ฃผ์–ด์ง„๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ์ž์ „๊ฑฐ๋กœ ์ „๊ตญ ๋ˆ„๋น„๋Š” ๋“œ๋ฆผ์ฝ”๋ฆฌ์•„ ๋ฆฌํฌํ„ฐ๋‹จ ๋ชจ์ง‘ ### ๋ณธ๋ฌธ: ์ž์ „๊ฑฐ๋กœ ์ „๊ตญ ๋ˆ„๋น„๋Š” ๋“œ๋ฆผ์ฝ”๋ฆฌ์•„ ๋ฆฌํฌํ„ฐ๋‹จ ๋ชจ์ง‘ o ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€(์žฅ๊ด€ : <NAME>)๋Š” ์ž์ „๊ฑฐ๋ฅผ ํƒ€๊ณ  ์ „๊ตญ์„ ๋ˆ„๋น„๋ฉฐ, ๊ตญ๋ฏผ์˜ ์ œ์•ˆ๊ณผ ์•„์ด๋””์–ด๋กœ ๊ตญ๊ฐ€์ •์ฑ…์„ ํ•จ๊ป˜ ๋งŒ๋“ค์–ด๋‚˜๊ฐ€๋Š” '์œ„ํ‚ค' ๋ฐฉ์‹ ์‚ฌ์ดํŠธใ€Œ๋“œ๋ฆผ์ฝ”๋ฆฌ์•„ใ€(www.dreamkorea.org)์— ํ™œ๋ฐœํ•œ ์ง€์‹๊ธฐ๋ถ€ ํ™œ๋™์„ ํ•ด ์ค„ '๋“œ๋ฆผ์ฝ”๋ฆฌ์•„ ์ž์ „๊ฑฐ ๋ฆฌํฌํ„ฐ๋‹จ'์„ ๋ชจ์ง‘ํ•œ๋‹ค๊ณ  16์ผ ๋ฐํ˜”๋‹ค. o '๋“œ๋ฆผ์ฝ”๋ฆฌ์•„ ์ž์ „๊ฑฐ ๋ฆฌํฌํ„ฐ๋‹จ'์€ ์ž์ „๊ฑฐ๋ฅผ ํƒ€๊ณ  ๋‹ค๋‹Œ ๊ฒฝํ—˜์„ ๋ฐ”ํƒ•์œผ๋กœ ์ €ํƒ„์†Œ ๋…น์ƒ‰์„ฑ์žฅ์„ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ์•„์ด๋””์–ด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—ญํ• ๊ณผ ํ•จ๊ป˜, ์ง€์—ญ์˜ ๋ช…์†Œ์™€ ์ถ•์ œ ๋“ฑ์— ๊ด€ํ•œ ๋‚ด์šฉ์„ ์ทจ์žฌํ•˜๊ณ <NAME>๋ฉด์„œ ์ •์ฑ…์„ ์ œ์•ˆํ•˜๊ธฐ๋„ ํ•˜๋Š” ๋“ฑ ์ ๊ทน์ ์ธ ์ฐธ์—ฌ์ž๋กœ์„œ ํ™œ๋™ํ•  ์˜ˆ์ •์ด๋‹ค. ์‹œโ€ค๋„ ๋‹จ์œ„๋กœ ์šด์˜๋  ๋ฆฌํฌํ„ฐ๋‹จ์€ ๋“œ๋ฆผ์ฝ”๋ฆฌ์•„ ์‚ฌ์ดํŠธ์—์„œ ์ƒ์‹œ ๋ชจ์ง‘ํ•œ๋‹ค. ํ•œํŽธ, ๋“œ๋ฆผ ์ฝ”๋ฆฌ์•„๋Š” ์ค‘์š”ํ•œ ๊ด€๋ จ ํ–‰์‚ฌ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ํ™ˆํŽ˜์ด์ง€์—์„œ ์‹ ์ฒญ์„ ๋ฐ›์•„ ์†Œ์ •์˜ ์‹ฌ์‚ฌ๋ฅผ ๊ฑฐ์ณ ํŠน๋ณ„ ๋ฆฌํฌํ„ฐ ํŒ€์„ ๊ตฌ์„ฑ, ์šด์˜ํ•  ๊ณ„ํš์ด๋‹ค. ํ™˜๊ฒฝ์˜ฌ๋ฆผํ”ฝ'์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๋žŒ์‚ฌ๋ฅด์ดํšŒ๋Š” ์ž์ „๊ฑฐ๋กœ 1์ฐจ์ ์œผ๋กœ ์˜ค๋Š” 28์ผ๋ถ€ํ„ฐ ์—ด๋ฆฌ๊ณ , ํŠน๋ณ„ ๋ฆฌํฌํ„ฐ ํŒ€์ด ์ž์ „๊ฑฐ๋ฅผ ํƒ€๊ณ  ์ฐธ์—ฌํ•  ์˜ˆ์ •์ด๋‹ค. ์Šต์ง€ ๋ณด์กด์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํ•œ๋ˆˆ์— ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋ฆฌ์•„ ์‚ฌ์ดํŠธ์— ์˜ฌ๋ ค ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋œ๋‹ค. o <NAME> ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€ ๋‰ด๋ฏธ๋””์–ดํ™๋ณด๊ณผ์žฅ์€ "'์ž์ „๊ฑฐ ๋ฆฌํฌํ„ฐ๋‹จ'์€ ์ž์ „๊ฑฐ๋ฅผ ํƒ€๊ณ  ์ „๊ตญ์„ ๋ˆ„๋น„๋ฉฐ ์ƒ์ƒํ•œ ์ง€์—ญ์˜ ์†Œ์‹์„ ์ „ํ•˜๊ณ , ์ž์ „๊ฑฐ ์ด์šฉ์ž๋กœ์„œ์˜ ๊ฒฝํ—˜์„ ๋ฐ”ํƒ•์œผ๋กœ ๊ผญ ํ•„์š”ํ•œ ์ข‹์€ ์ •์ฑ…๋“ค์„ ์ œ์•ˆํ•˜๊ฒŒ ๋  ๊ฒƒ"์ด๋ผ๋ฉฐ "์นœํ™˜๊ฒฝ ๊ตํ†ต์ˆ˜๋‹จ์ธ ์ž์ „๊ฑฐ๊ฐ€ ํฐ ๊ด€์‹ฌ์„ ๋ฐ›๊ณ  ์žˆ๋Š” ์ƒํ™ฉ์—์„œ ์ž์ „๊ฑฐ ๋ฆฌํฌํ„ฐ๋‹จ์— ๋งŽ์€ ๊ด€์‹ฌ์ด ๋ชจ์•„์งˆ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ•œ๋‹ค"๋ผ๊ณ  ๋ฐํ˜”๋‹ค. ํ•œํŽธ ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€๋Š” ์˜ค๋Š” 19์ผ ์„œ์šธ์‹œ์ฒญ ์•ž ๊ด‘์žฅ์—์„œ ์ž์ „๊ฑฐ ์—ฌํ–‰๊ฐ€ <NAME> ์”จ ๋“ฑ์ด ์ฐธ์„ํ•œ ๊ฐ€์šด๋ฐ ๋“œ๋ฆผ์ฝ”๋ฆฌ์•„ ์ž์ „๊ฑฐ ๋ฆฌํฌํ„ฐ๋‹จ ๋ฐœ๋Œ€์‹์„ ๊ฐœ์ตœํ•  ์˜ˆ์ •์ด๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: โ€œ ์ƒˆ ์ •๋ถ€์˜ ํƒœ๊ถŒ๋„ ์ง„ํฅ ๋ฐ ๊ฐœํ˜๋ฐฉ์•ˆ ๋ชจ์ƒ‰โ€ ### ๋ณธ๋ฌธ: ์ƒˆ ์ •๋ถ€์˜ ํƒœ๊ถŒ๋„ ์ง„ํฅ ๋ฐ ๊ฐœํ˜ ๋ฐฉ์•ˆ ๋ชจ์ƒ‰" ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€(์žฅ๊ด€ <NAME>)๋Š” 7์›” 25์ผ(๊ธˆ) 14:00์— ๊ตญ๋ฆฝ๋ฏผ์†๋ฐ•๋ฌผ๊ด€ ๋Œ€๊ฐ•๋‹น์—์„œ ํƒœ๊ถŒ๋„ ์ง„ํฅ ์ •์ฑ… ํ† ๋ก ํšŒ๋ฅผ ๊ฐœ์ตœํ•œ๋‹ค. ์ƒˆ ์ •๋ถ€์˜ ํƒœ๊ถŒ๋„ ์ง„ํฅ ๊ธฐ๋ณธ๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•˜๊ธฐ ์œ„ํ•ด ์—ด๋ฆฌ๋Š” ์ด๋ฒˆ ํ† ๋ก ํšŒ๋Š” ํƒœ๊ถŒ๋„ ์ง„ํฅ ๋ฐ ํƒœ๊ถŒ๋„๊ณต์› ์กฐ์„ฑ ๋“ฑ์— ๊ด€ํ•œ ๋ฒ•๋ฅ ์— ์˜๊ฑฐํ•ด ํƒœ๊ถŒ๋„ ์ง„ํฅ๊ณผ ๊ตญ๊ธฐ์› ๋ฐœ์ „ ๋ฐฉ์•ˆ์„ ์ฃผ์ œ๋กœ ํ•œ๋‹ค. ์ •๋ถ€์—์„œ '08.6.22'๋ถ€ํ„ฐ 5๊ฐœ๋…„ ๋‹จ์œ„์˜ 'ํƒœ๊ถŒ๋„ ์ง„ํฅ ๊ธฐ๋ณธ๊ณ„ํš'์„ ์˜๋ฌด์ ์œผ๋กœ ์ˆ˜๋ฆฝํ•ด์•ผ ํ•œ๋‹ค. ์ด์— ๋”ฐ๋ผ ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€์—์„œ๋Š” ์ „๋ฌธ ์—ฐ๊ตฌ ๋‹จ์ฒด๋ฅผ ํ†ตํ•ด ์—ฐ๊ตฌ์šฉ์—ญ์„ ์ง„ํ–‰ํ•ด ์™”๊ณ  ๊ทธ๋™์•ˆ ๋„์ถœ๋œ ์—ฐ๊ตฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ด€๋ จ ์ „๋ฌธ๊ฐ€์˜ ๊ฒ€์ฆ์„ ๊ฑฐ์น˜๊ธฐ ์œ„ํ•ด ํ† ๋ก ํšŒ๋ฅผ ๊ฐœ์ตœํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ํ† ๋ก ํšŒ์—๋Š” ํƒœ๊ถŒ๋„ํ•™์„ ์ „๊ณตํ•œ ๊ด€๋ จ ์ „๋ฌธ๊ฐ€ ๋“ฑ์ด ์ฐธ์„ํ•˜์—ฌ ํƒœ๊ถŒ๋„ ์ข…์ฃผ๊ตญ์œผ๋กœ์„œ์˜ ์œ„์ƒ ๊ฐ•ํ™”์™€ ๋Œ€์™ธ์  ์—ญํ•  ํ™•๋Œ€, ํƒœ๊ถŒ๋„ ์˜ฌ๋ฆผํ”ฝ ์ข…๋ชฉ ์œ ์ง€๋ฅผ ์œ„ํ•œ ๋Œ€์ฑ…, ์ €๋ณ€ ํ™•๋Œ€, ๋ฌธํ™”์‚ฐ์—…๊ณผ ๊ด€๊ด‘์‚ฐ์—… ์—ฐ๊ณ„๋ฅผ ํ†ตํ•œ ๋ฐœ์ „ ๋ฐฉ์•ˆ ๋“ฑ์— ๋Œ€ํ•ด ์ง‘์ค‘์ ์œผ๋กœ ๋…ผ์˜๋  ์˜ˆ์ •์ด๋‹ค. ๋˜ํ•œ ๊ตญ๊ธฐ์›์„ ๋น„๋กฏํ•œ ํƒœ๊ถŒ๋„๊ณ„์˜ ๊ฐ•๋„ ๋†’์€ ๊ฐœํ˜๊ณผ ๋ฐœ์ „๋ฐฉ์•ˆ๋„ ์ œ์‹œ๋  ์˜ˆ์ •์ด๋‹ค. ๊ธˆ๋…„ 9์›” 4์ผ ํƒœ๊ถŒ๋„์˜ ๋‚ ์— ๋ฐœํ‘œ๋  ํƒœ๊ถŒ๋„ ์ง„ํฅ ๊ธฐ๋ณธ๊ณ„ํš์€ ํ† ๋ก ํšŒ๋ฅผ ํ†ตํ•ด ์ œ์‹œ๋œ ์˜๊ฒฌ๋“ค์„ ๋ฐ˜์˜ํ•˜์—ฌ ์ˆ˜๋ฆฝ๋œ๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: โ€œ ๋„์„œ๊ด€์ •๋ณด์ •์ฑ…์œ„์›ํšŒ ๋ฐœ์กฑ โ€ ### ๋ณธ๋ฌธ: " ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ… ์œ„์›ํšŒ ๋ฐœ์กฑ " ์ •๋ถ€๋Š” ๋„์„œ๊ด€ ์ •์ฑ…์˜ ์ฃผ์š” ์‚ฌํ•ญ์„ ์ˆ˜๋ฆฝยท์‹ฌ์˜ยท์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•œ ๋Œ€ํ†ต๋ น ์†Œ์†์˜ ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ… ์œ„์›ํšŒ ์œ„์›์žฅ์— <NAME>(้Ÿ“็›ธๅฎŒ) ์ „ ์—ฐ์„ธ๋Œ€ํ•™๊ต ๋ถ€์ด์žฅ์„ ์œ„์ด‰ํ•˜๊ณ , ๋™ ์œ„์›ํšŒ ๋ฐœ์กฑ์‹์„ 6์›” 19์ผ ์˜คํ›„ ๊ด‘ํ™”๋ฌธ ๋„๋ ด๋นŒ๋”ฉ์—์„œ ๊ฐœ์ตœํ•  ์˜ˆ์ •์ด๋‹ค. ์ด๋ฒˆ์— ๋ฐœ์กฑํ•˜๋Š” ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ… ์œ„์›ํšŒ๋Š” ์ง€๋‚œ 2006๋…„ 2์›” 27์ผ ๋ฌธํ™”๊ด€๊ด‘๋ถ€ "๊ณต๊ณต๋„์„œ๊ด€ ์ •์ฑ… ํ˜„ํ™ฉ๊ณผ ๋ฐœ์ „๋ฐฉ์•ˆ"์— ๋Œ€ํ•œ ๋Œ€ํ†ต๋ น ๋ณด๊ณ  ์‹œ, ๋Œ€ํ†ต๋ น๊ป˜์„œ ์šฐ๋ฆฌ๋‚˜๋ผ์˜ ๋„์„œ๊ด€ ๊ด€๋ จ ์ •์ฑ…์„ ์—ฌ๋Ÿฌ ์ •๋ถ€ ๋ถ€์ฒ˜์—์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ด€๊ณ„๋กœ ์กฐ์ •ยทํ˜‘์˜๋ฅผ ์œ„ํ•œ ํŠผ์‹คํ•œ ๊ธฐ๊ตฌ์˜ ํ•„์š”์„ฑ์„ ๊ฐ•์กฐํ•˜์‹œ๊ณ  ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ… ์œ„์›ํšŒ์˜ ์†Œ์†์„ ๋Œ€ํ†ต๋ น ์†Œ์† ํ•˜์— ๋‘๋„๋ก ์ง€์‹œํ•œ ๋ฐ”์— ๋”ฐ๋ฅธ ๊ฒƒ์ด๋‹ค. ์ดํ›„ ๊ด€๋ จ ๋ฒ•์ œ๋ฅผ ๋งˆ๋ จํ•˜๊ธฐ ์œ„ํ•œ ์ ˆ์ฐจ๋ฅผ ๊ฑฐ์ณ ๊ธˆ๋…„ 4์›” ์‹œํ–‰๋œ ๋„์„œ๊ด€ ๋ฒ•์€ ๋Œ€ํ†ต๋ น ์†Œ์† ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ… ์œ„์›ํšŒ ๋ฐ ๋ฌธํ™”๊ด€๊ด‘๋ถ€ ๋‚ด ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ…๊ธฐํš๋‹จ ์„ค๋ฆฝ ๋“ฑ ์ •์ฑ… ์‹œ์Šคํ…œ์˜ ์ „๋ฉด ๊ฐœํŽธ์„ ๊ทœ์ •ํ•˜๊ณ  ์žˆ๋‹ค. ์ง€์‹ ๊ธฐ๋ฐ˜์‚ฌํšŒ์˜ ํ•ต์‹ฌ ๊ฑฐ์ ์œผ๋กœ ๋„์„œ๊ด€์„ ์œก์„ฑยท์ง€์›ํ•˜๊ฒ ๋‹ค๋Š” ์ฐธ์—ฌ ์ •๋ถ€์˜ ์ •์ฑ…์˜์ง€๋ฅผ ํ‘œ๋ฐฉํ•˜๋ฉฐ ๋ฐœ์กฑํ•œ ๋™ ์œ„์›ํšŒ๋Š” ๊ทธ๋™์•ˆ ์ œ๊ธฐ๋˜์—ˆ๋˜ ๋„์„œ๊ด€ ํ–‰์ •์ฒด๊ณ„์˜ ๋ถ„์‚ฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ฐ˜์„ ๋งˆ๋ จํ–ˆ๋‹ค. ๊ณผ๊ฑฐ '๋„์„œ๊ด€ ๋ฐ ๋…์„œ์ง„ํฅ ์œ„์›ํšŒ' ๋“ฑ ์—ฌ๋Ÿฌ ๊ธฐ๊ตฌ๊ฐ€ ๋ฌธํ™”๊ด€๊ด‘๋ถ€ ์žฅ๊ด€์˜ ๋‹จ์ˆœ ์ž๋ฌธ ๊ธฐ๊ตฌ ์—ญํ• ์— ๊ตญํ•œ๋˜์–ด ์‹ค์งˆ์ ์ธ ์šด์˜์ด ๋˜์ง€ ๋ชปํ•œ ํ•œ๊ณ„๊ฐ€ ์žˆ์—ˆ์œผ๋‚˜ ์ƒˆ๋กœ์ด ์„ค๋ฆฝ๋œ ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ… ์œ„์›ํšŒ๋Š” ๋„์„œ๊ด€ ๊ด€๋ จ ์ •๋ถ€๋ถ€์ฒ˜์˜ ์žฅ ๋ฐ ๋ฏผ๊ฐ„ ์ „๋ฌธ๊ฐ€๋ฅผ ์œ„์›์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ๋ฒ”์ •๋ถ€ ์ฐจ์›์˜ ์ •์ฑ… ์ž…์•ˆ๊ณผ ์ง‘ํ–‰๊ธฐ๊ตฌ๋กœ์„œ ํ•„์š”ํ•œ ๋ฒ•์  ํ† ๋Œ€๋ฅผ ํ™•๋ณดํ•œ ๊ฒƒ์ด๋‹ค. ใ€Œ๋„์„œ๊ด€ ๋ฒ•ใ€์ œ12์กฐ์— ์˜๊ฑฐ ์„ค์น˜ยท์šด์˜๋˜๋Š” ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ… ์œ„์›ํšŒ๋Š” ๋ฌธํ™”๊ด€๊ด‘๋ถ€, ๊ต์œก์ธ์ ์ž์›๋ถ€ ๋“ฑ 13๊ฐœ ์ค‘์•™ํ–‰์ •๊ธฐ๊ด€์˜ ์žฅ๊ณผ ๋„์„œ๊ด€์— ๊ด€ํ•œ ์ „๋ฌธ์ง€์‹ ๋ฐ ๊ฒฝํ—˜์ด ํ’๋ถ€ํ•œ ๋ฏผ๊ฐ„์œ„์› ๋“ฑ 26๋ช…์œผ๋กœ ๊ตฌ์„ฑ๋˜์—ˆ์œผ๋ฉฐ, ํ–ฅํ›„ ๋„์„œ๊ด€ ์ •์ฑ…์€ ๋™ ์œ„์›ํšŒ๋ฅผ ํ†ตํ•˜์—ฌ ์ •๋ถ€ ๊ฐ ๋ถ€์ฒ˜์—์„œ ๋ถ„์‚ฐ์ ์œผ๋กœ ์ถ”์ง„๋˜๋Š” ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ…์„ ๊ตญ๊ฐ€ ์ฐจ์›์—์„œ ํ˜‘์˜ยท์กฐ์ •ยทํ‰๊ฐ€ ๋“ฑ์„ ํ†ตํ•ด ์ •์ฑ…์˜ ์ผ๊ด€์„ฑ๊ณผ ์ข…ํ•ฉ์„ฑ์„ ๊ฐ–์ถค์œผ๋กœ์จ ์ •์ฑ…๊ณผ ์˜ˆ์‚ฐ ์ง‘ํ–‰์˜ ํšจ์œจ์„ฑ ์ œ๊ณ ๋ฅผ ๊ธฐ๋Œ€ํ•˜๊ณ  ์žˆ๋‹ค. ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ…๊ธฐํš๋‹จ์€ ๋˜ํ•œ ๋™ ์œ„์› ํšŒ์˜ ๊ธฐ๋Šฅ์„ ๋ณด์ขŒํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธํ™”๊ด€๊ด‘๋ถ€์— ์‹ ์„ค๋˜์—ˆ๋‹ค. ๋‹จ์žฅ์„ ํฌํ•จํ•œ 3ํŒ€ 25๋ช…์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋™ ๊ธฐํš๋‹จ์€ ๋ฌธํ™”๋ถ€ยท๊ต์œก๋ถ€ยทํ–‰์ž๋ถ€ยท์ •ํ†ต๋ถ€ ๋“ฑ ์—ฌ๋Ÿฌ ๋ถ€์ฒ˜๋กœ ๋ถ„์‚ฐ๋œ ๋„์„œ๊ด€ ๊ด€๋ จ ์ •์ฑ…์„ ํ†ตํ•ฉยท์กฐ์ •ํ•˜๊ฒŒ ๋œ๋‹ค. ์ •๋ถ€์—์„œ๋Š” ๋„์„œ๊ด€ ์ •๋ณด์ •์ฑ… ์œ„์›ํšŒ ๋ฐ ๋™ ๊ธฐํš๋‹จ์˜ ๋ฐœ์กฑ์œผ๋กœ ๋„์„œ๊ด€ ์ •์ฑ… ๊ด€๋ จ ์œ ๊ด€ ๋ฒ•๋ น ๋ฐ ์‹œ์ฑ…์„ ๊ฒ€ํ† ยท์กฐ์ •ํ•˜๊ณ  ๋„์„œ๊ด€ ๋ฐœ์ „ ์ข…ํ•ฉ ๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•จ์œผ๋กœ์จ ๊ด€์ข…์„ ํ†ตํ•ฉํ•˜๋Š” ๋„์„œ๊ด€ ์ •์ฑ…์„ ์ถ”์ง„ํ•  ๊ฒƒ์ด๋ฉฐ, ํŠนํžˆ ๊ธ‰์†ํžˆ ๋ณ€ํ™”ํ•˜๊ณ  ์žˆ๋Š” ์ •๋ณด๊ธฐ์ˆ  ํ™˜๊ฒฝ์— ๋„์„œ๊ด€์ด ํšจ๊ณผ์ ์œผ๋กœ ๋Œ€์‘ํ•˜๋„๋ก ์ •๋ณดํ™” ์ข…ํ•ฉ ๊ณ„ํš์„ ์ˆ˜๋ฆฝยท์‹œํ–‰ํ•  ๋ฐฉ์นจ์ด๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: 2007ใ€Œ์ž‘์€๋ฏธ์ˆ ๊ด€ใ€-์—ฌ์ˆ˜๊ณตํ•ญ ์ „์‹œ๋กœ ์‹œ์ž‘ ### ๋ณธ๋ฌธ: ์ •๋ถ€ ๊ณผ์ฒœ ์ฒญ์‚ฌ, ๋Œ€์ „ ํ•œ๋ฐญ๋„์„œ๊ด€, ๊ตญ๋ฆฝ์–ด๋ฆฐ์ด์ฒญ์†Œ๋…„๋„์„œ๊ด€ ๋“ฑ 8ํšŒ ์šด์˜ ๊ตญ๋ฆฝํ˜„๋Œ€๋ฏธ์ˆ ๊ด€(๊ด€์žฅ: <NAME>)์€ ๊ตญ๋ฏผ์˜ ๋ฌธํ™”ํ–ฅ์ˆ˜๊ถŒ ์‹ ์žฅ์„ ์œ„ํ•ด ์ „์‹œ์žฅ ๋ฐ– ์ „์‹œ ์กฐ์„ฑ ์‚ฌ์—…์˜ ์ผํ™˜์œผ๋กœ ์šด์˜ ์ค‘์ธใ€Œ์ž‘์€ ๋ฏธ์ˆ ๊ด€ใ€ Small Art Museum์„ ์˜ฌํ•ด, 2์›” 23์ผ ์•„๋ฆ„๋‹ค์šด ํ•œ๋ ค์ˆ˜๋„ ์ „๋‚จ ์—ฌ์ˆ˜๊ณตํ•ญ์„ ์‹œ์ž‘์œผ๋กœ ์ด 8ํšŒ ์ง„ํ–‰ํ•œ๋‹ค. ๊ธˆ๋…„๋„ ์ฒซ ์ „์‹œ๊ฐ€ ์—ด๋ฆฌ๋Š” ์—ฌ์ˆ˜์‹œ๋Š” ์ธ๊ทผ ๊ตญ๊ฐ€์‚ฐ์—…๋‹จ์ง€์™€ ๋”๋ถˆ์–ด ๋ฐ”๋‹ค์— ์ธ์ ‘ํ•œ ๋‚จ๋„๋งŒ์˜ ์•„๋ฆ„๋‹ค์›€์„ ๊ฐ„์งํ•œ, ๋ฌธํ™”์™€ ์‚ฐ์—…์ด ์–ด์šฐ๋Ÿฌ์ง„ ๊ณ ์žฅ์œผ๋กœ, ์ž‘์€ ๋ฏธ์ˆ ๊ด€์€ ์—ฌ์ˆ˜์‹œ์— ์†Œ์žฌํ•œ ์—ฌ์ˆ˜๊ณตํ•ญ ๋Œ€ํ•ฉ์‹ค์—์„œ ์—ด๋ฆฐ๋‹ค. ใ€Žํ˜„๋Œ€๋ฏธ์ˆ ๊ณผ ํ•œ๊ตญ๋ฏธ์ˆ ใ€์ด๋ผ๋Š” ์ฃผ์ œ๋กœ ์—ด๋ฆฌ๋Š” ๋ณธ ์ „์‹œ๋Š” ์šฐ๋ฆฌ๋‚˜๋ผ๋งŒ์ด ์ง€๋‹ˆ๊ณ  ์žˆ๋Š” ์ž์—ฐ๋ฏธ์™€ ๊ทธ ์†์— ๊ณต์กดํ•˜๋ฉฐ ์‚ด์•„๊ฐ€๋Š” ์‚ฌ๋žŒ๋“ค์˜ ํ˜„๋Œ€์  ๋ฏธํ•™์„ ๋ณด์—ฌ์ฃผ๋Š” ์ž‘ํ’ˆ๋“ค๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ํ•œ๊ตญํ™” 17์ , ์„œ์–‘ํ™” 15์ , ๋ฌธ์ธํ™” 2์ , ํŒํ™” 5์  ๋ฐ ์กฐ๊ฐ 7์ , ๊ณต์˜ˆ ๋ฐ ์‚ฌ์ง„ 6์  ๋“ฑ ํ•œ๊ตญ๋ฏธ๋ฅผ ์•Œ๋ฆฌ๋Š” ์ž‘ํ’ˆ 50์—ฌ ์ ์ด ์ „์‹œ๋œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋‚จ๋„์˜ ์•„๋ฆ„๋‹ค์šด ๊ฒฝ์น˜์™€ ์šฐ๋ฆฌ ์ž‘๊ฐ€๋“ค์˜ ์กฐํ˜•๋ฏธ๋ฅผ ๋™์‹œ์— ๊ฐ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ๊ฐ€ ์ œ๊ณต๋  ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋œ๋‹ค. 2012๋…„ ์„ธ๊ณ„ ํ•ด์–‘์—‘์Šคํฌ ์œ ์น˜๋ฅผ ์œ„ํ•ด ์ ๊ทน์ ์ธ ๋…ธ๋ ฅ์„ ํ•˜๊ณ  ์žˆ๋Š” ์—ฌ์ˆ˜์‹œ์˜ ๊ด€๋ฌธ ๊ฒฉ์ธ ์—ฌ์ˆ˜๊ณตํ•ญ์— ์ž‘์€ ๋ฏธ์ˆ ๊ด€์„ ์กฐ์„ฑํ•จ์œผ๋กœ์จ ๊ณตํ•ญ์„ ํ†ตํ•ด ์ž…๊ตญํ•˜๋Š” ์™ธ๊ตญ์ธ ์‹ค์‚ฌ๋‹จ๊ณผ ๋‚ดยท์™ธ๊ตญ์ธ ๊ด€๊ด‘๊ฐ์˜ ๋งŽ์€ ๊ด€์‹ฌ๊ณผ ํ˜ธํ‰์„ ๋ฐ›์„ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋œ๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ๊ตญ๋ฆฝ๊ตญ์–ด์›, ๊ตญ๋ฏผ์˜ ๊ตญ์–ด๋Šฅ๋ ฅ ์‹คํƒœ ์กฐ์‚ฌ ์‹ค์‹œ ### ๋ณธ๋ฌธ: ๋ฌธํ™”๊ด€๊ด‘๋ถ€ ๊ตญ๋ฆฝ๊ตญ์–ด์›(์›์žฅ <NAME>)์€ 30๋…„ ๋งŒ์— ๊ตญ๋ฏผ์˜ ๊ตญ์–ด๋Šฅ๋ ฅ ์‹คํƒœ ์กฐ์‚ฌ๋ฅผ ์‹ค์‹œํ•˜๋Š”๋ฐ, ์ด๋Š” ๊ตญ๋ฏผ์˜ ๋ฌธ๋งน๋ฅ , ๋ฌธํ•ด๋Šฅ๋ ฅ ์กฐ์‚ฌ์ด๋‹ค. ํ•œ๊ตญ์–ด์˜ ๊ตญ์ œ์  ์œ„์ƒ์€ ํ”„๋ž‘์Šค์–ด, ์ดํƒˆ๋ฆฌ์•„์–ด์™€ ๋น„์Šทํ•œ ์„ธ๊ณ„ 10์œ„๊ถŒ์ด๊ณ  ์™ธ๊ตญ์—์„œ ํ•œ๊ตญ์–ด๋ฅผ ๋ฐฐ์šฐ๋ ค๋Š” ์‚ฌ๋žŒ๋“ค์ด ํ•˜๋ฃจ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์ฆ๊ฐ€ํ•˜๊ณ  ์žˆ๋‹ค. ํ•œ๊ตญ์–ด๊ฐ€ 2007๋…„ 9์›” ์„ธ๊ณ„ ์ง€์‹ ์žฌ์‚ฐ๊ถŒ ๊ธฐ๊ตฌ์— ์˜ํ•ด ์˜์–ด, ์ŠคํŽ˜์ธ์–ด, ์•„๋ž์–ด์— ์ด์–ด ์„ธ๊ณ„์—์„œ ์•„ํ™‰ ๋ฒˆ์งธ๋กœ ๊ตญ์ œ ๊ณต๊ฐœ์–ด๋กœ ์ฑ„ํƒ๋จ์œผ๋กœ์จ ๋ช…์‹ค์ƒ๋ถ€ํ•œ ๊ตญ์ œ์–ด๋กœ ๋ถ€์ƒํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ตญ๋‚ด ์ƒํ™ฉ์€ ์™ธ๋ž˜์–ดยท์™ธ๊ตญ์–ด์˜ ์œ ์ž…๊ณผ ๋ฒ”๋žŒ์ด ์‹ฌํ•ด์ง€๋ฉด์„œ ํ•œ๊ตญ์–ด์— ๋Œ€ํ•œ ์ž๊ธ์‹ฌ์ด ์ ์ฐจ ์•ฝํ•ด์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์™ธ๊ตญ์–ด ๊ต์œก์ด ๊ฐ•์กฐ๋˜๋ฉด์„œ ์™ธ๊ตญ์–ด ํ•™์Šต์˜ ๊ธฐ์ดˆ์ธ ๋ชจ๊ตญ์–ด ๋Šฅ๋ ฅ์˜ ์ค‘์š”์„ฑ์ด ๊ฐ„๊ณผ๋˜๊ณ  ์žˆ๋‹ค. ๋˜ ์šฐ๋ฆฌ ๊ตญ๋ฏผ๋“ค์˜ ๊ตญ์–ด๋Šฅ๋ ฅ์€ ๋งค์šฐ ์šฐ๋ ค๋˜๋Š” ์ˆ˜์ค€์œผ๋กœ ๋ณด๊ณ  ์žˆ๋Š”๋ฐ, ์„œ์šธ์˜ ์ดˆ๋“ฑํ•™๊ต 36ํ•™๋…„ ํ•™์ƒ 100๋ช… ์ค‘ 3~4๋ช…์ด ํ•œ๊ธ€์„ ํ•ด๋…ํ•˜๋Š” ๋ฐ ์–ด๋ ค์›€์„ ๊ฒช๊ณ  ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ๋‹ค. ๋˜ํ•œ ๋‹ค๋ฌธํ™”๊ฐ€์ • ์ž๋…€๋“ค์˜ ๋Œ€๋‹ค์ˆ˜๊ฐ€ ์šฐ๋ฆฌ๋ง๊ณผ ๊ธ€์„ ์“ฐ๋Š” ๋ฐ ์–ด๋ ค์›€์„ ๊ฒช๊ณ  ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๋ ค์ ธ ์ด๋“ค์˜ ๊ตญ์–ด๋Šฅ๋ ฅ ํ–ฅ์ƒ์„ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ์ •์ฑ…์ด ์ ˆ์‹คํ•œ ์‹ค์ •์ด๋‹ค. ์ด์— ๊ตญ๋ฆฝ๊ตญ์–ด์›์€ ๊ตญ์–ด๊ธฐ๋ณธ๋ฒ•์— ์˜๊ฑฐํ•˜์—ฌ ๊ตญ๋ฏผ์˜ ๊ตญ์–ด๋Šฅ๋ ฅ์— ๋Œ€ํ•œ ์‹คํƒœ์กฐ์‚ฌ๋ฅผ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด๋Š” ๊ฐ๊ด€์ ์ธ ์กฐ์‚ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์œ„๊ธฐ์— ์ฒ˜ํ•œ ๊ตญ๋ฏผ์˜ ๊ตญ์–ด๋Šฅ๋ ฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์‹คํšจ์„ฑ ์žˆ๋Š” ์ •์ฑ…์„ ํŽด ๋‚˜๊ฐ€๊ธฐ ์œ„ํ•จ์ด๋‹ค. ์ด๋ฒˆ ์กฐ์‚ฌ๋Š” ์˜ค๋Š” 2~3์›”์˜ ์˜ˆ๋น„์กฐ์‚ฌ์™€ 5~6์›”์˜ ๋ณธ ์กฐ์‚ฌ๋กœ ๋‚˜๋ˆ„์–ด ์ง„ํ–‰๋˜๋ฉฐ, ๋ฌธ๋งน ์—ฌ๋ถ€์™€ ๋ฌธํ•ด๋Šฅ๋ ฅ์˜ ์ •๋„๋ฅผ ์ธก์ •ํ•  ๊ฒƒ์ด๋‹ค. ๊ฐ€์ •๊ณผ ์ง์žฅ์—์„œ ์ผ์ƒ์ƒํ™œ์„ ์˜์œ„ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ฌธ์„œ ํ•ด์„ ๋Šฅ๋ ฅ์„ ๋ฌธํ•ด๋Šฅ๋ ฅ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์„ ์ง„๊ตญ์˜ ๊ฒฝ์šฐ ์˜๋ฌด ๊ต์œก ์ทจํ•™๋ฅ ์ด ๊ฑฐ์˜ 100%์— ๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ๋งน ์—ฌ๋ถ€๋ณด๋‹ค๋Š” ๋ฌธํ•ด๋Šฅ๋ ฅ์— ์ดˆ์ ์„ ๋งž์ถ˜ ์–ธ์–ด๋Šฅ๋ ฅ ์กฐ์‚ฌ๊ฐ€ ์ผ๋ฐ˜์ ์ด๋‹ค. ์ด๋Š” ์‚ฐ์—…์‚ฌํšŒ๋ฅผ ์ง€๋‚˜ ๊ณ ๋„์˜ ์ง€์‹์ •๋ณดํ™” ์‚ฌํšŒ๋กœ ์ง„์ž…ํ•˜๋ฉด์„œ ๋‚˜ํƒ€๋‚˜๋Š” ์ž์—ฐ์Šค๋Ÿฌ์šด ํ˜„์ƒ์œผ๋กœ ๋ฌธํ•ด๋ ฅ์„ ํ–ฅ์ƒ์‹œํ‚ด์œผ๋กœ์จ ๊ตญ๊ฐ€ ๊ฒฝ์Ÿ๋ ฅ์„ ๋†’์ด๊ณ  ์ด๋Ÿฌํ•œ ์กฐ์‚ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋ณด๋‹ค ๋‹ค์–‘ํ•œ ์ •์ฑ…์„ ํŽผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์˜ˆ๋น„์กฐ์‚ฌ๋ฅผ ํ†ตํ•ด ์„ฑ์ธ 500๋ช…, ์ดˆ๋“ฑํ•™๊ต 6ํ•™๋…„ ์ƒ 300๋ช…์„ ๋Œ€์ƒ์œผ๋กœ ์งˆ๋ฌธ์ง€๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ๋ณด์™„ํ•˜์—ฌ ๋ณธ ์กฐ์‚ฌ๋ฅผ ์‹ค์‹œํ•  ๊ณ„ํš์ด๋‹ค. ๋ณธ์กฐ ์‚ฌ๋Š” ์„ฑ์ธ 4,500๋ช…๊ณผ ์ดˆ๋“ฑํ•™์ƒ 1,700๋ช…์„ ๋Œ€์ƒ์œผ๋กœ ์˜ค๋Š” 5์›” ์‹ค์‹œํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋ฒˆ ๊ตญ์–ด๋Šฅ๋ ฅ ์‹คํƒœ ์กฐ์‚ฌ๊ฐ€ ์ด๋ฃจ์–ด์ง€๋ฉด 1970๋…„ ํ†ต๊ณ„์ฒญ ์กฐ์‚ฌ ์ด๋ž˜ 30์—ฌ ๋…„ ๋งŒ์— ์ฒ˜์Œ์œผ๋กœ ๋ฌธ๋งน๋ฅ ์— ๋Œ€ํ•œ ๊ฐ๊ด€์ ์ด๊ณ  ๊ณผํ•™์ ์ธ ํ†ต๊ณ„์น˜๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ตญ๋ฆฝ๊ตญ์–ด์›์€ ๋น„๋ฌธํ•ด์ž๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•œ ๋ฌธํ•ด ๊ต์œก, ๊ตญ์ œ๊ฒฐํ˜ผ ์ด์ฃผ์—ฌ์„ฑ๊ณผ ๊ทธ ์ž๋…€์— ๋Œ€ํ•œ ๋Œ€์ฑ… ๋งˆ๋ จ ๋“ฑ ๊ตญ๋ฏผ ์ „๋ฐ˜์˜ ๊ตญ์–ด๋Šฅ๋ ฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ์ •์ฑ…์„ ์ˆ˜๋ฆฝยท์‹œํ–‰ํ•˜๊ณ  ์ œ์•ˆํ•  ๊ณ„ํš์ด๋‹ค. ๋˜ํ•œ ์œ ๋„ค์Šค์ฝ” ๋“ฑ ๊ด€๋ จ ๊ตญ์ œ๊ธฐ๊ตฌ์—๋„ ์ •ํ™•ํ•œ ๋ฌธ๋งน๋ฅ  ํ†ต๊ณ„์น˜๋ฅผ ์ œ๊ณตํ•˜๊ฒŒ ๋œ๋‹ค. ์ตœ๊ทผ ๊ตฌ๋ฏธ ์„ ์ง„๊ตญ์—์„œ๋Š” ๋‹ค์–‘ํ•œ ๋ฌธํ•ด๋Šฅ๋ ฅ์— ๋Œ€ํ•œ ์—ฐ๊ตฌ์™€ ์‹คํƒœ์กฐ์‚ฌ๊ฐ€ ๊ตญ๊ฐ€์  ์ฐจ์›์—์„œ ์ด๋ฃจ์–ด์ง€๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ถ”์„ธ์— ๋งž์ถ”์–ด ์šฐ๋ฆฌ๋‚˜๋ผ๋„ ๊ตญ๋ฏผ์˜ ๊ตญ์–ด๋Šฅ๋ ฅ ํ–ฅ์ƒ์„ ์œ„ํ•ด ๋ฌธํ•ด๋Šฅ๋ ฅ์„ ๊ทœ๋ช…ํ•˜๊ณ  ๊ตญ์–ด๋Šฅ๋ ฅ ํ–ฅ์ƒ์„ ์œ„ํ•œ ๋Œ€์ฑ…์„ ์‹œ๊ธ‰ํžˆ ์„ธ์›Œ์•ผ ํ•œ๋‹ค. ํ•œ๊ตญ์–ด๊ฐ€ ๊ตญ๊ฐ€ ๋ฐœ์ „์˜ ์›๋™๋ ฅ์ธ ์ด ์‹œ๋Œ€์— ๊ตญ๋ฏผ์˜ ํŠผํŠผํ•œ ๊ตญ์–ด๋Šฅ๋ ฅ์€ ๊ตญ๊ฐ€ ๊ฒฝ์Ÿ๋ ฅ ๊ฐ•ํ™”๋ฅผ ์œ„ํ•ด์„œ๋„ ํ•„์š”ํ•˜๋‹ค. ์ด๋Ÿฐ ํ˜„์‹ค ์ธ์‹์„ ๋ฐ”ํƒ•์œผ๋กœ ํ•œ ์ด๋ฒˆ ๊ตญ์–ด๋Šฅ๋ ฅ ์‹คํƒœ ์กฐ์‚ฌ๊ฐ€ ์ด๋ฃจ์–ด์กŒ๋‹ค. ํ•œํŽธ ๊ตญ๋ฆฝ๊ตญ์–ด์›์€ ์•ž์œผ๋กœ 5๋…„๋งˆ๋‹ค ๊ตญ๋ฏผ์˜ ๊ตญ์–ด๋Šฅ๋ ฅ ์‹คํƒœ๋ฅผ ์กฐ์‚ฌํ•ด ๊ตญ์–ด๋Šฅ๋ ฅ์˜ ๋ณ€ํ™” ์ถ”์ด๋ฅผ ๋ถ„์„ํ•˜๊ณ  ์ด์— ๋”ฐ๋ผ ๊ตญ๋ฏผ์˜ ๊ตญ์–ด๋Šฅ๋ ฅ ํ–ฅ์ƒ์„ ์œ„ํ•œ ์ •์ฑ…์„ ์‹œํ–‰ํ•  ๊ณ„ํš์ด๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ๊ฒŒ์ž„์œ„, โ€˜์œˆ๋„์šฐ ๋น„์Šคํƒ€โ€™๋‚ด์žฅ ๊ฒŒ์ž„๋ฌผ์— ์ฒซ ๋“ฑ๊ธ‰๋ถ„๋ฅ˜ ์‹ ์ฒญ ๋ฐ›์•„ ### ๋ณธ๋ฌธ: ๊ฐœ์ธ์šฉ ์ปดํ“จํ„ฐ(PC) ์šด์šฉ์ฒด์ œ(OS)์˜ ๋‚ด์žฅ ๊ฒŒ์ž„๋ฌผ๋„ ์•ž์œผ๋กœ๋Š” ๋“ฑ๊ธ‰ ๋ถ„๋ฅ˜๋ฅผ ๋ฐ›์•„์•ผ ์œ ํ†ต์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. 2์ผ ๊ฒŒ์ž„๋ฌผ ๋“ฑ๊ธ‰ ์œ„์›ํšŒ(์•ฝ์นญ '๊ฒŒ์ž„์œ„', ์œ„์›์žฅ <NAME>)์— ๋”ฐ๋ฅด๋ฉด, ๊ฐœ์ธ์šฉ ์ปดํ“จํ„ฐ ์šด์šฉ์ฒด์ œ๋กœ๋Š” ์ฒ˜์Œ์œผ๋กœ ๋ฏธ๊ตญ ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ(MS) ์‚ฌ์˜ '์œˆ๋„ ๋น„์Šคํƒ€' ๋‚ด์žฅ ๊ฒŒ์ž„๋ฌผ์˜ ๋“ฑ๊ธ‰ ๋ถ„๋ฅ˜ ์‹ ์ฒญ์ด ์ตœ๊ทผ ์ ‘์ˆ˜๋๋‹ค. ์ด๋ฒˆ ์‹ ์ฒญ์€ ์ง€๋‚œ 2์›” 6์ผ ๊ฒŒ์ž„์œ„๊ฐ€ '์œˆ๋„ ๋น„์Šคํƒ€'์— ํฌํ•จ๋œ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๊ฒŒ์ž„๋ฌผ์— ๋Œ€ํ•ด ๋“ฑ๊ธ‰ ๋ถ„๋ฅ˜ ์‹ ์ฒญ์„ ํ•ด ์ค„ ๊ฒƒ์„ ์ •์‹์œผ๋กœ ์š”์ฒญํ•œ ๋ฐ ๋”ฐ๋ฅธ ๊ฒƒ์ด๋‹ค. ๊ฒŒ์ž„์œ„๋Š” "์œˆ๋„ ๋น„์Šคํƒ€์— ๋‚ด์žฅ๋œ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๊ฒŒ์ž„๋ฌผ๋„ '๊ฒŒ์ž„์‚ฐ์—…์ง„ํฅ์— ๊ด€ํ•œ ๋ฒ•๋ฅ '์— ๋”ฐ๋ผ ๋“ฑ๊ธ‰ ๋ถ„๋ฅ˜ ๋Œ€์ƒ์ด ๋˜๋ฉฐ, ๋“ฑ๊ธ‰์„ ๋ฐ›์€ ๊ฒŒ์ž„๋ฌผ์€ ๋“ฑ๊ธ‰ ๋‚ด์šฉ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค"๋ผ๊ณ  ๋ฐํ˜”๋‹ค. ์ด์— ๋”ฐ๋ผ ๊ฐœ์ธ์šฉ ์ปดํ“จํ„ฐ ์šด์šฉ์ฒด์ œ์˜ ๋‚ด์žฅ ๊ฒŒ์ž„๋ฌผ๋„ ๋“ฑ๊ธ‰ ๋ถ„๋ฅ˜๋ฅผ ๋ฐ›์•„์•ผ ์œ ํ†ต์ด ๊ฐ€๋Šฅํ•ด์กŒ๋‹ค. ์ด๋ฒˆ์— ์‹ ์ฒญํ•œ '์œˆ๋„ ๋น„์Šคํƒ€'์˜ ๋‚ด์žฅ ๊ฒŒ์ž„๋ฌผ์€ '์ฒด์Šค', '์นด๋“œ๋†€์ด', '์ง€๋ขฐ ์ฐพ๊ธฐ', 'ํ•˜ํŠธ', 'ํ”„๋ฆฌ์…€', '๊ตฌ์Šฌ ๋„ฃ๊ธฐ', '์ŠคํŒŒ์ด๋” ์นด๋“œ๋†€์ด', '์ŠคํŒŒ์ด๋” ์นด๋“œ๋†€์ด', '์ŠคํŒŒ์ด๋” ์นด๋“œ๋†€์ด', '๋งˆ์ž‘' ๋“ฑ 9์ข…์ด๋‹ค. ์•ž์œผ๋กœ ๊ฒŒ์ž„์œ„๋Š” ๊ฐœ์ธ์šฉ ์ปดํ“จํ„ฐ ์šด์šฉ์ฒด์ œ ์™ธ์— ๋ชจ๋ฐ”์ผํฐ, ํœด๋Œ€ํ˜• ๋ฉ€ํ‹ฐ๋ฏธ๋””์–ด ํ”Œ๋ ˆ์ด์–ด(PMP), ๋””์ง€ํ„ธ ์…‹ํ†ฑ๋ฐ•์Šค ๋“ฑ์˜ ๋‚ด์žฅ ๊ฒŒ์ž„๋ฌผ์— ๋Œ€ํ•ด์„œ๋„ ๋“ฑ๊ธ‰ ๋ถ„๋ฅ˜ ์‹ ์ฒญ์„ ๋ฐ›์•„ ๋“ฑ๊ธ‰์„ ๋ถ€์—ฌํ•  ๋ฐฉ์นจ์ด๋‹ค. ๋˜ํ•œ ๊ฒŒ์ž„์œ„๋Š” ๋‚ด์žฅ ๊ฒŒ์ž„๋ฌผ์— ๋Œ€ํ•œ ๋“ฑ๊ธ‰ ๋ถ„๋ฅ˜๋ฅผ ํ†ตํ•ด ๊ณผ๋ชฐ์ž… ๋“ฑ ์—ฌ๋Ÿฌ ๋ฌธ์ œ๋ฅผ ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ–ˆ๋‹ค. ํ•œํŽธ, ๊ฒŒ์ž„์œ„๋Š” ์ฒญ์†Œ๋…„ ๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด '์œˆ๋„ ๋น„์Šคํƒ€'์— ๋ณดํ˜ธ์ž ์ œ์–ด ๊ธฐ๋Šฅ(Parental Control)์„ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ์•ˆ์„ MS ์‚ฌ์™€ ํ˜‘์˜ ์ค‘์ธ๋ฐ, ์ƒ๋‹นํ•œ ์ง„์ „์ด ์ด๋ฃจ์–ด์ง„ ๊ฒƒ์œผ๋กœ ์•Œ๋ ค์กŒ๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ํƒœ๊ถŒ๋„, ๋Œ€ํ•œ๋ฏผ๊ตญ์„ ๋Œ€ํ‘œํ•˜๋Š” ์„ธ๊ณ„์  ๋ฌธํ™”์ž์‚ฐ์œผ๋กœ ์œก์„ฑโ€ ### ๋ณธ๋ฌธ: ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€(์žฅ๊ด€ <NAME>๋Š” ํƒœ๊ถŒ๋„ ์ง„ํฅ ๋ฐ ํƒœ๊ถŒ๋„๊ณต์› ์กฐ์„ฑ ๋“ฑ์— ๊ด€ํ•œ ๋ฒ•๋ฅ  ์‹œํ–‰('08.6.22)์— ๋”ฐ๋ฅธ ํƒœ๊ถŒ๋„์ง„ํฅ ๊ธฐ๋ณธ๊ณ„ํš('09~'13)์„ ์ˆ˜๋ฆฝยท๋ฐœํ‘œํ•˜๋ฉด์„œ "ํƒœ๊ถŒ๋„, ๋Œ€ํ•œ๋ฏผ๊ตญ์„ ๋Œ€ํ‘œํ•˜๋Š” ์„ธ๊ณ„์  ๋ฌธํ™”์ž์‚ฐ์œผ๋กœ ์œก์„ฑ" ํ•˜๊ฒ ๋‹ค๊ณ  ๋ฐํ˜”๋‹ค. ์ด๋ฒˆ์— ์ˆ˜๋ฆฝยท๋ฐœํ‘œ๋œ ํƒœ๊ถŒ๋„์ง„ํฅ ๊ธฐ๋ณธ๊ณ„ํš์€ ํƒœ๊ถŒ๋„ ์ง„ํฅ์„ ์œ„ํ•œ ๊ธฐ๋ณธ๋ฐฉํ–ฅ, ํƒœ๊ถŒ๋„์˜ ์„ธ๊ณ„ํ™”, ๊ตญ๊ธฐ ํƒœ๊ถŒ๋„ ์ •๋ฆฝ์„ ์œ„ํ•œ ๊ธฐ๋ฐ˜ ๊ฐ•ํ™”, ์ €๋ณ€ ํ™•๋Œ€, ์„ธ๊ณ„์  ๋ฌธํ™”์‚ฐ์—… ๋ฐ ๊ด€๊ด‘๋ธŒ๋žœ๋“œํ™” ๋“ฑ์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ตœ์ดˆ์˜ ์ •๋ถ€ ์ฐจ์›์˜ ํƒœ๊ถŒ๋„ ์ „๋ฐ˜์„ ํฌ๊ด„ํ•˜๋Š” ์ข…ํ•ฉ ๊ณ„ํš์ด์ž ๋ฒ•์ •๊ณ„ํš์ด๋‹ค. ๊ด€๊ณ„ ๊ธฐ๊ด€๊ณผ ํ˜‘์˜ํ•˜์—ฌ 2013๋…„๊นŒ์ง€ 3185์–ต ์›์„ ์—ฐ์ฐจ ํˆฌ์žํ•  ๊ณ„ํš์ด๋ฉฐ, ์‚ฌ์—…๋น„๋Š” ๊ด€๊ณ„ ๊ธฐ๊ด€๊ณผ ํ˜‘์˜ํ•ด ํ™•๋ณดํ•  ์˜ˆ์ •์ด๋‹ค. ๋น„ <NAME>, ๋Œ€ํ•œ๋ฏผ๊ตญ์„ ๋Œ€ํ‘œํ•˜๋Š” ์„ธ๊ณ„์  ๋ฌธํ™”์ž์‚ฐ - ์Šคํฌ์ธ ์˜ ์˜์—ญ์„ ๋„˜์–ด ์ „ํ†ต, ๋ฌธํ™”, ์‚ฐ์—…์„ ํฌ๊ด„ํ•˜๋Š” ๋ณตํ•ฉ ๋ฌธํ™”์ž์‚ฐ์œผ๋กœ ์œก์„ฑ - โ 4๋Œ€ ์ค‘์  ์ถ”์ง„์ „๋žต ์ „๋žต 1 : ํƒœ๊ถŒ๋„์˜ ์„ธ๊ณ„ํ™” ์ „๋žต 2 : ๊ตญ๊ธฐ ํƒœ๊ถŒ๋„ ์ •๋ฆฝ์„ ์œ„ํ•œ ๊ธฐ๋ฐ˜ ๊ฐ•ํ™” ์ „๋žต 3 : ์ „ ๊ตญ๋ฏผ์ด ์ฆ๊ธฐ๋Š” ์ƒํ™œ ์Šคํฌ์ธ ๋กœ ์ €๋ณ€ ํ™•๋Œ€ ์ „๋žต 4 : ์„ธ๊ณ„์ ์ธ ๋ฌธํ™”์‚ฐ์—… ๋ฐ ๊ด€๊ด‘ ๋ธŒ๋žœ๋“œํ™” ์„ธ๊ณ„ํ™” ๊ฐ•ํ™” ํƒœ๊ถŒ๋„์˜ ์„ธ๊ณ„ํ™” ๊ธฐ๋ฐ˜ ๊ฐ•ํ™” ๊ตญ๊ธฐ ํƒœ๊ถŒ๋„ ์ •๋ฆฝ์„ ์œ„ํ•œ ๊ธฐ๋ฐ˜ ๊ฐ•ํ™” ์ €๋ณ€ ํ™•๋Œ€ ์ „ ๊ตญ๋ฏผ์ด ์ฆ๊ธฐ๋Š” ์ƒํ™œ์Šคํฌ์ธ  ์ €๋ณ€ ํ™•๋Œ€ ๊ธฐ๋ฐ˜ ๊ฐ•ํ™” ํƒœ๊ถŒ๋„ ์‚ฐ์—… ์ด‰์ง„ ์„ธ๊ณ„์  ๋ฌธํ™”์‚ฐ์—… ๋ฐ ๊ด€๊ด‘ ๋ธŒ๋žœ๋“œํ™” ๋Œ€ํ•œ๋ฏผ๊ตญ์„ ๋Œ€ํ‘œํ•˜๋Š” ์„ธ๊ณ„์  ๋ฌธํ™”์ž์‚ฐ ์Šคํฌ์ธ ์˜ ์˜์—ญ์„ ๋„˜์–ด ์ „ํ†ต, ๋ฌธํ™”, ์‚ฐ์—…๊นŒ์ง€ ํฌ๊ด„ํ•˜๋Š” ๋ณตํ•ฉ์  ์ž์‚ฐ์œผ๋กœ ์œก์„ฑ ํƒœ๊ถŒ๋„๋Š” ์šฐ๋ฆฌ ๋ฏผ์กฑ์˜ ์–ผ๊ณผ ์Šฌ๊ธฐ๋ฅผ ๋‹ด์•„ ์‹ฌ์‹ ์„ ์—ฐ๋งˆํ•ด ์˜จ ์ „ํ†ต๋ฌด์˜ˆ์ด์ž ์„ธ๊ณ„ 188๊ฐœ๊ตญ 7์ฒœ์—ฌ๋งŒ ๋ช…์ด ์ˆ˜๋ จํ•˜๊ณ  ์žˆ๋Š” ์šฐ๋ฆฌ๋‚˜๋ผ์˜ ๋Œ€ํ‘œ์ ์ธ ๋ฌธํ™”๋ธŒ๋žœ๋“œ๋กœ์„œ 21์„ธ๊ธฐ ์Šคํฌ์ธ ยท๋ฌธํ™”ยท๊ด€๊ด‘์‚ฐ์—…์˜ ํ•ต์‹ฌ ์ฝ˜ํ…์ธ ๋กœ ์„ฑ์žฅํ•  ์ตœ์ƒ์˜ ๋ฌธํ™” ๋™๋ ฅ์ด ๋  ๊ฒƒ์œผ๋กœ ํ‰๊ฐ€๋˜๊ณ  ์žˆ๋‹ค. ใ€Œํƒœ๊ถŒ๋„์ง„ํฅ ๊ธฐ๋ณธ๊ณ„ํšใ€์€ ์ด๋Ÿฌํ•œ ๊ตญ๊ฐ€ ๋ฐœ์ „์„ ๊ฒฌ์ธํ•  ํƒœ๊ถŒ๋„๋ฅผ ์œ„ํ•˜์—ฌ ์ •๋ถ€๊ฐ€ ํ–ฅํ›„ 2009๋…„๋ถ€ํ„ฐ 2013๋…„๊นŒ์ง€ 5๋…„๊ฐ„ ์‹œํ–‰ํ•  ์ข…ํ•ฉ ๋งˆ์Šคํ„ฐํ”Œ๋žœ(Master Plan)์œผ๋กœ์„œ ๋น„์ „ ๋ฐ ์ด๋ฅผ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ค‘์  ์ถ”์ง„๊ณผ์ œ(Action Plan)์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋‹ค. ํƒœ๊ถŒ๋„, ๋Œ€ํ•œ๋ฏผ๊ตญ์„ ๋Œ€ํ‘œํ•˜๋Š” ์„ธ๊ณ„์  ๋ฌธํ™”์ž์‚ฐ์ด๋ผ๋Š” ๋น„์ „์„ ์„ธ์šฐ๊ณ , ์ด๋ฅผ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ 4๋Œ€ ์ถ”์ง„์ „๋žต, 14๊ฐœ ์ค‘์  ์ถ”์ง„๊ณผ์ œ๋ฅผ ์ด๋ฒˆ ์ข…ํ•ฉ ๊ณ„ํš์—์„œ ์ œ์‹œํ•˜๊ณ  ์žˆ๋‹ค. ๋น„์ „ ๋‹ฌ์„ฑ์„ ์œ„ํ•œ 4๋Œ€ ์ถ”์ง„์ „๋žต ๋ฐ 14๊ฐœ ์ค‘์  ์ถ”์ง„๊ณผ์ œ I-1. ํƒœ๊ถŒ๋„์˜ ์„ธ๊ณ„ํ™” I-1. ๋น„์ „ ๋‹ฌ์„ฑ์„ ์œ„ํ•œ 4๋Œ€ ์ถ”์ง„์ „๋žต ๋ฐ 14๊ฐœ ์ถ”์ง„๊ณผ์ œ ์˜ฌ๋ฆผํ”ฝ ์ฝ”์–ด(Core) ์ข…๋ชฉ ์œ ์ง€๋ฅผ ์œ„ํ•œ ๋„คํŠธ์›Œํฌ ๊ตฌ์ถ• I-2. ๊ธ€๋กœ๋ฒŒ ์Šคํฌ์ธ  ์ง€์› ์‹œ์Šคํ…œ ์ •๋น„(IOC ๊ธฐ์ค€ ์ถฉ์กฑ) I-3. ํƒœ๊ถŒ๋„์˜ ์ „๋žต์  ํ•ด์™ธ ์ง„์ถœ II-1. ํƒœ๊ถŒ๋„์˜ ์ธ๋ฅ˜๋ฌธํ™”์‚ฌ์  ๊ฐ€์น˜ ๋ฐ ์ •์ฒด์„ฑ ๊ทœ๋ช… II-2. ํƒœ๊ถŒ๋„์˜ ์ „๋žต์  ํ•ด์™ธ ์ง„์ถœ II-1. ๊ตญ๊ธฐ ํƒœ๊ถŒ๋„ ์ •๋ฆฝ์„ ์œ„ํ•œ ๊ธฐ๋ฐ˜ ๊ตฌ์ถ• II-2. ํƒœ๊ถŒ๋„ ๋‹จ์ฒด ๊ฐ„ ์—ญํ•  ๋ฐ ํ˜‘๋ ฅ์ฒด๊ณ„ ๊ตฌ์ถ• II-3. ์Šนํ’ˆ๋‹จ ์‹ฌ์‚ฌ ๋ฐ ๊ฒฝ๊ธฐ ์ œ๋„ ์„ ์ง„ํ™” II-4. ์ „ ๊ตญ๋ฏผ์ด ์ฆ๊ธฐ๋Š” ์ƒํ™œ ์Šคํฌ์ธ ๋กœ ์ €๋ณ€ ํ™•๋Œ€ III-1. ์„ฑ์ธ๊ณผ ๊ฐ€์กฑ์ด ์ฐธ์—ฌํ•˜๋Š” ์ƒํ™œ๋ฐ€์ฐฉํ˜• ์Šคํฌ์ธ  ๋ณด๊ธ‰ III-2. ํƒœ๊ถŒ๋„ ์‚ฌ์ด๋ฒ„ ์›”๋“œ ๊ตฌ์ถ• III-1. ํƒœ๊ถŒ๋„ ์‚ฌ์ด๋ฒ„ ์›”๋“œ ๊ตฌ์ถ• III-2. ํ•™๊ต ๋ฐ ์†Œ์™ธ๊ณ„์ธต์— ํƒœ๊ถŒ๋„ ๋ณด๊ธ‰ ํ™•๋Œ€ III-3. ํƒœ๊ถŒ๋„์˜ ์‹ค์šฉ์  ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ III-4. ํƒœ๊ถŒ๋„์žฅ ๊ฒฝ์˜ ํ™œ์„ฑํ™” ์ง€์› VI. ์„ธ๊ณ„์ ์ธ ๋ฌธํ™” ์‚ฐ์—… ๋ฐ ๊ด€๊ด‘๋ธŒ๋žœ๋“œํ™” VI-1. ํƒœ๊ถŒ๋„ ํ•œ๋ฅ˜ ๊ด€๊ด‘ ๊ฑฐ์ ์‹œ์„ค ํ™•์ถฉ VI-2. ์„ธ๊ณ„์ธ์˜ ํƒœ๊ถŒ๋„ ์ถ•์ œ ๋ฐ ๋ฌธํ™”์ƒํ’ˆ ๊ฐœ๋ฐœ โ…ฅ-3. ํƒœ๊ถŒ๋„์˜ ๋ฌธํ™”์‚ฐ์—… ์›์ฒœ ์†Œ์žฌํ™” ๋ฐ ์˜ˆ์ˆ ์ฐฝ์ž‘ ์ง€์› ์ „๋žต 1 (ํƒœ๊ถŒ๋„์˜ ์„ธ๊ณ„ํ™”ใ€•: ์œ ๋„ ๋“ฑ ๊ฒฝ์Ÿ ์ข…๋ชฉ์— ์•ž์„œ๋Š” ๊ธ€๋กœ๋ฒŒ ์Šคํฌ์ธ  IOC๋Š” 2013๋…„ ์ดํšŒ์—์„œ ๊ธฐ์กด 28๊ฐœ ์˜ฌ๋ฆผํ”ฝ ์ข…๋ชฉ์„ 25๊ฐœ ์ฝ”์–ด(Core) ์ข…๋ชฉ์œผ๋กœ ์ถ•์†Œํ•  ์˜ˆ์ •์ด๋‹ค. ์ •๋ถ€๋Š” ํƒœ๊ถŒ๋„๋ฅผ ์˜ฌ๋ฆผํ”ฝ ์ฝ”์–ด ์ข…๋ชฉ์œผ๋กœ ์œ ์ง€ํ•จ์€ ๋ฌผ๋ก  ์œ ๋„ ๋“ฑ ์œ ์‚ฌ์ข…๋ชฉ์— ์›”๋“ฑํžˆ ์•ž์„œ๋Š” ์„ธ๊ณ„์ ์ธ ์Šคํฌ์ธ  ์ข…๋ชฉ์œผ๋กœ ์œก์„ฑํ•˜๊ธฐ ์œ„ํ•˜์—ฌ IOC๊ฐ€ ์š”๊ตฌํ•˜๋Š” ๊ธ€๋กœ๋ฒŒ ์Šคํƒ ๋”๋“œ ํ™•๋ณด, WTF, ๊ตญ๊ธฐ์› ๋“ฑ ๊ด€๋ จ ๋‹จ์ฒด์˜ ๊ธ€๋กœ๋ฒŒ ์ง€์› ์‹œ์Šคํ…œ ๊ตฌ์ถ• ๋“ฑ์˜ ๋ฐฉ์•ˆ์„ ๋งˆ๋ จํ•˜๊ณ , ํƒœ๊ถŒ๋„์˜ ์ „๋žต์  ํ•ด์™ธ ์ง„์ถœ์„ ํ™•๋Œ€ํ•˜๋Š” ์ •์ฑ…์„ ์ˆ˜๋ฆฝํ•˜์˜€์œผ๋ฉฐ ์ด๋Ÿฌํ•œ ์ •์ฑ…๋“ค์€ '<NAME> IOC ์„ ์ˆ˜์œ„์›' ๋‹น์„ ์„ ๊ณ„๊ธฐ๋กœ ํƒ„๋ ฅ์„ ๋ฐ›๊ฒŒ ๋  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋œ๋‹ค. โ–ถ์ค‘์  ์ถ”์ง„๊ณผ์ œ 1 : ์˜ฌ๋ฆผํ”ฝ Core ์ข…๋ชฉ ์œ ์ง€๋ฅผ ์œ„ํ•œ ๋„คํŠธ์›Œํฌ ๊ตฌ์ถ• โ‹… IOC ์Šคํฌ์ธ โ‹…๊ต์œกโ‹…๋ฌธํ™”ํฌ๋Ÿผ, ํƒœ๊ถŒ๋„์˜ ๋‚  ๋“ฑ ๊ณ„๊ธฐ ํ™๋ณด โ‹… WTF ์—ฐ๋ฝ์‚ฌ๋ฌด์†Œ ์ „๋ฌธ ์ธ๋ ฅ ๋ฐฐ์น˜ ๋“ฑ ๊ธฐ๋Šฅ ํ™•์ถฉ(์Šค์œ„์Šค ๋กœ์ž”) โ‹… ํ•ด์™ธ ํ•œ๊ตญ ๋ฌธํ™”์›์˜ ํƒœ๊ถŒ๋„ ๋ณด๊ธ‰ ์ „์ง„๊ธฐ์ง€ํ™”(2013๋…„๊นŒ์ง€ 100์–ต ์› ํˆฌ์ž) โ‹… ํ•ด์™ธ ํƒœ๊ถŒ๋„์žฅ ์ „ํ†ต ์ธํ…Œ๋ฆฌ์–ด ๋ฐ ์†Œ์žฌ ๊ฐœ๋ฐœ โ–ถ ์ค‘์  ์ฃผ์ง„ ๊ณผ์ œ 2 : ๊ธ€๋กœ๋ฒŒ ์Šคํฌ์ธ  ์ง€์› ์‹œ์Šคํ…œ ์ •๋น„ โ‹… WTF ๊ฐœ๋ฐฉ์„ฑ ํ™•๋Œ€(์‚ฌ๋ฌด๊ตญ์˜ ๊ตญ์ œํ™” ์ถ”์ง„) โ‹… ํ•ด์™ธ ๋‹จ์ฆ ๋ฐœ๊ธ‰ ์‹œ์Šคํ…œ ๊ฐœ์„  โ‹… ์„ธ๊ณ„ ํƒœ๊ถŒ๋„ ์•„์นด๋ฐ๋ฏธ(WTA) ์„ค๋ฆฝ(ํƒœ๊ถŒ๋„ ๊ณต์› ๋‚ด) โ–ถ ์ค‘์  ์ถ”์ง„๊ณผ์ œ 3 : ํƒœ๊ถŒ๋„์˜ ์ „๋žต์  ํ•ด์™ธ ์ง„์ถœ ํ™•๋Œ€ โ‹… ๊ตญ๊ฐ€๋Œ€ํ‘œ ํƒœ๊ถŒ๋„ ์‹œ๋ฒ”๋‹จ ์ฐฝ์„คโ‹…์šด์˜(2013๋…„๊นŒ์ง€ 50์–ต ์› ํˆฌ์ž) โ‹… ๊ตญ๋‚ด ์ „๋ฌธ ์ธ๋ ฅ์˜ ํ•ด์™ธ ์ทจ์—… ์ง€์›(์—ฐ๊ฐ„ 10์–ต ์›) ๋ฐ ํ•ด์™ธ ์ทจ์—…๋ฐ•๋žŒํšŒ ๊ฐœ์ตœ โ‹… ๊ฐœ๋„๊ตญ ํƒœ๊ถŒ๋„ ์šฉํ’ˆ ์ง€์› ๋ฐ ์‚ฌ๋ฒ” ํŒŒ๊ฒฌ(์—ฐ๊ฐ„ 10์–ต ์›) ์ „๋žต 2 : ๊ตญ๊ธฐ ํƒœ๊ถŒ๋„ ์ •๋ฆฝ์„ ์œ„ํ•œ ์„ ์ˆœํ™˜ ์‹œ์Šคํ…œ ๊ตฌ์ถ• ํƒœ๊ถŒ๋„๋Š” ์šฐ๋ฆฌ๋‚˜๋ผ์˜ ์ „ํ†ต๋ฌด์˜ˆ์ด๋‚˜ ์ค‘๊ตญ, ์ผ๋ณธ ๋“ฑ์ด ์ž๊ตญ ๊ธฐ์›์„ค์„ ์ฃผ์žฅํ•˜๋Š” ๋“ฑ ์ธ๋ฌธํ•™์  ๊ธฐ๋ฐ˜ ์ทจ์•ฝ์— ๋”ฐ๋ฅธ ๋งŽ์€ ๋ฌธ์ œ์ ์ด ์ œ๊ธฐ๋˜๊ณ  ์žˆ๋‹ค. ํ•œ๊ตญ์˜ ์ „ํ†ต๋ฌด์˜ˆ์ธ ํƒœ๊ถŒ๋„์˜ ํ•™๋ฌธ์  ๊ธฐ๋ฐ˜ ๊ฐ•ํ™”๋ฅผ ์œ„ํ•œ ์ •์ฑ…๊ณผ ํƒœ๊ถŒ๋„ ์„ฑ๋ฆฝ ํ›„ ์•ฝ 40๋…„์ด๋ผ๋Š” ์งง์€ ๊ธฐ๊ฐ„์— ์„ธ๊ณ„์  ์Šคํฌ์ธ ๋กœ ๊ธ‰์†ํ•˜๊ฒŒ ๋ฐœ์ „ํ•˜๋Š” ๊ณผ์ •์—์„œ ์•ผ๊ธฐ๋œ ๊ตญ๋‚ด์™ธ ํ˜ผ์„  ๋ฐ ๋ฌธ์ œ์ ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ฑ…์  ๊ฐœ์„ ๋ฐฉ์•ˆ์„ ๋‹ด๊ณ  ์žˆ๋‹ค. ๊ตญ๊ธฐ์›, ์„ธ๊ณ„ํƒœ๊ถŒ๋„์—ฐ๋งน, ๋Œ€ํ•œ ํƒœ๊ถŒ๋„ ํ˜‘ํšŒ, ํƒœ๊ถŒ๋„์ง„ํฅ์žฌ๋‹จ์˜ ์—ญํ• ์„ ๊ฒฝ๊ธฐ๋ถ€๋ฌธ, ๋ฌด๋„ ๋ถ€๋ฌธ, ์‚ฐ์—…๋ถ€ ๋ฌธ์˜ 3๋Œ€ ์ถ•์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ฐœ์ „์  ์„ ์ˆœํ™˜ ๊ตฌ์กฐ๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. ํŠนํžˆ ๊ตญ๊ธฐ์›์„ '์„ธ๊ณ„ ํƒœ๊ถŒ๋„ ์ค‘์•™๋„์žฅ'์œผ๋กœ ์ทจ์ง€๋ฅผ ์‚ด๋ ค ๋ฌด๋„์˜ ๋ณธ์‚ฐ์œผ๋กœ ์œก์„ฑํ•˜๊ณ  ์Šนํ’ˆ๋‹จ ์‹ฌ์‚ฌ ๋“ฑ ๊ฒฝ๊ธฐ ์ œ๋„๋ฅผ ์„ ์ง„ํ™”ํ•œ๋‹ค. ์ค‘์  ์ถ”์ง„๊ณผ์ œ 1 : ํƒœ๊ถŒ๋„์˜ ์ธ๋ฅ˜ ๋ฌธํ™”์‚ฌ์  ๊ฐ€์น˜ ๋ฐ ์ •์ฒด์„ฑ ๊ทœ๋ช… โ‹… ํƒœ๊ถŒ๋„ํ•™ ์ •๋ฆฝ์„ ์œ„ํ•œ ์—ฐ๊ตฌ ์ง€์› โ‹… ๋ฌด์ˆ  ๊ด€๋ จ ์—ญ์‚ฌ, ์ธ๋ฌธํ•™ ๊ธฐ๋ฐ˜์˜ ๊ตญ์ œ ํ•™์ˆ  ์ฝ˜ํผ๋Ÿฐ์Šค ๋ฐ ํฌ๋Ÿผ ๊ฐœ์ตœ(์—ฐ 1ํšŒ) โ‹… ํƒœ๊ถŒ๋„ ํ•™ํšŒ ์„ค๋ฆฝ ๋ฐ ํ•™์ˆ ์ง€ ๋ฐœ๊ฐ„ โ–ถ ์ค‘์  ์ถ”์ง„๊ณผ์ œ 2 : ํƒœ๊ถŒ๋„ ๋‹จ์ฒด ๊ฐ„ ์—ญํ•  ๋ฐ ํ˜‘๋ ฅ์ฒด๊ณ„ ๊ตฌ์ถ• โ‹… 3๋Œ€ ๊ธฐ๋Šฅ ์ค‘์‹ฌ์˜ ๋‹จ์ฒด ๊ฐ„ ์—ญํ•  ๋ถ„๋‹ด (๊ฒฝ๊ธฐ) : WTF, KTF, (๋ฌด๋„ ๋ฐ ์—ญ์‚ฌ) : ๊ตญ๊ธฐ์›, (์‚ฐ์—…) : ํƒœ๊ถŒ๋„์ง„ํฅ์žฌ๋‹จ โ‹… ๋‹จ์ฒด ๊ฐ„ ์ƒ์„ค ๊ณต๋™ํ˜‘์˜์ฒด ์šด์˜ โ‹… ๊ตญ๊ธฐ์›์„ ๋ฌด๋„(ๆญฆ้“)์˜ ๋ณธ์‚ฐ์œผ๋กœ ์œก์„ฑ(๊ธฐ๋Šฅ ๊ฐ•ํ™”, ๋ฒ•์ •๋ฒ•์ธํ™”์— ๋”ฐ๋ฅธ ๊ณต๊ณต์„ฑ ๊ฐ•ํ™”) โ–ถ ์ค‘์  ์ถ”์ง„๊ณผ์ œ 3 : ์Šนํ’ˆโ‹…๋‹จ ์‹ฌ์‚ฌ ๋ฐ ๊ฒฝ๊ธฐ ์ œ๋„ ์„ ์ง„ํ™” โ‹… ๋‹จ์ฆ ๋ฐœ๊ธ‰ ์˜จ๋ผ์ธ ์‹œ์Šคํ…œ ๋„์ž… ๋ฐ ์‹ฌ์‚ฌ๋น„ ๊ณต์‹œ์ œ ๋„์ž… โ‹… ๊ตญ๋‚ดโ‹…์™ธ ๊ฒฝ๊ธฐ ๊ทœ์น™ ์‹ฌํŒ ๊ทœ์ • ํ†ต์ผ๋กœ ํŒ์ • ์‹œ๋น„ ๊ฐœ์„  (์‹ฌํŒ ์ œ๋„ ๊ฐœ์„  ๋ฐ ์‹ฌํŒ ๊ต์œก ํ”„๋กœ๊ทธ๋žจ ํ‘œ์ค€ํ™” ์ง€์›) โ‹… ํƒœ๊ถŒ๋„ '์‚ฌ์ด๋ฒ„ ์›”๋“œ' ๊ตฌ์ถ• ์ „๋žต 3 : ์ „ ๊ตญ๋ฏผ์ด ์ฐธ์—ฌํ•˜๋Š” ์ƒํ™œ๋ฐ€์ฐฉํ˜• ์Šคํฌ์ธ ๋กœ ์ €๋ณ€ ํ™•๋Œ€ ํƒœ๊ถŒ๋„๋Š” ํ•ด์™ธ์—์„œ ๋Œ€ํ•œ๋ฏผ๊ตญ=ํƒœ๊ถŒ๋„๋ผ๊ณ  ์ธ์‹๋  ์ •๋„์˜ ์„ธ๊ณ„์  ์ธ์ง€๋„๋ฅผ ๋ณด์œ ํ•˜๊ณ  ์žˆ์Œ์—๋„ ์ •์ž‘ ์ข…์ฃผ๊ตญ์ธ ์šฐ๋ฆฌ๋‚˜๋ผ์—์„œ๋Š” ์ˆ˜๋ จ์ธ๊ตฌ ์ „์ฒด์˜ 80 % ๊ฐ€ ์–ด๋ฆฐ์ด์ผ ์ •๋„๋กœ ์ค‘โ‹…์žฅ๋…„์ธต์—์„œ ์™ธ๋ฉด๋ฐ›๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŽธ์ค‘๋œ ํƒœ๊ถŒ๋„ ์ˆ˜๋ จ ํ˜„์ƒ์„ ๊ทน๋ณตํ•˜๊ณ  ์ˆ˜๋ จ์ธ๊ตฌ๋ฅผ ๋‹จ๊ธฐ๊ฐ„์— 23๋ฐฐ๋กœ ๋Š˜๋ฆฌ๊ณ  ๊ณ ๋ฆฝ๋œ ๊ฐœ์ธ ์œ„์ฃผ์˜ ๊ฐ€์กฑ๊ด€๊ณ„๋ฅผ ๊ทน๋ณตํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฌธํ™” ํŠธ๋ Œ๋“œ๋กœ ์œก์„ฑํ•œ๋‹ค. ํ•™๊ต ํƒœ๊ถŒ๋„๋ฅผ ์ •๋ฆฝํ•˜๊ณ , ์†Œ์™ธ๊ณ„์ธต์— ๋Œ€ํ•œ ํƒœ๊ถŒ๋„ ๋ณด๊ธ‰ ๋“ฑ ํƒœ๊ถŒ๋„์˜ ๊ณต๊ณต์  ๊ธฐ๋Šฅ๊ณผ ์‹ค์šฉ์  ๊ธฐ๋Šฅ์„ ๊ฐ•ํ™”ํ•˜๋ฉฐ, ํƒœ๊ถŒ๋„์žฅ ๊ฒฝ์˜ ํ™œ์„ฑํ™”๋ฅผ ์ง€์›ํ•˜๋Š” ์ •์ฑ…์„ ๋งˆ๋ จํ•œ๋‹ค. ์ค‘์  ์ถ”์ง„๊ณผ์ œ 1 : ์„ฑ์ธโ‹…๊ฐ€์กฑ์ด ์ฐธ์—ฌํ•˜๋Š” ์ƒํ™œ๋ฐ€์ฐฉํ˜• ์Šคํฌ์ธ  ๋ณด๊ธ‰ โ‹… ๊ฐ€์กฑ ์ˆ˜๋ จ ํ”„๋กœ๊ทธ๋žจ ๊ฐœ๋ฐœ ๋ฐ ์บ ํŽ˜์ธ ์ „๊ฐœ โ‹… "Best ํƒœ๊ถŒ ๊ฐ€์กฑ ์„ ๋ฐœ๋Œ€ํšŒ"๊ฐœ์ตœ โ‹… ํƒœ๊ถŒ๋„ ์ด ๋Ÿฌ๋‹(e-Learning) ํ”„๋กœ๊ทธ๋žจ ๊ฐœ๋ฐœ โ‹… ์ „๊ตญ ๋‹จ์œ„ ์ƒํ™œ์ฒด์œก ํƒœ๊ถŒ๋„ ๊ฒฝ์—ฐ ๋Œ€ํšŒ ๊ฐœ์ตœ โ–ถ ์ค‘์  ์ถ”์ง„๊ณผ์ œ 2 : ํ•™๊ต ์†Œ์™ธ๊ณ„์ธต ํƒœ๊ถŒ๋„ ๋ณด๊ธ‰ ํ™•๋Œ€ โ‹… ํ•™๊ต ์ฒด์œก ํƒœ๊ถŒ๋„ ํ”„๋กœ๊ทธ๋žจ ๊ฐœ๋ฐœ โ‹… ํ•™๊ต ์ˆœํšŒ "ํƒœ๊ถŒ๋„ ๊ต์œก ์‹œ๋ฒ”๋‹จ" ์šด์˜ โ‹… ์†Œ์™ธ๊ณ„์ธต์„ ์œ„ํ•œ "ํƒœ๊ถŒ๋„ ๋ฐ”์šฐ์ฒ˜"๋„์ž… โ–ถ ์ค‘์  ์ถ”์ง„๊ณผ์ œ 3 : ํƒœ๊ถŒ๋„์˜ ์‹ค์šฉ์  ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ โ‹… ํƒœ๊ถŒ๋„ ๋ช…์ƒ์ˆ˜๋ จ, ์‹ฌ๋ฆฌ์น˜๋ฃŒ, ์˜ˆ์ˆ ์น˜๋ฃŒ ํ”„๋กœ๊ทธ๋žจ ๊ฐœ๋ฐœ โ‹… ํƒœ๊ถŒ๋„ ์šด๋™์ฒ˜๋ฐฉ(ํด๋ฆฌ๋‹‰) ์‹œ๋ฒ”์‚ฌ์—… โ–ถ ์ค‘์  ์ถ”์ง„๊ณผ์ œ 4 : ํƒœ๊ถŒ๋„์žฅ ๊ฒฝ์˜ ํ™œ์„ฑํ™” ์ง€์› โ‹… ํƒœ๊ถŒ๋„์žฅ์˜ ํ•™๊ตโ‹…์‚ฌํšŒ๊ต์œก ์ง€์›์„ผํ„ฐํ™” โ‹… ์ผ์„  ํƒœ๊ถŒ๋„ ๊ฒฝ์˜๊ฐœ์„  ์ง€์›์„ ์œ„ํ•œ ๋„์žฅ ๊ฒฝ์˜ํฌ๋Ÿผ ๊ฐœ์ตœ(์ง€์—ญ๋ณ„ ์—ฐ๊ฐ„ 4ํšŒ) โ‹… ์šฐ์ˆ˜ ๋„์žฅ ๊ฒฝ์˜์ž์ƒ ์ œ์ •(ํƒœ๊ถŒ๋„์˜ ๋‚  ๋“ฑ๊ณผ ์—ฐ๊ณ„) โ‹… ๋„์žฅ์‹œ์„ค ๊ฐœ์„  ์œต์ž์‚ฌ์—…(์Šคํฌ์ธ ์‚ฐ์—… ์œต์ž์‚ฌ์—… ๋“ฑ๊ณผ ์—ฐ๊ณ„) ์ „๋žต 4 : ์ œ2, ์ œ3์˜ ๋กœ๋ด‡ ํƒœ๊ถŒ V ๋“ฑ ์ฝ˜ํ…์ธ  ์‚ฐ์—… ๋ฐ ํƒœ๊ถŒ ํ•œ๋ฅ˜ ๊ด€๊ด‘ ์ด‰์ง„ ํƒœ๊ถŒ๋„๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋กœ๋ด‡ ํƒœ๊ถŒ V, ๋ฎค์ง€์ปฌ JUMP ๋“ฑ ์„ฑ๊ณต์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ๋‹จ์ˆœ ์Šคํฌ์ธ ๋ฅผ ๋„˜์–ด ๋Œ€ํ•œ๋ฏผ๊ตญ์„ ์ƒ์ง•ํ•˜๋Š” ๋†’์€ ๋ธŒ๋žœ๋“œ ๊ฐ€์น˜์™€ ๋ฌธํ™”์‚ฐ์—…์œผ๋กœ์˜ ๋ฐœ์ „ ์ž ์žฌ๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , '์ข…์ฃผ๊ตญ' ํ”„๋ฆฌ๋ฏธ์—„์„ ํ†ตํ•ด ๋งŽ์€ ํ•ด์™ธ ๊ด€๊ด‘๊ฐ์„ ์œ ์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ์—ฐ๊ด€ ์‚ฐ์—…์œผ๋กœ์˜ ๋ฐœ์ „์„ ๊ฒฌ์ธํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ˜ํ…์ธ ์™€ ์„ธ๊ณ„์  ๋ฌธํ™”์ด๋ฒคํŠธ ๊ฐœ๋ฐœ ๋“ฑ ๊ด€๊ด‘ ๋ฐ ์‚ฐ์—… ๋ฐœ์ „๋ฐฉ์•ˆ์„ ์ œ์‹œํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ˆ˜๋„๊ถŒ์— ํƒœ๊ถŒ๋„ ์ƒ์„ค๊ณต์—ฐ์žฅ์„ ๊ฑด๋ฆฝํ•˜์—ฌ ์™ธ๊ตญ์ธ์„ ์œ„ํ•œ ์—ฐ๊ฐ„ ์ƒ์‹œ ๊ณต์—ฐ ์ƒํ’ˆํ™”ํ•˜๊ณ , ์Šนํ’ˆ๋‹จ ์‹ฌ์‚ฌ๋ฅผ ์ง€์—ญ๋ณ„ ๋ช…ํ’ˆ ์˜๋ก€ ์ถ•์ œ๋กœ ๊ฐœ๋ฐœํ•˜๊ณ , ๊ตญ์ œ ํ”„๋กœํƒœ๊ถŒ๋„๋Œ€ํšŒ ์ฐฝ์„ค์„ ๊ฒ€ํ† ํ•œ๋‹ค. ํƒœ๊ถŒ๋„ ์†Œ์žฌ์˜ ๋งŒํ™”. ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋“ฑ ํ‚ฌ๋Ÿฌ ์ฝ˜ํ…์ธ ์™€ ๊ณต์—ฐ์˜ˆ์ˆ  ์ฐฝ์ž‘์„ ์ง€์›ํ•œ๋‹ค. ์ค‘์  ์ถ”์ง„๊ณผ์ œ 1 : ํƒœ๊ถŒ๋„ ํ•œ๋ฅ˜ ๊ด€๊ด‘ ๊ฑฐ์ ์‹œ์„ค ํ™•์ถฉ โ‹… ํƒœ๊ถŒ๋„ ๊ณต์›์„ ์„ธ๊ณ„ ํƒœ๊ถŒ๋„์ธ์˜ ์„ฑ์ง€๋กœ ์กฐ์„ฑ(์ด ์‚ฌ์—…๋น„ 6,009์–ต ์›) โ‹… ์ˆ˜๋„๊ถŒ์— ํƒœ๊ถŒ๋„ ์ƒ์„ค ๊ณต์—ฐ์žฅ ๊ฑด๋ฆฝ โ–ถ ์ค‘์  ์ถ”์ง„๊ณผ์ œ 2 : ์„ธ๊ณ„์ธ์˜ ํƒœ๊ถŒ๋„ ์ถ•์ œ ๋ฐ ๊ด€๊ด‘์ƒํ’ˆ ๊ฐœ๋ฐœ โ‹…"ํƒœ๊ถŒ๋„์˜ ๋‚ (9์›” 4์ผ)", "์„ธ๊ณ„ ํƒœ๊ถŒ๋„ ๋ฌธํ™”์—‘์Šคํฌ"๋ฅผ ํƒœ๊ถŒ๋„ ๋ถ„์•ผ ๋Œ€ํ‘œ ์ถ•์ œ๋กœ ๊ฐœ๋ฐœ โ‹…๊ตญ์ œ ํ”„๋กœํƒœ๊ถŒ๋„ ๋Œ€ํšŒ(World Series) ์ฐฝ์„ค โ‹…์Šนํ’ˆโ‹…๋‹จ ์‹ฌ์‚ฌ๋ฅผ ์ง€์—ญ๋ณ„ ๋ช…ํ’ˆ ์˜๋ก€ ์ถ•์ œ๋กœ ๊ฐœ๋ฐœ(์ฃผ๋ง ์ „์šฉ ๊ฒฝ๊ธฐ์žฅ ์ง€์ •) โ–ถ ์ค‘์  ์ถ”์ง„๊ณผ์ œ 2 : ์„ธ๊ณ„์ธ์˜ ํƒœ๊ถŒ๋„ ์ถ•์ œ ๋ฐ ๊ด€๊ด‘์ƒํ’ˆ ๊ฐœ๋ฐœ โ‹… ํƒœ๊ถŒ๋„ ๊ณ ์ฆ์„ ํ†ตํ•œ "๋ฌธํ™” ์›ํ˜• ๋ฐœ๊ตด์‚ฌ์—…" โ‹… ํƒœ๊ถŒ๋„ ์†Œ์žฌ ํ‚ฌ๋Ÿฌ ์ฝ˜ํ…์ธ  ๊ฐœ๋ฐœ์‚ฌ์—… โ‹… ํƒœ๊ถŒ๋„ ์‘์šฉ์˜ˆ์ˆ  ์ฐฝ์ž‘ ํ™œ์„ฑํ™” ์‚ฌ์—…(๋ฎค์ง€์ปฌ, ํ–‰์œ„์˜ˆ์ˆ  ๋“ฑ) ์ •๋ถ€๋Š” ๊ธฐ๋ณธ๊ณ„ํš์˜ ์ˆ˜๋ฆฝ์„ ์œ„ํ•˜์—ฌ ํƒœ๊ถŒ๋„ ๊ด€๋ จ ๋‹จ์ฒด, ํ•™๊ณ„ ๋ฐ ํ˜„์žฅ ์ „๋ฌธ๊ฐ€ ๊ทธ๋ฆฌ๊ณ  ํ† ๋ก ํšŒ ๋“ฑ์„ ํ†ตํ•ด ์˜๊ฒฌ์„ ์ˆ˜๋ ดํ•ด ์™”๋‹ค. ์•ž์œผ๋กœ ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€๋Š” ํƒœ๊ถŒ๋„ ์ข…์ฃผ๊ตญ์œผ๋กœ์„œ์˜ ์œ„์ƒ์„ ๋†’์ด๊ณ  ์„ธ๊ณ„์ธ์ด ์ธ์ •ํ•˜๋Š” ์Šคํฌ์ธ ๋กœ ์ž๋ฆฌ๋งค๊น€ํ•  ์ˆ˜ ์žˆ๋„๋ก ํƒœ๊ถŒ๋„์ง„ํฅ ๊ธฐ๋ณธ๊ณ„ํš์„ ์ฐจ์งˆ ์—†์ด ์ถ”์ง„ํ•ด ๋‚˜๊ฐˆ ๊ณ„ํš์ด๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ์ด ๋‹ฌ(4์›”)์˜ ์šฐ์ˆ˜ ์‹ ์ธ์Œ๋ฐ˜ ์„ ์ • ### ๋ณธ๋ฌธ: ์ด ๋‹ฌ(4์›”)์˜ ์šฐ์ˆ˜ ์‹ ์ธ์Œ๋ฐ˜์œผ๋กœ ๋ฝ‘ํ˜”๋‹ค. 4์›”์˜ ์šฐ์ˆ˜ ์‹ ์ธ์Œ๋ฐ˜์œผ๋กœ ๋ฌธํ™”๊ด€๊ด‘๋ถ€(์žฅ๊ด€ <NAME>)์™€ ํ•œ๊ตญ ๋ฌธํ™”์ฝ˜ํ…์ธ ์ง„ํฅ์›(์›์žฅ <NAME>์€ ํฌ์ฝ”์Šค(<NAME>)"์˜ Recall to One, s mind- (์ฃผ) ์ œ์ด์—์ด์น˜์ฝ”์˜ค์Šค ๋ฏธ๋””์–ด ์ œ์ž‘-์„ ์„ ์ •ํ–ˆ๋‹ค. 3์ผ ์˜ค์ „ 11์‹œ ๋ฌธํ™”๊ด€๊ด‘๋ถ€ ์ฐจ๊ด€์‹ค์—์„œ <NAME> ์ฐจ๊ด€์ด ์ฐธ์„ํ•œ ๊ฐ€์šด๋ฐ ์‹œ์ƒ์‹์ด ์—ด๋ ธ๋‹ค. 1์ฐจ ์ „๋ฌธ๊ฐ€ ์‹ฌ์‚ฌ์—์„œ "<NAME>(sei)", "<NAME>์™€ ํ•จ๊ป˜ ์ตœ์ข… ํ›„๋ณด๋กœ ์„ ์ •๋œ Recall to One, s mind๋Š” 4์›” ํ•œ ๋‹ฌ๊ฐ„ ์ผ€์ด๋ธ” TV ์— ๋„ท(m.net)๊ณผ ์Œ์•…์ „๋ฌธ ํฌํ„ธ์‚ฌ์ดํŠธ์ธ ์— ๋„ท๋‹ท์ปด ๋“ฑ ์˜จ ์˜คํ”„๋ผ์ธ์—์„œ ํ™๋ณด๋œ๋‹ค. ์ดํ›„ ์‹ค์‹œ๋œ 2์ฐจ ๋ˆ„๋ฆฌ๊พผ ํˆฌํ‘œ์—์„œ ์ด 53,343ํ‘œ ์ค‘ 26,721ํ‘œ(50 % )์˜ ์ง€์ง€๋ฅผ ์–ป์–ด 4์›”์˜ ์šฐ์ˆ˜ ์‹ ์ธ์Œ๋ฐ˜์œผ๋กœ ์„ ์ •๋๋‹ค. <NAME>์˜ ์†Œ์†์‚ฌ ์†Œ์žฅ ์ค‘ (์ฃผ) ์ œ์ด์—์ด์น˜์ฝ”์˜ค์Šค ๋ฏธ๋””์–ด ๋Œ€ํ‘œ๋Š” "์ด๋ฒˆ ์ˆ˜์ƒ์€ ์ด์ œ ๋ง‰ ๊ฐ€์š”๊ณ„์— ๋ฐœ์„ ๋“ค์—ฌ๋†“์€ ์‹ ์ธ๊ฐ€์ˆ˜์—๊ฒŒ๋Š” ๋“ ๋“ ํ•œ ๋ฒ„ํŒ€๋ชฉ๊ณผ ๊ฐ™๋‹ค"๋ผ๋ฉฐ "์†Œ์ˆ˜์˜ ํ†ฑ์Šคํƒ€๊ฐ€ ํ•œ๊ตญ ๊ฐ€์š”๊ณ„๋ฅผ ์ด๋Œ์–ด๊ฐ€๋Š” ํ˜„์‹ค์—์„œ ์‹ ์ธ์—๊ฒŒ๋Š” ์ƒ๊ฐ๋ณด๋‹ค ๋งŽ์€ ๊ธฐํšŒ๊ฐ€ ์ฃผ์–ด์ง€์ง€ ์•Š์ง€๋งŒ ์ด๋ฒˆ ์ˆ˜์ƒ์„ ๊ณ„๊ธฐ๋กœ <NAME>๊ฐ€ ํ™œ๋ฐœํ•˜๊ฒŒ ํ™œ๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅ์„ ์•„๋ผ์ง€ ์•Š๊ฒ ๋‹ค"๋ผ๊ณ  ๋งํ–ˆ๋‹ค. ํŒ ๋ฐœ๋ผ๋“œ, ๋งˆ์ด๋„ˆ ๋ฐœ๋ผ๋“œ ๋“ฑ ๋‹ค์–‘ํ•œ ์Œ์•…์„ฑ์„ ์ง€๋‹Œ ์‹ ์ธ๊ฐ€์ˆ˜ <NAME>๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์žฅ๋ฅด์˜ ์Œ์•…์ƒํ™œ์„ ํ•˜๋˜ <NAME>, <NAME>, <NAME> ์ด 4๋ช…์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ํŒ€์ด๋‹ค. ํŠนํžˆ ๊ทธ๋ฃน๋ช…์€ 4๊ฐ€์ง€ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์šฐ์ฃผ๋ผ๋Š” ๋œป์„ ๋‹ด๊ณ  ์žˆ๋Š”๋ฐ ํ•œ๊ตญ์ธ์€ 3๋ช…, ์ผ๋ณธ์ธ์€ 1๋ช…์ด๋‹ค. ์ง€๋‚œ 3์›” 2์ผ ๋ฐœ๋งค๋œ ๋ฐ๋ท” ์•จ๋ฒ” Recall to One, s mind๋Š” 4๋ช…์˜ ํ•˜๋ชจ๋‹ˆ๊ฐ€ ์ง‘์•ฝ๋œ 11๊ณก๊ณผ ๊ฐ์ž์˜ ์ƒ‰๊น”๊ณผ ๊ฐœ์„ฑ์„ ๋‹ด์•„ ๋ถ€๋ฅธ ์†”๋กœ๊ณก 4๊ณก์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. ํ”Œ๋ผ์ด ํˆฌ ๋” ์Šค์นด์ด ๋“ฑ์˜ ์•จ๋ฒ”์— ์ฐธ์—ฌํ–ˆ๋˜ ์ž‘๊ณก๊ฐ€ <NAME>๊ณผ<NAME>๊ฐ€ ์ฐธ์—ฌํ•ด ์•จ๋ฒ”์˜ ์™„์„ฑ๋„๋ฅผ ๋†’์˜€๋‹ค. ํ•œํŽธ ๊ตญ๋‚ด ์Œ๋ฐ˜์‹œ์žฅ์— ์ƒˆ๋กœ์šด ํ™œ๊ธฐ๋ฅผ ๋ถˆ์–ด๋„ฃ๊ธฐ ์œ„ํ•ด ์ง„ํ–‰๋˜๊ณ  ์žˆ๋Š” ์‹ ์ธ์Œ๋ฐ˜ ๋ฐœ๊ตด ๋ฐ ํ™๋ณด ์ง€์› ์‚ฌ์—… '์ด ๋‹ฌ์˜ ์šฐ์ˆ˜ ์‹ ์ธ์Œ๋ฐ˜ ์„ ์ • ์‚ฌ์—…'์€ ๋ˆ„๋ฆฌ์ง‘(www.mnet.com/hotpick)์„ ์ƒˆ๋กญ๊ฒŒ ์—ด๊ณ , ์— ๋„ท(M.net)๊ณผ ์ผ๊ฐ„ ์Šคํฌ์ธ ๋ฅผ ๋น„๋กฏํ•œ ๊ฐ์ข… ์œ ๋ฌด์„  ๋งค์ฒด๋ฅผ ํ†ตํ•ด ์ ๊ทน์ ์œผ๋กœ ์‚ฌ์—… ํ™๋ณด์— ๋‚˜์„œ๊ณ  ์žˆ๋‹ค. โ€ป๋ฌธํ™”๊ด€๊ด‘๋ถ€๊ฐ€ ์ฃผ๊ด€ํ•˜๋Š” ์ด๋ฒˆ ๋‹ฌ์˜ ์šฐ์ˆ˜ ์‹ ์ธ์Œ๋ฐ˜ ์„ ์ • ์‚ฌ์—…์€ ์นจ์ฒด๋œ ๊ตญ๋‚ด ์Œ๋ฐ˜์‹œ์žฅ์„ ํ™œ์„ฑํ™”ํ•˜๊ณ  ์Œ์•…์‚ฐ์—…์˜ ์ฐฝ์ž‘ ๊ธฐ๋ฐ˜์„ ๋งˆ๋ จํ•˜๋Š” ๋ฐ ๊ทธ ๋ชฉ์ ์ด ์žˆ๋‹ค. ๋ฌธํ™”๊ด€๊ด‘๋ถ€๋Š” ๋งค์›” ์šฐ์ˆ˜ํ•œ ์‹ ์ธ์Œ๋ฐ˜์„ ์„ ์ •ํ•˜์—ฌ ํ™๋ณด ๋ฐ ํ”„๋กœ๋ชจ์…˜์„ ์ง€์›ํ•จ์œผ๋กœ์จ ์‹ ์ธ ์Œ์•… ์ฝ˜ํ…์ธ ์˜ ์–‘์  ์งˆ์  ํ–ฅ์ƒ์„ ๋•๊ณ  ์žˆ๋‹ค. ์ง€๋‚œ 3์›”์—๋Š” ์‹ ์ธ๊ฐ€์ˆ˜ ์œคํ˜•๋ ฌ์˜ ์œคํ˜•๋ ฌ 1: (ํŽธ์•ˆํ• :์ผ) ์ง‘- <NAME> ์ œ์ž‘- ์ด ์šฐ์ˆ˜ ์‹ ์ธ์Œ๋ฐ˜์— ์„ ์ •๋˜์–ด ์–ธ๋ก ์ด๋‚˜ ๋ฐฉ์†ก์—์„œ ํฐ ์ธ๊ธฐ๋ฅผ ๋Œ์—ˆ๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: โ€œ๋ฌธํ™”๋ถ€, P2P ยท ์›นํ•˜๋“œ ์ด 31๊ฐœ ์—…์ฒด์— ๊ณผํƒœ๋ฃŒ ๋ถ€๊ณผโ€ ### ๋ณธ๋ฌธ: "๋ฌธํ™”๋ถ€, P2P ยท ์›นํ•˜๋“œ ์ด 31๊ฐœ ์—…์ฒด์— ๊ณผํƒœ๋ฃŒ ๋ถ€๊ณผ" ๋ฌธํ™”๊ด€๊ด‘๋ถ€(์žฅ๊ด€ <NAME>)๋Š” ํŠน์ˆ˜ํ•œ ์œ ํ˜•์˜ '์˜จ๋ผ์ธ ์„œ๋น„์Šค ์ œ๊ณต์ž'(์ดํ•˜ "OSP")์˜ ์˜ํ™”ยท์Œ์•… ์ €์ž‘๋ฌผ์— ๋Œ€ํ•œ ๊ธฐ์ˆ ์  ์กฐ์น˜ 4์ฐจ ๋ชจ๋‹ˆํ„ฐ๋ง('07.12.5~12.8) ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ์ด 31๊ฐœ P2Pยท์›นํ•˜๋“œ ์—…์ฒด์— 210๋งŒ ์›์—์„œ 2,500๋งŒ ์›๊นŒ์ง€ ๊ณผํƒœ๋ฃŒ๋ฅผ ์ฐจ๋“ฑ ๋ถ€๊ณผํ–ˆ๋‹ค. ์ด๋ฒˆ ๊ณผํƒœ๋ฃŒ๋Š” ์ž‘๋…„ 8์›”๋ถ€ํ„ฐ 11์›”๊นŒ์ง€ 3์ฐจ๋ก€ ๊ฑธ์นœ ๋ชจ๋‹ˆํ„ฐ๋ง๊ณผ ๊ฒฝ๊ณ ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  4์ฐจ ๋ชจ๋‹ˆํ„ฐ๋ง์—์„œ ์ถฉ๋ถ„ํ•œ ๊ธฐ์ˆ ์  ์กฐ์น˜(๋ฏธ ์ฐจ๋‹จ์œจ 5% ์ดํ•˜)๋ฅผ ์ทจํ•˜์ง€ ์•Š์€ ์—…์ฒด์— ๋ถ€๊ณผ๋˜์—ˆ์œผ๋ฉฐ, 4์ฐจ ๋ชจ๋‹ˆํ„ฐ๋ง ๋Œ€์ƒ 38๊ฐœ ์—…์ฒด ์ค‘ 31๊ฐœ ์—…์ฒด๊ฐ€ ํ•ด๋‹น๋˜์—ˆ๋‹ค.ํ•„ํ„ฐ๋ง์œจ์ด ๋†’์€ 5๊ฐœ ์—…์ฒด, ์‚ฌ์ดํŠธ๊ฐ€ ํ์‡„๋œ 2๊ฐœ ์—…์ฒด ๋“ฑ ์ด 7๊ฐœ ์—…์ฒด๊ฐ€ ๊ณผํƒœ๋ฃŒ๋ฅผ ๋‚ด์ง€ ์•Š์•˜์ง€๋งŒ, ์ตœ๊ณ ์•ก 2,500๋งŒ ์›์„ ๋ถ€๊ณผ ๋ฐ›์€ ๊ณณ์€ 2๊ฐœ ์—…์ฒด์ด๋‹ค. ์—…์ฒด๋ณ„ ๊ฒฐ๊ณผ ๋ณ„์ฒจ ์ฐธ์กฐ(์—…์ฒด๋ช… ์˜๋ฌธ ๋จธ๋ฆฌ๊ธ€์ž ๋“ฑ ํ‘œ๊ธฐ) ๋ฌธํ™”๋ถ€๋Š” ์ด๋ฒˆ ๊ณผํƒœ๋ฃŒ ๋ถ€๊ณผ ์ „์— '07.12.14์ผ ๊ณผํƒœ๋ฃŒ ์˜ˆ์ • ๊ธˆ์•ก ๋“ฑ์„ ํ†ต๋ณดํ•˜๊ณ  12์ผ๊ฐ„('07.12.17~28) ์˜๊ฒฌ ์ง„์ˆ  ๊ธฐ๊ฐ„์„ ๋‘์—ˆ๋‹ค. ์—…์ฒด ์˜๊ฒฌ ์ค‘ ์Œ์› DNA ๊ธฐ์ˆ  ๋„์ž… ๋“ฑ ์ž๊ตฌ ๋…ธ๋ ฅ์„ ์ถฉ๋ถ„ํžˆ ์†Œ๋ช…ํ•˜์˜€๊ฑฐ๋‚˜ ์˜์„ธ ์‚ฌ์—…์ž(์—ฐ ๋งค์ถœ 4,800๋งŒ ์› ์ดํ•˜, ๋ถ€๊ฐ€๊ฐ€์น˜์„ธ๋ฒ•์ƒ ๊ฐ„์ด๊ณผ์„ธ๋Œ€์ƒ์ž ๊ธฐ์ค€ ์ธ์šฉ) ์ž„์„ ์ฆ๋ช…ํ•œ ๊ฒฝ์šฐ ์ด๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ ๊ณผํƒœ๋ฃŒ ์˜ˆ์ • ๊ธˆ์•ก์˜ 20~30%๋ฅผ ๊ฐ๊ฒฝํ•˜์˜€๋‹ค. ์ด๋ฒˆ์— ๊ณผํƒœ๋ฃŒ๋ฅผ ๋ถ€๊ณผ ๋ฐ›์€ ์—…์ฒด๋Š” ์ฒ˜๋ถ„์„ ๋ฐ›์€ ๋‚ ๋กœ๋ถ€ํ„ฐ 30์ผ ์ด๋‚ด์— ์ด์˜ ์ œ๊ธฐ๋ฅผ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ๊ฒฝ์šฐ ํ•ด๋‹น ์‚ฌํ•ญ์€ ๋ฒ•์›์— ํ†ต๋ณด๋˜์–ด ๋น„์†ก์‚ฌ๊ฑด์ ˆ์ฐจ๋ฒ•์— ๋”ฐ๋ฅธ ๊ณผํƒœ๋ฃŒ ์žฌํŒ์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ์ด์˜ ์ œ๊ธฐ๋ฅผ ํ•˜์ง€ ์•Š๊ณ  ๊ธฐํ•œ ๋‚ด ๊ณผํƒœ๋ฃŒ๋ฅผ ๋‚ด์ง€ ์•Š์„ ๊ฒฝ์šฐ ๊ตญ์„ธ ์ฒด๋‚ฉ์ฒ˜๋ถ„์˜ ์˜ˆ์— ๋”ฐ๋ผ ์ง•์ˆ˜๋œ๋‹ค. ์ด๋ฒˆ 4์ฐจ ๋ชจ๋‹ˆํ„ฐ๋ง ๊ฒฐ๊ณผ ์ „์ฒด์ ์œผ๋กœ ์˜ํ™”๋ถ€๋ฌธ์—์„œ ์ง€๋‚œ 1ยท2ยท3์ฐจ ๋ชจ๋‹ˆํ„ฐ๋ง์— ๋น„ํ•ด ๊ธฐ์ˆ ์  ์กฐ์น˜๊ฐ€ ํฌ๊ฒŒ ํ–ฅ์ƒ๋œ ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ๋Š”๋ฐ, ์ด๋Š” ์ง€๋‚œ 1ยท2์ฐจ ๋ชจ๋‹ˆํ„ฐ๋ง๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ์ด๋‹ค. ์Œ์•…์˜ ๊ฒฝ์šฐ๋„ ์ƒ๋Œ€์ ์œผ๋กœ ์–‘ํ˜ธํ•œ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ–ˆ๋‹ค. ์˜ํ™”(50ํŽธ)์˜ ๊ฒฝ์šฐ ์กฐ์‚ฌ๋Œ€์ƒ ์ „์ฒด ์‚ฌ์ดํŠธ์—์„œ ํ‰๊ท  32.5% (1์ฐจ 67.6% 2์ฐจ 59.2% 3์ฐจ 44% 4์ฐจ 44% )๋ฅผ ๋‹ค์šด๋กœ๋“œ ๊ฐ€๋Šฅํ•˜์—ฌ ์ตœ์ดˆ 1์ฐจ ๋ชจ๋‹ˆํ„ฐ๋ง('07. 8์›”)์— ๋น„ํ•ด 35% p ์ด์ƒ ๋‚˜์•„์กŒ๋‹ค. ์Œ์•…(100๊ณก)์˜ ๊ฒฝ์šฐ ๋ฏธ์ฐจ๋‹จ์œจ์ด ํ‰๊ท  11.2%๋กœ 3์ฐจ(26.3%)์— ๋น„ํ•ด ์†Œํญ(3% ) ๊ฐœ์„ ๋˜์–ด 3์ฐจ๋ณด๋‹ค ๊ณ„์†ํ•ด์„œ ๋‚˜์•„์กŒ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ์ •๋ถ€์˜ ๊ณผํƒœ๋ฃŒ ๋ถ€๊ณผ ์กฐ์น˜์— ๋ฏผ๊ฐํ•˜๊ฒŒ ๋ฐ˜์‘ํ•˜๋Š” P2Pยท์›นํ•˜๋“œ ์—…๊ณ„๊ฐ€ ํ•„ํ„ฐ๋ง์œจ์„ ๋†’์ด๊ณ  ์ •๋ถ€์˜ ์ ๊ทน์ ์ธ ๋ถˆ๋ฒ• ์ €์ž‘๋ฌผ ๊ทผ์ ˆ ์˜์ง€๊ฐ€ ์–ด๋Š ์ •๋„ ์„ฑ๊ณผ๋ฅผ ๋‚ด๊ณ  ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. ๋ฌธํ™”๋ถ€๋Š” ์•ž์œผ๋กœ ์˜ํ™”ยท์Œ์•…๋ฟ ์•„๋‹ˆ๋ผ ๊ฒŒ์ž„ยท์ถœํŒยท๋ฐฉ์†ก ์ €์ž‘๋ฌผ๊นŒ์ง€ P2Pยท์›นํ•˜๋“œ์˜ ๊ธฐ์ˆ ์  ์กฐ์น˜์— ๋Œ€ํ•œ ๋ชจ๋‹ˆํ„ฐ๋ง์„ ํ™•๋Œ€ํ•œ๋‹ค๋Š” ๊ณ„ํš์ด๋ฉฐ, ํฌํ„ธ์— ๋Œ€ํ•œ ์‚ญ์ œ ๋ช…๋ น๊ถŒ ๋ฐœ๋™(์ €์ž‘๊ถŒ๋ฒ• ์ œ133์กฐ) ๋“ฑ๋„ ๋ณ‘ํ–‰ํ•˜์—ฌ ์ถ”์ง„ํ•ด ์˜จ๋ผ์ธ์ƒ์˜ ๋ถˆ๋ฒ• ์ €์ž‘๋ฌผ ๊ทผ์ ˆ์„ ๊ฐ•ํ™”ํ•ด ๋‚˜๊ฐ„๋‹ค๋Š” ๋ฐฉ์นจ์ด๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ์ฆ๊ฑฐ์šด ์„ฑํƒ„์ ˆ๊ณผ ์—ฐ๋ง์—ฐ์‹œ๋Š” ๋Œ€ํ•œ๋ฏผ๊ตญ ๊ตฌ์„๊ตฌ์„์—์„œ!! ### ๋ณธ๋ฌธ: ๋ฌธํ™”๊ด€๊ด‘๋ถ€(์žฅ๊ด€ <NAME>)์™€ ํ•œ๊ตญ๊ด€๊ด‘๊ณต์‚ฌ(์‚ฌ์žฅ <NAME>)๋Š” ๊ตญ๋‚ด๊ด€๊ด‘ ํ™œ์„ฑํ™”๋ฅผ ์œ„ํ•˜์—ฌ ์„ฑํƒ„์ ˆ ๋ฐ ์—ฐ๋ง์—ฐ์‹œ์— ๊ฐ€๋ณผ ๋งŒํ•œ ๋ช…์†Œ๋ฅผ ์„ ์ •, ์ด๋“ค ๋ช…์†Œ๋ฅผ ์ฃผ์š” ์ฝ”์Šค๋กœ ํ•˜๋Š” ๊ตญ๋‚ด์—ฌํ–‰์ƒํ’ˆ์„ ๊ตญ๋‚ด์—ฌํ–‰์‚ฌ์—ฐํ•ฉํšŒ์™€ ๊ณต๋™๊ฐœ๋ฐœํ•˜๊ณ , ์ถ”์ฒจ์— ์˜ํ•ด ๋ช…์†Œ ๋ฐฉ๋ฌธ์ž์—๊ฒŒ ๊ฒฝํ’ˆ์„ ์ œ๊ณตํ•˜๋Š”'๊ตฌ์„๊ตฌ์„ ๋‘˜๋Ÿฌ๋ณด๊ธฐ (Travel Rally Korea-season 1)'ํ–‰์‚ฌ๋ฅผ ๊ฐœ์ตœํ•œ๋‹ค.์˜ค๋Š” 12์›” 15์ผ๋ถ€ํ„ฐ 2008๋…„ 1์›” 14์ผ๊นŒ์ง€ ํ•œ ๋‹ฌ๊ฐ„ ์ง„ํ–‰๋˜๋Š” '๊ตฌ์„๊ตฌ์„ ๋‘˜๋Ÿฌ๋ณด๊ธฐ (Travel Rally Korea-season 1)'์€ ๊ตฌ์„๊ตฌ์„ ๋‘˜๋Ÿฌ๋ณด๊ธฐ (Travel Rally Korea-season 1)์€ ์ฃผ๋ฌธ์ง„ ์†Œ๋Œ๋ฐ”์œ„, ์•ˆ๋ฉด๋„ ์•ˆ๋ฉด์•”, ์šธ์‚ฐ ๋Œ€์™•์•”, ์šธ๋ฆ‰๋„ ๋…๋„์ผ์ถœ์ „๋ง๋Œ€ 4๊ฐœ์†Œ์˜ ์ผ์ถœ ์ผ๋ชฐ ๋ช…์†Œ, ์„œ์ฒœ ๋งˆ๋Ÿ‰์ง„, ๋ถ€์•ˆ ๋‚ด์†Œ์‚ฌ ์ˆฒ๊ธธ, ๋Œ€์ „ ์žฅํƒœ์‚ฐ ์ž์—ฐ ํœด์–‘๋ฆผ 3๊ฐœ์†Œ์˜ ํฌ๋ฆฌ์Šค๋งˆ์Šค ๋ช…์†Œ์™€ ๋ฌธ๊ฒฝ ์˜จ์ฒœ, ์‚ฐ์ฒญ ์ฐธ์ˆฏ๊ตด, ๋‹น์ง„ ์™œ๋ชฉ๋งˆ์„ 3๊ฐœ์†Œ ์˜ ๋”ฐ๋œปํ•œ ๊ฒจ์šธ์ด๋ผ๋Š” 3๊ฐ€์ง€ ํ…Œ๋งˆ๋กœ ๋‚˜๋‰˜์–ด ์ด 10๊ฐœ ๋ช…์†Œ์—์„œ ์ง„ํ–‰๋œ๋‹ค.(๊ทธ๋ฆผ ์šธ์‚ฐ ๋Œ€์™•์•”)์œ„์˜ ๋ช…์†Œ๋ฅผ ๋ฐฉ๋ฌธํ•˜์—ฌ ํ˜„์žฅ์— ๋น„์น˜๋œ ์‘๋ชจํ•จ์— ์ ‘์ˆ˜ํ•˜๊ฑฐ๋‚˜ ํ˜„์žฅ ํฌํ† ์กด์„ ๋ฐฐ๊ฒฝ์œผ๋กœ ์ดฌ์˜ํ•œ ์‚ฌ์ง„์„ ์ด๋ฒคํŠธ ํŽ˜์ด์ง€(www.visitkorea.or.kr)์— ๋“ฑ๋กํ•˜๋ฉด ๋ณธ ํ–‰์‚ฌ์— ์ฐธ์—ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์•ฝ 200๋ช…์—๊ฒŒ 3,000๋งŒ ์› ์ƒ๋‹น์˜ ๊ฒฝํ’ˆ์ด ์ค€๋น„๋œ ์ด๋ฒˆ ์ด๋ฒคํŠธ์—๋Š” ๊ฐ€์žฅ ๋งŽ์€ ์ง€์—ญ์„ ๋ฐฉ๋ฌธํ•œ ๊ด€๊ด‘๊ฐ์—๋Š” ๋ถ€์ƒ์œผ๋กœ 150๋งŒ ์› ์ƒ๋‹น์˜ ํ™ฉ๊ธˆ์นด๋“œ ๋“ฑ์ด ์ฃผ์–ด์ง€๊ฒŒ ๋˜๋ฉฐ, ์ตœ๋‹ค์ง€์—ญ ๋ฐฉ๋ฌธ์ž ์ƒ์œ„ 100๋ช…์—๊ฒŒ๋„ ํ–‰์‚ฌ ๊ธฐ๋…ํ’ˆ ๋ฐ ์ง€๋ฐฉ ํ† ์‚ฐํ’ˆ์ด ์ฃผ์–ด์ง€๊ฒŒ ๋œ๋‹ค. 1๊ฐœ ์ง€์—ญ๋งŒ์„ ๋ฐฉ๋ฌธํ•ด๋„ ์ถ”์ฒจ๊ถŒ์ด ์ฃผ์–ด์ง€๋ฉฐ, ์‚ฌ์ง„ ๋ฐ ์ˆ˜๊ธฐ๊ณต๋ชจ ์ด๋ฒคํŠธ ๋“ฑ์„ ํ†ตํ•ด์„œ๋„ ๊ตญ๋‚ด ์—ฌํ–‰์ƒํ’ˆ๊ถŒ ๋ฐ ๋‹ค์–‘ํ•œ ๊ธฐ๋…ํ’ˆ๋“ค์ด ์ฐธ๊ฐ€์ž๋“ค์—๊ฒŒ ์ฃผ์–ด์งˆ ๊ณ„ํš์ด๋‹ค.์ด์™€ ๋”๋ถˆ์–ด, ๋ฌธํ™”๊ด€๊ด‘๋ถ€์™€ ํ•œ๊ตญ๊ด€๊ด‘๊ณต์‚ฌ๋Š” ๊ตญ๋‚ด์—ฌํ–‰์‚ฌ์—ฐํ•ฉํšŒ์™€ ์†์„ ์žก๊ณ  ํ•ด์™ธ์—ฌํ–‰ ์ˆ˜์š”๋ฅผ ๊ตญ๋‚ด๋กœ<NAME>๊ธฐ ์œ„ํ•˜์—ฌ ์„ฑํƒ„์ ˆ๊ณผ ์—ฐ๋ง์—ฐ์‹œ์— ใ€Ž์ถ”์ฒœ ๊ฐ€๋ณผ ๋งŒํ•œ ๊ณณใ€์„ ์ค‘์‹ฌ์œผ๋กœ ๊ตญ๋‚ด์—ฌํ–‰์ƒํ’ˆ์„ ๊ฐœ๋ฐœํ•˜์˜€๋‹ค. "๋‚จ์› ๊ตญ์•…์บ๋Ÿด์Œ์•…ํšŒ์™€ ์ž„์‹ค ์น˜์ฆˆ ๋งŒ๋“ค๊ธฐ"์™€ ๊ฐ™์€ ์„ฑํƒ„์ ˆ ์ƒํ’ˆ 9๊ฐœ์™€ "ํ•ด๋‚จ ๋•…๋๋งˆ์„ ํ•ด๋‹์ด์™€ ๋Œ€ํฅ์‚ฌ ๊ธฐ์ฐจ์—ฌํ–‰"๋“ฑ์˜ ์‹ ๋…„์ƒํ’ˆ 6๊ฐœ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ์—ฌํ–‰์ƒํ’ˆ ์ฐธ๊ฐ€ํ›„๊ธฐ๋ฅผ ์˜ฌ๋ฆฐ ์ƒํ’ˆ ๊ตฌ๋งค์ž 50๋ช…์—๊ฒŒ๋Š” ํ•œ๊ตญ๊ด€๊ด‘๊ณต์‚ฌ์—์„œ ์ฃผ์ตœํ•˜๋Š” "๊ตฌ์„๊ตฌ์„ ๊ตญ๋‚ด์—ฌํ–‰"์— ๋ฌด๋ฃŒ ์ดˆ์ฒญ(1์ธ๋™๋ฐ˜ ๊ฐ€๋Šฅ)ํ•  ๊ณ„ํš์ด๋‹ค.๋ฌธํ™”๊ด€๊ด‘๋ถ€์™€ ํ•œ๊ตญ๊ด€๊ด‘๊ณต์‚ฌ๋Š” ์˜ฌํ•ด ์—ฐ๋ง์—ฐ์‹œ ํ•ด์™ธ์—ฌํ–‰์ด ์‚ฌ์ƒ ์ตœ๋Œ€๋กœ 12์›” ์ถœ๊ตญ์ž๊ฐ€ ์ž‘๋…„๋Œ€๋น„ 30 % ์ฆ๊ฐ€ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ๊ฐ€์šด๋ฐ, ๊ตฌ์„๊ตฌ์„ ๋‘˜๋Ÿฌ๋ณด๊ธฐ (Travel Rally Korea-season 1) ์ด๋ฒคํŠธ์™€ ์„ฑํƒ„์ ˆ ๋ฐ ์—ฐ๋ง์—ฐ์‹œ ๊ตญ๋‚ด์—ฌํ–‰์ƒํ’ˆ ๊ฐœ๋ฐœ๋กœ ํ•ด์™ธ์—ฌํ–‰ ์ˆ˜์š”๋ฅผ ๊ตญ๋‚ด๋กœ<NAME>๊ณ  ๊ตญ๋ฏผ๋“ค์˜ ๋‹ค์–‘ํ•œ ์—ฌํ–‰์š•๊ตฌ๋ฅผ ์ถฉ์กฑ์‹œํ‚ฌ ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ•˜๊ณ  ์žˆ๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: 2008 ํ•œ์Šคํƒ€์ผ ๋ฐ•๋žŒํšŒ ### ๋ณธ๋ฌธ: 2008 ํ•œ์Šคํƒ€์ผ ๋ฐ•๋žŒํšŒ - ์ด๋‹ฌ 7์›” 31์ผ(๋ชฉ)๋ถ€ํ„ฐ 8์›” 3์ผ(์ผ)๊นŒ์ง€ ์ฝ”์—‘์Šค ํƒœํ‰์–‘ํ™€์—์„œ ๊ฐœ์ตœ - ํ•œ๊ธ€, ํ•œ์‹, ํ•œ๋ณต, ํ•œ์˜ฅ, ํ•œ์ง€, ํ•œ๊ตญ์Œ์•… ๋“ฑ 6๊ฐœ ๋ถ„์•ผ ์ „ํ†ต๋ฌธํ™” ๋Œ€ํ‘œ ์ฝ˜ํ…์ธ  ์ฐธ์—ฌ - ์•„์‹œ์•„์ฒญ์†Œ๋…„์ถ•์ „, ๊ตญ์•…์ฝ˜์„œํŠธ, ํ•œ๊ธ€์ฒดํ—˜์ „ ๋“ฑ ๋‹ค์ฑ„๋กœ์šด ๊ณต์—ฐ๊ณผ ์ฒดํ—˜ ์ง„ํ–‰ ๋ฐ•๋žŒํšŒ ๊ฐœ์š” ๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€(์žฅ๊ด€ <NAME>)๋Š” ๋†๋ฆผ์ˆ˜์‚ฐ์‹ํ’ˆ๋ถ€, ๊ตญํ† ํ•ด์–‘๋ถ€, ์ „์ฃผ์‹œ์™€ ๊ณต๋™์œผ๋กœ 2008๋…„ 7์›” 31์ผ(๋ชฉ)๋ถ€ํ„ฐ 8์›” 3์ผ(์ผ)๊นŒ์ง€ ์ฝ”์—‘์Šค ํƒœํ‰์–‘ํ™€์—์„œ '2008 ํ•œ์Šคํƒ€์ผ๋ฐ•๋žŒํšŒ'๋ฅผ ๊ฐœ์ตœํ•œ๋‹ค. 'ํ•œ์Šคํƒ€์ผ'์€ ์šฐ๋ฆฌ ๋ฌธํ™” ์›๋ฅ˜๋กœ์„œ ๋Œ€ํ‘œ์„ฑ๊ณผ ์ƒ์ง•์„ฑ์„ ๊ฐ–๋Š” ์ „ํ†ต๋ฌธํ™” ์ค‘ ์‚ฐ์—…ํ™” ๊ฐ€๋Šฅ์„ฑ๊ณผ ์„ธ๊ณ„ํ™” ํ•„์š”์„ฑ์ด ๋†’์€ ํ•œ๊ธ€, ํ•œ์‹, ํ•œ๋ณต, ํ•œ์˜ฅ, ํ•œ์ง€, ํ•œ๊ตญ์Œ์•… 6๊ฐœ ๋ถ€๋ฌธ์„ ์„ ์ •ํ•˜์—ฌ ์ •์ฑ…์  ์ง€์›์„ ํ•จ์œผ๋กœ์จ ๋ถ€๊ฐ€๊ฐ€์น˜์™€ ๊ตญ๊ฐ€ ์ด๋ฏธ์ง€๋ฅผ ๋†’์ด๋Š” ์‚ฌ์—…์ด๋‹ค. '2008 ํ•œ์Šคํƒ€์ผ๋ฐ•๋žŒํšŒ'๋Š” ์šฐ๋ฆฌ์˜ ์ „ํ†ต๋ฌธํ™”๋ฅผ ํ˜„๋Œ€์  ์ฝ˜ํ…์ธ  ์‚ฐ์—…์œผ๋กœ ๋ฐœ์ „์‹œํ‚จ ๋ถ„์•ผ๋ณ„ ์šฐ์ˆ˜ ๊ธฐ์—…์ด ์ฐธ์—ฌํ•˜์—ฌ ํ•œ์Šคํƒ€์ผ ์‚ฐ์—…์˜ ํ˜„์žฌ์˜ ๋ฏธ์…˜๊ณผ ๋ฏธ๋ž˜์˜ ๋น„์ „์„ ์ œ์‹œํ•˜๊ฒŒ ๋˜๋ฉฐ, ์ด๋ฒˆ ๋ฐ•๋žŒํšŒ๋Š” 88๊ฐœ ์—…์ฒด 350๋ถ€์Šค(์ฃผ์ œ๊ด€ ํฌํ•จ)๊ฐ€ ์ฐธ์—ฌํ•œ๋‹ค.๋ฌธํ™”์ฒด์œก๊ด€๊ด‘๋ถ€๋Š” ์ด๋ฅผ ํ†ตํ•ด ์ „ํ†ต๋ฌธํ™”์ฝ˜ํ…์ธ  ์‚ฐ์—…์˜ ์—ญ๋Ÿ‰์„ ๊ฒฐ์ง‘ํ•˜๊ณ  ์šฐ์ˆ˜ ์ฝ˜ํ…์ธ ๋ฅผ ๋ฐœ๊ตด, ํ™•์‚ฐํ•˜์—ฌ ์ƒํ™œํ™”, ์‚ฐ์—…ํ™”, ์„ธ๊ณ„ํ™”์— ๊ณ„๋Ÿ‰ํ•  ์ˆ˜ ์žˆ๋Š” ์„ฑ๊ณผ๋ฅผ ์ด๋ฃจ๊ฒ ๋‹ค๋Š” ์ „๋žต์„ ๊ฐ–๊ณ  ์žˆ๋‹ค. ๋˜ํ•œ ์ด๋ฒˆ ๋ฐ•๋žŒํšŒ์—๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ํ”„๋กœ๊ทธ๋žจ์— ์ง‘์ค‘ํ•˜์—ฌ ์ „ํ†ต๋ฌธํ™”์‚ฐ์—… ์ข…์‚ฌ์ž์—๊ฒŒ ์‹ค์งˆ์ ์ธ ์ด์ต์ด ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ฒ ๋‹ค๋Š” ๊ณ„ํš์ด๋‹ค.๊ฐ ๋ถ„์•ผ๋ณ„๋กœ ๊ตญ๋‚ด์™ธ ๋ฐ”์ด์–ด์™€ ์—…๊ณ„ ์ข…์‚ฌ์ž๋ฅผ ์ดˆ์ฒญํ•ด 1๋Œ€1 ๋ฏธํŒ…๊ณผ ๊ณต๊ฐœ ํ”„๋ ˆ์  ํ…Œ์ด์…˜, ๊ธฐ์—…ํƒ๋ฐฉ ํ”„๋กœ๊ทธ๋žจ ๋“ฑ์ด ์ง„ํ–‰๋œ๋‹ค. ํ•œ์Šคํƒ€์ผ ๋Œ€ํ‘œ ๋ธŒ๋žœ๋“œ ์ฐธ์—ฌ๋Š” ํ•œ๊ธ€๋ถ€๋ฌธ์—์„œ๋Š” ๋Œ€ํ‘œ์ ์ธ ์บ˜๋ฆฌ๊ทธ๋ผํ”ผ ์—…์ฒด์ธ 'ํ•„๋ฌต'(์˜ํ™” '๋ณต์ˆ˜๋Š” ๋‚˜์˜ ๊ฒƒ', ์„œ์  '๋ด‰์ˆœ์ด์–ธ๋‹ˆ' ์ œ๋ชฉ๊ธ€์”จ๋ฅผ ์ œ์ž‘)์ด, ํ•œ์‹๋ถ€๋ฌธ์—์„œ๋Š” ์ „์ฃผ์ „ํ†ต๋น„๋น”๋ฐฅ ํ”„๋žœ์ฐจ์ด์ฆˆ์ธ '๊ณ ๊ถF&B'๊ฐ€ ๋‹ด๋‹นํ•œ๋‹ค. ํ•œ๋ณต์€ ์ƒํ™œํ•œ๋ณต ๋ธŒ๋žœ๋“œ '๋Œ์‹ค๋‚˜์ด'๊ฐ€ ์ด๋ฏธ ์ฐธ์—ฌ ์˜์‚ฌ๋ฅผ ๋ฐํ˜”๊ณ , ํ•œ์˜ฅ์€ ๊ฒฝ์ฃผ ํ•œ์˜ฅํ˜ธํ…” ๋ผ๊ถ์„ ์‹œ๊ณตํ•œ '์ด์—ฐ๊ฑด์ถ•'์ด ํ•จ๊ป˜ํ•œ๋‹ค. ํ•œ์ง€์—์„œ๋Š” ์šฐ๋ฆฌ๋‚˜๋ผ ๋Œ€ํ‘œ ํ•œ์ง€์ œ์กฐ์‚ฌ์ธ '์ฒœ์–‘์ œ์ง€'๊ฐ€ ์ฐธ์—ฌํ•ด ํ•œ์ง€์˜ ์šฐ์ˆ˜์„ฑ๊ณผ ๋‹ค์–‘ํ•œ ํ™œ์šฉ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•  ์˜ˆ์ •์ด๋‹ค. ํ•œ๊ตญ์Œ์•…์—์„œ๋Š” ๊ตญ๋‚ด ์ตœ๋Œ€ ๊ตญ์•…๊ธฐ์ œ์กฐ์‚ฌ์ธ '๋‚œ๊ณ„๊ตญ์•…๊ธฐ์ œ์ž‘์ดŒ'์—์„œ ํ˜„๋Œ€์  ๊ตญ์•…๊ธฐ ์ „์‹œ์™€ ์ฒดํ—˜์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ•๋žŒํšŒ๋ฅผ ์ฆ๊ธฐ๋Š” 7๊ฐ€์ง€ Tip ๋จผ์ € '2008 ํ•œ์Šคํƒ€์ผ๋ฐ•๋žŒํšŒ' ์— ๋ธ”๋Ÿผ์€ TV๋“œ๋ผ๋งˆ '๋Œ€์™•์„ธ์ข…', '์—„๋งˆ๊ฐ€ ๋ฟ”๋‚ฌ๋‹ค' ๋“ฑ์„ ์“ด ์บ˜๋ฆฌ๊ทธ๋ผํผ <NAME>๋‹˜์˜ ์ž‘ํ’ˆ์œผ๋กœ ์„ธ๋ จ๋˜๋ฉด์„œ๋„ ์ž์œ ๋กœ์šด ๋Š๋‚Œ์„ ์ค€๋‹ค. '์•„์‹œ์•„์ฒญ์†Œ๋…„์ถ•์ „'์€ ์•„์‹œ์•„ 22๊ฐœ๊ตญ ์ฒญ์†Œ๋…„๋“ค์ด ๋ชจ์—ฌ ์ง„ํ–‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๊ฐ ๊ตญ์˜ ๋ฌธํ™”๋ฅผ ๋ฐ•๋žŒํšŒ์— ์„ ๋ณด์ด๊ฒŒ ๋œ๋‹ค.์ด๋Š” ๋ฐ•๋žŒํšŒ์˜ ์ปจ์…‰์ธ ๋ฌธํ™”์˜ ์ผ๋ฐฉ์  ์ด์‹๊ณผ ๋‹จํŽธ์  ์ฝ˜ํ…์ธ ์˜ ์ˆ˜์ถœ๋ณด๋‹ค๋Š” ๋ฌธํ™”์˜ ๋‹ค์–‘์„ฑ๊ณผ ๊ต๋ฅ˜์˜ ์Œ๋ฐฉํ–ฅ์„ฑ์„ ์‹คํ˜„ํ•œ ๊ธฐํš์ด๊ธฐ๋„ ํ•˜๋‹ค. ์ด๋“ค์€ ๋ฌผ๋ก  ์ž๊ตญ์˜ ์Œ์‹, ์˜์ƒ, ๊ณต์—ฐ ๋ฌธํ™”๋ฅผ ์†Œ๊ฐœํ•จ๊ณผ ๋™์‹œ์— ์šฐ๋ฆฌ์˜ ๋ฌธํ™”๋ฅผ ์ฒดํ—˜ํ•˜๋Š” ๊ธฐํšŒ๋ฅผ ๊ฐ–๋Š”๋‹ค. ํ•œ์˜ฅ ํ…Œ๋งˆ๊ด€์€ ์ž์ฒด๋กœ๋„ ๋ช…๋ฌผ์ด์ง€๋งŒ ์ œ์ž‘๊ณผ์ •์ด ๊ธฐ๋ก๋  ๋งŒํ•œ ๊ฐ€์น˜๊ฐ€ ์žˆ๋‹ค. ์ฃผ์ถง๋Œ๋ถ€ํ„ฐ ์ถ”๋…€, ์šฉ๋งˆ๋ฃจ, ๊ธฐ์™€๊นŒ์ง€ ์‹ค์ œ ํ•œ์˜ฅ์„ ๊ฑด์ถ•ํ•˜๋Š” ๊ฒƒ์€ ๋ฌด๋Œ€์„ธํŠธ์ฒ˜๋Ÿผ ์™ธ๊ด€๋งŒ ํ•œ์˜ฅ์Šค๋Ÿฝ๊ฒŒ ์ง“๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ํ˜„๋Œ€์‹ ๊ณต๋ฒ•์„ ์ ์šฉํ•ด ๋‹จ 3์ผ ๋งŒ์— 135m2 ๋„“์ด์˜ ํ•œ์˜ฅ ํ•œ ์ฑ„๋ฅผ ์ง“๋Š” ๊ฒƒ์ด ๊ด€๊ฑด์ด๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ „๊ตญ์—์„œ ์‹ค๋ ฅ ์žˆ๋Š” 12๊ฐœ ์—…์ฒด๊ฐ€ ๋ชจ์—ฌ ํ˜‘๋ ฅํ•˜์—ฌ ์ง€์€ <NAME>์ด ๊ณง ๋‰ด์š•์œผ๋กœ ๋‚ ์•„๊ฐ„๋‹ค. ํ•œ์ง€๋กœ ๊พธ๋ฉฐ์ง„ UN <NAME> ์‚ฌ๋ฌด์ด์žฅ์˜ ๊ด€์ € ๊ฒŒ์ŠคํŠธ๋ฃธ. ๋‹น์‹œ ์ธํ…Œ๋ฆฌ์–ด๋ฅผ ๋‹ด๋‹นํ–ˆ๋˜ ์ „์ฃผ '์ง€๋‹ด'ํŒ€์ด ๋ฐ•๋žŒํšŒ์žฅ์— ๊ทธ ๊ฒŒ์ŠคํŠธ๋ฃธ์„ ๊ทธ๋Œ€๋กœ ์žฌํ˜„ํ•œ๋‹ค. ํ•œ์ง€๋“ฑ๊ณผ ์˜ˆ์ˆ  ๋ฒฝ์ง€๋กœ ์šฐ๋ฆฌ ํ•œ์ง€ ๊ณ ์œ ์˜ ์€์€ํ•˜๊ณ  ์šฐ์•„ํ•œ ๋ถ„์œ„๊ธฐ๋ฅผ ์—ฐ์ถœํ•œ๋‹ค. ํ•œ์‹ ๋ถ€๋ฌธ์—์„œ๋Š” ๋…ํŠนํ•œ ์†Œ์žฌ๊ฐ€ ๋ˆˆ๊ธธ์„ ๋ˆ๋‹ค. ํ•œ๊ตญ์ „ํ†ต์Œ์‹์—ฐ๊ตฌ์†Œ์—์„œ ์ค‘๊ตญ์ธ๊ณผ ์ผ๋ณธ์ธ์˜ ์ž…๋ง›์— ๋งž๋Š” ํ•œ์‹๋ฉ”๋‰ด๋ฅผ ๊ฐœ๋ฐœํ•˜์—ฌ ์„ ๋ณด์ด๋Š” ๊ฒƒ์ด๋‹ค. ๋ณด์–‘์‹์„ ์„ ํ˜ธํ•˜๋Š” ์ค‘๊ตญ์ธ์„ ์œ„ํ•ด ๊ถ์ค‘๋Œ€ํ•˜์žฃ์ฆ™๋ƒ‰์ฑ„์™€ ์žฅ์–ด์žก์ฑ„ ๋“ฑ 20์—ฌ ๊ฐ€์ง€ ๋ฉ”๋‰ด๋ฅผ ์„ ๋ณด์ด๊ณ  ๊น”๋”ํ•œ ๋ง›์„ ์„ ํ˜ธํ•˜๋Š” ์ผ๋ณธ์ธ์—๊ฒŒ๋Š” ํ•ด๋ฌผ์ „๊ณจ ๋“ฑ 20์—ฌ ๊ฐ€์ง€ ๋ฉ”๋‰ด๋ฅผ ์ค€๋น„ํ–ˆ๋‹ค. ์šฐ๋ฆฌ ์ž…๋ง›๊ณผ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ์ง€ ๋น„๊ต ์‹œ์‹ํ•ด ๋ณด๋Š” ๊ฒƒ์ด ๊ด€์ „ ํฌ์ธํŠธ. ํ•œ๋ณต ๋ถ€๋ฌธ์€ ์‹ค์šฉ์ ์ธ ์•„์ดํ…œ์œผ๋กœ ์งœ์—ฌ ์žˆ๋‹ค. ํ•œ์‚ฐ๋ชจ์‹œํ…Œํฌ๋†€๋กœ์ง€๊ฐ€ ๊ฐœ๋ฐœํ•œ ์‹ ์†Œ์žฌ๋ฅผ ์ง์ ‘ ๋งŒ์ ธ๋ณผ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋„ˆ. ํ•œ๋ณต ์ƒํ™œํ™”๋ฅผ ์œ„ํ•ด ๊ตฌ๊น€์ด ์ ๊ณ  ๋ฌผ๋นจ๋ž˜๊ฐ€ ๊ฐ€๋Šฅํ•œ ์‹ ์†Œ์žฌ๊ฐ€ ์ฒซ์„ ์„ ๋ณด์ธ๋‹ค. ๋ˆˆ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์—†๋Š” ๊นŒ๋‹ญ์— ์†์œผ๋กœ ์ง์ ‘ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ตญ์•…์ฝ˜์„œํŠธ๋„ ์—ด๋ฆฐ๋‹ค. ํ“จ์ „ ํ•ด๊ธˆ์ฃผ์ž '<NAME>'๊ณผ '<NAME> ์ถœ์‹ ์˜ ํ”ผ๋ฆฌ์ฃผ์ž '<NAME'๊ฐ€ ๋ฌด๋Œ€์— ์˜ค๋ฅธ๋‹ค. ๋ณธ๋ช…๋ณด๋‹ค '<NAME>'์œผ๋กœ ๋” ์œ ๋ช…ํ•œ ์—ฝ๊ธฐ ํŒ์†Œ๋ฆฌ '<NAME'๋„ ๊ตญ์•…์˜ ๋Œ€์ค‘ํ™”์— ๋™์ฐธํ•œ๋‹ค. ๋ฐ•๋žŒํšŒ ๊ธฐ๊ฐ„ ๋™์•ˆ ๋ฌด๋Œ€์— ์„œ๋Š” ํŒ€์€ ์–ด๋ฆฐ์ด ๊ตญ์•…์—ฐ์ฃผ๋‹จ 'ATTI' ๋“ฑ 12๊ฐœ ํŒ€์ด๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: Django ์ž์Šต ### ๋ณธ๋ฌธ: Django ์ž์Šต, ์š”์•ฝ, ์ •๋ฆฌ ์ถœ์ฒ˜ ์ฐธ๊ณ  ์„œ์  Django๋กœ ๋ฐฐ์šฐ๋Š” ์‰ฝ๊ณ  ๋น ๋ฅธ ์›น ๊ฐœ๋ฐœ - ํŒŒ์ด์ฌ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ Django๋ฅผ ํ™œ์šฉํ•œ ์‰ฝ๊ณ  ๋น ๋ฅธ ์›น ๊ฐœ๋ฐœ - ํŒŒ์ด์ฌ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ [์‹ค์ „ ํŽธ] Tango With Django A beginner's Guide to Web Development With Python Django 1.9 Two Scoops of Django - Best Practices for Django 1.8 Mastering Django Core The Complete Guide to Django 1.8 LTS Django by Example Web Development with Django Cookbook, 2nd Django Design Pattern s and Best Practices Django Unleashed ์†Œ์Šค์ฝ”๋“œ Test-Driven Development with Python Lightweight Django Using REST, WebSockets, and Backbone Learning Django Web Development ๊ฐ•์ขŒ ๋ฐ ํŒ ์‚ฌ์ดํŠธ ์žฅ๊ณ  ๊ฑธ์Šค ํŠœํ† ๋ฆฌ์–ผ ๋‚ ๋กœ ๋จน๋Š” Django ์›น ํ”„๋ ˆ์ž„์›Œํฌ ๊ฐ•์ขŒ ์ดˆ๋ณด ๋ฉํ‚ค์˜ ๊ฐœ๋ฐœ ๊ณต๋ถ€๋กœ ๊ทธ Web Forefront - Beginning Django Simple is Better Than Complex Learning Django 1.10 DoKy's Blog Mastering Django Tango with Django Effective Django Matthew Daly's Blog - Django Blog ๊ฐœ๋ฐœ ๊ฐ•์ขŒ ๊ฒฝ์˜ ํ•™๋„์˜ ์ขŒ์ถฉ์šฐ๋Œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ-Django ์ฐธ๊ณ  ์˜คํ”ˆ์†Œ์Šค django-page-cms django-simple-blog django-blog-zinnia django-blogango kboard django-sitetree django-treenav 01. ํ”„๋กœ์ ํŠธ ๋ฐ ์•ฑ์˜ ๊ตฌ์กฐ์™€ ์„ค์ • Django ํ”„๋กœ์ ํŠธ ๋ฐ ์•ฑ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ, ํŒŒ์ผ์˜ ๊ตฌ์กฐ์™€ ์„ค์ •์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๋ฅผ ์ •๋ฆฌํ•œ๋‹ค. 01) pyenv์™€ ๊ฐ€์ƒํ™˜๊ฒฝ ํŒŒ์ด์ฌ ์„ค์น˜ ์œ ์˜์‚ฌํ•ญ pyenv ๊ฐœ์š” pyenv ์„ค์น˜ (์šฐ๋ถ„ํˆฌ) pyenv ์„ค์น˜ ๋ฐ ํŒŒ์ด์ฌ ๋นŒ๋“œ ์ค€๋น„ ํŒŒ์ด์ฌ ์„ค์น˜ pyenv๋กœ ์„ค์น˜ํ•œ ํŒŒ์ด์ฌ ์„ค์น˜ ๋””๋ ‰ํ„ฐ๋ฆฌ ํŒŒ์ด์ฌ ๋ฒ„์ „ ์„ ํƒ ์ˆœ์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „ ์„ ํƒ ์„ค์น˜ํ•œ ํŒŒ์ด์ฌ ์‚ญ์ œ pyenv ์‚ญ์ œ virtualenv ๊ฐ€์ƒํ™˜๊ฒฝ ์ค€๋น„ ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ ๊ฐ€์ƒํ™˜๊ฒฝ ์„ ํƒ ๊ฐ€์ƒํ™˜๊ฒฝ ์‚ญ์ œ ์š”์•ฝ: pyenv/virtualenv ์‹ค๋ฌด์  ์‹œ๋‚˜๋ฆฌ์˜ค macOS์—์„œ pyenv, pyenv-virtualenv ์„ค์น˜ ๊ฒฐ๋ก  ์ง€์นจ ํ—ค๋”ฉ 1 ํ—ค๋”ฉ 2 ํ—ค๋”ฉ 3 ํ—ค๋”ฉ 4 ํ—ค๋”ฉ 5 ํŒŒ์ด์ฌ ์„ค์น˜ ํŒŒ์ด์ฌ ๋‹ค์šด๋กœ๋“œ ํŽ˜์ด์ง€์—์„œ ์œˆ๋„์˜ ๊ฒฝ์šฐ Windows x86-64 executable installer ํŒŒ์ผ์„ ๋ฐ›์•„ ์„ค์น˜ํ•œ๋‹ค. ์œ ์˜์‚ฌํ•ญ ์„ค์น˜ ๊ฒฝ๋กœ ๋ฌธ์ œ PATH ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์‹œ์Šคํ…œ ๋“ฑ๋ก ๋ฌธ์ œ pyenv ๊ฐœ์š” 01-01. ํ”„๋กœ๊ทธ๋žจ ์„ค์น˜ ํŽ˜์ด์ง€์—์„œ virtualenv ๊ฐ€์ƒํ™˜๊ฒฝ์„ ๊ฐ„๋‹จํžˆ ์†Œ๊ฐœํ•˜๊ณ  ์žˆ๋‹ค. pyenv์˜ ๊ธฐ๋Šฅ์€ github ํ™ˆํŽ˜์ด์ง€ README ๋ฌธ์„œ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์†Œ๊ฐœํ•˜๊ณ  ์žˆ๋‹ค. ์‹œ์Šคํ…œ ์ „์—ญ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ํ”„๋กœ์ ํŠธ๋ณ„ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋™์‹œ์— ์—ฌ๋Ÿฌ ๋ฒ„์ „์˜ ํŒŒ์ด์ฌ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. tox ์œ ํ‹ธ๋ฆฌํ‹ฐ์™€ ๊ฒฐํ•ฉํ•˜์—ฌ ๋นŒ๋“œ ํ˜ธํ™˜์„ฑ ์ฒดํฌ์— ์œ ์šฉํ•˜๋‹ค. pyenv๋Š” ๋งค์šฐ ํŽธ๋ฆฌํ•œ ๋„๊ตฌ์ด์ง€๋งŒ ์œˆ๋„๋Š” ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค. pyenv๋Š” ํŒŒ์ด์ฌ ์‹คํ–‰ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์‹œ์Šคํ…œ์—์„œ ์ง์ ‘ ๋นŒ๋“œ ํ•˜์—ฌ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œˆ๋„์—์„œ ํŒŒ์ด์ฌ ๋นŒ๋“œ ํ™˜๊ฒฝ ๊ตฌ์ถ•์€ ์™„์ „ํžˆ ๋‹ค๋ฅธ ์ฃผ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. pyenv ์„ค์น˜ (์šฐ๋ถ„ํˆฌ) pyenv ์„ค์น˜ ๋ฐ ํŒŒ์ด์ฌ ๋นŒ๋“œ ์ค€๋น„ ์‹œ์Šคํ…œ์— curl ํŒจํ‚ค์ง€๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. curl์€ ์›๊ฒฉ ์„œ๋ฒ„์—์„œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ”„๋กœํ† ์ฝœ๋กœ ๋ช…๋ นํ–‰์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ธฐ ์œ„ํ•œ ๋„๊ตฌ์ด๋‹ค. ํ˜„์žฌ ์šฐ๋ถ„ํˆฌ 16.04 LTS ๋ฒ„์ „์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ curl์ด ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š์•„ ์ง์ ‘ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค. $ sudo apt-get install curl ์ด์ œ curl ๋ช…๋ น์–ด๋กœ pyenv๋ฅผ ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. $ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash ์„ค์น˜๊ฐ€ ์˜ฌ๋ฐ”๋กœ ์™„๋ฃŒ๋˜๋ฉด ~/.pyenv ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ  ํ™ˆ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ~/.bashrc ๋˜๋Š” ~/.bash_profile ํŒŒ์ผ ์•ˆ์— ๋‹ค์Œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. export PATH="${HOME}/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" ์ด์ œ ํ„ฐ๋ฏธ๋„์„ ์žฌ์‹œ์ž‘ํ•˜๋ฉด PATH ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— pyenv ๋„๊ตฌ์˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ถ”๊ฐ€๋˜๊ณ  pyenv์™€ pyenv-virtualenv๋ฅผ ๊ตฌ๋™ํ•œ๋‹ค. pyenv๋Š” ํŒŒ์ด์ฌ์„ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ์ง์ ‘ ๋นŒ๋“œ ํ•˜์—ฌ ์„ค์น˜ํ•˜๋ฏ€๋กœ ์ปดํŒŒ์ผ๋Ÿฌ์™€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค. sudo apt-get install build-essential libreadline-dev zlib1g-dev libbz2-dev libsqlite3-dev libssl-dev ํŒŒ์ด์ฌ ์„ค์น˜ pyenv์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํŒŒ์ด์ฌ ๋ฒ„์ „์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. pyenv versions * system (set by /home/user/.pyenv/version) ์šฐ๋ถ„ํˆฌ 16.04 LTS์—์„œ๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ 2.7.12 ๋ฒ„์ „๊ณผ 3.5.2 ๋ฒ„์ „์ด ์„ค์น˜๋œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‹œ์Šคํ…œ์— ์„ค์น˜๋œ ์ด ํŒŒ์ด์ฌ์„ system ํŒŒ์ด์ฌ์œผ๋กœ ๋ถ„๋ฅ˜ํ•œ๋‹ค. ์•„์ง๊นŒ์ง€ pyenv๋ฅผ ํ†ตํ•ด ํŒŒ์ด์ฌ์„ ๋นŒ๋“œ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์‹œ์Šคํ…œ ํŒŒ์ด์ฌ๋งŒ ์กด์žฌํ•œ๋‹ค. pyenv๋ฅผ ํ†ตํ•ด ํŒŒ์ด์ฌ ๋นŒ๋“œ ์„ค์น˜๋Š” pyenv install ๋ช…๋ น์–ด๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ นํ•  ์ˆ˜ ์žˆ๋‹ค. $ pyenv install 2.7.13 $ pyenv install 3.4.7 $ pyenv install 3.5.3 $ pyenv install 3.6.2 ๋งŒ์•ฝ ํŒŒ์ด์ฌ 2.7, 3.4, 3.5, 3.6 ๋ฒ„์ „์œผ๋กœ ๋ชจ๋“  ํ˜ธํ™˜์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์ด ๊ฐ๊ฐ์˜ ์ตœ์‹  ๋ฒ„์ „์„ ๋‹ค์šด๋กœ๋“œ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. pyenv install ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ด์ฌ ๋ฒ„์ „ ๋ชฉ๋ก์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. $ pyenv install -list Available versions: 2.1.3 2.2.3 2.3.7 2.4 2.4.1 2.4.2 2.4.3 ... ์ƒ๋žต ... ์œ„์™€ ๊ฐ™์ด ๋ช…๋ นํ•œ ๊ฒฝ์šฐ pyenv versions ๋ช…๋ น์–ด๋กœ ์„ค์น˜๋œ ํŒŒ์ด์ฌ์˜ ๋ฒ„์ „์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค: $ pyenv versions * system (set by /home/user/.pyenv/version) 2.7.13 3.4.7 3.5.3 3.6.2 ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ค์น˜ํ–ˆ์ง€๋งŒ ์•„์ง๊นŒ์ง€๋„ ๊ฒฐ๊ตญ ์‹œ์Šคํ…œ ํŒŒ์ด์ฌ์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. pyenv๋กœ ์„ค์น˜ํ•œ ํŒŒ์ด์ฌ ์„ค์น˜ ๋””๋ ‰ํ„ฐ๋ฆฌ ํŒŒ์ด์ฌ์€ $(pyenv root)/versions ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์„ค์น˜๋œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ™ˆ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ~/.pyenv/versions ๋””๋ ‰ํ„ฐ๋ฆฌ์ด๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ $(pyenv root)/versions ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋ฆ„์„ ๊ธฐ์ค€์œผ๋กœ pyenv์—์„œ ๋ฒ„์ „์„ ๊ด€๋ฆฌํ•œ๋‹ค. ์•ž์„œ ์˜ˆ์‹œ์™€ ๊ฐ™์ด pyenv๋กœ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ค์น˜ํ–ˆ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์กด์žฌํ•œ๋‹ค. $(pyenv root)/versions/2.7.13/ $(pyenv root)/versions/3.4.7/ $(pyenv root)/versions/3.5.3/ $(pyenv root)/versions/3.6.2/ ํŒŒ์ด์ฌ ๋ฒ„์ „ ์„ ํƒ ์ˆœ์„œ pyenv์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  pyenv๊ฐ€ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ ํƒํ•˜๋Š” ์ˆœ์„œ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดํ•ดํ•˜๊ณ  ์ •ํ™•ํžˆ ์‚ฌ์šฉํ•˜๋ ค๋Š” ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ ํƒํ•ด์•ผ ํ•œ๋‹ค. PYENV_VERSION ํ™˜๊ฒฝ ๋ณ€์ˆ˜์˜ ๊ฐ’ pyenv shell ๋ช…๋ น์–ด๋กœ ํ˜„์žฌ ํ„ฐ๋ฏธ๋„ ์„ธ์…˜์˜ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•œ๋‹ค. ํ„ฐ๋ฏธ๋„์ด ์—ด๋ฆฐ ์ƒํƒœ์—์„œ ํŠน์ • ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ ํƒํ•˜์—ฌ ๋ช…๋ นํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ๋งŒ์•ฝ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •์„ ์ œ๊ฑฐํ•˜๋ ค๋ฉด pyenv shell --unset์œผ๋กœ ๋ช…๋ นํ•˜๊ฑฐ๋‚˜ ํ„ฐ๋ฏธ๋„์„ ์ข…๋ฃŒํ•˜๊ณ  ์ƒˆ๋กœ ์—ฐ๋‹ค. .python-version ํŒŒ์ผ์˜ ๊ฐ’ pyenv local ๋ช…๋ น์–ด๋กœ ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์‹คํ–‰๋˜๋Š” ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ค์ •ํ•œ๋‹ค. pyenv local ๋ช…๋ น์–ด๋กœ. python-version ํŒŒ์ผ์ด ๋งŒ๋“ค์–ด์ง€๊ณ  ์ด ํŒŒ์ผ์ด ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋ถ€ํ„ฐ ์žฌ๊ท€์ ์œผ๋กœ ๋ชจ๋“  ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ ์šฉ๋œ๋‹ค. ๋”ฐ๋ผ์„œ, ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—์„œ ๋ช…์‹œ์ ์œผ๋กœ ํŠน์ • ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ ํƒํ•˜๊ณ ์ž ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ๋งŒ์•ฝ ๋””๋ ‰ํ„ฐ๋ฆฌ ์„ค์ •์„ ์ œ๊ฑฐํ•˜๋ ค๋ฉด pyenv local --unset์œผ๋กœ ๋ช…๋ นํ•˜๊ฑฐ๋‚˜ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—. python-version ํŒŒ์ผ์„ ์‚ญ์ œํ•œ๋‹ค. $(pyenv root)/version ํŒŒ์ผ์˜ ๊ฐ’ pyenv global ๋ช…๋ น์–ด๋กœ ์‹œ์Šคํ…œ ์ „์—ญ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ค์ •ํ•œ๋‹ค. $(pyenv root)/version ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” "system" ๋””ํดํŠธ ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค. ๋งŽ์€ ํŠœํ† ๋ฆฌ์–ผ์ด pyenv๋กœ ์„ค์น˜ํ•œ ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด pyenv shell, pyenv local, pyenv global ๋ช…๋ น์–ด๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์„ค๋ช…ํ•˜๊ณ  ๊ฐ๊ฐ์˜ ์ฐจ์ด๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ ๋‹ค. ์„ค์ • ์šฐ์„ ์ˆœ์œ„ ๊ด€๊ณ„๋ฅผ ์˜ฌ๋ฐ”๋กœ ์ดํ•ดํ•ด์•ผ ์‚ฌ์šฉํ•  ๋•Œ ํ˜ผ๋™์ด ์—†๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „ ์„ ํƒ tox ์œ ํ‹ธ๋ฆฌํ‹ฐ๋กœ ํ˜ธํ™˜์„ฑ ๊ฒ€์‚ฌํ•  ๋•Œ ๋งค์šฐ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ์ด๋‹ค. pyenv๋ฅผ ํ†ตํ•ด ํŒŒ์ด์ฌ 2์™€ ํŒŒ์ด์ฌ 3๋ฅผ ๋™์‹œ์— ์„ค์น˜ํ•˜์—ฌ ๋นŒ๋“œ ๊ฒ€์‚ฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•ž์„œ ์šฐ๋ถ„ํˆฌ 16.04 LTS์˜ ๊ฒฝ์šฐ ์‹œ์Šคํ…œ ํŒจํ‚ค์ง€๋กœ ํŒŒ์ด์ฌ ๋ฒ„์ „ 2.7.12, 3.5.2 ๋ฒ„์ „์ด ์„ค์น˜๋˜๊ณ  system ํŒŒ์ด์ฌ์œผ๋กœ ๋ถ„๋ฅ˜๋œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  pyenv install ๋ช…๋ น์–ด๋กœ ํ•„์š”์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ด์ฌ์„ ์„ค์น˜ํ–ˆ๋‹ค. python ๋ช…๋ น์–ด๋กœ ์‹คํ–‰๋˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” system ํŒŒ์ด์ฌ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค: $ python -V Python 2.7.12 $ python3 -V Python 3.5.2 pyenv๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•˜๊ธฐ๋กœ ํ•œ ์ด์ƒ system ๋ฒ„์ „์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ฒ ๋‹ค. ์ด์ œ ์‹œ์Šคํ…œ ์•ˆ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด python global ๋ช…๋ น์–ด๋กœ ๋ฒ„์ „์„ ๋‚˜์—ดํ•ด ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. $ pyenv global 3.6.2 3.5.3 3.4.7 2.7.13 ์ด์ œ ์‹œ์Šคํ…œ์—์„œ python, python3 ๋ช…๋ น์–ด๋กœ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฒ„์ „์„ ํ™•์ธํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋‘ pyenv๋กœ ์„ค์น˜ํ•œ 3.6.2 ๋ฒ„์ „์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. $ python -V Python 3.6.2 $ python3 -V Python 3.6.2 ๋งŒ์•ฝ 3.5.3, 3.4.7, 2.7.13 ๊ฐ™์€ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๊ฐ๊ฐ python3.5, python3.4, python2.7 ๋ช…๋ น์–ด๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋Š” python2.7๋กœ system ๊ธฐ๋ณธ ๋ฒ„์ „ 2.7.12๊ฐ€ ์•„๋‹ˆ๋ผ 2.7.13 ๋ฒ„์ „์ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. $ python2.7 Python 2.7.13 (default, Aug 20 2017, 15:43:40) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์‹คํ–‰ ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. $ pyenv which python3.6 /home/user/.pyenv/versions/3.6.2/bin/python3.6 ๋งŒ์•ฝ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ํŒŒ์ด์ฌ์ด ์˜ฌ๋ฐ”๋กœ ์„ค์น˜๋˜์ง€ ์•Š์€ ๊ฒƒ์ด๋‹ค. pyenv: python3.6: command not found The `python3.6' command exists in these Python versions: 3.6.2 ๋˜ํ•œ ์ฐธ๊ณ ๋กœ ์—ฌ๋Ÿฌ ๋ฒ„์ „์˜ ํŒŒ์ด์ฌ์„ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด python global ๋ช…๋ น์–ด๊ฐ€ ์•„๋‹ˆ๋ผ python local ๋ช…๋ น์–ด๋กœ๋„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์„ค์น˜ํ•œ ํŒŒ์ด์ฌ ์‚ญ์ œ pyenv install ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•œ ํŒŒ์ด์ฌ์€ pyenv uninstall ๋ช…๋ น์–ด๋กœ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. pyenv uninstall 3.6.2 pyenv ์‚ญ์ œ pyenv ์„ค์ •์ด ๊ผฌ์ด๊ณ  ์„ค์น˜๋œ ์—ฌ๋Ÿฌ ๋ฒ„์ „์˜ ํŒŒ์ด์ฌ์ด ๋ญ”๊ฐ€ ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚จ๋‹ค๋ฉด pyenv ์ž์ฒด๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. pyenv์˜ ์‚ญ์ œ๋Š” $(pyenv root) ๊ฒฝ๋กœ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ฐ„๋‹จํžˆ ์‚ญ์ œํ•˜๋ฉด ๋˜๋Š”๋ฐ ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ~/.pyenv ๋””๋ ‰ํ„ฐ๋ฆฌ์ด๋‹ค. virtualenv ๊ฐ€์ƒํ™˜๊ฒฝ ์ค€๋น„ curl๋กœ ์ธ์Šคํ†จ๋Ÿฌ๋ฅผ ๋‚ด๋ ค๋ฐ›์•„ pyenv ์„ค์น˜ํ•œ ๊ฒฝ์šฐ์—๋Š” virtualenv๊ฐ€ ๊ฐ™์ด ์„ค์น˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์„ค์น˜ํ•  ํ•„์š”๋Š” ์—†๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ž์„œ ~/.bashrc ๋˜๋Š” ~/.bash_profile ํŒŒ์ผ์— ๋‹ค์Œ ์ค„์„ ์ถ”๊ฐ€ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์œ„ํ•œ ์ค€๋น„๊ฐ€ ์ด๋ฏธ ๋˜์–ด ์žˆ๋‹ค. eval "$(pyenv virtualenv-init -)" ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ ๊ฐ€์ƒํ™˜๊ฒฝ์˜ ์ƒ์„ฑ์€ pyenv virtualenv ๋ช…๋ น์–ด๋กœ ์ƒ์„ฑํ•œ๋‹ค. pyenv virtualenv venv ํ˜„์žฌ ํŒŒ์ด์ฌ์˜ ํ™˜๊ฒฝ์œผ๋กœ venv ์ด๋ฆ„์˜ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. $ pyenv versions system * 2.7.13 (set by /home/user/.pyenv/version) * 3.4.7 (set by /home/user/.pyenv/version) * 3.5.3 (set by /home/user/.pyenv/version) * 3.6.2 (set by /home/user/.pyenv/version) 3.6.2/envs/venv venv ์‹œ์Šคํ…œ ๊ธฐ๋ณธ ํŒŒ์ด์ฌ ๋ฒ„์ „์ด 3.6.2์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋ฒ„์ „์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์ƒํ™˜๊ฒฝ์ด ๋งŒ๋“ค์–ด์กŒ๋‹ค. ๋ช…์‹œ์ ์œผ๋กœ ํŒŒ์ด์ฌ ํŠน์ • ๋ฒ„์ „์„ ์ง€์ •ํ•ด์„œ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ๋งŒ๋“ค๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ช…๋ นํ•œ๋‹ค. $ pyenv virtualenv 3.5.3 venv 3.5.3 ๋ฒ„์ „์„ ์ง€์ •ํ–ˆ๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. $ pyenv versions system * 2.7.13 (set by /home/user/.pyenv/version) * 3.4.7 (set by /home/user/.pyenv/version) * 3.5.3 (set by /home/user/.pyenv/version) 3.5.3/envs/venv * 3.6.2 (set by /home/user/.pyenv/version) venv ๊ฐ€์ƒํ™˜๊ฒฝ ์„ ํƒ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์€ ๊ฒฐ๊ตญ pyenv์—์„œ ์–ด๋–ค ๋ฒ„์ „์˜ ํŒŒ์ด์ฌ์„ ์„ ํƒํ•  ๊ฒƒ์ธ๊ฐ€ ํ•˜๋Š” ๋ฌธ์ œ์™€ ๊ฐ™๋‹ค. ๋”ฐ๋ผ์„œ ํ•„์š”์— ๋”ฐ๋ผ pyenv shell, pyenv local, pyenv global ๋ช…๋ น์–ด ์ค‘ ํ•˜๋‚˜๋กœ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์„ ํƒํ•œ๋‹ค. $ pyenv shell venv ์œ„์™€ ๊ฐ™์ด ํŠน์ • ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์„ ํƒํ•˜๊ณ  python, python3 ๋ฒ„์ „์„ ํ™•์ธํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (venv) $ pyenv which python /home/user/.pyenv/versions/venv/bin/python (venv) $ python -V Python 3.5.3 (venv) $ pyenv which python3 /home/user/.pyenv/versions/venv/bin/python3 (venv) $ python3 -V Python 3.5.3 ์œ„์™€ ๊ฐ™์ด ์‰˜์—์„œ (venv) ๊ฐ€์ƒํ™˜๊ฒฝ์œผ๋กœ ์ง„์ž…ํ•œ ๊ฒฝ์šฐ์—๋Š” pyenv๋กœ ์„ค์น˜ํ•œ 3.4, 3.6 ๊ฐ™์€ ๋‹ค๋ฅธ ๋ฒ„์ „์˜ ํŒŒ์ด์ฌ์„ ๋ช…๋ นํ–‰์—์„œ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค. $ pyenv which python3.4 pyenv: python3.4: command not found The `python3.4' command exists in these Python versions: 3.4.7 ๊ฐ€์ƒํ™˜๊ฒฝ ์‚ญ์ œ ๊ฐ€์ƒํ™˜๊ฒฝ์˜ ์‚ญ์ œ๋Š” pyenv install๋กœ ์„ค์น˜ํ•œ ํŒŒ์ด์ฌ์„ ์‚ญ์ œํ•˜๋Š” pyenv uninstall ๋ช…๋ น์–ด์™€ ๊ฐ™๋‹ค. $ pyenv uninstall venv ์š”์•ฝ: pyenv/virtualenv ์‹ค๋ฌด์  ์‹œ๋‚˜๋ฆฌ์˜ค pyenv๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์ด์œ  ์ค‘ ํ•˜๋‚˜๊ฐ€ tox์™€ ์—ฐ๋™ํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ฒ„์ „์˜ ํŒŒ์ด์ฌ์œผ๋กœ ๋นŒ๋“œ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒƒ์ด๋‹ค. tox๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ณ  tox๋ฅผ ์—ฐ๋™ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ๋‹ค. pyenv ์„ค์น˜ $ apt-get install curl $ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash ~/.bashrc ๋˜๋Š” ~/.bash_profile ํŒŒ์ผ ์ƒ์„ฑ export PATH="${HOME}/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" ๋นŒ๋“œ ์ปดํŒŒ์ผ๋Ÿฌ ๋ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒจํ‚ค์ง€ ์„ค์น˜ $ sudo apt-get install build-essential libreadline-dev zlib1g-dev libbz2-dev libsqlite3-dev libssl-dev pyenv๋กœ ํŒŒ์ด์ฌ ๋‹ค์šด๋กœ๋“œ ๋นŒ๋“œ ์„ค์น˜ $ pyenv install 2.7.13 $ pyenv install 3.4.7 $ pyenv install 3.5.3 $ pyenv install 3.6.2 ๊ฐ€์ƒํ™˜๊ฒฝ ์„ค์น˜ (-p ์˜ต์…˜์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด ๊ฐ€์ƒํ™˜๊ฒฝ์œผ๋กœ ์ง„์ž…ํ–ˆ์„ ๋•Œ ๋‹ค๋ฅธ ๋ฒ„์ „์˜ python์„ ์ฐพ์„ ์ˆ˜ ์—†์Œ) $ pyenv virtualenv -p python3.6 3.6.2 venv ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „ ์„ ํƒ(๋””ํดํŠธ=venv 3.6.2 ๋ฒ„์ „ ๊ฐ€์ƒํ™˜๊ฒฝ) $ pyenv global venv 3.5.3 3.4.7 2.7.13 ๋งŒ์•ฝ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ๋“ฑ๋กํ•ด๋‘์ง€ ์•Š์œผ๋ฉด ์ดํ›„ tox ๋ช…๋ น์–ด ์‹คํ–‰ ์‹œ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์ฐพ์ง€ ๋ชปํ•ด ๊ฐ€์ƒํ™˜๊ฒฝ์ด ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ํŒŒ์ด์ฌ ๋ฒ„์ „ ์‰˜๋กœ ์ง€์ • ((venv) ํ”„๋กฌํ”„ํŠธ๊ฐ€ ํ™œ์„ฑํ™”๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ) $ pyenv shell venv tox ์„ค์น˜ ๋ฐ ์‹คํ–‰ $ pip install tox $ tox macOS์—์„œ pyenv, pyenv-virtualenv ์„ค์น˜ homebrew ์„ค์น˜ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" pyenv, pyenv-virtualenv ํŒจํ‚ค์ง€ ์„ค์น˜ $ brew update $ brew install pyenv $ brew install pyenv-virtualenv ~/.bash_profile ํŒŒ์ผ์˜ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" ์ดํ•˜ ๊ฐ€์ƒํ™˜๊ฒฝ ์„ค์น˜ ๋‚ด์šฉ์€ ์œ„์˜ ๋‚ด์šฉ๊ณผ ๋™์ผํ•˜๋‹ค. ๋งŒ์•ฝ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib? CFLAGS="-I$(brew --prefix openssl)/include" \ LDFLAGS="-L$(brew --prefix openssl)/lib" \ pyenv install -v 3.4.3 ๊ฒฐ๋ก  ์ง€์นจ ์ถœ์ฒ˜: ํŒŒ์ด์ฌ์˜ ๊ฐœ๋ฐœ โ€œํ™˜๊ฒฝโ€(env) ๋„๊ตฌ๋“ค ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์•„๋‹ˆ์ง€๋งŒ, ํŒŒ์ด์ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์„ค์น˜ํ•ด์„œ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์‹œ์Šคํ…œ์—์„œ ์ œ๊ณตํ•˜๋Š” ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž(APT๋‚˜ ํ™ˆ ๋ธŒ๋ฃจ ๋“ฑ)๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์„ค์น˜ํ•˜์„ธ์š”. ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์•„๋‹ˆ์ง€๋งŒ, ํŒŒ์ด์ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์œ ๋‚œํžˆ ๋งŽ์ด ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. pipsi๋ฅผ ์ด์šฉํ•ด ํŒŒ์ด์ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋จธ์ด๊ณ , ํ•˜๋‚˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ 3.3 ์ด์ƒ์„ ์ด์šฉํ•  ๊ฒฝ์šฐ pyvenv๋กœ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค์–ด์„œ ๊ฐœ๋ฐœํ•˜์„ธ์š”. ๊ทธ ์ด์ „์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์ด์šฉํ•  ๊ฒฝ์šฐ virtualenv๋ฅผ ํ™œ์šฉํ•˜์„ธ์š”. ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋จธ์ด๊ณ , ์—ฌ๋Ÿฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค. virtualenvwrapper๋ฅผ ํ™œ์šฉํ•˜์„ธ์š”. ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋จธ์ด๊ณ , ์—ฌ๋Ÿฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋‹ค์–‘ํ•œ ํŒŒ์ด์ฌ ๋ฒ„์ „์œผ๋กœ ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค. pyenv-virtualenvwrapper๋ฅผ ํ™œ์šฉํ•˜์„ธ์š”. ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋จธ์ด๊ณ , ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค. pyenv์™€ tox๋ฅผ ํ™œ์šฉํ•˜์„ธ์š”. ์ ์–ด๋„ Django ๊ฐœ๋ฐœ์ž๋ผ๋ฉด 3, 4, 5, 6 ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ค‘ ํ•˜๋‚˜์ผ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์žฌ๋ฐฐํฌ ๊ฐ€๋Šฅํ•œ Django ์•ฑ์„ ๊ฐœ๋ฐœํ•œ๋‹ค๋ฉด pyenv, tox๋ฅผ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. ํ—ค๋”ฉ 1 ํ—ค๋”ฉ 2 ํ—ค๋”ฉ 3 ํ—ค๋”ฉ 4 ํ—ค๋”ฉ 5 02) Django ์„ค์น˜ Django ์„ค์น˜ ๊ฐ€์ƒํ™˜๊ฒฝ ์ค€๋น„ ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ ๊ฐ€์ƒํ™˜๊ฒฝ ํ™œ์„ฑํ™” (์œˆ๋„) ๊ฐ€์ƒํ™˜๊ฒฝ ๋‚˜์˜ค๊ธฐ ๊ธฐ์กด ๊ฐ€์ƒํ™˜๊ฒฝ ์—…๋ฐ์ดํŠธ PIP์œผ๋กœ Django ์„ค์น˜ Django ์„ค์น˜ ์„ค์น˜ ๋ฒ„์ „ ํ™•์ธ PyCharm ์„ค์น˜ Django๋ฅผ ๋น„๋กฏํ•œ PyCharm IDE๋ฅผ ์„ค์น˜ํ•œ๋‹ค. Django ์„ค์น˜ ๊ฐ€์ƒํ™˜๊ฒฝ ์ค€๋น„ ํŒŒ์ด์ฌ ๋ฒ„์ „ 3.4๋ถ€ํ„ฐ virtualenv ํŒจํ‚ค์ง€๋ฅผ ๋ณ„๋„๋กœ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ python -m venv my_env ๊ฐ€์ƒํ™˜๊ฒฝ ํ™œ์„ฑํ™” (์œˆ๋„) venv\Scripts\activate.bat ๊ฐ€์ƒํ™˜๊ฒฝ ๋‚˜์˜ค๊ธฐ (venv) venv\Scripts\deactivate ๊ธฐ์กด ๊ฐ€์ƒํ™˜๊ฒฝ ์—…๋ฐ์ดํŠธ ์šฐ๋ถ„ํˆฌ ๋“ฑ์—์„œ ์‹œ์Šคํ…œ ํŒŒ์ด์ฌ์ด ์—…๊ทธ๋ ˆ์ด๋“œ๋˜๋Š” ๊ฒฝ์šฐ ์˜ˆ๋ฅผ ๋“ค์–ด, 3.5 ๋ฒ„์ „์—์„œ 3.6 ๋ฒ„์ „์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ๋˜๋Š” ๊ฒฝ์šฐ์— ๊ธฐ์กด์— ๊ตฌ ๋ฒ„์ „์— ์ข…์†์ ์ธ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์—…๊ทธ๋ ˆ์ด๋“œํ•ด์ค˜์•ผ ํ•œ๋‹ค. python3 -m venv --upgrade venv PIP์œผ๋กœ Django ์„ค์น˜ Django ์„ค์น˜ pip install Django==1.11.2 ์„ค์น˜ ๋ฒ„์ „ ํ™•์ธ import django print(django.VERSION) print(django.get_version()) django.VERSION (1, 11, 2, 'final', 0) ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜ django.get_version() 1.11.2 ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ PyCharm ์„ค์น˜ PyCharm Pro๋Š” ์ƒ์šฉ ํ”„๋กœ๊ทธ๋žจ์ด์ง€๋งŒ ์ปค๋ฎค๋‹ˆํ‹ฐ ์—๋””์…˜์„ ๋ฌด๋ฃŒ๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ปค๋ฎค๋‹ˆํ‹ฐ ์—๋””์…˜์€ Django๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์ง€์›ํ•˜์ง€ ์•Š์ง€๋งŒ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋ฐœ์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. PyCharm IDE๋Š” ์ž๋ฐ”๋กœ ๊ฐœ๋ฐœ๋œ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ JRE๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. 02) Django ํ”„๋กœ์ ํŠธ ๋ฐ ์•ฑ ๊ตฌ์กฐ Django ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ ๊ธฐ๋ณธ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ํŒŒ์ผ ๊ตฌ์กฐ ๊ธฐ๋ณธ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ํŒŒ์ผ ์„ค๋ช… ๋ช…๋ น์–ด ์ •๋ฆฌ ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ ํŒŒ์ด์ฌ ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ ๋ฐ ํ™œ์„ฑํ™” Django ์„ค์น˜ ๋ฐ ๋ฒ„์ „ ํ™•์ธ Django ํ”„๋กœ์ ํŠธ ์ €์žฅ์†Œ ์ƒ์„ฑ ํ…Œ์ŠคํŠธ ์„œ๋ฒ„ ๊ตฌ๋™ gitlab ์ฃผ์†Œ์—์„œ ๊ทธ๋Œ€๋กœ ๋‚ด๋ ค๋ฐ›๊ธฐ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ ํ•ด๊ฒฐ django-admin.py ํŒŒ์ผ ์‹คํ–‰์ด ์˜ฌ๋ฐ”๋กœ ์•ˆ ๋˜๋Š” ๊ฒฝ์šฐ Django ์•ฑ ๊ตฌ์กฐ ๊ธฐ๋ณธ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ํŒŒ์ผ ๊ตฌ์กฐ ๊ณตํ†ต ๋ชจ๋“ˆ ๊ตฌ์กฐ ์ฟ ํ‚ค ์ปคํ„ฐ ์ฟ ํ‚ค ์ปคํ„ฐ ์„ค์น˜ ์ฟ ํ‚ค ์ปคํ„ฐ๋กœ Django ์•ฑ ๋ผˆ๋Œ€ ๋งŒ๋“ค๊ธฐ ์˜๊ฒฌ ๋ฐ ์š”์•ฝ Django ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ ๊ธฐ๋ณธ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ํŒŒ์ผ ๊ตฌ์กฐ ์ตœ์ดˆ Django ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ repo ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ นํ•˜๋ฉด conf ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค. django-admin.py startproject conf . ์œ„ ๋ช…๋ น์–ด๋กœ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํ”„๋กœ์ ํŠธ_์ด๋ฆ„/ repo/ conf/ __init__.py settings.py urls.py wsgi.py manage.py venv/ PyCharm์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ž„ํฌํŠธ ํ•  ๋•Œ๋Š” repo ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ž„ํฌํŠธํ•˜๊ณ  ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ €์žฅ์†Œ์— ์†Œ์Šค ์ปค๋ฐ‹ ํ•œ๋‹ค. ๊ธฐ๋ณธ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ํŒŒ์ผ ์„ค๋ช… ํ”„๋กœ์ ํŠธ ๋‹จ์œ„๋กœ ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ฐ€์ง„๋‹ค. repo: Django ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์†Œ์Šค ์ฝ”๋“œ ๋””๋ ‰ํ„ฐ๋ฆฌ = ์ €์žฅ์†Œ ์ปค๋ฐ‹ ๊ด€๋ฆฌ venv: ๊ฐ€์ƒํ™˜๊ฒฝ ๋””๋ ‰ํ„ฐ๋ฆฌ mkdir ๋ช…๋ น์–ด๋กœ repo ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑ ํ›„ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—์„œ startproject ์˜ต์…˜์œผ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ conf ์ด๋ฆ„ ๋Œ€์‹ ์— ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ํ”„๋กœ์ ํŠธ์˜ ์„ค์ • ํŒŒ์ผ์ด ๋“ค์–ด๊ฐ€๋ฏ€๋กœ conf ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ํƒ€ ํ”„๋กœ์ ํŠธ์™€ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋ฆ„์„ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์œผ๋กœ ํ•œ๋‹ค. venv ๋””๋ ‰ํ„ฐ๋ฆฌ๋Š” ์ข…์†์„ฑ ๋ถ„๋ฆฌ์˜ ์›์น™์— ๋”ฐ๋ผ ํ”„๋กœ์ ํŠธ์˜ ๋…๋ฆฝ์ ์ธ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์ œ๊ณตํ•œ๋‹ค. ํŒŒ์ด์ฌ 3.4 ๋ฒ„์ „๋ถ€ํ„ฐ๋Š” ๋ณ„๋„์˜ virtualenv ํŒจํ‚ค์ง€ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ๊ฐ€์ƒํ™˜๊ฒฝ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋ช…๋ น์–ด ์ •๋ฆฌ ์ƒ๊ธฐ ๊ตฌ์กฐ์— ๋”ฐ๋ฅธ ๊ตฌ์„ฑ์„ ์œ„ํ•œ ์œˆ๋„ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ ๋ช…๋ น์–ด๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ mkdir ํ”„๋กœ์ ํŠธ_์ด๋ฆ„ ํŒŒ์ด์ฌ ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ ๋ฐ ํ™œ์„ฑํ™” python -m venv venv venv\Scripts\activate.bat Django ์„ค์น˜ ๋ฐ ๋ฒ„์ „ ํ™•์ธ pip์œผ๋กœ ์ธํ„ฐ๋„ท์—์„œ ๋‹ค์šด๋กœ๋“œํ•ด ์„ค์น˜ํ•œ๋‹ค. pip install Django ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  Django ๋ฒ„์ „ ํ™•์ธ์„ ํ†ตํ•ด ์˜ฌ๋ฐ”๋กœ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. import django django.VERSION quit() Django ํ”„๋กœ์ ํŠธ ์ €์žฅ์†Œ ์ƒ์„ฑ mkdir repo cd repo django-admin.py startproject conf . ํ…Œ์ŠคํŠธ ์„œ๋ฒ„ ๊ตฌ๋™ ๋‹ค์Œ๊ณผ ๊ฐ™์ด Django ํ…Œ์ŠคํŠธ ์„œ๋ฒ„๋ฅผ ๊ตฌ๋™ํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ €๋กœ http://127.0.0.1:8000/ ์ฃผ์†Œ์— ์ ‘์†ํ•ด ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•œ๋‹ค. python manage.py runserver gitlab ์ฃผ์†Œ์—์„œ ๊ทธ๋Œ€๋กœ ๋‚ด๋ ค๋ฐ›๊ธฐ Django ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ์‹œ --template ์˜ต์…˜์œผ๋กœ ๊ทธ๋Œ€๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์˜ ์ตœ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ นํ•˜๋ฉด repo ํ”„๋กœ์ ํŠธ/์ €์žฅ์†Œ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. django-admin.py startproject --template https://gitlab.com/mairoo/django-quickstarter/repository/archive.zip repo ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ ํ•ด๊ฒฐ django-admin.py ํŒŒ์ผ ์‹คํ–‰์ด ์˜ฌ๋ฐ”๋กœ ์•ˆ ๋˜๋Š” ๊ฒฝ์šฐ ์œˆ๋„์—์„œ. py ํ™•์žฅ์ž์— ์—ฐ๊ฒฐ ํ”„๋กœ๊ทธ๋žจ์ด ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ django-admin.py ํŒŒ์ผ์ด ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  ์—๋””ํ„ฐ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๊ฑฐ๋‚˜ ํŒŒ์ด์ฌ ๊ฐ€์ƒํ™˜๊ฒฝ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์•„๋‹Œ ์‹œ์Šคํ…œ์— ์„ค์น˜๋œ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋กœ ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•ด venv\Scripts ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์•„๋ž˜ ๋ฐฐ์น˜ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค. django-admin.bat ํŒŒ์ผ @echo off python "%VIRTUAL_ENV%\Scripts\django-admin.py" %* ์œ„ ๋ฐฐ์น˜ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  django-admin.py ๋Œ€์‹ ์— django-admin.bat ๋ช…๋ น์–ด๋กœ Django ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. django-admin.bat startproject conf . Django ์•ฑ ๊ตฌ์กฐ ๊ธฐ๋ณธ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ํŒŒ์ผ ๊ตฌ์กฐ ์•ฑ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ๊ธฐ๋ณธ ์ƒ์„ฑ ํŒŒ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. bookmark/ __init__.py admin.py apps.py migrations/ models.py tests.py views.py Django ๋ฒ„์ „ 1.9๋ถ€ํ„ฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋Š” apps.py ํŒŒ์ผ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋“ฑ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'bookmark.apps.BookmarkConfig', ] ๋‹จ์ˆœํžˆ bookmark ๋ชจ๋“ˆ ์ด๋ฆ„์œผ๋กœ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์ง€๋งŒ bookmark.apps.BookmarkConfig ์„ค์ • ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ์ด ๋ณด๋‹ค ์ •ํ™•ํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ณตํ†ต ๋ชจ๋“ˆ ๊ตฌ์กฐ Two Scoops of Django ์ฑ…์—์„œ ์†Œ๊ฐœํ•˜๋Š” ๊ณตํ†ต ๋ชจ๋“ˆ ๊ตฌ์กฐ์ด๋‹ค. Django ๊ฐœ๋ฐœ์ž๋“ค๋ผ๋ฆฌ์˜ ์ด๋ฆ„ ๊ทœ์น™ ์•ฝ์†์ด๋‹ค. bookmark/ __init__.py admin.py apps.py migrations/ models.py tests.py views.py urls.py forms.py behaviors.py constants.py decorators.py db/ fields.py factories.py helpers.py managers.py signals.py viewmixins.py ์ถ”๊ฐ€๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. urls.py: ์•ฑ์˜ URL ํŒจํ„ด ์„ ์–ธ forms.py: ์ž…๋ ฅ ํผ ์„ ์–ธ behaviors.py: ๋ชจ๋ธ ๋ฏน์Šค์ธ ์œ„์น˜์— ๋Œ€ํ•œ ์˜ต์…˜ constants.py: ์•ฑ์— ์“ฐ์ด๋Š” ์ƒ์ˆ˜ ์„ ์–ธ decorators.py: ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ db/: ์—ฌ๋Ÿฌ ํ”„๋กœ์ ํŠธ์—์„œ ์šฉ๋˜๋Š” ์ปค์Šคํ…€ ๋ชจ๋ธ์ด๋‚˜ ์ปดํฌ๋„ŒํŠธ fields.py: ํผ ํ•„๋“œ factories.py: ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ํŒฉํ† ๋ฆฌ ํŒŒ์ผ helpers.py: ๋ทฐ์™€ ๋ชจ๋ธ ํŒŒ์ผ์„ ๊ฐ€๋ณ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜ ์„ ์–ธ managers.py: models.py๊ฐ€ ๋„ˆ๋ฌด ์ปค์งˆ ๊ฒฝ์šฐ ์ปค์Šคํ…€ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๊ฐ€ ์œ„์น˜ signals.py: ์ปค์Šคํ…€ ์‹œ๊ทธ๋„ viewmixins.py: ๋ทฐ ๋ชจ๋“ˆ๊ณผ ํŒจํ‚ค์ง€๋ฅผ ๋” ๊ฐ€๋ณ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๋ทฐ ๋ฏน์Šค์ธ์„ ์ด ๋ชจ๋“ˆ๋กœ ์ด์ „ ์ฟ ํ‚ค ์ปคํ„ฐ ์ฟ ํ‚ค ์ปคํ„ฐ ํ”„๋กœ์ ํŠธ๋Š” Django ํ”„๋กœ์ ํŠธ๋ฅผ ๋นจ๋ฆฌ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด์ง„ ์•ฑ์˜ ๊ตฌ์กฐ์ด๋‹ค. ์ฟ ํ‚ค ์ปคํ„ฐ ์„ค์น˜ pip install cookiecutter ์ฟ ํ‚ค ์ปคํ„ฐ๋กœ Django ์•ฑ ๋ผˆ๋Œ€ ๋งŒ๋“ค๊ธฐ cookiecutter https://github.com/pydanny/cookiecutter-django.git You've cloned /home/mairoo/.cookiecutters/cookiecutter-django before. Is it okay to delete and re-clone it? [yes]: yes Cloning into 'cookiecutter-django'... remote: Counting objects: 7833, done. remote: Compressing objects: 100% (42/42), done. remote: Total 7833 (delta 19), reused 0 (delta 0), pack-reused 7791 Receiving objects: 100% (7833/7833), 2.82 MiB | 991.00 KiB/s, done. Resolving deltas: 100% (5078/5078), done. Checking connectivity... done. project_name [Project Name]: project_slug [project_name]: author_name [Daniel Roy Greenfeld]: email [you@example.com]: description [A short description of the project.]: domain_name [example.com]: version [0.1.0]: timezone [UTC]: use_whitenoise [y]: use_celery [n]: y use_mailhog [n]: y use_sentry_for_error_reporting [y]: use_opbeat [n]: y use_pycharm [n]: y windows [n]: y use_python3 [y]: use_docker [y]: use_heroku [n]: y use_compressor [n]: y Select postgresql_version: 1 - 9.5 2 - 9.4 3 - 9.3 4 - 9.2 Choose from 1, 2, 3, 4 [1]: 1 Select js_task_runner: 1 - Gulp 2 - Grunt 3 - Webpack 4 - None Choose from 1, 2, 3, 4 [1]: 4 use_lets_encrypt [n]: y Select open_source_license: 1 - MIT 2 - BSD 3 - GPLv3 4 - Apache Software License 2.0 5 - Not open source Choose from 1, 2, 3, 4, 5 [1]: 1 You selected to use Let's Encrypt, please see the documentation for instructions on how to use this in production. You must generate a dhparams.pem file before running docker-compose in a production environment. ์˜๊ฒฌ ๋ฐ ์š”์•ฝ ์ฟ ํ‚ค ์ปคํ„ฐ์˜ ์ƒ์„ธ ์˜ต์…˜์„ ๋ชจ๋‘ ํŒŒ์•…ํ•ด์„œ ์ดํ•ดํ•˜๊ธฐ ์ „๊นŒ์ง€๋Š” ์‹ค๋ฌด์ ์œผ๋กœ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ๋‹ค ์“ฐ๊ธฐ์—” ๋ฌด๋ฆฌ๊ฐ€ ์žˆ๋‹ค. ์ฟ ํ‚ค ์ปคํ„ฐ ์ž์ฒด๋กœ ์ƒ๋‹นํžˆ ๋งŽ์€ ์˜์กด์„ฑ์„ ์„ค์น˜ํ•œ๋‹ค. (arrow, binaryornot, chardet, click, future, Jinaja2, jinja2-time, MarkupSafe, poyo, python-dateutil, six, whichcraft) ๋ชจ๋“  ์˜ต์…˜์„ ํฌํ•จํ•œ ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•˜์—ฌ ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์ฐพ์•„์„œ ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•˜๋ฉด ์ข‹์€ ์ง€์นจ์ด ๋œ๋‹ค. 03) Django ํ”„๋กœ์ ํŠธ ์„ค์ • ์„ค์ • ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ • ํ…œํ”Œ๋ฆฟ ์„ค์ • ์ง€์—ญ ์‹œ๊ฐ ๋ฐ ๋‹ค๊ตญ์–ด ์„ค์ • ์ •์  ํŒŒ์ผ ์„ค์ • ๋ฏธ๋””์–ด ํŒŒ์ผ ์„ค์ • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ๋ก ์„ค์ • ์‹ค๋ฌด settings ํŒจํ‚ค์ง€๋กœ ๊ตฌํ˜„ ์„ค์ • ๋ฐ์ดํ„ฐ์˜ ๋ถ„๋ฅ˜ ์ฃผ์š” ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ํŒŒ์ผ ์„ค๋ช… Django ๊ธฐ๋ณธ ์„ค์ • ์˜ต์…˜์€ settings.py ๋ชจ๋“ˆ ํŒŒ์ผ ํ•˜๋‚˜๋กœ ์„ค์ •ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค๋ฌด์ ์œผ๋กœ๋Š” ํŒจํ‚ค์ง€๋กœ ๋งŒ๋“ค์–ด ์„ค์ •์„ ๋‚˜๋ˆ ์„œ ๊ด€๋ฆฌํ•œ๋‹ค. settings.py ๊ธฐ๋ณธ ๊ฐ’์„ ์‚ดํŽด๋ณด๊ณ  ํŒจํ‚ค์ง€๋กœ ๋‚˜๋ˆ„๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ๋‹ค. ์„ค์ • ๊ธฐ๋ณธ settings.py ํŒŒ์ผ์˜ ๋‚ด์šฉ์€ ํฌ๊ฒŒ ๋‹ค์Œ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ • ํ…œํ”Œ๋ฆฟ ์„ค์ • ์ง€์—ญ ์‹œ๊ฐ ๋ฐ ๋‹ค๊ตญ์–ด ์„ค์ • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋“ฑ๋ก ์ •์  ํŒŒ์ผ ์„ค์ • ๋ฏธ๋””์–ด ํŒŒ์ผ ์„ค์ • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ • DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } ํ”„๋กœ์ ํŠธ ๊ธฐ๋ณธ ์„ค์ •๊ฐ’์€ ์œ„์™€ ๊ฐ™์ด sqlite๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™ ์„ค์ •์€ ๋‹ค์Œ ๋งํฌ์˜ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•œ๋‹ค. Django ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™ ์„ค์ • ํ…œํ”Œ๋ฆฟ ์„ค์ • TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] ํ”„๋กœ์ ํŠธ ๊ธฐ๋ณธ ์„ค์ •๊ฐ’์€ ์œ„์™€ ๊ฐ™์œผ๋ฉฐ templates ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ DIRS ๋ณ€์ˆ˜๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค. 'DIRS': [os.path.join(BASE_DIR, 'templates')], ์ง€์—ญ ์‹œ๊ฐ ๋ฐ ๋‹ค๊ตญ์–ด ์„ค์ • LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True ํ”„๋กœ์ ํŠธ ๊ธฐ๋ณธ ์„ค์ •๊ฐ’์€ ์œ„์™€ ๊ฐ™์œผ๋ฉฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค. LANGUAGE_CODE = 'ko-kr' TIME_ZONE = 'Asia/Seoul' USE_I18N = True USE_L10N = True USE_TZ = True ์ •์  ํŒŒ์ผ ์„ค์ • STATIC_URL = '/static/' ํ”„๋กœ์ ํŠธ ๊ธฐ๋ณธ ์„ค์ •๊ฐ’์€ ์œ„์™€ ๊ฐ™์œผ๋ฉฐ STATIC_URL ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] ๋ฏธ๋””์–ด ํŒŒ์ผ ์„ค์ • ํ”„๋กœ์ ํŠธ ๊ธฐ๋ณธ ์„ค์ •๊ฐ’์€ ์—†์œผ๋ฉฐ ํŒŒ์ผ ์—…๋กœ๋“œ ๊ธฐ๋Šฅ ๊ตฌํ˜„์„ ์œ„ํ•ด ๋‹ค์Œ ์„ค์ •์„ ์ถ”๊ฐ€ํ•œ๋‹ค. MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') runserver ํ…Œ์ŠคํŠธ ์„œ๋ฒ„ ๊ตฌ๋™ ์‹œ์— /media ๊ฒฝ๋กœ๋ฅผ ์˜ฌ๋ฐ”๋กœ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋”ฐ๋ผ์„œ URL ํŒจํ„ด ๋งค์นญ์„ ํ•ด์ฃผ๋Š” ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. from django.conf.urls.static import static urlpatterns = [ .... ] if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ๋ก bookmark ์•ฑ์„ ์ƒ์„ฑํ•œ ๊ฒฝ์šฐ ํ•ด๋‹น ์•ฑ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ๊ธฐ๋ณธ ์ƒ์„ฑ ํŒŒ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. bookmark/ migrations/ __init__.py admin.py apps.py models.py tests.py views.py Django ๋ฒ„์ „ 1.9๋ถ€ํ„ฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋Š” apps.py ํŒŒ์ผ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋“ฑ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'bookmark.apps.BookmarkConfig', ] ๋‹จ์ˆœํžˆ bookmark ๋ชจ๋“ˆ ์ด๋ฆ„์œผ๋กœ ๋“ฑ๋กํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ bookmark.apps.BookmarkConfig ์„ค์ • ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ์ด ๋ณด๋‹ค ์ •ํ™•ํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค. ์„ค์ • ์‹ค๋ฌด settings ํŒจํ‚ค์ง€๋กœ ๊ตฌํ˜„ Django ๊ธฐ๋ณธ ์„ค์ • ์˜ต์…˜์€ settings.py ๋ชจ๋“ˆ ํŒŒ์ผ ํ•˜๋‚˜๋กœ ์„ค์ •ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค๋ฌด์ ์œผ๋กœ๋Š” ํŒจํ‚ค์ง€๋กœ ๋งŒ๋“ค์–ด ์„ค์ •์„ ๋‚˜๋ˆ ์„œ ๊ด€๋ฆฌํ•œ๋‹ค. ์ฆ‰, settings.py ํŒŒ์ผ ํ•˜๋‚˜๊ฐ€ ์•„๋‹ˆ๋ผ settings ํŒจํ‚ค์ง€(๋””๋ ‰ํ„ฐ๋ฆฌ)๋ฅผ ๋งŒ๋“ค๊ณ  ์ด ์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ชจ๋“ˆ(ํŒŒ์ผ)์„ ์ƒ์„ฑํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ_์ด๋ฆ„/ repo/ app1/ app2/ conf/ __init__.py settings/ base.py local.py production.py test.py urls.py wsgi.py manage.py README.md requirements.txt .gitignore venv/ run/ uwsgi.ini uwsgi.sock gunicorn.sock logs/ ssl/ ์„ค์ • ๋ฐ์ดํ„ฐ์˜ ๋ถ„๋ฅ˜ settings ํŒจํ‚ค์ง€(๋””๋ ‰ํ„ฐ๋ฆฌ) ์•ˆ์—๋Š” base.py, local.py, production.py ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ชจ๋“ˆ(ํŒŒ์ผ)์ด ๋“ค์–ด์žˆ๋‹ค. ์ด ํŒŒ์ผ๋“ค์˜ ์šฉ๋„์™€ ๊ตฌ๋ถ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํ™˜๊ฒฝ๋งˆ๋‹ค ๋‹ค๋ฅธ ๊ณต๊ฐœ์ ์ธ ์„ค์ • local.py, test.py, production.py ๋“ฑ์œผ๋กœ ํŒŒ์ผ์„ ๋‚˜๋ˆ„๊ณ  ์ €์žฅ์†Œ์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค. ๋ชจ๋“  ํ™˜๊ฒฝ์—์„œ ๋™์ผํ•œ ๊ณต๊ฐœ์ ์ธ ์„ค์ • base.py ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ ๊ตฌ์ฒด์ ์ธ local.py, test, py, production.py ๋“ฑ์—์„œ ์ž„ํฌํŠธ ํ•œ๋‹ค. ํ™˜๊ฒฝ๋งˆ๋‹ค ๋‹ค๋ฅธ ๋น„๊ณต๊ฐœ์ ์ธ ์„ค์ • local.py, test.py, production.py ๋“ฑ์œผ๋กœ ํŒŒ์ผ์„ ๋‚˜๋ˆ„๊ณ  ์„ค์ •๊ฐ’์€ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ๋กœ๋“œํ•œ๋‹ค. ๋ชจ๋“  ํ™˜๊ฒฝ์—์„œ ๋™์ผํ•œ ๋น„๊ณต๊ฐœ์ ์ธ ์„ค์ • base.py ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์„ค์ •๊ฐ’์€ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ๋กœ๋“œํ•˜๋ฉฐ ๊ตฌ์ฒด์ ์ธ local.py, test.py, production.py ๋“ฑ์—์„œ ์ž„ํฌํŠธ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๊ณต๊ฐœ์ ์ธ ์„ค์ •์€ ์ €์žฅ์†Œ์—์„œ ์ฝ”๋“œ๋กœ ์ €์žฅ๋˜์–ด ๊ด€๋ฆฌ๋˜์ง€๋งŒ ๋น„๊ณต๊ฐœ ์„ค์ •์€ ์ €์žฅ์†Œ์— ์ ˆ๋Œ€ ์ปค๋ฐ‹ ๋˜์–ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. ๋น„๊ณต๊ฐœ ์„ค์ • ์ •๋ณด ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๊ฐ€์žฅ ํ”ํ•˜๋ฉฐ ์‹œ์Šคํ…œ์— local.sh, production.sh์™€ ๊ฐ™์€ ์‰˜ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฃผ์š” ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ํŒŒ์ผ ์„ค๋ช… ํ”„๋กœ์ ํŠธ ๋‹จ์œ„๋กœ ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ฐ€์ง„๋‹ค. repo: Django ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์†Œ์Šค ์ฝ”๋“œ ๋””๋ ‰ํ„ฐ๋ฆฌ = ์ €์žฅ์†Œ ์ปค๋ฐ‹ ๊ด€๋ฆฌ venv: ๊ฐ€์ƒํ™˜๊ฒฝ ๋””๋ ‰ํ„ฐ๋ฆฌ run: ๋ฐฐํฌ ํ›„ ์†Œ์ผ“ ํŒŒ์ผ ๋ฐ ์—ฐ๋™ ์„ค์ • ํŒŒ์ผ์ด ์œ„์น˜ํ•˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ logs: ๋กœ๊ทธ ๋””๋ ‰ํ„ฐ๋ฆฌ ssl: SSL ํ‚ค ํŒŒ์ผ ๋””๋ ‰ํ„ฐ๋ฆฌ mkdir ๋ช…๋ น์–ด๋กœ repo ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑ ํ›„ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—์„œ startproject ์˜ต์…˜์œผ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ conf ์ด๋ฆ„ ๋Œ€์‹ ์— ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ํ”„๋กœ์ ํŠธ์˜ ์„ค์ • ํŒŒ์ผ์ด ๋“ค์–ด๊ฐ€๋ฏ€๋กœ conf ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ํƒ€ ํ”„๋กœ์ ํŠธ์™€ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋ฆ„์„ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์œผ๋กœ ํ•œ๋‹ค. venv ๋””๋ ‰ํ„ฐ๋ฆฌ๋Š” ์ข…์†์„ฑ ๋ถ„๋ฆฌ์˜ ์›์น™์— ๋”ฐ๋ผ ํ”„๋กœ์ ํŠธ์˜ ๋…๋ฆฝ์ ์ธ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์ œ๊ณตํ•œ๋‹ค. ํŒŒ์ด์ฌ 3.4 ๋ฒ„์ „๋ถ€ํ„ฐ๋Š” ๋ณ„๋„์˜ virtualenv ํŒจํ‚ค์ง€ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ๊ฐ€์ƒํ™˜๊ฒฝ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. run ๋””๋ ‰ํ„ฐ๋ฆฌ๋Š” WSGI ์„œ๋ฒ„๊ฐ€ ๋™์ž‘ํ•  ๋•Œ ์†Œ์ผ“ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์ด๋‹ค. ๋ณดํ†ต์€ ๊ทธ๋ฃน์˜ ์†Œ์œ ๊ถŒ์„ www-data๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ๋งŒ์•ฝ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ manage.py๋กœ ํ…Œ์ŠคํŠธ ์„œ๋ฒ„๋ฅผ ๊ตฌ๋™ํ•œ๋‹ค๋ฉด ๋ถˆํ•„์š”ํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ์ด๋‹ค. Two scoops of Django ์ฑ…์—์„œ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ข…๋ฅ˜๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ๋ถ„ํ•œ๋‹ค. ์ €์žฅ์†Œ ๋ฃจํŠธ Django ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ : repo ์„ค์ • ๋ฃจํŠธ : conf ํ•ด๋‹น ์ฑ…์—์„œ๋Š” ์ตœ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์ €์žฅ์†Œ ๋ฃจํŠธ๋กœ ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ์™€ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•˜์ง€๋งŒ ๊ตณ์ด ๋ถ„๋ฆฌํ•  ํ•„์š”์„ฑ์„ ์•„์ง์€ ๋Š๋ผ์ง€ ๋ชปํ–ˆ๋‹ค. 04) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™ SQLite ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘์† ์„ค์ • PostgreSQL ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฐ ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘์† ์„ค์ • MySQL SQLite SQLite ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ณ„๋„์˜ ํŒจํ‚ค์ง€ ์„ค์น˜๊ฐ€ ํ•„์š” ์—†๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘์† ์„ค์ • conf/settings.py ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์„ค์ •์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค. SQLite๋Š” ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ด๋ฏ€๋กœ ๋ณ„๋„์˜ ์•„์ด๋””/๋น„๋ฐ€๋ฒˆํ˜ธ ์ ‘์† ์ •๋ณด๊ฐ€ ์—†๋‹ค. DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } PostgreSQL ํŒจํ‚ค์ง€ ์„ค์น˜ ๋…๋ฆฝ๋œ ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ psycopg2 ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ๋‹ค. pip install psycopg2 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฐ ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์‚ฌ์šฉ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด psql ์ฝ˜์†”์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์‹คํ–‰ํ•œ๋‹ค. sudo su - postgres psql psql (9.5.4) Type "help" for help. ์•„๋ž˜์™€ ๊ฐ™์ด django_test ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์‹œ์Šคํ…œ ๊ณ„์ •์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ์ž django_user๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. CREATE DATABASE django_test; CREATE USER django_user WITH PASSWORD 'django_pass'; ALTER ROLE django_user SET client_encoding TO 'utf8'; ALTER ROLE django_user SET default_transaction_isolation TO 'read committed'; ALTER ROLE django_user SET timezone TO 'UTC'; GRANT ALL PRIVILEGES ON DATABASE django_test TO django_user; \q ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์‚ฌ์šฉ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ถŒํ•œ๊นŒ์ง€ ๋ถ€์—ฌํ–ˆ์œผ๋ฏ€๋กœ psql ์‚ฌ์šฉ์ž์—์„œ ๋น ์ ธ๋‚˜์˜จ๋‹ค. exit ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘์† ์„ค์ • conf/settings.py ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์„ค์ •์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ์ ‘์† ์ •๋ณด๋Š” ์•ž์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑํ•  ๋•Œ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„, ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ๊ฐ™๋‹ค. DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'django_test', 'USER': 'django_user', 'PASSWORD': 'django_pass', 'HOST': 'localhost', 'PORT': '', } } MySQL 02. ๋ชจ๋ธ๊ณผ ORM 01) ๋ชจ๋ธ์˜ ์„ ์–ธ ๋ชจ๋ธ์˜ ์„ ์–ธ๊ณผ ๊ด€๋ฆฌ์ž ํ™”๋ฉด์— ๋“ฑ๋ก ๋ชจ๋ธ์˜ ์„ ์–ธ ๊ด€๋ฆฌ์ž ํ™”๋ฉด์— ๋“ฑ๋ก ์ฃผ์š” ํ•„๋“œ ํ•„๋“œ ํƒ€์ž…๊ณผ ์˜ต์…˜ DateTimeField Meta ๋‚ด๋ถ€ ํด๋ž˜์Šค ์˜ต์…˜ verbose_name ์˜ต์…˜ verbose_name_plural ์˜ต์…˜ ordering ์˜ต์…˜ db_table ์˜ต์…˜ abstract ์˜ต์…˜ ๋ชจ๋ธ ์ฝ”๋”ฉ ๊ฐ€์ด๋“œ ํ•„๋“œ ์ด๋ฆ„์€ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ _(๋ฐ‘์ค„)๋กœ ๋„์–ด ์“ด๋‹ค. Meta ํด๋ž˜์Šค๋Š” ํ•„๋“œ ์„ ์–ธ ๋ฐ‘์— ํ•œ ์ค„ ๋„๊ณ  ์œ„์น˜ํ•œ๋‹ค. ์ฃผ์š” ๋ฉ”์„œ๋“œ๊ฐ€ ์œ„์น˜ํ•˜๋Š” ์ˆœ์„œ ๋ชจ๋ธ์˜ ์„ ์–ธ๊ณผ ๊ด€๋ฆฌ์ž ํ™”๋ฉด์— ๋“ฑ๋ก ๋ชจ๋ธ์˜ ์„ ์–ธ models.py ํŒŒ์ผ ์˜ˆ์‹œ from django.db import models class Bookmark(models.Model): title = models.CharField(max_length=100, blank=True, null=True) url = models.URLField('url', unique=True) class Meta: verbose_name = '๋ถ๋งˆํฌ' verbose_name_plural = '๋ถ๋งˆํฌ ๋ชจ์Œ' ordering = ['title', ] def __str__(self): return self.title ๊ด€๋ฆฌ์ž ํ™”๋ฉด์— ๋“ฑ๋ก admin.py ํŒŒ์ผ ์˜ˆ์‹œ from django.contrib import admin from bookmark.models import Bookmark class BookmarkAdmin(admin.ModelAdmin): list_display = ('title', 'url') admin.site.register(Bookmark, BookmarkAdmin) ์ฃผ์š” ํ•„๋“œ ํ•„๋“œ ํƒ€์ž…๊ณผ ์˜ต์…˜ DateTimeField ๊ธฐ๋ณธ๊ฐ’ = ์ƒ์„ฑ ์‹œ ์ƒ์„ฑ ์‹œ๊ฐ = ์‚ฌ์šฉ์ž ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ pub_date = models.DateTimeField('date published', default=datetime.datetime.now) ๊ธฐ๋ณธ๊ฐ’ = ์ƒ์„ฑ ์‹œ ์ƒ์„ฑ ์‹œ๊ฐ = ์‚ฌ์šฉ์ž ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ (์ž…๋ ฅ ํ•„๋“œ ๋…ธ์ถœ ์•ˆ ํ•จ) pub_date = models.DateTimeField('date published', default=datetime.datetime.now, editable=False) ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€ ์‹œ ์ž๋™ ์ถ”๊ฐ€ = ์‚ฌ์šฉ์ž ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€(์ž…๋ ฅ ํ•„๋“œ ๋…ธ์ถœ ์•ˆ ํ•จ) created = models.DateTimeField(auto_now_add=True) ๋ ˆ์ฝ”๋“œ ๋ณ€๊ฒฝ ์‹œ ์ž๋™ ์ถ”๊ฐ€ = ์‚ฌ์šฉ์ž ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€(์ž…๋ ฅ ํ•„๋“œ ๋…ธ์ถœ ์•ˆ ํ•จ) updated = models.DateTimeField(auto_now=True) Meta ๋‚ด๋ถ€ ํด๋ž˜์Šค ์˜ต์…˜ verbose_name ์˜ต์…˜ ์‚ฌ์šฉ์ž๊ฐ€ ์ฝ๊ธฐ ์‰ฌ์šด ๋ชจ๋ธ ๊ฐ์ฒด์˜ ์ด๋ฆ„์œผ๋กœ ๊ด€๋ฆฌ์ž ํ™”๋ฉด ๋“ฑ์—์„œ ํ‘œ์‹œ๋œ๋‹ค. ์˜์–ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์ˆ˜ํ˜•์ด๋‹ค. verbose_name ์˜ต์…˜์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด CamelCase ํด๋ž˜์Šค ์ด๋ฆ„์„ ๊ธฐ์ค€์œผ๋กœ camel case ์ด์™€ ๊ฐ™์ด ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. verbose_name_plural ์˜ต์…˜ ์‚ฌ์šฉ์ž๊ฐ€ ์ฝ๊ธฐ ์‰ฌ์šด ๋ชจ๋ธ ๊ฐ์ฒด์˜ ์ด๋ฆ„์œผ๋กœ ๊ด€๋ฆฌ์ž ํ™”๋ฉด ๋“ฑ์—์„œ ํ‘œ์‹œ๋˜๋Š” ๊ฒƒ์€ ๋™์ผํ•˜๋‚˜ ์˜์–ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ณต์ˆ˜ํ˜•์ด๋‹ค. ํ•œ๊ตญ์–ด์—์„œ๋Š” ๊ตณ์ด ๋‹จ์ˆ˜์™€ ๋ณต์ˆ˜๋ฅผ ๊ตฌ๋ณ„ํ•ด ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ verbose_name๊ณผ ๋™์ผํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค. verbose_name_plural ์˜ต์…˜์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด verbose_name์— s๋ฅผ ๋ถ™์ธ๋‹ค. ordering ์˜ต์…˜ ๋ชจ๋ธ์˜ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•˜๋ฉฐ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์ง€์ •ํ•  ๊ฒฝ์šฐ ํ•„๋“œ ์ด๋ฆ„์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋‚˜์—ดํ•œ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  -๋ฅผ ๋ถ™์ด๋ฉด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. ๋‹ค์Œ ์˜ˆ์‹œ๋Š” pub_date ํ•„๋“œ ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋‹ค์‹œ author ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค. ordering = ['-pub_date', 'author'] db_table ์˜ต์…˜ abstract ์˜ต์…˜ ๋ชจ๋ธ ์ฝ”๋”ฉ ๊ฐ€์ด๋“œ ํ•„๋“œ ์ด๋ฆ„์€ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ _(๋ฐ‘์ค„)๋กœ ๋„์–ด ์“ด๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ•„๋“œ ์ด๋ฆ„ ์„ ์–ธ์„ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ํ•˜๊ณ  _๋กœ ๋„์–ด ์“ด๋‹ค. class Person(models.Model): first_name = models.CharField(max_length=20) last_name = models.CharField(max_length=40) Meta ํด๋ž˜์Šค๋Š” ํ•„๋“œ ์„ ์–ธ ๋ฐ‘์— ํ•œ ์ค„ ๋„๊ณ  ์œ„์น˜ํ•œ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ•„๋“œ ์ด๋ฆ„ ์„ ์–ธ ์•„๋ž˜ ํ•œ ์ค„ ๋„๊ณ  Meta ํด๋ž˜์Šค๊ฐ€ ์œ„์น˜ํ•œ๋‹ค. class Person(models.Model): first_name = models.CharField(max_length=20) last_name = models.CharField(max_length=40) class Meta: verbose_name_plural = 'people' ์ฃผ์š” ๋ฉ”์„œ๋“œ๊ฐ€ ์œ„์น˜ํ•˜๋Š” ์ˆœ์„œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ•„๋“œ ์ปค์Šคํ…€ ๋งค๋‹ˆ์ € ์†์„ฑ Meta ํด๋ž˜์Šค def __init__() ๋ฉ”์„œ๋“œ def __str__() ๋ฉ”์„œ๋“œ def save() ๋ฉ”์„œ๋“œ def get_absolute_url() ๋ฉ”์„œ๋“œ ๊ธฐํƒ€ ์ปค์Šคํ…€ ๋ฉ”์„œ๋“œ 02) ๋ชจ๋ธ์˜ ๊ด€๊ณ„ ๋งคํ•‘ ๋ชจ๋ธ์˜ ๊ด€๊ณ„ ๋งคํ•‘ ์ผ๋Œ€๋‹ค ๊ด€๊ณ„ ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„ ์ผ๋Œ€์ผ ๊ด€๊ณ„ ์กฐ์ธ(join) select_related prefetch_related ๋ชจ๋ธ์˜ ๊ด€๊ณ„ ๋งคํ•‘ ์ผ๋Œ€๋‹ค ๊ด€๊ณ„ ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„ ์ผ๋Œ€์ผ ๊ด€๊ณ„ ์กฐ์ธ(join) select_related ์™ธ๋ž˜ ํ‚ค(foreign key) ์ฐธ์กฐ ๊ด€๊ณ„์—์„œ ๊ด€๊ณ„๋œ ํ•„๋“œ์˜ ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๊ด€๊ณ„๋ณ„๋กœ ๊ฐœ๋ณ„ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ถ€๋‹ด์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•ด ์ ์ ˆํ•œ ๊ณณ์— select_related() ๋ฉ”์„œ๋“œ๋กœ JOIN์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์™ธ๋ž˜ ํ‚ค๊ฐ€ NOT NULL์ธ ๊ฒฝ์šฐ์—๋Š” INNER JOIN์œผ๋กœ ๋งŒ๋“ค๊ณ  NULL์ธ ๊ฒฝ์šฐ์—๋Š” LEFT OUTER JOIN์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค. Message.objects \ .filter(board__slug=self.kwargs['slug']) \ .filter(status='published') \ .order_by('-created') ๋จผ์ € ์œ„์™€ ๊ฐ™์ด select_related() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ํ…œํ”Œ๋ฆฟ์—์„œ {% url 'board:message-detail' message.board.slug message.id %}, {{ message.author }} ์‚ฌ์šฉํ•  ๋•Œ๋งˆ๋‹ค ์•„๋ž˜์˜ ๊ฐœ๋ณ„ ์ฟผ๋ฆฌ๊ฐ€ ์ด 3ํšŒ ์ˆ˜ํ–‰๋œ๋‹ค. ๋ฌธ์ œ๋Š” ๊ฒŒ์‹œ๋ฌผ ๋ชฉ๋ก์ด 20๊ฐœ๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ํ•œ ํŽ˜์ด์ง€ ๋ Œ๋”๋ง์— 3๊ฐœ ์ฟผ๋ฆฌ๊ฐ€ 20๋ฒˆ ์ด 60ํšŒ ์ˆ˜ํ–‰๋œ๋‹ค. SELECT "board_message"."id", "board_message"."board_id", "board_message"."title", "board_message"."author_id", "board_message"."content", FROM "board_message" WHERE ("board_message"."status" = 'published') ORDER BY "board_message"."created" DESC; SELECT "board_board"."id", "board_board"."title", "board_board"."slug", FROM "board_board" WHERE "board_board"."id" = 1; SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."user name", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = 1; ์—ฌ๋Ÿฌ ์ฐจ๋ก€ ๊ฐœ๋ณ„ ์ฟผ๋ฆฌ๊ฐ€ ๋ฐ˜๋ณต ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์„ JOIN์„ ํ†ตํ•ด ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. Message.objects \ .select_related('author') \ .select_related('board') \ .filter(board__slug=self.kwargs['slug']) \ .filter(status='published') \ .order_by('-created') ์œ„์™€ ๊ฐ™์ด ๊ฐ์ฒด ๋ชฉ๋ก์„ ๊ตฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์กฐ์ธํ•œ ์ฟผ๋ฆฌ๋ฌธ์„ ์‹คํ–‰ํ•œ๋‹ค. SELECT "board_message"."id", "board_message"."board_id", "board_message"."title", "board_message"."author_id", "board_message"."content", "board_board"."id", "board_board"."title", "board_board"."slug", "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."user name", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "board_message" INNER JOIN "board_board" ON ("board_message"."board_id" = "board_board"."id") LEFT OUTER JOIN "auth_user" ON ("board_message"."author_id" = "auth_user"."id") WHERE ("board_board"."slug" = 'pic' AND "board_message"."status" = 'published') ORDER BY "board_message"."created" DESC; prefetch_related 03) ๋ชจ๋ธ ๋งค๋‹ˆ์ € ๋ชจ๋ธ ๋งค๋‹ˆ์ € ๋””ํดํŠธ ๋ชจ๋ธ ๋งค๋‹ˆ์ € = objects ์ปค์Šคํ…€ ๋ชจ๋ธ ๋งค๋‹ˆ์ € ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹ ์ปค์Šคํ…œ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ์ •์˜ํ•œ๋‹ค. ๋ชจ๋ธ์— ์ปค์Šคํ…€ ๋งค๋‹ˆ์ €๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ์ปค์Šคํ…€ ๋ชจ๋ธ ๋งค๋‹ˆ์ €์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์˜ˆ์‹œ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ๋ฉ”์„œ๋“œ ์ฒด์ธ๋กœ ์ง€์ •ํ•˜๋Š” ๋ฐฉ์‹ ์ปค์Šคํ…œ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ์ •์˜ํ•œ๋‹ค. ๋ชจ๋ธ์˜ ๊ธฐ๋ณธ ๋งค๋‹ˆ์ €๋ฅผ ์ปค์Šคํ…€ ๋ฉ”์„œ๋“œ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ์ปค์Šคํ…€ ๋ชจ๋ธ ๋งค๋‹ˆ์ €์˜ ๋ฉ”์„œ๋“œ ์ฒด์ธ ํ˜ธ์ถœ ์˜ˆ์‹œ ์ •๋ฆฌ ์ฐธ๊ณ ๋ฌธํ—Œ ๋ชจ๋ธ ๋งค๋‹ˆ์ € ๋””ํดํŠธ ๋ชจ๋ธ ๋งค๋‹ˆ์ € = objects ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฟผ๋ฆฌ์™€ ์—ฐ๋™๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค. ๊ฐ ๋ชจ๋ธ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ตœ์†Œ ํ•˜๋‚˜์˜ ๋งค๋‹ˆ์ €๋ฅผ ๊ฐ€์ง„๋‹ค. ๋””ํดํŠธ ๋ชจ๋ธ ๋งค๋‹ˆ์ €์˜ ์ด๋ฆ„์€ objects์ด๋‹ค. ์ปค์Šคํ…€ ๋ชจ๋ธ ๋งค๋‹ˆ์ € ์ปค์Šคํ…€ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€์ด๋‹ค. ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ๋งค๋ฒˆ ์ถ”๊ฐ€ํ•ด์„œ ํŠน์ • ๋ชจ๋ธ ๋งค๋‹ˆ์ € ์ธ์Šคํ„ด์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ์‹ ๋””ํดํŠธ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ๋ณ€๊ฒฝ ํ›„ ๋ฉ”์„œ๋“œ ์ฒด์ธ์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹ ์ปค์Šคํ…œ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ์ •์˜ํ•œ๋‹ค. blog/models.py ํŒŒ์ผ์— ์ถ”๊ฐ€ class PublishedManager(models.Manager): def get_queryset(self): return super(PublishedManager, self).get_queryset().filter(status='published') ๋ชจ๋ธ์— ์ปค์Šคํ…€ ๋งค๋‹ˆ์ €๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. blog/models.py ํŒŒ์ผ ์ˆ˜์ • class Post(models.Model): objects = models.Manager() published = PublishedManager() STATUS_CHOICES = ( ('draft', 'Draft'), ('published', 'Published'), ) publish = models.DateTimeField(default=timezone.now) ... ์ƒ๋žต ... ์ปค์Šคํ…€ ๋ชจ๋ธ ๋งค๋‹ˆ์ €์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์˜ˆ์‹œ >>> from blog.models import Post >>> Post.objects.count() >>> Post.published.count() ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ๋ฉ”์„œ๋“œ ์ฒด์ธ๋กœ ์ง€์ •ํ•˜๋Š” ๋ฐฉ์‹ ์ปค์Šคํ…œ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ์ •์˜ํ•œ๋‹ค. blog/models.py ํŒŒ์ผ์— ์ถ”๊ฐ€ class PublishedManager(models.Manager): use_for_related_fields = True def published(self, **kwargs): return self.filter(status='published', **kwargs) use_for_related_fields = True ์˜ต์…˜์€ ๊ธฐ๋ณธ ๋งค๋‹ˆ์ €๋กœ ์ด ๋งค๋‹ˆ์ €๋ฅผ ์ •์˜ํ•œ ๋ชจ๋ธ์ด ์žˆ์„ ๋•Œ ์ด ๋ชจ๋ธ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ชจ๋“  ๊ด€๊ณ„ ์ฐธ์กฐ์—์„œ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ๋ชจ๋ธ์˜ ๊ธฐ๋ณธ ๋งค๋‹ˆ์ €๋ฅผ ์ปค์Šคํ…€ ๋ฉ”์„œ๋“œ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. blog/models.py ํŒŒ์ผ ์ˆ˜์ • class Post(models.Model): objects = PublishedManager() STATUS_CHOICES = ( ('draft', 'Draft'), ('published', 'Published'), ) publish = models.DateTimeField(default=timezone.now) ... ์ƒ๋žต ... ์ปค์Šคํ…€ ๋ชจ๋ธ ๋งค๋‹ˆ์ €์˜ ๋ฉ”์„œ๋“œ ์ฒด์ธ ํ˜ธ์ถœ ์˜ˆ์‹œ >>> from blog.models import Post >>> Post.objects.count() >>> Post.objects.published().count() ์ •๋ฆฌ Two Scoops of Django ์ฑ…์—์„œ๋Š” ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ๋ฉ”์„œ๋“œ๋กœ ์ง€์ •ํ•˜๋Š” ๋ฐฉ์‹์„ ๊ถŒ์žฅํ•˜๋Š”๋ฐ ๊ทธ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. First, when using model inheritance, children of abstract base classes receive their parentโ€™s model manager, and children of concrete base classes do not. ๋ชจ๋ธ ์ƒ์†์„ ์ด์šฉํ•  ๋•Œ ์ถ”์ƒ(abstract) ํด๋ž˜์Šค์˜ ์ž์‹ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ์˜ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋ฅผ ์ƒ์†๋ฐ›์ง€๋งŒ ๊ตฌ์ƒ(concrete) ํด๋ž˜์Šค์˜ ์ž์‹ ํด๋ž˜์Šค๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค. Second, the first manager applied to a model class is the one that Django treats as the default. This is breaks significantly with the normal Python pattern, causing what can appear to be unpredictable results from QuerySets. ๋ชจ๋ธ ํด๋ž˜์Šค์—์„œ ์„ ์–ธํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ๋ชจ๋ธ ๋งค๋‹ˆ์ €๋Š” Django์˜ ๋””ํดํŠธ ๋งค๋‹ˆ์ €์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋งค๋‹ˆ์ €๋ฅผ ๋‚˜์—ดํ•˜๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์ธ ํŒŒ์ด์ฌ ํŒจํ„ด์— ๋งค์šฐ ์–ด๊ธ‹๋‚˜๋Š” ๊ฒƒ์œผ๋กœ QuerySets๋กœ๋ถ€ํ„ฐ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์ฐธ๊ณ ๋ฌธํ—Œ How to use custom manager with related objects? 04) ๋ชจ๋ธ ์ƒ์† 05) ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์ œ๊ฑฐ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์ œ๊ฑฐ ์ฒซ ๋ฒˆ์งธ ์‹œ๋‚˜๋ฆฌ์˜ค ๋‘ ๋ฒˆ์งธ ์‹œ๋‚˜๋ฆฌ์˜ค ํ˜„์žฌ ๋ฐ˜์˜ ์•ˆ ๋œ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์ด ์—†๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ ์•ฑ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํžˆ์Šคํ† ๋ฆฌ ์‚ญ์ œ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์‚ญ์ œ ์ดˆ๊ธฐ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์ƒ์„ฑ ํŽ˜์ดํฌ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์ œ๊ฑฐ Django ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋„๊ตฌ๋Š” ๋ชจ๋ธ ๊ด€๋ฆฌ์— ํ›Œ๋ฅญํ•œ ๋„๊ตฌ์ด๋‹ค ๊ทธ๋Ÿฌ๋‚˜ ๋„ˆ๋ฌด ๋งŽ์ด ์ €์žฅ์†Œ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋•Œ๋กœ๋Š” ๋ถˆํ•„์š”ํ•œ ๋ฌธ์ œ๋‚˜ ๋ถ€์ž‘์šฉ์„ ์ผ์œผํ‚ค๊ธฐ๋„ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ€๋”์€ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์„ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ฒซ ๋ฒˆ์งธ ์‹œ๋‚˜๋ฆฌ์˜ค ์•„์ง ์ „ํ˜€ ๋ฐฐํฌ๋˜์ง€ ์•Š์€ ๊ฐœ๋ฐœ ์ค‘์ธ ํ”„๋กœ์ ํŠธ๋Š” ์ค‘๊ฐ„์ค‘๊ฐ„์— ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ์‹น<NAME>๋Š” ๊ฒƒ๋„ ์ข‹๋‹ค. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ๋ชจ๋“  ํŒŒ์ผ์„ __init__.py ๋ชจ๋“ˆ ๋นผ๊ณ  ์‹น<NAME>๋‹ค. ๋ฆฌ๋ˆ…์Šค์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋กœ ์‰ฝ๊ฒŒ ์ง€์šธ ์ˆ˜ ์žˆ๋‹ค. find . -path "*/migrations/*.py" -not -name "__init__.py" -delete find . -path "*/migrations/*.pyc" -delete ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. ๊ธฐ๋ณธ SQLite ์—”์ง„์œผ๋กœ ๊ฐœ๋ฐœ ์ค‘์ด๋ผ๋ฉด db.sqlite3 ํŒŒ์ผ์„ ์‚ญ์ œํ•œ๋‹ค. ์ดˆ๊ธฐ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. python manage.py makemigrations python manage.py migrate ๋‘ ๋ฒˆ์งธ ์‹œ๋‚˜๋ฆฌ์˜ค ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์€ ๋ชจ๋‘<NAME>์ง€๋งŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ๋Š” ์œ ์ง€ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์ด๋‹ค. ํ˜„์žฌ ๋ฐ˜์˜ ์•ˆ ๋œ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์ด ์—†๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. python manage.py makemigrations ์œ„์™€ ๊ฐ™์ด ๋ช…๋ นํ•˜์—ฌ ์•„๋ž˜ ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธํ•˜๋ฉด ์ถ”๊ฐ€๋กœ ๋ฐ˜์˜ํ•  ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์ด ์—†๋Š” ๊ฒฝ์šฐ๋‹ค. No changes detected ๊ทธ๋Ÿฌ๋‚˜ ์ถ”๊ฐ€๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์ด ๋งŒ๋“ค์–ด์ง€๋ฉด ์ด๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ นํ•˜์—ฌ ๋ฐ˜์˜ํ•˜๋„๋ก ํ•œ๋‹ค. python manage.py migrate ํ”„๋กœ์ ํŠธ ์•ฑ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํžˆ์Šคํ† ๋ฆฌ ์‚ญ์ œ python manage.py showmigrations ์œ„์™€ ๊ฐ™์ด ๋ช…๋ นํ•˜๋ฉด ํ”„๋กœ์ ํŠธ ์•ˆ์— ์•ฑ๋“ค์˜ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. admin [X] 0001_initial [X] 0002_logentry_remove_auto_add auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length [X] 0004_alter_user_user name_opts [X] 0005_alter_user_last_login_null [X] 0006_require_contenttypes_0002 [X] 0007_alter_validators_add_error_messages [X] 0008_alter_user_user name_max_length blog [X] 0001_initial [X] 0002_auto_20170610_1904 [X] 0003_auto_20170610_1906 [X] 0004_auto_20170610_2044 [X] 0005_auto_20170613_1152 [X] 0006_post_description contenttypes [X] 0001_initial [X] 0002_remove_content_type_name sessions [X] 0001_initial ์œ„ ์˜ˆ์ œ์—์„œ๋Š” blog ์•ฑ์˜ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ นํ•˜์—ฌ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. python manage.py migrate --fake blog zero ์œ„ ๋ช…๋ น์–ด์˜ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. Operations to perform: Unapply all migrations: blog Running migrations: Rendering model states... DONE Unapplying blog.0006_post_description... FAKED Unapplying blog.0005_auto_20170613_1152... FAKED Unapplying blog.0004_auto_20170610_2044... FAKED Unapplying blog.0003_auto_20170610_1906... FAKED Unapplying blog.0002_auto_20170610_1904... FAKED Unapplying blog.0001_initial... FAKED ๋‹ค์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋ช…๋ นํ•  ์ˆ˜ ์žˆ๋‹ค. python manage.py showmigrations ์•„๋ž˜ ๊ฒฐ๊ณผ์—์„œ blog์˜ ๊ฒฝ์šฐ์—๋Š” ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํžˆ์Šคํ† ๋ฆฌ ์•ž์— [X] ํ‘œ์‹œ๊ฐ€ ์‚ฌ๋ผ์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. admin [X] 0001_initial [X] 0002_logentry_remove_auto_add auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length [X] 0004_alter_user_user name_opts [X] 0005_alter_user_last_login_null [X] 0006_require_contenttypes_0002 [X] 0007_alter_validators_add_error_messages [X] 0008_alter_user_user name_max_length blog [ ] 0001_initial [ ] 0002_auto_20170610_1904 [ ] 0003_auto_20170610_1906 [ ] 0004_auto_20170610_2044 [ ] 0005_auto_20170613_1152 [ ] 0006_post_description contenttypes [X] 0001_initial [X] 0002_remove_content_type_name sessions [X] 0001_initial ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์‚ญ์ œ blog ์•ฑ์˜ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— __init__.py ํŒŒ์ผ์„ ๋นผ๊ณ  ๋ชจ๋‘ ์‚ญ์ œํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ํ™•์ธํ•ด ๋ณธ๋‹ค. python manage.py showmigrations ๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด blog ์•ฑ์—๋Š” ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์ด ์—†๋‹ค๋Š” ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. admin [X] 0001_initial [X] 0002_logentry_remove_auto_add auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length [X] 0004_alter_user_user name_opts [X] 0005_alter_user_last_login_null [X] 0006_require_contenttypes_0002 [X] 0007_alter_validators_add_error_messages [X] 0008_alter_user_user name_max_length blog (no migrations) contenttypes [X] 0001_initial [X] 0002_remove_content_type_name sessions [X] 0001_initial ์ดˆ๊ธฐ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์ƒ์„ฑ ์ด์ œ ์ดˆ๊ธฐ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ นํ•œ๋‹ค. python manage.py makemigrations ์ตœ์ดˆ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ 0001_initial.py์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. Migrations for 'blog': blog\migrations\0001_initial.py - Create model Post ํŽ˜์ดํฌ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์ด ์ด๋ฏธ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๋งˆ์น˜ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ นํ•˜์—ฌ ํŽ˜์ดํฌ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•œ๋‹ค. python manage.py migrate --fake-initial ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Operations to perform: Apply all migrations: admin, auth, blog, contenttypes, sessions Running migrations: Applying blog.0001_initial... FAKED ๊ทธ๋ฆฌ๊ณ  ์ตœ์ข… ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด blog ์•ฑ์—๋Š” 0001_initial ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜๋งŒ ๋ฐ˜์˜๋œ ๊ฒƒ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. python manage.py showmigrations admin [X] 0001_initial [X] 0002_logentry_remove_auto_add auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length [X] 0004_alter_user_user name_opts [X] 0005_alter_user_last_login_null [X] 0006_require_contenttypes_0002 [X] 0007_alter_validators_add_error_messages [X] 0008_alter_user_user name_max_length blog [X] 0001_initial contenttypes [X] 0001_initial [X] 0002_remove_content_type_name sessions [X] 0001_initial ์š”์•ฝํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ๋ช…๋ นํ•œ๋‹ค. python manage.py makemigrations python manage.py showmigrations python manage.py migrate --fake ํ”„๋กœ์ ํŠธ_์•ฑ zero ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์‚ญ์ œ python manage.py makemigrations python manage.py migrate --fake-initial 03. ๋ทฐ(์ปจํŠธ๋กค๋Ÿฌ) 01) URL ํŒจํ„ด ๋งคํ•‘ ๊ธฐ๋ณธ ๋ฌธ๋ฒ• url ์ •์˜ ํด๋ž˜์Šค ํ˜• ๋ทฐ์™€ ํ•จ์ˆ˜ํ˜• ๋ทฐ ํ˜ธ์ถœ ํŒŒ์ผ ์ธํด๋ฃจ๋“œ ์ถ”๊ฐ€์‚ฌํ•ญ ์ฃผ์š” ์‹ค๋ฌด ์˜ˆ์ œ PK - ์ •์ˆ˜ ์Šฌ๋Ÿฌ๊ทธ(slug) - ๋ฌธ์ž์—ด PK + ์Šฌ๋Ÿฌ๊ทธ ์กฐํ•ฉ ์‚ฌ์šฉ์ž ์•„์ด๋””(์ด๋ฉ”์ผ ์ฃผ์†Œ ํฌํ•จ) ์—ฐ๋„๋ณ„ ์›”๋ณ„ ์ผ๋ณ„ URI ๊ด€๋ จ ๊ทœ์น™ ๋ฌธ์„œ(document) ์ง‘ํ•ฉ(collection) ์ฐฝ๊ณ (store) ์ปจํŠธ๋กค๋Ÿฌ Post ๋ชจ๋ธ์„ ๋‹ค๋ฃจ๋Š” URL ๊ทœ์น™ ์˜ˆ์‹œ ๊ธฐํƒ€ ์œ ์˜์‚ฌํ•ญ ๊ธฐ๋ณธ ๋ฌธ๋ฒ• url ์ •์˜ urlpatterns = [ url(์ •๊ทœ์‹, ๋ทฐ, kwargs=None, name=None, prefix=''), ] ์ •๊ทœ์‹: URL์„ ์ •๊ทœ์‹์œผ๋กœ ํ‘œํ˜„ ๋ทฐ: URL ๋งค์นญ์ด ๋˜๋ฉด ๋ถˆ๋Ÿฌ์˜ฌ ๋ทฐ (CBV ๋˜๋Š” FBV) kwargs: ์ •๊ทœ์‹ ์ธ์ž์—์„œ ์ถ”์ถœํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ์™ธ์— ์ถ”๊ฐ€์ ์ธ ์ธ์ž๋ฅผ ํŒŒ์ด์ฌ ์‚ฌ์ „ ํƒ€์ž…์˜ ํ‚ค์›Œ๋“œ ์ธ์ž๋กœ ๋ทฐ ํ•จ์ˆ˜์— ์ „๋‹ฌ ๊ฐ€๋Šฅ name: URL ๋ณ„๋กœ ๋ณ„๋ช…์„ ๋‘์–ด ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์—์„œ ์‚ฌ์šฉ prefix: ๋ทฐ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ ‘๋‘์‚ฌ ๋ฌธ์ž์—ด 2.0 ๋ฒ„์ „๋ถ€ํ„ฐ๋Š” path() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค. ๋น„๊ต ์ •๋ฆฌ ์˜ˆ์ • ํด๋ž˜์Šค ํ˜• ๋ทฐ์™€ ํ•จ์ˆ˜ํ˜• ๋ทฐ ํ˜ธ์ถœ ๋ทฐ๋Š” ์š”์ฒญ์„ ๋ฐ›์•„ ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•ด ์ฃผ๋Š” ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์ด๋‹ค. Django์—์„œ๋Š” ๋ทฐ๋ฅผ ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๊ณ  ํด๋ž˜์Šค ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํด๋ž˜์Šค ํ˜• ๋ทฐ ํ˜ธ์ถœ urlpatterns = [ url(r'^$', Home.as_view(), name='home'), ] ํ•จ์ˆ˜ํ˜• ๋ทฐ ํ˜ธ์ถœ urlpatterns = [ url(r'^$', views.home, name='home'), ] ํŒŒ์ผ ์ธํด๋ฃจ๋“œ urlpatterns = [ url(r'^polls/', include('polls.urls', namespace='polls')), ] ์ถ”๊ฐ€์‚ฌํ•ญ 1.8 ๋ฒ„์ „ ์ดํ›„ patterns() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ urlpatterns ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •์˜ํ•œ๋‹ค. ์ฃผ์š” ์‹ค๋ฌด ์˜ˆ์ œ ์ถœ์ฒ˜: List of Useful URL Patterns PK - ์ •์ˆ˜ ์ •๊ทœ์‹: (?P<pk>\d+) ์˜ˆ์‹œ url(r'^questions/(?P<pk>\d+)/$', QuestionDV.as_view(), name='question_detail'), ์Šฌ๋Ÿฌ๊ทธ(slug) - ๋ฌธ์ž์—ด ์ •๊ทœ์‹: (?P<slug>[-\w]+) ์˜ˆ์‹œ url(r'^posts/(?P<slug>[-\w]+)/$', PostDV.as_view(), name='post_detail'), PK + ์Šฌ๋Ÿฌ๊ทธ ์กฐํ•ฉ ์ •๊ทœ์‹: (?P<slug>[-\w]+)-(?P<pk>\d+) ์˜ˆ์‹œ url(r'^blog/(?P<slug>[-\w]+)-(?P<pk>\d+)/$', PostDV.as_view(), name='blog_post'), ์‚ฌ์šฉ์ž ์•„์ด๋””(์ด๋ฉ”์ผ ์ฃผ์†Œ ํฌํ•จ) ์ •๊ทœ์‹: (?P<month>[0-9]{2})/(?P<day>[0-9]{2}) ์˜ˆ์‹œ url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', PostDAV.as_view(), name='post_day_archive'), URI ๊ด€๋ จ ๊ทœ์น™ RESTful API ์ด๋ฆ„ ์ง“๋Š” ๊ทœ์น™์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ผ๊ด€์„ฑ ์žˆ๋Š” ์ด๋ฆ„์„ ์ง€์„ ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. ๋ฌธ์„œ(document) http://api.example.com/device-management/managed-devices/{device-id} http://api.example.com/user-management/users/{id} http://api.example.com/user-management/users/admin ์œ„ ์˜ˆ์‹œ์—์„œ ๋ฌธ์„œ๋Š” device-management, user-management ๊ฐ™์€ ๋‹จ์ˆ˜ํ˜• ๋ช…์‚ฌ์ด๋‹ค. Django์—์„œ๋Š” ์ฃผ๋กœ ์•ฑ ์ด๋ฆ„์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ์ง‘ํ•ฉ(collection) http://api.example.com/device-management/managed-devices http://api.example.com/user-management/users http://api.example.com/user-management/users/{id}/accounts ์œ„ ์˜ˆ์‹œ์—์„œ ์ง‘ํ•ฉ์€ managed-devices, users ๊ฐ™์€ ๋ณต์ˆ˜ํ˜• ๋ช…์‚ฌ์ด๋‹ค. accounts๋Š” ์ง‘ํ•ฉ์ด ์•„๋‹ˆ๊ณ  ์•„๋ž˜์—์„œ ์„ค๋ช…ํ•˜๋Š” ์ฐฝ๊ณ ์ด๋‹ค. ์ฐฝ๊ณ (store) http://api.example.com/cart-management/users/{id}/carts http://api.example.com/song-management/users/{id}/playlists ์œ„ ์˜ˆ์‹œ์—์„œ ์ฐฝ๊ณ ๋Š” carts ๋˜๋Š” playlists ๊ฐ™์€ ๋ณต์ˆ˜ํ˜• ๋ช…์‚ฌ์ด๋‹ค. ์ด๋Š” ์—ฌ๋Ÿฌ users ์ค‘ ํŠน์ • {id} ๊ฐ’์˜ ์‚ฌ์šฉ์ž์˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ, ์žฌ์ƒ๋ชฉ๋ก์„ ๋œปํ•œ๋‹ค. ์ปจํŠธ๋กค๋Ÿฌ http://api.example.com/cart-management/users/{id}/cart/checkout http://api.example.com/song-management/users/{id}/playlist/play ์œ„ ์˜ˆ์‹œ์—์„œ checkout, play ๊ฐ™์€ ๋™์‚ฌ๋กœ ์ปฌ๋ ‰์…˜/์Šคํ† ์–ด์˜ ํ•œ ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ๋™์ž‘(์•ก์…˜)์„ ๋œปํ•œ๋‹ค. Post ๋ชจ๋ธ์„ ๋‹ค๋ฃจ๋Š” URL ๊ทœ์น™ ์˜ˆ์‹œ ๋ชฉ๋ก: /blog/posts/ ์ƒ์„ฑ: /blog/new/ ๋ณด๊ธฐ: /blog/posts/1/ ์ˆ˜์ •: /blog/posts/1/edit/ ์‚ญ์ œ: /blog/posts/1/delete/ ๊ฐ์ฒด์™€ ๋ฌด๊ด€ํ•œ ๋ฉ”์„œ๋“œ: /blog/posts/view-name/ ๊ฐ์ฒด์™€ ๊ด€๋ จ ์žˆ๋Š” ๋ฉ”์„œ๋“œ: /blog/posts/1/view-name/ ๊ธฐํƒ€ ์œ ์˜์‚ฌํ•ญ ๊ณ„์ธต๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด / (์Šฌ๋ž˜์‹œ)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. URI ๋์—๋Š” / (์Šฌ๋ž˜์‹œ)๋ฅผ ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค. ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด _ (๋ฐ‘์ค„) ๋Œ€์‹ ์— - (๋Œ€์‹œ)๋ฅผ ์ผ๊ด€์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๋ชจ๋“  ๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ํ†ต์ผํ•œ๋‹ค. 02) ํด๋ž˜์Šค ํ˜• ๋ทฐ (CBV) ํด๋ž˜์Šค ํ˜• ๋ทฐ (CBV, Class-Based View) CBV์˜ ์žฅ์  CBV ์‚ฌ์šฉ ๊ฐ€์ด๋“œ๋ผ์ธ ์ œ๋„ˆ๋ฆญ ๋ทฐ์™€ ์ƒ์† ์ฃผ์š” ์ œ๋„ˆ๋ฆญ ๋ทฐ ๋ชฉ๋ก ๊ธฐ๋ฐ˜ ๋ทฐ(Base View) ์ œ๋„ˆ๋ฆญ ๋ณด๊ธฐ ๋ทฐ(Generic Display View) ์ œ๋„ˆ๋ฆญ ์ˆ˜์ • ๋ทฐ(Generic Edit View) ์ œ๋„ˆ๋ฆญ ๋‚ ์งœ ๋ทฐ(Generic Date View) ์ œ๋„ˆ๋ฆญ ๋ทฐ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ์†์„ฑ ๋ณ€์ˆ˜ ์˜ค๋ฒ„๋ผ์ด๋”ฉ model queryset template_name context_object_name paginate_by date_field form_class success_url ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ def get_queryset() def get_context_data(**kwargs) def form_valid(form) ๋ชจ๋ธ์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• 3๊ฐ€์ง€ ์˜ˆ์ œ ์ฝ”๋“œ ๋ฏน์Šค์ธ ๋‹ค์ค‘ ์ƒ์† ํด๋ž˜์Šค ํ˜• ๋ทฐ (CBV, Class-Based View) ํด๋ž˜์Šค ํ˜• ๋ทฐ๋Š” ์ƒ์†๊ณผ ๋ฏน์Šค์ธ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ๋ทฐ๋ฅผ ์ฒด๊ณ„์ ์œผ๋กœ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. CBV์˜ ์žฅ์  GET, POST ๋“ฑ HTTP ๋ฉ”์„œ๋“œ์— ๋”ฐ๋ฅธ ์ฒ˜๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ if ํ•จ์ˆ˜ ๋Œ€์‹ ์— ๋ฉ”์„œ๋“œ ๋ช…์œผ๋กœ ์ฝ”๋“œ์˜ ๊ตฌ์กฐ๊ฐ€ ๊น”๋”ํ•˜๋‹ค. ๋‹ค์ค‘ ์ƒ์† ๊ฐ™์€ ๊ฐ์ฒด์ง€ํ–ฅ ๊ธฐ๋ฒ•์„ ํ™œ์šฉํ•ด ์ œ๋„ˆ๋ฆญ ๋ทฐ, ๋ฏน์Šค์ธ ํด๋ž˜์Šค ๋“ฑ์„ ์‚ฌ์šฉํ•ด ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ๊ณผ ๊ฐœ๋ฐœ ์ƒ์‚ฐ์„ฑ์„ ๋†’์—ฌ์ค€๋‹ค. CBV ์‚ฌ์šฉ ๊ฐ€์ด๋“œ๋ผ์ธ ๋ทฐ๋Š” ๊ฐ„๋‹จ ๋ช…๋ฃŒํ•ด์•ผ ํ•œ๋‹ค. ๋ทฐ ์ฝ”๋“œ์˜ ์–‘์€ ์ ์œผ๋ฉด ์ ์„์ˆ˜๋ก ์ข‹๋‹ค. ๋ทฐ ์•ˆ์—์„œ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ทฐ๋Š” ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋กœ์ง์—์„œ ๊ด€๋ฆฌํ•˜๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์€ ๋ชจ๋ธ์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๋งค์šฐ ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ์—๋งŒ ํผ์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค. 403, 404, 500 ์—๋Ÿฌ ํ•ธ๋“ค๋ง์—๋Š” CBV๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  FBV๋ฅผ ์ด์šฉํ•œ๋‹ค. ๋ฏน์Šค์ธ์€ ๊ฐ„๋‹จ๋ช…๋ฃŒํ•ด์•ผ ํ•œ๋‹ค. ์ œ๋„ˆ๋ฆญ ๋ทฐ์™€ ์ƒ์† ์ œ๋„ˆ๋ฆญ ๋ทฐ์˜ 4๊ฐ€์ง€ ๋ถ„๋ฅ˜ ๊ธฐ๋ฐ˜ ๋ทฐ(Base View): ๋ทฐ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค๋ฅธ, ์ œ๋„ˆ๋ฆญ ๋ทฐ์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ๋˜๋Š” ๊ธฐ๋ณธ ์ œ๋„ˆ๋ฆญ ๋ทฐ ์ œ๋„ˆ๋ฆญ ๋ณด๊ธฐ ๋ทฐ(Generic Display View): ๊ฐ์ฒด์˜ ๋ชฉ๋ก ๋˜๋Š” ํ•˜๋‚˜์˜ ๊ฐ์ฒด ์ƒ์„ธ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ทฐ ์ œ๋„ˆ๋ฆญ ์ˆ˜์ • ๋ทฐ(Generic Edit View): ํผ์„ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๋ทฐ ์ œ๋„ˆ๋ฆญ ๋‚ ์งœ ๋ทฐ(Generic Date View): ๋‚ ์งœ ๊ธฐ๋ฐ˜ ๊ฐ์ฒด์˜ ์—ฐ/์›”/์ผ ํŽ˜์ด์ง€๋กœ ๊ตฌ๋ถ„ํ•ด ๋ณด์—ฌ์ฃผ๋Š” ๋ทฐ ์ฃผ์š” ์ œ๋„ˆ๋ฆญ ๋ทฐ ๋ชฉ๋ก ๊ธฐ๋ฐ˜ ๋ทฐ(Base View) View: ์ตœ์ƒ์œ„ ๋ถ€๋ชจ ์ œ๋„ˆ๋ฆญ ๋ทฐ ํด๋ž˜์Šค TemplateView: ์ฃผ์–ด์ง„ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ๋ Œ๋”๋ง RedirectView: ์ฃผ์–ด์ง„ URL๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์ œ๋„ˆ๋ฆญ ๋ณด๊ธฐ ๋ทฐ(Generic Display View) DetailView: ์กฐ๊ฑด์— ๋งž๋Š” ํ•˜๋‚˜์˜ ๊ฐ์ฒด ์ถœ๋ ฅ ListView: ์กฐ๊ฑด์— ๋งž๋Š” ๊ฐ์ฒด ๋ชฉ๋ก ์ถœ๋ ฅ ์ œ๋„ˆ๋ฆญ ์ˆ˜์ • ๋ทฐ(Generic Edit View) FormView: ํผ์ด ์ฃผ์–ด์ง€๋ฉด ํ•ด๋‹น ํผ์„ ์ถœ๋ ฅ CreateView: ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํผ ์ถœ๋ ฅ UpdateView: ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ํผ์„ ์ถœ๋ ฅ DeleteView: ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ•˜๋Š” ํผ์„ ์ถœ๋ ฅ ์ œ๋„ˆ๋ฆญ ๋‚ ์งœ ๋ทฐ(Generic Date View) YearArchiveView: ์ฃผ์–ด์ง„ ์—ฐ๋„์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ์ถœ๋ ฅ MonthArchiveView: ์ฃผ์–ด์ง„ ์›”์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ์ถœ๋ ฅ DayArchiveView: ์ฃผ์–ด์ง„ ๋‚ ์งœ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ์ถœ๋ ฅ TodayArchiveView: ์˜ค๋Š˜ ๋‚ ์งœ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ์ถœ๋ ฅ DateDetailView: ์ฃผ์–ด์ง„ ์—ฐ, ์›”, ์ผ PK(๋˜๋Š” ์Šฌ๋Ÿฌ๊ทธ)์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ์ถœ๋ ฅ ์ œ๋„ˆ๋ฆญ ๋ทฐ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ์†์„ฑ ๋ณ€์ˆ˜ ์˜ค๋ฒ„๋ผ์ด๋”ฉ model ๊ธฐ๋ณธ ๋ทฐ(View, Template, RedirectView) 3๊ฐœ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋“  ์ œ๋„ˆ๋ฆญ ๋ทฐ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. queryset ๊ธฐ๋ณธ ๋ทฐ(View, Template, RedirectView) 3๊ฐœ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋“  ์ œ๋„ˆ๋ฆญ ๋ทฐ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. queryset์„ ์‚ฌ์šฉํ•˜๋ฉด model ์†์„ฑ์€ ๋ฌด์‹œ๋œ๋‹ค. template_name TemplateView๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“  ์ œ๋„ˆ๋ฆญ ๋ทฐ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ๋ช…์„ ๋ฌธ์ž์—ด๋กœ ์ง€์ •ํ•œ๋‹ค. context_object_name ๋ทฐ์—์„œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์— ์ „๋‹ฌํ•˜๋Š” ์ปจํ…์ŠคํŠธ ๋ณ€์ˆ˜๋ช…์„ ์ง€์ •ํ•œ๋‹ค. paginate_by ListView์™€ ๋‚ ์งœ ๊ธฐ๋ฐ˜ ๋ทฐ(์˜ˆ, YearArchiveView)์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. ํŽ˜์ด์ง• ๊ธฐ๋Šฅ์ด ํ™œ์„ฑํ™”๋œ ๊ฒฝ์šฐ ํŽ˜์ด์ง€๋‹น ์ถœ๋ ฅ ํ•ญ๋ชฉ ์ˆ˜๋ฅผ ์ •์ˆ˜๋กœ ์ง€์ •ํ•œ๋‹ค. date_field ๋‚ ์งœ ๊ธฐ๋ฐ˜ ๋ทฐ(์˜ˆ, YearArchiveView)์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ํ•„๋“œ์˜ ํƒ€์ž…์€ DateField ๋˜๋Š” DateTimeField์ด๋‹ค. form_class FormView, CreateView, UpdateView์—์„œ ํผ์„ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•œ๋‹ค. success_url FormView, CreateView, UpdateView, DeleteView์—์„œ ํผ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ์„ฑ๊ณตํ•œ ํ›„ ๋ฆฌ๋””์ด๋ ‰ํŠธํ•  URL ์ฃผ์†Œ์ด๋‹ค. ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ def get_queryset() ๊ธฐ๋ณธ ๋ทฐ(View, Template, RedirectView) 3๊ฐœ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋“  ์ œ๋„ˆ๋ฆญ ๋ทฐ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. ๋””ํดํŠธ๋Š” queryset ์†์„ฑ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. queryset ์†์„ฑ์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋ชจ๋ธ ๋งค๋‹ˆ์ € ํด๋ž˜์Šค์˜ all() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด QuerySet ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค. def get_context_data(**kwargs) ๋ทฐ์—์„œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์— ๋„˜๊ฒจ์ฃผ๋Š” ์ปจํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•œ๋‹ค. def form_valid(form) ๋ชจ๋ธ์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• 3๊ฐ€์ง€ model ์†์„ฑ ๋ณ€์ˆ˜ ์ง€์ • queryset ์†์„ฑ ๋ณ€์ˆ˜ ์ง€์ • def get_queryset() ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ์˜ˆ์ œ ์ฝ”๋“œ from django.views.generic import ListView, DetailView from .models import Question class IndexView(ListView): template_name = 'cbvpolls/index.html' context_object_name = 'latest_question_list' def get_queryset(self): return Question.objects.order_by('-pub_date')[:5] class DetailView(DetailView): model = Question template_name = 'cbvpolls/detail.html' class ResultsView(DetailView): model = Question template_name = 'cbvpolls/results.html' ๋ฏน์Šค์ธ ๋‹ค์ค‘ ์ƒ์† 03) ํ•จ์ˆ˜ํ˜• ๋ทฐ (FBV) ํ•จ์ˆ˜ํ˜• ๋ทฐ (FBV, Function-Based View) ์˜ˆ์ œ ์ฝ”๋“œ ํ•จ์ˆ˜ํ˜• ๋ทฐ (FBV, Function-Based View) ์˜ˆ์ œ ์ฝ”๋“œ from django.shortcuts import render, get_object_or_404 from django.core.urlresolvers import reverse from .models import Question def index(request): latest_question_list = Question.objects.all().order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context) def detail(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/detail.html', {'question': question}) def results(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/results.html', {'question': question}) 04. ํ…œํ”Œ๋ฆฟ 01) ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜ ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜ request ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜ request GET ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฏธํฌํ•จ: {{ request.path }} - /board/free GET ํŒŒ๋ผ๋ฏธํ„ฐ ํฌํ•จ: {{ request.get_full_path }} /board/free?page=3&q=test 02) ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ ์ค„ ๋ฐ”๊ฟˆ ๋ฌธ์ž ์ž๋ฅด๊ธฐ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ํŠน์ˆ˜๋ฌธ์ž ์ด์Šค์ผ€์ดํ”„ HTML ํƒœ๊ทธ ์ œ๊ฑฐ ๋Œ€๋ฌธ์ž๋กœ ์†Œ๋ฌธ์ž๋กœ ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋„์–ด์“ฐ๊ธฐ ์•ˆ ํ•˜๊ธฐ ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ ์ค„ ๋ฐ”๊ฟˆ ๋ฌธ์ž ์ž๋ฅด๊ธฐ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ํŠน์ˆ˜๋ฌธ์ž ์ด์Šค์ผ€์ดํ”„ HTML ํƒœ๊ทธ ์ œ๊ฑฐ ๋Œ€๋ฌธ์ž๋กœ ์†Œ๋ฌธ์ž๋กœ ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ blog ์ด๋ฆ„์˜ ์•ฑ ์•ˆ์— ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์†Œ์Šค ํŒŒ์ผ์˜ ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. blog/ templatetags/ __init__.py blog_filters.py blog_tags.py ์•ฑ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— templatetags ์ด๋ฆ„์˜ ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ์•ˆ์—๋Š” ๋‚ด์šฉ์ด ๋นˆ ํŒŒ์ผ __init__.py ํŒŒ์ผ์ด ์กด์žฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ฑ_ filters.py ํŒŒ์ผ ์•ˆ์— ์ปค์Šคํ…€ ํ•„ํ„ฐ๋ฅผ ์ •์˜ํ•˜๊ณ  ์•ฑ_ tags ํŒŒ์ผ ์•ˆ์— ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ์ •์˜ํ•œ๋‹ค. blog/templatetags/blog_filters.py ํŒŒ์ผ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. import bleach import markdown from django import template from django.template.defaultfilters import stringfilter from django.utils.safestring import mark_safe register = template.Library() @register.filter @stringfilter def markdownify(text): tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'ol', 'ul', 'li', 'div', 'p', 'code', 'blockquote', 'pre', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'a', 'em', 'strong', 'hr', 'img'] attrs = { '*': ['class', 'id'], 'a': ['href', 'rel'], 'img': ['alt', 'src'], } return mark_safe( bleach.clean( markdown.markdown(text, output_format='html', extensions=['markdown.extensions.tables', 'markdown.extensions.fenced_code', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ], ), tags=tags, attributes=attrs, strip=True)) ์ฃผ์–ด์ง„ ๋งˆํฌ๋‹ค์šด<NAME>์˜ ๋ฌธ์ž์—ด์„ HTML<NAME>์˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๋Š” ์ปค์Šคํ…€ ํ•„ํ„ฐ์ด๋‹ค. ์ด๋Š” ํ…œํ”Œ๋ฆฟ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. <div class="row"> <div class="col-md-12 post"> {% load blog_filters %} {{ post.content|markdownify }} </div> </div> ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋„์–ด์“ฐ๊ธฐ ์•ˆ ํ•˜๊ธฐ |(bar)๋กœ ์—ฐ๊ฒฐํ•  ๋•Œ๋Š” ๋„์–ด์“ฐ๊ธฐํ•˜์ง€ ์•Š๋Š”๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์žˆ์„ ๋•Œ url ํƒœ๊ทธ์˜ ์•„๊ทœ๋จผํŠธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž˜๋ชป ํŒŒ์•…ํ•˜๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. {% url 'blog:post_month_archive' date|date:'Y' date|date:'m' %} 03) ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ URL ์ƒ์„ฑ ๊ฐ„๋‹จํ•œ ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ํŒจํ‚ค์ง€ ๊ตฌ์„ฑ ์ปค์Šคํ…€ ํƒœ๊ทธ ๊ตฌํ˜„ ์ปค์Šคํ…€ ํƒœ๊ทธ ์‚ฌ์šฉํ•˜๊ธฐ ๋ณต์žกํ•œ ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ๊ตฌํ˜„ ํŒจํ‚ค์ง€ ๊ตฌ์„ฑ ์ปค์Šคํ…€ ํƒœ๊ทธ ๊ตฌํ˜„ ์ปค์Šคํ…€ ํƒœ๊ทธ ์‚ฌ์šฉํ•˜๊ธฐ ์žฌ๊ท€์  ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ๊ตฌํ˜„ ํŒจํ‚ค์ง€ ๊ตฌ์„ฑ ์ปค์Šคํ…€ ํƒœ๊ทธ ๊ตฌํ˜„ ์ปค์Šคํ…€ ํƒœ๊ทธ ์‚ฌ์šฉํ•˜๊ธฐ ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ URL ์ƒ์„ฑ ํ…œํ”Œ๋ฆฟ ์†Œ์Šค ์ฝ”๋“œ์—์„œ URL์„ ํ•˜๋“œ์ฝ”๋”ฉํ•˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค. {% url '๋„ค์ž„์ŠคํŽ˜์ด์Šค:๋ทฐ_์ด๋ฆ„' ์•„๊ทœ๋จผํŠธ1 ์•„๊ทœ๋จผํŠธ2 %} ๊ฐ„๋‹จํ•œ ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ํŒจํ‚ค์ง€ ๊ตฌ์„ฑ blog ์•ฑ์˜ ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ์ด๋ฆ„ ๊ทœ์น™์— ๋”ฐ๋ผ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ–๋Š”๋‹ค. blog/ templatetags/ __init__.py blog_tags.py templatetags ์ด๋ฆ„์˜ ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ๋นˆ ํŒŒ์ผ์˜ __init__.py ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ฑ_์ด๋ฆ„_ tags.py ๋ชจ๋“ˆ์„ ๋งŒ๋“ ๋‹ค. ์ปค์Šคํ…€ ํƒœ๊ทธ ๊ตฌํ˜„ ์ตœ๊ทผ ๊ฒŒ์‹œ๋ฌผ n ๊ฐœ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ์™€ ๋น„์Šทํ•˜๋‹ค. from django import template from .. models import Post register = template.Library() @register.simple_tag def recent_posts(count): posts = Post.objects.filter(status='published').order_by('-published')[:count] return posts ์ปค์Šคํ…€ ํƒœ๊ทธ ์‚ฌ์šฉํ•˜๊ธฐ ํ…œํ”Œ๋ฆฟ์—์„œ ์ตœ์‹  ๊ธ€ 5๊ฐœ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” recent_posts ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. {% load blog_tags %} <ul> {% recent_posts 5 as posts %} {% for post in posts %} <li><a href="{{ post.get_absolute_url }}">{{ post.title|truncatechars:25 }}</a></li> {% endfor %} </ul> ๋ณต์žกํ•œ ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ๊ตฌํ˜„ ์—ฌ๊ธฐ์„œ ๋ณต์žกํ•˜๋‹ค๋Š” ๊ฒƒ์˜ ๊ธฐ์ค€์€ ๋‹ค์Œ 2๊ฐ€์ง€์ด๋‹ค. ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ํ˜ธ์ถœํ•  ๋•Œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ์—์„œ ๋‹ค๋ฅธ html ํŽ˜์ด์ง€๋ฅผ ๋ Œ๋”๋ง ํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. ํŒจํ‚ค์ง€ ๊ตฌ์„ฑ mptt ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ navbar ๋ฉ”๋‰ด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ์ด๋ฆ„ ๊ทœ์น™์— ๋”ฐ๋ผ ๊ธฐ๋ณธ ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ์™€ ๋น„์Šทํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ–๋Š”๋‹ค. navbar/ templates/ menu.html templatetags/ __init__.py navbar_tags.py templatetags ์ด๋ฆ„์˜ ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ๋นˆ ํŒŒ์ผ์˜ __init__.py ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ฑ_์ด๋ฆ„_ tags.py ๋ชจ๋“ˆ์„ ๋งŒ๋“œ๋Š” ์ ์€ ๊ฐ™๋‹ค. ์ฐจ์ด์ ์ด ์žˆ๋‹ค๋ฉด ๋ Œ๋”๋ง ํ•  html ํŒŒ์ผ์„ templates ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ๋„ฃ์–ด๋‘”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ปค์Šคํ…€ ํƒœ๊ทธ ํ•จ์ˆ˜์˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ @register.simple_tag์ด ์•„๋‹ˆ๋ผ @register.tag๋กœ ์‚ฌ์šฉํ•œ๋‹ค. simple_tag๋กœ ์ง€์ •ํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ปค์Šคํ…€ ํƒœ๊ทธ ๊ตฌํ˜„ {% navbar_menu '๋ฉ”๋‰ด ์ด๋ฆ„' '๋ Œ๋”๋ง. html' %} ์œ„์™€ ๊ฐ™์€<NAME>์œผ๋กœ ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ '๋ฉ”๋‰ด ์ด๋ฆ„'๊ณผ '๋ Œ๋”๋ง. html' ์•„๊ทœ๋จผํŠธ๋ฅผ ์ปค์Šคํ…€ ํƒœ๊ทธ ๊ตฌํ˜„ํ•  ๋•Œ ์˜ฌ๋ฐ”๋กœ ์ „๋‹ฌ๋ฐ›์•„์•ผ ํ•œ๋‹ค. from django import template from .. models import MenuItem register = template.Library() @register.tag def navbar_menu(parser, token): template_name = 'menu.html' # ์ž…๋ ฅ๋ฐ›์€ ํ† ํฐ์„ ๋ถ„๋ฆฌํ•œ๋‹ค. tokens = token.split_contents() num = len(tokens) if num not in [2, 3]: raise template.TemplateSyntaxError('%r tag requires two arguments.' % tokens[0]) tree_query_set = MenuItem.objects.filter(menu__title=tokens[1][1:-1]) if num == 3: template_name = tokens[2][1:-1] return Menu(tree_query_set, template_name) class Menu(template.Node): def __init__(self, tree_query_set, template_name): self.tree_query_set = tree_query_set self.template_name = template_name def render(self, context): t = context.template.engine.get_template(self.template_name) return t.render( template.Context({ 'tree_menu': self.tree_query_set }, autoescape=context.autoescape)) ์ปค์Šคํ…€ ํƒœ๊ทธ ํ•จ์ˆ˜์˜ ์‹œ๊ทธ๋‹ˆ์ฒ˜๋ฅผ (parser, token)์œผ๋กœ ํ•  ๊ฒฝ์šฐ token ๋ณ€์ˆ˜๋Š” ํƒœ๊ทธ ์ด๋ฆ„์„ ํฌํ•จํ•˜์—ฌ ์ „๋‹ฌ๋ฐ›์€ ์ „์ฒด ๋ณ€์ˆ˜๊ฐ€ ๋œ๋‹ค. ์ด๋•Œ, ์ž…๋ ฅ๋ฐ›์€ ํ† ํฐ์„ split_contents ๋ฉ”์„œ๋“œ๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด 2๊ฐœ ๋˜๋Š” 3๊ฐœ์˜ ํ† ํฐ์ด ๋œ๋‹ค. ๋ Œ๋”๋ง ํ•  html ํŒŒ์ผ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋””ํดํŠธ ์ด๋ฆ„์œผ๋กœ menu.html์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ token์— {% navbar _menu 'top_menu' %} ์ด์™€ ๊ฐ™์ด ์ „๋‹ฌํ•  ๊ฒฝ์šฐ ๋‘ ๋ฒˆ์งธ ์•„๊ทœ๋จผํŠธ ํ† ํฐ์˜ ๋ฌธ์ž์—ด์€ top_menu๊ฐ€ ์•„๋‹ˆ๋ผ 'top_menu' ๋”ฐ์˜ดํ‘œ๊ฐ€ ๊ฐ์‹ธ์ง„ ํ˜•ํƒœ์ด๋‹ค. MenuItem.objects.filter(menu__title=tokens[1][1:-1]) ์ด์™€ ๊ฐ™์ด ํ•„ํ„ฐ๋กœ ๊ฒ€์ƒ‰ ์กฐ๊ฑด ๋ฌธ์ž์—ด์„ ์“ธ ๋•Œ๋Š” ์•ž๋’ค์˜ ๋”ฐ์˜ดํ‘œ๋ฅผ ๋ฒ—๊ฒจ๋‚ด๋Š” ์ฝ”๋“œ [1:-1] ์ฝ”๋“œ๋ฅผ ์จ์•ผ ํ•œ๋‹ค. ์ปค์Šคํ…€ ํƒœ๊ทธ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์€ ๋ฐ˜๋“œ์‹œ template.Node ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๊ณ  render() ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋ Œ๋”๋ง ํ•  html ํŒŒ์ผ์— ๋„˜๊ฒจ์ค„ ์ปจํ…์ŠคํŠธ ๋ณ€์ˆ˜๋Š” template.Context ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋„˜๊ฒจ์ค€๋‹ค. /templates/menu.html ํŒŒ์ผ {% load i18n %} {% load mptt_tags %} <ul class="nav navbar-nav"> {% recursetree tree_menu %} {% if not node.is_leaf_node %} <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> {{ node.title }} <span class="caret"></span> </a> <ul class="dropdown-menu"> {{ children }} </ul> </li> {% else %} <li> <a href="{{ node.path }}" target="{{ node.target }}">{% trans node.title %}</a> </li> {% endif %} {% endrecursetree %} </ul> ๋ Œ๋”๋ง ํ•˜๋Š” html ํŒŒ์ผ์˜ ๋‚ด์šฉ์€ ์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์•ž์„œ ํ˜ธ์ถœํ•˜๋Š” ์ชฝ์—์„œ tree_menu ์ปจํ…์ŠคํŠธ ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ฒจ์ค€ ๊ฒƒ์— ์œ ์˜ํ•œ๋‹ค. ๋˜ํ•œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์˜ ์œ„์น˜๋Š” ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‹œ์Šคํ…œ ์ „์—ญ templates ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋ผ ์•ฑ/templates ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์ฐพ๋Š”๋‹ค. ์ปค์Šคํ…€ ํƒœ๊ทธ ์‚ฌ์šฉํ•˜๊ธฐ ์•ž์„œ ๋งŒ๋“  ์ปค์Šคํ…€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ๋Š” ํ…œํ”Œ๋ฆฟ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. {% load navbar_tags %} {% navbar_menu 'top_menu' 'menu.html' %} ์žฌ๊ท€์  ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ๊ตฌํ˜„ ํŒจํ‚ค์ง€ ๊ตฌ์„ฑ ์ปค์Šคํ…€ ํƒœ๊ทธ ๊ตฌํ˜„ from django import template from django.utils.safestring import mark_safe from mptt.utils import get_cached_trees from .. models import MenuItem register = template.Library() class NavbarNode(template.Node): def __init__(self, nodes, tree_query_set): self.nodes = nodes self.tree_query_set = tree_query_set self.request = template.Variable('request') def _render_menu_item(self, context, menu_item): request = self.request.resolve(context) nodes = [] context.push() for child in menu_item.get_children(): nodes.append(self._render_menu_item(context, child)) menu_item.active = False if menu_item.match == 'equals' and request.path == menu_item.url \ or menu_item.match == 'startswith' and request.path.startswith(menu_item.url): menu_item.active = True context['menu_item'] = menu_item context['children'] = mark_safe(''.join(nodes)) rendered = self.nodes.render(context) context.pop() return rendered def render(self, context): roots = get_cached_trees(self.tree_query_set) nodes = [self._render_menu_item(context, menu_item) for menu_item in roots] return ''.join(nodes) @register.tag def navbar(parser, token): # separates the arguments on spaces while keeping quoted strings together tokens = token.split_contents() if len(tokens) != 2: raise template.TemplateSyntaxError('%r tag requires a tree queryset.' % tokens[0]) nodes = parser.parse(('endnavbar',)) parser.delete_first_token() tree_query_set = MenuItem.objects \ .get(title=tokens[1][1:-1]) \ .get_descendants(include_self=False) return NavbarNode(nodes, tree_query_set) ์ปค์Šคํ…€ ํƒœ๊ทธ ์‚ฌ์šฉํ•˜๊ธฐ {% load navbar_tags %} {% navbar 'ํ™ˆ' %} {% if not menu_item.is_leaf_node %} <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> {{ menu_item.title }} <span class="caret"></span> </a> <ul class="dropdown-menu"> {{ children }} </ul> </li> {% else %} <li{% if menu_item.active %} class="active"{% endif %}> <a href="{{ menu_item.url }}">{{ menu_item.title }}</a> </li> {% endif %} {% endnavbar %} 04) ํ…œํ”Œ๋ฆฟ์˜ ์ƒ์† ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ 05) ํ…œํ”Œ๋ฆฟ ์‹ค๋ฌด ํ…œํ”Œ๋ฆฟ ๊ตฌ์กฐ ์ „์—ญ์  templates ๋””๋ ‰ํ„ฐ๋ฆฌ vs. ์•ฑ๋ณ„ templates ๋””๋ ‰ํ„ฐ๋ฆฌ 2๋‹จ๊ณ„ ํ…œํ”Œ๋ฆฟ ๊ตฌ์กฐ vs. 3๋‹จ๊ณ„ ํ…œํ”Œ๋ฆฟ ๊ตฌ์กฐ Jinja2 DTL๊ณผ Jinja2 Jinja2 ์„ค์น˜ ๋ฐ ์„ค์ • ํ…œํ”Œ๋ฆฟ ๊ตฌ์กฐ ์ „์—ญ์  templates ๋””๋ ‰ํ„ฐ๋ฆฌ vs. ์•ฑ๋ณ„ templates ๋””๋ ‰ํ„ฐ๋ฆฌ Two Scoops of Django ์ฑ…์—์„œ๋Š” templates/ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ์•ฑ๋ณ„๋กœ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋‘๊ณ  ์ผ๊ด„์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. ๋ฌผ๋ก  ์ทจํ–ฅ์— ๋”ฐ๋ผ์„œ ์•ฑ๋งˆ๋‹ค templates/ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋‘˜ ์ˆ˜๋„ ์žˆ๋‹ค. 2๋‹จ๊ณ„ ํ…œํ”Œ๋ฆฟ ๊ตฌ์กฐ vs. 3๋‹จ๊ณ„ ํ…œํ”Œ๋ฆฟ ๊ตฌ์กฐ 2๋‹จ๊ณ„ ํ…œํ”Œ๋ฆฟ ๊ตฌ์กฐ๋Š” ๋ชจ๋“  ํ…œํ”Œ๋ฆฟ์€ ํ•˜๋‚˜์˜ base.html ํŒŒ์ผ์„ ์ƒ์†ํ•œ๋‹ค. templates/ base.html dashboard.html # base.html ์ƒ์† profiles/ profiles_detail.html # base.html ์ƒ์† profiles_form.html # base.html ์ƒ์† 3๋‹จ๊ณ„ ํ…œํ”Œ๋ฆฟ ๊ตฌ์กฐ๋Š” ์•ฑ๋ณ„๋กœ base_<์•ฑ_์ด๋ฆ„>.html ํŒŒ์ผ์ด ์žˆ๊ณ  ์ด๋Š” base.html ํŒŒ์ผ์„ ์ƒ์†ํ•œ๋‹ค. ๊ฐ ์•ฑ ์•ˆ์˜ ํ…œํ”Œ๋ฆฟ์€ ๋ชจ๋‘ base_<์•ฑ_์ด๋ฆ„>.html ํŒŒ์ผ์„ ์ƒ์†ํ•œ๋‹ค. base.html๊ณผ ๊ฐ™์€ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  ํ…œํ”Œ๋ฆฟ์€ base.html ํŒŒ์ผ์„ ์ƒ์†ํ•œ๋‹ค. templates/ base.html dashboard.html # base.html ์ƒ์† profiles/ base_profiles.html # base.html ์ƒ์† profiles_detail.html # base_profiles.html ์ƒ์† profiles_form.html # base_profiles.html ์ƒ์† 3๋‹จ๊ณ„ ํ…œํ”Œ๋ฆฟ ๊ตฌ์กฐ๋Š” ์•ฑ๋งˆ๋‹ค ๋ ˆ์ด์•„์›ƒ์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ์— ์ ํ•ฉํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ง€๋ฐฉ ๋‰ด์Šค ์„น์…˜, ๋‹จ๋ฌธ ๊ด‘๊ณ  ์„น์…˜, ํ–‰์‚ฌ ์„น์…˜๋งˆ๋‹ค ๋‹ค๋ฅธ ๋ ˆ์ด์•„์›ƒ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค. Jinja2 Django 1.8 ๋ฒ„์ „๋ถ€ํ„ฐ ๋‹ค์ค‘ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์ง€์›ํ•œ๋‹ค. DTL๊ณผ Jinja2๋ฅผ ๋ชจ๋‘ ๊ธฐ๋ณธ ๋‚ด์žฅํ•˜๊ณ  ์žˆ๋‹ค. DTL๊ณผ Jinja2 DTL์˜ ์žฅ์  Django์˜ ๊ณต์‹ ํ…œํ”Œ๋ฆฟ ์—”์ง„์œผ๋กœ ๋ณ„๋„์˜ ์„ค์น˜์™€ ์„ค์ •์ด ํ•„์š” ์—†๋‹ค. DTL+Django ์กฐํ•ฉ์ด DTL+Jinja2 ์กฐํ•ฉ๋ณด๋‹ค ๋” ๋งŽ์ด ์‚ฌ์šฉ๋˜์–ด ๊ฒ€์ฆ๋˜์—ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์„œ๋“œํŒŒํ‹ฐ Django ํŒจํ‚ค์ง€์—์„œ DTL์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋งŽ์€ ์–‘์˜ DTL ๋ ˆ๊ฑฐ์‹œ ์ฝ”๋“œ๋ฅผ Jinja2๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ ์ƒ๋‹นํžˆ ํฐ ์ž‘์—…์ด๋‹ค. Jinja2์˜ ์žฅ์  Django์™€ ๋…๋ฆฝ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. DTL ๋ฌธ๋ฒ•๋ณด๋‹ค Jinja2 ๋ฌธ๋ฒ•์ด ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•์— ๋” ๊ฐ€๊นŒ์›Œ ์ง๊ด€์ ์ด๋‹ค. Jinja2๊ฐ€ ์ข€ ๋” ๋ช…์‹œ์ ์ด๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. Jinja2๊ฐ€ ์ข€ ๋” ๋น ๋ฅด๋‹ค. ์–ด๋Š ๊ฒƒ์„ ์„ ํƒํ•  ๊ฒƒ์ธ๊ฐ€. Django๋ฅผ ์ฒ˜์Œ ๋ฐฐ์šฐ๋Š” ๊ฐœ๋ฐœ์ž๋Š” DTL์„ ์ด์šฉํ•œ๋‹ค. DTL๋กœ ์ž‘์„ฑ๋œ ๋ ˆ๊ฑฐ์‹œ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ ํŠน๋ณ„ํžˆ ์„ฑ๋Šฅ ํ–ฅ์ƒ์ด ํ•„์š”ํ•œ ๋ช‡ ํŽ˜์ด์ง€๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” DTL์„ ์ด์šฉํ•œ๋‹ค. Django๋ฅผ ๊ณต๋ถ€ํ•˜๊ณ  ๋‘˜์„ ๋น„๊ตํ•ด์„œ ์„ ํƒํ•œ๋‹ค. Jinja2 ์„ค์น˜ ๋ฐ ์„ค์ • Django๋Š” ๋‹ค์ค‘ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์ง€์›ํ•˜์ง€๋งŒ Jinja2 ํŒจํ‚ค์ง€๊ฐ€ ํ•จ๊ป˜ ๋ฐฐํฌ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ๋จผ์ € Jinja2 ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ๋‹ค. pip install Jinja2 settings.py ํŒŒ์ผ์„ ์ˆ˜์ •ํ•œ๋‹ค. TEMPLATES = [ { 'BACKEND': 'django.template.backends.jinja2.Jinja2', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { # ... some options here ... }, }, ] ํ…œํ”Œ๋ฆฟ ๋ฐฑ์—”๋“œ API๋ฅผ django.template.backends.django.DjangoTemplates์—์„œ django.template.backends.jinja2.Jinja2๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. templates ๋””๋ ‰ํ„ฐ๋ฆฌ์— Jinja2 ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์ด ์œ„์น˜ํ•œ๋‹ค. 05) ํŽ˜์ด์ง€ ๋„ค์ด์…˜ ํŽ˜์ด์ง€ ๋„ค์ด์…˜ Paginator ๊ฐ์ฒด Page ๊ฐ์ฒด ์˜ˆ์ œ ์ฝ”๋“œ ํด๋ž˜์Šค ํ˜• ๋ทฐ ํ…œํ”Œ๋ฆฟ ์ฝ”๋“œ ๋ฏน์Šค ์ธ๊ณผ ํ…œํ”Œ๋ฆฟ ์ธํด๋ฃจ๋“œ ํŽ˜์ด์ง€ ๋„ค์ด์…˜ Paginator ๊ฐ์ฒด ์ฃผ์š” ์†์„ฑ paginator.page_range: ์ „์ฒด ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ xrange Page ๊ฐ์ฒด ์ฃผ์š” ์†์„ฑ page_obj.number: ํ˜„์žฌ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ page.previous_page_number: ์ด์ „ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ page.next_page_number: ๋‹ค์Œ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ ์˜ˆ์ œ ์ฝ”๋“œ ํŽ˜์ด์ง€๊ฐ€ 20๊ฐœ, 30๊ฐœ๊ฐ€ ๋„˜์–ด๊ฐ€๋„ ์ „์ฒด๋ฅผ ๋ณด์—ฌ์ฃผ๋ฏ€๋กœ ๋ช‡ ๊ฐœ์”ฉ ๋Š์–ด์„œ ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์„ ๋ณ„๋„๋กœ ์ฒ˜๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ํด๋ž˜์Šค ํ˜• ๋ทฐ ํด๋ž˜์Šค ํ˜• ๋ทฐ์— ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. paginated_by = 10 # ํ•œ ํŽ˜์ด์ง€ ๋ชฉ๋ก์— ํ‘œ์‹œ ๊ฒŒ์‹œ๋ฌผ ์ˆ˜ (๋‹จ ํ•œ ์ค„๋กœ ํŽ˜์ด์ง€ ๋„ค์ด์…˜์ด ๊ตฌํ˜„๋จ) def get_context_data(self, **kwargs): context = super(PostListView, self).get_context_data(**kwargs) block_size = 5 # ํ•˜๋‹จ์˜ ํŽ˜์ด์ง€ ๋ชฉ๋ก ์ˆ˜ start_index = int((context['page_obj'].number - 1) / self.block_size) * self.block_size end_index = min(start_index + self.block_size, len(context['paginator'].page_range)) context['page_range'] = context['paginator'].page_range[start_index:end_index] return context ํ…œํ”Œ๋ฆฟ ์ฝ”๋“œ ํ…œํ”Œ๋ฆฟ ์ฝ”๋“œ๋Š” ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์˜ ํŽ˜์ด์ง€ ๋„ค์ด์…˜ ๋””์ž์ธ์„ ํ™œ์šฉํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. <!-- Pagination --> <nav aria-label="Page navigation"> <ul class="pagination"> {% if page_obj.has_previous %} <li> <a href="?page={{ page_obj.previous_page_number }}" aria-label="Previous"> <span aria-hidden="true">ยซ</span> </a> </li> {% else %} <li class="disabled"><span>ยซ</span></li> {% endif %} {% for i in page_range %} {% if page_obj.number == i %} <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li> {% else %} <li><a href="?page={{ i }}">{{ i }}</a></li> {% endif %} {% endfor %} {% if page_obj.has_next %} <li> <a href="?page={{ page_obj.next_page_number }}" aria-label="Next"> <span aria-hidden="true">ยป</span> </a> </li> {% else %} <li class="disabled"><span>ยป</span></li> {% endif %} </ul> </nav> ๋ฏน์Šค ์ธ๊ณผ ํ…œํ”Œ๋ฆฟ ์ธํด๋ฃจ๋“œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํด๋ž˜์Šค ๋ทฐ๋งˆ๋‹ค ์œ„์™€ ๊ฐ™์ด ๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๊ท€์ฐฎ์€ ์ผ์ด๋‹ค. ๋ฏน์Šค์ธ์„ ์ž‘์„ฑํ•ด๋†“๊ณ  ํ•ด๋‹น ๋ฏน์Šค์ธ์„ ํด๋ž˜์Šค ํ˜• ๋ทฐ ์„ ์–ธ์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. PageableMixin ๋ฏน์Šค์ธ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค. class PageableMixin(object): logger = logging.getLogger(__name__) paginate_by = 10 block_size = 10 def get_context_data(self, **kwargs): self.logger.debug('PageableMixin.get_context_data()') context = super(PageableMixin, self).get_context_data(**kwargs) start_index = int((context['page_obj'].number - 1) / self.block_size) * self.block_size end_index = min(start_index + self.block_size, len(context['paginator'].page_range)) context['page_range'] = context['paginator'].page_range[start_index:end_index] return context PageableMixin ๋ฏน์Šค์ธ์„ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค ํ˜• ๋ทฐ MessageListView๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์˜ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. class MessageListView(BoardContextMixin, PageableMixin, ListView): logger = logging.getLogger(__name__) context_object_name = 'messages' template_name = 'board/message_list.html' def __init__(self): self.logger.debug('MessageListView.__init__()') super(MessageListView, self).__init__() self.block_size = 10 # default value def get_queryset(self): self.logger.debug('MessageListView.get_queryset()') return Message.objects.published() \ .select_related('author') \ .select_related('board') \ .filter(board__slug=self.kwargs['slug']) \ .order_by('-date_created') def get_context_data(self, **kwargs): self.logger.debug('MessageListView.get_context_data()') context = super(MessageListView, self).get_context_data(**kwargs) context['board'] = self.board # already fetched by BoardContextMixin.dispatch() return context def get_paginate_by(self, queryset): self.logger.debug('MessageListView.get_paginate_by()') self.block_size = self.board.block_size return self.board.chunk_size PageableMixin์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ paginate_by์™€ block_size๋Š” ๊ฒฐ๊ตญ MessageListView ํด๋ž˜์Šค์˜ get_paginate_by() ๋ฉ”์„œ๋“œ์— ์˜ํ•ด ์„€๋„ ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„์˜ ํ…œํ”Œ๋ฆฟ ์ฝ”๋“œ๋ฅผ ํŽ˜์ด์ง€๋งˆ๋‹ค ์“ฐ์ง€ ์•Š๊ณ  ๋ณ„๋„์˜ ํŒŒ์ผ๋กœ _pagination.html ๊ฐ™์ด ๋ถ„๋ฆฌํ•˜๊ณ  ์ด๋ฅผ ์ธํด๋ฃจ๋“œ ํ•˜๋Š” ์ฝ”๋“œ๋กœ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. {% include '_pagination.html' with page_obj=page_obj page_range=page_range %} ์œ„์™€ ๊ฐ™์ด _pagination.html ํŒŒ์ผ์„ ์ธํด๋ฃจ๋“œํ•˜๊ธฐ ์œ„ํ•ด page_obj, page_range ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ๋‹ค. 05. ํผ 01) ํผ ํผ ์ฒ˜๋ฆฌ ๊ณผ์ • 3๋‹จ๊ณ„ ์ผ๋ฐ˜ ํผ ๋ชจ๋ธ ํผ ๋ชจ๋ธ ํผ์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ˆœ์„œ ํผ ์ฒ˜๋ฆฌ ๊ณผ์ • 3๋‹จ๊ณ„ ํผ ์ฒ˜๋ฆฌ ๊ณผ์ •์˜ 3๋‹จ๊ณ„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. GET ๋ฉ”์„œ๋“œ๋กœ ์ฒ˜์Œ์œผ๋กœ ํผ์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋นˆ ํผ ๋˜๋Š” ๊ฐ’์„ ์ฑ„์šด(prepopulated) ํผ์ด๋‹ค. ์œ ํšจํ•˜์ง€ ์•Š์€ ๊ฐ’์„ ๋ฐ›์€ ๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ํผ์„ ๋‹ค์‹œ ์ถœ๋ ฅํ•œ๋‹ค. ์œ ํšจํ•œ ๊ฐ’์„ ๋ฐ›์€ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•œ๋‹ค. Django์˜ ํผ์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๋กœ ์กด์žฌํ•œ๋‹ค. ์ผ๋ฐ˜ ํผ ๋ชจ๋ธ ํผ ์ผ๋ฐ˜ ํผ ๋ชจ๋ธ ํผ CreateView๋ฅผ ์ƒ์†ํ•œ ํด๋ž˜์Šค์—์„œ model ์†์„ฑ์„ ์ง€์ •ํ•˜๋ฉด ์ž๋™์œผ๋กœ ํ•ด๋‹น ModelForm์ด ์ƒ์„ฑ๋œ๋‹ค. class MessageCreateView(CreateView): model = Message fields = ['title', 'content', ] template_name = 'board/message_form.html' ์œ ํšจ์„ฑ ๊ฒ€์ฆ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์ปค์Šคํ…€ ModelForm ์„ ์ง์ ‘ ์ง€์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด model ์†์„ฑ ๋Œ€์‹ ์— form_class ์†์„ฑ์„ ์ง€์ •ํ•œ๋‹ค. class MessageCreateView(CreateView): form_class = MessageForm template_name = 'board/message_form.html' CreateView์—์„œ๋Š” model ์†์„ฑ์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์ง€๋งŒ UpdateView์—์„œ๋Š” model ์†์„ฑ๊นŒ์ง€ ๊ผญ ์ง€์ •ํ•ด ์ค˜์•ผ ํ•œ๋‹ค. class MessageUpdateView(UpdateView): model = Message form_class = MessageForm template_name = 'board/message_form.html' ๋งŒ์•ฝ model ์†์„ฑ์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. django.core.exceptions.ImproperlyConfigured: MessageUpdateView is missing a QuerySet. Define MessageUpdateView.model, MessageUpdateView.queryset, or override MessageUpdateView.get_queryset(). ๋ชจ๋ธ ํผ์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ˆœ์„œ GET ๋ฉ”์„œ๋“œ ์š”์ฒญ ์‹œ ์ฃผ์š” ๋ฉ”์„œ๋“œ์˜ ํ˜ธ์ถœ ์ˆœ์„œ์ด๋‹ค. dispatch() get_context_data() get_initial() POST ๋ฉ”์„œ๋“œ ์š”์ฒญ ์‹œ ์ฃผ์š” ๋ฉ”์„œ๋“œ์˜ ํ˜ธ์ถœ ์ˆœ์„œ์ด๋‹ค. dispatch() get_initial() form_valid() get_success_url() ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ get_context_data() ๋ฉ”์„œ๋“œ๊ฐ€ POST ์š”์ฒญ ์‹œ์—๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋ฐ˜๋ฉด์— form_valid() ๋ฉ”์„œ๋“œ๋Š” ํผ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก๋ฐ›์ง€ ๋ชปํ–ˆ์œผ๋ฏ€๋กœ ์—ญ์‹œ GET ์š”์ฒญ ์‹œ์—๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค. GET, POST ์š”์ฒญ์—์„œ ๊ณตํ†ต๋œ ์ž‘์—…์„ ํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ get_initial() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ์ด ๋ฉ”์„œ๋“œ๋Š” FormMixin์˜ ๋ฉ”์„œ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— FormMixin์„ ์ƒ์†๋ฐ›์ง€ ์•Š๋Š” ํด๋ž˜์Šค ํ˜• ๋ทฐ์—์„œ๋Š” ์ž‘์—…์ด ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š๋Š”๋‹ค. ์•ฑ ์ „์ฒด ๋ทฐ์—์„œ ๊ณตํ†ต์œผ๋กœ ํ˜ธ์ถœ๋˜๊ธธ ์›ํ•œ๋‹ค๋ฉด dispatch() ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์•ผ ํ•œ๋‹ค. 02) ํผ ๊ฒ€์ฆ 03) ํŒŒ์ผ ์—…๋กœ๋“œ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ์™€ ์ถœ๋ ฅ ํŒŒ์ผ ์—…๋กœ๋“œ์™€ ๋‹ค์šด๋กœ๋“œ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ์™€ ์ถœ๋ ฅ ํŒŒ์ผ ์—…๋กœ๋“œ์™€ ๋‹ค์šด๋กœ๋“œ 04) ์ปค์Šคํ…€ ์œ„์ ฏ ์ปค์Šคํ…€ ์œ„์ ฏ widgets.py ํŒŒ์ผ templates/simplemde/simplemde.html ํŒŒ์ผ ์ปค์Šคํ…€ ์œ„์ ฏ ๋””ํดํŠธ TextArea ๋Œ€์‹ ์— ์œ„์ง€์œ„๊ทธ ์—๋””ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ ์ปค์Šคํ…€ ์œ„์ ฏ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ€์ง€ WYSYWIG ์—๋””ํ„ฐ django ๋ชจ๋“ˆ์€ ํŽธ๋ฆฌํ•˜๊ฒŒ ์„ค์น˜ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋งŒ์•ฝ ์—๋””ํ„ฐ ๋ณ€๊ฒฝ์„ ์œ„ํ•ด ์ œ๊ฑฐํ•  ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์—์„œ ๋ชจ๋ธ์˜ ํ•„๋“œ ํƒ€์ž…์„ ๋ณ€๊ฒฝํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์— ์•ฑ์˜ ์˜์กด์„ฑ์ด ๋‚จ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋‚ด์šฉ์€ ๊ทธ๋Œ€๋กœ ๋‚จ๊ฒจ๋‘๊ณ  ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ์„<NAME>๋Š” ๊ฒƒ์ด๋‹ค. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํŒŒ์ผ ์‚ญ์ œํ•˜๊ธฐ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋Š” SimpleMDE ์—๋””ํ„ฐ๋ฅผ ์œ„ํ•œ ์ปค์Šคํ…€ ์œ„์ ฏ์„ ๋งŒ๋“œ๋Š” ์˜ˆ์‹œ์ด๋‹ค. widgets.py ํŒŒ์ผ from django.forms import widgets from django.template import loader from django.utils.safestring import mark_safe class SimpleMDEWidget(widgets.Textarea): # ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์ด ์•ฑ ์•ˆ์— ์กด์žฌํ•˜๋„๋ก ํ–ˆ๋‹ค. ์ด๋Š” ๋ณ„๋„์˜ ์•ฑ์œผ๋กœ ์ œ์ž‘ํ•  ๋•Œ ๋™์ž‘ํ•œ๋‹ค. template_name = 'simplemde/simplemde.html' # static/simplemde ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ์žˆ๋Š” CSS์™€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. # simplemde.min.css์™€ simplemde.min.js ํŒŒ์ผ์€ SimpleMDE์—์„œ ๋ฐฐํฌํ•˜๋Š” ํŒŒ์ผ์ด๋‹ค. class Media: css = { 'all': ( 'simplemde/simplemde.min.css', 'simplemde/custom.css', ) } js = ( 'simplemde/simplemde.min.js', ) def __init__(self, attrs=None, wrapper_class='simplemde-box', options=''): # ํ…œํ”Œ๋ฆฟ์„ ์ถœ๋ ฅํ•  ๋•Œ wrapper_class์™€ options ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ฒจ์ฃผ๊ธฐ ์œ„ํ•ด ๊ฐ€์ ธ์˜จ๋‹ค. self.wrapper_class = wrapper_class self.options = options super(SimpleMDEWidget, self).__init__(attrs=attrs) def render(self, name, value, attrs=None, renderer=None): context = { 'widget': { 'name': name, 'value': value, 'wrapper_class': self.wrapper_class, 'options': self.options, } } # ํ…œํ”Œ๋ฆฟ์„ ๋ Œ๋”๋ง ํ•œ๋‹ค. template = loader.get_template(self.template_name).render(context) # ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์˜ ๋ฌธ์ž์—ด์„ ์ด์Šค์ผ€์ดํ”„ํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•œ๋‹ค. return mark_safe(template) templates/simplemde/simplemde.html ํŒŒ์ผ <div class="{{ widget.wrapper_class }}"> <textarea id="id-{{ widget.name }}" name="{{ widget.name }}">{% if widget.value %}{{ widget.value }}{% endif %}</textarea> <script> simplemde = new SimpleMDE({ element: document.getElementById("id-{{ widget.name }}"), {% autoescape off %}{{ widget.options }}{% endautoescape %} }); </script> </div> ์œ„์ ฏ์œผ๋กœ ๋ Œ๋”๋ง ํ•˜๋Š” ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์ด๋‹ค. {% autoescape off %}{{ widget.options }}{% endautoescape %} ๊ตฌ๋ฌธ์ด ํ•„์š”ํ•œ ์ด์œ ๋Š” ์œ„์ ฏ์„ ๋ Œ๋”๋ง ํ•  ๋•Œ mark_safe() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ˜ธ์ถœ๋  ๋•Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์˜ต์…˜์ด ์ด์Šค์ผ€์ดํ”„ ๋˜๋Š” ๊ฑธ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. 06. ์ธ์ฆ๊ณผ ๊ถŒํ•œ 01) Django ๊ธฐ๋ณธ ์ธ์ฆ Django ์ œ๊ณต ์ธ์ฆ ๊ธฐ๋Šฅ ์ด๋ฆ„ ๊ทœ์น™ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ” ์„ค๊ณ„ ๊ตฌํ˜„ settings.py ํŒŒ์ผ ์„ค์ • URL ํŒจํ„ด ๋ฐ ๋ทฐ ํšŒ์› ๊ฐ€์ž… ํผ ๋ทฐ์™€ ํšŒ์› ๊ฐ€์ž… ์™„๋ฃŒ ์ถœ๋ ฅ ์ •์˜ ํ…œํ”Œ๋ฆฟ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋กœ๊ทธ์ธ ๋กœ๊ทธ์•„์›ƒ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ดˆ๊ธฐํ™” Django ์ œ๊ณต ์ธ์ฆ ๊ธฐ๋Šฅ ์ด๋ฆ„ ๊ทœ์น™ django.contrib.auth.urls ๋ชจ๋“ˆ์—๋Š” ์ธ์ฆ ๊ด€๋ จ URL ํŒจํ„ด๊ณผ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ทฐ๊ฐ€ ๋ฏธ๋ฆฌ ์ •์˜๋˜์–ด ์žˆ๋‹ค. urlpatterns = [ url(r'^login/$', views.LoginView.as_view(), name='login'), url(r'^logout/$', views.LogoutView.as_view(), name='logout'), url(r'^password_change/$', views.PasswordChangeView.as_view(), name='password_change'), url(r'^password_change/done/$', views.PasswordChangeDoneView.as_view(), name='password_change_done'), url(r'^password_reset/$', views.PasswordResetView.as_view(), name='password_reset'), url(r'^password_reset/done/$', views.PasswordResetDoneView.as_view(), name='password_reset_done'), url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'), url(r'^reset/done/$', views.PasswordResetCompleteView.as_view(), name='password_reset_complete'), ] ์œ„ ์ •์˜๋ฅผ ์ด์šฉํ•˜๋˜ ๊ฐœ๋ฐœ์ž๋Š” ์‹ค์ œ๋กœ ํ…œํ”Œ๋ฆฟ์„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. URL ํŒจํ„ด ๋ทฐ ์ด๋ฆ„ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ๋ช… /accounts/login/ login() registration/login.html /accounts/logout/ logout() registration/logged_out.html (๊ฐœ๋ฐœ์ž ์ง€์ •) logout_then_login() (๊ฐœ๋ฐœ์ž๊ฐ€ ์ง€์ •) /accounts/password_change/ password_change() registration/password_change_form.html /accounts/password_change/done/ password_change_done() registration/password_change_done.html /accounts/password_reset/ password_reset() registration/password_reset_form.html registration/password_reset_email.html registration/password_reset_subject.txt /accounts/password_reset/done/ password_reset_done() registration/password_reset_done.html /accounts/reset/ password_reset_confirm() registration/password_reset_confirm.html /accounts/reset/done/ password_reset_complete() registration/password_reset_complete.html ๋‹ค์Œ ํ•ญ๋ชฉ์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๊ฐœ๋ฐœํ•ด์•ผ ํ•œ๋‹ค. URL ํŒจํ„ด ๋ทฐ ์ด๋ฆ„ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ๋ช… /accounts/register/ UserCreasteView(CreateView) registration/register.html /accounts/register/done UserCreateDoneTemplateView(TemplateView) registration/register_done.html ํผ ํด๋ž˜์Šค ๋กœ๊ทธ์ธ ํ™”๋ฉด ํผ: AuthenticationForm ํšŒ์› ๊ฐ€์ž… ํผ: UserCreationForm ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ํผ: PasswordChangeForm ๊ธฐ๋ณธ ํ…Œ์ด๋ธ” ์„ค๊ณ„ ๊ตฌํ˜„ settings.py ํŒŒ์ผ ์„ค์ • settings.py์—์„œ django.contrib.auth ์•ฑ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์น˜๋˜์–ด ์žˆ๊ณ  ์ธ์ฆ ๊ด€๋ จ ๊ธฐ๋ณธ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', # ๊ธฐ๋ณธ ์„ค์น˜๋จ 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] LOGIN_URL = '/accounts/login/' # ๊ธฐ๋ณธ๊ฐ’ LOGOUT_URL = '/accounts/logout/' # ๊ธฐ๋ณธ๊ฐ’ LOGIN_REDIRECT_URL = '/' # ๋ฐ˜๋“œ์‹œ ์ •์˜ํ•  ๊ฒƒ! URL ํŒจํ„ด ๋ฐ ๋ทฐ urlpatterns = [ url(r'^accounts/', include('django.contrib.auth.urls')), url(r'^accounts/register/$', UserCreateView.as_view(), name='register'), url(r'^accounts/register/done/$', UserCreateDoneTemplateView.as_view(), name='register_done'), ] django.contrib.auth.urls ๋ชจ๋“ˆ์„ ์ธํด๋ฃจ๋“œํ•˜์—ฌ Django๊ฐ€ ๊ธฐ๋ณธ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ์ตœ๋Œ€ํ•œ ์žฌ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  UserCreateView ๋ทฐ์™€ UserCreateDoneTemplateView ๋ทฐ๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๋งŒ๋“ ๋‹ค. urls.py ๋ชจ๋“ˆ์„ ๋ถ„๋ฆฌํ•˜์—ฌ namespace๋ฅผ ๋ถ€์—ฌํ•˜๊ณ  ๋ณ„๋„์˜ ์•ฑ ์•ˆ์—์„œ URL ํŒจํ„ด์„ ์ •์˜ํ•  ๊ฒฝ์šฐ ์˜ฌ๋ฐ”๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ํšŒ์› ๊ฐ€์ž… ํผ ๋ทฐ์™€ ํšŒ์› ๊ฐ€์ž… ์™„๋ฃŒ ์ถœ๋ ฅ ์ •์˜ ํšŒ์› ๊ฐ€์ž… ๋ทฐ from django.contrib.auth.forms import UserCreationForm class UserCreateView(CreateView): template_name = 'registration/register.html' form_class = UserCreationForm success_url = reverse_lazy('common:register_done') # ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์ •ํ™•ํ•˜๊ฒŒ ํšŒ์› ๊ฐ€์ž… ์™„๋ฃŒ ํ›„ ์•ˆ๋‚ด ํŽ˜์ด์ง€ ์ถœ๋ ฅ ๋ทฐ class UserCreateDoneTemplateView(TemplateView): template_name = 'registration/register_done.html' ํ…œํ”Œ๋ฆฟ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋กœ๊ทธ์ธ ๋กœ๊ทธ์•„์›ƒ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ดˆ๊ธฐํ™” 02) Django ๊ธฐ๋ณธ ๊ถŒํ•œ ๊ด€๋ฆฌ Django ๊ธฐ๋ณธ ๊ถŒํ•œ ๊ด€๋ฆฌ Django ๊ธฐ๋ณธ ๊ถŒํ•œ ๊ด€๋ฆฌ 03) User ๋ชจ๋ธ์˜ ํ™•์žฅ ๊ธฐ๋ฒ• ๋น„๊ต ๋ชฉ์  User ๋ชจ๋ธ์˜ ํ™•์žฅ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ๋ฒ• ํ”„๋ฝ์‹œ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ๊ฐœ์š” ๊ตฌํ˜„ User ๋ชจ๋ธ๊ณผ ์ผ๋Œ€์ผ ๊ด€๊ณ„์˜ ํ”„๋กœํ•„ ํ…Œ์ด๋ธ” ์ถ”๊ฐ€ํ•˜๊ธฐ ๊ฐœ์š” ๊ตฌํ˜„ AbstractUser ๋ชจ๋ธ ์ƒ์†ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ User ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ๊ฐœ์š” ๊ตฌํ˜„ AbstractBaseUser ๋ชจ๋ธ ์ƒ์†ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ User ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ๊ฐœ์š” ๊ตฌํ˜„ ๊ฒฐ๋ก  ๋˜๋Š” ์˜๊ฒฌ ์ฐธ๊ณ ๋ฌธํ—Œ ๋ชฉ์  Django์˜ ๊ธฐ๋ณธ User ๋ชจ๋ธ์ด ์ œ๊ณตํ•˜๋Š” ํ•„๋“œ ์™ธ์— ์ถ”๊ฐ€์ ์ธ ์‚ฌ์šฉ์ž ์ •๋ณด, ํ”„๋กœํ•„ ๋“ฑ์„ ์ €์žฅํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ๊ธฐ๋ณธ User ๋ชจ๋ธ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ User ๋ชจ๋ธ์˜ ํ™•์žฅ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ๋ฒ• ํ”„๋ฝ์‹œ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ User ๋ชจ๋ธ๊ณผ ์ผ๋Œ€์ผ ๊ด€๊ณ„์˜ ํ”„๋กœํ•„ ํ…Œ์ด๋ธ” ์ถ”๊ฐ€ํ•˜๊ธฐ AbstractUser ๋ชจ๋ธ ์ƒ์†ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ User ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ AbstractBaseUser ๋ชจ๋ธ ์ƒ์†ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ User ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ํ”„๋ฝ์‹œ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ๊ฐœ์š” ํ”„๋ฝ์‹œ ๋ชจ๋ธ์ด๋ž€ ์ƒˆ ํ…Œ์ด๋ธ”์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋“ฑ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ ๋ณ€๊ฒฝ ์—†์ด ๋‹จ์ˆœํžˆ ์ƒ์†ํ•œ ํด๋ž˜์Šค์ด๋‹ค. ์ •๋ ฌ ์ˆœ์„œ ๊ฐ™์€ ๊ธฐ์กด ๋ชจ๋ธ์˜ ๋™์ž‘์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ถ€๊ฐ€์ ์ธ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์—†์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ตฌํ˜„ ํ”„๋ฝ์‹œ ๋ชจ๋ธ ๊ธฐ๋ฒ•์€ User ๋ชจ๋ธ์„ ์ƒ์†ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ํ…Œ์ด๋ธ”์—” ์–ด๋– ํ•œ ๋ณ€๊ฒฝ๋„ ์—†๋‹ค. from django.contrib.auth.models import User from .managers import PersonManager class Person(User): objects = PersonManager() class Meta: proxy = True ordering = ('first_name', ) def do_something(self): ... ์œ„ ์ฝ”๋“œ์˜ ๋‚ด์šฉ์„ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. User ๋ชจ๋ธ์„ ์ƒ์†ํ•œ Person ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค. Meta ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋ฉด์„œ ํ”„๋ฝ์‹œ ๋ชจ๋ธ ํด๋ž˜์Šค์ž„์„ ์„ ์–ธํ•˜๊ณ  ์ •๋ ฌ ์ˆœ์„œ๋ฅผ first_name ๊ธฐ์ค€์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. do_something ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. User.objects.all()๊ณผ Person.objects.all() ์ฝ”๋“œ๋Š” ์Šคํ‚ค๋งˆ์˜ ๋ณ€๊ฒฝ์ด ์—†์œผ๋ฏ€๋กœ ๊ฐ™์€ ์ฟผ๋ฆฌ๋กœ ๋™์ž‘ํ•œ๋‹ค. User ๋ชจ๋ธ๊ณผ ์ผ๋Œ€์ผ ๊ด€๊ณ„์˜ ํ”„๋กœํ•„ ํ…Œ์ด๋ธ” ์ถ”๊ฐ€ํ•˜๊ธฐ ๊ฐœ์š” ๊ธฐ์กด User ๋ชจ๋ธ๊ณผ OneToOneField๋กœ ์ผ๋Œ€์ผ ๊ด€๊ณ„๋ฅผ ๋งบ๋Š” Django ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉ์ž์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Django์˜ ์ธ์ฆ ์‹œ์Šคํ…œ์„ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•˜๊ณ  ๋กœ๊ทธ์ธ, ๊ถŒํ•œ ๋ถ€์—ฌ ๋“ฑ๊ณผ ์ƒ๊ด€์ด ์—†๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด ํ•„๋“œ๋ฅผ ์ €์žฅํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค. ๊ตฌํ˜„ Profile ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•œ๋‹ค. from django.db import models from django.contrib.auth.models import User class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(max_length=500, blank=True) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(null=True, blank=True) AbstractUser ๋ชจ๋ธ ์ƒ์†ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ User ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ๊ฐœ์š” AbstractUser ๋ชจ๋ธ์„ ์ƒ์†ํ•œ User ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด settings.py์— ์ฐธ์กฐ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ด ๊ธฐ๋ฒ•์˜ ์‚ฌ์šฉ ์—ฌ๋ถ€๋Š” ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ ์ „์— ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ถ”ํ›„์— settings.AUTH_USER_MODEL ๋ณ€๊ฒฝ ์‹œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ๋ฅผ ์•Œ๋งž๊ฒŒ ์žฌ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ์ž ๋ชจ๋ธ ํ•„๋“œ์— ์ถ”๊ฐ€๋‚˜ ์ˆ˜์ •์œผ๋กœ ๋๋‚˜์ง€ ์•Š๊ณ  ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ผ์ด ๋œ๋‹ค. ์ด ๊ธฐ๋ฒ•์€ ๊ธฐ์กด Django์˜ User ๋ชจ๋ธ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ ์ธ์ฆ ์ฒ˜๋ฆฌ ๋ถ€๋ถ„์€ Django์˜ ๊ฒƒ์„ ์ด์šฉํ•˜๋ฉด์„œ ๋ช‡๋ช‡ ์‚ฌ์šฉ์ž ์ •์˜ ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ๊ตฌํ˜„ AbstractBaseUser ๋ชจ๋ธ ์ƒ์†ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ User ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ๊ฐœ์š” AbstractBaseUser ๋ชจ๋ธ์„ ์ƒ์†ํ•œ User ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ  settings.py์— ์ฐธ์กฐ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์€ AbstractUser ๋ชจ๋ธ์„ ์ƒ์†ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋”ฐ๋ผ์„œ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ ์ „์— ์ด ๊ธฐ๋ฒ•์˜ ์‚ฌ์šฉ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ AbstractUser ๋ชจ๋ธ์„ ์ƒ์†ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋‹ฌ๋ฆฌ ๋กœ๊ทธ์ธ ์•„์ด๋””๋กœ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๊ฑฐ๋‚˜ Django ๋กœ๊ทธ์ธ ์ ˆ์ฐจ๊ฐ€ ์•„๋‹Œ ์ธ์ฆ ์ ˆ์ฐจ๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ตฌํ˜„ AbstractBaseUser์˜ ์ƒ์† ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค. ๊ฒฐ๋ก  ๋˜๋Š” ์˜๊ฒฌ Django์˜ ๊ธฐ๋ณธ ์ธ์ฆ ์‹œ์Šคํ…œ์„ ์žฌ์‚ฌ์šฉ ์—ฌ๋ถ€๊ฐ€ ์–ด๋– ํ•œ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š”๋ฐ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค. Django์˜ ๊ธฐ๋ณธ ์ธ์ฆ ์‹œ์Šคํ…œ์„ ์žฌ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ผ๋Œ€์ผ ๊ด€๊ณ„๋ฅผ ๋งบ๋Š” ํ”„๋กœํ•„ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌํ•œ๋‹ค. ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๊ด€๋ฆฌ๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด AbstractBaseUser ํ…Œ์ด๋ธ”์„ ์ƒ์†ํ•ด ๋ชจ๋ธ์„ ๋งŒ๋“ ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ ๋งŒ์•ฝ์— Django๋กœ ์‹ ๊ทœ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค๋ฉด ์ผ๋Œ€์ผ ๊ด€๊ณ„๋ฅผ ๋งบ๋Š” ํ”„๋กœํ•„ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ณ  ๊ธฐ์กด ์‹œ์Šคํ…œ์„ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” AbstractBaseUser ํ…Œ์ด๋ธ”์„ ์ƒ์†ํ•ด ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•˜๋‹ค. ์ฐธ๊ณ ๋ฌธํ—Œ How to Extend Django User Model 04) ์ปค์Šคํ…€ User ๋ชจ๋ธ (AbstractBaseUser์˜ ์ƒ์†) AbstractBaseUser ํด๋ž˜์Šค ์ƒ์†์˜ ์žฅ๋‹จ์  ๊ตฌํ˜„ ์˜ˆ์‹œ account/models.py account/forms.py account/admin.py settings.py AbstractBaseUser ํด๋ž˜์Šค ์ƒ์†์˜ ์žฅ๋‹จ์  AbstractBaseUser ๋ชจ๋ธ์„ ์ƒ์†ํ•œ User ์ปค์Šคํ…€ ๋ชจ๋ธ์„ ๋งŒ๋“ค๋ฉด ๋กœ๊ทธ์ธ ์•„์ด๋””๋กœ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ Django ๋กœ๊ทธ์ธ ์ ˆ์ฐจ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์ธ์ฆ ์ ˆ์ฐจ๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ธฐ์กด์— ์šด์˜ ์ค‘์ด๋˜ PHP ์„ค๋ฃจ์…˜์˜ ํšŒ์› ๋””๋น„๋ฅผ ๊ทธ๋Œ€๋กœ ์žฌ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด AbstractBaseUser ๋ชจ๋ธ์„ ์ƒ์†ํ•œ User ์ปค์Šคํ…€ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๋‹จ์ ์€ ์šด์˜ ์ค‘์— ์‹œ์Šคํ…œ ์‚ฌ์šฉ์ž ๋ชจ๋ธ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์–ด๋ ต๋‹ค๋Š” ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฏธ ์šด์˜ ์ค‘์ธ Django ๊ธฐ๋ฐ˜ ์›น ์‚ฌ์ดํŠธ์˜ ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋ƒฅ ๊ธฐ์กด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์ž. ๊ตฌํ˜„ ์˜ˆ์‹œ Django ๊ณต์‹ ๋ฌธ์„œ์—์„œ Customizing authentication in Django - A full example ๋ถ€๋ถ„์— ์˜ˆ์ œ๊ฐ€ ์žˆ๋‹ค. ํ•ด๋‹น ์˜ˆ์ œ์—์„œ ์ˆ˜์ •๋œ ๋ถ€๋ถ„์„ ์ค‘์‹ฌ์œผ๋กœ ์„ค๋ช…ํ•œ๋‹ค. account/models.py ํšŒ์› ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด account ๊ฐ™์ด ์›ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ๋ณ„๋„์˜ ์•ฑ์„ ๋งŒ๋“ ๋‹ค. ๊ณต์‹ ๋ฌธ์„œ ์˜ˆ์ œ์™€ ๋‹ค๋ฅธ ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. AbstractBaseUser ์ƒ์†๋ฟ๋งŒ ์•„๋‹ˆ๋ผ PermissionsMixin์„ ๋‹ค์ค‘ ์ƒ์†ํ•œ๋‹ค. Django์˜ ๊ธฐ๋ณธ ๊ทธ๋ฃน, ํ—ˆ๊ฐ€๊ถŒ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ์žฌ์‚ฌ์šฉํ•œ๋‹ค. ์ƒ์ผ(date_of_birth) ํ•„๋“œ ๋Œ€์‹ ์— ๋‹‰๋„ค์ž„(nickname) ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. from django.contrib.auth.models import ( BaseUserManager, AbstractBaseUser, PermissionsMixin ) from django.db import models from django.utils import timezone from django.utils.translation import ugettext_lazy as _ class UserManager(BaseUserManager): def create_user(self, email, nickname, password=None): """ ์ฃผ์–ด์ง„ ์ด๋ฉ”์ผ, ๋‹‰๋„ค์ž„, ๋น„๋ฐ€๋ฒˆํ˜ธ ๋“ฑ ๊ฐœ์ธ ์ •๋ณด๋กœ User ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ """ if not email: raise ValueError(_('Users must have an email address')) user = self.model( email=self.normalize_email(email), nickname=nickname, ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, nickname, last_name, first_name, password): """ ์ฃผ์–ด์ง„ ์ด๋ฉ”์ผ, ๋‹‰๋„ค์ž„, ๋น„๋ฐ€๋ฒˆํ˜ธ ๋“ฑ ๊ฐœ์ธ ์ •๋ณด๋กœ User ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๋‹จ, ์ตœ์ƒ์œ„ ์‚ฌ์šฉ์ž์ด๋ฏ€๋กœ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•œ๋‹ค. """ user = self.create_user( email=email, password=password, nickname=nickname, ) user.is_superuser = True user.save(using=self._db) return user class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField( verbose_name=_('Email address'), max_length=255, unique=True, ) nickname = models.CharField( verbose_name=_('Nickname'), max_length=30, unique=True ) is_active = models.BooleanField( verbose_name=_('Is active'), default=True ) date_joined = models.DateTimeField( verbose_name=_('Date joined'), default=timezone.now ) # ์ด ํ•„๋“œ๋Š” ๋ ˆ๊ฑฐ์‹œ ์‹œ์Šคํ…œ ํ˜ธํ™˜์„ ์œ„ํ•ด ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. salt = models.CharField( verbose_name=_('Salt'), max_length=10, blank=True ) objects = UserManager() USER NAME_FIELD = 'email' REQUIRED_FIELDS = ['nickname', ] class Meta: verbose_name = _('user') verbose_name_plural = _('users') ordering = ('-date_joined',) def __str__(self): return self.nickname def get_full_name(self): return self.nickname def get_short_name(self): return self.nickname @property def is_staff(self): "Is the user a member of staff?" # Simplest possible answer: All superusers are staff return self.is_superuser get_full_name.short_description = _('Full name') account/forms.py from django import forms from django.contrib.auth.forms import ReadOnlyPasswordHashField from django.utils.translation import ugettext_lazy as _ from .models import User, UserManager class UserCreationForm(forms.ModelForm): # ์‚ฌ์šฉ์ž ์ƒ์„ฑ ํผ email = forms.EmailField( label=_('Email'), required=True, widget=forms.EmailInput( attrs={ 'class': 'form-control', 'placeholder': _('Email address'), 'required': 'True', } ) ) nickname = forms.CharField( label=_('Nickname'), required=True, widget=forms.TextInput( attrs={ 'class': 'form-control', 'placeholder': _('Nickname'), 'required': 'True', } ) ) password1 = forms.CharField( label=_('Password'), widget=forms.PasswordInput( attrs={ 'class': 'form-control', 'placeholder': _('Password'), 'required': 'True', } ) ) password2 = forms.CharField( label=_('Password confirmation'), widget=forms.PasswordInput( attrs={ 'class': 'form-control', 'placeholder': _('Password confirmation'), 'required': 'True', } ) ) class Meta: model = User fields = ('email', 'nickname') def clean_password2(self): # ๋‘ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ์ผ์น˜ ํ™•์ธ password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise forms.ValidationError("Passwords don't match") return password2 def save(self, commit=True): # Save the provided password in hashed format user = super(UserCreationForm, self).save(commit=False) user.email = UserManager.normalize_email(self.cleaned_data['email']) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user class UserChangeForm(forms.ModelForm): # ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ํผ password = ReadOnlyPasswordHashField( label=_('Password') ) class Meta: model = User fields = ('email', 'password', 'last_name', 'first_name', 'is_active', 'is_superuser') def clean_password(self): # Regardless of what the user provides, return the initial value. # This is done here, rather than on the field, because the # field does not have access to the initial value return self.initial["password"] account/admin.py account ์•ฑ์˜ admin.py ํŒŒ์ผ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค. from django.contrib import admin from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.models import Group from django.utils.translation import ugettext_lazy as _ from .forms import UserCreationForm, UserChangeForm from .models import User class UserAdmin(BaseUserAdmin): # The forms to add and change user instances form = UserChangeForm add_form = UserCreationForm # The fields to be used in displaying the User model. # These override the definitions on the base UserAdmin # that reference specific fields on auth.User. list_display = ('get_full_name', 'email', 'nickname', 'is_active', 'is_superuser', 'date_joined') list_display_links = ('get_full_name',) list_filter = ('is_superuser', 'is_active',) fieldsets = ( (None, {'fields': ('email', 'password')}), (_('Personal info'), {'fields': ('nickname', )}), (_('Permissions'), {'fields': ('is_active', 'is_superuser',)}), ) # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin # overrides get_fieldsets to use this attribute when creating a user. add_fieldsets = ( (None, { 'classes': ('wide',), 'fields': ('email', 'nickname', 'password1', 'password2')} ), ) search_fields = ('email','nickname') ordering = ('-date_joined',) filter_horizontal = () # Now register the new UserAdmin... admin.site.register(User, UserAdmin) settings.py ์‹œ์Šคํ…œ ์‚ฌ์šฉ์ž ๋ชจ๋ธ์„ ๋””ํดํŠธ์—์„œ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •์„ ๋ณ€๊ฒฝํ•œ๋‹ค. AUTH_USER_MODEL = 'account.User' 05) ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณ€๊ฒฝ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณ€๊ฒฝ ๊ตฌํ˜„ ์˜ˆ์‹œ ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ ๋ฐฑ์—”๋“œ ๊ตฌํ˜„ ๋ฐฑ์—”๋“œ ๋“ฑ๋ก ํšŒ์› ๊ฐ€์ž… ์ฒ˜๋ฆฌ User ๋ชจ๋ธ์—์„œ set_password ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ๋ฐ ๋ฆฌ์…‹ User ๋ชจ๋ธ์—์„œ check_password ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณ€๊ฒฝ ๊ธฐ์กด ์‹œ์Šคํ…œ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ ์ธ์ฆ ๋ฐฑ์—”๋“œ(Authentication Backend)์˜ authenticate() ๋ฉ”์„œ๋“œ์™€ get_user() ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค. AbstractBaseUser์˜ ์ƒ์† ์ฐธ๊ณ  ๊ทธ๋ฆฌ๊ณ  User ๋ชจ๋ธ์˜ set_password()์™€ check_password() ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค. ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณ€๊ฒฝ์„ ์œ„ํ•ด ๋‹ค์Œ ์‚ฌํ•ญ์„ ๊ฐ€์ •ํ•œ๋‹ค. AbstractBaseUser ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ด User ๋ชจ๋ธ ์žฌ์ •์˜ํ•œ๋‹ค. User ๋ชจ๋ธ์—๋Š” ๊ธฐ์กด ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ˜ธํ™˜์„ ์œ„ํ•ด password, salt ํ•„๋“œ ๋“ฑ์ด ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’์œผ๋กœ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ๊ตฌํ˜„ ์˜ˆ์‹œ PHP ์˜คํ”ˆ์†Œ์Šค ์‡ผํ•‘๋ชฐ Opencart์˜ ๊ธฐ์กด ํšŒ์› DB๋ฅผ Django์—์„œ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์˜ˆ์ œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค. ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ ๋ฐฑ์—”๋“œ ๊ตฌํ˜„ account/backends.py ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋ฐฑ์—”๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค. ์ด ๋ฐฑ์—”๋“œ ์•ˆ์— authenticate() ๋ฉ”์„œ๋“œ๊ฐ€ ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค. from hashlib import sha1 from .models import User class OpencartBackend: # Django ๋กœ๊ทธ์ธ ํ”„๋Ÿฌ์‹œ ์ €๊ฐ€ ํ˜ธ์ถœํ•˜๋Š” ์ธ์ฆ ๋ฉ”์„œ๋“œ def authenticate(self, user name=None, password=None): try: # ์ปค์Šคํ…€ User ๋ชจ๋ธ์—์„œ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ user name์œผ๋กœ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ user = User.objects.get(email=User name) # Salted password SHA1 hashing 3 iterations # Opencart PHP ํ”„๋กœ๊ทธ๋žจ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ hashed = sha1( (user.salt + sha1( (user.salt + sha1( password.encode('utf8') ).hexdigest()).encode('utf8') ).hexdigest()).encode('utf8') ).hexdigest() if user.password == hashed: return user else: return None except User.DoesNotExist: return None # Required for your backend to work properly - unchanged in most scenarios def get_user(self, user_id): try: return User.objects.get(pk=user_id) except User.DoesNotExist: return None ์œ„ ์˜ˆ์‹œ ์ฝ”๋“œ๋Š” ์†”ํŠธ๊ฐ’์œผ๋กœ SHA1 ์•”ํ˜ธํ™” 3ํšŒ ๋Œ๋ฆฌ๋Š” ๊ธฐ์กด ์‹œ์Šคํ…œ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ–ˆ๋‹ค. ๊ธฐ์กด ์‹œ์Šคํ…œ์˜ ์•”ํ˜ธํ™” ๋ฐฉ์‹์€ ์‹œ์Šคํ…œ๋งˆ๋‹ค ๋‹ค๋ฅด๋ฏ€๋กœ ์•Œ๋งž๊ฒŒ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋ฐฑ์—”๋“œ ๋“ฑ๋ก settings.py ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐฑ์—”๋“œ๋ฅผ ๋“ฑ๋กํ•œ๋‹ค. AUTHENTICATION_BACKENDS = ('account.backends.OpencartBackend',) ์•ฑ. ๋ฐฑ์—”๋“œ ๋ชจ๋“ˆ. ํด๋ž˜์Šค ๊ตฌ์กฐ๋กœ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์ ์–ด์ค€๋‹ค. ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฉ”์ผ, ๋น„๋ฐ€๋ฒˆํ˜ธ, ์†”ํŠธ ๊ฐ’์ด ์˜ฌ๋ฐ”๋กœ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ๋กœ๊ทธ์ธ์ด ์ž˜ ๋œ๋‹ค. ํšŒ์› ๊ฐ€์ž… ์ฒ˜๋ฆฌ User ๋ชจ๋ธ์—์„œ set_password ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํšŒ์› ๊ฐ€์ž…์—์„œ๋Š” ์ฒซ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์กด ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก User ๋ชจ๋ธ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. salt ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. set_password() ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•œ๋‹ค. import os from hashlib import sha1, md5 class User(AbstractBaseUser, PermissionsMixin): ... ์ƒ๋žต ... salt = models.CharField( verbose_name=_('Salt'), max_length=10, blank=True ) ... ์ƒ๋žต ... def set_password(self, raw_password): # Opencart์˜ salt ๊ฐ’์€ 9์ž๋ฆฌ์˜ alphanumeric ๋ฌธ์ž์—ด salt = md5(os.urandom(128)).hexdigest()[:9] # Opencart PHP ํ”„๋กœ๊ทธ๋žจ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ hashed = sha1( (salt + sha1( (salt + sha1( raw_password.encode('utf8') ).hexdigest()).encode('utf8') ).hexdigest()).encode('utf8') ).hexdigest() self.salt = salt self.password = hashed ... ์ƒ๋žต ... ์ด์ œ ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž๋ฅผ ๋งŒ๋“ค ๋•Œ์—๋„ ๊ธฐ์กด ์‹œ์Šคํ…œ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋งž๊ฒŒ password์™€ salt ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ๋ฐ ๋ฆฌ์…‹ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ๊ณผ ๋ฆฌ์…‹์—์„œ๋Š” ์ƒˆ๋กœ์šด ๋น„๋ฐ€๋ฒˆํ˜ธ ์ €์žฅ์„ ์œ„ํ•ด set_password() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์ง€๋งŒ ๊ทธ์ „์— ๊ธฐ์กด ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž…๋ ฅํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” check_password() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ ์ด๋ฅผ ๊ธฐ์กด ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ˆ˜์ •ํ•œ๋‹ค. User ๋ชจ๋ธ์—์„œ check_password ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ User ๋ชจ๋ธ์˜ check_password() ๋ฉ”์„œ๋“œ๋ฅผ ๊ธฐ์กด ์‹œ์Šคํ…œ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋งž๊ฒŒ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์•ผ ํ•œ๋‹ค. import os from hashlib import sha1, md5 class User(AbstractBaseUser, PermissionsMixin): ... ์ƒ๋žต ... def check_password(self, raw_password): try: user = User.objects.get(email=self.email) hashed = sha1( (user.salt + sha1( (user.salt + sha1( raw_password.encode('utf8') ).hexdigest()).encode('utf8') ).hexdigest()).encode('utf8') ).hexdigest() if user.password == hashed: return True else: return False except User.DoesNotExist: return False ... ์ƒ๋žต ... 06) ํšŒ์› ๊ฐ€์ž… ์ด๋ฉ”์ผ ์ธ์ฆ ์ฒ˜๋ฆฌ ํšŒ์› ๊ฐ€์ž… ์ด๋ฉ”์ผ ์ธ์ฆ ์ฒ˜๋ฆฌ ํšŒ์› ๊ฐ€์ž… ํผ ์ธ์ฆ ๋ฉ”์ผ ๋ฐœ์†ก ์–‘์‹ ์ด๋ฉ”์ผ ์ธ์ฆ ํ™œ์„ฑํ™” ์ฒ˜๋ฆฌ ๋ทฐ ์ด๋ฉ”์ผ ์ธ์ฆ ์™„๋ฃŒ ํ…œํ”Œ๋ฆฟ URL ํŒจํ„ด ๋“ฑ๋ก ์ฐธ๊ณ  UID ๋ณ€ํ™˜ ํ† ํฐ ์ƒ์„ฑ ์ด๋ฉ”์ผ ๋ฐœ์†ก ์ฒ˜๋ฆฌ ์ฐธ๊ณ  ์‚ฌ์ดํŠธ ํšŒ์› ๊ฐ€์ž… ์ด๋ฉ”์ผ ์ธ์ฆ ์ฒ˜๋ฆฌ ํšŒ์› ๊ฐ€์ž… ํผ UserCreationForm์„ ์ƒ์†ํ•œ ์›น ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ํšŒ์›๊ฐ€์ž…์„ ๋ฐ›๊ธฐ ์œ„ํ•œ WebUserCreationForm ํผ ํด๋ž˜์Šค๋ฅผ ๋ณ„๋„๋กœ ๋งŒ๋“ ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๊ฐ™์€ ํผ์„ ์‚ฌ์šฉํ•ด์„œ ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์—์„œ ์‚ฌ์šฉ์ž๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒฝ์šฐ ์ด๋ฉ”์ผ ๋ฐœ์†ก์„ ์œ„ํ•ด request ๊ฐ์ฒด๋ฅผ ์–ป์ง€ ๋ชปํ•˜๋ฏ€๋กœ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ํŠนํžˆ, ๊ด€๋ฆฌ์ž๊ฐ€ ์ง์ ‘ ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ํ•  ๋• ์–ด๋–ค ๋ชฉ์ ์ด ์žˆ์„ ๊ฒƒ์ด๋ฏ€๋กœ ๊ตณ์ด ์ด๋ฉ”์ผ ์ธ์ฆ ํ™œ์„ฑํ™”๊ฐ€ ๋ถˆํ•„์š”ํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋œ๋‹ค. Django ๊ธฐ๋ณธ ํผ์—์„œ ์›น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์œ„ํ•œ ํผ์„ ๋ถ„๋ฆฌํ•˜๋ฉด ์ด์šฉ์•ฝ๊ด€, ๊ฐœ์ธ์ •๋ณด๋ณดํ˜ธ ๋ฐฉ์นจ ๋™์˜ ํผ ๋“ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. UserCreationForm ํผ ํด๋ž˜์Šค๋Š” ์ด๋ฏธ email ํ•„๋“œ๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ณ  ํ•„์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›๋„๋ก ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. class UserCreationForm(forms.ModelForm): email = forms.EmailField( label=_('Email'), required=True, widget=forms.EmailInput( attrs={ 'class': 'form-control', 'placeholder': _('Email address'), 'required': 'True', } ) ) ... ์ƒ๋žต ... class Meta: model = User fields = ('email', 'last_name', 'first_name', 'nickname') UserCreationForm ํผ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” WebUserCreationForm ํผ ํด๋ž˜์Šค๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค. class WebUserCreationForm(UserCreationForm): terms = forms.BooleanField( label=_('Terms of service'), widget=forms.CheckboxInput( attrs={ 'required': 'True', } ), error_messages={ 'required': _('You must agree to the Terms of service to sign up'), } ) privacy = forms.BooleanField( label=_('Privacy policy'), widget=forms.CheckboxInput( attrs={ 'required': 'True', } ), error_messages={ 'required': _('You must agree to the Privacy policy to sign up'), } ) def __init__(self, *args, **kwargs): # important to "pop" added kwarg before call to parent's constructor self.request = kwargs.pop('request') super(UserCreationForm, self).__init__(*args, **kwargs) def save(self, commit=True): user = super(WebUserCreationForm, self).save(commit=False) if commit: user.is_active = False user.save() # Send user activation mail current_site = get_current_site(self.request) subject = (_('Welcome To %s! Confirm Your Email') % current_site.name) message = render_to_string('registration/user_activate_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': PasswordResetTokenGenerator().make_token(user), }) email = EmailMessage(subject, message, to=[user.email]) email.send() return user ํ† ํฐ ์ƒ์„ฑ์„ ์œ„ํ•ด PasswordResetTokenGenerator Django ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•œ๋‹ค. ๋ณ„๋„๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ์ž‘ํ•  ํ•„์š”๊ฐ€ ์—†๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ถ”๊ฐ€ ํ•„๋“œ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. ์ธ์ฆ ๋ฉ”์ผ ๋ฐœ์†ก ์–‘์‹ templates/registration/user_activate_email.html ํŒŒ์ผ {% load i18n %}{% autoescape off %} {% blocktrans %} Please click on the link to confirm your registration, {% endblocktrans %} http://{{ domain }}{% url 'account:activate' uidb64=uid token=token %} {% endautoescape %} ์ด๋ฉ”์ผ ์ธ์ฆ ํ™œ์„ฑํ™” ์ฒ˜๋ฆฌ ๋ทฐ account/views.py ํŒŒ์ผ class UserActivateView(TemplateView): logger = logging.getLogger(__name__) template_name = 'registration/user_activate_complete.html' def get(self, request, *args, **kwargs): self.logger.debug('UserActivateView.get()') uid = force_text(urlsafe_base64_decode(self.kwargs['uidb64'])) token = self.kwargs['token'] self.logger.debug('uid: %s, token: %s' % (uid, token)) try: user = User.objects.get(pk=uid) except(TypeError, ValueError, OverflowError, User.DoesNotExist): self.logger.warning('User %s not found' % uid) user = None if user is not None and PasswordResetTokenGenerator().check_token(user, token): user.is_active = True user.save() self.logger.info('User %s(pk=%s) has been activated.' % (user, user.pk)) return super(UserActivateView, self).get(request, *args, **kwargs) ์ด๋ฉ”์ผ ์ธ์ฆ ์™„๋ฃŒ ํ…œํ”Œ๋ฆฟ {% extends "base.html" %} {% load i18n %} {% block title %}{{ title }}{% endblock %} {% block content %} <div class="container"> <div class="spacer_30">ย </div> <div class="spacer_90 hidden-sm hidden-xs">ย </div> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-default"> <div class="panel-body"> <p>{% trans "Your account has been activated. You may go ahead and log in now." %}</p> <p><a class="btn btn-success" href="{% url 'account:login' %}">{% trans 'Log in' %}</a></p> </div> </div> </div> </div> </div> {% endblock %} URL ํŒจํ„ด ๋“ฑ๋ก urlpatterns = [ ... ์ƒ๋žต ... url( r'^register/$', UserCreateView.as_view(), name='register' ), url( r'^register/done/$', UserCreateDoneTemplateView.as_view(), name='register-done' ), url( r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', UserActivateView.as_view(), name='activate' ) ] ์ฐธ๊ณ  UID ๋ณ€ํ™˜ ์‚ฌ์šฉ์ž PK ๊ฐ’์„ UID๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode uid = urlsafe_base64_encode(force_bytes(user.pk)) UID์—์„œ ์‚ฌ์šฉ์ž PK๋ฅผ ๊ตฌํ•œ๋‹ค. from django.utils.encoding import force_text from django.utils.http import urlsafe_base64_decode uid = force_text(urlsafe_base64_decode(self.kwargs['uidb64'])) ํ† ํฐ ์ƒ์„ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ† ํฐ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. from django.contrib.auth.tokens import PasswordResetTokenGenerator token = PasswordResetTokenGenerator().make_token(user) ํ† ํฐ์ด ์˜ฌ๋ฐ”๋ฅธ์ง€ ๊ฒ€์ฆํ•˜๊ณ  True/False ๋ฐ˜ํ™˜ํ•œ๋‹ค. PasswordResetTokenGenerator().check_token(user, token) ํ† ๊ทผ์˜ ์œ ํšจ๊ธฐ๊ฐ„์€ ๊ธฐ๋ณธ๊ฐ’์ด 3์ผ์ด๋ฉฐ ์ด๋ฅผ ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ settings.py์—์„œ ๋ณ€๊ฒฝํ•œ๋‹ค. PASSWORD_RESET_TIMEOUT_DAYS = 1 ๋‹จ, ์ด ๊ฐ’์„ ๋ฐ”๊พธ๋ฉด ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ฆฌ์…‹ ์š”์ฒญ ์ด๋ฉ”์ผ์˜ ํ† ํฐ ์œ ํšจ๊ธฐ๊ฐ„๋„ 1์ผ๋กœ ์ค„์–ด๋“ ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์–ด๋Š ์ชฝ์ด๋“  ๊ตณ์ด ์ด ํ† ํฐ ๊ฐ’์„ 1์ผ ์ด์ƒ ์œ ์ง€ํ•  ์ด์œ ๋Š” ์—†์–ด ๋ณด์ธ๋‹ค. ์ด๋ฉ”์ผ ๋ฐœ์†ก ์ฒ˜๋ฆฌ from django.contrib.sites.shortcuts import get_current_site from django.core.mail import EmailMessage from django.template.loader import render_to_string current_site = get_current_site(self.request) subject = (_('Welcome To %s! Confirm Your Email') % current_site.name) message = render_to_string('registration/user_activate_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': PasswordResetTokenGenerator().make_token(user), }) email = EmailMessage(subject, message, to=[user.email]) email.send() registration/user_activate_email.html ์–‘์‹์— ๋”ฐ๋ผ์„œ ์ด๋ฉ”์ผ์„ ๋ฐœ์†กํ•œ๋‹ค. ๋‹จ, settings.py ํŒŒ์ผ์—์„œ ์ด๋ฉ”์ผ ๋ฐฑ์—”๋“œ๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•˜๋ฉฐ ํ…Œ์ŠคํŠธ๋กœ gmail์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๋น„์Šทํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = 'user name@gmail.com' EMAIL_HOST_PASSWORD = '์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ' EMAIL_USE_TLS = True ์ฐธ๊ณ  ์‚ฌ์ดํŠธ Django registration with confirmation email 07) ํšŒ์› ๊ฐ€์ž… ๋ฐ ๋กœ๊ทธ์ธ reCAPTCHA ์ž…๋ ฅ ํšŒ์› ๊ฐ€์ž…ํ•  ๋•Œ ๋กœ๊ทธ์ธํ•  ๋•Œ ํšŒ์› ๊ฐ€์ž… ๋ฐ ๋กœ๊ทธ์ธํ•  ๋•Œ ๋ถˆํŠน์ • ๋‹ค์ˆ˜ ๋˜๋Š” ๋ด‡์œผ๋กœ๋ถ€ํ„ฐ ๊ธฐ๊ณ„์ ์ธ ์‹œ๋„๋กœ ๊ณต๊ฒฉ์„ ๋‹นํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ”ผํ•ด๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํšŒ์› ๊ฐ€์ž… ๋ฐ ๋กœ๊ทธ์ธํ•  ๋•Œ reCAPTCHA๋ฅผ ๋„์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ตฌ๊ธ€ reCAPTCHA ํŽ˜์ด์ง€์—์„œ ํ‚ค๋ฅผ ๋ฐœ๊ธ‰๋ฐ›๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํšŒ์› ๊ฐ€์ž…๊ณผ ๋กœ๊ทธ์ธํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•˜์—ฌ reCAPTCHA๋ฅผ ์–ด๋–ป๊ฒŒ ์ ์šฉํ• ์ง€ ์‚ดํŽด๋ณธ๋‹ค. ํšŒ์› ๊ฐ€์ž…ํ•  ๋•Œ ์‚ฌ์šฉ์ž ์ •์˜ WebUserCreationForm ํผ ํด๋ž˜์Šค์— clean() ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค. class WebUserCreationForm(UserCreationForm): ... ์ƒ๋žต ... def clean(self): # Google reCAPTCHA ''' reCAPTCHA ๊ฒ€์ฆ ์‹œ์ž‘ ''' recaptcha_response = self.request.POST.get('g-recaptcha-response') url = 'https://www.google.com/recaptcha/api/siteverify' values = { 'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY, 'response': recaptcha_response } data = urllib.parse.urlencode(values).encode() req = urllib.request.Request(url, data=data) response = urllib.request.urlopen(req) result = json.loads(response.read().decode()) ''' reCAPTCHA ๊ฒ€์ฆ ๋ ''' if not result['success']: raise forms.ValidationError(_('reCAPTCHA error occurred.')) return super(WebUserCreationForm, self).clean() ํšŒ์›๊ฐ€์ž… ํ…œํ”Œ๋ฆฟ ํŽ˜์ด์ง€๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. <form action="." method="post" class="form-horizontal"> {% csrf_token %} {% if form.errors %} <div class="alert alert-danger" role="alert"> {% for error in form.errors.values %} {{ error|striptags }} {% endfor %} </div> {% endif %} <div class="form-group"> <label for="{{ form.email.id_for_label }}" class="col-sm-3 control-label">{{ form.email.label }}</label> <div class="col-sm-9"> {{ form.email }} {{ form.email.errors|striptags }} </div> </div> ... ์ƒ๋žต ... <div class="form-group"> <div class="col-sm-offset-3 col-sm-9"> <div class="checkbox"> <label> {{ form.terms }} {% trans 'I have read and agree to the Terms of service.' %} </label> </div> </div> </div> ... ์ƒ๋žต ... <div class="form-group"> <div class="col-sm-offset-3 col-sm-9"> <!-- Google reCAPTCHA --> <script src='https://www.google.com/recaptcha/api.js'></script> <div class="g-recaptcha" data-sitekey="๊ณต๊ฐœ ์‚ฌ์ดํŠธ ํ‚ค๊ฐ’"></div> </div> </div> <div class="form-group"> <div class="col-sm-offset-3 col-sm-9"> <button type="submit" class="btn btn-default">{% trans 'Sign up' %}</button> </div> </div> </form> ๋กœ๊ทธ์ธํ•  ๋•Œ ์‚ฌ์šฉ์ž ์ •์˜ UserLoginForm ํผ ํด๋ž˜์Šค์— clean() ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค. WebUserCreationForm ํผ ํด๋ž˜์Šค์— clean() ๋ฉ”์„œ๋“œ์™€ ๋‹ค๋ฅธ ๋ถ€๋ถ„์€ ๋ถ€๋ชจ ์ƒ์„ฑ์ž ํ˜ธ์ถœ ์ด๋ฆ„๋ฐ–์— ์—†๋‹ค. class UserLoginForm(AuthenticationForm): ... ์ƒ๋žต ... def clean(self): # Google reCAPTCHA ''' reCAPTCHA ๊ฒ€์ฆ ์‹œ์ž‘ ''' recaptcha_response = self.request.POST.get('g-recaptcha-response') url = 'https://www.google.com/recaptcha/api/siteverify' values = { 'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY, 'response': recaptcha_response } data = urllib.parse.urlencode(values).encode() req = urllib.request.Request(url, data=data) response = urllib.request.urlopen(req) result = json.loads(response.read().decode()) ''' reCAPTCHA ๊ฒ€์ฆ ๋ ''' if not result['success']: raise forms.ValidationError(_('reCAPTCHA error occurred.')) return super(UserLoginForm, self).clean() ๋กœ๊ทธ์ธ ํ…œํ”Œ๋ฆฟ ํŽ˜์ด์ง€๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. <form action="." method="post" accept-charset="UTF-8" role="form"> {% csrf_token %} {% if form.errors %} <div class="alert alert-danger" role="alert"> {% for error in form.errors.values %} {{ error|striptags }} {% endfor %} </div> {% endif %} <fieldset> <div class="form-group"> {{ form.user name }} {{ form.user name.errors }} </div> <div class="form-group"> {{ form.password }} {{ form.password.errors }} </div> <div class="form-group"> <!-- Google reCAPTCHA --> <script src='https://www.google.com/recaptcha/api.js'></script> <div class="g-recaptcha" data-sitekey="๊ณต๊ฐœ ์‚ฌ์ดํŠธ ํ‚ค๊ฐ’"></div> </div> <div class="checkbox"> <label> <input name="remember" type="checkbox" value="Remember Me"> {% trans 'Remember me' %} </label> <a href="{% url 'account:password_reset' %}" class="pull-right">{% trans 'Forgot password' %}</a> </div> <input type="hidden" name="next" value="{{ next }}"/> <input class="btn btn-mg btn-success btn-block" type="submit" value="{% trans 'Sign in' %}"> </fieldset> </form> 08) ๋กœ๊ทธ์ธ ๋กœ๊น… (์‹œ๊ทธ๋„) ์‹œ๊ทธ๋„ ๋ชจ๋ธ ์ž‘์„ฑ ๋กœ๊ทธ์ธ ๋กœ๊ทธ๋ฅผ ์ €์žฅํ•˜๋Š” ์‹œ๊ทธ๋„ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ ๊ด€๋ฆฌ์ž ํ™”๋ฉด ๋ณด๊ธฐ ๋งค๋ฒˆ ๋กœ๊ทธ์ธํ•  ๋•Œ ์‚ฌ์šฉ์ž์˜ ๊ธฐ๋ณธ์ ์ธ ์ ‘์† ์ •๋ณด๋ฅผ ๋กœ๊ทธ ๋‚จ๊ฒจ๋‘๋Š” ๊ฒƒ์€ ํ–ฅํ›„ ๋ณด์•ˆ ์‹œ์Šคํ…œ ๊ด€๋ฆฌ์ƒ ์œ ์šฉํ•˜๋‹ค. ์‹œ๊ทธ๋„ ์‹œ๊ทธ๋„ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. account/signals.py ํŒŒ์ผ import logging from django.contrib.auth.signals import user_logged_in, user_logged_out from django.dispatch import receiver @receiver(user_logged_in) def sig_user_logged_in(sender, user, request, **kwargs): logger = logging.getLogger(__name__) logger.debug("user logged in: %s at %s" % (user, request.META['REMOTE_ADDR'])) @receiver ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด sig_user_logged_in() ํ•จ์ˆ˜ ์ด๋ฆ„์œผ๋กœ user_logged_in ์‹œ๊ทธ๋„์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค. ์ฐธ๊ณ ๋กœ django/contrib/auth/__init__.py ํŒŒ์ผ์˜ login(request, user, backend=None) ํ•จ์ˆ˜ ๋งจ ๋ฐ‘์— ๋ณด๋ฉด ๋กœ๊ทธ์ธ ์ž‘์—…์„ ์™„๋ฃŒํ•œ ํ›„ ์‹œ๊ทธ๋„์„ ๋ณด๋‚ด๋Š” ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค. python user_logged_in.send(sender=user.__class__, request=request, user=user) ๋กœ๊ทธ์ธํ•  ๋•Œ๋งˆ๋‹ค ์ด๋ฏธ ์œ„์™€ ๊ฐ™์ด ์‹œ๊ทธ๋„์„ ๋ณด๋‚ด๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์ด๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋ฉด ๋œ๋‹ค. ์‹œ๊ทธ๋„ ์ˆ˜์‹  ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ  ์ด๋ฅผ Django ์•ฑ ์„ค์ •์„ ํ†ตํ•ด ์•„๋ž˜์˜ ์ฝ”๋“œ๋กœ ๋“ฑ๋กํ•ด์•ผ ํ•œ๋‹ค. account/apps.py ํŒŒ์ผ from django.apps import AppConfig from django.utils.translation import ugettext_lazy as _ class AccountConfig(AppConfig): name = 'account' verbose_name = _('Account') def ready(self): import account.signals ์ด์ œ ๋กœ๊ทธ์ธํ•  ๋•Œ ์ฝ˜์†”์—์„œ ๋กœ๊ทธ์ธ ๋กœ๊ทธ๊ฐ€ ์˜ฌ๋ฐ”๋กœ ์ฐํžˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋ชจ๋ธ ์ž‘์„ฑ from django.conf import settings from django.db import models from model_utils.models import TimeStampedModel class UserLoginLog(TimeStampedModel): user = models.ForeignKey( settings.AUTH_USER_MODEL, verbose_name=_('User'), related_name='login_logs', blank=True, null=True ) ip_address = models.GenericIPAddressField( verbose_name=_('IP Address') ) user_agent = models.CharField( verbose_name=_('HTTP User Agent'), max_length=300, ) class Meta: verbose_name = _('user login log') verbose_name_plural = _('user login logs') ordering = ('-created',) def __str__(self): return '%s %s' % (self.user, self.ip_address) ๋กœ๊ทธ์ธ ๋กœ๊ทธ๋ฅผ ์ €์žฅํ•˜๋Š” ์‹œ๊ทธ๋„ ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ ์ด์ œ ์‹ค์ œ๋กœ UserLoginLog ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‹œ๊ทธ๋„ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜์—์„œ ์ž‘์„ฑํ•œ๋‹ค. from ipware.ip import get_ip @receiver(user_logged_in) def sig_user_logged_in(sender, user, request, **kwargs): log = UserLoginLog() log.user = user log.ip_address = get_ip(request) log.user_agent = request.META['HTTP_USER_AGENT'] log.save() ์‚ฌ์šฉ์ž ์•„์ดํ”ผ ์ฃผ์†Œ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด request.META['REMOTE_ADDR'] ์ฝ”๋“œ๊ฐ€ ์•„๋‹Œ django-ipware ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๊ด€๋ฆฌ์ž ํ™”๋ฉด ๋ณด๊ธฐ ๊ด€๋ฆฌ์ž ๋ชจ๋ธ์„ ๋“ฑ๋กํ•œ๋‹ค. account/admin.py ํŒŒ์ผ class UserLoginLogAdmin(admin.ModelAdmin): list_display = ('user', 'ip_address', 'user_agent',) list_filter = ('ip_address',) date_hierarchy = 'created' admin.site.register(UserLoginLog, UserLoginLogAdmin) 07. ๊ด€๋ฆฌ์ž ํ™”๋ฉด 01) ์Šˆํผ ์œ ์ € ์ƒ์„ฑ ์Šˆํผ์œ ์ € ์ƒ์„ฑ ์Šˆํผ์œ ์ € ์ƒ์„ฑ ์Šˆํผ์œ ์ € ์•„์ด๋””์™€ ์ด๋ฉ”์ผ์„ ์ž…๋ ฅํ•˜๊ณ  ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋‘ ๋ฒˆ ์ž…๋ ฅํ•œ๋‹ค. >python manage.py createsuperuser User name (leave blank to use 'pinco'): admin Email address: test@example.com Password: Password (again): Superuser created successfully. ์œ„ ์˜ˆ์‹œ์—์„œ ์Šˆํผ์œ ์ €์˜ ์•„์ด๋””๋Š” ํŽธ์˜์ƒ admin์œผ๋กœ ํ•˜์˜€๋‹ค. 02) ๊ด€๋ฆฌ์ž ํ™”๋ฉด ๊ฐœ์„  admin URL ๊ตฌํ•˜๊ธฐ ์ธ๋ผ์ธ ํผ์…‹์— ์ถ”๊ฐ€/์‚ญ์ œ jQuery ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ admin URL ๊ตฌํ•˜๊ธฐ AdminSite๋Š” ๋‹ค์Œ URL ํŒจํ„ด์„ ์ œ๊ณตํ•œ๋‹ค. ํŽ˜์ด์ง€ 1 URL ์ด๋ฆ„ ํŒŒ๋ผ๋ฏธํ„ฐ ์ธ๋ฑ์Šค admin:index ๋กœ๊ทธ์•„์›ƒ logout ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ password_change ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ ์™„๋ฃŒ password_change_done ์•ฑ ์ธ๋ฑ์Šค ํŽ˜์ด์ง€ app_list app_lebel ๊ฐ์ฒด ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ view_on_site content_type_id, object_id ModelAdmin์€ ๋‹ค์Œ URL ํŒจํ„ด์„ ์ œ๊ณตํ•œ๋‹ค. ํŽ˜์ด์ง€ 1 URL ์ด๋ฆ„ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ชฉ๋ก ์ˆ˜์ • ์•ฑ_๋ชจ๋ธ๋ช…_ changelist ์ถ”๊ฐ€ ์•ฑ_๋ชจ๋ธ๋ช…_ add ํžˆ์Šคํ† ๋ฆฌ ์•ฑ_๋ชจ๋ธ๋ช…_ history object_id ์‚ญ์ œ ์•ฑ_๋ชจ๋ธ๋ช…_ delete object_id ์ˆ˜์ • ์•ฑ_๋ชจ๋ธ๋ช…_ change object_id ์ธ๋ผ์ธ ํผ์…‹์— ์ถ”๊ฐ€/์‚ญ์ œ jQuery ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ Post ๋ชจ๋ธ์„ ์ฐธ์กฐํ•˜๋Š” Image ๋ชจ๋ธ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. Post ๋ชจ๋ธ์„ ์ˆ˜์ •ํ•  ๋•Œ Image ๋ชจ๋ธ์„ ๋™์‹œ์— ์ถ”๊ฐ€, ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์ธ๋ผ์ธ ํผ์…‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. class ImageInline(admin.StackedInline): model = Image extra = 2 class PostAdmin(admin.ModelAdmin): ... inlines = [ImageInline] ... ์ด ํผ์…‹์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€/์‚ญ์ œ๋  ๋•Œ๋งˆ๋‹ค ํ•ฉ๊ณ„ ๋“ฑ์„ ์œ„ํ•œ jQuery ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋จผ์ € ํ•ด๋‹น ๊ด€๋ฆฌ์ž ํ…œํ”Œ๋ฆฟ ํŽ˜์ด์ง€๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์•ผ ํ•œ๋‹ค. blog ์•ฑ์˜ Post ๋ชจ๋ธ ๋ณ€๊ฒฝ ํŽ˜์ด์ง€์ด๋ฏ€๋กœ ๋ชจ๋ธ ๋ณ€๊ฒฝ์„ ์œ„ํ•œ ๊ด€๋ฆฌ์ž ํ…œํ”Œ๋ฆฟ ํŽ˜์ด์ง€ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์€ templates/admin/blog/post/change_form.html ํŒŒ์ผ์„ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ admin/change_form.html ํŒŒ์ผ์„ ์ƒ์† ์žฌ์ •์˜ํ•œ๋‹ค. {% extends 'admin/change_form.html' %} {% load static %} {% block admin_change_form_document_ready %} {{ block.super }} <script type="text/javascript" src="{% static 'js/blog/post/formset_handlers.js' %}"></script> {% endblock %} ํ•ด๋‹น ํŽ˜์ด์ง€์—์„œ {% static 'js/blog/post/formset_handlers.js' %} ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋กœ๋“œํ•˜๊ณ  ์‹ค์ œ js ํŒŒ์ผ ์ฃผ์†Œ๋Š” ์•„๋งˆ๋„ /js/blog/post/formset_handlers.js๋กœ ํ•ด์„๋  ๊ฒƒ์ด๋‹ค. formset_handlers.js ํŒŒ์ผ์˜ ๋‚ด์šฉ์˜ ๋ผˆ๋Œ€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (function ($) { $(document).on('formset:added', function (event, $row, formsetName) { console.log('row added'); }); $(document).on('formset:removed', function (event, $row, formsetName) { console.log('row deleted') }); })(django.jQuery); 03) ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€ ์…€๋ ‰ํŠธ ๋ฐ•์Šค ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ์™ธ๋ž˜ ํ‚ค ์ฐธ์กฐ๋กœ ์…€๋ ‰ํŠธ ๋ฐ•์Šค ๋‚ด์šฉ ์ œํ•œ ์ข…์† ์ค‘์ฒฉ๋œ(cascading) ์…€๋ ‰ํŠธ ๋ฉ”๋‰ด ์™ธ๋ž˜ ํ‚ค ์ฐธ์กฐ๋กœ ์…€๋ ‰ํŠธ ๋ฐ•์Šค ๋‚ด์šฉ ์ œํ•œ "์Šคํฌ์ธ " ๊ฒŒ์‹œํŒ์— "์ถ•๊ตฌ", "์•ผ๊ตฌ" ๋“ฑ์„ ๋ถ„๋ฅ˜ํ•˜๊ณ  ์žˆ๊ณ  "๊ฒŒ์ž„" ๊ฒŒ์‹œํŒ์— "RPG", "FPS" ๋“ฑ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฒŒ์‹œ๋ฌผ ๋ชจ๋ธ์—์„œ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ "์ถ•๊ตฌ", "์•ผ๊ตฌ", "RPG", "FPS" ๋“ฑ ๋ชจ๋“  ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ๋‚˜์—ด๋œ๋‹ค. "์Šคํฌ์ธ " ๊ฒŒ์‹œํŒ์„ ์„ ํƒํ–ˆ์„ ๋•Œ๋Š” "์ถ•๊ตฌ", "์•ผ๊ตฌ" ๋“ฑ ์ข…๋ชฉ๋งŒ ๋ชฉ๋ก์— ๋ณด์ด๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์‚ฌ์šฉ์ž ํŽธ์˜์„ฑ๊ณผ ๋ฐ์ดํ„ฐ์˜ ์ •ํ•ฉ์„ฑ์„ ์œ„ํ•ด ์˜ฌ๋ฐ”๋ฅด๋‹ค. ์ฆ‰, ์™ธ๋ž˜ ํ‚ค์˜ ์ฐธ์กฐ ํ•„๋“œ ๋‚ด์šฉ์— ๋”ฐ๋ผ ์„ ํƒ์ ์œผ๋กœ ์…€๋ ‰ํŠธ ๋ฐ•์Šค ๋ชฉ๋ก์„ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด formfield_for_foreignkey() ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•œ๋‹ค. def get_form(self, request, obj=None, **kwargs): request.current_object = obj return super(MessageAdmin, self).get_form(request, obj, **kwargs) def formfield_for_foreignkey(self, db_field, request, **kwargs): instance = request.current_object if db_field.name == 'category': kwargs['queryset'] = Category.objects.filter(board=instance.board) return super(MessageAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) ๋ฌธ์ œ๋Š” formfield_for_foreignkey ๋ฉ”์„œ๋“œ์—์„œ request.user ๊ฐ์ฒด๋Š” ๋ฐ”๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ฐธ์กฐํ•˜๋Š” ์ธ์Šคํ„ด์Šค์—๋Š” ๋ฐ”๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค. ํŠน์ • ์ธ์Šคํ„ด์Šค๋ฅผ ๋„˜๊ฒจ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ๋Š” get_form() ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜๋Š” ํŠธ๋ฆญ์„ ์จ์•ผ ํ•œ๋‹ค. get_form() ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์„œ request ๊ฐ์ฒด์— ํ˜„์žฌ ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹ด์•„๋‘๊ณ  formfield_for_foreignkey ํ•„๋“œ์—์„œ ๊บผ๋‚ด ์“ฐ๋Š”<NAME>์ด๋‹ค. ์ข…์† ์ค‘์ฒฉ๋œ(cascading) ์…€๋ ‰ํŠธ ๋ฉ”๋‰ด 04) ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€ ์ž…๋ ฅ ํ•„๋“œ ์ปค์Šคํ…€ ์œ„์ ฏ ์ ์šฉ ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€ ์ž…๋ ฅ ํ•„๋“œ ์ปค์Šคํ…€ ์œ„์ ฏ ์ ์šฉ ํ•„๋“œ ์ „์ฒด ์œ„์ ฏ ์ ์šฉ ํŠน์ • ํ•„๋“œ์—๋งŒ ์œ„์ ฏ ์ ์šฉ forms.py admin.py ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€ ์ž…๋ ฅ ํ•„๋“œ ์ปค์Šคํ…€ ์œ„์ ฏ ์ ์šฉ ์ปค์Šคํ…€ ํ•„๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ์š”๋ น์€ ์ปค์Šคํ…€ ํ•„๋“œ ๋งŒ๋“ค๊ธฐ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค. ํ•„๋“œ ์ „์ฒด ์œ„์ ฏ ์ ์šฉ PostAdmin ๊ฐ™์ด ๋ชจ๋ธ ์–ด๋“œ๋ฏผ์— ์กด์žฌํ•˜๋Š” TextField ๋ชจ๋‘ ํŠน์ • ์œ„์ ฏ์„ ์ผ๊ด„ ์ ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์ด๋‹ค. formfield_overrides ๋ณ€์ˆ˜์— ๋”•์…”๋„ˆ๋ฆฌ<NAME>์œผ๋กœ ์œ„์ ฏ์„ ์ง€์ •ํ•œ๋‹ค. from django.db import models from django.contrib import admin from .widgets import SimpleMDEWidget from .models import Post class PostAdmin(admin.ModelAdmin): formfield_overrides = { models.TextField: {'widget': SimpleMDEWidget(wrapper_class='simplemde-box-admin', options=options)}, } PostAdmin ๊ฐ™์ด ๋ชจ๋ธ ์–ด๋“œ๋ฏผ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ TextField๊ฐ€ ์กด์žฌํ•ด๋„ ์ผ๊ด„ ์ ์šฉ๋˜๋ฏ€๋กœ ํŽธ๋ฆฌํ•˜๋‹ค. ํŠน์ • ํ•„๋“œ์—๋งŒ ์œ„์ ฏ ์ ์šฉ PostAdmin ๊ฐ™์ด ๋ชจ๋ธ ์–ด๋“œ๋ฏผ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ TextField๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์— ์ผ๊ด„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ณ  content ํ•„๋“œ์—๋งŒ ์ปค์Šคํ…€ ์œ„์ ฏ์„ ์ ์šฉํ•˜๊ณ  description ํ•„๋“œ์—๋Š” ๋””ํดํŠธ TextArea๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. forms.py from django import forms from django.conf import settings from .widgets import SimpleMDEWidget from .models import Post class PostAdminForm(forms.ModelForm): class Meta: model = Post exclude = [] options = getattr(settings, "SIMPLEMDE_OPTIONS", '') widgets = { 'content': SimpleMDEWidget(wrapper_class='simplemde-box-admin', options=options), } widget ๋ณ€์ˆ˜ ๋”•์…”๋„ˆ๋ฆฌ์— ๋ช…์‹œ์ ์œผ๋กœ ์ ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ํ•„๋“œ ์ด๋ฆ„์„ ์ง€์ •ํ•œ๋‹ค. exclude ๋˜๋Š” fields ์†์„ฑ์œผ๋กœ ์ž…๋ ฅ ๊ฐ€๋Šฅ ํ•„๋“œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. admin.py ๊ด€๋ฆฌ์ž์—์„œ ์“ธ PostAdminForm ์ปค์Šคํ…€ ํผ์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ๋˜ํ•œ ๊ด€๋ฆฌ์ž ํ™”๋ฉด์—์„œ ์‚ฌ์šฉํ•  simplemde.css ์Šคํƒ€์ผ ์‹œํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. from django.contrib import admin from .forms import PostAdminForm class PostAdmin(admin.ModelAdmin): form = PostAdminForm class Media: css = { 'all': ('css/simplemde.css',) } 08. ๋ณด์•ˆ 01) Django ๋ณด์•ˆ ์„ค์ • Django ์ฃผ์š” ๋ณด์•ˆ ๊ธฐ๋Šฅ Django ๋ณด์•ˆ ์„ค์ • ์˜ต์…˜ ์šด์˜ ํ™˜๊ฒฝ์—์„œ DEBUG ์˜ต์…˜ ๋„๊ธฐ ๋ณด์•ˆํ‚ค ๋น„๊ณต๊ฐœ ์•ˆ์ „ํ•œ ์ฟ ํ‚ค ์ด์šฉํ•˜๊ธฐ ALLOWED_HOSTS CSRF XSS SQL ์ธ์ ์…˜ SSL ์„ค์ • Django ์ฃผ์š” ๋ณด์•ˆ ๊ธฐ๋Šฅ XSS(cross-site scripting) CSRF(cross-site request forgery) SQL ์ธ์ ์…˜ ํด๋ฆญ์žฌํ‚น(clickjacking) Django ๋ณด์•ˆ ์„ค์ • ์˜ต์…˜ ์šด์˜ ํ™˜๊ฒฝ์—์„œ DEBUG ์˜ต์…˜ ๋„๊ธฐ DEBUG = False ๋ฐ ALLOWED_HOSTS ์„ค์ • ๋ณด์•ˆํ‚ค ๋น„๊ณต๊ฐœ SECRET_KEY๋ฅผ ์†Œ์Šค ์ €์žฅ์†Œ์— ์ปค๋ฐ‹ ํ•˜์ง€ ์•Š์Œ ์•ˆ์ „ํ•œ ์ฟ ํ‚ค ์ด์šฉํ•˜๊ธฐ SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True ALLOWED_HOSTS CSRF XSS SQL ์ธ์ ์…˜ SSL ์„ค์ • 09. ๊ตญ์ œํ™” 01) ๋‹ค๊ตญ์–ด ์ง€์› ์šฉ์–ด ์ •๋ฆฌ ๋‹ค๊ตญ์–ด ์ง€์› ์ ˆ์ฐจ ์œˆ๋„ gettext/iconv ํ”„๋กœ๊ทธ๋žจ ์„ค์น˜ Django ์ „์—ญ ์„ค์ • ๋ฏธ๋“ค์›จ์–ด ์„ค์ • ๊ธฐ๋ณธ ์–ธ์–ด ์„ค์ • ๋ฒˆ์—ญ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ ์†Œ์Šค ์ฝ”๋“œ ๋ฒˆ์—ญ ๋ฌธ๊ตฌ ์ฒ˜๋ฆฌ ๋ชจ๋ธ ํ…œํ”Œ๋ฆฟ ๋ฒˆ์—ญ ๋ฉ”์‹œ์ง€ ํŒŒ์ผ ์ƒ์„ฑ ๋ฉ”์‹œ์ง€ ํŒŒ์ผ ์ปดํŒŒ์ผ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฒˆ์—ญ ์ฒ˜๋ฆฌ๊ฐ€ ์•ˆ ๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” fuzzy ํ”Œ๋ž˜๊ทธ MacOS์—์„œ gettext ์„ค์น˜ ์šฉ์–ด ์ •๋ฆฌ ๊ตญ์ œํ™”(i18n, internationalization): ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง€์—ญํ™” ์ง€์›์„ ์œ„ํ•ด ์†Œํ”„ํŠธ์›จ์–ด์ ์œผ๋กœ ์ค€๋น„ํ•˜๋Š” ๊ฒƒ ์ง€์—ญํ™”(l8n, localization): ๋ฒˆ์—ญ๊ฐ€๊ฐ€ ๋ฒˆ์—ญํ•˜๊ณ  ์ง€์—ญ<NAME>์— ๋งž๊ฒŒ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ ๋กœ์ผ€์ผ ์ด๋ฆ„: ko_KR์€ ๋Œ€ํ•œ๋ฏผ๊ตญ ํ•œ๊ตญ์–ด์ด๊ณ  ko_KP๋Š” ๋ถํ•œ ํ•œ๊ตญ์–ด ์˜๋ฏธ๋กœ "์–ธ์–ด_๊ตญ๊ฐ€"<NAME>์ด๋‹ค. ์–ธ์–ด ์ฝ”๋“œ: ๋ธŒ๋ผ์šฐ์ €๊ฐ€ HTTP ํ—ค๋” Accept-Language๋กœ ๋ณด๋‚ด๋Š” ๊ฐ’์œผ๋กœ "์–ธ์–ด-๊ตญ๊ฐ€"<NAME>์ด๋ฉฐ ko-kr ๊ฐ™์ด ์“ฐ๋Š”๋ฐ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋˜์–ด ์žˆ๋‹ค. Django ์„ค์ • ํŒŒ์ผ์—์„œ ์“ฐ์ด๋Š” ๊ฐ’์ด๋‹ค. .po ํŒŒ์ผ: ๋ฒˆ์—ญ๊ฐ€๊ฐ€ ์ง์ ‘ ๋ฒˆ์—ญํ•˜๋Š” ๋ฉ”์‹œ์ง€ ํŒŒ์ผ์ด๋‹ค. .mo ํŒŒ์ผ: Django๊ฐ€ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐœ๋ฐœ์ž๊ฐ€. po ๋ฒˆ์—ญ ๋ฉ”์‹œ์ง€ ํŒŒ์ผ์„ ์ปดํŒŒ์ผํ•ด ๋งŒ๋“  ํŒŒ์ผ์ด๋‹ค. ๋‹ค๊ตญ์–ด ์ง€์› ์ ˆ์ฐจ ์œˆ๋„ gettext/iconv ํ”„๋กœ๊ทธ๋žจ ์„ค์น˜ gettext 0.19.8.1 and iconv 1.14 - Binaries for Windows ๋งํฌ์—์„œ ์•Œ๋งž์€ ๋ฒ„์ „์„ ์„ค์น˜ํ•œ๋‹ค. shared DLL ํŒŒ์ผ์€ ์„ค์น˜ ์šฉ๋Ÿ‰์ด ์ž‘๊ณ  DLL ๋ฏธํฌํ•จ static ํŒŒ์ผ์€ ์„ค์น˜ ์šฉ๋Ÿ‰์ด ํฌ๋‹ค. ๋น„๋ก ์šฉ๋Ÿ‰์ด ์ปค๋„ ๊น”๋”ํ•œ ์„ค์น˜, ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•œ static ํŒŒ์ผ์„ ์„ค์น˜ํ•œ๋‹ค. Django ์ „์—ญ ์„ค์ • settings.py ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ˆ˜์ • ๋ฐ ์ถ”๊ฐ€ํ•œ๋‹ค. ๋ฏธ๋“ค์›จ์–ด ์„ค์ • settings.py ํŒŒ์ผ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด SessionMiddleware์™€ CommonMiddleware ์‚ฌ์ด์— LocaleMiddleware๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ๊ธฐ๋ณธ ์–ธ์–ด ์„ค์ • settings.py ํŒŒ์ผ์—์„œ ๋‹ค์Œ ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•œ๋‹ค. LANGUAGE_CODE = 'ko-KR' settings.py ํŒŒ์ผ์—์„œ ์ƒ๋‹จ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด import ๋ฌธ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. from django.utils.translation import ugettext_lazy as _ ํ”„๋กœ์ ํŠธ์—์„œ ์ง€์›ํ•  ๋‹ค๊ตญ์–ด ์–ธ์–ด ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค. LANGUAGES = [ ('ko', _('Korean')), ('en', _('English')), ] ๋ฒˆ์—ญ ํŒŒ์ผ์ด ๋“ค์–ด์žˆ๋Š” locale ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ง€์ •ํ•˜๊ณ  ์‹ค์ œ๋กœ ๋””๋ ‰ํ„ฐ๋ฆฌ๋„ ๋งŒ๋“ค๋„๋ก ํ•œ๋‹ค. LOCALE_PATHS = ( os.path.join(BASE_DIR, 'locale'), ) ๋ฒˆ์—ญ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ ์‹ค์ œ๋กœ ๋ฒˆ์—ญ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์ปดํŒŒ์ผํ•˜๋Š” ์ž‘์—…์œผ๋กœ ์‹ค์งˆ์ ์œผ๋กœ ๊ฐ€์žฅ ์‹œ๊ฐ„์ด ๋งŽ์ด ์†Œ์š”๋˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ์†Œ์Šค ์ฝ”๋“œ ๋ฒˆ์—ญ ๋ฌธ๊ตฌ ์ฒ˜๋ฆฌ ๋ชจ๋ธ models.py ํŒŒ์ผ์—์„œ ์˜ˆ์‹œ์ด๋‹ค. from django.utils.translation import ugettext_lazy as _ class Post(models.Model): STATUS_CHOICES = ( ('draft', _('Draft')), ('published', _('Published')), ) title = models.CharField(_('Title'), max_length=250) ํ…œํ”Œ๋ฆฟ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์—์„œ ์˜ˆ์‹œ์ด๋‹ค. <li><a href="{% url "blog:post_index" %}">{{ _('Blog') }}</a></li> ๋ฒˆ์—ญ ๋ฉ”์‹œ์ง€ ํŒŒ์ผ ์ƒ์„ฑ ์›๋ž˜๋Š” ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ์ „์ฒด ๋ฉ”์‹œ์ง€ ํŒŒ์ผ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. python manage.py makemessage -a ๊ทธ๋Ÿฌ๋‚˜ ์ตœ์ดˆ์—๋Š” ์ž˜ ๋™์ž‘ํ•˜์ง€ ์•Š์•„์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐœ๋ณ„์ ์œผ๋กœ ๋ฉ”์‹œ์ง€ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค. python manage.py makemessages -l ko python manage.py makemessages -l en ๋ฉ”์‹œ์ง€ ํŒŒ์ผ ์ปดํŒŒ์ผ ์ƒ์„ฑํ•œ ๋ฒˆ์—ญ ํŒŒ์ผ์„ Django๊ฐ€ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ปดํŒŒ์ผํ•œ๋‹ค. python manage.py compilemessages ์ปดํŒŒ์ผ ํ›„์—๋Š” Django ์„œ๋ฒ„๋ฅผ ์žฌ๊ธฐ ๋™ํ•ด์•ผ ๋ฉ”์‹œ์ง€ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜์˜๋œ๋‹ค. ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฒˆ์—ญ ์ฒ˜๋ฆฌ๊ฐ€ ์•ˆ ๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” fuzzy ํ”Œ๋ž˜๊ทธ .po ๋ฉ”์‹œ์ง€ ํŒŒ์ผ์— ์ฃผ์„์œผ๋กœ #, fuzzy ํ‘œ์‹œ๊ฐ€ ์žˆ์œผ๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์€ ๋ฒˆ์—ญ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ๋ฒˆ์—ญ ๋ฌธ์ž์—ด ์ƒ๋‹จ์— ์ฃผ์„์œผ๋กœ #, fuzzy ํ‘œ์‹œ๊ฐ€ ์žˆ์œผ๋ฉด ํ•ด๋‹น ๋ถ€๋ถ„์˜ ์ฃผ์„์„ ์‚ญ์ œํ•œ๋‹ค. ์ฃผ์„์ด๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ๋ฒˆ์—ญ๋˜์ง€ ์•Š์•„ ์›์ธ์„ ์ฐพ๋Š”๋ฐ ์‹œ๊ฐ„์„ ๋งŽ์ด ์†Œ๋น„ํ•  ์ˆ˜ ์žˆ๋‹ค. MacOS์—์„œ gettext ์„ค์น˜ ์•„๋ž˜์™€ ๊ฐ™์ด gettext ํ”„๋กœ๊ทธ๋žจ์ด ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ด๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค. $ python manage.py makemessages CommandError: Can't find xgettext. Make sure you have GNU gettext tools 0.15 or newer installed. ์•„๋ž˜์™€ ๊ฐ™์ด brew๋กœ gettext๋ฅผ ์„ค์น˜ํ•œ๋‹ค. $ brew install gettext $ brew link gettext --force ๋‹จ์ˆœํžˆ ์„ค์น˜๋งŒ ํ•˜๊ณ  ๋งํฌํ•˜์ง€ ์•Š์œผ๋ฉด ๋ช…๋ นํ–‰์—์„œ ์‹คํ–‰ํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค. 02) ์ง€์—ญ ์‹œ๊ฐ 10. ์„œ๋“œํŒŒํ‹ฐ ํŒจํ‚ค์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ 01) ํƒœ๊ทธ ํŒจํ‚ค์ง€ ์„ ํƒ django-taggit ์„ค์น˜ django-taggit ์•ฑ ๋“ฑ๋ก ์ปค์Šคํ…€ ํƒœ๊ทธ ๋ชจ๋ธ ์ •์˜ ํƒœ๊ทธ ์ž…๋ ฅ ๊ทœ์น™ ์ฃผ์š” ์‚ฌํ•ญ ์ •๋ฆฌ django-tagging ์„ค์น˜ django-tagging ์•ฑ ๋“ฑ๋ก ๋ชจ๋ธ์— Tag ํ•„๋“œ ์ถ”๊ฐ€ ํƒœ๊ทธ ์ž…๋ ฅ ๊ทœ์น™ ํŒจํ‚ค์ง€ ์„ ํƒ Django ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋น„๊ต ์‚ฌ์ดํŠธ์—์„œ ๋ณด๋ฉด ๋Œ€ํ‘œ์ ์œผ๋กœ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒจํ‚ค์ง€๋Š” django-tagging๊ณผ django-taggit์ด ์žˆ๋‹ค. django-tagging django-taggit django-tagging์€ brosner๊ฐ€ ๊ฐœ๋ฐœํ•˜๋˜ ๊ฒƒ์€ 2010๋…„ ์ดํ›„ ๋” ์ด์ƒ ๊ฐœ๋ฐœ, ์œ ์ง€ ๋ณด์ˆ˜๋˜์ง€ ์•Š๊ณ  ์žˆ๊ณ  Fantomas42๊ฐ€ ํ˜„์žฌ ์ด์–ด์„œ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ๋‹ค. django-taggit ํŒจํ‚ค์ง€๋Š” Django ์ฝ”์–ด ๊ฐœ๋ฐœ์ž๋„ ์ฐธ์—ฌํ•˜๊ณ  ์žˆ๋Š” ํŠน์ง•์ด ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ django-taggit์˜ ๊ฒฝ์šฐ tag cloud ๊ธฐ๋Šฅ ๋“ฑ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” django-taggit-templatetags ๋ณ„๋„์˜ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค. django-taggit ์„ค์น˜ pip install django-taggit pip install django-taggit-templatetags django-taggit ์•ฑ ๋“ฑ๋ก INSTALLED_APPS ๋ณ€์ˆ˜์— 'taggit', ์ถ”๊ฐ€ํ•ด django-taggit ํŒจํ‚ค์ง€๋ฅผ ๋“ฑ๋ก ์„ค์น˜ํ•œ๋‹ค. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'taggit', 'blog.apps.BlogConfig', ] ์ปค์Šคํ…€ ํƒœ๊ทธ ์Šฌ๋Ÿฌ๊ทธ๋ฅผ ๋งŒ๋“ค ๋•Œ ๊ธฐ๋ณธ๊ฐ’์€ unidecode ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ํƒœ๊ทธ๋กœ ๋‚˜๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์˜๋ฌธ์œผ๋กœ ๋ณ€ํ™˜ํ•œ na๊ฐ€ ์Šฌ๋Ÿฌ๊ทธ๊ฐ€ ๋œ๋‹ค. ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด๋กœ ์Šฌ๋Ÿฌ๊ทธ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด ์ง์ ‘ ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๋ชจ๋ธ ์ •์˜ models.py ํŒŒ์ผ from taggit.managers import TaggableManager from taggit.models import ( TagBase, TaggedItemBase ) class PostTag(TagBase): # NOTE: django-taggit does not allow unicode by default. slug = models.SlugField( verbose_name=_('slug'), unique=True, max_length=100, allow_unicode=True, ) class Meta: verbose_name = _("tag") verbose_name_plural = _("tags") def slugify(self, tag, i=None): return default_slugify(tag, allow_unicode=True) class TaggedPost(TaggedItemBase): content_object = models.ForeignKey( 'Post', on_delete=models.CASCADE, ) tag = models.ForeignKey( 'PostTag', related_name="%(app_label) s_%(class) s_items", on_delete=models.CASCADE, ) class Meta: verbose_name = _("tagged post") verbose_name_plural = _("tagged posts") class Post(models.Model): โ€ฆ ์ƒ๋žต โ€ฆ # ์ถ”๊ฐ€ tags = TaggableManager( verbose_name=_('tags'), help_text=_('A comma-separated list of tags.'), blank=True, through=TaggedPost, ) ํƒœ๊ทธ ์ž…๋ ฅ ๊ทœ์น™ ํƒœ๊ทธ 1, ํƒœ๊ทธ 2, ํƒœ๊ทธ 3 ํƒœ๊ทธ 4, ์œ„์™€ ๊ฐ™์ด ํƒœ๊ทธ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ ํƒœ๊ทธ๋Š” ์ด 3๊ฐœ์ด๊ณ  ์ €์žฅ ํ›„ ๊ด€๋ฆฌ์ž ํ™”๋ฉด์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธ๋œ๋‹ค. "ํƒœ๊ทธ 3 ํƒœ๊ทธ 4", ํƒœ๊ทธ 1, ํƒœ๊ทธ 2 ์ •๋ ฌ ์ˆœ์„œ๋Š” ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌถ์–ด ํ‘œ์‹œ๋œ๋‹ค. ๋‹จ, ๋์— ,(์ฝค๋งˆ)๋Š” ์•Œ์•„์„œ ๋ฒ„๋ ค์ง„๋‹ค. ์ฃผ์š” ์‚ฌํ•ญ ์ •๋ฆฌ ๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋‘ ๊ธฐ์กด์˜ ํƒœ๊ทธ๋ฅผ ์‚ญ์ œํ•˜๋ฉด ๊ด€๊ณ„๋Š” ์‚ญ์ œ๋˜์ง€๋งŒ ํƒœ๊ทธ ํ•ญ๋ชฉ ์ž์ฒด๊ฐ€ ์‚ญ์ œ๋˜์ง„ ์•Š๋Š”๋‹ค. django-tagging ์„ค์น˜ pip install django-tagging django-tagging ์•ฑ ๋“ฑ๋ก INSTALLED_APPS ๋ณ€์ˆ˜์— 'tagging.apps.TaggingConfig', ์ถ”๊ฐ€ํ•ด django-tagging ํŒจํ‚ค์ง€๋ฅผ ๋“ฑ๋ก ์„ค์น˜ํ•œ๋‹ค. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'tagging.apps.TaggingConfig', 'blog.apps.BlogConfig', ] ๋ชจ๋ธ์— Tag ํ•„๋“œ ์ถ”๊ฐ€ class Post(models.Model): title = models.CharField('Title', max_length=50) slug = models.SlugField('Slug', unique=False, allow_unicode=True, help_text='dashed words for title alias') description = models.CharField('Description', max_length=100, blank=True, help_text='Simple description text') content = models.TextField('Content') published = models.DateTimeField('Date published', default=timezone.now) created = models.DateTimeField('Date created', auto_now_add=True) updated = models.DateTimeField('Date updated', auto_now=True) tag = TagField() # ์ถ”๊ฐ€ ํƒœ๊ทธ ์ž…๋ ฅ ๊ทœ์น™ ํƒœ๊ทธ ์ž…๋ ฅ ๋ฌธ์ž์—ด ํƒœ๊ทธ ๊ฒฐ๊ณผ ์„ค๋ช… apple ball cat [apple], [ball], [cat] ์ฝค๋งˆ๊ฐ€ ์—†์œผ๋ฉด ๋„์–ด์“ฐ๊ธฐ๋กœ ๋ถ„๋ฆฌ apple, ball cat [apple], [ball cat] ์ฝค๋งˆ๊ฐ€ ์žˆ์œผ๋ฉด ์ฝค๋งˆ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ๋„์–ด์“ฐ๊ธฐ ํฌํ•จ ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ๊ฐ„์ฃผ "apple, ball" cat dog [apple, ball], [cat], [dog] ๋ชจ๋“  ์ฝค๋งˆ๋ฅผ ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ๋ฉด ๋„์–ด์“ฐ๊ธฐ๋กœ ๋ถ„๋ฆฌ "apple, ball", cat dog [apple, ball], [cat dog] ๋”ฐ์˜ดํ‘œ๋กœ ์•ˆ ๊ฐ์‹ผ ์ฝค๋งˆ๊ฐ€ ์žˆ์œผ๋ฉด ์ฝค๋งˆ๋กœ ๋ถ„๋ฆฌ apple "ball cat" dog [apple], [ball cat], [dog] ์ฝค๋งˆ๊ฐ€ ์—†์œผ๋ฉด ๋„์–ด์“ฐ๊ธฐ๋กœ ๋ถ„๋ฆฌ "apple" "ball dog [apple], [ball], [dog] ์•ˆ ๋‹ซํžŒ ๋”ฐ์˜ดํ‘œ๋Š” ๋ฌด์‹œ ํƒœ๊ทธ 1, ํƒœ๊ทธ 2, ํƒœ๊ทธ 3 ํƒœ๊ทธ 4, ์œ„์™€ ๊ฐ™์ด ํƒœ๊ทธ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ ํƒœ๊ทธ๋Š” ์ด 3๊ฐœ์ด๋‹ค. ํƒœ๊ทธ 1 ํƒœ๊ทธ 2 ํƒœ๊ทธ 3 ํƒœ๊ทธ 4 ๊ด€๋ฆฌ์ž ํ™”๋ฉด์—์„œ๋Š” ๋งจ ๋์— ,(์ฝค๋งˆ)๋ฅผ ๋ถ™์—ฌ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ ๋ฒ„๋ ค์ง€์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ์ž…๋ ฅ๋œ๋‹ค. 02) ๋Œ“๊ธ€ ํŒจํ‚ค์ง€ ์„ ํƒ django-discuss ์„ค์น˜ ์„ค์ • ํ…œํ”Œ๋ฆฟ django-comments-xtd ์„ค์น˜ ์„ค์ • ํŒจํ‚ค์ง€ ์„ ํƒ ์˜ˆ์ „์—๋Š” Django ์•ˆ์— django-contrib-comments ํŒจํ‚ค์ง€๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํ•จ๋˜์–ด ๋ฐฐํฌ๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋” ์ด์ƒ Django ๊ณต์‹ ํŒ€์—์„œ ํ•ด๋‹น ํŒจํ‚ค์ง€๋ฅผ ๊ด€๋ฆฌํ•˜์ง€ ์•Š๊ณ  ๋ถ„๋ฆฌ๋˜์–ด ๊ด€๋ฆฌ๋œ๋‹ค. django-disqus django-comments-xtd django-disqus๋Š” ํŽ˜์ด์Šค๋ถ, ํŠธ์œ„ํ„ฐ, ๊ตฌ๊ธ€ ๊ณ„์ • ๋“ฑ์„ ์ด์šฉํ•ด ๋กœ๊ทธ์ธํ•ด ๋Œ“๊ธ€์„ ๋‚จ๊ธธ ์ˆ˜ ์žˆ๋Š” ์‹œ์Šคํ…œ์ด๋‹ค. ์‚ฌ์šฉ๋ฒ•์ด ๋งค์šฐ ์‰ฌ์šด ํŽธ์ด๋‹ค. django-comments-xtd๋Š” django-contrib-comments ํŒจํ‚ค์ง€๋ฅผ ์ƒ์† ํ™•์žฅํ•ด์„œ ์Šค๋ ˆ๋“œ(๋Œ“๊ธ€์— ๋Œ“๊ธ€์„ ๋‹ฌ ์ˆ˜ ์žˆ๋Š”) ๋Œ“๊ธ€ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์„ ๋‹ด๊ณ  ์žˆ๋‹ค. django-discuss ์„ค์น˜ pip์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด django-disqus ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ๋‹ค. pip install django-disqus ์„ค์ • settings.py ํŒŒ์ผ์—์„œ django-disqus์™€ django.contrib.sites ์•ฑ์„ ๋“ฑ๋กํ•˜๊ณ  SITE_ID ๋ฐ DISQUS_WEBSITE_SHORTNAME ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # ์ถ”๊ฐ€ ์‚ฌํ•ญ 'django.contrib.sites', 'disqus', ] # Disqus SITE_ID = 1 DISQUS_WEBSITE_SHORTNAME = 'your_website_shortname' DISQUS_WEBSITE_SHORTNAME ๊ฐ’์€ disqus ์‚ฌ์ดํŠธ์— ํšŒ์› ๊ฐ€์ž…ํ•ด ํ• ๋‹น๋ฐ›๋Š”๋‹ค. ํ…œํ”Œ๋ฆฟ ์ƒ์„ธ ๋ณด๊ธฐ ๋ทฐ(DetailView) ํ•˜๋‹จ์— ๋Œ“๊ธ€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. <!-- Disqus Comments --> {% load disqus_tags %} {% disqus_show_comments %} django-comments-xtd ์„ค์น˜ pip์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด django-comments-xtd ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ๋‹ค. pip install django-comments-std ์„ค์ • settings.py ํŒŒ์ผ์—์„œ ์•ฑ์„ ๋“ฑ๋กํ•˜๋Š”๋ฐ ์ด๋•Œ ๋ฐ˜๋“œ์‹œ django_comments_xtd๊ฐ€ django_comments ๋ณด๋‹ค ์œ„์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. INSTALLED_APPS += [ 'django.contrib.sites', 'django_comments_xtd', 'django_comments', ] settings.py ํŒŒ์ผ์— ๋‹ค์Œ ์„ค์ • ๋ณ€์ˆ˜๋ฅผ ๋“ฑ๋กํ•œ๋‹ค. COMMENTS_APP = 'django_comments_xtd' COMMENTS_XTD_MAX_THREAD_LEVEL = 1 ๋Œ“๊ธ€ ์•ฑ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์•ฑ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋Š”๋ฐ ์ด๋Š” ์• ์ดˆ์— django-contrib-comment ์•ฑ์—์„œ ์ปค์Šคํ…€ ๋ชจ๋ธ์„ ์ง€์ •ํ•  ๋•Œ ์“ฐ๋Š” ์†์„ฑ์ด๋‹ค. URL ํŒจํ„ด์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋“ฑ๋กํ•œ๋‹ค. urlpatterns = [ ... ์ƒ๋žต ... url(r'^comments/', include('django_comments_xtd.urls')), ... ์ƒ๋žต ... ] ๋กœ๊ทธ์ธํ•ด ๊ด€๋ฆฌ์ž ํ™”๋ฉด์—์„œ ์‚ฌ์ดํŠธ๋“ค์— ๊ธฐ๋ณธ ์‚ฌ์ดํŠธ์— ๋„๋ฉ”์ธ ์ •๋ณด๋ฅผ example.com์ด ์•„๋‹ˆ๋ผ 127.0.0.1:8000์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ๋ฌผ๋ก  ์šด์˜ํ™˜๊ฒฝ์—์„œ๋Š” ํ•ด๋‹น ์‚ฌ์ดํŠธ์˜ ๋„๋ฉ”์ธ์„ ์ž…๋ ฅํ•œ๋‹ค. 03) ๋ชจ๋ธ ์œ ํ‹ธ๋ฆฌํ‹ฐ django-model-utils ์„ค์น˜ ํ•„๋“œ StatusField MonitorField ๋ชจ๋ธ ๋ชจ๋ธ ๋งค๋‹ˆ์ € InheritanceManager ๊ธฐํƒ€ ์œ ํ‹ธ๋ฆฌํ‹ฐ Django์˜ ๋ชจ๋ธ ์„ ์–ธ์— ์ž์ฃผ ์“ฐ์ด๋Š” ๋ฏน์Šค ์ธ๊ณผ ์œ ํ‹ธ๋ฆฌํ‹ฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. django-model-utils ์„ค์น˜ ์•„๋ž˜์™€ ๊ฐ™์ด PIP ๋ช…๋ น์–ด๋กœ ์‰ฝ๊ฒŒ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๊ณ  ๊ตณ์ด settings.py ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š์•„๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. pip install django-model-utils ํ•„๋“œ StatusField ๋ ˆ์ฝ”๋“œ์˜ ์ƒํƒœ๋ฅผ ์ง€์ •ํ•˜๋Š” ํ•„๋“œ์ด๋‹ค. from model_utils.fields import StatusField from model_utils import Choices class Article(models.Model): STATUS_CHOICES = Choices('draft', 'published') # ... status = StatusField(choices_name='STATUS_CHOICES') StatusField๋Š” db_index=True ์„ค์ •์ด ๊ธฐ๋ณธ๊ฐ’์ด ์•„๋‹ˆ๋‹ค. ์ƒํƒœ ํ•„๋“œ๋กœ ์ž์ฃผ ํ•„ํ„ฐ๋ง์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์ง์ ‘ ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. MonitorField ์–ด๋–ค ํ•„๋“œ์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ๋ณ€๊ฒฝ ์‹œ๊ฐ์„ ์ €์žฅํ•˜๋Š” ํ•„๋“œ์ด๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด when ์˜ต์…˜์„ ์ฃผ๋ฉด ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๋Š” ํ•„๋“œ๊ฐ€ ํŠน์ • ๊ฐ’์ผ ๋•Œ์—๋งŒ ์—…๋ฐ์ดํŠธํ•˜๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค. from model_utils.fields import MonitorField, StatusField class Article(models.Model): STATUS = Choices('draft', 'published') status = StatusField() published_at = MonitorField(monitor='status', when=['published']) ๋ชจ๋ธ ๋ชจ๋ธ ๋งค๋‹ˆ์ € InheritanceManager InheritanceManager(์ƒ์† ๋งค๋‹ˆ์ €)๋Š” ์ƒ์†๊ณผ ๋‹คํ˜•์„ฑ์„ ์ง€์›ํ•˜๋Š” ๋งค๋‹ˆ์ €์ด๋‹ค. ํ˜„์žฌ Django 2.0์—์„œ๋Š” _clone() ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์ด _chain()์œผ๋กœ ๋ณ€๊ฒฝ๋˜์–ด ์ด์— ๋Œ€ํ•œ ํŒจ์น˜๊ฐ€ ๋˜๊ธฐ ์ „๊นŒ์ง€ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. Place ๋ชจ๋ธ์„ ์ƒ์†ํ•˜๋Š” Restaurant, Bar ๋ชจ๋ธ์„ ์ •์˜ํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ ์‹ค์ œ Restaurant ๋˜๋Š” Bar์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ Place ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. nearby_places = Place.objects.filter(location='here') ์ด๋Ÿฌํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•ด Place ๋ชจ๋ธ์— InheritanceManager๋ฅผ ๋ถ™์—ฌ์„œ select_subclasses() ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. from model_utils.managers import InheritanceManager class Place(models.Model): # ... objects = InheritanceManager() class Restaurant(Place): # ... class Bar(Place): # ... nearby_places = Place.objects.filter(location='here').select_subclasses() for place in nearby_places: # "place" will automatically be an instance of Place, Restaurant, or Bar ๊ฐ ์„œ๋ธŒ ํด๋ž˜์Šค๋ฅผ ์กฐ์ธํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ถ€๊ฐ€์  ์ฟผ๋ฆฌ๊ฐ€ ๋งŽ์ด ์‹คํ–‰๋˜๋ฏ€๋กœ ์กฐ์ธ ํšŸ์ˆ˜๋ฅผ ์ค„์ด๊ณ  ํŠน์ • ํ•˜์œ„ ํด๋ž˜์Šค๋งŒ ํ•„ํ„ฐ๋งํ•˜๊ธฐ ์œ„ํ•ด ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ ๋„˜๊ฒจ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. nearby_places = Place.objects.select_subclasses("restaurant") # restaurants will be Restaurant instances, bars will still be Place instances nearby_places = Place.objects.select_subclasses("restaurant", "bar") # all Places will be converted to Restaurant and Bar instances. ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ ๋„˜๊ฒจ์ฃผ๋Š” ๋Œ€์‹  ๊ทธ๋ƒฅ ํ•˜์œ„ ํด๋ž˜์Šค ์ž์ฒด๋ฅผ ๋„˜๊ฒจ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. nearby_places = Place.objects.select_subclasses(Restaurant) # restaurants will be Restaurant instances, bars will still be Place instances nearby_places = Place.objects.select_subclasses(Restaurant, Bar) # all Places will be converted to Restaurant and Bar instances. ๋ณ„๋กœ ๊ถŒ์žฅํ•˜๊ณ  ์‹ถ์ง€ ์•Š์ง€๋งŒ ์ด ๋‘˜์„ ์„ž์–ด ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค. nearby_places = Place.objects.select_subclasses(Restaurant, "bar") # all Places will be converted to Restaurant and Bar instances. InheritanceManager๋Š” ๋˜ํ•œ get() ๋ฉ”์„œ๋“œ ๋Œ€์‹  get_subclass() ๋ฉ”์„œ๋“œ๋กœ ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค. place = Place.objects.get_subclass(id=some_id) # "place" will automatically be an instance of Place, Restaurant, or Bar select_subclasses() ๋˜๋Š” get_subclass() ๋ฉ”์„œ๋“œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด InheritanceManager๋Š” ๋””ํดํŠธ ๋งค๋‹ˆ์ €์™€ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ธฐ๋ณธ ๋งค๋‹ˆ์ € ๋Œ€์‹ ์— ์‚ฌ์šฉํ•ด๋„ ์ถฉ๋Œ์€ ์—†๋‹ค. ๊ธฐํƒ€ ์œ ํ‹ธ๋ฆฌํ‹ฐ 04) ๊ณ„์ธต ํŠธ๋ฆฌ ๊ตฌ์กฐ django-mptt ์žฅ๋‹จ์  ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ ์•ฑ ๋“ฑ๋ก ๊ฒŒ์‹œ๋ฌผ๊ณผ ์นดํ…Œ๊ณ ๋ฆฌ ๋ชจ๋ธ ๊ด€๋ฆฌ์ž ๋“ฑ๋ก ์ฃผ์š” ์ฟผ๋ฆฌ ์ „์ฒด ์ฟผ๋ฆฌ์…‹ ๊ตฌํ•˜๊ธฐ ์ฟผ๋ฆฌ์…‹ ์กฐ์ธํ•˜์—ฌ ๊ฒฐ๊ณผ ์–ป๊ธฐ breadcrumbs ๊ตฌํ˜„ ๊ธฐํƒ€ ์˜ˆ์‹œ ํ…œํ”Œ๋ฆฟ recursetree django-treebeard ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ ์•ฑ ๋“ฑ๋ก ์นดํ…Œ๊ณ ๋ฆฌ ๋ชจ๋ธ ๊ด€๋ฆฌ์ž ๋“ฑ๋ก ์ฃผ์š” ์ฟผ๋ฆฌ ๊ณ„์ธต ํŠธ๋ฆฌ ๊ตฌ์กฐ ๊ตฌํ˜„์„ ์œ„ํ•ด ๋Œ€ํ‘œ์ ์œผ๋กœ django-treebeard, django-mptt ๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์กด์žฌํ•œ๋‹ค. ๊ฐ€์žฅ ๋งŽ์€ ์‚ฌ์šฉ์ž๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒƒ์€ django-mptt์ด๊ณ  ๊ทผ๋ž˜์— django-cms๋‚˜ wagtail ๊ฐ™์€ CMS ์„ค๋ฃจ์…˜์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ django-treebeard์ด๋‹ค. django-mptt ์žฅ๋‹จ์  ์ฟผ๋ฆฌ๋ฅผ ์ตœ์†Œํ™”ํ•˜์—ฌ ๋น ๋ฅธ ์งˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์žฅ์ ์ด๋‹ค. ๋ฐ˜๋ฉด์— ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ณ„์† ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๋Š” ์ƒ๋Œ€์ ์œผ๋กœ ๋Š๋ฆฐ ๊ฒŒ ๋‹จ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ดˆ๊ธฐ์— ํ•œ ๋ฒˆ ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์กฐํ™” ํ•ด๋†“๊ณ  ์ž์ฃผ ๋ณ€๊ฒฝ ์—†์ด ๊ทธ๋ƒฅ ์งˆ์˜๋งŒ ํ•˜์—ฌ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๋งค์šฐ ํ›Œ๋ฅญํ•˜๊ณ  ๋งค์šฐ ํฐ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ง€์†์ ์œผ๋กœ ์ž์ฃผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ๋•Œ๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ ์•ฑ ๋“ฑ๋ก ํŒจํ‚ค์ง€๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด pip์œผ๋กœ ์„ค์น˜ํ•œ๋‹ค. pip install django-mptt ์„ค์น˜ํ•œ ์•ฑ์€ settings.py ํŒŒ์ผ์— ๋“ฑ๋กํ•œ๋‹ค. INSTALLED_APPS += [ ... ์„ค์น˜ํ•œ ์•ฑ ..., 'mptt', ] ๊ฒŒ์‹œ๋ฌผ๊ณผ ์นดํ…Œ๊ณ ๋ฆฌ ๋ชจ๋ธ from mptt.fields import TreeForeignKey from mptt.models import MPTTModel class Post(models.Model): title = models.CharField( verbose_name=_('title'), max_length=250 ) slug = models.SlugField( verbose_name=_('slug'), max_length=250, unique=False, allow_unicode=True ) category = TreeForeignKey( 'Category', verbose_name=_('category'), null=True, blank=True, db_index=True, on_delete=models.SET_NULL, ) ... ์ƒ๋žต ... class Category(MPTTModel): parent = TreeForeignKey( 'self', verbose_name=_('parent'), blank=True, null=True, related_name='children', db_index=True ) title = models.CharField( verbose_name=_('title'), max_length=128 ) slug = models.SlugField( verbose_name=_('slug'), max_length=250, unique=True, allow_unicode=True ) class Meta: ordering = ['tree_id', 'lft'] class MPTTMeta: order_insertion_by = ['title'] def __str__(self): return self.title order_insertion_by ํ•„๋“œ ์„ค์ •์€ ํŠธ๋ฆฌ์— ์ƒˆ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ์ˆœ์„œ๋ฅผ ์ •์˜ํ•˜๋Š” ํ•„๋“œ ์ด๋ฆ„์˜ ๋ฆฌ์ŠคํŠธ์ด๋‹ค. ์ด ๋ฆฌ์ŠคํŠธ์˜ ๊ธฐ๋ณธ๊ฐ’์€ [] ๋นˆ ๋ฆฌ์ŠคํŠธ์ด๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ NOT NULL ํ•„๋“œ์ด๋ฉด ์–ด๋Š ํ•„๋“œ๋‚˜ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ  ์ˆœ์„œ์— ์žˆ์–ด ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํ•„๋“œ ์ด๋ฆ„์„ ๊ฐ€์žฅ ๋จผ์ € ๋†“๋Š”๋‹ค. ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•  ๋•Œ ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€์ ์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•œ๋‹ค. ์ด ์˜ต์…˜์€ ์นดํ…Œ๊ณ ๋ฆฌ ํŠธ๋ฆฌ์ฒ˜๋Ÿผ ํ•ญ์ƒ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ์ •๋ ฌ๋˜์–ด์•ผ ํ•˜๋Š” ๊ฑฐ์˜ ์ •์ ์ธ(๋ณ€๊ฒฝ์ด ๋ณ„๋กœ ์—†๋Š”) ๊ตฌ์กฐ๋ฅผ ์œ ์ง€ํ•  ๋•Œ ์œ ๋ฆฌํ•˜๋‹ค. ๊ด€๋ฆฌ์ž ๋“ฑ๋ก from mptt.admin import DraggableMPTTAdmin from .models import ( Post, Category ) class CategoryAdmin(DraggableMPTTAdmin): list_display = ( 'tree_actions', 'indented_title', 'title', 'slug', ) prepopulated_fields = {'slug': ('title',)} mptt_level_indent = 20 admin.site.register(Category, CategoryAdmin) ๋“œ๋ž˜๊ทธ ์•ค ๋“œ๋กญ์œผ๋กœ ํŠธ๋ฆฌ ๊ตฌ์กฐ ์œ„์น˜๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก DraggableMPTTAdmin ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ด ์–ด๋“œ๋ฏผ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ์ธํ„ฐ๋„ท ์ต์Šคํ”Œ๋กœ๋Ÿฌ์˜ ๊ฒฝ์šฐ 9 ๋ฒ„์ „ ์ด์ƒ์—์„œ ๋™์ž‘ํ•œ๋‹ค. ๋…ธ๋“œ ์ˆ˜๊ฐ€ ๋ช‡ ๋ฐฑ ๊ฐœ๋ฅผ ๋„˜์–ด๊ฐ€๊ฑฐ๋‚˜ ๊นŠ์ด๊ฐ€ 10๋‹จ๊ณ„ ์ด์ƒ์ธ ํฐ ํŠธ๋ฆฌ๋Š” ์ž˜ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ง€์—ฐ๋œ ๋กœ๋”ฉ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•œ ํŒจ์น˜๊ฐ€ ํ•„์š”ํ•œ ์‹ค์ •์ด๋‹ค. list_per_page๋Š” ๊ธฐ๋ณธ๊ฐ’์ด 2000์œผ๋กœ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—๋Š” ํŽ˜์ด์ง•์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฃผ์š” ์ฟผ๋ฆฌ ์ „์ฒด ์ฟผ๋ฆฌ์…‹ ๊ตฌํ•˜๊ธฐ context['category_tree'] = Category.objects.all() ์ฟผ๋ฆฌ์…‹ ์กฐ์ธํ•˜์—ฌ ๊ฒฐ๊ณผ ์–ป๊ธฐ Post.objects.published() \ .select_related('author') \ .select_related('category') \ .filter( category__in=Category.objects.filter( slug=self.kwargs['slug'] ).get_descendants(include_self=True) ) breadcrumbs ๊ตฌํ˜„ ์ž์‹ ์˜ ๋…ธ๋“œ๋ฅผ ํฌํ•จํ•ด์„œ ๋ถ€๋ชจ ๋…ธ๋“œ ์ „์ฒด ๊ตฌ์กฐ๋ฅผ ๊ตฌํ•œ๋‹ค. MenuItem.objects.get(pk=11).get_ancestors(include_self=True) ๊ธฐํƒ€ ์˜ˆ์‹œ Post.objects.filter(category__in=Category.objects.get(pk=2).get_descendants(include_self=True)) Post.objects.filter(category__in=Category.objects.get(title='Django').get_descendants(include_self=True)) MenuItem.objects.get(pk=1).get_leafnodes() MenuItem.objects.get(pk=3).get_ancestors() MenuItem.objects.get(pk=1).is_root_node() # 1, 2 ๋ฃจํŠธ ๋…ธ๋“œ ํ…œํ”Œ๋ฆฟ recursetree {% load mptt_tags %} {% recursetree category_tree %} {% if not node.is_leaf_node %} <li> <a href="{% url 'blog:post-category' node.slug %}">{% trans node.title %}</a> <ul> {{ children }} </ul> </li> {% else %} <li> <a href="{% url 'blog:post-category' node.slug %}">{% trans node.title %}</a> </li> {% endif %} {% endrecursetree %} django-treebeard ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ ์•ฑ ๋“ฑ๋ก ํŒจํ‚ค์ง€๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด pip์œผ๋กœ ์„ค์น˜ํ•œ๋‹ค. pip install django-treebeard ์„ค์น˜ํ•œ ์•ฑ์€ settings.py ํŒŒ์ผ์— ๋“ฑ๋กํ•œ๋‹ค. INSTALLED_APPS += [ ... ์„ค์น˜ํ•œ ์•ฑ ..., 'treebeard', ] TreeAdmin ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋ ค๋ฉด treebeard์˜ ํ…œํ”Œ๋ฆฟ ๊ฒฝ๋กœ๋ฅผ TEMPLATE_DIRS ๊ฒฝ๋กœ์— ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ TEMPLATE_CONTEXT_PROCESSORS ์„ค์ •์— django.core.context_processors.request์„ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•œ๋‹ค. ์นดํ…Œ๊ณ ๋ฆฌ ๋ชจ๋ธ from django.db import models from treebeard.mp_tree import MP_Node class Category(MP_Node): name = models.CharField(max_length=30) #node_order_by = ['name'] def __str__(self): return self.name node_order_by ์˜ต์…˜์€ ๋ฆฌ์ŠคํŠธ๋กœ ๋‚˜์—ดํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋…ธ์ถœ ์‹œ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ๋ฌด์กฐ๊ฑด ๋”ฐ๋ฅธ๋‹ค. ๊ด€๋ฆฌ์ž ๋“ฑ๋ก from django.contrib import admin from treebeard.admin import TreeAdmin from treebeard.forms import movenodeform_factory from .models import Category class CategoryAdmin(TreeAdmin): form = movenodeform_factory(Category) admin.site.register(Category, CategoryAdmin) ์ฃผ์š” ์ฟผ๋ฆฌ 05) ์œ„์ง€์œ„๊ทธ ์—๋””ํ„ฐ ํŒจํ‚ค์ง€ ์„ ํƒ Summernote ์„ค์น˜ ์„ค์ • ์‚ฌ์šฉํ•˜๊ธฐ admin ํŽ˜์ด์ง€ ์„ค์ • ํผ ๋งŒ๋“ค๊ธฐ ์ถœ๋ ฅ ํ›„๊ธฐ SimpleMDE ํ›„๊ธฐ ํŒจํ‚ค์ง€ ์„ ํƒ Summernote SimpleMDE CKEditor TinyMCE Summernote ์„ค์น˜ pip ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•œ๋‹ค. pip install django-summernote ์„ค์ • settings.py ํŒŒ์ผ์— django-summernote ์•ฑ ๋“ฑ๋กํ•œ๋‹ค. INSTALLED_APPS += ('django_summernote', ) urls.py ํŒŒ์ผ์— django_summernote.urls์„ ๋“ฑ๋กํ•œ๋‹ค. urlpatterns = [ ... url(r'^summernote/', include('django_summernote.urls')), ... ] ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ์œ„ํ•œ MEDIA_URL ๊ฒฝ๋กœ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •ํ•œ๋‹ค. ์—๋””ํ„ฐ๋กœ ์—…๋กœ๋“œํ•œ ์ฒจ๋ถ€ํŒŒ์ผ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋„๋ก ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•œ๋‹ค. python manage.py migrate ์‚ฌ์šฉํ•˜๊ธฐ admin ํŽ˜์ด์ง€ ์„ค์ • Post ๋ชจ๋ธ์— TextField๋ฅผ ์ผ๊ด„์ ์œผ๋กœ Summernote๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. from django.contrib import admin from django.utils import timezone from django_summernote.admin import SummernoteModelAdmin from .models import Post class PostAdmin(SummernoteModelAdmin): ... ์ƒ๋žต ... admin.site.register(Post, PostAdmin) ํผ ๋งŒ๋“ค๊ธฐ ์ถœ๋ ฅ ์ €์žฅ๋œ ๋‚ด์šฉ์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•  ๋•Œ๋Š” safe ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. {{ post.content|safe }} ํ›„๊ธฐ ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ์ด๋ฏธ ๊ตฌํ˜„ํ•ด์„œ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŽธ๋ฆฌํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ฒŒ์‹œ๋ฌผ๋ณ„๋กœ ์ฒจ๋ถ€ํŒŒ์ผ ๊ด€๋ฆฌ๋ฅผ ํ•˜๋ ค๋ฉด ๋ณ„๋„๋กœ ๋‹ค์‹œ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. SimpleMDE ํ›„๊ธฐ admin ๊ด€๋ฆฌ์ž ํ™”๋ฉด์—์„œ ์‚ฌ์šฉํ•  ๋•Œ CSS๊ฐ€ ๊ฐ€๋กœ๊ฐ€ ์ „์ฒด๋ฅผ ์ฐจ์ง€ํ•˜๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค. 06) RSS ํ”ผ๋“œ Django ๊ณต์‹ ํŒจํ‚ค์ง€์—์„œ django.contrib.syndication.views.Feed ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์ด๋ฅผ ์ƒ์†ํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. Feed ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ํด๋ž˜์Šค์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค. class LatestPostFeed(Feed): title = _('Blog Feeds') link = '/feed' description = _('Blog Recent Posts') def items(self): return Post.objects.filter(status='published').order_by('-published')[:10] def item_title(self, item): return item.title def item_description(self, item): return item.description def item_link(self, item): return reverse('blog:post_detail', args=(item.pk, item.slug,)) Post ๋ชจ๋ธ์€ title, description ํ•„๋“œ๋ฅผ ๊ฐ–๊ณ  ์žˆ์œผ๋ฉฐ ์ ˆ๋Œ€ URL ๋งํฌ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” link ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. urls.py ํŒŒ์ผ์—์„œ URL ํŒจํ„ด์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. urlpatterns = [ url(r'^feed/$', LatestPostFeed(), name='post_feed'), ] 07) ์‚ฌ์ดํŠธ ํ”„๋ ˆ์ž„์›Œํฌ ์‚ฌ์ดํŠธ ํ”„๋ ˆ์ž„์›Œํฌ ๋“ฑ๋ก settings.py ํŒŒ์ผ ์ˆ˜์ • django_site ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์‚ฌ์ดํŠธ ๋„๋ฉ”์ธ๊ณผ ์ด๋ฆ„ ๋ณ€๊ฒฝ Django ๊ณต์‹ ํŒจํ‚ค์ง€์—์„œ django.contrib.sites ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ฌ์ดํŠธ ๊ธฐ๋ณธ ์ •๋ณด๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋ณต์ˆ˜ ์‚ฌ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์ดํŠธ ํ”„๋ ˆ์ž„์›Œํฌ ๋“ฑ๋ก settings.py ํŒŒ์ผ ์ˆ˜์ • settings.py ํŒŒ์ผ์— django.contrib.sites ์•ฑ์„ ๋“ฑ๋ก ์„ค์น˜ํ•œ๋‹ค. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # Additional Django apps 'django.contrib.sites', ] SITE_ID ์ •์ˆซ๊ฐ’์€ django_site ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ํ˜„์žฌ ์‚ฌ์ดํŠธ์˜ ๊ฐ’์ด๋‹ค. django_site ํ…Œ์ด๋ธ”์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‚ฌ์ดํŠธ๋ฅผ ๋“ฑ๋กํ•˜๊ณ  ๊ทธ์ค‘ ํ•˜๋‚˜๋ฅผ ์ง€์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. SITE_IDE = 1 django_site ํ…Œ์ด๋ธ” ์ƒ์„ฑ django_site ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ makemigrations ํ•„์š” ์—†์ด ๋ฐ”๋กœ migrate ๋ช…๋ นํ•œ๋‹ค. python manage.py migrate ์‚ฌ์ดํŠธ ๋„๋ฉ”์ธ๊ณผ ์ด๋ฆ„ ๋ณ€๊ฒฝ ์‚ฌ์ดํŠธ ๋„๋ฉ”์ธ๊ณผ ์ด๋ฆ„ ๊ธฐ๋ณธ๊ฐ’์ด example.com์ด๋‹ค. ์ด๋ฅผ ํŒŒ์ด์ฌ ์ฝ˜์†”์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. from django.contrib.sites.models import Site mysite = Site.objects.get_current() mysite.domain = 'my-site.com' mysite.name = 'My site powered by Django' mysite.save() 08) ์ธ์ฆ ๋ฉ”์ผ ๊ฐ€์ž… ์ฒ˜๋ฆฌ django-registration ์„ค์น˜ ๋ฐ ์ค€๋น„ HMAC ํ™œ์„ฑํ™” ๋ฐฉ์‹ settings.py urls.py ํ…œํ”Œ๋ฆฟ django-registration ๋ณธ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์ด์œ ๋Š” ์ด๋ฉ”์ผ ์ธ์ฆ 2๋‹จ๊ณ„ ๊ฐ€์ž… ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. ๋ฌผ๋ก  1๋‹จ๊ณ„ ๊ฐ€์ž…๋„ ์ง€์›ํ•˜์ง€๋งŒ ์ด ๊ฒฝ์šฐ๋Š” ๊ตณ์ด ์ด ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. 2๋‹จ๊ณ„ ๊ฐ€์ž…์„ ์œ„ํ•ด HMAC ํ™œ์„ฑํ™” ๋ฐฉ์‹๊ณผ ๋ชจ๋ธ ๊ธฐ๋ฐ˜ ํ™œ์„ฑํ™” ๋ฐฉ์‹ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ HMAC ํ™œ์„ฑํ™” ๋ฐฉ์‹์„ ๊ถŒ์žฅํ•˜๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋Š” HMAC ํ™œ์„ฑํ™” ๋ฐฉ์‹๋งŒ ๋‹ค๋ฃฌ๋‹ค. ์„ค์น˜ ๋ฐ ์ค€๋น„ Django์˜ ๊ธฐ๋ณธ ์ธ์ฆ ์‹œ์Šคํ…œ์ธ django.contrib.auth์ด INSTALLED_APPS ๋ณ€์ˆ˜์— ์˜ฌ๋ฐ”๋กœ ๋“ฑ๋ก์ด ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. Django ์„ค์น˜ ๊ธฐ๋ณธ๊ฐ’์ด๋ฏ€๋กœ ํฌ๊ฒŒ ์‹ ๊ฒฝ ์“ธ ํ•„์š”๋Š” ์—†๋‹ค. pip ํŒจํ‚ค์ง€๋กœ ๊ฐ„๋‹จํžˆ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. pip install django-registration ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๊ณ  INSTALLED_APPS ๋ณ€์ˆ˜์— ์„ค์น˜ํ•œ ํŒจํ‚ค์ง€ ์•ฑ์„ registration์œผ๋กœ ๋“ฑ๋กํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ ์ด์œ ๋Š” ์•ฑ์—์„œ ๋ณ„๋„๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. HMAC ํ™œ์„ฑํ™” ๋ฐฉ์‹ settings.py ํšŒ์› ๊ฐ€์ž…์„ ๋ฐ›๊ณ  ํšŒ์› ๊ฐ€์ž… ์ด๋ฉ”์ผ ์ธ์ฆ์„ ๋งŒ ํ•˜๋ฃจ ์•ˆ์— ์™„๋ฃŒํ•˜๋„๋ก ์„ค์ •ํ•œ๋‹ค. ACCOUNT_ACTIVATION_DAYS = 1 REGISTRATION_OPEN = True # ๊ธฐ๋ณธ๊ฐ’ urls.py /accounts/register/, /accounts/login/ ๊ฐ™์€ URL ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก url ํŒจํ„ด์„ ์ถ”๊ฐ€ํ•œ๋‹ค. from django.conf.urls import include, url urlpatterns = [ ... ์ƒ๋žต ... url(r'^accounts/register/$', RegistrationView.as_view( form_class=WebUserCreationForm ), name='registration_register'), url(r'^accounts/', include('registration.backends.hmac.urls')), ... ์ƒ๋žต ... ] ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด URL ํŒจํ„ด๊ณผ ๋ทฐ๊ฐ€ ๋งคํ•‘๋œ๋‹ค. /accounts/register/ [name='registration_register'] /accounts/activate/complete/ [name='registration_activation_complete'] /accounts/activate/(?P[-:\w]+)/ [name='registration_activate'] /accounts/register/ [name='registration_register'] /accounts/register/complete/ [name='registration_complete'] /accounts/register/closed/ [name='registration_disallowed'] /accounts/login/ [name='auth_login'] /accounts/logout/ [name='auth_logout'] /accounts/password/change/ [name='auth_password_change'] /accounts/password/change/done/ [name='auth_password_change_done'] /accounts/password/reset/ [name='auth_password_reset'] /accounts/password/reset/confirm/(?P[0-9A-Za-z_-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/ [name='auth_password_reset_confirm'] /accounts/password/reset/complete/ [name='auth_password_reset_complete'] /accounts/password/reset/done/ [name='auth_password_reset_done'] ํ…œํ”Œ๋ฆฟ django-registration ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ…œํ”Œ๋ฆฟ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. registration/registration_form.html : ํšŒ์›๊ฐ€์ž… ํผ registration/registration_complete.html (TemplateView) : ํšŒ์›๊ฐ€์ž… ์™„๋ฃŒ ๋ฉ”์‹œ์ง€ registration/activate.html : ์ด๋ฉ”์ผ ์ธ์ฆ ์‹คํŒจ ๋ฉ”์‹œ์ง€ registration/activation_complete.html (TemplateView) : ์ด๋ฉ”์ผ ์ธ์ฆ ์™„๋ฃŒ ๋ฉ”์‹œ์ง€ registration/activation_email_subject.txt : ๋ฐœ์†ก ์ด๋ฉ”์ผ ์ œ๋ชฉ registration/activation_email.txt : ๋ฐœ์†ก ์ด๋ฉ”์ผ ๋ณธ๋ฌธ ๋‚ด์šฉ registration/registration_closed.html (TemplateView) : ํšŒ์›๊ฐ€์ž… ๋ถˆ๊ฐ€ ๋ฉ”์‹œ์ง€ django.contrib.auth ๊ธฐ๋ณธ๊ฐ’์„ ๋”ฐ๋ฅด๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ…œํ”Œ๋ฆฟ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. login.html logout.html password_change_form.html password_change_done.html password_reset_form.html password_reset_email.html password_reset_subject.txt password_reset_done.html password_reset_confirm.html password_reset_complete.html 09) OAuth ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ข…๋ฅ˜ django-allauth ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ ์ค€๋น„ ์ฃผ์š” ์„ค์ •๊ฐ’ ๊ณ„์ • ํ”„๋กœํ•„ ํ•„๋“œ ์ถ”๊ฐ€ ๊ตฌํ˜„ ์ผ๋Œ€์ผ ๊ด€๊ณ„ ํ”„๋กœํ•„ ๋ชจ๋ธ ์ •์˜ AbstractUser ํด๋ž˜์Šค์˜ ์ƒ์† SNS ์„ค์ • ํŽ˜์ด์Šค๋ถ social-auth-app-django ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ ์ค€๋น„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์„ค์ • settings.py urls.py SNS ์„ค์ • ํŽ˜์ด์Šค๋ถ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ข…๋ฅ˜ django-social-auth (deprecated) python-social-auth (deprecated) social-app-django (๋‹จ, ํŒจํ‚ค ์ง€๋ช…์€ social-auth-app-django) django-allauth ์ฆ‰, ํฌ๊ฒŒ social-app-django์™€ django-allauth ๋‘ ํŒจํ‚ค์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. ๋งŽ์€ ์ฐจ์ด๊ฐ€ ์žˆ์ง€๋งŒ ์‰ฝ๊ฒŒ ์ด์•ผ๊ธฐํ•˜๋ฉด social-app-django๋Š” django-registration๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ ์“ธ ์ˆ˜ ์žˆ๊ณ  all-in-one์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฒŒ django-allauth์ด๋‹ค. django-allauth์˜ ์žฅ์ ์€ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ์†Œ์…œ ๋กœ๊ทธ์ธ์„ ์ง€์›ํ•˜๊ณ  ํšŒ์›๊ฐ€์ž…์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. SNS ๊ณต๊ธ‰์ž(provider)๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ถ€์ •ํ™•ํ•œ ์ •๋ณด(์˜ˆ: ์ธ์ฆ๋ฐ›์ง€ ์•Š์€ ์ด๋ฉ”์ผ ์ฃผ์†Œ ๋“ฑ)๋ฅผ ๋กœ๊ทธ์ธ ์—ฐ๋™ ๊ณผ์ •์—์„œ ์ •ํ™•ํ•œ ์ž…๋ ฅ ์š”๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ, Django ์‹œ์Šคํ…œ ๊ธฐ์กด ์‚ฌ์šฉ์ž์˜ ๊ฒฝ์šฐ์—๋„ /accounts/social/connections/ ๊ฒฝ๋กœ์—์„œ ์†Œ์…œ ๋กœ๊ทธ์ธ ๊ณ„์ • ์—ฐ๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ remember me ๊ธฐ๋Šฅ ๋˜ํ•œ ์ง€์›ํ•œ๋‹ค. django-allauth ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ ์ค€๋น„ pip install django-allauth settings.py ํŒŒ์ผ INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', # <- ์˜์กด์„ฑ ์•ฑ 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', # <- ์˜์กด์„ฑ ์•ฑ 'allauth', # <- ์ถ”๊ฐ€ 'allauth.account', # <- ์ถ”๊ฐ€ 'allauth.socialaccount', # <- ์ถ”๊ฐ€ # 'allauth.socialaccount.providers.facebook', # <- ํ•„์š”ํ•œ ์†Œ์…œ ๋กœ๊ทธ์ธ ์ถ”๊ฐ€ AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', # <- ๋””ํดํŠธ ๋ชจ๋ธ ๋ฐฑ์—”๋“œ 'allauth.account.auth_backends.AuthenticationBackend', # <- ์ถ”๊ฐ€ ) SITE_ID = 1 # ์‚ฌ์ดํŠธ ์•„์ด๋”” ๊ธฐ๋ณธ๊ฐ’ urls.py ํŒŒ์ผ urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^accounts/', include('allauth.urls')), # <- ์ถ”๊ฐ€ ] ์ฃผ์š” ์„ค์ •๊ฐ’ ACCOUNT_AUTHENTICATION_METHOD: ๋กœ๊ทธ์ธ์ธ์ฆ ๋ฐฉ๋ฒ•์œผ๋กœ user name, email, user name_email์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. email๋กœ ์„ค์ •ํ•  ๋•Œ๋Š” ACCOUNT_EMAIL_REQUIRED = True ์˜ต์…˜์„ ๊ฐ™์ด ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ACCOUNT_EMAIL_REQUIRED: ํšŒ์›๊ฐ€์ž…ํ•  ๋•Œ ์ด๋ฉ”์ผ ์ฃผ์†Œ ์ž…๋ ฅ ํ•„์ˆ˜ ์—ฌ๋ถ€์ด๋‹ค. ๋””ํดํŠธ ๊ฐ’์€ False์ด๋ฏ€๋กœ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๊ฐ€์ž…๋œ๋‹ค. ACCOUNT_USER NAME_REQUIRED: ํšŒ์› ๊ฐ€์ž…ํ•  ๋•Œ user name ์ž…๋ ฅ ํ•„์ˆ˜ ์—ฌ๋ถ€์ด๋‹ค. ๋””ํดํŠธ ๊ฐ’์€ True์ด๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ACCOUNT_AUTHENTICATION_METHOD๋ฅผ ํ†ตํ•ด ์ด๋ฉ”์ผ๋กœ ๋กœ๊ทธ์ธ์œผ๋กœ ์„ค์ •ํ•˜๋”๋ผ๋„ user name์„ ์ž…๋ ฅํ•ด์•ผ ๊ฐ€์ž…๋œ๋‹ค. ACCOUNT_EMAIL_VERIFICATION: ์ด๋ฉ”์ผ<NAME> ์ธ์ฆ์ด ํ•„์š”ํ•œ ์ง€์ด๋‹ค. 'mandatory', 'optional', 'none' ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ 'mandatory'๋Š” ํšŒ์›๊ฐ€์ž… ํ›„ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์ธ์ฆํ•˜์ง€ ์•Š์œผ๋ฉด ํšŒ์›๊ฐ€์ž…ํ•˜๋”๋ผ๋„ ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์—†๋‹ค. 'optional'์€ ์ธ์ฆ ์ด๋ฉ”์ผ์€ ๋ฐœ์†ก๋˜์ง€๋งŒ ์ธ์ฆํ•˜์ง€ ์•Š์•„๋„ ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๊ณ  'none'์€ ์ธ์ฆ ๋ฉ”์ผ์„ ๋ณด๋‚ด์ง€๋„ ์•Š๊ณ  ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ACCOUNT_LOGIN_ATTEMPTS_LIMIT: ์ง€์ •๋œ ํšŸ์ˆ˜(๊ธฐ๋ณธ๊ฐ’=5) ๋งŒํผ ๋กœ๊ทธ์ธ ์‹คํŒจํ•  ๊ฒฝ์šฐ ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT ์„ค์ •๊ฐ’์œผ๋กœ ์ง€์ •ํ•œ ์‹œ๊ฐ„(๋‹จ์œ„=์ดˆ) ๋งŒํผ ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์—†๋‹ค. allauth ๋กœ๊ทธ์ธ ๋ทฐ์—์„œ ์ ์šฉ๋˜๊ณ  Django ๊ธฐ๋ณธ ๊ด€๋ฆฌ์ž ๋กœ๊ทธ์ธ ๋ทฐ์—๋Š” ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT: ๋กœ๊ทธ์ธ ์‹คํŒจ ์‹œ ๋‹ค์‹œ ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์—†๋Š” ์‹œ๊ฐ„(๊ธฐ๋ณธ๊ฐ’=300์ดˆ)์ด๋‹ค. ACCOUNT_USER_MODEL_USER NAME_FIELD: ์ปค์Šคํ…€ ์‚ฌ์šฉ์ž ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์•„์ด๋”” ํ•„๋“œ์˜ ์ด๋ฆ„์ด user name์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ด๋ฆ„์ผ ๊ฒฝ์šฐ ์ง€์ •ํ•œ๋‹ค. ๋งŒ์•ฝ None์œผ๋กœ ์ง€์ •ํ•  ๊ฒฝ์šฐ allauth์—์„œ user name๊ณผ ๊ด€๋ จ๋œ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด ๊ฒฝ์šฐ ACCOUNT_USER NAME_REQUIRED ๊ฐ’ ๋˜ํ•œ ๋ฐ˜๋“œ์‹œ False๋กœ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. ACCOUNT_USER_MODEL_EMAIL_FIELD: ์ปค์Šคํ…€ ์‚ฌ์šฉ์ž ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋ฉ”์ผ ํ•„๋“œ์˜ ์ด๋ฆ„์ด ๊ธฐ๋ณธ๊ฐ’ email์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ด๋ฆ„์ผ ๊ฒฝ์šฐ ์ง€์ •ํ•œ๋‹ค. ๋งŒ์•ฝ None์œผ๋กœ ์ง€์ •ํ•  ๊ฒฝ์šฐ allauth์—์„œ email๊ณผ ๊ด€๋ จ๋œ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด ๊ฒฝ์šฐ ACCOUNT_EMAIL_REQUIRED ๊ฐ’ ๋˜ํ•œ ๋ฐ˜๋“œ์‹œ False๋กœ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. ACCOUNT_SIGNUP_FORM_CLASS: ํšŒ์›๊ฐ€์ž… ํผ ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•˜๊ณ  ํ•ด๋‹น ํด๋ž˜์Šค๋Š” def signup(self, request, user) ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. SOCIALACCOUNT_AUTO_SIGNUP: ๋””ํดํŠธ ๊ฐ’์€ True์ด๋ฉฐ SNS ๊ณต๊ธ‰์ž์—์„œ ๋„˜๊ฒจ๋ฐ›์€ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฐ”๋กœ ํšŒ์›๊ฐ€์ž…์‹œํ‚จ๋‹ค. ๋ถ€๊ฐ€์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•ด False๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์‹œ ACCOUNT_AUTHENTICATION_METHOD = 'email' ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_EMAIL_VERIFICATION = 'mandatory' ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5 ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 300 SOCIALACCOUNT_AUTO_SIGNUP = False ACCOUNT_SIGNUP_FORM_CLASS = accounts.forms.SignupForm ์œ„์™€ ๊ฐ™์€ ์˜ˆ์‹œ ์„ค์ •์€ ํšŒ์›๊ฐ€์ž…ํ•  ๋•Œ ์ด๋ฉ”์ผ๊ณผ ์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ํ•จ๊ป˜ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์ž…๋ ฅ๋ฐ›์€ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๋ฐ˜๋“œ์‹œ ์ธ์ฆํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ ์†Œ์…œ ๋กœ๊ทธ์ธ์œผ๋กœ ํšŒ์›๊ฐ€์ž…์ด ์ž๋™์œผ๋กœ ๋˜์ง€๋Š” ์•Š๊ณ  ํ•„์ˆ˜ ์ž…๋ ฅ ํ•„๋“œ๋ฅผ ๋ฐ˜๋“œ์‹œ ์ž…๋ ฅ ํ›„ ์ด๋ฉ”์ผ ์ธ์ฆํ•ด์•ผ ๊ฐ€์ž…์ด ์™„๋ฃŒ๋œ๋‹ค. ๊ณ„์ • ํ”„๋กœํ•„ ํ•„๋“œ ์ถ”๊ฐ€ ๊ตฌํ˜„ ๊ณ„์ • ํ”„๋กœํ•„์„ ์œ„ํ•ด ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. * AbstractBaseUser ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•œ ์ปค์Šคํ…€ ๋ชจ๋ธ ์ •์˜ * AbstractUser ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•œ ์ปค์Šคํ…€ ๋ชจ๋ธ ์ •์˜ * ์ผ๋Œ€์ผ ๊ด€๊ณ„๋กœ ํ”„๋กœํ•„์„ ์œ„ํ•œ ํ…Œ์ด๋ธ”์˜ ์ถ”๊ฐ€ ์ž์„ธํ•œ ์„ค๋ช…์€ User ๋ชจ๋ธ์˜ ํ™•์žฅ ๊ธฐ๋ฒ• ๋น„๊ต ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•œ๋‹ค. AbstractBaseUser ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์ด์œ  ์ค‘์— ํ•˜๋‚˜๊ฐ€ ๋กœ๊ทธ์ธ ์•„์ด๋””๋กœ user name ๋Œ€์‹ ์— email์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ์ธ๋ฐ django-allauth ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ACCOUNT_AUTHENTICATION_METHOD = 'email' ์˜ต์…˜์œผ๋กœ ๊ฐ„๋‹จํžˆ ํ•ด๊ฒฐ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์‚ฌ์šฉ์ž ํ”„๋กœํ•„ ํŽ˜์ด์ง€๋ฅผ ์œ„ํ•ด AbstractUser ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ผ๋Œ€์ผ ๊ด€๊ณ„๋กœ ํ”„๋กœํ•„ ํ…Œ์ด๋ธ”์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ ํ”„๋กœํ•„ ํŽ˜์ด์ง€์—๋Š” ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๊ฐ€ ํฐ ์ •๋ณด๋“ค์ด ๋งŽ์œผ๋ฏ€๋กœ ํšŒ์› ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด auth_user ํ…Œ์ด๋ธ”์ด ์ง€๋‚˜์น˜๊ฒŒ ์ปค์งˆ ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฒฐ๊ตญ Django ๊ณต์‹ ๋ฌธ์„œ๊ฐ€ ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ์‹์ธ ์ผ๋Œ€์ผ ํ…Œ์ด๋ธ” ์ถ”๊ฐ€๊ฐ€ ๊ฐ€์žฅ ํ˜„์‹ค์ ์ธ ๋Œ€์•ˆ์ด ๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ์ผ๋Œ€์ผ ๊ด€๊ณ„ ํ”„๋กœํ•„ ๋ชจ๋ธ ์ •์˜ account ์•ฑ ์ด๋ฆ„์€ allauth์—์„œ ์‚ฌ์šฉํ•˜์—ฌ ์ถฉ๋Œํ•˜๋ฏ€๋กœ member, user ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์•ฑ์„ ์ƒ์„ฑํ•œ๋‹ค. member ์•ฑ์„ ์˜ˆ์‹œ๋กœ ํ•œ๋‹ค. settings.py ํŒŒ์ผ์—์„œ member ์•ฑ์„ ๋“ฑ๋กํ•œ๋‹ค. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', 'member.apps.MemberConfig', # <- ์ถ”๊ฐ€ ] ACCOUNT_SIGNUP_FORM_CLASS = 'member.forms.SignupForm' # <- ์•„๋ž˜์—์„œ ๊ธฐ์ˆ ํ•  ํšŒ์›๊ฐ€์ž… ํผ์„ ์‚ฌ์šฉ member/models.py ํŒŒ์ผ from django.conf import settings from django.db import models class Profile(models.Model): user = models.OneToOneField( settings.AUTH_USER_MODEL, # <- ํŠน์ • ์‚ฌ์šฉ์ž ๋ชจ๋ธ์— ์ข…์†์ ์ด์ง€ ์•Š๋‹ค. on_delete=models.CASCADE ) phone = models.CharField( max_length=100 ) class Meta: db_table = 'account_profile' app_label = 'account' # <- account ์•ฑ ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ ๊ด€๋ฆฌ๋˜๋„๋ก ํ•œ๋‹ค. member/forms.py ํŒŒ์ผ from django import forms from django.utils.translation import ugettext_lazy as _ from .models import Profile class SignupForm(forms.Form): first_name = forms.CharField(label=_('First name'), max_length=30, widget=forms.TextInput( attrs={'placeholder': _('First name'), })) last_name = forms.CharField(label=_('Last name'), max_length=30, widget=forms.TextInput( attrs={'placeholder': _('Last name'), })) phone = forms.CharField(label=_('Phone number'), max_length=30, widget=forms.TextInput( attrs={'placeholder': _('Phone number'), })) def signup(self, request, user): user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.save() profile = Profile() profile.user = user profile.phone = self.cleaned_data['phone'] profile.save() User ๋ชจ๋ธ์— first_name, last_name ํ•„๋“œ๊ฐ€ ์กด์žฌํ•˜๊ณ  Profile ๋ชจ๋ธ์— phone ํ•„๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค. ๋ณ„๋„๋กœ ๊ฐ ์ธ์Šคํ„ด์Šค์—์„œ ์•Œ๋งž๊ฒŒ save() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค. AbstractUser ํด๋ž˜์Šค์˜ ์ƒ์† ์ผ๋Œ€์ผ ํ…Œ์ด๋ธ” ์ถ”๊ฐ€๊ฐ€ ๊ฐ€์žฅ ์ ํ•ฉํ•  ๊ฒƒ ๊ฐ™์ง€๋งŒ AbstractUser ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์‚ดํŽด๋ณธ๋‹ค. users/models.py ํŒŒ์ผ from django.contrib.auth.models import AbstractUser # AbstractUser๋Š” ์ด๋ฏธ user name, email, password, first_name, last_name ๊ฐ™์€ ํ•„๋“œ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Œ class CustomUser(AbstractUser): phone = models.CharField( max_length=100 ) # ํšŒ์› ๊ฐ€์ž… ์‹œ ์ด๋ฉ”์ผ ์ž…๋ ฅ์„ ํ•„์ˆ˜๋กœ ํ•œ๋‹ค. REQUIRED_FIELDS = ["email"] settings.py ํŒŒ์ผ AUTH_USER_MODEL = 'users.CustomUser' # ์œ„์—์„œ ์ •์˜ํ•œ CustomUser ๋ชจ๋ธ ํด๋ž˜์Šค ์ง€์ • member/forms.py ํŒŒ์ผ class SignupForm(forms.Form): # ์ผ๋Œ€์ผ ๊ด€๊ณ„ ํ”„๋กœํ•„ ๋ชจ๋ธ ์˜ˆ์‹œ์™€ ๋™์ผ def signup(self, request, user): user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.phone = self.cleaned_data['phone'] user.save() ๋ชจ๋‘ user ํ…Œ์ด๋ธ”์— ์†ํ•œ ํ•„๋“œ์ด๋ฏ€๋กœ ์†์„ฑ๊ฐ’ ๋Œ€์ž… ํ›„ ์ €์žฅํ•œ๋‹ค. SNS ์„ค์ • ํŽ˜์ด์Šค๋ถ settings.py ํŒŒ์ผ SOCIALACCOUNT_PROVIDERS = { 'facebook': { 'METHOD': 'oauth2', 'SCOPE': ['email', 'public_profile', ], # 'user_friends'๋Š” ์š”์ฒญ ์•ˆ ํ•จ # 'AUTH_PARAMS': {'auth_type': 'reauthenticate'}, # ๋งค๋ฒˆ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ฌป์ง€ ์•Š์œผ๋ ค๋ฉด ์ฃผ์„ ์ฒ˜๋ฆฌ 'INIT_PARAMS': {'cookie': True}, 'FIELDS': [ 'id', 'email', 'name', 'first_name', 'last_name', 'verified', 'locale', 'timezone', 'link', 'gender', 'updated_time', ], 'EXCHANGE_TOKEN': True, 'LOCALE_FUNC': lambda request: 'kr_KR', 'VERIFIED_EMAIL': False, 'VERSION': 'v2.4', }, } SOCIAL_AUTH_FACEBOOK_KEY = '์•ฑ ID' SOCIAL_AUTH_FACEBOOK_SECRET = '์•ฑ ์‹œํฌ๋ฆฟ ์ฝ”๋“œ' social-auth-app-django ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ ์ค€๋น„ pip install social-auth-app-django settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'social_django', # <-- django ๊ธฐ๋ณธ ํŒจํ‚ค์ง€ ๋ฐ‘์— ] python manage.py migrate ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•„์š”ํ•œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•œ๋‹ค. manage.py migrate ์„ค์ • settings.py ๋ฏธ๋“ค์›จ์–ด ํด๋ž˜์Šค ๋“ฑ๋กํ•œ๋‹ค. MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'social_django.middleware.SocialAuthExceptionMiddleware', # <-- ๋์— ์ถ”๊ฐ€ ] ํ…œํ”Œ๋ฆฟ์˜ ์ปจํ…์ŠคํŠธ ํ”„๋กœ์„ธ์„œ ๋“ฑ๋กํ•œ๋‹ค. TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'social_django.context_processors.backends', # <-- ๋์— ์ถ”๊ฐ€ 'social_django.context_processors.login_redirect', # <-- ๋์— ์ถ”๊ฐ€ ], 'debug': DEBUG, }, }, ] ์ธ์ฆ ๋ฐฑ์—”๋“œ ๋“ฑ๋กํ•œ๋‹ค. AUTHENTICATION_BACKENDS = ( 'social_core.backends.kakao.KakaoOAuth2', # <-- ์นด์นด์˜คํ†ก 'social_core.backends.line.LineOAuth2', # <-- ๋ผ์ธ 'social_core.backends.google.GoogleOAuth2', # <-- ๊ตฌ๊ธ€ 'social_core.backends.facebook.FacebookOAuth2', # <-- ํŽ˜์ด์Šค๋ถ 'social_core.backends.twitter.TwitterOAuth', # <-- ํŠธ์œ„ํ„ฐ 'django.contrib.auth.backends.ModelBackend', # <-- Django ์‹œ์Šคํ…œ ์‚ฌ์šฉ์ž๋กœ ๋กœ๊ทธ์ธ ) urls.py URL ํŒจํ„ด์„ ๋“ฑ๋กํ•œ๋‹ค. urlpatterns = [ ... ์ƒ๋žต ... url(r'^oauth/', include('social_django.urls', namespace='social')), # <-- ... ์ƒ๋žต ... ] SNS ์„ค์ • ํŽ˜์ด์Šค๋ถ ์‚ฌ์šฉ์ž๊ฐ€ ์ด๋ฉ”์ผ ์ œ๊ณต ๋™์˜ํ•ด๋„ ํŽ˜์ด์Šค๋ถ์—์„œ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ด๋ฉ”์ผ ๊ฐ€์ž…ํ–ˆ์œผ๋‚˜ ์ธ์ฆํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ „ํ™”๋ฒˆํ˜ธ๋กœ ๊ฐ€์ž…ํ•ด์„œ ์ด๋ฉ”์ผ์ด ์—†๋Š” ๊ฒฝ์šฐ 10) ์„ฌ๋„ค์ผ ์ด๋ฏธ์ง€ ํŒจํ‚ค์ง€ ์„ ํƒ easy-thumbnails ์„ค์น˜ ๋ฐ ๋“ฑ๋ก ๋ชจ๋ธ ๋ณ€๊ฒฝ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ์ฃผ์š” ์˜ต์…˜ ์„ฌ๋„ค์ผ ์ด๋ฏธ์ง€ ์ „์ฒด ์‚ญ์ œ ํŒจํ‚ค์ง€ ์„ ํƒ Django ์„ฌ๋„ค์ผ ์ด๋ฏธ์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋น„๊ต ์‚ฌ์ดํŠธ์—์„œ ๋ณด๋ฉด ๋Œ€ํ‘œ์ ์œผ๋กœ ์„ฌ๋„ค์ผ ์ด๋ฏธ์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒจํ‚ค์ง€๋Š” easy-thumbnails, django-imagekit, sorl-thumbnail ๋“ฑ์ด ์žˆ๋‹ค. image-thumbnails django-imagekit sorl-thumbnail easy-thumbnails ์„ค์น˜ ๋ฐ ๋“ฑ๋ก pip ํŒจํ‚ค์ง€๋กœ ์„ค์น˜ํ•œ๋‹ค. pip install easy_thumbnails settings.py ํŒŒ์ผ์— ์•ฑ์„ ๋“ฑ๋กํ•œ๋‹ค. INSTALLED_APPS += [ 'easy_thumbnails', ] ๋ชจ๋ธ ๋ณ€๊ฒฝ ๊ธฐ์กด models.FileField ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ easy_thumbnails.fields.ThumbnailerField ํ•„๋“œ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  models.ImageField ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ easy_thumbnails.fields.ThumbnailerImageField ํ•„๋“œ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ๋‹ค์Œ์€ ์˜ˆ์‹œ ์„ ์–ธ์ด๋‹ค. thumbnail = ThumbnailerImageField( verbose_name=_('Thumbnail'), upload_to='blog/thumbnails/%Y/%m', blank=True ) ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ๋‹ค์Œ์€ ํ…œํ”Œ๋ฆฟ ํƒœ๊ทธ ์‚ฌ์šฉ ์˜ˆ์‹œ์ด๋‹ค. {% if post.thumbnail %} {% load thumbnail %} {% thumbnail post.thumbnail 260x180 as im %} <img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"> {% else %} <img src="http://placehold.it/260x180" alt="thumbnail"> {% endif %} ์ฃผ์š” ์˜ต์…˜ ์„ฌ๋„ค์ผ ์ด๋ฏธ์ง€ ์ „์ฒด ์‚ญ์ œ class Post(Model): thumbnail = ThumbnailerImageField() ์œ„์™€ ๊ฐ™์ด Post ๋ชจ๋ธ์ด ์กด์žฌํ•˜๊ณ  ์•„๋ž˜ thumbnail ์ด๋ฏธ์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ  ํ•  ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ฌ๋„ค์ผ์„ ์‚ญ์ œํ•œ๋‹ค. for m in Post.objects.all(): m.image.delete_thumbnails() 11) ๊ตฌ๊ธ€ reCAPTCHA ํšŒ์› ๊ฐ€์ž… ์‹œ ๊ตฌ๊ธ€ reCAPTCHA ์‚ฌ์ดํŠธ ๋“ฑ๋ก ๋น„๋ฐ€ํ‚ค ๋“ฑ๋ก reCAPTCHA ์ž…๋ ฅ ํŽ˜์ด์ง€ ์ž…๋ ฅ ๊ฒ€์ฆ ์„œ๋“œ ํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ฐธ๊ณ  ์ฐธ๊ณ  ์‚ฌ์ดํŠธ ํšŒ์› ๊ฐ€์ž… ์‹œ ๊ตฌ๊ธ€ reCAPTCHA ๋ด‡์— ์˜ํ•œ ๊ธฐ๊ณ„์ ์ธ ํšŒ์› ๊ฐ€์ž…์„ ๋ง‰๊ธฐ ์œ„ํ•ด CAPTCHA ๋ฌธ์ž์—ด ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ๊ตฌ๊ธ€ reCAPTCHA ์„œ๋น„์Šค ์ด์šฉ์„ ์‚ดํŽด๋ณธ๋‹ค. ์‚ฌ์ดํŠธ ๋“ฑ๋ก ๊ตฌ๊ธ€ reCAPTCHA ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์—์„œ ์‚ฌ์ดํŠธ๋ฅผ ๋“ฑ๋กํ•˜์—ฌ ํ‚ค๋ฅผ ๋ฐœ๊ธ‰๋ฐ›๋Š”๋‹ค. ์œ„ ํ™”๋ฉด๊ณผ ๊ฐ™์ด ๋‚ด ์ปดํ“จํ„ฐ์—์„œ ํ…Œ์ŠคํŠธ ๋ชฉ์ ์œผ๋กœ ๋กœ์ปฌ ํ˜ธ์ŠคํŠธ IP 127.0.0.1์„ ๋„๋ฉ”์ธ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ณต๊ฐœํ‚ค์ธ ์‚ฌ์ดํŠธ ํ‚ค(Site key)์™€ ๋น„๋ฐ€ํ‚ค(Secret key)๋ฅผ ๋ฐœ๊ธ‰๋ฐ›๋Š”๋‹ค. ์‚ฌ์ดํŠธ ํ‚ค๋Š” ์›น ํŽ˜์ด์ง€์˜ ์œ„์ ฏ์œผ๋กœ ๊ณต๊ฐœ๋˜๋Š” ๊ฐ’์ด์ง€๋งŒ ๋น„๋ฐ€ํ‚ค๋Š” settings.py ํŒŒ์ผ์— ๋น„๊ณต๊ฐœ๋กœ ์ €์žฅ๋˜๋Š” ๊ฐ’์ด๋‹ค. ๋น„๋ฐ€ํ‚ค ๋“ฑ๋ก settings.py ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋“ฑ๋กํ•œ๋‹ค. GOOGLE_RECAPTCHA_SECRET_KEY = '6LdHtSgUAAAAAJM8ehkmf-.............' reCAPTCHA ์ž…๋ ฅ ํŽ˜์ด์ง€ <form method="post"> {% csrf_token %} {{ form.as_p }} <script src='https://www.google.com/recaptcha/api.js'></script> <div class="g-recaptcha" data-sitekey="๋ฐœ๊ธ‰๋ฐ›์€_๊ณ ์œ _์‚ฌ์ดํŠธ_ํ‚ค๊ฐ’"></div> <button type="submit" class="btn btn-primary">Post</button> </form> ์—ฌ๊ธฐ์—์„œ ์ค‘์š”ํ•œ ์ฝ”๋“œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ์™€ ์‚ฌ์ดํŠธ ํ‚ค๊ฐ’์„ ๋„˜๊ธฐ๋Š” div ํƒœ๊ทธ ๋‘ ์ค„์ด๋‹ค. ์ž…๋ ฅ ๊ฒ€์ฆ ์ œ๋„ˆ๋ฆญ๋ทฐ CreateView๋ฅผ ์ƒ์†ํ•œ MessageCreateView ๋ทฐ ์˜ˆ์‹œ ์ฝ”๋“œ์ด๋‹ค. class MessageCreateView(BoardContextMixin, CreateView): logger = logging.getLogger(__name__) form_class = MessageForm template_name = 'board/message_form.html' def get_form_kwargs(self): # 'self.request' ๊ฐ์ฒด๋ฅผ ํผ์— ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์˜ค๋ฒ„๋ผ์ด๋”ฉ self.logger.debug('MessageListView.get_form_kwargs()') kwargs = super(MessageCreateView, self).get_form_kwargs() kwargs['request'] = self.request return kwargs get_form_kwargs() ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์•ผ ํ•˜๋Š”๋ฐ ํผ ๊ฐ์ฒด์— request ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๋„๋ก ์•„๊ทœ๋จผํŠธ๋ฅผ ์ €์žฅํ•ด๋‘”๋‹ค. ๋ชจ๋ธ ํผ์„ ์ƒ์†ํ•œ MessageForm ํผ ํด๋ž˜์Šค ์˜ˆ์‹œ์ด๋‹ค. class MessageForm(forms.ModelForm): def __init__(self, *args, **kwargs): # important to "pop" added kwarg before call to parent's constructor self.request = kwargs.pop('request') super(MessageForm, self).__init__(*args, **kwargs) def clean(self): self.logger.debug('MessageForm.clean()') # Google reCAPTCHA recaptcha_response = self.request.POST.get('g-recaptcha-response') url = 'https://www.google.com/recaptcha/api/siteverify' values = { 'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY, 'response': recaptcha_response } data = urllib.parse.urlencode(values).encode() req = urllib.request.Request(url, data=data) response = urllib.request.urlopen(req) result = json.loads(response.read().decode()) if not result['success']: raise ValidationError(_('reCAPTCHA error occurred.')) return super(MessageForm, self).clean() ๋จผ์ € init() ์ƒ์„ฑ์ž๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์„œ ๋ทฐ์—์„œ ์ €์žฅํ•ด๋‘” request ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด์˜จ๋‹ค. ๋‚˜์ค‘์— ๊ฒ€์ฆ์„ ์œ„ํ•ด POST๋กœ ์ „๋‹ฌ๋ฐ›์€ g-recaptcha-response ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. ๋ทฐ์˜ form_valid() ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹Œ ํผ ํด๋ž˜์Šค์˜ clean() ๋ฉ”์„œ๋“œ์—์„œ reCAPTCHA ์ž…๋ ฅ<NAME> ๊ฒ€์‚ฌ๋ฅผ ํ•œ๋‹ค. form_valid() ๋ฉ”์„œ๋“œ๋Š”<NAME> ๊ฒ€์‚ฌ๋ฅผ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ์ด๋ฏธ<NAME> ๊ฒ€์‚ฌ๋ฅผ ๋งˆ์นœ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์„œ๋“œ ํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ requests ์„œ๋“œ ํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†Œ์Šค ์ฝ”๋“œ์˜ ์–‘์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. recaptcha_response = self.request.POST.get('g-recaptcha-response') url = 'https://www.google.com/recaptcha/api/siteverify' values = { 'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY, 'response': recaptcha_response } data = urllib.parse.urlencode(values).encode() req = urllib.request.Request(url, data=data) response = urllib.request.urlopen(req) result = json.loads(response.read().decode()) ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์žฌ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. import requests recaptcha_response = request.POST.get('g-recaptcha-response') data = { 'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY, 'response': recaptcha_response } r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data) result = r.json() ์บก์Šํ™”ํ•ด์„œ ์ฝ”๋“œ์˜ ์–‘์ด ์กฐ๊ธˆ ์ค„๊ธฐ๋Š” ํ–ˆ์ง€๋งŒ ๋ณ„๋„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์—†์ด copy&paste๋กœ ์ž‘์„ฑํ•ด๋„ ๋ฌด๋ฐฉํ•  ์ˆ˜์ค€ ๊ฐ™๋‹ค. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ฐธ๊ณ  ์ฐธ๊ณ  ์‚ฌ์ดํŠธ How to Add reCAPTCHA to a Django Site 11. ํ…Œ์ŠคํŠธ 01) ๋‹จ์œ„ ํ…Œ์ŠคํŠธ 12. WSGI ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ๋ฐฐํฌ WSGI ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„ ์—ฐ๋™ ์˜ˆ์ œ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์›น์„œ๋ฒ„ nginx์™€ ์—ฐ๋™ ๊ฐ€๋Šฅํ•œ WSGI ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„๋กœ uWSGI์™€ Gunicorn ๋‘ ๊ฐ€์ง€๋ฅผ ์‚ดํŽด๋ณธ๋‹ค. 01) ๋ฐฐํฌ ์ ๊ฒ€์‚ฌํ•ญ ๋ฐฐํฌ ์ ๊ฒ€์‚ฌํ•ญ manage.py check --deploy ๋ช…๋ น ์ฃผ์š” ๋ฐฐํฌ ์„ค์ •๊ฐ’ SECRET_KEY DEBUG ALLOWED_HOSTS ์ฐธ๊ณ  ์ž๋ฃŒ ๋ฐฐํฌ ์ ๊ฒ€์‚ฌํ•ญ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌ ์ „์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์‚ฌํ•ญ์„ ์ ๊ฒ€ํ•ด์•ผ ํ•œ๋‹ค. manage.py check --deploy ๋ช…๋ น Django์—์„œ ๊ธฐ๋ณธ์ ์ธ ์ ๊ฒ€์‚ฌํ•ญ์„ ์ œ์•ˆํ•ด ์ฃผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค. Django ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •๋œ settings.py ํŒŒ์ผ์„ ๋ณ„๋‹ค๋ฅธ ์ˆ˜์ • ์—†์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ นํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. > manage.py check --deploy ystem check identified some issues: WARNINGS: ?: (security.W004) You have not set a value for the SECURE_HSTS_SECONDS setting. If your entire site is served only over SSL, you may want to consider setting a value and enabling HTTP Strict Transport Security. Be sure to read the documentation first; enabling HSTS carelessly can cause serious, irreversible problems. ?: (security.W006) Your SECURE_CONTENT_TYPE_NOSNIFF setting is not set to True, so your pages will not be served with an 'x-content-type-options: nosniff' header. You should consider enabling this header to prevent the browser from identifying content types inc orrectly. ?: (security.W007) Your SECURE_BROWSER_XSS_FILTER setting is not set to True, so your pages will not be served with an 'x-xss-protection: 1; mode=block' header. You should consider enabling this header to activate the browser's XSS filtering and help prevent XS S attacks. ?: (security.W008) Your SECURE_SSL_REDIRECT setting is not set to True. Unless your site should be available over both SSL and non-SSL connections, you may want to either set this setting True or configure a load balancer or reverse-proxy server to redirect all connections to HTTPS. ?: (security.W012) SESSION_COOKIE_SECURE is not set to True. Using a secure-only session cookie makes it more difficult for network traffic sniffers to hijack user sessions. ?: (security.W016) You have 'django.middleware.csrf.CsrfViewMiddleware' in your MIDDLEWARE, but you have not set CSRF_COOKIE_SECURE to True. Using a secure-only CSRF cookie makes it more difficult for network traffic sniffers to steal the CSRF token. ?: (security.W018) You should not have DEBUG set to True in deployment. ?: (security.W019) You have 'django.middleware.clickjacking.XFrameOptionsMiddleware' in your MIDDLEWARE, but X_FRAME_OPTIONS is not set to 'DENY'. The default is 'SAMEORIGIN', but unless there is a good reason for your site to serve other parts of itself in a f rame, you should change it to 'DENY'. ?: (security.W020) ALLOWED_HOSTS must not be empty in deployment. System check identified 9 issues (0 silenced). ์ฃผ์š” ๋ฐฐํฌ ์„ค์ •๊ฐ’ SECRET_KEY ๋ณด์•ˆ์ƒ ์ ˆ๋Œ€ ๊ณต๊ฐœ๋˜๋ฉด ์•ˆ ๋˜๋Š” ํ‚ค๊ฐ’์œผ๋กœ ์‹ค์ œ ๋ฐฐํฌํ•  ๋•Œ๋Š” settings.py ํŒŒ์ผ์— ํ•˜๋“œ์ฝ”๋”ฉํ•˜๊ธฐ๋ณด๋‹ค๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋˜๋Š” ํŒŒ์ผ๋กœ ์ฝ์–ด๋“œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์„ ๊ถŒ์žฅํ•œ๋‹ค. ์†Œ์Šค์ฝ”๋“œ์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ฝ์–ด ์„ค์ •ํ•˜๋Š” ๋ฒ• import os SECRET_KEY = os.environ['SECRET_KEY'] ํŒŒ์ผ์„ ์ฝ์–ด ์„ค์ •ํ•˜๋Š” ๋ฒ• with open('/etc/secret_key.txt') as f: SECRET_KEY = f.read().strip() DEBUG ์šด์˜์„œ๋ฒ„์—์„œ๋Š” ์ ˆ๋Œ€๋กœ ๋””๋ฒ„๊น… ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. DEBUG = False ALLOWED_HOSTS ๋””๋ฒ„๊น… ๋ชจ๋“œ์—์„œ ALLOWED_HOSTS ๋ณ€์ˆ˜๊ฐ€ ๋นˆ ๋ฆฌ์ŠคํŠธ์ผ ๊ฒฝ์šฐ ['localhost', '127.0.0.1', '[::1]'] ์˜๋ฏธ๊ฐ€ ๋œ๋‹ค. ์ฆ‰, ๋กœ์ปฌ ํ˜ธ์ŠคํŠธ์—์„œ๋งŒ ์ ‘์†์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋””๋ฒ„๊น… ๋ชจ๋“œ๋ฅผ ๋„๋ฉด ์ผ์ฒด ์ ‘์†์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•œ ํ˜ธ์ŠคํŠธ์—๋งŒ ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋‹ค. ALLOWED_HOSTS = ['example.com', 'www.example.com', 'localhost', ] ์ฐธ๊ณ  ์ž๋ฃŒ Deployment checklist 02) Nginx, uWSGI ๋ฐฐํฌ ์‹œ์Šคํ…œ ๊ตฌ์„ฑ ์˜ˆ์‹œ Django ๊ฐ€์ƒํ™˜๊ฒฝ ์ค€๋น„ ํŒจํ‚ค์ง€ ์„ค์น˜ ํ”„๋กœ์ ํŠธ/์ €์žฅ์†Œ ์ƒ์„ฑ STATIC_ROOT ๋””๋ ‰ํ„ฐ๋ฆฌ ์ง€์ • ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ admin ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ static ๋ฐ์ดํ„ฐ ๋ชจ์œผ๊ธฐ ํ…Œ์ŠคํŠธ ์„œ๋ฒ„ ๊ตฌ๋™ ๋ฐ ์ ‘์† ํ™•์ธ uWSGI ๊ตฌ๋™ ํ…Œ์ŠคํŠธ uWSGI ์˜ต์…˜ ํŒŒ์ผ ์„œ๋น„์Šค ๋“ฑ๋ก ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ uWSGI ์„œ๋น„์Šค ๋“ฑ๋ก uWSGI ์„œ๋น„์Šค ๊ตฌ๋™ ํ™•์ธ nginx ์‚ฌ์ดํŠธ ์„ค์ • ์ถ”๊ฐ€ ์‚ฌ์ดํŠธ ์ถ”๊ฐ€ nginx ์„ค์ • ๋ฌธ๋ฒ• ๊ฒ€์‚ฌ ๋ฐ ์žฌ๊ธฐ๋™ ๋ฐฉํ™”๋ฒฝ ํ•ด์ œ ํ•œ ์„œ๋ฒ„์— ์—ฌ๋Ÿฌ Django ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์Šคํ…œ ์ „์—ญ uWSGI ์„ค์น˜ uWSGI ์„ค์ • Nginx ์„ค์ • ์ฐธ๊ณ ๋ฌธํ—Œ ์‹œ์Šคํ…œ ๊ตฌ์„ฑ ์˜ˆ์‹œ ์šฐ๋ถ„ํˆฌ 16.04 Django nginx uWSGI ๋‹ค์Œ ์„ค์น˜ ์˜ˆ์ œ๋Š” ์‹ค์ œ ๋ฌผ๋ฆฌ ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ vultr.com์˜ ๊ฐ€์ƒ ์„œ๋ฒ„ ์ธ์Šคํ„ด์Šค์—์„œ ์ง„ํ–‰ํ•˜์˜€๋‹ค. ์˜ˆ์ œ์—์„œ ์‹œ์Šคํ…œ ๊ณ„์ • ์ด๋ฆ„์€ foo, ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์€ django_test๋กœ ๊ฐ€์ •ํ•œ๋‹ค. Django ๊ฐ€์ƒํ™˜๊ฒฝ ์ค€๋น„ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์„ค์น˜ํ•˜๊ณ  ํ™œ์„ฑํ™”ํ•œ๋‹ค. cd ~ mkdir django_test cd django_test pyvenv venv source venv/bin/activate (venv) ํŒจํ‚ค์ง€ ์„ค์น˜ ๋…๋ฆฝ๋œ ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ Django๋ฅผ ๋น„๋กฏํ•œ ํ•„์š”ํ•œ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ๋‹ค. pip install Django uwsgi ํ”„๋กœ์ ํŠธ/์ €์žฅ์†Œ ์ƒ์„ฑ mkdir repo run sudo chown foo:www-data run cd repo django-admin.py startproject conf . STATIC_ROOT ๋””๋ ‰ํ„ฐ๋ฆฌ ์ง€์ • conf/settings.py ํŒŒ์ผ์˜ ๋์— ๋‹ค์Œ ์ค„์„ ์ถ”๊ฐ€ํ•œ๋‹ค. STATIC_ROOT = os.path.join(BASE_DIR, 'static/') ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ./manage.py makemigrations ./manage.py migrate admin ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ ./manage.py createsuperuser static ๋ฐ์ดํ„ฐ ๋ชจ์œผ๊ธฐ ./manage.py collectstatic ํ…Œ์ŠคํŠธ ์„œ๋ฒ„ ๊ตฌ๋™ ๋ฐ ์ ‘์† ํ™•์ธ ./manage.py runserver 0.0.0.0:8000 http://์„œ๋ฒ„_์•„์ดํ”ผ:8000/ ์ฃผ์†Œ๋กœ ์ ‘์†ํ•˜์—ฌ ๋‚ด์šฉ์ด ์ž˜ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. uWSGI ๊ตฌ๋™ ํ…Œ์ŠคํŠธ uwsgi --http :8000 --home /home/foo/django_test/venv --chdir /home/foo/django_test/repo --module conf.wsgi เธท์ฃผ์š” ์˜ต์…˜์˜ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. --http ํฌํŠธ ๋ฒˆํ˜ธ๋ฅผ ์ง€์ •ํ•œ๋‹ค. --home virtualenv ๊ฐ€์ƒํ™˜๊ฒฝ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ง€์ •ํ•œ๋‹ค. --chdir manage.py๊ฐ€ ๋“ค์–ด์žˆ๋Š” Django ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ง€์ •ํ•œ๋‹ค. --module WSGI ๋ชจ๋“ˆ์„ ์ง€์ •ํ•œ๋‹ค. ํŒŒ์ด์ฌ WSGI HTTP ์„œ๋ฒ„๋ฅผ ๊ตฌ๋™ํ•˜์—ฌ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ http://์„œ๋ฒ„_์•„์ดํ”ผ:8000/ ์ฃผ์†Œ์— ์ ‘์†ํ•˜์—ฌ ๋‚ด์šฉ์ด ์ž˜ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. uWSGI ์„œ๋ฒ„๊ฐ€ ์˜ฌ๋ฐ”๋กœ ๋™์ž‘ํ•˜๋ฉด ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ ์ด์ œ ๋น ์ ธ๋‚˜์˜จ๋‹ค. deactivate uWSGI ์˜ต์…˜ ํŒŒ์ผ /home/foo/django_test/run/uwsgi.ini ํŒŒ์ผ ์ƒ์„ฑ [uwsgi] uid = foo base = /home/%(uid)/django_test home = %(base)/venv chdir = %(base)/repo module = conf.wsgi:application env = DJANGO_SETTINGS_MODULE=conf.settings master = true processes = 5 socket = %(base)/run/uwsgi.sock chown-socket = %(uid):www-data chmod-socket = 660 vacuum = true ๋‹จ์ผ WSGI ์„œ๋ฒ„๋งŒ ๊ตฌ๋™ํ•  ๊ฒƒ์ด๋ผ๋ฉด ์œ„์™€ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ์—. ini ํŒŒ์ผ์„ ๋†“์„ ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ WSGI ์„œ๋ฒ„๋ฅผ ๊ตฌ๋™ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด /etc/uwsgi/sites์™€ ๊ฐ™์€ ์‹œ์Šคํ…œ ์„ค์ • ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜. ini ํŒŒ์ผ์„ ๋‘˜ ์ˆ˜ ์žˆ๋‹ค. ์„œ๋น„์Šค ๋“ฑ๋ก ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ /etc/systemd/system/uwsgi.service ํŒŒ์ผ์„ ์•„๋ž˜์™€ ๊ฐ™์€ ๋‚ด์šฉ์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค. [Unit] Description=uWSGI Emperor service [Service] ExecStart=/home/foo/django_test/venv/bin/uwsgi \ --emperor /home/foo/django_test/run User=foo Group=www-data Restart=on-failure KillSignal=SIGQUIT Type=notify NotifyAccess=all StandardError=syslog [Install] WantedBy=multi-user.target --emperor ์˜ต์…˜์œผ๋กœ uwsgi.ini ํŒŒ์ผ์ด ๋“ค์–ด์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ง€์ •ํ•œ๋‹ค. uWSGI ์„œ๋น„์Šค ๋“ฑ๋ก sudo systemctl start uwsgi sudo systemctl enable uwsgi uWSGI ์„œ๋น„์Šค ๊ตฌ๋™ ํ™•์ธ systemctl status uwsgi ๋งŒ์•ฝ ๊ตฌ๋™ ์‹คํŒจ ์‹œ ์—๋Ÿฌ ๋กœ๊ทธ๋Š” /var/log/syslog์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. nginx ์‚ฌ์ดํŠธ ์„ค์ • ์ถ”๊ฐ€ /etc/nginx/sites-available/django_test ํŒŒ์ผ์„ ์•„๋ž˜์™€ ๊ฐ™์€ ๋‚ด์šฉ์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค. upstream withthai-django { server unix:/home/foo/django_test/run/uwsgi.sock; } server { listen 80; server_name 128.199.192.157; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/foo/django_test/repo; } location / { include /etc/nginx/uwsgi_params; uwsgi_pass django; } } ์‚ฌ์ดํŠธ ์ถ”๊ฐ€ sudo ln -s /etc/nginx/sites-available/django_test /etc/nginx/sites-enabled nginx ์„ค์ • ๋ฌธ๋ฒ• ๊ฒ€์‚ฌ ๋ฐ ์žฌ๊ธฐ๋™ sudo nginx -t sudo systemctl restart nginx ๋ฐฉํ™”๋ฒฝ ํ•ด์ œ sudo ufw delete allow 8000 sudo ufw allow 'Nginx Full' ํ•œ ์„œ๋ฒ„์— ์—ฌ๋Ÿฌ Django ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์Šคํ…œ ์ „์—ญ uWSGI ์„ค์น˜ sudo apt-get install python3-dev python3-pip python3-setuptools sudo -H pip3 install uwsgi uWSGI ์„ค์ • ์—ฌ๋Ÿฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜. ini ํŒŒ์ผ์„ /etc/uwsgi/sites ๋””๋ ‰ํ„ฐ๋ฆฌ ํ•œ๊ณณ์œผ๋กœ ๋ชจ์€๋‹ค. [Unit] Description=uWSGI Emperor service [Service] ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites Restart=on-failure KillSignal=SIGQUIT Type=notify NotifyAccess=all StandardError=syslog [Install] WantedBy=multi-user.target ExecStart ๋ณ€์ˆ˜์—์„œ --emperor /etc/uwsgi/sites ์˜ต์…˜์„ ๋‘๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด๋‹ค. ์‹œ์Šคํ…œ ์ „์—ญ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋ฏ€๋กœ User, Group ๋ณ€์ˆ˜ ์„ค์ •์„ ์—†์•ค๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹œ์Šคํ…œ ์ „์—ญ์˜ uWSGI๋ฅผ ์ด์šฉํ•˜๋ฏ€๋กœ ๊ฐ€์ƒํ™˜๊ฒฝ์˜ uWSGI๋Š” ๊ตณ์ด ์„ค์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. Nginx ์„ค์ • upstream ์ •์˜ ์ด๋ฆ„์ด ๊ฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„๋งˆ๋‹ค ์ค‘๋ณต๋˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•œ๋‹ค. server_name์—์„œ ๋„๋ฉ”์ธ ์ด๋ฆ„์„ ์˜ฌ๋ฐ”๋กœ ์„ ์–ธํ•˜๊ณ  ์ •์  ํŒŒ์ผ, ๋กœ๊ทธํŒŒ์ผ ๋“ฑ์˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์ •ํ™•ํžˆ ์ง€์ •ํ•œ๋‹ค. ์ฐธ๊ณ ๋ฌธํ—Œ How To Serve Django Applications with uWSGI and Nginx on Ubuntu 16.04 31์žฅ ์žฅ๊ณ  ํ”„๋กœ์ ํŠธ ๋ฐฐํฌํ•˜๊ธฐ - 31.1 ์ž‘์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•œ ๋‹จ์ผ ์„œ๋ฒ„์˜ ๊ฒฝ์šฐ / Two Scoops of Django 03) Nginx, Gunicorn ๋ฐฐํฌ ์‹œ์Šคํ…œ ๊ตฌ์„ฑ ์˜ˆ์‹œ Django ๊ฐ€์ƒํ™˜๊ฒฝ ์ค€๋น„ ํŒจํ‚ค์ง€ ์„ค์น˜ ํ”„๋กœ์ ํŠธ/์ €์žฅ์†Œ ์ƒ์„ฑ STATIC_ROOT ๋””๋ ‰ํ„ฐ๋ฆฌ ์ง€์ • ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ admin ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ static ๋ฐ์ดํ„ฐ ๋ชจ์œผ๊ธฐ ํ…Œ์ŠคํŠธ ์„œ๋ฒ„ ๊ตฌ๋™ ๋ฐ ์ ‘์† ํ™•์ธ Gunicorn ๊ตฌ๋™ ํ…Œ์ŠคํŠธ ์„œ๋น„์Šค ๋“ฑ๋ก ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ Gunicorn ์„œ๋น„์Šค ๋“ฑ๋ก Gunicorn ์„œ๋น„์Šค ๊ตฌ๋™ ํ™•์ธ nginx ์‚ฌ์ดํŠธ ์„ค์ • ์ถ”๊ฐ€ ์‚ฌ์ดํŠธ ์ถ”๊ฐ€ nginx ์„ค์ • ๋ฌธ๋ฒ• ๊ฒ€์‚ฌ ๋ฐ ์žฌ๊ธฐ๋™ ๋ฐฉํ™”๋ฒฝ ํ•ด์ œ ์ฐธ๊ณ ๋ฌธํ—Œ ์‹œ์Šคํ…œ ๊ตฌ์„ฑ ์˜ˆ์‹œ ์šฐ๋ถ„ํˆฌ 16.04 Django nginx Gunicorn ๋‹ค์Œ ์„ค์น˜ ์˜ˆ์ œ๋Š” ์‹ค์ œ ๋ฌผ๋ฆฌ ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ vultr.com์˜ ๊ฐ€์ƒ ์„œ๋ฒ„ ์ธ์Šคํ„ด์Šค์—์„œ ์ง„ํ–‰ํ•˜์˜€๋‹ค. ์˜ˆ์ œ์—์„œ ์‹œ์Šคํ…œ ๊ณ„์ • ์ด๋ฆ„์€ foo, ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์€ django_test๋กœ ๊ฐ€์ •ํ•œ๋‹ค. Django ๊ฐ€์ƒํ™˜๊ฒฝ ์ค€๋น„ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์„ค์น˜ํ•˜๊ณ  ํ™œ์„ฑํ™”ํ•œ๋‹ค. cd ~ mkdir django_test cd django_test pyvenv venv source venv/bin/activate (venv) ํŒจํ‚ค์ง€ ์„ค์น˜ ๋…๋ฆฝ๋œ ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ Django๋ฅผ ๋น„๋กฏํ•œ ํ•„์š”ํ•œ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ๋‹ค. pip install Django gunicorn ํ”„๋กœ์ ํŠธ/์ €์žฅ์†Œ ์ƒ์„ฑ mkdir repo run sudo chown foo:www-data run cd repo django-admin.py startproject conf . STATIC_ROOT ๋””๋ ‰ํ„ฐ๋ฆฌ ์ง€์ • conf/settings.py ํŒŒ์ผ์˜ ๋์— ๋‹ค์Œ ์ค„์„ ์ถ”๊ฐ€ํ•œ๋‹ค. STATIC_ROOT = os.path.join(BASE_DIR, 'static/') ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ./manage.py makemigrations ./manage.py migrate admin ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ ./manage.py createsuperuser static ๋ฐ์ดํ„ฐ ๋ชจ์œผ๊ธฐ ./manage.py collectstatic ํ…Œ์ŠคํŠธ ์„œ๋ฒ„ ๊ตฌ๋™ ๋ฐ ์ ‘์† ํ™•์ธ ./manage.py runserver 0.0.0.0:8000 http://์„œ๋ฒ„_์•„์ดํ”ผ:8000/ ์ฃผ์†Œ๋กœ ์ ‘์†ํ•˜์—ฌ ๋‚ด์šฉ์ด ์ž˜ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. Gunicorn ๊ตฌ๋™ ํ…Œ์ŠคํŠธ gunicorn --bind 0.0.0.0:8000 conf.wsgi:application ํŒŒ์ด์ฌ WSGI HTTP ์„œ๋ฒ„ Gunicorn์œผ๋กœ ๊ตฌ๋™ํ•˜์—ฌ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ http://์„œ๋ฒ„_์•„์ดํ”ผ:8000/ ์ฃผ์†Œ์— ์ ‘์†ํ•˜์—ฌ ๋‚ด์šฉ์ด ์ž˜ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. Gunicorn ์„œ๋ฒ„๊ฐ€ ์˜ฌ๋ฐ”๋กœ ๋™์ž‘ํ•˜๋ฉด ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ ์ด์ œ ๋น ์ ธ๋‚˜์˜จ๋‹ค. deatviate ์„œ๋น„์Šค ๋“ฑ๋ก ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ /etc/systemd/system/gunicorn.service ํŒŒ์ผ์„ ์•„๋ž˜์™€ ๊ฐ™์€ ๋‚ด์šฉ์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค. [Unit] Description=gunicorn daemon After=network.target [Service] User=foo Group=www-data WorkingDirectory=/home/foo/django_test/repo ExecStart=/home/foo/django_test/venv/bin/gunicorn \ --workers 3 \ --bind unix:/home/foo/django_test/run/gunicorn.sock \ conf.wsgi:application [Install] WantedBy=multi-user.target Gunicorn ์„œ๋น„์Šค ๋“ฑ๋ก sudo systemctl start gunicorn sudo systemctl enable gunicorn Gunicorn ์„œ๋น„์Šค ๊ตฌ๋™ ํ™•์ธ systemctl status gunicorn ๋งŒ์•ฝ ๊ตฌ๋™ ์‹คํŒจ ์‹œ ์—๋Ÿฌ ๋กœ๊ทธ๋Š” /var/log/syslog์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. nginx ์‚ฌ์ดํŠธ ์„ค์ • ์ถ”๊ฐ€ /etc/nginx/sites-available/django_test ํŒŒ์ผ์„ ์•„๋ž˜์™€ ๊ฐ™์€ ๋‚ด์šฉ์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค. server { listen 80; server_name 128.199.161.10; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/foo/django_test/repo; } location / { include proxy_params; proxy_pass http://unix:/home/foo/django_test/run/gunicorn.sock; } } ์‚ฌ์ดํŠธ ์ถ”๊ฐ€ sudo ln -s /etc/nginx/sites-available/django_test /etc/nginx/sites-enabled nginx ์„ค์ • ๋ฌธ๋ฒ• ๊ฒ€์‚ฌ ๋ฐ ์žฌ๊ธฐ๋™ sudo nginx -t sudo systemctl restart nginx ๋ฐฉํ™”๋ฒฝ ํ•ด์ œ sudo ufw delete allow 8000 sudo ufw allow 'Nginx Full' ์ฐธ๊ณ ๋ฌธํ—Œ How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 16.04 31์žฅ ์žฅ๊ณ  ํ”„๋กœ์ ํŠธ ๋ฐฐํฌํ•˜๊ธฐ - 31.1 ์ž‘์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•œ ๋‹จ์ผ ์„œ๋ฒ„์˜ ๊ฒฝ์šฐ / Two Scoops of Django 04) SSL ์„ค์น˜ SSL ์„ค์น˜ 05) ์ด๋ฉ”์ผ ์„œ๋ฒ„ ์—ฐ๋™ ์˜ˆ์ œ gmail ์—ฐ๋™ ์˜ˆ์ œ gmail ์—ฐ๋™ ๋Œ€์šฉ๋Ÿ‰ ์ด๋ฉ”์ผ ๋ฐœ์†ก์ด ์•„๋‹Œ ๋น„์ƒ์—…์ ์ธ ์šฉ๋„๋กœ ๊ฐœ๋ฐœ ํ…Œ์ŠคํŠธ์—๋Š” gmail ๋ฐœ์†ก ์ •๋„๋ฉด ์ถฉ๋ถ„ํ•˜๋‹ค. EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = 'user name@gmail.com ๋˜๋Š” user name@example.com' EMAIL_HOST_PASSWORD = '์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ' EMAIL_USE_TLS = True ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ EMAIL_HOST_USER ์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” gmail ๊ณ„์ •์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ ์–ด์ฃผ๋˜ EMAIL_HOST_PASSWORD ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์›น์—์„œ ๋กœ๊ทธ์ธํ•˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ด€๋ฆฌ ํŽ˜์ด์ง€์—์„œ ๋ณ„๋„๋กœ ์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ด๋ฅผ ์ ์–ด์ค€๋‹ค. ๊ฐœ๋ฐœ ๋ฐ ํ…Œ์ŠคํŠธ ์šฉ๋„๋กœ ์ด๋ฉ”์ผ ๋ฐœ์†กํ•  ๋•Œ ์•„๋ž˜ ์ฝ”๋“œ๋กœ ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. send_mail('Subject here', 'Here is the message.', 'user name@gmail.com', ['abc@example.com'], fail_silently=False) ํ”ผ์‹ฑ ์‚ฌ๊ธฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ณด๋‚ด๋Š” ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ user name@gmail.com ์•„๋‹Œ ๋‹ค๋ฅธ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋กœ ์ง€์ •ํ•˜๋”๋ผ๋„ gmail์—์„œ ๋ฌด์กฐ๊ฑด ๊ตฌ๊ธ€ ๊ณ„์ •์œผ๋กœ ์น˜ํ™˜ํ•˜์—ฌ ๋ณด๋‚ธ๋‹ค. 06) ๋ฐฐํฌ ์ž๋™ํ™” Invoke๋Š” ํŒŒ์ด์ฌ 3 ํ˜ธํ™˜์œผ๋กœ Fabric ๋Œ€์‹  ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 13. ์„ฑ๋Šฅ 01) ๋กœ๊ทธ์™€ ํ”„๋กœํŒŒ์ผ๋ง ๋กœ๊น… settings.py ํŒŒ์ผ ๋กœ๊น… Django์—์„œ ๋กœ๊ทธ ๋ ˆ๋ฒจ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜๋‰œ๋‹ค. DEBUG: ๋””๋ฒ„๊น… ๋ชฉ์ ์œผ๋กœ ๋กœ์šฐ ๋ ˆ๋ฒจ ์‹œ์Šคํ…œ ์ •๋ณด INFO: ์ผ๋ฐ˜์  ์‹œ์Šคํ…œ ์ •๋ณด WARNING: ๊ฒฝ๋ฏธํ•œ ๋ฌธ์ œ ๋ฐœ์ƒ์„ ์•Œ๋ฆฌ๋Š” ์ •๋ณด ERROR: ์ค‘์š”ํ•œ ๋ฌธ์ œ ๋ฐœ์ƒ์„ ์•Œ๋ฆฌ๋Š” ์ •๋ณด CRITICAL: ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๋ฅผ ์•Œ๋ฆฌ๋Š” ์ •๋ณด settings.py ํŒŒ์ผ LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', } }, 'loggers': { 'django.db.backends': { 'handlers': ['console'], 'level': 'DEBUG', }, 'your_app': { 'handlers': ['console'], 'level': 'DEBUG', }, } } 02) ์บ์‹ฑ ์บ์‹œ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๋ฉ”๋ชจ๋ฆฌ ์บ์‹œ ํ…œํ”Œ๋ฆฟ ๋ถ€๋ถ„ ์บ์‹œ ์บ์‹œ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๋ฉ”๋ชจ๋ฆฌ ์บ์‹œ CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'unique-snowflake', 'TIMEOUT': 300, # ๊ธฐ๋ณธ๊ฐ’ 300์ดˆ = 5๋ถ„ 'OPTIONS': { 'MAX_ENTRIES': 300 # ๊ธฐ๋ณธ๊ฐ’ = 300 } } } LocMemCache ๋ฉ”๋ชจ๋ฆฌ ์บ์‹œ๋ฅผ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ…œํ”Œ๋ฆฟ ๋ถ€๋ถ„ ์บ์‹œ ์บ์‹œ ํ•  ๋‚ด์šฉ์„ ํ†ต์งธ๋กœ cache ํƒœ๊ทธ๋กœ ๊ฐ์‹ผ๋‹ค. {% load cache %} {% cache 600 sidebar %} .. sidebar .. {% endcache %} ์œ„์™€ ๊ฐ™์ด cache ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ๋ฉด ์—ฌ๋Ÿฌ ๋ฒˆ ์š”์ฒญ์ด ๋“ค์–ด์™€๋„ 600์ดˆ๊ฐ„ ๊ทธ ์•ˆ์˜ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์งˆ์˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. 14. ๋ฐฐํฌ ๊ฐ€๋Šฅํ•œ ์žฌ์‚ฌ์šฉ ์•ฑ 01) ํŒŒ์ด์ฌ ๋ฐฐํฌํŒ ํŒจํ‚ค์ง• ๋ฐฐํฌํŒ ํ”„๋กœ๊ทธ๋žจ ๊ธฐ๋ณธ ๊ตฌ์กฐ ํŒŒ์ด์ฌ ๋ชจ๋“ˆ/ํŒจํ‚ค์ง€ ์ด๋ฆ„ ๊ทœ์น™ ์˜ˆ์ œ ์†Œ์Šค์ฝ”๋“œ sample ํŒจํ‚ค์ง€์˜ hello() ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‚ฌ์šฉ pip ๋ช…๋ น์–ด ์„ค์น˜ ๋ฐ ์‚ญ์ œ ๊ด€๋ฆฌ ๋ฐฐํฌํŒ์˜ ์„ค์น˜ sample ํŒจํ‚ค์ง€์˜ hello() ํ•จ์ˆ˜ ํ˜ธ์ถœ ํŒจํ‚ค์ง€์˜ hello() ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฐํฌํŒ ๋นŒ๋“œ ๋ฐฐํฌํŒ์˜ ์‚ญ์ œ Setuptools ๋ชจ๋“ˆ setup() ํ•จ์ˆ˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์˜ต์…˜ MANIFEST.in ํŒŒ์ผ ์š”์•ฝ ์ฐธ๊ณ  ๋ฌธ์„œ ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ์ด ๋ฌธ์„œ์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์šฉ์–ด๋ฅผ ์ •์˜ํ•œ๋‹ค. ๋ฐฐํฌํŒ: pip์œผ๋กœ ์„ค์น˜ ๊ฐ€๋Šฅํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํŒจํ‚ค์ง€: ํŒŒ์ด์ฌ ๋ชจ๋“ˆ๊ณผ __init.py ํŒŒ์ผ์ด ์ด ๋“ค์–ด์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ชจ๋“ˆ: ํŒŒ์ด์ฌ. py ์†Œ์Šค ํŒŒ์ผ ์‹ค์ œ๋กœ๋Š” ๋ฐฐํฌํŒ์ด๋ผ๋Š” ํ‘œํ˜„ ๋Œ€์‹ ์— ํŒจํ‚ค์ง€๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜์ง€๋งŒ ์ด๋ฒˆ ๋ฌธ์„œ์—์„œ๋Š” ํ˜ผ๋™์„ ํ”ผํ•˜๊ณ  ๋ช…ํ™•ํžˆ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. ๋ฐฐํฌํŒ ํ”„๋กœ๊ทธ๋žจ ๊ธฐ๋ณธ ๊ตฌ์กฐ ์ตœ์†Œํ•œ์˜ ๋ฐฐํฌํŒ ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. packaging-tutorial/ sample/ __init__.py greeting.py setup.py ๊ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ํŒŒ์ผ์€ ๋‹ค์Œ ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š”๋‹ค. packaging-tutorial/: packaging-tutorial.git ๊ฐ™์€ ์ €์žฅ์†Œ ๋””๋ ‰ํ„ฐ๋ฆฌ์ด๋‹ค. packaging-tutorial/sample/: ํŒŒ์ด์ฌ ์˜ˆ์ œ ํŒจํ‚ค์ง€๋ฅผ ์œ„ํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ์ด๋‹ค. packaging-tutorial/sample/__init__.py: ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ํŒจํ‚ค์ง€์ž„์„ ํŒŒ์ด์ฌ์—๊ฒŒ ์•Œ๋ ค์ฃผ๋Š” ํŒŒ์ผ์ด๋‹ค. packaging-tutorial/sample/greeting.py: sample ํŒจํ‚ค์ง€์— ์†ํ•˜๋Š” greeting ๋ชจ๋“ˆ/ํŒŒ์ผ์ด๋‹ค. packaging-tutorial/setup.py: ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ ํŒจํ‚ค์ง€๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ€์žฅ ํ•ต์‹ฌ์ ์ธ ํŒŒ์ผ์ด๋‹ค. ํŒŒ์ด์ฌ ๋ชจ๋“ˆ/ํŒจํ‚ค์ง€ ์ด๋ฆ„ ๊ทœ์น™ ํŒŒ์ด์ฌ ๋ชจ๋“ˆ/ํŒจํ‚ค์ง€์˜ ์ด๋ฆ„ ๊ทœ์น™์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ์ž‘์„ฑํ•œ๋‹ค. pypi์—์„œ ์œ ์ผํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ์–ด๋Š” -(ํ•˜์ดํ”ˆ)์ด ์•„๋‹ˆ๋ผ _(๋ฐ‘์ค„)๋กœ ๋„์–ด ์“ด๋‹ค. ์ €์žฅ์†Œ ์ด๋ฆ„์€ packaging-tutorial๊ณผ ๊ฐ™์ด ํ•˜์ดํ”ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ํŒจํ‚ค์ง€์˜ ์ด๋ฆ„์€ sample๊ณผ ๊ฐ™์ด ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ์ž‘์„ฑํ•œ๋‹ค. ์˜ˆ์ œ ์†Œ์Šค์ฝ”๋“œ packaging-tutorial/sample/__init__.py ํŒŒ์ผ ์•„๋ฌด๋Ÿฐ ๋‚ด์šฉ์ด ์—†๋Š” ๋นˆ ํŒŒ์ผ์ด๋‹ค. ํŒŒ์ผ ์ด๋ฆ„๋งŒ __init__.py๋กœ ๋งž์ถฐ์ค€๋‹ค. packaging-tutorial/sample/greeting.py ํŒŒ์ผ def hello(): print('Hello world.') ๋‹จ์ˆœํžˆ 'Hello world' ๋ฌธ์ž์—ด๋งŒ ์ถœ๋ ฅํ•˜๋Š” hello() ํ•จ์ˆ˜ ํ•˜๋‚˜๋งŒ ์ •์˜๋˜์–ด ์žˆ๋‹ค. packaging-tutorial/setup.py from setuptools import setup setup(name='packaging-tutorial', version='0.1', packages=['sample'], ) ํŒจํ‚ค์ง€์— ๊ด€ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ถ€๊ฐ€ ์ •๋ณด๊ฐ€ ์—†์ด ํŒจํ‚ค์ง€ ๋นŒ๋“œ์— ํ•„์š”ํ•œ ํ•ต์‹ฌ ์†์„ฑ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•˜์—ฌ setup() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. name ์†์„ฑ: ๋ฐฐํฌํŒ์˜ ์ด๋ฆ„์œผ๋กœ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฐฐํฌํŒ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด PyPI์— ๋“ฑ๋ก๋œ ์™ธ๋ถ€ ๋ฐฐํฌํŒ ๋˜๋Š” ๋‚ด๋ถ€ ํ”„๋กœ์ ํŠธ์˜ ๋ฐฐํฌํŒ ์ด๋ฆ„๊ณผ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ด๋ฆ„์„ ์ง“๋Š”๋‹ค. ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š๊ณ  ์œ ์ผํ•œ ์ด๋ฆ„์„ ์ง“๋Š”๋‹ค. version ์†์„ฑ: ์ดˆ๊ธฐ ๋ฒ„์ „์œผ๋กœ 0.1๋กœ ์ง€์ •ํ•˜์˜€๋‹ค. packages ์†์„ฑ: ๋ฐฐํฌํŒ์— ํฌํ•จ์‹œํ‚ฌ ํŒจํ‚ค์ง€(๋””๋ ‰ํ„ฐ๋ฆฌ) ๋ชฉ๋ก์„ ๋‚˜์—ดํ•œ๋‹ค. ์ตœ์ƒ์œ„ ํŒจํ‚ค์ง€๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ•˜์œ„ ํŒจํ‚ค์ง€๋“ค๊นŒ์ง€ ๋ชจ๋‘ ๋‚˜์—ดํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•œ๋‹ค. ํŒจํ‚ค์ง€ ์•ˆ์˜ ๋ชจ๋“ˆ(ํŒŒ์ผ)์„ ๋‚˜์—ดํ•  ํ•„์š”๋Š” ์—†๋‹ค. sample ํŒจํ‚ค์ง€์˜ hello() ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‚ฌ์šฉ packaging-tutorial ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—์„œ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ นํ•˜์—ฌ hello() ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. $ python Python 3.6.2 (default, Aug 20 2017, 17:24:58) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from sample.greeting import hello >>> hello() Hello world. >>> ๋งŒ์•ฝ packaging-tutorial ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์•„๋‹Œ ๊ณณ์—์„œ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ชจ๋“ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. >>> from sample.greeting import hello Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'sample' >>> pip ๋ช…๋ น์–ด ์„ค์น˜ ๋ฐ ์‚ญ์ œ ๊ด€๋ฆฌ ๋ฐฐํฌํŒ์˜ ์„ค์น˜ packaging-tutorial ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—์„œ pip install . ๋ช…๋ นํ•˜์—ฌ ๋ฐฐํฌํŒ์„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. $ pip install . Processing /home/m/Projects/packaging-tutorial Building wheels for collected packages: packaging-tutorial Running setup.py bdist_wheel for packaging-tutorial ... done Stored in directory: /home/m/.cache/pip/wheels/a2/76/a3/e04ee8b8f9f8511822ef4052878bcdf28ad30e7a0c223f02ec Successfully built packaging-tutorial Installing collected packages: packaging-tutorial Successfully installed packaging-tutorial-0.1 pip ๋ฐฐํฌํŒ ์„ค์น˜ ํ™•์ธ์€ pip freeze ๋ช…๋ น์–ด๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค. $ pip freeze packaging-tutorial==0.1 sample ํŒจํ‚ค์ง€์˜ hello() ํ•จ์ˆ˜ ํ˜ธ์ถœ ํŒจํ‚ค์ง€์˜ hello() ํ•จ์ˆ˜ ํ˜ธ์ถœ ์•ž์„œ ๋ฐฐํฌํŒ์„ ์„ค์น˜ํ•˜๊ธฐ ์ „์—๋Š” packaging-tutorial ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—์„œ๋งŒ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ hello() ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ packaging-tutorial ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ–ˆ๊ธฐ ๋Œ€๋ฌธ์— ํ•ด๋‹น ํŒŒ์ด์ฌ ํ™˜๊ฒฝ์—์„œ๋Š” ์–ด๋Š ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ๋‚˜ hello() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐํฌํŒ ๋นŒ๋“œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ๋Š” ๋ฐฐํฌํŒ ์†Œ์Šค์ฝ”๋“œ์—์„œ pip install . ๋ช…๋ น์œผ๋กœ ๋ฐ”๋กœ ๋ฐฐํฌํŒ์„ ์„ค์น˜ํ–ˆ์ง€๋งŒ ์ง„์ •ํ•œ ์˜๋ฏธ์˜ ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด python setup.py sdist ๋ช…๋ น์–ด๋กœ ๋ฐฐํฌํŒ์„ ๋นŒ๋“œ ํ•  ์ˆ˜ ์žˆ๋‹ค. $ python setup.py sdist running sdist running egg_info creating packaging_tutorial.egg-info writing packaging_tutorial.egg-info/PKG-INFO writing dependency_links to packaging_tutorial.egg-info/dependency_links.txt writing top-level names to packaging_tutorial.egg-info/top_level.txt writing manifest file 'packaging_tutorial.egg-info/SOURCES.txt' reading manifest file 'packaging_tutorial.egg-info/SOURCES.txt' writing manifest file 'packaging_tutorial.egg-info/SOURCES.txt' warning: sdist: standard file not found: should have one of README, README.rst, README.txt running check warning: check: missing required meta-data: url creating packaging-tutorial-0.1 creating packaging-tutorial-0.1/packaging_tutorial.egg-info creating packaging-tutorial-0.1/sample copying files to packaging-tutorial-0.1... copying setup.py -> packaging-tutorial-0.1 copying packaging_tutorial.egg-info/PKG-INFO -> packaging-tutorial-0.1/packaging_tutorial.egg-info copying packaging_tutorial.egg-info/SOURCES.txt -> packaging-tutorial-0.1/packaging_tutorial.egg-info copying packaging_tutorial.egg-info/dependency_links.txt -> packaging-tutorial-0.1/packaging_tutorial.egg-info copying packaging_tutorial.egg-info/top_level.txt -> packaging-tutorial-0.1/packaging_tutorial.egg-info copying sample/__init__.py -> packaging-tutorial-0.1/sample copying sample/greeting.py -> packaging-tutorial-0.1/sample Writing packaging-tutorial-0.1/setup.cfg creating dist Creating tar archive removing 'packaging-tutorial-0.1' (and everything under it) ์ด์ œ dist ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— packaging-tutorial-0.1.tar.gz ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค. ์†Œ์Šค ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—์„œ pip install.๋กœ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฐฐํฌํŒ ์••์ถ• ํŒŒ์ผ์„ ์ง€์ •ํ•˜์—ฌ ์„ค์น˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. pip install dist/packaging-tutorial-0.1.tar.gz ๋ฐฐํฌํŒ์˜ ์‚ญ์ œ pip uninstall ๋ช…๋ น์–ด๋กœ ๋ฐฐํฌํŒ์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. $ pip uninstall packaging-tutorial Uninstalling packaging-tutorial-0.1: /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/packaging_tutorial-0.1.dist-info/DESCRIPTION.rst /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/packaging_tutorial-0.1.dist-info/INSTALLER /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/packaging_tutorial-0.1.dist-info/METADATA /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/packaging_tutorial-0.1.dist-info/RECORD /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/packaging_tutorial-0.1.dist-info/WHEEL /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/packaging_tutorial-0.1.dist-info/metadata.json /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/packaging_tutorial-0.1.dist-info/top_level.txt /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/sample/__init__.py /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/sample/__pycache__/__init__.cpython-36.pyc /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/sample/__pycache__/greeting.cpython-36.pyc /home/m/.pyenv/versions/3.6.2/envs/venv/lib/python3.6/site-packages/sample/greeting.py Proceed (y/n)? y Successfully uninstalled packaging-tutorial-0.1 Setuptools ๋ชจ๋“ˆ setup() ํ•จ์ˆ˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์˜ต์…˜ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ํฌํ•จ๋œ Distutils ๋ชจ๋“ˆ์„ ํ†ตํ•ด setup.py ํŒŒ์ผ์— ๋”ฐ๋ผ ๋ฐฐํฌํŒ์ด ์„ค์น˜๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ distutils.core ํŒจํ‚ค์ง€์˜ setup() ํ•จ์ˆ˜๋ฅผ ์“ธ ์ˆ˜ ์žˆ์ง€๋งŒ ๋งŽ์€ ๊ฐœ๋ฐœ์ž๊ฐ€ setuptools ํŒจํ‚ค์ง€์˜ setup() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” setup() ํ•จ์ˆ˜์— ๋„˜๊ธธ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ์ ์ธ ํŒŒ๋ผ๋ฏธํ„ฐ ์™ธ์— ๋‹ค์–‘ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ดํŽด๋ณธ๋‹ค. packaging-tutorial/setup.py ํŒŒ์ผ import os from setuptools import setup, find_packages with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme: README = readme.read() setup(name='packaging-tutorial', version='0.1', packages=find_packages(exclude=['docs', 'tests']), description='Example package for packaging tutorial', long_description=README, url='https://www.example.com/', author='John Doe', author_email='test' '@' 'exmaple.com', license='MIT', classifiers=[ 'Development Status :: 3 - Alpha', 'Intended Audience :: Developers', 'Topic :: Software Development :: Build Tools', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', ], install_requires=[ 'foo', 'bar', ], scripts=['manage.py'], zip_safe=False, ) ๋‹ค์Œ ์†์„ฑ์€ ๋ฐฐํฌํŒ ๋ฉ”ํƒ€ ์ •๋ณด๋กœ์„œ ๋ฐฐํฌํŒ ๋นŒ๋“œ ์ž์ฒด์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์†์„ฑ์€ ์•„๋‹ˆ๋‹ค. description ์†์„ฑ: ๋ฐฐํฌํŒ์— ๋Œ€ํ•œ ์„ค๋ช… long_description ์†์„ฑ: ๋ฐฐํฌํŒ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ README.md ํŒŒ์ผ์˜ ๋‚ด์šฉ์œผ๋กœ ๋ณดํ†ต์€ ์˜ˆ์‹œ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•œ๋‹ค. ์•„๋ž˜์—์„œ ๋‹ค์‹œ ์–ธ๊ธ‰ํ•˜์ง€๋งŒ README.md ํŒŒ์ผ์„ ํ•จ๊ป˜ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋„๋ก MANIFEST.in ํŒŒ์ผ์— ๊ธฐ์ˆ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์‹ค์ œ ๋ฐฐํฌํ•  ๋•Œ README.md ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์–ด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. url ์†์„ฑ: ๋ฐฐํฌํŒ ํ™ˆํŽ˜์ด์ง€ author ์†์„ฑ: ๋ฐฐํฌํŒ ์ž‘์„ฑ์ž ์ด๋ฆ„ author_email ์†์„ฑ: ๋ฐฐํฌํŒ ์ž‘์„ฑ์ž ์ด๋ฉ”์ผ ์ฃผ์†Œ, ๋ฉ”์ผ ์ˆ˜์ง‘ํ•ด ์ŠคํŒธ ๋ฉ”์ผ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์œ„์™€ ๊ฐ™์ด ์ ๋‹นํžˆ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐํ•˜์—ฌ ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ๋งŒ๋“ ๋‹ค. license ์†์„ฑ: ๋ฐฐํฌํŒ์˜ ๋ผ์ด์„ ์Šค classifiers ์†์„ฑ: PyPI ๋“ฑ๋ก์„ ์œ„ํ•ด ๋ฐฐํฌํŒ ๋ถ„๋ฅ˜ ์ •๋ณด์ด๋‹ค. ๊ณต์‹ ๋ฌธ์„œ์—์„œ ๊ฐ€๋Šฅํ•œ ๊ฐ’์˜ ๋ชฉ๋ก์„ ์ฐธ๊ณ ํ•œ๋‹ค. ๋‹ค์Œ ์†์„ฑ์€ ๋ฐฐํฌํŒ ๋นŒ๋“œ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์†์„ฑ์œผ๋กœ ์ •ํ™•ํžˆ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. packages ์†์„ฑ: setuptools๋ฅผ ์“ฐ๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ด์œ  ์ค‘์— ํ•˜๋‚˜๊ฐ€ ๋ฐ”๋กœ find_packages() ํ•จ์ˆ˜์˜ ๊ธฐ๋Šฅ์ด๋‹ค. ์ง์ ‘ ์„ค์น˜ํ•  ํŒจํ‚ค์ง€ ๋ชฉ๋ก์„ ๋‚˜์—ดํ•œ๋‹ค๋ฉด ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ํŒจํ‚ค์ง€๊นŒ์ง€ ๋‚˜์—ดํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ find_package() ํ•จ์ˆ˜๊ฐ€ ์ฒ˜๋ฆฌํ•ด ์ค€๋‹ค. ์ด๋•Œ docs, tests ๊ฐ™์ด ํฌํ•จ๋˜์ง€ ์•Š์•„์•ผ ํ•  ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ช…์‹œ์ ์œผ๋กœ exclude=['docs', 'tests'] ๊ฐ™์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ฒจ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. install_requires ์†์„ฑ: ๋ฐฐํฌํŒ์ด ํ•„์š”๋กœ ํ•˜๋Š” ํŒจํ‚ค์ง€ ๋ชฉ๋ก scripts ์†์„ฑ: ๋ฐฐํฌํŒ์— ํฌํ•จ๋  ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ๋ชฉ๋ก (์˜ˆ๋ฅผ ๋“ค์–ด, Django ํ”„๋กœ์ ํŠธ์˜ manage.py ํŒŒ์ผ์„ ๊ฐ™์ด ๋ฐฐํฌํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ) zip_safe ์†์„ฑ: zip ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด์„œ ์‹คํ–‰ํ•ด๋„ ํŒจํ‚ค์ง€๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ MANIFEST.in ํŒŒ์ผ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ ์™ธ์˜ ํŒŒ์ผ๋„ ๋ฐฐํฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์–ด๋–ค ํŒŒ์ผ/๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ํ•จ๊ป˜ ๋ฐฐํฌํ•ด์•ผ ํ• ์ง€ MANIFEST.in ํŒŒ์ผ์— ๊ธฐ์ˆ ํ•œ๋‹ค. MANIFEST.in ํŒŒ์ผ ์˜ˆ์‹œ include LICENSE include README.md recursive-include your_app/static * recursive-include your_app/templates * global-exclude conf/settings/secret.py ์œ„ ์˜ˆ์‹œ์—์„œ include ์ง€์‹œ์–ด๋กœ LICENSE, README.md ํŒŒ์ผ์„ ๋ฐฐํฌํŒ์— ํฌํ•จํ•œ๋‹ค. recursive-include ์ง€์‹œ์–ด๋กœ your_app/static, your_app/templates ๊ฐ™์€ ํŠน์ • ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋‚ด์šฉ์„ ์žฌ๊ท€์ ์œผ๋กœ ๋ฐฐํฌํŒ์— ์ „๋ถ€ ํฌํ•จ์‹œํ‚ค๋„๋ก ํ•œ๋‹ค. global-exclude ์ง€์‹œ์–ด๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ๋‚˜ ๊ณต๊ฐœ๋˜์ง€ ์•Š์•„์•ผ ํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์†Œ์Šค ํŒŒ์ผ์€ ๋ฌด์กฐ๊ฑด ํฌํ•จ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค. ์š”์•ฝ ๋ฐฐํฌํŒ ํŒจํ‚ค์ง•์„ ์œ„ํ•ด ์ž‘์„ฑํ•  ํŒŒ์ผ์€ ํฌ๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. setup.py MANIFEST.py README.md ์ฐธ๊ณ  ๋ฌธ์„œ How To Package Your Python Code pypa/sampleproject ์˜ˆ์‹œ ํ”„๋กœ์ ํŠธ 02) Django ํ”„๋กœ์ ํŠธ ๋ฐ ์•ฑ ํŒจํ‚ค์ง• Django ํ”„๋กœ์ ํŠธ ํŒจํ‚ค์ง• ๋ฐฐํฌํŒ ๋งŒ๋“ค๊ธฐ ๋ฐฐํฌํŒ ์„ค์น˜ ๋ฐฐํฌํŒ ์‹คํ–‰ Django ์•ฑ ํŒจํ‚ค์ง• Django ํ”„๋กœ์ ํŠธ ํŒจํ‚ค์ง• ๋ฐฐํฌํŒ ๋งŒ๋“ค๊ธฐ Django ํ”„๋กœ์ ํŠธ๋ฅผ ํŒจํ‚ค์ง• ํ•˜๋Š” ๊ฒƒ์€ ์ƒ๊ฐ์ฒ˜๋Ÿผ ์œ ์šฉํ•œ ์ผ์ด์ง„ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. ์ด์œ ๋Š” Django ํ”„๋กœ์ ํŠธ๋Š” ์‚ฌ์ดํŠธ๋งˆ๋‹ค ์„ค์ •๊ฐ’ ๋“ฑ์ด ๋‹ค ์ œ๊ฐ๊ฐ์ด์–ด์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋งŒ๋“ค์–ด๋„ ์žฌ์‚ฌ์šฉ์ด ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ๊ธฐ์„œ๋Š” ํ•™์Šต ์ฐจ์›์—์„œ Django ํ”„๋กœ์ ํŠธ๋ฅผ ์ง์ ‘ ํŒจํ‚ค์ง• ํ•ด๋ณธ๋‹ค. setup.py ํŒŒ์ผ ์˜ˆ์‹œ import os from setuptools import setup, find_packages with open(os.path.join(os.path.dirname(__file__), 'README.md')) as readme: README = readme.read() setup(name='django-quickstarter', version='0.1', packages=find_packages(exclude=[ 'docs', ]), description='Django-based project boilerplate that follows best practices', long_description=README, url='https://www.example.com/', author='John Doe', author_email='test' '@' 'exmaple.com', license='MIT', classifiers=[ 'Development Status :: 3 - Alpha', 'Framework :: Django', 'Intended Audience :: Developers', 'Topic :: Software Development :: Build Tools', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', ], install_requires=[ 'Django', 'django-crispy-forms', ], setup_requires=[ ], scripts=[ 'manage.py', ], ) MANIFEST.in ํŒŒ์ผ include LICENSE include README.md recursive-include requirements * recursive-include conf/static * recursive-include conf/templates * global-exclude conf/settings/secret.py ๋ฐฐํฌํŒ ์„ค์น˜ pip install ๋ช…๋ น์–ด๋กœ Django ํ”„๋กœ์ ํŠธ์˜ ๋ฐฐํฌํŒ์„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. $ pip install django-quickstarter-0.1.tar.gz ๋ฐฐํฌํŒ ์‹คํ–‰ Django ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ settings.py ํŒŒ์ผ์—๋Š” ๋น„๋ฐ€ํ‚ค ๊ฐ™์€ ๋น„๊ณต๊ฐœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ํŒจํ‚ค์ง• ํ•  ๋•Œ ํฌํ•จํ•˜๊ธฐ๊ฐ€ ๊ณค๋ž€ํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ์ง์ ‘ ~/.pyenv/versions/3.6.2/envs/venv-test/lib/python3.6/site-packages/conf/settings/secret.py ๊ฐ™์€ ๋น„๊ณต๊ฐœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํŒŒ์ผ์„ ์ˆ˜๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค. ๋˜ํ•œ ์‹คํ–‰ํ•˜๋”๋ผ๋„ ์„ค์ •๊ฐ’์˜ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ์—๋Š” ์ง์ ‘ ~/.pyenv/versions/3.6.2/envs/venv-test/lib/python3.6/site-packages/conf/settings/production.py ๊ฐ™์€ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. Django ์•ฑ ํŒจํ‚ค์ง• 03) tox์™€ coverage tox pyenv tox.ini ํŒŒ์ผ ์‹คํ–‰ travis coverage tox pyenv pyenv์™€ ๊ฐ€์ƒํ™˜๊ฒฝ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค. tox.ini ํŒŒ์ผ tox-quickstart ๋ช…๋ น์–ด๋กœ tox.ini ํŒŒ์ผ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๊ณต์‹ ๋ฌธ์„œ์—์„œ ์†Œ๊ฐœํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ tox.ini ํŒŒ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค: [tox] envlist = py27, py34, py35, py36 [testenv] commands = pytest deps = pytest [tox] ์„น์…˜์˜ envlist ๋ณ€์ˆ˜๋Š” ํ…Œ์ŠคํŠธํ•˜๋ ค๋Š” ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ๋‚˜์—ดํ•œ๋‹ค. ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” 2.7, 3.4, 3.5, 3.6 ๋ฒ„์ „์œผ๋กœ ํ…Œ์ŠคํŠธํ•  ๊ฒƒ์ด๋‹ค. [testenv] ์„น์…˜์˜ commands ๋ณ€์ˆ˜๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด์˜ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. pytest python setup.py test nosetests package.module trial package.module [testenv] ์„น์…˜์˜ deps ๋ณ€์ˆ˜๋กœ ํ…Œ์ŠคํŠธ ์‹คํ–‰์„ ์œ„ํ•ด ์˜์กด์„ฑ ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š” ํŒจํ‚ค์ง€๋ฅผ ๋‚˜์—ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” pytest ์‹คํ–‰ํ•˜๊ธฐ๋กœ ํ–ˆ์œผ๋ฏ€๋กœ pytest ํŒจํ‚ค์ง€๋ฅผ ์ž„์‹œ ์„ค์น˜ํ•˜์—ฌ ํ…Œ์ŠคํŠธ ์ง„ํ–‰ํ•œ๋‹ค. ์‹คํ–‰ travis coverage 99. ๋ถ€๋ก 01) ํŒŒ์ด์ฌ Django๋ฅผ ๊ณต๋ถ€ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ํŒŒ์ด์ฌ ๊ธฐ์ดˆ 02) vultr ์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐ ์„ค์ • ๊ธฐ๋ณธ ์ค€๋น„ ์‹œ์Šคํ…œ ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ ๋ฃจํŠธ ๋กœ๊ทธ์ธ ์ ‘์† ๊ธˆ์ง€ admin ๊ทธ๋ฃน์˜ ์‚ฌ์šฉ์ž๋งŒ su/sudo ๋ช…๋ น์–ด ์‚ฌ์šฉ ํ—ˆ๊ฐ€ ์„ค์น˜ ์ฃผ์š” ํŒจํ‚ค์ง€ ์„ค์น˜ ๊ธฐ๋ณธ ์ค€๋น„ ์ด๋Š” vultr.com์˜ ์ธ์Šคํ„ด์Šค ์„œ๋ฒ„์—์„œ ์„ค์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ์ ์ธ ์‹œ์Šคํ…œ ์‚ฌ์šฉ์ž๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๋ถ€๋ถ„์€ ์‹ค์ œ ์‹œ์Šคํ…œ ์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๋ฉด ์ƒ๋žต ๊ฐ€๋Šฅํ•œ ๊ณผ์ •์ด๋‹ค. ์‹œ์Šคํ…œ ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ foo ์ด๋ฆ„์˜ ์‹œ์Šคํ…œ ์‚ฌ์šฉ์ž๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. useradd -G admin -b /home -m -s /bin/bash foo passwd foo ๋ฃจํŠธ ๋กœ๊ทธ์ธ ์ ‘์† ๊ธˆ์ง€ foo ์‚ฌ์šฉ์ž๋Š” admin ๊ทธ๋ฃน์— ์†ํ•˜๋ฏ€๋กœ sudo ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฃจํŠธ ์ ‘์†์€ ๊ธˆ์ง€ํ•œ๋‹ค. passwd -d -l root usermod -s /bin/false root admin ๊ทธ๋ฃน์˜ ์‚ฌ์šฉ์ž๋งŒ su/sudo ๋ช…๋ น์–ด ์‚ฌ์šฉ ํ—ˆ๊ฐ€ ์‹œ์Šคํ…œ ๋ณด์•ˆ์ƒ admin ๊ทธ๋ฃน์˜ ์‚ฌ์šฉ์ž๋งŒ su/sudo ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. dpkg-statoverride --update --add root admin 4750 /bin/su dpkg-statoverride --update --add root admin 4750 /usr/bin/sudo ์„ค์น˜ ์ฃผ์š” ํŒจํ‚ค์ง€ ์„ค์น˜ ํŒจํ‚ค์ง€๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค. sudo apt-get update && sudo apt-get dist-upgrade ์ตœ์†Œํ•œ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ nginx์™€ python3-venv๋ฅผ ์„ค์น˜ํ•œ๋‹ค. sudo apt-get install python3-venv nginx ํ•„์š”์— ๋”ฐ๋ผ apt-get install ๋ช…๋ น์–ด๋กœ ๋‹ค์Œ ํŒจํ‚ค์ง€๋“ค์„ ์ถ”๊ฐ€๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. PostgreSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ จ ํŒจํ‚ค์ง€ postgresql libpq-dev postgresql-contrib PostgreSQL, uWSGI, Gunicorn ๋“ฑ์˜ ์—ฐ๋™์„ ์œ„ํ•œ ๋นŒ๋“œ ๊ด€๋ จ ํŒจํ‚ค์ง€ python3-dev build-essential ์šฐ๋ถ„ํˆฌ 16.04 ๋ฒ„์ „์—์„œ ํŒŒ์ด์ฌ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋ช…์‹œ์ ์œผ๋กœ python3 ๋ฒ„์ „์œผ๋กœ ๋ช…๋ นํ•ด์•ผ ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ํŒŒ์ด์ฌ ์„ค์น˜๋ฅผ ํ™•์ธํ•œ๋‹ค. python3 Python 3.5.2 (default, Jul 5 2016, 12:43:10) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 03) PyCharm ํ™œ์šฉ Django ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ํ•˜๊ธฐ PyCharm IDE ๊ธฐ๋ณธ๊ฐ’ ์ฃผ์š” ํ™œ์šฉ ์ฃผ์š” ๋‹จ์ถ•ํ‚ค ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ ํ•ด๊ฒฐ runserver ์‹คํ–‰ ์‹œ 0xC0000005 ์ข…๋ฃŒ ์ฝ”๋“œ Django ์ฝ˜์†” ์ฐฝ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋กœ๋”ฉ ์‹คํŒจ Django ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ํ•˜๊ธฐ PyCharm IDE ๊ธฐ๋ณธ๊ฐ’ ์‹คํ–‰ ๋ฐ ๋””๋ฒ„๊ทธ ์„ค์ • ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ Django ์ฝ˜์†” PyCharm Django ์ง€์› ์ฃผ์š” ํ™œ์šฉ ์ฃผ์š” ๋‹จ์ถ•ํ‚ค ์ž๋™ ์™„์„ฑ: Ctrl+Space ํŒŒ์ผ ์—ด๊ธฐ: Ctrl+Shift+N ๊ฒฝ๋กœ์—์„œ ์—ด๊ธฐ: Ctrl+Shift+F ๋น ๋ฅธ ๊ฒ€์ƒ‰: Shift ๋‘ ๋ฒˆ ์„ ์–ธ์œผ๋กœ ์ด๋™: Ctrl+B ์ฝ”๋“œ ํฌ๋งทํŒ…: Ctrl+Alt+L import ๊ตฌ๋ฌธ ์ •๋ฆฌ: Ctrl+Alt+O ํŒŒ์ผ ๋‹ซ๊ธฐ: Ctrl+F4 ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ ํ•ด๊ฒฐ runserver ์‹คํ–‰ ์‹œ 0xC0000005 ์ข…๋ฃŒ ์ฝ”๋“œ 2017.1.3 ์ด์ „ ๋ฒ„์ „์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด runserver ์‹คํ–‰ ์‹œ 0xC0000005 ์ข…๋ฃŒ ์ฝ”๋“œ๋กœ ํ…Œ์ŠคํŠธ ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ํ˜„์ƒ์ด ์žˆ์—ˆ๋‹ค. "C:\Program Files (x86)\JetBrains\PyCharm 143.19\bin\runnerw.exe" C:\Python\Python35\python.exe C:/Users/mairoo/PycharmProjects/django35/manage.py runserver 8000 Process finished with exit code -1073741819 (0xC0000005) ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋ฉ”๋‰ด์—์„œ Run > Edit Configurations ์„ ํƒ ํ›„ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋“ฑ๋กํ•œ๋‹ค. ์ด๋ฆ„(Name): =C: ๊ฐ’(Value): c:\" ์ถœ์ฒ˜: runnerw.exe sometimes finishes with exit code -1073741819 (0xC0000005) Django ์ฝ˜์†” ์ฐฝ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋กœ๋”ฉ ์‹คํŒจ ๊ฐœ๋ฐœ ์ค‘์— ํ”„๋กœ์ ํŠธ ์ด๋ฆ„, ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋ฆ„ ๋“ฑ์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  Django ์ฝ˜์†”์ด ์˜ฌ๋ฐ”๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์—์„œ ๋ณด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ File > Settings ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•ด ์„ค์ • ํŒ์—…์ด ๋œจ๋ฉด Build, Execution, Deployment > Console > Django Console ๋ฉ”๋‰ด์—์„œ DJANGO_SETTINGS_MODULE ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์•Œ๋งž์€ ๊ฐ’์œผ๋กœ ์žฌ์„ค์ •ํ•œ๋‹ค. 04) 12 ํŒฉํ„ฐ ์•ฑ (12 Factor App) ์ฝ”๋“œ ๋ฒ ์ด์Šค(Codebase) ์˜์กด์„ฑ(Dependencies) ์„ค์ •(Config) ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค(Backing Services) ๋นŒ๋“œ, ๋ฆด๋ฆฌ์Šค, ์‹คํ–‰(Build, Release, Run) ํ”„๋กœ์„ธ์Šค(Processes) ํฌํŠธ ๋ฐ”์ธ๋”ฉ(Port Binding) ๋™์‹œ์„ฑ(Concurrency) ํ๊ธฐ ๊ฐ€๋Šฅ(Disposability) ์šด์˜/๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์ผ์น˜(Dev/Prod Parity) ๋กœ๊ทธ(Logs) ๊ด€๋ฆฌ ํ”„๋กœ์„ธ์Šค(Admin Processes) ์ฐธ๊ณ ๋ฌธํ—Œ SaaS, PaaS ์•ฑ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•๋ก  ์ฝ”๋“œ ๋ฒ ์ด์Šค(Codebase) ์ฝ”๋“œ๋Š” ํ•œ๊ณณ์—์„œ ๊ด€๋ฆฌํ•˜๊ณ  Git ์‚ฌ์šฉ์„ ์ถ”์ฒœํ•œ๋‹ค. ์ฝ”๋“œ๋Š” ์—ฌ๋Ÿฌ ๋Œ€์˜ ๋ถ„์‚ฐ์ฒ˜๋ฆฌ ์šด์˜์„œ๋ฒ„๋‚˜ ์—ฌ๋Ÿฌ ๊ฐœ๋ฐœ์ž์˜ ์ปดํ“จํ„ฐ๋กœ ๋ฐฐํฌ, ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋‹ค. ์˜์กด์„ฑ(Dependencies) ์˜์กด์„ฑ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” requirements.txt ๋“ฑ์— ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•œ๋‹ค. pip์€ ์ข…์†์„ฑ ์„ ์–ธ์— ์‚ฌ์šฉ๋˜๊ณ  virtualenv๋Š” ๋…๋ฆฝ๋œ ๊ฐ€์ƒํ™˜๊ฒฝ์œผ๋กœ ์‹œ์Šคํ…œ ์‚ฌ์ด์— ์ข…์†์„ฑ ๋ถ„๋ฆฌ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. curl, ImageMagick ๊ฐ™์€ ์‹œ์Šคํ…œ ์œ ํ‹ธ๋ฆฌํ‹ฐ์— ์˜์กดํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ชจ๋“  ์‹œ์Šคํ…œ์—์„œ ์กด์žฌํ•œ๋‹ค๊ณ  ๋ณด์žฅํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ์กด์žฌํ•˜๋”๋ผ๋„ 100% ํ˜ธํ™˜๋œ๋‹ค๊ณ  ๋ณด์žฅํ•  ์ˆ˜ ์—†๋‹ค. ์„ค์ •(Config) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ค์ •์€ ์šด์˜, ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๋“ฑ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋น„๋กฏํ•œ ์—ฌ๋Ÿฌ ์„œ๋ฒ„์˜ ์ ‘์† ์ •๋ณด, ๋น„๋ฐ€๋ฒˆํ˜ธ ๋“ฑ ์„ค์ •์„ ์ฝ”๋“œ์— ์ €์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฃผ์š” ์„ค์ •์€ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ €์žฅ์†Œ์— ์ปค๋ฐ‹ ๋˜์ง€ ์•Š๋Š” ํŒŒ์ผ์— ์ €์žฅํ•œ๋‹ค. ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค(Backing Services) ์ฝ”๋“œ๋Š” ๋กœ์ปฌ ์„œ๋น„์Šค์™€ ์„œ๋“œํŒŒํ‹ฐ ์„œ๋น„์Šค๋ฅผ ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด URI๋กœ ์ ‘๊ทผํ•œ๋‹ค. postgres://USER:PASSWORD@HOST:PORT/NAME mysql://USER:PASSWORD@HOST:PORT/NAME ๋นŒ๋“œ, ๋ฆด๋ฆฌ์Šค, ์‹คํ–‰(Build, Release, Run) ๋นŒ๋“œ, ๋ฆด๋ฆฌ์Šค, ์‹คํ–‰ ๋‹จ๊ณ„๋ฅผ ์—„๊ฒฉํžˆ ๊ตฌ๋ถ„ํ•œ๋‹ค. ๋นŒ๋“œ๋Š” ์†Œ์Šค๋ฅผ ์ปดํŒŒ์ผํ•˜๊ณ  ์ •์  ํŒŒ์ผ์„ ๊ตฌ์„ฑํ•ด์„œ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋ฒˆ๋“ค๋กœ ๋ณ€ํ™˜์‹œํ‚ค๋Š” ๊ฒƒ์ด๋‹ค. ๋ฆด๋ฆฌ์Šค๋Š” ์šด์˜ ๋˜๋Š” ๊ฐœ๋ฐœ์„œ๋ฒ„์— ๋ฐฐํฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์‹คํ–‰์€ ๋Ÿฐํƒ€์ž„ ํ™˜๊ฒฝ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์ด ๋™์ž‘ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ง‘ํ•ฉ์ด๋‹ค. ํ”„๋กœ์„ธ์Šค(Processes) ๋ฌด์ƒํƒœ ํ”„๋กœ์„ธ์Šค๋กœ ์‹คํ–‰๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค ์‚ฌ์ด์— ์•„๋ฌด๊ฒƒ๋„<NAME>์ง€ ์•Š๋Š”๋‹ค. ์œ ์ง€๊ฐ€ ํ•„์š”ํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ฒ˜๋Ÿผ ์•ˆ์ •๋œ ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค์— ์ €์žฅํ•œ๋‹ค. ์บ์‹œ ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌด์กฐ๊ฑด ์‹ ๋ขฐํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์„ธ์…˜ ๋ฐ์ดํ„ฐ๋ฅผ ์•ฑ์˜ ํ”„๋กœ์„ธ์Šค ๋ฉ”๋ชจ๋ฆฌ์— ์บ์‹ฑ ํ•œ ํ›„ ๊ฐ™์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ดํ›„์— ๋™์ผํ•œ ํ”„๋กœ์„ธ์Šค๋กœ ์š”์ฒญํ•  ๊ฑฐ๋ผ๊ณ  ๊ฐ€์ •ํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. Sticky Session์€ 12 Factor์— ์œ„๋ฐฐ๋œ๋‹ค. ํฌํŠธ ๋ฐ”์ธ๋”ฉ(Port Binding) 80 ํฌํŠธ๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜์—ฌ ์‹ค์ œ ์›น ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ๋™์‹œ์„ฑ(Concurrency) ๊ฐ™์€ ์ผ์„ ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ์ˆ˜์ง์ ์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋ฅธ ์ผ์„ ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ์ˆ˜ํ‰์ ์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ๊ธฐ ๊ฐ€๋Šฅ(Disposability) ํ”„๋กœ์„ธ์Šค๋Š” ๋น ๋ฅด๊ฒŒ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ํ”„๋กœ์„ธ์Šค๊ฐ€ ๊ฐ‘์ž๊ธฐ ์ฃฝ๋”๋ผ๋„ ์„œ๋น„์Šค ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. ์šด์˜/๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์ผ์น˜(Dev/Prod Parity) ๊ฐœ๋ฐœ ํ™˜๊ฒฝ๊ณผ ์šด์˜ํ™˜๊ฒฝ์„ ์ตœ๋Œ€ํ•œ ๋น„์Šทํ•˜๊ฒŒ ์œ ์ง€ํ•˜์—ฌ ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ์žฌ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ๋กœ๊ทธ(Logs) stdout ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ๋งŒ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ด๋Š” loggack/slf4j๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฐ” ๊ฐœ๋ฐœ์ž์—๊ฒŒ๋Š” ๋‹ค์†Œ ๋‚ฉ๋“ํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๋‹ค. ๊ด€๋ฆฌ ํ”„๋กœ์„ธ์Šค(Admin Processes) ์šด์˜ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— migrate ๊ฐ™์€ ๊ด€๋ฆฌ ์ž‘์—…์€ ์ผํšŒ์„ฑ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค. ๊ด€๋ฆฌ ์ฝ”๋“œ๋Š” ๋™๊ธฐํ™” ๋ฌธ์ œ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ๋ฐฐํฌ๋œ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ฐ’์„ ์ง์ ‘ ์ˆ˜์ •ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ์ฐธ๊ณ ๋ฌธํ—Œ The Twelve-Factor App 12-Factor Apps in Plain English https://dzone.com/articles/the-12-factor-app-a-java-developers-perspective 05) OWASP Top 10 ๋ณด์•ˆ ์œ„ํ—˜ Top 10 ์ธ์ ์…˜ ์ธ์ฆ ๋ฐ ์„ธ์…˜ ๊ด€๋ฆฌ ์ทจ์•ฝ์  ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ์Šคํฌ๋ฆฝํŒ…(XSS) ์ทจ์•ฝํ•œ ์ง์ ‘ ๊ฐ์ฒด ์ฐธ์กฐ ๋ณด์•ˆ ์„ค์ • ์˜ค๋ฅ˜ ๋ฏผ๊ฐ ๋ฐ์ดํ„ฐ ๋…ธ์ถœ ๊ธฐ๋Šฅ ์ˆ˜์ค€์˜ ์ ‘๊ทผ ํ†ต์ œ ๋ˆ„๋ฝ ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ์š”์ฒญ ๋ณ€์กฐ(CSRF) ์•Œ๋ ค์ง„ ์ทจ์•ฝ์ ์ด ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ ๊ฒ€์ฆ๋˜์ง€ ์•Š์€ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋ฐ ํฌ์›Œ๋“œ ์ฐธ๊ณ ๋ฌธํ—Œ ๋ณด์•ˆ ์œ„ํ—˜ Top 10 ์ธ์ ์…˜ ์ธ์ฆ ๋ฐ ์„ธ์…˜ ๊ด€๋ฆฌ ์ทจ์•ฝ์  ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ์Šคํฌ๋ฆฝํŒ…(XSS) ์ทจ์•ฝํ•œ ์ง์ ‘ ๊ฐ์ฒด ์ฐธ์กฐ ๋ณด์•ˆ ์„ค์ • ์˜ค๋ฅ˜ ๋ฏผ๊ฐ ๋ฐ์ดํ„ฐ ๋…ธ์ถœ ๊ธฐ๋Šฅ ์ˆ˜์ค€์˜ ์ ‘๊ทผ ํ†ต์ œ ๋ˆ„๋ฝ ํฌ๋กœ์Šค ์‚ฌ์ดํŠธ ์š”์ฒญ ๋ณ€์กฐ(CSRF) ์•Œ๋ ค์ง„ ์ทจ์•ฝ์ ์ด ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ ๊ฒ€์ฆ๋˜์ง€ ์•Š์€ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋ฐ ํฌ์›Œ๋“œ ์ฐธ๊ณ ๋ฌธํ—Œ ๊ฐ€์žฅ ์‹ฌ๊ฐํ•œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ณด์•ˆ ์œ„ํ—˜ 10 ๊ฐ€์ง€<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ๋น…๋ฐ์ดํ„ฐ - ํ•˜๋‘ก, ํ•˜์ด๋ธŒ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ ### ๋ณธ๋ฌธ: ์ด ์ฑ…์€ ํ•˜๋‘ก์„ ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์„ ๋Œ€์ƒ์œผ๋กœ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก์€ ๋น…๋ฐ์ดํ„ฐ ๊ธฐ์ˆ ์˜ ์‹œ์ž‘์ ์ž…๋‹ˆ๋‹ค. ํ•˜๋‘ก์ด ๋งต๋ฆฌ๋“€์Šค์™€ HDFS ๊ธฐ์ˆ ์„ ์†Œ๊ฐœํ•˜๋ฉด์„œ ๋น…๋ฐ์ดํ„ฐ๋ฅผ ์ ๋‹นํ•œ ๊ฐ€๊ฒฉ์œผ๋กœ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ํ•˜๋‘ก์ด ์˜คํ”ˆ ์†Œ์Šค๊ฐ€ ๋˜๋ฉด์„œ ํ•˜๋‘ก์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ํ•˜๋‘ก ์—์ฝ” ์‹œ์Šคํ…œ๋“ค์ด ๋‹ค์–‘ํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚˜๋ฉด์„œ ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„ ๊ธฐ์ˆ ์ด ํญ๋ฐœ์ ์œผ๋กœ ๋ฐœ์ „ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ๋Š” SQL์„ ์ด์šฉํ•˜์—ฌ ๋งต๋ฆฌ๋“€์Šค๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. SQL์„ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ์ž๊ฐ€ ์•„๋‹ˆ์–ด๋„ ์‰ฝ๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŒŒ์ผ์˜ ์ •๋ณด์˜ ๋ฌผ๋ฆฌ์ ์ธ ๊ตฌ์กฐ๋ฅผ ํ…Œ์ด๋ธ” ํ˜•ํƒœ์˜ ๋…ผ๋ฆฌ์  ๊ตฌ์กฐ๋กœ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ ๋ถ„์„์ด ๋”์šฑ ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค. ์ด ์ฑ…์€ ํ•˜๋‘ก 2.7 ๋ฒ„์ „, ํ•˜์ด๋ธŒ 2.1 ๋ฒ„์ „์„ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฒ„์ „์˜ ์ฐจ์ด๋กœ ์ธํ•ด ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด๋‚˜ ํ‹€๋ฆฐ ๋ถ€๋ถ„์€ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ์ˆ˜์ •ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. mail to <EMAIL> 1-๋น…๋ฐ์ดํ„ฐ ๋น…๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋น…๋ฐ์ดํ„ฐ๋ž€? ๋น…๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋‹จ๊ณ„ ๋น…๋ฐ์ดํ„ฐ ์—์ฝ”์‹œ์Šคํ…œ 1-๋น…๋ฐ์ดํ„ฐ๋ž€? ๋น…๋ฐ์ดํ„ฐ๋Š” ํฐ ์‚ฌ์ด์ฆˆ์˜ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์œ ์˜๋ฏธํ•œ ์ง€ํ‘œ๋ฅผ ๋ถ„์„ํ•ด ๋‚ด๋Š” ๊ฒƒ์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SNS, ๋กœ๊ทธ, ๋ฌธ์„œ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ์ˆ˜์ง‘ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ˜•ํƒœ์˜ ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์˜์‚ฌ๊ฒฐ์ •์— ๋„์›€์„ ์ฃผ๋Š” ์ง€ํ‘œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ •์˜ ๋ฐ์ดํ„ฐ ๊ทœ๋ชจ์— ์ดˆ์ ์„ ๋งž์ถ˜ ์ •์˜(๋งฅํ‚จ์ง€, 2011) ๊ธฐ์กด DB ๊ด€๋ฆฌ ๋„๊ตฌ์˜ ์ˆ˜์ง‘, ์ €์žฅ, ๊ด€๋ฆฌ, ๋ถ„์„ ์—ญ๋Ÿ‰์„ ๋„˜์–ด์„œ๋Š” ๋ฐ์ดํ„ฐ ์—…๋ฌด ์ˆ˜ํ–‰ ๋ฐฉ์‹์— ์ดˆ์ ์„ ๋งž์ถ˜ ์ •์˜(IDC, 2011) ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๊ฐ€์น˜๋ฅผ ์ถ”์ถœํ•˜๊ณ , ๋ฐ์ดํ„ฐ์˜ ๋น ๋ฅธ ์ˆ˜์ง‘, ๋ฐœ๊ตด, ๋ถ„์„์„ ์ง€์›ํ•˜๋„๋ก ๊ณ ์•ˆ๋œ ๊ธฐ์ˆ  ๋ฐ ์•„ํ‚คํ…์ฒ˜ ์ถœํ˜„ ๋ฐฐ๊ฒฝ ์ตœ๊ทผ์— ๋น…๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์€ ๊ด€์‹ฌ์„ ๋ฐ›๊ธฐ ์‹œ์ž‘ํ•œ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์–‘์˜ ์ฆ๊ฐ€์™€ ๋ฐ์ดํ„ฐ ์ €์žฅ๊ธฐ์ˆ  ๋ฐœ๋‹ฌ SNS ๋“ฑ์žฅ, ์Šค๋งˆํŠธ ๊ธฐ๊ธฐ ๋ณด๊ธ‰์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ฆ๊ฐ€ ๋””์ง€ํ„ธ ์ €์žฅ๊ธฐ์ˆ ๊ณผ ์žฅ์น˜์˜ ๋ฐœ๋‹ฌ ๊ฒฝ์ œ์  ํƒ€๋‹น์„ฑ ์ฆ๊ฐ€ / ์ €์žฅ ์žฅ์น˜์˜ ๊ฐ€๊ฒฉ ์ธํ•˜ 1980๋…„๋Œ€ 1G 10์–ต ์ด์ƒ์ด๋˜ ๋ฉ”๋ชจ๋ฆฌ ๊ฐ€๊ฒฉ์ด 2010๋…„๋Œ€ 100์› ๋ฏธ๋งŒ์œผ๋กœ ๋–จ์–ด์ง ๋Œ€์šฉ๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์—ฌ๋„ ๊ฒฝ์ œ์„ฑ์ด ์žˆ์Œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๊ธฐ์ˆ  ๋ฐœ๋‹ฌ ๋ถ„์‚ฐ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ ๊ธฐ์ˆ ์˜ ๋ฐœ๋‹ฌ๋กœ ํ•ฉ๋ฆฌ์ ์ธ ์‹œ๊ฐ„ ์•ˆ์— ๋ฐ์ดํ„ฐ ๋ถ„์„์ด ๊ฐ€๋Šฅํ•ด์ง CPU ๋ฐœ์ „, ํด๋ผ์šฐ๋“œ ์ปดํ“จํŒ…, ํ•˜๋‘ก ๋“ฑ ์˜คํ”ˆ์†Œ์Šค ํ™œ์„ฑํ™”๋กœ ์Šค์ผ€์ผ ์•„์›ƒ์ด ํŽธ๋ฆฌํ•ด์ง ๋น…๋ฐ์ดํ„ฐ ํŠน์ง• - 3V ๋น…๋ฐ์ดํ„ฐ์˜ ํŠน์ง•์€ 3V๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Volume - ํฌ๊ธฐ ์ €์žฅ ์žฅ์น˜ ๊ฐ€๊ฒฉ์˜ ํ•˜๋ฝ, ๋„คํŠธ์›Œํฌ ์†๋„์˜ ํ–ฅ์ƒ์œผ๋กœ ์ˆ˜ ํŽ˜ํƒ€๋ฐ”์ดํŠธ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งค์ผ ์ƒ์„ฑ Variety - ๋‹ค์–‘์„ฑ ์ •ํ˜•, ๋ฐ˜ ์ •ํ˜•, ๋น„์ •ํ˜• ํ˜•ํƒœ์˜ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ Velocity - ์†๋„ ์ •๋ณด์˜ ์œ ํ†ต ์†๋„๊ฐ€ ๊ต‰์žฅํžˆ ๋น ๋ฆ„. ๋ฐ์ดํ„ฐ์˜ ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ๋น ๋ฆ„. ์ผ, ์ฃผ, ์›”๋‹จ์œ„ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ์™€ ์ดˆ ๋‹จ์œ„ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋น…๋ฐ์ดํ„ฐ ํŠน์ง• - 5V 3V์— ์•„๋ž˜์˜ ๋‘ ๊ฐ€์ง€๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ 5V๋กœ ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. Value - ๊ฐ€์น˜ ์œ ์˜๋ฏธํ•œ ๊ฐ€์น˜๋ฅผ ๊ฐ€์ง€๋Š” ์ง€ํ‘œ ์ง€ํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ์˜ ์˜์‚ฌ ๊ฒฐ์ •์— ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๋ฅผ ์ œ๊ณต Veracity - ์ •ํ™•์„ฑ ๋น…๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฝ‘์•„๋‚ธ ๋ฐ์ดํ„ฐ์˜ ์‹ ๋ขฐ์„ฑ, ์ •ํ™•์„ฑ์ด ๋†’์Œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ๋” ์ •ํ™•ํ•œ ๋ถ„์„์ด ๊ฐ€๋Šฅ ๋น…๋ฐ์ดํ„ฐ ํ™œ์šฉ ๋น…๋ฐ์ดํ„ฐ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์˜์—ญ์—์„œ ํ™œ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์—…, ์ •๋ถ€, ๊ฐœ์ธ์ด ์–ด๋–ป๊ฒŒ ๋น…๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ์—… ๋น…๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•ด ์†Œ๋น„์ž์˜ ํ–‰๋™์„ ๋ถ„์„ํ•˜๊ณ  ์‹œ์žฅ ๋ณ€๋™์„ ์˜ˆ์ธกํ•˜์—ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์„ ํ˜์‹ ํ•˜๊ฑฐ๋‚˜ ์‹ ์‚ฌ์—…์„ ๋ฐœ๊ตดํ•ฉ๋‹ˆ๋‹ค. ํŽ˜์ด์Šค๋ถ ๋งˆ์šฐ์Šค ์ปค์„œ์˜ ์›€์ง์ž„์„ ์ˆ˜์ง‘ํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ด์šฉ ํ™˜๊ฒฝ ๊ฐœ์„ ๊ณผ ๊ด‘๊ณ  ํšจ๊ณผ์˜ ๊ทน๋Œ€ํ™”์— ํ™œ์šฉ ์•„๋งˆ์กด ์‚ฌ์šฉ์ž์˜ ๊ฐœ์ธ ์ •๋ณด, ๊ตฌ๋งค ๋‚ด์—ญ ๋“ฑ์„ ํ™œ์šฉํ•˜์—ฌ ์ž๋™ ๋„์„œ ์ถ”์ฒœ ๊ตฌ๋งค๋‚ด์—ญ, ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋‚ด์—ญ ๋“ฑ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฌผํ’ˆ์˜ ๊ตฌ๋งค๋ฅผ ์˜ˆ์ธกํ•˜์—ฌ ๋ฌผ๋ฅ˜ ์˜ˆ์ธก ๋ฐฐ์†ก ์„œ๋น„์Šค ์ •๋ถ€ ๊ธฐ์ƒ, ์ธ๊ตฌ์ด๋™, ๊ฐ์ข… ํ†ต๊ณ„ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜์—ฌ ์‚ฌํšŒ ๋ณ€ํ™”๋ฅผ ์ถ”์ •ํ•˜๊ฑฐ๋‚˜, ํ™˜๊ฒฝ ํƒ์ƒ‰, ์ฃผ๋ณ€๊ตญ์˜ ์ƒํ™ฉ์„ ๋ถ„์„ํ•˜์—ฌ ์žฅ๊ธฐ์ ์ธ ๊ด€์ ์˜ ๋Œ€์‘์ฑ…์„ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค. ์˜ฌ๋นผ๋ฏธ ๋ฒ„์Šค ์„œ์šธ์‹œ์™€ KT๊ฐ€ ํ˜‘๋ ฅํ•˜์—ฌ ์‹ฌ์•ผ์‹œ๊ฐ„ ์œ ๋™์ธ๊ตฌ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์‹ฌ์•ผ ์‹œ๊ฐ„ ์ „์šฉ ๋ฒ„์Šค์˜ ๋ฐฐ์ฐจ ์„œ๋น„์Šค ์‹œ์ž‘ ๊ฐœ์ธ ๊ฐœ์ธ์˜ ๋ชฉ์ ์— ๋”ฐ๋ผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฏธ๊ตญ ๋Œ€ํ†ต๋ น์„ ๊ฑฐ ๋ฒ„๋ฝ ์˜ค๋ฐ”๋งˆ ๋Œ€ํ†ต๋ น์ด ๋น…๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ<NAME>์ž ๊ฐœ์ธ์—๊ฒŒ ๋งž์ถคํ˜• ๊ณต์•ฝ ์ •๋ณด ์ œ๊ณต 1-๋ฐ์ดํ„ฐ์˜ ํ˜•ํƒœ ์ˆ˜์ง‘ ํ˜•ํƒœ ๋ฐ์ดํ„ฐ๋Š” ์ˆ˜์ง‘ ํ˜•ํƒœ์— ๋”ฐ๋ผ ์ •ํ˜•, ๋ฐ˜ ์ •ํ˜•, ๋น„์ •ํ˜•์œผ๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค. ๋น…๋ฐ์ดํ„ฐ๋Š” ์ •ํ˜• ๋ฐ์ดํ„ฐ๋ณด๋‹ค๋Š” ๋น„์ •ํ˜•, ๋ฐ˜ ์ •ํ˜•์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋” ๋งŽ์ด ์ˆ˜์ง‘๋ฉ๋‹ˆ๋‹ค. ์ˆ˜์ง‘๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์–‘ํ•œ ๋„๊ตฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •ํ˜• ํ˜•ํƒœ๋กœ ๋ณ€ํ˜•ํ•˜๊ณ  ๋ถ„์„์— ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, CSV, ์—‘์…€๊ณผ ๊ฐ™์ด ์นผ๋Ÿผ ๋‹จ์œ„์˜ ๋ช…ํ™•ํ•œ ๊ตฌ๋ถ„์ž์™€ ํ˜•ํƒœ๊ฐ€ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ฐ˜ ์ •ํ˜• XML, HTML, JSON ํ˜•ํƒœ์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ˜•ํƒœ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋‚˜ ์Šคํ‚ค๋งˆ๊ฐ€ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋น„์ •ํ˜• ๋™์˜์ƒ, SNS ๋ฉ”์‹œ์ง€, ์‚ฌ์ง„, ์˜ค๋””์˜ค, ์Œ์„ฑ ๋ฐ์ดํ„ฐ์ฒ˜๋Ÿผ ํ˜•ํƒœ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋Š” ์ˆ˜์ง‘๊ณผ ์ฒ˜๋ฆฌํ•˜๋Š” ์‹œ๊ฐ„์— ๋”ฐ๋ผ ๋ฐฐ์น˜ ๋ฐ์ดํ„ฐ, ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์‹œ, ์ผ, ์ฃผ, ์›” ๋‹จ์œ„๋กœ ์ผ์ •ํ•œ ์ฃผ๊ธฐ๋กœ ์ˆ˜์ง‘, ์ฒ˜๋ฆฌ๋˜๋Š” ๋ฐ์ดํ„ฐ ์‹ค์‹œ๊ฐ„ ์‹ค์‹œ๊ฐ„ ๊ฒ€์ƒ‰์–ด, ์‹ค์‹œ๊ฐ„ ์ฐจํŠธ์ฒ˜๋Ÿผ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ณผ ๋™์‹œ์— ์ฒ˜๋ฆฌ๋˜๋Š” ๋ฐ์ดํ„ฐ 2-๋ถ„์„ ํ˜•ํƒœ ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„ ํ˜•ํƒœ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ™”ํ˜• ๋ถ„์„ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ฟผ๋ฆฌ์— ๋ฐ”๋กœ ๋ฐ˜์‘ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ถ„์„ ๋ฐฉ๋ฒ• ๋Œ€ํ™”ํ˜• ๋Œ€์‹œ๋ณด๋“œ ๋ฐฐ์น˜ ๋ถ„์„ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์ •ํ•œ ์ฃผ๊ธฐ๋กœ ๋ถ„์„ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ผ/์ฃผ/์›”๊ฐ„ ๋ณด๊ณ ์„œ ์‹ค์‹œ๊ฐ„ ๋ถ„์„ ์‚ฌ์šฉ์ž์˜ ์—ฌ๋Ÿฌ ์ž…๋ ฅ์ด ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ €์žฅ๋˜๊ณ  ๋ถ„์„ํ•˜๋Š” ๋ฐฉ๋ฒ• ๊ฒฐ์ œ/์‚ฌ๊ธฐ ๊ฒฝ๊ณ  1๋ถ„ ์ธก์ • ๊ธฐ๊ณ„ ํ•™์Šต ๊ธฐ๊ณ„ ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด ์˜ˆ์ธก ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ• ์‹ฌ๋ฆฌ ๋ถ„์„, ์˜ˆ์ธก ๋ชจ๋ธ 2-๋น…๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋‹จ๊ณ„ ๋น…๋ฐ์ดํ„ฐ๋Š” ๋‹ค์Œ์˜ 5๋‹จ๊ณ„๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ˆ˜์ง‘ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” ๋‹จ๊ณ„ ์ •ํ˜•, ๋น„์ •ํ˜•, ๋ฐ˜ ์ •ํ˜• ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ์ •์ œ ์ˆ˜์ง‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ ์žฌํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š” ์—†๋Š” ๋ฐ์ดํ„ฐ, ๊นจ์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ๋‹จ๊ณ„ ๋ฐ˜ ์ •ํ˜•, ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ๋Š” ๋ถ„์„์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ์™ธ์— ํ•„์š” ์—†๋Š” ๋ถ€๋ถ„์„ ์ œ๊ฑฐํ•˜๋Š” ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•จ ์ ์žฌ ์ •์ œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด ์ ์žฌํ•˜๋Š” ๋‹จ๊ณ„ RDB, NoSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, Redshift, Druid ๋“ฑ์˜ ๋„๊ตฌ์— ์ ์žฌ ๋ถ„์„ ์ ์žฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์˜๋ฏธ ์žˆ๋Š” ์ง€ํ‘œ๋กœ ๋ถ„์„ํ•˜๋Š” ๋‹จ๊ณ„ ์˜์‚ฌ ๊ฒฐ์ •๊ถŒ์ž๋‚˜ ์ด์šฉ์ž๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ ๋ถ„์„ํ•˜๋Š” ๋‹จ๊ณ„ ์‹œ๊ฐํ™” ๋ถ„์„ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ํ‘œ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๋‹จ๊ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ์ฐจํŠธ๋กœ ๋ถ„์„ํ•˜๋Š” ๋‹จ๊ณ„ 1-์ˆ˜์ง‘ ๋น…๋ฐ์ดํ„ฐ๋Š” ๋‚ด๋ถ€/์™ธ๋ถ€์˜ ์—ฌ๋Ÿฌ ์›์ฒœ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์–‘ํ•œ<NAME>์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€/์™ธ๋ถ€ ๋ฐ์ดํ„ฐ ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ ์‹œ์Šคํ…œ ๋กœ๊ทธ, DB ๋ฐ์ดํ„ฐ ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ ๋™์˜์ƒ, ์˜ค๋””์˜ค ์ •๋ณด ์›น ํฌ๋กค๋ง ๋ฐ์ดํ„ฐ SNS ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ๋ฐฉ์‹ ๊ธฐ์กด ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ HTTP ์›น์„œ๋น„์Šค, RDB, FTP, JMS, Text ์ƒˆ๋กœ์šด ๋ฐฉ์‹์˜ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ SNS์˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ Text, ์ด๋ฏธ์ง€,<NAME>์ƒ ์ „ํ™” ์Œ์„ฑ, GPS IoT ๋””๋ฐ”์ด์Šค ์„ผ์„œ ๊ณต๊ฐ„ ๋ฐ์ดํ„ฐ + ์ธ๊ตฌ ๋ฐ์ดํ„ฐ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ํŠธ๋žœ์žญ์…˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•  ๋•Œ ์—ฐ๋™ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ ๋‹ค๋ฉด ๊ฐœ๋ณ„์ ์œผ๋กœ ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ˆ˜๋ฐฑ, ์ˆ˜์ฒœ ๊ฐœ๊ฐ€ ๋˜๋ฉด ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ๊ฐ€ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ์œ ์‹ค, ๋ฐ์ดํ„ฐ์˜ ์ „์†ก ์—ฌ๋ถ€ ํ™•์ธ์„ ์œ„ํ•œ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ๊ธฐ์ˆ  Flume ํ”Œ๋ฃธ์€ ๋งŽ์€ ์–‘์˜ ๋กœ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ˆ˜์ง‘, ์ทจํ•ฉ, ์ด๋™ํ•˜๊ธฐ ์œ„ํ•œ ๋ถ„์‚ฐํ˜• ์†Œํ”„ํŠธ์›จ์–ด Kafka ์˜คํ”ˆ ์†Œ์Šค ๋ฉ”์‹œ์ง€ ๋ธŒ๋กœ์ปค ํ”„๋กœ์ ํŠธ Sqoop ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์™€ ์•„ํŒŒ์น˜ ํ•˜๋‘ก ๊ฐ„์˜ ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋“ค์„ ํšจ์œจ์ ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ฃผ๋Š” ๋ช…๋ น ์ค„ ์ธํ„ฐํŽ˜์ด์Šค ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ Nifi ์†Œํ”„ํŠธ์›จ์–ด ์‹œ์Šคํ…œ ๊ฐ„ ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ์ž๋™ํ™”ํ•˜๋„๋ก ์„ค๊ณ„๋œ ์†Œํ”„ํŠธ์›จ์–ด ํ”„๋กœ์ ํŠธ Flink ์˜คํ”ˆ ์†Œ์Šค ์ŠคํŠธ๋ฆผ ์ฒ˜๋ฆฌ ํ”„๋ ˆ์ž„ ์›Œํฌ Splunk ๊ธฐ๊ณ„๊ฐ€ ์ƒ์„ฑํ•œ ๋น… ๋ฐ์ดํ„ฐ๋ฅผ, ์›น ์Šคํƒ€์ผ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ๊ฒ€์ƒ‰, ๋ชจ๋‹ˆํ„ฐ๋ง, ๋ถ„์„ํ•˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด Logstash ์‹ค์‹œ๊ฐ„ ํŒŒ์ดํ”„๋ผ์ธ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ์˜คํ”ˆ์†Œ์Šค ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ์—”์ง„ Fluentd ํฌ๋กœ์Šค ํ”Œ๋žซํผ ์˜คํ”ˆ ์†Œ์Šค ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ์†Œํ”„ํŠธ์›จ์–ด ํ”„๋กœ์ ํŠธ 2-์ •์ œ ์ •์ œ ๋‹จ๊ณ„๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ ๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ๋กœ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฒฝ๋กœ์—์„œ ์ˆ˜์ง‘๋œ ๋ฐ์ดํ„ฐ๊ฐ€<NAME>์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„์„ ๋‹จ๊ณ„์— ์‚ฌ์šฉํ•  ๋„๊ตฌ์— ๋งž๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•  ๋•Œ ์˜ค๋ฅ˜ ๋ฐ์ดํ„ฐ, ๋ถˆํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ •์ œํ•œ ๋ฐ์ดํ„ฐ๋Š” ์••์ถ•ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์—ฌ์ค๋‹ˆ๋‹ค. ์ •์ œ ๋‹จ๊ณ„ Identification ์•Œ๋ ค์ง„ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํฌ๋งท์ด๋‚˜ ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ์— ํ• ๋‹น๋œ ๊ธฐ๋ณธ ํฌ๋งท์„ ์‹๋ณ„ Filtration ์ˆ˜์ง‘๋œ ์ •๋ณด์—์„œ ์ •ํ™•ํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋Š” ์ œ์™ธ Validation ๋ฐ์ดํ„ฐ<NAME>์„ ๊ฒ€์ฆ Noise Reduction ์˜ค๋ฅ˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐ ๋ถ„์„ ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ๋Š” ์ œ์™ธ Transformation ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ ๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ Compression ์ €์žฅ ์žฅ์น˜ ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ๋ณ€ํ™˜ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์••์ถ• Integration ์ฒ˜๋ฆฌ ์™„๋ฃŒํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ ์žฌ 3-์ ์žฌ ์ ์žฌ ๋‹จ๊ณ„๋Š” ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๋ณด๊ด€ํ•˜๊ณ  ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ถ„์„์— ์‚ฌ์šฉํ•  ๋„๊ตฌ์— ๋”ฐ๋ผ NoSQL, RDB, ํด๋ผ์šฐ๋“œ ์Šคํ† ๋ฆฌ์ง€, HDFS ๋“ฑ ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ ์žฌํ•ฉ๋‹ˆ๋‹ค. RDB์—์„œ ์ถ”์ถœํ•œ ๋ฐ์ดํ„ฐ๋‚˜, CSV ํ˜•ํƒœ๋กœ ์ œ๊ณต๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋ณ„๋„์˜ ์ •์ œ ๋‹จ๊ณ„ ์—†์ด ๋ฐ”๋กœ ์ ์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4-๋ถ„์„ ๋ถ„์„ ๋‹จ๊ณ„๋Š” ์ ์žฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์˜์‚ฌ ๊ฒฐ์ •์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ๋ฆฌํฌํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ๋Œ€์šฉ๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•œ ์ฒ˜๋ฆฌ ์—”์ง„์ด ํ•„์š”ํ•˜๊ณ , ํšจ์œจ์ ์œผ๋กœ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํŒŒํ‹ฐ์…”๋‹, ์ธ๋ฑ์‹ฑ ๋“ฑ์˜ ๊ธฐ์ˆ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์‹œ๊ฐ„ ๋ถ„์„, ๋ฐฐ์น˜ ๋ถ„์„(์ผ, ์ฃผ, ์›”๋‹จ์œ„)์„ ์ด์šฉํ•ด ๋ฆฌํฌํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 5-์‹œ๊ฐํ™” ์ตœ์ข…์ ์œผ๋กœ ์‹œ๊ฐํ™” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ๋„ˆ๋ฌด ๋งŽ์€ ๋ฐ์ดํ„ฐ๋Š” ์ •๋ณด ๊ณผ์ž‰์œผ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ํ™•์ธํ•˜๊ธฐ์— ๋ถ€๋‹ด์ด ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ ๋น ๋ฅด๊ฒŒ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ์˜ ์‹œ๊ฐํ™”๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. 3-๋น…๋ฐ์ดํ„ฐ ์—์ฝ”์‹œ์Šคํ…œ ๋น…๋ฐ์ดํ„ฐ๋Š” ์ˆ˜์ง‘, ์ •์ œ, ์ ์žฌ, ๋ถ„์„, ์‹œ๊ฐํ™”์˜ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์น˜๋Š” ๋™์•ˆ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ์ˆ ์„ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌ๋˜๊ณ , ์ด ๊ธฐ์ˆ ๋“ค์„ ํ†ตํ‹€์–ด ๋น…๋ฐ์ดํ„ฐ ์—์ฝ” ์‹œ์Šคํ…œ(Bigdata Eco System)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜์ง‘ ๊ธฐ์ˆ  ํ”Œ๋ฃธ(Flume) ์นดํ”„์นด(Kafka) NiFi Sqoop scribe Fluentd ์ž‘์—… ๊ด€๋ฆฌ ๊ธฐ์ˆ  Airflow Azkaban Oozie ๋ฐ์ดํ„ฐ ์ง๋ ฌํ™” Avro Thrift Protocol Buffers ์ €์žฅ HDFS S3 NoSQL HBase ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ MapReduce Spark Impala Presto Hive Hcatalog Pig ํด๋Ÿฌ์Šคํ„ฐ ๊ด€๋ฆฌ YARN Mesos ๋ถ„์‚ฐ ์„œ๋ฒ„ ๊ด€๋ฆฌ Zookeeper ์‹œ๊ฐํ™” Zeppelin Hue ๋ณด์•ˆ Ranger ๋ฐ์ดํ„ฐ ๊ฑฐ๋ฒ„๋„Œ์Šค Atlas Amundsen ์ˆ˜์ง‘ ๊ธฐ์ˆ  ์ˆ˜์ง‘ ๊ธฐ์ˆ ์€ ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•œ ์›์ฒœ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. ์›์ฒœ ๋ฐ์ดํ„ฐ๋Š” ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ๊ธฐ์ˆ , ๋ฐฐ์น˜ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ๊ธฐ์ˆ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์›์ฒœ ๋ฐ์ดํ„ฐ์˜ ์ข…๋ฅ˜์—๋„ ๋กœ๊ทธ ๋ฐ์ดํ„ฐ, DB ๋ฐ์ดํ„ฐ, API ํ˜ธ์ถœ ๋ฐ์ดํ„ฐ ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”Œ๋ฃธ(Flume) ํ”Œ๋ฃธ์€ ํด๋ผ์šฐ๋ฐ๋ผ์—์„œ ๊ฐœ๋ฐœํ•œ ์„œ๋ฒ„ ๋กœ๊ทธ ์ˆ˜์ง‘ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ๊ฐ ์„œ๋ฒ„์— ์—์ด์ „ํŠธ๊ฐ€ ์„ค์น˜๋˜๊ณ , ์—์ด์ „ํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š” ์ปฌ๋ ‰ํ„ฐ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. Apache Flume(https://flume.apache.org/) ์˜คํ”ˆ์†Œ์Šค ์ž‘๋ช… ์„ผ์Šค: ์•„ํŒŒ์น˜ ํ”Œ๋ฃธ(Apache Flume)(๋ฐ”๋กœ ๊ฐ€๊ธฐ) ํ”Œ๋ฃธ Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ ์นดํ”„์นด(Kafka) ์นดํ”„์นด๋Š” ๋งํฌ๋“œ ์ธ์—์„œ ๊ฐœ๋ฐœํ•œ ๋ถ„์‚ฐ ๋ฉ”์‹œ์ง• ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. ๋Œ€์šฉ๋Ÿ‰ ์‹ค์‹œ๊ฐ„ ๋กœ๊ทธ ์ฒ˜๋ฆฌ์— ํŠนํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐœํ–‰(publish) - ๊ตฌ๋…(subscribe) ๋ชจ๋ธ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. Kafka([http://kafka.apache.org]) ์นดํ”„์นด ์†Œ๊ฐœ ๋ฐ ์•„ํ‚คํ…์ฒ˜ ์ •๋ฆฌ(๋ฐ”๋กœ ๊ฐ€๊ธฐ) Streaming Platform์œผ๋กœ์จ์˜ Apache Kafka ๋ฐ”๋กœ ๊ฐ€๊ธฐ ์นดํ”„์นด Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ NiFi ๋ฏธ๊ตญ ๊ตญ๊ฐ€ ์•ˆ๋ณด๊ตญ(NSA)์—์„œ ๊ฐœ๋ฐœํ•œ ์‹œ์Šคํ…œ ๊ฐ„ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์„ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ, ๊ด€๋ฆฌ, ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์ ์˜ ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. Nifi(https://nifi.apache.org) ๋‚˜์ด ํŒŒ์ด Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ NSA์˜ Dataflow ์—”์ง„ Apache NiFi ์†Œ๊ฐœ์™€ ์„ค์น˜ ๋ฐ”๋กœ ๊ฐ€๊ธฐ Flume, Kafka, Nifi ๋ฐ”๋กœ ๊ฐ€๊ธฐ Sqoop RDBMS์™€ HDFS ๊ฐ„ ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ „์†ก์„ ์œ„ํ•œ ์„ค๋ฃจ์…˜์ž…๋‹ˆ๋‹ค. HDFS, RDBMS, DW, NoSQL ๋“ฑ ๋‹ค์–‘ํ•œ ์ €์žฅ์†Œ์— ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์‹ ์†ํ•˜๊ฒŒ ์ „์†กํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ƒ์šฉ RDBMS๋„ ์ง€์›ํ•˜๊ณ , MySQL, PostgreSQL ์˜คํ”ˆ์†Œ์Šค RDBMS๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. Sqoop(http://sqoop.apache.org/) ์Šค์ฟฑ Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ scribe ์Šคํฌ๋ฆฌ๋ธŒ๋Š” ํŽ˜์ด์Šค๋ถ์—์„œ ์ œ์ž‘ํ•œ ๋กœ๊ทธ ์ˆ˜์ง‘ ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. ๋ฉ”์‹œ์ง€ ํ์— ์Œ“์ธ ๋กœ๊ทธ๋ฅผ DB๋‚˜ ๋ฉ”์‹œ์ง€ ํ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. C++๋กœ ์ œ์ž‘๋˜์–ด ์†๋„๊ฐ€ ๋น ๋ฅด๊ณ , ํŽ˜์ด์Šค๋ถ์˜ ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉ๋˜์—ˆ์„ ์ •๋„๋กœ ์•ˆ์ •์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” ํŽ˜์ด์Šค๋ถ์ด Calligraphus๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 2014๋…„ ์ดํ›„ ๊ฐœ์„ ์‚ฌํ•ญ์ด ์—†์Šต๋‹ˆ๋‹ค. Scribe(scribe github) Fluentd ํŠธ๋ ˆ์ € ๋ฐ์ดํ„ฐ์—์„œ ๊ฐœ๋ฐœํ•œ ๋กœ๊ทธ ์ˆ˜์ง‘ ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ ๋ฃจ๋น„์™€ C๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ํ˜•ํƒœ์˜ ๋กœ๊ทธ๋ฅผ ์ „๋‹ฌ๋ฐ›์•„์„œ ์›ํ•˜๋Š” ์ €์žฅ์†Œ์— ์Œ“์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋น„์ •ํ˜•, ๋ฐ˜ ์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ํ•„ํ„ฐ๋ง, ๋ฒ„ํผ๋ง ํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋‚˜ ํด๋ผ์šฐ๋“œ ์ €์žฅ์†Œ์— ํšจ์œจ์ ์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ๋Š” Flume NG์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. Flume์˜ Source, Channel, Sink๊ฐ€ Input, Buffer, Output์œผ๋กœ ๋Œ€์ฒด๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์žฅ์ ์€ ๊ฐ ํŒŒํŠธ ๋ณ„๋กœ ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ๋งŒ๋“ค๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. Fluentd Fluentd github ์ž‘์—… ๊ด€๋ฆฌ ๊ธฐ์ˆ  ์ž‘์—… ๊ด€๋ฆฌ ๊ธฐ์ˆ ์€ ๋น…๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋‹จ๊ณ„๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ƒ์„ฑ, ๊ด€๋ฆฌํ•˜๊ณ  ๋ชจ๋‹ˆํ„ฐ๋งํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. Airflow ์—์–ดํ”Œ๋กœ๋Š” ์—์–ด๋น„์•ค๋น„์—์„œ ๊ฐœ๋ฐœํ•œ ๋ฐ์ดํ„ฐ ํ๋ฆ„์˜ ์‹œ๊ฐํ™”, ์Šค์ผ€์ฅด๋ง, ๋ชจ๋‹ˆํ„ฐ๋ง์ด ๊ฐ€๋Šฅํ•œ ์›Œํฌํ”Œ๋กœ ํ”Œ๋žซํผ์ž…๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ, ํ”„๋ ˆ์Šคํ† , DBMS ์—”์ง„๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Airflow(http://nerds.airbnb.com/airflow) Apache Airflow - Workflow ๊ด€๋ฆฌ ๋„๊ตฌ ๋ฐ”๋กœ ๊ฐ€๊ธฐ ๋ฐ์ดํ„ฐ ์›Œํฌํ”Œ๋กœ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ Apache Airflow #1๋ฐ”๋กœ ๊ฐ€๊ธฐ Azkaban ์•„์ฆˆ์นด๋ฐ˜์€ ๋งํฌ๋“œ ์ธ์—์„œ ๊ฐœ๋ฐœํ•œ ์›Œํฌํ”Œ๋กœ ์Šค์ผ€์ค„๋Ÿฌ, ์‹œ๊ฐํ™”๋œ ์ ˆ์ฐจ, ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๊ด€๋ฆฌ, ์ž‘์—… ๋ชจ๋‹ˆํ„ฐ๋ง ๋ฐ ์•Œ๋žŒ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๋Š” ์›Œํฌํ”Œ๋กœ ๊ด€๋ฆฌ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. Azkaban(https://azkaban.github.io) Azkaban - Hadoop Workflow Management System(Opensource by LinkedIn) ๋ฐ”๋กœ ๊ฐ€๊ธฐ Oozie ์šฐ์ง€๋Š” ํ•˜๋‘ก ์ž‘์—…์„ ๊ด€๋ฆฌํ•˜๋Š” ์›Œํฌํ”Œ๋กœ ๋ฐ ์ฝ”๋””๋„ค์ดํ„ฐ ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. ์ž๋ฐ” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„๋กœ UI ์ œ๊ณตํ•˜๊ณ , ๋งต๋ฆฌ๋“€์Šค, hive, pig ์ž‘์—… ๊ฐ™์€ ํŠนํ™”๋œ ์•ก์…˜์œผ๋กœ ๊ตฌ์„ฑ๋œ XML ํฌ๋งท์˜ ์›Œํฌํ”Œ๋กœ์šฐ๋กœ ์ž‘์—…์„ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค. https://oozie.apache.org/ Oozie Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ Airflow vs Azkaban vs Oozie ๋ฐ”๋กœ ๊ฐ€๊ธฐ ๋ฐ์ดํ„ฐ ์ง๋ ฌํ™” ๋น…๋ฐ์ดํ„ฐ ์—์ฝ” ์‹œ์Šคํ…œ์ด ๋‹ค์–‘ํ•œ ๊ธฐ์ˆ ๊ณผ ์–ธ์–ด๋กœ ๊ตฌํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์–ธ์–ด ๊ฐ„์— ๋‚ด๋ถ€ ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ ์ง๋ ฌํ™” ๊ธฐ์ˆ ์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. Avro ์—์ด๋ธŒ๋กœ(Avro)๋Š” ์•„ํŒŒ์น˜์˜ ํ•˜๋‘ก ํ”„๋กœ์ ํŠธ์—์„œ ๊ฐœ๋ฐœ๋œ ์›๊ฒฉ ํ”„๋Ÿฌ์‹œ์ € ํ˜ธ์ถœ(RPC) ๋ฐ ๋ฐ์ดํ„ฐ ์ง๋ ฌํ™” ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. ์ž๋ฃŒํ˜•๊ณผ ํ”„๋กœํ† ์ฝœ ์ •์˜๋ฅผ ์œ„ํ•ด JSON์„ ์‚ฌ์šฉํ•˜๋ฉฐ ์ฝคํŒฉํŠธ ๋ฐ”์ด๋„ˆ๋ฆฌ ํฌ๋งท์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ง๋ ฌํ™”ํ•ฉ๋‹ˆ๋‹ค. https://avro.apache.org/ Avro Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ Thrift ์Šค ๋ฆฌํ”„ํŠธ๋Š” ํŽ˜์ด์Šค๋ถ์—์„œ ๊ฐœ๋ฐœํ•œ ์„œ๋กœ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ๊ฐœ๋ฐœ๋œ ๋ชจ๋“ˆ์˜ ํ†ตํ•ฉ์„ ์ง€์›ํ•˜๋Š” RPC ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ์„œ๋น„์Šค ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์„ ์–ธํ•˜๋ฉด, RPC ํ˜•ํƒœ์˜ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ์ฝ”๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค. ์ž๋ฐ”, C++, C#, Perl, PHP, ํŒŒ์ด์ฌ, ๋ธํŒŒ์ด, Erlang, Go, Node.js ๋“ฑ๊ณผ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ์–ธ์–ด๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. Thrift(http://thrift.apache.org) ์Šค ๋ฆฌํ”„ํŠธ Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ RPC - Apache Thrift ์ž…๋ฌธ 1๋ถ€ ๋ฐ”๋กœ ๊ฐ€๊ธฐ Protocol Buffers //polyline.proto syntax = "proto2"; message Point { required int32 x = 1; required int32 y = 2; optional string label = 3; } ํ”„๋กœํ† ์ฝœ ๋ฒ„ํผ(Protocol Buffers)๋Š” ๊ตฌ๊ธ€์—์„œ ๊ฐœ๋ฐœํ•œ RPC ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ง๋ ฌํ™”ํ•˜๋Š” ๋ฐฉ์‹์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. C++, C#, Go, Java, Python, Object C, Javascript, Ruby ๋“ฑ ๋‹ค์–‘ํ•œ ์–ธ์–ด๋ฅผ ์ง€์›ํ•˜๋ฉฐ ํŠนํžˆ ์ง๋ ฌํ™” ์†๋„๊ฐ€ ๋น ๋ฅด๊ณ  ์ง๋ ฌํ™”๋œ ํŒŒ์ผ์˜ ํฌ๊ธฐ๋„ ์ž‘์•„์„œ Apache Avro ํŒŒ์ผ ํฌ๋งท๊ณผ ํ•จ๊ป˜ ๋งŽ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Protocol Buffers(https://developers.google.com/protocol-buffers/) ๊ตฌ๊ธ€ ํ”„๋กœํ† ์ฝœ ๋ฒ„ํผ(๋ฐ”๋กœ ๊ฐ€๊ธฐ) ์ €์žฅ ๋น…๋ฐ์ดํ„ฐ๋Š” ๋Œ€์šฉ๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ์˜ ์ €์žฅ์˜ ์•ˆ์ •์„ฑ๊ณผ ์†๋„๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. HDFS ์™ธ์—๋„ ์•„๋งˆ์กด AWS์˜ S3, MS Azure์˜ Data Lake, Blob Storage, Google์˜ Cloud Storage๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. HDFS ํ•˜๋‘ก ๋ถ„์‚ฐ ํŒŒ์ผ ์‹œ์Šคํ…œ(HDFS, Hadoop distributed file system)์€ ํ•˜๋‘ก ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์œ„ํ•ด ์ž๋ฐ” ์–ธ์–ด๋กœ ์ž‘์„ฑ๋œ ๋ถ„์‚ฐ ํ™•์žฅ ํŒŒ์ผ ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. HDFS๋Š” ๋ฒ”์šฉ ์ปดํ“จํ„ฐ๋ฅผ ํด๋Ÿฌ์Šคํ„ฐ๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ๋Œ€์šฉ๋Ÿ‰์˜ ํŒŒ์ผ์„ ๋ธ”๋ก ๋‹จ์œ„๋กœ ๋ถ„ํ• ํ•˜์—ฌ ์—ฌ๋Ÿฌ ์„œ๋ฒ„์— ๋ณต์ œํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. HDFS Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ S3 S3๋Š” ์•„๋งˆ์กด์—์„œ ์ œ๊ณตํ•˜๋Š” ์ธํ„ฐ๋„ท์šฉ ์ €์žฅ์†Œ์ž…๋‹ˆ๋‹ค. ์•„๋งˆ์กด์—์„œ ์ž์ฒด์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์„œ๋น„์Šค์— ์ž˜ ์ ์šฉ๋˜๋Š” ์ €์žฅ์†Œ์ž…๋‹ˆ๋‹ค. ์•„๋งˆ์กด S3(๋ฐ”๋กœ ๊ฐ€๊ธฐ) NoSQL HBase hbase(main):003:0> describe 'test' Table test is ENABLED test COLUMN FAMILIES DESCRIPTION {NAME => 'cf', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'f alse', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'} 1 row(s) Took 0.9998 seconds HBase๋Š” HDFS ๊ธฐ๋ฐ˜์˜ ์นผ๋Ÿผ ๊ธฐ๋ฐ˜ NoSQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ž…๋‹ˆ๋‹ค. ๊ตฌ๊ธ€์˜ ๋น…ํ…Œ์ด๋ธ”(BigTable) ๋…ผ๋ฌธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐœ๋ฐœ๋์Šต๋‹ˆ๋‹ค. ์‹ค์‹œ๊ฐ„ ๋žœ๋ค ์กฐํšŒ ๋ฐ ์—…๋ฐ์ดํŠธ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๊ฐ ํ”„๋กœ์„ธ์Šค๋Š” ๊ฐœ์ธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. HBase์˜ ๊ธฐ๋ณธ ๋™์ž‘ ๋‹จ์œ„๋Š” ์นผ๋Ÿผ์ž…๋‹ˆ๋‹ค. H ๋งˆ์Šคํ„ฐ๊ฐ€ H ๋ฆฌ์ „์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผํ‚คํผ๊ฐ€ H ๋งˆ์Šคํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜์—ฌ SPOF๋ฅผ ํšŒํ”ผํ•ฉ๋‹ˆ๋‹ค. HBase(http://hbase.apache.org) HBase Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ HBase์˜ ์ดํ•ด HBase An In-Depth Look at the HBase Architecture ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋Š” ๋น…๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. ํ•˜๋‘ก์˜ ๋งต๋ฆฌ๋“€์Šค๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ, ์ŠคํŒŒํฌ, ํ•˜์ด๋ธŒ, HBase, ์ž„ํŒ”๋ผ ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ์ˆ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. MapReduce ๋งต๋ฆฌ๋“€์Šค๋Š” HDFS ์ƒ์—์„œ ๋™์ž‘ํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ถ„์„ ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ๋‹จ์œ„์ž‘์—…์„ ๋ฐ˜๋ณตํ•  ๋•Œ ํšจ์œจ์ ์ธ ๋งต๋ฆฌ๋“€์Šค ๋ชจ๋ธ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค. Spark ์ŠคํŒŒํฌ(Spark)๋Š” ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜์˜ ๋ฒ”์šฉ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ํ”Œ๋žซํผ์ž…๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ, ๋จธ์‹ ๋Ÿฌ๋‹, SQL ์งˆ์˜ ์ฒ˜๋ฆฌ, ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, ๊ทธ๋ž˜ํ”„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ฒ˜๋ฆฌ์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 2009๋…„ ๋ฒ„ํด๋ฆฌ ๋Œ€ํ•™์˜ AMPLab์—์„œ ์‹œ์ž‘๋์œผ๋ฉฐ, ํ˜„์žฌ ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ์„ฑ์žฅํ•˜๊ณ  ์žˆ๋Š” ์˜คํ”ˆ์†Œ์Šค ํ”„๋กœ์ ํŠธ ์ค‘์˜ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. Spark(http://spark.apache.org) ์ŠคํŒŒํฌ Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ ์ŠคํŒŒํฌ ์†Œ๊ฐœ ๋ฐ ์‹ค์Šต ๋ฐ”๋กœ ๊ฐ€๊ธฐ Impala ์ž„ํŒ”๋ผ(Impala)๋Š” ํด๋ผ์šฐ๋ฐ๋ผ์—์„œ ๊ฐœ๋ฐœํ•œ ํ•˜๋‘ก ๊ธฐ๋ฐ˜์˜ ๋ถ„์‚ฐ ์ฟผ๋ฆฌ ์—”์ง„์ž…๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , C++๋กœ ๊ฐœ๋ฐœํ•œ ์ธ ๋ฉ”๋ชจ๋ฆฌ ์—”์ง„์„ ์‚ฌ์šฉํ•ด ๋น ๋ฅธ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ž„ํŒ”๋ผ๋Š” ๋ฐ์ดํ„ฐ ์กฐํšŒ๋ฅผ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋กœ HiveQL์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ˆ˜์ดˆ ๋‚ด์— SQL ์งˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2015๋…„ ๋ง ์•„ํŒŒ์น˜ ์žฌ๋‹จ์˜ ์ธํ๋ฒ ์ด์…˜ ํ”„๋กœ์ ํŠธ๋กœ ์ฑ„ํƒ๋์Šต๋‹ˆ๋‹ค. Impala(http://impala.io) ์ž„ํŒ”๋ผ Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ Hadoop์—์„œ์˜ ์‹ค์‹œ๊ฐ„ SQL ์งˆ์˜: Impala ๋ฐ”๋กœ ๊ฐ€๊ธฐ Presto ํ”„๋ ˆ์Šคํ† (Presto)๋Š” ํŽ˜์ด์Šค๋ถ์ด ๊ฐœ๋ฐœํ•œ ๋Œ€ํ™”ํ˜• ์งˆ์˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ถ„์‚ฐ ์ฟผ๋ฆฌ ์—”์ง„์ž…๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ SQL๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ์งˆ์˜ ๊ฒฝ์šฐ ํ•˜์ด๋ธŒ ๋Œ€๋น„ 10๋ฐฐ ์ •๋„ ๋น ๋ฅธ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๋ฉฐ, ํ˜„์žฌ ์˜คํ”ˆ์†Œ์Šค๋กœ ๊ฐœ๋ฐœ์ด ์ง„ํ–‰๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Presto(https://prestodb.io) ํ”„๋ ˆ์Šคํ†  ์†Œ๊ฐœ ๋ฐ”๋กœ ๊ฐ€๊ธฐ Hive hive> SELECT col1, col2 FROM t1 1 3 1 3 1 4 2 5 ํ•˜์ด๋ธŒ(Hive)๋Š” ํ•˜๋‘ก ๊ธฐ๋ฐ˜์˜ ๋ฐ์ดํ„ฐ์›จ์–ดํ•˜์šฐ์ง•์šฉ ์„ค๋ฃจ์…˜์ž…๋‹ˆ๋‹ค. ํŽ˜์ด์Šค๋ถ์—์„œ ๊ฐœ๋ฐœํ–ˆ์œผ๋ฉฐ, ์˜คํ”ˆ์†Œ์Šค๋กœ ๊ณต๊ฐœ๋˜๋ฉฐ ์ฃผ๋ชฉ๋ฐ›์€ ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. SQL๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ HiveQL์ด๋ผ๋Š” ์ฟผ๋ฆฌ ์–ธ์–ด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ž๋ฐ”๋ฅผ ๋ชจ๋ฅด๋Š” ๋ฐ์ดํ„ฐ ๋ถ„์„๊ฐ€๋“ค๋„ ์‰ฝ๊ฒŒ ํ•˜๋‘ก ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค. HiveQL์€ ๋‚ด๋ถ€์ ์œผ๋กœ ๋งต๋ฆฌ๋“€์Šค ์žก์œผ๋Ÿฌ ๋ณ€ํ™˜๋˜์–ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. Hive(http://hive.apache.org) ํ•˜์ด๋ธŒ Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ Hcatalog HCatalog๋Š” Pig, MapReduce, Spark์—์„œ Hive ๋ฉ”ํƒ€ ์Šคํ† ์–ด ํ…Œ์ด๋ธ”์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๊ธฐํƒ€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” REST ์ธํ„ฐํŽ˜์ด์Šค ๋ฐ ๋ช…๋ น ์ค„ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. HCatalog(https://cwiki.apache.org/confluence/display/Hive/HCatalog) Pig A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float); B = FOREACH A GENERATE name; DUMP B; (John) (Mary) ํ”ผ๊ทธ(Pig)๋Š” ์•ผํ›„์—์„œ ๊ฐœ๋ฐœ๋์œผ๋‚˜ ํ˜„์žฌ๋Š” ์•„ํŒŒ์น˜ ํ”„๋กœ์ ํŠธ์— ์†ํ•œ ํ”„๋กœ์ ํŠธ๋กœ์„œ, ๋ณต์žกํ•œ ๋งต๋ฆฌ๋“€์Šค ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๋Œ€์ฒดํ•  ํ”ผ๊ทธ ๋ผํ‹ด(Pig Latin)์ด๋ผ๋Š” ์ž์ฒด ์–ธ์–ด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค API๋ฅผ ๋งค์šฐ ๋‹จ์ˆœํ™”ํ•œ ํ˜•ํƒœ์ด๊ณ  SQL๊ณผ ์œ ์‚ฌํ•œ ํ˜•ํƒœ๋กœ ์„ค๊ณ„๋์Šต๋‹ˆ๋‹ค. SQL๊ณผ ์œ ์‚ฌํ•˜๊ธฐ๋งŒ ํ•  ๋ฟ, ๊ธฐ์กด SQL ์ง€์‹์„ ํ™œ์šฉํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์šด ํŽธ์ž…๋‹ˆ๋‹ค. ํ”ผ๊ทธ Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ Pig(http://pig.apache.org) ํด๋Ÿฌ์Šคํ„ฐ ๊ด€๋ฆฌ ๋น…๋ฐ์ดํ„ฐ๋Š” ๋‹จ์ผ ์‹œ์Šคํ…œ์ด๋ณด๋‹ค๋Š” ๋ณดํ†ต ํด๋Ÿฌ์Šคํ„ฐ๋กœ ์ฒ˜๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ž์›์˜ ํšจ์œจ์ ์ธ ์‚ฌ์šฉ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ์˜ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ์ˆ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. YARN ์–€(YARN)์€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ํด๋Ÿฌ์Šคํ„ฐ ์ž์›(CPU, ๋ฉ”๋ชจ๋ฆฌ, ๋””์Šคํฌ ๋“ฑ)๊ณผ ์Šค์ผ€์ฅด๋ง์„ ์œ„ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด ํ•˜๋‘ก์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ํ”„๋ ˆ์ž„์›Œํฌ์ธ ๋งต๋ฆฌ๋“€์Šค์˜ ๋‹จ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‹œ์ž‘๋œ ํ”„๋กœ์ ํŠธ์ด๋ฉฐ, ํ•˜๋‘ก 2.0๋ถ€ํ„ฐ ์ด์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค, ํ•˜์ด๋ธŒ, ์ž„ํŒ”๋ผ, ํƒ€์กฐ, ์ŠคํŒŒํฌ ๋“ฑ ๋‹ค์–‘ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๋“ค์€ ์–€์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ํ• ๋‹น๋ฐ›์•„์„œ, ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. YARN(http://hadoop.apache.org) http://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-site/index.html Mesos ๋ฉ”์†Œ ์Šค(Mesos)๋Š” ํด๋ผ์šฐ๋“œ ์ธํ”„๋ผ์ŠคํŠธ๋Ÿญ์ฒ˜ ๋ฐ ์ปดํ“จํŒ… ์—”์ง„์˜ ๋‹ค์–‘ํ•œ ์ž์›(CPU, ๋ฉ”๋ชจ๋ฆฌ, ๋””์Šคํฌ)์„ ํ†ตํ•ฉ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  ์ž์› ๊ด€๋ฆฌ ํ”„๋กœ์ ํŠธ์ž…๋‹ˆ๋‹ค. ๋ฉ”์†Œ์Šค๋Š” 2009๋…„ ๋ฒ„ํด๋ฆฌ ๋Œ€ํ•™์—์„œ Nexus๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์‹œ์ž‘๋œ ํ”„๋กœ์ ํŠธ์ด๋ฉฐ, 2011๋…„ ๋ฉ”์†Œ์Šค๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝ๋์œผ๋ฉฐ, ํ˜„์žฌ๋Š” ์•„ํŒŒ์น˜ ํƒ‘ ๋ ˆ๋ฒจ ํ”„๋กœ์ ํŠธ๋กœ ์ง„ํ–‰ ์ค‘์ด๋ฉฐ, ํŽ˜์ด์Šค๋ถ, ์—์–ด๋น„์—”๋น„, ํŠธ์œ„ํ„ฐ, ์ด๋ฒ ์ด ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…๋“ค์ด ๋ฉ”์†Œ์Šค๋กœ ํด๋Ÿฌ์Šคํ„ฐ ์ž์›์„ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”์†Œ์Šค๋Š” ํด๋Ÿฌ์Šคํ„ฐ๋ง ํ™˜๊ฒฝ์—์„œ ๋™์ ์œผ๋กœ ์ž์›์„ ํ• ๋‹นํ•˜๊ณ  ๊ฒฉ๋ฆฌํ•ด ์ฃผ๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ œ๊ณตํ•˜๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ๋ถ„์‚ฐ ํ™˜๊ฒฝ์—์„œ ์ž‘์—… ์‹คํ–‰์„ ์ตœ์ ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1๋งŒ ๋Œ€ ์ด์ƒ์˜ ๋…ธ๋“œ์—๋„ ๋Œ€์‘์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์›น ๊ธฐ๋ฐ˜์˜ UI, ์ž๋ฐ”, C++, ํŒŒ์ด์ฌ API๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‘ก, ์ŠคํŒŒํฌ(Spark), ์Šคํ†ฐ(Storm), ์ผ๋ž˜์Šคํ‹ฑ ์„œ์น˜(Elastic Search), ์นด์‚ฐ๋“œ๋ผ(Cassandra), ์  ํ‚จ์Šค(Jenkins) ๋“ฑ ๋‹ค์–‘ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฉ”์†Œ์Šค์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”์กฐ์Šค Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ Apache Mesos ์†Œ๊ฐœ ๋ฐ”๋กœ ๊ฐ€๊ธฐ Mesos(http://mesos.apache.org) ๋ถ„์‚ฐ ์„œ๋ฒ„ ๊ด€๋ฆฌ ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ์ˆ ์ด ์ด์šฉ๋  ๋•Œ ํ•˜๋‚˜์˜ ์„œ๋ฒ„์—์„œ ๋ชจ๋“  ์ž‘์—…์ด ์ง„ํ–‰๋˜๋ฉด ์ด ์„œ๋ฒ„๊ฐ€ ๋‹จ์ผ ์‹คํŒจ ์ง€์ (SPOF)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•œ ๋ฆฌ์Šคํฌ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด ๋ถ„์‚ฐ ์„œ๋ฒ„ ๊ด€๋ฆฌ ๊ธฐ์ˆ ์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. Zookeeper ๋ถ„์‚ฐ ํ™˜๊ฒฝ์—์„œ ์„œ๋ฒ„ ๊ฐ„์˜ ์ƒํ˜ธ ์กฐ์ •์ด ํ•„์š”ํ•œ ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ์‹œ์Šคํ…œ์œผ๋กœ, ํฌ๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋„ค ๊ฐ€์ง€ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ์งธ, ํ•˜๋‚˜์˜ ์„œ๋ฒ„์—๋งŒ ์„œ๋น„์Šค๊ฐ€ ์ง‘์ค‘๋˜์ง€ ์•Š๊ฒŒ ์„œ๋น„์Šค๋ฅผ ์•Œ๋งž๊ฒŒ ๋ถ„์‚ฐํ•ด ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ๋‘˜์งธ, ํ•˜๋‚˜์˜ ์„œ๋ฒ„์—์„œ ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค๋ฅธ ์„œ๋ฒ„์™€๋„ ๋™๊ธฐํ™”ํ•ด์„œ ๋ฐ์ดํ„ฐ์˜ ์•ˆ์ •์„ฑ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค. ์…‹์งธ, ์šด์˜(active) ์„œ๋ฒ„์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์—†์„ ๊ฒฝ์šฐ, ๋‹ค๋ฅธ ๋Œ€๊ธฐ ์ค‘์ธ ์„œ๋ฒ„๋ฅผ ์šด์˜ ์„œ๋ฒ„๋กœ ๋ฐ”๊ฟ”์„œ ์„œ๋น„์Šค๊ฐ€ ์ค‘์ง€ ์—†์ด ์ œ๊ณต๋˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋„ท์งธ, ๋ถ„์‚ฐ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•˜๋Š” ์„œ๋ฒ„์˜ ํ™˜๊ฒฝ์„ค์ •์„ ํ†ตํ•ฉ์ ์œผ๋กœ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด HA(High Availability) ๊ตฌ์„ฑ๋œ HDFS ๋„ค์ž„๋…ธ๋“œ์˜ Active ๋…ธ๋“œ ์„ ์ถœ, HBase ๋ฆฌ ์ „ ์„œ๋ฒ„์˜ Active ์„œ๋ฒ„ ์„ ์ถœ, Hiveserver2์˜ ๋‹ค์ค‘ ์„ ํƒ ๋“ฑ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. Zookeeper(http://zookeeper.apache.org) ์ฃผํ‚คํผ Wiki ๋ฐ”๋กœ ๊ฐ€๊ธฐ ์‹œ๊ฐํ™” Zeppelin Zeppelin์€ ํ•œ๊ตญ์˜ NFLab์ด๋ผ๋Š” ํšŒ์‚ฌ์—์„œ ๊ฐœ๋ฐœํ•˜์—ฌ Apache top level ํ”„๋กœ์ ํŠธ๋กœ ์ตœ๊ทผ ์Šน์ธ๋ฐ›์€ ์˜คํ”ˆ์†Œ์Šค ์„ค๋ฃจ์…˜์œผ๋กœ, Notebook์ด๋ผ๊ณ  ํ•˜๋Š” ์›น ๊ธฐ๋ฐ˜ Workspace์— Spark, Tajo, Hive, ElasticSearch ๋“ฑ ๋‹ค์–‘ํ•œ ์„ค๋ฃจ์…˜์˜ API, Query ๋“ฑ์„ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์›น์— ๋‚˜ํƒ€๋‚ด๋Š” ์„ค๋ฃจ์…˜์ž…๋‹ˆ๋‹ค. http://zeppelin.apache.org/ ์•„ํŒŒ์น˜ ์ œํ”Œ๋ฆฐ ์†Œ๊ฐœ ๋ฐ”๋กœ ๊ฐ€๊ธฐ ์˜คํ”ˆ์†Œ์Šค ์ผ๊ธฐ 2: Apache Zeppelin ์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€? Hue ํ•˜๋‘ก ํœด(Hue, Hadoop User Experience)๋Š” ํ•˜๋‘ก๊ณผ ํ•˜๋‘ก ์—์ฝ” ์‹œ์Šคํ…œ์˜ ์ง€์›์„ ์œ„ํ•œ ์›น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ์˜คํ”ˆ ์†Œ์Šค์ž…๋‹ˆ๋‹ค. Hive ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ , ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•œ ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์žก์˜ ์Šค์ผ€์ค„๋ง์„ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์™€ ์žก, HDFS, ๋“ฑ ํ•˜๋‘ก์„ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. https://github.com/cloudera/hue ๋ณด์•ˆ Ranger ๋ ˆ์ธ์ €๋Š” ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ์˜ ๊ฐ ๋ชจ๋“ˆ์— ๋Œ€ํ•œ ๋ณด์•ˆ ์ •์ฑ…์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. HDFS์˜ ACL, Hive ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ ‘๊ทผ ๊ถŒํ•œ ๋“ฑ์˜ ๋ณด์•ˆ ์ •์ฑ…๊ณผ ๊ฐ ๋ชจ๋“ˆ์— ๋Œ€ํ•œ ์ ‘๊ทผ ๊ธฐ๋ก(Audit)์„ ๋ณด๊ด€ํ•ฉ๋‹ˆ๋‹ค. https://ranger.apache.org/ ๋ฐ์ดํ„ฐ ๊ฑฐ๋ฒ„๋„Œ์Šค ๋ฐ์ดํ„ฐ ๊ฑฐ๋ฒ„๋„Œ์Šค๋Š” ๊ธฐ์—…์˜ ์—ฌ๊ธฐ์ €๊ธฐ ์‚ฐ์žฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ™์€ ์ €์žฅ์†Œ์— ๊ด€๋ฆฌ, ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๊ทœ์น™์— ๋งž๊ฒŒ ํ‘œ์ค€ํ™”ํ•˜๋Š” ์ „์‚ฌ ์ฐจ์›์˜ ๋น…๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ ์ฒด๊ณ„์ž…๋‹ˆ๋‹ค. Atlas ์•„ํ‹€๋ผ์Šค๋Š” ๋ฐ์ดํ„ฐ ๊ฑฐ๋ฒ„๋„Œ์Šค๋กœ ์กฐ์ง์ด ๋ณด์•ˆ/์ปดํ”Œ๋ผ์ด์–ธ์Šค ์š”๊ตฌ์‚ฌํ•ญ์„ ์ค€์ˆ˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ž์›์— ๋Œ€ํ•œ ํƒœ๊น…, ๋‹ค์šด์ŠคํŠธ๋ฆผ ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•œ ํƒœ๊ทธ ์ „ํŒŒ, ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ์— ๋Œ€ํ•œ ๋ณด์•ˆ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ์•Œ๋ฆผ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ , Hive, HBase, Kafka์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์„ ์•Œ๋ฆฌ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Atlas Amundsen ์•„๋ฌธ์„ผ์€ ๋ฐ์ดํ„ฐ ๋””์Šค์ปค๋ฒ„๋ฆฌ ํ”Œ๋žซํผ์ž…๋‹ˆ๋‹ค. ๊ธฐ์—…์— ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ , ์ถ”์ฒœํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰, ์ถ”์ฒœ, ๋ฏธ๋ฆฌ ๋ณด๊ธฐ/์นผ๋Ÿผ ํ†ต๊ณ„/์†Œ์œ ์ž/์ฃผ ์‚ฌ์šฉ์ž๋“ค์ด ์ž˜ ํ‘œํ˜„๋œ ํ…Œ์ด๋ธ” ์ƒ์„ธ ํŽ˜์ด์ง€๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. Amundsen 1-Flume ํด๋ผ์šฐ๋ฐ๋ผ์—์„œ ๋Œ€๋Ÿ‰์˜ ๋กœ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ์†Œ์Šค์—์„œ ์ˆ˜์ง‘ํ•˜์—ฌ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ๊ฐœ๋ฐœํ•˜์—ฌ ์˜คํ”ˆ ์†Œ์Šค๋กœ ๊ณต๊ฐœํ•œ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” ์•„ํŒŒ์น˜ ํƒ‘ ๋ ˆ๋ฒจ ์˜คํ”ˆ ์†Œ์Šค๋กœ Flume 1.1.0๋ถ€ํ„ฐ ๊ตฌ์กฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด Flume-OG์™€ Flume-NG(New Generation)๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค. 2020.04 ํ˜„์žฌ 1.9.0 ๋ฒ„์ „์ด ์ตœ์‹  ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. ํŠน์ง• ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ”Œ๋ฃธ ์—์ด์ „ํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ํ™•์žฅ ๊ฐ€๋Šฅ ๋‹ค์–‘ํ•œ ์—ฐ๊ฒฐ ๋ชจ๋“œ๋ฅผ ์ง€์›ํ•˜์—ฌ ์ตœ์ข… ๋ชฉ์ ์ง€์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ์œ ์—ฐํ•œ ๊ตฌ์„ฑ์ด ๊ฐ€๋Šฅ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ, File, DB์— ์ž„์‹œ ์ €์žฅํ•˜์—ฌ ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ๋ณต๊ตฌ ๊ฐ€๋Šฅํ•˜๊ฒŒ ์„ค์ •ํ•˜์—ฌ ์‹ ๋ขฐ์„ฑ ์žˆ์Œ Flume ๊ตฌ์กฐ: OG vs NG OG๋Š” master๊ฐ€ agent๋ฅผ ๊ด€๋ฆฌ ๋ฐ์ดํ„ฐ๊ฐ€ ์ง‘์ค‘๋˜๋ฉด master์˜ ๋ณ‘๋ชฉํ˜„์ƒ์ด ๋ฐœ์ƒํ•จ Flume OG Flume NG ์šฉ์–ด Event Flume์—์„œ ์ „๋‹ฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋‹จ์œ„ ํ—ค๋”์™€ ๋ณด๋””๋กœ ๊ตฌ์„ฑ ํ—ค๋”: ์„ค์ •๊ฐ’. ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ ๋ณด๋””: ์ „๋‹ฌํ•  ๋ฐ์ดํ„ฐ # ์ •๋ณด ์˜ˆ์‹œ 2019-07-18 05:19:41,401 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:95)] Event: { headers:{file=/temp.file} body: 31 32 33 31 32 33 0D 123123. } Agent ์—์ด์ „ํŠธ๋Š” ์†Œ์Šค, ์ฑ„๋„, ์‹ฑํฌ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘์„ ์œ„ํ•œ JVM ํ”„๋กœ์„ธ์Šค ์†Œ์Šค๋กœ ์ž…๋ ฅ๋œ ๋ฉ”์‹œ์ง€๋ฅผ ์ฑ„๋„์— ์ €์žฅํ•˜๊ณ , ์ €์žฅ๋œ ๋ฉ”์‹œ์ง€ ๋ฌถ์Œ์„ ์‹ฑํฌ๋กœ ์ „๋‹ฌ ์†Œ์Šค(Source) ์›น ์„œ๋ฒ„ ๊ฐ™์€ ์™ธ๋ถ€ ์†Œ์Šค์— ์˜ํ•ด ์ „๋‹ฌ๋˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์ง‘ ์™ธ๋ถ€ ์†Œ์Šค๋Š” Flume์ด ์ธ์‹ํ•˜๋Š” ํ˜•ํƒœ๋กœ ์ด๋ฒคํŠธ๋ฅผ ์ „๋‹ฌ Avro, Thrift, File Http ์†Œ์Šค ๋“ฑ์ด ์žˆ์Œ ์ฑ„๋„(Channel) ์†Œ์Šค๊ฐ€ ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์‹ ํ•˜๋ฉด ์ฑ„๋„์— ์ž„์‹œ ์ €์žฅ ์ฑ„๋„์€ ์‹ฑํฌ๊ฐ€ ์ด๋ฒคํŠธ๋ฅผ ๋‹ค๋ฅธ ๋ชฉ์ ์ง€๋กœ ์ „๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํŒŒ์ผ์ด๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ๋“ฑ์— ์ด๋ฒคํŠธ๋ฅผ ๋ณด๊ด€ ๋ฉ”๋ชจ๋ฆฌ, File, Kafka, DB ์ฑ„๋„ ๋“ฑ์ด ์žˆ์Œ ์‹ฑํฌ(Sink) ์ฑ„๋„์— ์ €์žฅ๋œ ์ด๋ฒคํŠธ๋ฅผ ์™ธ๋ถ€ ์ €์žฅ์†Œ, ๋‹ค๋ฅธ ํ”Œ๋ฃธ ์—์ด์ „ํŠธ๋กœ ์ „๋‹ฌ ์†Œ์Šค์™€ ์‹ฑํฌ๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ง„ํ–‰ HDFS, Hive, Thrift, Avro ์‹ฑํฌ๊ฐ€ ์žˆ์Œ ์ฑ„๋„ ์‹ค๋ ‰ํ„ฐ(Channel Selector) ํ•˜๋‚˜์˜ ์†Œ์Šค์— ๋‹ค์ˆ˜์˜ ์ฑ„๋„์ด ์—ฐ๊ฒฐ๋˜์—ˆ์„ ๋•Œ ์ด๋ฒคํŠธ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ธฐ์ค€์œผ๋กœ ๋ณต์ œ(replicating), ๋ฉ€ํ‹ฐํ”Œ๋ ‰์‹ฑ(multiplexing)์ด ์žˆ์Œ ๋ณต์ œ: ๋ชจ๋“  ์ฑ„๋„์— ๋™์ผํ•œ ์ด๋ฒคํŠธ๋ฅผ ์ „๋‹ฌ. ๊ธฐ๋ณธ ์„ค์ • ๋ฉ€ํ‹ฐํ”Œ๋ ‰์‹ฑ: ํ—ค๋” ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ๋ถ„๊ธฐ ์‹ฑํฌ ํ”„๋กœ์„ธ์„œ(Sink Processors) ์ฑ„๋„์— ์—ฐ๊ฒฐ๋œ ์‹ฑํฌ๋ฅผ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด์„œ ์‚ฌ์šฉ ๊ธฐ๋ณธ ์„ค์ •์€ ํ•œ ๊ฐœ์˜ ์‹ฑํฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , Failover ๋ชจ๋“œ ์‹œ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ๋ชจ๋“œ๋ถ€ํ„ฐ ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋‹ค์Œ ์ˆœ์œ„์˜ ์‹ฑํฌ๋ฅผ ์‚ฌ์šฉ ์ธํ„ฐ์…‰ํ„ฐ(Interceptor) ์†Œ์Šค๋กœ ๋“ค์–ด์˜จ ์ด๋ฒคํŠธ์˜ ํ—ค๋”๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜, ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉ ์‹œ๊ฐ„ ์ถ”๊ฐ€(TimeStamp Interceptor)๋‚˜, ์ˆ˜์ง‘ ์„œ๋ฒ„(Host Interceptor) ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉ ์—ฐ๊ฒฐ ๋ชจ๋“œ multi-agent flow ์—์ด์ „ํŠธ์˜ ์‹ฑํฌ์™€ ์†Œ์Šค๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ, ๋‹ค์ˆ˜์˜ ์—์ด์ „ํŠธ๋ฅผ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ์—ฐ๊ฒฐ Consolidation ํ•˜๋‚˜์˜ ์—์ด์ „ํŠธ๊ฐ€ ์—ฌ๋Ÿฌ ์—์ด์ „ํŠธ์—์„œ ์ˆ˜์ง‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌ Multiplexing the flow ํ•˜๋‚˜์˜ ์—์ด์ „ํŠธ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ฑ„๋„๋กœ ์ด๋ฒคํŠธ๋ฅผ ์ „๋‹ฌ ์‹คํ–‰ ์˜ˆ์ œ netcat ์†Œ์Šค, ๋ฉ”๋ชจ๋ฆฌ ์ฑ„๋„, ๋กœ๊ทธ ์‹ฑํฌ๋ฅผ ์ด์šฉํ•˜์—ฌ http๋กœ ์ „๋‹ฌ๋œ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. flume ๋‹ค์šด๋กœ๋“œ ๋ฐ ์••์ถ• ํ•ด์ œ # flume download wget http://apache.mirror.cdnetworks.com/flume/1.9.0/apache-flume-1.9.0-bin.tar.gz # ์••์ถ• ํ•ด์ œ tar -zxvf apache-flume-1.9.0-bin.tar.gz configuration ํŒŒ์ผ ์ˆ˜์ • # ์„ค์ • ํŒŒ์ผ ์ˆ˜์ • vi conf/example.conf # a1 ์—์ด์ „ํŠธ ์„ ์–ธ. ์†Œ์Šค:r1, ์‹ฑํฌ:k1, ์ฑ„๋„:c1 a1.sources = r1 a1.sinks = k1 a1.channels = c1 # ์†Œ์Šค ์„ค์ •. 44444 ํฌํŠธ์— netcat์œผ๋กœ ์ฝ์Œ a1.sources.r1.type = netcat a1.sources.r1.bind = localhost a1.sources.r1.port = 44444 # ์‹ฑํฌ ์„ค์ •. ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธฐ๋Š” ํƒ€์ž…. ๋””๋ฒ„๊ทธ์šฉ a1.sinks.k1.type = logger # ์ฑ„๋„ ์„ค์ •. ๋ฉ”๋ชจ๋ฆฌ ํƒ€์ž… a1.channels.c1.type = memory a1.channels.c1.capacity = 1000 a1.channels.c1.transactionCapacity = 100 # ์—์ด์ „ํŠธ์˜ ์†Œ์Šค, ์ฑ„๋„, ์‹ฑํฌ๋ฅผ ์—ฐ๊ฒฐ a1.sources.r1.channels = c1 a1.sinks.k1.channel = c1 ์‹คํ–‰ ./bin/flume-ng agent --conf conf --conf-file ./conf/example.conf --name a1 -Dflume.root.logger=INFO, console telnet localhost 44444 2-Kafka Kafka๋Š” LinkedIn์—์„œ ๊ฐœ๋ฐœํ•œ ๋ถ„์‚ฐ ์ŠคํŠธ๋ฆฌ๋ฐ ํ”Œ๋žซํผ์ž…๋‹ˆ๋‹ค. ๋ฉ”์‹œ์ง•, ๋ฉ”ํŠธ๋ฆญ ์ˆ˜์ง‘, ๋กœ๊ทธ ์ˆ˜์ง‘, ์ŠคํŠธ๋ฆผ ์ฒ˜๋ฆฌ ๋“ฑ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2020๋…„ 5์›” ๊ธฐ์ค€ 2.5.0 ๋ฒ„์ „์ด ์ตœ์‹  ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. ํŠน์ง• ๋น ๋ฅด๋‹ค: Fast ์ˆ˜ ์ฒœ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์†Œ์Šค๋กœ๋ถ€ํ„ฐ ์ดˆ๋‹น ์ˆ˜๋ฐฑ ๋ฉ”๊ฐ€ ๋ฐ”์ดํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„๋„ ์•ˆ์ •์ ์œผ๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ ํ™•์žฅ ๊ฐ€๋Šฅ: Scalable ๋ฉ”์‹œ์ง€๋ฅผ ํŒŒํ‹ฐ์…˜์œผ๋กœ ๋ถ„๋ฆฌํ–์—ฌ ๋ถ„์‚ฐ ์ €์žฅ, ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด ํด๋Ÿฌ์Šคํ„ฐ๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ํ™•์žฅ ๊ฐ€๋Šฅ ์•ˆ์ •์ ์ด๋‹ค: Durable ํด๋Ÿฌ์Šคํ„ฐ์— ํŒŒํ‹ฐ์…˜ ๋ณต์ œํ•˜์—ฌ ์žฅ์•  ๋‚ด๊ตฌ์„ฑ์„ ๊ฐ€์ง ๋ฐœํ–‰/๊ตฌ๋…(Pub/Sub) ๋ชจ๋ธ Kafka๋Š” ๋ฐœํ–‰-๊ตฌ๋…(Pub/Sub) ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋ฐœํ–‰-๊ตฌ๋… ๋ชจ๋ธ์€ ๋ฐœํ–‰์ž(Producer)๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ํŠน์ • ์ˆ˜์‹ ์ž์—๊ฒŒ ์ง์ ‘ ๋ณด๋‚ด๋Š” ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ผ ์ฃผ์ œ(topic)์— ๋งž๊ฒŒ ๋ธŒ๋กœ์ปค์—๊ฒŒ ์ „๋‹ฌํ•˜๋ฉด ๊ตฌ๋…์ž(Consumer)๊ฐ€ ๋ธŒ๋กœ์ปค์— ์š”์ฒญํ•ด์„œ ๊ฐ€์ ธ๊ฐ€๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๋ฐœํ–‰์ž๋Š” ๋ฉ”์‹œ์ง€๋ฅผ topic์œผ๋กœ ์นดํ…Œ๊ณ ๋ฆฌํ™” ๊ตฌ๋…์ž๋Š” topic์— ๋งž๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ๋ธŒ๋กœ์ปค์—๊ฒŒ ์š”์ฒญ ๋ฐœํ–‰์ž์™€ ๊ตฌ๋…์ž๋Š” ์„œ๋กœ ์•Œ์ง€ ๋ชปํ•จ ์นดํ”„์นด์˜ ์ฃผ์š” ๊ตฌ์„ฑ Topic, Partiton Producer, Consumer Broker, Zookeepr Consumer Group Replication Topic-Partition Topic ๋ฉ”์‹œ์ง€๋Š” topic์œผ๋กœ ๋ถ„๋ฅ˜ topic์€ ๋ฐœํ–‰์ž๊ฐ€ ์ŠคํŠธ๋ฆผ์„ ๋ฐœํ–‰ํ•˜๋Š” ๋‹จ์œ„ ์ŠคํŠธ๋ฆผ์˜ ๋ฐœํ–‰๊ณผ ๊ตฌ๋…์€ topic ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌ Partition ํ•˜๋‚˜์˜ topic์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒํ‹ฐ์…˜์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ 1๊ฐœ์˜ ํ† ํ”ฝ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒํ‹ฐ์…˜์œผ๋กœ ์ €์žฅ๋˜๊ณ , ํ•˜๋‚˜์˜ ํŒŒํ‹ฐ์…˜์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋กœ๊ทธ๋กœ ๊ธฐ๋ก 1:N = Topic:Partition, 1:N = Partition:Log ํ•˜๋‚˜์˜ ํ† ํ”ฝ์„ ์—ฌ๋Ÿฌ ํŒŒํ‹ฐ์…˜์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ๋ฉ”์‹œ์ง€ ๋ถ„์‚ฐ ์ฒ˜๋ฆฌ๋กœ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๋†’์ผ ์ˆ˜ ์žˆ๊ณ , ๋ถ„์‚ฐ ์ €์žฅ์„ ํ†ตํ•ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ์Œ ํŒŒํ‹ฐ์…˜์˜ ํฌ๊ธฐ๋Š” ์šด์˜ ์ค‘์— ๋™์ ์œผ๋กœ ์ค„์ผ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํŒŒํ‹ฐ์…˜ ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •ํ•  ๋•Œ ์ฃผ์˜ํ•ด์•ผ ํ•จ ํŒŒํ‹ฐ์…˜์—์„œ ๋ฉ”์‹œ์ง€์˜ ์ƒ๋Œ€์ ์ธ ์œ„์น˜๋ฅผ ์˜คํ”„์…‹(offset)์ด๋ผ ํ•˜๊ณ , ๊ตฌ๋…์ž๋Š” ํ˜„์žฌ๊นŒ์ง€ ์ฝ์€ ์˜คํ”„์…‹์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญ ํŒŒํ‹ฐ์…˜์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณต์ œ๋ณธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ €์žฅ๋จ ํŒŒํ‹ฐ์…˜ replica ์„ค์ •์— ๋”ฐ๋ผ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์ œํ•˜์—ฌ ์ €์žฅ ๋ฐ์ดํ„ฐ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋ณต์ œ๋œ ๋ฐ์ดํ„ฐ๋กœ ๋ณต๊ตฌ ํŒŒํ‹ฐ์…˜์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ผ์šด๋“œ ๋กœ๋นˆ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ 3๊ฐœ์˜ ํŒŒํ‹ฐ์…˜์ด ์žˆ์œผ๋ฉด P0, P1, P2์— ์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅ ํŒŒํ‹ฐ์…˜ ๋ถ„๋ฐฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ค์ •ํ•˜์—ฌ ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํŒŒํ‹ฐ์…˜ ๋ถ„๋ฅ˜๋„ ๊ฐ€๋Šฅ P0์€ A ํ‚ค, P1์€ B ํ‚ค๋งŒ ์ €์žฅ Producer, Consumer ๋ฐœํ–‰์ž(Producer) ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ์‚ฐํ•˜๋Š” ์ฃผ์ฒด ๋ฉ”์‹œ์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ๋ธŒ๋กœ์ปค(Broker)์—๊ฒŒ ํ† ํ”ฝ(Topic)์œผ๋กœ ๋ถ„๋ฅ˜๋œ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌ ๋ฉ”์‹œ์ง€๋Š” ๋ฐฐ์น˜ ํ˜•ํƒœ๋กœ ์ „๋‹ฌ ๋ฐœํ–‰์ž๋Š” ๊ตฌ๋…์ž์˜ ์กด์žฌ๋ฅผ ์•Œ์ง€ ๋ชปํ•จ ๊ตฌ๋…์ž(Consumer) ์†Œ๋น„์ž๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์†Œ๋น„ํ•˜๋Š” ์ฃผ์ฒด ๋ฐœํ–‰์ž์˜ ์กด์žฌ๋ฅผ ์•Œ์ง€ ๋ชปํ•จ ์›ํ•˜๋Š” ํ† ํ”ฝ์„ ๊ตฌ๋…ํ•˜์—ฌ ์Šค์Šค๋กœ ์กฐ์ ˆํ•ด๊ฐ€๋ฉด์„œ ์†Œ๋น„ํ•  ์ˆ˜ ์žˆ์Œ ์›ํ•˜๋Š” ํ† ํ”ฝ์˜ ๊ฐ ํŒŒํ‹ฐ์…˜์— ์กด์žฌํ•˜๋Š” ์˜คํ”„์…‹์˜ ์œ„์น˜๋ฅผ ๊ธฐ์–ตํ•˜๊ณ  ๊ด€๋ฆฌํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต์„ ๊ด€๋ฆฌ ์˜คํ”„์…‹ ๊ด€๋ฆฌ๋ฅผ ํ†ตํ•ด ๋ฐœํ–‰์ž, ๊ตฌ๋…์ž์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•ด๋„ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ฝ์—ˆ๋˜ ์œ„์น˜์—์„œ๋ถ€ํ„ฐ ๋‹ค์‹œ ๊ตฌ๋… ๊ฐ€๋Šฅ fail-over์— ๋Œ€ํ•œ ์‹ ๋ขฐ๊ฐ€ ์กด์žฌ ๊ตฌ๋…์ž ๊ทธ๋ฃน(Consumer Group) ๊ตฌ๋…์ž๋“ค์˜ ๋ฌถ์Œ์œผ๋กœ ํ•˜๋‚˜์˜ ํŒŒํ‹ฐ์…˜์— ํ•˜๋‚˜์˜ ๊ตฌ๋…์ž ๊ทธ๋ฃน์ด ์กด์žฌํ•จ ํŒŒํ‹ฐ์…˜์€ ๊ตฌ๋…์ž ๊ทธ๋ฃน์˜ ๊ตฌ๋…์ž์™€ 1:N ๋งค์นญ ํŒŒํ‹ฐ์…˜ 4 : ๊ตฌ๋…์ž 4 = ํŒŒํ‹ฐ์…˜๊ณผ ๊ตฌ๋…์ž๊ฐ€ 1:1๋กœ ๋งค์นญ ํŒŒํ‹ฐ์…˜ 4 : ๊ตฌ๋…์ž 5 = ๊ตฌ๋…์ž 1๊ฐœ๋Š” ๋Œ€๊ธฐ. ํŒŒํ‹ฐ์…˜ 5 : ๊ตฌ๋…์ž 4 = ๊ตฌ๋…์ž 1๊ฐœ๋Š” 2๊ฐœ์˜ ํŒŒํ‹ฐ์…˜์„ ์†Œ๋น„ ํŒŒํ‹ฐ์…˜์„ ๋Š˜๋ฆด ๋•Œ๋Š”, ๊ตฌ๋…์ž์˜ ๊ฐœ์ˆ˜๋„ ๊ณ ๋ คํ•ด์•ผ ํ•จ ๊ธฐ๋ณธ ์„ค์ •์€ ํŒŒํ‹ฐ์…˜๊ณผ ๊ตฌ๋…์ž์˜ ์ˆ˜๋ฅผ ๋™์ผํ•˜๊ฒŒ ์„ค์ • ๋ฉ”์‹œ์ง€๊ฐ€ ํŒŒํ‹ฐ์…˜์— ์Œ“์ด๋Š” ์†๋„์™€ ๊ตฌ๋…์ž๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋Š” ์†๋„๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ ์ ˆํ•œ ๊ฐœ์ˆ˜ ์„ค์ •์ด ํ•„์š”ํ•จ ๊ตฌ๋…์ž ๊ทธ๋ฃน์—์„œ ํ•˜๋‚˜์˜ ๊ตฌ๋…์ž์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋‹ค๋ฅธ ๊ตฌ๋…์ž๊ฐ€ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ตฌ๋…์ž์˜ ํŒŒํ‹ฐ์…˜ ๋ฐ์ดํ„ฐ๋„ ํ•จ๊ป˜ ์ฒ˜๋ฆฌํ•˜์—ฌ ์žฅ์• ๋ฅผ ๊ทน๋ณตํ•จ ๋ธŒ๋กœ์ปค(Kafka Cluster) ํด๋Ÿฌ์Šคํ„ฐ๋กœ ๊ตฌ์„ฑ๋œ ๋ฉ”์‹œ์ง€ ํ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ ๋ฉ”์‹œ์ง€๋Š” ํด๋Ÿฌ์Šคํ„ฐ์— ํŒŒํ‹ฐ์…˜ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด ๊ด€๋ฆฌ/๋ณต์ œ๋จ ํŒŒ์ผ ์‹œ์Šคํ…œ์— ๋ฉ”์‹œ์ง€๋ฅผ ์ €์žฅํ•˜๋ฏ€๋กœ ์œ ์‹ค์ด ์—†๊ณ  ๋ณต๊ตฌ ๊ฐ€๋Šฅ ํ•˜๋“œ๋””์Šคํฌ์˜ ์ˆœ์ฐจ์  ์ฝ๊ธฐ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜์—ฌ ์†๋„๋ฅผ ์œ ์ง€ ๊ตฌ๋…์ž๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ๊ฐ€์ ธ๊ฐ€๋„ ๋ฐ”๋กœ ์‚ญ์ œํ•˜์ง€ ์•Š์Œ ๊ธฐ๋ณธ ์„ค์ •์€ 7์ผ๊ฐ„ ์ €์žฅํ•˜๊ณ  ์‚ญ์ œ ์˜ˆ์ œ # ์นดํ”„์นด ๋‹ค์šด๋กœ๋“œ $ wget http://apache.mirror.cdnetworks.com/kafka/2.3.0/kafka_2.12-2.3.0.tgz # ์••์ถ• ํ•ด์ œ $ tar -xf kafka_2.12-2.3.0.tgz # 1. ์ฃผํ‚คํผ ์‹คํ–‰ $ bin/zookeeper-server-start.sh config/zookeeper.properties # 2. kafka ์„œ๋ฒ„ ์‹คํ–‰: ๋ธŒ๋กœ์ปค: Broker $ bin/kafka-server-start.sh config/server.properties # 3. Topic ์ƒ์„ฑ $ bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test # 3-1. Topic ํ™•์ธ $ bin/kafka-topics.sh --list --bootstrap-server localhost:9092 test # 4. ๋ฉ”์‹œ์ง€ ์ „์†ก: ํ”„๋กœ๋“€์„œ: Producer $ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test >abcd >efgh # 5. ๋ฉ”์‹œ์ง€ ํ™•์ธ: ์ปจ์Šˆ๋จธ: Consumer $ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning abcd efgh ์ฐธ๊ณ  KAFKA์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž ์นด์นด์˜ค๋Š” ๊ฐ ์„œ๋ฒ„๊ฐ€ ์‹œ์Šคํ…œ ์ ์œผ๋กœ ๋ฌถ์—ฌ ์žˆ์–ด์„œ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋™์‹œ์— ๋‹ค์šด๋˜์–ด ์นดํ”„์นด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ปคํ”Œ๋ง์„ ์ค„์—ฌ์คŒ. ํŒŒํ‹ฐ์…˜์€ 8~20๊ฐœ๋กœ ์„ค์ • Kafka ๊ธฐ๋ณธ ๊ฐœ๋… ์žก๊ธฐ Kafka๋ฅผ ์ด์šฉํ•œ ๋ฉ”์‹œ์ง• ์‹œ์Šคํ…œ ๊ตฌ์„ฑํ•˜๊ธฐ MSA ์•„ํ‚คํ…์ฒ˜ ๊ตฌ์„ฑํ•  ๋•Œ Kafka๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฉ”์‹œ์ง• ์‹œ์Šคํ…œ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ• Kafka ์šด์˜์ž๊ฐ€ ๋งํ•˜๋Š” ์ฒ˜์Œ ์ ‘ํ•˜๋Š” Kafka ํŒŒํ‹ฐ์…˜ ์ˆœ์„œ์— ๋”ฐ๋ฅธ ๋ฉ”์‹œ์ง€ ์ˆœ์„œ: ํŒŒํ‹ฐ์…˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ผ ๋•Œ ๋ฉ”์‹œ์ง€๋Š” ํŒŒํ‹ฐ์…˜์˜ ์—ฌ๋Ÿฌ ์œ„์น˜์— ์ €์žฅ๋˜์–ด ๊ตฌ๋…์ž๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๋ฉ”์‹œ์ง€์˜ ๋ฐœ์ƒ ์ˆœ์„œ์™€ ๊ตฌ๋…์ž๊ฐ€ ๋ฐ›์€ ๋ฉ”์‹œ์ง€์˜ ์ˆœ์„œ๊ฐ€ ๊ผญ ์ผ์น˜ํ•˜์ง€๋Š” ์•Š์Œ Kafka ์šด์˜์ž๊ฐ€ ๋งํ•˜๋Š” Kafka Consumer Group ๊ตฌ๋…์ž ๊ทธ๋ฃน(Consumer Group): ์žฅ์•  ๋ณต๊ตฌ์„ฑ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ณ , ๋ถ„์‚ฐ ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ๋น ๋ฅธ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•จ ์นดํ”„์นด ์„ค์น˜ ์‹œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์„ค์ • 4๊ฐ€์ง€ log.retention.hours=72: ๋ธŒ๋กœ์ปค์— ๋กœ๊ทธ ์œ ์ง€ ์‹œ๊ฐ„. ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์•„์ง€๋ฉด ๋ณต์ œ์— ์˜ํ•ด ๋งŽ์€ ์šฉ๋Ÿ‰์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Œ. ์ ์ ˆํ•œ ์‹œ๊ฐ„ ์„ค์ •์ด ํ•„์š”ํ•จ. delete.topic.enable=true: ๋””์Šคํฌ ์šฉ๋Ÿ‰ ํ™•๋ณด๋ฅผ ์œ„ํ•ด ํ† ํ”ฝ์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•จ. ์ด ์˜ต์…˜์„ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐ”๋กœ ์‚ญ์ œ๋˜์ง€ ์•Š๊ณ , ์‚ญ์ œ ํ”Œ๋ž˜๊ทธ์— ์ฒดํฌ๋งŒ ๋จ allow.auto.create.topics=false: ์„ ์–ธํ•˜์ง€ ์•Š์€ ํ† ํ”ฝ์œผ๋กœ ๋ฉ”์‹œ์ง€๊ฐ€ ๋“ค์–ด์˜ฌ ๋•Œ ํ† ํ”ฝ์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š๋„๋ก ์„ค์ •. log.dirs=/data: ๋ฉ”์‹œ์ง€๊ฐ€ ์ €์žฅ๋˜๋Š” ์‹ค์ œ ๊ฒฝ๋กœ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์„ค์ •ํ•ด์•ผ ํ•จ LINE์—์„œ Kafka๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• - 1ํŽธ ๋ถ„์‚ฐ ํ์ž‰ ์‹œ์Šคํ…œ๊ณผ ๋ฐ์ดํ„ฐ ํ—ˆ๋ธŒ๋กœ ์ด์šฉ ํ•˜๋‚˜์˜ ํด๋Ÿฌ์Šคํ„ฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง‘์ค‘ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํ—ˆ๋ธŒ ์ฝ˜์…‰ํŠธ๋กœ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋‹จ์ˆœํ•˜๊ฒŒ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๊ณ , ์šด์˜์˜ ํšจ์œจ์„ฑ์„ ์–ป์„ ์ˆ˜ ์žˆ์Œ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ๋†’์€ ์‹ ๋ขฐ์„ฑ์„ ํ™•๋ณด ์š”์ฒญ ์ˆ˜๋ฅผ ์ œ์–ด. ์•ˆ์ •์„ฑ ํ™•๋ณด๋ฅผ ์œ„ํ•ด ๋ฐ์ดํ„ฐ์˜ ์–‘๋ณด๋‹ค ์š”์ฒญ ์ˆ˜(request quota)๋ฅผ ์ œ์–ดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•จ ๋ธŒ๋กœ์ปค์˜ ์Šค๋ ˆ๋“œ ์‹œ๊ฐ„์„ ์ œ์–ดํ•˜์—ฌ ํ•˜๋‚˜์˜ ํ† ํ”ฝ์œผ๋กœ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ชฐ๋ ค๋„ ๋‹ค๋ฅธ ์ž‘์—…์— ์˜ํ–ฅ์ด ๊ฐ€์ง€ ์•Š๊ฒŒ ํ•จ 3-Sqoop Sqoop์€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ•˜๋‘ก HDFS ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋œ ์˜คํ”ˆ์†Œ์Šค ์†Œํ”„ํŠธ์›จ์–ด์ž…๋‹ˆ๋‹ค. 2009๋…„ ๋ฐœํ‘œ๋˜์—ˆ๊ณ , 2012๋…„์— Apache Top Level Project๋กœ ์ง€์ • Sqoop 1, Sqoop 2์˜ ๋‘ ๊ฐ€์ง€ ๋ฒ„์ „์ด ์กด์žฌํ•จ Sqoop 1์€ ํด๋ผ์ด์–ธํŠธ ๋ฐฉ์‹ CLI ๋ช…๋ น์–ด๋กœ ์ž‘์—…์„ ์‹คํ–‰ Sqoop 2๋Š” ํด๋ผ์ด์–ธํŠธ ๋ฐฉ์‹์— ์„œ๋ฒ„์‚ฌ์ด๋“œ ๋ฐฉ์‹์ด ์ถ”๊ฐ€๋จ Sqoop ์„œ๋ฒ„๊ฐ€ ์กด์žฌํ•˜๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ์„œ๋ฒ„์— ์š”์ฒญํ•˜์—ฌ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ์‹ HDFS์™€ RDB ๊ฐ„ ๋ฐ์ดํ„ฐ ์ „์†ก RDBMS > HDFS or HDFS > RDBMS ์ด๋™ ๊ฐ€๋Šฅ Hive, Pig, Hbase๋กœ ์ด๋™ ๊ฐ€๋Šฅ ์˜ˆ์ œ Sqoop์˜ ๋™์ž‘์€ import์™€ export๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. import๋Š” DB์˜ ๋ฐ์ดํ„ฐ๋ฅผ HDFS๋กœ ์˜ฎ๊ธฐ๋Š” ๋ฐฉ์‹์ด๊ณ , export๋Š” HDFS์˜ ๋ฐ์ดํ„ฐ๋ฅผ DB๋กœ ์˜ฎ๊ธฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. import: DB to HDFS sqoop import \ --connect jdbc:mysql://loclhost:7777/db? zeroDateTimeBehavior=convertToNull \ --user name scott \ --password tiger \ --query 'select * from sample_table WHERE $CONDITIONS' \ --target-dir hdfs://localhost/user/hadoop/ export: HDFS to DB sqoop export --connect jdbc:mysql://loclhost:7777/db?zeroDateTimeBehavior=convertToNull \ --user name scott \ --password tiger \ --table sample_table \ --export-dir hdfs://localhost/user/hadoop/ \ --columns column1, column2, column3 ์ฐธ๊ณ  SqoopUserGuide 4-๋น…๋ฐ์ดํ„ฐ ์‹œ์Šคํ…œ ์œ ๋ช… IT ํšŒ์‚ฌ์—์„œ ๋น…๋ฐ์ดํ„ฐ ์‹œ์Šคํ…œ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  แ„แ…กแ„แ…กแ„‹๋ฐ์ดํ„ฐ ํŒŒ์ดํ”„๋ผ์ธแ„แ…ฅแ„‘แ…กแ„‹แ…ตแ„‘แ…ณแ„…แ…กแ„‹แ…ตแ†ซ NDC 2018 - ์•ผ์ƒ์˜ ๋•… ๋“€๋ž‘๊ณ ์˜ ๋ฐ์ดํ„ฐ ์—”์ง€๋‹ˆ์–ด๋ง ์ด์•ผ๊ธฐ: ๋กœ๊ทธ ์‹œ์Šคํ…œ ๊ตฌ์ถ• ๊ฒฝํ—˜ ๊ณต์œ  NDC 2018 - SparkAirflow๋กœck, Airflow ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ํƒ„๋ ฅ์ ์ด๊ณ  ์œ ์—ฐํ•œ ๋ฐ์ดํ„ฐ ๋ถ„์‚ฐ์ฒ˜๋ฆฌ ์ž๋™ํ™” ์ธํ”„๋ผ ๊ตฌ์ถ• 1-๋น…๋ฐ์ดํ„ฐ ์‹œ์Šคํ…œ:๋ฉœ๋ก  ์ˆ˜์ง‘ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟฑ์„ ์ด์šฉํ•˜์—ฌ ์ˆ˜์ง‘ ๋กœ๊ทธ ๋ฐ์ดํ„ฐ ํ”Œ๋ฃธ์„ ์ด์šฉํ•˜์—ฌ ํ•œ ์‹œ๊ฐ„๋งˆ๋‹ค ์‰˜ ์Šคํฌ๋ฆฝํŠธ(scp)๋กœ ์ˆ˜์ง‘ ํ—ˆ๋“œ์Šจ์„ ์ด์šฉํ•˜์—ฌ ๋ฐฐ์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ ๋ถ„์„ ์‹ค์‹œ๊ฐ„ ๋ถ„์„๊ณผ ๋ฐฐ์น˜ ๋ถ„์„์„ ์ œ๊ณต Hive๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ถ„์„ ๊ฒฐ๊ณผ ์ œ๊ณต ๋ฐ์ดํ„ฐ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ MR, Mahout, Tajo, Spark๋„ ์ด์šฉ SQL ๊ธฐ๋ฐ˜์˜ ๋ถ„์„ ํ”Œ๋žซํผ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•จ ์„œ๋น„์Šค ์˜จ๋ผ์ธ ์„œ๋น„์Šค์™€ ํ†ต๊ณ„ ์‹œ์Šคํ…œ ์ œ๊ณต MySQL๊ณผ HBase, ElasticSearch๋ฅผ ์ด์šฉํ•˜์—ฌ ์ œ๊ณต ๋ฉœ๋ก  ์ค€ ์‹ค์‹œ๊ฐ„ ์ˆ˜์ง‘ Flume์„ ์ด์šฉํ•œ ์ค€ ์‹ค์‹œ๊ฐ„ ์ˆ˜์ง‘ ์ฐธ๊ณ  ๋ฉœ๋ก  ๋น…๋ฐ์ดํ„ฐ ์ด์•ผ๊ธฐ MelOn ๋น…๋ฐ์ดํ„ฐ ํ”Œ๋žซํผ๊ณผ Tajo ์ด์•ผ๊ธฐ 2-๋น…๋ฐ์ดํ„ฐ ์‹œ์Šคํ…œ:๋„ค์ด๋ฒ„ - DataProc ๋ฐ์ดํ„ฐ ๋กœ๊ทธ(DataLog) ์ผ๋ž˜์Šคํ‹ฑ ์„œ์น˜ ๊ธฐ๋ฐ˜ 2017๋…„์— ๊ตฌ์ถ•ํ•œ ๋กœ๊ทธ ํ†ตํ•ฉ ๊ด€๋ฆฌ ํ”Œ๋žซํผ ๊ฒ€์ƒ‰ ์„œ๋น„์Šค์˜ ๋ชจ๋“  ๋กœ๊ทธ๋ฅผ ํ•œ๊ณณ์— ๋ชจ์•„ ํšจ์œจ์ ์ธ ๋ถ„์„์„ ์œ„ํ•œ ํ™˜๊ฒฝ์„ ์ œ๊ณต ์ดˆ๋‹น 22๋งŒ ๊ฑด ์‹ค์‹œ๊ฐ„ ์ƒ‰์ธ์ด ๊ฐ€๋Šฅ ๋ฐ์ดํ„ฐ ์Šคํ† ์–ด(DataStore) HBase ๊ธฐ๋ฐ˜ ๋ฐ์ดํ„ฐ ์นดํƒˆ๋กœ๊ทธ๋ฅผ ํ†ตํ•ด ๋ณด๊ด€๋œ ๋ฐ์ดํ„ฐ์˜ ๋ชฉ๋ก, ์ƒ์„ธ์ •๋ณด, ์ƒ์‚ฐ์ž์™€ ์†Œ๋น„์ž๋ฅผ ํ•œ๋ˆˆ์— ์•Œ ์ˆ˜ ์žˆ๋„๋ก ์ œ๊ณต ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ํšจ์œจ์ ์ธ ํ™œ์šฉ์„ ์œ„ํ•ด SQL ๊ธฐ๋ฐ˜์˜ ์ฒ˜๋ฆฌ ์‹œ์Šคํ…œ์„ ๊ตฌ์ถ• ๋น„์Šทํ•œ ํ˜•ํƒœ์˜ ์š”์ฒญ์ด ๋งŽ์œผ๋ฏ€๋กœ SQL ํ…œํ”Œ๋ฆฟ์„ ์ œ๊ณตํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›(Hue) ๋น ๋ฅธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ๊ฐ€๊ณต ํ…Œ์ด๋ธ”์„ ์ œ๊ณต. ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฏธ๋ฆฌ ํ…Œ์ด๋ธ”๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์ ์žฌ ํ•˜์ด๋ธŒ์˜ ORC, ํŒŒํ‹ฐ์…˜, ๋ฒ„์ผ“ํŒ…์„ ์ ๊ทน ํ™œ์šฉ ๋ฐ์ดํ„ฐ ํ”„๋ก(DataProc) ๋ณด๊ด€๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋งˆ์Œ๊ป ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์„ ์ œ๊ณต ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž์œ ๋กญ๊ฒŒ ์ปดํ“จํŒ… ์ž์›์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์„ ์ œ๊ณต ์ฐธ๊ณ  [๋„ค์ด๋ฒ„ ์–ด๋ฒค์ €์Šค] ๊ตญ๋‚ด ์ตœ๋Œ€ ๋น…๋ฐ์ดํ„ฐ, ์ด๋ ‡๊ฒŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค ๋„ค์ด๋ฒ„ ๋กœ๊ทธ๋ฅผ ์ง€ํƒฑํ•˜๋Š” ํž˜ 3-๋น…๋ฐ์ดํ„ฐ ์‹œ์Šคํ…œ:์นด์นด์˜ค ๊ด‘๊ณ  ์‹œ์Šคํ…œ ์นด์นด์˜ค ๊ด‘๊ณ  ์‹œ์Šคํ…œ์€ ํ•˜๋ฃจ 59TB์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌ ์นด์นด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๊ฐ๋„๋กœ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ‚ค๋ฆฐ(Kylin)์„ ์ด์šฉ ํ‚ค๋ฆฐ์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ๋ธŒ ํ˜•ํƒœ๋กœ ๊ฐ€๊ณตํ•˜์—ฌ ๋ณด๊ด€ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž์˜ ์ฟผ๋ฆฌ์— ๋น ๋ฅธ ์†๋„๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ณต ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ๊ธฐ์ˆ  ์นดํ”„์นด, ๋กœ๊ทธ์Šคํƒœ์‰ฌ ์ŠคํŒŒํฌ ์ŠคํŠธ๋ฆฌ๋ฐ, ํ”Œ๋งํฌ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ํ•˜์ด๋ธŒ, ์ž„ํŒ”๋ผ ์ €์žฅ HDFS, ์นด์‚ฐ๋“œ๋ผ, HBase, ์ผ๋ž˜์Šคํ‹ฑ ์„œ์น˜, ๋ ˆ๋””์Šค ์‹œ๊ฐํ™” ํƒœ๋ธ”๋ฃจ, ์ œํ”Œ๋ฆฐ, ์นดํ”„์นด, ํ‚ค๋ฆฐ ์šด์˜ ์—์–ดํ”Œ๋กœ, ๊ทธ๋ผํŒŒ๋‚˜, ํ‚ค๋ฐ”๋‚˜, ํ”„๋กœ๋ฉ”ํ…Œ์šฐ์Šค ์ฐธ๊ณ  ์นด์นด์˜ค if 2019 - ๊ด‘๊ณ  ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์‹œ์Šคํ…œ ์†Œ๊ฐœ 4-๋น…๋ฐ์ดํ„ฐ ์‹œ์Šคํ…œ:์ฟ ํŒก ์ฟ ํŒก์€ RDB๋ฅผ ์ด์šฉํ•œ ์ดˆ๊ธฐ๋ถ€ํ„ฐ 2019๋…„๊นŒ์ง€ 4๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ์„œ ๋น…๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์‹œ์Šคํ…œ์„ ์™„์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํŽ˜์ด์ฆˆ 1 ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ด์šฉํ•œ ์ฒ˜๋ฆฌ ํŽ˜์ด์ฆˆ 2 ์˜จํ”„๋ฆฌ๋ฏธ์Šค ํ•˜๋‘ก, ํ•˜์ด๋ธŒ, MPP ์‹œ์Šคํ…œ ๋ฐ์ดํ„ฐ ์ƒ์‚ฐ๊ณผ ์ˆ˜์š”์˜ ์ฆ๊ฐ€ ๊ณ ๊ฐ์˜ ํ–‰๋™์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ฐ์ดํ„ฐ์˜ ์ฆ๊ฐ€๋กœ ์ธํ•œ ๋ณ‘๋ชฉ ํ˜„์ƒ ๋ฐœ์ƒ ์˜จ ํ”„๋ ˆ๋ฏธ์Šค ํ™˜๊ฒฝ์—์„œ๋Š” ์Šค์ผ€์ผ ์•„์›ƒ์ด ์‰ฝ์ง€ ์•Š์Œ ์•„ํ‚คํ…์ฒ˜ ๋ฐ์ดํ„ฐ ์†Œ์Šค์˜ ๊ตฌ๋ถ„ ServerLog, ClientLog, DBMS, External Data ํ•˜๋‘ก, MPP ์‹œ์Šคํ…œ(์•„๋งˆ๋„ ๊ทธ๋ฆฐํ”Œ๋Ÿผ) ํŽ˜์ด์ฆˆ 3 ํด๋ผ์šฐ๋“œ ์ธํ”„๋ผ๋กœ ์ด๋™ ๊ณ ๊ฐ์˜ ํ–‰๋™ ๊ธฐ๋ฐ˜ ๋กœ๊ทธ๋„ ์ˆ˜์ง‘ EDW๋ฅผ ์Šคํƒ€ ์Šคํ‚ค๋งˆ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌ์ถ• ์•„ํ‚คํ…์ฒ˜ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘์€ ๊ทธ๋Œ€๋กœ, ์ €์žฅ์€ ํด๋ผ์šฐ๋“œ ์ €์žฅ ๊ณต๊ฐ„ ์—์–ดํ”Œ๋กœ ์Šค์ผ€์ค„๋Ÿฌ ๋„์ž… ๋น…๋ฐ์ดํ„ฐ ๊ณต๊ฐ„๊ณผ EDW ๊ณต๊ฐ„์œผ๋กœ ๋ถ„๋ฆฌ ์‚ฌ์šฉ์ž๋ฅผ ์œ„ํ•œ ํœด, ํƒœ๋ธ”๋ฃจ, ์ œํ”Œ๋ฆฐ, CLI, ๋‚ด๋ถ€ ๋„๊ตฌ ๋“ฑ์„ ์ œ๊ณต ํŽ˜์ด์ฆˆ 4 ๋น…๋ฐ์ดํ„ฐ ํ”Œ๋žซํผ ํด๋Ÿฌ์Šคํ„ฐ ๋ผ์ดํ”„ ์‚ฌ์ดํด ์Šค์ผ€์ผ๋ง ์ •์ฑ… ๋จธ์‹  ์ด๋ฏธ์ง€ ์‚ฌ์ „ ๋นŒ๋“œ ์›น ๋กœ๊น… ํ”Œ๋žซํผ ์นดํ”„์นด๋ฅผ ์ด์šฉํ•œ ํ”Œ๋žซํผ ๊ตฌ์ถ• ๋ฐ์ดํ„ฐ ์›จ์–ดํ•˜์šฐ์Šค ํ•˜์ด๋ธŒ, ํœด, ํ”„๋ ˆ์ŠคํŠธ, ์ œํ”Œ๋ฆฐ์„ ์ด์šฉํ•œ ํ™˜๊ฒฝ ์ œ๊ณต ์ฐธ๊ณ  ์ฟ ํŒก ๋ฐ์ดํ„ฐ ํ”Œ๋žซํผ์˜ ์ง„ํ™” 5-๋น…๋ฐ์ดํ„ฐ ์‹œ์Šคํ…œ:LINE ๊ด‘๊ณ  ๋ฐ์ดํ„ฐ ํŒŒ์ดํ”„๋ผ์ธ BigDB LINE์˜ ๊ด‘๊ณ  ๋ฐ์ดํ„ฐ ํŒŒ์ดํ”„๋ผ์ธ BigDB์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (๊ธฐ์ค€. 2017.07) BigDB๋ž€? BigDB๋Š” LINE ๊ด‘๊ณ ์˜ ๋น…๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ํŒŒ์ดํ”„๋ผ์ธ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘, ๊ฐ€๊ณต, ์žฌ๊ฐ€๊ณต, ์กฐํšŒ ๋“ฑ์˜ ๊ธฐ๋Šฅ ์ œ๊ณต ์‹ค์‹œ๊ฐ„ ๋ถ„์„ LINE ๊ด‘๊ณ ์˜ ๋ถ„์„ ํ˜•ํƒœ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๊ด‘๊ณ ์— ๋…ธ์ถœ์ด ๋˜์—ˆ์„ ๊ฒฝ์šฐ ํ•ด๋‹น ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์•„์„œ ์ฆ‰์‹œ ์ฒ˜๋ฆฌ ๋ฐฐ์น˜ ๋ถ„์„ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์•„์„œ 1์‹œ๊ฐ„์ด๋‚˜ 1์ผ, ๋“ฑ๋กํ•œ ์‹œ๊ฐ„๋งˆ๋‹ค ์ฒ˜๋ฆฌ ์œ ์—ฐํ•œ ๋ฐ์ดํ„ฐ ์ œ๊ณต ๊ธฐ๋Šฅ BigDB๋Š” ๋ถ„์„์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณต ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ์™€ ์ •์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ Join ํ•˜์—ฌ ์ œ๊ณต ํŠน์ง• REST CLI(Command Line Interface) ๊ธฐ๋Šฅ์„ ์ œ๊ณต ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ์™€ ์ •์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ hiveContext Table๋กœ ์ƒ์„ฑํ•˜๋Š” ํ๋ฆ„์„ ์ œ์–ด ์‚ฌ์šฉ์ž ์š”์ฒญ์˜ ๋ฐ์ดํ„ฐ<NAME>์„ ์ง€์ •ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ, ์“ฐ๊ธฐ๋ฅผ ์œ„ํ•œ ๋ฉ€ํ‹ฐ์„ธ์…˜์„ ์ง€์› ์‚ฌ์šฉ์ž ์š”์ฒญ์˜ ๋ฐ์ดํ„ฐ๋ฅผ Spark SQL ๊ธฐ๋ฐ˜์œผ๋กœ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ Join ๊ธฐ๋Šฅ ์ œ๊ณต ์ €์žฅ์†Œ๋ฅผ ํฌ๊ฒŒ ๋‘ ๊ฐœ๋กœ ๊ตฌ๋ถ„ ์‚ฌ์šฉ์ž ์š”์ฒญ์˜ ๊ฐ€๊ณต ๋ฐ์ดํ„ฐ๋ฅผ ์Šค์ผ€์ผ์— ๋งž๋Š” ์ €์žฅ์†Œ๋ฅผ ์ง€์ •ํ•˜์—ฌ ๊ฐ€์šฉ์„ฑ์„ ํ™•๋ณดํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ์ œ๊ณต ๊ตฌ์กฐ ์„ค๋ฃจ์…˜ ๊ตฌ์กฐ ๋‹ค์–‘ํ•œ ์˜คํ”ˆ์†Œ์Šค๋ฅผ ํ™œ์šฉ ํ†ตํ•ฉ๋œ ํˆด์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ณ„๋„์˜ ์„ค๋ฃจ์…˜์„ ํฌํ•จํ•˜๊ณ , ์ด ์„ค๋ฃจ์…˜์ด ๋…ธ๋“œ์˜ ์ž์›์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Œ ๋‹ค์–‘ํ•œ ์˜คํ”ˆ์†Œ์Šค๋ฅผ ํ™œ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์˜์กด์„ฑ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ ์ตœ๋Œ€ํ•œ ๋ณ€๊ฒฝ์€ ์—†๊ฒŒ ํ•˜๊ณ , ์„ค๋ฃจ์…˜ ๊ฐ„์˜ ํ๋ฆ„์— ๋Œ€ํ•œ ์ปจํŠธ๋กค ์—ดํ• ์„ BigDB Core์™€ API๋กœ ๊ฐœ๋ฐœ Message Proxy: akka.http๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ JSON ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ , Kafka์˜ ์ •ํ•ด์ง„ Topic์— produce ํ•ฉ๋‹ˆ๋‹ค. Kafka: ์ˆ˜์ง‘๋œ JSON ๋ฐ์ดํ„ฐ๋ฅผ 7์ผ๊ฐ„ ๋ณด๊ด€ํ•˜๋ฉฐ, Partition์€ ์ŠคํŠธ๋ฆฌ๋ฐ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”์–ด์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. Streaming: Spark์„ ์‚ฌ์šฉํ•˜๋ฉฐ, 5์ดˆ ์ฃผ๊ธฐ๋กœ Task๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ์˜ ๋ณ€ํ™˜ ์ž‘์—… ํ›„ ์Šคํ‚ค๋งˆ์—์„œ ์ง€์ •ํ•œ Table์— Data Frame์„ InsertInto๋กœ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Streaming์ด ๋‘ ๊ฐœ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๊ณ , ์™ผ์ชฝ์€ ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ Table, ์˜ค๋ฅธ์ชฝ์€ ์›๋ณธ ๋ฐ์ดํ„ฐ์™€ ์›๋ณธ๊ณผ์˜ Join ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ Table์„ ๋‹ค๋ฃจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. HDFS: ์ˆ˜์ง‘๋œ JSON ๋ฐ์ดํ„ฐ๋ฅผ ์••์ถ•ํ•œ ํ›„ Parquet<NAME>์œผ๋กœ ์ €์žฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์™ผ์ชฝ์€ SSD ๋””์Šคํฌ๋ฅผ, ์˜ค๋ฅธ์ชฝ์€ SATA ๋””์Šคํฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Federation ์„ค์ •์œผ๋กœ ๊ฐ๊ฐ์˜ HDFS๋Š” Namespace ๋งŒ์œผ๋กœ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Spark: Zeppelin, Streaming, BigDB ๊ฐ๊ฐ ๋ณ„๋„์˜ ์„ธ์…˜์œผ๋กœ ๋™์ž‘์„ ํ•˜๋ฉฐ, Hive Meta-Store๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Table์€ ์„ธ์…˜ ๊ฐ„์— ๊ณต์œ ๋ฉ๋‹ˆ๋‹ค. Locality๋ฅผ ์œ„ํ•ด์„œ Table(์‹ค์ œ ๋ฐ์ดํ„ฐ ํŒŒ์ผ)์ด ์กด์žฌํ•˜๋Š” ํŒŒํŠธ์˜ ์ž์›์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. End Point: Web/Zeppelin/BigDB API ๋“ฑ์—์„œ Spark์˜ ์ž์›๊ณผ Table์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ์—์„œ๋Š” ์ฃผ๋กœ ์ง‘๊ณ„์™€ ์กฐํšŒ๋ฅผ ํ•˜๊ณ , ์™ผ์ชฝ์—์„œ๋Š” ์กฐํšŒ์™€ ์Šค์ผ€์ค„๋ง๋œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. BigDB Core/API: akka.http๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํ‚ค๋งˆ์˜ ์ƒ์„ฑ๊ณผ ๊ด€๋ฆฌ, ์ฟผ๋ฆฌ ๊ธฐ๋ฐ˜์˜ ์ง‘๊ณ„๋‚˜ ์Šค์ผ€์ค„๋ง๋œ ์ž‘์—…์˜ ์ˆ˜ํ–‰์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. terminus.js๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ REST CLI๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. End Point์— ๊ฒฐ๊ณผ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ JSON ๋ฐ์ดํ„ฐ๋ฅผ CSV/TSV/JSON ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์‹œ์Šคํ…œ ๊ตฌ์กฐ ๋””์Šคํฌ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋‘ ๊ฐœ์˜ ํด๋Ÿฌ์Šคํ„ฐ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์‚ฌ์šฉ SSD์˜ ๊ฒฝ์šฐ๋Š” ์ฝ๊ธฐ/์“ฐ๊ธฐ๊ฐ€ ๋น ๋ฅด๊ณ  ์šฉ๋Ÿ‰์ด ์ž‘์€ ๊ฒฝ์šฐ์— ์‚ฌ์šฉ SATA์˜ ๊ฒฝ์šฐ๋Š” ์ฝ๊ธฐ/์“ฐ๊ธฐ๊ฐ€ SSD์— ๋น„ํ•ด์„œ ๋Š๋ฆฌ์ง€๋งŒ ์šฉ๋Ÿ‰์ด ํฐ ๋…ธ๋“œ์ผ ๋•Œ ์‚ฌ์šฉ SATA๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์˜ ๊ฒฝ์šฐ๋Š” ๋ฌผ๋ฆฌ์ ์œผ๋กœ 12๊ฐœ์˜ ๋””์Šคํฌ๋ฅผ ๋ถ„์‚ฐํ•ด์„œ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ํšจ์œจ์„ฑ์„ ํ™•๋ณด CPU์˜ ๊ฒฝ์šฐ๋Š” ๋™์ผํ•˜๊ฒŒ ๊ตฌ์„ฑ Memory์˜ ๊ฒฝ์šฐ๋Š” ๋””์Šคํฌ์˜ ๊ณต๊ฐ„์„ ๊ณ ๋ คํ•œ Rack ๊ตฌ์„ฑ์œผ๋กœ ์ฐจ์ด ๋‚˜๊ฒŒ ๊ตฌ์„ฑ ๋‘ ๊ฐœ์˜ ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ ๋ชจ๋‘ ์ˆ˜ํ‰ ํ™•์žฅ์„ ๊ณ ๋ ค SSD SSD๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋Ÿฌ์Šคํ„ฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ง‘๊ณ„ ํ›„์˜ ์ž‘์€ ์‚ฌ์ด์ฆˆ๋ฅผ ์žฅ๊ธฐ๊ฐ„ ๋ณด๊ด€ํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ SATA SATA๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋Ÿฌ์Šคํ„ฐ๋Š” ์›๋ณธ ๋ฐ์ดํ„ฐ์™€ ์›๋ณธ ๋ฐ์ดํ„ฐ ์ˆ˜์ค€์œผ๋กœ ๊ฐ€๊ณต๋˜๋Š” ํฐ ์‚ฌ์ด์ฆˆ๋ฅผ ์žฅ๊ธฐ๊ฐ„ ๋ณด๊ด€ํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ ๋ฉ”๋ชจ๋ฆฌ Memory์˜ ๊ฒฝ์šฐ๋Š” Parquet๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด์„œ ์‚ฌ์šฉ์„ฑ์ด ์ ์–ด์ง€๊ณ  ์žˆ๊ณ , ๋‚จ๋Š” Memory๋ฅผ Elasticsearch์— ์ผ๋ถ€ ํ• ๋‹นํ•จ์œผ๋กœ์จ ๊ฒ€์ƒ‰์ด ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ํ™œ์šฉ ๋„คํŠธ์›Œํฌ ์กฐํšŒ ์‹œ ๋Œ€์ƒ์ด ๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ํฌ๊ณ , ๊ทธ์— ๋”ฐ๋ผ์„œ ์…”ํ”Œ์˜ ์„ฑ๋Šฅ์„ ํ™•๋ณดํ•˜๊ธฐ ์œ„ํ•ด ๋„คํŠธ์›Œํฌ๋Š” 10GB๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ๋˜๋Š” ์„ค๋ฃจ์…˜ ๋ชจ๋‘ ๊ฐ€๋Šฅํ•œ ๋กœ์ปฌ ํ†ต์‹ ์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ • Proxy: ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ตœ์†Œํ™”ํ•ด์„œ, ์ž์›์˜ ์‚ฌ์šฉ๋„ ์ตœ์†Œํ™”ํ•˜๊ณ  ์Šค์ผ€์ผ์˜ ํ™•์žฅ์„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. Kafka: Locality ํ™•๋ณด๋ฅผ ์œ„ํ•ด์„œ Spark, HDFS์™€ ๊ฐ™์€ ๋…ธ๋“œ์— ์ตœ์†Œํ™”ํ•ด์„œ ๊ตฌ์„ฑ์„ ํ•˜๊ณ , ๋ฐ์ดํ„ฐ์˜ ๋ฒ„ํผ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Spark & Spark Streaming: Kafka๋กœ๋ถ€ํ„ฐ ์œ ์‹ค ์—†๋Š” ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ , PreferBroker ์„ค์ •์„ ํ†ตํ•ด์„œ Locality๋ฅผ ํ™•๋ณดํ•ฉ๋‹ˆ๋‹ค. Table ๋ฐ์ดํ„ฐ ๊ณต์œ ๋ฅผ ์œ„ํ•ด์„œ Streaming์—์„œ๋Š” Data Frame์„ InsertInto๋กœ ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Data Frame์„ in-memory๋กœ Cache ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š”, Parquet<NAME>์„ ์‚ฌ์šฉํ•˜์—ฌ ์นผ๋Ÿผ ๋ณ„๋กœ ์ฟผ๋ฆฌ์˜ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. Memory๋Š” Spark์˜ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์šฉ๋„์™€ ๊ฒ€์ƒ‰ ์šฉ๋„๋กœ, ์งง์€ ๊ธฐ๊ฐ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” Elasticsearch์— ํ• ๋‹นํ•˜๋„๋ก ๋…ธ๋“œ์— ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€๋Ÿ‰์„ ์ฆ์„คํ•ด ๋‘ก๋‹ˆ๋‹ค. HDFS: SCR(Short-Circuit Local Reads) ์„ค์ •์„ ํ™œ์„ฑํ™”์‹œ์ผœ์„œ ๋กœ์ปฌ ํ†ต์‹ ์˜ ํšจ์œจ์„ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. Federation ์„ค์ •์„ ํ†ตํ•ด์„œ๋Š” ํด๋Ÿฌ์Šคํ„ฐ ๊ฐ„์˜ ๋ฐ์ดํ„ฐ Path ๊ณต์œ ๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ NameNode๋Š” HA ๊ตฌ์„ฑ์„ ํ•จ์œผ๋กœ์จ ์žฅ์• ์— ๋Œ€์‘ํ•ฉ๋‹ˆ๋‹ค. HDFS์˜ ๊ฒฝ์šฐ SSD๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŒŒํŠธ์™€ SATA๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŒŒํŠธ๋กœ ๋‘ ๊ฐœ์˜ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์šด์˜ํ•ฉ๋‹ˆ๋‹ค. SSD์˜ ๊ฒฝ์šฐ ์“ฐ๊ธฐ์™€ ์ฝ๊ธฐ์˜ ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•˜์ง€๋งŒ, ์ „์ฒด ์šฉ๋Ÿ‰์€ ํ•œ๊ณ„๊ฐ€ ์žˆ์–ด์„œ ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์Œ“์•„์„œ ํ™œ์šฉํ•˜๊ณ , SATA์˜ ๊ฒฝ์šฐ ๋…ธ๋“œ ๋ณ„๋กœ ๋ฌผ๋ฆฌ์ ์œผ๋กœ 12๊ฐœ์˜ ๋””์Šคํฌ๋ฅผ no-mirror๋กœ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์“ฐ๊ธฐ์™€ ์ฝ๊ธฐ ๋ฉด์—์„œ์˜ ํšจ์œจ์„ ๊ณ ๋ คํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ „์ฒด ์šฉ๋Ÿ‰์ด ํฌ๊ธฐ ๋•Œ๋ฌธ์— ํ–ฅํ›„ 5๋…„๊ฐ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋Šฅ ์†Œ๊ฐœ REST CLI (Command Line Interface) terminus.js๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, REST ๋ฐฉ์‹์œผ๋กœ Command๋ฅผ ์šด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ • Create Table[Schema] REST CLI๋ฅผ ํ†ตํ•ด ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด, ์Šคํ‚ค๋งˆ์—์„œ ์ง€์ •๋œ Source ์œ„์น˜๋กœ๋ถ€ํ„ฐ Data๋ฅผ ์ฝ์–ด์„œ ์ƒ์„ฑํ•œ Table์— ์Šคํ‚ค๋งˆ์˜<NAME>์— ๋งž๊ฒŒ ์ €์žฅ ์Šคํ‚ค๋งˆ์˜ ์ •๋ณด๋Š” Zookeeper ๋‚ด์— ๋ณด๊ด€ Message Proxy์™€ Spark์—์„œ ์Šคํ‚ค๋งˆ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ Validation ๋ฐ ํ˜• ๋ณ€ํ™˜ ์Šคํ‚ค๋งˆ์— ์ง€์ •๋œ Table์˜ ํŒŒํ‹ฐ์…˜ ์ •๋ณด๋ฅผ ํ™œ์šฉ ์ŠคํŠธ๋ฆฌ๋ฐ์˜ ์งง์€ ๊ฐ„๊ฒฉ์œผ๋กœ ์ž…๋ ฅ๋œ ๋งŽ์€ ํŒŒํ‹ฐ์…˜ ์ •๋ณด๋Š” ๋งค์ผ ์ƒˆ๋ฒฝ ์‹œ๊ฐ„์— ํŒŒํ‹ฐ์…˜์„ merge ์‹ค์ œ ํ…Œ์ŠคํŠธ ์‹œ 80๋งŒ ๊ฐœ ์ •๋„์˜ ํŒŒํ‹ฐ์…˜์ด ์ƒ์„ฑ๋˜๋ฉด ์„ฑ๋Šฅ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒ ์ŠคํŠธ๋ฆฌ๋ฐ์ด 5์ดˆ ๊ฐ„๊ฒฉ์œผ๋กœ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งค์ผ merge๋ฅผ ์ˆ˜ํ–‰ํ•จ์œผ๋กœ์จ ์ด๋Ÿฌํ•œ ์ด์Šˆ๋ฅผ ํ”ผํ•  ์ˆ˜ ์žˆ์—ˆ์Œ Support [Multi] Session Table์— ์ž…๋ ฅ๋œ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ Spark ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Œ ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ Zeppelin, JDBC, Web ๋“ฑ์—์„œ ์„œ๋กœ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Œ InsertInto ๋™์ž‘์—์„œ๋Š” ์ŠคํŠธ๋ฆฌ๋ฐ ์‹œ ํŒŒํ‹ฐ์…˜์„ ์œ ์ผํ•˜๊ฒŒ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ๋‹ค๋ฅธ ์„ธ์…˜์˜ Spark์—์„œ ์กฐํšŒํ•  ๋•Œ refresh table์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ ๋„ ์ž…๋ ฅ๋œ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Œ [Realtime] Data Join Data Join์€ ๋ถ„์„์˜ ํ•œ ํ˜•ํƒœ๋กœ Online ๋ถ„์„์˜ ๊ฒฝ์šฐ, ์‹ค์‹œ๊ฐ„์œผ๋กœ ์—ฌ๋Ÿฌ Table์„ Join ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•จ์œผ๋กœ์จ ํ•™์Šต ์‹œ Join ํ•˜๋Š” ํฐ ๋น„์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ์—ˆ์Œ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ์™€ ์ •์ ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ ๋ฐ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ๋ฅผ ๋Œ€๋Ÿ‰์œผ๋กœ Join ํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ์˜ ์œ„์น˜๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ์ด์œ ๋กœ ๋งŽ์€ ๋น„ํšจ์œจ์ ์ธ ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒ ์ด ๊ฒฝ์šฐ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ž…๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์‹œ์ ์— Join ํ•˜๊ฒŒ ํ•จ์œผ๋กœ์จ, Join ๋น„์šฉ์„ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ์—ˆ์Œ Support Input/Output Spec ([ ] : Beta Phase) ์ง€์›ํ•˜๋Š” Output์˜ ํ˜•ํƒœ๋Š” Table, Kafka, File, Elasticsearch์™€ Web์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ TSV, CSV, JSON ํ˜•ํƒœ ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” Kafka๊ฐ€ Input์ด ๋˜๋ฉฐ, ํ˜•ํƒœ์— ๋”ฐ๋ผ Kafka ์—†์ด HDFS์— ๋ฐ”๋กœ ์ ์žฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉ BigDB API๋ฅผ ์ด์šฉํ•ด์„œ ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ Input/Output์€ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Œ Join์˜ ๊ฒฝ์šฐ๋Š” ์Šคํ‚ค๋งˆ ์ƒ์„ฑ ์ดํ›„ BigDB API๋ฅผ ์ด์šฉํ•ด์„œ ์Šค์ผ€์ค„๋Ÿฌ์— ์ฟผ๋ฆฌ์™€ ์ˆ˜ํ–‰ ๊ฐ„๊ฒฉ์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Œ ์ง‘๊ณ„์˜ ๊ฒฝ์šฐ๋„ ์Šค์ผ€์ค„๋Ÿฌ์— ์ฟผ๋ฆฌ์™€ ์ˆ˜ํ–‰ ๊ฐ„๊ฒฉ์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Œ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ์–‘์— ๋”ฐ๋ผ ๋‘ ๊ฐœ์˜ HDFS ์ค‘์—์„œ ์ ์ ˆํ•œ ๊ณณ์„ ์„ ํƒ Use Case ๊ธฐ๊ฐ„๋ณ„ ๋ฐ์ดํ„ฐ ์กฐํšŒ ๋ฐ ๋ถ„์„ ๊ด‘๊ณ ์‚ฌ์—… ๋ถ€์„œ๋‚˜ ๊ธฐํš ๋ถ€์„œ ๋“ฑ์˜ ํšŒ์‚ฌ ๋‚ด ์‚ฌ์šฉ์ž์˜ ๊ฒฝ์šฐ, ์ฃผ๋กœ ๊ธฐ๊ฐ„๋ณ„ ๋ฐ์ดํ„ฐ ์กฐํšŒ๋ฅผ ํ†ตํ•œ ๋ถ„์„๊ณผ ๋ณด๊ณ  ์—…๋ฌด๋ฅผ ์ˆ˜ํ–‰ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ๋Š” ๋ณ„๋„๋กœ Dashboard๋ฅผ Web์œผ๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ์‚ฌ์šฉ Input: Kafka (Realtime Advertisement Impression & Click Log Data) Join Target: None Support Output: JSON (For Dashboard web page) ๋ชฉ์ : 5์ดˆ ์ฃผ๊ธฐ์˜ ์‹ค์‹œ๊ฐ„ ๊ด‘๊ณ  ๋กœ๊ทธ ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ์„ฑ๋œ ์Šคํ‚ค๋งˆ์— ๋”ฐ๋ผ ์ง€์ •๋œ Table์— InsertInto๋กœ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ˆ„์ ๋œ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ๋“ฑ๋กํ•œ ์ง‘๊ณ„ ์ฟผ๋ฆฌ์™€ ์ˆ˜ํ–‰ ๊ฐ„๊ฒฉ์— ๋”ฐ๋ผ ์ƒˆ๋กœ์šด ์ง‘๊ณ„ Table์„ ์ƒ์„ฑํ•˜๊ณ  ์ง‘๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์„ฑ๋Šฅ: 5์ดˆ๋งˆ๋‹ค 10,000๊ฑด~100,000๊ฑด์˜ ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ๋ฅผ 1์ดˆ ์ดํ•˜๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ , 3๊ฐœ์›” ๋™์•ˆ์˜ ๋ˆ„์  ๋ฐ์ดํ„ฐ ์กฐํšŒ๋Š” ์ˆ˜ ์ดˆ ์ด๋‚ด๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. Dashboard web ํŽ˜์ด์ง€์—์„œ๋Š” BigDB API๋ฅผ ํ†ตํ•ด์„œ ์ฃผ๊ธฐ์ ์œผ๋กœ ์ง‘๊ณ„๋œ ๋ฐ์ดํ„ฐ๋ฅผ JSON ํ˜•ํƒœ๋กœ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ง‘๊ณ„๋œ ๋ฐ์ดํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ด์ฆˆ๋„ ๋งŽ์ด ์ค„์–ด๋“ค๊ณ  ๊ธฐ๊ฐ„์— ๋”ฐ๋ผ ์ˆ˜ ์ดˆ์—์„œ ์ˆ˜์‹ญ ์ดˆ ์‚ฌ์ด๋กœ web์— ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. Dashboard๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ad-hoc ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์™€ machine learning์„ ํ†ตํ•œ ๊ฒฐ๊ณผ ์กฐํšŒ ๋“ฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ad-hoc ์ฟผ๋ฆฌ ๊ด‘๊ณ ์‚ฌ์—… ๋ถ€์„œ๋‚˜ ๊ธฐํš, ๊ฐœ๋ฐœ ๋ถ€์„œ ๋“ฑ์˜ ํšŒ์‚ฌ ๋‚ด ์‚ฌ์šฉ์ž์˜ ๊ฒฝ์šฐ, ์—…๋ฌด์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋ถ„์„ ์ž‘์—…์„ ์œ„ํ•ด BigDB๋กœ ๋งŒ๋“ค์–ด์ง„ Table์— ad-hoc ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ์กฐํšŒ ๊ฐœ๋ฐœ ๋ถ€์„œ ์‚ฌ์šฉ์ž์˜ ๊ฒฝ์šฐ๋Š” ์ข€ ๋” ๋ณต์žกํ•œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ad-hoc ์ฟผ๋ฆฌ ๋ฐ UDF๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ๊ฐœ๋ฐœํ•˜์—ฌ ์‚ฌ์šฉ ์ด๋ฅผ ์œ„ํ•ด์„œ ์ฃผ๋กœ Zeppelin์„ ์‚ฌ์šฉ Input: Kafka (Realtime Advertisement Impression & Click Log Data) Join Target: None Support Output: Table (For Zeppelin) ๋ชฉ์ : ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ž…๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ง€์ •๋œ Table์— ๋ˆ„์ ํ•จ์œผ๋กœ์จ Zeppelin์„ ํ™œ์šฉํ•˜์—ฌ ad-hoc ์ฟผ๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ •ํ•ด์ง„ ์ฟผ๋ฆฌ๋ฅผ ๋“ฑ๋กํ•˜๊ณ  ์ง‘๊ณ„ Table์— ๊ฒฐ๊ณผ๋ฅผ ๋ˆ„์ ํ•จ์œผ๋กœ์จ ์ง‘๊ณ„์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ์˜ ad-hoc ์ฟผ๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ฑ๋Šฅ: 5์ดˆ๋งˆ๋‹ค 10,000๊ฑด~100,000๊ฑด์˜ ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ๋ฅผ 1์ดˆ ์ดํ•˜๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ , ์ง‘๊ณ„ ์ฟผ๋ฆฌ์˜ ์œ ํ˜•์— ๋”ฐ๋ผ ์ˆ˜ ์ดˆ ์ด๋‚ด๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜ํ–‰ ์‹œ์—๋Š” ์ž์›์„ ์ ์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‹ค์‹œ๊ฐ„ Join์„ ํ†ตํ•ด์„œ ๊ฐ€๊ณต ํ›„ ์žฌ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ถ„์„ ๋ฐ ์˜ˆ์ธก์„ ์œ„ํ•œ Online ๋ฐ์ดํ„ฐ Joiner ์—ญํ•  (beta phase) ์‚ฌ์šฉ์ž์—๊ฒŒ ์ข€ ๋” ์œ ์ตํ•œ ๊ด‘๊ณ ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ, ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ๋ถ„์„ํ•˜์—ฌ ์˜จ๋ผ์ธ์œผ๋กœ ๋ถ„์„๋œ ๊ด‘๊ณ ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ๋งŽ์€ ์‹œ๋„๋ฅผ ์ˆ˜ํ–‰ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋ฐœ์ƒํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€์˜ ๋ฐ์ดํ„ฐ ์ •๋ณด๋ฅผ BigDB๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Table๋กœ ๋งŒ๋“ค๊ณ  ์žˆ์œผ๋ฉฐ, ๋ถ„์„ ์ž‘์—… ์‹œ ์‹œ๊ฐ„์ด ๋งŽ์ด ์†Œ์š”๋˜๋Š” Join ๊ณผ์ •์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ฒ˜๋ฆฌํ•จ์œผ๋กœ์จ ๋ถ„์„์— ๋Œ€ํ•œ ํšจ์œจ์„ ๋†’์ด๊ณ  ์žˆ์Œ Input: Kafka (Realtime Advertisement Impression & Click Log Data) Join Target: HDFS (Daily User Demo Data) Support Output: Kafka & Table ๋ชฉ์ : 5์ดˆ ์ฃผ๊ธฐ์˜ ์‹ค์‹œ๊ฐ„ ๊ด‘๊ณ  ๋กœ๊ทธ ๋ฉ”์‹œ์ง€์™€ ์ผ๋ณ„ ์ง‘๊ณ„๋œ ์‚ฌ์šฉ์ž์˜ ๋ฐ๋ชจ ์ •๋ณด๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ Join ํ•จ์œผ๋กœ์จ ๋ถ„์„ ์‹œ Join ๋น„์šฉ์„ ์ค„์ด๊ณ , ์˜จ๋ผ์ธ์œผ๋กœ ๊ด‘๊ณ ์— ๋Œ€ํ•œ CTR(click-through rate)์„ ๋†’์ด๋Š” ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์„ฑ๋Šฅ: 5์ดˆ๋งˆ๋‹ค 10,000๊ฑด~100,000๊ฑด์˜ ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ๋ฅผ 1์ดˆ ์ดํ•˜์˜ ์‹œ๊ฐ„ ๋™์•ˆ ์ €์žฅํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ  ํ•ด๋‹น ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ์™€ 7์ฒœ๋งŒ ๊ฑด ๋ฐ์ดํ„ฐ์˜ Join ์‹œ 2์ดˆ~3์ดˆ๊ฐ€ ์†Œ์š”๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ณ„ ๋˜๋Š” ์‹œ๊ฐ„๋ณ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ Join ํ•˜๋Š” ๊ฒฝ์šฐ ๋Œ€๋Ÿ‰ ๋ฐ์ดํ„ฐ์˜ ์…”ํ”Œ ๋ฐœ์ƒ์œผ๋กœ ์ธํ•ด Join์— ๋Œ€ํ•œ ๋น„์šฉ์ด ์ฆ๊ฐ€ ๋˜ํ•œ batch ๋ถ„์„๋ณด๋‹ค๋Š” ๊ด‘๊ณ ์˜ ์‹ค์‹œ๊ฐ„ ๋ถ„์„์— ๋ฐ”๋กœ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์„œ๋น„์Šค์˜ ์งˆ์  ๋ถ„์„์— ๋Œ€ํ•œ ์„ฑ๊ณผ๋ฅผ ํ™•๋ณดํ•˜๊ณ ์ž ๋…ธ๋ ฅํ•˜์˜€์Œ ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ๋Š” ์ข€ ๋” ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๊ด‘๊ณ ๋ฅผ ํ†ตํ•ด ์ œ๊ณต๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ๋„ ์žˆ์Œ ์ฐธ๊ณ  LINE ๊ด‘๊ณ  ๋ฐ์ดํ„ฐ ํŒŒ์ดํ”„๋ผ์ธ BigDB 6-Hive์—์„œ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์‡ผํ•‘ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๊ฒŒ ETL ๊ฐœ์„ ํ•˜๊ธฐ LINE ์‡ผํ•‘์˜ ์‡ผํ•‘ ํ”Œ๋žซํผ ์ •๋ณด์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ตฌ์กฐ Hive Kafka ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์ „์†ก ์ง€์› HBase ๋ฐ์ดํ„ฐ ์ €์žฅ ๋งˆ์น˜๋ฉฐ ๊ฐœ์„  ์ž‘์—…์œผ๋กœ ์–ป์€ ์ด์ ์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ์˜ ์‚ฌ์šฉ์„ฑ์ด ์ฆ๋Œ€๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋™์•ˆ์—๋Š” ์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ฒ˜์—์„œ ETL์ด ์™„๋ฃŒ๋  ์‹œ์ ์„ ๊ณต์œ  ๋ฐ›์•„ ํ›„์† ๋ฐฐ์น˜ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ณค ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋•Œ๋ฌธ์— ๊ฐ„ํ˜น Hadoop ์ง€์—ฐ์ด ๋ฐœ์ƒํ•˜๋ฉด ํ•˜๋ฃจ ์ „์— ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฒ„๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ ์ฃผ๊ธฐ, ์‹œ๊ฐ„ ์ฃผ๊ธฐ ๋˜๋Š” ์ค€ ์‹ค์‹œ๊ฐ„ ์ฃผ๊ธฐ๋กœ ์—…๋ฐ์ดํŠธ๋œ ํ…Œ์ด๋ธ”์„ ๋ทฐ๋กœ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋ฉด์„œ ๋ฌธ์ œ๊ฐ€ ์‚ฌ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. Hadoop ์ง€์—ฐ์ด ๋ฐœ์ƒํ•˜๋”๋ผ๋„ ๋น„๊ต์  ์ตœ๊ทผ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋๊ณ , ์‹œ๊ฐ„ ์ œ์•ฝ์ด ์‚ฌ๋ผ์ง€๋ฉด์„œ ์‚ฌ์šฉ์ฒ˜๊ฐ€ ์›ํ•˜๋Š” ์‹œ๊ฐ„์— ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋งˆ์ดํฌ๋กœ ๋ฐฐ์น˜๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด์ „์—๋Š” ํ†ต๊ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋ฃจ ๋‹จ์œ„๋กœ ๋ฐ–์— ๋งŒ๋“ค ์ˆ˜ ์—†์—ˆ์ง€๋งŒ ์ด์ œ๋Š” ์‹œ๊ฐ„ ๋‹จ์œ„๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ์ด๋ฒคํŠธ ๋“œ๋ฆฌ๋ธ ์•„ํ‚คํ…์ฒ˜์˜ ์šด์˜ ๋ฐ ์œ ์ง€ ๋ณด์ˆ˜ ๋น„์šฉ์ด ๊ฐ์†Œํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํŒ๋งค์ž๊ฐ€ ์ƒํ’ˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜๋ชป ์˜ฌ๋ฆฌ๊ฑฐ๋‚˜ ์œ ์ž…๋œ ์ƒํ’ˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜๋ชป ์ •์ œํ•ด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ๋””๋ฒ„๊น…ํ•˜๋ฉฐ ์›์ธ์„ ์ฐพ์•„์•ผ ํ•˜๋Š”๋ฐ LINE ์‡ผํ•‘์˜ ๊ฒฝ์šฐ ์ƒํ’ˆ ์ •๋ณด ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ๋‹ค ๋ณด๋‹ˆ ์‰ฝ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. Kafka์— ์กด์žฌํ•˜๋Š” ๋งŽ์€ ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ ์ž์ฒด๋„ ์–ด๋ ค์šด๋ฐ ๋ณด์œ  ๊ธฐํ•œ๋„ ์งง์•„ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ์ง€ ๋ฉฐ์น ์ด ์ง€๋‚˜๋ฉด ์›์ธ์„ ์ฐพ์„ ์ˆ˜ ์—†์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด KSQL๊ณผ Elasticsearch๋ฅผ ๋„์ž…ํ•ด ํ™œ์šฉํ•˜๊ณ  ์žˆ์—ˆ์ง€๋งŒ ๋น„์šฉ ๋ฌธ์ œ๋กœ ํ™œ์šฉ๋„๋ฅผ ๋” ๋†’์ด๊ธฐ๋Š” ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด์ œ Hive์— ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋ฉด์„œ ๊ณ ๋ฏผ๊ฑฐ๋ฆฌ๊ฐ€ ํ•ด๊ฒฐ๋์Šต๋‹ˆ๋‹ค. SQL ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” HiveQL๋กœ ์†์‰ฝ๊ฒŒ ์ƒํ’ˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๊ณ , ์ƒ๋Œ€์ ์œผ๋กœ ๋น„์šฉ์ด ์ €๋ ดํ•œ HDFS๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋ณด์œ  ๊ธฐํ•œ๋„ ๋Š˜๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  Hive์—์„œ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์‡ผํ•‘ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๊ฒŒ ETL ๊ฐœ์„ ํ•˜๊ธฐ Kafka์™€ MongoDB, Kubernetes๋กœ ์œ ์—ฐํ•˜๊ณ  ํ™•์žฅ ๊ฐ€๋Šฅํ•œ LINE ์‡ผํ•‘ ํ”Œ๋žซํผ ๊ตฌ์ถ•ํ•˜๊ธฐ 2-ํ•˜๋‘ก(hadoop) ํ•˜๋‘ก(hadoop)์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค. ํ•˜๋‘ก์ด๋ž€? HDFS MapReduce YARN ์ž‘์—… ์ง€์› ๋„๊ตฌ 1-ํ•˜๋‘ก์ด๋ž€? ํ•˜๋‘ก์€ 2006๋…„ ์•ผํ›„์˜ ๋”๊ทธ ์ปคํŒ…์ด '๋„›์น˜'๋ผ๋Š” ๊ฒ€์ƒ‰์—”์ง„์„ ๊ฐœ๋ฐœํ•˜๋Š” ๊ณผ์ •์—์„œ ๋Œ€์šฉ๋Ÿ‰์˜ ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์กด์˜ RDB ๊ธฐ์ˆ ๋กœ๋Š” ์ฒ˜๋ฆฌ๊ฐ€ ํž˜๋“ค๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ซ๊ณ , ์ƒˆ๋กœ์šด ๊ธฐ์ˆ ์„ ์ฐพ๋Š” ์ค‘ ๊ตฌ๊ธ€์—์„œ ๋ฐœํ‘œํ•œ GFS์™€ MapReduce ๊ด€๋ จ ๋…ผ๋ฌธ์„ ์ฐธ๊ณ ํ•˜์—ฌ ๊ฐœ๋ฐœํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ดํ›„ ์•„ํŒŒ์น˜ ์žฌ๋‹จ์˜ ์˜คํ”ˆ ์†Œ์Šค๋กœ ๊ณต๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก์€ ํ•˜๋‚˜์˜ ์„ฑ๋Šฅ ์ข‹์€ ์ปดํ“จํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋Œ€์‹ , ์ ๋‹นํ•œ ์„ฑ๋Šฅ์˜ ๋ฒ”์šฉ ์ปดํ“จํ„ฐ ์—ฌ๋Ÿฌ ๋Œ€๋ฅผ ํด๋Ÿฌ์Šคํ„ฐ ํ™”ํ•˜๊ณ , ํฐ ํฌ๊ธฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ๋ณ‘๋ ฌ๋กœ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜์—ฌ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ๋†’์ด๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•˜๋Š” ๋ถ„์‚ฐ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์˜คํ”ˆ์†Œ์Šค ํ”„๋ ˆ์ž„์›Œํฌ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2020. 06์›” ๊ธฐ์ค€ ์ตœ์‹  ๋ฒ„์ „์€ v3.2.1, v2.10์ž…๋‹ˆ๋‹ค. ํ•˜๋‘ก์˜ ๊ตฌ์„ฑ ์š”์†Œ ํ•˜๋‘ก์€ 4๊ฐœ์˜ ์ฃผ์š” ๋ชจ๋“ˆ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. Hadoop Common ํ•˜๋‘ก์˜ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•œ ๊ณตํ†ต ์ปดํฌ๋„ŒํŠธ ๋ชจ๋“ˆ Hadoop HDFS ๋ถ„์‚ฐ ์ €์žฅ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ชจ๋“ˆ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์„œ๋ฒ„๋ฅผ ํ•˜๋‚˜์˜ ์„œ๋ฒ„์ฒ˜๋Ÿผ ๋ฌถ์–ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ Hadoop YARN ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํด๋Ÿฌ์Šคํ„ฐ ์ž์›๊ด€๋ฆฌ ๋ฐ ์Šค์ผ€์ค„๋ง ๋‹ด๋‹น Hadoop Mapreduce ๋ถ„์‚ฐ๋˜์–ด ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋ถ„์‚ฐ ์ฒ˜๋ฆฌ ๋ชจ๋“ˆ Hadoop Ozone ํ•˜๋‘ก์„ ์œ„ํ•œ ์˜ค๋ธŒ์ ํŠธ ์ €์žฅ์†Œ ํ•˜๋‘ก์˜ ์žฅ๋‹จ์  ์žฅ์  ์˜คํ”ˆ์†Œ์Šค๋กœ ๋ผ์ด์„ ์Šค์— ๋Œ€ํ•œ ๋น„์šฉ ๋ถ€๋‹ด์ด ์ ์Œ ์‹œ์Šคํ…œ์„ ์ค‘๋‹จํ•˜์ง€ ์•Š๊ณ , ์žฅ๋น„์˜ ์ถ”๊ฐ€๊ฐ€ ์šฉ์ด(Scale Out) ์ผ๋ถ€ ์žฅ๋น„์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๋„ ์ „์ฒด ์‹œ์Šคํ…œ ์‚ฌ์šฉ์„ฑ์— ์˜ํ–ฅ์ด ์ ์Œ(Fault tolerance) ์ €๋ ดํ•œ ๊ตฌ์ถ• ๋น„์šฉ๊ณผ ๋น„์šฉ ๋Œ€๋น„ ๋น ๋ฅธ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์˜คํ”„๋ผ์ธ ๋ฐฐ์น˜ ํ”„๋กœ์„ธ์‹ฑ์— ์ตœ์ ํ™” ๋‹จ์  HDFS์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ๋ถ„์„๊ฐ™์ด ์‹ ์†ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์ž‘์—…์—๋Š” ๋ถ€์ ํ•ฉ ๋„ˆ๋ฌด ๋งŽ์€ ๋ฒ„์ „๊ณผ ๋ถ€์‹คํ•œ ์„œํฌํŠธ ์„ค์ •์˜ ์–ด๋ ค์›€ 01-ํ•˜๋‘ก ๋ฒ„์ „๋ณ„ ํŠน์ง• ํ•˜๋‘ก์˜ ๋ฒ„์ „ 1 ๋ณ„ ํŠน์ง•์„ ์•Œ์•„๋ณด๊ธฐ ์ „์— ๊ฐ ๋ฒ„์ „์˜ ํŠน์ง•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก v1์—์„œ ํ•˜๋‘ก์˜ ๊ธฐ๋ณธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ •๋ฆฝํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ(๋งต๋ฆฌ๋“€์Šค)๋Š” ์žกํŠธ๋ž˜์ปค์™€ ํƒœ์Šคํฌ ํŠธ๋ž˜์ปค๊ฐ€ ๋‹ด๋‹นํ•˜๊ณ , ๋ถ„์‚ฐ ์ €์žฅ(HDFS)์€ ๋„ค์ž„๋…ธ๋“œ์™€ ๋ฐ์ดํ„ฐ๋…ธ๋“œ๊ฐ€ ๋‹ด๋‹นํ•˜๋„๋ก ๊ตฌ์กฐ๋ฅผ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ์˜ ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ž์›๊ด€๋ฆฌ์™€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ผ์ดํ”„์‚ฌ์ดํด ๊ด€๋ฆฌ๋ฅผ ์žกํŠธ๋ž˜์ปค๊ฐ€ ๋ชจ๋‘ ๋‹ด๋‹นํ•˜์—ฌ ๋ณ‘๋ชฉํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก v2์—์„œ๋Š” ์žกํŠธ๋ž˜์ปค์˜ ๋ณ‘๋ชฉํ˜„์ƒ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ YARN ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋„์ž…ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์žกํŠธ๋ž˜์ปค์˜ ๊ธฐ๋Šฅ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ž์›๊ด€๋ฆฌ๋Š” ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์™€ ๋…ธ๋“œ ๋งค๋‹ˆ์ €, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ผ์ดํ”„ ์‚ฌ์ดํด ๊ด€๋ฆฌ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์™€ ์ปจํ…Œ์ด๋„ˆ์—๊ฒŒ ๋‹ด๋‹นํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก v3์—์„œ๋Š” ์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ์„ ๋„์ž…ํ•˜์—ฌ HDFS์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ ํšจ์œจ์„ฑ์„ ์ฆ๊ฐ€์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, YARN ํƒ€์ž„๋ผ์ธ ์„œ๋น„์Šค๋ฅผ ๊ฐœ์„ ํ•˜๊ณ , ํ•˜๋‘ก v1๋ถ€ํ„ฐ ์‚ฌ์šฉ๋œ ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์žฌ์ž‘์„ฑํ•˜์—ฌ ์•ˆ์ •์„ฑ์„ ๋†’์˜€์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ์ฒ˜๋ฆฌ์— ๋„ค์ดํ‹ฐ๋ธŒ ํ”„๋กœ๊ทธ๋žจ์„ ๋„์ž…ํ•˜์—ฌ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก v1 2011๋…„์— ์ •์‹ ๋ฐœํ‘œ๋œ ํ•˜๋‘ก v1์€ ๋ถ„์‚ฐ ์ €์žฅ, ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ถ„์‚ฐ ์ €์žฅ์€ ๋„ค์ž„๋…ธ๋“œ์™€ ๋ฐ์ดํ„ฐ๋…ธ๋“œ๊ฐ€ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ๋Š” ๋ธ”๋ก ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ , ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋…ธ๋“œ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ธ”๋ก ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ๋‹จ์œ„ ๋ฐ์ดํ„ฐ๋Š” ๋ณต์ œํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์œ ์‹ค์— ๋Œ€๋น„ํ•ฉ๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ๋Š” ์žกํŠธ๋ž˜์ปค์™€ ํƒœ์Šคํฌ ํŠธ๋ž˜์ปค๊ฐ€ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์žกํŠธ๋ž˜์ปค๊ฐ€ ์ „์ฒด ์ž‘์—…์˜ ์ง„ํ–‰ ์ƒํ™ฉ์„ ๊ด€๋ฆฌํ•˜๊ณ , ์ž์› ๊ด€๋ฆฌ๋„ ์ฒ˜๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ์ตœ๋Œ€ 4000๋Œ€์˜ ๋…ธ๋“œ๋ฅผ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํƒœ์Šคํฌ ํŠธ๋ž˜์ปค๋Š” ์‹ค์ œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ์˜ ์ž‘์—… ๋‹จ์œ„๋Š” ์Šฌ๋กฏ(slot)์ž…๋‹ˆ๋‹ค. ๋งต ์Šฌ๋กฏ, ๋ฆฌ๋“€์Šค ์Šฌ๋กฏ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๊ณ , ์‹คํ–‰ ์‹œ์ ์— ์—ญํ• ์ด ์ •ํ•ด์ง€๋ฉด ์Šฌ๋กฏ์˜ ์šฉ๋„๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋งต ์ž‘์—…์ด ์ง„ํ–‰ ์ค‘์—๋Š” ๋ฆฌ๋“€์Šค ์Šฌ๋กฏ์€ ๋Œ€๊ธฐ ์ƒํƒœ๋กœ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ 100% ํ™œ์šฉ๋˜์ง€ ์•Š์„ ๋•Œ๋„ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก v1 ํŠน์ง• ๋ถ„์‚ฐ ์ €์žฅ, ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ •์˜ ๋ถ„์‚ฐ ์ €์žฅ(HDFS) ๋„ค์ž„๋…ธ๋“œ, ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๊ฐ€ ์ฒ˜๋ฆฌ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ(MapReduce) ์žกํŠธ๋ž˜์ปค, ํ…Œ์ŠคํŠธ ํŠธ๋ž˜์ปค๊ฐ€ ์ฒ˜๋ฆฌ ํด๋Ÿฌ์Šคํ„ฐ ๋‹น ์ตœ๋Œ€ 4000๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ๋“ฑ๋ก ์ž‘์—… ์ฒ˜๋ฆฌ๋ฅผ ์Šฌ๋กฏ(slot) ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌ ๋งต, ๋ฆฌ๋“€์Šค ์Šฌ๋กฏ์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ฒ˜๋ฆฌ ํ•˜๋‘ก v2 2012๋…„ ์ •์‹ ๋ฐœํ‘œ๋œ ํ•˜๋‘ก v2๋Š” ์žกํŠธ๋ž˜์ปค์˜ ๋ณ‘๋ชฉํ˜„์ƒ์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•˜์—ฌ YARN ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋„์ž…ํ•˜์˜€์Šต๋‹ˆ๋‹ค. YARN ์•„ํ‚คํ…์ฒ˜๋Š” ์žกํŠธ๋ž˜์ปค์˜ ๊ธฐ๋Šฅ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์ž์›๊ด€๋ฆฌ๋Š” ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์™€ ๋…ธ๋“œ ๋งค๋‹ˆ์ €๊ฐ€ ๋‹ด๋‹นํ•˜๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ผ์ดํ”„ ์‚ฌ์ดํด ๊ด€๋ฆฌ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๊ฐ€ ๋‹ด๋‹นํ•˜๊ณ , ์ž‘์—…์˜ ์ฒ˜๋ฆฌ๋Š” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ž์›๊ด€๋ฆฌ์™€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ด€๋ฆฌ์˜ ๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•ด ํด๋Ÿฌ์Šคํ„ฐ ๋‹น ์ตœ๋Œ€ ๋งŒ๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. YARN ์•„ํ‚คํ…์ฒ˜์˜ ์ž‘์—…์˜ ์ฒ˜๋ฆฌ ๋‹จ์œ„๋Š” ์ปจํ…Œ์ด๋„ˆ์ž…๋‹ˆ๋‹ค. ์ž‘์—…์— ์ œ์ถœ๋˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๊ฐ€ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์— ์ž์›์„ ์š”์ฒญํ•˜์—ฌ ์‹ค์ œ ์ž‘์—…์„ ๋‹ด๋‹นํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ• ๋‹น๋ฐ›์•„ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ๋Š” ์ž‘์—…์ด ์š”์ฒญ๋˜๋ฉด ์ƒ์„ฑ๋˜๊ณ , ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ์ข…๋ฃŒ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ YARN ์•„ํ‚คํ…์ฒ˜์—์„œ๋Š” MR๋กœ ๊ตฌํ˜„๋œ ์ž‘์—…์ด ์•„๋‹ˆ์–ด๋„ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ• ๋‹น๋ฐ›์•„์„œ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Spark, HBase, Storm ๋“ฑ ๋‹ค์–‘ํ•œ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก v2 ํŠน์ง• YARN์„ ๋„์ž…ํ•˜์—ฌ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝ ํด๋Ÿฌ์Šคํ„ฐ ๊ด€๋ฆฌ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €, ๋…ธ๋“œ ๋งค๋‹ˆ์ € ์ž‘์—… ๊ด€๋ฆฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ, ์ปจํ…Œ์ด๋„ˆ MR ์™ธ Spark, Hive, Pig ๋“ฑ ๋‹ค๋ฅธ ๋ถ„์‚ฐ ์ฒ˜๋ฆฌ ๋ชจ๋ธ๋„ ์ˆ˜ํ–‰ ๊ฐ€๋Šฅ ํด๋Ÿฌ์Šคํ„ฐ ๋‹น 1๋งŒ ๊ฐœ ์ด์ƒ์˜ ๋…ธ๋“œ ๋“ฑ๋ก ๊ฐ€๋Šฅ ์ž‘์—… ์ฒ˜๋ฆฌ๋ฅผ ์ปจํ…Œ์ด๋„ˆ(container) ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌ ํ•˜๋‘ก v3 2017๋…„ ์ •์‹ ๋ฐœํ‘œ๋œ ํ•˜๋‘ก v3๋Š” ์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ, YARN ํƒ€์ž„๋ผ์ธ ์„œ๋น„์Šค v2 ๋“ฑ์ด ๋„์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก v2๊นŒ์ง€ HDFS์—์„œ ์žฅ์•  ๋ณต๊ตฌ๋ฅผ ์œ„ํ•ด ํŒŒ์ผ ๋ณต์ œ๋ฅผ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋ณต์ œ ๋‹จ์œ„๊ฐ€ 3๊ฐœ์—ฌ์„œ, ํŒŒ์ผ 1๊ฐœ๋‹น 2๊ฐœ์˜ ๋ณต์ œ๋ณธ์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด 1G ๋ฐ์ดํ„ฐ ์ €์žฅ์— 3G์˜ ์ €์žฅ์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ์€ ํŒจ๋ฆฌํ‹ฐ ๋ธ”๋ก์„ ์ด์šฉํ•˜์—ฌ 1G ๋ฐ์ดํ„ฐ ์ €์žฅ์— 1.5G์˜ ๋””์Šคํฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๊ณ  ์ €์žฅ์†Œ์˜ ํšจ์œจ์„ฑ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ YARN ํƒ€์ž„๋ผ์ธ ์„œ๋ฒ„๋ฅผ ๊ฐœ์„ ํ•˜๊ณ , ํ•˜๋‘ก v1๋ถ€ํ„ฐ ์‚ฌ์šฉํ•˜๋˜ ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑํ•˜์—ฌ ๋ฒ„๊ทธ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋„ค์ดํ‹ฐ๋ธŒ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์…”ํ”Œ ๋‹จ๊ณ„์˜ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ , JAVA8์„ ์ง€์›ํ•˜๋„๋ก ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ณ ๊ฐ€์šฉ์„ฑ์„ ์œ„ํ•˜์—ฌ 2๊ฐœ ์ด์ƒ์˜ ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์ง€์›ํ•˜๋Š” ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ฐœ์„ ์ ์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก v3 ํŠน์ง• ์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ ๋„์ž… ๊ธฐ์กด์˜ ๋ธ”๋ก ๋ณต์ œ(Replication)๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ HDFS ์‚ฌ์šฉ๋Ÿ‰ ๊ฐ์†Œ YARN ํƒ€์ž„๋ผ์ธ ์„œ๋น„์Šค v2 ๋„์ž… ๊ธฐ์กด ํƒ€์ž„๋ผ์ธ ์„œ๋น„์Šค๋ณด๋‹ค ๋งŽ์€ ์ •๋ณด๋ฅผ ํ™•์ธ ๊ฐ€๋Šฅ ์Šคํฌ๋ฆฝํŠธ ์žฌ์ž‘์„ฑ ๋ฐ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ํ˜•ํƒœ๋กœ ์ˆ˜์ • ์˜ค๋ž˜๋œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์žฌ์ž‘์„ฑํ•˜์—ฌ ๋ฒ„๊ทธ ์ˆ˜์ • ๊ธฐ๋ณธ ํฌํŠธ ๋ณ€๊ฒฝ NameNode 50470 โ†’ 9871 50070 โ†’ 9870 8020 โ†’ 9820 Secondary NameNode 50091 โ†’ 9869 50090 โ†’ 9868 DataNode ports: 50020 โ†’ 9867 50010 โ†’ 9866 50475 โ†’ 9865 50075 โ†’ 9864 JAVA8 ์ง€์› ๋„ค์ดํ‹ฐ๋ธŒ ์ฝ”๋“œ ์ตœ์ ํ™” ๊ณ ๊ฐ€์šฉ์„ฑ์„ ์œ„ํ•ด 2๊ฐœ ์ด์ƒ์˜ ๋„ค์ž„๋…ธ๋“œ ์ง€์› ํ•˜๋‚˜๋งŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ์Šคํƒ ๋ฐ”์ด ๋…ธ๋“œ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์ง€์› ๊ฐ€๋Šฅ ์Šคํƒ ๋ฐ”์ด ๋…ธ๋“œ Ozone ์ถ”๊ฐ€ ์˜ค๋ธŒ์ ํŠธ ์ €์žฅ์†Œ ์ถ”๊ฐ€ ์ฐธ๊ณ  NEW Hadoop3์™€ Erasure Coding(๋ฐ”๋กœ ๊ฐ€๊ธฐ) Hadooop Realease (๋ฐ”๋กœ ๊ฐ€๊ธฐ) ํ•˜๋‘ก์€ ํ˜„์žฌ(2020.05 ๊ธฐ์ค€) v3๊นŒ์ง€ ๊ณต๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. โ†ฉ 2-HDFS HDFS(Hadoop Distributed File System)๋Š” ๋ฒ”์šฉ ํ•˜๋“œ์›จ์–ด์—์„œ ๋™์ž‘ํ•˜๊ณ , ์žฅ์•  ๋ณต๊ตฌ์„ฑ์„ ๊ฐ€์ง€๋Š” ๋ถ„์‚ฐ ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. HDFS๋Š” ์‹ค์‹œ๊ฐ„ ์ฒ˜๋ฆฌ๋ณด๋‹ค๋Š” ๋ฐฐ์น˜์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋น ๋ฅธ ๋ฐ์ดํ„ฐ ์‘๋‹ต์‹œ๊ฐ„์ด ํ•„์š”ํ•œ ์ž‘์—…์—๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ๋‹จ์ผ ์‹คํŒจ ์ง€์ (SPOF)์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋„ค์ž„๋…ธ๋“œ ๊ด€๋ฆฌ๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํŠน์ง• ๋ธ”๋ก ๋‹จ์œ„ ์ €์žฅ HDFS๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ธ”๋ก ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ์‚ฌ์ด์ฆˆ๋ณด๋‹ค ์ž‘์€ ํŒŒ์ผ์€ ๊ธฐ์กด ํŒŒ์ผ์˜ ์‚ฌ์ด์ฆˆ๋กœ ์ €์žฅํ•˜๊ณ , ๋ธ”๋ก ์‚ฌ์ด์ฆˆ๋ณด๋‹ค ํฐ ํฌ๊ธฐ์˜ ๋ฐ์ดํ„ฐํŒŒ์ผ์€ ๋ธ”๋ก ๋‹จ์œ„ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ผ ๋””์Šคํฌ์˜ ๋ฐ์ดํ„ฐ๋ณด๋‹ค ํฐ ํŒŒ์ผ๋„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ธ”๋ก ๋‹จ์œ„๊ฐ€ 256MB ์ผ ๋•Œ 1G ํŒŒ์ผ์€ 4๊ฐœ์˜ ๋ธ”๋ก์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ €์žฅ๋˜๊ณ , 10MB ํŒŒ์ผ์€ ํ•˜๋‚˜์˜ ๋ธ”๋ก์œผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ๋ณต์ œ๋ฅผ ์ด์šฉํ•œ ์žฅ์•  ๋ณต๊ตฌ HDFS๋Š” ์žฅ์•  ๋ณต๊ตฌ๋ฅผ ์œ„ํ•ด์„œ ๊ฐ ๋ธ”๋ก์„ ๋ณต์ œํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก์˜ ๊ธฐ๋ณธ ๋ณต์ œ ๋‹จ์œ„๋Š” 3์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋ธ”๋ก์€ 3๊ฐœ์˜ ๋ธ”๋ก์œผ๋กœ ๋ณต์ œ๋˜๊ณ , ๊ฐ™์€ ๋ž™(Rack)์˜ ์„œ๋ฒ„์™€ ๋‹ค๋ฅธ ๋ž™(Rack)์˜ ์„œ๋ฒ„๋กœ ๋ณต์ œ๋˜์–ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋ฉด ๋ณต์ œํ•œ ๋‹ค๋ฅธ ๋ธ”๋ก์„ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต๊ตฌํ•ฉ๋‹ˆ๋‹ค. 1G ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณต์ œ๋˜์–ด 3G์˜ ์ €์žฅ ๊ณต๊ฐ„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ฝ๊ธฐ ์ค‘์‹ฌ HDFS๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ ์“ฐ๋ฉด ์—ฌ๋Ÿฌ ๋ฒˆ ์ฝ๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŒŒ์ผ์˜ ์ˆ˜์ •์€ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ์˜ ์ˆ˜์ •์„ ์ œํ•œํ•˜์—ฌ ๋™์ž‘์„ ๋‹จ์ˆœํ™”ํ•˜๊ณ  ์ด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ๋•Œ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ง€์—ญ์„ฑ ๋งต๋ฆฌ๋“€์Šค๋Š” HDFS์˜ ๋ฐ์ดํ„ฐ ์ง€์—ญ์„ฑ์„ ์ด์šฉํ•ด์„œ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค. ์ฒ˜๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ๋Š” ๊ณณ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋™์‹œํ‚ค์ง€ ์•Š๊ณ , ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๊ณณ์—์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋™์‹œํ‚ค๋Š” ๋น„์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 01-๊ตฌ์กฐ(Architecture) HDFS๋Š” ๋งˆ์Šคํ„ฐ ์Šฌ๋ ˆ์ด๋ธŒ ๊ตฌ์กฐ๋กœ ํ•˜๋‚˜์˜ ๋„ค์ž„๋…ธ๋“œ์™€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋…ธ๋“œ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๋ฐ์ดํ„ฐ๋Š” ๋ธ”๋ก ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด ๋ฐ์ดํ„ฐ๋…ธ๋“œ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๊ณ , ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ข…๋ฅ˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ €์žฅ ํ˜•ํƒœ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ ๊ด€๋ฆฌ ๋ฐ์ดํ„ฐ๋…ธ๋“œ ๋ธ”๋ก ํŒŒ์ผ ์ €์žฅ ํ˜•ํƒœ ๋ฐ์ดํ„ฐ๋…ธ๋“œ ์ƒํƒœ ํ™œ์„ฑ์ƒํƒœ ์šด์˜ ์ƒํƒœ ๋„ค์ž„๋…ธ๋“œ ๊ตฌ๋™ ๊ณผ์ • ํŒŒ์ผ ์ฝ๊ธฐ/์“ฐ๊ธฐ ํŒŒ์ผ ์ฝ๊ธฐ ํŒŒ์ผ ์“ฐ๊ธฐ ์ฐธ๊ณ  ๋„ค์ž„๋…ธ๋“œ ๋„ค์ž„๋…ธ๋“œ์˜ ์ฃผ์š” ์—ญํ• ์€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ์™€ ๋ฐ์ดํ„ฐ๋…ธ๋“œ์˜ ๊ด€๋ฆฌ์ž…๋‹ˆ๋‹ค. ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋Š” ํŒŒ์ผ ์ด๋ฆ„, ํŒŒ์ผ ํฌ๊ธฐ, ํŒŒ์ผ ์ƒ์„ฑ ์‹œ๊ฐ„, ํŒŒ์ผ ์ ‘๊ทผ ๊ถŒํ•œ, ํŒŒ์ผ ์†Œ์œ ์ž ๋ฐ ๊ทธ๋ฃน ์†Œ์œ ์ž, ํŒŒ์ผ์ด ์œ„์น˜ํ•œ ๋ธ”๋ก์˜ ์ •๋ณด ๋“ฑ์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ฐ์ดํ„ฐ๋…ธ๋“œ์—์„œ ์ „๋‹ฌํ•˜๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ์ „์ฒด ๋…ธ๋“œ์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ •๋ณด์™€ ํŒŒ์ผ ์ •๋ณด๋ฅผ ๋ฌถ์–ด์„œ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์œ„์น˜(dfs.name.dir)์— ๋ณด๊ด€๋ฉ๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ์‹คํ–‰๋  ๋•Œ ํŒŒ์ผ์„ ์ฝ์–ด์„œ ๋ฉ”๋ชจ๋ฆฌ์— ๋ณด๊ด€ํ•ฉ๋‹ˆ๋‹ค. ์šด์˜ ์ค‘์— ๋ฐœ์ƒํ•œ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋„ค์ž„๋…ธ๋“œ์˜ ๋ฉ”๋ชจ๋ฆฌ์—๋Š” ๋ฐ”๋กœ ์ ์šฉ๋˜๊ณ , ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์ •์‚ฌํ•ญ์„ ๋‹ค์Œ ๊ตฌ๋™์‹œ ์ ์šฉ์„ ์œ„ํ•ด์„œ ์ฃผ๊ธฐ์ ์œผ๋กœ Edist ํŒŒ์ผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ข…๋ฅ˜ Fsimage ํŒŒ์ผ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์™€ ๋ธ”๋ก ์ •๋ณด Edits ํŒŒ์ผ ํŒŒ์ผ์˜ ์ƒ์„ฑ, ์‚ญ์ œ์— ๋Œ€ํ•œ ํŠธ๋žœ์žญ์…˜ ๋กœ๊ทธ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๋‹ค๊ฐ€ ์ฃผ๊ธฐ์ ์œผ๋กœ ์ƒ์„ฑ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ €์žฅ ํ˜•ํƒœ ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์œ„์น˜(dfs.name.dir)์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŒŒ์ผ์˜ ํ˜•ํƒœ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. # ๋„ค์ž„๋…ธ๋“œ์˜ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ƒ์„ฑ๋จ $ ls -alh /hadoop/hdfs/namenode/current total 1847556 -rw-r--r-- 1 hdfs hadoop 217 Sep 2 03:36 VERSION -rw-r--r-- 1 hdfs hadoop 1523554 Feb 21 14:30 edits_0000000003027897761-0000000003027906608 -rw-r--r-- 1 hdfs hadoop 1351557 Feb 21 14:32 edits_0000000003027906609-0000000003027914396 -rw-r--r-- 1 hdfs hadoop 1391038 Feb 21 14:34 edits_0000000003027914397-0000000003027922191 ... -rw-r--r-- 1 hdfs hadoop 836860 Feb 21 23:53 edits_0000000003029906692-0000000003029911098 -rw-r--r-- 1 hdfs hadoop 890807 Feb 21 23:55 edits_0000000003029911099-0000000003029915811 -rw-r--r-- 1 hdfs hadoop 1048576 Feb 21 23:56 edits_inprogress_0000000003029915812 -rw-r--r-- 1 hdfs hadoop 760128990 Feb 21 18:50 fsimage_0000000003028899087 -rw-r--r-- 1 hdfs hadoop 62 Feb 21 18:50 fsimage_0000000003028899087.md5 -rw-r--r-- 1 hdfs hadoop 762005689 Feb 21 23:50 fsimage_0000000003029901533 -rw-r--r-- 1 hdfs hadoop 62 Feb 21 23:50 fsimage_0000000003029901533.md5 -rw-r--r-- 1 hdfs hadoop 11 Feb 21 23:55 seen_txid ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ VERSION: ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ HDFS์˜ ID, ํƒ€์ž… ๋“ฑ ์ •๋ณด edits_0000xxx-0000xxx: ํŠธ๋žœ์žญ์…˜ ์ •๋ณด. edits_ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘๋ฒˆํ˜ธ-ํŠธ๋žœ์žญ์…˜ ์ข…๋ฃŒ ๋ฒˆํ˜ธ๊นŒ์ง€์˜ ์ •๋ณด๋ฅผ ์ €์žฅ eidts_inprogress_000xx: ์ตœ์‹  ํŠธ๋žœ์žญ์…˜ ์ •๋ณด. ์••์ถ•๋˜์ง€ ์•Š์€ ์ •๋ณด fsimage_000xxx: 000xxx๊นŒ์ง€ ํŠธ๋žœ์žญ์…˜ ์ •๋ณด๊ฐ€ ์ฒ˜๋ฆฌ๋œ fsimage fsimage_000xxx.md5: fsiamge์˜ ํ•ด์‹œ๊ฐ’ seen_txid: ํ˜„์žฌ ํŠธ๋žœ์žญ์…˜ ID ๋ฐ์ดํ„ฐ ๋…ธ๋“œ ๊ด€๋ฆฌ ๋„ค์ž„๋…ธ๋“œ๋Š” ๋ฐ์ดํ„ฐ๋…ธ๋“œ๊ฐ€ ์ฃผ๊ธฐ์ ์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ํ•˜ํŠธ๋น„ํŠธ(3์ดˆ, dfs.heart beat.interval)์™€ ๋ธ”๋ก ๋ฆฌํฌํŠธ(6์‹œ๊ฐ„, dfs.blockreport.intervalMsec)๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์˜ ๋™์ž‘ ์ƒํƒœ, ๋ธ”๋ก ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜ํŠธ๋น„ํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋…ธ๋“œ๊ฐ€ ๋™์ž‘ ์ค‘์ด๋ผ๋Š” ๊ฒƒ์„ ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜ํŠธ๋น„ํŠธ๊ฐ€ ๋„์ฐฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋„ค์ž„๋…ธ๋“œ๋Š” ๋ฐ์ดํ„ฐ๋…ธ๋“œ๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•˜๊ณ , ๋” ์ด์ƒ IO ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ์กฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ๋ฆฌํฌํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ HDFS์— ์ €์žฅ๋œ ํŒŒ์ผ์— ๋Œ€ํ•œ ์ตœ์‹  ์ •๋ณด๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ๋ฆฌํฌํŠธ์—๋Š” ๋ฐ์ดํ„ฐ๋…ธ๋“œ์— ์ €์žฅ๋œ ๋ธ”๋ก ๋ชฉ๋ก๊ณผ ๊ฐ ๋ณผ๋ก์ด ๋กœ์ปฌ ๋””์Šคํฌ์˜ ์–ด๋””์— ์ €์žฅ๋˜์–ด ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋…ธ๋“œ ๋ฐ์ดํ„ฐ๋…ธ๋“œ๋Š” ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์€ ๋ธ”๋ก ๋‹จ์œ„๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋…ธ๋“œ๋Š” ์ฃผ๊ธฐ์ ์œผ๋กœ ๋„ค์ž„๋…ธ๋“œ์— ํ•˜ํŠธ๋น„ํŠธ์™€ ๋ธ”๋ก ๋ฆฌํฌํŠธ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜ํŠธ๋น„ํŠธ๋Š” ๋ฐ์ดํ„ฐ๋…ธ๋“œ์˜ ๋™์ž‘ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š”๋ฐ ์ด์šฉ๋ฉ๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ๋Š” ํ•˜ํŠธ๋น„ํŠธ๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋…ธ๋“œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•˜์—ฌ ๋” ์ด์ƒ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ๋ฆฌํฌํŠธ๋กœ ๋ธ”๋ก์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ฒดํฌํ•˜๊ณ , ๋„ค์ž„๋…ธ๋“œ์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ํŒŒ์ผ ์ €์žฅ ํ˜•ํƒœ ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์œ„์น˜(dfs.data.dir)์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŒŒ์ผ์˜ ํ˜•ํƒœ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก์€ ๋ธ”๋ก๊ณผ ๋ธ”๋ก์˜ ๋ฉ”ํƒ€ ์ •๋ณด๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. blk_12345 ํŒŒ์ผ ๋ธ”๋ก ์ตœ๋Œ€ ํฌ๊ธฐ๊ฐ€ ๋ธ”๋ก ์‚ฌ์ด์ฆˆ(dfs.blocksize) ํฌ๊ธฐ๋กœ ์ƒ์„ฑ ๋ธ”๋ก ๋ณต์ œ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ๋™์ผํ•œ ์ด๋ฆ„์˜ ๋ธ”๋ก์ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋…ธ๋“œ์— ์ƒ์„ฑ๋จ blk_12345_29082353.meta ๋ธ”๋ก์˜ ๋ฉ”ํƒ€ ์ •๋ณด ./hdfs/current/BP-11233441/current/finalized/subdir187/subdir191: total 676K drwxr-xr-x 2 hdfs hdfs 4.0K Sep 8 04:30. drwxr-xr-x 258 hdfs hdfs 8.0K Aug 31 22:21 .. -rw-r--r-- 1 hdfs hdfs 40K Aug 31 22:46 blk_12345 -rw-r--r-- 1 hdfs hdfs 327 Aug 31 22:46 blk_12345_29082353.meta -rw-r--r-- 1 hdfs hdfs 19K Aug 31 22:46 blk_12346 -rw-r--r-- 1 hdfs hdfs 155 Aug 31 22:46 blk_12346_29082375.meta -rw-r--r-- 1 hdfs hdfs 262K Aug 31 22:46 blk_12347 -rw-r--r-- 1 hdfs hdfs 2.1K Aug 31 22:46 blk_12347_29082433.meta ๋ฐ์ดํ„ฐ๋…ธ๋“œ ์ƒํƒœ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •๋ณด๋Š” ๋‘ ๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์œ ํ˜•์€ ํ™œ์„ฑ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์œ ํ˜•์€ ์„œ๋น„์Šค๊ฐ€ ์šด์˜ ์ค‘์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํ™œ์„ฑ์ƒํƒœ ํ™œ์„ฑ ์ƒํƒœ๋Š” ๋ฐ์ดํ„ฐ๋…ธ๋“œ๊ฐ€ Live ์ƒํƒœ์ธ์ง€ Dead ์ƒํƒœ์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋…ธ๋“œ๊ฐ€ ํ•˜ํŠธ๋น„ํŠธ๋ฅผ ์ฃผ๊ธฐ์ ์œผ๋กœ ์ „๋‹ฌํ•˜์—ฌ ์‚ด์•„ ์žˆ๋Š”์ง€ ํ™•์ธ๋˜๋ฉด Live ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋…ธ๋“œ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์ง€์ •ํ•œ ์‹œ๊ฐ„ ๋™์•ˆ(dfs.namenode.stale.datanode.interval) ํ•˜ํŠธ๋น„ํŠธ๋ฅผ ๋ฐ›์ง€ ๋ชปํ•˜๋ฉด ๋„ค์ž„๋…ธ๋“œ๋Š” ๋ฐ์ดํ„ฐ๋…ธ๋“œ์˜ ์ƒํƒœ๋ฅผ Stale ์ƒํƒœ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ ์ง€์ •ํ•œ ์‹œ๊ฐ„ ๋™์•ˆ ์‘๋‹ต์ด ์—†์œผ๋ฉด Dead ๋…ธ๋“œ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ์šด์˜ ์ƒํƒœ ์šด์˜ ์ƒํƒœ๋Š” ๋ฐ์ดํ„ฐ๋…ธ๋“œ์˜ ์—…๊ทธ๋ ˆ์ด๋“œ, ํŒจ์น˜ ๊ฐ™์€ ์ž‘์—…์„ ํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋น„์Šค๋ฅผ ์ž ์‹œ ๋ฉˆ์ถ”์–ด์•ผ ํ•  ๊ฒฝ์šฐ ๋ธ”๋ก์„ ์•ˆ์ „ํ•˜๊ฒŒ ๋ณด๊ด€ํ•˜๊ธฐ ์œ„ํ•ด ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์ƒํƒœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. NORMAL: ์„œ๋น„์Šค ์ƒํƒœ DECOMMISSIONED: ์„œ๋น„์Šค ์ค‘๋‹จ ์ƒํƒœ DECOMMISSION_INPROGRESS: ์„œ๋น„์Šค ์ค‘๋‹จ ์ƒํƒœ๋กœ ์ง„ํ–‰ ์ค‘ IN_MAINTENANCE: ์ •๋น„ ์ƒํƒœ ENTERING_MAINTENANCE: ์ •๋น„ ์ƒํƒœ๋กœ ์ง„ํ–‰ ์ค‘ dfs.hosts ํŒŒ์ผ(DataNodeAdmin์— ๋…ธ๋“œ์˜ ์ƒํƒœ๋ฅผ json ํ˜•ํƒœ๋กœ ๊ธฐ์ˆ ํ•˜์—ฌ ์šด์˜ ์ค‘์— ์„œ๋น„์Šค๋ฅผ ์ž ์‹œ ๋ฉˆ์ถ”๊ณ  ์ •๋น„๋ฅผ ํ•œ ํ›„ ๋‹ค์‹œ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ ๊ตฌ๋™ ๊ณผ์ • ๋„ค์ž„๋…ธ๋“œ๋Š” ๋‹ค์Œ์˜ ์ˆœ์„œ๋กœ ๊ตฌ๋™๋ฉ๋‹ˆ๋‹ค. Fsimage์™€ Edits๋ฅผ ์ฝ์–ด์„œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ํŒŒ์ผ์˜ ํฌ๊ธฐ๊ฐ€ ํฌ๋ฉด ๊ตฌ๋™ ์‹œ์ž‘ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Fsimage๋ฅผ ์ฝ์–ด ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌํ•ฉ๋‹ˆ๋‹ค. Edits ํŒŒ์ผ์„ ์ฝ์–ด์™€์„œ ๋ณ€๊ฒฝ ๋‚ด์—ญ์„ ๋ฐ˜์˜ ํ˜„์žฌ์˜ ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ๋ฅผ ์Šค๋ƒ…์ƒท์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ Fsimage ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๋ธ”๋ก ๋ฆฌํฌํŠธ๋ฅผ ์ˆ˜์‹ ํ•˜์—ฌ ๋งคํ•‘ ์ •๋ณด ์ƒ์„ฑ ์„œ๋น„์Šค ์‹œ์ž‘ ํŒŒ์ผ ์ฝ๊ธฐ/์“ฐ๊ธฐ HDFS์˜ ํŒŒ์ผ์— ์ ‘๊ทผํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ HDFS ๋ช…๋ นํ–‰ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ Java, C API๋ฅผ ์ œ๊ณตํ•˜์—ฌ ์ด๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ์ ‘๊ทผํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ์ฝ๊ธฐ ๋„ค์ž„๋…ธ๋“œ์— ํŒŒ์ผ์ด ๋ณด๊ด€๋œ ๋ธ”๋ก ์œ„์น˜ ์š”์ฒญ ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ๋ธ”๋ก ์œ„์น˜ ๋ฐ˜ํ™˜ ๊ฐ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์— ํŒŒ์ผ ๋ธ”๋ก์„ ์š”์ฒญ ๋…ธ๋“œ์˜ ๋ธ”๋ก์ด ๊นจ์ ธ ์žˆ์œผ๋ฉด ๋„ค์ž„๋…ธ๋“œ์— ์ด๋ฅผ ํ†ต์ง€ํ•˜๊ณ  ๋‹ค๋ฅธ ๋ธ”๋ก ํ™•์ธ ํŒŒ์ผ ์“ฐ๊ธฐ ๋„ค์ž„๋…ธ๋“œ์— ํŒŒ์ผ ์ •๋ณด๋ฅผ ์ „์†กํ•˜๊ณ , ํŒŒ์ผ์˜ ๋ธ”๋ก์„ ์จ์•ผ ํ•  ๋…ธ๋“œ ๋ชฉ๋ก ์š”์ฒญ ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ํŒŒ์ผ์„ ์ €์žฅํ•  ๋ชฉ๋ก ๋ฐ˜ํ™˜ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์— ํŒŒ์ผ ์“ฐ๊ธฐ ์š”์ฒญ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ ๊ฐ„ ๋ณต์ œ๊ฐ€ ์ง„ํ–‰ ์ฐธ๊ณ  ํ•˜๋‘ก ๋ฐ๋ชฌ์˜ ๊ธฐ๋™ ๊ณผ์ •๊ณผ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ดํ•ด DataNode Admin 1. ๋ธ”๋ก HDFS ํŒŒ์ผ์€ ์ง€์ •ํ•œ ํฌ๊ธฐ์˜ ๋ธ”๋ก์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง€๊ณ , ๊ฐ ๋ธ”๋ก์€ ๋…๋ฆฝ์ ์œผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. HDFS์˜ ๋ธ”๋ก์€ 128MB์™€ ๊ฐ™์ด ๋งค์šฐ ํฐ ๋‹จ์œ„์ž…๋‹ˆ๋‹ค. ๋ธ”๋ก์ด ํฐ ์ด์œ ๋Š” ํƒ์ƒ‰ ๋น„์šฉ์„ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ธ”๋ก์ด ํฌ๋ฉด ํ•˜๋“œ๋””์Šคํฌ์—์„œ ๋ธ”๋ก์˜ ์‹œ์ž‘์ ์„ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ๊ณ , ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋Š”๋ฐ ๋” ๋งŽ์€ ์‹œ๊ฐ„์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ธ”๋ก์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋Œ€์šฉ๋Ÿ‰ ํŒŒ์ผ์„ ์ „์†กํ•˜๋Š” ์‹œ๊ฐ„์€ ๋””์Šคํฌ IO ์†๋„์— ํฌ๊ฒŒ ์˜ํ–ฅ์„ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ํฌ๊ธฐ ํŒŒ์ผ ๋ถ„ํ•  ๊ธฐ๋ณธ ๋ธ”๋ก ํฌ๊ธฐ๋ฅผ ๋„˜์–ด์„œ๋Š” ํŒŒ์ผ์€ ๋ธ”๋ก ํฌ๊ธฐ๋กœ ๋ถ„ํ• ํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ํฌ๊ธฐ๋ณด๋‹ค ์ž‘์€ ํŒŒ์ผ์€ ๋‹จ์ผ ๋ธ”๋ก์œผ๋กœ ์ €์žฅ ์‹ค์ œ ํŒŒ์ผ ํฌ๊ธฐ์˜ ๋ธ”๋ก์ด ๋จ ๋ธ”๋ก ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋””์Šคํฌ ์‚ฌ์ด์ฆˆ๋ณด๋‹ค ๋” ํฐ ํŒŒ์ผ์„ ๋ณด๊ด€ํ•  ์ˆ˜ ์žˆ์Œ ๋ธ”๋ก ๋‹จ์œ„๋กœ ํŒŒ์ผ์„ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— 700G * 2 = 1.4T ํฌ๊ธฐ์˜ HDFS์— 1T์˜ ํŒŒ์ผ ์ €์žฅ ๊ฐ€๋Šฅ ๋ธ”๋ก ์ถ”์ƒํ™”์˜ ์ด์  ์ฒซ ๋ฒˆ์งธ๋Š” ํŒŒ์ผ ํ•˜๋‚˜์˜ ํฌ๊ธฐ๊ฐ€ ๋‹จ์ผ ๋””์Šคํฌ์˜ ์šฉ๋Ÿ‰๋ณด๋‹ค ๋” ์ปค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”์ผ์— ์ฒจ๋ถ€ํŒŒ์ผ์„ ์ „์†กํ•  ๋•Œ ํ•œ ๋ฒˆ์— ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š” ์šฉ๋Ÿ‰์ด ์ œํ•œ๋˜์–ด ์žˆ์„ ๋•Œ ๋ถ„ํ•  ์••์ถ•์„ ํ†ตํ•ด ์ „์†ก ๊ฐ€๋Šฅํ•œ ์šฉ๋Ÿ‰์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. HDFS๋„ ํฐ ํŒŒ์ผ์„ ๋ธ”๋ก ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด์„œ ๋‹จ์ผ ๋””์Šคํฌ์˜ ์šฉ๋Ÿ‰๋ณด๋‹ค ํฐ ํŒŒ์ผ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ํŒŒ์ผ ๋‹จ์œ„๋ณด๋‹ค ๋ธ”๋ก ๋‹จ์œ„๋กœ ์ถ”์ƒํ™”๋ฅผ ํ•˜๋ฉด ์Šคํ† ๋ฆฌ์ง€์˜ ์„œ๋ธŒ์‹œ์Šคํ…œ์„ ๋‹จ์ˆœํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ ํƒ์ƒ‰ ์ง€์ ์ด๋‚˜ ๋ฉ”ํƒ€์ •๋ณด๋ฅผ ์ €์žฅํ•  ๋•Œ ์‚ฌ์ด์ฆˆ๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๊ตฌํ˜„์ด ์ข€ ๋” ์‰ฝ์Šต๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ๋Š” ๋‚ด ๊ณ ์žฅ์„ฑ์„ ์ œ๊ณตํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ณต์ œ(replication)์„ ๊ตฌํ˜„ํ•  ๋•Œ ๋งค์šฐ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ๋‹จ์œ„๋กœ ๋ณต์ œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ ๋ณต์ œ์—๋„ ์–ด๋ ค์›€ ์—†์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ๋…ธ๋“œ์— ๊ฐ™์€ ๋ธ”๋ก์ด ์กด์žฌํ•˜์ง€ ์•Š๋„๋ก ๋ณต์ œํ•˜์—ฌ ๋…ธ๋“œ๊ฐ€ ๊ณ ์žฅ์ผ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ๋…ธ๋“œ์˜ ๋ธ”๋ก์œผ๋กœ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ธ”๋ก ์ง€์—ญ์„ฑ ๋งต๋ฆฌ๋“€์Šค๋ฅผ ์ด์šฉํ•œ ๋ถ„์‚ฐ ์ปดํ“จํŒ…์€ ๋ธ”๋ก์˜ ์ง€์—ญ์„ฑ์„ ์ด์šฉํ•ด ์„ฑ๋Šฅ์„ ๋†’์ž…๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ํ˜„์žฌ ๋…ธ๋“œ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ธ”๋ก์„ ์ด์šฉํ•˜๋Š” ๋ธ”๋ก ์ง€์—ญ์„ฑ(Block Locality)์„ ํ†ตํ•ด ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ์ „์†ก ์‹œ๊ฐ„ ๊ฐ์†Œ ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ํ™•์ธ์„ ์œ„ํ•œ ๋””์Šคํฌ ํƒ์ƒ‰ ์‹œ๊ฐ„ ๊ฐ์†Œ ์ ์ ˆํ•œ ๋‹จ์œ„์˜ ๋ธ”๋ก ํฌ๊ธฐ๋ฅผ ์ด์šฉํ•œ CPU ์ฒ˜๋ฆฌ์‹œ๊ฐ„ ์ฆ๊ฐ€ ํด๋ผ์šฐ๋“œ ์ €์žฅ ๊ณต๊ฐ„(eg. S3)์„ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ HDFS๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ณด๋‹ค ์†๋„๊ฐ€ ๋Š๋ ค์ง‘๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํด๋ผ์šฐ๋“œ ์ €์žฅ ๊ณต๊ฐ„์„ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์˜๊ตฌ์ ์ธ ๋ฐ์ดํ„ฐ ๋ณด๊ด€ ๋ฐ HDFS ๊ด€๋ฆฌ๋น„ ์ ˆ๊ฐ์— ๋”ฐ๋ฅธ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ธ”๋ก ์ž‘์—… ์ˆœ์„œ ๋ธ”๋ก ์ง€์—ญ์„ฑ์„ ์œ„ํ•œ ์ž‘์—… ์šฐ์„ ์ˆœ์œ„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ๋…ธ๋“œ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๊ฐ™์€ ๋ž™(Rack)์˜ ๋…ธ๋“œ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋‹ค๋ฅธ ๋ž™์˜ ๋…ธ๋“œ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋ธ”๋ก ์Šค์บ๋„ˆ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋Š” ์ฃผ๊ธฐ์ ์œผ๋กœ ๋ธ”๋ก ์Šค์บ๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋ธ”๋ก์˜ ์ฒดํฌ์„ฌ์„ ํ™•์ธํ•˜๊ณ  ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก ์บ์‹ฑ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ค‘ ์ž์ฃผ ์ฝ๋Š” ๋ธ”๋ก์€ ๋ธ”๋ก ์บ์‹œ(block cache)๋ผ๋Š” ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์˜ ๋ฉ”๋ชจ๋ฆฌ์— ๋ช…์‹œ์ ์œผ๋กœ ์บ์‹ฑ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ ๋‹จ์œ„๋กœ ์บ์‹ฑ ํ•  ์ˆ˜๋„ ์žˆ์–ด์„œ ์กฐ์ธ์— ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ ๋“ฑ๋กํ•˜์—ฌ ์ฝ๊ธฐ ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ hdfs cacheadmin Usage: bin/hdfs cacheadmin [COMMAND] [-addDirective -path <path> -pool <pool-name> [-force] [-replication <replication>] [-ttl <time-to-live>]] [-modifyDirective -id <id> [-path <path>] [-force] [-replication <replication>] [-pool <pool-name>] [-ttl <time-to-live>]] [-listDirectives [-stats] [-path <path>] [-pool <pool>] [-id <id>] [-removeDirective <id>] [-removeDirectives -path <path>] [-addPool <name> [-owner <owner>] [-group <group>] [-mode <mode>] [-limit <limit>] [-maxTtl <maxTtl>] [-modifyPool <name> [-owner <owner>] [-group <group>] [-mode <mode>] [-limit <limit>] [-maxTtl <maxTtl>]] [-removePool <name>] [-listPools [-stats] [<name>]] [-help <command-name>] # pool ๋“ฑ๋ก $ hdfs cacheadmin -addPool pool1 Successfully added cache pool pool1. # path ๋“ฑ๋ก $ hdfs cacheadmin -addDirective -path /user/hadoop/shs -pool pool1 Added cache directive 1 # ์บ์‹œ ํ™•์ธ hdfs cacheadmin -listDirectives Found 1 entry ID POOL REPL EXPIRY PATH 1 pool1 1 never /user/hadoop/shs 2. ์„ธ์ปจ๋”๋ฆฌ ๋„ค์ž„๋…ธ๋“œ ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ๊ตฌ๋™๋˜๊ณ  ๋‚˜๋ฉด Edits ํŒŒ์ผ์ด ์ฃผ๊ธฐ์ ์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ์˜ ํŠธ๋žœ์žญ์…˜์ด ๋นˆ๋ฒˆํ•˜๋ฉด ๋น ๋ฅธ ์†๋„๋กœ Edits ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋„ค์ž„๋…ธ๋“œ์˜ ๋””์Šคํฌ ๋ถ€์กฑ ๋ฌธ์ œ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ์žฌ๊ตฌ๋™ ๋˜๋Š” ์‹œ๊ฐ„์„ ๋Š๋ ค์ง€๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ์ปจ๋”๋ฆฌ ๋„ค์ž„๋…ธ๋“œ๋Š” Fsimage์™€ Edits ํŒŒ์ผ์„ ์ฃผ๊ธฐ์ ์œผ๋กœ ๋จธ์ง€ ํ•˜์—ฌ ์ตœ์‹  ๋ธ”๋ก์˜ ์ƒํƒœ๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ๋จธ์ง€ ํ•˜๋ฉด์„œ Edits ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋””์Šคํฌ ๋ถ€์กฑ ๋ฌธ์ œ๋„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 02-HDFS Federation ๋„ค์ž„๋…ธ๋“œ๋Š” ํŒŒ์ผ ์ •๋ณด ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์ด ๋งŽ์•„์ง€๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋Š˜์–ด๋‚˜๊ฒŒ ๋˜๊ณ , ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๊ฐ€ ๋ฌธ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ํ•˜๋‘ก v2๋ถ€ํ„ฐ HDFS ํŽ˜๋”๋ ˆ์ด์…˜์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. HDFS ํŽ˜๋”๋ ˆ์ด์…˜์€ ๋””๋ ‰ํ„ฐ๋ฆฌ(๋„ค์ž„์ŠคํŽ˜์ด์Šค) ๋‹จ์œ„๋กœ ๋„ค์ž„๋…ธ๋“œ๋ฅผ ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด user, hadoop, tmp ์„ธ ๊ฐœ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์กด์žฌํ•  ๋•Œ, /user, /hadoop, /tmp ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‹จ์œ„๋กœ ์ด 3๊ฐœ์˜ ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํŒŒ์ผ์„ ๊ด€๋ฆฌํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. HDFS ํŽ˜๋”๋ ˆ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์ผ, ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๋Š” ๋„ค์ž„์ŠคํŽ˜์ด์Šค์™€ ๋ธ”๋ก์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๋Š” ๋ธ”๋ก ํ’€์„ ๊ฐ ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ๋…๋ฆฝ์ ์œผ๋กœ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋„ค์ž„์ŠคํŽ˜์ด์Šค์™€ ๋ธ”๋ก ํ’€์„ ๋„ค์ž„์ŠคํŽ˜์ด์Šค ๋ณผ๋ฅจ์ด๋ผ ํ•˜๊ณ  ๋„ค์ž„์ŠคํŽ˜์ด์Šค ๋ณผ๋ฅจ์€ ๋…๋ฆฝ์ ์œผ๋กœ ๊ด€๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ ๋„ค์ž„๋…ธ๋“œ์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒจ๋„ ๋‹ค๋ฅธ ๋„ค์ž„๋…ธ๋“œ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  HDFS Federation ์ฐธ๊ณ  HDFS Federation ์„ค์ • 03-HDFS ๊ณ ๊ฐ€์šฉ์„ฑ HDFS๋Š” ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ๋‹จ์ผ ์‹คํŒจ ์ง€์ ์ž…๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋ชจ๋“  ์ž‘์—…์ด ์ค‘์ง€๋˜๊ณ , ํŒŒ์ผ์„ ์ฝ๊ฑฐ๋‚˜ ์“ธ ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜๋‘ก v2์—์„œ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ HDFS ๊ณ ๊ฐ€์šฉ์„ฑ(High Availability)์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. HDFS ๊ณ ๊ฐ€์šฉ์„ฑ์€ ์ด์ค‘ํ™”๋œ ๋‘ ๋Œ€์˜ ์„œ๋ฒ„์ธ ์•กํ‹ฐ๋ธŒ(active) ๋„ค์ž„๋…ธ๋“œ์™€ ์Šคํƒ ๋ฐ”์ด(standby) ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์•กํ‹ฐ๋ธŒ ๋„ค์ž„๋…ธ๋“œ์™€ ์Šคํƒ ๋ฐ”์ด ๋„ค์ž„๋…ธ๋“œ๋Š” ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๋ธ”๋ก ๋ฆฌํฌํŠธ์™€ ํ•˜ํŠธ๋น„ํŠธ๋ฅผ ๋ชจ๋‘ ๋ฐ›์•„์„œ ๋™์ผํ•œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ง€ํ•˜๊ณ , ๊ณต์œ  ์Šคํ† ๋ฆฌ์ง€๋ฅผ ์ด์šฉํ•˜์—ฌ ์—๋””ํŠธ ํŒŒ์ผ์„ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ์•กํ‹ฐ๋ธŒ ๋„ค์ž„๋…ธ๋“œ๋Š” ๋„ค์ž„๋…ธ๋“œ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ์Šคํƒ ๋ฐ”์ด ๋„ค์ž„๋…ธ๋“œ๋Š” ์•กํ‹ฐ๋ธŒ ๋„ค์ž„๋…ธ๋“œ์™€ ๋™์ผํ•œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ •๋ณด๋ฅผ ์œ ์ง€ํ•˜๋‹ค๊ฐ€, ์•กํ‹ฐ๋ธŒ ๋„ค์ž„๋…ธ๋“œ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์Šคํƒ ๋ฐ”์ด ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ์•กํ‹ฐ๋ธŒ ๋„ค์ž„๋…ธ๋“œ๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์•กํ‹ฐ๋ธŒ ๋„ค์ž„๋…ธ๋“œ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ์ž๋™์œผ๋กœ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ๋ณดํ†ต ์ฃผํ‚คํผ๋ฅผ ์ด์šฉํ•˜์—ฌ ์žฅ์•  ๋ฐœ์ƒ ์‹œ ์ž๋™์œผ๋กœ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์Šคํƒ ๋ฐ”์ด ๋„ค์ž„๋…ธ๋“œ๋Š” ์„ธ์ปจ๋”๋ฆฌ ๋„ค์ž„๋…ธ๋“œ์˜ ์—ญํ• ์„ ๋™์ผํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ HDFS๋ฅผ ๊ณ ๊ฐ€์šฉ์„ฑ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜์˜€์„ ๋•Œ๋Š” ์„ธ์ปจ๋”๋ฆฌ ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ๊ณ ๊ฐ€์šฉ์„ฑ ๋ชจ๋“œ์—์„œ ์„ธ์ปจ๋”๋ฆฌ ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. QJM(Quorum Journal Manager) QJM์€ HDFS ์ „์šฉ ๊ตฌํ˜„์ฒด๋กœ, ๊ณ ๊ฐ€์šฉ์„ฑ ์—๋””ํŠธ ๋กœ๊ทธ๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์„ค๊ณ„๋˜์—ˆ๊ณ  HDFS์˜ ๊ถŒ์žฅ ์˜ต์…˜์ž…๋‹ˆ๋‹ค. QJM์€ ์ €๋„ ๋…ธ๋“œ ๊ทธ๋ฃน์—์„œ ๋™์ž‘ํ•˜๋ฉฐ, ๊ฐ ์—๋””ํŠธ ๋กœ๊ทธ๋Š” ์ „์ฒด ์ €๋„ ๋…ธ๋“œ์— ๋™์‹œ์— ์“ฐ์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ €๋„ ๋…ธ๋“œ๋Š” ์„ธ ๊ฐœ๋กœ ๊ตฌ์„ฑํ•˜๋ฉฐ, ๊ทธ์ค‘ ํ•˜๋‚˜๊ฐ€ ์†์ƒ๋˜์–ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋Š” ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์€ ์ฃผํ‚คํผ์˜ ์ž‘๋™ ๋ฐฉ์‹๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•˜์ง€๋งŒ QJM์€ ์ฃผํ‚คํผ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ์ด๋Ÿฐ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ๋‹ค๋Š” ์ ์ด ์ค‘์š”ํ•˜๋‹ค. ๋ฌผ๋ก  HDFS ๊ณ ๊ฐ€์šฉ์„ฑ์€ ์•กํ‹ฐ๋ธŒ ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์„ ์ถœํ•˜๊ธฐ ์œ„ํ•ด ์ฃผํ‚คํผ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์Šคํƒ ๋ฐ”์ด ๋„ค์ž„๋…ธ๋“œ๋ฅผ ํ™œ์„ฑํ™”์‹œํ‚ค๋Š” ์ „ํ™˜ ์ž‘์—…์€ ์žฅ์•  ๋ณต๊ตฌ ์ปจํŠธ๋กค๋Ÿฌ(failover controller)๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋กœ ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์žฅ์•  ๋ณต๊ตฌ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ธฐ๋ณธ ์„ค์ •์€ ๋‹จ ํ•˜๋‚˜์˜ ๋„ค์ž„๋…ธ๋“œ๋งŒ ํ™œ์„ฑ ์ƒํƒœ์— ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ์ฃผํ‚คํผ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. NFS(Network File System) NFS๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—๋””ํŠธ ํŒŒ์ผ์„ ๊ณต์œ  ์Šคํ† ๋ฆฌ์ง€๋ฅผ ์ด์šฉํ•˜์—ฌ<NAME>๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ณต์œ  ์Šคํ† ๋ฆฌ์ง€์— ์—๋””ํŠธ ๋กœ๊ทธ๋ฅผ<NAME>๊ณ  ํŽœ์‹ฑ์„ ์ด์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋„ค์ž„๋…ธ๋“œ๋งŒ ์—๋””ํŠธ ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ  HDFS High Availability With QJM HDFS High Availability With NFS ํ•˜๋‘ก ๋„ค์ž„๋…ธ๋“œ ์ด์ค‘ํ™” ์‹œ fencing์˜ ์—ญํ•  04-HDFS ์„ธ์ดํ”„ ๋ชจ๋“œ HDFS์˜ ์„ธ์ดํ”„ ๋ชจ๋“œ(safemode)๋Š” ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์„ธ์ดํ”„ ๋ชจ๋“œ๊ฐ€ ๋˜๋ฉด ๋ฐ์ดํ„ฐ๋Š” ์ฝ๊ธฐ ์ „์šฉ ์ƒํƒœ๊ฐ€ ๋˜๊ณ , ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€์™€ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ ๋ณต์ œ๋„ ์ผ์–ด๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ด€๋ฆฌ์ž๊ฐ€ ์„œ๋ฒ„ ์šด์˜ ์ •๋น„๋ฅผ ์œ„ํ•ด ์„ธ์ดํ”„ ๋ชจ๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋„ค์ž„๋…ธ๋“œ์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒจ์„œ ์ •์ƒ์ ์ธ ๋™์ž‘์„ ํ•  ์ˆ˜ ์—†์„ ๋•Œ ์ž๋™์œผ๋กœ ์„ธ์ดํ”„ ๋ชจ๋“œ๋กœ ์ „ํ™˜๋ฉ๋‹ˆ๋‹ค. ์„ธ์ดํ”„ ๋ชจ๋“œ์—์„œ ํŒŒ์ผ ์ˆ˜์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์˜ค๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. $ hadoop fs -put ./sample.txt /user/sample.txt put: Cannot create file/user/sample2.txt._COPYING_. Name node is in safe mode. ์„ธ์ดํ”„ ๋ชจ๋“œ ์ปค๋งจ๋“œ ์„ธ์ดํ”„ ๋ชจ๋“œ ์ƒํƒœ์˜ ํ™•์ธ, ์ง„์ž…, ํ•ด์ œ ์ปค๋งจ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ์„ธ์ดํ”„ ๋ชจ๋“œ ์ƒํƒœ ํ™•์ธ $ hdfs dfsadmin -safemode get Safe mode is OFF # ์„ธ์ดํ”„ ๋ชจ๋“œ ์ง„์ž… $ hdfs dfsadmin -safemode enter Safe mode is ON # ์„ธ์ดํ”„ ๋ชจ๋“œ ํ•ด์ œ $ hdfs dfsadmin -safemode leave Safe mode is OFF ์„ธ์ดํ”„ ๋ชจ๋“œ์˜ ๋ณต๊ตฌ HDFS ์šด์˜ ์ค‘ ๋„ค์ž„๋…ธ๋“œ ์„œ๋ฒ„์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒจ์„œ ์„ธ์ดํ”„ ๋ชจ๋“œ์— ์ง„์ž…ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋„ค์ž„๋…ธ๋“œ ์ž์ฒด์˜ ๋ฌธ์ œ์™€ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์˜ ๋ฌธ์ œ์ผ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. fsck ๋ช…๋ น์œผ๋กœ HDFS์˜ ๋ฌด๊ฒฐ์„ฑ์„ ์ฒดํฌํ•˜๊ณ , hdfs dfsadmin -report ๋ช…๋ น์œผ๋กœ ๊ฐ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ™•์ธํ•˜๊ณ  ํ•ด๊ฒฐํ•œ ํ›„ ์„ธ์ดํ”„ ๋ชจ๋“œ๋ฅผ ํ•ด์ œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 05-HDFS ๋ฐ์ดํ„ฐ ๋ธ”๋ก ๊ด€๋ฆฌ HDFS ์šด์˜ ์ค‘ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋ฉด, ๋ฐ์ดํ„ฐ ๋ธ”๋ก์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปค๋ŸฝํŠธ(CORRUPT) ์ƒํƒœ์™€ ๋ณต์ œ ๊ฐœ์ˆ˜ ๋ถ€์กฑ(Under replicated) ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ปค๋ŸฝํŠธ ๋ธ”๋ก HDFS๋Š” ํ•˜ํŠธ๋น„ํŠธ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ๋ธ”๋ก์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒƒ์„ ๊ฐ์ง€ํ•˜๊ณ  ์ž๋™์œผ๋กœ ๋ณต๊ตฌ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์— ๋ณต์ œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋ณต๊ตฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋“  ๋ณต์ œ ๋ธ”๋ก์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒจ์„œ ๋ณต๊ตฌํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋˜๋ฉด ์ปค๋ŸฝํŠธ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ปค๋ŸฝํŠธ ์ƒํƒœ์˜ ํŒŒ์ผ๋“ค์€ ์‚ญ์ œํ•˜๊ณ , ์›๋ณธ ํŒŒ์ผ์„ ๋‹ค์‹œ HDFS์— ์˜ฌ๋ ค์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ปค๋ŸฝํŠธ ์ƒํƒœ ํ™•์ธ HDFS ์ปค๋งจ๋“œ์˜ fsck๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒํƒœ๋ฅผ ์ฒดํฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปค๋ŸฝํŠธ ์ƒํƒœ์˜ ๋ธ”๋ก๊ณผ ๋ณต์ œ ๊ฐœ์ˆ˜๊ฐ€ ๋ถ€์กฑํ•œ ๋ธ”๋ก์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๋ฃจํŠธ์˜ ์ƒํƒœ ์ฒดํฌ $ hdfs fsck / # /user/hadoop/ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ƒํƒœ ์ฒดํฌ $ hdfs fsck /user/hadoop/ Status: HEALTHY Total size: 1378743129 B Total dirs: 612 Total files: 2039 Total symlinks: 0 Total blocks (validated): 2039 (avg. block size 676185 B) Minimally replicated blocks: 2039 (100.0 %) Over-replicated blocks: 0 (0.0 %) Under-replicated blocks: 2039 (100.0 %) Mis-replicated blocks: 0 (0.0 %) Default replication factor: 2 Average block replication: 1.0 Corrupt blocks: 0 Missing replicas: 4004 (66.258484 %) Number of data-nodes: 1 Number of racks: 1 FSCK ended at Thu Dec 06 05:31:42 UTC 2018 in 37 milliseconds The filesystem under path '/user/hadoop' is HEALTHY ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๊ณ , ์ปค๋ŸฝํŠธ ์ƒํƒœ ์ด๋ฉด hdfs fsck -delete ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ์ปค๋ŸฝํŠธ ์ƒํƒœ์˜ ๋ธ”๋ก์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ์ปค๋ŸฝํŠธ ์ƒํƒœ์˜ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด์„œ ์ค‘์š”ํ•œ ํŒŒ์ผ์€ ๋ณต์ œ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ ค์ฃผ๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. hadoop fs -setrep ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. The filesystem under path '/user/hadoop' is CORRUPT # ์ปค๋ŸฝํŠธ ์ƒํƒœ์˜ ํŒŒ์ผ ์‚ญ์ œ $ hdfs fsck -delete # /user/hadoop/์˜ ๋ณต์ œ ๊ฐœ์ˆ˜๋ฅผ 5๋กœ ์กฐ์ • $ hadoop fs -setrep 5 /user/hadoop/ # /user/hadoop/ ํ•˜์œ„์˜ ๋ชจ๋“  ํŒŒ์ผ์˜ ๋ณต์ œ ๊ฐœ์ˆ˜๋ฅผ ์กฐ์ • $ hadoop fs -setrep 5 -R /user/hadoop/ ๋ณต์ œ ๊ฐœ์ˆ˜ ๋ถ€์กฑ ์ƒํƒœ ๋ณต์ œ๊ฐœ์ˆ˜ ๋ถ€์กฑ ์ƒํƒœ๋Š” ํŒŒ์ผ์— ์ง€์ •๋œ ๋ณต์ œ ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ์ดํ„ฐ ๋ธ”๋ก์— ๋ณต์ œ๋˜์ง€ ์•Š์•˜์„ ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ๋ณต์ œ ๊ฐœ์ˆ˜๋ฅผ ๋งŽ์ด ์ง€์ •ํ–ˆ์„ ๋•Œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. fsck ๋ช…๋ น์œผ๋กœ ๋ณต์ œ ๊ฐœ์ˆ˜๊ฐ€ ๋ถ€์กฑํ•œ ํŒŒ์ผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณต์ œ๊ฐœ์ˆ˜๊ฐ€ ๋ถ€์กฑํ•œ ํŒŒ์ผ์€ ๋ณต์ œ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ ค์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. hadoop fs -setrep ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. # /user/hadoop/์˜ ๋ณต์ œ ๊ฐœ์ˆ˜๋ฅผ 5๋กœ ์กฐ์ • $ hadoop fs -setrep 5 /user/hadoop/ # /user/hadoop/ ํ•˜์œ„์˜ ๋ชจ๋“  ํŒŒ์ผ์˜ ๋ณต์ œ ๊ฐœ์ˆ˜๋ฅผ ์กฐ์ • $ hadoop fs -setrep 5 -R /user/hadoop/ 06-HDFS ํœด์ง€ํ†ต HDFS๋Š” ์‚ฌ์šฉ์ž์˜ ์‹ค์ˆ˜์— ์˜ํ•œ ํŒŒ์ผ ์‚ญ์ œ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํœด์ง€ํ†ต ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํœด์ง€ํ†ต ๊ธฐ๋Šฅ์ด ์„ค์ •๋˜๋ฉด HDFS์—์„œ ์‚ญ์ œํ•œ ํŒŒ์ผ์€ ๋ฐ”๋กœ ์‚ญ์ œ๋˜์ง€ ์•Š๊ณ , ๊ฐ ์‚ฌ์šฉ์ž์˜ ํ™ˆ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•„๋ž˜ ํœด์ง€ํ†ต ๋””๋ ‰ํ† ๋ฆฌ(/user/์œ ์ €๋ช…/.Trash)๋กœ ์ด๋™๋ฉ๋‹ˆ๋‹ค. ํœด์ง€ํ†ต ์•„๋ž˜์˜ ํŒŒ์ผ์€ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํœด์ง€ํ†ต ๋””๋ ‰ํ„ฐ๋ฆฌ๋Š” ์ง€์ •ํ•œ ๊ฐ„๊ฒฉ์œผ๋กœ ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ์œ ํšจ ๊ธฐ๊ฐ„์ด ๋งŒ๋ฃŒ๋˜๋ฉด ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ์‚ญ์ œ๋˜๋ฉด ํ•ด๋‹น ๋ธ”๋ก์„ ํ•ด์ œํ•˜๊ณ , ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํœด์ง€ํ†ต ์„ค์ • ํœด์ง€ํ†ต ๊ด€๋ จ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์„ค์ •๊ฐ’ ๋น„๊ณ  fs.trash.interval ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ญ์ œํ•˜๋Š” ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ(๋ถ„). 0์ด๋ฉด ํœด์ง€ํ†ต ๊ธฐ๋Šฅ์„ ๋”. fs.trash.checkpoint.interval ์ฒดํฌํฌ์ธํŠธ๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฐ„๊ฒฉ(๋ถ„). fs.trash.interval๊ณผ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์•„์•ผ ํ•จ. ์ฒดํฌ ํฌ์ธํ„ฐ๊ฐ€ ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์œ ํšจ๊ธฐ๊ฐ„์ด ์ง€๋‚œ ์ฒดํฌํฌ์ธํŠธ๋Š” ์‚ญ์ œ. ํœด์ง€ํ†ต ์„ค์ •๊ฐ’ core-site.xml์— ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. <property> <name>fs.trash.interval</name> <value>1440</value> </property> <property> <name>fs.trash.checkpoint.interval</name> <value>120</value> </property> ํœด์ง€ํ†ต ๋ช…๋ น # ํœด์ง€ํ†ต์„ ๋น„์›€. $ hadoop fs -expunge # ํœด์ง€ํ†ต์„ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ์‚ญ์ œ $ hadoop fs -rm -skipTrash /user/data/file ์ฐธ๊ณ  HDFS ํœด์ง€ํ†ต ์„ค์ •: ๋ฐ”๋กœ ๊ฐ€๊ธฐ 07-HDFS ๋ช…๋ น์–ด HDFS ์ปค๋งจ๋“œ๋Š” ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ, ์šด์˜์ž ์ปค๋งจ๋“œ, ๋””๋ฒ„๊ทธ ์ปค๋งจ๋“œ๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ชจ๋“œ๋งˆ๋‹ค ๋‹ค์–‘ํ•œ ์ปค๋งจ๋“œ๊ฐ€ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์šฉ ๋ฐ ์šด์˜์— ํ•„์ˆ˜์ ์ธ ๋ช‡ ๊ฐ€์ง€ ์ปค๋งจ๋“œ๋งŒ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ์ปค๋งจ๋“œ์˜ ๋ชฉ๋ก์€ HDFS Commands Guide1์„ ์ฐธ๊ณ ํ•˜์‹ญ์‹œ์˜ค. ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ ๋ชฉ๋ก dfs ์ปค๋งจ๋“œ dfs ์ปค๋งจ๋“œ ๋ช…๋ น์–ด cat text ls mkdir cp mv get put rm setrep test touchz stat setfacl getfacl count fsck ์ปค๋งจ๋“œ fsck ์ปค๋งจ๋“œ ๋ช…๋ น์–ด ์‚ฌ์šฉ๋ฒ• ์šด์˜์ž ์ปค๋งจ๋“œ ์šด์˜์ž ์ปค๋งจ๋“œ ๋ชฉ๋ก fsck ์ปค๋งจ๋“œ ๋ช…๋ น์–ด dfsadmin ์ปค๋งจ๋“œ ์‚ฌ์šฉ๋ฒ• ์ „์ฒด ๋ช…๋ น์–ด -report -safemode -triggerBlockReport fetchdt checknative ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ๋Š” hdfs, hadoop ์‰˜์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ถ€ ์ปค๋งจ๋“œ๋Š” hdfs ์‰˜์„ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฐ ์‰˜์˜ ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๋ฉฐ, ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # dfs ์ปค๋งจ๋“œ๋Š” ๋‘˜ ๋‹ค ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ $ hdfs dfs -ls $ hadoop fs -ls # fsck ์ปค๋งจ๋“œ๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ ์ถœ๋ ฅ $ hdfs fsck / $ hadoop fsck / # fs.defaultFS ์„ค์ •๊ฐ’ ํ™•์ธ $ hdfs getconf -confKey fs.defaultFS hdfs://127.0.0.1:8020 # ๋ช…๋ น์–ด๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•จ $ hadoop getconf Error: Could not find or load main class getconf ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ ๋ชฉ๋ก ์ปค๋งจ๋“œ ๋น„๊ณ  classpath Hadoop jar ํŒŒ์ผ์— ํ•„์š”ํ•œ ํด๋ž˜์Šค ํŒจ์Šค ์ถœ๋ ฅ dfs ํŒŒ์ผ ์‹œ์Šคํ…œ ์‰˜ ๋ช…๋ น์–ด fetchdt ๋„ค์ž„๋…ธ๋“œ์˜ ๋ธ๋ฆฌ๊ฒŒ์ด์…˜ ํ† ํฐ ํ™•์ธ fsck ํŒŒ์ผ ์‹œ์Šคํ…œ ์ƒํƒœ ์ฒดํฌ getconf ์„ค์ •๋œ Config ์ •๋ณด ํ™•์ธ groups ์‚ฌ์šฉ์ž์— ์„ค์ •๋œ ๊ทธ๋ฃน ์ •๋ณด ํ™•์ธ lsSnapshottableDir ์Šค๋ƒ…์ƒท์ด ๊ฐ€๋Šฅํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ชฉ๋ก ํ™•์ธ jmxget JMX ์ธํฌ๋ฉ”์ด์…˜ ํ™•์ธ oev Offline Edits Viewr, Edits ํŒŒ์ผ์˜ ์ƒํƒœ ํ™•์ธ oiv Offline Image Viewr, ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ์ƒํƒœ ํ™•์ธ(2.4 ์ด์ƒ) oiv_legacy oiv 2.4 ๋ฏธ๋งŒ์˜ ์ƒํƒœ ํ™•์ธ snapshotDiff HDFS ์Šค๋ƒ…์ƒท์˜ ์ƒํƒœ ํ™•์ธ version ๋ฒ„์ „ ํ™•์ธ ์ด ๋ช…๋ น์–ด ์ค‘์—์„œ ํ•„์ˆ˜์ ์ธ dfs์™€ fsck ์ปค๋งจ๋“œ์— ๋Œ€ํ•ด์„œ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. dfs ์ปค๋งจ๋“œ dfs ์ปค๋งจ๋“œ๋Š” ํŒŒ์ผ์‹œ์Šคํ…œ ์‰˜์„ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. dfs๋Š” hdfs dfs, hadoop fs, hadoop dfs ์„ธ ๊ฐ€์ง€ ํ˜•ํƒœ๋กœ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ๋ช…๋ น์–ด๋Š” ํŒŒ์ผ์‹œ์Šคํ…œ์‰˜ ๊ฐ€์ด๋“œ(๋ฐ”๋กœ ๊ฐ€๊ธฐ)๋ฅผ ํ™•์ธํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด์˜ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. $ hdfs dfs -ls Found 17 items drwxr-xr-x - hadoop hadoop 0 2018-11-30 06:15 datas $ hadoop fs -ls Found 17 items drwxr-xr-x - hadoop hadoop 0 2018-11-30 06:15 datas dfs ์ปค๋งจ๋“œ ๋ช…๋ น์–ด ๋ช…๋ น์–ด ๋น„๊ณ  cat ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ํ™•์ธ text ํ…์ŠคํŠธ<NAME>์œผ๋กœ ํŒŒ์ผ์„ ์ฝ์Œ, ์••์ถ• ํŒŒ์ผ๋„ ํ…์ŠคํŠธ<NAME>์œผ๋กœ ํ™•์ธ appendToFile ์ง€์ •ํ•œ ํŒŒ์ผ์— ๋‚ด์šฉ์„ ์ถ”๊ฐ€(append) checksum ํŒŒ์ผ์˜ ์ฒดํฌ ์„ฌ ํ™•์ธ chgrp ํŒŒ์ผ์˜ ๊ทธ๋ฃน ๋ณ€๊ฒฝ chmod ํŒŒ์ผ์˜ ๋ชจ๋“œ ๋ณ€๊ฒฝ chown ํŒŒ์ผ์˜ ์†Œ์œ ๊ถŒ ๋ณ€๊ฒฝ count ์ง€์ •ํ•œ ๊ฒฝ๋กœ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฐœ์ˆ˜, ํŒŒ์ผ ๊ฐœ์ˆ˜, ํŒŒ์ผ ์‚ฌ์ด์ฆˆ ํ™•์ธ df ํŒŒ์ผ์‹œ์Šคํ…œ์˜ ์šฉ๋Ÿ‰ ํ™•์ธ(๋ณต์ œ๋ณธ์„ ํฌํ•จํ•œ ์šฉ๋Ÿ‰) du ์ง€์ •ํ•œ ๊ฒฝ๋กœ์˜ ์šฉ๋Ÿ‰ ํ™•์ธ(๋‹จ์ผ ํŒŒ์ผ์˜ ์šฉ๋Ÿ‰) dus ์ง€์ •ํ•œ ๊ฒฝ๋กœ์˜ ํ•˜์œ„ ํด๋”๋ฅผ ํฌํ•จํ•œ ์šฉ๋Ÿ‰ ํ™•์ธ expunge ํœด์ง€ํ†ต์„ ๋น„์›€. ํŒŒ์ผ์„ ์˜๊ตฌํžˆ ์‚ญ์ œ. getfacl ๋””๋ ‰ํ„ฐ๋ฆฌ, ํŒŒ์ผ์˜ ACL(Access Control List) ํ™•์ธ setfacl ๋””๋ ‰ํ„ฐ๋ฆฌ, ํŒŒ์ผ์˜ ACL(Access Control List) ์„ค์ • getfattr ๋””๋ ‰ํ„ฐ๋ฆฌ, ํŒŒ์ผ์˜ ์ถ”๊ฐ€์ ์ธ ์†์„ฑ ํ™•์ธ setfattr ๋””๋ ‰ํ„ฐ๋ฆฌ, ํŒŒ์ผ์˜ ์ถ”๊ฐ€์ ์ธ ์†์„ฑ ์„ค์ • getmerge ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ์˜ ํŒŒ์ผ๋“ค์„ ๋กœ์ปฌ์˜ ํŒŒ์ผ๋กœ ๋จธ์ง€ find ์ฃผ์–ด์ง„ ํ‘œํ˜„์— ๋งž๋Š” ํŒŒ์ผ์„ ์กฐํšŒ ls ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ํŒŒ์ผ ์กฐํšŒ lsr ๋””๋ ‰ํ„ฐ๋ฆฌ ํ•˜์œ„์˜ ํŒŒ์ผ์„ ํฌํ•จํ•˜์—ฌ ์กฐํšŒ mkdir ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ copyFromLocal ๋กœ์ปฌ์˜ ํŒŒ์ผ์„ HDFS๋กœ ๋ณต์‚ฌ copyToLocal HDFS์˜ ํŒŒ์ผ์„ ๋กœ์ปฌ๋กœ ๋ณต์‚ฌ cp ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ๋กœ ํŒŒ์ผ์„ ๋ณต์‚ฌ moveFromLocal ๋กœ์ปฌ์˜ ํŒŒ์ผ์„ HDFS๋กœ ์ด๋™ moveToLocal HDFS์˜ ํŒŒ์ผ์„ ๋กœ์ปฌ๋กœ ์ด๋™ mv ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ์˜ ํŒŒ์ผ์„ ์ด๋™, ํŒŒ์ผ ์ด๋ฆ„ ๋ณ€๊ฒฝ์—๋„ ์ด์šฉ get copyToLocal ๋ช…๋ น๊ณผ ๋น„์Šท put copyFromLocal ๋ช…๋ น๊ณผ ๋น„์Šท createSnapshot ์Šค๋ƒ…์ƒท ์ƒ์„ฑ deleteSnapshot ์Šค๋ƒ…์ƒท ์‚ญ์ œ renameSnapshot ์Šค๋ƒ…์ƒท ์ด๋ฆ„ ๋ณ€๊ฒฝ rm ํŒŒ์ผ ์‚ญ์ œ rmdir ๋””๋ ‰ํ„ฐ๋ฆฌ ์‚ญ์ œ rmr ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ์˜ ํ•˜์œ„ ๊ฒฝ๋กœ๊นŒ์ง€ ์‚ญ์ œ setrep ๋ ˆํ”Œ๋ฆฌ์ผ€์ด์…˜ ํŒฉํ„ฐ ์„ค์ • stat ์ฃผ์–ด์ง„ ์˜ต์…˜์— ๋”ฐ๋ผ ํŒŒ์ผ์˜ ์ •๋ณด๋ฅผ ํ™•์ธ tail ํŒŒ์ผ์˜ ๋งˆ์ง€๋ง‰ 1kbyte๋ฅผ ์ถœ๋ ฅ test ์ฃผ์–ด์ง„ ์˜ต์…˜์— ๋”ฐ๋ผ ํŒŒ์ผ, ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ touchz 0byte ํŒŒ์ผ ์ƒ์„ฑ truncate ์ฃผ์–ด์ง„ ํŒจํ„ด์— ๋”ฐ๋ผ ํŒŒ์ผ ์‚ญ์ œ usage ์ฃผ์–ด์ง„ ๋ช…๋ น์–ด ์‚ฌ์šฉ๋ฒ• ํ™•์ธ help ๋ช…๋ น์–ด ์‚ฌ์šฉ๋ฒ• ํ™•์ธ df๋Š” ๋ณต์ œ๊ฐœ์ˆ˜๋ฅผ ํฌํ•จํ•œ ์ „์ฒด์˜ ์šฉ๋Ÿ‰์„ ํ‘œํ˜„ํ•˜๊ณ , du ๋‹จ์ผ ํŒŒ์ผ ๋˜๋Š” ๊ฒฝ๋กœ์˜ ์šฉ๋Ÿ‰์ž…๋‹ˆ๋‹ค. 1M ํŒŒ์ผ์˜ ์šฉ๋Ÿ‰์„ du๋กœ ํ™•์ธํ•˜๋ฉด 1M์œผ๋กœ ๋‚˜์˜ค์ง€๋งŒ, df๋กœ ํŒŒ์ผ ์‹œ์Šคํ…œ์˜ ์šฉ๋Ÿ‰์„ ํ™•์ธํ•˜๋ฉด ๋ณต์ œ๊ฐœ์ˆ˜๊ฐ€ 3์ผ ๋•Œ 3M์œผ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ์ด ์ค‘์—์„œ ์ฃผ์š” ๋ช…๋ น์–ด์˜ ์‚ฌ์šฉ๋ฒ•์„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. cat ์ง€์ •ํ•œ ํŒŒ์ผ์„ ๊ธฐ๋ณธ ์ž…๋ ฅ์œผ๋กœ ์ฝ์–ด์„œ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. $ hadoop fs -cat /user/file.txt text ์ง€์ •ํ•œ ํŒŒ์ผ์„ ํ…์ŠคํŠธ<NAME>์œผ๋กœ ์ฝ์Šต๋‹ˆ๋‹ค. gzip, snappy ๋“ฑ์˜<NAME>์œผ๋กœ ์••์ถ•๋œ ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ํ…์ŠคํŠธ<NAME>์œผ๋กœ ์ถœ๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. $ hadoop fs -text /user/file.txt ls ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ์˜ ํŒŒ์ผ ๋ชฉ๋ก์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์˜ต์…˜: -h, -R,-u $ hadoop fs -ls /user/ # ์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ํŒŒ์ผ ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฉ”๊ฐ€, ๊ธฐ๊ฐ€๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์ถœ๋ ฅ $ hadoop fs -ls -h /user/ # ํ•˜์œ„ ํด๋”๊นŒ์ง€ ์กฐํšŒ $ hadoop fs -ls -R /user/ # ์•ก์„ธ์Šค ์‹œ๊ฐ„์„ ์กฐํšŒ. ๊ธฐ๋ณธ ์„ค์ •์€ ์ƒ์„ฑ ์‹œ๊ฐ„. ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ ์‹œ๊ฐ„์„ ํ™•์ธํ•˜์—ฌ ํŒŒ์ผ ์ •๋ฆฌ ๊ฐ€๋Šฅ $ hadoop fs -ls -u /user/ mkdir ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์˜ต์…˜: -p $ hadoop fs -mkdir /user/folder # /user/folder1/folder2๋ฅผ ์ƒ์„ฑ, ์ƒ์œ„ ํด๋”๊ฐ€ ์—†์œผ๋ฉด ์ž๋™์œผ๋กœ ์ƒ์„ฑ $ hadoop fs -mkdir -p /user/folder1/folder2 cp HDFS ์ƒ์˜ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. $ hadoop fs -cp /user/data1.txt /user/data2.txt mv HDFS ์ƒ์˜ ํŒŒ์ผ์„ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. $ hadoop fs -mv /user/data1.txt /user/data2.txt get HDFS์˜ ํŒŒ์ผ์„ ๋กœ์ปฌ์— ๋ณต์‚ฌํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์˜ต์…˜: -f $ hadoop fs -get /user/data1.txt ./ # ๋™์ผํ•œ ์ด๋ฆ„์˜ ํŒŒ์ผ์ด ์กด์žฌํ•˜๋ฉด ๋ฎ์–ด์”€ $ hadoop fs -get -f /user/data1.txt ./ put ๋กœ์ปฌ์˜ ํŒŒ์ผ์„ HDFS์— ๋ณต์‚ฌํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์˜ต์…˜: -f $ hadoop fs -put ./data1.txt /user/ # ๋™์ผํ•œ ์ด๋ฆ„์˜ ํŒŒ์ผ์ด ์กด์žฌํ•˜๋ฉด ๋ฎ์–ด์”€ $ hadoop fs -put -f ./data1.txt /user/ rm HDFS์˜ ํŒŒ์ผ์„ ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์˜ต์…˜: -r, -spkipTrash $ hadoop fs -rm /user/data1.txt # ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ํฌํ•จํ•˜์—ฌ ํ•˜์œ„์˜ ๋ชจ๋“  ํŒŒ์ผ์„ ์‚ญ์ œ, ๋””๋ ‰ํ„ฐ๋ฆฌ ์‚ญ์ œ ์‹œ ํ•„์š”ํ•จ $ hadoop fs -rm -r /user/ # ์“ฐ๋ ˆ๊ธฐํ†ต์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํŒŒ์ผ ์‚ญ์ œ $ hadoop fs -skipTrash /user/ setrep ๋””๋ ‰ํ„ฐ๋ฆฌ, ํŒŒ์ผ์˜ ๋ ˆํ”Œ๋ฆฌ์ผ€์ด์…˜ ํŒฉํ„ฐ๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์˜ต์…˜: -R # ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋ณต์ œ๊ฐœ์ˆ˜๋ฅผ 5๋กœ ์„ค์ • $ hadoop fs -setrep 5 /user/ # ํ•˜์œ„์˜ ๋ชจ๋“  ๋””๋ ‰ํ„ฐ๋ฆฌ, ํŒŒ์ผ์˜ ๋ณต์ œ๊ฐœ์ˆ˜๋ฅผ 5๋กœ ์„ค์ • $ hadoop fs -setrep 5 -R /user/ test ํŒŒ์ผ, ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # -d: ๊ฒฝ๋กœ๊ฐ€ ๋””๋ ‰ํ„ฐ๋ฆฌ์ด๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. # -e: ๊ฒฝ๋กœ๊ฐ€ ์žˆ์œผ๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. # -f: ๊ฒฝ๋กœ๊ฐ€ ํŒŒ์ผ์ด๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. # -s: ๊ฒฝ๋กœ๊ฐ€ ๋น„์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. # -w: ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๊ณ  ์“ฐ๊ธฐ ๊ถŒํ•œ์ด ๋ถ€์—ฌ๋œ ๊ฒฝ์šฐ 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. # -r: ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๊ณ  ์ฝ๊ธฐ ๊ถŒํ•œ์ด ๋ถ€์—ฌ๋œ ๊ฒฝ์šฐ 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. # -z: ํŒŒ์ผ ๊ธธ์ด๊ฐ€ 0์ด๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. # /user/test.txt ํŒŒ์ผ์ด ์กด์žฌํ•˜๋ฉด 0์„ ๋ฐ˜ํ™˜ $ hadoop fs -test -e /user/test.txt touchz 0byte ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. $ hadoop fs -touchz /user/test.txt stat ์ฃผ์–ด์ง„ ํฌ๋งท์— ๋”ฐ๋ฅธ ํŒŒ์ผ์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # ์ฃผ์š” ํฌ๋งท # %y : ๋งˆ์ง€๋ง‰ ์ˆ˜์ • ์‹œ๊ฐ„ # %x : ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ ์‹œ๊ฐ„ # %n : ํŒŒ์ผ ์ด๋ฆ„ # %b : ํŒŒ์ผ ์‚ฌ์ด์ฆˆ (byte) $ hadoop fs -stat "%y %n" hdfs://127.0.0.1:8020/* setfacl ํŒŒ์ผ์˜ ACL์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ls ๋ช…๋ น์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ์˜ ๊ถŒํ•œ๊ณผ ๋ณ„๋„๋กœ ๊ถŒํ•œ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # -m: ๊ถŒํ•œ์„ ์ˆ˜์ • # -b: ์„ค์ •ํ•œ ๊ถŒํ•œ์„ ์‚ญ์ œ # user a์—๊ฒŒ /user/file์˜ ์ฝ๊ธฐ(r), ์“ฐ๊ธฐ(w) ๊ถŒํ•œ์„ ์คŒ $ hadoop fs -setfacl -m user:a:rw- /user/file # /user/file์—์„œ ์‹ ๊ทœ๋กœ ์ƒ์„ฑ๋˜๋Š” ํŒŒ์ผ, ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋Œ€ํ•˜์—ฌ user a์—๊ฒŒ์˜ ์ฝ๊ธฐ(r), ์“ฐ๊ธฐ(w) ๊ถŒํ•œ์„ ์คŒ $ hadoop fs -setfacl -m default:user:a:rw- /user/file getfacl ํŒŒ์ผ์˜ ACL์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. $ hadoop fs -getfacl /user/file # file: hdfs:///user/file # owner: c # group: g user::rw- user:a:rw- group::r-- mask::rw- other::--- count ์ง€์ •ํ•œ ๊ฒฝ๋กœ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฐœ์ˆ˜, ํŒŒ์ผ ๊ฐœ์ˆ˜, ํŒŒ์ผ ์‚ฌ์ด์ฆˆ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. -q, -v ์˜ต์…˜๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฐœ์ˆ˜ 150 # ํŒŒ์ผ ๊ฐœ์ˆ˜ 2000 # ๋””๋ ‰ํ„ฐ๋ฆฌ ์šฉ๋Ÿ‰ 123456789 $ hadoop fs -count /user 150 2000 123456789 hdfs:///user # -h ์‚ฌ์šฉ์ž๊ฐ€ ์ฝ๊ธฐ ํŽธํ•˜๊ฒŒ ๋ณ€ํ™˜ # -q ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ฟผํ„ฐ ์ •๋ณด ํ™•์ธ: QUOTA, REMAINING_QUOTA, SPACE_QUOTA, REMAINING_SPACE_QUOTA, DIR_COUNT, FILE_COUNT, CONTENT_SIZE, PATHNAME # -u ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ฟผํ„ฐ ์ •๋ณด ํ™•์ธ: QUOTA, REMAINING_QUOTA, SPACE_QUOTA, REMAINING_SPACE_QUOTA, PATHNAME # -v๋Š” -q, -u์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ํ—ค๋”๋ฅผ ํ•จ๊ป˜ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. $ hadoop fs -count -q -v hdfs:///user/sample QUOTA REM_QUOTA SPACE_QUOTA REM_SPACE_QUOTA DIR_COUNT FILE_COUNT CONTENT_SIZE PATHNAME 8000000 471204 109951162777600 99303499783522 286862 7241934 3549220998026 hdfs:///user/sample fsck ์ปค๋งจ๋“œ fsck ์ปค๋งจ๋“œ๋Š” HDFS ํŒŒ์ผ์‹œ์Šคํ…œ์˜ ์ƒํƒœ๋ฅผ ์ฒดํฌํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. fsck ์ปค๋งจ๋“œ๋Š” ํŒŒ์ผ์‹œ์Šคํ…œ์— ๋ธ”๋ก ์ƒํƒœ ํ™•์ธ, ํŒŒ์ผ์˜ ๋ณต์ œ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋„ค์ž„๋…ธ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ์ƒํƒœ๋ฅผ ๋ณต๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— fsck ์ปค๋งจ๋“œ๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ํ™•์ธํ•ด๋„ ์ƒํƒœ๋ฅผ ์ •์ •ํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. fsck ์ปค๋งจ๋“œ ๋ช…๋ น์–ด ์ปค๋งจ๋“œ ๋น„๊ณ  path ์ฒดํฌ๋ฅผ ์œ„ํ•œ ๊ฒฝ๋กœ -list-corruptfileblocks ์ปค๋ŸฝํŠธ ์ƒํƒœ์˜ ๋ธ”๋ก์„ ์ถœ๋ ฅ -delete ์ปค๋ŸฝํŠธ ํŒŒ์ผ ์‚ญ์ œ -move ์ปค๋ŸฝํŠธ ๋ธ”๋ก์„ /lost+found ํด๋”๋กœ ์ด๋™ -files ์ฒดํฌํ•œ ํŒŒ์ผ ์ถœ๋ ฅ -files -blocks ๋ธ”๋ก ๋ฆฌํฌํŠธ ์ถœ๋ ฅ -files -blocks -locations ๋ธ”๋ก์˜ ์œ„์น˜๋ฅผ ์ถœ๋ ฅ -files -blocks -racks ๋ธ”๋ก์˜ ๋ž™ ์ •๋ณด ์ถœ๋ ฅ -files -blocks -replicaDetails ๋ณต์ œ ๊ฐœ์ˆ˜ ์ •๋ณด ์ถœ๋ ฅ -files -blocks -upgradedomains ๋ธ”๋ก์˜ ๋„๋ฉ”์ธ์„ ๊ฐฑ์‹  -includeSnapshots ์Šค๋ƒ…์ƒท์„ ํฌํ•จํ•ด์„œ ์ฒดํฌ -openforwrite ์“ฐ๊ธฐ ์ž‘์—…์„ ์œ„ํ•ด ์—ด๋ฆฐ ํŒŒ์ผ ์ถœ๋ ฅ -storagepolicies ๋ธ”๋ก์˜ ์ €์žฅ ์ •์ฑ… ์ถœ๋ ฅ -maintenance ๋ธ”๋ก์˜ ๊ด€๋ฆฌ ์ •๋ณด ์ถœ๋ ฅ -blockId ๋ธ”๋ก ID ์ถœ๋ ฅ ์‚ฌ์šฉ๋ฒ• fsck ์ปค๋งจ๋“œ๋Š” ์šฐ์„  ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ์‹œ์Šคํ…œ์˜ ์ƒํƒœ๋ฅผ ์ฒดํฌํ•  ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๊ณ  ํ•„์š”ํ•œ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. fsck ์ปค๋งจ๋“œ๋กœ ํŒŒ์ผ ์‹œ์Šคํ…œ์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๊ณ , -delete ์ปค๋งจ๋“œ๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋‚œ ํŒŒ์ผ์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ hdfs fsck <path> [-list-corruptfileblocks [-move | -delete | -openforwrite] [-files [-blocks [-locations | -racks]]]] [-includeSnapshots] [-storagepolicies] [-blockId <blk_Id>] $ hdfs fsck / $ hdfs fsck /user/hadoop/ $ hdfs fsck /user -list-corruptfileblocks $ hdfs fsck /user -delete $ hdfs fsck /user -files $ hdfs fsck /user -files -blocks $ hdfs fsck / Status: HEALTHY Total size: 7683823089 B Total dirs: 3534 Total files: 14454 Total symlinks: 0 Total blocks (validated): 14334 (avg. block size 536055 B) Minimally replicated blocks: 14334 (100.0 %) Over-replicated blocks: 0 (0.0 %) Under-replicated blocks: 14334 (100.0 %) Mis-replicated blocks: 0 (0.0 %) Default replication factor: 2 Average block replication: 1.0 Corrupt blocks: 0 Missing replicas: 31288 (68.58095 %) Number of data-nodes: 1 Number of racks: 1 FSCK ended at Fri Dec 28 04:07:32 UTC 2018 in 172 milliseconds ์šด์˜์ž ์ปค๋งจ๋“œ ์šด์˜์ž ์ปค๋งจ๋“œ๋„ hdfs, hadoop ์‰˜์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ถ€ ์ปค๋งจ๋“œ๋Š” hdfs ์‰˜์„ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฐ ์‰˜์˜ ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๋ฉฐ, ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ๋‘˜ ๋‹ค balancer๋ฅผ ์‹คํ–‰ $ hdfs balancer $ hadoop balancer ์šด์˜์ž ์ปค๋งจ๋“œ ๋ชฉ๋ก ์šด์˜์ž ์ปค๋งจ๋“œ๋Š” ์ฃผ๋กœ ์‹คํ–‰, ์„ค์ • ๊ด€๋ จ ๋ช…๋ น์–ด๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ช…๋ น์–ด์˜ ์ฃผ์š” ์˜ต์…˜์€ Administration Commands2๋ฅผ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ปค๋งจ๋“œ ๋น„๊ณ  namenode ๋„ค์ž„๋…ธ๋“œ ์‹คํ–‰ datanode ๋ฐ์ดํ„ฐ ๋…ธ๋“œ ์‹คํ–‰ secondarynamenode ์„ธ์ปจ๋”๋ฆฌ ๋„ค์ž„๋…ธ๋“œ ์‹คํ–‰ balancer HDFS ๋ฐธ๋ ์‹ฑ ์ฒ˜๋ฆฌ cacheadmin ์ž์ฃผ ์ฝ๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์บ์‹œ ์ฒ˜๋ฆฌ crypto ์•”ํ˜ธํ™” ์ฒ˜๋ฆฌ dfsadmin HDFS ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ Admin ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ช…๋ น dfsrouter HDFS ์—ฐํ•ฉ ๋ผ์šฐํŒ… ์‹คํ–‰ dfsrouteradmin ๋ฐ์ดํ„ฐ ๋…ธ๋“œ ๋ผ์šฐํŒ… ์„ค์ • haadmin HA ์‹คํ–‰ ๋ช…๋ น์–ด(QJM ๋˜๋Š” NFS) journalnode QJM์„ ์ด์šฉํ•œ HA, ์ €๋„๋…ธ๋“œ์šฉ ๋ช…๋ น์–ด mover ๋ฐ์ดํ„ฐ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์šฉ ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ช…๋ น์–ด nfs3 NFS3 ๊ฒŒ์ดํŠธ์›จ์ด ๋ช…๋ น์–ด portmap NFS3 ๊ฒŒ์ดํŠธ์›จ์ด ํฌํŠธ ๋งต ๋ช…๋ น์–ด storagepolicies HDFS ์ €์žฅ ์ •์ฑ… ์„ค์ • ๋ช…๋ น์–ด zkfc ์ฃผํ‚คํผ FailOver ์ปจํŠธ๋กค๋Ÿฌ ์‹คํ–‰ fsck ์ปค๋งจ๋“œ ๋ช…๋ น์–ด HDFS์˜ ์ƒํƒœ๋ฅผ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค. ์ปค๋ŸฝํŠธ ํŒŒ์ผ, ์–ธ๋” ๋ ˆํ”Œ๋ฆฌ์ผ€์ดํŠธ ์ƒํƒœ์˜ ํŒŒ์ผ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # ๋ฃจํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ถ€ํ„ฐ hdfs์˜ ์ƒํƒœ๋ฅผ ์ฒดํฌ $ hdfs fsck / dfsadmin ์ปค๋งจ๋“œ dfsadmin ์ปค๋งจ๋“œ๋Š” hdfs์˜ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์„ค์ • ๋ฐ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฟผํ„ฐ(Quota) ์„ค์ •, ๋…ธ๋“œ๋“ค์˜ ๋ฆฌํ”„๋ ˆ์‹œ, ๋…ธ๋“œ๋“ค์˜ ๋™์ž‘ ๋ฐ ์ •์ง€๋“ฑ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์š” ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ๋ฒ• dfsadmin ์ปค๋งจ๋“œ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ๋ช…๋ น์–ด์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ช…๋ น์–ด $ hdfs dfsadmin Usage: hdfs dfsadmin Note: Administrative commands can only be run as the HDFS superuser. [-report [-live] [-dead] [-decommissioning]] [-safemode <enter | leave | get | wait>] [-saveNamespace] [-rollEdits] [-restoreFailedStorage true|false|check] [-refreshNodes] [-setQuota <quota> <dirname>...<dirname>] [-clrQuota <dirname>...<dirname>] [-setSpaceQuota <quota> [-storageType <storagetype>] <dirname>...<dirname>] [-clrSpaceQuota [-storageType <storagetype>] <dirname>...<dirname>] [-finalizeUpgrade] [-rollingUpgrade [<query|prepare|finalize>]] [-refreshServiceAcl] [-refreshUserToGroupsMappings] [-refreshSuperUserGroupsConfiguration] [-refreshCallQueue] [-refresh <host:ipc_port> <key> [arg1.. argn] [-reconfig <datanode|...> <host:ipc_port> <start|status>] [-printTopology] [-refreshNamenodes datanode_host:ipc_port] [-deleteBlockPool datanode_host:ipc_port blockpoolId [force]] [-setBalancerBandwidth <bandwidth in bytes per second>] [-fetchImage <local directory>] [-allowSnapshot <snapshotDir>] [-disallowSnapshot <snapshotDir>] [-shutdownDatanode <datanode_host:ipc_port> [upgrade]] [-getDatanodeInfo <datanode_host:ipc_port>] [-metasave filename] [-triggerBlockReport [-incremental] <datanode_host:ipc_port>] [-help [cmd]] -report HDFS์˜ ๊ฐ ๋…ธ๋“œ๋“ค์˜ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. HDFS์˜ ์ „์ฒด ์‚ฌ์šฉ๋Ÿ‰๊ณผ ๊ฐ ๋…ธ๋“œ์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Configured Capacity: ๊ฐ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์—์„œ HDFS์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ• ๋‹น๋œ ์šฉ๋Ÿ‰ Present Capacity: HDFS์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์šฉ๋Ÿ‰ Configured Capacity์—์„œ Non DFS Used ์šฉ๋Ÿ‰์„ ๋บ€ ์‹ค์ œ ๋ฐ์ดํ„ฐ ์ €์žฅ์— ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์šฉ๋Ÿ‰ DFS Remaining: HDFS์—์„œ ๋‚จ์€ ์šฉ๋Ÿ‰ DFS Used: HDFS์— ์ €์žฅ๋œ ์šฉ๋Ÿ‰ Non DFS Used: ๋งต๋ฆฌ๋“€์Šค ์ž„์‹œ ํŒŒ์ผ, ์ž‘์—… ๋กœ๊ทธ ๋“ฑ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์— ์ €์žฅ๋œ ๋ธ”๋ก ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹Œ ํŒŒ์ผ์˜ ์šฉ๋Ÿ‰ Xceivers: ํ˜„์žฌ ์ž‘์—… ์ค‘์ธ ๋ธ”๋ก์˜ ๊ฐœ์ˆ˜ $ hdfs dfsadmin -report Configured Capacity: 165810782208 (154.42 GB) Present Capacity: 152727556096 (142.24 GB) DFS Remaining: 140297670656 (130.66 GB) DFS Used: 12429885440 (11.58 GB) DFS Used%: 8.14% Under replicated blocks: 18861 Blocks with corrupt replicas: 0 Missing blocks: 0 Missing blocks (with replication factor 1): 0 ------------------------------------------------- Live datanodes (1): Name: x.x.x.x:50010 (data_node) Hostname: data_node Decommission Status : Normal Configured Capacity: 165810782208 (154.42 GB) DFS Used: 12429885440 (11.58 GB) Non DFS Used: 13083226112 (12.18 GB) DFS Remaining: 140297670656 (130.66 GB) DFS Used%: 7.50% DFS Remaining%: 84.61% Configured Cache Capacity: 0 (0 B) Cache Used: 0 (0 B) Cache Remaining: 0 (0 B) Cache Used%: 100.00% Cache Remaining%: 0.00% Xceivers: 2 Last contact: Thu Apr 25 08:29:50 UTC 2019 -safemode ์„ธ์ดํ”„ ๋ชจ๋“œ์— ์ง„์ž…ํ•˜๊ณ  ๋น ์ ธ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ต์…˜ ๋น„๊ณ  get ์„ธ์ดํ”„ ๋ชจ๋“œ ์ƒํƒœ๋ฅผ ํ™•์ธ enter ์„ธ์ดํ”„ ๋ชจ๋“œ ์ง„์ž… leave ์„ธ์ดํ”„ ๋ชจ๋“œ ๋ณต๊ตฌ wait ์„ธ์ดํ”„ ๋ชจ๋“œ์ด๋ฉด ๋Œ€๊ธฐํ•˜๋‹ค๊ฐ€, ์„ธ์ดํ”„ ๋ชจ๋“œ๊ฐ€ ๋๋‚˜๋ฉด ํšŒ๋ณต $ hdfs dfsadmin -safemode get Safe mode is OFF $ hdfs dfsadmin -safemode enter Safe mode is ON $ hdfs dfsadmin -safemode get Safe mode is ON $ hdfs dfsadmin -safemode leave Safe mode is OFF $ hdfs dfsadmin -safemode wait Safe mode is OFF -triggerBlockReport ๋ฐ์ดํ„ฐ๋…ธ๋“œ์—๊ฒŒ ๋ธ”๋ก ๋ฆฌํฌํŠธ๋ฅผ ์ „๋‹ฌํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. $ hdfs dfsadmin -triggerBlockReport datanode_host_name:datanode_ipc_port fetchdt HDFS์— ์ ‘์†ํ•˜๊ธฐ ์œ„ํ•œ ๋ธ๋ฆฌ๊ฒŒ์ด์…˜ ํ† ํฐ์„ ํš๋“ํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์ž…๋‹ˆ๋‹ค. ์ปค๋ฒ„ ๋กœ์Šค ์ธ์ฆ์ด ์„ค์ •๋œ HDFS ํ™˜๊ฒฝ์—์„œ ์ปค๋ฒ„ ๋กœ์Šค ์ธ์ฆ์„ ํš๋“ํ•œ ํ›„ ํ† ํฐ์„ ๋ฐœ๊ธ‰๋ฐ›์•„์„œ ์ปค๋ฒ„ ๋กœ์Šค ์ธ์ฆ ์—†์ด HDFS์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ํ† ํฐ ํŒŒ์ผ๋กœ ๋ฐœ๊ธ‰ $ hdfs fetchdt token_file # ์ƒ์„ฑ๋œ ํŒŒ์ผ์„ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ์„ค์ • export HADOOP_TOKEN_FILE_LOCATION=[๊ฒฝ๋กœ]/token_file # ๋ฐ์ดํ„ฐ ์กฐํšŒ ์‹œ ํ† ํฐ ํŒŒ์ผ์„ ์ด์šฉํ•˜๊ฒŒ ๋จ hadoop fs -ls hdfs:///user checknative ์„œ๋ฒ„์— ์„ค์น˜๋œ ๋„ค์ดํ‹ฐ๋ธŒ ์••์ถ• ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. core-site.xml์˜ io.compression.codecs์— ์„ค์ •๋œ ์••์ถ• ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋„ค์ดํ‹ฐ๋ธŒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. $ hadoop checknative -a Native library checking: hadoop: true /opt/hadoop-2.10.0/lib/native/libhadoop.so.1.0.0 zlib: true /lib64/libz.so.1 snappy: true /lib64/libsnappy.so.1 zstd : true /lib64/libzstd.so.1 lz4: true revision:10301 bzip2: true /lib64/libbz2.so.1 openssl: false EVP_CIPHER_CTX_cleanup core-site.xml์˜ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. <configuration> <property> <name>io.compression.codecs</name> <value>org.apache.hadoop.io.compress.GzipCodec, org.apache.hadoop.io.compress.DefaultCodec, org.apache.hadoop.io.compress.SnappyCodec</value> </property> </configuration> HDFSCommands ๋งค๋‰ด์–ผ: ๋ฐ”๋กœ ๊ฐ€๊ธฐ โ†ฉ Administration Commands: ๋ฐ”๋กœ ๊ฐ€๊ธฐ โ†ฉ 08-WebHDFS REST API ์‚ฌ์šฉ๋ฒ• HDFS๋Š” REST API๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์กฐํšŒํ•˜๊ณ , ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜์—ฌ ์›๊ฒฉ์ง€์—์„œ HDFS์˜ ๋‚ด์šฉ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•œ ๋ช…๋ น์–ด๋Š” WebHDFS ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ํ™•์ธ ๋ฐ”๋ž๋‹ˆ๋‹ค. 1 REST API ์„ค์ • REST API๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” hdfs-site.xml์— ๋‹ค์Œ์˜ ์„ค์ •์ด ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. <property> <name>dfs.webhdfs.enabled</name> <value>true</value> </property> <property> <name>dfs.namenode.http-address</name> <value>0.0.0.0:50070</value> </property> ํŒŒ์ผ ๋ฆฌ์ŠคํŠธ ํ™•์ธ ์œ„์—์„œ ์„ค์ •ํ•œ http ํฌํŠธ๋กœ ์š”์ฒญ์„ ๋‚ ๋ฆฌ๋ฉด json<NAME>์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. curl ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # /user/hadoop/ ์œ„์น˜๋ฅผ ์กฐํšŒ $ curl -s http://127.0.0.1:50070/webhdfs/v1/user/hadoop/?op=LISTSTATUS https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/WebHDFS.html โ†ฉ 09-HDFS ์•”ํ˜ธํ™” HDFS๋Š” ๋ฏผ๊ฐ์ •๋ณด์˜ ๋ณด์•ˆ์„ ์œ„ํ•ด ์•”ํ˜ธํ™” ๊ธฐ๋Šฅ์„ ์ œ๊ณต 1 ํ•ฉ๋‹ˆ๋‹ค. ์•”ํ˜ธํ™”๋ฅผ ์ ์šฉํ•˜๋ฉด ๋””์Šคํฌ์— ์ €์žฅ๋˜๋Š” ํŒŒ์ผ์„ ์•”ํ˜ธํ™”ํ•˜์—ฌ ์ €์žฅํ•˜๊ณ , HDFS์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ ‘๊ทผํ•  ๋•Œ ํ•˜๋‘ก KMS๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‚ค ๊ธฐ๋ฐ˜์œผ๋กœ ์ „์†ก ๋ฐ์ดํ„ฐ์˜ ์•”/๋ณตํ˜ธํ™”๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์˜ˆ์ œ HDFS ์•”ํ˜ธํ™”๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ์•”ํ˜ธํ™” ํ‚ค ์ƒ์„ฑ $ hadoop key create mykey # zone ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑํ•˜๊ณ  ์•”ํ˜ธํ™” ์ง€์—ญ์œผ๋กœ ์„ค์ •, mykey๋ฅผ ์ด์šฉํ•˜๋„๋ก ์„ค์ • $ hadoop fs -mkdir /zone $ hdfs crypto -createZone -keyName mykey -path /zone # ํ‚ค ํ™•์ธ $ hadoop key list Listing keys for KeyProvider: org.apache.hadoop.crypto.key.kms.LoadBalancingKMSClientProvider mykey # ์•”ํ˜ธํ™” ์ง€์—ญ ํ™•์ธ $ hdfs crypto -listZones /zone sample_key # As the normal user, put a file in, read it out $ hadoop fs -put helloWorld /zone $ hadoop fs -cat /zone/helloWorld # As the normal user, get encryption information from the file $ hdfs crypto -getFileEncryptionInfo -path /zone/helloWorld console output: {cipherSuite: {name: AES/CTR/NoPadding, algorithmBlockSize: 16}, cryptoProtocolVersion: CryptoProtocolVersion{description='Encryption zones', version=1, unknownValue=null}, edek: 2010d301afbd43b58f10737ce4e93b39, iv: ade2293db2bab1a2e337f91361304cb3, keyName: mykey, ezKeyVersionName: mykey@0} ํ•˜๋‘ก KMS REST API ํ•˜๋‘ก KMS2๋Š” REST API ์„œ๋ฒ„๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. 9700 ํฌํŠธ๊ฐ€ ๊ธฐ๋ณธ ํฌํŠธ์ž…๋‹ˆ๋‹ค. curl http://$(hostname -f):9700/kms/v1/keys/names Transparent Encryption in HDFS โ†ฉ Hadoop Key Management Server(KMS) โ†ฉ 10-HDFS ์‚ฌ์šฉ๋Ÿ‰ ์ œํ•œ ์„ค์ • HDFS ๊ด€๋ฆฌ์ž๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณ„๋กœ ํŒŒ์ผ ๊ฐœ์ˆ˜์™€ ํŒŒ์ผ ์šฉ๋Ÿ‰์„ ์ œํ•œ 1 ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์„ค์ •์€ ๊ฐœ๋ณ„ ์ ์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜๋งŒ ์„ค์ •ํ•˜๊ฑฐ๋‚˜, ๋™์‹œ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ ๊ฐœ์ˆ˜ ์ œํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณ„๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ ๊ฐœ์ˆ˜๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ• ๋‹น๋Ÿ‰์„ ์ดˆ๊ณผํ•˜๋ฉด ํŒŒ์ผ, ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ ์šฉ๋Ÿ‰ ์ œํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณ„๋กœ ์šฉ๋Ÿ‰์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ ์šฉ๋Ÿ‰๋งŒ ํฌํ•จ๋˜๊ณ , ๋””๋ ‰ํ„ฐ๋ฆฌ๋Š” ์šฉ๋Ÿ‰์— ํฌํ•จ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ œํ•œ ์„ค์ • ๋ช…๋ น ํŒŒ์ผ ๊ฐœ์ˆ˜ ์ œํ•œ, ํŒŒ์ผ ์šฉ๋Ÿ‰ ์ œํ•œ์€ hdfs dfsadmin ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ํŒŒ์ผ ๊ฐœ์ˆ˜ ์ œํ•œ. ์ตœ๋Œ€ Long.MAX_VALUE ๋งŒํผ ์ œํ•œ ๊ฐ€๋Šฅ hdfs dfsadmin -setQuota <N> <directory>...<directory> # ํŒŒ์ผ ๊ฐœ์ˆ˜ ์ œํ•œ ์ดˆ๊ธฐํ™” hdfs dfsadmin -clrQuota <directory>...<directory> # ํŒŒ์ผ ์šฉ๋Ÿ‰ ์ œํ•œ. ์ตœ๋Œ€ Long.MAX_VALUE ๋งŒํผ ์ œํ•œ ๊ฐ€๋Šฅ hdfs dfsadmin -setSpaceQuota <N> <directory>...<directory> # ํŒŒ์ผ ์šฉ๋Ÿ‰ ์ œํ•œ ์ดˆ๊ธฐํ™” hdfs dfsadmin -clrSpaceQuota <directory>...<directory> ์ œํ•œ ๋ช…๋ น ํ™•์ธ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณ„๋กœ ์„ค์ •๋œ ์ œํ•œ์€ hadoop fs -count ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # quota ์„ค์ • ํ™•์ธ $ hadoop fs -count -q -v hdfs:///user/d1 QUOTA REM_QUOTA SPACE_QUOTA REM_SPACE_QUOTA DIR_COUNT FILE_COUNT CONTENT_SIZE PATHNAME 1000000 27300 10000000000 100000000 98337 1171599 12314 hdfs:///user/d1 # QUOTA, REMAINING_QUOTA, SPACE_QUOTA, REMAINING_SPACE_QUOTA, PATHNAME $ hadoop fs -count -u hdfs:///user/d1 1000000 273022 10000000000 80000 hdfs:///user/d1 # HDFS Quotas Guide โ†ฉ 11-๋ฐ์ดํ„ฐ ์••์ถ• ๋ฐ์ดํ„ฐ ์••์ถ• ์—ฌ๋ถ€์™€ ์‚ฌ์šฉํ•  ์••์ถ•<NAME>์€ ์„ฑ๋Šฅ์— ํฐ ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์••์ถ•์„ ๊ณ ๋ คํ•ด์•ผ ํ•  ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋‘ ๊ฐ€์ง€ ์žฅ์†Œ๋Š” MapReduce ์ž‘์—… ๋ฐ HBase์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ธก๋ฉด์ž…๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์›์น™์€ ์„œ๋กœ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์••์ถ• ๋ฐ ์••์ถ• ํ•ด์ œํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ์ฒ˜๋ฆฌ ์šฉ๋Ÿ‰, ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์“ฐ๋Š” ๋ฐ ํ•„์š”ํ•œ ๋””์Šคํฌ IO ๋ฐ ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๋Š” ๋ฐ ํ•„์š”ํ•œ ๋„คํŠธ์›Œํฌ ๋Œ€์—ญํญ์˜ ๊ท ํ˜•์„ ์œ ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์š”์†Œ์˜ ์˜ฌ๋ฐ”๋ฅธ ๊ท ํ˜•์€ ์‚ฌ์šฉ ํŒจํ„ด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํด๋Ÿฌ์Šคํ„ฐ ๋ฐ ๋ฐ์ดํ„ฐ์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ์ด๋ฏธ ์••์ถ•๋œ ๊ฒฝ์šฐ ์••์ถ•ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค (์˜ˆ : JPEG<NAME>์˜ ์ด๋ฏธ์ง€). ์‹ค์ œ๋กœ ๊ฒฐ๊ณผ ํŒŒ์ผ์€ ์‹ค์ œ๋กœ ์›๋ณธ๋ณด๋‹ค ํด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์••์ถ• ์œ ํ˜• GZIP ์••์ถ•์€ Snappy ๋˜๋Š” LZO๋ณด๋‹ค ๋งŽ์€ CPU ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๋” ๋†’์€ ์••์ถ•๋ฅ ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. GZip์€ ์ข…์ข… ์ฝœ๋“œ ๋ฐ์ดํ„ฐ์— ์ ํ•ฉํ•˜๋ฉฐ ๋“œ๋ฌผ๊ฒŒ ์•ก์„ธ์Šค ๋ฉ๋‹ˆ๋‹ค. Snappy ๋˜๋Š” LZO๋Š” ์ž์ฃผ ์•ก์„ธ์Šคํ•˜๋Š” ํ•ซ ๋ฐ์ดํ„ฐ์— ๋” ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. BZip2๋Š” ์••์ถ• ๋ฐ ์••์ถ• ํ•ด์ œ ์‹œ ์•ฝ๊ฐ„์˜ ์†๋„๋กœ ์ผ๋ถ€ ์œ ํ˜•์˜ ํŒŒ์ผ์— ๋Œ€ํ•ด GZip๋ณด๋‹ค ๋” ๋งŽ์€ ์••์ถ•์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. HBase๋Š” BZip2 ์••์ถ•์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Snappy๋Š” ์ข…์ข… LZO๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•ฉ๋‹ˆ๋‹ค. ์œ ์˜๋ฏธํ•œ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. MapReduce์˜ ๊ฒฝ์šฐ ์••์ถ• ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ BZip2, LZO ๋ฐ Snappy<NAME>์€ ๋ถ„ํ•  ๊ฐ€๋Šฅํ•˜์ง€๋งŒ GZip์€ ๋ถ„ํ• ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ถ„ํ• <NAME> HBase ๋ฐ์ดํ„ฐ์™€ ๊ด€๋ จ์ด ์—†์Šต๋‹ˆ๋‹ค. MapReduce์˜ ๊ฒฝ์šฐ ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ, ์ถœ๋ ฅ ๋˜๋Š” ๋‘˜ ๋‹ค๋ฅผ ์••์ถ•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. MapReduce ์ž‘์—…์— ์ œ๊ณต ํ•œ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์ ์ ˆํžˆ ์กฐ์ •ํ•˜์‹ญ์‹œ์˜ค. ๋‹ค์Œ ์˜ˆ์ œ๋Š” ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ์™€ ์ถœ๋ ฅ์„ ๋ชจ๋‘ ์••์ถ•ํ•ฉ๋‹ˆ๋‹ค. MR2๊ฐ€ ๋จผ์ € ํ‘œ์‹œ๋œ ๋‹ค์Œ MR1์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. hadoop jar hadoop-examples-.jar sort "-Dmapreduce.compress.map.output=true" "-Dmapreduce.map.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec" "-Dmapreduce.output.compress=true" "-Dmapreduce.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec" -outKey org.apache.hadoop.io.Text -outValue org.apache.hadoop.io.Text input output ์ฐธ๊ณ  Choosing a Data Compression Format 12-RPC HDFS๋Š” ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ ๊ฐ„ ํ†ต์‹ ์— RPC๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. RPC๋Š” Remote Procedure Call์€ ์›๊ฒฉ์ง€์— ์žˆ๋Š” ๋…ธ๋“œ์˜ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Hadoop RPC, IPC Protection Hadoop RPC๋ฅผ ์ด์šฉํ•œ ์„œ๋ฒ„/ํด๋ผ์ด์–ธํŠธ ๊ตฌํ˜„ 13-์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ(EC) HDFS์˜ ์ด๋ ˆ์ด์ € ์ฝ”๋”ฉ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ชฉ์  HDFS๋Š” 3x ๋ณต์ œ(์›๋ณธ 1๊ฐœ, ๋ณต์ œ๋ณธ 2๊ฐœ, ์ด 3๊ฐœ์˜ ํŒŒ์ผ ์œ ์ง€)๋ฅผ ์ด์šฉํ•ด์„œ ๋‚ด ๊ฒฐํ•จ์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ณต์ œ๋Š” ์ €์žฅ ๊ณต๊ฐ„, ๋„คํŠธ์›Œํฌ ๋Œ€์—ญํญ ๋“ฑ์—์„œ 200%์˜ ์˜ค๋ฒ  ํ—ค๋“œ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์œผ๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ์ƒ๋Œ€์ ์œผ๋กœ ๋‚ฎ์€ ์•ก์„ธ์Šค ํ™œ๋™์ด ์žˆ๋Š” ์ฝœ๋“œ ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ๊ฒฝ์šฐ ์ถ”๊ฐ€ ๋ธ”๋ก ๋ณต์ œ๋ณธ์€ ๊ฑฐ์˜ ์•ก์„ธ์Šค ๋˜์ง€ ์•Š์ง€๋งŒ ๋‹ค๋ฅธ ๋ณต์ œ๋ณธ๊ณผ ๋™์ผํ•œ ์–‘์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ์†Œ๋น„ํ•ฉ๋‹ˆ๋‹ค. EC(Erasure Coding)๋ฅผ ์ด์šฉํ•ด์„œ ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. EC๋Š” ํ›จ์”ฌ ์ ์€ ์ €์žฅ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ์ˆ˜์ค€์˜ ๋‚ด ๊ฒฐํ•จ์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ EC ์„ค์ •์—์„œ ์Šคํ† ๋ฆฌ์ง€ ์˜ค๋ฒ„ํ—ค๋“œ๋Š” 50% ์ดํ•˜์ž…๋‹ˆ๋‹ค. EC๋Š” ํŒŒ์ผ์˜ ๋ณต์ œ ๊ฐœ์ˆ˜๋Š” ํ•ญ์ƒ 1์ด๋ฉฐ setrep ๋ช…๋ น์„ ํ†ตํ•ด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ฐฐ๊ฒฝ ์Šคํ† ๋ฆฌ์ง€ ์‹œ์Šคํ…œ์—์„œ EC์˜ ์ฃผ์š” ์šฉ๋„๋Š” RAID ๊ตฌ์„ฑ์ž…๋‹ˆ๋‹ค. RAID๋Š” ์ŠคํŠธ๋ผ์ดํ•‘์„ ํ†ตํ•ด EC๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ŠคํŠธ๋ผ์ดํ•‘์€ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์ˆœ์ฐจ์ ์ธ ๋ฐ์ดํ„ฐ (์˜ˆ : ํŒŒ์ผ)๋ฅผ ๋” ์ž‘์€ ๋‹จ์œ„ (์˜ˆ : ๋น„ํŠธ, ๋ฐ”์ดํŠธ ๋˜๋Š” ๋ธ”๋ก)๋กœ ๋‚˜๋ˆ„๊ณ  ์—ฐ์†๋œ ์žฅ์น˜๋ฅผ ๋‹ค๋ฅธ ๋””์Šคํฌ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์›๋ณธ ๋ฐ์ดํ„ฐ ์…€์˜ ๊ฐ ์ŠคํŠธ๋ผ์ดํ”„์— ๋Œ€ํ•ด ํŠน์ • ์ˆ˜์˜ ํŒจ๋ฆฌํ‹ฐ ์…€ ์ด ๊ณ„์‚ฐ๋˜๊ณ  ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์„ ์ธ์ฝ”๋”ฉ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์ŠคํŠธ๋ผ์ดํ•‘ ์…€์˜ ์˜ค๋ฅ˜๋Š” ๋‚จ์•„์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋ฐ ํŒจ๋ฆฌํ‹ฐ ์…€์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ„์‚ฐ์„ ๋””์ฝ”๋”ฉ ํ•˜์—ฌ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. EC๋ฅผ HDFS์™€ ํ†ตํ•ฉํ•˜๋ฉด ๊ธฐ์กด ๋ณต์ œ ๊ธฐ๋ฐ˜ HDFS ๋ฐฐํฌ์™€ ์œ ์‚ฌํ•œ ๋ฐ์ดํ„ฐ ๋‚ด๊ตฌ์„ฑ์„ ์ œ๊ณตํ•˜๋ฉด์„œ ์Šคํ† ๋ฆฌ์ง€ ํšจ์œจ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 6 ๊ฐœ์˜ ๋ธ”๋ก์ด ์žˆ๋Š” 3x ๋ณต์ œ ํŒŒ์ผ์€ 6 * 3 = 18 ๋ธ”๋ก์˜ ๋””์Šคํฌ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ EC (6 ๋ฐ์ดํ„ฐ, 3 ํŒจ๋ฆฌํ‹ฐ) ๋ฐฐํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 9 ๋ธ”๋ก์˜ ๋””์Šคํฌ ๊ณต๊ฐ„ ๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ์กฐ EC์˜ ์ŠคํŠธ๋ผ์ดํ•‘์—๋Š” ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ์งธ, ์˜จ๋ผ์ธ EC (EC<NAME>์œผ๋กœ ์ฆ‰์‹œ ๋ฐ์ดํ„ฐ ์“ฐ๊ธฐ)๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜์—ฌ ๋ณ€ํ™˜ ๋‹จ๊ณ„๋ฅผ ํ”ผํ•˜๊ณ  ์ฆ‰์‹œ ์ €์žฅ ๊ณต๊ฐ„์„ ์ ˆ์•ฝํ•ฉ๋‹ˆ๋‹ค. ์˜จ๋ผ์ธ EC๋Š” ๋˜ํ•œ ์—ฌ๋Ÿฌ ๋””์Šคํฌ ์Šคํ•€๋“ค์„ ๋ณ‘๋ ฌ๋กœ ํ™œ์šฉํ•˜์—ฌ ์ˆœ์ฐจ I/O ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํ•˜์ด ์—”๋“œ ๋„คํŠธ์›Œํ‚น์ด ์žˆ๋Š” ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ํŠนํžˆ ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ๋‘˜์งธ, ์ž‘์€ ํŒŒ์ผ์„ ์—ฌ๋Ÿฌ DataNode์— ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ฐฐํฌํ•˜๊ณ  ์—ฌ๋Ÿฌ ํŒŒ์ผ์„ ๋‹จ์ผ ์ฝ”๋”ฉ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์„ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‚ญ์ œ, ํ• ๋‹น๋Ÿ‰ ๋ณด๊ณ  ๋ฐ ์—ฐํ•ฉ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค ๊ฐ„ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜๊ณผ ๊ฐ™์€ ํŒŒ์ผ ์ž‘์—…์ด ํฌ๊ฒŒ ๋‹จ์ˆœํ™”๋ฉ๋‹ˆ๋‹ค. ๋ฐฐํฌ ์ด๋ ˆ์ด์ € ์ฝ”๋”ฉ์€ CPU ๋ฐ ๋„คํŠธ์›Œํฌ ์ธก๋ฉด์—์„œ ํด๋Ÿฌ์Šคํ„ฐ์— ์ถ”๊ฐ€ ์š”๊ตฌ ์‚ฌํ•ญ์„ ๋ถ€๊ณผํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”ฉ ๋ฐ ๋””์ฝ”๋”ฉ ์ž‘์—…์€ HDFS ํด๋ผ์ด์–ธํŠธ์™€ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ ๋ชจ๋‘์—์„œ ์ถ”๊ฐ€ CPU๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ˆ์ด์ € ์ฝ”๋”ฉ์—๋Š” ๊ตฌ์„ฑ๋œ EC ์ŠคํŠธ๋ผ์ดํ”„ ๋„ˆ๋น„๋งŒํผ ํด๋Ÿฌ์Šคํ„ฐ์— ์ตœ์†Œํ•œ ๋งŽ์€ DataNode๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. EC ์ •์ฑ… RS (6,3)์˜ ๊ฒฝ์šฐ ์ด๋Š” ์ตœ์†Œ 9 ๊ฐœ์˜ DataNode๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์‚ญ์ œ ์ฝ”๋“œ ํŒŒ์ผ์€ ๋ž™ ๋‚ด ๊ฒฐํ•จ์„ฑ์„ ์œ„ํ•ด ๋ž™์— ๋ถ„์‚ฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ŠคํŠธ๋ผ์ดํ”„ ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ธ ๋•Œ ๋Œ€๋ถ€๋ถ„์˜ ์ž‘์—…์ด ๋ž™ ์™ธ๋ถ€์— ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋„คํŠธ์›Œํฌ ๋Œ€์—ญํญ์€ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ž™ ๋‚ด ๊ฒฐํ•จ์„ฑ์„ ์œ„ํ•ด ์ถฉ๋ถ„ํ•œ ์ˆ˜์˜ ๋ž™์„ ๊ฐ–๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ ํ‰๊ท ์ ์œผ๋กœ ๊ฐ ๋ž™์ด EC ํŒจ๋ฆฌํ‹ฐ ๋ธ”๋ก ์ˆ˜๋ณด๋‹ค ๋งŽ์€ ์ˆ˜์˜ ๋ธ”๋ก์„ ๋ณด์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ณต์‹์€ (๋ฐ์ดํ„ฐ ๋ธ”๋ก + ํŒจ๋ฆฌํ‹ฐ ๋ธ”๋ก) / ํŒจ๋ฆฌํ‹ฐ ๋ธ”๋ก์ด๋ฉฐ ๋ฐ˜์˜ฌ๋ฆผ๋ฉ๋‹ˆ๋‹ค. EC ์ •์ฑ… RS (6,3)์˜ ๊ฒฝ์šฐ ์ด๋Š” ์ตœ์†Œ 3 ๊ฐœ์˜ ๋ž™ ((6 + 3) / 3 = 3์œผ๋กœ ๊ณ„์‚ฐ), ์ด์ƒ์ ์œผ๋กœ๋Š” ๊ณ„ํš๋œ ์ค‘๋‹จ๊ณผ ๊ณ„ํš๋˜์ง€ ์•Š์€ ์ค‘๋‹จ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ 9๊ฐœ ์ด์ƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํŒจ๋ฆฌํ‹ฐ ์…€ ์ˆ˜๋ณด๋‹ค ๋ž™์ด ์ ์€ ํด๋Ÿฌ์Šคํ„ฐ์˜ ๊ฒฝ์šฐ HDFS๋Š” ๋ž™ ๋‚ด ๊ฒฐํ•จ์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์—†์ง€๋งŒ ๋…ธ๋“œ ์ˆ˜์ค€ ๋‚ด ๊ฒฐํ•จ์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ŠคํŠธ๋ผ์ดํ”„ ํŒŒ์ผ์„ ์—ฌ๋Ÿฌ ๋…ธ๋“œ์— ๋ถ„์‚ฐํ•˜๋ ค๊ณ  ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ์œ ์‚ฌํ•œ ์ˆ˜์˜ DataNode๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ž™์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ •์ฑ… ๊ตฌ์„ฑ ๊ธฐ๋ณธ์ ์œผ๋กœ dfs.namenode.ec.system.default.policy์— ์ •์˜๋œ ์ •์ฑ…์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋“  ์ด๋ ˆ์ด์ € ์ฝ”๋”ฉ ์ •์ฑ…์€ ๋น„ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ ๊ด€๋ฆฌ์ž๋Š” ํด๋Ÿฌ์Šคํ„ฐ ํฌ๊ธฐ ๋ฐ ์›ํ•˜๋Š” ๋‚ด ๊ฒฐํ•จ์„ฑ ์†์„ฑ์„ ๊ธฐ๋ฐ˜์œผ๋กœ hdfs ec [-enablePolicy -policy ] ๋ช…๋ น์„ ํ†ตํ•ด ์ •์ฑ…์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 9 ๊ฐœ์˜ ๋ž™์ด ์žˆ๋Š” ํด๋Ÿฌ์Šคํ„ฐ์˜ ๊ฒฝ์šฐ RS-10-4-1024k ์™€ ๊ฐ™์€ ์ •์ฑ…์€ ๋ž™ ์ˆ˜์ค€ ๋‚ด ๊ฒฐํ•จ์„ฑ์„ ์œ ์ง€ํ•˜์ง€ ์•Š์œผ๋ฉฐ RS-6-3-1024k ๋˜๋Š” RS-3-2-1024k ๊ฐ€ ๋” ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด€๋ฆฌ์ž๊ฐ€ ๋…ธ๋“œ ์ˆ˜์ค€ ๋‚ด ๊ฒฐํ•จ์„ฑ์—๋งŒ ๊ด€์‹ฌ ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ํด๋Ÿฌ์Šคํ„ฐ์— ์ตœ์†Œ 14 ๊ฐœ์˜ DataNode๊ฐ€ ์žˆ๋Š” ํ•œ RS-10-4-1024k๋Š” ์ ์ ˆํ•ฉ๋‹ˆ๋‹ค. ์‹œ์Šคํ…œ ๊ธฐ๋ณธ EC ์ •์ฑ…์€ 'dfs.namenode.ec.system.default.policy'๊ตฌ์„ฑ์„ ํ†ตํ•ด ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ตฌ์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด '-setPolicy'๋ช…๋ น์—์„œ ์ •์ฑ… ์ด๋ฆ„์ด ์ „๋‹ฌ๋˜์ง€ ์•Š์„ ๋•Œ ๊ธฐ๋ณธ EC ์ •์ฑ…์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ 'dfs.namenode.ec.system.default.policy'๋Š” "RS-6-3-1024k"์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋ณต๊ตฌ ์ž‘์—…์€ ๋‹ค์Œ ๊ตฌ์„ฑ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์กฐ์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. dfs.datanode.ec.reconstruction.stripedread.timeout.millis- ์ŠคํŠธ๋ผ์ดํ”„ ์ฝ๊ธฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ. ๊ธฐ๋ณธ๊ฐ’์€ 5000ms์ž…๋‹ˆ๋‹ค. dfs.datanode.ec.reconstruction.stripedread.buffer.size- ํŒ๋…๊ธฐ ์„œ๋น„์Šค์˜ ๋ฒ„ํผ ํฌ๊ธฐ. ๊ธฐ๋ณธ๊ฐ’์€ 64KB์ž…๋‹ˆ๋‹ค. dfs.datanode.ec.reconstruction.threads- ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์žฌ๊ตฌ์„ฑ ์ž‘์—…์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์Šค๋ ˆ๋“œ ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 8 ๊ฐœ ์Šค๋ ˆ๋“œ์ž…๋‹ˆ๋‹ค. dfs.datanode.ec.reconstruction.xmits.weight- ๋ณต์ œ๋œ ๋ธ”๋ก ๋ณต๊ตฌ์™€ ๋น„๊ตํ•˜์—ฌ EC ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋ณต๊ตฌ ์ž‘์—…์—์„œ ์‚ฌ์šฉํ•˜๋Š” xmit์˜ ์ƒ๋Œ€์  ๊ฐ€์ค‘์น˜์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 0.5์ž…๋‹ˆ๋‹ค. EC ๋ณต๊ตฌ ์ž‘์—…์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜ ๊ณ„์‚ฐ์„ ๋น„ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด 0์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, EC ์ž‘์—…์—๋Š” ํ•ญ์ƒ 1 ๊ฐœ์˜ xmit์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ญ์ œ ์ฝ”๋”ฉ ๋ณต๊ตฌ ์ž‘์—…์˜ xmits๋Š” ์ฝ๊ธฐ ์ŠคํŠธ๋ฆผ ์ˆ˜์™€ ์“ฐ๊ธฐ ์ŠคํŠธ๋ฆผ ์ˆ˜ ์‚ฌ์ด์˜ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด EC ๋ณต๊ตฌ ์ž‘์—…์ด 6 ๊ฐœ ๋…ธ๋“œ์—์„œ ์ฝ๊ณ  2 ๊ฐœ ๋…ธ๋“œ์— ์จ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ xmits๋Š” max (6, 2) * 0.5 = 3์ž…๋‹ˆ๋‹ค. ๋ณต์ œ๋œ ํŒŒ์ผ์— ๋Œ€ํ•œ ๋ณต๊ตฌ ์ž‘์—…์€ ํ•ญ์ƒ 1 xmit์œผ๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. NameNode๋Š” dfs.namenode.replication.max-streams์—์„œ ์ด xmitsInProgress๋ฅผ ๋บ€ ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ณต์ œ๋œ ํŒŒ์ผ๊ณผ EC ํŒŒ์ผ์˜ xmit์„ ๊ฒฐํ•ฉํ•˜๋Š” DataNode์—์„œ ์ด DataNode์— ๋Œ€ํ•œ ๋ณต๊ตฌ ์ž‘์—…์„ ์˜ˆ์•ฝํ•ฉ๋‹ˆ๋‹ค. CLI ๋ช…๋ น์–ด hdfs ec [generic options] [-setPolicy -path <path> [-policy <policyName>] [-replicate]] [-getPolicy -path <path>] [-unsetPolicy -path <path>] [-listPolicies] [-addPolicies -policyFile <file>] [-listCodecs] [-enablePolicy -policy <policyName>] [-disablePolicy -policy <policyName>] [-removePolicy -policy <policyName>] [-verifyClusterSetup -policy <policyName>...<policyName>] [-help [cmd ...]] setPolicy ์ •์ฑ…์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. getPolicy ์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ ์ ์ฑ…์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. unsetPolicy ์ •์ฑ… ์„ค์ •์„ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค. listPolicies HDFS ๊ฒฝ๋กœ์— ๋“ฑ๋ก๋œ ๋ชจ๋“  (ํ™œ์„ฑํ™”, ๋น„ํ™œ์„ฑํ™” ๋ฐ ์ œ๊ฑฐ๋œ) ์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ ์ •์ฑ…์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. addPolicies ์ด๋ ˆ์ด์ € ์ฝ”๋”ฉ ์ •์ฑ…์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. removePolicy ์ด๋ ˆ์ด์ € ์ฝ”๋”ฉ ์ •์ฑ…์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. listCodecs ์ด๋ ˆ์ด์ € ์ฝ”๋”ฉ ์ฝ”๋ฑ ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. enablePolicy ์ด๋ ˆ์ด์ € ์ฝ”๋”ฉ ์ •์ฑ…์„ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. disablePolicy ์ด๋ ˆ์ด์ € ์ฝ”๋”ฉ ์ •์ฑ…์„ ๋น„ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. verifyClusterSetup ํด๋Ÿฌ์Šคํ„ฐ ์„ค์ •์ด ํ™œ์„ฑํ™”๋œ ๋ชจ๋“  ์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ ์ •์ฑ…์„ ์ง€์›ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค CLI ์˜ˆ์ œ file.txt๋ฅผ ํ™•์ธํ•˜๋ฉด ๋ฐ์ดํ„ฐ ๋ธ”๋ก์„ ์–ด๋Š ์ •๋„ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ ์„ค์ • $ hdfs ec -setPolicy -path hdfs:///cold Set default erasure coding policy on hdfs:///cold # ๋ณต์ œ ์„ค์ • $ hadoop fs -du -v hdfs:///rep SIZE DISK_SPACE_CONSUMED_WITH_ALL_REPLICAS FULL_PATH_NAME 2005986464 6017959392 hdfs:///rep/file.txt # ์ด๋ ˆ์ด์ ธ ์ฝ”๋”ฉ ์ •์ฑ… ์„ค์ • $ hadoop fs -du -v hdfs:///ec SIZE DISK_SPACE_CONSUMED_WITH_ALL_REPLICAS FULL_PATH_NAME 2005986464 3009473696 hdfs:///ec/file.txt ์ฐธ๊ณ  HDFSErasureCoding 14-Rack Awareness Hadoop ๊ตฌ์„ฑ ์š”์†Œ๋Š” ๋ž™์„ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด HDFS ๋ธ”๋ก ๋ฐฐ์น˜๋Š” ํ•˜๋‚˜์˜ ๋ธ”๋ก ๋ณต์ œ๋ณธ์„ ์„œ๋กœ ๋‹ค๋ฅธ ๋ž™์— ๋ฐฐ์น˜ํ•˜๋Š” ๋‚ด ๊ฒฐํ•จ์„ฑ์„ ์œ„ํ•ด ๋ž™ ์ธ์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํด๋Ÿฌ์Šคํ„ฐ ๋‚ด์—์„œ ๋„คํŠธ์›Œํฌ ์Šค์œ„์น˜ ์žฅ์•  ๋˜๋Š” ํŒŒํ‹ฐ์…˜์ด ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ๊ฐ€์šฉ์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ž™ ์ธ์‹(Rack Awareness) ๊ตฌ์„ฑ ๋ž™ ์ธ์‹์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” core-site.xml์— ๋‹ค์Œ ์„ค์ •์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์„ค์ •๋œ ์Šคํฌ๋ฆฝํŠธ(์‰˜ ์Šคํฌ๋ฆฝํŠธ or ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ)๋Š” ๋…ธ๋“œ๊ฐ€ ์–ด๋–ค ๋ž™์ธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. <property> <name>net.topology.script.file.name</name> <value>/etc/hadoop/conf/topology.sh</value> </property> ์„ค์ •๋œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋„ค์ž„๋…ธ๋“œ, ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๊ฐ€ ์‹คํ–‰ํ•˜์—ฌ ๋…ธ๋“œ์˜ ๋ž™์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. # ์‹คํ–‰ ์˜ˆ์ œ, ๋ฐ˜ํ™˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ž™์ด ๋จ bash /etc/hadoop/conf/topology.sh 10.182.10.100 ๋งคํ•‘ ๋ฐ์ดํ„ฐ ์Šคํฌ๋ฆฝํŠธ ๋งคํ•‘ ๋ฐ์ดํ„ฐ ์Šคํฌ๋ฆฝํŠธ๋Š” topology.data ํŒŒ์ผ์„ ์ฝ์–ด์„œ ๋ž™์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. #!/bin/bash # Supply appropriate rack prefix RACK_PREFIX="default" # To test, supply a hostname as script input if [ $# -gt 0 ]; then HADOOP_CONF=${HADOOP_CONF:-"/etc/hadoop/conf"} while [ $# -gt 0 ] ; do nodeArg=$1 exec< ${HADOOP_CONF}/topology.data result="" while read line ; do ar=( $line) if [ "${ar[0]}" = "$nodeArg" ] ; then result="${ar[1]}" fi done shift if [ -z "$result" ] ; then echo -n "/$RACK_PREFIX/rack" else echo -n "/$RACK_PREFIX/rack_$result" fi done else echo -n "/$RACK_PREFIX/rack" fi 10.182.0.1 rack-1 10.182.0.2 rack-2 ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ ๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ๋Š” IP๋ฅผ ํ™•์ธํ•ด์„œ 3์ž๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๋ž™์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. 10.182.1.X, 10.182.2.X ์™€ ๊ฐ™์€<NAME>์œผ๋กœ ๋ž™์ด ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. #!/usr/bin/python3 # this script makes assumptions about the physical environment. # 1) each rack is its own layer 3 network with a /24 subnet, which # could be typical where each rack has its own # switch with uplinks to a central core router. # +-----------+ # |core router| # +-----------+ # / \ # +-----------+ +-----------+ # |rack switch| |rack switch| # +-----------+ +-----------+ # | data node | | data node | # +-----------+ +-----------+ # | data node | | data node | # +-----------+ +-----------+ # 2) topology script gets list of IP's as input, calculates network address, and prints '/network_address/ip'. import netaddr import sys sys.argv.pop(0) # discard name of topology script from argv list as we just want IP addresses netmask = '255.255.255.0' # set netmask to what's being used in your environment. The example uses a /24 for ip in sys.argv: # loop over list of datanode IP's address = '{0}/{1}'.format(ip, netmask) # format address string so it looks like 'ip/netmask' to make netaddr work try: network_address = netaddr.IPNetwork(address).network # calculate and print network address print("/{0}".format(network_address)) except: print("/rack-unknown") # print catch-all value if unable to calculate network address bash ์Šคํฌ๋ฆฝํŠธ ๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ๋Š” IP์˜ ๋งˆ์ง€๋ง‰์„ ๋ž™์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 10.182.0.1, 10.182.0.6 ๋…ธ๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ๋งˆ์ง€๋ง‰ IP ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ๊ฐ 1๋ฒˆ ๋…ธ๋“œ, 6๋ฒˆ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. #!/usr/bin/env bash # Here's a bash example to show just how simple these scripts can be # Assuming we have flat network with everything on a single switch, we can fake a rack topology. # This could occur in a lab environment where we have limited nodes, like 2-8 physical machines on a unmanaged switch. # This may also apply to multiple virtual machines running on the same physical hardware. # The number of machines isn't important, but that we are trying to fake a network topology when there isn't one. # +----------+ +--------+ # |jobtracker| |datanode| # +----------+ +--------+ # \ / # +--------+ +--------+ +--------+ # |datanode|--| switch |--|datanode| # +--------+ +--------+ +--------+ # / \ # +--------+ +--------+ # |datanode| |namenode| # +--------+ +--------+ # With this network topology, we are treating each host as a rack. This is being done by taking the last octet # in the datanode's IP and prepending it with the word '/rack-'. The advantage for doing this is so HDFS # can create its 'off-rack' block copy. # 1) 'echo $@' will echo all ARGV values to xargs. # 2) 'xargs' will enforce that we print a single argv value per line # 3) 'awk' will split fields on dots and append the last field to the string '/rack-'. If awk # fails to split on four dots, it will still print '/rack-' last field value echo $@ | xargs -n 1 | awk -F '.' '{print "/rack-"$NF}' ๋ž™ ์ธ์‹ ํ™•์ธ ๋ž™์„ ๊ตฌ์„ฑํ•˜์—ฌ ์„ค์ •๋˜๋ฉด ์ธ์‹๋œ ๋ž™์„ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. datanode ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋Š” report ๋ช…๋ น์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ ๋…ธ๋“œ์˜ ์ •๋ณด์— Rack ์ •๋ณด๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. $ hdfs dfsadmin -report Configured Capacity: xx Present Capacity: xx DFS Remaining: xx DFS Used: xx DFS Used%: 0.13% Under replicated blocks: 0 Blocks with corrupt replicas: 0 Missing blocks: 0 Missing blocks (with replication factor 1): 0 Pending deletion blocks: 902 ------------------------------------------------- Live datanodes (15): Name: 10.0.0.1:50010 (user-host.com) Hostname: user-host.com Rack: /rack/rack-1 nodemanager ๋…ธ๋“œ ๋งค๋‹ˆ์ €๋Š” ๊ฐœ๋ณ„ ๋…ธ๋“œ์˜ ์ƒํƒœ ์ •๋ณด์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ๋ณ„ ๋…ธ๋“œ์˜ ์ƒํƒœ ์ •๋ณด์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ yarn node -status user-host.com:45454 Node Report : Node-Id : user-host.com:45454 Rack : /rack/rack-1 15-๋ฐธ๋Ÿฐ์„œ(balance) HDFS๋ฅผ ์šด์˜ํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ ๋ถˆ๊ท ํ˜•์ด ๋ฐœ์ƒํ•˜์—ฌ ๋ฐธ๋Ÿฐ์‹ฑ์„ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ๋ฐธ๋Ÿฐ์„œ๋Š” ๋ž™ ์ธ์‹(Rack Awareness) ์„ค์ •์ด ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถˆ๊ท ํ˜•์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ ํ•˜๋‘ก์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ ๊ณต๊ฐ„์ด ๋ถ€์กฑํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค๋ฅธ ๋…ธ๋“œ์˜ ์‚ฌ์šฉ ๊ณต๊ฐ„์€ 70~80% ์ •๋„์ธ๋ฐ ์‹ ๊ทœ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋Š” ์‚ฌ์šฉ ๊ณต๊ฐ„์ด 0% ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ ํŠน์ • ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์— ๋ธ”๋ก์ด ๋งŽ์ด ์ €์žฅ๋˜์–ด ๋ฐ์ดํ„ฐ๋…ธ๋“œ ๊ฐ„ ์ €์žฅ ๊ณต๊ฐ„ ์ฐจ์ด๊ฐ€ 20~30% ์ •๋„ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ ํŠน์ • ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์€ ๊ฒฝ์šฐ ๋„ค์ž„๋…ธ๋“œ๋Š” ๋ฐ์ดํ„ฐ ์ €์žฅ ๊ณต๊ฐ„์ด ์ž‘์€ ๋…ธ๋“œ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ด ๊ฒฝ์šฐ ํŠน์ • ๋…ธ๋“œ๋กœ I/O๊ฐ€ ์ง‘์ค‘ ๋˜๊ฒŒ ๋จ HDFS Balancer ๋ฐธ๋Ÿฐ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ƒํ™ฉ์— ๋”ฐ๋ผ ์ž‘์—… ์‹œ๊ฐ„์ด ๊ต‰์žฅํžˆ ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. $ hdfs balancer -threshold 5 Time Stamp Iteration# Bytes Already Moved Bytes Left To Move Bytes Being Moved NameNode Nov 7, 2022 6:50:16 AM 0 4.23 GB 14.96 GB 10 GB hdfs://hdfs-namenode:8020 Nov 7, 2022 7:06:49 AM 1 9.96 GB 9.63 GB 10 GB hdfs://hdfs-namenode:8020 Nov 7, 2022 7:22:56 AM 2 15.53 GB 4.23 GB 8.77 GB hdfs://hdfs-namenode:8020 The cluster is balanced. Exiting... Nov 7, 2022 7:23:05 AM 3 15.53 GB 0 B 0 B hdfs://hdfs-namenode:8020 Nov 7, 2022 7:23:05 AM Balancing took 44.9548 minutes ๋Œ€์—ญํญ ๋ฐธ๋Ÿฐ์„œ๋Š” ์ž‘์—… ๊ฐ„ ๋งŽ์€ ๋ฐ์ดํ„ฐ ์ด๋™์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€์—ญํญ์„ ์ง€์ •ํ•˜์—ฌ ๋‹ค๋ฅธ ์ž‘์—…์— ์˜ํ–ฅ์ด ๊ฐ€์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. hdfs dfsadmin -setBalancerBandwidth 1073741824 threshold ๊ฐ ๋…ธ๋“œ ๊ฐ„ ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ ๋น„์œจ ์ฐจ์ด๋ฅผ ์ž„๊ณ—๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. hdfs balancer -threshold 3 ์ถ”๊ฐ€ ์„ค์ •: hdfs-site.xml dfs.datanode.balance.max.concurrent.moves: 50 ๋ฐ์ดํ„ฐ๋…ธ๋“œ๊ฐ€ ๋ฐ์ดํ„ฐ ์ด๋™์— ์‚ฌ์šฉํ•  ์Šค๋ ˆ๋“œ ๊ฐœ์ˆ˜ dfs.datanode.balance.bandwidthPerSec: 10MB ๋ฐ์ดํ„ฐ ์ด๋™์— ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์—ญํญ 3-๋งต๋ฆฌ๋“€์Šค ๋งต๋ฆฌ๋“€์Šค๋Š” ๊ฐ„๋‹จํ•œ ๋‹จ์œ„์ž‘์—…์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ๋‹จ์œ„์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋งต(Map) ์ž‘์—…๊ณผ ๋งต ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฌผ์„ ๋ชจ์•„์„œ ์ง‘๊ณ„ํ•˜๋Š” ๋ฆฌ๋“€์Šค(Reduce) ๋‹จ๊ณ„๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํ•˜๋‘ก์—์„œ ๋ถ„์‚ฐ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์€ ๋งต๊ณผ ๋ฆฌ๋“€์Šค๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ๋งต, ๋ฆฌ๋“€์Šค ์ž‘์—…์€ ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ž‘์—…์œผ๋กœ, ์—ฌ๋Ÿฌ ์ปดํ“จํ„ฐ์—์„œ ๋™์‹œ์— ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ์ž‘์—… ๋‹จ์œ„ ํ•˜๋‘ก v1์˜ ์ž‘์—… ๋‹จ์œ„๋Š” ์žก(job)์ด๊ณ , ํ•˜๋‘ก v2์˜ ์ž‘์—… ๋‹จ์œ„๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜(application)์ž…๋‹ˆ๋‹ค. YARN ์•„ํ‚คํ…์ฒ˜๊ฐ€ ๋„์ž…๋˜๋ฉด์„œ ์ด๋ฆ„์€ ๋ณ€๊ฒฝ๋˜์—ˆ์ง€๋งŒ ๋™์ผํ•˜๊ฒŒ ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์žก์€ ๋งต ํƒœ์Šคํฌ์™€ ๋ฆฌ๋“€์Šค ํƒœ์Šคํฌ๋กœ ๋‚˜๋ˆ„์–ด์ง‘๋‹ˆ๋‹ค. ํƒœ์Šคํฌ๋Š” ์–ดํ…œํ”„ํŠธ(attempt) ๋‹จ์œ„๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํ•˜๋‘ก ์žก์ด ์ƒ์„ฑ๋˜๋ฉด ์•„์ด๋””๊ฐ€ job_xxx_xxx๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด ์•„์ด๋””๋กœ ์žก์˜ ์ƒํƒœ, ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. YARN์—์„œ๋Š” application_xxx_xxx๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ ‘๋‘์–ด๋Š” ๋‹ค๋ฅด์ง€๋งŒ ๊ฐ™์€ ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์žก์—์„œ ์ƒ์„ฑ๋˜๋Š” ๋งต ํƒœ์Šคํฌ์™€ ๋ฆฌ๋“€์Šค ํƒœ์Šคํฌ๋Š” ์•„์ด๋””๊ฐ€ attempt_xxx_xxx_m_000000_0์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋งต ํƒœ์Šคํฌ๋Š” ์ค‘๊ฐ„ ์•„์ด๋””๊ฐ€ m์œผ๋กœ ์ƒ์„ฑ๋˜๊ณ , ๋ฆฌ๋“€์Šค ํƒœ์Šคํฌ๋Š” r๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์žก์•„์ด๋””: job_1520227878653_30484 ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์•„์ด๋””: application_1520227878653_30484 ์–ดํ…œํ”„ํŠธ ์•„์ด๋””: attempt_1520227878653_30484_m_000000_0 ๋งต๋ฆฌ๋“€์Šค ์žฅ์•  ๊ทน๋ณต(Failover) ๋งต๋ฆฌ๋“€์Šค๋Š” ์‹คํ–‰ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์„ค์ • 1 ๋œ ํšŸ์ˆ˜๋งŒํผ ์ž๋™์œผ๋กœ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ณต ํ›„์—๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ž‘์—…์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ณ  ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๋กœ๊ทธ์ž…๋‹ˆ๋ฅผ ๋ณด๋ฉด ์žก(job_1520227878653_30484)์ด ์ƒ์„ฑ๋˜๊ณ  ์‹คํ–‰๋˜๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ๋งต์žก์˜ ์–ดํ…œํ”„ํŠธ(attempt_1520227878653_30484_m_000000_0)๊ฐ€ ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์žก ์•„์ด๋”” ๋งˆ์ง€๋ง‰์˜ ์ˆซ์ž๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. $ hadoop jar cctv.jar com.sec.cctv.CctvMain /user/cctv/ /user/cctv_output/ 18/10/19 08:22:42 WARN mapreduce.JobResourceUploader: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this. 18/10/19 08:22:42 INFO input.FileInputFormat: Total input paths to process : 1 18/10/19 08:22:42 INFO lzo.GPLNativeCodeLoader: Loaded native gpl library 18/10/19 08:22:42 INFO lzo.LzoCodec: Successfully loaded & initialized native-lzo library [hadoop-lzo rev 418fa8c602f2a4b153c1a89806305f 6b5a27a524] 18/10/19 08:22:42 INFO mapreduce.JobSubmitter: number of splits:1 18/10/19 08:22:42 INFO Configuration.deprecation: mapred.job.queue.name is deprecated. Instead, use mapreduce.job.queuename 18/10/19 08:22:42 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1520227878653_30484 18/10/19 08:22:43 INFO impl.YarnClientImpl: Submitted application application_1520227878653_30484 /application_1520227878653_30484/ 18/10/19 08:22:43 INFO mapreduce.Job: Running job: job_1520227878653_30484 18/10/19 08:22:48 INFO mapreduce.Job: Job job_1520227878653_30484 running in uber mode : false 18/10/19 08:22:48 INFO mapreduce.Job: map 0% reduce 0% 18/10/19 08:22:51 INFO mapreduce.Job: Task Id : attempt_1520227878653_30484_m_000000_0, Status : FAILED Error: java.io.IOException: Type mismatch in value from map: expected org.apache.hadoop.io.Text, received org.apache.hadoop.io.IntWritable at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect(MapTask.java:1095) at org.apache.hadoop.mapred.MapTask$NewOutputCollector.write(MapTask.java:724) at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89) at org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context.write(WrappedMapper.java:112) at com.sec.cctv.CctvMapper.map(CctvMapper.java:17) at com.sec.cctv.CctvMapper.map(CctvMapper.java:9) at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146) at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:796) at org.apache.hadoop.mapred.MapTask.run(MapTask.java:342) at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:164) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:422) at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1698) at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158) 18/10/19 08:22:54 INFO mapreduce.Job: Task Id : attempt_1520227878653_30484_m_000000_1, Status : FAILED Error: java.io.IOException: Type mismatch in value from map: expected org.apache.hadoop.io.Text, received org.apache.hadoop.io.IntWritable at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect(MapTask.java:1095) at org.apache.hadoop.mapred.MapTask$NewOutputCollector.write(MapTask.java:724) at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89) at org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context.write(WrappedMapper.java:112) at com.sec.cctv.CctvMapper.map(CctvMapper.java:17) at com.sec.cctv.CctvMapper.map(CctvMapper.java:9) at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146) at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:796) at org.apache.hadoop.mapred.MapTask.run(MapTask.java:342) at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:164) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:422) at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1698) at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158) 18/10/19 08:22:57 INFO mapreduce.Job: Task Id : attempt_1520227878653_30484_m_000000_2, Status : FAILED Error: java.io.IOException: Type mismatch in value from map: expected org.apache.hadoop.io.Text, received org.apache.hadoop.io.IntWritable at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect(MapTask.java:1095) at org.apache.hadoop.mapred.MapTask$NewOutputCollector.write(MapTask.java:724) at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89) at org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context.write(WrappedMapper.java:112) at com.sec.cctv.CctvMapper.map(CctvMapper.java:17) at com.sec.cctv.CctvMapper.map(CctvMapper.java:9) at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146) at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:796) at org.apache.hadoop.mapred.MapTask.run(MapTask.java:342) at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:164) at java.security.AccessController.doPrivileged(Native Method) at javax.security.auth.Subject.doAs(Subject.java:422) at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1698) at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158) 18/10/19 08:23:02 INFO mapreduce.Job: map 100% reduce 100% 18/10/19 08:23:02 INFO mapreduce.Job: Job job_1520227878653_30484 failed with state FAILED due to: Task failed task_1520227878653_30484_m_000000 Job failed as tasks failed. failedMaps:1 failedReduces:0 18/10/19 08:23:02 INFO mapreduce.Job: Counters: 13 Job Counters Failed map tasks=4 Killed reduce tasks=7 Launched map tasks=4 Other local map tasks=3 Data-local map tasks=1 Total time spent by all maps in occupied slots (ms)=327375 Total time spent by all reduces in occupied slots (ms)=0 Total time spent by all map tasks (ms)=7275 Total time spent by all reduce tasks (ms)=0 Total vcore-milliseconds taken by all map tasks=7275 Total vcore-milliseconds taken by all reduce tasks=0 Total megabyte-milliseconds taken by all map tasks=10476000 Total megabyte-milliseconds taken by all reduce tasks=0 ๋งต ์ž…๋ ฅ ๋ถ„ํ•  ๋งต์˜ ์ž…๋ ฅ์€ ์Šคํ”Œ๋ฆฟ(InputSplit) ๋‹จ์œ„๋กœ ๋ถ„ํ• ๋ฉ๋‹ˆ๋‹ค. ๋งต ์ž‘์—…์€ ํฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ๋…ธ๋“œ์—์„œ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ , ๋ถ„ํ• ํ•˜์—ฌ ๋™์‹œ์— ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌํ•˜์—ฌ ์ž‘์—… ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•ฉ๋‹ˆ๋‹ค. ์Šคํ”Œ๋ฆฟ์ด ์ž‘์œผ๋ฉด ์ž‘์—… ๋ถ€ํ•˜๊ฐ€ ๋ถ„์‚ฐ๋˜์–ด ์„ฑ๋Šฅ์„ ๋†’์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์Šคํ”Œ๋ฆฟ์˜ ํฌ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ์ž‘์œผ๋ฉด ๋งต ์ž‘์—…์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ณ  ๋งต ์ž‘์—… ์ƒ์„ฑ์„ ์œ„ํ•œ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ์ฆ๊ฐ€ํ•˜์—ฌ ์ž‘์—…์ด ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ž‘์—…์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๊ฐœ์ˆ˜์˜ ๋งต ์ž‘์—…์„ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋งต ์ž‘์—…์˜ ์ ์ ˆํ•œ ์Šคํ”Œ๋ฆฟ ํฌ๊ธฐ๋Š” ๋ฐ์ดํ„ฐ ์ง€์—ญ์„ฑ์˜ ์ด์ ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š” HDFS ๋ธ”๋ก์˜ ๊ธฐ๋ณธ ํฌ๊ธฐ(128MB)์ž…๋‹ˆ๋‹ค. ๋งต ์ž‘์—… ๋ฐ์ดํ„ฐ ์ง€์—ญ์„ฑ ๋งต ์ž‘์—…์€ HDFS์— ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๋…ธ๋“œ์—์„œ ์‹คํ–‰ํ•  ๋•Œ ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋„คํŠธ์›Œํฌ ๋Œ€์—ญ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๋…ธ๋“œ์—์„œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ๋™์ผํ•œ ๋ž™์˜ ๋…ธ๋“œ, ๋‹ค๋ฅธ ๋ž™์˜ ๋…ธ๋“œ ์ˆœ์„œ๋กœ ๋งต ์ž‘์—…์ด ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋…ธ๋“œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. ๋งต ์ž‘์—…์˜ ์ ์ ˆํ•œ ์Šคํ”Œ๋ฆฟ ํฌ๊ธฐ๊ฐ€ HDFS ๋ธ”๋ก์˜ ๊ธฐ๋ณธ ํฌ๊ธฐ์ธ ์ด์œ ๋Š” ๋‹จ์ผ ๋…ธ๋“œ์— ํ•ด๋‹น ๋ธ”๋ก์ด ๋ชจ๋‘ ์ €์žฅ๋œ๋‹ค๊ณ  ํ™•์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ์ž…๋ ฅ ํฌ๊ธฐ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์Šคํ”Œ๋ฆฟ ํฌ๊ธฐ๊ฐ€ ๋ธ”๋ก์˜ ๊ธฐ๋ณธ ํฌ๊ธฐ์ผ ๋•Œ ๋งต ์ž‘์—…์ด ๋กœ์ปฌ ๋””์Šคํฌ์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ด์šฉํ•˜์—ฌ, ๋‹ค๋ฅธ ๋…ธ๋“œ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก๋ฐ›์•„ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋Š” ๋กœ์ปฌ ๋””์Šคํฌ์— ์ž„์‹œ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋งต ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋Š” ๋ฆฌ๋“€์Šค ์ž‘์—…์˜ ์ž…๋ ฅ์œผ๋กœ ์“ฐ์ด๋Š” ์ž„์‹œ ๊ฒฐ๊ณผ๋ฌผ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฆฌ๋“€์Šค ์ž‘์—…์€ ๋งต ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์ง€์—ญ์„ฑ์˜ ์žฅ์ ์ด ์—†์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋“€์Šค ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋Š” HDFS์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์Šค ์ž‘์—…์˜ ๊ฐœ์ˆ˜๋Š” ์ž…๋ ฅ ํฌ๊ธฐ์™€ ์ƒ๊ด€์—†์ด ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋“€์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ด๋ฉด ๋ฆฌ๋“€์Šค์˜ ๊ฐœ์ˆ˜๋งŒํผ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•˜๊ณ  ๋งต์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ ํŒŒํ‹ฐ์…˜์— ๋ถ„๋ฐฐํ•ฉ๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜ ๋ณ„๋กœ ํ‚ค๊ฐ€ ์กด์žฌํ•˜๊ณ  ๋™์ผํ•œ ํ‚ค๋Š” ๊ฐ™์€ ํŒŒํ‹ฐ์…˜์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์˜ ์ข…๋ฅ˜ ๋งต๋ฆฌ๋“€์Šค๋Š” ๋ฆฌ๋“€์„œ ์ž‘์—…์ด ์žˆ๋Š” ๊ฒฝ์šฐ์™€ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์ฝ์–ด์„œ ๋ฐ”๋กœ ์“ฐ๋Š” ์ž‘์—…์˜ ๊ฒฝ์šฐ ๋ฆฌ๋“€์„œ๊ฐ€ ํ•„์š” ์—†์–ด์„œ ๋งคํผ๋งŒ ์žˆ๋Š” ์ž‘์—…(Mapper Only)์ด ๋ฉ๋‹ˆ๋‹ค. ์ง‘๊ณ„๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ํ•ด์„œ ๋ฆฌ๋“€์„œ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ •๋ ฌ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋Š” ๋ฆฌ๋“€์„œ๊ฐ€ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€์˜ ๊ฒฝ์šฐ ๋ฆฌ๋“€์„œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž‘์—…์˜ ์ตœ์ข… ๋งคํผ, ๋ฆฌ๋“€์„œ์˜ ์ˆ˜๋งŒํผ ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ๊ฐ€ ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ์ •๋ ฌ ์ž‘์—… ๊ฐ™์€ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ ํ•˜๋‚˜๋กœ ๋ชจ๋“  ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ ์ผ๋ฐ˜์ ์ธ ์ง‘๊ณ„ ์ž‘์—…์˜ ๊ฒฝ์šฐ ๋ฆฌ๋“€์„œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ์˜ ์ˆ˜๋งŒํผ ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. HDFS์˜ ๋ถ€ํ•˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ถ”๊ฐ€์ ์ธ ํŒŒ์ผ ๋จธ์ง€ ์ž‘์—…์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ(Mapper Only ์ž‘์—…) ์›์ฒœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ ๊ฐ€๊ณต์„ ํ•˜๊ณ  ๋ฐ”๋กœ ์“ฐ๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ ์ž‘์—…์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋น ๋ฆ…๋‹ˆ๋‹ค. ๋งคํผ์˜ ์ˆ˜๋งŒํผ ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€์ ์ธ ํŒŒ์ผ ๋จธ์ง€ ์ž‘์—…์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต ๋ฐ˜๋ณต ์„ค์ •(mapreduce.map.maxattempts), ๋ฆฌ๋“€์Šค ๋ฐ˜๋ณต ์„ค์ •(mapreduce.reduce.maxattempts). ๊ธฐ๋ณธ์ ์œผ๋กœ 3ํšŒ ๋ฐ˜๋ณต ์„ค์ •๋˜์–ด ์žˆ์Œ โ†ฉ 1-์ฒ˜๋ฆฌ ๋‹จ๊ณ„ ๋งต๋ฆฌ๋“€์Šค์˜ ์ฒ˜๋ฆฌ๋‹จ๊ณ„๋Š” 8๋‹จ๊ณ„๋กœ ํฌ๊ฒŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ๊ณ„๋Š” ์ž‘์—…์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ์ƒ๋žต๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ์ฒ˜๋ฆฌ ๋‹จ๊ณ„ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋‹จ๊ณ„ ํ…์ŠคํŠธ, csv, gzip ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ ๋งต์œผ๋กœ ์ „๋‹ฌ ๋งต(Map) ์ž…๋ ฅ์„ ๋ถ„ํ• ํ•˜์—ฌ ํ‚ค๋ณ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌ ์ปด๋ฐ”์ด๋„ˆ(Combiner) ๋„คํŠธ์›Œํฌ๋ฅผ ํƒ€๊ณ  ๋„˜์–ด๊ฐ€๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•˜์—ฌ ๋งต์˜ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ฆฌ ๋กœ์ปฌ ๋ฆฌ๋“€์„œ๋ผ๊ณ ๋„ ํ•จ ์ปด๋ฐ”์ด๋„ˆ๋Š” ์ž‘์—…์˜ ์„ค์ •์— ๋”ฐ๋ผ ์—†์„ ์ˆ˜๋„ ์žˆ์Œ ํŒŒํ‹ฐ์…”๋„ˆ(Partitoner) ๋งต์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ‚ค๊ฐ’์„ ํ•ด์‹œ ์ฒ˜๋ฆฌํ•˜์—ฌ ์–ด๋–ค ๋ฆฌ๋“€์„œ๋กœ ๋„˜๊ธธ์ง€๋ฅผ ๊ฒฐ์ • ์…”ํ”Œ(Shuffle) ๊ฐ ๋ฆฌ๋“€์„œ๋กœ ๋ฐ์ดํ„ฐ ์ด๋™ ์ •๋ ฌ(Sort) ๋ฆฌ๋“€์„œ๋กœ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํ‚ค๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ ๋ฆฌ๋“€์„œ(Reduce) ๋ฆฌ๋“€์„œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅ ์ถœ๋ ฅ ๋ฆฌ๋“€์„œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ •์˜๋œ ํ˜•ํƒœ๋กœ ์ €์žฅ ์ž…๋ ฅ ์ž…๋ ฅ ๋‹จ๊ณ„์€ InputFormat ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. InputFormat ์ž…๋ ฅ ํŒŒ์ผ์ด ๋ถ„ํ• ๋˜๋Š” ๋ฐฉ์‹(InputSplit)์ด๋‚˜ ์ฝ์–ด๋“ค์ด๋Š” ๋ฐฉ์‹(RecordReader)์„ ์ •์˜ํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. InputFormat ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜์—ฌ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‘ก์€ ํŒŒ์ผ์„ ์ฝ๊ธฐ ์œ„ํ•œ FileInputFormat์ด๋‚˜, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ์„ ํ•œ ๋ฒˆ์— ์ฝ์„ ์ˆ˜ ์žˆ๋Š” CombineFileInputFormat ๋“ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. package org.apache.hadoop.mapreduce; @InterfaceAudience.Public @InterfaceStability.Stable public abstract class InputFormat<K, V> { /** * Logically split the set of input files for the job. */ public abstract List<InputSplit> getSplits(JobContext context ) throws IOException, InterruptedException; /** * Create a record reader for a given split. The framework will call */ public abstract RecordReader<K, V> createRecordReader(InputSplit split, TaskAttemptContext context ) throws IOException, InterruptedException; } InputSplit InputSplist์€ ๋งต์˜ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„ํ• ํ•˜๋Š” ๋ฐฉ์‹์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ์œ„์น˜์™€ ์ฝ์–ด ๋“ค์ด๋Š” ๊ธธ์ด๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. package org.apache.hadoop.mapreduce; @InterfaceAudience.Public @InterfaceStability.Stable public abstract class InputSplit { /** * Get the size of the split, so that the input splits can be sorted by size. */ public abstract long getLength() throws IOException, InterruptedException; /** * Get the list of nodes by name where the data for the split would be local. * The locations do not need to be serialized. */ public abstract String[] getLocations() throws IOException, InterruptedException; /** * Gets info about which nodes the input split is stored on and how it is * stored at each location. */ @Evolving public SplitLocationInfo[] getLocationInfo() throws IOException { return null; } } RecordReader RecordReader๋Š” ์‹ค์ œ ํŒŒ์ผ์— ์ ‘๊ทผํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ๋“ค์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ <ํ‚ค, ๋ฐธ๋ฅ˜> ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. package org.apache.hadoop.mapreduce; import java.io.Closeable; import java.io.IOException; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; /** * The record reader breaks the data into key/value pairs for input to the */ @InterfaceAudience.Public @InterfaceStability.Stable public abstract class RecordReader<KEYIN, VALUEIN> implements Closeable { /** * Called once at initialization. */ public abstract void initialize(InputSplit split, TaskAttemptContext context ) throws IOException, InterruptedException; /** * Read the next key, value pair. */ public abstract boolean nextKeyValue() throws IOException, InterruptedException; /** * Get the current key */ public abstract KEYIN getCurrentKey() throws IOException, InterruptedException; /** * Get the current value. */ public abstract VALUEIN getCurrentValue() throws IOException, InterruptedException; /** * The current progress of the record reader through its data. */ public abstract float getProgress() throws IOException, InterruptedException; /** * Close the record reader. */ public abstract void close() throws IOException; } Mapper ๋งคํผ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” Mapper ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๊ณ  map() ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. run() ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด๋ฉด ์‹ค์ œ ๋งคํผ ์ž‘์—…์ด ๋™์ž‘ํ•˜๋Š” ๋ฐฉ์‹์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. setup() ๋ฉ”์„œ๋“œ๋กœ ๋งคํผ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , RecordReader๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ map(ํ‚ค, ๋ฐธ๋ฅ˜) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ cleanup() ๋ฉ”์„œ๋“œ๋กœ ์‚ฌ์šฉํ•œ ๋ฆฌ์†Œ์Šค์˜ ๋ฐ˜ํ™˜ ๋“ฑ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. package org.apache.hadoop.mapreduce; @InterfaceAudience.Public @InterfaceStability.Stable public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> { /** * The <code>Context</code> passed on to the {@link Mapper} implementations. */ public abstract class Context implements MapContext<KEYIN, VALUEIN, KEYOUT, VALUEOUT> { } /** * Called once at the beginning of the task. */ protected void setup(Context context) throws IOException, InterruptedException { // NOTHING } /** * Called once for each key/value pair in the input split. Most applications * should override this, but the default is the identity function. */ @SuppressWarnings("unchecked") protected void map(KEYIN key, VALUEIN value, Context context) throws IOException, InterruptedException { context.write((KEYOUT) key, (VALUEOUT) value); } /** * Called once at the end of the task. */ protected void cleanup(Context context) throws IOException, InterruptedException { // NOTHING } /** * Expert users can override this method for more complete control over the * execution of the Mapper. */ public void run(Context context) throws IOException, InterruptedException { setup(context); try { while (context.nextKeyValue()) { map(context.getCurrentKey(), context.getCurrentValue(), context); } } finally { cleanup(context); } } } Combiner ๋งต๋ฆฌ๋“€์Šค ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์ž์›์€ ์œ ํ•œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งต ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ๋“€์Šค ์ž‘์—…์˜ ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์ „์— ์ •๋ฆฌ๋ฅผ ํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์ „์†ก์— ํ•„์š”ํ•œ ์ž์›์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ปด๋ฐ”์ด๋„ˆ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ปด๋ฐ”์ด๋„ˆ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋Š” ์ œ์•ฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’, ์นด์šดํŠธ ๊ฐ™์€ ํ•จ์ˆ˜๋Š” ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ํ‰๊ท  ํ•จ์ˆ˜๋Š” ๋งตํผ ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ํ‰๊ท  ๋‚ด์–ด, ๋ฆฌ๋“€์Šค ์ž‘์—…์— ์‚ฌ์šฉํ•˜๋ฉด ์ตœ์ข… ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋“€์Šค ํ•จ์ˆ˜๋ฅผ ์ปด๋ฐ”์ด๋„ˆ ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ปด๋ฐ”์ด๋„ˆ ํ•จ์ˆ˜๋ฅผ ๋ฆฌ๋“€์Šค ํ•จ์ˆ˜๋ฅผ ์™„์ „ํžˆ ๋Œ€์ฒดํ•  ์ˆ˜๋„ ์—†์Šต๋‹ˆ๋‹ค. ๋งต ๋‹จ๊ณ„์—์„œ ์ปด๋ฐ”์ด๋„ˆ ํ•จ์ˆ˜๋ฅผ ์“ฐ๋”๋ผ๋„ ๋ฆฌ๋“€์Šค ํ•จ์ˆ˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋งต์—์„œ ์˜ค๋Š” ๊ฐ™์€ ํ‚ค์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์—ฌ์ „ํžˆ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ปด๋ฐ”์ด๋„ˆ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋งค ํผ์™€ ๋ฆฌ๋“€์„œ ์‚ฌ์ด์˜ ์…”ํ”Œ ๋‹จ๊ณ„์—์„œ ์ „์†ก๋˜๋Š” ๋ฐ์ดํ„ฐ์–‘์„ ์ค„์ผ ์ˆ˜ ์žˆ์–ด์„œ ์ž‘์—…์˜ ์†๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Partitioner & Shuffle ๋งต ์ž‘์—…์ด ์ข…๋ฃŒ๋˜๋ฉด ์ž‘์—…์ด ๋๋‚œ ๋…ธ๋“œ๋ถ€ํ„ฐ ๋ฆฌ๋“€์„œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š”๋ฐ ์ด๋ฅผ ์…”ํ”Œ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋งต์˜ ๊ฒฐ๊ณผ ํ‚ค๋ฅผ ๋ฆฌ๋“€์„œ๋กœ ๋ถ„๋ฐฐํ•˜๋Š” ๊ธฐ์ค€์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ํŒŒํ‹ฐ์…˜์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ•ด์‹œ ํŒŒํ‹ฐ์…˜์„ ๋ณด๋ฉด ํ‚ค์˜ ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋งŒํผ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ํŒŒํ‹ฐ์…˜์€ ๊ฐ™์€ ๋ฆฌ๋“€์„œ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. package org.apache.hadoop.mapreduce.lib.partition; @InterfaceAudience.Public @InterfaceStability.Stable public class HashPartitioner<K, V> extends Partitioner<K, V> { /** Use {@link Object#hashCode()} to partition. */ public int getPartition(K key, V value, int numReduceTasks) { return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks; } } ๊ธฐ๋ณธ ํŒŒํ‹ฐ์…”๋„ˆ์—์„œ Integer.MAX_VALUE ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ๋…ผ๋ฆฌํ•ฉ(&) ์—ฐ์‚ฐ์„ ํ•˜๋Š” ์ด์œ ๋Š” ํŒŒํ‹ฐ์…˜์˜ ๋ฒˆํ˜ธ๊ฐ€ ์–‘์ˆ˜์—ฌ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ž๋ฐ”์˜ intํ˜•์˜ ์ฒซ ๋ฒˆ์งธ byte๋Š” 0์ด๋ฉด ์–‘์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด์‹œ ์ฝ”๋“œ ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ ์Œ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋ฉด ๋…ผ๋ฆฌํ•ฉ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์–‘์ˆ˜๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ปค์Šคํ…€ ํŒŒํ‹ฐ์…”๋„ˆ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์Œ์ˆ˜๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š๋„๋ก ์ž˜ ์ฒ˜๋ฆฌํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Sort ๋ฆฌ๋“€์Šค ์ž‘์—… ์ „์— ์ „๋‹ฌ๋ฐ›์€ ํ‚ค๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ ๋ฆฌ๋“€์Šค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ List< Value > ํ˜•ํƒœ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๊ทธ๋ฃนํ•‘ ์ž‘์—…๋„ ํ•จ๊ป˜ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ์˜ ํ‚ค์™€ ๋‹ค๋ฅธ ๊ฐ’์„ ํ•จ๊ป˜ ์ด์šฉํ•˜๋Š” ๋ณตํ•ฉํ‚ค์˜ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒŒํ‹ฐ์…”๋‹, ์†ŒํŠธ, ๊ทธ๋ฃนํ•‘ ์ž‘์—…์„ ๊ฑฐ์น˜๋ฉด์„œ ๋ณตํ•ฉํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•  ๋•Œ ํŒŒํ‹ฐ์…˜์˜ ๊ธฐ์ค€์ด ๋˜๋Š” ์ฃผํ‚ค(Primary Key) ์™ธ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์„ธ์ปจ๋”๋ฆฌ ์†ŒํŠธ(Secondary Sort)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Reduce ๋ฆฌ๋“€์„œ๋Š” ํ‚ค๋ณ„๋กœ ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฆฌ๋“€์„œ ์ž‘์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” Reducer ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๊ณ  reduce() ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. run() ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด๋ฉด ์‹ค์ œ ๋งคํผ ์ž‘์—…์ด ๋™์ž‘ํ•˜๋Š” ๋ฐฉ์‹์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. setup() ๋ฉ”์„œ๋“œ๋กœ ๋ฆฌ๋“€์„œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ run(ํ‚ค, list(๋ฐธ๋ฅ˜)) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ cleanup() ๋ฉ”์„œ๋“œ๋กœ ์‚ฌ์šฉํ•œ ๋ฆฌ์†Œ์Šค์˜ ๋ฐ˜ํ™˜ ๋“ฑ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค package org.apache.hadoop.mapreduce; @Checkpointable @InterfaceAudience.Public @InterfaceStability.Stable public class Reducer<KEYIN, VALUEIN, KEYOUT, VALUEOUT> { /** * The <code>Context</code> passed on to the {@link Reducer} implementations. */ public abstract class Context implements ReduceContext<KEYIN, VALUEIN, KEYOUT, VALUEOUT> { } /** * Called once at the start of the task. */ protected void setup(Context context ) throws IOException, InterruptedException { // NOTHING } /** * This method is called once for each key. Most applications will define * their reduce class by overriding this method. The default implementation * is an identity function. */ @SuppressWarnings("unchecked") protected void reduce(KEYIN key, Iterable<VALUEIN> values, Context context ) throws IOException, InterruptedException { for(VALUEIN value: values) { context.write((KEYOUT) key, (VALUEOUT) value); } } /** * Called once at the end of the task. */ protected void cleanup(Context context ) throws IOException, InterruptedException { // NOTHING } /** * Advanced application writers can use the * {@link #run(org.apache.hadoop.mapreduce.Reducer.Context)} method to * control how the reduce task works. */ public void run(Context context) throws IOException, InterruptedException { setup(context); try { while (context.nextKey()) { reduce(context.getCurrentKey(), context.getValues(), context); // If a back up store is used, reset it Iterator<VALUEIN> iter = context.getValues().iterator(); if(iter instanceof ReduceContext.ValueIterator) { ((ReduceContext.ValueIterator<VALUEIN>) iter).resetBackupStore(); } } } finally { cleanup(context); } } } ์ถœ๋ ฅ ์ถœ๋ ฅ ๋‹จ๊ณ„๋Š” OutputFormat ๋‹จ๊ณ„๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. OutputFormat ์ถœ๋ ฅํ”ผ์ผ์— ๊ธฐ๋ก๋˜๋Š”<NAME>์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. Text<NAME>, Csv<NAME> ๋“ฑ์„ ์„ค์ •ํ•˜๊ณ  ํŒŒ์ผ์„ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. import java.io.IOException; @InterfaceAudience.Public @InterfaceStability.Stable public abstract class OutputFormat<K, V> { public abstract RecordWriter<K, V> getRecordWriter(TaskAttemptContext context ) throws IOException, InterruptedException; public abstract void checkOutputSpecs(JobContext context ) throws IOException, InterruptedException; public abstract OutputCommitter getOutputCommitter(TaskAttemptContext context ) throws IOException, InterruptedException; } RecordWriter ์‹ค์ œ ํŒŒ์ผ์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ  hadoop mapreduce tutorial github MapReduce source 1-์›Œ๋“œ ์นด์šดํŠธ ๋งต๋ฆฌ๋“€์Šค์˜ ์ฒ˜๋ฆฌ ๋‹จ๊ณ„๋ฅผ ์›Œ๋“œ ์นด์šดํŠธ ์˜ˆ์ œ๋กœ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์›Œ๋“œ ์นด์šดํŠธ ์˜ˆ์ œ๋Š” ํ•˜๋‘ก์˜ ๋งต๋ฆฌ๋“€์Šค ํŠœํ† ๋ฆฌ์–ผ์˜ ์›Œ๋“œ ์นด์šดํŠธ ์˜ˆ์ œ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์›Œ๋“œ ์นด์šดํŠธ๋Š” ์ž…๋ ฅ๋œ ํŒŒ์ผ์˜ ๋ฌธ์ž ์ˆ˜๋ฅผ ์„ธ๋Š” ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. ์›Œ๋“œ ์นด์šดํŠธ์˜ ์ž‘์—… ๋‹จ๊ณ„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์›Œ๋“œ ์นด์šดํŠธ์˜ ์ „์ฒด ์†Œ์Šค์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งคํผ๋ฅผ ๊ตฌํ˜„ํ•œ TokenizerMapper, ๋ฆฌ๋“€์„œ๋ฅผ ๊ตฌํ˜„ํ•œ IntSumReducer ์žก์„ ์„ค์ •ํ•˜๋Š” main() ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { // ๋งต public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { // ์ž…๋ ฅ๋œ ํ•œ ๋ผ์ธ(value)์„ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„ํ• ํ•˜์—ฌ // context ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž„์‹œํŒŒ์ผ๋กœ ์ €์žฅ StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } // ๋ฆฌ๋“€์Šค public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); // ํ‚ค(๋ฌธ์ž) ๋ณ„๋กœ ์ „๋‹ฌ๋œ ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•˜์—ฌ ์ถœ๋ ฅ public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { // Job ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•˜๋‘ก ์ž‘์—…์„ ์‹คํ–‰ Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); // ์ž…๋ ฅ ํŒŒ์ผ ์œ„์น˜ FileOutputFormat.setOutputPath(job, new Path(args[1])); // ์ถœ๋ ฅ ํŒŒ์ผ ์œ„์น˜ System.exit(job.waitForCompletion(true) ? 0 : 1); } } ์‹คํ–‰ ํ•˜๋‘ก ๋งต๋ฆฌ๋“€์Šค์˜ ์‹คํ–‰์€ jar ํŒŒ์ผ์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์›Œ๋“œ ์นด์šดํŠธ ์˜ˆ์ œ๋ฅผ ๋นŒ๋“œ ํ•˜์—ฌ jar ํŒŒ์ผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ž‘์—… ์ค€๋น„ ๋จผ์ € ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  HDFS์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ํŒŒ์ผ์˜<NAME>์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. $ cat word.txt Deer Bear River Car Car River Deer Car Bear # ์ž‘์—… ํŒŒ์ผ ๋ณต์‚ฌ $ hadoop fs -put ./word.txt /user/word/input/ ์‹คํ–‰ ์‹คํ–‰์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด hadoop jar ๋ช…๋ น์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์ž‘์—… ๋Œ€์ƒ jar ํŒŒ์ผ๊ณผ ํด๋ž˜์Šค ๋ช…์„ ๋ช…์‹œํ•˜๊ณ  ์ž‘์—… ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž…๋ ฅ ์œ„์น˜์™€ ์ถœ๋ ฅ ์œ„์น˜๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. $ hadoop jar Mapreduce.jar sdk.WordCount /user/word/input /user/word/output ์ž‘์—… ๊ฒฐ๊ณผ ์ž‘์—… ๊ฒฐ๊ณผ๋Š” part ํŒŒ์ผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. _SUCCESS๋Š” ์ž‘์—…์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์ž์ฒด์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. # ์‹คํ–‰ ๊ฒฐ๊ณผ ํ™•์ธ $ hadoop fs -ls /user/word/output/ Found 8 items -rw-r--r-- 2 hadoop hadoop 0 2019-02-21 05:39 /user/word/output/_SUCCESS -rw-r--r-- 2 hadoop hadoop 0 2019-02-21 05:39 /user/word/output/part-r-00000 -rw-r--r-- 2 hadoop hadoop 0 2019-02-21 05:39 /user/word/output/part-r-00001 -rw-r--r-- 2 hadoop hadoop 7 2019-02-21 05:39 /user/word/output/part-r-00002 -rw-r--r-- 2 hadoop hadoop 0 2019-02-21 05:39 /user/word/output/part-r-00003 -rw-r--r-- 2 hadoop hadoop 0 2019-02-21 05:39 /user/word/output/part-r-00004 -rw-r--r-- 2 hadoop hadoop 0 2019-02-21 05:39 /user/word/output/part-r-00005 -rw-r--r-- 2 hadoop hadoop 21 2019-02-21 05:39 /user/word/output/part-r-00006 # ํŒŒ์ผ ๋‚ด์šฉ ํ™•์ธ $ hadoop fs -cat /user/word/output/part* Deer 2 Bear 2 Car 3 River 2 ์›Œ๋“œ ์นด์šดํŠธ ์ฒ˜๋ฆฌ ๋‹จ๊ณ„ ์ž…๋ ฅ ์›Œ๋“œ ์นด์šดํŠธ๋Š” ํŒŒ์ผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ ์œ„์น˜๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•˜๋ฉด FileInputFormat์„ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค. FileInputFormat์€ ์ง€์ •ํ•œ ์œ„์น˜์˜ ํŒŒ์ผ์„ ๋ผ์ธ ๋‹จ์œ„๋กœ ์ฝ์–ด์„œ ๋งต์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. FileInputFormat.addInputPath(job, new Path(args[0])); ๋งต์€ ์ „๋‹ฌ๋ฐ›์€ <ํ‚ค, ๋ฐธ๋ฅ˜>์—์„œ ๋ฐธ๋ฅ˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต๋ฐฑ ๋‹จ์œ„๋กœ ๋ถ„ํ• ํ•˜์—ฌ ๋ฌธ์ž ์ˆ˜๋ฅผ ์„ธ์–ด์ค๋‹ˆ๋‹ค. ๋งต์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ฆฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ๋ฌธ์ž๊ฐ€ ์žˆ์–ด๋„ ํ•ฉ๊ณ„๋ฅผ ๋‚ด์ง€ ์•Š๊ณ  ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. [์ž…๋ ฅ, <๋ฒ„ํผ ๋ฒˆํ˜ธ, ๋ผ์ธ>] 1, Deer Bear River 16, Car Car River 30, Deer Car Bear [์ถœ๋ ฅ, <๋ฌธ์ž, 1>] Dear 1 Bear 1 River 1 Car 1 Car 1 River 1 Dear 1 Car 1 Bear 1 map ํ•จ์ˆ˜์˜ key๋กœ ๋“ค์–ด์˜ค๋Š” ๊ฐ’์ด ๋ฒ„ํผ ๋ฒˆํ˜ธ์ด๊ณ , value๋กœ ๋“ค์–ด์˜ค๋Š” ๊ฐ’์ด ๋ผ์ธ์ด ๋ฉ๋‹ˆ๋‹ค. ์ด value๋ฅผ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ์ž˜๋ผ์„œ ๋ฌธ์ž ๋‹จ์œ„๋กœ ํŒŒ์ผ์ด ์“ฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. public void map(Object key, Text value, Context context) throws IOException, InterruptedException { // ์ž…๋ ฅ๋œ ํ•œ ๋ผ์ธ(value)์„ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„ํ• ํ•˜์—ฌ // context ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž„์‹œํŒŒ์ผ๋กœ ์ €์žฅ StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } ์ปด๋ฐ”์ด๋„ˆ(Combiner) ์ปด๋ฐ”์ด๋„ˆ๋Š” ์žก์— ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ปด๋ฐ”์ด๋„ˆ๋Š” ์„ค์ •ํ•ด๋„ ๋˜๊ณ , ์„ค์ •ํ•˜์ง€ ์•Š์•„๋„ ๋™์ž‘์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. job.setCombinerClass(IntSumReducer.class); ์ปด๋ฐ”์ด๋„ˆ๋ฅผ ์„ค์ •ํ•˜๋ฉด ๋กœ์ปฌ์—์„œ ๋ฆฌ๋“€์„œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์ฒ˜๋Ÿผ ๋ฆฌ๋“€์„œ๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์ค„์–ด๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๋„คํŠธ์›Œํฌ ์ž์›์˜ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [์ž…๋ ฅ, <๋ฌธ์ž, 1>] Dear 1 Bear 1 River 1 Car 1 Car 1 River 1 Dear 1 Car 1 Bear 1 [์ถœ๋ ฅ, <๋ฌธ์ž, List(1)>] Dear List(1, 1) Bear List(1, 1) Car List(1, 1, 1) River List(1, 1) ํŒŒํ‹ฐ์…”๋„ˆ, ์…”ํ”Œ, ์†ŒํŠธ ์›Œ๋“œ ์นด์šดํŠธ ์˜ˆ์ œ์—์„œ ํŒŒํ‹ฐ์…”๋„ˆ, ์…”ํ”Œ, ์†ŒํŠธ ๋‹จ๊ณ„๋Š” ๋”ฐ๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๊ธฐ๋ณธ ๋‹จ๊ณ„๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งต์˜ ๊ฒฐ๊ณผ๋Š” ๋ฆฌ๋“€์Šค์˜ ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. [๋งต์˜ ๊ฒฐ๊ณผ] Dear 1 Bear 1 River 1 Car 1 Car 1 River 1 [๋ฆฌ๋“€์„œ์˜ ์ž…๋ ฅ] Dear List(1, 1) Bear List(1, 1) Car List(1, 1, 1) River List(1, 1) ๋ฆฌ๋“€์„œ(Reduce) ๋ฆฌ๋“€์„œ๋Š” ํ‚ค๋ณ„๋กœ ์ „๋‹ฌ๋œ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•˜์—ฌ ์ „์ฒด ๋“ฑ์žฅ ํšŒ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. [์ž…๋ ฅ, <๋ฌธ์ž, List(1)>] Dear List(1, 1) Bear List(1, 1) Car List(1, 1, 1) River List(1, 1) [์ถœ๋ ฅ, <๋ฌธ์ž, ํšŸ์ˆ˜>] Deer 2 Bear 2 Car 3 River 2 ๋ฌธ์ž๊ฐ€ key๋กœ ๋“ค์–ด์˜ค๊ณ , ๋“ฑ์žฅ ํšŸ์ˆ˜๊ฐ€ Iterable ํ˜•ํƒœ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’๋“ค์„ ๋ชจ๋‘ ๋”ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } ์ถœ๋ ฅ ์ง€์ •ํ•œ ์œ„์น˜์— ํŒŒ์ผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๋Š” ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜์™€ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. FileOutputFormat.setOutputPath(job, new Path(args[1])); ์ฐธ๊ณ  hadoop mapreduce tutorial (๋ฐ”๋กœ ๊ฐ€๊ธฐ) 2-์›Œ๋“œ ์นด์šดํŠธ 2 - ์นด์šดํ„ฐ, ๋ถ„์‚ฐ ์บ์‹œ ์›Œ๋“œ ์นด์šดํŠธ 2 ์˜ˆ์ œ๋Š” ๊ธฐ์กด ์›Œ๋“œ ์นด์šดํŠธ ์˜ˆ์ œ๋ฅผ ๋ณด๊ฐ•ํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. setup ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์นด์šดํ„ฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ถ„์‚ฐ ์บ์‹œ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์˜ต์…˜ ํŒŒ์„œ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์†Œ์Šค ์ฝ”๋“œ ์›Œ๋“œ ์นด์šดํŠธ 2์˜ ์ „์ฒด ์†Œ์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. package sdk; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.Counter; import org.apache.hadoop.util.GenericOptionsParser; import org.apache.hadoop.util.StringUtils; public class WordCount2 { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { // ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ์นด์šดํ„ฐ static enum CountersEnum { INPUT_WORDS } private final static IntWritable one = new IntWritable(1); private Text word = new Text(); private boolean caseSensitive; // ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ private Set<String> patternsToSkip = new HashSet<String>(); // ๋ฌธ์ž์—์„œ ์ œ๊ฑฐํ•  ๊ธฐํ˜ธ private Configuration conf; // ๋งต ์ž‘์—…์˜ ์„ค์ • @Override public void setup(Context context) throws IOException, InterruptedException { conf = context.getConfiguration(); caseSensitive = conf.getBoolean("wordcount.case.sensitive", true); // ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ if (conf.getBoolean("wordcount.skip.patterns", false)) { // ์Šคํ‚ต ํŒจํ„ด ํŒŒ์ผ์„ ๋ถ„์‚ฐ ์บ์‹œ์—์„œ ๊ฐ€์ ธ์™€์„œ patternsToSkip์— ์„ค์ • URI[] patternsURIs = Job.getInstance(conf).getCacheFiles(); for (URI patternsURI : patternsURIs) { Path patternsPath = new Path(patternsURI.getPath()); String patternsFileName = patternsPath.getName().toString(); parseSkipFile(patternsFileName); } } } private void parseSkipFile(String fileName) { try (BufferedReader fis = new BufferedReader(new FileReader(fileName))) { String pattern = null; while ((pattern = fis.readLine()) != null) { patternsToSkip.add(pattern); } } catch (IOException ioe) { System.err.println("Caught exception while parsing the cached file '" + StringUtils.stringifyException(ioe)); } } @Override public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String line = (caseSensitive) ? value.toString() : value.toString().toLowerCase(); // patternsToSkip๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐ for (String pattern : patternsToSkip) { line = line.replaceAll(pattern, ""); } StringTokenizer itr = new StringTokenizer(line); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); // ์นด์šดํ„ฐ์— 1์„ ๋”ํ•จ Counter counter = context.getCounter("User Custom Counter", CountersEnum.INPUT_WORDS.toString()); counter.increment(1); } } } public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); // ์˜ต์…˜ ํŒŒ์„œ๋ฅผ ์ด์šฉํ•ด์„œ ์˜ต์…˜์˜ ๊ฐœ์ˆ˜๊ฐ€ 2 ๋˜๋Š” 4๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ฉด ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ ํ›„ ์ข…๋ฃŒ // -D๋กœ ์ „๋‹ฌ๋˜๋Š” ์˜ต์…˜์€ conf์— ์„ค์ • GenericOptionsParser optionParser = new GenericOptionsParser(conf, args); String[] remainingArgs = optionParser.getRemainingArgs(); if (!(remainingArgs.length != 2 || remainingArgs.length != 4)) { System.err.println("Usage: wordcount <in> <out> [-skip skipPatternFile]"); System.exit(2); } // ๋งต๋ฆฌ๋“€์Šค ์žก ์„ค์ • Job job = Job.getInstance(conf, "word count 2"); job.setJarByClass(WordCount2.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); // ์˜ต์…˜์œผ๋กœ -skip์œผ๋กœ ์ „๋‹ฌ๋œ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ๋ถ„์‚ฐ ์บ์‹œ์— ์ถ”๊ฐ€ํ•˜๊ณ , // wordcount.skip.patterns๋ฅผ true๋กœ ์„ค์ • List<String> otherArgs = new ArrayList<String>(); for (int i = 0; i < remainingArgs.length; ++i) { if ("-skip".equals(remainingArgs[i])) { job.addCacheFile(new Path(remainingArgs[++i]).toUri()); job.getConfiguration().setBoolean("wordcount.skip.patterns", true); } else { otherArgs.add(remainingArgs[i]); // ํŒŒ์ผ ๊ฒฝ๋กœ } } FileInputFormat.addInputPath(job, new Path(otherArgs.get(0))); FileOutputFormat.setOutputPath(job, new Path(otherArgs.get(1))); System.exit(job.waitForCompletion(true) ? 0 : 1); } } setup ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๋งต๋ฆฌ๋“€์Šค ํ”„๋ ˆ์ž„์›Œํฌ๋Š” map, reduce ํ•จ์ˆ˜์˜ ์‹คํ–‰ ์ „ setup ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ž‘์—…์— ํ•„์š”ํ•œ ์„ค์ •๊ฐ’๊ณผ ์ „์ฒ˜๋ฆฌ๋ฅผ ์—ฌ๊ธฐ์„œ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…์ŠคํŠธ์—์„œ ์„ค์ •๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ์„ค์ •์— ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. @Override public void setup(Context context) throws IOException, InterruptedException { conf = context.getConfiguration(); caseSensitive = conf.getBoolean("wordcount.case.sensitive", ... } ์นด์šดํ„ฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์นด์šดํ„ฐ(Counter)๋Š” enum์„ ์ด์šฉํ•˜์—ฌ ์นด์šดํ„ฐ๋ฅผ ๋“ฑ๋กํ•˜๊ณ , ์ปจํ…์ŠคํŠธ์—์„œ ์นด์šดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•œ ์นด์šดํ„ฐ๋Š” ๋กœ๊ทธ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. // ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ์นด์šดํ„ฐ static enum CountersEnum { INPUT_WORDS } // ์นด์šดํ„ฐ ์ด์šฉ Counter counter = context.getCounter("User Custom Counter", CountersEnum.INPUT_WORDS.toString()); counter.increment(1); ์นด์šดํ„ฐ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋งต๋ฆฌ๋“€์Šค ์‹คํ–‰ ๋กœ๊ทธ์—์„œ ์นด์šดํ„ฐ ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. WRONG_MAP=0 WRONG_REDUCE=0 User Custom Counter INPUT_WORDS=9 File Input Format Counters Bytes Read=57 ๋ถ„์‚ฐ ์บ์‹œ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ถ„์‚ฐ ์บ์‹œ๋Š” ์žก์— addCacheFile์„ ์ด์šฉํ•˜์—ฌ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. ๋งต, ๋ฆฌ๋“€์Šค์—์„œ ์ด์šฉํ•  ๋•Œ๋Š” getCacheFiles๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. // main() if ("-skip".equals(remainingArgs[i])) { job.addCacheFile(new Path(remainingArgs[++i]).toUri()); job.getConfiguration().setBoolean("wordcount.skip.patterns", true); } // setUp์—์„œ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• URI[] patternsURIs = Job.getInstance(conf).getCacheFiles(); ์˜ต์…˜ ํŒŒ์„œ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์˜ต์…˜ ํŒŒ์„œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐœ๋ณ„์ ์œผ๋กœ ์„ค์ •์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•„๋„, ์„ค์ •๊ฐ’์„ ํšจ์œจ์ ์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹คํ–‰ ์‹œ์ ์— ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์„ค์ •๊ฐ’์˜ ์ ‘๋‘์–ด๋ฅผ ๋ถ„์„ํ•˜์—ฌ conf์— ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. // ์˜ต์…˜ ํŒŒ์„œ GenericOptionsParser optionParser = new GenericOptionsParser(conf, args); // ์‹คํ–‰ ์‹œ ์„ค์ • ์ถ”๊ฐ€ $ hadoop jar SdkMapreduce.jar sdk.WordCount2 -Dmapred.job.queue.name=queue_name /user/word/input2 /user/word/output2 ์›Œ๋“œ ์นด์šดํŠธ 2 ์‹คํ–‰ ์ค€๋น„ ์›Œ๋“œ ์นด์šดํŠธ 2๋Š” ์‹คํ–‰์„ ์œ„ํ•ด ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ํŒŒ์ผ์˜ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๊ณ  HDFS์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ๋Š” ๋‘ ๊ฐœ์˜ ํŒŒ์ผ๋กœ ๋˜์–ด ์žˆ์ง€๋งŒ ํ•˜๋‚˜์˜ ํŒŒ์ผ์„ ์ด์šฉํ•ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. $ cat file1.txt Hello World, Bye World! $ cat file2.txt Hello Hadoop, Goodbye to hadoop. # ํŒŒ์ผ ๋ณต์‚ฌ $ hadoop fs -put file*txt /user/word/input2/ ์‹คํ–‰ ์›Œ๋“œ ์นด์šดํŠธ 2๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ hadoop jar SdkMapreduce.jar sdk.WordCount2 /user/word/input2 /user/word/output2 $ hadoop fs -ls /user/word/output2/ Found 8 items -rw-r--r-- 2 hadoop hadoop 0 2019-02-21 07:44 /user/word/output2/_SUCCESS -rw-r--r-- 2 hadoop hadoop 0 2019-02-21 07:44 /user/word/output2/part-r-00000 -rw-r--r-- 2 hadoop hadoop 15 2019-02-21 07:44 /user/word/output2/part-r-00001 -rw-r--r-- 2 hadoop hadoop 0 2019-02-21 07:44 /user/word/output2/part-r-00002 -rw-r--r-- 2 hadoop hadoop 0 2019-02-21 07:44 /user/word/output2/part-r-00003 -rw-r--r-- 2 hadoop hadoop 10 2019-02-21 07:44 /user/word/output2/part-r-00004 -rw-r--r-- 2 hadoop hadoop 17 2019-02-21 07:44 /user/word/output2/part-r-00005 -rw-r--r-- 2 hadoop hadoop 25 2019-02-21 07:44 /user/word/output2/part-r-00006 # ์‹คํ–‰ ๊ฒฐ๊ณผ ํ™•์ธ $ hadoop fs -cat /user/word/output2/part* Bye 1 Goodbye 1 Hadoop, 1 World, 1 Hello 2 World! 1 hadoop. 1 to 1 ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ์‹คํ–‰ wordcount.case.sensitive ์˜ต์…˜์„ ์ด์šฉํ•˜๋ฉด ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ์‹คํ–‰ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹คํ–‰ ๊ฒฐ๊ณผ ๋‹ค์Œ์ฒ˜๋Ÿผ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ์–ด ํ™•์ธํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ hadoop jar SdkMapreduce.jar sdk.WordCount2 -Dwordcount.case.sensitive=false /user/word/input2 /user/word/output2 # ์‹คํ–‰ ๊ฒฐ๊ณผ ํ™•์ธ $ hadoop fs -cat /user/word/output2/part* bye 1 world! 1 hadoop, 1 hello 2 world, 1 goodbye 1 hadoop. 1 to 1 ๊ธฐํ˜ธ๋Š” ์ œ๊ฑฐํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋„๋ก ์‹คํ–‰ ๊ธฐํ˜ธ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํŒจํ„ด ํŒŒ์ผ์„ ๋จผ์ € HDFS์— ์˜ฌ๋ ค์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŒจํ„ด ํŒŒ์ผ์„ ์—…๋กœ๋‘ ํ›„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ cat replace.txt \! \, \. # ํŒจํ„ด ํŒŒ์ผ ์—…๋กœ๋“œ $ hadoop fs -put ./replace.txt /user/word/replace.txt # ํŒจํ„ด ํŒŒ์ผ ์‹คํ–‰ $ hadoop jar SdkMapreduce.jar sdk.WordCount2 -Dmapred.job.queue.name=queue_name -Dwordcount.case.sensitive=false /user/word/input2 /user/word/output2 -skip /user/word/replace.txt # ์‹คํ–‰ ๊ฒฐ๊ณผ ํ™•์ธ $ hadoop fs -cat /user/word/output2/part* world 2 bye 1 hadoop 2 hello 2 goodbye 1 to 1 ์ฐธ๊ณ  Hadoop MapReduce Example: WordCount v2.0 (๋ฐ”๋กœ ๊ฐ€๊ธฐ) 2-๋ณด์กฐ ๋„๊ตฌ ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ฃผ๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ์„ฑ ๋„๊ตฌ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์นด์šดํ„ฐ ํ•˜๋‘ก์€ ๋งต๋ฆฌ๋“€์Šค ์žก์˜ ์ง„ํ–‰ ์ƒํ™ฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์นด์šดํ„ฐ(Counter)๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์žก์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋งต๋ฆฌ๋“€์Šค์˜ ์ž‘์—… ์ƒํ™ฉ ์ž…์ถœ๋ ฅ ์ƒํ™ฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์นด์šดํ„ฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์นด์šดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 18/10/19 08:23:02 INFO mapreduce.Job: Counters: 13 Job Counters Failed map tasks=4 Killed reduce tasks=7 Launched map tasks=4 Other local map tasks=3 Data-local map tasks=1 Total time spent by all maps in occupied slots (ms)=327375 Total time spent by all reduces in occupied slots (ms)=0 Total time spent by all map tasks (ms)=7275 Total time spent by all reduce tasks (ms)=0 Total vcore-milliseconds taken by all map tasks=7275 Total vcore-milliseconds taken by all reduce tasks=0 Total megabyte-milliseconds taken by all map tasks=10476000 Total megabyte-milliseconds taken by all reduce tasks=0 ๋ถ„์‚ฐ ์บ์‹œ(Distributed Cache) ๋งต๋ฆฌ๋“€์Šค ์žก์—์„œ ๊ณต์œ ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด์•ผ ํ•  ๋•Œ ๋ถ„์‚ฐ ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ์žก์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ธํ•ด์•ผ ํ•  ๊ฒฝ์šฐ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. // ๋“œ๋ผ์ด๋ฒ„์— ๋“ฑ๋ก Job job = new Job(); ... job.addCacheFile(new Path(filename).toUri()); // ๋งตํผ์—์„œ ์‚ฌ์šฉ Path[] localPaths = context.getLocalCacheFiles(); 3-๋ฉ”๋ชจ๋ฆฌ ์„ค์ • ๋งต๋ฆฌ๋“€์Šค์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์€ mapred-site.xml ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์—ฌ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ mapred-default.xml๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Mapper ์™€ Reducer ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • yarn.app.mapreduce.am.resource.mb ๋…ธ๋“œ์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ํ• ๋‹นํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ yarn.app.mapreduce.am.command-opts ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์˜ ํžˆํ”„ ์‚ฌ์ด์ฆˆ mapreduce.map.memory.mb ๋งต ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์„ค์ •ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ mapreduce.map.java.opts ๋งต ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์„ค์ •ํ•˜๋Š” ์ž๋ฐ” ์˜ต์…˜ Xmx ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ํžˆํ”„ ์‚ฌ์ด์ฆˆ๋ฅผ ์„ค์ • ๋งต ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ(mapreduce.map.momory.mb)์˜ 80%๋กœ ์„ค์ • mapreduce.map.cpu.vcores ๋งต ์ปจํ…Œ์ด๋„ˆ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ€์ƒ ์ฝ”์–ด ๊ฐœ์ˆ˜ ๊ธฐ๋ณธ๊ฐ’์€ 1 mapreduce.reduce.memory.mb ๋ฆฌ๋“€์Šค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์„ค์ •ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋งต ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ(mapreduce.map.memory.mb)์˜ 2๋ฐฐ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์  mapreduce.reduce.java.opts ๋ฆฌ๋“€์Šค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์„ค์ •ํ•˜๋Š” ์ž๋ฐ” ์˜ต์…˜ Xmx ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ํžˆํ”„ ์‚ฌ์ด์ฆˆ๋ฅผ ์„ค์ • ๋ฆฌ๋“€์Šค ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ์˜ 80%๋กœ ์„ค์ • mapreduce.reduce.cpu.vcores ๋ฆฌ๋“€์Šค ์ปจํ…Œ์ด๋„ˆ์˜ ์ฝ”์–ด ๊ฐœ์ˆ˜ mapred.child.java.opts ๋งต๊ณผ ๋ฆฌ๋“€์Šค ํƒœ์Šคํฌ์˜ JVM ์‹คํ–‰ ์˜ต์…˜, Heap ์‚ฌ์ด์ฆˆ ์„ค์ • mapreduce.map.java.opts, mapreduce.reduce.java.opts ์„ค์ •์ด ์ด ์„ค์ •์„ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜์—ฌ ์„ค์ • ๊ธฐ๋ณธ ์„ค์ •์€ -Xmx200m <property> <name>yarn.app.mapreduce.am.resource.mb</name> <value>2880</value> </property> <property> <name>mapreduce.map.memory.mb</name> <value>1024</value> </property> <property> <name>mapreduce.map.java.opts</name> <value>-Xmx820m</value> </property> <property> <name>mapreduce.map.cpu.vcores</name> <value>1</value> </property> MR ์—”์ง„ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • MR ์—”์ง„์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. MR ์ปจํ…Œ์ด๋„ˆ์˜ map์„ reduce๋กœ ๋ฐ”๊พธ๋ฉด ๋ฆฌ๋“€์„œ์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์ž…๋‹ˆ๋‹ค. TEZ ์—”์ง„ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • TEZ ์—”์ง„์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋งคํผ ์„ค์ • ๋งคํผ ์ฒ˜๋ฆฌ ์ค‘ ๋ฐœ์ƒํ•˜๋Š” ์ž„์‹œ ๋ฐ์ดํ„ฐ์˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. mapreduce.task.io.sort.mb ๋งต์˜ ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ํ™˜ํ˜• ๋ฒ„ํผ์˜ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ ๋งต์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์„ค์ •ํ•œ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€, io.sort.spill.percent ์ด์ƒ์— ๋„๋‹ฌํ•˜๋ฉด ์ž„์‹œ ํŒŒ์ผ๋กœ ์ถœ๋ ฅ split/sort ์ž‘์—…์„ ์œ„ํ•œ ์˜ˆ์•ฝ ๋ฉ”๋ชจ๋ฆฌ ๋งค ํผ๊ฐ€ ์†ŒํŒ…์— ์‚ฌ์šฉํ•˜๋Š” ๋ฒ„ํผ ์‚ฌ์ด์ฆˆ๋ฅผ ์„ค์ • ๋””์Šคํฌ์— ์“ฐ๋Š” ํšŸ์ˆ˜๊ฐ€ ์ค„์–ด๋“ฆ mapreduce.map.sort.spill.percent ๋งต์˜ ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฒ„ํผ(mapreduce.task.io.sort.mb)๊ฐ€ ์„ค์ •ํ•œ ๋น„์œจ์— ๋„๋‹ฌํ•˜๋ฉด ๋กœ์ปฌ ๋””์Šคํฌ์— ์ž„์‹œ ํŒŒ์ผ ์ถœ๋ ฅ mapreduce.task.io.sort.factor ํ•˜๋‚˜์˜ ์ •๋ ฌ๋œ ์ถœ๋ ฅ ํŒŒ์ผ๋กœ ๋ณ‘ํ•ฉํ•˜๊ธฐ ์œ„ํ•œ ์ž„์‹œ ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜ mapreduce.cluster.local.dir ์ž„์‹œ ํŒŒ์ผ์ด ์ €์žฅ๋˜๋Š” ์œ„์น˜ <property> <name>mapreduce.task.io.sort.mb</name> <value>200</value> </property> <property> <name>mapreduce.map.sort.spill.percent</name> <value>0.80</value> </property> <property> <name>mapreduce.task.io.sort.factor</name> <value>100</value> </property> <property> <name>mapreduce.cluster.local.dir</name> <value>${hadoop.tmp.dir}/mapred/temp</value> </property> ์…”ํ”Œ ์„ค์ • ์…”ํ”Œ ๋‹จ๊ณ„์˜ ์„ค์ •์€ ๋งต์—์„œ ์ „๋‹ฌ๋ฐ›์€ ์ž„์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ์Šค๋ ˆ๋“œ์˜ ๊ฐœ์ˆ˜ ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. mapreduce.reduce.shuffle.parallelcopies ์…”ํ”Œ ๋‹จ๊ณ„์—์„œ ๋งต์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ๋“€์„œ๋กœ ์ „๋‹ฌํ•˜๋Š” ์Šค๋ ˆ๋“œ์˜ ๊ฐœ์ˆ˜ mapreduce.reduce.memory.total.bytes ์…”ํ”Œ ๋‹จ๊ณ„์—์„œ ์ „๋‹ฌ๋œ ๋งต์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋ฒ„ํผ์˜ ํฌ๊ธฐ ๊ธฐ๋ณธ๊ฐ’์€ 1024MB mapreduce.reduce.shuffle.input.buffer.percent ๋ฉ”๋ชจ๋ฆฌ ๋ฒ„ํผ ํฌ๊ธฐ์˜ ๋น„์œจ์„ ๋„˜์–ด์„œ๋ฉด ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๋Š”๋ฐ ์ด ๋น„์œจ์„ ์ง€์ •ํ•ด ์ฃผ๋Š” ์„ค์ •๊ฐ’ mapreduce.reduce.memory.total.bytes * mapreduce.reduce.shuffle.input.buffer.percent์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๋„˜์–ด์„œ๋ฉด ํŒŒ์ผ๋กœ ์ €์žฅ ๊ธฐ๋ณธ๊ฐ’์€ 0.7 mapreduce.reduce.shuffle.memory.limit.percent ๋ฉ”๋ชจ๋ฆฌ ๋ฒ„ํผ์˜ ํฌ๊ธฐ์— ๋น„ํ•ด ํŒŒ์ผ์˜ ๋น„์œจ์ด ์ด ์„ค์ •์„ ๋„˜์–ด์„œ๋ฉด ๋ฐ”๋กœ ๋””์Šคํฌ์— ์“ฐ์ž„ mapreduce.reduce.memory.total.bytes * mapreduce.reduce.shuffle.memory.limit.percent ํฌ๊ธฐ๋ฅผ ๋„˜์–ด์„œ๋ฉด ํŒŒ์ผ๋กœ ์ €์žฅ ๊ธฐ๋ณธ๊ฐ’์€ 0.25 <property> <name>mapreduce.reduce.shuffle.parallelcopies</name> <value>20</value> </property> <property> <name>mapreduce.reduce.memory.total.bytes</name> <value>1024MB</value> </property> <property> <name>mapreduce.reduce.shuffle.input.buffer.percent</name> <value>0.7</value> </property> <property> <name>mapreduce.reduce.shuffle.memory.limit.percent/name> <value>0.25</value> </property> ๋งคํผ, ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜ ์„ค์ • ๋งค ํผ์™€ ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์›ํ•˜๋Š” ๋Œ€๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. mapreduce.job.maps ๋งคํผ์˜ ๊ฐœ์ˆ˜ ์„ค์ • mapreduce.job.reduces ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜ ์„ค์ • ์ž…๋ ฅ ์‚ฌ์ด์ฆˆ์— ๋”ฐ๋ผ ๋งคํผ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. mapreduce.input.fileinputformat.split.maxsize ๋งคํผ์— ์ž…๋ ฅ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ์‚ฌ์ด์ฆˆ ์ฒ˜๋ฆฌํ•˜๋ ค๊ณ  ํ•˜๋Š” ์ด size/mapreduce.input.fileinputformat.split.maxsize = ๋งคํผ ๊ฐœ์ˆ˜ mapreduce.input.fileinputformat.split.minsize ๋งคํผ์— ์ž…๋ ฅ ๊ฐ€๋Šฅํ•œ ์ตœ์†Œ ์‚ฌ์ด์ฆˆ ๋‹จ๊ณ„๋ณ„ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • ๋งค๋ฆฌ ๋“€์Šค ๋‹จ๊ณ„์˜ ์ž์„ธํ•œ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  ์ž๋ฃŒ ### YARN (MR2 Included) ์„ค์ • ๊ฐ€์ด๋“œ hadoop ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • YARN & MRv2 ๋ฆฌ์†Œ์Šค ์„ค์ • Hadoop Yarn memory settings in HDInsight YARN ์„ค์ •๊ฐ’ ์„ค์ • ์ •๋ณด ํŠœ๋‹ ์ •๋ณด mapred-site.xml MapReduce Tutorial 4-์„ฑ๋Šฅ ์ตœ์ ํ™” ๋งต๋ฆฌ๋“€์Šค๋Š” ์—ฌ๋Ÿฌ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์น˜๊ธฐ ๋•Œ๋ฌธ์— ํŠน์ • ์„ค์ • ํ•˜๋‚˜๋กœ ์„ฑ๋Šฅ์„ ๋†’์ด๊ธฐ๋Š” ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ž‘์—… ์†๋„๊ฐ€ ๋Š๋ฆฐ ์ด์œ ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์„ ์ˆ˜ ๋•Œ๋ฌธ์— ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •ํ•ด ๋ณด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋งคํผ, ๋ฆฌ๋“€์„œ ์ˆ˜ ์„ค์ • ๋งคํผ์ˆ˜์™€ ๋ฆฌ๋“€์„œ์ˆ˜์— ๋”ฐ๋ผ ์ž‘์—…์˜ ์†๋„๊ฐ€ ๋นจ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งคํผ ํ•˜๋‚˜์— ๋งŽ์€ ํŒŒ์ผ์ด ๋ชฐ๋ฆฌ๊ฑฐ๋‚˜, ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์ž‘์—…์ด์–ด์„œ GC์— ๋งŽ์€ ์‹œ๊ฐ„์ด ๊ฑธ๋ ค์„œ ๊ทธ๋Ÿด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›์ฒœ ๋ฐ์ดํ„ฐ์˜ ์ž…๋ ฅ ์‚ฌ์ด์ฆˆ์— ๋”ฐ๋ผ ๋งคํผ, ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์กฐ์ ˆํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. <property> <name>mapreduce.job.maps</name> <value>100</value> </property> <property> <name>mapreduce.job.reduces</name> <value>50</value> </property> ์ •๋ ฌ ์†์„ฑ ํŠœ๋‹(io.sort.* ํŠœ๋‹) ๋งต ์ž‘์—…์€ ์ž„์‹œ ๊ฒฐ๊ณผ ํŒŒ์ผ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์œผ๋กœ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋กœ์ปฌ ๋””์Šคํฌ์— ์ €์žฅ๋œ ํŒŒ์ผ์ด ์ค„์–ด๋“ค์ˆ˜๋ก ๋งต ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๋ณ‘ํ•ฉ, ๋„คํŠธ์›Œํฌ ์ „์†ก, ๋ฆฌ๋“€์„œ์˜ ๋ณ‘ํ•ฉ ์ž‘์—… ์‹œ๊ฐ„์ด ๋‹จ์ถ•๋ฉ๋‹ˆ๋‹ค. ์Šคํ•„ ๋˜๋Š” ํŒŒ์ผ์„ ์ค„์ด๋ ค๋ฉด ์Šคํ•„์ „ ๋ฉ”๋ชจ๋ฆฌ ๋ฒ„ํผ ํฌ๊ธฐ์ธ io.sort.mb๋ฅผ ๋Š˜์ด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ๋ฒ„ํผ๊ฐ€ ์ปค์ ธ์„œ ๋กœ์ปฌ์— ์ €์žฅ๋  ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ์ค„์–ด๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. <property> <name>mapreduce.task.io.sort.mb</name> <value>200</value> </property> <property> <name>mapreduce.map.sort.spill.percent</name> <value>0.80</value> </property> <property> <name>mapreduce.task.io.sort.factor</name> <value>100</value> </property> ์ปด๋ฐ”์ด๋„ˆ ํด๋ž˜์Šค ์ ์šฉ ์ปด๋ฐ”์ด๋„ˆ๋ฅผ ์ ์šฉํ•˜๋ฉด ๋งต ์ž‘์—…์˜ ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฆฌ๋“€์„œ๋กœ ์ „์†ก๋˜๊ธฐ ์ „์— ์ปด๋ฐ”์ด๋„ˆ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜์—ฌ, ๋ฐ์ดํ„ฐ๋ฅผ ์ค„์—ฌ์„œ ๋„คํŠธ์›Œํฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ด๊ณ  ๋ฆฌ๋“€์„œ์˜ ์ž‘์—… ์†๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); // ์ฝค๋ฐ”์ด๋„ˆ ์ ์šฉ job.setReducerClass(IntSumReducer.class); ๋งต ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ ์••์ถ• ๋งต ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์••์ถ•ํ•˜์—ฌ ๋„คํŠธ์›Œํฌ ํŠธ๋ž˜ํ”ฝ์„ ์ค„์—ฌ ์ฃผ๋ฉด ํ”Œ๋ ˆ์ธ ํ…์ŠคํŠธ๋ฅผ ์ด์šฉํ•  ๋•Œ๋ณด๋‹ค ์†๋„๊ฐ€ ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. <property> <name>mapreduce.map.output.compress</name> <value>true</value> </property> <property> <name>mapreduce.map.output.compress.codec</name> <value>org.apache.hadoop.io.compress.SnappyCodec</value> </property> ์ž‘์€ ํŒŒ์ผ ๋ฌธ์ œ(small file problem) ์ˆ˜์ • ๋„ค์ž„๋…ธ๋“œ๋Š” ํŒŒ์ผ์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์™€ ๋ธ”๋ก์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ž‘์€ ์‚ฌ์ด์ฆˆ์˜ ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ ๊ฐœ ์กด์žฌํ•˜๊ฒŒ ๋˜๋ฉด ์ด ํŒŒ์ผ๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š”๋ฐ ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ฌ์šฉ๋˜๊ณ , ๋งต๋ฆฌ๋“€์Šค ์ž‘์—… ์ฒ˜๋ฆฌ ์ค‘ ๋งŽ์€ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋˜์–ด ๋„ค์ž„๋…ธ๋“œ์— ๋ณ‘๋ชฉํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜์–ด ์ž‘์—… ์†๋„๊ฐ€ ๋Š๋ ค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž‘์€ ์‚ฌ์ด์ฆˆ์˜ ํŒŒ์ผ์„ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ํ•ฉ์ณ์„œ HDFS ๋ธ”๋ก ์‚ฌ์ด์ฆˆ ํฌ๊ธฐ์˜ ํŒŒ์ผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ฃผ์š” ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ์ž‘์€ ์‚ฌ์ด์ฆˆ์˜ ํŒŒ์ผ์„ ํ•ฉ์ณ์„œ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํ•˜๋‘ก์€ har ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ์ž‘์€ ํŒŒ์ผ์„ ๋ฌถ์–ด์„œ ํ•˜๋‚˜์˜ ์••์ถ• ํŒŒ์ผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. hadoop archives har-files-hadoop-archive-files 5-์˜ˆ์ œ ๋งต๋ฆฌ๋“€์Šค ์˜ˆ์ œ๋Š” ๊ณต๊ณต๋ฐ์ดํ„ฐ ํฌํ„ธ์˜ ์ „๊ตญ CCTV ํ‘œ์ค€ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ณต๊ณต๋ฐ์ดํ„ฐ ํฌํ„ธ์—์„œ CSV ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋กœ ๋‹ค์šด๋กœ๋“œ 1 ํ•˜๊ณ , HDFS์— ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. # CSV ๋ฐ์ดํ„ฐ ํ™•์ธ $ hadoop fs -ls /user/cctv/ Found 1 items -rw-r--r-- 2 hadoop hadoop 30805141 2018-10-19 08:11 /user/cctv/data.csv ๊ด€๋ฆฌ ๊ธฐ๊ด€๋ช… ๊ธฐ์ค€์œผ๋กœ ๊ฑด์ˆ˜ ํ™•์ธ ๋งคํผ ๋ฆฌ๋“€์„œ Job ์‹คํ–‰ ๊ฒฐ๊ณผ ๊ด€๋ฆฌ ๊ธฐ๊ด€๋ช…(ํ‚ค) ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๊ฑด์ˆ˜ ํ™•์ธ ํŒŒํ‹ฐ์…”๋„ˆ job ์‹คํ–‰ ๊ฒฐ๊ณผ ๊ด€๋ฆฌ๊ธฐ๊ด€, ์„ค์น˜ ๋ชฉ์  ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๊ฑด์ˆ˜ ํ™•์ธ ๋งคํผ ๋ณตํ•ฉํ‚ค ํŒŒํ‹ฐ์…”๋„ˆ SortComparator GroupingComparator ๋ฆฌ๋“€์„œ Job ์‹คํ–‰ ๊ฒฐ๊ณผ ๊ด€๋ฆฌ ๊ธฐ๊ด€๋ช… ๊ธฐ์ค€์œผ๋กœ ๊ฑด์ˆ˜ ํ™•์ธ ๋งคํผ ์ „๊ตญ CCTV ํ‘œ์ค€ ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์นผ๋Ÿผ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ด€๋ฆฌ ๊ธฐ๊ด€๋ช…์ž…๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค์—์„œ ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ผ์ธ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ, ํƒญ(\t)์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. package com.sec.cctv; import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class CctvMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); @Override protected void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException { // ๊ด€๋ฆฌ ๊ธฐ๊ด€๋ช… ์ถ”์ถœ String[] strs = value.toString().split("\t"); word.set(strs[0]); context.write(word, one); } } ๋ฆฌ๋“€์„œ ๋ฆฌ๋“€์„œ๋กœ๋Š” ๋งคํผ์—์„œ ์“ด ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ๋”ํ•˜์—ฌ ์ฃผ๋ฉด ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. package com.sec.cctv; import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class CctvReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); @Override protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException { int sum = 0; for(IntWritable value : values) sum += value.get(); result.set(sum); context.write(key, result); } } Job ์žก ํ•จ์ˆ˜๋Š” ์ž‘์—…์— ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋งคํผ, ๋ฆฌ๋“€์„œ, ์ปด๋ฐ”์ด๋„ˆ๋ฅผ ์„ค์ •ํ•˜๊ณ  ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. FileInputFormat์— ์ž…๋ ฅ, ์ถœ๋ ฅ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. package com.sec.cctv; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class CctvMain { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "cctv"); job.setJarByClass(CctvMain.class); job.setMapperClass(CctvMapper.class); job.setCombinerClass(CctvReducer.class); job.setReducerClass(CctvReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ์‹คํ–‰ ๊ฒฐ๊ณผ ์šฐ์„  ์œ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ปดํŒŒ์ผํ•˜๊ณ  jar ํŒŒ์ผ๋กœ ๋ฌถ์–ด ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋ฎ์–ด์“ฐ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์žฌ์ž‘์—…์„ ์œ„ํ•ด์„œ๋Š” ์ถœ๋ ฅ ์œ„์น˜๋ฅผ ์ง€์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค. # ์‹คํ–‰ $ hadoop jar cctv.jar com.sec.cctv.CctvMain /user/cctv/ /user/cctv_output/ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋งŒํผ์˜ ๊ฒฐ๊ณผ ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๊ณ , ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•œ _SUCCESS ํด๋”๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๊ฒฐ๊ณผ ํ™•์ธ $ hadoop fs -ls /user/cctv_output/ Found 8 items -rw-r--r-- 2 hadoop hadoop 0 2018-10-19 08:36 /user/cctv_output/_SUCCESS -rw-r--r-- 2 hadoop hadoop 2869 2018-10-19 08:36 /user/cctv_output/part-r-00000 -rw-r--r-- 2 hadoop hadoop 2965 2018-10-19 08:36 /user/cctv_output/part-r-00001 -rw-r--r-- 2 hadoop hadoop 2052 2018-10-19 08:36 /user/cctv_output/part-r-00002 -rw-r--r-- 2 hadoop hadoop 2418 2018-10-19 08:36 /user/cctv_output/part-r-00003 -rw-r--r-- 2 hadoop hadoop 2786 2018-10-19 08:36 /user/cctv_output/part-r-00004 -rw-r--r-- 2 hadoop hadoop 2902 2018-10-19 08:36 /user/cctv_output/part-r-00005 -rw-r--r-- 2 hadoop hadoop 2986 2018-10-19 08:36 /user/cctv_output/part-r-00006 # ํŒŒ์ผ์˜ ๋‚ด์šฉ ํ™•์ธ $ hadoop fs -cat /user/cctv_output/part-r-00003 CCTV ํ†ตํ•ฉ๊ด€์ œ์„ผํ„ฐ 160 ๊ฐ•์›๋žœ๋“œ 56 ๊ฒฝ๊ธฐ๋„ ๊ด‘๋ช…์‹œ ์ •๋ณด ํ†ต์‹ ๊ณผ 342 ๊ฒฝ๊ธฐ๋„ ์‹œํฅ์‹œ์ฒญ 1112 ๊ฒฝ๊ธฐ๋„ ์•ˆ์–‘์‹œ (๊ตํ†ต์ •์ฑ…๊ณผ) 1041 ๊ฒฝ๊ธฐ๋„ ์–‘ํ‰๊ตฐ์ฒญ 997 ๋งต๋ฆฌ๋“€์Šค๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋กœ๊ทธ์— ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉ๋œ ๋ฆฌ์†Œ์Šค ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ๋œ ๋งคํผ, ๋ฆฌ๋“€์„œ, ์ปด๋ฐ”์ด๋„ˆ์˜ ์ˆ˜์™€ ์‚ฌ์šฉ๋Ÿ‰ ๋“ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ hadoop jar cctv.jar com.sec.cctv.CctvMain /user/cctv/ /user/cctv_output/ 18/10/19 09:11:39 INFO impl.TimelineClientImpl: Timeline service address: http://host_url:8188/ws/v1/timeline/ 18/10/19 09:11:40 INFO client.RMProxy: Connecting to ResourceManager at host_url/host_url:8032 18/10/19 09:11:40 WARN mapreduce.JobResourceUploader: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this. 18/10/19 09:11:40 INFO input.FileInputFormat: Total input paths to process : 1 18/10/19 09:11:40 INFO lzo.GPLNativeCodeLoader: Loaded native gpl library 18/10/19 09:11:40 INFO lzo.LzoCodec: Successfully loaded & initialized native-lzo library [hadoop-lzo rev 418fa8c602f2a4b153c1a89806305f 6b5a27a524] 18/10/19 09:11:40 INFO mapreduce.JobSubmitter: number of splits:1 18/10/19 09:11:40 INFO Configuration.deprecation: mapred.job.queue.name is deprecated. Instead, use mapreduce.job.queuename 18/10/19 09:11:40 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1520227878653_30488 18/10/19 09:11:40 INFO impl.YarnClientImpl: Submitted application application_1520227878653_30488 18/10/19 09:11:40 INFO mapreduce.Job: The url to track the job: http://host_url:20888/proxy/application_1520227878653_30488/ 18/10/19 09:11:40 INFO mapreduce.Job: Running job: job_1520227878653_30488 18/10/19 09:11:45 INFO mapreduce.Job: Job job_1520227878653_30488 running in uber mode : false 18/10/19 09:11:45 INFO mapreduce.Job: map 0% reduce 0% 18/10/19 09:11:50 INFO mapreduce.Job: map 100% reduce 0% 18/10/19 09:11:54 INFO mapreduce.Job: map 100% reduce 29% 18/10/19 09:11:55 INFO mapreduce.Job: map 100% reduce 43% 18/10/19 09:11:56 INFO mapreduce.Job: map 100% reduce 86% 18/10/19 09:11:57 INFO mapreduce.Job: map 100% reduce 100% 18/10/19 09:11:57 INFO mapreduce.Job: Job job_1520227878653_30488 completed successfully 18/10/19 09:11:57 INFO mapreduce.Job: Counters: 49 File System Counters FILE: Number of bytes read=12425 FILE: Number of bytes written=1043109 FILE: Number of read operations=0 FILE: Number of large read operations=0 FILE: Number of write operations=0 HDFS: Number of bytes read=30805288 HDFS: Number of bytes written=18978 HDFS: Number of read operations=24 HDFS: Number of large read operations=0 HDFS: Number of write operations=14 Job Counters Launched map tasks=1 Launched reduce tasks=7 Data-local map tasks=1 Total time spent by all maps in occupied slots (ms)=115650 Total time spent by all reduces in occupied slots (ms)=1541250 Total time spent by all map tasks (ms)=2570 Total time spent by all reduce tasks (ms)=17125 Total vcore-milliseconds taken by all map tasks=2570 Total vcore-milliseconds taken by all reduce tasks=17125 Total megabyte-milliseconds taken by all map tasks=3700800 Total megabyte-milliseconds taken by all reduce tasks=49320000 Map-Reduce Framework Map input records=139632 Map output records=139632 Map output bytes=4103862 Map output materialized bytes=12397 Input split bytes=147 Combine input records=139632 Combine output records=635 Reduce input groups=635 Reduce shuffle bytes=12397 Reduce input records=635 Reduce output records=635 Spilled Records=1270 Shuffled Maps =7 Failed Shuffles=0 Merged Map outputs=7 GC time elapsed (ms)=529 CPU time spent (ms)=9210 Physical memory (bytes) snapshot=2002321408 Virtual memory (bytes) snapshot=34572668928 Total committed heap usage (bytes)=1917845504 Shuffle Errors BAD_ID=0 CONNECTION=0 IO_ERROR=0 WRONG_LENGTH=0 WRONG_MAP=0 WRONG_REDUCE=0 File Input Format Counters Bytes Read=30805141 File Output Format Counters Bytes Written=18978 ๊ด€๋ฆฌ ๊ธฐ๊ด€๋ช…(ํ‚ค) ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๊ฑด์ˆ˜ ํ™•์ธ ๋‹ค์Œ ์˜ˆ์ œ๋Š” ๋ฆฌ๋“€์„œ์˜ ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๊ฑด์ˆ˜๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋งคํผ์˜ ๊ฒฐ๊ณผ๋Š” ๋ฆฌ๋“€์„œ๋กœ ์ „๋‹ฌํ•  ๋•Œ ์…”ํ”Œ & ์ •๋ ฌ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋˜์–ด ๋ฆฌ๋“€์„œ์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ํŒŒํ‹ฐ์…”๋„ˆ๊ฐ€ ํ‚ค๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ๊ธฐ์ค€์ด ๋˜๊ณ , ๊ธฐ๋ณธ ํŒŒํ‹ฐ์…”๋„ˆ๋Š” ํ•ด์‹œ ํŒŒํ‹ฐ์…”๋„ˆ ์ž…๋‹ˆ๋‹ค. ๊ด€๋ฆฌ ๊ธฐ๊ด€๋ช… ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด์‹œ ํŒŒํ‹ฐ์…”๋„ˆ๋ฅผ ๋Œ€์ฒดํ•  ํŒŒํ‹ฐ์…”๋„ˆ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ •๋ ฌ์„ ์œ„ํ•ด์„œ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ™์ด ๋ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ•˜๋‚˜๋กœ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜๋ฅผ ํ•˜๋‚˜๋กœ ํ•˜์ง€ ์•Š์œผ๋ฉด ํŒŒ์ผ๋ณ„๋กœ ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…”๋„ˆ ํŒŒํ‹ฐ์…”๋„ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด getPartition() ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ‚ค ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ํŒŒํ‹ฐ์…˜์„ ๋‚˜๋ˆ„๋Š” ๊ธฐ์ค€์ด ๋˜๋„๋ก ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. package com.sec.cctv; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Partitioner; public class CctvPartitioner extends Partitioner<Text, IntWritable> { @Override public int getPartition(Text key, IntWritable value, int numPartitions) { return (key.toString().charAt(0)) % numPartitions; } } job ์žก์—์„œ ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋ฅผ 1๊ฐœ๋กœ ์„ค์ •ํ•˜๊ณ , ํŒŒํ‹ฐ์…”๋„ˆ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. package com.sec.cctv; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class CctvMain { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "cctv"); job.setJarByClass(CctvMain.class); job.setNumReduceTasks(1); job.setMapperClass(CctvMapper.class); job.setCombinerClass(CctvReducer.class); job.setPartitionerClass(CctvPartitioner.class); job.setReducerClass(CctvReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ์‹คํ–‰ ๊ฒฐ๊ณผ ํŒŒํ‹ฐ์…˜์„ ์ถ”๊ฐ€ํ•œ ๋งต๋ฆฌ๋“€์Šค๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ์‹คํ–‰ ๊ฒฐ๊ณผ $ hadoop fs -ls /user/cctv_output/ Found 2 items -rw-r--r-- 2 hadoop hadoop 0 2018-10-19 09:59 /user/cctv_output/_SUCCESS -rw-r--r-- 2 hadoop hadoop 18978 2018-10-19 09:59 /user/cctv_output/part-r-00000 # ํŒŒ์ผ ๋‚ด์šฉ $ hadoop fs -cat /user/cctv_output/part-r-00000 CCTV ํ†ตํ•ฉ๊ด€์ œ์„ผํ„ฐ 160 ๊ฐ€์˜ค๋ฆฌ 1 ๊ฐ€ํ‰๊ตฐ์ฒญ 9 ๊ฐ•๋ฆ‰ ์‹œ์ฒญ 1 ๊ฐ•๋ถ ๊ณต์˜์ฃผ์ฐจ์žฅ 4 ๊ฐ•๋ถ๊ตฌ 2 ๊ฐ•๋ถ๋ฌธํ™” ์˜ˆ์ˆ  ํšŒ๊ด€ 1 ๊ฐ•๋ถ๋ฌธํ™” ์ •๋ณด๋„์„œ๊ด€ 1 ๊ฐ•๋ถ์›ฐ๋น™์Šคํฌ์ธ ์„ผํ„ฐ ๊ด€๋ฆฌ๊ธฐ๊ด€, ์„ค์น˜ ๋ชฉ์  ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๊ฑด์ˆ˜ ํ™•์ธ ์ด๋ฒˆ์—๋Š” ๋ณตํ•ฉํ‚ค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ด€๋ฆฌ๊ธฐ๊ด€๊ณผ ์„ค์น˜ ๋ชฉ์ ์„ ์ด์šฉํ•˜์—ฌ ๊ด€๋ฆฌ๊ธฐ๊ด€๋ณ„ ์„ค์น˜ ๋ชฉ์  ๊ฑด์ˆ˜์™€ ๋ชจ๋“  CCTV์˜ ๊ฑด์ˆ˜๋ฅผ ์กฐํšŒํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด๊ณผ ๊ฐ™์ด ๊ด€๋ฆฌ๊ธฐ๊ด€์„ ํ‚ค๋กœ ํ•˜๋ฉด ์„ค์น˜ ๋ชฉ์ ์„ ๋ฆฌ๋“€์„œ๋ณ„๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ด€๋ฆฌ๊ธฐ๊ด€๊ณผ ์„ค์น˜ ๋ชฉ์ ์„ ์—ฐ๊ฒฐํ•˜์—ฌ ํ•˜๋‚˜์˜ ํ‚ค(ex: ๊ด€๋ฆฌ๊ธฐ๊ด€_์„ค์น˜ ๋ชฉ์ )๋กœ ์ด์šฉํ•˜๋ฉด, ์„œ๋กœ ๋‹ค๋ฅธ ๋ฆฌ๋“€์„œ๋กœ ๋ณด๋‚ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” ์„ธ์ปจ๋”๋ฆฌ ์†ŒํŠธ(Secondary Sort)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ์ปจ๋”๋ฆฌ ์†ŒํŠธ๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฆฌ๋“€์„œ์˜ ์ „์ฒด ์ž…๋ ฅ์„ ์ •๋ ฌํ•˜๋Š” ๊ธฐ์ค€์ด ๋˜๋Š” SortComparator์™€ ๋ฆฌ๋“€์„œ์˜ reduce() ๋ฉ”์„œ๋“œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ Iterable(values)๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๊ทธ๋ฃนํ•‘ ๊ธฐ์ค€์ด ๋˜๋Š” GroupingComparator๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งคํผ์˜ ์ถœ๋ ฅ์œผ๋กœ <<๊ด€๋ฆฌ๊ธฐ๊ด€, ๋ชฉ์ >, ๋ชฉ์ >๊ณผ๊ฐ™์ด ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…”๋„ˆ๋Š” ๋ณตํ•ฉํ‚ค ์ค‘ ๊ด€๋ฆฌ๊ธฐ๊ด€์„ ๊ธฐ์ค€์œผ๋กœ ๋ฆฌ๋“€์„œ๋ฅผ ๋‚˜๋ˆ„์–ด์ค๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ๋Š” ๋ณตํ•ฉํ‚ค๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ด€๋ฆฌ๊ธฐ๊ด€, ๋ชฉ์  ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฆฌ๋“€์„œ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธฐ๋Š” ๊ทธ๋ฃนํ•‘ ๊ธฐ์ค€์„ ๋ณตํ•ฉํ‚ค์˜ ๊ด€๋ฆฌ๊ธฐ๊ด€์„ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ๋Š” ์ž…๋ ฅ์œผ๋กœ <<๊ด€๋ฆฌ๊ธฐ๊ด€, ๋ชฉ์ >, List<๋ชฉ์ >>์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ชฉ์ ์˜ ์œ ๋‹ˆํฌํ•œ ๊ฐ’๊ณผ ๋ฆฌ์ŠคํŠธ์˜ ์ „์ฒด ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋ฉด ๊ด€๋ฆฌ๊ธฐ๊ด€๋ณ„ ์„ค์น˜ ๋ชฉ์ ์˜ ๊ฑด์ˆ˜์™€ ๋ชจ๋“  CCTV์˜ ๊ฑด์ˆ˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. # ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์‹ญ ๋ฆฌ ๋Œ€๋ฐญ์ถ•๊ตฌ์žฅ 1 4 # ๋‹ค๋ชฉ์  1๊ฐœ์˜ ์šฉ๋„๋กœ 4๊ฐœ์˜ CCTV๋ฅผ ๊ฐ€์ง ํ•ฉ์ฒœ๊ตฐ์ฒญ 6 326 ๋งคํผ ๋งคํผ์—์„œ๋Š” ๊ด€๋ฆฌ๊ธฐ๊ด€๊ณผ ์„ค์น˜ ๋ชฉ์ ์„ ์ถ”์ถœํ•˜๊ณ , ๋ณตํ•ฉํ‚ค(CctvComparePair)๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋งคํผ์˜ ๋ฐธ๋ฅ˜๋Š” ๋ชฉ์ ์„ ์ž…๋ ฅํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. package com.sec.cctv_2; import java.io.IOException; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class CctvMapper extends Mapper<Object, Text, CctvComparePair, Text> { private final static Text one = new Text(); @Override protected void map(Object key, Text value, Mapper<Object, Text, CctvComparePair, Text>.Context context) throws IOException, InterruptedException { String[] strs = value.toString().split("\t"); String admin = strs[0]; // ๊ด€๋ฆฌ ๊ธฐ๊ด€๋ช… ์ถ”์ถœ String purpose = strs[3]; // ์„ค์น˜ ๋ชฉ์  ์ถ”์ถœ CctvComparePair pair = new CctvComparePair(admin, purpose); one.set(purpose); context.write(pair, one); } } ๋ณตํ•ฉํ‚ค ๋ณตํ•ฉ ํ‚ค๋Š” ๊ด€๋ฆฌ๊ธฐ๊ด€๊ณผ ์„ค์น˜ ๋ชฉ์ ์„ ๊ฐ™์ด ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒ์„ฑํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ๋น„๊ต ์—ฐ์‚ฐ๊ณผ ์“ฐ๊ธฐ(write)๋ฅผ ์œ„ํ•ด์„œ WritableComparable์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. compareTo()๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ๋ณตํ•ฉํ‚ค ๋น„๊ต์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค. package com.sec.cctv_2; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import org.apache.hadoop.io.WritableComparable; /** * ์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋Š” ์„ธ์ปจ๋”๋ฆฌ ํ‚ค๋ฅผ ํฌํ•จํ•˜๋Š” ๋ณตํ•ฉ ํด๋ž˜์Šค * @author User * */ public class CctvComparePair implements WritableComparable<CctvComparePair> { private String admin; private String purpose; public CctvComparePair() { } public CctvComparePair(String admin, String road) { super(); this.admin = admin; this.purpose = road; } @Override public void write(DataOutput out) throws IOException { out.writeUTF(admin); out.writeUTF(purpose); } @Override public void readFields(DataInput in) throws IOException { admin = in.readUTF(); purpose = in.readUTF(); } @Override public int compareTo(CctvComparePair key) { int result = admin.compareTo(key.admin); if (result == 0) { result = purpose.compareTo(key.purpose); } return result; } @Override public String toString() { return new StringBuffer().append(admin).append("\t").append(purpose).toString(); } public String getAdmin() { return admin; } public void setAdmin(String admin) { this.admin = admin; } public String getPurpose() { return purpose; } public void setPurpose(String road) { this.purpose = road; } } ํŒŒํ‹ฐ์…”๋„ˆ ํŒŒํ‹ฐ์…”๋„ˆ๋Š” ๋ฆฌ๋“€์„œ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ์ค€์ด ๋˜๋ฏ€๋กœ ๊ด€๋ฆฌ๊ธฐ๊ด€์„ ๊ธฐ์ค€์œผ๋กœ ๋ฆฌ๋“€์„œ๋ฅผ ๊ตฌ๋ถ„ํ•ด ์ค๋‹ˆ๋‹ค. package com.sec.cctv_2; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Partitioner; /** * ๊ด€๋ฆฌ ๊ธฐ๊ด€์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋Š” ์ปค์Šคํ…€ ํŒŒํ‹ฐ์…”๋„ˆ * * @author User * */ public class CctvPartitioner extends Partitioner<CctvComparePair, Text> { @Override public int getPartition(CctvComparePair key, Text value, int numPartitions) { return (key.getAdmin().charAt(0)) % numPartitions; } } SortComparator ๋ฆฌ๋“€์„œ์˜ ์ „์ฒด ์ž…๋ ฅ์„ ์ •๋ ฌํ•˜๋Š” ๊ธฐ์ค€์ด ๋˜๋Š” SortComparator๋ฅผ ๊ตฌํ˜„ํ•ด ์ค๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ์˜ ์ „์ฒด ์ž…๋ ฅ์€ ๊ด€๋ฆฌ๊ธฐ๊ด€, ์„ค์น˜ ๋ชฉ์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. package com.sec.cctv_2; import org.apache.hadoop.io.WritableComparable; import org.apache.hadoop.io.WritableComparator; /** * ๋ฆฌ๋“€์„œ์˜ ์ „์ฒด ์ž…๋ ฅ์„ ์ •๋ ฌํ•˜๋Š” ๊ธฐ์ค€์ด ๋˜๋Š” SortComparator * * @author User * */ public class CctvSortComparator extends WritableComparator { public CctvSortComparator() { super(CctvComparePair.class, true); } @Override public int compare(WritableComparable a, WritableComparable b) { CctvComparePair x = (CctvComparePair) a; CctvComparePair y = (CctvComparePair) b; return x.compareTo(y); } } GroupingComparator ๋ฆฌ๋“€์„œ์—์„œ ์‹ค์ œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋Š” reduce(KEYIN key, Iterable values, Context context) ๋ฉ”์„œ๋“œ์— values ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ๊ทธ๋ฃนํ•‘์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ด€๋ฆฌ๊ธฐ๊ด€์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ•‘์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ๊ด€๋ฆฌ ๊ธฐ๊ด€๋ช…์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ reduce() ๋ฉ”์„œ๋“œ์—๋Š” ๊ด€๋ฆฌ๊ธฐ๊ด€์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ List<์„ค์น˜ ๋ชฉ์ > ๊ฐ’์ด ์ „๋‹ฌ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. package com.sec.cctv_2; import org.apache.hadoop.io.WritableComparable; import org.apache.hadoop.io.WritableComparator; /** * reduce()์˜ values๋ฅผ ๊ทธ๋ฃนํ•‘ํ•˜๋Š” ์ปค์Šคํ…€ ๊ทธ๋ฃนํ•‘ Comparator * * @author User * */ public class CctvGroupingComparator extends WritableComparator { public CctvGroupingComparator() { super(CctvComparePair.class, true); } @Override public int compare(WritableComparable a, WritableComparable b) { CctvComparePair x = (CctvComparePair) a; CctvComparePair y = (CctvComparePair) b; return x.getAdmin().compareTo(y.getAdmin()); } } ๋ฆฌ๋“€์„œ ๋ฆฌ๋“€์„œ๋Š” ๊ด€๋ฆฌ์ฃผ์ฒด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ List<๋ชฉ์ > ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์œ ๋‹ˆํฌํ•œ ๋ชฉ์ ์˜ ๊ฐœ์ˆ˜(๊ด€๋ฆฌ์ฃผ์ฒด๋ณ„ ๋ชฉ์ ), ์ „์ฒด ๋ชฉ์ ์˜ ๊ฐœ์ˆ˜(CCTV์˜ ๊ฐœ์ˆ˜)๋ฅผ ์„ธ์–ด์„œ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. package com.sec.cctv_2; import java.io.IOException; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class CctvReducer extends Reducer<CctvComparePair, Text, Text, Text> { private Text outputKey = new Text(); private Text outputValue = new Text(); @Override protected void reduce(CctvComparePair key, Iterable<Text> values, Reducer<CctvComparePair, Text, Text, Text>.Context context) throws IOException, InterruptedException { int uniq = 0; int sum = 0; String previous = ""; for (Text value : values) { String current = value.toString(); if (!previous.equals(current)) { uniq++; previous = current; } sum++; } outputKey.set(key.getAdmin()); outputValue.set(new StringBuffer().append(uniq).append("\t").append(sum).toString()); context.write(outputKey, outputValue); } } Job ์žก์—์„œ๋Š” ํŒŒํ‹ฐ์…˜, ์†ŒํŠธ, ๊ทธ๋ฃนํ•‘์„ ์œ„ํ•œ ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•˜๊ณ , ์ด๋ฒˆ์—๋Š” ๋งคํผ์˜ ์ถœ๋ ฅ๊ณผ ๋ฆฌ๋“€์„œ์˜ ์ถœ๋ ฅ์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ๊ฐ์˜ ๊ฐ’์„ ์„ค์ •ํ•ด ์ฃผ๋Š” ์ž‘์—…์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. package com.sec.cctv_2; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class CctvMain { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "cctv"); job.setJarByClass(CctvMain.class); // ๋งคํผ ๋ฆฌ๋“€์„œ job.setMapperClass(CctvMapper.class); job.setReducerClass(CctvReducer.class); // ํŒŒํ‹ฐ์…”๋„ˆ, ์†ŒํŠธ, ๊ทธ๋ฃนํ•‘ job.setPartitionerClass(CctvPartitioner.class); job.setSortComparatorClass(CctvSortComparator.class); job.setGroupingComparatorClass(CctvGroupingComparator.class); // ๋งต์˜ ์ถœ๋ ฅ job.setMapOutputKeyClass(CctvComparePair.class); job.setMapOutputValueClass(Text.class); // ๋ฆฌ๋“€์„œ์˜ ์ถœ๋ ฅ job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ์‹คํ–‰ ๊ฒฐ๊ณผ ์ž‘์—…์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ hadoop fs -ls /user/cctv_2_output/ Found 8 items -rw-r--r-- 2 hadoop hadoop 0 2018-10-23 07:15 /user/cctv_2_output/_SUCCESS -rw-r--r-- 2 hadoop hadoop 0 2018-10-23 07:15 /user/cctv_2_output/part-r-00000 -rw-r--r-- 2 hadoop hadoop 0 2018-10-23 07:15 /user/cctv_2_output/part-r-00001 -rw-r--r-- 2 hadoop hadoop 16402 2018-10-23 07:15 /user/cctv_2_output/part-r-00002 -rw-r--r-- 2 hadoop hadoop 850 2018-10-23 07:15 /user/cctv_2_output/part-r-00003 -rw-r--r-- 2 hadoop hadoop 481 2018-10-23 07:15 /user/cctv_2_output/part-r-00004 -rw-r--r-- 2 hadoop hadoop 45 2018-10-23 07:15 /user/cctv_2_output/part-r-00005 -rw-r--r-- 2 hadoop hadoop 2473 2018-10-23 07:15 /user/cctv_2_output/part-r-00006 $ hadoop fs -cat /user/cctv_2_output/part-r-00005 ์ถฉ์ฒญ๋ถ๋„ ๋‹จ์–‘๊ตฐ์ฒญ 3 184 ์ถฉ์ฒญ๋ถ๋„ ๋ณด์€๊ตฐ์ฒญ 5 201 ์ถฉ์ฒญ๋ถ๋„ ์˜๋™๊ตฐ์ฒญ 8 244 ์ถฉ์ฒญ๋ถ๋„ ์˜ฅ์ฒœ๊ตฐ์ฒญ 3 278 CSV ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๋ฉด euc-kr ์ธ์ฝ”๋”ฉ์ด๋ผ์„œ ํ•˜๋‘ก์—์„œ ํ•œ๊ธ€์ด ๊นจ์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œํ•˜์‹œ๋ฉด ๋ฉ”๋ชจ์žฅ ๋“ฑ์„ ์ด์šฉํ•˜์—ฌ UTF-8 ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ฃผ์‹ญ์‹œ์˜ค. โ†ฉ 1-๋งต๋ฆฌ๋“€์Šค ๊ธฐ๋ณธ ์˜ˆ์ œ ํ•˜๋‘ก์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋งต๋ฆฌ๋“€์Šค ์˜ˆ์ œ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋“ค์„ ์ด์šฉํ•˜์—ฌ ํ•˜๋‘ก์˜ ์„ค์น˜ ์ƒํƒœ, ์„ฑ๋Šฅ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pi pi ๊ฐ’์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. export HADOOP_ROOT_LOGGER=INFO, console hadoop jar /opt/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.10.0.jar \ pi 32 10000000 4-YARN YARN(Yet Another Resource Negotiator)์€ ํ•˜๋‘ก 2์—์„œ ๋„์ž…ํ•œ ํด๋Ÿฌ์Šคํ„ฐ ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ ๋ฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ผ์ดํ”„ ์‚ฌ์ดํด ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋ฐฐ๊ฒฝ ํ•˜๋‘ก 1์—์„œ๋Š” ์žกํŠธ๋ž˜์ปค๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ผ์ดํ”„ ์‚ฌ์ดํด ๊ด€๋ฆฌ์™€ ํด๋Ÿฌ์Šคํ„ฐ ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋‹ด๋‹นํ•˜์—ฌ ๋ณ‘๋ชฉ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์žกํŠธ๋ž˜์ปค ํ•œ ๋Œ€๊ฐ€ ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๊ด€๋ฆฌํ•ด์•ผ ํ•˜๊ณ , ๋ชจ๋“  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ด€๋ฆฌํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ์žกํŠธ๋ž˜์ปค์— ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•ด์•ผ ํ–ˆ๊ณ , ์ตœ๋Œ€ 4000๋Œ€์˜ ๋…ธ๋“œ๊นŒ์ง€ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์žกํŠธ๋ž˜์ปค๋Š” ์Šฌ๋กฏ ๋‹จ์œ„๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ด€๋ฆฌํ•˜์—ฌ ์‹œ์Šคํ…œ์˜ ์ „์ฒด ์ž์›์„ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ์Šฌ๋กฏ ๋‹จ์œ„ ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ๋Š” ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ, CPU ์ž์›์„ ๋ถ„ํ• ํ•˜์—ฌ ์Šฌ๋กฏ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. 100GB์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€์ง€๋Š” ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ 1G๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ 100๊ฐœ์˜ ์Šฌ๋กฏ์„ ๋งŒ๋“ค๊ณ , 60๊ฐœ์˜ ๋งต ์Šฌ๋กฏ, 40๊ฐœ์˜ ๋ฆฌ๋“€์„œ ์Šฌ๋กฏ์œผ๋กœ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ์Šฌ๋กฏ์€ ๊ฐ๊ฐ์˜ ์—ญํ• ์— ๋งž๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋งต ์Šฌ๋กฏ์ด ๋™์ž‘ํ•˜๋Š” ๋™์•ˆ ๋ฆฌ๋“€์„œ ์Šฌ๋กฏ์€ ๋Œ€๊ธฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งต ์Šฌ๋กฏ์— ๋” ๋งŽ์€ ์ผ์„ ํ•˜๊ฒŒ ๋˜๋”๋ผ๋„ ๋ฆฌ๋“€์„œ ์Šฌ๋กฏ์€ ๋Œ€๊ธฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์žก ํŠธ๋ž˜์ปค์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…๋งŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด์„œ ์œ ์—ฐ์„ฑ์ด ๋ถ€์กฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค API๋ฅผ ๊ตฌํ˜„ํ•œ ์ž‘์—…๋งŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— SQL ๊ธฐ๋ฐ˜ ์ž‘์—…์˜ ์ฒ˜๋ฆฌ๋‚˜, ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜์˜ ์ž‘์—…์˜ ์ฒ˜๋ฆฌ์— ์–ด๋ ค์›€์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋‹จ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•˜์—ฌ YARN ์•„ํ‚คํ…์ฒ˜๊ฐ€ ๋„์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค. YARN ๊ตฌ์„ฑ YARN์€ ์žกํŠธ๋ž˜์ปค์˜ ๊ธฐ๋Šฅ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์ž์› ๊ด€๋ฆฌ๋Š” ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์™€ ๋…ธ๋“œ ๋งค๋‹ˆ์ €, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ผ์ดํ”„ ์‚ฌ์ดํด ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์™€ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋‹ด๋‹นํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ž์›๊ด€๋ฆฌ ํด๋Ÿฌ์Šคํ„ฐ ์ž์› ๊ด€๋ฆฌ๋Š” ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €(ResourceManager)์™€ ๋…ธ๋“œ ๋งค๋‹ˆ์ €(NodeManager)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋…ธ๋“œ ๋งค๋‹ˆ์ €๋Š” ํด๋Ÿฌ์Šคํ„ฐ์˜ ๊ฐ ๋…ธ๋“œ๋งˆ๋‹ค ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ๋…ธ๋“œ์˜ ์ž์› ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ , ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์— ํ˜„์žฌ ์ž์› ์ƒํƒœ๋ฅผ ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋Š” ๋…ธ๋“œ ๋งค๋‹ˆ์ €๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ํด๋Ÿฌ์Šคํ„ฐ ์ „์ฒด์˜ ์ž์›์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ž์› ์‚ฌ์šฉ ์ƒํƒœ๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์—์„œ ์ž ์ž์›์„ ์š”์ฒญํ•˜๋ฉด ๋น„์–ด ์žˆ๋Š” ์ž์›์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ž์›์„ ๋ถ„๋ฐฐํ•˜๋Š” ๊ทœ์น™์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์Šค์ผ€์ค„๋Ÿฌ(Scheduler)์ž…๋‹ˆ๋‹ค. ์Šค์ผ€์ค„๋Ÿฌ์— ์„ค์ •๋œ ๊ทœ์น™์— ๋”ฐ๋ผ ์ž์›์„ ํšจ์œจ์ ์œผ๋กœ ๋ถ„๋ฐฐํ•ฉ๋‹ˆ๋‹ค. ๋ผ์ดํ”„์‚ฌ์ดํด ๊ด€๋ฆฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ผ์ดํ”„ ์‚ฌ์ดํด ๊ด€๋ฆฌ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ(Application Master)์™€ ์ปจํ…Œ์ด๋„ˆ(Container)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ œ์ถœํ•˜๋ฉด, ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋Š” ๋น„์–ด ์žˆ๋Š” ๋…ธ๋“œ์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๋Š” ์ž‘์—… ์‹คํ–‰์„ ์œ„ํ•œ ์ž์›์„ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์— ์š”์ฒญํ•˜๊ณ , ์ž์›์„ ํ• ๋‹น๋ฐ›์•„์„œ ๊ฐ ๋…ธ๋“œ์— ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ์‹ค์ œ ์ž‘์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ๋Š” ์‹ค์ œ ์ž‘์—…์ด ์‹คํ–‰๋˜๋Š” ๋‹จ์œ„์ž…๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ž‘์—…์ด ์ข…๋ฃŒ๋˜๋ฉด ๊ฒฐ๊ณผ๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์—๊ฒŒ ์•Œ๋ฆฌ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๋Š” ๋ชจ๋“  ์ž‘์—…์ด ์ข…๋ฃŒ๋˜๋ฉด ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์— ์•Œ๋ฆฌ๊ณ  ์ž์›์„ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ YARN์—์„œ๋Š” ๋งต๋ฆฌ๋“€์Šค API๋กœ ๊ตฌํ˜„๋œ ํ”„๋กœ๊ทธ๋žจ ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค, ํ”ผ๊ทธ, ์Šคํ†ฐ, ์ŠคํŒŒํฌ ๋“ฑ ํ•˜๋‘ก ์—์ฝ” ์‹œ์Šคํ…œ์˜ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๊ธฐ์ˆ ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๋Œ€ 1๋งŒ ๊ฐœ์˜ ๋…ธ๋“œ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ ๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•˜์—ฌ 1๋งŒ ๋Œ€ ์ด์ƒ์˜ ํด๋Ÿฌ์Šคํ„ฐ๋กœ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ™•์žฅ์„ฑ์„ ์ œ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค. 1-YARN-Scheduler ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋Š” ํด๋Ÿฌ์Šคํ„ฐ ์ž์›์„ ๊ด€๋ฆฌํ•˜๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์˜ ์š”์ฒญ์„ ๋ฐ›์•„์„œ ์ž์›์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ž์› ํ• ๋‹น์„ ์œ„ํ•œ ์ •์ฑ…์„ ์Šค์ผ€์ค„๋Ÿฌ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‘ก์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ์Šค์ผ€์ค„๋Ÿฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ”ผํฌ(FIFO) ์Šค์ผ€์ค„๋Ÿฌ ํŽ˜์–ด(Fair) ์Šค์ผ€์ค„๋Ÿฌ ์ปคํŒจ์‹œํ‹ฐ(Capacity) ์Šค์ผ€์ค„๋Ÿฌ ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • ์Šค์ผ€์ค„๋Ÿฌ๋Š” yarn-site.xml ํŒŒ์ผ์— ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. yarn.resourcemanager.scheduler.class์— ๋‹ค์Œ์˜ ํด๋ž˜์Šค๋ช…์„ ์ ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์Šค์ผ€์ค„๋Ÿฌ ํด๋ž˜์Šค๋ช… FIFO ์Šค์ผ€์ค„๋Ÿฌ org.apache.hadoop.yarn.server.resourcemanager.scheduler.fifo.FifoScheduler Fair ์Šค์ผ€์ค„๋Ÿฌ org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler Capacity ์Šค์ผ€์ค„๋Ÿฌ org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. <property> <name>yarn.resourcemanager.scheduler.class</name> <value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler</value> </property> FIFO Scheduler FIFO ์Šค์ผ€์ค„๋Ÿฌ๋Š” ์ด๋ฆ„๊ณผ ๊ฐ™์ด ๋จผ์ € ๋“ค์–ด์˜จ ์ž‘์—…์ด ๋จผ์ € ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ž‘์—…์˜ ์ œ์ถœ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌ๋˜๊ณ , ๋จผ์ € ๋“ค์–ด์˜จ ์ž‘์—…์ด ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ์ž‘์—…์€ ๋Œ€๊ธฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ์ž์›์„ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— FIFO ์Šค์ผ€์ค„๋Ÿฌ๋Š” ํ…Œ์ŠคํŠธ ๋ชฉ์ ์œผ๋กœ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. Fair Scheduler Fair ์Šค์ผ€์ค„๋Ÿฌ๋Š” ์ œ์ถœ๋œ ์ž‘์—…์ด ๋™๋“ฑํ•˜๊ฒŒ ๋ฆฌ์†Œ์Šค๋ฅผ ์ ์œ ํ•ฉ๋‹ˆ๋‹ค. ์ž‘์—… ํ์— ์ž‘์—…์ด ์ œ์ถœ๋˜๋ฉด ํด๋Ÿฌ์Šคํ„ฐ๋Š” ์ž์›์„ ์กฐ์ ˆํ•˜์—ฌ ์ž‘์—…์— ๊ท ๋“ฑํ•˜๊ฒŒ ์ž์›์„ ํ• ๋‹นํ•˜์—ฌ ์ค๋‹ˆ๋‹ค. Capacity Scheduler ํ•˜๋‘ก 2์˜ ๊ธฐ๋ณธ ์Šค์ผ€์ค„๋Ÿฌ์ž…๋‹ˆ๋‹ค. ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ํ๋ฅผ ์„ ์–ธํ•˜๊ณ  ๊ฐ ํ ๋ณ„๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž์›์˜ ์šฉ๋Ÿ‰์„ ์ •ํ•˜์—ฌ ์ฃผ๋ฉด ๊ทธ ์šฉ๋Ÿ‰์— ๋งž๊ฒŒ ์ž์›์„ ํ• ๋‹นํ•˜์—ฌ ์ค๋‹ˆ๋‹ค. ์ฐธ๊ณ  Fair scheduler ์„ค์ •: ๋ฐ”๋กœ ๊ฐ€๊ธฐ Capacity scheduler ์„ค์ •: ๋ฐ”๋กœ ๊ฐ€๊ธฐ 1-์ปคํŒจ์‹œํ‹ฐ ์Šค์ผ€์ค„๋Ÿฌ(Capacity Scheduler) ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ •๊ฐ’ capacity-scheduler.xml ์ฃผ์š” ์„ค์ • ์‚ฌ์šฉ์ž ํ ๋งคํ•‘(queue-mappings) ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • ํ์˜ ๊ณ„์ธต ๊ตฌ์กฐ capacity-scheduler.xml ์„ค์ • ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • ํ™•์ธ ํ ์„ค์ • ๋ณ€๊ฒฝ ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • ์‹œ ์ฃผ์˜ ์‚ฌํ•ญ ์ฐธ๊ณ  ํ•˜๋‘ก 2์˜ ๊ธฐ๋ณธ ์Šค์ผ€์ค„๋Ÿฌ์ธ ์ปคํŒจ์‹œํ‹ฐ ์Šค์ผ€์ค„๋Ÿฌ(Capacity Scheduler)์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ปคํŒจ์‹œํ‹ฐ ์Šค์ผ€์ค„๋Ÿฌ๋Š” ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ๊ณ„์ธตํ™”๋œ ํ๋ฅผ ์„ ์–ธํ•˜๊ณ , ํ๋ณ„๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์šฉ๋Ÿ‰์„ ํ• ๋‹นํ•˜์—ฌ ์ž์›์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 100G์˜ ๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰์„ ๊ฐ€์ง€๋Š” ํด๋Ÿฌ์Šคํ„ฐ์—์„œ A, B ๋‘ ๊ฐœ์˜ ํ์— ๊ฐ๊ฐ 40%, 60%์˜ ์šฉ๋Ÿ‰(capacity)๋ฅผ ์„ค์ •ํ•˜๋ฉด A ํ๋Š” 40G, B ํ๋Š” 60G์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ž์›์— ์—ฌ์œ ๊ฐ€ ์žˆ๋‹ค๋ฉด ์„ค์ •์„ ์ด์šฉํ•˜์—ฌ ๊ฐ ํ์— ์„ค์ •๋œ ์šฉ๋Ÿ‰ ์ด์ƒ์˜ ์ž์›์„ ์ด์šฉํ•˜๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์šด์˜ ์ค‘์—๋„ ํ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์—ฐ์„ฑ๋„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ •๊ฐ’ ์ปคํŒจ์‹œํ‹ฐ ์Šค์ผ€์ค„๋Ÿฌ์˜ ์ฃผ์š” ์„ค์ •๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์„ค์ •๊ฐ’ ๋น„๊ณ  yarn.scheduler.capacity.maximum-applications PRE, RUNNIG ์ƒํƒœ๋กœ ์„ค์ •๋  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฐœ์ˆ˜ yarn.scheduler.capacity.maximum-am-resource-percent ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ(AM)์— ํ• ๋‹น ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ๋น„์œจ. AM์€ ์‹ค์ œ ์ž‘์—…์ด ๋Œ์ง€ ์•Š๊ณ  ์ž‘์—…์„ ๊ด€๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์—…์— ๋งŽ์€ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด ์ด ๊ฐ’์„ ์ ๋‹นํžˆ ์กฐ์ ˆํ•ด์•ผ ํ•จ yarn.scheduler.capacity.root.queues root ํ์— ๋“ฑ๋กํ•˜๋Š” ํ์˜ ์ด๋ฆ„. root ํ๋Š” ํ•˜์œ„์— ๋™๋กํ•  ํ๋ฅผ ์œ„ํ•ด ๋…ผ๋ฆฌ์ ์œผ๋กœ๋งŒ ์กด์žฌ yarn.scheduler.capacity.root.[ํ ์ด๋ฆ„].maximum-am-resource-percent ํ์—์„œ AM์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž์›์˜ ๋น„์œจ yarn.scheduler.capacity.root.[ํ ์ด๋ฆ„].capacity ํ์˜ ์šฉ๋Ÿ‰ ๋น„์œจ yarn.scheduler.capacity.root.[ํ ์ด๋ฆ„].user-limit-factor ํ์— ์„ค์ •๋œ ์šฉ๋Ÿ‰ * limit-factor ๋งŒํผ ๋‹ค๋ฅธ ํ์˜ ์šฉ๋Ÿ‰์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ. ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ž์›์„ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ. maxmimum-capacity ์ด์ƒ์œผ๋กœ๋Š” ์ด์šฉํ•  ์ˆ˜ ์—†์Œ. yarn.scheduler.capacity.root.[ํ ์ด๋ฆ„].maximum-capacity ํ๊ฐ€ ์ตœ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์šฉ๋Ÿ‰ capacity-scheduler.xml ์ฃผ์š” ์„ค์ • <configuration> <property> <name>yarn.scheduler.capacity.maximum-applications</name> <value>10000</value> </property> <property> <name>yarn.scheduler.capacity.maximum-am-resource-percent</name> <value>0.1</value> <description> ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์— ํ• ๋‹น ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ๋น„์œจ. </description> </property> <property> <name>yarn.scheduler.capacity.resource-calculator</name> <value>org.apache.hadoop.yarn.util.resource.DefaultResourceCalculator</value> </property> <property> <name>yarn.scheduler.capacity.root.queues</name> <value>prd, stg</value> <description> The queues at the this level (root is the root queue). </description> </property> <!-- capacity --> <property> <name>yarn.scheduler.capacity.root.prd.capacity</name> <value>80</value> </property> <property> <name>yarn.scheduler.capacity.root.stg.capacity</name> <value>20</value> </property> <!-- user-limit-factor --> <property> <name>yarn.scheduler.capacity.root.prd.user-limit-factor</name> <value>1</value> </property> <property> <name>yarn.scheduler.capacity.root.stg.user-limit-factor</user-limit-factor</name> <value>2</value> </property> <!-- maximum-capacity --> <property> <name>yarn.scheduler.capacity.root.prd.maximum-capacity</name> <value>100</value> </property> <property> <name>yarn.scheduler.capacity.root.stg.maximum-capacity</name> <value>30</value> </property> </configuration> ์‚ฌ์šฉ์ž ํ ๋งคํ•‘(queue-mappings) ์‚ฌ์šฉ์ž๊ฐ€ ํ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„, ์ž๋™์œผ๋กœ ์‚ฌ์šฉ์ž์™€ ํ๊ฐ€ ๋งคํ•‘ ๋˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. u:์œ ์ €๋ช…:ํ ์œ ์ €์™€ ํ๋ฅผ ๋งคํ•‘ g:๊ทธ๋ฃน๋ช…:ํ ๊ทธ๋ฃน๊ณผ ํ๋ฅผ ๋งคํ•‘ u:%user:%user ์œ ์ €๋ฅผ ์œ ์ €๋ช… ํ์— ๋งคํ•‘ u:%user:%primary_group ์œ ์ €๋ฅผ ์œ ์ €์˜ ํ”„๋ผ์ด๋จธ๋ฆฌ ๊ทธ๋ฃน๋ช… ํ์— ๋งคํ•‘ queue-mappings ์„ค์ •์— ์ •์˜๋œ ์ˆœ์„œ์— ๋”ฐ๋ผ ํ๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. <property> <name>yarn.scheduler.capacity.queue-mappings</name> <value>u:user1:queue1, g:group1:queue2, u:%user:%user, u:user2:%primary_group</value> <description> Here, <user1> is mapped to <queue1>, <group1> is mapped to <queue2>, maps users to queues with the same name as user, <user2> is mapped to queue name same as <primary group> respectively. The mappings will be evaluated from left to right, and the first valid mapping will be used. </description> </property> ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • ๋‹ค์Œ์€ ๊ณ„์ธต๊ตฌ์กฐ๋กœ ํ๋ฅผ ์„ค์ •ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ๋Š” prod, dev, eng, science์ž…๋‹ˆ๋‹ค. root ์•„๋ž˜ prod, dev๊ฐ€ ๋“ฑ๋ก๋˜๊ณ , dev ์•„๋ž˜ eng, science ํ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ์˜ ๊ณ„์ธต ๊ตฌ์กฐ root prod[capacity:40%, max:100%] dev[capacity:60%, max:75%] eng[capacity:50%] science[capacity:50%] capacity-scheduler.xml ์„ค์ • ํ๋ณ„ ์„ค์ •๊ฐ’์„ ์ง€์ •ํ•  ๋•Œ๋Š” xml์˜ name์— ํ์˜ ๊ณ„์ธต ํ˜•ํƒœ๋กœ ์„ค์ •ํ•  ํ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. <?xml version="1.0"?> <configuration> <property> <name>yarn.scheduler.capacity.root.queues</name> <value>prod, dev</value> </property> <property> <name>yarn.scheduler.capacity.root.dev.queues</name> <value>eng, science</value> </property> <property> <name>yarn.scheduler.capacity.root.prod.capacity</name> <value>40</value> </property> <property> <name>yarn.scheduler.capacity.root.dev.capacity</name> <value>60</value> </property> <property> <name>yarn.scheduler.capacity.root.dev.maximum-capacity</name> <value>75</value> </property> <property> <name>yarn.scheduler.capacity.root.dev.eng.capacity</name> <value>50</value> </property> <property> <name>yarn.scheduler.capacity.root.dev.science.capacity</name> <value>50</value> </property> </configuration> ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • ํ™•์ธ ์„ค์ •๋œ ํ์˜ ๋ชฉ๋ก๊ณผ ํ˜„์žฌ ์‚ฌ์šฉ ์ค‘์ธ ์šฉ๋Ÿ‰์„ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ์— ์„ค์ •๋œ ์ •๋ณด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. # Capacity: ์„ค์ •๋œ ์šฉ๋Ÿ‰ # MaximumCapacity: ์ตœ๋Œ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์šฉ๋Ÿ‰, ๊ธฐ๋ณธ๊ฐ’ 100 # CurrentCapacity: ํ˜„์žฌ ์‚ฌ์šฉ ์ค‘์ธ ์šฉ๋Ÿ‰ $ mapred queue -list ====================== Queue Name : prod Queue State : running Scheduling Info : Capacity: 20.0, MaximumCapacity: 100.0, CurrentCapacity: 0.0 ====================== Queue Name : dev Queue State : running Scheduling Info : Capacity: 60.0, MaximumCapacity: 75.0, CurrentCapacity: 0.0 ====================== Queue Name : eng Queue State : running Scheduling Info : Capacity: 50.0, MaximumCapacity: 100.0, CurrentCapacity: 0.0 ====================== Queue Name : science Queue State : running Scheduling Info : Capacity: 50.0, MaximumCapacity: 100.0, CurrentCapacity: 0.0 ํ ์„ค์ • ๋ณ€๊ฒฝ ์šด์˜ ์ค‘ ์Šค์ผ€์ค„๋Ÿฌ์˜ ์„ค์ •์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋Š” capacity-scheduler.xml ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜๊ณ  ๋‹ค์Œ ๋ช…๋ น์„ ์ด์šฉํ•ด์„œ ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. capacity, user-limit-factor, maximum-capacity ์ˆ˜์ •, root์˜ ์‹ ๊ทœ ํ ์ถ”๊ฐ€๋Š” ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ํ๋ฅผ ์‚ญ์ œํ•  ๋•Œ๋Š” ํ๋ฅผ STOPPED ์ƒํƒœ๋กœ ๋ณ€๊ฒฝ ํ›„ ์ฒ˜๋ฆฌ ์ค‘์ธ ์ž‘์—…์ด ์—†์„ ๋•Œ ์‚ญ์ œ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. # capacity-scheduler.xml ์„ ์„ค์ •์„ ํ†ตํ•ด ํ๋ฅผ ๋‹ค์‹œ ์„ค์ • $ yarn rmadmin -refreshQueues ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • ์‹œ ์ฃผ์˜ ์‚ฌํ•ญ ์ปคํŒจ์‹œํ‹ฐ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์„ค์ •ํ•  ๋•Œ๋Š” ํ์˜ ์—ญํ• ์— ๋”ฐ๋ผ ์šฉ๋Ÿ‰์„ ์ž˜ ๋ฐฐ๋ถ„ํ•˜๊ณ , ์ตœ๋Œ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ์šฉ๋Ÿ‰(maximum-capacity)๊ณผ ์‚ฌ์šฉ์ž ์ œํ•œ(user-limit-factor)์„ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž์›์˜ ์šฉ๋Ÿ‰์„ ์ œํ•œํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ๋งˆ๋‹ค ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์šฉ๋Ÿ‰์ด ์žˆ์ง€๋งŒ ์ „์ฒด ํด๋Ÿฌ์Šคํ„ฐ์— ์—ฌ์œ ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ธฐ๋ณธ ์šฉ๋Ÿ‰์— ์‚ฌ์šฉ์ž ์ œํ•œ์„ ๊ณฑํ•œ ์šฉ๋Ÿ‰(capacity * user-limit-factor) ๋˜๋Š” ์ด ๊ฐ’์ด ์ตœ๋Œ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ์šฉ๋Ÿ‰์„ ๋„˜์–ด์„œ๋Š” ๊ฒฝ์šฐ ์ตœ๋Œ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ์šฉ๋Ÿ‰๋งŒํผ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. ํ์˜ ์šฉ๋Ÿ‰ = min(capacity * user-limit-factor, maximum-capacity) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์— ํ• ๋‹น๋˜๋Š” ์šฉ๋Ÿ‰ ๋น„์œจ(yarn.scheduler.capacity.maximum-am-resource-percent)์„ ์ž˜ ์กฐ์ ˆํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ œ์ถœ๋œ ์ž‘์—…์ด ๋งŽ์•„์ง€๋ฉด์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๊ฐ€ ๋งŽ์€ ์ž์›์„ ๊ฐ€์ ธ๊ฐ€๋ฉด, ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ์ž์›์ด ๋ถ€์กฑํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ž‘์—…์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๋น„์œจ์„ ์ฐพ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ  Capacity scheduler ์„ค์ •: ๋ฐ”๋กœ ๊ฐ€๊ธฐ 2-ํŽ˜์–ด ์Šค์ผ€์ค„๋Ÿฌ(Fair Scheduler) ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ •๊ฐ’ yarn-site.xml fair-scheduler.xml ์ฐธ๊ณ  ํ•˜๋‘ก์˜ ํŽ˜์–ด ์Šค์ผ€์ค„๋Ÿฌ(Fair Scheduler)์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŽ˜์–ด ์Šค์ผ€์ค„๋Ÿฌ๋Š” ์ œ์ถœ๋œ ์ž‘์—…์ด ๋™๋“ฑํ•˜๊ฒŒ ๋ฆฌ์†Œ์Šค๋ฅผ ์ ์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ž‘์—… ํ์— ์ž‘์—…์ด ์ œ์ถœ๋˜๋ฉด ํด๋Ÿฌ์Šคํ„ฐ๋Š” ์ž์›์„ ์กฐ์ ˆํ•˜์—ฌ ๋ชจ๋“  ์ž‘์—…์— ๊ท ๋“ฑํ•˜๊ฒŒ ์ž์›์„ ํ• ๋‹นํ•˜์—ฌ ์ค๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ์™€ CPU๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž์›์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŽ˜์–ด ์Šค์ผ€์ค„๋Ÿฌ๋Š” ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ๊ณ„์ธตํ™”๋œ ํ๋ฅผ ์„ ์–ธํ•˜๊ณ , ํ๋ณ„๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์šฉ๋Ÿ‰์„ ํ• ๋‹นํ•˜์—ฌ ์ž์›์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 100G์˜ ๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰์„ ๊ฐ€์ง€๋Š” ํด๋Ÿฌ์Šคํ„ฐ์—์„œ A, B ๋‘ ๊ฐœ์˜ ํ์— ๊ฐ๊ฐ ์ตœ์ € ์ž์›(minResource) <10000 mb, 10vcores> ์ตœ๋Œ€ ์ž์›(maxResource) <60000 mb, 30vcores>์„ ์„ค์ •ํ•˜๊ณ , ๊ฐ ํ๊ฐ€ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์ตœ๋Œ€ 60G๊นŒ์ง€์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A ํ์˜ ํ•˜์œ„์— A_sub_1, A_sub_2์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ํ๋ฅผ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ •๊ฐ’ ํŽ˜์–ด ์Šค์ผ€์ค„๋Ÿฌ๋Š” yarn-site.xml์— ์Šค์ผ€์ค„๋Ÿฌ ๊ด€๋ จ ์„ค์ •์„ ํ•˜๊ณ , fair-scheduler.xml์— ํ ๊ด€๋ จ ์„ค์ •์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. 10์ดˆ๋งˆ๋‹ค ์„ค์ • ํŒŒ์ผ์„ ์ฝ์–ด์„œ ํ ์„ค์ •์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค. ํŽ˜์–ด ์Šค์ผ€์ค„๋Ÿฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. <property> <name>yarn.resourcemanager.scheduler.class</name> <value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler</value> </property> yarn-site.xml ์„ค์ •๊ฐ’ ๊ธฐ๋ณธ๊ฐ’ ๋น„๊ณ  yarn.scheduler.fair.allocation.file fair-scheduler.xml ์„ค์ • ํŒŒ์ผ์˜ ์ด๋ฆ„ yarn.scheduler.fair.user-as-default-queue true ํ์ด๋ฆ„์„ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ๊ธฐ๋ณธํ์˜ ์‚ฌ์šฉ ์—ฌ๋ถ€ yarn.scheduler.fair.preemption false ์šฐ์„ ์ˆœ์œ„ ์„ ์ ์˜ ์‚ฌ์šฉ ์—ฌ๋ถ€ fair-scheduler.xml <?xml version="1.0"?> <allocations> <queue name="dev"> <minResources>10000 mb, 10vcores</minResources> <maxResources>60000 mb, 30vcores</maxResources> <maxRunningApps>50</maxRunningApps> <maxAMShare>1.0</maxAMShare> <weight>2.0</weight> <schedulingPolicy>fair</schedulingPolicy> </queue> <queue name="prd"> <minResources>10000 mb, 10vcores</minResources> <maxResources>60000 mb, 30vcores</maxResources> <maxRunningApps>100</maxRunningApps> <maxAMShare>0.1</maxAMShare> <weight>2.0</weight> <schedulingPolicy>fair</schedulingPolicy> <queue name="sub_prd"> <aclSubmitApps>charlie</aclSubmitApps> <minResources>5000 mb, 0vcores</minResources> </queue> </queue> <user name="sample_user"> <maxRunningApps>30</maxRunningApps> </user> <userMaxAppsDefault>5</userMaxAppsDefault> <queueMaxAMShareDefault>0.2</queueMaxAMShareDefault> <queuePlacementPolicy> <rule name="specified"/> <rule name="primaryGroup" create="false"/> <rule name="default" queue="dev"/> </queuePlacementPolicy> </allocations> ์ฐธ๊ณ  Fair scheduler ์„ค์ •: ๋ฐ”๋กœ ๊ฐ€๊ธฐ 2-YARN ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • YARN์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์€ yarn-site.xml ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์—ฌ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋…ธ๋“œ ๋งค๋‹ˆ์ €์˜ ๋ฉ”๋ชจ๋ฆฌ, CPU ๊ฐœ์ˆ˜์™€ ์ปจํ…Œ์ด๋„ˆ์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€, ์ตœ์†Œ ๋ฉ”๋ชจ๋ฆฌ ๋“ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ yarn-default.xml ์„ ์ฐธ๊ณ (yarn-default.xml) ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ์„ค์ • yarn.nodemanager.resource.memory-mb ํด๋Ÿฌ์Šคํ„ฐ์˜ ๊ฐ ๋…ธ๋“œ์—์„œ ์ปจํ…Œ์ด๋„ˆ ์šด์˜์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ์ด๋Ÿ‰ ๋…ธ๋“œ์˜ OS๋ฅผ ์šด์˜ํ•  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ œ์™ธํ•˜๊ณ  ์„ค์ • ๊ธฐ๋ณธ๊ฐ’์€ ์žฅ๋น„์— ์„ค์ •๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ 80% ์ •๋„๋ฅผ ์„ค์ • ๋…ธ๋“œ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ 32G์ธ ๊ฒฝ์šฐ ์šด์˜์ฒด์ œ๋ฅผ ์œ„ํ•œ 4G๋ฅผ ์ œ์™ธํ•˜๊ณ  28G๋ฅผ ์„ค์ • yarn.nodemanager.resource.cpu-vcores ํด๋Ÿฌ์Šคํ„ฐ์˜ ๊ฐ ๋…ธ๋“œ์—์„œ ์ปจํ…Œ์ด๋„ˆ ์šด์˜์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” CPU์˜ ๊ฐœ์ˆ˜ ๊ธฐ๋ณธ๊ฐ’์€ ์žฅ๋น„์— ์„ค์น˜๋œ CPU์˜ 80% ์ •๋„๋ฅผ ์„ค์ • ๋…ธ๋“œ์— ์„ค์น˜๋œ CPU๊ฐ€ 40๊ฐœ์ผ ๊ฒฝ์šฐ 32๋ฅผ ์„ค์ • yarn.scheduler.maximum-allocation-mb ํ•˜๋‚˜์˜ ์ปจํ…Œ์ด๋„ˆ์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ์ตœ๋Œ“๊ฐ’ 8G๊ฐ€ ๊ธฐ๋ณธ ๊ฐ’ yarn.scheduler.minimum-allocation-mb ํ•˜๋‚˜์˜ ์ปจํ…Œ์ด๋„ˆ์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’ 1G๊ฐ€ ๊ธฐ๋ณธ๊ฐ’ yarn.nodemanager.vmem-pmem-ratio ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ๋Œ€๋น„ ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ๋น„์œจ mapreduce.map.memory.mb * ์„ค์ •๊ฐ’์˜ ๋น„์œจ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ 1G๋กœ ์„ค์ •ํ•˜๊ณ , ์ด ๊ฐ’์„ 10์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ 10G ์‚ฌ์šฉ yarn.nodemanager.vmem-check-enabled ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•œ ์ œํ•œ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ, true์ผ ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ๋„˜์–ด์„œ๋ฉด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ข…๋ฃŒ false๋กœ ์„ค์ •ํ•˜์—ฌ ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ๋Š” yarn.nodemanager.pmem-check-enabled ๋ฌผ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•œ ์ œํ•œ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ, true์ผ ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ๋„˜์–ด์„œ๋ฉด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ข…๋ฃŒ yarn-site.xml ์„ค์ • ์˜ˆ์ œ <property> <name>yarn.nodemanager.resource.memory-mb</name> <value>28672</value> </property> <property> <name>yarn.nodemanager.resource.cpu-vcores</name> <value>8</value> </property> <property> <name>yarn.scheduler.maximum-allocation-mb</name> <value>8192</value> </property> <property> <name>yarn.scheduler.minimum-allocation-mb</name> <value>1024</value> </property> <property> <name>yarn.nodemanager.vmem-pmem-ratio</name> <value>2.1</value> </property> <property> <name>yarn.nodemanager.pmem-check-enabled</name> <value>true</value> </property> <property> <name>yarn.nodemanager.vmem-check-enabled</name> <value>false</value> </property> 3-YARN ๋ช…๋ น์–ด HDFS ์ปค๋งจ๋“œ๋Š” ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ, ์šด์˜์ž ์ปค๋งจ๋“œ๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ชจ๋“œ๋งˆ๋‹ค ๋‹ค์–‘ํ•œ ์ปค๋งจ๋“œ๊ฐ€ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์šฉ ๋ฐ ์šด์˜์— ํ•„์ˆ˜์ ์ธ ๋ช‡ ๊ฐ€์ง€ ์ปค๋งจ๋“œ๋งŒ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ์ปค๋งจ๋“œ์˜ ๋ชฉ๋ก์€ YARN Commands Guide1์„ ์ฐธ๊ณ ํ•˜์‹ญ์‹œ์˜ค. ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ ๋ชฉ๋ก application ์ปค๋งจ๋“œ ์ž‘์—… ๋ชฉ๋ก ํ™•์ธ ์ž‘์—… ์ƒํƒœ ํ™•์ธ ์ž‘์—… ์ข…๋ฃŒ applicationattempt ์ปค๋งจ๋“œ container ์ปค๋งจ๋“œ logs ์ปค๋งจ๋“œ ์šด์˜์ž ์ปค๋งจ๋“œ ์šด์˜์ž ์ปค๋งจ๋“œ ๋ชฉ๋ก rmadmin ํ์ •๋ณด ๊ฐฑ์‹  ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ ๋ชฉ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ์ค‘์—์„œ ์ฃผ์š” ์˜ต์…˜์ธ application ๊ณผ logs์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ปค๋งจ๋“œ ๋ชฉ๋ก ๋ช…๋ น์–ด ์„ค๋ช… application ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ •๋ณด ํ™•์ธ, ์ž‘์—… ์ข…๋ฃŒ applicationattempt ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ Attempt ์ •๋ณด classpath ํ•„์š”ํ•œ ํด๋ž˜์Šค ์ •๋ณด container ์ปจํ…Œ์ด๋„ˆ ์ •๋ณด jar jar ํŒŒ์ผ ์‹คํ–‰ logs ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ๊ทธ ํ™•์ธ node ๋…ธ๋“œ ์ •๋ณด queue ํ ์ •๋ณด version ๋ฒ„์ „ ์ •๋ณด envvars ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ •๋ณด application ์ปค๋งจ๋“œ application ์ปค๋งจ๋“œ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ณ , ์ž‘์—…์„ ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์š” ์ปค๋งจ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž‘์—… ๋ชฉ๋ก ํ™•์ธ -list ์˜ต์…˜์„ ์ด์šฉํ•ด์„œ ํ˜„์žฌ ์ž‘์—… ์ค‘์ธ ์ž‘์—… ๋ชฉ๋ก์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž‘์—… ์ƒํƒœ, ์ง„ํ–‰ ์ƒํ™ฉ, ์ž‘์—…ํ, ํŠธ๋ž˜ํ‚น URL ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ yarn application -list Total number of applications (application-types: [] and states: [SUBMITTED, ACCEPTED, RUNNING]):19 Application-Id Application-Name Application-Type User Queue State Final-State Progress Tracking-URL application_1536937158836_712399 OOZIE-launcher MAPREDUCE hadoop q1 RUNNING UNDEFINED 95% http://host:43902 application_1536937158836_712407 HIVE-ID TEZ hadoop q2 RUNNING UNDEFINED 67.77% http://host:33535/ui/ ์ž‘์—… ์ƒํƒœ ํ™•์ธ ์ž‘์—… ๋ชฉ๋ก์—์„œ ํ™•์ธํ•œ ์ž‘์—… ID(Application-Id)๋ฅผ ์ด์šฉํ•˜์—ฌ ํ˜„์žฌ ์ž‘์—… ์ƒํƒœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ yarn application -status application_1234_1 Application Report : Application-Id : application_1234_1 Application-Name : HIVE-name Application-Type : MAPREDUCE User : hadoop Queue : prd Start-Time : 1560327373054 Finish-Time : 0 Progress : 95% State : RUNNING Final-State : UNDEFINED Tracking-URL : http://host:43902 RPC Port : 43360 AM Host : host Aggregate Resource Allocation : 2459450 MB-seconds, 960 vcore-seconds Diagnostics : ์ž‘์—… ์ข…๋ฃŒ -kill ์˜ต์…˜์„ ์ด์šฉํ•ด์„œ ์ž‘์—…์„ ๊ฐ•์ œ๋กœ ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž‘์—… ๋ชฉ๋ก์—์„œ ํ™•์ธํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์•„์ด๋””๋‚˜ ์žก์•„์ด๋””๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. $ yarn application -kill <application_id> applicationattempt ์ปค๋งจ๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ˜„์žฌ ์‹œ๋„(attempt) ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์„ค์ •์— ๋”ฐ๋ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ž๋™์œผ๋กœ ์žฌ์ž‘์—…ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ yarn applicationattempt -list application_1234_1 Total number of application attempts :1 ApplicationAttempt-Id State AM-Container-Id Tracking-URL appattempt_1234_1_000001 RUNNING container_1234_1_01_000001 http://host:20888/proxy/application_1234_1/ container ์ปค๋งจ๋“œ ํ˜„์žฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋™์ž‘ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Attempt ID(ApplicationAttempt-Id)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ณด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. $ yarn container -list appattempt_1234_1_000001 Total number of containers :94 Container-Id Start Time Finish Time State Host Node Http Address LOG-URL container_1234_1_01_138704 Wed Jun 12 08:40:17 +0000 2019 N/A RUNNING host-1:8041 http://host-1:8042 http://host-1:8042/node/containerlogs/container_1234_1_01_138704/hadoop container_1234_1_01_138638 Wed Jun 12 08:40:02 +0000 2019 N/A RUNNING host-2:8041 http://host-2:8042 http://host-2:8042/node/containerlogs/container_1234_1_01_138638/hadoop logs ์ปค๋งจ๋“œ logs ์ปค๋งจ๋“œ๋Š” ์ž‘์—…์ด ์ข…๋ฃŒ๋œ ์žก์˜ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์ž…๋‹ˆ๋‹ค. ์ž‘์—… ์ค‘์ธ ์žก์˜ ๋กœ๊ทธ๋Š” ํžˆ์Šคํ† ๋ฆฌ ์„œ๋ฒ„์— ์ €์žฅ๋˜๊ธฐ ์ „์ด๋ผ์„œ ํ™•์ธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ž‘์—… ์ค‘์ธ ์žก์€ ์ž‘์—… ๋ชฉ๋ก์—์„œ ํ™•์ธํ•œ ํŠธ๋ž˜ํ‚น URL์— ์ ‘์†ํ•ด์„œ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. CLI ํ™˜๊ฒฝ์ด๋ผ๋ฉด lynx ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋˜๊ณ , ์›น์œผ๋กœ ์ ‘์†ํ•ด์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ yarn logs -applicationId <application_id> ์šด์˜์ž ์ปค๋งจ๋“œ ์šด์˜์ž ์ปค๋งจ๋“œ ๋ชฉ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ์ค‘์—์„œ ์ฃผ์š” ์˜ต์…˜์ธ rmadmin์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šด์˜์ž ์ปค๋งจ๋“œ ๋ชฉ๋ก ๋ช…๋ น์–ด ์„ค๋ช… daemonlog ๋กœ๊ทธ ๋ ˆ๋ฒจ ์„ค์ • nodemanager ๋…ธ๋“œ ๋งค๋‹ˆ์ € ์‹คํ–‰ proxyserver ํ”„๋ฝ์‹œ ์„œ๋ฒ„ ์‹คํ–‰ resourcemanager ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ์‹คํ–‰ rmadmin ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ์–ด๋“œ๋ฏผ ํด๋ผ์ด์–ธํŠธ ๋ช…๋ น schedulerconf ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • ์—…๋ฐ์ดํŠธ scmadmin ๊ณต์œ  ์บ์‹œ ๋งค๋‹ˆ์ € ์–ด๋“œ๋ฏผ ํด๋ผ์ด์–ธํŠธ ๋ช…๋ น sharedcachemanager ๊ณต์œ  ์บ์‹œ ๋งค๋‹ˆ์ € ์‹คํ–‰ timelineserver ํƒ€์ž„๋ผ์ธ ์„œ๋ฒ„ ์‹คํ–‰ rmadmin rmadmin ์˜ต์…˜์„ ์ด์šฉํ•ด์„œ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์— ๋™๋ก๋œ ํ์™€ ๋…ธ๋“œ ์ •๋ณด๋ฅผ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ์ •๋ณด ๊ฐฑ์‹  ์ปคํŒจ์‹œํ‹ฐ ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ •(capacity-scheduler.xml)์„ ๋‹ค์‹œ ์ฝ์–ด์„œ ์ •๋ณด๋ฅผ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค. $ yarn rmadmin -refreshQueues YARN Commans (๋ฐ”๋กœ ๊ฐ€๊ธฐ) โ†ฉ 4-YARN REST API ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋Š” REST API๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ƒํƒœ ์ •๋ณด, ์šด์˜์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‘๋‹ต ํ˜•ํƒœ๋Š” JSON, XML ํ˜•ํƒœ๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ๋งค๋‰ด์–ผ 1์—์„œ ์ „์ฒด ๋ชฉ๋ก์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํŒŒ์ด์ฌ์„ ์ด์šฉํ•ด์„œ ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋ฉ”ํŠธ๋ฆญ ์ •๋ณด๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ ๋ฉ”ํŠธ๋ฆญ ์ •๋ณด ํ™•์ธ ํด๋Ÿฌ์Šคํ„ฐ ๋ฉ”ํŠธ๋ฆญ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๋Š” URI๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น URI๋ฅผ GET ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ—ค๋”์— { 'Content-Type': 'application/json' }๋กœ ์ •๋ณด๋ฅผ ์„ค์ •ํ•˜๋ฉด json<NAME>์œผ๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. http://<rm http address:port>/ws/v1/cluster/metrics ๋ฉ”ํŠธ๋ฆญ ํ™•์ธ REST API ์˜ˆ์ œ ํŒŒ์ด์ฌ์„ ์ด์šฉํ•ด์„œ RMA ๋ฉ”ํŠธ๋ฆญ์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib, json, urllib2, datetime from urllib2 import HTTPError def request_get(request_url): return request(request_url, "GET", "", {'Content-Type': 'application/json'}) def request(request_url, request_type="GET", data="", header={}): opener = urllib2.build_opener(urllib2.HTTPHandler) request_get = urllib2.Request(request_url, data, header) request_get.get_method = lambda: request_type response = opener.open(request_get) response_info = response.info() response_body = response.read() json_obj = json.loads(response_body) print(json.dumps(json_obj, sort_keys=True, indent=4, separators=(',', ': '))) def main(): rma_url = "http://<RMA ์ฃผ์†Œ>:<RMA ํฌํŠธ>/ws/v1/cluster/metrics" request_get(rma_url) if __name__ == '__main__': main() ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. { "clusterMetrics": { "activeNodes": 2, "allocatedMB": 0, "allocatedVirtualCores": 0, "appsCompleted": 25000, "appsFailed": 1, "appsKilled": 1, "appsPending": 0, "appsRunning": 0, "appsSubmitted": 1, "availableMB": 1, "availableVirtualCores": 23, "containersAllocated": 0, "containersPending": 0, "containersReserved": 0, "decommissionedNodes": 0, "decommissioningNodes": 0, "lostNodes": 0, "rebootedNodes": 0, "reservedMB": 0, "reservedVirtualCores": 0, "totalMB": 25000, "totalNodes": 2, "totalVirtualCores": 23, "unhealthyNodes": 0 } } ResourceManager REST API's (๋ฐ”๋กœ ๊ฐ€๊ธฐ) โ†ฉ 5-YARN Node Labels YARN์€ 2.6๋ฒ„์ „๋ถ€ํ„ฐ ๋…ธ๋“œ ๋ ˆ์ด๋ธ”(Node Label) 1 ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋…ธ๋“œ ๋ ˆ์ด๋ธ”์€ ์„œ๋ฒ„๋ฅผ ํŠน์„ฑ์— ๋งž๊ฒŒ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์–ด๋Š ํšŒ์‚ฌ์—์„œ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ Batch, Stream ํ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— SSD๋ฅผ ์„ค์น˜ํ•œ ์„œ๋ฒ„์™€ GPU๋ฅผ ์„ค์น˜ํ•œ ์„œ๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. SSD๋ฅผ ์„ค์น˜ํ•œ ์„œ๋ฒ„๋Š” IO๊ฐ€ ๋งŽ์€ ์ž‘์—…์— ์œ ๋ฆฌํ•˜๊ณ , GPU๋ฅผ ์„ค์น˜ํ•œ ์„œ๋ฒ„๋Š” ์—ฐ์‚ฐ์ด ๋งŽ์€ ์ž‘์—…์— ์œ ๋ฆฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ๋ฐฉ์‹๋Œ€๋กœ ํด๋Ÿฌ์Šคํ„ฐ์— ์„œ๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋Š” ์„œ๋ฒ„์˜ ํŠน์„ฑ์— ๋Œ€ํ•œ ๊ตฌ๋ถ„ ์—†์ด ์—ฌ์œ ๊ฐ€ ์žˆ๋Š” ์„œ๋ฒ„์—์„œ ์ž‘์—…์„ ์ง„ํ–‰ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ๋…ธ๋“œ ๋ ˆ์ด๋ธ”์„ ์ด์šฉํ•˜์—ฌ ๊ฐ ์„œ๋ฒ„์— ์œ ๋ฆฌํ•œ ์ž‘์—…์„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SSD๋ฅผ ์„ค์น˜ํ•œ ์„œ๋ฒ„๋Š” SSD, GPU๋ฅผ ์„ค์น˜ํ•œ ์„œ๋ฒ„๋Š” GPU ๋ ˆ์ด๋ธ”๋กœ ์„ค์ •ํ•˜๊ณ  Batch ํ๋Š” SSD ๋ ˆ์ด๋ธ”, Stream ํ๋Š” GPU ๋ ˆ์ด๋ธ”์„ ์ด์šฉํ•˜๋„๋ก ์„ค์ •ํ•˜๋ฉด ๋ฆฌ์†Œ๋“œ ๋งค๋‹ˆ์ €๋Š” Batch ํ์— ๋“ค์–ด์˜จ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๋•Œ SSD ๋ ˆ์ด๋ธ”์ด ์„ค์ •๋œ ์„œ๋ฒ„์—์„œ ์ž‘์—…ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ ์ž‘์—…์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๊ณ  ์ž‘์—… ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” ๋…ธ๋“œ ํŒŒํ‹ฐ์…˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ๋…ธ๋“œ ์ปจ์ŠคํŠธ๋ ˆ์ธํŠธ2 ๊ธฐ๋Šฅ์€ ํ˜„์žฌ ์ž‘์—… ์ค‘์ž…๋‹ˆ๋‹ค. ํŠน์ง• ํ•˜๋‚˜์˜ ๋…ธ๋“œ๋Š” ํ•˜๋‚˜์˜ ํŒŒํ‹ฐ์…˜์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ(One node can have only one node partition) ๊ธฐ๋ณธ๊ฐ’์€ DEFAULT ํŒŒํ‹ฐ์…˜(partition="") ํด๋Ÿฌ์Šคํ„ฐ๋Š” ์—ฌ๋Ÿฌ ํŒŒํ‹ฐ์…˜(์—ฌ๋Ÿฌ ๋…ธ๋“œ)์œผ๋กœ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Œ ์Šค์ผ€์ค„๋Ÿฌ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ ํŒŒํ‹ฐ์…˜์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์†Œ์Šค์˜ ์–‘์„ ์„ค์ •ํ•ด์•ผ ํ•จ ๋…ธ๋“œ ๋ ˆ์ด๋ธ”๋กœ ์ง€์ •๋˜์ง€ ์•Š์€ ํ๋Š” ๊ธฐ๋ณธ ํŒŒํ‹ฐ์…˜์„ ์ด์šฉํ•˜๊ฒŒ ๋จ ์ ‘๊ทผ์ œ์–ด(ACL) ํ์—์„œ ์„ค์ •๋œ ๋…ธ๋“œ๋งŒ ์ž‘์—…์— ์ด์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋ฅผ ํ†ตํ•ด ๊ฐ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ ‘๊ทผ์ œ์–ด๊ฐ€ ๊ฐ€๋Šฅํ•จ ํด๋Ÿฌ์Šคํ„ฐ ์‚ฌ์šฉ๋Ÿ‰ ์ œ์–ด ๊ฐ ๋…ธ๋“œ๋งˆ๋‹ค ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž์›์˜ ๋น„์œจ์„ ์ง€์ •ํ•˜์—ฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Œ ํŒŒํ‹ฐ์…˜์˜ ์ข…๋ฅ˜ Exclusive ํŒŒํ‹ฐ์…˜ ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ž์›์— ์—ฌ์œ ๊ฐ€ ์žˆ์–ด๋„ ์ง€์ •ํ•œ ํŒŒํ‹ฐ์…˜๋งŒ ์ด์šฉํ•˜์—ฌ ์ž‘์—…์„ ์ฒ˜๋ฆฌ Non-Exclusive ํŒŒํ‹ฐ์…˜ ํด๋Ÿฌ์Šคํ„ฐ์— ์—ฌ์œ ๊ฐ€ ์žˆ๋‹ค๋ฉด DEFAULT ํŒŒํ‹ฐ์…˜์œผ๋กœ ์š”์ฒญํ•œ ์ž‘์—…์— ๋Œ€ํ•ด์„œ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ ์„ค์ • ๋…ธ๋“œ ๋ ˆ์ด๋ธ” ์„ค์ • ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์—์„œ ๋…ธ๋“œ ๋ ˆ์ด๋ธ”์„ ์ง€์›ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ yarn-site.xml์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. <property> <name>yarn.node-labels.enabled</name> <value>true</value> </property> <property> <name>yarn.node-labels.fs-store.root-dir</name> <value>hdfs://namenode:port/path/to/store/node-labels/</value> </property> ๋…ธ๋“œ ๋ ˆ์ด๋ธ” ๊ด€๋ จ ๋ช…๋ น์–ด yarn ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ๋…ธ๋“œ ๋ ˆ์ด๋ธ”์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋ ˆ์ด๋ธ” ์ •๋ณด ํ™•์ธ # ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋ ˆ์ด๋ธ” ์ •๋ณด ํ™•์ธ $ yarn cluster --list-node-labels Node Labels: <GPU:exclusivity=false> ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋…ธ๋“œ ๋ ˆ์ด๋ธ” ์ถ”๊ฐ€ ๋ฐ ์‚ญ์ œ # ํด๋Ÿฌ์Šคํ„ฐ ๋…ธ๋“œ ๋ ˆ์ด๋ธ” ์ถ”๊ฐ€, exclusive ๊ธฐ๋ณธ๊ฐ’์€ true $ yarn rmadmin -addToClusterNodeLabels "label_1(exclusive=true),label_2(exclusive=false)" # ํด๋Ÿฌ์Šคํ„ฐ ๋…ธ๋“œ ๋ ˆ์ด๋ธ” ์‚ญ์ œ $ yarn rmadmin -removeFromClusterNodeLabels "label_1" ๋…ธ๋“œ ๋ ˆ์ด๋ธ” ์ถ”๊ฐ€ ๋ฐ ์‚ญ์ œ # ๋…ธ๋“œ ๋ ˆ์ด๋ธ” ์ถ”๊ฐ€ $ yarn rmadmin -replaceLabelsOnNode โ€œnode1[:port]=label1 node2=label2โ€ ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • ์Šค์ผ€์ค„๋Ÿฌ์— ์„ค์ •ํ•˜๋Š” ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์„ค์ •์€ ์ปคํŒจ์‹œํ‹ฐ ์Šค์ผ€์ค„๋Ÿฌ๋Š” capacity-scheduler.xml์— ์„ค์ •ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์„ค์ • ๋น„๊ณ  yarn.scheduler.capacity.<queue-path>.capacity DEFAULT ํŒŒํ‹ฐ์…˜์— ์†ํ•˜๋Š” ๋…ธ๋“œ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ํ์˜ ๋ฐฑ๋ถ„์œจ ์„ค์ •. ๊ฐ ๋ถ€๋ชจ์˜ ์ง๊ณ„ ์ž๋…€์— ๋Œ€ํ•œ ๊ธฐ๋ณธ ์šฉ๋Ÿ‰์˜ ํ•ฉ์€ 100 yarn.scheduler.capacity.<queue-path>.accessible-node-labels "hbase, storm"๊ณผ ๊ฐ™์ด ๊ด€๋ฆฌ์ž๊ฐ€ ๊ฐ ํ์—์„œ ๋ ˆ์ด๋ธ”์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๊ณ  ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ ˆ์ด๋ธ”์„ ์ง€์ •. ํ๊ฐ€ ๋ ˆ์ด๋ธ” hbase ๋ฐ storm์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•จ. ๋ชจ๋“  ํ๋Š” ๋ ˆ์ด๋ธ”์ด ์—†๋Š” ๋…ธ๋“œ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์šฉ์ž๋Š” ์ด๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„ ๋จ. ์‚ฌ์šฉ์ž๊ฐ€ ์ด ํ•„๋“œ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒ์œ„ ํ•„๋“œ์—์„œ ์ƒ์†. yarn.scheduler.capacity.<queue-path>.accessible-node-labels.<label>.capacity <label> partition์— ์†ํ•˜๋Š” ๋…ธ๋“œ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ํ์˜ ๋ฐฑ๋ถ„์œจ์„ ์„ค์ •. ๊ฐ ๋ถ€๋ชจ ์•„๋ž˜์˜ ์ง๊ณ„ ์ž๋…€์— ๋Œ€ํ•œ <label> ์šฉ๋Ÿ‰์˜ ํ•ฉ๊ณ„๋Š” 100. ๊ธฐ๋ณธ์ ์œผ๋กœ 0. yarn.scheduler.capacity.<queue-path>.accessible-node-labels.<label>.maximum-capacity yarn.scheduler.capacity.<queue-path>.maximum-capacity์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๊ฐ ๋Œ€๊ธฐ์—ด์˜ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ์ตœ๋Œ€ ์šฉ๋Ÿ‰. ๊ธฐ๋ณธ์ ์œผ๋กœ 100. yarn.scheduler.capacity.<queue-path>.default-node-label-expression "hbase"์™€ ๊ฐ™์€ ๊ฐ’. ์ฆ‰, ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด ์ž์› ์š”์ฒญ์— ๋…ธ๋“œ ๋ ˆ์ด๋ธ”์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ํ์— ์ œ์ถœ ํ•œ ๊ฒฝ์šฐ "hbase"๋ฅผ default-node-label-expression์œผ๋กœ ์‚ฌ์šฉ. ์˜ˆ์ œ ์–ด๋Š ํšŒ์‚ฌ์—์„œ ์—”์ง€๋‹ˆ์–ด ๋ถ€์„œ(engineering), ๋งˆ์ผ€ํŒ… ๋ถ€์„œ(marketing), ์˜์—…๋ถ€์„œ(sales)๊ฐ€ ํ•˜๋‚˜์˜ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ 1/3์”ฉ ๋‚˜๋ˆ ์„œ ์“ด๋‹ค๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. yarn.scheduler.capacity.root.queues=engineering, marketing, sales yarn.scheduler.capacity.root.engineering.capacity=33 yarn.scheduler.capacity.root.marketing.capacity=34 yarn.scheduler.capacity.root.sales.capacity=33 ์—”์ง€๋‹ˆ์–ด ๋ถ€์„œ์™€ ๋งˆ์ผ€ํŒ… ๋ถ€์„œ๊ฐ€ ์—…๋ฌด๊ฐ€ ๋Š˜์–ด๋‚˜์„œ GPU๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋จธ์‹ ๋“ค์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋จธ์‹ ๋“ค๋กœ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๋”ฐ๋กœ ๊ตฌ์ถ•ํ•˜๊ฒŒ ๋˜๋ฉด ์šด์˜๋น„๊ฐ€ ๋Š˜์–ด๋‚˜์„œ ๊ธฐ์กด์˜ ํด๋Ÿฌ์Šคํ„ฐ์— ๋จธ์‹ ์„ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ GPU ๋…ธ๋“œ ๋ ˆ์ด๋ธ”์„ ์ถ”๊ฐ€ํ•ด์„œ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  GPU ๋…ธ๋“œ๋“ค์€ ์—”์ง€๋‹ˆ์–ด ๋ถ€์„œ์™€ ๋งˆ์ผ€ํŒ… ๋ถ€์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์—”์ง€๋‹ˆ์–ด ๋ถ€์„œ์™€ ๋งˆ์ผ€ํŒ… ๋ถ€์„œ๋Š” GPU ๋…ธ๋“œ๋ฅผ 1/2์”ฉ ๋‚˜๋ˆ ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. yarn.scheduler.capacity.root.engineering.accessible-node-labels=GPU yarn.scheduler.capacity.root.marketing.accessible-node-labels=GPU yarn.scheduler.capacity.root.engineering.accessible-node-labels.GPU.capacity=50 yarn.scheduler.capacity.root.marketing.accessible-node-labels.GPU.capacity=50 yarn.scheduler.capacity.root.engineering.default-node-label-expression=GPU https://hadoop.apache.org/docs/r2.7.3/hadoop-yarn/hadoop-yarn-site/NodeLabel.html โ†ฉ Node Constraints. ํ•˜๋‚˜์˜ ๋…ธ๋“œ์— ์—ฌ๋Ÿฌ ๋ ˆ์ด๋ธ”์„ ์ง€์›. โ†ฉ 6-YARN ๊ณ ๊ฐ€์šฉ์„ฑ YARN์€ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๊ฐ€ ๋‹จ์ผ ์‹คํŒจ ์ง€์ ์ž…๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ž์›๊ด€๋ฆฌ, ์ž‘์—… ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‘ก 2.4 ๋ฒ„์ „๋ถ€ํ„ฐ HA ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € HA ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ๊ณ ๊ฐ€์šฉ์„ฑ์€ ์ฃผํ‚คํผ์™€ ์•กํ‹ฐ๋ธŒ, ์Šคํƒ ๋ฐ”์ด ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋ฅผ ์ด์šฉํ•˜์—ฌ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋Š” ํด๋Ÿฌ์Šคํ„ฐ์™€ ์ž‘์—… ์ƒํƒœ ๋ณด๊ด€์„ ์œ„ํ•œ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์ €์ •์†Œ๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ์ฃผํ‚คํผ๋ฅผ ์ด์šฉํ•œ ZKRMStateStore์™€ ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ์ด์šฉํ•œ FileSystemRMStateStore๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ZKRMStateStore๋ฅผ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. ์žฅ์•  ๊ทน๋ณต ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ์ˆ˜๋™์œผ๋กœ CLI ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์•กํ‹ฐ๋ธŒ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผํ‚คํผ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. # ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ์ƒํƒœ ํ™•์ธ $ yarn rmadmin -getServiceState rm1 active $ yarn rmadmin -getServiceState rm2 standby # ์ƒํƒœ ๋ณ€๊ฒฝ $ yarn rmadmin -transitionToStandby rm1 ์„ค์ • ๊ณ ๊ฐ€์šฉ์„ฑ ์„ค์ •์€ yarn.resourcemanager.ha.enabled ์„ค์ • ์—ฌ๋ถ€๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. <property> <name>yarn.resourcemanager.ha.enabled</name> <value>true</value> </property> <property> <name>yarn.resourcemanager.cluster-id</name> <value>cluster1</value> </property> <property> <name>yarn.resourcemanager.ha.rm-ids</name> <value>rm1, rm2</value> </property> <property> <name>yarn.resourcemanager.hostname.rm1</name> <value>master1</value> </property> <property> <name>yarn.resourcemanager.hostname.rm2</name> <value>master2</value> </property> <property> <name>yarn.resourcemanager.webapp.address.rm1</name> <value>master1:8088</value> </property> <property> <name>yarn.resourcemanager.webapp.address.rm2</name> <value>master2:8088</value> </property> <property> <name>hadoop.zk.address</name> <value>zk1:2181, zk2:2181, zk3:2181</value> </property> ์ฐธ๊ณ  ResourceManagerHA 7-ํƒ€์ž„๋ผ์ธ ์„œ๋น„์Šค v.2 ํ•˜๋‘ก 3 ๋ฒ„์ „์—์„œ YARN์€ ํƒ€์ž„๋ผ์ธ ์„œ๋น„์Šค v.2๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํŠน์ง• YARN ํƒ€์ž„ ๋ผ์ธ ์„œ๋น„์Šค v.2๋Š” v.1 ๋ฐ v.1.5์— ์ด์€ ํƒ€์ž„ ๋ผ์ธ ์„œ๋ฒ„์˜ ๋‹ค์Œ ์ฃผ์š” ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. V.2๋Š” v.1์˜ ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ๊ณผ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. ํ™•์žฅ์„ฑ V.1์€ writer / reader ๋ฐ ์Šคํ† ๋ฆฌ์ง€์˜ ๋‹จ์ผ ์ธ์Šคํ„ด์Šค๋กœ ์ œํ•œ๋˜๋ฉฐ ์†Œ๊ทœ๋ชจ ํด๋Ÿฌ์Šคํ„ฐ ์ด์ƒ์œผ๋กœ ํ™•์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. V.2๋Š” ๋” ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ๋ถ„์‚ฐ ์ž‘์„ฑ๊ธฐ ์•„ํ‚คํ…์ฒ˜์™€ ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ๋ฐฑ์—”๋“œ ์ €์žฅ์†Œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. YARN Timeline Service v.2๋Š” ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ (์“ฐ๊ธฐ)๊ณผ ๋ฐ์ดํ„ฐ ์ œ๊ณต (์ฝ๊ธฐ)์„ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ YARN ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ ์ˆ˜์ง‘ ๊ธฐ์ธ ๋ถ„์‚ฐ ์ˆ˜์ง‘๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋”๋Š” REST API๋ฅผ ํ†ตํ•ด ์ฟผ๋ฆฌ๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ „์šฉ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. YARN Timeline Service v.2๋Š” Apache HBase๊ฐ€ ์ฝ๊ธฐ ๋ฐ ์“ฐ๊ธฐ์— ๋Œ€ํ•œ ์‘๋‹ต ์‹œ๊ฐ„์„ ์œ ์ง€ํ•˜๋ฉด์„œ ํฐ ํฌ๊ธฐ๋กœ ์ž˜ ํ™•์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— Apache HBase๋ฅผ ๊ธฐ๋ณธ ๋ฐฑ์—… ์Šคํ† ๋ฆฌ์ง€๋กœ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์œ ์šฉ์„ฑ ํ–ฅ์ƒ ๋งŽ์€ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž๋Š” ์ž‘์—…์˜ ์ง„ํ–‰ ํ๋ฆ„(flow) ๋˜๋Š” YARN ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ๋…ผ๋ฆฌ์  ๊ทธ๋ฃน ์ˆ˜์ค€์˜ ์ •๋ณด์— ๊ด€์‹ฌ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฆฌ์  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์™„๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด ์ผ๋ จ์˜ YARN ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ๋” ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ํƒ€์ž„ ๋ผ์ธ ์„œ๋น„์Šค v.2๋Š” ํ๋ฆ„ ๊ฐœ๋…์„ ๋ช…์‹œ ์ ์œผ๋กœ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ๋ฆ„ ์ˆ˜์ค€์—์„œ ๋ฉ”ํŠธ๋ฆญ ์ง‘๊ณ„๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ตฌ์„ฑ ๋ฐ ๋ฉ”ํŠธ๋ฆญ๊ณผ ๊ฐ™์€ ์ •๋ณด๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ •๋ณด๋กœ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๋‹ค์ด์–ด๊ทธ๋žจ์€ ์„œ๋กœ ๋‹ค๋ฅธ YARN ํ•ญ๋ชฉ ๋ชจ๋ธ๋ง ํ๋ฆ„ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ตฌ์กฐ YARN Timeline Service v.2๋Š” ์ˆ˜์ง‘๊ธฐ (writer) ์„ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฑ์—”๋“œ ์Šคํ† ๋ฆฌ์ง€์— ๋ฐ์ดํ„ฐ๋ฅผ ์”๋‹ˆ๋‹ค. ์ˆ˜์ง‘๊ธฐ๋Š” ์ „์šฉ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ๋งˆ์Šคํ„ฐ์™€ ํ•จ๊ป˜ ๋ฐฐํฌ๋˜๊ณ  ํ•จ๊ป˜ ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ์ž ํƒ€์ž„ ๋ผ์ธ ์ˆ˜์ง‘๊ธฐ๋ฅผ ์ œ์™ธํ•˜๊ณ  ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์†ํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ˆ˜์ค€ ํƒ€์ž„ ๋ผ์ธ ์ˆ˜์ง‘๊ธฐ๋กœ ์ „์†ก๋ฉ๋‹ˆ๋‹ค. ํŠน์ • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ€ํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๋Š” ํ•จ๊ป˜ ๋ฐฐ์น˜๋œ ํƒ€์ž„ ๋ผ์ธ ์ˆ˜์ง‘๊ธฐ (์ด ๋ฆด๋ฆฌ์Šค์˜ NM ๋ณด์กฐ ์„œ๋น„์Šค)์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐ์ดํ„ฐ๋ฅผ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ ์ค‘์ธ ๋‹ค๋ฅธ ๋…ธ๋“œ์˜ ๋…ธ๋“œ ๊ด€๋ฆฌ์ž๋„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๋ฅผ ์‹คํ–‰ ์ค‘์ธ ๋…ธ๋“œ์˜ ํƒ€์ž„ ๋ผ์ธ ์ˆ˜์ง‘๊ธฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ์”๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ์ž๋Š” ์ž์ฒด ํƒ€์ž„ ๋ผ์ธ ์ˆ˜์ง‘๊ธฐ๋„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์“ฐ๊ธฐ ๋ณผ๋ฅจ์„ ํ•ฉ๋ฆฌ์ ์œผ๋กœ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด YARN ์ผ๋ฐ˜ ์ˆ˜๋ช…์ฃผ๊ธฐ ์ด๋ฒคํŠธ ๋งŒ ๋‚ด ๋ณด๋ƒ…๋‹ˆ๋‹ค. ํƒ€์ž„ ๋ผ์ธ ๋ฆฌ๋”๋Š” ํƒ€์ž„ ๋ผ์ธ ์ˆ˜์ง‘ ๊ธฐ์™€๋Š” ๋ณ„๋„์˜ ๋ฐ๋ชฌ์ด๋ฉฐ REST API๋ฅผ ํ†ตํ•ด ์ฟผ๋ฆฌ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฐ ์ „๋…ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๋‹ค์ด์–ด๊ทธ๋žจ์€ ๋†’์€ ์ˆ˜์ค€์˜ ์„ค๊ณ„๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์„ค์ • ์‚ฌ์ „ ์ž‘์—… HBase ์„ค์ • ํƒ€์ž„ ๋ผ์ธ ์„œ๋ฒ„ v.2๋Š” ์ €์žฅ์†Œ๋กœ HBase๋ฅผ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— HBase๋ฅผ ๊ตฌ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. coprocessor ์„ค์ • coprocessor ์‚ฌ์šฉ์„ ์œ„ํ•ด์„œ jar ํŒŒ์ผ์„ HDFS์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํƒ€์ž„๋ผ์ธ ์„œ๋ฒ„ ์Šคํ‚ค๋งˆ ์ƒ์„ฑ ๊ตฌ์„ฑํ•œ HBase์— ํƒ€์ž„๋ผ์ธ ์„œ๋น„์Šค๋ฅผ ์œ„ํ•œ ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํƒ€์ž„๋ผ์ธ ์„œ๋ฒ„ ์Šคํ‚ค๋งˆ ์ƒ์„ฑ export HADOOP_CLASSPATH=/opt/hadoop/share/hadoop/yarn/timelineservice/hadoop-yarn-server-timelineservice-hbase-client-3.2.1.jar:/opt/hbase/lib/*:/opt/hadoop/share/hadoop/yarn/timelineservice/hadoop-yarn-server-timelineservice-hbase-common-3.2.1.jar hadoop org.apache.hadoop.yarn.server.timelineservice.storage.TimelineSchemaCreator -create -skipExistingTable ์ฐธ๊ณ  TimelineServiceV2 installing hadoop3 5-์ž‘์—… ์ง€์› ๋„๊ตฌ(Hadoop Common) ํ•˜๋‘ก์—๋Š” ์ž‘์—… ์ง€์› ๋ฐ ๋ชจ๋‹ˆํ„ฐ๋ง์„ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. 1-DistCp ํ•˜๋‘ก์€ ํด๋Ÿฌ์Šคํ„ฐ ๋‚ด์˜ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ ์ด๋™์„ ์œ„ํ•œ DistCp(Distribute Copy) ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ํŒŒ์ผ ๋ณต์‚ฌ ๋ช…๋ น์ธ hadoop fs -cp๋กœ๋Š” ํŒŒ์ผ์„ ํ•˜๋‚˜์”ฉ ๋ณต์‚ฌํ•˜์ง€๋งŒ DistCp ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜๋ฉด, ๋งต๋ฆฌ๋“€์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋Œ€๊ทœ๋ชจ์˜ ํŒŒ์ผ์„ ๋ณ‘๋ ฌ๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ์ด์šฉํ•˜๊ฒŒ ๋˜๊ณ  ํ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด ํ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋Œ€๊ทœ๋ชจ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ์ž‘์—…์ด๊ธฐ ๋•Œ๋ฌธ์— ๋„คํŠธ์›Œํฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ž˜ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋„ˆ๋ฌด ๋งŽ์€ ๋งคํผ๋ฅผ ํ• ๋‹นํ•˜๋ฉด ๋„คํŠธ์›Œํฌ ์ž์›์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜์—ฌ ์šด์˜์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šด์˜ ์ƒํ™ฉ์—์„œ DistCp ์ž‘์—… ์‹œ ๋„คํŠธ์›Œํฌ ์‚ฌ์šฉ๋Ÿ‰์„ ๊ผญ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ณ , ๋งคํผ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ ์ง„์ ์œผ๋กœ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ํด๋Ÿฌ์Šคํ„ฐ์˜ ์„ค์ •์ด ๋‹ค๋ฅด๋‹ค๋ฉด ๋ณต์‚ฌ ์œ„์น˜์˜ ๋ธ”๋ก ์‚ฌ์ด์ฆˆ, ๋ณต์ œ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์˜ต์…˜ ์˜ต์…˜ ๋น„๊ณ  -update ๋ณต์‚ฌ ์‹œ ํŒŒ์ผ ์ด๋ฆ„, ์‚ฌ์ด์ฆˆ๋ฅผ ๋น„๊ตํ•ด์„œ ๋ณต์‚ฌ -overwrite ๊ธฐ์กด ๋ณต์‚ฌ ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๊ณ  ๋ฎ์–ด์”€ -f ๋ณต์‚ฌ source ์œ„์น˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ ํŒŒ์ผ๋กœ ์ „๋‹ฌ. -m ๋งคํผ ๊ฐœ์ˆ˜ ์„ค์ • -D ํ•˜๋‘ก ์˜ต์…˜ ์ „๋‹ฌ ์‚ฌ์šฉ๋ฒ• ๋””์ŠคํŠธ ์นดํ”ผ ๋ช…๋ น์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์˜ต์…˜์€ ๋งค๋‰ด์–ผ 1์„ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. # a ํด๋”๋ฅผ b๋กœ ๋ณต์‚ฌ $ hadoop distcp hdfs:///user/a hdfs:///user/b # a, b ํด๋”๋ฅผ c๋กœ ๋ณต์‚ฌ $ hadoop distcp hdfs:///user/a hdfs:///user/b hdfs:///user/c # -D ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ํ์ด๋ฆ„ ์„ค์ • $ hadoop distcp -Dmapred.job.queue.name=queue hdfs:///user/a hdfs:///user/b # -D ์˜ต์…˜์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ ์ „๋‹ฌ $ hadoop distcp -Dmapreduce.map.memory.mb=2048 hdfs:///user/a hdfs:///user/b # ํŒŒ์ผ ์ด๋ฆ„, ์‚ฌ์ด์ฆˆ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋ณ€๊ฒฝ ๋‚ด์—ญ ์žˆ๋Š” ํŒŒ์ผ๋งŒ ์ด๋™ $ hadoop distcp -update hdfs:///user/a hdfs:///user/b hdfs:///user/c # ๋ชฉ์ ์ง€์˜ ํŒŒ์ผ์„ ๋ฎ์–ด์”€ $ hadoop distcp -overwrite hdfs:///user/a hdfs:///user/b hdfs:///user/c ๋งคํผ ๊ฐœ์ˆ˜ ์„ค์ • hadoop distcp \ -m 10 \ hdfs://source-nn/dir \ hdfs://target-nn/dir AWS S3 ๊ฐ„ ๋ฐ์ดํ„ฐ ์ด๋™ AWS์˜ S3์—์„œ distcp๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋™ํ•  ๋•Œ๋Š” ์˜ต์…˜ ๊ฐ’์— ์•ก์„ธ์Šคํ‚ค์™€ ์‹œํฌ๋ฆฟํ‚ค๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. hadoop distcp \ -Dfs.s3n.awsAccessKeyId=[source_์•ก์„ธ์Šคํ‚ค1] \ -Dfs.s3n.awsSecretAccessKey=[source_์‹œํฌ๋ฆฟํ‚ค 1] \ -Dfs.s3.awsAccessKeyId=[target_์•ก์„ธ์Šคํ‚ค2] \ -Dfs.s3.awsSecretAccessKey=[target_์‹œํฌ๋ฆฟํ‚ค 2] \ -Dmapred.job.queue.name=q2 \ s3n://[source_url] s3://[target_url] ์ผ๋ฐ˜ ํ•˜๋‘ก๊ณผ ์ปค๋ฒ„ ๋กœ์Šค ํ•˜๋‘ก ๊ฐ„ ๋ฐ์ดํ„ฐ ์ด๋™ ์ปค๋ฒ„ ๋กœ์Šค ์ ์šฉ๋œ ์‹œํ์–ด ํ•˜๋‘ก๊ณผ ์ผ๋ฐ˜ ํ•˜๋‘ก ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋™ํ•  ๋•Œ๋Š” ์‹œํ์–ด ํ•˜๋‘ก์—์„œ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹œํ์–ด ํ•˜๋‘ก์€ swebhdfs๋ฅผ ์ด์šฉํ•˜๊ณ , ์ผ๋ฐ˜ ํ•˜๋‘ก์€ webhdfs๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. hadoop distcp -D ipc.client.fallback-to-simple-auth-allowed=true \ webhdfs://source-nn:50070/dir \ swebhdfs://target-nn:50470/dir ์ปค๋ฒ„ ๋กœ์Šค ํ•˜๋‘ก ๊ฐ„ ๋ฐ์ดํ„ฐ ์ด๋™ ์‹œํ์–ด ํ•˜๋‘ก ๊ฐ„ ๋ฐ์ดํ„ฐ ์ด๋™ ์‹œ์—๋Š” ๋‹ค์Œ์˜ ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. hadoop distcp -Dmapreduce.job.hdfs-servers.token-renewal.exclude=target-nn \ webhdfs://source-nn:50070/dir \ swebhdfs://target-nn:50470/dir ๋””์ŠคํŠธ ์นดํ”ผ ๋งค๋‰ด์–ผ(๋ฐ”๋กœ ๊ฐ€๊ธฐ) โ†ฉ 2-ํ•˜๋‘ก ์•„์นด์ด๋ธŒ ํ•˜๋‘ก HDFS๋Š” ์ž‘์€ ์‚ฌ์ด์ฆˆ์˜ ํŒŒ์ผ์ด ๋งŽ์•„์ง€๋ฉด ๋„ค์ž„๋…ธ๋“œ์—์„œ ์ด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š”๋ฐ ๋งŽ์€ ์–ด๋ ค์›€์„ ๊ฒช๊ฒŒ ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ธ”๋ก ์‚ฌ์ด์ฆˆ ์ •๋„๋กœ ํŒŒ์ผ์„ ์œ ์ง€ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ํ•˜๋‘ก์€ ํŒŒ์ผ์„ ๋ฌถ์–ด์„œ ๊ด€๋ฆฌํ•˜๊ณ , ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜๋‘ก ์•„์นด์ด๋ธŒ(Hadoop Archive) ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‘ก ์•„์นด์ด๋ธŒ๋กœ ๋ฌถ์€ ํŒŒ์ผ์€ har ์Šคํ‚ค๋งˆ๋ฅผ ์ด์šฉํ•ด์„œ ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‘ก ์•„์นด์ด๋ธŒ๋Š” ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์„ ์ด์šฉํ•ด์„œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ๋œ ์•„์นด์ด๋ธŒ๋Š” ls ๋ช…๋ น์œผ๋กœ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. har ์Šคํ‚ค๋งˆ๋ฅผ ์ด์šฉํ•ด์„œ ํŒŒ์ผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์˜ ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌํ•˜๋ฉด ํ•˜๋‘ก์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ ํ•˜๋‘ก ์•„์นด์ด๋ธŒ ์ƒ์„ฑ์€ archive ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ์‚ฌ์šฉ ๋ฐฉ๋ฒ• hadoop archive -archiveName <NAME>.har -p <parent path> [-r <replication factor>]<src>* <dest> # ์‚ฌ์šฉ ์˜ˆ์ œ(ํ ์ด๋ฆ„ ์ ์šฉ) $ hadoop archive -archiveName -Dmapred.job.queue.name=queue_name sample.har -p /user/data/ /user/ 19/01/14 01:57:52 INFO mapreduce.Job: Job job_1520227878653_38308 running in uber mode : false 19/01/14 01:57:52 INFO mapreduce.Job: map 0% reduce 0% 19/01/14 01:57:56 INFO mapreduce.Job: map 100% reduce 0% 19/01/14 01:58:01 INFO mapreduce.Job: map 100% reduce 100% 19/01/14 01:58:01 INFO mapreduce.Job: Job job_1520227878653_38308 completed successfully 19/01/14 01:58:01 INFO mapreduce.Job: Counters: 49 File System Counters FILE: Number of bytes read=126 # sample.har ํ™•์ธ # ls ๋ช…๋ น์œผ๋กœ ๋ณด๋ฉด sample.har ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Œ $ hadoop fs -ls /user/ Found 1 items drwxr-xr-x - hadoop hadoop 0 2019-01-14 01:57 /user/sample.har # sample.har ๋””๋ ‰ํ„ฐ๋ฆฌ ํ™•์ธ $ hadoop fs -ls /user/sample.har/ Found 4 items -rw-r--r-- 2 hadoop hadoop 0 2019-01-14 01:57 /user/sample.har/_SUCCESS -rw-r--r-- 5 hadoop hadoop 117 2019-01-14 01:57 /user/sample.har/_index -rw-r--r-- 5 hadoop hadoop 23 2019-01-14 01:57 /user/sample.har/_masterindex -rw-r--r-- 2 hadoop hadoop 746 2019-01-14 01:57 /user/sample.har/part-0 # har ์Šคํ‚ค๋งˆ๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ํ™•์ธ $ hadoop fs -ls har:///user/sample.har/ Found 1 items -rw-r--r-- 2 hadoop hadoop 746 2018-05-23 04:15 har:///user/sample.har/test.txt ํ•ด์ œ ํ•˜๋‘ก ์•„์นด์ด๋ธŒ ํŒŒ์ผ์˜ ์••์ถ•์„ ํ•ด์ œํ•  ๋•Œ๋Š” distcp๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. #sample.har ์••์ถ• ํ•ด์ œ $ hadoop distcp -Dmapred.job.queue.name=queue_name har:///user/sample.har/ /user/decompress/ # ์••์ถ• ํ•ด์ œ ํ™•์ธ $ hadoop fs -ls /user/decompress/ Found 1 items -rw-r--r-- 2 hadoop hadoop 746 2019-01-14 04:04 /user/decompress/test.txt ์ฐธ๊ณ  ํ•˜๋‘ก ์•„์นด์ด๋ธŒ ๋งค๋‰ด์–ผ(๋ฐ”๋กœ ๊ฐ€๊ธฐ) 6-์˜ค๋ธŒ์ ํŠธ ์ €์žฅ์†Œ(Hadoop Ozone) ์˜ค์กด(Ozone)์€ ํ•˜๋‘ก์„ ์œ„ํ•œ ํ™•์žฅ์„ฑ(scalable) ์žˆ๋Š” ๋ถ„์‚ฐ ๊ฐ์ฒด ์ €์žฅ์†Œ(distributed object store)์ž…๋‹ˆ๋‹ค. 2020๋…„ 3์›” 24์ผ์— 0.5.0-beta ๋ฒ„์ „์ด ๋ฐœํ‘œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ŠคํŒŒํฌ, ํ•˜์ด๋ธŒ, YARN์€ ๋ณ„๋„์˜ ์ˆ˜์ • ์—†์ด ์˜ค์กด์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค์กด์€ ์ž๋ฐ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ CLI ํ™˜๊ฒฝ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ž๋ฐ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” RPC์™€ REST ํ”„๋กœํ† ์ฝœ์„ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Ozone ๊ตฌ์„ฑ์š”์†Œ ์˜ค์กด์€ ๋ณผ๋ฅจ, ๋ฒ„ํ‚ท, ํ‚ค๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ณผ๋ฅจ์€ ์‚ฌ์šฉ์ž ๊ณ„์ •๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ด€๋ฆฌ์ž๋งŒ ๋ณผ๋ฅจ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒ„ํ‚ท์€ ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„ํ‚ท์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ‚ค๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋‹ค๋ฅธ ๋ฒ„ํ‚ท์€ ์ €์žฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ‚ค๋Š” ํŒŒ์ผ๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„ํ‚ท์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ‚ค๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณผ๋ฅจ > ๋ฒ„ํ‚ท > ํ‚ค ๋‹จ์œ„๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. 7-์„ค์ • ํ•˜๋‘ก ๊ด€๋ จ ์ฃผ์š” ์„ค์ •์€ ${HADOOP_HOME}/conf ์•„๋ž˜ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์„ค์ • ํŒŒ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. core-site.xml ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ฃผ์š” ์„ค์ • hdfs-site.xml HDFS ๊ด€๋ จ ์„ค์ • mapred-site.xml ๋งต๋ฆฌ๋“€์Šค ์ž‘์—… ๊ด€๋ จ ์„ค์ • yarn-site.xml YARN ๊ด€๋ จ ์„ค์ • hadoop-env.sh ํ•˜๋‘ก์„ ์ด์šฉํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ • 1-core-site.xml Hadoop ๊ด€๋ จ ์ฃผ์š” ์„ค์ •์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์„ค์ • ์„ค์ • ๊ธฐ๋ณธ๊ฐ’ ๋น„๊ณ  fs.defaultFS file:/// ํ•˜๋‘ก์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ ‘๊ทผํ•  ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ์„ค์ •. hdfs://{ํ˜ธ์ŠคํŠธ๋ช…} ์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•จ ์ฐธ๊ณ  core-site.default - 3.3.2 2-hdfs-site.xml HDFS ๊ด€๋ จ ์ฃผ์š” ์„ค์ •์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋„ค์ž„ ๋…ธ๋“œ ์„ค์ • ๊ธฐ๋ณธ๊ฐ’ ๋น„๊ณ  dfs.namenode.name.dir file://${hadoop.tmp.dir}/dfs/name ๋„ค์ž„๋…ธ๋“œ์˜ ์—๋””ํŠธ ๋กœ๊ทธ๋ฅผ ์ €์žฅํ•  ์œ„์น˜๋ฅผ ์ง€์ • ์ฐธ๊ณ  hdfs-site.default - 2.10.2 3-yarn-site.xml YARN ๊ด€๋ จ ์ฃผ์š” ์„ค์ •์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋…ธ๋“œ ๋งค๋‹ˆ์ € ์„ค์ • ๊ธฐ๋ณธ๊ฐ’ ๋น„๊ณ  yarn.nodemanager.local-dirs /hadoop/yarn/local ๋…ธ๋“œ ๋งค๋‹ˆ์ €์˜ ์ž„์‹œ ํŒŒ์ผ ์ €์žฅ ์œ„์น˜. ํฐ ์‚ฌ์ด์ฆˆ์˜ ๋””์Šคํฌ๊ฐ€ ์ข‹์Œ ํƒ€์ž„๋ผ์ธ/์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํžˆ์Šคํ† ๋ฆฌ ์„œ๋ฒ„ ์„ค์ • ๊ธฐ๋ณธ๊ฐ’ ๋น„๊ณ  yarn.timeline-service.generic-application-history.max-applications 10000 ํƒ€์ž„๋ผ์ธ ์„œ๋ฒ„๊ฐ€ ๋ณด๊ด€ํ•  ์ตœ๋Œ€ ํžˆ์Šคํ† ๋ฆฌ ๊ฐœ์ˆ˜ 4-mapred-site.xml ๋งต๋ฆฌ๋“€์Šค ์ž‘์—… ๊ด€๋ จ ์ฃผ์š” ์„ค์ •์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์„ค์ • ๊ธฐ๋ณธ๊ฐ’ ๋น„๊ณ  yarn.app.mapreduce.am.job.client.port-range ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์˜ AM์ด ๋ฐ”์ธ๋”ฉ ๋˜๋Š” ํฌํŠธ ๋ ˆ์ธ์ง€ yarn.app.mapreduce.am.webapp.port-range ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์˜ AM์ด ์‹คํ–‰ํ•˜๋Š” ์›น์•ฑ์ด ๋ฐ”์ธ๋”ฉ ๋˜๋Š” ํฌํŠธ ๋ ˆ์ธ์ง€ 5-๋ณด์•ˆ ์„ค์ • ์•”ํ˜ธํ™” ์„ค์ • ์‹œ ์ฃผ์˜ํ•  ์ ์€ ์•”ํ˜ธํ™”์— ๋”ฐ๋ฅธ ์˜ค๋ฒ„ํ—ค๋“œ๋กœ ์ธํ•ด์„œ ์ฝ๊ธฐ, ์“ฐ๊ธฐ ์„ฑ๋Šฅ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•”ํ˜ธํ™” ์„ค์ • ์„ค์ •๊ฐ’ dfs.encrypt.data.transfer true dfs.encrypt.data.transfer.algorithm 3des dfs.data.transfer.protection privacyintegrity, authentication dfs.encrypt.data.transfer HDFS ๋ฐ์ดํ„ฐ ์ „์†ก ์ฑ„๋„ ๋ฐ ํด๋ผ์ด์–ธํŠธ๊ฐ€ HDFS์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ์ฑ„๋„์ด ์•”ํ˜ธํ™”๋˜์—ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. HDFS ๋ฐ์ดํ„ฐ ์ „์†ก ์ฑ„๋„์—๋Š” DataNode ์‚ฌ์ด์˜ ๋ฐ์ดํ„ฐ ์ „์†ก ์ฑ„๋„๊ณผ ํด๋ผ์ด์–ธํŠธ๊ฐ€ DataNode์— ์•ก์„ธ์Šคํ•˜๊ธฐ ์œ„ํ•œ DT(Data Transfer) ์ฑ„๋„์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. true ๊ฐ’ ์€ ์ฑ„๋„์ด ์•”ํ˜ธํ™”๋˜์—ˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ฑ„๋„์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์•”ํ˜ธํ™”๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. dfs.encrypt.data.transfer.algorithm HDFS ๋ฐ์ดํ„ฐ ์ „์†ก ์ฑ„๋„ ๋ฐ ํด๋ผ์ด์–ธํŠธ๊ฐ€ HDFS์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ์ฑ„๋„์ด ์•”ํ˜ธํ™”๋˜์—ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” dfs.encrypt.data.transfer๊ฐ€ true๋กœ ์„ค์ •๋œ ๊ฒฝ์šฐ์—๋งŒ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 3des์ด๋ฉฐ, ์ด๋Š” 3DES ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์•”ํ˜ธํ™”์— ์‚ฌ์šฉ๋จ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ฐ’์„ rc4๋กœ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ณด์•ˆ ์œ„ํ—˜์„ ๋ฐฉ์ง€ํ•˜๋ ค๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ด ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค. dfs.data.transfer.protection Hadoop์— ์žˆ๋Š” ๊ฐ ๋ชจ๋“ˆ์˜ RPC ์ฑ„๋„์ด ์•”ํ˜ธํ™”๋˜์—ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ฑ„๋„์—๋Š” ๋‹ค์Œ์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ํด๋ผ์ด์–ธํŠธ๊ฐ€ HDFS์— ์•ก์„ธ์Šคํ•˜๊ธฐ ์œ„ํ•œ RPC ์ฑ„๋„ HDFS์˜ ๋ชจ๋“ˆ ๊ฐ„ RPC ์ฑ„๋„(์˜ˆ: DataNode์™€ NameNode ๊ฐ„์˜ RPC ์ฑ„๋„) ํด๋ผ์ด์–ธํŠธ๊ฐ€ YARN์— ์•ก์„ธ์Šคํ•˜๊ธฐ ์œ„ํ•œ RPC ์ฑ„๋„ NodeManager์™€ ResourceManager ๊ฐ„์˜ RPC ์ฑ„๋„ YARN ๋ฐ HDFS์— ์•ก์„ธ์Šคํ•˜๊ธฐ ์œ„ํ•œ Spark ์šฉ RPC ์ฑ„๋„ MapReduce๊ฐ€ YARN ๋ฐ HDFS์— ์•ก์„ธ์Šคํ•˜๊ธฐ ์œ„ํ•œ RPC ์ฑ„๋„ HBase๊ฐ€ HDFS์— ์•ก์„ธ์Šคํ•˜๊ธฐ ์œ„ํ•œ RPC ์ฑ„๋„ ๊ฐœ์ธ ์ •๋ณด ์ฑ„๋„์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์•”ํ˜ธํ™”๋˜์–ด ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ธ์ฆ ์ฑ„๋„์ด ์•”ํ˜ธํ™”๋˜์ง€ ์•Š์€ ๊ฒƒ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. 3-ํ•˜์ด๋ธŒ(hive) ํ•˜์ด๋ธŒ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ” ํ•จ์ˆ˜ ํŠธ๋žœ์žญ์…˜ ์„ฑ๋Šฅ ์ตœ์ ํ™” 1-ํ•˜์ด๋ธŒ๋ž€? ํ•˜์ด๋ธŒ๋Š” ํ•˜๋‘ก ์—์ฝ”์‹œ์Šคํ…œ ์ค‘์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋ธ๋งํ•˜๊ณ  ํ”„๋กœ์„ธ์‹ฑ ํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ ์›จ์–ดํ•˜์šฐ์ง•์šฉ ์„ค๋ฃจ์…˜์ž…๋‹ˆ๋‹ค. RDB์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ”๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ HDFS์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋ฉฐ, ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋Œ€์ƒ์œผ๋กœ SQL๊ณผ ์œ ์‚ฌํ•œ HiveQL ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Hive ๊ตฌ์„ฑ์š”์†Œ ํ•˜์ด๋ธŒ๋Š” ๋‹ค์Œ์˜ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. UI ์‚ฌ์šฉ์ž๊ฐ€ ์ฟผ๋ฆฌ ๋ฐ ๊ธฐํƒ€ ์ž‘์—…์„ ์‹œ์Šคํ…œ์— ์ œ์ถœํ•˜๋Š” ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค CLI, Beeline, JDBC ๋“ฑ Driver ์ฟผ๋ฆฌ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ์ž‘์—…์„ ์ฒ˜๋ฆฌ ์‚ฌ์šฉ์ž ์„ธ์…˜์„ ๊ตฌํ˜„ํ•˜๊ณ , JDBC/ODBC ์ธํ„ฐํŽ˜์ด์Šค API ์ œ๊ณต Compiler ๋ฉ”ํƒ€ ์Šคํ† ์–ด๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ฟผ๋ฆฌ ๊ตฌ๋ฌธ์„ ๋ถ„์„ํ•˜๊ณ  ์‹คํ–‰๊ณ„ํš์„ ์ƒ์„ฑ Metastore ๋””๋น„, ํ…Œ์ด๋ธ”, ํŒŒํ‹ฐ์…˜์˜ ์ •๋ณด๋ฅผ ์ €์žฅ Execution Engine ์ปดํŒŒ์ผ๋Ÿฌ์— ์˜ํ•ด ์ƒ์„ฑ๋œ ์‹คํ–‰ ๊ณ„ํš์„ ์‹คํ–‰ ํ•˜์ด๋ธŒ ์‹คํ–‰ ์ˆœ์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ œ์ถœํ•œ SQL ๋ฌธ์„ ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์ปดํŒŒ์ผ๋Ÿฌ์— ์š”์ฒญํ•˜์—ฌ ๋ฉ”ํƒ€ ์Šคํ† ์–ด์˜ ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ์ฒ˜๋ฆฌ์— ์ ํ•ฉํ•œ ํ˜•ํƒœ๋กœ ์ปดํŒŒ์ผ ์ปดํŒŒ์ผ๋œ SQL์„ ์‹คํ–‰ ์—”์ง„์œผ๋กœ ์‹คํ–‰ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๊ฐ€ ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ž์›์„ ์ ์ ˆํžˆ ํ™œ์šฉํ•˜์—ฌ ์‹คํ–‰ ์‹คํ–‰ ์ค‘ ์‚ฌ์šฉํ•˜๋Š” ์›์ฒœ ๋ฐ์ดํ„ฐ๋Š” HDFS ๋“ฑ์˜ ์ €์žฅ ์žฅ์น˜๋ฅผ ์ด์šฉ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐ˜ํ™˜ 1-ํ•˜์ด๋ธŒ ๋ฒ„์ „๋ณ„ ํŠน์ง• ํ•˜์ด๋ธŒ๋Š” SQL์„ ํ•˜๋‘ก์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํŽ˜์ด์Šค๋ถ์—์„œ ์ž์‚ฌ์˜ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•ด ๊ฐœ๋ฐœํ•˜์—ฌ ์•„ํŒŒ์น˜ ์˜คํ”ˆ์†Œ์Šค ํ”„๋กœ์ ํŠธ๋กœ ๋„˜์–ด์™”์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ์˜ ๊ฐ ๋ฒ„์ „๋ณ„ ํŠน์ง•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Hive 1.0 2012๋…„ 0.10 ๋ฒ„์ „์„ ์‹œ์ž‘์œผ๋กœ ์ ์ฐจ ๋ฐœ์ „ํ•˜์—ฌ, 2015๋…„ 2์›” 1.0 ๋ฒ„์ „์ด ๋ฐœํ‘œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํŠน์ง• SQL์„ ์ด์šฉํ•œ ๋งต๋ฆฌ๋“€์Šค ์ฒ˜๋ฆฌ ํŒŒ์ผ ๋ฐ์ดํ„ฐ์˜ ๋…ผ๋ฆฌ์  ํ‘œํ˜„ ๋น…๋ฐ์ดํ„ฐ์˜ ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๋ฅผ ๋ชฉํ‘œ Hive 2.0 ํ•˜์ด๋ธŒ 1.0์„ ๊ฐœ์„ ํ•˜์—ฌ 2016๋…„ 2์›” 2.0 ๋ฒ„์ „์ด ๋ฐœํ‘œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. LLAP์˜ ๋“ฑ์žฅ๊ณผ ๊ธฐ๋ณธ ์‹คํ–‰ ์—”์ง„์ด TEZ๋กœ ๋ณ€๊ฒฝ๋˜์–ด ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํŠน์ง• LLAP(Live Long and Process) ๊ตฌ์กฐ ์ถ”๊ฐ€ Spark ์ง€์› ๊ฐ•ํ™” CBO ๊ฐ•ํ™” HPLSQL ์ถ”๊ฐ€ LLAP ์ž‘์—…์„ ์‹คํ–‰ํ•œ ๋ฐ๋ชฌ์„ ๊ณ„์† ์œ ์ง€ํ•˜์—ฌ, ํ•ซ ๋ฐ์ดํ„ฐ๋ฅผ ์บ์Šํ•˜์—ฌ ํ•  ์ˆ˜ ์žˆ์–ด ๋น ๋ฅธ ์†๋„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. LLAP๋Š” ์ž‘์—…์„ ๋„์™€์ฃผ๋Š” ๋ณด์กฐ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” MR, TEZ ๊ฐ™์€ ์ž‘์—… ์—”์ง„์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋˜ํ•œ HDFS ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์˜๊ตฌํžˆ ์ €์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์—… ๋ชจ๋“œ๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, TEZ ์—”์ง„์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ๋งค๋‰ด์–ผ LLAP [๋ฐ”๋กœ ๊ฐ€๊ธฐ] HPLSQL ์˜ค๋ผํด์˜ PL/SQL๊ณผ ๋น„์Šทํ•œ Procedural SQL์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ์„ ๋ชฉํ‘œ๋กœ ๊ฐœ๋ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด FOR๋ฅผ ์ด์šฉํ•œ ๋ฃจํ”„๋ฌธ์ด๋‚˜ ์ปค์„œ ๋“ฑ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. FOR i IN 1.. 10 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP Hive 3.0 2018๋…„ 5์›” 3.0 ๋ฒ„์ „์ด ๋ฐœํ‘œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ์—”์ง„, ํ•˜์ด๋ธŒ CLI๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  TEZ ์—”์ง„๊ณผ ๋น„ ๋ผ์ธ์„ ์ด์šฉํ•˜์—ฌ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํŠน์ง• ๋กค์„ ์ด์šฉํ•œ ์ž‘์—… ์ƒํƒœ ๊ด€๋ฆฌ(workload management) ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ๊ฐ•ํ™” ๊ตฌ์ฒดํ™” ๋ทฐ(Materialized View) ์ถ”๊ฐ€ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์บ์Šํ•˜์—ฌ ๋” ๋น ๋ฅธ ์†๋„๋กœ ์ž‘์—… ๊ฐ€๋Šฅ ํ…Œ์ด๋ธ” ์ •๋ณด ๊ด€๋ฆฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ถ”๊ฐ€ workload ๊ด€๋ฆฌ ๋กค, ๊ถŒํ•œ์„ ์ด์šฉํ•œ ์ž‘์—… ์ƒํƒœ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. SQL์„ ์ด์šฉํ•˜์—ฌ ์›Œํฌ ๋กœ๋“œ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ๋กค์„ ์ƒ์„ฑํ•˜๊ณ  ์ด๋ฅผ ์ ์šฉํ•˜์—ฌ ์ž‘์—…์˜ ๋ถ€ํ•˜์— ๋”ฐ๋ผ ์ฟผ๋ฆฌ์˜ ์„ฑ๋Šฅ, ์‹คํ–‰ ์—ฌ๋ถ€๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CREATE RESOURCE PLAN daytime; Materialized View ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๋Š” ๊ตฌ์ฒดํ™” ๋ทฐ(Materialized View) ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ๋ณด๊ด€ํ•˜์—ฌ ์ฟผ๋ฆฌ ์ˆ˜ํ–‰ ์‹œ ๋น ๋ฅธ ์†๋„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CREATE MATERIALIZED VIEW mv1 AS SELECT empid, deptname, hire_date FROM emps JOIN depts ON (emps.deptno = depts.deptno) WHERE hire_date >= '2016-01-01'; ํ•˜์ด๋ธŒ ๊ตฌ์ฒดํ™” ๋ทฐ ๋งค๋‰ด์–ผ (๋ฐ”๋กœ ๊ฐ€๊ธฐ) ํ…Œ์ด๋ธ” ์ •๋ณด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ถ”๊ฐ€ ๊ธฐ์กด์—๋Š” ํ•˜์ด๋ธŒ ๋ฉ”ํƒ€ ์Šคํ† ์–ด์—๋งŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋˜ ์ •๋ณด๋ฅผ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ํ†ตํ•ด์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ ์ •๋ณด, ํ†ต๊ณ„์ •๋ณด ๋“ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ  ์ปค๋„ฅํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ ‘๊ทผํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. use information_schema; use sys; ์ฐธ๊ณ  ํ•˜์ด๋ธŒ 2, ํ•˜์ด๋ธŒ 3์˜ ์ƒˆ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์ฐธ๊ณ  ์ž๋ฃŒ๋“ค์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋ฐœํ‘œ ์ž๋ฃŒ๋“ค์„ ํ™•์ธํ•ด ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ 2 ํ•˜์ด๋ธŒ 2์˜ 10๊ฐ€์ง€ ์ƒˆ ๊ธฐ๋Šฅ (๋ฐ”๋กœ ๊ฐ€๊ธฐ) ํ•˜์ด๋ธŒ 2 - SQL, Speed, Scale (๋ฐ”๋กœ ๊ฐ€๊ธฐ) ํ•˜์ด๋ธŒ 3 ํ•˜์ด๋ธŒ 3์˜ ์ƒˆ ๊ธฐ๋Šฅ (๋ฐ”๋กœ ๊ฐ€๊ธฐ) ํ•˜์ด๋ธŒ 3 - New Horizon(๋ฐ”๋กœ ๊ฐ€๊ธฐ) ํ•˜์ด๋ธŒ 3์˜ ์ƒˆ ๊ธฐ๋Šฅ ์†Œ๊ฐœ (๋ฐ”๋กœ ๊ฐ€๊ธฐ) 2-ํ•˜์ด๋ธŒ ์„œ๋น„์Šค ํ•˜์ด๋ธŒ๋Š” ํŽธ๋ฆฌํ•œ ์ž‘์—…์„ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ์Šคํ† ์–ด ๋ฉ”ํƒ€ ์Šคํ† ์–ด ์„œ๋น„์Šค๋Š” HDFS ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ์Šคํ† ์–ด๋Š” 3๊ฐ€์ง€ ์‹คํ–‰ ๋ชจ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ๋กœ ๋™์ž‘ํ•  ๋•Œ๋Š” ์ž„๋ฒ ์ด๋””๋“œ ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์‹ค์ œ ์šด์˜์—์„œ๋Š” ๋ฆฌ๋ชจํŠธ ๋ชจ๋“œ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ์ด๋””๋“œ(Embeded) ๋ณ„๋„์˜ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ๊ตฌ์„ฑํ•˜์ง€ ์•Š๊ณ  ๋”๋น„ DB๋ฅผ ์ด์šฉํ•œ ๋ชจ๋“œ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์œ ์ €๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ ๋กœ์ปฌ(Local) ๋ณ„๋„์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, ํ•˜์ด๋ธŒ ๋“œ๋ผ์ด๋ฒ„์™€ ๊ฐ™์€ JVM์—์„œ ๋™์ž‘ ๋ฆฌ๋ชจํŠธ(Remode) ๋ณ„๋„์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ , ๋ณ„๋„์˜ JVM์—์„œ ๋‹จ๋…์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ๋ชจ๋“œ ๋ฆฌ๋ชจํŠธ๋กœ ๋™์ž‘ํ•˜๋Š” ํ•˜์ด๋ธŒ ๋ฉ”ํƒ€ ์Šคํ† ์–ด๋ฅผ HCat ์„œ๋ฒ„ 1๋ผ๊ณ ๋„ ํ•จ ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2(hiveserver2) ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2๋Š” ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ๊ฐœ๋ฐœ๋œ ํด๋ผ์ด์–ธํŠธ์™€ ์—ฐ๋™ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 1์„ ๊ฐœ์„ ํ•˜์—ฌ ์ธ์ฆ๊ณผ ๋‹ค์ค‘ ์‚ฌ์šฉ์ž ๋™์‹œ์„ฑ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์“ฐ ๋ฆฌํ”„ํŠธ, JDBC, ODBC ์—ฐ๊ฒฐ์„ ์‚ฌ์šฉํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ํ†ต์‹ ํ•˜์—ฌ ํ•˜์ด๋ธŒ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋น„ ๋ผ์ธ(beeline) ์ผ๋ฐ˜์ ์ธ CLI์ฒ˜๋Ÿผ ๋‚ด์žฅํ˜• ๋ชจ๋“œ๋กœ ์ž‘๋™ํ•˜๊ฑฐ๋‚˜ JDBC๋กœ ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2 ํ”„๋กœ์„ธ์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜์ด๋ธŒ์˜ ๋ช…๋ นํ–‰ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. CLI๋Š” ๋กœ์ปฌ ํ•˜์ด๋ธŒ ์„œ๋น„์Šค์—๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋น„ ๋ผ์ธ์€ ์›๊ฒฉ ํ•˜์ด๋ธŒ ์„œ๋น„์Šค์— ์ ‘์†ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. HCatalog HCatalog๋Š” Pig, MapReduce, Spark์—์„œ ํ•˜์ด๋ธŒ์˜ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์ถ”์ƒ ๊ณ„์ธต์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. % spark-shell --jars /usr/lib/hive-hcatalog/share/hcatalog/hive-hcatalog-core-1.0.0-amzn-3.jar scala> val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc); scala> val df = hiveContext.sql("SELECT * FROM impressions") scala> df.show() EMR-Hcatalog using WebHCat HCatalog์˜ ๊ธฐ๋Šฅ์„ REST API๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ 50111 ํฌํŠธ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. # WebHCat ๋ฒ„์ „ ํ™•์ธ curl -s 'http://localhost:50111/templeton/v1/version' # ๋ช…๋ น ์‹คํ–‰ curl -s -d execute="show tables" \ -d statusdir="pokes.output" \ 'http://localhost:50111/templeton/v1/hive? user.name=root' WebHCat Reference HCatalogInstallHCat-HCatalogServer โ†ฉ 3-ํ•˜์ด๋ธŒ CLI ํ•˜์ด๋ธŒ CLI(Command Line Interface)๋Š” ํ•˜์ด๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ์ธํ„ฐ๋ž™ํ‹ฐ๋ธŒ ์‰˜์„ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ๋ช…๋ น์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฟผ๋ฆฌ ์‹คํ–‰ ๋ฐฉ๋ฒ•, ์œ ์šฉํ•œ ์˜ต์…˜, ๋‚ด๋ถ€ ๋ช…๋ น์–ด๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ƒ์„ธํ•œ ๋‚ด์šฉ์€ ํ•˜์ด๋ธŒ ๋งค๋‰ด์–ผ 1์„ ์ฐธ๊ณ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. CLI ์˜ต์…˜ ํ•˜์ด๋ธŒ CLI์˜ ์ฃผ์š” ์˜ต์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. hiveconf๋Š” ์˜ต์…˜ ๊ฐ’์„ ์„ค์ •ํ•  ๋•Œ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. hivevar๋Š” ์ฟผ๋ฆฌ์— ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•  ๋•Œ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. usage: hive -e <quoted-query-string> ์ปค๋งจ๋“œ ๋ผ์ธ์œผ๋กœ ์‹คํ–‰ํ•  ์ฟผ๋ฆฌ -f <filename> ์ฟผ๋ฆฌ๊ฐ€ ์ž‘์„ฑ๋œ ํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ --hiveconf <property=value> ํ•˜์ด๋ธŒ ์„ค์ •๊ฐ’ ์ž…๋ ฅ ์˜ˆ) --hiveconf tez.queue.name=queue --hivevar <key=value> ์ฟผ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ ์ž…๋ ฅ ์˜ˆ) --hivevar targetDate=20180101 -- ์˜ต์…˜ ์ง€์ • ๋ฐฉ๋ฒ• $ hive --hiveconf tez.queue.name=queue --hivevar targetDate=20180101 ์ฟผ๋ฆฌ ์‹คํ–‰ ํ•˜์ด๋ธŒ ์ฟผ๋ฆฌ ์‹คํ–‰์€ ์ธํ„ฐ๋ž™ํ‹ฐ๋ธŒ ์‰˜์—์„œ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•, ์ปค๋งจ๋“œ ๋ผ์ธ ์ž…๋ ฅ, ํŒŒ์ผ ์ž…๋ ฅ ๋ฐฉ๋ฒ• ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธํ„ฐ๋ž™ํ‹ฐ๋ธŒ ์‰˜ ์ž…๋ ฅ ํ•˜์ด๋ธŒ CLI๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ์‰˜์„ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. -- hive ์‹คํ–‰ ํ›„ ์ฟผ๋ฆฌ ์ž…๋ ฅ $ hive hive> select 0; OK Time taken: 1.367 seconds, Fetched: 1 row(s) hive> select * from table; ์ปค๋งจ๋“œ ๋ผ์ธ ์ž…๋ ฅ ์ปค๋งจ๋“œ ๋ผ์ธ ์ž…๋ ฅ์€ -e ์˜ต์…˜์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. -- ์ปค๋งจ๋“œ ๋ผ์ธ์—์„œ ๋ฐ”๋กœ ์ž…๋ ฅ $ hive -e "select * from table" -- ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ์˜ ์‹คํ–‰ ์—”์ง„๊ณผ, ๋ณ€์ˆ˜๋ฅผ ์„ค์ • -- ์ปค๋งจ๋“œ ๋ผ์ธ ์ž…๋ ฅ์—์„œ ์„ค์ •๊ฐ’ ์ „๋‹ฌ $ hive -e 'SELECT * FROM table WHERE yymmdd=${hivevar:targetDate}' \ --hiveconf hie.execution.engine=tez \ --hiveconf tez.queue.name=queue_name \ --hivevar targetDate=20180101 ํŒŒ์ผ ์ž…๋ ฅ ํŒŒ์ผ ์ž…๋ ฅ์€ ์ฟผ๋ฆฌ๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•ด ๋†“๊ณ  ํ•ด๋‹น ํŒŒ์ผ์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. -f ์˜ต์…˜์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. $ cat sample.hql SELECT * FROM table WHERE yymmdd=${hivevar:targetDate} -- ํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ ์ž…๋ ฅ $ hive -f sample.hql --hivevar targetDate=20180101 ๋กœ๊น… ๋ฐฉ๋ฒ• ํ•˜์ด๋ธŒ CLI๋Š” log4j๋ฅผ ์ด์šฉํ•˜์—ฌ ๋กœ๊น…ํ•ฉ๋‹ˆ๋‹ค. ๋กœ๊น… ๋ฐฉ๋ฒ•์„ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” log4j ์„ค์ • ํŒŒ์ผ์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ --hiveconf ์˜ต์…˜์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. # ํŒŒ์ผ ๋กœ๊น… -- ๋กœ๊น… ๋ ˆ๋ฒจ, ํŒŒ์ผ ์œ„์น˜ ๋ณ€๊ฒฝ hive --hiveconf hive.log.file=hive_debug.log \ --hiveconf hive.log.dir=./ \ --hiveconf hive.root.logger=DEBUG, DRFA # ์ฝ˜์†” ์ถœ๋ ฅ hive --hiveconf hive.root.logger=DEBUG, console CLI ๋‚ด๋ถ€ ๋ช…๋ น์–ด ํ•˜์ด๋ธŒ CLI์˜ ์ฃผ์š” ๋‚ด๋ถ€ ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ƒ์„ธํ•œ ์„ค๋ช…์€ ํ•˜์ด๋ธŒ ๋งค๋‰ด์–ผ 2๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ปค๋งจ๋“œ ์„ค๋ช… exit ์ข…๋ฃŒ reset ์„ค์ •๊ฐ’ ์ดˆ๊ธฐํ™” set <key>=<value> ์„ค์ •๊ฐ’ ์ž…๋ ฅ set ํ•˜์ด๋ธŒ์˜ ์„ค์ •๊ฐ’ ์ถœ๋ ฅ set -v ํ•˜๋‘ก, ํ•˜์ด๋ธŒ์˜ ์„ค์ •๊ฐ’ ์ถœ๋ ฅ add file <> ํŒŒ์ผ ์ถ”๊ฐ€ add files <> ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ ์ถ”๊ฐ€, ๊ณต๋ฐฑ์œผ๋กœ ๊ฒฝ๋กœ ๊ตฌ๋ถ„ add jar <> jar ํŒŒ์ผ ์ถ”๊ฐ€ add jars <> ์—ฌ๋Ÿฌ ๊ฐœ์˜ jar ํŒŒ์ผ ์ถ”๊ฐ€, ๊ณต๋ฐฑ์œผ๋กœ ๊ฒฝ๋กœ ๊ตฌ๋ถ„ !<command> ์‰˜ ์ปค๋งจ๋“œ ์‹คํ–‰ dfs <dfs command> ํ•˜๋‘ก dfs ์ปค๋งจ๋“œ ์‹คํ–‰ jar ํŒŒ์ผ ์ถ”๊ฐ€๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌํ˜„ํ•œ UDF, SerDe ๋“ฑ ์ž๋ฐ” ํด๋ž˜์Šค ์‚ฌ์šฉํ•  ๋•Œ jar ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ์ถ”๊ฐ€๋Š” ์Šคํฌ๋ฆฝํŠธ๋‚˜ UDF์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”ํƒ€ ํŒŒ์ผ ๋“ฑ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. -- ์˜ต์…˜ ์„ค์ • hive> set mapred.reduce.tasks=32; -- ๋ชจ๋“  ์˜ต์…˜ ๊ฐ’ ํ™•์ธ hive> set; -- CLI ์ƒ์—์„œ ์„ค์ •ํ•œ ์˜ต์…˜ ์ดˆ๊ธฐํ™” hive> reset; -- ์‰˜์ปค๋งจ๋“œ ์‹คํ–‰ hive> !ls -alh; -- dfs ์ปค๋งจ๋“œ ์‹คํ–‰ hive> dfs -ls /user/; -- ํŒŒ์ผ ์ถ”๊ฐ€ hive> add file hdfs:///user/sample.txt; -- ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ ์ถ”๊ฐ€. ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ hive> add files hdfs:///user/sample1.txt hdfs:///user/sample2.txt; -- jar ํŒŒ์ผ ์ถ”๊ฐ€ hive> add jar hdfs:///user/sample.jar; ํ•˜์ด๋ธŒ CLI ๋งค๋‰ด์–ผ ๋ฐ”๋กœ ๊ฐ€๊ธฐ โ†ฉ ํ•˜์ด๋ธŒ CLI ์ปค๋งจ๋“œ ๋ฐ”๋กœ ๊ฐ€๊ธฐ โ†ฉ 4-๋น„ ๋ผ์ธ(beeline) ๋น„ ๋ผ์ธ์€ SQLLine1 ๊ธฐ๋ฐ˜์˜ ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2(hiveserver2)์— ์ ‘์†ํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. JDBC๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค. ๋น„ ๋ผ์ธ ์ ‘์† ์˜ต์…˜์€ ํ•˜์ด๋ธŒ ๋งค๋‰ด์–ผ 2๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋น„ ๋ผ์ธ ์ ‘์†-ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2 ๋น„ ๋ผ์ธ์—์„œ ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2์— ์ ‘์†ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋น„ ๋ผ์ธ CLI๋ฅผ ์‹คํ–‰ ํ›„์— ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋น„ ๋ผ์ธ CLI๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ์˜ต์…˜์œผ๋กœ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2์— ์ ‘์†ํ•˜๋ฉด ํ”„๋กฌํ”„ํŠธ์— ์ ‘์†ํ•œ ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2์˜ ์ ‘์† ์ •๋ณด๊ฐ€ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2์˜ ์ ‘๊ทผ ํฌํŠธ๋Š” hive.server2.thrift.port์— ์„ค์ •๋œ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. # connect ๋ช…๋ น์„ ์ด์šฉํ•œ ์ ‘์† ๋ฐฉ๋ฒ• $ beeline beeline> !connect jdbc:hive2://localhost:10000 scott tiger 0: jdbc:hive2://localhost:10000> # ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜์„ ์ด์šฉํ•œ ์ ‘์† ๋ฐฉ๋ฒ• $ beeline -u jdbc:hive2://localhost:10000 -n scott -p tiger 0: jdbc:hive2://localhost:10000> ๋น„ ๋ผ์ธ ์ ‘์†-TLS/SSL ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2 SSL ์ ์šฉ๋œ ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2์— ์ ‘๊ทผํ•  ๋•Œ๋Š” transportMode, httpPath, sslTrustStore, trustStorePassword๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. SSL ์ ์šฉ๋œ ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2๋Š” HTTP ๋ชจ๋“œ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๊ณ , Http ๋ชจ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 10001๋ฒˆ ํฌํŠธ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํฌํŠธ๋„ ๋ณ€๊ฒฝํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. beeline> !connect jdbc:hive2://localhost:10001/default;transportMode=http;httpPath=/cliservice;ssl=true;sslTrustStore=/jks-file-path;trustStorePassword=jks-password ๋น„ ๋ผ์ธ ์ ‘์†-์ฃผํ‚คํผ ๋น„ ๋ผ์ธ์€ ์ฃผํ‚คํผ์— ์˜ํ•ด์„œ HA ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2์— ์ ‘์†ํ•˜์—ฌ ๋น„ ๋ผ์ธ CLI๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผํ‚คํผ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ ‘์† ์ฃผ์†Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. jdbc:hive2://[ZOOKEEPER_QUORUM]/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2 # ํ˜ธ์ŠคํŠธ๋ช…, ์ฃผํ‚คํผ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์ฃผ์˜ํ•ด์„œ ์‹ ์ฒญ $ beeline> !connect jdbc:hive2://host1:2181, host2:2181, host3:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=[hiveserver2_zooKeeper_namespace] [user_name] [pass_word] ๋น„ ๋ผ์ธ SQLLine ์ปค๋งจ๋“œ ๋น„ ๋ผ์ธ์—์„œ๋Š” ๋Š๋‚Œํ‘œ๋ฅผ ์ด์šฉํ•˜์—ฌ SQLLine ์ปค๋งจ๋“œ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SQLLine ์ปค๋งจ๋“œ์˜ ์ƒ์„ธํ•œ ๋‚ด์šฉ์€ ๋งค๋‰ด์–ผ์„ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 0: jdbc:hive2://localhost:10000> !columns tbl; 0: jdbc:hive2://localhost:10000> !quit 0: jdbc:hive2://localhost:10000> !columns tbl; ๋น„ ๋ผ์ธ ํ•˜์ด๋ธŒ ์ปค๋งจ๋“œ ๋น„ ๋ผ์ธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ฃผ์š” ํ•˜์ด๋ธŒ ์ปค๋งจ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. CLI์™€ ๋™์ผํ•œ ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปค๋งจ๋“œ ์„ค๋ช… reset ์„ค์ •๊ฐ’ ์ดˆ๊ธฐํ™” set <key>=<value> ์„ค์ •๊ฐ’ ์ž…๋ ฅ set ํ•˜์ด๋ธŒ์˜ ์„ค์ •๊ฐ’ ์ถœ๋ ฅ set -v ํ•˜๋‘ก, ํ•˜์ด๋ธŒ์˜ ์„ค์ •๊ฐ’ ์ถœ๋ ฅ add file <> ํŒŒ์ผ ์ถ”๊ฐ€ add files <> ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ ์ถ”๊ฐ€, ๊ณต๋ฐฑ์œผ๋กœ ๊ฒฝ๋กœ ๊ตฌ๋ถ„ add jar <> jar ํŒŒ์ผ ์ถ”๊ฐ€ add jars <> ์—ฌ๋Ÿฌ ๊ฐœ์˜ jar ํŒŒ์ผ ์ถ”๊ฐ€, ๊ณต๋ฐฑ์œผ๋กœ ๊ฒฝ๋กœ ๊ตฌ๋ถ„ dfs <dfs command> ํ•˜๋‘ก dfs ์ปค๋งจ๋“œ ์‹คํ–‰ 0: jdbc:hive2://localhost:10000> set mapred.reduce.tasks=32; No rows affected (0.044 seconds) 0: jdbc:hive2://localhost:10000> reset; No rows affected (0.134 seconds) 0: jdbc:hive2://localhost:10000> dfs -ls /user/; +------------------------------------------------------------------------------+--+ | DFS Output | +------------------------------------------------------------------------------+--+ | drwxrwxrwx - hadoop hadoop 0 2018-12-04 07:14 /user/hadoop | +------------------------------------------------------------------------------+--+ ์ถœ๋ ฅ ํฌ๋งท ๋น„ ๋ผ์ธ์€ ์ถœ๋ ฅ ํฌ๋งท์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. XML, CSV, TSV ํ˜•ํƒœ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ์ž‘์—…์„ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— ํŽธ๋ฆฌํ•˜๊ฒŒ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # --outputformat=[table/vertical/csv/tsv/dsv/csv2/tsv2] $ beeline --outputformat=csv -f sample.hql SQLLine๋Š” ์ž๋ฐ”๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” DB ์ ‘์† ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๊ฐ€๊ธฐ โ†ฉ ํ•˜์ด๋ธŒ ๋น„ ๋ผ์ธ ์‹คํ–‰ ๋งค๋‰ด์–ผ โ†ฉ 5-๋ฉ”ํƒ€ ์Šคํ† ์–ด ํ•˜์ด๋ธŒ์˜ ๋ฉ”ํƒ€์ •๋ณด๋Š” ํŒŒ์ผ์˜ ๋ฌผ๋ฆฌ์ ์ธ ์œ„์น˜์™€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋…ผ๋ฆฌ์ ์ธ ์ •๋ณด๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”ํƒ€์ •๋ณด๋ฅผ ๋ณด๊ด€ํ•˜๊ณ  ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์— ๋”ฐ๋ผ ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ณณ์ด ํ•˜์ด๋ธŒ ๋ฉ”ํƒ€ ์Šคํ† ์–ด์ž…๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ์Šคํ† ์–ด๋Š” ์“ฐ ๋ฆฌํ”„ํŠธ 1 ํ”„๋กœํ† ์ฝœ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ์„œ๋น„์Šค์— ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ์ •๋ณด๋Š” JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์ด์šฉํ•˜์—ฌ RDBMS์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ์Šคํ† ์–ด ํƒ€์ž… ๋ฉ”ํƒ€ ์Šคํ† ์–ด๋Š” ์‹คํ–‰ ์œ ํ˜•์— ๋”ฐ๋ผ 3๊ฐ€์ง€ ํƒ€์ž…์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ํƒ€์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ์Šคํ† ์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํ•˜์ด๋ธŒ์—์„œ ํ•„์š”ํ•œ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ”์„ ํ™•์ธํ•˜๊ณ  ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ์ด๋””๋“œ ๋ฉ”ํƒ€ ์Šคํ† ์–ด ๊ธฐ๋ณธ ์„ค์ •์˜ ํ•˜์ด๋ธŒ๋Š” ๋”๋น„ DB๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ์ด๋””๋“œ ๋ฉ”ํƒ€ ์Šคํ† ์–ด๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๋ช…์˜ ์œ ์ €๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฃผ๋กœ ํ…Œ์ŠคํŠธ ๋ชฉ์ ์œผ๋กœ๋งŒ ์ด์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. hive-site.xml ์„ค์ • <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:derby:metastore_db;create=true </value> <description>JDBC connect string for a JDBC metastore </description> </property> ๋กœ์ปฌ ๋ฉ”ํƒ€ ์Šคํ† ์–ด ๋กœ์ปฌ ๋ฉ”ํƒ€ ์Šคํ† ์–ด๋Š” ํ•˜์ด๋ธŒ์™€ ๊ฐ™์€ JVM์—์„œ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋Š” ์™ธ๋ถ€์˜ RDBMS์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ž๊ฐ€ ๋™์‹œ์— ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. hive-site.xml ์„ค์ • <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://[IP]:[port]/[๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„]</value> <description>user name to use against metastore database</description> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>org.mariadb.jdbc.Driver</value> <description>user name to use against metastore database</description> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>[์‚ฌ์šฉ์ž๋ช…]</value> <description>user name to use against metastore database</description> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>[์•”ํ˜ธ]</value> <description>password to use against metastore database</description> </property> ์›๊ฒฉ ๋ฉ”ํƒ€ ์Šคํ† ์–ด ์›๊ฒฉ ๋ฉ”ํƒ€ ์Šคํ† ์–ด๋Š” ๋ฉ”ํƒ€ ์Šคํ† ์–ด๊ฐ€ ๋ณ„๋„์˜ JVM์—์„œ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์“ฐ ๋ฆฌํ”„ํŠธ ํ”„๋กœํ† ์ฝœ์„ ์ด์šฉํ•˜์—ฌ ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค. hive-site.xml ์„ค์ • <property> <name>hive.metastore.uris</name> <value>thrift://[๋ฉ”ํƒ€ ์Šคํ† ์–ด IP]:[๋ฉ”ํƒ€ ์Šคํ† ์–ด Port]</value> <description>JDBC connect string for a JDBC metastore</description> </property> ์ด๊ธฐ์ข… ๊ฐ„ ํ†ต์‹ ์„ ์œ„ํ•œ ํ”„๋กœํ† ์ฝœ. Apache Thrift (๋ฐ”๋กœ ๊ฐ€๊ธฐ) โ†ฉ 1-๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ•˜์ด๋ธŒ์˜ ํ…Œ์ด๋ธ”๋“ค์— ๋Œ€ํ•œ ๋…ผ๋ฆฌ์ ์ธ ์ •๋ณด๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ์Šคํ† ์–ด ์„œ๋น„์Šค๋Š” ์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด๋ฅผ ์—ฌ๋Ÿฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๋”๋น„, MSSQL, MySQL, ์˜ค๋ผํด ๋“ฑ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํ•˜์ด๋ธŒ ์„œ๋น„์Šค์— ํ•„์š”ํ•œ ์Šคํ‚ค๋งˆ 1์„ ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•˜๊ณ  ์„œ๋น„์Šคํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ํ™•์ธํ•˜๋Š” ์ •๋ณด๋Š” ์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ •๋ณด์— ๊ธฐ๋ฐ˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•˜์ด๋ธŒ ํ…Œ์ด๋ธ” ๊ด€๋ จํ•˜์—ฌ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ง์ ‘ ์ ‘์†ํ•˜์—ฌ ํ™•์ธํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ํ…Œ์ด๋ธ” ํ•˜์ด๋ธŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ฃผ์š” ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”๋ช… ์„ค๋ช… DBS ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด TBLS ํ…Œ์ด๋ธ” ์ •๋ณด PARTITIONS ํŒŒํ‹ฐ์…˜ ์ •๋ณด ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ ํ™•์ธ SQL ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ ํ™•์ธ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -- DB ์ •๋ณด SELECT * FROM DBS; -- ํ…Œ์ด๋ธ” ์ •๋ณด SELECT * FROM TBLS; -- DB, ํ…Œ์ด๋ธ” ์กฐ์ธ SELECT * FROM TBLS t, DBS d WHERE t.DB_ID = d.DB_ID ORDER BY d.NAME; -- ํ…Œ์ด๋ธ” ์ด๋ฆ„์— sample ์ด ๋“ค์–ด๊ฐ€๋Š” ํ…Œ์ด๋ธ”์„ ์ฐพ์•„์„œ, ๋”” ๋น„๋ช…, ํ…Œ์ด๋ธ”๋ช…, ํŒŒํ‹ฐ์…˜๋ช…์„ ์ถœ๋ ฅ SELECT d.NAME, t.TBL_NAME, p.PART_NAME FROM DBS d, TBLS t, PARTITIONS p WHERE d.DB_ID = t.DB_ID AND t.TBL_ID = p.TBL_ID AND t.TBL_NAME like '%sample%'; ์ „์ฒด ํ…Œ์ด๋ธ” ํ•˜์ด๋ธŒ ๋ฉ”ํƒ€ ์Šคํ† ์–ด์˜ ์ „์ฒด ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. +---------------------------+ | Tables_in_hive | +---------------------------+ | AUX_TABLE | | BUCKETING_COLS | | CDS | | COLUMNS_V2 | | COMPACTION_QUEUE | | COMPLETED_COMPACTIONS | | COMPLETED_TXN_COMPONENTS | | DATABASE_PARAMS | | DBS | | DB_PRIVS | | DELEGATION_TOKENS | | FUNCS | | FUNC_RU | | GLOBAL_PRIVS | | HIVE_LOCKS | | IDXS | | INDEX_PARAMS | | KEY_CONSTRAINTS | | MASTER_KEYS | | NEXT_COMPACTION_QUEUE_ID | | NEXT_LOCK_ID | | NEXT_TXN_ID | | NOTIFICATION_LOG | | NOTIFICATION_SEQUENCE | | NUCLEUS_TABLES | | PARTITIONS | | PARTITION_EVENTS | | PARTITION_KEYS | | PARTITION_KEY_VALS | | PARTITION_PARAMS | | PART_COL_PRIVS | | PART_COL_STATS | | PART_PRIVS | | ROLES | | ROLE_MAP | | SDS | | SD_PARAMS | | SEQUENCE_TABLE | | SERDES | | SERDE_PARAMS | | SKEWED_COL_NAMES | | SKEWED_COL_VALUE_LOC_MAP | | SKEWED_STRING_LIST | | SKEWED_STRING_LIST_VALUES | | SKEWED_VALUES | | SORT_COLS | | TABLE_PARAMS | | TAB_COL_STATS | | TBLS | | TBL_COL_PRIVS | | TBL_PRIVS | | TXNS | | TXN_COMPONENTS | | TYPES | | TYPE_FIELDS | | VERSION | | WRITE_SET | +---------------------------+ MySQL ์ƒ์„ฑ ์ฟผ๋ฆฌ: GitHub ๋ฐ”๋กœ ๊ฐ€๊ธฐ โ†ฉ 2-๋ฉ”ํƒ€ ์Šคํ† ์–ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ • ์›๊ฒฉ ํ•˜์ด๋ธŒ ๋ฉ”ํƒ€ ์Šคํ† ์–ด๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” MySQL, ์˜ค๋ผํด ๊ฐ™์€ RDB์— ํ•˜์ด๋ธŒ ๋ฉ”ํƒ€ ์Šคํ† ์–ด ์Šคํ‚ค๋งˆ๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. hive-site.xml์— RDB์— ์ ‘์†์„ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ์ปค๋งจ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. hive-site.xml ์„ค์ • ํ•˜์ด๋ธŒ์˜ conf ์•„๋ž˜ hive-site.xml์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ ‘์†์„ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. <?xml version="1.0"?> <configuration> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://database_ip:database_port/database_name</value> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>org.mariadb.jdbc.Driver</value> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>user_name</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>password</value> </property> </configuration> ์Šคํ‚ค๋งˆ ์ƒ์„ฑ ์Šคํ‚ค๋งˆ ์ƒ์„ฑ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. dbType์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. mssql, mysql, oracle, postgres๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. $ hive --service schemaTool -dbType mysql -initSchema ์Šคํ‚ค๋งˆ ์—…๊ทธ๋ ˆ์ด๋“œ ํ•˜์ด๋ธŒ ๋ฒ„์ „์„ ๋ฐ”๊พธ๊ฒŒ ๋˜๋ฉด ํ•˜์œ„ ๋ฒ„์ „์˜ ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์œ„ ๋ฒ„์ „์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. $ hive --service schemaTool -dbType mysql -upgradeSchema ์Šคํ‚ค๋งˆ ์ •๋ณด ํ™•์ธ ํ•˜์ด๋ธŒ ์Šคํ‚ค๋งˆ์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. $ hive --service schemaTool -dbType mysql -info Metastore connection URL: jdbc:mysql://database_ip:database_port/database_name Metastore Connection Driver : org.mariadb.jdbc.Driver Metastore connection User: user_name Hive distribution version: 2.3.0 Metastore schema version: 2.3.0 schemaTool completed 2-๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ•˜์ด๋ธŒ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„์„ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ์˜ ๊ธฐ๋ณธ ์ €์žฅ ์œ„์น˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋กœ์ผ€์ด์…˜์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์„ค์ •๊ฐ’ 1์„ ๊ธฐ๋ณธ ์œ„์น˜๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋กœ์ผ€์ด์…˜์€ ํ…Œ์ด๋ธ”์ด ๋กœ์ผ€์ด์…˜์„ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ๊ธฐ๋ณธ ์ €์žฅ ์œ„์น˜ 2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. # ๊ธฐ๋ณธ ์œ„์น˜ hive.metastore.warehouse.dir = hdfs:///user/hive/ # ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ธฐ๋ณธ ์œ„์น˜ hdfs:///user/hive/{๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ช…}.db # ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ์œ„์น˜ hdfs:///user/hive/{๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ช…}.db/{ํ…Œ์ด๋ธ”๋ช…} ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ƒ์„ฑ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ƒ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ DDL ๋งค๋‰ด์–ผ์— ๋”ฐ๋ฅด๋ฉด DATABASE์™€ SCHEMA๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. 3 ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ €์žฅ ๋กœ์ผ€์ด์…˜๊ณผ ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. IF NOT EXISTS ๊ตฌ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๊ฐ€ ์žˆ์œผ๋ฉด ์ƒ์„ฑํ•˜์ง€ ์•Š๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_path] [WITH DBPROPERTIES (property_name=property_value, ...)]; ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ˆ˜์ • ALTER๋ฅผ ์ด์šฉํ•˜์—ฌ ํ”„๋กœํผํ‹ฐ, ์œ ์ €, ๋กœ์ผ€์ด์…˜์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ์ผ€์ด์…˜์˜ ๋ณ€๊ฒฝํ•ด๋„ ํ•˜์œ„ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ด๋™๋˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”, ํŒŒํ‹ฐ์…˜์˜ ๋กœ์ผ€์ด์…˜์—๋Š” ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ธฐ๋ณธ ๋กœ์ผ€์ด์…˜๋งŒ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, ...); -- (Note: SCHEMA added in Hive 0.14.0) ALTER (DATABASE|SCHEMA) database_name SET OWNER [USER|ROLE] user_or_role; -- (Note: Hive 0.13.0 and later; SCHEMA added in Hive 0.14.0) ALTER (DATABASE|SCHEMA) database_name SET LOCATION hdfs_path; -- (Note: Hive 2.2.1, 2.4.0 and later) ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์‚ญ์ œ DROP ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ RESTRICT ์˜ต์…˜์„ ์ด์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. IF EXISTS ๊ตฌ๋ฌธ์„ ์ด์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๊ฐ€ ์กด์žฌํ•  ๋•Œ๋งŒ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. RESTRICT: ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์— ํ…Œ์ด๋ธ”์ด ์žˆ์œผ๋ฉด ์‚ญ์ œ ๋ถˆ๊ฐ€ CASCADE: ํ…Œ์ด๋ธ”์ด ์žˆ์–ด๋„ ์‚ญ์ œ ๊ฐ€๋Šฅ DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE]; ์˜ˆ์ œ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ƒ์„ฑ CREATE DATABASE IF NOT EXISTS sample_database COMMENT "test database" LOCATION "/user/shs/sample_database/" WITH DBPROPERTIES ( 'key1' = 'value1', 'key2' = 'value2' ); -- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด ํ™•์ธ hive> DESC DATABASE EXTENDED sample_database; OK sample_database test database hdfs:///user/shs/sample_database hadoop USER {key1=value1, key2=value2} -- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด ์ˆ˜์ • ALTER DATABASE sample_database SET DBPROPERTIES ("key1"="value4"); hive> DESC DATABASE EXTENDED sample_database; OK sample_database test database hdfs:///user/shs/sample_database hadoop USER {key1=value4, key2=value2} -- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋“œ๋กญ -- ํ•˜์œ„์— ํ…Œ์ด๋ธ”์ด ์กด์žฌํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ hive> DROP DATABASE sample_database; FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database sample_database is not empty. One or more tables exist.) -- CASCADE ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ์‚ญ์ œ hive> DROP DATABASE sample_database CASCADE; OK Time taken: 0.227 seconds ์ฐธ๊ณ  ํ•˜์ด๋ธŒ DDL - ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค hive.metastore.warehouse.dir โ†ฉ {๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋กœ์ผ€์ด์…˜}/{ํ…Œ์ด๋ธ”๋ช…} โ†ฉ ํ•˜์ด๋ธŒ DDL์˜ ์ •์˜ - The uses of SCHEMA and DATABASE are interchangeable โ€“ they mean the same thing. ๋ฐ”๋กœ ๊ฐ€๊ธฐ โ†ฉ 3-ํ…Œ์ด๋ธ” ํ•˜์ด๋ธŒ์—์„œ ํ…Œ์ด๋ธ”์€ HDFS ์ƒ์— ์ €์žฅ๋œ ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ์— ๋Œ€ํ•œ ๋ฉ”ํƒ€ ์ •๋ณด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ์ €์žฅ๋œ ํŒŒ์ผ์˜ ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์ •๋ณด์™€ ์ €์žฅ ์œ„์น˜, ์ž…๋ ฅ ํฌ๋งท, ์ถœ๋ ฅ ํฌ๋งท, ํŒŒํ‹ฐ์…˜ ์ •๋ณด, ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•œ ์ •๋ณด ๋“ฑ ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์ €์žฅ ์œ„์น˜ LOCATION ํ…Œ์ด๋ธ” ํƒ€์ž… MANAGED EXTERNAL TEMPORARY ํŒŒํ‹ฐ์…˜ PARTITIONED BY ๋ฒ„์ผ“ํŒ…, ์Šคํ CLUSTERED BY SORTED BY INTO BUCKETS SKEWED BY ํ…Œ์ด๋ธ” ํฌ๋งท(ROW FORMAT) DELIMITED ์„œ๋ฐ(SerDe) ์ €์žฅ ํฌ๋งท(STORED AS) ํ…Œ์ด๋ธ” ์ˆ˜์ • ์นผ๋Ÿผ ์ˆ˜์ • - CHANGE ์นผ๋Ÿผ ์ˆ˜์ • - REPLACE ์ฐธ๊ณ  ํ…Œ์ด๋ธ” ์ƒ์„ฑ ํ…Œ์ด๋ธ” ์ƒ์„ฑ์€ CREATE ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ƒ์„ธํ•œ ์˜ต์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์ฟผ๋ฆฌ CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.] table_name -- (Note: TEMPORARY available in Hive 0.14.0 and later) [(col_name data_type [COMMENT col_comment], ... [constraint_specification])] [COMMENT table_comment] [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS] [SKEWED BY (col_name, col_name, ...) -- (Note: Available in Hive 0.10.0 and later)] ON ((col_value, col_value, ...), (col_value, col_value, ...), ...) [STORED AS DIRECTORIES] [ [ROW FORMAT row_format] [STORED AS file_format] | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)] -- (Note: Available in Hive 0.6.0 and later) ] [LOCATION hdfs_path] [TBLPROPERTIES (property_name=property_value, ...)] -- (Note: Available in Hive 0.6.0 and later) [AS select_statement]; -- (Note: Available in Hive 0.5.0 and later; not supported for external tables) -- LIKE๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ธฐ ์กด์žฌํ•˜๋Š” ํ…Œ์ด๋ธ”๊ณผ ๋™์ผํ•˜๊ฒŒ ํ…Œ์ด๋ธ” ์ƒ์„ฑ CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.] table_name LIKE existing_table_or_view_name [LOCATION hdfs_path]; -- ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ์ค€ ํฌ๋งท row_format : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char] [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] [NULL DEFINED AS char] -- (Note: Available in Hive 0.13 and later) | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)] -- ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ํŒŒ์ผ์˜ ํƒ€์ž… file_format: : SEQUENCEFILE | TEXTFILE -- (Default, depending on hive.default.fileformat configuration) | RCFILE -- (Note: Available in Hive 0.6.0 and later) | ORC -- (Note: Available in Hive 0.11.0 and later) | PARQUET -- (Note: Available in Hive 0.13.0 and later) | AVRO -- (Note: Available in Hive 0.14.0 and later) | INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname -- ํ…Œ์ด๋ธ”์˜ ์ œ์•ฝ์กฐ๊ฑด constraint_specification: : [, PRIMARY KEY (col_name, ...) DISABLE NOVALIDATE ] [, CONSTRAINT constraint_name FOREIGN KEY (col_name, ...) REFERENCES table_name(col_name, ...) DISABLE NOVALIDATE ์ €์žฅ ์œ„์น˜ LOCATION ํ…Œ์ด๋ธ”์˜ ์ €์žฅ ์œ„์น˜๋Š” ํ…Œ์ด๋ธ”์— ์“ฐ๋Š” ๋ฐ์ดํ„ฐ์˜ ์ €์žฅ ์œ„์น˜์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ €์žฅ ์œ„์น˜ ์•„๋ž˜ ํ…Œ์ด๋ธ” ์ด๋ฆ„์˜ ํด๋”๋กœ ๊ธฐ๋ณธ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” ํƒ€์ž… MANAGED ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์‹œ ์˜ต์…˜์„ ๋”ฐ๋กœ ์ฃผ์ง€ ์•Š์œผ๋ฉด ๋งค๋‹ˆ์ง€๋“œ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์„ธ์…˜์ด ์ข…๋ฃŒ๋˜์–ด๋„ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ์™€ ํŒŒ์ผ์€ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์„ DROP ํ•˜๋ฉด ํŒŒ์ผ๋„ ํ•จ๊ป˜ ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค. EXTERNAL EXTERNAL ์˜ต์…˜์€ ๋งค๋‹ˆ์ง€๋“œ ํ…Œ์ด๋ธ”๊ณผ ํŒŒ์ผ ์‚ญ์ œ ์ •์ฑ…์„ ์ œ์™ธํ•˜๊ณ  ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ต์Šคํ„ฐ๋„ ํ…Œ์ด๋ธ”์€ DROP ํ•˜๋ฉด ํŒŒ์ผ์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์‹ค์ˆ˜๋กœ ์ธํ•œ ํŒŒ์ผ ์‚ญ์ œ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ EXTERNAL ํ…Œ์ด๋ธ”๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. TEMPORARY TEMPORARY ์˜ต์…˜์€ ํ˜„์žฌ ์„ธ์…˜์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์„ธ์…˜์ด ์ข…๋ฃŒ๋˜๋ฉด ์ œ๊ฑฐ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ž„์‹œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ์— ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜ PARTITIONED BY ํŒŒํ‹ฐ์…˜์€ ํด๋” ๊ตฌ์กฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„ํ• ํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. PARTITIOND BY์— ์ง€์ •ํ•œ ์นผ๋Ÿผ์˜ ์ •๋ตค๋ฅผ ์ด์šฉํ•˜์—ฌ ํด๋” ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜ ์ƒ์„ฑ ์‹œ ์ •๋ณด์˜ ์ œ๊ณต ์œ ๋ฌด์— ๋”ฐ๋ผ ๋‹ค์ด๋‚ด๋ฏน ํŒŒํ‹ฐ์…˜๊ณผ ์Šคํƒœํ‹ฑ ํŒŒํ‹ฐ์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ๋Š” ํด๋” ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ธฐ ๋•Œ๋ฌธ์— ํŒŒํ‹ฐ์…˜์ด ์—†๋‹ค๋ฉด ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐ„์ด ๊ฐˆ์ˆ˜๋ก ๋ฐ์ดํ„ฐ๊ฐ€ ์Œ“์ด๊ฒŒ ๋˜๋ฉด ์ ์  ์กฐํšŒ ์‹œ๊ฐ„์ด ๊ธธ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์ผ์ž๋‚˜ ํŠน์ • ์กฐ๊ฑด์„ ์ด์šฉํ•˜์—ฌ ํŒŒํ‹ฐ์…˜์„ ์ง€์ •ํ•˜๊ณ , ์กฐํšŒ ์‹œ์— ํŒŒํ‹ฐ์…˜์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋ฉด ์กฐํšŒ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -- ์ผ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํŒŒํ‹ฐ์…˜ ์ƒ์„ฑ CREATE TABLE tbl( col1 STRING ) PARTITIONED BY (yymmdd STRING); -- ๋ฐ์ดํ„ฐ ์ €์žฅ ๊ตฌ์กฐ hdfs://tbl/yymmddval=20180501/0000_0 hdfs://tbl/yymmddval=20180502/0000_0 hdfs://tbl/yymmddval=20180503/0000_0 -- ์กฐํšŒ SELECT yymmdd, count(1) FROM tbl WHERE yymmdd between '20180501' and '20180503' GROUP BY yymmdd ๋ฒ„์ผ“ํŒ…, ์Šคํ CLUSTERED BY SORTED BY INTO BUCKETS ๋ฒ„์ผ“ํŒ…์€ CLUSTERED BY๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ SORTED BY์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์„ค์ •ํ•œ ๋ฒ„ํ‚ท์˜ ๊ฐœ์ˆ˜(ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜)์— ์ง€์ •ํ•œ ์นผ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด์‹œ ์ฒ˜๋ฆฌํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„์ผ“ํŒ…ํ•œ ํ…Œ์ด๋ธ”์€ ์กฐ์ธ ์‹œ์— SMB ์กฐ์ธ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด ์กฐ์ธ ์‹œ์— ์†๋„๊ฐ€ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. -- col2๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฒ„์ผ“ํŒ… ํ•˜์—ฌ 20๊ฐœ์˜ ํŒŒ์ผ์— ์ €์žฅ CREATE TABLE tbl( col1 STRING, col2 STRING ) CLUSTERED BY col2 SORTED BY col2 INTO 20 BUCKETS SKEWED BY ์Šคํ๋Š” ๊ฐ’์„ ๋ถ„๋ฆฌ๋œ ํŒŒ์ผ์— ์ €์žฅํ•˜์—ฌ ํŠน์ •ํ•œ ๊ฐ’์ด ์ž์ฃผ ๋“ฑ์žฅํ•  ๋•Œ ์†๋„๋ฅผ ๋†’์ด๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. -- col1์˜ col_value ๊ฐ’์„ ์Šคํ๋กœ ์ €์žฅ CREATE TABLE tbl ( col1 STRING, col2 STRING ) SKEWED BY (col1) on ('col_value'); ํ…Œ์ด๋ธ” ํฌ๋งท(ROW FORMAT) ํ…Œ์ด๋ธ” ํฌ๋งท(ROW FORMAT)์€ ๋ฐ์ดํ„ฐ๋ฅผ ์นผ๋Ÿผ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ตฌ๋ถ„์ž(delimeter)์™€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด์„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ง€์ •ํ•˜๋Š” ์„œ๋ฐ(SerDe)๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ ๊ตฌ๋ถ„์ž์™€ ์„œ๋ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. DELIMITED ํ•˜์ด๋ธŒ๋Š” ๊ตฌ๋ถ„์ž์— ๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ์นผ๋Ÿผ ๋‹จ์œ„๋กœ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ์˜ ๊ตฌ๋ถ„์ž๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๊ตฌ๋ถ„์ž ์นผ๋Ÿผ ๊ตฌ๋ถ„์ž: \001, ์ปฌ๋ ‰์…˜ ์•„์ดํ…œ ๊ตฌ๋ถ„์ž: \002, ๋งต ์•„์ดํ…œ ๊ตฌ๋ถ„์ž: \003 -- ํ•˜์ด๋ธŒ์˜ ๊ธฐ๋ณธ ๊ตฌ๋ถ„์ž๋ฅผ ์ด์šฉํ•œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ -- ์ž…๋ ฅ ๋ฐ์ดํ„ฐ $ cat sample.txt a, val1^val2^val3, key1:val1^key2:val2 -- ROW FORMAT์„ ์ด์šฉํ•œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ CREATE TABLE tbl ( col1 STRING, col2 ARRAY<STRING>, col3 MAP<STRING, STRING> ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY '^' MAP KEYS TERMINATED BY ':'; -- ๋ฐ์ดํ„ฐ ๋กœ๋“œ LOAD DATA LOCAL INPATH './sample.txt' INTO TABLE tbl; -- ๋ฐ์ดํ„ฐ ์กฐํšŒ, ๊ตฌ๋ถ„์ž์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ตฌ๋ถ„๋จ hive> select * from tbl; OK a ["val1","val2","val3"] {"key1":"val1","key2":"val2"} -- ์ง€์ • ๊ฐ€๋Šฅํ•œ ๊ตฌ๋ถ„์ž FIELDS TERMINATED BY '\t' -- ์นผ๋Ÿผ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ์ค€ COLLECTION ITEMS TERMINATED BY ',' -- ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ์ค€ MAP KEYS TERMINATED BY '=' -- ๋งต ๋ฐ์ดํ„ฐ์˜ ํ‚ค์™€ ๋ฐธ๋ฅ˜๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ์ค€ LINES TERMINATED BY '\n' --๋กœ(row)๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ์ค€ ESCAPED BY '\\' -- ๊ฐ’์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์Œ NULL DEFINED AS 'null' -- null ๊ฐ’์„ ํ‘œํ˜„(0.13 ๋ฒ„์ „์—์„œ ์ถ”๊ฐ€) ์„œ๋ฐ(SerDe) ์„œ๋ฐ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด์„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ์—์„œ ์ œ๊ณตํ•˜๋Š” ์„œ๋ฐ๋Š” ๊ธฐ๋ณธ์„œ๋ฐ, ์ •๊ทœ์‹(RegExSerDe), JSON(JsonSerDe), CSV(OpenCSVSerde)๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐœ๋ฐœํ•˜์—ฌ ์ ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์„œ๋ฐ์˜ ์ƒ์„ธํ•œ ์‚ฌ์šฉ๋ฒ•์€ ํ•˜์ด๋ธŒ ์œ„ํ‚ค๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์„œ๋ฐ์˜ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- RegEx ์„œ ๋ฐ -- 127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 CREATE TABLE apachelog ( host STRING, identity STRING, user STRING, time STRING, request STRING, status STRING, size STRING, referer STRING, agent STRING) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe' WITH SERDEPROPERTIES ( "input.regex" = "([^]*) ([^]*) ([^]*) (-|\\[^\\]*\\]) ([^ \"]*|\"[^\"]*\") (-|[0-9]*) (-|[0-9]*)(?: ([^ \"]*|\".*\") ([^ \"]*|\".*\"))?" ); -- JSON ์„œ ๋ฐ CREATE TABLE my_table( a string, b bigint ) ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe' STORED AS TEXTFILE; -- CSV ์„œ ๋ฐ CREATE TABLE my_table( a string, b string ) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde' WITH SERDEPROPERTIES ( "separatorChar" = "\t", "quoteChar" = "'", "escapeChar" = "\\" ) STORED AS TEXTFILE; ์ €์žฅ ํฌ๋งท(STORED AS) STROED AS๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ํŒŒ์ผ ํฌ๋งท์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ €์žฅ ํฌ๋งท์€ TEXTFILE, SEQUENCEFILE, ORC, PARQUET ๋“ฑ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐœ๋ฐœํ•˜์—ฌ ์ ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ์„ธํ•œ ๋‚ด์šฉ์€ ํ•˜์ด๋ธŒ ์œ„ํ‚ค๋ฅผ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. -- ์ €์žฅ ํฌ๋งท์„ ORC๋กœ ์„ค์ •ํ•˜๊ณ , ORC ๊ด€๋ จ ์„ค์ • ์ •๋ณด ์ „๋‹ฌ CREATE TABLE tbl ( col1 STRING ) STORED AS ORC TBLPROPERTIES ("orc.compress"="SNAPPY"); -- INPUTFORMAT, OUTPUTFORMAT์„ ๋”ฐ๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ CREATE TABLE tbl1 ( col1 STRING ) STORED AS INPUTFORMAT "com.hadoop.mapred.DeprecatedLzoTextInputFormat" OUTPUTFORMAT "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat"; ํ…Œ์ด๋ธ” ์ˆ˜์ • ALTER ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ” ์นผ๋Ÿผ ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋Š” ๊ณต์‹ ๋ฌธ์„œ์— ๊ณต๊ฐœ๋œ ์˜ˆ์ œ์ธ๋ฐ ํ•˜์ด๋ธŒ ๋ฒ„์ „์— ๋”ฐ๋ผ์„œ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์˜ ์„œ๋ฐ์— ๋”ฐ๋ผ์„œ๋„ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ๋Š” EXTERNAL ํ…Œ์ด๋ธ”์ผ ๊ฒฝ์šฐ ํ…Œ์ด๋ธ”์„ ์‹ ๊ทœ๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์นผ๋Ÿผ ์ˆ˜์ • - CHANGE ๋‹ค์Œ์€ ์นผ๋Ÿผ์˜ ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•˜๋Š” CHAGE ๋ช…๋ น์–ด ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. -- ํ…Œ์ด๋ธ” ์ƒ์„ฑ CREATE TABLE test_change (a int, b int, c int); -- a ์นผ๋Ÿผ์„ a1์œผ๋กœ ์ด๋ฆ„ ์ˆ˜์ • ALTER TABLE test_change CHANGE a a1 INT; -- a1์„ a2๋กœ ๋ณ€๊ฒฝํ•˜๊ณ , b ์นผ๋Ÿผ ๋’ค๋กœ ์ด๋™ ALTER TABLE test_change CHANGE a1 a2 STRING AFTER b; -- ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๊ฐ€ ๋ณ€๊ฒฝ๋จ: b int, a2 string, c int. -- c๋ฅผ c1์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์ฒซ๋ฐด์งธ ์œ„์น˜๋กœ ์ด๋™ ALTER TABLE test_change CHANGE c c1 INT FIRST; -- ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๊ฐ€ ๋ณ€๊ฒฝ๋จ: c1 int, b int, a2 string. -- a2๋ฅผ a3๋กœ ๋ฐ”๊พธ๊ณ  b ์นผ๋Ÿผ ๋’ค๋กœ ์ด๋™ ALTER TABLE test_change CHANGE a2 a3 INT COMMENT 'this is column a3' AFTER b; ์นผ๋Ÿผ ์ˆ˜์ • - REPLACE ๋‹ค์Œ์€ ์นผ๋Ÿผ์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๋Š” REPLACE ๋ช…๋ น์–ด ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์ด ๋ช…๋ น์–ด๋Š” ๋ชจ๋“  ์นผ๋Ÿผ์„ ์‚ญ์ œ ํ›„, ์ƒˆ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. -- tbl ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ์„ ๋ชจ๋‘ ์‚ญ์ œ ํ›„ x, y, z ์นผ๋Ÿผ์œผ๋กœ ์ƒ์„ฑ ALTER TABLE tbl REPLACE COLUMNS (x INT COMMENT 'this is column x', y INT COMMENT 'this is column y', z INT COMMENT 'this is column z'); ์ฐธ๊ณ  LanguageManual DDL Alter Table/Partition/Column 01-๋ฐ์ดํ„ฐ ํƒ€์ž… ํ•˜์ด๋ธŒ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ๊ธฐ๋ณธ ์›์‹œ ํƒ€์ž…๊ณผ ๋ณตํ•ฉ ํƒ€์ž…์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ํ•˜์ด๋ธŒ ์œ„ํ‚ค๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. data_type : primitive_type | array_type | map_type | struct_type | union_type -- (Note: Available in Hive 0.7.0 and later) primitive_type : TINYINT | SMALLINT | INT | BIGINT | BOOLEAN | FLOAT | DOUBLE | DOUBLE PRECISION -- (Note: Available in Hive 2.2.0 and later) | STRING | BINARY -- (Note: Available in Hive 0.8.0 and later) | TIMESTAMP -- (Note: Available in Hive 0.8.0 and later) | DECIMAL -- (Note: Available in Hive 0.11.0 and later) | DECIMAL(precision, scale) -- (Note: Available in Hive 0.13.0 and later) | DATE -- (Note: Available in Hive 0.12.0 and later) | VARCHAR -- (Note: Available in Hive 0.12.0 and later) | CHAR -- (Note: Available in Hive 0.13.0 and later) array_type : ARRAY < data_type > map_type : MAP < primitive_type, data_type > struct_type : STRUCT < col_name : data_type [COMMENT col_comment], ...> union_type : UNIONTYPE < data_type, data_type, ... > -- (Note: Available in Hive 0.7.0 and later) CREATE TABLE tbl ( col1 INT, col2 STRING, col3 ARRAY<STRING>, col4 MAP<STRING, STRING>, col5 STRUCT<age:INT, name:STRING>, col6 UNIONTYPE<int, double, array<string>, struct<a:int, b:string>> ); ๊ธฐ๋ณธ ํƒ€์ž… ๊ธฐ๋ณธ ํƒ€์ž…์€ INT, FLOAT, STRING, DATE ๋“ฑ ์„ ์ง€์›ํ•œ๋‹ค. ๋ณตํ•ฉ ํƒ€์ž… ๋ณตํ•ฉ ํƒ€์ž…์€ array, map, struct, union์„ ์ง€์›ํ•œ๋‹ค. ARRAY ๋ฐฐ์—ด ํƒ€์ž…์ด๋‹ค. ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. SELECT col3[0] FROM tbl; MAP ์‚ฌ์ „ ํƒ€์ž…์ด๋‹ค. ํ‚ค๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. SELECT col4['key1'] FROM tbl; STRUCT ์ž๋ฐ”์˜ ํด๋ž˜์Šค์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์นผ๋Ÿผ์˜ ์ •๋ณด์— ํ•„๋“œ๋ช…์œผ๋กœ ์ ‘๊ทผํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. SELECT col5.age FROM tbl; SELECT col5.name FROM tbl; UNIONTYPE ์ง€์ •ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ค‘ ํ•˜๋‚˜๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ ์‹œ์— ๋ณด์ด๋Š” ์ •๋ณด๋Š” {๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ฒˆํ˜ธ:๋ฐ์ดํ„ฐ}์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. CREATE TABLE union_test( foo UNIONTYPE<int, double, array<string>, struct<a:int, b:string>> ); -- 0: int -- 1: double -- 2: array<string> -- 3: struct<a:int, b:string> SELECT foo FROM union_test; {0:1} {1:2.0} {2:["three","four"]} {3:{"a":5, "b":"five"}} {2:["six","seven"]} {3:{"a":8, "b":"eight"}} {0:9} {1:10.0} 02-์ž…๋ ฅ(Insert), ์กฐํšŒ(Select) ์ž…๋ ฅ๊ณผ ์กฐํšŒ๋Š” ํ•˜์ด๋ธŒ ํ…Œ์ด๋ธ”์˜ ๋ฉ”ํƒ€ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ , ์“ฐ๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์˜ ๋ฉ”ํƒ€ ์ •๋ณด์— ์‹ค์ œ ํŒŒ์ผ์˜ ๋กœ์ผ€์ด์…˜๊ณผ ๋ฐ์ดํ„ฐ์˜ ํฌ๋งท์ด ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์ฝ๊ณ , ์“ฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ(INSERT)์€ INSERT ๋ฌธ์œผ๋กœ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๋Š” ๋ฐฉ๋ฒ•๊ณผ ํ…Œ์ด๋ธ”์˜ ์ €์žฅ ์œ„์น˜, ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ๋•Œ ์ง€์ •ํ•œ LOCATION์— ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜ ํ…Œ์ด๋ธ”์€ ํŒŒํ‹ฐ์…˜์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง„ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์กฐํšŒ(SELECT)๋Š” ํ…Œ์ด๋ธ”์˜ LOCATION์˜ ์œ„์น˜์— ์žˆ๋Š” ํŒŒ์ผ์„ ์ฝ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ๋Š” ๋กœ์ผ€์ด์…˜ ์•„๋ž˜์˜ ๋ชจ๋“  ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋•Œ๋ฌธ์— ์šฉ๋Ÿ‰์ด ํฐ ํŒŒ์ผ์„ ์ €์žฅํ•œ๋‹ค๋ฉด ํŒŒํ‹ฐ์…˜์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์‚ฐํ•˜์—ฌ ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ๊ณผ ์กฐํšŒ ๋ฐฉ๋ฒ•์€ ํŒŒ์ผ์„ ์ฝ์–ด์„œ ํ…Œ์ด๋ธ”์— ์“ฐ๋Š” ๋ฐฉ๋ฒ•, ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ์“ฐ๋Š” ๋ฐฉ๋ฒ• ๋งˆ์ง€๋ง‰์œผ๋กœ ํ…Œ์ด๋ธ”์„ ์ฝ์–ด์„œ ์ง€์ •ํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์“ฐ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ to ํ…Œ์ด๋ธ” LOAD ๋ช…๋ น์œผ๋กœ ํ…Œ์ด๋ธ”์— ์“ฐ๊ธฐ ํ…Œ์ด๋ธ”์˜ LOCATION์— ํŒŒ์ผ์„ ๋ณต์‚ฌ ํ…Œ์ด๋ธ” ํŒŒํ‹ฐ์…˜์„ ์ถ”๊ฐ€/์ˆ˜์ •ํ•˜์—ฌ LOCATION์„ ํŒŒ์ผ ์œ„์น˜๋กœ ์ฃผ๋Š” ๋ฒ• MSCK ๋ฌธ์œผ๋กœ ํŒŒํ‹ฐ์…˜ ์—ฐ๊ฒฐ ํ…Œ์ด๋ธ” to ํ…Œ์ด๋ธ” INSERT ๋ฌธ FROM INSERT ๋ฌธ CREATE TABLE AS SELECT ๋ฌธ INSERT, UPDATE, DELETE, MERGE ํ…Œ์ด๋ธ” to ๋””๋ ‰ํ„ฐ๋ฆฌ INSERT DIRECTORY ๋ฌธ ํŒŒ์ผ to ํ…Œ์ด๋ธ” ํŒŒ์ผ์„ ์ฝ์–ด์„œ ํ…Œ์ด๋ธ”์˜ LOCATION์— ์“ฐ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. LOAD ๋ช…๋ น์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•, ํ…Œ์ด๋ธ”์˜ ๋กœ์ผ€์ด์…˜์— ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ• ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. LOAD ๋ช…๋ น์œผ๋กœ ํ…Œ์ด๋ธ”์— ์“ฐ๊ธฐ LOAD ๋ช…๋ น์œผ๋กœ ํŒŒ์ผ์„ ์ฝ์–ด์„œ ํ…Œ์ด๋ธ”์— ์“ฐ๋ฉด ํ…Œ์ด๋ธ”์˜ LOCATION์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. HDFS์™€ ๋กœ์ปฌ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ด๋ธ”์— ์“ธ ์ˆ˜ ์žˆ๊ณ , ํŒŒํ‹ฐ์…˜ ์ถ”๊ฐ€๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. LOAD ๋ช…๋ น์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. LOCAL์ด ์žˆ์œผ๋ฉด ๋กœ์ปฌ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ , ์—†์œผ๋ฉด HDFS์˜ ํŒŒ์ผ์„ ์ฝ์Šต๋‹ˆ๋‹ค. OVERWRITE๋กœ ํŒŒ์ผ์„ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)] LOAD ๋ช…๋ น์„ ์ด์šฉํ•œ ์˜ˆ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- hdfs์˜ ํŒŒ์ผ์„ ์ฝ์–ด์„œ tbl ํ…Œ์ด๋ธ”์— ์ž…๋ ฅ LOAD DATA INPATH 'hdfs://127.0.0.1/user/data/sample.csv' INTO TABLE tbl; -- hdfs์˜ ํŒŒ์ผ์„ ์ฝ์–ด์„œ tbl ํ…Œ์ด๋ธ”์˜ ํŒŒํ‹ฐ์…˜ yymmdd='20180510'์œผ๋กœ ์ž…๋ ฅ LOAD DATA INPATH '/user/data/sample.csv' INTO TABLE tbl PARTITION(yymmdd='20180510'); -- ๋กœ์ปฌ์˜ ํŒŒ์ผ์„ ์ฝ์–ด์„œ tbl ํ…Œ์ด๋ธ”์— ์ž…๋ ฅ LOAD DATA LOCAL INPATH './sample.csv' INTO TABLE tbl; -- test.txt ํŒŒ์ผ์„ sample1 ํ…Œ์ด๋ธ”์— ๋กœ๋“œํ•˜๊ณ  ๋ฐ์ดํ„ฐ ์กฐํšŒ LOAD DATA LOCAL INPATH './test.txt' INTO TABLE sample1; ํ…Œ์ด๋ธ”์˜ LOCATION์— ํŒŒ์ผ์„ ๋ณต์‚ฌ ํ…Œ์ด๋ธ”์˜ ๋ฉ”ํƒ€์ •๋ณด์—๋Š” ๋ฌผ๋ฆฌ์ ์ธ ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ์œ„ํ•œ LOCATION์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์„ ์กฐํšŒํ•  ๋•Œ ํ•ด๋‹น ์œ„์น˜์˜ ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋•Œ๋ฌธ์— ์ด ์œ„์น˜์— ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋กœ์ปฌ ์œ„์น˜๋Š” ์ง€์ •ํ•  ์ˆ˜๋Š” ์—†๊ณ , HDFS, S3 ๋“ฑ ํ•˜๋‘ก์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ ๊ณต์œ  ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์˜ LOCATION ์ •๋ณด๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ CREATE ์‹œ์— ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ , ALTER ๋ฌธ์„ ์ด์šฉํ•ด์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. -- ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋ฉด์„œ LOCATION์„ ์ง€์ • CREATE TABLE employee ( id String, name String ) LOCATION 'hdfs://127.0.0.1/user/data/'; -- ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ณ , ๋‚˜์ค‘์— ํ…Œ์ด๋ธ”์˜ ์œ„์น˜๋ฅผ ALTER ๋ช…๋ น์œผ๋กœ ์ง€์ • CREATE TABLE employee ( id String, name String); -- ALTER ๋ช…๋ น์œผ๋กœ ํ…Œ์ด๋ธ”์˜ ๋กœ์ผ€์ด์…˜ ๋ณ€๊ฒฝ ALTER TABLE employee SET LOCATION 'hdfs://127.0.0.1/user/data/'; ๋‹ค์Œ์€ sample1 ํ…Œ์ด๋ธ”์„ LOCATION์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ์ƒ์„ฑํ•œ ํ›„ ALTER ๋ฌธ์œผ๋กœ ๋กœ์ผ€์ด์…˜์„ ์ง€์ •ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. -- ํ…Œ์ด๋ธ” ์ƒ์„ฑ ํ›„ ์กฐํšŒ ์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Œ hive> create table sample1(col1 string); hive> select * from sample1; OK Time taken: 0.8 seconds -- HDFS์— ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌ $ hadopo fs -ls hdfs://0.0.0.0:8020/user/data/ 2018-05-21 07:38:47 14 test.txt -- ๋กœ์ผ€์ด์…˜์„ ์„ค์ • ํ›„ ์กฐํšŒ ์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ™•์ธ๋จ hive> alter table sample 1set location 'hdfs://0.0.0.0:8020/user/data/'; hive> select * from sample1; OK b d g Time taken: 0.055 seconds, Fetched: 7 row(s) LOCATION์„ ์ง€์ •ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์„œ๋ธŒ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ชจ๋‘๋ฅผ ์กฐํšŒํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์˜ต์…˜์„ ์„ค์ •ํ•˜์—ฌ ์กฐํšŒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. -- ํ•˜์œ„ ํด๋”๋ฅผ ๋ชจ๋‘ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ํ•˜๋Š” ์˜ต์…˜ set hive.supports.subdirectories=true; set mapred.input.dir.recursive=true; ํ…Œ์ด๋ธ” ํŒŒํ‹ฐ์…˜์„ ์ถ”๊ฐ€/์ˆ˜์ •ํ•˜์—ฌ LOCATION์„ ํŒŒ์ผ ์œ„์น˜๋กœ ์ฃผ๋Š” ๋ฒ• ํŒŒํ‹ฐ์…˜ ํ…Œ์ด๋ธ”์€ ํŒŒํ‹ฐ์…˜ ๋ณ„๋กœ LOCATION์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜์˜ LOCATION์— ํŒŒ์ผ์„ ์ด๋™์‹œํ‚ค๋ฉด ์กฐํšŒ ์‹œ์— ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์‹ ๊ทœ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•  ๋•Œ์™€ ๊ธฐ์กด ํŒŒํ‹ฐ์…˜์˜ LOCATON์„ ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- ์‹ ๊ทœ ํŒŒํ‹ฐ์…˜์„ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ LOCATION์„ ์ง€์ • ALTER TABLE employee ADD PARTITION (yymmdd='20180510') LOCATION 'hdfs://127.0.0.1/user/'; -- ๊ธฐ์กด ํŒŒํ‹ฐ์…˜์˜ LOCATION์„ ์ˆ˜์ • ALTER TABLE employee PARTITION (yymmdd='20180510') SET LOCATION 'hdfs://127.0.0.1/user/'; MSCK ๋ฌธ์œผ๋กœ ํŒŒํ‹ฐ์…˜ ์—ฐ๊ฒฐ MSCK ๋ฌธ์€ ํ…Œ์ด๋ธ”์„ ์‹ ๊ทœ ์ƒ์„ฑํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‹ ๊ทœ ํ…Œ์ด๋ธ” LOCATON ์ •๋ณด์— ๊ธฐ์กด ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ •๋ณด๋ฅผ ๋งค์นญ ์‹œํ‚ฌ ๋•Œ MSCK ๋ฌธ์„ ์ด์šฉํ•˜๋ฉด LOCATION ํ•˜์œ„์— ์กด์žฌํ•˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž๋™์œผ๋กœ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค. ์ฃผ๋กœ ํ…Œ์ด๋ธ” ์ •๋ณด๊ฐ€ ์‚ญ์ œ๋˜์–ด ํ…Œ์ด๋ธ”์„ ์‹ ๊ทœ๋กœ ์ƒ์„ฑํ•˜๊ณ  ํŒŒํ‹ฐ์…˜ ์ •๋ณด๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•  ๋•Œ๋‚˜, ์™ธ๋ถ€์—์„œ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ํŒŒํ‹ฐ์…˜ ์ •๋ณด๋ฅผ ์‹ ๊ทœ๋กœ ์ƒ์„ฑํ•˜์—ฌ์•ผ ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -- hdfs์— ์•„๋ž˜์™€ ๊ฐ™์ด ํด๋”๊ฐ€ ์กด์žฌ hdfs://127.0.0.1/user/employee/yymmdd=20180101/ hdfs://127.0.0.1/user/employee/yymmdd=20180101/ -- ํ…Œ์ด๋ธ” ์ƒ์„ฑ ํ›„ ๋กœ์ผ€์ด์…˜ ์ง€์ • -- ๋กœ์ผ€์ด์…˜์„ ํด๋”๊ฐ€ ์กด์žฌํ•˜๊ณ , ํŒŒํ‹ฐ์…˜์ด ์ƒ์„ฑ๋˜๋Š” ์œ„์น˜๋กœ ์ง€์ • CREATE TABLE employee ( name STRING, age STRING ) PARTITIONED BY (yymmdd STRING) LOCATION 'hdfs://127.0.0.1/user/employee'; -- ํŒŒํ‹ฐ์…˜ ์—ฐ๊ฒฐ MSCK REPAIR TABLE employee; ํ…Œ์ด๋ธ” to ํ…Œ์ด๋ธ” ํ…Œ์ด๋ธ”์˜ ์ •๋ณด๋ฅผ ์ฝ์–ด์„œ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. INSERT ๋ฌธ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ๋ฐฉ์‹์œผ๋กœ ํ…Œ์ด๋ธ”, ๋ทฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. INSERT ๋ฌธ์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...) [IF NOT EXISTS]] select_statement1 FROM from_statement; INSERT INTO TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 FROM from_statement; INSERT ๋ฌธ์€ ์ด์šฉํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์— ์ž…๋ ฅํ•  ์ˆ˜๋„ ์žˆ๊ณ , ํŒŒํ‹ฐ์…˜ ํ…Œ์ด๋ธ”์— ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. -- source์˜ ๋‚ด์šฉ์„ target ํ…Œ์ด๋ธ”์— ์ž…๋ ฅ INSERT INTO TABLE target SELECT * FROM source; -- OVERWRITE๊ฐ€ ๋ถ™์œผ๋ฉด ํ•ด๋‹น ์œ„์น˜์˜ ๋‚ด์šฉ์„ ์‚ญ์ œํ•˜๊ณ  ๋ฎ์–ด์“ด๋‹ค. INSERT OVERWRITE TABLE target PARTITION(col1 = 'a', col2) SELECT data1, date2 FROM source; FROM INSERT ๋ฌธ FROM INSERT ๋ฌธ์€ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์— ํ•œ ๋ฒˆ์— ์ž…๋ ฅํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. FROM ์ ˆ์— ์›์ฒœ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜์—ฌ ๋ทฐ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. FROM INSERT ๋ฌธ์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. FROM page_view_stg pvs INSERT OVERWRITE TABLE page_view PARTITION(dt='2008-06-08', country) SELECT pvs.ip, pvs.country; ๋‹ค์Œ์€ source1, source2 ํ…Œ์ด๋ธ”์„ ์ฝ์–ด์„œ target1, target2 ํ…Œ์ด๋ธ”์— ์ž…๋ ฅํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. FROM ( SELECT * FROM source1 UNION SELECT * FROM source2 ) R INSERT INTO TABLE target1 SELECT R.name, R.age INSERT OVERWRITE TABLE target2 PARTITION(col1 = 'a', col2) SELECT R.name, R.age; CREATE TABLE AS SELECT ๋ฌธ CTAS ๋ฌธ์€ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. SELECT๋กœ ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ๋ฅผ CREATE ๋ฌธ์œผ๋กœ ์ƒ์„ฑํ•œ ํ…Œ์ด๋ธ”์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. CREATE ์‹œ์— ๊ธฐ์กด์— ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•  ๋•Œ์™€ ๋™์ผํ•˜๊ฒŒ ๋ถ€๊ฐ€ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -- source_table์„ target_table๋กœ ์ƒ์„ฑ CREATE TABLE target_table AS SELECT * FROM source_table; -- CREATE ๋ฌธ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ƒ์„ฑํ•˜๊ณ  AS ๋ฌธ ๋‹ค์Œ์— SELECT ๋ฌธ ์ž…๋ ฅ CREATE TABLE new_key_value_store ROW FORMAT SERDE "org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe" STORED AS RCFile AS SELECT (key % 1024) new_key, concat(key, value) key_value_pair FROM key_value_store SORT BY new_key, key_value_pair; INSERT, UPDATE, DELETE, MERGE ํ•˜์ด๋ธŒ 0.14๋ถ€ํ„ฐ๋Š” INSERT, UPDATE, DELETE, MERGE ๋ฌธ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ๋ฒ„์ „์— ๋”ฐ๋ผ ์‹คํ–‰๋˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. UPDATE, DELETE๋Š” ํŠธ๋žœ์žญ์…˜ ์„ค์ •์„ ํ•˜์—ฌ์•ผ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CREATE TABLE students (name VARCHAR(64), age INT, gpa DECIMAL(3, 2)) CLUSTERED BY (age) INTO 2 BUCKETS STORED AS ORC; -- INSERT INSERT INTO TABLE students VALUES ('fred flintstone', 35, 1.28), ('barney rubble', 32, 2.32); -- UPDATE UPDATE students SET age = 10 WHERE name = 'fred flintstone'; -- DELETE DELETE FROM students WHERE name = 'fred flintstone'; -- MERGE MERGE INTO <target table> AS T USING <source expression/table> AS S ON <boolean expression1> WHEN MATCHED [AND <boolean expression2>] THEN UPDATE SET <set clause list> WHEN MATCHED [AND <boolean expression3>] THEN DELETE WHEN NOT MATCHED [AND <boolean expression4>] THEN INSERT VALUES<value list> ํ…Œ์ด๋ธ” to ๋””๋ ‰ํ„ฐ๋ฆฌ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜์—ฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ํŒŒ์ผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. INSERT DIRECTORY ๋ฌธ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ ์ง€์ •ํ•œ ์œ„์น˜์— ํŒŒ์ผ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ROW FORMAT์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. INSERT OVERWRITE [LOCAL] DIRECTORY directory1 [ROW FORMAT row_format] [STORED AS file_format] (Note: Only available starting with Hive 0.11.0) SELECT ... FROM ... Hive extension (multiple inserts): FROM from_statement INSERT OVERWRITE [LOCAL] DIRECTORY directory1 select_statement1 [INSERT OVERWRITE [LOCAL] DIRECTORY directory2 select_statement2] ... row_format : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char] [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] [NULL DEFINED AS char] (Note: Only available starting with Hive 0.13) ๋‹ค์Œ์€ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ํŒŒ์ผ์„ ์“ฐ๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. source ํ…Œ์ด๋ธ”์˜ ์ •๋ณด๋ฅผ ์ฝ์–ด์„œ ์ง€์ •ํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ROW FORMAT์„ ์ด์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ˜•ํ•˜์—ฌ ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. # /user/ ๋””๋ ‰ํ„ฐ๋ฆฌ์— source ํ…Œ์ด๋ธ”์„ ์ฝ์–ด์„œ ์ €์žฅ INSERT OVERWRITE DIRECTORY 'hdfs://1.0.0.1:8020/user/' SELECT * FROM source # /user/ ๋””๋ ‰ํ„ฐ๋ฆฌ์— source ํ…Œ์ด๋ธ”์„ ์ฝ์–ด์„œ ์นผ๋Ÿผ ๊ตฌ๋ถ„์„ ํƒญ์œผ๋กœ ์ €์žฅ INSERT OVERWRITE DIRECTORY 'hdfs://1.0.0.1:8020/user/' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' SELECT * FROM source # /user/ ๋””๋ ‰ํ„ฐ๋ฆฌ์— source ํ…Œ์ด๋ธ”์„ ์ฝ์–ด์„œ ์นผ๋Ÿผ ๊ตฌ๋ถ„์„ ์ฝค๋งˆ์œผ๋กœ ์ €์žฅํ•˜๋ฉด์„œ Gzip์œผ๋กœ ์••์ถ• # ํŒŒ์ผ์„ CSV ํ˜•ํƒœ๋กœ ์••์ถ•ํ•˜์—ฌ ์ €์žฅ set hive.exec.compress.output=true; set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; INSERT OVERWRITE DIRECTORY 'hdfs://1.0.0.1:8020/user/' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' SELECT * FROM source 1-๋ณตํ•ฉ ํƒ€์ž… ์ž…๋ ฅ STRUCT์™€ UNIONTYPE ๊ฐ™์€ ๋ณตํ•ฉ ํƒ€์ž…์˜ ์ž…๋ ฅ๊ณผ ์กฐํšŒ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. STRUCT ์ž๋ฐ”์˜ Value Object์™€ ์œ ์‚ฌ ์นผ๋Ÿผ๋ช…. ์†์„ฑ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผ UNIONTYPE ํ•˜๋‚˜์˜ ์นผ๋Ÿผ์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์†์„ฑ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ STRUCT ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๋ฐ ์กฐํšŒ -- ํ…Œ์ด๋ธ” ์ƒ์„ฑ CREATE TABLE struct_tbl ( struct_col STRUCT<age:INT, name:STRING> ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY ',' MAP KEYS TERMINATED BY '=' -- ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์˜ˆ์ œ. ์ฝค๋งˆ(,)๋กœ ๊ตฌ๋ถ„ $ cat sample.tsv 1, A 2, B -- ๋ฐ์ดํ„ฐ ์ ์šฉ LOAD DATA LOCAL INPATH './sample.tsv' INTO TABLE struct_tbl; -- ๋ฐ์ดํ„ฐ ์กฐํšŒ ๋ฐฉ๋ฒ• hive> SELECT struct_col.age FROM struct_tbl; 2 hive> SELECT struct_col.name FROM struct_tbl; B STRUCT ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ํ•˜์ด๋ธŒ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ํ•จ์ˆ˜๋กœ STRUCT๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. struct์™€ named_struct๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. -- struct๋Š” ์ด๋ฆ„ ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ hive> SELECT struct("a", "b"); OK {"col1":"a", "col2":"b"} -- named_struct๋Š” ์นผ๋Ÿผ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ hive> SELECT named_struct("a", 1, "b", 2); OK {"a":1, "b":2} STRUCT ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ๋ณตํ•ฉ ํƒ€์ž…์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์›ํ•˜๋Š” ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -- named_struct๋ฅผ ์ด์šฉํ•˜์—ฌ struct ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ struct_tbl์— ์ž…๋ ฅ FROM ( SELECT named_struct("age", 10, "name", "aa") AS col1 ) R INSERT INTO TABLE struct_tbl SELECT R.col1 -- ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ํ›„ ๊ฒฐ๊ณผ ์กฐํšŒ hive> SELECT * FROM struct_tbl; OK {"age":10, "name":"aa"} {"age":1, "name":"A"} {"age":2, "name":"B"} UNION ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๋ฐ ์กฐํšŒ -- ํ…Œ์ด๋ธ” ์ƒ์„ฑ CREATE TABLE union_tbl ( union_col UNIONTYPE<int, double, array<string>, struct<a:int, b:string>> ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY ',' MAP KEYS TERMINATED BY '=' -- ์ฝค๋งˆ ๊ตฌ๋ถ„์ž๋กœ ์ฒซ ๋ฒˆ์งธ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…, ๋‘ ๋ฒˆ์งธ๋Š” ๊ฐ’ $ cat union.tsv 0,1 1,2.0 2, a, b, c 3,1, A -- ๋ฐ์ดํ„ฐ ์ ์šฉ LOAD DATA LOCAL INPATH './union.tsv' INTO TABLE union_tbl; -- ๋ฐ์ดํ„ฐ ์กฐํšŒ SELECT union_col FROM union_tbl; {0:1} {1:2.0} {2:["a, b, c"]} UNION ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ํ•˜์ด๋ธŒ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ํ•จ์ˆ˜๋กœ UNION ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” create_unionํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. -- ์ฒซ ๋ฒˆ์งธ 0์ด ๋ฐ์ดํ„ฐ์˜ ์ธ๋ฑ์Šค hive> SELECT create_union(0, 60, "a"); {0:60} -- ์ฒซ ๋ฒˆ์งธ 1์ด ๋ฐ์ดํ„ฐ์˜ ์ธ๋ฑ์Šค hive> SELECT create_union(1, 60, "a"); {1:"a"} 03-ํŒŒํ‹ฐ์…˜ ํŒŒํ‹ฐ์…˜์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ๊ฐ™์€ ํŒŒ์ผ ๊ธฐ๋ฐ˜ ํ…Œ์ด๋ธ”์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  roww ์ •๋ณด๋ฅผ ์ฝ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์•„์ง€๋ฉด ์†๋„๊ฐ€ ๋Š๋ ค์ง‘๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜ ์นผ๋Ÿผ์€ where ์กฐ๊ฑด์—์„œ ์นผ๋Ÿผ์ฒ˜๋Ÿผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ์— ์ฝ์–ด ๋“ค์ด๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ค„์—ฌ์„œ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ํ–ฅ์ƒ์‹œ์ผœ ์ค๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์˜ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. CREATE TABLE tbl( col1 STRING ) PARTITIONED BY (yymmdd STRING); ํŒŒํ‹ฐ์…˜ ์ข…๋ฅ˜ ํŒŒํ‹ฐ์…˜์˜ ์ข…๋ฅ˜๋Š” ๋™์  ํŒŒํ‹ฐ์…˜(dynamic)๊ณผ ๊ณ ์ • ํŒŒํ‹ฐ์…˜(static)์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณ ์ • ํŒŒํ‹ฐ์…˜์€ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ์‹œ์ ์— ํŒŒํ‹ฐ์…˜ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž…๋ ฅ๋˜๋Š” ํŒŒํ‹ฐ์…˜์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™์  ํŒŒํ‹ฐ์…˜์€ ์นผ๋Ÿผ์˜ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ํŒŒํ‹ฐ์…˜์ด ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฟผ๋ฆฌ ์‹œ์ ์—๋Š” ํŒŒํ‹ฐ์…˜์„ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ ๊ณ ์ • ํŒŒํ‹ฐ์…˜, ๋™์  ํŒŒํ‹ฐ์…˜์„ ๋‹จ๋…์œผ๋กœ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๊ณ ์ •๊ณผ ๋™์  ํŒŒํ‹ฐ์…˜์„ ํ˜ผํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณ ์ • ํŒŒํ‹ฐ์…˜ ๊ณ ์ • ํŒŒํ‹ฐ์…˜์„ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์€ INSERT ๋ฌธ์— ํŒŒํ‹ฐ์…˜ ์ •๋ณด๋ฅผ ๊ณ ์ •๋œ ๊ฐ’์œผ๋กœ ์ „๋‹ฌํ•˜์—ฌ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ yymmdd ํŒŒํ‹ฐ์…˜์— '20180510' ๊ฐ’์„ ์ง์ ‘์ ์œผ๋กœ ์ „๋‹ฌํ•˜์—ฌ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. INSERT INTO TABLE tbl(yymmdd='20180510') SELECT name FROM temp; ์œ„์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํด๋” ๊ตฌ์กฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. hdfs://[tbl ํ…Œ์ด๋ธ” ๋กœ์ผ€์ด์…˜]/yymmdd=20180510/ ๋™์  ํŒŒํ‹ฐ์…˜ ๋™์  ํŒŒํ‹ฐ์…˜์„ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์€ INSERT ๋ฌธ์— ํŒŒํ‹ฐ์…˜ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋Š” ์นผ๋Ÿผ์„ ์ „๋‹ฌํ•˜์—ฌ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ yymmdd ํŒŒํ‹ฐ์…˜์— yymmdd ์นผ๋Ÿผ์„ ์ „๋‹ฌํ•˜์—ฌ ํŒŒํ‹ฐ์…˜์„ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. INSERT INTO TABLE tbl(yymmdd) SELECT name, yymmdd FROM temp; ์œ„์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ temp ํ…Œ์ด๋ธ”์˜ yymmdd ์นผ๋Ÿผ์— 20180510, 20180511 ๋‘ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. hdfs://[tbl ํ…Œ์ด๋ธ” ๋กœ์ผ€์ด์…˜]/yymmdd=20180510/ hdfs://[tbl ํ…Œ์ด๋ธ” ๋กœ์ผ€์ด์…˜]/yymmdd=20180511/ ํ•˜์ด๋ธŒ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋™์  ํŒŒํ‹ฐ์…˜๋งŒ ์ด์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ถŒ์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋™์  ํŒŒํ‹ฐ์…˜๋งŒ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” hive.exec.dynamic.partition.mode ์„ค์ •์„ nonstrict๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ๋™์  ํŒŒํ‹ฐ์…˜๋งŒ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •, ๊ธฐ๋ณธ๊ฐ’์€ strict๋กœ ๋™์  set hive.exec.dynamic.partition.mode=nonstrict; ๋™์  ํŒŒํ‹ฐ์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์†๋„๊ฐ€ ๋Š๋ ค์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋™์  ํŒŒํ‹ฐ์…˜์˜ ์ƒ์„ฑ ๊ฐœ์ˆ˜์— ์ œํžŒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์„ค์ •๋ณด๋‹ค ๋งŽ์€ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•  ๋•Œ๋Š” ๋‹ค์Œ์˜ ์„ค์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ๋™์  ํŒŒํ‹ฐ์…˜ ๊ฐœ์ˆ˜ set hive.exec.max.dynamic.partitions=1000; -- ๋…ธ๋“œ๋ณ„ ๋™์  ํŒŒํ‹ฐ์…˜ ์ƒ์„ฑ ๊ฐœ์ˆ˜ set hive.exec.max.dynamic.partitions.pernode=100; ๋™์  ํŒŒํ‹ฐ์…˜์— NULL ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ ํ•˜์ด๋ธŒ๋Š” NULL ๊ฐ’์„ ๊ธฐ๋ณธ ํŒŒํ‹ฐ์…˜ ๋ช…์„ ์ด์šฉํ•ด์„œ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. -- NULL ๊ฐ’์˜ ๊ธฐ๋ณธ ํŒŒํ‹ฐ์…˜ ๋ช… set hive.exec.default.partition.name=__HIVE_DEFAULT_PARTITION__; -- ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ํŒŒํ‹ฐ์…˜์ด ์ƒ์„ฑ hdfs://temp/yymmdd=20180510/hh=00/ hdfs://temp/yymmdd=20180510/hh=__HIVE_DEFAULT_PARTITION__/ -- ํ•ด๋‹น ํŒŒํ‹ฐ์…˜์„ ์กฐํšŒํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ SELECT * FROM temp WHERE hh = '__HIVE_DEFAULT_PARTITION__'; ํŒŒํ‹ฐ์…˜ ์ˆ˜์ •/์‚ญ์ œ ํŒŒํ‹ฐ์…˜์˜ ์ˆ˜์ •, ์‚ญ์ œ๋Š” ALTER ๋ฌธ์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜ ์‚ญ์ œ ์‹œ ๋งค๋‹ˆ์ง€๋“œ ํ…Œ์ด๋ธ”์ธ ๊ฒฝ์šฐ ํŒŒํ‹ฐ์…˜ ์œ„์น˜์˜ ๋ฐ์ดํ„ฐ๋„ ํ•จ๊ป˜ ์‚ญ์ œ๋˜๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ์‹ ๊ทœ ํŒŒํ‹ฐ์…˜ ์ถ”๊ฐ€ ALTER TABLE employee ADD PARTITION (yymmdd='20180510'); -- LOCATION์„ ์ง€์ •ํ•ด์„œ ์ถ”๊ฐ€ ALTER TABLE employee ADD PARTITION (yymmdd='20180510') LOCATION 'hdfs://127.0.0.1/user/yymmdd=20180510'; -- ํŒŒํ‹ฐ์…˜์ด ์—†์„ ๋•Œ ์ถ”๊ฐ€ ALTER TABLE employee ADD IF NOT EXISTS PARTITION (yymmdd='20180510'); -- ๋ฉ€ํ‹ฐ ํŒŒํ‹ฐ์…˜ ์ถ”๊ฐ€ ALTER TABLE employee ADD PARTITION (yymmdd='20180510') PARTITION (yymmdd='20180511'); -- ํŒŒํ‹ฐ์…˜ ์ˆ˜์ • ALTER TABLE employee PARTITION (yyyymmdd='20220101') RENAME TO PARTITION (yyyymmdd='20220102'); -- ํŒŒํ‹ฐ์…˜์˜ LOCATION ์ˆ˜์ • ALTER TABLE employee PARTITION (yymmdd='20180510') SET LOCATION 'hdfs://127.0.0.1/user/'; -- ํŒŒํ‹ฐ์…˜ ์‚ญ์ œ ALTER TABLE employee DROP PARTITION (yymmdd='20180510'); -- ํŒŒํ‹ฐ์…˜ ๋ฒ”์œ„ ์‚ญ์ œ, ๋น„๊ต์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด ๋ฒ”์œ„ ์‚ญ์ œ ๊ฐ€๋Šฅ ALTER TABLE employee DROP PARTITION (yymmdd < '20180510'); ALTER TABLE employee DROP PARTITION (yymmdd >= '20180510'); ํŒŒํ‹ฐ์…˜ ๋ณต๊ตฌ ํ…Œ์ด๋ธ” ์‚ญ์ œ, ์ถ”๊ฐ€๋กœ ์ธํ•˜์—ฌ ์‹ ๊ทœ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ณ , ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋‚˜ ์‹ ๊ทœ ๋ฐ์ดํ„ฐ๋กœ ํ…Œ์ด๋ธ”์˜ ํŒŒํ‹ฐ์…˜์„ ๋ณต๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ MSCK ๋ช…๋ น์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ๋งŽ์€ ํŒŒํ‹ฐ์…˜์„ ํ•œ ๋ฒˆ์— ๋ณต๊ตฌํ•˜๊ฒŒ ๋˜๋ฉด ์ž‘์—…์‹œ๊ฐ„์ด ๋Š˜์–ด๋‚˜์„œ ์—ฐ๊ฒฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋“ฑ์œผ๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ hive.msck.repair.batch.size๋ฅผ ์–‘์ˆ˜ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•  ํŒŒํ‹ฐ์…˜ ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •ํ•ด์„œ ๋ช…๋ น์–ด๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๋Œ๋ฆฌ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜์— ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ž๋‚˜ ํŒŒํ‹ฐ์…˜ ๊ทœ์น™์— ๋งž์ง€ ์•Š์„ ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, hive.msck.path.validation ์˜ต์…˜์„ ignore๋กœ ์„ค์ •ํ•˜์—ฌ ์˜ค๋ฅ˜๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ์ง„ํ–‰ํ•˜๊ฒŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. -- 0์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋ชจ๋“  ํŒŒํ‹ฐ์…˜์„ ๋ณต๊ตฌํ•œ๋‹ค. set hive.msck.repair.batch.size=0; -- ํŒŒํ‹ฐ์…˜์— ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ž๊ฐ€ ์žˆ์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ignore๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฌด์‹œํ•˜๊ณ  ๋„˜์–ด๊ฐ„๋‹ค. set hive.msck.path.validation=ignore; -- ํŒŒํ‹ฐ์…˜ ๋ณต๊ตฌ MSCK REPAIR TABLE employee; 04-๋ฒ„์ผ“ํŒ… ๋ฒ„์ผ“ํŒ…์€ ์ง€์ •๋œ ์นผ๋Ÿผ์˜ ๊ฐ’์„ ํ•ด์‹œ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ง€์ •ํ•œ ์ˆ˜์˜ ํŒŒ์ผ๋กœ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์กฐ์ธ์— ์‚ฌ์šฉ๋˜๋Š” ํ‚ค๋กœ ๋ฒ„ํ‚ท ์นผ๋Ÿผ์„ ์ƒ์„ฑํ•˜๋ฉด, ์†ŒํŠธ ๋จธ์ง€ ๋ฒ„ํ‚ท(SMB) ์กฐ์ธ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์–ด ์ˆ˜ํ–‰ ์†๋„๊ฐ€ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ด๊ณ , ๋ฒ„์ผ“ํŒ…์€ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ๋ณ„๋กœ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„์ผ“ํŒ… ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- col2๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฒ„์ผ“ํŒ… ํ•˜์—ฌ 20๊ฐœ์˜ ํŒŒ์ผ์— ์ €์žฅ CREATE TABLE tbl1( col1 STRING, col2 STRING ) CLUSTERED BY (col2) INTO 20 BUCKETS -- col2๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฒ„์ผ“ํŒ… ํ•˜๊ณ , col1 ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ 20๊ฐœ์˜ ํŒŒ์ผ์— ์ €์žฅ CREATE TABLE tbl2( col1 STRING, col2 STRING ) CLUSTERED BY (col2) SORTED BY (col1) INTO 20 BUCKETS ์˜ˆ์ œ ๋‹ค์Œ์€ ๋ฒ„์ผ“ํŒ… ํ…Œ์ด๋ธ” bucketed_table์„ ์ƒ์„ฑํ•˜๊ณ , source_table์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. -- ํ…Œ์ด๋ธ” ์ƒ์„ฑ CREATE TABLE buckted_table ( col1 STRING, col2 STRING ) CLUSTERED BY (col2) SORTED BY (col2) INTO 20 BUCKETS LOCATION '/user/bucketed_table/' -- ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์„ ์œ„ํ•œ ์†Œ์Šค ํ…Œ์ด๋ธ” CREATE TABLE source_table ( col1 ARRAY<STRING> ) ROW FORMAT DELIMITED COLLECTION ITEMS TERMINATED BY '\t'; -- ์†Œ์Šค ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ LOAD DATA LOCAL INPATH './cctv_utf8.csv' INTO TABLE source_table; -- ๋ฒ„์ผ“ํŒ… ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ ์ž…๋ ฅ INSERT INTO TABLE buckted_table SELECT col1[0], col1[3] FROM source_table; bucketed_table์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ ํ›„ ์กฐํšŒํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง€์ •ํ•œ ๊ฐœ์ˆ˜์˜ ๋ฒ„ํ‚ท(ํŒŒ์ผ. 20๊ฐœ)์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ hadoop fs -ls /user/bucketed_table/ Found 20 items -rwxr-xr-x 2 hadoop hadoop 55246 2018-11-08 05:22 /user/bucketed_table/000000_0 -rwxr-xr-x 2 hadoop hadoop 101 2018-11-08 05:22 /user/bucketed_table/000001_0 -rwxr-xr-x 2 hadoop hadoop 2227 2018-11-08 05:22 /user/bucketed_table/000002_0 -rwxr-xr-x 2 hadoop hadoop 3171874 2018-11-08 05:22 /user/bucketed_table/000003_0 -rwxr-xr-x 2 hadoop hadoop 65 2018-11-08 05:22 /user/bucketed_table/000004_0 -rwxr-xr-x 2 hadoop hadoop 102704 2018-11-08 05:22 /user/bucketed_table/000005_0 -rwxr-xr-x 2 hadoop hadoop 0 2018-11-08 05:22 /user/bucketed_table/000006_0 -rwxr-xr-x 2 hadoop hadoop 636043 2018-11-08 05:22 /user/bucketed_table/000007_0 -rwxr-xr-x 2 hadoop hadoop 92 2018-11-08 05:22 /user/bucketed_table/000008_0 -rwxr-xr-x 2 hadoop hadoop 42 2018-11-08 05:22 /user/bucketed_table/000009_0 -rwxr-xr-x 2 hadoop hadoop 379097 2018-11-08 05:22 /user/bucketed_table/000010_0 -rwxr-xr-x 2 hadoop hadoop 148419 2018-11-08 05:22 /user/bucketed_table/000011_0 -rwxr-xr-x 2 hadoop hadoop 49212 2018-11-08 05:22 /user/bucketed_table/000012_0 -rwxr-xr-x 2 hadoop hadoop 1866 2018-11-08 05:22 /user/bucketed_table/000013_0 -rwxr-xr-x 2 hadoop hadoop 2082 2018-11-08 05:22 /user/bucketed_table/000014_0 -rwxr-xr-x 2 hadoop hadoop 123 2018-11-08 05:22 /user/bucketed_table/000015_0 -rwxr-xr-x 2 hadoop hadoop 1268 2018-11-08 05:22 /user/bucketed_table/000016_0 -rwxr-xr-x 2 hadoop hadoop 834307 2018-11-08 05:22 /user/bucketed_table/000017_0 -rwxr-xr-x 2 hadoop hadoop 3631 2018-11-08 05:22 /user/bucketed_table/000018_0 -rwxr-xr-x 2 hadoop hadoop 62 2018-11-08 05:22 /user/bucketed_table/000019_0 05-์Šคํ ์Šคํ๋Š” ์นผ๋Ÿผ์— ํŠน์ • ๋ฐ์ดํ„ฐ๊ฐ€ ์ฃผ๋กœ ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์Šคํ๋Š” ํŒŒํ‹ฐ์…˜๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ์šฉ๋„๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜์€ ์ฃผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํฌ๊ฒŒ ๊ตฌ๋ถ„ํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ์ผ์ž๋ณ„๋กœ ๊ตฌ๋ถ„ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์Šคํ๋Š” ์นผ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์Šคํ๋Š” ํ•˜๋‚˜์˜ ์นผ๋Ÿผ์— ํŠน์ • ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชฐ๋ ค์„œ ์ƒ์„ฑ๋  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด num ์นผ๋Ÿผ์— 1~1000๊นŒ์ง€์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ค๋Š”๋ฐ, ์ฃผ๋กœ 1, 2๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์ด ๋“ค์–ด์˜จ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ํŒŒํ‹ฐ์…˜์€ 1000๊ฐœ์˜ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์Šคํ๋Š” 1, 2 ์™€ ๋‚˜๋จธ์ง€ 3๊ฐœ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋‚˜ ํŒŒ์ผ๋กœ ๊ตฌ๋ณ„ํ•˜์—ฌ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋„ค์ž„๋…ธ๋“œ์˜ ๊ด€๋ฆฌ ํฌ์ธํŠธ๊ฐ€ ์ค„์–ด๋“œ๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜๊ณผ ์Šคํ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ์ƒ์„ฑ๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ํŒŒํ‹ฐ์…˜์€ ๋ฐ์ดํ„ฐ๋ฅผ ํฌ๊ฒŒ ๊ตฌ๋ถ„ํ•  ๋•Œ ์‚ฌ์šฉ /year=2018/month=07/day=01 /year=2018/month=07/day=02 # ์Šคํ๋Š” ์นผ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ ์‚ฌ์šฉ /year=2018/month=07/day=01/code=1 /year=2018/month=07/day=01/code=2 /year=2018/month=07/day=01/code=HIVE_DEFAULT_LIST_BUCKETING_DIR_NAME/ ์Šคํ์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. CREATE TABLE tbl ( col1 STRING, col2 STRING ) SKEWED BY (col1) on ('value1', 'value2' ) [STORED as DIRECTORIES]; ์˜ˆ์ œ ๋‹ค์Œ์€ col2 ์นผ๋Ÿผ์— 1๋กœ ๋“ค์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ ๋งŽ์„ ๋•Œ ์Šคํ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. -- ์Šคํ ํ…Œ์ด๋ธ” ์ƒ์„ฑ -- col2์— ๋“ค์–ด์˜ค๋Š” ๊ฐ’์ค‘ 1๋กœ ๋“ค์–ด์˜ค๋Š” ๊ฐ’๋งŒ ์Šคํ๋กœ ์ €์žฅ CREATE TABLE skewed_table ( col1 STRING, col2 STRING ) SKEWED BY (col2) ON ('1') STORED AS DIRECTORIES LOCATION '/user/skewed_table/'; -- ์†Œ์Šค ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์Šคํ ํ…Œ์ด๋ธ”์— ์ž…๋ ฅ INSERT INTO TABLE skewed_table SELECT col1[0], col1[4] FROM source_table; ์Šคํ ํ…Œ์ด๋ธ”์— ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ๋‹ค์Œ ๊ฐ™์ด ๊ฐœ๋ณ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๊ฐ’์„ ๋ณด๊ด€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. STORED AS DIRECTORIES ์˜ต์…˜์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ๋ถ„ ์—†์ด ํŒŒ์ผ๋กœ ๋”ฐ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. $ hadoop fs -ls /user/skewed_table/ Found 2 items drwxr-xr-x - hadoop hadoop 0 2018-11-08 06:17 /user/skewed_table/HIVE_DEFAULT_LIST_BUCKETING_DIR_NAME drwxr-xr-x - hadoop hadoop 0 2018-11-08 06:17 /user/skewed_table/col2=1 $ hadoop fs -ls -R /user/skewed_table/ drwxr-xr-x - hadoop hadoop 0 2018-11-08 06:17 /user/skewed_table/HIVE_DEFAULT_LIST_BUCKETING_DIR_NAME -rw-r--r-- 2 hadoop hadoop 1490659 2018-11-08 06:17 /user/skewed_table/HIVE_DEFAULT_LIST_BUCKETING_DIR_NAME/000000_0 drwxr-xr-x - hadoop hadoop 0 2018-11-08 06:17 /user/skewed_table/col2=1 -rw-r--r-- 2 hadoop hadoop 2335328 2018-11-08 06:17 /user/skewed_table/col2=1/000000_0 06-์ •๋ ฌ ํ•˜์ด๋ธŒ์˜ ์ •๋ ฌ์€ order by, sort by, distribute by, cluster by ๋„ค ๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ORDER BY ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜์—ฌ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ํด ๊ฒฝ์šฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ , Out Of Memory ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ์ž‘์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ณ , ๋ฐ์ดํ„ฐ๊ฐ€ ํด ๊ฒฝ์šฐ limit ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌ ๊ฐœ์ˆ˜๋ฅผ ์ œํ•œํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. -- ํ…Œ์ด๋ธ” ํ’€ ์Šค์บ” ๊ฐ™์€ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ์ฟผ๋ฆฌ๋Š” nonstrict ๋ชจ๋“œ์—์„œ๋งŒ ๋™์ž‘ set hive.mapred.mode=nonstrict; SELECT * FROM tbl ORDER BY number; -- strict ๋ชจ๋“œ ์ผ ๋•Œ๋Š” LIMIT ๊ฐ€ ์žˆ์–ด์•ผ๋งŒ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ set hive.mapred.mode=strict; SELECT * FROM tbl ORDER BY number LIMIT 100; SORT BY ๋ฆฌ๋“€์„œ ๋ณ„๋กœ ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜๋งŒํผ ์ƒ์„ฑ๋˜๋Š” ํŒŒ์ผ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ •๋ ฌ๋˜์–ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. [1, 4, 3, 2, 5]์˜ ๋ฐ์ดํ„ฐ๋ฅผ 2๊ฐœ์˜ ๋ฆฌ๋“€์„œ๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋ฆฌ๋“€์„œ1์— (1, 4, 3) ์ด ์ „๋‹ฌ๋˜๊ณ  ๋ฆฌ๋“€์„œ2์— (5, 2)๊ฐ€ ์ „๋‹ฌ๋˜๋ฉด, 2๊ฐœ์˜ ๋ฆฌ๋“€์„œ๊ฐ€ 2๊ฐœ์˜ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ๊ฐ์˜ ๊ฒฐ๊ณผ ํŒŒ์ผ์ด (1, 3, 4)์™€ (2, 5)๋กœ ์ •๋ ฌ๋˜์–ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. SELECT * FROM tbl SORT BY number; DISTRIBUTE BY ๋งคํผ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ๋“€์„œ๋กœ ์ „๋‹ฌํ•  ๋•Œ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋Š” row๋Š” ๊ฐ™์€ ๋ฆฌ๋“€์„œ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ๋กœ ์ „๋‹ฌํ•  ๋•Œ ์ •๋ ฌํ•˜์—ฌ ์ „๋‹ฌํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. [1, 4, 1, 3, 2]์˜ ๋ฐ์ดํ„ฐ๋ฅผ 2๊ฐœ์˜ ๋ฆฌ๋“€์„œ๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋ฆฌ๋“€์„œ1์— (1, 2, 1)์ด ์ „๋‹ฌ๋˜๊ณ  ๋ฆฌ๋“€์„œ2์— (4, 3)์ด ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ๊ฐ’์€ ๊ฐ™์€ ๋ฆฌ๋“€์„œ๋กœ ์ „๋‹ฌ๋˜์ง€๋งŒ, ์ „๋‹ฌํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. SELECT * FROM tbl DISTRIBUTE BY number; CLUSTER BY sort by ์™€ DISTRIBUTE by๋ฅผ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋Š” row๋Š” ๊ฐ™์€ ๋ฆฌ๋“€์„œ์— ์ „๋‹ฌ๋˜๊ณ , ๋ฆฌ๋“€์„œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. [1, 4, 1, 3, 2]์˜ ๋ฐ์ดํ„ฐ๋ฅผ 2๊ฐœ์˜ ๋ฆฌ๋“€์„œ๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋ฆฌ๋“€์„œ1์— (1, 2, 1)์ด ์ „๋‹ฌ๋˜๊ณ  ๋ฆฌ๋“€์„œ2์— (4, 3)์ด ์ „๋‹ฌํ•˜๊ณ  ๋ฆฌ๋“€์„œ1 (1, 1, 2), ๋ฆฌ๋“€์„œ2 (3, 4) ํ˜•ํƒœ๋กœ ์ ˆ๋ ฌ๋˜์–ด ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. SELECT * FROM tbl CLUSTER BY age; ์ฐธ๊ณ  Hive Wiki(๋ฐ”๋กœ ๊ฐ€๊ธฐ) 07-์„œ๋ฐ(SerDe) ์„œ๋ฐ(SerDe, Serializer/Deserialaizer)๋Š” ํ•˜์ด๋ธŒ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด์„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ๋Š” ์„œ๋ฐ์™€ ํŒŒ์ผ ํฌ๋งท์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ , ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ๋Š” ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ ํŒŒ์ผ ํฌ๋งท(FileFormat)์„ ์ด์šฉํ•˜๊ณ , ๋””์‹œ ๋ฆฌ์–ผ ๋ผ์ด์ €(Deserializer)๋ฅผ ์ด์šฉํ•˜์—ฌ ์›์ฒœ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ด๋ธ” ํฌ๋งท์— ๋งž๋Š” ๋กœ์šฐ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์“ธ ๋•Œ๋Š” ๋กœ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ๋ฆฌ์–ผ๋ผ์ด์ €(Serializer)๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‚ค, ๋ฐธ๋ฅ˜ ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ํŒŒ์ผ ํฌ๋งท์„ ์ด์šฉํ•˜์—ฌ ์ €์žฅ ์œ„์น˜์— ์”๋‹ˆ๋‹ค. ์„œ๋ฐ๋Š” doDeserialize(), doSerialize()๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ๊ฐ๊ฐ์˜ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. HDFS files --> InputFileFormat --> [key, value] --> Deserializer --> Row object Row object --> Serializer --> [key, value] --> OutputFileFormat --> HDFS files ํ•˜์ด๋ธŒ ๊ธฐ๋ณธ ์„œ ๋ฐ ํ•˜์ด๋ธŒ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ์„œ๋ฐ๋Š” 7๊ฐ€์ง€(Avro, ORC, RegEx, Thrift, Parquet, CSV, JsonSerDe)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์„œ๋ฐ๋Š” STORED AS์— ์ง€์ •ํ•˜๋Š” ํŒŒ์ผ์˜ ํฌ๋งท์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ์„ ํƒ๋ฉ๋‹ˆ๋‹ค. Avro, ORC, Parquet ์€ ์„œ๋ฐ์™€ ์ธํ’‹, ์•„์›ƒํ’‹ ํฌ๋งท์ด ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€๋Š” ๊ธฐ๋ณธ LazySimpleSerDe์™€ ํŒŒ์ผ์— ๋”ฐ๋ฅธ ์ธํ’‹, ์•„์›ƒํ’‹ ํฌ๋งท์ด ์„ค์ • 1 ๋ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์— ์„ค์ •๋˜๋Š” ์„œ๋ฐ๋Š” desc formatted ๋ช…๋ น์œผ๋กœ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. # ORC ํ…Œ์ด๋ธ” ์ƒ์„ฑ CREATE TABLE orc_tbl ( col STRING ) STORED AS ORC; # ORC ํ…Œ์ด๋ธ” ํ™•์ธ hive>desc formatted orc_tbl; # ORC ํ…Œ์ด๋ธ”์˜ ์„œ๋ฐ, ์ธํ’‹ ์•„์›ƒํ’‹ ํฌ๋งท # Storage Information SerDe Library: org.apache.hadoop.hive.ql.io.orc.OrcSerde InputFormat: org.apache.hadoop.hive.ql.io.orc.OrcInputFormat OutputFormat: org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat # TXT ํ…Œ์ด๋ธ” ์ƒ์„ฑ CREATE TABLE txt_tbl ( col STRING ); # TXT ํ…Œ์ด๋ธ” ํ™•์ธ hive>desc formatted txt_tbl; # TXT ํ…Œ์ด๋ธ”์˜ ์„œ๋ฐ, ์ธํ’‹ ์•„์›ƒํ’‹ ํฌ๋งท # Storage Information SerDe Library: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe InputFormat: org.apache.hadoop.mapred.TextInputFormat OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat ์ปค์Šคํ…€ ์„œ ๋ฐ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์„œ๋ฐ์™ธ์— ์‚ฌ์šฉ์ž๊ฐ€ ์„œ๋ฐ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค 2. ์›์ฒœ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜์—ฌ ํ…Œ์ด๋ธ”์˜ ํฌ๋งท์— ๋งž๋Š” ๋ฐ์ดํ„ฐ ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ๋•Œ ํฌ๋งท์„ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— doDeserialize()๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ ๋‹ค์Œ์€ LazySimpleSerDe๋ฅผ ์ƒ์†ํ•˜์—ฌ ์ปค์Šคํ…€ ์„œ๋ฐ๋ฅผ ๊ตฌํ˜„ํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ์ฒ˜๋Ÿผ ๊ฐ’์— ๋Š๋‚Œํ‘œ(!)๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์‹œ์— ์ด๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•œ ์ปค์Šคํ…€ ์„œ๋ฐ ์ž…๋‹ˆ๋‹ค. $ cat sample.txt david 23! cole 3! 5 anna ! 92 LazySimpleSerDe๋ฅผ ์ƒ์†ํ•˜๊ณ  doDeserialize()๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. import org.apache.hadoop.hive.serde2.SerDeException; import org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.Writable; public class SampleSerDe extends LazySimpleSerDe { public SampleSerDe() throws SerDeException { super(); } @Override public Object doDeserialize(Writable field) throws SerDeException { // ๋Š๋‚Œํ‘œ๋Š” ์ œ๊ฑฐ String temp = field.toString().replaceAll("!", ""); return super.doDeserialize(new Text(temp)); } } ์‚ฌ์šฉ๋ฐฉ๋ฒ• ์ปค์Šคํ…€ ์„œ๋ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋ฅผ jar ํŒŒ์ผ๋กœ ๋ฌถ์–ด์„œ ADD JAR ๋ช…๋ น์„ ์ด์šฉํ•ด ์ถ”๊ฐ€ํ•˜๊ณ  ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์‹œ์— ์„ค์ •๊ฐ’์„ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค. ์ดํ›„ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋ฉด ๋Š๋‚Œํ‘œ๊ฐ€ ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ํด๋ž˜์Šค๊ฐ€ ๋“ค์–ด ์žˆ๋Š” jar ํŒŒ์ผ ์ถ”๊ฐ€ hive> ADD JAR ./hiveUDF.jar; # ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์‹œ์— ์„œ ๋ฐ ์ •๋ณด ๋ฐ ํ”„๋กœํผํ‹ฐ ์ •๋ณด ์ „๋‹ฌ hive> CREATE TABLE serde_tbl ( col1 STRING , col2 STRING ) ROW FORMAT SERDE 'com.sec.hive.serde.SampleSerDe' WITH SERDEPROPERTIES ( "field.delim" = "\t" ) # ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ hive> LOAD DATA LOCAL INPATH './sample.txt' INTO TABLE serde_tbl; # ๋ฐ์ดํ„ฐ ์กฐํšŒ hive> select * from serde_tbl; OK david 23 cole 35 anna 92 ๋„ค์ดํ‹ฐ๋ธŒ ์„œ๋ฐ์˜ ์„ค์ •(๋ฐ”๋กœ ๊ฐ€๊ธฐ) โ†ฉ ํ•˜์ด๋ธŒ ์ปค์Šคํ…€ ์„œ ๋ฐ ๊ตฌํ˜„ ๋งค๋‰ด์–ผ(๋ฐ”๋กœ ๊ฐ€๊ธฐ) โ†ฉ 08-๊ฐ€์ƒ ์นผ๋Ÿผ ํ•˜์ด๋ธŒ์—๋Š” ์ž…๋ ฅ๋œ ์›์ฒœ ๋ฐ์ดํ„ฐ์˜ ์œ„์น˜๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€์ƒ ์นผ๋Ÿผ(Virtual Column)์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ ์„ค๋ช… INPUT__FILE__NAME ๋งคํผ์˜ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ ํŒŒ์ผ์˜ ์ด๋ฆ„ BLOCK__OFFSET_INSIDE_FILE ํŒŒ์ผ์—์„œ ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์œ„์น˜ ๊ฐ€์ƒ ์นผ๋Ÿผ์€ ๋‹ค์Œ์ฒ˜๋Ÿผ SELECT ๋ฌธ, WHER ์กฐ๊ฑด์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. select INPUT__FILE__NAME, key, BLOCK__OFFSET__INSIDE__FILE from src; select key, count(INPUT__FILE__NAME) from src group by key order by key; select * from src where BLOCK__OFFSET__INSIDE__FILE > 12000 order by key; 09-์ฟผ๋ฆฌ ๋ถ„์„ ํ•˜์ด๋ธŒ์—์„œ๋Š” explain ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ ์‹คํ–‰ ๊ณ„ํš์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹คํ–‰๊ณ„ํš์˜ ์ „์ฒด ๋งค๋‰ด์–ผ์€ ํ•˜์ด๋ธŒ ๋งค๋‰ด์–ผ 1์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. EXPLAIN [EXTENDED|AST|DEPENDENCY|AUTHORIZATION|LOCKS|VECTORIZATION|ANALYZE] query hive> EXPLAIN select * from tbl; hive> EXPLAIN EXTENDED select * from tbl; ์‹คํ–‰๊ณ„ํš์€ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์Šคํ…Œ์ด์ง€ ์ •๋ณด, ์Šคํ…Œ์ด์ง€์—์„œ ์ฒ˜๋ฆฌ๋˜๋Š” ์ž‘์—…์˜ ์ •๋ณด๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. CBO๋ฅผ ์ด์šฉํ•˜๋ฉด ํ…Œ์ด๋ธ”์˜ ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ตœ์ ํ™”๋ฉ๋‹ˆ๋‹ค. { "STAGE DEPENDENCIES": { "Stage-1": { "ROOT STAGE": "TRUE" }, "Stage-8": { "DEPENDENT STAGES": "Stage-1", "CONDITIONAL CHILD TASKS": "Stage-5, Stage-4, Stage-6" }, "Stage-5": {}, "Stage-2": { "DEPENDENT STAGES": "Stage-5, Stage-4, Stage-7" }, "Stage-0": { "DEPENDENT STAGES": "Stage-2" }, "Stage-3": { "DEPENDENT STAGES": "Stage-0" }, "Stage-4": {}, "Stage-6": {}, "Stage-7": { "DEPENDENT STAGES": "Stage-6" } } , "STAGE PLANS": { "Stage-1": { "Tez": { ... ์˜ต์…˜ EXTENDED ์ถ”๊ฐ€ ์ •๋ณด ํ™•์ธ AST Abstract Syntax Tree ์ •๋ณด ํ™•์ธ DEPENDENCY ํ…Œ์ด๋ธ” ๊ฐ„ ์˜์กด ์ •๋ณด ํ™•์ธ AUTHORIZATION ํ…Œ์ด๋ธ” ์กฐํšŒ ๊ถŒํ•œ ์ •๋ณด ํ™•์ธ LOCKS ํ…Œ์ด๋ธ”์˜ ๋ฝ ์ •๋ณด ํ™•์ธ VECTORIZATION ๋ฒกํ„ฐํ™” ์ฒ˜๋ฆฌ ์ •๋ณด ํ™•์ธ ANALYZE ์‹ค์ œ ์ฐธ์กฐํ•˜๋Š” row ์ •๋ณด ํ™•์ธ https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Explain โ†ฉ 10-ํ†ต๊ณ„ ์ •๋ณด ํ•˜์ด๋ธŒ๋Š” ํ…Œ์ด๋ธ”์˜ ๋กœ์šฐ ์ˆ˜, ํŒŒ์ผ ๊ฐœ์ˆ˜, ์‚ฌ์ด์ฆˆ ๋“ฑ์˜ ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น ๋ฅธ ๋ฐ์ดํ„ฐ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ํ†ต๊ณ„์ •๋ณด๋Š” CBO๋ฅผ ์ด์šฉํ•œ ์‹คํ–‰๊ณ„ํš ์ตœ์ ํ™”, ๋‹จ์ˆœ ์นด์šดํŠธ ์ฟผ๋ฆฌ ๋“ฑ์— ์‚ฌ์šฉ๋˜์–ด ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ๋†’์—ฌ ์ค๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์˜ ํ†ต๊ณ„์ •๋ณด๋ฅผ ์„ค์ •ํ•˜๋Š” ์˜ต์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๋„๋ก ๋˜์–ด ์žˆ์–ด์„œ ๋”ฐ๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์•„๋„ ํ…Œ์ด๋ธ”์˜ ๋ฉ”ํƒ€์ •๋ณด์— ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ„์ „, ์ œ์กฐ์‚ฌ์— ๋”ฐ๋ผ ์„ค์ •์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ™•์ธํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. set hive.stats.autogather=true; set hive.stats.column.autogather=true; ํ†ต๊ณ„์ •๋ณด ์ˆ˜์ง‘ DML์„ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ๋•Œ๋Š” ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ž๋™์œผ๋กœ ์ˆ˜์ง‘ํ•˜์ง€๋งŒ ํŒŒ์ผ ์‹œ์Šคํ…œ ์ƒ์˜ ์ •๋ณด๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” anayze ๋ช…๋ น์œผ๋กœ ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. analyze ์ปค๋งจ๋“œ๋Š” ํ…Œ์ด๋ธ” ๋‹จ์œ„, ํŒŒํ‹ฐ์…˜ ๋‹จ์œ„๋กœ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ†ต๊ณ„์ •๋ณด ์ˆ˜์ง‘์€ ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ž‘์—…์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ด์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ANALYZE TABLE [db_name.] tablename [PARTITION(partcol1[=val1], partcol2[=val2], ...)] -- (Note: Fully support qualified table name since Hive 1.2.0, see HIVE-10007.) COMPUTE STATISTICS [FOR COLUMNS] -- (Note: Hive 0.10.0 and later.) [CACHE METADATA] -- (Note: Hive 2.1.0 and later.) [NOSCAN]; # tbl ํ…Œ์ด๋ธ” ํ†ต๊ณ„์ •๋ณด ์ˆ˜์ง‘ hive> ANALYZE TABLE tbl COMPUTE STATISTICS; # tbl ํ…Œ์ด๋ธ”์˜ yymmdd๊ฐ€ '2018-01-01'์ธ ํŒŒํ‹ฐ์…˜์˜ ํ†ต๊ณ„์ •๋ณด ์ˆ˜์ง‘ hive> ANALYZE TABLE tbl PARTITION(yymmdd='2018-01-01') COMPUTE STATISTICS; # ์นผ๋Ÿผ ํ†ต๊ณ„์ •๋ณด ์ˆ˜์ง‘ hive> ANALYZE TABLE tbl PARTITION(yymmdd='2018-01-01') COMPUTE STATISTICS FOR COLUMNS; ํ†ต๊ณ„์ •๋ณด๋Š” desc extended|formatted ์ปค๋งจ๋“œ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์ฒ˜๋Ÿผ ํ…Œ์ด๋ธ” ์ •๋ณด, ํŒŒํ‹ฐ์…˜ ์ •๋ณด์— ํ†ต๊ณ„์ •๋ณด๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ๋ณด์ž…๋‹ˆ๋‹ค. hive> desc formatted tbl partition(yymmddval='20180101'); OK # col_name data_type comment col1 string Partition Parameters: COLUMN_STATS_ACCURATE {\"BASIC_STATS\":\"true\"} numFiles 6 numRows 618048 rawDataSize 2230248184 totalSize 8546118 transient_lastDdlTime 1544059910 ํ†ต๊ณ„์ •๋ณด ํ™œ์šฉ ์ด๋ ‡๊ฒŒ ์ˆ˜์ง‘ํ•œ ํ†ต๊ณ„์ •๋ณด๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ค์ •์„ ์ด์šฉํ•˜์—ฌ count ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•  ๋•Œ ์ด ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ • ํ›„ count๋ฅผ ์‹คํ–‰ํ•˜๋ฉด MR ์ž‘์—…์„ ํ•˜์ง€ ์•Š๊ณ  ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”๋กœ ํ™•์ธํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. set hive.compute.query.using.stats=true; -- ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ด์šฉํ•œ ์ž‘์—… hive> select count(*) from table; ํ†ต๊ณ„์ •๋ณด ํ™œ์šฉ ์‹œ ์ฃผ์˜ํ•  ์  ํ†ต๊ณ„์ •๋ณด๋Š” INSERT ๋ช…๋ น์„ ์ด์šฉํ•  ๋•Œ ๊ณ„์‚ฐ๋˜์–ด ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์— ๋ณด๊ด€๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŒŒํ‹ฐ์…˜ ์œ„์น˜์— ํŒŒ์ผ์„ ์ง์ ‘ ๋ณต์‚ฌํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐฑ์‹ ๋˜์ง€ ์•Š์•„์„œ ์ •ํ™•ํ•œ ๊ฐ’์ด ๋‚˜์˜ค์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŒŒ์ผ๋งŒ ๋”ฐ๋กœ ๋ณต์‚ฌํ•œ ๊ฒฝ์šฐ์—๋Š” ANALYZE ๋ช…๋ น์„ ์ด์šฉํ•˜์—ฌ ์ •๋ณด๋ฅผ ๊ฐฑ์‹ ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  ํ•˜์ด๋ธŒ StatsDev ๋งค๋‰ด์–ผ(๋ฐ”๋กœ ๊ฐ€๊ธฐ) ํ•˜์ด๋ธŒ Statistics ์„ค์ • ๋งค๋‰ด์–ผ(๋ฐ”๋กœ ๊ฐ€๊ธฐ) 11-ํŒŒ์ผ ๋จธ์ง€ ํ•˜์ด๋ธŒ์˜ ์ž‘์—… ์ค‘ ๋งคํผ ๋‹จ๋… ์ž‘์—…์˜ ๊ฒฝ์šฐ ํŒŒ์ผ์ด ๋งŽ์ด ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž‘์€ ์‚ฌ์ด์ฆˆ์˜ ํŒŒ์ผ์ด ๋งŽ์ด ์ƒ์„ฑ๋˜๋ฉด HDFS์— ๋ถ€๋‹ด์ด ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋•Œ๋Š” ํŒŒ์ผ์„ ๋ฌถ์–ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ๋จธ์ง€ ์„ค์ •์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ ๋จธ์ง€๋ฅผ ์ด์šฉํ•  ๋•Œ ์ฃผ์˜ํ•  ์ ์€ ๋„ˆ๋ฌด ์ž‘์€ ์‚ฌ์ด์ฆˆ์˜ ํŒŒ์ผ์ด ๋งŽ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ž‘์—…์‹œ๊ฐ„์ด ๊ธธ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 1KB ์‚ฌ์ด์ฆˆ์˜ ํŒŒ์ผ 3000๊ฐœ๋ฅผ 256MB ์‚ฌ์ด์ฆˆ๋กœ ๋ฌถ๋Š” ๋จธ์ง€ ์ž‘์—…์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. -- ๋งตํผ ๋‹จ๋… ์ž‘์—…์ผ ๋•Œ ๋จธ์ง€ set hive.merge.mapfiles=true; -- ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์ผ ๋•Œ ๋จธ์ง€ set hive.merge.mapredfiles=true; -- ํ…Œ์ฆˆ ์ž‘์—…์ผ ๋•Œ ๋จธ์ง€ set hive.merge.tezfiles=true; -- ๋จธ์ง€ ์ž‘์—…์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ํŒŒ์ผ ์‚ฌ์ด์ฆˆ(32MB ์ดํ•˜) set hive.merge.smallfiles.avgsize=32000000; -- ๋จธ์ง€ ํŒŒ์ผ์„ ๋ฌถ์„ ๋•Œ ๊ธฐ์ค€(256MB) set hive.merge.size.per.task=256000000; 12-ํŒŒ์ผ ์••์ถ• ํ•˜์ด๋ธŒ๋ฅผ ์ด์šฉํ•˜์—ฌ INSERT DIRECTORY, CTAS ๋ฌธ์œผ๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ๋•Œ ์›ํ•˜๋Š” ํƒ€์ž…์˜ ํ˜•ํƒœ๋กœ ํŒŒ์ผ์„ ์••์ถ•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์••์ถ• ๊ฐ€๋Šฅํ•œ ์ฝ”๋ฑ์€ io.compression.codecs์— ์„ค์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. <property> <name>io.compression.codecs</name> <value>org.apache.hadoop.io.compress.GzipCodec, org.apache.hadoop.io.compress.DefaultCodec, org.apache.hadoop.io.compress.BZip2Codec, org.apache.hadoop.io.compress.SnappyCodec, com.hadoop.compression.lzo.LzoCodec, com.hadoop.compression.lzo.LzopCodec </value> </property> ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ์••์ถ•ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž‘์—… ์ „์— ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์••์ถ•ํ•˜๊ฒ ๋‹ค๋Š” ์„ค์ •๊ณผ ์••์ถ• ์ฝ”๋ฑ์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Gzip์œผ๋กœ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ์••์ถ• ์—ฌ๋ถ€ ์„ค์ • set hive.exec.compress.output=true; # ์••์ถ• ์ฝ”๋ฑ ์„ค์ • set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; ์••์ถ• ์‚ฌ์šฉ INSERT DIRECTORY์™€ CTAS ๋ฌธ์„ ์ด์šฉํ•ด์„œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. set hive.exec.compress.output=true; set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec; # table์„ ์ฝ์–ด์„œ /user/tables/์— CSV ํ˜•ํƒœ๋กœ ์••์ถ•ํ•˜์—ฌ ์ €์žฅ INSERT OVERWRITE DIRECTORY 'hdfs:///user/tables/' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' SELECT * FROM table WHERE name = 'csv' # table์„ ์ฝ์–ด์„œ csvsample ํ…Œ์ด๋ธ”๋Ÿฌ ์ €์žฅ CREATE TABLE csvsample ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LOCATION '/user/csv/' AS SELECT * FROM table WHERE name = 'csv' ํŒŒ์ผ์„ ์••์ถ•ํ•˜์—ฌ ์ €์žฅํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด. gz ํŒŒ์ผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ๋˜๋Š” ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๋Š” ๋งคํผ only ์žก์€ ๋งคํผ์˜ ๊ฐœ์ˆ˜์ด๊ณ  ๋ฆฌ๋“€์„œ ์ž‘์—…์€ ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ๊ฐœ์ˆ˜ ์กฐ์ •์ด ํ•„์š”ํ•˜๋ฉด ๋งค ํผ์™€ ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ € ์ ˆํ•˜ ๋ฉด ๋ฉ๋‹ˆ๋‹ค. $ hadoop fs -ls /user/csv/ -rwxr-xr-x 2 hadoop hadoop 72361505 2019-04-03 08:05 /user/csv/000000_0.gz -rwxr-xr-x 2 hadoop hadoop 74060122 2019-04-03 08:05 /user/csv/000001_0.gz -rwxr-xr-x 2 hadoop hadoop 60733841 2019-04-03 08:05 /user/csv/000002_0.gz 13-์กฐ์ธ ํƒ€์ž… hive์—์„œ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ์กฐ์ธ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์…”ํ”Œ ์กฐ์ธ, ๋งต ์กฐ์ธ, ์ •๋ ฌ-๋ณ‘ํ•ฉ-๋ฒ„ํ‚ท ์กฐ์ธ 3๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์กฐ์ธ์˜ ํ˜ธ์นญ์ด ์—ฌ๋Ÿฌ ๊ฐœ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์กฐ์ธ ๋ช…์นญ ์…”ํ”Œ ์กฐ์ธ(Shuffle Join) ๋จธ์ง€ ์กฐ์ธ(Merge Join) ๋งต ์กฐ์ธ(Map Join) ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ์กฐ์ธ(Broadcast Join), ๋งต ์‚ฌ์ด๋“œ ์กฐ์ธ(Mapside Join) ์ •๋ ฌ-๋ณ‘ํ•ฉ-์กฐ์ธ(SMB Join. Sort-Merge-Bucket Join) - ์…”ํ”Œ ์กฐ์ธ ์…”ํ”Œ ์กฐ์ธ์€ ์…”ํ”Œ(Shuffle) ๋‹จ๊ณ„์—์„œ ์กฐ์ธ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•  ๋•Œ ๊ฐ ํ…Œ์ด๋ธ”์„ ๋งต(Map) ๋‹จ๊ณ„์—์„œ ์ฝ๊ณ , ํŒŒํ‹ฐ์…˜ ํ‚ค๋ฅผ ์กฐ์ธ ํ‚ค๋กœ ์„ค์ •ํ•˜์—ฌ ์…”ํ”Œ ๋‹จ๊ณ„์—์„œ ์กฐ์ธ ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฆฌ๋“€์„œ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ด๋™๋˜๊ณ  ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ ํฌ๊ธฐ์™€ ๊ตฌ์„ฑ์—๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ๊ฐ€์žฅ ์ž์›์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ  ๋Š๋ฆฐ ์กฐ์ธ ๋ฐฉ์‹ ๋งต ์กฐ์ธ ๋งต ์กฐ์ธ์€ ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•  ๋•Œ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์ด ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ ๋˜์–ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์ด ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ์„ ์ •๋„๋กœ ์ž‘์„ ๋•Œ ๋งต ์กฐ์ธ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. hive.auto.convert.join์ด true ์ผ ๋•Œ ์ ์šฉ๋˜๊ณ  hive.auto.convert.join.noconditionaltask.size์ด ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆด ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ์‚ฌ์ด์ฆˆ๋กœ 10MB์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์…”ํ”Œ ์กฐ์ธ์— ๋น„ํ•˜์—ฌ ๋น ๋ฅธ ์†๋„๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ ํ…Œ์ด๋ธ”์ด ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ํฌ๊ธฐ์—ฌ์•ผ ํ•จ -- ๋งต ์กฐ์ธ ์ ์šฉ์„ ์œ„ํ•œ ์„ค์ •. ๊ธฐ๋ณธ 10MB๋กœ ์„ค์ • set hive.auto.convert.join=true; set hive.auto.convert.join.noconditionaltask.size=10000000; SMB ์กฐ์ธ SMB ์กฐ์ธ์€ ์กฐ์ธ ํ…Œ์ด๋ธ”์ด ๋ฒ„์ผ“ํŒ… ๋˜์–ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒ„์ผ“ํŒ…๋œ ํ‚ค์˜ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น ๋ฅด๊ฒŒ ์กฐ์ธ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ํฌ๊ธฐ์˜ ํ…Œ์ด๋ธ”์—์„œ๋„ ๊ฐ€์žฅ ๋น ๋ฅธ ์†๋„๋กœ ์กฐ์ธ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์ด ๋ฒ„์ผ“ํŒ…์ด ๋˜์–ด ์žˆ์–ด์•ผ ํ•จ -- SMB ์กฐ์ธ์„ ์œ„ํ•œ ์„ค์ • set hive.auto.convert.sortmerge.join=true; set hive.optimize.bucketmapjoin=true; set hive.optimize.bucketmapjoin.sortedmerge=true; 1-๋งต ์กฐ์ธ vs ์…”ํ”Œ ์กฐ์ธ ๋งต ์กฐ์ธ๊ณผ ์…”ํ”Œ ์กฐ์ธ์˜ ์ฐจ์ด๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ table_a๋Š” 14.7G์ด๊ณ , table_b๋Š” 5KB์ž…๋‹ˆ๋‹ค. ์ด ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•  ๋•Œ ๊ฐ ์กฐ์ธ์— ๋”ฐ๋ฅธ ์„ฑ๋Šฅ์„ ํ™•์ธํ•ด ๋ณด๋ฉด ์…”ํ”Œ ์กฐ์ธ์ผ ๋•Œ๋Š” ๋ฆฌ๋“€์„œ ๋‹จ๊ณ„๊ฐ€ ์ถ”๊ฐ€๋˜๊ณ  ๋งต ์กฐ์ธ์— ๋น„ํ•˜์—ฌ 2๋ฐฐ์˜ ์‹œ๊ฐ„์ด ๋” ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # table_a์™€ table_b๋ฅผ ์กฐ์ธํ•˜์—ฌ join_test ํ…Œ์ด๋ธ” ์ƒ์„ฑ # table_a: 14.7 GB # table_b: 5 KB CREATE TABLE join_test AS select a.deviceid, b.cnty_cd from db_a.table_a a, db_b.table_b b where a.date = '20191020' and a.code = b.code_cd ์ž‘์—… ์‹œ๊ฐ„ ๋งต ์กฐ์ธ ์ž‘์—… ์‹œ๊ฐ„์€ 75.14์ดˆ, ์…”ํ”Œ ์กฐ์ธ ์ž‘์—… ์‹œ๊ฐ„์€ 130.73์ดˆ์ž…๋‹ˆ๋‹ค. # ๋งต ์กฐ์ธ(Map Join) ---------------------------------------------------------------------------------------------- VERTICES MODE STATUS TOTAL COMPLETED RUNNING PENDING FAILED KILLED ---------------------------------------------------------------------------------------------- Map 1 .......... container SUCCEEDED 31 31 0 0 0 0 Map 2 .......... container SUCCEEDED 1 1 0 0 0 0 ---------------------------------------------------------------------------------------------- VERTICES: 02/02 [==========================>>] 100% ELAPSED TIME: 75.14 s ---------------------------------------------------------------------------------------------- # ์…”ํ”Œ ์กฐ์ธ(Shuffle Join, Merge Join) ---------------------------------------------------------------------------------------------- VERTICES MODE STATUS TOTAL COMPLETED RUNNING PENDING FAILED KILLED ---------------------------------------------------------------------------------------------- Map 1 .......... container SUCCEEDED 33 33 0 0 0 0 Map 3 .......... container SUCCEEDED 1 1 0 0 0 0 Reducer 2 ...... container SUCCEEDED 1009 1009 0 0 0 0 ---------------------------------------------------------------------------------------------- VERTICES: 03/03 [==========================>>] 100% ELAPSED TIME: 130.73 s ---------------------------------------------------------------------------------------------- ์‹คํ–‰ ๊ณ„ํš ํ™•์ธ ๋งต ์กฐ์ธ hive (sample_db)> explain CREATE TABLE join_test > AS > select a.deviceid, b.cnty_cd > from db_a.table_a a, > db_b.table_b b > where a.date = '20191020' > and a.code = b.code_cd > ; OK Plan optimized by CBO. Vertex dependency in root stage Map 1 <- Map 2 (BROADCAST_EDGE) Stage-3 Stats-Aggr Operator Stage-4 Create Table Operator: name:sample_db.join_test Stage-2 Dependency Collection{} Stage-1 Map 1 File Output Operator [FS_10] table:{"name:":"sample_db.join_test"} Select Operator [SEL_9] (rows=290865947 width=2073) Output:["_col0","_col1"] Map Join Operator [MAPJOIN_15] (rows=290865947 width=2073) Conds:SEL_2._col1=RS_7.UDFToString(_col0)(Inner),HybridGraceHashJoin:true, Output:["_col0","_col4"] <-Map 2 [BROADCAST_EDGE] BROADCAST [RS_7] PartitionCols:UDFToString(_col0) Select Operator [SEL_5] (rows=513 width=10) Output:["_col0","_col1"] Filter Operator [FIL_14] (rows=513 width=10) predicate:code_cd is not null TableScan [TS_3] (rows=513 width=10) db_b@table_b, b, Tbl:COMPLETE, Col:NONE, Output:["code_cd","cnty_cd"] <-Select Operator [SEL_2] (rows=264423583 width=2073) Output:["_col0","_col1"] Filter Operator [FIL_13] (rows=264423583 width=2073) predicate:code is not null TableScan [TS_0] (rows=264423583 width=2073) db_a@table_a, a, Tbl:COMPLETE, Col:NONE, Output:["deviceid","code"] Stage-0 Move Operator Please refer to the previous Stage-1 Time taken: 0.143 seconds, Fetched: 39 row(s) ์…”ํ”Œ ์กฐ์ธ hive (sample_db)> explain CREATE TABLE join_test > AS > select a.deviceid, b.cnty_cd > from db_a.table_a a, > db_b.table_b b > where a.date = '20191020' > and a.code = b.code_cd > ; OK Plan optimized by CBO. Vertex dependency in root stage Reducer 2 <- Map 1 (SIMPLE_EDGE), Map 3 (SIMPLE_EDGE) Stage-3 Stats-Aggr Operator Stage-4 Create Table Operator: name:sample_db.join_test Stage-2 Dependency Collection{} Stage-1 Reducer 2 File Output Operator [FS_10] table:{"name:":"sample_db.join_test"} Select Operator [SEL_9] (rows=290865947 width=2073) Output:["_col0","_col1"] Merge Join Operator [MERGEJOIN_15] (rows=290865947 width=2073) Conds:RS_6._col1=RS_7.UDFToString(_col0)(Inner),Output:["_col0","_col4"] <-Map 1 [SIMPLE_EDGE] SHUFFLE [RS_6] PartitionCols:_col1 Select Operator [SEL_2] (rows=264423583 width=2073) Output:["_col0","_col1"] Filter Operator [FIL_13] (rows=264423583 width=2073) predicate:code is not null TableScan [TS_0] (rows=264423583 width=2073) db_a@table_a, a, Tbl:COMPLETE, Col:NONE, Output:["deviceid","code"] <-Map 3 [SIMPLE_EDGE] SHUFFLE [RS_7] PartitionCols:UDFToString(_col0) Select Operator [SEL_5] (rows=513 width=10) Output:["_col0","_col1"] Filter Operator [FIL_14] (rows=513 width=10) predicate:code_cd is not null TableScan [TS_3] (rows=513 width=10) db_b@table_b, b, Tbl:COMPLETE, Col:NONE, Output:["code_cd","cnty_cd"] Stage-0 Move Operator Please refer to the previous Stage-1 Time taken: 0.146 seconds, Fetched: 42 row(s) ์‹คํ–‰๊ณ„ํš ๋น„๊ต ๋‘ ์ฟผ๋ฆฌ์˜ ์‹คํ–‰๊ณ„ํš์„ ๋น„๊ตํ•ด ๋ณด๋ฉด ์…”ํ”Œ ์กฐ์ธ์€ ๋ฆฌ๋“€์„œ ๋‹จ๊ณ„๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ํ™•์‹คํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ ๋‹จ๊ณ„๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ์…”ํ”Œ ์กฐ์ธ๋ณด๋‹ค๋Š” ๋งต ์กฐ์ธ์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์ƒ ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. 2-๋งต ์กฐ์ธ ๊ธฐ์ค€ ๋งต ์กฐ์ธ์€ ์ž‘์€ ํฌ๊ธฐ์˜ ํ…Œ์ด๋ธ”์„ ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌํ•˜์—ฌ ์กฐ์ธ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋Š” ์ž๋ฐ” ๊ฐ์ฒด์˜ ์‚ฌ์ด์ฆˆ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ํŒŒ์ผ์˜ ์‚ฌ์ด์ฆˆ์™€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ์˜ ํ…Œ์ด๋ธ”์€ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๊ณ  ์žˆ๋Š” ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. 65,454๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ<NAME>๋งŒ ๋‹ค๋ฅด๊ฒŒ ํ•˜์—ฌ ์ €์žฅํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งต ์กฐ์ธ์—๋Š” ๊ฐ์ฒด ์‚ฌ์ด์ฆˆ๋ฅผ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— hive.auto.convert.join.noconditionaltask.size๋ฅผ ์„ค์ •ํ•  ๋•Œ ๊ฐ์ฒด ์‚ฌ์ด์ฆˆ ๊ธฐ์ค€์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์„ค์ •์ด 10MB ์ผ ๋•Œ table_orc๋Š” ์…”ํ”Œ ์กฐ์ธ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๊ณ , table_txt๋Š” ๋งต ์กฐ์ธ์œผ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. table_orc๋ฅผ ๋งต ์กฐ์ธ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” hive.auto.convert.join.noconditionaltask.size=20000000์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ORC<NAME>์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ์••์ถ•ํ•˜์—ฌ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌผ๋ฆฌ์ ์ธ ํŒŒ์ผ์˜ ํฌ๊ธฐ๊ฐ€ ์›๋ณธ์— ๋น„ํ•˜์—ฌ ์ค„์–ด๋“  ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์ฒด ์‚ฌ์ด์ฆˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…๊ณผ ์ž๋ฐ” ๊ฐ์ฒด์˜ ์˜ค๋ฒ„ํ—ค๋“œ ํ†ต๊ณ„ ์ •๋ณด ๋“ฑ์„ ์ถ”๊ฐ€ํ•œ ์‚ฌ์ด์ฆˆ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์›๋ณธ ์‚ฌ์ด์ฆˆ๋ณด๋‹ค ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”<NAME> ํŒŒ์ผ ์‚ฌ์ด์ฆˆ(totalSize) ๊ฐ์ฒด ์‚ฌ์ด์ฆˆ(rawDataSize) table_orc ORC 254,632(248 KB) 18,850,752(17.9 MB) table_txt TXT 2,639,622(2.5 MB) 2,574,168 (2.4 MB) table ์ •๋ณด ๋น„๊ต table_txt hive (hseok1_seo)> desc formatted table_txt; OK # col_name data_type comment column_1 string column_2 string column_3 string # Detailed Table Information Table Parameters: numFiles 1 numRows 65454 rawDataSize 2574168 totalSize 2639622 # Storage Information SerDe Library: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe InputFormat: org.apache.hadoop.mapred.TextInputFormat OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat table_orc hive (hseok1_seo)> desc formatted table_orc; OK # col_name data_type comment column_1 string column_2 string column_3 string # Detailed Table Information Table Parameters: numFiles 1 numRows 65454 rawDataSize 18850752 totalSize 254632 # Storage Information SerDe Library: org.apache.hadoop.hive.ql.io.orc.OrcSerde InputFormat: org.apache.hadoop.hive.ql.io.orc.OrcInputFormat OutputFormat: org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat 4-ํ•จ์ˆ˜ ํ•˜์ด๋ธŒ์˜ ํ•จ์ˆ˜๋Š” UDF1, UDAF2, UDTF3๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ํƒ€์ž…์˜ ๊ธฐ๋ณธ ํ•จ์ˆ˜๋Š” ํ•˜์ด๋ธŒ ๋งค๋‰ด์–ผ์„ ํ™•์ธํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜ ์™ธ์—๋„ ์‚ฌ์šฉ์ž๊ฐ€ ์ƒ์„ฑํ•œ ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. UDF UDAF UDTF UDTF ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜ UDTF ํ•จ์ˆ˜ ์˜ˆ์ œ LATERAL VIEW json_tuple ์˜ˆ์ œ ์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜ UDF UDF๋Š” 1๊ฐœ์˜ ์—ด(row)์„ ์ฒ˜๋ฆฌํ•˜์—ฌ, 1๊ฐœ์˜ ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. substr(), round() ๋“ฑ์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. -- ๋ฌธ์ž์—ด์˜ ๋‘ ๋ฒˆ์งธ index๋ถ€ํ„ฐ 3๊ฐœ์˜ ์ž๋ฅผ ๋ฐ˜ํ™˜ hive> select substr('ABCDEFG', 2, 3); OK BCD hive> select substr(col1, 2, 3) from tbl1; OK BCD UDAF UDAF๋Š” N ๊ฐœ์˜ ์—ด์„ ์ด์šฉํ•˜์—ฌ, 1๊ฐœ์˜ ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. RDB์˜ ์œˆ๋„ ํ•จ์ˆ˜๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. count(), sum(), max() ๋“ฑ์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. SELECT count(1) FROM tbl1 GROUP BY col1 UDTF UDTF๋Š” 1๊ฐœ์˜ ์—ด์„ ์ž…๋ ฅ๋ฐ›์•„์„œ N ๊ฐœ์˜ ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ, ๋งต ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ด๋ธ”๋กœ ๋ณด์—ฌ์ค„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , LATERAL VIEW์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์› ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ์™€ ์กฐ์ธํ•œ ํ˜•ํƒœ๋กœ ๊ฐ’์„ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. hive> select explode(array(1, 2, 3)); OK 2 UDTF ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜ explode, inline, posexplode array, map, struct<NAME>์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ json_tuple json ๋ฌธ์ž์—ด์„ ํŒŒ์‹ฑ ํ•˜์—ฌ ๋ฐ˜ํ™˜ get_json_object() ์™€ ๋น„์Šทํ•œ๋ฐ ์†๋„๊ฐ€ ๋น ๋ฆ„ xpath๋ฅผ ์ด์šฉํ•œ ์ฒ˜๋ฆฌ๋Š” ์•ˆ๋จ url_tuple url ๋ฌธ์ž๋ฅผ ํŒŒ์‹ฑ HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE๋ฅผ ๋ฐ˜ํ™˜ stack ์ „๋‹ฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ–‰์œผ๋กœ ๋ฐ˜ํ™˜ UDTF ํ•จ์ˆ˜ ์˜ˆ์ œ UDTF ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- EMPLOYEE ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ ํ™•์ธ hive> desc employee; OK id string name string lists array<string> maps map<string, string> salary int hive> select * from employee; OK id-1 john ["a","b","c","d"] {"k1":"v1","k2":"v2"} 100 id-2 sam ["e","f","g","h"] {"k3":"v3","k4":"v4"} 300 id-3 tim ["i","j","k","l"] {"k5":"v5","k6":"v6"} 1000 id-4 paul ["z","c","v","b"] {"k7":"v7","k8":"v8"} 800 id-5 kill ["q","w","e","r"] {"k9":"v9","k0":"v0"} 600 -- ๋ฆฌ์ŠคํŠธ๋ฅผ explode() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ hive> select explode(lists) as col1 > from employee; OK b d f h j l c b w r Time taken: 0.054 seconds, Fetched: 20 row(s) -- ๋งต์„ explode() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ key, value ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ hive> select explode(maps) as (key, value) > from employee; OK k1 v1 k2 v2 k3 v3 k4 v4 k5 v5 k6 v6 k7 v7 k8 v8 k9 v9 k0 v0 Time taken: 0.052 seconds, Fetched: 10 row(s) -- ๋ฆฌ์ŠคํŠธ๋ฅผ posexplode() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ, ํฌ์ง€์…˜ ์นผ๋Ÿผ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ hive> select posexplode(lists) as (pos, col1) > from employee; OK 0 a 1 b 2 c 3 d 0 e 1 f 2 g 3 h 0 i 1 j 2 k 3 l 0 z 1 c 2 v 3 b 0 q 1 w 2 e 3 r Time taken: 0.059 seconds, Fetched: 20 row(s) -- inline, array, struct๋ฅผ ์ด์šฉํ•œ JSON ๋ฌธ์ž์—ด์˜ ํ…Œ์ด๋ธ”ํ™” ์˜ˆ์ œ hive> SELECT inline(array( struct(get_json_object(str, "$.key1")) > , struct(get_json_object(str, "$.key2")) )) > FROM ( SELECT '{ "key1": "a", "value1" : "1", "key2": "b", "value2" : "2" }' AS str) t > ; OK b Time taken: 0.031 seconds, Fetched: 2 row(s) LATERAL VIEW LATERAL VIEW๋Š” UDTF ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ์กด ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ์— ์ถ”๊ฐ€ํ•˜์—ฌ, ์กฐ์ธ๋œ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. -- listTable ๊ฒฐ๊ณผ ํ™•์ธ hive> select col_list from listTable; OK ["1","2","3"] ["4","5","6"] ["7","8","9"] -- LATERAL VIEW๋ฅผ ์ด์šฉํ•ด ๊ธฐ์กด ํ…Œ์ด๋ธ”์˜ ๊ฒฐ๊ณผ์™€ ํ•จ๊ป˜ ์ถœ๋ ฅ hive> SELECT col_list, item > FROM listTable LATERAL VIEW explode(col_list) t as item > ; OK ["1","2","3"] 1 ["1","2","3"] 2 ["1","2","3"] 3 ["4","5","6"] 4 ["4","5","6"] 5 ["4","5","6"] 6 ["7","8","9"] 7 ["7","8","9"] 8 ["7","8","9"] 9 Time taken: 0.142 seconds, Fetched: 9 row(s) -- explode() ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ hive> SELECT explode(col_list) > FROM listTable; OK 2 4 6 8 Time taken: 0.05 seconds, Fetched: 9 row(s) json_tuple ์˜ˆ์ œ json_tuple์€ LATERAL VIEW ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ json ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SELECT j.service_id, j.component_id FROM json_table t LATERAL VIEW JSON_TUPLE(t.json_body, 'service_id', 'component_id') j AS service_id, component_id; ์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜ ์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ ํ•จ์ˆ˜ ์™ธ์— ์‚ฌ์šฉ์ž๊ฐ€ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. JAVA ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ , jar ํŒŒ์ผ๋กœ ๋ฌถ์–ด์„œ ADD JAR ๋ช…๋ น์œผ๋กœ jar ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. CREATE FUNCTION ๋ช…๋ น์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์—ฌ SELECT ๋ฌธ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ƒ์„ธํ•œ ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•์€ ํ•˜์ด๋ธŒ ๋งค๋‰ด์–ผ 4๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. -- JAR ์ถ”๊ฐ€ ๋ฐ ํ•จ์ˆ˜ ์ƒ์„ฑ ADD JAR hdfs:///user/hive/SampleUDF.jar; CREATE TEMPORARY FUNCTION time_stamp AS 'sdk.hive.TimeStampUDF'; -- ๊ธฐ์กด ํ•จ์ˆ˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ SELECT time_stamp(col1) FROM sample; UDF ํ•จ์ˆ˜ ๋งค๋‰ด์–ผ(Built-in Funcs) (๋ฐ”๋กœ ๊ฐ€๊ธฐ) โ†ฉ UDAF ํ•จ์ˆ˜ ๋งค๋‰ด์–ผ(Built-in Aggregate Functions) (๋ฐ”๋กœ ๊ฐ€๊ธฐ โ†ฉ UDTF ํ•จ์ˆ˜ ๋งค๋‰ด์–ผ(Built-in Table-Generating Functions) (๋ฐ”๋กœ ๊ฐ€๊ธฐ โ†ฉ ์‚ฌ์šฉ์ž ํ•จ์ˆ˜ ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•(๋ฐ”๋กœ ๊ฐ€๊ธฐ) โ†ฉ 1-๋‚ด์žฅ ํ•จ์ˆ˜(Built-In Function) ํ•˜์ด๋ธŒ์—์„œ ์ œ๊ณตํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋‚ด์žฅ ํ•จ์ˆ˜ ์ค‘์—์„œ ์œ ์šฉํ•œ ํ•จ์ˆ˜ ๋ช‡ ๊ฐ€์ง€์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜์˜ ์ „์ฒด ๋ชฉ๋ก์€ ํ•˜์ด๋ธŒ ๋งค๋‰ด์–ผ 1์„ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ชฉ๋ก, ํ™•์ธ ๋ช…๋ น์–ด ๊ด€๊ณ„ํ˜• ๋ช…๋ น์–ด ๋…ผ๋ฆฌํ˜• ๋ช…๋ น์–ด ๋ณตํ•ฉํ˜• ์ƒ์„ฑ ๋ช…๋ น์–ด ๊ธฐ๋ณธ ๋ช…๋ น์–ด ์บ์ŠคํŒ… ๋ช…๋ น ์ปฌ๋ ‰์…˜ ๋ช…๋ น ๋‚ ์งœ ๋ช…๋ น ์กฐ๊ฑด ํ•จ์ˆ˜ ๋ฌธ์ž์—ด ํ•จ์ˆ˜ UDAF ๋ชฉ๋ก, ํ™•์ธ ๋ช…๋ น์–ด ํ•˜์ด๋ธŒ์— ๋“ฑ๋ก๋œ ํ•จ์ˆ˜, ํ…Œ์ด๋ธ”, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด๋Š” show์™€ desc ๋˜๋Š” describe๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ค๋ช… show ํ•จ์ˆ˜, ํ…Œ์ด๋ธ”, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ชฉ๋ก์„ ํ™•์ธ desc ํ•จ์ˆ˜, ํ…Œ์ด๋ธ”, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์„ค์ •๊ฐ’์„ ํ™•์ธ, extended, formatted ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ธ ์„ค์ •์„ ํ™•์ธ -- ๋ชฉ๋ก ํ™•์ธ show databases; show tables; show functions; -- ์„ค๋ช… ํ™•์ธ desc table tbl; desc extends tbl; desc formatted tbl; -- describe๋Š” desc์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. describe table tbl; ๊ด€๊ณ„ํ˜• ๋ช…๋ น์–ด WHERE ์กฐ๊ฑด์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ด€๊ณ„ํ˜• ๋ช…๋ น์–ด ์ค‘ between A and B๋Š” ์ผ์ž๋ณ„ ํŒŒํ‹ฐ์…˜์œผ๋กœ ๊ตฌ๋ถ„๋œ ํ…Œ์ด๋ธ”์˜ ์กฐํšŒ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ์ž ํŒŒํ‹ฐ์…˜์ด ๋ฌธ์žํ˜•(String)์œผ๋กœ ์„ ์–ธ๋˜์–ด ์žˆ์–ด๋„ ์ž๋™์œผ๋กœ ์บ์ŠคํŒ…ํ•˜์—ฌ ๋น„๊ตํ•ด ์ค๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ค๋ช… A between B and C A์˜ ๊ฐ’์ด B ์ด์ƒ, C ์ดํ•˜์ด๋ฉด TRUE ๋ฐ˜ํ™˜ SELECT * FROM tbl WHERE yymmdd between '20180101' and '20180107'; ๋…ผ๋ฆฌํ˜• ๋ช…๋ น์–ด ๋…ผ๋ฆฌํ˜• ๋ช…๋ น์–ด ์ค‘์—์„œ ์–ด๋–ค ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ๊ฒ€์ฆํ•˜๋Š” IN์€ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ์กด์žฌ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -- col ์นผ๋Ÿผ์˜ ๊ฐ’์ด ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ ์•ˆ์— ์žˆ์œผ๋ฉด ์ฒ˜๋ฆฌ SELECT * FROM tbl WHERE col IN (SELECT col FROM sub_tbl) -- col ์นผ๋Ÿผ์˜ ๊ฐ’์ด a, b, c ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ฒ˜๋ฆฌ SELECT * FROM tbl WHERE col NOT IN ( 'a', 'b', 'c' ) ๋ณตํ•ฉํ˜• ์ƒ์„ฑ ๋ช…๋ น์–ด ํ•˜์ด๋ธŒ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ณตํ•ฉ ํƒ€์ž…(Map, Array, Struc)์„ ์ฟผ๋ฆฌ์—์„œ ์ง์ ‘ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ค๋ช… map(key1, val1, ) ์ฃผ์–ด์ง„ ํ‚ค์™€ ๊ฐ’์œผ๋กœ ๋งต์„ ์ƒ์„ฑ array(val1, val2) ์ฃผ์–ด์ง„ ๊ฐ’์œผ๋กœ ๋ฐฐ์—ด์„ ์ƒ์„ฑ struct(val1, val2) ์ฃผ์–ด์ง„ ๊ฐ’์œผ๋กœ ๊ตฌ์กฐ์ฒด๋ฅผ ์ƒ์„ฑ named_struct(col1, val1, col2, val2, ...) ์ฃผ์–ด์ง„ ๊ฐ’์œผ๋กœ ์ด๋ฆ„์žˆ๋Š” ๊ตฌ์กฐ์ฒด๋ฅผ ์ƒ์„ฑ create_union(index, val1, val2) ์ฃผ์–ด์ง„ ๊ฐ’์œผ๋กœ Union ์ƒ์„ฑ -- ๋งต ์ƒ์„ฑ hive> select map('key1', 'value1', 'key2', 'value2'); OK {"key1":"value1","key2":"value2"} -- ๋ฐฐ์—ด ์ƒ์„ฑ hive> select array(1, 2, 3); OK [1,2,3] -- ๊ตฌ์กฐ์ฒด ์ƒ์„ฑ hive> select struct(1, "a"); OK {"col1":1, "col2":"a"} -- ์ด๋ฆ„ ์žˆ๋Š” ๊ตฌ์กฐ์ฒด ์ƒ์„ฑ hive> SELECT named_struct("age", 10, "name", "aa") AS col1 OK {"age":10, "name":"aa"} -- ์ฒซ ๋ฒˆ์งธ 0์ด ๋ฐ์ดํ„ฐ์˜ ์ธ๋ฑ์Šค hive> SELECT create_union(0, 60, "a"); {0:60} -- ์ฒซ ๋ฒˆ์งธ 1์ด ๋ฐ์ดํ„ฐ์˜ ์ธ๋ฑ์Šค hive> SELECT create_union(1, 60, "a"); {1:"a"} ๊ธฐ๋ณธ ๋ช…๋ น์–ด ํ•˜์ด๋ธŒ์˜ ๊ธฐ๋ณธ ๋ช…๋ น์–ด ์ค‘์—์„œ ์œ ์šฉํ•œ ๋ช…๋ น์–ด์˜ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ช…๋ น์–ด๋Š” ํ•˜์ด๋ธŒ ๋งค๋‰ด์–ผ์„ ์ฐธ๊ณ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์บ์ŠคํŒ… ๋ช…๋ น ์ฃผ์–ด์ง„ ๊ฐ’์˜ ํ˜•์„ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๋•Œ cast๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ค๋ช… cast( expr as <type>) ์ฃผ์–ด์ง„ ๊ฐ’์˜ ํ˜•์„ ๋ณ€๊ฒฝ -- ๋ฌธ์ž์—ด์„ intํ˜•์œผ๋กœ ๋ณ€๊ฒฝ hive> select cast( "1" as int); OK ์ปฌ๋ ‰์…˜ ๋ช…๋ น ์ปฌ๋ ‰์…˜์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ค๋ช… size(Map or Array) ๋งต๊ณผ ๋ฐฐ์—ด์˜ ๊ฐ’์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ array_contains(Array, value) ๋ฐฐ์—ด์— ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธ sort_array(Array) ๋ฐฐ์—ด์„ ์ •๋ ฌ -- ์‚ฌ์ด์ฆˆ ํ™•์ธ hive> select size(map('key1', 'value1', 'key2', 'value2')); OK -- ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ hive> select array_contains(array(1, 2, 3), 2); OK true -- ๋ฐฐ์—ด์„ ์ •๋ ฌ hive> select sort_array(array(3, 1, 2)); OK [1,2,3] ๋‚ ์งœ ๋ช…๋ น ๋‚ ์งœ ๊ด€๋ จ ํ•จ์ˆ˜๋Š” ๋‚ ์งœ์˜ ์ถœ๋ ฅ ํฌ๋งท์„ ๋ณ€๊ฒฝํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ผ์ž๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ค๋ช… unix_timestamp(string date[, string format]) ์ฃผ์–ด์ง„ ๊ฐ’์„ unixtime์œผ๋กœ ๋ณ€ํ™˜ from_unixtime(bigint unixtime[, string format]) unixtime์„ ์ผ์ž๋กœ ๋ณ€ํ™˜ -- ๋ฌธ์ž์—ด ์ผ์ž๋ฅผ unix ํƒ€์ž„์œผ๋กœ ๋ณ€ํ™˜ hive> select unix_timestamp("2018-01-01 00:00:00", 'yyyy-MM-dd HH:mm:ss'); OK 1514764800 -- unix ํƒ€์ž„์„ ์ง€์ •ํ•œ ํƒ€์ž…์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ hive> select from_unixtime(1514764800, 'yyyy-MM-dd HH:mm:ss'); OK 2018-01-01 00:00:00 ์กฐ๊ฑด ํ•จ์ˆ˜ ์กฐ๊ฑด ํ•จ์ˆ˜๋Š” ์กฐ๊ฑด์— ๋”ฐ๋ผ ๊ฐ’์˜ ์ถœ๋ ฅ์„ ๋ณ€๊ฒฝํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ค๋ช… if(์กฐ๊ฑด, ์ฐธ, ๊ฑฐ์ง“) ์กฐ๊ฑด์„ ์ด์šฉํ•˜์—ฌ ๋งž๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ isnull( a) null ๊ฐ’์ธ์ง€ ์ฒดํฌ nvl(T value, T default_value) null์ด๋ฉด ๊ธฐ๋ณธ๊ฐ’ ๋ฐ˜ํ™˜ COALESCE(T v1, T v2, ...) null ์ด ์•„๋‹ ๊ฒฝ์šฐ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ -- ์กฐ๊ฑด๋ฌธ์ด True ์ด๋ฉด ์ฐธ์˜ ๊ฐ’, False ์ด๋ฉด ๊ฑฐ์ง“์˜ ๊ฐ’ hive> select if(1=1, 'a','b'); OK -- ์กฐ๊ฑด์ด null์ด๋ฉด true ๋ฐ˜ํ™˜ hive> select isnull(null); OK true -- ์ž…๋ ฅ๊ฐ’์ด null์ด๋ฉด ๊ธฐ๋ณธ๊ฐ’ ๋ฐ˜ํ™˜ hive> select nvl(null, 'a'); OK -- ์ž…๋ ฅ๊ฐ’์ด null์ด ์•„๋‹ˆ๋ฉด ์ž…๋ ฅ๊ฐ’ ๋ฐ˜ํ™˜ hive> select nvl('b', 'a'); OK -- ์ˆœ์„œ๋Œ€๋กœ null ์ด ์•„๋‹Œ ์ตœ์ดˆ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ hive> select coalesce(null,'a','b'); OK hive> select coalesce(1, 'a','b'); OK hive> select coalesce(null, null,'b'); OK ๋ฌธ์ž์—ด ํ•จ์ˆ˜ ๋ฌธ์ž์—ด์„ ์กฐ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์ข…๋ฅ˜๊ฐ€ ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ์ค‘์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋ฌธ์ž์—ด์„ ๋ณ‘ํ•ฉํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ค๋ช… concat(string binary A, string concat_ws(string SEP, string A, string B...) SEP๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ๋ณ‘ํ•ฉ concat_ws(string SEP, array<string>) SEP๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ๋ณ‘ํ•ฉ -- ๋ฌธ์ž์—ด ๋ณ‘ํ•ฉ hive> select concat('A','B'); OK AB -- SEP๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ณ‘ํ•ฉ hive> select concat_ws(',','a','b'); OK a, b -- SEP๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด์„ ๋ณ‘ํ•ฉ hive> select concat_ws(',',array('a','b','c')); OK a, b, c ๋‹ค์Œ์€ ๋ฌธ์ž์—ด์„ ์กฐ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ค๋ช… substr(string A, int start) ๋ฌธ์ž์—ด์„ ์ž๋ฆ„ substring(string A, int start) ๋ฌธ์ž์—ด์„ ์ž๋ฆ„ trim(string A) ๋ฌธ์ž์—ด ์•ž, ๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐ replace(string A, string old, string new) A ์•ˆ์˜ old ๋ฌธ์ž์—ด์„ new๋กœ ๋ณ€๊ฒฝ -- start ์ง€์ ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ hive> select substr('123456789',3); OK 3456789 -- start ์ง€์ ๋ถ€ํ„ฐ ์ง€์ •ํ•œ ๊ฐœ์ˆ˜์˜ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ hive> select substr('123456789',3,2); OK 34 -- ๋ฌธ์ž์—ด ์–‘์ชฝ์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐ hive> select trim(' 123 '); OK 123 -- ๋ฌธ์ž์—ด์— ์กด์žฌํ•˜๋Š” old ๋ฌธ์ž๋ฅผ new ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ hive> select replace('123123123','2','a'); OK 1a31a31a3 ๋‹ค์Œ์€ ๋ฌธ์ž์—ด์„ ๋งต, ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์„ค๋ช… str_to_map(text[, delimiter1, delimiter2]) String์„ ๊ตฌ๋ถ„์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‚ค, ๊ฐ’์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋งต์œผ๋กœ ๋ณ€ํ™˜ split(string str, string pat) ๋ฌธ์ž์—ด์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ -- ๋ฌธ์ž์—ด์„ ๋งต์œผ๋กœ ๋ณ€ํ™˜ hive> select str_to_map('a:1, b:2, c:3', ',' ,':'); OK {"a":"1","b":"2","c":"3"} -- ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ hive> select split('a, b, c',','); OK ["a","b","c"] UDAF ํ•จ์ˆ˜ ์„ค๋ช… collect_set(col) ์ค‘๋ณต ์ œ๊ฑฐ๋œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ collect_list(col) ์นผ๋Ÿผ ๊ฐ’์˜ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ hive> select col1, col2 from tbl; 1 a 1 a 2 a 2 b hive> select col1, collect_list(col2) from tbl group by col1; 1 ['a', 'a'] 2 ['a', 'b'] hive> select col1, collect_set(col2) from tbl group by col1; 1 ['a'] 2 ['a', 'b'] ํ•˜์ด๋ธŒ ๋‚ด์žฅ ํ•จ์ˆ˜ ๋งค๋‰ด์–ผ (๋ฐ”๋กœ ๊ฐ€๊ธฐ) โ†ฉ 2-์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜ ์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜๋Š” ํ•˜์ด๋ธŒ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ํ•จ์ˆ˜ ๊ตฌํ˜„์„ ์ƒ์†ํ•˜์—ฌ Java๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. UDF, UDAF, UDTF๋ฅผ ๊ฐ๊ฐ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ UDTF ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 1-UDF ๊ตฌํ˜„ UDF๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. UDF(org.apache.hadoop.hive.ql.exec.UDF) ์ƒ์† evaluate() ํ•จ์ˆ˜ ๊ตฌํ˜„ GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) ์ƒ์† initialize(), evaluate(), getDisplayString() ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ UDF๋ฅผ ์ƒ์†ํ•˜๋ฉด ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ฒ˜๋ฆฌ๋˜๋Š” ๋ถ€๋ถ„๋งŒ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. GenericUDF๋Š” ๋ณตํ•ฉ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. GenericUDF๋ฅผ ์ƒ์†ํ•˜๋ฉด ๋ฐ์ดํ„ฐ์˜ ๊ฒ€์ฆ ๋ถ€๋ถ„๊ณผ ์‹ค์ œ ์ฒ˜๋ฆฌ ๋ถ€๋ถ„์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. UDF๋ณด๋‹ค๋Š” ๋ณต์žกํ•˜์ง€๋งŒ ์ƒ์„ธํ•œ ๊ตฌํ˜„์„ ํ•  ์ˆ˜ ์žˆ๊ณ , ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๊ฒ€์ฆ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. UDF ๊ตฌํ˜„ ์•„๋ž˜์˜ UDF๋Š” evalute() ํ•จ์ˆ˜๋ฅผ ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„ ๋กœ๋”ฉ์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ฌธ์žํ˜•์€ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ , intํ˜• ์ˆซ์ž๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ์ˆซ์ž์— 1์„ ๋”ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋งต ํ˜•์€ ํ‚ค๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•ด์„œ ํ‚ค๊ฐ€ ์žˆ์œผ๋ฉด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์—†์œผ๋ฉด None ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. import java.util.Map; import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.io.Text; public class SampleUDF extends UDF { public Text evaluate(Text text) { // ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜ return new Text(text.toString().toUpperCase()); } public int evaluate(int number) { // ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž์— 1์„ ๋”ํ•˜์—ฌ ๋ฐ˜ํ™˜ return number + 1; } public String evaluate(Map<String, String> map, String key) { // ์ž…๋ ฅ๋ฐ›์€ ํ‚ค์˜ ๋ฐธ๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉด ๋ฐ˜ํ™˜ํ•˜๊ณ , ์—†์œผ๋ฉด None๋ฅผ ๋ฐ˜ํ™˜ return map.containsKey(key) ? map.get(key) : "None"; } } ์‚ฌ์šฉ๋ฐฉ๋ฒ• -- UDF๊ฐ€ ํฌํ•จ๋œ jar ์ถ”๊ฐ€ ADD JAR hdfs:///user/hiveUDF.jar; CREATE FUNCTION func AS 'com.sec.hive.udf.GeneralUDF'; // intํ˜•์€ +1 hive> select func(1); OK Time taken: 0.816 seconds, Fetched: 1 row(s) -- ๋ฌธ์žํ˜•์€ ๋Œ€๋ฌธ์ž ๋ฐ˜ํ™˜ hive> select func('small'); OK SMALL Time taken: 0.032 seconds, Fetched: 1 row(s) -- ์ผ์น˜ํ•˜๋Š” ๊ฐ’์ด ์—†์œผ๋ฉด ์˜ค๋ฅ˜ hive> select func(array(1, 2, 3)); FAILED: SemanticException [Error 10014]: Line 1:7 Wrong arguments '3': No matching method for class com.sec.hive.udf.GeneralUDF with (array<int>). Possible choices: _FUNC_(int) _FUNC_(map<string, string>, string) _FUNC_(string) GenericUDF๋ฅผ ๊ตฌํ˜„ ๋‹ค์Œ UDF๋Š” ๋ฌธ์ž์—ด์ด ๋“ค์–ด ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋ฌธ์ž ๊ธธ์ด์˜ ์ดํ•ฉ์„ ์„ธ๋Š” UDF์ž…๋‹ˆ๋‹ค. initialize(), evalute(), getDisplayString() ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. import java.util.List; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.UDFArgumentException; import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException; import org.apache.hadoop.hive.ql.metadata.HiveException; import org.apache.hadoop.hive.ql.udf.generic.GenericUDF; import org.apache.hadoop.hive.serde2.lazy.LazyString; import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector; import org.apache.hadoop.io.IntWritable; @Description(name = "sumListStringLength", value = "_FUNC_(value) - Returns value that sum list string length.", extended = "Example:\n > SELECT _FUNC_(Array<String>) FROM table LIMIT 1;") public class ListGenericUDF extends GenericUDF { ListObjectInspector listOi; @Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { // initialize ํ•จ์ˆ˜์—์„œ๋Š” // ์ž…๋ ฅ๋ฐ›์€ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•œ ๊ฒ€์ฆ // ๋ฐ˜ํ™˜ํ•˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•œ ๊ฒ€์ฆ // ํ•จ์ˆ˜์— ์ž…๋ ฅ๋ฐ›๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐœ์ˆ˜ ํ™•์ธ if (arguments.length != 1) throw new UDFArgumentLengthException("function argument need 1."); // ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํƒ€์ž… ํ™•์ธ ObjectInspector inspector = arguments[0]; if (!(inspector instanceof ListObjectInspector)) throw new UDFArgumentException("function argument need List"); listOi = (ListObjectInspector) inspector; // ์ž…๋ ฅ๋ฐ›๋Š” ๋ฆฌ์ŠคํŠธ ๋‚ด ์—˜๋ฆฌ๋จผํŠธ์˜ ๊ฐ์ฒด ํƒ€์ž… ํ™•์ธ if (!(listOi.getListElementObjectInspector() instanceof StringObjectInspector)) throw new UDFArgumentException("array argument need "); // ๋ฐ˜ํ™˜์€ ๋ฌธ์ž์—ด์˜ ์ˆ˜์ด๋ฏ€๋กœ int ๊ฐ์ฒด ๋ฐ˜ํ™˜ return PrimitiveObjectInspectorFactory.writableIntObjectInspector; } @SuppressWarnings("unchecked") @Override public Object evaluate(DeferredObject[] arguments) throws HiveException { // arguments์˜ ๊ฐ์ฒด๋ฅผ ํ˜• ๋ณ€ํ™˜ List<LazyString> list = (List<LazyString>) listOi.getList(arguments[0].get()); if (list == null) return null; int sum = 0; for (LazyString str : list) { sum += str.getWritableObject().getLength(); } return new IntWritable(sum); } @Override public String getDisplayString(String[] children) { StringBuffer buffer = new StringBuffer(); buffer.append("sumListStringLength(Array<String>), "); for (String child : children) buffer.append(child).append(","); return buffer.toString(); } } ์‚ฌ์šฉ๋ฐฉ๋ฒ• ADD JAR hdfs:///user/hiveUDF.jar; CREATE FUNCTION listFunc AS 'com.sec.hive.udf.ListGenericUDF'; hive> select * from listTable; OK ["1","2","3"] ["4","5","6"] ["7","8","9"] ["abcdefg","alskdjfalskd","alksdfjalskdfj"] ["aslkdfjalskdf","asldkjfalskd","asldkfja"] ["asldkfjalskd","asdlkfjalskdjflaksd","asldkjfalsdkjflkasd","alsdkjfalkdjf"] -- col_list๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋”ํ•˜์—ฌ ๋ฐ˜ํ™˜ hive> select listFunc(col_list) > from listTable; OK 3 33 33 63 Time taken: 0.307 seconds, Fetched: 6 row(s) 2-UDAF ๊ตฌํ˜„ UDAF๋Š” AbstractGenericUDAFResolver๋ฅผ ์ƒ์†ํ•˜์—ฌ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. Resolver๋ฅผ ์ƒ์†ํ•˜์—ฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž… ์ฒดํฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•œ Evaluator ํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. Resolver ํด๋ž˜์Šค: ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž… ์ฒดํฌ ์˜คํผ๋ ˆ์ดํ„ฐ ๊ตฌํ˜„ ์‹ค์ œ ์ฒ˜๋ฆฌ ํ”„๋กœ์„ธ์Šค ๊ตฌํ˜„์ฒด(GenericUDAFEvaluator)๋ฅผ ๋ฐ˜ํ™˜ Evaluator ํด๋ž˜์Šค: init(), merge(), terminatePartial() ๋“ฑ์˜ ์‹ค์ œ ์ฒ˜๋ฆฌ ๊ตฌํ˜„ getNewAggregationBuffer() - ์ง‘๊ณ„์— ์‚ฌ์šฉํ•  AggregationBuffer ๋ฐ˜ํ™˜ reset - aggregation ์ด ์žฌ์‚ฌ์šฉ๋  ๋•Œ์˜ ์ฒ˜๋ฆฌ init - ์ž…๋ ฅ๋ฐ›๋Š” ์•„๊ทœ๋จผํŠธ์™€ ๋ฐ˜ํ™˜๊ฐ’์˜ ํƒ€์ž…์„ ์ง€์ • iterate - ๋งค ํผ๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๋™์•ˆ ๋ฐ˜๋ณตํ•˜๋Š” ์ž‘์—… terminatePartial - ๋ถ€๋ถ„์ ์œผ๋กœ ์ง‘๊ณ„ ์ž‘์—…์„ ์ข…๋ฅ˜ ํ•  ๋•Œ ์ž‘์—… merge - ์ง‘๊ณ„ ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ๋จธ์ง€ ํ•  ๋•Œ terminate - ์ž‘์—…์ด ์ข…๋ฃŒ๋  ๋•Œ ์‹ค์ œ ๋ฐ์ดํ„ฐ์˜ ์ฒ˜๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. init() ํ•จ์ˆ˜๋กœ ์ดˆ๊ธฐํ™” ํ›„ iterate() ํ•จ์ˆ˜๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋งคํผ์˜ ๋งˆ์ง€๋ง‰์— terminatePartial() ํ•จ์ˆ˜๋กœ ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๋ฆฌ๋“€์„œ์—์„œ ๋‹ค์‹œ merge()๋ฅผ ํ†ตํ•ด ๊ฐ ๋งคํผ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋จธ์ง€ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  tereminate()๋ฅผ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌํ˜„ ์•„๋ž˜๋Š” Sum ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; import org.apache.hadoop.hive.ql.metadata.HiveException; import org.apache.hadoop.hive.ql.parse.SemanticException; import org.apache.hadoop.hive.ql.udf.generic.AbstractGenericUDAFResolver; import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory; import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils; import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo; import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; /** * String, int๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ํ•ฉ๊ณ„๋ฅผ ๋ฐ˜ํ™˜ * * @author User * */ public class SumInt extends AbstractGenericUDAFResolver { @Override public GenericUDAFEvaluator getEvaluator(TypeInfo[] info) throws SemanticException { // ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ•˜๋‚˜๋งŒ ๋ฐ›์Œ if (info.length != 1) { throw new UDFArgumentTypeException(info.length - 1, "Exactly one argument is expected."); } // ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ ํƒ€์ž…์ด ์•„๋‹ˆ๋ฉด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ if (info[0].getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but " + info[0].getTypeName() + " was passed as parameter 1."); } // ์ „๋‹ฌ๋œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํƒ€์ž…์ด ์ŠคํŠธ๋ง์ด๋ฉด SumStringEvaluator, ์•„๋‹ˆ๋ฉด SumIntEvaluator ์ฒ˜๋ฆฌ PrimitiveCategory category = ((PrimitiveTypeInfo) info[0]).getPrimitiveCategory(); if (category == PrimitiveCategory.STRING || category == PrimitiveCategory.INT) { return new SumEvalutor(); } else { throw new UDFArgumentTypeException(0, "Only string, int type arguments are accepted but " + info[0].getTypeName() + " was passed as parameter 1."); } } @SuppressWarnings("deprecation") public static class SumEvalutor extends GenericUDAFEvaluator { protected PrimitiveObjectInspector inputOI; @Override public ObjectInspector init(Mode m, ObjectInspector[] parameters) throws HiveException { super.init(m, parameters); inputOI = (PrimitiveObjectInspector) parameters[0]; return PrimitiveObjectInspectorFactory.javaIntObjectInspector; } static class SumAggregationBuffer implements AggregationBuffer { int sum; } @Override public AggregationBuffer getNewAggregationBuffer() throws HiveException { SumAggregationBuffer sum = new SumAggregationBuffer(); sum.sum = 0; return sum; } @Override public void reset(AggregationBuffer agg) throws HiveException { ((SumAggregationBuffer) agg).sum = 0; } @Override public void iterate(AggregationBuffer agg, Object[] parameters) throws HiveException { ((SumAggregationBuffer) agg).sum += getInt(parameters[0]); } @Override public Object terminatePartial(AggregationBuffer agg) throws HiveException { return ((SumAggregationBuffer) agg).sum; } @Override public void merge(AggregationBuffer agg, Object partial) throws HiveException { ((SumAggregationBuffer) agg).sum += getInt(partial); } @Override public Object terminate(AggregationBuffer agg) throws HiveException { return ((SumAggregationBuffer) agg).sum; } public int getInt(Object strObject) { return PrimitiveObjectInspectorUtils.getInt(strObject, inputOI); } } } ์‚ฌ์šฉ๋ฐฉ๋ฒ• -- UDF๊ฐ€ ํฌํ•จ๋œ jar ์ถ”๊ฐ€ ADD JAR hdfs:///user/hiveUDF.jar; CREATE FUNCTION sumInt AS 'com.sec.hive.udf.SumInt'; -- ๋ฐ์ดํ„ฐ ํ™•์ธ hive> select * from intTable; OK 2 4 6 8 10 -- ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ํ™•์ธ select sumInt(col) from intTable; Query ID = hadoop_20181113081733_822f2f53-139c-419b-bb67-fb9e572994a4 Total jobs = 1 Launching Job 1 out of 1 OK 55 Time taken: 14.06 seconds, Fetched: 1 row(s) ์ฐธ๊ณ  hive RANK ํ•จ์ˆ˜ ๊ตฌํ˜„ 3-UDTF ๊ตฌํ˜„ UDTF๋Š” GenericUDTF๋ฅผ ์ƒ์†ํ•˜์—ฌ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. initialize() ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฒ€์ฆ๊ณผ ์นผ๋Ÿผ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ process() ์‹ค์ œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ forward() ํ•จ์ˆ˜์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊น€ close() ์ž์›์˜ ๋ฐ˜ํ™˜์„ ์ฒ˜๋ฆฌ ๊ตฌํ˜„ ๋‹ค์Œ์€ ๋”œ๋ฆฌ ๋ฏธํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•˜๊ณ  ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” UDTF ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. import java.util.ArrayList; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.UDFArgumentException; import org.apache.hadoop.hive.ql.metadata.HiveException; import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory; import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector; import org.apache.hadoop.io.Text; @Description(name = "string_parse", value = "_FUNC_(delimiter, string) - ") public class StringParseUDTF extends GenericUDTF { private transient final Object[] forwardListObj = new Object[1]; protected PrimitiveObjectInspector inputOI1; protected PrimitiveObjectInspector inputOI2; @Override public StructObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException { inputOI1 = (PrimitiveObjectInspector) argOIs[1]; inputOI2 = (PrimitiveObjectInspector) argOIs[1]; ArrayList<String> fieldNames = new ArrayList<String>(); ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>(); fieldNames.add("col"); fieldOIs.add(inputOI1); fieldOIs.add(inputOI2); return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs); } @Override public void process(Object[] o) throws HiveException { String delim = (String) inputOI1.getPrimitiveJavaObject(o[0]); String datas = (String) inputOI2.getPrimitiveJavaObject(o[1]); for(String str: datas.split(delim)) { forwardListObj[0] = new Text(str); forward(forwardListObj); } } @Override public void close() throws HiveException { } } ์‚ฌ์šฉ๋ฐฉ๋ฒ• -- UDF๊ฐ€ ํฌํ•จ๋œ JAR ์ถ”๊ฐ€ ๋ฐ ํ•จ์ˆ˜ ์ƒ์„ฑ ADD JAR hdfs:///user/hiveUDF.jar; CREATE TEMPORARY FUNCTION parseStr AS 'com.sec.hive.udf.StringParseUDTF'; hive> SELECT parseStr(",", "1,2,3"); OK 2 hive> SELECT parseStr("-", "a-b-c"); OK b ์ฐธ๊ณ  hive explode() ํ•จ์ˆ˜ ๊ตฌํ˜„ ํ•˜์ด๋ธŒ UDTF ๊ตฌํ˜„ ์œ„ํ‚ค 4-TRANSFORM ๊ตฌํ˜„ TRANSFORM ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ์ •ํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ ๋•Œ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์ •ํ˜• ๊ตฌ์กฐ๋กœ ๋ณ€๊ฒฝํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌํ˜„ํ•œ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ์™€ TRANSFORM ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์˜<NAME>์œผ๋กœ <์ž…๋ ฅ>๋˜๋Š” ๋กœ๊ทธ๋ฅผ <์ถœ๋ ฅ>์ฒ˜๋Ÿผ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. <์ž…๋ ฅ> DATA1 Column1-1 Column1-2 DATA2 Column2-1 Column2-2 <์ถœ๋ ฅ> DATA1 Column1-1 Column1-2 DATA2 Column2-1 Column2-2 ๊ตฌํ˜„ ์ž…๋ ฅ์„ ์ถœ๋ ฅ์˜ ๊ตฌ์กฐ์ ์ธ ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. #!/usr/bin/python # -*- coding: utf-8 -*- import re, json, sys, time def readFile(): with sys.stdin as lines: str_list = [] for line in lines: # DATA ์‹œ์ž‘ํ•˜๋ฉด ์ถœ๋ ฅ if line.startswith("DATA") and len(str_list) != 0: print "\t".join(str_list) del str_list[:] str_list.append(line.strip()) else: str_list.append(line.strip()) # ๋งˆ์ง€๋ง‰ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ print "\t".join(str_list) if __name__ == "__main__": readFile() ์‚ฌ์šฉ๋ฐฉ๋ฒ• -- ํ…Œ์ด๋ธ” ์ƒ์„ฑ CREATE EXTERNAL TABLE sample_temp ( rawLine STRING ) LOCATION "/user/data/txt/"; -- trsnsform(์ž…๋ ฅ ์นผ๋Ÿผ๋ช…) using ํŒŒ์ผ ์œ„์น˜ as (์ถœ๋ ฅ ์นผ๋Ÿผ) -- ์ด๋Ÿฐ ํ˜•ํƒœ๋กœ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. SELECT TRANSFORM(rawLine) USING "hdfs:///user/custom_mapred.py" AS (type, dt1, dt2) FROM sample_temp; Total MapReduce CPU Time Spent: 1 seconds 710 msec OK DATA1 Column1-1 Column1-2 DATA2 Column2-1 Column2-2 ์ฐธ๊ณ  Hive - LanguageManual Transform 3-Macro ํ•˜์ด๋ธŒ๋Š” ๋งคํฌ๋กœ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ’์ด๋‚˜ ๋ณตํ•ฉ ํ•จ์ˆ˜๋ฅผ ๋งคํฌ๋กœ๋กœ ์„ค์ •ํ•˜์—ฌ ํŽธ๋ฆฌํ•˜๊ฒŒ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ์„ฑ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์นผ๋Ÿผ ์ด๋ฆ„์„ ํ‘œํ˜„์‹์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋นŒํŠธ์ธ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งคํฌ๋กœ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งคํฌ๋กœ๋Š” ํ˜„์žฌ ์„ธ์…˜์—๋งŒ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. CREATE TEMPORARY MACRO macro_name([col_name col_type, ...]) expression; ๋งคํฌ๋กœ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. CREATE TEMPORARY MACRO fixed_number() 42; CREATE TEMPORARY MACRO string_len_plus_two(x string) length(x) + 2; CREATE TEMPORARY MACRO simple_add (x int, y int) x + y; CREATE TEMPORARY MACRO mfunc(a string, b string, c string) CONCAT(CONCAT_WS("/", a, b, c), "/"); hive> select fixed_number(); OK 42 hive> select string_len_plus_two("AB"); OK hive> select simple_add(10, 5); OK 15 hive> select mfunc('a', 'b', 'c'); OK a/b/c/ ์‚ญ์ œ ๋งคํฌ๋กœ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์—ฌ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. DROP TEMPORARY MACRO [IF EXISTS] macro_name; 5-๊ด€๋ฆฌ ํ•˜์ด๋ธŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ”, ํ•จ์ˆ˜ ๋“ฑ์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ desc, show ๋ช…๋ น์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1-DESCRIBE DESC๋Š” ๋ฉ”ํƒ€ ์Šคํ† ์–ด์— ์ •์˜๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„, ์†์„ฑ, ์„ค์ • ์ •๋ณด ๋“ฑ์„ ํ™•์ธํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. DESC๋ฅผ ์ด์šฉํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ชฉ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Database Table/View/Materialized View/Column Display Column Statistics Partition Describe Database DESCRIBE DATABASE๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„, ์ฃผ์„(์„ค์ •๋œ ๊ฒฝ์šฐ) ๋ฐ ํŒŒ์ผ ์‹œ์Šคํ…œ์˜ ๋ฃจํŠธ ์œ„์น˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. SCHEMAS์™€ DATABASES๋Š” ๋™์ผํ•œ ์˜๋ฏธ๋กœ ๋ฐ”๊พธ์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. EXTENDED๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์†์„ฑ๋„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. FORMATTED๋Š” ํฌ๋งท์— ๋งž๊ฒŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. DESCRIBE DATABASE [EXTENDED|FORMATTED] db_name; DESCRIBE SCHEMA [EXTENDED|FORMATTED] db_name; -- (Note: Hive 1.1.0 and later) Describe Table/View/Materialized View/Column DESCRIBE [EXTENDED|FORMATTED] table_name; Describe Partition DESCRIBE [EXTENDED|FORMATTED] table_name[.column_name] PARTITION partition_spec; hive> DESCRIBE formatted part_table partition (d='abc'); OK # col_name data_type comment i int # Partition Information # col_name data_type comment d string # Detailed Partition Information Partition Value: [abc] Database: default Table: part_table CreateTime: Wed Mar 30 16:57:14 PDT 2016 LastAccessTime: UNKNOWN Protect Mode: None Location: file:/tmp/warehouse/part_table/d=abc Partition Parameters: COLUMN_STATS_ACCURATE true numFiles 1 numRows 1 rawDataSize 1 totalSize 2 transient_lastDdlTime 1459382234 # Storage Information SerDe Library: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe InputFormat: org.apache.hadoop.mapred.TextInputFormat OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat Compressed: No Num Buckets: -1 Bucket Columns: [] Sort Columns: [] Storage Desc Params: serialization.format 1 Time taken: 0.334 seconds, Fetched: 35 row(s) 2-SHOW SHOW๋Š” ๋ฉ”ํƒ€ ์Šคํ† ์–ด์— ์ •์˜๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ”, ๋ทฐ, ํ…Œ์ด๋ธ” ํŒŒํ‹ฐ์…˜ ๋“ฑ์˜ ๋ชฉ๋ก์„ ํ™•์ธํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. LIKE๋ฅผ ์ด์šฉํ•˜๋ฉด ์ •๊ทœ์‹์˜ ์™€์ผ๋“œ์นด๋“œ(*)๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ชฉ๋ก์„ ํ•œ ๋ฒˆ์— ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SHOW๋ฅผ ์ด์šฉํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ชฉ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Databases Tables/Views/Materialized Views/Partitions/Indexes Tables Views Materialized Views Partitions Table/Partition Extended Table Properties Create Table Indexes Columns Functions Locks Conf Transactions Compactions Show Databases SHOW DATABASES ๋˜๋Š” SHOW SCHEMAS๋Š” ๋ฉ”ํƒ€ ์Šคํ† ์–ด์— ์ •์˜๋œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. SCHEMAS์™€ DATABASES๋Š” ๋™์ผํ•œ ์˜๋ฏธ๋กœ ๋ฐ”๊พธ์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. LIKE ์ ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ •๊ทœ์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชฉ๋ก์„ ํ•„ํ„ฐ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ์‹์˜ ์™€์ผ๋“œ์นด๋“œ(*)๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ชฉ๋ก์„ ํ•œ ๋ฒˆ์— ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 'employee', 'emp', 'emp|*ees'๋Š” ๋ชจ๋‘ 'employees'๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. SHOW (DATABASES|SCHEMAS) [LIKE 'identifier_with_wildcards']; Show Tables/Views/Materialized Views/Partitions/Indexes SHOW Tables/Views/Materialized Views/Partitions/Indexes๋Š” ํ˜„์žฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(๋˜๋Š” IN ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…์‹œ์ ์œผ๋กœ ๋ช…๋ช…๋œ)์˜ ๋ชจ๋“  ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”๊ณผ ๋ทฐ๋ฅผ ์„ ํƒ์  ์ •๊ทœ์‹๊ณผ ์ผ์น˜ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. Show Tables -- Show Tables SHOW TABLES [IN database_name] ['identifier_with_wildcards']; Show Views -- Show Views SHOW VIEWS [IN/FROM database_name] [LIKE 'pattern_with_wildcards']; SHOW VIEWS; -- show all views in the current database SHOW VIEWS 'test_*'; -- show all views that start with "test_" SHOW VIEWS '*view2'; -- show all views that end in "view2" SHOW VIEWS LIKE 'test_view1|test_view2'; -- show views named either "test_view1" or "test_view2" SHOW VIEWS FROM test1; -- show views from database test1 SHOW VIEWS IN test1; -- show views from database test1 (FROM and IN are same) SHOW VIEWS IN test1 "test_*"; -- show views from database test2 that start with "test_" Show Materialized Views -- Show Materialized Views SHOW MATERIALIZED VIEWS [IN/FROM database_name] [LIKE 'pattern_with_wildcardsโ€™]; Show Partitions -- Show Partitions SHOW PARTITIONS table_name; SHOW PARTITIONS [db_name.] table_name [PARTITION(partition_spec)]; -- (Note: Hive 0.13.0 and later) SHOW PARTITIONS table_name PARTITION(ds='2010-03-03'); -- (Note: Hive 0.6 and later) SHOW PARTITIONS table_name PARTITION(hr='12'); -- (Note: Hive 0.6 and later) SHOW PARTITIONS table_name PARTITION(ds='2010-03-03', hr='12'); -- (Note: Hive 0.6 and later) Show Table/Partition Extended -- Show Table/Partition Extended SHOW TABLE EXTENDED [IN|FROM database_name] LIKE 'identifier_with_wildcards' [PARTITION(partition_spec)]; Show Table Properties -- Show Table Properties SHOW TBLPROPERTIES tblname; SHOW TBLPROPERTIES tblname("foo"); Show Create Database SHOW CREATE DATABASE์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ ๋ช…๋ น์–ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -- Show Create Table SHOW CREATE DATABASE db_name; hive> SHOW CREATE DATABASE db_name; OK CREATE DATABASE `db_name` LOCATION 'hdfs://127.0.0.1:8020/hdfs' Show Create Table SHOW CREATE TABLE์€ ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๋ช…๋ น์–ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -- Show Create Table SHOW CREATE TABLE ([db_name.] table_name|view_name); Show Indexes -- Show Indexes SHOW [FORMATTED] (INDEX|INDEXES) ON table_with_index [(FROM|IN) db_name]; Show Columns -- Show Columns SHOW COLUMNS (FROM|IN) table_name [(FROM|IN) db_name]; -- SHOW COLUMNS CREATE DATABASE test_db; USE test_db; CREATE TABLE foo(col1 INT, col2 INT, col3 INT, cola INT, colb INT, colc INT, a INT, b INT, c INT); -- SHOW COLUMNS basic syntax SHOW COLUMNS FROM foo; -- show all column in foo SHOW COLUMNS FROM foo "*"; -- show all column in foo SHOW COLUMNS IN foo "col*"; -- show columns in foo starting with "col" OUTPUT col1, col2, col3, cola, colb, colc SHOW COLUMNS FROM foo '*c'; -- show columns in foo ending with "c" OUTPUT c, colc SHOW COLUMNS FROM foo LIKE "col1|cola"; -- show columns in foo either col1 or cola OUTPUT col1, cola SHOW COLUMNS FROM foo FROM test_db LIKE 'col*'; -- show columns in foo starting with "col" OUTPUT col1, col2, col3, cola, colb, colc SHOW COLUMNS IN foo IN test_db LIKE 'col*'; -- show columns in foo starting with "col" (FROM/IN same) OUTPUT col1, col2, col3, cola, colb, colc -- Non existing column pattern resulting in no match SHOW COLUMNS IN foo "nomatch*"; SHOW COLUMNS IN foo "col+"; -- + wildcard not supported SHOW COLUMNS IN foo "nomatch"; Show Functions SHOW FUNCTIONS๋Š” LIKE๋กœ ์ง€์ •๋œ ๊ฒฝ์šฐ ์ •๊ทœ์‹์œผ๋กœ ํ•„ํ„ฐ๋ง ๋œ ๋ชจ๋“  ์‚ฌ์šฉ์ž ์ •์˜ ๋ฐ ๋‚ด์žฅ ํ•จ์ˆ˜๋ฅผ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. SHOW FUNCTIONS [LIKE "<pattern>"]; Show Locks SHOW LOCKS๋Š” ํ…Œ์ด๋ธ” ๋˜๋Š” ํŒŒํ‹ฐ์…˜์˜ ์ž ๊ธˆ์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ž ๊ธˆ์— ๋Œ€ํ•œ ์ •๋ณด๋Š” Hive Concurrency Model์„ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. SHOW LOCKS <table_name>; SHOW LOCKS <table_name> EXTENDED; SHOW LOCKS <table_name> PARTITION (<partition_spec>); SHOW LOCKS <table_name> PARTITION (<partition_spec>) EXTENDED; SHOW LOCKS (DATABASE|SCHEMA) database_name; -- (Note: Hive 0.13.0 and later; SCHEMA added in Hive 0.14.0) Show Transactions ํŠธ๋žœ์žญ์…˜ ํ‘œ์‹œ๋Š” Hive ํŠธ๋žœ์žญ์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ ๊ด€๋ฆฌ์ž๊ฐ€ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜์—ฌ ์‹œ์Šคํ…œ์—์„œ ํ˜„์žฌ ์—ด๋ ค ์žˆ๊ณ  ์ค‘๋‹จ๋œ ๋ชจ๋“  ํŠธ๋žœ์žญ์…˜ ๋ชฉ๋ก์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. + transaction ID + transaction state + user who started the transaction + machine where the transaction was started + timestamp when the transaction was started (as of Hive 2.2.0) + timestamp for last heart beat (as of Hive 2.2.0 ) SHOW TRANSACTIONS; Show Compactions SHOW COMPACTIONS๋Š” ๋‹ค์Œ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜์—ฌ Hive ํŠธ๋žœ์žญ์…˜์ด ์‚ฌ์šฉ๋  ๋•Œ ํ˜„์žฌ ์••์ถ•๋˜๊ฑฐ๋‚˜ ์••์ถ•๋  ์˜ˆ์ •์ธ ๋ชจ๋“  ํ…Œ์ด๋ธ” ๋ฐ ํŒŒํ‹ฐ์…˜์˜ ๋ชฉ๋ก์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. SHOW COMPACTIONS; 6-ํŠธ๋žœ์žญ์…˜ ํ•˜์ด๋ธŒ๋Š” 0.13๋ฒ„์ „๋ถ€ํ„ฐ ํŠธ๋žœ์žญ์…˜์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ์ˆ˜์ •์ด ์—†๋Š” HDFS์˜ ํŠน์„ฑ์ƒ ๋ชจ๋“  ๊ธฐ๋Šฅ์ด ์™„๋ฒฝํ•˜๊ฒŒ ์ง€์›๋˜์ง€ ์•Š๊ณ  ๋‹ค์Œ์˜ ๊ธฐ๋Šฅ๋งŒ์„ ์ง€์›ํ•˜๊ณ , ๊ธฐ๋ณธ(default) ํŠธ๋žœ์žญ์…˜ ์„ค์ •์€ off๋กœ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ์„ธํ•œ ๋‚ด์šฉ์€ Hive Wiki์˜ ํ•˜์ด๋ธŒ ํŠธ๋žœ์žญ์…˜์„ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. BEGIN, COMMIT, ROLLBACK์€ ์•„์ง ์ง€์›ํ•˜์ง€ ์•Š์Œ, ํ˜„์žฌ๋Š” auto-commit๋งŒ ์ง€์› ORC ํŒŒ์ผ ํฌ๋งท, ๋ฒ„์ผ“ํŒ… ์„ค์ •์ด ๋œ ๋งค๋‹ˆ์ง€๋“œ ํ…Œ์ด๋ธ”์—์„œ๋งŒ ์ง€์› Non-ACID ์„ธ์…˜์—์„œ๋Š” ACID ํ…Œ์ด๋ธ”์— ์ ‘๊ทผ ๋ถˆ๊ฐ€ ํŠธ๋žœ์žญ์…˜์˜ ์ฒ˜๋ฆฌ ์ˆœ์„œ HDFS๋Š” ํŒŒ์ผ์˜ ๋ณ€๊ฒฝ/์ˆ˜์ •์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ HDFS์—์„œ ํŠธ๋žœ์žญ์…˜์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฒ ์ด์Šค(base) ํŒŒ์ผ์— ๊ธฐ๋กํ•˜๊ณ , ํŠธ๋žœ์žญ์…˜(์ƒ์„ฑ/์ˆ˜์ •/์‚ญ์ œ)์ด ๋ฐœ์ƒํ•  ๋•Œ๋งˆ๋‹ค ๋ธํƒ€(delta) ํŒŒ์ผ์— ๋‚ด์šฉ์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ ๋ฒ ์ด์Šค ํŒŒ์ผ์— ๋ธํƒ€ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ ์šฉํ•˜์—ฌ ์ˆ˜์ •๋œ ๋‚ด์šฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์ด๋‚˜ ํŒŒํ‹ฐ์…˜์€ ๋ฒ ์ด์Šค ํŒŒ์ผ์˜ ์ง‘ํ•ฉ์œผ๋กœ ์ €์žฅ insert, update, delete์— ๋Œ€ํ•ด์„œ๋Š” ๋ธํƒ€ ํŒŒ์ผ๋กœ ์ €์žฅ ์ฝ๋Š” ์‹œ์ ์— ๋ฒ ์ด์Šค ํŒŒ์ผ๊ณผ, ๋ธ ํ„ฐ ํŒŒ์ผ์„ ํ•ฉ์ณ์„œ ์ˆ˜์ •๋œ ๋‚ด์šฉ์„ ๋ฐ˜ํ™˜ ํŒŒ์ผ์‹œ์Šคํ…œ์— ์ €์žฅ๋œ ํŒŒ์ผ์„ ํ™•์ธํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. base_xxx ํŒŒ์ผ๊ณผ delta_xxx ํŒŒ์ผ์ด ํ•จ๊ป˜ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. hive> dfs -ls -R /user/hive/warehouse/t; drwxr-xr-x - ekoifman staff 0 2016-06-09 17:03 /user/hive/warehouse/t/base_0000022 -rw-r--r-- 1 ekoifman staff 602 2016-06-09 17:03 /user/hive/warehouse/t/base_0000022/bucket_00000 drwxr-xr-x - ekoifman staff 0 2016-06-09 17:06 /user/hive/warehouse/t/delta_0000023_0000023_0000 -rw-r--r-- 1 ekoifman staff 611 2016-06-09 17:06 /user/hive/warehouse/t/delta_0000023_0000023_0000/bucket_00000 drwxr-xr-x - ekoifman staff 0 2016-06-09 17:07 /user/hive/warehouse/t/delta_0000024_0000024_0000 -rw-r--r-- 1 ekoifman staff 610 2016-06-09 17:07 /user/hive/warehouse/t/delta_0000024_0000024_0000/bucket_00000 ์ปดํŒฉ์…˜ ์ปดํŒฉ์…˜์€ ๋ธํƒ€ ํŒŒ์ผ(delta_xxx)์„ ์ •๋ฆฌํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ํŠธ๋žœ์žญ์…˜์ด ๋งŽ์•„์ง€๋ฉด ๋ธํƒ€ ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๊ณ  ํŒŒ์ผ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ปค์ง€๋ฉด์„œ ๋„ค์ž„๋…ธ๋“œ์˜ ๊ด€๋ฆฌ ํฌ์ธํŠธ๊ฐ€ ๋Š˜์–ด๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€ํ•˜๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์„œ๋น„์Šค๋กœ ์ปดํŒฉ์…˜์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ๋ธํƒ€ ํŒŒ์ผ์ด ๋งŽ์•„์ง€๋ฉด, ๋งˆ์ด๋„ˆ ์ปดํŒฉ์…˜์ด ๋ฐœ์ƒํ•˜์—ฌ ๋ธํƒ€ ํŒŒ์ผ์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๊ณ , ๋ธํƒ€ ํŒŒ์ผ์ด ์ ์  ์ปค์ง€๋ฉด ๋ฉ”์ด์ € ์ปดํŒฉ์…˜์ด ๋ฐœ์ƒํ•˜์—ฌ ๋ฒ ์ด์Šค ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ปดํŒฉ์…˜์€ ํŠธ๋žœ์žญ์…˜์ด ๋ฐœ์ƒํ•  ๋•Œ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š๊ณ , ์ฃผ๊ธฐ์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜๋Š” ์ปดํŒฉ์…˜ ์Šค์ผ€์ค„์— ๋”ฐ๋ผ ๋งต๋ฆฌ๋“€์Šค ์žก์œผ๋Ÿฌ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ด๋„ˆ ์ปดํŒฉ์…˜(minor compaction) ๋ธํƒ€ ํŒŒ์ผ์„ ๋ชจ์•„์„œ ๋ฒ„์ผ“๋‹น ํ•˜๋‚˜์˜ ๋ธํƒ€ ํŒŒ์ผ๋กœ ๋‹ค์‹œ ์ƒ์„ฑ ๋ฉ”์ด์ € ์ปดํŒฉ์…˜(major compaction) ๋ฒ ์ด์Šค ํŒŒ์ผ๊ณผ ๋ธํƒ€ ํŒŒ์ผ์„ ์ƒˆ๋กœ์šด ๋ฒ ์ด์Šค ํŒŒ์ผ๋กœ ์ƒ์„ฑ ๋ฒ ์ด์Šค ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ ํŒŒํ‹ฐ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์€ t๋ผ๋Š” ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜์˜€์„ ๋•Œ HDFS๋ฅผ ํ™•์ธํ•˜๋ฉด ๊ตฌ์กฐ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. base_ ํŒŒ์ผ์ด ๊ธฐ๋ณธ ํŒŒ์ผ์ด๊ณ , delta_ ํŒŒ์ผ์ด ๋ธํƒ€ ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ๋•Œ ํ•˜์ด๋ธŒ๋Š” base_ ํŒŒ์ผ์˜ ๋‚ด์šฉ์— delta_ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ํ•ฉ์ณ์„œ ์ˆ˜์ •์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. hive> dfs -ls -R /user/hive/warehouse/t; drwxr-xr-x - ekoifman staff 0 2016-06-09 17:03 /user/hive/warehouse/t/base_0000022 -rw-r--r-- 1 ekoifman staff 602 2016-06-09 17:03 /user/hive/warehouse/t/base_0000022/bucket_00000 drwxr-xr-x - ekoifman staff 0 2016-06-09 17:06 /user/hive/warehouse/t/delta_0000023_0000023_0000 -rw-r--r-- 1 ekoifman staff 611 2016-06-09 17:06 /user/hive/warehouse/t/delta_0000023_0000023_0000/bucket_00000 drwxr-xr-x - ekoifman staff 0 2016-06-09 17:07 /user/hive/warehouse/t/delta_0000024_0000024_0000 -rw-r--r-- 1 ekoifman staff 610 2016-06-09 17:07 /user/hive/warehouse/t/delta_0000024_0000024_0000/bucket_00000 ํŠธ๋žœ์žญ์…˜, ์ปดํŒฉ์…˜ ์„ค์ • ํŠธ๋žœ์žญ์…˜์€ hive.txn.manager๊ณผ hive.support.concurrency๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. set hive.support.concurrency=true; set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager; ์ปดํŒฉ์…˜์€ hive.compactor.initiator.on๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. set hive.compactor.initiator.on=true; set hive.compactor.worker.threads=3; ConfigurationProperties-TransactionsandCompactor ํŠธ๋žœ์žญ์…˜ ํ…Œ์ด๋ธ” ์ƒ์„ฑ ํŠธ๋žœ์žญ์…˜ ํ…Œ์ด๋ธ”์€ ๋ฒ„์ผ“ํŒ…์„ ์„ค์ •ํ•ด์•ผ ํ•˜๊ณ , ํ…Œ์ด๋ธ” ์ €์žฅ ํƒ€์ž…์„ ORC๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ด๋ธ” ํ”„๋กœํผํ‹ฐ์— "transactional"="true"๋ฅผ ์„ค์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. CREATE TABLE table_name ( id int, name string ) CLUSTERED BY (id) INTO 2 BUCKETS STORED AS ORC TBLPROPERTIES ("transactional"="true", "compactor.mapreduce.map.memory.mb"="2048", -- specify compaction map job properties "compactorthreshold.hive.compactor.delta.num.threshold"="4", -- trigger minor compaction if there are more than 4 delta directories "compactorthreshold.hive.compactor.delta.pct.threshold"="0.5" -- trigger major compaction if the ratio of size of delta files to -- size of base files is greater than 50% ); ํ…Œ์ด๋ธ” ์ปดํŒฉ์…˜ ์„ค์ • ํ…Œ์ด๋ธ” ์ปดํŒฉ์…˜ ์„ค์ • ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ALTER TABLE table_name COMPACT 'minor' WITH OVERWRITE TBLPROPERTIES ("compactor.mapreduce.map.memory.mb"="3072"); -- specify compaction map job properties ALTER TABLE table_name COMPACT 'major' WITH OVERWRITE TBLPROPERTIES ("tblprops.orc.compress.size"="8192"); -- change any other Hive table properties ํŠธ๋žœ์žญ์…˜ ํ™•์ธ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ์ƒํ™ฉ์€ ๋‹ค์Œ์˜ ๋ช…๋ น์œผ๋กœ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. hive> show transactions; OK Transaction ID Transaction State Started Time Last Heart beat Time User Hostname 96 OPEN 1584422197000 1584422197000 hadoop home 1-๋ฝ(Lock) ํŠธ๋žœ์žญ์…˜๊ณผ ๋ฝ์€ ๋™์‹œ์„ฑ์„ ์ง€์›ํ•˜๋Š” ์žฅ์น˜์ž…๋‹ˆ๋‹ค. ํŠธ๋žœ์žญ์…˜์€ ์ž‘์—…์˜ ๋…ผ๋ฆฌ์ ์ธ ๋‹จ์œ„์ž…๋‹ˆ๋‹ค. ๋ฝ์€ ํŠธ๋žœ์žญ์…˜์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ํ…Œ์ด๋ธ”, ํŒŒํ‹ฐ์…˜์— ์ ‘๊ทผ์„ ์ œ์–ดํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฝ์˜ ์ข…๋ฅ˜: ๊ณต์œ  ์ž ๊ธˆ, ๋ฐฐํƒ€์  ์ž ๊ธˆ ๋ฝ์€ Shared(S), Exclusive(X)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฝ์€ ๊ณต์œ  ์ž ๊ธˆ(S)๊ณผ ๋ฐฐํƒ€์  ์ž ๊ธˆ(X)์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณต์œ  ์ž ๊ธˆ์€ ์ฝ๊ธฐ ์ž ๊ธˆ(Read Lock)์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์œผ๋ ค๊ณ  ํ•  ๋•Œ ๋‹ค๋ฅธ ๊ณต์œ  ์ž ๊ธˆ์€ ํ—ˆ์šฉ๋˜์ง€๋งŒ, ๋ฐฐํƒ€์  ์ž ๊ธˆ์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฐฐํƒ€์  ์ž ๊ธˆ์€ ์“ฐ๊ธฐ ์ž ๊ธˆ(Write Lock)์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝ(INSERT, UPDATE, DELETE) ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋ฐฐํƒ€์  ์ž ๊ธˆ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐํƒ€์  ์ž ๊ธˆ์ด ๊ฑธ๋ฆฌ๋ฉด ๊ณต์œ  ์ž ๊ธˆ, ๋ฐฐํƒ€์  ์ž ๊ธˆ์„ ์„ค์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ฝ์˜ ํš๋“ ๋ฝ์€ ๋…ผํŒŒํ‹ฐ์…˜ ํ…Œ์ด๋ธ”๊ณผ ํŒŒํ‹ฐ์…˜ ํ…Œ์ด๋ธ”์—์„œ ๋”ฐ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋…ผํŒŒํ‹ฐ์…˜ ํ…Œ์ด๋ธ”์€ ์ง๊ด€์ ์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์„ ์ฝ์„ ๋•Œ๋Š” S ์ž ๊ธˆ์„ ํš๋“ํ•˜๊ณ , ๋‹ค๋ฅธ ์ž‘์—…์—์„œ๋Š” X ์ž ๊ธˆ์„ ํš๋“ํ•ฉ๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜ ํ…Œ์ด๋ธ”์€ ์ฝ์„ ๋•Œ ํ…Œ์ด๋ธ”์— S ์ž ๊ธˆ, ํŒŒํ‹ฐ์…˜์— S ์ž ๊ธˆ์„ ํš๋“ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ž‘์—…์—์„œ๋Š” ํ…Œ์ด๋ธ”์— S ์ž ๊ธˆ, ํŒŒํ‹ฐ์…˜์— X ์ž ๊ธˆ์„ ํš๋“ํ•ฉ๋‹ˆ๋‹ค. ๋ฝ์„ ํš๋“ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Hive Command Locks Acquired select .. T1 partition P1 S on T1, T1.P1 insert into T2(partition P2) select .. T1 partition P1 S on T2, T1, T1.P1 and X on T2.P2 insert into T2(partition P.Q) select .. T1 partition P1 S on T2, T2.P, T1, T1.P1 and X on T2.P.Q alter table T1 rename T2 X on T1 alter table T1 add cols X on T1 alter table T1 replace cols X on T1 alter table T1 change cols X on T1 alter table T1 concatenate X on T1 alter table T1 add partition P1 S on T1, X on T1.P1 alter table T1 drop partition P1 S on T1, X on T1.P1 alter table T1 touch partition P1 S on T1, X on T1.P1 alter table T1 set serdeproperties S on T1 alter table T1 set serializer S on T1 alter table T1 set file format S on T1 alter table T1 set tblproperties X on T1 alter table T1 partition P1 concatenate X on T1.P1 drop table T1 X on T1 ๋ฝ ํ™•์ธ ๋ฝ์„ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. SHOW LOCKS <TABLE_NAME>; SHOW LOCKS <TABLE_NAME> EXTENDED; SHOW LOCKS <TABLE_NAME> PARTITION (<PARTITION_DESC>); SHOW LOCKS <TABLE_NAME> PARTITION (<PARTITION_DESC>) EXTENDED; ํ•˜์ด๋ธŒ์—์„œ ๋ฝ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. hive (default)> show locks; OK Lock ID Database Table Partition State Blocked By Type Transaction ID Last Heart beat Acquired At UseHostname Agent Info 406.1 default table_name NULL ACQUIRED SHARED_READ 96 0 1584422197000 hadoop home hadoop_20200317051637_e6f8965b-eb5d-4281-b60a-8dc7f499c7d5 Time taken: 0.014 seconds, Fetched: 2 row(s) ์ฐธ๊ณ  Hive-Locking 7-์„ฑ๋Šฅ ์ตœ์ ํ™” ์ž‘์—… ์—”์ง„ ์„ ํƒ: TEZ ์—”์ง„ ์‚ฌ์šฉ ํŒŒ์ผ ์ €์žฅ ํฌ๋งท: ORC ํŒŒ์ผ ์‚ฌ์šฉ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐฉ์‹: ๋ฒกํ„ฐํ™”(Vectorization) ์‚ฌ์šฉ ๋ฐ์ดํ„ฐ ์ €์žฅ ํšจ์œจํ™”: ํŒŒํ‹ฐ์…”๋‹, ๋ฒ„์ผ“ํŒ… ์‚ฌ์šฉ ํ†ต๊ณ„์ •๋ณด ์ด์šฉ: ํ•˜์ด๋ธŒ stat ์‚ฌ์šฉ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ด์šฉ: CBO YARN: ์ž‘์—… ํ ์„ค์ • ํ•˜์ด๋ธŒ์˜ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž‘์—… ์—”์ง„ ์„ ํƒ: TEZ ์—”์ง„ ์‚ฌ์šฉ ๋งต๋ฆฌ๋“€์Šค(MR) ์—”์ง„์€ ์—ฐ์‚ฐ์˜ ์ค‘๊ฐ„ ํŒŒ์ผ์„ ๋กœ์ปฌ ๋””์Šคํฌ์— ์“ฐ๋ฉด์„œ ์ง„ํ–‰ํ•˜์—ฌ ์ด๋กœ ์ธํ•œ ์žฆ์€ IO ์ฒ˜๋ฆฌ๋กœ ์ž‘์—…์ด ๋Š๋ ค์ง‘๋‹ˆ๋‹ค. ํ…Œ์ฆˆ(TEZ) ์—”์ง„์€ ์ž‘์—… ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜์—ฌ ๋งต๋ฆฌ๋“€์Šค๋ณด๋‹ค ๋น ๋ฅธ ์†๋„๋กœ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. set hive.execution.engine=tez; ํŒŒ์ผ ์ €์žฅ ํฌ๋งท: ORC ํŒŒ์ผ ์‚ฌ์šฉ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ์— ORC ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ORC ํŒŒ์ผ ํฌ๋งท์€ ๋ฐ์ดํ„ฐ๋ฅผ ์นผ๋Ÿผ ๋‹จ์œ„๋กœ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ ์†๋„๊ฐ€ ๋น ๋ฅด๊ณ , ์••์ถ•๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค. CREATE TABLE table1 ( ) STORED AS ORC; ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐฉ์‹: ๋ฒกํ„ฐํ™”(Vectorization) ์‚ฌ์šฉ ๋ฒกํ„ฐํ™” ์ฒ˜๋ฆฌ๋Š” ํ•œ ๋ฒˆ์— 1ํ–‰์„ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ , ํ•œ ๋ฒˆ์— 1024ํ–‰์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ์†๋„๋ฅผ ๋†’์ด๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. ORC ํŒŒ์ผ ํฌ๋งท์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ•„ํ„ฐ๋ง, ์กฐ์ธ, ์ง‘ํ•ฉ ์—ฐ์‚ฐ์—์„œ 40~50% ์ •๋„์˜ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. set hive.vectorized.execution.enabled=true; ๋ฐ์ดํ„ฐ ์ €์žฅ ํšจ์œจํ™”: ํŒŒํ‹ฐ์…”๋‹, ๋ฒ„์ผ“ํŒ… ์‚ฌ์šฉ ํ•˜์ด๋ธŒ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰์— ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•œ ๋ฐฉ์•ˆ์œผ๋กœ ํŒŒํ‹ฐ์…”๋‹, ๋ฒ„์ผ“ํŒ… ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…”๋‹์€ ๋ฐ์ดํ„ฐ๋ฅผ ํด๋” ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ €์žฅํ•˜๊ณ , ๋ฒ„์ผ“ํŒ…์€ ์ง€์ •ํ•œ ๊ฐœ์ˆ˜์˜ ํŒŒ์ผ์— ์นผ๋Ÿผ์˜ ํ•ด์‹œ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•œ ๋ฒˆ์— ์ฝ์„ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CREATE TABLE table1 ( ) PARTITIONED BY(part_col STRING); ํ†ต๊ณ„์ •๋ณด ์ด์šฉ: ํ•˜์ด๋ธŒ stat ์‚ฌ์šฉ ํ•˜์ด๋ธŒ๋Š” ํ…Œ์ด๋ธ”, ํŒŒํ‹ฐ์…˜์˜ ์ •๋ณด๋ฅผ ๋ฉ”ํƒ€ ์Šคํ† ์–ด์— ์ €์žฅํ•˜๊ณ  ์กฐํšŒ๋‚˜ count, sum ๊ฐ™์€ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์ด ์ •๋ณด๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ์—ฐ์‚ฐ ์—†์ด ๋ฐ”๋กœ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์—…์˜ ์†๋„๊ฐ€ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. set hive.stats.autogather=true; ์˜ตํ‹ฐ๋งˆ์ด์ € ์ด์šฉ: CBO ํ•˜์ด๋ธŒ๋Š” ์นดํƒˆ๋ฆฌ์ŠคํŠธ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ด์šฉํ•˜์—ฌ ํšจ์œจ์ ์œผ๋กœ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. explain์„ ์ด์šฉํ•˜์—ฌ ์ž‘์—… ๋ถ„์„ ์ƒํƒœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. set hive.cbo.enable=true; hive> explain select A from ta, tb where ta.id = tb.id; YARN: ์ž‘์—… ํ ์„ค์ • YARN์˜ ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ •์„ ํ†ตํ•ด ์ž‘์—…์˜ ํšจ์œจ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž‘์—…์˜ ์„ฑ๊ฒฉ์— ๋”ฐ๋ผ ํ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค์–ด์„œ ์Šค์ผ€์ค„๋Ÿฌ์˜ ์‚ฌ์šฉ ์„ค์ •์„ ์ ์šฉํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ํ์— ๋ชจ๋“  ์ž‘์—…์„ ๋„ฃ์ง€ ์•Š๊ณ , batch, adhoc ๊ฐ™์€ ํ˜•ํƒœ๋กœ ํ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ์˜ ์ตœ๋Œ€ ์‚ฌ์šฉ๋Ÿ‰ ์„ค์ •์„ ํ†ตํ•ด ์ ์ ˆํ•˜๊ฒŒ ์ž‘์—…์„ ๋ถ„์‚ฐํ•˜์—ฌ ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. 1-TEZ ํ…Œ์ฆˆ(TEZ)๋Š” YARN ๊ธฐ๋ฐ˜์˜ ๋น„๋™๊ธฐ ์‚ฌ์ดํด ๊ทธ๋ž˜ํ”„ ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ์—์„œ ๋งต๋ฆฌ๋“€์Šค ๋Œ€์‹  ์‹คํ–‰ ์—”์ง„์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค๋Š” ๋งต ๋‹จ๊ณ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ ์ฒ˜๋ฆฌํ•˜๊ณ , ๋ฆฌ๋“€์Šค ๋‹จ๊ณ„์—์„œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ž‘์—…์ด ์—ฌ๋Ÿฌ ๋‹จ๊ณ„์˜ ๋งต, ๋ฆฌ๋“€์Šค๋ฅผ ๊ฑฐ์น˜๊ฒŒ ๋˜๋ฉด ์ค‘๊ฐ„ ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ HDFS์— ์“ฐ๊ณ , ๋‹ค์‹œ ๋งต ๋‹จ๊ณ„์—์„œ ํŒŒ์ผ์„ ์ฝ์–ด์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ž‘์—… ์ค‘๊ฐ„ ์ž„์‹œ ๋ฐ์ดํ„ฐ๋„ ๋””์Šคํฌ์— ์“ฐ๊ฒŒ ๋˜์–ด IO ์ž‘์—…์œผ๋กœ ์ธํ•œ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋งŽ์•˜์Šต๋‹ˆ๋‹ค. ํ…Œ์ฆˆ๋Š” ๋งต ๋‹จ๊ณ„ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๊ณ , ์ด๋ฅผ ๋ฆฌ๋“€์Šค ๋‹จ๊ณ„๋กœ ๋ฐ”๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋“€์Šค ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ๋งต ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ๋ฆฌ๋“€์Šค ๋‹จ๊ณ„๋กœ ์ „๋‹ฌํ•˜์—ฌ IO ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์—ฌ์„œ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค vs ํ…Œ์ฆˆ AWS EMR์„ ์ด์šฉํ•œ ํ…Œ์ŠคํŠธ์—์„œ ๋™์ผํ•œ ์ž‘์—…์—์„œ ๋งต๋ฆฌ๋“€์Šค๋Š” ์•ฝ 50์ดˆ, ํ…Œ์ฆˆ๋Š” 30์ดˆ๋กœ 40% ์ •๋„ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…: 49.699์ดˆ Starting Job = job_1464200677872_0002, Tracking URL = http://ec2-host:20888/proxy/application_1464200677872_0002/ Kill Command = /usr/lib/hadoop/bin/hadoop job -kill job_1464200677872_0002 Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1 2016-05-27 04:53:11,258 Stage-1 map = 0%, reduce = 0% 2016-05-27 04:53:25,820 Stage-1 map = 13%, reduce = 0%, Cumulative CPU 10.45 sec 2016-05-27 04:53:32,034 Stage-1 map = 33%, reduce = 0%, Cumulative CPU 16.06 sec 2016-05-27 04:53:35,139 Stage-1 map = 40%, reduce = 0%, Cumulative CPU 18.9 sec 2016-05-27 04:53:37,211 Stage-1 map = 53%, reduce = 0%, Cumulative CPU 21.6 sec 2016-05-27 04:53:41,371 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 25.08 sec 2016-05-27 04:53:49,675 Stage-1 map = 100%, reduce = 100%, Cumulative CPU 29.93 sec MapReduce Total cumulative CPU time: 29 seconds 930 msec Ended Job = job_1464200677872_0002 Moving data to: s3://myBucket/mr-test/os_requests MapReduce Jobs Launched: Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 29.93 sec HDFS Read: 599 HDFS Write: 0 SUCCESS Total MapReduce CPU Time Spent: 29 seconds 930 msec OK Time taken: 49.699 seconds ํ…Œ์ฆˆ ์ž‘์—…: 30.711์ดˆ Time taken: 0.517 seconds Query ID = hadoop_20160527050505_dcdc075f-8338-4041-adc3-d2ffe69dfcdd Total jobs = 1 Launching Job 1 out of 1 Status: Running (Executing on YARN cluster with App id application_1464200677872_0003) -------------------------------------------------------------------------------- VERTICES STATUS TOTAL COMPLETED RUNNING PENDING FAILED KILLED -------------------------------------------------------------------------------- Map 1 .......... SUCCEEDED 1 1 0 0 0 0 Reducer 2 ...... SUCCEEDED 1 1 0 0 0 0 -------------------------------------------------------------------------------- VERTICES: 02/02 [==========================>>] 100% ELAPSED TIME: 27.61 s -------------------------------------------------------------------------------- Moving data to: s3://myBucket/tez-test/os_requests OK Time taken: 30.711 seconds ์‹คํ–‰ ์—”์ง„ ์„ค์ • ํ•˜์ด๋ธŒ์—์„œ ํ…Œ์ฆˆ ์—”์ง„์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- ํ…Œ์ฆˆ ์—”์ง„ ์„ค์ • set hive.execution.engine=tez; set tez.queue.name=tez_queue_name; -- ๋งต๋ฆฌ๋“€์Šค ์—”์ง„ ์„ค์ • set hive.execution.engine=mr; set mapred.job.queue.name=mr_queue_name; 1-TEZ ์ž‘์—… ์ตœ์ ํ™” TEZ๋กœ ์ž‘์—…์„ ์ง„ํ–‰ํ•  ๋•Œ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. Tez ๋กœ๊ทธ ์„ฑ๊ณต/์‹คํŒจ ํ™•์ธ Tez์˜ ๋กœ๊ทธ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์„ฑ๊ณต, ์‹คํŒจ ํ™•์ธ์„ ํ†ตํ•ด ์ž‘์—…์˜ ํšจ์œจ์ ์ธ ์ฒ˜๋ฆฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A(+B, -C)/D A๋Š” ์„ฑ๊ณตํ•œ ์ž‘์—…์˜ ์ˆ˜ B๋Š” ์‹คํ–‰ ์ค‘์ธ ์ž‘์—…์˜ ์ˆ˜ C๋Š” ์‹คํŒจ ํ•œ ์ž‘์—…์˜ ์ˆ˜ D๋Š” ์ „์ œ ์ฐฉ์—…์˜ ์ˆ˜ Map 1: 0(+1)/1 Map 4: 1/1 Reducer 2: 0/1 Reducer 3: 0/1 ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๋Š” ์ฟผ๋ฆฌ๋ฅผ ๋ถ„์„ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ฟผ๋ฆฌ๋ฅผ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด ์ฟผ๋ฆฌ์— ์‚ฌ์šฉ๋˜๋Š” ํ…Œ์ด๋ธ”์˜ ์ •๋ณด๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ์Šคํ† ์–ด์— ์ €์žฅ๋œ ํ†ต๊ณ„์ •๋ณด์™€ ORC ๊ฐ™์€ ํŒŒ์ผ์˜ ํ’‹ํ„ฐ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฟผ๋ฆฌ์— ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ๋‹ค๋ฉด ํฐ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. set tez.am.resource.memory.mb=2048; set tez.am.launch.cmd-opts=-Xmx1800m; ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • ์ปจํ…Œ์ด๋„ˆ๋Š” ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์‚ฌ์ด์ฆˆ์— ๋งž๊ฒŒ ์ ๋‹นํ•œ ํฌ๊ธฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํฌ๋ฉด ์ƒ์„ฑ๋˜๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ž‘์•„์ง€๊ณ , ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ž‘์œผ๋ฉด ์ปจํ…Œ์ด๋„ˆ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์ง‘๋‹ˆ๋‹ค. ๊ฐ ์ปจํ…Œ์ด๋„ˆ์˜ ํžˆํ”„ ์‚ฌ์ด์ฆˆ์— ๋”ฐ๋ผ GC ๋ฐœ์ƒ ์—ฌ๋ถ€๊ฐ€ ๊ฒฐ์ •๋˜๊ณ , ์ด๋กœ ์ธํ•œ ์ž‘์—…์˜ ์ง€์—ฐ์ด ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. set hive.tez.container.size=2048; set hive.tez.java.opts=-Xmx1800m; ์Šคํ”Œ๋ฆฟ ์‚ฌ์ด์ฆˆ - ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๋Š” ์ฟผ๋ฆฌ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋งค ํผ์™€ ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ฒฝ์šฐ ๋งคํผ 1์€ 44๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ•„์š”๋กœ ํ•˜๊ณ , ๋ฆฌ๋“€์„œ2์™€ 3์€ 12๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ทธ๋ฃนํ•‘ ์‚ฌ์ด์ฆˆ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ ํŒŒ์ผ์˜ ์‚ฌ์ด์ฆˆ์™€ ํŒŒ์ผ ๊ฐœ์ˆ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ๊ฐ€ ๊ณ„์‚ฐํ•˜์—ฌ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. -- ๋งคํผ, ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜ Map 1: 44/44 Map 4: 1/1 Reducer 2: 0(+12)/12 Reducer 3: 12/12 -- ๋งคํผ ๊ฐœ์ˆ˜ ์„ค์ • tez.grouping.max-size tez.grouping.min-size -- ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜ ์„ค์ • hive.exec.reducers.bytes.per.reducer GC ํ™•์ธ ์ž‘์—…์— ์‚ฌ์šฉ๋œ ๋ฆฌ์†Œ์Šค ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งˆ์ง€๋ง‰์— ๋ณด๊ณ ์„œ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. hive.tez.exec.print.summary ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋งต ์กฐ์ธ ๋งต ์กฐ์ธ์„ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ…Œ์ด๋ธ”์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ง€์ •ํ•œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์‚ฌ์ด์ฆˆ๋ณด๋‹ค ์ž‘์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. set hive.auto.convert.join.noconditionaltask.size=10000000; ํŒŒํ‹ฐ์…˜ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์ž…๋ ฅ์œผ๋กœ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•  ๋•Œ ๋งŽ์€ ์ž‘์—…์ด ํ•˜๋‚˜์˜ ํŒŒํ‹ฐ์…˜์— ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•˜๋ฉด ์ž‘์—…์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” hive.optimize.sort.dynamic.partition ์˜ต์…˜์„ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ ์€ ์ˆ˜์˜ ํŒŒํ‹ฐ์…˜์—๋Š” ์ž‘์—…์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 10๊ฐœ ์ด์ƒ์˜ ํŒŒํ‹ฐ์…˜์ผ ๋•Œ true๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ํ†ต๊ณ„ ์ •๋ณด ํ•˜์ด๋ธŒ CBO๋Š” ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ†ต๊ณ„์ •๋ณด๋Š” ํ•˜์ด๋ธŒ ๋ฉ”ํƒ€ ์Šคํ† ์–ด DB์— ์ €์žฅ๋˜๊ณ , ์ด ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ์œˆ๋„ ํ•จ์ˆ˜ ์ด์šฉ ์‹œ ๋น ๋ฅธ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  Tez Internals #1 โ€“ Number of Map Tasks Understanding Considerations to Move Existing MapReduce Jobs in Hive to Tez 2-๊ทธ๋ฃนํ•‘ ์„ค์ • ์ตœ์ ํ™” ํŒŒ์ผ ๊ฐœ์ˆ˜์™€ ์‚ฌ์ด์ฆˆ์— ๋”ฐ๋ฅธ ๊ทธ๋ฃนํ•‘ ์‚ฌ์ด์ฆˆ ์„ค์ •๊ณผ ์ฒ˜๋ฆฌ ์†๋„ ์ฐจ์ด ํ•˜์ด๋ธŒ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜์™€ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ์ฟผ๋ฆฌ์—์„œ๋„ ์„ค์ •์— ๋”ฐ๋ผ ์†๋„ ์ฐจ์ด๊ฐ€ ๋งŽ์ด ๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SELECT A, count(*) FROM tbl GROUP BY A ORDER BY A; ์นผ๋Ÿผ A๋กœ ํŒŒํ‹ฐ์…”๋‹ ํŒŒํ‹ฐ์…˜๋‹น 60 ์—ฌ๊ฐœ์˜ ORC ํŒŒ์ผ ํŒŒ์ผ๋‹น 10~20KB ์ด ์ƒํ™ฉ์—์„œ TEZ ์—”์ง„ ๊ธฐ๋ณธ ์„ค์ •์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด 2148์ดˆ๊ฐ€ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. ํŒŒ์ผ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ž‘์•„์„œ ๋งค ํผ๊ฐ€ ์ž‘๊ฒŒ ์ƒ์„ฑ๋˜์ง€๋งŒ, ORC ํŒŒ์ผ์˜ ํŠน์„ฑ์ƒ ํ•˜๋‚˜์˜ ํŒŒ์ผ์— ๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๊ณ , ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์†Œ๋ชจ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ TEZ ์—”์ง„์˜ ๊ทธ๋ฃนํ•‘ ์‚ฌ์ด์ฆˆ๋ฅผ ์กฐ์ ˆํ•˜์—ฌ ๋งคํผ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ ค์„œ, ํ•˜๋‚˜์˜ ๋งค ํผ๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ค„์ด๋ฉด ์†๋„๊ฐ€ ๋นจ๋ผ์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ค์ • ๋ณ€๊ฒฝ์„ ํ†ตํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹œ๊ฐ„์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์„ค์ • ํฌ๊ธฐ ๋งคํผ๊ฐœ์ˆ˜ ์‹œ๊ฐ„ tez.grouping.max-size 1G 3 2148.009 tez.grouping.max-size 25600 8671 139.95 tez.grouping.max-size 256000 1231 51.30 TEZ ์„ค์ • ๋ณ€๊ฒฝ set tez.grouping.max-size=256000; set tez.grouping.min-size=128000; 2-ORC ORC(Optimized Row Columnar)๋Š” ์นผ๋Ÿผ ๊ธฐ๋ฐ˜์˜ ํŒŒ์ผ ์ €์žฅ ๋ฐฉ์‹์œผ๋กœ, Hadoop, Hive, Pig, Spark ๋“ฑ์— ์ ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ๋•Œ ๋ณดํ†ต ์นผ๋Ÿผ ๋‹จ์œ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ORC๋Š” ์นผ๋Ÿผ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์นผ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ๋น ๋ฅด๊ณ , ์••์ถ•ํšจ์œจ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ORC ์„ค์ • ํ•˜์ด๋ธŒ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด STORED AS๋ฅผ ORC๋กœ ์„ ์–ธํ•˜๊ณ , TBLPROPERTIES์— ์„ค์ • ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. CREATE TABLE table1 ( col1 string, col2 string ) STORED AS ORC TBLPROPERTIES ( "orc.compress"="ZLIB", "orc.compress.size"="262144", "orc.create.index"="true", "orc.stripe.size"="268435456", "orc.row.index.stride"="3000", "orc.bloom.filter.columns"="col1, col2" ); ORC ์„ค์ •๊ฐ’ orc.compress ๊ธฐ๋ณธ๊ฐ’: ZLIB ์••์ถ•๋ฐฉ์‹ ์„ค์ • (one of NONE, ZLIB, SNAPPY) orc.compress.size ๊ธฐ๋ณธ๊ฐ’: 262,144 ์••์ถ•์„ ์ฒ˜๋ฆฌํ•  ์ฒญํฌ ์‚ฌ์ด์ฆˆ ์„ค์ •(256 * 1024 = 262,144) orc.create.index ๊ธฐ๋ณธ๊ฐ’: true ์ธ๋ฑ์Šค ์‚ฌ์šฉ ์—ฌ๋ถ€ orc.row.index.stride ๊ธฐ๋ณธ๊ฐ’: 10,000 ์„ค์ • row ์ด์ƒ์ผ ๋•Œ ์ธ๋ฑ์Šค ์ƒ์„ฑ (must be >= 1000) orc.stripe.size ๊ธฐ๋ณธ๊ฐ’: 67,108,864 ์ŠคํŠธ๋ผ์ดํ”„๋ฅผ ์ƒ์„ฑํ•  ์‚ฌ์ด์ฆˆ (64 * 1024 *1024 = 67,108,864)), ์„ค์ • ์‚ฌ์ด์ฆˆ๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ์ƒ์„ฑ orc.bloom.filter.columns ๊ธฐ๋ณธ๊ฐ’: "" ๋ธ”๋ฃธ ํ•„ํ„ฐ 1์„ ์ƒ์„ฑํ•  ์นผ๋Ÿผ ์ •๋ณด, ์ฝค๋งˆ(,)๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅ orc.bloom.filter.fpp ๊ธฐ๋ณธ๊ฐ’: 0.05 ๋ธ”๋ฃธ ํ•„ํ„ฐ์˜ ์˜คํŒ ํ™•๋ฅ (fpp=false positive portability) ์„ค์ • (must >0.0 and <1.0) ์›์†Œ๊ฐ€ ์ง‘ํ•ฉ์— ์†ํ•˜๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ํ™•๋ฅ ์  ์ž๋ฃŒ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๋ธ”๋ฃธ ํ•„ํ„ฐ์— ์˜ํ•ด ์–ด๋–ค ์›์†Œ๊ฐ€ ์ง‘ํ•ฉ์— ์†ํ•œ๋‹ค๊ณ  ํŒ๋‹จ๋œ ๊ฒฝ์šฐ ์‹ค์ œ๋กœ๋Š” ์›์†Œ๊ฐ€ ์ง‘ํ•ฉ์— ์†ํ•˜์ง€ ์•Š๋Š” ๊ธ์ • ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋ฐ˜๋Œ€๋กœ ์›์†Œ๊ฐ€ ์ง‘ํ•ฉ์— ์†ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋‹จ๋˜์—ˆ๋Š”๋ฐ ์‹ค์ œ๋กœ๋Š” ์›์†Œ๊ฐ€ ์ง‘ํ•ฉ์— ์†ํ•˜๋Š” ๋ถ€์ • ์˜ค๋ฅ˜๋Š” ์ ˆ๋Œ€๋กœ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ํŠน์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. โ†ฉ 3-CBO ํ•˜์ด๋ธŒ 0.14 ๋ฒ„์ „๋ถ€ํ„ฐ ์‚ฌ์šฉ์ž์˜ ์ฟผ๋ฆฌ๋ฅผ ์ตœ์ ํ™”ํ•˜๋Š” CBO๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. CBO๊ฐ€ ์ ์šฉ๋˜๋ฉด ์‚ฌ์šฉ์ž์˜ ์ฟผ๋ฆฌ๋ฅผ ๋ถ„์„ํ•ด์„œ ์ฟผ๋ฆฌ๋ฅผ ์ตœ์ ํ™”ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ…Œ์ด๋ธ” A, B,์˜ ์กฐ์ธ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์กฐ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๊ตํ•˜์—ฌ ์…”ํ”Œ ๋‹จ๊ณ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์ค๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฟผ๋ฆฌ์—์„œ t1, t2๋ฅผ ๋ชจ๋‘ ์ฝ์–ด์„œ ์กฐ์ธํ•˜์ง€ ์•Š๊ณ , t2๋ฅผ ์ฝ์„ ๋•Œ ๋จผ์ € ํ•„ํ„ฐ๋ง์„ ์ฒ˜๋ฆฌํ•œ ๋’ค ์กฐ์ธํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ตœ์ ํ™”ํ•˜์—ฌ ์ค๋‹ˆ๋‹ค. t2์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์ด๋™ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ค„์–ด๋“ค์–ด์„œ ์ฒ˜๋ฆฌ ๋น„์šฉ์ด ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค. SELECT sum(v) FROM ( SELECT t1.id, t1.value AS v FROM t1 JOIN t2 WHERE t1.id = t2.id AND t2.id > 50000) inner ) outer CBO ์˜ต์…˜ CBO๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ์˜ ์˜ต์…˜์„ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ true ์ƒํƒœ์ž…๋‹ˆ๋‹ค. -- CBO ์ ์šฉ set hive.cbo.enable=true; -- ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๋Š” ํ…Œ์ด๋ธ”๊ณผ INSERT ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ ์ž๋™์œผ๋กœ ํ†ต๊ณ„์ •๋ณด ์ˆ˜์ง‘ set hive.stats.autogather=true; set hive.stats.fetch.column.stats=true; set hive.stats.fetch.partition.stats=true; ํ†ต๊ณ„์ •๋ณด๋ฅผ ์ž๋™์œผ๋กœ ์ˆ˜์ง‘ํ•˜์ง€ ์•Š์œผ๋ฉด ANALYZE ๋ช…๋ น์„ ์ด์šฉํ•ด์„œ ์ˆ˜๋™์œผ๋กœ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ†ต๊ณ„์ •๋ณด ์ˆ˜์ง‘๋„ ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์œผ๋ฉด ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ANALYZE TABLE sample_table PARTITION(yymmdd='20180201') COMPUTE STATISTICS for columns; ANALYZE TABLE sample_table PARTITION(yymmdd='20180201') COMPUTE STATISTICS; explain ์ฟผ๋ฆฌ ํ™•์ธ explain ๋ช…๋ น์œผ๋กœ CBO ์ ์šฉ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CBO๊ฐ€ ์ž‘์šฉ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด "Plan optimized by CBO."๋ผ๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. hive> explain INSERT OVERWRITE DIRECTORY 'hdfs:///user/data/location' > select name, count(1) > from sample_table > where yymmdd=20180201 > group by name > > ; OK Plan optimized by CBO. CBO ์ ์šฉ ๋ถˆ๊ฐ€ CBO๊ฐ€ ์ ์šฉ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ƒํ™ฉ์ด ๋ช‡ ๊ฐ€์ง€ ์žˆ์Šต๋‹ˆ๋‹ค. CBO ์ ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๋กœ๊ทธ์— ๋ถˆ๊ฐ€๋Šฅํ•œ ์›์ธ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ "Plan not optimized by CBO." ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋˜๋ฉด ํ•˜์ด๋ธŒ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•˜์—ฌ ์ ์šฉ ๋ถˆ๊ฐ€ ์›์ธ์„ ํ™•์ธํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 2019-04-05T08:08:12,490 INFO [main([])]: parse.BaseSemanticAnalyzer (:()) - Not invoking CBO because the statement has sort by ์ ์šฉ ๋ถˆ๊ฐ€ ์ƒํ™ฉ CBO ์ ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ช‡ ๊ฐ€์ง€ ์ƒํ™ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํผ(transform)์€ ์‚ฌ์šฉ ๋ถˆ๊ฐ€ ์ธ๋ผ์ธ Lateral View Join๋งŒ ๊ฐ€๋Šฅ UNIQUE ์กฐ์ธ์€ ๋ถˆ๊ฐ€ ์„œ๋ธŒ ์ฟผ๋ฆฌ ์‚ฌ์šฉ ๋ถˆ๊ฐ€ Having ์ ˆ์— select์˜ alias ๊ฐ€ ๋“ค์–ด์žˆ์œผ๋ฉด ์‚ฌ์šฉ ๋ถˆ๊ฐ€ Sort By ์‚ฌ์šฉ ๋ถˆ๊ฐ€ ์ฐธ๊ณ  Cost-based optimization in Hive ๋ฐ”๋กœ ๊ฐ€๊ธฐ HIVE 0.14 Cost Based Optimizer (CBO) Technical Overview ๋ฐ”๋กœ ๊ฐ€๊ธฐ 4-์••์ถ• ํ•˜๋‘ก, ํ•˜์ด๋ธŒ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์ €์žฅ ์žฅ์น˜๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ์••์ถ•ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ gzip, snappy์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. gzip gzip์€ GNU zip์˜ ์•ฝ์ž์ด๋ฉฐ ์œ ๋‹‰์Šค์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์••์ถ• ๋ฐฉ์‹์˜ ํ•œ ์ข…๋ฅ˜์ž…๋‹ˆ๋‹ค. DEFLATE ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜๊ณ , ์••์ถ•๋ฅ ์ด ๋†’์ง€๋งŒ CPU ์‚ฌ์šฉ๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค. snappy snappy๋Š” ๊ตฌ๊ธ€์—์„œ ๊ฐœ๋ฐœํ•œ ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ ๋‹นํ•œ ์ˆ˜์ค€์˜ ์••์ถ•๋ฅ ์— ๋น ๋ฅธ ์••์ถ•/ํ•ด์ œ ์†๋„๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. gzip vs snappy ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋น„๊ต ํ•˜์ด๋ธŒ์—์„œ ๋‘ ๊ฐœ์˜ ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. 100MB์˜ ์›๋ณธ ํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•œ ๊ฒฐ๊ณผ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. gzip์„ ์ด์šฉํ•  ๋•Œ ์••์ถ•ํŒŒ์ผ์€ 4MB๊ฐ€ ๋˜์—ˆ๊ณ , snappy๋ฅผ ์ด์šฉํ•˜๋ฉด 10MB๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์••์ถ•ํ•  ๋•Œ CPU๋Š” snappy๊ฐ€ 30~40%, gzip์ด 50~60% ์ •๋„์˜ ์‚ฌ์šฉ๋ฅ ์„ ๋ณด์˜€์Šต๋‹ˆ๋‹ค. ์••์ถ• ์šฉ๋Ÿ‰ ์›๋ณธ 104857627 100 gzip 4851875 4 snappy 10968637 10 ํ•˜์ด๋ธŒ ์ ์šฉ ํ•˜์ด๋ธŒ๋Š” ์••์ถ•ํŒŒ์ผ์˜ ํƒ€์ž…์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ๋ฆฌ๋”๋ฅผ ์„ ํƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๊ฐœ์˜ ํŒŒ์ผ์ด ํ•œ๊ณณ์— ์žˆ์–ด๋„ ๋ฌธ์ œ์—†์ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ํ™˜๊ฒฝ์—์„œ gzip์˜ ์šฉ๋Ÿ‰์ด ๋” ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ๋งคํผ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ž‘๊ฒŒ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์›๋ณธ์ด 100G ์ผ ๋•Œ gzip์€ 4G, snappy๋Š” 10G๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ…Œ์ฆˆ ์—”์ง„์„ ์ด์šฉํ•  ๋•Œ ๋งค ํผ๊ฐ€ Gzip์€ 4๊ฐœ, snappy๋Š” 10๊ฐœ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์„ค์ •์—์„œ ๋งคํผ๋‹น ์ƒ์„ฑ๋˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— gzip์˜ ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ๋” ๋Šฆ์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ์„ค์ •์œผ๋กœ snappy๋กœ 1,994์ดˆ ๊ฑธ๋ฆฌ๋Š” ์ž‘์—…์ด gzip์—์„œ๋Š” 2,687์ดˆ๊ฐ€ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ „์ฒด ํด๋Ÿฌ์Šคํ„ฐ์˜ ์‚ฌ์šฉ๋ฅ ๋„ ์ƒ๋‹นํ•œ ์ฐจ์ด๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. gzip์„ ์ด์šฉํ•  ๋•Œ๋Š” tez.grouping.max-size๋ฅผ ์กฐ์ ˆํ•˜์—ฌ ์ ๋‹นํ•œ ํฌ๊ธฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  ๊ณ ์† ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋น„๊ต ํ…Œ์ŠคํŠธ: LZO/Snappy/SynLZ/LZ4/QuickLZ/Zlib What is Google Snappy? High-speed data compression and decompression 8-์„ค์ • ํ•˜์ด๋ธŒ์˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์œ ์šฉํ•œ ์„ค์ •์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์„ค์ •๊ฐ’ ์‹คํ–‰ ์—”์ง„, ํ ์„ค์ • MR ์„ค์ • TEZ ์„ค์ • TEZ ์—”์ง„ ์‹คํ–‰ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ๋งคํผ ์„ค์ • ๋ฐ์ดํ„ฐ ์‚ฌ์ด์ฆˆ์— ๋”ฐ๋ฅธ ๋งคํผ ๊ฐœ์ˆ˜ ์„ค์ • ๋งคํผ ๊ฐœ์ˆ˜ ๊ณ ์ • ๋ฆฌ๋“€์„œ ์„ค์ • ์••์ถ• ์„ค์ • ํŒŒ์ผ ๋จธ์ง€ ์„ค์ • ์ž„์‹œ ํŒŒ์ผ ์œ„์น˜ ์„ค์ • ๋‹ค์ด๋‚ด๋ฏน ํŒŒํ‹ฐ์…˜ ์„ค์ • MSCK ์ฒ˜๋ฆฌ ์„ค์ • ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ ์กฐํšŒ ์„ค์ • ์ฟผ๋ฆฌ ์˜ค๋ฅ˜ ๋ฌด์‹œ ์„ค์ • ํ”„๋กฌํ”„ํŠธ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ช… ํ‘œ์‹œ ์กฐํšŒ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์‹œ์— ์นผ๋Ÿผ ์ด๋ฆ„ ์ถœ๋ ฅ ์ฟผ๋ฆฌ ์กฐํšŒ ๋ชจ๋“œ ์„ค์ • ๋งต ์กฐ์ธ ์„ค์ • ์ปจํ…Œ์ด๋„ˆ ์˜ˆ์—ด(prewarm) ๊ธฐ๋ณธ ์„ค์ •๊ฐ’ ํ•˜์ด๋ธŒ ์ฟผ๋ฆฌ ์‹คํ–‰์„ ์œ„ํ•œ ๊ธฐ๋ณธ ์„ค์ •๊ฐ’์ž…๋‹ˆ๋‹ค. ์‹คํ–‰ ์—”์ง„ ์„ ํƒ, ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •, ๋งคํผ ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜ ์„ ํƒ์„ ์œ„ํ•œ ์„ค์ •์ž…๋‹ˆ๋‹ค. -- ํ•˜์ด๋ธŒ ์‹คํ–‰ ์—”์ง„ ์„ค์ • set hive.execution.engine=tez; set tez.queue.name=q2; -- TEZ AM ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • set tez.am.resource.memory.mb=2048; set tez.am.java.opts=-Xmx1600m; -- TEZ ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • set hive.tez.container.size=2048; set hive.tez.java.opts=-Xmx1600m; -- ํ…Œ์ฆˆ ์—”์ง„์˜ ๋งคํผ ๊ฐœ์ˆ˜ ์„ค์ •. ์ผ๋ถ€ ํ™˜๊ฒฝ์—์„œ ์„ค์ •์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋งต๋ฆฌ๋“€์Šค ์„ค์ •๋„ ๊ฐ™์ด ํ•ด์•ผ ํ•จ -- ๋งคํผ๋Š” 256MB์— ํ•˜๋‚˜์”ฉ ์ƒ์„ฑ set mapreduce.input.fileinputformat.split.maxsize=256000000; set mapreduce.input.fileinputformat.split.minsize=128000000; set tez.grouping.max-size=256000000; set tez.grouping.min-size=128000000; -- ํ…Œ์ฆˆ ์—”์ง„์˜ ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜ ์„ค์ • -- 128MB์— ํ•˜๋‚˜์”ฉ ์ƒ์„ฑ set mapred.reduce.tasks=-1; set hive.exec.reducers.bytes.per.reducer=128000000; -- ๋ฆฌ๋“€์„œ 10๊ฐœ ๊ณ ์ • set mapred.reduce.tasks=10; -- ๋‹ค์ด๋‚ด๋ฏน ํŒŒํ‹ฐ์…˜ ์„ค์ • set hive.exec.dynamic.partition.mode=nonstrict; ์‹คํ–‰ ์—”์ง„, ํ ์„ค์ • ํ•˜์ด๋ธŒ๋Š” ๋ฒ„์ „์— ๋”ฐ๋ผ mr, tez, spark ์‹คํ–‰์—”์ง„์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์—”์ง„์— ๋”ฐ๋ผ YARN ํ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- ์‹คํ–‰ ์—”์ง„ ์„ค์ • set hive.execution.engine=mr; set hive.execution.engine=tez; set hive.execution.engine=spark; -- ์‹คํ–‰ ์—”์ง„๋ณ„ ์ปคํŒจ์‹œํ‹ฐ ์Šค์ผ€์ค„๋Ÿฌ์˜ ํ ์ด๋ฆ„ ์„ค์ • set mapred.job.queue.name=queueName; set tez.queue.name=queueName; set spark.job.queue.name=queueName; MR ์„ค์ • MR ์—”์ง„์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ, ๋งคํผ, ๋ฆฌ๋“€์„œ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- ๋งต๋ฆฌ๋“€์Šค ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • set yarn.app.mapreduce.am.resource.mb=2048; set yarn.app.mapreduce.am.command-opts=-Xmx1600m; -- ๋งคํผ์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • set mapreduce.map.memory.mb=2048; set mapreduce.map.java.opts=-Xmx1600m; -- ๋ฆฌ๋“€์„œ์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ • set mapreduce.reduce.memory.mb=2048; set mapreduce.reduce.java.opts=-Xmx1600m; TEZ ์„ค์ • TEZ ์—”์ง„์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ, ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. -- TEZ ์žก์„ ์‹คํ–‰ํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. set tez.am.resource.memory.mb=2048; set tez.am.java.opts=-Xmx1600m; -- TEZ ์—”์ง„์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. set hive.tez.container.size=2048; set hive.tez.java.opts=-Xmx1600m; // container์˜ 80% -- ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์†ŒํŒ…ํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ set tez.runtime.io.sort.mb=800; // container์˜ 40% TEZ ์—”์ง„ ์‹คํ–‰ ๊ฒฐ๊ณผ ์ถœ๋ ฅ TEZ ์—”์ง„์œผ๋กœ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•œ ํ›„ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ๋ฆฌํฌํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •๊ฐ’์ž…๋‹ˆ๋‹ค. MR, Spark ์—”์ง„์—์„œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. set hive.tez.exec.print.summary=true; ๋งคํผ ์„ค์ • ๋งค ํผ์™€ ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋Š” ์‹คํ–‰ ์‹œ๊ฐ„์— ์ง์ ‘์ ์ธ ์˜ํ–ฅ์„ ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ž…๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์‚ฌ์ด์ฆˆ์— ๋”ฐ๋ผ ๊ฐœ์ˆ˜๋ฅผ ์กฐ์ ˆํ•ด ์ฃผ๋ฉด ์„ฑ๋Šฅ์„ ๋น ๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์‚ฌ์ด์ฆˆ์— ๋”ฐ๋ฅธ ๋งคํผ ๊ฐœ์ˆ˜ ์„ค์ • ๋งคํผ๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 1GB ์ผ ๋•Œ ์ตœ๋Œ€ ๋งคํผ ์‚ฌ์ด์ฆˆ๊ฐ€ 256MB ์ด๋ฉด ๋งคํผ๋Š” 4๊ฐœ ์ƒ์„ฑ๋˜์–ด ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ tez ์ž‘์—…์—์„œ MR ์„ค์ •์„ ๊ฐ™์ด ์ž…๋ ฅํ•ด์•ผ ๋งคํผ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์‚ฌ์ด์ฆˆ์— ๋”ฐ๋ผ ๋งคํผ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜์ง€ ์•Š์œผ๋ฉด MR ์—”์ง„, TEZ ์—”์ง„ ์„ค์ •์„ ๊ฐ™์ด ์ž…๋ ฅํ•ด ๋ณด์‹ญ์‹œ์˜ค. ๋งคํผ ๊ฐœ์ˆ˜๋Š” ์›๋ณธ ํŒŒ์ผ์˜ ํฌ๊ธฐ์— ์˜ํ–ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 1MB ํฌ๊ธฐ์˜ ํŒŒ์ผ์ด 4๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ทธ๋ฃนํ•‘ ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์—ฌ๋„ ๋งค ํผ๊ฐ€ 4๊ฐœ๊ฐ€ ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์‚ฌํ•ญ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งˆ์Šคํ„ฐ์˜ ๋กœ๊ทธ์—์„œ grouper.TezSplitGrouper ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. -- MR ์—”์ง„์˜ ๋งคํผ๋‹น ์ตœ๋Œ€ ์ฒ˜๋ฆฌ ์‚ฌ์ด์ฆˆ set mapreduce.input.fileinputformat.split.maxsize=268435456; set mapreduce.input.fileinputformat.split.minsize=134217728; -- TEZ ์—”์ง„์˜ ๋งคํผ๋‹น ์ตœ๋Œ€ ์ฒ˜๋ฆฌ ์‚ฌ์ด์ฆˆ set tez.grouping.max-size=268435456; set tez.grouping.min-size=134217728; ๋งคํผ ๊ฐœ์ˆ˜ ๊ณ ์ • ๋งคํผ only ์žก์—์„œ ์ตœ์ข… ์ƒ์„ฑ๋˜๋Š” ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ๋ฆฌ์†Œ์Šค์˜ ํšจ์œจ์ ์ธ ์‚ฌ์šฉ์„ ์ด์œ ๋กœ ๋งคํผ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ ์ •ํ•ด์•ผ ํ•  ๋•Œ๋Š” ๋‹ค์Œ์˜ ์„ค์ •์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. -- MR ์—”์ง„์˜ ๋งคํผ ๊ฐœ์ˆ˜ ์„ค์ • set mapreduce.job.maps=1; -- TEZ ์—”์ง„์˜ ๋งคํผ ๊ฐœ์ˆ˜ ์„ค์ • set tez.grouping.split-count=1; ๋ฆฌ๋“€์„œ ์„ค์ • ๋ฆฌ๋“€์„œ๋Š” ์ž…๋ ฅ ์‚ฌ์ด์ฆˆ๊ฐ€ 1GB ์ผ ๋•Œ ์ตœ๋Œ€ ์ฒ˜๋ฆฌ ์‚ฌ์ด์ฆˆ๊ฐ€ 256MB ์ด๋ฉด ๋ฆฌ๋“€์„œ 4๊ฐœ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ตœ๋Œ€ ๋ฆฌ๋“€์„œ ์‚ฌ์šฉ ๊ฐœ์ˆ˜๋‚˜ ๋ฆฌ๋“€์„œ ์‚ฌ์šฉ ๊ฐœ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜ ์„ค์ • ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋“€์„œ๋ณ„ ์ตœ๋Œ€ ์ฒ˜๋ฆฌ ์‚ฌ์ด์ฆˆ > ์ตœ๋Œ€ ๋ฆฌ๋“€์„œ ์‚ฌ์šฉ ๊ฐœ์ˆ˜ > ๋ฆฌ๋“€์„œ ์‚ฌ์šฉ ๊ฐœ์ˆ˜ -- ๋ฆฌ๋“€์„œ ์‚ฌ์šฉ ๊ฐœ์ˆ˜ ์ง€์ • set mapreduce.job.reduces=100; -- ์ตœ๋Œ€ ๋ฆฌ๋“€์„œ ์‚ฌ์šฉ ๊ฐœ์ˆ˜ set hive.exec.reducers.max=100; -- ๋ฆฌ๋“€์„œ๋ณ„ ์ตœ๋Œ€ ์ฒ˜๋ฆฌ ์‚ฌ์ด์ฆˆ set hive.exec.reducers.bytes.per.reducer=268435456; ์••์ถ• ์„ค์ • ํ•˜์ด๋ธŒ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์••์ถ•ํ•˜๋Š” ์„ค์ •์„ ์ด์šฉํ•ด ๋„คํŠธ์›Œํฌ ํ†ต์‹ ๋Ÿ‰๊ณผ ์ €์žฅ ์šฉ๋Ÿ‰์˜ ์ด์ ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -- ํ•˜์ด๋ธŒ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์••์ถ•ํ•  ๊ฒƒ์ธ์ง€ ์„ค์ • set hive.exec.compress.output=true; set hive.exec.compress.intermediate=true; set mapreduce.output.fileoutputformat.compress.codec=org.apache.hadoop.io.compress.SnappyCodec; ํŒŒ์ผ ๋จธ์ง€ ์„ค์ • HDFS๋Š” ์ž‘์€ ์‚ฌ์ด์ฆˆ์˜ ํŒŒ์ผ์ด ๋งŽ์œผ๋ฉด ์„ฑ๋Šฅ์— ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ๋Š” ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋จธ์ง€ ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. avgsize ์ดํ•˜์˜ ํŒŒ์ผ์„ ๋ชจ์•„์„œ task ์‚ฌ์ด์ฆˆ์˜ ํŒŒ์ผ๋กœ ๋จธ์ง€ ํ•˜์—ฌ ์ค๋‹ˆ๋‹ค. ์ž‘์€ ์‚ฌ์ด์ฆˆ์˜ ํŒŒ์ผ์„ ๋งŽ์ด ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ๋จธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋จธ์ง€ ์ž‘์—…์— ์‹œ๊ฐ„์ด ๋งค์šฐ ๋งŽ์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฌ์šฉ์— ์ฃผ์˜ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. set hive.merge.mapfiles=true; // ๋งคํผ only ๊ฒฐ๊ณผ ๋จธ์ง€ set hive.merge.mapredfiles=true; // ๋งต๋ฆฌ๋“€์Šค ๊ฒฐ๊ณผ ๋จธ์ง€ set hive.merge.tezfiles=true; // tez ๊ฒฐ๊ณผ ๋จธ์ง€ set hive.merge.size.per.task=256000000; set hive.merge.smallfiles.avgsize=32000000; ์ž„์‹œ ํŒŒ์ผ ์œ„์น˜ ์„ค์ • ํ•˜์ด๋ธŒ ์‹คํ–‰ ์ค‘ ๋ฐœ์ƒํ•˜๋Š” ์ž„์‹œ ํŒŒ์ผ์˜ ์œ„์น˜ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. set hive.exec.scratchdir=/tmp; set hive.exec.local.scratchdir=/tmp; set hive.exec.stagingdir=/tmp/hive-staging; ๋‹ค์ด๋‚ด๋ฏน ํŒŒํ‹ฐ์…˜ ์„ค์ • ํ•˜์ด๋ธŒ์˜ ๋‹ค์ด๋‚ด๋ฏน ํŒŒํ‹ฐ์…˜ ์„ค์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. set hive.exec.dynamic.partition=true; -- ๋‹ค์ด๋‚ด๋ฏน ํŒŒํ‹ฐ์…˜ ์‚ฌ์šฉ ์—ฌ๋ถ€ ์„ค์ • set hive.exec.dynamic.partition.mode=nonstrict; -- ์Šคํƒœํ‹ฑ ํŒŒํ‹ฐ์…˜๊ณผ์˜ ํ˜ผํ•ฉ ์‚ฌ์šฉ ์—ฌ๋ถ€ set hive.exec.max.dynamic.partitions.pernode=500; -- ๋…ธ๋“œ๋ณ„ ๋‹ค์ด๋‚ด๋ฏน ํŒŒํ‹ฐ์…˜ ๊ฐœ์ˆ˜ ์„ค์ • set hive.exec.max.dynamic.partitions=10000; -- ์ „์ฒด ๋‹ค์ด๋‚ด๋ฏน ํŒŒํ‹ฐ์…˜ ๊ฐœ์ˆ˜ ์„ค์ • MSCK ์ฒ˜๋ฆฌ ์„ค์ • MSCK ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ ๋ฐฐ์น˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์กฐ์ •ํ•ด์„œ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•  ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ์ค„์—ฌ์„œ ํ•˜์ด๋ธŒ์˜ ๋ถ€ํ•˜๋ฅผ ์ค„์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. -- 0์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋ชจ๋“  ํŒŒํ‹ฐ์…˜์„ ๋ณต๊ตฌํ•œ๋‹ค. ์„ค์ •ํ•œ ๊ฐ’๋งŒํผ ์ฒ˜๋ฆฌ set hive.msck.repair.batch.size=0; -- ํŒŒํ‹ฐ์…˜์— ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ž๊ฐ€ ์žˆ์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ignore๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฌด์‹œํ•˜๊ณ  ๋„˜์–ด๊ฐ„๋‹ค. set hive.msck.path.validation=ignore; ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ ์กฐํšŒ ์„ค์ • ํ…Œ์ด๋ธ” LOCATION์˜ ๋ฐ์ดํ„ฐ์™€ ํ•˜์œ„์— ์œ„์น˜ํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๋‹ค์Œ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. set hive.supports.subdirectories=true; set mapred.input.dir.recursive=true; ์ฟผ๋ฆฌ ์˜ค๋ฅ˜ ๋ฌด์‹œ ์„ค์ • ํ•˜์ด๋ธŒ๋Š” hql ํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ฟผ๋ฆฌ ์ˆ˜ํ–‰์„ ์ข…๋ฃŒํ•˜๊ณ , cli ๋„ ๋™์ž‘์„ ์ข…๋ฃŒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ด๋„ ๋ฌด์‹œํ•˜๊ณ  ๋‹ค์Œ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ํ•˜๋Š” ์˜ต์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. set hive.cli.errors.ignore=true; ํ”„๋กฌํ”„ํŠธ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ช… ํ‘œ์‹œ ํ•˜์ด๋ธŒ ํ”„๋กฌํ”„ํŠธ์— ํ˜„์žฌ ์‚ฌ์šฉ ์ค‘์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ช…์„ ํ‘œ์‹œํ•˜๊ฒŒ ํ•˜๋ฉด ์‚ฌ์šฉ์ž ์˜ค๋ฅ˜์— ์˜ํ•œ ์‹ค์ˆ˜๋ฅผ ์‚ฌ์ „์— ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. set hive.cli.print.current.db=true ์กฐํšŒ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์‹œ์— ์นผ๋Ÿผ ์ด๋ฆ„ ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ ์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์นผ๋Ÿผ ์ด๋ฆ„์„ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜๋Š” ์˜ต์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. set hive.cli.print.header=true; ์ฟผ๋ฆฌ ์กฐํšŒ ๋ชจ๋“œ ์„ค์ • ์ฟผ๋ฆฌ์˜ ์„ฑ๋Šฅ์„ ์œ„ํ•ด ํ…Œ์ด๋ธ” ํ’€ ์Šค์บ”(Full scan)์„ ์ œํ•œํ•˜๋Š” ํ•˜๊ฑฐ๋‚˜, msck repair ์ฒ˜๋ฆฌ ์‹œ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ nonstrict ๋ชจ๋“œ์—์„œ๋งŒ ๋™์ž‘ํ•  ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ๋Š” ๋‹ค์Œ์˜ ์˜ต์…˜์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. set hive.mapred.mode=strict | nonstrict; strict ๋ชจ๋“œ์—์„œ๋Š” ํŒŒํ‹ฐ์…˜ ์ฒ˜๋ฆฌ๋œ ํ…Œ์ด๋ธ”์„ ์กฐํšŒํ•  ๋•Œ ๋‚˜, group by, order by๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ where ์กฐ๊ฑด์ด ์—†์œผ๋ฉด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. hive (default)> select * from tbl_table_hdfs; FAILED: SemanticException Queries against partitioned tables without a partition filter are disabled for safety reasons. If you know what you are doing, please set hive.strict.checks.large.query to false and that hive.mapred.mode is not set to 'strict' to proceed. Note that if you may get errors or incorrect results if you make a mistake while using some of the unsafe features. No partition predicate for Alias "tbl_user" Table "tbl_user" ๋งต ์กฐ์ธ ์„ค์ • ๋งต ์กฐ์ธ์„ ์ด์šฉํ•˜๋ฉด ํ…Œ์ด๋ธ”์˜ ์กฐ์ธ ์ฒ˜๋ฆฌ๊ฐ€ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ 10MB๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€๋Š” ๋ฐ์ดํ„ฐ ๊ธฐ์ค€ ์‚ฌ์ด์ฆˆ์ด๋ฏ€๋กœ, ํŒŒ์ผ์˜ ์‚ฌ์ด์ฆˆ์™€๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค. desc formatted๋กœ ํ…Œ์ด๋ธ” ์ •๋ณด๋ฅผ ํ™•์ธํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํ…Œ์ด๋ธ” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ rawDataSize ๊ธฐ์ค€์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. set hive.auto.convert.join=true; set hive.auto.convert.join.noconditionaltask.size=10000000; ์ปจํ…Œ์ด๋„ˆ ์˜ˆ์—ด(prewarm) ํ•˜๋‘ก 2์—์„œ๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ฏธ๋ฆฌ ์‹คํ–‰ํ•˜์—ฌ ์ž‘์—…์„ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์ด ์ƒ๊ธฐ๋Š” ์ž‘์—…์˜ ๊ฒฝ์šฐ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•˜์—ฌ ์ž‘์—…์„ ๋น ๋ฅด๊ฒŒ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. set hive.prewarm.enabled=true; set hive.prewarm.numcontainers=10; 1-ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2 ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2 ๊ด€๋ จ ์„ค์ •์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์„ธ์…˜ ์„ค์ • ํ•˜์ด๋ธŒ ์„œ๋ฒ„ 2 ์„ธ์…˜ ๊ด€๋ จ ์„ค์ •์ž…๋‹ˆ๋‹ค. ์„ธ์…˜์„ ์œ ์ง€ํ•˜๋Š” ์‹œ๊ฐ„๊ณผ ์ข…๋ฃŒํ•˜๋Š” ์‹œ๊ฐ„์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. <property> <name>hive.server2.idle.operation.timeout</name> <value>1h</value> </property> <property> <name>hive.server2.idle.session.timeout</name> <value>3h</value> </property> <property> <name>hive.server2.session.check.interval</name> <value>3600000</value> </property> 4-ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ ์šด์˜ ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์šด์˜ํ•˜๋ฉด์„œ ์•Œ์•„๋‘๋ฉด ์ข‹์€ ํŒ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ์˜ ๊ธฐ๋ณธ ํฌํŠธ(Port) 1-ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ ๊ธฐ๋ณธ ํฌํŠธ(Port) ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ์˜ ๊ธฐ๋ณธ ํฌํŠธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํƒ€์ž… ์„œ๋ฒ„ ํฌํŠธ ์ข…๋ฅ˜ ์‚ฌ์šฉ๋ฒ• ์„ค๋ช… hdfs hdfs 8020 RPC hadoop fs -ls hdfs://$(hostname -f):8020/ ๋„ค์ž„๋…ธ๋“œ ํ˜ธ์ถœ hdfs webhdfs 50070 http curl -s http://$(hostname -f):50070/webhdfs/v1/?op=GETFILESTATUS jq hdfs|webhdfs-proxy|14000|curl -s "http:///Misplaced & Misplaced & (hostname -f):8088 yarn|timelineserver|8188|curl -s http:// ( o t a e f ) 8188 w / 1 t m l n y r | a n p o y s r e | 20888 M p e u e h s o y e v r 19888 c r โˆ’ h t : / (hostname -f):19888/ws/v1/history/info hive|metastore|9083|thrift:// ( o t a e f ) 9083 i e h v โˆ’ e v r | 10000 j b : i e : / o a h s : 10000 i e h v โˆ’ e v r w b I 10002 l n h t : / (hostname -f):10002 spark|spark-history-server|18080|curl http:// ( o t a e f ) 18080 a i v / p l c t o s p r | i y s r e | 8998 l n h t : / o a h s : 8998 m | a o p k s 9700 c r h t : / (hostname -f):9700/kms/v1/keys/names 2-ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ ์šด์˜ ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์šด์˜ํ•˜๋ฉด์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์‚ฌํ•ญ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ ๋„ค์ž„๋…ธ๋“œ๋Š” ํž™๋ฉ”๋ชจ๋ฆฌ์— HDFS์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ํŒŒ์ผ์˜ ๋ฉ”ํƒ€์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ JVM์˜ ํžˆํ”„ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ 1์— ๋”ฐ๋ผ ์ „์ฒด ํŒŒ์ผ, ๋ธ”๋ก์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ œํ•œ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ, ๋ธ”๋ก์ด ๋งŽ์ด ์ƒ์„ฑ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ์˜ ํ•œ๊ณ„์น˜์— ๋„๋‹ฌํ•˜๋ฉด JVM์˜ ํž™๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋Š˜๋ ค์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์žฌ๊ธฐ ๋™ํ•ด์•ผ ํ•˜๊ณ , ๋„ค์ž„๋…ธ๋“œ๋Š” ์žฌ๊ธฐ๋™ ํ•  ๋•Œ ๋ธ”๋ก ์ •๋ณด๋ฅผ ์žฌ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด fsimage, edits ํŒŒ์ผ์„ ์ฝ์–ด์„œ ๋ธ”๋ก ์ •๋ณด๋ฅผ ์žฌ๊ตฌ์ถ•ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฐ์ดํ„ฐ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๋ธ”๋ก ์ •๋ณด๋ฅผ ๋ฐ›์•„์„œ ๊ฒฐ๊ณผ๋ฅผ ์—ฐ๋™ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ํŒŒ์ผ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์„์ˆ˜๋ก ๋„ค์ž„๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ๊ฐ€๋™ํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ๊ธธ์–ด์ง€๊ณ , ์ด ์‹œ๊ฐ„ ๋™์•ˆ ํด๋Ÿฌ์Šคํ„ฐ์˜ ์šด์˜์ด ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ• Hadoop Archive ์ด์šฉ ํ•˜๋‘ก์€ har ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ์„ ํ•˜๋‚˜๋กœ ๋ฌถ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํŒŒ์ผ ์ •๋ฆฌ ํŒŒ์ผ์˜ ์ ‘๊ทผ์‹œ๊ฐ„์„ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๋œ ํŒŒ์ผ์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ls ๋ช…๋ น์–ด์˜ -u ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผ ์‹œ๊ฐ„(access time)์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ํŒŒ์ผ์ด ๋งŽ์œผ๋ฉด ๋„ค์ž„๋…ธ๋“œ์— ๋ถ€ํ•˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. hadoop fs -ls -u /user/ oiv(offline fsimage viewer)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผ ์‹œ๊ฐ„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•œ ๋‚ด์šฉ์„ ๋ถ„์„ํ•˜์—ฌ ์ ‘๊ทผ์‹œ๊ฐ„์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. hdfs oiv HDFS ์—ฐํ•ฉ(Federation) ์ด์šฉ HDFS ์—ฐํ•ฉ ๊ธฐ๋Šฅ์€ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณ„๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. hdfs:///user, hdfs:///datas, hdfs:///temps ๊ฐ™์€ 3๊ฐœ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ฐ๊ฐ์˜ ๋„ค์ž„๋…ธ๋“œ๋กœ ์„œ๋น„์Šคํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฐ„์—๋Š” cp, mv๋ฅผ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. distcp๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์˜ฎ๊ฒจ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ๋จธ์ง€(merge) ๊ธฐ๋Šฅ ์ด์šฉ ํ•˜์ด๋ธŒ๋Š” ์ตœ์ข… ์ž‘์—… ํŒŒ์ผ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ํ™•์ธํ•˜์—ฌ, ์ง€์ •ํ•œ ๊ธฐ์ค€ ์ดํ•˜์˜ ํŒŒ์ผ์„ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ๋ฌถ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Map, Reduce ์ž‘์—… ์ดํ›„ Merge ์ž‘์—…์ด ์ถ”๊ฐ€์ ์œผ๋กœ ์ƒ์„ฑ๋˜์–ด ๊ฒฐ๊ณผ ํŒŒ์ผ์„ ๋จธ์ง€ ํ•ฉ๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ ์žฅ์•  ๋Œ€์‘ ๋„ค์ž„๋…ธ๋“œ ์žฅ์•  HDFS๋Š” ๋„ค์ž„๋…ธ๋“œ๊ฐ€ SPOF๋กœ ๋„ค์ž„๋…ธ๋“œ์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํšŒ ํŒŒํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฃผํ‚คํผ๋ฅผ ์ด์šฉํ•ด HDFS๋ฅผ HA ๊ตฌ์„ฑ์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์‚ฌ๋ก€ 1: ๋ฐ์ดํ„ฐ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์— ์žฌ์‹œ์ž‘ํ•˜์—ฌ ๋„ค์ž„๋…ธ๋“œ์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•จ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์— ์žฌ์‹œ์ž‘ํ•˜์—ฌ ๋„ค์ž„๋…ธ๋“œ์— ๋ฐ์ดํ„ฐ๋…ธ๋“œ์˜ ๋ธ”๋ก ๋ฆฌํฌํŒ…๊ณผ ๋ธ”๋ก ๋ฆฌํฌํŒ…์„ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ๋‚จ๊ธฐ๋Š” INFO ๋กœ๊ทธ, ์ฃผํ‚คํผ์˜ ๋„ค์ž„๋…ธ๋“œ ํ—ฌ์Šค๋ชจ๋‹ˆํ„ฐ๋ง RPC ํฌํŠธ์™€ ๋ธ”๋ก ๋ฆฌํฌํŒ… ํฌํŠธ๊ฐ€ ๋™์ผํ•˜์—ฌ ์‘๋‹ต์‹œ๊ฐ„์ด ์ดˆ๊ณผ๋˜๋ฉด์„œ ๋„ค์ž„๋…ธ๋“œ์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. HDFS์˜ ๋กœ๊ทธ๋ฅผ TRACE๋กœ ๋ณ€๊ฒฝํ•˜๊ณ , ์ฃผํ‚คํผ์˜ RPC ํฌํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋…ธ๋“œ ๋งค๋‹ˆ์ € ์žฅ์•  ์‚ฌ๋ก€ 1: ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ์ด์ „์œผ๋กœ ์ •๋ณด๊ฐ€ ์ œ๋Œ€๋กœ ๋ฐ˜์˜๋˜์ง€ ์•Š์•„์„œ ์žฅ์•  ๋ฐœ์ƒ HA ๊ตฌ์„ฑ๋œ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ๋…ธ๋“œ ๋งค๋‹ˆ์ €๊ฐ€ ์ค‘๋‹จ๋œ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์˜ ์ •๋ณด๋ฅผ ๊ณ„์† ์บ์Šํ•˜์—ฌ ๋…ธ๋“œ ๋งค๋‹ˆ์ €์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•กํ‹ฐ๋ธŒ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์™€ ์Šคํƒ ๋ฐ”์ด ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์ž‘์—…์˜ ์•ˆ์ •์„ฑ์„ ํ™•๋ณดํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ์ž‘์—…์€ ์Šคํ…Œ์ด์ง€์—์„œ ํ…Œ์ŠคํŠธ ํ›„ ์šด์˜์— ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ์žฅ์•  ์‚ฌ๋ก€ 1: ์ฃผํ‚คํผ๊ฐ€ ์‘๋‹ตํ•˜์ง€ ์•Š์•„์„œ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๊ฐ€ ์ž‘์—…์˜ ์ •๋ณด๋ฅผ ์ฃผํ‚คํผ์— ๋ณด๊ด€ํ•  ๋•Œ ์ฃผํ‚คํผ์— ๋„ˆ๋ฌด ๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณด๊ด€๋˜์–ด ์ฃผํ‚คํผ๊ฐ€ ์‘๋‹ต์„ ํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์™„๋ฃŒ๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ •๋ณด๋Š” ์ €์žฅํ•˜์ง€ ์•Š๊ณ , ์ฃผํ‚คํผ์˜ ๋ฒ„ํผ ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋…ธ๋“œ OS ์—…๊ทธ๋ ˆ์ด๋“œ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋…ธ๋“œ์˜ OS ์—…๊ทธ๋ ˆ์ด๋“œ๋Š” ๋‹ค์Œ์„ ๊ณ ๋ คํ•˜์—ฌ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. YARN ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰ ํ™˜๊ฒฝ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋˜๋Š” OS ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ง€์— ์œ ์˜ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. OS ๋ณ„๋กœ /usr/bin/๊ณผ ๊ฐ™์€ ์‹คํ–‰ ๋ช…๋ น์–ด์˜ ์œ„์น˜๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•˜๋‘ก ์ž‘์—… ๋ฆฌ์†Œ์Šค ํ•˜๋‘ก์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์ž‘์—…์€ ์–ด๋–ค OS์—์„œ ์‹คํ–‰๋ ์ง€ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. OS ๋ฒ„์ „์— ๋งž๋Š” ์ž‘์—… ๋ฆฌ์†Œ์Šค๊ฐ€ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. OS์— ์˜์กดํ•˜๋Š” ์ž‘์—… OS์— ์„ค์น˜๋œ ๊ธฐ๋ณธ ๋ช…๋ น์–ด์˜ ๋ฒ„์ „์— ๋”ฐ๋ผ ์ž‘์—… ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฒ„์ „์—์„œ ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ์˜ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ์ž‘์—…์€ ์‚ฌ์ „์— ์—ฌ๋Ÿฌ ๋ฐฉ๋ฉด์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๊ณ , YARN ์ปจํ…Œ์ด๋„ˆ์˜ ์ž‘์—… ํ™˜๊ฒฝ์„ ํ†ต์ผํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ์žฅ๋น„๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ œ๊ฑฐ ํ›„ ์—…๊ทธ๋ ˆ์ด๋“œ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  ๋„ค์ด๋ฒ„ - ๋ฉ€ํ‹ฐํ…Œ๋„ŒํŠธ ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ ์šด์˜ ๊ฒฝํ—˜๊ธฐ 1๋งŒ ๋ธ”๋ก๋‹น 1G์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. โ†ฉ 1-Line์˜ ํด๋Ÿฌ์Šคํ„ฐ ์žฅ์•  ๋Œ€์‘ Line์—์„œ ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์šด์˜ํ•˜๋ฉด์„œ ๋ฐœ์ƒํ•œ ์žฅ์•  ์ƒํ™ฉ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์—”์ง€๋‹ˆ์–ด๋ง ๊ด€๋ จ ์†Œํ”„ํŠธ์›จ์–ด ์žฅ์•  ๋Œ€์‘ ์‚ฌ๋ก€์—์„œ ์ƒ์„ธํ•œ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Apache Hadoop YARN ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € failover ๋ฐœ์ƒ ๋ฌธ์ œ์™€ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ํ˜„์ƒ ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ๋™์ž‘ํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๋ฉด์„œ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๊ฐ€ ์ฃผํ‚คํผ์˜ ์‘๋‹ต์— ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜์ง€ ๋ชปํ•˜์—ฌ HA ๊ตฌ์„ฑ๋œ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์˜ Failover ํ˜„์ƒ์ด ๋ฐœ์ƒํ•จ Failover๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์•กํ‹ฐ๋ธŒ ๋…ธ๋“œ๊ฐ€ ์Šคํƒ ๋ฐ”์ด ๋…ธ๋“œ๋กœ ๋ณ€๊ฒฝ ์•กํ‹ฐ๋ธŒ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๊ฐ€ ํ•˜ํŠธ๋น„ํŠธ๋ฅผ ์ฃผํ‚คํผ๋กœ ์ „์†กํ•˜์ง€ ๋ชปํ•ด์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ ์›์ธ ์•กํ‹ฐ๋ธŒ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์˜ JVM์„ ํ™•์ธ Failover ๋ฐœ์ƒ ์‹œ JVM์ด<NAME> ๊ธฐ๋ก์ด ์—†์–ด GC๋Š” ์•„๋‹˜ CPU ์‚ฌ์šฉ๋ฅ  ๋“ฑ ํ‰์†Œ์™€ ํฐ ์ฐจ์ด๊ฐ€ ์—†์Œ ๋”ฐ๋ผ์„œ ์›์ธ์€ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ๋‚ด๋ถ€์— ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์ถ”์ • ๊ฐ์ข… ๋กœ๊ทธ ๋ถ„์„ ๋ฐ JMX metric ๋ชจ๋‹ˆํ„ฐ๋ง์„ ํ†ตํ•ด Failover ๋ฐœ์ƒ ์ง์ „ JVM์˜ ์Šค๋ ˆ๋“œ ๊ฐœ์ˆ˜๊ฐ€ ํ‰์†Œ 1,000๊ฐœ์—์„œ 15,000๊ฐœ๊นŒ์ง€ ์ฆ๊ฐ€ํ•œ ๊ฒƒ์„ ํ™•์ธ ์†Œ์Šค์ฝ”๋“œ ๋ถ„์„ ๊ฒฐ๊ณผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์šด์˜, ์ปจํ…Œ์ด๋„ˆ ์ •์ง€ ์‹œ ๋ฐœ์ƒํ•˜๋Š” ์ •๋ณด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์Šค๋ ˆ๋“œ๋กœ ํ™•์ธ ์กฐ์น˜ ์ƒํ™ฉ ์ปจํ…Œ์ด๋„ˆ ์ •์ง€ ์‹œ ๋ฐœ์ƒํ•˜๋Š” ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๋„๋ก hadoop.registry.rm.enabled ์„ค์ •์„ false๋กœ ๋ณ€๊ฒฝ ํ˜„์žฌ ์‚ฌ์šฉ ์ค‘์ธ EMR์—๋„ ํ•ด๋‹น ์„ค์ •์€ false์ž„. Apache Hadoop HDFS NameNode failover ๋ฐœ์ƒ ๋ฌธ์ œ์™€ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ํ˜„์ƒ HA ๊ตฌ์„ฑ๋œ HDFS ๋„ค์ž„๋…ธ๋“œ์— ํŠน์ • ์‹œ๊ฐ„๋Œ€์— Failover ๋ฐœ์ƒ ๋„ค์ž„๋…ธ๋“œ ํ—ฌ์Šค ์ฒดํฌ๊ฐ€ ํƒ€์ž„์•„์›ƒ๋˜์–ด ๋ฐœ์ƒ ์›์ธ .Trash ํ•˜์œ„ ๋ฐ์ดํ„ฐ๋ฅผ<NAME>๋Š” ๊ณผ์ •์—์„œ ํŠน์ • ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์‚ญ์ œํ•˜๋ฉด์„œ write lock์ด ์žฅ์‹œ๊ฐ„(36์ดˆ) ์œ ์ง€๋จ ๊ทธ ํ›„์—๋„ ๋ธ”๋ก์„ ์‚ญ์ œํ•˜๋ฉด์„œ GC๊ฐ€ 1~7์ดˆ๊ฐ„ ๋ฐœ์ƒ ๋„ค์ž„๋…ธ๋“œ RPC Client Port ํ๋ฅผ ํ™•์ธํ•˜๋ฉด write lock์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ํ๊ฐ€ ๋Œ€๋Ÿ‰(3,500)์œผ๋กœ ๋ฐœ์ƒ ์ฃผํ‚คํผ์˜ ํ—ฌ์Šค ์ฒดํฌ ์š”์ฒญ ํฌํŠธ์™€ RPC ํด๋ผ์ด์–ธํŠธ ํฌํŠธ๊ฐ€ ๋™์ผํ•˜์—ฌ ์žฅ์‹œ๊ฐ„ ๋Œ€๊ธฐ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ๋„ค์ž„๋…ธ๋“œ Failover ๋ฐœ์ƒ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ .Trash ๋””๋ ‰ํ„ฐ๋ฆฌ ํ•˜์œ„ ํŒŒ์ผ์˜ ์‚ญ์ œ ๊ฐ„๊ฒฉ ๋‹จ์ถ• ์‚ญ์ œ ๊ฐ„๊ฒฉ์„ ์ค„์—ฌ์„œ ๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ํœด์ง€ํ†ต์— ์Œ“์ด๊ธฐ ์ „์— ์ฒ˜๋ฆฌ ์ฃผํ‚คํผ์˜ ํ—ฌ์Šค ์š”์ฒญ ํฌํŠธ ๋ณ€๊ฒฝ ์‘๋‹ต ํฌํŠธ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ์š”์ฒญ์— ๋Œ€์‘ Apache Zeppelin Notebook ์Šค์ผ€์ค„๋Ÿฌ ์ž‘๋™ ์ด์ƒ ๋ฌธ์ œ์™€ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ํ˜„์ƒ ์ œํ”Œ๋ฆฐ์˜ ์Šค์ผ€ ์ค„๋ฆฌ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ํ˜„์ƒ ์žฌ์‹œ์ž‘ํ•˜๋ฉด ํ•ด๊ฒฐ๋จ ์›์ธ ๋กœ๊ทธ์—์„œ ์›์ธ์„ ํ™•์ธํ•˜์ง€ ๋ชปํ•ด์„œ JVM ํ”„๋กœ์„ธ์Šค์˜ ์Šค๋ ˆ๋“œ ๋คํ”„ ํ™•์ธ Quartz Scheduler Worker ์Šค๋ ˆ๋“œ๊ฐ€ ๋ชจ๋‘ sleeping ์ƒํƒœ์ธ ๊ฒƒ์„ ํ™•์ธ ๋ฒ„๊ทธ๋กœ ์ธํ•œ ์ฒ˜๋ฆฌ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ์ œํ”Œ๋ฆฐ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์ฒ˜๋ฆฌ Apache Zeppelin deadlock ๋ฐœ์ƒ ๋ฌธ์ œ์™€ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ํ˜„์ƒ ๊ฐ‘์ž๊ธฐ ์ œํ”Œ๋ฆฐ์ด ์‘๋‹ตํ•˜์ง€ ์•Š๋Š” ํ˜„์ƒ์ด ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐœ์ƒ ์›์ธ ๋กœ๊ทธ์—์„œ ์›์ธ์„ ํ™•์ธํ•˜์ง€ ๋ชปํ•ด์„œ ์ œํ”Œ๋ฆฐ์˜ JVM ํ”„๋กœ์„ธ์Šค ์Šค๋ ˆ๋“œ ๋คํ”„๋ฅผ ํ™•์ธ ๋ฐ๋“œ๋ฝ์ด ๋ฐœ์ƒํ•œ ๊ฒƒ์„ ํ™•์ธ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ์†Œ์Šค์ฝ”๋“œ์˜ ์ˆ˜์ •์ด ํ•„์š”ํ•˜์—ฌ OSS์— ์•Œ๋ฆผ ์›์ธ์ด ๋ฐœ์ƒํ•˜๋Š” ์ˆœ์„œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๊ถŒ๊ณ  Apache Spark SQL ์„ฑ๋Šฅ ์ด์Šˆ์™€ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ํ˜„์ƒ ์ฟผ๋ฆฌ ์‹คํ–‰์ด ์™„๋ฃŒ๋˜์ง€ ์•Š๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•จ ํŒŒํ‹ฐ์…˜์„ ์ง€์ •ํ•˜์—ฌ๋„ ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์Œ ๋“œ๋ผ์ด๋ฒ„ ํ”„๋กœ์„ธ์Šค์˜ CPU ์‚ฌ์šฉ๋ฅ ์ด 100%๊ฐ€ ๋˜์–ด ์ฟผ๋ฆฌ ์‹คํ–‰์ด ์™„๋ฃŒ๋˜์ง€ ์•Š์Œ ํ™•์ธํ•ด ๋ณด๋‹ˆ ๋Œ€์ƒ ํ…Œ์ด๋ธ”์˜ ์กฐํšŒ์— ์‚ฌ์šฉ๋˜๋Š” ํŒŒํ‹ฐ์…˜ ๊ฐœ์ˆ˜๊ฐ€ 4,368,000๊ฐœ ์›์ธ JVM์˜ ๋“œ๋ผ์ด๋ฒ„ ์Šค๋ ˆ๋“œ๋ฅผ ํ™•์ธ ํŠน์ • ์Šค๋ ˆ๋“œ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋Œ€๋Ÿ‰์œผ๋กœ ์‚ฌ์šฉ ์ฟผ๋ฆฌ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ์ตœ์ ํ™”ํ•˜๋Š” ์Šค๋ ˆ๋“œ ์ฟผ๋ฆฌ๊ฐ€ ์‹ค์ œ ํŒŒ์ผ์„ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ํŒŒํ‹ฐ์…˜์˜ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋งŒ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฐ์ถœํ•˜๋„๋ก ์ตœ์ ํ™” ์ง„ํ–‰ spark.sql.optimizer.metadataOnly=true ์ผ ๋•Œ ์ตœ์ ํ™” ์ง„ํ–‰ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ์ฟผ๋ฆฌ์˜ ๊ฒ€์ƒ‰ ๋Œ€์ƒ ํŒŒํ‹ฐ์…˜ ์ˆ˜๊ฐ€ ๋งŽ์•„์„œ ํŒŒํ‹ฐ์…˜ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” ๋ถ€ํ•˜๊ฐ€ ๋†’์•„์ง€๋ฉด์„œ GC๊ฐ€ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐœ์ƒํ•จ ํŒŒํ‹ฐ์…˜ ์ˆ˜๊ฐ€ ๋งŽ์€ ํ…Œ์ด๋ธ”์„ ์ฒ˜๋ฆฌํ•  ๋•Œ๋Š” spark.sql.optimizer.metadataOnly=false๋กœ ์„ค์ • 2-๋„ค์ด๋ฒ„์˜ ํด๋Ÿฌ์Šคํ„ฐ ์žฅ์•  ๋Œ€์‘ ๋„ค์ด๋ฒ„์—์„œ ๋ฐœํ‘œํ•œ ์ž๋ฃŒ๋ฅผ ํ†ตํ•ด ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์šด์˜ํ•˜๋ฉด์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์‚ฌํ•ญ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ ๋„ค์ž„๋…ธ๋“œ๋Š” ํž™๋ฉ”๋ชจ๋ฆฌ์— HDFS์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ํŒŒ์ผ์˜ ๋ฉ”ํƒ€์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ JVM์˜ ํžˆํ”„ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ(๋งŒ ๋ธ”๋ก๋‹น 1G์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ฌ์šฉ)์— ๋”ฐ๋ผ ์ „์ฒด ํŒŒ์ผ, ๋ธ”๋ก์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ œํ•œ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ, ๋ธ”๋ก์ด ๋งŽ์ด ์ƒ์„ฑ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ์˜ ํ•œ๊ณ„์น˜์— ๋„๋‹ฌํ•˜๋ฉด JVM์˜ ํž™๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋Š˜๋ ค์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์žฌ๊ธฐ ๋™ํ•ด์•ผ ํ•˜๊ณ , ๋„ค์ž„๋…ธ๋“œ๋Š” ์žฌ๊ธฐ๋™ ํ•  ๋•Œ ๋ธ”๋ก ์ •๋ณด๋ฅผ ์žฌ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด fsimage, edits ํŒŒ์ผ์„ ์ฝ์–ด์„œ ๋ธ”๋ก ์ •๋ณด๋ฅผ ์žฌ๊ตฌ์ถ•ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฐ์ดํ„ฐ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๋ธ”๋ก ์ •๋ณด๋ฅผ ๋ฐ›์•„์„œ ๊ฒฐ๊ณผ๋ฅผ ์—ฐ๋™ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ํŒŒ์ผ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์„์ˆ˜๋ก ์žฌ๊ธฐ๋™์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ๊ธธ์–ด์ง€๊ณ , ์ด ์‹œ๊ฐ„ ๋™์•ˆ ํด๋Ÿฌ์Šคํ„ฐ์˜ ์šด์˜์ด ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ• Hadoop Archive ์ด์šฉ ํ•˜๋‘ก์€ har ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ์„ ํ•˜๋‚˜๋กœ ๋ฌถ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํŒŒ์ผ ์ •๋ฆฌ ํŒŒ์ผ์˜ ์ ‘๊ทผ์‹œ๊ฐ„์„ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๋œ ํŒŒ์ผ์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ls ๋ช…๋ น์–ด์˜ -u ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผ ์‹œ๊ฐ„(access time)์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ํŒŒ์ผ์ด ๋งŽ์œผ๋ฉด ๋„ค์ž„๋…ธ๋“œ์— ๋ถ€ํ•˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. hadoop fs -ls -u /user/ oiv(offline fsimage viewer)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผ ์‹œ๊ฐ„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค์ž„๋…ธ๋“œ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•œ ๋‚ด์šฉ์„ ๋ถ„์„ํ•˜์—ฌ ์ ‘๊ทผ์‹œ๊ฐ„์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. hdfs oiv HDFS ์—ฐํ•ฉ(Federation) ์ด์šฉ HDFS ์—ฐํ•ฉ ๊ธฐ๋Šฅ์€ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณ„๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋„ค์ž„๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. hdfs:///user, hdfs:///datas, hdfs:///temps ๊ฐ™์€ 3๊ฐœ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ฐ๊ฐ์˜ ๋„ค์ž„๋…ธ๋“œ๋กœ ์„œ๋น„์Šคํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฐ„์—๋Š” cp, mv๋ฅผ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. distcp๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์˜ฎ๊ฒจ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ๋จธ์ง€(merge) ๊ธฐ๋Šฅ ์ด์šฉ ํ•˜์ด๋ธŒ๋Š” ์ตœ์ข… ์ž‘์—… ํŒŒ์ผ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ํ™•์ธํ•˜์—ฌ, ์ง€์ •ํ•œ ๊ธฐ์ค€ ์ดํ•˜์˜ ํŒŒ์ผ์„ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ๋ฌถ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Map, Reduce ์ž‘์—… ์ดํ›„ Merge ์ž‘์—…์ด ์ถ”๊ฐ€์ ์œผ๋กœ ์ƒ์„ฑ๋˜์–ด ๊ฒฐ๊ณผ ํŒŒ์ผ์„ ๋จธ์ง€ ํ•ฉ๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ ์žฅ์•  ๋Œ€์‘ ๋„ค์ž„๋…ธ๋“œ ์žฅ์•  HDFS๋Š” ๋„ค์ž„๋…ธ๋“œ๊ฐ€ SPOF๋กœ ๋„ค์ž„๋…ธ๋“œ์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํšŒํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฃผํ‚คํผ๋ฅผ ์ด์šฉํ•ด HDFS๋ฅผ HA ๊ตฌ์„ฑ์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์‚ฌ๋ก€ 1: ๋ฐ์ดํ„ฐ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์— ์žฌ์‹œ์ž‘ํ•˜์—ฌ ๋„ค์ž„๋…ธ๋“œ์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•จ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์— ์žฌ์‹œ์ž‘ํ•˜์—ฌ ๋„ค์ž„๋…ธ๋“œ์— ๋ฐ์ดํ„ฐ๋…ธ๋“œ์˜ ๋ธ”๋ก ๋ฆฌํฌํŒ…๊ณผ ๋ธ”๋ก ๋ฆฌํฌํŒ…์„ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ๋‚จ๊ธฐ๋Š” INFO ๋กœ๊ทธ, ์ฃผํ‚คํผ์˜ ๋„ค์ž„๋…ธ๋“œ ํ—ฌ์Šค๋ชจ๋‹ˆํ„ฐ๋ง RPC ํฌํŠธ์™€ ๋ธ”๋ก ๋ฆฌํฌํŒ… ํฌํŠธ๊ฐ€ ๋™์ผํ•˜์—ฌ ์‘๋‹ต์‹œ๊ฐ„์ด ์ดˆ๊ณผ๋˜๋ฉด์„œ ๋„ค์ž„๋…ธ๋“œ์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. HDFS์˜ ๋กœ๊ทธ๋ฅผ TRACE๋กœ ๋ณ€๊ฒฝํ•˜๊ณ , ์ฃผํ‚คํผ์˜ RPC ํฌํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋…ธ๋“œ ๋งค๋‹ˆ์ € ์žฅ์•  ์‚ฌ๋ก€ 1: ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ์ด์ „์œผ๋กœ ์ •๋ณด๊ฐ€ ์ œ๋Œ€๋กœ ๋ฐ˜์˜๋˜์ง€ ์•Š์•„์„œ ์žฅ์•  ๋ฐœ์ƒ HA ๊ตฌ์„ฑ๋œ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ๋…ธ๋“œ ๋งค๋‹ˆ์ €๊ฐ€ ์ค‘๋‹จ๋œ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์˜ ์ •๋ณด๋ฅผ ๊ณ„์† ์บ์Šํ•˜์—ฌ ๋…ธ๋“œ ๋งค๋‹ˆ์ €์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•กํ‹ฐ๋ธŒ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์™€ ์Šคํƒ ๋ฐ”์ด ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์ž‘์—…์˜ ์•ˆ์ •์„ฑ์„ ํ™•๋ณดํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ์ž‘์—…์€ ์Šคํ…Œ์ด์ง€์—์„œ ํ…Œ์ŠคํŠธ ํ›„ ์šด์˜์— ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ € ์žฅ์•  ์‚ฌ๋ก€ 1: ์ฃผํ‚คํผ๊ฐ€ ์‘๋‹ตํ•˜์ง€ ์•Š์•„์„œ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๊ฐ€ ์ž‘์—…์˜ ์ •๋ณด๋ฅผ ์ฃผํ‚คํผ์— ๋ณด๊ด€ํ•  ๋•Œ ์ฃผํ‚คํผ์— ๋„ˆ๋ฌด ๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณด๊ด€๋˜์–ด ์ฃผํ‚คํผ๊ฐ€ ์‘๋‹ต์„ ํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์™„๋ฃŒ๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ •๋ณด๋Š” ์ €์žฅํ•˜์ง€ ์•Š๊ณ , ์ฃผํ‚คํผ์˜ ๋ฒ„ํผ ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋…ธ๋“œ OS ์—…๊ทธ๋ ˆ์ด๋“œ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋…ธ๋“œ์˜ OS ์—…๊ทธ๋ ˆ์ด๋“œ๋Š” ๋‹ค์Œ์„ ๊ณ ๋ คํ•˜์—ฌ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. YARN ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰ ํ™˜๊ฒฝ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋˜๋Š” OS ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •์— ์œ ์˜ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. OS ๋ณ„๋กœ /usr/bin/๊ณผ ๊ฐ™์€ ์‹คํ–‰ ๋ช…๋ น์–ด์˜ ์œ„์น˜๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•˜๋‘ก ์ž‘์—… ๋ฆฌ์†Œ์Šค ํ•˜๋‘ก์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์ž‘์—…์€ ์–ด๋–ค OS์—์„œ ์‹คํ–‰๋ ์ง€ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. OS ๋ฒ„์ „์— ๋งž๋Š” ์ž‘์—… ๋ฆฌ์†Œ์Šค๊ฐ€ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. OS์— ์˜์กดํ•˜๋Š” ์ž‘์—… OS์— ์„ค์น˜๋œ ๊ธฐ๋ณธ ๋ช…๋ น์–ด์˜ ๋ฒ„์ „์— ๋”ฐ๋ผ ์ž‘์—… ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฒ„์ „์—์„œ ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ์˜ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ์ž‘์—…์€ ์‚ฌ์ „์— ์—ฌ๋Ÿฌ ๋ฐฉ๋ฉด์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๊ณ , YARN ์ปจํ…Œ์ด๋„ˆ์˜ ์ž‘์—… ํ™˜๊ฒฝ์„ ํ†ต์ผํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ์— ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ์žฅ๋น„๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ œ๊ฑฐ ํ›„ ์—…๊ทธ๋ ˆ์ด๋“œ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  ๋„ค์ด๋ฒ„ - ๋ฉ€ํ‹ฐํ…Œ๋„ŒํŠธ ํ•˜๋‘ก ํด๋Ÿฌ์Šคํ„ฐ ์šด์˜ ๊ฒฝํ—˜๊ธฐ 3-๋„ค์ž„๋…ธ๋“œ ํžˆํ”„ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ/์šฉ๋Ÿ‰ ๊ณ„์‚ฐ๋ฒ• HDFS๋Š” ๋„ค์ž„๋…ธ๋“œ์˜ ํžˆํ”„ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ๋ฐฑ๋งŒ ๋ธ”๋ก๋‹น 1GB์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋ก์˜ ๊ฐœ์ˆ˜, ๋ธ”๋ก์˜ ๊ธฐ๋ณธ ํฌ๊ธฐ, ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ๋„ค์ž„๋…ธ๋“œ์˜ ํžˆํ”„ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ™ฉ์„ ๊ณ ๋ คํ•ด์„œ ์ ์ ˆํ•˜๊ฒŒ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ์‚ฌ์ด์ฆˆ์— ๋”ฐ๋ฅธ ํžˆํ”„ ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ณธ ๋ธ”๋ก ํฌ๊ธฐ๊ฐ€ 128MB, ๋ณต์ œ ๊ฐœ์ˆ˜๋Š” 1์ผ ๋•Œ ๋„ค์ž„๋…ธ๋“œ์˜ ํžˆํ”„ ๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ฒฝ์šฐ์™€ ๊ฐ™์ด ์ ์ ˆํ•œ ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. 1024MB ํŒŒ์ผ 1๊ฐœ ํŒŒ์ผ inode: 1 ๊ฐœ ๋ธ”๋ก ๊ฐœ์ˆ˜: 1024 / 128 = 8๊ฐœ 9๊ฐœ์˜ ๊ฐ์ฒด * 150 byte = 1,350 byte 128MB ํŒŒ์ผ 8๊ฐœ ํŒŒ์ผ inode: 8๊ฐœ ๋ธ”๋ก ๊ฐœ์ˆ˜: 128 / 128 = 1 * 8 = 8๊ฐœ 16๊ฐœ์˜ ๊ฐ์ฒด * 150 byte = 2,400 byte 1MB ํŒŒ์ผ 1024๊ฐœ ํŒŒ์ผ inode: 1024๊ฐœ ๋ธ”๋ก ๊ฐœ์ˆ˜: 1 /128 = 1 * 1024 = 1024๊ฐœ 2048๊ฐœ์˜ ๊ฐ์ฒด * 150 byte = 307,200 byte ๋ณต์ œ ๊ฐœ์ˆ˜์— ๋”ฐ๋ฅธ ํžˆํ”„ ๋ฉ”๋ชจ๋ฆฌ ๋‹ค์Œ์€ ๋ณต์ œ ๊ฐœ์ˆ˜์— ๋”ฐ๋ฅธ ํžˆํ”„ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ์˜ ์ฐจ์ด์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค. ๋ธ”๋ก ์‚ฌ์ด์ฆˆ: 128MB, ๋ณต์ œ๊ฐœ์ˆ˜ 1 ํ˜ธ์ŠคํŠธ 200๊ฐœ, ๋ธ”๋ก: 128MB, ๋ณต์ œ๊ฐœ์ˆ˜: 1 ํ˜ธ์ŠคํŠธ๋‹น 24TB 200 * 24 TB = 4800 TB ๋ณต์ œ๊ฐœ์ˆ˜ 1์ด๋ฏ€๋กœ ๋ธ”๋ก๋‹น 128MB ํ•„์š”ํ•จ 4800TB / 128MB = 36,000,000 ๋ฐฑ๋งŒ(1,000,000)๋ธ”๋ก๋‹น 1G 36G์˜ ํžˆํ”„ ์‚ฌ์ด์ฆˆ๊ฐ€ ํ•„์š”ํ•จ ๋ธ”๋ก ์‚ฌ์ด์ฆˆ: 128MB, ๋ณต์ œ๊ฐœ์ˆ˜ 3 ํ˜ธ์ŠคํŠธ 200๊ฐœ, ๋ธ”๋ก: 128MB, ๋ณต์ œ๊ฐœ์ˆ˜: 3 ํ˜ธ์ŠคํŠธ๋‹น 24TB 200 * 24 TB = 4800 TB ๋ณต์ œ๊ฐœ์ˆ˜ 3์ด๋ฏ€๋กœ ๋ธ”๋ก๋‹น 384MB ํ•„์š”ํ•จ 4800TB / 384MB = 12,000,000 ๋ฐฑ๋งŒ(1,000,000)๋ธ”๋ก๋‹น 1G 12G์˜ ํžˆํ”„ ์‚ฌ์ด์ฆˆ๊ฐ€ ํ•„์š”ํ•จ 4-๋””์Šคํฌ ์‚ฌ์ด์ฆˆ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ๋Š” ๋””์Šคํฌ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ๋งŽ์€ ๋””์Šคํฌ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋…ธ๋“œ ๋งค๋‹ˆ์ €์˜ ๋””์Šคํฌ๋„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋”ฐ๋ผ ๋งŽ์€ ์šฉ๋Ÿ‰์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ ์ ˆํ•œ ์šฉ๋Ÿ‰์„ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ŠคํŒŒํฌ ์ŠคํŠธ๋ฆฌ๋ฐ ์ž‘์—…์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋กœ๊ทธ ์ •๋ณด๋„ ์ผ์ • ๊ธฐ๊ฐ„ ์Œ“์ด๊ฒŒ ๋˜๋ฉด ๋ฌด์‹œํ•  ์ˆ˜ ์—†๋Š” ์ˆ˜์ค€์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์— ์–ด๋–ค ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๋”์šฑ ๋งŽ์€ ์šฉ๋Ÿ‰์ด ์Œ“์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค๋‚˜ ์ŠคํŒŒํฌ ์ž‘์—…์—์„œ ์…”ํ”Œ ๋‹จ๊ณ„์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฐ์ดํ„ฐ๋„ ์ƒ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์…”ํ”Œ์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰์„ ๋„˜์–ด์„œ ์ž„์‹œ๋กœ ๋””์Šคํฌ์— ์Œ“์ด๊ฒŒ ๋˜๋ฉด ๋””์Šคํฌ ๋ถ€์กฑ์„ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ ์ ˆํ•œ ์šฉ๋Ÿ‰์„ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์šด์˜ํ•˜๋ฉด์„œ ๊ฐ ์ƒํ™ฉ์—์„œ ๋‹จ์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋กœ๊ทธ๋งŒ 60G, ๋‹จ์ผ ์ž‘์—…์˜ ์…”ํ”Œ ๋ฐ์ดํ„ฐ๋งŒ 50G๊ฐ€ ์Œ“์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค๊ณ„์—์„œ ์ด๋Ÿฐ ๋ถ€๋ถ„์„ ๊ณ ๋ คํ•˜๋ฉด ์ข‹๊ฒ ์ง€๋งŒ ๋ชจ๋“  ์ƒํ™ฉ์—์„œ ๊ทธ๋ ‡๋ฐ ๋  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋…ธ๋“œ ๋งค๋‹ˆ์ €์˜ ๋””์Šคํฌ๋„ ์ ๋‹นํ•œ ํฌ๊ธฐ๋กœ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 5-๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ ๋ฐ์ดํ„ฐ๋…ธ๋“œ, ๋…ธ๋“œ ๋งค๋‹ˆ์ €๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์ด์ฆˆ๋„ ์ž˜ ์ƒ๊ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋งŽ์ง€ ์•Š์ง€๋งŒ ์šด์˜ ์ผ์ž๊ฐ€ ๊ธธ์–ด์ง€๊ณ , ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๋„ ์ ์  ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋…ธ๋“œ ๋งค๋‹ˆ์ €์— ํ•  ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์„ค์ •ํ•  ๋•Œ ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์™€ ๊ฐ™์€ ๋…ธ๋“œ์— ๋™์ž‘ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๋ฐ์ดํ„ฐ ๋…ธ๋“œ์™€ ๋…ธ๋“œ ๋งค๋‹ˆ์ € OS ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ƒ๊ฐํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋…ธ๋“œ: 15~20G ๋…ธ๋“œ ๋งค๋‹ˆ์ €: 10~15G OS: ๋ฉ”๋ชจ๋ฆฌ 4G ์—ฌ๊ธฐ์— ์ถ”๊ฐ€ ์—ฌ์œ ๋ถ„ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ƒ๊ฐํ•ด์„œ ๋…ธ๋“œ ๋งค๋‹ˆ์ €์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. <property> <name>yarn.nodemanager.resource.memory-mb</name> <value>102400</value> </property> 3-ํ•˜๋‘ก ์ž‘์—… ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์šด์˜ ํ•˜๋‘ก ์ž‘์—… ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ๋•Œ ํ™•์ธํ•ด์•ผ ํ•  ์‚ฌํ•ญ์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ ์ž‘์—… ์ œ์ถœ ์—ฌ๋ถ€ ํ™•์ธ ํด๋Ÿฌ์Šคํ„ฐ์— ์ž‘์—…์ด ์ œ์ถœ๋˜๊ณ  AM์ด ํ• ๋‹น๋˜๊ณ , AM์ด ์ž‘์—…์„ ๋ถ„์„ํ•˜์—ฌ ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์ด ์‹œ์ž‘๋˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. YARN์— ์ž‘์—…์ด ์ œ์ถœ๋˜์–ด๋„ AM์ด ์‹คํ–‰๋˜์ง€ ์•Š๊ณ , ACCEPTED ์ƒํƒœ๋กœ ๋Œ€๊ธฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ์›์ธ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ์— ์—ฌ์œ ๊ฐ€ ์—†์„ ๋•Œ ์Šค์ผ€์ค„๋Ÿฌ์˜ ์„ค์ •์— ์˜ํ•ด AM์ด ํ• ๋‹น๋˜์ง€ ์•Š์„ ๋•Œ ํด๋Ÿฌ์Šคํ„ฐ์— ์—ฌ์œ ๊ฐ€ ์—†์„ ๋•Œ ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ 100% ํ™œ์šฉ๋˜๊ณ  ์žˆ์–ด์„œ ๋‹ค๋ฅธ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์„ ๋•Œ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ฆ์„คํ•˜์ง€ ์•Š์œผ๋ฉด ์ž‘์—… ์†๋„๋ฅผ ๋†’์ด๊ธฐ ํž™๋“ญ๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ฆ์„คํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์„ ํšจ์œจํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์Šค์ผ€์ค„๋Ÿฌ์˜ ์„ค์ •์— ์˜ํ•ด AM์ด ํ• ๋‹น๋˜์ง€ ์•Š์„ ๋•Œ ์Šค์ผ€์ค„๋Ÿฌ๋Š” ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ AM์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋น„์œจ์„ ์ œํ•œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. AM์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž์›์„ ์ œํ•œํ•˜์—ฌ ์‹ค์ œ ๋งต๋ฆฌ๋“€์Šค๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งŽ์ด ์ƒ์„ฑํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ AM์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋น„์œจ์„ ๋Š˜๋ ค์„œ ์ž์›์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด๋„ ๋˜๊ณ , ๊ธฐ์กด ํ์™€ ๋‹ค๋ฅธ ์„ค์ •์„ ๊ฐ€์ง€๋Š” ํ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๊ธฐ์กด ์„ค์ •์— ์˜ํ–ฅ์ด ์—†๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž‘์—… ์‹œ๊ฐ„ ํ™•์ธ ๋งต๋ฆฌ๋“€์Šค ์ž‘์—…์ด RUNNING ์ƒํƒœ์—์„œ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ฒฝ์šฐ์—๋Š” ๋‹ค์–‘ํ•œ ์›์ธ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ 100% ํ™œ์šฉํ•˜์ง€ ๋ชปํ•  ๋•Œ ์›์ฒœ ๋ฐ์ดํ„ฐ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋„ˆ๋ฌด ํด ๋•Œ ๋งตํผ, ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜๊ฐ€ ์ ์ ˆํ•˜์ง€ ๋ชปํ•  ๋•Œ ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•  ๋•Œ ์ •๊ทœ์‹ ํ•จ์ˆ˜ ์ฒ˜๋ฆฌ์— ์˜ค๋ž˜ ๊ฑธ๋ฆด ๋•Œ ๋งŽ์€ ํŒŒ์ผ ์ˆ˜๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ ๋„คํŠธ์›Œํฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋งŽ์„ ๋•Œ ํ•˜๋‚˜์˜ ๋…ธ๋“œ์— ์ž‘์—…์ด ๋ชฐ๋ฆด ๊ฒฝ์šฐ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ 100% ํ™œ์šฉํ•˜์ง€ ๋ชปํ•  ๋•Œ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ 100% ํ™œ์šฉํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ •์„ ๋”ฐ๋ฅด๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ปคํŒจ์‹œํ‹ฐ ์Šค์ผ€์ค„๋Ÿฌ๋Š” maximum-capacity ์„ค์ •์„ ํ†ตํ•ด ํ๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์ž์›์˜ ์ƒํ•œ์„ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„ค์ •์„ ํ™•์ธํ•˜์—ฌ ์ ์ ˆํ•œ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์›์ฒœ ๋ฐ์ดํ„ฐ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋„ˆ๋ฌด ํด ๋•Œ ์ž‘์—…์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ์›์ฒœ ๋ฐ์ดํ„ฐ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋„ˆ๋ฌด ํฐ ๊ฒฝ์šฐ ์ž‘์—…์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ์ž‘์—…์˜ ์ž…๋ ฅ ํŒŒ์ผ ํฌ๊ธฐ์™€ ๊ฐœ์ˆ˜๊ฐ€ ํฐ ๊ฒฝ์šฐ์—๋Š” ์ž‘์—… ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ ์ž‘์—…์€ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์—… ์กฐ๊ฑด์„ ์ œํ•œํ•˜์—ฌ ์›์ฒœ ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์—ฌ ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ์˜ ํŒŒํ‹ฐ์…”๋‹, ๋ฒ„์ผ“ํŒ…์„ ์ ์ ˆํžˆ ํ™œ์šฉํ•˜์—ฌ ์ผ ๋‹จ์œ„, ํฐ ์นดํ…Œ๊ณ ๋ฆฌ ๋‹จ์œ„๋กœ ํŒŒ์ผ์„ ๋‚˜๋ˆ„์–ด ์ค€๋‹ค๋ฉด ์ž‘์—… ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ž…๋ ฅํ•˜๋Š” ํŒŒ์ผ ์‚ฌ์ด์ฆˆ์™€ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งตํผ, ๋ฆฌ๋“€์„œ ๊ฐœ์ˆ˜๊ฐ€ ์ ์ ˆํ•˜์ง€ ๋ชปํ•  ๋•Œ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ์— ๋น„ํ•ด ๋งคํผ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ž‘๊ฑฐ๋‚˜, ๋ฆฌ๋“€์„œ๋กœ ์ž…๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ ํฌ๊ธฐ์— ๋น„ํ•ด ๋ฆฌ๋“€์„œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ž‘์„ ๋•Œ ์ž‘์—…์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ tez๋Š” 1G๋‹น 1๊ฐœ์˜ ๋งคํผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ์••์ถ•๋ฅ  ๋งตํผ์—์„œ ์ฒ˜๋ฆฌ๋˜๋Š” ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ 512MB๋‚˜ 256MB๋กœ ์ค„์—ฌ์ฃผ๋ฉด ์„ฑ๋Šฅ์ด ๋นจ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ์˜ UDTF ํ•จ์ˆ˜์— ์˜ํ•ด ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ณด๋‹ค ๋งŽ์€ ์ˆ˜์˜ ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒฝ์šฐ์—๋„ ๋งตํผ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ ค์ฃผ๋ฉด ์ž‘์—…์‹œ๊ฐ„์ด ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•  ๋•Œ ๋งตํผ, ๋ฆฌ๋“€์„œ์—์„œ ์‹คํ–‰๋˜๋Š” ์ž‘์—…์ด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๊ณผ๋„ํ•œ GC๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์ž‘์—… ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ด๋ธŒ์—์„œ UDAF, UDTF ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋‚ด๋ถ€์—์„œ ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๊ณ , ๋ฉ”๋ชจ๋ฆฌ ๋ฒ„ํผ๋ฅผ ๋„˜์–ด์„œ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์ด๋ฅผ ๋กœ์ปฌ์— ์ž„์‹œํŒŒ์ผ๋กœ ์“ฐ๊ณ , ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด์„œ ์ž‘์—…์‹œ๊ฐ„์ด ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ๋Š” JVM ์˜ต์…˜์„ ์ˆ˜์ •ํ•˜์—ฌ ํž™๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋Š˜๋ ค์ฃผ๋ฉด ์ž‘์—…์‹œ๊ฐ„์ด ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. ์ •๊ทœ์‹ ํ•จ์ˆ˜ ์ฒ˜๋ฆฌ์— ์˜ค๋ž˜ ๊ฑธ๋ฆด ๋•Œ ์ •๊ทœ์‹์€ ํŽธ๋ฆฌํ•˜์ง€๋งŒ ํŠน์ • ์กฐ๊ฑด์—์„œ ์ •๊ทœ์‹์˜ ์—ฐ์‚ฐ์ด ๋งŽ์•„์ ธ์„œ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์„ ๋ณด์žฅํ•˜๋Š” ์ •๊ทœ์‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๊ฑฐ๋‚˜, ์ •๊ทœ์‹ ๋Œ€์ƒ์ด ๋˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ œํ•œํ•˜๋Š” ๋ฐฉ๋ฒ• ๋“ฑ์œผ๋กœ ์ •๊ทœ์‹์„ ํŠœ๋‹ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŽ์€ ํŒŒ์ผ ์ˆ˜๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ ๋งต๋ฆฌ๋“€์Šค, ์…”ํ”Œ ๋‹จ๊ณ„์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ž„์‹œ ํŒŒ์ผ์ด ๋งŽ์•„์ง€๋ฉด ์žฆ์€ I/O๋กœ ์ธํ•˜์—ฌ ์ž‘์—…์ด ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ž„์‹œ ํŒŒ์ผ์„ ์••์ถ•ํ•˜๊ฑฐ๋‚˜, ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ž„์‹œ ํŒŒ์ผ์ด ๋งŽ์•„์ง€๋ฉด ์žฆ์€ I/O๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์ž‘์—…์ด ๋Š๋ ค์ง‘๋‹ˆ๋‹ค. ์ž„์‹œ ํŒŒ์ผ์ด ๋งŽ์•„์ ธ์„œ ๋…ธ๋“œ์˜ i-node๊ฐ€ ๋ถ€์กฑํ•ด์ง€๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์Šคํ„ฐ ๋…ธ๋“œ์˜ ๊ฒฝ์šฐ ๊ฐ์ข… ์„œ๋น„์Šค์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋กœ๊ทธ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋“ฑ ๋งŽ์€ ํŒŒ์ผ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์ž‘์—…์šฉ ๋””์Šคํฌ๋ฅผ ๋ถ„๋ฆฌํ•ด ์ฃผ๋Š” ๊ฒƒ๋„ ์ข‹์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋งŽ์„ ๋•Œ ๋งต๋ฆฌ๋“€์Šค์™€ HDFS๋Š” ์ž‘์—… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ณต์‚ฌ, HDFS ๋ธ”๋ก ๋ณต์‚ฌ, ๋‚ด๋ถ€ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ ๋“ฑ ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์ด ๋Š์ž„์—†์ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ S3, NAS ๊ฐ™์€ ์™ธ๋ถ€ ์ €์žฅ์†Œ์— ์ €์žฅ๋œ ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•  ๋•Œ๋„ ๋„คํŠธ์›Œํฌ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋„คํŠธ์›Œํฌ ๋Œ€์—ญํญ์ด ์ž‘์„ ๋•Œ๋Š” ๋ฐ์ดํ„ฐ ์ด๋™ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ ์ ˆํ•œ ๋„คํŠธ์›Œํฌ ๋Œ€์—ญํญ์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋…ธ๋“œ์— ์ž‘์—…์ด ๋ชฐ๋ฆด ๊ฒฝ์šฐ ์Šค์ผ€์ค„๋Ÿฌ์˜ ๋…ธ๋“œ ๋ ˆ์ด๋ธ” ๊ตฌ์„ฑ์œผ๋กœ ์ธํ•˜์—ฌ ํŠน์ • ๋…ธ๋“œ๋กœ ์ž‘์—…์ด ๋ชฐ๋ฆฌ๋Š” ๊ฒฝ์šฐ ์ž‘์—… ์‹œ๊ฐ„์ด ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋…ธ๋“œ์— ์ž‘์—…์ด ์ง‘์ค‘๋˜๋ฉด ๊ณผ๋ถ€ํ•˜๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ํ†ต์‹  ํฌํŠธ๊ฐ€ ๋งˆ๋น„๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž‘์—…์„ ์ ์ ˆํžˆ ๋ถ„๋ฐฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž‘์—… ํŠœ๋‹ ๋งต๋ฆฌ๋“€์Šค, ํ•˜์ด๋ธŒ ์ž‘์—…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งต๋ฆฌ๋“€์Šค ๋งคํผ, ๋ฆฌ๋“€์„œ ์ˆ˜ ์„ค์ • ์ •๋ ฌ ์†์„ฑ ํŠœ๋‹ ์ปดํŒŒ์ด๋„ˆ ํด๋ž˜์Šค ์ ์šฉ ๋งต ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ ์••์ถ• small file ๋ฌธ์ œ ์ˆ˜์ • ํ•˜์ด๋ธŒ ์ž‘์—… ์—”์ง„ ์„ ํƒ: TEZ ์—”์ง„ ์‚ฌ์šฉ ํŒŒ์ผ ์ €์žฅ ํฌ๋งท: ORC ํŒŒ์ผ ์‚ฌ์šฉ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐฉ์‹: ๋ฒกํ„ฐํ™”(Vectorization) ์‚ฌ์šฉ ๋ฐ์ดํ„ฐ ์ €์žฅ ํšจ์œจํ™”: ํŒŒํ‹ฐ์…”๋‹, ๋ฒ„์ผ“ํŒ… ์‚ฌ์šฉ ํ†ต๊ณ„์ •๋ณด ์ด์šฉ: ํ•˜์ด๋ธŒ stat ์‚ฌ์šฉ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ด์šฉ: CBO YARN: ์ž‘์—… ํ ์„ค์ • 1-ํ•˜์ด๋ธŒ ์„ฑ๋Šฅ ์ตœ์ ํ™”-ETL ์„ฑ๋Šฅ ํ–ฅ์ƒ ํŒ-NC soft NC ์†Œํ”„ํŠธ์—์„œ ๊ณต๊ฐœํ•œ Hive์™€ RDBMS์˜ ETL ํŒ์„ ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค. ETL ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ํŒ๋“ค HiveQL ์ธก๋ฉด์—์„œ ์„ฑ๋Šฅ ํ–ฅ์ƒํ•˜๊ธฐ ์กฐ๊ฑด์ ˆ ๋‚ด์˜ UDF ์ œ๊ฑฐ where ์กฐ๊ฑด์ ˆ์— UDF๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•˜์ด๋ธŒ ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ์ •๋ณด๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์—†์–ด์„œ ํŒŒํ‹ฐ์…˜ ๊ฐ€์ง€์น˜๊ธฐ(Partiton Pruning)์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ๋Šฅ์— ๋‚˜์œ ์˜ํ–ฅ์„ ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. DISTINCT COUNT ์—ฐ์‚ฐ ํ”ผํ•˜๊ธฐ COUNT(DISTINCT column) ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ํ•˜๋‚˜์˜ ๋ฆฌ๋“€์„œ์—์„œ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์—…์ด ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. GROUP BY ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. JOIN ์‚ฌ์šฉ ์‹œ ๊ณ ๋ คํ•  ์  ํ•˜์ด๋ธŒ ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์˜ ํ…Œ์ด๋ธ”์ด ๊ฐ€์žฅ ํฌ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ํฐ ๋ฐ์ดํ„ฐ์˜ ํ…Œ์ด๋ธ”์„ ๋งˆ์ง€๋ง‰์— ๋†“๊ฑฐ๋‚˜ /*+STREAMTABLE(a)*/ ์˜ต์…˜์„ ์ด์šฉํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. ์•„์›ƒ ์กฐ์ธ ์‹œ ์กฐ์ธ ์ˆ˜ํ–‰ ํ›„ WHERE ์กฐ๊ฑด์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์ฒฉ SELECT ๋ฌธ์„ ์ด์šฉํ•ด ๋จผ์ € ๋ฐ์ดํ„ฐ๋ฅผ ํ•„ํ„ฐ๋ง ํ›„ ์กฐ์ธ์„ ์ง„ํ–‰ํ•˜๋„๋ก ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. SELECT ์‚ฌ์šฉ ์‹œ ๊ณ ๋ ค ์‚ฌํ•ญ *๋ฅผ ์ด์šฉํ•ด ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ์•Š๊ณ , ํ•„์š”ํ•œ ์นผ๋Ÿผ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. RDBMS ์ฟผ๋ฆฌ ์ธก๋ฉด์—์„œ ์„ฑ๋Šฅ ํ–ฅ์ƒํ•˜๊ธฐ ๋ถˆํ•„์š”ํ•œ ์ธ๋ฑ์Šค ์ค„์ด๊ธฐ ์ธ๋ฑ์Šค๋Š” DDL ๋ฌธ์˜ ์„ฑ๋Šฅ์— ์•ˆ ์ข‹์€ ์˜ํ–ฅ์„ ์ฃผ๊ณ , Nested Loops Join์„ ์ด์šฉํ•˜๊ฒŒ ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ ์ ˆํ•œ ์ˆ˜์˜ ์ธ๋ฑ์Šค๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. WHERE ์กฐ๊ฑด์—์„œ ํ•จ์ˆ˜ ํ”ผํ•˜๊ธฐ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์ •ํ™•ํ•œ ์นด๋””๋‚ ๋ฆฌํ‹ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ์•ˆ ์ข‹์€ ์„ ํƒ์„ ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. OR ์กฐ๊ฑด ํ”ผํ•˜๊ธฐ OR ์กฐ๊ฑด์€ ์ตœ์ ํ™”๋˜์ง€ ์•Š์€ ์‹คํ–‰๊ณ„ํš์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. OR ์กฐ๊ฑด์€ UNION ๋ฌธ์„ ๋Œ€์ฒดํ•˜๋ฉด ๋” ๋น ๋ฅผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ETL ์ž‘์—… ์ธก๋ฉด์—์„œ ์„ฑ๋Šฅ ํ–ฅ์ƒํ•˜๊ธฐ 2-HDFS์˜ Heterogeneous Storage ์ ์šฉ๊ธฐ - Pinpoint ๋น„์šฉ ํšจ์œจํ™” ๋„ค์ด๋ฒ„์—์„œ ๊ณต๊ฐœํ•œ HDFS๋ฅผ ์ด์ข… ์ €์žฅ ๊ณต๊ฐ„์„ ์ด์šฉํ•˜์—ฌ ์„ฑ๋Šฅ์„ ๋†’์ด๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. # HDFS์˜ Heterogeneous Storage ์ ์šฉ๊ธฐ - Pinpoint ๋น„์šฉ ํšจ์œจํ™” ์ด์ข… ์ €์žฅ ๊ณต๊ฐ„(Heterogeneous Storage) ์ ์šฉ ์Šคํ† ๋ฆฌ์ง€ ์œ ํ˜• ์ง€์ • ์Šคํ† ๋ฆฌ์ง€ ์ •์ฑ… ์ง€์ • ๋ฐ์ดํ„ฐ ์ด๋™ ์ ์šฉ ํšจ๊ณผ ๋น„์šฉ ํšจ์œจ์„ฑ ํ–ฅ์ƒ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์†๋„ ํ–ฅ์ƒ ๋ฐ์ดํ„ฐ ํ‹ฐ์–ด๋ง ๊ฐœ๋ฐœ ๋น„์šฉ๊ณผ ์šด์˜์˜ ์ตœ์†Œํ™” 9-์ฐธ๊ณ  ์„ค์ • ์ž๋ฃŒ HDFS NameNode HA ๊ตฌ์„ฑ Hadoop3 Installing hadoop3 - ํ•˜๋‘ก 3 ์„ค์ • ๊ตญ๋‚ด ์ž๋ฃŒ ๋ผ์ธ - ๋ฐ์ดํ„ฐ ์—”์ง€๋‹ˆ์–ด๋ง ๊ด€๋ จ ์†Œํ”„ํŠธ์›จ์–ด ์žฅ์•  ๋Œ€์‘ ์‚ฌ๋ก€ ํ•ด์™ธ ์ž๋ฃŒ Shifting to Hive Part II: Best Practices and Optimizations ํ•˜์ด๋ธŒ ์ตœ์ ํ™” ๋ฐฉ์•ˆ<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: WikibooksHaskell ### ๋ณธ๋ฌธ: ์†Œ๊ฐœ ์ด ์ฑ…์€ wikibooks์˜ ํ•˜์Šค ์ผˆ ์ฑ…์„ ๋ฒˆ์—ญํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์›๋ฌธ์˜ ๋ผ์ด์„ ์Šค๋ฅผ ๋”ฐ๋ผ ์ด ์ฑ…์˜ ๋ผ์ด์„ ์Šค๋„ CCL BY-SA(์ €์ž‘์ž ํ‘œ์‹œ, ๋™์ผ ์กฐ๊ฑด ๋ณ€๊ฒฝ ํ—ˆ๋ฝ, ์ƒ์—…์  ์ด์šฉ ์ œํ•œ ์—†์Œ)์ž…๋‹ˆ๋‹ค. 1์ฐจ ๋ฒˆ์—ญ์„ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ผ ๋•Œ ์›๋ฌธ์ด ์•„์˜ˆ ๋ˆ„๋ฝ๋œ ๊ฒƒ๋“ค์€ ๊ฑด๋„ˆ๋›ฐ์–ด ๋ถ™์˜€์Šต๋‹ˆ๋‹ค. Beginner's Track์€ ๋Œ€๋ถ€๋ถ„ ๋‚ด์šฉ์ด ์™„์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. Advanced Track์€ ์ €๋„ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๊ณ  ์˜ฎ๊ธด ๋‚ด์šฉ์ด ๋งŽ์•„์„œ ์ฐธ๊ณ ๋งŒ ํ•˜๊ณ  ์›๋ฌธ์„ ์ฝ๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์›๋ฌธ๋„ ๋ฏธ์™„์„ฑ์ธ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. Haskell in Practice์€ ๋‚œํ•ดํ•œ ๋‚ด์šฉ์€ ์—†์ง€๋งŒ ์›๋ฌธ์ด ๋ฏธ์™„์„ฑ์ด๊ฑฐ๋‚˜ ๋‚ก์€ ์ •๋ณด์ธ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ง„ํ–‰ ์ƒํ™ฉ ์ดˆ๊ธ‰๋ฐ˜ - 34/34 ์™„๋ฃŒ ํ•˜์Šค ์ผˆ ๊ธฐ์ดˆ - 9/9 ์™„๋ฃŒ ํ•˜์Šค ์ผˆ ์ดˆ๊ธ‰ - 9/9 ์™„๋ฃŒ ํ•˜์Šค ์ผˆ ์ค‘๊ธ‰ - 7/7 ์™„๋ฃŒ ๋ชจ๋‚˜๋“œ - 9/9 ์™„๋ฃŒ ๊ณ ๊ธ‰๋ฐ˜ - 28/38 ์™„๋ฃŒ ํ•˜์Šค ์ผˆ ๊ณ ๊ธ‰ - 11/16 ์™„๋ฃŒ (5๊ฐœ๋Š” ์›๋ฌธ ์—†์Œ) ํƒ€์ž…๊ณผ์˜ ์œ ํฌ - 6/7 ์™„๋ฃŒ (1๊ฐœ๋Š” ์›๋ฌธ ์—†์Œ) ์—ฌ๋Ÿฌ ์ด๋ก ๋“ค - 4/6 ์™„๋ฃŒ (2๊ฐœ๋Š” ์›๋ฌธ ์—†์Œ) ํ•˜์Šค ์ผˆ ์„ฑ๋Šฅ - 7/9 ์™„๋ฃŒ (2๊ฐœ๋Š” ์›๋ฌธ ์—†์Œ) ํ•˜์Šค ์ผˆ ์‹ค์ „ - 16/17 ์™„๋ฃŒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ ˆํผ๋Ÿฐ์Šค - 7/7 ์™„๋ฃŒ ์ผ๋ฐ˜ ์ž‘์—… - 5/5 ์™„๋ฃŒ ํŠน์ˆ˜ ์ž‘์—… - 4/5 ์™„๋ฃŒ (1๊ฐœ๋Š” ์›๋ฌธ ์—†์Œ) ๊ธฐํƒ€ ํ•˜์Šค ์ผˆ ์ž๋ฃŒ Real World Haskell ์˜จ๋ผ์ธ ์ฑ… ํ•˜์Šค์ผˆ์œ„ํ‚ค ํƒ€์ž… ๋ฐฑ๊ณผ(HaskellWiki Typeclassopedia) ํ•˜์Šค์ผˆ์„ ๋ฐฐ์šธ ๋•Œ ์•Œ์•˜๋”๋ผ๋ฉด ์ข‹์•˜์„ ๊ฒƒ๋“ค(What I Wish I Knew When Learning Haskell) Parallel and Concurrent Programming in Haskell 1 ์ดˆ๊ธ‰๋ฐ˜ ์—ฌ๊ธฐ์„œ๋Š” ํ•˜์Šค์ผˆ์˜ ๊ธฐ์ดˆ์™€ ์ž์ฃผ ์“ฐ์ด๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ดˆ๊ธ‰๋ฐ˜์„ ๋งˆ์น˜๊ณ  ๋‚˜๋ฉด ๊ฐ„๋‹จํ•œ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์žฅ์€ ์‹ค์ „ ์—ฐ์Šต์„ ์œ„ํ•œ ์—ฐ์Šต๋ฌธ์ œ์™€ ํ•ด๋‹ต์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ๋ชฉ์ฐจ ํ•˜์Šค ์ผˆ ๊ธฐ์ดˆ ํ™˜๊ฒฝ ๊ฐ–์ถ”๊ธฐ(Getting set up) ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜(Variables and functions) ์ง„์œ„ ๊ฐ’(Truth values) ํƒ€์ž…์˜ ๊ธฐ์ดˆ(Type basics) ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ(Lists and tuples) ํƒ€์ž…์˜ ๊ธฐ์ดˆ 2(Type basics II) ๋‹ค์Œ ๊ณผ์ •(Next steps) ์–ดํœ˜ ์Œ“๊ธฐ(Building vocabulary) ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ(Simple input and output) ํ•˜์Šค ์ผˆ ์ดˆ๊ธ‰ ์žฌ๊ท€(Recursion) ๋ฆฌ์ŠคํŠธ ๋ณด์ถฉ ์„ค๋ช…(More about lists) ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ(List processing) ํƒ€์ž… ์„ ์–ธ(Type declarations) ํŒจํ„ด ๋งค์นญ(Pattern matching) ์ œ์–ด ๊ตฌ์กฐ(Control structures) ํ•จ์ˆ˜ ๋ณด์ถฉ ์„ค๋ช…(More on functions) ๊ณ ์ฐจ ํ•จ์ˆ˜(Higher order functions) GHCi ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ(Using GHCi effectively) ํ•˜์Šค ์ผˆ ์ค‘๊ธ‰ ๋ชจ๋“ˆ(Modules) ๋…๋ฆฝ ์‹คํ–‰ ํ”„๋กœ๊ทธ๋žจ(Standalone programs) ๋“ค์—ฌ ์“ฐ๊ธฐ(Indentation) ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณด์ถฉ ์„ค๋ช…(More on datatypes) ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค(Other data structures) ํด๋ž˜์Šค์™€ ํƒ€์ž…(Classes and types) Functor ํด๋ž˜์Šค(The Functor class) ๋ชจ๋‚˜๋“œ ์„œ์žฅ: IO, ์ด๋ฅธ๋ฐ” ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ Maybe - List do ํ‘œ๊ธฐ IO - State Alternative์™€ MonadPlus ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ 1 ํ•˜์Šค ์ผˆ ๊ธฐ์ดˆ ํ•˜์Šค ์ผˆ ๊ธฐ์ดˆ ํ™˜๊ฒฝ ๊ฐ–์ถ”๊ธฐ(Getting set up) ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜(Variables and functions) ์ง„์œ„ ๊ฐ’(Truth values) ํƒ€์ž…์˜ ๊ธฐ์ดˆ(Type basics) ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ(Lists and tuples) ํƒ€์ž…์˜ ๊ธฐ์ดˆ 2(Type basics II) ๋‹ค์Œ ๊ณผ์ •(Next steps) ์–ดํœ˜ ์Œ“๊ธฐ(Building vocabulary) ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ(Simple input and output) 1 ํ™˜๊ฒฝ ๊ฐ–์ถ”๊ธฐ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Getting_set_up ์›๋ฌธ์—์„œ๋Š” Haskell Platform ์„ค์น˜๋ฅผ ์ถ”์ฒœํ•˜์ง€๋งŒ ์‹ค ์‚ฌ์šฉ์ž๋“ค ์‚ฌ์ด์—๋Š” ์ด์— ๋ฐ˜ํ•˜๋Š” ์˜๊ฒฌ์ด ๋งŽ์ด ๋‚˜์˜ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ : https://mail.haskell.org/pipermail/haskell-community/2015-September/000014.html Haskell Platform์€ 2022๋…„์— ํ๊ธฐ๋˜๊ณ  GHCup์œผ๋กœ ๋Œ€์ฒด๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์žฅ์—์„œ๋Š” ํ•˜์Šค ์ผˆ๋กœ ์ฝ”๋”ฉ์„ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ๋‹ค. ํ•˜์Šค ์ผˆ ์„ค์น˜ํ•˜๊ธฐ ํ•˜์Šค์ผˆ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด, ์ฆ‰ ์ปดํ“จํ„ฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ํ–‰๋™ํ•˜๋ผ๊ณ  ์‚ฌ๋žŒ์ด ์˜์‚ฌ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด๋‹ค. ์š”๋ฆฌ๋ฒ•์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๋‹ค. ์šฐ๋ฆฌ๋Š” ์š”๋ฆฌ๋ฒ•์„ ์ž‘์„ฑํ•˜๊ณ  ์ปดํ“จํ„ฐ๋Š” ๊ทธ๊ฒƒ์„ ์‹œํ–‰ํ•œ๋‹ค. ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ๋ผ๋Š” ํŠน๋ณ„ํ•œ ํ”„๋กœ๊ทธ๋žจ์ด ํ•„์š”ํ•˜๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํ•˜์Šค ์ผˆ๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ฅผ ๋ฐ›์•„ ๊ธฐ๊ณ„์–ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๊ธฐ๊ณ„์–ด๋Š” ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•˜๋Š” ๋ณด๋‹ค ์›์‹œ์ ์ธ ์–ธ์–ด๋‹ค. ์š”๋ฆฌ๋ฒ•์— ๋‹ค์‹œ ๋น„์œ ํ•ด ๋ณด๋ฉด ์šฐ๋ฆฌ๋Š” ์š”๋ฆฌ๋ฒ•(ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ)์„ ์ž‘์„ฑํ•˜๊ณ  ์š”๋ฆฌ์‚ฌ(์ปดํŒŒ์ผ๋Ÿฌ ํ”„๋กœ๊ทธ๋žจ)๋Š” ์žฌ๋ฃŒ๋“ค์„ ๋ชจ์•„์„œ ๋จน์„ ์ˆ˜ ์žˆ๋Š” ์š”๋ฆฌ(์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ)๋กœ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค. ๋ฌผ๋ก  ์™„์„ฑ๋œ ์š”๋ฆฌ๋กœ๋ถ€ํ„ฐ ๊ทธ ์š”๋ฆฌ๋ฒ•์„ ์‰ฝ๊ฒŒ ์–ป์–ด๋‚ผ ์ˆ˜๋Š” ์—†๋‹ค(๊ทธ๋ฆฌ๊ณ  ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•˜์—ฌ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด๋‚ธ ๋’ค์—๋Š” ๊ทธ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์–ป์–ด๋‚ผ ์ˆ˜ ์—†๋‹ค). ํ•˜์Šค ์ผˆ ๊ณต๋ถ€๋ฅผ ์‹œ์ž‘ํ•˜๋ ค๋ฉด ํ•˜์Šค ์ผˆ ํ”Œ๋žซํผ์„ ๋‚ด๋ ค๋ฐ›๊ณ  ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค. ์ด ํ”Œ๋žซํผ์—๋Š” "Glasgow Haskell Compiler" ์ฆ‰ GHC, ๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ์—๊ฒŒ ํ•„์š”ํ•œ ๊ทธ ์™ธ ๋ชจ๋“  ๊ฒƒ์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ๋ฐ ์„ค์น˜ ์—†์ด ํ•˜์Šค์ผˆ์˜ ๊ธฐ๋ณธ์„ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด Haskell.org ํ™ˆํŽ˜์ด์ง€์—์„œ ๊ฐ„๋‹จํ•œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด ์œ„ํ‚ค ์ฑ…์˜ ์„ค๋ช…์€ ์™„์ „ํ•œ GHC๋ฅผ ์„ค์น˜ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ง€๋งŒ ๊ธฐ์ดˆ์ ์ธ ๊ฒƒ๋“ค์€ ์›น์‚ฌ์ดํŠธ ๋ฒ„์ „์—์„œ๋„ ์ž‘๋™ํ•œ๋‹ค. ์ž ๊น UNIX ์œ ์ €์—๊ฒŒ: ์†Œ์Šค๋ฅผ ์ง์ ‘ ์ปดํŒŒ์ผํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•œ๋‹ค๋ฉด GHC์— ๋Œ€ํ•ด์„œ๋Š” ๋ณ„๋กœ ์ข‹์€ ์ƒ๊ฐ์ด ์•„๋‹ˆ๋‹ค. ํŠนํžˆ GHC๋ฅผ ์ฒ˜์Œ ์„ค์น˜ํ•˜๋Š” ๊ฑฐ๋ผ๋ฉด ๋”์šฑ ๊ทธ๋ ‡๋‹ค. GHC ์ž์ฒด๊ฐ€ ๋Œ€๋ถ€๋ถ„ ํ•˜์Šค ์ผˆ๋กœ ์ž‘์„ฑ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์†Œ์Šค๋กœ๋ถ€ํ„ฐ GHC๋ฅผ ์ง์ ‘ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ํ•˜๋Š” ๊ฒƒ์€ ์•„์ฃผ ์–ด๋ ต๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ๋นŒ๋“œ ๊ณผ์ •์ด ์ƒ๋‹นํžˆ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ  ๋””์Šคํฌ ๊ณต๊ฐ„์„ ๋งŽ์ด ์†Œ๋น„ํ•œ๋‹ค. ๊ทธ๋ž˜๋„ ์ •๋ง๋กœ GHC๋ฅผ ์†Œ์Šค๋กœ๋ถ€ํ„ฐ ๋นŒ๋“œ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด GHC ํ™ˆํŽ˜์ด์ง€์˜ Building and Porting GHC๋ฅผ ๋ณผ ๊ฒƒ. ์š”์•ฝํ•˜์ž๋ฉด, ์†Œ์Šค๋กœ๋ถ€ํ„ฐ ์ปดํŒŒ์ผํ•˜๋Š” ๋Œ€์‹  ๊ทธ๋ƒฅ ํ•˜์Šค ์ผˆ ํ”Œ๋žซํผ์„ ๋‚ด๋ ค๋ฐ›๋Š” ๊ฒƒ์„ ๊ฐ•๋ ฅํžˆ ์ถ”์ฒœํ•œ๋‹ค. ๊ฑธ์Œ๋งˆ ๋–ผ๊ธฐ ํ•˜์Šค ์ผˆ ํ”Œ๋žซํผ์„ ์„ค์น˜ํ–ˆ์œผ๋ฉด GHCi๋ผ๋Š”('i'๋Š” '๋ฐ˜์‘ํ˜• interactive'์„ ๋œปํ•จ) ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. ๋‹ค์Œ ์ค‘ ์—ฌ๋Ÿฌ๋ถ„์˜ ์šด์˜์ฒด์ œ์— ๋งž๋Š” ์ ˆ์ฐจ๋ฅผ ๋”ฐ๋ฅด๋ฉด ๋œ๋‹ค. ์œˆ๋„์šฐ์ฆˆ: Start - Run์„ ๋ˆ„๋ฅด๊ณ  'cmd'๋ฅผ ์ž…๋ ฅํ•œ ํ›„ ์—”ํ„ฐ๋ฅผ ๋ˆ„๋ฅธ๋‹ค. ghci๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์—”ํ„ฐ๋ฅผ ํ•œ ๋ฒˆ ๋” ๋ˆ„๋ฅธ๋‹ค. ๋งฅ OS: "Applications/Utilities" ํด๋”์˜ "Terminal" ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์—ฐ๋‹ค. ๋‚˜ํƒ€๋‚˜๋Š” ์ฐฝ์— ghci๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์—”ํ„ฐ ํ‚ค๋ฅผ ๋ˆ„๋ฅธ๋‹ค. ๋ฆฌ๋ˆ…์Šค: ํ„ฐ๋ฏธ๋„์„ ์—ด๊ณ  ghci ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด๋Ÿฐ ๊ฒƒ์ด ์ถœ๋ ฅ๋  ๊ฒƒ์ด๋‹ค. GHCi, version 8.10.7: http://www.haskell.org/ghc/ :? for help Prelude> ์ฒซ ๋ฒˆ์งธ ์ค„์€ GHCi ๋ฒ„์ „์ด๊ณ  GHCi์—์„œ ๋„์›€๋ง์„ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์ค€๋‹ค. Prelude> ๋ถ€๋ถ„์€ ํ”„๋กฌํ”„ํŠธ(prompt)๋ผ๊ณ  ํ•œ๋‹ค. ์—ฌ๊ธฐ์— ๋ช…๋ น์„ ์ž…๋ ฅํ•˜๋ฉด GHCi๋Š” ๊ทธ์— ๋ฐ˜์‘ํ•ด ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋†“๋Š”๋‹ค. ์ด ํ”„๋กฌํ”„ํŠธ๋Š” ์ง€๊ธˆ ๋กœ๋“œ๋œ ๋ชจ๋“ˆ์ด Prelude๋ผ๋Š” ๊ฒƒ๋„ ํ‘œ์‹œํ•œ๋‹ค. Prelude๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๋‚ด์žฅ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ์ด์ œ ๊ฐ„๋‹จํ•œ ์‚ฐ์ˆ˜๋ฅผ ํ•ด๋ณด์ž. Prelude> 2 + 2 Prelude> 5 + 4 * 3 17 Prelude> 2 ^ 5 32 ์ด ์—ฐ์‚ฐ์ž๋“ค์€ ๋Œ€๋ถ€๋ถ„์˜ ๋‹ค๋ฅธ ์–ธ์–ด์— ์žˆ๋Š” ์—ฐ์‚ฐ์ž๋“ค๊ณผ ๊ฐ™๋‹ค. +๋Š” ๋”ํ•˜๊ธฐ, *๋Š” ๊ณฑํ•˜๊ธฐ, ^๋Š”<NAME>(์ฆ‰ b )๋‹ค. ๋‘ ๋ฒˆ์งธ ์˜ˆ์‹œ์—์„œ ํ•˜์Šค์ผˆ์ด ํ‘œ์ค€ ์—ฐ์‚ฐ ์ˆœ์„œ(์˜ˆ๋ฅผ ๋“ค๋ฉด ๋”ํ•˜๊ธฐ๋ณด๋‹ค ๊ณฑํ•˜๊ธฐ ๋จผ์ €)๋ฅผ ๋”ฐ๋ฅธ๋‹ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด์ œ ํ•˜์Šค์ผˆ์„ ๊ณ„์‚ฐ๊ธฐ๋กœ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์‚ฌ์‹ค ํ•˜์Šค์ผˆ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ณ„์‚ฐ๊ธฐ๋‹ค. ๋‹ค๋งŒ ์ˆซ์ž๋ฟ ์•„๋‹ˆ๋ผ ๋ฌธ์ž, ๋ฆฌ์ŠคํŠธ, ํ•จ์ˆ˜, ํŠธ๋ฆฌ, ์‹ฌ์ง€์–ด ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ๊นŒ์ง€ ์ฒ˜๋ฆฌํ•˜๋Š” ๋งค์šฐ ๊ฐ•๋ ฅํ•œ ๊ณ„์‚ฐ๊ธฐ๋‹ค(์ด ์šฉ์–ด๋“ค์ด ์ต์ˆ™ํ•˜์ง€ ์•Š์•„๋„ ๊ฑฑ์ •ํ•  ํ•„์š” ์—†๋‹ค). GHCi๋ฅผ ๋– ๋‚˜๋ ค๋ฉด :quit ๋˜๋Š” :q๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. Prelude> :quit Leaving GHCi. GHCi๋Š” ๊ฐ•๋ ฅํ•œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์ด๋‹ค. ์ง„๋„๋ฅผ ๋‚˜์•„๊ฐ์— ๋”ฐ๋ผ ์†Œ์Šค ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํŒŒ์ผ์„ GHCi๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ํŒŒ์ผ๋“ค์˜ ๊ฐ ๋ถ€๋ถ„์„ ํ‰๊ฐ€(evaluate) ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šธ ๊ฒƒ์ด๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ž˜ ๋”ฐ๋ผ์™”๋‹ค๋ฉด(๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด talk page๋ฅผ ํ†ตํ•ด ์ด ์œ„ํ‚ค ์ฑ…์„ ๊ฐœ์„ ํ•˜๋Š” ๊ฒƒ์„ ๋„์™€์ฃผ์„ธ์š”!), ๋‹ค์Œ ์žฅ์„ ์ฝ์„ ์ค€๋น„๊ฐ€ ๋œ ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ๋Š” ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•ด ํ•˜์Šค์ผˆ์˜ ๋ช‡ ๊ฐ€์ง€ ๊ธฐ๋ณธ ๊ฐœ๋…์„ ์†Œ๊ฐœํ•œ๋‹ค. 2 ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Variables_and_functions ๋ณ€์ˆ˜(variable) ํ•˜์Šค ์ผˆ ์†Œ์Šค ํŒŒ์ผ ์ฃผ์„(comment) ๋ช…๋ นํ˜• ์–ธ์–ด์˜ ๋ณ€์ˆ˜ ํ•จ์ˆ˜(function) ํ‰๊ฐ€(evaluation) ๋‹ค์ค‘ ๋งค๊ฐœ๋ณ€์ˆ˜ ํ•จ์ˆ˜ ํ•ฉ์„ฑ์— ๊ด€ํ•ด์„œ ์ง€์—ญ ์ •์˜(local definition) where ์ ˆ ์Šค์ฝ”ํ”„(scope) ์š”์•ฝ ์ด ์žฅ์˜ ๋ชจ๋“  ์˜ˆ์ œ๋Š” ํ•˜์Šค ์ผˆ ์†Œ์Šค ํŒŒ์ผ์— ์ž…๋ ฅํ•˜๊ณ  ๊ทธ ํŒŒ์ผ์„ GHC๋กœ ๋ถˆ๋Ÿฌ์™€์„œ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์–ด๋–ค ์˜ˆ์ œ๋“  "Prelude>" ํ”„๋กฌํ”„ํŠธ๋Š” ์†Œ์Šค์— ํฌํ•จํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋‹ค. ์ด ํ”„๋กฌํ”„ํŠธ๋Š” GHCi ๊ฐ™์€ ํ™˜๊ฒฝ์— ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” ์ฝ”๋“œ๋ฅผ ํŒŒ์ผ์— ๋„ฃ๊ณ  ๊ทธ ํŒŒ์ผ์„ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ๋ณ€์ˆ˜(variable) ์•ž์„  ์žฅ์—์„œ GHCi๋ฅผ ๊ณ„์‚ฐ๊ธฐ๋กœ ํ™œ์šฉํ–ˆ๋‹ค. ๋ฌผ๋ก  ์ด๋Ÿฐ ๊ฒƒ์€ ์งง์€ ๊ณ„์‚ฐ์—๋‚˜ ์“ธ๋ชจ๊ฐ€ ์žˆ๋‹ค. ๋” ๊ธด ๊ณ„์‚ฐ์„ ํ•˜๊ฑฐ๋‚˜ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ ค๋ฉด ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋“ค์„ ๋ณด๊ด€ํ•ด์•ผ ํ•œ๋‹ค. ์ค‘๊ฐ„ ๊ฒฐ๊ณผ์— ์ด๋ฆ„์„ ํ• ๋‹นํ•˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ด€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ์ด๋ฆ„์„ ๋ณ€์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด ๊ฐ ๋ณ€์ˆ˜๋Š” ๊ทธ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์œผ๋กœ ์น˜ํ™˜๋œ๋‹ค. ๋‹ค์Œ ๊ณ„์‚ฐ์„ ๋ณด์ž. Prelude> 3.141592653 * 5^2 78.539816325 ์ด ๊ฐ’์€ ๋ฐ˜์ง€๋ฆ„ 5์ธ ์›์˜ ๋Œ€๋žต์ ์ธ ๋„“์ด๋กœ์„œ = r์ด๋ผ๋Š” ๊ณต์‹์— ๋”ฐ๋ฅธ ๊ฒƒ์ด๋‹ค. ฯ€โ‰’3.141592653์˜ ์ž๋ฆฟ์ˆ˜๋“ค์„ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์€ ์„ฑ๊ฐ€์‹œ๊ณ  ์ฒ˜์Œ ๋ช‡ ์ž๋ฆฌ๋ฅผ ๊ธฐ์–ตํ•˜๋Š” ๊ฒƒ์กฐ์ฐจ ๊ท€์ฐฎ์€ ์ผ์ด๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ๋ฌด์˜๋ฏธํ•œ ๋ฐ˜๋ณต๊ณผ ์•”๊ธฐ๋ฅผ ๊ธฐ๊ณ„์— ์œ„์ž„ํ•œ๋‹ค. ์ง€๊ธˆ ๊ฐ™์€ ๊ฒฝ์šฐ ํ•˜์Šค์ผˆ์€ ์ด๋ฏธ ฯ€๋ฅผ ์ˆ˜์‹ญ ์ž๋ฆฌ๊นŒ์ง€ ๋ณด๊ด€ํ•˜๋Š” pi๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ํฌํ•จํ•œ๋‹ค. pi๋ฅผ ์“ฐ๋ฉด ์ฝ”๋“œ๊ฐ€ ๋” ๊น”๋”ํ•  ๋ฟ ์•„๋‹ˆ๋ผ ์ •๋ฐ€๋„๋„ ๋†’๋‹ค. Prelude> pi 3.141592653589793 Prelude> pi * 5^2 78.53981633974483 ๋ณ€์ˆ˜ pi์™€ ๊ทธ ๊ฐ’ 3.141592653589793๋Š” ๊ณ„์‚ฐ์—์„œ ์„œ๋กœ ๋ฐ”๊ฟ” ์“ธ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์Šค ์ผˆ ์†Œ์Šค ํŒŒ์ผ ๋‘๊ณ ๋‘๊ณ  ์“ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์œผ๋ฉด ํ™•์žฅ์ž๊ฐ€. hs์ธ ํ•˜์Šค ์ผˆ ์†Œ์Šค ํŒŒ์ผ์— ๊ทธ ์ฝ”๋“œ๋ฅผ ์ €์žฅํ•œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ. hs ํŒŒ์ผ์€ ํ‰๋ฌธ(plain text)์ด๋‹ค. ์ฝ”๋”ฉ์— ์ ํ•ฉํ•œ ํ…์ŠคํŠธ ์—๋””ํ„ฐ๋ฅผ ์ด์šฉํ•ด ์ด ํŒŒ์ผ๋“ค์„ ์ž‘์—…ํ•˜๋ฉด ๋œ๋‹ค. (ํ…์ŠคํŠธ ์—๋””ํ„ฐ์— ๋Œ€ํ•œ ์œ„ํ‚คํ”ผ๋””์•„ ๊ธ€์„ ์ฝ์–ด๋ณด์ž) ๊ดœ์ฐฎ์€ ์†Œ์Šค ์ฝ”๋“œ ์—๋””ํ„ฐ๋“ค์€ ์ฝ๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ƒ‰์น ํ•˜๋Š” ๊ตฌ๋ฌธ ํ•˜์ด๋ผ์ดํŒ…(syntax highlighting)์„ ์ œ๊ณตํ•œ๋‹ค. ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ ์‚ฌ์ด์—์„  Vim๊ณผ Emacs๊ฐ€ ์ธ๊ธฐ ์žˆ๋‹ค. ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ปดํ“จํ„ฐ์— ๋””๋ ‰ํ„ฐ๋ฆฌ(์ฆ‰ ํด๋”)๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ , ์•ž์œผ๋กœ ์ด ์ฑ…์—์„œ ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ ๋งŒ๋“ค ํ•˜์Šค ์ผˆ ํŒŒ์ผ๋“ค์„ ์—ฌ๊ธฐ์— ์ €์žฅํ•˜์ž. ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋ฆ„์€ HaskellWikibook ์ •๋„๋ฉด ๋  ๊ฒƒ์ด๋‹ค. ์ด ๋””๋ ‰ํ„ฐ๋ฆฌ์— Varfun.hs๋ผ๋Š” ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. r = 5.0 ์ด ์ฝ”๋“œ๋Š” ๋ณ€์ˆ˜ r์„ ๊ฐ’ 5.0์œผ๋กœ์„œ ์ •์˜ํ•œ๋‹ค. ์ž ๊น: ์ค„์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ๊ณต๋ฐฑ์ด ์—†๋Š”์ง€ ํ™•์ธํ•˜์ž. ํ•˜์Šค์ผˆ์€ ๊ณต๋ฐฑ์— ๋ฏผ๊ฐํ•˜๋‹ค. ๊ทธ๋‹ค์Œ ํ„ฐ๋ฏธ๋„์—์„œ HaskellWikibook ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ์ด๋™ํ•˜์—ฌ GHCi๋ฅผ ์—ด๊ณ  :load ๋ช…๋ น์œผ๋กœ Varfun.hs ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. Prelude> :load Varfun.hs [1 of 1] Compiling Main ( Varfun.hs, interpreted) Ok, modules loaded: Main. :load๋Š” :l๋กœ ์ถ•์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค. (:l Vafun.hs์ฒ˜๋Ÿผ) GHCi๊ฐ€ Could not find module 'Varfun.hs' ๊ฐ™์€ ์˜ค๋ฅ˜๋ฅผ ๋‚ด๋ฑ‰๋Š”๋‹ค๋ฉด GHCi๋ฅผ ๋‹ค๋ฅธ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์‹คํ–‰ํ–ˆ๊ฑฐ๋‚˜ ํŒŒ์ผ์„ ๋‹ค๋ฅธ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ €์žฅํ•œ ๊ฒƒ์ด๋‹ค. GHCi ์•ˆ์—์„œ :cd ๋ช…๋ น์„ ์จ์„œ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. (์˜ˆ: :cd HaskellWikibook) ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์™”์œผ๋ฉด GHCi์˜ ํ”„๋กฌํ”„ํŠธ๊ฐ€ "Prelude"์—์„œ "*Main"์œผ๋กœ ๋ณ€ํ•œ๋‹ค. ์ด์ œ ์ƒˆ๋กœ ์ •์˜ํ•œ ๋ณ€์ˆ˜ r์„ ๊ณ„์‚ฐ์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. *Main> r 5.0 *Main> pi * r^2 78.53981633974483 ์ž˜ ์•Œ๋ ค์ง„ ๊ณต์‹ = r ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฐ˜์ง€๋ฆ„ 5.0์ธ ์›์˜ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ–ˆ๋‹ค. ์ด๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š” ์ด์œ ๋Š” ์šฐ๋ฆฌ๊ฐ€ Varfun.hs ํŒŒ์ผ์— r์„ ์ •์˜ํ–ˆ๊ณ  pi๋Š” ํ‘œ์ค€ ํ•˜์Šค ์ผˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋„“์ด ๊ณต์‹์— ๋Œ€ํ•œ ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์ •์˜ํ•ด์„œ ์ด ๊ณต์‹์— ์ ‘๊ทผํ•˜๊ธฐ ๋” ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด๋ณด์ž. ์†Œ์Šค ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•œ๋‹ค. r = 5.0 area = pi * r ^ 2 ํŒŒ์ผ์„ ์ €์žฅํ•œ๋‹ค. ํŒŒ์ผ์„ ๋กœ๋“œํ–ˆ๋˜ GHCi๋ฅผ ์•„์ง ์‹คํ–‰ ์ค‘์ด๋ผ๋ฉด :reload (์งง๊ฒŒ๋Š” :r) ๋ช…๋ น์„ ์ž…๋ ฅํ•œ๋‹ค. *Main> :reload Compiling Main ( Varfun.hs, interpreted) Ok, modules loaded: Main. *Main> ์ด์ œ r๊ณผ area๋ผ๋Š” ๋‘ ๋ณ€์ˆ˜๋ฅผ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. *Main> area 78.53981633974483 *Main> area / r 15.707963267948966 ์ž ๊น let ํ‚ค์›Œ๋“œ(ํŠน๋ณ„ํ•œ ๋œป์ด ์žˆ๋Š” ๋‹จ์–ด)๋กœ GHCi ํ”„๋กฌํ”„ํŠธ์—์„œ ์†Œ์Šค ํŒŒ์ผ ์—†์ด ๋ฐ”๋กœ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. Prelude> let area = pi * 5 ^ 2 ํŽธ๋ฆฌํ•  ๋•Œ๋„ ์žˆ์ง€๋งŒ GHCi์—์„œ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์€ ๋ณต์žกํ•œ ์ž‘์—…์—๋Š” ์‹ค์šฉ์ ์ด์ง€ ์•Š๋‹ค. ๋ณดํ†ต์€ ์†Œ์Šค ํŒŒ์ผ์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ฃผ์„(comment) ์†Œ์Šค ํŒŒ์ผ์€ ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ ์™ธ์—๋„ ์ฃผ์„์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์Šค์ผˆ์—๋Š” ์ฃผ์„์ด ๋‘ ์ข…๋ฅ˜ ์žˆ๋‹ค. ํ•˜๋‚˜๋Š” --์œผ๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๊ทธ ์ค„์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ์ด์–ด์ง„๋‹ค. x = 5 -- x is 5. y = 6 -- y is 6. -- z = 7 -- z is not defined. ์—ฌ๊ธฐ์„œ x์™€ y๋Š” ์‹ค์ œ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ์— ์ •์˜๋˜์ง€๋งŒ z๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค. ๋‘ ๋ฒˆ์งธ ์ข…๋ฅ˜์˜ ์ฃผ์„์€ {- ... -}๋กœ ๊ฐ์‹ธ๋Š” ๊ฒƒ์ด๋ฉฐ ์—ฌ๋Ÿฌ ์ค„๋กœ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. answer = 2 * {- block comment, crossing lines and... -} 3 {- inline comment. -} * 7 ์ฃผ์„์€ ํ”„๋กœ๊ทธ๋žจ ์ผ๋ถ€๋ฅผ ์„ค๋ช…ํ•˜๊ฑฐ๋‚˜ ๋ฌธ๋งฅ์ƒ ์–ด๋–ค ๊ธฐ๋ก์„ ๋‚จ๊ธฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. ์ฃผ์„์ด ๋„ˆ๋ฌด ๋งŽ์œผ๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ์ฝ๋Š” ๊ฒƒ์ด ์˜คํžˆ๋ ค ํž˜๋“ค์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋‚จ์šฉ์„ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ๊ทธ์— ๋Œ€์‘ํ•˜๋Š” ์ฃผ์„๋„ ์‹ ์ค‘ํžˆ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. ๋‚ก์€ ์ฃผ์„์€ ํฐ ํ˜ผ๋™์„ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ๋ช…๋ นํ˜• ์–ธ์–ด์˜ ๋ณ€์ˆ˜ ๋ช…๋ นํ˜•(imperative) ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ์ต์ˆ™ํ•œ ๋…์ž๋Š” ํ•˜์Šค์ผˆ์˜ ๋ณ€์ˆ˜๊ฐ€ C ๊ฐ™์€ ์–ธ์–ด์˜ ๋ณ€์ˆ˜์™€ ์ƒ๋‹นํžˆ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ๋ˆˆ์น˜์ฑ˜์„ ๊ฒƒ์ด๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฒฝํ—˜์ด ์—†๋‹ค๋ฉด ์ด ์ ˆ์„ ๊ฑด๋„ˆ๋›ฐ์–ด๋„ ๋˜์ง€๋งŒ, ์‚ฌ๋žŒ๋“ค์ด ํ•˜์Šค์ผˆ์„ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€ ๋น„๊ตํ•˜๋Š” ๋งŽ์€ ๊ฒฝ์šฐ(์˜ˆ๋ฅผ ๋“ค๋ฉด ๋Œ€๋ถ€๋ถ„์˜ ํ•˜์Šค ์ผˆ ๊ต์žฌ)์—์„œ ์ผ๋ฐ˜์ ์ธ ์ƒํ™ฉ์„ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ๊ฒƒ์ด๋‹ค. ๋ช…๋ นํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ๋ณ€์ˆ˜๋ฅผ ์ปดํ“จํ„ฐ ๋ฉ”๋ชจ๋ฆฌ ์•ˆ์˜ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ ์žฅ์†Œ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค. ์ด ์ ‘๊ทผ๋ฒ•์€ ์ปดํ“จํ„ฐ์˜ ๊ธฐ๋ณธ์ ์ธ ๋™์ž‘ ์›๋ฆฌ์™€ ์—ฐ๊ฒฐ๋œ๋‹ค. ๋ช…๋ นํ˜• ํ”„๋กœ๊ทธ๋žจ์€ ์ปดํ“จํ„ฐ๊ฐ€ ํ•  ์ผ์„ ๋ช…์‹œํ•œ๋‹ค. ๊ณ ์ˆ˜์ค€ ๋ช…๋ นํ˜• ์–ธ์–ด๋“ค์€ ์ง์ ‘์ ์ธ ์ปดํ“จํ„ฐ ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ์™€ ๊ฝค ๋ฉ€์–ด์กŒ์ง€๋งŒ ๋‹จ๊ณ„๋ณ„๋กœ ์ƒ๊ฐํ•˜๋Š” ๋ฐฉ์‹์€ ๋™์ผํ•˜๋‹ค. ์ด์™€ ๋‹ฌ๋ฆฌ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ๊ณ ์ˆ˜์ค€ ์ˆ˜ํ•™์  ๋„๊ตฌ๋ฅผ ํ†ตํ•ด ์‚ฌ๊ณ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋ฉฐ, ๋ณ€์ˆ˜๋“ค์ด ์„œ๋กœ ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐ๋˜๋Š”์ง€๋ฅผ ์ •์˜ํ•˜๊ณ  ์ด๊ฒƒ์„ ์ปดํ“จํ„ฐ๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ๊ณ„๋ณ„ ๋ช…๋ น์œผ๋กœ ๋ฒˆ์—ญํ•˜๋Š” ์ผ์€ ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ ๋งก๊ธด๋‹ค. ์˜ˆ์‹œ๋ฅผ ํ•˜๋‚˜ ๋ณด์ž. ๋‹ค์Œ ์ฝ”๋“œ๋Š” ํ•˜์Šค์ผˆ์—์„œ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. r = 5 r = 2 ๋ช…๋ นํ˜• ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ์ด๊ฒƒ์„ ์ฒ˜์Œ์—๋Š” r = 5๋กœ ์„ค์ •ํ•˜๊ณ  ๊ทธ๋‹ค์Œ r = 2๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค๊ณ  ์ฝ์„ ๊ฒƒ์ด๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ์œ„ ์ฝ”๋“œ๋Š” "r์˜ ์ค‘๋ณต ์„ ์–ธ"์ด๋ผ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฑ‰๋Š”๋‹ค. ์ฃผ์–ด์ง„ ์Šค์ฝ”ํ”„ ์•ˆ์—์„œ ํ•œ ํ•˜์Šค ์ผˆ ๋ณ€์ˆ˜๋Š” ํ•œ ๋ฒˆ๋งŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ณ  ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์Šค์ผˆ์˜ ๋ณ€์ˆ˜๋Š” ๊ฑฐ์˜ ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ์‚ฌ์‹ค์€ ์ˆ˜ํ•™์˜ ๋ณ€์ˆ˜ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. ์ˆ˜ํ•™ ๊ต์‹ค์—์„œ ์šฐ๋ฆฌ๋Š” ํ•œ ๋ฌธ์ œ ์•ˆ์˜ ์–ด๋–ค ๋ณ€์ˆ˜๊ฐ€ ๊ทธ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ ๋ณธ ์ ์ด ์—†๋‹ค. ์ •ํ™•ํžˆ ๋งํ•˜๋ฉด ํ•˜์Šค ์ผˆ ๋ณ€์ˆ˜๋Š” ๋ถˆ๋ณ€ immutable์ด๋‹ค. ํ•˜์Šค ์ผˆ ๋ณ€์ˆ˜๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์— ์ž…๋ ฅํ•˜๋Š” ๋ฐ์ดํ„ฐ์— ์˜ํ•ด์„œ๋งŒ ๋ณ€ํ•œ๋‹ค. r์„ ํ•œ ์ฝ”๋“œ์—์„œ ๋‘ ๋ฐฉ๋ฒ•์œผ๋กœ ์ •์˜ํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ ํŒŒ์ผ์„ ๋ณ€๊ฒฝํ•ด์„œ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜๋Š” ์žˆ๋‹ค. ์œ„ ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•ด ๋ณด์ž. r = 2.0 area = pi * r ^ 2 ๋ฌผ๋ก  ์—ฌ๊ธฐ์—๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. r์ด ์ •์˜๋œ ๊ณณ์—์„œ r์„ ์ˆ˜์ •ํ•˜๋ฉด ์ฝ”๋“œ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์—์„œ r์„ ์ด์šฉํ•˜๋Š” ๋ชจ๋“  ๊ฐ’์ด ์ž๋™์œผ๋กœ ๊ฐฑ์‹ ๋œ๋‹ค. ํ˜„์‹ค์˜ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ์ผ๋ถ€ ๋ณ€์ˆ˜๋“ค์„ ์ฝ”๋“œ์— ๋ช…์‹œํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ ๊ฐ’์€ ํ”„๋กœ๊ทธ๋žจ์ด ์™ธ๋ถ€ ํŒŒ์ผ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ์‚ฌ์šฉ์ž ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„ ๋•Œ ์ •์˜๋œ๋‹ค. ํ•˜์ง€๋งŒ ์ง€๊ธˆ์€ ๋ณ€์ˆ˜๋ฅผ ๋‚ด๋ถ€์— ์ •์˜ํ•˜๋Š” ๋ฐฉ์‹์„<NAME>๋‹ค. ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ์™€์˜ ์ƒํ˜ธ์ž‘์šฉ์€ ๋‚˜์ค‘์— ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ์€ ๋ช…๋ นํ˜• ์–ธ์–ด์™€์˜ ์ฃผ๋œ ์ฐจ์ด์ ์„ ๋ณด์—ฌ์ฃผ๋Š” ๋˜ ๋‹ค๋ฅธ ์˜ˆ์‹œ๋‹ค. r = r + 1 ์ด ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋Š” "๋ณ€์ˆ˜ r์„ ์ฆ๊ฐ€์‹œํ‚ค๋Š”" (์ฆ‰ ๋ฉ”๋ชจ๋ฆฌ ๋‚ด ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋Š”) ๊ฒƒ์ด ์•„๋‹ˆ๋ผ r์˜ ์žฌ๊ท€์  ์ •์˜๋‹ค. (์ฆ‰ r์„ ์ด์šฉํ•ด r์„ ์ •์˜) ์žฌ๊ท€๋Š” ๋‚˜์ค‘์— ์ž์„ธํžˆ ์„ค๋ช…ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งŒ์•ฝ r์„ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์•ž์„œ ์ •์˜ํ–ˆ๋‹ค๋ฉด r = r + 1์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฑ‰์—ˆ์„ ๊ฒƒ์ด๋‹ค. r = r + 1์€ ์ˆ˜ํ•™์ ์œผ๋กœ ๋งํ•˜์ž๋ฉด 5 = 5 + 1 ๊ฐ™์€ ๊ฒƒ์ธ๋ฐ, ๋ถ„๋ช…ํžˆ ์ž˜๋ชป๋œ ๊ฒƒ์ด๋‹ค. ๋ณ€์ˆ˜๋“ค์˜ ๊ฐ’์ด ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ๋ณ€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ์ˆœ์„œ๋กœ๋„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ์ฝ”๋“œ๋“ค์€ ์ •ํ™•ํžˆ ๊ฐ™์€ ์ผ์„ ํ•œ๋‹ค. y = x * 2 x = 3 x = 3 y = x * 2 ํ•˜์Šค์ผˆ์—๋Š” "x๋ฅผ y๋ณด๋‹ค ๋จผ์ € ์„ ์–ธํ•œ๋‹ค"๋ผ๋Š” ๊ฐœ๋…์ด ์—†๋‹ค. ๋ฌผ๋ก  y๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์—ฌ์ „ํžˆ x์˜ ๊ฐ’์ด ํ•„์š”ํ•˜์ง€๋งŒ ์ด๋Š” ํŠน์ • ์ˆซ์ž ๊ฐ’์ด ํ•„์š”ํ•˜๊ธฐ ์ „์—๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์€ ์ผ์ด๋‹ค. ํ•จ์ˆ˜(function) ์ƒˆ ์›์˜ ๋„“์ด๋ฅผ ๊ตฌํ•  ๋•Œ๋งˆ๋‹ค ํ”„๋กœ๊ทธ๋žจ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ ์ง€๋ฃจํ•˜๊ณ  ํ•œ ๋ฒˆ์— ์› ํ•œ ๊ฐœ๋งŒ ๊ณ„์‚ฐํ•œ๋‹ค๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ๋‘ ๋ฒˆ์งธ ์›์„ ์œ„ํ•œ ์ƒˆ ๋ณ€์ˆ˜ r2์™€ area2๋ฅผ ๋งŒ๋“ค๋ฉด ์› 2๊ฐœ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. 1 r = 5 area = pi * r ^ 2 r2 = 3 area2 = pi * r2 ^ 2 ๋ฌผ๋ก  ์ด๋Ÿฐ ์ƒ๊ฐ ์—†๋Š” ๋ฐ˜๋ณต์„ ์—†์• ๋ ค๋ฉด ๋„“์ด์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋Š” ํ•˜๋‚˜๋งŒ ์žˆ๊ณ  ์ด ํ•จ์ˆ˜๋ฅผ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ˜์ง€๋ฆ„์— ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. ํ•จ์ˆ˜๋Š” ์ธ์ž(argument) ๊ฐ’(๋˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜(parameter))์„ ์ทจํ•ด์„œ ๊ฒฐ๊ด๊ฐ’์„ ๋Œ๋ ค์ค€๋‹ค. (๊ทธ ๋ณธ์งˆ์€ ์ˆ˜ํ•™์˜ ํ•จ์ˆ˜์™€ ๊ฐ™๋‹ค) ํ•˜์Šค์ผˆ์—์„œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์€ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ์ขŒ๋ณ€์— ํ•จ์ˆ˜ ์ธ์ž๋ฅผ ๋†“๋Š”๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ์€ r์ด๋ผ๋Š” ์ธ์ž์— ์˜์กดํ•˜๋Š” ํ•จ์ˆ˜ area๋ฅผ ์ •์˜ํ•œ๋‹ค. area r = pi * r ^ 2 ๋ฌธ๋ฒ•์„ ์ž์„ธํžˆ ๋ณด๋ฉด ํ•จ์ˆ˜ ์ด๋ฆ„(์—ฌ๊ธฐ์„œ๋Š” area)์ด ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜ค๊ณ  ๊ทธ๋‹ค์Œ ๊ณต๋ฐฑ ํ•œ ์นธ๊ณผ ์ธ์ž(์—ฌ๊ธฐ์„œ๋Š” r)๊ฐ€ ๋‚˜์˜จ๋‹ค. = ๊ธฐํ˜ธ ๋‹ค์Œ์˜ ํ•จ์ˆ˜ ์ •์˜๋Š” ๊ทธ ์ธ์ž๋ฅผ ์ด๋ฏธ ์ •์˜๋œ ๋‹ค๋ฅธ ์šฉ์–ด๋“ค๊ณผ ํ•จ๊ป˜ ์ด์šฉํ•˜๋Š” ํ•˜๋‚˜์˜ ๊ณต์‹์ด๋‹ค. ์ด์ œ ์ธ์ž์— ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’๋“ค์„ ๋„ฃ์–ด์„œ ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ ํŒŒ์ผ์— ์ €์žฅํ•˜๊ณ  GHCi๋กœ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์™€์„œ ๋‹ค์Œ์„ ์‹œ๋„ํ•ด ๋ณด์ž. *Main> area 5 78.53981633974483 *Main> area 3 28.274333882308138 *Main> area 17 907.9202768874502 ํ•จ์ˆ˜์— ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ˜์ง€๋ฆ„์„ ๋„ฃ์–ด์„œ ํ˜ธ์ถœํ•˜๋ฉด ์–ด๋–ค ์›์˜ ๋„“์ด๋“  ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ์˜ ํ•จ์ˆ˜๋Š” ์ˆ˜ํ•™์ ์œผ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋œ๋‹ค. = r ์ˆ˜ํ•™์—์„œ๋Š” A(5) = 78.54 ๋˜๋Š” A(3) = 28.27์ฒ˜๋Ÿผ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ผ๋‹ค. ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋Š” ๊ด„ํ˜ธ๊ฐ€ ์žˆ์–ด๋„ ์ž‘๋™ํ•˜์ง€๋งŒ ์ด ์ฑ…์—์„œ๋Š” ๊ด€๋ก€์— ๋”ฐ๋ผ ์ƒ๋žตํ•œ๋‹ค. ํ•˜์Šค์ผˆ์€ ์–ด๋””์„œ๋“  ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ์šฐ๋ฆฌ๋Š” ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์—ฌ๋ถ„์˜ ๊ธฐํ˜ธ๋ฅผ ์ค„์ด๊ณ ์ž ํ•œ๋‹ค. ๋ฐ˜๋“œ์‹œ ํ•จ๊ป˜ ํ‰๊ฐ€ํ•ด์•ผ ํ•˜๋Š” ํ‘œํ˜„์‹(๊ฐ’์„ ๋Œ๋ ค์ฃผ๋Š” ์ž„์˜์˜ ์ฝ”๋“œ)๋“ค์˜ ๊ฒฝ์šฐ์—๋Š” ์—ฌ์ „ํžˆ ๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค์Œ ํ‘œํ˜„์‹๋“ค์ด ์–ด๋–ป๊ฒŒ ์„œ๋กœ ๋‹ค๋ฅด๊ฒŒ ํ•ด์„๋˜๋Š”์ง€ ๋ˆˆ์—ฌ๊ฒจ๋ณด์ž. 5 * 3 + 2 -- 15 + 2 = 17 (๊ณฑ์…ˆ์„ ๋ง์…ˆ๋ณด๋‹ค ๋จผ์ €) 5 * (3 + 2) -- 5 * 5 = 25 (๊ด„ํ˜ธ ๋•๋ถ„์—) area 5 * 3 -- (area 5) * 3 area (5 * 3) -- area 15 ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋“ค์€ +๋‚˜ * ๊ฐ™์€ ๋ชจ๋“  ์—ฐ์‚ฐ์ž๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌ๋ฐ›๋Š” ๊ฒƒ์— ์œ ์˜ํ•˜๋ผ. ์ด๋Š” ์ˆ˜ํ•™์—์„œ ๊ณฑ์…ˆ์ด ๋ง์…ˆ๋ณด๋‹ค ๋จผ์ € ๋˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์ด๋‹ค. ํ‰๊ฐ€(evaluation) GHCi์— ํ‘œํ˜„์‹์„ ์ž…๋ ฅํ•˜๋ฉด ์ •ํ™•ํžˆ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š” ๊ฑธ๊นŒ? ์—”ํ„ฐ ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด GHCi๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•œ ํ‘œํ˜„์‹์„ ํ‰๊ฐ€ํ•œ๋‹ค. ์ด ๋ง์€ ๊ฐ๊ฐ์˜ ํ•จ์ˆ˜๋ฅผ ๊ทธ ์ •์˜๋กœ ์น˜ํ™˜ํ•˜๊ณ  ๋‹จ์ผ ๊ฐ’์ด ๋‚จ์„ ๋•Œ๊นŒ์ง€ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด area 5์˜ ํ‰๊ฐ€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰๋œ๋‹ค. area 5 => { replace the left-hand side area r = ... by the right-hand side ... = pi * r^2 } pi * 5^2 => { replace pi by its numerical value } 3.141592653589793 * 5^2 => { apply exponentiation (^) } 3.141592653589793 * 25 => { apply multiplication (*) } 78.53981633974483 ์—ฌ๊ธฐ์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ํ•จ์ˆ˜๋ฅผ ์ ์šฉ(apply) ํ•˜๊ฑฐ๋‚˜ ํ˜ธ์ถœ(call) ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํ•จ์ˆ˜ ์ •์˜์˜ ์ขŒ๋ณ€์„ ์šฐ๋ณ€์œผ๋กœ ์น˜ํ™˜ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. GHCi๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚œ๋‹ค. ์—ฌ๊ธฐ ํ•จ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ ๋” ์žˆ๋‹ค. double x = 2 * x quadruple x = double (double x) square x = x * x half x = x / 2 ์—ฐ์Šต๋ฌธ์ œ GHCi๊ฐ€ quadruple 5๋ฅผ ์–ด๋–ป๊ฒŒ ํ‰๊ฐ€ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•˜๋ผ. ์ธ์ž๋ฅผ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๊ณ  12๋ฅผ ๋นผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ๋‹ค์ค‘ ๋งค๊ฐœ๋ณ€์ˆ˜ ํ•จ์ˆ˜๋Š” ์ธ์ž๋ฅผ ํ•˜๋‚˜๋ณด๋‹ค ๋งŽ์ด ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด ํ•จ์ˆ˜๋Š” ์ง์‚ฌ๊ฐํ˜•์˜ ๊ธธ์ด์™€ ๋„ˆ๋น„๋ฅผ ๋ฐ›์•„ ๊ทธ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. areaRect l w = l * w *Main> areaRect 5 10 50 ์ด ์˜ˆ์ œ๋Š” ์‚ผ๊ฐํ˜•์˜ ๋„“์ด ( = h) ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. areaTriangle b h = (b * h) / 2 *Main> areaTriangle 3 9 13.5 ์—ฌ๊ธฐ์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ์ธ์ˆ˜๋“ค์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ์ด๊ฒƒ์ด ํ‘œํ˜„์‹์„ ๋ฌถ๊ธฐ ์œ„ํ•ด ๊ด„ํ˜ธ๋ฅผ ์“ฐ๊ธฐ๋„ ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด x๋ฅผ ๋„ค ์ œ๊ณฑํ•˜๋ ค๋ฉด ๋‹จ์ˆœํžˆ ์ด๋ ‡๊ฒŒ ์“ธ ์ˆ˜๋Š” ์—†๋‹ค. quadruple x = double double x -- error ์ด๋Ÿฌ๋ฉด double์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋‘ ์ธ์ž double๊ณผ x์— ์ ์šฉํ•˜๊ฒŒ ๋œ๋‹ค. ํ•จ์ˆ˜๋„ ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ธ์ž๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์— ์œ ์˜ํ•˜๋ผ. (๊ทธ ์ด์œ ๋Š” ๋‚˜์ค‘์— ๋ณผ ๊ฒƒ์ด๋‹ค) ์ง€๊ธˆ์€ ์ด ์˜ˆ์ œ๊ฐ€ ์ž‘๋™ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ์ธ์ž๋ฅผ ๊ด„ํ˜ธ๋กœ ๋‘˜๋Ÿฌ์‹ธ์•ผ ํ•œ๋‹ค. quadruple x = double (double x) ์ธ์ž๋Š” ํ•ญ์ƒ ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ์ „๋‹ฌ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด subtract x y = x - y *Main> subtract 10 5 *Main> subtract 5 10 -5 ์—ฌ๊ธฐ์„œ subtract 10 5๋Š” 10 - 5๋กœ ํ‰๊ฐ€๋˜์ง€๋งŒ subtract 5 10์€ 5 - 10์œผ๋กœ ํ‰๊ฐ€๋˜๋Š”๋ฐ ๊ทธ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์ƒ์ž์˜ ๋ถ€ํ”ผ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ๊ธฐ์ž(Giza)์— ์žˆ๋Š” ์œ ๋ช…ํ•œ ํ”ผ๋ผ๋ฏธ๋“œ๋ฅผ ์ด๋ฃจ๋Š” ๋Œ์€ ๋Œ€๋žต ๋ช‡ ๊ฐœ์ผ๊นŒ? ํžŒํŠธ: ํ”ผ๋ผ๋ฏธ๋“œ์˜ ๋ถ€ํ”ผ์™€ ๊ฐ๊ฐ์˜ ๋ธ”๋ก์˜ ๋ถ€ํ”ผ์— ๋Œ€ํ•œ ์ถ”์ •์น˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ํ•จ์ˆ˜ ํ•ฉ์„ฑ์— ๊ด€ํ•ด์„œ ๋ฌผ๋ก  ์šฐ๋ฆฌ๊ฐ€ ์•ž์„œ ์ •์˜ํ•œ ํ•จ์ˆ˜๋“ค์„ ํ™œ์šฉํ•ด ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋Š” ๋ง์…ˆ (+) ๋˜๋Š” ๊ณฑ์…ˆ (*) ๊ฐ™์€ ๋ฏธ๋ฆฌ ์ •์˜๋œ ํ•จ์ˆ˜๋“ค์„(ํ•˜์Šค์ผˆ์—์„œ๋Š” ์—ฐ์‚ฐ์ž๊ฐ€ ํ•จ์ˆ˜๋กœ์„œ ์ •์˜๋œ๋‹ค) ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์›๋ฆฌ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ •์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” ์ง์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์žฌํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. areaRect l w = l * w areaSquare s = areaRect s s *Main> areaSquare 5 25 ์–ด์จŒ๋“  ์ •์‚ฌ๊ฐํ˜•์€ ๋‹จ์ง€ ๋‘ ๋ณ€์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ์ง์‚ฌ๊ฐํ˜•์ผ ๋ฟ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์›ํ†ต์˜ ๋ถ€ํ”ผ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ์›ํ†ต์˜ ๋ถ€ํ”ผ๋Š” ์› ๋ชจ์–‘์ธ ๋ฐ”๋‹ฅ์˜ ๋„“์ด(์ด ์žฅ์—์„œ ์ด๋ฏธ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜์ด๋‹ˆ ์žฌํ™œ์šฉํ•  ๊ฒƒ) ๊ณฑํ•˜๊ธฐ ๋†’์ด๋‹ค. ์ง€์—ญ ์ •์˜(local definition) where ์ ˆ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ๊ทธ ํ•จ์ˆ˜์— ํ•œ์ •๋œ ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋ฅผ ์ •์˜ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ผ๊ฐํ˜•์˜ ๋ณ€ a, b, c๋กœ ๊ทธ ์‚ผ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ—ค๋ก ์˜ ๊ณต์‹์„ ๊ณ ๋ คํ•ด ๋ณด์ž. heron a b c = sqrt (s * (s - a) * (s - b) * (s - c)) where s = (a + b + c) / 2 ๋ณ€์ˆ˜ s๋Š” ์‚ผ๊ฐํ˜•์˜ ๋‘˜๋ ˆ์˜ ์ ˆ๋ฐ˜์ธ๋ฐ ์ œ๊ณฑ๊ทผ ํ•จ์ˆ˜ sqrt์˜ ์ธ์ž๋กœ s๋ฅผ ๋„ค ๋ฒˆ ์“ฐ๋Š” ๊ฒƒ์€ ์ง€๋ฃจํ•œ ์ผ์ด๋‹ค. ๋‹จ์ˆœํžˆ ์ •์˜๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์“ฐ๋Š” ๊ฒƒ์€ ๋จนํžˆ์ง€ ์•Š๋Š”๋‹ค. heron a b c = sqrt (s * (s - a) * (s - b) * (s - c)) s = (a + b + c) / 2 -- a, b, and c are not defined here ๋ณ€์ˆ˜ a, b, c ๊ฐ€๋Š” heron ํ•จ์ˆ˜์˜ ์šฐ๋ณ€์—์„œ๋งŒ ์ด์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ s์˜ ์ •์˜๋Š” heron์˜ ์šฐ๋ณ€์˜ ์ผ๋ถ€๊ฐ€ ์•„๋‹ˆ๋‹ค. s๋ฅผ ์šฐ๋ณ€์˜ ์ผ๋ถ€๋กœ ๋งŒ๋“ค๋ ค๋ฉด where ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. where์™€ ์ง€์—ญ ์ •์˜๋ฅผ ๊ณต๋ฐฑ 4๊ฐœ๋กœ ๋“ค์˜€์Œ์œผ๋กœ์จ ์ด์–ด์ง€๋Š” ์ •์˜๋“ค๊ณผ ๊ตฌ๋ณ„ํ•œ ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. ๋‹ค์Œ์€ ์ง€์—ญ ์ •์˜์™€ ์ตœ์ƒ์œ„ ์ •์˜๋ฅผ ์„ž์–ด ์“ฐ๋Š” ๋˜ ๋‹ค๋ฅธ ์˜ˆ์‹œ๋‹ค. areaTriangleTrig a b c = c * height / 2 -- use trigonometry where cosa = (b ^ 2 + c ^ 2 - a ^ 2) / (2 * b * c) sina = sqrt (1 - cosa ^ 2) height = b * sina areaTriangleHeron a b c = result -- use Heron's formula where result = sqrt (s * (s - a) * (s - b) * (s - c)) s = (a + b + c) / 2 ์Šค์ฝ”ํ”„(scope) ์•ž์„  ์˜ˆ์ œ๋ฅผ ์ž์„ธํžˆ ๋“ค์—ฌ๋‹ค๋ณด๋ฉด ๋ณ€์ˆ˜ ์ด๋ฆ„ a, b, c๋ฅผ ๋‘ ๋„“์ด ํ•จ์ˆ˜์— ํ•œ ๋ฒˆ์”ฉ, ์ด ๋‘ ๋ฒˆ์”ฉ ์‚ฌ์šฉํ–ˆ๋‹ค. ์ด๊ฒŒ ์–ด๋–ป๊ฒŒ ๊ฐ€๋Šฅํ•œ ๊ฑธ๊นŒ? ๋‹ค์Œ GHCi ์‹œํ€€์Šค๋ฅผ ์‚ดํŽด๋ณด์ž. Prelude> let r = 0 Prelude> let area r = pi * r ^ 2 Prelude> area 5 78.53981633974483 ์•ž์˜ let r = 0 ์ •์˜ ๋•Œ๋ฌธ์— ๋„“์ด๊ฐ€ 0์„ ๋ฐ˜ํ™˜ํ–ˆ๋‹ค๋ฉด ๋ถˆํŽธํ•˜๊ณ  ๋†€๋ผ์šด ์ผ์ด์—ˆ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ ์ผ์ด ์ผ์–ด๋‚˜์ง€ ์•Š์€ ์ด์œ ๋Š” ๋‘ ๋ฒˆ์งธ๋กœ r์„ ์ •์˜ํ•  ๋•Œ๋Š” ๋˜ ๋‹ค๋ฅธ r์— ๋Œ€ํ•ด ๋งํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์‚ฌ๋žŒ์ด ์กด์ด๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง€๋Š”์ง€ ์ƒ๊ฐํ•ด ๋ณด์ž. ์กด์ด ํ•œ ๋ช…๋งŒ ์žˆ๋Š” ๋ฌธ๋งฅ์—์„œ ์šฐ๋ฆฌ๋Š” ์•„๋ฌด ํ˜ผ๋ž€ ์—†์ด "์กด"์— ๋Œ€ํ•ด ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—๋„ ๋ฌธ๋งฅ๊ณผ ๋น„์Šทํ•œ ์Šค์ฝ”ํ”„๋ผ๋Š” ๊ฐœ๋…์ด ์žˆ๋‹ค. ์Šค์ฝ”ํ”„์˜ ๊ธฐ์ˆ ์ ์ธ ๋ฉด๋ฉด์„ ๋‹น์žฅ์€ ์„ค๋ช…ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ์ง€๊ธˆ์€ ์—ฌ๋Ÿฌ๋ถ„์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ „๋‹ฌํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’์ด๊ณ  ํ•จ์ˆ˜ ์ •์˜์—์„œ ๋ณ€์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ๋ถ€๋ฅด๋Š”์ง€์™€ ๋ฌด๊ด€ํ•˜๋‹ค๋Š” ๊ฒƒ๋งŒ ์•Œ์•„๋‘์ž. ๊ทธ๋ ‡๊ธด ํ•˜์ง€๋งŒ ๋ณ€์ˆ˜์— ์ ์ ˆํ•œ ๊ณ ์œ  ์ด๋ฆ„์„ ๋ถ™์ด๋ฉด ๋…์ž๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ์‰ฌ์›Œ์ง„๋‹ค. ์š”์•ฝ ๋ณ€์ˆ˜๋Š” ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. ๊ฐ’์€ ์ž„์˜์˜ ํ•˜์Šค ์ผˆ ํ‘œํ˜„์‹์ด๋‹ค. ๋ณ€์ˆ˜๋Š” ์Šค์ฝ”ํ”„ ๋‚ด์—์„œ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•จ์ˆ˜๋Š” ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ ์ž‘์„ฑ์„ ๋•๋Š”๋‹ค. ํ•จ์ˆ˜๋Š” ํ•˜๋‚˜๋ณด๋‹ค ๋งŽ์€ ์ธ์ž๋ฅผ ์ทจํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์†Œ์Šค ํŒŒ์ผ ์•ˆ์—์„œ ์ฝ”๋“œ๊ฐ€ ์•„๋‹Œ ํ…์ŠคํŠธ์ธ ์ฃผ์„๋„ ๋ฐฐ์› ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์— ๋ฌธ์ž๋ฟ ์•„๋‹ˆ๋ผ ์ˆซ์ž๋„ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์Šค ์ผˆ ๋ณ€์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•˜์ง€๋งŒ ๊ทธ๋‹ค์Œ์—๋Š” ๋ฌธ์ž, ์ˆซ์ž, ๋ฐ‘์ค„(_), ๋”ฐ์˜ดํ‘œ(') ๋ฌด์—‡์ด๋“  ๊ฐ€๋Šฅํ•˜๋‹ค. โ†ฉ 3 ์ง„์œ„ ๊ฐ’ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Truth_values ๋™๋“ฑ ๋น„๊ต์™€ ๊ธฐํƒ€ ๋น„๊ต๋“ค ๋ถˆ๋ฆฌ์–ธ ๊ฐ’ ํƒ€์ž… ์ž…๋ฌธ ์ค‘์œ„ ์—ฐ์‚ฐ์ž(infix operator) ๋ถˆ๋ฆฌ์–ธ ์—ฐ์‚ฐ ๊ฐ€๋“œ(guard) where์™€ ๊ฐ€๋“œ ๋™๋“ฑ ๋น„๊ต์™€ ๊ธฐํƒ€ ๋น„๊ต๋“ค ๋ฐ”๋กœ ์•ž ์žฅ์—์„œ ๋‹ค์Œ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋“ฑํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ–ˆ์—ˆ๋‹ค. r = 5 ์ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ํ‰๊ฐ€ํ•˜๋ฉด (์ด ์ •์˜์˜ ์Šค์ฝ”ํ”„ ์•ˆ์—์„œ) r์„ ๋ชจ๋‘ 5๋กœ ์น˜ํ™˜ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. ๋น„์Šทํ•˜๊ฒŒ, ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ํ‰๊ฐ€ํ•˜๋ฉด f x = x + 3 f ๋‹ค์Œ์— ์ˆซ์ž(f์˜ ์ธ์ž)๊ฐ€ ๋ถ™๋Š” ๋ชจ๋“  ์ž๋ฆฌ๊ฐ€ ๊ทธ ์ˆซ์ž์— 3์„ ๋”ํ•œ ๊ฒƒ์œผ๋กœ ์น˜ํ™˜๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ˆ˜ํ•™์—์„œ ๋“ฑํ˜ธ๋Š” ์—ญ์‹œ ์ค‘์š”ํ•˜์ง€๋งŒ ๋ฏธ๋ฌ˜ํ•˜๊ฒŒ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ๋„ ์“ฐ์ธ๋‹ค. ๊ทธ ์˜ˆ๋กœ ๋‹ค์Œ์˜ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋ฅผ ๋ณด์ž. ์˜ˆ: ๋‹ค์Œ ๋“ฑ์‹์„ ํ‘ธ์‹œ์˜ค. x + 3 = 5 ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ์˜ ๊ด€์‹ฌ์‚ฌ๋Š” ๊ฐ’ 5๋ฅผ x + 3์œผ๋กœ ํ‘œํ˜„ํ•˜๊ฑฐ๋‚˜ x + 3์„ 5๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋“ฑ์‹ x + 3 = 5๋ฅผ<NAME>(proposition)๋กœ์„œ ๋ณด๊ณ , ์–ด๋–ค ์ˆซ์ž x์— 3๋ฅผ ๋”ํ•˜๋ฉด 5๊ฐ€ ๋‚˜์˜จ๋‹ค๊ณ  ์ฝ๋Š”๋‹ค. ์ด ๋“ฑ์‹์„ ํ‘ผ๋‹ค๋Š” ๊ฒƒ์€ ์ด<NAME>๋ฅผ ์ฐธ์œผ๋กœ ๋งŒ๋“œ๋Š” x ๊ฐ’์„ (๋งŒ์•ฝ ์žˆ๋‹ค๋ฉด) ์ฐพ์•„๋‚ธ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ด ์˜ˆ์—์„œ๋Š” ์ดˆ๋“ฑ ๋Œ€์ˆ˜๋ฅผ ์ด์šฉํ•ด x = 2๋ผ ํŒ๋‹จ ๋‚ด๋ฆด ์ˆ˜ ์žˆ๋‹ค. (์ฆ‰ 2๋Š” ์ด ๋“ฑ์‹์„ ์ฐธ์œผ๋กœ ๋งŒ๋“œ๋Š” ์ˆซ์ž์ด๋ฉฐ 2 + 3 = 5์ด๋‹ค.) ๊ฐ’๋“ค์ด ๋™์ผํ•œ์ง€ ๋น„๊ตํ•˜๋Š” ์ผ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋„ ์œ ์šฉํ•˜๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ๊ทธ๋Ÿฐ ๊ฒ€์‚ฌ๋Š” ๋‹จ์ˆœํžˆ ๋“ฑ์‹์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ๋“ฑํ˜ธ๋Š” ์ด๋ฏธ ์ •์˜๋ฅผ ์œ„ํ•ด ์“ฐ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜์Šค์ผˆ์€ ๊ทธ ๋Œ€์‹  ์ด์ค‘ ๋“ฑํ˜ธ ==๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์œ„์˜<NAME>๋ฅผ GHCi์— ์ž…๋ ฅํ•ด ๋ณด์ž. Prelude> 2 + 3 == 5 True GHCi๋Š” "True"๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, 2 + 3์ด 5์™€ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฐธ์ด ์•„๋‹Œ ๋“ฑ์‹์„ ์“ฐ๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? Prelude> 7 + 3 == 5 False ๋…ผ๋ฆฌ<NAME>ํ•˜๋‹ค. ๋‹ค์Œ์œผ๋กœ๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ํ•จ์ˆ˜๋ฅผ ์ด๋Ÿฐ ๊ฒ€์‚ฌ์— ํ™œ์šฉํ•ด ๋ณด์ž. ์ด ๊ณผ๋ชฉ์„ ์‹œ์ž‘ํ•  ๋•Œ ์–ธ๊ธ‰ํ•œ ํ•จ์ˆ˜ f๋ฅผ ๊ฐ€์ง€๊ณ  ์‹คํ—˜ํ•ด ๋ณด์ž. Prelude> let f x = x + 3 Prelude> f 2 == 5 True ์˜ˆ์ƒํ•œ ๋Œ€๋กœ๋‹ค. f 2๋Š” 2 + 3์œผ๋กœ ํ‰๊ฐ€๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‘ ์ˆซ์ž ๊ฐ’ ์ค‘ ๋ฌด์—‡์ด ํฐ์ง€ ๋น„๊ตํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜์Šค์ผˆ์€ <(๋ฏธ๋งŒ), >(์ดˆ๊ณผ), <=(์ดํ•˜), >=(์ด์ƒ) ๋“ฑ ๋งŽ์€ ๊ฒ€์‚ฌ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด ๊ฒ€์‚ฌ๋“ค์€ ==(๋™์ผ)๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด <๋ฅผ ์ด์ „ ๊ณผ๋ชฉ์˜ area ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์จ์„œ ํŠน์ • ๋ฐ˜์ง€๋ฆ„์˜ ์›์ด ์–ด๋–ค ๊ฐ’๋ณด๋‹ค ์ž‘์€์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. Prelude> let area r = pi * r ^ 2 Prelude> area 5 < 50 False ๋ถˆ๋ฆฌ์–ธ ๊ฐ’ GHCi๊ฐ€ ์ด๋Ÿฐ ์‚ฐ์ˆ <NAME>๋ฅผ ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€ ๊ฒฐ์ •ํ•  ๋•Œ ์‹ค์ œ๋กœ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š” ๊ฑธ๊นŒ? ์กฐ๊ธˆ ๋‹ค๋ฅธ ์ด์•ผ๊ธฐ์ง€๋งŒ ์—ฐ๊ด€์ด ์žˆ๋Š” ๋‹ค์Œ ๋ฌธ์ œ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. GHCi์— ์‚ฐ์ˆ  ํ‘œํ˜„์‹์„ ์ž…๋ ฅํ•˜๋ฉด ๊ทธ ํ‘œํ˜„์‹์€ ํ‰๊ฐ€๋˜๊ณ , ๊ฒฐ๊ณผ ์‚ฐ ์ˆ ๊ฐ’์ด ํ™”๋ฉด์— ํ‘œ์‹œ๋œ๋‹ค. Prelude> 2 + 2 ์ด ์‚ฐ์ˆ  ํ‘œํ˜„์‹์„ ํ•ญ๋“ฑ ๋น„๊ต๋กœ ๋Œ€์ฒดํ•˜๋ฉด ๋น„์Šทํ•œ ์ผ์ด ๋ฒŒ์–ด์ง„๋‹ค. Prelude> 2 == 2 True "4"๋Š” ์ผ์ข…์˜ ์…ˆ, ์ˆ˜๋Ÿ‰ ๊ฐ™์€ ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. "True"๋Š”<NAME>์˜ ์ฐธ์„ ์˜๋ฏธํ•˜๋Š” ๊ฐ’(value)์ด๋‹ค. ์ด๋Ÿฐ ๊ฐ’์„ ์ง„์œ„ ๊ฐ’(true value) ๋˜๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’(boolean value) 1์ด๋ผ ๋ถ€๋ฅธ๋‹ค. ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์€ ์˜ค์ง True์™€ False๋งŒ ์กด์žฌํ•œ๋‹ค. ํƒ€์ž… ์ž…๋ฌธ True์™€ False๋Š” ๋‹จ์ˆœํžˆ ๋น„์œ ๊ฐ€ ์•„๋‹ˆ๋ผ ์‹ค์ œ ๊ฐ’์ด๋‹ค. ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์€ ํ•˜์Šค์ผˆ์—์„œ ์ˆซ์ž ๊ฐ’๊ณผ ๊ฐ™์€ ์‹ ๋ถ„์„ ๊ฐ€์ง€๊ณ  ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋‹ค. Prelude> True == True True Prelude> True == False False True๋Š” True์™€ ๊ฐ™๊ณ  True๋Š” False์™€ ๊ฐ™์ง€ ์•Š๋‹ค. ์ด์ œ ๋นจ๋ฆฌ ๊ฐ€๋ณด์ž. ๊ทธ๋Ÿฌ๋ฉด 2๋Š” True์™€ ๊ฐ™์„๊นŒ? Prelude> 2 == True <interactive>:1:0: No instance for (Num Bool) arising from the literal โ€˜2โ€™ at <interactive>:1:0 Possible fix: add an instance declaration for (Num Bool) In the first argument of โ€˜(==)โ€™, namely โ€˜2โ€™ In the expression: 2 == True In an equation for โ€˜itโ€™: it = 2 == True ๋•ก! ์ด ์งˆ๋ฌธ์€ ์• ์ดˆ์— ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ˆซ์ž๋ฅผ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒƒ๊ณผ ๋น„๊ตํ•˜๊ฑฐ๋‚˜ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ถˆ๋ฆฌ์–ธ์ด ์•„๋‹Œ ๊ฐ’๊ณผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์Šค์ผˆ์€ ์ด ๊ฐœ๋…์„ ๋ฐ›์•„๋“ค์ด๊ณ , ์œ„์˜ ๋ชป์ƒ๊ธด ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋Š” ์ด์— ๋Œ€ํ•ด ๋ถˆํ‰ํ•˜๊ณ  ์žˆ๋‹ค. ๋ณต์žกํ•œ ๋ถ€๋ถ„์„ ๊ฑด๋„ˆ๋›ฐ๋ฉด ์ด ๋ฉ”์‹œ์ง€๋Š” ==์˜ ์ขŒ๋ณ€์— ์ˆซ์ž(Num)๊ฐ€ ์žˆ๊ณ , ๊ทธ๋ž˜์„œ ์šฐ๋ณ€์— ์ˆซ์ž๋ฅผ ๊ธฐ๋Œ€ํ•œ๋‹ค๊ณ  ๋งํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’(Bool)์€ ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ํ•ญ๋“ฑ ๊ฒ€์‚ฌ๊ฐ€ ์‹คํŒจํ•œ๋‹ค. ์ฆ‰ ๊ฐ’์€ ํƒ€์ž…(type)์„ ๊ฐ€์ง€๋ฉฐ, ํƒ€์ž…์€ ์šฐ๋ฆฌ๊ฐ€ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์žˆ๊ณ  ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์—†๋Š”์ง€, ๊ทธ ํ•œ๊ณ„๋ฅผ ์ •์˜ํ•œ๋‹ค. True์™€ False๋Š” Bool ํƒ€์ž…์˜ ๊ฐ’์ด๋‹ค. 2๋Š” ์กฐ๊ธˆ ๋ณต์žกํ•œ๋ฐ, ์ˆซ์ž ํƒ€์ž…์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด์— ๊ด€ํ•œ ์„ค๋ช…์€ ๋‚˜์ค‘์œผ๋กœ ๋ฏธ๋ฃจ๊ฒ ๋‹ค. ํƒ€์ž…์€ ๋งค์šฐ ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ๋‹ค. ๊ฐ’๋“ค์ด ๋ง์ด ๋˜๋Š” ๊ทœ์น™ ํ•˜์—์„œ ํ–‰๋™ํ•˜๋„๋ก ์ œํ•œํ•˜์—ฌ, ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ์ž‘์„ฑ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ ๋‹ค. ํƒ€์ž…์€ ํ•˜์Šค์ผˆ์—์„œ ์•„์ฃผ ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž…์— ๋Œ€ํ•ด์„œ ์•ž์œผ๋กœ ์ž์ฃผ ๋…ผํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ์ค‘์œ„ ์—ฐ์‚ฐ์ž(infix operator) 2 == 2 ๊ฐ™์€ ํ•ญ๋“ฑ ๊ฒ€์‚ฌ๋Š” 2 + 2์ฒ˜๋Ÿผ ๋‹จ์ง€ ํ‘œํ˜„์‹์ด๋ฉฐ ๊ฑฐ์˜ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ฐ’์œผ๋กœ ํ‰๊ฐ€๋œ๋‹ค. ํ‘œํ˜„์‹์ด๋ผ๋Š” ๊ฒƒ์€ ์•ž์„  ์˜ˆ์ œ์˜ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์—์„œ๋„ ๋งํ•˜๊ณ  ์žˆ๋‹ค. In the expression: 2 == True ์šฐ๋ฆฌ๊ฐ€ ํ”„๋กฌํ”„ํŠธ์— 2 == 2๋ฅผ ์ž…๋ ฅํ•˜๊ณ  GHCi๊ฐ€ True๋กœ "๋Œ€๋‹ต"ํ•  ๋•Œ, GHCi๋Š” ๋‹จ์ง€ ํ‘œํ˜„์‹์„ ํ‰๊ฐ€ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ์‚ฌ์‹ค ==๋Š” ๊ทธ ์ž์ฒด๊ฐ€ ์ธ์ž ๋‘ ๊ฐœ๋ฅผ ๋ฐ›๋Š” ํ•จ์ˆ˜๋‹ค. (๋‘ ์ธ์ž๊ฐ€ ๊ฐ๊ฐ ํ•ญ๋“ฑ ๊ฒ€์‚ฌ์˜ ์ขŒ๋ณ€๊ณผ ์šฐ๋ณ€์— ์œ„์น˜ํ•˜๋Š”) ํ•˜์ง€๋งŒ ๊ทธ ๋ฌธ๋ฒ•์€ ๋‹ค์†Œ ์ธ์ƒ์ ์ด๋‹ค. ํ•˜์Šค์ผˆ์€ 2-์ธ์ž ํ•จ์ˆ˜๋ฅผ, ์ธ์ž๋“ค ์‚ฌ์ด์— ์œ„์น˜ํ•˜๋Š” ์ค‘์œ„ ์—ฐ์‚ฐ์ž๋กœ์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค. ํ•จ์ˆ˜ ์ด๋ฆ„์— ์•ŒํŒŒ๋ฒณ์ด๋‚˜ ์ˆซ์ž๊ฐ€ ๋“ค์–ด๊ฐ€์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ด๋Ÿฐ ์ค‘์œ„ ํ‘œ๊ธฐ๋Š” ํ”ํ•œ ์šฉ๋ฒ•์ด๋‹ค. ์ด๋Ÿฐ ํ•จ์ˆ˜๋ฅผ "ํ‘œ์ค€" ๋ฐฉ์‹์œผ๋กœ (ํ•จ์ˆ˜ ์ด๋ฆ„์ด ์ธ์ž๋“ค๋ณด๋‹ค ์•ž์— ์˜ค๋Š”, ์ „์œ„ ์—ฐ์‚ฐ์ž๋กœ์„œ) ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•จ์ˆ˜ ์ด๋ฆ„์„ ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์•ผ ํ•œ๋‹ค. ์ฆ‰ ๋‹ค์Œ ํ‘œํ˜„์‹๋“ค์€ ์™„๋ฒฝํžˆ ๋™๋“ฑํ•˜๋‹ค. Prelude> 4 + 9 == 13 True Prelude> (==) (4 + 9) 13 True ์—ฌ๊ธฐ์„œ (==)๊ฐ€ ์•ž ๊ณผ๋ชฉ์˜ areaRec์™€ ๋น„์Šทํ•˜๊ฒŒ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฐœ๋…์€ ๋‹ค๋ฅธ ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž๋“ค(<, >, <=, >=)๊ณผ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž๋“ค(+, * ๋“ฑ)์—๋„ ์ ์šฉ๋œ๋‹ค. ์ด๊ฒƒ๋“ค์€ ๋ชจ๋‘ ์ธ์ž๋ฅผ ๋‘ ๊ฐœ ์ทจํ•˜๋Š” ํ•จ์ˆ˜์ด๋ฉฐ ๋ณดํ†ต์€ ์ค‘์œ„ ์—ฐ์‚ฐ์ž๋กœ ์ž‘์„ฑ๋œ๋‹ค. ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด ํ•˜์Šค์ผˆ์—์„œ ์‹ค์ฒด๊ฐ€ ์žˆ๋Š” ๊ฒƒ๋“ค์€ ๊ฐ’ ๋˜๋Š” ํ•จ์ˆ˜๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ถˆ๋ฆฌ์–ธ ์—ฐ์‚ฐ ํ•˜์Šค์ผˆ์€ ์ง„์œ„ ๊ฐ’์„ ๋…ผ๋ฆฌ<NAME>๋กœ์„œ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ ํ•จ์ˆ˜๋ฅผ 3๊ฐœ ์ œ๊ณตํ•œ๋‹ค. (&&)์€ and ์—ฐ์‚ฐ(๋…ผ๋ฆฌ๊ณฑ)์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋‘ ๋ถˆ ๋ฆฌ์–ธ ๊ฐ’์ด ์ฃผ์–ด์งˆ ๋•Œ, ๋‘˜ ๋‹ค True์ธ ๊ฒฝ์šฐ True๋กœ ํ‰๊ฐ€๋˜๊ณ  ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ False๋กœ ํ‰๊ฐ€๋œ๋‹ค. Prelude> (3 < 8) && (False == False) True Prelude> (&&) (6 <= 5) (1 == 1) False (||)์€ or ์—ฐ์‚ฐ(๋…ผ๋ฆฌํ•ฉ)์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋‘ ๋ถˆ ๋ฆฌ์–ธ ๊ฐ’์ด ์ฃผ์–ด์งˆ ๋•Œ, ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ True์ธ ๊ฒฝ์šฐ True๋กœ, ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ False๋กœ ํ‰๊ฐ€๋œ๋‹ค. Prelude> (2 + 2 == 5) || (2 > 0) True Prelude> (||) (18 == 17) (9 >= 11) False not์€ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์˜ ๋ฐ˜์ „(negation)์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ฆ‰ True๋Š” False๋กœ, False๋Š” True๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. Prelude> not (5 * 2 == 10) False ํ•˜์Šค ์ผˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” ๊ฐ™์ง€ ์•Š์Œ(not equal to)์„ ์œ„ํ•œ ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž ํ•จ์ˆ˜ (/=)๊ฐ€ ์ด๋ฏธ ๋“ค์–ด์žˆ์ง€๋งŒ, ์šฐ๋ฆฌ๊ฐ€ ์‰ฝ๊ฒŒ ์ง์ ‘ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. x /= y = not (x == y) ์ค‘์œ„ ํ‘œ๊ธฐ๋Š” ์—ฐ์‚ฐ์ž๋ฅผ ์ •์˜ํ•  ๋•Œ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ASCII ๊ธฐํ˜ธ๋“ค(ํ‚ค๋ณด๋“œ์—์„œ ๊ฐ€์žฅ ํ”ํžˆ ์“ฐ์ด๋Š” ๊ธฐํ˜ธ๋“ค)์„ ์‚ฌ์šฉํ•ด์„œ ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ์—ฐ์‚ฐ์ž๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. ๊ฐ€๋“œ(guard) ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์€ ๋ถˆ๋ฆฌ์–ธ ์—ฐ์‚ฐ์ž๋ฅผ ์ข…์ข… ๊ฐ„ํŽธํ•˜๊ณ  ์ถ•์•ฝ๋œ ๊ตฌ๋ฌธ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๋™์ผํ•œ ๋กœ์ง์„ ๋‹ค๋ฅธ ์‹์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์„ ํŽธ์˜ ๋ฌธ๋ฒ•(syntactic sugar)์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, ์‚ฌ๋žŒ์ด ๋ณด๊ธฐ์— ์ฝ”๋“œ๊ฐ€ ๋” ์ด์˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฐ ๋ฌธ๋ฒ• ์ค‘ ๊ฐ€๋“œ๋ฅผ ๋จผ์ € ์•Œ์•„๋ณด์ž. ๊ฐ€๋“œ๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์— ๊ธฐ๋ฐ˜ํ•ด ๋‹จ์ˆœํ•˜์ง€๋งŒ ๊ฐ•๋ ฅํ•œ ํ•จ์ˆ˜๋“ค์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ ˆ๋Œ“๊ฐ’ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์ž. ์‹ค์ˆ˜์˜ ์ ˆ๋Œ“๊ฐ’์€ ๊ทธ ์ˆ˜์—์„œ ๊ธฐํ˜ธ๋ฅผ ์ œ๊ฑฐํ•œ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์Œ์ˆ˜ ๋ฉด(0๋ณด๋‹ค ์ž‘์œผ๋ฉด) ๊ทธ ๊ธฐํ˜ธ๋ฅผ ๋ฐ˜์ „์‹œํ‚ค๊ณ  ์Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด ๊ทธ๋Œ€๋กœ ๋‘”๋‹ค. ๊ทธ ์ •์˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ์„ ์ˆ˜ ์žˆ๋‹ค. x = { , if โ‰ฅ โˆ’ , if < ์—ฌ๊ธฐ์„œ |x|๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ์“ฐ์ด๋Š” ์‹ค์ œ ํ‘œํ˜„์‹์€ x์— ๊ด€ํ•ด ์„ธ์šด<NAME>๋“ค์˜ ์ง‘ํ•ฉ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค. ๋งŒ์•ฝ x โ‰ฅ 0์ด ์ฐธ์ด๋ฉด ์ฒซ ๋ฒˆ์งธ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ x < 0์ธ ๊ฒฝ์šฐ์—๋Š” ๋‘ ๋ฒˆ์งธ ํ‘œํ˜„์‹์„ ๋Œ€์‹  ์‚ฌ์šฉํ•œ๋‹ค. ์šฐ๋ฆฌ์—๊ฒ ์ด ๊ฒฐ์ • ๊ณผ์ •์„ ํ‘œํ˜„ํ•  ์ˆ˜๋‹จ์ด ํ•„์š”ํ•˜๋‹ค. ๊ฐ€๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 2 ์˜ˆ์ œ: ์ ˆ๋Œ“๊ฐ’ ํ•จ์ˆ˜. absolute x | x < 0 = -x | otherwise = x ํ™•์‹คํžˆ ์œ„์˜ ์ฝ”๋“œ๋Š” ์ด์— ๋Œ€์‘ํ•˜๋Š” ์ˆ˜ํ•™์  ์ •์˜๋งŒํผ์ด๋‚˜ ๊ฐ€๋…์„ฑ ์žˆ๋‹ค. ์ด ์ •์˜๋ฅผ ํ•˜๋‚˜์”ฉ ํ•ด๋ถ€ํ•ด ๋ณด์ž. ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜ ์ •์˜์ฒ˜๋Ÿผ ์‹œ์ž‘ํ•œ๋‹ค. ํ•จ์ˆ˜ ์ด๋ฆ„ absolute๋ฅผ ์ ๊ณ , ์ด ํ•จ์ˆ˜๊ฐ€ ๋‹จ์ผ ๋งค๊ฐœ๋ณ€์ˆ˜ x๋ฅผ ์ทจํ•œ๋‹ค๊ณ  ์•Œ๋ฆฐ๋‹ค. =์™€ ์ •์˜์˜ ์šฐ๋ณ€์„ ์ ๋Š” ๋Œ€์‹  ์ค„ ๋ฐ”๊ฟˆ์„ ํ•˜๊ณ , ๋‘ ๋Œ€์ฒด๋ฌธ์„ ๋ณ„๊ฐœ์˜ ์ค„์— ๋†“์•˜๋‹ค. 3 ์ด ๋Œ€์ฒด๋ฌธ๋“ค์ด ๊ฐ€๋“œ๋‹ค. ๊ณต๋ฐฑ์€ ๋‹จ์ˆœํžˆ ๋ฏธ์ ์ธ ์ด์œ ๋กœ ๋„ฃ์€ ๊ฒƒ์ด ์•„๋‹ˆ๋ฉฐ ์ฝ”๋“œ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํŒŒ์‹ฑ ๋˜๋ ค๋ฉด ํ•„์ˆ˜๋‹ค. ๊ฐ๊ฐ์˜ ๊ฐ€๋“œ๋Š” ํŒŒ์ดํ”„ ๋ฌธ์ž์ธ |์œผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ํŒŒ์ดํ”„ ๋’ค์—๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์œผ๋กœ ํ‰๊ฐ€๋  ํ‘œํ˜„์‹(๋ถˆ๋ฆฌ์–ธ ์กฐ๊ฑด์‹ ๋˜๋Š” ์ˆ ์–ด์‹(predicate)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฆ„)์„ ๋†“๊ณ , ๊ทธ ๋’ค์—๋Š” ์ •์˜์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์ด ์˜จ๋‹ค. ํ•จ์ˆ˜๋Š” ์ˆ ์–ด์‹์ด True๋กœ ํ‰๊ฐ€๋˜๋Š” ์ค„์˜ ๋“ฑํ˜ธ์™€ ๊ทธ ์šฐ๋ณ€๋งŒ์„ ์‚ฌ์šฉํ•œ๋‹ค. otherwise ๋ถ„๊ธฐ๋Š” ์„ ํ–‰ํ•˜๋Š” ์ˆ ์–ด์‹๋“ค ์ค‘ True๋กœ ํ‰๊ฐ€๋˜๋Š” ๊ฒŒ ์—†์„ ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ์ด ๊ฒฝ์šฐ x๊ฐ€ 0๋ณด๋‹ค ์ž‘์ง€ ์•Š๋‹ค๋ฉด x๋Š” 0๊ณผ ๊ฐ™๊ฑฐ๋‚˜ ๊ทธ๋ณด๋‹ค ์ปค์•ผ๋งŒ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋งˆ์ง€๋ง‰ ์ˆ ์–ด์‹์€ x >= 0์œผ๋กœ ์“ธ ์ˆ˜๋„ ์žˆ์—ˆ์ง€๋งŒ, otherwise๋„ ์ž˜ ์ž‘๋™ํ•œ๋‹ค. ์ž ๊น otherwise์˜ ์ด๋ฉด์— ๋ฌธ๋ฒ•์ƒ์˜ ๋งˆ๋ฒ•์€ ์—†๋‹ค. otherwise๋Š” ํ•˜์Šค์ผˆ์˜ ๋‹ค๋ฅธ ๊ธฐ๋ณธ ๋ณ€์ˆ˜, ํ•จ์ˆ˜๋“ค๊ณผ ํ•จ๊ป˜ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉฐ ๋‹จ์ˆœํžˆ otherwise = True ์ด๋‹ค. ์ด ์ •์˜๋Š” otherwise๋ฅผ ์ „๋ฐฉ ์ˆ˜๋น„ ๊ฐ€๋“œ๋กœ ๋งŒ๋“ ๋‹ค. ๊ฐ€๋“œ ์ˆ ์–ด์‹์˜ ํ‰๊ฐ€๋Š” ์ˆœ์ฐจ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์ˆ ์–ด์‹๋“ค ์ค‘ ํ•˜๋‚˜๋„ True๋กœ ํ‰๊ฐ€๋˜์ง€ ์•Š์„ ๋•Œ๋งŒ otherwise์— ๋„๋‹ฌํ•˜๊ฒŒ ๋œ๋‹ค. (๊ทธ๋Ÿฌ๋‹ˆ otherwise๋ฅผ ๋งˆ์ง€๋ง‰ ๊ฐ€๋“œ๋กœ ์„ธ์› ๋Š”์ง€ ํ•ญ์ƒ ํ™•์ธํ•˜์ž!) ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ํ•ญ์ƒ otherwise๋ฅผ ๋‘๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์–ด๋–ค ์ž…๋ ฅ์— ๋Œ€ํ•ด ์•„๋ฌด ์ˆ ์–ด ์‹๋„ ์ฐธ์ด ์•„๋‹ˆ๋ฉด ๋ชป์ƒ๊ธด ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ž ๊น ์ฒซ ๋ฒˆ์งธ ๊ฐ€๋“œ์—์„œ | x < 0 = -x x๋ฅผ ๋ฐ˜์ „ํ•˜๊ธฐ ์œ„ํ•ด ๋งˆ์ด๋„ˆ์Šค ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ๊ธฐํ˜ธ ๋ฐ˜์ „์„ ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์€ ์ผ์ข…์˜ ํŠน์ˆ˜ ์‚ฌ๋ก€๋‹ค. -๋Š” ์ธ์ž๋ฅผ ํ•˜๋‚˜ ๋ฐ›์•„์„œ 0 - x๋กœ ํ‰๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉฐ ์ถ•์•ฝ ๋ฌธ๋ฒ•์ผ ๋ฟ์ด๋‹ค. ์ด๋Ÿฐ ์ถ•์•ฝ์€ ํŽธ๋ฆฌํ•˜์ง€๋งŒ ๊ฐ€๋” (-)๋ฅผ ์‹ค์ œ๋กœ ํ•จ์ˆ˜๋กœ์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ(๋บ„์…ˆ ์—ฐ์‚ฐ์ž)๊ณผ ์ถฉ๋Œํ•ด์„œ ์„ฑ๊ฐ€์‹ฌ์˜ ์›์ธ์ด ๋  ์ˆ˜ ์žˆ๋‹ค. (๊ด„ํ˜ธ๋กœ ๋ฌถ์ง€ ์•Š๊ณ  ์Œ์ˆ˜ ๋„ค ๊ฐœ๋ฅผ ๊ฐ€์ง€๊ณ  ๋บ„์…ˆ์„ ์„ธ ๋ฒˆ ํ•ด๋ณด์ž) ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์Œ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  absolute๋ฅผ ํ…Œ์ŠคํŠธํ•ด ๋ณด๋ ค๋ฉด ์ด๋ ‡๊ฒŒ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. Prelude> absolute (-10) 10 where์™€ ๊ฐ€๋“œ where ์ ˆ์€ ํŠนํžˆ ๊ฐ€๋“œ์™€ ํ•จ๊ป˜ ์“ธ ๋•Œ ํŽธ๋ฆฌํ•˜๋‹ค. 2์ฐจ ๋ฐฉ์ •์‹ x + x c 0 ์˜ (์‹ค์ˆ˜์ธ) ํ•ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด์ž. numOfSolutions a b c | disc > 0 = 2 | disc == 0 = 1 | otherwise = 0 where disc = b^2 - 4*a*c where ์ •์˜๋Š” ๋ชจ๋“  ๊ฐ€๋“œ์˜ ์Šค์ฝ”ํ”„ ๋‚ด์— ์žˆ์œผ๋ฏ€๋กœ disc์˜ ํ‘œํ˜„์‹์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ์ด ์šฉ์–ด๋Š” ์ˆ˜ํ•™์ž์ด์ž ์ฒ ํ•™์ž์ธ ์กฐ์ง€ ๋ถ€์šธ(George Boole)์— ๋Œ€ํ•œ ํ—Œ์ •์ด๋‹ค. โ†ฉ ์ด ํ•จ์ˆ˜๋Š” ์ด๋ฏธ ํ•˜์Šค์ผˆ์ด abs๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ œ๊ณตํ•˜๋ฏ€๋กœ ์‹ค์ „์—์„œ๋Š” ์ง์ ‘ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. โ†ฉ ์—ฌ๋Ÿฌ ์ค„์„ ํ•ฉ์ณ์„œ ํ•œ ์ค„์— ์ „๋ถ€ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์—ˆ์ง€๋งŒ ๊ทธ๋Ÿฌ๋ฉด ์ฝ๊ธฐ๊ฐ€ ๋” ํž˜๋“ค์—ˆ์„ ๊ฒƒ์ด๋‹ค. โ†ฉ 4 ํƒ€์ž…์˜ ๊ธฐ์ดˆ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Type_basics ์„œ๋ฌธ ์™œ ํƒ€์ž…์ด ์œ ์šฉํ•œ๊ฐ€ ๋ฐ˜์‘ํ˜• ์ปค๋งจ๋“œ :type ํ™œ์šฉํ•˜๊ธฐ ๋ฌธ์ž์™€ ๋ฌธ์ž์—ด ํ•จ์ˆ˜ํ˜• ํƒ€์ž… ์˜ˆ์ œ: not ์˜ˆ์ œ: chr์™€ ord ์ธ์ž๊ฐ€ ๋‘˜ ์ด์ƒ์ธ ํ•จ์ˆ˜ ์‹ค์ „ ์˜ˆ์ œ: openWindow ์ฝ”๋“œ ์•ˆ์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜ ํƒ€์ž… ์ถ”๋ก  ํƒ€์ž…๊ณผ ๊ฐ€๋…์„ฑ ํƒ€์ž…์€ ์˜ค๋ฅ˜๋ฅผ<NAME>๋‹ค ๋…ธํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ํƒ€์ž…์€ ๋น„์Šทํ•œ ๊ฐ’๋“ค์„ ๋ฒ”์ฃผ๋กœ ๋ฌถ๋Š”๋‹ค. ํ•˜์Šค์ผˆ์˜ ํƒ€์ž… ์ฒด๊ณ„๋Š” ์ฝ”๋“œ ์ƒ์—์„œ์˜ ์‹ค์ˆ˜๋ฅผ ์ค„์—ฌ์ฃผ๋Š” ๊ฐ•๋ ฅํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ์„œ๋ฌธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๊ฐœ์ฒด(entity)๋ฅผ ๋‹ค๋ฃจ๋Š” ์ผ์ด๋‹ค. ๋‘ ์ˆ˜๋ฅผ ๋”ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. 2 + 3 2์™€ 3์€ ๋ฌด์—‡์ธ๊ฐ€? ๋ฌผ๋ก  ์ˆซ์ž๋‹ค. ๊ฐ€์šด๋ฐ์˜ ๋”ํ•˜๊ธฐ ๊ธฐํ˜ธ๋Š” ๋ฌด์—‡์ธ๊ฐ€? ๋ถ„๋ช… ์ˆซ์ž๋Š” ์•„๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ˆซ์ž ๋‘ ๊ฐœ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ, ์ฆ‰ ๋ง์…ˆ์„ ๋œปํ•œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์˜ ์ด๋ฆ„์„ ๋ฌผ์–ด๋ณด๊ณ  "Hello" ๋ฉ”์‹œ์ง€๋กœ ๋‹ตํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ƒ๊ฐํ•ด ๋ณด์ž. ์—ฌ๋Ÿฌ๋ถ„์˜ ์ด๋ฆ„๋„, Hello๋ผ๋Š” ๋‹จ์–ด๋„ ์ˆซ์ž๋Š” ์•„๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ด๊ฒƒ๋“ค์€ ๋ฌด์—‡์ผ๊นŒ? ์šฐ๋ฆฌ๋Š” ๋ชจ๋“  ๋‹จ์–ด์™€ ๋ฌธ์žฅ์„ ํ†ตํ‹€์–ด ํ…์ŠคํŠธ๋ผ๊ณ  ๋ถ€๋ฅผ ์ˆ˜๋„ ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋Š” String(๋ฌธ์ž์—ด)์ด๋ผ๋Š” ๋‹ค์†Œ ์ด์ƒ‰์ ์ธ ๋‚ฑ๋ง์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด๋Š” "๋ฌธ์ž๋“ค์˜ ๋‚˜์—ด"์˜ ์ค„์ž„๋ง์ด๋‹ค. ํ•˜์Šค์ผˆ์—๋Š” ๋ชจ๋“  ํƒ€์ž… ์ด๋ฆ„์ด ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ทœ์น™์ด ์žˆ๋‹ค. ์•ž์œผ๋กœ ์ด ๊ด€์Šต์„ ๊ณ ์ˆ˜ํ•  ๊ฒƒ์ด๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ํƒ€์ž…์ด๋ผ๋Š” ๊ฐœ๋…์„ ๋ช…ํ™•ํžˆ ๋ณด์—ฌ์ค€๋‹ค. ์šฐ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์‚ฌ๋žŒ๋“ค์˜ ์—ฐ๋ฝ์ฒ˜์— ๊ด€ํ•œ ์ƒ์„ธ ์ •๋ณด๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ํ…Œ์ด๋ธ”์ด ์žˆ๋‹ค๊ณ  ์น˜์ž. ์ผ์ข…์˜ ๊ฐœ์ธ ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์ธ ์…ˆ์ด๋‹ค. ๊ทธ ๋‚ด์šฉ๋ฌผ์€ ์ด๋Ÿฐ ์‹์ด๋‹ค. ์„ฑ ์ด๋ฆ„ ์ „ํ™”๋ฒˆํ˜ธ ์ฃผ์†Œ Sherlock Holmes 743756 221B Baker Street London Bob Jones 655523 99 Long Road Street Villestown ๊ฐ ํ•ญ๋ชฉ(entry)์˜ ํ•„๋“œ๋“ค์€ ๊ฐ’์„ ํฌํ•จํ•œ๋‹ค. Sherlock์€ ๊ฐ’์ด๊ณ , 99 Long Road Street Villestown๋„, 655523๋„ ๊ฐ’์ด๋‹ค. ์ด ์˜ˆ์ œ์˜ ๊ฐ’๋“ค์„ ํƒ€์ž…์˜ ๊ด€์ ์—์„œ ๋ถ„๋ฅ˜ํ•ด ๋ณด์ž. "์„ฑ"๊ณผ "์ด๋ฆ„"์€ ํ…์ŠคํŠธ๋ฅผ ํฌํ•จํ•˜๊ณ , ๋”ฐ๋ผ์„œ ์ด ๊ฐ’๋“ค์˜ ํƒ€์ž…์€ String์ด๋ผ ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ์–ธ๋œป ๋ณด๋ฉด ์ฃผ์†Œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ  ์‹ถ๊ฒ ์ง€๋งŒ, ๋‹จ์ˆœํ•ด ๋ณด์ด๋Š” ์ฃผ์†Œ์˜ ์ด๋ฉด์—๋Š” ์ƒ๋‹นํžˆ ๋ณต์žกํ•œ ์˜๋ฏธ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. ์ฃผ์†Œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด์„ํ• ์ง€์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์–‘ํ•œ ๊ด€์Šต์ด ์žˆ๋‹ค. ๊ฐ€๋ น ์ฃผ์†Œ ํ…์ŠคํŠธ๊ฐ€ ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•˜๋ฉด ๊ทธ ์ˆซ์ž๋Š” ๊ทธ ์ง‘์˜ ๋ฒˆ์ง€์ผ ์ˆ˜ ์žˆ๋‹ค. ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฉด ๋ถ„๋ช… ๊ทธ ์ง‘์˜ ์ด๋ฆ„์ผ ๊ฒƒ์ด๋‹ค. ์•„, "PO Box"๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์ œ์™ธํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ์ˆœํžˆ ์šฐํŽธํ•จ ์ฃผ์†Œ์ด๋ฉฐ ์‚ฌ๋žŒ์ด ์–ด๋”” ์‚ฌ๋Š”์ง€ ์ „ํ˜€ ์•Œ๋ ค์ฃผ์ง€ ์•Š๋Š”๋‹ค. ์ฃผ์†Œ์˜ ๊ฐ ๋ถ€๋ถ„์€ ์ €๋งˆ๋‹ค ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค. ์›์น™์ ์œผ๋กœ๋Š” ์ฃผ์†Œ๋ฅผ String ๊ฐ’์ด๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ทธ๋Ÿฌ๋ฉด ์ฃผ์†Œ์˜ ์ค‘์š”ํ•œ ํŠน์ง•๋“ค์„ ํฌ์ฐฉํ•˜์ง€ ๋ชปํ•œ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋ฌด์–ธ๊ฐ€๋ฅผ String ๊ฐ’์ด๋ผ๊ณ  ํ•˜๋ฉด, ๊ทธ๊ฒƒ์ด ๋ฌธ์ž์˜ ๋‚˜์—ด์ด๋ผ๊ณ  ๋งํ•˜๋Š” ๊ฒƒ์ผ ๋ฟ์ด๋‹ค. ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•˜๋‚˜์˜ ํŠนํ™”๋œ ํƒ€์ž…์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋ฉด ๋” ๋งŽ์€ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ ๋ฌด์–ธ๊ฐ€๊ฐ€ Address ๊ฐ’์ด๋ผ๋Š” ๊ฒƒ์„ ์•ˆ๋‹ค๋ฉด, ๊ทธ ๋ฐ์ดํ„ฐ ์กฐ๊ฐ์— ๊ด€ํ•ด ๋” ๋งŽ์€ ๊ฒƒ์„ ์ฆ‰์‹œ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋ น, ์ฃผ์†Œ์— ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌํ•˜๋Š” "๊ด€์Šต"์„ ํ™œ์šฉํ•ด ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ๋ฐฉ์นจ์„ ์ „ํ™”๋ฒˆํ˜ธ์—๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. TelephoneNumber๋ผ๋Š” ํƒ€์ž…์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ์–ด๋–ค ์ˆซ์ž์˜ ๋‚˜์—ด์„ ๋ฐ›์•˜๋Š”๋ฐ ๊ทธ๊ฒƒ์˜ ํƒ€์ž…์ด TelephoneNumber๋ผ๋ฉด, ์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ์ด ๋‹จ์ˆœํ•œ Number ๊ฐ’์ผ ๋•Œ๋ณด๋‹ค ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ฒ˜์Œ ์ž๋ฆฟ์ˆ˜๋“ค์„ ํ†ตํ•ด ์ง€์—ญ ๋ถ€ํ˜ธ๋‚˜ ๊ตญ๊ฐ€ ๋ถ€ํ˜ธ ๊ฐ™์€ ๊ฒƒ์„ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๊ทธ์ € Number๋กœ ๊ฐ„์ฃผํ•˜์ง€ ์•Š๋Š” ๋˜ ๋‹ค๋ฅธ ์ด์œ ๋Š” ์ด๊ฑธ ๊ฐ€์ง€๊ณ  ์‚ฐ์ˆ˜๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ๋ง์ด ์•ˆ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. TelephoneNumber์— 100์„ ๊ณฑํ•˜๋Š” ๊ฒƒ์˜ ์˜๋ฏธ์™€ ๊ทธ ๊ธฐ๋Œ€๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ? ๊ทธ๋Ÿฐ ๋ฒˆํ˜ธ๋กœ๋Š” ๋ˆ„๊ตฌ์—๊ฒŒ๋„ ์ „ํ™”๋ฅผ ๊ฑธ ์ˆ˜ ์—†๋‹ค. ๋˜ํ•œ ์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋Š” ๋ชจ๋‘ ์ค‘์š”ํ•˜๋‹ค. ๋ฐ˜์˜ฌ๋ฆผ์œผ๋กœ ์ž๋ฆฟ์ˆ˜ ์ผ๋ถ€๋ฅผ ๋‚ ๋ฆฌ๊ฑฐ๋‚˜ ์‹œ์ž‘ ๋ถ€๋ถ„์˜ 0์„ ์ƒ๋žตํ•˜๋Š” ๊ฒƒ์€ ์šฉ๋‚ฉํ•  ์ˆ˜ ์—†๋‹ค. ์™œ ํƒ€์ž…์ด ์œ ์šฉํ•œ๊ฐ€ ๊ฐœ์ฒด๋“ค์„ ์„œ์ˆ ํ•˜๊ณ  ๋ถ„๋ฅ˜ํ•˜๋Š” ๊ฒƒ์ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ์–ด๋–ป๊ฒŒ ๋„์›€์ด ๋˜๋Š” ๊ฑธ๊นŒ? ํƒ€์ž…์„ ์ •์˜ํ•˜๋ฉด ๊ทธ๊ฒƒ์„ ๊ฐ€์ง€๊ณ  ๋ฌด์—‡์ด ๊ฐ€๋Šฅํ•˜๊ณ  ๋ฌด์—‡์ด ๋ถˆ๊ฐ€๋Šฅํ•œ์ง€๋ฅผ ๊ธฐ์ˆ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌ๋ฉด ๊ฑฐ๋Œ€ํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ๊ด€๋ฆฌํ•˜๊ณ  ์˜ค๋ฅ˜๋ฅผ ํ”ผํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์‰ฌ์›Œ์ง„๋‹ค. ๋ฐ˜์‘ํ˜• ์ปค๋งจ๋“œ :type ํ™œ์šฉํ•˜๊ธฐ GHCi๋ฅผ ์ด์šฉํ•ด ํƒ€์ž…์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž. ๋ชจ๋“  ํ‘œํ˜„์‹์˜ ํƒ€์ž…์€ :type (์งง๊ฒŒ๋Š” :t)์ด๋ผ๋Š” ์ปค๋งจ๋“œ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์ „ ๊ณผ๋ชฉ์˜ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’๋“ค์— ์‹œํ—˜ํ•ด ๋ณด์ž. ์˜ˆ: GHCi์—์„œ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์˜ ํƒ€์ž… ํ™•์ธํ•˜๊ธฐ Prelude> :type True True :: Bool Prelude> :type False False :: Bool Prelude> :t (3 < 5) (3 < 5) :: Bool :: ๊ธฐํ˜ธ๋Š” "... ์ด๋‹ค์Œ์˜ ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค"๋ผ๊ณ  ์ฝ์œผ๋ฉด ๋˜๋ฉฐ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜(type signature)๋ฅผ ๋œปํ•œ๋‹ค. :type์€ ํ•˜์Šค์ผˆ์—์„œ ์ง„์œ„ ๊ฐ’๋“ค์˜ ํƒ€์ž…์ด Bool ์ž„์„ ๋“œ๋Ÿฌ๋‚ธ๋‹ค. ๋ถˆ๋ฆฌ์–ธ ๊ฐ’๋“ค์€ ๋‹จ์ˆœํžˆ ๊ฐ’ ๋น„๊ต๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜์ž. Bool์€ ์˜ˆ/์•„๋‹ˆ์š” ๋‹ต์˜ ์˜๋ฏธ๋ฅผ ํฌ์ฐฉํ•˜๋ฉฐ, ๋”ฐ๋ผ์„œ ๊ทธ๋Ÿฐ ์ข…๋ฅ˜์˜ ๋ชจ๋“  ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ์— ์–ด๋–ค ์ด๋ฆ„์ด ์žˆ๋Š”์ง€, ์‚ฌ์šฉ์ž๊ฐ€ on/off ์˜ต์…˜์„ ํ† ๊ธ€ ํ–ˆ๋Š”์ง€๊ฐ€ ๊ทธ๋Ÿฐ ์ •๋ณด๋‹ค. ๋ฌธ์ž์™€ ๋ฌธ์ž์—ด ๋ญ”๊ฐ€ ์ƒˆ๋กœ์šด ๊ฒƒ์— :t๋ฅผ ์จ๋ณด์ž. ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ์ž๋Š” ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์„œ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ์€ ๋‹จ์ผ ๋ฌธ์ž H์ด๋‹ค. ์˜ˆ: GHCi์—์„œ ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ์ž์— :type ๋ช…๋ น ์‚ฌ์šฉํ•˜๊ธฐ Prelude> :t 'H' 'H' :: Char ์ฆ‰ ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ์ž ๊ฐ’์€ Char("character"์˜ ์ค„์ž„๋ง) ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค. ํ•˜์ง€๋งŒ ๋”ฐ์˜ดํ‘œ๋Š” ๋‹จ๋… ๋ฌธ์ž์—๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋” ๊ธด ํ…์ŠคํŠธ, ์ฆ‰ ๋ฌธ์ž์˜ ๋‚˜์—ด์„ ์ž…๋ ฅํ•˜๋ ค๋ฉด ์Œ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ: GHCi์—์„œ ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ์ž์—ด์— :t ๋ช…๋ น ์‚ฌ์šฉํ•˜๊ธฐ Prelude> :t "Hello World" "Hello World" :: [Char] ์™œ Char๊ฐ€ ๋‹ค์‹œ ๋‚˜์˜ค๋Š” ๊ฑธ๊นŒ? ์ฐจ์ด์ ์€ ๊ฐ๊ด„ํ˜ธ(square bracket)์— ์žˆ๋‹ค. [Char]๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ž๊ฐ€ ์ค„์ค„์ด ์ด์–ด์ ธ์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ˜•์„ฑํ•˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. ํ•˜์Šค์ผˆ์€ ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋ฌธ์ž๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ํ•˜์Šค์ผˆ์—์„œ ์ค‘์š”ํ•œ ๊ฐœ๋…์ด๋ฉฐ ์กฐ๋งŒ๊ฐ„ ์ž์„ธํžˆ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’ "H"(์Œ๋”ฐ์˜ดํ‘œ์— ์ฃผ์˜)์— :type์„ ์‚ฌ์šฉํ•ด ๋ณด์ž. ๋ฌด์Šจ ์ผ์ด ๋ฒŒ์–ด์ง€๋Š”๊ฐ€? ๊ทธ ์ด์œ ๋Š”? ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’ 'Hello World'(๊ทธ๋ƒฅ ๋”ฐ์˜ดํ‘œ์— ์ฃผ์˜)์— :type์„ ์‚ฌ์šฉํ•ด ๋ณด์ž. ๋ฌด์Šจ ์ผ์ด ๋ฒŒ์–ด์ง€๋Š”๊ฐ€? ๊ทธ ์ด์œ ๋Š”? ์šฐ์—ฐํžˆ๋„ ํ•˜์Šค์ผˆ์—๋Š” ์‚ฌ๋žŒ ์–ธ์–ด์˜ ๋™์˜์–ด('big'๊ณผ 'large'์ฒ˜๋Ÿผ ๋œป์ด ๊ฐ™์€ ๋‚ฑ๋ง๋“ค)์™€ ์ฉ ๋น„์Šทํ•˜๊ฒŒ ์ž‘๋™ํ•˜๋Š”, ํƒ€์ž… ๋™์˜์–ด๋ผ๋Š” ๊ฒƒ์ด ์žˆ๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ํƒ€์ž… ๋™์˜์–ด๋Š” ํƒ€์ž…์„ ์œ„ํ•œ ๋˜ ๋‹ค๋ฅธ ์ด๋ฆ„์ด๋‹ค. ๊ฐ€๋ น String์€ [Char]์˜ ๋™์˜์–ด๋กœ ์ •์˜๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋‘˜์€ ์„œ๋กœ๋ฅผ ์ž์œ ๋กญ๊ฒŒ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ์€ ์™„๋ฒฝํžˆ ํƒ€๋‹นํ•˜๋ฉฐ "Hello World" :: String ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ํ›จ์”ฌ ๊ฐ€๋…์„ฑ์ด ์ข‹๋‹ค. ์ด์ œ๋ถ€ํ„ฐ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ํ…์ŠคํŠธ ๊ฐ’์„ [Char] ๋Œ€์‹  String์ด๋ผ ์นญํ•  ๊ฒƒ์ด๋‹ค. ํ•จ์ˆ˜ํ˜• ํƒ€์ž… ์ง€๊ธˆ๊นŒ์ง€๋Š” ๊ฐ’(๋ฌธ์ž์—ด, ๋ถˆ๋ฆฌ์–ธ, ๋ฌธ์ž ๋“ฑ)์ด ์–ด๋–ป๊ฒŒ ํƒ€์ž…์„ ๊ฐ€์ง€๊ณ  ์ด๋Ÿฐ ํƒ€์ž…์ด ์–ด๋–ป๊ฒŒ ๊ฐ’์„ ๋ถ„๋ฅ˜ํ•˜๊ณ  ์„ค๋ช…ํ•˜๋Š”์ง€๋ฅผ ์•Œ์•„๋ดค๋‹ค. ์ด์ œ ํ•˜์Šค์ผˆ์˜ ํƒ€์ž… ์ฒด๊ณ„๋ฅผ ์ง„์ •์œผ๋กœ ๊ฐ•๋ ฅํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์ปค๋‹ค๋ž€ ์ „ํ™˜์ ์„ ๋งž์ดํ•˜์ž. ํ•จ์ˆ˜ ์—ญ์‹œ ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค. 1 ๊ทธ ์˜ˆ์ œ๋ฅผ ๋ช‡ ๊ฐœ ์‚ดํŽด๋ณด๊ฒ ๋‹ค. ์˜ˆ์ œ: not not์„ ์ด์šฉํ•˜๋ฉด ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜์ „์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค(True๋Š” False๋กœ, False๋Š” True๋กœ). ์ด ํ•จ์ˆ˜์˜ ํƒ€์ž…์„ ๋ฐํ˜€๋‚ด๋ ค๋ฉด ๋‘ ๊ฐ€์ง€๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. ๋ฐ”๋กœ not์ด ์ž…๋ ฅ์œผ๋กœ ์ทจํ•˜๋Š” ๊ฐ’์˜ ํƒ€์ž…๊ณผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์˜ ํƒ€์ž…์ด๋‹ค. ์ด ์˜ˆ์ œ์—์„  ์ƒํ™ฉ์ด ๊ฐ„๋‹จํ•˜๋‹ค. not์€ (๋ฐ˜์ „์‹œํ‚ฌ) Bool ๊ฐ’์„ ์ทจํ•ด (๋ฐ˜์ „๋œ) Bool ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๊ฒƒ์„ ํ‘œ๊ธฐํ•˜๋ฉด ์˜ˆ: not์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ not :: Bool -> Bool "not์€ Bool ํƒ€์ž…์˜ ๋ฌด์–ธ๊ฐ€๋กœ๋ถ€ํ„ฐ Bool ํƒ€์ž…์˜ ๋ฌด์–ธ๊ฐ€๋กœ ๊ฐ€๋Š” ํ•จ์ˆ˜๋‹ค"๋ผ๊ณ  ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜์— :t๋ฅผ ์“ฐ๋ฉด ์˜ˆ์ƒํ•œ ๋Œ€๋กœ ๋‚˜์˜จ๋‹ค. Prelude> :t not not :: Bool -> Bool ํ•จ์ˆ˜์˜ ํƒ€์ž…์€ ๊ทธ ํ•จ์ˆ˜์˜ ์ธ์ž์˜ ํƒ€์ž…๊ณผ ๋Œ๋ ค์ฃผ๋Š” ๊ฐ’์˜ ํƒ€์ž…์œผ๋กœ ๊ธฐ์ˆ ๋œ๋‹ค. ์˜ˆ์ œ: chr์™€ ord ํ…์ŠคํŠธ๋Š” ์ปดํ“จํ„ฐ์—์„œ ๊ณจ์นซ๊ฑฐ๋ฆฌ๋‹ค. ๊ฐ€์žฅ ์ € ์ˆ˜์ค€์—์„œ ์ปดํ“จํ„ฐ๋Š” ์˜ค์ง 1๊ณผ 0 ๋ฐ–์— ๋ชจ๋ฅธ๋‹ค. ์ปดํ“จํ„ฐ๋Š” ์ด์ง„ ์ฒด๊ณ„์—์„œ ์ž‘๋™ํ•œ๋‹ค. ์ด์ง„์ˆ˜๋กœ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ์€ ์ „ํ˜€ ํŽธํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ๋žŒ๋“ค์€ ์ปดํ“จํ„ฐ๊ฐ€ ํ…์ŠคํŠธ๋ฅผ ๋ณด๊ด€ํ•  ์ˆ˜๋‹จ์„ ๋งŒ๋“ค์–ด๋ƒˆ๋‹ค. ๋ชจ๋“  ๋ฌธ์ž๋Š” ์ผ๋‹จ ์ˆซ์ž๋กœ ๋ณ€ํ™˜๋˜๊ณ , ์ด ์ˆซ์ž๋Š” ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜๋˜์–ด ์ €์žฅ๋œ๋‹ค. ์ด๊ฒƒ์ด ํ…์ŠคํŠธ ํ•œ ์กฐ๊ฐ(๋ฌธ์ž๋“ค์˜ ์—ด)์ด ์ด์ง„์ˆ˜๋กœ ๋ถ€ํ˜ธํ™”๋˜๋Š” ์ ˆ์ฐจ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋Œ€๊ฐœ ๋ฌธ์ž๋ฅผ ์ˆ˜ํ•™์  ํ‘œ์ƒ์œผ๋กœ ๋ถ€ํ˜ธํ™”ํ•˜๋Š” ๊ฒƒ์—๋งŒ ์‹ ๊ฒฝ ์“ฐ๋Š”๋ฐ, ์ปดํ“จํ„ฐ๊ฐ€ ๋ณดํ†ต์€ ์ด์ง„์ˆ˜๋กœ์˜ ๋ณ€ํ™˜์„ ์•Œ์•„์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž๋“ค์„ ์ ๊ณ  ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ธฐ๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 'a'๋Š” 1, 'b'๋Š” 2 ์ˆœ์œผ๋กœ ๋Œ€์‘์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ASCII ํ‘œ์ค€์ด ๋ฐ”๋กœ ์ด๋Ÿฐ ๊ฒƒ์ด๋‹ค. ASCII๋Š” ๋„๋ฆฌ ์“ฐ์ด๋Š” 128๊ฐœ ๋ฌธ์ž์— ์ˆœ์„œ๋Œ€๋กœ ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ธด๋‹ค. ๋ฌผ๋ก  ์—‰๋ฉ์ด ๊น”๊ณ  ์•‰์•„ ์ปค๋‹ค๋ž€ ์ฐธ์กฐํ‘œ์—์„œ ๋ถ€ํ˜ธํ™”๋ฅผ ํ•  ๋•Œ๋งˆ๋‹ค ๋ฌธ์ž๋ฅผ ์ผ์ผ์ด ์ฐพ๋Š” ๊ฒƒ์€ ๋”ฐ๋ถ„ํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ ์ผ์„ ๋Œ€์‹ ํ•ด์ฃผ๋Š” chr('char'๋ผ ๋ฐœ์Œ)์™€ ord๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. 2 ์˜ˆ: chr์™€ ord์˜ ์‹œ๊ทธ๋„ˆ์ณ chr :: Int -> Char ord :: Char -> Int Char๊ฐ€ ๋ฌด์—‡์„ ๋œปํ•˜๋Š”์ง€๋Š” ์•Œ๊ณ  ์žˆ๋‹ค. ์œ„ ์‹œ๊ทธ๋„ˆ์ณ์—์„œ Int๋Š” ์ฒ˜์Œ ๋ณด๋Š” ํƒ€์ž…์œผ๋กœ, ์ •์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ์ˆ˜ ์ค‘ ํ•˜๋‚˜๋‹ค. 3 chr์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋Š” Int ํƒ€์ž…์˜ ์ธ์ž ์ฆ‰ ์ •์ˆ˜๋ฅผ ์ทจํ•ด์„œ Char ํƒ€์ž…์˜ ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ํ‰๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ค€๋‹ค. ord๋Š” ๊ทธ ๋ฐ˜๋Œ€๋‹ค. Char ํƒ€์ž…์˜ ๊ฐ’์„ ์ทจํ•ด์„œ Int ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋กœ๋ถ€ํ„ฐ ์–ป์€ ์ •๋ณด๋กœ ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ๋ฌธ์ž๋ฅผ ์ˆซ์ž ๋ถ€ํ˜ธ๋กœ ๋ถ€ํ˜ธํ™”ํ•˜๊ณ (ord) ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ๊ทธ๊ฒƒ์„ ๋‹ค์‹œ ๋ฌธ์ž๋กœ ๋ณตํ˜ธํ™” ํ•˜๋Š”์ง€๊ฐ€(chr) ๋ถ„๋ช…ํžˆ ๋ณด์ธ๋‹ค. ์ข€ ๋” ํ™•์‹คํžˆ ํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๊ธฐ chr๊ณผ ord๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๊ฐ€ ์žˆ๋‹ค. ๋‘ ํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ž˜์„œ GHCi์—์„œ ์ด๊ฒƒ๋“ค์„ ์‹œํ—˜ํ•ด ๋ณด๊ธฐ ์ „์— :module Data.Char(๋˜๋Š” :m Data.Char)๋กœ ์ด๊ฒƒ๋“ค์ด ์ •์˜๋œ Data.Char ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•œ๋‹ค. ์˜ˆ: chr๊ณผ ord์— ๋Œ€ํ•œ ํ•จ์ˆ˜ ํ˜ธ์ถœ Prelude> :m Data.Char Prelude Data.Char> chr 97 'a' Prelude Data.Char> chr 98 'b' Prelude Data.Char> ord 'c' 99 ์ธ์ž๊ฐ€ ๋‘˜ ์ด์ƒ์ธ ํ•จ์ˆ˜ ์ง€๊ธˆ๊นŒ์ง€ ์‚ฌ์šฉํ•œ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋Š” ์ธ์ž๊ฐ€ ํ•˜๋‚˜์ธ ํ•จ์ˆ˜์—๋Š” ์ถฉ๋ถ„ํ–ˆ์ง€๋งŒ, ์ด๋Ÿฐ ํ•จ์ˆ˜์˜ ํƒ€์ž…์€ ์–ด๋–จ๊นŒ? ์˜ˆ: ์ธ์ž๊ฐ€ ํ•˜๋‚˜๋ณด๋‹ค ๋งŽ์€ ํ•จ์ˆ˜ xor p q = (p || q) && not (p && q) (xor์€ ๋ฐฐํƒ€์  or ํ•จ์ˆ˜๋กœ, ๋‘ ์ธ์ž ์ค‘ ํ•˜๋‚˜๋งŒ True์ธ ๊ฒฝ์šฐ True๋กœ ํ‰๊ฐ€๋œ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ False๋กœ ํ‰๊ฐ€๋œ๋‹ค) ์ธ์ž๊ฐ€ ์—ฌ๋Ÿฟ์ธ ํ•จ์ˆ˜์˜ ํƒ€์ž…์„ ์งœ๋Š” ์ผ๋ฐ˜์ ์ธ ์ ˆ์ฐจ๋Š” ๋ชจ๋“  ์ธ์ž์˜ ํƒ€์ž…์„ ํ•œ ์ค„์— ์ˆœ์„œ๋Œ€๋กœ ์“ฐ๊ณ (์ด ๊ฒฝ์šฐ p ๋‹ค์Œ q) ์ด๊ฒƒ๋“ค์„ ->๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ๊ทธ ์ค„์˜ ๋์— ๊ฒฐ๊ด๊ฐ’์˜ ํƒ€์ž…์„ ์ ๊ณ  ๋ฐ”๋กœ ์ „์— ์ตœ์ข… ->๋ฅผ ๋†“๋Š”๋‹ค. 4 ์ด ์˜ˆ์ œ์—์„œ๋Š” ์ธ์ž๋“ค์˜ ํƒ€์ž…์„ ์จ ๋‚ด๋ ค๊ฐ„๋‹ค. ์ด ๊ฒฝ์šฐ (||)์™€ (&&)์˜ ์‚ฌ์šฉ์—์„œ p์™€ q๊ฐ€ Bool ํƒ€์ž…์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด ์ •ํ•ด์ง„๋‹ค. Bool Bool ^^ p is a Bool ^^ q is a Bool as well ๊ทธ ์‚ฌ์ด์— ->๋ฅผ ์ฑ„์šด๋‹ค. Bool -> Bool ๊ฒฐ๊ด๊ฐ’ ํƒ€์ž…๊ณผ ๋งˆ์ง€๋ง‰ ->๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ์—ฌ๊ธฐ์„  ๊ธฐ๋ณธ์ ์ธ ๋ถˆ๋ฆฌ์–ธ ์—ฐ์‚ฐ์„ ํ•˜๋ฏ€๋กœ ๊ทธ ๊ฒฐ๊ณผ๋„ Bool์ด๋‹ค. Bool -> Bool -> Bool ^^ We're returning a Bool ^^ This is the extra -> that got added in ์ตœ์ข… ์‹œ๊ทธ๋„ˆ์ณ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์˜ˆ: xor์˜ ์‹œ๊ทธ๋„ˆ์ณ xor :: Bool -> Bool -> Bool ์‹ค์ „ ์˜ˆ์ œ: openWindow ํ•˜์Šค ์ผˆ ์‹ค์ „์—์„œ ๋ฐฐ์šฐ๊ฒ ์ง€๋งŒ, GUI(๊ทธ๋ž˜ํ”ฝ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค Graphical User Interface)๋Š” ํ•˜์Šค ์ผˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค ์ค‘ ์ธ๊ธฐ ์žˆ๋Š” ๋ถ€๋ฅ˜์˜ ํ•˜๋‚˜๋‹ค. GUI๋Š” ์ปดํ“จํ„ฐ ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ์ต์ˆ™ํ•œ ๋ฉ”๋‰ด, ๋ฒ„ํŠผ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฐฝ, ๋งˆ์šฐ์Šค ์›€์ง์ด๊ธฐ ๋“ฑ ์‹œ๊ฐ์ ์ธ ๊ฒƒ๋“ค์„ ๋‹ค๋ฃจ๋Š” ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค ์ค‘ ํ•˜๋‚˜์— openWindow๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ƒˆ ์œˆ๋„๋ฅผ ์—ด ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์—ฌ๋Ÿฌ๋ถ„์ด ๋ฌธ์„œ ํŽธ์ง‘๊ธฐ๋ฅผ ๋งŒ๋“ค๊ณ  ์žˆ๋Š”๋ฐ ์‚ฌ์šฉ์ž๊ฐ€ 'Options' ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด, ์—ฌ๋Ÿฌ๋ถ„์€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์˜ต์…˜์„ ํฌํ•จํ•˜๋Š” ์ƒˆ ์œˆ๋„๋ฅผ ์—ด์–ด์•ผ ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ๋“ค์—ฌ๋‹ค๋ณด์ž. 5 ์˜ˆ์ œ: openWindow openWindow :: WindowTitle -> WindowSize -> Window ์•„์ง ๋ชจ๋ฅด๋Š” ํƒ€์ž…๋“ค์ด์ง€๋งŒ ์•„์ฃผ ๋‹จ์ˆœํ•˜๋‹ค. WindowTitle, WindowSize, Window ์„ธ ํƒ€์ž… ๋ชจ๋‘ openWindow๋ฅผ ์ œ๊ณตํ•˜๋Š” GUI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ •์˜ํ•˜๋Š” ๊ฒƒ๋“ค์ด๋‹ค. ๋‘ ํ™”์‚ดํ‘œ๋Š” ์ฒ˜์Œ์˜ ๋‘ ํƒ€์ž…์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์˜ ํƒ€์ž…์ด๊ณ  ๋งˆ์ง€๋ง‰ ํƒ€์ž…์€ ๊ฒฐ๊ณผ์˜ ํƒ€์ž…์ž„์„ ๋œปํ•œ๋‹ค. WindowTitle์€ ์œˆ๋„์˜ ์ œ๋ชฉ(๋ณดํ†ต์€ ์œˆ๋„ ๋งจ ์œ„์˜ ํƒ€์ดํ‹€ ๋ฐ”์— ํ‘œ์‹œ๋จ)์„ ๋œปํ•˜๊ณ  WindowSize๋Š” ์œˆ๋„๊ฐ€ ์–ผ๋งˆ๋‚˜ ํฐ์ง€๋ฅผ ๊ธฐ์ˆ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ openWindow๋Š” ์‹ค์ œ ์œˆ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Window ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ๋ณธ ์ ์ด ์—†๊ฑฐ๋‚˜ ์‹ค์ œ๋กœ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ชจ๋ฅด๋”๋ผ๋„ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ๋ณด๋ฉด ๊ทธ ํ•จ์ˆ˜๊ฐ€ ๋ฌด์Šจ ์šฉ๋„์ธ์ง€ ๋Œ€๋žต ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ณผ ๋•Œ๋งˆ๋‹ค :t์œผ๋กœ ํ™•์ธํ•ด ๋ณด๋Š” ์Šต๊ด€์„ ๋“ค์—ฌ๋ณด์ž. ํ•˜์Šค์ผˆ์˜ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜๋“ค์„ ๋ฐฐ์šฐ๊ณ  ํ•˜์Šค์ผˆ์˜ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์œ ์šฉํ•œ ์ง๊ด€์„ ๊ธฐ๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋‹ค์Œ ํ•จ์ˆ˜๋“ค์˜ ํƒ€์ž…์€ ๋ฌด์—‡์ธ๊ฐ€? ์ˆซ์ž๊ฐ€ ๊ด€์—ฌํ•˜๋Š” ํ•จ์ˆ˜๋“ค์˜ ๊ฒฝ์šฐ ๊ทธ ์ˆซ์ž๊ฐ€ Int๋ผ๊ณ  ๊ฐ€์ •ํ•ด๋„ ๋œ๋‹ค. negate ํ•จ์ˆ˜๋Š” Int๋ฅผ ๋ฐ›์•„์„œ ๋ถ€ํ˜ธ๋ฅผ ๋ฐ˜์ „ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด negate 4 = -4์ด๊ณ  negate (-2) = 2์ด๋‹ค. (||) ํ•จ์ˆ˜๋Š” 'or'์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ Bool ๊ฐ’ ๋‘ ๊ฐœ๋ฅผ ์ทจํ•ด์„œ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ True์ธ ๊ฒฝ์šฐ True๋ฅผ, ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. monthLength ํ•จ์ˆ˜๋Š” ์œค๋…„์ธ ๊ฒฝ์šฐ True, ์•„๋‹ˆ๋ฉด False์ธ Bool ๊ฐ’๊ณผ ๋ช‡ ๋ฒˆ์งธ ๋‹ฌ์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Int ๊ฐ’์„ ์ทจํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋‹ฌ์˜ ์š”์ผ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Int ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. f x y = not x && y g x = (2*x - 1)^2 ์ฝ”๋“œ ์•ˆ์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜ ์ง€๊ธˆ๊นŒ์ง€ ํƒ€์ž…์˜ ์ด๋ฉด์— ์žˆ๋Š” ๊ธฐ๋ณธ์ ์ธ ์ด๋ก ๊ณผ ๊ทธ ์ด๋ก ์ด ํ•˜์Šค์ผˆ์— ์–ด๋–ป๊ฒŒ ์ ์šฉ๋˜๋Š”์ง€๋ฅผ ์•Œ์•„๋ดค๋‹ค. ์ด์ œ ์†Œ์Šค ํŒŒ์ผ ์•ˆ์—์„œ ํ•จ์ˆ˜์— ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ๋ถ™์ด๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. ๋‹ค์Œ์€ ์•ž์„œ ์˜ˆ์ œ๋กœ ๋“ค์—ˆ๋˜ xor ํ•จ์ˆ˜๋‹ค. ์˜ˆ์ œ: ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ๋ถ™์ธ ํ•จ์ˆ˜ xor :: Bool -> Bool -> Bool xor p q = (p || q) && not (p && q) ์šฐ๋ฆฌ๊ฐ€ ํ•  ์ผ์€ ์ด๊ฒŒ ์ „๋ถ€๋‹ค. ์ตœ๋Œ€ํ•œ ๋ช…๋ฃŒํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์‹œ๊ทธ๋„ˆ์ฒ˜๋Š” ๋Œ€์‘ํ•˜๋Š” ํ•จ์ˆ˜ ์ •์˜ ๋ฐ”๋กœ ์•ž์— ๋†“๋Š”๋‹ค. ์ด๋ ‡๊ฒŒ ์ถ”๊ฐ€ํ•œ ์‹œ๊ทธ๋„ˆ์ฒ˜๋Š” ๋‘ ๊ฐ€์ง€ ์—ญํ• ์„ ๋งก๋Š”๋‹ค. ๊ทธ ํ•จ์ˆ˜์˜ ํƒ€์ž…์„ ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ์‚ฌ๋žŒ์—๊ฒŒ๋„, ์ปดํŒŒ์ผ๋Ÿฌ ๋ฐ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—๋„ ๋ช…ํ™•ํžˆ ์•Œ๋ฆฐ๋‹ค. ํƒ€์ž… ์ถ”๋ก  ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๊ฐ€ ์ธํ„ฐํ”„๋ฆฌํ„ฐ(๋˜๋Š” ์ปดํŒŒ์ผ๋Ÿฌ)์—๊ฒŒ ํ•จ์ˆ˜์˜ ํƒ€์ž…์„ ์•Œ๋ ค์ค€๋‹ค๋ฉด ์ง€๊ธˆ๊นŒ์ง€๋Š” ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  ์–ด๋–ป๊ฒŒ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์™”๋˜ ๊ฑธ๊นŒ? ์šฐ๋ฆฌ๊ฐ€ ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ํ•˜์Šค์ผˆ์—๊ฒŒ ์•Œ๋ ค์ฃผ์ง€ ์•Š์œผ๋ฉด ํ•˜์Šค์ผˆ์€ ํƒ€์ž… ์ถ”๋ก ์ด๋ผ๋Š” ์ ˆ์ฐจ๋ฅผ ํ†ตํ•ด ๊ทธ ํƒ€์ž…์„ ์•Œ์•„๋‚ธ๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์•Œ๊ณ  ์žˆ๋Š” ๊ฒƒ๋“ค์˜ ํƒ€์ž…์œผ๋กœ ์‹œ์ž‘ํ•ด์„œ ๊ฐ’์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์˜ ํƒ€์ž…์„ ์•Œ์•„๋‚ธ๋‹ค. ๋‹ค์Œ์€ ํ”ํ•œ ์˜ˆ์‹œ๋‹ค. ์˜ˆ์ œ: ๊ฐ„๋‹จํ•œ ํƒ€์ž… ์ถ”๋ก  -- ์ด ํ•จ์ˆ˜์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋Š” ์˜๋„์ ์œผ๋กœ ์ƒ๋žต๋จ isL c = c == 'l' isL์€ ์ธ์ž c๋ฅผ ์ทจํ•ด c == 'l'์„ ํ‰๊ฐ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๊ฐ€ ์—†์œผ๋ฉด c์˜ ํƒ€์ž…๊ณผ ๊ฒฐ๊ด๊ฐ’์˜ ํƒ€์ž…์ด ๋ช…์‹œ๋˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ ํ‘œํ˜„์‹ c == 'l'์—์„œ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” 'l'์ด Char ๊ฐ’์ด๋ž€ ๊ฑธ ์•Œ์•„๋‚ธ๋‹ค. c์™€ 'l'์ด (==)์—์„œ ํ•ญ๋“ฑ ๋น„๊ต๋˜๊ณ  (==)์˜ ๋‘ ์ธ์ž๋Š” ํƒ€์ž…์ด ๊ฐ™์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 6, c๋Š” ๋ฐ˜๋“œ์‹œ Char ๊ฐ’์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒฐ๋ก ์ด ๋‚˜์˜จ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, isL c๋Š” (==)์˜ ๊ฒฐ๊ณผ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— Bool์ด์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด ํ•จ์ˆ˜์˜ ์‹œ๊ทธ๋„ˆ์ฒ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์˜ˆ์ œ: ํƒ€์ž…์„ ์ฒจ๋ถ€ํ•œ isL isL :: Char -> Bool isL c = c == 'l' ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ๋นผ๋จน์œผ๋ฉด ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ด๋Ÿฐ ๊ณผ์ •์„ ํ†ตํ•ด ๊ทธ ํƒ€์ž…์„ ์ฐพ๋Š”๋‹ค. isL์— ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ๋ถ™์ด๊ฑฐ๋‚˜ ๋ถ™์ด์ง€ ์•Š๊ณ ์„œ :t๋ฅผ ์จ์„œ ๊ฒ€์ฆํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๊ฐ€ ์ถ”๋ก ์ด ๋˜๋Š” ๊ฒƒ์ด๋ผ๋ฉด ์™œ ์ž‘์„ฑํ•ด์•ผ ํ• ๊นŒ? ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํƒ€์ž…์„ ์ถ”๋ก ํ•˜๊ธฐ์— ์ •๋ณด๊ฐ€ ๋ถ€์กฑํ•ด์„œ ์‹œ๊ทธ๋„ˆ์ฒ˜๊ฐ€ ํ•„์ˆ˜์ ์ด๋‹ค. ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ์ด์šฉํ•ด ํ•จ์ˆ˜๋‚˜ ๊ฐ’์˜ ์ตœ์ข… ํƒ€์ž…์„ ์ œํ•œํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ์ง€๊ธˆ์€ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•  ํ•„์š”๊ฐ€ ์—†์ง€๋งŒ, ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ๋ช…์‹œํ•  ์ด์œ ๋Š” ๊ทธ ์™ธ์—๋„ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๋ฌธ์„œํ™”: ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋Š” ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํ•จ์ˆ˜๋Š” ๊ทธ ์ด๋ฆ„๊ณผ ํƒ€์ž…๋งŒ ๋ณด๋ฉด ๋ฌด์Šจ ์ผ์„ ํ•˜๋Š”์ง€ ์ถฉ๋ถ„ํžˆ ์ถ”์ธกํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก  ์ฝ”๋“œ์— ์ ์ ˆํžˆ ์ฃผ์„์„ ๋‹ค๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜์ง€๋งŒ, ํƒ€์ž…์„ ํ™•์‹คํžˆ ํ•˜๋Š” ๊ฒƒ๋„ ๋งŽ์€ ๋„์›€์ด ๋œ๋‹ค. ๋””๋ฒ„๊น…: ํ•จ์ˆ˜์— ํƒ€์ž…์„ ๋‹ฌ์•„๋†“๊ณ  ํ•จ์ˆ˜ ๋ชธ์ฒด ์•ˆ์—์„œ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ๋ฐ”๊พธ๋ฉด, ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ปดํŒŒ์ผ ๋„์ค‘์— ์—ฌ๋Ÿฌ๋ถ„์˜ ํ•จ์ˆ˜๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Œ์„ ์•Œ๋ ค์ค„ ๊ฒƒ์ด๋‹ค. ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๊ฐ€ ์—†์œผ๋ฉด ์˜ค๋ฅ˜ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์ปดํŒŒ์ผ๋˜๊ณ , ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž˜๋ชป๋œ ํƒ€์ž…๋“ค์„ ํ• ๋‹นํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „์—๋Š” ์ด๋Ÿฐ ์‹ค์ˆ˜๋ฅผ ํ–ˆ๋‹ค๋Š” ๊ฑธ ์•Œ์•„๋‚ด์ง€ ๋ชปํ•  ๊ฒƒ์ด๋‹ค. ํƒ€์ž…๊ณผ ๊ฐ€๋…์„ฑ ๋ณด๋‹ค ํ˜„์‹ค์ ์ธ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์‹œ๊ทธ๋„ˆ์ฒ˜๊ฐ€ ๋ฌธ์„œํ™”์— ์–ด๋–ป๊ฒŒ ๋„์›€์ด ๋˜๋Š”์ง€ ์•Œ์•„๋ณด์ž. ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์ž‘์€ ๋ชจ๋“ˆ์œผ๋กœ์„œ (๋ชจ๋“ˆ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งˆ๋ จํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์ˆ˜๋‹จ์ด๋‹ค), GHC์— ํฌํ•จ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์กฐ์งํ™”ํ•œ๋‹ค. ์ž ๊น ์—ฌ๊ธฐ ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์ด ์‹ค์ œ๋กœ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๋ ค ๊ณจ๋จธ๋ฆฌ๋ฅผ ์•“์ง€ ๋ง์ž. ์šฐ๋ฆฌ๊ฐ€ ์•„์ง ๋‹ค๋ฃจ์ง€ ์•Š์€ ๊ธฐ๋Šฅ์ด ๋งŽ์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ง€๊ธˆ์€ ๊ทธ๋ƒฅ ์ฝ๊ณ  ์ฆ๊ธฐ๋ฉด ๋œ๋‹ค. ์˜ˆ์ œ: ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“ˆ module StringManip where import Data.Char uppercase, lowercase :: String -> String uppercase = map toUpper lowercase = map toLower capitalize :: String -> String capitalize x = let capWord [] = [] capWord (x:xs) = toUpper x : xs in unwords (map capWord (words x)) ์ด ์ž‘์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋ฌธ์ž์—ด ์กฐ์ž‘ ํ•จ์ˆ˜๋ฅผ 3๊ฐœ ์ œ๊ณตํ•œ๋‹ค. uppercase๋Š” ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ, lowercase๋Š” ์†Œ๋ฌธ์ž๋กœ, capitalize๋Š” ๋ชจ๋“  ๋‚ฑ๋ง์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๊ฐ ํ•จ์ˆ˜๋Š” String ๊ฐ’์„ ์ธ์ž๋กœ ์ทจํ•ด ๋‹ค๋ฅธ String์œผ๋กœ ํ‰๊ฐ€ํ•œ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด ํ•จ์ˆ˜๋“ค์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ชฐ๋ผ๋„ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ๋“ค์—ฌ๋‹ค๋ณด๊ณ  ์ธ์ž๋“ค๊ณผ ๋ฐ˜ํ™˜๊ฐ’์˜ ํƒ€์ž…์„ ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์— ์ ์ ˆํ•œ ํ•จ์ˆ˜ ์ด๋ฆ„๋„ ๊ณ๋“ค์ด๋ฉด ํ•จ์ˆ˜๋“ค์„ ์–ด๋–ป๊ฒŒ ์“ธ์ง€ ์ถฉ๋ถ„ํžˆ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ํ•จ์ˆ˜๋“ค์˜ ํƒ€์ž…์ด ๊ฐ™์œผ๋ฉด ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. ์œ„์—์„œ uppercase์™€ lowercase์ฒ˜๋Ÿผ ํ•จ์ˆ˜๋“ค์˜ ์ด๋ฆ„์„ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜๋ฉด ๋œ๋‹ค. ํƒ€์ž…์€ ์˜ค๋ฅ˜๋ฅผ<NAME>๋‹ค ํƒ€์ž… ์žˆ๋Š” ์–ธ์–ด์—์„œ ํƒ€์ž…์€ ์˜ค๋ฅ˜ ๋ฐฉ์ง€์˜ ํ•ต์‹ฌ์ด๋‹ค. ํ‘œํ˜„์‹๋“ค์„ ์—ฌ๊ธฐ์ €๊ธฐ ์ „๋‹ฌํ•  ๋•Œ๋Š” ์—ฌ๊ธฐ์„œ ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ํƒ€์ž…์ด ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค. ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผํ•  ๋•Œ ํƒ€์ž… ์˜ค๋ฅ˜๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ์€ ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•  ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฌ๋ฉด ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ฒ„๊ทธ๋ฅผ ์ค„์ด๋Š” ๋ฐ ๋„์›€์ด ๋œ๋‹ค. ์•„์ฃผ ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ํ•˜๋‚˜ ๋“ค์–ด๋ณด๋ฉด, ์˜ˆ์ œ: ํƒ€์ž… ๊ฒ€์‚ฌ๊ฐ€ ์—†๋Š” ํ”„๋กœ๊ทธ๋žจ "hello" + " world" -- type error ํ”„๋กœ๊ทธ๋žจ์— ์ด๋Ÿฐ ์ค„์ด ์žˆ์œผ๋ฉด ์ปดํŒŒ์ผ์— ์‹คํŒจํ•˜๋Š”๋ฐ, ๋‘ ๋ฌธ์ž์—ด์„ ๋”ํ•  ์ˆ˜๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ์ด์™€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ธด ๊ฒฐํ•ฉ ์—ฐ์‚ฐ์ž(concatenation operator)๋ฅผ ์˜๋„ํ–ˆ์„ ๊ฒƒ์ด๋‹ค. ์ด ์—ฐ์‚ฐ์ž๋Š” ๋‘ ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜๋กœ ํ•ฉ์นœ๋‹ค. ์˜ˆ์ œ: ์˜ค๋ฅ˜ ์ˆ˜์ •ํ•œ ํ”„๋กœ๊ทธ๋žจ "hello" ++ " world" -- "hello world" ์ด๋Ÿฐ ์˜คํƒ€๋Š” ์‰ฝ๊ฒŒ ๋‚˜์˜ค๋ฉฐ, ํ•˜์Šค์ผˆ์€ ์ปดํŒŒ์ผํ•  ๋•Œ ์ด๋Ÿฐ ์˜ค๋ฅ˜๋ฅผ ์žก์•„๋‚ธ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๊ณ  ๋ฒ„๊ทธ๊ฐ€ ๋“ฑ์žฅํ•  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆด ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒƒ์ด๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•˜๋‹ค ๋ณด๋ฉด ํƒ€์ž…๋“ค๋„ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฐ ๋ณ€๊ฒฝ์ด ์˜๋„๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฐ๊ณผ๋ผ๋ฉด ์ปดํŒŒ์ผํ•  ๋•Œ ๋ฐ”๋กœ ๋“œ๋Ÿฌ๋‚˜๊ฒŒ ๋œ๋‹ค. ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์€ ํƒ€์ž… ์˜ค๋ฅ˜๋ฅผ ๋ชจ๋‘ ๊ณ ์น˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ปดํŒŒ์ผํ•˜๋ฉด ๋Œ€์ฒด๋กœ "๊ทธ๋ƒฅ ์ž˜ ๋Œ์•„๊ฐ„๋‹ค"๋ผ๊ณ  ๋งํ•˜๊ณ ๋Š” ํ•œ๋‹ค. ๊ทธ ๋™์ž‘์€ ์˜๋„์™€ ์ผ์น˜ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ์ด ํ„ฐ์ง€์ง€๋Š” ์•Š๋Š”๋‹ค. ํ•˜์Šค์ผˆ์€ ๋‹ค๋ฅธ ์–ธ์–ด๋ณด๋‹ค ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜(ํ”„๋กœ๊ทธ๋žจ์ด ์ปดํŒŒ์ผํ•  ๋•Œ๊ฐ€ ์•„๋‹ˆ๋ผ ์‹คํ–‰ํ•˜๊ณ  ์žˆ์„ ๋•Œ ์ž˜๋ชป๋˜๋Š” ์ƒํ™ฉ)๊ฐ€ ํ›จ์”ฌ ํฌ๊ท€ํ•˜๋‹ค. ๋…ธํŠธ ๋” ์‹ฌ์˜คํ•œ ์‚ฌ์‹ค์€ ํ•จ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ๋“ค์ฒ˜๋Ÿผ ๊ฐ’์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. โ†ฉ chr๊ณผ ord๊ฐ€ ํ•˜๋Š” ์ผ์€ ์ •ํ™•ํžˆ๋Š” ์ด๊ฒŒ ์•„๋‹ˆ์ง€๋งŒ ์šฐ๋ฆฌ์˜ ์˜๋„์—๋Š” ์ž˜ ๋“ค์–ด๋งž๋Š” ์„ค๋ช…์ด๋ฏ€๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค. โ†ฉ ์‚ฌ์‹ค Int๋Š” ์ •์ˆ˜๋ฅผ ์œ„ํ•œ ์œ ์ผํ•œ ํƒ€์ž…์ด ์•„๋‹ˆ๋‹ค! Int์˜ ์นœ์ฒ™๋“ค์€ ๊ณง ๋งŒ๋‚  ๊ฒƒ์ด๋‹ค. โ†ฉ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋ก ์ด ์ง€๊ธˆ์€ ์‹œ์‹œํ•ด ๋ณด์ด์ง€๋งŒ ์‚ฌ์‹ค ์—ฌ๊ธฐ์—๋Š” ์•„์ฃผ ์‹ฌ์˜คํ•œ ์ด์œ ๊ฐ€ ์žˆ๋‹ค. ์ด์— ๊ด€ํ•ด์„œ๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜ ์žฅ์—์„œ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. โ†ฉ ์˜๋„์— ๋งž์ถฐ ๊ฐ„์†Œํ™”ํ•œ ๊ฒƒ์ด์ง€๋งŒ ๊ทธ ๋ณธ์งˆ์€ ๊ฐ™๋‹ค. โ†ฉ ์ง„์œ„ ๊ฐ’ ์žฅ์—์„œ ๋…ผ์˜ํ–ˆ๋“ฏ์ด (==)์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๊ฐ€ ์ด๋ฅผ ์ง„์ˆ ํ•œ๋‹ค. ๊ถ๊ธˆํ•˜๋ฉด ์ง์ ‘ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์—ฌ๊ธฐ ์“ฐ์ธ ํ‘œ๊ธฐ์— ๋Œ€ํ•œ ์™„๋ฒฝํ•œ ์„ค๋ช…์€ ์กฐ๊ธˆ ๊ธฐ๋‹ค๋ ค์•ผ ํ•œ๋‹ค. โ†ฉ 5 ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Lists_and_tuples ๋ฆฌ์ŠคํŠธ ๋ฆฌ์ŠคํŠธ ๊ตฌ์ถ•ํ•˜๊ธฐ ๋ฌธ์ž์—ด์€ ๋ฆฌ์ŠคํŠธ์ผ ๋ฟ์ด๋‹ค ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๋ฆฌ์ŠคํŠธ ํŠœํ”Œ ๋‹ค์ˆ˜์— ๊ด€ํ•œ ๋‹ค๋ฅธ ํ‘œ๊ธฐ๋ฒ• ํŠœํ”Œ ๋‚ด์˜ ํŠœํ”Œ (๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ์กฐํ•ฉ๋“ค) ๊ฐ’ ํš๋“ํ•˜๊ธฐ ๋ณด๋ฅ˜๋œ ์งˆ๋ฌธ๋“ค ๋‹คํ˜•์„ฑ ํƒ€์ž… ์˜ˆ์ œ: fst์™€ snd ์š”์•ฝ ํ•˜์Šค์ผˆ์—๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๊ทผ๋ณธ์ ์ธ ๊ตฌ์กฐ์ฒด๊ฐ€ ๋‘ ๊ฐœ ์žˆ๋Š”๋ฐ ๋ฐ”๋กœ ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์ด๋‹ค. ๋‘˜ ๋‹ค ์—ฌ๋Ÿฌ ๊ฐ’์„ ํ•˜๋‚˜์˜ ํ•ฉ์„ฑ ๊ฐ’์œผ๋กœ ๋ฌถ์Œ์œผ๋กœ์จ ์ž‘๋™ํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ GHCi์—์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ช‡ ๊ฐœ ๋งŒ๋“ค์–ด๋ณด์ž. Prelude> let numbers = [1,2,3,4] Prelude> let truths = [True, False, False] Prelude> let strings = ["here", "are", "some", "strings"] ๊ฐ๊ด„ํ˜ธ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•˜๊ณ , ๊ฐœ๊ฐœ์˜ ์›์†Œ๋“ค์€ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ œ์•ฝ์€ ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๋ชจ๋“  ์›์†Œ๋Š” ํƒ€์ž…์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ํƒ€์ž…์ด ํ˜ผ์žฌ๋œ ์›์†Œ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •์˜ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์ „ํ˜•์ ์ธ ํƒ€์ž… ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. Prelude> let mixed = [True, "bonjour"] <interactive>:1:19: Couldn't match `Bool' against `[Char]' Expected type: Bool Inferred type: [Char] In the list element: "bonjour" In the definition of `mixed': mixed = [True, "bonjour"] ๋ฆฌ์ŠคํŠธ ๊ตฌ์ถ•ํ•˜๊ธฐ ๊ฐ๊ด„ํ˜ธ์™€ ์‰ผํ‘œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ ์ „์ฒด๋ฅผ ํ•œ ๋ฒˆ์— ๊ธฐ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ• ์™ธ์— "cons"๋ผ๊ณ  ๋ถ€๋ฅด๋Š” (:) ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•œ ์กฐ๊ฐ์”ฉ ์Œ“์•„ ์˜ฌ๋ฆด ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๊ณต์ •์„ ์ปจ์‹ฑ(consing)์ด๋ผ ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค. ์ด ์šฉ์–ด๋Š” "to cons"("constructor"์˜ mnemonic)๋ผ๋Š” ๋™์‚ฌ๋ฅผ ๋ฐœ๋ช…ํ•œ LISP ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์—๊ฒŒ์„œ ์œ ๋ž˜ํ•œ ๊ฒƒ์œผ๋กœ์„œ ์›์†Œ๋ฅผ ๋ฆฌ์ŠคํŠธ ์•ž์— ๋ถ™์ด๋Š” ํŠน์ • ์ž‘์—…์„ ์ง€์นญํ•œ๋‹ค. ์˜ˆ: ๋ฆฌ์ŠคํŠธ์— ๋ฌด์–ธ๊ฐ€๋ฅผ ์ปจ์‹ฑํ•˜๊ธฐ Prelude> let numbers = [1,2,3,4] Prelude> numbers [1,2,3,4] Prelude> 0:numbers [0,1,2,3,4] ๋ฌด์–ธ๊ฐ€๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ปจ์‹ฑ(something:someList)ํ•˜๋ฉด ์šฐ๋ฆฌ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ ค๋ฐ›๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์›ํ•˜๋Š” ๋งŒํผ ์ปจ์‹ฑ์„ ๊ณ„์†ํ•  ์ˆ˜ ์žˆ๋‹ค. cons ์—ฐ์‚ฐ์ž๋Š” ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ํ‰๊ฐ€๋œ๋‹ค. cons๋ฅผ ์ƒ๊ฐํ•˜๋Š” ๋ณด๋‹ค ์ผ๋ฐ˜์ ์ธ ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ์™ผ์ชฝ์—, ์ „์ฒด ํ‘œํ˜„์‹์„ ์˜ค๋ฅธ์ชฝ์— ์ทจํ•œ๋‹ค๊ณ  ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ: ๋ฆฌ์ŠคํŠธ์— ๋งŽ์€ ๊ฒƒ์„ ์ปจ์‹ฑํ•˜๊ธฐ Prelude> 1:0:numbers [1,0,1,2,3,4] Prelude> 2:1:0:numbers [2,1,0,1,2,3,4] Prelude> 5:4:3:2:1:0:numbers [5,4,3,2,1,0,1,2,3,4] ์‚ฌ์‹ค ํ•˜์Šค์ผˆ์€ ๋ชจ๋“  ์›์†Œ๋ฅผ ๋นˆ ๋ฆฌ์ŠคํŠธ []์— ์ปจ์‹ฑํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ชจ๋“  ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. ์‰ผํ‘œ์™€ ๊ด„ํ˜ธ ํ‘œ๊ธฐ๋Š” ํŽธ์˜ ๋ฌธ๋ฒ•์ผ ๋ฟ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ [1,2,3,4,5]์™€ 1:2:3:4:5:[]๋Š” ์™„์ „ํžˆ ๋™์น˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ์—๋Š” ์ž ์žฌ์ ์ธ ํ•จ์ •์ด ์žˆ์–ด์„œ ์กฐ์‹ฌํ•ด์•ผ ํ•œ๋‹ค. True:False:[]์™€ ๊ฐ™์€ ๊ฒƒ์€ ๋”ํ•  ๋‚˜์œ„ ์—†์ด ํ›Œ๋ฅญํ•œ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ์ง€๋งŒ True:False๋Š” ์•„๋‹ˆ๋‹ค. ์˜ˆ: ์œผ์•…! Prelude> True:False <interactive>:1:5: Couldn't match `[Bool]' against `Bool' Expected type: [Bool] Inferred type: Bool In the second argument of `(:)', namely `False' In the definition of `it': it = True : False True:False๋Š” ์ต์ˆ™ํ•œ ํƒ€์ž… ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐฐ์ถœํ•œ๋‹ค. cons ์—ฐ์‚ฐ์ž (:)๋Š” (์ง„์งœ๋กœ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜์ผ ๋ฟ์ด๋‹ค) ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ธฐ๋Œ€ํ–ˆ๋Š”๋ฐ ์šฐ๋ฆฌ๋Š” ๋ฆฌ์ŠคํŠธ์— ๋ฌด์–ธ๊ฐ€๋ฅผ ๋งค๋‹ฌ ์ค„๋งŒ ์•„๋Š” ๊ฒƒ์—๊ฒŒ Bool์„ ์ค˜๋ฒ„๋ ธ๋‹ค. 1 ๊ทธ๋Ÿฌ๋‹ˆ cons๋ฅผ ์“ธ ๋•Œ๋Š” ๋‹ค์Œ์„ ๊ธฐ์–ตํ•˜์ž. ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋“ค์€ ํƒ€์ž…์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ๋ฌด์–ธ๊ฐ€๋ฅผ ๋ฆฌ์ŠคํŠธ์— cons ํ•  ์ˆ˜๋งŒ ์žˆ๋‹ค. ๋‹ค๋ฅธ ์‹์œผ๋กœ๋Š” ์•ˆ ๋œ๋‹ค(๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฌด์–ธ๊ฐ€์— cons ํ•  ์ˆ˜ ์—†์Œ). ๋”ฐ๋ผ์„œ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ํ•ญ๋ชฉ์€ ๋ฆฌ์ŠคํŠธ์—ฌ์•ผ ํ•˜๋ฉฐ ์™ผ์ชฝ์˜ ํ•ญ๋ชฉ๋“ค์€ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ ๋…๋ฆฝ ์›์†Œ์—ฌ์•ผ ํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ 3:[True, False]๋Š” ์ž‘๋™ํ• ๊นŒ? ๋˜๋Š” ์ด์œ , ํ˜น์€ ์•ˆ ๋˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ? ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด (์‹œ์ž‘ ๋ถ€๋ถ„์—) 8์„ cons ํ•˜๋Š” ํ•จ์ˆ˜ cons8์„ ์ž‘์„ฑํ•ด ๋ณด์ž. ๋‹ค์Œ์˜ ๋ฆฌ์ŠคํŠธ๋“ค์„ ๊ฐ€์ง€๊ณ  ๊ฒ€์ •ํ•ด ๋ณด์ž. cons8 [] cons8 [1,2,3] cons8 [True, False] let foo = cons8 [1,2,3] cons8 foo ์œ„ ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ 8์ด ๋ฆฌ์ŠคํŠธ์˜ ๋์— ์˜ค๋„๋ก ํ•ด๋ณด์ž. (ํžŒํŠธ: ์ด์ „ ์žฅ์˜ ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž ++๋ฅผ ๋– ์˜ฌ๋ ค๋ณด์ž) ์ธ์ž ๋‘ ๊ฐœ, ๋ฆฌ์ŠคํŠธ์™€ ์–ด๋–ค ๊ฒƒ์„ ์ทจํ•ด์„œ ๊ทธ ์–ด๋–ค ๊ฒƒ์„ ๋ฆฌ์ŠคํŠธ์— cons ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. ์ด๋ ‡๊ฒŒ ์‹œ์ž‘ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. let myCons list thing = ๋ฌธ์ž์—ด์€ ๋ฆฌ์ŠคํŠธ์ผ ๋ฟ์ด๋‹ค ํƒ€์ž…์˜ ๊ธฐ์ดˆ ๊ณผ๋ชฉ์—์„œ ์งง๊ฒŒ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ํ•˜์Šค์ผˆ์˜ ๋ฌธ์ž์—ด์€ ๋ฌธ์ž๋“ค์˜ ๋ฆฌ์ŠคํŠธ์ผ ๋ฟ์ด๋‹ค. ์ด๋Š” String ํƒ€์ž…์˜ ๊ฐ’์„ ์—ฌํƒ€ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ๊ฐ€๋ น ๋ฌธ์ž์—ด์„ ์Œ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ผ ๋ฌธ์ž๋“ค์˜ ์ˆœ์—ด๋กœ ์ญ‰ ์ž…๋ ฅํ•˜๋Š” ๋Œ€์‹ , Char ๊ฐ’๋“ค์„ (:)๋กœ ์—ฐ๊ฒฐํ•˜๊ณ  ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ๋งˆ๋ฌด๋ฆฌ ์ง“๊ฑฐ๋‚˜ ์‰ผํ‘œ-๊ฐ๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋‹ค. Prelude>"hey" == ['h','e','y'] True Prelude>"hey" == 'h':'e':'y':[] True ์Œ๋”ฐ์˜ดํ‘œ ๋ฌธ์ž์—ด์€ ์ข€ ๋” ํŽธ๋ฆฌํ•œ ๊ตฌ๋ฌธ์ผ ๋ฟ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๋ฆฌ์ŠคํŠธ ๋ฆฌ์ŠคํŠธ๋Š” ๋ฌด์—‡์ด๋“  ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๊ฒƒ๋“ค์ด ๋ชจ๋‘ ๊ฐ™์€ ํƒ€์ž…์ด๊ธฐ๋งŒ ํ•œ๋‹ค๋ฉด. ๋ฆฌ์ŠคํŠธ๋„ ๊ทธ๋Ÿฐ ๊ฒƒ ์ค‘ ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ๋Š” ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค! ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์ด๊ฒƒ์„ ์‹œ๋„ํ•ด ๋ณด์ž. ์˜ˆ: ๋ฆฌ์ŠคํŠธ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค Prelude> let listOfLists = [[1,2],[3,4],[5,6]] Prelude> listOfLists [[1,2],[3,4],[5,6]] ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ€๋” ์ƒ๋‹นํžˆ ํ˜ผ๋ž€์Šค๋Ÿฌ์šด๋ฐ, ์–ด๋–ค ๊ฒƒ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋Š” ๊ทธ ์–ด๋–ค ๊ฒƒ๋“ค ์ž์ฒด์™€๋Š” ๊ฐ™์€ ํƒ€์ž…์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ฐ€๋ น Int ํƒ€์ž…์€ [Int] ํƒ€์ž…๊ณผ ๋‹ค๋ฅด๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ๊ฐ€์ง€๊ณ  ์ด๊ฒƒ๋“ค์ด ๋‚ดํฌํ•˜๋Š” ์˜๋ฏธ๋ฅผ ์ •๋ฆฌํ•ด ๋ณด์ž. ์—ฐ์Šต๋ฌธ์ œ ์ด๊ฒƒ๋“ค ์ค‘ ์–ด๋–ค ๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅธ ํ•˜์Šค์ผˆ์ด๊ณ  ์–ด๋–ค ๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์„๊นŒ? cons ํ‘œ๊ธฐ๋กœ ์žฌ์ž‘์„ฑํ•ด ๋ณด์ž. [1,2,3, []] [1, [2,3],4] [[1,2,3],[]] ์ด๊ฒƒ๋“ค ์ค‘ ์–ด๋–ค ๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅธ ํ•˜์Šค์ผˆ์ด๊ณ  ์–ด๋–ค ๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์„๊นŒ? ์‰ผํ‘œ์™€ ๊ด„ํ˜ธ ํ‘œ๊ธฐ๋กœ ์žฌ์ž‘์„ฑํ•ด ๋ณด์ž. []:[[1,2,3],[4,5,6]] []:[] []:[]:[] [1]:[]:[] ["hi"]:[1]:[] ํ•˜์Šค์ผˆ์—์„œ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ• ๊นŒ? ๊ทธ ์ด์œ ๋Š”? ๋‹ค์Œ ๋ฆฌ์ŠคํŠธ๋Š” ํ•˜์Šค์ผˆ์—์„œ ์™œ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์„๊นŒ? [[1,2],3, [4,5]] ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋Š” ๋ณต์žกํ•˜๊ณ  ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ(2์ฐจ์› ํ–‰๋ ฌ์ด๋ผ๋˜๊ฐ€)๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์šฉํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค. ๋˜ํ•œ ํ•˜์Šค์ผˆ์˜ ํƒ€์ž… ์ฒด๊ณ„๊ฐ€ ๋น›์„ ๋ฐœํ•˜๋Š” ์žฅ์†Œ์ด๊ธฐ๋„ ํ•˜๋‹ค. ์‚ฌ๋žŒ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์€ (์ด ์œ„ํ‚ค ์ฑ…์˜ ๊ณต๋™ ์ €์ž๋“ค ํฌํ•จ) ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋งˆ๋‹ค ํ•ญ์ƒ ํ˜ผ๋ž€์— ๋น ์ง€๊ณค ํ•˜๋ฉฐ, ํƒ€์ž…์— ์ œ์•ฝ์„ ๊ฑธ๋ฉด ์ž ์žฌ์ ์ธ ๋ฌธ์ œ๋ฅผ ํ—ค์น˜๊ณ  ๋‚˜์•„๊ฐ€๋Š” ๋ฐ ๋„์›€์ด ๋œ๋‹ค. ํŠœํ”Œ ๋‹ค์ˆ˜์— ๊ด€ํ•œ ๋‹ค๋ฅธ ํ‘œ๊ธฐ๋ฒ• ํŠœํ”Œ์€ ์—ฌ๋Ÿฌ ๊ฐ’์„ ํ•œ ๊ฐ’์— ๋‹ด๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด๋‹ค. ํŠœํ”Œ๊ณผ ๋ฆฌ์ŠคํŠธ์—๋Š” ๋‘ ๊ฐ€์ง€ ์ค‘๋Œ€ํ•œ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. ํŠœํ”Œ์€ ๊ณ ์ •๋œ ๊ฐœ์ˆ˜์˜ ์›์†Œ๋“ค์„ ๊ฐ€์ง„๋‹ค(๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ immutable). ํŠœํ”Œ์— cons ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๋ช‡ ๊ฐœ์˜ ๊ฐ’์ด ์ €์žฅ๋ ์ง€ ๋ฏธ๋ฆฌ ์•„๋Š” ๊ฒฝ์šฐ์—๋Š” ํŠœํ”Œ์„ ์“ฐ๋Š” ๊ฒƒ์ด ํƒ€๋‹นํ•˜๋‹ค. ๊ฐ€๋ น ํ•œ ์ ์˜ 2D ์ขŒํ‘œ๋ฅผ ๋ณด๊ด€ํ•˜๊ธฐ ์œ„ํ•œ ํƒ€์ž…์„ ์›ํ•œ๋‹ค๋ฉด ์ ๋งˆ๋‹ค ๊ฐ’์ด ๋ช‡ ๊ฐœ ํ•„์š”ํ•œ์ง€ ์•Œ๊ณ  ์žˆ์œผ๋ฏ€๋กœ(2๊ฐœ. x ์ขŒํ‘œ์™€ y์ขŒํ‘œ) ํŠœํ”Œ์ด ํ•ฉ๋ฆฌ์ ์ด๋‹ค. ํŠœํ”Œ์˜ ์›์†Œ๋“ค์€ ๊ฐ™์€ ํƒ€์ž…์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค. ๊ฐ€๋ น ์ „ํ™”๋ฒˆํ˜ธ๋ถ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์„ธ ๊ฐœ์˜ ๊ฐ’ ์ด๋ฆ„, ์ „ํ™”๋ฒˆํ˜ธ, ์ฃผ์†Œ๋ฅผ ์‚ฌ๋žŒ๋ณ„๋กœ ๋ฌถ์–ด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ๋Š” ์„ธ ๊ฐ’์˜ ํƒ€์ž…์ด ๊ฐ™์ง€ ์•Š์•„ ๋ฆฌ์ŠคํŠธ๋Š” ๋ณ„ ๋„์›€์ด ์•ˆ ๋  ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ํŠœํ”Œ์€ ๋„์›€์ด ๋œ๋‹ค. ํŠœํ”Œ์€ ๊ด„ํ˜ธ ์•ˆ์— ์›์†Œ๋“ค์„ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ƒ์„ฑํ•œ๋‹ค. ํŠœํ”Œ์˜ ๊ฒฌ๋ณธ์„ ๋ช‡ ๊ฐœ ๋ณด์ž. ์˜ˆ: ํŠœํ”Œ ๊ฒฌ๋ณธ๋“ค (True, 1) ("Hello world", False) (4, 5, "Six", True, 'b') ์ฒซ ๋ฒˆ์งธ ํŠœํ”Œ์€ ๋‘ ์›์†Œ๋ฅผ ๋‹ด๋Š”๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” True๊ณ  ๋‘ ๋ฒˆ์งธ๋Š” 1์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ํŠœํ”Œ๋„ ๋‘ ์›์†Œ๋ฅผ ๊ฐ€์ง„๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” "Hello world"๊ณ  ๋‘ ๋ฒˆ์งธ๋Š” False๋‹ค. ์„ธ ๋ฒˆ์งธ ํŠœํ”Œ์€ ๋‹ค์„ฏ ๊ฐœ์˜ ์›์†Œ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” 4(์ˆซ์ž), ๋‘ ๋ฒˆ์งธ๋Š” 5(๋‹ค์‹œ ์ˆซ์ž), ์„ธ ๋ฒˆ์งธ๋Š” "Six"(๋ฌธ์ž์—ด), ๋„ค ๋ฒˆ์งธ๋Š” True(๋ถˆ๋ฆฌ์–ธ ๊ฐ’), ๋‹ค์„ฏ ๋ฒˆ์งธ๋Š” 'b'(๋ฌธ์ž)๋‹ค. ๋ช…๋ช…๋ฒ•์„ ๋น ๋ฅด๊ฒŒ ์†Œ๊ฐœํ•˜์ž๋ฉด, ํฌ๊ธฐ n์ธ ํŠœํ”Œ์„ n-ํŠœํ”Œ์ด๋ผ ํ‘œ๊ธฐํ•œ๋‹ค. 2-ํŠœํ”Œ(์›์†Œ๊ฐ€ 2๊ฐœ์ธ ํŠœํ”Œ)์€ ์ง(pair), 3-ํŠœํ”Œ์€ ์„ธ ์ง(triple)์ด๋ผ ํ•œ๋‹ค. ๊ทธ๋ณด๋‹ค ํฌ๊ธฐ๊ฐ€ ํฐ ํŠœํ”Œ๋“ค์€ ์‚ฌ์‹ค ํ”ํ•˜์ง€ ์•Š์ง€๋งŒ ๊ตณ์ด ๋ช…๋ช… ์ฒด๊ณ„๋ฅผ ํ™•์žฅํ•˜์ž๋ฉด ๋„ค ์ง(quadruple), ๋‹ค์„ฏ ์ง(quintuple) ๋“ฑ์ด ๋˜๊ฒ ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋Š” 4, ๋‘ ๋ฒˆ์งธ ์›์†Œ๋Š” "hello", ์„ธ ๋ฒˆ์งธ ์›์†Œ๋Š” True์ธ 3-ํŠœํ”Œ์„ ์ž‘์„ฑํ•ด ๋ณด์ž. ๋‹ค์Œ ์ค‘ ์˜ฌ๋ฐ”๋ฅธ ํŠœํ”Œ๋“ค์€ ๋ฌด์—‡์ผ๊นŒ? (4, 4) (4, "hello") (True, "Blah", "foo") ๋ฆฌ์ŠคํŠธ๋Š” ์ƒˆ๋กœ์šด ์›์†Œ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ปจ์‹ฑํ•˜์—ฌ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ˆซ์ž์˜ ๋ฆฌ์ŠคํŠธ์— ์ˆซ์ž๋ฅผ cons ํ•˜๋ฉด ์ˆซ์ž์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ ค๋ฐ›๋Š”๋‹ค. ํŠœํ”Œ์„ ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์—†๋‹ค๋Š” ๊ฒƒ์€ ๋ช…๋ฐฑํ•˜๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ? ๋…ผ์˜๋ฅผ ์œ„ํ•ด ๊ทธ๋Ÿฐ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ๋ฌด์–ธ๊ฐ€๋ฅผ ํŠœํ”Œ์— "์ปจ์‹ฑ"ํ•˜๋ฉด ๋ฌด์—‡์„ ์–ป๊ฒŒ ๋ ๊นŒ? ํŠœํ”Œ์€ ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜๋ณด๋‹ค ๋งŽ์€ ๊ฐ’์„ ๋ฐ˜ํ™˜๋ฐ›๊ณ  ์‹ถ์„ ๋•Œ๋„ ํŽธ๋ฆฌํ•˜๋‹ค. ๋งŽ์€ ์–ธ์–ด์—์„œ๋Š” ๋‘˜ ์ด์ƒ์„ ํ•œ๊บผ๋ฒˆ์— ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด ๊ทธ ํ•จ์ˆ˜์—์„œ๋งŒ ์“ฐ๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ๋กœ ๊ฐ์‹ธ์•ผ ํ•œ๋‹ค. ํ•˜์Šค์ผˆ์—์„  ์•„์ฃผ ํŽธ๋ฆฌํ•œ ๋Œ€์•ˆ์œผ๋กœ ๊ทธ ๊ฐ’๋“ค์„ ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํŠœํ”Œ ๋‚ด์˜ ํŠœํ”Œ (๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ์กฐํ•ฉ๋“ค) ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์ถ”๋ก ์„ ํŠœํ”Œ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠœํ”Œ๋„ ํ˜•์ฒด๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ํŠœํ”Œ ๋‚ด์— ํŠœํ”Œ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค(์–ผ๋งˆ๋“ ์ง€ ๋ณต์žกํ•˜๊ฒŒ ์ค‘์ฒฉํ•  ์ˆ˜ ์žˆ์Œ). ๋น„์Šทํ•˜๊ฒŒ ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ, ๋ฆฌ์ŠคํŠธ์˜ ํŠœํ”Œ, ๊ฐ™์€ ์„ ์ƒ์˜ ์˜จ๊ฐ– ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ˆ: ํŠœํ”Œ๊ณผ ๋ฆฌ์ŠคํŠธ ์ค‘์ฒฉ์‹œํ‚ค๊ธฐ ((2,3), True) ((2,3), [2,3]) [(1,2), (3,4), (5,6)] ํŠœํ”Œ์˜ ํƒ€์ž…์€ ๊ทธ ํฌ๊ธฐ๋ฟ ์•„๋‹ˆ๋ผ, ๋ฆฌ์ŠคํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ทธ ํŠœํ”Œ์ด ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋“ค์˜ ํƒ€์ž…๋„ ๊ณ ๋ คํ•ด์„œ ์ •์˜๋œ๋‹ค. ๊ฐ€๋ น ํŠœํ”Œ ("Hello",32)์™€ (47, "world")๋Š” ๊ทธ ๊ทผ๊ฐ„์ด ๋‹ค๋ฅด๋‹ค. ํ•˜๋‚˜๋Š” (String, Int) ํƒ€์ž…์ด๊ณ  ๋‹ค๋ฅธ ๊ฑด (Int, String) ํƒ€์ž…์ด๋‹ค. ์—ฌ๊ธฐ์—๋Š” ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ ๊ตฌ์ถ•์— ๊ด€ํ•ด ํ•จ์˜ํ•˜๋Š” ๋ฐ”๊ฐ€ ์žˆ๋‹ค. [("a",1),("b",9),("c",9)] ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ๋Š” ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ ํ•˜์Šค์ผˆ์€ [("a",1),(2, "b"),(9, "c")] ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋‹ค์Œ ์ค‘ ์˜ฌ๋ฐ”๋ฅธ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋“ค์€ ๋ฌด์—‡์ผ๊นŒ? ๊ทธ ์ด์œ ๋Š”? 1:(2,3) (2,4):(2,3) (2,4):[] [(2,4),(5,5),('a','b')] ([2,4],[2,2]) ๊ฐ’ ํš๋“ํ•˜๊ธฐ ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์ด ์“ธ๋ชจ๊ฐ€ ์žˆ์œผ๋ ค๋ฉด ๊ทธ ์•ˆ์— ํฌํ•จ๋œ ๊ฐ’๋“ค์— ์ ‘๊ทผํ•  ์ˆ˜๋‹จ์ด ํ•„์š”ํ•˜๋‹ค. ์ ์˜ 2D ์ขŒํ‘œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ง(2-ํŠœํ”Œ)๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ž. ์ฒด์ŠคํŒ์˜ ํŠน์ • ์‚ฌ๊ฐํ˜•์„ ํ‘œํ˜„ํ•˜๋ ค ํ•œ๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ณด์ž. ๋ชจ๋“  rank(๊ฐ€๋กœ์ค„)์— 1์—์„œ 8๊นŒ์ง€ ์ด๋ฆ„ํ‘œ๋ฅผ ๋ถ™์ด๊ณ  file(์„ธ๋กœ์ค„)์—๋„ ๋น„์Šทํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ง (2, 5)๋Š” rank 2์™€ file 5์— ์žˆ๋Š” ์‚ฌ๊ฐํ˜•์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. rank๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๊ทธ rank์˜ ๋ชจ๋“  ์กฐ๊ฐ์„ ์ฐพ๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค๊ณ  ํ•  ๋•Œ, ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ๋ชจ๋“  ์กฐ๊ฐ์˜ ์ขŒํ‘œ๋ฅผ ํ›‘์–ด๋ณด๊ณ  rank ๋ถ€๋ถ„์ด ์šฐ๋ฆฌ์—๊ฒŒ ์š”๊ตฌ๋œ ๊ทธ ํ–‰์ธ์ง€ ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ํ•œ ์กฐ๊ฐ์˜ (x, y) ์ขŒํ‘œ์ง์„ ์–ป์€ ํ›„์—๋Š” ์ด ํ•จ์ˆ˜๋Š” x(rank ์ขŒํ‘œ)๋ฅผ ์ถ”์ถœํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๋ชฉ์ ์œผ๋กœ fst, snd ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ ์ง์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ํš๋“ํ•œ๋‹ค. 2 ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž. ์˜ˆ: fst์™€ snd ์‚ฌ์šฉํ•˜๊ธฐ Prelude> fst (2, 5) Prelude> fst (True, "boo") True Prelude> snd (5, "Hello") "Hello" ์ด ํ•จ์ˆ˜๋“ค์€ ์ •์˜์— ๋”ฐ๋ผ ์ง์— ๋Œ€ํ•ด์„œ๋งŒ ์ž‘๋™ํ•œ๋‹ค.3 ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ์—๋Š” head ๋ฐ tail์ด fst ๋ฐ snd์™€ ๋Œ€๋žต์ ์œผ๋กœ ๋น„์Šทํ•˜๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์€ (:)๋กœ ์—ฐ๊ฒฐ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ถ„ํ•ดํ•œ๋‹ค. head๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋กœ ํ‰๊ฐ€๋˜๊ณ , tail์€ ๋ฆฌ์ŠคํŠธ์˜ ๋‚˜๋จธ์ง€๋ฅผ ๋Œ๋ ค์ค€๋‹ค. ์˜ˆ: head์™€ tail ์‚ฌ์šฉํ•˜๊ธฐ Prelude> 2:[7,5,0] [2,7,5,0] Prelude> head [2,7,5,0] Prelude> tail [2,7,5,0] [7,5,0] ๋…ธํŠธ ๋ถˆํ–‰ํžˆ๋„ head์™€ tail์—๋Š” ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค. ์ด๊ฒƒ๋“ค ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋นˆ ๋ฆฌ์ŠคํŠธ์— ์ ์šฉํ•˜๋ฉด... Prelude> head [] *** Exception: Prelude.head: empty list ...ํ„ฐ์ง€๊ณ  ๋งŒ๋‹ค. ๋นˆ ๋ฆฌ์ŠคํŠธ๋Š” ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋„, ๋‹ค๋ฅธ ์›์†Œ๋„ ๊ฐ€์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. GHCi ๋ฐ”๊นฅ์—์„œ ๋นˆ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด head๋‚˜ tail์„ ์‹คํ–‰ํ–ˆ๋‹ค๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ๊ณ ์žฅ ๋‚ฌ์„ ๊ฒƒ์ด๋‹ค. ๋‹น๋ถ„๊ฐ„ head์™€ tail์„ ๋‹ค๋ฃฐ ํ…๋ฐ, ์‹ค์ œ ์ฝ”๋“œ์—์„œ ๊ทธ๋Ÿฐ ๊ธฐ๋Šฅ ๊ณ ์žฅ์ด ์ผ์–ด๋‚  ์œ„ํ—˜์€ ํ”ผํ•˜๊ณ  ์‹ถ๋‹ค. ๋‚˜์ค‘์—๋Š” ๋” ๋‚˜์€ ์„ ํƒ์ง€๋ฅผ ๋ฐฐ์šธ ๊ฒƒ์ด๋‹ค. "๋ญ๊ฐ€ ๋ฌธ์ œ์•ผ? ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ํ˜ธ์ถœํ•˜๊ธฐ ์ „์— ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ์–ด๋–ป๊ฒŒ๋“  ํ™•์ธํ•˜๋ฉด head์™€ tail์€ ์ž˜ ์ž‘๋™ํ•  ํ…๋ฐ."๋ผ๋Š” ์˜๋ฌธ์ด ๋“ค ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ์ด๋Ÿฐ ๋ฐฉ์‹์€ ์œ„ํ—˜ํ•˜๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ์ ์  ๋” ์ปค์ง€๊ณ  ๋ณต์žกํ•ด์ง€๋ฉด ์šฐ๋ฆฌ๊ฐ€ head๋‚˜ tail์— ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•˜๊ฒŒ ๋  ์ˆ˜๋„ ์žˆ๋Š” ์žฅ์†Œ๊ฐ€ ๊ธ‰๊ฒฉํžˆ ๋งŽ์•„์ง€๊ณ , ์šฐ๋ฆฌ๊ฐ€ ์‹ค์ˆ˜ํ•  ์ˆ˜๋„ ์žˆ๋Š” ์žฅ์†Œ๋„ ๊ทธ๋งŒํผ ๋งŽ์•„์ง„๋‹ค. ๊ฒฝํ—˜์ƒ ํ•จ์ˆ˜๊ฐ€ ๊ฒฝ๊ณ  ์—†์ด ์‹คํŒจํ•˜๋Š” ๊ฒƒ์€ ํ”ผํ•ด์•ผ ํ•œ๋‹ค. ์ด ์ฑ…์„ ์ง„ํ–‰ํ•จ์— ๋”ฐ๋ผ ๊ทธ๋Ÿฐ ์œ„ํ—˜์„ ํ”ผํ•  ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•๋“ค์„ ๋ฐฐ์šฐ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ๋ณด๋ฅ˜๋œ ์งˆ๋ฌธ๋“ค ์—ฌ๊ธฐ์„œ ์†Œ๊ฐœํ•œ ๋„ค ํ•จ์ˆ˜๋Š” ์ด ์ ˆ์„ ์‹œ์ž‘ํ•  ๋•Œ ๋งํ•œ ๋ฌธ์ œ๋ฅผ ์™„๋ฒฝํžˆ ํ’€๊ธฐ์—๋Š” ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™๋‹ค. fst์™€ snd๊ฐ€ ์ง์˜ ๊ฒฝ์šฐ์—๋Š” ๋งŒ์กฑ์Šค๋Ÿฌ์šด ํ•ด๊ฒฐ์ฑ…์„ ์ œ๊ณตํ•˜์ง€๋งŒ ์„ธ ๊ฐœ ์ด์ƒ์˜ ์›์†Œ๊ฐ€ ์žˆ๋Š” ํŠœํ”Œ์€ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ? ๊ทธ๋ฆฌ๊ณ  ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋‚˜๋จธ์ง€์™€ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋‚˜์€ ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ? ์ง€๊ธˆ์€ ์ด ์งˆ๋ฌธ๋“ค์„ ๋ณด๋ฅ˜ํ•  ๊ฒƒ์ด๋‹ค. ๋Œ€์‹  ํ•„์ˆ˜์ ์ธ ๊ธฐ๋ฐ˜์„ ๋‹ค์ง„ ํ›„์— ๋ฆฌ์ŠคํŠธ ์กฐ์ž‘์— ๊ด€ํ•œ ์ดํ›„์˜ ์žฅ๋“ค์—์„œ ์ด ์ฃผ์ œ๋กœ ๋Œ์•„์˜ฌ ๊ฒƒ์ด๋‹ค. ์ง€๊ธˆ์€ ๋ฆฌ์ŠคํŠธ์˜ head์™€ tail์„ ๋ถ„๋ฆฌํ•˜๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๋ฌด์—‡์ด๋“  ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋‘์ž. ์—ฐ์Šต๋ฌธ์ œ fst์™€ snd๋ฅผ ๊ฒฐํ•ฉํ•ด ํŠœํ”Œ (("Hello", 4), True)์—์„œ 4๋ฅผ ์ถ”์ถœํ•ด ๋ณด์ž. ํ‘œ์ค€ ์ฒด์Šค ํ‘œ๊ธฐ๋ฒ•์€ ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค. ํ‘œ์ค€์—์„œ๋Š” ํ–‰์„ 1์—์„œ 8๋กœ, ์—ด์„ a์—์„œ h๋กœ ์„ผ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ด€๋ก€์ƒ ์—ด์„ ๋จผ์ € ๋งํ•œ๋‹ค. ํŠน์ • ์œ„์น˜์— ('a', 4)์ฒ˜๋Ÿผ ๋ฌธ์ž ํ•˜๋‚˜์™€ ์ˆซ์ž ํ•˜๋‚˜๋กœ ์ด๋ฆ„ํ‘œ๋ฅผ ๋ถ™์ผ ์ˆ˜ ์žˆ์„๊นŒ? ์ด๊ฒƒ๊ณผ ๋ฆฌ์ŠคํŠธ์˜ ์ค‘์š”ํ•œ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ? ๋ฆฌ์ŠคํŠธ์˜ head์™€ tail์„ ํŠœํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฐ ๋‘ ๋ฒˆ์งธ ์›์†Œ๋กœ์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. ๋ฆฌ์ŠคํŠธ์˜ ๋‹ค์„ฏ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ head์™€ tail์„ ์ด์šฉํ•ด ์ž‘์„ฑํ•ด ๋ณด์ž. ์ด๊ฒƒ์— ๋ฌด์—‡์ด ์„ฑ๊ฐ€์‹œ๊ณ  ์–ด๋–ค ๋‹จ์ ์ด ์žˆ๋Š”์ง€ ๋น„ํ‰ํ•ด ๋ณด์ž. ๋‹คํ˜•์„ฑ ํƒ€์ž… ๋ฆฌ์ŠคํŠธ์˜ ํƒ€์ž…์€ ๊ทธ ์›์†Œ๋“ค์˜ ํƒ€์ž…์— ์˜์กดํ•˜๋ฉฐ ์›์†Œ์˜ ํƒ€์ž…์— ๊ฐ๊ด„ํ˜ธ๋ฅผ ๊ฐ์‹ธ์„œ ํ‘œ๊ธฐํ•œ๋‹ค. Prelude> :t [True, False] [True, False] :: [Bool] Prelude> :t ["hey", "my"] ["hey", "my"] :: [[Char]] ๊ทธ๋Ÿฌ๋ฏ€๋กœ Bool์˜ ๋ฆฌ์ŠคํŠธ๋Š” [Char] (์ฆ‰ ๋ฌธ์ž์—ด)์˜ ๋ฆฌ์ŠคํŠธ๋‚˜ Int์˜ ๋ฆฌ์ŠคํŠธ์™€๋Š” ๋‹ค๋ฅธ ํƒ€์ž…์ด๋‹ค. ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜์˜ ํƒ€์ž…์— ๋ช…์‹œํ•œ ํƒ€์ž…์˜ ์ธ์ž๋งŒ ๋ฐ›์•„๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ „์—์„œ ๋ณต์žกํ•œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๊ณค ํ•œ๋‹ค. head์˜ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. [Int], [Bool], [String]์€ ๋ชจ๋‘ ๋‹ค๋ฅธ ํƒ€์ž…์ด๋ฏ€๋กœ headInt :: [Int] -> Int, headBool :: [Bool] -> Bool, headString :: [String] -> String ๋“ฑ ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋ณ„๋„์˜ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฌ๋ฉด ๋„ˆ๋ฌด ์„ฑ๊ฐ€์‹œ๊ณ  ๋ฌด๋ถ„๋ณ„ํ•˜๋‹ค. ๋ฌด์—‡๋ณด๋‹ค ๋ฆฌ์ŠคํŠธ๋Š” ๊ทธ๊ฒƒ์ด ํฌํ•จํ•˜๋Š” ๊ฐ’๋“ค์˜ ํƒ€์ž…์— ๋ฌด๊ด€ํ•˜๊ฒŒ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์กฐ๋ฆฝ๋œ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ์–ป์–ด๋‚ด๋Š” ์ ˆ์ฐจ๋„ ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋™์ผํ•˜๊ธฐ๋ฅผ ๋ฐ”๋ž€๋‹ค. ๋‹คํ–‰ํžˆ๋„ ๋ชจ๋“  ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋Š” head๋ผ๋Š” ๋‹จ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค. Prelude> head [True, False] True Prelude> head ["hey", "my"] "hey" ์ด๊ฒŒ ์–ด๋–ป๊ฒŒ ๊ฐ€๋Šฅํ• ๊นŒ? ํ‰์†Œ๋Œ€๋กœ head์˜ ํƒ€์ž…์„ ํ™•์ธํ•ด ๋ณด๋ฉด ์ข‹์€ ํžŒํŠธ๊ฐ€ ๋ณด์ธ๋‹ค. -- ์˜ˆ์ œ: ์šฐ๋ฆฌ์˜ ์ฒซ ๋ฒˆ์งธ ๋‹คํ˜• ํƒ€์ž… Prelude> :t head head :: [a] -> a ๊ฐ๊ด„ํ˜ธ ์•ˆ์˜ a๋Š” ํƒ€์ž…์ด ์•„๋‹ˆ๋‹ค. ํƒ€์ž… ์ด๋ฆ„์€ ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์ž. ์ด๊ฒƒ์€ ํƒ€์ž… ๋ณ€์ˆ˜๋‹ค. ํ•˜์Šค์ผˆ์€ ํƒ€์ž… ๋ณ€์ˆ˜์˜ ์ž๋ฆฌ์— ์•„๋ฌด ํƒ€์ž…์ด๋‚˜ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค. ํƒ€์ž… ์ด๋ก (์ˆ˜ํ•™์˜ ํ•œ ๊ฐˆ๋ž˜)์—์„œ๋Š” ์ด๊ฒƒ์„ ๋‹คํ˜•์„ฑ polymorphism์ด๋ผ ๋ถ€๋ฅธ๋‹ค. ๋‹จ์ผ ํƒ€์ž…๋งŒ์„ ๊ฐ€์ง€๋Š” ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋Š” ๋‹จ์ผ ํ˜• monomorphic์ด๋ผ ํ•˜๊ณ , ํ•˜๋‚˜๋ณด๋‹ค ๋งŽ์€ ํƒ€์ž…์„ ํ—ˆ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํƒ€์ž… ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋“ค์€ ๋‹คํ˜•์„ฑ polymorphic์ด๋ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด head์˜ ํƒ€์ž…์ด ๋œปํ•˜๋Š” ๋ฐ”๋Š” ์ž„์˜ ํƒ€์ž…(a)์˜ ๊ฐ’๋“ค์˜ ๋ฆฌ์ŠคํŠธ([a])๋ฅผ ์ทจํ•ด์„œ ๊ฐ™์€ ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜๋‚˜์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ ๋‚ด์—์„œ, ํ•œ ํƒ€์ž… ๋ณ€์ˆ˜์˜ ๋ชจ๋“  ๊ฒฝ์šฐ๋Š” ๊ฐ™์€ ํƒ€์ž…์ด์–ด์•ผ ํ•จ์„ ๋ช…์‹ฌํ•˜์ž. ์˜ˆ๋ฅผ ๋“ค์–ด, f :: a -> a ๋Š” f๊ฐ€ ์ž„์˜ ํƒ€์ž…์˜ ์ธ์ž๋ฅผ ์ทจํ•ด ์ธ์ž์™€ ๊ฐ™์€ ํƒ€์ž…์˜ ๋ฌด์–ธ๊ฐ€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๋œป์ด๊ณ , ๋ฐ˜๋ฉด์— f :: a -> b ๋Š” f๊ฐ€ ์ž„์˜ ํƒ€์ž…์˜ ์ธ์ž๋ฅผ ์ทจํ•ด ์ž„์˜ ํƒ€์ž…์˜ ๊ฒฐ๊ณผ๋ฅผ ๋Œ๋ ค์ฃผ๋Š”๋ฐ ๊ทธ ํƒ€์ž…์ด a ์ž๋ฆฌ์— ๋“ค์–ด๊ฐˆ ํƒ€์ž…๊ณผ ๊ฐ™์„ ์ˆ˜๋„ ๊ฐ™์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ํƒ€์ž… ๋ณ€์ˆ˜๋“ค์€ ๊ทธ ํƒ€์ž…๋“ค์ด ๋ฐ˜๋“œ์‹œ ์„œ๋กœ ๋‹ค๋ฅด๋‹ค๊ณ  ๋ช…์‹œํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋‹จ์ง€ ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ๋งํ•  ๋ฟ์ด๋‹ค. ์˜ˆ์ œ: fst์™€ snd ์•ž์„œ ๋ดค๋“ฏ์ด fst์™€ snd๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ง์˜ ์ผ๋ถ€๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ์ง€๊ธˆ์ฏค์€ ํ•จ์ˆ˜๋ฅผ ๋ณผ ๋•Œ๋งˆ๋‹ค "์ด๊ฒƒ์˜ ํƒ€์ž…์ด ๋ญ˜๊นŒ?" ํ•˜๊ณ  ๊ถ๊ธˆํ•ดํ•˜๋Š” ์Šต๊ด€์ด ์ƒ๊ฒผ์„ ๊ฒƒ์ด๋‹ค. fst์™€ snd์˜ ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ž. ๋‘ ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ ์ง์„ ํ•˜๋‚˜ ์ทจํ•ด์„œ ์ด ์ง์˜ ํ•œ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฌด์—‡๋ณด๋‹ค ์ง์˜ ํƒ€์ž…์€ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ๊ทธ ์›์†Œ๋“ค์˜ ํƒ€์ž…์— ์˜์กดํ•˜๋ฏ€๋กœ ์ด ํ•จ์ˆ˜๋“ค๋„ ๋‹คํ˜•์„ฑ์ด์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŠœํ”Œ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ทธ ๋‚ด๋ถ€ ํƒ€์ž…๋“ค์ด ๋™์ข…์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฑธ ๋ช…์‹ฌํ•˜์ž. ๋”ฐ๋ผ์„œ ์ด๋ ‡๊ฒŒ ์ ์œผ๋ฉด fst :: (a, a) -> a fst๊ฐ€ ์ง์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ์›์†Œ๊ฐ€ ๊ฐ™์€ ํƒ€์ž…์ผ ๋•Œ๋งŒ ์ž‘๋™ํ•˜๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ฌ๋ฐ”๋ฅธ ํƒ€์ž…์€ ์˜ˆ์ œ: fst์™€ snd์˜ ํƒ€์ž… fst :: (a, b) -> a snd :: (a, b) -> b fst์™€ snd์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ ๋ง๊ณ ๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋ชฐ๋ž๋‹ค๊ณ  ํ•ด๋„ ์ด๊ฒƒ๋“ค์ด ๊ฐ๊ฐ ์ง์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ์ถ”์ธกํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฆฌ๊ฐ€ ์žˆ์ง€๋งŒ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค๋„ ์ด์™€ ๊ฐ™์€ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ๋‹ค. ์‹œ๊ทธ๋„ˆ์ณ๊ฐ€ ๋งํ•ด์ฃผ๋Š” ๊ฒƒ์€ ์ง์˜ ์ฒซ ๋ฒˆ์งธ ๋ฐ ๋‘ ๋ฒˆ์งธ ๋ถ€๋ถ„๊ณผ ํƒ€์ž…์ด ๊ฐ™์€ ๋ฌด์–ธ๊ฐ€๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ๋ฟ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋‹ค์Œ ํ•จ์ˆ˜๋“ค์— ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ๋ถ€์—ฌํ•ด ๋ณด์ž. ์ด์ „ ์ ˆ์˜ ์„ธ ๋ฒˆ์งธ ์—ฐ์Šต๋ฌธ์ œ์˜ ํ•ด๋‹ต("... ๋ฆฌ์ŠคํŠธ์˜ ๋จธ๋ฆฌ์™€ ๊ผฌ๋ฆฌ๋ฅผ ํŠœํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ์›์†Œ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜") ์ด์ „ ์ ˆ์˜ ๋„ค ๋ฒˆ์งธ ์—ฐ์Šต๋ฌธ์ œ์˜ ํ•ด๋‹ต("... ๋ฆฌ์ŠคํŠธ์˜ ๋‹ค์„ฏ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜") h x y z = chr (x - 2) (์ด์ „ ์žฅ์—์„œ ๋…ผํ•œ chr์„ ๊ธฐ์–ตํ•˜์ž) ์š”์•ฝ ์ด ์žฅ์—์„œ ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์ด๋ผ๋Š” ๋‘ ์ƒˆ๋กœ์šด ๊ฐœ๋…์„ ์†Œ๊ฐœํ–ˆ๋‹ค. ๋‘˜ ์‚ฌ์ด์˜ ์ค‘์š”ํ•œ ์œ ์‚ฌ์ ๊ณผ ์ฐจ์ด์ ์„ ์ข…ํ•ฉํ•ด ๋ณด์ž. ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ๊ด„ํ˜ธ์™€ ์‰ผํ‘œ๋กœ ์ •์˜๋œ๋‹ค: [1, 2, 3] ๋ฆฌ์ŠคํŠธ๋Š” ๊ทธ ์›์†Œ๋“ค์˜ ํƒ€์ž…์ด ๊ฐ™์€ ํ•œ ๋ฌด์—‡์ด๋“  ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ๋˜ํ•œ cons ์—ฐ์‚ฐ์ž (:)๋ฅผ ์ด์šฉํ•ด ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ๋ฌด์–ธ๊ฐ€๋ฅผ ๋ฆฌ์ŠคํŠธ์— cons ํ•˜๋Š” ๊ฒƒ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ํŠœํ”Œ์€ ๊ด„ํ˜ธ์™€ ์‰ผํ‘œ๋กœ ์ •์˜๋œ๋‹ค: ("Bob", 32) ํŠœํ”Œ์€ ์„œ๋กœ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๋ฌด์—‡์ด๋“  ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค. ํŠœํ”Œ์˜ ๊ธธ์ด๋Š” ๊ทธ ํŠœํ”Œ์˜ ํƒ€์ž…์— ๋ฐ˜์˜๋˜์–ด ์žˆ๋‹ค. ์ฆ‰ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๋‘ ํŠœํ”Œ์€ ํƒ€์ž…์ด ๋‹ค๋ฅด๋‹ค. ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์€ ์–ด๋–ค ์‹์œผ๋กœ๋“  ๊ฒฐํ•ฉ๋  ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ, ๋ฆฌ์ŠคํŠธ์˜ ํŠœํ”Œ... ํ•˜์ง€๋งŒ ๊ทธ ์กฐํ•ฉ์ด ํƒ€๋‹นํ•˜๋ ค๋ฉด ๊ทธ๊ฒƒ๋“ค์˜ ๊ธฐ์ค€์„ ๋งŒ์กฑํ•ด์•ผ๋งŒ ํ•œ๋‹ค. ์ด์ฏค์—์„œ ํƒ€์ž…์˜ ๊ฐ€์น˜์— ๋Œ€ํ•ด ์˜๋ฌธ์ด ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. ํƒ€์ž…์€ ์ฒ˜์Œ์—๋Š” ์„ฑ๊ฐ€์‹œ๊ฒŒ ๋Š๊ปด์ง€์ง€๋งŒ ์‚ฌ์‹ค์€ ์•„์ฃผ ์œ ์šฉํ•˜๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ํ•˜์Šค ์ผˆ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋‹ค๊ฐ€ ๋ฌด์–ธ๊ฐ€ ํ„ฐ์ง€๋ฉด, ์ฐจ๋ผ๋ฆฌ "ํƒ€์ž… ์˜ค๋ฅ˜"์˜€์œผ๋ฉด ํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. โ†ฉ ์ข€ ๋” ๊ธฐ์ˆ ์ ์œผ๋กœ ๋งํ•˜์ž๋ฉด, "... ์›์†Œ๋“ค์„ ํˆฌ์˜ํ•˜๋Š” ํˆฌ์˜์ฒด..." ์ด๋‹ค. ์ˆ˜ํ•™์ ์œผ๋กœ๋Š”, ํ•œ ๊ตฌ์กฐ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜๋ฅผ ํˆฌ์˜(projection)์ด๋ผ๊ณ  ํ•œ๋‹ค. โ†ฉ ์‚ฌ์‹ค ์ž„์˜ ํฌ๊ธฐ์˜ ํŠœํ”Œ์—์„œ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์„ค๊ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ƒ๊ฐ๋งŒํผ ๊ฐ„๋‹จ์น˜ ์•Š์€ ์ผ์ด๊ณ , ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ fst์™€ snd๋Š” ๊ทธ๋Ÿฐ ์‹์œผ๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. โ†ฉ 6 ํƒ€์ž…์˜ ๊ธฐ์ดˆ 2 ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Type_basics_II Num ํด๋ž˜์Šค ์ˆซ์ž ํƒ€์ž…๋“ค ๋‹คํ˜•์„ฑ ์ถ”์ธก ๋‹จ์ผํ˜• ๋ฌธ์ œ ์ˆซ์ž ๋„ˆ๋จธ์˜ ํด๋ž˜์Šค ๋…ธํŠธ ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ์ˆซ์ž ํƒ€์ž…๋“ค์ด ํ•˜์Šค์ผˆ์—์„œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ๋˜๋Š”์ง€ ๋ณด์—ฌ์ฃผ๊ณ  ํƒ€์ž… ์‹œ์Šคํ…œ์˜ ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ํŠน์„ฑ์„ ์†Œ๊ฐœํ•œ๋‹ค. ๊ณ„์†ํ•˜๊ธฐ์— ์•ž์„œ ์ž ์‹œ ์ˆจ์„ ๊ณ ๋ฅด๊ณ  ์ด๋Ÿฐ ์งˆ๋ฌธ์„ ์ƒ๊ฐํ•ด ๋ณด์ž. (+) ํ•จ์ˆ˜์˜ ํƒ€์ž…์€ ๋ฌด์—‡์ผ๊นŒ?1 Num ํด๋ž˜์Šค ์ˆ˜ํ•™์—๋Š” ํ•จ๊ป˜ ๋”ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜์˜ ์ข…๋ฅ˜์— ๋ช‡ ๊ฐ€์ง€ ์ œ์•ฝ์ด ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด 2 + 3(๋‘ ์ž์—ฐ์ˆ˜), (-7) + 5.12(์Œ์˜ ์ •์ˆ˜์™€ ์‹ค์ˆ˜), 1/7 + ฯ€(์œ ๋ฆฌ์ˆ˜์™€ ๋ฌด๋ฆฌ์ˆ˜)... ๋“ฑ์ด ์žˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ ๋ชจ๋‘ ํƒ€๋‹นํ•˜๋‹ค. ์‚ฌ์‹ค ๋ชจ๋“  ์ž„์˜์˜ ๋‘ ์‹ค์ˆ˜๋Š” ๋ง์…ˆ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ๋Ÿฐ ์ผ๋ฐ˜์„ฑ์„ ๊ฐ€์žฅ ๋‹จ์ˆœํ•˜๊ฒŒ ํฌ์ฐฉํ•˜๋ ค๋ฉด ํ•˜์Šค์ผˆ์—๋Š” ์ผ๋ฐ˜ํ™”๋œ Number ํƒ€์ž…์ด ํ•„์š”ํ•˜๊ณ , ๊ทธ๋Ÿฐ (+)์˜ ์‹œ๊ทธ๋„ˆ์ฒ˜๋Š” ๋‹จ์ˆœํžˆ (+) :: Number -> Number -> Number ์ด์–ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ์„ค๊ณ„๋Š” ์ปดํ“จํ„ฐ๊ฐ€ ์‚ฐ์ˆ˜๋ฅผ ํ•˜๋Š” ๋ฐฉ์‹์— ์ž˜ ๋“ค์–ด๋งž์ง€ ์•Š๋Š”๋‹ค. ์ปดํ“จํ„ฐ๋Š” ์ •์ˆ˜๋ฅผ ๋ฉ”๋ชจ๋ฆฌ ๋‚ด ์ผ๋ จ์˜ ์ด์ง„์ˆ˜๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์ง€๋งŒ ์‹ค์ˆ˜์—๋Š” ์ด ์ ‘๊ทผ๋ฒ•์ด ๋จนํžˆ์ง€ ์•Š๋Š”๋‹ค.2 ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์‹ค์ˆ˜๋ฅผ ๋‹ค๋ฃจ๋ ค๋ฉด ๋ถ€๋™์†Œ์ˆ˜์ ์ด๋ผ๋Š” ๋” ๋ณต์žกํ•œ ์ธ์ฝ”๋”ฉ์ด ํ•„์š”ํ•˜๋‹ค. ๋ถ€๋™์†Œ์ˆ˜์ ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์‹ค์ˆ˜๋ฅผ ๋‹ค๋ฃจ๋Š” ํ•ฉ๋ฆฌ์ ์ธ ์ˆ˜๋‹จ์ด์ง€๋งŒ ๋ถˆํŽธํ•œ ์ ๋„ ์žˆ์–ด์„œ (ํŠนํžˆ ์ •๋ฐ€๋„์˜ ์†Œ์‹ค) ์ •์ˆ˜์—๋Š” ๋” ๊ฐ„๋‹จํ•œ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ๋‹ค. ์ฆ‰ ์šฐ๋ฆฌ๋Š” ์ˆซ์ž๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ตœ์†Œํ•œ ๋‘ ๊ฐ€์ง€ ๊ฐ€์ง„๋‹ค. ํ•˜๋‚˜๋Š” ์ •์ˆ˜, ํ•˜๋‚˜๋Š” ์ผ๋ฐ˜์ ์ธ ์‹ค์ˆ˜๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. ๊ฐ ์ ‘๊ทผ๋ฒ•์€ ์„œ๋กœ ๋‹ค๋ฅธ ํ•˜์Šค ์ผˆ ํƒ€์ž…์— ๋Œ€์‘ํ•œ๋‹ค. ๋”์šฑ์ด ์ปดํ“จํ„ฐ๋Š” (+) ๊ฐ™์€ ์—ฐ์‚ฐ์„ ๋™์ผ ํฌ๋งท์˜ ์ˆซ์ž๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฒ”์šฐ์ฃผ์  Number ํƒ€์ž…์€ ๊ณ ์‚ฌํ•˜๊ณ  ์ •์ˆ˜์™€ ์‹ค์ˆ˜๋ฅผ ์„ž๋Š” (+)๋„ ์“ธ ์ˆ˜ ์—†๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ•˜์Šค์ผˆ์€ ์ ์–ด๋„ ์ •์ˆ˜๋“ค ๋˜๋Š” ์‹ค์ˆ˜๋“ค ์‚ฌ์ด์—๋Š” ๋™์ผํ•œ (+) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. GHCi์—์„œ ์ง์ ‘ ํ™•์ธํ•ด ๋ณด์ž. Prelude> 3 + 4 Prelude> 4.34 + 3.12 7.46 ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์„ ๋…ผํ•  ๋•Œ, ํ•จ์ˆ˜๊ฐ€ ๋‹คํ˜•์„ฑ์ด๋ฉด ์„œ๋กœ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ์ธ์ž๋“ค์„ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๋Š” ๊ฑธ ๋ดค์—ˆ๋‹ค. ์ด๋Ÿฐ ์‚ฌ์‹ค์„ ๊ณ ๋ คํ•ด ๋ณด๋ฉด (+)์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋กœ ์ด๋Ÿฐ ๊ฒŒ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™๋‹ค. (+) :: a -> a -> a (+)๋Š” a๋ผ๋Š” ๋™์ผ ํƒ€์ž…(์ •์ˆ˜์ผ ์ˆ˜๋„ ๋ถ€๋™์†Œ์ˆ˜์  ์ˆ˜์ผ ์ˆ˜๋„ ์žˆ๋Š”)์˜ ๋‘ ์ธ์ˆ˜๋ฅผ ์ทจํ•ด a ํƒ€์ž…์˜ ๊ฒฐ๊ณผ๋กœ ํ‰๊ฐ€ํ•  ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ด ํ•ด๊ฒฐ์ฑ…์—๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ์•ž์„œ ๋ดค๋“ฏ์ด ํƒ€์ž… ๋ณ€์ˆ˜ a๋Š” ๋ชจ๋“  ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. (+)์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๊ฐ€ ์ด๊ฒƒ์ด๋ผ๋ฉด ๋‘ Bool๋„, ๋‘ Char๋„ ๋”ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑด๋ฐ ๊ทธ๋‹ค์ง€ ๋ง์ด ๋˜์ง€ ์•Š๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋Œ€์‹  (+)์˜ ์‹ค์ œ ์‹œ๊ทธ๋„ˆ์ณ๋Š” ํ•œ ์–ธ์–ด ํŠน์„ฑ์„ ํ™œ์šฉํ•˜๋Š”๋ฐ, ์ด ํŠน์„ฑ์€ a๊ฐ€ ์ˆซ์ž ํƒ€์ž…์ธ ํ•œ ์–ด๋Š ํƒ€์ž…์ด๋“  ๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์˜๋ฏธ์ƒ ์ œํ•œ์„ ๊ฑฐ๋Š” ๊ฒƒ์„ ๊ฐ€๋Šฅ์ผ€ ํ•œ๋‹ค. (+) :: (Num a) => a -> a -> a Num์€ ํƒ€์ž… ํด๋ž˜์Šค๋กœ, ์ˆซ์ž๋กœ ๊ฐ„์ฃผ๋˜๋Š” ๋ชจ๋“  ํƒ€์ž…์„ ์•„์šฐ๋ฅด๋Š”, ํƒ€์ž…๋“ค์˜ ๋ชจ์ž„์ด๋‹ค. 3 ์‹œ๊ทธ๋„ˆ์ณ์˜ (Num a) => ๋ถ€๋ถ„์€ a๋ฅผ ์ˆซ์žํ˜•์œผ๋กœ, ์ข€ ๋” ์ •ํ™•ํ•˜๊ฒŒ๋Š”, Num์˜ ์ธ์Šคํ„ด์Šค๋“ค๋กœ ์ œํ•œํ•œ๋‹ค. ์ˆซ์ž ํƒ€์ž…๋“ค ๊ทธ๋Ÿฌ๋ฉด ์‹œ๊ทธ๋„ˆ์ณ์—์„œ a๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” Num์˜ ์ธ์Šคํ„ด์Šค๋ผ๋Š” ๊ฑด ์‹ค์ œ๋กœ๋Š” ๋ฌด์Šจ ์ˆซ์ž ํƒ€์ž…์ผ๊นŒ? ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ˆซ์ž ํƒ€์ž…์€ Int, Integer, Double์ด๋‹ค. Int๋Š” ๋Œ€๋ถ€๋ถ„ ์–ธ์–ด์˜ ๊ทธ ์ •์ˆ˜ ํƒ€์ž…์— ๋Œ€์‘ํ•œ๋‹ค. ์ปดํ“จํ„ฐ์˜ ํ”„๋กœ์„ธ์„œ์— ๋”ฐ๋ผ ๊ณ ์ •๋œ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. (32๋น„ํŠธ ๊ธฐ๊ณ„์—์„œ๋Š” -2147483648์—์„œ 2147483647๊นŒ์ง€) Integer๋„ ์ •์ˆ˜๋ฅผ ์œ„ํ•ด ์“ฐ์ด์ง€๋งŒ Int์™€ ๋‹ฌ๋ฆฌ ํšจ์œจ์„ฑ์„ ์กฐ๊ธˆ ํฌ์ƒํ•ด์„œ ์ž„์˜ ํฌ๊ธฐ์˜ ๊ฐ’์„ ์ง€์›ํ•œ๋‹ค. Double์€ ๋ฐฐ์ •๋ฐ€๋„ ๋ถ€๋™์†Œ์ˆ˜์  ํƒ€์ž…์œผ๋กœ, ๋Œ€๋‹ค์ˆ˜์˜ ๊ฒฝ์šฐ ์‹ค์ˆ˜๋ฅผ ์œ„ํ•œ ์ข‹์€ ์„ ํƒ์ด๋‹ค. (Float์ด๋ผ๊ณ  Double์˜ ๋‹จ์ • ๋ฐ€๋„ ์นœ๊ตฌ๊ฐ€ ์žˆ๋Š”๋ฐ ์ •๋ฐ€๋„์˜ ์ถ”๊ฐ€ ์†์‹ค ๋•Œ๋ฌธ์— ๋Œ€๊ฐœ๋Š” Double์— ๋ฐ€๋ฆฐ๋‹ค) ์ด๋“ค ํƒ€์ž…์€ ํ•˜์Šค์ผˆ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ์ผ์ƒ ์ž‘์—…์—์„œ ์“ฐ๊ฒŒ ๋  ๊ฒƒ๋“ค์ด๋‹ค. ๋‹คํ˜•์„ฑ ์ถ”์ธก ์•„์ง ์„ค๋ช…ํ•˜์ง€ ์•Š์€ ๊ฒŒ ํ•˜๋‚˜ ๋” ์žˆ๋‹ค. ์‹œ์ž‘๋ถ€์—์„œ ์–ธ๊ธ‰ํ•œ ๋ง์…ˆ ์˜ˆ์‹œ๋ฅผ ์‹œ๋„ํ•ด ๋ดค๋‹ค๋ฉด ์ด๋Ÿฐ ๊ฒƒ์ด ์™„๋ฒฝํžˆ ํƒ€๋‹นํ•˜๋‹จ ๊ฑธ ์•Œ๊ณ  ์žˆ์„ ๊ฒƒ์ด๋‹ค. Prelude> (-7) + 5.12 -1.88 ์—ฌ๊ธฐ์„œ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๋‘ ์ˆซ์ž๋ฅผ ๋”ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์ •์ˆ˜์™€ ์ •์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฒƒ... (+)์˜ ํƒ€์ž… ๋•Œ๋ฌธ์— ๋ถˆ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์•˜์—ˆ๋‚˜? ์ด ๋ฌผ์Œ์— ๋‹ตํ•˜๋ ค๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์ž…๋ ฅํ•œ ์ˆซ์ž๋“ค์ด ์‹ค์ œ๋กœ๋Š” ๋ฌด์Šจ ํƒ€์ž…์ธ์ง€ ๋ด์•ผ ํ•œ๋‹ค. Prelude> :t (-7) (-7) :: (Num a) => a ์ด๊ฒƒ ๋ณด๊ฒŒ, (-7)์€ Int๋„ Integer๋„ ์•„๋‹ˆ๋‹ค! ๋Œ€์‹  ์ด๊ฒƒ์€ ๋‹คํ˜•์„ฑ ์ƒ์ˆ˜๋กœ์„œ, ํ•„์š”ํ•œ ์–ด๋Š ์ˆซ์ž ํƒ€์ž…์œผ๋กœ๋“  "๋ณ€ํ•  ์ˆ˜" ์žˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋‹ค๋ฅธ ์ˆซ์ž๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋ช…ํ™•ํ•ด์ง„๋‹ค. Prelude> :t 5.12 5.12 :: (Fractional t) => t 5.12๋„ ๋‹คํ˜•์„ฑ ์ƒ์ˆ˜์ธ๋ฐ Num๋ณด๋‹ค ์ œํ•œ์ ์ธ Fractional ํด๋ž˜์Šค์— ์†ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ชจ๋“  Fractional์€ Num์ด์ง€๋งŒ ๋ชจ๋“  Num์ด Fractional์ธ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค(๊ฐ€๋ น Int์™€ Integer๋Š” Fractional์ด ์•„๋‹ˆ๋‹ค) ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์€ (-7) + 5.12๋ฅผ ํ‰๊ฐ€ํ•  ๋•Œ, ์ด ์ˆซ์ž๋“ค์˜ ์‹ค์ œ ํƒ€์ž…์„ ๊ฒฐ์ •ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ ๋ฐฉ๋ฒ•์€ ํด๋ž˜์Šค ๋ช…์„ธ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ํƒ€์ž… ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (-7)์€ ์–ด๋–ค Num๋„ ๋  ์ˆ˜ ์žˆ์ง€๋งŒ, 5.12์—๋Š” ์ถ”๊ฐ€์ ์ธ ์ œํ•œ์ด ์žˆ์–ด์„œ, 5.12์˜ ํƒ€์ž…์ด (-7)์ด ๋ฌด์—‡์ด ๋ ์ง€๋ฅผ ๊ฒฐ์ •ํ•  ๊ฒƒ์ด๋‹ค. ๋‘˜์˜ ํƒ€์ž…์ด ์–ด๋•Œ์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ๋‹จ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— 5.12๋Š” ๊ธฐ๋ณธ์ ์ธ Fractional ํƒ€์ž…์ธ Double๋กœ ๊ฐ„์ฃผ๋  ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ (-7)๋„ Double์ด ๋˜๊ณ  ๋ง์…ˆ์„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜์—ฌ Double์„ ๋ฐ˜ํ™˜๋ฐ›๊ฒŒ ๋œ๋‹ค.4 ์ด ๊ณผ์ •์„ ๋” ์™€๋‹ฟ๊ฒŒ ํ•˜๋Š” ๋น ๋ฅธ ๊ฒ€์‚ฌ๊ฐ€ ์žˆ๋‹ค. ์†Œ์Šค ํŒŒ์ผ์—์„œ ์ด๋ ‡๊ฒŒ ์ •์˜ํ•œ๋‹ค. x = 2 ๊ทธ๋ฆฌ๊ณ  ์ด ํŒŒ์ผ์„ GHCi๋กœ ๋ถˆ๋Ÿฌ์™€ x์˜ ํƒ€์ž…์„ ํ™•์ธํ•œ๋‹ค. ๊ทธ๋‹ค์Œ ํŒŒ์ผ์— ๋ณ€์ˆ˜ y๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. x = 2 y = x + 3 ํŒŒ์ผ์„ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์™€์„œ x์™€ y์˜ ํƒ€์ž…์„ ํ™•์ธํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ y๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  x = 2 y = x + 3.1 ๋‘ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์— ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ๋ณด๋ผ. ๋‹จ์ผํ˜• ๋ฌธ์ œ ์ˆซ์ž ํƒ€์ž…๊ณผ ํด๋ž˜์Šค์˜ ์ •๊ตํ•จ์€ ๋ณต์žกํ•จ์œผ๋กœ ์ด์–ด์ง€๊ธฐ๋„ ํ•œ๋‹ค. ๊ฐ€๋ น, ํ”ํ•œ ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ์ž์ธ (/)๋ฅผ ๋ณด์ž. ๊ทธ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (/) :: (Fractional a) => a -> a -> a a๋ฅผ ๋ถ„์ˆ˜ํ˜•์œผ๋กœ ์ œํ•œํ•˜๋Š” ๊ฒƒ์€ ํ•„์ˆ˜์‚ฌํ•ญ์ธ๋ฐ ๋‘ ์ •์ˆ˜์˜ ๋‚˜๋ˆ—์…ˆ ๊ฒฐ๊ณผ๋Š” ๋Œ€๊ฐœ ์ •์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ž˜๋„ ์—ฌ์ „ํžˆ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. Prelude> 4 / 3 1.3333333333333333 ๋ฆฌํ„ฐ๋Ÿด 4์™€ 3์€ ๋‹คํ˜•์„ฑ ์ƒ์ˆ˜๊ณ  (/)์˜ ์š”๊ตฌ์— ๋”ฐ๋ผ Double ํƒ€์ž…์œผ๋กœ ๊ฐ„์ฃผ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ, ํ•œ ์ˆซ์ž๋ฅผ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์„ ๊ฐ€์ •ํ•ด ๋ณด์ž. 5 ๋‹น์—ฐํžˆ length ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. Prelude> 4 / length [1,2,3] ๊ทธ๋Ÿฐ๋ฐ... ์ด๋Ÿฐ... <interactive>:1:0: No instance for (Fractional Int) arising from a use of `/' at <interactive>:1:0-17 Possible fix: add an instance declaration for (Fractional Int) In the expression: 4 / length [1, 2, 3] In the definition of `it': it = 4 / length [1, 2, 3] ํ‰์†Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด ๋ฌธ์ œ๋Š” length์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ๋ณด๋ฉด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. length :: [a] -> Int length์˜ ๊ฒฐ๊ณผ๋Š” ๋‹คํ˜•์„ฑ ์ƒ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ Int์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  Int๋Š” Fractional์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— (/)์˜ ์‹œ๊ทธ๋„ˆ์ณ์— ๋“ค์–ด๋งž์ง€ ์•Š๋Š”๋‹ค. ์ด ๋ฌธ์ œ์—์„œ ํƒˆ์ถœํ•˜๋Š” ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•˜๋Š” ํŽธ๋ฆฌํ•œ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ๊ธ€์„ ๊ณ„์† ์ฝ๊ธฐ ์ „์— ์ด๋ฆ„๊ณผ ์‹œ๊ทธ๋„ˆ์ณ๋งŒ์œผ๋กœ ์ด ํ•จ์ˆ˜๊ฐ€ ํ•˜๋Š” ์ผ์„ ์ถ”์ธกํ•ด ๋ณด์ž. fromIntegral :: (Integral a, Num b) => a -> b fromIntegral์€ Integral ํƒ€์ž…(Int๋‚˜ Integer ๋“ฑ)์˜ ๋ฌด์–ธ๊ฐ€๋ฅผ ์ธ์ž๋กœ ์ทจํ•ด์„œ ๋‹คํ˜•์„ฑ ์ƒ์ˆ˜๋กœ ๋งŒ๋“ ๋‹ค. length์™€ ๊ฒฐํ•ฉํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋ฅผ (/)์˜ ์‹œ๊ทธ๋„ˆ์ณ์— ๋งž์ถœ ์ˆ˜ ์žˆ๋‹ค. Prelude> 4 / fromIntegral (length [1,2,3]) 1.3333333333333333 ์ฒ˜์Œ์—๋Š” ์ด ํ‘œํ˜„์‹์ด ๊ณผ๋„ํ•˜๊ฒŒ ๋ณต์žกํ•ด ๋ณด์ด๊ฒ ์ง€๋งŒ, ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ์ˆซ์ž๋ฅผ ์กฐ์ž‘ํ•  ๋•Œ ํ‘œํ˜„์‹์„ ๋” ์ฒ ์ €ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ธ์ž๊ฐ€ Int์ธ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋ฉด ๊ทธ ์ธ์ž๋Š” ์ ˆ๋Œ€ ์•Œ์•„์„œ Integer๋‚˜ Double๋กœ ๋ณ€ํ™˜๋˜์ง€ ์•Š๋Š”๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด fromIntegral ๊ฐ™์€ ํ•จ์ˆ˜๋กœ ํ”„๋กœ๊ทธ๋žจ์—๊ฒŒ ๊ทธ ์ผ์„ ๋ช…ํ™•ํžˆ ์ง€์‹œํ•ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ ์ ˆ์ œ๋œ ํƒ€์ž… ์ฒด๊ณ„์˜ ๊ฒฐ๊ณผ๋กœ ํ•˜์Šค์ผˆ์—๋Š” ์ˆซ์ž๋ฅผ ๋‹ค๋ฃจ๋Š” ๋†€๋ž๋„๋ก ๋‹ค์–‘ํ•œ ํด๋ž˜์Šค์™€ ํ•จ์ˆ˜๋“ค์ด ์žˆ๋‹ค. ์ˆซ์ž ๋„ˆ๋จธ์˜ ํด๋ž˜์Šค ํƒ€์ž… ํด๋ž˜์Šค์˜ ์šฉ๋„๋Š” ์‚ฐ์ˆ ์„ ๋„˜์–ด ๋งŽ์€ ๊ฒƒ์ด ์žˆ๋‹ค. ๊ฐ€๋ น (==)์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (==) :: (Eq a) => a -> a -> Bool (+)๋‚˜ (/)์ฒ˜๋Ÿผ (==)๋„ ๋‹คํ˜•์„ฑ ํ•จ์ˆ˜๋‹ค. (==)๋Š” ๊ฐ™์€ ํƒ€์ž…์˜ ๋‘ ๊ฐ’์„ ๋น„๊ตํ•ด Bool ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ์ด ๋‘ ๊ฐ’์€ ๋ฐ˜๋“œ์‹œ Eq ํด๋ž˜์Šค์— ์†ํ•ด์•ผ ํ•œ๋‹ค. Eq๋Š” ํ•ญ๋“ฑ ๋น„๊ต๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฐ’๋“ค์˜ ํƒ€์ž…๋“ค์˜ ํด๋ž˜์Šค์ด๋ฉฐ ๋ชจ๋“  ๊ธฐ๋ณธ์ ์ธ ๋น„ํ•จ์ˆ˜ํ˜• ํƒ€์ž…๋“ค์ด ์—ฌ๊ธฐ์— ํฌํ•จ๋œ๋‹ค. 6 ํƒ€์ž… ํด๋ž˜์Šค๋Š” ํƒ€์ž… ์ฒด๊ณ„์— ๋งŽ์€ ๊ฐ•๋ ฅํ•จ์„ ๋ณดํƒœ๋Š” ์•„์ฃผ ๋ฒ”์šฉ์ ์ธ ์–ธ์–ด ํŠน์„ฑ์ด๋‹ค. ์ด ์ฑ…์˜ ๋’ท๋ถ€๋ถ„์—์„œ ์ด ์ฃผ์ œ๋กœ ๋Œ์•„์™€ ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ์šฐ๋ฆฌ ์ž…๋ง›๋Œ€๋กœ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์„ ๋ณผ ๊ฒƒ์ด๋‹ค. ๋…ธํŠธ "ํƒ€์ž…์˜ ๊ธฐ์ดˆ"์—์„œ ์šฐ๋ฆฌ๊ฐ€ ํ•œ ๊ถŒ๊ณ ๋ฅผ ๋”ฐ๋ž๋‹ค๋ฉด :t๋กœ ์‹œํ—˜ํ•ด ๋ด์„œ ์ด ๊ธฐ๋ฌ˜ํ•œ ๋‹ต์„ ์ด๋ฏธ ๋ดค์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋žฌ๋‹ค๋ฉด, ๊ธ€์—์„œ ์ด์–ด์ง€๋Š” ๋ถ„์„์„ ์ด ์‹œ๊ทธ๋„ˆ์ณ์˜ ์˜๋ฏธ๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ธธ๋กœ ์—ฌ๊ธฐ๊ธฐ๋ฅผ. โ†ฉ ๊ทธ ์ด์œ ๋Š” ๋‘ ์‹ค์ˆ˜ ์‚ฌ์ด์— ๋ฌดํ•œํžˆ ๋งŽ์€ ์‹ค์ˆ˜๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๊ฐ€ ๋ฌด์Šจ ์ง“์„ ํ•ด๋„ ์ด ๋ฌดํ•œํ•จ์„ ๋ฉ”๋ชจ๋ฆฌ์— ๊ทธ๋Œ€๋กœ ํ‘œํ˜„ํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค. โ†ฉ ์•„์ฃผ ๋Š์Šจํ•œ ์ •์˜์ง€๋งŒ ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ๋” ์ž์„ธํžˆ ๋…ผํ•  ์ค€๋น„๊ฐ€ ๋  ๋•Œ๊นŒ์ง€๋Š” ์ถฉ๋ถ„ํ•˜๋‹ค. โ†ฉ ๋…ธ๋ จํ•œ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์—๊ฒŒ : ์ด๊ฒƒ์€ C(๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ๋งŽ์€ ์–ธ์–ด๋“ค)๋กœ ์ž‘์„ฑ๋œ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์•”๋ฌต์  ์บ์ŠคํŒ…(์ •์ˆ˜ ๋ฆฌํ„ฐ๋Ÿด์ด ์กฐ์šฉํžˆ double์œผ๋กœ ๋ณ€ํ™˜๋จ)์„ ๋‹ค๋ฃจ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ํšจ๊ณผ์ฒ˜๋Ÿผ ๋ณด์ผ ๊ฒƒ์ด๋‹ค. ์ฐจ์ด์ ์€ C์—์„œ๋Š” ๊ทธ ๋ณ€ํ™˜์ด ์—ฌ๋Ÿฌ๋ถ„์˜ ๋“ฑ ๋’ค์—์„œ ๋ฒŒ์–ด์ง€์ง€๋งŒ ํ•˜์Šค์ผˆ์—์„œ๋Š” ๋ณ€์ˆ˜๋‚˜ ๋ฆฌํ„ฐ๋Ÿด์ด ๋‹คํ˜•์„ฑ ์ƒ์ˆ˜์ผ ๋•Œ๋งŒ ๊ทธ๋Ÿฐ ๋ณ€ํ™˜์ด ์ผ์–ด๋‚œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ ์ฐจ์ด๋Š” ๋ฐ˜๋ก€๋ฅผ ๋ณด๋ฉด ๊ณง ๋ถ„๋ช…ํ•ด์งˆ ๊ฒƒ์ด๋‹ค. โ†ฉ ์žˆ์„ ๋ฒ•ํ•œ ์‹œ๋‚˜๋ฆฌ์˜ค - ๋ฆฌ์ŠคํŠธ ๋‚ด ๊ฐ’๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•ด ๋ณด์ž. โ†ฉ ๋‘ ํ•จ์ˆ˜์˜ ํ•ญ๋“ฑ ๋น„๊ต๋Š” ์•„์ฃผ ๋‹ค๋ฃจ๊ธฐ ํž˜๋“  ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ๋œ๋‹ค. โ†ฉ 7 ๋‹ค์Œ ๊ณผ์ • ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Next_steps if / then / else ํŒจํ„ด ๋งค์นญ์˜ ๋„์ž… ํŠœํ”Œ ํŒจํ„ด๊ณผ ๋ฆฌ์ŠคํŠธ ํŒจํ„ด let ๋ฐ”์ธ๋”ฉ ๋…ธํŠธ ์ด ์žฅ์—์„œ๋Š” ํŒจํ„ด ๋งค์นญ์ด๋ผ๋Š” ํ•˜์Šค์ผˆ์˜ ํ•ต์‹ฌ ํŠน์„ฑ๊ณผ if ํ‘œํ˜„์‹ ๋ฐ let ๋ฐ”์ธ๋”ฉ์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฌธ๋ฒ•์„ ์†Œ๊ฐœํ•œ๋‹ค. if / then / else ํ•˜์Šค ์ผˆ ๋ฌธ๋ฒ•์€ if... then... (else...) ํ˜•ํƒœ์˜ ํ”ํ•œ ์กฐ๊ฑด ํ‘œํ˜„์‹์„ ์ง€์›ํ•œ๋‹ค. ๊ฐ€๋ น ์ธ์ž๊ฐ€ 0๋ณด๋‹ค ์ž‘์œผ๋ฉด (-1)์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ์ธ์ž๊ฐ€ 0์ด๋ฉด 0์„, ์ธ์ž๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด 1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ๋Ÿฐ ์ผ์„ ํ•˜๋Š” signum์ด๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์ด๋ฏธ ์ •์˜๋˜์–ด ์žˆ์ง€๋งŒ, ์„ค๋ช…์„ ์œ„ํ•ด ์ง์ ‘ ์ •์˜ํ•ด ๋ณด์ž. ์˜ˆ์ œ: signum ํ•จ์ˆ˜ mySignum x = if x < 0 then -1 else if x > 0 then 1 else 0 ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ—˜ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. *Main> mySignum 5 *Main> mySignum 0 *Main> mySignum (5-10) -1 *Main> mySignum (-1) -1 ๋งˆ์ง€๋ง‰์˜ "-1"์„ ๊ฐ์‹ผ ๊ด„ํ˜ธ๋Š” ํ•„์ˆ˜์‚ฌํ•ญ์ด๋‹ค. ๋นผ๋จน์œผ๋ฉด ์‹œ์Šคํ…œ์€ ์—ฌ๋Ÿฌ๋ถ„์ด mySignum์—์„œ 1์„ ๋นผ๋ ค ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•  ๊ฒƒ์ด๊ณ , ์ด๊ฑด ํƒ€์ž… ๋ถˆ๋Ÿ‰์ด๋‹ค. if/then/else ๊ตฌ์กฐ์—์„œ๋Š” ๋งจ ์ฒ˜์Œ์— ์กฐ๊ฑด์‹(์ด ๊ฒฝ์šฐ x < 0)์ด ํ‰๊ฐ€๋œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ True ์ด๋ฉด ๊ตฌ์กฐ ์ „์ฒด๊ฐ€ then ํ‘œํ˜„์‹์œผ๋กœ ํ‰๊ฐ€๋œ๋‹ค. ์•„๋‹ˆ๋ฉด(์กฐ๊ฑด์ด False ์ด๋ฉด) else ํ‘œํ˜„์‹์œผ๋กœ ํ‰๊ฐ€๋œ๋‹ค. ๋ชจ๋“  ๊ฒƒ์ด ์ง๊ด€์ ์ด๋‹ค. ํ•˜์ง€๋งŒ ๋ช…๋ นํ˜• ์–ธ์–ด๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•ด๋ดค๋‹ค๋ฉด ํ•˜์Šค์ผˆ์—์„  ํ•ญ์ƒ then๊ณผ else ๋ชจ๋‘๋ฅผ ์š”๊ตฌํ•œ๋‹ค๋Š” ์ ์ด ๋†€๋ผ์šธ ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค. ์ด๋Š” if ๊ตฌ์กฐ๊ฐ€ ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ ์–ด๋–ค ๊ฐ’์„ ๊ฒฐ๊ณผ๋กœ ๋‚ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ข€ ๋” ์ •ํ™•ํžˆ๋Š”, ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ ๊ฐ™์€ ํƒ€์ž…์˜ ๊ฐ’์„ ๊ฒฐ๊ณผ๋กœ ๋‚ด์•ผ ํ•œ๋‹ค. ์œ„์™€ ๊ฐ™์€ if / then / else ํ•จ์ˆ˜ ์ •์˜๋Š” ์ด์ „ ๊ณผ๋ชฉ์—์„œ ์†Œ๊ฐœํ•œ ๊ฐ€๋“œ ๋ฌธ๋ฒ•์œผ๋กœ ์‰ฝ๊ฒŒ ์žฌ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์ œ: if์—์„œ ๊ฐ€๋“œ๋กœ mySignum x | x < 0 = -1 | x > 0 = 1 | otherwise = 0 ๋น„์Šทํ•˜๊ฒŒ, ์ง„์œ„ ๊ฐ’ ์žฅ์˜ ์ ˆ๋Œ“๊ฐ’ ํ•จ์ˆ˜๋ฅผ if/then/else๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์ œ: guard์—์„œ if๋กœ abs x = if x < 0 then -x else x ์™œ ๊ฐ€๋“œ๋ฅผ ๋†”๋‘๊ณ  if๋ฅผ ์“ฐ๋Š”๊ฐ€? ๋’ค์˜ ์˜ˆ์ œ์—์„œ๋„, ์—ฌ๋Ÿฌ๋ถ„์ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ๋•Œ๋„ ๋ณด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ, ์กฐ๊ฑด๋ถ€๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ฐ๊ฐ์˜ ๋ฐฉ์‹์€ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹๊ฑฐ๋‚˜ ํŽธ๋ฆฌํ•˜๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—๋Š” ๋‘ ๋ฐฉ์‹ ๋ชจ๋‘ ์ž˜ ์ž‘๋™ํ•œ๋‹ค. ํŒจํ„ด ๋งค์นญ์˜ ๋„์ž… ๊ฒฝ์ฃผ์ž๋“ค์ด ๊ฐ ๊ฒฝ์ฃผ์—์„œ์˜ ์ˆœ์œ„์— ๋”ฐ๋ผ ์ ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒฝ์Ÿ์—์„œ ํ†ต๊ณ„์น˜๋ฅผ ์ถ”์ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•ด ๋ณด์ž. ์ ์ˆ˜ ๊ทœ์น™์€ ์ด๋ ‡๋‹ค. ์Šน์ž๋Š” 10์  2๋“ฑ์€ 6์  3๋“ฑ์€ 4์  4๋“ฑ์€ 3์  5๋“ฑ์€ 2์  6๋“ฑ์€ 2์  ๋‚˜๋จธ์ง€๋Š” 0์  ์ˆœ์œ„(1๋“ฑ์€ ์ •์ˆ˜ 1๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๋“ฑ 1)๋ฅผ ๋ฐ›์•„์„œ ํš๋“ํ•œ ์ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•œ ๊ฐ€์ง€ ํ•ด๊ฒฐ์ฑ…์€ if/then/else๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ์ œ: if/then/else๋กœ ์ ์ˆ˜ ์„ธ๊ธฐ pts :: Int -> Int pts x = if x == 1 then 10 else if x == 2 then 6 else if x == 3 then 4 else if x == 4 then 3 else if x == 5 then 2 else if x == 6 then 1 else 0 ์œฝ. ๋ถ„๋ช… if/then/else ๋Œ€์‹  ๊ฐ€๋“œ๋ฅผ ์ผ์œผ๋ฉด ์ด๋ ‡๊ฒŒ ํ‰๋ฌผ์Šค๋Ÿฝ์ง„ ์•Š์•˜์„ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋“  ํ•ญ๋“ฑ ๊ฒ€์‚ฌ๋ฅผ ์“ฐ๊ณ  (๋˜ ์ฝ๋Š”) ๊ฒƒ๋„ ์ง€๋ฃจํ•œ ์ผ์ด๋‹ค. ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์˜ˆ์ œ: ์กฐ๊ฐ ํ•จ์ˆ˜ ์ •์˜๋ฅผ ์ด์šฉํ•ด ์ ์ˆ˜ ์„ธ๊ธฐ pts :: Int -> Int pts 1 = 10 pts 2 = 6 pts 3 = 4 pts 4 = 3 pts 5 = 2 pts 6 = 1 pts _ = 0 ํ›จ์”ฌ ๋‚ซ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ pts๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ •์˜ํ•˜๋ฉด (์ž„์˜๋กœ ์กฐ๊ฐ ์ •์˜๋ผ๊ณ  ๋ถ€๋ฅด์ž) ๋ถ„๋ช… ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ์‚ฌ๋žŒ์€ ์ด ํ•จ์ˆ˜๊ฐ€ ๋ฌด์—‡์„ ํ•˜๋Š”์ง€ ๋ช…ํ™•ํžˆ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ทธ ๋ฌธ๋ฒ•์€ ์šฐ๋ฆฌ๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ํ•˜์Šค์ผˆ์ด๋ผ๊ธฐ์—” ๊ธฐ์ดํ•˜๋‹ค. ์™œ pts์— ๋“ฑ์‹์ด 7๊ฐœ๋‚˜ ์žˆ์„๊นŒ? ์ขŒ๋ณ€์˜ ์ˆซ์ž๋“ค์€ ๋ญ๊ณ , x๋Š” ์–ด๋””๋กœ ๊ฐ„ ๊ฑธ๊นŒ? ํ•˜์Šค์ผˆ์˜ ์ด๋Ÿฐ ํŠน์„ฑ์€ ํŒจํ„ด ๋งค์นญ์ด๋ผ๊ณ  ํ•œ๋‹ค. pts๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๊ทธ ์ธ์ž๋Š” ๊ฐ ๋“ฑ์‹์˜ ์ขŒ๋ณ€์— ์žˆ๋Š” ์ˆซ์ž, ์ฆ‰ ํŒจํ„ด์ด๋ผ๋Š” ๊ฒƒ๊ณผ ๋น„๊ต(๋งค์นญ) ๋œ๋‹ค. ์ด๋Ÿฌํ•œ ๋น„๊ต๋Š” ๋“ฑ์‹๋“ค์„ ์ž‘์„ฑํ•œ ์ˆœ์„œ๋Œ€๋กœ ํ–‰ํ•ด์ง„๋‹ค. ๊ทธ๋ž˜์„œ ์ธ์ž๋Š” ์ฒซ ๋ฒˆ์งธ ๋“ฑ์‹์˜ 1๊ณผ ๋น„๊ต๋œ๋‹ค. ์ธ์ž๊ฐ€ 1์ด๋ฉด ๋งž๋Š” ๊ฑธ ์ฐพ์€ ๊ฒƒ์ด๊ณ  ์ฒซ ๋ฒˆ์งธ ๋“ฑ์‹์ด ์‚ฌ์šฉ๋œ๋‹ค. ๋”ฐ๋ผ์„œ pts 1์€ 10์œผ๋กœ ํ‰๊ฐ€๋œ๋‹ค. ์•„๋‹ˆ๋ฉด ๊ฐ™์€ ์ ˆ์ฐจ๋ฅผ ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋“ฑ์‹๋“ค์ด ์‹œ๋„๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งˆ์ง€๋ง‰์€ ์‚ฌ๋ญ‡ ๋‹ค๋ฅด๋‹ค. _๋Š” ํŠน์ˆ˜ ํŒจํ„ด์œผ๋กœ, ์ข…์ข… "์™€์ผ๋“œ์นด๋“œ"๋ผ ๋ถ€๋ฅด๋Š”๋ฐ, "์•„๋ฌด๊ฑฐ๋‚˜"๋ผ๊ณ  ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. _๋Š” ๋ชจ๋“  ๊ฒƒ๊ณผ ์ผ์น˜ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ธ์ž๊ฐ€ ์•ž์„  ํŒจํ„ด๋“ค ์ค‘ ์–ด๋Š ๊ฒƒ์—๋„ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด pts๋Š” 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ธ์ž๋ฅผ ๋‚˜ํƒ€๋‚ผ x ๊ฐ™์€ ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ด์œ ๋Š”, ์ •์˜๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๊ทธ๋Ÿฐ ๊ฒŒ ํ•„์š”ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๋ฐ˜ํ™˜๊ฐ’์ด ์ƒ์ˆ˜๋‹ค. ๋ณ€์ˆ˜๋Š” ์ •์˜์˜ ์šฐ๋ณ€๊ณผ์˜ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์šฐ๋ฆฌ์˜ pts ํ•จ์ˆ˜์—์„œ๋Š” x๊ฐ€ ํ•„์š” ์—†๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋ฉด pts๋ฅผ ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๊ฒฝ์ฃผ์ž๋“ค์—๊ฒŒ ์ฃผ์–ด์ง€๋Š” ์ ์ˆ˜๋Š” 3๋“ฑ์—์„œ 6๋“ฑ๊นŒ์ง€๋Š” ๊ท ์ผํ•˜๊ฒŒ 1์ ์”ฉ ๊ฐ์†Œํ•œ๋‹ค. ์—ฌ๊ธฐ์— ์ฐฉ์•ˆํ•ด์„œ ๋ฐฉ์ •์‹ 7๊ฐœ ์ค‘ 3๊ฐœ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์ œ: ํ˜ผํ•ฉ ์Šคํƒ€์ผ pts :: Int -> Int pts 1 = 10 pts 2 = 6 pts x | x <= 6 = 7 - x | otherwise = 0 ์ฆ‰ ์ •์˜์˜ ๋‘ ๊ฐ€์ง€ ์Šคํƒ€์ผ์„ ์„ž์„ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์‹ค์€ ๋“ฑ์‹์˜ ์ขŒ๋ณ€์— pts x๋ผ๊ณ  ์“ธ ๋•Œ๋„ ํŒจํ„ด ๋งค์นญ์„ ์ด์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค! ํŒจํ„ด์œผ๋กœ์„œ์˜ x๋Š”(๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ๋ณ€์ˆ˜ ์ด๋ฆ„๋“ค์€) _์ฒ˜๋Ÿผ ๋ชจ๋“  ๊ฒƒ๊ณผ ์ผ์น˜ํ•œ๋‹ค. ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ์šฐ๋ณ€์—์„œ๋„ ๊ทธ ์ด๋ฆ„์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค(์ด ๊ฒฝ์šฐ 7 - x๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•˜๋‹ค). ์—ฐ์Šต๋ฌธ์ œ pts์˜ ๋‘ ๋ฒˆ์งธ ๋ฒ„์ „์—์„œ ์„ธ ๋ฒˆ์งธ ๋ฒ„์ „์œผ๋กœ ์˜ฎ๊ฒจ๊ฐˆ ๋•Œ ์†์ž„์ˆ˜๋ฅผ ์•ฝ๊ฐ„ ์ผ๋‹ค. ์ด ๋‘˜์€ ์ •ํ™•ํžˆ ๊ฐ™์€ ์ผ์„ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๊ทธ ์ฐจ์ด๋ฅผ ์•Œ์•„๋ณด๊ฒ ๋Š”๊ฐ€? ์ •์ˆ˜ ์™ธ์—๋„ ํŒจํ„ด ๋งค์นญ์€ ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ๊ฐ’๊ณผ ์ž‘๋™ํ•œ๋‹ค. ํ•œ ๊ฐ€์ง€ ์œ ์šฉํ•œ ์˜ˆ๋Š” ๋ถˆ๋ฆฌ์–ธ์ด๋‹ค. ๊ฐ€๋ น ์ง„์œ„ ๊ฐ’์—์„œ ๋งŒ๋‚ฌ๋˜ ๋…ผ๋ฆฌํ•ฉ ์—ฐ์‚ฐ์ž (||)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์ œ: (||) (||) :: Bool -> Bool -> Bool False || False = False _ || _ = True ๋˜๋Š” ์˜ˆ์ œ: ๋‹ค๋ฅธ ๋ฐฉ์‹์˜ (||) (||) :: Bool -> Bool -> Bool True || _ = True False || y = y ํ•œ ๋ฒˆ์— ๋‘ ๊ฐœ ์ด์ƒ์˜ ์ธ์ž๋“ค์„ ๋น„๊ตํ•˜๋ฉด, ๋ชจ๋“  ์ธ์ž๊ฐ€ ์ผ์น˜ํ•  ๋•Œ๋งŒ ๋“ฑ์‹์ด ์‚ฌ์šฉ๋œ๋‹ค. ํŒจํ„ด ๋งค์นญ์„ ์“ธ ๋•Œ ์ž˜๋ชป๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์„ ๋ช‡ ๊ฐœ ๋…ผ์˜ํ•˜๋ฉฐ ์ด ์ ˆ์„ ๋งˆ๋ฌด๋ฆฌํ•˜์ž. ๋ชจ๋“  ๊ฒƒ์— ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด(pts ์˜ˆ์ œ์˜ ๋งˆ์ง€๋ง‰ ํŒจํ„ด ๊ฐ™์€ ๊ฒƒ)์„ ๋” ๊ตฌ์ฒด์ ์ธ ํŒจํ„ด์˜ ์•ž์— ๋†“์œผ๋ฉด ํ›„์ž๋Š” ๋ฌด์‹œ๋œ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ GHC(i)๋Š” "ํŒจํ„ด ๋งค์นญ์ด ์ค‘๋ณต๋จ Pattern match(es) are overlapped"์ด๋ผ ๊ฒฝ๊ณ ํ•  ๊ฒƒ์ด๋‹ค. ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์ด ํ•˜๋‚˜๋„ ์—†์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ํ†ต์ƒ ํŒจํ„ด๋“ค์ด ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค๋ฃจ๋„๋ก ํ•˜๋Š” ๊ฒŒ ์ข‹์€ ์ƒ๊ฐ์ธ๋ฐ, otherwise ๊ฐ€๋“œ๊ฐ€ ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ ๊ฐ•๋ ฅํžˆ ๊ถŒ์žฅ๋˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, (&&) 2๋ฅผ ์—ฌ๋Ÿฌ ๋ฐฉ์‹์œผ๋กœ ์žฌ์ •์˜ํ•˜๋ฉฐ ๋†€ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๊ฒƒ์€ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ๋ฒ„์ „์ด๋‹ค. (&&) :: Bool -> Bool -> Bool x && x = x -- oops! _ && _ = False ์ด ํ”„๋กœ๊ทธ๋žจ์€ ์ธ์ž๋“ค์ด ๊ฐ™์€์ง€ ๊ฒ€์‚ฌํ•˜์ง€ ์•Š๋Š”๋ฐ ๋‘˜ ๋‹ค์— ๊ฐ™์€ ์ด๋ฆ„์„ ์ผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋งค์นญ์— ๊ด€ํ•œ ํ•œ, ์ฒซ ๋ฒˆ์งธ ์ค„์— _ && _๋ผ ์“ด ๊ฑฐ๋‚˜ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋” ์•ˆ ์ข‹์•„์ง„๋‹ค. ๋‘ ์ธ์ž์— ๊ฐ™์€ ์ด๋ฆ„์„ ๋ถ€์—ฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— GHC(i)๋Š” "x์˜ ์ •์˜๋“ค์ด ์ƒ์ถฉํ•จ(Conflicting definitions for 'x')"์ด๋ผ๋ฉฐ ์ด ํ•จ์ˆ˜๋ฅผ ๊ฑฐ์ ˆํ•  ๊ฒƒ์ด๋‹ค. ํŠœํ”Œ ํŒจํ„ด๊ณผ ๋ฆฌ์ŠคํŠธ ํŒจํ„ด ์œ„์˜ ์˜ˆ์ œ๋“ค์€ ํŒจํ„ด ๋งค์นญ์ด ๋” ์šฐ์•„ํ•œ ์ฝ”๋“œ ์ž‘์„ฑ์— ๋„์›€์„ ์ค€๋‹ค๋Š” ๊ฒƒ์€ ๋ณด์—ฌ์ฃผ์ง€๋งŒ, ํŒจํ„ด ๋งค์นญ์ด ์™œ ์ค‘์š”ํ•œ์ง€๋Š” ์„ค๋ช…ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿผ fst์˜ ์ •์˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ์ด ํ•จ์ˆ˜๋Š” ์ง์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ์ถ”์ถœํ•œ๋‹ค. ์ง€๊ธˆ ์‹œ์ ์—์„œ ์ด๋Š” ๋ถˆ๊ฐ€๋Šฅํ•œ ์ผ์ฒ˜๋Ÿผ ๋ณด์ด๋Š”๋ฐ, ์ง์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์— ์ ‘๊ทผํ•˜๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์€ fst ์ž์ฒด๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‹ค์Œ ํ•จ์ˆ˜๋Š” fst์™€ ๊ฐ™์€ ์ผ์„ ํ•œ๋‹ค. (GHCi์—์„œ ํ™•์ธํ•ด ๋ณด์ž) ์˜ˆ์ œ: fst์˜ ์ •์˜ fst' :: (a, b) -> a fst' (x, _) = x ๋งˆ๋ฒ•์ด๋‹ค! ๋“ฑ์‹์˜ ์ขŒ๋ณ€์—์„œ ์ •๊ทœ ๋ณ€์ˆ˜๋ฅผ ์“ฐ๋Š” ๋Œ€์‹  ์ธ์ž๋ฅผ 2-์ง์˜ ํŒจํ„ด์œผ๋กœ ๊ธฐ์ž…ํ–ˆ๋‹ค. ์ฆ‰ ๋ณ€์ˆ˜์™€ _ ํŒจํ„ด์œผ๋กœ ์ฑ„์›Œ์ง„ (,) ์ธ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด ๋ณ€์ˆ˜๋Š” ํŠœํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ์„ฑ๋ถ„๊ณผ ์ž๋™์œผ๋กœ ์—ฐ๊ด€๋˜๊ณ  ๋“ฑ์‹์˜ ์šฐ๋ณ€์—์„œ ์ด์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. snd์˜ ์ •์˜๋„ ๋ฌผ๋ก  ๋น„์Šทํ•˜๋‹ค. ์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ธฐ๊ต๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ head์™€ tail์˜ ์‹ค์ œ ์ •์˜๋‹ค. ์˜ˆ์ œ: head, tail, ๊ทธ๋ฆฌ๊ณ  ํŒจํ„ด head :: [a] -> a head (x:_) = x head [] = error "Prelude.head: empty list" tail :: [a] -> [a] tail (_:xs) = xs tail [] = error "Prelude.tail: empty list" ์ด์ „ ์˜ˆ์ œ์™€ ๊ทผ๋ณธ์ ์œผ๋กœ ๋‹ค๋ฅธ ์ ์€ (,)๋ฅผ cons ์—ฐ์‚ฐ์ž (:)์˜ ํŒจํ„ด์œผ๋กœ ๋Œ€์ฒดํ•œ ๊ฒƒ๋ฟ์ด๋‹ค. ์ด ํ•จ์ˆ˜๋“ค๋„ ๋นˆ ๋ฆฌ์ŠคํŠธ []์˜ ํŒจํ„ด์„ ํ™œ์šฉํ•˜๋Š” ๋“ฑ์‹์„ ํฌํ•จํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋นˆ ๋ฆฌ์ŠคํŠธ๋Š” ๋จธ๋ฆฌ๋‚˜ ๊ผฌ๋ฆฌ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— error๋กœ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ ๋ง๊ณ ๋Š” ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์ด ์—†๋‹ค. ์š”์•ฝํ•˜์ž๋ฉด, ํŒจํ„ด ๋งค์นญ์˜ ์ง„์ •ํ•œ ํž˜์€ ์ด๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ ๋ณต์žกํ•œ ๊ฐ’์˜ ์ผ๋ถ€์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ํŠนํžˆ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ์€ ์žฌ๊ท€์™€ ๊ทธ ์ดํ›„์˜ ์žฅ์—์„œ ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ์“ฐ์ผ ๊ฒƒ์ด๋‹ค. ๋‚˜์ค‘์— ์ด ๋งˆ๋ฒ• ๊ฐ™์€ ํŠน์„ฑ์˜ ์™ธ๊ฒฌ ๋’คํŽธ์—์„œ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ํƒ๊ตฌํ•  ๊ฒƒ์ด๋‹ค. let ๋ฐ”์ธ๋”ฉ let ๋ฐ”์ธ๋”ฉ์„ ์งง๊ฒŒ ์†Œ๊ฐœํ•˜๋ฉฐ ์ด๋ฒˆ ์žฅ์„ ๋งˆ๋ฌด๋ฆฌํ•˜๊ฒ ๋‹ค. (let์€ ์ง€์—ญ ์„ ์–ธ์„ ์œ„ํ•œ where ์ ˆ์˜ ๋Œ€์ฒด๋ฌธ์ด๋‹ค) ์˜ˆ๋ฅผ ๋“ค์–ด x + x c ๊ผด์˜ ๋‹คํ•ญ์‹์˜ ํ•ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๋ณด์ž. (์ฆ‰ ์ด์ฐจ ๋ฐฉ์ •์‹์˜ ๋‹ต. ์ค‘ํ•™๊ต ์ˆ˜ํ•™ ์‹œ๊ฐ„์„ ๋– ์˜ฌ๋ ค๋ณด์ž) ๊ทธ ๋‹ต์€ ์ด๋ ‡๋‹ค. = b b โˆ’ a 2 x์˜ ๋‘ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. roots a b c = ((-b + sqrt(b*b - 4*a*c)) / (2*a), (-b - sqrt(b*b - 4*a*c)) / (2*a)) ํ•˜์ง€๋งŒ sqrt(b*b - 4*a*c) ํ•ญ์„ ๋‘ ๋ฒˆ ์“ฐ๋Š” ๊ฒƒ์€ ์„ฑ๊ฐ€์‹œ๊ธฐ ๋•Œ๋ฌธ์—, ๋Œ€์‹  where ๋˜๋Š” ๋ฐ‘์—์„œ ์„ค๋ช…ํ•  let ์„ ์–ธ์„ ์ด์šฉํ•˜์—ฌ ์ง€์—ญ ๋ฐ”์ธ๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. roots a b c = let sdisc = sqrt (b*b - 4*a*c) in ((-b + sdisc) / (2*a), (-b - sdisc) / (2*a)) let ํ‚ค์›Œ๋“œ๋ฅผ ์„ ์–ธ ์•ž์— ๋†“๊ณ , in์„ ์ด์šฉํ•ด ํ•จ์ˆ˜์˜ "์ฃผ" ๋ชธ์ฒด๋กœ ๋Œ์•„์˜จ๋‹ค๋Š” ์‹ ํ˜ธ๋ฅผ ๋ณด๋‚ธ๋‹ค. ํ•˜๋‚˜์˜ let... in ๋ธ”๋ก ๋‚ด์— ์—ฌ๋Ÿฌ ์„ ์–ธ์„ ๋†“์„ ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ๋“ค์ด ๊ฐ™์€ ์–‘๋งŒํผ ๋“ค์—ฌ์“ฐ๊ธฐ ๋˜์—ˆ๋Š”์ง€ ํ™•์‹คํžˆ ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋ฌธ๋ฒ• ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. roots a b c = let sdisc = sqrt (b*b - 4*a*c) twice_a = 2*a in ((-b + sdisc) / twice_a, (-b - sdisc) / twice_a) ๊ฒฝ๊ณ : ํ•˜์Šค์ผˆ์—์„  ๋“ค์—ฌ ์“ฐ๊ธฐ๊ฐ€ ๋ฌธ๋ฒ•์ƒ ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์—ฌ๋Ÿฌ๋ถ„์ด ํƒญ์„ ์“ฐ๊ณ  ์žˆ๋Š”์ง€ ์ŠคํŽ˜์ด์Šค๋ฅผ ์“ฐ๊ณ  ์žˆ๋Š”์ง€ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ์ตœ์„ ์ฑ…์€ ํƒญ์„ ๋‘ ๊ฐœ๋‚˜ ๋„ค ๊ฐœ์˜ ์ŠคํŽ˜์ด์Šค๋กœ ๋Œ€์ฒดํ•˜๋„๋ก ํ…์ŠคํŠธ ์—๋””ํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํƒญ์„ ์ŠคํŽ˜์ด์Šค์™€ ๊ตฌ๋ถ„ํ•ด์„œ ์“ธ ๊ฒƒ์ด๋ผ๋ฉด ์ ์–ด๋„ ํƒญ์ด ํ•ญ์ƒ ๊ฐ™์€ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค. ์ž ๊น ๋“ค์—ฌ์“ฐ๊ธฐ ์žฅ์—์„œ ๋“ค์—ฌ์“ฐ๊ธฐ ๊ทœ์น™์˜ ๋ชจ๋“  ๊ฒƒ์„ ๋‹ค๋ฃฌ๋‹ค. ๋…ธํŠธ ์—ฌ๊ธฐ์„œ๋Š” ํ•จ์ˆ˜์— ๋ง์ด ์•ˆ ๋˜๋Š” ๊ฐ’(๊ฐ€๋ น (-4))์„ ์ „๋‹ฌํ•  ๋•Œ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚ ์ง€ ๋ณ„๋กœ ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ ๋ณดํ†ต์€ ๋‚˜์ค‘์— ๊นœ์ง ๋†€๋ผ์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ทธ๋Ÿฐ "์ด์ƒํ•œ" ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค. โ†ฉ GHCi์—์„œ ์ด๋ฅผ ์‹คํ—˜ํ•ด ๋ณด๋ ค๋ฉด, ์ด๋ฆ„ ์ถฉ๋Œ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ๋ถ„์˜ ํ•จ์ˆ˜๋ฅผ ๋ญ”๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ, ๊ฐ€๋ น (&!&)์œผ๋กœ ๋ช…๋ช…ํ•ด ๋ณด์ž. โ†ฉ 8 ์–ดํœ˜ ์Œ“๊ธฐ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Building_vocabulary ํ•จ์ˆ˜ ํ•ฉ์„ฑ ์–ดํœ˜ ์‚ฌ์ „์˜ ํ•„์š”์„ฑ Prelude์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค one exhibit ์–ดํœ˜ ์‚ฌ์ „ ์–ป๊ธฐ ์ด ์ฑ…์—์„œ ์™ธ๋ถ€ ์ž๋ฃŒ๋“ค ๋…ธํŠธ ์ด๋ฒˆ ์žฅ์€ ์•ž๋’ค์˜ ์žฅ๊ณผ๋Š” ๋‹ค์†Œ ๋‹ค๋ฅผ ๊ฒƒ์ด๋‹ค. ์ฃผ๋œ ๋ชฉํ‘œ๊ฐ€ ์ƒˆ ํŠน์„ฑ์„ ์†Œ๊ฐœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•˜์Šค์ผˆ์„ ๊ณต๋ถ€ํ•˜๋Š”(๊ทธ๋ฆฌ๊ณ  ํ™œ์šฉํ•˜๋Š”!) ๊ฒƒ์— ๋Œ€ํ•œ ์กฐ์–ธ์„ ์ฃผ๊ธฐ ์œ„ํ•œ ๋ง‰๊ฐ„ ์ •๋„๋กœ ์ƒ๊ฐํ•˜๋ผ. ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” ํ•จ์ˆ˜๋“ค์˜ ์–ดํœ˜ ์‚ฌ์ „(vocabulary)์„ ์–ป๋Š” ๊ฒƒ์˜ ์ค‘์š”ํ•จ๊ณผ, ์ด์— ๊ด€ํ•ด ์ด ์ฑ…์ด๋‚˜ ๊ธฐํƒ€ ์ž๋ฃŒ์—์„œ ์–ด๋–ป๊ฒŒ ๋„์›€์„ ์ฃผ๋Š”์ง€ ๋…ผํ•  ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ์— ์•ž์„œ ํ•จ์ˆ˜ ํ•ฉ์„ฑ์˜ ์š”์ ์„ ๋น ๋ฅด๊ฒŒ ์งš์–ด๋ณด์ž. ํ•จ์ˆ˜ ํ•ฉ์„ฑ ํ•จ์ˆ˜ ํ•ฉ์„ฑ์€ ์ •๋ง ๊ฐ„๋‹จํ•œ ๊ฐœ๋…์ด๋‹ค. ์–ด๋–ค ๊ฐ’์— ํ•œ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜์— ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. ๋‹ค์Œ์˜ ์ •๋ง ๊ฐ„๋‹จํ•œ ๋‘ ํ•จ์ˆ˜๋ฅผ ๋ณด์ž. ์˜ˆ: ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋“ค f x = x + 3 square x = x^2 ์ด๊ฒƒ๋“ค์„ ๋ฌด์—‡์„ ๋จผ์ € ์ ์šฉํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ฉ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. Prelude> square (f 1) 16 Prelude> square (f 2) 25 Prelude> f (square 1) Prelude> f (square 2) ์•ˆ์ชฝ์˜ ํ•จ์ˆ˜๋ฅผ ๋‘˜๋Ÿฌ์‹ผ ๊ด„ํ˜ธ๋Š” ํ•„์ˆ˜๋‹ค. ์ด๋Ÿฌ์ง€ ์•Š์œผ๋ฉด ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด square f๋‚˜ f square์˜ ๊ฐ’์„ ๊ตฌํ•˜๋ ค ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•  ๊ฒƒ์ด๋‹ค. ๋‘˜ ๋‹ค ์•„๋ฌด ์˜๋ฏธ๋„ ์—†๋‹ค. ๋‘ ํ•จ์ˆ˜์˜ ํ•ฉ์„ฑ์€ ๊ทธ ์ž์ฒด๋กœ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๊ฐ€ ๋œ๋‹ค. ์ˆซ์ž์— f ๋‹ค์Œ square๋ฅผ, ํ˜น์€ ๊ทธ ๋ฐ˜๋Œ€๋กœ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ”„๋กœ๊ทธ๋žจ์—์„œ ํ”ํ•˜๊ณ , ๋˜ ์˜๋ฏธ ์žˆ๊ณ , ํ•˜์—ฌ๊ฐ„ ์ค‘์š”ํ•œ ์—ฐ์‚ฐ์ด๋ผ๋ฉด ์ž์—ฐ์Šค๋ ˆ ๋‹ค์Œ ๋‹จ๊ณ„๋Š” ์ด๋Ÿฐ ๊ฒƒ์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ: ํ•ฉ์„ฑ๋œ ํ•จ์ˆ˜๋“ค squareOfF x = square (f x) fOfSquare x = f (square x) ํ•ฉ์„ฑ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋‹ค๋ฅธ ํ›Œ๋ฅญํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ํ•จ์ˆ˜ ํ•ฉ์„ฑ ์—ฐ์‚ฐ์ž์ธ (.)๋ฅผ ์“ฐ๋Š” ๊ฒƒ์œผ๋กœ, ๋‘ ํ•จ์ˆ˜ ์‚ฌ์ด์— ๊ณต๋ฐฑ์„ ๋„ฃ๋Š” ๊ฒƒ๋งŒํผ์ด๋‚˜ ๊ฐ„๋‹จํ•˜๋‹ค. ์˜ˆ: (.)๋กœ ํ•ฉ์„ฑํ•œ ํ•จ์ˆ˜ squareOfF x = (square . f) x fOfSquare x = (f . square) x ์ด๋ ‡๊ฒŒ ํ•ด๋„ ํ•จ์ˆ˜๋“ค์€ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— g(f(x)) == (g . f) x ์ž„์„ ๋ช…์‹ฌํ•˜๋ผ. 1 ์–ดํœ˜ ์‚ฌ์ „์˜ ํ•„์š”์„ฑ ํ•จ์ˆ˜ ํ•ฉ์„ฑ์€ ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋“ค์„ ๊ธฐ์ดˆ ๋ฒฝ๋Œ ์‚ผ์•„ ๋ณต์žกํ•œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ํ•˜์Šค์ผˆ์˜ ํ•ต์‹ฌ ์ž์งˆ ์ค‘ ํ•˜๋‚˜๋Š” ๊ธฐ๋ฐ˜ ํ•จ์ˆ˜๋ฅผ ๋‚ด๊ฐ€ ์ž‘์„ฑํ–ˆ๋“  ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์ž‘์„ฑํ–ˆ๋“ , ํ•ฉ์„ฑ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ •๋ง ๊ฐ„๋‹จํ•˜๋‹ค๋Š” ๊ฒƒ์œผ๋กœ 2, ๊ฐ„๋‹จํ•˜๊ณ  ์šฐ์•„ํ•˜๊ณ  ํ’๋ถ€ํ•œ ์ฝ”๋“œ ์ž‘์„ฑ์— ๋„์›€์ด ๋œ๋‹ค. ํ•จ์ˆ˜ ํ•ฉ์„ฑ์„ ํ•˜๋ ค๋ฉด ํ•ฉ์„ฑํ•  ํ•จ์ˆ˜๋“ค์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ์ง  ์ฝ”๋“œ์•ผ ์–ด๋–ป๊ฒŒ ์“ฐ๋Š” ๊ฑด์ง€ ์•Œ๊ณ  ์žˆ์ง€๋งŒ, GHC์˜ ์„ค์น˜ํŒ์—๋Š” ๋‹ค์–‘ํ•œ ์ผ๋ฐ˜์ ์ธ ์ž‘์—…์„ ์œ„ํ•œ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•˜๋Š” ๋ง‰๋Œ€ํ•œ ์ข…ํ•ฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค(์ฆ‰ ํŒจํ‚ค์ง€๋กœ ๋ฌถ์ธ ์ฝ”๋“œ)์ด ๋”ธ๋ ค์˜จ๋‹ค. ๊ทธ๋ž˜์„œ ๋Šฅ๋ฅ ์ ์ธ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ ๊ธฐ์ดˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์— ์ต์ˆ™ํ•ด์ ธ์•ผ ํ•œ๋‹ค. ์ตœ์†Œํ•œ, ํ•„์š”ํ•  ๋•Œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด์˜ ๊ด€๋ จ ํ•จ์ˆ˜๋“ค์„ ์ฐพ์„ ์ค„์€ ์•Œ์•„์•ผ ํ•œ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ๋‹ค๋ฅธ ๊ด€์ ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ ํ•˜์Šค ์ผˆ ๋ฌธ๋ฒ•์˜ ์ƒ๋‹นํ•œ ๋ถ€๋ถ„์„ ๊ฒช์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์žฌ๊ท€ ์žฅ์„ ๋๋‚ธ ํ›„์—๋Š” ์›ํ•˜๋Š” ์–ด๋–ค ๋ฆฌ์ŠคํŠธ ์กฐ์ž‘์ด๋“  ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ง€๊ธˆ ์‹œ์ ์—์„œ ์™„์ „ํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฑด ๋”์ฐํ•˜๊ฒŒ ๋น„ํšจ์œจ์ ์ธ๋ฐ, ์ง€๊ธˆ๊นŒ์ง„ ์ฃผ๋กœ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋งŽ์€ ๋ถ€๋ถ„์„ ์žฌ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋๋‚ฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ž˜ ์•Œ๋ ค์ง„ ์ž๋ช…ํ•œ ํ”„๋กœ๊ทธ๋žจ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ์šฐ๋ฆฌ์˜ ๋‡Œ์„ธํฌ๋Š” ์ง„์งœ๋กœ ๊ด€์‹ฌ ์žˆ๋Š” ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ๋‚ซ๋‹ค. ๋˜ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์€ ์šฐ๋ฆฌ๋งŒ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ๋ฐœํ•˜๋Š” ๋ฐ๋„ ๋„์›€์ด ๋œ๋‹ค. 3 Prelude์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค ๋‹ค์Œ์€ ํ•˜์Šค ์ผˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์— ๊ด€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์‚ฌํ•ญ์ด๋‹ค. ๋ฌด์—‡๋ณด๋‹ค๋„ Prelude๋Š” ๋ชจ๋“  ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์— ๊ธฐ๋ณธ์ ์œผ๋กœ ์ ์žฌ๋˜๋Š” ํ•ต์‹ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. ๊ธฐ๋ณธ ํƒ€์ž…๋“ค๊ณผ ๋”๋ถˆ์–ด ์–ด๋””์—๋‚˜ ์“ฐ์ด๊ณ  ๊ทนํžˆ ์œ ์šฉํ•œ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. Prelude ์ž์ฒด ๊ทธ๋ฆฌ๊ณ  Prelude์˜ ํ•จ์ˆ˜๋“ค์„ ์ด ์ž…๋ฌธ์„ฑ ๊ณผ๋ชฉ ์ „์ฒด์— ๊ฑธ์ณ ๊ณ„์† ์ฐธ๊ณ ํ•  ๊ฒƒ์ด๋‹ค. Prelude์˜ ๋„ˆ๋จธ์—๋Š” ๊ด‘๋ฒ”์œ„ํ•œ ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•˜๋Š” ํ•ต์‹ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์ด ์žˆ๋‹ค. ์ด๊ฒƒ๋“ค์ด GHC์™€ ํ•จ๊ป˜ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ Prelude์ฒ˜๋Ÿผ ์ž๋™์œผ๋กœ ๋ถˆ๋Ÿฌ์™€์ง€๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋Œ€์‹  ์ด๊ฒƒ๋“ค์€ ๋ชจ๋“ˆ๋กœ์„œ ์ด์šฉ ๊ฐ€๋Šฅํ•œ๋ฐ, ๋ฐ˜๋“œ์‹œ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ์— ๋“ค์—ฌ์™€์•ผ ํ•œ๋‹ค. ๋‚˜์ค‘์— ๋ชจ๋“ˆ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”๊ฐ€์— ๊ด€ํ•œ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. ์ง€๊ธˆ์€ ์†Œ์Šค ํŒŒ์ผ ์œ—๋ถ€๋ถ„์— ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ ๋“ค์—ฌ์˜ค๋Š” ์ค„์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ๋งŒ ์•Œ์•„๋‘˜ ๊ฒƒ. ์˜ˆ๋ฅผ ๋“ค์–ด permutation ํ•จ์ˆ˜๋Š” Data.List ๋ชจ๋“ˆ์— ๋“ค์–ด์žˆ๋Š”๋ฐ ์ด ๋ชจ๋“ˆ์„ ๋“ค์—ฌ์˜ค๋ ค๋ฉด ์—ฌ๋Ÿฌ๋ถ„์˜. hs ํŒŒ์ผ์˜ ๊ผญ๋Œ€๊ธฐ์— import Data.List๋ผ๋Š” ์ค„์„ ์ถ”๊ฐ€ํ•œ๋‹ค. ๋‹ค์Œ์€ ์™„์ „ํ•œ ์†Œ์Šค ํŒŒ์ผ์˜ ์ƒ๊น€์ƒˆ๋‹ค. ์˜ˆ: ์†Œ์Šค ํŒŒ์ผ์— ๋ชจ๋“ˆ ๋“ค์—ฌ์˜ค๊ธฐ import Data.List testPermutations = permutations "Prelude" GHCi๋กœ ๋น ๋ฅด๊ฒŒ ์‹œํ—˜ํ•ด ๋ณด๋ ค๋ฉด ๋ช…๋ น์ค„์—์„œ :m +Data.List๋ฅผ ์ž…๋ ฅํ•ด ๊ทธ ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. Prelude> :m +Data.List Prelude Data.List> :t permutations permutations :: [a] -> [[a]] one exhibit ๊ณ„์†ํ•˜๊ธฐ ์ „์— Prelude์˜ ๊ธฐ๋ณธ ํ•จ์ˆ˜๋“ค์— ์ต์ˆ™ํ•ด์ง€๋ฉด ๋ฌด์Šจ ๋„์›€์ด ๋˜๋Š”์ง€ ๊ทธ ์˜ˆ์‹œ๋ฅผ ํ•˜๋‚˜ ๋ณด์ž(์•ฝ๊ฐ„ ์ž‘์œ„์ ์ด๋‹ค. ๋™์˜ํ•œ๋‹ค.) 4. ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ๋‚ฑ๋ง๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์„ ์ทจํ•ด ๋‚ฑ๋ง๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋’ค์ง‘์€ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค๊ณ  ์น˜์ž. ์ฆ‰ "Mary had a little lamb"๋Š” "lamb little a had Mary"์ด ๋œ๋‹ค. ์ด์ œ ์šฐ๋ฆฌ๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ํ•˜์Šค์ผˆ๊ณผ ์žฌ๊ท€ ์žฅ์„ ๋ฐฐ์šฐ๋ฉฐ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์•ฝ๊ฐ„์˜ ํ†ต์ฐฐ๋ ฅ์„ ํ•œ๊ป ํ™œ์šฉํ•ด ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์ด ๊ทธ ๋‹ต์ด๋‹ค. ๋„ˆ๋ฌด ์˜ค๋ž˜ ์ณ๋‹ค๋ณด์ง€ ๋ง๋ผ! ์˜ˆ: There be dragons monsterRevWords :: String -> String monsterRevWords input = rejoinUnreversed (divideReversed input) where divideReversed s = go1 [] s where go1 divided [] = divided go1 [] (c:cs) | testSpace c = go1 [] cs | otherwise = go1 [[]] (c:cs) go1 (w:ws) [c] | testSpace c = (w:ws) | otherwise = ((c:w):ws) go1 (w:ws) (c:c':cs) | testSpace c = if testSpace c' then go1 (w:ws) (c':cs) else go1 ([c']:w:ws) cs | otherwise = if testSpace c' then go1 ((c:w):ws) (c':cs) else go1 ((c:w):ws) (c':cs) testSpace c = c == ' ' rejoinUnreversed [] = [] rejoinUnreversed [w] = reverseList w rejoinUnreversed strings = go2 (' ' : reverseList newFirstWord) (otherWords) where (newFirstWord : otherWords) = reverseList strings go2 rejoined ([]:[]) = rejoined go2 rejoined ([]:(w':ws')) = go2 (rejoined) ((' ':w'):ws') go2 rejoined ((c:cs):ws) = go2 (c:rejoined) (cs:ws) reverseList [] = [] reverseList w = go3 [] w where go3 rev [] = rev go3 rev (c:cs) = go3 (c:rev) cs ์ด๊ฒƒ์—๋Š” ๋ฌธ์ œ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ๋‹ค. ๊ทธ์ค‘ ์„ธ ๊ฐœ๋งŒ ๋ณด์ž. ์šฐ๋ฆฌ๊ฐ€ monsterRevWords๋Š” ๊ธฐ๋Œ€๋˜๋Š” ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ๋™์ž‘ํ•˜๋ฉด, ์—ฌ๋Ÿฌ๋ถ„์€ ์ด ํ•จ์ˆ˜๋ฅผ ์˜จ๊ฐ– ์ข…๋ฅ˜์˜ ๊ฐ€๋Šฅํ•œ ์ž…๋ ฅ์— ์ผ์ผ์ด ์‹œํ—˜ํ•ด ๋ณด๊ฑฐ๋‚˜ ์ด๊ฑธ ์ดํ•ดํ•˜๋ ค ์‹œ๋„ํ•˜๋‹ค ๋”์ฐํ•œ ๋‘ํ†ต์ด ์˜ฌ ๊ฒƒ์ด๋‹ค.(๊ทธ๋Ÿฌ์ง€ ๋ง๊ธฐ๋ฅผ...) ๊ฒŒ๋‹ค๊ฐ€ ํ•จ์ˆ˜๋ฅผ ์ด๋ ‡๊ฒŒ ๋ชป๋‚˜๊ฒŒ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ ๋‚˜์ค‘์— ๋ฒ„๊ทธ๋ฅผ ๊ณ ์ณ์•ผ ํ•˜๊ฑฐ๋‚˜ ์กฐ๊ธˆ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค๋ฉด 5, ์‹œ๊ฐ„์„ ๊ฒ๋‚˜๊ฒŒ ํˆฌ์žํ•  ๊ฐ์˜ค๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ์•Œ์•„๋ณด๊ธฐ ์‰ฌ์šด ์ž ์žฌ์ ์ธ ๋ฌธ์ œ๊ฐ€ ์ ์–ด๋„ ํ•˜๋‚˜ ์žˆ๋‹ค. ์ •์˜๋ฅผ ํ•œ ๋ฒˆ ๋” ํ›‘์–ด๋ณด๋ฉด ์ค‘๊ฐ„ ์–ธ์ €๋ฆฌ์— ๋ฌธ์ž๊ฐ€ ๊ณต๋ฐฑ์ธ์ง€ ์•„๋‹Œ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” testSpace๋ผ๋Š” ๋„์šฐ๋ฏธ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๊ฒ€์‚ฌ๋Š” ์˜ค์ง ์ผ๋ฐ˜์ ์ธ ๊ณต๋ฐฑ ๋ฌธ์ž(์ฆ‰ ' ')๋งŒ์„ ํฌํ•จํ•˜๊ณ  ๋‹ค๋ฅธ ๊ณต๋ฐฑ ๋ฌธ์ž๋“ค(ํƒญ, ์ค„๋ฐ”๊ฟˆ ๋“ฑ)์€ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค. 6 ์œ„์˜ ์“ฐ๋ ˆ๊ธฐ ๋Œ€์‹  ๋‹ค์Œ์˜ Prelude ํ•จ์ˆ˜๋“ค์„ ํ™œ์šฉํ•ด์„œ ํ›จ์”ฌ ์ž˜ํ•  ์ˆ˜ ์žˆ๋‹ค. words ํ•จ์ˆ˜. ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ๋‚ฑ๋ง๋“ค๋กœ ๋ฏฟ์„ ๋งŒํ•˜๊ฒŒ ์ชผ๊ฐœ์„œ ๋ฌธ์ž์—ด๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. reverse ํ•จ์ˆ˜. ๋ฆฌ์ŠคํŠธ๋ฅผ ๋’ค์ง‘๋Š”๋‹ค(์šฐ์—ฐํžˆ๋„ ์œ„์˜ reverseList๊ฐ€ ํ•˜๋Š” ์ผ์ด๋‹ค). unwords ํ•จ์ˆ˜. words์˜ ๋ฐ˜๋Œ€๋˜๋Š” ์ผ์„ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํ•จ์ˆ˜ ํ•ฉ์„ฑ์€ ์šฐ๋ฆฌ์˜ ๋ฌธ์ œ๋ฅผ ์ˆœ์‹๊ฐ„์— ํ•ด๊ฒฐํ•œ๋‹ค. ์˜ˆ: ํ•˜์Šค์ผˆ์‹ revWords revWords :: String -> String revWords input = (unwords . reverse . words) input ์งง๊ณ , ๋‹จ์ˆœํ•˜๊ณ , ๊ฐ€๋…์„ฑ ์žˆ๊ณ , Prelude๋Š” ๋ฏฟ์„ ๋งŒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฒ„๊ทธ ๊ฑฑ์ •์ด ์—†๋‹ค. 7 ๊ทธ๋Ÿฌ๋‹ˆ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋‹ค monsterRevWords ๊ฐ™์€ ๊ฒŒ ๋ณด์ด๊ธฐ ์‹œ์ž‘ํ•œ๋‹ค๋ฉด ์—ฌ๋Ÿฌ๋ถ„์˜ ๋„๊ตฌ์ƒ์ž, ์ฆ‰ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋“ค์—ฌ๋‹ค๋ณผ ๊ฒƒ. ์–ดํœ˜ ์‚ฌ์ „ ์–ป๊ธฐ ์œ„์˜ ์—„์ค‘ํ•œ ๊ฒฝ๊ณ  ์ดํ›„ ์šฐ๋ฆฌ๊ฐ€ ์•ž์œผ๋กœ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊นŠ๊ฒŒ ํŒŒ๊ณ ๋“ค ๊ฑฐ๋ผ๊ณ  ๊ธฐ๋Œ€ํ• ์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ๊ทธ ๊ธธ๋กœ ๊ฐ€์ง€ ์•Š๋Š”๋‹ค. ์ ์–ด๋„ ์ด ์ฑ…์˜ ์ดˆ๋ฐ˜๋ถ€์—์„œ๋Š”. ์ดˆ๊ธ‰๋ฐ˜์€ ํ•˜์Šค ์ผˆ ์–ธ์–ด ๊ธฐ์ดˆ์˜ ๋Œ€๋ถ€๋ถ„์„ ์ฝ๊ธฐ ์‰ฝ๊ณ  ์‚ฌ๋ฆฌ์— ๋งž๊ฒŒ ์••์ถ•ํ•˜์—ฌ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ธ๋ฐ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ฒด๊ณ„์ ์œผ๋กœ ๋‹ค๋ฃจ์ž๋ฉด ๊ทธ๊ฑธ ํฌ์ƒํ•ด์•ผ ํ•œ๋‹ค. ์–ด๋Š ๊ฒฝ์šฐ๋“  ์ฝ”์Šค๋ฅผ ๊ณ„์†ํ•˜๋ฉฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ ์ฐจ ์†์— ์ฅ๊ฒŒ ๋  ํ„ฐ์ด๋‹ˆ ์—ฌ๊ธฐ์„œ ๋ฉˆ์ถ”๊ณ  ์Šค์Šค๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ณต๋ถ€ํ•˜๋Ÿฌ ๊ฐˆ ํ•„์š”๋Š” ์—†๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ฐฐ์šธ ๋•Œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๋“ค์„ ๋ช‡ ๊ฐœ ์ œ์•ˆํ•ด ๋ณธ๋‹ค. ์ด ์ฑ…์—์„œ ํ•˜์Šค ์ผˆ ์ดˆ๊ธ‰์— ๋ฐœ์„ ๋“ค์ด๊ณ  ๋‚˜๋ฉด Prelude ํ•จ์ˆ˜๋“ค์˜ ์ •์˜์™€ ๋™๋“ฑํ•œ ๊ฒƒ์„ ์ž‘์„ฑํ•˜๋Š” ์—ฌ๋Ÿฌ ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ๋งŒ๋‚  ๊ฒƒ์ด๋‹ค. (์ฃผ๋กœ ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ์— ๊ด€ํ•œ ๊ฒƒ๋“ค) ์ด ์—ฐ์Šต๋ฌธ์ œ๋“ค์„ ํ’€ ๋•Œ๋งˆ๋‹ค ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ ˆํผํ† ๋ฆฌ์— ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜์ฏค์€ ๋ณดํƒค ์ˆ˜ ์žˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ์˜ˆ์ œ์—์„œ๋“  ๊ทธ๋ƒฅ ์ง€๋‚˜๊ฐ€๋“ฏ ์–ธ๊ธ‰ํ•˜๋“  ๋” ๋งŽ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์†Œ๊ฐœํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋•Œ๋งˆ๋‹ค ํ•จ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ๋ชจ๋กœ ์‹คํ—˜ํ•ด ๋ณด๋ผ. ํƒ€์ž…์˜ ๊ธฐ์ดˆ์—์„œ ์–ธ๊ธ‰ํ•œ, ํƒ€์ž…์— ๊ด€ํ•œ ์Šต๊ด€์ ์ธ ํ˜ธ๊ธฐ์‹ฌ์„ ํ•จ์ˆ˜ ์ž์ฒด๋กœ๊นŒ์ง€ ๋„“ํž ๊ฒƒ. ์ฒ˜์Œ์˜ ๋ช‡ ์žฅ์€ ์„œ๋กœ ์ƒ๋‹นํžˆ ์–ฝํ˜€์žˆ์ง€๋งŒ, ์ฑ…์˜ ๋’ท๋ถ€๋ถ„๋“ค์€ ๋ณด๋‹ค ๋…๋ฆฝ์ ์ด๊ณ , ํŠนํžˆ 3๋ถ€์ธ ํ•˜์Šค ์ผˆ ์‹ค์ „์ด ๊ทธ๋ ‡๋‹ค. ๊ฑฐ๊ธฐ์„œ ๊ณ„์ธต์  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์žฅ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ•˜์Šค ์ผˆ ์ดˆ๊ธ‰์„ ๋–ผ๊ณ  ๋‚˜๋ฉด ๋Œ€๋ถ€๋ถ„์˜ ๋‚ด์šฉ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์ดˆ๊ธ‰๋ฐ˜์˜ ๋์ž๋ฝ์— ๋‹ค๋‹ค๋ฅด๋ฉด ๋…ผ์˜ํ•  ๊ฐœ๋…๋“ค(ํŠนํžˆ ๋ชจ๋‚˜๋“œ)์„ ๋”ฐ๋ผ๊ฐ€๋ฉด ์ž์—ฐ์Šค๋ ˆ ํ•ต์‹ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ค‘์š”ํ•œ ๋ถ€๋ถ„๋“ค์„ ํƒ๊ตฌํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ์™ธ๋ถ€ ์ž๋ฃŒ๋“ค ๋ฌด์—‡๋ณด๋‹ค๋„ ๋ฌธ์„œ๊ฐ€ ์žˆ๋‹ค. ์ง€๊ธˆ ๋‹น์žฅ ์œ ์šฉํ•˜๊ธฐ์—” ํ™•์‹คํžˆ ๋„ˆ๋ฌด ๋ฌด๋ฏธ๊ฑด์กฐํ•˜์ง€๋งŒ ๊ณง ๊ทธ ๊ฐ€์น˜๊ฐ€ ๋“œ๋Ÿฌ๋‚  ๊ฒƒ์ด๋‹ค. ์˜จ๋ผ์ธ์—์„œ Prelude ๋ช…์„ธ์„œ์™€ GHC์— ๋”ธ๋ ค์˜ค๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๊ด€ํ•œ ๋ฌธ์„œ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. ์•ˆ๋‚ด ๊ธฐ๋Šฅ๋„ ์ถœ์ค‘ํ•˜๊ณ  ํ•œ ๋ฒˆ ํด๋ฆญ๋งŒ์œผ๋กœ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Hoogle์€ ๊ธฐ๋ง‰ํžŒ ๋ฌธ์„œ ๊ฒ€์ƒ‰ ์ˆ˜๋‹จ์ด๋‹ค. Hoogle์€ ํ•ต์‹ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค๋ฃจ๋Š” ํ•˜์Šค ์ผˆ ๊ฒ€์ƒ‰ ์—”์ง„์ด๋‹ค. ํ•จ์ˆ˜ ์ด๋ฆ„์ด๋‚˜ ํƒ€์ž… ์„ ์–ธ ๋“ฑ ๋ชจ๋“  ๊ฒƒ์„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. GHC์— ํฌํ•จ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋„ˆ๋จธ์—๋Š” ๊ฑฐ๋Œ€ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ƒํƒœ๊ณ„๊ฐ€ ์žˆ๊ณ  Hackage๋ฅผ ํ†ตํ•ด ์–ป์„ ์ˆ˜ ์žˆ๊ณ  cabal ์ด๋ž€ ๋„๊ตฌ๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. Hackage ์‚ฌ์ดํŠธ์—๋Š” ๊ฑฐ๊ธฐ์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์— ๊ด€ํ•œ ๋ฌธ์„œ๋„ ์žˆ๋‹ค. ์ดˆ๊ธ‰๋ฐ˜์—์„œ๋Š” ํ•ต์‹ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ”๊นฅ์„ ๋ชจํ—˜ํ•˜์ง€ ์•Š์ง€๋งŒ ์—ฌ๋Ÿฌ๋ถ„์ด ์ž์‹ ๋งŒ์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๊ณ  ๋‚˜๋ฉด Hackage์— ์ด๋Œ๋ฆฌ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ๋‹ค๋ฅธ ํ•˜์Šค ์ผˆ ๊ฒ€์ƒ‰ ์—”์ง„์œผ๋กœ Hayoo! ๊ฐ€ ์žˆ๋‹ค. ์ด๊ฒƒ์€ Hackage์˜ ์ „๋ถ€๋ฅผ ๋‹ค๋ฃฌ๋‹ค. ์ ์ ˆํ•œ ๋•Œ๋งˆ๋‹ค ๋‹ค๋ฅธ ํ•™์Šต ์ž๋ฃŒ๋“ค์„ ์†Œ๊ฐœํ•  ๊ฒƒ์ด๋‹ค. ํŠนํžˆ ์ค‘๊ธ‰๋ฐ˜๊ณผ ๊ณ ๊ธ‰ ์ฃผ์ œ์—์„œ. ๋…ธํŠธ (.)๋Š” ๊ฐ™์€ ์‹์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ์ˆ˜ํ•™ ์—ฐ์‚ฐ์ž ฮฟ๋ฅผ ๋ณธ๋œฌ ๊ฒƒ์ด๋‹ค. (g ฮฟ f)(x) = g(f(x))์ด๋‹ค. โ†ฉ ์ด๋Ÿฐ ๊ฐ„ํŽธํ•จ์€ ์šฐ๋ฆฌ๊ฐ€ ์–ธ๊ธ‰ํ•œ ๋ฌธ๋ฒ• ๋•๋ถ„๋งŒ์€ ์•„๋‹ˆ๋ฉฐ, ์ฃผ๋กœ ๋‚˜์ค‘์— ๊นŠ๊ฒŒ ์„ค๋ช…ํ•˜๊ณ  ๋…ผํ•  ํŠน์„ฑ๋“ค, ํŠนํžˆ ๊ณ ์ฐจ ํ•จ์ˆ˜ ๋•๋ถ„์ด๋‹ค. โ†ฉ ํ•œ ๊ฐ€์ง€ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋Š” ๊ณง ๋‚˜์˜ฌ ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ์— ๊ด€ํ•œ ์žฅ๋“ค์—์„œ ๋‹ค๋ฃฐ map, filter, fold ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ์‹œ๋กœ ์—ฌ๋Ÿฌ ๋ชจ๋‚˜๋“œ๊ฐ€ ์žˆ๋Š”๋ฐ ๋‚˜์ค‘์— ์‹ฌ๋„ ์žˆ๊ฒŒ ๊ณต๋ถ€ํ•  ๊ฒƒ์ด๋‹ค. โ†ฉ ์ด ์˜ˆ์ œ๋Š” HaskellWiki์˜ Simple Unix Tools์— ์˜๊ฐ์„ ๋ฐ›์€ ๊ฒƒ์ด๋‹ค. โ†ฉ ๊ณต๋™ ์ €์ž๋“ค์˜ ๋…ธํŠธ: "๋‚˜์ค‘์—? ๋‚œ ์ด๊ฑธ 30๋ถ„ ์ „์— ์ผ๋Š”๋ฐ ์ด๊ฒŒ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ฒŒ์จ ๊ฐ€๋ฌผ๊ฐ€๋ฌผํ•ด..." โ†ฉ ๋ฌธ์ž๊ฐ€ ๊ณต๋ฐฑ์ธ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ์‹ ๋ขฐํ•  ๋งŒํ•œ ๋ฐฉ๋ฒ•์€ Data.Char ๋ชจ๋“ˆ์˜ isSpace ํ•จ์ˆ˜๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด๋‹ค. โ†ฉ ์˜์‹ฌ์ด ๋“ ๋‹ค๋ฉด Prelude์—๋“  Data.List์—๋“  ๋‹ค๋ฅธ ํ•จ์ˆ˜๊ฐ€ ๋งŽ์€๋ฐ ์ด๊ฒƒ๋“ค๋กœ monsterRevWords๋ฅผ ๋ณด๋‹ค ์ •์ƒ์ ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฆ„๋งŒ ์Š์ž๋ฉด (++), concat, groupBy, intersperse ๋“ฑ์ด ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๊ฒฝ์šฐ์—” ํ•œ ์ค„์งœ๋ฆฌ ์ฝ”๋“œ์— ๋น„ํ•  ๋ฐ”๊ฐ€ ์—†๋Š” ๊ฒƒ๋“ค์ด๋ผ ํ•„์š” ์—†๋‹ค. โ†ฉ 9 ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Simple_input_and_output ํ˜„์‹ค๋กœ ๋Œ์•„์˜ค๋‹ค do๋ฅผ ํ†ตํ•œ ์•ก์…˜ ์—ฐ๊ณ„ ์™ผ์ชฝ ํ™”์‚ดํ‘œ์— ๋Œ€ํ•œ ์„ค๋ช… <-๋Š” ๋งˆ์ง€๋ง‰ ์•ก์…˜์„ ์ œ์™ธํ•˜๊ณ  ์–ด๋–ค ์•ก์…˜์—๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค ์•ก์…˜ ์ œ์–ดํ•˜๊ธฐ ์•ก์…˜ ํ•ด๋ถ€ํ•˜๊ธฐ(Actions under the microscope) ์•ก์…˜์˜ ํƒ€์ž…์„ ์—ผ๋‘์— ๋‘”๋‹ค ํ‘œํ˜„์‹ ํƒ€์ž…๋„ ๋”ฐ์ ธ๋ณธ๋‹ค ๋” ์ฝ์„๊ฑฐ๋ฆฌ ํ˜„์‹ค๋กœ ๋Œ์•„์˜ค๋‹ค ์šฐ๋ฆฌ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ๋‚ด๋ถ€์—์„œ๋งŒ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์„ ๋„˜์–ด ์„ธ๊ณ„์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธธ ์›ํ•œ๋‹ค. ์–ด๋Š ์–ธ์–ด๋“  ์ž…๋ฌธ์ž๋ฅผ ์œ„ํ•œ ๊ฐ€์žฅ ํ”ํ•œ ํ”„๋กœ๊ทธ๋žจ์€ ํ™”๋ฉด์— "hello world" ์ธ์‚ฌ๋ง์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์Šค ์ผˆ ๋ฒ„์ „์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Prelude> putStrLn "Hello, World!" putStrLn์€ ํ‘œ์ค€ Prelude ํ•จ์ˆ˜๋‹ค. ์ด๋ฆ„์˜ putStr ๋ถ€๋ถ„์ด ์•”์‹œํ•˜๋“ฏ์ด ์ด ํ•จ์ˆ˜๋Š” String ๊ฐ’์„ ์ธ์ž๋กœ ์ทจํ•ด ๊ทธ๊ฑธ ํ™”๋ฉด์— ํ‘œ์‹œํ•œ๋‹ค. putStr์ด๋ผ๋Š” ํ•จ์ˆ˜ ์ž์ฒด๋„ ๋”ฐ๋กœ ์žˆ์ง€๋งŒ, ๋ณดํ†ต์€ "Ln"์„ ๋ถ™์—ฌ์„œ ์ค„๋ฐ”๊ฟˆ๋„ ์ถœ๋ ฅํ•˜๋„๋ก ๋งŒ๋“ ๋‹ค. ์ด๋Ÿฌ๋ฉด ๋‹ค์Œ์— ์ถœ๋ ฅํ•  ๊ฒƒ์€ ์ƒˆ๋กœ์šด ์ค„์—์„œ ์‹œ์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค. ์ด์ œ ์ด๋Ÿฐ ์ƒ๊ฐ์ด ๋“ค ๊ฒƒ์ด๋‹ค. "putStrLn ํ•จ์ˆ˜์˜ ํƒ€์ž…์ด ๋ญ˜๊นŒ?" ์ด ํ•จ์ˆ˜๋Š” String์„ ์ทจํ•ด์„œ... ๋ญ˜ ๋Œ๋ ค์ฃผ๋ƒ๋ฉด... ์–ด? ๋ญ๋ผ๊ณ  ํ•ด์•ผ ํ•˜์ง€? ์ด ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌด์–ธ๊ฐ€๋ฅผ ๋Œ๋ ค์ฃผ์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹  ๊ทธ ๊ฒฐ๊ณผ๋กœ ์ธํ•ด ์ปดํ“จํ„ฐ๊ฐ€ ํ™”๋ฉด์„ ๋ณ€๊ฒฝํ•˜๋„๋ก ๋งŒ๋“ ๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด ์ด ํ•จ์ˆ˜๋Š” ํ”„๋กœ๊ทธ๋žจ ๋ฐ”๊นฅ์— ์žˆ๋Š” ์„ธ์ƒ์—์„œ ๋ฌด์–ธ๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ด๊ฒƒ์˜ ํƒ€์ž…์„ ๋ญ๋ผ๊ณ  ํ•ด์•ผ ํ• ๊นŒ? GHCi๊ฐ€ ๋ญ๋ผ๊ณ  ํ•˜๋Š”์ง€ ๋ณด์ž. Prelude> :t putStrLn putStrLn :: String -> IO () "IO"๋Š” "์ž…๋ ฅ(input)๊ณผ ์ถœ๋ ฅ(output)"์„ ๋œปํ•œ๋‹ค. ํƒ€์ž…์— IO๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด ํ”„๋กœ๊ทธ๋žจ ์™ธ๋ถ€ ์„ธ์ƒ๊ณผ์˜ ์ƒํ˜ธ์ž‘์šฉ์ด ์ˆ˜๋ฐ˜๋œ๋‹ค. ์•ž์œผ๋กœ ์ด๋Ÿฐ IO ๊ฐ’๋“ค์„ ์•ก์…˜์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ฒ ๋‹ค. IO ํƒ€์ž…์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„, ์ด ๊ฒฝ์šฐ ()๋Š”, ์•ก์…˜์˜ ๊ฒฐ๊ด๊ฐ’์˜ ํƒ€์ž…์ด๋‹ค. ์ฆ‰ ์•ก์…˜์ด (ํ”„๋กœ๊ทธ๋žจ ๋ฐ”๊นฅ์—์„œ ํ•˜๋Š” ์ผ์ด ์•„๋‹ˆ๋ผ) ํ”„๋กœ๊ทธ๋žจ์— ๋Œ๋ ค์ฃผ๋Š” ๋ฌด์–ธ๊ฐ€์˜ ํƒ€์ž…์ด๋‹ค. ()๋Š” ("์œ ๋‹›"์ด๋ผ ํ•จ) ํƒ€์ž…์€ ์˜ค์ง ํ•˜๋‚˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š”๋ฐ ๊ทธ ๊ฐ’๋„ ()๋ผ ๋ถ€๋ฅธ๋‹ค. (์‚ฌ์‹ค์ƒ ์›์†Œ๊ฐ€ ์—†๋Š” ํŠœํ”Œ์ด๋‹ค) putStrLn์€ ์„ธ์ƒ์— ์ถœ๋ ฅ์„ ๋ณด๋‚ด์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ์—๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋Œ๋ ค์ฃผ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ()๋Š” ๋‹จ์ˆœํžˆ ์ž๋ฆฌ๋ฅผ ์ฑ„์šฐ๋Š” ์šฉ๋„๋กœ ์“ฐ์ธ๋‹ค. IO ()๋Š” "()๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์•ก์…˜"์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ IO๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ๋“ค์ด๋‹ค. ํ™”๋ฉด์— ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค ํ‚ค๋ณด๋“œ์—์„œ ๋ฌธ์ž์—ด์„ ์ฝ์–ด์˜จ๋‹ค ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ด๋‹ค ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜จ๋‹ค IO๋ฅผ ์‹ค์ œ๋กœ ์ž‘๋™ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฑด ๋ฌด์—‡์ผ๊นŒ? putStrLn์—์„œ ํ™”๋ฉด ์† ํ”ฝ์…€๋กœ ๊ฐ€๊ธฐ๊นŒ์ง€ ๊ทธ ์ด๋ฉด์—์„œ๋Š” ๋งŽ์€ ์ผ์ด ์ผ์–ด๋‚œ๋‹ค. ํ•˜์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๊ทธ ๋””ํ…Œ์ผ์„ ์ดํ•ดํ•  ํ•„์š”๋Š” ์—†๋‹ค. ์™„์ „ํ•œ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์€ ์‚ฌ์‹ค ํ•˜๋‚˜์˜ ๊ฑฐ๋Œ€ํ•œ IO ์•ก์…˜์ด๋‹ค. ์ปดํŒŒ์ผ๋œ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ด ์•ก์…˜์˜ ์ด๋ฆ„์€ main์ด๊ณ  ๊ทธ ํƒ€์ž…์€ IO ()์ด๋‹ค. ์ด๋Ÿฐ ๊ด€์ ์—์„œ ๋ณด๋ฉด ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์•ก์…˜๋“ค๊ณผ ํ•จ์ˆ˜๋“ค์„ ๊ฒฐํ•ฉํ•˜์—ฌ ์ด์ฒด์ ์ธ ์•ก์…˜ main์„ ํ˜•์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ผœ๋ฉด main์ด ์‹คํ–‰๋œ๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์–ด๋–ป๊ฒŒ ์‹คํ–‰ํ• ์ง€๋ฅผ ์ปดํ“จํ„ฐ์—๊ฒŒ ์ง€์‹œํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ํƒ€์ž…์˜ ๊ธฐ์ดˆ ์žฅ์—์„œ openWindow ํ•จ์ˆ˜์˜ ํƒ€์ž…์€ ๊ฐ„์†Œํ™”๋œ ๊ฒƒ์ด๋ผ๊ณ  ํ–ˆ์—ˆ๋‹ค. ๊ทธ ์‹ค์ œ ํƒ€์ž…์ด ์–ด๋•Œ์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๊ฐ€? do๋ฅผ ํ†ตํ•œ ์•ก์…˜ ์—ฐ๊ณ„ do ํ‘œ๊ธฐ๋Š” ์•ก์…˜๋“ค์„ ๋ฌถ๋Š” ํŽธ๋ฆฌํ•œ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•œ๋‹ค. (do๋Š” ํ•˜์Šค์ผˆ์—์„œ ์œ ์šฉํ•œ ์ž‘์—…์„ ํ•˜๋ ค๋ฉด ํ•„์ˆ˜๋‹ค.) ๋‹ค์Œ ํ”„๋กœ๊ทธ๋žจ์„ ์‚ดํŽด๋ณด์ž. ์˜ˆ์‹œ: ์ด๋ฆ„์ด ๋ญ์˜ˆ์š”? main = do putStrLn "Please enter your name: " name <- getLine putStrLn ("Hello, " ++ name ++ ", how are you?") ์ž ๊น do ํ‘œ๊ธฐ๋Š” ์ง€๊ธˆ๊นŒ์ง€ ๋ด์˜จ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ์™€ ์•„์ฃผ ๋‹ฌ๋ผ ๋ณด์ด์ง€๋งŒ, ์‹ค์€ ์—ฌ๋Ÿฌ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ํŽธ์˜ ๋ฌธ๋ฒ•์ผ ๋ฟ์ด๋ฉฐ ๊ทธ์ค‘์—์„œ๋„ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ (>>=) ์—ฐ์‚ฐ์ž๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•˜๊ณ  ๊ทธ๋‹ค์Œ do ํ‘œ๊ธฐ๋ฅผ ์†Œ๊ฐœํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ถฉ๋ถ„ํ•œ ์„ค๋ช…์„ ํ•˜๋ ค๋ฉด ๊ทธ์— ์•ž์„œ ๋‹ค๋ฃฐ ์ฃผ์ œ๊ฐ€ ๋งŽ์ด ์žˆ๋‹ค. ์ง€๊ธˆ do๋กœ ๊ฑด๋„ˆ๋›ด ๊ฒƒ์€ ์‹ค์šฉ์ ์ธ ์ง€๋ฆ„๊ธธ๋กœ์„œ, IO๋ฅผ ํฌํ•จํ•œ ์™„์ „ํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ๋‹น์žฅ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ ๋‹ค. do๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€๋Š” ๋‚˜์ค‘์— ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ์žฅ์—์„œ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. do๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์ „์— getLine์„ ์‚ดํŽด๋ณด์ž. getLine์€ ๋ฐ”๊นฅ์„ธ์ƒ, ์ด ๊ฒฝ์šฐ ํ„ฐ๋ฏธ๋„๋กœ ๋‚˜๊ฐ€์„œ String์„ ํ•˜๋‚˜ ๊ฐ€์ ธ์˜จ๋‹ค. ๊ทธ ํƒ€์ž…์€ ๋ฌด์—‡์ผ๊นŒ? Prelude> :t getLine getLine :: IO String getLine์ด IO ์•ก์…˜์ด๊ณ , ์‹คํ–‰๋˜๋ฉด String์„ ํ•˜๋‚˜ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿผ ์ž…๋ ฅ์€? ํ•จ์ˆ˜๋Š” ์ธ์ž๋ฅผ ์ทจํ•ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฑธ ๋ฐ˜์˜ํ•˜๋Š” a -> b ์‹์˜ ํƒ€์ž…์„ ๊ฐ€์ง€๋Š”๋ฐ getLine์€ ์ธ์ž๋ฅผ ์ทจํ•˜์ง€ ์•Š๋Š”๋‹ค. getLine์€ ํ„ฐ๋ฏธ๋„์˜ ๋ฌด์—‡์ด๋“  ๊ทธ๊ฑธ ์ž…๋ ฅ์œผ๋กœ ์ทจํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ”๊นฅ์„ธ์ƒ์˜ ๊ทธ ์ค„์€ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ ์•ˆ์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ์ „์—๋Š” ๋ฌด์–ธ๊ฐ€ ํƒ€์ž… ์žˆ๊ณ  ๊ฒฐ์ •๋œ ๊ฐ’์ด ์•„๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์€ ์‹คํ–‰๋˜๊ธฐ ์ „์—๋Š” ๋ฐ”๊นฅ ์„ธ๊ณ„์˜ ์ƒํƒœ๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— IO ์•ก์…˜์˜ ์ •ํ™•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๋‹ค. ์ด๋Ÿฐ IO ์•ก์…˜๋“ค๊ณผ ํ”„๋กœ๊ทธ๋žจ์˜ ๋‹ค๋ฅธ ์ธก๋ฉด๋“ค์˜ ๊ด€๊ณ„๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์•ก์…˜๋“ค์€ ๋ฐ˜๋“œ์‹œ ์ฝ”๋“œ์—์„œ ๋ฏธ๋ฆฌ ์ •์˜๋œ, ์˜ˆ์ธก ๊ฐ€๋Šฅํ•œ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค. IO๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š” ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜๋“ค์˜ ๊ฒฝ์šฐ๋Š” ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ์žฅ์†Œ์— ๋„๋‹ฌํ•˜๊ธฐ๋งŒ ํ•œ๋‹ค๋ฉด ์ •ํ™•ํ•œ ์‹คํ–‰ ์ˆœ์„œ๊ฐ€ ๊ทธ๋ ‡๊ฒŒ ํฐ ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์ด๋ฆ„ ๋ฌป๊ธฐ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์šฐ๋ฆฌ๋Š” ์•ก์…˜ 3๊ฐœ๋ฅผ ์—ฐ๊ณ„ํ•˜๊ณ  ์žˆ๋‹ค. ์ธ์‚ฌ๋ง์„ ์ถœ๋ ฅํ•˜๋Š” putStrLn, ๊ทธ๋‹ค์Œ getLine, ๊ทธ๋ฆฌ๊ณ  ๋˜ ๋‹ค๋ฅธ putStrLn์ด๋‹ค. getLine์—์„œ ์‚ฌ์šฉํ•œ <- ํ‘œ๊ธฐ๋Š” ์•ก์…˜์˜ ๊ฒฐ๊ด๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ํ• ๋‹นํ•˜๋Š” ์ˆ˜๋‹จ์ด๋‹ค. ์ด ๋ณ€์ˆ˜๋Š” ๋‹ค๋ฅธ ๊ณณ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค(์ด ๊ฒฝ์šฐ ์ตœ์ข… ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ์„ ์ค€๋น„ํ•˜๊ธฐ ์œ„ํ•ด). ๋งˆ์ง€๋ง‰ ์•ก์…˜์€ putStrLn์˜ ๊ฒฐ๊ณผ์ด๋ฏ€๋กœ ์ „์ฒด ํ”„๋กœ๊ทธ๋žจ์˜ ํƒ€์ž…์€ IO ()์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ง๊ฐ์‚ผ๊ฐํ˜•์˜ ๋ฐ‘๋ณ€๊ณผ ๋†’์ด๋ฅผ ์š”์ฒญํ•ด์„œ ๊ทธ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•ด ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•œ๋‹ค. ์ƒํ˜ธ์ž‘์šฉ์€ ์ด๋Ÿฐ ์‹์ด์–ด์•ผ ํ•œ๋‹ค. The base? 3.3 The height? 5.4 The area of that triangle is 8.91 ํžŒํŠธ: read ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ "3.3" ๊ฐ™์€ ์‚ฌ์šฉ์ž ๋ฌธ์ž์—ด์„ ์ˆซ์ž 3.3์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  show ํ•จ์ˆ˜๋กœ ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์™ผ์ชฝ ํ™”์‚ดํ‘œ์— ๋Œ€ํ•œ ์„ค๋ช… getLine ๊ฐ™์€ ์•ก์…˜์€ ๊ฑฐ์˜ ํ•ญ์ƒ ๊ฐ’์„ ์–ป๋Š” ์šฉ๋„๋กœ ์“ฐ์ด์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ๋ฐ˜๋“œ์‹œ ๊ทธ ๊ฐ’์„ ํฌ์ฐฉํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด๋ ‡๊ฒŒ ์“ฐ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ˆ: getLine ์ง์ ‘ ์‹คํ–‰ํ•˜๊ธฐ main = do putStrLn "Please enter your name: " getLine putStrLn ("Hello, how are you?") ์ด ๊ฒฝ์šฐ ์ž…๋ ฅ์„ ์ „ํ˜€ ํ™œ์šฉํ•˜์ง€ ์•Š์ง€๋งŒ ์œ ์ €์—๊ฒŒ ์ด๋ฆ„์„ ์ž…๋ ฅํ•  ์ˆ˜๋Š” ์žˆ๊ฒŒ ๋งŒ๋“ ๋‹ค. <-๋ฅผ ์ƒ๋žตํ•˜๋ฉด ์•ก์…˜์€ ๋ฐœ์ƒํ•˜์ง€๋งŒ ๊ทธ ๋ฐ์ดํ„ฐ๋Š” ์–ด๋””์—๋„ ์ €์žฅ๋˜์ง€ ์•Š๊ณ  ์ด ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ ‘๊ทผํ•  ์ˆ˜๋„ ์—†๋‹ค. <-๋Š” ๋งˆ์ง€๋ง‰ ์•ก์…˜์„ ์ œ์™ธํ•˜๊ณ  ์–ด๋–ค ์•ก์…˜์—๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค ์–ด๋–ค ์•ก์…˜๋“ค๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ์–ป์–ด์˜ค๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ์ง€์— ๊ด€ํ•œ ์ œ์•ฝ์€ ๊ฑฐ์˜ ์—†๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” ๊ฐ ์•ก์…˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ๋„ฃ๋Š”๋‹ค. (๋งˆ์ง€๋ง‰์€ ์ œ์™ธํ•˜๊ณ ... ๋‚˜์ค‘์— ์•Œ์•„๋ณด์ž) ์˜ˆ: ๋ชจ๋“  ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ๋„ฃ๊ธฐ main = do x <- putStrLn "Please enter your name: " name <- getLine putStrLn ("Hello, " ++ name ++ ", how are you?") ๋ณ€์ˆ˜ x๋Š” ์•ก์…˜์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ์–ป์–ด์˜ค์ง€๋งŒ ์ด ๊ฒฝ์šฐ์—๋Š” ๋ณ„ ์“ธ๋ชจ๊ฐ€ ์—†๋‹ค. ์ด ์•ก์…˜์€ ๋‹จ์œ„ ๊ฐ’ ()๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฆ‰ ๊ธฐ์ˆ ์ ์œผ๋กœ๋Š” ์–ด๋–ค ์•ก์…˜์—์„œ๋„ ๊ฐ’์„ ๋ฝ‘์•„์˜ฌ ์ˆ˜ ์žˆ์ง€๋งŒ ํ•ญ์ƒ ๊ทธ๋Ÿด ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š” ๊ฑด ์•„๋‹ˆ๋‹ค. ์™œ ๋งˆ์ง€๋ง‰ ์•ก์…˜์—์„œ๋Š” ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์—†์„๊นŒ? ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ๋ณด์ž. ์˜ˆ: ๋งˆ์ง€๋ง‰ ์•ก์…˜์—์„œ ๊ฐ’ ์–ป์–ด์˜ค๊ธฐ main = do x <- putStrLn "Please enter your name: " name <- getLine y <- putStrLn ("Hello, " ++ name ++ ", how are you?") ์œฝ! ์˜ค๋ฅ˜๋‹ค! HaskellWikibook.hs:5:2: The last statement in a 'do' construct must be an expression ์ด๊ฑธ ์ดํ•ดํ•˜๋ ค๋ฉด ์ง€๊ธˆ๋ณด๋‹ค ํ•˜์Šค์ผˆ์„ ๊นŠ๊ฒŒ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค. ๋Œ€๋žต ๋งํ•˜์ž๋ฉด <-๋ฅผ ์จ์„œ ์•ก์…˜์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ์–ป์„ ๋•Œ, ํ•˜์Šค์ผˆ์€ ๋‹ค๋ฅธ ์•ก์…˜์ด ๋’ค์— ์˜ฌ ๊ฒƒ์ด๋ผ๊ณ  ๊ธฐ๋Œ€ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์•ก์…˜์€ <-๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค. ์•ก์…˜ ์ œ์–ดํ•˜๊ธฐ if/then/else ๊ฐ™์€ ์ผ๋ฐ˜์ ์ธ ํ•˜์Šค ์ผˆ ์š”์†Œ๋ฅผ do ํ‘œ๊ธฐ ๋‚ด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ฃผ์˜์‚ฌํ•ญ์ด ์กฐ๊ธˆ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ "์ˆซ์ž ์•Œ์•„๋งžํžˆ๊ธฐ" ํ”„๋กœ๊ทธ๋žจ์—์„œ, doGuessing num = do putStrLn "Enter your guess:" guess <- getLine if (read guess) < num then do putStrLn "Too low!" doGuessing num else if (read guess) > num then do putStrLn "Too high!" doGuessing num else putStrLn "You Win!" if/then/else๋Š” ์กฐ๊ฑด์‹, "then" ๋ถ„๊ธฐ, "else" ๋ถ„๊ธฐ ์ด๋ ‡๊ฒŒ ์ธ์ž๋ฅผ ์„ธ ๊ฐœ ๋ฐ›๋Š”๋‹ค. ์กฐ๊ฑด์‹์˜ ํƒ€์ž…์€ Bool์ด์–ด์•ผ ํ•˜๊ณ , ๋‘ ๋ถ„๊ธฐ๋Š” ํƒ€์ž…๋งŒ ๊ฐ™์œผ๋ฉด ์–ด๋Š ํƒ€์ž…์ด๋“  ๊ฐ€๋Šฅํ•˜๋‹ค. if/then/else ์ „์ฒด์˜ ํƒ€์ž…์€ ๋‘ ๋ถ„๊ธฐ์˜ ํƒ€์ž…์ด๋‹ค. ๊ฐ€์žฅ ๋ฐ”๊นฅ์˜ ๋น„๊ต์—์„œ๋Š” (read guess) < num์ด ์กฐ๊ฑด์‹์ด๋‹ค. ํ™•์‹คํžˆ ์˜ฌ๋ฐ”๋ฅธ ํƒ€์ž…์ด๋‹ค. ์ด๋ฒˆ์—” "then" ๋ถ„๊ธฐ๋ฅผ ๋ณด์ž. do putStrLn "Too low!" doGuessing num ์—ฌ๊ธฐ์„  ๋‘ ์•ก์…˜ putStrLn๊ณผ doGuessing์„ ์—ฐ๊ณ„ํ•œ๋‹ค. ์ฒซ ์•ก์…˜์˜ ํƒ€์ž…์€ IO ()์ด๋ฏ€๋กœ ๊ดœ์ฐฎ๋‹ค. ๋‘ ๋ฒˆ์งธ๋„ IO () ํƒ€์ž…์œผ๋กœ ๊ดœ์ฐฎ๋‹ค. ์ „์ฒด ๊ณ„์‚ฐ์˜ ๊ฒฐ๊ณผ ํƒ€์ž…์€ ๋ฐ”๋กœ ๋งˆ์ง€๋ง‰ ๊ณ„์‚ฐ์˜ ํƒ€์ž…์ด๋‹ค. ๋”ฐ๋ผ์„œ "then" ๋ถ„๊ธฐ์˜ ํƒ€์ž…๋„ IO ()์ด๋‹ค. ๋น„์Šทํ•œ ๋งฅ๋ฝ์—์„œ "else" ๋ถ„๊ธฐ์˜ ํƒ€์ž…๋„ IO ()๋‹ค. ๋”ฐ๋ผ์„œ if/then/else ์ „์ฒด์˜ ํƒ€์ž…๋„ IO ()์ด๋‹ค. ๋ฐ”๋กœ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•œ ๊ฒƒ์ด๋‹ค. ์ž ๊น: "๋‚œ ์ด๋ฏธ do ๋ธ”๋ก์„ ์‹œ์ž‘ํ–ˆ์œผ๋‹ˆ ๋˜ ๋‹ค๋ฅธ do๋Š” ํ•„์š” ์—†๊ฒ ์ง€."๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์กฐ์‹ฌํ•˜์ž. ์ด๋ ‡๊ฒŒ ์ฝ”๋”ฉํ•  ์ˆ˜๋Š” ์—†๋‹ค. do if (read guess) < num then putStrLn "Too low!" doGuessing num else ... ์—ฌ๊ธฐ์„  do๋ฅผ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ๋Ÿฌ๋Š” putStrLn๊ณผ doGuessing ํ˜ธ์ถœ์ด ์—ฐ๊ณ„๋˜์–ด์•ผ ํ•จ์„ ์•Œ์ง€ ๋ชปํ•˜๊ณ , putStrLn์„ ์ธ์ž ์„ธ ๊ฐœ๋ฅผ ๊ฐ€์ง€๊ณ  ํ˜ธ์ถœํ•˜๋ ค ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ ์ธ์ž๋“ค์ด๋ž€ ๋ฐ”๋กœ ๋ฌธ์ž์—ด, doGuessing ํ•จ์ˆ˜, ์ •์ˆ˜ num์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ด ํ”„๋กœ๊ทธ๋žจ์„ ๊ฑฐ๋ถ€ํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ด๋ฆ„์„ ๋ฌป๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•œ๋‹ค. ๊ทธ ์ด๋ฆ„์ด Simon, John, Phil ์ค‘ ํ•˜๋‚˜๋ผ๋ฉด '๋‚˜๋Š” ํ•˜์Šค์ผˆ์ด ํ›Œ๋ฅญํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ผ๊ณ  ์ƒ๊ฐํ•ด'๋ผ๊ณ  ์‚ฌ์šฉ์ž์—๊ฒŒ ๋งํ•œ๋‹ค. ์ด๋ฆ„์ด Koen ์ด๋ฉด 'ํ•˜์Šค์ผˆ์„ ๋””๋ฒ„๊น…ํ•˜๋Š” ๊ฒƒ์€ ์žฌ๋ฐŒ์–ด'๋ผ๊ณ  ๋งํ•œ๋‹ค. (Koen Classen์€ ํ•˜์Šค ์ผˆ ๋””๋ฒ„๊น…์„ ์ž‘์—…ํ•˜๋Š” ์‚ฌ๋žŒ ์ค‘ ํ•˜๋‚˜๋‹ค) ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ ๋‹น์‹ ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค๊ณ  ๋งํ•œ๋‹ค. (๋ฌธ๋ฒ•์ƒ ์ด ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ์ตœ์†Œํ•œ if / then / else๋ฅผ ์“ฐ๋Š” ๋ฒ„์ „์€ ์ž‘์„ฑํ•˜์ž) ์•ก์…˜ ํ•ด๋ถ€ํ•˜๊ธฐ(Actions under the microscope) ์ง€๊ธˆ์€ ์•ก์…˜์ด ์‰ฌ์›Œ ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ์‚ฌ์‹ค ์•ก์…˜์€ ํ•˜์Šค ์ผˆ ์ž…๋ฌธ์ž๋“ค์˜ ํ”ํ•œ ์ง„์ž…์žฅ๋ฒฝ์ด๋‹ค. ์•ก์…˜์„ ๋‹ค๋ฃจ๋‹ค ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋ฉด ๋ฐ‘์— ์ œ์‹œํ•œ ์‚ฌ๋ก€๋“ค ์ค‘ ํ•˜๋‚˜์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž. ์ง€๊ธˆ์€ ์ด ์ ˆ์„ ๊ฑด๋„ˆ๋›ฐ๊ณ  ์‹ค์ œ๋กœ ๋ฌธ์ œ๋ฅผ ๊ฒช์—ˆ์„ ๋•Œ ๋Œ์•„์˜ค๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. ์•ก์…˜์˜ ํƒ€์ž…์„ ์—ผ๋‘์— ๋‘”๋‹ค ์ด๋ฆ„์„ ์–ป์–ด ๋‹ค์‹œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ์€ ์œ ํ˜น์ด ๋“ค ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค. ๋‹ค์Œ์€ ์„ฑ๊ณต์ ์ด์ง€ ์•Š์€ ์‹œ๋„๋‹ค. ์˜ˆ์‹œ: ์™œ ์•ˆ ๋˜์ง€? main = do putStrLn "What is your name? " putStrLn ("Hello " ++ getLine) ์ด๊ฑด ๋ฌด์Šจ ์˜ค๋ฅ˜์ผ๊นŒ? HaskellWikiBook.hs:3:26: Couldn't match expected type `[Char]' against inferred type `IO String' ์œ„์˜ ์˜ˆ์ œ๋ฅผ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด๋ณด์ž. ์ด ํ”„๋กœ๊ทธ๋žจ์ด ์ปดํŒŒ์ผ๋ ๊นŒ? ์˜ˆ์‹œ: ์ด๊ฒƒ๋„ ์•ˆ ๋œ๋‹ค main = do putStrLn getLine ํ•„์š”์น˜ ์•Š์€ "What is your name"๊ณผ ์ •์ค‘ํ•œ "Hello"๋งŒ ๋นผ๋ฉด ๊ฑฐ์˜ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. ์ด๊ฒƒ์„ ์ดํ•ดํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ํƒ€์ž…์„ ๋”ฐ์ ธ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ๋Œ€์กฐํ•ด ๋ณด์ž. putStrLn :: String -> IO () getLine :: IO String ํƒ€์ž…์˜ ๊ธฐ์ดˆ์—์„œ ๋ฐฐ์šด ์‚ฌ๊ณ ๋ฐฉ์‹์„ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•˜๋ฉด ์™œ ํ‹€๋ ธ๋Š”์ง€๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. putStrLn์€ ์ž…๋ ฅ์œผ๋กœ์„œ String์„ ๊ธฐ๋Œ€ํ•œ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์€ String์ด ์•„๋‹ˆ๋ผ ๋ฏธ๋ฌ˜ํ•˜๊ฒŒ ๋น„์Šทํ•œ IO String๋‹ค. IO String์€ ์‹คํ–‰๋˜๋ฉด String์„ ๋Œ๋ ค์ฃผ๋Š” ์•ก์…˜์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. putStrLn์ด ์›ํ•˜๋Š” String์„ ์–ป์œผ๋ ค๋ฉด, ์•ก์…˜์„ ์‹คํ–‰ํ•œ ๋‹ค์Œ ์™ผ์ชฝ ํ™”์‚ดํ‘œ <-๋ฅผ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ: ์ด์ œ ๋œ๋‹ค main = do name <- getLine putStrLn name ์•ž์„œ์˜ ๋ณต์žกํ•œ ์˜ˆ์ œ๋กœ ๋Œ์•„๊ฐ€๋ฉด main = do putStrLn "What is your name? " name <- getLine putStrLn ("Hello " ++ name) ํ‘œํ˜„์‹ ํƒ€์ž…๋„ ๋”ฐ์ ธ๋ณธ๋‹ค ์šฐ๋ฆฌ๋Š” ์•ก์…˜์„ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š” ์ƒํ™ฉ์—์„  ๊ทธ ์•ก์…˜์„ ํ™œ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์ค‘์š”ํ•œ ๋ฐœ์ƒ์„ ์–ป์—ˆ๋‹ค. ๊ทธ ๋ฐ˜๋Œ€๋กœ, ์•ก์…˜์ด ์˜ˆ์ƒ๋˜๋Š” ์ƒํ™ฉ์—์„œ ์•ก์…˜์ด ์•„๋‹Œ ๊ฒƒ์„ ์ด์šฉํ•  ์ˆ˜๋Š” ์—†๋‹ค. ์‚ฌ์šฉ์ž๋ฅผ ํ™˜์˜ํ•˜๋ ค ํ•˜๋Š”๋ฐ ์ด๋ฒˆ์—๋Š” ๋งŒ๋‚˜๋ ค๋‹ˆ ๋ฌด์ฒ™ ๋“ค๋– ์„œ ์ด๋ฆ„์„ ํฌ๊ฒŒ ์™ธ์น˜๋ ค๊ณ  ํ•œ๋‹ค. ์˜ˆ์‹œ: ํฅ๋ฏธ๋กญ์ง€๋งŒ ํ‹€๋ ธ๋‹ค. ์™œ? import Data.Char (toUpper) main = do name <- getLine loudName <- makeLoud name putStrLn ("Hello " ++ loudName ++ "!") putStrLn ("Oh boy! Am I excited to meet you, " ++ loudName) -- Don't worry too much about this function; it just capitalises a String makeLoud :: String -> String makeLoud s = map toUpper s ์ด๋ ‡๊ฒŒ ํ‹€๋ ธ๋‹ค... Couldn't match expected type `IO' against inferred type `[]' Expected type: IO t Inferred type: String In a 'do' expression: loudName <- makeLoud name ์œ„์—์„œ ๋ดค๋˜ ๋ฌธ์ œ์™€ ๋น„์Šทํ•˜๋‹ค. IO ํƒ€์ž…์ด ์˜ˆ์ƒ๋˜๋Š” ๋ฌด์–ธ๊ฐ€์™€ ๊ทธ๋Ÿฐ ๊ฑธ ๋‚ด๋†“์ง€ ์•Š๋Š” ๋ฌด์–ธ๊ฐ€ ์‚ฌ์ด์— ๋ถˆ์ผ์น˜๊ฐ€ ์žˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์™ผ์ชฝ ํ™”์‚ดํ‘œ <-์˜ ์“ฐ์ž„์ƒˆ๊ฐ€ ๋ฌธ์ œ๋‹ค. makeLoud name์˜ ๊ฐ’์„ ์–ป์œผ๋ ค๊ณ  ํ•˜๋Š”๋ฐ, <-์˜ ์žฌ๋ฃŒ๋Š” ์•„๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์•ž์˜ ์ ˆ์—์„œ ๋ณธ ๋ถˆ์ผ์น˜์™€ ๊ฐ™์€ ๊ฒƒ์ธ๋ฐ ์ด๋ฒˆ์—๋Š” ์ •๊ทœ String์„ IO String์œผ๋กœ ์“ฐ๋ ค๊ณ  ํ–ˆ๋‹ค๋Š” ๊ฒƒ์ด ๋‹ค๋ฅด๋‹ค. ๋ถ„๋ช…ํžˆ ์ด ๋‘˜์€ ๋‹ค๋ฅธ ๊ฒƒ์ด๋‹ค. ํ›„์ž๋Š” ์‹คํ–‰๋  ์•ก์…˜์ด๊ณ  ์ „์ž๋Š” ์ž๊ธฐ ์ผ์—๋‚˜ ์‹ ๊ฒฝ ์“ฐ๋Š” ํ‘œํ˜„์‹์ด๋‹ค. ๋‹จ์ˆœํžˆ loudName = makeLoud name์ด๋ผ๊ณ  ์“ธ ์ˆ˜๋Š” ์—†๋Š”๋ฐ do๋Š” ์•ก์…˜๋“ค์„ ์—ฐ๊ณ„ํ•˜๊ณ  loudName = makeLoud name์€ ์•ก์…˜์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿผ ์ด ๊ณจ์นซ๋ฉ์–ด๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ํ• ๊นŒ? ์—ฌ๋Ÿฌ ์„ ํƒ์ง€๊ฐ€ ์žˆ๋‹ค. makeLoud๋ฅผ ์•ก์…˜์œผ๋กœ ์ „ํ™˜์‹œ์ผœ IO String์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ฌด ์ด์œ  ์—†์ด ์•ก์…˜์ด ์„ธ์ƒ์œผ๋กœ ๋‚˜๊ฐ€๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ์ง€๋Š” ์•Š๋‹ค. ์šฐ๋ฆฌ์˜ ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ๋Š” ๋ชจ๋“  ๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ๋Œ์•„๊ฐ€๋Š”์ง€ ๋ฏฟ์„ ๋งŒํ•˜๊ฒŒ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋‹ค. ์•ก์…˜์ด ๋ฐ”๊นฅ์„ธ์ƒ๊ณผ ์—ฐ๊ด€๋˜๋ฉด ์šฐ๋ฆฌ์˜ ๊ฒฐ๊ด๊ฐ’์€ ๋œ ์˜ˆ์ธก์ ์ด๊ฒŒ ๋œ๋‹ค. IO makeLoud๋ฅผ ์–ด๋–ป๊ฒŒ๋“  ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๊ฑด ๊ธธ์„ ์ž˜๋ชป ๋“ค์–ด์„  ๊ฒƒ์ด๋‹ค. ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. makeLoud๋ฅผ, IO๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํ•จ์ˆ˜์—์„œ ์“ฐ๋ ค๊ณ  ํ•œ๋‹ค๋ฉด? IO ์•ก์…˜์ด ์ •๋ง๋กœ ํ•„์š”ํ•œ ๋•Œ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ์–ด์šธ๋ฆฌ๊ณ  ์‹ถ์ง€ ์•Š๋‹ค. ๋Œ€๋ฌธ์ž ์ด๋ฆ„์„ ์•ก์…˜์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด return์ด๋ผ๋Š” ํŠน์ˆ˜ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด loudName <- return (makeLoud name)์ด๋ผ ์“ธ ์ˆ˜ ์žˆ๋‹ค. makeLoud ํ•จ์ˆ˜ ์ž์ฒด๋Š” ํ›Œ๋ฅญํ•˜๊ฒŒ ๋†”๋‘๊ณ  IO๋กœ๋ถ€ํ„ฐ ์ž์œ ๋กœ์šด ๋™์‹œ์— IO์™€ ํ˜ธํ™˜๋œ๋‹ค๋Š” ์ ์—์„œ ์ข€ ๋‚ซ๋‹ค. ๊ทธ๋ž˜๋„ ์ข€ ํˆฌ๋ฐ•ํ•œ๋ฐ, ๋งฅ๋น ์ง€๊ฒŒ ๋ญ”๊ฐ€๋ฅผ ๋ฐ˜ํ™˜๋งŒ ํ•˜๊ณ ์„œ ๊ทธ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ€๋Š” ์•ก์…˜์ด ์ˆ˜ํ–‰๋˜๋Š” ๊ผด์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ์–ผ๋งˆ๋‚˜ ํฅ๋ฏธ์ง„์ง„ํ•œ๊ฐ€! (์ž ๊น: return์˜ ์ ์ ˆํ•œ ์‚ฌ์šฉ๋ฒ•์€ ๋‚˜์ค‘์— ๋ฐฐ์šธ ๊ฒƒ์ด๋‹ค) ์•„๋‹ˆ๋ฉด let ๋ฐ”์ธ๋”ฉ์„ ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค... ์‚ฌ์‹ค ํ•˜์Šค์ผˆ์—๋Š” ์•ก์…˜ ์•ˆ์—์„œ์˜ let ๋ฐ”์ธ๋”ฉ์„ ์œ„ํ•œ ํŠน๋ณ„ ํŽธ์˜ ๋ฌธ๋ฒ•์ด ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์ด๋‹ค. ์˜ˆ: do ๋ธ”๋ก ๋‚ด์—์„œ์˜ let ๋ฐ”์ธ๋”ฉ main = do name <- getLine let loudName = makeLoud name putStrLn ("Hello " ++ loudName ++ "!") putStrLn ("Oh boy! Am I excited to meet you, " ++ loudName) ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ด๋ฉด ์œ„์˜ let ๋ฐ”์ธ๋”ฉ์—๋Š” in์ด ๋น ์กŒ๋‹ค๋Š” ๊ฑธ ์•Œ์•„์ฑŒ ์ˆ˜ ์žˆ๋‹ค. do ๋ธ”๋ก ๋‚ด์˜ let ๋ฐ”์ธ๋”ฉ์€ in ํ‚ค์›Œ๋“œ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. ๋ฌผ๋ก  in์„ ์“ธ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ง€์ €๋ถ„ํ•œ ์—ฌ๋ถ„์˜ do ๋ธ”๋ก์„ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค. ๋„์›€์ด ๋ ๋Š”์ง€ ๋ชฐ๋ผ๋„ ๋‹ค์Œ์˜ ๋‘ ์ฝ”๋“œ๋Š” ๋™๋“ฑํ•˜๋‹ค. ํŽธํ•จ ๋ถˆํŽธํ•จ do name <- getLine let loudName = makeLoud name putStrLn ("Hello " ++ loudName ++ "!") putStrLn ( "Oh boy! Am I excited to meet you, " ++ loudName) do name <- getLine let loudName = makeLoud name in do putStrLn ("Hello " ++ loudName ++ "!") putStrLn ( "Oh boy! Am I excited to meet you, " ++ loudName) ์—ฐ์Šต๋ฌธ์ œ let ๋ฐ”์ธ๋”ฉ์˜ ๋ถˆํŽธํ•œ ๋ฒ„์ „์—์„  ์™œ ์—ฌ๋ถ„์˜ do ํ‚ค์›Œ๋“œ๊ฐ€ ํ•„์š”ํ• ๊นŒ? ํ•ญ์ƒ ์—ฌ๋ถ„์˜ do๊ฐ€ ํ•„์š”ํ•œ ๊ฑธ๊นŒ? (์ถ”๊ฐ€ ์ ์ˆ˜) ์ˆ˜์ƒํ•˜๊ฒŒ๋„ in ์—†๋Š” let์€ ์šฐ๋ฆฌ๊ฐ€ ์ฑ…์˜ ์ดˆ๋ฐ˜๋ถ€์—์„œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์ผ๋˜ ๋ฐ”๋กœ ๊ทธ๊ฒƒ์ด๋‹ค. ์™œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„  in ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ๋žตํ•ด๋„ ๊ดœ์ฐฎ์€๋ฐ ์†Œ์Šค ํŒŒ์ผ์—์„  in์„ ๋„ฃ์–ด์•ผ ํ•˜๋Š” ๊ฑธ๊นŒ? ๋” ์ฝ์„๊ฑฐ๋ฆฌ ์ด์ œ ๋” ํ™”๋ คํ•œ ์ž…์ถœ๋ ฅ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ๊ธฐ๋ฐ˜์„ ๊ฐ–์ถ”์—ˆ๋‹ค. ๋‹ค์Œ์€ ์ด ์ฑ…์˜ ์ฃผ ๊ณผ๋ชฉ๋“ค๊ณผ ๋ณ‘ํ–‰ํ•ด์„œ ๋ณผ ๋งŒํ•œ IO ๊ด€๋ จ ์ฃผ์ œ๋“ค์ด๋‹ค. ๊ณผ๋ชฉ๋“ค์„ ์ฐจ๋ก€๋Œ€๋กœ ์ง„ํ–‰ํ•ด์„œ ํƒ€์ž…์— ๊ด€ํ•ด ๋” ๋ฐฐ์šฐ๊ณ  ์ตœ์ข…์ ์œผ๋กœ ๋ชจ๋‚˜๋“œ์— ๋„๋‹ฌํ•œ๋‹ค. ๋˜๋Š”, GUI ์žฅ์—์„œ ๊ทธ๋ž˜ํ”ฝ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๋ฐฐ์šฐ๊ธฐ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. IO ๊ด€๋ จ ๊ธฐ๋Šฅ๋“ค์„ ๋” ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด System.IO ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ณต๋ถ€๋ฅผ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. 2 ํ•˜์Šค ์ผˆ ์ดˆ๊ธ‰ ํ•˜์Šค ์ผˆ ์ดˆ๊ธ‰ ์žฌ๊ท€(Recursion) ๋ฆฌ์ŠคํŠธ ๋ณด์ถฉ ์„ค๋ช…(More about lists) ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ(List processing) ํƒ€์ž… ์„ ์–ธ(Type declarations) ํŒจํ„ด ๋งค์นญ(Pattern matching) ์ œ์–ด ๊ตฌ์กฐ(Control structures) ํ•จ์ˆ˜ ๋ณด์ถฉ ์„ค๋ช…(More on functions) ๊ณ ์ฐจ ํ•จ์ˆ˜(Higher order functions) GHCi ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ(Using GHCi effectively) 1 ์žฌ๊ท€ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Recursion ์ˆ˜์น˜ ์žฌ๊ท€ ๊ณ„์Šน ํ•จ์ˆ˜ ๋ฃจํ”„, ์žฌ๊ท€, ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ˆ„์  ๋‹ค๋ฅธ ์žฌ๊ท€ ํ•จ์ˆ˜๋“ค ๋ฆฌ์ŠคํŠธ ๊ธฐ๋ฐ˜ ์žฌ๊ท€ ์žฌ๊ท€์— ๋„ˆ๋ฌด ๋“ค๋œจ์ง€ ๋ง์ž... ์žฌ๊ท€ ํ•จ์ˆ˜๋Š” ํ•˜์Šค์ผˆ์—์„œ ์ค‘์ถ”์ ์ธ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ปดํ“จํ„ฐ ๊ณผํ•™๊ณผ ์ˆ˜ํ•™ ์ „๋ฐ˜์— ๊ฑธ์ณ ๋ฒ”์šฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์žฌ๊ท€๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ˜๋ณต์˜ ํ•œ ํ˜•ํƒœ์ธ๋ฐ ์šฐ๋ฆฌ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žฌ๊ท€์ ์ด๋ผ๋Š” ๊ฒƒ์˜ ์˜๋ฏธ์™€ ๊ทธ ๋™์ž‘์„ ๋ถ„๋ฆฌํ•ด์„œ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์žฌ๊ท€ ํ•จ์ˆ˜์˜ ์˜๋ฏธ๋Š” ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋œปํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋™์ž‘ ๋ฉด์—์„œ ์žฌ๊ท€ ํ•จ์ˆ˜๋Š” if/else/then ํ‘œํ˜„์‹ ๋˜๋Š” ํŒจํ„ด ๋งค์นญ ๊ฐ™์€ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ๋งŒ ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์—๋Š” ์žฌ๊ท€๋ฅผ ์ข…๋ฃŒํ•˜๋Š” ๊ธฐ๋ณธ ๊ฐ€์ •(base case)์ด ์ ์–ด๋„ ํ•˜๋‚˜ ์žˆ๊ณ  ํ•จ์ˆ˜๊ฐ€ ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋„๋ก ํ•˜์—ฌ ๋ฃจํ”„๋ฅผ ๋งŒ๋“œ๋Š” ์žฌ๊ท€ ๊ฐ€์ •(recursive case)์ด ํฌํ•จ๋œ๋‹ค. ์ข…๋ฃŒ ์กฐ๊ฑด์ด ์—†์œผ๋ฉด ์žฌ๊ท€ ํ•จ์ˆ˜๋Š” ์˜์›ํžˆ ๋ฃจํ”„๋ฅผ ๋Œ์•„์„œ ๋ฌดํ•œ ํšŒ๊ท€๋ฅผ ์ผ์œผํ‚จ๋‹ค. ์ˆ˜์น˜ ์žฌ๊ท€ ๊ณ„์Šน ํ•จ์ˆ˜ ์ˆ˜ํ•™, ํŠนํžˆ ์กฐํ•ฉ๋ก ์—๋Š” ๊ณ„์Šน(factorial)์ด๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.1 ์ด ํ•จ์ˆ˜๋Š” ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜ ํ•˜๋‚˜๋ฅผ ์ธ์ž๋กœ ์ทจํ•ด "n" ์ดํ•˜์˜ ๋ชจ๋“  ์–‘์˜ ์ •์ˆ˜๋ฅผ ์ฐพ์•„ ๊ณฑํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 6์˜ ๊ณ„์Šน(6!์œผ๋กœ ํ‘œ๊ธฐ)์€ 1 ร— 2 ร— 3 ร— 4 ร— 5 ร— 6 = 720์ด๋‹ค. ์ด๊ฒƒ์„ ํ•˜์Šค์ผˆ์—์„œ ์žฌ๊ท€ ์Šคํƒ€์ผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ธ์ ‘ํ•œ ๋‘ ์ˆ˜์˜ ๊ณ„์Šน์„ ์‚ดํŽด๋ณด์ž. ์˜ˆ: ์—ฐ์†๋œ ์ˆ˜๋“ค์˜ ๊ณ„์Šน Factorial of 6 = 6 ร— 5 ร— 4 ร— 3 ร— 2 ร— 1 Factorial of 5 = 5 ร— 4 ร— 3 ร— 2 ร— 1 ์ˆซ์ž๋“ค์„ ์–ด๋–ป๊ฒŒ ์ •๋ ฌํ–ˆ๋Š”์ง€์— ์ฃผ๋ชฉํ•˜์ž. ์—ฌ๊ธฐ์„œ 6! ์ด 5!๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์‹ค 6! ์€ ๋‹จ์ง€ 6 ร— 5!์ด๋‹ค. ๊ณ„์†ํ•ด ๋ณด์ž. ์˜ˆ: ์—ฐ์†๋œ ์ˆ˜๋“ค์˜ ๊ณ„์Šน Factorial of 4 = 4 ร— 3 ร— 2 ร— 1 Factorial of 3 = 3 ร— 2 ร— 1 Factorial of 2 = 2 ร— 1 Factorial of 1 = 1 ์–ด๋–ค ์ˆ˜์˜ ๊ณ„์Šน์€ ๋ฐ”๋กœ ๊ทธ ์ˆ˜์™€ ๊ทธ ์ˆ˜๋ณด๋‹ค ํ•˜๋‚˜ ์ž‘์€ ์ˆ˜์˜ ๊ณ„์Šน์„ ๊ณฑํ•œ ๊ฒƒ์ด๋‹ค. ์˜ˆ์™ธ๊ฐ€ ํ•˜๋‚˜ ์žˆ๋‹ค. 0์˜ ๊ณ„์Šน์€ 0์— -1์˜ ๊ณ„์Šน์„ ๊ณฑํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค(๊ณ„์Šน์€ ์–‘์ˆ˜๋งŒ์„ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค). ์‚ฌ์‹ค 0์˜ ๊ณ„์Šน์€ 1์ด๋‹ค(๊ทธ๋ ‡๊ฒŒ ์ •ํ•œ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ƒฅ ๊ทธ๋ ‡๋‹ค๊ณ  ์น˜์ž. 2) ๋”ฐ๋ผ์„œ 0์€ ์ด ์žฌ๊ท€์˜ ๊ธฐ๋ณธ ๊ฐ€์ •์ด๋‹ค. 0์— ๋‹ค๋‹ค๋ฅด๋ฉด ์žฌ๊ท€ ์—†์ด ๋‹ต์ด 1์ด๋ผ๊ณ  ๋ฐ”๋กœ ๋‹ตํ•œ๋‹ค. ๊ณ„์Šน ํ•จ์ˆ˜์˜ ์ •์˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์š”์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค. 0์˜ ๊ณ„์Šน์€ 1์ด๋‹ค. ๋‹ค๋ฅธ ์ž„์˜์˜ ์ˆ˜์˜ ๊ณ„์Šน์€ ๊ทธ ์ˆ˜์— ํ•˜๋‚˜ ์ ์€ ์ˆ˜์˜ ๊ณ„์Šน์„ ๊ณฑํ•œ ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์„ ๊ทธ๋Œ€๋กœ ํ•˜์Šค ์ผˆ๋กœ ๋ฒˆ์—ญํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: ๊ณ„์Šน ํ•จ์ˆ˜ factorial 0 = 1 factorial n = n * factorial (n-1) ์ด ์ฝ”๋“œ๋Š” factorial์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„์€ 0์˜ ๊ณ„์Šน์ด 1์ด๊ณ , ๋‘ ๋ฒˆ์งธ ์ค„์€ ๋‹ค๋ฅธ ์ž„์˜์˜ ์ˆ˜ n์˜ ๊ณ„์Šน์ด n ๊ณฑํ•˜๊ธฐ n-1์˜ ๊ณ„์Šน๊ณผ ๊ฐ™๋‹ค๊ณ  ์„ ์–ธํ•œ๋‹ค. n-1์„ ๊ฐ์‹ผ ๊ด„ํ˜ธ์— ์ฃผ๋ชฉํ•˜๋ผ. ๊ด„ํ˜ธ๊ฐ€ ์—†์—ˆ์œผ๋ฉด (factorial n) - 1๋กœ ํ•ด์„๋˜์—ˆ์„ ๊ฒƒ์ด๋‹ค. ํ•จ์ˆ˜ ์ ์šฉ(ํ•จ์ˆ˜๋ฅผ ์–ด๋–ค ๊ฐ’์— ์ ์šฉํ•˜๋Š” ๊ฒƒ)์€ ๊ทธ๋ฃนํ•‘์ด ๋ช…์‹œ๋˜์ง€ ์•Š์œผ๋ฉด ์ตœ์šฐ์„  ์ˆœ์œ„๋ฅผ ์ฐจ์ง€ํ•จ์„ ๊ธฐ์–ตํ•˜์ž. (์ด๋ฅผ ๋‘๊ณ  ํ•จ์ˆ˜ ์ ์šฉ์ด ๋‹ค๋ฅธ ๋ฌด์—‡๋ณด๋‹ค ๊ธด๋ฐ€ํ•˜๊ฒŒ ๋ฌถ์ธ๋‹ค๊ณ  ํ•œ๋‹ค) ์ž ๊น ์œ„์˜ factorial ํ•จ์ˆ˜๋Š” ์ฉ ์ž˜ ์ •์˜๋œ ๊ฒƒ์ด์ง€๋งŒ, ์กฐ๊ทธ๋งŒ ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— GHCi์—์„  ํ•œ ์ค„๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒŒ ์šฉ์ดํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด ์ค‘๊ด„ํ˜ธ(์ฆ‰ {์™€ })์™€ ์„ธ๋ฏธ์ฝœ๋ก ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. let { factorial 0 = 1 ; factorial n = n * factorial (n - 1) } ํ•˜์Šค์ผˆ์€ ์‚ฌ์‹ค ์ค„๋ฐ”๊ฟˆ๊ณผ ๋‹ค๋ฅธ ๊ณต๋ฐฑ๋“ค์„ ์œ„์˜ ๊ตฌ๋ถ„ ๋ฌธ์ž์™€ ๊ทธ๋ฃนํ•‘ ๋ฌธ์ž์˜ ๋Œ€์šฉ์œผ๋กœ ์“ด๋‹ค. ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์€ ๋Œ€์ฒด๋กœ ์ค„๋ฐ”๊ฟˆ๊ณผ ์ ์ ˆํ•œ ๋“ค์—ฌ ์“ฐ๊ธฐ์˜ ๋ง์‘ฅํ•จ์„ ์„ ํ˜ธํ•˜์ง€๋งŒ ์„ธ๋ฏธ์ฝœ๋ก  ๊ฐ™์€ ํ‘œ์‹œ๋ฌผ์„ ์„ ํ˜ธํ•œ๋‹ค๋ฉด ์ด๊ฒƒ๋„ ๊ดœ์ฐฎ๋‹ค. let ๋ฌธ์˜ ์ค‘๊ด„ํ˜ธ๋ฅผ ๋–ผ์–ด๋‚ด๋ฉด ํ•จ์ˆ˜์˜ ๋งˆ์ง€๋ง‰ ์ •์˜๋งŒ ์‚ฌ์šฉ๋˜์–ด, ์ข…๋ฃŒ๋˜๋Š” ๊ธฐ๋ณธ ๊ฐ€์ •์„ ์žƒ๊ณ  ๋ฌดํ•œ ์žฌ๊ท€๋ฅผ ํ•˜๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜๋ผ. ์œ„์˜ ์˜ˆ์ œ๋Š” ์ˆซ์ž n์˜ ๊ณ„์Šน๊ณผ ์•ฝ๊ฐ„ ์ž‘์€ ์ˆ˜ n-1์˜ ๊ณ„์Šน ๊ฐ„์˜ ์•„์ฃผ ๊ฐ„๋‹จํ•œ ๊ด€๊ณ„๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ํ•˜๋‚˜์˜ ์œ„์ž„์œผ๋กœ ์ƒ๊ฐํ•˜๋ผ. ์žฌ๊ท€ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ๋ช…๋ น์€ ๋ถ€์ฐจ์ ์ธ ์ž‘์—…์„ ์œ„์ž„ํ•œ๋‹ค. ์œ„์ž„๋ฐ›๋Š” ํ•จ์ˆ˜๋Š” ์œ„์ž„ํ•˜๋Š” ํ•จ์ˆ˜์™€ ๋˜‘๊ฐ™์€ ๋ช…๋ น์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋ณ€ํ•˜๋Š” ๊ฒƒ์€ ๋ฐ์ดํ„ฐ๋ฟ์ด๋‹ค. ์žฌ๊ท€ ํ•จ์ˆ˜์— ๊ด€ํ•ด ์ •๋ง๋กœ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๊ฒƒ์€ ๊ฐ๊ฐ์˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ๊ฐ™์€ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ์œ„์ž„์„ ์ถ”์ ํ•˜๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. factorial 3์„ ์‹คํ–‰ํ•˜๋ฉด ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ๋ณด์ž. 3์€ 0์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— 2์˜ ๊ณ„์Šน์„ ๊ณ„์‚ฐํ•œ๋‹ค 2๋Š” 0์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— 1์˜ ๊ณ„์Šน์„ ๊ณ„์‚ฐํ•œ๋‹ค 1์€ 0์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— 0์˜ ๊ณ„์Šน์„ ๊ณ„์‚ฐํ•œ๋‹ค 0์€ 0์ด๋ฏ€๋กœ 1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค 1์˜ ๊ณ„์Šน ๊ณ„์‚ฐ์„ ์™„์„ฑํ•˜๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ์ˆ˜ 1์— 0์˜ ๊ณ„์Šน์ธ 1์„ ๊ณฑํ•˜์—ฌ 1(1ร—1)์„ ์–ป๋Š”๋‹ค. 2์˜ ๊ณ„์Šน ๊ณ„์‚ฐ์„ ์™„์„ฑํ•˜๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ์ˆ˜ 2์— 1์˜ ๊ณ„์Šน์ธ 1์„ ๊ณฑํ•˜์—ฌ 2(2ร—1ร—1)์„ ์–ป๋Š”๋‹ค. 3์˜ ๊ณ„์Šน ๊ณ„์‚ฐ์„ ์™„์„ฑํ•˜๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ์ˆ˜ 3์— 2์˜ ๊ณ„์Šน์ธ 2๋ฅผ ๊ณฑํ•˜์—ฌ 6(3ร—2ร—1ร—1)์„ ์–ป๋Š”๋‹ค. (1์„ ๋‘ ๋ฒˆ ๊ณฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋๋‚˜๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. 0์˜ ๊ธฐ๋ณธ ๊ฐ€์ •์€ 1์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 1์„ ๊ณฑํ•˜๋ฉด ์•„๋ฌด ์˜ํ–ฅ๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ดœ์ฐฎ๋‹ค. ์›ํ•œ๋‹ค๋ฉด factorial์ด 1์—์„œ ๋ฉˆ์ถ”๊ฒŒ ์„ค๊ณ„ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ด๊ฒƒ์ด ๊ด€์Šต์ด๊ณ , 0์˜ ๊ณ„์Šน์„ ์ •์˜ํ•˜๋Š” ๊ฒŒ ์œ ์šฉํ•  ๋•Œ๋„ ์žˆ๋‹ค.) ๋จผ์ € ์žฌ๊ท€ ํ˜ธ์ถœ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ณ„์‚ฐ๋˜๊ณ  ๊ทธ๋‹ค์Œ ๊ณฑ์…ˆ์œผ๋กœ ๊ฒฐํ•ฉ๋˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก  ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ฝ๊ฑฐ๋‚˜ ๊ตฌ์„ฑํ•  ๋•Œ ์žฌ๊ท€๋ฅผ ์ด๋ ‡๊ฒŒ "ํ’€์–ดํ—ค์น " ํ•„์š”๋Š” ๊ฑฐ์˜ ์—†๋‹ค. ๊ทธ ๋™์ž‘์€ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด๊ณ , ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์€ ์ถ”์ƒ์ ์ธ ์ˆ˜์ค€์—์„œ ์ผํ•  ์ˆ˜ ์žˆ๋‹ค. factorial์˜ ์žฌ๊ท€์  ์ •์˜์—์„œ ํ•˜๋‚˜ ๋” ๋ˆˆ์—ฌ๊ฒจ๋ณผ ์ ์€ ๋‘ ์„ ์–ธ์˜ ์ˆœ์„œ(factorial 0์˜ ์„ ์–ธ ๋‹ค์Œ factorial n์˜ ์„ ์–ธ)์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์Šค์ผˆ์€ ์œ„์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ œ์ผ ์ฒ˜์Œ ์ผ์น˜ํ•˜๋Š” ํ•จ์ˆ˜ ์ •์˜๋ฅผ ์“ฐ๊ฒ ๋‹ค๊ณ  ๊ฒฐ์ •ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ(factorial n)๋ฅผ '๊ธฐ๋ณธ ๊ฐ€์ •'(factorial 0)์˜ ์•ž์— ๋†“์œผ๋ฉด ์ด ์ผ๋ฐ˜์ ์ธ n์€ ์ „๋‹ฌ๋˜๋Š” ๋ชจ๋“  ๊ฒƒ(0 ํฌํ•จ)์— ์ผ์น˜ํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿผ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” factorial 0์ด 0 * factorial (-1)๊ณผ ๊ฐ™๋‹ค๊ณ  ๊ฒฐ๋ก ์ง“๊ณ  ์Œ์˜ ๋ฌดํ•œ์œผ๋กœ ์น˜๋‹ซ๋Š”๋‹ค(๋ถ„๋ช… ์šฐ๋ฆฌ๊ฐ€ ๋ฐ”๋ผ๋Š” ๋ฐ”๋Š” ์•„๋‹ˆ๋‹ค). ๋”ฐ๋ผ์„œ ๋ณต์ˆ˜ ๊ฐœ์˜ ํ•จ์ˆ˜ ์ •์˜๋Š” ํ•ญ์ƒ ๊ตฌ์ฒด์ ์ธ ๊ฒƒ์—์„œ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๊ฒƒ ์ˆœ์œผ๋กœ ๋‚˜์—ดํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๊ณ„์Šน ํ•จ์ˆ˜๋ฅผ ํ•˜์Šค ์ผˆ ์†Œ์Šค ํŒŒ์ผ๋กœ ์ž‘์„ฑํ•˜๊ณ  GHCi๋กœ ๋ถˆ๋Ÿฌ์™€๋ณด์ž. factorial 5๋‚˜ factorial 1000์„ ์‹œ๋„ํ•ด ๋ณด์ž. factorial (-1)์€ ์–ด๋–ค๊ฐ€? ์™œ ๊ทธ๋Ÿฐ ์ผ์ด ์ผ์–ด๋‚ ๊นŒ? ์ˆ˜ n์˜ ๊ฒน๊ณ„์Šน์€ 1(๋˜๋Š” 2)์—์„œ n๊นŒ์ง€์˜ ๋ชจ๋“  2 ์ฐจ์ด ๋‚˜๋Š” ์ˆ˜์˜ ๊ณฑ์ด๋‹ค. ๊ฐ€๋ น 8์˜ ๊ฒน๊ณ„์Šน์€ 8 ร— 6 ร— 4 ร— 2 = 384, 7์˜ ๊ฒน๊ณ„์Šน์€ 7 ร— 5 ร— 3 ร— 1 = 105์ด๋‹ค. ํ•˜์Šค ์ผˆ๋กœ doublefactorial ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด์ž. ๋ฃจํ”„, ์žฌ๊ท€, ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ˆ„์  ๋ช…๋ นํ˜• ์–ธ์–ด๋Š” ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์ด ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฌธ๋งฅ์—์„œ ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด C ๊ฐ™์€ ์ „ํ˜•์ ์ธ ๋ช…๋ นํ˜• ์–ธ์–ด์—์„œ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฐฉ๋ฒ•์€ for ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ: ๋ช…๋ นํ˜• ์–ธ์–ด์—์„œ์˜ ๊ณ„์Šน ํ•จ์ˆ˜ int factorial(int n) { int res = 1; for ( ; n > 1; n--) res *= n; return res; } ์ด for ๋ฃจํ”„๋Š” res์— n์„ ๋ฐ˜๋ณตํ•˜์—ฌ ๊ณฑํ•œ๋‹ค. ๊ฐ ๋ฐ˜๋ณต ํ›„์—๋Š” n์—์„œ 1์„ ๋บ€๋‹ค(๋ฐ”๋กœ n--์ดํ•˜๋Š” ์ผ์ด๋‹ค). ๋ฐ˜๋ณต์€ n์ด 1๋ณด๋‹ค ํฌ์ง€ ์•Š์œผ๋ฉด ๋ฉˆ์ถ˜๋‹ค. ์ด๋Ÿฐ ํ•จ์ˆ˜๋ฅผ ํ•˜์Šค ์ผˆ๋กœ ๊ณง์ด๊ณง๋Œ€๋กœ ๋ฒˆ์—ญํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•œ๋ฐ, ๋ณ€์ˆ˜ res์™€ n์˜ ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ(ํŒŒ๊ดด์  ๊ฐฑ์‹ )์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ๋ฃจํ”„๋Š” ํ•ญ์ƒ ๋™๋“ฑํ•œ ํ˜•ํƒœ์˜ ์žฌ๊ท€๋กœ ๋ฒˆ์—ญํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐฑ์‹ ํ•ด์•ผ ํ•˜๋Š” ๋ฃจํ”„ ๋ณ€์ˆ˜๋ฅผ ์žฌ๊ท€ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด๊ฒƒ์€ ์œ„์˜ ๋ฃจํ”„๋ฅผ ํ•˜์Šค์ผˆ์‹ ์žฌ๊ท€๋กœ "๋ฒˆ์—ญ"ํ•œ ๊ฒƒ์ด๋‹ค. ์˜ˆ: ์žฌ๊ท€๋กœ ๋ฃจํ”„ ๋ชจ์‚ฌํ•˜๊ธฐ factorial n = go n 1 where go n res | n > 1 = go (n - 1) (res * n) | otherwise = res go๋Š” ์‹ค์ œ๋กœ ๊ณ„์Šน ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ณด์กฐ ํ•จ์ˆ˜๋‹ค. go๋Š” ์—ฌ๋ถ„์˜ ์ธ์ž res๋ฅผ ์ทจํ•˜๋Š”๋ฐ res๋Š” ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ์Œ“์•„ ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•œ ๋ˆ„์  ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์“ฐ์˜€๋‹ค. ์ž ๊น ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ์ต์ˆ™ํ•œ ์–ธ์–ด๋ฅผ ์ƒ๊ฐํ•ด์„œ ์žฌ๊ท€๊ฐ€ ์ผ์œผํ‚ค๋Š” ํผํฌ๋จผ์Šค ๋ฌธ์ œ๋ฅผ ๊ฑฑ์ •ํ• ์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค. ํ•˜์ง€๋งŒ ํ•˜์Šค์ผˆ์ด๋‚˜ ์—ฌํƒ€ ํ•จ์ˆ˜ํ˜• ์–ธ์–ด์˜ ์ปดํŒŒ์ผ๋Ÿฌ๋“ค์€ ์žฌ๊ท€๋ฅผ ์œ„ํ•œ ๋งŽ์€ ์ตœ์ ํ™”๋ฅผ ํฌํ•จํ•˜๋Š”๋ฐ, ์žฌ๊ท€๊ฐ€ ์–ผ๋งˆ๋‚˜ ํ”ํžˆ ํ•„์š”ํ•œ์ง€๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๋†€๋ž„ ์ผ์€ ์•„๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•˜์Šค์ผˆ์€ ๊ฒŒ์œผ๋ฅด๋‹ค. ์ฆ‰ ๊ณ„์‚ฐ์ด ์ˆ˜ํ–‰๋˜๋Š” ๊ฒƒ์€ ์˜ค์ง ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค๋ฅธ ๊ณ„์‚ฐ์ด ์š”๊ตฌํ•  ๋•Œ๋ฟ์ด๋‹ค. ์ด๋Š” ์ผ๋ถ€ ํผํฌ๋จผ์Šค ๋ฌธ์ œ๋ฅผ ํšŒํ”ผํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋œ๋‹ค. ์ด์™€ ๊ด€๋ จํ•œ ๋ฌธ์ œ๋“ค๊ณผ ๊ทธ ๋ฌธ์ œ๋“ค์ด ์ˆ˜๋ฐ˜ํ•˜๋Š” ์ค‘์š”ํ•œ ์„ธ๋ถ€ ์š”์†Œ๋“ค์€ ๋’ค์—์„œ ๋” ๋…ผํ•  ๊ฒƒ์ด๋‹ค. ๋‹ค๋ฅธ ์žฌ๊ท€ ํ•จ์ˆ˜๋“ค ์กฐ๋งŒ๊ฐ„ ๋“œ๋Ÿฌ๋‚˜๊ฒ ์ง€๋งŒ factorial ํ•จ์ˆ˜์— ๋ญ”๊ฐ€ ํŠน๋ณ„ํ•œ ๊ฒƒ์€ ์—†๋‹ค. ๋งŽ์€ ์ˆ˜์น˜์  ํ•จ์ˆ˜๋“ค์€ ์žฌ๊ท€์ ์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ •์˜๋œ๋‹ค. ๊ฐ€๋ น ๊ณฑ์…ˆ์„ ์ƒ๊ฐํ•ด ๋ณด์ž. ๊ณฑ์…ˆ์„ ์ฒ˜์Œ ๋ฐฐ์› ์„ ๋•Œ(๊ทธ๋•Œ๊ฐ€ ๊ธฐ์–ต๋‚˜๋Š”๊ฐ€? :)) '๋ฐ˜๋ณต ๋ง์…ˆ'์ด๋ผ๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณค์„ ๊ฒƒ์ด๋‹ค. ์ฆ‰ 5 ร— 4๋Š” ์ˆซ์ž 5์˜ ๋ณต์‚ฌ๋ณธ ๋„ค ๊ฐœ๋ฅผ ํ•ฉํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋ฌผ๋ก  5์˜ ๋ณต์‚ฌ๋ณธ 4๊ฐœ๋ฅผ ๋”ํ•˜๋Š” ๊ฒƒ์€ ๋ณต์‚ฌ๋ณธ 3๊ฐœ์— 5๋ฅผ ํ•˜๋‚˜ ๋”ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ์ฆ‰ 5 ร— 4 = 5 ร— 3 + 5์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์ž์—ฐ์Šค๋ ˆ ๊ณฑ์…ˆ์˜ ์žฌ๊ท€ ์ •์˜๊ฐ€ ๋‚˜์˜จ๋‹ค. ์˜ˆ: ์žฌ๊ท€์ ์œผ๋กœ ์ •์˜๋œ ๊ณฑ์…ˆ mult n 0 = 0 -- ์•„๋ฌด๊ฐœ ๊ณฑํ•˜๊ธฐ 0์€ ์˜์ด๋‹ค mult n 1 = n -- ์•„๋ฌด๊ฐœ ๊ณฑํ•˜๊ธฐ 1์€ ๊ทธ๋Œ€๋กœ๋‹ค mult n m = (mult n (m - 1)) + n -- ์žฌ๊ท€: ํ•˜๋‚˜ ์ ์€ ๊ฒƒ๊ณผ ๊ณฑํ•˜๊ณ  ์—ฌ๋ถ„์˜ ๋ณต์‚ฌ๋ณธ์„ ํ•˜๋‚˜ ๋”ํ•œ๋‹ค ์ž ๊น ๋ฌผ๋Ÿฌ์„œ์„œ ๋ณด๋ฉด ์ˆ˜์น˜ ์žฌ๊ท€๊ฐ€ ์ผ๋ฐ˜์ ์ธ ์žฌ๊ท€ ํŒจํ„ด์— ์–ผ๋งˆ๋‚˜ ์ž˜ ๋“ค์–ด๋งž๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ˆ˜์น˜ ์žฌ๊ท€์˜ ๊ธฐ๋ณธ ๊ฐ€์ •์€ ํ†ต์ƒ ๊ทธ ๋‹ต์ด ์ฆ‰๊ฐ ๋‚˜์˜ค๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ตฌ์ฒด์ ์ธ ์ˆ˜(์ฃผ๋กœ 0์ด๋‚˜ 1)๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ์žฌ๊ท€ ๊ฐ€์ •์€ ๋” ์ž‘์€ ์ธ์ˆ˜๋กœ ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ข…์ ์ธ ๋‹ต์„ ๋„์ถœํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด๋•Œ ์“ฐ์ด๋Š” '๋” ์ž‘์€ ์ธ์ˆ˜'๋Š” ์ฃผ๋กœ ํ˜„์žฌ ์ธ์ˆ˜๋ณด๋‹ค ํ•˜๋‚˜ ์ž‘์€ ๊ฒƒ์œผ๋กœ, '์ˆ˜์ง์„ ์„ ๊ฑธ์–ด ๋‚ด๋ ค๊ฐ€๋Š”' ์žฌ๊ท€๋ฅผ ์ด๋Œ์–ด๋‚ธ๋‹ค(์œ„์˜ factorial์ด๋‚˜ mult์ฒ˜๋Ÿผ). ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ์›์‹œ์ ์ธ ํŒจํ„ด๋งŒ ๊ฐ€๋Šฅํ•œ ๊ฑด ์•„๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ๋„ ์ด๋Ÿฐ ๋” ์ž‘์€ ์ธ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ factorial 3์„ ๊ฐ€์ง€๊ณ  ํ•œ ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๊ณฑ์…ˆ 5 ร— 4๋ฅผ ์ „๊ฐœํ•˜๋ผ. power x y๊ฐ€ x์˜ y ์Šน์ธ ์žฌ๊ท€ ํ•จ์ˆ˜ power๋ฅผ ์ •์˜ํ•˜๋ผ. plusOne x = x + 1์ด๋ž€ ํ•จ์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค. ๋‹ค๋ฅธ (+)๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  addition x y๊ฐ€ x์™€ y๋ฅผ ๋”ํ•˜๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜ addition์„ ์ •์˜ํ•˜๋ผ. (์–ด๋ ค์›€) ์ธ์ž์˜ ์ •์ˆ˜ ๋กœ๊ทธ(๋ฐ‘์ด 2)๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜ log2๋ฅผ ์ •์˜ํ•˜๋ผ. ์ฆ‰ log2๋Š” ์ธ์ž ์ดํ•˜์˜ ๊ฐ€์žฅ ํฐ 2์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ์˜<NAME>๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด log2 16 = 4, log2 11 = 3, log2 1 = 0์ด๋‹ค. (์ž‘์€ ํžŒํŠธ: ์ด ์—ฐ์Šต๋ฌธ์ œ๋“ค ๋ฐ”๋กœ ์ „ ๋‹จ๋ฝ์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์žฅ์„ ์ฝ์„ ๊ฒƒ.) ๋ฆฌ์ŠคํŠธ ๊ธฐ๋ฐ˜ ์žฌ๊ท€ ํ•˜์Šค์ผˆ์˜ ๋งŽ์€ ํ•จ์ˆ˜๊ฐ€ ์žฌ๊ท€์ ์ธ๋ฐ, ํŠนํžˆ ๋ฆฌ์ŠคํŠธ ๊ด€๋ จ ํ•จ์ˆ˜๋“ค์ด ๊ทธ๋ ‡๋‹ค.4 ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋ฅผ ์•Œ์•„๋‚ด๋Š” length ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด์ž. ์˜ˆ: length์˜ ์žฌ๊ท€์  ์ •์˜ length :: [a] -> Int length [] = 0 length (x:xs) = 1 + length xs ๋…ธํŠธ ์œ„ ์ •์˜๋ฅผ ์†Œ์Šค ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•  ๋•Œ GHCi๊ฐ€ "๋ชจํ˜ธํ•œ ์ถœ์ฒ˜(ambiguous occurrence)"์— ๋Œ€ํ•ด ๋ถˆํ‰ํ•  ๊ฒƒ์ด๋‹ค. Prelude๊ฐ€ ์ด๋ฏธ length๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ •์˜ํ•˜๋ ค๋Š” ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ๋‹ค๋ฅธ ๊ฒƒ, ๊ฐ€๋ น length'๋‚˜ myLength๋กœ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์‹คํžˆ ํ•˜๊ธฐ ์œ„ํ•ด ํ•œ๊ตญ์–ด๋กœ ํ’€์–ด๋ณด์ž. length์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋Š” ์ž„์˜ ํƒ€์ž…์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด Int๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค๊ณ  ์•Œ๋ฆฐ๋‹ค. ๋‹ค์Œ ์ค„์€ ๋นˆ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ 0์ด๋ผ ์„ ์–ธํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž์—ฐ์Šค๋ ˆ ์ด๊ฒƒ์ด ๊ธฐ๋ณธ ๊ฐ€์ •์ด ๋œ๋‹ค. ๋งˆ์ง€๋ง‰ ์ค„์ด ์žฌ๊ท€ ๊ฐ€์ •์ด๋‹ค. ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด ์ฒซ ๋ฒˆ์งธ ์›์†Œ(์—ฌ๊ธฐ์„  x)์™€ ๋ฆฌ์ŠคํŠธ์˜ ๋‚˜๋จธ์ง€(์—ฌ๊ธฐ์„  xs, ์ฆ‰ x์˜ ๋ณต์ˆ˜ํ˜•. ์›์†Œ๊ฐ€ ๋” ์—†์œผ๋ฉด ๋‹จ์ˆœํžˆ ๋นˆ ๋ฆฌ์ŠคํŠธ)๋กœ ์ชผ๊ฐ ๋‹ค. ์ด ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋Š” 1(x๋ฅผ ๊ณ ๋ ค) ๋”ํ•˜๊ธฐ xs์˜ ๊ธธ์ด๋‹ค(๋‹ค์Œ ๊ณผ์ •์˜ tail๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ xs๋Š” ์ธ์ž์ธ ๋ฆฌ์ŠคํŠธ๊ฐ€ (:) ํŒจํ„ด์— ์ผ์น˜ํ•  ๋•Œ ๊ฐ–์ถฐ์ง„๋‹ค). ์ด๋ฒˆ์—๋Š” ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž‡๋Š” ์—ฐ๊ฒฐ ํ•จ์ˆ˜ (++)๋ฅผ ์‚ดํŽด๋ณด์ž. ์˜ˆ: ์žฌ๊ท€์  (++) Prelude> [1,2,3] ++ [4,5,6] [1,2,3,4,5,6] Prelude> "Hello " ++ "world" -- String์€ Char๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋‹ค "Hello world" (++) :: [a] -> [a] -> [a] [] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys length๋ณด๋‹จ ์กฐ๊ธˆ ๋ณต์žกํ•˜์ง€๋งŒ ์ชผ๊ฐœ๊ณ  ๋‚˜๋ฉด ๊ทธ๋‹ค์ง€ ์–ด๋ ต์ง€ ์•Š๋‹ค. (++)์˜ ํƒ€์ž…์€ (++)๊ฐ€ ๊ฐ™์€ ํƒ€์ž…์˜ ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด ๊ฐ™์€ ํƒ€์ž…์˜ ๋˜ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์‚ฐํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ฆฐ๋‹ค. ๊ธฐ๋ณธ ๊ฐ€์ •์€ ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌ์ŠคํŠธ ys์™€ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ys ์ž์ฒด์™€ ๊ฐ™๋‹ค๊ณ  ์„ ์–ธํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์žฌ๊ท€ ๊ฐ€์ •์€ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋ฅผ x(๋จธ๋ฆฌ)์™€ xs(๊ผฌ๋ฆฌ)๋กœ ์ชผ๊ฐœ์„œ, xs์™€ ys๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  ๊ทธ ์•ž์— ๋จธ๋ฆฌ์ธ x๋ฅผ ๋ถ™์ด๋ผ๊ณ  ์„ ์–ธํ•œ๋‹ค. ์—ฌ๊ธฐ ํŒจํ„ด์ด ๋ณด์ธ๋‹ค. ๋ฆฌ์ŠคํŠธ ๊ธฐ๋ฐ˜ ํ•จ์ˆ˜์—์„œ๋Š” ๊ธฐ๋ณธ ๊ฐ€์ •์ด ํ†ต์ƒ ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆ˜๋ฐ˜ํ•˜๊ณ , ์žฌ๊ท€ ๊ฐ€์ •์€ ๋ฆฌ์ŠคํŠธ์˜ ๊ผฌ๋ฆฌ๋ฅผ ํ•จ์ˆ˜์— ๋‹ค์‹œ ๋„˜๊ฒจ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ ์ฐจ ์ž‘์•„์ง€๊ฒŒ ๋งŒ๋“ ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋‹ค์Œ์˜ ๋ฆฌ์ŠคํŠธ ๊ธฐ๋ฐ˜ ํ•จ์ˆ˜๋“ค์„ ์žฌ๊ท€์ ์œผ๋กœ ์ •์˜ํ•˜๋ผ. ๊ฐ๊ฐ ๊ธฐ๋ณธ ๊ฐ€์ •์ด ์–ด๋–จ์ง€๋ฅผ ์ƒ๊ฐํ•œ ๋‹ค์Œ์— ์žฌ๊ท€ ๊ฐ€์ •์€ ์–ด๋–จ์ง€๋ฅผ ์ƒ๊ฐํ•ด ๋ณผ ๊ฒƒ. (์—ฌ๊ธฐ์˜ ๋ชจ๋“  ํ•จ์ˆ˜๋Š” Prelude์— ์ด๋ฏธ ์žˆ์œผ๋ฏ€๋กœ GHCi์—์„œ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ •์˜๋ฅผ ์‹œํ—˜ํ•  ๋•Œ๋Š” ์ด๋ฆ„์„ ๋‹ค๋ฅด๊ฒŒ ํ•ด์•ผ ํ•œ๋‹ค) replicate :: Int -> a -> [a]๋Š” ์นด์šดํŠธ์™€ ํ•œ ์›์†Œ๋ฅผ ์ทจํ•ด ๊ทธ ์›์†Œ๋ฅผ ๊ทธ๋งŒํผ ๋ฐ˜๋ณตํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด replicate 3 'a' = "aaa"๋‹ค. (ํžŒํŠธ: ์นด์šดํŠธ๊ฐ€ 0์ผ ๋•Œ ๋ฌด์–ธ๊ฐ€์˜ ๋ณต์ œ๊ฐ€ ๋ฌด์—‡์ด์–ด์•ผ ํ•˜๋Š”์ง€ ์ƒ๊ฐํ•ด ๋ณด๋ผ. ์นด์šดํŠธ 0์ด ์—ฌ๋Ÿฌ๋ถ„์˜ '๊ธฐ๋ณธ ๊ฐ€์ •'์ด๋‹ค.) (!!) :: [a] -> Int -> a๋Š” ์ฃผ์–ด์ง„ '์ธ๋ฑ์Šค'์˜ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋Š” ์ธ๋ฑ์Šค 0์—, ๋‘ ๋ฒˆ์งธ๋Š” 1์— ์žˆ๋Š” ์‹์ด๋‹ค. Note that with this function, you're recursing both numerically and down a list.5 (์‚ด์ง ์–ด๋ ค์›€) zip :: [a] -> [b] -> [(a, b)]๋Š” ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด 'zip'ํ•˜์—ฌ, ์ฒซ ๋ฒˆ์งธ ์ง์€ ๋‘ ๋ฆฌ์ŠคํŠธ์˜ ์ฒ˜์Œ ์›์†Œ๋“ค์ด๊ณ , ๊ทธ๋ ‡๊ฒŒ ๊ณ„์†๋œ๋‹ค. ๊ฐ€๋ น zip [1,2,3] "abc" = [(1, 'a'), (2, 'b'), (3, 'c')]์ด๋‹ค. ๋‘ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด ํ•˜๋‚˜๋ผ๋„ ๋‹ค ๋–จ์–ด์ง€๋Š” ์‹œ์ ์—์„œ ๋ฉˆ์ถœ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋ น zip [1,2] "abc" = [(1, 'a'), (2, 'b')]์ด๋‹ค. factorial์˜ ๋ฃจํ”„์‹ ๋Œ€์ฒด ๋ฒ„์ „์ฒ˜๋Ÿผ ๋ณด์กฐ ํ•จ์ˆ˜์™€ ๋ˆ„์  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ length๋ฅผ ์ •์˜ํ•˜๋ผ. ์žฌ๊ท€๋Š” ๋ฆฌ์ŠคํŠธ์™€ ์ˆ˜์— ๊ด€ํ•œ ๊ฑฐ์˜ ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์“ฐ์ธ๋‹ค. ๋‚˜์ค‘์— ๋ฆฌ์ŠคํŠธ ๊ธฐ๋ฐ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ•„์š”ํ•  ๋•Œ ๋นˆ ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ์™€ ๋น„์–ด์žˆ์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ๋กœ ์‹œ์ž‘ํ•ด์„œ ํ˜น์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žฌ๊ท€์ ์ธ์ง€ ์‚ดํŽด๋ณด๋ผ. ์žฌ๊ท€์— ๋„ˆ๋ฌด ๋“ค๋œจ์ง€ ๋ง์ž... ํ•˜์Šค์ผˆ์—์„œ ์žฌ๊ท€์˜ ๋ฒ”์šฉ์„ฑ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์šฐ๋ฆฌ๊ฐ€ ๋ช…์‹œ์ ์œผ๋กœ ์žฌ๊ท€ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ผ์€ ๋“œ๋ฌผ๋‹ค. ๋Œ€์‹  ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜๋“ค์€ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ์šฐ๋ฆฌ ๋Œ€์‹  ์žฌ๊ท€๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด factorial์€ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋” ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜๋กœ ๊ณ„์Šน ๊ตฌํ˜„ํ•˜๊ธฐ factorial n = product [1.. n] ๋ถ€์ •ํ–‰์œ„ ๊ฐ™์ง€ ์•Š์€๊ฐ€? :) ๋Œ€๋ถ€๋ถ„์˜ ๋…ธ๋ จํ•œ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์€ factorial์„ ์ž‘์„ฑํ•  ๋•Œ ๋ช…์‹œ์ ์ธ ์žฌ๊ท€ ๋Œ€์‹  ์ด๋ ‡๊ฒŒ ํ•  ๊ฒƒ์ด๋‹ค. ๋ฌผ๋ก  product ํ•จ์ˆ˜๋Š” ๊ทธ ์ด๋ฉด์—์„œ ๋ชจ์ข…์˜ ๋ฆฌ์ŠคํŠธ ์žฌ๊ท€๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€๋งŒ, 6 ์ด๋Ÿฐ ์‹์œผ๋กœ factorial์„ ์ž‘์„ฑํ•˜๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋จธ์ธ ์šฐ๋ฆฌ๋Š” ๊ทธ๋Ÿฐ ์žฌ๊ท€๋ฅผ ์‹ ๊ฒฝ ์“ธ ํ•„์š”๊ฐ€ ์—†๋‹ค. ์ˆ˜ํ•™์—์„œ n! ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜ n์˜ ๊ณ„์Šน์„ ๋œปํ•˜์ง€๋งŒ ์ด ๋ฌธ๋ฒ•์€ ํ•˜์Šค์ผˆ์—์„œ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„  ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. โ†ฉ ์‚ฌ์‹ค 0์˜ ๊ณ„์Šน์„ 1๋กœ ์ •์˜ํ•œ ๊ฑด ์ž„์˜๋กœ ํ•œ ๊ฒŒ ์•„๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” 0์˜ ๊ณ„์Šน์ด ๊ณต ๊ณฑ์…ˆ(empty product)์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. โ†ฉ ํฅ๋ฏธ๋กญ๊ฒŒ๋„ ๊ตฌ์‹ ๊ณผํ•™ ๊ณ„์‚ฐ๊ธฐ๋Š” 1000์˜ ๊ณ„์Šน ๊ฐ™์€ ๊ฑธ ๋‹ค๋ฃฐ ์ˆ˜ ์—†๋Š”๋ฐ ์ž๋ฆฟ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ์ปค์„œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค! โ†ฉ ์šฐ์—ฐ์˜ ์ผ์น˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ์ˆ˜์ • ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜๊ฐ€ ์—†์„ ๋•Œ ์žฌ๊ท€๋Š” ์ œ์–ด ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•  ์œ ์ผํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ์—ฌ๊ธฐ์— ์ต์ˆ™ํ•ด์ง€๊ธฐ ์ „์—๋Š” ์–ด๋–ค ํ•œ๊ณ„์ฒ˜๋Ÿผ ๋ณด์ผ ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค. โ†ฉ ๋ถ€์ˆ˜์ ์œผ๋กœ (!!)๋Š” ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ/๊ฐ’ ํš๋“ํ•˜๊ธฐ์˜ ๋„ค ๋ฒˆ์งธ ์—ฐ์Šต๋ฌธ์ œ์˜ ๊ทธ ๋ฌธ์ œ์— ํ•ฉ๋ฆฌ์ ์ธ ํ•ด๋‹ต์„ ์ œ๊ณตํ•œ๋‹ค. โ†ฉ ์‚ฌ์‹ค product๋Š” foldl์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๊ณ  foldl์ด ์‹ค์ œ๋กœ ์žฌ๊ท€๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. โ†ฉ 2 ๋ฆฌ์ŠคํŠธ ๋ณด์ถฉ ์„ค๋ช… ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Lists_II ๋ฆฌ์ŠคํŠธ ์žฌ๊ตฌ์ถ•ํ•˜๊ธฐ ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ ๋” ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ ์ปค๋ง map ํ•จ์ˆ˜ ํŒ๊ณผ ํŠธ๋ฆญ ์ด์ค‘์  ํ‘œ๊ธฐ(Dot Dot Notation) ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ head์™€ tail์— ๊ด€ํ•œ ๋…ธํŠธ ์•ž์„œ ํ•˜์Šค์ผˆ์€ cons ์—ฐ์‚ฐ์ž (:)์™€ ๋นˆ ๋ฆฌ์ŠคํŠธ []๋ฅผ ํ†ตํ•ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐฐ์› ๋‹ค. ์žฌ๊ท€์™€ ํŒจํ„ด ๋งค์นญ์„ ์กฐํ•ฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๋ดค๋‹ค. ์ด๋ฒˆ ์žฅ๊ณผ ๋‹ค์Œ ์žฅ์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋” ์‹ฌ๋„ ์žˆ๋Š” ๊ธฐ๋ฒ•๋“ค์„ ์‚ดํŽด๋ณด๊ณ  ๋ช‡ ๊ฐ€์ง€ ์ƒˆ๋กœ์šด ํ‘œ๊ธฐ๋“ค์„ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ, ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹(list comprehension), ๊ณ ์ฐจ ํ•จ์ˆ˜ ๊ฐ™์€ ํ•˜์Šค ์ผˆ ๊ธฐ๋Šฅ๋“ค์„ ๋ง›๋ณผ ๊ฒƒ์ด๋‹ค. ์ž ๊น ์ด๋ฒˆ ์žฅ ์ „์ฒด์— ๊ฑธ์ณ ์šฐ๋ฆฌ๋Š” ๋ฆฌ์ŠคํŠธ ์›์†Œ๋“ค์„ ๋”ํ•˜๊ณ , ๋นผ๊ณ , ๊ณฑํ•˜๋Š” ํ•จ์ˆ˜๋“ค์„ ์ฝ๊ณ  ๋˜ ์ž‘์„ฑํ•  ๊ฒƒ์ด๋‹ค. ๊ฐ„๊ฒฐํ•จ์„ ์œ„ํ•ด ๋ฆฌ์ŠคํŠธ ์›์†Œ๋“ค์ด Integer ํƒ€์ž…์ด๋ผ ๊ฐ€์ •ํ•˜๊ฒ ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํƒ€์ž…์˜ ๊ธฐ์ดˆ 2์—์„œ ํ–ˆ๋˜ ๋…ผ์˜๋ฅผ ๋– ์˜ฌ๋ ค๋ณด๋ฉด Num ํƒ€์ž… ํด๋ž˜์Šค์— ์†ํ•˜๋Š” ํƒ€์ž…์€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์—ˆ๋‹ค. ํ•จ์ˆ˜๋“ค์„ ๋‹คํ˜•์„ฑ์œผ๋กœ ๋งŒ๋“ค์–ด ๋ฆฌ์ŠคํŠธ ์›์†Œ๋“ค์ด Num ํด๋ž˜์Šค์˜ ์ž„์˜์˜ ํƒ€์ž…์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค๋ ค๋ฉด, ๊ทธ๋Ÿฐ ํ•จ์ˆ˜๋“ค์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๊ฐ€ ๋ฌด์—‡์ด์–ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์ผ์ข…์˜ ์—ฐ์Šต ๋ฌธ์ œ ์‚ผ์•„ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ•จ์ˆ˜์˜ ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด ๊ทธ๊ฒƒ๋“ค์„ ๋ช…์‹œํ•˜์ง€ ์•Š๊ณ  ์ƒ๋žตํ•˜๊ณ , GHCi๋กœ ๊ทธ ํ•จ์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ :t๋ฅผ ์‚ฌ์šฉํ•ด ํƒ€์ž… ์ถ”๋ก ์ด ์ด๋„๋Š” ๊ธธ์„ ๋”ฐ๋ผ๊ฐ€์ž. ๋ฆฌ์ŠคํŠธ ์žฌ๊ตฌ์ถ•ํ•˜๊ธฐ ๋‹ค์Œ ํ•จ์ˆ˜๋Š” ์ •์ˆ˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ์›์†”๋ฅด ๋‘ ๋ฐฐ๋กœ ๋งŒ๋“ ๋‹ค. doubleList :: [Integer] -> [Integer] doubleList [] = [] doubleList (n:ns) = (2 * n) : doubleList ns ๊ธฐ๋ณธ ๊ฐ€์ •์€ ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ํ‰๊ฐ€๋˜๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ๋‹ค. ์žฌ๊ท€ ๊ฐ€์ •์—์„œ doubleList๋Š” (:)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. ์ด ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋Š” ์ธ์ž์˜ ๋จธ๋ฆฌ์˜ ๋‘ ๋ฐฐ์ด๋ฉฐ, ๊ฒฐ๊ณผ์˜ ๋‚˜๋จธ์ง€๋Š” doubleList๋ฅผ ์ธ์ž์˜ ๊ผฌ๋ฆฌ์— ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•˜์—ฌ ์–ป์–ด์ง„๋‹ค. ๊ผฌ๋ฆฌ๊ฐ€ ๋นˆ ๋ฆฌ์ŠคํŠธ๋ผ๋ฉด ๊ธฐ๋ณธ ๊ฐ€์ •์ด ์‹คํ–‰๋˜๊ณ  ์žฌ๊ท€๋Š” ๋ฉˆ์ถ˜๋‹ค. 1 ๋‹ค์Œ์˜ ์˜ˆ์‹œ ํ‘œํ˜„์‹์ด ์–ด๋–ป๊ฒŒ ํ‰๊ฐ€๋˜๋Š”์ง€ ์—ฐ๊ตฌํ•ด ๋ณด์ž. doubleList [1,2,3,4] ๋Œ€์ˆ˜ํ•™ ๊ต๊ณผ์„œ์ฒ˜๋Ÿผ ์ธ์ž๋ฅผ ํ•จ์ˆ˜ ์ •์˜๋กœ ์น˜ํ™˜ํ•ด๊ฐ€๋ฉด ๊ธธ๊ฒŒ ๋Š˜์–ด๋œจ๋ฆด ์ˆ˜ ์žˆ๋‹ค. doubleList 1:[2,3,4] = (1*2) : doubleList (2 : [3,4]) = (1*2) : (2*2) : doubleList (3 : [4]) = (1*2) : (2*2) : (3*2) : doubleList (4 : []) = (1*2) : (2*2) : (3*2) : (4*2) : doubleList [] = (1*2) : (2*2) : (3*2) : (4*2) : [] = 2 : 4 : 6 : 8 : [] = [2, 4, 6, 8] ๋ชจ๋“  ์›์†Œ๋ฅผ ๋‘ ๋ฐฐ๋กœ ๋งŒ๋“ค์–ด ์›๋ณธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์žฌ๊ตฌ์ถ•ํ–ˆ๋‹ค. ์ด ๊ธฐ๋‚˜๊ธด ํ‰๊ฐ€ ์—ฐ์Šต์—์„œ ์šฐ๋ฆฌ๊ฐ€ ๊ณฑ์…ˆ์„ ํ‰๊ฐ€ํ•˜๊ธฐ๋กœ ๊ณ ๋ฅธ ์‹œ์ ์€ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ผ์น˜์ง€ ์•Š๋Š”๋‹ค. ํ‰๊ฐ€๋Š” doubleList์˜ ๊ฐ๊ฐ์˜ ์žฌ๊ท€ ํ˜ธ์ถœ ์งํ›„์— ํ•  ์ˆ˜๋„ ์žˆ์—ˆ๋‹ค.2 ํ•˜์Šค์ผˆ์€ ์ด๋Ÿฐ ํ‰๊ฐ€ ์ˆœ์„œ์˜ ์œ ์—ฐํ•จ์„ ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ๋ฐฉ์‹์œผ๋กœ ํ™œ์šฉํ•œ๋‹ค. ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋กœ์„œ, ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋ฌด์–ธ๊ฐ€๋ฅผ ์–ธ์ œ ์‹ค์ œ๋กœ ํ‰๊ฐ€ํ• ์ง€๋ฅผ ๋Œ€๋ถ€๋ถ„ ๊ฒฐ์ •ํ•œ๋‹ค. ์ง€์—ฐ ํ‰๊ฐ€ ์–ธ์–ด๋กœ์„œ, ํ•˜์Šค์ผˆ์€ ์ตœ์ข… ๊ฐ’์ด ํ•„์š”ํ•  ๋•Œ๊นŒ์ง€ ํ‰๊ฐ€๋ฅผ ๋ฏธ๋ฃฌ๋‹ค(๊ฐ€๋”์€ ์˜์˜ ํ•„์š”ํ•˜์ง€ ์•Š๊ธฐ๋„ ํ•˜๋‹ค).3 ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ๊ด€์ ์—์„œ ํ‰๊ฐ€ ์ˆœ์„œ๋ฅผ ์‹ ๊ฒฝ ์“ธ ์ผ์€ ๋“œ๋ฌผ๋‹ค. 4 ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์›์†Œ๋ฅผ ์„ธ ๋ฐฐ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด doubleList์™€ ๊ฐ™์€ ์ „๋žต์„ ์ทจํ•  ์ˆ˜ ์žˆ๋‹ค. tripleList :: [Integer] -> [Integer] tripleList [] = [] tripleList (n:ns) = (3 * n) : tripleList ns ๊ทธ๋Ÿฐ๋ฐ ์„œ๋กœ ๋‹ค๋ฅธ ๋ชจ๋“  ์Šน์ˆ˜(4, 8, 17 ๋“ฑ)๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ ๊ณฑ์…ˆ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์ง€๋Š” ์•Š๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์ž„์˜์˜ ์ˆ˜๋ฅผ ๊ณฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์šฉ์ ์ธ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž. ์ด ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋Š” ์ธ์ž๋ฅผ ๋‘ ๊ฐœ ๋ฐ›๋Š”๋‹ค. ํ•˜๋‚˜๋Š” ํ”ผ์Šน์ˆ˜๊ณ  ํ•˜๋‚˜๋Š” ๊ณฑ์…ˆ์„ ํ•  Integer ๋ฆฌ์ŠคํŠธ๋‹ค. multiplyList :: Integer -> [Integer] -> [Integer] multiplyList _ [] = [] multiplyList m (n:ns) = (m*n) : multiplyList m ns ์ด ์˜ˆ์ œ๋Š” _๋ฅผ "์‹ ๊ฒฝ ์•ˆ ์”€"(don't care) ํŒจํ„ด์œผ๋กœ์จ ์‚ฌ์šฉํ•œ๋‹ค. ๊ธฐ๋ณธ ๊ฐ€์ •์—๋Š” ํ”ผ์Šน์ˆ˜๊ฐ€ ์“ฐ์ด์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ์ด๋ฆ„(m, n, ns ๋“ฑ)์„ ๋ถ€์—ฌํ•˜๋Š” ๋Œ€์‹  ๋ฌด์‹œํ•œ๋‹ค. mutiplyList๊ฐ€ ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Prelude> multiplyList 17 [1,2,3,4] [17,34,51,68] ์—ฐ์Šต๋ฌธ์ œ ๋‹ค์Œ ํ•จ์ˆ˜๋“ค์„ ์ž‘์„ฑํ•˜๊ณ  ํ™•์ธํ•ด ๋ณธ๋‹ค. ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ์žŠ์ง€ ๋ง์ž. takeInt๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ฒ˜์Œ n ๊ฐœ ํ•ญ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰ takeInt 4 [11,21,31,41,51,61]๋Š” [11,21,31,41]์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. dropInt๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ฒ˜์Œ n ๊ฐœ ํ•ญ๋ชฉ์„ ๋ฒ„๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰ dropInt 3 [11,21,31,41,51]์€ [41,51]์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. sumInt๋Š” ๋ฆฌ์ŠคํŠธ ๋‚ด ํ•ญ๋ชฉ๋“ค์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. scanSum์€ ๋ฆฌ์ŠคํŠธ ๋‚ด ํ•ญ๋ชฉ๋“ค์„ ๋”ํ•ด ์ค‘๊ฐ„ ํ•ฉ๋“ค์„ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰ scanSum [2,3,4,5]์€ [2,5,9,14]์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. diffs๋Š” ์ธ์ ‘ ํ•ญ๋ชฉ๋“ค์˜ ์ฐจ์ด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰ diffs [3,5,6,8]์€ [2,1,2]์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (ํžŒํŠธ: ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด ๋Œ€์‘ํ•˜๋Š” ํ•ญ๋ชฉ๋“ค์˜ ์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ณด์กฐ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ์ธ์ž๊ฐ€ ์ ์–ด๋„ ๋‘˜์ธ ๋ฆฌ์ŠคํŠธ๋Š” (x:y:ys) ํŒจํ„ด์— ์ผ์น˜ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฒ˜์Œ์˜ ์„ธ ํ•จ์ˆ˜๋Š” Prelude์— take, drop, sum ์ด๋ž€ ์ด๋ฆ„์œผ๋กœ ๋“ค์–ด์žˆ๋‹ค. ๋” ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ ์ด๋ฒˆ ์žฅ์„ ์›์†Œ๋“ค์— 2๋ฅผ ๊ณฑํ•˜๋Š” ์ œํ•œ๋œ ํ•จ์ˆ˜๋กœ ์‹œ์ž‘ํ–ˆ๋‹ค. ๊ฐ ์Šน์ˆ˜๋งˆ๋‹ค ์ƒˆ ํ•จ์ˆ˜๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ซ๊ณ  multiplyList๋ฅผ ๋งŒ๋“ค์–ด ์ž„์˜์˜ Integer๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด์ œ ๋ง์…ˆ ๊ฐ™์€ ๋‹ค๋ฅธ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๊ฐ ์›์†Œ์˜ ์ œ๊ณฑ์„ ๊ณ„์‚ฐํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? ํ•˜์Šค์ผˆ์˜ ํ•œ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋ฉด ๋” ์ผ๋ฐ˜ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ํ•ด๊ฒฐ์ฑ…์ด ๊ฝค ๋†€๋ผ์šธ ์ˆ˜๋„ ์žˆ๊ธฐ์— ๋น™ ๋‘˜๋Ÿฌ ๊ฐ€๋Š” ์ ‘๊ทผ๋ฒ•์„ ํƒํ•  ๊ฒƒ์ด๋‹ค. multiplyList์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ์‚ดํŽด๋ณด์ž. multiplyList :: Integer -> [Integer] -> [Integer] ๊ฐ€์žฅ ๋จผ์ € ์•Œ์•„์•ผ ํ•  ์ ์€ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ์˜ -> ํ™”์‚ดํ‘œ๊ฐ€ ์šฐ๊ฒฐํ•ฉ(right-associative)์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰ ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. multiplyList :: Integer -> ( [Integer] -> [Integer] ) ์ด๊ฑธ ์–ด๋–ป๊ฒŒ ์ดํ•ดํ•ด์•ผ ํ• ๊นŒ? multiplyList๊ฐ€ Integer ์ธ์ž ํ•˜๋‚˜๋ฅผ ์ทจํ•ด ๋˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋กœ ํ‰๊ฐ€ํ•˜๋Š”๋ฐ, ๊ทธ ํ•จ์ˆ˜๋Š” Integer์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด ๋˜ ๋‹ค๋ฅธ Integer ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ์•ž์„œ์˜ doubleList ํ•จ์ˆ˜๋ฅผ multiplyList๋ฅผ ์ด์šฉํ•ด ์žฌ์ •์˜ํ•ด ๋ณด์ž. doubleList :: [Integer] -> [Integer] doubleList xs = multiplyList 2 xs ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด xs๋ฅผ ๊น”๋”ํžˆ ์†Œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. doubleList = multiplyList 2 ์ธ์ž ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ด๋Ÿฐ ์‹์˜ ์ •์˜๋ฅผ "์ธ์ž ์ƒ๋žต"(point-free) ๋ฐฉ์‹์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ด ํ‘œํ˜„์‹์€ ์™„๋ฒฝํžˆ ํ˜•ํƒœ๋ฅผ ๊ฐ–์ถ˜ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ multiplyList์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž(doubleList์˜ ์œ ์ผํ•œ ์ธ์ž์™€ ๊ฐ™์€ ๊ทธ๊ฒƒ)๋Š” ์—„๋ฐ€ํžˆ๋Š” ๋ถˆํ•„์š”ํ•˜๋‹ค. multiplyList์— ์ธ์ž๋ฅผ ํ•˜๋‚˜๋งŒ ์ ์šฉํ•ด๋„ ํ‰๊ฐ€๊ฐ€ ์‹คํŒจํ•˜์ง€ ์•Š์œผ๋ฉฐ, ๋‹จ์ง€ ์ตœ์ข… [Integer] ๊ฐ’์œผ๋กœ ์ข…๊ฒฐํ•˜๋Š” ๋Œ€์‹  [Integer] -> [Integer] ํƒ€์ž…์˜ ์ข€ ๋” ๊ตฌ์ฒด์ ์ธ ํ•จ์ˆ˜๋ฅผ ์šฐ๋ฆฌ์—๊ฒŒ ๋Œ๋ ค์ค€๋‹ค. ์œ„์˜ ๊ธฐ๊ต๋Š” ํ•˜์Šค์ผˆ์˜ ํ•จ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ๊ฐ’๋“ค์ฒ˜๋Ÿผ ํ–‰๋™ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์‹œ์‚ฌํ•œ๋‹ค. ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, ์ธ์ž๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š๊ณ  ๋…๋ฆฝ ๊ฐœ์ฒด๋กœ์„œ ์กด์žฌํ•˜๋Š” ํ•จ์ˆ˜๋ผ. ํ•จ์ˆ˜๊ฐ€ ๋งˆ์น˜ ์ผ๋ฐ˜์ ์ธ ์ƒ์ˆ˜์ธ ๊ฒƒ๋งŒ ๊ฐ™๋‹ค. ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ์ธ์ž๋กœ ์“ธ ์ˆ˜๋„ ์žˆ์„๊นŒ? ์ด๊ฒƒ์ด multiplyList์— ๊ด€ํ•œ ์šฐ๋ฆฌ์˜ ๋”œ๋ ˆ๋งˆ์˜ ์—ด์‡ ๋‹ค. ์šฐ๋ฆฌ์—๊ฒ ๊ณฑ์…ˆ๋ฟ ์•„๋‹ˆ๋ผ ์ž„์˜์˜ ์ ์ ˆํ•œ ํ•จ์ˆ˜๋ฅผ ์ทจํ•ด ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋“ค์— ์ ์šฉํ•˜๋Š” ๊ทธ๋Ÿฐ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. applyToIntegers :: (Integer -> Integer) -> [Integer] -> [Integer] applyToIntegers _ [] = [] applyToIntegers f (n:ns) = (f n) : applyToIntegers f ns applyToIntegers๋ฅผ ๊ฐ€์ง€๊ณ  ์ž„์˜์˜ Integer -> Integer ํ•จ์ˆ˜๋ฅผ ์ทจํ•ด ๊ทธ ํ•จ์ˆ˜๋ฅผ Integer์˜ ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋“ค์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก  ์ด ์ผ๋ฐ˜ํ™”๋œ ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  multiplyList๋ฅผ ์žฌ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํŠนํžˆ applyToIntegers์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋กœ (*) ํ•จ์ˆ˜๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋‹ค. multiplyList :: Integer -> [Integer] -> [Integer] multiplyList m = applyToIntegers ((*) m) ์—ฌ๊ธฐ์„œ๋Š” (*) ํ•จ์ˆ˜์— ์ธ์ž๋ฅผ ํ•˜๋‚˜๋งŒ ๋„˜๊ฒจ์„œ ๋‹ค๋ฅธ ์ธ์ž๋ฅผ ๋ฐ›์„ ์ค€๋น„๊ฐ€ ๋œ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. (์ด ๊ฒฝ์šฐ ๊ทธ ์ธ์ž๋Š” ์ฃผ์–ด์ง„ ๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ์ˆ˜๋“ค๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ๋‹ค) ์ปค๋ง ์ด ๋ชจ๋“  ์ถ”์ƒํ™”๊ฐ€ ํ—ท๊ฐˆ๋ฆฐ๋‹ค๋ฉด ๊ตฌ์ฒด์ ์ธ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž. ํ•˜์Šค์ผˆ์—์„œ 5 * 7์„ ํ•  ๋•Œ, (*) ํ•จ์ˆ˜๋Š” ๋‘ ์ธ์ž๋ฅผ ํ•œ๊บผ๋ฒˆ์— ์ทจํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์‚ฌ์‹ค์€ ๋จผ์ € 5๋ฅผ ์ทจํ•ด์„œ 5*๋ผ๋Š” ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋Œ๋ ค์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋Š” ํ•˜๋‚˜์˜ ์ธ์ž๋ฅผ ์ทจํ•ด 5์— ๊ณฑํ•œ๋‹ค. ์ฆ‰ ์šฐ๋ฆฌ๋Š” 5* ํ•จ์ˆ˜์— 7์„ ์ „๋‹ฌํ•ด์„œ ๊ทธ๊ฒƒ์ด ์ตœ์ข… ํ‰๊ฐ€๋œ ์ˆ˜(35)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰ ํ•˜์Šค์ผˆ์˜ ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ์‚ฌ์‹ค ์ธ์ž๋ฅผ ํ•˜๋‚˜๋งŒ ์ทจํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ์ตœ์ข… ๊ฒฐ๊ณผ์— ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์ค‘๊ฐ„ ํ•จ์ˆ˜๋“ค์ด ์ƒ์„ฑ๋ ์ง€ ์•Œ๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์šฐ๋ฆฌ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋งˆ์น˜ ์—ฌ๋Ÿฌ ์ธ์ž๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์ทจ๊ธ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํ†ต์ƒ ๋งํ•˜๋Š” ํ•จ์ˆ˜์˜ ์ธ์ž ๊ฐœ์ˆ˜๋Š” ์‚ฌ์‹ค ์ฒซ ๋ฒˆ์งธ ์ธ์ž์™€ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’ ์‚ฌ์ด์˜ ์ผ์ธ์ž ํ•จ์ˆ˜์˜ ๊ฐœ์ˆ˜๋‹ค. ๋ณต์žกํ•œ ํ•จ์ˆ˜์— ์ธ์ž๋“ค์„ ๊ณต๊ธ‰ํ•  ๋•Œ ์ค‘๊ฐ„ ํ•จ์ˆ˜๋“ค์„ ์ƒ์„ฑํ•˜๋Š” ์ด ๊ณผ์ •์„ ์ปค๋งcurrying์ด๋ผ ๋ถ€๋ฅธ๋‹ค(ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€ ๋™๋ช…์ด๊ธฐ๋„ ํ•œ ํ•˜์Šค ์ผˆ ์นด๋ ˆ(Haskell Curry)์˜ ์ด๋ฆ„์„ ๋”ด ๊ฒƒ์ด๋‹ค). map ํ•จ์ˆ˜ applyToIntegers์˜ ํƒ€์ž…์ด (Integer -> Integer) -> [Integer] -> [Integer]์ด๊ธด ํ•˜์ง€๋งŒ ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์—๋Š” ์ •์ˆ˜์— ํ•œ์ •๋˜๋Š” ๊ทธ๋Ÿฐ ๊ฒŒ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋งŒ ๋ฐ”๊ฟ”์„œ applyToChars, applyToStrings, applyToLists ๊ฐ™์€ ๊ฒƒ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฑด ์ •๋ง ๋‚ญ๋น„๋‹ค. ๊ฐ ํƒ€์ž…๋งˆ๋‹ค ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋ ค๊ณ  ์—ฌ๊ธฐ๊นŒ์ง€ ๊ธฐ์–ด์˜ฌ๋ผ์˜จ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค! ๊ฒŒ๋‹ค๊ฐ€ ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ๊ฐ€๋ น (Integer -> String) -> [Integer] -> [String]์œผ๋กœ ๋ฐ”๊ฟ”์„œ, ํ•จ์ˆ˜๊ฐ€ Integer -> String ํƒ€์ž…์˜ ํ•จ์ˆ˜๋ฅผ ์ทจํ•ด Integer ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ๊ฐ์˜ ์›์†Œ์— ์ ์šฉ๋  [Integer] -> [String] ํƒ€์ž…์˜ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ๋„ ์•„๋ฌด ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. ๊ทธ๋ฆฌํ•˜์—ฌ ์ผ๋ฐ˜ํ™”์˜ ๋ํŒ์€ applyToIntegers์˜ ์™„์ „ํžˆ ๋‹คํ˜•์„ฑ์ธ ๋ฒ„์ „์œผ๋กœ ์‹œ๊ทธ๋„ˆ์ณ๊ฐ€ (a -> b) -> [a] -> [b]์ธ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ ํ•จ์ˆ˜๊ฐ€ Prelude์— ์ด๋ฏธ ์žˆ๋‹ค. map์ด๋ผ๋Š” ํ•จ์ˆ˜๋กœ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋œ๋‹ค. map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = (f x) : map f xs map์„ ์ด์šฉํ•˜๋ฉด ๋ณ„ ๋…ธ๋ ฅ์„ ๋“ค์ด์ง€ ์•Š๊ณ  ํ•จ์ˆ˜๋“ค์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. multiplyList :: Integer -> [Integer] -> [Integer] multiplyList m = map ((*) m) ...๊ทธ๋ฆฌ๊ณ ... heads :: [[a]] -> [a] heads = map head Prelude> heads [[1,2,3,4],[4,3,2,1],[5,10,15]] [1,4,5] map์€ ํ•œ ํ•จ์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์›์†Œ์— ์ ์šฉํ•˜๋Š” ์ด์ƒ์ ์ธ ๋ฒ”์šฉ ํ•ด๋ฒ•์ด๋‹ค. ๊ธฐ์กด์˜ doubleList ๋ฌธ์ œ๋Š” map์˜ ์•„์ฃผ ๊ตฌ์ฒด์ ์ธ ํ•œ ๋ฒ„์ „์ผ ๋ฟ์ด์—ˆ๋‹ค. map์ฒ˜๋Ÿผ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ทจํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ณ ์ฐจ ํ•จ์ˆ˜๋ผ ๋ถ€๋ฅด๊ณ , ์ด๊ฒƒ์€ ์•„์ฃผ ์œ ์šฉํ•˜๋‹ค. ํŠนํžˆ ๋‹ค์Œ ์žฅ์—์„œ ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ์— ์“ฐ์ด๋Š” ๋‹ค๋ฅธ ์ค‘์š”ํ•œ ๊ณ ์ฐจ ํ•จ์ˆ˜๋“ค์„ ๋ช‡ ๊ฐœ ๋งŒ๋‚  ๊ฒƒ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ map์„ ์ด์šฉํ•˜์—ฌ, Int์˜ ๋ฆฌ์ŠคํŠธ xs๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋‹ค์Œ์„ ๋ฐ˜ํ™˜ํ•˜๋ผ. xs์˜ ์›์†Œ๋“ค์„ ๊ฐ๊ธฐ ๋ฐ˜์ „ํ•œ ๋ฆฌ์ŠคํŠธ xs์˜ ๊ฐ ์›์†Œ์— ๋Œ€ํ•œ ์ธ์ˆ˜๋“ค์„ ํฌํ•จํ•˜๋Š”, Int์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ xss. ์ธ์ˆ˜๋“ค์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. divisors p = [ f | f <- [1.. p], pmodf == 0 ] xss์˜ ์›์†Œ๋ณ„ ๋ฐ˜์ „ ์—ฐ์† ๊ธธ์ด ๋ถ€ํ˜ธํ™”(Run Length Encoding; RLE)์˜ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. RLE์˜ ๋ฐœ์ƒ์€ ๊ฐ„๋‹จํ•˜๋‹ค. ์–ด๋–ค ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด "aaaabbaaa" ๊ฐ ๋ฌธ์ž๊ฐ€ ์—ฐ์†๋˜๋Š” ๊ธธ์ด๋ฅผ ์ทจํ•ด ์••์ถ•ํ•œ๋‹ค. (4, 'a'), (2, 'b'), (3, 'a') concat ํ•จ์ˆ˜์™€ group ํ•จ์ˆ˜๊ฐ€ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ๋‹ค. group์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Data.List ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•œ๋‹ค. ghci ํ”„๋กฌํ”„ํŠธ์—์„œ :m Data.List์„ ์น˜๊ฑฐ๋‚˜ ํ•˜์Šค ์ผˆ ์†Œ์Šค ์ฝ”๋“œ ํŒŒ์ผ์— import Data.List์„ ์ถ”๊ฐ€ํ•ด์„œ ์ด ๋ชจ๋“ˆ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. encode ํ•จ์ˆ˜์™€ decode ํ•จ์ˆ˜์˜ ํƒ€์ž…์€ ๋ฌด์—‡์ธ๊ฐ€? ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ(๊ฐ€๋ น [(4, 'a'), (6, 'b')])๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฌธ์ž์—ด(๊ฐ€๋ น "4a6b")์œผ๋กœ ๋ณ€ํ™˜ํ•  ๊ฒƒ์ธ๊ฐ€? (๋ณด๋„ˆ์Šค) ์›๋ž˜ ๋ฌธ์ž์—ด์— ์ˆซ์ž์ธ ๋ฌธ์ž๊ฐ€ ๊ธˆ์ง€๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ๊ทธ ๋ฌธ์ž์—ด์„ ์–ด๋–ป๊ฒŒ ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋‹ค์‹œ ํŒŒ์‹ฑ ํ•  ๊ฒƒ์ธ๊ฐ€? ํŒ๊ณผ ํŠธ๋ฆญ ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ๋” ๋ฐฐ์šฐ๊ธฐ ์ „์— ํ•˜์Šค์ผˆ์˜ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์œ ์šฉํ•œ ๋ช‡ ๊ฐ€์ง€ ์žก๋‹คํ•œ ์ ์„ ๊ด€์ฐฐํ•ด ๋ณด์ž. ์ด์ค‘์  ํ‘œ๊ธฐ(Dot Dot Notation) ํ•˜์Šค์ผˆ์—๋Š” ๊ท ์ผํ•˜๊ฒŒ ๋–จ์–ด์ง„ ์ •์ˆ˜๋“ค์˜ ์ •๋ ฌ๋œ ๋ชฉ๋ก์„ ์ž‘์„ฑํ•˜๋Š” ํŽธ๋ฆฌํ•œ ๋‹จ์ถ•๋ฒ•์ด ์žˆ๋‹ค. ๊ทธ ์˜ˆ๋กœ, ์ฝ”๋“œ ๊ฒฐ๊ณผ ---- ------ [1.. 10] [1,2,3,4,5,6,7,8,9,10] [2,4.. 10] [2,4,6,8,10] [5,4.. 1] [5,4,3,2,1] [1,3.. 10] [1,3,5,7,9] ๋ฌธ์ž์—๋„ ๊ฐ™์€ ํ‘œ๊ธฐ๋ฅผ ์“ธ ์ˆ˜ ์žˆ๊ณ  ์‹ฌ์ง€์–ด ๋ถ€๋™์†Œ์ˆ˜์  ์ˆ˜๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฐ˜์˜ฌ๋ฆผ ์˜ค์ฐจ ๋•Œ๋ฌธ์— ์ข‹์€ ์ƒ๊ฐ์€ ์•„๋‹ˆ์ง€๋งŒ. ์ด๊ฒƒ์„ ์‹œ๋„ํ•ด ๋ณด๋ผ. [0,0.1 .. 1] ์ž ๊น .. ํ‘œ๊ธฐ๋Š” ์˜ค์ง ์—ฐ์†๋œ ์›์†Œ๋“ค์˜ ์ฐจ์ด๊ฐ€ ์ผ์ •ํ•  ๋•Œ๋งŒ ์ž‘๋™ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ๋Š” ์“ธ ์ˆ˜ ์—†๋‹ค... [0,1,1,2,3,5,8.. 100] ์ด๋ž˜๋†“๊ณ  ํ”ผ๋ณด๋‚˜์น˜์ˆ˜์—ด์˜ ๋‚˜๋จธ์ง€๋ฅผ ๋Œ๋ ค๋ฐ›๋Š” ๋งˆ๋ฒ•์„ ๊ธฐ๋Œ€ํ•  ์ˆœ ์—†๋‹ค. 5 ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ ํ•˜์Šค ์ผˆ ๋ฆฌ์ŠคํŠธ์— ๊ด€ํ•ด ๊ฐ€์žฅ ๋‡Œ๊ฐ€ ๊ผฌ์ด๋Š” ์ ์€ ๋ฌดํ•œํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ์€ 1๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ •์ˆ˜๋“ค์˜ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. [1..] (GHCi์—์„œ ์ด๊ฑธ ์‹œ๋„ํ•œ๋‹ค๋ฉด, Ctrl-c๋กœ ํ‰๊ฐ€๋ฅผ ๋ฉˆ์ถœ ์ˆ˜ ์žˆ์Œ์„ ๊ธฐ์–ตํ•˜๋ผ.) ์žฌ๊ท€ ํ•จ์ˆ˜๋กœ๋„ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. intsFrom n = n : intsFrom (n+1) -- ๊ธฐ๋ณธ ๊ฐ€์ •์ด ์—†๋‹ค! positiveInts = intsFrom 1 ์ด๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” ํ•˜์Šค์ผˆ์ด ์ง€์—ฐ ํ‰๊ฐ€๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์Šค์ผˆ์€ ์–ด๋Š ์‹œ์ ์—๋“  ํ•„์š”ํ•œ ๊ฒƒ ์ด์ƒ์œผ๋กœ ํ‰๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ‰๋ฒ”ํ•œ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์€ ํ‰๊ฐ€๊ฐ€ ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ๊ฐ’์„ ์š”๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์ง„๋‹ค. ๋ฆฌ์ŠคํŠธ ์ „์ฒด๋ฅผ ์ •๋ ฌํ•˜๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์ด ๊ทธ ์˜ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฒƒ์€ "evens"์„ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ [2,4,6,8...]์œผ๋กœ ์ •์˜ํ•œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ "evens"๋ฅผ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๊ณ , ์ตœ์ข… ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ๋ฆฌ์ŠคํŠธ์˜ ์ผ๋ถ€๋งŒ ํ‰๊ฐ€ํ•ด๋„ ๋˜๋ฉด ์ž˜ ์ž‘๋™ํ•œ๋‹ค. ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋Š” ํ•˜์Šค์ผˆ์—์„œ ์ƒ๋‹นํžˆ ์“ธ๋ชจ ์žˆ๋‹ค. ๊ฐ€๋”์€ ์œ ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š๋‹ˆ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •์˜ํ•˜๊ณ  ์ฒ˜์Œ์˜ ๋ช‡ ํ•ญ๋ชฉ์„ ์ทจํ•˜๋Š” ๊ฒƒ์ด ๋” ํŽธํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋Œ€๊ฐœ ์งง์€ ๊ฒƒ์— ๋งž์ถฐ ๋ฉˆ์ถ”๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฌดํ•œ์œผ๋กœ ๋งŒ๋“ค๋ฉด ์ฒซ ๋ฒˆ์งธ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋Š” ์ƒํ˜ธ์ž‘์šฉํ˜• ํ”„๋กœ๊ทธ๋žจ์˜ ์ตœ์ƒ์œ„์—์„œ ์ „ํ†ต์ ์ธ ๋ฌดํ•œ ๋ฃจํ”„์˜ ๊ฐ„ํŽธํ•œ ๋Œ€์ฒด์žฌ๊ฐ€ ๋˜๊ธฐ๋„ ํ•œ๋‹ค. head์™€ tail์— ๊ด€ํ•œ ๋…ธํŠธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ชผ๊ฐœ๊ธฐ ์œ„ํ•ด (:) ํŒจํ„ด๊ณผ head/tail ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์•ผ ํ•  ๋•Œ๋Š” ๋Œ€๊ฐœ ํŒจํ„ด ๋งค์นญ์ด ์„ ํ˜ธํ•  ๋งŒํ•˜๋‹ค. head์™€ tail์˜ ๋‹จ์ˆœํ•จ๊ณผ ๊ฐ„๋ช…ํ•จ์€ ์œ ํ˜น์Šค๋Ÿฝ์ง€๋งŒ ์ด๊ฒƒ๋“ค์ด ๋นˆ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์‹คํŒจํ•œ๋‹ค๋Š” ์ ์„ ๊นŒ๋จน๊ธฐ๋Š” ๋„ˆ๋ฌด ์‰ฝ๊ณ  ๋Ÿฐํƒ€์ž„ ํฌ๋ž˜์‹œ๋Š” ์ „ํ˜€ ์ข‹์€ ์ผ์ด ์•„๋‹ˆ. Prelude ํ•จ์ˆ˜ null :: [a] -> Bool์ด ํŒจํ„ด ๋งค์นญ ์—†์ด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฑด์ „ํ•œ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•˜์ง€๋งŒ(null์€ ๋นˆ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด True๋ฅผ, ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค), ๋นˆ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ์€ ๊ทธ์— ๋Œ€์‘๋˜๋Š” if-then-else ํ‘œํ˜„์‹๋ณด๋‹ค ๋” ๊น”๋”ํ•˜๊ณ  ๊นจ๋—ํ•œ ๊ฒฝํ–ฅ์ด ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์ด ์žฅ์˜ ์ฒซ ๋ฒˆ์งธ ์—ฐ์Šต๋ฌธ์ œ ๋ชจ์Œ์— ๋Œ€ํ•œ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ•ด๋‹ต๊ณผ ๊ด€๋ จํ•˜์—ฌ, scanSum (takeInt 10 [1..])๊ณผ takeInt 10 (scanSum [1..])์— ์ฐจ์ด๊ฐ€ ์žˆ์„๊นŒ? ๋ฆฌ์ŠคํŠธ์— ์ ์šฉํ•˜๋ฉด ๋งˆ์ง€๋ง‰ ์›์†Œ์™€ ๊ทธ ์›์†Œ๋ฅผ ๋บ€ ๋‚˜๋จธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. ์ด ๊ธฐ๋Šฅ์€ Prelude๊ฐ€ last์™€ init ํ•จ์ˆ˜๋กœ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด๋‹ค. head์™€ tail์ฒ˜๋Ÿผ ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์œผ๋ฉด ํญ๋ฐœํ•œ๋‹ค. ๊ธฐ๋ณธ ๊ฐ€์ •์„ ๋นผ๋จน์œผ๋ฉด ์žฌ๊ท€๊ฐ€ ๋นˆ ๋ฆฌ์ŠคํŠธ์— ๋‹ค๋‹ค๋ž์„ ๋•Œ (x:xs) ํŒจํ„ด ๋งค์นญ์ด ์‹คํŒจํ•˜๊ณ  ์šฐ๋ฆฌ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค. โ†ฉ ๊ณ„์‚ฐ ์ค‘ ์˜ค๋ฅ˜์ด๊ฑฐ๋‚˜ ๋๋‚˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์—†๊ธฐ๋งŒ ํ•œ๋‹ค๋ฉด. ์ด ๊ฒฝ์šฐ์—” ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค. โ†ฉ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํšจ์œจ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ž์œ ๋กœ์ด ์ด๊ฒƒ๋“ค์„ ์ฆ‰์‹œ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. โ†ฉ ํ•œ ๊ฐ€์ง€ ์˜ˆ์™ธ๋Š” ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ์ธ ๊ฒฝ์šฐ๋กœ(!) ๊ณง ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. โ†ฉ http://en.wikipedia.org/wiki/Fibonacci_number โ†ฉ 3 ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/List_processing ์ ‘๊ธฐ(fold) foldr foldl foldr1๊ณผ foldl1 ์ ‘๊ธฐ์™€ ์ง€์—ฐ์„ฑ scan ๊ฑธ๋Ÿฌ๋‚ด๊ธฐ(filter) ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹(list comprehension) ์ ‘๊ธฐ(fold) ์ ‘๊ธฐ๋Š” map์ฒ˜๋Ÿผ ํ•จ์ˆ˜์™€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•˜๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜๋‹ค. ํ•˜์ง€๋งŒ ํ•จ์ˆ˜๋ฅผ ์›์†Œ๋งˆ๋‹ค ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ด ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ ์›์†Œ๋“ค์„ ๋‹จ์ผ ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ํ•ฉ์„ฑํ•œ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ๊ตฌ์ฒด์ ์ธ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉฐ ์‹œ์ž‘ํ•˜์ž. sum ๊ฐ™์€ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: sum sum :: [Integer] -> Integer sum [] = 0 sum (x:xs) = x + sum xs ๋˜๋Š” product. ์˜ˆ: product product :: [Integer] -> Integer product [] = 1 product (x:xs) = x * product xs ๋˜๋Š” concat. ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด ํ•˜๋‚˜๋กœ ์—ฐ๊ฒฐํ•œ๋‹ค. ์˜ˆ: concat concat :: [[a]] -> [a] concat [] = [] concat (x:xs) = x ++ concat xs ์ด ์˜ˆ์‹œ๋“ค์—๋Š” ๋ชจ๋‘ ์–ด๋–ค ๊ณตํ†ต๋œ ์žฌ๊ท€ ํŒจํ„ด์ด ์žˆ๋‹ค. ์ด ํŒจํ„ด์€ ์ ‘๊ธฐ๋ผ๋Š” ๊ฒƒ์œผ๋กœ, ์•„๋งˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ "๊ผฌ๊นƒ๊ผฌ๊นƒ ์ ‘๋Š”๋‹ค"๊ฑฐ๋‚˜ ํ•จ์ˆ˜๊ฐ€ ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋“ค "์‚ฌ์ด์—์„œ ๊ฐ์‹ธ์ง„๋‹ค"๋ผ๋Š” ๋ฐœ์ƒ์—์„œ ๋‚˜์˜จ ๊ฒƒ ๊ฐ™๋‹ค. ํ‘œ์ค€ Prelude์—๋Š” ๋„ค ๊ฐœ์˜ fold ํ•จ์ˆ˜ foldr, foldl, foldr1, foldl1์ด ์ •์˜๋˜์–ด ์žˆ๋‹ค. foldr ์šฐ๊ฒฐํ•ฉ์„ฑ์ธ foldr์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ ‘๋Š”๋‹ค. ์ง„ํ–‰ํ•จ์— ๋”ฐ๋ผ foldr์€ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ ์›์†Œ๋ฅผ ๋ˆ„์‚ฐ ๊ธฐ๋ผ๋Š” ๊ณ„์†๋˜๋Š” ๊ฐ’๊ณผ ๊ฒฐํ•ฉํ•œ๋‹ค. foldr์„ ํ˜ธ์ถœํ•  ๋•Œ ๋ˆ„์‚ฐ๊ธฐ์˜ ์ดˆ๊นƒ๊ฐ’์ด ์ธ์ž๋กœ ์„ค์ •๋œ๋‹ค. foldr :: (a -> b -> b) -> b -> [a] -> b foldr f acc [] = acc foldr f acc (x:xs) = f x (foldr f acc xs) ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ์ธ์ž๊ฐ€ ๋‘ ๊ฐœ์ธ ํ•จ์ˆ˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋ˆ„์‚ฐ๊ธฐ์˜ "์˜" ๊ฐ’, ์„ธ ๋ฒˆ์งธ๋Š” ์ ‘์–ด๋ฒ„๋ฆด ๋ฆฌ์ŠคํŠธ๋‹ค. sum์—์„œ f๋Š” (+), acc๋Š” 0์ด๊ณ  concat์—์„œ f๋Š” (++), acc๋Š” []๋‹ค. ์•ž์˜ ์˜ˆ์ œ๋“ค์ฒ˜๋Ÿผ ์ ‘๊ธฐ์— ์ „๋‹ฌ๋˜๋Š” ํ•จ์ˆ˜์˜ ๋‘ ์ธ์ž๋Š” ๋Œ€๊ฐœ ๊ฐ™์€ ํƒ€์ž…์ด์ง€๋งŒ ๊ผญ ๊ทธ๋ž˜์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. foldr f acc xs๊ฐ€ ํ•˜๋Š” ์ผ์€ xs ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ๊ฐ์˜ cons (:)์„ ํ•จ์ˆ˜ f๋กœ ๋Œ€์ฒดํ•˜๊ณ  ๋งˆ์ง€๋ง‰์˜ ๋นˆ ๋ฆฌ์ŠคํŠธ๋Š” acc๋กœ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ์ด๋‹ค. a : b : c : [] ๋Š” ์ด๋ ‡๊ฒŒ ๋œ๋‹ค. f a (f b (f c acc)) ๊ด„ํ˜ธ๋“ค์ด ๋ฆฌ์ŠคํŠธ์˜ ๋์—์„œ ์–ด๋–ป๊ฒŒ ์ค‘์ฒฉ๋˜๋Š”์ง€์— ์ฃผ๋ชฉํ•˜๋ผ. ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ํŠธ๋ฆฌ๋กœ ๋ฌ˜์‚ฌํ•˜๋ฉด ์šฐ์•„ํ•œ ์‹œ๊ฐํ™”๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. : f / \ / \ a : foldr f acc a f / \ -------------> / \ b : b f / \ / \ c [] c acc ์—ฌ๊ธฐ์„œ foldr (:) []์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์™„์ „ํžˆ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์•„๋ฌด ์˜ํ–ฅ์ด ์—†๋Š” ์ด๋Ÿฐ ์œ ์˜ ํ•จ์ˆ˜๋ฅผ ํ•ญ๋“ฑ ํ•จ์ˆ˜๋ผ ํ•œ๋‹ค. ์—ฌ๋Ÿฌ ๊ฒฝ์šฐ์—์„œ ํ•ญ๋“ฑ ํ•จ์ˆ˜๋ฅผ ์ฐพ์•„๋ณด๋Š” ์Šต๊ด€์„ ๊ธธ๋Ÿฌ์•ผ ํ•˜๋ฉฐ ์ด์— ๊ด€ํ•ด์„œ๋Š” ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ๋ฐฐ์šธ ๋•Œ ๋” ๋…ผํ•  ๊ฒƒ์ด๋‹ค. foldl ์ขŒ ๊ฒฐํ•ฉ์„ฑ foldl์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์„œ, ์ขŒ์ธก์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. foldl :: (a -> b -> a) -> a -> [b] -> a foldl f acc [] = acc foldl f acc (x:xs) = foldl f (f acc x) xs ๊ฒฐ๊ณผ ํ‘œํ˜„์‹์˜ ๊ด„ํ˜ธ๋“ค์€ ๋ฆฌ์ŠคํŠธ์˜ ์™ผ์ชฝ ๋์— ์Œ“์ธ๋‹ค. ์œ„์˜ ๋ฆฌ์ŠคํŠธ๋Š” foldl f acc์— ์˜ํ•ด ๋ณ€ํ™˜๋œ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋œ๋‹ค. f (f (f acc a) b) c ์ด์— ๋Œ€์‘ํ•˜๋Š” ํŠธ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. : f / \ / \ a : foldl f acc f c / \ -------------> / \ b : f b / \ / \ c [] acc a ๋ชจ๋“  ์ ‘๊ธฐ๊ฐ€ ์™ผ์ชฝ ์›์†Œ์™€ ์˜ค๋ฅธ์ชฝ ์›์†Œ ๋‘˜ ๋‹ค๋ฅผ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธ‰์ž๋“ค์€ ๊ทธ ์ด๋ฆ„์ด ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ๋‹ค. foldr์€ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ ‘๊ธฐ์˜ ์ค„์ž„๋ง, foldl์€ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ ‘๊ธฐ์˜ ์ค„์ž„๋ง๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์ด๋ฆ„๋“ค์€ ์ ‘๊ธฐ๊ฐ€ ์‹œ์ž‘ํ•˜๋Š” ์ง€์ ์„ ์ง€์นญํ•œ๋‹ค. ์ž ๊น ๊ธฐ์ˆ ์  ์ธก๋ฉด์˜ ๊ธฐ๋ก: foldl์€ ๊ผฌ๋ฆฌ ์žฌ๊ท€๋‹ค. ์ฆ‰ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜์—ฌ ์ฆ‰์‹œ ์žฌ๊ท€ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์žฌ๊ท€๋ฅผ ๊ฐ„๋‹จํ•œ ๋ฃจํ”„๋กœ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋Š” ์ˆ˜ํ–‰๋Šฅ๋ ฅ ์ธก๋ฉด์—์„œ ์ข‹์€ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ํ•˜์Šค์ผˆ์€ ๊ฒŒ์œผ๋ฅธ ์–ธ์–ด๊ณ  f์— ๋Œ€ํ•œ ํ˜ธ์ถœ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ‰๊ฐ€๋˜์ง€ ์•Š์€ ์ฑ„ ๋‚จ๊ฒจ์ง€๊ธฐ ๋•Œ๋ฌธ์—, ๋ฆฌ์ŠคํŠธ์˜ ์ „์ฒด๋ฅผ ํฌํ•จํ•˜๋Š”, ๋ฏธํ‰๊ฐ€๋œ ํ‘œํ˜„์‹์„ ๋ฉ”๋ชจ๋ฆฌ์— ์–น์–ด๋†“๊ฒŒ ๋œ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค f๋ฅผ ์ฆ‰๊ฐ ํ‰๊ฐ€ํ•˜๋„๋ก ๊ฐ•์ œํ•˜๋Š” foldl'์ด๋ผ๋Š” ์ ๊ทน์ ์ธ ์ ‘๊ธฐ๊ฐ€ ์žˆ๋‹ค. ํ•จ์ˆ˜ ์ด๋ฆ„ ๋์˜ ๋”ฐ์˜ดํ‘œ๋Š” "ํ‹ฑ"์ด๋ผ๊ณ  ๋ฐœ์Œํ•˜๊ณ  ํ•จ์ˆ˜๋Š” "ํด๋“œ-์—˜-ํ‹ฑ"์ด๋‹ค. ํ‹ฑ์€ ํ•˜์Šค ์ผˆ ์‹๋ณ„์ž๋กœ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž๋‹ค. foldl'์€ Data.List ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค(์†Œ์Šค ํŒŒ์ผ์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— import Data.List์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋“ค์—ฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค). ๊ฒฝํ—˜์ƒ foldr์€ ๋ฌดํ•œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ ํ˜น์€ ์ ‘๊ธฐ๊ฐ€ ์ž๋ฃŒ ๊ตฌ์กฐ ๊ตฌ์ถ• ์ˆ˜๋‹จ์ผ ๋•Œ ์“ฐ๊ณ , foldl'์€ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์œ ํ•œํ•˜๊ณ  ๋‹จ์ผ ๊ฐ’์œผ๋กœ ์ ‘ํž ๋•Œ ์“ฐ๋Š” ๊ฒŒ ์ข‹๋‹ค. ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์ง€ ์•Š์œผ๋ฉด ์ž‘๋™์€ ํ•˜๊ฒ ์ง€๋งŒ, foldl(ํ‹ฑ ์—†๋Š”)์„ ์“ธ ์ข‹์€ ์ด์œ ๋Š” ์ „ํ˜€ ์—†๋‹ค. foldr1๊ณผ foldl1 ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด foldr์˜ ํƒ€์ž… ์„ ์–ธ์€ ๋ฆฌ์ŠคํŠธ ์›์†Œ๋“ค๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ํƒ€์ž…์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๊ฐ€๋Šฅ์ผ€ ํ•œ๋‹ค. ๊ฐ€๋ น read๋Š” ๋ฌธ์ž์—ด์„ ์ทจํ•ด ๋ชจ์ข…์˜ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋‹ค(ํƒ€์ž… ์ฒด๊ณ„๋Š” ๊ทธ๊ฒŒ ์–ด๋–ค ํƒ€์ž…์ธ์ง€ ์•Œ์•„๋‚ผ ๋งŒํผ ์ถฉ๋ถ„ํžˆ ๋˜‘๋˜‘ํ•˜๋‹ค). ๋‹ค์Œ ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์ž์—ด์„ ๋ถ€๋™์†Œ์ˆ˜์  ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ์˜ˆ: ๋ฆฌ์ŠคํŠธ ์›์†Œ๋“ค๊ณผ ๊ฒฐ๊ด๊ฐ’์€ ๋‹ค๋ฅธ ํƒ€์ž…์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค addStr :: String -> Float -> Float addStr str x = read str + x sumStr :: [String] -> Float sumStr = foldr addStr 0.0 foldr์˜ ํƒ€์ž…์—์„œ ํƒ€์ž… ๋ณ€์ˆ˜ a์™€ b๋ฅผ Float๊ณผ String์œผ๋กœ ์น˜ํ™˜ํ•˜๋ฉด ์ด ํƒ€์ž…์ด ์˜ฌ๋ฐ”๋ฅด๋‹จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. foldr1("ํด๋“œ-์•Œ-์›")์ด๋ž€ ๋ณ€์ข…๋„ ์žˆ๋Š”๋ฐ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ทจํ•˜์—ฌ ์˜๊ฐ’์„ ์—†์•ค๋‹ค. foldr1 :: (a -> a -> a) -> [a] -> a foldr1 f [x] = x foldr1 f (x:xs) = f x (foldr1 f xs) foldr1 _ [] = error "Prelude.foldr1: empty list" ๊ทธ๋ฆฌ๊ณ  foldl1๋„ ์žˆ๋‹ค. foldl1 :: (a -> a -> a) -> [a] -> a foldl1 f (x:xs) = foldl f x xs foldl1 _ [] = error "Prelude.foldl1: empty list" ์ž ๊น: foldl์ฒ˜๋Ÿผ, Data.List ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” foldl1์˜ ์ ๊ทน์ ์ธ ๋ฒ„์ „ foldl1'์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. foldl1๊ณผ foldr1์—์„  ๋ชจ๋“  ํƒ€์ž…์ด ๊ฐ™์•„์•ผ ํ•˜๊ณ  ๋นˆ ๋ฆฌ์ŠคํŠธ๋Š” ์˜ค๋ฅ˜๋‹ค. ์ด ๋ณ€์ข…๋“ค์€ ์ดˆ๊ธฐ ๋ˆ„์‚ฐ ๊ฐ’์œผ๋กœ ๋งˆ๋•…ํ•œ ํ›„๋ณด๊ฐ€ ์—†๊ณ  ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ์„ ์ผ์ด ์—†๋‹ค๊ณ  ํ™•์‹ ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ์˜์‹ฌ์˜ ์—ฌ์ง€๊ฐ€ ์žˆ๋‹ค๋ฉด foldr๊ณผ foldl'์„ ๊ณ ์ˆ˜ํ•˜๋ผ. ์ ‘๊ธฐ์™€ ์ง€์—ฐ์„ฑ ์šฐ๊ฒฐํ•ฉ์„ฑ ์ ‘๊ธฐ๊ฐ€ ํ•˜์Šค์ผˆ์—์„œ ์ขŒ ๊ฒฐํ•ฉ์„ฑ ์ ‘๊ธฐ๋ณด๋‹ค ์ž์—ฐ์Šค๋Ÿฌ์šด ์ด์œ ๋Š” ์˜ค๋ฅธ์ชฝ ์ ‘๊ธฐ๋Š” ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ๊ธฐ๋Šฅํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ ‘๊ธฐ๋Š” ๋ฌดํ•œํ•œ ๊ฒฐ๊ณผ ์ „์ฒด์— ์ ‘๊ทผํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๋” ํฐ ๋ฌธ๋งฅ์—์„œ ์ด์šฉํ•˜๊ธฐ์— ์™„๋ฒฝํ•˜๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ foldr์€ ํ•„์š”ํ•œ ๋งŒํผ ์–ผ๋งˆ๋“ ์ง€ ๋‚˜์•„๊ฐˆ ์ˆ˜ ์žˆ๊ณ  ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋‚˜๋จธ์ง€๋Š” ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์™ผ์ชฝ ์ ‘๊ธฐ๋Š” ์ž…๋ ฅ ๋ฆฌ์ŠคํŠธ์˜ ๋์— ๋‹ค๋‹ค๋ฅผ ๋•Œ๊นŒ์ง€ ์ž๊ธฐ ์ž์‹ ์„ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค(์žฌ๊ท€ ํ˜ธ์ถœ์ด f์˜ ์ธ์ž์—์„œ๋Š” ์ด๋ฃจ์–ด์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ). ๋งํ•  ํ•„์š”๋„ ์—†์ด foldl์˜ ์ž…๋ ฅ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฌดํ•œํ•˜๋ฉด ๊ทธ ๋์€ ๋‹ค๋‹ค๋ฅผ ์ˆ˜ ์—†๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋กœ ์ •์ˆ˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด ์ˆซ์ž n์„ n ๋ฒˆ ๋ณต์ œํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์‚ฐํ•˜๋Š” ํ•จ์ˆ˜ echoes๋ฅผ ๊ณ ๋ คํ•ด ๋ณด์ž. ์ด ๋ฉ”์•„๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด Prelude ํ•จ์ˆ˜ replicate์„ ์“ธ ๊ฒƒ์ด๋‹ค. replicate n x๋Š” ๋ชจ๋“  ์›์†Œ๊ฐ€ x์ธ ๊ธธ์ด n์˜ ๋ฆฌ์ŠคํŠธ๋‹ค. echoes๋Š” foldr๋กœ ๊ฝค ๊ฐ„๋‹จํžˆ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. echoes = foldr (\ x xs -> (replicate x x) ++ xs) [] ์ž ๊น: ์ด ์ •์˜๋Š” \ x xs -> ๊ตฌ๋ฌธ ๋•์— ๊ฝค ๊ฐ„๊ฒฐํ•˜๋‹ค. ๋žŒ๋‹ค(ฮป)์ฒ˜๋Ÿผ ์ƒ๊ธด \๋Š” ์ด๋ฆ„ ์—†๋Š” ํ•จ์ˆ˜์ด๋ฉฐ, ์šฐ๋ฆฌ๋Š” ์ด ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ๋ฐ์„œ๋Š” ์“ฐ์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์‹ ์•ˆ์— ์ผํšŒ์šฉ ํ•จ์ˆ˜๋ฅผ ๋‚ด์žฅํ•œ ๊ฒƒ์ด๋‹ค. x์™€ xs๋Š” ๋žŒ๋‹ค์˜ ์ธ์ž๊ณ , ์ •์˜์˜ ์šฐ๋ณ€์€ -> ๋’ค์— ์˜จ๋‹ค. ๋˜๋Š”, ์—ญ์‹œ ๊ฐ„ํŽธํ•œ foldl์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. echoes = foldl (\xs x -> xs ++ (replicate x x)) [] ํ•˜์ง€๋งŒ foldr ๋ฒ„์ „๋งŒ์ด [1..] ๊ฐ™์€ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ž‘๋™ํ•œ๋‹ค. ์‹œ๋„ํ•ด ๋ณด๋ผ! (GHCi์—์„œ ์‹œ๋„ํ•˜๋Š” ๊ฑฐ๋ผ๋ฉด Ctrl-c๋กœ ํ‰๊ฐ€๋ฅผ ๋ฉˆ์ถœ ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋‹ˆํ„ฐ์— ๋ˆˆ์„ ๋ถ™์ด๊ณ  ๋นจ๋ฆฌํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ˆœ์‹๊ฐ„์— ๋ฐ”๋‹ฅ๋‚˜ ์‹œ์Šคํ…œ์ด ๋ฉˆ์ถœ ๊ฒƒ์ด๋‹ค.) ์•Œ์•„์ฑ˜์„์ง€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ๋งˆ์ง€๋ง‰ ์˜ˆ์ œ๋Š” map ์ž์ฒด๋„ ์ ‘๊ธฐ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. map f = foldr (\x xs -> f x : xs) [] ์ ‘๊ธฐ๋Š” ์ต์ˆ™ํ•ด์ง€๋Š” ๋ฐ ๋ณ„๋กœ ์‹œ๊ฐ„์ด ์•ˆ ๊ฑธ๋ฆฌ์ง€๋งŒ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๊ทผ๋ณธ์ ์ธ ํŒจํ„ด์ด๋ฉฐ ๊ฒฐ๊ตญ ์•„์ฃผ ์ž์—ฐ์Šค๋Ÿฌ์›Œ์งˆ ๊ฒƒ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๊ณ  ๊ทธ ๊ตฌ์„ฑ์š”์†Œ๋“ค๋กœ๋ถ€ํ„ฐ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌ์ถ•ํ•˜๋ ค๋ฉด ์–ธ์ œ๋“ ์ง€ ์ ‘๊ธฐ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋‹ค์Œ ํ•จ์ˆ˜๋“ค์„(์•ž์˜ sum, product, concat์ฒ˜๋Ÿผ) ์žฌ๊ท€์ ์œผ๋กœ ์ •์˜ํ•œ ๋‹ค์Œ ์ ‘๊ธฐ๋กœ ๋ฐ”๊ฟ”๋ณด๋ผ. and :: [Bool] -> Bool. Bool์˜ ๋ฆฌ์ŠคํŠธ์˜ ์ „์ฒด๊ฐ€ True ๋ฉด True๋ฅผ, ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. or :: [Bool] -> Bool. Bool์˜ ๋ฆฌ์ŠคํŠธ์—์„œ ํ•˜๋‚˜๋ผ๋„ True ๋ฉด True๋ฅผ, ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. foldl1๊ณผ foldr1์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ํ•˜๋ผ. maximum :: Ord a => [a] -> a. ๋ฆฌ์ŠคํŠธ์˜ ์ตœ๋Œ€ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค(ํžŒํŠธ: max :: Ord a => a -> a -> a๋Š” ๋‘ ๊ฐ’ ์ค‘ ์ž‘์ง€ ์•Š์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค). minimum :: Ord a => [a] -> a. ๋ฆฌ์ŠคํŠธ์˜ ์ตœ์†Œ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค(ํžŒํŠธ: min :: Ord a => a -> a -> a์€ ๋‘ ๊ฐ’ ์ค‘ ํฌ์ง€ ์•Š์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค). ์ ‘๊ธฐ๋กœ(์–ด๋–ค ๋ฒ„์ „?) reverse :: [a] -> [a]๋ฅผ ์ •์˜ํ•˜๋ผ. ์›์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋’ค์ง‘์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๊ฒƒ๋“ค์€ ์ด๋ฏธ Prelude ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”ํ•˜๋ฉด ์–ธ์ œ๋“  ์†์— ๊ฐ€๊นŒ์ด ์žˆ๋‹ค. (์—ฌ๋Ÿฌ๋ถ„์˜ ํ•ด๋‹ต์„ GHCi์—์„œ ์‹œํ—˜ํ•˜๋ ค๋ฉด ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์จ์•ผ ํ•œ๋‹ค๋Š” ๋œป์ด๊ธฐ๋„ ํ•˜๋‹ค) scan "scan"์€ map๊ณผ ์ ‘๊ธฐ์˜ ๊ฒฝํ•ฉ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ๋ฅผ ์ ‘์œผ๋ฉด ๋‹จ์ผ ๊ฒฐ๊ณผ๋กœ ๋ˆ„์ ๋˜๋Š” ๋ฐ˜๋ฉด ํ•œ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ์˜ ์›์†Œ๋ฅผ ์‚ฌ์ƒํ•˜๋ฉด ๋ˆ„์ ๋˜์ง€ ์•Š๋Š”๋‹ค. scan์€ ๋‘˜ ๋‹ค ํ•œ๋‹ค. ์ ‘๊ธฐ์ฒ˜๋Ÿผ ๊ฐ’์„ ๋ˆ„์ ํ•˜์ง€๋งŒ ๊ฒฐ๊ด๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋Œ€์‹  ๋ชจ๋“  ์ค‘๊ฐ„์˜ ๊ฐ’์„ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ‘œ์ค€ Prelude์—๋Š” ๋„ค ๊ฐœ์˜ scan ํ•จ์ˆ˜๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. scanl :: (a -> b -> a) -> a -> [b] -> [a] ์ด๊ฒƒ์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์™ผ์ชฝ๋ถ€ํ„ฐ ๋ˆ„์ ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์ด ๋œ๋‹ค. ์ฆ‰ scanl (+) 0 [1,2,3] = [0,1,3,6]์ด๋‹ค. scanl1 :: (a -> a -> a) -> [a] -> [a] scanl๊ณผ ๊ฐ™์ง€๋งŒ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ์˜ ๋ฒˆ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์“ด๋‹ค. ์ž…๋ ฅ ํ•ญ๋ชฉ๊ณผ ์ถœ๋ ฅ ํ•ญ๋ชฉ์ด ๊ฐ™์€ ํƒ€์ž…์ด๋ฉด ๋Œ€๊ฐœ ์ด๊ฑธ ์‚ฌ์šฉํ•œ๋‹ค. ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ์˜ ์ฐจ์ด์— ์œ ์˜ํ•  ๊ฒƒ. scanl1 (+) [1,2,3] = [1,3,6]์ด๋‹ค. scanr :: (a -> b -> b) -> b -> [a] -> [b] scanr1 :: (a -> a -> a) -> [a] -> [a] ๋‘ ํ•จ์ˆ˜๋Š” scanl๊ณผ scanl1์˜ ์ •ํ™•ํžˆ ๋ฐ˜๋Œ€๋‹ค. ์ด๊ณ„๋ฅผ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ๋ˆ„์ ํ•œ๋‹ค. ์ฆ‰ scanr (+) 0 [1,2,3] = [6,5,3,0] scanr1 (+) [1,2,3] = [6,5,3] ์—ฐ์Šต๋ฌธ์ œ ์—ฌ๋Ÿฌ๋ถ„ ๊ณ ์œ ์˜ scanr์„ ๋จผ์ € ์žฌ๊ท€๋กœ, ๊ทธ๋‹ค์Œ foldr์œผ๋กœ ์ •์˜ํ•˜๋ผ. ๋‹ค์Œ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ํ•˜๋ผ. factList :: Integer -> [Integer]. 1์—์„œ ์ธ์ž๊นŒ์ง€์˜ ๊ณ„์Šน๋“ค์˜ ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด factList 4 = [1,2,6,24]์ด๋‹ค. ์ถ”๊ฐ€ ์˜ˆ์ • ๊ฑธ๋Ÿฌ๋‚ด๊ธฐ(filter) ํ•„ํ„ฐ๋ง์€ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ˆ˜ํ–‰๋˜๋Š” ์•„์ฃผ ํ”ํ•œ ์—ฐ์‚ฐ์œผ๋กœ, ์ฒ˜์Œ ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์›์†Œ๋“ค๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ž‘์—…์„ ๋œปํ•œ๋‹ค. ๊ทธ ๊ฐ„๋‹จํ•œ ์˜ˆ๊ฐ€ ์ •์ˆ˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด ์ง์ˆ˜๋งŒ ๋‹ด๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. retainEven :: [Int] -> [Int] retainEven [] = [] retainEven (n:ns) = -- mod n 2๋Š” ์ •์ˆ˜ n์„ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ์ฆ‰ ์ง์ˆ˜์ผ ๋•Œ๋Š” 0์ด๋‹ค. if ((mod n 2) == 0) then n : (retainEven ns) else retainEven ns ์ด๊ฑธ๋กœ ์ž˜ ์ž‘๋™ํ•˜์ง€๋งŒ ๋‹ค์†Œ ์žฅํ™ฉํ•œ ํ•ด๊ฒฐ์ฑ…์ด๋‹ค. ํ•„ํ„ฐ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•˜๊ณ  ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ด ์žˆ์œผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. Prelude๊ฐ€ filter๋ž€ ๊ฑธ ์ด๋ฏธ ์ œ๊ณตํ•˜๊ณ  ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. filter :: (a -> Bool) -> [a] -> [a] ์ฆ‰ (a -> Bool) ํ•จ์ˆ˜๋Š” ์กฐ๊ฑด์— ๋Œ€ํ•ด ์›์†Œ๋ฅผ ์‹œํ—˜ํ•˜๊ณ , ๊ทธ๋‹ค์Œ ๊ฑธ๋Ÿฌ๋‚ผ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๊ณ , ๊ฑธ๋Ÿฌ๋‚ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. filter๋กœ retainEven๋ฅผ ์ž‘์„ฑํ•˜๋ ค๋ฉด ์ด๋Ÿฐ ์‹์œผ๋กœ ์กฐ๊ฑด์„ (a -> Bool) ํƒ€์ž…์˜ ๋ณด์กฐ ํ•จ์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ด์•ผ ํ•œ๋‹ค. isEven :: Int -> Bool isEven n = ((mod n 2) == 0) ๊ทธ๋Ÿฌ๋ฉด retainEven๋Š” ๋‹จ์ˆœํžˆ retainEven ns = filter isEven ns xs ๋Œ€์‹  ns๋ฅผ ์ผ๋Š”๋ฐ ์ด๊ฒƒ๋“ค์ด ์ˆซ์ž์ผ ๋ฟ ๋‹ค๋ฅธ ๊ฒƒ์ด ์•„๋‹˜์„ ์•ˆ๋‹ค๋Š” ๊ฑธ ํ•ฉ๋ฆฌ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋Ÿฐ ๊ฑด ๋ฌด์‹œํ•˜๊ณ  ๋” ๊ฐ„๊ฒฐํ•œ ์ธ์ž ์ƒ๋žต์‹ ์ •์˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. retainEven = filter isEven ์šฐ๋ฆฌ๊ฐ€ ์•ž์„œ map๊ณผ ์ ‘๊ธฐ์— ๋Œ€ํ•ด ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด ์„ค๋ช…ํ•œ ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ์ด๊ฒƒ๋“ค๋„ filter์ฒ˜๋Ÿผ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ทจํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๊ฒƒ๋“ค์„ ์ธ์ž ์ƒ๋žต์‹์œผ๋กœ ์“ฐ๋ฉด ์ด๋Ÿฌํ•œ "ํ•จ์ˆ˜์˜ ํ•จ์ˆ˜" ๊ด€์ ์ด ๋‹๋ณด์ธ๋‹ค. ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹(list comprehension) ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์€ ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ•๋ ฅํ•˜๊ณ , ๊ฐ„๊ฒฐํ•˜๊ณ , ํ‘œํ˜„๋ ฅ ํ’๋ถ€ํ•œ ๋ฌธ๋ฒ•์  ๋„๊ตฌ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ค‘์—์„œ๋„ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์€ ํ•„ํ„ฐ๋ง์„ ์œ„ํ•œ ํŽธ์˜ ๋ฌธ๋ฒ•์ด๋‹ค. Prelude์˜ filter๋ฅผ ์“ฐ๋Š” ๋Œ€์‹  retainEven์„ ์ด๋ ‡๊ฒŒ๋„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. retainEven es = [ n | n <- es, isEven n ] ์ด ์••์ถ•๋œ ๋ฌธ๋ฒ•์€ ์ฒ˜์Œ ๋ณด๋ฉด ์•ฝ๊ฐ„ ๊ฒ์ด ๋‚˜๊ฒ ์ง€๋งŒ ์ชผ๊ฐœ๋ณด๋ฉด ๋‹จ์ˆœํ•˜๋‹ค. ์ด๊ฑธ ํ•ด์„ํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€, (์ค‘๊ฐ„๋ถ€ํ„ฐ ์‹œ์ž‘) ๋ฆฌ์ŠคํŠธ es๋ฅผ ์ทจํ•ด ๊ฐ๊ฐ์˜ ์›์†Œ๋ฅผ ๊ฐ’ n์œผ๋กœ์„œ ๋ฝ‘๋Š”๋‹ค("<-"). (์‰ผํ‘œ ๋’ค) ๋ฝ‘์•„๋‚ธ n๋งˆ๋‹ค ๋ถˆ๋ฆฌ์–ธ ์กฐ๊ฑด์‹ isEven n์„ ๊ฒ€์‚ฌํ•œ๋‹ค. (์ˆ˜์ง ๋ง‰๋Œ€ ์•ž) ์˜ค์ง ๋ถˆ๋ฆฌ์–ธ ์กฐ๊ฑด์ด ๋งŒ์กฑ๋  ๋•Œ๋งŒ n์„ ์ƒˆ๋กœ ์ƒ์„ฑ๋  ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค(์ „์ฒด ํ‘œํ˜„์‹์„ ๋‘˜๋Ÿฌ์‹ผ ๊ฐ๊ด„ํ˜ธ์— ์œ ์˜). ๊ทธ๋Ÿฌ๋ฏ€๋กœ es๊ฐ€ [1,2,3,4]์™€ ๊ฐ™๋‹ค๋ฉด [2,4]๋ฅผ ๋Œ๋ ค๋ฐ›๋Š”๋‹ค. 1๊ณผ 3์ด ๋ฝ‘ํžˆ์ง€ ์•Š๋Š” ๊ฑด (isEven n) == False์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์€ ์‰ฝ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ๊ฐ•์ ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ๋กœ, ๊ฒ€์‚ฌ๋ฅผ ์›ํ•˜๋Š” ๋งŒํผ(์•„์˜ˆ ์•ˆ ํ•  ์ˆ˜๋„) ์žˆ๋‹ค. ๋ณต์ˆ˜ ์กฐ๊ฑด์„ ์ž‘์„ฑํ•˜๋ ค๋ฉด ํ‘œํ˜„์‹์˜ ๋ชฉ๋ก์„ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค(๋ฌผ๋ก  ๋ถˆ๋ฆฌ์–ธ๋“ค๋กœ ํ‰๊ฐ€๋˜์–ด์•ผ ํ•œ๋‹ค). ๊ฐ„๋‹จํ•œ ์˜ˆ๋กœ retainEven์„ 100๋ณด๋‹ค ํฐ ์ˆ˜๋งŒ ์–ป๋„๋ก ์ˆ˜์ •ํ•˜๋ ค๋ฉด, retainLargeEvens :: [Int] -> [Int] retainLargeEvens es = [ n | n <- es, isEven n, n > 100 ] ๊ฒŒ๋‹ค๊ฐ€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ถ”๊ฐ€ํ•  ์›์†Œ๋กœ n๋งŒ ์จ์•ผ ํ•˜๋Š” ๊ฒƒ๋„ ์•„๋‹ˆ๋‹ค. ์ˆ˜์ง ๋ง‰๋Œ€ ์•ž์— ์–ด๋–ค ํ‘œํ˜„ ์‹๋„ ๋†“์„ ์ˆ˜ ์žˆ๋‹ค ๋ฌผ๋ก  ๋ฆฌ์ŠคํŠธ์˜ ํƒ€์ž…๊ณผ ํ˜ธํ™˜๋˜๋Š” ํ•œ์—์„œ). ๊ฐ€๋ น ๋ชจ๋“  ์ง์ˆ˜์—์„œ 1์„ ๋นผ๊ณ  ์‹ถ๋‹ค๋ฉด, evensMinusOne es = [ n - 1 | n <- es, isEven n ] ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์ด๋ž€ ๋ฌธ๋ฒ•์€ map๊ณผ filter์˜ ๊ธฐ๋Šฅ์„ ํ†ตํ•ฉํ•œ๋‹ค. ์ด์ œ์•ผ ๊ฐ„๊ฒฐํ•ด์กŒ๋‹ค(๊ทธ๋ฆฌ๊ณ  ์—ฌ์ „ํžˆ ๊ฐ€๋…์„ฑ ์žˆ๋‹ค!). ๋” ๋งˆ์Œ์— ๋“œ๋Š” ์ ์€ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์˜ ์™ผ์ชฝ ํ™”์‚ดํ‘œ ํ‘œ๊ธฐ๋ฅผ ํŒจํ„ด ๋งค์นญ๊ณผ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด (Int, Int) ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด, ๋‘ ๋ฒˆ์งธ ์›์†Œ๊ฐ€ ์ง์ˆ˜์ธ ํŠœํ”Œ๋“ค์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค๊ณ  ์น˜์ž. ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์„ ์“ฐ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. firstForEvenSeconds :: [(Int, Int)] -> [Int] firstForEvenSeconds ps = [ fst p | p <- ps, isEven (snd p) ] -- ์—ฌ๊ธฐ์„œ p๋Š” ์ง์ด๋‹ค. ํŒจํ„ด์œผ๋กœ ๋” ๊ฐ€๋…์„ฑ ์ข‹๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. firstForEvenSeconds ps = [ x | (x, y) <- ps, isEven y ] ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ | ์•ž์— ์–ด๋–ค ํ‘œํ˜„ ์‹๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์ด ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋“ค์˜ 2๋ฐฐ์ˆ˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์›ํ•œ๋‹ค๋ฉด, doubleOfFirstForEvenSeconds :: [(Int, Int)] -> [Int] doubleOfFirstForEvenSeconds ps = [ 2 * x | (x, y) <- ps, isEven y ] ์ŠคํŽ˜์ด์Šค๋ฅผ ๋นผ๋ฉด ํ•จ์ˆ˜ ์ฝ”๋“œ๊ฐ€ ํ•จ์ˆ˜ ์ด๋ฆ„๋ณด๋‹ค ์งง๋‹ค! ๊ทธ๋ฆฌ๊ณ  ๋”ํ•œ ๊ธฐ๊ต๋„ ์žˆ๋‹ค. allPairs :: [(Int, Int)] allPairs = [ (x, y) | x <- [1.. 4], y <- [5.. 8] ] ์ด ์กฐ๊ฑด ์ œ์‹œ์‹์€ ๋‘ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ์—์„œ ์›์†Œ๋“ค์„ ๋ฝ‘๋Š”๋‹ค. ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋Š” [1.. 4]์—์„œ, ๋‘ ๋ฒˆ์งธ๋Š” [5.. 8]์—์„œ ๋ฝ‘์•„ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ (x, y) ์ง์„ ์ƒ์„ฑํ•œ๋‹ค. ์ตœ์ข… ์ง ๋ฆฌ์ŠคํŠธ๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ(์—ฌ๊ธฐ์„  1)๋ฅผ ๊ฐ€์ง€๊ณ  ์ƒ์„ฑํ•œ ๊ฒƒ๋“ค ๋‹ค์Œ ์ฒซ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์˜ ๋‘ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์ƒ์„ฑํ•œ ๊ฒƒ๋“ค์ด ์˜ค๋Š” ์‹์ด๋‹ค. ์ด ์˜ˆ์—์„œ ์™„์ „ํ•œ ๋ฆฌ์ŠคํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (๋ช…๋ฃŒํ•จ์„ ์œ„ํ•ด ์ค„๋ฐ”๊ฟˆ์„ ๋„ฃ์—ˆ๋‹ค) Prelude> [(x, y)|x<-[1.. 4],y<-[5.. 8]] [(1,5),(1,6),(1,7),(1,8), (2,5),(2,6),(2,7),(2,8), (3,5),(3,6),(3,7),(3,8), (4,5),(4,6),(4,7),(4,8)] ์ตœ์ข… ๋ฆฌ์ŠคํŠธ์— ํฌํ•จ๋  ์กฐํ•ฉ๋“ค์„ ์ œํ•œํ•˜๋Š” ์กฐ๊ฑด๋„ ์‰ฝ๊ฒŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. somePairs = [ (x, y) | x <- [1.. 4], y <- [5.. 8], x + y > 8 ] ์ด ๋ฆฌ์ŠคํŠธ๋Š” ์›์†Œ๋“ค์˜ ํ•ฉ์ด 8๋ณด๋‹ค ํฐ ์ง๋งŒ์„ ํฌํ•จํ•œ๋‹ค. ์ฒ˜์Œ์€ (1, 8)์ด๊ณ  ๋‹ค์Œ์€ (2, 7) ์ด ์˜ค๋Š” ์‹์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ํ•จ์ˆ˜ returnDivisible :: Int -> [Int] -> [Int]์„ ์ž‘์„ฑํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ •์ˆ˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฑธ๋Ÿฌ๋‚ด์„œ, ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋“ค๋งŒ ํ†ต๊ณผ์‹œํ‚จ๋‹ค. ์ •์ˆ˜ x์™€ n์— ๋Œ€ํ•ด (mod x n) == 0์ด๋ฉด x๋Š” n์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. (์ง์ˆ˜ ๊ฒ€์‚ฌ๋Š” ์ด๊ฒƒ์˜ ํŠน์ˆ˜ํ•œ ๊ฒฝ์šฐ๋‹ค) ํ•จ์ˆ˜ choosingTails :: [[Int]] -> [[Int]]๋ฅผ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค. ๋นˆ ๋ฆฌ์ŠคํŠธ๋Š” ์ ์ ˆํ•œ ๊ฐ€๋“œ(ํ•„ํ„ฐ)๋ฅผ ์‚ฌ์šฉํ•ด ๊ฑธ๋Ÿฌ๋‚ด๊ณ  head๊ฐ€ 5๋ณด๋‹ค ํฐ ๋ฆฌ์ŠคํŠธ๋“ค์˜ tail๋“ค๋งŒ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. choosingTails [[7,6,3],[],[6,4,2],[9,4,3],[5,5,5]] -- [[6,3],[4,2],[4,3]] ๊ฐ€๋“œ๋“ค์˜ ์ˆœ์„œ๊ฐ€ ์ƒ๊ด€์ด ์žˆ์„๊นŒ? ์•ž์„  ์—ฐ์Šต๋ฌธ์ œ๋“ค์—์„œ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜๋“ค์„ ์ด์šฉํ•ด์„œ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์ด ์ ˆ์—์„œ ๋ดค๋“ฏ์ด ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์€ ๊ทผ๋ณธ์ ์œผ๋กœ filter์™€ map์˜ ํŽธ์˜ ๋ฌธ๋ฒ•์ด๋‹ค. ์ด์ œ ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•ด์„œ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹ ๋ฌธ๋ฒ•์„ ์ด์šฉํ•ด filter์™€ map์˜ ๋Œ€์ฒด ๋ฒ„์ „์„ ์ •์˜ํ•ด ๋ณด์ž. ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹ ๋Œ€์‹  filter์™€ map์„ ์‚ฌ์šฉํ•ด์„œ doubleOfFirstForEvenSeconds์„ ์žฌ์ž‘์„ฑํ•ด ๋ณด์ž. 4 ํƒ€์ž… ์„ ์–ธ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Type_declarations ๋‹ค๋ฅธ ์ฑ•ํ„ฐ๋“ค์„ ๋ดค์ง€๋งŒ newtype์— ๊ด€ํ•œ ์„ค๋ช…์ด ์—†์–ด์„œ HaskellWiki์—์„œ newtype ํŽ˜์ด์ง€๋ฅผ ๋”ฐ๋กœ ๋ฒˆ์—ญํ–ˆ์Šต๋‹ˆ๋‹ค. http://codeonwort.tistory.com/279 data์™€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํƒ€์ž… ๋ถ„ํ•ดํ•˜๊ธฐ ํƒ€์ž… ๋™์˜์–ด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ type ์–ธ์–ด์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ํƒ€์ž…๋“ค๋งŒ ์จ์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋งŒ์˜ ํƒ€์ž…์„ ์ •์˜ํ•˜๋ฉด ๋งŽ์€ ์ด์ ์ด ์žˆ๋‹ค. ํ•ด๊ฒฐํ•˜๋ ค๋Š” ๋ฌธ์ œ์˜ ๊ด€์ ์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์–ด์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์„ค๊ณ„ํ•˜๊ณ  ์ž‘์„ฑํ•˜๊ณ  ์ดํ•ดํ•˜๋Š” ๊ฒƒ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ ๋‹ค. ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ ์กฐ๊ฐ๋“ค์— ๋Œ€ํ•ด, ๋‹จ์ˆœํžˆ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์— ๊ฐ’์„ ๋„ฃ๊ฑฐ๋‚˜ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋ณด๋‹ค ํŽธ๋ฆฌํ•˜๊ณ  ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„๋งŒ์˜ ๋งž์ถคํ˜• ํƒ€์ž…์— ํŒจํ„ด ๋งค์นญ๊ณผ ํƒ€์ž… ์ฒด๊ณ„๋ฅผ ํ•œ๊ป ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์Šค์ผˆ์—๋Š” ์ƒˆ๋กœ์šด ํƒ€์ž…์„ ์„ ์–ธํ•˜๋Š” ์„ธ ๊ฐœ์˜ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. data ์„ ์–ธ. ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ •์˜ํ•œ๋‹ค. type ์„ ์–ธ. ํƒ€์ž… ๋™์˜์–ด๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. newtype ์„ ์–ธ. ์œ„์˜ ๋‘˜์„ ์„ž์€ ๊ฒƒ์ด๋‹ค. ์ด๋ฒˆ ์žฅ์—์„œ๋Š” data์™€ type์„ ๋‹ค๋ฃฌ๋‹ค. newtype์€ ๋‚˜์ค‘์— ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. data์™€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜ data๋Š” ์ด๋ฏธ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค์„ ๊ธฐ์ดˆ ๋ฒฝ๋Œ ์‚ผ์•„ ์ƒˆ๋กœ์šด ํƒ€์ž…๋“ค์„ ์ •์˜ํ•˜๋Š” ๋ฐ ์“ฐ์ธ๋‹ค. ๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ ๊ธฐ๋…์ผ ๋ชฉ๋ก์„ ์œ„ํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ๋‹ค. data Anniversary = Birthday String Int Int Int -- name, year, month, day | Wedding String String Int Int Int -- spouse name 1, spouse name 2, year, month, day ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ํƒ€์ž…์ธ Anniversary๋ฅผ ์„ ์–ธํ•˜๋Š”๋ฐ, Birthday๋„ Wedding๋„ ๋  ์ˆ˜ ์žˆ๋‹ค. Birthday๋Š” ๋ฌธ์ž์—ด ํ•˜๋‚˜์™€ ์ •์ˆ˜ ์„ธ ๊ฐœ๋ฅผ ํฌํ•จํ•˜๊ณ  Wedding์€ ๋ฌธ์ž์—ด ๋‘ ๊ฐœ์™€ ์ •์ˆ˜ ์„ธ ๊ฐœ๋ฅผ ํฌํ•จํ•œ๋‹ค. ์ด ๋‘˜์˜ ์ •์˜๋Š” ์ˆ˜์ง ๋ง‰๋Œ€๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ์ฃผ์„์€ ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ์ด์—๊ฒŒ ์ƒˆ๋กœ์šด ํƒ€์ž…๋“ค์˜ ์˜๋„๋œ ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•œ๋‹ค. ์ด ์„ ์–ธ์„ ํ†ตํ•ด ์šฐ๋ฆฌ๋Š” Anniversary์— ๋Œ€ํ•œ ๋‘ ๊ฐœ์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์–ป๊ฒŒ ๋˜์—ˆ๋‹ค. ์ ์ ˆํ•˜๊ฒŒ๋„ ๊ทธ ์ด๋ฆ„์€ Birthday์™€ Wedding์ด๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์€ ์ƒˆ๋กœ์šด Anniversary๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•œ๋‹ค. data ์„ ์–ธ์œผ๋กœ ์ •์˜๋œ ํƒ€์ž…๋“ค์„ ๋Œ€์ˆ˜์  ๋ฐ์ดํ„ฐ ํƒ€์ž…(algebraic data type)์ด๋ผ ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค. ์ด์— ๊ด€ํ•ด์„œ๋Š” ๋‚˜์ค‘์— ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ์œผ๋ ˆ ๊ทธ๋Ÿฌ๋“ฏ์ด ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์˜ ๋Œ€์†Œ๋ฌธ์ž ์—ฌ๋ถ€๋Š” ์ค‘์š”ํ•˜๋‹ค. ํƒ€์ž… ์ด๋ฆ„๊ณผ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ ๋ฌธ๋ฒ•์ƒ ์„ธ๋ถ€์‚ฌํ•ญ ์™ธ์—๋„ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€ ๋งŒ๋‚œ "์ „ํ†ต์ ์ธ" ํ•จ์ˆ˜์™€ ์ƒ๋‹นํžˆ ๋น„์Šทํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. ์‚ฌ์‹ค GHCi์—์„œ :t๋ฅผ ํ†ตํ•ด Birthday์˜ ํƒ€์ž…์„ ์งˆ์˜ํ•˜๋ฉด ๋‹ค์Œ์„ ์–ป๊ฒŒ ๋œ๋‹ค. *Main> :t Birthday Birthday :: String -> Int -> Int -> Int -> Anniversary Birthday๋Š” String ํ•˜๋‚˜์™€ Int ์„ธ ๊ฐœ๋ฅผ ์ธ์ž๋กœ ์ทจํ•ด Anniversary๋กœ ํ‰๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜์ผ ๋ฟ์ด๋‹ค. ์ด ๊ธฐ๋…์ผ์€ ์šฐ๋ฆฌ๊ฐ€ Birthday ์ƒ์„ฑ์ž์— ๋ช…์‹œํ•œ ๋„ค ์ธ์ž๋ฅผ ํฌํ•จํ•  ๊ฒƒ์ด๋‹ค. ์ƒ์„ฑ์ž ํ˜ธ์ถœ์€ ์—ฌํƒ€ ํ•จ์ˆ˜ ํ˜ธ์ถœ๊ณผ ๋ณ„๋ฐ˜ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 1968๋…„ 7์›” 3์ผ์— ํƒœ์–ด๋‚œ John Smith ์”จ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. johnSmith :: Anniversary johnSmith = Birthday "John Smith" 1968 7 3 ์ด ๋‚จ์ž๋Š” 1987๋…„ 3์›” 4์ผ์— Jane Smith์™€ ๊ฒฐํ˜ผํ–ˆ๋‹ค. smithWedding :: Anniversary smithWedding = Wedding "John Smith" "Jane Smith" 1987 3 4 ๋‘ ๊ธฐ๋…์ผ์„ ํ•œ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค. anniversariesOfJohnSmith :: [Anniversary] anniversariesOfJohnSmith = [johnSmith, smithWedding] ๋˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์ถ•ํ•  ๋•Œ ์ƒ์„ฑ์ž๋“ค์„ ๋ฐ”๋กœ ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋‹ค(๊ฒฐ๊ณผ ์ฝ”๋“œ๊ฐ€ ์ข€ ๋„ˆ์ €๋ถ„ํ•˜์ง€๋งŒ). anniversariesOfJohnSmith = [Birthday "John Smith" 1968 7 3, Wedding "John Smith" "Jane Smith" 1987 3 4] ํƒ€์ž… ๋ถ„ํ•ดํ•˜๊ธฐ ์šฐ๋ฆฌ์˜ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ทธ ๋‚ด์šฉ๋ฌผ์— ์ ‘๊ทผํ•  ๋ฐฉ๋ฒ•์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์— ์ •์˜ํ•œ ๊ธฐ๋…์ผ์˜ ์•„์ฃผ ๊ธฐ๋ณธ์ ์ธ ์ž‘์—…์€ String์œผ๋กœ ํฌํ•จ๋œ ์ด๋ฆ„๊ณผ ๋‚ ์งœ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ showAnniversary ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค(์ฝ”๋“œ๋ฅผ ๋ช…๋ฃŒํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด showDate๋ž€ ๋ณด์กฐ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ๋‹น๋ถ„๊ฐ„์€ ๋ฌด์‹œํ•˜์ž). showDate :: Int -> Int -> Int -> String showDate y m d = show y ++ "-" ++ show m ++ "-" ++ show d showAnniversary :: Anniversary -> String showAnniversary (Birthday name year month day) = name ++ " born " ++ showDate year month day showAnniversary (Wedding name1 name2 year month day) = name1 ++ " married " ++ name2 ++ " on " ++ showDate year month day ์ด ์˜ˆ์ œ๋Š” ์šฐ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋‚ด์žฅ๋œ ๊ฐ’๋“ค์„ ์–ด๋–ป๊ฒŒ ๋ถ„ํ•ดํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. showAnniversary๋Š” Anniversary ํƒ€์ž…์˜ ๋‹จ์ผ ์ธ์ž๋ฅผ ์ทจํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ •์˜์˜ ์ขŒ๋ณ€์—์„œ ์ธ์ž์˜ ์ด๋ฆ„๋งŒ ์ ๋Š” ๋Œ€์‹  ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ธฐ์ž…ํ•˜๊ณ  ๊ทธ ์ƒ์„ฑ์ž์˜ ๊ฐ ์ธ์ž์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ–ˆ๋‹ค(Anniversary์˜ ๋‚ด์šฉ๋ฌผ๊ณผ ๋Œ€์‘ํ•œ๋‹ค). "์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋Š”" ๊ฒƒ์˜ ๊ณต์‹์ ์ธ ์„œ์ˆ ์€ ๋ณ€์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค์ด๋‹ค. "๋ฐ”์ธ๋”ฉ"์€ ๋ณ€์ˆ˜๋ฅผ ๊ฐ๊ฐ์˜ ๊ฐ’์— ํ• ๋‹นํ•ด์„œ ํ•จ์ˆ˜ ์ •์˜์˜ ์šฐ๋ณ€์—์„œ ๊ทธ ๊ฐ’๋“ค์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์—์„œ ์“ฐ์ด๋Š” ๊ฒƒ์ด๋‹ค. "Birthday" ๊ธฐ๋…์ผ๊ณผ "Wedding" ๊ธฐ๋…์ผ ๋ชจ๋‘๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ƒ์„ฑ์ž๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ๋‘ ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค. showAnniversary๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ, ๊ทธ ์ธ์ž๊ฐ€ Birthday ๊ธฐ๋…์ผ์ด๋ผ๋ฉด ์ฒซ ๋ฒˆ์งธ ์ •์˜๊ฐ€ ์‚ฌ์šฉ๋˜์–ด ๋ณ€์ˆ˜ name, month, date, year๊ฐ€ ๋‚ด์šฉ๋ฌผ๋“ค์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. ์ธ์ž๊ฐ€ Wedding ๊ธฐ๋…์ผ์ด๋ฉด ๋‘ ๋ฒˆ์งธ ์ •์˜๊ฐ€ ์‚ฌ์šฉ๋˜์–ด ๊ฐ™์€ ์‹์œผ๋กœ ๋ณ€์ˆ˜๋“ค์ด ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. ์ƒ์„ฑ์ž์˜ ํƒ€์ž…์— ๋”ฐ๋ผ ํ•จ์ˆ˜์˜ ์—ฌ๋Ÿฌ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋Š” ์ด๋Ÿฐ ๊ณผ์ •์€ case ๋ฌธ์ด๋‚˜ ์กฐ๊ฐ ํ•จ์ˆ˜ ์ •์˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ผ์–ด๋‚˜๋Š” ์ผ๊ณผ ์ƒ๋‹นํžˆ ๋‹ฎ์•˜๋‹ค. ์ƒ์„ฑ์ž ์ด๋ฆ„๊ณผ ๋ฐ”์ธ๋”ฉ ๋ณ€์ˆ˜๋ฅผ ๊ฐ์‹ธ๋Š” ๊ด„ํ˜ธ๋Š” ํ•„์ˆ˜๋ผ๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜์ž. ๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๋‚˜ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ์ด๊ฒƒ๋“ค์„ ๋‹จ์ผ ์ธ์ž๋กœ ์ทจ๊ธ‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋˜ํ•œ ๊ด„ํ˜ธ ๋‚ด์˜ ํ‘œํ˜„์‹์ด ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ๋Œ€ํ•œ ํ˜ธ์ถœ์ฒ˜๋Ÿผ ์ƒ๊ฒผ์–ด๋„, ์‚ฌ์‹ค์€ ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ๋„ ํ™•์‹คํžˆ ํ•ด๋‘๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์ž ๊น: ์ด ์—ฐ์Šต๋ฌธ์ œ์˜ ํ•ด๋‹ต์ด ์ด๋ฒˆ ์žฅ์ด ๋๋‚˜๊ฐˆ ์ฆˆ์Œ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ์ „์— ํ’€์–ด๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. ์œ„์˜ ํ•จ์ˆ˜ ์ •์˜๋ฅผ ๋‹ค์‹œ ์ฝ์–ด๋ณด์ž. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ๋„์šฐ๋ฏธ ํ•จ์ˆ˜ showDate๋ฅผ ์ž์„ธํžˆ ๋ณด์ž. ์ด๊ฒƒ์ด "์ฝ”๋“œ์˜ ๋ช…๋ฃŒํ•จ์„ ์œ„ํ•ด" ์žˆ๋‹ค๊ณ  ๋งํ—€์ง€๋งŒ ์“ฐ์ด๋Š” ๋ฐฉ์‹์ด ์กฐ๊ธˆ ์ด์ƒํ•˜๋‹ค. ์šฐ๋ฆฌ๋Š” ์„ธ ๊ฐœ์˜ ๋ณ„๊ฐœ์˜ Int ์ธ์ž๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•˜์ง€๋งŒ ์ด ์ธ์ž๋“ค์„ ํ•ญ์ƒ ๋‹จ์ผ ๋‚ ์งœ์˜ ์ผ๋ถ€๋กœ์„œ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. Anniversary์˜ ์—ฐ์›”์ผ์„ ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ์›”์„ ๋‘ ๋ฒˆ ๋„˜๊ธฐ๊ณ  ์ผ์€ ์ƒ๋žตํ•˜๋Š” ๊ทธ๋Ÿฐ ์ผ์€ ์‚ฌ๋ฆฌ์— ๋งž์ง€ ์•Š๋‹ค. ์ด ์žฅ์—์„œ ๋ด์˜จ ๊ฒƒ๋“ค์„ ํ™œ์šฉํ•˜์—ฌ ์ด๋Ÿฐ ์กฐ์žกํ•จ์„ ์—†์•จ ์ˆ˜ ์žˆ์„๊นŒ? ๊ฐ๊ฐ ๋…„, ์›”, ์ผ์— ๋Œ€์‘ํ•˜๋Š” ์„ธ ๊ฐœ์˜ Int๋กœ ๊ตฌ์„ฑ๋œ Date ํƒ€์ž…์„ ์„ ์–ธํ•˜์ž. ๊ทธ๋ฆฌ๊ณ  showDate๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑํ•ด์„œ ์ด ์ƒˆ๋กœ์šด Date ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋„๋ก ๋งŒ๋“ค์–ด๋ณด์ž. ๊ทธ๋Ÿฌ๋ ค๋ฉด showAnniversary์™€ Anniversary๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐ”๊ฟ”์•ผ ํ• ๊นŒ? ํƒ€์ž… ๋™์˜์–ด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ type ์ด ๊ณผ๋ชฉ์˜ ๋„์ž…๋ถ€์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ๋งž์ถคํ˜• ํƒ€์ž…์„ ์“ฐ๋Š” ์ด์œ  ์ค‘ ํ•˜๋‚˜๋Š” ์ฝ”๋“œ์˜ ๋ช…๋ฃŒํ•จ์ด๋‹ค. ๊ทธ๋Ÿฐ ๋œป์—์„œ Anniversary ํƒ€์ž… ๋‚ด์˜ String๋“ค์ด ์ด๋ฆ„์œผ๋กœ ์“ฐ์ด๋ฉด์„œ๋„ ์ผ๋ฐ˜ String์ฒ˜๋Ÿผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ์Œ์„ ํ™•์‹คํžˆ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ๋„ ๊ฐ™๋‹ค. ์ด๋ฅผ type ์„ ์–ธ์ด๋ผ ํ•œ๋‹ค. type Name = String ์œ„์˜ ์ฝ”๋“œ๋Š” Name์ด ์ด์ œ String์˜ ๋™์˜์–ด๋ผ๊ณ  ์„ ์–ธํ•œ๋‹ค. String์„ ์ทจํ•˜๋Š” ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ์ด์ œ Name๋„ ์ทจํ•  ๊ฒƒ์ด๋‹ค(๊ทธ ๋ฐ˜๋Œ€๋กœ Name์„ ์ทจํ•˜๋Š” ํ•จ์ˆ˜๋„ ๋ชจ๋“  String์„ ๋ฐ›์•„๋“ค์ผ ๊ฒƒ์ด๋‹ค). type ์„ ์–ธ์˜ ์šฐ๋ณ€์€ ๋” ๋ณต์žกํ•œ ํƒ€์ž…์ผ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋ น String ์ž์ฒด๋Š” ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ด๋ ‡๊ฒŒ ์ •์˜๋˜์–ด ์žˆ๋‹ค. type String = [Char] ์šฐ๋ฆฌ๊ฐ€ ์จ์™”๋˜ ๊ธฐ๋…์ผ ๋ชฉ๋ก์œผ๋กœ๋„ ๋น„์Šทํ•œ ๊ฒƒ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. type AnniversaryBook = [Anniversary] ํƒ€์ž… ๋™์˜์–ด๋Š” ๋Œ€์ฒด๋กœ ํŽธ์˜๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. ํƒ€์ž… ๋™์˜์–ด๋Š” ํƒ€์ž…์˜ ์—ญํ• ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ๋ณต์žกํ•œ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ ํƒ€์ž… ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์œ„ํ•œ ๋ณ„์นญ์„ ์ œ๊ณตํ•œ๋‹ค. ํƒ€์ž… ๋™์˜์–ด๋ฅผ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์ง€๋Š” ์ˆœ์ „ํžˆ ๊ฐœ์ธ์˜ ์ทจํ–ฅ ๋ฌธ์ œ๋‹ค. ๋™์˜์–ด๋ฅผ ๋‚จ๋ฐœํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ํ˜ผ๋ž€์Šค๋Ÿฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค(Int๋‚˜ String ๊ฐ™์€ ํ”ํ•œ ํƒ€์ž…์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ด๋ฆ„์„ ์“ฐ๋Š” ์žฅ๋ฌธ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์ƒ๊ฐํ•ด ๋ณด์ž). ํƒ€์ž… ๋™์˜์–ด์™€ ์•ž์˜ ์ ˆ์—์„œ ์ œ์•ˆํ•œ ์—ฐ์Šต๋ฌธ์ œ(*)๋ฅผ ํ†ตํ•ฉํ•˜์—ฌ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. (() ์Šคํฌ์ผ๋Ÿฌ๋ฅผ ์•ˆ ๋ณด๊ณ  ๊ทธ ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ์‹œ๋„ํ•  ๋งˆ์ง€๋ง‰ ๊ธฐํšŒ๋‹ค.*) type Name = String data Anniversary = Birthday Name Date | Wedding Name Name Date data Date = Date Int Int Int -- Year, Month, Day johnSmith :: Anniversary johnSmith = Birthday "John Smith" (Date 1968 7 3) smithWedding :: Anniversary smithWedding = Wedding "John Smith" "Jane Smith" (Date 1987 3 4) type AnniversaryBook = [Anniversary] anniversariesOfJohnSmith :: AnniversaryBook anniversariesOfJohnSmith = [johnSmith, smithWedding] showDate :: Date -> String showDate (Date y m d) = show y ++ "-" ++ show m ++ "-" ++ show d showAnniversary :: Anniversary -> String showAnniversary (Birthday name date) = name ++ " born " ++ showDate date showAnniversary (Wedding name1 name2 date) = name1 ++ " married " ++ name2 ++ " on " ++ showDate date ์ด๋Ÿฐ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์—์„œ๋„ Int, String, ๊ด€๋ จ๋œ ๋ฆฌ์ŠคํŠธ๋งŒ ์จ์„œ ๋™์ผํ•œ ์ž‘์—…์„ ํ–ˆ์„ ๋•Œ๋ณด๋‹ค ๋‹จ์ˆœํ•จ๊ณผ ๋ช…๋ฃŒํ•จ์ด ๋“œ๋Ÿฌ๋‚œ๋‹ค. Date ํƒ€์ž…์€ ์—ญ์‹œ ๊ทธ ์ด๋ฆ„์ด Date์ธ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง„๋‹ค๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. ์—ฌ๊ธฐ์—๋Š” ์•„๋ฌด ๋ฌธ์ œ๊ฐ€ ์—†์œผ๋ฉฐ ์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜๋ฟ์ผ ๋•Œ ์ƒ์„ฑ์ž์˜ ์ด๋ฆ„๊ณผ ํƒ€์ž…์˜ ์ด๋ฆ„์„ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ์ข‹์€ ์Šต๊ด€์ด๋‹ค. ์ด๋Š” ํ•จ์ˆ˜์˜ ์—ญํ• ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฐ„๋‹จํ•œ ๊ธธ์ด๋‹ค. ์ž ๊น ์ง€๊ธˆ๊นŒ์ง€์˜ ๊ธฐ์ดˆ์ ์ธ ์˜ˆ์‹œ๋“ค์„ ๋ณด๊ณ  ๋‚˜๋ฉด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด, ํŠนํžˆ ์—ฌ๋Ÿฌ๋ถ„์ด ํƒ€ ์–ธ์–ด์˜ ๋น„์Šทํ•œ ํŠน์„ฑ์— ์ต์ˆ™ํ•˜๋‹ค๋ฉด, ๊ฑฐ์ถ”์žฅ์Šค๋Ÿฝ๊ฒŒ ๋ณด์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ์ƒ์„ฑ์ž๋ฅผ ๋” ํŽธํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ๋ฌธ๋ฒ•์  ์š”์†Œ๋“ค์ด ์žˆ์œผ๋ฉฐ ์ด์— ๊ด€ํ•ด์„œ๋Š” ๋‚˜์ค‘์— ์ƒ์„ฑ์ž์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋ž€ ์ฃผ์ œ๋กœ ๋Œ์•„์˜ฌ ๋•Œ ๋” ์ž์„ธํžˆ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. 5 ํŒจํ„ด ๋งค์นญ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Pattern_matching ํŒจํ„ด ๋งค์นญ ๋ถ„์„ํ•˜๊ธฐ ์ƒ์„ฑ์ž์™€์˜ ์—ฐ๊ด€์„ฑ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋Š” ์ด์œ  ํŠœํ”Œ ์ƒ์„ฑ์ž ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’๊ณผ์˜ ์ผ์น˜ ๋ฌธ๋ฒ• ํŠธ๋ฆญ as ํŒจํ„ด ๋ ˆ์ฝ”๋“œ์˜ ๋„์ž… ํŒจํ„ด ๋งค์นญ์„ ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ณณ ๋“ฑ์‹ let ํ‘œํ˜„์‹๊ณผ where ์ ˆ ๋žŒ๋‹ค ์ถ”์ƒํ™” ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹(list comprehension) do ๋ธ”๋ก ๋…ธํŠธ ์•ž์„  ๊ณผ๋ชฉ๋“ค์—์„œ ํŒจํ„ด ๋งค์นญ์„ ์†Œ๊ฐœํ•˜๊ณ  ์ž์ฃผ ์–ธ๊ธ‰ํ–ˆ๋‹ค. ์ด์ œ ํ•˜์Šค์ผˆ์— ์–ด๋Š ์ •๋„ ์ต์ˆ™ํ•ด์กŒ์œผ๋‹ˆ ๋” ์ •ํ™•ํ•˜๊ณ  ๊นŠ์€ ์•ˆ๋ชฉ์„ ๊ธฐ๋ฅผ ๋•Œ๊ฐ€ ๋˜์—ˆ๋‹ค. ๋‹ค์Œ์˜ ์••์ถ•๋œ ๋ฌธ์žฅ์œผ๋กœ ์šด์„ ๋–ผ๊ณ  ์ด๋ฒˆ ์žฅ์—์„œ ์ „๋ฐ˜์ ์œผ๋กœ ํ’€์–ดํ—ค์น  ๊ฒƒ์ด๋‹ค. ํŒจํ„ด ๋งค์นญ์€ ๊ฐ’์„ ํŒจํ„ด์— ์ผ์น˜์‹œํ‚ค๋ ค ์‹œ๋„ํ•˜๊ณ , ํ•„์š”ํ•˜๋‹ค๋ฉด ์„ฑ๊ณต์ ์œผ๋กœ ์ผ์น˜ํ•œ ๊ณณ์— ๋ณ€์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. ํŒจํ„ด ๋งค์นญ ๋ถ„์„ํ•˜๊ธฐ ํŒจํ„ด ๋งค์นญ์€ ์‚ฌ์‹ค์ƒ ์–ด๋””์—๋‚˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ์˜ map ์ •์˜๋ฅผ ๋ณด์ž. map _ [] = [] map f (x:xs) = f x : map f xs ์—ฌ๊ธฐ์—๋Š” ๋“ฑ์‹๋งˆ๋‹ค 2๊ฐœ์”ฉ ์ด 4๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ํŒจํ„ด์ด ๋“ค์–ด์žˆ๋‹ค. f๋Š” ๋ชจ๋“  ๊ฒƒ์— ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์œผ๋กœ์„œ, ๋ณ€์ˆ˜ f๋ฅผ ๊ทธ ์ž๋ฆฌ์—์„œ ์ผ์น˜ํ•˜๋Š” ๋ฌด์—‡์—๋“  ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. (x:xs)๋Š” ๋น„์–ด์žˆ์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ์— ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์œผ๋กœ์„œ, ๋ณ€์ˆ˜ x์— ๋ฐ”์ธ๋”ฉ ๋˜๋Š” ๋ฌด์–ธ๊ฐ€์™€ xs์— ๋ฐ”์ธ๋”ฉ ๋˜๋Š” ๋‹ค๋ฅธ ๋ฌด์–ธ๊ฐ€๊ฐ€ (:) ํ•จ์ˆ˜์— ์˜ํ•ด ์—ฐ๊ฒฐ๋œ๋‹ค. []๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ์— ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์ด๋‹ค. ์•„๋ฌด ๋ณ€์ˆ˜๋„ ๋ฐ”์ธ๋”ฉ ํ•˜์ง€ ์•Š๋Š”๋‹ค. _๋Š” ๋ชจ๋“  ๊ฒƒ์— ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์ด์ง€๋งŒ ์•„๋ฌด๊ฒƒ๋„ ๋ฐ”์ธ๋”ฉ ํ•˜์ง€ ์•Š๋Š”๋‹ค. (์™€์ผ๋“œ์นด๋“œ, "์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š์Œ" ํŒจํ„ด) (x:xs) ํŒจํ„ด์—์„œ x์™€ xs๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ผ๋ถ€๋ถ„์— ์ผ์น˜์‹œํ‚ค๊ธฐ ์œ„ํ•œ ํ•˜์œ„ ํŒจํ„ด๋“ค๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด ํŒจํ„ด๋“ค์€ f์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์•„๋ฌด๊ฒƒ์—๋‚˜ ์ผ์น˜ํ•œ๋‹ค. ์ผ์น˜๊ฐ€ ์„ฑ๊ณตํ•ด์„œ x์˜ ํƒ€์ž…์ด a๋ผ๋ฉด xs์˜ ํƒ€์ž…์€ [a]๋ผ๋Š” ๊ฒƒ์€ ๋ช…๋ฐฑํ•˜๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, xs๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ์—๋„ ์ผ์น˜ํ•˜๋ฉฐ ๋”ฐ๋ผ์„œ ์›์†Œ๊ฐ€ ํ•˜๋‚˜์ธ ๋ฆฌ์ŠคํŠธ๋ผ๋„ (x:xs)์— ์ผ์น˜ํ•œ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋ถ„์„ํ•œ ๊ฒƒ์„ ๋ณด๋ฉด ํŒจํ„ด ๋งค์นญ์ด ๋‹ค์Œ ์ˆ˜๋‹จ๋“ค์„ ์ œ๊ณตํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ’์„ ์ธ์‹ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด map์ด ํ˜ธ์ถœ๋˜๊ณ  ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ []์— ์ผ์น˜ํ•˜๋ฉด map์˜ ๋‘ ๋ฒˆ์งธ ๋“ฑ์‹์ด ์•„๋‹ˆ๋ผ ์ฒซ ๋ฒˆ์งธ ๋“ฑ์‹์ด ์‚ฌ์šฉ๋œ๋‹ค. ์ธ์‹๋œ ๊ฐ’์— ๋ณ€์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. ์œ„ ๊ฒฝ์šฐ์—๋Š” ๋‘ ๋ฒˆ์งธ ๋“ฑ์‹์ด ์‚ฌ์šฉ๋  ๊ฒฝ์šฐ ๋ณ€์ˆ˜ f, x, xs์—๋Š” map์˜ ์ธ์ˆ˜๋กœ์„œ ์ „๋‹ฌ๋œ ๊ฐ’๋“ค์ด ํ• ๋‹น๋˜๊ณ , ๋”ฐ๋ผ์„œ =์˜ ์šฐ๋ณ€์—์„œ ์ด ๋ณ€์ˆ˜๋“ค์„ ํ†ตํ•ด ๊ทธ ๊ฐ’๋“ค์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. _์™€ []๊ฐ€ ๋ณด์—ฌ์ฃผ๋“ฏ์ด ๋ฐ”์ธ๋”ฉ์€ ํŒจํ„ด ๋งค์นญ์˜ ํ•„์ˆ˜ ์š”์†Œ๊ฐ€ ์•„๋‹ˆ๋ฉฐ ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ํŒจํ„ด์œผ๋กœ์„œ ์“ธ ๋•Œ์˜ ๋ถ€์ˆ˜ ํšจ๊ณผ์ผ ๋ฟ์ด๋‹ค. ๊ฐ’์„ ๋ถ„ํ•ดํ•œ๋‹ค. (x:xs) ํŒจํ„ด์ด ๋‘ ๋ณ€์ˆ˜๋ฅผ ๊ฐ๊ฐ, ์ผ์น˜ํ•˜๋Š” ์ธ์ˆ˜(๋น„์–ด์žˆ์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ)์˜ ๋จธ๋ฆฌ์™€ ๊ผฌ๋ฆฌ์— ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ๊ฒƒ์ด ๊ทธ ์˜ˆ์‹œ๋‹ค. ์ƒ์„ฑ์ž์™€์˜ ์—ฐ๊ด€์„ฑ ์œ„์—์„œ ๋ฉด๋ฐ€ํžˆ ๋ถ„์„ํ•˜๊ธด ํ–ˆ์ง€๋งŒ, (:) ์—ฐ์‚ฐ์ž์˜ ๊ฒฐ๊ณผ๋ฅผ ๋˜๋Œ๋ฆฐ ๊ฒƒ ๋งˆ๋ƒฅ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ถ„ํ•ดํ•œ ๊ฒƒ์ด ๋งˆ์น˜ ๋งˆ๋ฒ•์ฒ˜๋Ÿผ ๋ณด์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ์ฃผ์˜: ์ด๋Ÿฐ ์ž‘์—…์„ ๋ชจ๋“  ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด (++)๋ฅผ ์ด์šฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ์˜ ์ฒ˜์Œ ์„ธ ์›์†Œ๋ฅผ ์ž˜๋ผ๋‚ด๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด์ž. dropThree ([x, y, z] ++ xs) = xs ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ๊ฒƒ์€ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. (++) ํ•จ์ˆ˜๋Š” ํŒจํ„ด์œผ๋กœ ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. ์‚ฌ์‹ค ๋ฆฌ์ŠคํŠธ์— ์ž‘์šฉํ•˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ํ•จ์ˆ˜๋Š” ํŒจํ„ด ๋งค์นญ์— ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿผ ์–ด๋–ค ํ•จ์ˆ˜๋“ค์ด ํ—ˆ์šฉ๋˜๋Š” ๊ฑธ๊นŒ? ํ•œ ๋งˆ๋””๋กœ ๋งํ•˜๋ฉด ์ƒ์„ฑ์ž(constructor), ์ฆ‰ ๋Œ€์ˆ˜ ์ž๋ฃŒํ˜•(algebraic data type)์˜ ๊ฐ’์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ์“ฐ์ด๋Š” ํ•จ์ˆ˜๋“ค๋งŒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ž„์‹œ๋กœ ๋งŒ๋“  ๋‹ค์Œ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž. data Foo = Bar | Baz Int ์—ฌ๊ธฐ์„œ Bar์™€ Baz๋Š” Foo ํƒ€์ž…์˜ ์ƒ์„ฑ์ž๋‹ค. ์ด๊ฒƒ๋“ค์„ ์‚ฌ์šฉํ•ด Foo ๊ฐ’์„ ํŒจํ„ด ๋งค์นญํ•˜๊ณ , Baz์— ์˜ํ•ด ์ƒ์„ฑ๋œ Foo์— ํฌํ•จ๋œ Int ๊ฐ’์— ๋ณ€์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•  ์ˆ˜ ์žˆ๋‹ค. f :: Foo -> Int f Bar = 1 f (Baz x) = x - 1 ์ด๋Š” ํƒ€์ž… ์„ ์–ธ ๊ณผ๋ชฉ์˜ showAnniversary, showDate์™€ ์ƒ๋‹นํžˆ ์œ ์‚ฌํ•˜๋‹ค. data Date = Date Int Int Int -- Year, Month, Day showDate :: Date -> String showDate (Date y m d) = show y ++ "-" ++ show m ++ "-" ++ show d showDate ์ •์˜์˜ ์ขŒ๋ณ€์˜ (Date y m d) ํŒจํ„ด์€ Date ์ƒ์„ฑ์ž๋กœ ๋งŒ๋“ค์–ด์ง„ Date ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๊ณ  ๋ณ€์ˆ˜ y, m, d๋ฅผ Date ๊ฐ’์˜ ๋‚ด์šฉ์— ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋Š” ์ด์œ  ํŒจํ„ด ๋งค์นญ์— ๊ด€ํ•œ ํ•œ, ๋ฆฌ์ŠคํŠธ๋Š” data๋ฅผ ํ†ตํ•ด ์ •์˜๋œ ๋‹ค๋ฅธ ๋Œ€์ˆ˜ ์ž๋ฃŒํ˜•๋“ค๊ณผ ๋‹ค๋ฅผ ๋ฐ”๊ฐ€ ์—†๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ๋‹ค์Œ์˜ data ์„ ์–ธ์— ์˜ํ•ด ์ •์˜๋œ ๊ฒƒ์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค(์ด ๊ตฌ๋ฌธ์€ ์‹ค์ œ๋กœ๋Š” ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ์ด๋Ÿฐ ์‹์œผ๋กœ ์ •์˜ํ•˜๊ธฐ์—๋Š” ํ•˜์Šค์ผˆ์— ๋„ˆ๋ฌด ๊นŠ์ˆ™์ด ๋ฟŒ๋ฆฌ๋ฅผ ๋‚ด๋ ธ๋‹ค). data [a] = [] | a : [a] ๋นˆ ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ []์™€ (:) ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์˜ ์ƒ์„ฑ์ž์ด๋ฉฐ ๋”ฐ๋ผ์„œ ์ด๊ฒƒ๋“ค์— ํŒจํ„ด์„ ์ผ์น˜์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. []๋Š” ์ธ์ˆ˜๋ฅผ ์ทจํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํŒจํ„ด ๋งค์นญ์—์„œ []์—๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋  ์ˆ˜ ์—†๋‹ค. (:)๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋จธ๋ฆฌ์™€ ๊ผฌ๋ฆฌ๋ผ๋Š” ๋‘ ์ธ์ˆ˜๋ฅผ ์ทจํ•ด, ํŒจํ„ด์ด ์ธ์‹๋  ๊ฒฝ์šฐ ๋ณ€์ˆ˜๋“ค์„ ์ด ๋‘˜์— ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. Prelude> :t [] [] :: [a] Prelude> :t (:) (:) :: a -> [a] -> [a] ๊ฒŒ๋‹ค๊ฐ€ [x, y, z]๋Š” x:y:z:[]์˜ ํŽธ์˜ ๊ตฌ๋ฌธ์ผ ๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์— dropThree ๊ฐ™์€ ๊ฒƒ์€ ํŒจํ„ด ๋งค์นญ๋งŒ์œผ๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค. dropThree :: [a] -> [a] dropThree (_:_:_:xs) = xs dropThree _ = [] ์ฒซ ๋ฒˆ์งธ ํŒจํ„ด์€ ์›์†Œ๊ฐ€ 3๊ฐœ ์ด์ƒ์ธ ์–ด๋Š ๋ฆฌ์ŠคํŠธ์—๋‚˜ ์ผ์น˜ํ•œ๋‹ค. ๋ญ๋“ ์ง€ ์žก๋Š” ๋‘ ๋ฒˆ์งธ ํŒจํ„ด์€ ํ•ฉ๋ฆฌ์ ์ธ ๊ธฐ๋ณธ๊ฐ’ 1์„ ์ œ๊ณตํ•˜์—ฌ, ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ ํŒจํ„ด์— ์ผ์น˜ํ•˜์ง€ ์•Š์„ ๋•Œ ํŒจํ„ด ๋งค์นญ ์‹คํŒจ๋กœ ๋Ÿฐํƒ€์ž„ ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์„<NAME>๋‹ค. ์ž ๊น dropThree ํ•จ์ˆ˜๋ฅผ ํŒจํ„ด ๋งค์นญ๋งŒ์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ด์„œ ๊ผญ ๊ทธ๋ž˜์•ผ ํ•œ๋‹ค๋Š” ๋ง์€ ์•„๋‹ˆ๋‹ค! ์ด ํ•ด๊ฒฐ์ฑ…์€ ๊ฐ„๋‹จํ•˜์ง€๋งŒ ์ด๋Ÿฐ ๊ฒƒ์„ ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ์€ ์—ฌ์ „ํžˆ ์‹œ๊ฐ„ ๋‚ญ๋น„๋‹ค. ๊ทธ์ € Prelude๋ฅผ ์ด์šฉํ•ด drop 3 xs๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค. ์žฌ๊ท€ ํ•จ์ˆ˜ ๋•Œ ํ•œ ๋ง์„ ๊ทธ๋Œ€๋กœ ํ•ด๋ณด๋ฉด, ๊ทธ๋ ‡๋‹ค๊ณ  ํŒจํ„ด ๋งค์นญ์—๋„ ๋„ˆ๋ฌด ๋“ค๋œจ์ง€ ๋ง์ž... ํŠœํ”Œ ์ƒ์„ฑ์ž ํŠœํ”Œ์˜ ๊ฒฝ์šฐ๋„ ๋น„์Šทํ•œ ๋…ผ์˜๊ฐ€ ์ ์šฉ๋œ๋‹ค. ํŒจํ„ด ๋งค์นญ์„ ํ†ตํ•ด ํŠœํ”Œ์˜ ๊ตฌ์„ฑ ์„ฑ๋ถ„๋“ค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. fstPlusSnd :: (Num a) => (a, a) -> a fstPlusSnd (x, y) = x + y norm3D :: (Floating a) => (a, a, a) -> a norm3D (x, y, z) = sqrt (x^2 + y^2 + z^2) ์ด ๋˜ํ•œ ํŠœํ”Œ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ์–ด์„œ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ง(pair)์˜ ์ƒ์„ฑ์ž๋Š” ์‰ผํ‘œ ์—ฐ์‚ฐ์ž (,)์ด๋‹ค. ๋” ํฐ ํŠœํ”Œ์˜ ๊ฒฝ์šฐ (,,), (,,,) ๋“ฑ์„ ์“ด๋‹ค. ์ด ์—ฐ์‚ฐ์ž๋“ค์€ ํ‘œ์ค€ ๋ฐฉ์‹์œผ๋กœ๋Š” ์ค‘์œ„ ์—ฐ์‚ฐ์ž๋กœ ์“ธ ์ˆ˜ ์—†๋‹ค. 5, 3์€ (5, 3)์„ ์ž‘์„ฑํ•˜๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ์—ฐ์‚ฐ์ž๋“ค์€ ๋ชจ๋‘ ์ „์น˜ ์—ฐ์‚ฐ์ž๋กœ ์“ฐ์ผ ์ˆ˜ ์žˆ๋‹ค. Prelude> (,) 5 3 (5,3) Prelude> (,,,) "George" "John" "Paul" "Ringo" ("George","John","Paul","Ringo") ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’๊ณผ์˜ ์ผ์น˜ ์•ž์„œ ๋…ผ์˜ํ–ˆ๋“ฏ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฐ ํ•จ์ˆ˜ ์ •์˜๋„ ํŒจํ„ด ๋งค์นญ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. f :: Int -> Int f 0 = 1 f 1 = 5 f 2 = 2 f _ = -1 ์—ฌ๊ธฐ์„œ๋Š” f์˜ ์ธ์ˆ˜๋ฅผ Int ๋ฆฌํ„ฐ๋Ÿด์ธ 0, 1, 2, ๋งˆ์ง€๋ง‰์œผ๋กœ _์™€ ์ผ์น˜์‹œํ‚จ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ˆซ์ž ๋ฐ ๋ฌธ์ž ๋ฆฌํ„ฐ๋Ÿด์€ ๊ทธ ์ž์ฒด๋กœ 2 ํŒจํ„ด ๋งค์นญ์— ์“ฐ์ผ ์ˆ˜ ์žˆ๊ณ  ์ƒ์„ฑ์ž ํŒจํ„ด๊ณผ ํ•จ๊ป˜ ์“ฐ์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹ค์Œ ํ•จ์ˆ˜๋Š” g :: [Int] -> Bool g (0:[]) = False g (0:xs) = True g _ = False ๋ฆฌ์ŠคํŠธ [0]์— ๋Œ€ํ•ด False๋กœ, ์ฒซ ๋ฒˆ์งธ ์›์†Œ๊ฐ€ 0์ด๊ณ  ๊ผฌ๋ฆฌ๊ฐ€ ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹Œ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด True๋กœ, ๋‹ค๋ฅธ ๋ชจ๋“  ๊ฒฝ์šฐ False๋กœ ํ‰๊ฐ€๋œ๋‹ค. ๋˜ํ•œ [1,2,3]์ฒ˜๋Ÿผ ๋ฆฌํ„ฐ๋Ÿด์ด ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋‚˜ "abc"(['a','b','c']์™€ ๋™์ผ)๋„ ํŒจํ„ด ๋งค์นญ์— ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ํ˜•ํƒœ๋Š” (:) ์ƒ์„ฑ์ž์˜ ํŽธ์˜ ๊ตฌ๋ฌธ์ผ ๋ฟ์ด๋‹ค. ์œ„์˜ ๋‚ด์šฉ์€ ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’์—๋งŒ ํ•ด๋‹นํ•œ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์€ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. k = 1 --again, this won't work as expected h :: Int -> Bool h k = True h _ = False ์—ฐ์Šต๋ฌธ์ œ ์œ„์˜ ๊ฒฐํ•จ ์žˆ๋Š” ํ•จ์ˆ˜ h๋ฅผ GHCi์—์„œ 1์ธ ์ธ์ˆ˜์™€ 1์ด ์•„๋‹Œ ์ธ์ˆ˜๋กœ ์‹œํ—˜ํ•ด ๋ณด์ž. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ๋ญ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ๋Š”์ง€ ์„ค๋ช…ํ•ด ๋ณด์ž. ์ด๋ฒˆ ์ ˆ์—์„œ ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’์„ ์ด์šฉํ•œ ํŒจํ„ด ๋งค์นญ์„ ๋‹ค๋ฃจ๋ฉด์„œ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’ True์™€ False๋Š” ์ „ํ˜€ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ, ์ด ๋‘˜๋„ ํŒจํ„ด ๋งค์นญ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ ์„ค๋ช…ํ•  ๊ฒƒ์ธ๋ฐ, ์™œ ์ด ๋‘˜์„ ๋น ๋œจ๋ ธ๋Š”์ง€ ์ถ”์ธกํ•ด ๋ณด์ž. (ํžŒํŠธ: ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ์ž‘์„ฑํ•˜๋Š” ๋ณ„๊ฐœ์˜ ๋ฐฉ๋ฒ•์ด ์žˆ์„๊นŒ?) ๋ฌธ๋ฒ• ํŠธ๋ฆญ as ํŒจํ„ด ๊ฐ€๋” ํŒจํ„ด๊ณผ ๊ฐ’์„ ์ผ์น˜์‹œํ‚ฌ ๋•Œ, ์ผ์น˜๋˜๋Š” ๊ฐ’ ์ „์ฒด์— ์ด๋ฆ„์„ ์ด๋ฆ„์„ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ๊ฒŒ ์œ ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด as ํŒจํ„ด์ด๋‹ค. var@pattern ๊ผด์ด๋ฉฐ, pattern์— ๋งค์นญ๋˜๋Š” ์ „์ฒด ๊ฐ’์— ์ด๋ฆ„ var๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. ๋‹ค์Œ์€ map์˜ ๋ณ€ํ˜•์ด๋‹ค. contrivedMap :: ([a] -> a -> b) -> [a] -> [b] contrivedMap f [] = [] contrivedMap f list@(x:xs) = f list x : contrivedMap f xs contrivedMap์€ ๋งค๊ฐœ ํ•จ์ˆ˜ f์— x๋ฟ ์•„๋‹ˆ๋ผ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ๋„ ์žฌ๊ท€ ํ˜ธ์ถœ์˜ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ๋‹ค. as ํŒจํ„ด์„ ์“ฐ์ง€ ์•Š์œผ๋ฉด head๋ฅผ ์“ฐ๊ฑฐ๋‚˜ ์›๋ž˜์˜ list๋ฅผ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์žฌ๊ตฌ์ถ•, ์ฆ‰ ์šฐ๋ณ€์—์„œ x:xs๋ฅผ ํ‰๊ฐ€ํ•ด์•ผ ํ–ˆ์„ ๊ฒƒ์ด๋‹ค. contrivedMap :: ([a] -> a -> b) -> [a] -> [b] contrivedMap f [] = [] contrivedMap f (x:xs) = f (x:xs) x : contrivedMap f xs ์—ฐ์Šต๋ฌธ์ œ List Processing์˜ ์—ฐ์Šต๋ฌธ์ œ scanr์„ as ํŒจํ„ด์„ ์จ์„œ ๊ตฌํ˜„ํ•˜๋ผ. ๋ ˆ์ฝ”๋“œ์˜ ๋„์ž… ์›์†Œ๊ฐ€ ๋งŽ์€ ์ƒ์„ฑ์ž๋ฅผ ์œ„ํ•ด ๋ ˆ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ๋ฌธ์„ ํ†ตํ•ด ์ž๋ฃŒํ˜•์˜ ๊ฐ’๋“ค์— ์ด๋ฆ„ ์ง“๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค. data Foo2 = Bar2 | Baz2 {bazNumber::Int, bazName::String} ๋ ˆ์ฝ”๋“œ๋ฅผ ์“ฐ๋ฉด ๊ด€์‹ฌ ์žˆ๋Š” ๋ณ€์ˆ˜๋งŒ ๋งค์นญ, ๋ฐ”์ธ๋”ฉ ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ฝ”๋“œ๊ฐ€ ๋ณด๋‹ค ๊น”๋”ํ•ด์ง„๋‹ค. h :: Foo2 -> Int h Baz2 {bazName=name} = length name h Bar2 {} = 0 data ์„ ์–ธ์—์„œ ๋ ˆ์ฝ”๋“œ๋ฅผ ์“ฐ์ง€ ์•Š๋”๋ผ๋„, {} ํŒจํ„ด์œผ๋กœ ์›์†Œ๋“ค์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ์ƒ๊ด€์—†์ด ์ƒ์„ฑ์ž๋ฅผ ๋งค์นญํ• ` ์ˆ˜ ์žˆ๋‹ค. data Foo = Bar | Baz Int g :: Foo -> Bool g Bar {} = True g Baz {} = False ์ƒ์„ฑ์ž Bar์™€ Baz์˜ ์›์†Œ๋“ค์˜ ๊ฐœ์ˆ˜๋‚˜ ํƒ€์ž…์„ ๋ฐ”๊พธ๋”๋ผ๋„ ํ•จ์ˆ˜ g๋Š” ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณด์ถฉ ์„ค๋ช… ์žฅ์˜ ๊ธฐ๋ช… ํ•„๋“œ ์ ˆ์—์„œ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋” ์ž์„ธํžˆ ๋‹ค๋ฃฐ ๋•Œ ๋ ˆ์ฝ”๋“œ ๊ตฌ๋ฌธ์˜ ์ถ”๊ฐ€์ ์ธ ์ด์ ์„ ๋ณผ ๊ฒƒ์ด๋‹ค. ํŒจํ„ด ๋งค์นญ์„ ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ณณ ์งง๊ฒŒ ๋งํ•˜๋ฉด ๋ณ€์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ๋ชจ๋“  ๊ณณ์— ํŒจํ„ด ๋งค์นญ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ „๋ก€๋ฅผ ํ•œ ๋ฒˆ ํ›‘์–ด๋ณด์ž. ๋‹ค์Œ ์žฅ์—์„œ ๋ช‡ ๊ฐ€์ง€๋ฅผ ๋” ์†Œ๊ฐœํ•  ๊ฒƒ์ด๋‹ค. ๋“ฑ์‹ ๊ฐ€์žฅ ๋ช…ํ™•ํ•œ ์šฉ๋ก€๋Š” ํ•จ์ˆ˜ ์ •์˜์‹์˜ ์ขŒ๋ณ€์œผ๋กœ, ์ง€๊ธˆ๊นŒ์ง€์˜ ์˜ˆ์ œ์˜ ์ฃผ์ œ์˜€๋‹ค. map _ [] = [] map f (x:xs) = f x : map f xs ๋‘ ๋“ฑ์‹ ๋ชจ๋‘ ์ขŒ๋ณ€์—์„œ ํŒจํ„ด ๋งค์นญ์„ ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์‹์—์„œ๋Š” ๋ณ€์ˆ˜ ๋ฐ”์ธ๋”ฉ๋„ ์ˆ˜ํ–‰ํ•œ๋‹ค. let ํ‘œํ˜„์‹๊ณผ where ์ ˆ let๊ณผ where๋Š” ๋กœ์ปฌ ๋ณ€์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ์ˆ˜๋‹จ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ํŒจํ„ด ๋งค์นญ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. y = let (x:_) = map (*2) [1,2,3] in x + 5 ์ด๋Š” ๋‹ค์Œ๊ณผ ๋™๋“ฑํ•˜๋‹ค. y = x + 5 where (x:_) = map (*2) [1,2,3] x๋Š” map ((*) 2) [1,2,3]์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. y๋Š” ๋”ฐ๋ผ์„œ 2 + 5 = 7๋กœ ๋ณ€ํ™˜๋œ๋‹ค. ๋žŒ๋‹ค ์ถ”์ƒํ™” ๋žŒํƒ€ ์ถ”์ƒํ™”์— ํŒจํ„ด ๋งค์นญ์„ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. swap = \(x, y) -> (y, x) ํ•˜์ง€๋งŒ ์ด ๊ตฌ๋ฌธ์€ ๋‹จ ํ•˜๋‚˜์˜ ํŒจํ„ด๋งŒ ํ—ˆ์šฉํ•œ๋‹ค. (์ธ์ž๊ฐ€ ์—ฌ๋Ÿฟ์ธ ๋žŒ๋‹ค ์ถ”์ƒํ™”์˜ ๊ฒฝ์šฐ ์ธ์ž ๋‹น ํ•œ ํŒจํ„ด) ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹(list comprehension) ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์—์„œ | ๋’ค์— ํŒจํ„ด ๋งค์นญ์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ๊ต‰์žฅํžˆ ์œ ์šฉํ•˜๋ฉฐ ์กฐ๊ฑด ์ œ์‹œ์‹์˜ ํ‘œํ˜„๋ ฅ์„ ํ’๋ถ€ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค. ๊ทธ ์ž‘๋™๋ฒ•์„ ๋‹ค์†Œ ๋ณต์žกํ•œ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์‚ดํŽด๋ณด์ž. Prelude๋Š” ๋‹ค์Œ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง€๋Š” Maybe ํƒ€์ž…์„ ์ œ๊ณตํ•œ๋‹ค. data Maybe a = Nothing | Just a Maybe๋Š” ๋Œ€๊ฐœ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ๋ชจ๋ฅด๋Š” ์ž‘์—…์˜ ๊ฒฐ๊ด๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐ ์“ฐ์ธ๋‹ค. ์ž‘์—…์ด ์„ฑ๊ณตํ•˜๋ฉด Just ์ƒ์„ฑ์ž๊ฐ€ ์‚ฌ์šฉ๋˜์–ด ๊ฒฐ๊ด๊ฐ’์ด ์ „๋‹ฌ๋œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด Nothing์ด ์‚ฌ์šฉ๋œ๋‹ค. 3 ๋ณด์กฐ ํ•จ์ˆ˜ catMaybes (Data.Maybe ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ์— ์žˆ์Œ)๋Š” Maybe์˜ ๋ฆฌ์ŠคํŠธ("Just"์™€ "Nothing" Maybe๋ฅผ ๋ชจ๋‘ ํฌํ•จ)๋ฅผ ์ทจํ•ด Nothing์€ ๊ฑธ๋Ÿฌ๋‚ด๊ณ  Just x์—์„œ Just ๋ž˜ํผ๋ฅผ ๊ฑท์–ด๋‚ด ๊ทธ ์•ˆ์˜ ๊ฐ’์„ ์–ป๋Š”๋‹ค. ๋ฆฌ์ŠคํŠธ ํ•ด์„์„ ์ด์šฉํ•˜๋ฉด ๋งค์šฐ ์ง๊ด€์ ์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. catMaybes :: [Maybe a] -> [a] catMaybes ms = [ x | Just x <- ms ] ์ด ์ž‘์—…์— ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์„ ์“ธ ๋•Œ์˜ ๋˜ ๋‹ค๋ฅธ ์žฅ์ ์€ ํŒจํ„ด ๋งค์นญ์ด ์‹คํŒจํ•˜๋ฉด(์ฆ‰ Nothing์„ ๋งŒ๋‚˜๋ฉด) ms์˜ ๋‹ค์Œ ์›์†Œ๋กœ ์ด๋™ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์—ฌ ์šฐ๋ฆฌ๊ฐ€ ๊ด€์‹ฌ ์—†๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. 4 do ๋ธ”๋ก ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ ์žฅ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ do ๋ธ”๋ก์—์„œ๋„ ์™ผ์ชฝ ํ™”์‚ดํ‘œ ๋ฐ”์ธ๋”ฉ์˜ ์ขŒ๋ณ€์— ํŒจํ„ด ๋งค์นญ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. putFirstChar = do (c:_) <- getLine putStrLn [c] do ๋ธ”๋ก ๋‚ด์˜ let ๋ฐ”์ธ๋”ฉ์€ ํŒจํ„ด ๋งค์นญ์— ๊ด€ํ•œ ํ•œ ์ง„์งœ let ํ‘œํ˜„์‹์ด๋‚˜ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ๋…ธํŠธ ์ด ํŠน์ •ํ•œ ์ž‘์—…์—๋Š” ํ•ฉ๋ฆฌ์ ์ธ๋ฐ, ๊ทธ ์ด์œ ๋Š” dropThree๋ฅผ ๊ฐ€๋ น ์›์†Œ๊ฐ€ 2๊ฐœ์ธ ๋ฆฌ์ŠคํŠธ์— ์ ์šฉํ–ˆ์„ ๋•Œ []๋ฅผ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๋ผ ์˜ˆ์ƒํ•˜๋Š” ๊ฒŒ ์ž์—ฐ์Šค๋Ÿฝ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‹ค๋ฅธ ๋ฌธ์ œ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ๋งค์นญ์ด ์‹คํŒจํ–ˆ๋‹ค๊ณ  ์ž„์˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒŒ ๋ง์ด ์•ˆ ๋  ์ˆ˜๋„ ์žˆ๋‹ค. ๋‚˜์ค‘์— ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. โ†ฉ ์˜ˆ์ƒํ–ˆ๊ฒ ์ง€๋งŒ ์ด๋Ÿฐ ์œ ์˜ ๋ฆฌํ„ฐ๋Ÿด๊ณผ์˜ ๋งค์นญ์€ ์ƒ์„ฑ์ž์— ๊ธฐ๋ฐ˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ๊ทธ ์ด๋ฉด์—์„œ๋Š” ๋“ฑ์‹ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. โ†ฉ ๊ทธ๋Ÿฐ ์—ฐ์‚ฐ์˜ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ๋Š” ์‚ฌ์ „(dictionary)์—์„œ ๊ฐ’์„ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ ๊ตฌํ˜„์€ ๋‹จ์ˆœํžˆ ํ‚ค, ๊ฐ’์˜ ์ง์„ ๋‚˜ํƒ€๋‚ด๋Š” ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ [(a, b)]์ผ ์ˆ˜๋„ ์žˆ๊ณ  ๋” ๋ณต์žกํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์–ด๋Š ๊ฒฝ์šฐ๋“  ์ž„์˜์˜ ํ‚ค๋กœ ๊ฐ’์— ์ ‘๊ทผํ•˜๋ ค ํ•  ๋•Œ ๊ทธ ํ‚ค์™€ ์—ฐ๊ด€๋œ ๊ฐ’์„ ์ฐพ์œผ๋ฆฌ๋ผ๋Š” ๋ณด์žฅ์€ ์—†๋‹ค. โ†ฉ ์ด๊ฒŒ ํŒจํ„ด ๋งค์นญ ์‹คํŒจ๋กœ ๊ณ ์žฅ ๋‚˜์ง€ ์•Š๊ณ  ์ž‘๋™ํ•˜๋Š” ์ด์œ ๋Š” ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์˜ ๋ณธ ๋ชจ์Šต๊ณผ ๊ด€๋ จ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์€ ์‚ฌ์‹ค ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ์˜ ๋ž˜ํผ๋‹ค. ์ด๊ฒŒ ๋ฌด์Šจ ๋œป์ธ์ง€๋Š” ๋ชจ๋‚˜๋“œ๋ฅผ ๋…ผํ•  ๋•Œ ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. โ†ฉ 6 ์ œ์–ด ๊ตฌ์กฐ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Control_structures if์™€ guard ์žฌ์กฐ๋ช… if ํ‘œํ˜„์‹ ๋ผ์›Œ ๋„ฃ๊ธฐ case ํ‘œํ˜„์‹ ์•ก์…˜ ์ œ์–ด ์žฌ์กฐ๋ช… return์— ๊ด€ํ•œ ๋…ธํŠธ ํ•˜์Šค์ผˆ์€ ์—ฌ๋Ÿฌ ๊ฐ’ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ช‡ ๊ฐ€์ง€ ์ œ๊ณตํ•œ๋‹ค. ํ•˜์Šค ์ผˆ ๊ธฐ์ดˆ ์žฅ์—์„œ ๊ทธ์ค‘ ์ผ๋ถ€๋ฅผ ์‚ดํŽด๋ดค๋‹ค. ์ด ์ ˆ์—์„œ๋Š” ์ง€๊ธˆ๊นŒ์ง€ ๋ดค๋˜ ๊ฒƒ๋“ค์„ ์ข€ ๋” ์ƒ์„ธํžˆ ๋…ผ์˜ํ•˜๊ณ  ์ƒˆ๋กœ์šด ์ œ์–ด ๊ตฌ์กฐ๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. if์™€ guard ์žฌ์กฐ๋ช… ์ด๋ฏธ ๋ดค๋˜ ๊ฒƒ์ด์ง€๋งŒ, ๋‹ค์Œ์€ if ํ‘œํ˜„์‹์˜ ๊ตฌ๋ฌธ์ด๋‹ค. if <condition> then <true-value> else <false-value> <condition>์€ ๋ถˆ๋ฆฌ์–ธ์œผ๋กœ ํ‰๊ฐ€๋˜๋Š” ํ‘œํ˜„์‹์ด๋‹ค. <condition>์ด True ์ด๋ฉด <true-value>๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ  ์•„๋‹ˆ๋ฉด <false-value>๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. ํ•˜์Šค์ผˆ์—์„œ if๋Š” ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜๋˜๋Š” ํ‘œํ˜„์‹์ด์ง€, ๋ช…๋ นํ˜• ์–ธ์–ด๋“ค๊ณผ ๋‹ฌ๋ฆฌ ์‹คํ–‰๋˜๋Š” ๋ช…๋ น๋ฌธ statement์ด ์•„๋‹ˆ๋‹ค. 1 ๊ทธ๋ž˜์„œ ํ•˜์Šค์ผˆ์—์„œ๋Š” else๊ฐ€ ํ•„์ˆ˜๋‹ค. if๊ฐ€ ํ‘œํ˜„์‹์ด๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ฑด์ด ์ฐธ์ด๋ƒ ๊ฑฐ์ง“์ด๋ƒ์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด์•ผ ํ•˜๋ฉฐ else๋Š” ์ด๋ฅผ ๋ณด์žฅํ•œ๋‹ค. ๋˜ํ•œ <true-value>์™€ <false-value>๋Š” ๊ฐ™์€ ํƒ€์ž…์œผ๋กœ ํ‰๊ฐ€๋˜์–ด์•ผ ํ•˜๊ณ  ๊ทธ ํƒ€์ž…์ด if ํ‘œํ˜„์‹ ์ „์ฒด์˜ ํƒ€์ž…์ด ๋œ๋‹ค. if ํ‘œํ˜„์‹์„ ์—ฌ๋Ÿฌ ์ค„์— ์ชผ๊ฐœ์–ด ์“ธ ๊ฒฝ์šฐ else์˜ ๋“ค์—ฌ ์“ฐ๊ธฐ๋Š” ๋Œ€๊ฐœ if๊ฐ€ ์•„๋‹ˆ๋ผ then์— ๋งž์ถฐ์ง„๋‹ค. ํ”ํžˆ ์ด๋Ÿฐ ์Šคํƒ€์ผ์„ ์“ด๋‹ค. describeLetter :: Char -> String describeLetter c = if c >= 'a' && c <= 'z' then "Lower case" else if c >= 'A' && c <= 'Z' then "Upper case" else "Not an ASCII letter" guard์™€ ์ตœ์ƒ์œ„ if ํ‘œํ˜„์‹์€ ๋Œ€๊ฐœ ์„œ๋กœ ๋ฐ”๊ฟ” ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ๋Š” guard๋ฅผ ์ด์šฉํ•˜๋ฉด ์กฐ๊ธˆ ๊น”๋”ํ•ด์ง„๋‹ค. describeLetter :: Char -> String describeLetter c | c >= 'a' && c <= 'z' = "Lower case" | c >= 'A' && c <= 'Z' = "Upper case" | otherwise = "Not an ASCII letter" otherwise๋Š” ๋‹จ์ง€ True์˜ ๋ณ„์นญ์ž„์„ ๊ธฐ์–ตํ•˜์ž. ๋งˆ์ง€๋ง‰ guard๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํฌ์ฐฉํ•ด if ํ‘œํ˜„์‹ ๋์˜ else์™€ ๊ฐ™์€ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๊ฐ€๋“œ๋Š” ์ˆœ์„œ๋Œ€๋กœ ํ‰๊ฐ€๋œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์„ฑ์„ ๊ณ ๋ คํ•ด ๋ณด์ž. f (pattern1) | predicate1 = w | predicate2 = x f (pattern2) | predicate3 = y | predicate4 = z f์˜ ์ธ์ˆ˜๋Š” pattern1๊ณผ ํŒจํ„ด ๋งค์นญ๋œ๋‹ค. ์„ฑ๊ณตํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ ๊ฐ€๋“œ๋กœ ์ง„ํ–‰ํ•œ๋‹ค. predicate1์ด True๋กœ ํ‰๊ฐ€๋˜๋ฉด w๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ  ์•„๋‹ˆ๋ฉด predicate2๊ฐ€ ํ‰๊ฐ€๋œ๋‹ค. predicate2๊ฐ€ ์ฐธ์ด๋ฉด x๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. ์ด๋ฒˆ์—๋„ ์•„๋‹ˆ๋ฉด ๋‹ค์Œ ๊ฒฝ์šฐ๋กœ ๋„˜์–ด๊ฐ€ ์ธ์ˆ˜๋ฅผ pattern2์™€ ์ผ์น˜์‹œํ‚ค๋ ค ์‹œ๋„ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  guard์— ๋Œ€ํ•ด predicate3์™€ predicate4๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ™์€ ์ ˆ์ฐจ๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค. ๋ฌผ๋ก  ์–ด๋Š ํŒจํ„ด, ์–ด๋Š ์ˆ ์–ด์‹์—๋„ ํŒจํ„ด์ด ๋งž์ง€ ์•Š์œผ๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค. ์–ด๋Š ์ œ์–ด ๊ตฌ์กฐ๋ฅผ ํƒํ•˜๋“  ๋ชจ๋“  ๊ฒฝ์šฐ๊ฐ€ ์ฒ˜๋ฆฌ๋˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. if ํ‘œํ˜„์‹ ๋ผ์›Œ ๋„ฃ๊ธฐ if๊ฐ€ ํ‘œํ˜„์‹์ด๋ผ๋Š” ์ ์—์„œ ๋‚˜์˜ค๋Š” ๊ฒฐ๋ก ์€ ํ•˜์Šค ์ผˆ ํ‘œํ˜„์‹์ด ๋“ค์–ด๊ฐ€๋Š” ์–ด๋””๋“  if๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์œผ๋กœ, ์ด๋Ÿฐ ์ฝ”๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. g x y = (if x == 0 then 1 else sin x / x) * y ์ค„๋ฐ”๊ฟˆ ์—†์ด if ํ‘œํ˜„์‹์„ ์จ์„œ ๊ฐ„๊ฒฐํ•จ์„ ๊ทน๋Œ€ํ™”ํ–ˆ๋‹ค. if์™€ ๋‹ฌ๋ฆฌ guard ๋ธ”๋ก์€ ํ‘œํ˜„์‹์ด ์•„๋‹ˆ์–ด์„œ, ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด let์ด๋‚˜ where ์ •์˜๊ฐ€ ์ตœ์„ ์ด๋‹ค. ๋ฌผ๋ก  ๋ณต์žกํ•œ ํ•œ ์ค„ if ํ‘œํ˜„์‹์€ ์ฝ๊ธฐ ํž˜๋“ค ๊ฒƒ์ด๋ฉฐ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ let๊ณผ where๊ฐ€ ๊ดœ์ฐฎ์€ ๋Œ€์•ˆ์ด๋‹ค. case ํ‘œํ˜„์‹ ์•„์ง case ํ‘œํ˜„์‹์ด๋ผ๋Š” ์ œ์–ด ๊ตฌ์กฐ๋ฅผ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ, case์™€ ์กฐ๊ฐ ํ•จ์ˆ˜ ์ •์˜์˜ ๊ด€๊ณ„๋Š” if ํ‘œํ˜„์‹๊ณผ guard์˜ ๊ด€๊ณ„์™€ ์œ ์‚ฌํ•˜๋‹ค. ๋‹ค์Œ์˜ ๊ฐ„๋‹จํ•œ ์กฐ๊ฐ ์ •์˜๋ฅผ ๋ณด์ž. f 0 = 18 f 1 = 15 f 2 = 12 f x = 12 - x ์ด๊ฒƒ์€ ๋‹ค์Œ์˜ ์˜ˆ์ œ์™€ ๋™๋“ฑํ•˜๋ฉฐ, ์‚ฌ์‹ค ์•„๋ž˜ ์ฝ”๋“œ์˜ ํŽธ์˜ ๊ตฌ๋ฌธ์ด๋‹ค. f x = case x of 0 -> 18 1 -> 15 2 -> 12 _ -> 12 - x ๋‘˜ ์ค‘ ์–ด๋Š ์ •์˜๋ฅผ ์„ ํƒํ•˜๋“  f๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๊ฐ™์€ ๊ณผ์ •์ด ๋ฐœ์ƒํ•œ๋‹ค. ์ธ์ˆ˜ x๋Š” ๋ชจ๋“  ํŒจํ„ด์— ์ˆœ์„œ๋Œ€๋กœ ๋งค์นญ๋œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋งค์นญ์—์„  = ๊ธฐํ˜ธ(์กฐ๊ฐ ๋ฒ„์ „) ๋˜๋Š” ํ™”์‚ดํ‘œ(case ๋ฒ„์ „)์˜ ์šฐ๋ณ€์— ์žˆ๋Š” ํ‘œํ˜„์‹์ด ํ‰๊ฐ€๋œ๋‹ค. ์ด case ํ‘œํ˜„์‹์—์„œ ํŒจํ„ด์— x๋ฅผ ์ ์„ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. ๋งŒ๋Šฅ ํŒจํ„ด _์ด ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๊ฐ–๋Š”๋‹ค. 2 case๋ฅผ ์‚ฌ์šฉํ•  ๋• ๋“ค์—ฌ ์“ฐ๊ธฐ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. ๊ฐ ๋ถ„๊ธฐ๋Š” of ํ‚ค์›Œ๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ค„์˜ ์‹œ์ž‘์ ๋ณด๋‹ค ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋“ค์—ฌ ์จ์•ผ ํ•˜๋ฉฐ ๋ชจ๋“  ๋ถ„๊ธฐ์˜ ๋“ค์—ฌ ์“ฐ๊ธฐ๊ฐ€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ์„ค๋ช…์„ ์œ„ํ•ด case ํ‘œํ˜„์‹์˜ ์˜ฌ๋ฐ”๋ฅธ ์–‘์‹ ๋‘ ๊ฐœ๋ฅผ ์ฒจ๋ถ€ํ•œ๋‹ค. f x = case x of 0 -> 18 1 -> 15 2 -> 12 _ -> 12 - x f x = case x of 0 -> 18 1 -> 15 2 -> 12 _ -> 12 - x case ๋ถ„๊ธฐ์˜ ์ขŒ๋ณ€์€ ํŒจํ„ด์ผ ๋ฟ์ด๋ฏ€๋กœ ์กฐ๊ฐ ํ•จ์ˆ˜ ์ •์˜์˜ ๊ฒฝ์šฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ”์ธ๋”ฉ์— ์“ฐ์ผ ์ˆ˜ ์žˆ๋‹ค. describeString :: String -> String describeString str = case str of (x:xs) -> "The first character of the string is: " ++ [x] ++ "; and " ++ "there are " ++ show (length xs) ++ " more characters in it." [] -> "This is an empty string." ์ด ํ•จ์ˆ˜๋Š” str์˜ ์ผ๋ถ€ ์†์„ฑ์„ ์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด๋กœ ์„œ์ˆ ํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋จธ๋ฆฌ์™€ ๊ผฌ๋ฆฌ์— ๋ณ€์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•  ๋•Œ case ๊ตฌ๋ฌธ์ด ํŽธ๋ฆฌํ•˜์ง€๋งŒ, if ๋ฌธ๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. (๋นˆ ๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ null str ์กฐ๊ฑด์„ ์ด์šฉ) ๋งˆ์ง€๋ง‰์œผ๋กœ if ํ‘œํ˜„์‹์ฒ˜๋Ÿผ case ํ‘œํ˜„ ์‹๋„ ๋‹ค๋ฅธ ํ‘œํ˜„์‹์ด ๋“ค์–ด๊ฐ€๋Š” ์–ด๋””๋“  ๋ผ์›Œ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. (์กฐ๊ฐ ์ •์˜๋Š” ๊ทธ๋Ÿด ์ˆ˜ ์—†๋‹ค) data Colour = Black | White | RGB Int Int Int describeBlackOrWhite :: Colour -> String describeBlackOrWhite c = "This colour is" ++ case c of Black -> " black" White -> " white" RGB 0 0 0 -> " black" RGB 255 255 255 -> " white" _ -> "... uh... something else" ++ ", yeah?" ์œ„์˜ case ๋ธ”๋ก์€ ์–ด๋Š ๋ฌธ์ž์—ด์—๋„ ๋“ค์–ด๋งž๋Š”๋‹ค. describeBlackOrWhite๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ž‘์„ฑํ•˜๋ฉด let/where๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. (์ •์˜๋Š” ์ข€ ์ฝ๊ธฐ ์–ด๋ ค์›Œ์ง€์ง€๋งŒ) ์—ฐ์Šต๋ฌธ์ œ case ๋ฌธ์œผ๋กœ fakeIf ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ if ํ‘œํ˜„์‹์„ ๋Œ€์ฒดํ•ด ๋ณด์ž. ์•ก์…˜ ์ œ์–ด ์žฌ์กฐ๋ช… ์ด๋ฒˆ ์žฅ์˜ ๋งˆ์ง€๋ง‰์œผ๋กœ์„œ ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ ์žฅ์˜ ๋…ผ์˜๋ฅผ ๋˜์งš์–ด๋ณด๋ฉฐ ์ œ์–ด ๊ตฌ์กฐ์— ๊ด€ํ•œ ์ถ”๊ฐ€ ์‚ฌํ•ญ์„ ๋ช‡ ๊ฐœ ์†Œ๊ฐœํ•˜๊ฒ ๋‹ค. ์•ก์…˜ ์ œ์–ด ์ ˆ์—์„œ ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด do ๋ธ”๋ก ๋‚ด์—์„œ if ํ‘œํ˜„์‹์„ ์ด์šฉํ•œ ์•ก์…˜์˜ ์กฐ๊ฑด๋ถ€ ์‹คํ–‰์„ ์„ค๋ช…ํ•œ ๋ฐ” ์žˆ๋‹ค. doGuessing num = do putStrLn "Enter your guess:" guess <- getLine if (read guess) < num then do putStrLn "Too low!" doGuessing num else if (read guess) > num then do putStrLn "Too high!" doGuessing num else do putStrLn "You Win!" case ํ‘œํ˜„์‹์œผ๋กœ ๋™์ผํ•œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด ์ผ๋‹จ Prelude ํ•จ์ˆ˜ compare๋ฅผ ๋„์ž…ํ•ด์•ผ ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๊ฐ™์€ ํƒ€์ž…์ธ ๋‘ ๊ฐ’์„ ์ทจํ•ด Ordering ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ๊ทธ ๊ฐ’์€ GT, LT, EQ ์ค‘ ํ•˜๋‚˜๋กœ ์ฒซ ๋ฒˆ์งธ๊ฐ€ ๋‘ ๋ฒˆ์งธ๋ณด๋‹ค ํฐ์ง€, ์ž‘์€์ง€, ๊ฐ™์€์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. doGuessing num = do putStrLn "Enter your guess:" guess <- getLine case compare (read guess) num of LT -> do putStrLn "Too low!" doGuessing num GT -> do putStrLn "Too high!" doGuessing num EQ -> putStrLn "You Win!" -> ๋’ค์˜ do๋Š” LT, GT์—๋งŒ ํ•„์š”ํ•˜๋‹ค. ๋‘ ๊ฒฝ์šฐ์—๋งŒ ์•ก์…˜์„ ๊ณ„์†ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. return์— ๊ด€ํ•œ ๋…ธํŠธ ์ด์ œ ํ˜ผ๋ž€์˜ ์—ฌ์ง€๋ฅผ ์—†์• ๋ ค๊ณ  ํ•œ๋‹ค. ํ†ต์ƒ C ๊ฐ™์€ ๋ช…๋ นํ˜• ์–ธ์–ด์—์„œ๋Š” doGuessing์„ ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๋‹ค(C๋ฅผ ๋ชจ๋ฅธ๋‹ค๋ฉด ์„ธ๋ถ€์ ์ธ ๊ฑด ์ œ์ณ๋‘๊ณ  if-else ์—ฐ์‡„๋งŒ ๋”ฐ๋ผ๊ฐˆ ๊ฒƒ). void doGuessing(int num) { printf("Enter your guess:"); int guess = atoi(readLine()); if (guess == num) { printf("You win!\n"); return (); } // we won't get here if guess == num if (guess < num) { printf("Too low!\n"); doGuessing(num); } else { printf("Too high!\n"); doGuessing(num); } } ์ด doGuessing์€ ๋จผ์ € ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค. ์ด๋•Œ๋Š” doGuessing์„ ์ƒˆ๋กœ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  if์— else๊ฐ€ ๋”ฐ๋ผ๋ถ™์ง€ ์•Š๋Š”๋‹ค. ์ถ”์ธก์ด ๋งž์•˜๋‹ค๋ฉด return ๋ฌธ์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•˜์—ฌ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋“ค์€ ๊ฑด๋„ˆ๋›ด๋‹ค. ํ•˜์Šค ์ผˆ๋กœ ๋Œ์•„๊ฐ€์„œ, do ๋ธ”๋ก ๋‚ด์˜ ์•ก์…˜ ์—ฐ์‡„๋Š” ๋งˆ์น˜ ๋ช…๋ นํ˜• ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ณด์ด๊ณ , ์‹ค์ œ๋กœ Prelude์—๋Š” return์ด ์žˆ๋‹ค. ๊ทธ๋Ÿผ case ๋ฌธ์ด if ๋ฌธ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค๋ฃจ๋„๋ก ๊ฐ•์ œํ•˜๊ธฐ ์•Š๋Š” ๊ฑธ ์•„๋Š” ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์œ„์˜ C ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์˜ฎ๊ฒจ๋ณด๋ ค ํ•˜์ง€ ์•Š์•˜์„๊นŒ? (๊ถ๊ธˆํ•˜๋ฉด ์ง์ ‘ ์‹คํ–‰ํ•ด ๋ณด๋ผ...) doGuessing num = do putStrLn "Enter your guess:" guess <- getLine case compare (read guess) num of EQ -> do putStrLn "You win!" return () -- we don't expect to get here if guess == num if (read guess < num) then do print "Too low!"; doGuessing num else do print "Too high!"; doGuessing num ...๊ทธ๋Ÿฐ๋ฐ ์ƒ๊ฐ๋Œ€๋กœ ๋˜์ง€ ์•Š๋Š”๋‹ค! ๋งž๊ฒŒ ์ถ”์ธกํ•œ ๊ฒฝ์šฐ ์ด ํ•จ์ˆ˜๋Š” "You win!"์„ ์ถœ๋ ฅํ•˜๊ณ , return()์—์„œ ์ข…๋ฃŒ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹  if ํ‘œํ˜„์‹์œผ๋กœ ์ด์–ด๊ฐ€์„œ guess๊ฐ€ num๋ณด๋‹ค ์ž‘์€์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. ๋ฌผ๋ก  ๊ทธ๋ ‡์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— else ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€ "Too high!"๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์žฌ์ถ”์ธก์„ ์š”์ฒญํ•œ๋‹ค. ์˜ค ์ธก์„ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋‚˜์„ ๊ฒƒ์ด ์—†๋‹ค. ์ด์ œ case ๋ฌธ์„ ํ‰๊ฐ€ํ•˜์—ฌ compare์˜ ๊ฒฐ๊ณผ๋กœ LT๋‚˜ GT๋ฅผ ์–ป์œผ๋ ค ํ•  ๊ฒƒ์ด๊ณ , ์–ด๋Š ๊ฒฝ์šฐ๋“  ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์ด ์—†์–ด ํ”„๋กœ๊ทธ๋žจ์€ ๊ฒฐ๊ตญ ์˜ˆ์™ธ๋ฅผ ๋งŒ๋“ค๋ฉฐ ์ฆ‰์‹œ ์‹คํŒจํ•  ๊ฒƒ์ด๋‹ค. (๋ถˆ์™„์ „ํ•œ case ์ž์ฒด๋งŒ์œผ๋กœ ์˜์‹ฌ์„ ๋ถˆ๋Ÿฌ์ผ์œผํ‚ค๊ธฐ ์ถฉ๋ถ„ํ•˜๋‹ค) ์—ฌ๊ธฐ์„œ์˜ ๋ฌธ์ œ๋Š” return์ด C๋‚˜ ์ž๋ฐ”์˜ ์ด๋ฆ„์ด ๊ฐ™์€ ๊ทธ ๋ช…๋ น๋ฌธ๊ณผ ์ „ํ˜€ ๊ฐ™์€ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋Š” ์ ์ด๋‹ค. ๋‹น๋ถ„๊ฐ„์€ return์ด ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋ผ๊ณ  ์น˜์ž. 4 return()์€ ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋Š” ์•ก์…˜์œผ๋กœ ํ‰๊ฐ€๋œ๋‹ค. return์€ ์ œ์–ด ํ๋ฆ„์— ์•„๋ฌด ์˜ํ–ฅ๋„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค. ๋งž๊ฒŒ ์ถ”์ธกํ•œ ๊ฒฝ์šฐ case ๋ฌธ์€ IO ํƒ€์ž…์˜ ์•ก์…˜์ธ return()์„ ํ‰๊ฐ€ํ•˜๋ฉฐ ์‹คํ–‰์€ ์ •์ƒ์ ์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค. ๊ฒฐ๋ก ์€ ์•ก์…˜๊ณผ do ๋ธ”๋ก์ด ๋ช…๋ นํ˜• ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ํ•˜์Šค์ผˆ์˜ ๊ณ ์œ  ๋ฌธ๋ฒ•์„ ๋”ฐ๋ผ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ 1. ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ/์•ก์…˜ ์ œ์–ด์˜ "Haskell greeting" ์˜ˆ์ œ๋ฅผ case ๋ฌธ์œผ๋กœ ์žฌ์ž‘์„ฑํ•˜๋ผ. 2. ๋‹ค์Œ ํ”„๋กœ๊ทธ๋žจ์€ ๋ฌด์—‡์„ ์ถœ๋ ฅํ•˜๋Š”๊ฐ€? ๊ทธ ์ด์œ ๋Š”? main = do x <- getX putStrLn x getX = do return "My Shangri-La" return "beneath" return "the summer moon" return "I will" return "return" return "again" C๋‚˜ ์ž๋ฐ”๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•ด๋ดค๋‹ค๋ฉด, ํ•˜์Šค์ผˆ์˜ if/then/else๊ฐ€ ์‚ผ ํ•ญ ์—ฐ์‚ฐ์ž ?:์™€ ๋™๋“ฑ ๋‹คํ•˜๋Š” ๊ฒƒ์„ ์•Œ์•„์ฑ˜์„ ๊ฒƒ์ด๋‹ค. โ†ฉ ์™œ ์ด๋Ÿฐ์ง€๋ฅผ ๋ณด๋ ค๋ฉด ํŒจํ„ด ๋งค์นญ ์ ˆ์˜ ๋งค์นญ, ๋ฐ”์ธ๋”ฉ์— ๊ด€ํ•œ ๋…ผ์˜๋ฅผ ์‚ดํŽด๋ณผ ๊ฒƒ. โ†ฉ ๋”ฐ๋ผ์„œ ์ˆซ์ž<NAME>์— ๋Œ€ํ•œ ์ผ์น˜ ๋น„๊ต๋งŒ์œผ๋กœ ์ œํ•œ๋˜๋Š” ๋ช…๋ นํ˜• ์–ธ์–ด์˜ switch/case ๋ฌธ๋ณด๋‹ค ํ•˜์Šค์ผˆ์˜ case ๋ฌธ์ด ๋ณด๋‹ค ๋‹ค์žฌ๋‹ค๋Šฅํ•˜๋‹ค. โ†ฉ ๋ณด์ถฉ ์„ค๋ช…: ๋ณด๋‹ค ์ •ํ™•ํžˆ ์„ค๋ช…ํ•˜์ž๋ฉด, return์€ ๊ฐ’์„ ์ทจํ•ด, ๊ทธ ๊ฐ’์„ ํ‰๊ฐ€๋˜๋Š” ์‹œ์ ์— ๊ฐ€์„œ ์›๋ž˜ ๊ฐ’์„ ๋Œ๋ ค์ฃผ๋Š” ์•ก์…˜์œผ๋กœ ๋งŒ๋“ ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด ์˜ˆ์‹œ์˜ do ๋ธ”๋ก๋“ค ์ค‘ ํ•˜๋‚˜์— return "strawberry"๋ฅผ ๋„ฃ์œผ๋ฉด IO String ํƒ€์ž…์„ ๊ฐ€์งˆ ๊ฒƒ์ด๋ฉฐ ์ด๋Š” getLine๊ณผ ๊ฐ™์€ ํƒ€์ž…์ด๋‹ค. ๋ง์ด ์•ˆ ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„๋„ ๊ฑฑ์ •ํ•˜์ง€ ๋ง๋ผ. ๋‚˜์ค‘์— ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋ชจ๋‚˜๋“œ๋ฅผ ๋…ผ์˜ํ•  ๋•Œ return์ด ์ง„์งœ๋กœ ํ•˜๋Š” ์ผ์„ ๊นจ๋‹ซ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. โ†ฉ 7 ํ•จ์ˆ˜ ๋ณด์ถฉ ์„ค๋ช… ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/More_on_functions let๊ณผ where ์žฌ์กฐ๋ช… ์ต๋ช… ํ•จ์ˆ˜ - ๋žŒ๋‹ค(lambda) ์—ฐ์‚ฐ์ž ์„น์…˜ section ์—ฐ์Šต๋ฌธ์ œ ํ•จ์ˆ˜ ์‚ฌ์šฉ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ›Œ๋ฅญํ•œ ๊ธฐ๋Šฅ๋“ค์ด ์žˆ๋‹ค. let๊ณผ where ์žฌ์กฐ๋ช… ์ด์ „์˜ ์—ฌ๋Ÿฌ ์žฅ์—์„œ ๋…ผ์˜ํ•œ ๋ฐ”์™€ ๊ฐ™์ด let๊ณผ where๋Š” ๋กœ์ปฌ ํ•จ์ˆ˜ ์ •์˜์— ์œ ์šฉํ•˜๋‹ค. addStr :: Float -> String -> Float addStr x str = x + read str sumStr :: [String] -> Float sumStr = foldl addStr 0.0 addStr์„ ๋‹ค๋ฅธ ๋ฐ์„œ ์“ธ ์ผ์ด ์ ˆ๋Œ€ ์—†๋‹ค๋ฉด? ๋กœ์ปฌ ๋ฐ”์ธ๋”ฉ์„ ์ด์šฉํ•ด์„œ sumStr์„ ์žฌ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. sumStr = let addStr x str = x + read str in foldl addStr 0.0 where ์ ˆ์„ ์“ฐ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค. sumStr = foldl addStr 0.0 where addStr x str = x + read str ๊ทธ๋Ÿผ ๋ฐ”์ธ๋”ฉ์ด ์ •์˜์˜ ๋‚˜๋จธ์ง€์˜ ์•ž์— ์˜ค๋Š๋ƒ ๋’ค์— ์˜ค๋Š๋ƒ์˜ ์ทจํ–ฅ ์ฐจ์ด์ผ ๋ฟ์ผ๊นŒ? let๊ณผ where์—๋Š” ์ค‘์š”ํ•œ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. let ... in ๊ตฌ์กฐ๋Š” if/then/else์ฒ˜๋Ÿผ ํ‘œํ˜„์‹์ด๋‹ค. ๋ฐ˜๋Œ€๋กœ where ์ ˆ์€ ๊ฐ€๋“œ์™€ ๊ฐ™์•„์„œ ํ‘œํ˜„์‹์ด ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ let ๋ฐ”์ธ๋”ฉ์€ ๋ณต์žกํ•œ ํ‘œํ˜„์‹ ๋‚ด๋ถ€์—์„œ๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. f x = if x > 0 then (let lsq = (log x) ^ 2 in tan lsq) * sin x else 0 ๋ฐ”๊นฅ ๊ด„ํ˜ธ ์•ˆ์˜ ํ‘œํ˜„์‹์€ ๊ทธ ์ž์ฒด๋กœ ์ข…๊ฒฐ๋˜๋ฉฐ x์˜ ๋กœ๊ทธ๋ฅผ ์ œ๊ณฑํ•œ ๊ฒƒ์˜ ํƒ„์  ํŠธ๋กœ ํ‰๊ฐ€๋œ๋‹ค. lsq์˜ ์Šค์ฝ”ํ”„๋Š” ๊ด„ํ˜ธ ๋ฐ–์œผ๋กœ ํ™•์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค. then ๋ถ„๊ธฐ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ then (let lsq = (log x) ^ 2 in tan lsq) * (sin x + lsq) let ์ฃผ๋ณ€์˜ ๊ด„ํ˜ธ๋ฅผ ๋ฒ—๊ฒจ๋‚ด์ง€ ์•Š๋Š” ํ•œ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. where ์ ˆ์ด ์™„๋ฒฝํ•œ ํ‘œํ˜„์‹์ด ์•„๋‹ˆ์ง€๋งŒ case ํ‘œํ˜„์‹์— ํฌํ•จ๋  ์ˆ˜ ์žˆ๋‹ค. describeColour c = "This colour " ++ case c of Black -> "is black" White -> "is white" RGB red green blue -> " has an average of the components of " ++ show av where av = (red + green + blue) `div` 3 ++ ", yeah?" ์ด ์˜ˆ์ œ์ฒ˜๋Ÿผ where ์ ˆ์„ ๋“ค์—ฌ ์“ฐ๋ฉด RGB red green blue์ธ ๊ฒฝ์šฐ์—๋งŒ av ๋ณ€์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋„๋ก ์Šค์ฝ”ํ”„๋ฅผ ์„ค์ •ํ•œ๋‹ค. case ๋ถ„๊ธฐ๋“ค๊ณผ ๊ฐ™์€ ์ˆ˜์ค€์œผ๋กœ ๋“ค์—ฌ ์“ฐ๋ฉด av๋ฅผ ๋ชจ๋“  ๋ถ„๊ธฐ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ๊ฐ€๋“œ๋ฅผ ์ด์šฉํ•œ ์˜ˆ์‹œ๋‹ค. doStuff :: Int -> String doStuff x | x < 3 = report "less than three" | otherwise = report "normal" where report y = "the input is " ++ y ๊ฐ€๋“œ๋งˆ๋‹ค ๋“ฑํ˜ธ ๊ธฐํ˜ธ๊ฐ€ ํ•˜๋‚˜์”ฉ ์žˆ์–ด์„œ, ๋ชจ๋“  ๊ฐ€๋“œ์˜ ์Šค์ฝ”ํ”„์— let ํ‘œํ˜„์‹์ด ๋“ค์–ด๊ฐ€๋„๋ก ํ•  ๋ฐฉ๋ฒ•์ด where ์ ˆ๊ณผ ๋‹ฌ๋ฆฌ ์—†๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” where๊ฐ€ ํŽธ๋ฆฌํ•˜๋‹ค. ์ต๋ช… ํ•จ์ˆ˜ - ๋žŒ๋‹ค(lambda) addStr ๊ฐ™์ด ๋‹ค๋ฅธ ํ•จ์ˆ˜ ์ •์˜ ์•ˆ์—์„œ๋งŒ ์กด์žฌํ•˜๊ณ  ๋‹ค์‹  ์“ฐ์ง€ ์•Š์„ ํ•จ์ˆ˜์— ๊ตณ์ด ์ด๋ฆ„์„ ๋ถ™์—ฌ์•ผ ํ• ๊นŒ? ๋Œ€์‹  ๋žŒ๋‹ค ํ•จ์ˆ˜๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” ์ต๋ช…์˜ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด sumStr์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. sumStr = foldl (\ x str -> x + read str) 0.0 ๊ด„ํ˜ธ ์•ˆ์˜ ํ‘œํ˜„์‹์ด ๋žŒ๋‹ค ํ•จ์ˆ˜๋‹ค. ๋ฐฑ์Šฌ๋ž˜์‹œ()๋Š” ๊ทธ๋ฆฌ์Šค ๋ฌธ์ž ๋žŒ๋‹ค(ฮป)์™€ ๊ฐ€์žฅ ๋น„์Šทํ•œ ASCII ๋ฌธ์ž๋กœ์„œ ์“ฐ์ธ ๊ฒƒ์ด๋‹ค. ์ด ๋žŒ๋‹ค ํ•จ์ˆ˜๋Š” ๋‘ ์ธ์ˆ˜ x์™€ str์„ ์ทจํ•ด "x + read str"๋กœ ํ‰๊ฐ€ํ•œ๋‹ค. ์ด sumStr์€ let ๋ฐ”์ธ๋”ฉ์—์„œ addStr์„ ์“ด ๊ธฐ์กด์˜ sumStr๊ณผ ์ •ํ™•ํžˆ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. ๋žŒ๋‹ค๋Š” map, fold ๋ถ€๋ฅ˜์˜ ํ•จ์ˆ˜์—์„œ ํ•œ ๋ฒˆ ์“ฐ๊ณ  ๋ง ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ๋•Œ, ํŠนํžˆ ๊ทธ ํ•จ์ˆ˜๊ฐ€ ๊ฐ„๋‹จํ•  ๋•Œ ํŽธ๋ฆฌํ•˜๋‹ค. (๋ณต์žกํ•œ ํ‘œํ˜„์‹์„ ๋žŒ๋‹ค๋กœ ์“ฐ๋ฉด ๊ฐ€๋…์„ฑ์„ ํ•ด์น  ์ˆ˜ ์žˆ๋‹ค) ๋žŒ๋‹ค ํ‘œํ˜„์‹ ์•ˆ์—์„œ๋Š” ์ •์‹ ํ•จ์ˆ˜ ์ •์˜์ฒ˜๋Ÿผ ์ธ์ž์— ๋ณ€์ˆ˜๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๊ธฐ์„œ๋„ ํŒจํ„ด ๋งค์นญ์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๋ช…ํ™•ํ•œ ์˜ˆ๊ฐ€ tail์„ ๋žŒ๋‹ค๋กœ ์žฌ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. tail' = (\ (_:xs) -> xs) ์ฃผ์˜: ํ•˜์Šค์ผˆ์—์„œ ๋žŒ๋‹ค๋Š” ํŠน์ˆ˜ ๋ฌธ์ž์ด๊ธฐ ๋•Œ๋ฌธ์— \๋Š” ๊ทธ ์ž์ฒด๋กœ ํ•จ์ˆ˜๋กœ ์ทจ๊ธ‰๋˜๊ณ  ๋‹ค์Œ์— ์˜ค๋Š” ๊ณต๋ฐฑ ์•„๋‹Œ ๋ฌด์Šจ ๋ฌธ์ž๋“  ๊ทธ๊ฒƒ์ด ์ฒซ ๋ฒˆ์งธ ์ธ์ž์˜ ๋ณ€์ˆ˜๊ฐ€ ๋œ๋‹ค. ๊ทธ๋ž˜๋„ ๋žŒ๋‹ค์™€ ์ธ์ˆ˜ ์‚ฌ์ด์— ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๊ตฌ๋ฌธ์ฒ˜๋Ÿผ ๊ณต๋ฐฑ์„ ๋‘๋Š” ๊ฒŒ ์ข‹์€ ํ˜•ํƒœ๋‹ค. (ํŠนํžˆ ๋žŒ๋‹ค์˜ ์ธ์ˆ˜๊ฐ€ ๋‘˜ ์ด์ƒ์ผ ๋•Œ) ์—ฐ์‚ฐ์ž ํ•˜์Šค์ผˆ์—์„œ ์ธ์ˆ˜๋ฅผ ๋‘ ๊ฐœ ์ทจํ•˜๊ณ  ์ด๋ฆ„์ด ์ „์ ์œผ๋กœ ์•ŒํŒŒ๋ฒณ์ด๋‚˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ์—ฐ์‚ฐ์ž๋กœ ๊ฐ„์ฃผ๋œ๋‹ค. ๊ฐ€์žฅ ํ”ํ•œ ์˜ˆ๊ฐ€ ๋ง์…ˆ(+)์ด๋‚˜ ๋บ„์…ˆ(-) ๊ฐ™์€ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž๋‹ค. ๋‹ค๋ฅธ ํ•จ์ˆ˜์™€ ๋‹ฌ๋ฆฌ ์—ฐ์‚ฐ์ž๋Š” ๋ณดํ†ต ์ค‘์œ„(infix)๋กœ ์“ฐ์—ฌ, ๋‘ ์ธ์ˆ˜ ์‚ฌ์ด์— ๋“ค์–ด๊ฐ„๋‹ค. ๋ชจ๋“  ์—ฐ์‚ฐ์ž๋Š” ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ๋ฉด ๋‹ค๋ฅธ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ „์น˜(prefix)๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค. -- ์ด๊ฒƒ๋“ค์€ ๋™์ผํ•˜๋‹ค: 2 + 4 (+) 2 4 ๋‹ค๋ฅธ ํ•จ์ˆ˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ƒˆ๋กœ์šด ์—ฐ์‚ฐ์ž๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฆ„์— ์•ŒํŒŒ๋ฒณ์ด๋‚˜ ์ˆซ์ž๋ฅผ ๋„ฃ์ง€๋งŒ ๋งˆ๋ผ. ๋‹ค์Œ์€ Data.List์˜ ์ฐจ์ง‘ํ•ฉ ์ •์˜๋‹ค. (\\) :: (Eq a) => [a] -> [a] -> [a] xs \\ ys = foldl (\zs y -> delete y zs) xs ys ์œ„ ์˜ˆ์ œ์—์„œ ๋ณด๋“ฏ์ด ์—ฐ์‚ฐ์ž๋Š” ์ค‘์œ„๋กœ๋„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ „์น˜๋กœ ์ž‘์„ฑํ•œ ๊ฐ™์€ ์ •์˜๋„ ์ž‘๋™ํ•œ๋‹ค. (\\) xs ys = foldl (\zs y -> delete y zs) xs ys ์—ฐ์‚ฐ์ž์˜ ํƒ€์ž… ์„ ์–ธ์€ ์ค‘์œ„ ๋ฒ„์ „์ด ์—†์œผ๋ฉฐ ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์•ผ ํ•œ๋‹ค. ์„น์…˜ section ์„น์…˜์€ ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ›Œ๋ฅญํ•œ ํŽธ์˜ ๊ตฌ๋ฌธ์ด๋‹ค. ๊ด„ํ˜ธ ์•ˆ์— ์ธ์ˆ˜ ํ•˜๋‚˜์™€ ํ•จ๊ป˜ ๋“ค์–ด์žˆ๋Š” ์—ฐ์‚ฐ์ž๋Š”... (2+) 4 (+4) 2 ...๊ทธ ์ž์ฒด๋กœ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด (2+)์˜ ํƒ€์ž…์€ (Num a) => a -> a์ด๋‹ค. ์„น์…˜์„ map (+2) [1.. 4] == [3.. 6] ์ด๋ ‡๊ฒŒ ๋‹ค๋ฅธ ํ•จ์ˆ˜์— ๋„˜๊ธธ ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹ค๋ฅธ ์˜ˆ๋กœ๋Š” ๋ฆฌ์ŠคํŠธ ๋ณด์ถฉ ์„ค๋ช…์—์„œ ์ž‘์„ฑํ•œ multiplyList ํ•จ์ˆ˜์— ์žฅ์‹์„ ๋‹ฌ ์ˆ˜ ์žˆ๋‹ค. multiplyList :: Integer -> [Integer] -> [Integer] multiplyList m = map (m*) "์ผ๋ฐ˜์ ์ธ" ์ „์น˜ ํ•จ์ˆ˜๋ฅผ ์—ฐ์‚ฐ์ž๋กœ์„œ ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด ์—ญ ๋”ฐ์˜ดํ‘œ(backtick)์œผ๋กœ ๊ฐ์‹ธ๋ฉด ๋œ๋‹ค. 1 `elem` [1.. 4] ์ด๋Ÿฐ ๊ฒƒ์„ ๋ณด๊ณ  ํ•จ์ˆ˜๋ฅผ ์ค‘์œ„๋กœ ๋งŒ๋“ ๋‹ค๊ณ  ํ•˜๋ฉฐ ๊ฐ€๋…์„ฑ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ํ•˜๊ณค ํ•œ๋‹ค. 1 'elem' [1.. 4]๋Š” elem 1 [1.. 4] ๋ณด๋‹ค ์ฝ๊ธฐ ์‰ฝ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์ค‘์œ„๋กœ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. elem :: (Eq a) => a -> [a] -> Bool x `elem` xs = any (==x) xs ์ด๋ฒˆ์—๋„ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋Š” ์ „์น˜<NAME>์ž„์— ์ฃผ์˜ํ•˜๋ผ. ์„น์…˜๋„ ์ค‘์œ„ ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์“ธ ์ˆ˜ ์žˆ๋‹ค. (1 `elem`) [1.. 4] (`elem` [1.. 4]) 1 ๋ฌผ๋ก  ์ดํ•ญ ํ•จ์ˆ˜ ์ฆ‰ ์ธ์ˆ˜๊ฐ€ ๋‘ ๊ฐœ์ธ ๊ฒƒ๋งŒ ์ค‘์œ„๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋žŒ๋‹ค๋Š” ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ณ„๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์„ ํ”ผํ•˜๋Š” ํ›Œ๋ฅญํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ๋‹ค์Œ์˜ let ๋˜๋Š” where ๋ฐ”์ธ๋”ฉ์„ ๋žŒ๋‹ค๋กœ ๋ฐ”๊ฟ”๋ณด๋ผ. map f xs where f x = x * 2 + 3 let f x y = read x + y in foldr f 1 xs ์„น์…˜์€ ๋‹จ์ง€ ๋žŒ๋‹ค ์—ฐ์‚ฐ์˜ ํŽธ์˜ ๊ตฌ๋ฌธ์ด๋‹ค. ์ฆ‰ (+2)๋Š” \x -> x + 2 ์™€ ๋™๋“ฑํ•˜๋‹ค. ๋‹ค์Œ ์„น์…˜์˜ ๋žŒ๋‹ค ๋Œ€์‘์€ ๋ฌด์—‡์ธ๊ฐ€? ๊ทธ ํƒ€์ž…์€ ์–ด๋–ป๊ฒŒ ๋˜๋Š”๊ฐ€? (4+) (1 elem) (notElem "abc") 8 ๊ณ ์ฐจ ํ•จ์ˆ˜ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Higher-order_functions ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ Ord ํด๋ž˜์Šค ๋น„๊ต ๋ฐฉ๋ฒ• ์„ ํƒํ•˜๊ธฐ ๊ณ ์ฐจ ํ•จ์ˆ˜์™€ ํƒ€์ž… ํ•จ์ˆ˜ ์กฐ์ž‘ ์ธ์ˆ˜ ๋’ค์ง‘๊ธฐ ํ•ฉ์„ฑ composition ์‘์šฉ uncurry์™€ curry id์™€ const ์—ฐ์Šต๋ฌธ์ œ ๋…ธํŠธ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํ•ต์‹ฌ์—๋Š” ํ•จ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ๊ฐ’๋“ค๊ณผ ๋‹ค๋ฅผ ๋ฐ” ์—†๋‹ค๋Š” ๋ฐœ์ƒ์ด ์ž๋ฆฌ ์žก๊ณ  ์žˆ๋‹ค. ํ•จ์ˆ˜ํ˜•์˜ ๊ฐ•๋ ฅํ•จ์€ ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ์ •๊ทœ ๊ฐ’์ฒ˜๋Ÿผ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์— ์žˆ๋‹ค. ์ฆ‰ ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ณ  ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜๋ฐ›๋Š”๋‹ค. ํ•จ์ˆ˜๋Š” ํ•˜์Šค์ผˆ์˜ ๋„์ฒ˜์— ์กด์žฌํ•œ๋‹ค. ๊ทธ๋Ÿฐ ์˜ˆ๋กœ๋Š” ์ด๋ฏธ map๊ณผ fold ๊ฐ™์€ ๊ฒƒ์„ ๋ดค๋‹ค. ๋ฆฌ์ŠคํŠธ ๋ณด์ถฉ ์„ค๋ช…์—์„œ map์„ ๋…ผ์˜ํ•  ๋•Œ ๊ณ ์ฐจ ํ•จ์ˆ˜์˜ ํ”ํ•œ ์˜ˆ๋ฅผ ๋ดค๋‹ค. ์ด์ œ ํ•จ์ˆ˜๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ์ฝ”๋“œ์˜ ์ผ๋ฐ˜์ ์ธ ์ž‘์„ฑ๋ฒ•์„ ํƒํ—˜ํ•˜๋ ค ํ•œ๋‹ค. ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌ์ฒด์ ์ธ ์˜ˆ์‹œ๋กœ ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ ์ž‘์—…์„ ์‚ดํŽด๋ณด๋ ค ํ•œ๋‹ค. ํ€ต ์ •๋ ฌ์€ ์œ ๋ช…ํ•œ ์žฌ๊ท€ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ํ€ต ์ •๋ ฌ์˜ ์ •๋ ฌ ์ „๋žต์„ ๋ฆฌ์ŠคํŠธ์— ์ ์šฉํ•˜๋ ค๋ฉด ๋จผ์ € ํ•œ ์›์†Œ๋ฅผ ์„ ํƒํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ์˜ ๋‚˜๋จธ์ง€๋ฅผ (A) ์„ ํƒ๋œ ์›์†Œ์˜ ์•ž์— ์˜ฌ ์›์†Œ๋“ค, (B) ์„ ํƒ๋œ ์›์†Œ์™€ ๊ฐ™์€ ์›์†Œ๋“ค, (C) ๋’ค์— ์˜ฌ ์›์†Œ๋“ค๋กœ ๋ถ„๋ฆฌํ•œ๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ (A)์™€ (C)์— ๊ฐ™์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•œ๋‹ค. ์ถฉ๋ถ„ํžˆ ์žฌ๊ท€ ์ •๋ ฌ์„ ํ•˜๊ณ  ๋‚˜๋ฉด ๋ชจ๋“  ๊ฒƒ์„ ๋‹ค์‹œ ์—ฐ๊ฒฐํ•ด ์ตœ์ข…์ ์œผ๋กœ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ป๋Š”๋‹ค. ์ด ์ „๋žต์„ ํ•˜์Šค ์ผˆ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๋‹ค. -- Type signature: any list with elements in the Ord class can be sorted. quickSort :: (Ord a) => [a] -> [a] -- Base case: -- If the list is empty, there is nothing to do. quickSort [] = [] -- The recursive case: -- We pick the first element as our "pivot", the rest is to be sorted. -- Note how the pivot itself ends up included the middle part. quickSort (x : xs) = (quickSort less) ++ (x : equal) ++ (quickSort more) where less = filter (< x) xs equal = filter (== x) xs more = filter (> x) xs ์šฐ๋ฆฌ์˜ quickSort๋Š” ๋‹ค์†Œ ์–ด์„คํ”„๋‹ค. ๋” ํšจ์œจ์ ์ธ ๊ตฌํ˜„์—์„œ๋Š” ๊ฐ๊ฐ์˜ ์žฌ๊ท€ ๋‹จ๊ณ„์—์„œ filter๋ฅผ ํ†ตํ•œ ์„ธ ๊ฐœ์˜ ํŒจ์Šค๋ฅผ ์“ฐ์ง€ ์•Š์œผ๋ฉฐ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด (++)๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ์šฐ๋ฆฌ์˜ ๊ตฌํ˜„๊ณผ ๋‹ฌ๋ฆฌ ์›๋ž˜ ํ€ต ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์œ ๋™์„ฑ mutability์„ ์ด์šฉํ•ด ์ œ์ž๋ฆฌ์—์„œ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. 1 ์ง€๊ธˆ์€ ์ •ํ™•ํ•œ ๊ตฌํ˜„๋ณด๋‹ค ์ •๋ ฌ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ ํŒจํ„ด์— ๊ด€์‹ฌ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ๊ฒƒ์„ ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š๊ฒ ๋‹ค. Ord ํด๋ž˜์Šค ํ•˜์Šค์ผˆ์˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ Ord ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋‹ค. Eq๊ฐ€ ์ผ์น˜ ๊ฒ€์‚ฌ๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋“ฏ์ด Ord๋Š” ์ˆœ์„œ ๊ฒ€์‚ฌ๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. Ord ํด๋ž˜์Šค๋Š” ์–ด๋–ค ์ˆœ์„œ๊ฐ€ ํ•ด๋‹น ํƒ€์ž…์— ์ž์—ฐ์Šค๋Ÿฌ์šด์ง€๋ฅผ ์ •์˜ํ•œ๋‹ค. Ord๋Š” ๋‹ค์Œ ํƒ€์ž…์„ ๊ฐ€์ง€๋Š” compare ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. compare :: (Ord a) => a -> a -> Ordering compare๋Š” ๋‘ ๊ฐ’์„ ์ทจํ•ด ๋‘˜์„ ๋น„๊ตํ•˜๊ณ  Ordering ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ ๊ฐ’์€ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด ๋‘ ๋ฒˆ์งธ ๊ฐ’๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ LT, ๊ฐ™์„ ๊ฒฝ์šฐ EQ, ํด ๊ฒฝ์šฐ GT์ด๋‹ค. Ord ํƒ€์ž…์—์„œ (<), Eq์˜ (==), (>)๋Š” compare์˜ ๋‹จ์ถ•ํ‚ค๋กœ์„œ, ์„ธ ๊ฐ€๋Šฅ์„ฑ์„ ๊ฒ€์‚ฌํ•˜๊ณ  ๊ธฐ์ž…ํ•œ ์ˆœ์„œ๊ฐ€ ๊ทธ ํƒ€์ž…์˜ Ord ๋ช…์„ธ์— ๋”ฐ๋ผ ์ฐธ์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Bool ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. quickSort์˜ ์ •์˜์—์„œ filter์— ์‚ฌ์šฉํ•œ ๊ฐ๊ฐ์˜ ๊ฒ€์‚ฌ๋Š” compare์˜ ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๋“ค ํ•˜๋‚˜ํ•˜๋‚˜์— ๋Œ€์‘ํ•˜๊ณ , ๋”ฐ๋ผ์„œ less๋ฅผ less = filter (\y -> y compare x == LT) xs์ฒ˜๋Ÿผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋น„๊ต ๋ฐฉ๋ฒ• ์„ ํƒํ•˜๊ธฐ quickSort๋ฅผ ๊ฐ€์ง€๊ณ  ์›์†Œ๊ฐ€ Ord ํด๋ž˜์Šค์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜๊ธฐ๋ž€ ์‰ฌ์šด ์ผ์ด๋‹ค. String์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜๋ ค๋ฉด quickSort๋ฅผ ๊ทธ ๋ฆฌ์ŠคํŠธ์— ์ ์šฉํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. ์ด์ œ ๋‹จ์–ด๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ๋Š” ๊ฐ€์ƒ์˜ ์‚ฌ์ „์„ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค. (์ˆ˜์ฒœ ๋‹จ์–ด๋ฅผ ๋‹ด์€ ์‚ฌ์ „์—๋„ ์ž˜ ์ž‘๋™ํ•  ๊ฒƒ์ด๋‹ค) dictionary = ["I", "have", "a", "thing", "for", "Linux"] quickSort dictionary๋Š” ๋‹ค์Œ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ["I", "Linux", "a", "for", "have", "thing"] ์ •๋ ฌ์—์„œ ๋Œ€์†Œ๋ฌธ์ž๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ๊ณ ๋ ค๋˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์Šค ์ผˆ String์€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์˜ ๋ฆฌ์ŠคํŠธ๋‹ค. ์œ ๋‹ˆ์ฝ”๋“œ๋Š”(๊ทธ๋ฆฌ๊ณ  ๋Œ€๋ถ€๋ถ„์˜ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ์€) ๋Œ€๋ฌธ์ž์˜ ๋ฌธ์ž ์ฝ”๋“œ๋ฅผ ์†Œ๋ฌธ์ž์˜ ๋ฌธ์ž ์ฝ”๋“œ๋ณด๋‹ค ๋‚ฎ๊ฒŒ ๋ช…์‹œํ•œ๋‹ค. ๋”ฐ๋ผ์„œ "z"๋Š” "a"๋ณด๋‹ค ์ž‘๋‹ค. ์ง„์งœ ์‚ฌ์ „์‹ ์ •๋ ฌ์„ ํ•˜๋ ค๋ฉด ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ณ ๋ คํ•˜๋Š” quickSort๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์œ„์˜ compare ๋…ผ์˜์—์„œ ํžŒํŠธ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. quickSort์˜ ์žฌ๊ท€ ๋ถ„๊ธฐ ๋ถ€๋ถ„์„ ์ด๋ ‡๊ฒŒ ์žฌ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. quickSort compare (x : xs) = (quickSort compare less) ++ (x : equal) ++ (quickSort compare more) where less = filter (\y -> y `compare` x == LT) xs equal = filter (\y -> y `compare` x == EQ) xs more = filter (\y -> y `compare` x == GT) xs ์ฒ˜์Œ์˜ quickSort๋ณด๋‹ค ๋œ ๊น”๋”ํ•˜์ง€๋งŒ ์›์†Œ์˜ ์ˆœ์„œ ๋งค๊น€์ด ์ „์ ์œผ๋กœ compare ํ•จ์ˆ˜์— ๋‹ฌ๋ ค์žˆ์Œ์„ ๋ช…ํ™•ํžˆ ๋ณด์—ฌ์ค€๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์šฐ๋ฆฌ๋Š” compare๋ฅผ ์ž…๋ง›์— ๋งž๋Š” (Ord a) => a -> a -> Ordering ํ•จ์ˆ˜๋กœ ๊ต์ฒด๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. ์ƒˆ๋กœ์šด quickSort'๋Š” ๋น„๊ต ํ•จ์ˆ˜ ๊ทธ๋ฆฌ๊ณ  ์ •๋ ฌํ•  ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•˜๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜๋‹ค. quickSort' :: (Ord a) => (a -> a -> Ordering) -> [a] -> [a] -- No matter how we compare two things the base case doesn't change, -- so we use the _ "wildcard" to ignore the comparison function. quickSort' _ [] = [] -- c is our comparison function quickSort' c (x : xs) = (quickSort' c less) ++ (x : equal) ++ (quickSort' c more) where less = filter (\y -> y `c` x == LT) xs equal = filter (\y -> y `c` x == EQ) xs more = filter (\y -> y `c` x == GT) xs ์ด quickSort' ํ•จ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์šฉ๋„๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ ค๋ฉด reverse (quickSort dictionary)๋กœ ๊ธฐ์กด์˜ ์ •๋ ฌ๋œ ๋ชฉ๋ก์„ ๋’ค์ง‘์–ด๋„ ๋˜์ง€๋งŒ, ์• ์ดˆ์— ์ •๋ ฌ์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ํ•˜๋ ค๋ฉด, ํ†ต์ƒ์˜ Ordering์„ ๋’ค์ง‘์–ด ๋ฐ˜ํ™˜ํ•˜๋Š” ๋น„๊ต ํ•จ์ˆ˜๋ฅผ quickSort'์— ๊ฑด๋„ค๋ฉด ๋œ๋‹ค. -- the usual ordering uses the compare function from the Ord class usual = compare -- the descending ordering, note we flip the order of the arguments to compare descending x y = compare y x -- the case-insensitive version is left as an exercise! insensitive = ... -- How can we do case-insensitive comparisons without making a big list of all possible cases? ์ž ๊น Data.List๋Š” ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ์„ ์œ„ํ•œ sort ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ํ€ต ์ •๋ ฌ์„ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹  ๋ณ‘ํ•ฉ ์ •๋ ฌ์ด๋ผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํšจ์œจ์ ์ธ ๊ตฌํ˜„์„ ์‚ฌ์šฉํ•œ๋‹ค. Data.List์—๋Š” ์šฐ๋ฆฌ์˜ quickSort'์ฒ˜๋Ÿผ ๋งž์ถคํ˜• ๋น„๊ต ํ•จ์ˆ˜๋ฅผ ์ทจํ•˜๋Š” sortBy ์—ญ์‹œ ๋“ค์–ด์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ quickSort' insensitive dictionary๊ฐ€ ["a", "for", "have", "I", "Linux", "thing"]๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก insensitive ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ๊ณ ์ฐจ ํ•จ์ˆ˜์™€ ํƒ€์ž… ์ตœ์ข… ๊ฒฐ๊ณผ ๊ฐ€๋Š” ๋„์ค‘ ๊ฑฐ์ณ๊ฐ€๋Š” ์ค‘๊ฐ„ ํ•จ์ˆ˜๋“ค์„ ์ƒ์„ฑํ•˜๋Š”, ์ปค๋งcurrying์ด๋ผ๋Š” ๊ฐœ๋…์€ ์•ž์„œ ๋ฆฌ์ŠคํŠธ ๋ณด์ถฉ ์„ค๋ช… ์žฅ์—์„œ ์ฒ˜์Œ ์†Œ๊ฐœํ–ˆ๋‹ค. ์ง€๊ธˆ์ด ์ปค๋ง์˜ ์ž‘๋™ ๋ฐฉ์‹์„ ๋˜์งš์„ ์ข‹์€ ์‹œ๊ธฐ๋‹ค. quickSort'์˜ ํƒ€์ž…์€ (a -> a -> Ordering) -> [a] -> [a]์ด๋‹ค. ๋Œ€์ฒด๋กœ ๊ณ ์ฐจ ํ•จ์ˆ˜์˜ ํƒ€์ž…์€ ๊ทธ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•œ ์ง€์นจ์„ ์ฃผ๊ณค ํ•œ๋‹ค. ์œ„์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ํ•ด์„ํ•˜๋Š” ์ง๊ด€์ ์ธ ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. "quickSort'๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋‘ a์˜ ์ˆœ์„œ๋ฅผ ๋งค๊ธฐ๋Š” ํ•จ์ˆ˜๋ฅผ ์ทจํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” a๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ a๋“ค์˜ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค." ์ด ์ •๋„๋ฉด ์ˆœ์„œ ๋งค๊น€ ํ•จ์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ์— ์“ด๋‹ค๋Š” ๊ฑธ ์ถ”์ธกํ•˜๋Š” ๋ฐ ์ถฉ๋ถ„ํ•˜๋‹ค. a -> a -> Ordering ์„ ๊ฐ์‹ธ๋Š” ๊ด„ํ˜ธ๊ฐ€ ํ•„์ˆ˜๋ผ๋Š” ๊ฒƒ์— ์œ ์˜ํ•˜๋ผ. ์ด๋Š” a -> a -> Ordering ์ด ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋กœ์จ ๋‹จ๋… ์ธ์ž๋ฅผ ํ˜•์„ฑํ•จ์„ ๋ช…์‹œํ•œ๋‹ค. ๊ด„ํ˜ธ๊ฐ€ ์—†์œผ๋ฉด a -> a -> Ordering -> [a] -> [a] ๊ฐ€ ๋˜์–ด ์ธ์ž๊ฐ€ ๋‘ ๊ฐœ๊ฐ€ ์•„๋‹Œ ๋„ค ๊ฐœ๊ฐ€ ๋˜๋ฉฐ(์ด ์ค‘ ์–ด๋Š ๊ฒƒ๋„ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค) ์›ํ•˜๋Š” ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. -> ์—ฐ์‚ฐ์ž๋Š” ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฒฐํ•ฉ(right-associative) ๋˜๊ธฐ ๋•Œ๋ฌธ์— quickSort'์˜ ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ฌถ์œผ๋ฉด ์‚ฌ์‹ค a -> (a -> (Ordering -> ([a] -> [a]))) ์ด ๋œ๋‹ค. ์™„๋ฒฝํžˆ ๋ง์ด ๋œ๋‹ค. ์กฐ์ • ๊ฐ€๋Šฅํ•œ ๋น„๊ต ํ•จ์ˆ˜ ์ธ์ž๊ฐ€ ์—†๋˜ ์›๋ž˜์˜ quickSort์˜ ํƒ€์ž…์€ [a] -> [a]์˜€๋‹ค. quickSort๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•˜์—ฌ ์ •๋ ฌํ•œ๋‹ค. quickSort'๋Š” quickSort ๊ผด์˜ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋‹ค! (a -> a -> Ordering) ๋ถ€๋ถ„์— compare๋ฅผ ๊ฝƒ์•„ ๋„ฃ์œผ๋ฉด ์›๋ž˜ quickSort ํ•จ์ˆ˜๋ฅผ ๋Œ๋ ค๋ฐ›๋Š”๋‹ค. ์ธ์ž๋กœ ๋‹ค๋ฅธ ๋น„๊ต ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด quickSort ํ•จ์ˆ˜์˜ ๋ณ€ํ˜•์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ฌผ๋ก  ๋น„๊ต ํ•จ์ˆ˜๋ฟ ์•„๋‹ˆ๋ผ ์‹ค์ œ๋กœ ์ •๋ ฌํ•  ๋ฆฌ์ŠคํŠธ๋„ ์ธ์ž๋กœ ๋„˜๊ธฐ๋ฉด ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” quickSort ๊ผด์˜ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋Œ€์‹  ๊ณ„์†ํ•˜์—ฌ ๊ทธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒˆ ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๊ณ  ์ตœ์ข… ๊ฒฐ๊ณผ๋กœ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ ค๋ฐ›๋Š”๋‹ค. ์—ฐ์Šต๋ฌธ์ œ (๋„์ „) ๋‹ค์Œ ์—ฐ์Šต๋ฌธ์ œ๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜, ์žฌ๊ท€, ์ž…์ถœ๋ ฅ์„ ํ•˜๋‚˜๋กœ ๋ฌถ์€ ๊ฒƒ์ด๋‹ค. ๋ช…๋ นํ˜• ์–ธ์–ด์—์„œ for ๋ฃจํ”„๋ผ๊ณ  ์•Œ๋ ค์ง„ ๊ฒƒ์„ ์šฐ๋ฆฌ ์†์œผ๋กœ ๋งŒ๋“ค๊ณ ์ž ํ•œ๋‹ค. ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. for :: a -> (a -> Bool) -> (a -> a) -> (a -> IO ()) -> IO () for i p f job = -- ??? ์ด ํ•จ์ˆ˜์˜ ์šฉ๋ฒ•์€ ์ด๋Ÿฐ ์‹์ด๋‹ค. for 1 (<10) (+1) print 1์—์„œ 9๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•œ๋‹ค. for์— ์š”๊ตฌ๋˜๋Š” ๋™์ž‘์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. for๋Š” ์ดˆ๊นƒ๊ฐ’ i์—์„œ ์‹œ์ž‘ํ•ด job i๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  f๋ฅผ ์ด์šฉํ•ด ์ด ๊ฐ’์„ ์ˆ˜์ •ํ•˜๊ณ  ์ˆ˜์ •๋œ f ๊ฐ’์ด ์กฐ๊ฑด p๋ฅผ ๋งŒ์กฑํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. ๋งŒ์กฑํ•˜์ง€ ์•Š์œผ๋ฉด ์ค‘๋‹จํ•œ๋‹ค. ์•„๋‹ˆ๋ฉด for ๋ฃจํ”„๋Š” i์˜ ์ž๋ฆฌ์— ์ˆ˜์ •๋œ f i๋ฅผ ๋„ฃ๊ณ  for ๋ฃจํ”„๋Š” ๊ณ„์†๋œ๋‹ค. 1. ํ•˜์Šค ์ผˆ๋กœ for ๋ฃจํ”„๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. 2. ์œ„ ๋‹จ๋ฝ์—์„œ๋Š” for ๋ฃจํ”„๋ฅผ ๋ช…๋ นํ˜• ์‹์œผ๋กœ ์„ค๋ช…ํ–ˆ๋‹ค. ํ•จ์ˆ˜ํ˜•์˜ ๊ด€์ ์—์„œ ์—ฌ๋Ÿฌ๋ถ„์˜ ๊ตฌํ˜„์„ ์„œ์ˆ ํ•˜๋ผ. ๊ทธ ์™ธ์—๋„ ์‹œ๋„ํ•ด ๋ณผ ๋งŒํ•œ ๋„์ „ ๋ฌธ์ œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค. 1. "1์—์„œ 10๊นŒ์ง€์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ" ๊ฐ™์€ ์ž‘์—…์„ ์ƒ๊ฐํ•ด ๋ณด๋ผ. print๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ๊ณ  ์ˆซ์ž์˜ ๋ฆฌ์ŠคํŠธ์— print๋ฅผ ์ ์šฉํ•˜๋ ค ํ•  ๋•Œ, map์„ ์“ฐ๋Š” ๊ฒŒ ์ž์—ฐ์Šค๋Ÿฌ์šธ ๊ฒƒ ๊ฐ™์€๋ฐ ์ •๋ง ๊ทธ๋Ÿด๊นŒ? 2. ํ•จ์ˆ˜ sequenceIO :: [IO a] -> IO [a]๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. ์•ก์…˜์˜ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ด ํ•จ์ˆ˜๋Š” ๊ฐ ์•ก์…˜์„ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰ํ•˜๊ณ  ๊ทธ๊ฒƒ๋“ค์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋Œ๋ ค๋ฐ›๋Š”๋‹ค. 3. ํ•จ์ˆ˜ mapIO :: (a -> IO b) -> [a] -> IO [b]๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. ์ด ํ•จ์ˆ˜๋Š” a -> IO b ํƒ€์ž…์˜ ํ•จ์ˆ˜์™€ [a] ํƒ€์ž…์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ๊ทธ ์•ก์…˜์„ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์‹คํ–‰ํ•œ ๋‹ค์Œ, ๊ฒฐ๊ณผ๋“ค์„ ๋Œ๋ ค๋ฐ›๋Š”๋‹ค. ํ•จ์ˆ˜ ์กฐ์ž‘ ์œ ์šฉํ•˜๊ฒŒ ๋„๋ฆฌ ์“ฐ์ด๋Š” ๋ฒ”์šฉ ๊ณ ์ฐจ ํ•จ์ˆ˜๋“ค์„ ๋ช‡ ๊ฐœ ๋…ผ์˜ํ•˜๋ฉฐ ์ด๋ฒˆ ์žฅ์„ ๋งˆ๋ฌด๋ฆฌ ์ง“๊ฒ ๋‹ค. ์ด๊ฒƒ๋“ค์— ์ต์ˆ™ํ•ด์ง€๋ฉด ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์ฝ๋Š” ๊ธฐ์ˆ ์ด ๋ˆˆ์— ๋„๊ฒŒ ํ–ฅ์ƒ๋  ๊ฒƒ์ด๋‹ค. ์ธ์ˆ˜ ๋’ค์ง‘๊ธฐ filp์€ ํŽธ๋ฆฌํ•˜๊ณ  ์ž‘์€ Prelude ํ•จ์ˆ˜๋‹ค. ์ธ์ž๊ฐ€ ๋‘ ๊ฐœ์ธ ํ•จ์ˆ˜๋ฅผ ์ทจํ•ด ๊ฐ™์€ ํ•จ์ˆ˜์ธ๋ฐ ๋‘ ์ธ์ž๊ฐ€ ๋’ค๋ฐ”๋€ ๋ณ€ํ˜•์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. flip :: (a -> b -> c) -> b -> a -> c flip ์‚ฌ์šฉ๋ฒ•: Prelude> (flip (/)) 3 1 0.3333333333333333 Prelude> (flip map) [1,2,3] (*2) [2,4,6] quickSort ์˜ˆ์ œ์˜ ๋น„๊ต ํ•จ์ˆ˜ descending์˜ ์ธ์ž ์ƒ๋žต ๋ฒ„์ „์„ flip์„ ์ด์šฉํ•ด ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. descending = flip compare flip์ด ํŠนํžˆ ์œ ์šฉํ•œ ๋•Œ๋Š” ์„œ๋กœ ํƒ€์ž…์ด ๋‹ค๋ฅธ ์ธ์ž๊ฐ€ 2๊ฐœ์ธ ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๋Š”๋ฐ ๊ทธ ๊ณ ์ฐจ ํ•จ์ˆ˜์˜ ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ๊ณ ๋ คํ•  ๋•Œ ๋‘ ์ธ์ž์˜ ์ˆœ์„œ๊ฐ€ ์ž˜๋ชป๋œ ๊ฒฝ์šฐ๋‹ค. ํ•ฉ์„ฑ composition ํ•ฉ์„ฑ ์—ฐ์‚ฐ์ž (.)๋Š” ๋˜ ๋‹ค๋ฅธ ๊ณ ์ฐจ ํ•จ์ˆ˜๋‹ค. (.)์˜ ์‹œ๊ทธ๋„ˆ์ณ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (.) :: (b -> c) -> (a -> b) -> a -> c (.)๋Š” ๋‘ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ๋‘ ๋ฒˆ์งธ ์ธ์ž์™€ ์ฒซ ๋ฒˆ์งธ ์ธ์ž ์ˆœ์œผ๋กœ ์ ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ•ฉ์„ฑ๊ณผ ๊ณ ์ฐจ ํ•จ์ˆ˜๋กœ ๊ฐ•๋ ฅํ•œ ๊ธฐ๊ต๋ฅผ ๋ถ€๋ฆด ์ˆ˜ ์žˆ๋‹ค. ์กฐ๊ทธ๋งŒ ์˜ˆ๋กœ Data.List ๋ชจ๋“ˆ์— ์ •์˜๋œ inits ํ•จ์ˆ˜๋ฅผ ๋ณด์ž. ๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด "์ธ์ž์˜ ๋ชจ๋“  ์ ˆ๋ฉด initial segment์„ ์งง์€ ๊ฒƒ์ด ์•ž์— ์˜ค๋Š” ์ˆœ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค"๋ผ๊ณ  ์“ฐ์—ฌ์žˆ๋‹ค. Prelude Data.List> inits [1,2,3] [[],[1],[1,2],[1,2,3]] inits๋Š” Prelude์˜ ๊ณ ์ฐจ ํ•จ์ˆ˜ flip, scanl, (.), map์„ ์ด์šฉํ•ด ํ•œ ์ค„๋งŒ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. (๊ทน์  ํšจ๊ณผ๋ฅผ ์œ„ํ•ด ์ธ์ž ์ƒ๋žต ์‹์œผ๋กœ ์ž‘์„ฑ) myInits :: [a] -> [[a]] myInits = map reverse . scanl (flip (:)) [] ์ด๋ ‡๊ฒŒ ๊ฝ‰ ๋ˆŒ๋Ÿฌ ๋‹ด์€ ์ •์˜๋Š” ์ฒ˜์Œ์—๋Š” ๋ฒ…์ฐฐ ์ˆ˜ ์žˆ์œผ๋‹ˆ ๊ฐ ํ•จ์ˆ˜๊ฐ€ ํ•˜๋Š” ์ผ์„ ๋– ์˜ฌ๋ฆฌ๊ณ  ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ์•ˆ๋‚ด์ธ ์‚ผ์•„ ํ•œ ๊บผํ’€์”ฉ ์ฐฌ์ฐฌํžˆ ๋œฏ์–ด๋ณด์ž. myInits์˜ ์ •์˜๋Š” ๋งค์šฐ ๊ฐ„๊ฒฐํ•˜๋ฉฐ ๊ด„ํ˜ธ๋ฅผ ๊ฑฐ์˜ ์“ฐ์ง€ ์•Š์•„ ๊น”๋”ํ•˜๋‹ค. ๊ธฐ๋‚˜๊ธด (.) ์—ฐ์‡„๋กœ ํ•ฉ์„ฑ์„ ๋„ˆ๋ฌด ๋งŽ์ด ํ•˜๋‹ค ๋ณด๋ฉด ์ž์—ฐ์Šค๋ ˆ ์ฝ”๋“œ๊ฐ€ ํ˜ผ๋ž€์Šค๋Ÿฌ์›Œ์ง€๊ธฐ ๋งˆ๋ จ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ ์ ˆํžˆ ๋ฐฐ์น˜ํ•˜๋ฉด ์ด๋Ÿฌํ•œ ์ธ์ž ์ƒ๋žต<NAME>์ด ๋น›์„ ๋ฐœํ•œ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ์ด ๊ตฌํ˜„์€ ์ƒ๋‹นํžˆ "๊ณ ์ˆ˜์ค€"์ด๋‹ค. ํŒจํ„ด ๋งค์นญ์ด๋‚˜ ์žฌ๊ท€ ๊ฐ™์€ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ๋ช…์‹œ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋ฐฐ์น˜ํ•œ ํ•จ์ˆ˜๋“ค ์ฆ‰ ๊ณ ์ฐจ ํ•จ์ˆ˜๋“ค๊ณผ ์ธ์ž๋กœ์„œ ์ „๋‹ฌ๋œ ํ•จ์ˆ˜๋“ค์ด ๊ทธ๋Ÿฐ ์—ฐ๊ณ„ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์‘์šฉ ($)๋Š” ์˜๋ฌธ์˜ ๊ณ ์ฐจ ์—ฐ์‚ฐ์ž๋‹ค. ํƒ€์ž…์€ ์ด๋ ‡๋‹ค. ($) :: (a -> b) -> a -> b ํ•จ์ˆ˜๋ฅผ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›๋Š”๋ฐ ํ•˜๋Š” ์ผ์ด๋ผ๊ณค ๊ทธ ํ•จ์ˆ˜๋ฅผ ๋‘ ๋ฒˆ์งธ ์ธ์ž์— ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ, (head $ "abc") == (head "abc")์ด๋‹ค. ๋ญ ์ด๋Ÿฐ ์“ธ๋ชจ์—†๋Š” ์—ฐ์‚ฐ์ž๊ฐ€ ๋‹ค ์žˆ์„๊นŒ? ํ•˜์ง€๋งŒ ์ด ํ•จ์ˆ˜์—๋Š” ๋‘ ๊ฐ€์ง€ ํฅ๋ฏธ๋กœ์šด ์ ์ด ์žˆ๋‹ค. ๋จผ์ € ($)๋Š” ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์ •์ƒ์ ์ธ ํ•จ์ˆ˜ ์ ์šฉ๊ณผ ๋‹ฌ๋ฆฌ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์•„์ฃผ ๋‚ฎ๋‹ค. 2 ๊ฒฐ๊ณผ์ ์œผ๋กœ $๋กœ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊นจ์„œ ์ค‘์ฒฉ๋œ ๊ด„ํ˜ธ์—์„œ ์˜ค๋Š” ํ˜ผ๋ž€์„ ํ”ผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ๊ด„ํ˜ธ๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ  myInits์˜ ์ธ์ž ๋ช…์‹œ ๋ฒ„์ „์„ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. myInits :: [a] -> [[a]] myInits xs = map reverse . scanl (flip (:)) [] $ xs ํ•œ ๊ฐ€์ง€ ๋”, ($)๋Š” ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๋Š” ํ•จ์ˆ˜๊ณ  ํ•จ์ˆ˜๋Š” ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ํฅ๋ฏธ๋กœ์šด ํ‘œํ˜„์‹์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. map ($ 2) [(2*), (4*), (8*)] ๊ทธ๋ ‡๋‹ค. ํ•จ์ˆ˜์˜ ๋ฆฌ์ŠคํŠธ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์™„๋ฒฝํžˆ ํƒ€๋‹นํ•˜๋‹ค. uncurry์™€ curry ์ด๋ฆ„์ด ์•”์‹œํ•˜๋“ฏ์ด uncurry๋Š” ์ปค๋ง์„ ์ทจ์†Œํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. ์ฆ‰ ์ธ์ˆ˜๊ฐ€ ๋‘˜์ธ ํ•จ์ˆ˜๋ฅผ, ํ•˜๋‚˜์˜ ์Œ๋งŒ์„ ์ธ์ž๋กœ ๊ฐ–๋Š” ํ•จ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. uncurry :: (a -> b -> c) -> (a, b) -> c Prelude> let addPair = uncurry (+) Prelude> addPair (2, 3) uncurry์˜ ํฅ๋ฏธ๋กœ์šด ์šฉ๋ฒ•์€ ($)์™€์˜ ์กฐํ•ฉ์œผ๋กœ ์Œ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋‘ ๋ฒˆ์งธ ์›์†Œ์— ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Prelude> uncurry ($) (reverse, "stressed") "desserts" uncurry์˜ ๋ฐ˜๋Œ€์ธ curry๋„ ์žˆ๋‹ค. curry :: ((a, b) -> c) -> a -> b -> c Prelude> curry addPair 2 3 -- addPair as in the earlier example. ๋Œ€๋ถ€๋ถ„์˜ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋Š” ์ด๋ฏธ ์ปค๋ง์ด ๋˜์–ด ์žˆ์–ด์„œ uncurry์™€ ๋‹ฌ๋ฆฌ curry๋Š” ๊ฑฐ์˜ ์“ฐ์ด์ง€ ์•Š๋Š”๋‹ค. id์™€ const ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ทธ ์ž์ฒด๋กœ๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ์ง€๋งŒ ๊ณ ์ฐจ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ž์ฃผ ์“ฐ์ด๋Š” ํ•จ์ˆ˜ ๋‘ ๊ฐœ๋ฅผ ์–ธ๊ธ‰ํ•ด์•ผ๊ฒ ๋‹ค. ํ•ญ๋“ฑ ํ•จ์ˆ˜ id๋Š” ์ธ์ˆ˜๋ฅผ ๊ทธ๋Œ€๋กœ ๋Œ๋ ค์ฃผ๋Š” a -> a ํƒ€์ž…์˜ ํ•จ์ˆ˜๋‹ค. Prelude> id "Hello" "Hello" id์™€ ๋น„์Šทํ•œ ๊ฐœ๋…์œผ๋กœ const๋Š” ์ด๋ ‡๊ฒŒ ์ž‘๋™ํ•˜๋Š” a -> b -> a ํ•จ์ˆ˜๋‹ค. Prelude> const "Hello" "world" "Hello" const๋Š” ๋‘ ์ธ์ˆ˜๋ฅผ ์ทจํ•ด ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋งŒ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋Š” ๋ฒ„๋ฆฐ๋‹ค. ์ธ์ˆ˜๊ฐ€ ํ•˜๋‚˜์ธ a -> (b -> a) ํ•จ์ˆ˜๋กœ ๋ณด๋ฉด ์ด ํ•จ์ˆ˜๋Š” ์ƒ์ˆ˜ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ด ์ƒ์ˆ˜ ํ•จ์ˆ˜๋Š” ๋ฌด์Šจ ์ธ์ˆ˜๋ฅผ ๋ฐ›๋“  ๊ฐ™์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. id์™€ const๋Š” ์ฒ˜์Œ์—๋Š” ์•„๋ฌด ์“ธ๋ชจ๋„ ์—†์–ด ๋ณด์ผ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ๊ณ ์ฐจ ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋ฉด ์•„๋ฌด๊ฒƒ๋„ ์•ˆ ํ•˜๊ฑฐ๋‚˜ ํ•ญ์ƒ ๊ฐ™์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์˜์‚ฌ dummy ํ•จ์ˆ˜๋ฅผ ๋„˜๊ฒจ์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. id์™€ const๋Š” ๊ทธ๋Ÿฐ ๋•Œ์— ํŽธ๋ฆฌํ•œ ์˜์‚ฌ ํ•จ์ˆ˜๋‹ค. ์—ฐ์Šต๋ฌธ์ œ curry, uncurry, const์˜ ๊ตฌํ˜„์„ ์ž‘์„ฑํ•˜๋ผ. ๋‹ค์Œ ํ•จ์ˆ˜๋“ค์ด ์–ด๋–ค ์ผ์„ ํ•˜๋Š”์ง€ ์ง์ ‘ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ์„ค๋ช…ํ•˜๋ผ. uncurry const curry fst curry swap (์—ฌ๊ธฐ์„œ swap :: (a, b) -> (b, a)๋Š” ์Œ์˜ ๋‘ ์›์†Œ๋ฅผ ๋งž๋ฐ”๊พผ๋‹ค. swap์€ Data.Tuple์— ์žˆ๋‹ค) (๊ณ ๋‚œ๋„) foldr๋กœ foldl์„ ๊ตฌํ˜„ํ•˜๋ผ. ํžŒํŠธ: ์ผ๋‹จ ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ์˜ foldr๊ณผ foldl์„ ๋ณต์Šตํ•˜๋ผ. ๋‘ ๊ฐ€์ง€ ํ•ด๋ฒ•์ด ์žˆ๋Š”๋ฐ ํ•˜๋‚˜๋Š” ์‰ฝ์ง€๋งŒ ๋ฐ‹๋ฐ‹ํ•˜๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ๋ฌด์ฒ™ ํฅ๋ฏธ๋กญ๋‹ค. ๊ทธ ํฅ๋ฏธ๋กœ์šด ๊ฒƒ์˜ ๊ฒฝ์šฐ ๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ฉ์ • ํ• ์ง€ ์‹ ์ค‘ํžˆ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด๋‹ค. ๋…ธํŠธ "์ง„์ •ํ•œ" ์ œ์ž๋ฆฌ ํ€ต ์ •๋ ฌ์„ ํ•˜์Šค์ผˆ์—์„œ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ดˆ๊ธ‰ ์ž๋ฐ˜์—์„œ ๋…ผ์˜ํ•˜์ง€ ์•Š๋Š” ๋ณด๋‹ค ๊ณ ๊ธ‰์Šค๋Ÿฌ์šด ๋„๊ตฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. โ†ฉ ์ƒ๊ธฐํ•˜๋Š” ์˜๋ฏธ๋กœ ๋งํ•˜์ž๋ฉด, ์—ฌ๊ธฐ์„œ ์šฐ์„ ์ˆœ์œ„๋ž€ ์ˆ˜ํ•™์—์„œ *๊ฐ€ +๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๋‹ค(์ฆ‰ ๋จผ์ € ํ‰๊ฐ€๋œ๋‹ค)๋Š” ๋ง๊ณผ ๊ฐ™์€ ๋งฅ๋ฝ์ด๋‹ค. โ†ฉ 9 GHCi ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Using_GHCi_effectively ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค ํƒญ ์™„์„ฑ ": commands" GHCi์—์„œ ํ•จ์ˆ˜์˜ ์‹œ๊ฐ„ ์ธก์ • ์—ฌ๋Ÿฌ ์ค„ ์ž…๋ ฅ GHCi๋Š” ์—ฌ๋Ÿฌ ์ˆ˜๋‹จ์„ ํ†ตํ•ด ๋ณด๋‹ค ํšจ์œจ์ ์ธ ์ž‘์—…์„ ๋ณด์กฐํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” GHCi๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋„์›€ ๋˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์‹ค์ „ ์ง€์‹์„ ๋…ผ์˜ํ•œ๋‹ค. ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค ํƒญ ์™„์„ฑ ๋‹ค๋ฅธ ๋งŽ์€ ํ„ฐ๋ฏธ๋„ ํ”„๋กœ๊ทธ๋žจ์ฒ˜๋Ÿผ GHCi์—์„œ๋„ ์‹œ์ž‘ ๋ฌธ์ž๋ฅผ ๋ช‡ ๊ฐœ ์ž…๋ ฅํ•˜๊ณ  ํƒญ ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ ๊ทธ ๋ฌธ์ž๋“ค๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ํ•ญ๋ชฉ๋“ค์˜ ๋ชฉ๋ก์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์—ด์ด ํ•˜๋‚˜๋งŒ ์žˆ์„ ๋•Œ ํƒญ์„ ๋ˆ„๋ฅด๋ฉด ๊ทธ ๋ฌธ์ž์—ด์„ ์ž๋™์œผ๋กœ ์™„์„ฑํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด fol<ํƒญ>์„ ๋ˆ„๋ฅด๋ฉด "d"๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค. ("fold" ๋ง๊ณ ๋Š” "fol"๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ•ญ๋ชฉ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค) ๋‹ค์‹œ ํƒญ์„ ๋ˆ„๋ฅด๋ฉด Prelude์— ํฌํ•จ๋œ ๋„ค ํ•จ์ˆ˜(foldl, foldl1, foldr, foldr1)๊ฐ€ ๋‚˜์—ด๋œ๋‹ค. ๋‹ค๋ฅธ ๋ชจ๋“ˆ๋“ค์„ ์ž„ํฌํŠธ ํ•ด๋†“๋‹ค๋ฉด ๋” ๋งŽ์€ ํ•ญ๋ชฉ์ด ๋‚˜ํƒ€๋‚  ์ˆ˜๋„ ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ๋“ค์–ด์žˆ๋Š” ํŒŒ์ผ์„ GHCi๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋„ ํƒญ ์™„์„ฑ์ด ์ž‘๋™ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด :l fi<ํƒญ>์„ ์ž…๋ ฅํ•˜๋ฉด ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ(GHCi๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์žˆ๋˜ ์œ„์น˜)์—์„œ "fi"๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ํŒŒ์ผ์ด ๋œฌ๋‹ค. ๋ชจ๋“ˆ์„ ๋“ค์—ฌ์˜ฌ ๋•Œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. :m +Da<ํƒญ>์„ ์ž…๋ ฅํ•˜๋ฉด ์„ค์น˜๋œ ํŒจํ‚ค์ง€ ์ค‘ "Da"๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๋ชจ๋“ˆ์ด ๋‚˜ํƒ€๋‚œ๋‹ค. ": commands" GHCi ๋ช…๋ น์ค„์—์„œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์œ„ํ•œ ์ปค๋งจ๋“œ๋“ค์€ ":" (์ฝœ๋ก ) ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•œ๋‹ค. :help ๋˜๋Š” :h -- ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ปค๋งจ๋“œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค :load ๋˜๋Š” :l -- ํŒŒ์ผ์„ GHCi๋กœ ๋ถˆ๋Ÿฌ์˜จ๋‹ค(์ปค๋งจ๋“œ์— ํŒŒ์ผ ์ด๋ฆ„์„ ๋ฐ˜๋“œ์‹œ ํฌํ•จํ•  ๊ฒƒ) :reload ๋˜๋Š” :r -- ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋ถˆ๋Ÿฌ์˜จ ํŒŒ์ผ์„ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์˜จ๋‹ค(๊ทธ ํŒŒ์ผ์„ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ ์œ ์šฉ) :type ๋˜๋Š” :t -- ์ปค๋งจ๋“œ์— ํฌํ•จ๋œ ํ‘œํ˜„์‹์˜ ํƒ€์ž…์„ ์ถœ๋ ฅํ•œ๋‹ค :module ๋˜๋Š” :m -- ํ•ด๋‹น ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค(์ปค๋งจ๋“œ์— ๋ชจ๋“ˆ ์ด๋ฆ„์„ ํฌํ•จํ•  ๊ฒƒ). ๋ชจ๋“ˆ ์ด๋ฆ„ ์•ž์— -๋ฅผ ๋ถ™์—ฌ์„œ ๋ถˆ๋Ÿฌ์˜จ ๊ฑธ ์ทจ์†Œํ•  ์ˆ˜๋„ ์žˆ๋‹ค :browse -- ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ๋ชจ๋“  ํ•จ์ˆ˜์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ํ‘œ์‹œํ•œ๋‹ค ์—ฌ๊ธฐ์„œ๋„ ํƒญ ์™„์„ฑ์œผ๋กœ ์ปค๋งจ๋“œ์˜ ๋ชฉ๋ก์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ปค๋งจ๋“œ๋ฅผ ๋ณด๋ ค๋ฉด :<ํƒญ> ์„ ์ž…๋ ฅํ•˜๋ผ. GHCi์—์„œ ํ•จ์ˆ˜์˜ ์‹œ๊ฐ„ ์ธก์ • GHCi๋Š” ํ•จ์ˆ˜ ์‹คํ–‰์ด ์–ผ๋งˆ๋‚˜ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š”์ง€ ์ธก์ •ํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•˜๋ฉฐ, ์ด๊ฒƒ์€ ํ•œ ํ•จ์ˆ˜์˜ ์–ด๋–ค ๋ฒ„์ „์ด ๊ฐ€์žฅ ๋น ๋ฅธ์ง€ ์ฐพ์„ ๋•Œ ์œ ์šฉํ•˜๋‹ค. (์˜ˆ๋ฅผ ๋“ค๋ฉด ์–ด๋–ค ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€์ผ ๋•Œ) ghci ๋ช…๋ น์ค„์— :set +s๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ํ™•์ธํ•˜๋ ค๋Š” ํ•จ์ˆ˜๋“ค์„ ์‹คํ–‰ํ•œ๋‹ค. GHCi๊ฐ€ ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ ๋’ค์— ๊ทธ ํ•จ์ˆ˜์˜ ์‹คํ–‰ ์‹œ๊ฐ„์ด ํ‘œ์‹œ๋œ๋‹ค. ์—ฌ๋Ÿฌ ์ค„ ์ž…๋ ฅ ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ฑฐ๋‚˜, ghci ์•ˆ์—์„œ ๋ณ„๊ฐœ์˜ ํŒŒ์ผ์„ ์ž‘์„ฑํ•ด ๋ถˆ๋Ÿฌ์˜ค์ง€ ์•Š๊ณ  do ๋ธ”๋ก์„ ์ž…๋ ฅํ•˜๋ ค๋ฉด ์‰ฌ์šด ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์ƒˆ๋กœ์šด ์ค„์„ :{๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ์ค„ ๋ฐ”๊ฟˆ์ด ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์—”ํ„ฐ๋ฅผ ์นœ๋‹ค. ์—ฌ๋Ÿฌ ์ค„ ์ž…๋ ฅ์ด ๋๋‚ฌ์œผ๋ฉด :}๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ์˜ˆ์‹œ *Main> :{ *Main| let askname = do *Main| putStrLn "What is your name?" *Main| name <- getLine *Main| putStrLn $ "Hello " ++ name *Main| :} *Main> :set +m ์ปค๋งจ๋“œ(์—ฌ๋Ÿฌ ์ค„ ์ž…๋ ฅ ํ—ˆ์šฉ)๋ฅผ ์จ๋„ ๋œ๋‹ค. ์ด๋•Œ๋Š” ๋นˆ ์ค„์—์„œ ๋ธ”๋ก์ด ๋๋‚œ๋‹ค. ์ถ”๊ฐ€๋กœ, ghci ๋ช…๋ น์ค„์—์„œ ์ค„ ๋ฐ”๊ฟˆ์€ ;๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค. *Main> let askname1 = do ; putStrLn "what is your name?" ; name <- getLine ; putStrLn $ "Hello " ++ name 3 ํ•˜์Šค ์ผˆ ์ค‘๊ธ‰ ํ•˜์Šค ์ผˆ ์ค‘๊ธ‰ ๋ชจ๋“ˆ(Modules) ๋…๋ฆฝ ์‹คํ–‰ ํ”„๋กœ๊ทธ๋žจ(Standalone programs) ๋“ค์—ฌ ์“ฐ๊ธฐ(Indentation) ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณด์ถฉ ์„ค๋ช…(More on datatypes) ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค(Other data structures) ํด๋ž˜์Šค์™€ ํƒ€์ž…(Classes and types) Functor ํด๋ž˜์Šค(The Functor class) 1 ๋ชจ๋“ˆ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Modules ๋ชจ๋“ˆ ๋“ค์—ฌ์˜ค๊ธฐ(Importing) ํ•œ์ • ๋“ค์—ฌ์˜ค๊ธฐ(Qualified imports) ์ •์˜ ์€๋‹‰ ์žฌ์ž‘๋ช… ๋“ค์—ฌ์˜ค๊ธฐ ์žฌ์ž‘๋ช…๊ณผ ํ•œ์ • ๋“ค์—ฌ์˜ค๊ธฐ๋ฅผ ์„ž์–ด ์“ฐ๊ธฐ ๋‚ด๋ณด๋‚ด๊ธฐ(Exporting) ๋…ธํŠธ ๋ชจ๋“ˆ์€ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋ฅผ ์กฐ์งํ™”ํ•˜๋Š” ์ฃผ๋œ ์ˆ˜๋‹จ์ด๋‹ค. ๋ชจ๋“ˆ์€ import ๋ฌธ์„ ์จ์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์Šค์ฝ”ํ”„ ๋‚ด๋กœ ๋“ค์—ฌ์˜ฌ ๋•Œ ์–ธ๋œป ๋ดค์—ˆ๋‹ค. ๋ชจ๋“ˆ์„ ์•Œ๊ณ  ๋‚˜๋ฉด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋” ์ž˜ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์šฐ๋ฆฌ๊ฐ€ ์Šค์Šค๋กœ ํ”„๋กœ๊ทธ๋žจ์˜ ๊ตฌ์กฐ๋ฅผ ๋นš๊ณ  GHCi์™€ ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ๋…๋ฆฝ ์‹คํ–‰ ํ”„๋กœ๊ทธ๋žจ์„ ์ œ์ž‘ํ•  ๋•Œ ๋„์›€์ด ๋œ๋‹ค. (์šฐ์—ฐํžˆ๋„ ๋ฐ”๋กœ ๋‹ค์Œ ์žฅ์˜ ์ฃผ์ œ๊ฐ€ ๋…๋ฆฝ ์‹คํ–‰ ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค) ๋ชจ๋“ˆ ํ•˜์Šค ์ผˆ ๋ชจ๋“ˆ 1์€ ์„œ๋กœ ์—ฐ๊ด€๋œ ๊ธฐ๋Šฅ๋“ค์„ ํ•˜๋‚˜์˜ ํŒจํ‚ค์ง€๋กœ ๋ชจ์œผ๊ณ  ์ด๋ฆ„์ด ๊ฐ™์€ ๋ณ„๊ฐœ์˜ ํ•จ์ˆ˜๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š” ์œ ์šฉํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ๋ชจ๋“ˆ ์ •์˜๋Š” ํ•˜์Šค ์ผˆ ํŒŒ์ผ์˜ ๋งจ ์ฒ˜์Œ์— ์˜จ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ๋ชจ๋“ˆ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. module YourModule where ๋‹ค์Œ ์‚ฌํ•ญ์„ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ๋ชจ๋“ˆ ์ด๋ฆ„์€ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ๊ฐ๊ฐ์˜ ํŒŒ์ผ์€ ๋ชจ๋“ˆ์„ ํ•˜๋‚˜๋งŒ ํฌํ•จํ•œ๋‹ค. ํŒŒ์ผ์˜ ์ด๋ฆ„์€ ๋ชจ๋“ˆ ์ด๋ฆ„์— ํŒŒ์ผ ํ™•์žฅ์ž. hs๋ฅผ ๋”ํ•œ ๊ฒƒ์ด๋‹ค. ๋ชจ๋“ˆ ์ด๋ฆ„์— ํฌํ•จ๋œ ์  '.'์€ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋Œ€์‘ํ•œ๋‹ค. 2 ์ฆ‰ YourModule ๋ชจ๋“ˆ์€ YourModule.hs ํŒŒ์ผ์— ๋“ค์–ด๊ฐ€๊ณ  Foo.Bar ๋ชจ๋“ˆ์€ Foo/Bar.hs ๋˜๋Š” Foo\Bar.hs ํŒŒ์ผ์— ๋“ค์–ด๊ฐ„๋‹ค. ๋ชจ๋“ˆ ์ด๋ฆ„์€ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ์ผ ์ด๋ฆ„๋„ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. ๋“ค์—ฌ์˜ค๊ธฐ(Importing) ํ•œ ๋ชจ๋“ˆ์€ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์˜ ํ•จ์ˆ˜๋ฅผ ๋“ค์—ฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ๋ชจ๋“ˆ ์„ ์–ธ๊ณผ ๋‚˜๋จธ์ง€ ์ฝ”๋“œ์˜ ์‚ฌ์ด์— import ์„ ์–ธ์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค. import Data.Char (toLower, toUpper) -- import only the functions toLower and toUpper from Data.Char import Data.List -- import everything exported from Data.List import MyModule -- import everything exported from MyModule ๋“ค์—ฌ์˜จ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค์„ ๊ธฐ์ˆ ํ•  ๋•Œ๋Š” ์ด๋ฆ„ ๋’ค์— ๋“ค์—ฌ์˜จ ์ƒ์„ฑ์ž๋“ค์˜ ๋ชฉ๋ก์„ ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ผ๋‹ค. import Data.Tree (Tree(Node)) -- import only the Tree data type and its Node constructor from Data.Tree ์„œ๋กœ ์ค‘๋ณต๋˜๋Š” ์ •์˜๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“ˆ๋“ค์„ ๋“ค์—ฌ์˜ฌ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? ์•„๋‹ˆ๋ฉด ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์™”๋Š”๋ฐ ์—ฌ๋Ÿฌ๋ถ„๋งŒ์˜ ํ•จ์ˆ˜๋ฅผ ์žฌ์ •์˜ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด? ์ด๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๋ฐ”๋กœ ํ•œ์ • ๋“ค์—ฌ์˜ค๊ธฐ, ์ •์˜ ์€๋‹‰, ์žฌ์ž‘๋ช… ๋“ค์—ฌ์˜ค๊ธฐ๋‹ค. ํ•œ์ • ๋“ค์—ฌ์˜ค๊ธฐ(Qualified imports) MyModule๊ณผ MyOtherModule ๋‘˜ ๋‹ค remove_e๋ผ๋Š” ์ •์˜๋ฅผ ํฌํ•จํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ์ด ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์—์„œ ๋ชจ๋“  e๋ฅผ<NAME>๋‹ค. ๊ทธ๋Ÿฐ๋ฐ MyModule์€ ์†Œ๋ฌธ์ž e๋งŒ<NAME>์ง€๋งŒ MyOtherModule์€ ๋Œ€์†Œ๋ฌธ์ž e๋ฅผ ๋ชจ๋‘<NAME>๋‹ค. ์ด ๊ฒฝ์šฐ ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์• ๋งคํ•œ ๊ตฌ์„์ด ์žˆ๋‹ค. import MyModule import MyOtherModule -- someFunction puts a c in front of the text, and removes all e's from the rest someFunction :: String -> String someFunction text = 'c' : remove_e text ๋ฌด์Šจ remove_e๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฑด์ง€ ํ™•์‹คํ•˜์ง€ ์•Š๋‹ค! ์ด๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ํ”ผํ•˜๋ ค๋ฉด qualified ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. import qualified MyModule import qualified MyOtherModule someFunction text = 'c' : MyModule.remove_e text -- Will work, removes lower case e's someOtherFunction text = 'c' : MyOtherModule.remove_e text -- Will work, removes all e's someIllegalFunction text = 'c' : remove_e text -- Won't work as there is no remove_e defined ํ›„์ž์˜ ์ฝ”๋“œ ์กฐ๊ฐ์—์„œ ์ด๋ฆ„์ด remove_e์ธ ํ•จ์ˆ˜๋Š” ์ „ํ˜€ ์ด์šฉํ•  ์ˆ˜ ์—†๋‹ค. ํ•œ์ • ๋“ค์—ฌ์˜ค๊ธฐ๋ฅผ ํ•˜๋ฉด ๋ชจ๋“  ๋“ค์—ฌ์˜จ ๊ฐ’์€ ๋ชจ๋“ˆ ์ด๋ฆ„์„ ์•ž์— ํฌํ•จํ•œ๋‹ค. ์ •์ƒ์ ์ธ ๋“ค์—ฌ์˜ค๊ธฐ๋ฅผ ํ•ด๋„ ๋ชจ๋“ˆ ์ด๋ฆ„์„ ์•ž์— ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค. ("qualified" ํ‚ค์›Œ๋“œ๋ฅผ ๋„ฃ์ง€ ์•Š์•„๋„ MyModule.remove_e๋Š” ์ž‘๋™ํ•œ๋‹ค) ์ž ๊น MyModule.remove_e ๊ฐ™์€ ํ•œ์ • ์ด๋ฆ„๊ณผ ํ•จ์ˆ˜ ํ•ฉ์„ฑ ์—ฐ์‚ฐ์ž (.)์— ๊ด€ํ•ด ์• ๋งคํ•œ ์ ์ด ์žˆ๋‹ค. reverse.MyModule.remove_e๋ผ๊ณ  ์“ฐ๋ฉด ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํ˜ผ๋ž€์Šค๋Ÿฌ์›Œํ•  ๊ฒƒ์ด๋‹ค. ํ•œ ๊ฐ€์ง€ ๋ฉ‹์ง„ ํ•ด๊ฒฐ์ฑ…์ด ์žˆ๋‹ค. ํ•จ์ˆ˜ ํ•ฉ์„ฑ์—๋Š” ํ•ญ์ƒ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. ์˜ˆ๋ฅผ ๋“ค์–ด, reverse . remove_e ๋˜๋Š” Just . remove_e ๋˜๋Š” ์‹ฌ์ง€์–ด Just . MyModule.remove_e๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ •์˜ ์€๋‹‰ ์ด๋ฒˆ์—๋Š” MyModule๊ณผ MyOtherModule์„ ๋“ค์—ฌ์˜ค์ง€๋งŒ ์†Œ๋ฌธ์ž๋งŒ ์ง€์šธ ์ผ์€ ์—†๊ณ  ๋ชจ๋“  e๋ฅผ<NAME>๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ๋ชจ๋“  remove_e ํ˜ธ์ถœ์— MyOtherModule์„ ๋ถ™์ด๋Š” ๊ฑด ๋ฌด์ฒ™์ด๋‚˜ ์ง€๋ฃจํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ƒฅ MyModule์—์„œ remove_e๋ฅผ ์ œ์™ธํ•  ์ˆ˜๋Š” ์—†์„๊นŒ? import MyModule hiding (remove_e) import MyOtherModule someFunction text = 'c' : remove_e text ์ด ์ฝ”๋“œ๋Š” import ์ค„์˜ hiding ๋‹จ์–ด ๋•๋ถ„์— ์ž˜ ์ž‘๋™ํ•œ๋‹ค. "hiding" ํ‚ค์›Œ๋“œ ๋’ค์— ์˜ค๋Š” ๊ฒƒ์€ ๋“ค์—ฌ์˜ค์ง€ ์•Š๋Š”๋‹ค. ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์„œ ์—ฌ๋Ÿฌ ํ•ญ๋ชฉ์„ ์€๋‹‰ํ•  ์ˆ˜ ์žˆ๋‹ค. import MyModule hiding (remove_e, remove_f) ์žฌ์ž‘๋ช… ๋“ค์—ฌ์˜ค๊ธฐ ๋ฎ์–ด์“ฐ๊ธฐ๋ฅผ ์œ„ํ•œ ๊ธฐ๋ฒ•์€ ์•„๋‹ˆ์ง€๋งŒ, qualified ํ”Œ๋ž˜๊ทธ์™€ ํ•จ๊ป˜ ์ž์ฃผ ์“ฐ์ด๊ณค ํ•œ๋‹ค. import qualified MyModuleWithAVeryLongModuleName someFunction text = 'c' : MyModuleWithAVeryLongModuleName.remove_e text ํŠนํžˆ qualified๋ฅผ ์“ธ ๋•Œ๋ฉด ์ด๋Ÿฐ ๊ฒƒ์ด ๋งค์šฐ ์„ฑ๊ฐ€์‹œ๋‹ค. as ํ‚ค์›Œ๋“œ๋กœ ์ด ์ƒํ™ฉ์„ ํƒ€๊ฐœํ•  ์ˆ˜ ์žˆ๋‹ค. import qualified MyModuleWithAVeryLongModuleName as Shorty someFunction text = 'c' : Shorty.remove_e text ๋“ค์—ฌ์˜จ ํ•จ์ˆ˜์— MyModuleWithAVeryLongModuleName ๋Œ€์‹  Shorty๋ฅผ ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ์žฌ์ž‘๋ช…์€ qualified๊ฐ€ ์žˆ๋“  ์—†๋“  ๊ฐ€๋Šฅํ•˜๋‹ค. ์ถฉ๋Œํ•˜๋Š” ํ•ญ๋ชฉ์ด ์—†๋Š” ํ•œ ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์„ ๋“ค์—ฌ์™€ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์žฌ์ž‘๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค. import MyModule as My import MyCompletelyDifferentModule as My ์ด ๊ฒฝ์šฐ MyModule๊ณผ MyCompletelyDifferentModule ๋ชจ๋‘ My๋กœ ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์žฌ์ž‘๋ช…๊ณผ ํ•œ์ • ๋“ค์—ฌ์˜ค๊ธฐ๋ฅผ ์„ž์–ด ์“ฐ๊ธฐ ๊ฐ€๋”์€ ๊ฐ™์€ ๋ชจ๋“ˆ์— import ์ง€์‹œ๋ฌธ์„ ๋‘ ๋ฒˆ ์“ฐ๋Š” ๊ฒƒ์ด ํŽธํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์ „ํ˜•์ ์ธ ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. import qualified Data.Set as Set import Data.Set (Set, empty, insert) ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด Data.Set ๋ชจ๋“ˆ์˜ ๋ชจ๋“  ๊ฒƒ์— "Set"์ด๋ผ๋Š” ๋ณ„์นญ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ  ๋ช‡ ๊ฐ€์ง€ ํ•จ์ˆ˜(empty, insert, constructor)๋Š” "Set"์„ ์•ˆ ๋ถ™์—ฌ๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‚ด๋ณด๋‚ด๊ธฐ(Exporting) ์ด ๊ธ€์˜ ์„œ๋‘์˜ ์˜ˆ์ œ์—์„œ "MyModule์ด ๋‚ด๋ณด๋‚ธ ๋ชจ๋“  ๊ฒƒ์„ ๋“ค์—ฌ์˜จ๋‹ค"๋ผ๋Š” ๋ง์„ ์ผ๋‹ค. 3 ์—ฌ๊ธฐ์„œ ์งˆ๋ฌธ. ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ๋‚ด๋ณด๋‚ด๊ณ  ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ๋‚ด๋ถ€์— ๋†”๋‘˜์ง€ ์–ด๋–ป๊ฒŒ ๊ฒฐ์ •ํ•  ๊ฒƒ์ธ๊ฐ€? ๊ทธ ๋ฐฉ๋ฒ•์€ ์ด๋ ‡๋‹ค. module MyModule (remove_e, add_two) where add_one blah = blah + 1 remove_e text = filter (/= 'e') text add_two blah = add_one . add_one $ blah ์—ฌ๊ธฐ์„œ remove_e์™€ add_two๋งŒ ๋‚ด๋ณด๋‚ด๊ธฐ์˜ ๋Œ€์ƒ์ด๋‹ค. add_two๋Š” add_one์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ MyModule์„ ๋“ค์—ฌ์˜ค๋Š” ๋ชจ๋“ˆ์— ์žˆ๋Š” ํ•จ์ˆ˜์—์„œ๋Š” add_one์ด ๋‚ด๋ณด๋‚ด๊ธฐ์˜ ๋Œ€์ƒ์ด ์•„๋‹ˆ๋ผ ์ง์ ‘ ์ด์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋‚ด๋ณด๋‚ด๊ธฐ๋Š” ๋“ค์—ฌ์˜ค๊ธฐ์™€ ๋น„์Šทํ•˜๋‹ค. ํƒ€์ž…์„ ์“ฐ๊ณ  ์ƒ์„ฑ์ž ๋ชฉ๋ก์„ ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ผ๋‹ค. module MyModule2 (Tree(Branch, Leaf)) where data Tree a = Branch {left, right :: Tree a} | Leaf a ์ด ๊ฒฝ์šฐ ๋ชจ๋“ˆ ์„ ์–ธ์„ "MyModule2 (Tree(..))"๋กœ ๋‹ค์‹œ ์จ์„œ ๋ชจ๋“  ์ƒ์„ฑ์ž๋ฅผ ๋‚ด๋ณด๋‚ด๋„๋ก ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‚ด๋ณด๋‚ด๊ธฐ ๋ชฉ๋ก์„ ์œ ์ง€ํ•˜๋ฉด ์ด๋ฆ„ ๊ณต๊ฐ„ ์˜ค์—ผ์„ ์ค„์ด๊ณ  ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—” ๋ถˆ๊ฐ€๋Šฅํ•œ ์ปดํŒŒ์ผ ํƒ€์ž„ ์ตœ์ ํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋…ธํŠธ ๋ชจ๋“ˆ ์ฒด๊ณ„์— ๊ด€ํ•œ ๋ณด๋‹ค ์ž์„ธํ•œ ์‚ฌํ•ญ์€ ํ•˜์Šค ์ผˆ ๋ณด๊ณ ์„œ๋ฅผ ๋ณด์ž. โ†ฉ Haskell2010 ์ด์ „์— ํ•˜์Šค์ผˆ์˜ ์ตœ์‹  ํ‘œ์ค€์ด์—ˆ๋˜ Haskell98์—์„œ๋Š” ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์ด ์ƒ๋‹นํžˆ ๋ณด์ˆ˜์ (?) ์ด์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์š”์ฆ˜์€ ๊ณ„์ธต์  ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์„ ์ˆ˜์šฉํ•˜์—ฌ ๋งˆ์นจํ‘œ๋ฅผ ํ†ตํ•ด ์ด๋ฆ„ ๊ณต๊ฐ„๋“ค์„ ๋ถ„๋ฆฌํ•œ๋‹ค. โ†ฉ ๋ชจ๋“ˆ์€ ๋‹ค๋ฅธ ๋ฐ์„œ ๋“ค์—ฌ์˜จ ํ•จ์ˆ˜๋ฅผ ๋‚ด๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ๋‹ค. ์ƒํ˜ธ ์žฌ๊ท€์ ์œผ๋กœ ์˜์กดํ•˜๋Š” ๋ชจ๋“ˆ๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ํŠน์ˆ˜ํ•œ ์กฐ์น˜๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. โ†ฉ 2 ๋…๋ฆฝ ์‹คํ–‰ ํ”„๋กœ๊ทธ๋žจ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Standalone_programs ๊ฐ„๋‹จํ•œ ํ•˜์Šค ์ผˆ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ์ฆ‰ ๋…๋ฆฝ ์‹คํ–‰ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž. Main ๋ชจ๋“ˆ ๊ธฐ๋ณธ์ ์ธ ์š”๊ตฌ์‚ฌํ•ญ์œผ๋กœ Main ๋ชจ๋“ˆ์ด ์žˆ์–ด์•ผ ํ•˜๊ณ  ์ด ๋ชจ๋“ˆ์€ IO() ํƒ€์ž…์˜ main ์ด๋ž€ ์ง„์ž…์ ์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค. -- thingamie.hs module Main where main = putStrLn "Bonjour, world!" ์ด๊ฒƒ์„ GHCi๋กœ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ๋ฒ•์€ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋‹ค. ๋‹ค์Œ์€ ์ด ํŒŒ์ผ์„ ์ปดํŒŒ์ผํ•˜๊ณ  ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. $ ghc --make -o bonjourWorld thingamie.hs $ ./bonjourWorld Bonjour, world! ๋ณด๋ผ! ์ด๋กœ์จ ํ•˜์Šค ์ผˆ๋กœ ๋งŒ๋“  ๋…๋ฆฝ ์‹คํ–‰ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํ•˜๋‚˜ ๋งŒ๋“ค์—ˆ๋‹ค. ๋‹ค๋ฅธ ๋ชจ๋“ˆ ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ์€ ์ ์  ๋ณต์žกํ•ด์ง€๋ฉฐ ์—ฌ๋Ÿฌ ํŒŒ์ผ๋กœ ์ชผ๊ฐœ์ง„๋‹ค. ์—ฌ๊ธฐ ๋ชจ๋“ˆ ๋‘ ๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์žˆ๋‹ค. -- Hello.hs module Hello where hello = "Bonjour, world!" -- thingamie.hs module Main where import Hello main = putStrLn hello ์ด ์ƒˆ๋กœ์šด ํ”„๋กœ๊ทธ๋žจ์„ ์ „๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ปดํŒŒ์ผํ•  ์ˆ˜ ์žˆ๋‹ค. --make ํ”Œ๋ž˜๊ทธ๋Š” ์ปดํŒŒ์ผํ•˜๋ ค๋Š” ํŒŒ์ผ๋“ค์˜ ์˜์กด์„ฑ์„ GHC๊ฐ€ ์ž๋™์œผ๋กœ ๊ฐ์ง€ํ•˜๋„๋ก ํ•œ๋‹ค. thingamie.hs๊ฐ€ Hello ๋ชจ๋“ˆ์„ ๋“ค์—ฌ์˜ค๊ธฐ ๋•Œ๋ฌธ์—, GHC๋Š” ํ˜„ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ Hello๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํ•˜์Šค ์ผˆ ํŒŒ์ผ์„ ์ฐพ์„ ๊ฒƒ์ด๋‹ค. Hello๊ฐ€ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์— ์˜์กดํ•œ๋‹ค๋ฉด GHC๋Š” ๊ทธ ์˜์กด์„ฑ๋„ ์•Œ์•„์„œ ๊ฐ์ง€ํ•œ๋‹ค. $ ghc --make -o bonjourWorld thingamie.hs $ ./bonjourWorld Bonjour, world! Main ๋ชจ๋“ˆ์ด ํฌํ•จ๋œ ํŒŒ์ผ์ธ thingamie.hs์˜ ์ด๋ฆ„์ด ๋ชจ๋“ˆ ์ด๋ฆ„๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„์ฑ˜๋Š”๊ฐ€? ๋ชจ๋“ˆ ์žฅ์—์„œ ํ•œ ๋ง๊ณผ ๋‹ค๋ฅธ๋ฐ ์–ด๋–ป๊ฒŒ ๋œ ๊ฑธ๊นŒ? ๋ชจ๋“ˆ ์ด๋ฆ„๊ณผ ํŒŒ์ผ ์ด๋ฆ„์„ ๋งž์ถ”๋Š” ๊ฒƒ์€ GHC๊ฐ€ ํŒŒ์ผ๋“ค์„ ์ž๋™์œผ๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. ์ด ์ž‘์—…์€ ํ•ญ์ƒ Main์—์„œ ์‹œ์ž‘ํ•˜๊ณ  GHC์—์„œ ๋ช…๋ ์ค„์— ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์€ Main์ด ๋“  ํŒŒ์ผ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด ํŒŒ์ผ์€ ๊ด€์Šต์„ ๋”ฐ๋ฅผ ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋‹ค๋ฅธ ์œ„์น˜์—์„œ(ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ค‘์ฒฉ ๊ตฌ์กฐ๋ฅผ ํฌํ•จํ•ด์„œ) ์†Œ์Šค ํŒŒ์ผ์„ ๊ฒ€์ƒ‰ํ•˜๋ ค๋ฉด, -i ํ”Œ๋ž˜๊ทธ๋กœ ์˜์กด์„ฑ ๊ฒ€์ƒ‰์˜ ์‹œ์ž‘์ ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ํ”Œ๋ž˜๊ทธ๋Š” ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋ฆ„์„ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค. ๋‹ค์†Œ ์ธ์œ„์ ์ธ ๋‹ค์Œ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” src/ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์„ธ ํŒŒ์ผ์ด ์ „๋ถ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค. ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. HaskellProgram/ src/ Main.hs GUI/ Interface.hs Functions/ Mathematics.hs Main ๋ชจ๋“ˆ์€ ๋ชจ๋“ˆ ์ด๋ฆ„๊ณผ ์œ ์‚ฌํ•œ ๊ฒฝ๋กœ๋ฅผ ๊ฒ€์ƒ‰ํ•ด ์˜์กด ๊ด€๊ณ„๋ฅผ ๋“ค์—ฌ์˜จ๋‹ค. ๋ง์ธ์ฆ‰ import GUI.Interface๋Š” GUI/Interface๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค(์•Œ๋งž์€ ํŒŒ์ผ ํ™•์žฅ์ž๋ฅผ ๋ง๋ถ™์—ฌ์„œ). ์ด ํ”„๋กœ๊ทธ๋žจ์„ HaskellProgram ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์ปดํŒŒ์ผํ•˜๋ ค๋ฉด GHC๋ฅผ ์ด๋ ‡๊ฒŒ ์‹คํ–‰ํ•˜๋ผ. $ ghc --make -isrc -o sillyprog Main.hs 3 ๋“ค์—ฌ์“ฐ๊ธฐ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Indentation ๋“ค์—ฌ ์“ฐ๊ธฐ์˜ ํ™ฉ๊ธˆ๋ฅ  ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ํŠน๋ณ„ํ•œ ๋ฌธ์ž๋“ค ๋ ˆ์ด์•„์›ƒ ์‹ค์ „ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๊นŒ์ง€ ๋“ค์—ฌ ์“ฐ๊ธฐ ํ•˜๋ผ do ์•ˆ์˜ if ๋…ธํŠธ ํ•˜์Šค์ผˆ์€ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ์˜ ์žฅํ™ฉํ•จ์„ ๋œ์–ด๋‚ธ๋‹ค. ์‹ค์ œ ์šฉ๋ฒ•์€ ์กฐ๊ธˆ ๋ณต์žกํ•˜์ง€๋งŒ ๊ทผ๋ณธ์ ์ธ ๋ ˆ์ด์•„์›ƒ ๊ทœ์น™์€ ๋ช‡ ๊ฐœ ๋ฐ–์— ์—†๋‹ค.1 ๋“ค์—ฌ ์“ฐ๊ธฐ์˜ ํ™ฉ๊ธˆ๋ฅ  ์–ด๋–ค ํ‘œํ˜„์‹์˜ ์ผ๋ถ€์ธ ์ฝ”๋“œ๋Š” ๊ทธ ํ‘œํ˜„์‹์˜ ์‹œ์ž‘ ๋ถ€๋ถ„๋ณด๋‹ค ๋” ๋“ค์—ฌ ์จ์•ผ ํ•œ๋‹ค. ๊ทธ ํ‘œํ˜„์‹์ด ๊ทธ ์ค„์—์„œ ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ์š”์†Œ๊ฐ€ ์•„๋‹์ง€๋ผ๋„ ๊ทธ๋ ‡๊ฒŒ ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ€์žฅ ์‰ฌ์šด ์˜ˆ๊ฐ€ 'let' ๋ฐ”์ธ๋”ฉ ๊ทธ๋ฃน์ด๋‹ค. ๋ณ€์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ๋“ฑ์‹์€ 'let' ํ‘œํ˜„์‹์˜ ์ผ๋ถ€์ด๋ฉฐ ๋”ฐ๋ผ์„œ ๊ทธ ๋ฐ”์ธ๋”ฉ ๊ทธ๋ฃน์˜ ์‹œ์ž‘์ธ 'let' ํ‚ค์›Œ๋“œ๋ณด๋‹ค ๋” ๋“ค์—ฌ ์จ์•ผ ํ•œ๋‹ค. ๋ณ„๊ฐœ์˜ ์ค„์—์„œ ํ‘œํ˜„์‹์„ ์‹œ์ž‘ํ•  ๋•Œ๋Š” ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ๋” ํ•˜๋ฉด ๋œ๋‹ค. ๋ฌผ๋ก  ๋” ๋งŽ์ด ๋„์–ด ์“ฐ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๊ณ  ๊ทธ๊ฒŒ ๋” ๊น”๋”ํ•  ๊ฒƒ์ด๋‹ค. let x = a y = b ์ฒซ ๋ฒˆ์งธ ์ ˆ์„ 'let' ์˜†์— ๋‘๊ณ  ๋‚˜๋จธ์ง€๋ฅผ ์ค„ ๋งž์ถฐ ๋“ค์—ฌ ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค. ํ‹€๋ฆผ ํ‹€๋ฆผ ๋งž์Œ let x = a y = b let x = a y = b let x = a y = b ์ž…๋ฌธ์ž๋“ค์€ ์ด๊ฒƒ ๋•Œ๋ฌธ์— ํ˜ผ๋ž€์— ๋น ์ง€๊ณ ๋Š” ํ•œ๋‹ค. ๋ชจ๋“  ๊ทธ๋ฃน ํ‘œํ˜„์‹์€ ์ •ํ™•ํžˆ ์—ด์„ ๋งž์ถฐ์•ผ ํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„์—์„œ ํ•˜์Šค์ผˆ์€ ํ‘œํ˜„์‹์˜ ์ขŒ์ธก์— ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ์„ ๊ณต๋ฐฑ์ด ์•„๋‹ˆ์–ด๋„ ๋“ค์—ฌ ์“ฐ๊ธฐ๋กœ ์„ผ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋” ๋“ค์–ด๋ณด๊ฒ ๋‹ค. do foo bar baz do foo bar baz where x = a y = b case x of p -> foo p' -> baz 'case'์˜ ๊ฒฝ์šฐ ๋”ธ๋ ค์˜ค๋Š” ์ฒซ ๋ฒˆ์งธ ํ‘œํ˜„์‹์„ 'case' ํ‚ค์›Œ๋“œ์™€ ๊ฐ™์€ ์„ ์— ๋†“๋Š” ์ฝ”๋“œ๋Š” ์˜ฌ๋ฐ”๋ฅด๊ธด ํ•˜์ง€๋งŒ ํ”ํ•˜์ง€๋Š” ์•Š๋‹ค. ๊ทธ๋ž˜์„œ ๋ถ€์ˆ˜์ ์ธ ํ‘œํ˜„์‹๋“ค์€ 'case' ์ ˆ๋ณด๋‹ค ๋”ฑ ํ•œ ๋ฒˆ ๋” ๋“ค์—ฌ์“ฐ๊ธฐ ๋˜๊ณ ๋Š” ํ•œ๋‹ค. ํ™”์‚ดํ‘œ๋“ค์„ ์ •๋ ฌํ•œ ๊ฒƒ์€ ์ˆœ์ „ํžˆ ์‹ฌ๋ฏธ์ ์ธ ๊ฒƒ์ด๋ฉฐ ๋‹ค๋ฅธ ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ํŒ๋‹จ๋˜์ง€ ์•Š๋Š”๋‹ค. ์˜ค์ง ๋“ค์—ฌ ์“ฐ๊ธฐ, ์ฆ‰ ๊ฐ€์žฅ ์™ผ์ชฝ ๋์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ณต๋ฐฑ๋งŒ์ด ๋ ˆ์ด์•„์›ƒ ํ•ด์„์„ ๋‹ค๋ฅด๊ฒŒ ๋งŒ๋“ ๋‹ค. ํ‘œํ˜„์‹์ด ์™ผ์ชฝ ๋์—์„œ ์‹œ์ž‘ํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒํ™ฉ์ด ๋ณต์žกํ•ด์ง„๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” ๊ทธ์ € ํ‘œํ˜„์‹์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์„ ํฌํ•จํ•˜๋Š” ์ค„๋ณด๋‹ค๋งŒ ๋งŽ์ด ๋“ค์—ฌ ์“ฐ๋Š” ๊ฒƒ์ด ์ƒ์ฑ…์ด๋‹ค. myFunction firstArgument secondArgument = do -- the 'do' doesn't start at the left-hand edge foo -- so indent these commands more than the beginning of the line containing the 'do'. bar baz ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋Œ€์•ˆ๋“ค๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. myFunction firstArgument secondArgument = do foo bar baz myFunction firstArgument secondArgument = do foo bar baz myFunction firstArgument secondArgument = do foo bar baz ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ํŠน๋ณ„ํ•œ ๋ฌธ์ž๋“ค ์‚ฌ์‹ค ๊ทธ๋ฃนํ™”์™€ ๋ถ„๋ฆฌ์— C ๊ฐ™์€ "์ผ์ฐจ์›" ์–ธ์–ด์ฒ˜๋Ÿผ ์„ธ๋ฏธ์ฝœ๋ก ๊ณผ ์ค‘๊ด„ํ˜ธ๋ฅผ ์“ด๋‹ค๋ฉด ๋“ค์—ฌ ์“ฐ๊ธฐ๋Š” ์„ ํƒ ์‚ฌํ•ญ์ผ ๋ฟ์ด๋‹ค. ์˜๋ฏธ ์žˆ๋Š” ๋“ค์—ฌ ์“ฐ๊ธฐ๊ฐ€ ์ฝ”๋“œ๋ฅผ ๋” ๋ณด๊ธฐ ์ข‹๊ฒŒ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์ด ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์˜ ์—ฌ๋ก ์ด์ง€๋งŒ, ํ•œ ์–‘์‹์—์„œ ๋‹ค๋ฅธ ์–‘์‹์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ฉด ๋“ค์—ฌ์“ฐ๊ธฐ ๊ทœ์น™์„ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋œ๋‹ค. ์ „์ฒด์ ์ธ ๋ ˆ์ด์•„์›ƒ ๊ณผ์ •์€ ์„ธ ๊ฐœ์˜ ๋ณ€ํ™˜ ๊ทœ์น™๊ณผ, ๊ฑฐ์˜ ๋งˆ์ฃผ์น  ์ผ์ด ์—†๋Š” ๋„ค ๋ฒˆ์งธ ๊ทœ์น™์œผ๋กœ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ ˆ์ด์•„์›ƒ ํ‚ค์›Œ๋“œ(let, where, of, do) ์ค‘ ํ•˜๋‚˜๋ฅผ ๋งŒ๋‚˜๋ฉด ๋ฐ”๋กœ ๋’ค์— ์—ฌ๋Š” ์ค‘๊ด„ํ˜ธ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค. ๊ฐ™์€ ์ˆ˜์ค€์œผ๋กœ ๋“ค์—ฌ ์“ด ๊ฒƒ์€ ์„ธ๋ฏธ์ฝœ๋ก ์„ ์‚ฝ์ž…ํ•œ๋‹ค. ์ ๊ฒŒ ๋“ค์—ฌ ์“ด ๊ฒƒ์— ๋‹ซ๋Š” ์ค‘๊ด„ํ˜ธ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค. ๋ชฉ๋ก์—์„œ ์˜ˆ์ƒํ•˜์ง€ ๋ชปํ•œ ๊ฒƒ์ด ์žˆ์œผ๋ฉด ์„ธ๋ฏธ์ฝœ๋ก  ๋Œ€์‹  ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ด ์ •์˜๋Š” foo :: Double -> Double foo x = let s = sin x c = cos x in 2 * s * c ๋“ค์—ฌ์“ฐ๊ธฐ ๊ทœ์น™์„ ๋ฌด์‹œํ•˜๊ณ  ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. foo :: Double -> Double; foo x = let { s = sin x; c = cos x; } in 2 * s * c GHCi์—์„œ ํ•œ ์ค„์งœ๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” ๊ด„ํ˜ธ์™€ ์„ธ๋ฏธ์ฝœ๋ก ์„ ๋ช…์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํŽธ๋ฆฌํ•˜๋‹ค. Prelude> let foo :: Double -> Double; foo x = let { s = sin x; c = cos x } in 2 * s * c ์—ฐ์Šต๋ฌธ์ œ ์ œ์–ด ๊ตฌ์กฐ ์žฅ์˜ ์ด ์ฝ”๋“œ๋ฅผ ๊ด„ํ˜ธ์™€ ์„ธ๋ฏธ์ฝœ๋ก ์„ ์‚ฌ์šฉํ•ด ์žฌ์ž‘์„ฑํ•˜๋ผ. doGuessing num = do putStrLn "Enter your guess:" guess <- getLine case compare (read guess) num of LT -> do putStrLn "Too low!" doGuessing num GT -> do putStrLn "Too high!" doGuessing num EQ -> putStrLn "You Win!" ๋ ˆ์ด์•„์›ƒ ์‹ค์ „ ํ‹€๋ฆผ ํ‹€๋ฆผ ๋งž์Œ ๋งž์Œ do first thing second thing third thing do first thing second thing third thing do first thing second thing third thing do first thing second thing third thing ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๊นŒ์ง€ ๋“ค์—ฌ ์“ฐ๊ธฐ ํ•˜๋ผ ์œ„์—์„œ ์„ค๋ช…ํ•œ "๋“ค์—ฌ ์“ฐ๊ธฐ์˜ ํ™ฉ๊ธˆ๋ฅ "์— ๋”ฐ๋ผ, do ๋ธ”๋ก ๋‚ด์˜ ์ค‘๊ด„ํ˜ธ๋Š” do ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฐ”๋กœ ๋‹ค์Œ์— ์˜ค๋Š” ๊ฒƒ์— ์˜์กดํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด ๊ธฐ๋ฌ˜ํ•œ ์ฝ”๋“œ๋Š” ํ•˜๋“ฑ์˜ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. do first thing second thing third thing ๊ฒฐ๊ณผ์ ์œผ๋กœ if์™€ do๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ ์กฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ‹€๋ฆผ ๋งž์Œ ๋งž์Œ if foo then do first thing second thing third thing else do something else if foo then do first thing second thing third thing else do something else if foo then do first thing second thing third thing else do something else ์ด ๋ชจ๋“  ๊ฒƒ์€ do์— ๊ด€ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ do ์•ˆ์—์„œ ๊ฐ™์€ ์ˆ˜์ค€์— ์žˆ๋Š” ํ•ญ๋ชฉ๋“ค์„ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์— ๊ด€ํ•œ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ์˜ ๋ชจ๋“  ์ฝ”๋“œ๋Š” ์ธ์ •๋œ๋‹ค. main = do first thing second thing ๋˜๋Š” main = do first thing second thing ๋˜๋Š” main = do first thing second thing do ์•ˆ์˜ if ์ด ์กฐํ•ฉ์€ ๋งŽ์€ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋ฅผ ํ—ท๊ฐˆ๋ฆฌ๊ฒŒ ํ•œ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์™œ ์ž‘๋™ํ•˜์ง€ ์•Š์„๊นŒ? ๋ฉ‹์ง€์ง€๋งŒ ํ‹€๋ฆผ ๋ฉ‹์ง€์ง€๋„ ์•Š๊ณ  ํ‹€๋ฆผ -- why is this bad? do first thing if condition then foo else bar third thing -- still bad, just explicitly so do { first thing ; if condition ; then foo ; else bar ; third thing } ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” if ํ‘œํ˜„์‹ ์ž‘์„ฑ์ด ๋๋‚˜์ง€ ์•Š์•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ ๊ตฌ๋ฌธ์„ ์‹œ์ž‘ํ•˜๋Š” ๊ฑด์ง€ ํ˜ผ๋ž€์Šค๋Ÿฌ์›Œํ•œ๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” if condition; ๊ฐ™์ด ์ ํžŒ ๊ฒƒ์„ ๋ณด์ง€๋งŒ ์ด ์ฝ”๋“œ๋Š” ์™„์„ฑ์ด ์•ˆ ๋˜์–ด ์žˆ๋‹ค. ์ด๊ฒƒ์„ ๊ณ ์น˜๋ ค๋ฉด if ๋ธ”๋ก์˜ ํ•˜๋‹จ๋ถ€๋ฅผ ๋“ค์—ฌ ์จ์„œ then๊ณผ else๊ฐ€ if ๋ฌธ์˜ ์ผ๋ถ€๊ฐ€ ๋˜๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค. ๋ฉ‹์ง€๊ณ  ๋งž์Œ ๋ฉ‹์ง€์ง„ ์•Š์ง€๋งŒ ๋งž์Œ -- whew, fixed it! do first thing if condition then foo else bar third thing -- the fixed version without sugar do { first thing ; if condition then foo else bar ; third thing } ์ด์ œ do ๋ธ”๋ก์€ if ๋ฌธ ์ „์ฒด๋ฅผ ํ•œ ํ•ญ๋ชฉ์œผ๋กœ ๋ณธ๋‹ค. if-then-else ๋ฌธ์ด do ๋ธ”๋ก ์•ˆ์— ์žˆ์ง€ ์•Š์„ ๋•Œ๋Š” ์ด๋Ÿฐ ๋“ค์—ฌ ์“ฐ๊ธฐ๊ฐ€ ๊ธฐ์ˆ ์ ์œผ๋กœ ๋ถˆํ•„์š”ํ•˜์ง€๋งŒ ํ•ด๊ฐ€ ๋˜์ง€๋Š” ์•Š์œผ๋‹ˆ if-then-else๋ฅผ ํ•ญ์ƒ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋“ค์—ฌ ์“ฐ๋Š” ๊ฒƒ์€ ์ข‹์€ ์Šต๊ด€์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ do ์•ˆ์˜ if ๋ฌธ์ œ๋Š” ๋งŽ์€ ํ•˜์Šค ์ผˆ ์‚ฌ์šฉ์ž๋ฅผ ํ˜ผ๋ž€์Šค๋Ÿฝ๊ฒŒ ํ–ˆ๊ณ  ๊ธ‰๊ธฐ์•ผ ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ Haskell prime initiative์— ์ œ์•ˆํ•˜๊ธฐ๋ฅผ, if then else ์‚ฌ์ด์— ์ถ”๊ฐ€์ ์œผ๋กœ ์„ธ๋ฏธ์ฝœ๋ก ์„ ๋„ฃ์ž๊ณ  ํ–ˆ๋‹ค. ์ด๊ฒŒ ๋„์›€์ด ๋ ๊นŒ? ๋“ค์—ฌ์“ฐ๊ธฐ ๊ด€๋ จ ๋ฌธ์ œ๋Š” do๊ฐ€ ์™œ ๋ชจ๋‚˜ ๋”• ์—ฐ์‚ฐ์ž (>>=)์˜ ํŽธ์˜ ๊ตฌ๋ฌธ์ธ์ง€ ์‚ดํŽด๋ณผ ๋•Œ ์—ฐ๊ณ„ํ•˜์—ฌ ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. ๋…ธํŠธ The Haskell Report (lexemes)์˜ 2.7์ ˆ์„ ๋ณผ ๊ฒƒ. โ†ฉ 4 ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณด์ถฉ ์„ค๋ช… ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/More_on_datatypes ์—ด๊ฑฐํ˜• ๊ธฐ๋ช… ํ•„๋“œ Named Field(๋ ˆ์ฝ”๋“œ ๊ตฌ๋ฌธ) ํŽธ์˜ ๊ตฌ๋ฌธ์ผ ๋ฟ ๋งค๊ฐœํ™” ํƒ€์ž… Parameterized Type ๋‘˜ ์ด์ƒ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ข… ์˜ค๋ฅ˜ kind error ์—ด๊ฑฐํ˜• ์—ด๊ฑฐํ˜•์€ data ์„ ์–ธ์˜ ํŠน์ˆ˜ ๊ฒฝ์šฐ๋กœ์„œ ์–ด๋Š ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋„ ์ธ์ž๋ฅผ ๊ฐ–์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค. data Month = January | February | March | April | May | June | July | August | September | October | November | December ์ธ์ž๋ฅผ ๊ฐ–๋Š” ์ƒ์„ฑ์ž๋“ค๊ณผ ๊ฐ–์ง€ ์•Š๋Š” ์ƒ์„ฑ์ž๋“ค์„ ์„ž์„ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ ๊ฒฐ๊ณผ๋ฌผ์€ ์—ด๊ฑฐํ˜•์ด๋ผ ๋ถ€๋ฅด์ง€ ์•Š๋Š”๋‹ค. ๋‹ค์Œ ์˜ˆ์—์„œ๋Š” ๋งˆ์ง€๋ง‰ ์ƒ์„ฑ์ž๊ฐ€ ์ธ์ž ์„ธ ๊ฐœ๋ฅผ ์ทจํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ด๊ฑฐํ˜•์ด ์•„๋‹ˆ๋‹ค. data Colour = Black | Red | Green | Blue | Cyan | Yellow | Magenta | White | RGB Int Int Int ํด๋ž˜์Šค์™€ ํŒŒ์ƒํ˜• derivation์„ ๋…ผ์˜ํ•  ๋•Œ ์ถ”๊ฐ€๋กœ ๋ณด๊ฒ ์ง€๋งŒ ์—ด๊ฑฐํ˜•์ธ ๊ฒƒ๊ณผ ์•„๋‹Œ ๊ฒƒ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐ๋Š” ์‹ค์šฉ์ ์ธ ๋ฉด์˜ ์ด์œ ๊ฐ€ ์žˆ๋‹ค. ์šฐ์—ฐํžˆ๋„ Bool ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ์—ด๊ฑฐํ˜•์ด๋‹ค. data Bool = False | True deriving (Eq, Ord, Enum, Read, Show, Bounded) ๊ธฐ๋ช… ํ•„๋“œ Named Field(๋ ˆ์ฝ”๋“œ ๊ตฌ๋ฌธ) ํ™˜๊ฒฝ์„ค์ •์„ ๋‹ด์•„๋‘๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ƒ๊ฐํ•ด ๋ณด์ž. ์—ฌ๋Ÿฌ๋ถ„์ด ์ด ํƒ€์ž…์œผ๋กœ๋ถ€ํ„ฐ ๋ฉค๋ฒ„๋ฅผ ์ถ”์ถœํ•  ๋•Œ๋Š” ๊ธฐ๊ปํ•ด์•ผ ํ•œ๋‘ ๊ฐœ ์„ค์ •์—๋งŒ ๊ด€์‹ฌ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ฒŒ๋‹ค๊ฐ€, ๋งŽ์€ ์„ค์ •์ด ํƒ€์ž…์ด ๊ฐ™๋‹ค๋ฉด "์ž ๊น, ์ด ์›์†Œ๊ฐ€ ๋„ค ๋ฒˆ์งธ์˜€๋‚˜ ๋‹ค์„ฏ ๋ฒˆ์งธ์˜€๋‚˜..." ํ—ท๊ฐˆ๋ฆฌ๊ณ ๋Š” ํ•œ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ์ ‘๊ทผ์žaccessor ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ„ฐ๋ฏธ๋„ ํ”„๋กœ๊ทธ๋žจ์„ ์œ„ํ•œ ๋‹ค์Œ์˜ ํ™˜๊ฒฝ์„ค์ • ํƒ€์ž…์„ ๋ณด์ž. data Configuration = Configuration String -- user name String -- local host String -- remote host Bool -- is guest? Bool -- is super user? String -- current directory String -- home directory Integer -- time connected deriving (Eq, Show) ๊ทธ๋Ÿฌ๋ฉด ์ด๋Ÿฐ ์ ‘๊ทผ์ž ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. getUserName (Configuration un _ _ _ _ _ _ _) = un getLocalHost (Configuration _ lh _ _ _ _ _ _) = lh getRemoteHost (Configuration _ _ rh _ _ _ _ _) = rh getIsGuest (Configuration _ _ _ ig _ _ _ _) = ig -- and so on... ๋‹จ์ผ ์›์†Œ๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋ฌผ๋ก  ๋‚˜์ค‘์— ํ™˜๊ฒฝ์„ค์ •์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•œ๋‹ค๋ฉฐ ์ด ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ์ด์ œ ์ธ์ž์˜ ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค. ์ด๋Š” ๋งค์šฐ ์งœ์ฆ ๋‚˜๋Š” ์ผ์ด๊ณ  ๋ฒ„๊ทธ๊ฐ€ ์Šฌ๊ทธ๋จธ๋‹ˆ ๋“ค์–ด์˜ค๊ธฐ ์‰ฌ์šด ์žฅ์†Œ๋‹ค. ๋‹คํ–‰ํžˆ๋„ ํ•ด๊ฒฐ์ฑ…์ด ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž… ์„ ์–ธ์—์„œ ํ•„๋“œ์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. data Configuration = Configuration { user name :: String, localhost :: String, remotehost :: String, isguest :: Bool, issuperuser :: Bool, currentdir :: String, homedir :: String, timeconnected :: Integer } ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ์˜ ์ ‘๊ทผ์ž ํ•จ์ˆ˜๋“ค์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค. user name :: Configuration -> String localhost :: Configuration -> String -- etc. ๋˜ํ•œ ํŽธ๋ฆฌํ•œ ๊ฐฑ์‹  ์ˆ˜๋‹จ๋„ ์ œ๊ณต๋œ๋‹ค. ๋‹ค์Œ์€ Configuration์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋Š”, "ํ˜„์žฌ ์ž‘์—… ์ค‘์ธ ๋””๋ ‰ํ„ฐ๋ฆฌ"์™€ "๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณ€๊ฒฝ" ํ•จ์ˆ˜์˜ ์งง์€ ์˜ˆ์‹œ๋‹ค. changeDir :: Configuration -> String -> Configuration changeDir cfg newDir = if directoryExists newDir -- make sure the directory exists then cfg{currentdir = newDir} -- change our current directory else error "directory does not exist" postWorkingDir :: Configuration -> String postWorkingDir cfg = currentdir cfg -- retrieve our current directory ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ, ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ ํƒ€์ž… y์˜ ํ•„๋“œ x๋ฅผ z๋กœ ๊ฐฑ์‹ ํ•˜๋ ค๋ฉด y{x=z}๋ผ๊ณ  ์“ด๋‹ค. ๋‘˜ ์ด์ƒ์„ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ฐ๊ฐ์„ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ, y{x=z, a=b, c=d}์ฒ˜๋Ÿผ ์“ด๋‹ค. ์ž ๊น ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด์— ์ต์ˆ™ํ•œ ๋…์ž๋ผ๋ฉด ์ด ๋ชจ๋“  ๊ฑธ "์ ‘๊ทผ์ž ํ•จ์ˆ˜ accessor function"์™€ "๊ฐฑ์‹  ๋ฉ”์„œ๋“œ update method"์— ๊ด€ํ•œ ์ด์•ผ๊ธฐ๋กœ ์ƒ๊ฐํ•˜์—ฌ, y{x=z}๋Š” setter ๋ฉ”์„œ๋“œ๋กœ์„œ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” y์˜ x ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ• ์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์•„๋‹ˆ๋‹ค. ํ•˜์Šค ์ผˆ ๋ณ€์ˆ˜๋Š” ์ˆ˜์ • ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค ํ•˜์ง€ ์•Š์•˜๋˜๊ฐ€. conf2 = changeDir conf1 "/opt/foo/bar" ๊ฐ™์€ ์ฝ”๋“œ์—์„œ conf2๋Š” conf1๊ณผ ๊ฐ™์ง€๋งŒ currentdir๋งŒ์€ "/opt/foo/bar"๋กœ ๋‹ค๋ฅธ Configuration์œผ๋กœ ์ •์˜๋˜๊ณ , conf1์€ ๊ทธ๋Œ€๋กœ ๋‚จ์•„์žˆ๋Š”๋‹ค. ํŽธ์˜ ๊ตฌ๋ฌธ์ผ ๋ฟ ๋ฌผ๋ก  ์˜ˆ์ „์— ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ Configuration์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ์„ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ธฐ๋ช… ํ•„๋“œ๋Š” ํŽธ์˜ ๊ตฌ๋ฌธ์ผ ๋ฟ์ด๋‹ค. ์—ฌ์ „ํžˆ ์ด๋Ÿฐ ์‹์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค. getUserName (Configuration un _ _ _ _ _ _ _) = un ํ•˜์ง€๋งŒ ์ด๋Ÿด ํ•„์š”๋Š” ์ „ํ˜€ ์—†๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ๊ธฐ๋ช… ํ•„๋“œ์—๋„ ํŒจํ„ด ๋งค์นญ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. getHostData (Configuration {localhost=lh, remotehost=rh}) = (lh, rh) ์—ฌ๊ธฐ์„œ ๋ณ€์ˆ˜ lh๋Š” Configuration์˜ localhost ํ•„๋“œ์—, rh๋Š” remotehost ํ•„๋“œ์— ๋งค์นญ๋œ๋‹ค. ๋‘ ๋งค์นญ์€ ๋ฌผ๋ก  ์„ฑ๊ณตํ•  ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์—์„œ ๊ทธ๋žฌ๋“ฏ์ด ๋ณ€์ˆ˜ ์ด๋ฆ„ ๋Œ€์‹  ๊ฐ’์„ ๋„ฃ์–ด์„œ ๋” ์ œํ•œ๋œ ๋งค์นญ์„ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. GHC๋ฅผ ์“ฐ๊ณ  ์žˆ๋‹ค๋ฉด NamedFieldPuns ์–ธ์–ด ํ™•์žฅ์„ ์ด์šฉํ•ด ์ด๋Ÿฐ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. getHostData (Configuration {localhost, remotehost}) = (localhost, remotehost) ํ‘œ์ค€ ํ˜•ํƒœ์™€ ์„ž์–ด ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค. getHostData (Configuration {localhost, remotehost=rh}) = (localhost, rh) ์ด ์–ธ์–ด ํ™•์žฅ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— :set -XNamedFieldPuns ๋ช…๋ น์„ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ์†Œ์Šค ํŒŒ์ผ ์•ž๋จธ๋ฆฌ์— {-# LANGUAGE NamedFieldPuns #-} ํ”„๋ผ๊ทธ๋งˆfragma๋ฅผ ๋„ฃ๊ฑฐ๋‚˜ ์ปดํŒŒ์ผ๋Ÿฌ์— -XNamedFieldPuns๋ผ๋Š” ๋ช…๋ น ์ค„ ํ”Œ๋ž˜๊ทธ๋ฅผ ์ „๋‹ฌํ•˜๋ผ. Configuration์˜ ๊ฐ’๋“ค์€ ์ฒซ ๋ฒˆ์งธ ์ •์˜์ฒ˜๋Ÿผ ์˜ˆ์ „ ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๊ณ  ๋‘ ๋ฒˆ์งธ ์ •์˜์ฒ˜๋Ÿผ ๊ธฐ๋ช… ํ•„๋“œ์˜ ํƒ€์ž… ์•ˆ์— ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. initCFG = Configuration "nobody" "nowhere" "nowhere" False False "/" "/" 0 initCFG' = Configuration { user name="nobody", localhost="nowhere", remotehost="nowhere", isguest=False, issuperuser=False, currentdir="/", homedir="/", timeconnected=0 } ์ฒซ ๋ฒˆ์งธ๊ฐ€ ํ›จ์”ฌ ์งง์ง€๋งŒ ๋‘ ๋ฒˆ์งธ๊ฐ€ ํ›จ์”ฌ ๊น”๋”ํ•˜๋‹ค. ๊ฒฝ๊ณ : ๋‘ ๋ฒˆ์งธ ๋ฐฉ์‹์—์„œ๋Š” ํ•„๋“œ๋ฅผ ์ƒ๋žตํ•˜๋Š” ์ฝ”๋“œ์—ฌ๋„ ์ปดํŒŒ์ผ์ด ๋œ๋‹ค. cfgFoo = Configuration { user name = "Foo" } cfgBar = Configuraton { localhost = "Bar", remotehost = "Baz" } cfgUndef = Configuration {} ๊ธฐ์ž…ํ•˜์ง€ ์•Š์€ ํ•„๋“œ๋ฅผ ํ‰๊ฐ€ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค! ๋งค๊ฐœํ™” ํƒ€์ž… Parameterized Type ๋งค๊ฐœํ™” ํƒ€์ž…์€ ๋‹ค๋ฅธ ์–ธ์–ด์˜ "์ œ๋„ˆ๋ฆญgeneric"์ด๋‚˜ "ํ…œํ”Œ๋ฆฟ template"๊ณผ ์œ ์‚ฌํ•œ ๊ฐœ๋…์ด๋‹ค. ๋งค๊ฐœํ™” ํƒ€์ž…์€ ํ•˜๋‚˜ ์ด์ƒ์˜ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ทจํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ‘œ์ค€ Prelude ํƒ€์ž…์ธ Maybe๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋œ๋‹ค. data Maybe a = Nothing | Just a Maybe๋Š” ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜ a๋ฅผ ์ทจํ•œ๋‹ค. Maybe๋ฅผ ์ด์šฉํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. lookupBirthday :: [Anniversary] -> String -> Maybe Anniversary lookupBirthday ํ•จ์ˆ˜๋Š” ์ƒ์ผ ๋ ˆ์ฝ”๋“œ์˜ ๋ฆฌ์ŠคํŠธ์™€ ๋ฌธ์ž์—ด ํ•˜๋‚˜๋ฅผ ์ทจํ•ด Maybe Anniversary๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ํƒ€์ž…์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฌธ์ž์—ด๋กœ ์ฃผ์–ด์ง„ ์ด๋ฆ„์ด ์ƒ์ผ ๋ฆฌ์ŠคํŠธ์—์„œ ๋ฐœ๊ฒฌ๋˜๋ฉด ๋Œ€์‘ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ์˜ Just ๊ฐ’์ด๊ณ , ์•„๋‹ˆ๋ฉด Nothing์ด๋ผ๊ณ  ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค. Maybe๋Š” ํ•˜์Šค์ผˆ์—์„œ ์‹คํŒจ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ€์žฅ ๊ฐ„๊ฒฐํ•˜๊ณ  ํ”ํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์“ฐ์ผ ๋•Œ๋„ ์žˆ๋Š”๋ฐ ๊ทธ ์ธ์ž๋ฅผ ์„ ํƒ์‚ฌํ•ญ์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•จ์ด๋‹ค. ์ฆ‰ ์ƒ๋žตํ•˜๋ ค๋Š” ์ธ์ž์— Nothing์„ ์ „๋‹ฌํ•˜๋ ค๋Š” ์˜๋„๊ฐ€ ์žˆ๋‹ค. type๊ณผ newtype ์„ ์–ธ์„ ์ •ํ™•ํžˆ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋งค๊ฐœํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ๋งค๊ฐœ ํƒ€์ž…๋“ค์„ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฒฐํ•ฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ํƒ€์ž…์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘˜ ์ด์ƒ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•˜๋‚˜๋ณด๋‹ค ๋งŽ์„ ์ˆ˜ ์žˆ๋‹ค. Either ํƒ€์ž…์ด ๊ทธ๋Ÿฌํ•œ ์˜ˆ์‹œ๋‹ค. data Either a b = Left a | Right b ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ ์˜ˆ์ œ์—์„œ pairOff :: Int -> Either String Int pairOff people | people < 0 = Left "Can't pair off negative number of people." | people > 30 = Left "Too many people for this activity." | even people = Right (people `div` 2) | otherwise = Left "Can't pair off an odd number of people." groupPeople :: Int -> String groupPeople people = case pairOff people of Right groups -> "We have " ++ show groups ++ " group(s)." Left problem -> "Problem! " ++ problem pairOff๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ๋™ํ˜ธํšŒ activity์—์„œ ํŠน์ • ์ˆ˜์˜ ์‚ฌ๋žŒ๋“ค์„ ์ง์ง€์„ ๋•Œ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๊ทธ๋ฃน์ด ์ƒ๊ธธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋˜ํ•œ ์‚ฌ๋žŒ์ด ๋„ˆ๋ฌด ๋งŽ๊ฑฐ๋‚˜ ๋ˆ„๊ตฐ๊ฐ€ ๋‚˜๊ฐ€๊ฒŒ ๋  ๊ฒƒ๋„ ์•Œ๋ ค์ค€๋‹ค. ๋”ฐ๋ผ์„œ pairOff๋Š” ๊ทธ๋ฃน์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” int ๋˜๋Š” ๋ชจ์ž„์„ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ์ด์œ ๋ฅผ ์„ค๋ช…ํ•˜๋Š” String์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ข… ์˜ค๋ฅ˜ kind error ํ•˜์Šค ์ผˆ ๋งค๊ฐœ ํƒ€์ž…์˜ ์œ ์—ฐํ•จ์€ ํƒ€์ž… ์˜ค๋ฅ˜ ๊ฐ™์•„ ๋ณด์ด๋Š” ํƒ€์ž… ์„ ์–ธ ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹จ์ง€ ๊ทธ๊ฒƒ๋“ค์€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘์ด ์•„๋‹ˆ๋ผ ํƒ€์ž… ์„ ์–ธ ์‹œ์ ์— ์ผ์–ด๋‚œ๋‹ค. ์ด๋Ÿฐ "ํƒ€์ž…์˜ ํƒ€์ž…" ์˜ค๋ฅ˜๋ฅผ "์ข…" ์˜ค๋ฅ˜๋ผ ํ•œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ์ข…์„ ๊ฐ€์ง€๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์Šค์Šค๋กœ ์ถ”๋ก ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋งค๊ฐœํ™” ํƒ€์ž…์ด ์ž˜๋ชป๋˜์—ˆ๋‹ค๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ข… ์˜ค๋ฅ˜๋ฅผ ๋ณด๊ณ ํ•  ๊ฒƒ์ด๋‹ค. 5 ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Other_data_structures ํŠธ๋ฆฌ ํŠธ๋ฆฌ๋กœ์„œ์˜ ๋ฆฌ์ŠคํŠธ map๊ณผ fold map fold ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค ๋ฒ”์šฉ map ์ผ๋ฐ˜ํ™”๋œ fold ์žฌ๊ท€์  ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋Œ€ํ•œ fold ๋ฉ‹์ ธ ๋ณด์ด๋Š” ๋ง ๋…ธํŠธ ์ด ์žฅ์—์„œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๊ธฐ๋ฒ•๋“ค์„ ์–ด๋–ป๊ฒŒ ์ด์šฉํ•˜์—ฌ ๋” ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ์—ฌ๋Ÿฌ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ํŠนํžˆ ๊ฐ™์€ ํƒ€์ž…์˜ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ์žฌ๊ท€ ์ž๋ฃŒ ๊ตฌ์กฐ์˜ ์˜ˆ์‹œ๋ฅผ ๋ณผ ๊ฒƒ์ด๋‹ค. ์žฌ๊ท€ ์ž๋ฃŒ ๊ตฌ์กฐ๋Š” ๋งŽ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฒ•์—์„œ ์ƒ๋ช…์ˆ˜์™€ ๊ฐ™์œผ๋ฉฐ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๋Š” ๊ฒƒ์„ ์“ด๋‹ค๊ฑฐ๋‚˜ ํ•˜๋ฉด ๊ทธ๋Ÿฐ ๊ฒƒ์„ ์ƒˆ๋กœ ์ •์˜ํ•  ํ•„์š”๊ฐ€ ์—†์ง€๋งŒ ๊ทธ ์‹ค์ฒด๊ฐ€ ์–ด๋–ป๊ณ  ์กฐ์ž‘์€ ์–ด๋–ค ์‹์œผ๋กœ ๋˜๋Š”์ง€ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฒˆ ์žฅ์—์„œ ๊ตฌํ˜„ํ•˜๋Š” ์˜ˆ์ œ๋“ค์„ ์ž˜ ๋”ฐ๋ผ๊ฐ€๋ฉด ํ•˜์Šค ์ผˆ ๋Šฅ๋ ฅ์ด ์‹นํŠธ๋Š” ๋ฐ ์ข‹์€ ์—ฐ์Šต์ด ๋  ๊ฒƒ์ด๋‹ค. ์ž ๊น ํ•˜์Šค ์ผˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ƒํƒœ๊ณ„์—๋Š” ํ’๋ถ€ํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ๊ฐ€ ์žˆ์–ด์„œ(์žฌ๊ท€์ ์ธ ๊ฒƒ๋„ ์žˆ๊ณ  ์•„๋‹Œ ๊ฒƒ๋„ ์žˆ๋‹ค) ๊ด‘๋ฒ”์œ„ํ•œ ์‹ค์šฉ์  ์š”๊ตฌ๋ฅผ ๋งŒ์กฑํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ ์™ธ์—๋„ ๋งต, ์ง‘ํ•ฉ, ์œ ํ•œ ์ˆœ์—ด finite sequence, ๋ฐฐ์—ด ๋“ฑ ๋งŽ์€ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์žˆ๋‹ค. ํ•˜์Šค ์ผˆ ์‹ค์ „๋ฐ˜์˜ Data Structure primer๊ฐ€ ํ•ต์‹ฌ ์ž๋ฃŒ ๊ตฌ์กฐ๋“ค์„ ๋ฐฐ์šฐ๋Š” ๋ฐ๋Š” ์ข‹์€ ์ถœ๋ฐœ์ ์ด๋‹ค. ์ค‘๊ธ‰์ž ๊ณผ๋ชฉ์ด ๋ช‡ ๊ฐœ ์•ˆ ๋‚จ์•˜๋Š”๋ฐ ๋‹ค ๋๋‚ด๋ฉด ํ•œ ๋ฒˆ ํ›‘์–ด๋ณด๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. ํŠธ๋ฆฌ ์žฌ๊ท€ ์ž๋ฃŒ ๊ตฌ์กฐ ์ค‘ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์œ ํ˜•์€ ํŠธ๋ฆฌ๋‹ค. ํŠธ๋ฆฌ์—๋Š” ์—ฌ๋Ÿฌ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋Š”๋ฐ ๊ทธ์ค‘ ๊ฐ„๋‹จํ•œ ๊ฒƒ์„ ์˜ˆ์‹œ๋กœ ๊ณจ๋ž๋‹ค. ๊ทธ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. data Tree a = Leaf a | Branch (Tree a) (Tree a) ํŠธ๋ฆฌ๊ฐ€ ๋งค๊ฐœํ™”๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ Int์˜ ํŠธ๋ฆฌ, String์˜ ํŠธ๋ฆฌ, Maybe Int์˜ ํŠธ๋ฆฌ, (Int, String) ์ง์˜ ํŠธ๋ฆฌ ๋“ฑ์ด ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ํŠน๋ณ„ํ•œ ์ ์€ Tree๊ฐ€ ์ •์˜ ๊ทธ ์ž์ฒด์— ๋‚˜ํƒ€๋‚œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. Tree๋Š” a ํƒ€์ž…์˜ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ๋ฆฌํ”„(๋ง๋‹จ)์ด๊ฑฐ๋‚˜ Tree a ํƒ€์ž…์˜ ๋˜ ๋‹ค๋ฅธ ํŠธ๋ฆฌ ๋‘ ๊ฐœ๋ฅผ ๋‹ฌ๊ณ  ์žˆ๋Š” ๋ธŒ๋žœ์น˜(๋ถ„๊ธฐ)๋‹ค. ํŠธ๋ฆฌ๋กœ์„œ์˜ ๋ฆฌ์ŠคํŠธ ๋ฆฌ์ŠคํŠธ ๋ณด์ถฉ ์„ค๋ช…๊ณผ ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ์—์„œ ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‘ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. ํ•˜๋‚˜๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ([]๋กœ ํ‘œ๊ธฐ)์ธ ๊ฒฝ์šฐ๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ํŠน์ • ํƒ€์ž…์˜ ์›์†Œ ํ•˜๋‚˜์— ๋˜ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋”ํ•œ ๊ฒฝ์šฐ((x:xs)๋กœ ํ‘œ๊ธฐ)๋‹ค. ์ฆ‰ ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ์ •์˜๋Š” ์ด๋Ÿฐ ์‹์ด๋‹ค. -- ์˜์‚ฌ ํ•˜์Šค์ผˆ. ์‹ค์ œ๋กœ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค (๋ฆฌ์ŠคํŠธ๋Š” ํŠน๋ณ„ํ•œ ๋ฌธ๋ฒ•์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ) data [a] = [] | (a:[a]) ๋‹ค์Œ์€ ์‹ค์ œ๋กœ ๊ฐ€๋Šฅํ•˜๋ฉด์„œ ์œ„์™€ ๋™๋“ฑํ•œ ์ •์˜๋‹ค. data List a = Nil | Cons a (List a) ํŠธ๋ฆฌ์ฒ˜๋Ÿผ ๋ฆฌ์ŠคํŠธ๋„ ์žฌ๊ท€์ ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” []์™€ (:)๋‹ค. ์ด ๋‘˜์€ ๊ฐ๊ฐ Tree ์ •์˜์˜ Leaf์™€ Branch์— ํ•ด๋‹นํ•œ๋‹ค. ์ด๊ฒƒ์ด ๋‚ดํฌํ•˜๋Š” ๋ฐ”๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ์™€ (x:xs)์— ๋Œ€ํ•ด ํŒจํ„ด ๋งค์นญ์„ ํ–ˆ๋“ฏ์ด Leaf์™€ Branch๋„ ํŒจํ„ด ๋งค์นญ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. map๊ณผ fold ์šฐ๋ฆฌ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์œ„ํ•œ map๊ณผ fold๋ฅผ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด Tree ํƒ€์ž…์„ ์œ„ํ•œ map๊ณผ fold๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. ์š”์•ฝํ•˜์ž๋ฉด data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving (Show) data [a] = [] | (:) a [a] -- (:) a [a]๋Š” (a:[a])๋ฅผ ์ค‘์œ„๊ฐ€ ์•„๋‹ˆ๋ผ ์ „์œ„ ํ‘œ๊ธฐํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค ๋…ธํŠธ Deriving์€ ํด๋ž˜์Šค์™€ ํƒ€์ž…์—์„œ ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. ์ง€๊ธˆ์€ ํ•˜์Šค์ผˆ(๊ทธ๋ฆฌ๊ณ  ์ธํ„ฐํ”„๋ฆฌํ„ฐ)์—๊ฒŒ Tree ์ธ์Šคํ„ด์Šค๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋งํ•ด์ฃผ๋Š” ์ˆ˜๋‹จ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž. map ๋ฆฌ์ŠคํŠธ๋ฅผ ์œ„ํ•œ map ์ •์˜๋ฅผ ์‚ดํŽด๋ณด์ž. map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs treeMap์„ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ๊ทธ ํƒ€์ž…์€ ๋ฌด์—‡์ด์–ด์•ผ ํ• ๊นŒ? ํ•จ์ˆ˜์˜ ํƒ€์ž…์ด ๋ฌด์—‡์ด์–ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ์ƒ๊ฐ์ด ์žˆ์œผ๋ฉด ๊ทธ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ธฐ๊ฐ€ ์‰ฌ์›Œ์ง„๋‹ค. ์šฐ๋ฆฌ๋Š” treeMap์ด ์ž„์˜ ํƒ€์ž…์˜ Tree์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜์—ฌ, ๊ทธ ํŠธ๋ฆฌ์˜ ๊ฐ ์›์†Œ์— ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ  ๋™์ผ ํƒ€์ž…์˜ ๋˜ ๋‹ค๋ฅธ Tree๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธธ ์›ํ•œ๋‹ค. treeMap :: (a -> b) -> Tree a -> Tree b ์ด๊ฒƒ์€ ๋ฆฌ์ŠคํŠธ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ์™€ ๋น„์Šทํ•˜๋‹ค. ์ด์ œ Tree์— ๋Œ€ํ•ด ๋งํ•ด๋ณด์ž๋ฉด, ๊ฐ๊ฐ์˜ Leaf๋Š” ๊ฐ’์„ ํ•œ ๊ฐœ๋งŒ ํฌํ•จํ•˜๋ฏ€๋กœ ์šฐ๋ฆฌ๊ฐ€ ํ•  ์ผ์€ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ๊ทธ ๊ฐ’์— ์ ์šฉํ•ด ์ˆ˜์ •๋œ ๊ฐ’์„ ๋‹ด์€ ์ƒˆ Leaf๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์ „๋ถ€๋‹ค. treeMap :: (a -> b) -> Tree a -> Tree b treeMap f (Leaf x) = Leaf (f x) ๋นˆ ๋ฆฌ์ŠคํŠธ์— map์„ ์ ์šฉํ•œ ๊ฒฝ์šฐ์™€ ๋งŽ์ด ์œ ์‚ฌํ•˜๋‹ค. ์ด์ œ Branch๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š”๋ฐ, Branch๋Š” ๋‘ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ํฌํ•จํ•œ๋‹ค. ์ด๊ฒƒ๋“ค๋กœ ๋ญ˜ ํ•ด์•ผ ํ• ๊นŒ? ๋ฆฌ์ŠคํŠธ map์€ ๋ฆฌ์ŠคํŠธ์˜ ๊ผฌ๋ฆฌ์— ๋Œ€ํ•ด ์Šค์Šค๋กœ๋ฅผ ํ˜ธ์ถœํ–ˆ์œผ๋‹ˆ ๋‘ ํ•˜์œ„ ํŠธ๋ฆฌ์—๋„ ๊ทธ๋ ‡๊ฒŒ ํ•ด๋ณด์ž. treeMap์˜ ์™„์ „ํ•œ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. treeMap :: (a -> b) -> Tree a -> Tree b treeMap f (Leaf x) = Leaf (f x) treeMap f (Branch left right) = Branch (treeMap f left) (treeMap f right) treeMap f ์ž์ฒด๊ฐ€ Tree a -> Tree b ํƒ€์ž…์˜ ํ•จ์ˆ˜๋ผ๋Š” ์ ์— ์ฐฉ์•ˆํ•˜์—ฌ ์กฐ๊ธˆ ๋” ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. treeMap :: (a -> b) -> Tree a -> Tree b treeMap f = g where g (Leaf x) = Leaf (f x) g (Branch left right) = Branch (g left) (g right) ์ด ์ •์˜๊ฐ€ ๋ฐ”๋กœ ์™€๋‹ฟ์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋‹ค์‹œ ์ฝ์–ด๋ณด์ž. ์ด๋Ÿฐ ์‹์˜ ํŒจํ„ด ๋งค์นญ์ด ์ฒ˜์Œ์—๋Š” ์ด์ƒํ•˜๊ฒŒ ๋ณด์—ฌ๋„ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ํ™œ์šฉํ•˜๋Š” ํ•„์ˆ˜์ ์ธ ๋ฐฉ์‹์ด๋‹ค. ํŒจํ„ด ๋งค์นญ์€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์ˆ˜ํ–‰๋œ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์ž. ์ค€๋น„๊ฐ€ ๋˜์—ˆ๋‹ค๋ฉด Tree์— ๋Œ€ํ•œ fold๋กœ ๋„˜์–ด๊ฐ€์ž. fold ์ด๋ฒˆ์—๋„ ๋ฆฌ์ŠคํŠธ์˜ foldr ์ •์˜๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ณด์ž. foldr :: (a -> b -> b) -> b -> [a] -> b foldr f acc [] = acc foldr f acc (x:xs) = f x (foldr f acc xs) ๋ฆฌ์ŠคํŠธ์˜ ์ƒ์„ฑ์ž๋Š” ๋‘ ๊ฐœ๋ผ๋Š” ๊ฑธ ๋– ์˜ฌ๋ ค๋ณด์ž. (:) :: a -> [a] -> [a] -- ์›์†Œ ํ•˜๋‚˜๋ฅผ ๋ฐ›์•„์„œ ๋ฆฌ์ŠคํŠธ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„๊ณผ ๊ฒฐํ•ฉํ•œ๋‹ค [] :: [a] -- ๋นˆ ๋ฆฌ์ŠคํŠธ๋Š” ์ธ์ž๋ฅผ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค ๊ทธ๋Ÿฌ๋ฏ€๋กœ foldr์€ ๋‘ ์ƒ์„ฑ์ž์— ๋Œ€์‘ํ•˜๋Š” ๋‘ ์ธ์ž๋ฅผ ์ทจํ•œ๋‹ค. f :: a -> b -> b -- ํ•จ์ˆ˜๋Š” ์ธ์ž๋ฅผ ๋‘ ๊ฐœ ๋ฐ›์•„์„œ ์ด๊ฒƒ๋“ค์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๊ณ  ๋‹จ์ผ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค acc :: b -- ๋ˆ„์ ๊ธฐ๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚ ์ง€๋ฅผ ์ •์˜ํ•œ๋‹ค ์ž ์‹œ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ณ  ๋„˜์–ด๊ฐ€์ž. ์ตœ์ดˆ์— foldr์ด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์•˜๋‹ค๋ฉด ๊ธฐ๋ณธ ๋ˆ„์ ๊ธฐ(accumulator)๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹ˆ๋ฉด ์ฒซ ๋ฒˆ์งธ ์›์†Œ๊ฐ€ ๋ฆฌ์ŠคํŠธ์˜ tail์„ ์ ‘์€ ๊ฒฐ๊ณผ์™€ (f๋ฅผ ํ†ตํ•ด) ๊ฒฐํ•ฉ๋˜๋ฏ€๋กœ fold๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์„ ๋•Œ๊นŒ์ง€ ๊ณ„์†๋œ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์œ„ํ•œ foldr์ฒ˜๋Ÿผ treeFold๊ฐ€ ์–ด๋–ค ํƒ€์ž…์˜ ํŠธ๋ฆฌ๋ฅผ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธธ ์›ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ [a] -> b๊ฐ€ ์žˆ๋˜ ์ž๋ฆฌ์—๋Š” Tree a -> b๊ฐ€ ์˜จ๋‹ค. ๋ณ€ํ™˜์€ ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•ด์•ผ ํ• ๊นŒ? ๋จผ์ € ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ์ž๋ฅผ ๋‘ ๊ฐœ ๊ฐ€์ง€๋Š” ๊ฒƒ์ฒ˜๋Ÿผ Tree a๊ฐ€ ์ƒ์„ฑ์ž๋ฅผ ๋‘ ๊ฐœ ๊ฐ€์ง€๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. Branch :: Tree a -> Tree a -> Tree a Leaf :: a -> Tree a ๋”ฐ๋ผ์„œ treeFold ์—ญ์‹œ ๋‘ ์ƒ์„ฑ์ž์— ๋Œ€์‘ํ•˜๋Š” ๋‘ ์ธ์ˆ˜๋ฅผ ์ทจํ•œ๋‹ค. fbranch :: b -> b -> b fleaf :: a -> b ์ด ๋ชจ๋“  ๊ฑธ ํ•œ ๋ฐ ๋ชจ์œผ๋ฉด ๋‹ค์Œ์˜ ํƒ€์ž… ์ •์˜๋ฅผ ์–ป๋Š”๋‹ค. treeFold :: (b -> b -> b) -> (a -> b) -> Tree a -> b ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” (b -> b -> b) ํƒ€์ž…์„ ๊ฐ€์ง€๋ฉฐ, ํ•˜์œ„ ํŠธ๋ฆฌ๋“ค์„ ๊ฒฐํ•ฉํ•ด ๋‹จ์ผ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. a -> b ํƒ€์ž…์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” leaf ๋“ค์— ๋Œ€ํ•ด ์ˆ˜ํ–‰ํ•  ์ž‘์—…์„ ๊ธฐ์ˆ ํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. (๋ฆฌ์ŠคํŠธ์—์„œ ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๊ทธ๋Ÿฐ ๊ฒƒ์ฒ˜๋Ÿผ leaf๋Š” ์žฌ๊ท€์˜ ๋์ด๋‹ค) ๊ทธ๋ฆฌ๊ณ  Tree a ํƒ€์ž…์˜ ์„ธ ๋ฒˆ์งธ ์ธ์ž๋Š” fold ํ•˜๋ ค๋Š” ์ „์ฒด ํŠธ๋ฆฌ๋‹ค. treeMap๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ง€์—ญ ๋ณ€์ˆ˜ g๋ฅผ ๋„์ž…ํ•˜์—ฌ ์ธ์ž fbranch์™€ fleaf์˜ ๋ฐ˜๋ณต์„ ํ”ผํ•˜๊ฒ ๋‹ค. treeFold :: (b -> b -> b) -> (a -> b) -> Tree a -> b treeFold fbranch fleaf = g where -- definition of g goes here fleaf ์ธ์ž๋Š” Leaf์ธ ํ•˜์œ„ ํŠธ๋ฆฌ์— ๋Œ€ํ•ด ์ˆ˜ํ–‰ํ•  ์ž‘์—…์ด๋‹ค. g (Leaf x) = fleaf x fbranch ์ธ์ž๋Š” ๋‘ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ "folding"ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์ค€๋‹ค. g (Branch left right) = fbranch (g left) (g right) ์ „์ฒด ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋œ๋‹ค. treeFold :: (b -> b -> b) -> (a -> b) -> Tree a -> b treeFold fbranch fleaf = g where g (Leaf x) = fleaf x g (Branch left right) = fbranch (g left) (g right) ์ด๊ฒƒ๋“ค์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์œ„ํ•ด Tree ๋ฐ์ดํ„ฐ ์ •์˜์™€ treeMap, treeFold ํ•จ์ˆ˜๋ฅผ ํ•˜์Šค ์ผˆ ํŒŒ์ผ ํ•˜๋‚˜์— ๋ถ™์—ฌ ๋„ฃ๊ณ  ๋‹ค์Œ์˜ ์˜ˆ์ œ Tree์™€ fold ์šฉ ํ•จ์ˆ˜๋“ค์„ ๋„ฃ์–ด๋ณด์ž. tree1 :: Tree Integer tree1 = Branch (Branch (Branch (Leaf 1) (Branch (Leaf 2) (Leaf 3))) (Branch (Leaf 4) (Branch (Leaf 5) (Leaf 6)))) (Branch (Branch (Leaf 7) (Leaf 8)) (Leaf 9)) doubleTree = treeMap (*2) -- doubles each value in tree sumTree = treeFold (+) id -- sum of the leaf values in tree fringeTree = treeFold (++) (: []) -- list of the leaves of tree ์ด ํŒŒ์ผ์„ GHCi๋กœ ๋ถˆ๋Ÿฌ์™€ ์‹คํ–‰ํ•ด ๋ณด์ž. doubleTree tree1 sumTree tree1 fringeTree tree1 ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค map๊ณผ fold ํ•จ์ˆ˜๋Š” ์–ด๋–ค ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋Œ€ํ•ด์„œ๋„ ์ •์˜๋  ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์™€ ํŠธ๋ฆฌ์— ์ ์šฉํ–ˆ๋˜ ์ „๋žต์„ ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฒˆ ๋งˆ์ง€๋ง‰ ์ ˆ์—์„œ๋Š” ๋‹ค์Œ์˜ ์ƒ๋‹นํžˆ ์ด์ƒํ•˜๊ณ  ์ธ์œ„์ ์ธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋Œ€ํ•œ map๊ณผ fold๋ฅผ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๋‹ค. data Weird a b = First a | Second b | Third [(a, b)] | Fourth (Weird a b) ์œ ์ตํ•œ ์—ฐ์Šต์ด ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋” ์ฝ๊ธฐ์— ์•ž์„œ ์ง์ ‘ ์ฝ”๋”ฉํ•ด ๋ณด๊ธฐ๋ฅผ ์ถ”์ฒœํ•œ๋‹ค. ๋ฒ”์šฉ map Weird ํƒ€์ž…์„ ๋‹ค๋ฃฐ ๋•Œ ์ค‘์š”ํ•œ ์ฒซ ๋ฒˆ์งธ ์ฐจ์ด์ ์€ ์ด๊ฒƒ์ด ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋‘ ๊ฐœ ์ทจํ•œ๋‹ค๋Š” ์ ์ด๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— map ํ•จ์ˆ˜๋Š” ์ธ์ž๋ฅผ ๋‘ ๊ฐœ ์ทจํ•ด ํ•˜๋‚˜๋Š” a ํƒ€์ž…์˜ ์›์†Œ์— ์ ์šฉํ•˜๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” b ํƒ€์ž…์˜ ์›์†Œ์— ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ๊ณ ๋ คํ•˜๋ฉด weirdMap์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. weirdMap :: (a -> c) -> (b -> d) -> Weird a b -> Weird c d ๋‹ค์Œ ๋‹จ๊ณ„๋Š” weirdMap์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ํ•ต์‹ฌ์€ map์ด ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด์กดํ•œ๋‹ค๋Š” ๊ฒƒ์œผ๋กœ, ํ•จ์ˆ˜๋Š” ์›๋ž˜์˜ Weird์™€ ๊ฐ™์€ ์ƒ์„ฑ์ž๋ฅผ ์“ฐ๋Š” Weird๋กœ ํ‰๊ฐ€๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ํ•˜๋‚˜์˜ ์ •์˜๊ฐ€ ๊ฐ๊ฐ์˜ ์ƒ์„ฑ์ž๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ด๋“ค ์ƒ์„ฑ์ž๋Š” ํŒจํ„ด์œผ๋กœ์„œ ์‚ฌ์šฉ๋œ๋‹ค. ์ „๊ณผ ๊ฐ™์ด weirdMap์˜ ์ธ์ž ๋ชฉ๋ก์„ ๋ช‡ ๋ฒˆ์ด๊ณ  ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด where ์ ˆ์„ ์“ฐ๊ฒ ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋Œ€์ถฉ ์ด๋ ‡๊ฒŒ ์ƒ๊ฒผ๋‹ค. weirdMap :: (a -> c) -> (b -> d) -> Weird a b -> Weird c d weirdMap fa fb = g where g (First x) = --More to follow g (Second y) = --More to follow g (Third z) = --More to follow g (Fourth w) = --More to follow ์ฒ˜์Œ ๋‘ ๊ฒฝ์šฐ๋Š” Weird ์•ˆ์— a ๋˜๋Š” b ํƒ€์ž…์˜ ๋‹จ์ผ ์›์†Œ๋งŒ ์žˆ์–ด์„œ ๊ฝค ์ง๊ด€์ ์ด๋‹ค. weirdMap :: (a -> c) -> (b -> d) -> Weird a b -> Weird c d weirdMap fa fb = g where g (First x) = First (fa x) g (Second y) = Second (fb y) g (Third z) = --More to follow g (Fourth w) = --More to follow Thrid๋Š” ์›์†Œ ์ž์ฒด๊ฐ€ ์ž๋ฃŒ ๊ตฌ์กฐ(ํŠœํ”Œ)์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌํ•จํ•ด์„œ ์กฐ๊ธˆ ๋ณต์žกํ•˜๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ค‘์ฒฉ๋œ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ๋ˆ„๋ฒผ์•ผ ํ•œ๋‹ค. fa์™€ fb๋ฅผ a์™€ b ํƒ€์ž…์˜ ๋ชจ๋“  ์›์†Œ์— ์ ์šฉํ•˜๋ฉด ๊ฒฐ๊ตญ (map์ด ๋ฐ˜๋“œ์‹œ ๊ตฌ์กฐ๋ฅผ ๋ณด์กดํ•ด์•ผ ํ•˜๋ฏ€๋กœ) ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ [(c, d)]๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋ฉฐ ์ด ๋ฆฌ์ŠคํŠธ๋Š” ์ƒ์„ฑ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ๊ฒƒ์ด๋‹ค. ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ์ ‘๊ทผ๋ฒ•์€ Weird ๋‚ด๋ถ€์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ชผ๊ฐœ์„œ ํŒจํ„ด๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. g (Third []) = Third [] g (Third ((x, y):zs)) = Third ( (fa x, fb y) : ( (\(Third z) -> z) (g (Third zs)) ) ) ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋‹ˆ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ์ „ํ˜•์ ์ธ ์žฌ๊ท€ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ ๋Œ€์ƒ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ์— ์ ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์˜ ๋จธ๋ฆฌ (fa x, fb y)๋ฅผ ์–ป๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฑธ ์–ด๋””์— ์ปจ์‹ฑํ•ด์•ผ ํ• ๊นŒ? g๊ฐ€ Weird ์ธ์ž๋ฅผ ์š”๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ ๊ผฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด Weird๋ฅผ ๋งŒ๋“ค๊ณ  ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ g๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ Weird๋ฅผ ์ค„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ •๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ป์–ด์•ผ ํ•œ๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ ๋žŒ๋‹ค ํ•จ์ˆ˜์˜ ์—ญํ• ์ด๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๊ธฐ๋ณธ ๋ถ„๊ธฐ๋กœ ์ •์˜๋˜์–ด ์žˆ๋‹ค. ์ด ๋ชจ๋“  ์ผ์„ ๋๋‚ด๊ณ  ๋‚˜๋‹ˆ ํ•จ์ˆ˜๊ฐ€ ๊ฝค๋‚˜ ์ง€์ €๋ถ„ํ•ด์กŒ๋‹ค. g์˜ ๋ชจ๋“  ์žฌ๊ท€ ํ˜ธ์ถœ์€ zs๋ฅผ Weird๋กœ ๊ฐ์Œ€ ๊ฒƒ์„ ์š”๊ตฌํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์šฐ๋ฆฌ๊ฐ€ ์ •๋ง ํ•˜๊ณ  ์‹ถ์—ˆ๋˜ ๊ฒƒ์€ (fa x, fb y)์™€ ์ˆ˜์ •๋œ xs๋ฅผ ์ด์šฉํ•ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. ์ด ํ•ด๋ฒ•์˜ ๋ฌธ์ œ๋Š” g๊ฐ€ ๋ฆฌ์ŠคํŠธ ๋จธ๋ฆฌ์—๋Š” ํŒจํ„ด ๋งค์นญ ๋•์— ์ง์ ‘ ์ž‘๋™ํ•˜์ง€๋งŒ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ ๊ผฌ๋ฆฌ์—๋Š” ์ง์ ‘ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ํŒจํ„ด ๋งค์นญ์œผ๋กœ ๋ถ„ํ•ดํ•˜์ง€ ์•Š๊ณ  fa์™€ fb๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๋‚˜์„ ์ˆ˜๋„ ์žˆ๋‹ค(์ ์–ด๋„ g๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ด€๋˜๋Š” ํ•œ). ๊ทธ๋Ÿฐ๋ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์›์†Œ๋ณ„๋กœ ์ง์ ‘ ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋‹ค... g (Third z) = Third ( map (\(x, y) -> (fa x, fb y) ) z) ...์ด ํ›Œ๋ฅญํ•œ ์˜›๋‚  map ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ z ๋‚ด์˜ ๋ชจ๋“  ํŠœํ”Œ์„ ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ˆ˜์ •ํ•œ๋‹ค. ์‚ฌ์‹ค ์ด ์ •์˜์˜ ์ฒซ ๋ฒˆ์งธ ๋ฒ„์ „์€ ๊ทธ๋Ÿด์‹ธํ•ด ๋ณด์ด๋˜(ํ•˜์ง€๋งŒ ์ž˜๋ชป๋œ) Weird ํŒจํ‚น๊ณผ ์–ธ ํŒจํ‚น์„ ๋นผ๋ฉด ๋ฆฌ์ŠคํŠธ map์˜ ์‘์šฉ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ •๊ทœ ๋ฆฌ์ŠคํŠธ์— ๋ฐ”๋กœ ์ž‘๋™ํ•˜๋Š” map์— z๋ฅผ ๋ถ„ํ•ดํ•˜๋Š” ํŒจํ„ด์„ ๋งก๊ฒจ์„œ ํŒจํ‚น๊ณผ ์–ธ ํŒจํ‚น์„ ์ œ๊ฑฐํ–ˆ๋‹ค. g ๋‚ด๋ถ€์˜ map ์ •์˜๋ฅผ ์ „๊ฐœํ•ด์„œ ๊ทธ ์ฐจ์ด๋ฅผ ๋ณด๋‹ค ํ™•์‹คํ•˜๊ฒŒ ๋ณด๋Š” ๊ฒŒ ์œ ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊น”๋”ํ•œ ๋Œ€์•ˆ์ด ์žˆ๋‹ค. g (Third z) = Third [ (fa x, fb y) | (x, y) <- z ] Third ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ–ˆ์œผ๋‹ˆ Fourth ์ •์˜๋งŒ ๋‚จ์•˜๋‹ค. weirdMap :: (a -> c) -> (b -> d) -> Weird a b -> Weird c d weirdMap fa fb = g where g (First x) = First (fa x) g (Second y) = Second (fb y) g (Third z) = Third ( map (\(x, y) -> (fa x, fb y) ) z) g (Fourth w) = --More to follow ์—ฌ๊ธฐ์„œ๋Š” g๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ์ ์šฉํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. weirdMap :: (a -> c) -> (b -> d) -> Weird a b -> Weird c d weirdMap fa fb = g where g (First x) = First (fa x) g (Second y) = Second (fb y) g (Third z) = Third ( map (\(x, y) -> (fa x, fb y) ) z) g (Fourth w) = Fourth (g w) ์ผ๋ฐ˜ํ™”๋œ fold map์€ ๊ฐ ์œ ํ˜•์— ํ•จ์ˆ˜ ํ•˜๋‚˜์”ฉ์„ ์ธ์ž๋กœ ๊ธฐ์ž…ํ•˜์—ฌ ์ •์˜ํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ fold๋Š” ๊ทธ๊ฑธ๋กœ ์ถฉ๋ถ„ํ•˜์ง€ ์•Š๋‹ค. fold์—์„œ๋Š” ๋ชจ๋“  ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์œ„ํ•œ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ ์ƒ์„ฑ์ž๋Š” []์™€ (:)๋‹ค. foldr ํ•จ์ˆ˜์˜ acc ์ธ์ž๋Š” (:) ์ƒ์„ฑ์ž์— ๋Œ€์‘ํ•œ๋‹ค. Weird ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ์ƒ์„ฑ์ž๊ฐ€ ๋„ค ๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ๊ฐ์˜ ์ƒ์„ฑ์ž์— ๋ช…์‹œ๋œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋„ค ๊ฐœ์˜ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๋‹ค์Œ์œผ๋กœ Weird a b ํƒ€์ž…์˜ ์ธ์ž๊ฐ€ ์žˆ๋Š”๋ฐ ์šฐ๋ฆฌ๋Š” ์ด ์ธ์ž๊ฐ€ foldr ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์ž„์˜์˜ ํƒ€์ž…์œผ๋กœ ํ‰๊ฐ€๋˜๊ธธ ์›ํ•œ๋‹ค. ๋˜ํ•œ ์šฐ๋ฆฌ๊ฐ€ weirdFold์— ์ „๋‹ฌํ•  ๋„ค ํ•จ์ˆ˜๋Š” ๊ฐ™์€ ํƒ€์ž…์œผ๋กœ ํ‰๊ฐ€๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ์™€ ์ •์˜๋ฅผ ์–ผ์ถ” ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. weirdFold :: (something1 -> c) -> (something2 -> c) -> (something3 -> c) -> (something4 -> c) -> Weird a b -> c weirdFold f1 f2 f3 f4 = g where g (First x) = --Something of type c here g (Second y) = --Something of type c here g (Third z) = --Something of type c here g (Fourth w) = --Something of type c here ์ด์ œ something1, something2, something3, something4๊ฐ€ ์–ด๋–ค ํƒ€์ž…์ด์–ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋ฐํ˜€๋‚ด์•ผ ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์ด ์ธ์ž๋กœ ์ทจํ•˜๋Š” ์›์†Œ๋“ค์€ ์ƒ์„ฑ์ž ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ์— ๋ช…์‹œ๋œ ํƒ€์ž…์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์—, ์ƒ์„ฑ์ž๋ฅผ ๋ถ„์„ํ•˜๋ฉด ๋„์›€์ด ๋  ๊ฒƒ์ด๋‹ค. ์ด๋ฒˆ์—๋„ ์ฒ˜์Œ ๋‘ ํ•จ์ˆ˜์˜ ํƒ€์ž…๊ณผ ์ •์˜๋Š” ์‰ฝ๋‹ค. ์„ธ ๋ฒˆ์งธ๋„ ๊ทธ๋‹ค์ง€ ์–ด๋ ต์ง„ ์•Š์€๋ฐ (a, b)์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ folding ํ•  ๋•Œ ํŠœํ”Œ์€ ๋‹จ์ˆœํ•œ ํƒ€์ž…์ด๋‚˜ ๋‹ค๋ฅผ ๋ฐ” ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (map ์˜ˆ์ œ์™€ ๋‹ฌ๋ฆฌ ๊ทธ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋Š” ์ง€๊ธˆ์€ ๊ณ ๋ ค ๋Œ€์ƒ์ด ์•„๋‹ˆ๋‹ค) ํ•˜์ง€๋งŒ ๋„ค ๋ฒˆ์งธ ์ƒ์„ฑ์ž๋Š” ์žฌ๊ท€์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์กฐ์‹ฌํ•ด์•ผ ํ•˜๋‚˜. weirdMap์˜ ๊ฒฝ์šฐ์ฒ˜๋Ÿผ g ํ•จ์ˆ˜๋Š” ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ตœ์ข… ์ •์˜๊ฐ€ ๋‚˜์˜จ๋‹ค. weirdFold :: (a -> c) -> (b -> c) -> ([(a, b)] -> c) -> (c -> c) -> Weird a b -> c weirdFold f1 f2 f3 f4 = g where g (First x) = f1 x g (Second y) = f2 y g (Third z) = f3 z g (Fourth w) = f4 (g w) ๋…ธํŠธ weirdFold์—์„œ ์•„์ฃผ ๋ณต์žกํ•œ ํ‘œํ˜„์‹์„ ์˜ˆ์ƒํ–ˆ๋Š”๋ฐ ํ•ด๋‹ต์ด ๊ณง๋ฐ”๋กœ ๋‚˜์™€์„œ ๋†€๋ž๋‹ค๋ฉด, ์šฐ๋ฆฌ๋ฅผ ํ˜ผ๋ž€์Šค๋Ÿฝ๊ฒŒ ํ•˜๋Š” ๋ฆฌ์ŠคํŠธ์˜ ํŠน๋ณ„ํ•œ ๊ฐ๊ด„ํ˜ธ ๊ตฌ๋ฌธ์„ ์“ฐ์ง€ ์•Š๊ณ  ์ผ๋ฐ˜์ ์ธ foldr์„ ์ด๋Ÿฐ ์‹์œผ๋กœ ์จ์„œ ํ•œ ๋ฒˆ ๋ณด์ž. -- List a is [a], Cons is (:) and Nil is [] data List a = Cons a (List a) | Nil listFoldr :: (a -> b -> b) -> (b) -> List a -> b listFoldr fCons fNil = g where g (Cons x xs) = fCons x (g xs) g Nil = fNil ์ด์ œ ๋ณ‘๋ ฌ์„ฑ์ด ๋” ์ž˜ ๋ณด์ธ๋‹ค. ์•„์ง๋„ ๋ณต์žกํ•œ ๋ถ€๋ถ„์„ ๋ณด๋ฉด Cons (์ฆ‰ (:))๊ฐ€ ์ธ์ž๋ฅผ ๋‘ ๊ฐœ ์ทจํ•˜๊ณ  ๋”ฐ๋ผ์„œ fCons๋„ ์ธ์ž๋ฅผ ๋‘ ๊ฐœ ์ทจํ•œ๋‹ค. ๋˜ํ•œ Cons์˜ ๊ตฌ์กฐ๊ฐ€ ์žฌ๊ท€์ ์ด์–ด์„œ g์— ๋Œ€ํ•œ ์žฌ๊ท€ ํ˜ธ์ถœ์„ ์š”๊ตฌํ•˜๊ณ  fNil์€ ์‚ฌ์‹ค์€ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ธ์ˆ˜๋ฅผ ์ทจํ•˜์ง€ ์•Š๋Š”๋‹ค. ์žฌ๊ท€์  ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋Œ€ํ•œ fold fold์— ํ•œํ•ด์„œ Weird๋Š” ๋‹ค๋ฃจ๊ธฐ์— ๊ฝค ์ข‹์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด์—ˆ๋‹ค. ์žฌ๊ท€์  ์ƒ์„ฑ์ž๋Š” ํ•˜๋‚˜๋ฟ์ด๊ณ  ๋‹ค๋ฅธ ๊ตฌ์กฐ์— ์ค‘์ฒฉ๋˜์ง€๋„ ์•Š๋Š”๋‹ค. ์—ฌ๊ธฐ์— ์ง„์งœ๋กœ ๋ณต์žกํ•œ ๋‹ค์„ฏ ๋ฒˆ์งธ ์ƒ์„ฑ์ž๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? Fifth [Weird a b] a (Weird a a, Maybe (Weird a b)) ์ด๋Ÿฐ ์งˆ๋ฌธ์€ ์žˆ์„ ๋ฒ•ํ•˜๋ฉด์„œ๋„ ๊ตํ™œํ•˜๊ธฐ๋„ ํ•˜๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ ๊ทœ์น™๋“ค์ด ์ ์šฉ๋œ๋‹ค. fold์— ์ „๋‹ฌ๋˜๋Š” ํ•จ์ˆ˜๋Š” ๋Œ€์‘ํ•˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ๊ฐ™์€ ๊ฐœ์ˆ˜์˜ ์ธ์ž๋ฅผ ๊ฐ€์ง„๋‹ค. ๊ทธ๋Ÿฐ ํ•จ์ˆ˜์˜ ์ธ์ž๋“ค์˜ ํƒ€์ž…์€ ์ƒ์„ฑ์ž์˜ ์ธ์ˆ˜๋“ค์˜ ํƒ€์ž…๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค. ์ƒ์„ฑ์ž๊ฐ€ ์žฌ๊ท€์ ์ผ ๋•Œ(์ฆ‰ ์ž์‹ ์˜ ํƒ€์ž…๊ณผ ๊ฐ™์€ ์ธ์ž๋ฅผ ์ทจํ•  ๋•Œ)๋Š” ์˜ˆ์™ธ๋‹ค. ์ƒ์„ฑ์ž๊ฐ€ ์žฌ๊ท€์ ์ด๋ฉด, ๊ทธ ์ƒ์„ฑ์ž์˜ ์žฌ๊ท€์  ์ธ์ž๋Š” fold๋ฅผ ํ†ตํ•ด ํ‰๊ฐ€๋  ํƒ€์ž…์˜ ์ธ์ž์™€ ์ผ์น˜ํ•œ๋‹ค. 1 ์ƒ์„ฑ์ž๊ฐ€ ์žฌ๊ท€์ ์ด๋ฉด, ์žฌ๊ท€์  ์ƒ์„ฑ์ž ์ธ์ž๋“ค์— ์™„์ „ํ•œ fold ํ•จ์ˆ˜๋ฅผ (์žฌ๊ท€์ ์œผ๋กœ) ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์–ด๋–ค ์žฌ๊ท€์  ์›์†Œ๊ฐ€ ๋‹ค๋ฅธ ์ž๋ฃŒ ๊ตฌ์กฐ ๋‚ด๋ถ€์— ์žˆ๋‹ค๋ฉด, ๊ทธ ์ž๋ฃŒ ๊ตฌ์กฐ์— ์•Œ๋งž์€ map ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด fold ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ f5์˜ ํƒ€์ž…์€ f5 :: [c] -> a -> (Weird a a, Maybe c) -> c ๊ทธ๋ฆฌ๊ณ  Fifth์˜ ํƒ€์ž…์€ Fifth :: [Weird a b] -> a -> (Weird a a, Maybe (Weird a b)) -> Weird a b Fifth ์ƒ์„ฑ์ž์— ๋Œ€ํ•œ g์˜ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. g (Fifth list x (waa, mc)) = f5 (map g list) x (waa, maybeMap g mc) where maybeMap f Nothing = Nothing maybeMap f (Just w) = Just (f w) ๊ทธ๋Ÿฐ๋ฐ Weird a a ๋ถ€๋ถ„์—์„œ๋Š” ์ด์ƒํ•œ ์ผ์ด ์ „ํ˜€ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค. g๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ๋œ ๊ฑธ๊นŒ? ์ด๊ฑด ์žฌ๊ท€์ด์ง€ ์•Š์€๊ฐ€? ์‚ฌ์‹ค ์•„๋‹ˆ๋‹ค. Weird a a์™€ Weird a b๋Š” ๋‹ค๋ฅธ ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— ์ง„์งœ ์žฌ๊ท€๊ฐ€ ์•„๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด f2๊ฐ€ 'b' ํƒ€์ž…์„ ์˜ˆ์ƒํ•˜๋Š” ์ž๋ฆฌ์— 'a' ํƒ€์ž…์„ ๋†“๊ณ  ์ž‘์—…ํ•  ๊ฑฐ๋ผ๋Š” ๋ณด์žฅ์€ ์—†๋‹ค. ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋Ÿด ์ˆ˜๋„ ์žˆ์ง€๋งŒ ํ•ญ์ƒ ๊ทธ๋Ÿฐ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. maybeMap์˜ ์ •์˜๋„ ๋ณด์ž. ๋‹ค์Œ ์ด์œ ๋กœ ์ธํ•ด ์ด๊ฒƒ ์—ญ์‹œ map ํ•จ์ˆ˜๋‹ค. ๊ตฌ์กฐ๋ฅผ ๋ณด์กดํ•œ๋‹ค. ํƒ€์ž…๋งŒ์ด ๋ณ€ํ•œ๋‹ค. ๋ฉ‹์ ธ ๋ณด์ด๋Š” ๋ง ์—ฌ๊ธฐ์„œ ์ •์˜ํ•œ fold๋Š” ๋ฐ˜(ๅ) ์‚ฌ์ƒ(= catamorphism)์˜ ํ•œ ์˜ˆ๋‹ค. ๋ฐ˜์‚ฌ์ƒ์€ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ๋‹จ์ผ ๊ฐ’์œผ๋กœ ๋ˆŒ๋Ÿฌ ์ ‘์–ด๋ฒ„๋ฆฌ๋Š” ์ผ๋ฐ˜์ ์ธ ์ˆ˜๋‹จ์ด๋‹ค. ๋ฐ˜์‚ฌ์ƒ๊ณผ ๊ทธ์— ๊ด€๋ จ๋œ ์žฌ๊ท€ ๊ฐœ๋…์— ๋Œ€ํ•œ ์‹ฌ์˜คํ•œ ์ด๋ก ์ด ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ ์ด๋ก ์„ ํŒŒ๊ณ ๋“ค์ง€ ์•Š๋Š”๋‹ค. ์šฐ๋ฆฌ์˜ ์ฃผ ๋ชฉํ‘œ๋Š” ํƒ„ํƒ„ํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ํ•˜์Šค์ผˆ์—์„œ ์ž๋ฃŒ ๊ตฌ์กฐ ์กฐ์ž‘์˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์—ฐ์Šตํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋…ธํŠธ folding์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋˜ ๋‹ค๋ฅธ fold์˜ ๊ฒฐ๊ณผ๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ์ด๋Ÿฐ ์ข…๋ฅ˜์˜ ์žฌ๊ท€๋Š” ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠธ๋ฆฌ ๊ฐ™์€ ์ž๋ฃŒ ๊ตฌ์กฐ์˜ fold์—๊ฒŒ "๋ˆ„์ ๋˜๋Š”" ๊ธฐ๋Šฅ์„ ๋ถ€์—ฌํ•œ๋‹ค. โ†ฉ 6 ํด๋ž˜์Šค์™€ ํƒ€์ž… ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Classes_and_types ํด๋ž˜์Šค์™€ ์ธ์Šคํ„ด์Šค ํŒŒ์ƒ(deriving) ํด๋ž˜์Šค ์ƒ์† ํ‘œ์ค€ ํด๋ž˜์Šค ํƒ€์ž… ํ•œ์ • ๋‹ค๋ฅธ ์‚ฌ์šฉ๋ฒ• ๊ตฌ์ฒด์ ์ธ ์˜ˆ์ œ ์กฐ์–ธ ํ•œ ๋งˆ๋”” ๋…ธํŠธ ํƒ€์ž…์˜ ๊ธฐ์ดˆ 2์—์„œ ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ์ˆซ์žํ˜•์— ํ™œ์šฉ๋˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ์„œ ๊ฐ„๋‹จํžˆ ์†Œ๊ฐœํ–ˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ํด๋ž˜์Šค์—๋Š” ๋‹ค๋ฅธ ์“ฐ์ž„์ƒˆ๊ฐ€ ๋งŽ์ด ์žˆ๋‹ค. ๊ฐ„๋žตํžˆ ๋งํ•˜๋ฉด ํƒ€์ž… ํด๋ž˜์Šค์˜ ํ•ต์‹ฌ์€ ํŠน์ • ํƒ€์ž…์˜ ๊ฐ’๋“ค์— ๋Œ€ํ•ด ํŠน์ • ์ž‘์—…๋“ค์ด ๊ฐ€๋Šฅํ•จ์„ ๋ณด์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์–ด๋–ค ํƒ€์ž…์ด Fractional ํด๋ž˜์Šค์— ์†ํ•œ๋‹ค๋Š” ๊ฒƒ(์ „๋ฌธ ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ž๋ฉด, ์ธ์Šคํ„ด์Šคํ™”ํ•œ๋‹ค๋Š” ๊ฒƒ)์„ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด, ์šฐ๋ฆฌ๋Š” ๊ทธ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋‚˜๋ˆ„๊ธฐ๋ฅผ ์‹ค์ œ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์žฅ๋ฐ›๋Š”๋‹ค. ํด๋ž˜์Šค์™€ ์ธ์Šคํ„ด์Šค ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํƒ€์ž… ํด๋ž˜์Šค๊ฐ€ ์‹œ๊ทธ๋„ˆ์ฒ˜์— ์–ด๋–ป๊ฒŒ ๋‚˜ํƒ€๋‚˜๋Š”์ง€๋ฅผ ๋ด์™”๋‹ค. (==) :: (Eq a) => a -> a -> Bool ๊ด€์ ์„ ๋ฐ”๊ฟ”๋ณผ ์‹œ๊ฐ„์ด๋‹ค. ๋จผ์ € Prelude์˜ Eq ํด๋ž˜์Šค์˜ ์ •์˜๋ฅผ ๊ฐ€์ ธ์™€๋ณด๊ฒ ๋‹ค. class Eq a where (==), (/=) :: a -> a -> Bool -- Minimal complete definition: -- (==) or (/=) x /= y = not (x == y) x == y = not (x /= y) ์ด ์ •์˜๋Š” a ํƒ€์ž…์ด Eq ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋˜๋ ค๋ฉด, ํ•จ์ˆ˜ (==)์™€ (/=) (ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋“ค)์„ ์ง€์›ํ•ด์•ผ ํ•˜๊ณ  ์ด๋“ค์˜ ํƒ€์ž…์€ a -> a -> Bool ์ด์–ด์•ผ ํ•œ๋‹ค๊ณ  ์„ ์–ธํ•œ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ, ์ด ํด๋ž˜์Šค๋Š” (==)์™€ (/=)์˜ ๊ธฐ๋ณธ ์ •์˜๋ฅผ ์ œ๊ณตํ•˜๋Š”๋ฐ, ์ด๊ฒƒ๋“ค์€ ์„œ๋กœ๋ฅผ ์ด์šฉํ•ด ์ •์˜๋œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ Eq์˜ ํ•œ ํƒ€์ž…์— ๋‘ ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‘ ์ •์˜ํ•  ํ•„์š”๊ฐ€ ์—†๊ฒŒ ๋œ๋‹ค. ํ•˜๋‚˜๋งŒ ์žˆ์œผ๋ฉด ๋‹ค๋ฅธ ๊ฒƒ์€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค. ํ•œ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋ฉด, ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํƒ€์ž…๋“ค์„ ์ด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์‹œ์—์„œ๋Š” ์ธ์Šคํ„ด์Šค ์„ ์–ธ์„ ํ†ตํ•ด์„œ ํ•œ ๋Œ€์ˆ˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ Eq์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค์—ˆ๋‹ค. data Foo = Foo {x :: Integer, str :: String} instance Eq Foo where (Foo x1 str1) == (Foo x2 str2) = (x1 == x2) && (str1 == str2) ์ด์ œ Foo ๊ฐ’์— (==)์™€ (/=)๋ฅผ ํ”ํžˆ ์“ฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. *Main> Foo 3 "orange" == Foo 6 "apple" False *Main> Foo 3 "orange" /= Foo 6 "apple" True ์ค‘์š”ํ•œ ์‚ฌํ•ญ์„ ๋ช‡ ๊ฐœ ์งš์–ด๋ณด์ž. Eq ํด๋ž˜์Šค๋Š” ํ‘œ์ค€ Prelude์— ์ •์˜๋˜์–ด ์žˆ๋‹ค. ์ด ์ฝ”๋“œ๋Š” Foo ํƒ€์ž…์„ ์ •์˜ํ•˜๊ณ  Eq์˜ ์ธ์Šคํ„ด์Šค๋กœ ์„ ์–ธํ•œ๋‹ค. ์„ธ ๊ฐœ์˜ ์ •์˜(ํด๋ž˜์Šค, ๋ฐ์ดํ„ฐ ํƒ€์ž…, ์ธ์Šคํ„ด์Šค)๋Š” ์™„์ „ํžˆ ๋ณ„๊ฐœ์ด๋ฉฐ ์ด๊ฒƒ๋“ค์„ ์–ด๋–ป๊ฒŒ ๋ฌถ๋Š”๊ฐ€์— ๊ด€ํ•œ ๊ทœ์น™์€ ์—†๋‹ค. ์ด๊ฒƒ์€ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค. Bar๋ผ๋Š” ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด Integer ํƒ€์ž…์„ ์ด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ ์„ ์–ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํด๋ž˜์Šค๋Š” ํƒ€์ž…์ด ์•„๋‹ˆ๋ผ ํƒ€์ž…์˜ ๋ฒ”์ฃผ(category)๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋Š” ๊ฐ’์ด ์•„๋‹ˆ๋ผ ํƒ€์ž…์ด๋‹ค.2 Foo๋ฅผ ์œ„ํ•œ (==) ์ •์˜๋Š” Foo์˜ ํ•„๋“œ(์ฆ‰ Integer์™€ String)์˜ ๊ฐ’ ์—ญ์‹œ Eq์˜ ๊ตฌ์„ฑ์›์ด๋ผ๋Š” ์‚ฌ์‹ค์— ๊ธฐ๋ฐ˜ํ•œ๋‹ค. ์‚ฌ์‹ค ํ•˜์Šค์ผˆ์˜ ๊ฑฐ์˜ ๋ชจ๋“  ํƒ€์ž…์€ Eq์˜ ๊ตฌ์„ฑ์›์ด๋‹ค. (๊ฐ€์žฅ ์ฃผ๋ชฉํ•  ๋งŒํ•œ ์˜ˆ์™ธ๋Š” ํ•จ์ˆ˜๋‹ค.) type ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ •์˜ํ•œ ํƒ€์ž… ๋™์˜์–ด(type synonym)๋Š” ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค. ํŒŒ์ƒ(deriving) ๊ฐ’ ์‚ฌ์ด์˜ ํ•ญ๋“ฑ ๋น„๊ต๋Š” ํ”ํ•œ ์ผ์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ๋ถ„์ด ์‹ค์ œ๋กœ ์ž‘์„ฑํ•  ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์ž๋ฃŒํ˜•์ด Eq์˜ ๊ตฌ์„ฑ์›์ด์–ด์•ผ ํ•œ๋‹ค. ๋งŽ์€ ์ž๋ฃŒํ˜•์ด Ord๋‚˜ Show ๊ฐ™์€ ๋‹ค๋ฅธ Prelude ํด๋ž˜์Šค์˜ ๊ตฌ์„ฑ์›์ด๋‹ค. ๋ชจ๋“  ์ƒˆ๋กœ์šด ํƒ€์ž…์— ๋น„์Šทํ•œ ์ฝ”๋“œ๋ฅผ ์ฐ์–ด๋‚ด๋Š” ๊ฒƒ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ํ•˜์Šค์ผˆ์€ deriving ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด "๋ป”ํ•œ" ์ธ์Šคํ„ด์Šค ์ •์˜๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์„ ์–ธํ•˜๋Š” ์ˆ˜๋‹จ์„ ์ง€์›ํ•œ๋‹ค. Foo๋Š” ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. data Foo = Foo {x :: Integer, str :: String} deriving (Eq, Ord, Show) ์ด๋Ÿฌ๋ฉด Foo๋Š” Eq์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋˜์–ด ์šฐ๋ฆฌ๊ฐ€ ๋ฐฉ๊ธˆ ์ž‘์„ฑํ–ˆ๋˜ ==์˜ ์ •์˜์™€ ์™„์ „ํžˆ ๊ฐ™์€ ๊ฒƒ์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๋ฉฐ, ๋˜ํ•œ Foo๋Š” ์ ์ ˆํ•œ ๋ฐฉ์‹์œผ๋กœ Ord์™€ Show์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋œ๋‹ค. deriving๋Š” ์ผ๋ถ€ ๋‚ด์žฅ ํด๋ž˜์Šค๋“ค์— ํ•œ์ •ํ•ด์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„์ฃผ ๊ฐ„๋žตํžˆ ์„œ์ˆ ํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค. Eq ํ•ญ๋“ฑ ๊ฒ€์‚ฌ ์—ฐ์‚ฐ์ž ==์™€ /= Ord ๋น„๊ต ์—ฐ์‚ฐ์ž < <= > >= ๊ทธ๋ฆฌ๊ณ  min, max, ๋งˆ์ง€๋ง‰์œผ๋กœ compare Enum ์—ด๊ฑฐํ˜• ์ „์šฉ. [Blue .. Green] ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Bounded ์—ด๊ฑฐํ˜•์„ ์œ„ํ•œ ๊ฒƒ์ด์ง€๋งŒ ์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜์ธ ํƒ€์ž…์—๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. minBound์™€ maxBound๋ฅผ ๊ทธ ํƒ€์ž…์ด ์ทจํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜ํ•œ ๊ฐ’๊ณผ ์ƒํ•œ ๊ฐ’์œผ๋กœ์„œ ์ œ๊ณตํ•œ๋‹ค. Show show ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๊ฐ’์„ ๋ฌธ์ž์—ด๋ฃŒ ๋ณ€ํ™˜ํ•œ๋‹ค. ๊ธฐํƒ€ ๊ด€๋ จ๋œ ํ•จ์ˆ˜๋“ค๋„ ์ •์˜ํ•œ๋‹ค. Read read ํ•จ์ˆ˜์™€ ๊ธฐํƒ€ ๊ด€๋ จ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ํ•œ๋‹ค. read๋Š” ๋ฌธ์ž์—ด์„ ๊ทธ ํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๊ด€๋ จ ํ•จ์ˆ˜๋“ค์ด ํŒŒ์ƒ๋˜๋Š” ์ •ํ™•ํ•œ ๊ทœ์น™์€ language report์— ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋Œ€์ฒด๋กœ "์˜ฌ๋ฐ”๋ฅธ ๊ตฌํ˜„"์„ ํ•  ๊ฑฐ๋ผ๊ณ  ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋‚ด์˜ ์›์†Œ ํƒ€์ž…๋„ ํŒŒ์ƒํ•˜๋ ค๋Š” ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—ฌ์•ผ ํ•œ๋‹ค. ๋ฏธ๋ฆฌ ์ •์˜๋œ ๋ช‡ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ํŠน๋ณ„ํ•œ "๋งˆ๋ฒ•" ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํ•˜์Šค์ผˆ์˜ "๋‚ด์žฅ๋œ ๊ฒƒ์ด๋ผ๊ณ  ํŠน๋ณ„ํ•˜์ง€ ์•Š๋‹ค"๋ผ๋Š” ์ผ๋ฐ˜์ ์ธ ์›์น™์„ ์œ„๋ฐฐํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์† ๊ณ ์ƒ์„ ๋งŽ์ด ๋œ์–ด์ฃผ๊ธฐ๋„ ํ•˜๊ณ  ์ธ์Šคํ„ด์Šค ํŒŒ์ƒ์€ ์šฐ๋ฆฌ๊ฐ€ ๋ญ”๊ฐ€๋ฅผ ์ž˜๋ชป ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์„ ๋ง‰์•„์ค€๋‹ค. (์˜ˆ: x == y์™€ y == x๊ฐ€ ๊ฐ™์ง€ ์•Š์€ Eq์˜ ์ธ์Šคํ„ด์Šค๋Š” ์™„์ „ํžˆ ์ž˜๋ชป๋œ ๊ฒƒ์ด๋‹ค) 3 ํด๋ž˜์Šค ์ƒ์† ํด๋ž˜์Šค๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ Prelude์˜ Ord ์ •์˜์˜ ํ•ต์‹ฌ๋ถ€๋‹ค. class (Eq a) => Ord a where compare :: a -> a -> Ordering (<), (<=), (>=), (>) :: a -> a -> Bool max, min :: a -> a -> a ์‹ค์ œ ์ •์˜๋Š” ์ด๊ฒƒ๋ณด๋‹ค ๊ธธ๊ณ  ๋Œ€๋ถ€๋ถ„์˜ ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ ์ •์˜๋ฅผ ํฌํ•จํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•ต์‹ฌ์€ Ord๊ฐ€ Eq๋ฅผ ์ƒ์†ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„์˜ => ํ‘œ๊ธฐ๊ฐ€ ๋ฐ”๋กœ ์ƒ์†์„ ์˜๋ฏธํ•˜๋ฉฐ ์ด๋Š” ํด๋ž˜์Šค๊ฐ€ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ฐฉ์‹์„ ๋ฐ˜์˜ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋œปํ•˜๋Š” ๋ฐ”๋Š” Ord์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋  ํƒ€์ž…์€ Eq์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋ฉฐ ๋”ฐ๋ผ์„œ == ์—ฐ์‚ฐ๊ณผ /= ์—ฐ์‚ฐ์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 4 ํด๋ž˜์Šค๋Š” ์—ฌ๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•  ์ˆ˜ ์žˆ๋‹ค. =>์— ์•ž์„œ ๋ถ€๋ชจ๊ฐ€ ๋  ํด๋ž˜์Šค๋“ค์„ ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ๋ฉด ๋œ๋‹ค. Prelude์˜ ๋˜ ๋‹ค๋ฅธ ์ผ๋ถ€๋ฅผ ์ธ์šฉํ•ด ๋ณด์ž. class (Num a, Ord a) => Real a where -- | the rational equivalent of its real argument with full precision toRational :: a -> Rational ํ‘œ์ค€ ํด๋ž˜์Šค Haskell Report์—์„œ ๊ฐ€์ ธ์˜จ ์ด ๋‹ค์ด์–ด๊ทธ๋žจ์€ ํ‘œ์ค€ Prelude์˜ ํด๋ž˜์Šค๋“ค๊ณผ ํƒ€์ž…๋“ค ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. ๊ตต์€ ์ด๋ฆ„์€ ํด๋ž˜์Šค์ด๊ณ  ๋ณดํ†ต ๊ธ€์ž๋Š” ๊ฐ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ธ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. (->)๋Š” ํ•จ์ˆ˜๋ฅผ, []๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ํด๋ž˜์Šค๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ํ™”์‚ดํ‘œ๋Š” ์ƒ์† ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ ํ™”์‚ด์ด‰์ด ์žˆ๋Š” ์ชฝ์ด ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค๋‹ค. ๊ธฐ๋ณธ ํƒ€์ž… ํด๋ž˜์Šค๋“ค์˜ ๊ณ„์ธต๋„ ํƒ€์ž… ํ•œ์ • ์ฑ…์—์„œ ํด๋ž˜์Šค๊ฐ€ ๋งจ ์ฒ˜์Œ ๋“ฑ์žฅํ•œ ์˜ˆ์ œ๋กœ ๋Œ์•„๊ฐ€ ๋ณด์ž. (+) :: (Num a) => a -> a -> a (Num a) =>๋Š” ํƒ€์ž… ํ•œ์ •(type constraint)์œผ๋กœ์„œ, a ํƒ€์ž…์„ Num ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ ์ œํ•œํ•œ๋‹ค. ์‚ฌ์‹ค (+)๋Š” (*), (-)์™€ ๋”๋ถˆ์–ด Num์˜ ๋ฉ”์„œ๋“œ์ด๋‹ค. ๋‹จ (/)๋Š” ์•„๋‹ˆ๋‹ค. ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜์— ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ œ์•ฝ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. foo :: (Num a, Show a, Show b) => a -> a -> b -> String foo x y t = show x ++ " plus " ++ show y ++ " is " ++ show (x+y) ++ ". " ++ show t ์—ฌ๊ธฐ์„œ ์ธ์ˆ˜ x์™€ y๋Š” ๊ฐ™์€ ํƒ€์ž…์ด์–ด์•ผ ํ•˜๋ฉฐ ๊ทธ ํƒ€์ž…์€ Num๊ณผ Show์˜ ์ธ์Šคํ„ด์Šค์—ฌ์•ผ ํ•œ๋‹ค. ๋”์šฑ์ด ๋งˆ์ง€๋ง‰ ์ธ์ˆ˜์ธ t๋Š” (์•„๋งˆ a์™€ ๋‹ค๋ฅผ) ์–ด๋–ค ํƒ€์ž…์ธ๋ฐ, Show์˜ ์ธ์Šคํ„ด์Šค์—ฌ์•ผ ํ•œ๋‹ค. ์ด ์˜ˆ์‹œ์—์„œ๋Š” ๊ทธ ์ œ์•ฝ์ด ์ •์˜์— ์ด์šฉํ•œ ํ•จ์ˆ˜๋“ค(์ด ๊ฒฝ์šฐ (+)์™€ show)๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ ์ •์˜๋˜๋Š” ํ•จ์ˆ˜๋กœ ์–ด๋–ป๊ฒŒ ํ™•์‚ฐ๋˜๋Š” ์ง€๋„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ์šฉ๋ฒ• ํƒ€์ž… ์ œํ•œ์€ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฟ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์ƒํ™ฉ์—๋„ ๋„์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค. instance ์„ ์–ธ (๋ณดํ†ต ๋งค๊ฐœํ™” ํƒ€์ž…๊ณผ ํ•จ๊ป˜ ์“ฐ์ธ๋‹ค) class ์„ ์–ธ (ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ํƒ€์ž… ๋ณ€์ˆ˜ ์™ธ์— ์–ด๋–ค ํƒ€์ž… ๋ณ€์ˆ˜์—๋„ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ์‹์œผ๋กœ ๋ฉ”์„œ๋“œ ์‹œ๊ทธ๋„ˆ์ฒ˜์— ์ œ์•ฝ์„ ๊ฑธ ์ˆ˜ ์žˆ๋‹ค 5) data ์„ ์–ธ 6. ์ด๋•Œ๋Š” ์ƒ์„ฑ์ž ์‹œ๊ทธ๋„ˆ์ฒ˜์— ๋Œ€ํ•œ ์ œ์•ฝ์œผ๋กœ ์ž‘์šฉํ•œ๋‹ค. ์ž ๊น data ์„ ์–ธ์—์„œ์˜ ํƒ€์ž… ํ•œ์ •์€ ๋ณด๊ธฐ๋ณด๋‹จ ๊ทธ๋‹ค์ง€ ์œ ์šฉํ•˜์ง€ ์•Š๋‹ค. data (Num a) => Foo a = F1 a | F2 a String Foo๋Š” ์ƒ์„ฑ์ž๊ฐ€ ๋‘ ๊ฐœ์ธ ํƒ€์ž…์ด๋‹ค. ๋‘˜ ๋‹ค a ํƒ€์ž…์˜ ์ธ์ž๋ฅผ ์ทจํ•˜๋Š”๋ฐ ์ด ์ธ์ž๋Š” ๋ฐ˜๋“œ์‹œ Num์ด์–ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ (Num a) =>๋ผ๋Š” ์ œํ•œ์€ F1๊ณผ F2 ์ƒ์„ฑ์ž์—๋งŒ ์˜ํ–ฅ์„ ์ฃผ๋ฉฐ Foo๋ฅผ ์ˆ˜๋ฐ˜ํ•˜๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜์—์„œ๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ ์˜ˆ์‹œ์—์„œ fooSquared :: (Num a) => Foo a -> Foo a fooSquared (F1 x) = F1 (x * x) fooSquared (F2 x s) = F2 (x * x) s ์ƒ์„ฑ์ž๋Š” a๊ฐ€ Num ํด๋ž˜์Šค์˜ ํƒ€์ž…์ด๋ผ๊ณ  ๋ณด์žฅํ•˜์ง€๋งŒ fooSquared์˜ ์‹œ๊ทธ๋„ˆ์ฒ˜์—์„œ ๊ทธ ํ•œ์ •์„ ๋ณต์ œํ•  ์ˆ˜๋ฐ–์— ์—†๋‹ค. 7 ๊ตฌ์ฒด์ ์ธ ์˜ˆ์ œ ํƒ€์ž…, ํด๋ž˜์Šค, ํ•œ์ •(constraint)์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ๋” ์ž˜ ๋ณด๊ธฐ ์œ„ํ•ด ์•„์ฃผ ๊ฐ„๋‹จํ•˜์ง€๋งŒ ๋‹ค์†Œ ์ธ์œ„์ ์ธ ์˜ˆ์ œ๋ฅผ ๋ณด์ž. ์šฐ๋ฆฌ๋Š” Located ํด๋ž˜์Šค, Located๋ฅผ ์ƒ์†ํ•˜๋Š” Movable ํด๋ž˜์Šค, Movable ํ•œ์ •์ด ๋ถ™์œผ๋ฉฐ ๋ถ€๋ชจ ํด๋ž˜์Šค์ธ Located์˜ ๋ฉ”์„œ๋“œ๋“ค์„ ์ด์šฉํ•ด ๊ตฌํ˜„๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๊ฒƒ์ด๋‹ค. -- Location, in two dimensions. class Located a where getLocation :: a -> (Int, Int) class (Located a) => Movable a where setLocation :: (Int, Int) -> a -> a -- An example type, with accompanying instances. data NamedPoint = NamedPoint { pointName :: String , pointX :: Int , pointY :: Int } deriving (Show) instance Located NamedPoint where getLocation p = (pointX p, pointY p) instance Movable NamedPoint where setLocation (x, y) p = p { pointX = x, pointY = y } -- Moves a value of a Movable type by the specified displacement. -- This works for any movable, including NamedPoint. move :: (Movable a) => (Int, Int) -> a -> a move (dx, dy) p = setLocation (x + dx, y + dy) p where (x, y) = getLocation p ์กฐ์–ธ ํ•œ ๋งˆ๋”” ์œ„์˜ Movable ์˜ˆ์ œ๋ฅผ ๋„ˆ๋ฌด ๊นŠ๊ฒŒ ๋“ค์—ฌ๋‹ค๋ณด์ง€๋Š” ๋ง์ž. ์ด๊ฒƒ์€ ํด๋ž˜์Šค์™€ ๊ด€๋ จ๋œ ์–ธ์–ด ํŠน์ง•์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„์ผ ๋ฟ์ด๋‹ค. setLocation ๊ฐ™์ด, ์ƒ๊ฐํ•˜๊ธฐ์— ๋”ฐ๋ผ ์ผ๋ฐ˜ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฐœ๋ณ„ ๊ธฐ๋Šฅ์— ์ €๋งˆ๋‹ค ํƒ€์ž… ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•˜์ง€๋Š” ์•Š๋‹ค. ํŠนํžˆ ๋ชจ๋“  Located ์ธ์Šคํ„ด์Šค๊ฐ€ ์›€์ง์ผ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋ฉด Movable์€ ๋ถˆํ•„์š”ํ•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜๋งŒ ํ•„์š”ํ•˜๋ฉด ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ์“ธ ์ด์œ ๊ฐ€ ์ „ํ˜€ ์—†๋‹ค! ํด๋ž˜์Šค๋Š” ๊ทธ ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ์—ฌ๋Ÿฌ ํƒ€์ž…์ด ์žˆ๊ณ (๋˜๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ์ถ”๊ฐ€์ ์ธ ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•  ๊ฑฐ๋ผ๊ณ  ์˜ˆ์ƒ๋  ๋•Œ) ์‚ฌ์šฉ์ž๊ฐ€ ํƒ€์ž…๋“ค ์‚ฌ์ด์˜ ์ฐจ์ด๋ฅผ ์•Œ๊ฑฐ๋‚˜ ์‹ ๊ฒฝ ์“ฐ๊ธธ ์›ํ•˜์ง€ ์•Š์„ ๋•Œ ๊ฐ€์žฅ ํ›Œ๋ฅญํ•˜๊ฒŒ ์“ฐ์ด๋Š” ๋ฒ•์ด๋‹ค. Show๊ฐ€ ๋ฐ”๋กœ ๊ทธ๋Ÿฌํ•œ ์˜ˆ์‹œ๋‹ค. Show๋Š” ์ˆ˜๋งŽ์€ ํƒ€์ž…์ด ๊ตฌํ˜„ํ•˜๋Š” ๋ฒ”์šฉ ๊ธฐ๋Šฅ์ด๋ฉฐ show๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „ ์•Œ์•„์•ผ ํ•  ์‚ฌ์ „ ์ง€์‹์€ ์—†๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋“ค์–ด ์žˆ๋Š” ๋งŽ์€ ์ค‘์š”ํ•œ ํƒ€์ž… ํด๋ž˜์Šค๋“ค์„ ํŒŒํ—ค์น  ๊ฒƒ์ด๋‹ค. ์ด ํด๋ž˜์Šค๋“ค์€ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์ด ํ•œ ํด๋ž˜์Šค์— ์–ด์šฐ๋Ÿฌ์ง€๋Š” ์ข‹์€ ์˜ˆ์‹œ๋‹ค. ๋…ธํŠธ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด์˜ ๋‚˜๋ผ์—์„œ ์˜จ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์—๊ฒŒ: ํ•˜์Šค์ผˆ์˜ ํด๋ž˜์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์ƒ๊ฐํ•˜๋Š” ๊ทธ๋Ÿฐ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋ผ๋Š” ์šฉ์–ด์— ํ—ท๊ฐˆ๋ฆฌ์ง€ ๋ง์ž. ํƒ€์ž… ํด๋ž˜์Šค์˜ ์ผ๋ถ€ ์‚ฌ์šฉ๋ฒ•์€ ์ถ”์ƒ ํด๋ž˜์Šค๋‚˜ ์ž๋ฐ” ์ธํ„ฐํŽ˜์ด์Šค์™€ ๋น„์Šทํ•˜๊ธด ํ•˜์ง€๋งŒ, ๊ทผ๋ณธ์ ์ธ ์ฐจ์ด๊ฐ€ ์žˆ์œผ๋ฉฐ ์•ž์œผ๋กœ ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. โ†ฉ ์ด๊ฒƒ์ด ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด์™€์˜ ์ฃผ๋œ ์ฐจ์ด์ ์ด๋‹ค. ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด์—์„œ๋Š” ํด๋ž˜์Šค ์ž์ฒด๊ฐ€ ํƒ€์ž…์ด๋‹ค. โ†ฉ ์ด ๋งˆ๋ฒ•์„ ๋‹ค๋ฅธ ํด๋ž˜์Šค์— ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. GHC ํ™•์žฅ์€ ๋‹ค๋ฅธ ํ”ํžˆ ์“ฐ๋Š” ํด๋ž˜์Šค์—๋„ deriving์„ ํ—ˆ์šฉํ•˜๋ฉฐ ์ด๋•Œ ๊ทธ ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์€ ๋‹จ ํ•˜๋‚˜์ธ๋ฐ GHC ์ œ๋„ˆ๋ฆญ์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์€ ์ปค์Šคํ…€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ž๋™ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๊ฐ€๋Šฅ์ผ€ ํ•œ๋‹ค. โ†ฉ Prelude ๋ช…์„ธ์„œ์—์„œ ์™„์ „ํ•œ ์ •์˜๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด ๊ทธ ์ด์œ ๊ฐ€ ๋ช…ํ™•ํ•ด์ง„๋‹ค. ๊ธฐ๋ณธ ๊ตฌํ˜„์€ ๋น„๊ต๋  ๊ฐ’๋“ค์— (==)๋ฅผ ์ ์šฉํ•œ๋‹ค. โ†ฉ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ํƒ€์ž…์˜ ์ œํ•œ์€ ํด๋ž˜์Šค ์ƒ์†์„ ํ†ตํ•ด ์ง€์ •๋˜์–ด์•ผ ํ•œ๋‹ค. โ†ฉ newtype ์„ ์–ธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ๋‹จ type์€ ์•„๋‹ˆ๋‹ค. โ†ฉ ๊ถ๊ธˆํ•œ ๋…์ž๋“ค์„ ์œ„ํ•œ ๋ถ€์—ฐ ์„ค๋ช…: ์ด ๋ฌธ์ œ๋Š” ๊ณ ๊ธ‰๋ฐ˜์˜ "ํƒ€์ž…๊ณผ์˜ ์œ ํฌ" ์žฅ์—์„œ ๋…ผํ•˜๋Š” ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ๋“ค์ด ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฌธ์ œ๋“ค๊ณผ ์—ฐ๊ด€ ์žˆ๋‹ค. โ†ฉ 7 Functor ํด๋ž˜์Šค ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/The_Functor_class ๋™๊ธฐ Functor์˜ ๋„์ž… functor์˜ ๋ฒ•์น™ ๋ฌด์—‡์„ ์–ป์—ˆ๋Š”๊ฐ€? ์ด๋ฒˆ ์žฅ์—์„œ๋Š” Functor๋ผ๋Š” ์ค‘์š”ํ•œ ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. ๋™๊ธฐ ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์—์„œ ์–ด๋–ค ๊ทธ๋ฃนํ™”๋œ ๊ฐ’์˜ ๋ชจ๋“  ์›์†Œ์— ์ ์šฉ๋˜๋Š” ์—ฐ์‚ฐ์„ ๋ดค์—ˆ๋‹ค. ๊ทธ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ๊ฐ€ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋Š” map์ด๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋กœ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค์—ˆ๋˜ Tree ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์žˆ๋‹ค. data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving (Show) ๋‹ค์Œ์€ Tree๋ฅผ ์œ„ํ•ด ์ž‘์„ฑํ–ˆ๋˜ map ํ•จ์ˆ˜๋‹ค. treeMap :: (a -> b) -> Tree a -> Tree b treeMap f (Leaf x) = Leaf (f x) treeMap f (Branch left right) = Branch (treeMap f left) (treeMap f right) ์•ž์„œ ๋…ผ์˜ํ•œ ๋ฐ”์™€ ๊ฐ™์ด ์–ด๋–ค ์ž๋ฃŒ๊ตฌ์กฐ์—๋“  map<NAME>์˜ ํ•จ์ˆ˜๋ฅผ ์ƒ๊ฐํ•˜์—ฌ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ๋ณด์ถฉ ์„ค๋ช…์—์„œ map์„ ์ฒ˜์Œ ์†Œ๊ฐœํ•  ๋•Œ, ๋ฆฌ์ŠคํŠธ ์›์†Œ์— ๋Œ€ํ•œ ์•„์ฃผ ๊ตฌ์ฒด์ ์ธ ํ•จ์ˆ˜์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์ผ๋ฐ˜ํ™”๋ฅผ ๊ฑฐ์ณ map์„ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด๋งž๋Š” ํ•จ์ˆ˜์™€ ๊ฒฐํ•ฉํ–ˆ์—ˆ๋‹ค. ์ด์ œ ๋” ์ผ๋ฐ˜ํ™”ํ•˜์—ฌ, ๋ฆฌ์ŠคํŠธ์šฉ map, ํŠธ๋ฆฌ์šฉ map ๋“ฑ์„ ๋งŒ๋“œ๋Š” ๋Œ€์‹  ๋งคํ•‘ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ํƒ€์ž…์— ๋Œ€ํ•œ ๋ฒ”์šฉ map์„ ๋‘๋Š” ๊ฒƒ์ด ์–ด๋–จ๊นŒ? Functor์˜ ๋„์ž… Functor๋Š” ๋งคํ•‘ ๊ฐ€๋Šฅํ•œ ํƒ€์ž…์„ ์œ„ํ•œ Prelude ํด๋ž˜์Šค๋กœ์„œ, fmap์ด๋ผ๋Š” ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋œ๋‹ค. class Functor f where fmap :: (a -> b) -> f a -> f b ํƒ€์ž… ๋ณ€์ˆ˜ f์˜ ์šฉ๋ฒ•์ด ์ฒ˜์Œ์—๋Š” ๋‚ฏ์„ค ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค. f๋Š” ๋งค๊ฐœํ™”๋œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค. fmap์˜ ์‹œ๊ทธ๋„ˆ์ฒ˜์—์„œ f๋Š” a๋ฅผ ํ•˜๋‚˜์˜ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ทจํ•˜๊ณ  b๋ฅผ ๋‹ค๋ฅธ ๊ฒƒ์œผ๋กœ ์ทจํ•œ๋‹ค. Functor์˜ ์˜ˆ์‹œ๋ฅผ ํ•˜๋‚˜ ๋ณด์ž. f๋ฅผ Maybe๋กœ ์น˜ํ™˜ํ•˜๋ฉด fmap์˜ ์‹œ๊ทธ๋„ˆ์ณ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋œ๋‹ค. fmap :: (a -> b) -> Maybe a -> Maybe b ์ด๋Š” ๋‹ค์Œ์˜ ์ž์—ฐ์Šค๋Ÿฌ์šด ์ •์˜์™€ ๋“ค์–ด๋งž๋Š”๋‹ค. instance Functor Maybe where fmap f Nothing = Nothing fmap f (Just x) = Just (f x) (์šฐ์—ฐํžˆ๋„ ์ด ์ •์˜๋Š” Prelude์— ๋“ค์–ด์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ "๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค" ์žฅ์— ์žˆ๋Š” ๊ทธ ์˜ˆ์ œ์— ๋Œ€ํ•œ maybeMap์„ ๊ตฌํ˜„ํ•  ํ•„์š”๋Š” ์—†๋‹ค) ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ Functor ์ธ์Šคํ„ด์Šค๋Š” ๊ฐ„๋‹จํ•˜๊ณ  ์ด๊ฒƒ ์—ญ์‹œ Prelude์— ๋“ค์–ด์žˆ๋‹ค. instance Functor [] where fmap = map ๊ทธ๋ฆฌ๊ณ  fmap์˜ ์‹œ๊ทธ๋„ˆ์ณ์—์„œ f๋ฅผ []๋กœ ์น˜ํ™˜ํ•˜๋ฉด ์นœ์ˆ™ํ•œ map์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ fmap์€ ์ž„์˜์˜ ๋งค๊ฐœํ™” ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋Œ€ํ•ด map์„ ์ผ๋ฐ˜ํ™”ํ•œ ๊ฒƒ์ด๋‹ค. 1 ์ž์—ฐ์Šค๋ ˆ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ๋ฐ์ดํ„ฐ ํƒ€์ž…์—๋„ Functor ์ธ์Šคํ„ด์Šค๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ treeMap์„ Functor์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋ฐ”๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค. instance Functor Tree where fmap f (Leaf x) = Leaf (f x) fmap f (Branch left right) = Branch (fmap f left) (fmap f right) ๋‹ค์Œ์€ ์œ„์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์‹ค์ œ๋กœ ์“ฐ๋Š” fmap ๋ฐ๋ชจ๋‹ค. Tree์˜ data์™€ instance ์„ ์–ธ๋งŒ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ๋œ๋‹ค. ๋‚˜๋จธ์ง€๋Š” ์ด๋ฏธ Prelude์— ๋“ค์–ด์žˆ๋‹ค. *Main> fmap (2*) [1,2,3,4] [2,4,6,8] *Main> fmap (2*) (Just 1) Just 2 *Main> fmap (fmap (2*)) [Just 1, Just 2, Just 3, Nothing] [Just 2, Just 4, Just 6, Nothing] *Main> fmap (2*) (Branch (Branch (Leaf 1) (Leaf 2)) (Branch (Leaf 3) (Leaf 4))) Branch (Branch (Leaf 2) (Leaf 4)) (Branch (Leaf 6) (Leaf 8)) ์ž ๊น ์—ฌ๊ธฐ์„œ ์–ธ๊ธ‰ํ•œ []์™€ Maybe ์˜ˆ์‹œ ๋ฐ–์—๋„ ๋งŽ์€ ํŽธ๋ฆฌํ•œ Functor ์ธ์Šคํ„ด์Šค๋“ค์ด ์ œ๊ณต๋œ๋‹ค. ์™„์ „ํ•œ ๋ชฉ๋ก์€ Control.Monad ๋ชจ๋“ˆ์— ๋Œ€ํ•œ GHC์˜ ๋ฌธ์„œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. functor์˜ ๋ฒ•์น™ Functor์˜ ์ƒˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์ •์˜ํ•  ๋•Œ๋Š” ๋‘ ๊ฐœ์˜ functor ๋ฒ•์น™์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค. ์ด ๋ฒ•์น™๋“ค์— ๋ถˆ๊ฐ€์‚ฌ์˜ํ•œ ์ ์€ ์—†๋‹ค. ์ด ๋ฒ•์น™๋“ค์˜ ์—ญํ• ์€ fmap์ด ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜์—ฌ ๋งคํ•‘ ์ž‘์—…์„ ์‹ค์ œ๋กœ ์ˆ˜ํ–‰ํ•˜๋„๋ก ๋ณด์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 2 ์ฒซ ๋ฒˆ์งธ ๋ฒ•์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. fmap id == id id๋Š” ํ•ญ๋“ฑ ํ•จ์ˆ˜๋กœ์„œ ์ธ์ž๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ œ1๋ฒ•์น™์€ functor๋กœ id๋ฅผ ๋งคํ•‘ํ•˜๋ฉด ๋ฐ˜๋“œ์‹œ ๊ทธ functor๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•จ์„ ๋œปํ•œ๋‹ค. ์ œ2๋ฒ•์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. fmap (f . g) == fmap f. fmap g ์ด ๋ฒ•์น™์€ ํ•ฉ์„ฑ ํ•จ์ˆ˜๋ฅผ ๋งคํ•‘ํ•˜๋“  ๋จผ์ € ํ•˜๋‚˜๋ฅผ ๋งคํ•‘ํ•˜๊ณ  ๋‹ค์Œ ๊ฒƒ์„ ๋งคํ•‘ํ•˜๋“  ๋ฌด๊ด€ํ•ด์•ผ ํ•จ์„ ๋œปํ•œ๋‹ค. (์ ์šฉ ์ˆœ์„œ๋Š” ๋‘˜ ๋‹ค ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •) ๋ฌด์—‡์„ ์–ป์—ˆ๋Š”๊ฐ€? ์ด์ฏค์—์„œ Functor ํด๋ž˜์Šค์— ์˜ํ•œ ์ถ”๊ฐ€์ ์ธ ์ผ๋ฐ˜ํ™” ๊ณ„์ธต์œผ๋กœ ์ธํ•ด ๋ฌด์Šจ ์ด์ ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š”์ง€ ์ž๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๋‘ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ์ด๋“์ด ์žˆ๋‹ค. fmap ๋ฉ”์„œ๋“œ ๋•๋ถ„์— ์šฐ๋ฆฌ๋Š” ์„œ๋กœ ์ด๋ฆ„์ด ๋‹ค๋ฅธ ๋งคํ•‘ ๋ฉ”์„œ๋“œ๋“ค(maybeMap, treeMap, weirdMap ๋“ฑ)์„ ์ผ์ผ์ด ๊ธฐ์–ตํ•˜๊ณ  ์ฝ๊ณ  ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์ฝ”๋“œ๋Š” ๋” ๊น”๋”ํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์›Œ์ง„๋‹ค. fmap์ด ์‚ฌ์šฉ๋œ ๊ณณ์„ ๋ณด์ž๋งˆ์ž ์šฐ๋ฆฌ๋Š” ๋ฌด์Šจ ์ผ์ด ๋ฒŒ์–ด์ง€๋Š”์ง€ ๊ฐ์„ ์žก์„ ์ˆ˜ ์žˆ๋‹ค.3 ํƒ€์ž… ํด๋ž˜์Šค ์ฒด๊ณ„๋ฅผ ์ด์šฉํ•˜๋ฉด [], Maybe, Tree, ๊ทธ ์™ธ์— ์–ด๋–ค ํŽ‘ํ„ฐ์—๋„ ์ž‘๋™ํ•˜๋Š” fmap ๊ธฐ๋ฐ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋ฆ„ ์•„๋‹Œ ํ•ต์‹ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋งŽ์€ ์œ ์šฉํ•œ ํด๋ž˜์Šค๊ฐ€ Functor๋ฅผ ์ƒ์†ํ•œ๋‹ค. ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์˜จ๊ฐ– ์ข…๋ฅ˜์˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ํ•ด๋ฒ•์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ํ•˜์Šค์ผˆ์„ ์–ด๋–ค ์šฉ๋„๋กœ ์‚ฌ์šฉํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ์ƒˆ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•  ํ•„์š”๊ฐ€ ์—†์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ํƒ€์ž… ํด๋ž˜์Šค๋Š” ํ•ญ์ƒ ์“ฐ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ํ•˜์Šค์ผˆ์˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ•๋ ฅํ•œ ํŠน์ง•๊ณผ ์ •๊ตํ•œ ๊ธฐ๋Šฅ์€ ํƒ€์ž… ํด๋ž˜์Šค์— ์˜์กดํ•œ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ํด๋ž˜์Šค๋Š” ์šฐ๋ฆฌ์˜ ๊ณต๋ถ€์—์„œ ๋น ์งˆ ์ˆ˜ ์—†๋Š” ์กด์žฌ์ผ ๊ฒƒ์ด๋‹ค. ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ๊ฐ€์žฅ ์ง๊ด€์ ์ธ ์˜ˆ์‹œ๋‹ค. ํ•˜์ง€๋งŒ ์ž๋ฃŒ๊ตฌ์กฐ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์—†๋Š” ํŽ‘ํ„ฐ๋„ ์žˆ๋‹ค. ํ”ํžˆ๋“ค ํŽ‘ํ„ฐ๋ฅผ ์ปจํ…Œ์ด๋„ˆ์— ๋น„์œ ํ•˜๊ณค ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋“  ๋น„์œ ๊ฐ€ ๊ทธ๋ ‡๋“ฏ์ด ๋Œ€๋žต์ ์œผ๋กœ ๋“ค์–ด๋งž์„ ๋ฟ์ด๋‹ค. โ†ฉ ํŽ‘ํ„ฐ์˜ ๋ฒ•์น™, ๋”๋ถˆ์–ด ํŽ‘ํ„ฐ๋ผ๋Š” ๊ฐœ๋…์€ ๋ฒ”์ฃผ๋ก ์ด๋ผ๋Š” ์ˆ˜ํ•™์˜ ํ•œ ๊ฐˆ๋ž˜์— ๊ธฐ๋ฐ˜ํ•˜๋Š”๋ฐ ์ง€๊ธˆ์€ ์‹ ๊ฒฝ ์“ธ ์‚ฌํ•ญ์ด ์•„๋‹ˆ๋‹ค. ๊ณ ๊ธ‰๋ฐ˜์—์„œ ๊ด€๋ จ ์ฃผ์ œ๋“ค์„ ๋ณผ ๊ธฐํšŒ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค. โ†ฉ ์ด๋Š” ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ๋ช…์‹œ์ ์ธ ์žฌ๊ท€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณ ์ฐจ ํ•จ์ˆ˜์— ๊ธฐ๋ฐ˜ํ•œ ๊ตฌํ˜„์œผ๋กœ ๋Œ€์ฒดํ•  ๋•Œ ์–ป์–ด์ง€๋Š” ๋ช…๋ฃŒํ•จ๊ณผ ๋น„์Šทํ•˜๋‹ค. โ†ฉ 4 ๋ชจ๋‚˜๋“œ ๋ชจ๋‚˜๋“œ ์„œ์žฅ: IO, ์ด๋ฅธ๋ฐ” ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ Maybe - List do ํ‘œ๊ธฐ IO - State Alternative์™€ MonadPlus ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ 1 ์„œ์žฅ: IO, ์ ์šฉ์„ฑ(applicative) ํŽ‘ ํ„ฐ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Prologue:_IO,_an_applicative_functor functorial์˜ ์ ์ ˆํ•œ ๋ฒˆ์—ญ? (https://math.stackexchange.com/questions/2009361/what-does-it-mean-to-be-functorial-in-something) 1๋ง‰ : Applicative ํŽ‘ ํ„ฐ ๋‚ด์—์„œ์˜ ์ ์šฉ(application) 2๋ง‰ : IO ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ(Referential transparency) ์•ˆ๊ฐœ ๊ฑท์–ด๋‚ด๊ธฐ ์ด์ œ ์‹œ์ž‘์ผ ๋ฟ ๋…ธํŠธ ํŽ‘ํ„ฐ์˜ ๋„์ž…์€ ์ด ์ฑ…์˜ ๋ถ„์ˆ˜๋ น์ด๋‹ค. ์ด ๋„์ž…๋ถ€์—์„œ ๋‹ค์Œ ์žฅ๋“ค์„ ์œ„ํ•œ ๋ฌด๋Œ€๋ฅผ ์ค€๋น„ํ•˜๋ฉฐ ๊ทธ ์ด์œ ๊ฐ€ ์ ์ฐจ ๋“œ๋Ÿฌ๋‚  ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃจ๋Š” ์ฝ”๋“œ ์˜ˆ์ œ๋“ค์€ ์•„์ฃผ ๊ฐ„๋‹จํ•˜์ง€๋งŒ, ์ƒˆ๋กญ๊ณ  ์ค‘์š”ํ•œ ๊ฐœ๋…๋“ค์„ ๋‹ค๋ฃจ๋Š”๋ฐ ํ™œ์šฉ๋œ๋‹ค. ์ด ๊ฐœ๋…๋“ค์€ ์ฑ…์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๋‹ค์‹œ ๋ฐฉ๋ฌธํ•˜๊ณ  ๋” ๊นŠ๊ฒŒ ๋‹ค๋ฃจ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์ด๋ฒˆ ์žฅ์„ ์‹ ์ค‘ํ•˜๊ฒŒ ์—ฐ๊ตฌํ•˜์—ฌ ๊ฐ ๋‹จ๊ณ„์— ๋‚ดํฌ๋œ ๋œป์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด๊ณ  ์ฝ”๋“œ ์ƒ˜ํ”Œ์„ GHCi์—์„œ ํ™•์ธํ•ด ๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. 1๋ง‰ : Applicative ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์—์„œ๋Š” Text.Read ๋ชจ๋“ˆ์ด ์ œ๊ณตํ•˜๋Š” readMaybe ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•  ๊ฒƒ์ด๋‹ค. GHCi> :m +Text.Read GHCi> :t readMaybe readMaybe :: Read a => String -> Maybe a readMaybe๋Š” ๋ฌธ์ž์—ด์„ ํ•˜์Šค ์ผˆ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•œ๋‹ค. ์ œ๊ณต๋œ ๋ฌธ์ž์—ด์ด a ํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ํ•ด์„๋˜๊ธฐ ์ ํ•ฉํ•œ ํฌ๋งท์„ ๊ฐ€์ง€๋ฉด readMaybe๋Š” ๋ณ€ํ™˜๋œ ๊ฐ’์„ Just๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฒฐ๊ณผ๋Š” Nothing์ด๋‹ค. GHCi> readMaybe "3" :: Maybe Integer Just 3 GHCi> readMaybe "foo" :: Maybe Integer Nothing GHCi> readMaybe "3.5" :: Maybe Integer Nothing GHCi> readMaybe "3.5" :: Maybe Double Just 3.5 ๋…ธํŠธ readMaybe๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์–ด๋–ค ํƒ€์ž…์„ ์ฝ์œผ๋ ค๊ณ  ํ•˜๋Š”์ง€๋ฅผ ๋ช…์‹œํ•ด์•ผ ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์ด๋Š” ํƒ€์ž… ์ถ”๋ก ๊ณผ ์šฐ๋ฆฌ ์ฝ”๋“œ ๋‚ด์˜ ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ์กฐํ•ฉํ•ด ์ฒ˜๋ฆฌ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ€๋”์€ ์•Œ๋งž์€ ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋Œ€์‹  type annotation์„ ๋ถ™์ด๋Š” ๊ฒŒ ๋” ํŽธ๋ฆฌํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์˜ˆ์ œ์—์„œ readMaybe "3" :: Maybe Integer์˜ :: Maybe Integer๋Š” readMaybe "3"์˜ ํƒ€์ž…์ด Maybe Integer ์ž„์„ ๋œปํ•œ๋‹ค. readMaybe๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ ์ž‘์—…๋“ค์„ ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ ์žฅ์˜ ์Šคํƒ€์ผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ปค๋งจ๋“œ ๋ผ์ธ์„ ํ†ตํ•ด ์ œ๊ณตํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ›๋Š”๋‹ค. ์ˆซ์ž๋กœ์„œ ์ฝ๊ธฐ๋ฅผ ์‹œ๋„ํ•œ๋‹ค. (Double ํƒ€์ž…์„ ์ด์šฉ) ์ฝ๊ธฐ๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด ๊ทธ ์ˆซ์ž์˜ ๋‘ ๋ฐฐ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์•„๋‹ˆ๋ฉด ์„ค๋ช…๋ฌธ์„ ์ถœ๋ ฅํ•˜๊ณ  ๋‹ค์‹œ ์‹œ์ž‘ํ•œ๋‹ค. ๋…ธํŠธ ๊ณ„์†ํ•˜๊ธฐ ์ „์— ํ”„๋กœ๊ทธ๋žจ์„ ์ง์ ‘ ์ž‘์„ฑํ•ด ๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. readMaybe ์™ธ์—๋„ getLine, putStrLn, show ๋“ฑ ์œ ์šฉํ•œ ๊ฒƒ๋“ค์„ ๋ฐœ๊ฒฌํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ์ฝ˜์†”์„ ํ†ตํ•ด ์ฝ๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๋ฒ•์„ ๋ณต๊ธฐํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ ์žฅ์„ ๋ณด์ž. ๋‹ค์Œ์€ ๊ฐ€๋Šฅํ•œ ๊ตฌํ˜„ ์ค‘ ํ•˜๋‚˜๋‹ค. import Text.Read interactiveDoubling = do putStrLn "Choose a number:" s <- getLine let mx = readMaybe s :: Maybe Double case mx of Just x -> putStrLn ("The double of your number is " ++ show (2*x)) Nothing -> do putStrLn "This is not a valid number. Retrying..." interactiveDoubling GHCi> interactiveDoubling Choose a number: foo This is not a valid number. Retrying... Choose a number: The double of your number is 6.0 ์ž˜ ๋˜๊ณ  ๊ฐ„๋‹จํ•˜๋‹ค. ์ด ํ•ด๋ฒ•์„ ๋ณ€ํ˜•ํ•˜์ž๋ฉด Maybe๊ฐ€ Functor ์ž„์„ ํ™œ์šฉํ•ด case ๋ฌธ์—์„œ mx๋ฅผ ํ•ด์ฒดํ•˜๊ธฐ ์ „์— ๊ทธ ๊ฐ’์„ ๋‘ ๋ฐฐ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. interactiveDoubling = do putStrLn "Choose a number:" s <- getLine let mx = readMaybe s :: Maybe Double case fmap (2*) mx of Just d -> putStrLn ("The double of your number is " ++ show d) Nothing -> do putStrLn "This is not a valid number. Retrying..." interactiveDoubling ์ด๋ฒˆ์—๋Š” ๊ทธ๋ ‡๊ฒŒ ํ•ด๋„ ์‹ค์งˆ์ ์ธ ์ด๋“์ด ์—†์—ˆ์ง€๋งŒ, ์ด๋Ÿฐ ๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์—ผ๋‘์— ๋‘ ์ž. ํŽ‘ ํ„ฐ ๋‚ด์—์„œ์˜ ์ ์šฉ(application) ์ข€ ๋” ๋ณต์žกํ•œ ๊ฒƒ์„ ํ•ด๋ณด์ž. readMaybe๋กœ ์ˆซ์ž ๋‘ ๊ฐœ๋ฅผ ์ฝ๊ณ  ๊ทธ ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค. (์—ญ์‹œ ๊ณ„์†ํ•˜๊ธฐ ์ „์— ์ง์ ‘ ์ž‘์„ฑํ•ด ๋ณด๋Š” ๊ฒƒ์„ ์ œ์•ˆํ•œ๋‹ค) ๋‹ค์Œ์€ ๊ฐ€๋Šฅํ•œ ํ•ด๋ฒ• ์ค‘ ํ•˜๋‚˜๋‹ค. interactiveSumming = do putStrLn "Choose two numbers:" sx <- getLine sy <- getLine let mx = readMaybe sx :: Maybe Double my = readMaybe sy case mx of Just x -> case my of Just y -> putStrLn ("The sum of your numbers is " ++ show (x+y)) Nothing -> retry Nothing -> retry where retry = do putStrLn "Invalid number. Retrying..." interactiveSumming GHCi> interactiveSumming Choose two numbers: foo Invalid number. Retrying... Choose two numbers: foo Invalid number. Retrying... Choose two numbers: 4 The sum of your numbers is 7.0 interactiveSumming์€ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€๋งŒ ์ž‘์„ฑํ•˜๊ธฐ ์„ฑ๊ฐ€์‹œ๋‹ค. ํŠนํžˆ ์ค‘์ฒฉ๋œ case ๋ฌธ๋“ค์€ ์•„๋ฆ„๋‹ต์ง€ ์•Š๊ณ  ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ๊ฒƒ์„ ์กฐ๊ธˆ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ ๋‹ค. interactiveDoubling์˜ ๋‘ ๋ฒˆ์งธ ๋ฒ„์ „์—์„œ fmap์œผ๋กœ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ, ์ˆซ์ž๋“ค์„ ํ•ด์ฒดํ•˜๊ธฐ ์ „์— ๋”ํ•  ์ˆ˜๋‹จ์ด ์žˆ์—ˆ๋‹ค๋ฉด case๋ฅผ ํ•˜๋‚˜๋งŒ ์“ธ ์ˆ˜ ์žˆ์—ˆ์„ ๊ฒƒ์ด๋‹ค. -- Wishful thinking... case somehowSumMaybes mx my of Just z -> putStrLn ("The sum of your numbers is " ++ show z) Nothing -> do putStrLn "Invalid number. Retrying..." interactiveSumming ๊ทธ๋Ÿฐ๋ฐ somehowSumMaybes ์ž๋ฆฌ์— ๋ฌด์–ผ ๋„ฃ์–ด์•ผ ํ• ๊นŒ? ๊ฐ€๋ น fmap์œผ๋กœ๋Š” ์ถฉ๋ถ„ํ•˜์ง€ ์•Š๋‹ค. ํ•œํŽธ fmap (+)๋Š” Maybe๋กœ ๊ฐ์‹ผ ๊ฐ’์— (+)๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ž‘๋™ํ•œ๋‹ค. GHCi> :t (+) 3 (+) 3 :: Num a => a -> a GHCi> :t fmap (+) (Just 3) fmap (+) (Just 3) :: Num a => Maybe (a -> a) ์šฐ๋ฆฌ๋Š” Maybe๋กœ ๊ฐ์‹ผ ํ•จ์ˆ˜๋ฅผ ๋‘ ๋ฒˆ์งธ ๊ฐ’์— ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅธ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ๊ฐ€์ง€๋Š” ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b ์œ„ ํ•จ์ˆ˜๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. GHCi> fmap (+) (Just 3) <*> Just 4 Just 7 ๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ GHCi ํ”„๋กฌํ”„ํŠธ๋Š” ์ƒ์ƒ์ด ์•„๋‹ˆ๋‹ค. (<*>)๋Š” ์‹ค์กดํ•˜๊ณ  GHCi์—์„œ ์‹คํ—˜ํ•ด ๋ณด๋ฉด ์‹ค์ œ๋กœ ์ž‘๋™ํ•œ๋‹ค! fmap์˜ ์ค‘์œ„ ๋™์˜์–ด์ธ (<$>)๋„ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์˜ ํ‘œํ˜„์‹์€ ์ƒ๋‹นํžˆ ๊น”๋”ํ•˜๊ฒŒ ๋ณด์ธ๋‹ค. GHCi> (+) <$> Just 3 <*> Just 4 Just 7 ์‹ค์ œ ํƒ€์ž… (<*>)์€ ์šฐ๋ฆฌ๊ฐ€ ๋ฐฉ๊ธˆ ์ž‘์„ฑํ•œ ๊ฒƒ๋ณด๋‹ค ๋” ์ผ๋ฐ˜์ ์ด๋‹ค. ํ™•์ธํ•ด ๋ณด๋ฉด... GHCi> :t (<*>) (<*>) :: Applicative f => f (a -> b) -> f a -> f b Applicative๋ผ๋Š” ์ƒˆ๋กœ์šด ํƒ€์ž… ํด๋ž˜์Šค๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค. Applicative๋Š” ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ(applicative functor)๋“ค์˜ ํƒ€์ž… ํด๋ž˜์Šค๋‹ค. ์†Œ๊ฐœ๋ฅผ ํ•˜์ž๋ฉด ์ ์šฉ์„ฑ ํŽ‘ํ„ฐ๋Š” ํŽ‘ํ„ฐ์˜ ์ผ์ข…์œผ๋กœ์„œ ํ•จ์ˆ˜๋“ค์„ ํŽ‘ ํ„ฐ ๋‚ด์— ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ง€์›ํ•˜๊ณ , ๊ทธ๋Ÿผ์œผ๋กœ์จ ๋ถ€๋ถ„ ์ ์šฉ(๋ฐ ๋‹ค์ธ์ž ํ•จ์ˆ˜)์˜ ์›ํ™œํ•œ ์‚ฌ์šฉ์„ ๋•๋Š”๋‹ค. Applicative์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋Š” Functor์ด๋ฉฐ, Maybe ์™ธ์—๋„ ๋งŽ์€ ๋ฒ”์šฉ Functor๊ฐ€ Applicative์ด๋‹ค. ๋‹ค์Œ์€ Maybe๋ฅผ ์œ„ํ•œ Applicative ์ธ์Šคํ„ด์Šค๋‹ค. instance Applicative Maybe where pure = Just (Just f) <*> (Just x) = Just (f x) _ <*> _ = Nothing ์‚ฌ์‹ค (<*>)์˜ ์ •์˜๋Š” ์ƒ๋‹นํžˆ ๊ฐ„๋‹จํ•˜๋‹ค. ๋‘ ๊ฐ’ ๋ชจ๋‘ Nothing์ด ์•„๋‹ˆ๋ฉด ํ•จ์ˆ˜ f๋ฅผ x์— ์ ์šฉํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ Just๋กœ ๊ฐ์‹ผ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ Nothing์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ๋กœ์ง์ด interactiveSumming์—์„œ ์ค‘์ฒฉ case ๋ฌธ์ด ํ•˜๋Š” ์ผ๊ณผ ์ •ํ™•ํžˆ ๋™๋“ฑํ•˜๋‹ค๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. ์œ„์˜ ์ธ์Šคํ„ด์Šค์—๋Š” (<*>) ์™ธ์—๋„ pure๋ผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. GHCi> :t pure pure :: Applicative f => a -> f a pure๋Š” ๊ฐ’์„ ๋ฐ›์•„์„œ ๊ธฐ๋ณธ์ ์ด๊ณ  ์ž๋ช…ํ•œ ๋ฐฉ์‹์œผ๋กœ ํŽ‘ํ„ฐ์— ์ง‘์–ด๋„ฃ๋Š”๋‹ค. Maybe์˜ ๊ฒฝ์šฐ ๊ทธ ์ž๋ช…ํ•œ ๋ฐฉ์‹์€ ๊ฐ’์„ Just๋กœ ๊ฐ์‹ธ๋Š” ๊ฒƒ์ด๋‹ค. ์ž๋ช…ํ•˜์ง€ ์•Š์€ ๋ฐฉ์‹์œผ๋กœ๋Š” ๊ทธ ๊ฐ’์„ ๋ฒ„๋ฆฌ๊ณ  Nothing์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. pure๋ฅผ ์ด์šฉํ•˜๋ฉด ์œ„์˜ 3 ๋”ํ•˜๊ธฐ 4 ์˜ˆ์ œ๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. GHCi> (+) <$> pure 3 <*> pure 4 :: Num a => Maybe a Just 7 ํ˜น์€ ์ด๋Ÿฐ ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. GHCi> pure (+) <*> pure 3 <*> pure 4 :: Num a => Maybe a Just 7 Functor ํด๋ž˜์Šค๊ฐ€ ํ•ฉ๋ฆฌ์ ์ธ ์ธ์Šคํ„ด์Šค๋“ค์ด ์–ด๋–ป๊ฒŒ ํ–‰๋™ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๊ธฐ์ˆ ํ•˜๋Š” ๋ฒ•์น™๋“ค์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์ฒ˜๋Ÿผ Applicative์—๋„ ๊ทธ๋Ÿฐ ๋ฒ•์น™๋“ค์ด ์žˆ๋‹ค. ์ด ๋ฒ•์น™๋“ค์€ ํŠนํžˆ pure๋ฅผ ํ†ตํ•ด ๊ฐ’๋“ค์„ ํŽ‘ํ„ฐ์— ๋„ฃ๋Š” "์ž๋ช…ํ•œ" ๋ฐฉ์‹์ด ๋ฌด์—‡์ธ์ง€๋ฅผ ๊ธฐ์ˆ ํ•˜๋Š” ๋ฐ ์ค‘์ ์„ ๋‘”๋‹ค. ์ด๋ฒˆ ์žฅ์—์„œ ์ด๋ฏธ ๋งŽ์€ ์ฃผ์ œ๊ฐ€ ๋‚˜์™”๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๋ฒ•์น™๋“ค์„ ์ง€๊ธˆ ๋…ผ์˜ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋ฆฌ ๋ฉ€์ง€ ์•Š์€ ์‹œ์ ์— ์ด ์ค‘์š”ํ•œ ์ฃผ์ œ๋กœ ๋‹ค์‹œ ๋Œ์•„์˜ฌ ๊ฒƒ์ด๋‹ค. ๋…ธํŠธ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ์žฅ์˜ "์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ๋ฒ•์น™" ์„น์…˜์„ ๋จผ์ € ์ฝ๊ณ  ์™€๋„ ๋œ๋‹ค. "ZipList" ์„น์…˜์ด ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๊ฒƒ๋“ค๋งŒ ํ™œ์šฉํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ์˜ˆ์ œ๋“ค์„ ์ œ๊ณตํ•˜๋ฏ€๋กœ ์ด๊ฒƒ๋„ ๋ด๋‘๋ฉด ์ข‹๋‹ค. ์ •๋ฆฌํ•˜์ž๋ฉด ๋‹ค์Œ์€ (<*>)๋ฅผ ํ†ตํ•ด ๊ฐœ์„ ํ•œ interactiveSumming์ด๋‹ค. interactiveSumming = do putStrLn "Choose two numbers:" sx <- getLine sy <- getLine let mx = readMaybe sx :: Maybe Double my = readMaybe sy case (+) <$> mx <*> my of Just z -> putStrLn ("The sum of your numbers is " ++ show z) Nothing -> do putStrLn "Invalid number. Retrying..." interactiveSumming 2๋ง‰ : IO ์ง€๊ธˆ๊นŒ์ง€์˜ ์˜ˆ์ œ์—์„œ๋Š” getLine ๊ฐ™์€ I/O ์•ก์…˜๋“ค์„ ๋‹น์—ฐํ•˜๋‹ค๊ณ  ์—ฌ๊ฒผ๋‹ค. ์ด์ œ ๋ช‡ ์žฅ ์•ž์—์„œ ์ฒ˜์Œ ๋– ์˜ค๋ฅธ ์งˆ๋ฌธ์„ ์žฌ๊ณ ํ•  ์‹œ๊ฐ„์ด๋‹ค. getLine์˜ ํƒ€์ž…์€ ๋ฌด์—‡์ผ๊นŒ? ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ ์žฅ์—์„œ ๋ดค๋“ฏ์ด ๊ทธ ๋‹ต์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. GHCi> :t getLine getLine :: IO String ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๋ฐ”์— ๋ฏธ๋ฃจ์–ด๋ณด๋ฉด IO๋Š” ํƒ€์ž… ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๊ฐ€์ง€๋Š” ํƒ€์ž… ์ƒ์„ฑ์ž์ด๋ฉฐ, getLine์˜ ๊ฒฝ์šฐ ๊ทธ ํƒ€์ž… ๋ณ€์ˆ˜๋Š” String์œผ๋กœ์„œ ์ธ์Šคํ„ด์Šคํ™”๋œ๋‹ค. ํ•˜์ง€๋งŒ ๋ฌธ์ œ์˜ ๊ทผ์›์— ๋‹ค๋‹ค๋ฅด์ง€๋Š” ๋ชปํ–ˆ๋‹ค. IO String์˜ ์ฐธ๋œป์€ ๋ฌด์—‡์ด๊ณ  ํ‰๋ฒ”ํ•œ String๊ณผ ๋ฌด์—‡์ด ๋‹ค๋ฅธ ๊ฑธ๊นŒ? ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ(Referential transparency) ํ•˜์Šค์ผˆ์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์€ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ํ‘œํ˜„์‹์€ ์ฐธ์กฐ ํˆฌ๋ช…(referentially transparent) ํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ๋™์ž‘์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ์–ด๋–ค ํ‘œํ˜„์‹์ด๋“  ๊ทธ๊ฒƒ์˜ ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Œ์„ ๋œปํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ์˜ ์•„์ฃผ ๋‹จ์ˆœํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ๋ณด์ž. addExclamation :: String -> String addExclamation s = s ++ "!" main = putStrLn (addExclamation "Hello") ๊ทธ ๋™์ž‘์€ ๊ทธ๋‹ค์ง€ ๋†€๋ž์ง€ ์•Š๋‹ค. GHCi> main Hello! addExclamation s = s ++ "!"์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ์šฐ๋ฆฌ๋Š” main์ด addExclamation๋ฅผ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š๋„๋ก ์žฌ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. addExclamation ์ •์˜์˜ ์šฐ๋ณ€์—์„œ s๋ฅผ "Hello"๋กœ ๋Œ€์ฒดํ•˜๊ณ  addExclamation "Hello"๋ฅผ ๊ทธ ๊ฒฐ๊ณผ ํ‘œํ˜„์‹์œผ๋กœ ๋Œ€์ฒดํ•œ๋‹ค. ์•ž์„œ ๋งํ–ˆ๋“ฏ ํ”„๋กœ๊ทธ๋žจ์˜ ๋™์ž‘์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. GHCi> let main = putStrLn ("Hello" ++ "!") GHCi> main Hello! ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์€ ์ด๋Ÿฐ ์ข…๋ฅ˜์˜ ์น˜ํ™˜์ด ์ž‘๋™ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค. ์ด๋Ÿฐ ๋ณด์žฅ์€ ๋ชจ๋“  ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ํ™•์žฅ๋˜์–ด, ํ”„๋กœ๊ทธ๋žจ์„ ์ดํ•ดํ•˜๊ณ  ๊ทธ ๋™์ž‘์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ด์ œ getLine์˜ ํƒ€์ž…์ด String์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜์ž. ์ด ๊ฒฝ์šฐ getLine์„ addExclamation์˜ ์ธ์ž๋กœ ์“ธ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. -- Not actual code. main = putStrLn (addExclamation getLine) ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์—๋Š” ์ƒˆ๋กœ์šด ์˜๋ฌธ์ด ์ƒ๊ธด๋‹ค. ๋งŒ์•ฝ getLine์ด String์ด๋ผ๋ฉด ์–ด๋–ค String ์ธ๊ฐ€? ์—ฌ๊ธฐ์—๋Š” ๋งŒ์กฑ์Šค๋Ÿฌ์šด ๋‹ต์ด ์—†๋‹ค. "Hello", "Goodby", ํ˜น์€ ์‚ฌ์šฉ์ž๊ฐ€ ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅํ•œ ๋ฌด์—‡์ด๋“  ๋  ์ˆ˜ ์žˆ๋‹ค. ๋ฌด์—‡๋ณด๋‹ค getLine์„ ์ž„์˜์˜ String์œผ๋กœ ๊ต์ฒดํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ๋ง๊ฐ€์ง€๋Š”๋ฐ, ์‚ฌ์šฉ์ž๊ฐ€ ํ„ฐ๋ฏธ๋„์— ๋ฌธ์ž์—ด์„ ๋” ์ด์ƒ ์ž…๋ ฅํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ getLine์ด String ํƒ€์ž…์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์€ ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์„ ๊นจํŠธ๋ฆฐ๋‹ค. ๋‹ค๋ฅธ I/O ์•ก์…˜๋“ค๋„ ๋ชจ๋‘ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” ๋ถˆํˆฌ๋ช…ํ•˜๋ฉฐ, ๋ฏธ๋ฆฌ ์•„๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ํ”„๋กœ๊ทธ๋žจ์˜ ์™ธ๋ถ€์— ์žˆ๋Š” ์š”์ธ์— ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์•ˆ๊ฐœ ๊ฑท์–ด๋‚ด๊ธฐ getLine์ด ๋ณด์—ฌ์ฃผ๋“ฏ์ด I/O ์•ก์…˜์—๋Š” ๊ทผ๋ณธ์ ์ธ ๋น„๊ฒฐ์ •์„ฑ์ด ๊ด€์—ฌํ•œ๋‹ค. ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์„ ์œ ์ง€ํ•˜๋ ค๋ฉด ์ด๋Ÿฌํ•œ ๋น„๊ฒฐ์ •์„ฑ์„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ์ด๋Š” IO ํƒ€์ž… ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ด๋ค„์ง„๋‹ค. getLine์ด IO String์ด๋ผ๋Š” ๊ฒƒ์˜ ๋œป์€ ์ด๊ฒƒ์ด ์‹ค์ œ String์ด๋ผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ , ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋  ๋•Œ์—์„œ์•ผ ์‹ค์ฒดํ™”ํ•  String์„ ์œ„ํ•œ ์ž๋ฆฌํ‘œ์ด์ž ๊ทธ String์ด ์ „๋‹ฌ๋  ๊ฒƒ์ด๋ผ๋Š” ์•ฝ์†์ด๋‹ค. (getLine์˜ ๊ฒฝ์šฐ ํ„ฐ๋ฏธ๋„์„ ํ†ตํ•ด ๋ฐ›์•„์„œ) ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๊ฐ€ IO String์„ ์กฐ์ž‘ํ•  ๋•Œ, ์šฐ๋ฆฌ๋Š” ์ด ๋ฏธ์ง€์˜ String์ด ๋„์ฐฉํ•˜๋ฉด ๋ฌด์—‡์„ ํ•˜๊ฒŒ ๋ ์ง€์— ๋Œ€ํ•œ ๊ณ„ํš์„ ์„ธ์šฐ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ๋ฐ, ์ด ์ ˆ์—์„œ๋Š” ๋‘ ๊ฐ€์ง€๋ฅผ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ์„ธ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋ช‡ ์žฅ ๋’ค์—์„œ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ์‹ค์ œ๋กœ ๊ฑฐ๊ธฐ์— ์—†๋Š” ๊ฐ’์„ ๋‹ค๋ฃฌ๋‹ค๋Š” ๋ฐœ์ƒ์€ ์ฒ˜์Œ์—๋Š” ์ด์ƒํ•˜๊ฒŒ ๋ณด์ธ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ ํƒœ์—ฐํ•˜๊ฒŒ ๊ทธ๋Ÿฐ ๊ฒƒ์„ ๋…ผ์˜ํ•œ ์ ์ด ์žˆ๋‹ค. mx๊ฐ€ Maybe Double ์ผ ๋•Œ, fmap (2*) mx๋Š” ๊ฐ’์ด ๊ฑฐ๊ธฐ์— ์žˆ๋‹ค๋ฉด ๊ทธ ๊ฐ’์„ ๋‘ ๋ฐฐ๋กœ ๋งŒ๋“œ๋Š”๋ฐ, ์ด fmap์€ ๊ทธ ๊ฐ’์ด ์‹ค์ œ๋กœ ์กด์žฌํ•˜๋Š”์ง€์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. 1 Maybe a์™€ IO a๋Š” a ํƒ€์ž…์˜ ๊ฐ’์— ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋กœ ๋‹ค๋ฅธ ์ด์œ ๋กœ ๊ฐ„์ ‘์ ์ธ ์ธต์„ ํ•˜๋‚˜ ๋”ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ Maybe์ฒ˜๋Ÿผ IO๋„ Functor๋ผ๋Š” ๊ฒŒ ์ƒˆ์‚ผ์Šค๋Ÿฌ์šด ์ผ์€ ์•„๋‹ˆ๋ฉฐ, fmap์€ ์ด ๊ฐ„์ ‘์ ์ธ ์ธต์„ ํ†ต๊ณผํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ ์ˆ˜๋‹จ์ด๋‹ค. ์šฐ์„  IO๊ฐ€ Functor๋ผ๋Š” ์‚ฌ์‹ค์„ ์ด์šฉํ•ด์„œ ์•ž์ ˆ์˜ interactiveSumming์—์„œ let ์ •์˜๋ฅผ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. interactiveSumming :: IO () interactiveSumming = do putStrLn "Choose two numbers:" mx <- readMaybe <$> getLine -- equivalently: fmap readMaybe getLine my <- readMaybe <$> getLine case (+) <$> mx <*> my :: Maybe Double of Just z -> putStrLn ("The sum of your numbers is " ++ show z) Nothing -> do putStrLn "Invalid number. Retrying..." interactiveSumming readMaybe <$> getLine์€ "์ผ๋‹จ getLine์ด ๋ฌธ์ž์—ด์„ ์ „๋‹ฌํ•˜๋ฉด ๊ทธ๊ฒŒ ๋ฌด์—‡์ด๋“  readMaybe๋ฅผ ์ ์šฉํ•˜๋ผ"๋กœ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์€ ๊นจ์ง€์ง€ ์•Š๋Š”๋‹ค. readMaybe <$> getLine ์ด๋ฉด์˜ ๊ฐ’์€ getLine์˜ ๊ฐ’๋งŒํผ์ด๋‚˜ ๋ถˆํˆฌ๋ช…ํ•˜๊ณ  ๊ทธ ํƒ€์ž…(์ด ๊ฒฝ์šฐ IO (Maybe Double))์€ ์ด๊ฒƒ์„ ์–ด๋–ค ๊ฒฐ์ •๋œ ๊ฐ’(๊ฐ€๋ น Just 3)์œผ๋กœ ๊ต์ฒดํ•˜๋Š” ๊ฒƒ์„<NAME>๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์„ ์œ„๋ฐ˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. IO๋Š” Functor์ด๋ฉด์„œ Applicative์ด๊ธฐ๋„ ํ•˜๋ฏ€๋กœ I/O ์•ก์…˜์— ์˜ํ•ด ์ „๋‹ฌ๋œ ๊ฐ’์„ ์กฐ์ž‘ํ•˜๋Š” ๋‘ ๋ฒˆ์งธ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•œ๋‹ค. ์ด๊ฒƒ์€ interactiveSumming์™€ ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ interactiveConcatenating ์•ก์…˜์„ ํ†ตํ•ด ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฒ„์ „์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (<*>)๋ฅผ ์ด์šฉํ•ด์„œ ์–ด๋–ป๊ฒŒ ๋‹จ์ˆœํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„๊นŒ? interactiveConcatenating :: IO () interactiveConcatenating = do putStrLn "Choose two strings:" sx <- getLine sy <- getLine putStrLn "Let's concatenate them:" putStrLn (sx ++ sy) ๋‹ค์Œ์€ (<*>)๋ฅผ ํ™œ์šฉํ•œ ๋ฒ„์ „์ด๋‹ค. interactiveConcatenating :: IO () interactiveConcatenating = do putStrLn "Choose two strings:" sz <- (++) <$> getLine <*> getLine putStrLn "Let's concatenate them:" putStrLn sz (++) <$> getLine <*> getLine์€ ๋‘ I/O ์•ก์…˜(๋‘ getLine)์œผ๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ I/O ์•ก์…˜์ด๋‹ค. ์ด ์•ก์…˜์ด ์‹คํ–‰๋˜๋ฉด ๊ทธ ๋‘ I/O ์•ก์…˜์ด ์‹คํ–‰๋˜๊ณ  ์ด๊ฒƒ๋“ค์ด ์ „๋‹ฌํ•˜๋Š” ๋ฌธ์ž์—ด๋“ค์ด ์—ฐ๊ฒฐ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•  ๊ฒƒ์€ (<*>)์ด ์—ฐ๊ฒฐํ•˜๋Š” ์•ก์…˜๋“ค ๊ฐ„์— ์‹คํ–‰ ์ˆœ์„œ๊ฐ€ ์ผ๊ด€๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์‹คํ–‰ ์ˆœ์„œ๋Š” I/O๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์ค‘์š”ํ•˜๋‹ค. ๊ทธ๋Ÿฐ ์˜ˆ์‹œ๋Š” ๋๋„ ์—†์ง€๋งŒ ์ด๋Ÿฐ ์งˆ๋ฌธ์œผ๋กœ ์‹œ์ž‘ํ•ด ๋ณด์ž. ์œ„ ์—์ œ์—์„œ ๋‘ ๋ฒˆ์งธ getLine์„ (take 3 <$> getLine)์œผ๋กœ ๊ต์ฒดํ•œ๋‹ค๋ฉด ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด๋“ค ์ค‘ ์–ด๋–ค ๊ฒƒ์ด ์„ธ ๊ธ€์ž๋กœ ์ž˜๋ฆด๊นŒ? (<*>)๋Š” ์•ก์…˜๋“ค์˜ ์ˆœ์„œ๋ฅผ ์กด์ค‘ํ•˜๋ฏ€๋กœ ๊ทธ ์•ก์…˜๋“ค์„ ๋‚˜์—ดํ•˜๋Š” ์ˆ˜๋‹จ๋„ ์ œ๊ณตํ•œ๋‹ค. ํŠนํžˆ ๋‚˜์—ด๋งŒ ํ•˜๋ฉด ๋˜๊ณ  ์ฒซ ๋ฒˆ์งธ ์•ก์…˜์˜ ๊ฒฐ๊ณผ์— ๊ด€์‹ฌ์ด ์—†๋‹ค๋ฉด \_ y -> y๋ฅผ ํ†ตํ•ด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํŒŒ๊ธฐํ•  ์ˆ˜ ์žˆ๋‹ค. GHCi> (\_ y -> y) <$> putStrLn "First!" <*> putStrLn "Second!" First! Second! ์ด ์‚ฌ์šฉ ํŒจํ„ด์€ ์›Œ๋‚™ ํ”ํ•ด์„œ ํŠน๋ณ„ํžˆ ์ด๋ฅผ ์œ„ํ•œ (*>) ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ๋‹ค. u *> v = (\_ y -> y) <$> u <*> v GHCi> :t (*>) (*>) :: Applicative f => f a -> f b -> f b GHCi> putStrLn "First!" *> putStrLn "Second!" First! Second! interactiveConcatenating ์˜ˆ์ œ์—๋„ ์‰ฝ๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. interactiveConcatenating :: IO () interactiveConcatenating = do putStrLn "Choose two strings:" sz <- (++) <$> getLine <*> getLine putStrLn "Let's concatenate them:" *> putStrLn sz ๋” ๋‚˜์•„๊ฐ€ ์ด๋ ‡๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. interactiveConcatenating :: IO () interactiveConcatenating = do sz <- putStrLn "Choose two strings:" *> ((++) <$> getLine <*> getLine) putStrLn "Let's concatenate them:" *> putStrLn sz ๊ฐ (*>)๋Š” do ๋ธ”๋ก์—์„œ ์•ก์…˜๋“ค์ด ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰๋˜๋„๋ก ํ•˜๋Š” ๋งˆ๋ฒ•์˜ ์ค„๋ฐ”๊ฟˆ ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ๋Œ€์ฒดํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ˆ™์ง€ํ•˜์ž. ์‚ฌ์‹ค ๊ต์ฒด๋œ ์ค„๋ฐ”๊ฟˆ ๋“ค์€ (*>)์˜ ํŽธ์˜ ๋ฌธ๋ฒ•์ผ ๋ฟ์ด๋‹ค. ์•ž์„œ ๋งํ•˜๊ธฐ๋ฅผ ํŽ‘ํ„ฐ๋Š” ๊ทธ ์•ˆ์˜ ๊ฐ’์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ„์ ‘์ธต์„ ํ•˜๋‚˜ ๋Š˜๋ฆฐ๋‹ค๊ณ  ํ•˜์˜€๋‹ค. ์ด๋Ÿฌํ•œ ๊ด€์ฐฐ์„ ๋’ค์ง‘์–ด๋ณด๋ฉด ๊ฐ„์ ‘์ธต์€ ๋ฌธ๋งฅ(context)์— ์˜ํ•ด ์œ ๋ฐœ๋˜๊ณ , ๊ทธ ๋ฌธ๋งฅ ์•ˆ์—์„œ ๊ฐ’์ด ๋ฐœ๊ฒฌ๋˜๋Š” ๊ฒƒ์ด๋‹ค. IO์˜ ๊ฒฝ์šฐ ๊ฐ„์ ‘์€ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋  ๋•Œ์—๋งŒ ๊ฐ’์ด ๊ฒฐ์ •๋œ๋‹ค๋Š” ๊ฒƒ์ด๊ณ , ๋ฌธ๋งฅ์€ ์ด ๊ฐ’์„ ์ƒ์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ ์ผ๋ จ์˜ ๋ช…๋ น๋“ค๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. (getLine์˜ ๊ฒฝ์šฐ ๊ทธ ๋ช…๋ น์€ "ํ„ฐ๋ฏธ๋„์—์„œ ํ…์ŠคํŠธ ํ•œ ์ค„์„ ๋นผ์˜ค๋Š” ๊ฒƒ"์ด๋‹ค) ์ด๋Ÿฐ ๊ด€์ ์—์„œ (<*>)๋Š” ๋‘ functorial ๊ฐ’์„ ์ทจํ•ด์„œ ๊ทธ ์•ˆ์˜ ๊ฐ’๋“ค๋ฟ ์•„๋‹ˆ๋ผ ๋ฌธ๋งฅ๋“ค ์ž์ฒด๋„ ํ•ฉ์„ฑํ•˜๊ฒŒ ๋œ๋‹ค. IO์˜ ๊ฒฝ์šฐ ๋ฌธ๋งฅ์˜ ํ•ฉ์„ฑ์€ ํ•œ I/O ์•ก์…˜์˜ ๋ช…๋ น๋“ค์„ ๋‹ค๋ฅธ ์•ก์…˜์˜ ๋ช…๋ น๋“ค์— ๋ง๋ถ™์—ฌ์„œ ์•ก์…˜๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. ์ด์ œ ์‹œ์ž‘์ผ ๋ฟ ์ด๋ฒˆ ์žฅ์€ ๋‹ค์†Œ ์ •์‹ ์ด ์—†์—ˆ๋‹ค. ํ•ต์‹ฌ์„ ์š”์•ฝํ•ด ๋ณด์ž. Applicative๋Š” ์ ์šฉ์„ฑ ํŽ‘ํ„ฐ๋ฅผ ์œ„ํ•œ Functor์˜ ์„œ๋ธŒ ํด๋ž˜์Šค๋กœ์„œ, ํŽ‘ํ„ฐ๋ฅผ ๋– ๋‚˜์ง€ ์•Š๊ณ  ํ•จ์ˆ˜ ์ ์šฉ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํŽ‘ํ„ฐ๋‹ค. Applicative์˜ (<*>) ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์ค‘ ์ธ์ž๋ฅผ ์œ„ํ•œ fmap ์ผ๋ฐ˜ํ™”๋กœ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. IO a๋Š” a ํƒ€์ž…์˜ ์‹ค์žฌํ•˜๋Š” ๊ฐ’์ด ์•„๋‹ˆ๋ผ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋  ๋•Œ ์‹คํ˜„๋  a ๊ฐ’์˜ ์ž๋ฆฌํ‘œ์ด์ž ์ด ๊ฐ’์ด ์–ด๋–ค ์ˆ˜๋‹จ์„ ํ†ตํ•ด ์ „๋‹ฌ๋  ๊ฒƒ์ด๋ผ๋Š” ์•ฝ์†์ด๋‹ค. ์ด๋กœ์จ I/O ์•ก์…˜์„ ๋‹ค๋ฃฐ ๋•Œ๋„ ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์„ ๋งŒ์กฑํ•˜๊ฒŒ ๋œ๋‹ค. IO๋Š” ํŽ‘ ํ„ฐ์ด๊ณ  ํŠนํžˆ Applicative์˜ ์ธ์Šคํ„ด์Šค๋กœ์„œ, I/O ์•ก์…˜์— ์˜ํ•ด ์ƒ์‚ฐ๋œ ๊ฐ’์„ ๊ทธ ๋น„๊ฒฐ์ •์„ฑ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•œ๋‹ค. functorial ๊ฐ’์€ ์–ด๋–ค ๋ฌธ๋งฅ ์•ˆ์˜ ๊ฐ’๋“ค๋กœ ์ด๋ฃจ์–ด์กŒ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. (<$>) ์—ฐ์‚ฐ์ž(์ฆ‰ fmap)๋Š” ๊ทธ ๋ฌธ๋งฅ์„ ํ†ต๊ณผํ•ด์„œ ๊ธฐ์ €์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•œ๋‹ค. (<*>) ์—ฐ์‚ฐ์ž๋Š” ๋‘ functorial ๊ฐ’์˜ ๋ฌธ๋งฅ๋“ค๊ณผ ๊ธฐ์ € ๊ฐ’๋“ค์„ ํ•ฉ์„ฑํ•œ๋‹ค. IO์˜ ๊ฒฝ์šฐ (<*>)์™€ (*>)๋Š” I/O ์•ก์…˜๋“ค์„ ์—ฐ๊ฒฐํ•จ์œผ๋กœ์จ ๋ฌธ๋งฅ๋“ค์„ ํ•ฉ์„ฑํ•œ๋‹ค. do ๋ธ”๋ก์˜ ์—ญํ• ์€ ๋‹จ์ง€ (*>)์˜ ํŽธ์˜ ๋ฌธ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ๋งŽ์€ ๋ถ€๋ถ„์„<NAME>๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, do ๋ธ”๋ก์—๋Š” ์•„์ง ํฐ ์ˆ˜์ˆ˜๊ป˜๋ผ๊ฐ€ ์ˆจ์–ด์žˆ๋‹ค. ์™ผ์ชฝ ํ™”์‚ดํ‘œ๋Š” ๋ฌด์—‡์„ ํ• ๊นŒ? ์ด๋Ÿฐ do ๋ธ”๋ก์€... sx <- getLine ๋งˆ์น˜ IO ๋ฌธ๋งฅ ํ•˜์—์„œ getLine์ด ์ƒ์„ฑํ•œ ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์— ๊ด€ํ•œ ๋…ผ์˜ ๋•์— ์šฐ๋ฆฌ๋Š” ์ด์ œ ๊ทธ๊ฒƒ์ด ํ™˜์ƒ์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ทธ ์ด๋ฉด์—์„œ๋Š” ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š” ๊ฑธ๊นŒ? ๋‚˜์ค‘์— ์•Œ๊ฒŒ ๋  ํ…Œ๋‹ˆ ์ง€๊ธˆ์€ ์ž์œ ๋กญ๊ฒŒ ๊ณ ๋ฏผํ•ด ๋ณด์ž. ๋…ธํŠธ ๋‘ ์ƒํ™ฉ์˜ ์ฃผ์š” ์ฐจ์ด์ ์€ Maybe์˜ ๊ฒฝ์šฐ ๋น„๊ฒฐ์ •์„ฑ์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ผ ๋ฟ์ด๋ฉฐ, mx ๋’ค์— ์‹ค์ œ Double์ด ์˜ค๊ฒŒ ๋ ์ง€ ๋ฏธ๋ฆฌ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋” ์ •ํ™•ํžˆ ๋งํ•˜์ž๋ฉด, mx์˜ ๊ฐ’์ด I/O์— ์˜์กดํ•˜์ง€ ์•Š์•„์•ผ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. โ†ฉ 2 ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ ์›๋ฌธ : http://en.wikibooks.org/wiki/Haskell/Understanding_monads ์ •์˜ ๋™๊ธฐ: Maybe ํƒ€์ž… ํด๋ž˜์Šค Monad์™€ Applicative ๊ณ„์‚ฐ์ด๋ผ๋Š” ๊ฐœ๋… Notions of Computation ๋ชจ๋‚˜๋“œ์˜ ๋ฒ•์น™ ์ค‘๋ฆฝ ์› neutral element์œผ๋กœ์„œ์˜ return bind์˜ ๊ฒฐํ•ฉ๋ฒ•์น™ ๋ชจ๋‚˜ ๋”• ํ•ฉ์„ฑ ๋ชจ๋‚˜๋“œ์™€ ๋ฒ”์ฃผ๋ก  liftM๊ณผ ๊ทธ ์นœ๊ตฌ๋“ค ํ•˜์Šค์ผˆ์—์„œ ๋ชจ๋‚˜๋“œ๋Š” ๋งค์šฐ ์œ ์šฉํ•˜์ง€๋งŒ ์ฒ˜์Œ์—๋Š” ๊ทธ ๊ฐœ๋…์ด ๊ฝค ์–ด๋ ต๋‹ค. ๋ชจ๋‚˜๋“œ๋Š” ์ˆ˜๋งŽ์€ ์‘์šฉ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ๋žŒ๋“ค์€ ๋ชจ๋‚˜๋“œ๋ฅผ ํŠน์ • ๊ด€์ ์—์„œ๋งŒ ์„ค๋ช…ํ•˜๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋Š”๋ฐ, ๊ทธ๋Ÿฌ๋ฉด ์—ฌ๋Ÿฌ๋ถ„์ด ๋ชจ๋‚˜๋“œ๋ฅผ ์™„๋ฒฝํžˆ ์ดํ•ดํ•˜๋Š” ๋ฐ ํ˜ผ๋ž€์„ ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. ์—ญ์‚ฌ์ ์œผ๋กœ ๋ณด๋ฉด ๋ชจ๋‚˜๋“œ๋Š” ํ•˜์Šค์ผˆ์—์„œ ์ž…์ถœ๋ ฅ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋„์ž…๋˜์—ˆ๋‹ค. ๋ฏธ๋ฆฌ ์ •์˜๋œ ์‹คํ–‰ ์ˆœ์„œ๋Š” ํŒŒ์ผ ์ฝ๊ณ  ์“ฐ๊ธฐ ๊ฐ™์€ ์ž‘์—…์— ์ค‘๋Œ€ํ•œ ์‚ฌํ•ญ์ด๊ณ  ๋ชจ๋‚˜ ๋”• ์—ฐ์‚ฐ์€ ๋‚ด์žฌ๋œ inherent ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฅธ๋‹ค. ์ „์— ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ์—์„œ do ํ‘œ๊ธฐ๋ฅผ ์ด์šฉํ•œ ์—ฐ์‡„์™€ IO๋ฅผ ๋…ผ์˜ํ•œ ๋ฐ” ์žˆ๋‹ค. ์‚ฌ์‹ค do๋Š” ๋ชจ๋‚˜๋“œ์˜ ํŽธ์˜ ๊ตฌ๋ฌธ์ผ ๋ฟ์ด๋‹ค. ๋ชจ๋‚˜๋“œ๋Š” ์ž…์ถœ๋ ฅ์— ํ•œ์ •๋˜์ง€ ์•Š๋Š”๋‹ค. ๋ชจ๋‚˜๋“œ๋Š” ์˜ˆ์™ธ, ์ƒํƒœ, ๋น„๊ฒฐ์ •์„ฑ non-determinism, ์—ฐ์†์„ฑ continuation, ์ฝ”๋ฃจํ‹ด, ๊ทธ ์™ธ์— ์ˆ˜๋งŽ์€ ๊ฒƒ์„ ์ง€์›ํ•œ๋‹ค. ์‚ฌ์‹ค ๋ชจ๋‚˜๋“œ์˜ ๋‹ค์žฌ๋‹ค๋Šฅํ•จ ๋•์— ์ด ์ค‘ ์–ด๋Š ๊ฒƒ๋„ ํ•˜์Šค ์ผˆ ์–ธ์–ด์˜ ์ผ๋ถ€๋กœ ๋‚ด์žฅ๋  ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ ๋Œ€์‹  ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ •์˜๋˜์–ด ์žˆ๋‹ค. ์ •์˜ ๋ชจ๋‚˜๋“œ๋Š” ์„ธ ๊ฐ€์ง€์— ์˜ํ•ด ์ •์˜๋œ๋‹ค. ํƒ€์ž… ์ƒ์„ฑ์ž M return ํ•จ์ˆ˜ 1 "bind"๋ผ ๋ถ€๋ฅด๋Š” (>>=) ์—ฐ์‚ฐ์ž ์œ„์˜ ํ•จ์ˆ˜์™€ ์—ฐ์‚ฐ์ž๋Š” Monad ํƒ€์ž… ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์ด๋ฉฐ ๋‹ค์Œ์˜ ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค. return :: a -> M a (>>=) :: M a -> ( a -> M b) -> M b ๊ทธ๋ฆฌ๊ณ  ๋‚˜์ค‘์— ์„ค๋ช…ํ•  ์„ธ ๊ฐœ์˜ ๋ฒ•์น™์„ ๋”ฐ๋ผ์•ผ ํ•œ๋‹ค. ๊ตฌ์ฒด์ ์ธ ์˜ˆ์‹œ๋กœ Maybe ๋ชจ๋‚˜๋“œ๋ฅผ ๋ณด์ž. ํƒ€์ž… ์ƒ์„ฑ์ž๋Š” M = Maybe์ด๊ณ  return๊ณผ (>>=)๋Š” ์ด๋ ‡๊ฒŒ ์ •์˜๋œ๋‹ค. return :: a -> Maybe a return x = Just x (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b m >>= g = case m of Nothing -> Nothing Just x -> g x Maybe๋Š” ๋ชจ๋‚˜๋“œ๊ณ  return์€ ํ•˜๋‚˜์˜ ๊ฐ’์„ Just๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (>>=)๋Š” m :: Maybe a ๊ฐ’๊ณผ g :: a -> Maybe b ํ•จ์ˆ˜๋ฅผ ์ทจํ•œ๋‹ค. m์ด Nothing ์ด๋ฉด ํ•˜๋Š” ์ผ ์—†์ด ๊ฒฐ๊ณผ๋„ Nothing์ด๋‹ค. ๋ฐ˜๋Œ€๋กœ Just x์˜ ๊ฒฝ์šฐ Just๋กœ ๊ฐ์‹ผ x์— g๊ฐ€ ์ ์šฉ๋˜๊ณ  Maybe b๋ฅผ ๊ฒฐ๊ณผ๋กœ ๋‚ด๋†“๋Š”๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” g๊ฐ€ x์— ํ•˜๋Š” ์ผ์— ๋”ฐ๋ผ Nothing ์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ์ข…ํ•ฉํ•˜๋ฉด, m์— ํฌํ•จ๋œ ๊ฐ’์ด ์žˆ์œผ๋ฉด ์ด ๊ฐ’์— g๋ฅผ ์ ์šฉํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ Maybe ๋ชจ๋‚˜๋“œ์— ๋‹ค์‹œ ๋“ค์–ด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. return๊ณผ (>>=)๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๋Š” ์ฒซ๊ฑธ์Œ์€ ์–ด๋–ค ๊ฐ’๊ณผ ์ธ์ˆ˜๊ฐ€ ๋ชจ๋‚˜๋”•์ธ์ง€ ์•„๋‹Œ์ง€ ์ถ”์ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ค๋ฅธ ๋งŽ์€ ๊ฒฝ์šฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๊ฐ€ ๊ทธ ๊ณผ์ •์˜ ์•ˆ๋‚ด์ž๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ๋™๊ธฐ: Maybe (>>=)์™€ Maybe ๋ชจ๋‚˜๋“œ์˜ ์œ ์šฉํ•จ์„ ๋‹ค์Œ ์˜ˆ์ œ์—์„œ ์‚ดํŽด๋ณด์ž. ๋‘ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฐ€๊ณ„๋„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ฐ€์ •ํ•œ๋‹ค. father :: Person -> Maybe Person mother :: Person -> Maybe Person ์ด ํ•จ์ˆ˜๋“ค์€ ๋ˆ„๊ตฐ๊ฐ€์˜ ์•„๋ฒ„์ง€๋‚˜ ์–ด๋จธ๋‹ˆ์˜ ์ด๋ฆ„์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๊ทธ๋Ÿฐ ์ •๋ณด๊ฐ€ ์—†์„ ๋•Œ Maybe ๋•์— ํ”„๋กœ๊ทธ๋žจ์„ ๊ณ ์žฅ ๋‚ด๋Š” ๋Œ€์‹  Nothing ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์„ ๊ฒฐํ•ฉํ•˜์—ฌ ํ• ์•„๋ฒ„์ง€๋“ค์„ ์ฐพ์•„๋ณด์ž. ๋‹ค์Œ ํ•จ์ˆ˜๋Š” ์™ธํ• ์•„๋ฒ„์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค. maternalGrandfather :: Person -> Maybe Person maternalGrandfather p = case mother p of Nothing -> Nothing Just mom -> father mom ๋‹ค์Œ ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์นœํ• ์•„๋ฒ„์ง€์™€ ์™ธํ• ์•„๋ฒ„์ง€๊ฐ€ ๋ชจ๋‘ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. bothGrandfathers :: Person -> Maybe (Person, Person) bothGrandfathers p = case father p of Nothing -> Nothing Just dad -> case father dad of Nothing -> Nothing Just gf1 -> -- found first grandfather case mother p of Nothing -> Nothing Just mom -> case father mom of Nothing -> Nothing Just gf2 -> -- found second grandfather Just (gf1, gf2) ๋„ˆ๋ฌด ๊ธธ๊ณ  ๋ณต์žกํ•˜๋‹ค! ๊ฐ๊ฐ์˜ ์งˆ์˜๋Š” Nothing์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด์„œ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๊ณ  ๊ทธ๋Ÿด ๊ฒฝ์šฐ ์ „์ฒด ํ•จ์ˆ˜๋„ ์‹คํŒจํ•ด์•ผ ํ•œ๋‹ค. ๋ถ„๋ช…ํžˆ Nothing์„ ์“ฐ๊ณ  ๋˜ ์“ฐ๋Š” ๊ฒƒ๋ณด๋‹ค ๋‚˜์€ ๋ฐฉ๋ฒ•์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๋ชจ๋‚˜๋“œ๊ฐ€ ๋ฐ”๋กœ ๊ทธ ๋ฐฉ๋ฒ•์ด๋‹ค. ์˜ˆ์ปจ๋Œ€ ์™ธํ• ์•„๋ฒ„์ง€์— ์ ‘๊ทผํ•˜๋Š” ํ•จ์ˆ˜๋Š” (>>=) ์—ฐ์‚ฐ์ž์™€ ์ •ํ™•ํžˆ ๋™์ผํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ์ด๋ ‡๊ฒŒ ๋‹ค์‹œ ์“ธ ์ˆ˜ ์žˆ๋‹ค. maternalGrandfather p = mother p >>= father ๋žŒ๋‹ค ํ‘œํ˜„์‹๊ณผ return์˜ ๋„์›€์„ ๋ฐ›์•„ ๋‘ ํ• ์•„๋ฒ„์ง€๋ฅผ ์ฐพ๋Š” ํ•จ์ˆ˜๋„ ๋‹ค์‹œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. bothGrandfathers p = father p >>= (\dad -> father dad >>= (\gf1 -> mother p >>= -- this line works as "\_ -> mother p", but naming gf1 allows later return (\mom -> father mom >>= (\gf2 -> return (gf1, gf2) )))) ์ค‘์ฒฉ๋œ ๋žŒ๋‹ค ํ‘œํ˜„์‹์ด ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ˆˆ์—ฌ๊ฒจ๋ณผ ์ ์€ (>>=) ๋•์— ๋ชจ๋“  Nothing์„ ๋‚˜์—ดํ•˜๋Š” ๋ถ€๋ถ„์„ ์—†์• ์„œ ์ฝ”๋“œ์˜ ๊ด€์‹ฌ ์žˆ๋Š” ๋ถ€๋ถ„์—๋งŒ ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ข€ ๋” ์ •ํ™•ํžˆ ํ•˜์ž๋ฉด, father p์˜ ๊ฒฐ๊ณผ๋Š” ๋ชจ๋‚˜ ๋”• ๊ฐ’์ด๋‹ค. ๊ทธ ๊ฐ’์€ p์˜ ์•„๋ฒ„์ง€๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์žˆ๋Š๋ƒ ์—†๋Š๋ƒ์— ๋”ฐ๋ผ Just dad ๋˜๋Š” Nothing์ด๋‹ค. father ํ•จ์ˆ˜๊ฐ€ ๋ชจ๋‚˜๋“œ ๋”• ๊ฐ’์ด ์•„๋‹Œ ์ •๊ทœ ๊ฐ’์„ ์ทจํ•˜๊ธฐ ๋•Œ๋ฌธ์— (>>=)๋Š” ๋ชจ๋‚˜๋”•์ด ์•„๋‹Œ ๊ฐ’์ธ p์˜ dad๋ฅผ father์—๊ฒŒ ์ธ์ž๋กœ ์ „๋‹ฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  father dad์˜ ๊ฒฐ๊ณผ๋Š” ๋‹ค์‹œ ๋ชจ๋‚˜ ๋”• ๊ฐ’์ด๊ณ , ์ด ๊ณผ์ •์€ ๊ณ„์†๋œ๋‹ค. ์ฆ‰ (>>=)๋Š” ๋ชจ๋‚˜๋“œ๋ฅผ ๋– ๋‚˜์ง€ ์•Š๊ณ ๋„ ํ•จ์ˆ˜์— ๋ชจ๋‚˜๋”•์ด ์•„๋‹Œ ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. Maybe ๋ชจ๋‚˜๋“œ์˜ ๋ชจ๋‚˜ ๋”• ๊ด€์ ์€ ๊ทธ ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์„์ง€์— ๋Œ€ํ•œ ๋ถˆํ™•์‹ค์„ฑ์ด๋‹ค. ํƒ€์ž… ํด๋ž˜์Šค ํ•˜์Šค์ผˆ์—์„œ๋Š” Monad ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ๋ชจ๋‚˜๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. Monad๋Š” Control.Monad ๋ชจ๋“ˆ์˜ ์ผ๋ถ€์ด๋ฉฐ Prelude์— ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” ๋‹ค์Œ ๋ฉ”์„œ๋“œ๋“ค์„ ๊ฐ€์ง„๋‹ค. class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b fail :: String -> m a return๊ณผ bind ์™ธ์—๋„ (>>)์™€ fail์ด๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ์ด ๋‘˜๋„ ๊ธฐ๋ณธ ๊ตฌํ˜„์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๊ตณ์ด ์ •์˜ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. (>>) ์—ฐ์‚ฐ์ž๋Š” "then"์ด๋ผ ์ฝ์œผ๋ฉฐ ๊ทธ์ € ํŽธ์˜๋ฅผ ์œ„ํ•œ ๊ฒƒ์œผ๋กœ ๋Œ€๊ฐœ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„๋œ๋‹ค. m >> n = m >>= \_ -> n (>>)๋Š” ๋‘ ๋ชจ๋‚˜ ๋”• ์•ก์…˜์„ ์—ฐ๊ฒฐํ•˜๋Š”๋ฐ ๋‘ ๋ฒˆ์งธ ์•ก์…˜์ด ์ฒซ ๋ฒˆ์งธ ์•ก์…˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. IO ๊ฐ™์€ ๋ชจ๋‚˜๋“œ์—์„œ๋Š” ํ”ํ•œ ์ผ์ด๋‹ค. printSomethingTwice :: String -> IO () printSomethingTwice str = putStrLn str >> putStrLn str fail ํ•จ์ˆ˜๋Š” do ํ‘œ๊ธฐ ๋‚ด์—์„œ์˜ ํŒจํ„ด ๋งค์นญ ์‹คํŒจ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๊ธฐ์ˆ ์ ์œผ๋กœ ์–ด์ฉ” ์ˆ˜ ์—†์ด ํ•„์š”ํ•  ๋ฟ ๋ชจ๋‚˜๋“œ์™€๋Š” ์•„๋ฌด ์ƒ๊ด€์ด ์—†๋‹ค. fail์„ ์ฝ”๋“œ์—์„œ ์ง์ ‘ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. Monad์™€ Applicative Applicative๊ฐ€ Monad์˜ ์Šˆํผํด๋ž˜์Šค์ด๋ฉฐ ์ด์— ๋”ฐ๋ฅธ ์ฃผ๋ชฉํ•  ๋งŒํ•œ ๊ฒฐ๊ณผ๋“ค์ด ์žˆ๋‹ค๋Š” ๊ฑธ ์งš๊ณ  ๋„˜์–ด๊ฐ€์•ผ๊ฒ ๋‹ค. 2 ๋จผ์ € ๋ชจ๋“  Monad๋Š” Functor ์ด์ž Applicative์ด๊ณ , ๋”ฐ๋ผ์„œ ๋ชจ๋‚˜๋“œ์—๋„ fmap, pure, (<*>)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ ์‚ฌ์‹ค Monad ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•˜๋ ค๋ฉด Functor์™€ Applicative ์ธ์Šคํ„ด์Šค๋„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด ์žฅ์˜ ๋’ค์—์„œ ๋…ผ์˜ํ•œ๋‹ค. ์„ธ ๋ฒˆ์งธ๋กœ ์„œ๋ฌธ์„ ์ฝ์–ด๋ดค๋‹ค๋ฉด return๊ณผ (>>)์˜ ํƒ€์ž…๊ณผ ์—ญํ• ์ด ์นœ์ˆ™ํ•  ๊ฒƒ์ด๋‹ค... (*>) :: Applicative f => f a -> f b -> f b (>>) :: Monad m => m a -> m b -> m b pure :: Applicative f => a -> f a return :: Monad m => a -> m a (*>)์™€ (>>)์˜ ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ์ œ์•ฝ์ด Applicative์—์„œ Monad๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์‚ฌ์‹ค ๋‘ ๋ฉ”์„œ๋“œ ๊ฐ„์˜ ์ฐจ์ด๋Š” ์ด๊ฒƒ๋ฟ์ด๋‹ค. Monad๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ๋Š” ์–ธ์ œ๋“  (*>)์™€ (>>)๋ฅผ ์„œ๋กœ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค. pure์™€ return๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ์‚ฌ์‹ค Applicative ์ธ์Šคํ„ด์Šค์— pure์˜ ๋ณ„๋„ ์ •์˜๊ฐ€ ์žˆ๋‹ค๋ฉด return์„ ๊ตฌํ˜„ํ•  ํ•„์š”๋„ ์—†๋Š”๋ฐ, return์˜ ๊ธฐ๋ณธ ๊ตฌํ˜„์€ return = pure์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ณ„์‚ฐ์ด๋ผ๋Š” ๊ฐœ๋… Notions of Computation Maybe๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ํ‹€์— ๋ฐ•ํžŒ ์ฝ”๋“œ๋ฅผ ์—†์• ๋Š” ๋ฐ (>>=)์™€ return์ด ์•„์ฃผ ํŽธ๋ฆฌํ•˜๋‹ค๋Š” ๊ฑธ ๋ดค์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฑธ๋กœ๋Š” ๋ชจ๋‚˜๋“œ๊ฐ€ ์™œ ๊ทธํ† ๋ก ์ค‘์š”ํ•œ์ง€ ๋‚ฉ๋“ํ•˜๊ธฐ์—๋Š” ์ถฉ๋ถ„ํ•˜์ง€ ์•Š๋‹ค. ๋‘ ํ• ์•„๋ฒ„์ง€๋ฅผ ์ฐพ๋Š” ํ•จ์ˆ˜๋ฅผ, do ํ‘œ๊ธฐ์— ๊ด„ํ˜ธ์™€ ์„ธ๋ฏธ์ฝœ๋ก ์„ ๋ถ™์—ฌ ๋‹ค์‹œ ์ž‘์„ฑํ•˜๋ฉด์„œ ๋ชจ๋‚˜๋“œ ๊ณต๋ถ€๋ฅผ ๊ณ„์†ํ•˜๊ฒ ๋‹ค. ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ๊ฒฝํ—˜์ด ์žˆ์œผ๋ฉด ๊ทธ ์–ธ์–ด๊ฐ€ ์—ฐ์ƒ๋  ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค. bothGrandfathers p = do { dad <- father p; gf1 <- father dad; mom <- mother p; gf2 <- father mom; return (gf1, gf2); } ์ด ์ฝ”๋“œ๊ฐ€ ๋ช…๋ นํ˜• ์–ธ์–ด์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ์ด์œ ๋Š” ์ •๋ง ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์‚ฌ์‹ค ์ด ๋ช…๋ นํ˜• ์–ธ์–ด๋Š” ์˜ˆ์™ธ๋ฅผ ์ง€์›ํ•œ๋‹ค. father์™€ mother๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•˜๋Š” ๋ฐ ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ๋Š” ํ•จ์ˆ˜๋‹ค. ์ฆ‰ ์˜ˆ์™ธ๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋Ÿฐ ์ผ์ด ๋ฐœ์ƒํ•˜๋ฉด do ๋ธ”๋ก ์ „์ฒด๊ฐ€ ์‹คํŒจํ•œ๋‹ค. ์ฆ‰ ์˜ˆ์™ธ๋ฅผ ์ผ์œผํ‚ค๋ฉฐ ์ข…๋ฃŒํ•œ๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด ํ‘œํ˜„์‹ father p๋Š” Maybe Person ํƒ€์ž…์ด๋ฉฐ ํ•ด์„๋˜๊ธฐ๋กœ๋Š” ๋ช…๋ นํ˜• ์–ธ์–ด์—์„œ Person์„ ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•˜๋‚˜์˜ ๋ช…๋ น๋ฌธ์ฒ˜๋Ÿผ ํ•ด์„๋œ๋‹ค. ์ด ๋ง์€ ๋ชจ๋“  ๋ชจ๋‚˜๋“œ์— ๋Œ€ํ•ด ์„ฑ๋ฆฝํ•œ๋‹ค. M a ํƒ€์ž…์˜ ๊ฐ’์€ ๋ช…๋ นํ˜• ์–ธ์–ด์—์„œ a ํƒ€์ž…์˜ ๊ฐ’์„ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ช…๋ น๋ฌธ์œผ๋กœ ํ•ด์„๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์–ธ์–ด์˜ ์˜๋ฏธ semantic๋Š” ๋ชจ๋‚˜๋“œ M์— ์˜ํ•ด ๊ฒฐ์ •๋œ๋‹ค. 3 ์ด๋Ÿฐ ํ•ด์„์— ๋”ฐ๋ฅด๋ฉด bind ์—ฐ์‚ฐ์ž (>>=)๋Š” ์„ธ๋ฏธ์ฝœ๋ก ์˜ ํ•จ์ˆ˜ ๋ฒ„์ „์ผ ๋ฟ์ด๋‹ค. let ํ‘œํ˜„์‹์„ ํ•จ์ˆ˜ ์ ์šฉ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋“ฏ์ด let x = foo in x + 3๋Š” ๋‹ค์Œ์— ๋Œ€์‘๋œ๋‹ค (\x -> x + 3) foo ํ• ๋‹น๊ณผ ์„ธ๋ฏธ์ฝœ๋ก ์€ bind ์—ฐ์‚ฐ์ž๋กœ ์žฌ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. x <- foo; return (x + 3)๋Š” ๋‹ค์Œ์— ๋Œ€์‘๋œ๋‹ค foo >>= (\x -> return (x + 3)) return ํ•จ์ˆ˜๋Š” ๊ฐ’ a๋ฅผ M a๋กœ ์ „์ด์‹œํ‚จ๋‹ค. M a๋Š” ๋ชจ๋‚˜๋“œ M์— ๋Œ€์‘ํ•˜๋Š” ๋ช…๋ นํ˜• ์–ธ์–ด์˜ ๋ช…๋ น๋ฌธ๊ณผ ๊ฐ™๋‹ค. ๋ช…๋ นํ˜• ์–ธ์–ด์˜ ์—ฌ๋Ÿฌ ์˜๋ฏธ(semantic)๋Š” ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๋ชจ๋‚˜๋“œ์— ๋Œ€์‘ํ•œ๋‹ค. ๋‹ค์Œ ํ‘œ๋Š” ๋ชจ๋“  ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋ฐ˜๋“œ์‹œ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ณ ์ „์ ์ธ ์‚ฌํ•ญ์„ ์„ ๋ณ„ํ•œ ํ‘œ๋‹ค. ๋ชจ๋‚˜๋“œ์— ๊น”๋ฆฐ ๋ฐœ์ƒ์ด ์•„์ง๋„ ๋ช…ํ™•ํ•˜์ง€ ์•Š๋‹ค๋ฉด, ๋’ค์— ์ด์–ด์ง€๋Š” ์žฅ๋“ค์˜ ์˜ˆ์ œ๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ข‹์€ ๋„๊ตฌ์ƒ์ž๋„ ์–ป๊ณ  ์—ฌ๋Ÿฌ ๋ชจ๋‚˜๋“œ ๋’ค์— ์ˆจ์€ ๊ณตํ†ต๋œ ์ถ”์ƒํ™”๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๋ชจ๋‚˜๋“œ ๋ช…๋ นํ˜• ์–ธ์–ด์—์„œ์˜ ์˜๋ฏธ semantic ์œ„ํ‚ค ์ฑ… ๊ณผ๋ชฉ Maybe ์˜ˆ์™ธ(์ต๋ช…) ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ/Maybe Error ์˜ˆ์™ธ(์˜ค๋ฅ˜ ๋‚ด์šฉ์ด ์žˆ์Œ) ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ/Error State ์ „์—ญ ์ƒํƒœ global state ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ/State IO ์ž…์ถœ๋ ฅ ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ/IO [](๋ฆฌ์ŠคํŠธ) ๋น„๊ฒฐ์ •์„ฑ ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ/List Reader ํ™˜๊ฒฝ์„ค์ • Environment ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ/Reader Writer ๋กœ ๊ฑฐ Logger ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ/Writer ๋”์šฑ์ด, ์„œ๋กœ ๋‹ค๋ฅธ ์ด๋“ค ์˜๋ฏธ semantic๋Š” ๊ผญ ๋”ฐ๋กœ ์จ์•ผ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ๋ช‡๋ช‡ ์žฅ์—์„œ ๋ณด๊ฒ ์ง€๋งŒ ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ monad transformer๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ชจ๋‚˜๋“œ๋“ค์„ ํ•ฉ์„ฑํ•˜๊ณ , ์ผ์น˜์‹œํ‚ค๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ๋ชจ๋‚˜๋“œ์˜ semantic์„ ๋‹จ์ผ ๋ชจ๋‚˜๋“œ๋กœ ํ•ฉ์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ชจ๋‚˜๋“œ์˜ ๋ฒ•์น™ ํ•˜์Šค์ผˆ์—์„œ Monad ํƒ€์ž… ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋Š”(๊ทธ๋ฆฌ๊ณ  (>>=)์™€ return์˜ ๋ชจ๋“  ๊ตฌํ˜„์€) ๋‹ค์Œ์˜ ์„ธ ๋ฒ•์น™์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค. m >>= return = m -- ์šฐ๋‹จ ์œ„์›์˜ ๋ฒ•์น™(right unit) return x >>= f = f x -- ์ขŒ ๋‹จ์œ„์›์˜ ๋ฒ•์น™(left unit) (m >>= f) >>= g = m >>= (\x -> f x >>= g) -- ๊ฒฐํ•ฉ๋ฒ•์น™(associativity) ์ค‘๋ฆฝ ์› neutral element์œผ๋กœ์„œ์˜ return return์˜ ๋™์ž‘์€ ์ขŒ ๋‹จ์œ„์›(left unit)์˜ ๋ฒ•์น™๊ณผ ์šฐ๋‹จ ์œ„์›(right unit)์˜ ๋ฒ•์น™์— ์˜ํ•ด ๊ธฐ์ˆ ๋œ๋‹ค. ์ด ๋ฒ•์น™๋“ค์€ return์ด ์•„๋ฌด ๊ณ„์‚ฐ๋„ ํ•˜์ง€ ์•Š์Œ์„ ๋œปํ•œ๋‹ค. return์€ ๊ทธ์ € ๊ฐ’์„ ๋ณด๊ด€ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ์ฝ”๋“œ๋Š” maternalGrandfather p = do mom <- mother p gf <- father mom return gf ์šฐ๋‹จ ์œ„์›์˜ ๋ฒ•์น™์— ์˜ํ•ด ๋‹ค์Œ๊ณผ ์ •ํ™•ํžˆ ๊ฐ™๋‹ค. maternalGrandfather p = do mom <- mother p father mom bind์˜ ๊ฒฐํ•ฉ๋ฒ•์น™ ๊ฒฐํ•ฉ๋ฒ•์น™์€ ์„ธ๋ฏธ์ฝœ๋ก ์ด ๊ทธ๋Ÿฌ๋“ฏ์ด bind ์—ฐ์‚ฐ์ž (>>=)๊ฐ€ ๊ณ„์‚ฐ์˜ ์ˆœ์„œ๋งŒ ์‹ ๊ฒฝ ์“ธ ๋ฟ ๊ทธ ์ค‘์ฒฉ ๊ตฌ์กฐ๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์Œ์„ ๋ณด์žฅํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด bothGrandfathers๋ฅผ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. do๋ฅผ ์“ฐ์ง€ ์•Š์€ ์ด์ „ ๋ฒ„์ „๊ณผ ๋น„๊ตํ•ด ๋ณด๋ผ. bothGrandfathers p = (father p >>= father) >>= (\gf1 -> (mother p >>= father) >>= (\gf2 -> return (gf1, gf2) )) then ์—ฐ์‚ฐ์ž์˜ ๊ฒฐํ•ฉ ๋ฒ•์น™์€ ํŠน์ˆ˜ํ•œ ๊ฒฝ์šฐ๋‹ค. (m >> n) >> o = m >> (n >> o) ๋ชจ๋‚˜ ๋”• ํ•ฉ์„ฑ bind์˜ ๊ฒฐํ•ฉ ๋ฒ•์น™์„ ์ด๋ ‡๊ฒŒ ์žฌ๊ตฌ์„ฑํ•˜๋ฉด ๊ทธ ์˜๋ฏธ๋ฅผ ํฌ์ฐฉํ•˜๊ธฐ๊ฐ€ ๋” ์‰ฝ๋‹ค. (f >=> g) >=> h = f >=> (g >=> h) ์—ฌ๊ธฐ์„œ (>=>)๋Š” ๋ชจ๋‚˜ ๋”• ํ•ฉ์„ฑ ์—ฐ์‚ฐ์ž๋กœ์„œ ํ•จ์ˆ˜ ํ•ฉ์„ฑ ์—ฐ์‚ฐ์ž (.)์™€ ์•„์ฃผ ์œ ์‚ฌํ•˜์ง€๋งŒ ๊ทธ ์ธ์ž๋Š” ๋ฐ˜๋Œ€๋กœ๋‹ค. (>=>)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋œ๋‹ค. (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c f >=> g = \x -> f x >>= g (>=>)์„ ๋’ค์ง‘์€ (<=<)๋„ ์žˆ๋‹ค. ์ด๊ฑธ ์“ธ ๋•Œ๋Š” ํ•ฉ์„ฑ ์ˆœ์„œ๊ฐ€ (.)์™€ ์ผ์น˜ํ•˜๋ฉฐ, ๋”ฐ๋ผ์„œ (f <=< g)์—์„œ๋Š” g๊ฐ€ ๋จผ์ € ์˜จ๋‹ค. 4 ๋ชจ๋‚˜๋“œ์™€ ๋ฒ”์ฃผ๋ก  ๋ชจ๋‚˜๋“œ๋Š” ๋ฒ”์ฃผ๋ก ์ด๋ผ๋Š” ์ˆ˜ํ•™์˜ ํ•œ ๊ฐˆ๋ž˜์—์„œ ์œ ๋ž˜ํ–ˆ๋‹ค. ๋‹คํ–‰ํžˆ๋„ ํ•˜์Šค์ผˆ์—์„œ ๋ชจ๋‚˜๋“œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ฒ”์ฃผ๋ก ์„ ์ดํ•ดํ•  ํ•„์š”๋Š” ์ „ํ˜€ ์—†๋‹ค. ๋ฒ”์ฃผ๋ก ์˜ ๋ชจ๋‚˜๋“œ ์ •์˜๋Š” ์‚ฌ์‹ค ํ‘œํ˜„๋ฒ•์ด ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค. ์ด ํ‘œํ˜„๋ฒ•์„ ํ•˜์Šค ์ผˆ ์‹์œผ๋กœ ๋‹ค๋“ฌ์œผ๋ฉด ๋™๋“ฑํ•˜์ง€๋งŒ ๋˜ ๋‹ค๋ฅธ ๋ชจ๋‚˜๋“œ ์ •์˜๋ฅผ ์–ป๋Š”๋‹ค. ์ด๋กœ๋ถ€ํ„ฐ Monad ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๋˜ ๋‹ค๋ฅธ ํ†ต์ฐฐ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. 5 ์ง€๊ธˆ๊นŒ์ง€๋Š” (>>=)์™€ return์„ ํ†ตํ•ด ๋ชจ๋‚˜๋“œ๋ฅผ ์ •์˜ํ–ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์ •์˜์—์„œ๋Š” ๋ชจ๋‚˜๋“œ๋ฅผ ๋‘ ๊ฒฐํ•ฉ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” functor๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค. fmap :: (a -> b) -> M a -> M b -- functor return :: a -> M a join :: M (M a) -> M a Functor ํด๋ž˜์Šค์— ๊ด€ํ•œ ์žฅ์—์„œ ๋…ผ์˜ํ–ˆ๋“ฏ์ด ํŽ‘ํ„ฐ๋ฅผ ์ปจํ…Œ์ด๋„ˆ์— ๋น„์œ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์— ๋”ฐ๋ฅด๋ฉด functor M์€ ์ผ์ข…์˜ ์ปจํ…Œ์ด๋„ˆ๋กœ์„œ M a๋Š” a ํƒ€์ž…์˜ ๊ฐ’์„ "๋ณด๊ด€" ํ•˜๊ณ , ๋Œ€์‘ํ•˜๋Š” ๋งคํ•‘ ํ•จ์ˆ˜ fmap์€ ๊ทธ ๋‚ด๋ถ€์˜ ๊ฐ’์— ํ•จ์ˆ˜๋“ค์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด์„ํ•˜๋ฉด ์œ„ ํ•จ์ˆ˜๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘๋™ํ•œ๋‹ค. fmap์€ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ๋ชจ๋“  ์›์†Œ์— ์ ์šฉํ•œ๋‹ค return์€ ์›์†Œ๋ฅผ ์ปจํ…Œ์ด๋„ˆ๋กœ ๊ฐ์‹ผ๋‹ค join์€ ์ปจํ…Œ์ด๋„ˆ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ทจํ•ด ๋‹จ์ผ ์ปจํ…Œ์ด๋„ˆ๋กœ ํ‰ํƒ„ํ™”ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์„ ์ด์šฉํ•ด bind ์—ฐ์‚ฐ์ž๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. m >>= g = join (fmap g m) ๋น„์Šทํ•˜๊ฒŒ (>>=)์™€ return์„ ์ด์šฉํ•ด fmap๊ณผ join์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. fmap f x = x >>= (return . f) join x = x >>= id liftM๊ณผ ๊ทธ ์นœ๊ตฌ๋“ค ์•ž์„œ ๋ชจ๋“  Monad๋Š” Applicative์ด๊ณ  ๋”ฐ๋ผ์„œ Functor๋ผ๋Š” ๊ฒƒ์„ ์งš๊ณ  ๋„˜์–ด๊ฐ”์—ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ return๊ณผ (>>)๋Š” ๊ฐ๊ฐ pure์™€ (*>)์˜ ๋ชจ๋‚˜๋“œ ์ „์šฉ ๋ฒ„์ „์ด ๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ๋์ด ์•„๋‹ˆ๋‹ค. Control.Monad๋Š” liftM์„ ์ •์˜ํ•˜๋Š”๋ฐ ๊ทธ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๊ฐ€ ์™ ์ง€ ์ต์ˆ™ํ•˜๋‹ค. liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r ์˜์‹ฌํ–ˆ๋˜ ๋Œ€๋กœ liftM์€ ์šฐ๋ฆฌ๊ฐ€ ๋ฐ”๋กœ ์•ž ์ ˆ์—์„œ ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ (>>=)์™€ return์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•œ fmap ์ผ๋ฟ์ด๋‹ค. ๋”ฐ๋ผ์„œ liftM๊ณผ fmap์€ ์„œ๋กœ ๋ฐ”๊ฟ” ์“ธ ์ˆ˜ ์žˆ๋‹ค. ap๋Š” Control.Monad์— ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋กœ์„œ ๋ฌ˜ํ•œ ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค. ap :: Monad m => m (a -> b) -> m a -> m b ๋‹ค๋ฅธ ๊ฒฝ์šฐ์™€ ๋น„์Šทํ•˜๊ฒŒ ap๋Š” (<*>)์˜ ๋ชจ๋‚˜๋“œ ์ „์šฉ ๋ฒ„์ „์ด๋‹ค. Control.Monad๋ฅผ ๋น„๋กฏํ•ด ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ์—๋Š” Monad์— ํŠนํ™”๋œ ๋ฒ„์ „์˜ Applicative ํ•จ์ˆ˜๋“ค์ด ๊ณณ๊ณณ์— ์žˆ๋‹ค. ์ด๋Ÿฐ ํ•จ์ˆ˜๋“ค์ด ์กด์žฌํ•˜๋Š” ๋ฐ๋Š” ์—ญ์‚ฌ์ ์ธ ์ด์œ ๊ฐ€ ์žˆ๋‹ค. ํ•˜์Šค์ผˆ์— Monad์™€ Applicative๊ฐ€ ๋„์ž…๋˜๋Š” ์‚ฌ์ด์—๋Š” ๋ช‡ ๋…„์˜ ๊ฐ„๊ฒฉ์ด ์žˆ์—ˆ๊ณ  Applicative๊ฐ€ Monad์˜ ์Šˆํผํด๋ž˜์Šค๊ฐ€ ๋˜๊ธฐ๊นŒ์ง€๋Š” ๋” ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๊ธฐ์— ํŠนํ™”๋œ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์„ ํƒ์‚ฌํ•ญ์ด ๋˜์—ˆ๋‹ค. ์ด์ œ ์™€์„œ๋Š” ๋ชจ๋‚˜๋“œ ์ „์šฉ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•  ์ด์œ ๊ฐ€ ๊ฑฐ์˜ ์—†์–ด์ ธ์„œ, ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด return๊ณผ (>>)๋งŒ ๋ณด์ผ ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ๋“ค์˜ ์šฉ๋ฒ•์€ ํ•˜์Šค์ผˆ์—์„œ 20๋…„์ด ๋„˜๋„๋ก Applicative๊ฐ€ Monad์˜ ์Šˆํผํด๋ž˜์Šค๊ฐ€ ์•„๋‹ˆ์—ˆ๋˜ ๋•์— ์ž˜ ์ž๋ฆฌ ์žก๊ฒŒ ๋˜์—ˆ๋‹ค. ๋…ธํŠธ Applicative๊ฐ€ Monad์˜ ์Šˆํผํด๋ž˜์Šค์ด๋ฏ€๋กœ Monad๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐ€์žฅ ๋ช…๋ฃŒํ•œ ๋ฐฉ๋ฒ•์€ Functor ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ํด๋ž˜์Šค ๊ณ„์ธต๋„๋ฅผ ๋”ฐ๋ผ ๋‚ด๋ ค๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค. instance Functor Foo where fmap = -- etc. instance Applicative Foo where pure = -- etc. (<*>) = -- etc. instance Monad Foo where (>>=) = -- etc. ์•ž์œผ๋กœ ์ด์–ด์งˆ ์ฑ•ํ„ฐ๋“ค์—์„œ ์—ฌ๋Ÿฌ๋ถ„์€ Monad์˜ ์ธ์Šคํ„ด์Šค๋“ค์„ ์ž‘์„ฑํ•˜๊ณ  ์‚ฌ์šฉํ•ด ๋ณด๊ฑฐ๋‚˜, ์ด ์ฑ…์˜ ์˜ˆ์ œ๋“ค์„ ์‹คํ–‰ํ•ด ๋ณด๊ฑฐ๋‚˜, ์—ฌ๋Ÿฌ๋ถ„์ด ์ƒ๊ฐํ•˜๋Š” ๋‹ค๋ฅธ ์‹คํ—˜์„ ํ•ด๋ณผ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์œ„์—์„œ ๋ณด์—ฌ์ค€ ๋ฐฉ์‹์œผ๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•˜๋ ค๋ฉด pure์™€ (<*>)๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ด ์ฑ…์˜ ์ด ์‹œ์ ์—์„œ๋Š” Applicative ๋ฒ•์น™๋“ค์„ ์•„์ง ๋‹ค๋ฃจ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Š” ์‰ฌ์šด ์ผ์ด ์•„๋‹ˆ๋‹ค. (์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ์žฅ์— ๊ฐ€์„œ์•ผ ๋‹ค๋ฃฌ๋‹ค) ๋‹คํ–‰ํžˆ ๋Œ์•„๊ฐ€๋Š” ๊ธธ์ด ์žˆ๋‹ค. (>>=)์™€ return์„ ๊ตฌํ˜„ํ•˜์—ฌ ๊ทธ ์ž์ฒด๋กœ ์ถฉ๋ถ„ํ•œ Monad ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๋‚˜๋ฉด liftM, ap, return์„ ์‚ฌ์šฉํ•ด ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋“ค์„ ์ฑ„์›Œ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. instance Monad Foo where return = -- etc. (>>=) = -- etc. instance Applicative Foo where pure = return (<*>) = ap instance Functor Foo where fmap = liftM ๋ชจ๋‚˜๋“œ์— ๋Œ€ํ•œ ์ด ์ผ๋ จ์˜ ์ฑ•ํ„ฐ๋“ค์— ๋‚˜์˜ค๋Š” ์˜ˆ์ œ์™€ ์—ฐ์Šต๋ฌธ์ œ์—์„œ๋Š” Applicative ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•  ๊ฒƒ์„ ์š”๊ตฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ Applicative๋ฅผ ์ž์„ธํžˆ ๋…ผ์˜ํ•˜๊ธฐ ์ „๊นŒ์ง€๋Š” ์ด ์šฐํšŒ์ฑ…์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์ด return ํ•จ์ˆ˜๋Š” C๋‚˜ ์ž๋ฐ” ๊ฐ™์€ ๋ช…๋ นํ˜• ์–ธ์–ด์˜ return ํ‚ค์›Œ๋“œ์™€ ์•„๋ฌด ๊ด€๋ จ์ด ์—†๋‹ค. ๋‘˜์„ ํ—ท๊ฐˆ๋ฆฌ์ง€ ๋ง ๊ฒƒ. โ†ฉ ์ด ์ค‘์š”ํ•œ ์ƒํ•˜๊ด€๊ณ„๋Š” ์—ญ์‚ฌ์  ์šฐ์—ฐ ๋•์— ์ตœ๊ทผ์— ์™€์„œ์•ผ (2015๋…„ ์ดˆ๊ธฐ GHC 7.10 ๋ฒ„์ „) ๊ตฌํ˜„๋˜์—ˆ๋‹ค. ์ด๋ณด๋‹ค ์˜ค๋ž˜๋œ GHC ๋ฒ„์ „์„ ์“ฐ๊ณ  ์žˆ๋‹ค๋ฉด ์ด๋Ÿฐ ํด๋ž˜์Šค ์ œ์•ฝ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์šฐ๋ฆฌ๊ฐ€ ์•ž์œผ๋กœ ๊ณ ๋ คํ•  ์‹ค์šฉ์  ๊ด€์ ๋“ค ์ค‘ ์ผ๋ถ€๋Š” ํ•ด๋‹น์‚ฌํ•ญ์ด ์—†์„ ๊ฒƒ์ด๋‹ค. โ†ฉ "์˜๋ฏธ semantic"๋ผ ํ•จ์€ ์–ธ์–ด๊ฐ€ ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ๋งํ•˜๊ธฐ๋ฅผ ํ—ˆ๋ฝํ•œ ๊ฒƒ์ด๋‹ค. Maybe์˜ ์˜๋ฏธ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์‹คํŒจ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ๋ฝํ•˜์—ฌ, ๋ช…๋ น๋ฌธ์ด ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋Š” ๋ฐ ์‹คํŒจํ•˜๋ฉด ๋’ค์— ๋”ฐ๋ผ์˜ค๋Š” ๋ช…๋ น๋ฌธ์€ ๊ฑด๋„ˆ๋›ฐ๊ฒŒ ๋งŒ๋“ ๋‹ค. โ†ฉ ๋ฌผ๋ก  ์ •๊ทœ ํ•จ์ˆ˜ ํ•ฉ์„ฑ์— ์“ฐ์ด๋Š” ํ•จ์ˆ˜๋“ค์€ ๋ชจ๋‚˜ ๋”• ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๋ฐ˜๋ฉด, ๋ชจ๋‚˜ ๋”• ํ•ฉ์„ฑ์€ ๋ชจ๋‚˜ ๋”• ํ•จ์ˆ˜๋งŒ์„ ์ทจํ•œ๋‹ค. โ†ฉ ๊ณ ๊ธ‰๋ฐ˜์˜ ๋ฒ”์ฃผ๋ก ์—์„œ ์ด๋ก ์ ์ธ ์ธก๋ฉด์„ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. โ†ฉ 1 Maybe ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Understanding_monads/Maybe ์•ˆ์ „ํ•œ ํ•จ์ˆ˜ ์ฐธ์กฐํ‘œ ๊ณต๊ฐœ๋œ ๋ชจ๋‚˜๋“œ Maybe์™€ ์•ˆ์ „์„ฑ Maybe๋ฅผ ์˜ˆ์ œ๋กœ ์‚ผ์•„ ๋ชจ๋‚˜๋“œ๋ฅผ ์†Œ๊ฐœํ–ˆ์—ˆ๋‹ค. Maybe ๋ชจ๋‚˜๋“œ๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ์œผ๋กœ์จ "์ž˜๋ชป๋  ์ˆ˜ ์žˆ๋Š”" ๊ณ„์‚ฐ(computation)์„ ํ‘œํ˜„ํ•œ๋‹ค. ์ฐธ๊ณ ๋ฅผ ์œ„ํ•ด ์—ฌ๊ธฐ ์•ž์˜ ์žฅ์—์„œ ๋ดค๋˜ Maybe ์šฉ return๊ณผ (>>=)์˜ ์ •์˜๊ฐ€ ์žˆ๋‹ค.1 return :: a -> Maybe a return x = Just x (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b (>>=) m g = case m of Nothing -> Nothing Just x -> g x ์•ˆ์ „ํ•œ ํ•จ์ˆ˜ Maybe ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ๋‹ค์–‘ํ•œ ์ธ์ž ๋•Œ๋ฌธ์— ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์„ ๊ฐ์‹ธ๋Š” ์•ˆ์ „ํ•œ ๋ž˜ํผ wrapper๋ฅผ ๋งŒ๋“œ๋Š” ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด head์™€ tail์€ ๋น„์–ด์žˆ์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ์—๋งŒ ์ž‘๋™ํ•œ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์ „ํ˜•์ ์ธ ์˜ˆ๋Š” ์ด ์ ˆ์—์„œ ๋ณผ ๊ฒƒ์ธ๋ฐ, sqrt๋‚˜ log ๊ฐ™์€ ์ˆ˜ํ•™ ํ•จ์ˆ˜๋‹ค. ์‹ค์ˆ˜์˜ ๋ฒ”์œ„์—์„œ ๊ณ ๋ คํ•  ๋•Œ, ์ด๋“ค ํ•จ์ˆ˜๋Š” ์Œ์ด ์•„๋‹Œ ์ธ์ž์— ๋Œ€ํ•ด์„œ๋งŒ ์ •์˜๋œ๋‹ค. > log 1000 6.907755278982137 > log -1000 ''ERROR'' -- runtime error ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ”ผํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, log์˜ "์•ˆ์ „ํ•œ" ๊ตฌํ˜„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. safeLog :: (Floating a, Ord a) => a -> Maybe a safeLog x | x >= 0 = Just (log x) | otherwise = Nothing > safeLog 1000 Just 6.907755278982137 > safeLog -1000 Nothing ๋‚˜๋ˆ„๊ธฐ, ์ œ๊ณฑ๊ทผ, ์—ญ์‚ผ๊ฐํ•จ์ˆ˜๊ฐ™์ด ์ •์˜์—ญ์ด ์ œํ•œ๋œ ๋ชจ๋“  ํ•จ์ˆ˜์— ๋Œ€ํ•ด ๋น„์Šทํ•œ "์•ˆ์ „ํ•œ ํ•จ์ˆ˜"๋“ค์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. safeDiv, safeSqrt, safeArcSin ๋“ฑ safeLog์™€ ํƒ€์ž…์ด ๊ฐ™์ง€๋งŒ ๊ทธ ์ •์˜๋Š” ๊ฐ๊ฐ์˜ ์ •์˜์—ญ ์ œํ•œ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค. ์ด ๋ชจ๋‚˜ ๋”• ํ•จ์ˆ˜๋“ค์„ ๊ฒฐํ•ฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ๊ฐ€์žฅ ๊น”๋”ํ•œ ์ ‘๊ทผ๋ฒ•์€, ์ด์ „ ์žฅ์˜ ๊ฑฐ์˜ ๋๋ถ€๋ถ„์—์„œ ์งง๊ฒŒ ์–ธ๊ธ‰ํ–ˆ๋˜ ๋ชจ๋‚˜ ๋”• ํ•ฉ์„ฑ๊ณผ ์ธ์ž ์ƒ๋žต ์Šคํƒ€์ผ์ด๋‹ค. safeLogSqrt = safeLog <=< safeSqrt ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๊ณ  ๋ณด๋‹ˆ safeLogSqrt๋Š” ์•ˆ์ „ํ•˜์ง€ ์•Š๊ณ  ๋ชจ๋‚˜๋”•์ด ์•„๋‹Œ ๋ฒ„์ „๊ณผ ๋น„์Šทํ•˜๋‹ค. unsafeLogSqrt = log . sqrt ์ฐธ์กฐํ‘œ ์ฐธ์กฐํ‘œ๋Š” ํ‚ค์™€ ๊ฐ’์„ ์—ฐ๊ด€ ์ง“๋Š”๋‹ค. ๊ฐ’์„ ๊ฒ€์ƒ‰ํ•˜๋ ค๋ฉด ๊ทธ ๊ฐ’์˜ ํ‚ค๋ฅผ ์•Œ์•„๋‚ด์–ด ์ฐธ์กฐํ‘œ๋ฅผ ์ด์šฉํ•ด ๊ฒ€์ƒ‰ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ „ํ™”๋ฒˆํ˜ธ์— ๋Œ€์‘ํ•˜๋Š” ํ‚ค๊ฐ€ ์—ฐ๋ฝ์ฒ˜์ธ ์ฐธ์กฐํ‘œ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์ „ํ™”๋ฒˆํ˜ธ๋ถ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ƒ์ƒํ•ด ๋ณด์ž. ํ•˜์Šค์ผˆ์—์„œ ์ฐธ์กฐํ‘œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ธฐ์ดˆ์ ์ธ ๋ฐฉ๋ฒ•์€ ์ง์˜ ๋ฆฌ์ŠคํŠธ [(a, b)]๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. a๋Š” ํ‚ค์˜ ํƒ€์ž…์ด๊ณ  b๋Š” ๊ฐ’์˜ ํƒ€์ž…์ด๋‹ค. 2 ์ „ํ™”๋ฒˆํ˜ธ๋ถ€ ์ฐธ์กฐํ‘œ๋Š” ์ด๋Ÿฐ ์‹์ด๋‹ค. phonebook :: [(String, String)] phonebook = [ ("Bob", "01788 665242"), ("Fred", "01624 556442"), ("Alice", "01889 985333"), ("Jane", "01732 187565") ] ์ฐธ์กฐํ‘œ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•˜๋Š” ๊ฐ€์žฅ ํ”ํ•œ ์ผ์ด ๊ฐ’์„ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค. ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์—์„œ "Bob", "Fred", "Alice", "Jane"์„ ์ฐพ์œผ๋ ค ํ•  ๋•Œ๋Š” ์•„๋ฌด ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. ํ•˜์ง€๋งŒ "Zoe"๋ฅผ ์ฐพ์•„๋ณธ๋‹ค๋ฉด? Zoe๋Š” ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰์€ ์‹คํŒจํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํ‘œ์—์„œ ๊ฐ’์„ ์ฐพ๋Š” ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋Š” Maybe ๊ณ„์‚ฐ์ด๋‹ค. ์ด ํ•จ์ˆ˜๋Š” Prelude์— ๋“ค์–ด์žˆ๋‹ค. lookup :: Eq a => a -- ํ‚ค -> [(a, b)] -- ์‚ฌ์šฉํ•  ์ฐธ์กฐํ‘œ -> Maybe b -- ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ lookup์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ช‡ ๊ฐœ ๋ณด์ž. Prelude> lookup "Bob" phonebook Just "01788 665242" Prelude> lookup "Jane" phonebook Just "01732 187565" Prelude> lookup "Zoe" phonebook Nothing ์ด์ œ ๋ชจ๋‚˜ ๋”• ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ชจ๋“  ๋Šฅ๋ ฅ์„ ์ด๋Œ์–ด๋‚ด ์ด๊ฒƒ์„ ํ™•์žฅํ•ด ๋ณด์ž. ์ง€๊ธˆ ์ •๋ถ€๋ฅผ ์œ„ํ•ด ์ผํ•˜๊ณ  ์žˆ๊ณ  ๊ด€๊ณ„์ž๋“ค์˜ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ, ๊ฑฐ๋Œ€ํ•œ ์ •๋ถ€ ๊ทœ๋ชจ์˜ ์ฐธ์กฐํ‘œ์—์„œ ์ด ์ „ํ™”๋ฒˆํ˜ธ๋“ค์„ ์ฐพ์•„ ๊ด€๊ณ„์ž๋“ค์˜ ์ž๋™์ฐจ ๋“ฑ๋ก๋ฒˆํ˜ธ๋ฅผ ์•Œ์•„๋‚ด๋ ค ํ•œ๋‹ค๊ณ  ์น˜์ž. ์ด๊ฒƒ ์—ญ์‹œ ๋˜ ๋‹ค๋ฅธ Maybe-๊ณ„์‚ฐ์ด๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๋Š” ์‚ฌ๋žŒ์ด ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์—†์œผ๋ฉด ์ •๋ถ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๊ทธ๋“ค์˜ ๋“ฑ๋ก๋ฒˆํ˜ธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์„ ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ์—๊ฒŒ ํ•„์š”ํ•œ ๊ฒƒ์€ ์ฒซ ๋ฒˆ์งธ ๊ณ„์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ทจํ•ด ๋‘ ๋ฒˆ์งธ ๊ฒ€์ƒ‰์— ๋„ฃ๋Š”๋ฐ, ์˜ค์ง ์ฒซ ๋ฒˆ์งธ ๊ฒ€์ƒ‰์—์„œ ๊ฐ’์„ ์„ฑ๊ณต์ ์œผ๋กœ ์–ป์—ˆ์„ ๋•Œ๋งŒ ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. ๋ฌผ๋ก  ๋‘ ๊ฒ€์ƒ‰ ์ค‘ ํ•˜๋‚˜๋ผ๋„ Nothing์„ ๋Œ๋ ค์ฃผ๋ฉด ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” Nothing์ด์–ด์•ผ ํ•œ๋‹ค. getRegistrationNumber :: String -- ์ด๋ฆ„ -> Maybe String -- ๋“ฑ๋ก๋ฒˆํ˜ธ getRegistrationNumber name = lookup name phonebook >>= (\number -> lookup number governmentDatabase) ์ •๋ถ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ฒ€์ƒ‰ํ•ด์„œ ์–ป์€ ์ด ๊ฒฐ๊ณผ๋ฅผ ์ด์–ด์„œ ์„ธ ๋ฒˆ์งธ ๊ฒ€์ƒ‰์— ์‚ฌ์šฉํ•˜๋ ค ํ•œ๋‹ค๋ฉด(๊ด€๊ณ„์ž๋“ค์˜ ์ž๋™์ฐจ์„ธ์— ๋ถ€์ฑ„๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค๊ณ  ๋“ฑ๋ก๋ฒˆํ˜ธ๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค๋˜๊ฐ€ ํ•˜๋Š” ์ด์œ ๋กœ), getRegistrationNumber ํ•จ์ˆ˜๋ฅผ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. getTaxOwed :: String -- ์ด๋ฆ„ -> Maybe Double -- ์ง€๋ถˆํ•  ์„ธ๊ธˆ์˜ ์–‘ getTaxOwed name = lookup name phonebook >>= (\number -> lookup number governmentDatabase) >>= (\registration -> lookup registration taxDatabase) ๋˜๋Š” do ๋ธ”๋ก์„ ์“ฐ๋ฉด getTaxOwed name = do number <- lookup name phonebook registration <- lookup number governmentDatabase lookup registration taxDatabase ์—ฌ๊ธฐ์„œ ๋ฉˆ์ถฐ ์„œ๊ณ , ์–ด๋””์—์„œ๋“  Nothing์„ ๋ฐ›๊ฒŒ ๋  ๋•Œ ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚ ์ง€ ์ƒ๊ฐํ•ด ๋ณด์ž. ๊ทธ ์ •์˜์— ๋”ฐ๋ผ, >>=์˜ ์ฒซ ์ธ์ž๊ฐ€ Nothing ์ด๋ฉด >>=๋Š” ๋ฌด์Šจ ํ•จ์ˆ˜๋ฅผ ๋ฐ›๋“  Nothing์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํฐ ๊ณ„์‚ฐ์˜ ์–ด๋Š ๋‹จ๊ณ„์—์„œ๋“  Nothing์ด ์žˆ์œผ๋ฉด ๋‹ค๋ฅธ ํ•จ์ˆ˜์•ผ ์–ด๋–ป๋“  ๊ฐ„์— ์ „์ฒด๊ฐ€ Nothing์œผ๋กœ ๋๋‚œ๋‹ค. ์ฒซ ๋ฒˆ์งธ Nothing์„ ๋งŒ๋‚œ ๋’ค์˜ ๋ชจ๋“  >>=๋Š” ๊ทธ Nothing์„ ์„œ๋กœ์—๊ฒŒ ์ „๋‹ฌ๋งŒ ํ•˜๋ฉฐ ๋‹ค๋ฅธ ์ธ์ž๋Š” ๋ฌด์‹œํ•œ๋‹ค. ๊ธฐ์ˆ ์ ์œผ๋กœ ๋งํ•˜์ž๋ฉด Maybe ๋ชจ๋‚˜๋“œ์˜ ๊ตฌ์กฐ๋Š” ์‹คํŒจ๋ฅผ ํ™•์‚ฐ์‹œํ‚จ๋‹ค. ๊ณต๊ฐœ๋œ ๋ชจ๋‚˜๋“œ Maybe ๋ชจ๋‚˜๋“œ์˜ ๋˜ ๋‹ค๋ฅธ ํŠน์ง•์€ "๊ณต๊ฐœ๋˜์–ด ์žˆ๋‹ค"๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ๋Š” Just ๊ฐ’์˜ ๋‚ด์šฉ์„ ๋“ค์—ฌ๋‹ค๋ณด๊ณ  ํŒจํ„ด ๋งค์นญ์„ ํ†ตํ•ด ์—ฐ๊ด€๋œ ๊ฐ’์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. zeroAsDefault :: Maybe Int -> Int zeroAsDefault mx = case mx of Nothing -> 0 Just x -> x Nothing์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์น˜ํ™˜ํ•˜๋Š” ์ด๋Ÿฐ ์‚ฌ์šฉ๋ฒ•์€ Data.Maybe์˜ fromMaybe ํ•จ์ˆ˜์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. zeroAsDefault :: Maybe Int -> Int zeroAsDefault mx = fromMaybe 0 mx Prelude ํ•จ์ˆ˜ maybe๋Š” ์ถ”์ถœํ•œ ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด ์ด ์ผ์„ ๋ณด๋‹ค ์ผ๋ฐ˜ํ™”๋œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค. displayResult :: Maybe Int -> String displayResult mx = maybe "There was no result" (("The result was " ++) . show) mx ์ด๋Ÿฐ ๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ด Maybe์˜ ๊ฒฝ์šฐ์—๋Š” ํƒ€๋‹นํ•˜๋‹ค. ์‹คํŒจ๋ฅผ ๋งŒํšŒํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ชจ๋“  ๋ชจ๋‚˜๋“œ๊ฐ€ ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ณต๊ฐœ๋œ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋•Œ๋กœ ๋ชจ๋‚˜๋“œ๋Š” ๋ถˆํ•„์š”ํ•œ ์„ธ๋ถ€์‚ฌํ•ญ์„ ๊ฐ์ถ”๋„๋ก ์„ค๊ณ„๋œ๋‹ค. return๊ณผ (>>=)๋งŒ์œผ๋กœ๋Š” ๋ชจ๋‚˜ ๋”• ์—ฐ์‚ฐ์œผ๋กœ๋ถ€ํ„ฐ ๋‚ด๋ถ€์˜ ๊ฐ’์„ ์ถ”์ถœํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ฆฌ๊ณ  "์ถœ๊ตฌ ์—†๋Š”" ๋ชจ๋‚˜๋“œ, ์ฆ‰ ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ชจ๋‚˜๋“œ๋„ ๋ง์ด ์•ˆ ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๊ทธ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๊ฐ€ IO ๋ชจ๋‚˜๋“œ๋‹ค. Maybe์™€ ์•ˆ์ „์„ฑ Maybe๊ฐ€ ์ œ๊ณตํ•˜๋Š”, ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚ค์ง€ ์•Š๊ณ  ์‹คํŒจ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์šฐ์•„ํ•œ ์ˆ˜๋‹จ์„ ํ†ตํ•ด ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋ฅผ ๋” ์•ˆ์ „ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š”์ง€ ๋ดค๋‹ค. ๊ทธ๋Ÿผ ๋ชจ๋“  ๊ณณ์— Maybe๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ• ๊นŒ? ๊ธ€์Ž„๋‹ค. ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ๋•Œ, ์—ฌ๋Ÿฌ๋ถ„์€ ํ”„๋กœ๊ทธ๋žจ์ด ์ •์ƒ์ ์ธ ์ž‘์—… 3์„ ํ•˜๋Š” ๋„์ค‘ ๊ทธ ํ•จ์ˆ˜๊ฐ€ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋Š” ๋ฐ ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์ด์œ ๊ฐ€ ์—ฌ๋Ÿฌ๋ถ„์ด ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ์–ด์„œ ๋“ (์œ„ ์˜ˆ์ œ์ฒ˜๋Ÿผ), ๋˜๋Š” ์ธ์ž๋‚˜ ์ค‘๊ฐ„ ๊ฒฐ๊ด๊ฐ’์ด ๋ง์ด ์•ˆ ๋œ๋‹ค๋Š” ๊ฑธ ์•Œ๊ณ  ์žˆ์–ด์„œ ๋“ (์ธ์ž๊ฐ€ 10 ๋ฏธ๋งŒ์ผ ๋•Œ๋งŒ ์˜๋ฏธ ์žˆ๋Š” ๊ณ„์‚ฐ์ด๋ผ๋˜๊ฐ€), ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ์‹คํŒจ๋ฅผ ์žก๊ธฐ ์œ„ํ•ด ๊ผญ Maybe๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. ์ž„์˜์˜ ๊ธฐ๋ณธ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๋ฅผ ๋˜์ง€๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ํ›จ์”ฌ ๋‚ซ๋‹ค. ํ•˜์ง€๋งŒ ์ด์œ  ์—†์ด ๊ฒฐ๊ด๊ฐ’์˜ ํƒ€์ž…์— Maybe๋ฅผ ๋ถ™์—ฌ๋ด์•ผ ์ฝ”๋“œ๋ฅผ ์–ด์ง€๋ฅด๊ณ  ์œ„ํ—˜ํ•˜๊ฒŒ ๋งŒ๋“ค ๋ฟ์ด๋‹ค. ๋ถˆํ•„์š”ํ•œ Maybe๊ฐ€ ์žˆ๋Š” ํ•จ์ˆ˜์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋Š” ์ฝ”๋“œ์˜ ์‚ฌ์šฉ์ž์—๊ฒŒ ์‹คํŒจํ•  ์ผ๋„ ์—†๋Š”๋ฐ ๊ทธ๋Ÿด ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ๋งํ•˜๋Š” ์…ˆ์ด๋‹ค. ๋ฌผ๋ก  ๊ทธ ๋ฐ˜๋Œ€์ธ ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ ์‹คํŒจํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ๊ฑฐ์ง“๋งํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ๋‚ซ์ง€๋งŒ, ์šฐ๋ฆฌ๋Š” ์–ด๋–ค ๊ฒฝ์šฐ์—๋“  ๋ฐ”๋žŒ์งํ•œ ์ฝ”๋“œ๋ฅผ ์›ํ•˜์ง€ ์•Š๋Š”๊ฐ€. ๊ฒŒ๋‹ค๊ฐ€ Maybe๋ฅผ ์“ฐ๋ฉด ์‹คํŒจ๋ฅผ ํ™•์‚ฐํ•˜๋„๋ก ๊ฐ•์š”๋ฐ›๊ณ (fmap ๋˜๋Š” ๋ชจ๋‚˜ ๋”• ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด), ๊ฒฐ๊ตญ ์‹คํŒจํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ํŒจํ„ด ๋งค์นญ, maybe ํ•จ์ˆ˜, Data.Maybe์˜ fromMaybe๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ์‹คํŒจํ•  ์ผ์ด ์—†๋‹ค๋ฉด ์‹คํŒจ์— ๋Œ€ํ•œ ์ฝ”๋”ฉ์€ ์ƒํ™ฉ์„ ๋ณต์žกํ•˜๊ฒŒ ๋งŒ๋“ค ๋ฟ์ด๋‹ค. Data.Maybe์˜ ์‹ค์ œ ์ธ์Šคํ„ด์Šค๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ์ •์˜๋˜์–ด ์žˆ์ง€๋งŒ ์ด๊ฒƒ๊ณผ ์™„๋ฒฝํžˆ ๋™์น˜๋‹ค. โ†ฉ ํ•˜์Šค ์ผˆ ์‹ค์ „์—์„œ map์— ๋Œ€ํ•œ ๊ณผ๋ชฉ์„ ํ™•์ธํ•ด ๋ณด๋ฉด ์กฐ๊ธˆ ๋‹ค๋ฅด๊ณ  ๋” ์œ ์šฉํ•  ์ˆ˜๋„ ์žˆ๋Š” ๊ตฌํ˜„์ด ์žˆ๋‹ค. โ†ฉ "์ •์ƒ์ ์ธ ์ž‘์—…"์ด๋ผ ํ•จ์€ ์‹ค์„ธ๊ณ„์—์„œ ํ†ต์ œ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ƒํ™ฉ์— ์˜ํ•ด ๋ฐœ์ƒํ•˜๋Š” ์‹คํŒจ๋ฅผ ์ œ์™ธํ•œ ๊ฒƒ์ด๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์ด๋‚˜ ๊ฐœ๊ฐ€ ํ”„๋ฆฐํ„ฐ ์ผ€์ด๋ธ”์„ ๋ฌผ์–ด๋œฏ๋Š” ๊ฒƒ์ด ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ๋‹ค. โ†ฉ 2 List ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Understanding_monads/List ๋ชจ๋‚˜๋“œ๋กœ์„œ ์ธ์Šคํ„ด์Šคํ™”๋œ ๋ฆฌ์ŠคํŠธ ํ† ๋ผ์˜ ์นจ๊ณต ๋ณด๋“œ ๊ฒŒ์ž„ ์˜ˆ์ œ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹(List comprehensions) ๋ฆฌ์ŠคํŠธ๋Š” ํ•˜์Šค์ผˆ์˜ ๊ทผ๋ณธ์„ ์ด๋ฃจ๋Š” ์ผ๋ถ€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ๊นŒ์ง€ ์˜ค๋ฉด์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ์‚ฌ์šฉํ–ˆ๋‹ค. ์ค‘์š”ํ•œ ํ†ต์ฐฐ์€ ๋ฆฌ์ŠคํŠธ ํƒ€์ž… ์—ญ์‹œ ๋ชจ๋‚˜๋“œ๋ผ๋Š” ๊ฒƒ์ด๋‹ค! ๋ชจ๋‚˜๋“œ๋กœ์„œ์˜ ๋ฆฌ์ŠคํŠธ๋Š” ์ž„์˜ ๊ฐœ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋น„๊ฒฐ์ •์  ์—ฐ์‚ฐ์„ ๋ชจ๋ธ๋ง ํ•œ๋‹ค. Maybe๊ฐ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ณ„์‚ฐ์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ํ™•์‹คํ•œ ๋ณ‘๋ ฌ์„ฑ์ด ์žˆ์—ˆ๋Š”๋ฐ, ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ์—๋Š” 0๊ฐœ, 1๊ฐœ, ๋” ๋งŽ์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ’์˜ ๊ฐœ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด์— ๋ฐ˜์˜๋œ๋‹ค. ๋ชจ๋‚˜๋“œ๋กœ์„œ ์ธ์Šคํ„ด์Šคํ™”๋œ ๋ฆฌ์ŠคํŠธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์œ„ํ•œ return ํ•จ์ˆ˜๋Š” ๋‹จ์ˆœํžˆ ๊ฐ’์„ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์‚ฝ์ž…ํ•œ๋‹ค. return x = [x] ๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด, return์€ ์›์†Œ ํ•˜๋‚˜ ์ฆ‰ ์ธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค. ๋ฆฌ์ŠคํŠธ return์˜ ํƒ€์ž…์€ return :: a -> [a] ๋˜๋Š” ๋™์น˜์ธ ๊ฒƒ์œผ๋กœ return :: a -> [] a๋‹ค. ํ›„์ž์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๋ฉด return์˜ ์‹œ๊ทธ๋„ˆ์ณ์—์„œ ์ œ๋„ˆ๋ฆญ ํƒ€์ž… ์ƒ์„ฑ์ž(๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ์—์„œ M์ด๋ผ๊ณ  ํ•œ ๊ฒƒ)๋ฅผ ๋ฆฌ์ŠคํŠธ ํƒ€์ž… ์ƒ์„ฑ์ž [](๋นˆ ๋ฆฌ์ŠคํŠธ์™€ ๋ณ„๊ฐœ์˜ ๊ฒƒ์ด์ง€๋งŒ ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‰ฝ๋‹ค)๋กœ ๋Œ€์ฒดํ–ˆ๋‹ค๋Š” ๊ฒƒ์ด ๋ช…ํ™•ํ•ด์ง„๋‹ค. ๋ฐ”์ธ๋”ฉ ์—ฐ์‚ฐ์ž๋Š” ์ฉ ์ž๋ช…ํ•˜์ง€ ์•Š๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ์— ํƒ€์ž…์ด ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€๋ถ€ํ„ฐ ์‚ดํŽด๋ณด์ž. [a] -> (a -> [b]) -> [b] ์˜ˆ์ƒํ–ˆ๋˜ ๋Œ€๋กœ๋‹ค. ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ’์„ ๋นผ๋‚ด ํ•œ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ณ  ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์‹ค์ œ๋กœ ์ผ์–ด๋‚˜๋Š” ์ผ์€ ์ด๋ ‡๋‹ค. ๋จผ์ € ์ฃผ์–ด์ง„ ๋ฆฌ์ŠคํŠธ์— ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ๋งคํ•‘ํ•ด [[b]] ํƒ€์ž…์ธ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ป๋Š”๋‹ค. (๋ฌผ๋ก  ์—ฌ๋Ÿฌ๋ถ„์ด ๋งคํ•‘์— ์“ฐ๋Š” ๋งŽ์€ ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์ง€๋งŒ, ์œ„์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ์—์„œ ๋ณด๋“ฏ์ด ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ๋ชจ๋‚˜ ๋”• ๋ฐ”์ธ๋”ฉ์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ๋งŒ ์ž‘๋™ํ•œ๋‹ค) ์ผ๋ฐ˜์ ์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ป์œผ๋ ค๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ์›์†Œ๋“ค์„ ์—ฐ๊ฒฐํ•ด [b] ํƒ€์ž…์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”๋‹ค. ๋”ฐ๋ผ์„œ (>>=)์˜ ๋ฆฌ์ŠคํŠธ ๋ฒ„์ „์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. xs >>= f = concat (map f xs) bind ์—ฐ์‚ฐ์ž๋Š” ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๋ชจ๋‚˜๋“œ๊ฐ€ ๊ฐ๊ธฐ ์–ด๋–ค ์ผ์„ ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•œ ํ•ต์‹ฌ์ด๊ณ , bind ์—ฐ์‚ฐ์ž์˜ ์ •์˜๋ฅผ ํ†ตํ•ด ๊ทธ ๋ชจ๋‚˜๋“œ์˜ ์‚ฌ์šฉ๋ฒ•์„ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ๊ฐ€ ๋น„๊ฒฐ์ •์„ฑ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ๋งํ•˜๋Š” ์ด์œ ๋Š”, ์„œ๋กœ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค์„ ๋ฆฌ์ŠคํŠธ์— ๋งคํ•‘ํ•˜๋ฉด ์ž„์˜ ๊ฐœ์ˆ˜์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋“ค์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ† ๋ผ์˜ ์นจ๊ณต ์šฐ๋ฆฌ์—๊ฒŒ ์ต์ˆ™ํ•œ ๋ฆฌ์ŠคํŠธ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋“ค์„ ๋ชจ๋‚˜ ๋”• ์ฝ”๋“œ์™€ ํ†ตํ•ฉํ•˜๋Š” ๊ฒƒ์€ ์‰ฌ์šด ์ผ์ด๋‹ค. ์ด๋Ÿฐ ์˜ˆ์‹œ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ํ† ๋ผ๋Š” ์ƒˆ๋ผ๋ฅผ ๋ฐธ ๋•Œ๋งˆ๋‹ค ํ‰๊ท  ์—ฌ์„ฏ ๋งˆ๋ฆฌ์˜ ์ž์‹์„ ๋‚ณ๋Š”๋ฐ ๊ทธ์ค‘ ๋ฐ˜์ ˆ์€ ์•”์ปท์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ์—„๋งˆ ํ† ๋ผ ํ•œ ๋งˆ๋ฆฌ๋กœ ์‹œ์ž‘ํ•  ๋•Œ ์ด์–ด์ง€๋Š” ๊ฐ ์„ธ๋Œ€์—์„œ์˜ ์•”์ปท์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋ธ๋ง ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ํ† ๋ผ๋“ค์ด ์„ฑ์žฅํ•˜๊ณ  ๊ฐ์ž ์ƒˆ๋ผ๋ฅผ ๋ฐด ํ›„์˜ ์ƒˆ๋กœ์šด ์ƒˆ๋ผ์˜ ์ˆ˜๋ฅผ ๋งํ•œ๋‹ค. Prelude> let generation = replicate 3 Prelude> ["bunny"] >>= generation ["bunny","bunny","bunny"] Prelude> ["bunny"] >>= generation >>= generation ["bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny","bunny"] ์ด ์–ด์ฒ˜๊ตฌ๋‹ˆ์—†๋Š” ์˜ˆ์ œ์—์„œ๋Š” ๋ชจ๋“  ์›์†Œ๊ฐ€ ๋™์ผํ•˜์ง€๋งŒ ๊ฐ™์€ ๋…ผ๋ฆฌ๋กœ ๋ฐฉ์‚ฌ์„ฑ ๋ถ•๊ดด๋‚˜ ํ™”ํ•™ ๋ฐ˜์‘, ๊ทธ๋ฆฌ๊ณ  ๋‹จ์ผ์ฒด์—์„œ ์‹œ์ž‘ํ•ด ์ผ๋ จ์˜ ์›์†Œ๋“ค์„ ์ƒ์‚ฐํ•˜๋Š” ์–ด๋–ค ํ˜„์ƒ๋„ ๋ชจ๋ธ๋ง ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณด๋“œ ๊ฒŒ์ž„ ์˜ˆ์ œ ํ„ด ๊ธฐ๋ฐ˜ ๊ฒŒ์ž„์„ ๋ชจ๋ธ๋งํ•˜๊ณ  ์žˆ๊ณ  ๊ฒŒ์ž„์ด ์ง„ํ–‰๋  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ์ฐพ์œผ๋ ค ํ•œ๋‹ค. ์šฐ๋ฆฌ์—๊ฒ ํ˜„์žฌ ๋ณด๋“œ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‹ค์Œ ํ„ด์—์„œ์˜ ์„ ํƒ์ง€๋“ค์„ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. nextConfigs :: Board -> [Board] nextConfigs bd = undefined -- ์ž์ž˜ํ•œ ๊ฒƒ์€ ์ค‘์š”์น˜ ์•Š๋‹ค ๋‘ ํ„ด์ด ์ง€๋‚œ ๋’ค์˜ ๋ชจ๋“  ๊ฐ€๋Šฅ์„ฑ์„ ์ฐพ์•„๋‚ด๋ ค๋ฉด, ๋ณด๋“œ ์ƒํƒœ์˜ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด์žˆ๋Š” ๊ฐ ์›์†Œ์— ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋” ์ ์šฉํ•œ๋‹ค. nextConfigs๋Š” ๋ณด๋“œ ์ƒํƒœ๋ฅผ ์ทจํ•ด์„œ ์ƒˆ๋กœ์šด ๊ฐ€๋Šฅ ์ƒํƒœ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋‚˜ ๋”• ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์›์†Œ์— ํ•จ์ˆ˜๋ฅผ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ๋‹ค. nextConfigs bd >>= nextConfigs ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ํ•จ์ˆ˜์— ๋„˜๊ฒจ ๋‹ค์Œ ํ„ด์˜ ๊ฐ€๋Šฅ์„ฑ๋“ค์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒŒ์ž„ ๊ทœ์น™์— ๋”ฐ๋ผ์„œ๋Š” ๊ฐ€๋Šฅํ•œ ๋‹ค์Œ ํ„ด์ด ์—†๋Š” ๋ณด๋“œ ์ƒํƒœ์— ๋‹ค๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ ์ด ํ•จ์ˆ˜๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๋‹ค. 1 ๋ถ€์ˆ˜์ ์ธ ์–ธ๊ธ‰์„ ํ•˜์ž๋ฉด ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ์˜ ํ• ์•„๋ฒ„์ง€ ์˜ˆ์ œ์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ํ„ด์„ do ๋ธ”๋ก์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. threeTurns :: Board -> [Board] threeTurns bd = do bd1 <- nextConfigs bd -- bd1์€ 1ํ„ด ํ›„์˜ ๋ณด๋“œ ๊ตฌ์„ฑ์„ ์ฐธ์กฐํ•œ๋‹ค bd2 <- nextConfigs bd1 nextConfigs bd2 ์œ„์˜ ์ฝ”๋“œ๊ฐ€ ๋งˆ๋ฒ•์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค๋ฉด do ํ‘œ๊ธฐ๋Š” (>>=) ์—ฐ์‚ฐ์˜ ํŽธ์˜ ๋ฌธ๋ฒ•์ด๋ผ๋Š” ๊ฒƒ์„ ๋ช…์‹ฌํ•˜๋ผ. <-์˜ ์˜ค๋ฅธ์ชฝ์—๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ํ‰๊ฐ€๋  ํ•จ์ˆ˜๊ฐ€ ์ธ์ž์™€ ํ•จ๊ป˜ ์œ„์น˜ํ•œ๋‹ค. ์™ผ์ชฝ์˜ ๋ณ€์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ ์›์†Œ๋“ค์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ด <- ํ• ๋‹น ์ค„ ์ดํ›„์—๋Š” ๊ทธ ํ• ๋‹น๋œ ๋ณ€์ˆ˜๋ฅผ ์–ด๋–ค ํ•จ์ˆ˜ ์ธ์ž๋กœ ์“ฐ๋Š” ์ฝ”๋“œ๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ํ•จ์ˆ˜๋Š” <-๊ฐ€ ์žˆ๋Š” ์ค„์˜ ํ•จ์ˆ˜์—์„œ ๊ฑด๋„ˆ์˜จ ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ์›์†Œ๋“ค ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์‹คํ–‰๋œ๋‹ค. ์ด๋Ÿฐ ์›์†Œ๋ณ„ ์ฒ˜๋ฆฌ๋Š” (>>=) ์ •์˜์˜ map์— ํ•ด๋‹นํ•œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ์ธ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ(๊ฐ๊ฐ์˜ ๋ฆฌ์ŠคํŠธ๋Š” ์›๋ž˜ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ๊ฐ์˜ ์›์†Œ์— ๋Œ€์‘)๋Š” ๋‹จ์ผ ๋ฆฌ์ŠคํŠธ๋กœ ํ‰ํƒ„ํ™”๋œ๋‹ค. ((>>=) ์ •์˜์˜ concat) ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹(List comprehensions) ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ๋Š” ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹๊ณผ ๋†€๋ž๋„๋ก ๋น„์Šทํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. ๋ฐฉ๊ธˆ threeTurns๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์“ด do ๋ธ”๋ก์„ ์กฐ๊ธˆ ์ˆ˜์ •ํ•ด์„œ return์œผ๋กœ ๋๋‚ด๋ณด์ž. threeTurns bd = do bd1 <- nextConfigs bd bd2 <- nextConfigs bd1 bd3 <- nextConfigs bd2 return bd3 ์ด๊ฒƒ์€ ๋‹ค์Œ์˜ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์„ ์ •ํ™•ํžˆ ๋ฐ˜์˜ํ•œ๋‹ค. threeTurns bd = [ bd3 | bd1 <- nextConfigs bd, bd2 <- nextConfigs bd1, bd3 <- nextConfigs bd2 ] (๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์—์„œ, ํ•œ ๋ฆฌ์ŠคํŠธ์—์„œ ๋ฝ‘์€ ์›์†Œ๋“ค๋กœ ๊ทธ๋‹ค์Œ ์›์†Œ๋“ค์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์€ ์ „ํ˜€ ๋ฌธ์ œ ๋  ๊ฒŒ ์—†๋‹ค) ๋‘˜์ด ๋น„์Šทํ•œ ๊ฒƒ์€ ์šฐ์—ฐ์˜ ์ผ์น˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์€ concatMap์„ ์ด์šฉํ•ด ์ •์˜๋˜๋ฉฐ concatMap f xs = concat (map f xs))์ด๋‹ค. ์ด๊ฒƒ ์—ญ์‹œ ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ ๋ฐ”์ธ๋”ฉ์ด๋‹ค! ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ์˜ ๋ณธ์งˆ์„ ์ •๋ฆฌํ•˜์ž๋ฉด, ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ๋ฅผ ์œ„ํ•œ ๋ฐ”์ธ๋”ฉ์€ ๊ฒฐํ•ฉ๊ณผ ๋งคํ•‘์˜ ์กฐํ•ฉ์ด๋ฉฐ ํ•ฉ์„ฑ ํ•จ์ˆ˜์ธ concatMap์€ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ >>=์™€ ๊ฐ™๋‹ค. (๋ฌธ๋ฒ•์ƒ ์ˆœ์„œ๋งŒ ๋‹ค๋ฅด๋‹ค) ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ์™€ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์˜ ๋Œ€์‘ ๊ด€๊ณ„๋ฅผ ์™„๋ฒฝํžˆ ํ•˜๋ ค๋ฉด ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์ด ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฑธ๋Ÿฌ๋‚ด๊ธฐ๋ฅผ ์žฌํ˜„ํ•  ์ˆ˜๋‹จ์ด ํ•„์š”ํ•˜๋‹ค. ์–ด๋–ป๊ฒŒ ๊ทธ๋Ÿด ์ˆ˜ ์žˆ๋Š”์ง€๋Š” ๊ฐ€์‚ฐ์  ๋ชจ๋‚˜๋“œ ์žฅ์—์„œ ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. ์„ ํƒ ์‚ฌํ•ญ, ๊ณ ๊ธ‰ ์—ฐ์Šต๋ฌธ์ œ: ์œ ํ•œ ๊ฐœ์˜ ๊ฐ€๋Šฅ์„ฑ์„ ๊ฐ€์ง„ ๊ฒŒ์ž„์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ์žฌ๊ท€ ๋ฐ”์ธ๋”ฉ์„ ์—ฐ๊ตฌํ•ด ๋ณด๋ผ. ๋” ๋‚˜์•„๊ฐ€์„œ ๋นˆ ๋ฆฌ์ŠคํŠธ์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ๋„ ๊ทธ์ „์˜ ์ตœ์ข…์ ์ธ ๊ฐ€๋Šฅํ•œ ๋ณด๋“œ ์ƒํƒœ๋“ค์„ ์œ ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณ ๋ คํ•ด ๋ณด๋ผ. โ†ฉ 3 do ํ‘œ๊ธฐ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/do_Notation then ์—ฐ์‚ฐ์ž ๋ฒˆ์—ญํ•˜๊ธฐ bind ์—ฐ์‚ฐ์ž ๋ฒˆ์—ญํ•˜๊ธฐ fail ๋ฉ”์„œ๋“œ ์˜ˆ์ œ: ์‚ฌ์šฉ์ž ๋ฐ˜์‘ํ˜• ํ”„๋กœ๊ทธ๋žจ ๊ฐ’ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ํŽธ์˜ ๋ฌธ๋ฒ•์ผ ๋ฟ ๋…ธํŠธ ๊ฐ„๋‹จํ•œ ์ž…์ถœ๋ ฅ ์žฅ์—์„œ ๋ชจ๋‚˜๋“œ ๋ฌธ๋ฒ•์˜ ๋Œ€์•ˆ์œผ๋กœ do ๋ธ”๋ก์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ฒ˜์Œ ์†Œ๊ฐœํ–ˆ์—ˆ๋‹ค. ๊ทธ๋•Œ๋Š” ์ž…์ถœ๋ ฅ ์—ฐ์‚ฐ๋“ค์„ ์—ฐ์ด์–ด ํ•˜๊ธฐ ์œ„ํ•ด do๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ๋ชจ๋‚˜๋“œ๋Š” ์†Œ๊ฐœํ•˜์ง€ ์•Š์•˜์—ˆ๋‹ค. ์ด์ œ IO๊ฐ€ ๋˜ ํ•˜๋‚˜์˜ ๋ชจ๋‚˜๋“œ๋ผ๋Š” ๊ฒƒ์„ ํ™•์ธํ•ด ๋ณด์ž. ๋‹ค์Œ ์˜ˆ์ œ๋“ค์€ ์ „๋ถ€ IO๊ฐ€ ๊ด€๋ จ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•ž์—์„œ ๊ทธ๋žฌ๋“ฏ์ด ๊ณ„์‚ฐ computation๊ณผ ๋ชจ๋‚˜ ๋”• ๊ฐ’์„ ์•ก์…˜์ด๋ผ ์นญํ•˜๊ฒ ๋‹ค. ๋ฌผ๋ก  do๋Š” ์–ด๋Š ๋ชจ๋‚˜๋“œ์—๋„ ์ž‘๋™ํ•œ๋‹ค. do๊ฐ€ ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์— ๋Œ€ํ•ด IO๋ผ๊ณ  ํŠน๋ณ„ํ•  ๊ฒƒ์€ ์—†๋‹ค. then ์—ฐ์‚ฐ์ž ๋ฒˆ์—ญํ•˜๊ธฐ (>>) ์ฆ‰ then ์—ฐ์‚ฐ์ž๋Š” do ํ‘œ๊ธฐ์—์„œ๋„ ์ผ๋ฐ˜ ๋ฌธ๋ฒ•์—์„œ๋„ ๊ฑฐ์˜ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์•ก์…˜์˜ ์—ฐ์‡„๋ฅผ ๊ณ ๋ คํ•ด ๋ณด์ž. putStr "Hello" >> putStr " " >> putStr "world!" >> putStr "\n" do ํ‘œ๊ธฐ๋ฅผ ์ด์šฉํ•ด ๋‹ค์‹œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. do putStr "Hello" putStr " " putStr "world!" putStr "\n" ์ด ์ผ๋ จ์˜ ๋ช…๋ น์€ ๋งˆ์น˜ ๋ช…๋ นํ˜• ์–ธ์–ด์˜ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ํ•˜์Šค์ผˆ์—์„œ๋Š” ์•ก์…˜๋“ค์ด ๊ฐ™์€ ๋ชจ๋‚˜๋“œ ์•ˆ์— ์žˆ๋Š” ํ•œ ์—ฐ์‡„ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. IO ๋ชจ๋‚˜๋“œ์˜ ๋ฌธ๋งฅ์—์„œ ์šฐ๋ฆฌ๋Š” ํŒŒ์ผ์— ์“ฐ๊ธฐ, ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ ์—ด๊ธฐ, ์‚ฌ์šฉ์ž์—๊ฒŒ ์ž…๋ ฅ ์š”์ฒญํ•˜๊ธฐ ๋“ฑ์˜ ์•ก์…˜์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ด๊ฒƒ์€ do ํ‘œ๊ธฐ๋ฅผ ํŽธ์˜ ๋ฌธ๋ฒ•์„ ์“ฐ์ง€ ์•Š์€ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋กœ ํ•œ ๋‹จ๊ณ„์‹ ๋ฒˆ์—ญํ•œ ๊ฒƒ์ด๋‹ค. do action1 action2 action3 ์ด๋‹ค์Œ์€ action1 >> do action2 action3 ๊ทธ๋ ‡๊ฒŒ do ๋ธ”๋ก์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๊ณ„์†๋œ๋‹ค. bind ์—ฐ์‚ฐ์ž ๋ฒˆ์—ญํ•˜๊ธฐ (>>=)๋Š” do ํ‘œ๊ธฐ๋กœ ์˜ฎ๊ธฐ๊ธฐ๊ฐ€ ์กฐ๊ธˆ ์–ด๋ ต๋‹ค. (>>=)๋Š” ํ•˜๋‚˜์˜ ๊ฐ’, ์ฆ‰ ์•ก์…˜ ๋˜๋Š” ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ผ๋ จ์˜ ๋ฐ”์ธ๋”ฉ์˜ ์•„๋ž˜์ชฝ์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค. do ํ‘œ๊ธฐ๋Š” ์ „๋‹ฌ๋œ ๊ฐ’์— <-๋ฅผ ์ด์šฉํ•ด ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ํ• ๋‹นํ•œ๋‹ค. do x1 <- action1 x2 <- action2 action3 x1 x2 x1๊ณผ x2๋Š” action1๊ณผ action2์˜ ๊ฒฐ๊ณผ๊ฐ’์ด๋‹ค. ๋งŒ์•ฝ action1์ด IO Integer๋ผ๋ฉด x1์€ Integer์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. x1๊ณผ x2์— ์ €์žฅ๋œ ๊ฐ’๋“ค์€ action3์— ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋˜๊ณ , action3๋Š” ์„ธ ๋ฒˆ์งธ ์•ก์…˜์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด do ๋ธ”๋ก์€ ๋‹ค์Œ์˜ ์‹ค์ œ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ์™€ ๋งŽ์€ ๋ถ€๋ถ„์—์„œ ๋™๋“ฑํ•˜๋‹ค. action1 >>= \ x1 -> action2 >>= \ x2 -> action3 x1 x2 (>>=)์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ํ•จ์ˆ˜๋กœ์„œ, ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ์•ก์…˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฌด์—‡์„ ํ• ์ง€๋ฅผ ๊ธฐ์ˆ ํ•œ๋‹ค. ๋žŒ๋‹ค์˜ ์—ฐ์‡„๋Š” ๊ทธ ๊ฒฐ๊ณผ๋“ค์„ ์•„๋ž˜์ชฝ์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค. ๊ด„ํ˜ธ๋ฅผ ์ถ”๊ฐ€๋กœ ์“ฐ์ง€ ์•Š์•„๋„ ๋žŒ๋‹ค๊ฐ€ ํ‘œํ˜„์‹์˜ ๋๊นŒ์ง€ ํ™•์žฅ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์ˆ™์ง€ํ•  ๊ฒƒ. action3๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์‹œ์ ์—๋„ x1์€ ์Šค์ฝ”ํ”„ ๋‚ด์— ์žˆ๋‹ค. ์ค„๋ฐ”๊ฟˆ๊ณผ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ์ด์šฉํ•ด์„œ ๋žŒ๋‹ค ์—ฐ์‡„๋ฅผ ์ข€ ๋” ์ฝ๊ธฐ ์ข‹๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. action1 >>= \ x1 -> action2 >>= \ x2 -> action3 x1 x2 ์ด ์ฝ”๋“œ์—์„œ๋Š” ๊ฐ๊ฐ์˜ ๋žŒ๋‹ค ํ•จ์ˆ˜์˜ ์Šค์ฝ”ํ”„๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. do ํ‘œ๊ธฐ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ๋ฌถ์–ด๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. action1 >>= \ x1 -> action2 >>= \ x2 -> action3 x1 x2 ์ด๋Ÿฐ ํ‘œํ˜„๋ฒ•์˜ ์ฐจ์ด๋Š” ์–ด๋–ค ๊ฒŒ ๋ณด๊ธฐ ์ข‹์œผ๋ƒ์˜ ๋ฌธ์ œ์ผ ๋ฟ์ด๋‹ค. 1 fail ๋ฉ”์„œ๋“œ ์•ž์„œ ๋žŒ๋‹ค๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๊ฐ€ do ๋ธ”๋ก๊ณผ "๊ฑฐ์˜ ๋™๋“ฑํ•˜๋‹ค"๋ผ๊ณ  ๋งํ•œ ๋ฐ” ์žˆ๋‹ค. ์ด๋Š” ์ •ํ™•ํ•œ ๋ฒˆ์—ญ์ด ์•„๋‹Œ๋ฐ do ํ‘œ๊ธฐ์—์„œ๋Š” ํŒจํ„ด ๋งค์นญ ์‹คํŒจ์— ๋Œ€ํ•œ ํŠน๋ณ„ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. <-๋‚˜ ->์˜ ์™ผ์ชฝ์— ์œ„์น˜ํ•œ x1๊ณผ x2๋Š” ํŒจํ„ด ๋งค์นญ์˜ ๋Œ€์ƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ action1์ด Maybe Integer๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด do ๋ธ”๋ก์„ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. do Just x1 <- action1 x2 <- action2 action3 x1 x2 ๊ทธ๋ฆฌ๊ณ  x1์€ Integer ๊ฐ’์ด ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ action1์ด Nothing์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด? ๋ณดํ†ต์€ ํ”„๋กœ๊ทธ๋žจ์ด non-exhaustive patterns(์ผ์น˜ํ•˜๋Š” ํŒจํ„ด ์—†์Œ) ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚ค๋ฉฐ ๊ณ ์žฅ ๋‚  ๊ฒƒ์ด๋‹ค. ์ด ์˜ค๋ฅ˜๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด head๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ผ์–ด๋‚˜๋Š” ๊ทธ๋Ÿฐ ์˜ค๋ฅ˜๋‹ค. ํ•˜์ง€๋งŒ do ํ‘œ๊ธฐ์—์„œ๋Š” ๊ด€๋ จ๋œ ๋ชจ๋‚˜๋“œ์˜ fail ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํŒจ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์œ„์˜ do ๋ธ”๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฒˆ์—ญ๋œ๋‹ค. action1 >>= f where f (Just x1) = do x2 <- action2 action3 x1 x2 f _ = fail "..." -- ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ƒ์„ฑํ•œ ๋ฉ”์‹œ์ง€ fail์ด ์‹ค์ œ๋กœ ํ•˜๋Š” ์ผ์€ ๋ชจ๋‚˜๋“œ ์ธ์Šคํ„ด์Šค๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ค. ๋ณดํ†ต์€ ํŒจํ„ด ๋งค์นญ ์˜ค๋ฅ˜๋ฅผ ๋‹ค์‹œ ๋˜์ง€์ง€๋งŒ ๋‚˜๋ฆ„๋Œ€๋กœ ์‹คํŒจ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ชจ๋‚˜๋“œ๋„ ์žˆ๋‹ค. ๊ฐ€๋ น Maybe์˜ fail์€ fail _ = Nothing์ด๊ณ , ๋น„์Šทํ•˜๊ฒŒ ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ์˜ ๊ฒฝ์šฐ fail _ = []์ด๋‹ค. 2 fail ๋ฉ”์„œ๋“œ๋Š” do ํ‘œ๊ธฐ์˜ ์žฅ์น˜ arfifact๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋‚˜๋“œ๋ฅผ ์œ„ํ•ด fail์ด ๋ญ”๊ฐ€ ํ•ฉ๋ฆฌ์ ์ธ ๋Œ€์ฒ˜๋ฅผ ํ•ด์ค„ ๊ฑฐ๋ผ ํ™•์‹ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด, fail์„ ์ง์ ‘ ํ˜ธ์ถœํ•˜๋Š” ๋Œ€์‹  ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰๋˜๋Š” ํŒจํ„ด ๋งค์นญ ์‹คํŒจ ์ฒ˜๋ฆฌ๋ฅผ ๋ฏฟ์–ด์•ผ ํ•œ๋‹ค. ์˜ˆ์ œ: ์‚ฌ์šฉ์ž ๋ฐ˜์‘ํ˜• ํ”„๋กœ๊ทธ๋žจ ์ž ๊น ์šฐ๋ฆฌ๋Š” ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ ์œ„ํ•ด putStr๊ณผ getLine์„ ๋ฒˆ๊ฐˆ์•„ ์“ธ ๊ฒƒ์ด๋‹ค. ์ถœ๋ ฅ์— ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด System.IO๋ฅผ ๋“ค์—ฌ์˜ฌ ๋•Œ ์ถœ๋ ฅ ๋ฒ„ํผ๋ง output buffering์„ ๊บผ์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด ์ฝ”๋“œ์˜ ์ฒซ ์ค„์— hSetBuffering stdout NoBuffering์„ ์ž…๋ ฅํ•˜๋ผ. ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ํ•˜๋ ค๋ฉด ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ๋•Œ๋งˆ๋‹ค(์ฆ‰ getLine์„ ์“ธ ๋•Œ๋งˆ๋‹ค) ๊ทธ์— ์•ž์„œ hFlush stdout์œผ๋กœ ์ถœ๋ ฅ ๋ฒ„ํผ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์”ป์–ด๋‚ผ flush ์ˆ˜ ์žˆ๋‹ค. ghci์—์„œ ์ด ์ฝ”๋“œ๋ฅผ ์‹œํ—˜ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๊ทธ๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค. ์‚ฌ์šฉ์ž์—๊ฒŒ ์„ฑ๊ณผ ์ด๋ฆ„์„ ์งˆ๋ฌธํ•˜๋Š” ๋‹ค์Œ์˜ ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์‚ดํŽด๋ณด์ž. nameDo :: IO () nameDo = do putStr "What is your first name? " first <- getLine putStr "And your last name? " last <- getLine let full = first ++ " " ++ last putStrLn ("Pleased to meet you, " ++ full ++ "!") ์ด๋ฅผ ์‹ค์ œ ๋ชจ๋‚˜ ๋”• ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. nameLambda :: IO () nameLambda = putStr "What is your first name? " >> getLine >>= \ first -> putStr "And your last name? " >> getLine >>= \ last -> let full = first ++ " " ++ last in putStrLn ("Pleased to meet you, " ++ full ++ "!") ์ด์ฒ˜๋Ÿผ ๋‹จ์ง€ ์—ฌ๋Ÿฌ ์•ก์…˜์„ ์—ฐ์‡„ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, do ํ‘œ๊ธฐ์˜ ๋ช…๋ นํ˜• ์Šคํƒ€์ผ์ด ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ํŽธํ•˜๊ฒŒ ๋Š๊ปด์ง„๋‹ค. ํ•œํŽธ bind์™€ ๋žŒ๋‹ค๋ฅผ ๋ช…์‹œํ•œ ๋ชจ๋‚˜ ๋”• ์ฝ”๋“œ๋Š” ์ฒ˜์Œ์—” ๋ณ„๋กœ์ง€๋งŒ ์ž๊พธ ์ ‘ํ•˜๋‹ค ๋ณด๋‹ˆ ๊ดœ์ฐฎ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์—์„œ do ๋ธ”๋ก ์•ˆ์— let ๋ฌธ์ด ์žˆ๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. ์ด let ๋‹ค์Œ์— ์˜ค๋Š” ์ค„์ด, ํŽธ์˜ ๋ฌธ๋ฒ•์„ ์“ฐ์ง€ ์•Š์€ ๋‘ ๋ฒˆ์งธ ๋ฒ„์ „์—์„œ๋Š” in์— ๋”ฐ๋ผ๋ถ™์–ด, ์ •๊ทœ let ํ‘œํ˜„์‹์ด ๋œ๋‹ค. ๊ฐ’ ๋ฐ˜ํ™˜ํ•˜๊ธฐ do ํ‘œ๊ธฐ์˜ ๋งˆ์ง€๋ง‰ ๋ช…๋ น๋ฌธ์€ do ๋ธ”๋ก์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋‹ค. ์•ž์˜ ์˜ˆ์ œ์—์„œ ๊ฒฐ๊ด๊ฐ’์˜ ํƒ€์ž…์€ IO (), ์ฆ‰ IO ๋ชจ๋‚˜๋“œ ์ƒ์—์„œ ๋น„์–ด์žˆ๋Š” ๊ฐ’์ด์—ˆ๋‹ค. ์ด ์˜ˆ์ œ๋ฅผ ์ˆ˜์ •ํ•ด์„œ, ํš๋“ํ•œ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” IO String์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•ด๋ณด์ž. ์šฐ๋ฆฌ๊ฐ€ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ return์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋ฟ์ด๋‹ค. nameReturn :: IO String nameReturn = do putStr "What is your first name? " first <- getLine putStr "And your last name? " last <- getLine let full = first ++ " " ++ last putStrLn ("Pleased to meet you, " ++ full ++ "!") return full ์ด ์˜ˆ์ œ๋Š” IO ๋ชจ๋‚˜๋“œ ๋‚ด์˜ ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„๋˜๋Š” ์„ฑ๋ช…์„ "๋ฐ˜ํ™˜"ํ•  ๊ฒƒ์ด๊ณ , ์ด๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ์ด ์ด๋ฆ„์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. greetAndSeeYou :: IO () greetAndSeeYou = do name <- nameReturn putStrLn ("See you, " ++ name ++ "!") nameReturn์ด ์‹คํ–‰๋˜๋ฉด ๋ฐ˜ํ™˜๋œ ๊ฒฐ๊ด๊ฐ’(nameReturn ํ•จ์ˆ˜ ์•ˆ์—์„  "full"์ด์—ˆ๋˜ ๊ฒƒ)์ด greetAndSeeYou ํ•จ์ˆ˜ ์•ˆ์˜ "name" ๋ณ€์ˆ˜์— ํ• ๋‹น๋  ๊ฒƒ์ด๋‹ค. nameReturn์—์„œ ์ธ์‚ฌ๋ง ๋ถ€๋ถ„์€ ํ™”๋ฉด์— ์ถœ๋ ฅ๋˜๋Š”๋ฐ ์ด ์—ญ์‹œ ๊ณ„์‚ฐ ๊ณผ์ •์˜ ์ผ๋ถ€์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด "see you" ๋ฉ”์‹œ์ง€๊ฐ€ ์ถ”๊ฐ€๋กœ ์ถœ๋ ฅ๋  ๊ฒƒ์ด๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์€ IO ()๋‹ค. C ๊ฐ™์€ ๋ช…๋ นํ˜• ์–ธ์–ด๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ํ•˜์Šค์ผˆ์˜ return์ด C์˜ return๊ณผ ์ผ์น˜ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ• ์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์˜ˆ์ œ๋ฅผ ์กฐ๊ธˆ ๋ณ€ํ˜•ํ•˜๋ฉด ๊ทธ๋Ÿฐ ์ƒ๊ฐ์ด ์‚ฌ๋ผ์งˆ ๊ฒƒ์ด๋‹ค. nameReturnAndCarryOn = do putStr "What is your first name? " first <- getLine putStr "And your last name? " last <- getLine let full = first++" "++last putStrLn ("Pleased to meet you, "++full++"!") return full putStrLn "I am not finished yet!" ๋งˆ์ง€๋ง‰ ์ค„์˜ ๋ฌธ์ž์—ด์€ ์ถœ๋ ฅ๋˜๋Š”๋ฐ, return์€ ํ๋ฆ„์„ ๋๋‚ด๋Š” ๋ช…๋ น๋ฌธ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (C๋‚˜ ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋Š” ๊ทธ๋žฌ์„ ๊ฒƒ์ด๋‹ค) ๊ทธ๋ฆฌ๊ณ  nameReturnAndCarryOn์˜ ํƒ€์ž…์€ IO ()๋‹ค. ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ํ›„์—๋Š” return full์— ์˜ํ•ด ์ƒ์„ฑ๋œ IO String์ด ํ”์ ๋„ ์—†์ด ์‚ฌ๋ผ์ง„๋‹ค. ํŽธ์˜ ๋ฌธ๋ฒ•์ผ ๋ฟ ๋ฌธ๋ฒ•์ƒ์˜ ํŽธ์˜์ธ do ํ‘œ๊ธฐ๋Š” ๊ทผ๋ณธ์ ์œผ๋กœ ์ƒˆ๋กœ์šด ๊ฐœ๋…์„ ์•„๋ฌด๊ฒƒ๋„ ๋„์ž…ํ•˜์ง€ ์•Š์ง€๋งŒ, ๊ทธ ๋ช…๋ฃŒํ•จ๊ณผ ๋ฉ‹ ๋•Œ๋ฌธ์— ์• ์šฉ๋œ๋‹ค. ํ•˜์ง€๋งŒ do๋Š” ๋‹จ์ผ ์•ก์…˜์—๋Š” ์ ˆ๋Œ€ ์“ฐ์ด์ง€ ์•Š๋Š”๋‹ค. ํ•˜์Šค์ผˆ์‹ "Hello World"๋Š” ๋‹จ์ง€ main = putStrLn "Hello world!" ์ด๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋Š” ์ „ํ˜€ ๋ถˆํ•„์š”ํ•˜๋‹ค. fooRedundant = do x <- bar return x ๋ชจ๋‚˜๋“œ์˜ ๋ฒ•์น™ ๋•์— ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํžˆ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋ ‡๊ฒŒ ํ•ด์•ผ ํ•œ๋‹ค. foo = bar ํ•จ์ˆ˜ ํ•ฉ์„ฑ๊ณผ ๊ด€๋ จํ•ด ๋ฏธ๋ฌ˜ํ•˜์ง€๋งŒ ์ค‘์š”ํ•œ ์ ์ด ์žˆ๋‹ค. ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋“ฏ์ด ๋ฐ”๋กœ ์•ž ์ ˆ์˜ greetAndSeeYou ์•ก์…˜์€ ์ด๋ ‡๊ฒŒ ๊ณ ์น  ์ˆ˜ ์žˆ๋‹ค. greetAndSeeYou :: IO () greetAndSeeYou = nameReturn >>= \ name -> putStrLn ("See you, " ++ name ++ "!") ๋žŒ๋‹ค๊ฐ€ ์ข€ ๋ณด๊ธฐ ํž˜๋“ค๋‹ค๋ฉด ์–ด๋”˜๊ฐ€์— printSeeYou ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. printSeeYou :: String -> IO () printSeeYou name = putStrLn ("See you, " ++ name ++ "!") ๊ทธ๋Ÿฌ๋ฉด ํ•จ์ˆ˜ ์ •์˜์— ๋žŒ๋‹ค๋„ do๋„ ์—†์–ด์„œ ๊นจ๋—ํ•ด์ง„๋‹ค. greetAndSeeYou :: IO () greetAndSeeYou = nameReturn >>= printSeeYou ํ˜น์€, ๋ชจ๋‚˜๋”•์ด ์•„๋‹Œ seeYou ํ•จ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด seeYou :: String -> String seeYou name = "See you, " ++ name ++ "!" ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. -- Reminder: liftM f m == m >>= return . f == fmap f m greetAndSeeYou :: IO () greetAndSeeYou = liftM seeYou nameReturn >>= putStrLn ์ด ๋งˆ์ง€๋ง‰ ์˜ˆ์‹œ๋ฅผ liftM๊ณผ ํ•จ๊ป˜ ๊ธฐ์–ตํ•ด ๋‘˜ ๊ฒƒ. ๋ชจ๋‚˜ ๋”• ์ฝ”๋“œ์—์„œ ๋น„ ๋ชจ๋‚˜ ๋”• ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ณง ๋Œ์•„์˜ฌ ํ…๋ฐ, liftM์ด ํ•จ๊ป˜ํ•  ๊ฒƒ์ด๋‹ค. ๋…ธํŠธ 1. โ†‘์‚ฌ์‹ค ์ด ๊ฒฝ์šฐ์—๋Š” ๋“ค์—ฌ ์“ฐ๊ธฐ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. ๋‹ค์Œ๋„ ์˜ฌ๋ฐ”๋ฅธ ์ฝ”๋“œ๋‹ค. action1 >>= \ x1 -> action2 >>= \ x2 -> action3 x1 x2 ๋ฌผ๋ก  ์›ํ•œ๋‹ค๋ฉด ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ๋” ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹ค์Œ์€ ๊ฐˆ ๋ฐ๊นŒ์ง€ ๊ฐ„ ์˜ˆ์‹œ๋‹ค. action1 >>= \ x1 -> action2 >>= \ x2 -> action3 x1 x2 ํ™•์‹คํžˆ ๋„ˆ๋ฌดํ–ˆ์ง€๋งŒ, ๋” ๋‚˜๋น ์งˆ ์ˆ˜๋„ ์žˆ๋‹ค. action1 >>= \ x1 -> action2 >>= \ x2 -> action3 x1 x2 ์˜ฌ๋ฐ”๋ฅธ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ์ง€๋งŒ ๋„์ €ํžˆ ์ฝ์„ ์ˆ˜๊ฐ€ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์ œ๋ฐœ ์ด๋Ÿฐ ์‹์œผ๋กœ ์“ฐ์ง€ ์•Š๊ธฐ๋ฅผ. ์ผ๊ด€๋˜๊ณ  ์˜๋ฏธ ์žˆ๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๊ฒƒ. 2. โ†‘"ํŒจํ„ด ๋งค์นญ" ์žฅ์—์„œ ์ง€์ ํ–ˆ๋“ฏ์ด, ์ด๊ฒƒ์ด ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์—์„œ์˜ ํŒจํ„ด ๋งค์นญ ์‹คํŒจ๊ฐ€ ์กฐ์šฉํžˆ ๋ฌด์‹œ๋˜๋Š” ์ด์œ ๋‹ค. 4 IO ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Understanding_monads/IO ์ž…์ถœ๋ ฅ๊ณผ ์ˆœ์ˆ˜์„ฑ ํ•จ์ˆ˜์™€ ์ž…์ถœ๋ ฅ ์•ก์…˜ ๊ฒฐํ•ฉํ•˜๊ธฐ do ํ‘œ๊ธฐ ์žฌ๊ฒ€ํ†  ํ”„๋กœ๊ทธ๋žจ์˜ ์ผ๋ถ€๋กœ์„œ์˜ ์šฐ์ฃผ ์ˆœ์ˆ˜์™€ ๋น„์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜•๊ณผ ๋ช…๋ นํ˜• ์ž…์ถœ๋ ฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋‚˜ ๋”• ์ œ์–ด ๊ตฌ์กฐ ๋…ธํŠธ ํ•˜์Šค์ผˆ์€ ๋Š๊ธ‹ํ•œ ํ•จ์ˆ˜ํ˜• ์–ธ์–ด๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์‹ค์—์„œ๋Š” ์ž…์ถœ๋ ฅ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์ž…์ถœ๋ ฅ์€ ๋Š๊ธ‹ํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋‹ค. ๋Š๊ธ‹ํ•œ computation์€ ๊ฐ’์ด ์‹ค์ œ๋กœ ํ•„์š”ํ•  ๋•Œ๊ฐ€ ๋˜์–ด์„œ์•ผ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‘์„œ์—†์ด ๋Š๊ธ‹ํ•œ ์ž…์ถœ๋ ฅ์€ ํ˜„์‹ค์˜ ์‹คํ–‰ ์ˆœ์„œ ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ํ•˜์Šค์ผˆ์€ ์ด๋Ÿฐ ๋ฌธ์ œ์— IO ๋ชจ๋‚˜๋“œ๋กœ ๋Œ€์ฒ˜ํ•œ๋‹ค. ์ž…์ถœ๋ ฅ๊ณผ ์ˆœ์ˆ˜์„ฑ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋Š” ์ˆœ์ˆ˜ํ•œ ํ•จ์ˆ˜๋‹ค. ๊ฐ™์€ ์ธ์ž๋ฅผ ๋ฐ›์œผ๋ฉด ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋Š” ๋ฏฟ์„ ์ˆ˜ ์žˆ๊ณ  ์˜ˆ์ธก ๊ฐ€๋Šฅํ•˜๋‹ค. ๋””๋ฒ„๊น…๊ณผ ๊ฒ€์ฆ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ธ์ž ์™ธ์—๋Š” ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒƒ์ด ์—†๋‹ค๊ณ  ํ™•์‹ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๊ฐ–์ถ”๋Š” ๊ฒƒ๋„ ์‰ฝ๋‹ค. ํ”„๋กœ๊ทธ๋žจ ์•ˆ์— ์™„์ „ํžˆ ํฌํ•จ๋œ ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ปดํŒŒ์ผ๋œ ์ฝ”๋“œ๋ฅผ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜๋“ค์„ ์ด์ฒด์ ์œผ๋กœ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿผ ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ ์—ด๊ธฐ, ํŒŒ์ผ ์ž‘์„ฑํ•˜๊ธฐ, ์™ธ๋ถ€์—์„œ ์ž…๋ ฅ ์ฝ์–ด์˜ค๊ธฐ ๋“ฑ ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ ์ด์ƒ์˜ ์ผ์„ ํ•˜๋Š” ์•ก์…˜๋“ค์„ ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌํ•ด์•ผ ํ• ๊นŒ? ๊ทธ ํ•ต์‹ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ด๋Ÿฐ ์•ก์…˜์€ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. IO ๋ชจ๋‚˜๋“œ๋Š” ์•ก์…˜์„ ํ•˜์Šค ์ผˆ ๊ฐ’์œผ๋กœ ํ‘œํ˜„ํ•˜์—ฌ, ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ทธ ๊ฐ’์„ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ํ•จ์ˆ˜์™€ ์ž…์ถœ๋ ฅ ์•ก์…˜ ๊ฒฐํ•ฉํ•˜๊ธฐ ํ•จ์ˆ˜์™€ ์ž…์ถœ๋ ฅ์„ ๊ฒฐํ•ฉํ•˜์—ฌ ์™„์ „ํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด๋ณด์ž. ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฌธ์ž์—ด ์ž…๋ ฅ์„ ์š”์ฒญํ•œ๋‹ค. ๊ทธ ๋ฌธ์ž์—ด์„ ์ฝ์–ด๋“ค์ธ๋‹ค. liftM์„ ์‚ฌ์šฉํ•ด, ๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋งŒ๋“œ๋Š” shout ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค. ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. module Main where import Data.Char (toUpper) import Control.Monad main = putStrLn "Write your string: " >> liftM shout getLine >>= putStrLn shout = map toUpper ์™„์ „ํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ–ˆ์ง€๋งŒ ํƒ€์ž… ์ •์˜๋Š” ํ•˜๋‚˜๋„ ํฌํ•จ์‹œํ‚ค์ง€ ์•Š์•˜๋‹ค. ์–ด๋Š ๋ถ€๋ถ„์ด ํ•จ์ˆ˜๊ณ  ์–ด๋Š ๋ถ€๋ถ„์ด IO ์•ก์…˜์ด๊ณ  ์–ด๋””๊ฐ€ ๊ฐ’์ธ ๊ฑธ๊นŒ? ghci๋กœ ์ด ํ”„๋กœ๊ทธ๋žจ์„ ๋ถˆ๋Ÿฌ์™€์„œ ํƒ€์ž…์„ ํ™•์ธํ•ด ๋ณด์ž. main :: IO () putStrLn :: String -> IO () "Write your string: " :: [Char] (>>) :: Monad m => m a -> m b -> m b liftM :: Monad m => (a1 -> r) -> m a1 -> m r shout :: [Char] -> [Char] getLine :: IO String (>>=) :: Monad m => m a -> (a -> m b) -> m b ๋งŽ์€ ์ •๋ณด๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. ์ „๋ถ€ ๋ดค๋˜ ๊ฒƒ์ด์ง€๋งŒ ๋‹ค์‹œ ๊ฒ€ํ† ํ•ด ๋ณด์ž. main์€ IO ()๋‹ค. ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋Š” a -> b ์‹์˜ ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค. ์šฐ๋ฆฌ์˜ ํ”„๋กœ๊ทธ๋žจ ์ž์ฒด๋Š” IO ์•ก์…˜์ด๋‹ค. putStrLn์€ ํ•จ์ˆ˜๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ๊ฒฐ๊ณผ๋Š” IO ์•ก์…˜์ด๋‹ค. "Write your string: " ํ…์ŠคํŠธ๋Š” String ๊ฐ’์ด๋‹ค. (String์€ [Char]์˜ ๋™์˜์–ด์ผ ๋ฟ์ด๋‹ค) ์ด ํ…์ŠคํŠธ๋Š” putStrLn์˜ ์ธ์ž๋กœ ์“ฐ์˜€๊ณ  putStrLn์˜ ๊ฒฐ๊ณผ์ธ IO ์•ก์…˜์— ํ†ตํ•ฉ๋˜์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ putStrLn์€ ํ•จ์ˆ˜์ด์ง€๋งŒ IO ์•ก์…˜์œผ๋กœ ํ‰๊ฐ€๋œ๋‹ค. IO ํƒ€์ž…์˜ () ๋ถ€๋ถ„์€ ์ด์–ด์ง€๋Š” ํ•จ์ˆ˜๋‚˜ ์•ก์…˜์— ์•„๋ฌด๊ฒƒ๋„ ์ „๋‹ฌํ•  ์ˆ˜ ์—†์Œ์„ ์ง€์‹œํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์ด ํ•ต์‹ฌ์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ๋•Œ๋กœ IO ์•ก์…˜์ด ๋ญ”๊ฐ€๋ฅผ "๋ฐ˜ํ™˜ํ•œ๋‹ค"๋ผ๊ณ  ๋งํ•˜๊ณ ๋Š” ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฑธ ๊ณง์ด๊ณง๋Œ€๋กœ ๋ฐ›์•„๋“ค์ด๋ฉด ํ˜ผ๋ž€์„ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ๋งํ•  ๋•Œ๋Š” ๊ทธ ๋œป์ด ๋ช…ํ™•ํ•˜๋‹ค. ํ•˜์ง€๋งŒ IO ์•ก์…˜์€ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. getLine์„ ๋ณด์ž. getLine์€ ๊ฐ’์„ ํ•˜๋‚˜ ์ œ๊ณตํ•˜๋Š” IO ์•ก์…˜์ด๋‹ค. getLine์€ String ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ๋ฐ, getLine์ด ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์˜คํžˆ๋ ค getLine์€ IO ์•ก์…˜์ด๋ฉฐ, ํ‰๊ฐ€๋˜๋Š” ์‹œ์ ์— ํ•œ String ๊ฐ’์„ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค. ์ด ๊ฐ’์€ ์ด์–ด์ง€๋Š” fmap, liftM, (>>=) ๊ฐ™์€ ํ•จ์ˆ˜๋“ค์— ์ „๋‹ฌ๋  ์ˆ˜ ์žˆ๋‹ค. getLine์„ ์‚ฌ์šฉํ•ด String ๊ฐ’์„ ํš๋“ํ•  ๋•Œ, ๊ทธ ๊ฐ’์€ ๋ชจ๋‚˜ ๋”• ๊ฐ’์ด๋ฉฐ ๊ทธ ์ด์œ ๋Š” ๊ฐ’์ด IO functor ์•ˆ์— ๋“ค์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ๊ฐ’์€ ํ‰๋ฒ”ํ•œ(๋ชจ๋‚˜ ๋”•์ด ์•„๋‹ˆ๊ณ  functorial์ด ์•„๋‹Œ) ๊ฐ’์„ ์ทจํ•˜๋Š” ํ•จ์ˆ˜์— ์ง์ ‘ ์ „๋‹ฌํ•  ์ˆ˜ ์—†๋‹ค. liftM์€ ๋ชจ๋‚˜ ๋”• ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ์‚ฌ์ด์— ๋น„ ๋ชจ๋‚˜ ๋”• ํ•จ์ˆ˜๋ฅผ ์ทจํ•˜๋Š” ์ผ์„ ํ•œ๋‹ค. ์ด๋ฏธ ๋ดค๋“ฏ์ด (>>=)๋Š” ๋น„ ๋ชจ๋‚˜ ๋”• ํ•จ์ˆ˜๋ฅผ ์ทจํ•˜๋Š” ํ•จ์ˆ˜์— ๋ชจ๋‚˜ ๋”• ๊ฐ’์„ ์ „๋‹ฌํ•ด ๋ชจ๋‚˜ ๋”• ๊ฐ’์„ ๋Œ๋ ค๋ฐ›๋Š” ์ผ์„ ํ•œ๋‹ค. liftM์€ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜์—์„œ ๋น„ ๋ชจ๋‚˜ ๋”• ๊ฒฐ๊ณผ๋ฅผ ์ทจํ•ด ๋‹จ์ง€ (>>=)๋ฅผ ์œ„ํ•ด ๋ชจ๋‚˜ ๋”• ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ ์•ˆ์˜ ๋น„ ๋ชจ๋‚˜ ๋”• ๊ฐ’์„ ๋‹ค์Œ ํ•จ์ˆ˜์— ๋„˜๊ธฐ๋Š”๋ฐ ์ด๋Ÿฐ ๊ฒƒ์ด ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ณด์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ”๋กœ ์ด๋Ÿฐ ์—ฐ์‡„๊ฐ€ ๋ฏฟ์„ ์ˆ˜ ์žˆ๋Š” ์ˆœ์„œ๋ฅผ ๋งŒ๋“ค์–ด ์ˆœ์ˆ˜ ํ•จ์ˆ˜์™€ IO ์•ก์…˜์„ ํ†ตํ•ฉํ•  ๋•Œ ๋ชจ๋‚˜๋“œ๋ฅผ ๊ฒฐ์ •์ ์ธ ์š”์†Œ๋กœ ๋งŒ๋“ ๋‹ค. do ํ‘œ๊ธฐ ์žฌ๊ฒ€ํ†  ์—ฐ์‡„์— ์ฃผ๋ชฉํ•˜๋ฉด, do ํ‘œ๊ธฐ๋Š” IO ๋ชจ๋‚˜๋“œ์™€ ํŠนํžˆ ์–ด์šธ๋ฆฐ๋‹ค. ๋‹ค์Œ ํ”„๋กœ๊ทธ๋žจ์€ putStrLn "Write your string: " >> liftM shout getLine >>= putStrLn ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ ์น  ์ˆ˜ ์žˆ๋‹ค. do putStrLn "Write your string: " string <- getLine putStrLn (shout string) ํ”„๋กœ๊ทธ๋žจ์˜ ์ผ๋ถ€๋กœ์„œ์˜ ์šฐ์ฃผ IO ๋ชจ๋‚˜๋“œ๋ฅผ ๋ณด๋Š” ํ•œ ๊ฐ€์ง€ ๊ด€์ ์€, IO a๊ฐ€ ํ•˜๋‚˜์˜ ๊ณ„์‚ฐ computation์ด๋ฉฐ ์ž…์ถœ๋ ฅ์„ ํ†ตํ•ด ์„ธ๊ณ„์˜ ์ƒํƒœ๋ฅผ ๋ฐ”๊พธ๋ฉด์„œ a ํƒ€์ž…์˜ ๊ฐ’์„ ์ œ๊ณตํ•œ๋‹ค๊ณ  ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ๋‹น์—ฐํžˆ ์šฐ๋ฆฌ๊ฐ€ ๋ง ๊ทธ๋Œ€๋กœ ์„ธ๊ณ„์˜ ์ƒํƒœ๋ฅผ ์–ด์ฐŒํ•  ์ˆ˜๋Š” ์—†๋‹ค. IO functor๋Š” ์ถ”์ƒ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์„ธ๊ณ„๋Š” ์—ฌ๋Ÿฌ๋ถ„์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ์ถ”์–ด์ ธ ์žˆ๋‹ค. (์ฆ‰ IO๋ฅผ ํŒŒํ—ค์ณ ๊ทธ ์•ˆ์˜ ๊ฐ’์„ ๋“ค์—ฌ๋‹ค๋ณผ ์ˆ˜ ์—†๋‹ค. Maybe์™€๋Š” ๋‹ค๋ฅธ ์ƒํ™ฉ์ด๋‹ค.) ์ด๋ ‡๊ฒŒ ๋ณด๋ฉด IO๋Š” ๊ณง ์‚ดํŽด๋ณผ State ๋ชจ๋‚˜๋“œ์™€ ๋‹ฎ์€ ๊ตฌ์„์ด ์žˆ๋‹ค. ํ•˜์ง€๋งŒ State์˜ ๊ฒฝ์šฐ์—๋Š” ๋ณ€๊ฒฝ๋  ์ƒํƒœ๊ฐ€ ์ผ๋ฐ˜์ ์ธ ํ•˜์Šค ์ผˆ ๊ฐ’์œผ๋กœ ๋งŒ๋“ค์–ด์ ธ ์žˆ์–ด์„œ ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ง์ ‘ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ์ฃผ๋ฅผ IO๋ฅผ ํ†ตํ•ด ํ•˜์Šค ์ผˆ ๊ฐ’์— ์˜ํ•ด ์˜ํ–ฅ์„ ๋ฐ›๊ณ  ๋˜ ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฐ์ฒด๋กœ ๋ฐ”๋ผ๋ณด๋Š” ์ด๋Ÿฐ ๋ฐœ์ƒ์€ ๋‹จ์ง€ ๋น„์œ ์ด๋ฉฐ ์ตœ๋Œ€ํ•œ ๋Š์Šจํ•˜๊ฒŒ ํ•ด์„ํ–ˆ์„ ๋ฟ์ด๋‹ค. ๋” ํ˜„์‹ค์ ์ธ ์‚ฌ์‹ค์€ IO๊ฐ€ ์•„์ฃผ ๊ธฐ๋ณธ์ ์ธ ์ˆ˜์ค€์˜ ์ž‘์—…์„ ํ•˜์Šค ์ผˆ ์–ธ์–ด๋กœ ๋“ค์—ฌ์™”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 1 ํ•˜์Šค์ผˆ์€ ํ•˜๋‚˜์˜ ์ถ”์ƒ์ด๊ณ , ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์€ ์‹ค์ œ๋กœ ์‹คํ–‰๋˜๋ ค๋ฉด ๊ธฐ๊ณ„ ์ฝ”๋“œ๋กœ ์ปดํŒŒ์ผ๋˜์–ด์•ผ ํ•œ๋‹ค. IO์˜ ์‹ค์ œ ์ž‘๋™์€ ์•„์ฃผ ์ € ์ˆ˜์ค€์˜ ์ถ”์ƒ์—์„œ ์ผ์–ด๋‚˜๊ณ , ํ•˜์Šค ์ผˆ ์–ธ์–ด ๊ทธ ์ž์ฒด์˜ ์ •์˜์— ๋ฐ€์ ‘ํžˆ ์—ฐ๊ด€๋˜์–ด ์žˆ๋‹ค.2 ์ˆœ์ˆ˜์™€ ๋น„์ˆœ์ˆ˜ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋ณด์ž. speakTo :: (String -> String) -> IO String speakTo fSentence = liftM fSentence getLine -- ์šฉ๋ก€. sayHello :: IO String sayHello = speakTo (\name -> "Hello, " ++ name ++ "!") ์ž…์ถœ๋ ฅ ์•ก์…˜์„ ์œ„ํ•œ ๋ณ„๋„์˜ ํƒ€์ž…์ด ์—†๋Š” ๋Œ€๋ถ€๋ถ„์˜ ํƒ€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ speakTo์˜ ํƒ€์ž…์€ ์ด๋Ÿด ๊ฒƒ์ด๋‹ค. speakTo :: (String -> String) -> String ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ํƒ€์ž…์˜ speakTo๋Š” ์ ˆ๋Œ€ ํ•จ์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์—†๋‹ค! ํ•จ์ˆ˜๋Š” ๊ฐ™์€ ์ธ์ˆ˜๋ฅผ ๋ฐ›์œผ๋ฉด ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์‚ฐํ•œ๋‹ค. ํ•˜์ง€๋งŒ speakTo๊ฐ€ ๋Œ๋ ค์ฃผ๋Š” String์€ ํ„ฐ๋ฏธ๋„ ํ”„๋กฌํ”„ํŠธ์—์„œ ์ž…๋ ฅํ•œ ๋ฌด์–ธ๊ฐ€์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค. ํ•˜์Šค์ผˆ์—์„œ๋Š” ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ IO String์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ํ•ด๊ฒฐํ•œ๋‹ค. IO String ์ž์ฒด๋Š” String์ด ์•„๋‹ˆ์ง€๋งŒ, ์ž…์ถœ๋ ฅ์„ ๋™๋ฐ˜ํ•˜๋Š” ๋ช…๋ น(์ด ๊ฒฝ์šฐ์—๋Š” ํ„ฐ๋ฏธ๋„์—์„œ ์ž…๋ ฅ์„ ํ•œ ์ค„ ๋ฐ›๋Š” ์ž…์ถœ๋ ฅ)์„ ์ˆ˜ํ–‰ํ•จ์— ๋”ฐ๋ผ ์–ด๋–ค String์ด ์ „๋‹ฌ๋  ๊ฒƒ์ด๋ผ๋Š” ์•ฝ์†์ด๋‹ค. speakTo๊ฐ€ ํ‰๊ฐ€๋  ๋•Œ๋งˆ๋‹ค String ๊ฐ’์€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์ง€๋งŒ ์ž…์ถœ๋ ฅ ๋ช…๋ น์€ ํ•ญ์ƒ ๋™์ผํ•˜๋‹ค. ํ•˜์Šค์ผˆ์ด ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์–ธ์–ด๋ผ๊ณ  ๋งํ•  ๋•Œ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ํƒ€ ์–ธ์–ด์™€ ๋‹ฌ๋ฆฌ ๋ชจ๋“  ํ•จ์ˆ˜๊ฐ€ ์ง„์งœ๋กœ ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. ์ •ํ™•ํžˆ ํ•˜์ž๋ฉด ํ•˜์Šค ์ผˆ ํ‘œํ˜„์‹์€ ํ•ญ์ƒ ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ(referential transparency)์„ ๊ฐ€์ง„๋‹ค. ์ฆ‰ speakTo ๊ฐ™์€ ํ‘œํ˜„์‹์„ ๊ทธ๊ฒƒ์˜ ๊ฐ’, ์ด ๊ฒฝ์šฐ \fSentence -> liftM fSentence getLine์œผ๋กœ ์น˜ํ™˜ํ•ด๋„ ์ ˆ๋Œ€ ํ”„๋กœ๊ทธ๋žจ์˜ ์ž‘๋™์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ฐ˜๋ฉด์— getLine์ด ๋Œ๋ ค์ฃผ๋Š” String ๊ฐ’์€ ๋ถˆํˆฌ๋ช…ํ•˜๋‹ค. ๊ทธ ๊ฐ’์€ ๋ช…์‹œ๋˜์ง€ ์•Š๊ณ  ํ”„๋กœ๊ทธ๋žจ์ด ๋ฏธ๋ฆฌ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์—†๋‹ค. speakTo์˜ ํƒ€์ž…์ด ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๊ทธ ๋ฌธ์ œ ์žˆ๋Š” ํƒ€์ž…์ด๋ผ๋ฉด sayHello๋Š” String ๊ฐ’์ผ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ sayHello๋ฅผ ์–ด๋–ค ํŠน์ • ๋ฌธ์ž์—ด๋กœ ์น˜ํ™˜ํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ๋ง๊ฐ€๋œจ๋ฆฐ๋‹ค. ํ•˜์Šค์ผˆ์ด ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์–ธ์–ด์ง€๋งŒ IO ์•ก์…˜์€ ๋น„์ˆœ์ˆ˜ํ•˜๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” IO ์•ก์…˜์ด ์™ธ๋ถ€์— ๋ผ์น˜๋Š” ์˜ํ–ฅ์ด, (ํ•˜์Šค ์ผˆ ๋‚ด๋ถ€์— ์™„์ „ํžˆ ํฌํ•จ๋˜๋Š” ์ •๊ทœ ํšจ๊ณผ์™€ ๋ฐ˜๋Œ€๋กœ) ๋ถ€์ˆ˜ ํšจ๊ณผ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ˆœ์ˆ˜์„ฑ์ด๋ผ๋Š” ๊ฐœ๋…์ด ์—†๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋“ค์—์„œ๋Š” ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์— ๊ด€๋ จ๋œ ์ˆ˜๋งŽ์€ ์œ„์น˜์—์„œ ๋ถ€์ˆ˜ ํšจ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋ฐ˜๋ฉด ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์–ธ์–ด๋Š” ๋น„์ˆœ์ˆ˜ ๊ฐ’์„ ํฌํ•จํ•œ ํ‘œํ˜„์‹์กฐ์ฐจ ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์„ ๋ณด์žฅํ•œ๋‹ค. ์ด๊ฒƒ์ด ๋œปํ•˜๋Š” ๋ฐ”๋Š” ๋น„์ˆœ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•˜๊ณ  ์ถ”๋ก ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ๋ฐฉ์‹์œผ๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. functor๋‚˜ ๋ชจ๋‚˜๋“œ ๊ฐ™์€ ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์žฅ์น˜๋ฅผ ํ†ตํ•ด์„œ ๋ง์ด๋‹ค. IO ์•ก์…˜์€ ๋น„์ˆœ์ˆ˜ํ•˜์ง€๋งŒ ๊ทธ ์•ก์…˜์„ ์กฐ์ž‘ํ•˜๋Š” ๋ชจ๋“  ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋Š” ์ˆœ์ˆ˜ํ•˜๋‹ค. ํ•จ์ˆ˜ํ˜•์˜ ์ˆœ์ˆ˜ํ•จ์„ ์ž…์ถœ๋ ฅ ํƒ€์ž…๊ณผ ๊ฒฐํ•ฉํ•˜๋ฉด ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์—๊ฒŒ ์—ฌ๋Ÿฌ๋ชจ๋กœ ์ด์ต์ด ๋œ๋‹ค. ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์ด ๋ณด์žฅ๋˜๊ธฐ์— ์ปดํŒŒ์ผ๋Ÿฌ ์ตœ์ ํ™”์˜ ์—ฌ์ง€๊ฐ€ ๋งŽ์•„์ง„๋‹ค. ํƒ€์ž…๋งŒ์œผ๋กœ๋Š” IO ๊ฐ’๋“ค์„ ๋ถ„๊ฐ„ํ•  ์ˆ˜ ์—†์–ด์„œ ์–ด๋””๊ฐ€ ๋ถ€์ˆ˜ ํšจ๊ณผ๋‚˜ ๋ถˆํˆฌ๋ช…ํ•œ ๊ฐ’์ธ์ง€ ํ•œ๋ˆˆ์— ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค. IO ์ž์ฒด๊ฐ€ ๋˜ ๋‹ค๋ฅธ functor ์ผ๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์ธก ๊ฐ€๋Šฅํ•œ ์˜์—ญ์„ ์ตœ๋Œ€๋กœ ์œ ์ง€ํ•˜๊ณ  ์ˆœ์ˆ˜ ํ•จ์ˆ˜์™€ ์—ฐ ๊ฒŒ๋˜๋Š” ์ถ”๋ก ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ ๋‹ค. ํ•จ์ˆ˜ํ˜•๊ณผ ๋ช…๋ นํ˜• ๋ชจ๋‚˜๋“œ๋ฅผ ๋„์ž…ํ•  ๋•Œ ๋ชจ๋‚˜ ๋”• ํ‘œํ˜„์‹์„ ๋ช…๋ นํ˜• ์–ธ์–ด์˜ ๋ช…๋ น๋ฌธ์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋งํ•œ ๋ฐ” ์žˆ๋‹ค. ์ด๋Ÿฐ ํ•ด์„์€ IO์˜ ๊ฒฝ์šฐ ๋ฐ”๋กœ ์™€๋‹ฟ๋Š”๋ฐ, IO ์•ก์…˜ ๊ด€๋ จ ์ฝ”๋“œ๋Š” ํก์‚ฌ ์ „ํ˜•์ ์ธ ๋ช…๋ นํ˜• ์–ธ์–ด์ฒ˜๋Ÿผ ๋ณด์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ๋‹จ์ง€ ํ•ด์„์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฑธ ๋ช…ํ™•ํžˆ ํ•˜์ž. ๋ชจ๋‚˜๋“œ๋‚˜ do ํ‘œ๊ธฐ๋ฒ•์ด ํ•˜์Šค์ผˆ์„ ๋ช…๋ นํ˜• ์–ธ์–ด๋กœ ๋ฐ”๊พผ๋‹ค๋Š” ๋ง์€ ์•„๋‹ˆ๋‹ค. ํ•ต์‹ฌ์€ ๋ชจ๋‚˜ ๋”• ์ฝ”๋“œ๋ฅผ ๋ช…๋ นํ˜• ๊ตฌ๋ฌธ์œผ๋กœ ๋ณด๊ณ  ๊ทธ๋ ‡๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ ์˜๋ฏธ semantic๋Š” ๋ช…๋ นํ˜•์ด์ง€๋งŒ ๋ชจ๋‚˜๋“œ์™€ (>>=)์˜ ๊ตฌํ˜„์€ ์—ฌ์ „ํžˆ ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜•์ด๋‹ค. ์งง์€ ์˜ˆ์‹œ๋กœ ์ด ๊ตฌ๋ณ„์„ ๋ช…ํ™•ํžˆ ํ•˜์ž. int x; scanf("%d", &x); printf("%d\n", x); ์ „ํ˜•์ ์ธ ๋ช…๋ นํ˜• ์–ธ์–ด์ธ C์˜ ์ฝ”๋“œ๋‹ค. ๋ณ€์ˆ˜ x๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ทธ ๊ฐ’์„ scanf๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›๊ณ , printf๋กœ ๊ทธ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. IO do ๋ธ”๋ก ์•ˆ์—์„œ ๊ฐ™์€ ์ผ์„ ํ•˜๋Š” ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋ฅผ ํฝ ๋น„์Šทํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. x <- readLn print x ์˜๋ฏธ์ƒ์œผ๋กœ๋Š” ๋‘ ์ฝ”๋“œ๋Š” ๊ฑฐ์˜ ๋™๋“ฑํ•˜๋‹ค. 3 ํ•˜์ง€๋งŒ C ์ฝ”๋“œ์—์„œ ๋ช…๋ น๋ฌธ๋“ค์€ ํ”„๋กœ๊ทธ๋žจ์ด ์ˆ˜ํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด instruction ๋“ค๊ณผ ์ง์ ‘์ ์œผ๋กœ ๋Œ€์‘ํ•œ๋‹ค. ๋ฐ˜๋ฉด ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋Š” ํŽธ์˜ ๋ฌธ๋ฒ•์„ ๋–ผ์–ด๋‚ด๊ณ  ๋‚˜๋ฉด readLn >>= \x -> print x ๋ช…๋ น๋ฌธ์ด ์—†๋‹ค. ์˜ค์ง ์ ์šฉ๋˜๋Š” ํ•จ์ˆ˜๋“ค๋งŒ์ด ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ์ˆœ์„œ๋Š” ๋ฐ์ดํ„ฐ ์˜์กด์„ฑ์„ ๋”ฐ๋ผ ๊ฐ„์ ‘์ ์œผ๋กœ ์ง€์‹œ๋œ๋‹ค. ๋ชจ๋‚˜ ๋”• ๊ณ„์‚ฐ์„ (>>=)๋กœ ์—ฐ์‡„ํ•  ๋•Œ๋Š” ์•ž์„œ์˜ ๊ฒฐ๊ณผ์— ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ๋‚˜์ค‘์˜ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”๋‹ค. ๋†€๋ผ์›Œ๋ผ. print x๋ฅผ ํ‰๊ฐ€ํ•˜๋ฉด ๋ฌธ์ž์—ด์ด ํ„ฐ๋ฏธ๋„์— ์ถœ๋ ฅ๋œ๋‹ค. ๋ชจ๋‚˜๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ•˜์Šค์ผˆ์€ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์ด์ ์„ ์‚ด๋ฆฌ๋ฉด์„œ ๋ช…๋ นํ˜•์˜ ์˜๋ฏธ semantic๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ์ž…์ถœ๋ ฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ง€๊ธˆ๊นŒ์ง€๋Š” putStrLn๊ณผ getLine ๊ทธ๋ฆฌ๊ณ  ์ด๊ฒƒ๋“ค์„ ์กฐ๊ธˆ ๋ณ€ํ˜•ํ•œ ์›์‹œ์ ์ธ ์ž…์ถœ๋ ฅ๋งŒ์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” IO๋ฅผ ์ˆ˜๋ฐ˜ํ•˜๋Š” ๋งŽ์€ ํ•จ์ˆ˜์™€ ์•ก์…˜์„ ์ œ๊ณตํ•œ๋‹ค. ๊ทธ์ค‘ ํŒŒ์ผ ์ฝ๊ณ  ์“ฐ๊ธฐ๋ฅผ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ๊ธฐ๋Šฅ์ฒ˜๋Ÿผ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ๋“ค์„ ํ•˜์Šค ์ผˆ ์‹ค์ „์˜ IO ์žฅ์— ํฌํ•จ์‹œ์ผฐ๋‹ค. ๋ชจ๋‚˜ ๋”• ์ œ์–ด ๊ตฌ์กฐ ๋ชจ๋‚˜๋“œ๊ฐ€ ์•ก์…˜์˜ ์ˆœ์ฐจ์  ์‹คํ–‰์„ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์„ ๊ฐ€๋Šฅ์ผ€ ํ•œ๋‹ค๋ฉด, ๋ฃจํ”„ ๊ฐ™์€ ํ”ํ•œ ๋ฐ˜๋ณต ํŒจํ„ด๋„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ด ์ ˆ์—์„œ๋Š” ๋ฐ”๋กœ ๊ทธ๋Ÿฐ ๊ฒƒ์„ ๊ฐ€๋Šฅ์ผ€ ํ•˜๋Š” ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์—ฌ๋Ÿฌ ํ•จ์ˆ˜๋ฅผ ๋ณด์—ฌ์ค„ ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์ œ์‹œํ•˜๋Š” ์˜ˆ์ œ๋“ค์€ IO์— ๋Œ€ํ•œ ๊ฒƒ์ด์ง€๋งŒ ์ด์™€ ๊ด€๋ จ๋œ ๋ฐœ์ƒ์€ ๋ชจ๋“  ๋ชจ๋‚˜๋“œ์— ์ ์šฉ๋จ์„ ๋ช…์‹ฌํ•˜๋ผ. ๋ชจ๋‚˜ ๋”• ๊ฐ’์— ๋งˆ๋ฒ•์˜ ๋ฌด์–ธ๊ฐ€๋Š” ์—†๋‹ค. ๋ชจ๋‚˜ ๋”• ๊ฐ’๋„ ํ•˜์Šค์ผˆ์˜ ๋‹ค๋ฅธ ๊ฐ’๋“ค์ฒ˜๋Ÿผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ์‚ฌ์‹ค์— ๊ธฐ์ดˆํ•ด ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋‹ค์„ฏ ์ค„ ๋ฐ›๋Š” ํ•จ์ˆ˜๋ฅผ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ์–ด๋–จ๊นŒ? fiveGetLines = replicate 5 getLine ํ•˜์ง€๋งŒ ์ด ์ฝ”๋“œ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. GHCi์—์„œ ์‹œ๋„ํ•ด ๋ณด๋ผ! ๋ฌธ์ œ๋Š” replicate๊ฐ€ ์•ก์…˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์‚ฐํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•˜๋‚˜์˜ ์•ก์…˜(์ฆ‰ [IO String]์ด ์•„๋‹Œ IO [String])์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ์—๊ฒ ์•ก์…˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•˜์—ฌ ๊ทธ ์•ก์…˜๋“ค์„ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๊ฐ’๋“ค์„ ๋‹จ์ผ ๋ฆฌ์ŠคํŠธ๋กœ ๊ฒฐํ•ฉํ•˜๋Š” fold๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์šฐ์—ฐํžˆ๋„ ๊ทธ๋Ÿฐ ์ผ์„ ํ•˜๋Š” sequence๋ผ๋Š” Prelude ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. sequence :: (Monad m) => [m a] -> m [a] ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค. fiveGetLines = sequence $ replicate 5 getLine replicate์™€ sequence๋Š” ๋งค๋ ฅ์ ์ธ ์กฐํ•ฉ์„ ํ˜•์„ฑํ•œ๋‹ค. ๊ทธ๋ž˜์„œ Control.Monad๋Š” ์•ก์…˜์„ ์ž„์˜ ํšŸ์ˆ˜ ๋ฐ˜๋ณตํ•˜๋Š” replicateM ์ด๋ž€ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. Control.Monad๋Š” ๋ชจ๋‚˜ ๋”• zip, fold ๋“ฑ ๋น„์Šทํ•œ ๊ฐœ๋…์˜ ๋งŽ์€ ํŽธ์˜ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. fiveGetLinesAlt = replicateM 5 getLine ํŠนํžˆ ์ค‘์š”ํ•œ ์กฐํ•ฉ์€ map๊ณผ sequence๋‹ค. ์ด ๋‘˜์„ ํ•ฉํ•˜๋ฉด ๊ฐ’์˜ ๋ฆฌ์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ์•ก์…˜๋“ค์„ ๋งŒ๋“ค๊ณ , ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋“ค์„ ์ˆ˜์ง‘ํ•  ์ˆ˜ ์žˆ๋‹ค. Prelude ํ•จ์ˆ˜ mapM์€ ์ด ํŒจํ„ด์„ ํฌ์ฐฉํ•œ ๊ฒƒ์ด๋‹ค. mapM :: (Monad m) => (a -> m b) -> [a] -> m [b] ์œ„ ํ•จ์ˆ˜๋“ค์˜ ์ด๋ฆ„ ๋’ค์— _๋ฅผ ๋ถ™์ธ sequence_, mapM_, replicateM_ ๊ฐ™์€ ๋ณ€์ข…๋“ค์ด ์žˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์•ก์…˜์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์—๋งŒ ๊ด€์‹ฌ ์žˆ์„ ๋•Œ ์ ํ•ฉํ•˜๋‹ค. _์ด ์—†๋Š” ๊ฒƒ๊ณผ ๋น„๊ตํ•ด ๋ณด๋ฉด (>>)์™€ (>>=)์˜ ์ฐจ์ด์™€ ๋น„์Šทํ•˜๋‹ค. ๊ฐ€๋ น mapM_์˜ ํƒ€์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. mapM_ :: (Monad m) => (a -> m b) -> [a] -> m () ๋งˆ์ง€๋ง‰์œผ๋กœ Control.Monad๋Š” forM๊ณผ forM_๋„ ์ œ๊ณตํ•œ๋‹ค. ์ด๊ฒƒ๋“ค์€ mapM๊ณผ mapM_์„ ๋’ค์ง‘์€ ๊ฒƒ์ด๋‹ค. forM_์€ ๋ช…๋ นํ˜• ์–ธ์–ด์˜ for-each ๋ฃจํ”„๋ฅผ ํ•˜์Šค์ผˆ์— ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฐฉ์‹์œผ๋กœ ๋งŒ๋“  ๊ฒƒ์ด๋‹ค. ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ์— ๊ทธ ์ ์ด ๋ช…ํ™•ํžˆ ๋“œ๋Ÿฌ๋‚œ๋‹ค. forM_ :: (Monad m) => [a] -> (a -> m b) -> m () ์—ฐ์Šต๋ฌธ์ œ ๋ฐฉ๊ธˆ ์†Œ๊ฐœํ•œ ๋ชจ๋‚˜ ๋”• ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•ด์„œ ์ž„์˜์˜ ๊ฐ’์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ ์žฅ์˜ ํ† ๋ผ์˜ ์นจ๊ณต ์˜ˆ์ œ๋ฅผ ์ž„์˜ ์ˆ˜์˜ ์„ธ๋Œ€๋กœ ์ผ๋ฐ˜ํ™”ํ•˜๋ผ. Maybe ๋ชจ๋‚˜๋“œ์— ๋Œ€ํ•œ sequence์˜ ๋™์ž‘์„ ์˜ˆ์ƒํ•˜๋ผ. ๋…ธํŠธ ๊ธฐ์ˆ ์  ์šฉ์–ด๋Š” "์›์‹œํ˜• primitive"์œผ๋กœ, "์›์‹œ ์—ฐ์‚ฐ primitive operation"์˜ ๊ทธ ์›์‹œ๋‹ค. โ†ฉ ๋ฌผ๋ก  ๋ชจ๋“  ๊ณ ์ˆ˜์ค€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์— ๋Œ€ํ•ด ๊ฐ™์€ ๋ง์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ์—ฐํžˆ๋„ ํ•˜์Šค์ผˆ์˜ IO ์ž‘์—…์€ C ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์™ธ๋ถ€ ๊ธฐ๋Šฅ ์ธํ„ฐํŽ˜์ด์Šค(Foreign Function Interface; FFI)๋ฅผ ํ†ตํ•ด ์‹ค์ œ๋กœ ํ™•์žฅ ๊ฐ€๋Šฅํ•˜๋‹ค. C์—์„œ๋Š” ์ธ๋ผ์ธ ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ๋ฅผ ์“ธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜์Šค์ผˆ์€ ์ปดํ“จํ„ฐ๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ์— ๊ฐ„์ ‘์ ์œผ๋กœ ๊ด€์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ์…ˆ์ด๋‹ค. ์ด๋•Œ๋„ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋Š” ๊ทธ๋Ÿฐ ์™ธ๋ถ€ ์ž‘์—…์„ IO functor ๋‚ด์˜ ๊ฐ’๋“ค๋กœ์„œ ๊ฐ„์ ‘ ์กฐ์ž‘ํ•œ๋‹ค. โ†ฉ ํ•œ ๊ฐ€์ง€ ์ฐจ์ด์ ์€ C์—์„œ๋Š” x๊ฐ€ ์ˆ˜์ • ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜์ด๋ฉฐ ๋”ฐ๋ผ์„œ ํ•œ ๊ตฌ๋ฌธ์—์„œ x๋ฅผ ์„ ์–ธํ•˜๊ณ  ๋‹ค์Œ ๊ตฌ๋ฌธ์—์„œ ๊ทธ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์Šค์ผˆ์€ ๊ทธ๋Ÿฐ ๋ณ€๊ฒฝ์„ ์ ˆ๋Œ€ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. C ์ฝ”๋“œ๋ฅผ ๋” ์œ ์‚ฌํ•˜๊ฒŒ ๋ชจ๋ฐฉํ•˜๋ ค๋ฉด IORef๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. IORef๋Š” ํŒŒ๊ดด์  ๊ฐฑ์‹  destructive update์ด ๊ฐ€๋Šฅํ•œ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ๋ฐฉ cell์ด๋‹ค. ๋‹น์—ฐํžˆ IORef๋Š” IO ๋ชจ๋‚˜๋“œ ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. โ†ฉ 5 State ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Understanding_monads/State ์˜์‚ฌ ๋‚œ์ˆ˜ ํ•˜์Šค์ผˆ์—์„œ์˜ ๊ตฌํ˜„ ์˜ˆ์ œ: ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ IO ๋ชจ๋‚˜๋“œ ์ œ๊ฑฐํ•˜๊ธฐ IO ์—†๋Š” ์ฃผ์‚ฌ์œ„ State์˜ ๋„์ž… newtype ์ƒํƒœ ๋ชจ๋‚˜๋“œ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ธฐ State ์„ค์ •ํ•˜๊ณ  ์ ‘๊ทผํ•˜๊ธฐ ๊ฐ’๊ณผ ์ƒํƒœ์— ์ ‘๊ทผํ•˜๊ธฐ ์ฃผ์‚ฌ์œ„์™€ ์ƒํƒœ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ์˜์‚ฌ ๋‚œ์ˆ˜ ๊ฐ’๋“ค ๋…ธํŠธ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•œ ์ ์ด ์žˆ๋‹ค๋ฉด "์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋Š”" ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ดค์„ ๊ฒƒ์ด๋‹ค. ์ด ๊ฐœ๋…์„ ์ ‘ํ•ด ๋ณธ ์ ์ด ์—†๋Š” ์‚ฌ๋žŒ์„ ์œ„ํ•ด ์„ค๋ช…ํ•˜์ž๋ฉด, ์ƒํƒœ๋Š” ํ•จ์ˆ˜์˜ ์ธ์ž๋Š” ์•„๋‹ˆ์ง€๋งŒ ์–ด๋–ค ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ํ•„์š”ํ•œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ณ€์ˆ˜๋ฅผ ์นญํ•œ๋‹ค. C++ ๊ฐ™์€ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด๋Š” ์ƒํƒœ ๋ณ€์ˆ˜๋ฅผ ๊ฐ์ฒด ์•ˆ์—์„œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์˜ ํ˜•ํƒœ๋กœ ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค. C ๊ฐ™์€ ์ ˆ์ฐจ์  ์–ธ์–ด์—์„œ๋Š” ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ์Šค์ฝ”ํ”„์˜ ๋ฐ”๊นฅ์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํ•˜์Šค์ผˆ์—์„œ๋Š” ์ด๋Ÿฐ ๊ธฐ๋ฒ•์„ ๊ทธ๋Œ€๋กœ ์ ์šฉํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค. ์ƒํƒœ์—๋Š” ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•œ๋ฐ ์ด๋Š” ํ•˜์Šค์ผˆ์˜ ํ•จ์ˆ˜ ์ˆœ์ˆ˜์„ฑ๊ณผ ์ƒ์ถฉํ•œ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ•จ์ˆ˜์—์„œ ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ํŒจํ„ด ๋งค์นญ์„ ์ด์šฉํ•ด ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋” ์ผ๋ฐ˜์ ์ด๊ฑฐ๋‚˜ ํŽธ๋ฆฌํ•œ ํ•ด๋ฒ•์„ ์ฐพ๋Š” ๊ฒƒ์ด ์•Œ๋งž์„ ๋•Œ๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿผ State ๋ชจ๋‚˜๋“œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋„์›€์ด ๋˜๋Š”์ง€, ํ”ํ•œ ์˜ˆ์‹œ์ธ ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ์„ ํ†ตํ•ด ์‚ดํŽด๋ณด์ž. ์˜์‚ฌ ๋‚œ์ˆ˜ ์ง„์งœ ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ๋ณต์žกํ•œ ๊ณผ์ œ๋‹ค. ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋Š” ๋Œ€๊ฐœ ์˜์‚ฌ ๋‚œ์ˆ˜๋ฅผ<NAME>๋‹ค. "์˜์‚ฌ pseudo"๋ผ ๋ถˆ๋ฆฌ๋Š” ์ด์œ ๋Š” ์ง„์งœ ๋ฌด์ž‘์œ„๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ดˆ๊ธฐ ์ƒํƒœ(ํ”ํžˆ ์”จ๊ฐ’seed๋ผ๊ณ  ๋ถ€๋ฆ„)์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์˜์‚ฌ ๋‚œ์ˆ˜๋Š” ๋ฌด์ž‘์œ„์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ์ผ๋ จ์˜ ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์˜์‚ฌ ๋‚œ์ˆ˜๊ฐ€ ์š”์ฒญ๋  ๋•Œ๋งˆ๋‹ค ์ „์—ญ ์ƒํƒœ๊ฐ€ ๊ฐฑ์‹ ๋œ๋‹ค. 1 ์ผ๋ จ์˜ ์˜์‚ฌ ๋‚œ์ˆ˜๋Š” ์ดˆ๊ธฐ ์”จ๊ฐ’๊ณผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์•Œ๊ณ  ์žˆ์œผ๋ฉด ์žฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์Šค์ผˆ์—์„œ์˜ ๊ตฌํ˜„ ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์˜์‚ฌ ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ์‰ฌ์šด ์ผ์ด๋‹ค. ๋ณดํ†ต์€ C๋‚˜ C++์˜ rand()์ฒ˜๋Ÿผ ์˜์‚ฌ ๋‚œ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜ ๋˜๋Š” ๊ตฌํ˜„์— ๋”ฐ๋ผ ์ง„์งœ ๋ฌด์ž‘์œ„์ธ ํ•จ์ˆ˜๊ฐ€ ์žˆ๊ธฐ ๋งˆ๋ จ์ด๋‹ค. ํ•˜์Šค์ผˆ๋„ System.Random ๋ชจ๋“ˆ์— ๋น„์Šทํ•œ ๊ฒƒ์ด ์žˆ๋‹ค. > :m System.Random > :t randomIO randomIO :: Random a => IO a > randomIO -1557093684 ์ด ํ•จ์ˆ˜๋Š” ํ•˜์Šค์ผˆ์˜ ์™ธ๋ถ€์— ์žˆ๊ณ  IO๋ฅผ ํ†ตํ•ด ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋Š”, ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ๋ฅผ ์ฐธ์กฐํ•˜๋ฉฐ, ๋”ฐ๋ผ์„œ randomIO๋ฅผ ํ†ตํ•ด ์–ป์€ ๊ฐ’์€ ๋งค๋ฒˆ ๋‹ฌ๋ผ์ง„๋‹ค. ์˜ˆ์ œ: ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ ๊ธฐํšŒ๋ผ๋Š” ๊ฐœ๋…์ด ํ•„์š”ํ•œ ๊ฒŒ์ž„์„ ํ•˜๋‚˜ ์ฝ”๋”ฉํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ์น˜์ž. ํ˜„์‹ค์—์„  ๋Œ€๊ฐœ ์ฃผ์‚ฌ์œ„๋ฅผ ์“ด๋‹ค. ๊ทธ๋Ÿผ ํ•˜์Šค ์ผˆ๋กœ ์ฃผ์‚ฌ์œ„๋ฅผ ๋˜์ง€๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž. ์˜์‚ฌ ๋‚œ์ˆ˜์˜ ๋ฒ”์œ„๋ฅผ ๋ช…์‹œํ•˜๊ธฐ ์œ„ํ•ด randomR ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค. ์ฃผ์‚ฌ์œ„์˜ ๊ฒฝ์šฐ๋Š” randomR(1, 6)์ด๋‹ค. ๊ตด๋ฆด ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ’์„ ์–ป๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด randomR์˜ IO ๋ฒ„์ „์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. import Control.Monad import System.Random rollDiceIO :: IO (Int, Int) rollDiceIO = liftM2 (,) (randomRIO (1,6)) (randomRIO (1,6)) ์ด ํ•จ์ˆ˜๋Š” ์ฃผ์‚ฌ์œ„๋ฅผ ๋‘ ๊ฐœ ๊ตด๋ฆฐ๋‹ค. ์—ฌ๊ธฐ์„œ liftM2๋Š” ๋น„ ๋ชจ๋‚˜ ๋”• ์ด์ธ์ž ํ•จ์ˆ˜ (,)๋ฅผ ๋ชจ๋‚˜๋“œ์—์„œ ์ž‘๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜์—ˆ๋‹ค. (,)๋Š” ํŠœํ”Œ ์ƒ์„ฑ์ž์˜ ๋น„์ „์น˜ ๋ฒ„์ „์ด๋‹ค. ์ฃผ์‚ฌ์œ„ ๋‘ ๊ฐœ๋Š” IO ๋‚ด์˜ ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๊ตด๋ฆด ์ฃผ์‚ฌ์œ„์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜๋ฅผ ๋ฐ›์•„ 1์—์„œ 6 ์‚ฌ์ด์˜ ์˜์‚ฌ ์ •์ˆ˜๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ rollNDiceIO :: Int -> IO [Int]๋ฅผ ์ž‘์„ฑํ•˜๋ผ. IO ๋ชจ๋‚˜๋“œ ์ œ๊ฑฐํ•˜๊ธฐ randomIO์˜ ๋‹จ์ ์€ IO ๋ชจ๋‚˜๋“œ๋ฅผ ์จ์„œ ๋ฌด์Šจ ์ผ์ด ๋ฒŒ์–ด์งˆ์ง€ ์•Œ ์ˆ˜ ์—†๋Š” ํ”„๋กœ๊ทธ๋žจ ์™ธ๋ถ€์— ์ƒํƒœ๋ฅผ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. IO๋Š” ์™ธ๋ถ€ ์„ธ๊ณ„์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ํƒ€๋‹นํ•œ ์ด์œ ๊ฐ€ ์žˆ์„ ๋•Œ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. IO ๋ชจ๋‚˜๋“œ์˜ ๋Œ€์•ˆ์œผ๋กœ ๋กœ์ปฌ ์ƒ์„ฑ๊ธฐ๊ฐ€ ์žˆ๋‹ค. System.Random ๋ชจ๋“ˆ์— ์žˆ๋Š” random๊ณผ mkStdGen ํ•จ์ˆ˜๋กœ ์ƒ์„ฑํ•œ ํŠœํ”Œ์—๋Š” ์˜์‚ฌ ๋‚œ์ˆ˜ ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ์— ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์“ธ ์ƒˆ๋กœ์šด ์ƒ์„ฑ๊ธฐ๊ฐ€ ํ•จ๊ป˜ ๋“ค์–ด์žˆ๋‹ค. > :m System.Random > let generator = mkStdGen 0 -- "0" is our seed > generator 1 1 > random generator :: (Int, StdGen) (2092838931,1601120196 1655838864) IO ๋ชจ๋‚˜๋“œ๋Š” ํ”ผํ–ˆ์ง€๋งŒ ์ƒˆ๋กœ์šด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋‹ค. ๋ฌด์—‡๋ณด๋‹ค๋„ ์ƒ์„ฑ๊ธฐ๋กœ ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋ฉด, ์ด ๋ช…๋ฐฑํ•œ ์ •์˜๋Š” > let randInt = fst . random $ generator :: Int > randInt 2092838931 ์ด๊ฒƒ์€ ํ•ญ์ƒ 2092838931์ด๋ผ๋Š” ๋™์ผํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ•ญ์ƒ ๊ฐ™์€ ์ƒ์„ฑ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด random์„ ์ƒˆ๋กœ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ํŠœํ”Œ์˜ ๋‘ ๋ฒˆ์งธ ๋ฉค๋ฒ„ ์ฆ‰ ์ƒˆ๋กœ์šด ์ƒ์„ฑ๊ธฐ๋ฅผ ์ทจํ•ด ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค. > let (randInt, generator') = random generator :: (Int, StdGen) > randInt -- Same value 2092838931 > random generator' :: (Int, StdGen) -- Using new generator' returned from โ€œrandom generatorโ€ (-2143208520,439883729 1872071452) ๋ฌผ๋ก  ์ด๊ฒƒ์€ ๋‚œ์žกํ•˜๊ณ  ์ง€๋ฃจํ•˜๋‹ค. ์ƒˆ๋กœ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ ํ•จ์ˆ˜๋ฅผ ๊ณ„์† ๋งŒ๋“ค์–ด์•ผ ํ•˜๊ณ  ์ƒ์„ฑ๊ธฐ๋ฅผ ์‹ ์ค‘ํžˆ ์ „๋‹ฌํ•˜๋Š๋ผ ์•ผ๋‹จ๋ฒ•์„์„ ๋–จ์–ด์•ผ ํ•ด์„œ ๊ณจ์น˜๊ฐ€ ์•„ํ”„๋‹ค. IO ์—†๋Š” ์ฃผ์‚ฌ์œ„ ์•ž์˜ ์ฃผ์‚ฌ์œ„๋ฅผ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋˜์งˆ ์ˆ˜ ์žˆ๋‹ค. > randomR (1,6) (mkStdGen 0) (6, 40014 40692) ์ด ํŠœํ”Œ์€ ์ฃผ์‚ฌ์œ„ ํ•˜๋‚˜๋ฅผ ๋˜์ง„ ๊ฒฐ๊ณผ์™€ ์ƒˆ ์ƒ์„ฑ๊ธฐ๋ฅผ ํฌํ•จํ•œ๋‹ค. ์ฃผ์‚ฌ์œ„ ๋‘ ๊ฐœ ๊ตด๋ฆฌ๊ธฐ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. clumsyRollDice :: (Int, Int) clumsyRollDice = (n, m) where (n, g) = randomR (1,6) (mkStdGen 0) (m, _) = randomR (1,6) g ์—ฐ์Šต๋ฌธ์ œ ์ƒ์„ฑ๊ธฐ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ฒซ ๋ฒˆ์งธ ์›์†Œ๊ฐ€ ๋‚œ์ˆ˜๊ณ  ๋‘ ๋ฒˆ์งธ๊ฐ€ ์ตœ๊ทผ ์ƒ์„ฑ๊ธฐ์ธ ํŠœํ”Œ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ rollDice :: StdGen -> ((Int, Int), StdGen)๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. clumsyRollDice์˜ ๊ตฌํ˜„์€ ํ•œ ๋ฐฉ์— ์ž‘๋™ํ•˜์ง€๋งŒ, ํ•œ where ์ ˆ์—์„œ ๋‹ค๋ฅธ ์ ˆ๋กœ ์ƒ์„ฑ๊ธฐ g๋ฅผ ์šฐ๋ฆฌ ์†์œผ๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค. ์ด ์ ‘๊ทผ๋ฒ•์€ ์˜์‚ฌ ๋‚œ์ˆ˜๋ฅผ ๋‹ค๋Ÿ‰์œผ๋กœ ์ƒ์„ฑํ•  ๋•Œ ์ •๋ง๋กœ ์„ฑ๊ฐ€์‹ค ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ ์˜ค๋ฅ˜์— ์ทจ์•ฝํ•˜๋‹ค. ์˜ค๋ฅ˜์— ์ทจ์•ฝํ•œ ์ค‘๊ฐ„ ์ƒ์„ฑ๊ธฐ๋ฅผ where ์ ˆ์˜ ์ž˜๋ชป๋œ ์ค„์— ์ „๋‹ฌํ•ด๋ฒ„๋ฆฐ๋‹ค๋ฉด? ์šฐ๋ฆฌ์—๊ฒŒ ์ •๋ง๋กœ ํ•„์š”ํ•œ ๊ฒƒ์€ ํŠœํ”Œ์˜ ๋‘ ๋ฒˆ์งธ ๋ฉค๋ฒ„ ์ฆ‰ ์ƒˆ๋กœ์šด ์ƒ์„ฑ๊ธฐ๋ฅผ ์ž๋™์œผ๋กœ ์ถ”์ถœํ•ด random์„ ์ƒˆ๋กœ ํ˜ธ์ถœํ•  ๋•Œ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด๊ณณ์—์„œ State ๋ชจ๋‚˜๋“œ๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค. State์˜ ๋„์ž… ์ž ๊น ์ด ์žฅ์—์„œ๋Š” transformers ํŒจํ‚ค์ง€์˜ Control.Monad.Trans.State ๋ชจ๋“ˆ์ด ์ œ๊ณตํ•˜๋Š” ์ƒํƒœ ๋ชจ๋‚˜๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. ์‹ค์ „์—์„œ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋ฅผ ์ฝ๋‹ค ๋ณด๋ฉด mtl ํŒจํ‚ค์ง€์™€ ๋ฐ€์ ‘ํ•œ ๊ด€๋ จ์ด ์žˆ๋Š” Control.Monad.State ๋ชจ๋“ˆ์„ ๋ณด๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ๋‘ ๋ชจ๋“ˆ์˜ ์ฐจ์ด์ ์€ ์ง€๊ธˆ์€ ๊ณ ๋ คํ•  ๋ฐ”๊ฐ€ ์•„๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋…ผ์˜ํ•˜๋Š” ๋ชจ๋“  ๊ฒƒ์€ mtl ๋ณ€์ข…์—๋„ ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์Šค์ผˆ์˜ State ํƒ€์ž…์€ ์ƒํƒœ๋ฅผ ์ทจํ•ด ๊ฒฐ๊ด๊ฐ’๊ณผ ๊ทธ ๊ฒฐ๊ด๊ฐ’์ด ์ถ”์ถœ๋œ ํ›„์˜ ์ƒˆ๋กœ์šด ์ƒํƒœ๋ฅผ ํฌํ•จํ•˜๋Š” ํŠœํ”Œ์„ ์ƒ์„ฑํ•œ๋‹ค. ์ง€๊ธˆ์€ ๊ทธ ์ •์˜๊ฐ€ ๋‹ค์Œ๊ณผ ๋™๋“ฑํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ž. 2 newtype State s a = State { runState :: s -> (a, s) } ์—ฌ๊ธฐ์„œ s๋Š” ์ƒํƒœ์˜ ํƒ€์ž…, a๋Š” ์ƒ์‚ฐ๋œ ๊ฒฐ๊ด๊ฐ’์˜ ํƒ€์ž…์ด๋‹ค. ์œ„์˜ ํƒ€์ž…์ด State ํƒ€์ž…์ด๋ผ๊ณ  ํ™•์–ธํ•  ์ˆ˜๋Š” ์—†๋Š” ๊ฒƒ์ด, ๊ทธ ์•ˆ์˜ ๊ฐ์‹ธ์ง„ ๊ฐ’์ด ์ƒํƒœ ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ์ƒํƒœ ์ฒ˜๋ฆฌ๊ธฐ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. newtype ์œ„์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ผ๋ฐ˜์ ์ธ data ํ‚ค์›Œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ newtype ํ‚ค์›Œ๋“œ๋กœ ์ •์˜ํ•œ ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. newtype์€ ์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜๊ณ  ํ•„๋“œ๋„ ํ•˜๋‚˜์ธ ํƒ€์ž…์—๋งŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค. newtype์€ ์ž๋ช…ํ•œ ๋‹จ์ผ ํ•„๋“œ์˜ ๋ž˜ํ•‘ ์™€ ์–ธ๋ž˜ํ•‘์ด ์ปดํŒŒ์ผ๋Ÿฌ์— ์˜ํ•ด ์ œ๊ฑฐ๋จ์„ ๋ณด์žฅํ•œ๋‹ค. ์ด ๋•Œ๋ฌธ์— State ๊ฐ™์€ ๋‹จ์ˆœํ•œ ๋ž˜ํผ๋Š” newtype์œผ๋กœ ์ •์˜๋˜๊ณค ํ•œ๋‹ค. ๊ทธ๋Ÿผ type์œผ๋กœ ๋ณ„์นญ์„ ์ •์˜ํ•ด๋„ ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์„๊นŒ? type์€ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋Œ€ํ•œ ์ธ์Šคํ„ด์Šค ์ •์˜๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿด ์ˆ˜ ์—†๋‹ค. ์ƒํƒœ ๋ชจ๋‚˜๋“œ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ๋ชจ๋‚˜๋“œ๋“ค๊ณผ ๋‹ฌ๋ฆฌ State๋Š” ๋‘ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„๋‹ค. Monad๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด State๋ฅผ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๊ฒฐํ•ฉํ•ด์•ผ ํ•œ๋‹ค. instance Monad (State s) where ์ด์— ๋”ฐ๋ผ State String, State Int, State SomeLargeDataStructure ๋“ฑ ์ˆ˜๋งŽ์€ State ๋ชจ๋‚˜๋“œ๊ฐ€ ์กด์žฌํ•˜๊ฒŒ ๋œ๋‹ค. return ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„๋œ๋‹ค. return :: a -> State s a return x = state ( \ st -> (x, st) ) ํ’€์–ด์“ฐ๋ฉด, return์— ์ „๋‹ฌํ•œ ๊ฐ’์€ State ์ƒ์„ฑ์ž์— ๊ฐ์‹ธ์ง„ ํ•จ์ˆ˜๋ฅผ ์ƒ์‚ฐํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ƒํƒœ ๊ฐ’์„ ์ทจํ•ด, ๊ทธ๋Œ€๋กœ ๊ฒฐ๊ด๊ฐ’๊ณผ ํ•จ๊ป˜ ํŠœํ”Œ์˜ ๋‘ ๋ฒˆ์งธ ๋ฉค๋ฒ„๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฐ”์ธ๋”ฉ์€ ์กฐ๊ธˆ ๋ณต์žกํ•˜๋‹ค. (>>=) :: State s a -> (a -> State s b) -> State s b processor >>= processorGenerator = state $ \ st -> let (x, st') = runState processor st in runState (processorGenerator x) st' (>>=)๋Š” ์ƒํƒœ ์ฒ˜๋ฆฌ๊ธฐ์™€ ํ•œ ํ•จ์ˆ˜๋ฅผ ์ทจํ•˜๋Š”๋ฐ ์ด ํ•จ์ˆ˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ฒ˜๋ฆฌ๊ธฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ด์šฉํ•ด ๋‹ค๋ฅธ ์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ๊ฒฐํ•ฉํ•˜๋ฉด ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ์ทจํ•ด ๋‘ ๋ฒˆ์งธ ๊ฒฐ๊ณผ์™€ ์ƒํƒœ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์–ป๊ฒŒ ๋œ๋‹ค. ๋‹ค์Œ์˜ ๋„์‹์€ ์ด๋ฅผ ๋„์‹ํ™”ํ•˜์—ฌ ๋ณด์—ฌ์ฃผ๋Š”๋ฐ, ์กฐ๊ธˆ ๋‹ค๋ฅด์ง€๋งŒ ">>=" (bind) ํ•จ์ˆ˜์˜ ๋™๋“ฑํ•œ ํ˜•ํƒœ๋‹ค. wpA์™€ wpAB๋Š” pA์™€ pAB์˜ ๋ž˜ํ•‘ ๋ฒ„์ „์ด๋‹ค. bind๊ฐ€ ์ฃผ์–ด์ง„ ์ƒํƒœ ์ฒ˜๋ฆฌ๊ธฐ pA์™€ ์ƒ์„ฑ๊ธฐ f๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด ์ƒํƒœ ์ฒ˜๋ฆฌ๊ธฐ pAB๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์— ๊ด€ํ•œ ๋„์‹์  ํ‘œํ˜„. s1, s2, s3๋Š” ์‹ค์ œ ์ƒํƒœ๋‹ค. v2์™€ v3๋Š” ๊ฐ’์ด๋‹ค. pA, pB, pAB๋Š” ์ƒํƒœ ์ฒ˜๋ฆฌ๊ธฐ๋‹ค. ์ด ๋„์‹์—์„œ๋Š” State ๋ž˜ํผ ๋‚ด์˜ ํ•จ์ˆ˜ ๋ž˜ํ•‘๊ณผ ์–ธ๋ž˜ํ•‘์„ ์ƒ๋žตํ–ˆ๋‹ค. -- pAB = s1 --> pA --> (v2, s2) --> pB --> (v3, s3) wpA >>= f = wpAB where wpAB = state $ \s1 -> let pA = runState wpA (v2, s2) = pA s1 pB = runState $ f v2 (v3, s3) = pB s2 in (v3, s3) State ์„ค์ •ํ•˜๊ณ  ์ ‘๊ทผํ•˜๊ธฐ ๋ชจ๋‚˜๋“œ ์ธ์Šคํ„ด์Šคํ™”๋Š” ๋‹ค์–‘ํ•œ ์ƒํƒœ ์ฒ˜๋ฆฌ๊ธฐ์˜ ์กฐ์ž‘์„ ๊ฐ€๋Šฅ์ผ€ ํ•˜์ง€๋งŒ ์ด ์‹œ์ ์—์„œ ์˜๋ฌธ์ด ๋“ค ๊ฒƒ์ด๋‹ค. ์ตœ์ดˆ์˜ ์ƒํƒœ๋Š” ์ •ํ™•ํžˆ ์–ด๋””์„œ ์˜ค๋Š” ๊ฑธ๊นŒ? State s๋Š” ๋‘ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋Š” MonadState ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ด๊ธฐ๋„ ํ•˜๋‹ค. put newState = state $ \_ -> ((), newState) ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ด ํ•จ์ˆ˜๋Š” ์ƒํƒœ ์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ฒ˜๋ฆฌ๊ธฐ์˜ ์ž…๋ ฅ์€ ๋ฌด์‹œ๋˜๊ณ  ๊ทธ ๊ฒฐ๊ด๊ฐ’์€ ์šฐ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•œ ์ƒํƒœ๋ฅผ ๋‹ด๋Š” ํŠœํ”Œ์ด๋‹ค. ๊ฒฐ๊ณผ๋Š” ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—(๋ฌด์—‡๋ณด๋‹ค ์šฐ๋ฆฌ๋Š” ์ž…๋ ฅ์„ ๋ฌด์‹œํ–ˆ๋‹ค), ํŠœํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋Š” "null"์ด ๋œ๋‹ค. 3 put์˜ ๋ฐ˜๋Œ€๋˜๋Š” ์ž‘์—…์€ ์ƒํƒœ๋ฅผ ์ฝ์–ด๋“ค์ด๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” get์— ์˜ํ•ด ์ด๋ค„์ง„๋‹ค. get = state $ \st -> (st, st) ๊ทธ ๊ฒฐ๊ณผ ๋‚˜์˜ค๋Š” ์ƒํƒœ ์ฒ˜๋ฆฌ๊ธฐ๋Š” ์ƒ์„ฑ๋œ ํŠœํ”Œ์˜ ์–‘ ์œ„์น˜์— ์ž…๋ ฅ st๋ฅผ ๊ฒฐ๊ณผ๋กœ๋„ ์ƒํƒœ๋กœ๋„ ๋„ฃ์–ด, ๋‹ค๋ฅธ ์ฒ˜๋ฆฌ๊ธฐ์— ๋ฐ”์ธ๋”ฉ ๋  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ ๋‹ค. ๊ฐ’๊ณผ ์ƒํƒœ์— ์ ‘๊ทผํ•˜๊ธฐ State์˜ ์ •์˜๋กœ๋ถ€ํ„ฐ runState๊ฐ€ ์ ‘๊ทผ ์ž์ด๋ฉฐ ๊ทธ ์—ญํ• ์€ State a b ๊ฐ’์— ์ ์šฉํ•˜์—ฌ ์ƒํƒœ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์–ป๊ธฐ ์œ„ํ•จ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ดˆ๊ธฐ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ถ”์ถœ๋œ ๊ฐ’๊ณผ ์ƒˆ๋กœ์šด ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์œ ์‚ฌํ•œ ํ•จ์ˆ˜๋กœ evalState์™€ execState๊ฐ€ ์žˆ๋‹ค. State a b์™€ ์ดˆ๊ธฐ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด evalState ํ•จ์ˆ˜๋Š” ์ถ”์ถœ๋œ ๊ฐ’๋งŒ์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  execState๋Š” ์ƒˆ๋กœ์šด ์ƒํƒœ๋งŒ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. evalState :: State s a -> s -> a evalState processor st = fst ( runState processor st) execState :: State s a -> s -> s execState processor st = snd ( runState processor st) ์ฃผ์‚ฌ์œ„์™€ ์ƒํƒœ ์ฃผ์‚ฌ์œ„ ๋˜์ง€๊ธฐ ์˜ˆ์ œ์— State ๋ชจ๋‚˜๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์ž. "State"์™€ "์ƒํƒœ ์ฒ˜๋ฆฌ๊ธฐ"์˜ ํ˜ผ๋™์„ ๋ง‰๊ธฐ ์œ„ํ•ด ํƒ€์ž… ๋™์˜์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค. import Control.Monad.Trans.State import System.Random type GeneratorState = State StdGen ๋”ฐ๋ผ์„œ, GenerateState๋Š” ๋ณธ์งˆ์ ์œผ๋กœ StdGen -> (Int, StdGen) ํ•จ์ˆ˜์ด๋ฉฐ ์ƒ์„ฑ์ž ์ƒํƒœ์˜ ์ฒ˜๋ฆฌ๊ธฐ๋‹ค. ์ƒ์„ฑ๊ธฐ ์ƒํƒœ ์ž์ฒด๋Š” mkStdGen ํ•จ์ˆ˜์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ง„๋‹ค. GenerateState๋Š” ์–ด๋–ค ํƒ€์ž…์˜ ๊ฐ’์„ ์ถ”์ถœํ•˜๋ ค๋Š”์ง€๋Š” ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉฐ ์˜ค์ง ์ƒํƒœ์˜ ํƒ€์ž…๋งŒ์„ ๊ธฐ์ˆ ํ•˜๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. ์ด์ œ StdGen ์ƒ์„ฑ๊ธฐ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด 1์—์„œ 6 ์‚ฌ์ด์˜ ์ˆซ์ž๋ฅผ ๋‚ด๋†“๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. rollDie :: GeneratorState Int rollDie = do generator <- get let (value, newGenerator) = randomR (1,6) generator put newGenerator return value ํ•œ ๋‹จ๊ณ„์”ฉ ์งš์–ด๋ณด์ž. ์ฒซ ๋ฒˆ์งธ๋กœ <-์™€ get์„ ์ด์šฉํ•ด ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ๋ฅผ ์ค€๋น„ํ•œ๋‹ค. get ์€ ์ƒํƒœ๋ฅผ ์ด์šฉํ•ด ๋ชจ๋‚˜ ๋”• ๊ฐ’('m a' ์•ˆ์˜ 'a')์„ ๋ฎ์–ด์“ฐ๊ณ , ์ƒ์„ฑ๊ธฐ๋ฅผ ๊ทธ ์ƒํƒœ์— ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. (์˜์‹ฌ์Šค๋Ÿฌ์šฐ๋ฉด get๊ณผ >>=์˜ ์ •์˜๋ฅผ ๋– ์˜ฌ๋ ค๋ณด๋ผ) ๊ทธ๋‹ค์Œ randomR ํ•จ์ˆ˜๋กœ 1์—์„œ 6 ์‚ฌ์ด์˜ ์ •์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š”๋ฐ ์ด๋•Œ ์ค€๋น„ํ•ด๋†“์€ ์ƒ์„ฑ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋˜ํ•œ randomR์ด ๋ฐ˜ํ™˜ํ•œ ์ƒˆ ์ƒ์„ฑ๊ธฐ๋ฅผ ์šฐ์•„ํ•˜๊ฒŒ ๋ณด๊ด€ํ•œ๋‹ค. ์ด์ œ put ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ๋ฅผ newGenerator๊ฐ€ ๋˜๋„๋ก ์„ค์ •ํ•˜๋ฉด, ๋‹ค์Œ์—๋Š” ๋‹ค๋ฅธ ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ return์„ ์ด์šฉํ•ด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ GeneratorState ๋ชจ๋‚˜๋“œ๋กœ ์ง‘์–ด๋„ฃ๋Š”๋‹ค. ๋งˆ์นจ๋‚ด ๋ชจ๋‚˜ ๋”• ์ฃผ์‚ฌ์œ„๋ฅผ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. > evalState rollDie (mkStdGen 0) fst $ randomR (1,6) ๋งŒ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์„ ํ•˜๋ ค๊ณ  ์™œ ๋ชจ๋‚˜๋“œ๋ฅผ ๋Œ์–ด๋“ค์—ฌ ์ด๋Ÿฐ ๋ณต์žกํ•œ ํ‹€์„ ๋งŒ๋“ค์—ˆ์„๊นŒ? ๋‹ค์Œ์˜ ํ•จ์ˆ˜๋ฅผ ๋ณด์ž. rollDice :: GeneratorState (Int, Int) rollDice = liftM2 (,) rollDie rollDie ๋‘ ์˜์‚ฌ ๋‚œ์ˆ˜์˜ ํŠœํ”Œ์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ํ•จ์ˆ˜๋ฅผ ์žฅ๋งŒํ–ˆ๋‹ค. ๋‘ ๋‚œ์ˆ˜๊ฐ€ ๋Œ€์ฒด๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. > evalState rollDice (mkStdGen 666) (6,1) ๊ทธ ์ด๋ฉด์—์„œ๋Š” ๋ชจ๋‚˜๋“œ๋“ค์ด ์„œ๋กœ์—๊ฒŒ ์ƒํƒœ๋ฅผ ๋„˜๊ธฐ๊ณ  ์žˆ๋‹ค. ์ „์—๋Š” ์ƒํƒœ๋ฅผ ์ง์ ‘ ์ „๋‹ฌํ•ด์•ผ ํ•ด์„œ randomR(1, 6)์„ ์“ฐ๋Š” ๊ฒƒ์ด ์ƒ๋‹นํžˆ ์ง€์ €๋ถ„ํ–ˆ๋‹ค. ์ด์ œ ๋ชจ๋‚˜๋“œ๊ฐ€ ๊ทธ ์ผ์„ ๋Œ€์‹ ํ•ด์ค€๋‹ค. ์ „์ด ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ•์„ ์•Œ๋ฉด ์˜์‚ฌ ๋‚œ์ˆ˜์˜ ๋ณต์žกํ•œ ์กฐํ•ฉ(ํŠœํ”Œ, ๋ฆฌ์ŠคํŠธ, ๊ทธ ์™ธ ๋ญ๋“  ๊ฐ„์—)์„ ์ƒ์„ฑํ•˜๊ธฐ๊ฐ€ ์ˆœ์‹๊ฐ„์— ์‰ฌ์›Œ์ง„๋‹ค. ์—ฐ์Šต๋ฌธ์ œ rollNDiceIO์—์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๊ฒŒ, ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด 1์—์„œ 6์‚ฌ์ด์˜ ์ •์ˆ˜์ธ ์˜์‚ฌ ๋‚œ์ˆ˜๋ฅผ ๊ทธ ์ •์ˆ˜๋งŒํผ ํฌํ•จํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ rollNDice :: Int -> GeneratorState [Int]๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ์˜์‚ฌ ๋‚œ์ˆ˜ ๊ฐ’๋“ค ์ง€๊ธˆ๊นŒ์ง€๋Š” Int ํƒ€์ž…์˜ ์˜์‚ฌ ๋‚œ ์ˆ˜๋“ค๋งŒ์„ ๊ณ ๋ คํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ GeneratorState ๋ชจ๋‚˜๋“œ๋ฅผ ์ •์˜ํ•  ๋•Œ ์ด๋ฏธ ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ, ์ด ๋ชจ๋‚˜๋“œ๋Š” ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์˜ ํƒ€์ž…์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋ช…์‹œํ•˜์ง€ ์•Š๋Š”๋‹ค. ์‚ฌ์‹ค ์•”๋ฌต์ ์ธ ๊ฐ€์ •์ด ํ•˜๋‚˜ ์žˆ๋‹ค. random ํ˜ธ์ถœ๋กœ๋Š” Int ๋น„์Šทํ•œ ํƒ€์ž…์˜ ๊ฐ’๋“ค๋งŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. Random ํด๋ž˜์Šค๋Š” Int, Char, Integer, Bool, Double, Float ๋“ฑ์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋“ค์˜ ๊ธฐ๋ณธ์ ์ธ ๊ตฌํ˜„์„ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ค‘ ์–ด๋–ค ๊ฒƒ์ด๋“  ๋ฐ”๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. GeneratorState๋Š” ์ด๊ฒƒ์ด ์ƒ์„ฑํ•˜๋Š” ์˜์‚ฌ ๋‚œ์ˆ˜ ๊ฐ’์˜ ํƒ€์ž…์„ ๊ณ ๋ คํ•˜๋ฉด "๋ถˆ๊ฐ€์ง€๋ก  agnostic"์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋ช…์‹œ๋˜์ง€ ์•Š์€ ํƒ€์ž…์˜ ์˜์‚ฌ ๋‚œ์ˆ˜ ๊ฐ’์„ ์ œ๊ณตํ•˜๋Š” ๋น„์Šทํ•œ "๋ถˆ๊ฐ€์ง€๋ก " ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. (๊ทธ ํƒ€์ž…์ด Random์˜ ์ธ์Šคํ„ด์Šค์ธ ํ•œ) getRandom :: Random a => GeneratorState a getRandom = do generator <- get let (value, newGenerator) = random generator put newGenerator return value rollDie์™€ ๋น„๊ตํ•ด ๋ณด๋ฉด ์ด ํ•จ์ˆ˜๋Š” ์‹œ๊ทธ๋„ˆ์ณ์—์„œ Int ํƒ€์ž…์„ ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉฐ randomR ๋Œ€์‹  random์„ ์‚ฌ์šฉํ•œ๋‹ค. ์•„๋‹ˆ๋ฉด ๋˜‘๊ฐ™์€ ๊ฒƒ์ธ getRandom์„ Random์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. > evalState getRandom (mkStdGen 0) :: Bool True > evalState getRandom (mkStdGen 0) :: Char '\64685' > evalState getRandom (mkStdGen 0) :: Double 0.9872770354820595 > evalState getRandom (mkStdGen 0) :: Integer 2092838931 ๊ฒŒ๋‹ค๊ฐ€ ์ด ๋ชจ๋“  ๊ฒƒ์„ ํ•œ ๋ฒˆ์— ๋ถ€๋ฆฌ๊ธฐ๋„ ํฝ ์‰ฌ์›Œ์ง„๋‹ค. allTypes :: GeneratorState (Int, Float, Char, Integer, Double, Bool, Int) allTypes = liftM (,,,,,,) getRandom `ap` getRandom `ap` getRandom `ap` getRandom `ap` getRandom `ap` getRandom `ap` getRandom ์—ฌ๊ธฐ์„œ Control.Monad์— ์ •์˜๋œ ap ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋ฐ–์— ์—†๋Š”๋ฐ, liftM7 ๊ฐ™์€ ๊ฒƒ์€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” liftM5๊นŒ์ง€๋งŒ ์žˆ๋‹ค) ์—ฌ๊ธฐ์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ap๋Š” ๋‹ค์ค‘ ๊ณ„์‚ฐ์„, (๋ฆฌํ”„ํŠธ ๋œ) n ์› ์†Œ ํŠœํ”Œ ์ƒ์„ฑ์ž(์ด ๊ฒฝ์šฐ 7ํ•ญ๋ชฉ์ธ (,,,,,,))์˜ ์ ์šฉ์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ap๋ฅผ ์ข€ ๋” ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๊ทธ ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ์‚ดํŽด๋ณด์ž. ap :: (Monad m) => m (a -> b) -> m a -> m b ํ•˜์Šค์ผˆ์—์„œ a๋ž€ ํƒ€์ž…์€ ๊ฐ’๋ฟ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜๋„ ๋  ์ˆ˜ ์žˆ์Œ์„ ๋– ์˜ฌ๋ฆด ๊ฒƒ. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๋น„๊ตํ•ด ๋ณด๋ฉด >:type liftM (,,,,,,) getRandom liftM (,,,,,) getRandom :: (Random a1) => State StdGen (b -> c -> d -> e -> f -> (a1, b, c, d, e, f)) ๋ชจ๋‚˜๋“œ m์€ ๋ถ„๋ช…ํžˆ State StdGen(์šฐ๋ฆฌ๊ฐ€ GeneratorState๋ผ๊ณ  "๋ณ„์นญ์„ ๋ถ™์ธ")์ด์ง€๋งŒ ap์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ํ•จ์ˆ˜ b -> c -> d -> e -> f -> (a1, b, c, d, e, f)์ด๋‹ค. ap๋ฅผ ์ ์šฉํ•˜๊ณ  ๋˜ ์ ์šฉํ•˜๋ฉด(์—ฌ๊ธฐ์„œ๋Š” 6๋ฒˆ), ๋งˆ์นจ๋‚ด b๊ฐ€ ๋˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ์‹ค์ œ ๊ฐ’(์—ฌ๊ธฐ์„œ๋Š” 7์›์†Œ ํŠœํ”Œ)์ด ๋˜๋Š” ์‹œ์ ์— ๋‹ค๋‹ค๋ฅธ๋‹ค. ์ข…ํ•ฉํ•˜๋ฉด, ap๋Š” ๋ชจ๋‚˜๋“œ ๋‚ด์˜ ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‚˜ ๋”• ๊ฐ’์— ์ ์šฉํ•œ๋‹ค. (๋ชจ๋‚˜๋“œ ์•ˆ์— ์žˆ์ง€ ์•Š์€ ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‚˜ ๋”• ๊ฐ’์— ์ ์šฉํ•˜๋Š” liftM๊ณผ ๋น„๊ตํ•ด ๋ณด๋ผ) ๊ตฌํ˜„์„ ์ดํ•ดํ•œ๋‹ต์‹œ๊ณ  ์ •๋ง ๋งŽ์ด๋„ ํ–ˆ๋‹ค. allTypes ํ•จ์ˆ˜๋Š” Random์˜ ๋ชจ๋“  ๊ธฐ๋ณธ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ์˜์‚ฌ ๋‚œ์ˆ˜ ๊ฐ’๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. ์ƒ์„ฑ๊ธฐ๊ฐ€ ๋™์ผํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ์ž…์ฆํ•˜๊ธฐ ์œ„ํ•ด ๋์— Int๋ฅผ ๋˜ ๋„ฃ์—ˆ๋Š”๋ฐ, ๋‘ Int๋Š” ์„œ๋กœ ๋‹ค๋ฅผ ๊ฒƒ์ด๋‹ค. โ†ฉ ์šฐ๋ฆฌ์˜ ์ ‘๊ทผ๋ฒ•์˜ ๋ฏธ๋ฌ˜ํ•œ ๋ฌธ์ œ๋Š” transformers ํŒจํ‚ค์ง€๊ฐ€ State ํƒ€์ž…์„ ๋‹ค์†Œ ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ ์ฐจ์ด๋Š” State๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ดํ•ดํ•˜๋Š” ๋ฐ ์˜ํ–ฅ์„ ์ฃผ์ง€๋Š” ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ ์ด๋กœ ์ธํ•ด Control.Monad.Trans.State๋Š” State ์ƒ์„ฑ์ž๋ฅผ ๋‚ด๋ณด๋‚ด์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹  ๊ฐ™์€ ์ผ์„ ํ•˜๋Š” state ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ๊ตฌํ˜„์ด ์™œ ์œ„์—์„œ ์ œ์‹œํ•œ ๋ช…๋ฐฑํ•œ ๊ฒƒ์ด ์•„๋‹Œ์ง€์— ๊ด€ํ•ด์„œ๋Š” ๋ช‡ ๊ณผ๋ชฉ์„ ๋„˜๊ธด ํ›„ ๋‹ค์‹œ ๋…ผ์˜ํ•  ๊ฒƒ์ด๋‹ค. state :: (s -> (a, s)) -> State s a โ†ฉ () ํƒ€์ž…์˜ ๊ธฐ์ˆ ์  ์šฉ์–ด๋Š” ๋‹จ์œ„ unit์ด๋‹ค. โ†ฉ 3 Alternative์™€ MonadPlus ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/MonadPlus ์ •์˜ ์˜ˆ์ œ: ๋ณ‘๋ ฌ ํŒŒ์‹ฑ MonadPlus Alternative์™€ MonadPlus ๋ฒ•์น™ ์œ ์šฉํ•œ ํ•จ์ˆ˜๋“ค asum guard ์—ฐ์Šต๋ฌธ์ œ ๋ชจ ๋…ธ์ด๋“œ์™€์˜ ๊ด€๊ณ„ ๊ธฐํƒ€ ๋ฒ•์น™๋“ค ์ง€๊ธˆ๊นŒ์ง€ ์•Œ์•„๋ณธ ๋ฐ”์— ๋”ฐ๋ฅด๋ฉด Maybe ๋ชจ๋‚˜๋“œ์™€ ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ณ„์‚ฐ(computation)์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ณ„์‚ฐ์ด ์–ด๋–ค ์ด์œ ๋กœ๋“  ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ์„ ๋•Œ๋Š” Maybe๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค(๊ฒฐ๊ณผ๊ฐ€ 0๊ฐœ ๋˜๋Š” 1๊ฐœ). ๋งŽ์€ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๋Š” ๊ณ„์‚ฐ์—๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค(๊ฒฐ๊ณผ๊ฐ€ 0๊ฐœ์—์„œ ์ž„์˜์˜ ๋งŽ์€ ์ˆ˜). ๋‘ ๊ฒฝ์šฐ์— ๋ชจ๋‘ ์œ ์šฉํ•œ ์—ฐ์‚ฐ์ด ์žˆ๋Š”๋ฐ, ๋ณต์ˆ˜์˜ ๊ณ„์‚ฐ์— ์˜ํ•ด ์ƒ๊ธด ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š”(concatenate) ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ •์˜ ๋…ธํŠธ: Alternative ํด๋ž˜์Šค์™€ ๊ทธ ๋ฉ”์„œ๋“œ๋“ค์€ Control.Applicative ๋ชจ๋“ˆ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Alternative ํด๋ž˜์Šค๋Š” Applicative์˜ ์„œ๋ธŒ ํด๋ž˜์Šค๋‹ค. Applicative์˜ ์ธ์Šคํ„ด์Šค๋Š” ์ตœ์†Œํ•œ ๋‹ค์Œ์˜ ๋‘ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค. class Applicative f => Alternative f where empty :: f a (<|>) :: f a -> f a -> f a empty๋Š” ๊ฒฐ๊ณผ๊ฐ€ 0๊ฐœ์ธ applicative computation์ด๊ณ  (<|>)๋Š” ๋‘ computation์„ ๊ฒฐํ•ฉํ•˜๋Š” ์ดํ•ญ ํ•จ์ˆ˜๋‹ค. ๋‹ค์Œ์€ Maybe์™€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์ธ์Šคํ„ด์Šค ์ •์˜๋‹ค. instance Alternative Maybe where empty = Nothing -- Note that this could have been written more compactly. Nothing <|> Nothing = Nothing -- 0 results + 0 results = 0 results Just x <|> Nothing = Just x -- 1 result + 0 results = 1 result Nothing <|> Just x = Just x -- 0 results + 1 result = 1 result Just x <|> Just y = Just x -- 1 result + 1 result = 1 result: -- Maybe can only hold up to one result, -- so we discard the second one. instance Alternative [] where empty = [] (<|>) = (++) -- length xs + length ys = length (xs ++ ys) ์˜ˆ์ œ: ๋ณ‘๋ ฌ ํŒŒ์‹ฑ ์ „ํ†ต์ ์ธ ์ž…๋ ฅ ํŒŒ์‹ฑ์—์„œ๋Š” ์ž…๋ ฅ์„ ํ•œ ๊ธ€์ž์”ฉ ์†Œ๋น„ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. ์ฆ‰ ํŒŒ์‹ฑ ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ ๋ฌธ์ž์—ด์˜ ๋จธ๋ฆฌ ๋ถ€๋ถ„์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ธ€์ž๋“ค์„ ๋–ผ์–ด๋‚ธ๋‹ค("์†Œ๋น„ํ•œ๋‹ค"). ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋Œ€๋ฌธ์ž ํ•˜๋‚˜๋ฅผ ์†Œ๋น„ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ž์—ด ์•ž๋ถ€๋ถ„์˜ ๊ธ€์ž๋“ค์ด ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š์œผ๋ฉด ๊ทธ ํŒŒ์„œ๋Š” ์‹คํŒจํ•œ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ์—์„œ๋Š” ์ž…๋ ฅ์—์„œ ์ˆซ์ž ํ•˜๋‚˜๋ฅผ ์†Œ๋น„ํ•˜๊ณ  ํŒŒ์‹ฑ ๋œ ํ•ด๋‹น ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์‹คํŒจ ๊ฐ€๋Šฅ์„ฑ์€ Maybe๋ฅผ ํ†ตํ•ด ํ‘œํ˜„๋œ๋‹ค. digit i (c:_) | i > 9 || i < 0 = Nothing | otherwise = if [c] == show i then Just i else Nothing ๊ฐ€๋“œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ๊ฒ€์‚ฌํ•˜๋ ค๋Š” Int๊ฐ€ ํ•œ ์ž๋ฆฟ์ˆ˜์ž„์„ ๋ณด์žฅํ•œ๋‹ค. ํ•œ์ž๋ฆฌ๊ฐ€ ๋งž์œผ๋ฉด ๋ฌธ์ž์—ด์˜ ์ฒซ ๊ธ€์ž๊ฐ€ ์šฐ๋ฆฌ๊ฐ€ ํ™•์ธํ•˜๋ ค๋Š” ๊ทธ ์ˆซ์ž์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. ๊ฒ€์‚ฌ๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ๊ทธ ์ˆซ์ž๋ฅผ Just๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์‹คํŒจํ•˜๋ฉด Nothing์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (<|>)๋Š” ๋‘ ํŒŒ์„œ๋ฅผ ๋ณ‘๋ ฌ๋กœ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ์ฒซ ๋ฒˆ์งธ ํŒŒ์„œ๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด ๊ทธ๊ฒƒ์˜ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์•„๋‹ˆ๋ฉด ๋‘ ๋ฒˆ์งธ ํŒŒ์„œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋‘˜ ๋‹ค ์‹คํŒจํ•˜๋ฉด ์ด ํ•ฉ์„ฑ ํŒŒ์„œ๋Š” Nothing์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. digit๋ฅผ (<|>)์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์ด์ง„์ˆ˜ ๋ฌธ์ž์—ด์„ ํŒŒ์‹ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค. binChar :: String -> Maybe Int binChar s = digit 0 s <|> digit 1 s ํŒŒ์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ Alternative๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. Text.ParserCombinators.ReadP์˜ (+++)์™€ Text.ParserCombinators.Parsec.Prim์˜ (<|>)๊ฐ€ ๊ทธ๋Ÿฐ ์˜ˆ์‹œ๋‹ค. ์ด๋Ÿฐ ์‚ฌ์šฉ ํŒจํ„ด์€ ์„ ํƒ(choice)์ด๋ผ๋Š” ๊ด€์ ์—์„œ ์„œ์ˆ ํ•  ์ˆ˜ ์žˆ๋‹ค. binChar์— ๋„ฃ์œผ๋ฉด ์„ฑ๊ณต์ ์œผ๋กœ ํŒŒ์‹ฑ ๋  ๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ ๋‘ ๊ฐ€์ง€ ์„ ํƒ์ด ์žˆ๋‹ค. ๊ทธ ๋ฌธ์ž์—ด์€ '0' ๋˜๋Š” '1'์œผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. MonadPlus MonadPlus ํด๋ž˜์Šค๋Š” Alternative์™€ ๋ฐ€์ ‘ํ•œ ์—ฐ๊ด€์ด ์žˆ๋‹ค. class Monad m => MonadPlus m where mzero :: m a mplus :: m a -> m a -> m a ๊ทธ ์ •์˜๋Š” ๋ฉ”์„œ๋“œ ์ด๋ฆ„๋“ค์ด ๋‹ค๋ฅด๊ณ  Applicative ์ œ์•ฝ์ด Monad๋กœ ๋ฐ”๋€ ๊ฒƒ๋งŒ ๋นผ๋ฉด Alternative์™€ ๊ฐ™๋‹ค. Alternative์™€ MonadPlus์˜ ์ธ์Šคํ„ด์Šค๋“ค์„ ๋ชจ๋‘ ๊ฐ€์ง€๋Š” ํƒ€์ž…์˜ ๊ฒฝ์šฐ mzero์™€ mplus๋Š” ๊ฐ๊ฐ empty ๋ฐ (<|>)์™€ ๋™์น˜์—ฌ์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์™œ MonadPlus ๊ฐ™์€ ํด๋ž˜์Šค๊ฐ€ ๊ตณ์ด ํ•„์š”ํ•œ์ง€ ์˜๋ฌธ์ด ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์ด์œ ์˜ ์ผ๋ถ€๋Š” ์—ญ์‚ฌ์ ์ด๋‹ค. ํ•˜์Šค์ผˆ์— Applicative๊ฐ€ ๋„์ž…๋˜๊ธฐ ์˜ค๋ž˜์ „๋ถ€ํ„ฐ Monad๊ฐ€ ์กด์žฌํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ MonadPlus๋Š” Alternative๋ณด๋‹ค ํ›จ์”ฌ ์˜ค๋ž˜๋˜์—ˆ๋‹ค. ๊ทธ ์™ธ์—๋„ MonadPlus ๋ฉ”์„œ๋“œ๋“ค์ด Monad์™€ ์–ด๋–ป๊ฒŒ ์ƒํ˜ธ์ž‘์šฉํ• ์ง€์— ๊ด€ํ•œ ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Œ€๊ฐ’๋“ค์ด ์žˆ๋Š”๋ฐ ๊ทธ๊ฒƒ์ด Alternative์—๋Š” ์ ์šฉ๋˜์ง€ ์•Š์•„์„œ, ๋ฌด์–ธ๊ฐ€๊ฐ€ MonadPlus๋ผ๊ณ  ๋ช…์‹œํ•˜๋Š” ๊ฒƒ์€ ๊ทธ๊ฒƒ์ด Alternative๋ฉด์„œ Monad๋ผ๊ณ  ๋ช…์‹œํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๊ฐ•ํ•œ ์ฃผ์žฅ์ด๋‹ค. ์ด ๋ฌธ์ œ๋Š” ๋‹ค์Œ ์ ˆ์—์„œ ์ข€ ๋” ์•Œ์•„๋ณธ๋‹ค. Alternative์™€ MonadPlus ๋ฒ•์น™ ๋Œ€๋ถ€๋ถ„์˜ ๋ฒ”์šฉ ํด๋ž˜์Šค๋“ค์ฒ˜๋Ÿผ Alternative์™€ MonadPlus๋Š” ์ง€์ผœ์•ผ ํ•˜๋Š” ๋ฒ•์น™๋“ค์ด ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ๋ฒ•์น™๋“ค์˜ ์™„์ „ํ•œ ์ง‘ํ•ฉ์ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”์ง€์— ๊ด€ํ•œ ๋ณดํŽธ์  ๋™์˜๋Š” ์—†๋‹ค. Alternative์—์„œ ๊ฐ€์žฅ ํ”ํ•˜๊ฒŒ ์ฑ„ํƒ๋˜๊ณ  ๋˜ํ•œ ์ง๊ด€์„ ์–ป๋Š”๋ฐ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ฒ•์น™์€, empty์™€ (<|>)๊ฐ€ ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. -- empty is a neutral element empty <|> u = u u <|> empty = u -- (<|>) is associative u <|> (v <|> w) = (u <|> v) <|> w "๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑ" ํ•˜๋Š” ๊ฒƒ์— ํŠน๋ณ„ํ•œ ๊ฒƒ์€ ์—†๋‹ค. ํ•ญ๋“ฑ์›(nuetral element)๊ณผ ๊ฒฐํ•ฉ(associative)์€ ์ •์ˆ˜ ๋ง์…ˆ์ด ๊ฒฐํ•ฉ๋ฒ•์น™์„ ๋งŒ์กฑํ•˜๊ณ  ๊ทธ ํ•ญ๋“ฑ์›์ด 0์ด๋ผ๊ณ  ๋งํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ค๋ฅผ ๋ฐ”๊ฐ€ ์—†๋‹ค. ์‚ฌ์‹ค MonadPlus์˜ ๋ฉ”์„œ๋“œ๋“ค ์ด๋ฆ„์ด mzero์™€ mplus์ธ ๊ฒƒ์€ ์ด๋Ÿฐ ์œ ์‚ฌ์„ฑ ๋•Œ๋ฌธ์ด๋‹ค. MonadPlus์˜ ๊ฒฝ์šฐ์—๋Š” ์ตœ์†Œํ•œ ๋ชจ ๋…ธ์ด๋“œ ๋ฒ•์น™๋“ค์ด ํ•„์ˆ˜์ ์ด๋ฉฐ ์œ„์˜ ๋ฒ•์น™๋“ค์— ์ •ํ™•ํžˆ ๋Œ€์‘ํ•œ๋‹ค. mzero `mplus` m = m m `mplus` mzero = m m `mplus` (n `mplus` o) = (m `mplus` n) `mplus` o ๊ทธ๋ฆฌ๊ณ  ์ถ”๊ฐ€์ ์ธ ๋ฒ•์น™ 2๊ฐœ๊ฐ€ ์žˆ๋‹ค. Control.Monad ๋ฌธ์„œ์—์„œ ์ธ์šฉ: mzero >>= f = mzero -- left zero m >> mzero = mzero -- right zero mzero๋ฅผ ์‹คํŒจํ•œ ๊ณ„์‚ฐ(computation)์œผ๋กœ ํ•ด์„ํ•˜๋ฉด ์ด ๋ฒ•์น™๋“ค์€ monadic computation ์—ฐ์‡„ ์•ˆ์—์„œ ํ•œ ๋ฒˆ์˜ ์‹คํŒจ๊ฐ€ ์—ฐ์‡„ ์ „์ฒด์˜ ์‹คํŒจ๋กœ ์ด์–ด์ง์„ ๋œปํ•œ๋‹ค. Alternative์™€ MonadPlus์— ์ถ”๊ฐ€๋กœ ์ œ์•ˆ๋œ ๋ฒ•์น™๋“ค์€ ์ด ์žฅ์˜ ๋์—์„œ ์‚ดํŽด๋ณธ๋‹ค. ์œ ์šฉํ•œ ํ•จ์ˆ˜๋“ค ๋ฒ ์ด์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” Alternative์— ๊ด€๋ จํ•˜์—ฌ (<|>)์™€ empty ์™ธ์—๋„ ์œ ์šฉํ•œ ๋ฒ”์šฉ ํ•จ์ˆ˜๊ฐ€ 2๊ฐœ ์žˆ๋‹ค. asum Alternative๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋Š” alternative ๊ฐ’๋“ค์˜ ๋ฆฌ์ŠคํŠธ, ๊ฐ€๋ น [Maybe a]๋‚˜ [[a]]๋ฅผ ๋ฐ›์•„์„œ (<|>)๋กœ ๊ฒฐํ•ฉํ•˜๋Š” ์ผ์ด ํ”ํ•˜๋‹ค. Data.Foldable์˜ asum ํ•จ์ˆ˜๊ฐ€ ๊ทธ๋Ÿฐ ์ผ์„ ํ•œ๋‹ค. asum :: (Alternative f, Foldable t) => t (f a) -> f a asum = foldr (<|>) empty ์–ด๋–ป๊ฒŒ ๋ณด๋ฉด asum์€ ๋ฆฌ์ŠคํŠธ์˜ concat ์—ฐ์‚ฐ์„ ์ผ๋ฐ˜ํ™”ํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ ๋‘˜์€ ์™„๋ฒฝํžˆ ๋™์น˜๋‹ค. Maybe์˜ ๊ฒฝ์šฐ asum์€ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ Just x๋ฅผ ์ฐพ๊ณ  ํ•˜๋‚˜๋„ ์—†์œผ๋ฉด Nothing์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. Data.Foldable๊ณผ Control.Monad์— ๋‘˜ ๋‹ค ๋“ค์–ด์žˆ๋Š” msum์€ MonadPlus์— ํŠนํ™”๋œ asum์ด๋‹ค. msum :: (MonadPlus m, Foldable t) => t (m a) -> m a guard ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ๋ฅผ ๋…ผํ•  ๋•Œ ๊ฐ€๋“œ์™€ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์ด ์–ผ๋งˆ๋‚˜ ๋น„์Šทํ•œ์ง€ ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ, ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹ ํ•„ํ„ฐ๋ง์„ ๊ฐ€๋“œ์—์„œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€๋Š” ๋…ผ์˜ํ•˜์ง€ ์•Š์•˜๋‹ค. Control.Monad์˜ guard ํ•จ์ˆ˜๊ฐ€ ๋ฐ”๋กœ ๊ทธ๋Ÿฐ ์ผ์„ ํ•ด์ค€๋‹ค. ๋ชจ๋“  ํ”ผํƒ€๊ณ ๋ผ์Šค ์„ธ ์Œ(์ฆ‰ ์ง๊ฐ์‚ผ๊ฐํ˜•์˜ ๋ณ€๋“ค์˜ ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ ์Œ์˜ ์ •์ˆ˜๋“ค)์„ ๊ตฌํ•˜๋Š” ๋‹ค์Œ์˜ ์กฐ๊ฑด ์ œ์‹œ์‹์„ ์‚ดํŽด๋ณด์ž. ๋จผ์ € ๋ธŒ๋ฃจํŠธ ํฌ์Šค ์ ‘๊ทผ๋ฒ•์„ ๋ณด์ž. ํ•„ํ„ฐ๋ง์„ ์œ„ํ•ด ๋ถˆ๋ฆฌ์–ธ ์กฐ๊ฑด์‹, ์ด๋ฅธ๋ฐ” ํ”ผํƒ€๊ณ ๋ผ์Šค ์ •๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. pythags = [ (x, y, z) | z <- [1..], x <- [1.. z], y <- [x.. z], x^2 + y^2 == z^2 ] ์œ„์˜ ์กฐ๊ฑด ์ œ์‹œ์‹์„ ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ do ๋ธ”๋ก์œผ๋กœ ๋ฒˆ์—ญํ•˜๋ฉด: pythags = do z <- [1..] x <- [1.. z] y <- [x.. z] guard (x^2 + y^2 == z^2) return (x, y, z) guard ํ•จ์ˆ˜๋Š” ๋ชจ๋“  Alternative์— ๋Œ€ํ•ด ์ด๋ ‡๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. guard :: Alternative m => Bool -> m () guard True = pure () guard _ = empty guard๋Š” ์ˆ ์–ด๊ฐ€ False ์ด๋ฉด do ๋ธ”๋ก์„ empty๋กœ ํ™˜์›ํ•œ๋‹ค. left zero ๋ฒ•์น™์— ๋”ฐ๋ฅด๋ฉด mzero >>= f = mzero -- Or, equivalently: empty >>= f = empty >>= ์—ฐ์‚ฐ์ž์˜ ์ขŒ๋ณ€์˜ empty๋Š” ๋‹ค์‹œ empty๋ฅผ ์ƒ์‚ฐํ•œ๋‹ค. do ๋ธ”๋ก์€ (>>=)๋กœ ์—ฐ๊ฒฐ๋œ ํ‘œํ˜„์‹๋“ค๋กœ ๋ถ„ํ•ด๋˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋Š ์‹œ์ ์—๋“  empty๊ฐ€ ์žˆ์œผ๋ฉด do ๋ธ”๋ก ์ „์ฒด๋ฅผ empty๋กœ ๋งŒ๋“ ๋‹ค. pythags ์•ˆ์—์„œ guard๊ฐ€ ํ•˜๋Š” ์ผ์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด์ž. ๋จผ์ € ์ด๊ฒƒ์€ ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ์— ๋Œ€ํ•ด ์ •์˜๋œ guard๋‹ค. -- guard :: Bool -> [()] guard True = [()] guard _ = [] guard๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ธธ์„ ๋ง‰๋Š”๋‹ค. pythags์—์„œ ์šฐ๋ฆฌ๋Š” x^2 + y^2 + z^2๊ฐ€ False์ธ ๋ชจ๋“  ๊ธธ(์ฆ‰ x, y, z์˜ ์กฐํ•ฉ)์„ ๋ง‰์œผ๋ ค ํ•œ๋‹ค. ์œ„์˜ do ๋ธ”๋ก์„ ์ „๊ฐœํ•ด์„œ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž. pythags = [1..] >>= \z -> [1.. z] >>= \x -> [x.. z] >>= \y -> guard (x^2 + y^2 == z^2) >>= \_ -> return (x, y, z) >>=์™€ return์„ ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ์— ๋Œ€ํ•œ ๊ทธ๊ฒƒ๋“ค์˜ ์ •์˜๋กœ ์น˜ํ™˜ํ•˜๋ฉด (๊ทธ๋ฆฌ๊ณ  ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด let ๋ฐ”์ธ๋”ฉ์„ ๊ณ๋“ค์ด๋ฉด) ๋‹ค์Œ์„ ์–ป๋Š”๋‹ค. pythags = let ret x y z = [(x, y, z)] gd z x y = concatMap (\_ -> ret x y z) (guard $ x^2 + y^2 == z^2) doY z x = concatMap (gd z x) [x.. z] doX z = concatMap (doY z) [1.. z] doZ = concatMap (doX) [1..] in doZ guard๋Š” ๊ทธ ์ธ์ž๊ฐ€ False ๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์ž. ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€๋กœ์งˆ๋Ÿฌ ๋งคํ•‘์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋˜ ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋‚˜์˜จ๋‹ค. ๋”ฐ๋ผ์„œ gd ์•ˆ์—์„œ guard๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋‚˜์™”์œผ๋ฉด gd ์ž์ฒด๋„ ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์‚ฐํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ \_ -> ret x y z๋Š” ํ˜ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด๊ฒŒ ์™œ ์ค‘์š”ํ•œ์ง€๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์„ ํŠธ๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ•ด ๋ณด์ž. ์šฐ๋ฆฌ์˜ ํ”ผํƒ€๊ณ ๋ผ์Šค ์„ธ ์Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ฒฝ์šฐ ๋ฃจํŠธ์—์„œ ์‹œ์ž‘ํ•ด ๋ชจ๋“  z์— ๋Œ€ํ•œ ๋ธŒ๋žœ์น˜๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๊ณ , ์ด๋Ÿฐ ๋ธŒ๋žœ์น˜๋งˆ๋‹ค ๋ชจ๋“  x์— ๋Œ€ํ•œ ํ•˜์œ„ ๋ธŒ๋žœ์น˜๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ๋‹ค์‹œ ์ด๋Ÿฐ ๋ธŒ๋žœ์น˜๋งˆ๋‹ค ๋ชจ๋“  y์— ๋Œ€ํ•œ ํ•˜์œ„ ๋ธŒ๋žœ์น˜๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ ํŠธ๋ฆฌ๋Š” ์ด๋Ÿฐ ๋ชจ์–‘์ด๋‹ค. start |_________________________... | | | z 1 2 3 | |____ |____________ | | | | | | x 1 1 2 1 2 3 | |_ | |___ |_ | | | | | | | | | | | y 1 1 2 2 1 2 3 2 3 3 ๊ฐ z, x, y ์กฐํ•ฉ์€ ํŠธ๋ฆฌ๋ฅผ ๊ฒฝ์œ ํ•˜๋Š” ํ•œ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ  ๋‚˜๋ฉด ๋ฐ”๋‹ฅ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋ธŒ๋žœ์น˜๋“ค์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํ•ฉํ•œ๋‹ค. ์ˆ ์–ด๊ฐ€ ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š” ๋ชจ๋“  ๊ฒฝ๋กœ๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ํ‰๊ฐ€๋˜๋ฏ€๋กœ ์ด๋Ÿฌํ•œ ๊ฒฐํ•ฉ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค. ์—ฐ์Šต๋ฌธ์ œ 1 Alternative ๋ชจ ๋…ธ์ด๋“œ ๋ฒ•์น™๋“ค์„ Maybe์™€ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ฆ๋ช…ํ•ด ๋ณด์ž. 2 ๋ณ‘๋ ฌ ํŒŒ์‹ฑ ์˜ˆ์ œ๋ฅผ ๋ณด๊ฐ•ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž„์˜ ๋ฌธ์ž๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. -- | Consume a given character in the input, and return -- the character we just consumed, paired with rest of -- the string. We use a do-block so that if the -- pattern match fails at any point, 'fail' of the -- Maybe monad (i.e. Nothing) is returned. char :: Char -> String -> Maybe (Char, String) char c s = do c' : s' <- return s guard (c == c') return (c, s') ๊ทธ๋Ÿฌ๋ฉด ์ž„์˜์˜ ์˜ฌ๋ฐ”๋ฅธ 16์ง„์ˆ˜ ๋ฌธ์ž(0-9 ๋˜๋Š” a-f)๋ฅผ ํŒŒ์‹ฑ ํ•˜๋Š” hexChar ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. (ํžŒํŠธ: map digit [0.. 9] :: [String -> Maybe Int]) 3 guard์™€ Applicative ๊ฒฐํ•ฉ๊ธฐ ๋“ค(pure, (<*>), (*>) ๋“ฑ)์„ ์‚ฌ์šฉํ•˜์—ฌ Maybe ๋ชจ๋‚˜๋“œ ์žฅ์—์„œ ๋‚˜์˜จ safeLog๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์ž. Monad ๊ฒฐํ•ฉ๊ธฐ ๋“ค(return, (>>=), (>>)์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ชจ ๋…ธ์ด๋“œ์™€์˜ ๊ด€๊ณ„ ์œ„์—์„œ Alternative ๋ฒ•์น™๋“ค์„ ๋…ผํ•˜๋ฉด์„œ ๋ชจ ๋…ธ์ด๋“œ์˜ ์ˆ˜ํ•™์  ๊ฐœ๋…์„ ๋„Œ์ง€์‹œ ์–ธ๊ธ‰ํ–ˆ๋‹ค. ์‚ฌ์‹ค ํ•˜์Šค์ผˆ์—๋Š” Monoid ํด๋ž˜์Šค๊ฐ€ ์ด๋ฏธ ์žˆ๋‹ค. (Data.Monoid์— ์ •์˜๋จ) ๋ชจ ๋…ธ์ด๋“œ์— ๋Œ€ํ•ด์„œ๋Š” ์ดํ›„์˜ ์žฅ์—์„œ ์„ธ์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์ง€๋งŒ, ์ง€๊ธˆ์€ Monoid์˜ ์ตœ์†Œํ•œ์˜ ์ •์˜๊ฐ€ ๋‘ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์„ ์•Œ๋ฉด ์ถฉ๋ถ„ํ•˜๋‹ค. ๋ฐ”๋กœ ํ•ญ๋“ฑ์›(์ฆ‰ 0)๊ณผ ๊ฒฐํ•ฉ๋ฒ•์น™์ด ์„ฑ๋ฆฝํ•˜๋Š” ์ดํ•ญ ์—ฐ์‚ฐ(์ฆ‰ 'plus')์ด๋‹ค. class Monoid m where mempty :: m mappend :: m -> m -> m ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ„๋‹จํ•œ ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. instance Monoid [a] where mempty = [] mappend = (++) ๋‚ฏ์„ค์ง€ ์•Š๋‹ค. Alternative ๋ฐ MonadPlus์™€ ์ˆ˜์ƒํ•  ์ •๋„๋กœ ๋‹ฎ์•˜์ง€๋งŒ ์ค‘์š”ํ•œ ์ฐจ์ด์ ์ด ์žˆ๋‹ค. ์ธ์Šคํ„ด์Šค ์„ ์–ธ์—์„œ [] ๋Œ€์‹  [a]์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. ๋ชจ๋…ธ์ด๋“œ๋Š” ๋ฌด์–ธ๊ฐ€์˜ "๋ž˜ํผ"์ด๊ฑฐ๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‹คํ˜•์„ฑ์„ ๊ฐ€์งˆ ํ•„์š”๊ฐ€ ์—†๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ •์ˆ˜๋Š” 0์„ ํ•ญ๋“ฑ์›์œผ๋กœ ๋ง์…ˆ ํ•˜์—์„œ ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. Alternative๊ฐ€ ๋ณ„๊ฐœ์˜ ํƒ€์ž… ํด๋ž˜์Šค์ธ ์ด์œ ๋Š”, ๊ณ ์œ ์˜ ์„ฑ์งˆ์„ ๊ฐ€์ง€๋Š” ํŠน์ • ์œ ํ˜•์˜ ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํฌ์ฐฉ(capture) ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์ดํ•ญ ์—ฐ์‚ฐ (<|>) :: Alternative f => f a -> f a -> f a์€ Appilcative ๋ฌธ๋งฅ๊ณผ ๋ณธ์งˆ์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. (?) ๊ธฐํƒ€ ๋ฒ•์น™๋“ค ๋…ธํŠธ ์ด ์ ˆ์€ ๋ณด๋„ˆ์Šค๋‹ค. ์ด๋Ÿฐ ๋ฒ•์น™๋“ค์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋‘๋ฉด ์ข‹์ง€๋งŒ ์ด๊ฒƒ ๋•Œ๋ฌธ์— ์ž ์„ ์„ค์น  ํ•„์š”๋Š” ์—†๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์–ธ๊ธ‰ํ•œ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€์ •๋˜๋Š” ๋ฒ•์น™๋“ค ์™ธ์—๋„ ํŠน์ • ๊ด€์ ์—์„œ๋Š” ๋ง์ด ๋˜์ง€๋งŒ, ํ˜„์กดํ•˜๋Š” ๋ชจ๋“  Alternative์™€ MonadPlus์— ๋Œ€ํ•ด ์„ฑ๋ฆฝํ•˜์ง€๋Š” ์•Š๋Š” ๋ฒ•์น™๋“ค์ด ์žˆ๋‹ค. ํŠนํžˆ ํ˜„์žฌ์˜ MonadPlus๋Š” ์ถ”๊ฐ€์ ์ธ ๋ฒ•์น™๋“ค์„ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ๋Š” ๊ฐ€์ƒ์˜ ํด๋ž˜์Šค๋“ค์˜ ๊ต์ง‘ํ•ฉ์ด๋ผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Alternative์—๋Š” ๋‹ค์Œ์˜ ๋‘ ์ถ”๊ฐ€ ๋ฒ•์น™์ด ํ”ํžˆ ์ œ์•ˆ๋œ๋‹ค. ์ด๊ฒƒ๋“ค์€ Maybe์™€ ๋ฆฌ์ŠคํŠธ์—๋Š” ์„ฑ๋ฆฝํ•˜์ง€๋งŒ ์ฝ”์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์•ˆ์— ๋ฐ˜๋ก€๊ฐ€ ์กด์žฌํ•œ๋‹ค. ๋˜ํ•œ Alternative๊ฐ€ MonadPlus ๊ธฐ๋„ํ•  ๋•Œ, ์•ž์„œ ์–ธ๊ธ‰ํ•œ mzero ๋ฒ•์น™์€ ๋‹ค์Œ ๋ฒ•์น™๋“ค์˜ ๊ฒฐ๊ณผ๋ฌผ์ด ์•„๋‹ˆ๋‹ค. (f <|> g) <*> a = (f <*> a) <|> (g <*> a) -- right distributivity (of <*>) empty <*> a = empty -- right absorption (for <*>) MonadPlus์˜ ๊ฒฝ์šฐ left distribution ๋ฒ•์น™์ด ํ”ํžˆ ์ œ์•ˆ๋˜์ง€๋งŒ ์ด๋Š” ๋ฆฌ์ŠคํŠธ์—๋Š” ์„ฑ๋ฆฝํ•˜๋Š” ๋ฐ˜๋ฉด Maybe์—๋Š” ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค. (m `mplus` n) >>= k = (m >>= k) `mplus` (n >>= k) -- left distribution ํ•œ ๋ฉด left catch ๋ฒ•์น™์€ Maybe์—๋Š” ์„ฑ๋ฆฝํ•˜์ง€๋งŒ ๋ฆฌ์ŠคํŠธ์—๋Š” ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค. return x `mplus` m = return x -- left catch ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ๋ชจ๋“  MonadPlus ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด left distribution ๋˜๋Š” left catch ์ค‘ ํ•˜๋‚˜๋งŒ ์„ฑ๋ฆฝํ•  ๊ฑฐ๋ผ๊ณ  ๊ฐ€์ •๋œ๋‹ค. ๋‘˜ ๋‹ค๋Š” ์•ˆ ๋ ๊นŒ? ๊ทธ๋ ‡๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด์ž. ๊ทธ๋Ÿฌ๋ฉด ๋ชจ๋“  x, y :: m a์— ๋Œ€ํ•ด x `mplus` y = -- monad identity (return x >>= id) `mplus` (return y >>= id) = -- left distribution (return x `mplus` return y) >>= id = -- left catch return x >>= id = -- monad identity ์ด๋Ÿฌ๋ฉด ๊ฐ€์žฅ ์ž๋ช…ํ•œ MonadPlus ๊ตฌํ˜„ ์™ธ์—๋Š” ๋ชจ๋‘ ์†Œ๊ฑฐ๋œ๋‹ค. ๋” ๋‚˜์œ ๊ฒƒ์€ ์ž„์˜์˜ x์— ๋Œ€ํ•ด mzero `mplus` x = mzero๊ฐ€ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋ชจ ๋…ธ์ด๋“œ ํ•ญ๋“ฑ ๋ฒ•์น™ mzero `mplus` x = x์„ ์ถ”๊ฐ€ํ•ด ๋ณด๋ฉด ๊ทธ ๋ชจ๋‚˜๋“œ๋Š” ๊ฐ’์„ ํ•˜๋‚˜๋งŒ ๊ฐ€์ง€๊ณ  ๋”ฐ๋ผ์„œ ์ž๋ช…ํ•œ ๋ชจ๋‚˜๋“œ์ธ Data.Proxy.Proxy์™€ ๋™ํ˜•์ด ๋œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ ๋…ธ์ด๋“œ ๋ฒ•์น™๋“ค์— ๋Œ€ํ•ด์„œ๋„ ๊ฒฌํ•ด์˜ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ๋‹ค. ์ด ๋ฒ•์น™๋“ค์— ๋ฐ˜ํ•˜๋Š” ์‚ฌ๋ก€๋กœ์„œ ํŠน์ • ๋น„๊ฒฐ์ •๋ก ์  ๋ชจ๋‚˜๋“œ๋“ค์€ ๋Œ€๊ฐœ MonadPlus๋ฅผ ์ด์šฉํ•ด ํ‘œํ˜„๋˜๊ณ  ์ด ๊ฒฝ์šฐ ํ•ต์‹ฌ ๋ฒ•์น™๋“ค์€ left zero์™€ left distribution์ด์ง€๋งŒ, ์ด๋Ÿฐ ๊ฒฝ์šฐ์—์„œ ๋ชจ ๋…ธ์ด๋“œ ๋ฒ•์น™๋“ค์€ ์ง€ํ‚ค๊ธฐ ์–ด๋ ค์›Œ์„œ ์™„ํ™”ํ•˜๊ฑฐ๋‚˜ ์™„์ „ํžˆ ๋ฒ„๋ ค์•ผ ํ•œ๋‹ค. ๋‹ค์Œ์€ ํ˜ธ๊ธฐ์‹ฌ์ด ๋งŽ์€ ๋…์ž๋“ค์„ ์œ„ํ•œ ์ถ”๊ฐ€ ์ฝ์„๊ฑฐ๋ฆฌ๋‹ค. The Haskell Wiki on MonadPlus (์ด ๋…ผ์Ÿ์€ Alternative๊ฐ€ ์ƒ๊ธฐ๊ธฐ๋„ ์ „๋ถ€ํ„ฐ ์˜ค๋ž˜ ์ด์–ด์ ธ์˜จ ๊ฒƒ์ž„์„ ์ƒ๊ธฐํ•˜์ž.) ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์˜ Distinction between typeclasses MonadPlus, Alternative, and Monoid? ์™€ Confused by the meaning of the 'Alternative' type class and its relationship to other type classes (GHC 7.x/8.x์˜ ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์˜ ๋ฌธ์„œํ™”์— ๋ฐ˜์˜๋œ ํ˜„์žฌ ์ƒํƒœ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ๊ฐœ์š”. 2010 ํ•˜์Šค ์ผˆ ๋ฆฌํฌํŠธ๋Š” ์ด ๋ฌธ์ œ์— ๊ด€ํ•ด ์ •ํ™•ํžˆ ๊ทœ์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค.) From monoids to near-semirings: the essence of MonadPlus and Alternative by Rivas, Jaskelioff and Schrijvers (๋ชจ ๋…ธ์ด๋“œ ๋ฒ•์น™์„ ๋„˜์–ด Alternative๋ฅผ ์œ„ํ•œ right distribution๊ณผ right absorption, MonadPlus๋ฅผ ์œ„ํ•œ left zero์™€ left distribution์„ ํฌํ•จํ•˜๋Š” formulation) Wren Romano on MonadPlus and seminearrings (MonadPlus right zero ๋ฒ•์น™์ด ๋„ˆ๋ฌด ๊ฐ•ํ•˜๋‹ค๋Š” ์ฃผ์žฅ.) Oleg Kiselyov on the MonadPlus laws ๋น„๊ฒฐ์ •๋ก ์  ๋ชจ๋‚˜๋“œ์˜ ๊ฒฝ์šฐ ๋ชจ ๋…ธ์ด๋“œ ๋ฒ•์น™์„ ๋ฐ˜๋Œ€ํ•˜๋Š” ์ฃผ์žฅ.) ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ Must mplus always be associative? (MonadPlus์˜ ๋ชจ ๋…ธ์ด๋“œ ๋ฒ•์น™๋“ค์˜ ํšจ์šฉ์„ฑ์— ๊ด€ํ•œ ๋…ผ์˜.) 4 ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Monad_transformers ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ ๊ฒ€์ฆ ๊ฐ„๋‹จํ•œ ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ: MaybeT ๊ฐ„๋žตํ™”๋œ ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ ๊ฒ€์ฆ ๋ณ€ํ™˜๊ธฐ ๊ณผ์ž‰ ํƒ€์ž… ๊ณก์˜ˆ ์ „์ด(lifting) lift ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณ€ํ™˜๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ State ๋ณ€ํ™˜๊ธฐ ๊ฐ์‚ฌ์˜ ๋ง ๋…ธํŠธ ์ง€๊ธˆ๊นŒ์ง€ IO ์•ก์…˜, Maybe, ๋ฆฌ์ŠคํŠธ, ์ƒํƒœ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ๋ชจ๋‚˜๋“œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋„์›€์ด ๋˜๋Š”์ง€ ์•Œ์•„๋ดค๋‹ค. ๋ชจ๋‚˜๋“œ๊ฐ€ ์ดํ† ๋ก ๋ฒ”์šฉ์ ์ธ ๋„๊ตฌ๋“ค์„ ํ™œ์šฉํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ด๋ผ๋ฉด ์—ฌ๋Ÿฌ ๋ชจ๋‚˜๋“œ์˜ ๊ธฐ๋Šฅ์„ ํ•œ ๋ฒˆ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๋˜์ง€ ์•Š์„๊นŒ. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž…์ถœ๋ ฅ๊ณผ Maybe ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ผ๋˜๊ฐ€? IO (Maybe a) ๊ฐ™์€ ํƒ€์ž…์„ ์จ๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๊ทธ๋Ÿฌ๋ฉด IO do ๋ธ”๋ก ์•ˆ์—์„œ ํŒจํ„ด ๋งค์นญ์œผ๋กœ ๊ฐ’์„ ์ถ”์ถœํ•ด์•ผ ํ•˜๋Š”๋ฐ, Maybe ๋ชจ๋‚˜๋“œ๋Š” ๊ทธ๋Ÿด ์ผ ์—†์ด ์ž‘๋™ํ•˜๋„๋ก ๋˜์–ด ์žˆ๋‹ค. ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ(monad transformer)๋Š” ๋‘ ๋ชจ๋‚˜๋“œ์˜ ํ–‰๋™์„<NAME>๋Š” ๋‹จ์ผ ๋ชจ๋‚˜๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ํŠน๋ณ„ํ•œ ํƒ€์ž…์ด๋‹ค. ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ ๊ฒ€์ฆ ์ „ ์„ธ๊ณ„ IT ์ง์›๋“ค์˜ ํ˜„์‹ค์ ์ธ ๋ฌธ์ œ๋ฅผ ํ•˜๋‚˜ ๊ณ ๋ คํ•ด ๋ณด์ž. ์‚ฌ์šฉ์ž๋“ค์ด ๊ฐ•๋ ฅํ•œ '์žฅ๋ฌธ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ(passphrase)'๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•œ ๊ฐ€์ง€ ์ ‘๊ทผ๋ฒ•์€ ์‚ฌ์šฉ์ž๊ฐ€ ์„ฑ๊ฐ€์‹  ์š”๊ตฌ์‚ฌํ•ญ๋“ค(๋Œ€๋ฌธ์ž, ์ˆซ์ž, ์•ŒํŒŒ๋ฒณ์ด๋‚˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋ฅผ ์ ์–ด๋„ ํ•˜๋‚˜์”ฉ ํฌํ•จ)์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ์†Œํ•œ์˜ ๊ธธ์ด๋ฅผ ์ž…๋ ฅํ•˜๊ฒŒ ๊ฐ•์ œํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ๋ฅผ ๋ฐ›๋Š”๋‹ค. getPassphrase :: IO (Maybe String) getPassphrase = do s <- getLine if isValid s then return $ Just s else return Nothing -- The validation test could be anything we want it to be. isValid :: String -> Bool isValid s = length s >= 8 && any isAlpha s && any isNumber s && any isPunctuation s getPassphrase๋Š” IO ์•ก์…˜์ธ๋ฐ, ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŒจ์Šค์›Œ๋“œ๊ฐ€ isValid๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•  ๊ฒฝ์šฐ Nothing์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด Maybe๋„ ์‚ฌ์šฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ์„œ Maybe๋ฅผ ๋ชจ๋‚˜๋“œ๋กœ์„œ ์“ฐ๊ณ  ์žˆ์ง€ ์•Š๋‹ค. do ๋ธ”๋ก์€ IO ๋ชจ๋‚˜๋“œ ๋‚ด์— ์žˆ๊ณ  ์šฐ๋ฆฌ๋Š” Maybe ๊ฐ’์„ returnํ•  ๋ฟ์ด๋‹ค. ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ๋Š” getPassphrase ์ž‘์„ฑ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ๋ฟ ์•„๋‹ˆ๋ผ ๋ชจ๋“  ์ฝ”๋“œ ์กฐ๊ฐ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ผ๋‹จ ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ ์ˆ˜์ง‘ ํ”„๋กœ๊ทธ๋žจ์„ ๊ณ„์†ํ•ด ๋ณด์ž. askPassphrase :: IO () askPassphrase = do putStrLn "Insert your new passphrase:" maybe_value <- getPassphrase case maybe_value of Just value -> do putStrLn "Storing in database..." -- do stuff Nothing -> putStrLn "Passphrase invalid." ์ด ์ฝ”๋“œ๋Š” maybe_value ๋ณ€์ˆ˜๋ฅผ ํ•œ ์ค„๋งŒ์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ๋‹ค์Œ ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ๋ฅผ ๊ฒ€์ฆํ•œ๋‹ค. ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ๋ฅผ ์“ฐ๋ฉด ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ๋ฅผ ํ•œ ๋ฐฉ์— ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ํŒจํ„ด ๋งค์นญ๋„ isJust ๊ฐ™์€ ๊ด€๋ฃŒ๋„ ํ•„์š” ์—†๋‹ค. ์ด ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์—์„œ๋Š” ๊ทธ ์ด์ ์ด ์‚ฌ์†Œํ•ด ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ง„๊ฐ€๋Š” ๋ณต์žกํ•œ ์ƒํ™ฉ์—์„œ ๋“œ๋Ÿฌ๋‚œ๋‹ค. ๊ฐ„๋‹จํ•œ ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ: MaybeT getPassphrase์™€ ๊ด€๋ จ๋œ ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ๊ฐ„๋‹จํžˆ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด, ์šฐ๋ฆฌ๋Š” IO ๋ชจ๋‚˜๋“œ์— Maybe ๋ชจ๋‚˜๋“œ์˜ ์ผ๋ถ€ ํŠน์„ฑ์„ ๋ถ€์—ฌํ•˜๋Š” ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ๋ฅผ ์ •์˜ํ•˜๊ณ  MaybeT๋ผ ๋ถ€๋ฅผ ๊ฒƒ์ด๋‹ค. ์ด๋Š” ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ์— ์ด๋ฆ„์„ ๋ถ™์ผ ๋•Œ, ํŠน์„ฑ์„ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋‚˜๋“œ์˜ ์ด๋ฆ„์— "T"๋ฅผ ๋ถ™์—ฌ ๋งŒ๋“œ๋Š” ๊ด€์Šต์„ ๋”ฐ๋ฅธ ๊ฒƒ์ด๋‹ค. MaybeT๋Š” m (Maybe a)์˜ ๋ž˜ํผ๋‹ค. m์€ ์ž„์˜์˜ ๋ชจ๋‚˜๋“œ์ด๋ฉฐ ์ด ์˜ˆ์ œ์—์„œ๋Š” IO๋‹ค. newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) } ์ด ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ •์˜๋Š” m์— ๋Œ€ํ•ด ๋งค๊ฐœํ™”๋œ ํƒ€์ž… ์ƒ์„ฑ์ž MaybeT๋ฅผ ๋ช…์‹œํ•œ๋‹ค. ์—ญ์‹œ ์ด๋ฆ„์ด MaybeT์ธ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง€๊ณ , ๊ทธ ๊ธฐ์ €์˜ ํ‘œํ˜„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ํŽธ์˜์„ฑ ์ ‘๊ทผ์ž ํ•จ์ˆ˜ runMaybeT๋„ ์žˆ๋‹ค. ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ์˜ ์š”์ฒด๋Š” ๋ชจ๋‚˜๋“œ๋ฅผ ๋ชจ๋‚˜๋“œ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ MaybeT m์„ Monad ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. instance Monad m => Monad (MaybeT m) where return = MaybeT . return . Just -- The signature of (>>=), specialized to MaybeT m: -- (>>=) :: MaybeT m a -> (a -> MaybeT m b) -> MaybeT m b x >>= f = MaybeT $ do maybe_value <- runMaybeT x case maybe_value of Nothing -> return Nothing Just value -> runMaybeT $ f value return ํ•จ์ˆ˜๋Š” return = MaybeT . return . return๋ผ๊ณ  ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. (๊ฐ€๋…์„ฑ์ด ์กฐ๊ธˆ ์•ˆ ์ข‹์ง€๋งŒ) do ๋ธ”๋ก์˜ ์ฒซ ์ค„๋ถ€ํ„ฐ ๋ณด์ž. runMaybeT ์ ‘๊ทผ ์ž๋Š” x๋ฅผ m (Maybe a) ๊ณ„์‚ฐ์œผ๋กœ ํ•ด์ฒดํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ do ๋ธ”๋ก ์ „์ฒด๊ฐ€ m ์•ˆ์— ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์—ญ์‹œ ์ฒซ ๋ฒˆ์งธ ์ค„์—์„œ <-๋Š” ํ•ด์ฒด๋œ ๊ณ„์‚ฐ์œผ๋กœ๋ถ€ํ„ฐ Maybe a ๊ฐ’์„ ์ถ”์ถœํ•œ๋‹ค. case ๋ฌธ์€ maybe_value๋ฅผ ํ…Œ์ŠคํŠธํ•œ๋‹ค. Nothing์˜ ๊ฒฝ์šฐ Nothing์„ m์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. Just์˜ ๊ฒฝ์šฐ Just๋กœ๋ถ€ํ„ฐ ์˜จ value์— f๋ฅผ ์ ์šฉํ•œ๋‹ค. f์˜ ๊ฒฐ๊ณผ ํƒ€์ž…์€ MaybeT m b์ด๋ฏ€๋กœ ๊ฒฐ๊ณผ๋ฅผ m ๋ชจ๋‚˜๋“œ์— ๋‹ค์‹œ ๋„ฃ์œผ๋ ค๋ฉด ๋ณ„๋„์˜ runMaybeT๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ do ๋ธ”๋ก์€ ๊ทธ ์ž์ฒด๋กœ m (Maybe b) ํƒ€์ž…์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ MaybeT ์ƒ์„ฑ์ž๋กœ ๊ฐ์‹ผ๋‹ค. ์กฐ๊ธˆ ๋ณต์žกํ•˜์ง€๋งŒ ๋ฐฉ๋Œ€ํ•œ ๋ž˜ํ•‘๊ณผ ์–ธ๋ž˜ํ•‘์„ ์ œ์™ธํ•˜๋ฉด MaybeT์˜ bind ๊ตฌํ˜„์€ Maybe์˜ ์ต์ˆ™ํ•œ bind ์—ฐ์‚ฐ์ž ๊ตฌํ˜„๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. -- (>>=) for the Maybe monad maybe_value >>= f = case maybe_value of Nothing -> Nothing Just value -> f value do ์•ˆ์— ์ ‘๊ทผ์ž์ธ runMaybeT๊ฐ€ ์žˆ๋Š”๋ฐ ๊ทธ๋Ÿผ ์™œ do ๋ธ”๋ก ์ „์— MaybeT ์ƒ์„ฑ์ž๋ฅผ ์“ฐ๋Š” ๊ฑธ๊นŒ? do ๋ธ”๋ก์€ MaybeT m์ด ์•„๋‹ˆ๋ผ m ๋ชจ๋‚˜๋“œ ๋‚ด์— ์žˆ์–ด์•ผ๋งŒ ํ•œ๋‹ค. (์ด ์‹œ์ ์—์„œ๋Š” MaybeT m์˜ bind ์—ฐ์‚ฐ์ž๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์Œ) ๋Š˜ ๊ทธ๋žฌ๋“ฏ์ด Monad์˜ ์Šˆํผํด๋ž˜์Šค์ธ Applicative์™€ Functor๋ฅผ ์œ„ํ•œ ์ธ์Šคํ„ด์Šค๋“ค๋„ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค. instance Monad m => Applicative (MaybeT m) where pure = return (<*>) = ap instance Monad m => Functor (MaybeT m) where fmap = liftM ์ด ์™ธ์—๋„ MaybeT m์„ ๋ช‡ ๊ฐ€์ง€ ํด๋ž˜์Šค๋“ค์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค์–ด๋‘๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•˜๋‹ค. instance Monad m => Alternative (MaybeT m) where empty = MaybeT $ return Nothing x <|> y = MaybeT $ do maybe_value <- runMaybeT x case maybe_value of Nothing -> runMaybeT y Just _ -> return maybe_value instance Monad m => MonadPlus (MaybeT m) where mzero = empty mplus = (<|>) instance MonadTrans MaybeT where lift = MaybeT . (liftM Just) MonadTrans๋Š” lift ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋ฏ€๋กœ ์šฐ๋ฆฌ๋Š” m ๋ชจ๋‚˜๋“œ๋กœ๋ถ€ํ„ฐ ํ•จ์ˆ˜๋ฅผ ์ทจํ•ด์„œ MaybeT m ๋ชจ๋‚˜๋“œ๋กœ ๋ฐ๋ ค์˜จ ๋‹ค์Œ do ๋ธ”๋ก ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Alternative์™€ MonadPlus์˜ ๊ฒฝ์šฐ, Maybe๊ฐ€ ์ด ํด๋ž˜์Šค๋“ค์˜ ์ธ์Šคํ„ด์Šค์ด๋ฏ€๋กœ MaybeT m๋„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ๊ฒƒ์ด ํ•ฉ๋ฆฌ์ ์ด๋‹ค. ๊ฐ„๋žตํ™”๋œ ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ ๊ฒ€์ฆ ์œ„์˜ ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ ๊ฒ€์ฆ ์˜ˆ์ œ๋Š” MaybeT ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ๋ฅผ ์ด์šฉํ•ด ๊ฐ„์†Œํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. getPassphrase :: MaybeT IO String getPassphrase = do s <- lift getLine guard (isValid s) -- Alternative provides guard. return s askPassphrase :: MaybeT IO () askPassphrase = do lift $ putStrLn "Insert your new passphrase:" value <- getPassphrase lift $ putStrLn "Storing in database..." ์ฝ”๋“œ๊ฐ€ ๋” ๊ฐ„๋‹จํ•ด์กŒ๋‹ค. ํŠนํžˆ ์‚ฌ์šฉ์ž ํ•จ์ˆ˜ askPassphrase๊ฐ€ ๊ทธ๋ ‡๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๊ฒฐ๊ณผ๊ฐ€ Nothing ์ธ์ง€ Just ์ธ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ์ ์ด๋‹ค. bind ์—ฐ์‚ฐ์ž๊ฐ€ ๊ทธ ์ผ์„ ๋Œ€์‹ ํ•ด์ค€๋‹ค. MaybeT IO ๋ชจ๋‚˜๋“œ ์•ˆ์œผ๋กœ getLine ํ•จ์ˆ˜์™€ putStrLn ํ•จ์ˆ˜๋ฅผ ๋“ค์—ฌ์˜ค๊ธฐ ์œ„ํ•ด lift๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ–ˆ๋Š”์ง€์— ์ฃผ๋ชฉํ•˜์ž. ๋˜ํ•œ MaybeT IO๊ฐ€ Alternative์˜ ์ธ์Šคํ„ด์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ์˜ ํƒ€๋‹น์„ฑ ๊ฒ€์‚ฌ๋ฅผ guard ๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. guard๋Š” ๋‚˜์œ ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ์˜ ๊ฒฝ์šฐ empty(์ฆ‰ IO Nothing)์„ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๋‹ค. ๋ถ€์ˆ˜์ ์œผ๋กœ, MonadPlus์˜ ๋„์›€์„ ๋ฐ›์œผ๋ฉด ์‚ฌ์šฉ์ž์—๊ฒŒ ์˜ฌ๋ฐ”๋ฅธ ํŒจ์Šคํ”„๋ ˆ์ด์ฆˆ๋ฅผ ๋ฌดํ•œ์ • ์š”๊ตฌํ•˜๋Š” ๊ฒƒ๋„ ์•„์ฃผ ์‰ฌ์›Œ์ง„๋‹ค. askPassphrase :: MaybeT IO () askPassphrase = do lift $ putStrLn "Insert your new passphrase:" value <- msum $ repeat getPassphrase lift $ putStrLn "Storing in database..." ๋ณ€ํ™˜๊ธฐ ๊ณผ์ž‰ transformers ํŒจํ‚ค์ง€๋Š” ํ”ํžˆ ์“ฐ์ด๋Š” ๋ชจ๋‚˜๋“œ๋“ค์„ ์œ„ํ•œ ๋ณ€ํ™˜๊ธฐ๋“ค์ด ์žˆ๋Š” ๋ชจ๋“ˆ๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด MaybeT๋Š” Control.Monad.Trans.Maybe์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค) ์ด ๋ณ€ํ™˜๊ธฐ๋“ค์€ ๋น„๋ณ€ํ™˜๊ธฐ ๋ฒ„์ „๋“ค์— ๋Œ€์‘ํ•ด ์ผ๊ด€๋˜๊ฒŒ ์ •์˜๋˜์–ด ์žˆ๋‹ค. ์ฆ‰ ๊ทธ ๊ตฌํ˜„์ด ๋‹ค๋ฅธ ๋ชจ๋‚˜๋“œ๋ฅผ ์—ฎ๊ธฐ ์œ„ํ•œ ๋ถ€์ˆ˜์ ์ธ ๊ฐ์‹ธ๊ธฐ์™€ ํ’€์–ดํ—ค์น˜๊ธฐ๋ฅผ ๋นผ๋ฉด ๋Œ€๋™์†Œ์ดํ•˜๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ๋ณ€ํ™˜๊ธฐ๊ฐ€ ๊ทธ ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ผ๋Š” ๋น„๋ณ€ํ™˜๊ธฐ ๋ชจ๋‚˜๋“œ(์˜ˆ: MaybeT์˜ Maybe)๋ฅผ ์ „๊ตฌ ๋ชจ๋‚˜๋“œ(precursor monad)๋ผ ์นญํ•˜๊ณ  ๋ณ€ํ™˜๊ธฐ๊ฐ€ ์ ์šฉ๋˜๋Š” ๋‹ค๋ฅธ ๋ชจ๋‚˜๋“œ(์˜ˆ: MaybeT IO์˜ IO)๋ฅผ ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ(base monad)๋ผ ์นญํ•˜๊ฒ ๋‹ค. ์˜ˆ์‹œ๋ฅผ ์ž„์˜๋กœ ํ•˜๋‚˜ ๋ฝ‘์ž๋ฉด, ReaderT Env IO String์€ Env ํƒ€์ž…(์ „๊ตฌ ๋ชจ๋‚˜๋“œ์ธ Reader์˜ semantics)์˜ ์–ด๋–ค ํ™˜๊ฒฝ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ์ฝ์–ด์˜ค๊ณ , ์–ด๋–ค IO๋ฅผ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ, String ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ›์•„์˜ค๋Š” ์ž‘์—…์„ ์ˆ˜๋ฐ˜ํ•˜๋Š” ๊ณ„์‚ฐ(computation)์ด๋‹ค. ์ด ๋ณ€ํ™˜๊ธฐ์˜ bind ์—ฐ์‚ฐ์ž์™€ return์ด ์ „๊ตฌ ๋ชจ๋‚˜๋“œ์˜ semantic์„ ๋ฐ˜์˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ReaderT Env IO String ํƒ€์ž…์˜ do ๋ธ”๋ก์€ ๋ฐ”๊นฅ์—์„œ ๋ณด๋ฉด Reader ๋ชจ๋‚˜๋“œ์˜ do ๋ธ”๋ก์ฒ˜๋Ÿผ ๋ณด์ผ ๊ฒƒ์ด๋‹ค. ๋‹จ lift๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด IO ์•ก์…˜์„ ๋ผ์›Œ ๋„ฃ๊ธฐ๊ฐ€ ์•„์ฃผ ์‰ฌ์›Œ์ง„๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. ํƒ€์ž… ๊ณก์˜ˆ MaybeT์˜ ํƒ€์ž… ์ƒ์„ฑ์ž๋Š” ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ์˜ Maybe ๊ฐ’์— ๋Œ€ํ•œ wrapper๋ผ๋Š” ๊ฒƒ์„ ๋ดค์—ˆ๋‹ค. ๋Œ€์‘ํ•˜๋Š” ์ ‘๊ทผ์ž runMaybeT๋Š” m (Maybe a) ํƒ€์ž…์˜ ๊ฐ’์„ ๋Œ๋ ค์ค€๋‹ค. ์ฆ‰ ์ด ๊ฐ’์€ ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ ์•ˆ์—์„œ ๋ฐ˜ํ™˜๋œ, ์ „๊ตฌ ๋ชจ๋‚˜๋“œ์˜ ๊ฐ’์ด๋‹ค. ์ด์™€ ๋น„์Šทํ•˜๊ฒŒ ๋ฆฌ์ŠคํŠธ ๋ฐ Either์— ๊ธฐ๋ฐ˜ํ•œ ListT ๋ฐ ExceptT ๋ณ€ํ™˜๊ธฐ์˜ ๊ฒฝ์šฐ๋ฅผ ๋ณด๋ฉด, runListT :: ListT m a -> m [a] ๊ทธ๋ฆฌ๊ณ  runExceptT :: ExceptT e m a -> m (Either e a) ํ•˜์ง€๋งŒ ๋ชจ๋“  ๋ณ€ํ™˜๊ธฐ๊ฐ€ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ „๊ตฌ ๋ชจ๋‚˜๋“œ์™€ ์—ฐ๊ณ„๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์œ„์—์„œ ์˜ˆ๋กœ ๋“  ์ „๊ตฌ ๋ชจ๋‚˜๋“œ๋“ค๊ณผ ๋‹ฌ๋ฆฌ Writer, Reader, State, Cont ๋ชจ๋‚˜๋“œ๋Š” ๋‹ค์ค‘ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง€์ง€๋„ ์•Š๊ณ  ๋‹ค์ค‘ ์ธ์ž๋ฅผ ๊ฐ€์ง€๋Š” ์ƒ์„ฑ์ž๋„ ์—†๋‹ค. ๊ทธ๋ž˜์„œ ์ด๊ฒƒ๋“ค์€ ๋ณ€ํ™˜๊ธฐ ๋ฒ„์ „์˜ run... T์™€ ๋น„์Šทํ•œ ๋‹จ์ˆœํ•œ unwrapper๋กœ ์ž‘๋™ํ•˜๋Š” run... ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง„๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋Š” ๊ฐ๊ฐ์˜ ๊ฒฝ์šฐ run... ์™€ run... T ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ ํƒ€์ž…์„ ๋ณด์—ฌ์ค€๋‹ค. ๊ฐ๊ฐ ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ์™€ ๋ณ€ํ™˜๋œ ๋ชจ๋‚˜๋“œ์— ์˜ํ•ด ๊ฐ์‹ธ์ง„ ํƒ€์ž…์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. 1 ์ „๊ตฌ ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ ๊ธฐ์กด ํƒ€์ž… (์ „๊ตฌ ๋ชจ๋‚˜๋“œ์— ์˜ํ•ด "๊ฐ์‹ธ์ง€๋Š”") ํ•ฉ์„ฑ ํƒ€์ž… (๋ณ€ํ™˜๊ธฐ์— ์˜ํ•ด "๊ฐ์‹ธ์ง„") Writer WriterT (a, w) m (a, w) Reader ReaderT r -> a r -> m a State StateT s -> (a, s) s -> m (a, s) Cont ContT (a -> r) -> r (a -> m r) -> m r ๊ฒฐํ•ฉ๋œ ํƒ€์ž…์—์„œ๋Š” ์ „๊ตฌ ๋ชจ๋‚˜๋“œ ํƒ€์ž… ์ƒ์„ฑ์ž๊ฐ€ ๋น ์ง„ ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. Maybe๋‚˜ ๋ฆฌ์ŠคํŠธ ๋ฅ˜์ฒ˜๋Ÿผ ํฅ๋ฏธ๋กœ์šด ์ƒ์„ฑ์ž๊ฐ€ ์—†์œผ๋ฉด ๋ณ€ํ™˜๋œ ๋ชจ๋‚˜๋“œ๋ฅผ ํ’€์–ดํ—ค์นœ ํ›„์—๋Š” ์ „๊ตฌ ๋ชจ๋‚˜๋“œ์˜ ํƒ€์ž…์„ ์œ ์ง€ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋’ค์˜ ์„ธ ๊ฒฝ์šฐ๋Š” ํ•จ์ˆ˜ ํƒ€์ž…๋“ค์ด wrapping ๋œ ๊ฒƒ๋„ ๋ˆˆ์—ฌ๊ฒจ๋ณด์ž. ์˜ˆ๋ฅผ ๋“ค์–ด StateT๋Š” s -> (a, s) ๊ผด์˜ ์ƒํƒœ ๋ณ€ํ™˜ ํ•จ์ˆ˜๋“ค์„ s -> m (a, s) ๊ผด์˜ ์ƒํƒœ ๋ณ€ํ™˜ ํ•จ์ˆ˜๋“ค๋กœ ํƒˆ๋ฐ”๊ฟˆ์‹œํ‚จ๋‹ค. ๊ฐ์‹ธ์ง„ ํ•จ์ˆ˜์˜ ์ตœ์ข… ํƒ€์ž…๋งŒ์ด ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ ์•ˆ์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค. ReaderT๋„ ๋น„์Šทํ•˜๋‹ค. ContT๋Š” ์ƒํ™ฉ์ด ๋‹ค๋ฅธ๋ฐ, Cont(continuation ๋ชจ๋‚˜๋“œ)์˜ semantic ๋•Œ๋ฌธ์ด๋‹ค. ๊ฐ์‹ธ์ง„ ํ•จ์ˆ˜์™€ ๊ทธ๊ฒƒ์˜ ํ•จ์ˆ˜ ์ธ์ž์˜ ์ตœ์ข… ํƒ€์ž…๋“ค์€ ๊ฐ™์•„์•ผ ํ•˜๋ฉฐ, ๋”ฐ๋ผ์„œ ๋ณ€ํ™˜๊ธฐ๋Š” ๋‘˜ ๋‹ค ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ์— ๋„ฃ๋Š”๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋‚˜๋“œ์˜ ๋ณ€ํ™˜ ๋ฒ„์ „์„ ๋งŒ๋“œ๋Š” ๋งˆ๋ฒ•์˜ ๊ณต์‹์€ ์—†๋‹ค. ๊ฐ ๋ณ€ํ™˜๊ธฐ์˜ ํ˜•ํƒœ๋Š” ๋น„ ๋ณ€ํ™˜๊ธฐ ํƒ€์ž…์˜ ๋ฌธ๋งฅ์—์„œ ์–ด๋–ค ๊ฒƒ์ด ๋ง์ด ๋˜๋Š”์ง€์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค. ์ „์ด(lifting) ์ด์ œ lift ํ•จ์ˆ˜๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณผ ๊ฒƒ์ด๋‹ค. lift๋Š” ์ผ์ƒ์—์„œ ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ๋ฅผ ์ด์šฉํ•  ๋•Œ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ๊ฐ€์žฅ ๋จผ์ € ํ™•์‹คํžˆ ํ•  ๊ฒƒ์€ "lift"๋ผ๋Š” ์ด๋ฆ„ ๊ทธ ์ž์ฒด๋‹ค. ์šฐ๋ฆฌ๋Š” ๋น„์Šทํ•œ ์ด๋ฆ„์˜ liftM์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋‹ค. ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ์—์„œ ๋ดค๋“ฏ์ด liftM์€ fmap์˜ ๋ชจ๋‚˜๋“œ ํŠนํ™” ๋ฒ„์ „์ด๋‹ค. liftM :: Monad m => (a -> b) -> m a -> m b liftM์€ ๋ชจ๋‚˜๋“œ m ๋‚ด๋ถ€์˜ ๊ฐ’์— ํ•จ์ˆ˜ (a -> b)๋ฅผ ์ ์šฉํ•œ๋‹ค. ์ด๊ฒƒ์„ ์ธ์ž๊ฐ€ ํ•˜๋‚˜์ธ ํ•จ์ˆ˜๋กœ ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. liftM :: Monad m => (a -> b) -> (m a -> m b) liftM์€ ํ‰๋ฒ”ํ•œ ํ•จ์ˆ˜๋ฅผ m ์•ˆ์—์„œ ์ž‘๋™ํ•˜๋Š” ํ•จ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ "์ „์ด"๋Š” ๋ฌด์–ธ๊ฐ€๋ฅผ ๋‹ค๋ฅธ ๋ฌด์–ธ๊ฐ€๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์„ ์ง€์นญํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‚˜๋“œ๋กœ ๋ฐ”๊ฟจ๋‹ค. liftM์€ do ๋ธ”๋ก์ด๋‚˜ ๋‹ค๋ฅธ ๊ธฐ๊ต ์—†์ด ํ‰๋ฒ”ํ•œ ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‚˜ ๋”• ๊ฐ’์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. bind notation do notation liftM monadicValue >>= \x -> return (f x) do x <- monadicValue return (f x) liftM f monadicValue lift ํ•จ์ˆ˜๋Š” ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•œ๋‹ค. ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ ๊ณ„์‚ฐ์„ ํ•ฉ์„ฑ ๋ชจ๋‚˜๋“œ๋กœ ๋ฐ๋ ค์˜จ๋‹ค bring (์ข€ ๋” ์ผ๋ฐ˜์ ์ธ ๋‹จ์–ด๋ฅผ ์“ฐ์ž๋ฉด, ๊ฒฉ์ƒ์‹œํ‚จ๋‹ค promote). ๊ทธ๋Ÿผ์œผ๋กœ์จ ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ ๊ณ„์‚ฐ๋“ค์„ ํ•ฉ์„ฑ ๋ชจ๋‚˜๋“œ ๋‚ด์˜ ๋” ํฐ ๊ณ„์‚ฐ์˜ ์ผ๋ถ€๋กœ์„œ ์‰ฝ๊ฒŒ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. lift๋Š” Control.Monad.Trans.Class์— ์žˆ๋Š” MonadTrans ํด๋ž˜์Šค์˜ ํ•˜๋‚˜๋ฟ์ธ ๋ฉ”์„œ๋“œ๋‹ค. ๋ชจ๋“  ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ๋Š” MonadTrans์˜ ์ธ์Šคํ„ด์Šค์ด๋ฏ€๋กœ ์ด ๋ชจ๋‘์— lift๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. class MonadTrans t where lift :: (Monad m) => m a -> t m a IO ์—ฐ์‚ฐ์— ํŠนํ™”๋œ lift์˜ ๋ณ€์ข…์ธ liftIO๋ผ๋Š” ๊ฒƒ๋„ ์žˆ๋‹ค. ์ด๋Š” Control.Monad.IO.Class์— ์žˆ๋Š” MonadIO ํด๋ž˜์Šค์˜ ํ•˜๋‚˜๋ฟ์ธ ๋ฉ”์„œ๋“œ๋‹ค. class (Monad m) => MonadIO m where liftIO :: IO a -> m a liftIO๋Š” ์—ฌ๋Ÿฌ ๋ณ€ํ™˜๊ธฐ๊ฐ€ ๋‹จ์ผ ํ•ฉ์„ฑ ๋ชจ๋‚˜๋“œ๋กœ ์Œ“์—ฌ์žˆ์„ ๋•Œ ํŽธ๋ฆฌํ•˜๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ IO๋Š” ํ•ญ์ƒ ๊ฐ€์žฅ ์•ˆ์ชฝ์˜ ๋ชจ๋‚˜๋“œ์ด๋ฏ€๋กœ IO ๊ฐ’๋“ค์„ ๊ฐ€์žฅ ๋ฐ”๊นฅ์œผ๋กœ ๋นผ๋‚ด๋ ค๋ฉด ์ „์ด๋ฅผ ํ•œ ๋ฒˆ๋ณด๋‹ค ๋งŽ์ด ํ•ด์•ผ ํ•œ๋‹ค. liftIO๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์–ด๋Š ๊นŠ์ด์—์„œ๋“  IO ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ์ •์˜๋˜์–ด ์žˆ๋‹ค. lift ๊ตฌํ˜„ํ•˜๊ธฐ lift ๊ตฌํ˜„์€ ๋Œ€๊ฐœ ์ง๊ด€์ ์ด๋‹ค. MaybeT ๋ณ€ํ™˜๊ธฐ๋ฅผ ๋ณด์ž. instance MonadTrans MaybeT where lift m = MaybeT (liftM Just m) ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ์˜ ๋ชจ๋‚˜ ๋”• ๊ฐ’์œผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. liftM์„ ์ด์šฉํ•ด(fmap์„ ์จ๋„ ๋œ๋‹ค) ์ „๊ตฌ ๋ชจ๋‚˜๋“œ๋ฅผ ํ†ต๊ณผ(Just ์ƒ์„ฑ์ž๋ฅผ ๊ฑฐ์ณ) ํ•˜์—ฌ m a์—์„œ m (Maybe a)์— ๋„๋‹ฌํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ MaybeT ์ƒ์„ฑ์ž๋กœ ๊ฐ์‹ผ๋‹ค. ์—ฌ๊ธฐ์„œ liftM์€ ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ ์•ˆ์—์„œ ์ž‘๋™ํ•˜๋ฉฐ, ์ด๋Š” ์ด์ „์— ๋ณธ (>>=)์˜ ๊ตฌํ˜„์—์„œ do ๋ธ”๋ก์„ MaybeT๋กœ ๊ฐ์‹ผ ๊ฒƒ์ด ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ ์•ˆ์— ์žˆ๋˜ ๊ฒƒ๊ณผ ๋น„์Šทํ•œ ์ƒํ™ฉ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ liftM์€ ๋ฒ”์šฉ์ ์œผ๋กœ ์ •์˜๋  ์ˆ˜ ์žˆ๋Š”๋ฐ lift ํ•จ์ˆ˜๋Š” ์™œ ๋ชจ๋‚˜๋“œ๋งˆ๋‹ค ๋”ฐ๋กœ ์ •์˜ํ•ด์•ผ ํ• ๊นŒ? Identity๋Š” Data.Functor.Identity์— ์ •์˜๋œ ์ž๋ช…ํ•œ ํŽ‘ํ„ฐ๋‹ค. newtype Identity a = Identity { runIdentity :: a } ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์€ Monad ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง„๋‹ค. instance Monad Identity where return a = Identity a m >>= k = k (runIdentity m) ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ IdentityT๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์ž. Identity์™€ ๋น„์Šทํ•˜์ง€๋งŒ a ๋Œ€์‹  m a ํƒ€์ž…์˜ ๊ฐ’๋“ค์„ ๋ž˜ํ•‘ ํ•œ๋‹ค. ์ตœ์†Œํ•œ ์ด๊ฒƒ์˜ Monad ๋ฐ MonadTrans ์ธ์Šคํ„ด์Šค๋“ค์€ ์ž‘์„ฑํ•ด ๋ณด์ž. ๋ณ€ํ™˜๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ State ๋ณ€ํ™˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์ถ”๊ฐ€์ ์ธ ์˜ˆ์ œ๋กœ์„œ StateT์˜ ๊ตฌํ˜„์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณผ ๊ฒƒ์ด๋‹ค. ๊ณ„์†ํ•˜๊ธฐ ์ „์— State ๋ชจ๋‚˜๋“œ์— ๊ด€ํ•œ ์ ˆ์„ ๋‹ค์‹œ ์ฝ์–ด๋ณด๋ฉด ์ข‹๋‹ค. State ๋ชจ๋‚˜๋“œ๊ฐ€ newtype State s a = State { runState :: (s -> (a, s)) }๋ผ๋Š” ์ •์˜์— ๊ธฐ๋ฐ˜ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, StateT ๋ณ€ํ™˜๊ธฐ๋Š” ๋‹ค์Œ ์ •์˜์— ๊ธฐ๋ฐ˜ํ•œ๋‹ค. newtype StateT s m a = StateT { runStateT :: (s -> m (a, s)) } StateT s m์€ ๋‹ค์Œ์˜ Monad ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง„๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ „๊ตฌ ๋ชจ๋‚˜๋“œ์ธ State๋ฅผ ๊ณ์— ํ‘œ๊ธฐํ–ˆ๋‹ค. State StateT newtype State s a = State { runState :: (s -> (a, s)) } instance Monad (State s) where return a = State $ \s -> (a, s) (State x) >>= f = State $ \s -> let (v, s') = x s in runState (f v) s' newtype StateT s m a = StateT { runStateT :: (s -> m (a, s)) } instance (Monad m) => Monad (StateT s m) where return a = StateT $ \s -> return (a, s) (StateT x) >>= f = StateT $ \s -> do (v,s') <- x s -- get new value and state runStateT (f v) s' -- pass them to f ์šฐ๋ฆฌ์˜ return ์ •์˜๋Š” ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ์˜ return ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค. (>>=)๋Š” do ๋ธ”๋ก์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ ๋‚ด์—์„œ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋…ธํŠธ ๋ถ€์ˆ˜์ ์œผ๋กœ, State ๋ชจ๋‚˜๋“œ์— ๊ด€ํ•œ ์žฅ์—์„œ State ์ƒ์„ฑ์ž ๋Œ€์‹  state ํ•จ์ˆ˜๊ฐ€ ์žˆ์—ˆ๋˜ ์ด์œ ๋ฅผ ๋งˆ์นจ๋‚ด ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. transformers์™€ mtl ํŒจํ‚ค์ง€์—์„œ State s๋Š” StateT s Identity์˜ ํƒ€์ž… ๋™์˜์–ด๋กœ์„œ ๊ตฌํ˜„๋˜๋ฉฐ, ์—ฌ๊ธฐ์„œ Identity๋Š” ์•ž์ ˆ์˜ ์—ฐ์Šต๋ฌธ์ œ์—์„œ ์†Œ๊ฐœํ•œ ๋”๋ฏธ ๋ชจ๋‚˜๋“œ๋‹ค. ์ตœ์ข… ๋ชจ๋‚˜๋“œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€ ์จ์˜จ, newtype์„ ์ด์šฉํ•œ ์ •์˜์™€ ๋™๋“ฑํ•˜๋‹ค. ํ•ฉ์„ฑ๋œ StateT s m ๋ชจ๋‚˜๋“œ๋“ค์„ ์ƒํƒœ ๋ชจ๋‚˜๋“œ๋กœ์จ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋ถ„๋ช… get ๋ช…๋ น๊ณผ put ๋ช…๋ น์ด ํ•„์š”ํ•ด์งˆ ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” mtl ํŒจํ‚ค์ง€ ์Šคํƒ€์ผ์˜ ์ •์˜๋“ค์„ ๋ณด์—ฌ์ฃผ๊ฒ ๋‹ค. mtl์€ ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ๋“ค ์ž์ฒด ์™ธ์—๋„, ์ผ๋ฐ˜์ ์ธ ๋ชจ๋‚˜๋“œ๋“ค์˜ ๊ธฐ์ดˆ ์—ฐ์‚ฐ๋“ค์„ ์œ„ํ•œ ํƒ€์ž… ํด๋ž˜์Šค๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Control.Monad.State์˜ MonadState ํด๋ž˜์Šค๋Š” get๊ณผ put ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง„๋‹ค. instance (Monad m) => MonadState s (StateT s m) where get = StateT $ \s -> return (s, s) put s = StateT $ \_ -> return ((),s) MonadState s m => MonadState s (MaybeT m)์ฒ˜๋Ÿผ ๋‹ค๋ฅธ ๋ณ€ํ™˜๊ธฐ๋“ค๋กœ ๊ฐ์‹ผ ์ƒํƒœ ๋ชจ๋‚˜๋“œ๋“ค์„ ์œ„ํ•œ MonadState ์ธ์Šคํ„ด์Šค๋“ค๋„ ์žˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ get๊ณผ put์„ ๋ช…์‹œ์ ์œผ๋กœ ์ „์ดํ•  ํ•„์š”์„ฑ์„ ์—†์• ์„œ ์šฐ๋ฆฌ๋ฅผ ์ข€ ๋” ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค. ํ•ฉ์„ฑ๋œ ๋ชจ๋‚˜๋“œ๋“ค์„ ์œ„ํ•œ MonadState ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ทธ๋Ÿฌํ•œ ์ „์ด๋ฅผ ๋Œ€์‹  ์ฒ˜๋ฆฌํ•ด ์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋Š” ์ธ์Šคํ„ด์Šค๋“ค์„ ํ•ฉ์„ฑ ๋ชจ๋‚˜๋“œ๋กœ ์ „์ดํ•˜๋ฉด ์œ ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด StateT๊ฐ€ MonadPlus์˜ ์ธ์Šคํ„ด์Šค์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“  ํ•ฉ์„ฑ ๋ชจ๋‚˜๋“œ๋Š” MonadPlus์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. instance (MonadPlus m) => MonadPlus (StateT s m) where mzero = StateT $ \_ -> mzero (StateT x1) `mplus` (StateT x2) = StateT $ \s -> (x1 s) `mplus` (x2 s) mzero์™€ mplus์˜ ๊ตฌํ˜„์€ ์ž๋ช…ํ•˜๋‹ค. ์ฆ‰ ์‹ค์ œ ์ž‘์—…์„ ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ์˜ ์ธ์Šคํ„ด์Šค์—๊ฒŒ ์œ„์ž„ํ•œ๋‹ค. ๋ชจ๋‚˜๋“œ ๋ณ€ํ™˜๊ธฐ๋Š” lift๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ MonadTrans๋ฅผ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง์ž. instance MonadTrans (StateT s) where lift c = StateT $ \s -> c >>= (\x -> return (x, s)) lift ํ•จ์ˆ˜๋Š” ์ƒํƒœ ๋ณ€ํ™˜ ํ•จ์ˆ˜ StateT๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๊ธฐ๋ฐ˜ ๋ชจ๋‚˜๋“œ ๋‚ด์—์„œ์˜ ๊ณ„์‚ฐ์„, ๊ฒฐ๊ด๊ฐ’์„ ์ž…๋ ฅ ์ƒํƒœ๋กœ ํฌ์žฅํ•˜๋Š” ํ•จ์ˆ˜์— ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. ๊ฐ€๋ น StateT๋ฅผ List ๋ชจ๋‚˜๋“œ์— ์ ์šฉํ•œ๋‹ค๋ฉด, ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜(์ฆ‰ List ๋ชจ๋‚˜๋“œ ๋‚ด์—์„œ์˜ ๊ณ„์‚ฐ)๋Š” State s [] ์•ˆ์œผ๋กœ ์ „์ดํ•  ์ˆ˜ ์žˆ๊ณ  ๊ฑฐ๊ธฐ์„œ StateT (s -> [(a, s)])์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋œ๋‹ค. ์ฆ‰ ์ „์ด๋œ ๊ณ„์‚ฐ์€ ๊ทธ๊ฒƒ์˜ ์ž…๋ ฅ ์ƒํƒœ๋กœ๋ถ€ํ„ฐ ์—ฌ๋Ÿฌ ๊ฐœ์˜ (๊ฐ’, ์ƒํƒœ) ์ง์„ ์ƒ์‚ฐํ•œ๋‹ค. ์ด๊ฒƒ์€ StateT ๋‚ด์˜ ๊ณ„์‚ฐ(computation)์„ "๋‚˜๋ˆ ์„œ", ์ „์ด๋œ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ๊ฐ๊ฐ์˜ ๊ฐ’์— ๋Œ€ํ•ด ๊ณ„์‚ฐ์˜ ๋‹ค๋ฅธ ๋ถ„๊ธฐ๋“ค์„ ์ƒ์„ฑํ•œ๋‹ค. StateT๋ฅผ ๋‹ค๋ฅธ ๋ชจ๋‚˜๋“œ์— ์ ์šฉํ•˜๋ฉด lift ํ•จ์ˆ˜์˜ ์˜๋ฏธ๊ฐ€ ๋‹ฌ๋ผ์งˆ ๊ฒƒ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ get๊ณผ put์„ ์ด์šฉํ•ด state :: MonadState s m => (s -> (a, s)) -> m a๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์ž. MaybeT (State s)์™€ StateT s Maybe๋Š” ๋™๋“ฑํ• ๊นŒ? (ํžŒํŠธ: ๊ฐ ๊ฒฝ์šฐ์— run... T unwrapper๊ฐ€ ๋ฌด์—‡์„ ์ƒ์‚ฐํ•˜๋Š”์ง€ ๋น„๊ตํ•ด ๋ณด๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.) ๊ฐ์‚ฌ์˜ ๋ง ์ด ๊ณผ๋ชฉ์—์„  All About Monads์˜ ์ €์ž Jeff Newbern์˜ ํ—ˆ๋ฝ์„ ๋ฐ›์•„ ๋งŽ์€ ์ธ์šฉ๋ฌธ์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ๋…ธํŠธ wrapping ํ•ด์„์€ 2.0.0.0 ์ด์ „์˜ mtl ํŒจํ‚ค์ง€์— ๋Œ€ํ•ด์„œ๋งŒ ์˜ฌ๋ฐ”๋ฅด๋‹ค. โ†ฉ 2 ๊ณ ๊ธ‰๋ฐ˜ ์—ฌ๊ธฐ์„œ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ž๋ฃŒ๊ตฌ์กฐ, ํƒ€์ž… ์ด๋ก  ๋“ฑ ๋” ํญ๋„“์€ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐœ๋…์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋™์‹œ์„ฑ ๊ฐ™์€ ๋ณด๋‹ค ์‹ค์šฉ์ ์ธ ์ฃผ์ œ๋„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๋ชฉ์ฐจ ํ•˜์Šค ์ผˆ ๊ณ ๊ธ‰ ๋ชจ ๋…ธ์ด๋“œ ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ Foldable Traversable ์• ๋กœ ํŠœํ† ๋ฆฌ์–ผ ์• ๋กœ ์ดํ•ดํ•˜๊ธฐ Continuation passing style (CPS) ์ง€ํผ ๋ Œ์ฆˆ Comonads Value recursion (MonadFix) Effectful streaming ๊ฐ€๋ณ€ ๊ฐ์ฒด ๋™์‹œ์„ฑ Template Haskell Type Families ํƒ€์ž…๊ณผ์˜ ์œ ํฌ ๋‹คํ˜•์„ฑ ๊ธฐ์ดˆ Existentially qualified types ํƒ€์ž… ํด๋ž˜์Šค ๊ณ ๊ธ‰ ํŒฌํ…€ ํƒ€์ž… ์ผ๋ฐ˜ํ™”๋œ ๋Œ€์ˆ˜์  ๋ฐ์ดํ„ฐ ํƒ€์ž… (GADT) ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋Œ€์ˆ˜ ํƒ€์ž… ์ƒ์„ฑ์ž & ์ข…(kind) ์—ฌ๋Ÿฌ ์ด๋ก ๋“ค ํ‘œ๊ธฐ ์˜๋ฏธ๋ก (Denotational semantics) Equational reasoning Program derivation ๋ฒ”์ฃผ๋ก  Curry-Howard ๋™ํ˜• fix์™€ ์žฌ๊ท€ ํ•˜์Šค ์ผˆ ์„ฑ๋Šฅ ์ž…๋ฌธ ์„ฑ๋Šฅ ์˜ˆ์‹œ๋“ค ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ ์ง€์—ฐ์„ฑ Time and space profiling Strictness ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„ ์ž๋ฃŒ๊ตฌ์กฐ Parallelism 1 ํ•˜์Šค ์ผˆ ๊ณ ๊ธ‰ ํ•˜์Šค ์ผˆ ๊ณ ๊ธ‰ ๋ชจ ๋…ธ์ด๋“œ ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ Foldable Traversable ์• ๋กœ ํŠœํ† ๋ฆฌ์–ผ ์• ๋กœ ์ดํ•ดํ•˜๊ธฐ Continuation passing style (CPS) ์ง€ํผ ๋ Œ์ฆˆ Comonads - ์›๋ฌธ ์—†์Œ Value recursion (MonadFix) - ์›๋ฌธ ์—†์Œ Effectful streaming - ์›๋ฌธ ์—†์Œ ๊ฐ€๋ณ€ ๊ฐ์ฒด ๋™์‹œ์„ฑ - ์›๋ฌธ์ด ๋Œ€์ถฉ ์ž‘์„ฑ๋จ. "Parallel and Concurrent Programming in Haskell" ์ฑ…์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. Template Haskell - ์›๋ฌธ ์—†์Œ Type Families - ์›๋ฌธ ์—†์Œ 01 ๋ชจ ๋…ธ์ด๋“œ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Monoids ์†Œ๊ฐœ ์˜ˆ์ œ ํ•จ์ˆ˜ ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ ํ•˜์Šค ์ผˆ ์ •์˜์™€ ๋ฒ•์น™ ์˜ˆ์‹œ ๋™ํ˜•์‚ฌ์ƒ(Homomorphism) ๋” ์ฝ์„๊ฑฐ๋ฆฌ ๋…ธํŠธ ์•ž์—์„œ ๋ชจ ๋…ธ์ด๋“œ์™€ Monoid ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ์ง€๋‚˜๊ฐ€๋“ฏ ์–ธ๊ธ‰ํ–ˆ์—ˆ๋‹ค(ํŠนํžˆ MonadPlus๋ฅผ ๋…ผํ•  ๋•Œ). ์—ฌ๊ธฐ์„œ๋Š” ์ด๊ฒƒ๋“ค์„ ์ž์„ธํ•˜๊ฒŒ ์‚ดํŽด๋ณด๊ณ  ์–ด๋–ค ์ ์ด ์œ ์šฉํ•œ์ง€ ์‚ดํŽด๋ณธ๋‹ค. ์†Œ๊ฐœ ๋ชจ ๋…ธ์ด๋“œ(m, mappend, mempty)๋Š” m ํƒ€์ž…์œผ๋กœ์„œ, ๋‘ ์›์†Œ๋ฅผ ํ•ฉ์„ฑํ•˜๋Š” ๊ฒฐํ•ฉ์„ฑ ์—ฐ์‚ฐ mappend :: m -> m -> m(ํ•˜์Šค์ผˆ์—์„  (<>)๋ผ๊ณ ๋„ ๋ถ€๋ฆ„) ๊ทธ๋ฆฌ๊ณ  mappend์˜ ์ค‘๋ฆฝ ์›์ธ ์˜์› mempty :: m์„ ๊ฐ€์ง„๋‹ค. ์ด๊ฒƒ๋“ค์„ ๋ณด๋‹ค ๊ณต์‹์ ์œผ๋กœ ์ •์˜ํ•˜๊ธฐ ์ „์— ์‹ค์ „์—์„œ์˜ ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ๋ณด์ž. ์˜ˆ์ œ ๋ง›๋ณด๊ธฐ๋กœ ๋‹ค์Œ์˜ ํ”ํ•œ ํŒจํ„ด์„ ๋ณด์ž. > (5 + 6) + 10 == 5 + (6 + 10) True > (5 * 6) * 10 == 5 * (6 * 10) True > ("Hello" ++ " ") ++ "world!" == "Hello" ++ (" " ++ "world!") True ์ด ์„ฑ์งˆ์€ ๊ฒฐํ•ฉ์„ฑ(associativity)์ด๋ผ๋Š” ๊ฒƒ์œผ๋กœ, ์œ„์˜ ํŠน์ • ๊ฐ’๋“ค์—๋งŒ ์„ฑ๋ฆฝํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ง์…ˆ ํ•˜ ๋ชจ๋“  ์ •์ˆ˜์—, ๊ณฑ์…ˆ ํ•˜ ๋ชจ๋“  ์ •์ˆ˜์—, ๊ฒฐํ•ฉ ํ•˜ ๋ชจ๋“  ๋ฆฌ์ŠคํŠธ์— ์„ฑ๋ฆฝํ•œ๋‹ค. ์ด๊ฒƒ์€ ๋˜ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ํŒจํ„ด์ด๋‹ค. > 255 + 0 == 255 && 0 + 255 == 255 True > 255 * 1 == 255 && 1 * 255 == 255 True > [1,2,3] ++ [] == [1,2,3] && [] ++ [1,2,3] == [1,2,3] True ์—ฌ๊ธฐ์„œ 0์€ ์ •์ˆ˜๋ฅผ ๋”ํ•  ๋•Œ์˜ ํ•ญ๋“ฑ์›, 1์€ ๊ณฑํ•  ๋•Œ์˜ ํ•ญ๋“ฑ์›, []๋Š” ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์–ด๋ถ™์ผ ๋•Œ์˜ ํ•ญ๋“ฑ์›์ด๋‹ค. ๋”ฐ๋ผ์„œ, ์ •์ˆ˜๋Š” ๋ง์…ˆ ํ•˜์— ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑํ•˜๋ฉฐ 0์ด ๋‹จ์œ„์›์ด๋‹ค. (Integer, (+), 0) ์ •์ˆ˜๋Š” ๊ณฑ์…ˆ ํ•˜์— ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑํ•˜๋ฉฐ 1์ด ๋‹จ์œ„์›์ด๋‹ค. (Integer, (*), 1) ๋ฆฌ์ŠคํŠธ๋Š” ๊ฒฐํ•ฉ ํ•˜์— ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. ([a], (++), []) ์ •์ˆ˜๊ฐ€ ๋‘ ๊ฐœ๋ณ„ ์—ฐ์‚ฐ ํ•˜์— ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๊ฐœ์˜ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋งŒ๋“ ๋‹ค. Sum์€ ๋ง์…ˆ, Product๋Š” ๊ณฑ์…ˆ์„ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. > import Data.Monoid > Sum 5 <> Sum 6 <> Sum 10 Sum {getSum = 21} > mconcat [Sum 5, Sum 6, Sum 10] Sum {getSum = 21} > getSum $ mconcat $ map Sum [5, 6, 10] 21 > getProduct $ mconcat $ map Product [5, 6, 10] 300 > mconcat ["5", "6", "10"] "5610" ์ด๊ฒŒ ์–ด๋””๊ฐ€ ์œ ์šฉํ•˜๋‹จ ๊ฑธ๊นŒ? ํ•จ์ˆ˜ ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ ์„ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ๋ณด์ž. threeConcat :: [a] -> [a] -> [a] -> [a] threeConcat a b c = a ++ b ++ c ์ด ํ•จ์ˆ˜๊ฐ€ ์ž„์˜์˜ ๋ชจ ๋…ธ์ด๋“œ์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋„๋ก ์ผ๋ฐ˜ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. threeConcat' :: Monoid m => m -> m -> m -> m threeConcat' a b c = a <> b <> c -- > threeConcat' "Hello" " " "world!" -- "Hello world!" -- > threeConcat' (Sum 5) (Sum 6) (Sum 10) -- Sum {getSum = 21} Data.Foldable์˜ fold :: (Foldable t, Monoid m) => t m -> m ๊ฐ™์€ ํƒ€ ํ•จ์ˆ˜๋“ค์€ ๋ชจ ๋…ธ์ด๋“œ์˜ ์„ฑ์งˆ์„ ์ด์šฉํ•˜์—ฌ ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ž„์˜์˜ ์ ‘๊ธฐ ๊ฐ€๋Šฅํ•œ ๊ตฌ์กฐ๋ฅผ ๋‹จ์ผ ๋ชจ๋…ธ์ด๋“œ์„ฑ ๊ฐ’์œผ๋กœ ํ™˜์›ํ•œ๋‹ค. > fold ["Hello", " ", "world!"] "Hello world!" > fold (Just (Sum 10)) Sum {getSum = 10} > fold Nothing :: Sum Integer Sum {getSum = 0} ์ด๋Ÿฐ ์‹์ด๋ผ๋ฉด, ์šฐ๋ฆฌ๋งŒ์˜ ๋ชจ๋…ธ์ด๋“œ์„ฑ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ํฌํ•จํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ(ํŠธ๋ฆฌ ๊ฐ™์€)๋ฅผ ๋งŒ๋“ค์—ˆ์„ ๋•Œ fold ๊ฐ™์€ ์ด๋ฏธ ์ •์˜๋œ ํ•จ์ˆ˜๋“ค์„ ์ฆ‰์‹œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ Integer์— ๋Œ€ํ•ด, ๋ง์…ˆ ๋Œ€์‹  ๊ณฑ์…ˆ์„ ํ•ฉ์„ฑ ์—ฐ์‚ฐ์œผ๋กœ ์“ฐ๋Š” ์ œ2์˜ Monoid ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. 1 mempty์™€ mappend๋ฅผ mconcat์„ ์ด์šฉํ•ด์„œ ์ •์˜ํ•˜๋Š” ๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ ์ •์˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ์—ฌ๋Ÿฌ๋ถ„์˜ ์ •์˜๊ฐ€ ์œ„์˜ ๋ฒ•์น™๋“ค์„ ์ž๋™์œผ๋กœ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๋ผ. ํ•˜์Šค ์ผˆ ์ •์˜์™€ ๋ฒ•์น™ Data.Monoid์˜ Monoid ํƒ€์ž… ํด๋ž˜์Šค๋Š” ์ด ์ผ๋ฐ˜ํ™” ๊ฐœ๋…์„ ํฌ์ฐฉํ•œ ๊ฒƒ์ด๋‹ค. class Monoid a where mempty :: a mappend :: a -> a -> a mconcat :: [a] -> a mconcat = foldr mappend mempty ์„ธ ๋ฒˆ์งธ ๋ฉ”์„œ๋“œ์ธ mconcat์€ ๊ธฐ๋ณธ ๊ตฌํ˜„์ธ mconcat = foldr (++) []๊ฐ€ ์ œ๊ณต๋˜๋Š”๋ฐ ๋ฆฌ์ŠคํŠธ์˜ concat๊ณผ ๋™๋“ฑํ•œ ๊ฒƒ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ์ธ์Šคํ„ด์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋œ๋‹ค. instance Monoid [a] where mempty = [] mappend = (++) ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ๋ง๋ถ™์ž„(appending)์„ ์ง€์›ํ•˜๋Š” ํƒ€์ž…์œผ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒŒ ์ ๋‹นํ•˜๋‹ค. (Monoid ์ •์˜๊ฐ€ ๊ทน๋„๋กœ ํฌ๊ด„์ ์ด๊ณ  ์ž๋ฃŒ๊ตฌ์กฐ์— ํ•œ์ •๋œ ๊ฒŒ ์•„๋‹ˆ๋ผ์„œ ์‹œ์  ํ—ˆ์šฉ์ด ํ•„์š”ํ•˜์ง€๋งŒ. ๊ทธ๋ž˜์„œ "๋ง๋ถ™์ž„"์€ ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ๋น„์œ ์ ์ธ ๋œป์ด ๋œ๋‹ค) ์˜ˆ๋ฅผ ๋“ค์–ด ์ •์ˆ˜๋Š” ๋ง์…ˆ์„ "๋ง๋ถ™์ž„"์œผ๋กœ์„œ, 0์„ ์˜์›์œผ๋กœ์„œ ๊ฐ€์ง€๋Š” ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. -- | Monoid under addition. newtype Sum a = Sum { getSum :: a } -- | Monoid under multiplication. newtype Product a = Product { getProduct :: a } instance Num a => Monoid (Sum a) where mempty = Sum 0 Sum x `mappend` Sum y = Sum (x + y) instance Num a => Monoid (Product a) where mempty = Product 1 Product x `mappend` Product y = Product (x * y) Monoid์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๊ฐ€ ๋”ฐ๋ผ์•ผ ํ•˜๋Š” ๋ฒ•์น™์€ ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์ค‘๋ฆฝ์›๊ณผ ๊ฒฐํ•ฉ ์„ฑ์งˆ์— ๋Œ€์‘ํ•œ๋‹ค. mempty <> x = x x <> mempty = x x <> (y <> z) = (x <> y) <> z Monoid ํด๋ž˜์Šค๋Š” Data.Monoid์— ๋งŽ์€ ๊ณต์šฉ ํƒ€์ž…๋“ค์— ๋Œ€ํ•œ ์ธ์Šคํ„ด์Šค์™€ ํ•จ๊ป˜ ์ •์˜๋˜์–ด ์žˆ๋‹ค. ์˜ˆ์‹œ ์ด๋ ‡๊ฒŒ ์ด๋ฆ„๋„ ์–ด๋ ค์šด๋ฐ ๋ชจ๋…ธ์ด๋“œ๋Š” ํ‰์ดํ•˜๊ณ  ์ง€๋ฃจํ•ด ๋ณด์ธ๋‹ค. ๊ทธ๋Ÿฐ ์ธ์ƒ์— ์ž˜๋ชป ์ด๋Œ๋ฆฌ์ง€ ์•Š๊ธฐ๋ฅผ. ๋ชจ๋…ธ์ด๋“œ๋Š” ์—ฌ๋Ÿฌ ํฅ๋ฏธ๋กœ์šด ์‘์šฉ๋ฒ•์ด ์žˆ๋‹ค. Writer ๋ชจ๋‚˜๋“œ Write w a ํƒ€์ž…์˜ ๊ณ„์‚ฐ(computation)์€ w ํƒ€์ž…์˜ ๋ˆ„์ ๋˜๋Š” ์ถœ๋ ฅ์„ ์ƒ์‚ฐํ•˜๋Š” ๋™์‹œ์— a ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. w๋Š” Monoid์˜ ์ธ์Šคํ„ด์Šค์ด๊ณ  Writer ๋ชจ๋‚˜๋“œ์˜ bind ์—ฐ์‚ฐ์ž๋Š” ์ถœ๋ ฅ์„ ๋ˆ„์ ํ•˜๊ธฐ ์œ„ํ•ด mappend๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. w๋ฅผ ์ผ์ข…์˜ ๊ธฐ๋ก์„ ์œ„ํ•ด ์“ฐ๋Š” ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ Monoid ํด๋ž˜์Šค์˜ ์ผ๋ฐ˜์„ฑ ๋•๋ถ„์— ์–ด๋–ค ๋ชจ ๋…ธ์ด๋“œ ์ธ์Šคํ„ด์Šค๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์„œ ๋งŽ์€ ๊ฐ€๋Šฅ์„ฑ์ด ์—ด๋ฆฐ๋‹ค. Foldable ํด๋ž˜์Šค Data.Foldable์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” Foldable์€ ๋ฆฌ์ŠคํŠธ ์ ‘๊ธฐ ํ•จ์ˆ˜๋ฅผ(๊ทธ๋ฆฌ๊ณ  Data.List์™€ ๊ด€๋ จ๋œ ๋งŽ์€ ๊ฒƒ์„) ํƒ€ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค๋กœ ์ผ๋ฐ˜ํ™”ํ•˜๋Š” ๊ฐ„ํŽธํ•œ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•œ๋‹ค. 2 Data.Foldable์˜ ๊ตฌํ˜„์€ ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ž๋ฃŒ๊ตฌ์กฐ ๋‚ด์˜ ์›์†Œ๋“ค๋กœ๋ถ€ํ„ฐ ๋ชจ๋…ธ์ด๋“œ์„ฑ ๊ฐ’์„ ์ƒ์„ฑํ•˜๊ณ , ๊ทธ๋Ÿฌ๊ณ  ๋‚˜๋ฉด mappend๋กœ ์‰ฝ๊ฒŒ ํ•ฉ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. Fodable์€ Traversable ํด๋ž˜์Šค(Data.Traversable์— ๋“ค์–ด์žˆ์Œ)์˜ ์ •์˜์—์„œ ๋˜ ๋‹ค๋ฅธ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. Traversable์€ Control.Monad์˜ sequence๋ฅผ ์ผ๋ฐ˜ํ™”ํ•œ๋‹ค. ํ•‘๊ฑฐ ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์—์„œ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ตฌํ˜„์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์˜ฎ๊ฒจ๋ณด๋ฉด, ๋ชจ๋…ธ์ด๋“œ๋Š” ํšจ์œจ์ ์ด๊ณ  ๋‹ค์žฌ๋‹ค๋Šฅํ•œ ํ•‘๊ฑฐ ํŠธ๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ๊ตฌํ˜„์€ ๋ชจ๋…ธ์ด๋“œ์„ฑ ๊ฐ’์„ ํŠธ๋ฆฌ ๋…ธ๋“œ์˜ ํƒœ๊ทธ๋กœ ํ™œ์šฉํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ˆ˜๋ฐ˜๋˜๋Š” Monoid ์ธ์Šคํ„ด์Šค๋งŒ ๊ต์ฒดํ•˜๋ฉด ์—ฌ๋Ÿฌ ์ž๋ฃŒ๊ตฌ์กฐ(์‹œํ€€์Šค, ์šฐ์„ ์ˆœ์œ„ ํ, ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ ๋“ฑ)๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. 3 ์˜ต์…˜๊ณผ ํ™˜๊ฒฝ์„ค์ • ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋ฌธ๋งฅ์—์„œ, ๋ชจ๋…ธ์ด๋“œ๋Š” ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ์˜ต์…˜๊ณผ ํ™˜๊ฒฝ์„ค์ •์„ ๋‹ค๋ฃจ๋Š” ํŽธ๋ฆฌํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ๋‘ ๊ฐ€์ง€ ์˜ˆ๋Š” ํ•˜์Šค์ผˆ์˜ ํŒจํ‚ค์ง• ์‹œ์Šคํ…œ์ธ Cabal("ํŒจํ‚ค์ง€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๋ชจ ๋…ธ์ด๋“œ๋‹ค. ์„ค์ • ํŒŒ์ผ์€ ๋ชจ ๋…ธ์ด๋“œ๋‹ค. ๋ช…๋ น ์ค„ ํ”Œ๋ž˜๊ทธ์™€ ๋ช…๋ ์ค„ ํ”Œ๋ž˜๊ทธ์˜ ์ง‘ํ•ฉ์€ ๋ชจ ๋…ธ์ด๋“œ๋‹ค. ํ•˜์Šค ์ผˆ ๋นŒ๋“œ ์ •๋ณด๋Š” ๋ชจ ๋…ธ์ด๋“œ๋‹ค."[1]) ๊ทธ๋ฆฌ๊ณ  ํ•˜์Šค ์ผˆ๋กœ ๊ตฌํ˜„ํ•œ ํƒ€์ผ๋ง ์œˆ๋„ ๋งค๋‹ˆ์ €์ธ XMonad("xmonad configuration hook์€ ๋ชจ๋…ธ์ด๋“œ์„ฑ์ด๋‹ค."[2])์ด๋‹ค. ๋™ํ˜•์‚ฌ์ƒ(Homomorphism) ๋‘ ๋ชจ ๋…ธ์ด๋“œ a์™€ b ์‚ฌ์ด์˜ ํ•จ์ˆ˜ f :: a -> b๊ฐ€ ๊ทธ ๋ชจ ๋…ธ์ด๋“œ ๊ตฌ์กฐ๋ฅผ ๋ณด์กดํ•œ๋‹ค๋ฉด, ์ด ํ•จ์ˆ˜๋ฅผ ๋™ํ˜•์‚ฌ์ƒ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ, f mempty = mempty f (x `mappend` y) = f x `mappend` f y ๊ฐ€๋ น length๋Š” ([],++)์™€ (Int,+) ์‚ฌ์ด์—์„œ ๋™ํ˜•์‚ฌ์ƒ์ด๋‹ค. length [] = 0 length (xs ++ ys) = length x + length y ๋ชจ ๋…ธ์ด๋“œ์™€ ๋™ํ˜•์‚ฌ์ƒ์˜ "์‹ค์ „์—์„œ ๋‚˜ํƒ€๋‚˜๋Š”" ํฅ๋ฏธ๋กœ์šด ์˜ˆ์‹œ๋ฅผ Google Protocol Buffers API ๋ฌธ์„œ์—์„œ Chris Kuklewicz๊ฐ€ ๋ฐœ๊ฒฌํ–ˆ๋‹ค. ์ด ๊ธ€์˜ ์ธ์šฉ๋ฌธ์— ๊ธฐ์ดˆํ•ด, (ํŒŒ์ด์ฌ์˜) ๋‹ค์Œ ์„ฑ์งˆ์„ ๋ณด์ž. MyMessage message; message.ParseFromString(str1 + str2); ์ด๋Š” ๋‹ค์Œ๊ณผ ๋™๋“ฑํ•œ๋ฐ MyMessage message, message2; message.ParseFromString(str1); message2.ParseFromString(str2); message.MergeFrom(message2); ParseFromString์ด ๋™ํ˜•์‚ฌ์ƒ์ž„์„ ๋œปํ•œ๋‹ค. ํ•˜์Šค์ผˆ๊ณผ ๋ชจ๋…ธ์ด๋“œ๋กœ ๋ฒˆ์—ญํ•ด ๋ณด๋ฉด, ๋‹ค์Œ ๋“ฑ์‹๋“ค์ด ์„ฑ๋ฆฝํ•œ๋‹ค. parse :: String -> Message -- these are just equations, not actual code. parse [] = mempty parse (xs ++ ys) = parse xs `merge` parse ys ํŒŒ์‹ฑ์€ ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์™„๋ฒฝํžˆ ์„ฑ๋ฆฝํ•˜์ง„ ์•Š์ง€๋งŒ, ๋Œ€๋žต ๊ทธ๋ ‡๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋” ์ฝ์„๊ฑฐ๋ฆฌ ๋ชจ ๋…ธ์ด๋“œ์— ๋Œ€ํ•œ Dan Pipone (Sigfpe)์˜ ๋ธ”๋กœ๊ทธ ๊ธ€ ๊ทธ๋ฆฌ๊ณ  ๊ฒฐํ•ฉ๋ฒ•์น™์˜ ๋ณธ์„ฑ์— ๊ด€ํ•œ ๊ฒฌํ•ด ๋ชจ ๋…ธ์ด๋“œ ๊ด€๋ จ ๋งํฌ๋“ค ํ•‘๊ฑฐ ํŠธ๋ฆฌ์— ๊ด€ํ•œ ์ถ”๊ฐ€ ๊ฒฌํ•ด: FingerTrees. Cabal์—์„œ์˜ Monoid ์‚ฌ์šฉ์— ๊ด€ํ•œ ์ถ”๊ฐ€ ๊ฒฌํ•ด: [3], [4]. ๋…ธํŠธ Data.Monoid์— ๋Œ€ํ•œ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด Num ํƒ€์ž…์— ๋Œ€ํ•ด ๋‘ ์ธ์Šคํ„ด์Šค ๋ชจ๋‘ ์ •์˜๋˜์–ด ์žˆ๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋‘˜ ๋‹ค newtype ๋ž˜ํผ๊ฐ€ ์žˆ์ง€๋งŒ. โ†ฉ ์ด๋Ÿฐ ์‹์˜ ์ ‘๊ธฐ๋Š” ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์—์„œ ๋…ผ์˜ํ•œ ๊ทธ ์ ‘๊ธฐ์™€ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์— ์œ ์˜ํ•˜๋ผ. โ†ฉ Ralf Hinze์™€ Ross PattersonRalf์˜ ๋…ผ๋ฌธ์— ๊ธฐ์ดˆํ•œ ์ด ๋ธ”๋กœ๊ทธ ๊ธ€์€ ๋ชจ ๋…ธ์ด๋“œ๊ฐ€ ํ•‘๊ฑฐ ํŠธ๋ฆฌ์—์„œ ์–ด๋–ป๊ฒŒ ์“ฐ์ด๋Š”๊ฐ€์— ๊ด€ํ•ด ๊ฐ„๊ฒฐํ•˜๊ณ ๋„ ์•Œ๊ธฐ ์‰ฌ์šด ์„ค๋ช…์„ ๋‹ด๊ณ  ์žˆ๋‹ค. โ†ฉ 02 ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Applicative_Functors ํŽ‘ ํ„ฐ ์ž‘์šฉ์„ฑ ํŽ‘ ํ„ฐ ์ธ์Šคํ„ด์Šค ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ์‚ฌ์šฉํ•˜๊ธฐ ๋ชจ๋‚˜๋“œ์™€ ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ZipLists ์ฐธ๊ณ  ์ž๋ฃŒ ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ(applicative functor)๋Š” ์ถ”๊ฐ€์ ์ธ ์„ฑ์งˆ์„ ๊ฐ€์ง„ ํŽ‘ํ„ฐ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ํŽ‘ ํ„ฐ ๋‚ด๋ถ€์˜ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค(๊ทธ๋ž˜์„œ ์ด๋ฆ„์ด ์ด๋ ‡๋‹ค). ํŽ‘ ํ„ฐ ํŽ‘ํ„ฐ๋“ค(์ฆ‰ Functor ํƒ€์ž… ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋“ค)์€ "์‚ฌ์ƒ(map) ํ•  ์ˆ˜ ์žˆ๋Š”" ๊ตฌ์กฐ์ฒด๋‹ค. ๋‹ค์Œ์€ Functor์˜ ํด๋ž˜์Šค ์ •์˜๋‹ค. class Functor f where fmap :: (a -> b) -> f a -> f b ๊ฐ€์žฅ ์ž˜ ์•Œ๋ ค์ง„ ํŽ‘ํ„ฐ๋Š” ๋ฆฌ์ŠคํŠธ๋กœ, fmap์ด map์— ์ƒ์‘ํ•œ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋Š” Maybe์ด๋‹ค. instance Functor Maybe where fmap f (Just x) = Just (f x) fmap _ Nothing = Nothing ํ†ต์ƒ์ ์œผ๋กœ ๋ชจ๋“  ํŠธ๋ฆฌ ๋น„์Šทํ•œ ๊ตฌ์กฐ์ฒด์— ๋Œ€ํ•ด Functor์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋‹ค์Œ ํƒ€์ž…๋“ค์— ๋Œ€ํ•ด Functor์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ •์˜ํ•˜๋ผ. ์žฅ๋ฏธ ๋‚˜๋ฌด ํƒ€์ž…์˜ Tree. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋œ๋‹ค. data Tree a = Node a [Tree a] ๊ณ ์ •๋œ e์— ๋Œ€ํ•œ Either e. ํ•จ์ˆ˜ ํƒ€์ž… ((->) t). ์ด ๊ฒฝ์šฐ f a๋Š” (t -> a)๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ์ž‘์šฉ์„ฑ ํŽ‘ ํ„ฐ class (Functor f) => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b pure ํ•จ์ˆ˜๋Š” ์ž„์˜ ๊ฐ’์„ ํŽ‘ ํ„ฐ ๋‚ด๋ถ€๋กœ ์ „์ด์‹œํ‚จ๋‹ค. (<*>)๋Š” ํŽ‘ ํ„ฐ ๋‚ด๋ถ€์˜ ํ•จ์ˆ˜๋ฅผ ํŽ‘ํ„ฐ์˜ ๊ฐ’์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ์ด ํŽ‘ํ„ฐ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฒ•์น™์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค. pure id <*> v = v -- ํ•ญ๋“ฑ ๋ฒ•์น™(Identity) pure (.) <*> u <*> v <*> w = u <*> (v <*> w) -- ๊ฒฐํ•ฉ๋ฒ•์น™(Composition) pure f <*> pure x = pure (f x) -- ๋™ํ˜•์‚ฌ์ƒ(Homomorphism) u <*> pure y = pure ($ y) <*> u -- ๊ตํ™˜๋ฒ•์น™(Interchange) ๊ทธ๋ฆฌ๊ณ  Functor ์ธ์Šคํ„ด์Šค๋Š” ๋‹ค์Œ ๋ฒ•์น™์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค. fmap f x = pure f <*> x -- Fmap ์ธ์Šคํ„ด์Šค Maybe์˜ Functor ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณธ ์ ์ด ์žˆ์œผ๋‹ˆ ์ด๋ฒˆ์—๋Š” Maybe๋ฅผ Applicative๋กœ ๋งŒ๋“ค์–ด๋ณด์ž. pure์˜ ์ •์˜๋Š” ์‰ฝ๋‹ค. Just์ด๋‹ค. (<*>)์˜ ์ •์˜๋Š” ์ด๋ ‡๋‹ค. ๋‘ ์ธ์ž ์ค‘ ํ•˜๋‚˜๋ผ๋„ Nothing ์ด๋ฉด ๊ฒฐ๊ณผ๋Š” Nothing์ด๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ ๋‘ Just ๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ ํ•จ์ˆ˜์™€ ๊ทธ ์ธ์ž๋ฅผ ๋ฝ‘์•„๋‚ด ์ธ์ž์— ๊ทธ ํ•จ์ˆ˜๊ฐ€ ์ ์šฉ๋œ Just๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. instance Applicative Maybe where pure = Just (Just f) <*> (Just x) = Just (f x) _ <*> _ = Nothing ์—ฐ์Šต๋ฌธ์ œ Applicative์˜ ๋ฒ•์น™์ด ์ด Maybe์— ๋Œ€ํ•œ ์ธ์Šคํ„ด์Šค์— ์„ฑ๋ฆฝํ•จ์„ ๋ณด์—ฌ๋ผ. ๋‹ค์Œ์— ๋Œ€ํ•œ Applicative ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ๊ณ ์ •๋œ e์— ๋Œ€ํ•œ Either e ๊ณ ์ •๋œ t์— ๋Œ€ํ•œ ((->) t) ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ์‚ฌ์šฉํ•˜๊ธฐ ๋‹น์žฅ์€ (<*>)๋ฅผ ์–ด๋””์— ์“ธ์ง€ ๋ช…ํ™•ํ•˜์ง€ ์•Š์œผ๋‹ˆ ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฒช์—ˆ์„ ์ˆ˜๋„ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์ž. ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ •ํ•˜์ž. f :: Int -> Int -> Int f x y = 2 * x + y ๊ทธ๋Ÿฐ๋ฐ Int๋“ค ๋Œ€์‹  Maybe Int ํƒ€์ž…์— ์ด ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ  ์‹ถ๋‹ค. ์ „์— ์ด ๋ฌธ์ œ๋ฅผ ๋ดค๊ธฐ ๋•Œ๋ฌธ์— fmap2๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ•œ๋‹ค. fmap2 :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c fmap2 f (Just x) (Just y) = Just (f x y) fmap2 _ _ _ = Nothing ์ž ๊น์€ ํ–‰๋ณตํ•˜์ง€๋งŒ ์ด๋Ÿฌ๋ฉด ์ธ์ž๊ฐ€ ๋งŽ์•„์ง„๋‹ค. ์–ด๋–ป๊ฒŒ ์ผ๋ฐ˜ํ™”ํ• ๊นŒ? (<*>)๊ฐ€ ํ•„์š”ํ•˜๋‹ค, fmap f๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์–ด๋–ค ์ผ์ด ๋ฒŒ์–ด์ง€๋Š”์ง€ ๋ณด์ž. f :: (a -> b -> c) fmap :: Functor f => (d -> e) -> f d -> f e fmap f :: Functor f => f a -> f (b -> c) -- Identify d with a, and e with (b -> c) ์ด์ œ (<*>)์˜ ์šฉ๋ฒ•์ด ๋ช…ํ™•ํ•˜๋‹ค. f (b -> c)๊ฐ€ ์žˆ์œผ๋ฉด (f b -> f c)๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์ด์— ๋”ํ•ด fmap2 f a b = f `fmap` a <*> b fmap3 f a b c = f `fmap` a <*> b <*> c fmap4 f a b c d = f `fmap` a <*> b <*> c <*> d ์ด๋ฅผ ๋” ๊น”์Œˆํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด Control.Applicative ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” (<$>)๋ฅผ fmap์˜ ๋™์˜์–ด๋กœ ์ •์˜ํ•œ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ฝ”๋“œ๋Š” ์ฝ๊ธฐ ๋” ๊น”๋”ํ•˜๊ณ  ์ ์šฉ, ์žฌ์‚ฌ์šฉํ•˜๊ธฐ๊ฐ€ ์‰ฌ์›Œ์ง„๋‹ค. fmap2 f a b = f <$> a <*> b fmap3 f a b c = f <$> a <*> b <*> c fmap4 f a b c d = f <$> a <*> b <*> c <*> d ๋‹ค๋ฅธ ๊ฐœ์ˆ˜์˜ ์ธ์ž๋“ค์„ ์ˆ˜์šฉํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๊ณ ์ฐจ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ํ•ด์•ผ ํ•  ๋•Œ๋ฉด Applicative์˜ ์ ๋‹นํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ •์˜ํ•ด์„œ ์—ฌ๋Ÿฌ๋ถ„์˜ ์‚ถ์„ ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ๋– ์˜ฌ๋ ค๋ผ. ๋ฌผ๋ก  Control.Applicative)๋Š” ์œ„์˜ ํ•จ์ˆ˜๋“ค์„ ํŽธ์˜๋ฅผ ์œ„ํ•ด liftA์™€ liftA3๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค. ๋ชจ๋‚˜๋“œ์™€ ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ pure์˜ ํƒ€์ž…์ด ์–ด๋”˜๊ฐ€ ์ต์ˆ™ํ•˜๋‹ค. ์ด๊ฒƒ์˜ ํƒ€์ž… ํด๋ž˜์Šค ์ œํ•œ์„ ์ด๊ฒƒ์œผ๋กœ๋ถ€ํ„ฐ Applicative f => a -> f a ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊พธ๋ฉด Monad m => a -> m a return๊ณผ ์ •ํ™•ํžˆ ๊ฐ™์€ ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค. ์‚ฌ์‹ค์€ Monad์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋Š” Applicative์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ •์˜๋“ค์ด๋‹ค. pure = return (<*>) = ap ์—ฌ๊ธฐ์„œ ap๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋œ๋‹ค. ap f a = do f' <- f a' <- a return (f' a') Control.Monad์—๋„ ์ •์˜๋˜์–ด ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ Maybe์˜ Applicative ์ธ์Šคํ„ด์Šค๋ฅผ ์œ„์˜ ๋ณ€ํ™˜์„ ํ†ตํ•ด ์–ป์„ ์ˆ˜ ์žˆ์Œ์„ ํ™•์ธํ•˜๋ผ. ZipLists Applicative๊ฐ€ ์‚ถ์„ ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์ค€๋‹ค๋Š” ๋ฐœ์ƒ์œผ๋กœ ๋Œ์•„์˜ค์ž. ์•„๋งˆ ๊ฐ€์žฅ ์ž˜ ์•Œ๋ ค์ง„ ์˜ˆ๊ฐ€ Data.List์˜ zipWIthN ํ•จ์ˆ˜๋“ค์ผ ๊ฒƒ์ด๋‹ค. ์ ์šฉ์„ฑ ํŽ‘ํ„ฐ๊ฐ€ ์œ ์šฉํ•  ๋งŒํ•œ ์ข…๋ฅ˜์˜ ํŒจํ„ด์„ ๋ณด์ธ๋‹ค. ๊ธฐ์ˆ ์ ์ธ ์ด์œ  ๋•Œ๋ฌธ์— []์˜ Applicative ์ธ์Šคํ„ด์Šค๋Š” ์ •์˜ํ•  ์ˆ˜ ์—†๋‹ค. ์ด๋ฏธ ์ •์˜๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ์ธ์Šคํ„ด์Šค๋Š” ์™„์ „ํžˆ ๋‹ค๋ฅธ ์ž‘์—…์„ ํ•œ๋‹ค. fs <*> xs๋Š” fs์˜ ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ์ทจํ•ด xs์˜ ๋ชจ๋“  ๊ฐ’์— ์ ์šฉํ•œ๋‹ค. ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ž˜ํผ๋ฅผ ๋งŒ๋“ ๋‹ค. newtype ZipList a = ZipList [a] instance Functor ZipList where fmap f (ZipList xs) = ZipList (map f xs) ์ด๊ฒƒ์„ ์˜ˆ์ƒ๋œ ํ–‰๋™์„ ํ•˜๋Š” Applicative์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค๋ ค๋ฉด ๋จผ์ € (<*>)์˜ ์ •์˜๋ฅผ ๋ด์•ผ ํ•œ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํ•˜๋ ค๋Š” ๊ฒƒ์œผ๋กœ๋ถ€ํ„ฐ ์ƒ๋‹นํžˆ ์ง๊ด€์ ์œผ๋กœ ๋”ฐ๋ผ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (<*>) ์—ฐ์‚ฐ์ž๋Š” ํ•จ์ˆ˜์˜ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ’์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด์„œ ํ•จ์ˆ˜๋“ค๊ณผ ๊ฐ’๋“ค์„ ์ง์ง€์–ด ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋งˆ์น˜ zipWith ($)์ฒ˜๋Ÿผ ๋“ค๋ฆฐ๋‹ค. ZipList ๋ž˜ํผ๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค. instance Applicative ZipList where (ZipList fs) <*> (ZipList xs) = ZipList $ zipWith ($) fs xs pure = undefined ์ด์ œ pure๋งŒ ์ •์˜ํ•˜๋ฉด ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ ์ •์˜ํ•˜๋ฉด pure x = ZipList [x] ํ•ญ๋“ฑ ๋ฒ•์น™ pure id <*> v = v์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š”๋‹ค. v๊ฐ€ ํ•˜๋‚˜๋ณด๋‹ค ๋งŽ์€ ์›์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  zipWith๋Š” ๋‘ ์ž…๋ ฅ ์ค‘ ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฒƒ์˜ ๋ฆฌ์ŠคํŠธ๋งŒ์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. v๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์›์†Œ๋ฅผ ๊ฐ€์งˆ์ง€ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์žฅ ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ•์€ pure๊ฐ€ ๋ฌดํ•œ ์›์†Œ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ์ด์ œ ์šฐ๋ฆฌ์˜ ์ธ์Šคํ„ด์Šค ์„ ์–ธ์ด ์™„์„ฑ๋˜์—ˆ๋‹ค. instance Applicative ZipList where (ZipList fs) <*> (ZipList xs) = ZipList (zipWith ($) fs xs) pure x = ZipList (repeat x) ์ฐธ๊ณ  ์ž๋ฃŒ Control.Applicative 03 Foldable ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Foldable foldr ํ•ด๋ถ€ Foldable ํด๋ž˜์Šค ๋ฆฌ์ŠคํŠธ ๊ฐ™์€ folding Foldable์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์‚ฌ์‹ค๋“ค ๋…ธํŠธ Foldable ํƒ€์ž… ํด๋ž˜์Šค๋Š” ๋ฆฌ์ŠคํŠธ ์ ‘๊ธฐ(foldr ๋ฅ˜)์™€ ๊ทธ๋กœ๋ถ€ํ„ฐ ํŒŒ์ƒ๋œ ์—ฐ์‚ฐ๋“ค์„ ์ž„์˜ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์ผ๋ฐ˜ํ™”ํ•œ ๊ฒƒ์ด๋‹ค. Foldable์€ ๋งค์šฐ ์œ ์šฉํ•˜๋ฉฐ ๋˜ํ•œ ๋ชจ ๋…ธ์ด๋“œ๊ฐ€ ์ข‹์€ ์ถ”์ƒํ™”์˜ ๊ณต์‹ํ™”๋ฅผ ์–ด๋–ป๊ฒŒ ๋•๋Š”์ง€ ๋ณด์—ฌ์ฃผ๋Š” ํ›Œ๋ฅญํ•œ ์˜ˆ์‹œ๋‹ค. foldr ํ•ด๋ถ€ foldr์€ ์–ด์ง€๊ฐ„ํžˆ ๋ณต์žกํ•œ ํ•จ์ˆ˜๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ•จ์ˆ˜ ํ™”์‚ดํ‘œ์˜ ์–‘์ชฝ์—๋Š” ์ดํ•ญ ํ•จ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉฐ ๊ฐ๊ฐ์˜ ํƒ€์ž…๋“ค์€ ๋‘ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. foldr :: (a -> b -> b) -> b -> [a] -> b foldr์„ ์ผ๋ฐ˜ํ™”ํ•˜๋ ค๋ฉด ์ข€ ๋” ๋‹ค๋ฃจ๊ธฐ ์‰ฝ๊ฑฐ๋‚˜, ์ตœ์†Œํ•œ ๊ฐ„๋‹จํ•œ ์„ฑ๋ถ„๋“ค๋กœ ๋ถ„ํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์žˆ์œผ๋ฉด ์ข€ ๋” ํŽธ๋ฆฌํ•  ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ ์„ฑ๋ถ„๋“ค์€ ์–ด๋–ค ๋ชจ์Šต์ผ๊นŒ? ๋Œ€๊ฐ• ์„ค๋ช…ํ•˜์ž๋ฉด ๋ฆฌ์ŠคํŠธ ์ ‘๊ธฐ๋Š” ๋ฆฌ์ŠคํŠธ ์š”์†Œ๋“ค์„ ํ›‘์–ด์„œ ์ดํ•ญ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•ฉ์„ฑํ•œ๋‹ค. ์šฐ๋ฆฌ๋Š” ์˜จ์ „ํžˆ ๊ฐ’์˜ ์ง์„ ํ•ฉ์„ฑํ•˜๋Š” ๊ฒƒ์— ๊ด€ํ•œ ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ์•Œ๊ณ  ์žˆ๋‹ค. ๋ฐ”๋กœ Monoid๋‹ค. ๋‹ค์Œ์˜ fold r z์—์„œ a `f` (b `f` (c `f` z)) -- foldr f z [a, b, c] f = (<>) ๊ทธ๋ฆฌ๊ณ  (z = mempty)์œผ๋กœ ๋ฐ”๊พธ๋ฉด a <> (b <> (c <> mempty)) -- foldr (<>) mempty [a, b, c] mconcat = foldr mappend mempty์„ ์–ป๋Š”๋‹ค. foldr๋ณด๋‹ค ๊ฐ„๋‹จํ•˜๋ฉฐ ๊ตฌ์ฒด์ ์ด๋‹ค. ํ•ฉ์„ฑ ํ•จ์ˆ˜์™€ ์ดˆ๊ธฐ ๋ˆ„์ ๊ฐ’์„ ๋ช…์‹œํ•  ํ•„์š”๋„ ์—†๋‹ค. mappend(์ฆ‰ (<>))์™€ mempty๋ฅผ ์ผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. mconcat :: Monoid m => [m] -> m mconcat์€ ๋ชจ๋“  ์›์†Œ๋ฅผ ํ•ฉ์„ฑํ•œ๋‹ค๋Š” foldr์˜ ๊ฐœ๋…์„ ์ž˜ ํฌ์ฐฉํ•˜๋ฉฐ foldr์˜ ๋ช‡ ๊ฐ€์ง€ ์šฉ๋ฒ•์„ ํฌํ•จํ•œ๋‹ค. GHCi> mconcat ["Tree", "fingers"] -- concat "Treefingers" ํ›Œ๋ฅญํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” Monoid ์ธ์Šคํ„ด์Šค๋งŒ ์ ‘๋Š” ๊ฒƒ์œผ๋กœ ๋๋‚ด๊ณ  ์‹ถ์ง€ ์•Š๋‹ค. ์›์†Œ๋“ค์„ ์–ด๋–ค Monoid ํƒ€์ž…์œผ๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด, mconcat์„ ์‚ฌ์šฉํ•ด ์ž„์˜ ํƒ€์ž…์˜ ์›์†Œ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ ‘์„ ์ˆ˜ ์žˆ์Œ์„ ๊นจ๋‹ซ๋Š”๋‹ค๋ฉด ์ด ์ƒํ™ฉ์„ ํƒ€๊ฐœํ•  ์ˆ˜ ์žˆ๋‹ค. foldMap :: Monoid m => (a -> m) -> [a] -> m foldMap g = mconcat . fmap g ์ด๋Ÿฌ๋ฉด ์ƒํ™ฉ์ด ์ข€ ๋” ํฅ๋ฏธ๋กœ์›Œ์ง„๋‹ค. GHCi> foldMap Sum [1.. 10] Sum {getSum = 55} ์ง€๊ธˆ๊นŒ์ง„ ์ข‹๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ ์ž„์˜์˜ ํ•ฉ์„ฑ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ ‘๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ foldr ์‹œ๊ทธ๋„ˆ์ฒ˜์— ๋งž๋Š” ๋ชจ๋“  ์ดํ•ญ ํ•จ์ˆ˜๋Š” ๊ฐ’์„ Monoid ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ์“ธ ์ˆ˜ ์žˆ๋‹ค! ๋น„๊ฒฐ์€ foldr์— ์ „๋‹ฌ๋˜๋Š” ํ•ฉ์„ฑ ํ•จ์ˆ˜๋ฅผ ๋‹จํ•ญ ํ•จ์ˆ˜๋กœ์„œ ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. foldr :: (a -> (b -> b)) -> b -> [a] -> b ๊ทธ๋ฆฌ๊ณ  b -> b ๊ผด ํ•จ์ˆ˜๊ฐ€ ํ•ฉ์„ฑ ํ•˜์—์„œ (.)๋Š” mappend, id๋Š” mempty๋กœ์„œ ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ด์šฉํ•œ๋‹ค. 1 ๋Œ€์‘ํ•˜๋Š” Monoid ์ธ์Šคํ„ด์Šค๋Š” Data.Monoid์˜ Endo ๋ž˜ํผ๋ฅผ ํ†ตํ•ด ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.2 newtype Endo b = Endo { appEndo :: b -> b } instance Monoid Endo where mempty = Endo id Endo g `mappend` Endo f = Endo (g . f) ์ด์ œ ๋‹ค์Œ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. foldComposing :: (a -> (b -> b)) -> [a] -> Endo b foldComposing f = foldMap (Endo . f) ์ด ํ•จ์ˆ˜๋Š” b -> b ํ•จ์ˆ˜๋ฅผ ๊ฐ ์›์†Œ์— ์ ์šฉํ•ด ์ „๋ถ€ ํ•ฉ์„ฑํ•œ๋‹ค. Endo (f a) <> (Endo (f b) <> (Endo (f c) <> (Endo id))) -- foldComposing f [a, b, c] Endo (f a. (f b. (f c. id))) -- (<>) and (.) are associative, so we don't actually need the parentheses. -- As an example, here is a step-by-step evaluation: foldComposing (+) [1, 2, 3] foldMap (Endo . (+)) [1, 2, 3] mconcat (fmap (Endo . (+)) [1, 2, 3]) mconcat (fmap Endo [(+1), (+2), (+3)]) mconcat [Endo (+1), Endo (+2), Endo (+3)] Endo ((+1) . (+2) . (+3)) Endo (+6) ์ด ํ•จ์ˆ˜๋ฅผ ์ž„์˜์˜ b ๊ฐ’์— ์ ์šฉํ•˜๋ฉด... foldr :: (a -> (b -> b)) -> b -> [a] -> b foldr f z xs = appEndo (foldComposing f xs) z foldr์„ ๋‹ค์‹œ ์–ป๊ฒŒ ๋œ๋‹ค. ์ฆ‰ foldMap์œผ๋กœ foldr์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. foldMap์ด ๋” ๊ฐ„๋‹จํ•˜๊ณ  ๋”ฐ๋ผ์„œ ์ดํ•ดํ•˜๊ธฐ ๋” ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์— foldMap์€, foldr์„ ์ž„์˜์˜ ์ž๋ฃŒ ๊ตฌ์กฐ๋กœ ์ผ๋ฐ˜ํ™”ํ•˜๋Š” Foldable ํด๋ž˜์Šค์˜ ํ•ต์‹ฌ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ foldMap์˜ ๋‘ ๊ฐ€์ง€ ๊ตฌํ˜„์„ ์ž‘์„ฑํ•˜๋ผ. ํ•˜๋‚˜๋Š” foldr์„ ์ด์šฉํ•˜์—ฌ, ํ•˜๋‚˜๋Š” ๋ช…์‹œ์ ์ธ ์žฌ๊ท€๋งŒ์„ ์‚ฌ์šฉํ•˜๋ผ. Foldable ํด๋ž˜์Šค ์–ด๋–ค ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•œ Foldable์„ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด foldMap ๋˜๋Š” foldr ์ค‘ ํ•˜๋‚˜๋งŒ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค. ํ•˜์ง€๋งŒ Foldable์—๋Š” ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๊ฐ€ ๋งŽ์ด ์žˆ๋‹ค. -- ๋ฉ”์„œ๋“œ ์‹œ๊ทธ๋„ˆ์ฒ˜๋งŒ ์žˆ๋Š” ์š”์•ฝ๋œ ์ •์˜ class Foldable t where foldMap :: Monoid m => (a -> m) -> t a -> m foldr :: (a -> b -> b) -> b -> t a -> b -- ๋ชจ๋‘ ๊ธฐ๋ณธ ๊ตฌํ˜„์ด ์žˆ๋‹ค fold :: Monoid m => t m -> m -- ์ผ๋ฐ˜ํ™”๋œ mconcat foldr' :: (a -> b -> b) -> b -> t a -> b foldl :: (b -> a -> b) -> b -> t a -> b foldl' :: (b -> a -> b) -> b -> t a -> b foldr1 :: (a -> a -> a) -> t a -> a foldl1 :: (a -> a -> a) -> t a -> a toList :: t a -> [a] null :: t a -> Bool length :: t a -> Int elem :: Eq a => a -> t a -> Bool maximum :: Ord a => t a -> a minimum :: Ord a => t a -> a sum :: Num a => t a -> a product :: Num a => t a -> a ๋ถ€๊ฐ€ ๋ฉ”์„œ๋“œ๋“ค์€ ํ•„์š”ํ•˜๋ฉด ๋” ํšจ์œจ์ ์ธ ๊ตฌํ˜„์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋˜์–ด์žˆ๋‹ค. ์–ด์จŒ๋“  foldMap ๋˜๋Š” foldr๋งŒ ์ž‘์„ฑํ•˜๋ฉด ์œ„์— ๋‚˜์—ด๋œ ์œ ์šฉํ•œ ํ•จ์ˆ˜๋“ค์„ ๋ชจ๋‘ ๊ณต์งœ๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์‹ฌ์ง€์–ด Data.Foldable์€ ๋ชจ๋“  Foldable์— ๋Œ€ํ•ด ์ผ๋ฐ˜ํ™”๋œ mapM_, traverse_ ๋“ฑ์˜ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. ๋‹ค์Œ์€ Foldable์— ๋Œ€ํ•œ ์งง์€ ๋ฐ๋ชจ๋กœ์„œ Data.Map์„ ์ด์šฉํ•œ ๊ฒƒ์ด๋‹ค.3 GHCi> import qualified Data.Map as M GHCi> let testMap = M.fromList $ zip [0..] ["Yesterday","I","woke","up","sucking","a","lemon"] GHCi> length testMap GHCi> sum . fmap length $ testMap 29 GHCi> elem "lemon" testMap True GHCi> foldr1 (\x y -> x ++ (' ' : y)) testMap -- Be careful: foldr1 is partial! "Yesterday I woke up sucking a lemon" GHCi> import Data.Foldable GHCi> traverse_ putStrLn testMap Yesterday woke up sucking lemon ์œ ์šฉํ•œ ์ผ๋ฐ˜ํ™”๋ฅผ ๋„˜์–ด์„œ Foldable๊ณผ foldMap์€ fold์— ๋Œ€ํ•ด ๋” ์„ ์–ธ์ ์œผ๋กœ ์ƒ๊ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ•œ๋‹ค. ๊ฐ€๋ น sum์„ ๋ฆฌ์ŠคํŠธ(ํŠธ๋ฆฌ๋‚˜ ๊ทธ ์™ธ ์–ด๋–ค ์ž๋ฃŒ๊ตฌ์กฐ๊ฑด)๋ฅผ ํ›‘์œผ๋ฉฐ ์›์†Œ๋“ค์„ (+)๋กœ ๋ˆ„์ ํ•˜๋Š” ํ•จ์ˆ˜๋กœ ๋ณด๋Š” ๋Œ€์‹ , sum์ด ๊ฐ๊ฐ์˜ ์›์†Œ์˜ ๊ฐ’์„ ์งˆ์˜ํ•˜๊ณ  ์งˆ์˜์˜ ๊ฒฐ๊ณผ๋“ค์„ Sum ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ์ด์šฉํ•ด ์š”์•ฝํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์†Œํ•œ ์ฐจ์ด๋กœ ๋ณด์ด๊ฒ ์ง€๋งŒ ๋ชจ ๋…ธ์ด๋“œ ๊ด€์ ์€ fold๊ฐ€ ์ˆ˜๋ฐ˜๋œ ๋ฌธ์ œ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์–ด๋–ค ์ข…๋ฅ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋Š”๊ฐ€์™€ fold ๋˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์„ธ๋ถ€์‚ฌํ•ญ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์ƒํ™ฉ์„ ๋ช…๋ฃŒํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋ชจ ๋…ธ์ด๋“œ ํŒŒํ—ค์น˜๊ธฐ ๋†€์ด! ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๊ฐ ํ•จ์ˆ˜์— ๋Œ€ํ•ด mempty, mappend, ๊ทธ๋ฆฌ๊ณ  ํ•„์š”ํ•˜๋‹ค๋ฉด fold๋‚˜ foldMap์œผ๋กœ ๊ตฌํ˜„๋ , ๊ฐ’๋“ค์„ ์ค€๋น„ํ•˜๋Š” ํ•จ์ˆ˜์˜ ์กฐํ•ฉ์„ ์ œ์•ˆํ•˜๋ผ. newtype ์ธ์Šคํ„ด์Šค๋กœ ๊ณจ์น˜๋ฅผ ์•“์„ ํ•„์š”๋Š” ์—†๋‹ค. (์—ฌ๋Ÿฌ๋ถ„์˜ ํ•ด๋‹ต์„ foldMap์œผ๋กœ ์‹œํ—˜ํ•ด ๋ณผ ์ƒ๊ฐ์ด ์•„๋‹ˆ๋ผ๋ฉด) ์˜ˆ๋ฅผ ๋“ค์–ด "mempty๋Š” 0์ด๊ณ  mappend๋Š” (+)์ด๋‹ค"๋ผ๋Š” sum์— ๋Œ€ํ•œ ๋‹ต์œผ๋กœ ์™„๋ฒฝํžˆ ์ˆ˜์šฉ๋œ๋‹ค. ํ•„์š”ํ•˜๋‹ค๋ฉด ํ•จ์ˆ˜๋“ค์„ ๋ถ€๋ถ„ ์ ์šฉํ•˜๊ณ  ๊ณต๊ธ‰๋œ ์ธ์ž๋“ค์„ ํ•ด๋‹ต์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“  ์งˆ๋ฌธ์— id์™€ (.)์œผ๋กœ ๋‹ตํ•˜์ง€ ๋ง ๊ฒƒ - ๊ผผ์ˆ˜๋Š” ์•ˆ ๋œ๋‹ค! ํžŒํŠธ: Data.Monoid์˜ Monoid ์ธ์Šคํ„ด์Šค๋“ค์„ ๋ณผ ๊ฒƒ. a. product :: (Foldable t, Num a) => t a -> a b. concat :: Foldable t => t [a] -> [a] c. concatMap :: Foldable t => (a -> [b]) -> t a -> [b] d. all :: Foldable t => (a -> Bool) -> t a -> Bool e. elem :: Eq a => a -> t a -> Bool f. length :: t a -> Int g. traverse_ :: (Foldable t, Applicative f) => โ€Œ(a -> f b) -> t a -> f () h. mapM_ :: (Foldable t, Monad m) => โ€Œ(a -> m b) -> t a -> m () i. safeMaximum :: Ord a => t a -> Maybe a (like maximum, but handling emptiness.) j. find :: Foldable t => (a -> Bool) -> t a -> Maybe a k. composeL :: Foldable t => (b -> a -> b) -> t a -> b -> b (foldl๊ณผ ๋™์ผ) ๋ฆฌ์ŠคํŠธ ๊ฐ™์€ folding Foldable์—๋Š” toList :: Foldable t => t a -> [a]์ด ๋“ค์–ด์žˆ๋‹ค. ์ฆ‰ ์–ด๋–ค Foldable ์ž๋ฃŒ ๊ตฌ์กฐ๋“  ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ๊ทธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ ‘์œผ๋ฉด ์›๋ž˜ ๊ตฌ์กฐ๋ฅผ ์ง์ ‘ ์ ‘๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค. foldMap์„ ์ด์šฉํ•œ toList ๊ตฌํ˜„์€ ์•„๋งˆ ์ด๋Ÿด ๊ฒƒ์ด๋‹ค. 4 toList = foldMap (\x -> [x]) toList๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ•˜์Šค ์ผˆ ํƒ€์ž…์— ๋Œ€ํ•œ ์ž์œ  ๋ชจ๋…ธ์ด๋”๋ผ๋Š” ์‚ฌ์‹ค์„ ๋ฐ˜์˜ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ "์ž์œ "๋Š” ์–ด๋–ค ๊ฐ’์ด๋“  ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์—†์• ์ง€ ์•Š๊ณ  ๋ชจ๋…ธ์ด๋“œ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋œปํ•œ๋‹ค. 5 (์šฐ๋ฆฌ๋Š” a ํƒ€์ž…์˜ ๊ฐ’์„ ์›์†Œ๊ฐ€ ํ•˜๋‚˜์ธ [a] ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  (\x->[x])์™€ head๋กœ ์†์‹ค ์—†์ด ๋˜๋Œ๋ฆด ์ˆ˜ ์žˆ๋‹ค) ์ด์™€ ๊ด€๋ จ๋œ ์ค‘์š”ํ•œ ํŠน์„ฑ์€ toList์— ์˜ํ•ด ๋ช…๋ฐฑํ•ด์ง„๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ toList = id์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋ฉด -- Given a list xs :: [a] xsAsFoldMap :: Monoid m => (a -> m) -> m xsAsFoldMap = \f -> foldMap f xs xsAsFoldMap์— (x -> [x])๋ฅผ ๋„˜๊ฒจ ํ•ญ์ƒ ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ xs๋ฅผ ๋ณต๊ตฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋Ÿฐ ์‹์ด๋ฉด ๋ฆฌ์ŠคํŠธ๋Š” ๊ทธ๊ฒƒ์˜ ์˜ค๋ฅธ์ชฝ ์ ‘๊ธฐ์™€ ๋™์น˜๊ฐ€ ๋œ๋‹ค. ์ฆ‰ Foldable ์—ฐ์‚ฐ์„ ํ†ตํ•œ ์ ‘๊ธฐ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฆฌ์ŠคํŠธ๋ณด๋‹ค ๋ณต์žกํ•˜๋ฉด ์–ด์ฉ” ์ˆ˜ ์—†์ด ์†์‹ค ์—ฐ์‚ฐ์ด ๋œ๋‹ค. ๋‹ฌ๋ฆฌ ๋ณด๋ฉด Foldable์ด ์ œ๊ณตํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅ˜์˜ ์ ‘๊ธฐ๋Š” ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์—์„œ ๋ณธ ๊ฒƒ๊ณผ ๊ฐ™์€ ์ ‘๊ธฐ(๊ณต์‹ ์šฉ์–ด๋Š” catamorphism)๋ณด๋‹ค ๋œ ์ผ๋ฐ˜์ ์ด๊ณ , ๊ธฐ์กด ๊ตฌ์กฐ๋ฅผ ์žฌ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์—์„œ ์‚ฌ์šฉํ•œ ํŠธ๋ฆฌ ํƒ€์ž…์— ๋Œ€ํ•ด data Tree a = Leaf a | Branch (Tree a) (Tree a) a. Tree์— ๋Œ€ํ•œ Foldable ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. b. treeDepth :: Tree a -> Int๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์ž. ๋ฃจํŠธ์—์„œ ๊ฐ€์žฅ ๋จผ ์žŽ๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๊ฐ€์ง€์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. Foldable ๋˜๋Š” ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์—์„œ ์ •์˜ํ•œ treeFold๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์ž. ๋‘ ๋ฐฉ๋ฒ•์ด ๋ชจ๋‘ ๊ฐ€๋Šฅํ• ๊นŒ? Foldable์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์‚ฌ์‹ค๋“ค Foldable์€ ์›์น™์— ์ž…๊ฐ์ด๊ณ  ๋ฒ”์šฉ์ ์ด์–ด์„œ, ๊ทธ ์ž์ฒด๋กœ๋Š” ๋ชฉ์ ์ด ์—†๋Š” ํ•˜์Šค ์ผˆ ํด๋ž˜์Šค๋“ค ์‚ฌ์ด์—์„œ ์กฐ๊ธˆ ์ด๋ก€์ ์ด๋‹ค. ๊ฐ€์žฅ ๋น„์Šทํ•œ ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ฑ์งˆ๋กœ, ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ๋ฒ•์น™์ด ์•„๋‹ˆ๋‹ค(์–ด๋–ค ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด์„œ๋„ ์„ฑ๋ฆฝํ•˜๊ธฐ ๋•Œ๋ฌธ์—). ์–ด๋–ค ๋ชจ ๋…ธ์ด๋“œ ๋™ํ˜•์‚ฌ์ƒ g์— ๋Œ€ํ•ด foldMap (g . f) = g. foldMap f foldMap (g . f)๋ฅผ g. foldMap f๋กœ ๋ฐ”๊พธ๋ฉด g๋ฅผ ์ž๋ฃŒ๊ตฌ์กฐ ๋‚ด์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์›์†Œ์— ๋Œ€ํ•ด์„œ๊ฐ€ ์•„๋‹ˆ๋ผ ์ ‘๊ธฐ์˜ ๊ฒฐ๊ณผ์—๋งŒ ์ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด์ต์ด๋‹ค. Foldable ๊ตฌ์กฐ๊ฐ€ Functor์ด๊ธฐ๋„ ํ•˜๋‹ค๋ฉด, ๋‹ค์Œ๋„ ์ž๋™์œผ๋กœ ์„ฑ๋ฆฝํ•œ๋‹ค. foldMap f = fold . fmap f ...๊ทธ๋ฆฌ๊ณ  ํŽ‘ ํ„ฐ ์ œ2 ๋ฒ•์น™๊ณผ ์œ„์˜ ์„ฑ์งˆ์„ ์ ์šฉํ•˜๋ฉด ๋‹ค์Œ์„ ์–ป๋Š”๋‹ค. foldMap g. fmap f = foldMap (g . f) = g. foldMap f foldMap ๊ฐ™์€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ์–ด๋–ค Foldable ํƒ€์ž…์ด๋“  Functor ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ ธ์•ผ ํ•จ์„ ์•”์‹œํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ, Functor๊ฐ€ Foldable์˜ ์ƒ์œ„ ํด๋ž˜์Šค์ธ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์–ด๋–ค Foldable ์ธ์Šคํ„ด์Šค๊ฐ€ ์–ด๋–ค ์ด์œ ์—์„œ๊ฑด Functor๊ฐ€ ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ฐ€์žฅ ํ”ํ•œ ์˜ˆ๊ฐ€ Data.Set์˜ sets๋‹ค. ์ด๋“ค ์ง‘ํ•ฉ์˜ ์›์†Œ ํƒ€์ž…์€ Ord์˜ ์ธ์Šคํ„ด์Šค์—ฌ์•ผ ํ•˜๊ณ , ๋”ฐ๋ผ์„œ ์ด๊ฒƒ๋“ค์˜ map ํ•จ์ˆ˜๋Š” fmap์œผ๋กœ ์“ฐ์ผ ์ˆ˜ ์—†๋‹ค. fmap์€ ๋ถ€์ˆ˜์ ์ธ ํด๋ž˜์Šค ์ œํ•œ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ Data.Set.Set์€ ์œ ์šฉํ•œ Foldable ์ธ์Šคํ„ด์Šค๋‹ค. GHCi> import qualified Data.Set as S GHCi> let testSet = S.fromList [1,3,2,5,5,0] GHCi> testSet fromList [0,1,2,3,5] GHCi> import Data.Foldable GHCi> toList testSet [0,1,2,3,5] GHCi> foldMap show testSet "01235" ์—ฐ์Šต๋ฌธ์ œ a. ์ง์— ๋Œ€ํ•œ ๋ชจ ๋…ธ์ด๋“œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. (Monoid a, Monoid b) => Monoid (a, b) b. fst์™€ snd๊ฐ€ ๋ชจ ๋…ธ์ด๋“œ ๋™ํ˜•์‚ฌ์ƒ์ž„์„ ์ฆ๋ช…ํ•ด ๋ณด์ž. c. foldMap์˜ ๋ชจ ๋…ธ์ด๋“œ ๋™ํ˜•์‚ฌ์ƒ ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ์„ ์ฆ๋ช…ํ•ด ๋ณด์ž. foldMap f &&& foldMap g = foldMap (f &&& g) ์—ฌ๊ธฐ์„œ f &&& g = \x -> (f x, g x) ์ด ์—ฐ์Šต๋ฌธ์ œ๋Š” Edward Kmett์ด ์ œ์•ˆํ•œ ๊ฒƒ์ด๋‹ค. ๋…ธํŠธ ์ด ๊ธฐ๋ฒ•์€ ๊ณ ์ฐจ ํ•จ์ˆ˜ ๋๋ถ€๋ถ„์—์„œ foldl์— ๊ด€ํ•œ ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ดค๋‹ค๋ฉด ์ต์ˆ™ํ•  ๊ฒƒ์ด๋‹ค. โ†ฉ "Endo"๋Š” "๋‚ด๋ถ€ ์‚ฌ์ƒ endomorphism"์˜ ์ค„์ž„๋ง๋กœ, ํ•œ ํƒ€์ž…์—์„œ ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ์‚ฌ์ƒํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ผ์ปซ๋Š” ์€์–ด๋‹ค. โ†ฉ Data.Map๊ณผ ๋‹ค๋ฅธ ์œ ์šฉํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ ๊ตฌํ˜„์— ๋Œ€ํ•œ ์ •๋ณด๋Š” data structures primer๋ฅผ ๋ณด์ž. โ†ฉ Data.Foldable์€ ์ˆ˜ํ–‰๋Šฅ๋ ฅ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ ๊ตฌํ˜„์ด ์ด์™€ ๋‹ค๋ฅด๋‹ค. โ†ฉ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž์œ  ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค๊ณ  ๋งํ•  ๋•Œ๋Š” ๋น„์ข…๊ฒฐ(non-termination)์— ๊ด€ํ•ด ์ฃผ์˜ํ•  ์ ์ด ํ•˜๋‚˜ ์žˆ๋‹ค. Dan Doel์ด ์“ด Free Monoids in Haskell๋ฅผ ์ฝ์–ด๋ณด์ž. (์ด ๊ธ€์˜ ๋…ผ์˜๋Š” ๊ฝค ์ „๋ฌธ์ ์ด๋ฏ€๋กœ ์ง€๊ธˆ ๋ง‰ Foldable์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค๋ฉด ๊ทธ๋ฆฌ ์žฌ๋ฐŒ์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.) โ†ฉ ์†Œ์Šค(ํ•˜์Šค ์ผˆ ์นดํŽ˜) 1 โ†ฉ 04 Traversable ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Traversable ์ˆœํšŒ๋ฅผ ์œ„ํ•œ Functor๋“ค Traversable์˜ ์—ฌ๋Ÿฌ ํ•ด์„ Traversable ๋ฒ•์น™๋“ค fmap๊ณผ foldMap ๋˜์‚ด๋ฆฌ๊ธฐ Prelude์—๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ํƒ€์ž… ํด๋ž˜์Šค๊ฐ€ 5๊ฐœ ์žˆ๋Š”๋ฐ, Functor, Applicative, Monad, Foldable๋Š” ์ด๋ฏธ ์•Œ์•„๋ดค๋‹ค. ๋งˆ์ง€๋ง‰์€ Traversable์ด๋‹ค.1 ์ˆœํšŒ(traverse)๋Š” ๊ฑธ์–ด ์ง€๋‚˜๊ฐ„๋‹ค๋Š” ๋œป์ด๊ณ , Traversable์€ ๋ฐ”๋กœ ์ด ์ผ์„ ์ผ๋ฐ˜ํ™”ํ•œ๋‹ค. ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ๊ฑธ์–ด ์ง€๋‚˜๊ฐ€์„œ, ๋ฉˆ์ถœ ๋•Œ๋งˆ๋‹ค ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ˆ˜์ง‘ํ•œ๋‹ค. ์ˆœํšŒ๋ฅผ ์œ„ํ•œ Functor๋“ค ์ˆœํšŒ๊ฐ€ ๊ฑธ์–ด ์ง€๋‚˜๊ฐ€๊ธฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค๋ฉด ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ ์ˆœํšŒ๋ฅผ ํ•˜๊ณ  ์žˆ๋˜ ์…ˆ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ Functor์™€ Foldable ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณด์ž. instance Functor [] where fmap _ [] = [] fmap f (x:xs) = f x : fmap f xs instance Foldable [] where foldMap _ [] = mempty foldMap f (x:xs) = f x <> foldMap f xs fmap f๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฑธ์–ด ์ง€๋‚˜๊ฐ€ ๊ฐ ์›์†Œ์— f๋ฅผ ์ ์šฉํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„ ๋ฆฌ์ŠคํŠธ๋ฅผ ์žฌ๊ตฌ์ถ•ํ•œ๋‹ค. ๋น„์Šทํ•˜๊ฒŒ foldMap f๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฑธ์–ด ์ง€๋‚˜๊ฐ€ f๋ฅผ ๊ฐ ์›์†Œ์— ์ ์šฉํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„ mappend๋กœ ๊ฒฐํ•ฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ Functor์™€ Foldable๋งŒ์œผ๋กœ๋Š” ๋ชจ๋“  ์œ ์šฉํ•œ ์ˆœํšŒ ๋ฐฉ๋ฒ•์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†๋‹ค. ์Œ์ˆ˜๋ฅผ ๊ฒ€์‚ฌํ•ด์„œ Maybe๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ๋ณด์ž. deleteIfNegative :: (Num a, Ord a) => a -> Maybe a deleteIfNegative x = if x < 0 then Nothing else Just x ์ด deleteIfNegative์„ ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์“ฐ๋ ค๊ณ  ํ•œ๋‹ค. rejectWithNegatives :: (Num a, Ord a) => [a] -> Maybe [a] ์ด ํ•จ์ˆ˜๋Š” ์Œ์ˆ˜๊ฐ€ ํ•˜๋‚˜๋„ ์—†์œผ๋ฉด ์›๋ž˜ ๋ฆฌ์ŠคํŠธ๋ฅผ Just๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์•„๋‹ˆ๋ฉด Nothing์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. Foldable์ด๋‚˜ Functor๋Š” ๋„์›€์ด ๋˜์ง€ ์•Š๋Š”๋‹ค. Foldable์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ์˜ ๊ตฌ์กฐ๋ฅผ, ์šฐ๋ฆฌ๊ฐ€ ์ ‘๊ธฐ๋ฅผ ์œ„ํ•ด ์„ ํƒํ•œ Monoid์˜ ๊ตฌ์กฐ๋กœ ๋Œ€์ฒดํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๊ฒƒ์„ ์›๋ž˜ ๋ฆฌ์ŠคํŠธ๋‚˜ Nothing์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋ฐฉ๋ฒ•์ด ์—†๋‹ค. 2 Functor์˜ ๊ฒฝ์šฐ fmap์œผ๋กœ ๋  ๊ฒƒ ๊ฐ™์ง€๋งŒ... GHCi> let testList = [-5,3,2, -1,0] GHCi> fmap deleteIfNegative testList [Nothing, Just 3, Just 2, Nothing, Just 0] ์ด๋Ÿฌ๋ฉด Maybe๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์˜ Maybe ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. ์ž˜ ๋ณด๋ฉด ์ ‘๊ธฐ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ๊ฐ’๋“ค์„ ๊ฒฐํ•ฉํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ํŒŒ๊ดดํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๊ฐ’๋“ค์˜ Maybe ๋ฌธ๋งฅ๋“ค์„ ๊ฒฐํ•ฉํ•ด ๊ทธ ๊ฒฐํ•ฉ ๋ฌธ๋งฅ ์•ˆ์—์„œ ๋ฆฌ์ŠคํŠธ ๊ตฌ์กฐ๋ฅผ ์žฌ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค. ๋‹คํ–‰ํžˆ๋„ Functor ๋ฌธ๋งฅ์„ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ํƒ€์ž… ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค. ๋ฐ”๋กœ Applicative๋‹ค. 3 Applicative๋Š” ์šฐ๋ฆฌ์—๊ฒŒ ํ•„์š”ํ•œ ํด๋ž˜์Šค์ธ Traversable๋ฅผ ์ด๋Œ์–ด๋‚ธ๋‹ค. instance Traversable [] where -- sequenceA :: Applicative f => [f a] -> f [a] sequenceA [] = pure [] sequenceA (u:us) = (:) <$> u <*> sequenceA us -- ๋˜๋Š” ๋‹ค์Œ๊ณผ ๋™์น˜: instance Traversable [] where sequenceA us = foldr (\u v -> (:) <$> u <*> v) (pure []) us Traversable๊ณผ Applicative ๋ฌธ๋งฅ์˜ ๊ด€๊ณ„๋Š” Foldable๊ณผ Monoid ๊ฐ’์˜ ๊ด€๊ณ„์™€ ๋น„์Šทํ•˜๋‹ค. ์ด๋Ÿฐ ๊ด€์ ์—์„œ sequenceA๋Š” fold์™€ ์œ ์‚ฌํ•˜๋‹ค. sequenceA๋Š” ํ•œ ๊ตฌ์กฐ ๋‚ด์—์„œ ๋ฌธ๋งฅ์˜ ์ ์šฉ ํ•ฉ์‚ฐ(applicative summary)์„ ์ƒ์„ฑํ•˜๊ณ , ๊ทธ ๊ตฌ์กฐ๋ฅผ ์ƒˆ๋กœ์šด ๋ฌธ๋งฅ์—์„œ ์žฌ๊ตฌ์ถ•ํ•œ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๋˜ ๊ทธ ํ•จ์ˆ˜๋‹ค. GHCi> let rejectWithNegatives = sequenceA . fmap deleteIfNegative GHCi> :t rejectWithNegatives rejectWithNegatives :: (Num a, Ord a, Traversable t) => t a -> Maybe (t a) GHCi> rejectWithNegatives testList Nothing GHCi> rejectWithNegatives [0.. 10] Just [0,1,2,3,4,5,6,7,8,9,10] ๋‹ค์Œ์€ Traversable์˜ ๋ฉ”์„œ๋“œ ๋“ค์ด๋‹ค. class (Functor t, Foldable t) => Traversable t where traverse :: Applicative f => (a -> f b) -> t a -> f (t b) sequenceA :: Applicative f => t (f a) -> f (t a) -- These methods have default definitions. -- They are merely specialised versions of the other two. mapM :: Monad m => (a -> m b) -> t a -> m (t b) sequence :: Monad m => t (m a) -> m (t a) sequenceA๊ฐ€ fold์™€ ๋น„์Šทํ•˜๋‹ค๋ฉด traverse๋Š” foldMap๊ณผ ๋น„์Šทํ•˜๋‹ค. ์ด ๋‘˜์€ ์„œ๋กœ๋ฅผ ์ด์šฉํ•ด ์ •์˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ Traversable์˜ ์ตœ์†Œํ•œ์˜ ๊ตฌํ˜„์€ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ์œผ๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค. traverse f = sequenceA . fmap f sequenceA = traverse id Traversable์˜ ๋ฆฌ์ŠคํŠธ ์ธ์Šคํ„ด์Šค๋ฅผ traverse๋ฅผ ์ด์šฉํ•ด ๋‹ค์‹œ ์ž‘์„ฑํ•ด ๋ณด๋ฉด Functor์™€ Foldable์˜ ์œ ์‚ฌ์„ฑ์ด ๋” ๋ช…๋ฐฑํ•ด์ง„๋‹ค. instance Traversable [] where traverse _ [] = pure [] traverse f (x:xs) = (:) <$> f x <*> traverse f xs -- ๋˜๋Š” ๋‹ค์Œ๊ณผ ๋™์น˜: instance Traversable [] where traverse f xs = foldr (\x v -> (:) <$> f x <*> v) (pure []) xs ๋ณดํ†ต์€ Traversable์„ ๊ตฌํ˜„ํ•  ๋•Œ traverse๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์€๋ฐ, traverse์˜ ๊ธฐ๋ณธ ์ •์˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋‘ ๋ฒˆ ๊ฑธ์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (ํ•œ ๋ฒˆ์€ fmap์„ ์œ„ํ•ด, ํ•œ ๋ฒˆ์€ sequenceA๋ฅผ ์œ„ํ•ด) traverse๋ฅผ ์ด์šฉํ•ด rejectWithNegatives๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. rejectWithNegatives :: (Num a, Ord a, Traversable t) => t a -> Maybe (t a) rejectWithNegatives = traverse deleteIfNegative ์—ฐ์Šต๋ฌธ์ œ ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์˜ Tree์— ๋Œ€ํ•œ Traversable ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•˜๋ผ. Tree์˜ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. data Tree a = Leaf a | Branch (Tree a) (Tree a) Traversable์˜ ์—ฌ๋Ÿฌ ํ•ด์„ ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ์„ ํƒํ•œ ์ ์šฉ์„ฑ ํŽ‘ํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ Traversable ๊ตฌ์กฐ์ฒด๋“ค์„ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. traverse์˜ ํƒ€์ž…์€ ์ด๋ ‡๋‹ค. traverse :: (Applicative f, Traversable t) => (a -> f b) -> t a -> f (t b) ์—ฌํƒ€ ํด๋ž˜์Šค์—์„œ ๋ดค๋˜ ์‚ฌ์ƒ ํ•จ์ˆ˜๋“ค์˜ ํƒ€์ž…๊ณผ ๋‹ฎ์•˜๋‹ค. fmap์€ ํ•จ์ˆ˜ ์ธ์ž๋ฅผ ์ด์šฉํ•ด ์›๋ž˜ ๊ตฌ์กฐ์— ํŽ‘ ํ„ฐ ๋ฌธ๋งฅ์„ ์‚ฝ์ž…ํ•˜๊ณ , (>>=)๋Š” ๊ตฌ์กฐ ์ž์ฒด๋ฅผ ์ˆ˜์ •ํ•˜์ง€๋งŒ, traverse๋Š” ๊ทธ ๊ตฌ์กฐ์˜ ์œ„์— ์ƒˆ๋กœ์šด ๋ฌธ๋งฅ์ธต์„ ๋”ํ•œ๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด traverse๋Š” ์˜ํ–ฅ ์žˆ๋Š” ์ˆœํšŒ, ์ฆ‰ ์ข…ํ•ฉ์ ์ธ ๊ฒฐ๊ณผ(๋ฌธ๋งฅ์˜ ์ƒˆ๋กœ์šด ๋ฐ”๊นฅ ๋ ˆ์ด์–ด)๋ฅผ ์‚ฐ์ถœํ•˜๋Š” ์ˆœํšŒ๋ฅผ ๊ฐ€๋Šฅ์ผ€ ํ•œ๋‹ค. ์ƒˆ๋กœ์šด ๋ ˆ์ด์–ด์˜ ๊ธฐ์ €์— ์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ์™„์ „ํžˆ ๋ณต๊ตฌ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด, ๊ทธ ๊ตฌ์กฐ๋Š” ์›๋ž˜ ๊ตฌ์กฐ์™€ ์ผ์น˜ํ•  ๊ฒƒ์ด๋‹ค(๋ฌผ๋ก  ๊ฐ’๋“ค์€ ๋ฐ”๋€” ์ˆ˜ ์žˆ๋‹ค). ๋‹ค์Œ์€ ์ค‘์ฒฉ ๋ฆฌ์ŠคํŠธ์˜ ์˜ˆ์‹œ๋‹ค. GHCi> traverse (\x -> [0.. x]) [0.. 3] [[0,0,0,0],[0,0,0,1],[0,0,0,2],[0,0,0,3],[0,0,1,0],[0,0,1,1] ,[0,0,1,2],[0,0,1,3],[0,0,2,0],[0,0,2,1],[0,0,2,2],[0,0,2,3] ,[0,1,0,0],[0,1,0,1],[0,1,0,2],[0,1,0,3],[0,1,1,0],[0,1,1,1] ,[0,1,1,2],[0,1,1,3],[0,1,2,0],[0,1,2,1],[0,1,2,2],[0,1,2,3] ] ์•ˆ์ชฝ์˜ ๋ฆฌ์ŠคํŠธ๋“ค์€ ์›๋ž˜ ๋ฆฌ์ŠคํŠธ์˜ ๊ตฌ์กฐ๋ฅผ ์œ ์ง€ํ•˜์—ฌ, ๋ชจ๋‘ ์›์†Œ๋ฅผ ๋„ค ๊ฐœ์”ฉ ๊ฐ€์ง„๋‹ค. ๋ฐ”๊นฅ ๋ฆฌ์ŠคํŠธ๋Š” ์ƒˆ๋กœ์šด ๋ ˆ์ด์–ด๋กœ, ๊ฐ๊ฐ์˜ ์›์†Œ๊ฐ€ 0๊ณผ ์›๋ž˜ ๊ฐ’ ์‚ฌ์ด์— ์žˆ๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜์—ฌ ๋น„๊ฒฐ์ •์„ฑ์„ ๋„์ž…ํ•œ๋‹ค. sequenceA๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ฌธ๋งฅ์„ ๋ถ„๋ฐฐํ•˜๋Š”์ง€์— ์ดˆ์ ์„ ๋งž์ถ”์–ด Traversable์„ ์ดํ•ดํ•  ์ˆ˜๋„ ์žˆ๋‹ค. GHCi> sequenceA [[1,2,3,4],[5,6,7]] [[1,5],[1,6],[1,7],[2,5],[2,6],[2,7] ,[3,5],[3,6],[3,7],[4,5],[4,6],[4,7] ] ์ด ์˜ˆ์ œ์—์„œ sequenceA๊ฐ€ ์ด์ „์˜ ๋ฐ”๊นฅ ๊ตฌ์กฐ๋ฅผ ์ƒˆ๋กœ์šด ๊ตฌ์กฐ ๋‚ด๋ถ€์— ๋ถ„๋ฐฐํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ƒˆ๋กœ์šด ์•ˆ์ชฝ ๋ฆฌ์ŠคํŠธ๋“ค์€ ์ด์ „์˜ ๋ฐ”๊นฅ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ 2๊ฐœ ์›์†Œ๋ฅผ ๊ฐ€์ง„๋‹ค. ์ƒˆ๋กœ์šด ๋ฐ”๊นฅ ๊ตฌ์กฐ๋Š” ์›์†Œ๊ฐ€ 12๊ฐœ์ธ ๋ฆฌ์ŠคํŠธ๋กœ, ์›์†Œ๊ฐ€ 4๊ฐœ์ธ ๋ฆฌ์ŠคํŠธ์™€ 3๊ฐœ์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ (<*>)๋กœ ๊ฒฐํ•ฉํ•  ๋•Œ ์˜ˆ์ƒ๋˜๋Š” ๊ทธ๋Œ€๋กœ๋‹ค. ๋ถ„๋ฐฐ๋ผ๋Š” ๊ด€์ ์˜ ํฅ๋ฏธ๋กœ์šด ์ธก๋ฉด ํ•˜๋‚˜๋Š” ์™œ ์–ด๋–ค ํŽ‘ํ„ฐ๋“ค์€ Traversable์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋Š”๊ฐ€๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. (ํ•˜๋‚˜์˜ IO ์•ก์…˜์ด๋‚˜ ํ•จ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ๋ถ„๋ฐฐํ•˜๊ฒ ๋Š”๊ฐ€?) ์—ฐ์Šต๋ฌธ์ œ ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ์žฅ์„ ์ˆ™์ง€ํ•˜๋ฉด ๋‹ค์Œ ์—ฐ์Šต๋ฌธ์ œ๋“ค์„ ํ’€ ๋•Œ ๋„์›€์ด ๋  ๊ฒƒ์ด๋‹ค. ํ–‰๋ ฌ์„ ์ค‘์ฒฉ ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•ด ๋ณด์ž. ์•ˆ์ชฝ ๋ฆฌ์ŠคํŠธ๋“ค์ด ํ–‰์ด ๋œ๋‹ค. Traversable์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ์„ ๊ตฌํ˜„ํ•ด ๋ณด์ž. transpose :: [[a]] -> [[a]] ์ด ํ•จ์ˆ˜๋Š” ํ–‰๋ ฌ์„ ์ „์น˜ํ•œ๋‹ค. (์ฆ‰ ํ–‰๊ณผ ์—ด์„ ๋งž๋ฐ”๊พผ๋‹ค) ์ด ์—ฐ์Šต๋ฌธ์ œ์—์„œ ํ–‰๋“ค์˜ ๊ธธ์ด๊ฐ€ ๋ชจ๋‘ ๋‹ค๋ฅธ "๊ฐ€์งœ ํ–‰๋ ฌ"์€ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค. 2. traverse mappend๊ฐ€ ํ•˜๋Š” ์ผ์„ ์„ค๋ช…ํ•˜๋ผ. 3. ์ ์šฉ์„ฑ ํŽ‘ ํ„ฐ ์ง‘์ค‘ ์กฐ๋ช… ์‹œ๊ฐ„์ด๋‹ค. mapAccumL :: Traversable t =>โ€Œ (a -> b -> (a, c)) -> a -> t b -> (a, t c) ์ด ํƒ€์ž…์—์„œ ๋ฌด์–ธ๊ฐ€๊ฐ€ ๋– ์˜ค๋ฅด๋Š”๊ฐ€? ์ ์ ˆํ•œ Applicative๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๊ฒƒ์„ Traversable๋กœ ๊ตฌํ˜„ํ•ด ๋ณด์ž. ์ฒจ์–ธํ•˜์ž๋ฉด ๋‹ค์Œ์€ Data.Traversable ๋ฌธ์„œ์˜ mapAccumL ์„ค๋ช…์ด๋‹ค. mapAcuumL ํ•จ์ˆ˜๋Š” fmap๊ณผ foldl์„ ๊ฒฐํ•ฉํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ฐ ์›์†Œ์— ์ ์šฉํ•˜๊ณ  ๋ˆ„์  ์ธ์ž๋ฅผ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋„˜๊ฒจ ์ตœ์ข… ๋ˆ„์ ๊ฐ’์„ ์ƒˆ๋กœ์šด ์ž๋ฃŒ๊ตฌ์กฐ์™€ ํ•จ๊ป˜ ๋ฐ˜ํ™˜ํ•œ๋‹ค. Traversable ๋ฒ•์น™๋“ค ํ•ฉ๋ฆฌ์ ์ธ Traversable ์ธ์Šคํ„ด์Šค๋Š” ์ผ๋ จ์˜ ๋ฒ•์น™์„ ๋งŒ์กฑํ•œ๋‹ค. ๋‹ค์Œ์€ ๊ทธ์ค‘ ๋‘ ๊ฐ€์ง€๋‹ค. traverse Identity = Identity -- ํ•ญ๋“ฑ ๋ฒ•์น™ traverse (Compose . fmap g. f) = Compose . fmap (traverse g) . traverse f -- ๊ฒฐํ•ฉ๋ฒ•์น™ ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ๋ฒ•์น™์ด ๋”ธ๋ ค ๋‚˜์˜จ๋‹ค. -- If t is an applicative homomorphism, then t. traverse f = traverse (t . f) -- naturality ์ด ๋ฒ•์น™๋“ค์ด ์ฉ ์ž๋ช…ํ•˜์ง€๋Š” ์•Š์œผ๋‹ˆ ์ž์„ธํžˆ ์‚ดํŽด๋ณด์ž. ๋งˆ์ง€๋ง‰ ๋ฒ•์น™๋ถ€ํ„ฐ ๋ณด๋ฉด ์ ์šฉ์„ฑ ๋™ํ˜•์‚ฌ์ƒ(applicative homomorphism)์€ Applicative ์—ฐ์‚ฐ๋“ค์„ ๋ณด์กดํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. -- Given a choice of f and g, and for any a, t :: (Applicative f, Applicative g) => f a -> g a t (pure x) = pure x t (x <*> y) = t x <*> t y ์ด ์ •์˜๊ฐ€ ์ „์— ๋ณธ ๋ชจ ๋…ธ์ด๋“œ ๋™ํ˜•์‚ฌ์ƒ๊ณผ ๋น„์Šทํ•˜๊ธฐ๋„ ํ•˜๊ฑฐ๋‹ˆ์™€ naturality ๋ฒ•์น™์ด Foldable์— ๋Œ€ํ•œ ์žฅ์—์„œ ๋ดค๋˜ foldMap๊ณผ ๋ชจ ๋…ธ์ด๋“œ ๋™ํ˜•์‚ฌ์ƒ์— ๋Œ€ํ•œ ์„ฑ์งˆ์„ ์ •ํ™•ํžˆ ๋ฐ˜์˜ํ•˜๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. ํ•ญ๋“ฑ ๋ฒ•์น™์€ Identity๋ผ๋Š” ๋”๋ฏธ ํŽ‘ํ„ฐ๋ฅผ ์ˆ˜๋ฐ˜ํ•œ๋‹ค. newtype Identity a = Identity { runIdentity :: a } instance Functor Identity where fmap f (Identity x) = Identity (f x) instance Applicative Identity where pure x = Identity x Identity f <*> Identity x = Identity (f x) ์ด ๋ฒ•์น™์ด ๋งํ•˜๋Š” ๋ฐ”๋Š”, Identity ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ์ˆœํšŒ๊ฐ€ ํ•˜๋Š” ์ผ์ด๋ผ๊ณ ๋Š” ๊ทธ ๊ตฌ์กฐ๋ฅผ Identity๋กœ ๊ฐ์‹ธ๋Š” ๊ฒƒ๋ฟ์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์‹ค์ƒ ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋Š” ์…ˆ์ด๋‹ค. (runIdentity๋กœ ์›๋ž˜ ๊ตฌ์กฐ๋ฅผ ์ž๋ช…ํ•˜๊ฒŒ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ) Identity ์ƒ์„ฑ์ž๋Š” ๋”ฐ๋ผ์„œ ํ•ญ๋“ฑ ์ˆœํšŒ๊ณ , ์‚ฌ์‹ค ๋งค์šฐ ํ•ฉ๋ฆฌ์ ์ด๋‹ค. ๊ฒฐํ•ฉ ๋ฒ•์น™์€ Compose ํŽ‘ํ„ฐ๋ฅผ ์ด์šฉํ•ด ์„œ์ˆ ๋œ๋‹ค. newtype Compose f g a = Compose { getCompose :: f (g a) } instance (Functor f, Functor g) => Functor (Compose f g) where fmap f (Compose x) = Compose (fmap (fmap f) x) instance (Applicative f, Applicative g) => Applicative (Compose f g) where pure x = Compose (pure (pure x)) Compose f <*> Compose x = Compose ((<*>) <$> f <*> x) Compose๋Š” ํŽ‘ํ„ฐ๋“ค์˜ ํ•ฉ์„ฑ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋‘ Functor๋ฅผ ํ•ฉ์„ฑํ•˜๋ฉด ํ•˜๋‚˜์˜ Functor๊ฐ€ ๋˜๊ณ , ๋‘ Applicative๋ฅผ ํ•ฉ์„ฑํ•˜๋ฉด ํ•˜๋‚˜์˜ Applicative๊ฐ€ ๋œ๋‹ค 4. ๊ทธ ์ธ์Šคํ„ด์Šค๋“ค์€ ์ž๋ช…ํ•œ ๊ฒƒ๋“ค๋กœ, ํŽ‘ ํ„ฐ ๋ ˆ์ด์–ด๋ฅผ ๋‚ด๋ ค๊ฐ€๋ฉฐ ๋ฉ”์„œ๋“œ๋“ค์„ ์ด์–ด์ค€๋‹ค. ๊ฒฐํ•ฉ๋ฒ•์น™์€ ์šฐ๋ฆฌ๊ฐ€ ๋‘ ์ˆœํšŒ๋ฅผ ๋ณ„๊ฐœ๋กœ ํ•˜๋˜์ง€(ํ•ญ๋“ฑ์‹์˜ ์šฐ๋ณ€), ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•˜๊ธฐ ์œ„ํ•ด ํ•ฉ์„ฑํ•˜๋˜์ง€(์ขŒ๋ณ€) ์ƒ๊ด€์—†์Œ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ด๋Š” ํŽ‘ ํ„ฐ ์ œ2 ๋ฒ•์น™๊ณผ ๋น„์Šทํ•˜๋‹ค. fmap์ด ํ•„์š”ํ•œ ์ด์œ ๋Š” ๋‘ ๋ฒˆ์งธ ์ˆœํšŒ๊ฐ€(๋˜๋Š” ์ขŒ๋ณ€์—์„œ๋Š” ์ˆœํšŒ์˜ ๋‘ ๋ฒˆ์งธ ๋ถ€๋ถ„) ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์— ์˜ํ•ด ์ถ”๊ฐ€๋œ ๊ตฌ์กฐ์˜ ๊ณ„์ธต ๋ฐ‘์—์„œ ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. Compose๋Š” ํ•ฉ์„ฑ๋œ ์ˆœํšŒ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ณ„์ธต์— ์ ์šฉ๋˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•˜๋‹ค. Identity์™€ Compose๋Š” ๊ฐ๊ฐ Data.Functor.Identity์™€ Data.Functor.Compose์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฒ•์น™๋“ค์„ sequenceA๋ฅผ ์ด์šฉํ•ด ๊ณต์‹ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. sequenceA . fmap Identity = Identity -- identity sequenceA . fmap Compose = Compose . fmap sequenceA . sequenceA -- composition -- For any applicative homomorphism t: t. sequenceA = sequenceA . fmap t -- naturality ๋‹น์žฅ ์™€๋‹ฟ์ง€๋Š” ์•Š์ง€๋งŒ 5, ์ˆœํšŒ์˜ ๋ช‡ ๊ฐ€์ง€ ์œ ์ตํ•œ ํŠน์ง•์ด ์ด ๋ฒ•์น™๋“ค๋กœ๋ถ€ํ„ฐ ๋‚˜์˜จ๋‹ค. ์ˆœํšŒ๋Š” ์›์†Œ๋“ค์„ ๊ฑด๋„ˆ๋›ฐ์ง€ ์•Š๋Š”๋‹ค. ์ˆœํšŒ๋Š” ๊ฐ ์›์†Œ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ๋ฐฉ๋ฌธํ•œ๋‹ค. traverse pure = pure ์ˆœํšŒ๋Š” ๊ธฐ์กด ๊ตฌ์กฐ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค(๊ธฐ์กด ๊ตฌ์กฐ๋Š” ๋ณด์กด๋˜๊ฑฐ๋‚˜ ์™„์ „ํžˆ ํŒŒ๊ดด๋œ๋‹ค). fmap๊ณผ foldMap ๋˜์‚ด๋ฆฌ๊ธฐ ์•„์ง Traversable์˜ Functor์™€ Foldable ํด๋ž˜์Šค ์ œ์•ฝ์„ ์ •๋‹นํ™”ํ•˜์ง€ ์•Š์•˜๋‹ค. ์ด์œ ๋Š” ์•„์ฃผ ๊ฐ„๋‹จํ•˜๋‹ค. Traversable ์ธ์Šคํ„ด์Šค๊ฐ€ ์œ„์˜ ๋ฒ•์น™๋“ค์„ ๋งŒ์กฑํ•˜๋Š” ํ•œ fmap๊ณผ foldMap์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ traverse๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค. fmap์˜ ๊ฒฝ์šฐ ์šฐ๋ฆฌ๊ฐ€ ํ•  ์ผ์€ ์ˆœํšŒ๋ฅผ ์ž„์˜์˜ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด Identity๋ฅผ ์“ฐ๋Š” ๊ฒƒ๋ฟ์ด๋‹ค. fmap f = runIdentity . traverse (Identity . f) foldMap์„ ์‚ด๋ฆฌ๋ ค๋ฉด ์„ธ ๋ฒˆ์งธ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํŽ‘ ํ„ฐ์ธ Const(Control.Applicative์— ํฌํ•จ)๋ฅผ ๋„์ž…ํ•ด์•ผ ํ•œ๋‹ค. newtype Const a b = Const { getConst :: a } instance Functor (Const a) where fmap _ (Const x) = Const x Const๋Š” ์ƒ์ˆ˜ ํŽ‘ํ„ฐ๋‹ค. Const a b ํƒ€์ž…์˜ ๊ฐ’์€ b ๊ฐ’์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹  fmap์˜ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š” a ๊ฐ’์„ ๋ณด๊ด€ํ•œ๋‹ค. ์šฐ๋ฆฌ์˜ ํ˜„ ๋ชฉ์ ์—์„œ๋Š” ์ง„์งœ๋กœ ํฅ๋ฏธ๋กœ์šด ์ธ์Šคํ„ด์Šค๋Š” Applicative์˜ ๊ฒƒ์ด๋‹ค. instance Monoid a => Applicative (Const a) where pure _ = Const mempty Const x <*> Const y = Const (x `mappend` y) (<*>)๋Š” ๊ฐ ๋ฌธ๋งฅ์˜ ๊ฐ’์„ mappend๋กœ ํ•ฉ์„ฑํ•œ๋‹ค 6. ์ด๋ฅผ ์ด์šฉํ•ด ์ˆœํšŒ๋ฅผ ์ž„์˜์˜ Monoid m => a -> m ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด foldMap์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„์˜ ์ธ์Šคํ„ด์Šค ๋•์— ์ˆœํšŒ๋Š” ์ ‘๊ธฐ๊ฐ€ ๋œ๋‹ค. foldMap f = getConst . traverse (Const . f) traverse๋กœ๋ถ€ํ„ฐ ๊ฒ‰์œผ๋กœ๋Š” ์™„์ „ํžˆ ๋‹ฌ๋ผ ๋ณด์ด๋Š” ๋‘ ํ•จ์ˆ˜๋ฅผ ๋ณต๊ตฌํ•ด๋ƒˆ๊ณ , ์šฐ๋ฆฌ๊ฐ€ ํ•œ ์ผ์ด๋ผ๊ณ ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํŽ‘ ํ„ฐ ๋‘ ๊ฐœ๋ฅผ ๊ณจ๋ž์„ ๋ฟ์ด๋‹ค. ์ถ”์ƒํ™” ํŽ‘ํ„ฐ๋Š” ์ด๋ ‡๊ฒŒ ๊ฐ•๋ ฅํ•˜๋‹ค 7. ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด GHC Prelude์˜ ๋‹ค์„ฏ ํด๋ž˜์Šค๋ผ๊ณ  ํ•ด์•ผ ํ•œ๋‹ค. Haskell Report์— ๋”ฐ๋ฅด๋ฉด Applicative, Foldable, Traversable์€ Prelude์— ๊ณต์‹์ ์œผ๋กœ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ ์‹œ๊ฐ„๋ฌธ์ œ์ผ ๋ฟ์ด๋‹ค. โ†ฉ Data.Monoid์˜ Monoid a => Monoid (Maybe a) ์ธ์Šคํ„ด์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ? ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ํ•ด๋ณด๋ฉด ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์—†๋‹ค. โ†ฉ Applicative์˜ ๋ชจ ๋…ธ์ด๋“œ ํ‘œํ˜„์„ ๋ณด๋ฉด ๋ช…ํ™•ํ•ด์ง„๋‹ค. โ†ฉ ํ•˜์ง€๋งŒ ๋‘ ๋ชจ๋‚˜๋“œ์˜ ํ•ฉ์„ฑ์ด ๊ผญ ๋ชจ๋‚˜๋“œ๋Š” ์•„๋‹˜์„ ๊ธฐ์–ตํ•˜๋ผ. โ†ฉ ๊ธฐ์ˆ ์ ์ธ ์„ธ๋ถ€์‚ฌํ•ญ์€ Data.Traversable ๋ฌธ์„œ์— ์ธ์šฉ๋œ ๋…ผ๋ฌธ๋“ค์„ ํ™•์ธํ•  ๊ฒƒ. โ†ฉ ์ด๋Š” Applicative๊ฐ€ ๋ฌธ๋งฅ๋“ค์„ ์–ด๋–ป๊ฒŒ ๋ชจ๋…ธ์ด๋“œ์ ์œผ๋กœ ํ•ฉ์„ฑํ•˜๋Š”์ง€๋ฅผ ํ›Œ๋ฅญํžˆ ์„ค๋ช…ํ•œ๋‹ค. ๋ฌธ๋งฅ ๋‚ด์˜ ๊ฐ’๋“ค์„ ์ œ๊ฑฐํ•˜๋ฉด applicative ๋ฒ•์น™๋“ค์˜ ๋ชจ ๋…ธ์ด๋“œ ํ‘œํ˜„์€ ๋ชจ ๋…ธ์ด๋“œ ๋ฒ•์น™๋“ค๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•œ๋‹ค. โ†ฉ ์ด์— ๊ด€ํ•ด ์ตœ๊ณ ์ด์ž ์‹ค์šฉ์ ์ธ ์˜ˆ์‹œ๋Š” lens ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, ์‹ค๋กœ ํŽ‘ํ„ฐ์— ๋ฐ”์น˜๋Š” ํ—Œ์ •์ด๋‹ค. โ†ฉ 05 ์• ๋กœ ํŠœํ† ๋ฆฌ์–ผ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Arrow_tutorial ์Šคํ…ŒํŒ์˜ ์• ๋กœ ํŠœํ† ๋ฆฌ์–ผ Circuit์˜ ํƒ€์ž… ์ •์˜ Circuit<NAME> ์• ๋กœ proc ํ‘œ๊ธฐ ํ–‰๋งจ: ๋‚ฑ๋ง ๋งž์ถ”๊ธฐ ํ–‰๋งจ: ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ ๊ณ ๊ธ‰ ์ฃผ์ œ ์• ๋กœ ๋ช…๋ น์„ ํ•จ์ˆ˜์™€ ํ•ฉ์„ฑํ•˜๊ธฐ ์žฌ๊ท€์  ๋ฐ”์ธ๋”ฉ ArrowApply ๋…ธํŠธ ์ด ์žฅ์€ ์• ๋กœ์— ๋Œ€ํ•œ ์‹ค์ „ ํŠœํ† ๋ฆฌ์–ผ์ด๊ณ , ๋‹ค์Œ ์žฅ์ธ ์• ๋กœ ์ดํ•ดํ•˜๊ธฐ๋Š” ๋ณด๋‹ค ๊ฐœ๋…์ ์ธ ์†Œ๊ฐœ๋‹ค. ๋‘˜ ์ค‘ ๋ฌด์—‡์„ ๋จผ์ € ์ฝ์–ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค. ์Šคํ…ŒํŒ์˜ ์• ๋กœ ํŠœํ† ๋ฆฌ์–ผ ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์ง์ ‘ ์• ๋กœ๋ฅผ ์ œ์ž‘ํ•˜๊ณ , ์• ๋กœ proc๊ณผ do ํ‘œ๊ธฐ์˜ ์‚ฌ์šฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ๊ณ , ArrowChoice๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด์—ฌ์ค€๋‹ค. ๊ฐ„๋‹จํ•œ ํ–‰๋งจ ๊ฒŒ์ž„์œผ๋กœ ๋งˆ๋ฌด๋ฆฌํ•œ๋‹ค. ๋จผ์ € ์–ธ์–ด ํ”„๋ผ๊ทธ ๋งˆ๋ฅผ ์ด์šฉํ•ด ์ปดํŒŒ์ผ๋Ÿฌ์—์„œ ์• ๋กœ do ํ‘œ๊ธฐ๋ฅผ ํ™œ์„ฑํ™”ํ•œ๋‹ค. {-# LANGUAGE Arrows #-} ๊ทธ๋ฆฌ๊ณ  import ๋ฌธ๋„ ์ข€ ์žˆ๋‹ค. module Main where import Control.Arrow import Control.Monad import qualified Control.Category as Cat import Data.List import Data.Maybe import System.Random ๋ชจ๋“  ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋Š” ์• ๋กœ๋กœ์„œ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ•จ์ˆ˜ ํƒ€์ž… ์ƒ์„ฑ์ž (->)์— ๋Œ€ํ•œ Arrow ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ๋ณด๋‹ค ํฅ๋ฏธ๋กœ์šด, ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” (ํ‰๋ฒ”ํ•œ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜ ์• ๋กœ๋Š” ํ•  ์ˆ˜ ์—†๋Š”) ์• ๋กœ๋ฅผ ์ œ์ž‘ํ•  ๊ฒƒ์ด๋‹ค. ์• ๋กœ๋กœ ์ž…์ถœ๋ ฅ์„ ํฌํ•จํ•ด ์˜จ๊ฐ– ํšจ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„  ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ๋ช‡ ๊ฐœ๋งŒ ์‚ดํŽด๋ณธ๋‹ค. ์šฐ๋ฆฌ์˜ ์• ๋กœ๋ฅผ ํšŒ๋กœ(circuit)๋กœ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์—์„œ Circuit์ด๋ผ ๋ถ€๋ฅด๊ฒ ๋‹ค. 1 Circuit์˜ ํƒ€์ž… ์ •์˜ ํ‰๋ฒ”ํ•œ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋Š” a -> b ํƒ€์ž…์˜ ์• ๋กœ๋กœ ์ทจ๊ธ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ์˜ Circuit ์• ๋กœ๋Š” ๋‘ ๊ฐœ์˜ ๋‘๋“œ๋Ÿฌ์ง€๋Š” ํŠน์ง•์„ ๊ฐ€์ง„๋‹ค. ์ฒซ์งธ, Arrow ์ธ์Šคํ„ด์Šค๋ฅผ ๊น”๋”ํžˆ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด Circuit์„ newtype ์„ ์–ธ์œผ๋กœ ๊ฐ์‹ผ๋‹ค. ๋‘˜์งธ, ํšŒ๋กœ๊ฐ€ ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋ ค๋ฉด ์šฐ๋ฆฌ์˜ ์• ๋กœ๋Š” ์ž์‹ ์˜ ๋Œ€์ฒด๋ฌผ์„ ์ผ๋ฐ˜์ ์ธ ์ถœ๋ ฅ๊ฐ’ b์™€ ํ•จ๊ป˜ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. newtype Circuit a b = Circuit { unCircuit :: a -> (Circuit a b, b) } Circuit์„ ์• ๋กœ๋กœ ๋งŒ๋“ค๋ ค๋ฉด Category์™€ Arrow์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๋‹ค์Œ์˜ ์ •์˜ ์ „์ฒด์— ๊ฑธ์ณ, ๊ฐ Circuit์€ ํ•ญ์ƒ ๊ทธ๊ฒƒ์ด ๋ฐ˜ํ™˜ํ•˜๋Š”, ์ž์‹ ์˜ ์ƒˆ ๋ฒ„์ „์œผ๋กœ ์น˜ํ™˜๋œ๋‹ค. instance Cat.Category Circuit where id = Circuit $ \a -> (Cat.id, a) (.) = dot where (Circuit cir2) `dot` (Circuit cir1) = Circuit $ \a -> let (cir1', b) = cir1 a (cir2', c) = cir2 b in (cir2' `dot` cir1', c) Cat.id ํ•จ์ˆ˜๋Š” ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ์ž์‹ ์„ ์ž์‹ ์˜ ๋ณต์ œ๋กœ ์น˜ํ™˜ํ•œ๋‹ค. (.) ํ•จ์ˆ˜์˜ ์šฉ๋„๋Š” ๋‘ ์• ๋กœ๋ฅผ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์—ฐ์‡„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (>>>)์™€ (<<<)๋Š” (.)์— ๊ธฐ๋ฐ˜ํ•œ๋‹ค. (.)๋Š” Circuit ์ธ์ž๋“ค์„ ์‹คํ–‰ํ•˜์—ฌ ๋ฐ˜ํ™˜๋œ ๋‘ ๋Œ€์ฒด๋ฌผ์˜ dot์œผ๋กœ ์Šค์Šค๋กœ๋ฅผ ์น˜ํ™˜ํ•œ๋‹ค. instance Arrow Circuit where arr f = Circuit $ \a -> (arr f, f a) first (Circuit cir) = Circuit $ \(b, d) -> let (cir', c) = cir b in (first cir', (c, d)) arr์€ ํ‰๋ฒ”ํ•œ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋ฅผ ์• ๋กœ๋กœ ์ „์ด(lift) ์‹œํ‚จ๋‹ค. id์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ arr์ด ๋Œ๋ ค์ฃผ๋Š” ๋Œ€์ฒด๋ฌผ์€ ์ž๊ธฐ ์ž์‹ ์ธ๋ฐ ํ‰๋ฒ”ํ•œ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋Š” ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด์ œ ํšŒ๋กœ๋ฅผ ์‹คํ–‰ํ•  ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. runCircuit :: Circuit a b -> [a] -> [b] runCircuit _ [] = [] runCircuit cir (x:xs) = let (cir',x') = unCircuit cir x in x' : runCircuit cir' xs ๋‚˜ ๊ฐ™์€ mapAccumL ํŒฌ์ด๋ผ๋ฉด ์ด๋ ‡๊ฒŒ๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. runCircuit :: Circuit a b -> [a] -> [b] runCircuit cir inputs = snd $ mapAccumL (\cir x -> unCircuit cir x) cir inputs ๋˜๋Š”, ์—ํƒ€ ์†Œ๊ฑฐ(eta-reduction)๋ฅผ ํ•˜๊ณ  ๋‚˜๋ฉด ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•ด์ง„๋‹ค. runCircuit :: Circuit a b -> [a] -> [b] runCircuit cir inputs = snd $ mapAccumL unCircuit cir inputs Circuit<NAME> ์ดํ›„ ์ž‘์—…์˜ ๊ธฐ๋ฐ˜์ด ๋  ์ผ๋ฐ˜ํ™”๋œ ๋ˆ„์ ๊ธฐ๋ฅผ ์ •์˜ํ•ด ๋ณด์ž. accum'์€ accum๋ณด๋‹ค ๋œ ๋ฒ”์šฉ์ ์ธ ๋ฒ„์ „์ด๋‹ค. -- | ๊ณต๊ธ‰๋œ ํ•จ์ˆ˜์— ์˜ํ•ด ๊ฒฐ์ •๋˜๋Š” ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ˆ„์ ๊ธฐ -- | Accumulator that outputs a value determined by the supplied function. accum :: acc -> (a -> acc -> (b, acc)) -> Circuit a b accum acc f = Circuit $ \input -> let (output, acc') = input `f` acc in (accum acc' f, output) -- | ๋ˆ„์ ๊ธฐ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ˆ„์ ๊ธฐ -- | Accumulator that outputs the accumulator value. accum' :: b -> (a -> b -> b) -> Circuit a b accum' acc f = accum acc (\a b -> let b' = a `f` b in (b', b')) ๋‹ค์Œ์€ ์ž…๋ ฅ์œผ๋กœ์„œ ์ „๋‹ฌ๋œ ๋ชจ๋“  ์ˆ˜์˜ ์ค‘๊ฐ„ ์ดํ•ฉ๋“ค์„ ์ €์žฅํ•˜๋Š”, ์œ ์šฉํ•˜๊ณ  ๊ตฌ์ฒด์ ์ธ ๋ˆ„์ ๊ธฐ๋‹ค. total :: Num a => Circuit a a total = accum' 0 (+) ์ด ํšŒ๋กœ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. *Main> runCircuit total [1,0,1,0,0,2] [1,1,2,2,2,4] *Main> ์• ๋กœ proc ํ‘œ๊ธฐ ๋‹ค์Œ์€ ํ‰๊ท (statistical mean) ํ•จ์ˆ˜๋‹ค. mean1 :: Fractional a => Circuit a a mean1 = (total &&& (const 1 ^>> total)) >>> arr (uncurry (/)) ์ด ํ•จ์ˆ˜๋Š” ๋‘ ๋ˆ„์ ๊ธฐ ์…€(cell)์„ ์œ ์ง€ํ•˜๋Š”๋ฐ, ํ•˜๋‚˜๋Š” ํ•ฉ, ํ•˜๋‚˜๋Š” ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. ์ž…๋ ฅ์„ ํŒฌ์•„์›ƒ(fanout) ์—ฐ์‚ฐ์ž์ธ &&&๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์— ์•ž์„œ ์ž…๋ ฅ ๊ฐ’๋“ค์„ ์ œ๊ฑฐํ•˜๊ณ  1๋กœ ๋Œ€์ฒดํ•œ๋‹ค. const 1 ^>> total์€ arr (const 1) >>> total์˜ ๋‹จ์ถ• ํ‘œํ˜„์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ŠคํŠธ๋ฆผ์€ ์ž…๋ ฅ์˜ ํ•ฉ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ์ŠคํŠธ๋ฆผ์€ ๊ฐ ์ž…๋ ฅ์— ๋Œ€์‘ํ•˜๋Š” 1์˜ ํ•ฉ(์ฆ‰ ์ž…๋ ฅ์˜ ๊ฐœ์ˆ˜)์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ์ŠคํŠธ๋ฆผ์„ (/) ์—ฐ์‚ฐ์ž๋กœ ๊ฒฐํ•ฉํ•œ๋‹ค. ๋‹ค์Œ์€ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์• ๋กœ do ํ‘œ๊ธฐ๋ฅผ ์จ์„œ ์ž‘์„ฑํ•œ ๊ฒƒ์ด๋‹ค. mean2 :: Fractional a => Circuit a a mean2 = proc value -> do t <- total -< value n <- total -< 1 returnA -< t / n do ํ‘œ๊ธฐ๋Š” ์• ๋กœ ๊ฐ„์˜ ๊ฐ™์€ ๊ด€๊ณ„๋ฅผ ์„œ์ˆ ํ•˜์ง€๋งŒ ๊ทธ ๋ฐฉ๋ฒ•์ด ์™„์ „ํžˆ ๋‹ค๋ฅด๋‹ค. ์™€์ด์–ด๋ง์„ ๋ช…์‹œํ•˜๋Š” ๋Œ€์‹  ์—ฌ๋Ÿฌ๋ถ„์€ ๋ณ€์ˆ˜ ๋ฐ”์ธ๋”ฉ๊ณผ ์ˆœ์ˆ˜ ํ•˜์Šค ์ผˆ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ์• ๋กœ๋“ค์„ ์—ฐ๊ฒฐํ•˜๊ณ , ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ arr, >>>, &&& ๋“ฑ์„ ๋Œ€์‹  ์ฒ˜๋ฆฌํ•œ๋‹ค. ์• ๋กœ proc ํ‘œ๊ธฐ ์•ˆ์˜ let ๋ฌธ์€ ๋ชจ๋‚˜ ๋”• do ํ‘œ๊ธฐ ์•ˆ์˜ let์ฒ˜๋Ÿผ ์ˆœ์ˆ˜ํ•œ let์ด๋‹ค. proc์€ ์• ๋กœ ํ‘œ๊ธฐ๋ฅผ ๋„์ž…ํ•˜๋Š” ํ‚ค์›Œ๋“œ๋กœ์„œ ์• ๋กœ ์ž…๋ ฅ์„ ํŒจํ„ด(์—ฌ๊ธฐ์„  'value')์— ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. do ๋ธ”๋ก ๋‚ด์˜ ์• ๋กœ ๊ตฌ๋ฌธ์€ ๋‹ค์Œ ์ค‘ ํ•˜๋‚˜์˜ ์–‘์‹์„ ๊ฐ–๋Š”๋‹ค. ๋ณ€์ˆ˜ ๋ฐ”์ธ๋”ฉ ํŒจํ„ด <- ์• ๋กœ -< ์• ๋กœ ์ž…๋ ฅ์„ ์ œ๊ณตํ•˜๋Š” ์ˆœ์ˆ˜ ํ‘œํ˜„์‹ ์• ๋กœ -< ์• ๋กœ ์ž…๋ ฅ์„ ์ œ๊ณตํ•˜๋Š” ์ˆœ์ˆ˜ ํ‘œํ˜„์‹ ๋ชจ๋‚˜๋“œ์˜ ๊ฒฝ์šฐ์™€ ๋น„์Šทํ•˜๊ฒŒ, do ํ‚ค์›Œ๋“œ์˜ ์šฉ๋„๋Š” ๋ณ€์ˆ˜ ๋ฐ”์ธ๋”ฉ ํŒจํ„ด์„ <-์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์—ฌ๋Ÿฌ ์ค„์„ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ชจ๋‚˜๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋งˆ์ง€๋ง‰ ์ค„์€ ๋ณ€์ˆ˜ ๋ฐ”์ธ๋”ฉ ํŒจํ„ด์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๊ณ  ๋งˆ์ง€๋ง‰ ์ค„์˜ ๊ฒฐ๊ด๊ฐ’์€ ์• ๋กœ์˜ ๊ฒฐ๊ณผ๊ฐ’์ด๋‹ค. returnA๋Š” total์ฒ˜๋Ÿผ ํ•˜๋‚˜์˜ ์• ๋กœ ์ผ ๋ฟ์ด๋‹ค(์‚ฌ์‹ค returnA๋Š” arr id๋กœ ์ •์˜๋œ ํ•ญ๋“ฑ ์• ๋กœ๋‹ค). ์—ญ์‹œ ๋ชจ๋‚˜๋“œ์™€ ๋น„์Šทํ•˜๊ฒŒ, ๋งˆ์ง€๋ง‰์ด ์•„๋‹Œ ์ค„์—๋„ ๋ณ€์ˆ˜ ๋ฐ”์ธ๋”ฉ์ด ์—†์„ ์ˆ˜ ์žˆ๊ณ , ๊ทธ๋Ÿฌ๋ฉด ๊ฒฐ๊ด๊ฐ’์„ ๋ฌด์‹œํ•˜๊ณ  ๊ทธ ํšจ๊ณผ๋งŒ์„ ์–ป๊ฒŒ ๋œ๋‹ค. Circuit์—์„œ๋Š” ๊ทธ๋Ÿฐ ์ผ์„ ํ•  ์ด์œ ๊ฐ€ ์—†์ง€๋งŒ(๊ฒฐ๊ด๊ฐ’์„ ํ†ตํ•ด์„œ๊ฐ€ ์•„๋‹ˆ๊ณ ์„  ์–ด๋–ค ์ƒํƒœ๋„ ํƒˆ์ถœํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ), ๋งŽ์€ ์• ๋กœ๋Š” ๊ทธ๋ ‡๊ฒŒ ํ•œ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” 'proc' ํ‘œ๊ธฐ๊ฐ€ ์ฝ”๋“œ๋ฅผ ํ›จ์”ฌ ์ฝ๊ธฐ ์ข‹๊ฒŒ ๋งŒ๋“ ๋‹ค. ํ•œ ๋ฒˆ ์‹คํ—˜ํ•ด ๋ณด์ž. *Main> runCircuit mean1 [0,10,7,8] [0.0,5.0,5.666666666666667,6.25] *Main> runCircuit mean2 [0,10,7,8] [0.0,5.0,5.666666666666667,6.25] *Main> ํ–‰๋งจ: ๋‚ฑ๋ง ๋งž์ถ”๊ธฐ ์ด์ œ ํ–‰๋งจ ๊ฒŒ์ž„์ด๋‹ค. ์‚ฌ์ „์—์„œ ๋‚ฑ๋ง์„ ํ•˜๋‚˜ ๊ณจ๋ผ๋ณด์ž. generator :: Random a => (a, a) -> StdGen -> Circuit () a generator range rng = accum rng $ \() rng -> randomR range rng dictionary = ["dog", "cat", "bird"] pickWord :: StdGen -> Circuit () String pickWord rng = proc () -> do idx <- generator (0, length dictionary-1) rng -< () returnA -< dictionary !! idx generator์—์„œ๋Š” ๋ˆ„์‚ฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•ด ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ๋ฅผ ๋ณด๊ด€ํ•œ๋‹ค. ์ƒ์„ฑ์ž ์• ๋กœ๊ฐ€ ์ธ์ž๋ฅผ ์ทจํ•˜๋Š” ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜์— ์˜ํ•ด ์ƒ์„ฑ๋œ๋‹ค๋Š” ๊ฒƒ๋งŒ ๋นผ๋ฉด, pickWord๋Š” ๋”ฑํžˆ ์ƒˆ๋กœ์šด ๊ฐœ๋…์„ ๋„์ž…ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค์Œ์€ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋‹ค. *Main> rng <- getStdGen *Main> runCircuit (pickWord rng) [(), (), ()] ["dog","bird","dog"] *Main> ๋‹น๋ถ„๊ฐ„ ์ด ์กฐ๊ทธ๋งŒ ์• ๋กœ๋ฅผ ์“ด๋‹ค. ์ด ์• ๋กœ๋Š” ์ฒ˜์Œ์—๋งŒ True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ ์ดํ›„์—” ์ญ‰ False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. oneShot :: Circuit () Bool oneShot = accum True $ \_ acc -> (acc, False) *Main> runCircuit oneShot [(), (), (), (), ()] [True, False, False, False, False] ์ด ๊ฒŒ์ž„์˜ ๋ฉ”์ธ ์• ๋กœ๋Š” ๋ฐ˜๋ณตํ•˜์—ฌ ์‹คํ–‰๋  ๊ฒƒ์ด๊ณ , ์šฐ๋ฆฌ๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฐ˜๋ณต ๋•Œ๋งŒ ๋‚ฑ๋ง์„ ๊ณจ๋ผ๋‘๊ณ  ๊ฒŒ์ž„ ๋‚ด๋‚ด ๊ธฐ์–ตํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ ์ถœ๋ ฅ์„ ์—ฐ์ด์€ ๋ฃจํ”„์—์„œ ๋ฎ์–ด์“ฐ๊ธฐ๋ณด๋‹จ pickWord๋ฅผ ์‹ค์ œ๋กœ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰ํ•˜๋ ค ํ•œ๋‹ค(์‹ค์ œ ๊ตฌํ˜„์—์„  ๋งค์šฐ ๋Š๋ฆด ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ). ํ•˜์ง€๋งŒ, ํ˜„ ์ƒํƒœ์—์„œ๋Š” Circuit ๋‚ด์˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์€ ๋ฐ˜๋“œ์‹œ ๊ตฌ์„ฑ์š”์†Œ ์• ๋กœ๋“ค์˜ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ํ๋ฆ„์ด ํ•œ ํŒจ์Šค๋งŒ ๊ฑฐ์น˜๊ณ  ๋‹ค๋ฅธ ํŒจ์Šค๋Š” ๊ฑฐ์น˜์ง€ ์•Š๊ฒŒ ํ•˜๋ ค๋ฉด ์šฐ๋ฆฌ์˜ ์• ๋กœ๋ฅผ ArrowChoice์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๋‹ค์Œ์€ ์ตœ์†Œํ•œ์˜ ์ •์˜๋‹ค. instance ArrowChoice Circuit where left orig@(Circuit cir) = Circuit $ \ebd -> case ebd of Left b -> let (cir', c) = cir b in (left cir', Left c) Right d -> (left orig, Right d) getWord :: StdGen -> Circuit () String getWord rng = proc () -> do -- If this is the first game loop, run pickWord. mPicked becomes Just <word>. -- On subsequent loops, mPicked is Nothing. first Time <- oneShot -< () mPicked <- if first Time then do picked <- pickWord rng -< () returnA -< Just picked else returnA -< Nothing -- An accumulator that retains the last 'Just' value. mWord <- accum' Nothing mplus -< mPicked returnA -< fromJust mWord ArrowChoice๊ฐ€ ์ •์˜๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ด์ œ <- ๋’ค์— if๋ฅผ ๋†“์•„์„œ, ์–ด๋–ค ์• ๋กœ๋ฅผ ์‹คํ–‰ํ• ์ง€ ์„ ํƒํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค(pickWord๋ฅผ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ ๊ฑด๋„ˆ๋›ฐ๊ฑฐ๋‚˜). ์ด๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ํ•˜์Šค ์ผˆ if๊ฐ€ ์•„๋‹˜์— ์œ ์˜ํ•  ๊ฒƒ. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ด๊ฒƒ์„ ArrowChoice๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค. ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ case๋„ ๊ตฌํ˜„ํ•œ๋‹ค. ์ค‘์š”ํ•œ ๊ฒƒ์€ proc ์ธ์ž๋ฅผ ํฌํ•จํ•ด ์–ด๋–ค ๋กœ์ปฌ ์ด๋ฆ„ ๋ฐ”์ธ๋”ฉ๋„ <-์™€ -< ์‚ฌ์ด์˜ ์Šค์ฝ”ํ”„์— ์žˆ์ง€ ์•Š๋‹ค(if์™€ case์˜ ์กฐ๊ฑด๋ถ€๋Š” ์˜ˆ์™ธ)๋Š” ๊ฑธ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์€ ํ‹€๋ฆฐ ๊ฒƒ์ด๋‹ค. {- proc rng -> do idx <- generator (0, length dictionary-1) rng -< () -- ์ž˜๋ชป๋จ returnA -< dictionary !! idx -} ์‹คํ–‰ํ•  ์• ๋กœ(์—ฌ๊ธฐ์„  generator (0, length dictionary -1) rng)๊ฐ€ proc ๋ฌธ ์™ธ๋ถ€์— ์กด์žฌํ•˜๋Š” ์Šค์ฝ”ํ”„ ๋‚ด์—์„œ ํ‰๊ฐ€๋œ๋‹ค. ์ด ์Šค์ฝ”ํ”„์—์„  rng๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๋งž๋Š” ๊ฒƒ์ด, ์• ๋กœ๋Š” ์‹œ์ž‘ ๋ถ€๋ถ„(proc์˜ ๋ฐ”๊นฅ)์—์„œ๋งŒ ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์• ๋กœ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ์ƒ์„ฑ๋œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ฒ ๋Š”๊ฐ€? getWord๋ฅผ ์‹คํ—˜ํ•ด ๋ณด์ž. *Main> rng <- getStdGen *Main> runCircuit (getWord rng) [(), (), (), (), (), ()] ["dog","dog","dog","dog","dog","dog"] *Main> ํ–‰๋งจ: ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ ์ด๊ฒƒ์ด ํ–‰๋งจ ๊ฒŒ์ž„์ด๋‹ค. hangman :: StdGen -> Circuit String [String] hangman rng = proc userInput -> do word <- getWord rng -< () let letter = listToMaybe userInput guessed <- updateGuess -< (word, letter) hung <- updateHung -< (word, letter) let hangman = "HangMan: ["++replicate hung '#'++replicate (5-hung) ' '++"]" returnA -< if word == guessed then [guessed, "You won!"] else if hung >= 5 then [guessed, hangman, "You died!"] else [guessed, hangman] where updateGuess :: Circuit (String, Maybe Char) String updateGuess = accum' (repeat '_') $ \(word, letter) guess -> case letter of Just l -> map (\(w, g) -> if w == l then w else g) (zip word guess) Nothing -> take (length word) guess updateHung :: Circuit (String, Maybe Char) Int updateHung = proc (word, letter) -> do total -< case letter of Just l -> if l `elem` word then 0 else 1 Nothing -> 0 main = do rng <- getStdGen interact (unlines . -- Concatenate lines out output ("Welcome to Arrow Hangman":) . -- Prepend a greeting to the output concat . runCircuit (hangman rng) . -- Process the input lazily ("":) . -- Act as if the user pressed ENTER once at the start lines) -- Split input into lines ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ์€ ์‹คํ–‰ ์˜ˆ์‹œ๋‹ค. ์ตœ์ƒ์˜ ๊ฒฐ๊ณผ๋ฅผ ์œ„ํ•ด ๊ฒŒ์ž„์„ ์ปดํŒŒ์ผํ•˜๊ณ  GHCi๋ณด๋‹จ ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ํ•  ๊ฒƒ. Welcome to Arrow Hangman ___ HangMan: [ ] ___ HangMan: [# ] __g HangMan: [# ] d_g HangMan: [# ] dog You won! ๊ณ ๊ธ‰ ์ฃผ์ œ ์ด ์ ˆ์—์„œ๋Š” ์• ๋กœ ํ‘œ๊ธฐ์˜ ๋ชจ๋“  ๊ฒƒ์„ ๋‹ค๋ฃฌ๋‹ค. ์• ๋กœ ๋ช…๋ น์„ ํ•จ์ˆ˜์™€ ํ•ฉ์„ฑํ•˜๊ธฐ mean2๋ฅผ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ–ˆ์—ˆ๋‹ค. mean2 :: Fractional a => Circuit a a mean2 = proc value -> do t <- total -< value n <- total -< 1 returnA -< t / n GHC๋Š” ์• ๋กœ ๊ตฌ๋ฌธ์„ ๊ทธ ์• ๋กœ์— ์ž‘์šฉํ•˜๋Š” ํ•จ์ˆ˜์™€ ํ•ฉ์„ฑํ•˜๊ธฐ ์œ„ํ•œ banana ๊ด„ํ˜ธ ๋ฌธ๋ฒ•์„ ์ •์˜ํ•œ๋‹ค. (Ross Paterson์˜ ๋…ผ๋ฌธ 2์—์„œ๋Š” form ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, GHC๋Š” banana ๊ด„ํ˜ธ๋ฅผ ์ฑ„ํƒํ–ˆ๋‹ค) ๋”ฑํžˆ ์ด์œ ๋Š” ์—†์ง€๋งŒ mean์„ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. mean3 :: Fractional a => Circuit a a mean3 = proc value -> do (t, n) <- (| (&&&) (total -< value) (total -< 1) |) returnA -< t / n (| ... |) ๋‚ด์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์€ ์ž„์˜ ๊ฐœ์ˆ˜์˜ ์• ๋กœ๋ฅผ ์ทจํ•ด ํ•œ ์• ๋กœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. ์—ฌ๊ธฐ์„  ์ค‘์œ„ ํ‘œ๊ธฐ๋ฅผ ์“ธ ์ˆ˜ ์—†๋‹ค. ๊ทธ๋‹ค์Œ proc ๊ตฌ๋ฌธ์˜ ํ˜•ํƒœ๋กœ ์ธ์ž๋“ค์ด ์˜จ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์›ํ•œ๋‹ค๋ฉด ์ด ๊ตฌ๋ฌธ๋“ค์— do๋‚˜ <-๋กœ ๋ฐ”์ธ๋”ฉ์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ ์ธ์ž๋Š” ์• ๋กœ๋กœ ๋ฒˆ์—ญ๋œ ํ›„ (&&&) ํ•จ์ˆ˜์— ์ธ์ž๋กœ ์ „๋‹ฌ๋œ๋‹ค. banana ๊ด„ํ˜ธ๋Š” ์‚ฌ์‹ค ํ•„์š” ์—†๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•ด๋„ ๋ฌด์Šจ ๋œป์ธ์ง€ ์•Œ๋งŒํผ ๋˜‘๋˜‘ํ•˜๋‹ค. (์—ฌ๊ธฐ์„  ์ค‘์œ„ ํ‘œ๊ธฐ๊ฐ€ ํ—ˆ์šฉ๋œ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ) mean4 :: Fractional a => Circuit a a mean4 = proc value -> do (t, n) <- (total -< value) &&& (total -< 1) returnA -< t / n ๊ทธ๋Ÿผ banana ๊ด„ํ˜ธ๋Š” ์™œ ํ•„์š”ํ• ๊นŒ? ์œ„์˜ ํ‰์ดํ•œ ๋ฌธ๋ฒ•์ด ๋ชจํ˜ธํ•œ ์ƒํ™ฉ์„ ์œ„ํ•ด์„œ๋‹ค. ๊ทธ ์ด์œ ๋Š” proc ๋ช…๋ น์˜ ์• ๋กœ ๋ถ€๋ถ„์ด ํ‰๋ฒ”ํ•œ ํ•˜์Šค ์ผˆ ํ‘œํ˜„์‹์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. proc ๊ตฌ๋ฌธ ์•ˆ์— ๋ช…์‹œ๋œ ์• ๋กœ๋“ค์—๋Š” ๋‹ค์Œ์ด ์„ฑ๋ฆฝํ•จ์„ ๋ช…์‹ฌํ•˜๋ผ. ๋กœ์ปฌ ๋ณ€์ˆ˜ ๋ฐ”์ธ๋”ฉ์ด ํ—ˆ์šฉ๋˜๋Š” ๊ณณ์€ -< ๋’ค์˜ ์ž…๋ ฅ ํ‘œํ˜„์‹, if์™€ case์˜ ์กฐ๊ฑด์‹๋ฟ์ด๋‹ค. ์• ๋กœ ์ž์ฒด๋Š” proc ๋ฐ”๊นฅ์˜ ์Šค์ฝ”ํ”„์—์„œ ํ•ด์„๋œ๋‹ค. if์™€ case๋Š” ํ‰์ดํ•œ ํ•˜์Šค์ผˆ์ด ์•„๋‹ˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ ArrowChoice๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„๋œ๋‹ค. ์• ๋กœ๋ฅผ ๊ฒฐํ•ฉํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋“ค๋„ ์ผ๋ฐ˜์ ์ธ ํ•˜์Šค์ผˆ์ด ์•„๋‹ˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ banana ํ‘œ๊ธฐ๋ฅผ ์œ„ํ•œ ๋‹จ์ถ• ํ‘œํ˜„์ด๋‹ค. ์žฌ๊ท€์  ๋ฐ”์ธ๋”ฉ mean ์˜ˆ์ œ๋ฅผ ๋˜ ์šฐ๋ ค๋จน์ž๋ฉด, ๋‹ค์Œ์€ ์žฌ๊ท€ ๋ฐ”์ธ๋”ฉ์„ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด๊ฒƒ์ด ์ž‘๋™ํ•˜๋ ค๋ฉด ๊ทธ ์ž…๋ ฅ์„ ํ•œ ๋‹จ๊ณ„ ์ง€์—ฐ์‹œํ‚ค๋Š” ์• ๋กœ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. delay :: a -> Circuit a a delay last = Circuit $ \this -> (delay this, last) ์ด๊ฒƒ์ด delay๊ฐ€ ํ•˜๋Š” ์ผ์ด๋‹ค. *Main> runCircuit (delay 0) [5,6,7] [0,5,6] *Main> ์ด๊ฒƒ์€ mean์˜ ์žฌ๊ท€ ๋ฒ„์ „์ด๋‹ค. mean5 :: Fractional a => Circuit a a mean5 = proc value -> do rec (lastTot, lastN) <- delay (0,0) -< (tot, n) let (tot, n) = (lastTot + value, lastN + 1) let mean = tot / n returnA -< mean rec ๋ธ”๋ก์€ ๋‹ค์Œ ์‚ฌํ•ญ์„ ๋นผ๊ณ ๋Š” do ๋ธ”๋ก๊ณผ ๋‹ฎ์•˜๋‹ค. ๋งˆ์ง€๋ง‰ ์ค„์ด ๋ณ€์ˆ˜ ๋ฐ”์ธ๋”ฉ์ผ ์ˆ˜ ์žˆ๊ณ  ํ†ต์ƒ ๊ทธ๋ ‡๊ฒŒ ์“ด๋‹ค. let์ด ๋“  <-์— ์˜ํ•œ do ๋ธ”๋ก ๋ฐ”์ธ๋”ฉ์ด๋“  ๋ฌด๊ด€ํ•˜๋‹ค. rec ๋ธ”๋ก์€ ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋‹ค. var <- rec ... ์€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š๊ณ  rec์€ do ๋ธ”๋ก์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ์ผ ์ˆ˜ ์—†๋‹ค. ๋ณ€์ˆ˜์˜ ์‚ฌ์šฉ์ด ์‚ฌ์ดํด์„ ํ˜•์„ฑํ•  ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋œ๋‹ค(๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด rec์„ ์“ฐ๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค). rec์˜ machinery๋Š” ArrowLoop ํด๋ž˜์Šค์˜ ๋ฃจํ”„ ํ•จ์ˆ˜์— ์˜ํ•ด ์ฒ˜๋ฆฌ๋˜๊ณ , Circuit์— ๋Œ€ํ•ด์„œ๋Š” ์ด๋ ‡๊ฒŒ ์ •์˜๋œ๋‹ค. instance ArrowLoop Circuit where loop (Circuit cir) = Circuit $ \b -> let (cir', (c, d)) = cir (b, d) in (loop cir', c) ๊ทธ ์ด๋ฉด์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. rec์— ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉด์„œ rec ๋‚ด์—์„œ ์ „๋ฐฉ ์ฐธ์กฐ๋˜๋Š” ๋ณ€์ˆ˜๋“ค์€ loop์˜ ๋‘ ๋ฒˆ์งธ ํŠœํ”Œ ์›์†Œ๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋˜๋ฉฐ ๋ฃจํ”„๋ฅผ ๋ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ๋ณ€์ˆ˜ ๋ฐ”์ธ๋”ฉ๊ณผ ๊ทธ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋Š” ์–ด๋–ค ์ˆœ์„œ๋กœ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค(ํ•˜์ง€๋งŒ ์• ๋กœ ๊ตฌ๋ฌธ๋“ค์˜ ์ˆœ์„œ๋Š” ํšจ๊ณผ ๋ฉด์—์„œ ์ค‘์š”ํ•˜๋‹ค). rec์— ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉด์„œ rec ์™ธ๋ถ€์—์„œ ์ฐธ์กฐ๋˜๋Š” ๋ณ€์ˆ˜๋“ค์€ loop์˜ ์ฒซ ๋ฒˆ์งธ ํŠœํ”Œ ์›์†Œ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค. loop๋Š” (๊ทธ์— ๋”ฐ๋ผ rec์€) ๊ทธ์ € ๋ณ€์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค๋Š” ๊ฑธ ์ดํ•ดํ•˜๋Š” ๊ฒŒ ์ค‘์š”ํ•˜๋‹ค. ์ด๊ฒƒ์€ ๊ฐ’์„ ๋ณด๊ด€ํ–ˆ๋‹ค ๋‹ค์Œ ์‹คํ–‰ ๋•Œ ๋‹ค์‹œ ์ „๋‹ฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๊ฑด delay์˜ ๋ชซ์ด๋‹ค. ๋ณ€์ˆ˜ ์ฐธ์กฐ์— ์˜ํ•ด ํ˜•์„ฑ๋˜๋Š” ์‚ฌ์ดํด์€ ๋ชจ์ข…์˜ ๋”œ๋ ˆ์ด ์• ๋กœ๋‚˜ ์ง€์—ฐ ํ‰๊ฐ€์— ์˜ํ•ด ๋ถ„ํ•ด๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ฝ”๋“œ๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ํ‰๋ฒ”ํ•œ ํ•˜์Šค์ผˆ์—์„œ let a = a + 1์ด๋ผ๊ณ  ์“ธ ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฌดํ•œ ๋ฃจํ”„์˜ ๊ตฌ์ฒœ์„ ๋– ๋Œ ๊ฒƒ์ด๋‹ค. ArrowApply ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ์• ๋กœ ๊ตฌ๋ฌธ์˜ ์• ๋กœ ๋ถ€๋ถ„(-< ์•ž๋ถ€๋ถ„)์€ proc ๋‚ด์—์„œ ๋ฐ”์ธ๋”ฉ ๋œ ๋ณ€์ˆ˜๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์—†๋‹ค. ์ด ์ œํ•œ์„ ์—†์• ์ฃผ๋Š” -<<๋ผ๋Š” ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ๋‹ค. ์ด๊ฑธ ์“ฐ๋ ค๋ฉด ์• ๋กœ๊ฐ€ ArrowApply ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋…ธํŠธ ์• ๋กœ๋ฅผ ํšŒ๋กœ๋กœ ๋ณด๋Š” ๊ฒƒ์€ ์–ด๋Š ์ •๋„ Yampa๋ผ๋Š” functional reactive programming ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๋”ฐ์˜จ ๊ฒƒ์ด๋‹ค. โ†ฉ ์• ๋กœ proc ํ‘œ๊ธฐ์— ๊ด€ํ•œ Ross Paterson์˜ ๋…ผ๋ฌธ โ†ฉ 06 ์• ๋กœ ์ดํ•ดํ•˜๊ธฐ ์›๋ฌธ : http://en.wikibooks.org/wiki/Haskell/Understanding_arrows ๊ณต์žฅ๊ณผ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ ๋น„์œ  ์„ธ์ƒ์ด ๋กœ๋ด‡์œผ๋กœ ๊ฐ€๋“ํ•ด arr (>>>) first second *** &&& ํ•จ์ˆ˜๋Š” ์• ๋กœ๋‹ค ์• ๋กœ ํ‘œ๊ธฐ Maybe ํ•„ํ„ฐ ์• ๋กœ์˜ ํ™œ์šฉ ์ŠคํŠธ๋ฆผ ์ฒ˜๋ฆฌ(stream processing) ๋ˆ„์ˆ˜ ๋ฐฉ์ง€(avoiding leaks) ๊ทธ๋Ÿผ ๋ญ๊ฐ€ ๋” ์ข‹์€๊ฐ€? ์ •์  ํŒŒ์„œ์™€ ๋™์  ํŒŒ์„œ ์• ๋กœ ๊ฒฐํ•ฉ๊ธฐ (๋กœ๋ด‡) ๊ทธ๋ž˜์„œ ์• ๋กœ๊ฐ€ ์ฃผ๋Š” ๊ฒƒ์€? ๋ชจ๋‚˜๋“œ๋„ ์• ๋กœ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค ์• ๋กœ ์‹ค์ „ ๋” ๋ณด๊ธฐ ๊ฐ์‚ฌ์˜ ๋ง ๋…ธํŠธ ์šฐ๋ฆฌ๋Š” ํ•˜์Šค ์ผˆ ์• ๋กœ ํŽ˜์ด์ง€์—์„œ ์ž๋ฃŒ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ํ—ˆ๋ฝ๋ฐ›์•˜๋‹ค. ์ž์„ธํ•œ ์‚ฌํ•ญ์€ talk page๋ฅผ ๋ณผ ๊ฒƒ. ๊ณต์žฅ๊ณผ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ ๋น„์œ  ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ๊ณต์žฅ ๋น„์œ ๋ฅผ ํ†ตํ•ด, ํ๋ฆ„ ์ฒ˜๋ฆฌ๊ธฐ๋ผ๋Š” ๊ด€์ ์—์„œ ์• ๋กœ๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. ์†์— ๊ธฐ๋ฆ„๋•Œ๋ฅผ ๋ฌปํ˜€๋ณด์ž. ์—ฌ๋Ÿฌ๋ถ„์€ ๊ณต์žฅ์ฃผ๊ณ  ์ž‘์—… ๊ธฐ๊ณ„ ํ•œ ๋‹ค๋ฐœ์„ ์†Œ์œ ํ•˜๊ณ  ์žˆ๋‹ค. ์ด ์ž‘์—… ๊ธฐ๊ณ„๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์€์œ ๋‹ค. ๊ธฐ๊ณ„๋Š” ๋ชจ์ข…์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„ ๋ชจ์ข…์˜ ์ถœ๋ ฅ์„ ์ƒ์‚ฐํ•œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ชฉ์ ์€ ์ž‘์—… ๊ธฐ๊ณ„๋“ค์„ ๊ฒฐํ•ฉํ•˜์—ฌ ๋” ํ’๋ถ€ํ•˜๊ณ  ๋ณต์žกํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ๋ชจ๋‚˜๋“œ๋Š” ์ด ๊ธฐ๊ณ„๋“ค์„ ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ์• ๋กœ๋Š” ์ด๊ฒƒ๋“ค์„ ๋ณด๋‹ค ํฅ๋ฏธ๋กœ์šด ๋ฐฉ์‹์œผ๋กœ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ๋ชจ๋‚˜๋“œ ๊ณต์žฅ์—์„œ๋Š” ๊ธฐ๊ณ„์˜ ์ถœ๋ ฅ์„ ์ปจํ…Œ์ด๋„ˆ๋กœ ๊ฐ์‹ธ๋Š” ์ ‘๊ทผ๋ฒ•์„ ์ทจํ•œ๋‹ค. ์• ๋กœ ๊ณต์žฅ์€ ํ™•์—ฐํžˆ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ํƒํ•œ๋‹ค. ์ถœ๋ ฅ์„ ์ปจํ…Œ์ด๋„ˆ๋กœ ๊ฐ์‹ธ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ๊ธฐ๊ณ„ ์ž์ฒด๋ฅผ ๊ฐ์‹ผ๋‹ค. ์ข€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ๋Š”, ์• ๋กœ ๊ณต์žฅ์—์„œ๋Š” ๊ฐ๊ฐ์˜ ๊ธฐ๊ณ„์— ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์œ„ํ•œ ํ•œ ์Œ์˜ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ์žฅ์ฐฉํ•œ๋‹ค. ์ฆ‰ b -> c ํƒ€์ž…์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ, ์šฐ๋ฆฌ๋Š” ๊ทธ ๊ธฐ๊ณ„์— b์™€ c ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ๋ถ™์—ฌ์„œ ์ด ํ•จ์ˆ˜์™€ ๋™๋“ฑํ•œ ์• ๋กœ a๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค. ๊ทธ ์• ๋กœ๋Š” a b c ํƒ€์ž…์ด๊ณ , b์—์„œ c๋กœ ๊ฐ€๋Š” ์• ๋กœ a๋ผ ์ฝ๋Š”๋‹ค. ์„ธ์ƒ์ด ๋กœ๋ด‡์œผ๋กœ ๊ฐ€๋“ํ•ด ์•ž์„œ ์–ธ๊ธ‰ํ•˜๊ธฐ๋ฅผ ๊ธฐ๊ณ„๋“ค์„ ์„œ๋กœ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ชจ๋‚˜๋“œ๋ณด๋‹ค ์• ๋กœ๊ฐ€ ๋งŽ๋‹ค๊ณ  ํ–ˆ๋‹ค. ๋ชจ๋‚˜๋“œ์˜ ๋กœ๋ด‡์€ ๋‘ ๊ฐœ์ง€๋งŒ ์• ๋กœ ํƒ€์ž… ํด๋ž˜์Šค์˜ ๋กœ๋ด‡์€ 6๊ฐœ๋‹ค. arr ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋กœ๋ด‡์€ arr์œผ๋กœ ๊ทธ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋Š” arr :: (b -> c) -> a b c์ด๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด arr ๋กœ๋ด‡์€ b -> c ํƒ€์ž…์˜ ์ž‘์—… ๊ธฐ๊ณ„๋ฅผ ์ทจํ•ด์„œ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ๋ถ™์ž„์œผ๋กœ์จ b์—์„œ c๋กœ ๊ฐ€๋Š” ์• ๋กœ a๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค. (>>>) ๋‹ค์Œ ๋กœ๋ด‡์€ ์•„๋งˆ ๊ฐ€์žฅ ์ค‘์š”ํ•œ (>>>)์ด๋‹ค. ์ด๊ฒƒ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋‚˜๋“œ bind ๋กœ๋ด‡ (>>=)์™€ ๋™๋“ฑํ•œ ์• ๋กœ๋‹ค. bind์˜ ์• ๋กœ ๋ฒ„์ „์ธ (>>>)๋Š” ๋‘ ์• ๋กœ๋ฅผ ํ•œ ์‹œํ€€์Šค ์•ˆ์— ๋†“๋Š”๋‹ค. ์ฆ‰ ์ฒซ ๋ฒˆ์งธ ์• ๋กœ์˜ ์ถœ๋ ฅ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ๋‘ ๋ฒˆ์งธ ์• ๋กœ์˜ ์ž…๋ ฅ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ์— ์—ฐ๊ฒฐํ•œ๋‹ค. ์ด๋กœ์จ ์šฐ๋ฆฌ๋Š” ์ƒˆ๋กœ์šด ์• ๋กœ๋ฅผ ์–ป๋Š”๋‹ค. ํ•˜์ง€๋งŒ ํ•˜๋‚˜ ์ƒ๊ฐํ•ด ๋ณผ ๊ฒƒ์€, ์šฐ๋ฆฌ์˜ ์• ๋กœ๊ฐ€ ์ทจํ•  ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ํƒ€์ž…์ด ๋ฌด์—‡์ด๋ƒ๋Š” ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ์• ๋กœ์™€ ๋‘ ๋ฒˆ์งธ ์• ๋กœ์—์„œ ๊ฐ๊ฐ ์ถœ๋ ฅ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ์™€ ์ž…๋ ฅ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‘ ๋ฒˆ์งธ ์• ๋กœ๋Š” ์ฒซ ๋ฒˆ์งธ ์• ๋กœ๊ฐ€ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์ข…๋ฅ˜์˜ ์ž…๋ ฅ์„ ์ทจํ•ด์•ผ ํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์• ๋กœ์˜ ํƒ€์ž…์ด a b c๋ผ๋ฉด, ๋‘ ๋ฒˆ์งธ ์• ๋กœ์˜ ํƒ€์ž…์€ a c d ์—ฌ์•ผ ํ•œ๋‹ค. ๋‹ค์Œ์€ ์œ„์™€ ๊ฐ™์€ ๋„์‹์ด์ง€๋งŒ ํƒ€์ž…๊ณผ ๊ด€๋ จ๋œ ์‚ฌํ•ญ์ด ์ž˜ ๋ณด์ด๋„๋ก ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ์— ๋ฌผ๊ฑด์„ ๋†“์•˜๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ํ•ฉ์„ฑ๋œ ์• ๋กœ์˜ ํƒ€์ž…์€ ๋ฌด์—‡์ธ๊ฐ€? first ์ง€๊ธˆ์˜ ์• ๋กœ๋Š” ๋ชจ๋‚˜๋“œ์™€ ๊ฐ™์€ ์ผ๋งŒ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ํฅ๋ฏธ๋กญ๋‹ค! ์• ๋กœ ํƒ€์ž… ํด๋ž˜์Šค๋Š” ์• ๋กœ๊ฐ€ ์ž…๋ ฅ์˜ ์ง์— ์ž‘์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ฐ€๋Šฅ์ผ€ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ๋‚˜์ค‘์— ๋ณด๊ฒ ์ง€๋งŒ ์ด๋ฅผ ํ†ตํ•ด ๋ณ‘๋ ฌ ๊ณ„์‚ฐ์„ ์•„์ฃผ ๊ฐ„๊ฒฐํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ํ•จ์ˆ˜๋“ค ์ค‘ ์ฒซ ๋ฒˆ์งธ๊ฐ€ first๋‹ค. ์ด ํŠœํ† ๋ฆฌ์–ผ์„ ํœ™ํœ™ ๋„˜๊ธฐ๊ณ  ์žˆ๋‹ค๋ฉด ์ ์–ด๋„ ์ด ์ ˆ์—์„œ๋Š” ์„œํ–‰ํ•˜๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. first ๋กœ๋ด‡์€ ์• ๋กœ๋ฅผ ์ง„์ •์œผ๋กœ ์œ ์šฉํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ๋“ค ์ค‘ ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์• ๋กœ f๊ฐ€ ์ฃผ์–ด์ง€๋ฉด first ๋กœ๋ด‡์€ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋“ค๊ณผ ์—ฌ๋ถ„์˜ ๊ธฐ๊ณ„๋ฅผ ๋ถ™์—ฌ ์ƒˆ๋กœ์šด, ๋” ๋ณต์žกํ•œ ์• ๋กœ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. ์ž…๋ ฅ ์• ๋กœ์™€ ๋‹ฟ์€ ๊ธฐ๊ณ„๋Š” ์ž…๋ ฅ ์ง์„ ๊ตฌ์„ฑ์š”์†Œ๋“ค๋กœ ๋ถ„ํ•ดํ–ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ํ•œ ๋ฐ ๋ชจ์€๋‹ค. ์ด ๋’ค์— ๊น”๋ฆฐ ๋ฐœ์ƒ์€ ๋ชจ๋“  ์ง์˜ ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์ด f์— ๊ณต๊ธ‰๋˜๊ณ  ๋‘ ๋ฒˆ์งธ ๋ถ€๋ถ„์€ ๋นˆ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ์— ์ „๋‹ฌ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋ชจ๋“  ๊ฒƒ์„ ๋‹ค์‹œ ํ•œ ๋ฐ ๋ชจ์œผ๋ฉด ๊ณต๊ธ‰ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ์ง๋“ค์„ ์–ป๋Š”๋ฐ, ๋ชจ๋“  ์ง์˜ ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์ด f๋กœ๋ถ€ํ„ฐ ๋‚˜์˜จ ๋™๋“ฑํ•œ ์ถœ๋ ฅ์œผ๋กœ ์น˜ํ™˜๋˜์–ด ์žˆ๋‹ค. ์ด์ œ ์ด๊ฒƒ์˜ ํƒ€์ž…์„ ์ž๋ฌธํ•ด ๋ณด์ž. ์ž…๋ ฅ ํŠœํ”Œ๋“ค์˜ ํƒ€์ž…์ด (b, d)์ด๊ณ  ์ž…๋ ฅ ์• ๋กœ์˜ ํƒ€์ž…์ด a b c(์ฆ‰ b์—์„œ c๋กœ ๊ฐ€๋Š” ์• ๋กœ)๋ผ ํ•˜์ž. ์ถœ๋ ฅ์˜ ํƒ€์ž…์€? ์• ๋กœ๊ฐ€ ๋ชจ๋“  b๋ฅผ c๋กœ ๋ณ€ํ™˜ํ•˜๋‹ˆ ๋ชจ๋“  ๊ฒƒ์„ ๋‹ค์‹œ ํ•ฉ์น˜๊ณ  ๋‚˜๋ฉด ์ถœ๋ ฅ์˜ ํƒ€์ž…์€ ๋ฐ˜๋“œ์‹œ (c, d) ์—ฌ์•ผ ํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ first ๋กœ๋ด‡์˜ ํƒ€์ž…์€ ๋ฌด์—‡์ธ๊ฐ€? second first ๋กœ๋ด‡์ด ์ดํ•ด๊ฐ€ ๋œ๋‹ค๋ฉด second ๋กœ๋ด‡์€ ์‹์€ ์ฃฝ ๋จน๊ธฐ๋‹ค. first์™€ ๊ฐ™์€ ์ผ์„ ํ•˜๋Š”๋ฐ, ์• ๋กœ f์— ๊ณต๊ธ‰๋˜๋Š” ๊ฒƒ์ด ๋ชจ๋“  ์ž…๋ ฅ ์ง์˜ ์ฒซ ๋ฒˆ์งธ๊ฐ€ ์•„๋‹ˆ๋ผ ๋‘ ๋ฒˆ์งธ ๋ถ€๋ถ„์ด๋‹ค. second ๋กœ๋ด‡์˜ ์ •๋ง ํฅ๋ฏธ๋กœ์šด ์ ์€ ์•ž์„œ์˜ ๋กœ๋ด‡๋“ค๋กœ๋ถ€ํ„ฐ ์œ ๋„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค! ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ํ•„์š”ํ•œ ์• ๋กœ ๋กœ๋ด‡์€ arr, (>>>), first๋ฟ์ด๋‹ค. ๋‚˜๋จธ์ง€๋Š” ์ž์œ ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ํŠœํ”Œ์˜ ๋‘ ์›์†Œ๋ฅผ ๋งž๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ์ด ๋ณด์กฐ ํ•จ์ˆ˜๋ฅผ arr, (>>>), first ๋กœ๋ด‡๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ second ๋กœ๋ด‡์„ ๊ตฌํ˜„ํ•˜๋ผ. *** ์• ๋กœ์˜ ๊ฐ•์  ์ค‘ ํ•˜๋‚˜๋Š” ๋ณ‘๋ ฌ ๊ณ„์‚ฐ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์—๋Š” (***) ๋กœ๋ด‡์ด ์ ๊ฒฉ์ด๋‹ค. ๋‘ ์• ๋กœ f์™€ g๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ (***)๋Š” ์ด๋“ค์„ ํ•ฉ์„ฑํ•ด ์ƒˆ๋กœ์šด ์• ๋กœ๋กœ ๋งŒ๋“œ๋Š”๋ฐ ์ด๋•Œ ์•ž์˜ ๋‘ ๋กœ๋ด‡์—์„œ ๋ณธ ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐ›์นจ ๊ธฐ๊ณ„๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๊ฐœ๋…์ƒ first, second ๋กœ๋ด‡๊ณผ ๋ณ„๋ฐ˜ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค. ์ „๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ƒˆ ์• ๋กœ๋Š” ์ž…๋ ฅ์˜ ์ง์„ ์ทจํ•œ๋‹ค. ๊ทธ ์ง์„ ์ชผ๊ฐœ ๊ฐ๊ฐ์˜ ์ปจใ…‚์ด์–ด ๋ฒจํŠธ์— ๋ณด๋‚ธ๋‹ค. ์—ฌ๊ธฐ์„œ ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ์• ๋กœ ํ•˜๋‚˜, ๋นˆ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ ํ•˜๋‚˜๊ฐ€ ์•„๋‹ˆ๋ผ ๋ณ„๊ฐœ์˜ ๋‘ ์• ๋กœ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์•ˆ ๋  ๊ฒŒ ๋ญ๊ฐ€ ์žˆ๊ฒ ๋Š”๊ฐ€? ์—ฐ์Šต๋ฌธ์ œ (***) ๋กœ๋ด‡์˜ ํƒ€์ž…์€ ๋ฌด์—‡์ธ๊ฐ€? (>>>), first, second ๋กœ๋ด‡์„ ๊ฐ€์ง€๊ณ  (***) ๋กœ๋ด‡์„ ๊ตฌํ˜„ํ•˜๋ผ. &&& Arrow ํด๋ž˜์Šค์˜ ๋งˆ์ง€๋ง‰ ๋กœ๋ด‡์€ (***) ๋กœ๋ด‡๊ณผ ์•„์ฃผ ๋น„์Šทํ•˜๋‹ค. ๊ฒฐ๊ณผ ์• ๋กœ๊ฐ€ ์ง์ด ์•„๋‹ˆ๋ผ ๋‹จ์ผ ์ž…๋ ฅ์„ ์ทจํ•˜๋Š” ๊ฒƒ๋งŒ ๋นผ๋ฉด. ํ•˜์ง€๋งŒ ๊ธฐ๊ณ„์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ ๋˜‘๊ฐ™๋‹ค. ์ž…๋ ฅ์ด ํ•˜๋‚˜๋ฟ์ด๋ผ๋ฉด ์–ด๋–ป๊ฒŒ ์• ๋กœ ๋‘ ๊ฐœ๋กœ ์ผ์„ ํ•œ๋‹ค๋Š” ๊ฑธ๊นŒ? ๊ฐ„๋‹จํ•˜๋‹ค. ์ž…๋ ฅ์„ ๋ณต์‚ฌํ•ด์„œ ๊ทธ ๋ณต์‚ฌ๋ณธ์„ ๊ฐ ๊ธฐ๊ณ„์— ๊ณต๊ธ‰ํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์ž…๋ ฅ์„ ์ง์œผ๋กœ ๋ณต์ œํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ์ด ๋ณต์ œ ํ•จ์ˆ˜์™€ arr, (>>>), *** ๋กœ๋ด‡์„ ์ด์šฉํ•ด์„œ &&& ๋กœ๋ด‡์„ ๊ตฌํ˜„ํ•˜๋ผ. ๋น„์Šทํ•˜๊ฒŒ, ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ &&& ์—†์ด ์žฌ์ž‘์„ฑํ•˜๋ผ. addA f g = f &&& g >>> arr (\(y, z) -> y + z) ํ•จ์ˆ˜๋Š” ์• ๋กœ๋‹ค ์• ๋กœ ๋กœ๋ด‡ 6๊ฐœ๋ฅผ ๋ชจ๋‘ ๋ณด์—ฌ์คฌ์œผ๋‹ˆ, ์ด์ œ Arrow ํด๋ž˜์Šค๋ฅผ ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ํ•˜๋ฉฐ ๋ณด๋‹ค ํ™•์‹คํžˆ ์ดํ•ดํ•˜์ž. ๋ชจ๋‚˜๋“œ ์„ธ๊ณ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์• ๋กœ๋„ ์ข…๋ฅ˜๊ฐ€ ๋‹ค์–‘ํ•˜๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ์• ๋กœ๋Š”? ํ•จ์ˆ˜๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ๋Š”, ํ•จ์ˆ˜์˜ ํƒ€์ž… ์ƒ์„ฑ์ž (->)๋Š” Arrow์˜ ์ธ์Šคํ„ด์Šค๋‹ค. instance Arrow (->) where arr f = f f >>> g = g. f first f = \(x, y) -> (f x, y) ์ž์„ธํžˆ ์‚ดํŽด๋ณด์ž. arr - ํ•จ์ˆ˜๋ฅผ ์• ๋กœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์ž๋ช…ํ•˜๋‹ค. ์‚ฌ์‹ค ํ•จ์ˆ˜ ์ž์ฒด๊ฐ€ ์ด๋ฏธ ์• ๋กœ๋‹ค. (>>>) - ์ฒซ ๋ฒˆ์งธ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ์„ ๋‘ ๋ฒˆ์งธ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ๊ณต๊ธ‰ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ํ•จ์ˆ˜ ํ•ฉ์„ฑ์ด ์ œ๊ฒฉ์ด๋‹ค. first - ์กฐ๊ธˆ ๋ณต์žกํ•˜๋‹ค. ํ•จ์ˆ˜ f๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ์ž…๋ ฅ์˜ ์ง (x, y)๋ฅผ ์ทจํ•ด f๋ฅผ x์— ๋Œ€ํ•ด ์‹คํ–‰ํ•˜๊ณ  y๋Š” ๊ทธ๋Œ€๋กœ ๋†”๋‘๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์—„๋ฐ€ํžˆ๋Š” ์ด๊ฒƒ์ด ์• ๋กœ๋ฅผ ์™„์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ „๋ถ€์ง€๋งŒ, ์• ๋กœ ํƒ€์ž… ํด๋ž˜์Šค๋Š” ๋‚˜๋จธ์ง€ ์„ธ ๋กœ๋ด‡์„ ์—ฌ๋Ÿฌ๋ถ„์ด ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋„ ํ—ˆ์šฉํ•˜๋ฏ€๋กœ ํ•œ ๋ฒˆ ํ•ด๋ณด์ž. first f = \(x, y) -> (f x, y) -- ๋น„๊ต์šฉ second f = \(x, y) -> ( x, f y) -- first์™€ ๋น„์Šท f *** g = \(x, y) -> (f x, g y) -- takes two arrows, and not just one f &&& g = \x -> (f x, g x) -- feed the same input into both functions ๋! ์ด ์ด์ƒ ๊ฐ„๋‹จํ•  ์ˆ˜ ์—†๋‹ค. ์ด๊ฒƒ์ด ํ•จ์ˆ˜์˜ ์• ๋กœ๋กœ์„œ์˜ ๊ณต์‹ ์ธ์Šคํ„ด์Šค๋Š” ์•„๋‹ˆ๋‹ค. ์ง„์งœ๋ฅผ ์›ํ•œ๋‹ค๋ฉด ํ•˜์Šค ์ผˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋“ค์—ฌ๋‹ค๋ณผ ๊ฒƒ. ์• ๋กœ ํ‘œ๊ธฐ ์•ž์˜ Arrow ํŠœํ† ๋ฆฌ์–ผ์—์„œ proc๊ณผ -< ํ‘œ๊ธฐ๋ฅผ ์†Œ๊ฐœํ–ˆ๋‹ค. ์ด๊ฒƒ๋“ค์ด ๊ทธ ๋ชจ๋“  ์• ๋กœ ๋กœ๋ด‡๊ณผ ์–ด๋–ป๊ฒŒ ์„ž์ด๋Š” ๊ฑธ๊นŒ? ์Šฌํ”„๊ฒŒ๋„ ๋ชจ๋‚˜๋“œ do ํ‘œ๊ธฐ๋ณด๋‹จ ๋œ ์ง๊ด€์ ์ด์ง€๋งŒ, ํ•œ ๋ฒˆ ์‚ดํŽด๋ณด์ž. proc (์• ๋กœ ์ถ”์ƒํ™”)๋Š” ์ผ์ข…์˜ ๋žŒ๋‹ค๋‹ค. ํ•จ์ˆ˜ ๋Œ€์‹  ์• ๋กœ๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. -< (์• ๋กœ ์ ์šฉ)์€ ํ‘œํ˜„์‹์˜ ๊ฐ’์„ ์• ๋กœ์— ๊ณต๊ธ‰ํ•œ๋‹ค. addA :: Arrow a => a b Int -> a b Int -> a b Int addA f g = proc x -> do y <- f -< x z <- g -< x returnA -< y + z addA :: Arrow a => a b Int -> a b Int -> a b Int addA f g = arr (\ x -> (x, x)) >>> first f >>> arr (\ (y, x) -> (x, y)) >>> first g >>> arr (\ (z, y) -> y + z) addA :: Arrow a => a b Int -> a b Int -> a b Int addA f g = f &&& g >>> arr (\ (y, z) -> y + z) โ€ป ์›๋ฌธ์—์„œ ์ด ์ ˆ์€ ๋ฏธ์™„์„ฑ์ž…๋‹ˆ๋‹ค. Maybe ํ•„ํ„ฐ ๋ชจ๋“  ๋ชจ๋‚˜๋“œ๋Š” ์• ๋กœ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. โ€ป ์›๋ฌธ์—์„œ ์ด ์ ˆ์€ ๋ฏธ์™„์„ฑ์ž…๋‹ˆ๋‹ค. ์• ๋กœ์˜ ํ™œ์šฉ ์ด์ฏค์ด๋ฉด ์• ๋กœ์˜ ๊ธฐ์ œ๋ฅผ ์ถฉ๋ถ„ํžˆ ํŒŒ์•…ํ–ˆ์„ ํ…Œ๋‹ˆ ์• ๋กœ๊ฐ€ ์–ด๋””์— ์œ ์šฉํ•˜๋Š๋ƒ๋Š” ์ค‘์š”ํ•œ ์งˆ๋ฌธ์„ ํ•ด๋ณด์ž. ์ŠคํŠธ๋ฆผ ์ฒ˜๋ฆฌ(stream processing) โ€ป ์›๋ฌธ์—์„œ ์ด ์ ˆ์€ ๋ฏธ์™„์„ฑ์ž…๋‹ˆ๋‹ค. ๋ˆ„์ˆ˜ ๋ฐฉ์ง€(avoiding leaks) ์• ๋กœ๋Š” ์›๋ž˜ Swierstra์™€ Duponcheel์˜ ํšจ์œจ์  ํŒŒ์„œ ์„ค๊ณ„์—์„œ ๋‚˜์˜จ ๊ฒƒ์ด๋‹ค. 1 ๊ทธ ์„ค๊ณ„์˜ ์žฅ์ ์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋‚˜๋“œ ํŒŒ์„œ๊ฐ€ ์ •ํ™•ํžˆ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด์ž. ๋‹จ์ผ ๋‚ฑ๋ง์„ ํŒŒ์‹ฑ ํ•  ๋•Œ๋Š” ๋‹ค๋‹ฅ๋‹ค๋‹ฅ ๋ถ™์€ ๋ชจ๋‚˜๋“œ ํŒŒ์„œ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณค ํ•œ๋‹ค. Parsec์„ ์˜ˆ๋กœ ๋“ค๋ฉด, ํŒŒ์„œ ๋ฌธ์ž์—ด "word"๋Š” ์ด๋ ‡๊ฒŒ ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. word = do char 'w' >> char 'o' >> char 'r' >> char 'd' return "word" ๊ฐ ๋ฌธ์ž๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์‹œ๋„ํ•˜๊ณ , ๋งŒ์•ฝ ์ž…๋ ฅ์ด "worg"๋ผ๋ฉด ์ฒ˜์Œ ์„ธ ํŒŒ์„œ๋Š” ์„ฑ๊ณตํ•˜์ง€๋งŒ ๋งˆ์ง€๋ง‰ ํŒŒ์„œ๋Š” ์‹คํŒจํ•˜์—ฌ ์ „์ฒด ๋ฌธ์ž์—ด "word" ํŒŒ์„œ๊ฐ€ ์‹คํŒจํ•œ๋‹ค. ๋‘ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ํŒŒ์‹ฑ ํ•˜๋ ค๋ฉด ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์ƒˆ ํŒŒ์„œ๋ฅผ ๋งŒ๋“ค๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์‹œ๋„ํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํŒŒ์„œ๊ฐ€ ์‹คํŒจํ•˜๋ฉด ๊ฐ™์€ ์ž…๋ ฅ์„ ๊ฐ€์ง€๊ณ  ๋‹ค์Œ ๊ฒƒ์„ ์‹œ๋„ํ•œ๋‹ค. ab = do char 'a' <|> char 'b' <|> char 'c' "c"๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ํŒŒ์‹ฑ ํ•˜๋ ค๋ฉด a์™€ b๋ฅผ ๋‘˜ ๋‹ค ์‹œ๋„ํ•ด ๋ด์•ผ ํ•œ๋‹ค. one = do char 'o' >> char 'n' >> char 'e' return "one" two = do char 't' >> char 'w' >> char 'o' return "two" three = do char 't' >> char 'h' >> char 'r' >> char 'e' >> char 'e' return "three" nums = do one <|> two <|> three ์ด๋ ‡๊ฒŒ ํŒŒ์„œ ์„ธ ๊ฐœ๊ฐ€ ์žˆ์œผ๋ฉด ๋งˆ์ง€๋ง‰ ํŒŒ์„œ๊ฐ€ ์‹คํŒจํ•˜๊ธฐ ์ „์—๋Š” ๋ฌธ์ž์—ด "four"๊ฐ€ ์ด ํŒŒ์„œ์— ์‹คํŒจํ•˜๋Š”์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค. ํŒŒ์„œ๋“ค ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ž…๋ ฅ์˜ ์ƒ๋‹น ๋ถ€๋ถ„์„ ์žก์•„๋จน์ง€๋งŒ ์‹คํŒจํ•œ๋‹ค๋ฉด ๋งˆ์ง€๋ง‰ ํŒŒ์„œ๊ฐ€ ์‹คํŒจํ•  ๋•Œ๊นŒ์ง€ ํŒŒ์„œ์˜ ์—ฐ์‡„๋ฅผ ์ด์–ด๊ฐ€์•ผ ํ•œ๋‹ค. ๋’ท๋ถ€๋ถ„์˜ ํŒŒ์„œ๋“ค์ด ๋ฐ›์„ ์ˆ˜๋„ ์žˆ๋Š” ๋ชจ๋“  ์ž…๋ ฅ์€ ๋ฐ˜๋“œ์‹œ ๋ฉ”๋ชจ๋ฆฌ์— ๋‚จ์•„์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์—ฌ๋Ÿฌ๋ถ„์ด ๋‚™๊ด€ํ•œ ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ๋งŽ์€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๊ฒƒ์„ ์ฃผ๋กœ ๊ณต๊ฐ„ ๋ˆ„์ˆ˜(space leak)์ด๋ผ ๋ถ€๋ฅธ๋‹ค. ๋ชจ๋‚˜๋“œ ํŒŒ์„œ์˜ ์ผ๋ฐ˜์ ์ธ ํŒจํ„ด์€ ํ•œ ์„ ํƒ์ง€๊ฐ€ ์‹คํŒจํ•˜๋ฉด ๋‹ค๋ฅธ ์„ ํƒ์ง€๋Š” ์„ฑ๊ณตํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿผ ๋ญ๊ฐ€ ๋” ์ข‹์€๊ฐ€? Swierstra & Duponcheel (1996)์€ ๋˜‘๋˜‘ํ•œ ํŒŒ์„œ๋ผ๋ฉด ๋งจ ์ฒ˜์Œ ๋ฌธ์ž๋ฅผ ๋ณด๋Š” ์ฆ‰์‹œ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ–ˆ๋‹ค. ๊ฐ€๋ น ์œ„์˜ nums ํŒŒ์„œ์—์„œ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž ํŒŒ์„œ๋“ค์˜ ์„ ํƒ์€ "one"์— ๋Œ€ํ•ด 'o', "two"์™€ "three"์— ๋Œ€ํ•ด 't'๋กœ ์ œํ•œ๋œ๋‹ค. ์ด ๋˜‘๋˜‘ํ•œ ํŒŒ์„œ๋Š” ๋˜ํ•œ ์ž…๋ ฅ์„ ์ฆ‰์‹œ ๊ฐ€๋น„์ง€ ์ฝœ๋ ‰ํŒ…ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์•ž์„ ๋‚ด๋‹ค๋ด์„œ ๋‹ค๋ฅธ ํŒŒ์„œ๋“ค์ด ๊ทธ ์ž…๋ ฅ์„ ์ทจํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๋ณด๊ณ  ๊ทธ๋Ÿด ์ˆ˜ ์—†์œผ๋ฉด ์ž…๋ ฅ์„ ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ์ƒˆ๋กœ์šด ํŒŒ์„œ๋Š” ๋ชจ๋‚˜๋“œ ํŒŒ์„œ์™€ ๋งŽ์ด ์œ ์‚ฌํ•˜์ง€๋งŒ ์ฃผ๋œ ์ฐจ์ด์ ์€ ์ •์ ์ธ ์ •๋ณด๋ฅผ ๋‚ด๋ณด๋‚ธ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋ฌด์—‡์„ ํŒŒ์‹ฑ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋„ ์•Œ๋ ค์ค€๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํฐ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ์ด๊ฒƒ์€ ๋ชจ๋‚˜๋“œ ์ธํ„ฐํŽ˜์ด์Šค์™€ ์–ด์šธ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค. ๋ชจ๋‚˜๋“œ๋Š” (a -> m b)์ธ๋ฐ ํ•จ์ˆ˜์—๋งŒ ๊ธฐ๋ฐ˜ํ•œ๋‹ค. ์ •์  ์ •๋ณด๋ฅผ ๋ถ™์ผ ๋ฐฉ๋ฒ•์ด ์—†๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์—๊ฒ ๋‹จ ํ•œ ๊ฐœ์˜ ์„ ํƒ์ง€๋งŒ์ด ์žˆ๋‹ค. ์–ด๋–ค ์ž…๋ ฅ์„ ํˆฌ์ฒ™ํ•˜๊ณ , ํ†ต๊ณผํ•˜๋Š”์ง€ ์‹คํŒจํ•˜๋Š”์ง€ ์ง€์ผœ๋ณธ๋‹ค. ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ๋Š” ์˜ค๋žซ๋™์•ˆ ๋ชจ๋‚˜๋“œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ฒ”์šฉ์ ์ธ ๋„๊ตฌ๋กœ ๋‚ด์„ธ์› ์ง€๋งŒ ๊ทธ ์ธํ„ฐํŽ˜์ด์Šค์— ์–ด์šธ๋ฆฌ์ง€ ์•Š๋Š”, ๊ทธ๋Ÿฌ๋ฉด์„œ๋„ ํŠน๋ณ„ํžˆ ์œ ์šฉํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฑธ ์•Œ์•„๋‚ธ ๊ฒƒ์€ ์ผ์ข…์˜ ์ขŒ์ ˆ์ด์—ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์• ๋กœ๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค. John Hughes์˜ Generalising monads to arrows์€ ์ƒˆ๋กญ๊ณ  ๋ณด๋‹ค ์œ ์—ฐํ•œ ๋„๊ตฌ๋กœ์„œ ์• ๋กœ ์ถ”์ƒํ™”๋ฅผ ์ œ์•ˆํ–ˆ๋‹ค. ์ •์  ํŒŒ์„œ์™€ ๋™์  ํŒŒ์„œ Swierstra & Duponcheel์˜ ํŒŒ์„œ๋ฅผ ์• ๋กœ์˜ ๊ด€์ ์—์„œ ๋‚ฑ๋‚ฑ์ด ํŒŒํ—ค์ณ ๋ณด์ž. ์ด ํŒŒ์„œ๋Š” ๋‘ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ๊ฐ€์ง„๋‹ค. ์ž…๋ ฅ์ด ํŒŒ์‹ฑ์„ ์‹œ๋„ํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ๋น ๋ฅด๊ณ  ์ •์ ์ธ ํŒŒ์„œ์™€ ์‹ค์ œ๋กœ ํŒŒ์‹ฑ์„ ํ•˜๋Š” ๋Š๋ฆฌ๊ณ  ๋™์ ์ธ ํŒŒ์„œ๋‹ค. data Parser s a b = P (StaticParser s) (DynamicParser s a b) data StaticParser s = SP Bool [s] newtype DynamicParser s a b = DP ((a,[s]) -> (b,[s])) ์ •์  ํŒŒ์„œ๋Š” ๋นˆ ์ž…๋ ฅ์„ ๋ฐ›์•„๋“ค์ผ์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ํ”Œ๋ž˜๊ทธ, ๊ฐ€๋Šฅํ•œ ์‹œ์ž‘ ๋ฌธ์ž๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์ผ ๋ฌธ์ž์— ๋Œ€ํ•œ ์ •์  ํŒŒ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค. spCharA :: Char -> StaticParser Char spCharA c = SP False [c] ์ด ํŒŒ์„œ๋Š” ๋นˆ ๋ฌธ์ž์—ด์„ ์ˆ˜์šฉํ•˜์ง€ ์•Š๊ณ (False), ๊ฐ€๋Šฅํ•œ ์‹œ์ž‘ ๋ฌธ์ž๋“ค์˜ ๋ฆฌ์ŠคํŠธ์—๋Š” ์˜ค์ง 'c'๋งŒ ๋“ค์–ด์žˆ๋‹ค. ์ด ์ ˆ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ ๊ฒ€์ฆ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋™์  ํŒŒ์„œ๋Š” ์กฐ๊ธˆ ๋” ๋ณต์žกํ•˜๋‹ค. ์šฐ๋ฆฌ์—๊ฒŒ ๋ณด์ด๋Š” ๊ฒƒ์€ (a, [s])์—์„œ (b, [s])๋กœ ๊ฐ€๋Š” ํ•จ์ˆ˜๋‹ค. ๋‘ ํŒŒ์„œ์˜ ์—ฐ๊ณ„๋ผ๋Š” ๊ด€์ ์—์„œ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ด๋‹ค. ๊ฐ ํŒŒ์„œ๋Š” ์ด์ „ ํŒŒ์„œ(a)์˜ ๊ฒฐ๊ณผ๋ฅผ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์˜ ๋‚˜๋จธ์ง€ ์กฐ๊ฐ๋“ค([s])๊ณผ ํ•จ๊ป˜ ์„ญ์ทจํ•œ๋‹ค. a๋กœ ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•ด์„œ ์ž์‹ ์˜ ๊ฒฐ๊ณผ b๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๊ณ , ๋ฌธ์ž์—ฌ ๋ฅด์ด ์ผ๋ถ€๋ฅผ ์ทจํ•ด ๊ทธ๊ฒƒ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์‹ค์ œ ์˜ˆ์‹œ๋กœ ๋™์  ํŒŒ์„œ (Int, String) -> (Int, String)์„ ์ƒ๊ฐํ•ด ๋ณด์ž. Int๋Š” ์ง€๊ธˆ๊นŒ์ง€ ํŒŒ์‹ฑ ๋œ ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ด๋“ค ์ค‘ ๋ช‡ ๊ฐœ๋ฅผ ์—ฐ๊ณ„ํ•˜๊ณ  ๋ฌธ์ž์—ด "cake"์— ์ ์šฉํ•˜๋ฉด ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. result remaining before 0 cake after first parser 1 ake after second parser 2 ke after third parser 3 e ์š”์ ์€ ๋™์  ํŒŒ์„œ๊ฐ€ ํˆฌ์žก์„ ๋›ด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด์ „ ํŒŒ์„œ์˜ ์ถœ๋ ฅ์— ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•˜๊ณ (ํ˜•์‹์ ์œผ๋กœ๋Š” a -> b), ์ž…๋ ฅ ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ฅผ ์ทจํ•œ๋‹ค. ([s] -> [s]) ๋”ฐ๋ผ์„œ ๊ทธ ํƒ€์ž…์€ DP ((a, [s]) -> (b, [s]))์ด๋‹ค. ์ด์ œ ๋‹จ์ผ ๋ฌธ์ž์— ๋Œ€ํ•œ ๋™์  ํŒŒ์„œ๋ฅผ ๋ณด์ž. ์ฒซ ๋ฒˆ์งธ ์ž‘์—…์€ ์ž๋ช…ํ•˜๋‹ค. ์ด์ „ ํŒŒ์„œ์˜ ์ถœ๋ ฅ์„ ๋ฌด์‹œํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ง‰ ํŒŒ์‹ฑ ํ•œ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋‹ค์Œ์—๋Š” ์ŠคํŠธ๋ฆผ์—์„œ ๋ฌธ์ž ํ•˜๋‚˜๋ฅผ ๋ฝ‘์•„์˜จ๋‹ค. dpCharA :: Char -> DynamicParser Char Char Char dpCharA c = DP (\(_,x:xs) -> (x, xs)) ์—ฌ๊ธฐ์„œ ์งˆ๋ฌธ์„ ๋ช‡ ๊ฐœ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋ น ์ด์ „ ํŒŒ์„œ์˜ ์ถœ๋ ฅ์„ ๊ทธ๋ƒฅ ๋ฌด์‹œํ•  ๊ฑฐ๋ผ๋ฉด ๊ทธ๊ฑธ ๋ฐ›๋Š” ๋ชฉ์ ์ด ๋ฌด์—‡์ธ๊ฐ€? ์ง€๊ธˆ ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์„ ์˜ ๋‹ต๋ณ€์€ "์ž ๊น ๊ธฐ๋‹ค๋ ค๋ด"์ด๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ๋ชจ๋‚˜๋“œ์— ์ต์ˆ™ํ•˜๋‹ค๋ฉด bind ์—ฐ์‚ฐ์ž (>>=)๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ผ. bind๋Š” ๊ทธ ์ž์ฒด๋กœ๋„ ์•„์ฃผ ์œ ์šฉํ•˜์ง€๋งŒ ๊ฐ€๋” ๋‘ ๋ชจ๋‚˜๋“œ ๊ณ„์‚ฐ์„ ์—ฐ๊ณ„ํ•  ๋•Œ ์šฐ๋ฆฌ๋Š” ์ต๋ช… bind์ธ (>>)์„ ์‚ฌ์šฉํ•ด์„œ ์ฒซ ๋ฒˆ์งธ ๊ณ„์‚ฐ์˜ ์ถœ๋ ฅ์„ ๋ฌด์‹œํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ๊ฐ™์€ ์ƒํ™ฉ์ด๋‹ค. ํฅ๋ฏธ๋กœ์šด ๋Šฅ๋ ฅ์˜ ์ผ๋ถ€๋ฅผ ์†์•„๊ท€์— ์ฅ๊ณ  ์žˆ์ง€๋งŒ ๋‹น์žฅ์€ ์“ฐ์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿผ ๋‹ค์Œ ์งˆ๋ฌธ์€ ๋™์  ํŒŒ์„œ์—์„œ, ๋ฐฉ๊ธˆ ์ŠคํŠธ๋ฆผ์—์„œ ๋–ผ์–ด๋‚ธ ๋ฌธ์ž๊ฐ€ ํŒŒ์‹ฑ ํ•  ๋ฌธ์ž์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•˜๋Š๋ƒ๋Š” ๊ฒƒ์ด๋‹ค. x == c๋ฅผ ๊ฒ€์‚ฌํ•ด์•ผ ํ•˜๋‚˜? ์•„๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ด๊ฒƒ์ด ํ•ต์‹ฌ์ด๋‹ค. ์ด ์ž‘์—…์€ ์ •์  ํŒŒ์„œ๊ฐ€ ์ด๋ฏธ ๊ฒ€์‚ฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š” ์—†๋‹ค. ์–ด์จŒ๋“  ์ด๊ฒƒ๋“ค์„ ํ•œ ๋ฐ ๋ชจ์œผ๋ฉด, ์ด๊ฒƒ์ด ๋‹จ์ผ ๋ฌธ์ž์— ๋Œ€ํ•œ S+D ์Šคํƒ€์ผ ํŒŒ์„œ๋‹ค. charA :: Char -> Parser Char Char Char charA c = P (SP False [c]) (DP (\(_,x:xs) -> (x, xs))) ์• ๋กœ ๊ฒฐํ•ฉ๊ธฐ (๋กœ๋ด‡) ์ง€๊ธˆ๊นŒ์ง€ ๋…๋ฆฝ๋œ ๋…ธ์„ ์„ ๊ฑท๋Š” ๋‘ ๊ฐœ์˜ ์‚ฌ๊ณ ๋ฅผ ์‚ดํŽด๋ดค๋‹ค. ํ•œํŽธ์œผ๋กœ๋Š” ์• ๋กœ ๊ธฐ์ œ(์œ„์˜ ๊ฒฐํ•ฉ๊ธฐ/๋กœ๋ด‡)๋ฅผ ์‚ดํŽด๋ดค๋Š”๋ฐ, ๋ฌด์—‡์„ ์œ„ํ•œ ๊ฒƒ์ธ์ง€๋Š” ์ •ํ™•ํžˆ ๋ชจ๋ฅธ๋‹ค. ํ•œํŽธ์œผ๋กœ๋Š” Arrow ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ผ์ข…์˜ ํŒŒ์„œ๋ฅผ ์†Œ๊ฐœํ–ˆ๋‹ค. ๊ทธ ๋ชฉ์ ์€ ๊ณต๊ฐ„ ๋ˆ„์ˆ˜๋ฅผ ํ”ผํ•˜๋Š” ๊ฒƒ์ด๊ณ  ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด ๋น ๋ฅธ ์ •์  ํŒŒ์„œ์™€ ๋Š๋ฆฐ ๋™์  ํŒŒ์„œ๋ฅผ ๋ถ„๋ฆฌํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฒƒ์ด ๊ทธ ๋ชจ๋“  ์• ๋กœ ๊ธฐ์ œ์™€ ์–ด๋–ป๊ฒŒ ์—ฐ๊ด€๋˜๋Š”์ง€๋Š” ์•„์ง ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. ์ด ์ ˆ์—์„œ๋Š” ์šฐ๋ฆฌ์˜ ์ง€์‹์— ์žˆ๋Š” ํ‹ˆ์„ ๋ฉ”๊ฟ” ๋‘ ๋…ธ์„ ์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋ ค๊ณ  ํ•œ๋‹ค. ์šฐ๋ฆฌ๋Š” Parser s๋ฅผ ์œ„ํ•œ Arrow ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ , ๊ทธ๋Ÿผ์œผ๋กœ์จ ๋ฌด์—‡์ด ์• ๋กœ๋ฅผ ์œ ์šฉํ•˜๊ฒŒ ๋งŒ๋“œ๋Š”์ง€ ์งง์€ ๊ฒฝํ—˜์„ ์ฃผ๋ ค๊ณ  ํ•œ๋‹ค. instance Arrow (Parser s) where ์šฐ๋ฆฌ๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๊ฒƒ๋“ค ์ค‘ ํ•˜๋‚˜๋Š” ์ž„์˜์˜ ํ•จ์ˆ˜๋ฅผ ํŒŒ์‹ฑ ์• ๋กœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. "parse"๋ผ๋Š” ์šฉ์–ด๋ฅผ ๋Š์Šจํ•œ ์˜๋ฏธ๋กœ ์“ฐ๊ฒ ๋‹ค. ๊ฒฐ๊ณผ ์• ๋กœ๋Š” ์˜ค์ง ๋นˆ ๋ฌธ์ž์—ด๋งŒ์„ ๋ฐ›์•„๋“ค์ธ๋‹ค(์ด๊ฒƒ์˜ ์‹œ์ž‘ ๋ฌธ์ž๋“ค์˜ ์ง‘ํ•ฉ์€ []์ด๋‹ค). ์œ ์ผํ•œ ์—…๋ฌด๋Š” ์ด์ „ ํŒŒ์‹ฑ ์• ๋กœ์˜ ์ถœ๋ ฅ์„ ์ทจํ•ด ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ ์•„๋ฌด ์ž…๋ ฅ๋„ ์ทจํ•˜์ง€ ์•Š๋Š”๋‹ค. arr f = P (SP True []) (DP (\(b, s) -> (f b, s))) first ๊ฒฐํ•ฉ๊ธฐ๋Š” ๋ณด๋‹ค ์ง๊ด€์ ์ด๋‹ค. ์•ž์˜ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ๋ฅผ ๋– ์˜ฌ๋ฆฌ์ž. ์šฐ๋ฆฌ๋Š” ์ž…๋ ฅ์˜ ์ง (b, d)์„ ์ˆ˜์šฉํ•˜๋Š” ์ƒˆ๋กœ์šด ํŒŒ์„œ๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. ์ž…๋ ฅ์˜ ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์€ b๋กœ์„œ, ์šฐ๋ฆฌ๊ฐ€ ์‹ค์ œ๋กœ ํŒŒ์‹ฑ ํ•˜๋ ค๋Š” ๊ฒƒ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ถ€๋ถ„์€ ์™„์ „ํžˆ ๊ทธ๋Œ€๋กœ ๋„˜์–ด๊ฐ„๋‹ค. first (P sp (DP p)) = (P sp (DP (\((b, d),s) -> let (c, s') = p (b, s) in ((c, d),s')))) ํ•œํŽธ (>>>)์˜ ๊ตฌํ˜„์€ ์ƒ๊ฐ์„ ์ข€ ํ•ด๋ด์•ผ ํ•œ๋‹ค. ๋‘ ํŒŒ์„œ๋ฅผ ์ทจํ•ด์„œ ๊ฐ๊ฐ์˜ ์ •์  ํŒŒ์„œ์™€ ๋™์  ํŒŒ์„œ๋ฅผ ํ†ตํ•ฉํ•˜๋Š” ํ•ฉ์„ฑ ํŒŒ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. (P (SP empty1 start1) (DP p1)) >>> (P (SP empty2 start2) (DP p2)) = P (SP (empty1 && empty2) (if not empty1 then start1 else start1 `union` start2)) (DP (p2.p1)) ๋™์  ํŒŒ์„œ๋“ค์„ ํ•ฉ์„ฑํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ๋‹ค. ํ•จ์ˆ˜ ํ•ฉ์„ฑ์„ ํ•˜๋ฉด ๋œ๋‹ค. ์ •์  ํŒŒ์„œ๋“ค์„ ํ•ฉ์น˜๋Š” ๊ฒƒ์€ ์กฐ๊ธˆ ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค. ๋ฌด์—‡๋ณด๋‹ค๋„ ๋‘ ํŒŒ์„œ ๋ชจ๋‘ ๋นˆ ๋ฌธ์ž์—ด์„ ์ˆ˜์šฉํ•˜๋ฉด ํ•ฉ์„ฑ๋œ ํŒŒ์„œ๋„ ๊ทธ๋ ‡๊ฒŒ ๋ฐ–์— ํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋Ÿผ ์‹œ์ž‘ ๊ธฐํ˜ธ๋Š”? ์Œ, ํŒŒ์„œ๋“ค์€ ์ˆœ์ฐจ์ ์ผ ํ…Œ๋‹ˆ ํ•ฉ์„ฑ ํŒŒ์„œ์˜ ์‹œ์ž‘ ๊ธฐํ˜ธ๋Š” start์ด๊ฒ ์ง€? ์ด๋ณด๊ฒŒ, ์ธ์ƒ์€ ๋…น๋กํ•˜์ง€ ์•Š๋‹ค. ํŒŒ์„œ๋“ค์ด ๋นˆ ๋ฌธ์ž์—ด์„ ์ˆ˜์šฉํ•  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ ํŒŒ์„œ๊ฐ€ ๋นˆ ์ž…๋ ฅ์„ ์ˆ˜์šฉํ•˜๋Š” ๊ฐ€๋Šฅ์„ฑ์„ ๊ณ ๋ คํ•˜๋ ค๋ฉด ๋‘ ํŒŒ์„œ์˜ ์‹œ์ž‘ ๊ธฐํ˜ธ๋“ค์„ ์ „๋ถ€ ์ˆ˜์šฉํ•ด์•ผ ํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์œ„์˜ charA ํŒŒ์„œ๋ฅผ ๋ณด์ž. charA 'o' >>> charA 'n' >>> charA 'e'์˜ ๊ฒฐ๊ณผ๋Š”? ๊ทธ ํ•ฉ์„ฑ ํŒŒ์„œ๋ฅผ ๊ฐ„๋žตํ™”ํ•œ ๋ฒ„์ „์„ ์ž‘์„ฑํ•˜๋ผ. ๊ทธ ํŒŒ์„œ๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด์„ ์ˆ˜์šฉํ•˜๋Š”๊ฐ€? ์‹œ์ž‘ ๊ธฐํ˜ธ๋Š”? ์ด๊ฒƒ์˜ ๋™์  ํŒŒ์„œ๋Š” ๋ฌด์—‡์ธ๊ฐ€? ๊ทธ๋ž˜์„œ ์• ๋กœ๊ฐ€ ์ฃผ๋Š” ๊ฒƒ์€? ์šฐ๋ฆฌ์˜ Arrow ํƒ€์ž…์„ ๋‹ค์‹œ ๋ณด๊ณ  ์ •์  ํŒŒ์„œ ๋ถ€๋ถ„์„ ๊ฐ€๋ฆฌ๋ฉด ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์• ๋กœ ์ธ์Šคํ„ด์Šค์™€ ๋งŽ์ด ๋‹ฎ์•„ ์žˆ๋‹ค. arr f = \(b, s) -> (f b, s) first p = \((b, d), s) -> let (c, s') = p (b, s) in ((c, d), s')) p1 >>> p2 = p2. p1 ์—ฌ๊ธฐ์—” ๋œฌ๊ธˆ์—†์ด ๋ณ€์ˆ˜ s๊ฐ€ ๋“ฑ์žฅํ•˜๋Š”๋ฐ, ์ •์˜๋ฅผ ๋‹ค์†Œ ๋‚ฏ์„ค๊ฒŒ ๋งŒ๋“ค์ง€๋งŒ ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ์™€ ๊ณ„์‚ฐ ๊ธฐ๊ณ„์˜ ์œค๊ณฝ์„ ์–ด๋ ดํ’‹์ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์‹ค ์ด๊ฒƒ์€ ๊ฑฐ์˜ State ๋ชจ๋‚˜๋“œ์— ๋Œ€ํ•œ ์• ๋กœ์˜ ์ธ์Šคํ„ด์Šค์™€ ๋‹ฎ์•„ ์žˆ๋‹ค. (let f :: b -> c, p :: b -> State s c and . actually be <=<. ์ข‹๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฑด ๊ณ ์ „์ ์ธ ๋ชจ๋‚˜๋“œ ์Šคํƒ€์ผ์˜ bind๋กœ๋„ ํ•  ์ˆ˜ ์žˆ๋˜ ๊ฒƒ์ด๊ณ  first๋Š” ์‰ฝ๊ฒŒ ํŒจํ„ด ๋งค์นญ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ดํ•œ ๋ณด์กฐ ํ•จ์ˆ˜๊ฐ€ ๋˜์—ˆ์„ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค์‹œ ์ƒ๊ฐํ•ด ๋ณด์ž. ์šฐ๋ฆฌ์˜ Parser ํƒ€์ž…์€ ๋‹จ์ˆœํ•œ ๋™์  ํŒŒ์„œ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์ •์  ํŒŒ์„œ๋„ ๋“ค์–ด์žˆ๋‹ค. arr f = SP True [] first sp = sp (SP empty1 start1) >>> (SP empty2 start2) = (SP (empty1 && empty2) (if not empty1 then start1 else start1 `union` start2)) ์ด๊ฑด ์ ˆ๋Œ€๋กœ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ๊ทธ์ € ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ช‡ ๊ฐœ๋ฅผ ์ด๋ฆฌ์ €๋ฆฌ ๋ฐ€์–ด๋‚ผ ๋ฟ์ด๋‹ค. ํ•˜์ง€๋งŒ ์• ๋กœ ๋น„์œ  ์—ญ์‹œ ์ž˜ ๋“ค์–ด๋งž๊ณ , ์šฐ๋ฆฌ๋Š” ์ด๊ฑธ ์• ๋กœ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํฌ์žฅํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ํƒ€์ž…์„ ํ•ฉ์„ฑํ•  ๋•Œ, ์šฐ๋ฆฌ๋Š” 1:2์˜ ์ด์ต์„ ์–ป๋Š”๋‹ค. ์ •์  ํŒŒ์„œ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ๋™์  ํŒŒ์„œ๋ฅผ ๋”ฐ๋ผ๋‹ค๋‹Œ๋‹ค. Arrow ์ธํ„ฐํŽ˜์ด์Šค ๋•์— ์šฐ๋ฆฌ๋Š” ๋‘ ํŒŒ์„œ๋ฅผ ํ•œ ๋‹จ์œ„๋กœ์„œ ํˆฌ๋ช…ํ•˜๊ณ  ๋™์‹œ์ ์œผ๋กœ ํ•ฉ์„ฑ ๋ฐ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‹จ์œ„๋ฅผ ์ „ํ†ต์ ์ธ ๊ท ์ผ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์šด์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ๋ชจ๋‚˜๋“œ๋„ ์• ๋กœ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค ์• ๋กœ์˜ ์ง„์ •ํ•œ ์œ ์—ฐํ•จ์€ ๋ชจ๋‚˜๋“œ๊ฐ€ ์•„๋‹Œ ๊ฒƒ๋“ค์—์„œ ๋‚˜์˜จ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” ๊ทธ์ € ์ง€์ €๋ถ„ํ•œ ๋ฌธ๋ฒ•์ผ ๋ฟ์ด๋‹ค. -- Philippa Cowderoy ๋ชจ๋“  ๋ชจ๋‚˜๋“œ๋Š” ์• ๋กœ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ๋ฐํ˜€์กŒ๋‹ค. ๋‹ค์Œ์€ ์›๋ž˜์˜ ์• ๋กœ ๋…ผ๋ฌธ์—์„œ ๋”ฐ์˜จ ์ฃผ์š” ๋ฌธ๊ตฌ๋‹ค. Just as we think of a monadic type m a as representing a 'computation delivering an a '; so we think of an arrow type a b c, (that is, the application of the parameterised type a to the two parameters b and c) as representing 'a computation with input of type b delivering a c'; arrows make the dependence on input explicit. (์šฐ๋ฆฌ๊ฐ€ ๋ชจ๋‚˜๋“œ ํƒ€์ž… m์ด 'a๋ฅผ ๋ฐฐ๋‹ฌํ•˜๋Š” ๊ณ„์‚ฐ'์„ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์• ๋กœ ํƒ€์ž… a b c (์ฆ‰ ๋งค๊ฐœํ™” ํƒ€์ž… a๋ฅผ ๋‘ ๋งค๊ฐœ๋ณ€์ˆ˜ b์™€ c์— ์ ์šฉํ•˜๋Š” ๊ฒƒ)์ด 'a c๋ฅผ ๋ฐฐ๋‹ฌํ•˜๋Š” b ํƒ€์ž…์˜ ์ž…๋ ฅ์ด ์žˆ๋Š” ๊ณ„์‚ฐ'์„ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ž๋Š” ๊ฒƒ์ด๋‹ค. ์• ๋กœ๋Š” ์ž…๋ ฅ์— ๊ด€ํ•œ ์˜์กด์„ฑ์„ ๋ช…์‹œ์ ์ธ ๊ฒƒ์œผ๋กœ ๋งŒ๋“ ๋‹ค.) ์• ๋กœ๋ฅผ ๋ณด๋Š” ํ•œ ๊ฐ€์ง€ ๊ด€์ ์€ ์˜์–ด๊ฐ€ ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ๋™์‚ฌ๋ฅผ ๋ช…์‚ฌํ™”ํ•˜๋Š” ๊ฑธ ํ—ˆ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "I had a chat with them" ๋Œ€ "I chatted with them"์ฒ˜๋Ÿผ. ์• ๋กœ๊ฐ€ ๋ฐ”๋กœ ์ด๋Ÿฐ ๊ฒƒ์œผ๋กœ, a์—์„œ b๋กœ ๊ฐ€๋Š” ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ์ด ๊ฐ’์€ a์—์„œ b๋กœ ๊ฐ€๋Š” ์ผ๊ธ‰ ๋ณ€ํ™˜์ด๋‹ค. ์• ๋กœ ์‹ค์ „ ์• ๋กœ๋Š” ์ƒ๋Œ€์ ์œผ๋กœ ์ƒˆ๋กœ์šด ์ถ”์ƒํ™”์ง€๋งŒ, ์ด๋ฏธ ํ•˜์Šค ์ผˆ ์„ธ๊ณ„์—์„œ ๋งŽ์€ ์šฉ๋ก€๊ฐ€ ๋ฐœ๊ฒฌ๋˜์—ˆ๋‹ค. Hughes์˜ ์• ๋กœ ์Šคํƒ€์ผ ํŒŒ์„œ๋Š” ๊ทธ์˜ 2000๋…„ ๋…ผ๋ฌธ์—์„œ ์ฒ˜์Œ ์†Œ๊ฐœ๋˜์–ด๋Š” ๋ฐ ์“ธ๋งŒํ•œ ๊ตฌํ˜„์€ 2005๋…„ 5์›”์—๋‚˜ ๋‚˜์™”๋‹ค. Einar Karttumen์€ ํ‘œ์ค€ ํ•˜์Šค ์ผˆ ํŒŒ์„œ ํ•ฉ์„ฑ๊ธฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ Parsec์˜ ๊ธฐ๋Šฅ๋“ค์— ๊ทผ์ ‘ํ•œ PArrows๋ผ๋Š” ๊ตฌํ˜„์„ ์ž‘์„ฑํ–ˆ๋‹ค. ๊ทธ๋ž˜ํ”ฝ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•œ Fudgets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ˆ˜์ • ์š”๋ง ํ•˜์Šค ์ผˆ XML ํˆดํ‚ท(HXT)์€ ์• ๋กœ๋ฅผ ์ด์šฉํ•ด XML์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. ํ•˜์Šค ์ผˆ ์œ„ํ‚ค์— Gentle Introduction to HXT๋ผ๋Š” ํŽ˜์ด์ง€๊ฐ€ ์žˆ๋‹ค. Netwire - ๊ฒŒ์ž„ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ FRP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ. ์ ์šฉ์„ฑ ์• ๋กœ๋ฟ ์•„๋‹ˆ๋ผ ์• ๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋„ ์žˆ๋‹ค. ๋” ๋ณด๊ธฐ ๋ชจ๋‚˜๋“œ๋ฅผ ์• ๋กœ๋กœ ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ - John Hughes https://www.haskell.org/arrows/biblio.html ๊ฐ์‚ฌ์˜ ๋ง ์ด ๊ณผ๋ชฉ์€ ์›๋ž˜ The Monad.Reader 4๋ฅผ ์œ„ํ•ด ์ž‘์„ฑ๋œ, Shae Erisson์˜ An Introduction to Arrows์˜ ๋‚ด์šฉ์„ ๋นŒ๋ ค ์ผ๋‹ค. ๋…ธํŠธ Swierstra, Duponcheel. Deterministic, error correcting parser combinators โ†ฉ 06 ์• ๋กœ ์ดํ•ดํ•˜๊ธฐ (๊ฐœ์ •) (์ž‘์—… ์ค‘) ์›๋ฌธ : http://en.wikibooks.org/wiki/Haskell/Understanding_arrows ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๋งŽ์ด ๋ฐ”๋€Œ์–ด์„œ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ƒˆ๋กœ ๋ฒˆ์—ญํ•˜๋Š” ์ค‘์ž…๋‹ˆ๋‹ค. Arrow ํฌ์ผ“ ๊ฐ€์ด๋“œ ์• ๋กœ๋Š” ํ•จ์ˆ˜์™€ ๋งŽ์ด ๋‹ฎ์•˜๋‹ค Applicative์™€ Monad ์‚ฌ์ด์— ์žˆ๋Š” Arrow Arrow๋Š” ๋ฉ€ํ‹ฐํƒœ์Šคํฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค ArrowChoice๋Š” ๋‹จ์ •์ (resolute) ์ผ ์ˆ˜ ์žˆ๋‹ค ArrowApply๋Š” ๊ทธ์ € ๋”ฐ๋ถ„ํ•˜๋‹ค ์• ๋กœ ํ•ฉ์„ฑ ์ž๋Š” ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ๊ณณ์—์„œ ๋‚˜์˜จ๋‹ค ์• ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋ฆญ ๋ฐฉ์ง€ ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์€? ์Šคํƒœํ‹ฑ ํŒŒ์„œ์™€ ๋‹ค์ด๋‚ด๋ฏน ํŒŒ์„œ ์• ๋กœ ํ•ฉ์„ฑ์ž ๋„์ž…ํ•˜๊ธฐ ๊ทธ๋ž˜์„œ ์• ๋กœ๋Š” ์–ด๋””์— ์ข‹์€๊ฐ€? ์‹ค์ „์—์„œ์˜ ์• ๋กœ ๋” ์ฝ์„๊ฑฐ๋ฆฌ ์šฐ๋ฆฌ๋Š” ํ•˜์Šค ์ผˆ ์• ๋กœ ํŽ˜์ด์ง€์—์„œ ์ž๋ฃŒ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์„ ํ—ˆ๋ฝ๋ฐ›์•˜๋‹ค. ์ž์„ธํ•œ ์‚ฌํ•ญ์€ talk page๋ฅผ ๋ณผ ๊ฒƒ. ๋ชจ๋‚˜๋“œ์™€ ๋น„์Šทํ•˜๊ฒŒ ์• ๋กœ๋Š” ์–ด๋–ค ๋ฌธ๋งฅ ๋‚ด์—์„œ ์ผ์–ด๋‚˜๋Š” ๊ณ„์‚ฐ์„ ํ‘œํ˜„ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์• ๋กœ๋Š” ๋ชจ๋‚˜๋“œ๋ณด๋‹ค ๋” ์ผ๋ฐ˜ํ™”๋œ ์ถ”์ƒํ™”์ด๋ฉฐ, Monad ํด๋ž˜์Šค๋ณด๋‹ค ๋” ํ’๋ถ€ํ•œ ๋ฌธ๋งฅ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘˜์˜ ๊ทผ๋ณธ์ ์ธ ์ฐจ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์š”์•…ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋‚˜ ๋”• ํƒ€์ž… m a๊ฐ€ 'a๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ณ„์‚ฐ'์„ ํ‘œํ˜„ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ์• ๋กœ ํƒ€์ž… a b c (์ฆ‰ ๋งค๊ฐœํ™”๋œ ํƒ€์ž… a๋ฅผ ๋‘ ๋งค๊ฐœ๋ณ€์ˆ˜ b์™€ c์— ์ ์šฉํ•˜๋Š” ๊ฒƒ)์€ 'b ํƒ€์ž…์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„ c๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ณ„์‚ฐ'์„ ํ‘œํ˜„ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์• ๋กœ๋Š” ์ž…๋ ฅ์— ๋Œ€ํ•œ ์˜์กด์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. - John Hughes, Generalising Monads to Arrows[^1] ์ด๋ฒˆ ์žฅ์€ ํฌ๊ฒŒ 2๋ถ€๋กœ ๋‚˜๋‰œ๋‹ค. 1๋ถ€์—์„œ๋Š” ์• ๋กœ ๊ณ„์‚ฐ์ด ์šฐ๋ฆฌ์—๊ฒŒ ์ต์ˆ™ํ•œ ํŽ‘ ํ„ฐ ํด๋ž˜์Šค๋“ค๋กœ ํ‘œํ˜„๋œ ๊ณ„์‚ฐ๊ณผ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ์ง€ ์‚ดํŽด๋ณด๊ณ , ์• ๋กœ์™€ ๊ด€๋ จ๋œ ํ•ต์‹ฌ ํƒ€์ž… ํด๋ž˜์Šค๋“ค์„ ์†Œ๊ฐœํ•œ๋‹ค. 2๋ถ€์—์„œ๋Š” John Hughes๊ฐ€ ์• ๋กœ๋ฅผ ์†Œ๊ฐœํ•œ ๋…ผ๋ฌธ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ํŒŒ์„œ ์˜ˆ์ œ๋ฅผ ๊ณต๋ถ€ํ•œ๋‹ค. Arrow ํฌ์ผ“ ๊ฐ€์ด๋“œ ์• ๋กœ๋Š” ํ•จ์ˆ˜์™€ ๋งŽ์ด ๋‹ฎ์•˜๋‹ค ์• ๋กœ๋ฅผ ์ดํ•ดํ•˜๋Š” ์ฒซ๊ฑธ์Œ์€ ์• ๋กœ๊ฐ€ ํ•จ์ˆ˜์™€ ์–ผ๋งˆ๋‚˜ ๋น„์Šทํ•œ์ง€ ๊นจ๋‹ซ๋Š” ๊ฒƒ์ด๋‹ค. (->)์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Arrow ์ธ์Šคํ„ด์Šค์˜ ํƒ€์ž… ์ƒ์„ฑ์ž์˜ ์ข…(kind)์€ * -> * -> *์ด๋‹ค. ์ฆ‰ ํƒ€์ž… ์ธ์ž๋ฅผ ๋‘ ๊ฐœ ๋ฐ›๋Š”๋ฐ, ์ด๋Š” Monad๋Š” ์ธ์ž๋ฅผ ํ•˜๋‚˜๋งŒ ๋ฐ›๋Š” ๊ฒƒ๊ณผ ๋‹ค๋ฅธ ์ ์ด๋‹ค. ํŠนํžˆ Arrow๋Š” ์ƒ์œ„ ํด๋ž˜์Šค๋กœ Category๋ฅผ ๊ฐ€์ง„๋‹ค. Category๋Š” ์•„์ฃผ ๋Œ€๊ฐ• ์„ค๋ช…ํ•˜์ž๋ฉด ํ•จ์ˆ˜์ฒ˜๋Ÿผ ํ•ฉ์„ฑ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์„ ์œ„ํ•œ ํด๋ž˜์Šค๋‹ค. class Category y where id :: y a a -- identity for composition. (.) :: y b c -> y a b -> y a c -- associative composition. (ํ•จ์ˆ˜๊ฐ€ Category์˜ ์ธ์Šคํ„ด์Šค๋ผ๋Š” ๊ฑด ๋งํ•  ํ•„์š”๋„ ์—†๋‹ค. ์‚ฌ์‹ค ํ•จ์ˆ˜๋Š” Arrow์ด๊ธฐ๋„ ํ•˜๋‹ค.) ์ด๋Ÿฐ ์œ ์‚ฌ์„ฑ์˜ ๊ฒฐ๋ก ์€, Arrow ์—ฐ์‚ฐ์ž๋กœ ๊ฐ€๋“ํ•œ ํ‘œํ˜„์‹์„ ๋ณผ ๋•Œ ์ธ์ž ์ƒ๋žต์‹(point-free)์„ ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. (total &&& (const 1 ^>> total)) >>> arr (uncurry (/)) ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฐ’์„ ์ ์šฉํ•  ๊ณณ์„ ์ฐพ๋‹ค๊ฐ€ ๊ธˆ๋ฐฉ ๊ธธ์„ ์žƒ๋Š”๋‹ค. ์–ด์จŒ๋“  ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๋ด๋„ ๊ธธ์„ ์žƒ๊ธฐ ์‰ฌ์šด ๊ฑด ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ์ด๊ฒƒ์ด proc ํ‘œ๊ธฐ๊ฐ€ ์žˆ๋Š” ์ด์œ ๋‹ค. proc์€ ๋ณ€์ˆ˜ ์ด๋ฆ„๊ณผ ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์–ด๋–ค ์—ฐ์‚ฐ์ž๋Š” ์ˆจ๊ฒจ์„œ Arrow ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ ๋‹ค. ๊ณ„์†ํ•˜๊ธฐ ์ „์— Control.Category๋Š” (<<<) = (.)์™€ (>>>) = flip (.)๋„ ์ •์˜ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์งš๊ณ  ๋„˜์–ด๊ฐ€์•ผ๊ฒ ๋‹ค. ์ด๊ฒƒ๋“ค์€ ์• ๋กœ๋ฅผ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฒฐํ•ฉํ•  ๋•Œ ๋งค์šฐ ํ”ํ•˜๊ฒŒ ์“ฐ์ธ๋‹ค. Applicative์™€ Monad ์‚ฌ์ด์— ์žˆ๋Š” Arrow ๋ฐ”๋กœ ์•ž์—์„œ ๊ฒฝ๊ณ ํ–ˆ์ง€๋งŒ, ์‚ฌ์‹ค ์• ๋กœ๋ฅผ ์ ์šฉ์„ฑ ํŽ‘ํ„ฐ์™€ ๋ชจ๋‚˜๋“œ์— ๋น„๊ตํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํŠธ๋ฆญ์€ ํŽ‘ํ„ฐ๋ฅผ ์ข€ ๋” ์• ๋กœ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ๋งŒ๋“ค๊ณ  ๊ทธ ๋ฐ˜๋Œ€๋Š” ๋ง‰๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ Arrow y => y a b๋ฅผ Applicative f => f a ๋˜๋Š” Monad m => m a์™€ ๋น„๊ตํ•˜๋ฉด ์•ˆ ๋˜๊ณ , ๋‹ค์Œ๊ณผ ๋น„๊ตํ•ด์•ผ ํ•œ๋‹ค. static morphism์˜ ํƒ€์ž…์ธ Applicative f => f (a -> b) ์ฆ‰ (<*>)์˜ ์ขŒ๋ณ€์˜ ๊ฐ’ Kleisli morphism์˜ ํƒ€์ž…์ธ Monad m => a -> m b ์ฆ‰ (>>=) ์šฐ๋ณ€์˜ ํ•จ์ˆ˜ 2 ์‚ฌ์ƒ(morphism)์€ Category ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ์‚ฌ์‹ค static morphism๊ณผ Kleisli morphism ๋‘˜ ๋ชจ๋‘์— ๋Œ€ํ•œ Category์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ ๋‹นํ•˜๊ฒŒ ๋น„ํ‹€๋ฉด ์ ์ ˆํžˆ ๋น„๊ตํ•˜๋Š” ๋ฐ๋Š” ์ถฉ๋ถ„ํ•˜๋‹ค. ์ด ๋…ผ์˜์—์„œ Functor, Applicative, Monad๋ฅผ ๋น„๊ตํ–ˆ๋˜ sliding scale of power discussion๊ฐ€ ๋– ์˜ค๋ฅธ๋‹ค๋ฉด ์—ฌ๋Ÿฌ๋ถ„์ด ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ด๊ณ  ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ง€๊ธˆ ์ •ํ™•ํžˆ ๊ฐ™์€ ๊ธธ์„ ๋ฐŸ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋•Œ๋Š” ์‚ฌ์ƒ์˜ ํƒ€์ž…์ด ์–ด๋–ป๊ฒŒ ๊ฒฐ๊ณผ(effect)์˜ ์ƒ์„ฑ ๊ฐ€๋Šฅ ์œ ๋ฌด๋ฅผ ์ œํ•œํ•˜๋Š”์ง€์— ์ฃผ๋ชฉํ–ˆ๋‹ค. ๋ชจ๋‚˜ ๋”• ๋ฐ”์ธ๋“œ๋Š” Kleisli ์‚ฌ์ƒ์— ์ฃผ์–ด์ง„ a ๊ฐ’์— ์˜์กดํ•˜๋Š” ๊ณ„์‚ฐ์˜ ๊ฒฐ๊ณผ์— ๊ฑฐ์˜ ์ž„์˜์ ์ธ ๋ณ€๊ฒฝ์„ ์œ ๋ฐœํ•˜๋Š” ๋ฐ˜๋ฉด, static ์‚ฌ์ƒ ๋‚ด functorial wrapper์™€ ํ•จ์ˆ˜ ์• ๋กœ ์‚ฌ์ด์˜ ๊ฒฉ๋ฆฌ๋Š” applicative computation ๋‚ด์˜ ๊ฒฐ๊ณผ๊ฐ€ ํŽ‘ ํ„ฐ ๋‚ด์˜ ์–ด๋Š ๊ฐ’์—๋„ ์˜์กดํ•˜์ง€ ์•Š์Œ์„ ๋œปํ•œ๋‹ค.3 Monadic binds can induce near-arbitrary changes to the effects of a computation depending on the a values given to the Kleisli morphism, while the isolation between the functorial wrapper and the function arrow in static morphisms mean the effects in an applicative computation do not depend at all on the values within the functor ์ด๊ฑฐ ๋จผ ์†Œ๋ฆฌ์—ฌ ์• ๋กœ๊ฐ€ ์ด๋Ÿฐ ๊ด€์ ๊ณผ ์—ฐ๊ด€์ด ์ ์€ ๊ฒƒ์€ Arrow y => y a b ์•ˆ์—์„œ ๋ฌธ๋งฅ y์™€ ํ•จ์ˆ˜ ์• ๋กœ ์‚ฌ์ด์— ๊ฐ€๋Šฅ์„ฑ์˜ ๋ฒ”์œ„๋ฅผ ๋ช…ํ™•ํžˆ ๊ฒฐ์ •ํ•˜๋Š” ๊ด€๊ณ„๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. static morphism๊ณผ Kleisli morphism ๋ชจ๋‘ Arrow๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ  ๋ฐ˜๋Œ€๋กœ Arrow์˜ ์ธ์Šคํ„ด์Šค๋ฅผ Applicative ์ธ์Šคํ„ด์Šค๋งŒํผ ์ œํ•œ๋˜๊ฑฐ๋‚˜ Monad ์ธ์Šคํ„ด์Šค์ฒ˜๋Ÿผ ๊ฐ•๋ ฅํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. 4 ๋” ํฅ๋ฏธ๋กœ์šด ๊ฒƒ์€ Arrow์— ์„ธ ๋ฒˆ์งธ ์„ ํƒ์ง€๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ, ํ•œ ์ปจํ…์ŠคํŠธ ์•ˆ์— applicative ๊ฐ™์€ static effect์™€ ๋ชจ๋‚˜๋“œ ๊ฐ™์€ dynamic effect๋ฅผ ๋™์‹œ์— ๊ฐ€์ง€๋ฉด์„œ๋„ ์„œ๋กœ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์• ๋กœ๋Š” effect๋“ค์ด ๊ฒฐํ•ฉ๋˜๋Š” ๋ฐฉ์‹์„ ์„ธ๋ฐ€ํ•˜๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์ด ์ด๋ฒˆ ์žฅ ๋งˆ์ง€๋ง‰์—์„œ ์‚ดํŽด๋ณผ ๊ณ ์ „์ ์ธ ์˜ˆ์ œ์ธ ์• ๋กœ ๊ธฐ๋ฐ˜ ํŒŒ์„œ์˜ ์š”์ง€๋‹ค. Arrow๋Š” ๋ฉ€ํ‹ฐํƒœ์Šคํฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค ๋‹ค์Œ์€ Arrow ๋ฉ”์„œ๋“œ ๋“ค์ด๋‹ค. class Category y => Arrow y where -- Minimal implementation: arr and first arr :: (a -> b) -> y a b -- converts function to arrow first :: y a b -> y (a, c) (b, c) -- maps over first component second :: y a b -> y (c, a) (c, b) -- maps over second component (***) :: y a c -> y b d -> y (a, b) (c, d) -- first and second combined (&&&) :: y a b -> y a c -> y a (b, c) -- (***) on a duplicated value ์ด ๋ฉ”์„œ๋“œ๋“ค์„ ์ด์šฉํ•˜๋ฉด ์ผ๋ ฌ๋กœ ์—ฐ๊ฒฐ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ์• ๋กœ๋“ค์— ๋Œ€ํ•ด ๋งค ๋‹จ๊ณ„๋งˆ๋‹ค ์—ฌ๋Ÿฌ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐœ๋ณ„ ๊ณ„์‚ฐ์— ์“ฐ์ธ ๊ฐ’๋“ค์„ (์ค‘์ฒฉ๋˜์—ˆ์„ ์ˆ˜ ์žˆ๋Š”) ์ง ์•ˆ์˜ ์ง๋“ค์˜ ์›์†Œ๋กœ ์ €์žฅํ•˜๊ณ , ์ง์„ ๋‹ค๋ฃจ๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ๊ฐ์˜ ๊ฐ’์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฌ๋ฉด ๋‚˜์ค‘์„ ์œ„ํ•ด ์ค‘๊ฐ„ ๊ฐ’๋“ค์„ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ธ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ํ•จ์ˆ˜๋ฅผ ์“ฐ๋Š” ๋“ฑ์˜ ์ผ์„ ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. 5 ์‹œ๊ฐํ™”๋ฅผ ํ•˜๋ฉด ์• ๋กœ ๊ณ„์‚ฐ์˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™๋‹ค. ๋‹ค์Œ์€ (>>>)๋ฅผ ๋น„๋กฏ 6๊ฐœ์˜ Arrow ๋ฉ”์„œ๋“œ๋“ค์„ ๋ฌ˜์‚ฌํ•œ ๊ฒƒ์ด๋‹ค. arr๋Š” ํ•จ์ˆ˜๋ฅผ ์• ๋กœ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋‹ค๋ฅธ ์• ๋กœ์™€ ํ•ฉ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ ๋‹ค. ๋ชจ๋“  ์• ๋กœ๊ฐ€ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค, (>>>)๋Š” ๋‘ ์• ๋กœ๋ฅผ ํ•ฉ์„ฑํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์• ๋กœ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‘ ๋ฒˆ์งธ ์• ๋กœ์— ๊ณต๊ธ‰๋œ๋‹ค. first๋Š” ๋‘ ์ž…๋ ฅ์„ ๋‚˜๋ž€ํžˆ ์ทจํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ž…๋ ฅ์€ ์• ๋กœ๋ฅผ ํ†ตํ•ด ์ˆ˜์ •๋˜๋Š” ๋ฐ˜๋ฉด ๋‘ ๋ฒˆ์งธ ์ž…๋ ฅ์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค. second๋Š” ๋ฐ˜๋Œ€๋กœ ๋‘ ๋ฒˆ์งธ ์ž…๋ ฅ๋งŒ ์ˆ˜์ •ํ•œ๋‹ค. (***)๋Š” ๋‘ ์ž…๋ ฅ์„ ๊ฐ๊ฐ ๋‹ค๋ฅธ ์• ๋กœ๋ฅผ ํ†ตํ•ด ์ˆ˜์ •ํ•œ๋‹ค. (&&&)๋Š” ํ•œ ์ž…๋ ฅ์„ ์ทจํ•ด ๋ณต์ œํ•˜๊ณ  ๊ฐ๊ฐ ๋‹ค๋ฅธ ์• ๋กœ๋ฅผ ํ†ตํ•ด ์ˆ˜์ •ํ•œ๋‹ค. ์• ๋กœ ํŠœํ† ๋ฆฌ์–ผ์˜ mean ์• ๋กœ์˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„. ์ง์‚ฌ๊ฐํ˜•์€ ์• ๋กœ, ๋‘ฅ๊ทผ ์‚ฌ๊ฐํ˜•์€ arr๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ ์• ๋กœ, ์›์€ ๊ธฐํƒ€ ๋ฐ์ดํ„ฐ ํ๋ฆ„ ๋ถ„ํ• /๋ณ‘ํ•ฉ ์ง€์ ์ด๋‹ค. ๋‹ค๋ฅธ ํ•ฉ์„ฑ์ž๋“ค์€ ์ƒ๋žตํ–ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๋‹ค์Œ ์ฝ”๋“œ์— ๋Œ€์‘ํ•œ๋‹ค. (total &&& (const 1 ^>> total)) >>> arr (uncurry (/)) Control.Arrow๋Š” returnA = arr id๋ฅผ ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š๋Š” ์• ๋กœ๋กœ ์ •์˜ํ•œ๋‹ค. ์• ๋กœ ๋ฒ•์น™ ์ค‘ ํ•˜๋‚˜๋Š” returnA๊ฐ€ ๋ฐ˜๋“œ์‹œ Category ์ธ์Šคํ„ด์Šค์˜ id์™€ ๋™์น˜์—ฌ์•ผ ํ•œ๋‹ค๊ณ  ์ •์˜ํ•œ๋‹ค. 6 ArrowChoice๋Š” ๋‹จ์ •์ (resolute) ์ผ ์ˆ˜ ์žˆ๋‹ค Arrow๊ฐ€ ๋ฉ€ํ‹ฐํƒœ์Šคํ‚น์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค๋ฉด ArrowChoice๋Š” ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. class Arrow y => ArrowChoice y where -- Minimal implementation: left left :: y a b -> y (Either a c) (Either b c) -- maps over left choice right :: y a b -> y (Either c a) (Either c b) -- maps over right choice (+++) :: y a c -> y b d -> y (Either a b) (Either c d) -- left and right combined (|||) :: y a c -> y b c -> y (Either a b) c -- (+++), then merge results Either๋Š” ๊ฐ’์— ํƒœ๊ทธ๋ฅผ ๋งค๊ธฐ๋Š” ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•˜์—ฌ, ์• ๋กœ๋“ค์ด ๊ฐ’์— ๋ถ™์€ Left ๋˜๋Š” Right ํƒœ๊ทธ์— ๋”ฐ๋ผ ๊ทธ ๊ฐ’์„ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. Either๋ฅผ ์ˆ˜๋ฐ˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์€ Arrow๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์Œ์„ ์ˆ˜๋ฐ˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ๊ฒƒ์— ์ฃผ๋ชฉํ•  ๊ฒƒ. ์• ๋กœ ํŠœํ† ๋ฆฌ์–ผ์˜ getWord ์˜ˆ์ œ์˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„ ์ผ๋ถ€. ํŒŒ๋ž€์ƒ‰์€ Left ํƒœ๊ทธ๋ฅผ, ๋ถ‰์€์ƒ‰์€ Right๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. proc ํ‘œ๊ธฐ์˜ if ๋ถ€๋ถ„์ด True๋ฅผ left๋กœ, False๋ฅผ right๋กœ ๋ณด๋‚ด๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•  ๊ฒƒ. ์ƒ์‘ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. proc () -> do first Time <- oneShot -< () mPicked <- if first Time then do picked <- pickWord rng -< () returnA -< Just picked else returnA -< Nothing accum' Nothing mplus -< mPicked ArrowApply๋Š” ๊ทธ์ € ๋”ฐ๋ถ„ํ•˜๋‹ค ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ ArrowApply๋Š” ์• ๋กœ ๊ณ„์‚ฐ ๋„์ค‘ ์• ๋กœ๋ฅผ ๊ฐ’์— ์ง์ ‘ ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ผ๋ฐ˜์ ์ธ Arrow๋Š” ์ด๋Ÿฐ ๊ฒƒ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ArrowApply๋“ค์€ ๋Š์ž„์—†์ด ํ•ฉ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ์ข…์˜ run-arrow ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์• ๋กœ๋กœ๋ถ€ํ„ฐ ํ‰๋ฒ”ํ•œ ํ•จ์ˆ˜๋ฅผ ์–ป์–ด๋‚ธ ํ›„ ๋งˆ์ง€๋ง‰์—๋งŒ ์ ์šฉ์ด ๋ฐœ์ƒํ•œ๋‹ค. class Arrow y => ArrowApply y where app :: y (y a b, a) b -- applies first component to second (์˜ˆ๋ฅผ ๋“ค์–ด ํ•จ์ˆ˜๋ฅผ ์œ„ํ•œ app์€ uncurry ($) = \(f, x) -> f x์ด๋‹ค.) ํ•˜์ง€๋งŒ app์—๋Š” ๋ผˆ์•„ํ”ˆ ๋Œ€๊ฐ€๊ฐ€ ์žˆ๋‹ค. ์• ๋กœ ๊ณ„์‚ฐ ์•ˆ์—์„œ ์• ๋กœ๋ฅผ ๊ฐ’์œผ๋กœ์„œ ๊ตฌ์ถ•ํ•˜๊ณ  ์ ์šฉ์„ ํ†ตํ•ด ์ œ๊ฑฐํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๊ณ„์‚ฐ ๋‚ด ๊ฐ’๋“ค์ด ๋ฌธ๋งฅ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ด๊ฒƒ์€ ๋งˆ์น˜ ๋ชจ๋‚˜ ๋”• ๋ฐ”์ธ๋“œ๊ฐ€ ํ•˜๋Š” ์ผ์ฒ˜๋Ÿผ ๋“ค๋ฆฐ๋‹ค. ์‚ฌ์‹ค ArrowApply๊ฐ€ ํ•˜๋Š” ์ผ์€ ArrowApply ๋ฒ•์น™๋งŒ ์ง€ํ‚จ๋‹ค๋ฉด Monad์™€ ์™„์ „ํžˆ ๋™๋“ฑํ•˜๋‹ค. ์ตœ์ข… ๊ฒฐ๋ก ์€ ArrowApply ์• ๋กœ๋Š” ๋ถ€๋ถ„ ์ •์  ๋ฌธ๋งฅ์ฒ˜๋Ÿผ Monad๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜์ง€๋งŒ Arrow๋Š” ํ—ˆ์šฉํ•˜๋Š” ํฅ๋ฏธ๋กœ์šด ๊ฐ€๋Šฅ์„ฑ์„ ์ „ํ˜€ ์‹คํ˜„ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์• ๋กœ์˜ ์ง„์ •ํ•œ ์œ ์—ฐํ•จ์€ ๋ชจ๋‚˜๋“œ์—๋Š” ์—†๋Š” ๊ฒƒ๋“ค๋กœ๋ถ€ํ„ฐ ๋‚˜์˜ค๋Š” ๊ฒƒ์ด๋ฉฐ, ๊ทธ๊ฒŒ ์—†์œผ๋ฉด ๋‹จ์ง€ ์ง€์ €๋ถ„ํ•œ ๋ฌธ๋ฒ•์ผ ๋ฟ์ด๋‹ค. - Philippa Cowderoy ์• ๋กœ ํ•ฉ์„ฑ ์ž๋Š” ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ๊ณณ์—์„œ ๋‚˜์˜จ๋‹ค ํ•จ์ˆ˜๋Š” ์• ๋กœ์˜ ์ž๋ช…ํ•œ ์˜ˆ์‹œ์ด๋ฉฐ ๋”ฐ๋ผ์„œ Control.Arrow์˜ ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜๋“ค์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์• ๋กœ์™€ ์•„๋ฌด ์ƒ๊ด€ ์—†๋Š” ์ฝ”๋“œ์—์„œ ์• ๋กœ ํ•ฉ์„ฑ์ž๊ฐ€ ์“ฐ์ด๋Š” ๊ฒƒ์€ ํ”ํ•œ ์ผ์ด๋‹ค. ๋‹ค์Œ์€ ํ‰๋ฒ”ํ•œ ํ•จ์ˆ˜๋“ค์— ๋Œ€ํ•ด ์• ๋กœ ํ•ฉ์„ฑ์ž๊ฐ€ ๋ฌด์Šจ ์ผ์„ ํ•˜๋Š”์ง€๋ฅผ ์š”์•ฝํ•œ ๊ฒƒ์ด๋‹ค. ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ๋ชจ๋“ˆ์˜ ํ•ฉ์„ฑ์ž๋“ค๋„ ์˜†์— ํ‘œ์‹œํ–ˆ๋‹ค. (๋‹ค๋ฅธ ์ด๋ฆ„์„ ์„ ํ˜ธํ•˜๊ฑฐ๋‚˜ ๋‹จ์ˆœ ์ž‘์—…์—๋Š” ๋‹จ์ˆœํ•œ ๋ชจ๋“ˆ์„ ์„ ํ˜ธํ•˜๋Š” ๊ฒฝ์šฐ) TODO: ํ‘œ Data.Bifunctor ๋ชจ๋“ˆ์˜ Bifunctor ํด๋ž˜์Šค๋Š” ์Œ๊ณผ Either๋ฅผ ์ธ์Šคํ„ด์Šค๋กœ ๊ฐ€์ง„๋‹ค. Bifunctor๋Š” Functor์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ํ•จ์ˆ˜๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‘ ๊ฐ€์ง€์ด๋ฉฐ ๊ฐ๊ฐ first ๋ฉ”์„œ๋“œ์™€ second ๋ฉ”์„œ๋“œ์— ๋Œ€์‘ํ•œ๋‹ค. 7 ์—ฐ์Šต๋ฌธ์ œ second, (***), (&&&)์˜ ๊ตฌํ˜„์„ ์ž‘์„ฑํ•˜๋ผ. second์˜ ๊ตฌํ˜„์—๋Š” (>>>), arr, first (๊ทธ๋ฆฌ๊ณ  ๊ธฐํƒ€ ํ‰๋ฒ”ํ•œ ํ•จ์ˆ˜๋“ค)์„ ์‚ฌ์šฉํ•  ๊ฒƒ. ๊ทธ ํ›„์—๋Š” ๋‹ค๋ฅธ ํ•ฉ์„ฑ์ž๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. left๋ฅผ ์ด์šฉํ•ด right์˜ ๊ตฌํ˜„์„ ์ž‘์„ฑํ•˜๋ผ. liftY2 :: Arrow y =>(a -> b -> c) -> y r a -> y r b -> y r c์„ ๊ตฌํ˜„ํ•˜๋ผ. ์• ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋ฆญ ๋ฐฉ์ง€ ์• ๋กœ๋Š” Swierstra์™€ Duponcheel์˜ ํšจ์œจ์ ์ธ ํŒŒ์„œ ์„ค๊ณ„์—์„œ ์œ ๋ž˜๋œ ๊ฒƒ์ด๋‹ค 8. ์ด ์„ค๊ณ„๊ฐ€ ์™œ ์ข‹์€์ง€ ๋…ผ์˜ํ•˜๊ธฐ์— ์•ž์„œ ๋ชจ๋‚˜ ๋”• ํŒŒ์„œ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋น ๋ฅด๊ฒŒ ์‚ดํŽด๋ณด์ž. ๋‹ค์Œ์€ ๋ชจ๋‚˜ ๋”• ํŒŒ์„œ ํƒ€์ž…์„ ๊ฑฐ์˜ ๋ผˆ๋Œ€๋งŒ ๋ฌ˜์‚ฌํ•œ ๊ฒƒ์ด๋‹ค. newtype Parser s a = Parser { runParser :: [s] -> Maybe (a, [s]) } Parser๋Š” ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ(์—ฌ๊ธฐ์„œ๋Š” [s] ํƒ€์ž…์˜ ๋ฆฌ์ŠคํŠธ)์„ ์ทจํ•ด ์ž…๋ ฅ์—์„œ ์ฐพ์€ ๊ฒƒ์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ([a] ํƒ€์ž…์˜)์™€ ์ŠคํŠธ๋ฆผ(๋ณดํ†ต์€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์—์„œ ํŒŒ์„œ๊ฐ€ ์†Œ๋น„ํ•œ ์ž…๋ ฅ ์ผ๋ถ€๋ฅผ ์ œ์™ธํ•œ ๊ฒƒ), ๋˜๋Š” Nothing์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํŒŒ์„œ๊ฐ€ ์„ฑ๊ณตํ–ˆ๋Š”์ง€ ์‹คํŒจํ–ˆ๋Š”์ง€๋ฅผ ๋งํ•˜๋ ค๋ฉด ํŒŒ์„œ๊ฐ€ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์‚ฐํ–ˆ๋Š”์ง€๋ฅผ ๋ณธ๋‹ค. Parsec9์˜ ParsecT ๊ฐ™์€ ์‹ค์ „์˜ ํŒŒ์„œ ํƒ€์ž…์€ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด (ํŠนํžˆ ์‹คํŒจํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€) ํ›จ์”ฌ ๋ณต์žกํ•˜์ง€๋งŒ ์ด ๋‹จ์ˆœํ•œ Parser๋„ ์šฐ๋ฆฌ ๋ชฉ์ ์—๋Š” ์ถฉ๋ถ„ํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด ํŒŒ์„œ๋Š” ๋ฌธ์ž์—ด ๋‚ด ๋‹จ์ผ ๋ฌธ์ž๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. char :: Char -> Parser Char Char char c = Parser $ \input -> case input of [] -> Nothing x : xs -> if x == c then Just (c, xs) else Nothing ์ž…๋ ฅ ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฌธ์ž๊ฐ€ c๋ผ๋ฉด char c๋Š” ์„ฑ๊ณตํ•˜์—ฌ ์ฒซ ๋ฌธ์ž๋ฅผ ์†Œ๋น„ํ•˜๊ณ  c๋ฅผ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” ์‹คํŒจํ•œ๋‹ค. GHCi> runParser (char 'H') "Hello" Just ('H',"ello") GHCi> runParser (char 'G') "Hello" Nothing Parser ํƒ€์ž…์„ ๋‹ค์‹œ ๋ณด๋ฉด ์ด๊ฒƒ์€ Maybe์— ๋Œ€ํ•œ State [s]์ด๋‹ค. ๋”ฐ๋ผ์„œ Parser๋ฅผ Applicative, Monad, Alternative ๋“ฑ์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. (์ง์ ‘ ์‹œ๋„ํ•ด ๋ด๋„ ์ข‹๋‹ค.) ๋”ฐ๋ผ์„œ ๊ฐ„๋‹จํ•œ ํŒŒ์„œ๋กœ๋ถ€ํ„ฐ ๋ณต์žกํ•œ ํŒŒ์„œ๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด ๋งค์šฐ ๋‹ค์–‘ํ•œ ํ•ฉ์„ฑ์ž๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํŒŒ์„œ๋“ค์€ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋  ์ˆ˜๋„ ์žˆ๊ณ ... isHel :: Parser Char () isHel = char 'H' *> char 'e' *> char 'l' *> pure () ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ•ฉ์„ฑํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ... string :: String -> Parser Char String string = traverse char ... or be tried as alternatives to each other: solfege :: Parser Char String solfege = string "Do" <|> string "Re" <|> string "Mi" ๋งˆ์ง€๋ง‰ ์˜ˆ์‹œ์—์„œ Swierstra์™€ Duponcheel๊ฐ€ ์ œ์•ˆํ•˜๋ ค๋˜ ๋ฌธ์ œ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ง์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. solfege๋ฅผ ๋ฌธ์ž์—ด "Fa"์— ๋Œ€ํ•ด ์‹คํ–‰ํ•  ๋•Œ, ํŒŒ์„œ๊ฐ€ ์„ธ ์ œ์•ˆ์„ ๋ชจ๋‘ ์‹คํŒจํ•œ ํ›„์—์•ผ ๊ทธ ํŒŒ์„œ๊ฐ€ ์‹คํŒจํ•  ๊ฒƒ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋ณต์žกํ•œ ํŒŒ์„œ์—์„œ ์ œ์•ˆ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ ์ƒ๋‹น ๋ถ€๋ถ„ ์†Œ๋น„ํ•œ ํ›„์—์•ผ ์‹คํŒจํ•œ๋‹ค๋ฉด ์ดํ›„์˜ ์ผ๋ จ์˜ ํŒŒ์„œ๋“ค๋„ ๊ทธ๋Ÿฐ ์‹์œผ๋กœ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ์ดํ›„ ํŒŒ์„œ๋“ค์ด ์†Œ๋น„ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ์ž…๋ ฅ์€ ๋ฉ”๋ชจ๋ฆฌ์— ์œ ์ง€๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ณต๊ฐ„ ์‚ฌ์šฉ๋Ÿ‰์ด ๋‹จ์ˆœ ์˜ˆ์ƒ๋ณด๋‹ค ํ›จ์”ฌ ๋งŽ์•„์ ธ์„œ space leak์ด๋ผ ๋ถ€๋ฅด๋Š” ์ƒํ™ฉ์— ์ฒ˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์€? Swierstra์™€ Duponcheel(1996)์€ ํŒŒ์‹ฑ ์ž‘์—… ๊ฐ™์€ ๊ฑธ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋˜‘๋˜‘ํ•œ ํŒŒ์„œ๋Š” ์ฒซ ๋ฌธ์ž๋ฅผ ๋ณด์ž๋งˆ์ž ์ฆ‰์‹œ ์‹คํŒจํ•  ์ˆ˜ ์žˆ์Œ์„ ์•Œ์•„์ฐจ๋ ธ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ solfege ํŒŒ์„œ์˜ ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋กœ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์€ "Do", "Re", "Mi"์— ๋Œ€ํ•ด ๊ฐ๊ฐ D, R, M์œผ๋กœ ์ œํ•œ๋œ๋‹ค. ์ด ๋˜‘๋˜‘ํ•œ ํŒŒ์„œ๋Š” ์ž…๋ ฅ์˜ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์„ ๋” ์ผ์ฐ ์ˆ˜ํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ๋‹ค๋ฅธ ํŒŒ์„œ๋“ค์ด ์ž…๋ ฅ์„ ์†Œ๋น„ํ•  ์ˆ˜ ์žˆ์„์ง€ ๋ฏธ๋ฆฌ ์•Œ์•„๋ณด๊ณ  ์†Œ๋น„๋  ์ˆ˜ ์—†๋Š” ์ž…๋ ฅ์€ ๋ฒ„๋ฆด ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ์ƒˆ๋กœ์šด ํŒŒ์„œ๋Š” ๋ชจ๋‚˜ ๋”• ํŒŒ์„œ์™€ ๋น„์Šทํ•˜์ง€๋งŒ ์ •์  ์ •๋ณด๋ฅผ ์‚ฐ์ถœํ•œ๋‹ค๋Š” ์ฃผ์š” ์ฐจ์ด์ ์ด ์žˆ๋‹ค. ๋ชจ๋‚˜๋“œ์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๋ฌด์—‡์„ ํŒŒ์‹ฑ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๋งํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํฐ ๋ฌธ์ œ๊ฐ€ ํ•˜๋‚˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฑด Monad ์ธํ„ฐํŽ˜์ด์Šค์— ๋งž์ง€ ์•Š๋Š”๋‹ค. ๋ชจ๋‚˜ ๋”• ํ•ฉ์„ฑ์€ ์˜ค๋กœ์ง€ (a -> m b) ํ•จ์ˆ˜์™€ ํ•จ์ˆ˜๋“ค์— ์˜ํ•ด ์ž‘๋™ํ•œ๋‹ค. ์ •์  ์ •๋ณด๋ฅผ ๋ถ™์ผ ๋ฐฉ๋ฒ•์€ ์—†๋‹ค. ์„ ํƒ์ง€๋Š” ์˜ค๋กœ์ง€ ํ•˜๋‚˜๋‹ค. ์–ด๋–ค ์ž…๋ ฅ์„ ๋˜์ง€๊ณ  ํ†ต๊ณผํ•˜๋Š”์ง€ ์‹คํŒจํ•˜๋Š”์ง€ ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ์ด ํ™”์ œ๋ฅผ ์ฒ˜์Œ ๊บผ๋ƒˆ์„ ๋•Œ๋กœ ๋Œ์•„๊ฐ€์„œ, ๋ชจ๋‚˜ ๋”• ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ์™„๋ฒฝํ•œ ๋งŒ๋Šฅ ๋„๊ตฌ๋กœ ํ™๋ณด๋˜๊ณ  ์žˆ์—ˆ๊ธฐ์— ์ด ์ธํ„ฐํŽ˜์ด์Šค์— ๋งž์ง€ ์•Š์œผ๋ฉด์„œ ํŠน๋ณ„ํžˆ ์œ ์šฉํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์€ ์ผ์ข…์˜ ๋‚œ๊ด€์ด์—ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์• ๋กœ๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค. John Hughes์˜ Generalising monads to arrows๋Š” ์ƒˆ๋กญ๊ณ  ๋” ์œ ์—ฐํ•œ ๋„๊ตฌ๋กœ์„œ ์• ๋กœ ์ถ”์ƒํ™”๋ฅผ ์ œ์•ˆํ•œ๋‹ค. ์Šคํƒœํ‹ฑ ํŒŒ์„œ์™€ ๋‹ค์ด๋‚ด๋ฏน ํŒŒ์„œ Swierstra์™€ Duponcheel์˜ ํŒŒ์„œ๋ฅผ Hughes๊ฐ€ ์ œ์‹œํ•œ ์• ๋กœ์˜ ๊ด€์ ์—์„œ ์ž์„ธํžˆ ์‚ดํŽด๋ณด์ž. ์ด ํŒŒ์„œ๋Š” ๋‘ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ๊ฐ€์ง„๋‹ค. ํ•˜๋‚˜๋Š” ์ž…๋ ฅ์„ ํŒŒ์‹ฑ ํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ๋น ๋ฅธ ์Šคํƒœํ‹ฑ ํŒŒ์„œ๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ์‹ค์ œ ํŒŒ์‹ฑ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋Š๋ฆฐ ๋‹ค์ด๋‚ด๋ฏน ํŒŒ์„œ๋‹ค. import Control.Arrow import qualified Control.Category as Cat import Data.List (union) data Parser s a b = P (StaticParser s) (DynamicParser s a b) data StaticParser s = SP Bool [s] newtype DynamicParser s a b = DP ((a, [s]) -> (b, [s])) ์Šคํƒœํ‹ฑ ํŒŒ์„œ๋Š” ํŒŒ์„œ๊ฐ€ ๋นˆ ์ž…๋ ฅ์„ ํ—ˆ์šฉํ•˜๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ”Œ๋ž˜๊ทธ์™€ ๊ฐ€๋Šฅํ•œ ์‹œ์ž‘ ๋ฌธ์ž๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•œ ๋ฌธ์ž๋ฅผ ์œ„ํ•œ ํŒŒ์„œ๋Š” ์ด๋Ÿฐ ์‹์ด๋‹ค. spCharA :: Char -> StaticParser Char spCharA c = SP False [c] ์ด ํŒŒ์„œ๋Š” ๋นˆ ๋ฌธ์ž์—ด์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฉฐ(False) ๊ฐ€๋Šฅํ•œ ์‹œ์ž‘ ๋ฌธ์ž์˜ ๋ฆฌ์ŠคํŠธ๋Š” c๋กœ๋งŒ ๊ตฌ์„ฑ๋œ๋‹ค. ๋‹ค์ด๋‚ด๋ฏน ํŒŒ์„œ๋Š” ์ข€ ๋” ํ•ด๋ถ€ํ•ด์•ผ ํ•œ๋‹ค. ์ด๊ฒƒ์€ (a, [s])์—์„œ (b, [s])๋กœ ๊ฐ€๋Š” ํ•จ์ˆ˜๋‹ค. ๋‘ ํŒŒ์„œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒŒ ์‰ฝ๋‹ค. ๊ฐ ํŒŒ์„œ๋Š” ์ด์ „ ํŒŒ์„œ์˜ ๊ฒฐ๊ณผ(a)์™€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„([s])์„ ์†Œ๋น„ํ•ด์„œ a์— ๋ญ”๊ฐ€๋ฅผ ํ•œ ํ›„ ์ž์‹ ์˜ ๊ฒฐ๊ณผ b๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฌธ์ž์—ด ์ผ๋ถ€๋ฅผ ์†Œ๋น„ํ•œ ํ›„ ๊ทธ๊ฑธ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋Ÿฐ ๋™์ž‘์˜ ์˜ˆ์‹œ๋กœ ๋‹ค์ด๋‚ด๋ฏน ํŒŒ์„œ (Int, String) -> (Int, String)๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ์—ฌ๊ธฐ์„œ Int๋Š” ํŒŒ์‹ฑ ํ•œ ๋ฌธ์ž ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋Š” ๊ทธ๋Ÿฐ ํŒŒ์„œ ๋ช‡ ๊ฐœ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  ๋ฌธ์ž์—ด "cake"์— ๋Œ€ํ•ด ์‹คํ–‰ํ•˜๋ฉด ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. result remaining before 0 cake after first parser 1 ake after second parser 2 ke after third parser 3 e ์š”์ ์€ ๋‹ค์ด๋‚ด๋ฏน ํŒŒ์„œ๊ฐ€ ๋‘ ๊ฐ€์ง€ ์ผ์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด์ „ ํŒŒ์„œ์˜ ์ถœ๋ ฅ์— ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•˜๊ณ (a -> b), ์ž…๋ ฅ ์ŠคํŠธ๋ง์˜ ์ผ๋ถ€๋ฅผ ์†Œ๋น„ํ•˜๋ฉฐ([s] -> [s]), ๋”ฐ๋ผ์„œ ๊ทธ ํƒ€์ž…์€ DP ((a,[s]) -> (b,[s]))์ด๋‹ค. ์ด์ œ ๋‹จ์ผ ๋ฌธ์ž์— ๋Œ€ํ•œ ๋‹ค์ด๋‚ด๋ฏน ํŒŒ์„œ์˜ ๊ฒฝ์šฐ (ํƒ€์ž… (Char, String) -> (Char, String)) ์ฒซ ๋ฒˆ์งธ ์ž‘์—…์€ ์ž๋ช…ํ•˜๋‹ค. ์ด์ „ ํŒŒ์„œ์˜ ์ถœ๋ ฅ์„ ๋ฌด์‹œํ•˜๊ณ  ์šฐ๋ฆฌ๊ฐ€ ํŒŒ์‹ฑ ํ•œ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ŠคํŠธ๋ฆผ์—์„œ ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜ ์†Œ๋ชจํ•œ๋‹ค. dpCharA :: Char -> DynamicParser Char a Char dpCharA c = DP (\(_,_:xs) -> (c, xs)) ์—ฌ๊ธฐ์„œ ๋ช‡ ๊ฐ€์ง€ ์˜๋ฌธ์ด ๋“ค ๊ฒƒ์ด๋‹ค. ๊ฐ€๋ น ์ด์ „ ํŒŒ์„œ์˜ ์ถœ๋ ฅ์„ ๋ฌด์‹œํ•  ๊ฑฐ๋ผ๋ฉด ์• ์ดˆ์— ์™œ ๋ฐ›๋Š” ๊ฑธ๊นŒ? ๋‹ค์ด๋‚ด๋ฏน ํŒŒ์„œ๋Š” ์ŠคํŠธ๋ฆผ์—์„œ ๊ฐ€์ ธ์˜จ ๋ฌธ์ž๊ฐ€ ํŒŒ์‹ฑ ํ•  ๋ฌธ์ž์™€ ์ผ์น˜ํ•˜๋Š”์ง€ x == c๋กœ ๊ฒ€์‚ฌํ•ด์•ผ ํ•˜์ง€ ์•Š์„๊นŒ? ๋‘ ๋ฒˆ์งธ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์€ '์•„๋‹ˆ์˜ค'๋‹ค. ์‚ฌ์‹ค ์ด๊ฒƒ์ด ์š”์ง€์˜ ์ผ๋ถ€๋‹ค. ๊ทธ๋Ÿฐ ์ž‘์—…์ด ๋ถˆํ•„์š”ํ•œ ๊ฒƒ์€ ์Šคํƒœํ‹ฑ ํŒŒ์„œ๊ฐ€ ์ด๋ฏธ ๊ทธ ์ผ์„ ์ˆ˜ํ–‰ํ–ˆ์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์ž ํ•˜๋‚˜๋งŒ ๊ฒ€์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ™ฉ์ด ๊ฐ„๋‹จํ–ˆ๋‹ค. ๋ฌธ์ž ๋ช‡ ๊ฐœ๋ฅผ ์—ฐ์†์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ํŒŒ์„œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ๋‘ ๋ฒˆ์งธ ๋ฐ ๊ทธ ์ดํ›„์˜ ๋ฌธ์ž๋“ค์„ ํ™•์ธํ•˜๋Š” ๋‹ค์ด๋‚ด๋ฏน ํŒŒ์„œ๊ฐ€ ํ•„์š”ํ–ˆ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž ํŒŒ์„œ๋“ค ๋ช‡ ๊ฐœ๋ฅผ ์—ฐ๊ฒฐํ•ด์„œ ์ถœ๋ ฅ ๋ฌธ์ž์—ด์„ ๊ตฌ์„ฑํ•˜๊ธธ ์›ํ•œ๋‹ค๋ฉด ์ด์ „ ํŒŒ์„œ์˜ ์ถœ๋ ฅ์ด ํ•„์š”ํ–ˆ์„ ๊ฒƒ์ด๋‹ค. ๋‘ ํŒŒ์„œ๋ฅผ ํ•œ๋ฐ ๋ชจ์„ ์‹œ๊ฐ„์ด๋‹ค. ๋‹ค์Œ์€ ๋‹จ์ผ ๋ฌธ์ž๋ฅผ ์œ„ํ•œ S+D ์Šคํƒ€์ผ ํŒŒ์„œ๋‹ค. charA :: Char -> Parser Char a Char charA c = P (SP False [c]) (DP (\(_,_:xs) -> (c, xs))) ์ด ํŒŒ์„œ๋ฅผ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด runParser ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ž…๋ ฅ์— ์ •์  ๊ฒ€์‚ฌ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋™์  ํŒŒ์„œ๋ฅผ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. -- The Eq constraint on s is needed so that we can use elem. runParser :: Eq s => Parser s a b -> a -> [s] -> Maybe (b, [s]) runParser (P (SP emp _) (DP p)) a [] | emp = Just (p (a, [])) | otherwise = Nothing runParser (P (SP _ start) (DP p)) a input@(x:_) | x `elem` start = Just (p (a, input)) | otherwise = Nothing runParser๊ฐ€ [s] -> Maybe (b, [s]) ํƒ€์ž…์˜ ํ•จ์ˆ˜๋ฅผ ๋‚ด๋†“๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. ์ด๊ฒƒ์€ ์šฐ๋ฆฌ๊ฐ€ ์•ž์„œ ์„ค๋ช…ํ•œ ๋ชจ๋‚˜ ๋”• ํŒŒ์„œ์™€ ๋ณธ์งˆ์ ์œผ๋กœ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. ์ด์ œ charA๋ฅผ char์ฒ˜๋Ÿผ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. (์ฃผ์š”ํ•œ ์ฐจ์ด์ ์€ ๋”๋ฏธ ์ธ์ž๋กœ ()๋ฅผ ๋„˜๊ธด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค) charA๋Š” a ํƒ€์ž…์˜ ์ดˆ๊นƒ๊ฐ’์„ ์‹ค์ œ๋กœ๋Š” ํ™œ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ์€ ๋ง์ด ๋œ๋‹ค. ๋‹ค๋ฅธ ํŒŒ์„œ๋“ค์€ ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. GHCi> runParser (charA 'D') () "Do" Just ('D',"o") GHCi> runParser (charA 'D') () "Re" Nothing ์• ๋กœ ํ•ฉ์„ฑ์ž ๋„์ž…ํ•˜๊ธฐ ์ค€๋น„๋Š” ์ถฉ๋ถ„ํžˆ ํ–ˆ์œผ๋‹ˆ Parser s์— ๋Œ€ํ•œ Arrow ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์ž. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜๋ฉด ์• ๋กœ์˜ ์œ ์šฉํ•จ์„ ๋Š๋ผ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. instance Eq s => Arrow (Parser s) where arr๋Š” ์ž„์˜ ํ•จ์ˆ˜๋ฅผ ํŒŒ์‹ฑ ์• ๋กœ๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ ์•„์ฃผ ๋Š์Šจํ•œ ์˜๋ฏธ๋กœ "ํŒŒ์‹ฑ"์„ ํ™œ์šฉํ•œ๋‹ค. ๊ฒฐ๊ณผ ์• ๋กœ๋Š” ์˜ค๋กœ์ง€ ๋นˆ ๋ฌธ์ž์—ด๋งŒ์„ ๋ฐ›๋Š”๋‹ค. (์ฒซ ๋ฌธ์ž๋“ค์˜ ์ง‘ํ•ฉ์ด []) ์ด๊ฒƒ์˜ ์œ ์ผํ•œ ์—ญํ• ์€ ์ด์ „ ํŒŒ์‹ฑ ์• ๋กœ์˜ ์ถœ๋ ฅ์„ ๋ฐ›์•„์„œ ๊ทธ๊ฑธ๋กœ ๋ญ”๊ฐ€๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰ ์ž…๋ ฅ์„ ์ „ํ˜€ ์†Œ๋น„ํ•˜์ง€ ์•Š๋Š”๋‹ค. arr f = P (SP True []) (DP (\(b, s) -> (f b, s))) first ๊ฒฐํ•ฉ๊ธฐ๋„ ๊ฝค ์ง๊ด€์ ์ด๋‹ค. ์–ด๋–ค ํŒŒ์„œ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ ์ž…๋ ฅ์˜ ์Œ (b, d)๋ฅผ ๋ฐ›์•„๋“ค์ด๋Š” ์ƒˆ๋กœ์šด ํŒŒ์„œ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์‹ค์ œ๋กœ ํŒŒ์‹ฑ ํ•˜๋ ค๋Š” ๊ฒƒ์€ ์ž…๋ ฅ b์˜ ์ฒซ ๋ฒˆ์งธ ๊ตฌ์„ฑ์š”์†Œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์„ฑ๋ถ„์€ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ๋„˜๊ธด๋‹ค. first (P sp (DP p)) = P sp (DP (\((b, d),s) -> let (c, s') = p (b, s) in ((c, d),s'))) Category ์ธ์Šคํ„ด์Šค๋„ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค. id = arr id๊ฐ€ ์„ฑ๋ฆฝํ•ด์•ผ ํ•˜๋ฏ€๋กœ id๋Š” ์ž๋ช…ํ•˜๋‹ค. instance Eq s => Cat.Category (Parser s) where id = P (SP True []) (DP (\(b, s) -> (b, s))) -- Or simply: id = P (SP True []) (DP id) ๋ฐ˜๋ฉด (.)์˜ ๊ตฌํ˜„์€ ์ƒ๊ฐ์„ ์กฐ๊ธˆ ํ•ด์•ผ ํ•œ๋‹ค. ํŒŒ์„œ๋ฅผ ๋‘ ๊ฐœ ๋ฐ›์•„์„œ ๋‘ ์ธ์ž์˜ ์ •์  ๋ฐ ๋™์  ํŒŒ์„œ๋ฅผ ๋ชจ๋‘ ํ†ตํ•ฉํ•˜๋Š” ํ•ฉ์„ฑ ํŒŒ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. -- The Eq s constraint is needed for using union here. (P (SP empty1 start1) (DP p2)) . (P (SP empty2 start2) (DP p1)) = P (SP (empty1 && empty2) (if not empty1 then start1 else start1 `union` start2)) (DP (p2.p1)) ๋™์  ํŒŒ์„œ๋“ค์˜ ํ•ฉ์„ฑ์€ ๊ฝค๋‚˜ ์‰ฝ๋‹ค. ๋‹จ์ˆœํžˆ ํ•จ์ˆ˜ ํ•ฉ์„ฑ์„ ํ•˜๋ฉด ๋œ๋‹ค. ์ •์  ํŒŒ์„œ๋“ค์˜ ํ•ฉ์„ฑ์€ ์ƒ๊ฐ์„ ์กฐ๊ธˆ ํ•ด์•ผ ํ•œ๋‹ค. ๋จผ์ € ๋‘ ํŒŒ์„œ๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด๋งŒ์„ ๋ฐ›์•„๋“ค์ธ๋‹ค๋ฉด ํ•ฉ์„ฑ ํŒŒ์„œ๋„ ๋นˆ ๋ฌธ์ž์—ด๋งŒ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๋‹ค. ํ•ฉ๋ฆฌ์ ์ด๋‹ค. ์ด์ œ ์‹œ์ž‘ ๊ธฐํ˜ธ์— ๊ด€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์ž. Combining the dynamic parsers is easy enough; we just do function composition. Putting the static parsers together requires a little bit of thought. First of all, the combined parser can only accept the empty string if both parsers do. Fair enough, now how about the starting symbols? Well, the parsers are supposed to be in a sequence, so the starting symbols of the second parser shouldn't really matter. If life were simple, the starting symbols of the combined parser would only be start1. Alas, life is not simple, because parsers could very well accept the empty input. If the first parser accepts the empty input, then we have to account for this possibility by accepting the starting symbols from both the first and the second parsers ๊ทธ๋ž˜์„œ ์• ๋กœ๋Š” ์–ด๋””์— ์ข‹์€๊ฐ€? Parser ํƒ€์ž…์—์„œ ์Šคํƒœํ‹ฑ ํŒŒ์„œ ๋ถ€๋ถ„์„ ๋นผ๋ฉด ๋งˆ์น˜ ํ•จ์ˆ˜๋ฅผ ์œ„ํ•œ ์• ๋กœ ์ธ์Šคํ„ด์Šค์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. arr f = \(b, s) -> (f b, s) first p = \((b, d), s) -> let (c, s') = p (b, s) in ((c, d), s')) id = id p2. p1 = p2. p1 ๋ณ€์ˆ˜ s๊ฐ€ ์ •์˜๋ฅผ ์กฐ๊ธˆ ๋ฌ˜ํ•˜๊ฒŒ ๋งŒ๋“ค์ง€๋งŒ ๋Œ€์ฒด๋กœ first ํ•จ์ˆ˜์™€ ๋น„์Šทํ•ด ๋ณด์ธ๋‹ค. ์‚ฌ์‹ค ์ด๊ฒƒ์€ State ๋ชจ๋‚˜๋“œ/Kleisli morphism์„ ์œ„ํ•œ ์• ๋กœ ์ธ์Šคํ„ด์Šค์— ๊ฐ€๊น๋‹ค. (f :: b -> c, :: b -> State s c๋กœ ๋‘๋ฉด (.)๋Š” (<=<) = flip (>=>)๊ฐ€ ๋œ๋‹ค) TODO... That's fine, but we could have easily done that with bind in classic monadic style, with first becoming just an odd helper function that could be easily written with a bit of pattern matching. But remember, our Parser type is not just the dynamic parser โˆ’ it also contains the static parser. arr f = SP True [] first sp = sp (SP empty1 start1) >>> (SP empty2 start2) = (SP (empty1 && empty2) (if not empty1 then start1 else start1 `union` start2)) This is not at all a function, it's just pushing around some data types, and it cannot be expressed in a monadic way. But the Arrow interface can deal with just as well. And when we combine the two types, we get a two-for-one deal: the static parser data structure goes along for the ride along with the dynamic parser. The Arrow interface lets us transparently compose and manipulate the two parsers, static and dynamic, as a unit, which we can then run as a traditional, unified function. ์‹ค์ „์—์„œ์˜ ์• ๋กœ ์• ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์˜ˆ์‹œ๋“ค SQL ์ƒ์„ฑ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Opaleye (๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฌธ์„œ) Haskell XML Toolbox (ํ”„๋กœ์ ํŠธ ํŽ˜์ด์ง€์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฌธ์„œ)๋Š” ์• ๋กœ๋ฅผ ์‚ฌ์šฉํ•ด XML์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. Haskell Wiki์— Gentle Introduction to HXT๋ผ๋Š” ์œ„ํ‚ค ํŽ˜์ด์ง€๊ฐ€ ์žˆ๋‹ค. Netwire (๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฌธ์„œ)๋Š” ํ•จ์ˆ˜ํ˜• ๋ฐ˜์‘ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ (FRP) ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. FRP๋Š” ์ด๋ฒคํŠธ์™€ ์‹œ๊ฐ„์— ๋”ฐ๋ผ ๋ณ€ํ•˜๋Š” ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ํ˜• ํŒจ๋Ÿฌ๋‹ค์ž„์œผ๋กœ์„œ ์šฉ๋ก€๋กœ๋Š” ์œ ์ € ์ธํ„ฐํŽ˜์ด์Šค, ์‹œ๋ฎฌ๋ ˆ์ด์…˜, ๊ฒŒ์ž„์ด ์žˆ๋‹ค. Netwire๋Š” ์• ๋กœ ์ธํ„ฐํŽ˜์ด์Šค ์™ธ์— applicative ์ธํ„ฐํŽ˜์ด์Šค๋„ ํฌํ•จํ•œ๋‹ค. Yampa (Haskell Wiki ํŽ˜์ด์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฌธ์„œ)๋Š” ๋˜ ๋‹ค๋ฅธ ์• ๋กœ ๊ธฐ๋ฐ˜ FRP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋ฉฐ Netwire์˜ ์ „์‹ ์ด๋‹ค. Hughes์˜ ์• ๋กœ ์Šคํƒ€์ผ ํŒŒ์„œ๋Š” ๊ทธ์˜ 2000๋…„ ๋…ผ๋ฌธ์—์„œ ์ฒ˜์Œ ์†Œ๊ฐœ๋˜์—ˆ์ง€๋งŒ ๊ฐ€์šฉํ•œ ๊ตฌํ˜„์€ 2005๋…„ 5์›”์— Einar Karttunen์ด PArrows๋ฅผ ๋ฆด๋ฆฌ์Šคํ•˜๋ฉฐ ๋“ฑ์žฅํ–ˆ๋‹ค. ๋” ์ฝ์„๊ฑฐ๋ฆฌ Bibilography on arrows (haskell.org) ์• ๋กœ๋ฅผ ์†Œ๊ฐœํ•œ ๋…ผ๋ฌธ. ์ถœํŒ์‚ฌ์—์„œ ๋ฌด๋ฃŒ๋กœ ์—ด๋žŒํ•  ์ˆ˜ ์žˆ๋‹ค. โ†ฉ ์ด ๋‘ ๊ฐœ๋…์€ ๊ฐ๊ฐ static arrow์™€ Kleisli arrow๋กœ ์•Œ๋ ค์ ธ ์žˆ๋‹ค. "์• ๋กœ"๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๋ฏธ๋ฌ˜ํ•˜๊ฒŒ ๋‹ค๋ฅธ ๋‘ ๊ฐ€์ง€ ๋œป์œผ๋กœ ์“ฐ๋ฉด ์ด ๊ธ€์ด ๋งค์šฐ ํ˜ผ๋ž€์Šค๋Ÿฌ์›Œ์ง€๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ์ด ๋˜ ๋‹ค๋ฅธ ๋œป์˜ ๋™์˜์–ด์ธ "์‚ฌ์ƒ(morphism)"์„ ์ฑ„ํƒํ–ˆ๋‹ค. โ†ฉ ์ด๊ฒƒ๋“ค์ด static์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ์ด์œ ๋‹ค. ๊ฒฐ๊ณผ๋Š” ์ผ๋ จ์˜ ๊ณ„์‚ฐ์— ์˜ํ•ด ํ™•์ •๋œ๋‹ค. ์ƒ์„ฑ๋œ ๊ฐ’๋“ค์€ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ๋ชปํ•œ๋‹ค. โ†ฉ ์ž์„ธํ•œ ๊ฒƒ์€ Idioms are oblivious, arrows are meticulous, monads are promiscuous, by Sam Lindley, Philip Wadler and Jeremy Yallop๋ฅผ ๋ณผ ๊ฒƒ. โ†ฉ ์ค‘์ฒฉ๋œ ํŠœํ”Œ๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒŒ ์–ผ๋งˆ๋‚˜ ํ˜ผ๋ž€์Šค๋Ÿฌ์šด์ง€๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด "ํŽธํ•˜๊ฒŒ"๋ผ๋Š” ์กฐ๊ธˆ ๊ณผ์žฅ๋œ ๋ง์ด๋‹ค. โ†ฉ Arrow ๋ฐ ์šฐ๋ฆฌ๊ฐ€ ์—ฌ๊ธฐ์„œ ๋…ผ์˜ํ•˜๋Š” ๋‹ค๋ฅธ ์• ๋กœ ํด๋ž˜์Šค๋“ค์€ ์ €๋งˆ๋‹ค ๋ฒ•์น™์„ ๊ฐ€์ง„๋‹ค. ์—ฌ๊ธฐ์„œ ๊ทธ ๋ฒ•์น™๋“ค์„ ์งš๊ณ  ๋„˜์–ด๊ฐ€์ง€๋Š” ์•Š์„ ๊ฒƒ์ด์ง€๋งŒ Control.Arrow ๋ฌธ์„œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. โ†ฉ Data.Bifunctor๋Š” 7.10 ๋ฒ„์ „์—์„œ ์™€์„œ์•ผ ์ฝ”์–ด GHC ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ถ”๊ฐ€๋˜์—ˆ์œผ๋ฏ€๋กœ ์—ฌ๋Ÿฌ๋ถ„์ด ์˜ˆ์ „ ๋ฒ„์ „์„ ์“ฐ๊ณ  ์žˆ๋‹ค๋ฉด ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ ๊ฒฝ์šฐ bifunctors ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๋ฉด ๋˜๋ฉฐ, ์—ฌ๊ธฐ์—๋Š” ๋ฐ”์ดํŽ‘ํ„ฐ์™€ ๊ด€๋ จ๋œ ๋‹ค๋ฅธ ๋ชจ๋“ˆ๋“ค๋„ ๋“ค์–ด์žˆ๋‹ค. โ†ฉ Swierstra, Duponcheel. Deterministic, error correcting parser combinators. โ†ฉ Parsec์€ ์œ ๋ช…ํ•˜๊ณ  ๊ฐ•๋ ฅํ•œ ํŒŒ์‹ฑ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. ์ž์„ธํ•œ ๊ฒƒ์€ Hackage์˜ parsec ๋ฌธ์„œ๋ฅผ ๋ณผ ๊ฒƒ. โ†ฉ 07 Continuation Passing Style ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style continuation ์ด๋ž€? ์–ด๋–ค ์šฉ๋„์ธ๊ฐ€? continuation ์ „๋‹ฌํ•˜๊ธฐ ํ”ผํƒ€๊ณ ๋ผ์Šค thrice Cont ๋ชจ๋‚˜๋“œ callCC k์˜ ์‚ฌ์šฉ ์‹œ์  ๊ฒฐ์ •ํ•˜๊ธฐ ๋‚ด๋ง‰ ์˜ˆ์ œ: ๋ณต์žกํ•œ ์ œ์–ด ๊ตฌ์กฐ ์˜ˆ์ œ: ์˜ˆ์™ธ(exception) ์˜ˆ์ œ: ์ฝ”๋ฃจํ‹ด ์˜ˆ์ œ: ํŒจํ„ด ๋งค์นญ ๊ตฌํ˜„ํ•˜๊ธฐ ๋…ธํŠธ Continuation Passing Style (์ค„์—ฌ์„œ CPS)๋Š” ํ•จ์ˆ˜๊ฐ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์Šคํƒ€์ผ์ด๋‹ค. CPS๋Š” ๋Œ€์‹  continuation ์„ ์ด์šฉํ•ด ์ œ์–ด๊ถŒ์„ ๋„˜๊ธด๋‹ค. continuation์€ ๋‹ค์Œ์— ๋ฌด์—‡์ด ์ผ์–ด๋‚ ์ง€๋ฅผ ๊ธฐ์ˆ ํ•œ๋‹ค. ์ด๋ฒˆ ์žฅ์—์„œ๋Š” CPS๊ฐ€ ํ•˜์Šค์ผˆ์—์„œ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๊ณ , ํŠนํžˆ ๋ชจ๋‚˜๋“œ๋กœ CPS๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณธ๋‹ค. continuation ์ด๋ž€? ํ˜ผ๋ž€์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์•ž์—์„œ ๋ดค๋˜ ์˜ˆ์ œ๋ฅผ ์žฌ์ฐจ ์‚ดํŽด๋ณด๊ฒ ๋‹ค. ($) ์—ฐ์‚ฐ์ž๋ฅผ ์†Œ๊ฐœํ–ˆ์„ ๋•Œ > map ($ 2) [(2*), (4*), (8*)] [4,8,16] ์œ„ ํ‘œํ˜„์‹์€ map (*2) [2, 4, 8]๋ณด๋‹ค ์กฐ๊ธˆ ๊ธฐ๋ฌ˜ํ•˜์ง€๋งŒ ์ด์ƒํ•  ๊ฑด ์—†๋‹ค. ($) ์„น์…˜์€ ์ฝ”๋“œ๋ฅผ ๊ฑฐ๊พธ๋กœ ๋ณด์ด๊ฒŒ ๋งŒ๋“ค์–ด, ๋งˆ์น˜ ๊ฐ’์„ ํ•จ์ˆ˜์— ์ ์šฉํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋งŒ๋“ ๋‹ค. ์ด ๋ณ„๊ฑฐ ์•„๋‹Œ ๋’ค์ง‘๊ธฐ๊ฐ€ continuation passing style์˜ ํ•ต์‹ฌ์ด๋‹ค. CPS์˜ ๊ด€์ ์—์„œ ($ 2)๋Š” ์œ ์˜ˆ ๊ณ„์‚ฐ(suspended computation)์ด๋‹ค. ์ด ํ•จ์ˆ˜์˜ ํƒ€์ž…์€ ์ผ๋ฐ˜ํ™” ํƒ€์ž… (a -> r) -> r์œผ๋กœ์„œ, ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•œ๋‹ค. ์ธ์ž a -> r์ด ๋ฐ”๋กœ continuation์ด๋‹ค. ์ด๊ฒƒ์€ ๊ณ„์‚ฐ์ด ์–ด๋–ป๊ฒŒ ๊ฒฐ๊ณผ๋กœ ์ด์–ด์ง€๋Š”์ง€๋ฅผ ๊ธฐ์ˆ ํ•œ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ํ•จ์ˆ˜๋“ค์€ map์— ์˜ํ•ด continuation์œผ๋กœ์„œ ๊ณต๊ธ‰๋˜๊ณ , ์„ธ ๊ฐœ์˜ ๊ฐœ๋ณ„ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์‚ฐํ•œ๋‹ค. ์œ ์˜ˆ ๊ณ„์‚ฐ์€ ๋Œ€์ฒด๋กœ ํ‰์ดํ•œ ๊ฐ’๊ณผ ๋งž๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Œ์— ์ฃผ๋ชฉํ•  ๊ฒƒ. flip ($) 1์€ ์ž„์˜์˜ ๊ฐ’์„ ์œ ์˜ˆ ๊ณ„์‚ฐ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ์ด๊ฒƒ์˜ continuation์œผ๋กœ id์„ ์ „๋‹ฌํ•˜๋ฉด ์›๋ž˜ ๊ฐ’์„ ๋Œ๋ ค๋ฐ›๋Š”๋‹ค. ์–ด๋–ค ์šฉ๋„์ธ๊ฐ€? continuation์€ ๊ทธ์ € ํ•˜์Šค ์ผˆ ์ดˆ๋ณด์ž๋ฅผ ๋†€๋ž˜๋Š” ๋ฌด๋Œ€ ๋งˆ์ˆ ์ด ์•„๋‹ˆ๋‹ค. CPS๋กœ ํ”„๋กœ๊ทธ๋žจ์˜ ์ œ์–ด ํ๋ฆ„์„ ๋ช…์‹œ์ ์œผ๋กœ ์กฐ์ž‘ํ•˜๊ณ  ํฌ๊ฒŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋ น ์–ด๋–ค ํ”„๋Ÿฌ์‹œ์ €(์ ˆ์ฐจ)์—์„œ ์กฐ๊ธฐ์— ๋น ์ ธ๋‚˜์˜ค๋Š” ๊ฒƒ์„ continuation์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์™ธ์™€ ์‹คํŒจ๋„ continuation์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์„ฑ๊ณต์— ๋Œ€ํ•œ continuation, ์‹คํŒจ์— ๋Œ€ํ•œ continuation์„ ์ „๋‹ฌํ•˜๊ณ  ์•Œ๋งž์€ continuation์„ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ๋˜ ๋‹ค๋ฅธ ๊ฐ€๋Šฅ์„ฑ์€ ๊ณ„์‚ฐ์„ "์œ ์˜ˆ" ํ•˜๊ณ  ๋‹ค๋ฅธ ๋•Œ์— ๋˜๋Œ์•„์˜จ๋‹ค๊ฑฐ๋‚˜ ๋‹จ์ˆœํ•œ ํ˜•ํƒœ์˜ ๋™์‹œ์„ฑ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (์ฃผ๋ชฉํ•  ๋งŒํ•œ ๊ฒƒ์œผ๋กœ, ํ•˜์Šค์ผˆ์˜ ๊ตฌํ˜„์ฒด ์ค‘ ํ•˜๋‚˜์ธ Hugs๋Š” continuation์„ ์‚ฌ์šฉํ•ด ํ˜‘๋ ฅ์  cooperative ๋™์‹œ์„ฑ์„ ๊ตฌํ˜„ํ•œ๋‹ค) ํ•˜์Šค์ผˆ์—์„œ๋Š” continuation๋“ค์„ ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ ์ด์šฉํ•˜์—ฌ ๋ชจ๋‚˜๋“œ ์•ˆ์—์„œ ํฅ๋ฏธ๋กœ์šด ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ ๋Œ€๊ฐœ๋Š” ๋Œ€์•ˆ์ด ์žˆ๋Š”๋ฐ, ํŠนํžˆ ์ง€์—ฐ์„ฑ๊ณผ ์กฐํ™”๋ฅผ ์ด๋ฃฐ ๋•Œ ๊ทธ๋Ÿฌํ•˜๋‹ค. ์–ด๋–ค ๊ฒฝ์šฐ๋Š” CPS๊ฐ€ ํŠน์ • ์ƒ์„ฑ-ํŒจํ„ด ๋งค์นญ ์‹œํ€€์Šค๋“ค(์ฆ‰ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ณต์žกํ•œ ๊ตฌ์กฐ์ฒด๋ฅผ ํ˜ธ์ถœ์ž ์ธก์—์„œ ๊ฒฐ๊ตญ ๋ถ„ํ•ดํ•  ์˜ˆ์ •์ธ)์„ ์†Œ๊ฑฐํ•˜์—ฌ ์ˆ˜ํ–‰๋Šฅ๋ ฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ๋„ ํ•œ๋‹ค. ์ถฉ๋ถ„ํžˆ ๋˜‘๋˜‘ํ•œ ์ปดํŒŒ์ผ๋Ÿฌ๋ผ๋ฉด ๊ทธ๋Ÿฐ ์†Œ๊ฑฐ๋ฅผ ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜์ง€๋งŒ. continuation ์ „๋‹ฌํ•˜๊ธฐ continuation์„ ํ™œ์šฉํ•˜๋Š” ๊ธฐ์ดˆ์ ์ธ ๋ฐฉ๋ฒ•์€ ํ•จ์ˆ˜๊ฐ€ ํ‰๋ฒ”ํ•œ ๊ฐ’ ๋Œ€์‹  ์œ ์˜ˆ ๊ณ„์‚ฐ์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ๋‘ ๊ฐœ๋กœ ๊ทธ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค. ํ”ผํƒ€๊ณ ๋ผ์Šค ์˜ˆ: ๊ฐ„๋‹จํ•œ ๋ชจ๋“ˆ, continuation ๋ฏธ์‚ฌ์šฉ -- We assume some primitives add and square for the example: add :: Int -> Int -> Int add x y = x + y square :: Int -> Int square x = x * x pythagoras :: Int -> Int -> Int pythagoras x y = add (square x) (square y) ์œ ์˜ˆ ๊ณ„์‚ฐ์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ˆ˜์ •ํ•œ pythagoras๋Š” ์ด๋ ‡๊ฒŒ ์ƒ๊ฒผ๋‹ค. ์˜ˆ: ๊ฐ„๋‹จํ•œ ๋ชจ๋“ˆ. continuation ์‚ฌ์šฉํ•จ. -- We assume CPS versions of the add and square primitives, -- (note: the actual definitions of add_cps and square_cps are not -- in CPS form, they just have the correct type) add_cps :: Int -> Int -> ((Int -> r) -> r) add_cps x y = \k -> k (add x y) square_cps :: Int -> ((Int -> r) -> r) square_cps x = \k -> k (square x) pythagoras_cps :: Int -> Int -> ((Int -> r) -> r) pythagoras_cps x y = \k -> square_cps x $ \x_squared -> square_cps y $ \y_squared -> add_cps x_squared y_squared $ k pythagoras ์˜ˆ์ œ๋Š” ์ด๋ ‡๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. x๋ฅผ ์ œ๊ณฑํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ (\x_squared -> ...) continuation ์•ˆ์œผ๋กœ ๋˜์ง„๋‹ค y๋ฅผ ์ œ๊ณฑํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ (\y_squared -> ...) continuation ์•ˆ์œผ๋กœ ๋˜์ง„๋‹ค x_squared์™€ y_squared๋ฅผ ๋”ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ตœ์ƒ์œ„/ํ”„๋กœ๊ทธ๋žจ continuation์ธ k๋กœ ๋˜์ง„๋‹ค print๋ฅผ ํ”„๋กœ๊ทธ๋žจ continuation์œผ๋กœ์„œ ์ „๋‹ฌํ•˜์—ฌ GHCi์—์„œ ์ด๊ฒƒ์„ ์‹œํ—˜ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. *Main> pythagoras_cps 3 4 print 25 (Int -> r) -> r์˜ ๋ถ€๊ฐ€์ ์ธ ๊ด„ํ˜ธ๋“ค์„ ๋–ผ๊ณ  pythagoras_cps์˜ ํƒ€์ž…์„ ์›๋ž˜ pythagoras์˜ ํƒ€์ž…๊ณผ ๋น„๊ตํ•˜๋ฉด continuation์ด ์‚ฌ์‹ค์€ ์—ฌ๋ถ„์˜ ์ธ์ž๋กœ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. "continuation passing style"์ด๋ผ๋Š” ์ด๋ฆ„์ด ์™œ ๋ถ™์—ˆ๋Š”์ง€ ์•Œ ๊ฒƒ ๊ฐ™๋‹ค. ์—ญ์ž ์ฃผ: pythagoras_cps 3 4 print์˜ ์น˜ํ™˜ ๊ณผ์ • pyth_cps 3 4 print = square_cps x $ \x2 -> square_cps y $ \y2 -> add_cps x2 y2 print = square_cps x (\x2 -> square_cps y (\y2 -> add_cps x2 y2 print)) = square_cps x (\x2 -> square_cps y (\y2 -> print (add x2 y2)) = (\x2 -> square_cps y (\y2 -> print (add x2 y2)) (square x) = ((\x2 -> (\y2 -> print (add x2 y2)) (square y)) (square x) = (\y2 -> print (add (square x) y2)) (square y) = print (add (square x) (square y)) thrice ์˜ˆ: ๊ฐ„๋‹จํ•œ ๊ณ ์ฐจ ํ•จ์ˆ˜. continuation ๋ฏธ์‚ฌ์šฉ. thrice :: (a -> a) -> a -> a thrice f x = f (f (f x)) *Main> thrice tail "foobar" "bar" thrice ๊ฐ™์€ ๊ณ ์ฐจ ํ•จ์ˆ˜๋Š” CPS๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋‚˜๋ฉด ์ธ์ž๋„ CPS ํ˜•ํƒœ๋กœ ๋ฐ›๋Š”๋‹ค. ๋”ฐ๋ผ์„œ f :: a -> a๋Š” f_cps :: a -> ((a -> r) -> r)์ด ๋˜๊ณ  ์ตœ์ข… ํƒ€์ž…์€ thrice_cps :: (a -> ((a -> r) -> r)) -> a -> ((a -> r) -> r)์ด ๋œ๋‹ค. ์ •์˜์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ ํƒ€์ž…์—์„œ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋‚˜์˜จ๋‹ค. f๋ฅผ CPS ๋ฒ„์ „์œผ๋กœ ์น˜ํ™˜ํ•ด continuation์„ ์†์ˆ˜ ์ „๋‹ฌํ•œ๋‹ค. ์˜ˆ: ๊ฐ„๋‹จํ•œ ๊ณ ์ฐจ ํ•จ์ˆ˜. continuation ์‚ฌ์šฉํ•จ. thrice_cps :: (a -> ((a -> r) -> r)) -> a -> ((a -> r) -> r) thrice_cps f_cps x = \k -> f_cps x $ \fx -> f_cps fx $ \ffx -> f_cps ffx $ k Cont ๋ชจ๋‚˜๋“œ continuation-passing ํ•จ์ˆ˜๋ฅผ ์žฅ๋งŒํ–ˆ์œผ๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ์ด๋“ค์„ ํ•ฉ์„ฑํ•˜๋Š” ์˜๋ฆฌํ•œ ๋ฐฉ๋ฒ•, ํŠนํžˆ ์œ„์ฒ˜๋Ÿผ ์ค‘์ฒฉ ๋žŒ๋‹ค๋ฅผ ๊ธธ๊ฒŒ ์—ฐ์‡„ํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•˜๋‹ค. CPS ํ•จ์ˆ˜๋ฅผ suspended computation์— ์ ์šฉํ•˜๋Š” ์ปด๋น„๋„ค์ดํ„ฐ๊ฐ€ ์ข‹์€ ์ถœ๋ฐœ์ ์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํƒ€์ž…์ด ๊ฐ€๋Šฅํ•˜๋‹ค. chainCPS :: ((a -> r) -> r) -> (a -> ((b -> r) -> r)) -> ((b -> r) -> r) (๊ณ„์† ์ฝ๊ธฐ ์ „์— ์ด๊ฑธ ๊ตฌํ˜„ํ•ด ๋ณด๊ณ  ์‹ถ์€๊ฐ€? ํžŒํŠธ: ๊ฒฐ๊ณผ๊ฐ€ b -> r continuation์„ ์ทจํ•˜๋Š” ํ•จ์ˆ˜์ž„์„ ๊ธฐ์ˆ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•˜๋ผ. ๊ทธ๋‹ค์Œ์€ ํƒ€์ž…์ด ์—ฌ๋Ÿฌ๋ถ„์„ ์ด๋Œ ๊ฒƒ์ด๋‹ค.) ๊ทธ๋ฆฌ๊ณ  ์ด๊ฒƒ์ด ๊ทธ ๊ตฌํ˜„์ด๋‹ค. chainCPS s f = \k -> s $ \x -> f x $ k ๊ธฐ์กด์˜ suspended computation s๋ฅผ, ์ƒˆ๋กœ์šด ์œ ์˜ˆ ๊ณ„์‚ฐ(f์— ์˜ํ•ด ์ƒ์„ฑ๋จ)์„ ์ƒ์„ฑํ•˜๋Š” continuation์— ๊ณต๊ธ‰ํ•˜๊ณ , ์—ฌ๊ธฐ์— ์ตœ์ข… ๊ณ„์‚ฐ k๋ฅผ ๋„˜๊ธด๋‹ค. ๋†€๋ž„ ๊ฒƒ๋„ ์—†์ด ์ด๋Š” ์•ž์„  ์˜ˆ์ œ์˜ ์ค‘์ฒฉ ๋žŒ๋‹ค ํŒจํ„ด์„ ๊ณ ์Šค๋ž€ํžˆ ๋”ฐ๋ฅธ๋‹ค. chainCPS์˜ ํƒ€์ž…์ด ์ต์ˆ™ํ•˜์ง€ ์•Š์€๊ฐ€? (a -> r) -> r์„ (Monad m) => m a๋กœ, (b -> r) -> r์„ (Monad m) => m b๋กœ ์น˜ํ™˜ํ•˜๋ฉด (>>=) ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ์–ป๋Š”๋‹ค. ๋”์šฑ์ด ์šฐ๋ฆฌ์˜ ์˜ค๋žœ ์นœ๊ตฌ flip ($)์€ ๋งˆ์น˜ return ๊ฐ™์€ ์—ญํ• ์„ ํ•˜์—ฌ, ์œ ์˜ˆ ๊ณ„์‚ฐ์„ ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ๋งŒ๋“ ๋‹ค. ๋ง™์†Œ์‚ฌ, ๋ชจ๋‚˜๋“œ๋‹ค! ์ด์ œ ํ•„์š”ํ•œ ๊ฒƒ์€ 3 ์œ ์˜ˆ ๊ณ„์‚ฐ์„ ๊ฐ์‹ธ๋ฉฐ ํ†ต์ƒ์˜ ๋ž˜ํผ์™€ ์–ธ๋ž˜ํผ๊ฐ€ ์žˆ๋Š” Cont r a ํƒ€์ž…๋ฟ์ด๋‹ค. cont :: ((a -> r) -> r) -> Cont r a runCont :: Cont r a -> (a -> r) -> r Cont์— ๋Œ€ํ•œ ๋ชจ๋‚˜๋“œ ์ธ์Šคํ„ด์Šค๋Š” ์šฐ๋ฆฌ์˜ ํ‘œํ˜„๋ฒ•์—์„œ ๋ฐ”๋กœ ๋„์ถœ๋˜๋ฉฐ, ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ๋ž˜ํ•‘๊ณผ ์–ธ๋ž˜ํ•‘ ์ „๋žต์ด๋‹ค. instance Monad (Cont r) where return x = cont ($ x) s >>= f = cont $ \c -> runCont s $ \x -> runCont (f x) c ๊ฒฐ๋ก ์€ ์ด ๋ชจ๋‚˜๋“œ ์ธ์Šคํ„ด์Šค๊ฐ€ continuation passing(๊ทธ๋ฆฌ๊ณ  ๋žŒ๋‹ค ์—ฐ์‡„)๋ฅผ ์•”๋ฌต์ ์œผ๋กœ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋ชจ๋‚˜ ๋”• ๋ฐ”์ธ๋“œ๋Š” CPS ํ•จ์ˆ˜๋ฅผ ์œ ์˜ˆ ๊ณ„์‚ฐ์— ์ ์šฉํ•˜๊ณ , runCont๋Š” ์ตœ์ข… continuation์„ ์ œ๊ณตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ๋กœ ํ”ผํƒ€๊ณ ๋ผ์Šค ์˜ˆ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋œ๋‹ค. ์˜ˆ: Cont ๋ชจ๋‚˜๋“œ๋ฅผ ์ด์šฉํ•œ pythagoras -- Using the Cont monad from the transformers package. import Control.Monad.Trans.Cont add_cont :: Int -> Int -> Cont r Int add_cont x y = return (add x y) square_cont :: Int -> Cont r Int square_cont x = return (square x) pythagoras_cont :: Int -> Int -> Cont r Int pythagoras_cont x y = do x_squared <- square_cont x y_squared <- square_cont y add_cont x_squared y_squared callCC ์ž์—ฐ์Šค๋ ˆ ์œ ๋„๋˜๋Š” ๋ชจ๋‚˜๋“œ๋ฅผ ๋ณด๋Š” ๊ฒƒ์€ ์–ธ์ œ๋“ ์ง€ ๊ธฐ๋ถ„ ์ข‹์ง€๋งŒ, ์ด ์‹œ์ ์—์„œ ์‹ค๋ง์˜ ์—ฌ์ง€๊ฐ€ ๋‚จ์„ ์ˆ˜๋„ ์žˆ๋‹ค. CPS์˜ ์•ฝ์† ์ค‘ ํ•˜๋‚˜๋Š” continuation์„ ํ†ตํ•œ ์ •๋ฐ€ํ•œ ์ œ์–ด ํ๋ฆ„ ์กฐ์ž‘์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์šฐ๋ฆฌ๋Š” ํ•จ์ˆ˜๋ฅผ CPS๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๊ณง๋ฐ”๋กœ continuation์„ ๋ชจ๋‚˜๋“œ ๋’ค์— ์ˆจ๊ฒจ๋ฒ„๋ ธ๋‹ค. ์ด๊ฒƒ์„ ๋ฐ”๋กœ์žก๊ธฐ ์œ„ํ•ด continuation์˜ ๋ช…์‹œ์  ์ œ์–ด๋ฅผ (์›ํ•  ๋•Œ๋งŒ) ๋˜๋Œ๋ ค์ฃผ๋Š” callCC ํ•จ์ˆ˜๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. callCC ํ•จ์ˆ˜๋Š” ์•„์ฃผ ํŠน์ดํ•œ ํ•จ์ˆ˜์ธ๋ฐ, ์˜ˆ์ œ๋กœ ์†Œ๊ฐœํ•˜๋Š” ๊ฒƒ์ด ์ตœ๊ณ ๋‹ค. ์ž๋ช…ํ•œ ์˜ˆ์ œ๋กœ ์‹œ์ž‘ํ•ด ๋ณด์ž. ์˜ˆ: callCC๋ฅผ ์ด์šฉํ•œ square -- Without callCC square :: Int -> Cont r Int square n = return (n ^ 2) -- With callCC squareCCC :: Int -> Cont r Int squareCCC n = callCC $ \k -> k (n ^ 2) callCC์— ์ „๋‹ฌ๋˜๋Š” ์ธ์ž๋Š” ํ•จ์ˆ˜๋กœ์„œ ๊ทธ ๊ฒฐ๊ณผ๋Š” (์ผ๋ฐ˜ํ™”๋œ Cont r a ํƒ€์ž…์˜) ์œ ์˜ˆ ๊ณ„์‚ฐ์ด๋ฉฐ ์•ž์œผ๋กœ "callCC ๊ณ„์‚ฐ"์ด๋ผ ์นญํ•  ๊ฒƒ์ด๋‹ค. ์›์น™์ ์œผ๋กœ callCC ๊ณ„์‚ฐ์€ callCC ํ‘œํ˜„์‹ ์ „์ฒด๊ฐ€ ํ‰๊ฐ€๋˜๋Š” ๊ฒฐ๊ณผ๋‹ค. ์ฃผ์˜ํ•  ๊ฒƒ์€, callCC๋ฅผ ํŠน๋ณ„ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์ ์ด๊ธฐ๋„ ํ•œ๋ฐ, ์ธ์ž์— ๋Œ€ํ•œ ์ธ์ž์ธ k๋‹ค. k๋Š” ์‚ฌ์ถœ ์žฅ์น˜ ๊ฐ™์€ ํ•จ์ˆ˜๋‹ค. ์–ด๋””์„œ๋“  k๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด k์— ์ „๋‹ฌ๋œ ๊ฐ’์ด ์œ ์˜ˆ ๊ณ„์‚ฐ ๋‚ด์— ํ˜•์„ฑ๋˜๊ณ  ๊ทธ๋Ÿฌ๋ฉด callCC ํ˜ธ์ถœ ์‹œ์ ์—์„œ ์ œ์–ด ํ๋ฆ„ ๋‚ด์— ์‚ฝ์ž…๋œ๋‹ค. ์ด๊ฒƒ์€ ๋ฌด์กฐ๊ฑด ์ผ์–ด๋‚œ๋‹ค. ํŠนํžˆ callCC ๊ณ„์‚ฐ์—์„œ k ํ˜ธ์ถœ ๋‹ค์Œ์— ๋ฌด์—‡์ด ์˜ค๋“  ์ฆ‰์‹œ ์ œ๊ฑฐ๋œ๋‹ค. ๋‹ค๋ฅธ ๊ด€์ ์—์„œ ๋ณด๋ฉด k๋Š” callCC ๋‹ค์Œ์˜ ๊ณ„์‚ฐ์˜ ๋‚˜๋จธ์ง€๋ฅผ ํฌ์ฐฉํ•œ๋‹ค. k๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๊ทธ ์‹œ์ ์— ๊ฐ’์„ ๊ณ„์‚ฐ ๋‚ด์— ๋˜์ง„๋‹ค("callCC"์˜ ๋œป์€ "ํ˜„์žฌ continuation์œผ๋กœ ํ˜ธ์ถœํ•จ(call with current continuation)"์ด๋‹ค) ์ด ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์—์„œ๋Š” ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ํ‰์ดํ•œ return์ด์ง€๋งŒ callCC๋Š” ์ˆ˜๋งŽ์€ ๊ฐ€๋Šฅ์„ฑ์„ ์—ด์–ด์ฃผ๋ฉฐ ์ด์ œ ์ด๋ฅผ ํƒํ—˜ํ•˜๋ ค ํ•œ๋‹ค. k์˜ ์‚ฌ์šฉ ์‹œ์  ๊ฒฐ์ •ํ•˜๊ธฐ callCC๋Š” ์šฐ๋ฆฌ์—๊ฒŒ continuation ๋‚ด์— ๋ฌด์—‡์„ ๋˜์งˆ์ง€, ๊ทธ๊ฒƒ์ด ์–ธ์ œ ๋๋‚ ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ถŒํ•œ์„ ์ค€๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋Š” ์ด ํž˜์˜ ํ™œ์šฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ๋Š” ์ถœ๋ฐœ์ ์ด๋‹ค. ์˜ˆ: ๊ทธ๋Ÿด๋“ฏํ•œ ์ฒซ ๋ฒˆ์งธ callCC ํ•จ์ˆ˜ foo :: Int -> Cont r String foo x = callCC $ \k -> do let y = x ^ 2 + 3 when (y > 20) $ k "over twenty" return (show $ y - 4) foo๋Š” ์ž…๋ ฅ์„ ์ œ๊ณตํ•˜๊ณ  3์„ ๋”ํ•˜๋Š” ์•ฝ๊ฐ„ ๋ณ‘์ ์ธ ํ•จ์ˆ˜๋‹ค. ์ด ๊ณ„์‚ฐ์˜ ๊ฒฐ๊ณผ๊ฐ€ 20๋ณด๋‹ค ํฌ๋ฉด callCC ๊ณ„์‚ฐ์„ ์ฆ‰์‹œ ์ข…๋ฃŒํ•˜๊ณ (์ด ๊ฒฝ์šฐ ์ „์ฒด ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒ), foo์— ์ „๋‹ฌ๋  continuation ๋‚ด์— ๋ฌธ์ž์—ด "over twenty"๋ฅผ ๋˜์ง„๋‹ค. ์•„๋‹ˆ๋ฉด ์ด์ „์˜ ๊ณ„์‚ฐ์—์„œ 4๋ฅผ ๋นผ๊ณ , show๋ฅผ ์ ์šฉํ•˜๊ณ , continuation ๋‚ด์— ๋˜์ง„๋‹ค. ์ฃผ๋ชฉํ•  ๊ฒƒ์€ k๊ฐ€ ๋ช…๋ นํ˜• ์–ธ์–ด์—์„œ ํ•จ์ˆ˜๋ฅผ ์ฆ‰์‹œ ์ข…๋ฃŒํ•˜๋Š” return ๋ช…๋ น๋ฌธ์ฒ˜๋Ÿผ ์“ฐ์˜€๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿผ์—๋„ ์—ฌ์ „ํžˆ ํ•˜์Šค์ผˆ์ด๊ณ , k๋Š” ํ‰๋ฒ”ํ•œ ์ผ๊ธ‰ ํ•จ์ˆ˜์ด๋ฏ€๋กœ when ๊ฐ™์€ ๋‹ค๋ฅธ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ Reader์— ์ €์žฅํ•˜๋Š” ๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ž์—ฐ์Šค๋ ˆ callCC ํ˜ธ์ถœ์„ do ๋ธ”๋ก ์•ˆ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: do ๋ธ”๋ก์„ ์‚ฌ๋ฐ˜ํ•œ ๋” ๋ฐœ์ „๋œ `callCC` ์˜ˆ์ œ bar :: Char -> String -> Cont r Int bar c s = do msg <- callCC $ \k -> do let s0 = c : s when (s0 == "hello") $ k "They say hello." let s1 = show s0 return ("They appear to be saying " ++ s1) return (length msg) ๊ฐ’์„ ๊ฐ€์ง€๊ณ  k๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ callCC ํ˜ธ์ถœ ์ „์ฒด๊ฐ€ ๊ทธ ๊ฐ’์„ ์ทจํ•œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ k๋Š” ๋‹ค๋ฅธ ์–ธ์–ด์˜ goto ๋ช…๋ น์ฒ˜๋Ÿผ ๋œ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ k๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด callCC๋ฅผ ์ฒ˜์Œ ํ˜ธ์ถœํ•œ msg <- callCC $ ... ์ค„๋กœ ์‹คํ–‰ ํ๋ฆ„์„ ์˜ฎ๊ฒจ๋ฒ„๋ฆฐ๋‹ค. callCC์— ๋Œ€ํ•œ ์ธ์ž(๋‚ด๋ถ€ do ๋ธ”๋ก)๋Š” ๋” ์ด์ƒ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ ์˜ˆ์ œ๋Š” ์“ธ๋ชจ์—†๋Š” ์ค„์„ ํฌํ•จํ•˜๋Š” ์…ˆ์ด๋‹ค. ์˜ˆ: ํ•จ์ˆ˜ ๋น ์ ธ๋‚˜์˜ค๊ธฐ, ์“ธ๋ชจ์—†๋Š” ์ค„ ์žˆ์Œ. quux :: Cont r Int quux = callCC $ \k -> do let n = 5 k n return 25 quux๋Š” 25๊ฐ€ ์•„๋‹ˆ๋ผ 5๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. return 25 ์ค„์— ๋‹ค๋‹ค๋ฅด๊ธฐ ์ „์— quux๋ฅผ ์ข…๋ฃŒํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‚ด๋ง‰ ์ง€๊ธˆ๊นŒ์ง€ CPS๋ผ๋Š” ํŠธ๋ Œ๋“œ๋ฅผ ์ •์„ฑ๊ป ๋ถ„ํ•ดํ–ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์†Œ๊ฐœํ•  ๋•Œ๋ฉด ๊ทธ ํƒ€์ž…๋ถ€ํ„ฐ ๋ณด์—ฌ์ฃผ๊ณค ํ–ˆ๋Š”๋ฐ ์ด๋ฒˆ์—” ๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์•˜๋‹ค. ์ด์œ ๋Š” ๋‹จ์ˆœํ•˜๋‹ค. ์ด ํƒ€์ž…์€ ์ƒ๋‹นํžˆ ๋ณต์žกํ•ด์„œ ์ด ํ•จ์ˆ˜๊ฐ€ ๋ฌด์–ผ ํ•˜๊ณ  ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ํ†ต์ฐฐ์„ ๋ฐ”๋กœ ์ฃผ์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ ์ด์ œ callCC๋ฅผ ๋ถ„์„ํ•ด ๋ณผ ๋งŒํ•˜๋‹ค. ์ˆจ์„ ๊นŠ๊ฒŒ ๋“ค์ด์‰ฌ๊ณ ... callCC :: ((a -> Cont r b) -> Cont r a) -> Cont r a ์šฐ๋ฆฌ๊ฐ€ callCC์— ๋Œ€ํ•ด ์•Œ๊ณ  ์žˆ๋Š” ๊ฒƒ์— ๊ธฐ์ดˆํ•ด ์œ„๋ฌธ์žฅ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์ตœ์ข… ๊ฒฐ๊ณผ ํƒ€์ž…๊ณผ ์ธ์ž์˜ ๊ฒฐ๊ณผ ํƒ€์ž…์€ Cont r a์œผ๋กœ ๊ฐ™์•„์•ผ ํ•œ๋‹ค. k๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„ ๊ทธ์— ๋Œ€์‘ํ•˜๋Š” ๊ฒฐ๊ด๊ฐ’์ด ๊ฐ™์€ ํƒ€์ž…์ด์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿผ k์˜ ํƒ€์ž…์€? ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ k์˜ ์ธ์ž๋Š” ์œ ์˜ˆ ๊ณ„์‚ฐ ๋‚ด์— ํ˜•์„ฑ๋˜๊ณ  ์ด ๊ณ„์‚ฐ์€ callCC ํ˜ธ์ถœ ์‹œ์ ์— ์‚ฝ์ž…๋œ๋‹ค. ๋”ฐ๋ผ์„œ ํ›„์ž๊ฐ€ Cont r a ํƒ€์ž…์ด๋ผ๋ฉด k์˜ ์ธ์ž๋Š” a ํƒ€์ž…์ด์–ด์•ผ๋งŒ ํ•œ๋‹ค. ํฅ๋ฏธ๋กญ๊ฒŒ๋„ k์˜ ๊ฒฐ๊ณผ ํƒ€์ž…์€ ๊ฐ™์€ Cont r ์•ˆ์—๋งŒ ์žˆ์œผ๋ฉด ์ƒ๊ด€์—†๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด b๋Š” ์ž„์˜์˜ ํƒ€์ž…์„ ๋œปํ•œ๋‹ค. a ์ธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ์œ ์˜ˆ ๊ณ„์‚ฐ์ด callCC์— ๋”ฐ๋ผ์˜ค๋Š” continuation์„ ๋ฐ›์„ ๊ฒƒ์ด๊ณ , ๋”ฐ๋ผ์„œ k์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ทจํ•  continuation์€ ๋ฌด๊ด€ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ž ๊น k์˜ ๊ฒฐ๊ณผ ํƒ€์ž…์ด ๋ฌด์ž‘์œ„๋ผ๋Š” ๊ฒƒ์€ '์“ธ๋ชจ์—†๋Š” ์ค„' ์˜ˆ์ œ๋ฅผ ์กฐ๊ธˆ ์ˆ˜์ •ํ•œ ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ์™œ ํƒ€์ž… ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚ค๋Š”์ง€๋ฅผ ์„ค๋ช…ํ•œ๋‹ค. quux :: Cont r Int quux = callCC $ \k -> do let n = 5 when True $ k n k 25 k์˜ ๊ฒฐ๊ณผ ํƒ€์ž…์€ Cont r a ํ˜•ํƒœ์ด๋ฉด ๋ฌด์—‡์ด๋“  ๋  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ when์€ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ Cont r ()์œผ๋กœ ์ œํ•œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰์˜ k 25๋Š” quux์˜ ๊ฒฐ๊ณผ ํƒ€์ž…๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•ด๊ฒฐ์ฑ…์€ ๊ฐ„๋‹จํ•˜๋‹ค. ๋์˜ k๋ฅผ ํ‰๋ฒ”ํ•œ return์œผ๋กœ ๊ต์ฒดํ•˜๋ผ. callCC์˜ ๊ตฌํ˜„๊ณผ ํ•จ๊ป˜ ์ด ์ ˆ์„ ๋งˆ๋ฌด๋ฆฌํ•˜๊ฒ ๋‹ค. ์—ฌ๊ธฐ์„œ k๋ฅผ ์•Œ์•„๋ณด๊ฒ ๋Š”๊ฐ€? callCC f = cont $ \h -> runCont (f (\a -> cont $ \_ -> h a)) h ์ฉ ๋ช…์พŒํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋†€๋ผ์šด ๊ฒƒ์€ Cont์— ๋Œ€ํ•œ callCC, return, (>>=)์˜ ๊ตฌํ˜„์„ ๊ทธ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋กœ๋ถ€ํ„ฐ ์ž๋™์œผ๋กœ ๋„์ถœํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. Lennart Augustsson์˜ ํ”„๋กœ๊ทธ๋žจ Djinn[1]์ด ๊ทธ๋Ÿฐ ์ผ์„ ํ•ด์ค€๋‹ค. Djinn์˜ ์ด๋ก ์  ๋ฐฐ๊ฒฝ์— ๊ด€ํ•ด์„œ๋Š” Phil Gossett์˜ ๊ตฌ๊ธ€ tech talk๋ฅผ ๋ณด๋ผ. [2] ๊ทธ๋ฆฌ๊ณ  Pan Piponi์˜ ์ด ๊ธ€[3]์€ continuous passing style์„ ํŒŒ์ƒํ•˜๋Š” ๋ฐ Djinn์„ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ์ œ: ๋ณต์žกํ•œ ์ œ์–ด ๊ตฌ์กฐ ์ด์ œ ์ œ์–ด ํ๋ฆ„ ์กฐ์ž‘์— ๊ด€ํ•œ ์ข€ ๋” ํ˜„์‹ค์ ์ธ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž. ๋ฐ‘์— ์ œ์‹œ๋œ ์˜ˆ์‹œ๋Š” ์›๋ž˜ All about monads ํŠœํ† ๋ฆฌ์–ผ์˜ "The continuation monad" ์ ˆ์—์„œ ๊ฐ€์ ธ์˜จ ๊ฒƒ์œผ๋กœ, ํ—ˆ๊ฐ€๋ฅผ ๋ฐ›๊ณ  ์‚ฌ์šฉํ–ˆ๋‹ค. ์˜ˆ: ๋ณต์žกํ•œ ์ œ์–ด ๊ตฌ์กฐ๋ฅผ ์œ„ํ•ด Cont ์‚ฌ์šฉํ•˜๊ธฐ {- We use the continuation monad to perform "escapes" from code blocks. This function implements a complicated control structure to process numbers: Input (n) Output List Shown ========= ====== ========== 0-9 n none 10-199 number of digits in (n/2) digits of (n/2) 200-19999 n digits of (n/2) 20000-1999999 (n/2) backwards none >= 2000000 sum of digits of (n/2) digits of (n/2) -} fun :: Int -> String fun n = (`runCont` id) $ do str <- callCC $ \exit1 -> do -- define "exit1" when (n < 10) (exit1 (show n)) let ns = map digitToInt (show (n `div` 2)) n' <- callCC $ \exit2 -> do -- define "exit2" when ((length ns) < 3) (exit2 (length ns)) when ((length ns) < 5) (exit2 n) when ((length ns) < 7) $ do let ns' = map intToDigit (reverse ns) exit1 (dropWhile (=='0') ns') --escape 2 levels return $ sum ns return $ "(ns = " ++ (show ns) ++ ") " ++ (show n') return $ "Answer: " ++ str fun์€ ์ •์ˆ˜ n์„ ์ทจํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. ๊ทธ ๊ตฌํ˜„์€ Cont์™€ callCC๋ฅผ ์‚ฌ์šฉํ•ด ์ œ์–ด ๊ตฌ์กฐ๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š”๋ฐ ์ฃผ์„์— ์ ํžŒ ๋Œ€๋กœ n์ด ์†ํ•˜๋Š” ๋ฒ”์œ„์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ผ์„ ํ•œ๋‹ค. ์ฝ”๋“œ๋ฅผ ํ•ด๋ถ€ํ•ด ๋ณด์ž. ๋งจ ์œ„์˜ (runCont id)๋Š” ์šฐ๋ฆฌ๊ฐ€ Cont ๋ธ”๋ก์„ ์‹คํ–‰ํ•˜๋Š”๋ฐ ์ตœ์ข… continuation์ด id ์ž„์„ ๋œปํ•œ๋‹ค(๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด ์œ ์˜ˆ ๊ณ„์‚ฐ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ์ถ”์ถœํ•œ๋‹ค). fun์˜ ๊ฒฐ๊ณผ ํƒ€์ž…์ด Cont๋ฅผ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•„์ˆ˜๋‹ค. ๋”ฐ๋ผ์˜ค๋Š” callCC do ๋ธ”๋ก์˜ ๊ฒฐ๊ณผ์— str์„ ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. n์ด 10๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ณง์žฅ ์ข…๋ฃŒํ•˜๊ณ  n์„ ๋ณด์—ฌ์ค€๋‹ค. ์•„๋‹ˆ๋ฉด ๊ณ„์†ํ•œ๋‹ค. n `div` 2์˜ ์ž๋ฆฟ์ˆ˜๋“ค์„ ๋‹ด๋Š” ๋ฆฌ์ŠคํŠธ ns๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. n'(Int ๊ฐ’)์€ ๋”ฐ๋ผ์˜ค๋Š” callCC do ๋ธ”๋ก์˜ ๊ฒฐ๊ณผ์— ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค. length ns < 3์ด๋ฉด, ์ฆ‰ n `div` 2์˜ ์ž๋ฆฟ์ˆ˜๊ฐ€ 3๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ทธ ๊ฒฐ๊ด๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์ด ์•ˆ์ชฝ do ๋ธ”๋ก์„ ๋น ์ ธ๋‚˜์˜จ๋‹ค. n `div` 2์˜ ์ž๋ฆฟ์ˆ˜๊ฐ€ 5๋ณด๋‹ค ์ž‘์œผ๋ฉด ์›๋ž˜ n์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์•ˆ์ชฝ do ๋ธ”๋ก์„ ๋น ์ ธ๋‚˜์˜จ๋‹ค. n `div` 2์˜ ์ž๋ฆฟ์ˆ˜๊ฐ€ 7๋ณด๋‹ค ์ž‘์œผ๋ฉด ์•ˆ์ชฝ๊ณผ ๋ฐ”๊นฅ์ชฝ do ๋ธ”๋ก์„ ๋‘˜ ๋‹ค ๋น ์ ธ๋‚˜์˜ค๋Š”๋ฐ n `div` 2์˜ ์ž๋ฆฟ์ˆ˜๋“ค์„ ์ˆœ์„œ๋ฅผ ๋’ค์ง‘์–ด ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ n `div` 2์˜ ์ž๋ฆฟ์ˆ˜๋“ค์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ๋‚ด๋ถ€ do ๋ธ”๋ก์„ ์ข…๋ฃŒํ•œ๋‹ค. ๋ฌธ์ž์—ด "(ns = X) Y"์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์ด do ๋ธ”๋ก์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ X๋Š” ns ์ฆ‰ n `div` 2์˜ ์ž๋ฆฟ์ˆ˜๋“ค์ด๊ณ  Y๋Š” ์•ˆ์ชฝ do ๋ธ”๋ก์˜ ๊ฒฐ๊ณผ์ธ n'์ด๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•จ์ˆ˜ ์ „์ฒด๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” ๋ฌธ์ž์—ด "Answer: Z"์ธ๋ฐ ์—ฌ๊ธฐ์„œ Z๋Š” callCC do ๋ธ”๋ก์œผ๋กœ๋ถ€ํ„ฐ ์–ป์€ ๋ฌธ์ž์—ด์ด๋‹ค. ์˜ˆ์ œ: ์˜ˆ์™ธ(exception) continuation์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ๋ชจ๋ธ๋ง ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด continuation ๋‘ ๊ฐœ๋ฅผ ์œ ์ง€ํ•ด์•ผ ํ•œ๋‹ค. ํ•˜๋‚˜๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ๊ทธ ํ•ธ๋“ค๋Ÿฌ๋กœ ์šฐ๋ฆฌ๋ฅผ ๋ฐ๋ ค๊ฐ€๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ ํ›„์ฒ˜๋ฆฌ ์ฝ”๋“œ๋กœ ๋ฐ๋ ค๊ฐ„๋‹ค. ๋‹ค์Œ ํ•จ์ˆ˜๋Š” ๋‘ ์ˆ˜๋ฅผ ์ทจํ•ด ์ •์ˆ˜ ๋‚˜๋ˆ—์…ˆ์„ ํ•˜๋Š”๋ฐ, ๋ถ„๋ชจ๊ฐ€ 0์ธ ๊ฒฝ์šฐ ์‹คํŒจํ•œ๋‹ค. ์˜ˆ: ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋Š” div divExcpt :: Int -> Int -> (String -> Cont r Int) -> Cont r Int divExcpt x y handler = callCC $ \ok -> do err <- callCC $ \notOk -> do when (y == 0) $ notOk "Denominator 0" ok $ x `div` y handler err {- For example, runCont (divExcpt 10 2 error) id --> 5 runCont (divExcpt 10 0 error) id --> *** Exception: Denominator 0 -} ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š” ๊ฑธ๊นŒ? ์—ฌ๊ธฐ์„  callCC๋ฅผ ์ค‘์ฒฉํ•˜์—ฌ ๋‘ ๋ฒˆ ํ˜ธ์ถœํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ์•„๋ฌด ๋ฌธ์ œ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ์“ฐ์ผ continuation์— ์ด๋ฆ„์„ ๋ถ™์ด๊ณ , ๋‘ ๋ฒˆ์งธ๋Š” ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๊ณ  ์‹ถ์„ ๋•Œ ์“ฐ์ผ continuation์— ์ด๋ฆ„์„ ๋ถ™์ธ๋‹ค. ๋ถ„๋ชจ๊ฐ€ 0์ด ์•„๋‹ˆ๋ฉด x `div` y๋Š” ok continuation์— ๋˜์ ธ์ง€๊ณ  ๋”ฐ๋ผ์„œ ์‹คํ–‰ ํ๋ฆ„์€ divExcept์˜ ์ตœ์ƒ์œ„๋กœ ๋ฐ”๋กœ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค. ํ•˜์ง€๋งŒ ๋ถ„๋ชจ๊ฐ€ 0์ด๋ฉด notOK continuation์— ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋˜์ง€๊ณ  ์ด continuation์€ ๋‚ด๋ถ€ do ๋ธ”๋ก์„ ๋น ์ ธ๋‚˜์˜ค๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ด ๋ฌธ์ž์—ด์€ err์— ํ• ๋‹น๋˜๊ณ  handler์— ์ „๋‹ฌ๋œ๋‹ค. ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ณด๋‹ค ๋ฒ”์šฉ์ ์ธ ์ ‘๊ทผ๋ฒ•์„ ๋‹ค์Œ ํ•จ์ˆ˜์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ณ„์‚ฐ์„ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ(์ •ํ™•ํžˆ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋˜์ง€๋Š” ํ•จ์ˆ˜๋ฅผ ์ทจํ•˜๊ณ  ๊ณ„์‚ฐ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋‹ค), ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ทจํ•œ๋‹ค. ์ด ์˜ˆ๋Š” ์ผ๋ฐ˜ํ™”๋œ MonadCont ํด๋ž˜์Šค 4๋ฅผ ํ™œ์šฉํ•œ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” Cont์™€ ์ด์— ๋Œ€์‘ํ•˜๋Š” ContT ๋ณ€ํ™˜๊ธฐ๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ํฌํ•จํ•˜๊ณ  ์ด๊ฒƒ๋“ค์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ๋‹ค๋ฅธ continuation ๋ชจ๋‚˜๋“œ๋“ค๋„ ํฌํ•จํ•œ๋‹ค. ์˜ˆ: continuation์„ ์‚ฌ์šฉํ•œ ์ผ๋ฐ˜ํ™”๋œ try import Control.Monad.Cont tryCont :: MonadCont m => ((err -> m a) -> m a) -> (err -> m a) -> m a tryCont c h = callCC $ \ok -> do err <- callCC $ \notOk -> do x <- c notOk ok x h err ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ์€ try๋ฅผ ์‹ค์ œ๋กœ ํ™œ์šฉํ•œ ๊ฒƒ์ด๋‹ค. ์˜ˆ: try์˜ ํ™œ์šฉ data SqrtException = LessThanZero deriving (Show, Eq) sqrtIO :: (SqrtException -> ContT r IO ()) -> ContT r IO () sqrtIO throw = do ln <- lift (putStr "Enter a number to sqrt: " >> readLn) when (ln < 0) (throw LessThanZero) lift $ print (sqrt ln) main = runContT (tryCont sqrtIO (lift . print)) return ์ด ์˜ˆ์ œ์—์„œ ์˜ค๋ฅ˜๋ฅผ ๋˜์ง„๋‹ค๋Š” ๊ฒƒ์€ ์—์›Œ์‹ธ๋Š” callCC๋ฅผ ๋น ์ ธ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. sqrtIO ๋‚ด์˜ throw๋Š” tryCont์˜ ๋‚ด๋ถ€ callCC๋ฅผ ๋น ์ ธ๋‚˜์˜จ๋‹ค. ์˜ˆ์ œ: ์ฝ”๋ฃจํ‹ด ์ด ์ ˆ์—์„œ๋Š” CoroutineT ๋ชจ๋‚˜๋“œ๋ฅผ ์ œ์ž‘ํ•œ๋‹ค. ์ด ๋ชจ๋‚˜๋“œ๋Š” ์ƒˆ๋กœ์šด ์œ ์˜ˆ ์ฝ”๋ฃจํ‹ด์„ enqueue ํ•˜๋Š” fork์™€ ํ˜„ ์Šค๋ ˆ๋“œ๋ฅผ ์œ ์˜ˆํ•˜๋Š” yield๋ฅผ ์ œ๊ณตํ•œ๋‹ค. {-# LANGUAGE GeneralizedNewtypeDeriving #-} -- We use GeneralizedNewtypeDeriving to avoid boilerplate. As of GHC 7.8, it is safe. import Control.Applicative import Control.Monad.Cont import Control.Monad.State -- The CoroutineT monad is just ContT stacked with a StateT containing the suspended coroutines. newtype CoroutineT r m a = CoroutineT {runCoroutineT' :: ContT r (StateT [CoroutineT r m ()] m) a} deriving (Functor, Applicative, Monad, MonadCont, MonadIO) -- Used to manipulate the coroutine queue. getCCs :: Monad m => CoroutineT r m [CoroutineT r m ()] getCCs = CoroutineT $ lift get putCCs :: Monad m => [CoroutineT r m ()] -> CoroutineT r m () putCCs = CoroutineT . lift . put -- Pop and push coroutines to the queue. dequeue :: Monad m => CoroutineT r m () dequeue = do current_ccs <- getCCs case current_ccs of [] -> return () (p:ps) -> do putCCs ps p queue :: Monad m => CoroutineT r m () -> CoroutineT r m () queue p = do ccs <- getCCs putCCs (ccs++[p]) -- The interface. yield :: Monad m => CoroutineT r m () yield = callCC $ \k -> do queue (k ()) dequeue fork :: Monad m => CoroutineT r m () -> CoroutineT r m () fork p = callCC $ \k -> do queue (k ()) p dequeue -- Exhaust passes control to suspended coroutines repeatedly until there isn't any left. exhaust :: Monad m => CoroutineT r m () exhaust = do exhausted <- null <$> getCCs if not exhausted then yield >> exhaust else return () -- Runs the coroutines in the base monad. runCoroutineT :: Monad m => CoroutineT r m r -> m r runCoroutineT = flip evalStateT [] . flip runContT return . runCoroutineT' . (<* exhaust) ๋‹ค์Œ์€ ํ™œ์šฉ ์˜ˆ์‹œ๋‹ค. printOne n = do liftIO (print n) yield example = runCoroutineT $ do fork $ replicateM_ 3 (printOne 3) fork $ replicateM_ 4 (printOne 4) replicateM_ 2 (printOne 2) ์ถœ๋ ฅ: 4 2 3 4 ์˜ˆ์ œ: ํŒจํ„ด ๋งค์นญ ๊ตฌํ˜„ํ•˜๊ธฐ CPS ํ•จ์ˆ˜์˜ ํฅ๋ฏธ๋กœ์šด ์šฉ๋ฒ•์œผ๋กœ ํŒจํ„ด ๋งค์นญ์„ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์žˆ๋‹ค. ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ๊ทธ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค. ์˜ˆ์ œ: ๋‚ด์žฅ๋œ ๋งค์นญ check :: Bool -> String check b = case b of True -> "It's True" False -> "It's False" CPS๋ฅผ ๋ฐฐ์› ์œผ๋‹ˆ ์ด ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฆฌํŒฉํ† ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. type BoolCPS r = r -> r -> r true :: BoolCPS r true x _ = x false :: BoolCPS r false _ x = x check :: BoolCPS String -> String check b = b "It's True" "It's False" *Main> check true "It's True" *Main> check false "It's False" ์—ฌ๊ธฐ์„œ ์ผ์–ด๋‚˜๋Š” ์ผ์„ ๋ณด๋ฉด True์™€ False๋ฅผ ํ‰๋ฒ”ํ•œ ๊ฐ’ ๋Œ€์‹  ํ•จ์ˆ˜๋กœ ํ‘œํ˜„ํ–ˆ๋Š”๋ฐ, ์ด ํ•จ์ˆ˜๋“ค์€ ์ „๋‹ฌ๋ฐ›์€ ์ธ์ž๋“ค ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋˜๋Š” ๋‘ ๋ฒˆ์งธ๋ฅผ ์„ ํƒํ•œ๋‹ค. true์™€ false๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๊ฒŒ ํ–‰๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ํŒจํ„ด ๋งค์นญ๊ณผ ๋™์ผํ•œ ํšจ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”์šฑ์ด True, False์™€ true, false๋Š” \b -> b True False์™€ \b -> if b then true else false๋ฅผ ํ†ตํ•ด ์„œ๋กœ ๋ณ€ํ™˜๋  ์ˆ˜ ์žˆ๋‹ค. ์กฐ๊ธˆ ๋” ๋ณต์žกํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์ด๊ฒƒ์ด CPS์™€ ์–ด๋–ป๊ฒŒ ์—ฐ๊ด€๋˜๋Š”์ง€ ์•Œ์•„๋ณด์ž. ์˜ˆ์ œ: ๋” ๋ณต์žกํ•œ ํŒจํ„ด ๋งค์นญ๊ณผ ๊ทธ๊ฒƒ์˜ CPS ๋ฒ„์ „ data Foobar = Zero | One Int | Two Int Int type FoobarCPS r = r -> (Int -> r) -> (Int -> Int -> r) -> r zero :: FoobarCPS r zero x _ _ = x one :: Int -> FoobarCPS r one x _ f _ = f x two :: Int -> Int -> FoobarCPS r two x y _ _ f = f x y fun :: Foobar -> Int fun x = case x of Zero -> 0 One a -> a + 1 Two a b -> a + b + 2 funCPS :: FoobarCPS Int -> Int funCPS x = x 0 (+1) (\a b -> a + b + 2) *Main> fun Zero *Main> fun $ One 3 *Main> fun $ Two 3 4 *Main> funCPS zero *Main> funCPS $ one 3 *Main> funCPS $ two 3 4 ์•ž์„  ์˜ˆ์ œ์™€ ๋น„์Šทํ•˜๊ฒŒ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ํ‘œํ˜„ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜-๊ฐ’๋“ค์€ ์ „๋‹ฌ๋ฐ›์€ continuation๋“ค ์ค‘ ๋Œ€์‘ํ•˜๋Š” (์ฆ‰ ๋งค์นญ๋˜๋Š”) ๊ฒƒ์„ ๊ณจ๋ผ์„œ ์ „์ž์— ๋ณด๊ด€๋˜์–ด ์žˆ๋˜ ๊ฐ’๋“ค์„ ํ›„์ž์— ์ „๋‹ฌํ•œ๋‹ค. ํฅ๋ฏธ๋กœ์šด ์ ์€ ์ด ๊ณผ์ •์— ๋น„๊ต๊ฐ€ ๊ด€์—ฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์•Œ๊ณ  ์žˆ๋“ฏ์ด ํŒจํ„ด ๋งค์นญ์€ Eq์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹Œ ํƒ€์ž…๋“ค์—๋„ ์ž‘๋™ํ•œ๋‹ค. ํ•จ์ˆ˜-๊ฐ’๋“ค์€ ํŒจํ„ด๋“ค์ด ๋ฌด์—‡์ธ์ง€ "์•Œ๊ณ " ์˜ฌ๋ฐ”๋ฅธ continuation์„ ์ž๋™์œผ๋กœ ์„ ํƒํ•œ๋‹ค. ์ด๊ฑธ ๊ฐ€๋ น pattern_match :: [(pattern, result)] -> value -> result ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์™ธ๋ถ€์—์„œ ์ˆ˜ํ–‰ํ•œ๋‹ค๋ฉด ๋งค์นญํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์œ„ํ•ด ํŒจํ„ด๋“ค๊ณผ ๊ฐ’๋“ค์„ ์กฐ์‚ฌํ•˜๊ณ  ๋น„๊ตํ•ด์•ผ ํ–ˆ์„ ๊ฒƒ์ด๊ณ , ๋”ฐ๋ผ์„œ Eq ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•„์š”ํ–ˆ์„ ๊ฒƒ์ด๋‹ค. ๋…ธํŠธ ์ฆ‰ \x -> ($ x)์œผ๋กœ, ์™„์ „ํžˆ ํ’€์–ด์“ฐ๋ฉด \x -> \k -> k x์ด๋‹ค. โ†ฉ attoparsec์€ ํผํฌ๋จผ์Šค๋ฅผ ์œ„ํ•ด CPS๋ฅผ ํ™œ์šฉํ•œ ์˜ˆ์‹œ๋‹ค. โ†ฉ ๋ชจ๋‚˜๋“œ ๋ฒ•์น™์ด ์„ฑ๋ฆฝํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๋…์ž์—๊ฒŒ ์—ฐ์Šต ๋ฌธ์ œ๋กœ ๋‚จ๊ธด๋‹ค. โ†ฉ Control.Monad.Cont ๋ชจ๋“ˆ์€ mtl ํŒจํ‚ค์ง€์— ๋“ค์–ด์žˆ๋‹ค. โ†ฉ 08 ์ง€ํผ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Zippers ํ…Œ์„ธ์šฐ์Šค์™€ ์ง€ํผ ๋ฏธ๊ถ(The Labyrinth) ์•„๋ผ๋“œ๋„ค์˜ ์ง€ํผ(Zipper) ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๋ฏธ๋ถ„ ๊ธฐ๊ณ„์  ๋ฏธ๋ถ„ ๋ฏธ๋ถ„์„ ํ†ตํ•œ ์ง€ํผ ๊ณ ์ •์ ์˜ ๋ฏธ๋ถ„ ํ•จ์ˆ˜๋ฅผ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ๋ฏธ๋ถ„ํ•˜๊ธฐ ์ง€ํผ vs ๋ฌธ๋งฅ ๊ฒฐ๋ก  ๋…ธํŠธ ํ…Œ์„ธ์šฐ์Šค์™€ ์ง€ํผ ๋ฏธ๊ถ(The Labyrinth) "ํ…Œ์„ธ์šฐ์Šค. ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ์–ด." Ancient Geeks ์ฃผ์‹ํšŒ์‚ฌ์˜ ์ตœ๊ณ ๋งˆ์ผ€ํŒ…๊ฒฝ์˜์ž ํ˜ธ๋จธ๊ฐ€ ๋งํ–ˆ๋‹ค. ํ…Œ์„ธ์šฐ์Šค๋Š” ๋ฏธ๋…ธํƒ€์šฐ๋กœ์Šค ํ”ผ๊ฒจโ„ข๋ฅผ ์„ ๋ฐ˜์— ์˜ฌ๋ ค๋†“๊ณ  ๊ณ ๊ฐœ๋ฅผ ๋„๋•์˜€๋‹ค. "์š”์ฆ˜ ์• ๋“ค์€ ๊ณ ๋Œ€ ์‹ ํ™”์— ๊ด€์‹ฌ์ด ์—†์–ด. ์ŠคํŒŒ์ด๋”๋งจ์ด๋‚˜ ์Šคํฐ์ง€ ๋ฐฅ ๊ฐ™์€ ์ตœ์‹  ์˜์›…์„ ์ข‹์•„ํ•˜์ง€." ์˜์›…์ด๋ผ, ํ…Œ์„ธ์šฐ์Šค๋Š” ํฌ๋ ˆํƒ€ 1์—์„œ ์ž์‹ ์ด ์–ผ๋งˆ๋‚˜ ์˜์›…์ ์ด์—ˆ๋Š”์ง€ ์ž˜ ์•Œ๊ณ  ์žˆ๋‹ค. ํ•˜์ง€๋งŒ "์š”์ฆ˜ ์˜์›…๋“ค"์€ ์‚ฌ์‹ค์ ์œผ๋กœ ๋ณด์ด๋ ค๋Š” ์‹œ๋„์กฐ์ฐจ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์„ฑ๊ณต ์š”์ธ์ด ๋ญ˜๊นŒ? ์–ด์จŒ๋“ , ์ƒํ’ˆ์ด ์•ˆ ํŒ”๋ฆฌ๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์ง€ ์•Š์œผ๋ฉด ์ฃผ์ฃผ๋“ค์ด Ancient Geeks ์ฃผ์‹ํšŒ์‚ฌ๋ฅผ ์œ„ํ•ด ์Šคํ‹ฑ์Šค ๊ฐ•์— ๊ธธ์„ ํ„ฐ๋†“์„ ์ง€๋„ ๋ชจ๋ฅธ๋‹ค. "์•Œ์•˜๋‹ค! ํ…Œ์„ธ์šฐ์Šค. ์ƒ๊ฐ์ด ์žˆ์–ด. ๋„ˆ์™€ ๋ฏธ๋…ธํƒ€์šฐ๋กœ์Šค ์ด์•ผ๊ธฐ๋ฅผ ์ปดํ“จํ„ฐ ๊ฒŒ์ž„์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฑฐ์•ผ! ์–ด๋•Œ?" ํ˜ธ๋จธ ๋ง์ด ๋งž๋‹ค. ์ฑ…๋„ ์žˆ๊ณ , (์ฐจํŠธ๋ฅผ ์„๊ถŒํ•œ) ์„œ์‚ฌ์‹œ๋„ ์žˆ๊ณ , ์˜ํ™” 3๋ถ€์ž‘์—, ์…€ ์ˆ˜ ์—†๋Š” ํ…Œ์„ธ์šฐ์Šค & ๋ฏธ๋…ธํƒ€์šฐ๋ฃจ์Šคโ„ข ๊ธฐ๋ฏน์ด ์žˆ๋Š”๋ฐ ์ปดํ“จํ„ฐ ๊ฒŒ์ž„๋งŒ ์—†๋‹ค. "์™„๋ฒฝํ•ด. ์ž, ํ…Œ์„ธ์šฐ์Šค. ๊ทธ๊ฑธ ์ด์ œ ๋„ค๊ฐ€ ๊ตฌํ˜„ํ•ด์•ผ ํ•จใ…‹" ์ฐธ๋œ ์˜์›… ํ…Œ์„ธ์šฐ์Šค๋Š” ํšŒ์‚ฌ์˜ ๊ธฐ์‚ฌํšŒ์ƒ์„ ์œ„ํ•œ ์ œํ’ˆ์„ ๊ตฌํ˜„ํ•  ์–ธ์–ด๋กœ ํ•˜์Šค์ผˆ์„ ์„ ํƒํ–ˆ๋‹ค. ๋ฌผ๋ก  ๋ฏธ๋…ธ ํƒ€์šฐ๋ฃจ์Šค์˜ ๋ฏธ๊ถ์„ ํƒํ—˜ํ•˜๋Š” ๊ฒŒ ๊ฒŒ์ž„์˜ ํ•˜์ด๋ผ์ดํŠธ๋‹ค. ํ…Œ์„ธ์šฐ์Šค๋Š” ๊ณ ๋ฏผ์— ๋น ์กŒ๋‹ค. "2์ฐจ์› ๋ฏธ๊ถ์ด ์žˆ๋Š”๋ฐ ํ†ต๋กœ๋“ค์ด ์—ฌ๋Ÿฌ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐˆ๋ผ์ง€์ง€. ๋ฌผ๋ก  ์„ธ์„ธํ•œ ๊ธธ์ด์™€ ๊ฐ๋„๋กœ ์ถ”์ƒํ™”ํ•  ์ˆ˜ ์žˆ์–ด. ๋‚˜๊ฐ€๋Š” ๊ธธ์„ ์ฐพ์œผ๋ ค๋ฉด ๊ทธ ๊ฒฝ๋กœ๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ฐˆ๋ผ์ง€๋Š” ์ง€๋งŒ ์•Œ๋ฉด ๋ผ. ๋ฏธ๊ถ์„ ํŠธ๋ฆฌ๋กœ ๋ชจ๋ธ๋ง ํ•˜๋Š” ๊ฒŒ ์‰ฌ์šธ ๊ฑฐ์•ผ. ์ด๋Ÿฌ๋ฉด ํ•œ ๊ฐˆ๋ฆผ๊ธธ์˜ ๋‘ ๋ถ„๊ธฐ๋Š” ๋” ๊นŠ์ด ๋“ค์–ด๊ฐ์— ๋”ฐ๋ผ ์„œ๋กœ ํ•ฉ์ณ์ง€์ง€ ์•Š๊ณ  ํ”Œ๋ ˆ์ด์–ด๋Š” ๋น™๊ธ€๋น™๊ธ€ ๋Œ ์ˆ˜ ์—†์ง€. ํ•˜์ง€๋งŒ ๊ธธ์„ ์žƒ์„ ๊ฐ€๋Šฅ์„ฑ์ด ์ถฉ๋ถ„ํ•ด. ๊ทธ๋ฆฌ๊ณ  ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ธ๋‚ด์‹ฌ์ด ์žˆ์œผ๋ฉด ์™ผ์† ์งš๊ธฐ๋กœ ๋ฏธ๊ถ ์ „์ฒด๋ฅผ ํƒํ—˜ํ•  ์ˆ˜ ์žˆ์–ด." data Node a = DeadEnd a | Passage a (Node a) | Fork a (Node a) (Node a) ์˜ˆ์‹œ ๋ฏธ๊ถ๊ณผ ๊ทธ ํŠธ๋ฆฌ ํ‘œํ˜„ ํ…Œ์„ธ์šฐ์Šค๋Š” ๋ฏธ๊ถ์˜ ๋…ธ๋“œ๋“ค์ด a ํƒ€์ž…์˜ ์—ฌ๋ถ„์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ณด๊ด€ํ•˜๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค. ์ด ๋ณ€์ˆ˜๋Š” ๋‚˜์ค‘์— ๋…ธ๋“œ์˜ ์œ„์น˜, ์ฃผ๋ณ€์˜ ์กฐ๋ช…, ๋ฐ”๋‹ฅ์— ๋†“์ธ ๊ฒŒ์ž„ ์•„์ดํ…œ ๋ชฉ๋ก, ๊ทผ์ฒ˜์—์„œ ์–ด์Šฌ๋ ๊ฑฐ๋ฆฌ๋Š” ๋ชฌ์Šคํ„ฐ ๋ชฉ๋ก ๊ฐ™์€ ๊ฒŒ์ž„ ๊ด€๋ จ ์ •๋ณด๋ฅผ ๋‹ด์„ ๊ฒƒ์ด๋‹ค. ์ด์ œ ๋„์šฐ๋ฏธ ํ•จ์ˆ˜ ๋‘ ๊ฐœ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. get :: Node a -> a put :: a -> Node a -> Node a ์ด๊ฒƒ๋“ค์€ Node a์˜ ๋ชจ๋“  ์ƒ์„ฑ์ž์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž์— ์ €์žฅ๋œ a ํƒ€์ž…์˜ ๊ฐ’์„ ํš๋“ํ•˜๊ณ  ๋ณ€๊ฒฝํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ get๊ณผ put์„ ๊ตฌํ˜„ํ•˜๋ผ. get์˜ ๊ฒฝ์šฐ get (Passage x _) = x์ด๋‹ค. ๊ตฌ์ฒด์ ์ธ ์˜ˆ์‹œ๋กœ, ์œ„ ๊ทธ๋ฆผ์˜ ๋ฏธ๊ถ์„ Node (Int, Int) ํƒ€์ž…์˜ ๊ฐ’์„ ์ด์šฉํ•ด ์ž‘์„ฑํ•˜๋ผ. ์—ฌ๋ถ„์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ (Int, Int)๋Š” ๋…ธ๋“œ์˜ ์ง๊ต ์ขŒํ‘œ๋ฅผ ๋‹ด๋Š”๋‹ค. "๊ทธ๋Ÿฐ๋ฐ ๋ฏธ๊ถ์—์„œ ํ”Œ๋ ˆ์ด์–ด์˜ ํ˜„ ์œ„์น˜๋ฅผ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ•˜์ง€? ํ”Œ๋ ˆ์ด์–ด๋Š” ์™ผ์ชฝ์ด๋‚˜ ์˜ค๋ฅธ์ชฝ ๋ถ„๊ธฐ๋ฅผ ์„ ํƒํ•ด์„œ ๋” ๊นŠ์ˆ™์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์–ด. ์ด๋ ‡๊ฒŒ..." turnRight :: Node a -> Maybe (Node a) turnRight (Fork _ l r) = Just r turnRight _ = Nothing "ํ•˜์ง€๋งŒ ๋ฏธ๊ถ์˜ ํ˜„ ์ตœ์ƒ์œ„๋ฅผ ํ•˜์œ„ ๋ฏธ๊ถ์œผ๋กœ ์น˜ํ™˜ํ•˜๋ฉด ๋˜๋Œ์•„์˜ฌ ์ˆ˜๊ฐ€ ์—†์–ด." ํ…Œ์„ธ์šฐ์Šค๋Š” ๊ณ ์‹ฌํ–ˆ๋‹ค. "์•„, ์•„๋ผ๋“œ๋„ค์˜ ์‹คํƒ€๋ž˜๋กœ ๋˜๋Œ์•„์˜ค๊ธฐ ๋ฐฉ๋ฒ•์„ ์‘์šฉํ•  ์ˆ˜ ์žˆ์–ด. ํ”Œ๋ ˆ์ด์–ด์˜ ์œ„์น˜๋ฅผ ์‹คํƒ€๋ž˜๋ฅผ ํ’€์–ดํ—ค์นœ ๋ถ„๊ธฐ๋“ค์˜ ๋ชฉ๋ก์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋ฏธ๊ถ์€ ๊ทธ๋Œ€๋กœ ๋‚จ์•„์žˆ์ง€." data Branch = KeepStraightOn | TurnLeft | TurnRight type Thread = [Branch] ์•„๋ผ๋“œ๋„ค์˜ ์‹ค๋กœ ํ‘œํ˜„ํ•œ ํ”Œ๋ ˆ์ด์–ด ์œ„์น˜ "๊ฐ€๋ น [TurnRight, KeepStraightOn] ์‹ค๋ญ‰์น˜๋Š” ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ž…๊ตฌ์—์„œ ์˜ค๋ฅธ์ชฝ ๊ฐˆ๋ฆผ๊ธธ๋กœ ๊ฐ€๊ณ  Passage ํ•˜๋‚˜๋ฅผ ์ง์ง„ํ•ด์„œ ์ง€๊ธˆ ์œ„์น˜์— ๋„๋‹ฌํ–ˆ๋‹ค๋Š” ๋œป์ด์•ผ. ์ด ์‹ค๋ญ‰์น˜๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฏธ๊ถ์„ ๋” ๊นŠ์ˆ™์ด ๋“ค์–ด๊ฐ€๊ฑฐ๋‚˜ ๋˜๋Œ์•„์˜ฌ ์ˆ˜๊ฐ€ ์žˆ์ง€. ์˜ˆ๋ฅผ ๋“ค์–ด turnRight ํ•จ์ˆ˜๋Š” ์‹ค๋ญ‰์น˜์— TurnRight์„ ๋ง๋ถ™์ด๋Š” ๊ฑฐ์•ผ." turnRight :: Thread -> Thread turnRight t = t ++ [TurnRight] "์—ฌ๋ถ„์˜ ์ž๋ฃŒ, ์ฆ‰ ๊ฒŒ์ž„ ๊ด€๋ จ ์•„์ดํ…œ ๊ฐ™์€ ๊ฒƒ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ์‹ค๋ญ‰์น˜๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉด ๋˜๊ณ ." retrieve :: Thread -> Node a -> a retrieve [] n = get n retrieve (KeepStraightOn:bs) (Passage _ n) = retrieve bs n retrieve (TurnLeft :bs) (Fork _ l r) = retrieve bs l retrieve (TurnRight :bs) (Fork _ l r) = retrieve bs r ์—ฐ์Šต๋ฌธ์ œ a -> a ํƒ€์ž…์˜ ํ•จ์ˆ˜๋ฅผ ํ”Œ๋ ˆ์ด์–ด ์œ„์น˜์— ์žˆ๋Š” ์—ฌ๋ถ„์˜ ๋ฐ์ดํ„ฐ์— ์ ์šฉํ•˜๋Š” update ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ์ด ํ•ด๊ฒฐ์ฑ…์— ๋Œ€ํ•œ ํ…Œ์„ธ์šฐ์Šค์˜ ๋งŒ์กฑ์€ ๊ทธ๋ฆฌ ์˜ค๋ž˜๊ฐ€์ง€ ์•Š์•˜๋‹ค. "๊ทธ๋Ÿฐ๋ฐ ๊ฒฝ๋กœ๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ๋˜๋Œ์•„๊ฐ€๋ ค๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š”๋ฐ. ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ €์žฅํ•˜๋ฉด ํ”Œ๋ ˆ์ด์–ด ์œ„์น˜์˜ ๋ฏธ๊ถ ๋ฐ์ดํ„ฐ๋ฅผ ์ ‘๊ทผํ•  ๋•Œ ์‹ค๋ญ‰์น˜๋ฅผ ๊ณ„์† ๋”ฐ๋ผ๊ฐ€์•ผ ํ•˜๊ณ . ๋‘˜ ๋‹ค ์‹ค๋ญ‰์น˜์˜ ๊ธธ์ด์— ๋น„๋ก€ํ•˜๋Š” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š”๋ฐ, ๋ฏธ๊ถ์ด ํฌ๋ฉด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆด ๊ฑฐ์•ผ. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ• ์—†๋‚˜?" ์•„๋ผ๋“œ๋„ค์˜ ์ง€ํผ(Zipper) ํ…Œ์„ธ์šฐ์Šค๋Š” ๋…ธ๋ จํ•œ ์ „์‚ฌ์ง€๋งŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ˆ ์€ ๋งŽ์ด ํ›ˆ๋ จํ•˜์ง€ ์•Š์•„์„œ ๋งŒ์กฑ์Šค๋Ÿฌ์šด ํ•ด๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์—†์—ˆ๋‹ค. ๋ณ„ ์†Œ๋“ ์—†์ด ๊ณ ์‹ฌํ•˜๋˜ ํ…Œ์„ธ์šฐ์Šค๋Š” ์˜›์‚ฌ๋ž‘ ์•„๋ผ๋“œ๋„ค์— ๊ฒŒ ์กฐ์–ธ์„ ๊ตฌํ•˜๊ธฐ๋กœ ๊ฒฐ์‹ฌํ–ˆ๋‹ค. ์–ด์จŒ๋“  ์‹ค๋ญ‰์น˜๋ฅผ ๊ณ ์•ˆํ•œ ๊ฒƒ๋„ ์•„๋ผ๋“œ ๋„ค์ง€ ์•Š์€๊ฐ€. "์•„๋ผ๋“œ๋„ค ์ƒ๋‹ด์†Œ์ž…๋‹ˆ๋‹ค. ๋ฌด์—‡์„ ๋„์™€๋“œ๋ฆด๊นŒ์š”?" ํ…Œ์„ธ์šฐ์Šค๋Š” ๊ทธ ๋ชฉ์†Œ๋ฆฌ๋ฅผ ์ฆ‰์‹œ ์•Œ์•„๋ดค๋‹ค. "์•„๋ผ๋“œ๋„ค. ์•ˆ๋…•. ๋‚˜์•ผ. ํ…Œ์„ธ์šฐ์Šค." ๋ถˆํŽธํ•œ ์ •์ ์ด ๋Œ€ํ™”๋ฅผ ๋Š์—ˆ๋‹ค. ํ…Œ์„ธ์šฐ์Šค๋Š” ์•„๋ผ๋“œ๋„ค๋ฅผ ๋‚™์†Œ์Šค ์„ฌ์— ๋ฒ„๋ ธ๋˜ ์ž์‹ ์„ ๋˜‘๋˜‘ํžˆ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ์—ˆ๊ณ  ์•„๋ผ๋“œ๋„ค๊ฐ€ ์ž์‹ ์˜ ์ „ํ™”๋ฅผ ๋ฐ˜๊ธฐ์ง€ ์•Š์„ ๊ฒƒ๋„ ์•Œ๊ณ  ์žˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์–ด์ฉŒ๊ฒ ๋Š”๊ฐ€. Ancient Geeks ์ฃผ์‹ํšŒ์‚ฌ๊ฐ€ ํ•˜๋ฐ์Šค์—๊ฒŒ ๊ฐ€๋Š” ๊ธธ๋ชฉ์— ์„œ ์žˆ๊ณ  ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์—†๋Š” ๊ฒƒ์„. "์–ด... ์ž๊ธฐ... ์ž˜ ์ง€๋‚ด?" ์•„๋ผ๋“œ๋„ค๊ฐ€ ์ฐจ๊ฐ‘๊ฒŒ ๋Œ€๊พธํ–ˆ๋‹ค. "ํ…Œ์„ธ์šฐ์Šค ์”จ. ์ž๊ธฐ๋ผ ๋ถ€๋ฅด๋˜ ๋•Œ๋Š” ํ•œ์ฐธ ์˜ค๋ž˜์ „์ด์ง€. ๋ญ˜ ์›ํ•ด?" "์•„, ๊ทธ๊ฒŒ, ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฌธ์ œ๊ฐ€ ์ข€ ์ƒ๊ฒจ์„œ. ๋‚˜ ํ…Œ์„ธ์šฐ์Šค์™€ ๋ฏธ๋…ธํƒ€์šฐ๋กœ์Šคโ„ข๋ผ๋Š” ์ปดํ“จํ„ฐ ๊ฒŒ์ž„์„ ๋งŒ๋“ค๊ณ  ์žˆ๋Š”๋ฐ." "๋‹น์‹ ์˜ '์˜์›…์„ฑ'์„ ๋น›๋‚ด๊ธฐ ์œ„ํ•œ ๋˜ ๋‹ค๋ฅธ ์žฅ์‹ํ’ˆ์ธ๊ฐ€? ๊ทธ๊ฑฐ ๋•Œ๋ฌธ์— ์ฐจ๊ณ  ๋„˜์น˜๋Š” ์‚ฌ๋žŒ๋“ค ์ค‘์— ๋‚˜๋ฅผ ๋ณด๊ณ  ๋„์™€๋‹ฌ๋ผ๊ณ ? ์•„๋ผ๋“œ๋„ค๊ฐ€ ์•ผ์œ ํ–ˆ๋‹ค. "์•„๋ผ๋“œ๋„ค. ์ œ๋ฐœ. ์ด๋ ‡๊ฒŒ ๋นŒ๊ฒŒ. ์šฐ๋ฆฌ ์ฃผ์‹ํšŒ์‚ฌ๊ฐ€ ํŒŒ์‚ฐํ•  ์ง€๊ฒฝ์ด๋ž€ ๋ง์ด์•ผ. ์ด ๊ฒŒ์ž„์ด ๋งˆ์ง€๋ง‰ ํฌ๋ง์ด์•ผ!" ์ž ๊น์˜ ์ •์ ์ด ํ๋ฅด๊ณ  ์•„๋ผ๋“œ๋„ค๋Š” ๊ฒฐ์ •์„ ๋‚ด๋ ธ๋‹ค. "์ข‹์•„, ๋„์™€์ฃผ์ง€. ๋Œ€์‹  ์ฃผ์‹์˜ ์ƒ๋‹น ๋ถ€๋ถ„์„ ๋–ผ์ค˜์•ผ๊ฒ ์–ด. 30ํผ์„ผํŠธ." ํ…Œ์„ธ์šฐ์Šค์˜ ์•ˆ์ƒ‰์ด ์ฐฝ๋ฐฑํ•ด์กŒ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ฌ๋ฆฌ ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ๋Š”๊ฐ€? ์ด๋ฏธ ์ƒํ™ฉ์€ ์ถฉ๋ถ„ํžˆ ์ ˆ๋ง์ ์ด์—ˆ๊ณ  ํ…Œ์„ธ์šฐ์Šค๋Š” ์•„๋ผ๋“œ๋„ค์˜ ๊ณต๋™ ์ง€๋ถ„์„ 10ํผ์„ผํŠธ๋กœ ํ˜‘์ƒํ–ˆ๋‹ค. ํ…Œ์„ธ์šฐ์Šค๊ฐ€ ๊ณ ์‹ฌํ•˜๋˜ ๋ฏธ๊ถ ํ‘œํ˜„๋ฒ•์„ ์•„๋ผ๋“œ๋„ค์— ๊ฒŒ ๋งํ•˜์ž ์ฆ‰์‹œ ์กฐ์–ธ์„ ๋ฐ›์•˜๋‹ค. "์ง€ํผ ๋ฉด ๋˜๊ฒ ๊ตฐ." "๋ญ? ์ด๊ฑฐ๋ž‘ ๋‚ด ๋ฐ”์ง€ ์ง€ํผ๋ž‘ ๋ฌด์Šจ ์ƒ๊ด€์ด..." "์•„๋‹ˆ. ์ง€ํผ๋Š” Gรฉrard Huet2๊ฐ€ ์ œ์•ˆํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์•ผ." "์•„?" "์ข€ ๋” ์ž์„ธํžˆ๋Š”, ๋ฆฌ์ŠคํŠธ๋‚˜ ์ด์ง„ ํŠธ๋ฆฌ ๊ฐ™์€ ํŠธ๋ฆฌ ๋ฅ˜์˜ ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ ๋‚ด๋ถ€์˜ ํ•œ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ๋‹จ๋ฒˆ์— ์งš์–ด ์ƒ์ˆ˜ ์‹œ๊ฐ„ ๋‚ด์— ๊ฐฑ์‹  ๋ฐ ๊ฒ€์ƒ‰์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์ˆ˜๋‹จ์ด์ง€. 3 ๋„ค ๊ฒฝ์šฐ์—๋Š” ํ”Œ๋ ˆ์ด์–ด์˜ ์œ„์น˜์— ์‹ ๊ฒฝ์„ ์“ฐ๋Š” ๊ฑฐ๊ณ ." "๊ณ ์† ๊ฐฑ์‹ ์ด์•ผ ํ•˜๊ณ  ์‹ถ์ง€. ๊ทธ๋Ÿฐ๋ฐ ์ฝ”๋”ฉ์„ ์–ด๋–ป๊ฒŒ ํ•˜์ง€?" "๊ธฐ๋‹ค๋ ค๋ด. ์ฝ”๋”ฉ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜๋Š” ์—†์–ด. ์ƒ๊ฐํ•ด์•ผ ํ’€ ์ˆ˜ ์žˆ์ง€. ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์ƒ์ˆ˜ ์‹œ๊ฐ„ ๋‚ด์— ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์ตœ์ƒ์œ„ ๋…ธ๋“œ๋ฟ์ด์•ผ. 45 ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์ตœ์ƒ์œ„์— ์ง‘์ค‘ํ•ด์•ผ ํ•ด. ์ง€๊ธˆ์œผ๋กœ์„  ๋„ค ๋ฏธ๊ถ์˜ ์ตœ์ƒ์œ„ ๋…ธ๋“œ๊ฐ€ ํ•ญ์ƒ ์ž…๊ตฌ์ธ๋ฐ, ๋ฏธ๊ถ์„ ํ•˜์œ„ ๋ฏธ๊ถ์œผ๋กœ ์น˜ํ™˜ํ•˜๋Š” ๋„ค ๋ฐœ์ƒ์€ ํ”Œ๋ ˆ์ด์–ด์˜ ์œ„์น˜๋ฅผ ์ตœ์ƒ์œ„ ๋…ธ๋“œ๋กœ ๋†“๊ฒŒ ๋˜์ง€." "๋ฌธ์ œ๋Š” ์–ด๋–ป๊ฒŒ ๋Œ์•„๊ฐ€๋Š๋ƒ์•ผ. ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์„ ํƒํ•˜์ง€ ์•Š์€ ํ•˜์œ„ ๋ฏธ๊ถ์€ ์‹น ์†Œ์‹ค๋˜๋‹ˆ๊นŒ." "๋‚ด ์‹ค๋ญ‰์น˜๋ฅผ ํ™œ์šฉํ•ด." ์•„๋ผ๋“œ๋„ค๋Š” ์–ด๋ฆฌ๋‘ฅ์ ˆํ•œ ํ…Œ์„ธ์šฐ์Šค๊ฐ€ ์ด๋ฏธ ์‹œ๋„ํ•ด ๋ดค๋‹ค๊ณ  ๋ถˆํ‰ํ•  ํ‹ˆ์„ ์ฃผ์ง€ ์•Š๊ณ  ๋น ๋ฅด๊ฒŒ ์ด์–ด๊ฐ”๋‹ค. "์†Œ์‹ค๋œ ํ•˜์œ„ ๋ฏธ๊ถ๋“ค์„ ์‹ค๋ญ‰์น˜์— ๋ถ™์—ฌ์„œ ์‹ค์ œ๋กœ๋Š” ์†Œ์‹ค๋˜์ง€ ์•Š๊ฒŒ ํ•˜๋Š” ๊ฒŒ ํ•ต์‹ฌ์ด์•ผ. ์‹ค๋ญ‰์น˜์™€ ํ˜„ ํ•˜์œ„ ๋ฏธ๊ถ์ด ์„œ๋กœ๋ฅผ ๋ณด์ถฉํ•ด ์ „์ฒด ๋ฏธ๊ถ์„ ์ด๋ฃจ๊ฒŒ ํ•˜๋Š” ๊ฑฐ์ง€. 'ํ˜„' ํ•˜์œ„ ๋ฏธ๊ถ์€ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์„œ ์žˆ๋Š” ๊ณณ์„ ๋œปํ•ด. ์ง€ํผ๋Š” ๋‹จ์ˆœํžˆ ์‹ค๋ญ‰์น˜์™€ ํ˜„ ํ•˜์œ„ ๋ฏธ๊ถ์œผ๋กœ ๊ตฌ์„ฑ๋˜์ง€." type Zipper a = (Thread a, Node a) ์ง€ํผ๋Š” ์•„๋ผ๋“œ๋„ค์˜ ์‹ค๋ญ‰์น˜์™€ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์„œ ์žˆ๋Š” ํ•˜์œ„ ๋ฏธ๊ถ์˜ ์ตœ์ƒ์œ„๋กœ ๊ตฌ์„ฑ๋œ ์ง์ด๋‹ค. ์ฃผ ์‹ค๋ญ‰์น˜๋Š” ์ ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋˜์—ˆ๊ณ  ํ•˜์œ„ ๋ฏธ๊ถ๋“ค์ด ๋ถ™์–ด์žˆ๋‹ค. ์ด ์ง์„ ๊ฐ€์ง€๊ณ  ๋ฏธ๊ถ ์ „์ฒด๋ฅผ ์žฌ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ…Œ์„ธ์šฐ์Šค๋Š” ์•„๋ฌด ๋ง์ด ์—†์—ˆ๋‹ค. "์‹ค๋ญ‰์น˜๋ฅผ ํ˜„ ํ•˜์œ„ ๋ฏธ๊ถ์ด ๋†“์ธ ์ปจํ…์ŠคํŠธ๋กœ ๋ณผ ์ˆ˜๋„ ์žˆ์ง€. ์ด์ œ Thread a๋ฅผ ์ •์˜ํ•  ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด์ž๊ณ . Thread๋Š” ์ด์ œ ํ•˜์œ„ ๋ฏธ๊ถ๋“ค์„ ๋ณด๊ด€ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋ถ„์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ a๋ฅผ ์ทจํ•ด์•ผ ํ•ด. ์‹ค๋ญ‰์น˜๋Š” ์—ฌ์ „ํžˆ ๊ฐˆ๋ฆผ๊ธธ์˜ ๋ฆฌ์ŠคํŠธ์ง€๋งŒ ๊ฐˆ๋ฆผ๊ธธ ์ž์ฒด๋Š” ์ข€ ๋‹ฌ๋ผ์ง€์ง€." data Branch a = KeepStraightOn a | TurnLeft a (Node a) | TurnRight a (Node a) type Thread a = [Branch a] "๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฑด TurnLeft์™€ TurnRight์— ํ•˜์œ„ ๋ฏธ๊ถ์ด ๋ถ™๋Š”๋‹ค๋Š” ๊ฑฐ์•ผ. ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด ์‹ค๋ญ‰์น˜์— TurnRight์„ ๋”ํ•˜๊ณ  ์ด TurnRight์—๋Š” ์„ ํƒํ•˜์ง€ ์•Š์€ ์™ผ์ชฝ ๊ฐˆ๋ฆผ๊ธธ์„ ๋ถ™์—ฌ์„œ ์†Œ์‹ค๋˜์ง€ ์•Š๊ฒŒ ํ•˜๋Š” ๊ฑฐ์ง€." ํ…Œ์„ธ์šฐ์Šค๊ฐ€ ๋ผ์–ด๋“ค์—ˆ๋‹ค. "์ž ๊น๋งŒ. ๊ทธ๊ฑธ ์–ด๋–ป๊ฒŒ turnRight๋ผ๋Š” ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์ง€? ๊ทธ๋ฆฌ๊ณ  TurnRight์˜ a ํƒ€์ž… ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š”? ์•„, ์ž ๊น. ์†Œ์‹ค๋  ๊ฐˆ๋ฆผ๊ธธ๋งŒ ๋ถ™์ด๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ Fork๋ผ๋Š” ์ถ”๊ฐ€ ์ž๋ฃŒ๋„ ๋ถ™์ด๋Š” ๊ฑฐ๊ตฌ๋‚˜. ์•„๋‹ˆ๋ฉด ์ด๊ฒƒ๋„ ์†Œ์‹ค๋˜๋‹ˆ๊นŒ. ๊ทธ๋Ÿผ ์ƒˆ ๊ฐˆ๋ฆผ๊ธธ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์–ด." branchRight (Fork x l r) = TurnRight x l "์ด์ œ ์–ด๋–ป๊ฒŒ๋“  ์ด๊ฑธ ๊ฐ€์ง€๊ณ  ๊ธฐ์กด ์‹ค๋ญ‰์น˜๋ฅผ ํ™•์žฅํ•ด์•ผ ํ•˜๋Š”๋ฐ..." "์‹ค๋ญ‰์น˜์— ๊ด€ํ•œ ๋‘ ๋ฒˆ์งธ ์‚ฌํ•ญ์€ ๊ฑฐ๊พธ๋กœ ์ €์žฅ๋œ๋‹ค๋Š” ๊ฑฐ์•ผ. ์‹ค๋ญ‰์น˜๋ฅผ ํ™•์žฅํ•˜๋ ค๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ์•ž์— ์ƒˆ ๊ฐˆ๋ฆผ๊ธธ์„ ๋†“๋Š” ๊ฑฐ์•ผ. ๋˜๋Œ์•„๊ฐ€๋ ค๋ฉด ์ตœ์ƒ์œ„ ์›์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ณ ." "์•„ํ•˜. ๊ทธ๋Ÿฌ๋ฉด ๋‚ด๊ฐ€ ์ „์— ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ ํ™•์žฅ๊ณผ ํ›„ํ‡ด์— ์ƒ์ˆ˜ ์‹œ๊ฐ„๋งŒ ๊ฑธ๋ฆฌ๋Š”๊ตฐ. ๊ทธ๋Ÿผ turnRight์˜ ์ตœ์ข… ๋ฒ„์ „์€..." turnRight :: Zipper a -> Maybe (Zipper a) turnRight (t, Fork x l r) = Just (TurnRight x l : t, r) turnRight _ = Nothing ์ž…๊ตฌ์—์„œ ์˜ค๋ฅธ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ ์„ ํƒ. ๋‹น์—ฐํžˆ ์ฒ˜์Œ์—๋Š” ์‹ค๋ญ‰์น˜๊ฐ€ ๋น„์–ด์žˆ๋‹ค. ์‹ค๋ญ‰์น˜๋Š” ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ„๋‹ค๋Š” ๊ฒƒ์„ ๋ช…์‹ฌํ•˜๋ผ. ์ฆ‰ ์ตœ์ƒ์œ„ ์กฐ๊ฐ์ด ๊ฐ€์žฅ ์ตœ๊ทผ์˜ ๊ฒƒ์ด๋‹ค. "๋ณ„๋กœ ์–ด๋ ต์ง€ ์•Š์€๋ฐ. ํ†ต๋กœ๋ฅผ ์ง์ง„ํ•˜๋Š” keepStraightOn์€ ๊ฐˆ๋ฆผ๊ธธ ๊ณ ๋ฅด๊ธฐ๋ณด๋‹ค ์‰ฝ์ง€. ์—ฌ๋ถ„์˜ ๋ฐ์ดํ„ฐ๋งŒ ์œ ์ง€ํ•˜๋ฉด ๋˜๋‹ˆ๊นŒ." keepStraightOn :: Zipper a -> Maybe (Zipper a) keepStraightOn (t, Passage x n) = Just (KeepStraightOn x : t, n) keepStraightOn _ = Nothing ์ด๋ฒˆ์—๋Š” ํ†ต๋กœ๋ฅผ ์ง์ง„ํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ turnLeft ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ํ…Œ์„ธ์šฐ์Šค๋Š” ๋งŒ์กฑํ•˜๊ณ  ๊ณ„์†ํ–ˆ๋‹ค. "๋ฌผ๋ก  ํ›„ํ‡ด๊ฐ€ ํฅ๋ฏธ๋กœ์šด ๋ถ€๋ถ„์ด์ง€. ์–ด๋”” ๋ณด์ž..." back :: Zipper a -> Maybe (Zipper a) back ([] , _) = Nothing back (KeepStraightOn x : t, n) = Just (t, Passage x n) back (TurnLeft x r : t, l) = Just (t, Fork x l r) back (TurnRight x l : t, r) = Just (t, Fork x l r) "์‹ค๋ญ‰์น˜๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด ๋ฏธ๊ถ์˜ ์ž…๊ตฌ์— ์žˆ๋Š” ๊ฑฐ๊ณ  ํ›„ํ‡ดํ•  ์ˆ˜ ์—†์ง€. ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—” ์‹ค๋ญ‰์น˜๋ฅผ ๊ฐ์•„์˜ฌ๋ฆฌ๊ณ . ๊ทธ๋ฆฌ๊ณ  ์‹ค๋ญ‰์น˜์˜ ๋ถ€์ฐฉ๋ฌผ ๋•์— ์šฐ๋ฆฌ๊ฐ€ ์ง€๋‚˜์™”๋˜ ํ•˜์œ„ ๋ฏธ๊ถ์„ ์žฌ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ์–ด." ์•„๋ผ๋“œ๋„ค๊ฐ€ ์ฒจ์–ธํ–ˆ๋‹ค. "์ขŒ๋ณ€์— ๋ฐ”์šด๋”ฉ๋œ ๋ณ€์ˆ˜ x, l, r์ด ์šฐ๋ณ€์—๋„ ๋”ฑ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฑธ ๋ด. ๋”ฐ๋ผ์„œ ์ง€ํผ๋ฅผ ์˜ฌ๋ฆฌ๊ฑฐ๋‚˜ ๋‚ด๋ฆด ๋•Œ ์‹ค๋ญ‰์น˜์™€ ํ˜„ ํ•˜์œ„ ๋ฏธ๊ถ ์‚ฌ์ด์—์„œ๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์žฌ๋ฐฐ์น˜ํ•˜๋Š” ๊ฑฐ์ง€." ์—ฐ์Šต๋ฌธ์ œ ์ด์ œ ์ง€ํผ๋ฅผ ๋Œ์•„๋‹ค๋‹ ์ˆ˜ ์žˆ์œผ๋‹ˆ ํ”Œ๋ ˆ์ด์–ด ์œ„์น˜์˜ ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ์— ์ž‘์šฉํ•˜๋Š” get, put, update ํ•จ์ˆ˜๋ฅผ ์ฝ”๋”ฉํ•ด ๋ณด์ž. ์ง€ํผ๋Š” Node a๋ผ๋Š” ํŠน์ • ์‚ฌ๋ก€์— ๊ตญํ•œ๋˜์ง€ ์•Š๋Š”๋‹ค. ํŠธ๋ฆฌ ๋ฅ˜์˜ ๋ชจ๋“  ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์ง€ํผ๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์ง„ ํŠธ๋ฆฌ์˜ ์ง€ํผ๋ฅผ ๊ตฌ์ถ•ํ•ด ๋ณด๋ผ. data Tree a = Leaf a | Bin (Tree a) (Tree a) ์‹ค๋ญ‰์น˜๊ฐ€ ์ทจํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅํ•œ ๋ถ„๊ธฐ ์ฆ‰ Branch a์—์„œ ์‹œ์ž‘ํ•  ๊ฒƒ. ํŠธ๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ ์‹ค๋ญ‰์น˜์— ๋ถ™์ผ ๊ฒƒ์€ ๋ฌด์—‡์ธ๊ฐ€? ๋‹จ์ˆœํ•œ ๋ฆฌ์ŠคํŠธ๋„ ์ง€ํผ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. data List a = Empty | Cons a (List a) ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ์„๊นŒ? ํ…Œ์„ธ์šฐ์Šค์˜ ๋ฏธ๊ถ์— ๊ธฐ๋ฐ˜ํ•ด ์™„๋ฒฝํ•œ ๊ฒŒ์ž„์„ ์ž‘์„ฑํ•˜๋ผ. ์•„ํ•˜! ํ…Œ์„ธ์šฐ์Šค๊ฐ€ ๊ฐˆ๋งํ•˜๋˜, ๊ทธ๋ฆฌ๊ณ  Ancient Geeks ์ฃผ์‹ํšŒ์‚ฌ์— ํ•„์š”ํ•œ ๋ฐ”๋กœ ๊ทธ ํ•ด๋‹ต์ด๋‹ค! ์•„๋ผ๋“œ๋„ค ์ƒ๋‹ด์†Œ์— ์กฐ๊ธˆ ํŒ”๋ฆฌ๊ธด ํ–ˆ์ง€๋งŒ. ๊ทธ๋Ÿฐ๋ฐ ์งˆ๋ฌธ์ด ํ•˜๋‚˜ ๋‚จ์•„์žˆ๋‹ค. "์™œ ์ง€ํผ๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๊ฑฐ์ง€?" "๋ญ, '์•„๋ผ๋“œ๋„ค์˜ ์ง„์ฃผ ๋ชฉ๊ฑธ์ด'๋ผ๊ณ  ๋ถ€๋ฅผ ์ˆ˜๋„ ์žˆ์—ˆ๋Š”๋ฐ ์‹ค๋ญ‰์น˜๋Š” ์ง€ํผ์˜ ์—ด๋ฆฐ ๋ถ€๋ถ„, ํ•˜์œ„ ๋ฏธ๊ถ์€ ๋‹ซํžŒ ๋ถ€๋ถ„๊ณผ ๋‹ฎ์•˜๊ธฐ ๋•Œ๋ฌธ์ด์•ผ. ์ž๋ฃŒ๊ตฌ์กฐ ๋‚ด๋ฅผ ์ด๋ฆฌ์ €๋ฆฌ ๋Œ์•„๋‹ค๋‹ˆ๋Š” ๊ฑด ์ง€ํผ๋ฅผ ์˜ฌ๋ ธ๋‹ค ๋‚ด๋ ธ๋‹ค ํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๊ณ ." "'์•„๋ผ๋“œ๋„ค์˜ ์ง„์ฃผ ๋ชฉ๊ฑธ์ด'๋ผ. ํ…Œ์„ธ์šฐ์Šค๋Š” ๋ถ„๋ช…ํžˆ ์—…์‹ ์—ฌ๊ธฐ๋Š” ํˆฌ์˜€๋‹ค. "๋„ค ์‹ค๋ญ‰์น˜๊ฐ€ ํฌ๋ ˆํƒ€์—์„œ ๋„์›€์ด ๋˜์—ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ." "์‹ค๋ญ‰์น˜๊ฐ€ ๋„ค ๋ฐœ์ƒ์ธ ์–‘ ๋ง์ด์ง€." ์•„๋ผ๋“œ๋„ค๊ฐ€ ๋Œ€๊พธํ–ˆ๋‹ค. "์•„. ๋‚œ ์‹ค๋ญ‰์น˜ ํ•„์š” ์—†์–ด." ์‚ฌ์‹ค ํ…Œ์„ธ์šฐ์Šค๋Š” ๊ฒŒ์ž„์„ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š”๋ฐ ์‹ค๋ญ‰์น˜๊ฐ€ ํ•„์š”ํ–ˆ์ง€๋งŒ ๋ถ€์ •ํ–ˆ๋‹ค. ๋†€๋ž๊ฒŒ๋„ ์•„๋ผ๋“œ๋„ค๊ฐ€ ์ˆ˜๊ธํ–ˆ๋‹ค. "์‚ฌ์‹ค ์‹ค๋ญ‰์น˜๋Š” ํ•„์š”ํ•˜์ง€ ์•Š์•„. ๋˜ ๋‹ค๋ฅธ ๊ด€์ ์€ ํŠธ๋ฆฌ๋ฅผ ์†๊ฐ€๋ฝ์œผ๋กœ ๋ง ๊ทธ๋Œ€๋กœ ์ง‘์–ด์„œ ๊ณต์ค‘์œผ๋กœ ๋“ค์–ด ์˜ฌ๋ฆฌ๋Š” ๊ฑฐ์•ผ. ์ตœ์ƒ์œ„์— ์ดˆ์ ์„ ๋งž์ถ”๊ณ  ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ๋‹ค๋ฅธ ๋ถ„๊ธฐ๋Š” ์•„๋ž˜๋กœ ๋Š˜์–ด์ง€์ง€. ๊ทธ ๊ฒฐ๊ณผ ๋‚˜์˜จ ํŠธ๋ฆฌ์— ์•Œ๋งž์€ ๋Œ€์ˆ˜์  ์ž๋ฃŒํ˜•๋งŒ ํ• ๋‹นํ•ด. ์ง€ํผ์ฒ˜๋Ÿผ." ๋ชฉํ‘œ๋ฅผ ์†๊ฐ€๋ฝ์œผ๋กœ ์ง‘์–ด ๊ณต์ค‘์œผ๋กœ ๋“ค์–ด ์˜ฌ๋ฆฌ๋ฉด ๋งค๋‹ฌ๋ฆฐ ๋ถ„๊ธฐ๋“ค์ด ์—ฌ๋Ÿฌ๋ถ„์˜ ์†๊ฐ€๋ฝ์ด ์ตœ์ƒ์œ„์ธ ์ƒˆ๋กœ์šด ํŠธ๋ฆฌ๋ฅผ ํ˜•์„ฑํ•˜์—ฌ, ์•Œ๋งž์€ ๋Œ€์ˆ˜์  ์ž๋ฃŒํ˜•์— ์˜ํ•ด ๊ตฌ์กฐํ™”๋  ๊ฒƒ์ด๋‹ค. "์•„." ํ…Œ์„ธ์šฐ์Šค์—๊ฒ ์•„๋ผ๋“œ๋„ค์˜ ์‹ค๋ญ‰์น˜๊ฐ€ ํ•„์š” ์—†์—ˆ์ง€๋งŒ ์•„๋ผ๋“œ๋„ค๊ฐ€ ๊ทธ๊ฑธ ๋งํ•ด์ค˜์•ผ ํ–ˆ๋‹ค๋Š” ๊ฑด๊ฐ€? ๋„ˆ๋ฌดํ•˜๋Š”๊ตฐ. "๊ณ ๋งˆ์›Œ. ์•„๋ผ๋“œ๋„ค. ์•ˆ๋…•." ์•„๋ผ๋“œ๋„ค๋Š” ํžˆ์ฃฝ๊ฑฐ๋ ธ๋‹ค. ์–ด์จŒ๋“  ์ „ํ™”๊ธฐ๋กœ ๋ณด์ด๋Š” ๊ฑด ์•„๋‹ˆ๋‹ˆ๊นŒ. ์—ฐ์Šต๋ฌธ์ œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด์„œ ์ค‘๊ฐ„์˜ ํ•œ ์›์†Œ๋ฅผ ์†๊ฐ€๋ฝ์œผ๋กœ ์ง‘์–ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ณต์ค‘์œผ๋กœ ๋“ค์–ด ์˜ฌ๋ ค๋ผ. ๊ฒฐ๊ณผ ํŠธ๋ฆฌ์˜ ํƒ€์ž…์€ ์–ด๋–ป๊ฒŒ ๋˜๋Š”๊ฐ€? ๋ฐ˜๋…„ ํ›„, ํ…Œ์„ธ์šฐ์Šค๊ฐ€ ํ•œ ์ง„์—ด์žฅ ์•ž์— ๋ฉˆ์ถฐ ์„ ๋‹ค. ๋‹จ์ถ”๋ฅผ ์—ฌ๋ฉฐ, ์™ธํˆฌ ์†์œผ๋กœ ๋“ค์–ด๊ฐ€๋ ค๋Š” ์ฐจ๊ฐ€์šด ๋น—์ค„๊ธฐ๋ฅผ ๋ง‰์œผ๋ฉฐ. ์œ ๋ฆฌ์ฐฝ์— ๊ธ€์ž๋“ค์ด ๊นœ๋นก์ธ๋‹ค. "์ŠคํŒŒ์ด๋”๋งจ: lost in the web" -์‹ค๋ญ‰์น˜์˜ ๋ฏธ๊ถ์—์„œ ์—ฌ๋Ÿฌ๋ถ„์˜ ๊ธธ์„ ์ฐพ์œผ์„ธ์š”- ์œ„๋Œ€ํ•œ ์ปดํ“จํ„ฐ ๊ฒŒ์ž„. Ancient Geeks ์ฃผ์‹ํšŒ์‚ฌ ์ œ์ž‘ ํ…Œ์„ธ์šฐ์Šค๋Š” ์•„๋ผ๋“œ๋„ค์— ๊ฒŒ ์ „ํ™”๋ฅผ ๊ฑธ์–ด ํšŒ์‚ฌ์˜ ์ผ๋ถ€๋ฅผ ํŒ”์•˜๋˜ ๊ทธ๋‚ ์„ ์ €์ฃผํ–ˆ๋‹ค. ์•„๋ผ๋“œ๋„ค์˜ ๋‚จํŽธ ๋””์˜ค๋‹ˆ์†Œ์Šค๊ฐ€ ์šด์˜ํ•˜๋Š” WineOS ์ฃผ์‹ํšŒ์‚ฌ. ๊ทธ ํšŒ์‚ฌ์— ๋‹ฌ๊ฐ‘์ง€ ์•Š๊ฒŒ ๊ณต๊ฐœ๋งค์ˆ˜๋œ ๊ฒƒ์ด ์•„๋ผ๋“œ๋„ค์˜ ๊ณ„ํš์ด์—ˆ์„๊นŒ? ํ…Œ์„ธ์šฐ์Šค๋Š” ๋น—๋ฐฉ์šธ๋“ค์ด ์œ ๋ฆฌ์ฐฝ์„ ๋ฏธ๋„๋Ÿฌ์ง€๋Š” ๊ฒƒ์„ ๋ฌผ๋„๋Ÿฌ๋ฏธ ์ณ๋‹ค๋ณด์•˜๋‹ค. ์ƒ์‚ฐ ๋ผ์ธ์ด ๋ณ€๊ฒฝ๋˜๊ณ  ๋‚˜๋ฉด ์•„๋ฌด๋„ Theseus and the Minotaur โ„ข ์ƒํ’ˆ์„ ์ƒ์‚ฐํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ํ…Œ์„ธ์šฐ์Šค๋Š” ํ•œ์ˆจ์„ ๋‚ด์‰ฌ์—ˆ๋‹ค. ์˜์›…์œผ๋กœ์„œ ๊ทธ์˜ ์‹œ๋Œ€๋Š” ๋๋‚ฌ๋‹ค. ์ด์ œ๋Š” ์Šˆํผ ์˜์›…์˜ ์‹œ๋Œ€๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๋ฏธ๋ถ„ ์•ž ์ ˆ์—์„œ๋Š” ์ง€ํผ๋ฅผ ์†Œ๊ฐœํ–ˆ๋Š”๋ฐ, ์ง€ํผ๋Š” ํŠธ๋ฆฌ ๋ฅ˜์˜ ์ž๋ฃŒ๊ตฌ์กฐ์ธ Node a๋ฅผ ์†๊ฐ€๋ฝ(finger)์„ ํ†ตํ•ด ์กฐ์ž‘ํ•˜์—ฌ ์—ฌ๋Ÿฌ ํ•˜์œ„ ํŠธ๋ฆฌ์— ์ดˆ์ ์„ ๋งž์ถœ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์ˆ˜๋‹จ์ด๋‹ค. ์šฐ๋ฆฌ๊ฐ€ Node a๋ผ๋Š” ํŠน์ • ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์ง€ํผ๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธด ํ–ˆ์ง€๋งŒ, ๋‹ค๋ฅธ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋“ค๋กœ ์‰ฝ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์‚ผ์ง„ ํŠธ๋ฆฌ๋กœ ์‹œ์ž‘ํ•˜์ž. data Tree a = Leaf a | Node (Tree a) (Tree a) (Tree a) ๊ทธ๋ฆฌ๊ณ  ์ด์— ๋งž๋Š” Thread a์™€ Zipper a๋ฅผ ๋„์ถœํ•˜๋ผ. ๊ธฐ๊ณ„์  ๋ฏธ๋ถ„ ๊ทธ๋Ÿฐ๋ฐ ์–ด๋–ค ์ •ํ˜•์ ์ธ ์ž๋ฃŒํ˜•์— ๋Œ€ํ•ด์„œ๋„ ์ง€ํผ๋ฅผ ๋„์ถœํ•˜๋Š” ์™„์ „ํžˆ ๊ธฐ๊ณ„์ ์ธ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ๋†€๋ž๊ฒŒ๋„, ๋„์ถœํ•œ๋‹ค(derive)๋Š” ๋ง ๊ทธ๋Œ€๋กœ, ์ง€ํผ๋Š” ์ž๋ฃŒํ˜•์„ ๋ฏธ๋ถ„ํ•ด์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, Conor McBride6๊ฐ€ ์ฒ˜์Œ ๋ฐœ๊ฒฌํ•˜์˜€๋‹ค. ์ด์–ด์ง€๋Š” ์ ˆ์—์„œ๋Š” ์ด ๊ฒฝ์ด๋กœ์šด ์ˆ˜ํ•™์  ๋ณด๋ฐฐ๋ฅผ ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. derive๋Š” 'ํŒŒ์ƒํ•˜๋‹ค', '๋„์ถœํ•˜๋‹ค'๋ผ๋Š” ๋œป๋„ ์žˆ์ง€๋งŒ ์ˆ˜ํ•™์—์„œ๋Š” ์ฃผ๋กœ '๋ฏธ๋ถ„ํ•˜๋‹ค'๋ผ๋Š” ๋œป์œผ๋กœ ์“ฐ์ธ๋‹ค. ๊ตฌ์กฐ์  ์ƒ์„ฑ์„ ์œ„ํ•ด์„œ๋Š” ํƒ€์ž…์„ ๊ณ„์‚ฐํ•ด์•ผ ํ•œ๋‹ค. ํƒ€์ž…์˜ ๊ตฌ์กฐ์  ๊ณ„์‚ฐ์€ [Generic Programming] ์žฅ์—์„œ ๋ณ„๋„๋กœ ์„ค๋ช…ํ•˜๋ฉฐ ์—ฌ๊ธฐ์˜ ๋‚ด์šฉ์— ํฌ๊ฒŒ ์˜์กดํ•  ๊ฒƒ์ด๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์ง€ํผ๋“ค์— ์–ด๋–ค ๊ณตํ†ต์ ์ด ์žˆ๊ณ  ๋ฏธ๋ถ„ํ•  ๋•Œ ์–ด๋–ค ์˜๋ฏธ๋ฅผ ์ง€๋‹ˆ๋Š”์ง€ ๋ณด์ž. ์ด์ง„ ํŠธ๋ฆฌ์˜ ํƒ€์ž…์€ ๋‹ค์Œ ์žฌ๊ท€์‹์˜ ๊ณ ์ •์ ์ด๋‹ค. Tree2 1 Tree2 Tree2 ํŠธ๋ฆฌ๋ฅผ ๊ฑธ์–ด ๋‚ด๋ ค๊ฐˆ ๋•Œ ์šฐ๋ฆฌ๋Š” ๋งค๋ฒˆ ์™ผ์ชฝ์ด๋‚˜ ์˜ค๋ฅธ์ชฝ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ์„ ํƒํ•˜๊ณ , ์„ ํƒํ•˜์ง€ ์•Š์€ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ์•„๋ผ๋“œ๋„ค์˜ ์‹ค์— ์—ฐ๊ฒฐํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์‹ค์˜ ๊ฐ€์ง€(branch)์˜ ํƒ€์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Branch2 Tree2 Tree2 2 Tree2 ๋น„์Šทํ•˜๊ฒŒ, ์‚ผ์ง„ ํŠธ๋ฆฌ์˜ ์‹ค์€ Tree3 1 Tree3 Tree3 Tree3 ๋‹ค์Œ ํƒ€์ž…์˜ ๊ฐ€์ง€๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ Branch3 3 Tree3 Tree3 ๋งค ๋‹จ๊ณ„๋งˆ๋‹ค ์„ธ ํ•˜์œ„ ํŠธ๋ฆฌ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๊ณ  ๋‚˜๋จธ์ง€ ๋‘ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ๋ณด๊ด€ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋„ํ•จ์ˆ˜ d x = ร— ๊ทธ๋ฆฌ๊ณ  d x = ร— 2 ์™€ ๋น„์Šทํ•˜์ง€ ์•Š์€๊ฐ€? ์ด ๋ฏธ์Šคํ„ฐ๋ฆฌ์˜ ํ•ต์‹ฌ์€ ์ž๋ฃŒ ๊ตฌ์กฐ์˜ ์›ํ™€(one-hole) ๋ฌธ๋งฅ์ด๋ผ๋Š” ๊ฐœ๋…์ด๋‹ค. ํŠธ๋ฆฌ Tree์ฒ˜๋Ÿผ, ํƒ€์ž… X์— ๋Œ€ํ•ด ๋งค๊ฐœํ™”๋œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ƒ์ƒํ•ด ๋ณด๋ผ. ์ด์ œ ์ด ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ X ํƒ€์ž…์˜ ์•„์ดํ…œ ํ•˜๋‚˜๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ๊ทธ ๋นˆ ์œ„์น˜์— ์–ด๋–ป๊ฒŒ ํ‘œ์‹์„ ๋‚จ๊ธด๋‹ค๋ฉด, ํ‘œ์‹ ์žˆ๋Š” ๊ตฌ๋ฉ์„ ํฌํ•จํ•˜๋Š” ๊ตฌ์กฐ๋ฅผ ์–ป๊ฒŒ ๋œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋ฌผ์„ "์›ํ™€ ๋ฌธ๋งฅ"์ด๋ผ ๋ถ€๋ฅด๊ณ  X ํƒ€์ž…์˜ ์•„์ดํ…œ์„ ๊ทธ ๊ตฌ๋ฉ์— ๋‹ค์‹œ ๋„ฃ์œผ๋ฉด ์™„๋ฒฝํžˆ ์ฑ„์›Œ์ง„ Tree๋ฅผ ๋˜๋Œ๋ ค ๋ฐ›๋Š”๋‹ค. ์ด ๊ตฌ๋ฉ์€ ํŠน์ˆ˜ ์œ„์น˜, ์ดˆ์  ์—ญํ• ์„ ํ•œ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์„ ๋ณด๋ผ. ํŠธ๋ฆฌ Tree์—์„œ X ํƒ€์ž…์˜ ๊ฐ’์„ ํ•˜๋‚˜ ์ œ๊ฑฐํ•˜๋ฉด ๊ทธ ์œ„์น˜์— ๊ตฌ๋ฉ์ด ๋‚จ๋Š”๋‹ค X๋ฅผ ์›ํ™€ ๋ฌธ๋งฅ์— ์ง‘์–ด๋„ฃ๋Š” ์ž‘์—…์˜ ๋” ์ถ”์ƒํ™”๋œ ๋ฌ˜์‚ฌ ์šฐ๋ฆฌ๊ฐ€ ๊ด€์‹ฌ ์žˆ๋Š” ๊ฒƒ์€ ์›ํ™€ ๋ฌธ๋งฅ์— ๋„ฃ์„ ํƒ€์ž…์ด๋‹ค. ์ฆ‰ ์ด๊ฒƒ์„ ํ•˜์Šค ์ผˆ๋กœ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ•  ๊ฒƒ์ธ๊ฐ€? ๋ฌธ์ œ๋Š” ๊ทธ ์ดˆ์ ์„ ํšจ์œจ์ ์œผ๋กœ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ณง ๋ณด๊ฒ ์ง€๋งŒ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•œ ๊ท€๋‚ฉ๋ฒ•์„ ํ†ตํ•ด ์›ํ™€ ๋ฌธ๋งฅ์˜ ํ‘œํ˜„๋ฒ•์„ ์ฐพ์œผ๋ฉด ์ž๋™์œผ๋กœ ํšจ์œจ์ ์ธ ์ž๋ฃŒํ˜•์„ ์–ป๊ฒŒ ๋œ๋‹ค.7 ๊ทธ๋Ÿฌ๋‹ˆ, ์ž‘์šฉ์ž ์™€ ์ธ์ž ํƒ€์ž… ๋ฅผ ๊ฐ€์ง€๋Š” ์ž๋ฃŒ๊ตฌ์กฐ X ๊ฐ€ ์žˆ์„ ๋•Œ,์˜ ๊ตฌ์กฐ๋กœ๋ถ€ํ„ฐ ์›ํ™€ ๋ฌธ๋งฅ์˜ ํƒ€์ž… F X ๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด์ž. F ๋ผ๋Š” ํ‘œ๊ธฐ์—์„œ ์ด๋ฏธ ๋“œ๋Ÿฌ๋‚˜๋“ฏ์ด ํ•ฉ, ๊ณฑ, ํ•ฉ์„ฑ์˜ ์›ํ™€ ๋ฌธ๋งฅ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๊ทœ์น™์€ ๋ผ์ดํ”„๋‹ˆ์ธ ์˜ ๋ฏธ๋ถ„ ๊ทœ์น™๊ณผ ์ •ํ™•ํžˆ ๊ฐ™๋‹ค. ์›ํ™€ ๋ฌธ๋งฅ ์„ค๋ช… ( C n t) X = 0 = C n t X ์—๋Š” ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์›ํ™€ ๋ฌธ๋งฅ์˜ ํƒ€์ž…์€ empty์ด๋‹ค. ( I) X = 1 = d์—์„œ์˜ ์œ„์น˜๋Š” ํ•˜๋‚˜๋ฟ์ด๋‹ค.๋ฅผ ํ•˜๋‚˜ ์ œ๊ฑฐํ•˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ์—๋Š” ๋” ์ด์ƒ ์ด ์—†๋‹ค. ๊ทธ๋ฆฌ๊ณ ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๊ฐ€ ์˜ค์ง ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์—, d์˜ ์›ํ™€ ๋ฌธ๋งฅ์€ ํ•˜๋‚˜๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ ์›ํ™€ ๋ฌธ๋งฅ์˜ ํƒ€์ž…์€ ์‹ฑ๊ธ€ํ„ด ํƒ€์ž…์ด๋‹ค. ( + ) โˆ‚ + G + ํƒ€์ž…์˜ ์›์†Œ๋Š” ๊ทธ ํƒ€์ž…์ด ๋˜๋Š”์ด๊ธฐ ๋•Œ๋ฌธ์— ์›ํ™€ ๋ฌธ๋งฅ์€ F ๋˜๋Š” G ์ด๋‹ค. ( ร— ) F โˆ‚ + F G ![](https://upload.wikimedia.org/wikipedia/commons/2/20/One-hole-context-product.png) ์Œ์˜ ์›ํ™€ ๋ฌธ๋งฅ์—์„œ ๊ทธ ๊ตฌ๋ฉ์€ ์ฒซ ๋ฒˆ์งธ ๋˜๋Š” ๋‘ ๋ฒˆ์งธ ๊ตฌ์„ฑ์š”์†Œ์— ์žˆ๋‹ค. ( โˆ˜ ) ( F G ) โˆ‚ ![](https://upload.wikimedia.org/wikipedia/commons/6/61/One-hole-context-composition.png) ์—ฐ์‡„ ๋ฒ•์น™. The hole in a composition arises by making a hole in the enclosing structure and fitting the enclosed structure in. ๋ฌผ๋ก  ๊ตฌ๋ฉ์„ ์ฑ„์šฐ๋Š” plug ํ•จ์ˆ˜์˜ ํƒ€์ž…์€ ( F X ) X F X ์ด๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๊ตฌ๋ฌธ์€ ํŽ‘ํ„ฐ์˜ ๋ฏธ๋ถ„, ์ฆ‰ ์ธ์ž๊ฐ€ ํ•˜๋‚˜์ธ ํ•จ์ˆ˜์— ๊ด€ํ•œ ํ‘œ๊ธฐ์˜€๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์†์œผ๋กœ ์“ฐ๋Š” ๋ฐ ํŽธํ•œ ํ‘œ๊ธฐ๋ฒ•์ธ X ๋„ ์žˆ๊ณ  ์ด๊ฒƒ์ด ๊ณ„์‚ฐ์—๋„ ์ข€ ๋” ์ ํ•ฉํ•˜๋‹ค. ์•„๋ž˜ ์ฒจ์ž๋Š” ์šฐ๋ฆฌ๊ฐ€ ๊ทธ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ๋ฏธ๋ถ„ํ•˜๊ฒ ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ( F ) = โˆ‚ ( X ) ๋‹ค์Œ์€ ํ•˜๋‚˜์˜ ์˜ˆ์‹œ๋‹ค. ( d I) = โˆ‚ ( ร— ) = 1 X + X 1 โ‰… 2 X X ๋Š” ์ธ์ž ์ƒ๋žต, ์€ ์ธ์ž ํ‘œ๊ธฐ ๋ฐฉ์‹์ผ ๋ฟ ์ฐจ์ด๋Š” ์—†๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋ช‡ ๊ฐ€์ง€ ๋ฒ•์น™๋“ค์„ ์ธ์ž ์ƒ๋žต์‹์œผ๋กœ ์žฌ์ž‘์„ฑํ•˜๋ผ. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ณฑ์˜ ๋ฒ•์น™์˜ ์ขŒ๋ณ€์€ X ( X G X ) . . ๊ฐ€ ๋œ๋‹ค. ์›ํ™€ ๋ฌธ๋งฅ์— ์ต์ˆ™ํ•ด์ง€๊ธฐ ์œ„ํ•ด ๊ณฑ n := ร— ร—. ร— ์„<NAME>์ ์œผ๋กœ ๋ฏธ๋ถ„ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์›ํ™€ ๋ฌธ๋งฅ์— ์ƒ์‘ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋ผ. ์›ํ™€ ๋ฌธ๋งฅ์€ ํƒ€์ž…์˜ ๊ฐ’์„ ๋‹ค์‹œ ๋„ฃ์„ ์ˆ˜ ์—†๋‹ค๋ฉด ์“ธ๋ชจ๊ฐ€ ์—†๋‹ค. ์œ„์˜ ๋‹ค์„ฏ ๊ฐœ ๋ฒ•์น™์— ๋Œ€์‘ํ•˜๋Š” plug ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ๋‘ ๋ณ€์ˆ˜์˜ ์—ฐ์‡„ ๋ฒ•์น™์„ ๊ณต์‹ํ™”ํ•˜๊ณ  ๊ทธ๊ฒƒ์ด ์›ํ™€ ๋ฌธ๋งฅ์„ ์ƒ์„ฑํ•จ์„ ์ฆ๋ช…ํ•˜๋ผ. ๋ฐ”์ดํŽ‘ํ„ฐ X๋ฅผ ์ง ( , ) ๋‚ด์˜ ์ผ๋ฐ˜ ํŽ‘ํ„ฐ๋กœ ๋ณผ ๊ฒƒ. ๋ฐ”์ดํŽ‘ํ„ฐ์˜ ํŽธ๋ฏธ๋ถ„์— ๋Œ€ํ•œ ์ธ์ž ์ƒ๋žต์‹ ํ‘œ๊ธฐ๊ฐ€ ํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋ฏธ๋ถ„์„ ํ†ตํ•œ ์ง€ํผ ์œ„์˜ ๋ฒ•์น™๋“ค๋กœ ์žฌ๊ท€ ์ž๋ฃŒํ˜• F := ฮผ. X ์— ๋Œ€ํ•œ ์ง€ํผ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ F๋Š” ๋‹คํ˜• ํŽ‘ ํ„ฐ(polynomial functor)๋‹ค. ์ง€ํผ๋Š” ํŠน์ • ํ•˜์œ„ ํŠธ๋ฆฌ ์ฆ‰ F ํƒ€์ž…์˜ ํฐ ํŠธ๋ฆฌ ์•ˆ์— ์žˆ๋Š” ๊ฐ™์€ ํƒ€์ž…์˜ ํ•˜์œ„ ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์ดˆ์ ์ด๋‹ค. ์•ž์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ง€ํผ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ดˆ์ ์„ ๋งž์ถ”๊ธธ ์›ํ•˜๋Š” ํ•˜์œ„ ํŠธ๋ฆฌ, ๊ทธ ํ•˜์œ„ ํŠธ๋ฆฌ๊ฐ€ ์†ํ•˜๋Š” ๋ฌธ๋งฅ์ธ ์Šค๋ ˆ๋“œ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. i p r = ฮผ ร— o t x F ์ด์ œ ์ด ๋ฌธ๋งฅ์€ ์ผ๋ จ์˜ ๋‹จ๊ณ„์ด๋ฉฐ ๊ฐ ๋‹จ๊ณ„์—์„œ ฮผ ์•ˆ์— ์žˆ๋Š” ํŠน์ • ํ•˜์œ„ ํŠธ๋ฆฌ F ๋ฅผ ์„ ํƒํ•œ๋‹ค. ์„ ํƒํ•˜์ง€ ์•Š์€ ํ•˜์œ„ ํŠธ๋ฆฌ๋“ค์€ ์›ํ™€ ๋ฌธ๋งฅ F ( F ) ์— ์˜ํ•ด ์ˆ˜์ง‘๋œ๋‹ค. ์ด ๋ฌธ๋งฅ์˜ ๊ตฌ๋ฉ์€ ์šฐ๋ฆฌ๊ฐ€ ์ž…์žฅํ•˜์ง€ ์•Š์€ ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ์‚ญ์ œํ•จ์œผ๋กœ์จ ์ƒ๊ธด๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ํ•œ ๊ฒƒ์„ ์ข…ํ•ฉํ•˜๋ฉด o t x F = L s ( F ( F ) ) ๋˜๋Š” ๋‹ค์Œ๊ณผ ๋™์น˜๋‹ค. o t x F = 1 + โˆ‚ ( F ) C n e t ๊ตฌ์ฒด์ ์œผ๋กœ ๊ณ„์‚ฐ์ด ์–ด๋–ป๊ฒŒ ์ง„ํ–‰๋˜๋Š”์ง€๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ์˜ ๋ฏธ๊ถ ์ž๋ฃŒํ˜•์— ๋Œ€ํ•œ ์ง€ํผ๋ฅผ ์ฒด๊ณ„์ ์œผ๋กœ ๊ตฌ์ถ•ํ•ด ๋ณด์ž. data Node a = DeadEnd a | Passage a (Node a) | Fork a (Node a) (Node a) ์ด ์žฌ๊ท€ ํƒ€์ž…์€ ๋‹ค์Œ ํŽ‘ํ„ฐ์˜ o e A = A A X A X X ๊ณ ์ •์  o e = ฮผ. o e A์ด๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. o e โ‰… o e A ( o e) A A N d A A N d A ๋ฏธ๋ถ„ํ•˜๋ฉด X ( o e A) A 2 A X ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ์„ ์–ป๋Š”๋‹ค. N d F ( o e) A 2 A N d A ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋ฌธ๋งฅ์€ o t x N d F L s ( N d F ( o e) ) L s ( + ร— ร— ๋‹ค์Œ๊ณผ ๋น„๊ตํ•ด ๋ณด๋ฉด data Branch a = KeepStraightOn a | TurnLeft a (Node a) | TurnRight a (Node a) type Thread a = [Branch a] ์ด ๋‘˜์€ ์ •ํ™•ํžˆ ๊ฐ™๋‹ค! ์—ฐ์Šต๋ฌธ์ œ ์‚ผ์ง„ ํŠธ๋ฆฌ์— ๋Œ€ํ•œ ์ง€ํผ๋ฅผ ๋ฏธ๋ถ„์„ ํ†ตํ•ด ๊ตฌ์ถ•ํ•˜๋ผ. ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ์ง€ํผ๋ฅผ ๊ตฌ์ถ•ํ•˜๋ผ. ์•ž์˜ ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ๊ณ ๋ คํ•œ ์ˆ˜์‚ฌ์ ์ธ ์งˆ๋ฌธ: ๋ฆฌ์ŠคํŠธ์™€ ์Šคํƒ์€ ๋ฌด์—‡์ด ๋‹ค๋ฅธ๊ฐ€? ๊ณ ์ •์ ์˜ ๋ฏธ๋ถ„ ํ•ฉ๊ณผ ๊ณฑ ๋ง๊ณ ๋„ ์ž๋ฃŒํ˜•์— ๋Œ€ํ•ด ํ•  ์ด์•ผ๊ธฐ๋Š” ๋” ์žˆ๋‹ค. ๊ณ ์ •์  ์—ฐ์‚ฐ์ž๋Š” ๋”ฑํžˆ ๋ฏธ์ ๋ถ„์—์„œ ๋Œ€์‘ํ•˜๋Š” ๊ฒƒ์ด ์—†๋‹ค. ๊ทธ๋ž˜์„œ ์œ„์˜ ํ‘œ์—๋Š” ๊ณ ์ •์  F X = ฮผ. X Y ์˜ ๋ฏธ๋ถ„ ๋ฒ•์น™์ด ๋น ์ ธ์žˆ๋‹ค. X ( F X ) ? ์ด ๊ณต์‹์€ ๋‘ ๋ณ€์ˆ˜์˜ ์—ฐ์‡„ ๋ฒ•์น™์„ ์ˆ˜๋ฐ˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฐ์Šต๋ฌธ์ œ๋กœ ๋‚จ๊ธฐ๊ฒ ๋‹ค. ๋Œ€์‹  ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์ธ o e์— ๋Œ€ํ•ด ๊ณ„์‚ฐํ•ด ๋ณด์ž. A ( o e A ) โˆ‚ ( + ร— o e + ร— o e ร— o e) 1 N d A N d A N d A + A ( o e) ( + ร— ร— o e) ๋ฌผ๋ก  A ( o e) ๋ฅผ ๋” ์ด์ƒ ์ „๊ฐœํ•˜๋Š” ๊ฒƒ์€ ์“ธ๋ชจ๊ฐ€ ์—†์ง€๋งŒ ์ด๊ฒƒ์„ ๊ณ ์ •์ ์œผ๋กœ ๋ณด๊ณ  ๋‹ค์Œ์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. A ( o e) = ฮผ. A + S A X ์š”์•ฝํ•˜๋ฉด A 1 N d A N d A N d A ๊ทธ๋ฆฌ๊ณ  A A 2 A N d A ์ด๋‹ค. ์ด ์žฌ๊ท€ ํƒ€์ž…์€ ๋ฆฌ์ŠคํŠธ์ธ๋ฐ ์›์†Œ ํƒ€์ž…์ด A ์ด๊ณ  ๋นˆ ๋ฆฌ์ŠคํŠธ๋งŒ A ํƒ€์ž…์˜ ๊ธฐ๋ณธ ๊ฐ€์ •์œผ๋กœ ๋ฐ”๊พผ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์œ ํ•œํ•˜๋‹ค๋ฉด ๊ธฐ๋ณธ ๊ฐ€์ •์„ 1๋กœ ๋‘๊ณ  A ๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ๋นผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. A ( o e) T A ( X 1 S A X ) = T A L s ( A ) ๋งˆ์ง€๋ง‰ ๋ฌธ๋‹จ์—์„œ ๋„์ถœํ•œ ์ง€ํผ์™€ ๋น„๊ตํ•ด ๋ณด๋ฉด ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์€ ์šฐ๋ฆฌ์˜ ๋ฌธ๋งฅ์ด๊ณ  i t ( A ) C n e t o e ๋‹ค์Œ์— ์˜ํ•ด ร— A N d A ๊ฒฐ๊ตญ ๋‹ค์Œ์„ ์–ป๊ฒŒ ๋œ๋‹ค. i p r o e โ‰… A ( o e) A ๊ทธ๋Ÿฌ๋ฏ€๋กœ o e๋ฅผ์— ๋Œ€ํ•ด ๋ฏธ๋ถ„ํ•˜๋ฉด์˜ ์ง€ํผ๋ฅผ ์–ป๊ฒŒ ๋œ๋‹ค! ์—ฐ์Šต๋ฌธ์ œ 2๋ณ€์ˆ˜ ์—ฐ์‡„ ๋ฒ•์น™์„ ์ด์šฉํ•˜์—ฌ ๊ณ ์ •์ ์˜ ๋ฏธ๋ถ„ ๊ทœ์น™์„ ๊ณต์‹ํ™”ํ•˜๋ผ. inductive( ) ๊ณ ์ •์ ๊ณผ coinductive( ) ๊ณ ์ •์ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•„๋Š”๊ฐ€? coinductive ๊ณ ์ •์ ์— ๋Œ€ํ•œ ๋ฒ•์น™์€ ์–ด๋–ป๊ฒŒ ๋˜๋Š”๊ฐ€? ํ•จ์ˆ˜๋ฅผ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ๋ฏธ๋ถ„ํ•˜๊ธฐ ์›ํ™€ ๋ฌธ๋งฅ์˜ ํƒ€์ž…์„ ์ฐพ์„ ๋•Œ๋Š” f ( ) x ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. f ( ) g ( ) ๊ฐ™์€ ํ‘œํ˜„ ์‹๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด x d 2 2 2 ๋กœ์„œ, 4-์ง์˜ two-hole ๋ฌธ๋งฅ์ด๋‹ค. ๊ทธ ๋ฏธ๋ถ„์€ = 2 ๋ผ ํ•  ๋•Œ x d 2 = d 2 u = 2 = 2 2 ์™€ ๊ฐ™์ด ํ•œ๋‹ค. ์ง€ํผ vs ๋ฌธ๋งฅ ๊ทธ๋Ÿฐ๋ฐ ์ผ๋ฐ˜์ ์œผ๋กœ ์ง€ํผ์™€ one-hole ๋ฌธ๋งฅ์€ ๋‹ค๋ฅธ ๊ฒƒ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์ง€ํผ๋Š” ์ž„์˜์˜ ํ•˜์œ„ ํŠธ๋ฆฌ์— ๋Œ€ํ•œ ์ดˆ์ ์ธ ๋ฐ˜๋ฉด one-hole ๋ฌธ๋งฅ์€ ํƒ€์ž… ์ƒ์„ฑ์ž์˜ ์ธ์ž์— ๋Œ€ํ•œ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ์ž๋ฃŒํ˜•์—์„œ data Tree a = Leaf a | Bin (Tree a) (Tree a) ๊ณ ์ •์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. r e A = ฮผ. + ร— ์ง€ํผ๋Š” ์ตœ์ƒ์œ„๊ฐ€ Bin์ด๋‚˜ Leaf์ธ ํ•˜์œ„ ํŠธ๋ฆฌ์— ์ดˆ์ ์„ ๋งž์ถœ ์ˆ˜ ์žˆ์ง€๋งŒ r e์˜ one-hole ๋ฌธ๋งฅ์€ Leaf๋“ค์—๋งŒ ์ดˆ์ ์„ ๋งž์ถœ ์ˆ˜ ์žˆ๋Š”๋ฐ Leaf๋“ค์—์„œ๋งŒ ํƒ€์ž…์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. o e์˜ ๋„ํ•จ์ˆ˜๋Š” ์ง€ํผ์ธ๋ฐ ๋ชจ๋“  ํ•˜์œ„ ํŠธ๋ฆฌ์˜ ์ตœ์ƒ์œ„๋Š” ์— ์˜ํ•ด ์žฅ์‹๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋†€๋ž๊ฒŒ๋„ A ( r e A ) A T e A ์˜ ์ง€ํผ๋Š” ๊ฐ™์€ ํƒ€์ž…์ด๋‹ค. ๊ทธ ๊ณ„์‚ฐ์€ ์–ด๋ ต์ง€ ์•Š์ง€๋งŒ ์™œ ๊ทธ๋ ‡๊ฒŒ ๋˜๋Š”์ง€ ์ด์œ ๋ฅผ ์•„๋Š”๊ฐ€? F ์˜ ์ง€ํผ ๊ตฌ์ถ•์€ ๋ณด์กฐ ๋ณ€์ˆ˜๋ฅผ ๋„์ž…ํ•ด X Y F X Y ์— ๋Œ€ํ•ด ๋ฏธ๋ถ„ํ•˜๊ณ  =๋กœ ์น˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค. ์ด๊ฒƒ์ด ์™œ ์•Œ๋งž์€๊ฐ€? ๊ทธ ์ง€ํผ๊ฐ€ one-hole ๋ฌธ๋งฅ๊ณผ ๋‹ค๋ฅธ ํƒ€์ž… A ๋ฅผ ์ฐพ์•„๋ผ. ๊ฒฐ๋ก  ๋ฏธ์ ๋ถ„์˜ ๋ฒ•์น™๋“ค์ด ์–ด๋–ป๊ฒŒ ์ด์‚ฐ์ ์ธ ์„ธ๊ณ„์— ๋‚˜ํƒ€๋‚˜๋Š”์ง€ ์งˆ๋ฌธํ•˜๋ฉฐ ์ด ์ ˆ์„ ๋งˆ์น˜๊ฒ ๋‹ค. ์ง€๊ธˆ์€ ์•„๋ฌด๋„ ๋ชจ๋ฅธ๋‹ค. ์ตœ์†Œํ•œ ์„ ํ˜•(linear)์˜ ์ด์‚ฐ์  ํ‘œ๊ธฐ๋Š” "์˜ค์ง ํ•˜๋‚˜"์ž„์€ ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ํƒ€์ž…์˜ ํ•ญ๋ชฉ์„ one-hole ๋ฌธ๋งฅ์˜ ๊ตฌ๋ฉ์— ๋„ฃ๋Š” ํ•จ์ˆ˜์˜ ์ค‘์š”ํ•œ ํŠน์ง•์€ ๊ทธ ํ•ญ๋ชฉ์ด ์˜ค์ง ํ•œ ๋ฒˆ ์‚ฌ์šฉ๋œ๋‹ค๋Š” ๊ฒƒ, ์ฆ‰ ์„ ํ˜•์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์ด ์‚ฝ์ž… ๋งคํ•‘์˜ ํƒ€์ž…์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. X X ( โŠธ F X ) ์—ฌ๊ธฐ์„œ โŠธ๋Š” ์„ ํ˜• ํ•จ์ˆ˜๋กœ์„œ, ์„ ํ˜• ๋…ผ๋ฆฌ์—์„œ ๊ทธ๋ ‡๋“ฏ์ด ๊ทธ ์ธ์ž๋ฅผ ๋ณต์ œํ•˜๊ฑฐ๋‚˜ ๋ฌด์‹œํ•˜์ง€ ์•Š๋Š”๋‹ค. ์–ด๋–ค ์˜๋ฏธ์—์„œ one-hole ๋ฌธ๋งฅ์€ ํ•จ์ˆ˜ ๊ณต๊ฐ„ โŠธ X ์˜ ํ‘œํ˜„, ์ฆ‰ โ†’ X ์˜ ์„ ํ˜• ๊ทผ์‚ฌ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋…ธํŠธ ์ด์–ธ ์ŠคํŠœ์–ดํŠธ. The true story of how Tehseus found his way out of the labyrinth. Scientific American, 1991๋…„ 2์›”, 137์ชฝ. โ†ฉ Gรฉrard Huet. The Zipper. Journal of Functional Programming, 7 (5), Sept 1997, pp. 549--554. PDF โ†ฉ Gรฉrard Huet์ด ๋งํ•˜๋Š” ์ง€ํผ๋Š” ์ „์ฒด ํ•˜์œ„ ํŠธ๋ฆฌ๋ฅผ ๊ต์ฒดํ•˜๋Š” ๊ฒƒ์ด, ์—ฐ๊ด€๋œ ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์–ด๋„ ํ—ˆ์šฉ๋จ์— ์ฃผ๋ชฉํ•˜๋ผ. ์šฐ๋ฆฌ์˜ ๋ฏธ๊ถ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฌด๊ด€ํ•œ ์ผ์ด๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๋ฏธ๋ถ„ ์ ˆ์—์„œ ๋‹ค์‹œ ๋‹ค๋ฃจ๊ฒ ๋‹ค. โ†ฉ ๋ฌผ๋ก  ์ตœ์ƒ์œ„์—์„œ ์ƒ์ˆ˜ ๋‹จ๊ณ„๋งŒํผ ๋ฐ‘์˜ ์ž„์˜ ๋…ธ๋“œ๋„ ์ƒ์ˆ˜ ์‹œ๊ฐ„ ์•ˆ์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค. โ†ฉ ๋…ธ๋“œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ, ์ „์ฒด ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ ์ตœ์ƒ์œ„ ๋…ธ๋“œ ์™ธ์˜ ๋…ธ๋“œ๋“ค์ด ์˜ํ–ฅ์„ ๋ฐ›๋”๋ผ๋„ amortized ์ƒ์ˆ˜ ์‹œ๊ฐ„ ์•ˆ์— ๊ฐ€๋Šฅํ•จ์— ์ฃผ๋ชฉํ•˜๋ผ. ํ•œ ์˜ˆ๋กœ ์ด์ง„์ˆ˜ ํ‘œํ˜„์—์„œ ์ฆ๊ฐ€ ์—ฐ์‚ฐ์„ ๋ณด์ž. ๊ฐ€๋ น 111... 11์„ ์ฆ๊ฐ€์‹œํ‚ค๋ ค๋ฉด ๋ชจ๋“  ์ž๋ฆฟ์ˆ˜๋ฅผ ๊ฑด๋“œ๋ ค์„œ 1000.. 00์„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๊ทธ ์ฆ๊ฐ€ํ•จ์ˆ˜๋Š” ๊ทธ๋Ÿผ์—๋„ amortized ์ƒ์ˆ˜ ์‹œ๊ฐ„ ์•ˆ์— ์‹คํ–‰๋œ๋‹ค. (์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋Š” ์ƒ์ˆ˜ ์‹œ๊ฐ„์ด ์•„๋‹ˆ๋‹ค) โ†ฉ Conor Mc Bride. The Derivative of a Regular Type is its Type of One-Hole Contexts. ์˜จ๋ผ์ธ ์ด์šฉ ๊ฐ€๋Šฅ. PDF โ†ฉ ์ด ํ˜„์ƒ์€ ์ด๋ฏธ generic tries์—์„œ ๋‚˜ํƒ€๋‚œ๋‹ค. โ†ฉ 09 ๋ Œ์ฆˆ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Lenses_and_functional_references ๋ Œ์ฆˆ ๋ง›๋ณด๊ธฐ ๋ Œ์ฆˆ๋ฅผ ํ–ฅํ•œ ์—ฌ์ • ์ˆœํšŒ ์„ธํ„ฐ(setter) ์ ‘๊ธฐ Getter ๋งˆ์ง€๋ง‰ ๋ Œ์ฆˆ ํ•ฉ์„ฑ(composition) ์—ฐ์‚ฐ์ž ๋งŒ๋Šฅ์นผ State ์กฐ์ž‘ Iso Prism ๋ฒ•์น™ ๋‹คํ˜•์„ฑ ๊ฐฑ์‹  ์•„๋ฌด๋Ÿฐ ์กฐ๊ฑด ์—†์ด ๋” ์ฝ์„๊ฑฐ๋ฆฌ ์ด ์žฅ์€ ํ•จ์ˆ˜ํ˜• ์ฐธ์กฐ์— ๋Œ€ํ•œ ๊ฒƒ์ด๋‹ค. "์ฐธ์กฐ"๋ผ ํ•จ์€, ํ•จ์ˆ˜ํ˜• ์ฐธ์กฐ๊ฐ€ ๊ฐ’์˜ ์ผ๋ถ€๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ, ๊ทธ ์ผ๋ถ€์— ์ ‘๊ทผํ•˜๊ณ  ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋œปํ•œ๋‹ค. "ํ•จ์ˆ˜ํ˜•"์ด๋ผ ํ•จ์€ ํ•จ์ˆ˜ํ˜• ์ฐธ์กฐ๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๋ฐฉ์‹์ด ํ•จ์ˆ˜์—์„œ ๊ธฐ๋Œ€๋˜๋Š” ์œ ์—ฐํ•จ๊ณผ ํ•ฉ์„ฑ ๊ฐ€๋Šฅ์„ฑ(composability)์„ ์ œ๊ณตํ•จ์„ ๋œปํ•œ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋ฐฐ์šธ ๊ฒƒ์€ lens ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๊ตฌํ˜„ํ•˜๋Š” ํ•จ์ˆ˜ํ˜• ์ฐธ์กฐ๋‹ค. lens๋Š” ํ•จ์ˆ˜ํ˜• ์ฐธ์กฐ ์ค‘์—์„œ๋„ ํŠนํžˆ ์œ ๋ช…ํ•œ ๋ Œ์ฆˆ์—์„œ ๋”ฐ์˜จ ์ด๋ฆ„์ด๋‹ค. ๊ฐœ๋… ์ž์ฒด๋„ ๋งค์šฐ ํฅ๋ฏธ๋กญ์ง€๋งŒ, ๋ Œ์ฆˆ์™€ ๋‹ค๋ฅธ ํ•จ์ˆ˜ํ˜• ์ฐธ์กฐ๋“ค์€ ํŽธ๋ฆฌํ•˜๊ณ  ๋†€๋ž๋„๋ก ํ”ํ•œ ๊ด€์šฉ๊ตฌ๋“ค์„ ๊ฐ€๋Šฅ์ผ€ ํ•˜๋ฉฐ ๋งŽ์€ ์œ ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์“ฐ์ด๊ณ  ์žˆ๋‹ค. ๋ Œ์ฆˆ ๋ง›๋ณด๊ธฐ ๊ฐ€๋ฒผ์šด ๋ชธํ’€๊ธฐ๋กœ ๋ Œ์ฆˆ์˜ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ๊ฒ ๋‹ค. ๋ Œ์ฆˆ๋Š” ํ‰๋ฒ”ํ•œ ํ•˜์Šค ์ผˆ ๋ ˆ์ฝ”๋“œ ๊ตฌ๋ฌธ์„ ํ›Œ๋ฅญํ•˜๊ฒŒ ๋Œ€์ฒดํ•œ๋‹ค. ์ด ์ ˆ์—๋Š” ์„ค๋ช…์ด ๋ณ„๋กœ ์—†๋Š”๋ฐ, ๋น ์ง„ ๊ฒƒ์€ ๋’ค์—์„œ ๋ณด์ถฉํ•˜๊ฒ ๋‹ค. 2D ๋“œ๋กœ์ž‰ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ํ”ํžˆ ๋‚˜์˜ฌ ๋ฒ•ํ•œ ๋‹ค์Œ์˜ ํƒ€์ž…๋“ค์„ ์‚ดํŽด๋ณด์ž. -- ํ‰๋ฉด ์ƒ์˜ ์ . data Point = Point { positionX :: Double , positionY :: Double } deriving (Show) -- ํ•œ ์ ์—์„œ ๋‹ค๋ฅธ ์ ์œผ๋กœ ๊ฐ€๋Š” ์„ ๋ถ„. data Segment = Segment { segmentStart :: Point , segmentEnd :: Point } deriving (Show) -- ์ ๊ณผ ์„ ๋ถ„์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๋ณด์กฐ ํ•จ์ˆ˜๋“ค. makePoint :: (Double, Double) -> Point makePoint (x, y) = Point x y makeSegment :: (Double, Double) -> (Double, Double) -> Segment makeSegment start end = Segment (makePoint start) (makePoint end) ๋ ˆ์ฝ”๋“œ ๊ตฌ๋ฌธ์€ ํ•„๋“œ์— ์ ‘๊ทผํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์œ„ ํ•จ์ˆ˜๋“ค์„ ์ด์šฉํ•˜๋ฉด ์„ ๋ถ„์„ ์ •์˜ํ•˜๋Š” ์ ์˜ ์ขŒํ‘œ๋ฅผ ์‰ฝ๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. GHCi> let testSeg = makeSegment (0, 1) (2, 4) GHCi> positionY . segmentEnd $ testSeg GHCi> 4.0 ํ•˜์ง€๋งŒ ๊ฐฑ์‹ ์€ ์กฐ๊ธˆ ๊ทธ๋ ‡๋‹ค. GHCi> testSeg { segmentEnd = makePoint (2, 3) } Segment {segmentStart = Point {positionX = 0.0, positionY = 1.0} , segmentEnd = Point {positionX = 2.0, positionY = 3.0}} ๊ทธ๋ฆฌ๊ณ  ์ค‘์ฒฉ๋œ ํ•„๋“œ์— ๋„๋‹ฌํ•˜๊ธฐ๊ฐ€ ์ฉ ๊น”๋”ํ•˜์ง€ ์•Š๋‹ค. ์ข…์ ์˜ y ์ขŒํ‘œ์˜ ๊ฐ’์„ ๋‘ ๋ฐฐ๋กœ ํ•˜๋ ค๋ฉด ์ด๋ ‡๊ฒŒ ํ•ด์•ผ ํ•œ๋‹ค. GHCi> :set +m -- Enabling multi-line input in GHCi. GHCi> let end = segmentEnd testSeg GHCi| in testSeg { segmentEnd = end { positionY = 2 * positionY end } } Segment {segmentStart = Point {positionX = 0.0, positionY = 1.0} , segmentEnd = Point {positionX = 2.0, positionY = 8.0}} ๋ Œ์ฆˆ๋ฅผ ์“ฐ๋ฉด ์ด๋Ÿฐ ๋ฒˆ์žกํ•จ์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์‹œ ํ•ด๋ณด์ž. -- ์ด๋ฒˆ ์žฅ์˜ ๋ช‡ ์˜ˆ์ œ๋Š” GHC ํ™•์žฅ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜๋‹ค. -- TemplateHaskell์€ makeLenses๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•˜๋‹ค. RankNTypes์€ -- ๋‚˜์ค‘์— ๋‚˜์˜ฌ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋“ค์— ํ•„์š”ํ•˜๋‹ค. {-# LANGUAGE TemplateHaskell, RankNTypes #-} import Control.Lens data Point = Point { _positionX :: Double , _positionY :: Double } deriving (Show) makeLenses ''Point data Segment = Segment { _segmentStart :: Point , _segmentEnd :: Point } deriving (Show) makeLenses ''Segment makePoint :: (Double, Double) -> Point makePoint (x, y) = Point x y makeSegment :: (Double, Double) -> (Double, Double) -> Segment makeSegment start end = Segment (makePoint start) (makePoint end) ์—ฌ๊ธฐ์„œ ๋ฐ”๋€ ๊ฒƒ์€ makeLenses์„ ์“ด ๊ฒƒ๋ฟ์ด๋‹ค. ์ด๋Ÿฌ๋ฉด Point์™€ Segment์˜ ํ•„๋“œ๋“ค์— ๋Œ€ํ•œ ๋ Œ์ฆˆ๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค. (๋ฐ‘์ค„์€ makeLenses์˜ ์ž‘๋ช… ๊ด€๋ก€ ๋•Œ๋ฌธ์— ํ•„์š”ํ•˜๋‹ค) ๋’ค์—์„œ ๋ณด๊ฒ ์ง€๋งŒ ๋ Œ์ฆˆ ์ •์˜๋ฅผ ์†์ˆ˜ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์ „ํ˜€ ์–ด๋ ต์ง€ ์•Š๋‹ค. ํ•˜์ง€๋งŒ ๋ Œ์ฆˆ๋ฅผ ๋งŒ๋“ค ํ•„๋“œ๊ฐ€ ๋งŽ๋‹ค๋ฉด ์ง€๋ฃจํ•œ ์ž‘์—…์ด ๋  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ํŽธ๋ฆฌํ•  ๊ฒƒ์ด๋‹ค. makeLenses ๋•์— ํ•„๋“œ๋งˆ๋‹ค ๋ Œ์ฆˆ๊ฐ€ ์ƒ๊ฒผ๋‹ค. ๋ Œ์ฆˆ์˜ ์ด๋ฆ„์€ ํ•„๋“œ์˜ ์ด๋ฆ„์—์„œ ๋ฐ‘์ค„์„ ๋บ€ ๊ฒƒ์ด๋‹ค. GHCi> :info positionY positionY :: Lens' Point Double -- Defined at WikibookLenses.hs:9:1 GHCi> :info segmentEnd segmentEnd :: Lens' Segment Point -- Defined at WikibookLenses.hs:15:1 positionY :: Lens' Point Double์ด๋ผ๋Š” ํƒ€์ž…์€ positionY๊ฐ€ Point ๋‚ด์˜ Double์— ๋Œ€ํ•œ ์ฐธ์กฐ์ž„์„ ๋œปํ•œ๋‹ค. ์ด๋Ÿฐ ์ฐธ์กฐ ๋ฐฉ์‹์„ ๋‹ค๋ฃจ๋ ค๋ฉด lens ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ํ•ฉ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. view๊ฐ€ ๊ทธ์ค‘ ํ•˜๋‚˜์ธ๋ฐ, ๋ ˆ์ฝ”๋“œ ์ ‘๊ทผ์ž์ฒ˜๋Ÿผ ๋ Œ์ฆˆ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์„ ์–ป์–ด์˜จ๋‹ค. GHCi> let testSeg = makeSegment (0, 1) (2, 4) GHCi> view segmentEnd testSeg Point {_positionX = 2.0, _positionY = 4.0} set์€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์„ ๋ฎ์–ด์“ด๋‹ค. GHCi> set segmentEnd (makePoint (2, 3)) testSeg Segment {_segmentStart = Point {_positionX = 0.0, _positionY = 1.0} , _segmentEnd = Point {_positionX = 2.0, _positionY = 3.0}} ๋ Œ์ฆˆ์˜ ๊ต‰์žฅํ•œ ์  ์ค‘ ํ•˜๋‚˜๋Š” ํ•ฉ์„ฑํ•˜๊ธฐ ์‰ฝ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. GHCi> view (segmentEnd . positionY) testSeg GHCi> 4.0 segmentEnd . positionY ๊ฐ™์€ ํ•ฉ์„ฑ ๋ Œ์ฆˆ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์ˆœ์„œ๋Š” ํฐ ๊ฒƒ์—์„œ ์ž‘์€ ๊ฒƒ ์ˆœ์ด๋‹ค. ์ด ๊ฒฝ์šฐ ์„ ๋ถ„์˜ ํ•œ ์ ์— ๋Œ€ํ•œ ๋ Œ์ฆˆ๊ฐ€ ๊ทธ ์ ์˜ ์ขŒํ‘œ์— ๋Œ€ํ•œ ๋ Œ์ฆˆ๋ณด๋‹ค ์•ž์— ์˜จ๋‹ค. ๋ ˆ์ฝ”๋“œ ์ ‘๊ทผ์ž๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์กฐ๊ธˆ ๋†€๋ผ์šธ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ(์•ž์—์„œ ๋‚˜์˜จ ๋ Œ์ฆˆ ์—†๋Š” ์˜ˆ์ œ์™€ ๋น„๊ตํ•ด ๋ณด๋ผ) ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•œ (.)๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ตํžˆ ์•Œ๊ณ  ์žˆ๋Š” ๊ทธ ํ•จ์ˆ˜ ํ•ฉ์„ฑ ์—ฐ์‚ฐ์ž๋‹ค. ๋ Œ์ฆˆ์˜ ํ•ฉ์„ฑ์„ ํ†ตํ•ด ์ค‘์ฒฉ๋œ ๋ ˆ์ฝ”๋“œ ๊ฐฑ์‹ ์˜ ์ˆ˜๋ ์„ ๋น ์ ธ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ์ขŒํ‘œ๋ฅผ ๋‘ ๋ฐฐ๋กœ ํ‚ค์šฐ๋Š” ์˜ˆ์ œ๋ฅผ ์ˆ˜์ •ํ•œ ๊ฒƒ์œผ๋กœ, ๋ Œ์ฆˆ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์— over๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค. GHCi> over (segmentEnd . positionY) (2 *) testSeg Segment {_segmentStart = Point {_positionX = 0.0, _positionY = 1.0} , _segmentEnd = Point {_positionX = 2.0, _positionY = 8.0}} ์ด๋Ÿฐ ์˜ˆ์ œ๋“ค์ด ์ฒ˜์Œ์—๋Š” ๋งˆ์ˆ ์ฒ˜๋Ÿผ ๋ณด์ผ์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๋ Œ์ฆˆ ํ•˜๋‚˜๋กœ ์–ด๋–ป๊ฒŒ ๊ฐ’์„ ์–ป๊ณ , ์„ค์ •ํ•˜๊ณ , ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์–ด๋–ป๊ฒŒ ๋ Œ์ฆˆ๋ฅผ (.)๋กœ ํ•ฉ์„ฑํ•˜๋Š” ๊ฒŒ ๊ฐ€๋Šฅํ• ๊นŒ? makeLenses ์—†์ด ๋ Œ์ฆˆ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒŒ ์ •๋ง ์‰ฌ์šธ๊นŒ? ๋ Œ์ฆˆ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์–ด์กŒ๋Š”์ง€ ์•Œ์•„๋ณด๊ณ  ์ด ์งˆ๋ฌธ๋“ค์— ๋Œ€๋‹ตํ•ด ๋ณด์ž. ๋ Œ์ฆˆ๋ฅผ ํ–ฅํ•œ ์—ฌ์ • ๋ Œ์ฆˆ๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋งŽ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ฐœ๋…์  ๋งน์‹ ์„ ํ”ผํ•˜๋Š” ๊ตฌ๋ถˆ๊ตฌ๋ถˆํ•˜๊ณ  ์กฐ์‹ฌ์Šค๋Ÿฌ์šด ๊ธธ์„ ๊ฑธ์„ ๊ฒƒ์ด๋‹ค. ์ด ๊ธธ์—์„œ ๋ช‡ ๊ฐ€์ง€ ๋‹ค๋ฅธ ํ•จ์ˆ˜ํ˜• ์ฐธ์กฐ๋“ค์„ ์†Œ๊ฐœํ•  ๊ฒƒ์ด๋‹ค. lens์˜ ์šฉ๋ฒ•์— ๋”ฐ๋ผ ์ด์ œ๋ถ€ํ„ฐ "optics"๋ผ๋Š” ๋‹จ์–ด๋กœ ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜ํ˜• ์ฐธ์กฐ๋ฅผ ํ†ต์นญํ•˜๊ฒ ๋‹ค. ๋’ค์—์„œ ๋ณด๊ฒ ์ง€๋งŒ lens์˜ optics๋Š” ์„œ๋กœ ์—ฎ์—ฌ ๊ณ„์ธต๋„๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์ด ๊ณ„์ธต๋„๋ฅผ ํƒํ—˜ํ•  ๊ฒƒ์ด๋‹ค. ์ˆœํšŒ ์‹œ์ž‘์€ ๋ Œ์ฆˆ๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋Œ€์‹  ์ƒ๋‹นํžˆ ์—ฐ๊ด€์ด ๋งŽ์€ ์ˆœํšŒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. Traversable ์žฅ์—์„œ traverse๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ตฌ์กฐ๋ฅผ ๊ฑธ์–ด ์ง€๋‚˜๊ฐ€๋ฉฐ ์ „์ฒด์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ์‚ฐ์ถœํ•˜๋Š”๊ฐ€๋ฅผ ๋…ผ์˜ํ–ˆ์—ˆ๋‹ค. traverse :: (Applicative f, Traversable t) => (a -> f b) -> t a -> f (t b) traverse๋ฅผ ์“ธ ๋•Œ๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๊ธฐ ์œ„ํ•ด ์–ด๋–ค Applicative๋“  ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ Identity๋ฅผ ์ ์šฉ์„ฑ ํŽ‘ํ„ฐ๋กœ ์„ ํƒํ•˜๋ฉด traverse๋กœ๋ถ€ํ„ฐ fmap์ด ์–ป์–ด์ง€๋Š” ๊ฒƒ์„ ๋ณธ ์ ์ด ์žˆ๋‹ค. Monoid m => Applicative (Const m)์„ ์“ฐ๋ฉด foldMap๊ณผ Const m์— ๋Œ€ํ•ด์„œ๋„ ๊ฐ™์€ ์ผ์ด ์ผ์–ด๋‚œ๋‹ค. fmap f = runIdentity . traverse (Identity . f) foldMap f = getConst . traverse (Const . f) lens๋Š” ์ด ์•„์ด๋””์–ด๋ฅผ ๋“œ๋„“๊ฒŒ ํ™œ์šฉํ•œ๋‹ค. traverse ๊ฐ™์€ ๊ฒƒ์„ ์ด์šฉํ•ด Traversable ๊ตฌ์กฐ ๋‚ด๋ถ€์˜ ๊ฐ’์„ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์€ ์ „์ฒด์˜ ์ผ๋ถ€๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์‚ผ๋Š” ํ•œ ์˜ˆ์‹œ๋‹ค. ํ•˜์ง€๋งŒ traverse๋Š” ์œ ์—ฐํ•˜๊ธด ํ•˜์ง€๋งŒ ์ œํ•œ๋œ ๋ชฉํ‘œ๋งŒ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์˜ˆ๋กœ, Traversable ํŽ‘ํ„ฐ๊ฐ€ ์•„๋‹Œ ๊ตฌ์กฐ๋ฅผ ๊ฑธ์–ด ์ง€๋‚˜๊ฐ€๊ณ  ์‹ถ๋‹ค๊ณ  ํ•˜์ž. ๊ทธ๋ ‡๋‹ค๋ฉด Point ํƒ€์ž…์— ๋Œ€ํ•œ ๋‹ค์Œ ํ•จ์ˆ˜๋Š” ์ง€๊ทนํžˆ ํƒ€๋‹นํ•˜๋‹ค. pointCoordinates :: Applicative f => (Double -> f Double) -> Point -> f Point pointCoordinates g (Point x y) = Point <$> g x <*> g y pointCoordinates๋Š” Point์— ๋Œ€ํ•œ ์ˆœํšŒ๋‹ค. traverse์˜ ์ผ๋ฐ˜์ ์ธ ๊ตฌํ˜„๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋ณด์ด๊ณ  ๊ฑฐ์˜ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ Traversable ์žฅ์˜ rejectWithNegatives ์˜ˆ์ œ๋ฅผ ๊ณ ์นœ ๊ฒƒ์ด๋‹ค. GHCi> let deleteIfNegative x = if x < 0 then Nothing else Just x GHCi> pointCoordinates deleteIfNegative (makePoint (1, 2)) Just (Point {_positionX = 1.0, _positionY = 2.0}) GHCi> pointCoordinates deleteIfNegative (makePoint (-1, 2)) Nothing pointCoordinates๊ฐ€ ์ „ํ˜•์ ์œผ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ์ด๋Ÿฌํ•œ ์ผ๋ฐ˜ํ™”๋œ ์ˆœํšŒ ํ‘œ๊ธฐ๋Š” lens์˜ ํ•ต์‹ฌ ํƒ€์ž…๋“ค ์ค‘ ํ•˜๋‚˜์ธ Traversal์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. type Traversal s t a b = forall f. Applicative f => (a -> f b) -> s -> f t ๋…ธํŠธ type ์„ ์–ธ ์šฐ๋ณ€์˜ forall f.๋Š” f ์ž๋ฆฌ์— ์–ด๋–ค Appplicative๋“  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ์„ ๋œปํ•œ๋‹ค. ์ด๋Ÿฌ๋ฉด ์ขŒ๋ณ€์— f๋ฅผ ์–ธ๊ธ‰ํ•˜๊ฑฐ๋‚˜ Traversal์„ ์‚ฌ์šฉํ•  ๋•Œ ํŠน์ • f๋ฅผ ํƒํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. Traversal์„ ์ด์šฉํ•˜๋ฉด pointCoordinates์˜ ํƒ€์ž…์„ ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. Traversal Point Point Double Double Traversal s t a b์—์„œ ๊ฐ ํƒ€์ž… ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€ ์ž์„ธํžˆ ์‚ดํŽด๋ณด์ž. s๋Š” Point๊ฐ€ ๋œ๋‹ค. pointCoordinates๋Š” Point์— ๋Œ€ํ•œ ์ˆœํšŒ๋‹ค. t๋Š” Point๊ฐ€ ๋œ๋‹ค. pointCoordinates๋Š” Point๋ฅผ ์‚ฐ์ถœํ•œ๋‹ค. (์–ด๋–ค Applicative ๋ฌธ๋งฅ ์•ˆ์—์„œ) a๋Š” Double์ด ๋œ๋‹ค. pointCoordinates๋Š” Point ๋‚ด์˜ Double ๊ฐ’์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค. (์ ์˜ X, Y ์ขŒํ‘œ) b๋Š” Double์ด ๋œ๋‹ค. ๋ชฉํ‘œ Double ๊ฐ’์€ Double ๊ฐ’์ด ๋œ๋‹ค. (์›๋ž˜ ๊ฐ’๊ณผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋Š”) pointCoordinates์˜ ๊ฒฝ์šฐ s๋Š” t์™€ ๊ฐ™๊ณ  a๋Š” b์™€ ๊ฐ™๋‹ค. pointCoordinates๋Š” ์ˆœํšŒํ•œ ๊ตฌ์กฐ๋‚˜ ๊ทธ ์•ˆ์˜ ๋Œ€์ƒ์˜ ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ˜๋“œ์‹œ ๊ทธ๋Ÿด ํ•„์š”๋Š” ์—†๋‹ค. ๊ทธ ์˜ˆ๊ฐ€ traverse๋กœ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํƒ€์ž…์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. Traversable t => Traversal (t a) (t b) a b traverse๋Š” Traversable ๊ตฌ์กฐ ๋‚ด์˜ ๋ชฉํ‘ฏ๊ฐ’์˜ ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ณ , ๋” ๋‚˜์•„๊ฐ€, ๊ทธ ๊ตฌ์กฐ์˜ ํƒ€์ž… ์ž์ฒด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. Control.Lens.Traversal ๋ชจ๋“ˆ์€ Data.Traversable ํ•จ์ˆ˜๋“ค์˜ ์ผ๋ฐ˜ํ™” ๊ทธ๋ฆฌ๊ณ  ๋‹ค์–‘ํ•œ ์ˆœํšŒ ๊ด€๋ จ ๋„๊ตฌ๋“ค์„ ํฌํ•จํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ Segment๋ฅผ ์ •์˜ํ•˜๋Š” ์ ๋“ค์˜ ๋ชจ๋“  ์ขŒํ‘œ๋ฅผ ๋ฐ์ดํ„ฐ ์„ ์–ธ์— ํ‘œํ˜„๋œ ์ˆœ์„œ๋Œ€๋กœ ์ˆœํšŒํ•˜๋Š” extremityCoordinates๋ฅผ ์ž‘์„ฑํ•˜๋ผ. (ํžŒํŠธ: pointCoordinates ์ˆœํšŒ๋ฅผ ์ด์šฉํ•˜๋ผ) ์„ธํ„ฐ(setter) ๋‹ค์Œ ๊ณ„ํš์€ Traversable, Functor, Foldable ์‚ฌ์ด์˜ ์—ฐ๊ฒฐ์„ ์ผ๋ฐ˜ํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Functor๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ž. traverse๋กœ๋ถ€ํ„ฐ fmap์„ ๋ณต๊ตฌํ•˜๊ธฐ ์œ„ํ•ด Identity๋ฅผ ์ ์šฉ์„ฑ ํŽ‘ํ„ฐ๋กœ ์„ ํƒํ–ˆ์—ˆ๋‹ค. ๊ทธ๋Ÿผ์œผ๋กœ์จ ๋ถ€์ˆ˜ ํšจ๊ณผ ์—†์ด ๋ชฉํ‘ฏ๊ฐ’์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๋น„์Šทํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด Traversal์˜ ์ •์˜๋ฅผ ์„ ํƒํ•˜๊ณ ... forall f. Applicative f => (a -> f b) -> s -> f t ... f๋ฅผ Identity๋กœ ํ•œ์ •ํ•œ๋‹ค. (a -> Identity b) -> s -> Identity t lens์˜ ์šฉ๋ฒ•์—์„œ๋Š” ์ด๊ฒƒ์ด Setter๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ธฐ์ˆ ์ ์ธ ์ด์œ ๋กœ Control.Lens.Setter์˜ Setter ์ •์˜๋Š” ์ด๊ฒƒ๊ณผ ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค. type Setter s t a b = forall f. Settable f => (a -> f b) -> s -> f t ํ•˜์ง€๋งŒ ๋ฌธ์„œ๋ฅผ ๋“ค์—ฌ๋‹ค๋ณด๋ฉด Settable ํŽ‘ํ„ฐ๊ฐ€ Identity ๋˜๋Š” ์ด์™€ ์ƒ๋‹นํžˆ ๋น„์Šทํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๋ณ„๋กœ ์‹ ๊ฒฝ ์“ธ ์ฐจ์ด๋Š” ์•„๋‹ˆ๋‹ค. Traversal์—์„œ f๋ฅผ ์–ด๋–ค ๊ฒƒ์œผ๋กœ ์ œํ•œํ•˜๋ฉด ํƒ€์ž…์€ ์‚ฌ์‹ค ๋” ์ผ๋ฐ˜ํ™”๋œ๋‹ค. Traversal์ด ๋ชจ๋“  Applicative ํŽ‘ํ„ฐ์™€ ์ž‘๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Identity์™€ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋“  Traversal์€ Setter์ด๋ฉฐ Setter๋กœ์„œ ์“ฐ์ผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ์—ญ์€ ์•„๋‹ˆ๋‹ค. ๋ชจ๋“  ์„ธํ„ฐ๊ฐ€ ์ˆœํšŒ๋Š” ์•„๋‹ˆ๋‹ค. over๋Š” ์„ธํ„ฐ๋ฅผ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ํ•ฉ์„ฑ ์ž๋‹ค. fmap์ฒ˜๋Ÿผ ์ž‘๋™ํ•˜๋Š”๋ฐ, ๊ตฌ์กฐ์˜ ์–ด๋–ค ๋ถ€๋ถ„์ด ๋Œ€์ƒ์ธ์ง€ ๋ช…์‹œํ•˜๊ธฐ ์œ„ํ•ด ์„ธํ„ฐ๋ฅผ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋„˜๊ธด๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. GHCi> over pointCoordinates negate (makePoint (1, 2)) Point {_positionX = -1.0, _positionY = -2.0} ์‚ฌ์‹ค์€ mapped๋ผ๋Š” Setter๋กœ fmap์„ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. GHCi> over mapped negate [1.. 4] [-1, -2, -3, -4] GHCi> over mapped negate (Just 3) Just (-3) set์€ ๋˜ ๋‹ค๋ฅธ ์ค‘์š”ํ•œ ํ•ฉ์„ฑ์ž๋กœ์„œ, ๋ชจ๋“  ๋ชฉํ‘ฏ๊ฐ’์„ ์ƒ์ˆ˜๋กœ ๋Œ€์ฒดํ•œ๋‹ค. set setter x = over setter (const x),๋Š” (x <$) = fmap (const x) ์™€ ๋น„์Šทํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. GHCi> set pointCoordinates 7 (makePoint (1, 2)) Point {_positionX = 7.0, _positionY = 7.0} ์—ฐ์Šต๋ฌธ์ œ over๋ฅผ ์ด์šฉํ•ด ๋‹ค์Œ์„ ๊ตฌํ˜„ํ•˜๋ผ. scaleSegment :: Double -> Segment -> Segment scaleSegment n์ด ์„ ๋ถ„์˜ ๋ชจ๋“  ์ขŒํ‘œ์— n์„ ๊ณฑํ•˜๋„๋ก ํ•œ๋‹ค. (ํžŒํŠธ: ์ด์ „ ์—ฐ์Šต๋ฌธ์ œ์˜ ๋‹ต์„ ํ™œ์šฉํ•  ๊ฒƒ) mapped๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. ์ด ์—ฐ์Šต๋ฌธ์ œ์—์„œ๋Š” Settable ํŽ‘ํ„ฐ๋ฅผ Identity๋กœ ํ•œ์ •ํ•ด๋„ ๋œ๋‹ค. (ํžŒํŠธ: Data.Functor.Identity๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค) ์ ‘๊ธฐ ์ˆœํšŒ๋กœ์„œ์˜ fmap ๊ธฐ๋ฒ•์„ ์ผ๋ฐ˜ํ™”ํ–ˆ์œผ๋‹ˆ ์ˆœํšŒ๋กœ์„œ์˜ foldMap์„ ์ผ๋ฐ˜ํ™”ํ•  ์ฐจ๋ก€๋‹ค. ์ด๊ฒƒ์„ forall f. Applicative f => (a -> f b) -> s -> f t Const๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊พผ๋‹ค. forall r. Monoid r => (a -> Const r a) -> s -> Const r s Const์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋ฌด๊ด€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— b๋ฅผ a๋กœ, t๋ฅผ s๋กœ ๋ฐ”๊พผ๋‹ค. Setter์™€ Identity์—์„œ ๋ดค๋“ฏ์ด Conrol.Lens.Fold๋Š” Monoid r => Const r๋ณด๋‹ค ์กฐ๊ธˆ ์ผ๋ฐ˜ํ™”๋œ ๊ฒƒ์„ ์‚ฌ์šฉํ•œ๋‹ค. type Fold s a = forall f. (Contravariant f, Applicative f) => (a -> f a) -> s -> f s ๋…ธํŠธ Contravariant๋Š” contravariant functor๋ฅผ ์œ„ํ•œ ํƒ€์ž… ํด๋ž˜์Šค๋‹ค. Contravariant์˜ ํ•ต์‹ฌ ๋ฉ”์„œ๋“œ๋Š” contramap์ด๋‹ค. contramap :: Contravariant f => (a -> b) -> f b -> f a ์ƒ๊ธด ๊ฒƒ์€ fmap๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ, ๋ญ๋ผ ํ•ด์•ผ ํ• ๊นŒ, ์ด๊ฒƒ์€ ํ•จ์ˆ˜ ์• ๋กœ๋ฅผ ์‚ฌ์ƒ ์ฃผ๋ณ€์—์„œ ํšŒ์ „์‹œํ‚จ๋‹ค. (turns the function arrow around on mapping.) ํ•จ์ˆ˜ ์ธ์ž์— ๋Œ€ํ•ด ๋งค๊ฐœํ™”๋œ ํƒ€์ž…์€ Contravariant์˜ ์ „ํ˜•์ ์ธ ์˜ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Data.Functor.Contravariant๋Š” Predicate ํƒ€์ž…์„ ์ •์˜ํ•˜๋Š”๋ฐ, a ํƒ€์ž…์˜ ๊ฐ’์— ๋Œ€ํ•œ ๋ถˆ๋ฆฌ์–ธ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. newtype Predicate a = Predicate { getPredicate :: a -> Bool } GHCi> :m +Data.Functor.Contravariant GHCi> let largerThanFour = Predicate (> 4) GHCi> getPredicate largerThanFour 6 True Predicate์€ Contravariant์ด๋ฏ€๋กœ contrmap์œผ๋กœ Predicate์„ ์ˆ˜์ •ํ•˜์—ฌ, ํ…Œ์ŠคํŠธ ์ „์— ๊ฐ’์„ ์กฐ์ •ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค. GHCi> getPredicate (contramap length largerThanFour) "orange" True Contravariant์˜ ๋ฒ•์น™๋“ค์€ Functor์˜ ๋ฒ•์น™๋“ค๊ณผ ๋น„์Šทํ•˜๋‹ค. contramap id = id contramap (g . f) = contramap f. contramap g Monoid r => Const r์€ Contravariant์ด๋ฉด์„œ Applicative์ด๋‹ค. ํŽ‘ ํ„ฐ ๋ฒ•์น™๊ณผ ๋ฐ˜๋ณ€ ๋ฒ•์น™ ๋•์— Contravariant์ด๋ฉด์„œ Functor์ธ ๊ฒƒ์€ ๋งˆ์น˜ Const r์ฒ˜๋Ÿผ ๊ณตํŽ‘ํ„ฐ(vacuous functor)๊ฐ€ ๋˜์–ด, fmap๊ณผ contramap์ด ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ถ”๊ฐ€์ ์ธ Applicative ์ œ์•ฝ์€ Monoid r๊ณผ ๋Œ€์‘ํ•œ๋‹ค. ์ด๋กœ๋ถ€ํ„ฐ ๋Œ€์ƒ์œผ๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ Const ๋ฅ˜ ๋ฌธ๋งฅ๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ ์ ‘๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. ๋ชจ๋“  Traversal์€ Fold๋กœ์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ, Traversal์€ ์–ด๋–ค Applicative์™€๋„ ์ž‘๋™ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์—ฌ๊ธฐ์—๋Š” Contravariant์ธ ๊ฒƒ๋„ ํฌํ•จํ•œ๋‹ค. Traversal๊ณผ Setter์—์„œ ๋ณธ ์ƒํ™ฉ๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•œ๋‹ค. Control.Lens.Fold๋Š” Data.Foldable์— ์žˆ๋Š” ๋ชจ๋“  ๊ฒƒ์— ๋Œ€ํ•ด ์œ ์‚ฌํ’ˆ์„ ์ œ๊ณตํ•œ๋‹ค. ์ด ๋ชจ๋“ˆ์—์„œ ์ž์ฃผ ์“ฐ์ด๋Š” ๋‘ ํ•ฉ์„ฑ์ž๊ฐ€ ์žˆ๋Š”๋ฐ Fold ๋Œ€์ƒ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” toListOf์™€, GHCi> -- Using the solution to the exercise in the traversals subsection. GHCi> toListOf extremityCoordinates (makeSegment (0, 1) (2, 3)) [0.0,1.0,2.0,3.0] Data.Monoid์˜ First ๋ชจ ๋…ธ์ด๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ Fold์˜ ์ฒซ ๋Œ€์ƒ์„ ์ถ”์ถœํ•˜๋Š” preview์ด๋‹ค. GHCi> preview traverse [1.. 10] Just 1 Getter ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ˆœํšŒ์— ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŽ‘ํ„ฐ๋“ค์„ ์ œํ•œํ•˜์—ฌ Traversal์œผ๋กœ๋ถ€ํ„ฐ ๋” ์ผ๋ฐ˜ํ™”๋œ optics(Setter์™€ Fold)๋กœ ์˜ฎ๊ฒจ๊ฐ”๋Š”๋ฐ, ๊ทธ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰ ํŽ‘ํ„ฐ๊ฐ€ ๋‹ค๋ค„์•ผ ํ•˜๋Š” ๋ฒ”์œ„๋ฅผ ๋„“ํ˜€์„œ ๋” ๊ตฌ์ฒด์ ์ธ optics๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Fold์˜ ๊ฒฝ์šฐ... type Fold s a = forall f. (Contravariant f, Applicative f) => (a -> f a) -> s -> f s Applicative ์ œํ•œ์„ Functor๋กœ ์™„ํ™”ํ•˜๋ฉด Getter๋ฅผ ์–ป๋Š”๋‹ค. type Getter s a = forall f. (Contravariant f, Functor f) => (a -> f a) -> s -> f s f๋Š” ์—ฌ์ „ํžˆ Contravariant์ด ์ž Functor์ด์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, Const ๊ฐ™์€ ๊ณตํŽ‘ํ„ฐ(vacuous functor)๋กœ ๋‚จ๋Š”๋‹ค. ํ•˜์ง€๋งŒ Applicative ์ œ์•ฝ ์—†์ด๋Š” ๋ณต์ˆ˜ ๋Œ€์ƒ์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ•ฉ์„ฑํ•  ์ˆ˜ ์—†๋‹ค. ๊ฒฐ๊ตญ Getter๋Š” ํ•ญ์ƒ ํ•œ ๋Œ€์ƒ์„ ๊ฐ€์ง„๋‹ค. ๋Œ€์ƒ์„ ์–ผ๋งˆ๋“ ์ง€, 0๊ฐœ๋„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” Fold(๋˜๋Š” Setter๋‚˜ Traversal)๊ณผ ๋Œ€๋น„๋˜๋Š” ์ ์ด๋‹ค. f๋ฅผ Const r๋กœ ํ•œ์ •ํ•˜๋ฉด Getter์˜ ๋ณธ์งˆ์„ ๋“œ๋Ÿฌ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. someGetter :: (a -> Const r a) -> s -> Const r s Const r whatever ๊ฐ’๊ณผ r ๊ฐ’์€ ์„œ๋กœ ๋ฌด์†์‹ค ๋ณ€ํ™˜์ด๋ฏ€๋กœ ์œ„์˜ ํƒ€์ž…์€ ๋‹ค์Œ๊ณผ ๋™์น˜๋‹ค. someGetter'' :: s -> a someGetter'' x = someGetter' id x someGetter' k x = k (someGetter'' x) ๋”ฐ๋ผ์„œ Getter s a๊ฐ€ ํ•จ์ˆ˜ s -> a์™€ ๋™์น˜๋ผ๊ณ  ๊ฒฐ๋ก ์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ด€์ ์—์„œ๋Š” ๋Œ€์ƒ์„ ์˜ค์ง ํ•˜๋‚˜๋งŒ ์ทจํ•ด ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋†“๋Š” ๊ฒƒ์ด ์ž์—ฐ์Šค๋Ÿฝ๋‹ค. Control.Lens.Getter์˜ ๋‘ ๊ธฐ๋ณธ์ ์ธ ํ•ฉ์„ฑ์ž๊ฐ€ ์ž„์˜ ํ•จ์ˆ˜์˜ Getter๋ฅผ ๋งŒ๋“œ๋Š” to์™€ Getter๋ฅผ ์ž„์˜ ํ•จ์ˆ˜๋กœ ๋˜๋Œ๋ฆฌ๋Š” view๋ผ๋Š” ๊ฑด ๋†€๋ž์ง€ ์•Š์€ ์ผ์ด๋‹ค. GHCi> -- The same as fst (4, 1) GHCi> view (to fst) (4, 1) ๋…ธํŠธ Getter๊ฐ€ Fold๋ณด๋‹ค ๋œ ์ผ๋ฐ˜์ ์ด๋ผ๊ณ  ํ–ˆ์ง€๋งŒ, view๋Š” ์‚ฌ์‹ค Getter๋ฟ ์•„๋‹ˆ๋ผ Fold, Traversal์—๋„ ์ž‘๋™ํ•œ๋‹ค. GHCi> :m +Data.Monoid GHCi> view traverse (fmap Sum [1.. 10]) Sum {getSum = 55} GHCi> -- both traverses the components of a pair. GHCi> view both ([1,2],[3,4,5]) [1,2,3,4,5] ์ด๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” lens์˜ ํƒ€์ž… ๋ช…์„ธ์— ์žˆ๋Š” ๋ฌ˜ํ•œ ๊ตฌ์„ ๋•Œ๋ฌธ์ด๋‹ค. view์˜ ์ฒซ ์ธ์ž๋Š” ์ •ํ™•ํžˆ๋Š” Getter๊ฐ€ ์•„๋‹ˆ๋ผ Getting์ด๋‹ค. type Getting r s a = (a -> Const r a) -> s -> Const r s view :: MonadReader s m => Getting a s a -> m a Getting์€ ํŽ‘ ํ„ฐ ์ธ์ž๋ฅผ Const r๋กœ ํ•œ์ •ํ•˜๋ฉฐ, ์ด๋Š” Getter๋ฅผ ์œ„ํ•œ ๋ช…๋ฐฑํ•œ ์„ ํƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ด์— ๋Œ€ํ•ด Applicative ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ๋Š”์ง€๋Š”(์ฆ‰, r์ด Monoid ์ธ์ง€๋Š”) ๋ฌผ์Œํ‘œ๋กœ ๋‚จ๊ฒจ๋‘”๋‹ค. view๋ฅผ ์˜ˆ๋กœ ๋“ค๋ฉด a๊ฐ€ Monoid์ธ ํ•œ, Getting a s a๋Š” Fold๋กœ ์“ธ ์ˆ˜ ์žˆ๊ณ , Fold๋Š” ์ ‘๊ธฐ ๋Œ€์ƒ์ด ๋ชจ ๋…ธ์ด๋“œ ๋ฉด view์— ์“ธ ์ˆ˜ ์žˆ๋‹ค. Control.Lens.Getter์™€ Control.Lens.Fold์˜ ๋งŽ์€ ํ•ฉ์„ฑ ์ž๋Š” Getter๋‚˜ Fold๊ฐ€ ์•„๋‹ˆ๋ผ Getting์„ ์ด์šฉํ•˜์—ฌ ์ •์˜๋œ๋‹ค. Getting์„ ์“ฐ๋Š” ๊ฒƒ์˜ ์ด์ ์€ ๊ทธ ๊ฒฐ๊ณผ ๋‚˜์˜ค๋Š” ํƒ€์ž… ๋ช…์„ธ๋ฅผ ๋ณด๋ฉด ์ˆ˜ํ–‰๋˜๋Š” fold์— ๋Œ€ํ•ด ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Control.Lens.Fold์˜ hasn't๋ฅผ ์‚ดํŽด๋ณด๋ฉด, hasn't :: Getting All s a -> s -> Bool ์ด๊ฒƒ์€ ๋น„์–ด์žˆ์Œ์— ๋Œ€ํ•œ ์ผ๋ฐ˜ํ™”๋œ ๊ฒ€์‚ฌ๋‹ค. GHCi> hasn't traverse [1.. 4] False GHCi> hasn't traverse Nothing True Fold s a -> s -> Bool๋„ hasn't์˜ ๋ช…์„ธ๋กœ์„œ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ ๋ช…์„ธ์˜ Getting All์€ ๋งŽ์€ ๊ฒƒ์„ ๋งํ•ด์ฃผ๋Š”๋ฐ, ํŠนํžˆ hasn't๊ฐ€ ํ•˜๋Š” ์ผ์„ ๋šœ๋ ท์ด ๋ณด์—ฌ์ค€๋‹ค. hasn't๋Š” s ์•ˆ์˜ ๋ชจ๋“  a ๋Œ€์ƒ์„ All ๋ชจ๋…ธ์ด๋“œ๋กœ(๋” ์ •ํ™•ํžˆ๋Š”, All False๋กœ) ๋ณ€ํ™˜ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฒƒ๋“ค์„ ์ ‘์–ด์„œ ๋‚˜์˜จ ๊ฒฐ๊ณผ All๋กœ๋ถ€ํ„ฐ Bool์„ ์ถ”์ถœํ•œ๋‹ค. ๋งˆ์ง€๋ง‰ ๋ Œ์ฆˆ Traversal๋กœ ๋˜๋Œ์•„๊ฐ€ ๋ณด์ž. type Traversal s t a b = forall f. Applicative f => (a -> f b) -> s -> f t Fold์—์„œ Getter๋กœ ์˜ฎ๊ฒจ๊ฐˆ ๋•Œ ํ–ˆ๋“ฏ์ด Applicative ์ œ์•ฝ์„ Functor๋กœ ์™„ํ™”ํ•˜๋ฉด type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t ์ตœ์ข…์ ์œผ๋กœ Lens ํƒ€์ž…์„ ์–ป๋Š”๋‹ค. Traversal์—์„œ Lens๋กœ ์˜ฎ๊ฒจ๊ฐˆ ๋•Œ ๋ฌด์—‡์ด ๋ฐ”๋€Œ๋Š”๊ฐ€? ์•ž์„œ ๋ดค๋“ฏ์ด Applicative ์ œ์•ฝ์„ ์™„ํ™”ํ•˜๋ฉด ๋ณต์ˆ˜ ๋Œ€์ƒ์„ ์ˆœํšŒํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค. Traversal๊ณผ ๋‹ฌ๋ฆฌ Lens๋Š” ํ•ญ์ƒ ๋‹จ์ผ ๋Œ€์ƒ์— ์ดˆ์ ์„ ๋งž์ถ˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฐ ์ œ์•ฝ์— ์ข‹์€ ์ ๋„ ์žˆ๋Š” ๋ฒ•์ด๋‹ค. Lens์—์„œ๋Š” ์ •ํ™•ํžˆ ํ•œ ๋Œ€์ƒ์„ ์ฐพ์„ ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค. ๋ฐ˜๋ฉด Traversal์—์„œ๋Š” ๋Œ€์ƒ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ด๊ฑฐ๋‚˜ ์•„์˜ˆ ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค. Applicative ์ œ์•ฝ์˜ ๋ถ€์žฌ์™€ ๋Œ€์ƒ์˜ ์œ ์ผํ•จ์€ ๋ Œ์ฆˆ๋“ค์˜ ๋˜ ๋‹ค๋ฅธ ํ•ต์‹ฌ์„ ๋“œ๋Ÿฌ๋‚ธ๋‹ค. ๋ Œ์ฆˆ๋Š” getter๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค. Contravariant ๋”ํ•˜๊ธฐ Functor๋Š” ๋‹จ์ˆœ Functor๋ณด๋‹ค ๊ตฌ์ฒด์ ์ธ ์ œ์•ฝ์ด๊ณ , ๋”ฐ๋ผ์„œ Getter๋Š” Lens๋ณด๋‹ค ์ผ๋ฐ˜์ ์ด๋‹ค. ๋˜ํ•œ ๋ชจ๋“  Lens๋Š” Traversal์ด๋ฉฐ ๋”ฐ๋ผ์„œ Setter์ด๋‹ค. ๊ฒฐ๋ก ์€ ๋ Œ์ฆˆ๊ฐ€ getter๋กœ๋„, setter๋กœ๋„ ์“ฐ์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์ด ๋ Œ์ฆˆ๋กœ ๋ ˆ์ฝ”๋“œ ๋ ˆ์ด๋ธ”์„ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋‹ค. ๋…ธํŠธ ๋งˆ๋ฌด๋ฆฌํ•˜๊ธฐ ์ „์—, ๋ชจ๋“  Lens๋Š” Getter๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ฃผ์žฅ์€ ์กฐ๊ธˆ ์„ฑ๊ธ‰ํ•ด ๋ณด์ธ๋‹ค. ๋‘ ํƒ€์ž…์„ ๋‚˜๋ž€ํžˆ ๋†“์œผ๋ฉด type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t type Getter s a = forall f. (Contravariant f, Functor f) => (a -> f a) -> s -> f s Lens s t a b์—์„œ Getter s a๋กœ ์˜ฎ๊ธธ ๋•Œ s๋Š” t์™€, a๋Š” b์™€ ๊ฐ™์•„์ง„๋‹ค. ๋ชจ๋“  ๋ Œ์ฆˆ์—์„œ ์ด๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ์–ด๋–ป๊ฒŒ ํ™•์‹ ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€? Traversal๊ณผ Fold์˜ ๊ด€๊ณ„์—์„œ๋„ ๋น„์Šทํ•œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ์ง€๊ธˆ์€ ๊ทธ ๋Œ€๋‹ต์„ ๋ฏธ๋ค„๋‘๊ณ , optic ๋ฒ•์น™์— ๋Œ€ํ•œ ์ ˆ์—์„œ ๋Œ์•„์˜ฌ ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ์€ ํŠœํ”Œ์˜ ์ฒซ ์„ฑ๋ถ„์„ ๊ฐ€๋ฆฌํ‚ค๋Š” _1์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ Œ์ฆˆ์˜ ์œ ์—ฐํ•จ์„ ์„ค๋ช…ํ•˜๋Š” ์งง์€ ์˜ˆ์ œ๋‹ค. GHCi> _1 (\x -> [0.. x]) (4, 1) -- Traversal [(0,1),(1,1),(2,1),(3,1),(4,1)] GHCi> set _1 7 (4, 1) -- Setter (7,1) GHCi> over _1 length ("orange", 1) -- Setter, changing the types (6,1) GHCi> toListOf _1 (4, 1) -- Fold [4] GHCi> view _1 (4, 1) -- Getter ์—ฐ์Šต๋ฌธ์ œ Point์™€ Segment์˜ ํ•„๋“œ๋“ค์— ๋Œ€ํ•œ ๋ Œ์ฆˆ, ์ฆ‰ makeLenses๋กœ ์ƒ์„ฑํ–ˆ๋˜ ๊ฒƒ๋“ค์„ ๊ตฌํ˜„ํ•˜๋ผ. (ํžŒํŠธ: ํƒ€์ž…์„ ๋”ฐ๋ผ๊ฐˆ ๊ฒƒ. ํƒ€์ž… ๋ช…์„ธ๋ฅผ ์“ฐ๊ณ  ๋‚˜๋ฉด fmap๊ณผ ๋ ˆ์ฝ”๋“œ ๋ ˆ์ด๋ธ” ๋ง๊ณ  ์“ธ ๊ฒƒ์ด ๋ณ„๋กœ ์—†๋‹ค.) getter ํ•จ์ˆ˜ s -> a์™€ setter ํ•จ์ˆ˜ s -> b -> t๋ฅผ ์ทจํ•ด Lens s t a b๋ฅผ ์ƒ์„ฑํ•˜๋Š” lens ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. (ํžŒํŠธ: ์—ฌ๋Ÿฌ๋ถ„์˜ ๊ตฌํ˜„์ด ์ด์ „ ์—ฐ์Šต๋ฌธ์ œ ํ•ด๋‹ต์—์„œ ๋ฐ˜๋ณต๋˜๋Š” ๋ถ€๋ถ„์„ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค) ํ•ฉ์„ฑ(composition) ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ optic๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชจ์–‘์ด์—ˆ๋‹ค. (a -> f b) -> (s -> f t) ์—ฌ๊ธฐ์„œ f๋Š” ์ผ์ข…์˜ Functor์ด๋‹ค. s๋Š” optic์ด ์ž‘๋™ํ•˜๋Š” ์ „์ฒด ๊ตฌ์กฐ์˜ ํƒ€์ž…์ด๋‹ค. t๋Š” optic์„ ํ†ตํ•ด ๋˜๋Š” ๋ฌด์–ธ๊ฐ€์˜ ์ „์ฒด ํƒ€์ž…์ด๋‹ค. a๋Š” ๋ถ€๋ถ„๋“ค ์ฆ‰ optic์ด ๊ฐ€๋ฆฌํ‚ค๋Š” s ๋‚ด์˜ ๋Œ€์ƒ๋“ค์˜ ํƒ€์ž…์ด๋‹ค. b๋Š” ๊ทธ ๋ถ€๋ถ„๋“ค์ด optic์„ ํ†ตํ•ด ๋˜๋Š” ๋ฌด์–ธ๊ฐ€์˜ ํƒ€์ž…์ด๋‹ค. ์ด optic๋“ค์˜ ํ•ต์‹ฌ์ ์ธ ๊ณตํ†ต์ ์€ ๋ชจ๋‘ ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ๋Š”, ๋ถ€๋ถ„ (a -> f b)์— ์ž‘์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ „์ฒด์ธ (s -> f t)์— ์ž‘์šฉํ•˜๋Š” ํ•จ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋งคํ•‘ ํ•จ์ˆ˜๋‹ค. ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ฉ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋งจ ์ฒ˜์Œ์˜ ๋ Œ์ฆˆ ํ•ฉ์„ฑ ์˜ˆ์ œ๋ฅผ ๋‹ค์‹œ ๋ณด์ž. GHCi> let testSeg = makeSegment (0, 1) (2, 4) GHCi> view (segmentEnd . positionY) testSeg GHCi> 4.0 optic์€ ๋„˜๊ฒจ๋ฐ›์€ ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์ธ์ž๋กœ ๋งŒ๋“ฆ์œผ๋กœ์จ ๋” ํฐ ๊ตฌ์กฐ์— ์ž‘์šฉํ•˜๋„๋ก ํ•œ๋‹ค. (.)์ด ํ•จ์ˆ˜๋ฅผ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ํ•ฉ์„ฑํ•˜๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ฝ์„ ๋•Œ (.)๋กœ ํ•ฉ์„ฑ๋œ optic์˜ ๊ตฌ์„ฑ์š”์†Œ๋“ค์€ ์ ์ฐจ ์›๋ž˜ ๊ตฌ์กฐ์˜ ์ž‘์€ ๋ถ€๋ถ„๋“ค์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค. lens ํƒ€์ž… ์œ ์‚ฌํ˜•์— ์‚ฌ์šฉ๋˜๋Š” ๊ด€์Šต์€ ์ด๋Ÿฌํ•œ ํฐ ๊ฒƒ์—์„œ ์ž‘์€ ๊ฒƒ ์ˆœ์„œ์™€ ์ผ์น˜ํ•˜์—ฌ, s์™€ t๊ฐ€ a์™€ b๋ณด๋‹ค ๋จผ์ € ์˜จ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋Š” segmentEnd . positionY๋ฅผ ์˜ˆ์‹œ๋กœ, optic์ด ํ•˜๋Š” ์ผ์„ ์–ด๋–ป๊ฒŒ ๋งคํ•‘(์ž‘์€ ๊ฒƒ์—์„œ ํฐ ๊ฒƒ) ๋˜๋Š” ํฌ์ปค์‹ฑ(ํฐ ๊ฒƒ์—์„œ ์ž‘์€ ๊ฒƒ)์œผ๋กœ ๋ณด๋Š”์ง€ ์„ค๋ช…ํ•œ๋‹ค. ๋ Œ์ฆˆ segmentEnd positionY segmentEnd . positionY ํ’€์–ด์“ด ํƒ€์ž… Functor f => (Point -> f Point) -> (Segment -> f Segment) Functor f => (Double -> f Double) -> (Point -> f Point) Functor f => (Double -> f Double) -> (Segment -> f Segment) "๋งคํ•‘" ํ•ด์„ Point์˜ ํ•จ์ˆ˜์—์„œ Segment์˜ ํ•จ์ˆ˜๋กœ Double์˜ ํ•จ์ˆ˜์—์„œ Point์˜ ํ•จ์ˆ˜๋กœ Double์˜ ํ•จ์ˆ˜์—์„œ Segment์˜ ํ•จ์ˆ˜๋กœ Lens๋กœ ํ‘œํ˜„ํ•œ ํƒ€์ž… Lens Segment Segment Point Point Lens Point Point Double Double Lens Segment Segment Double Double Lens'๋กœ ํ‘œํ˜„ํ•œ ํƒ€์ž… Lens' Segment Point Lens' Point Double Lens' Segment Double "ํฌ์ปค์‹ฑ" ํ•ด์„ Segment ๋‚ด์˜ Point๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค Point ๋‚ด์˜ Double์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค Segment ๋‚ด์˜ Double์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค ๋…ธํŠธ Lens' ์œ ์‚ฌํ˜•์€ ๊ทธ์ € ํƒ€์ž…์„ ๋ฐ”๊พธ์ง€ ์•Š๋Š” ๋ Œ์ฆˆ์˜ ์ค„์ž„๋ง์ด๋‹ค. (์ฆ‰ s๊ฐ€ t์™€, a๊ฐ€ b์™€ ๊ฐ™์€ ๋ Œ์ฆˆ๋‹ค) type Lens' s a = Lens s s a a ๋น„์Šทํ•˜๊ฒŒ Traversal'๊ณผ Setter'๋„ ์žˆ๋‹ค. Lens์™€ Traversal ๊ฐ™์€ synonym๋“ค ๋’ค์— ์žˆ๋Š” ํƒ€์ž…๋“ค ๊ฐ„์˜ ์œ ์ผํ•œ ์ฐจ์ด๋Š” f์˜ ์ž๋ฆฌ์— ์–ด๋–ค ํŽ‘ํ„ฐ๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋Š”๊ฐ€ ๋ฟ์ด๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์„œ๋กœ ์ข…์ด ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ optic๋“ค์„, ๋ชจ๋‘๊ฐ€ ๋“ค์–ด๋งž๋Š” ํƒ€์ž…๋งŒ ์žˆ๋‹ค๋ฉด ์ž์œ ๋กœ์ด ์„ž์„ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๋‹ค. GHCi> -- A Traversal on a Lens is a Traversal. GHCi> (_2. traverse) (\x -> [-x, x]) ("foo", [1,2]) [("foo",[-1, -2]),("foo",[-1,2]),("foo",[1, -2]),("foo",[1,2])] GHCi> -- A Getter on a Lens is a Getter. GHCi> view (positionX . to negate) (makePoint (2,4)) -2.0 GHCi> -- A Getter on a Traversal is a Fold. GHCi> toListOf (both . to negate) (2, -3) [-2,3] GHCi> -- A Getter on a Setter does not exist (there is no unifying optic). GHCi> set (mapped . to length) 3 ["orange", "apple"] <interactive>:49:15: No instance for (Contravariant Identity) arising from a use of โ€˜toโ€™ In the second argument of โ€˜(.)โ€™, namely โ€˜to lengthโ€™ In the first argument of โ€˜setโ€™, namely โ€˜(mapped . to length)โ€™ In the expression: set (mapped . to length) 3 ["orange", "apple"] ์—ฐ์‚ฐ์ž ๋ช‡ ๊ฐ€์ง€ ๋ Œ์ฆˆ ํ•ฉ์„ฑ์ž์—๋Š” ์ค‘์œ„ ์—ฐ์‚ฐ์ž ๋™์˜์–ด ๋˜๋Š” ์ตœ์†Œํ•œ ๊ฑฐ์˜ ๋™๋“ฑํ•œ ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ๋‹ค. ๋‹ค์Œ์€ ์šฐ๋ฆฌ๊ฐ€ ์‚ดํŽด๋ณธ ๋ช‡ ํ•ฉ์„ฑ์ž์™€ ๊ทธ์— ๋Œ€์‘ํ•˜๋Š” ์—ฐ์‚ฐ์ž๋“ค์ด๋‹ค. ์ „์œ„ ์ค‘์œ„ view _1 (1,2) (1,2) ^. _1 set _1 7 (1,2) (_1 .~ 7) (1,2) over _1 (2 *) (1,2) (_1 %~ (2 *)) (1,2) toListOf traverse [1.. 4] [1.. 4] ^.. traverse preview traverse [] [] ^? traverse ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๋ Œ์ฆˆ ์—ฐ์‚ฐ์ž๋“ค(๊ฐ€๋ น (^.), (^..), (^?))์€ ๋Œ€์‘ํ•˜๋Š” ์ „์œ„ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด ์ธ์ž๊ฐ€ ๋’ค์ง‘ํ˜€์žˆ์–ด์„œ, ๊ฒฐ๊ณผ๋ฅผ ์ถ”์ถœํ•  ๊ตฌ์กฐ์ฒด๋ฅผ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค. ์ด๋Ÿฌ๋ฉด ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง€๋Š”๋ฐ, optic์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ตฌ์กฐ ์ „์ฒด๋ฅผ optic์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ถ€๋ถ„๋ณด๋‹ค ๋จผ์ € ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๊ณ , ์ด๋Š” ํ•ฉ์„ฑ optic์ด ํฐ ๊ฒƒ์—์„œ ์ž‘์€ ๊ฒƒ ์ˆœ์œผ๋กœ ์ž‘์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ฐ˜์˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (&) ์—ฐ์‚ฐ์ž๋Š” ๋‹จ์ˆœํžˆ flip ($)๋กœ ์ •์˜๋˜๋ฉฐ, ์ด๊ฒƒ ๋•์— ์ˆ˜์ • ์—ฐ์‚ฐ์ž(๊ฐ€๋ น (.~)๋‚˜ (%~))๋ฅผ ์“ธ ๋•Œ๋„ ๊ตฌ์กฐ๋ฅผ ๋จผ์ € ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. (&)๋Š” ์ˆ˜์ •ํ•  ํ•„๋“œ๊ฐ€ ๋งŽ์„ ๋•Œ ํŠนํžˆ ํŽธํ•˜๋‹ค. sextupleTest = (0,1,0,1,0,1) & _1 .~ 7 & _2 %~ (5 *) & _3 .~ (-1) & _4 .~ "orange" & _5 %~ (2 +) & _6 %~ (3 *) GHCi> sextupleTest (7,5, -1, "orange",2,3) ๋งŒ๋Šฅ์นผ ์ด๋งŒํผ ํ–ˆ์œผ๋ฉด ๋ Œ์ฆˆ๊ฐ€ ๋ถˆ๊ฐ€์‚ฌ์˜ํ•œ ๋งˆ์ˆ ์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๊ธฐ์— ์ถฉ๋ถ„ํ•œ ๊ฒƒ ๊ฐ™๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฒƒ์€ ๋น™์‚ฐ์˜ ์ผ๊ฐ์ด๋‹ค. lens๋Š” ํ’๋ถ€ํ•œ ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ๋‹ค์ฑ„๋กœ์šด ๊ฐœ๋…์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฑฐ๋Œ€ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. ๊ณค๋ž€ํ•œ ์ ์€ ์—ฌ๋Ÿฌ๋ถ„์ด ์ฝ”์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๋Š” ๋ฌด์—‡์„ ์ƒ๊ฐํ•˜๋“  lens ์–ด๋”˜๊ฐ€์— ๊ทธ๊ฒƒ์— ์ž‘์šฉํ•˜๋Š” ํ•ฉ์„ฑ์ž๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. lens์˜ ๋ชจ๋“  ๊ตฌ์„์„ ์„ค๋ช…ํ•˜๋Š” ์ฑ…์„ ์“ฐ๋ ค๋ฉด ์ง€๊ธˆ ์ฝ๋Š” ์ด ์ฑ…๋งŒํผ ๊ธธ์–ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. ์•ˆํƒ€๊น์ง€๋งŒ ์—ฌ๊ธฐ์„œ ๊ทธ๋Ÿฐ ๊ณต์„ ๋“ค์ผ ์ˆ˜๋Š” ์—†๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฑด ์—ฌ๋Ÿฌ๋ถ„์ด ์‹ค์ „์—์„œ ์–ธ์  ๊ฐ€ ๋งŒ๋‚  lens์˜ ๋ฒ”์šฉ ๋„๊ตฌ ๋ช‡ ๊ฐ€์ง€๋ฅผ ์งง๊ฒŒ ์†Œ๊ฐœํ•˜๋Š” ๊ฒƒ๋ฟ์ด๋‹ค. State ์กฐ์ž‘ lens ๋ชจ๋“ˆ์—๋Š” ์ƒํƒœ ํŽ‘ํ„ฐ๋ฅผ ์œ„ํ•œ ํ•ฉ์„ฑ์ž๋“ค์ด ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด: Control.Lens.Getter์˜ use๋Š” Control.Monad.State์˜ gets์™€ ๋น„์Šทํ•œ๋ฐ, ํ‰๋ฒ”ํ•œ ํ•จ์ˆ˜ ๋Œ€์‹  getter๋ฅผ ์ทจํ•œ๋‹ค. Control.Lens.Setter includes suggestive-looking operators that modify parts of a state targeted a setter (e.g. .= is analogous to set, %= to over and (+= x) to over (+x)). Control.Lens.Zoom๋Š” zoom์ด๋ผ๋Š” ์•„์ฃผ ํŽธ๋ฆฌํ•œ ํ•ฉ์„ฑ์ž๋ฅผ ์ œ๊ณตํ•œ๋‹ค. zoom์€ ์ˆœํšŒ(๋˜๋Š” ๋ Œ์ฆˆ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ์˜ ์ผ๋ถ€๋กœ ํ™•๋Œ€ํ•ด ๋“ค์–ด๊ฐ„๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด zoom ์€ ์ƒํƒœ ์žˆ๋Š” ๊ณ„์‚ฐ์„ ๋” ํฐ ์ƒํƒœ์™€ ์ž‘๋™ํ•˜๋Š” ๊ณ„์‚ฐ์œผ๋กœ ์ „์ดํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ์›๋ž˜ ์ƒํƒœ๋Š” ๊ทธ ํฐ ์ƒํƒœ์˜ ์ผ๋ถ€๊ฐ€ ๋œ๋‹ค. ์ด๋Ÿฐ ํ•ฉ์„ฑ์ž๋“ค์„ ์“ฐ๋ฉด ๊ทธ ์˜๋„๋ฅผ ์ž˜ ๋“œ๋Ÿฌ๋‚ด๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ์ฝ”๋“œ๋Š” ์ƒํƒœ์˜ ๊นŠ์€ ๋ถ€๋ถ„์„ ํˆฌ๋ช…ํ•˜๊ฒŒ ์กฐ์ž‘ํ•œ๋‹ค. import Control.Monad.State stateExample :: State Segment () stateExample = do segmentStart .= makePoint (0,0) zoom segmentEnd $ do positionX += 1 positionY *= 2 pointCoordinates %= negate GHCi> execState stateExample (makeSegment (1,2) (5,3)) Segment {_segmentStart = Point {_positionX = 0.0, _positionY = 0.0} , _segmentEnd = Point {_positionX = -6.0, _positionY = -6.0}} Iso ์ผ๋ จ์˜ Point์™€ Segment ์˜ˆ์ œ์—์„œ makePoint ํ•จ์ˆ˜๋ฅผ (Double, Double) ์ง์œผ๋กœ๋ถ€ํ„ฐ Point๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ํŽธ๋ฆฌํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ์จ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค. makePoint :: (Double, Double) -> Point makePoint (x, y) = Point x y ๊ทธ ๊ฒฐ๊ณผ ๋‚˜์˜ค๋Š” Point์˜ X, Y ์ขŒํ‘œ๋Š” ์›๋ž˜ ์ง์˜ ๋‘ ๊ตฌ์„ฑ์š”์†Œ์— ์ •ํ™•ํžˆ ๋Œ€์‘ํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด unmakePoint ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์„œ... unmakePoint :: Point -> (Double, Double) unmakePoint (Point x y) = (x, y) makePoint์™€ unmakePoint๊ฐ€ ์„œ๋กœ ์—ญ์ด ๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ์ด๊ฒƒ๋“ค์€ ์„œ๋กœ๋ฅผ ์ƒ์‡„ํ•œ๋‹ค. unmakePoint . makePoint = id makePoint . unmakePoint = id ๋ฌด์Šจ ๋ง์ธ๊ณ  ํ•˜๋‹ˆ, makePoint์™€ unmakePoint๋Š” ์ง์„ ์ ์œผ๋กœ, ์ ์„ ์ง์œผ๋กœ ๋ฌด์†์‹ค ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค. ์ „๋ฌธ ์šฉ์–ด๋ฅผ ์“ฐ์ž๋ฉด makePoint์™€ unmakePoint๋Š” ๋™ํ˜•์„ ํ˜•์„ฑํ•œ๋‹ค. unmakePoint๋Š” Lens' Point (Double, Double)๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ๋Œ€์นญ์œผ๋กœ makePoint๋Š” Lens' (Double, Double) Point๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๋ Œ์ฆˆ๋Š” ํ•œ ์Œ์˜ ์—ญ์ด๋‹ค. ์—ญ์ด ์žˆ๋Š” ๋ Œ์ฆˆ๋“ค์€ ๊ณ ์œ ์˜ Iso ๋™์˜ ํƒ€์ž…์„ ๊ฐ€์ง€๊ณ , Control.Lens.Iso์— ๋ณด์กฐ ๋„๊ตฌ๋“ค์ด ์ •์˜๋˜์–ด ์žˆ๋‹ค. iso ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•œ ์Œ์˜ ์—ญ์œผ๋กœ๋ถ€ํ„ฐ Iso๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. iso :: (s -> a) -> (b -> t) -> Iso s t a b pointPair :: Iso' Point (Double, Double) pointPair = iso unmakePoint makePoint Iso๋Š” Lense์ด๋ฏ€๋กœ ์ต์ˆ™ํ•œ ๋ Œ์ฆˆ ํ•ฉ์„ฑ์ž๋“ค๋„ ์ž˜ ์ž‘๋™ํ•œ๋‹ค. GHCi> import Data.Tuple (swap) GHCi> let testPoint = makePoint (2,3) GHCi> view pointPair testPoint -- Equivalent to unmakePoint (2.0,3.0) GHCi> view (pointPair . _2) testPoint 3.0 GHCi> over pointPair swap testPoint Point {_positionX = 3.0, _positionY = 2.0} ๊ทธ๋ฆฌ๊ณ  Iso๋Š” from์„ ์ด์šฉํ•˜์—ฌ ๋ฐ˜์ „๋  ์ˆ˜ ์žˆ๋‹ค. GHCi> :info from pointPair from :: AnIso s t a b -> Iso b a t s -- Defined in โ€˜Control.Lens.Isoโ€™ pointPair :: Iso' Point (Double, Double) -- Defined at WikibookLenses.hs:77:1 GHCi> view (from pointPair) (2,3) -- Equivalent to makePoint Point {_positionX = 2.0, _positionY = 3.0} GHCi> view (from pointPair . positionY) (2,3) 3.0 under๋Š” ๋˜ ๋‹ค๋ฅธ ํฅ๋ฏธ๋กœ์šด ํ•ฉ์„ฑ ์ž๋‹ค. under๋Š” ์ด๋ฆ„์—์„œ ๋ณด์ด๋“ฏ์ด over์™€ ๋น„์Šทํ•œ๋ฐ, from์—์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ˜์ „๋œ Iso๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. Data.Char์˜ chr์™€ ord๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  enum ๋™ํ˜•์‚ฌ์ƒ์„ ํ†ตํ•ด Char์˜ Int ํ‘œํ˜„์„ ๋‹ค๋ฃจ๋ฉด์„œ ์ด๋ฅผ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค. GHCi> :info enum enum :: Enum a => Iso' Int a -- Defined in โ€˜Control.Lens.Isoโ€™ GHCi> under enum (+7) 'a' 'h' newtype๊ณผ ๊ธฐํƒ€ ๋‹จ์ผ ์ƒ์„ฑ์ž ํƒ€์ž…๋“ค์€ ๋™ํ˜•์„ ๋งŒ๋“ ๋‹ค. Control.Lens.Wrapped๋Š” ์ด ์‚ฌ์‹ค์„ ํ™œ์šฉํ•˜์—ฌ Iso ๊ธฐ๋ฐ˜ ๋„๊ตฌ๋“ค์„ ์ œ๊ณตํ•˜๋Š”๋ฐ, ๊ทธ ์˜ˆ๋กœ newtype๋“ค์„ ์–ธ๋ž˜ํ•‘ํ•˜๊ธฐ ์œ„ํ•ด ๋ ˆ์ฝ”๋“œ ๋ ˆ์ด๋ธ” ์ด๋ฆ„์„ ๊ธฐ์–ตํ•  ํ•„์š”๋ฅผ ์—†์•ค๋‹ค. GHCi> let testConst = Const "foo" GHCi> -- getConst testConst GHCi> op Const testConst "foo" GHCi> let testIdent = Identity "bar" GHCi> -- runIdentity testIdent GHCi> op Identity testIdent "bar" ๊ทธ๋ฆฌ๊ณ  ์ธ์Šคํ„ด์Šค ์„ ํƒ์„ ์œ„ํ•œ newtype ๋ž˜ํ•‘์„ ์ข€ ๋” ๊น”๋”ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. GHCi> :m +Data.Monoid GHCi> -- getSum (foldMap Sum [1.. 10]) GHCi> ala Sum foldMap [1.. 10] 55 GHCi> -- getProduct (foldMap Product [1.. 10]) GHCi> ala Product foldMap [1.. 10] 3628800 Prism Iso๋ฅผ ํ†ตํ•ด optic๋“ค์˜ ๊ณ„์ธต๋„์—์„œ Lens๋ณด๋‹ค ๋ฐ‘์˜ ๋‹จ๊ณ„์— ์ฒ˜์Œ์œผ๋กœ ์ง„์ž…ํ–ˆ๋‹ค. ๋ชจ๋“  Iso๋Š” Lens ์ง€๋งŒ ๋ชจ๋“  Lens๊ฐ€ Iso๋Š” ์•„๋‹ˆ๋‹ค. Traversal์„ ๋˜๋Œ์•„๋ณด๋ฉด optic์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ์ด ์ ์ฐจ ํ๋ ค์ง€๋Š” ๊ฒƒ์„ ๊ด€์ฐฐํ•  ์ˆ˜ ์žˆ๋‹ค. Iso๋Š” ์ •ํ™•ํžˆ ํ•œ ๋Œ€์ƒ์„ ๊ฐ€์ง€๊ณ  ์—ญ์ด ์žˆ๋Š” optic์ด๋‹ค. Lens๋„ ์ •ํ™•ํžˆ ํ•œ ๋Œ€์ƒ์„ ๊ฐ€์ง€์ง€๋งŒ ์—ญ์ด ์—†๋‹ค. Traversable์€ ๋ณต์ˆ˜ ๋Œ€์ƒ์„ ๊ฐ€์ง€๊ณ  ์—ญ์ด ์—†๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€ ์˜ค๋ฉฐ ์šฐ๋ฆฌ๋Š” ๋จผ์ € ๊ฐ€์—ญ์„ฑ์„ ๋ฒ„๋ฆฌ๊ณ  ๊ทธ๋‹ค์Œ ๋Œ€์ƒ์˜ ์œ ์ผ์„ฑ์„ ๋ฒ„๋ ธ๋‹ค. ๋‹ค๋ฅธ ๊ธธ์„ ์ทจํ•ด์„œ ๊ฐ€์—ญ์„ฑ๋ณด๋‹ค ์œ ์ผ์„ฑ์„ ๋จผ์ € ๋ฒ„๋ฆฌ๋ฉด ๋™ํ˜•๊ณผ ์ˆœํšŒ ์‚ฌ์ด์—์„œ ๋‘ ๋ฒˆ์งธ ์ข…๋ฅ˜์˜ optic์ธ ํ”„๋ฆฌ์ฆ˜์„ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๋‹ค. Prism์€ ๊ฐ€์—ญ optic์ด์ง€๋งŒ ๋Œ€์ƒ์ด ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜๋Š” ์•„๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ฐ€์—ญ์„ฑ๊ณผ ๋ณต์ˆ˜ ๋Œ€์ƒ์€ ์–‘๋ฆฝํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋” ๋ช…ํ™•ํžˆ ํ•˜์ž๋ฉด, Prism์€ ๋Œ€์ƒ์ด ์—†๊ฑฐ๋‚˜ ์ •ํ™•ํžˆ ํ•˜๋‚˜๋งŒ ์žˆ๋‹ค. ์‹คํŒจ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๋‹จ์ผ ๋Œ€์ƒ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค๊ณ  ํ•˜๋‹ˆ ํŒจํ„ด ๋งค์นญ๊ฐ™์ด ๋“ค๋ฆฌ๋Š”๋ฐ, ํ”„๋ฆฌ์ฆ˜์€ ๋ฐ”๋กœ ๊ทธ ๊ฐœ๋…์„ ํฌ์ฐฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠœํ”Œ๊ณผ ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋ Œ์ฆˆ์˜ ์ž์—ฐ์Šค๋Ÿฌ์šด ์šฉ๋ก€๋ฅผ ์ œ๊ณตํ•˜๋“ฏ์ด Maybe, Either, ๊ธฐํƒ€ ๋ณต์ˆ˜ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง€๋Š” ํƒ€์ž…๋“ค์ด ํ”„๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค. ๋ชจ๋“  Prism์€ Traversal์ด๋ฏ€๋กœ ์ˆœํšŒ๋ฅผ ์œ„ํ•œ ๋ชจ๋“  ํ•ฉ์„ฑ์ž, setter, fold๋Š” ํ”„๋ฆฌ์ฆ˜์—๋„ ์ž‘๋™ํ•œ๋‹ค. GHCi> set _Just 5 (Just "orange") Just 5 GHCi> set _Just 5 Nothing Nothing GHCi> over _Right (2 *) (Right 5) Right 10 GHCi> over _Right (2 *) (Left 5) Left 5 GHCi> toListOf _Left (Left 5) [5] ํ•˜์ง€๋งŒ Prism์€ Getter๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋Œ€์ƒ์ด ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ์ด์œ ๋กœ ๋Œ€์ƒ์„ ์–ป๊ธฐ ์œ„ํ•ด view ๋Œ€์‹  preview๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. GHCi> preview _Right (Right 5) Just 5 GHCi> preview _Right (Left 5) Nothing Prism์˜ ์—ญ์„ ์–ป์œผ๋ ค๋ฉด Control.Lens.Review์˜ re์™€ review๋ฅผ ์“ด๋‹ค. re๋Š” from๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋‹จ์ˆœํžˆ Getter๋ฅผ ์ œ๊ณตํ•œ๋‹ค. review๋Š” ๋ฐ˜์ „๋œ ํ”„๋ฆฌ์ฆ˜์„ ์ทจํ•˜๋Š” view์™€ ๋™๋“ฑํ•˜๋‹ค. GHCi> view (re _Right) 3 Right 3 GHCi> review _Right 3 Right 3 ๋ Œ์ฆˆ๊ฐ€ ๋‹จ์ง€ ๋ ˆ์ฝ”๋“œ ํ•„๋“œ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋“ฏ์ด, ํ”„๋ฆฌ์ฆ˜์€ ์ƒ์„ฑ์ž ๋งค์นญ๋งŒ์„ ์œ„ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Control.Lens.Prism์— ์ •์˜๋œ only๋Š” ๋™์น˜ ๊ฒ€์‚ฌ๋ฅผ Prism์œผ๋กœ ๊ฐ์‹ผ๋‹ค. GHCi> :info only only :: Eq a => a -> Prism' a () -- Defined in โ€˜Control.Lens.Prismโ€™ GHCi> preview (only 4) (2 + 2) Just () GHCi> preview (only 5) (2 + 2) Nothing prism๊ณผ prism' ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์šฐ๋ฆฌ๋งŒ์˜ ํ”„๋ฆฌ์ฆ˜์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ Data.List์˜ stripPrefix๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์ œ๋‹ค. GHCi> :info prism prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b -- Defined in โ€˜Control.Lens.Prismโ€™ GHCi> :info prism' prism' :: (b -> s) -> (s -> Maybe a) -> Prism s s a b -- Defined in โ€˜Control.Lens.Prismโ€™ GHCi> import Data.List (stripPrefix) GHCi> :t stripPrefix stripPrefix :: Eq a => [a] -> [a] -> Maybe [a] prefixed :: Eq a => [a] -> Prism' [a] [a] prefixed prefix = prism' (prefix ++) (stripPrefix prefix) GHCi> preview (prefixed "tele") "telescope" Just "scope" GHCi> preview (prefixed "tele") "orange" Nothing GHCi> review (prefixed "tele") "graph" "telegraph" prefixed๋Š” lens ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ Data.List.Lens ๋ชจ๋“ˆ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ 1. Control.Lens.Prism์—๋Š” ๋‹ค์Œ์˜ ํƒ€์ž…(๊ฐ„๋žตํ™”๋จ)์„ ๊ฐ€์ง€๋Š” outside ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋‹ค. outside :: Prism s t a b -> Lens (t -> r) (s -> r) (b -> r) (a -> r) a. outside๊ฐ€ ํ•˜๋Š” ์ผ์„ ๊ตฌํ˜„์„ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š๊ณ  ์„ค๋ช…ํ•˜๋ผ. (ํžŒํŠธ: ๋ฌธ์„œ์—๋Š” outside๋ฅผ ํ†ตํ•ด "Prism์„ ์ผ์ข…์˜ ์ผ๋“ฑ๊ธ‰ ํŒจํ„ด์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค"๋ผ๊ณ  ์“ฐ์—ฌ์žˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์˜ ๋‹ต์€ ์ด๊ฒƒ์„ ํ™•์žฅํ•˜์—ฌ ์–ด๋–ป๊ฒŒ ๊ทธ๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ์„ค๋ช…ํ•ด์•ผ ํ•œ๋‹ค.) b. outside๋ฅผ ์‚ฌ์šฉํ•ด Prelude์˜ maybe์™€ either๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ. maybe :: b -> (a -> b) -> Maybe a -> b either :: (a -> c) -> (b -> c) -> Either a b -> c ๋ฒ•์น™ ์ •์ƒ์ ์ธ optic์ด๋ผ๋ฉด ์–ด๋–ป๊ฒŒ ํ–‰๋™ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๊ธฐ์ˆ ํ•˜๋Š” ๋ฒ•์น™๋“ค์ด ์žˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋‹ค๋ฃฌ optic ๋“ค์— ์ ์šฉ๋˜๋Š” ๋ฒ•์น™๋“ค์„ ์•Œ์•„๋ณด์ž. ๊ณ„์ธต๋„์˜ ๊ผญ๋Œ€๊ธฐ๋ถ€ํ„ฐ ๋ณด๋ฉด Foldable ํด๋ž˜์Šค์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Fold์˜ ๋ฒ•์น™์€ ์—†๋‹ค. ๋ชจ๋“  ํ•จ์ˆ˜๋Š” Getter๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Getter์˜ ๋ฒ•์น™๋„ ์—†๋‹ค. Setter์˜ ๋ฒ•์น™์€ ์žˆ๋‹ค. over๋Š” fmap์˜ ์ผ๋ฐ˜ํ™”์ด๊ธฐ ๋•Œ๋ฌธ์— ํŽ‘ ํ„ฐ ๋ฒ•์น™์˜ ๋Œ€์ƒ์ด๋‹ค. over s id = id over s g. over s f = over s (g . f) set s x = over s (const x)์ด๊ธฐ ๋•Œ๋ฌธ์— ํŽ‘ ํ„ฐ ์ œ2๋ฒ•์น™์€: set s y. set s x = set s y ์ฆ‰ ๋‘ ๋ฒˆ์˜ ์„ธํŒ…์€ ํ•œ ๋ฒˆ์˜ ์„ธํŒ…๊ณผ ๊ฐ™๋‹ค. Traversal ๋ฒ•์น™๋„ ๋น„์Šทํ•˜๊ฒŒ Traversable ๋ฒ•์น™์˜ ์ผ๋ฐ˜ํ™”๋‹ค. t pure = pure fmap (t g) . t f = getCompose . t (Compose . fmap g. f) Traversable ์žฅ์˜ ๊ฒฐ๋ก ์€ ์ด๋Ÿฐ ๊ฒƒ์ด์—ˆ๋‹ค. ๋ชจ๋“  ๋Œ€์ƒ์„ ์ •ํ™•ํžˆ ํ•œ ๋ฒˆ์”ฉ ์ˆœํšŒ ๋ฐฉ๋ฌธํ•œ ๋‹ค์Œ, ๋‘˜๋Ÿฌ์‹ธ๋Š” ๊ตฌ์กฐ๋ฅผ ๋ณด์กดํ•˜๊ฑฐ๋‚˜ ์™„์ „ํžˆ ํŒŒ๊ดดํ•ด์•ผ ํ•œ๋‹ค. ๋ชจ๋“  Lens๋Š” Traversal์ด ์ž Setter์ด๋ฉฐ ๋”ฐ๋ผ์„œ ์œ„์˜ ๋ฒ•์น™๋“ค์€ ๋ Œ์ฆˆ๋“ค์—๋„ ์ ์šฉ๋œ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ๋ชจ๋“  Lens๋Š” Getter์ด๋‹ค. ๋ Œ์ฆˆ๊ฐ€ getter ์ด์ž setter์ด๊ธฐ ๋•Œ๋ฌธ์— ์„ค์ • ๋Œ€์ƒ๊ณผ ํš๋“ ๋Œ€์ƒ์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ์ด ๊ณตํ†ต ์š”๊ตฌ์‚ฌํ•ญ์€ ๋‹ค์Œ ๋ฒ•์น™์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค. view l (set l x) = x set l (view l z) z = z ์œ„์—์„œ ๋ณธ setter์˜ "์ด์ค‘ ์„ค์ •" ๋ฒ•์น™๊ณผ ํ•จ๊ป˜ ์ด ๋ฒ•์น™๋“ค์„ ๋Œ€๊ฐœ ๋ Œ์ฆˆ ๋ฒ•์น™์ด๋ผ ๋ถ€๋ฅธ๋‹ค. ๋น„์Šทํ•œ ๋ฒ•์น™๋“ค์ด Prism์—๋„ ์žˆ๋Š”๋ฐ, view ๋Œ€์‹  preview๋ฅผ, set ๋Œ€์‹  review๋ฅผ ์“ด๋‹ค. preview p (review p x) = Just x review p <$> preview p z = Just z Iso๋Š” ๋ Œ์ฆˆ์ด๋ผ ํ”„๋ฆฌ์ฆ˜์ด์–ด์„œ ์œ„์˜ ๋ชจ๋“  ๋ฒ•์น™์ด ์„ฑ๋ฆฝํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋™ํ˜•์„ฑ preview i = Just . view i(์ฆ‰ preview๋Š” ์ ˆ๋Œ€ ์‹คํŒจํ•˜์ง€ ์•Š๋Š”๋‹ค)์— ๋”ฐ๋ผ ํ”„๋ฆฌ์ฆ˜ ๋ฒ•์น™์€ ๊ฐ„๋žตํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. view i (review i x) = x review i (view i z) = z ๋‹คํ˜•์„ฑ ๊ฐฑ์‹  Setter s t a b๋‚˜ Lens s t a b ๊ฐ™์€ optic ํƒ€์ž…๋“ค์—๋Š” ๋…๋ฆฝ๋œ ํƒ€์ž… ๋ณ€์ˆ˜ ๋„ค ๊ฐœ๊ฐ€ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค์–‘ํ•œ optic ๋ฒ•์น™๋“ค์„ ๊ณ ๋ คํ•ด ๋ณด๋ฉด ๋ชจ๋“  s, t, a, b์˜ ์กฐํ•ฉ์ด ๋ง์ด ๋˜๋Š” ๊ฑด ์•„๋‹ˆ๋‹ค. ๊ฐ€๋ น setter์˜ "์ด์ค‘ ์„ค์ •" ๋ฒ•์น™์„ ๋ณด์ž. set s y. set s x = set s y "๋‘ ๋ฒˆ์˜ ์„ค์ •์€ ํ•œ ๋ฒˆ์˜ ์„ค์ •๊ณผ ๊ฐ™๋‹ค"๊ฐ€ ๋ง์ด ๋˜๋ ค๋ฉด ๊ฐ™์€ setter๋กœ ๋‘ ๋ฒˆ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์•ผ ํ•œ๋‹ค. ์ด ๋ฒ•์น™์ด ์„ฑ๋ฆฝํ•˜๋ ค๋ฉด t๋ฅผ ์–ด๋–ป๊ฒŒ๋“  ํ•œ์ • ์ง€์–ด์„œ s์™€ ๊ฐ™๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. (๊ทธ๋Ÿฌ์ง€ ์•Š์œผ๋ฉด ๋งค๋ฒˆ setํ•  ๋•Œ๋งˆ๋‹ค ์ „์ฒด ํƒ€์ž…์ด ๋ฐ”๋€Œ์–ด ํƒ€์ž…์ด ๋งž์ง€ ์•Š๊ฒŒ ๋œ๋‹ค) ์œ„์™€ ๊ฐ™์€ ๋ฒ•์น™๋“ค์— ์ˆ˜๋ฐ˜๋˜๋Š” ํƒ€์ž… ์ œํ•œ์œผ๋กœ๋ถ€ํ„ฐ ๋ชจ๋ฒ”์ ์ธ Setter, Traversal, Prism, Lens์˜ ๋„ค ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์„œ๋กœ ์™„์ „ํžˆ ๋…๋ฆฝ์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋…๋ฆฝ์„ฑ์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด์ง€๋Š” ์•Š๊ณ , ๊ทธ ๊ฒฐ๋ก ์€ ๋ช‡ ๊ฐ€์ง€ ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋กœ a์™€ b๋Š” ๊ฐ™์€ ์˜ท๊ฐ์—์„œ ์ž˜๋ผ๋‚ธ ๊ฒƒ์ด๋ผ ์–ด๋–ค optic์ด ํƒ€์ž…์„ ๋ฐ”๊พธ๋”๋ผ๋„ a์™€ b๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ค ๋ฐฉ๋ฒ•์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. s์™€ t์— ๋Œ€ํ•ด์„œ๋„ ๊ฐ™๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ, a์™€ b๊ฐ€ ๊ฐ™๋‹ค๋ฉด s์™€ t๋„ ๊ฐ™์•„์•ผ๋งŒ ํ•œ๋‹ค. ์‹ค์ „์—์„œ ์ด ์ œํ•œ์‚ฌํ•ญ๋“ค์ด ๋œปํ•˜๋Š” ๋ฐ”๋Š”, ํƒ€์ž…์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ์œ ํšจํ•œ optic์€ s์™€ t๊ฐ€ a์™€ b์— ๋Œ€ํ•ด ๋งค๊ฐœํ™”๋˜๊ธฐ ๋งˆ๋ จ์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ํƒ€์ž…์„ ๋ฐ”๊พธ๋Š” ๊ฐฑ์‹ ์„ ๋‹คํ˜•์„ฑ ๊ฐฑ์‹ (polymorphic update)์ด๋ผ๊ณ  ์นญํ•œ๋‹ค. ์„ค๋ช…์„ ์œ„ํ•ด lens์—์„œ ์˜ˆ์ œ ๋ช‡ ๊ฐœ๋ฅผ ๊ฐ€์ ธ์™”๋‹ค. -- To avoid distracting details, -- we specialised the types of argument and _1. mapped :: Functor f => Setter (f a) (f b) a b contramapped :: Contravariant f => Setter (f b) (f a) a b argument :: Setter (b -> r) (a -> r) a b traverse :: Traversable t => Traversal (t a) (t b) a b both :: Bitraversable r => Traversal (r a a) (r b b) a b _1 :: Lens (a, c) (b, c) a b _Just :: Prism (Maybe a) (Maybe b) a b ์ด์ œ Lens ํƒ€์ž…์„ ์†Œ๊ฐœํ•  ๋•Œ ๋‚จ๊ฒจ๋‘” ์งˆ๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค. Lens์™€ Traversal์€ ํƒ€์ž… ๋ณ€๊ฒฝ์„ ํ—ˆ์šฉํ•˜๊ณ  Getter์™€ Fold๋Š” ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ๋ชจ๋“  Lens๊ฐ€ ํƒ€์ž… ๋ณ€๊ฒฝ์„ ํ—ˆ์šฉํ•˜๋Š” Traversal์ด๊ณ  Getter์™€ Fold๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๊ณ  ๋งํ•˜๊ธฐ๋Š” ์„ฑ๊ธ‰ํ•œ ๊ฒƒ ์•„๋‹Œ๊ฐ€? ๊ทธ๋Ÿฐ๋ฐ ํƒ€์ž… ๋ณ€์ˆ˜๋“ค์˜ ๋…๋ฆฝ์„ฑ์€ ๋ฒ•์น™์„ ๋งŒ์กฑํ•˜๋Š” Lens๋ผ๋ฉด Getter๋กœ ์“ฐ์ผ ์ˆ˜ ์žˆ๊ณ , ๋ฒ•์น™์„ ๋งŒ์กฑํ•˜๋Š” Traversal์€ Fold๋กœ ์“ฐ์ผ ์ˆ˜ ์žˆ์Œ์„ ๋œปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฒ•์น™์„ ๋งŒ์กฑํ•˜๋Š” ๋ Œ์ฆˆ์™€ ์ˆœํšŒ๋Š” ํƒ€์ž…์„ ๋ฐ”๊พธ์ง€ ์•Š๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•ญ์ƒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ฌด๋Ÿฐ ์กฐ๊ฑด ์—†์ด ์•ž์„œ ๋ดค๋“ฏ์ด lens ๊ฐ™์€ ํ•จ์ˆ˜๋‚˜ makeLenses ๊ฐ™์€ ์ž๋™ ์ƒ์„ฑ ๋„๊ตฌ๋ฅผ ์ด์šฉํ•ด์„œ optic์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ์ด๊ฒƒ๋“ค์€ ๋‹จ์ง€ ํŽธ๋ฆฌํ•œ ๋„์šฐ๋ฏธ๋‹ค. Lens๋‚˜ Traversal ๋“ฑ์ด ๊ทธ์ € ๋™์˜ ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์ •์˜๋Š” optic์„ ์ž‘์„ฑํ•  ๋•Œ ํ•„์š” ์—†๋‹ค. ๊ฐ€๋ น Lens s t a b ๋Œ€์‹  ์–ธ์ œ๋‚˜ Functor f => (a -> f b) -> (s -> f t)๋ผ๊ณ  ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ lens๋ฅผ ์ „ํ˜€ ์“ฐ์ง€ ์•Š๊ณ ๋„ lens์™€ ํ˜ธํ™˜๋˜๋Š” optic์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ง์ด๋‹ค. ๋ชจ๋“  Lens, Traversal, Setter, Getting์€ base ํŒจํ‚ค์ง€ ์™ธ์˜ ์–ด๋Š ๊ฒƒ์—๋„ ์˜์กดํ•˜์ง€ ์•Š๊ณ  ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋” ์ฝ์„๊ฑฐ๋ฆฌ ์•ž์„œ ์–ธ๊ธ‰ํ•˜๊ธฐ๋ฅผ lens๋งŒ์œผ๋กœ๋„ ์ฑ… ํ•œ ๊ถŒ์€ ๋‚˜์˜จ๋‹ค๊ณ  ํ–ˆ์—ˆ๋‹ค. ์—ฌ๊ธฐ์— ๊ฐ€์žฅ ๊ทผ์ ‘ํ•œ ๊ฒƒ์€ Artyom Kazak์˜ ๋ธ”๋กœ๊ทธ์— ์žˆ๋Š” "lens over tea" ์—ฐ์žฌ๋ฌผ์ด๋‹ค. ์ด ์—ฐ์žฌ์—์„œ๋Š” lens์—์„œ ํ•จ์ˆ˜ํ˜• ์ฐธ์กฐ์˜ ๊ตฌํ˜„์„ ํƒํ—˜ํ•˜๊ณ  ๊ทธ ์†์˜ ๊ฐœ๋…์„ ์—ฌ๊ธฐ์„œ ํ•œ ๊ฒƒ๋ณด๋‹ค ์‹ฌ๋„ ์žˆ๊ฒŒ ๋‹ค๋ฃฌ๋‹ค. ๊ฐ•๋ ฅ ์ถ”์ฒœํ•œ๋‹ค. lens์˜ ๊นƒํ—™ ์œ„ํ‚ค์— ์œ ์šฉํ•œ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค. ๋ฌผ๋ก  lens์˜ API ๋ฌธ์„œ๋„ ๋ณผ ๊ฐ€์น˜๊ฐ€ ์žˆ๋‹ค. lens๋Š” ๊ฑฐ๋Œ€ํ•˜๊ณ  ๋ณต์žกํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. ๊ทธ ๊ตฌํ˜„์„ ๊ณต๋ถ€ํ•˜๊ณ  ์‹ถ์ง€๋งŒ ์ข€ ๋” ๊ฐ„๋‹จํ•œ ๊ฒƒ์„ ์›ํ•œ๋‹ค๋ฉด microlens๋‚˜ lens-simple ๊ฐ™์ด lens์™€ ํ˜ธํ™˜๋˜๋Š” ์†Œํ˜• ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์ด ์ข‹์€ ์ถœ๋ฐœ์ ์ด๋‹ค. optic ๊ธฐ๋ฐ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ๋ฐฐ์šฐ๊ณ  ๋˜ํ•œ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ•จ์ˆ˜ํ˜• ์ฐธ์กฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ์“ฐ์ด๋Š”์ง€ ์ตํžˆ๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋“ค๋ฉด diagrams๋Š” lens๋ฅผ ํญ๋„“๊ฒŒ ํ™œ์šฉํ•˜์—ฌ ๊ทธ๋ž˜ํ”ฝ์Šค ์š”์†Œ์˜ ์†์„ฑ์„ ๋‹ค๋ฃจ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. wreq๋Š” lens ๊ธฐ๋ฐ˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ–์ถ˜ ์›น ํด๋ผ์ด์–ธํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. xml-lens๋Š” XML์„ ์ˆ˜์ •ํ•˜๊ธฐ ์œ„ํ•œ optic์„ ์ œ๊ณตํ•œ๋‹ค. formattable์€ ๋‚ ์งœ, ์‹œ๊ฐ„, ์ˆซ์ž ํฌ๋งคํŒ…์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. Formattable.NumFormat์€ lens ํŒจํ‚ค์ง€์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  lens ํ˜ธํ™˜ ๋ Œ์ฆˆ๋“ค์„ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ์˜ ์˜ˆ์‹œ๋‹ค. 10 ๊ฐ€๋ณ€ ๊ฐ์ฒด ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Mutable_objects IORef ๋™์‹œ์„ฑ์˜ ํ•จ์ • ST ๋ชจ๋‚˜๋“œ ๊ฐ€๋ณ€ ์ž๋ฃŒ๊ตฌ์กฐ ๋” ์ฝ์„๊ฑฐ๋ฆฌ ๋…ธํŠธ ํ•จ์ˆ˜ ์ˆœ์ˆ˜์„ฑ์€ ํ•˜์Šค์ผˆ์˜ ํ•ต์‹ฌ์ด๋‹ค. ํ•˜์Šค ์ผˆ ์ƒํƒœ๊ณ„์—์„œ๋Š” ๊ฐ€๋ณ€ ์ƒํƒœ๋ฅผ ๋˜๋„๋ก ํ”ผํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. State ๋ชจ๋‚˜๋“œ ๋•์— ์ƒํƒœ๋ฅผ ํŽธ๋ฆฌํ•˜๋ฉด์„œ ์ˆœ์ˆ˜ ํ•จ์ˆ˜์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ถ”์ ํ•  ์ˆ˜ ์žˆ๊ฑฐ๋‹ˆ์™€, containers๋‚˜ unordered-containers ํŒจํ‚ค์ง€์˜ ํšจ์œจ์ ์ธ ๋ถˆ๋ณ€ ์ž๋ฃŒ๊ตฌ์กฐ ๋•์— ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์—๊ฒŒ ๋ถˆ๋ณ€์„ฑ์ด ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ์ผ์€ ์ ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ์ € ๊ฐ€๋ณ€ ์ƒํƒœ๊ฐ€ ๋” ๋‚˜์„ ๋•Œ๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ํ•˜์Šค ์ผˆ ์ฝ”๋“œ์—์„œ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ์ž‘์„ฑ๋œ, ๊ฐ€๋ณ€ ์ƒํƒœ๋ฅผ ๊ฐ€์ •ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ. ์ด๋ฒคํŠธ-์ฝœ๋ฐฑ GUI ํˆดํ‚ท์ด ๋Œ€ํ‘œ์ ์ด๋‹ค. ํ•˜์Šค ์ผˆ๋กœ ๋ช…๋ นํ˜• ๊ฐ€๋ณ€ ๋ณ€์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ์–ธ์–ด๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ๋ณ€์ˆ˜๋ฅผ ํŒŒ๊ดด์ ์œผ๋กœ ๊ฐฑ์‹ ํ•ด์•ผ ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•  ๋•Œ ๊ณ„์‚ฐ ๋Šฅ๋ ฅ์„ ํ•œ ๋ฐฉ์šธ๊นŒ์ง€ ์ฅ์–ด์งœ๋‚ด์•ผ ํ•˜๋Š” ๊ฑฐ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฒ”์šฉ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋Š” ์ด๋Ÿฐ ์ž‘์—…๋“ค์„ ์ž˜ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์Šค์ผˆ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ๊ฐ€๋ณ€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ฟ ์•„๋‹ˆ๋ผ, ๊ฐ€๋ณ€์„ฑ์„ ํ†ต์ œ ํ•˜์— ๋‘์–ด, ๋ถˆ๋ณ€์„ฑ์ด ๊ธฐ๋ณธ์ธ ํ™˜๊ฒฝ์„ ํ‰ํ™”๋กญ๊ฒŒ ์œ ์ง€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. IORef ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ์˜ˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ž. ์œ ์ € ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์œ„ํ•œ ์ผ๋ฐ˜์ ์ธ ์ฝ”๋“œ๋Š” ์ด๋ฒคํŠธ์™€ ์ฝœ๋ฐฑ ๋ชจ๋ธ์„ ์ด์šฉํ•œ๋‹ค. ์ด๋ฒคํŠธ๋Š” ๋ฒ„ํŠผ์ด๋‚˜ ํ‚ค๋ณด๋“œ๋ฅผ ๋ˆ„๋ฅด๋Š” ๊ทธ๋Ÿฐ ๊ฒƒ์ด๊ณ , ์ฝœ๋ฐฑ์€ ๊ทธ ์ด๋ฒคํŠธ์— ์‘๋‹ตํ•˜์—ฌ ํ˜ธ์ถœ๋˜๋Š” ์ฝ”๋“œ ์กฐ๊ฐ์ด๋‹ค. ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ(์ฆ‰, ๊ทธ๋Ÿฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ์ฝ”๋“œ)๋Š” ์ธํ„ฐํŽ˜์ด์Šค ์š”์†Œ, ๊ด€๋ จ ์ด๋ฒคํŠธ, ๋Œ€์‘ํ•˜๋Š” ์ฝœ๋ฐฑ์„ ์„œ๋กœ ์—ฐ๊ฒฐํ•ด์•ผ ํ•œ๋‹ค. ๋‹ค์Œ์€ ์ฝœ๋ฐฑ์„ ๋“ฑ๋กํ•˜๋Š” ๊ฐ€์ƒ์˜ ํ•จ์ˆ˜๋‹ค. register :: (Element -> Event) -> Element -> IO () -> IO () IO () ์ธ์ž๋Š” ์ฝœ๋ฐฑ์ด๊ณ  register์˜ ๊ฒฐ๊ณผ๋Š” ์—ฐ๊ฒฐ์„ ๊ตฌ์„ฑํ•˜๋Š” IO ์•ก์…˜์ด๋‹ค. register click button1 (print "Hello")์„ ์‹คํ–‰ํ•˜๋ฉด button1์„ ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค ์ฝ˜์†”์— "Hello"๊ฐ€ ์ถœ๋ ฅ๋  ๊ฒƒ์ด๋‹ค. IO๊ฐ€ ๋งŒ์—ฐํ•˜๊ณ  ์˜๋ฏธ ์žˆ๋Š” ๋ฐ˜ํ™˜๊ฐ’๋„ ์—†๋Š” register์™€ ์œ„์˜ ์„ค๋ช…์„ ๋ณด๋‹ˆ ์˜๋ฝ์—†์ด ๋ช…๋ นํ˜•์ด๋ผ๋Š” ๋Š๋‚Œ์ด ๋“ ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๊ฐ€์ƒ์˜ GUI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•˜์Šค์ผˆ๊ณผ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋ช…๋ นํ˜• ์–ธ์–ด๋กœ ์ž‘์„ฑ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ›Œ๋ฅญํ•œ ์‚ฌ๋žŒ๋“ค์ด ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ•˜์Šค์ผˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ž‘์„ฑํ–ˆ์ง€๋งŒ, ์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋งค์šฐ ์–‡์€ ๊ฒƒ์ด์–ด์„œ ์›๋ž˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์Šคํƒ€์ผ์ด ์šฐ๋ฆฌ ์ฝ”๋“œ์— ๋“œ๋Ÿฌ๋‚œ๋‹ค 1. register๋ฅผ ์ด์šฉํ•ด ์ฝ˜์†”์— ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ๋Œ€ํ™” ์ƒ์ž๋ฅผ ๋„์šฐ๋Š” ๋“ฑ์˜ IO ์•ก์…˜์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์€ ์ถฉ๋ถ„ํžˆ ์‰ฝ๋‹ค. ํ•˜์ง€๋งŒ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ์–ด๋–ค ์นด์šดํ„ฐ๋ฅผ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? register์˜ ํƒ€์ž…์—๋Š” ์ฝœ๋ฐฑ์— ์ •๋ณด๋ฅผ ๋„˜๊ธฐ๊ฑฐ๋‚˜ ์ฝœ๋ฐฑ์œผ๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ๋ฐ›์„ ๋ฐฉ๋ฒ•์ด ๋“œ๋Ÿฌ๋‚˜์ง€ ์•Š๋Š”๋‹ค(๋ฐ˜ํ™˜ ํƒ€์ž…์ด ()์ด๋‹ค). State๋Š” ๋„์›€์ด ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ฝœ๋ฐฑ์— ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ๋„˜๊ธธ ๋ฐฉ๋ฒ•์ด ์žˆ๋”๋ผ๋„ ๊ทธ ์•ˆ์—์„œ State ๊ณ„์‚ฐ์„ ํ•œ ๋‹ค์Œ์€? ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฌด์—‡์„ ํ•  ๊ฒƒ์ธ๊ฐ€? ์นด์šดํ„ฐ์˜ ๋‹ค์Œ ์ƒํƒœ๋ฅผ ๋ฒ„ํŠผ์„ ๋˜ ๋ˆ„๋ฅผ ๋•Œ ์ฝœ๋ฐฑ์— ๋„˜๊ฒจ์•ผ ํ•˜๋Š”๋ฐ, ๊ทธ๊ฒŒ ์–ธ์ œ ์ผ์–ด๋‚ ์ง€๋„ ๋ชจ๋ฅด๊ณ  ์–ด์จŒ๊ฑด ๊ทธ ๊ฐ’์„ ์œ ์ง€ํ•˜๊ณ  ์žˆ์„ ๋ฐฉ๋ฒ•๋„ ์—†๋‹ค. ๋งŽ์€ ์–ธ์–ด์—์„œ ์ด ๋ฌธ์ œ์˜ ํ™•์‹คํ•œ ํ•ด๊ฒฐ์ฑ…์€ ์ฝœ๋ฐฑ ๋ฐ”๊นฅ์— ๊ฐ€๋ณ€ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฝœ๋ฐฑ์—์„œ ์ด ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ฝœ๋ฐฑ ์ฝ”๋“œ์—์„œ ์ด ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฑฑ์ •ํ•  ํ•„์š” ์—†๋‹ค. ํ•˜์Šค์ผˆ๋„ ๊ทธ๋Ÿฐ ๊ฒƒ์„ ํ—ˆ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์‚ฌ์‹ค ๊ฐ€๋ณ€ ๋ณ€์ˆ˜ ํƒ€์ž…์€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์œผ๋ฉฐ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๊ฒƒ์€ IORef๋‹ค. IORef๋Š” ๊ฐ€๋ณ€ ๊ฐ’์„ ๋‹ด๋Š” ์•„์ฃผ ๋‹จ์ˆœํ•œ ์ƒ์ž๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•œ๋‹ค. GHCi> import Data.IORef GHCi> :t newIORef newIORef :: a -> IO (IORef a) GHCi> box <- newIORef (4 :: Int) newIORef๋Š” ๊ฐ’์„ ํ•˜๋‚˜ ์ทจํ•ด์„œ ๊ทธ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ IORef๋ฅผ ๋Œ๋ ค์ค€๋‹ค. ์ด์ œ readIORef๋กœ ๊ทธ ์•ˆ์˜ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. GHCi> :t readIORef readIORef :: IORef a -> IO a GHCi> readIORef box >>= print ๊ทธ๋ฆฌ๊ณ  modifyIORef์™€ writeIORef๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. GHCi> modifyIORef box (2*) GHCi> readIORef box >>= print GHCi> writeIORef box 0 GHCi> readIORef box >>= print ๋ฒ„ํŠผ ํด๋ฆญ ์‚ฌ์ด์— ์œ ์ง€๋˜๋Š” ์ •๋„๋ผ๋ฉด, IORef ๋ฉด ์นด์šดํ„ฐ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•˜๋‹ค. setupGUI :: IORef Int -> IO () setupGUI counter = do -- However much other GUI preparation code we need. register click button1 (modifyIORef counter (+1)) main :: IO () main = do -- etc. counter <- newIORef (0 :: Int) setupGUI counter -- Then just use the counter value wherever necessary. IORef๋ฅผ ํ•ฉ๋‹นํ•œ ์ด์œ  ์—†์ด ๋งˆ๊ตฌ์žก์ด๋กœ ์“ฐ๋Š” ๊ฒƒ์— ์•„๋ฌด ์˜๋ฏธ๋„ ์—†์Œ์„ ์ฃผ์˜ํ•˜๋ผ. ๊ฐ€๋ณ€ ์ƒํƒœ์— ๋Œ€ํ•œ ๊ทผ๋ณธ์ ์ธ ๊ณ ๋ ค๋Š” ์ฐจ์น˜ํ•˜๊ณ , ๋ช…์‹œ์ ์ธ read/write/modify ํ˜ธ์ถœ์€ ์ „ํ˜€ ํŽธ๋ฆฌํ•˜์ง€ ์•Š๋‹ค. IORef๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ๋ถ€์ˆ˜์ ์ธ IO๋Š” ๋งํ•  ํ•„์š”๋„ ์—†๋‹ค(์šฐ๋ฆฌ์˜ ๊ฐ€์ƒ ์˜ˆ์ œ์—์„œ๋Š” ํฐ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋ฐ, GUI ์ฝ”๋“œ๊ฐ€ ์–ด์จŒ๋“  IO ๋‚ด๋ถ€์— ์žˆ์–ด์•ผ ํ•˜๊ณ , ํ”„๋กœ๊ทธ๋žจ์˜ ํ•ต์‹ฌ์„ ์ด๋ฃจ๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋“ค๊ณผ ๊ฒฉ๋ฆฌํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค). ํ•˜์ง€๋งŒ ํ”ผํ•  ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด IORef๊ฐ€ ์—ฌ๋Ÿฌ๋ถ„์„ ์œ„ํ•ด ์ค€๋น„๋˜์–ด ์žˆ๋‹ค. ๋™์‹œ์„ฑ์˜ ํ•จ์ • ์„œ๋ฌธ์—์„œ ๋งํ•˜์ง€ ์•Š์€ ์ค‘์š”ํ•œ ๊ฐ€๋ณ€ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์ฒ˜๊ฐ€ ์žˆ๋‹ค. ๋ฐ”๋กœ ๋™์‹œ์„ฑ์ด๋‹ค. ์ฆ‰ ์ปดํ“จํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ณ„์‚ฐ์„ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•˜๋Š” ์ƒํ™ฉ์ด๋‹ค. ๋™์‹œ์„ฑ ์‹œ๋‚˜๋ฆฌ์˜ค๋Š” ์‹œ์‹œํ•œ ๊ฒƒ(๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…์˜ ์ง„ํ–‰๋„๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์ง„ํ–‰ ๋ง‰๋Œ€)์—์„œ ๊ทนํžˆ ๋ณต์žกํ•œ ๊ฒƒ(์ˆ˜์ฒœ ๊ฐœ ์š”์ฒญ์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๋Š” ์„œ๋ฒ„๋‹จ ์†Œํ”„ํŠธ์›จ์–ด)์„ ์•„์šฐ๋ฅธ๋‹ค. ์›์น™์ ์œผ๋กœ ๋™์‹œ ๊ณ„์‚ฐ ์‚ฌ์ด์˜ ์‹คํ–‰ ์ˆœ์„œ๋Š” ์•„๋ฌด ๋ณด์žฅ๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด๋“ค ์‚ฌ์ด์˜ ์˜์‚ฌ์†Œํ†ต์—๋Š” ๊ฐ€๋ณ€ ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ ๋ณต์žกํ•ด์ง„๋‹ค. ๊ฐ€๋ณ€ ์ƒํƒœ๋ฅผ ํ™œ์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ๋Š” ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅํ•œ ์ˆœ๊ฐ„์˜ ๋…๋ฆฝ์ ์ธ ๊ณ„์‚ฐ์ด ์žˆ์„ ๋•Œ ์‹ฌ๊ฐํ•ด์ง„๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ณ„์‚ฐ A์—๋Š” ๊ณ„์‚ฐ B์˜ ๊ฒฐ๊ณผ๊ฐ€ ํ•„์š”ํ•œ๋ฐ, ์˜ˆ์ƒ๋ณด๋‹ค ์ผ์ฐ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์š”์ฒญํ•ด์„œ ๊ฐ€์งœ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ๋™์‹œ์„ฑ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๊ณ , ์ ์ ˆํ•œ ์กฐ์น˜๊ฐ€ ์—†์œผ๋ฉด ๋ฏธ๋ฌ˜ํ•œ ๋ฒ„๊ทธ๊ฐ€ ์‰ฝ๊ฒŒ ์นจํˆฌํ•œ๋‹ค. Data.IORef์—์„œ ๋™์‹œ์„ฑ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์•ˆ์ „์žฅ์น˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜๋Š” atomicallyModifyIORef์™€ atomicallyWriteIORef๋ฟ์ด๋‹ค. ์ด๊ฒƒ๋“ค์€ ๊ณ„์‚ฐ๋“ค ๊ฐ„์— ์œ ์ผํ•œ ๊ณต์œ  ์ž์›์ด ๋‹จ ํ•˜๋‚˜์˜ IORef ๋ฐ–์— ์—†๋Š” ์•„์ฃผ ๋‹จ์ˆœํ•œ ์ƒํ™ฉ์—์„œ๋‚˜ ๋„์›€์ด ๋œ๋‹ค. ํ•˜์Šค ์ผˆ ๋™์‹œ์„ฑ ์ฝ”๋“œ๋Š” ๋™์‹œ์„ฑ์„ ์œ„ํ•ด ๋งž์ถค ์ œ์ž‘๋œ ๋ณด๋‹ค ์ •๊ตํ•œ ๋„๊ตฌ๋“ค์„ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค. MVar(๊ฐ€๋ณ€ ๋ณ€์ˆ˜. ํ•œ ๊ณ„์‚ฐ์— ํ•„์š”ํ•œ ๋™์•ˆ์€ ๋‹ค๋ฅธ ๊ณ„์‚ฐ์—์„œ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋„๋ก ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. Control.Concurrent.MVar๋ฅผ ๋ณผ ๊ฒƒ)๋ผ๋˜๊ฐ€ stm ํŒจํ‚ค์ง€์˜ Control.Concurrent.STM(์†Œํ”„ํŠธ์›จ์–ด ํŠธ๋žœ์žญ์…˜ ๋ฉ”๋ชจ๋ฆฌ์˜ ๊ตฌํ˜„. ๋ชจ๋“  ๊ณต์œ  ๋ณ€์ˆ˜์˜ ๊ฐ€์šฉ์„ฑ์„ ๋ช…์‹œ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ณต์žกํ•จ์„ ๋œ์–ด์ฃผ๋ฉด์„œ ์•ˆ์ „ํ•œ ๋™์‹œ์„ฑ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋•๋Š” ๋™์‹œ์„ฑ ๋ชจ๋ธ)์ด ๋ฐ”๋กœ ๊ทธ๋Ÿฐ ๊ฒƒ๋“ค์ด๋‹ค 2. ST ๋ชจ๋‚˜๋“œ ์œ„์˜ IORef ์˜ˆ์ œ์—์„œ ๊ฐ€๋ณ€์„ฑ์€ ์™ธ๋ถ€์˜ ์š”์ธ ๋•Œ๋ฌธ์— ๊ฐ•์ œ๋˜์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์„œ๋ฌธ์˜ ๋‘ ์‹œ๋‚˜๋ฆฌ์˜ค(๊ฐ€๋ณ€์„ฑ๊ณผ ๊ทน์‹ฌํ•œ ๊ณ„์‚ฐ๋Ÿ‰์ด ํ•„์š”ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค)์˜ ๊ฒฝ์šฐ ๊ฐ€๋ณ€ ์ƒํƒœ๋Š” ๋‚ด๋ถ€ ์š”์ธ ๋•Œ๋ฌธ์— ํ•„์š”ํ•˜๋‹ค. ์ฆ‰ ์ตœ์ข… ๊ฒฐ๊ณผ์—๋Š” ๋ฐ˜์˜๋˜์ง€ ์•Š๋Š”๋‹ค. ๊ฐ€๋ น ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ์—๋Š” ๊ฐ€๋ณ€์„ฑ์ด ๊ทผ๋ณธ์ ์œผ๋กœ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฉฐ ๋”ฐ๋ผ์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜๊ณ  ์ƒˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์›์น™์ ์œผ๋กœ ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋‹ค. ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด๊ฐ€ ์›์†Œ ๊ฐ„ ์œ„์น˜๋ฅผ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด ํŒŒ๊ดด์  ๊ฐฑ์‹ ์„ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ๋ง์ด๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ ๊ฐ€๋ณ€์„ฑ์€ ๊ตฌํ˜„ ์„ธ๋ถ€์‚ฌํ•ญ์ผ ๋ฟ์ด๋‹ค. ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ์ด๋Ÿฐ ์ƒํ™ฉ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ›Œ๋ฅญํ•œ ๋„๊ตฌ๊ฐ€ ์žˆ๋‹ค. ๋ฐ”๋กœ Control.Monad.ST์˜ ST ๋ชจ๋‚˜๋“œ๋‹ค. data ST s a ST s a๋Š” State s a์™€ ๋น„์Šทํ•˜๊ฒŒ ๋ณด์ด๋Š”๋ฐ, ์‹ค์ œ๋กœ๋„ ๊ทธ๋ ‡๋‹ค. ST ๊ณ„์‚ฐ์€ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋†“๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๊ทธ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋ฅผ ์œ„ํ•ด Data.STRef๋Š” STRef๋ฅผ ์ œ๊ณตํ•œ๋‹ค. STRef s a๋Š” IORef a์™€ ๊ฐ™์ง€๋งŒ IO๊ฐ€ ์•„๋‹ˆ๋ผ ST s ๋ชจ๋‚˜๋“œ ๋‚ด๋ถ€์— ์žˆ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. ST๋ฅผ State๋‚˜ IO์™€ ๊ตฌ๋ณ„์ง“๋Š” ์ฃผ๋œ ์ฐจ์ด์ ์ด ์žˆ๋‹ค. Control.Monad.ST์—๋Š” runST ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. runST :: (forall s. ST s a) -> a ํƒ€์ž… ๋ช…์„ธ๊ฐ€ ์ถฉ๊ฒฉ์ ์ด๋‹ค. ST๊ฐ€ ๊ฐ€๋ณ€์„ฑ์„ ์ˆ˜๋ฐ˜ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ชจ๋‚˜๋“œ์—์„œ ๋‹จ์ˆœํžˆ ๊ฐ’์„ ๋ฝ‘์•„์˜ฌ ์ˆ˜ ์žˆ๋Š”๊ฐ€? ํ•ด๋‹ต์€ forall s.์— ์žˆ๋‹ค. ์ธ์ž์˜ ํƒ€์ž…์— forall s. ๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด ํƒ€์ž… ๊ฒ€์‚ฌ๊ธฐ์—๊ฒŒ ์ด๋ ‡๊ฒŒ ๋งํ•˜๋Š” ๊ฒƒ์ด๋‹ค. "s๋Š” ๋ฌด์—‡์ด๋“  ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด์— ๋Œ€ํ•ด ์–ด๋–ค ๊ฐ€์ •๋„ ํ•˜์ง€ ๋ง ๊ฒƒ." ํ•˜์ง€๋งŒ ์•„๋ฌด๋Ÿฐ ๊ฐ€์ •๋„ ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด s๋Š” ๋ฌด์—‡์—๋„ ๋งค์นญ๋  ์ˆ˜ ์—†๋‹ค๋Š” ๋œป์ด๋‹ค. ์‹ฌ์ง€์–ด runST๋ฅผ ๋‹ค๋ฅธ ๊ณณ์—์„œ ์‹คํ–‰ํ•  ๋•Œ์˜ s์™€๋„ ๋งค์นญํ•  ์ˆ˜ ์—†๋‹ค 3. GHCi> import Control.Monad.ST GHCi> import Data.STRef GHCi> -- Attempt to keep an STRef around to pass to pure code: GHCi> let ref = runST $ newSTRef (4 :: Int) <interactive>:125:19: Couldn't match type โ€˜aโ€™ with โ€˜STRef s Intโ€™ because type variable โ€˜sโ€™ would escape its scope This (rigid, skolem) type variable is bound by a type expected by the context: ST s a at <interactive>:125:11-37 Expected type: ST s a Actual type: ST s (STRef s Int) Relevant bindings include ref :: a (bound at <interactive>:125:5) In the second argument of โ€˜($)โ€™, namely โ€˜newSTRef (4 :: Int)โ€™ In the expression: runST $ newSTRef (4 :: Int) GHCi> -- The error message is quite clear: GHCi> -- "because type variable โ€˜sโ€™ would escape its scope" GHCi> -- Attempt to feed an STRef from one ST computation to another: GHCi> let x = runST $ readSTRef =<< runST (newSTRef (4 :: Int)) <interactive>:129:38: Couldn't match type โ€˜STRef s1 Intโ€™ with โ€˜ST s (STRef s a)โ€™ Expected type: ST s1 (ST s (STRef s a)) Actual type: ST s1 (STRef s1 Int) Relevant bindings include x :: a (bound at <interactive>:129:5) In the first argument of โ€˜runSTโ€™, namely โ€˜(newSTRef (4 :: Int))โ€™ In the second argument of โ€˜(=<<)โ€™, namely โ€˜runST (newSTRef (4 :: Int))โ€™ GHCi> -- The 's' from each computation are necessarily not the same. ์ด ํƒ€์ž… ๊ธฐ๊ต์˜ ๊ฒฐ๊ณผ๋กœ์„œ ๊ฐ ST ๊ณ„์‚ฐ ๋‚ด๋ถ€์— ๋‚ด๋ถ€ ์ƒํƒœ์™€ ๊ฐ€๋ณ€์„ฑ์ด ๊ฒฉ๋ฆฌ๋˜์–ด, ํ”„๋กœ๊ทธ๋žจ์˜ ๋‹ค๋ฅธ ๋ชจ๋“  ๋ถ€๋ถ„์—์„œ runST๋ฅผ ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋กœ ๋ณด๊ฒŒ ๋œ๋‹ค. ST์˜ ์‰ฌ์šด ์˜ˆ๋กœ ๋‹ค์Œ์€ ๋ฆฌ์ŠคํŠธ ํ•ฉ์˜ ๋ช…๋ นํ˜• ๋ฒ„์ „์ด๋‹ค[4]. import Control.Monad.ST import Data.STRef import Data.Foldable sumST :: Num a => [a] -> a sumST xs = runST $ do n <- newSTRef 0 for_ xs $ \x -> modifySTRef n (+x) readSTRef n ์–ด๋Š ๋ชจ๋กœ ๋ณด๋‚˜ sumST๋Š” ์ต์ˆ™ํ•œ sum ๋งŒํผ ์ˆœ์ˆ˜ํ•œ ํ•จ์ˆ˜๋‹ค. ๋ˆ„์‚ฐ๊ธฐ n์„ ํŒŒ๊ดด์  ๊ฐฑ์‹ ํ•˜๋Š” ๊ฒƒ์€ ๊ตฌํ˜„ ์„ธ๋ถ€์‚ฌํ•ญ์ผ ๋ฟ์ด๊ณ , n์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ตœ์ข… ๊ฒฐ๊ณผ ์™ธ์— ๋ˆ„์ถœ๋  ๊ธธ์€ ์—†๋‹ค. ์ด ๋‹จ์ˆœํ•œ ์˜ˆ์—์„œ๋Š” ST s a์˜ ํƒ€์ž… ๋ณ€์ˆ˜ s๊ฐ€ ๊ณ„์‚ฐ ๋‚ด์˜ ํŠน์ •ํ•œ ๊ฒƒ์— ๋Œ€์‘ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ด ํ™•์‹คํžˆ ๋“œ๋Ÿฌ๋‚œ๋‹ค. s๋Š” ์ธ๊ณต์ ์ธ ํ‘œ์‹์ผ ๋ฟ์ด๋‹ค. ์ฃผ๋ชฉํ•  ๋งŒํ•œ ๋˜ ๋‹ค๋ฅธ ์„ธ๋ถ€์‚ฌํ•ญ์€ for_๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์ ‘์ง€๋งŒ ํ•ฉ์€ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ˆ˜ํ–‰๋œ๋‹ค๋Š” ๊ฒƒ์œผ๋กœ, ๋ณ€๊ฒฝ์ด ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋‚˜์—ด๋œ ํ˜•ํƒœ์˜ applicative effect๋กœ์„œ ์ˆ˜ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ฐ€๋ณ€ ์ž๋ฃŒ๊ตฌ์กฐ ๊ฐ€๋ณ€ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ํ•„์š”์„ฑ์ด ์ž…์ฆ๋œ ์‚ฌ๋ก€์— ๋Œ€ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ€๋ณ€ ๋ฐฐ์—ด(๋ถˆ๋ณ€ ๋ฐฐ์—ด ํฌํ•จ)์€ GHC์— ๋™๋ด‰๋œ vector ํŒจํ‚ค์ง€๋‚˜ array ํŒจํ‚ค์ง€์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค 5. hashtables ํŒจํ‚ค์ง€์—์„œ๋Š” ๊ฐ€๋ณ€ ํ•ด์‹œ ํ…Œ์ด๋ธ”์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋‘ ST ๋ฒ„์ „๊ณผ IO ๋ฒ„์ „์„ ์ œ๊ณตํ•œ๋‹ค. ๋” ์ฝ์„๊ฑฐ๋ฆฌ Writing Yourself a Scheme in 48 Hours์˜ 7์žฅ์€ ์–ธ์–ด์—์„œ ๊ฐ€๋ณ€ ๋ณ€์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด IORef๋ฅผ ํ™œ์šฉํ•˜๋Š” ํฅ๋ฏธ๋กœ์šด ์˜ˆ์‹œ๋‹ค. Lennart Augustsson์˜ ๋ธ”๋กœ๊ทธ๋Š” ์ง„์งœ ํ€ต์†ŒํŠธ(์ฆ‰ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด ํŒŒ๊ดด์  ๊ฐฑ์‹ ์„ ํ•˜๋Š” ์›๋ณธ ์•Œ๊ณ ๋ฆฌ์ฆ˜)๋ฅผ ํ•˜์Šค์ผˆ์—์„œ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค€๋‹ค. ๊ทธ์˜ ๊ตฌํ˜„์€ ๊ฐ€๋ณ€์„ฑ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ ๊ฒฐํ•ฉ๊ธฐ ๋•์— ์ƒ๋‹นํžˆ ํฅ๋ฏธ๋กœ์šด๋ฐ, ํ•˜์Šค์ผˆ์„ C์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ž‘๋™ ์›๋ฆฌ๋ฅผ ์•Œ๋ ค๋ฉด ๋งํฌํ•œ ๊ธ€ ์•ž์˜ ๋‘ ๊ธ€์„ ํ™•์ธํ•  ๊ฒƒ. ๋…ธํŠธ ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฐ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์นญํ•˜๋Š” ๊ธฐ์ˆ ์  ์šฉ์–ด๋Š” ๋ฐ”์ธ๋”ฉ์ด๋‹ค. ๋ฐ”์ธ๋”ฉ์€ ์–‡์•„์„œ ์›๋ž˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ตฌ์กฐ๋ฅผ ํ™˜ํ•˜๊ฒŒ ๋“œ๋Ÿฌ๋‚ผ ์ˆ˜๋„ ์žˆ๊ณ  ์ถ”์ƒํ™” ๊ณ„์ธต์„ ๋”ํ•ด์„œ ๋ณด๋‹ค ํ•˜์Šค์ผˆ์Šค๋Ÿฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ๋ฐ”์ธ๋”ฉ์„ ๋งŒ๋“œ๋Š” ๊ธฐ๋ณธ์ ์ธ ๋„๊ตฌ๋Š” FFI(foreign function interface)๋กœ, ํ•˜์Šค ์ผˆ ์‹ค์ „์—์„œ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. โ†ฉ ์ด ์ฑ…์˜ ๋’ท๋ถ€๋ถ„์—์„œ ๊ทธ ํŠน์ง•๋“ค ์ค‘ ์ผ๋ถ€๋ฅผ ์†Œ๊ฐœํ•  ๊ฒƒ์ด๋‹ค. โ†ฉ ์ด๋Š” ์กด์žฌ์  existential ํƒ€์ž…์˜ ์˜ˆ์‹œ๋‹ค. "existential"์€ ์šฐ๋ฆฌ๊ฐ€ ์•„๋Š” ๊ฒƒ์ด๋ผ๊ณ ๋Š” ์ด๊ฒƒ์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ๋ฟ์ž„์„ ๋œปํ•œ๋‹ค. โ†ฉ ํ•˜์Šค์ผˆ์œ„ํ‚ค์˜ ST์— ๊ด€ํ•œ ํŽ˜์ด์ง€์—์„œ ๊ฐ€์ ธ์˜ด. โ†ฉ ๋ฐฐ์—ด์— ๊ด€ํ•œ ์ผ๋ฐ˜์ ์ธ ์„ค๋ช…์€ ์ž๋ฃŒ๊ตฌ์กฐ ์ž…๋ฌธ์„ ๋ณผ ๊ฒƒ. โ†ฉ 11 ๋™์‹œ์„ฑ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Concurrency ๋™์‹œ์„ฑ ์–ธ์ œ ๋™์‹œ์„ฑ์ด ํ•„์š”ํ•œ๊ฐ€? ์˜ˆ์ œ ์†Œํ”„ํŠธ์›จ์–ด ํŠธ๋žœ์žญ์…˜ ๋ฉ”๋ชจ๋ฆฌ ๋™์‹œ์„ฑ ํ•˜์Šค์ผˆ์—์„œ ๋™์‹œ์„ฑ์€ ๋Œ€๋ถ€๋ถ„ ํ•˜์Šค ์ผˆ ์Šค๋ ˆ๋“œ์— ์˜ํ•ด ์ด๋ค„์ง„๋‹ค. ํ•˜์Šค ์ผˆ ์Šค๋ ˆ๋“œ๋Š” ๋Ÿฐํƒ€์ž„์— ๊ตฌํ˜„๋˜๋Š” ์œ ์ € ์ŠคํŽ˜์ด์Šค ์Šค๋ ˆ๋“œ๋‹ค. ํ•˜์Šค ์ผˆ ์Šค๋ ˆ๋“œ๋Š” OS ์Šค๋ ˆ๋“œ๋ณด๋‹ค ์‹œ๊ฐ„ ๋ฐ ๊ณต๊ฐ„ ์ธก๋ฉด์—์„œ ํ›จ์”ฌ ํšจ์œจ์ ์ด๋‹ค. ์„ธ๋งˆํฌ์–ด ๊ฐ™์€ ์ „ํ†ต์ ์ธ ๋™๊ธฐํ™” ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ ์™ธ์—๋„ ํ•˜์Šค์ผˆ์€ ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•œ ๋™์‹œ ์ ‘๊ทผ์„ ํฌ๊ฒŒ ๋‹จ์ˆœํ™”ํ•˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด ํŠธ๋žœ์žญ์…˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ๋™์‹œ์„ฑ์„ ์œ„ํ•œ ๋ชจ๋“ˆ๋กœ Control.Concurrent.*์™€ Control.Monad.STM์ด ์žˆ๋‹ค. ์–ธ์ œ ๋™์‹œ์„ฑ์ด ํ•„์š”ํ•œ๊ฐ€? ์•„๋งˆ ์–ธ์ œ ํ•„์š”ํ•œ์ง€ ๋ณด๋‹ค๋Š” ์–ธ์ œ ํ•„์š”ํ•˜์ง€ ์•Š์€ ์ง€๊ฐ€ ๋” ์ค‘์š”ํ•  ๊ฒƒ์ด๋‹ค. ํ•˜์Šค์ผˆ์˜ ๋™์‹œ์„ฑ์€ ๋ฉ€ํ‹ฐํ”„๋กœ์„ธ์„œ ์ฝ”์–ด๋“ค์„ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒƒ์—๋Š” "๋ณ‘๋ ฌํ™”"๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๋™์‹œ์„ฑ์€ ๋‹จ์ผ ์ฝ”์–ด๊ฐ€ ๊ทธ ๊ด€์‹ฌ์‚ฌ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐ€์ง€, ๋ณดํ†ต์€ IO๋กœ ๋ถ„์‚ฐํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ„๋‹จํ•œ "์ •์ " ์›น์„œ๋ฒ„(์ฆ‰ ์ด๋ฏธ์ง€ ๊ฐ™์€ ์ •์  ์ฝ˜ํ…์ธ ๋งŒ ์ œ๊ณตํ•˜๋Š”)๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ์ด์ƒ์ ์œผ๋กœ๋Š” ๊ทธ๋Ÿฐ ์›น์„œ๋ฒ„๋Š” ํ”„๋กœ์„ธ์‹ฑ ๋ฆฌ์†Œ์Šค๋ฅผ ์ ๊ฒŒ ๋จน์–ด์•ผ ํ•œ๋‹ค. ๋Œ€์‹  ๋ฐ์ดํ„ฐ๋ฅผ ์ตœ๋Œ€ํ•œ ๋น ๋ฅด๊ฒŒ ์ „์†กํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋ณ‘๋ชฉ์€ ๋ถ„๋ช… ๋” ๋งŽ์€ ํ•˜๋“œ์›จ์–ด๋ฅผ ์Ÿ์•„๋ถ€์„ ์ˆ˜ ์žˆ๋Š” I/O ์ผ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ ์ปค๋„ฅ์…˜์— ๋Œ€ํ•ด ๋‹จ์ผ ํ”„๋กœ์„ธ์„œ ์ฝ”์–ด๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌํ•œ ์›น์„œ๋ฒ„์˜ C ๋ฒ„์ „์ด๋ผ๋ฉด ๊ฐ ์ปค๋„ฅ์…˜๊ณผ ๋ฆฌ์Šค๋‹ ์†Œ์ผ“์— ๋Œ€ํ•ด select()๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๊ฑฐ๋Œ€ํ•œ ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๊ฒƒ์ด๋‹ค. ๊ฐ ์˜คํ”ˆ ์ปค๋„ฅ์…˜์€ ๊ทธ ์ปค๋„ฅ์…˜์˜ ์ƒํƒœ๋ฅผ ๊ธฐ์ˆ ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ฐ€์กŒ์„ ๊ฒƒ์ด๋‹ค. (์ฆ‰ HTTP ํ—ค๋”๋ฅผ ๋ฐ›์•„ ํŒŒ์‹ฑํ•˜๊ณ  ํŒŒ์ผ์„ ๋ณด๋‚ด๋Š”) ๊ทธ๋Ÿฐ ๊ฑฐ๋Œ€ ๋ฃจํ”„๋Š” ์ง์ ‘ ์ฝ”๋”ฉํ•˜๊ธฐ์—๋Š” ์–ด๋ ต๊ณ  ์˜ค๋ฅ˜์— ์ทจ์•ฝํ•˜๋‹ค. ํ•˜์ง€๋งŒ ํ•˜์Šค ์ผˆ ๋™์‹œ์„ฑ์„ ํ™œ์šฉํ•˜๋ฉด ๋ฆฌ์Šค๋‹ ์†Œ์ผ“์—๋งŒ ์ง‘์ค‘ํ•˜๋Š” ๋ณด๋‹ค ์ž‘์€ ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฃจํ”„์—์„œ๋Š” ์ˆ˜๋ฝ๋œ ์ปค๋„ฅ์…˜๋งˆ๋‹ค ์ƒˆ๋กœ์šด "์Šค๋ ˆ๋“œ"๋ฅผ ์Šคํฐ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด ์ƒˆ๋กœ์šด "์Šค๋ ˆ๋“œ"๋ฅผ IO ๋ชจ๋‚˜๋“œ ์•ˆ์—์„œ HTTP ํ—ค๋”๋ฅผ ๋ฐ›๊ณ , ํŒŒ์‹ฑํ•˜๊ณ , ํŒŒ์ผ์„ ๋ณด๋‚ด๋„๋ก ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์Šค๋ ˆ๋“œ์˜ ์Šคํฐ์„ "์Šค๋ ˆ๋“œ"์˜ ์ƒํƒœ๋ฅผ ๊ธฐ์ˆ ํ•˜๋Š” ์ž‘์€ ๊ตฌ์กฐ์ฒด์˜ ํ• ๋‹น์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ์ด ๊ตฌ์กฐ์ฒด๋Š” C์—์„œ ์ •์˜ํ–ˆ์„ ๋ฒ•ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์œ ์‚ฌํ•˜๋‹ค. ๊ทธ๋Ÿฌ๊ณ ๋Š” ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๋ฅผ ํ•˜๋‚˜์˜ ๊ฑฐ๋Œ€ ๋ฃจํ”„๋กœ ๋ณ€ํ™˜ํ•  ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฐ ์Šค๋ ˆ๋“œ๊ฐ€ ๋…๋ฆฝ์ ์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•ด๋„ ๋‚ด๋ถ€์ ์œผ๋กœ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๊ทธ๊ฑธ select()๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ํ•˜๋Š” ํ•˜๋‚˜์˜ ๊ฑฐ๋Œ€ ๋ฃจํ”„ ๋˜๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ์‹œ์Šคํ…œ์— ์ตœ์„ ์ธ ๋ฌด์–ธ๊ฐ€๋กœ ๋ณ€ํ™˜ํ•  ๊ฒƒ์ด๋‹ค. ์˜ˆ์ œ ์˜ˆ์ œ: ํŒŒ์ผ์˜ ๋ณ‘๋ ฌ ๋‹ค์šด๋กœ๋“œ downloadFile :: URL -> IO () downloadFile = undefined downloadFiles :: [URL] -> IO () downloadFiles = mapM_ (forkIO . downloadFile) ์†Œํ”„ํŠธ์›จ์–ด ํŠธ๋žœ์žญ์…˜ ๋ฉ”๋ชจ๋ฆฌ ์†Œํ”„ํŠธ์›จ์–ด ํŠธ๋žœ์žญ์…˜ ๋ฉ”๋ชจ๋ฆฌ(STM)๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŠธ๋žœ์žญ์…˜๊ณผ ๋น„์Šทํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์—์„œ์˜ ํŠธ๋žœ์žญ์…˜์„ ์œ„ํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด๋‹ค. STM์€ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋”ฉ ํ™˜๊ฒฝ์—์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ๋•Œ ๊ณต์œ  ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ํฌ๊ฒŒ ๊ฐ„์†Œํ™”ํ•œ๋‹ค. STM์„ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ด์ƒ ์ž ๊ธˆ(locking)์— ์˜์กดํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. STM์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Control.Concurrent.STM์„ ์ž„ํฌํŠธ ํ•ด์•ผ ํ•œ๋‹ค. STM ๋ชจ๋‚˜๋“œ ๋‚ด๋ถ€๋กœ ๋“ค์–ด๊ฐ€๋ ค๋ฉด atomically ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. STM์€ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์„ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ํ”„๋ฆฌ๋ฏธํ‹ฐ๋ธŒ(TVar, TMVar, TChan, TArray)๋ฅผ ์ œ๊ณตํ•œ๋‹ค. 2 ํƒ€์ž…๊ณผ์˜ ์œ ํฌ ํƒ€์ž…๊ณผ์˜ ์œ ํฌ ๋‹คํ˜•์„ฑ ๊ธฐ์ดˆ Existentially qualified types ํƒ€์ž… ํด๋ž˜์Šค ๊ณ ๊ธ‰ ํŒฌํ…€ ํƒ€์ž… ์ผ๋ฐ˜ํ™”๋œ ๋Œ€์ˆ˜์  ๋ฐ์ดํ„ฐ ํƒ€์ž… (GADT) ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋Œ€์ˆ˜ ์›๋ฌธ์ด ์—†์Œ ํƒ€์ž… ์ƒ์„ฑ์ž & ์ข…(kind) 1 ๋‹คํ˜•์„ฑ ๊ธฐ์ดˆ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Polymorphism TODO ์›๋ฌธ๋„ ๋ฏธ์™„์„ฑ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. (2021-09-30 ๋งˆ์ง€๋ง‰ ํ™•์ธ) ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‹คํ˜•์„ฑ forall a Higher rank types runST ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅ์„ฑ System F ์˜ˆ์ œ ๋‹คํ˜•์„ฑ์˜ ๋‹ค๋ฅธ ํ˜•ํƒœ ๋…ธํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‹คํ˜•์„ฑ forall a ์—ฌ๋Ÿฌ๋ถ„์€ ์•„๋งˆ ๋‹คํ˜• ํ•จ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๋‹ค๋ฅธ ํƒ€์ž…์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋Š” ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์„ ๊ฒƒ์ด๋‹ค. ํ•œ ์˜ˆ์‹œ๋กœ length :: [a] -> Int ๋Š” ๋ฌธ์ž์—ด String = [Char]์ด๋“  ์ˆซ์ž๋“ค์˜ ๋ฆฌ์ŠคํŠธ [Int]์ด๋“  ์–ด๋–ค ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํƒ€์ž… ๋ณ€์ˆ˜ a๋Š” length๊ฐ€ ์–ด๋–ค ํƒ€์ž…์ด๋“  ๋ฐ›์•„๋“ค์ž„์„ ๋œปํ•œ๋‹ค. ๋‹ค๋ฅธ ๋‹คํ˜• ํ•จ์ˆ˜์˜ ์˜ˆ๋Š” fst :: (a, b) -> a snd :: (a, b) -> b map :: (a -> b) -> [a] -> [b] ํƒ€์ž… ๋ณ€์ˆ˜๋Š” ํ•ญ์ƒ ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ˜๋ฉด Int๋‚˜ String ๊ฐ™์€ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์€ ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ๋‘˜์€ ์ด๋ ‡๊ฒŒ ๊ตฌ๋ถ„ํ•œ๋‹ค. a๊ฐ€ ์ž„์˜์˜ ํƒ€์ž…์ด ๋  ์ˆ˜ ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋” ๋ช…์‹œ์ ์ธ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. length :: forall a. [a] -> Int ๋‹ฌ๋ฆฌ ๋งํ•˜๋ฉด, "๋ชจ๋“  ํƒ€์ž… a์— ๋Œ€ํ•ด length ํ•จ์ˆ˜๋Š” a ํƒ€์ž…์ธ ์›์†Œ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด์„œ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค." ์ด์ „์˜ ํƒ€์ž… ๋ช…์„ธ๋Š” forall1์„ ์“ฐ๋Š” ์ƒˆ๋กœ์šด ๋ช…์„ธ์˜ ์ถ•์•ฝ ํ‘œํ˜„์œผ๋กœ ์ƒ๊ฐํ•˜๋ผ. ์ฆ‰ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋น ๋œจ๋ฆฐ forall์„ ๋‚ด๋ถ€์—์„œ ๋ผ์›Œ ๋„ฃ์„ ๊ฒƒ์ด๋‹ค. ๋‹ค๋ฅธ ์˜ˆ๋กœ fst์˜ ํƒ€์ž… ๋ช…์„ธ๋Š” ๋‹ค์Œ์„ ์ค„์ธ ๊ฒƒ์ด๋‹ค. fst :: forall a. forall b. (a, b) -> a ๋˜๋Š” ๋‹ค์Œ๊ณผ ๋™์น˜๋‹ค. fst :: forall a b. (a, b) -> a ๋น„์Šทํ•˜๊ฒŒ map์˜ ํƒ€์ž…์€ ์‚ฌ์‹ค ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. map :: forall a b. (a -> b) -> [a] -> [b] ๋ฌด์–ธ๊ฐ€๋ฅผ ๋ชจ๋“  ํƒ€์ž…์— ์ ์šฉํ•œ๋‹ค๊ฑฐ๋‚˜ ๋ชจ๋“  ๊ฒƒ์— ๋Œ€ํ•ด ์„ฑ๋ฆฝํ•œ๋‹ค๋Š” ๋ฐœ์ƒ์„ universal quantification์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ˆ˜๋ฆฌ๋…ผ๋ฆฌ์—์„œ โˆ€2 ๊ธฐํ˜ธ(A๋ฅผ ๋’ค์ง‘์€ ๊ฒƒ. "forall"์œผ๋กœ ์ฝ๋Š”๋‹ค)๊ฐ€ ํ”ํžˆ ๊ทธ๋Ÿฐ ์˜๋ฏธ๋กœ ์“ฐ์ด๊ณ  universal quantifier๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค. Higher rank types ๋ช…์‹œ์ ์ธ forall์„ ์“ฐ๋ฉด ๋‹คํ˜• ์ธ์ž๋ฅผ ์ทจํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด foo :: (forall a. a -> a) -> (Char, Bool) foo f = (f 'c', f True) ์—ฌ๊ธฐ์„œ f๋Š” ๋‹คํ˜• ํ•จ์ˆ˜๊ณ  ๋ชจ๋“  ๊ฒƒ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ f๋Š” ๋ฌธ์ž c์™€ ์ด์ง„ ๊ฐ’ True ๋‘˜ ๋‹ค์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Haskell98์—์„œ๋Š” foo ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์—†๋Š”๋ฐ, ํƒ€์ž… ๊ฒ€์‚ฌ๊ธฐ๊ฐ€ f๋Š” Char ํƒ€์ž… ๋˜๋Š” Bool ํƒ€์ž…์˜ ๊ฐ’์—๋งŒ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋ถˆํ‰ํ•˜๊ณ  ๋”ฐ๋ผ์„œ ์ด ์ •์˜๋ฅผ ๊ฑฐ๋ถ€ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋‚˜๋งˆ ๊ฐ€์žฅ ์œ ์‚ฌํ•˜๊ฒŒ ํƒ€์ž…์„ ์จ๋ณด์ž๋ฉด, bar :: (a -> a) -> (Char, Bool) ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. bar :: forall a. ((a -> a) -> (Char, Bool)) ํ•˜์ง€๋งŒ ์ด๊ฒƒ์€ foo์™€ ๋งค์šฐ ๋‹ค๋ฅด๋‹ค. ๊ฐ€์žฅ ๋ฐ”๊นฅ์˜ forall์€ f๊ฐ€ bar์—๊ฒŒ ์•Œ๋ ค์ง€์ง€ ์•Š์€ ์–ด๋–ค ํƒ€์ž… a์— ๋Œ€ํ•ด a -> a ํ˜•ํƒœ์ธ ํ•œ, bar๊ฐ€ ์–ด๋–ค ์ธ์ž f์™€๋„ ์ž‘๋™ํ•จ์„ ์•ฝ์†ํ•œ๋‹ค. foo์™€ ๋น„๊ตํ•ด ๋ณด๋ฉด a -> a ํ˜•ํƒœ์ž„์„ ์•ฝ์†ํ•˜๋Š” ์ฃผ์ฒด๊ฐ€ ์ธ์ž f์ด๊ณ , ๊ทธ ์•ฝ์†์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์€ a = Char์™€ a = Bool์„ ์„ ํƒํ•˜๋Š” foo๋‹ค. ๋ช…๋ช…๋ฒ•์— ์˜ํ•˜๋ฉด bar ๊ฐ™์€ ๋‹จ์ˆœ ๋‹คํ˜• ํ•จ์ˆ˜๋Š” ๋žญํฌ-1 ํƒ€์ž…์ด๋ผ ๋ถ€๋ฅด๊ณ  foo์˜ ํƒ€์ž…์€ ๋žญํฌ-2 ํƒ€์ž…์œผ๋กœ ๋ถ„๋ฅ˜๋œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋žญํฌ-n ํƒ€์ž…์€ ๋žญํฌ-(n-1) ์ธ์ž๊ฐ€ ์ตœ์†Œ ํ•œ ๊ฐœ ์žˆ์ง€๋งŒ ๋žญํฌ๊ฐ€ ๋” ๋†’์€ ์ธ์ž๋Š” ์—†๋Š” ํ•จ์ˆ˜๋‹ค. ๊ณ ๋žญํฌ ํƒ€์ž…์˜ ์ด๋ก ์ <NAME> System F์œผ๋กœ์„œ 2์ฐจ ๋žŒ๋‹ค ๋Œ€์ˆ˜๋ผ๊ณ ๋„ ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ์ด์— ๊ด€ํ•ด์„œ๋Š” forall์˜ ์˜๋ฏธ, foo๋‚˜ bar์—์„œ forall์˜ ์œ„์น˜๊ฐ€ ๊ฐ€์ง€๋Š” ๋œป์„ ๋” ์ž˜ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด System F ์ ˆ์—์„œ ์ž์„ธํžˆ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. Haskell98์€ Hindley-Milner ํƒ€์ž… ์ฒด๊ณ„์— ๊ธฐ๋ฐ˜ํ•œ๋‹ค. ์ด ์ฒด๊ณ„๋Š” System F์˜ ํ•˜์œ„๋กœ forall๊ณผ ๋žญํฌ-2 ์ด์ƒ์˜ ํƒ€์ž…์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์–ธ์–ด ํ™•์žฅ RankNTypes3์„ ํ™œ์„ฑํ™”ํ•ด์•ผ System F์˜ ์ „๋ถ€๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ Haskell98์ด ๊ณ ๋žญํฌ ํƒ€์ž…์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ์ด์œ ๊ฐ€ ์žˆ๋Š” ๋ฒ•์ด๋‹ค. ์™„์ „ํ•œ System F๋ฅผ ์œ„ํ•œ ํƒ€์ž… ์ถ”๋ก ์€ ๊ฒฐ์ • ๋ถˆ๊ฐ€๋Šฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋ชจ๋“  ํƒ€์ž… ๋ช…์„ธ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ดˆ๊ธฐ ํ•˜์Šค์ผˆ์€ ๋‹จ์ˆœํ•œ ๋‹คํ˜• ํ•จ์ˆ˜๋งŒ์„ ์ œ๊ณตํ•˜๋Š” Hindley-Milner ํƒ€์ž… ์ฒด๊ณ„๋ฅผ ์ฑ„ํƒํ–ˆ๊ณ , ๊ทธ ๋Œ€์‹  ์™„๋ฒฝํ•œ ํƒ€์ž… ์ถ”๋ก ์„ ์ œ๊ณตํ–ˆ๋‹ค. ์ตœ๊ทผ ์—ฐ๊ตฌ์˜ ๋ฐœ๋‹ฌ๋กœ ํƒ€์ž… ๋ช…์„ธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ถ€๋‹ด์ด ์ค„์–ด ์š”์ฆ˜ ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ์—์„œ๋Š” ๋žญํฌ-n ํƒ€์ž…์„ ์‹ค์šฉ์ ์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค. runST ํ•˜์Šค ์ผˆ ์‹ค์ „ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋ผ๋ฉด ST ๋ชจ๋‚˜๋“œ๋กœ ๋žญํฌ-2 ํƒ€์ž…์„ ์ฒ˜์Œ ์ ‘ํ–ˆ์„ ๊ฒƒ์ด๋‹ค. ST ๋ชจ๋‚˜๋“œ๋Š” IO ๋ชจ๋‚˜๋“œ์™€ ๋น„์Šทํ•˜๊ฒŒ ๊ฐ€๋ณ€ ์ฐธ์กฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. newSTRef :: a -> ST s (STRef s a) readSTRef :: STRef s a -> ST s a writeSTRef :: STRef s a -> a -> ST s () ๊ทธ๋ฆฌ๊ณ  ๊ฐ€๋ณ€ ๋ฐฐ์—ด๋„ ์ง€์›ํ•œ๋‹ค. ํƒ€์ž… ๋ณ€์ˆ˜ s๋Š” ์กฐ์ž‘ํ•  ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ํ•˜์ง€๋งŒ IO์™€ ๋‹ฌ๋ฆฌ ์ด ์ƒํƒœ ์žˆ๋Š” ๊ณ„์‚ฐ์€ ์ˆœ์ˆ˜ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ ๋‹ค์Œ ํ•จ์ˆ˜๋Š” runST :: (forall s. ST s a) -> a ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•˜๊ณ , ๊ณ„์‚ฐ์„ ์‹คํ–‰ํ•˜๊ณ , ์ƒํƒœ๋ฅผ ์ œ๊ฑฐํ•œ ๋‹ค์Œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ณด๋“ฏ์ด ๋žญํฌ-2 ํƒ€์ž…์ด๋‹ค. ์™œ? ์š”์ ์€ ๊ฐ€๋ณ€ ์ฐธ์กฐ๊ฐ€ ํ•œ runST์— ๊ตญํ•œ๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด v = runST (newSTRef "abc") foo = runST (readSTRef v) ์ด๊ฒƒ์€ ์ž˜๋ชป๋˜์—ˆ๋Š”๋ฐ ๊ฐ€๋ณ€ ์ฐธ์กฐ๋ฅผ ํ•œ runST์˜ ๋ฌธ๋งฅ์—์„œ ์ƒ์„ฑํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ runST์—์„œ ์žฌ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด (forall s. ST s a) -> a์—์„œ ์ตœ์ข… ํƒ€์ž…์ธ a๋Š” v์˜ ๊ฒฝ์šฐ STRef s String์ด ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋žญํฌ-2 ํƒ€์ž…์ด ๊ทธ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค! ์ธ์ž๊ฐ€ s์— ๋Œ€ํ•ด ๋‹ค ํ˜•์ด์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ์ƒํƒœ s์— ๋Œ€ํ•ด ๊ฐ™์€ ํƒ€์ž…์ธ a๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. ์ตœ์ข… a๋Š” ์ƒํƒœ์™€ ๋ฌด๊ด€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์˜ ์ฝ”๋“œ๋Š” ํƒ€์ž… ์˜ค๋ฅ˜๋ฅผ ํ’ˆ๊ณ  ์žˆ๊ณ  ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๊ฑฐ๋ถ€ํ•  ๊ฒƒ์ด๋‹ค. ST ๋ชจ๋‚˜๋“œ์— ๋Œ€ํ•œ ๋” ์ž์„ธํ•œ ์„ค๋ช…์€ ์›๋…ผ๋ฌธ Lazy functional state threads์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.4 ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅ์„ฑ ์˜ˆ์ธก ๊ฐ€๋Šฅ = ํƒ€์ž… ๋ณ€์ˆ˜๊ฐ€ ๋‹จ์ผ ํƒ€์ž…์œผ๋กœ ์ธ์Šคํ„ด์Šคํ™”๋จ. ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅ = ๋‹คํ˜• ํƒ€์ž…. ์˜ˆ: length [id :: forall a. a -> a] ๋˜๋Š” Just (id :: forall a. a -> a). ๊ณ ๋žญํฌ์—์„œ ๋ฏธ๋ฌ˜ํ•˜๊ฒŒ ๋‹ค๋ฆ„. ๋‹คํ˜• ํƒ€์ž…๋“ค์˜ ์ผ๋ฐ˜์„ฑ์— ์˜ํ•œ ๊ด€๊ณ„, ์ฆ‰ isInstanceOf. ํ•˜์Šค ์ผˆ ์นดํŽ˜: RankNTypes ํ† ๋ง‰ ์„ค๋ช… System F Section goal = ์•”๋ฌต์  ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜ ์ „๋‹ฌ์ด ๋‡Œ์— ์ค„ ํƒ€๊ฒฉ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๋žŒ๋‹ค ๋Œ€์ˆ˜ ๊ธฐ์ดˆ System F = ์ด ๋ชจ๋“  โˆ€์˜ ๊ทผ๊ฐ„์ด ๋˜๋Š” ๊ฒƒ ๋ช…์‹œ์  ํƒ€์ž… ์‘์šฉ. map Int (+1) [1,2,3] ๊ฐ™์€ ๊ฑฐ. โˆ€๋Š” ํ•จ์ˆ˜ ์• ๋กœ ->์™€ ์œ ์‚ฌ. ํƒ€์ž…์— ๋”ฐ๋ฅธ ์šฉ์–ด. ํƒ€์ž… ์ธ์ž์—๋Š” ๋Œ€๋ฌธ์ž ฮ›, ๊ฐ’ ์ธ์ž์—๋Š” ์†Œ๋ฌธ์ž ฮป. ์˜ˆ์ œ ๋…์ž๊ฐ€ โˆ€๊ฐ€ ๋“ค์–ด๊ฐ„ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ• ์ง€ ๋ง์ง€ ์Šค์Šค๋กœ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ. Church numerals, Encoding of arbitrary recursive types (positivity conditions): &forall x. (F x -> x) -> x Continuations, Pattern-matching: maybe, either and foldr ๋‹คํ˜•์„ฑ์˜ ๋‹ค๋ฅธ ํ˜•ํƒœ Section goal = OOP์˜ ๋‹คํ˜•์„ฑ๊ณผ ๋น„๊ต. ํƒ€์ž… ํด๋ž˜์Šค์™€์˜ ์–ด์šธ๋ฆผ. ad-hoc polymorphism = ํƒ€์ž… s์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋™์ž‘ => ํ•˜์Šค ์ผˆ ํƒ€์ž… ํด๋ž˜์Šค parametric polymorphism = ์‹ค์ œ ์“ฐ์ด๋Š” ํƒ€์ž…๊ณผ ๋ฌด๊ด€ => โˆ€ subtyping ๋…ธํŠธ ํ‚ค์›Œ๋“œ forall์€ ํ•˜์Šค ์ผˆ 98 ํ‘œ์ค€์ด ์•„๋‹ˆ๋‹ค. ์–ธ์–ด ํ™•์žฅ ScopedTypeVariables, Rank2Types, RankNTypes ๋“ฑ์ด ์ปดํŒŒ์ผ๋Ÿฌ์—์„œ forall์„ ํ™œ์„ฑํ™”ํ•œ๋‹ค. ๋ฏธ๋ž˜์˜ ํ•˜์Šค ์ผˆ ํ‘œ์ค€์€ ์ด ์ค‘ ํ•˜๋‚˜๋ฅผ ํฌํ•จํ•  ๊ฒƒ์ด๋‹ค. โ†ฉ UnicodeSyntax ํ™•์žฅ์€ ํ•˜์Šค ์ผˆ ์†Œ์Šค ์ฝ”๋“œ์—์„œ forall ํ‚ค์›Œ๋“œ ๋Œ€์‹  โˆ€ ๊ธฐํ˜ธ๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. โ†ฉ ๋˜๋Š” ๋žญํฌ-2 ํƒ€์ž…๋งŒ ์›ํ•œ๋‹ค๋ฉด Rank2Types๋ฅผ ํ™œ์„ฑํ™”ํ•  ๊ฒƒ. โ†ฉ John Launchbury; Simon Peyton Jones (1994-??-??). Lazy functional state threads. ACM Press". pp. 24-35. โ†ฉ 2 Existentially qualified types ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Existentially_quantified_types forall ํ‚ค์›Œ๋“œ ์˜ˆ: ํ˜ผ์ข… ๋ฆฌ์ŠคํŠธ ์กด์žฌ์ ์ด๋ผ๋Š” ์šฉ์–ด์— ๋Œ€ํ•œ ์„ค๋ช… ์˜ˆ: runST ์›์‹œํ˜•์œผ๋กœ์„œ์˜ ํ•œ์ • ๋” ์ฝ์„๊ฑฐ๋ฆฌ Existential type, ์งง๊ฒŒ๋Š” existential๋Š” ํƒ€์ž…๋“ค์˜ ๋ชจ์Œ์„ ํ•˜๋‚˜์˜ ํƒ€์ž…์œผ๋กœ ์ฐŒ๋ถ€๋Ÿฌ๋œจ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. existential์€ GHC์˜ ํƒ€์ž… ์ฒด๊ณ„ ํ™•์žฅ์ด๋‹ค. ํ•˜์Šค ์ผˆ 98์—๋Š” ํฌํ•จ๋˜์ง€ ์•Š๊ณ  existential์„ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•˜๋ ค๋ฉด ์ปค๋งจ๋“œ ๋ผ์ธ ์ธ์ž๋กœ -XExistentialQuantification์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์†Œ์Šค ์ฝ”๋“œ ์œ—๋ถ€๋ถ„์— {-# LANGUAGE ExistentialQuantification #-}์„ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค. forall ํ‚ค์›Œ๋“œ forall ํ‚ค์›Œ๋“œ๋Š” ํƒ€์ž… ๋ณ€์ˆ˜๋ฅผ ์Šค์ฝ”ํ”„์— ๋ช…์‹œ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ์„ ์ˆ˜๋ฐฑ ๋ฒˆ๋„ ๋” ๋ดค์„ ๊ฒƒ์ด๋‹ค. ์˜ˆ: ๋‹คํ˜• ํ•จ์ˆ˜ map :: (a -> b) -> [a] -> [b] ์—ฌ๊ธฐ์„œ a์™€ b๋Š” ๋ฌด์—‡์ธ๊ฐ€? ๋ญ, ํƒ€์ž… ๋ณ€์ˆ˜ ์•„๋‹ˆ๊ฒ ๋‚˜. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ด๊ฒƒ๋“ค์ด ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๋ณด๊ณ  ์–ด๋Š ํƒ€์ž…์ด๋“  ์ฑ„์›Œ์ง€๋„๋ก ํ—ˆ์šฉํ•œ๋‹ค. ์ด๋ฅผ ๋‹ค๋ฅด๊ฒŒ ๋ณด๋Š” ๋ฐฉ๋ฒ•์€ ์ด ๋ณ€์ˆ˜๋“ค์ด universally quantified๋ผ๊ณ  ๋ณด๋Š” ๊ฒƒ์ด๋‹ค.<NAME> ๋…ผ๋ฆฌ๋ฅผ ๋ฐฐ์› ๋‹ค๋ฉด '๋ชจ๋“  ...์— ๋Œ€ํ•ด' ( ) ๋˜๋Š” `... ๊ฐ€ ์กด์žฌํ•œ๋‹ค' ( ) ๊ฐ™์€ ํ•œ์ •์ž๋ฅผ ๋ดค์„ ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ๋“ค์€ ๊ทธ๋‹ค์Œ์— ์˜ค๋Š” ๊ฒƒ๋“ค์„ 'ํ•œ์ •'ํ•œ๋‹ค. ( )๋Š” ๋‹ค์Œ์— ์˜ค๋Š” ๊ฒƒ์ด ์˜ค์ง ํ•˜๋‚˜์˜ x์— ๋Œ€ํ•ด์„œ๋งŒ ์ฐธ์ž„์„ ๋œปํ•œ๋‹ค. ( ) ์€ ๋‹ค์Œ์— ์˜ค๋Š” ๊ฒƒ์ด ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  x์— ๋Œ€ํ•ด ์ฐธ์ž„์„ ๋œปํ•œ๋‹ค. ( x x โ‰ฅ )์ด๋‚˜ ( x x = 27 ) ์ด ๊ทธ ์˜ˆ๋‹ค. forall ํ‚ค์›Œ๋“œ๋Š” ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ ํƒ€์ž…์„ ํ•œ์ •ํ•œ๋‹ค. map์˜ ํƒ€์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ ์ณ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: ํƒ€์ž… ๋ณ€์ˆ˜์˜ ์กด์žฌ ํ•œ์ • map :: forall a b. (a -> b) -> [a] -> [b] ๋ง์ธ์ฆ‰์Šจ ์šฐ๋ฆฌ๊ฐ€ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  a์™€ b์— ๋Œ€ํ•ด map์˜ ํƒ€์ž…์€ (a -> b) -> [a] -> [b]์ด๋‹ค. a = Int, b = String์„ ์„ ํƒํ•œ๋‹ค๋ฉด map์˜ ํƒ€์ž…์€ (Int -> String) -> [Int] -> [String]์ด ๋œ๋‹ค. map์˜ ์ผ๋ฐ˜์ ์ธ ํƒ€์ž…์„ ๋” ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์œผ๋กœ ์ธ์Šคํ„ด์Šคํ™”ํ•œ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ•˜์Šค์ผˆ์—์„œ ์†Œ๋ฌธ์ž ํƒ€์ž…์€ ์•Œ๊ณ  ์žˆ๋“ฏ์ด ์•”๋ฌต์ ์ธ forall ํ‚ค์›Œ๋“œ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ map์˜ ๋‘ ํƒ€์ž… ์„ ์–ธ์€ ๋™์น˜๋ฉฐ ์•„๋ž˜์˜ ์„ ์–ธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ์˜ˆ: ๋™์น˜์ธ ๋‘ ํƒ€์ž… ์„ ์–ธ id :: a -> a id :: forall a. a -> a ์ •๋ง ํฅ๋ฏธ๋กœ์šด ์ ์€ forall์„ ์–ด๋”” ๋‘˜์ง€๋ฅผ ํ•˜์Šค์ผˆ์— ์•Œ๋ ค์„œ ์ด ๊ธฐ๋ณธ ๋™์ž‘์„ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ ์˜ˆ๊ฐ€ ์กด์žฌ ํ•œ์ • ํƒ€์ž… ๋˜๋Š” ์กด์žฌ์  ํƒ€์ž…, ๊ฐ„๋‹จํžˆ๋Š” existential์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์ž ๊น, forall์€ ๋ณดํŽธ ํ•œ์ •์ž ์•„๋‹ˆ์—ˆ๋˜๊ฐ€? ์—ฌ๊ธฐ์„œ ์–ด๋–ป๊ฒŒ ์กด์žฌ์  ํƒ€์ž…์„ ์–ป๋Š”๋‹จ ๋ง์ธ๊ฐ€? ๊ทธ๊ฒƒ์€ ๋‚˜์ค‘์— ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ๊ทธ์ „์— ์กด์žฌ์  ํƒ€์ž…์˜ ๊ฐ•๋ ฅํ•จ์„ ์‹ค์ œ๋กœ ๋Š๊ปด๋ณด์ž. ์˜ˆ: ํ˜ผ์ข… ๋ฆฌ์ŠคํŠธ ํ•˜์Šค์ผˆ์˜ ํƒ€์ž… ํด๋ž˜์Šค ์ฒด๊ณ„์— ๊น”๋ฆฐ ์ „์ œ๋Š” ๊ณตํ†ต ์†์„ฑ์„<NAME>๋Š” ๋ชจ๋“  ํƒ€์ž…์„ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์—ฌ๋Ÿฌ๋ถ„์ด ์–ด๋–ค ํด๋ž˜์Šค C๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ํƒ€์ž…์„ ์•ˆ๋‹ค๋ฉด ๊ทธ ํƒ€์ž…์— ๋Œ€ํ•ด ๋ฌด์–ธ๊ฐ€๋ฅผ ์•„๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Int๋Š” Eq๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ธฐ์— ์šฐ๋ฆฌ๋Š” Int์˜ ์›์†Œ๋“ค์„ ํ•ญ๋“ฑ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Œ์„ ์•ˆ๋‹ค. ๊ฐ’๋“ค์˜ ๋ชจ์ž„์ด ์žˆ๋Š”๋ฐ ์ด๊ฒƒ๋“ค์˜ ํƒ€์ž…์ด ๋‹ค ๊ฐ™์€์ง€๋Š” ๋ชจ๋ฅด์ง€๋งŒ, ๋ชจ๋‘ ์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ๊ฒƒ์€ ์•ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ์ฆ‰ ๋ชจ๋“  ๊ฐ’์ด ํŠน์ • ์„ฑ์งˆ์„ ๊ฐ€์ง„๋‹ค. ์ด ๊ฐ’๋“ค์„ ๋ชจ๋‘ ํ•œ ๋ฆฌ์ŠคํŠธ์— ์ง‘์–ด๋„ฃ์œผ๋ฉด ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ๊ทธ๋Ÿด ์ˆ˜ ์—†๋Š”๋ฐ ๋ฆฌ์ŠคํŠธ๋Š” ํƒ€์ž…์— ๋Œ€ํ•ด ํ˜ผ์ข… homogeneous์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ๋‹จ์ผ ํƒ€์ž…๋งŒ์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์กด์žฌ์  ํƒ€์ž…์€ 'ํƒ€์ž… ์€๋‹‰์žhider ๋˜๋Š” ํƒ€์ž… ์ƒ์ž'๋ฅผ ์ •์˜ํ•˜์—ฌ ์ด ์ œํ•œ์„ ์™„ํ™”ํ•œ๋‹ค. ์˜ˆ: ํ˜ผ์ข… ๋ฆฌ์ŠคํŠธ์˜ ๊ตฌ์ถ• data ShowBox = forall s. Show s => SB s heteroList :: [ShowBox] heteroList = [SB (), SB 5, SB True] ์ด ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ •์˜์˜ ์˜๋ฏธ๋ฅผ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜์ง€๋Š” ์•Š๊ฒ ์ง€๋งŒ ๊ทธ ๋œป์€ ํ™• ์™€๋‹ฟ์„ ๊ฒƒ์œผ๋กœ ๋ฏฟ๋Š”๋‹ค. ์ค‘์š”ํ•œ ์ ์€ ์„ธ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๊ฐ’์— ๋Œ€ํ•ด ๊ฐ™์€ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ–ˆ๊ณ , ์ด๊ฒƒ๋“ค์„ ํ•œ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์„œ, ์ตœ์ข… ํƒ€์ž…๋“ค์ด ๊ฐ™๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทผ๋ณธ์ ์ธ ์›๋ฆฌ๋Š” forall ํ‚ค์›Œ๋“œ๋กœ ์ธํ•ด ์ƒ์„ฑ์ž์˜ ํƒ€์ž…์ด SB :: forall s. Show s => s -> ShowBox๊ฐ€ ๋˜์—ˆ๋‹ค๋Š” ์ ์ด๋‹ค. ์ด์ œ heteroList๋ฅผ ์ „๋‹ฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, SB ๋‚ด๋ถ€์˜ ๊ฐ’์— not ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•  ์ˆ˜๋Š” ์—†๋‹ค. ๊ทธ ๊ฐ’์˜ ํƒ€์ž…์ด Bool์ด ์•„๋‹ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์ด ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋“ค์— ๋Œ€ํ•ด ์•„๋Š” ๊ฒƒ์ด ์žˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ Show๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์‹ค ์šฐ๋ฆฌ๊ฐ€ ์•„๋Š” ๊ฒƒ์ด๋ผ๊ณค ์ด๊ฒƒ๋ฟ์ด๋‹ค. ์˜ˆ: ํ˜ผ์ข… ๋ฆฌ์ŠคํŠธ์˜ ํ™œ์šฉ instance Show ShowBox where show (SB s) = show s -- ์•„๋ž˜ ๋ฌธ๋‹จ์„ ๋ณผ ๊ฒƒ f :: [ShowBox] -> IO () f xs = mapM_ print xs main = f heteroList ํ•œ ๋ฒˆ ์‚ดํŽด๋ณด์ž. ShowBox์— ๋Œ€ํ•œ show์˜ ์ •์˜(์ฃผ์„ ๋‹ฌ๋ฆฐ ์ค„)์—์„œ ์šฐ๋ฆฌ๋Š” s์˜ ํƒ€์ž…์„ ๋ชจ๋ฅธ๋‹ค. ํ•˜์ง€๋งŒ ์•ž์„œ ๋งํ–ˆ๋“ฏ ์ด ํƒ€์ž…์ด Show์˜ ์ธ์Šคํ„ด์Šค๋ผ๋Š” ๊ฒƒ์€ SB ์ƒ์„ฑ์ž์˜ ์ œํ•œ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ s์— ํ•จ์ˆ˜ show๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ•จ์ˆ˜ ์ •์˜ ์šฐ๋ณ€์—์„œ ๋ณด๋“ฏ์ด ์ •๋‹นํ•˜๋‹ค. f์— ๋Œ€ํ•ด์„œ๋Š”, print์˜ ํƒ€์ž…์„ ๋– ์˜ฌ๋ ค๋ณด๋ผ. ์˜ˆ: ์—ฐ๊ด€๋œ ํ•จ์ˆ˜๋“ค์˜ ํƒ€์ž… print :: Show s => s -> IO () -- print x = putStrLn (show x) mapM_ :: (a -> m b) -> [a] -> m () mapM_ print :: Show s => [s] -> IO () ShowBox๋ฅผ Show์˜ ์ธ์Šคํ„ด์Šค๋กœ ์„ ์–ธํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๊ฐ’๋“ค์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์กด์žฌ์ ์ด๋ผ๋Š” ์šฉ์–ด์— ๋Œ€ํ•œ ์„ค๋ช… ์•ž์—์„œ ๋˜์กŒ๋˜ ์งˆ๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ€์ž. forall์ด ๋ณดํŽธ ํ•œ์ •์ž๋ผ๋ฉด ์™œ ์ด๊ฒƒ๋“ค์„ ์กด์žฌ์  ํƒ€์ž…์ด๋ผ ๋ถ€๋ฅด๋Š”๊ฐ€? forall์œผ๋กœ ์กด์žฌ์  ํƒ€์ž…์„ ์–ป์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜์Šค์ผˆ์—๋Š” ์ค‘๋ณต๋งŒ ๋  exists ํ‚ค์›Œ๋“œ๊ฐ€ ์—†๋‹ค. ๋ฌด์—‡๋ณด๋‹ค๋„ forall์€ '๋ชจ๋“  ...์— ๋Œ€ํ•ด'๋ฅผ ๋œปํ•œ๋‹ค. ํƒ€์ž…์„ ๊ทธ ํƒ€์ž…์˜ ๊ฐ’๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ ์ƒ๊ฐํ•ด ๋ณด๋ฉด Bool์€ ์ง‘ํ•ฉ {True, False, โŠฅ}์ด๊ณ (๋ฐ”ํ…€ ์ฆ‰ โŠฅ์€ ๋ชจ๋“  ํƒ€์ž…์˜ ๋ฉค๋ฒ„๋‹ค), Integer๋Š” ๋ชจ๋“  ์ •์ˆ˜์™€ ๋ฐ”ํ…€์˜ ์ง‘ํ•ฉ์ด๊ณ , String์€ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ฌธ์ž์—ด๊ณผ ๋ฐ”ํ…€์˜ ์ง‘ํ•ฉ์ด๊ณ ... ์ด๋Ÿฐ ์‹์ด๋‹ค. ๊ฐ€๋ น forall a. a๋Š” ๋ชจ๋“  ํƒ€์ž…์˜ ๊ต์ง‘ํ•ฉ์œผ๋กœ, {โŠฅ} ์ฆ‰ ์˜ค์ง ํ•œ ์›์†Œ(๋ฐ”ํ…€) ๋งŒ ๊ฐ€์ง€๋Š” ํƒ€์ž…(์ง‘ํ•ฉ)์ด๋‹ค. Bool์˜ ์›์†Œ๋“ค ์ค‘ ์˜ˆ๋ฅผ ๋“ค๋ฉด String์— ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์€ ๋ฌด์—‡์ธ๊ฐ€? ๋ฐ”ํ…€๋งŒ์ด ๋ชจ๋“  ํƒ€์ž…์— ๊ณตํ†ต์ธ ๊ฐ’์ด๋‹ค. ์ถ”๊ฐ€ ์˜ˆ์‹œ: [forall a. a]๋Š” ๋ชจ๋“  ์›์†Œ์˜ ํƒ€์ž…์ด forall a. a์ธ ๋ฆฌ์ŠคํŠธ, ์ฆ‰ ๋ฐ”ํ…€๋“ค์˜ ๋ฆฌ์ŠคํŠธ์˜ ํƒ€์ž…์ด๋‹ค. [forall a. Show a => a]๋Š” ๋ชจ๋“  ์›์†Œ์˜ ํƒ€์ž…์ด forall a. Show a => a์ธ ๋ฆฌ์ŠคํŠธ๋‹ค. Show ํด๋ž˜์Šค๋ผ๋Š” ์ œ์•ฝ์€ ์—ฌ๋Ÿฌ๋ถ„์ด ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•˜๋ ค๋Š” ์ง‘ํ•ฉ๋“ค์„ ์ œํ•œํ•œ๋‹ค. (์—ฌ๊ธฐ์„œ๋Š” ์˜ค์ง Show์˜ ์ธ์Šคํ„ด์Šค๋“ค์˜ ๊ต์ง‘ํ•ฉ) ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ โŠฅ๊ฐ€ ๋ชจ๋“  ํƒ€์ž…์— ๊ณตํ†ต๋œ ์œ ์ผํ•œ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ ์—ญ์‹œ ๋ฐ”ํ…€๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋‹ค. [forall a. Num a => a]๋Š” ๊ฐ ์›์†Œ๊ฐ€ Num์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ๋ชจ๋“  ํƒ€์ž… ์ค‘ ํ•˜๋‚˜์˜ ๋ฉค๋ฒ„์ธ ๋ฆฌ์ŠคํŠธ๋‹ค. forall a. Num a => a ํƒ€์ž…์ธ ์ˆซ์ž ๋ฆฌํ„ฐ๋Ÿด๊ณผ ๋ฐ”ํ…€์„ ํฌํ•จํ•œ๋‹ค. forall a. [a] ์›์†Œ๋“ค์ด ์–ด๋–ค ํ•˜๋‚˜์˜ ํƒ€์ž… a๋ฅผ ๊ฐ€์ง€๋Š” ๋ฆฌ์ŠคํŠธ. a๋Š” ํ”ผํ˜ธ์ถœ์ž์—์„œ ์–ด๋–ค ํƒ€์ž…์ด๋“  ์ง€์ • ๊ฐ€๋Šฅํ•˜๋‹ค. (๋”ฐ๋ผ์„œ ์ด๊ฒƒ๋„ ๋ฐ”ํ…€๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋‹ค) ํƒ€์ž…๋“ค ๊ฐ„์˜ ๊ต์ง‘ํ•ฉ์€ ์–ด๋–ค ์‹์œผ๋กœ๋“  ๋Œ€๋ถ€๋ถ„ ๊ฒฐ๊ตญ ๋ฐ”ํ…€๋“ค์˜ ์กฐํ•ฉ์ผ ๋ฟ์ด๋‹ค. ํƒ€์ž…๋“ค์ด ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง€๋Š” ๊ฐ’์ด ๋งŽ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์•ž์ ˆ์—์„œ 'ํƒ€์ž… ์€๋‹‰์ž'๋ฅผ ์ด์šฉํ•ด ํ˜ผ์ข… ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ ๊ฒƒ์„ ๋– ์˜ฌ๋ ค๋ณด์ž. ์ด์ƒ์ ์œผ๋กœ๋Š” ํ˜ผ์ข… ๋ฆฌ์ŠคํŠธ์˜ ํƒ€์ž…์ด [exists a. a] ์ฆ‰ ๋ชจ๋“  ์›์†Œ์˜ ํƒ€์ž…์ด exists a. a์ธ ๋ฆฌ์ŠคํŠธ์—ฌ์•ผ ํ•œ๋‹ค. ์ด exists ํ‚ค์›Œ๋“œ๋Š” (ํ•˜์Šค์ผˆ์—๋Š” ์—†๋‹ค) ํƒ€์ž…๋“ค์˜ ํ•ฉ์ง‘ํ•ฉ์œผ๋กœ์„œ [exists a. a]๋Š” ๋ชจ๋“  ์›์†Œ๊ฐ€ ์ž„์˜ ํƒ€์ž…์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ด์šฉํ•˜๋ฉด ์œ„์™€ ๊ฑฐ์˜ ๋™์ผํ•œ ๊ฒƒ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒƒ์„ ํ•˜๋‚˜ ์„ ์–ธํ•ด ๋ณด์ž. ์˜ˆ์ œ: ์กด์žฌ์  ํƒ€์ž… data T = forall a. MkT a ์ด๋Š” ๋‹ค์Œ์„ ์˜๋ฏธํ•œ๋‹ค. ์˜ˆ์ œ: ์กด์žฌ์  ์ƒ์„ฑ์ž์˜ ํƒ€์ž… MkT :: forall a. a -> T ์ฆ‰ MkT์— ์•„๋ฌด ๊ฐ’์ด๋‚˜ ๋„˜๊ธฐ๊ณ  ๊ทธ๊ฒƒ์„ T๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์ œ MkT ๊ฐ’์„ ๋ถ„ํ•ดํ•˜๋ ค ํ•˜๋ฉด ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚˜๋Š”๊ฐ€? ์˜ˆ์ œ: ์กด์žฌ์  ํƒ€์ž…์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ foo (MkT x) = ... -- x์˜ ํƒ€์ž…? ๋ฐฉ๊ธˆ ๋งํ–ˆ๋“ฏ์ด x๋Š” ์–ด๋–ค ํƒ€์ž…๋„ ๋  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ์ž„์˜ ํƒ€์ž…์˜ ๋ฉค๋ฒ„์ด๋ฉฐ ๊ทธ ํƒ€์ž…์€ x :: exists a. a์ด๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด T์˜ ์„ ์–ธ์€ ๋‹ค์Œ๊ณผ ๋™์ผํ•˜๋‹ค. ์˜ˆ์ œ: ์œ„ ์กด์žฌ์  ํƒ€์ž…๊ณผ ๋™์น˜ (์˜์‚ฌ ํ•˜์Šค์ผˆ) data T = MkT (exists a. a) ๊ฐ‘์ž๊ธฐ ์กด์žฌ์  ํƒ€์ž…์„ ์–ป๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด์ œ ํ˜ผ์ข… ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์ œ: ํ˜ผ์ข… ๋ฆฌ์ŠคํŠธ์˜ ์ƒ์„ฑ heteroList = [MkT 5, MkT (), MkT True, MkT map] ๋ฌผ๋ก  heteroList์— ํŒจํ„ด ๋งค์นญํ•  ๋•Œ ๊ทธ ์›์†Œ๋“ค์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ฌด ์ผ๋„ ํ•  ์ˆ˜๊ฐ€ ์—†๋Š”๋ฐ 1, ์šฐ๋ฆฌ๊ฐ€ ์•„๋Š” ๊ฒƒ์ด๋ผ๊ณ ๋Š” ๋‹จ์ง€ ์ด๊ฒƒ๋“ค์ด ์–ด๋–ค ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค๋Š” ๊ฒƒ๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ํด๋ž˜์Šค ์ œ์•ฝ์„ ๋„์ž…ํ•œ๋‹ค๋ฉด ์˜ˆ์ œ: ํด๋ž˜์Šค ์ œ์•ฝ์ด ์žˆ๋Š” ์กด์žฌ์  ํƒ€์ž… data T' = forall a. Show a => MkT' a ์ด๋Š” ๋‹ค์Œ๊ณผ ๋™์น˜๋‹ค. ์˜ˆ์ œ: '์ง„์งœ' ์กด์žฌ์  ํƒ€์ž…์œผ๋กœ ๋ฒˆ์—ญํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž… data T' = MkT' (exists a. Show a => a) ์ด๋ฒˆ์—๋„ ํด๋ž˜์Šค ์ œ์•ฝ์€ ํ•ฉ์ง‘ํ•ฉ์„ ๊ตฌํ•˜๋ ค๋Š” ํƒ€์ž…๋“ค์„ ์ œํ•œํ•˜์—ฌ, ์šฐ๋ฆฌ๋Š” MkT' ์•ˆ์˜ ๊ฐ’๋“ค์ด Show๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ์–ด๋–ค ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค๋Š” ๊ฒƒ์„ ์•ˆ๋‹ค. ์—ฌ๊ธฐ ๋‚ดํฌ๋œ ๋ฐ”๋Š”, exists a. Show a => a ํƒ€์ž…์˜ ๊ฐ’์— Show๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์ด ์‹ค์ œ๋กœ ์–ด๋–ค ํƒ€์ž…์ธ์ง€๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค. ์˜ˆ์ œ: ์œ„ ํ˜ผ์ข… ๋ฆฌ์ŠคํŠธ์˜ ํ™œ์šฉ heteroList' = [MkT' 5, MkT' (), MkT' True, MkT' "Sartre"] main = mapM_ (\(MkT' x) -> print x) heteroList' {- prints: () True "Sartre" -} ์š”์•ฝํ•˜๋ฉด, ๋ณดํŽธ ํ•œ์ •์ž์™€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๊ฒฐํ•ฉํ•˜์—ฌ ์กด์žฌ์  ํƒ€์ž…์„ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค. forall์„ ์ˆ˜๋ฐ˜ํ•˜๋Š” ํƒ€์ž…์˜ ๊ฐ€์žฅ ํฅ๋ฏธ๋กœ์šด ์‘์šฉ๋“ค์€ ์ด๋Ÿฌํ•œ ๊ฒฐํ•ฉ์„ ํ™œ์šฉํ•˜๊ณ , ์šฐ๋ฆฌ๋Š” ๊ทธ๋Ÿฐ ํƒ€์ž…์„ '์กด์žฌ์ '์ด๋ผ ๋ถ€๋ฅธ๋‹ค. ์กด์žฌ์  ํƒ€์ž…์„ ์›ํ•˜๋ฉด ๊ทธ ํƒ€์ž…์„ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ƒ์„ฑ์ž๋กœ ๊ฐ์‹ธ์•ผ ํ•œ๋‹ค. [exists a. a]์ฒ˜๋Ÿผ, ๊ทธ ํƒ€์ž…์€ ๋ฐ–์—์„œ๋Š” ์กด์žฌํ•  ์ˆ˜ ์—†๋‹ค. ์˜ˆ: runST ์•„๋งˆ ์ง€๊ธˆ๊นŒ์ง€ ST ๋ชจ๋‚˜๋“œ๋ผ๋Š” ๊ฒƒ์„ ๋ณธ ์ ์ด ์—†์„ ๊ฒƒ์ด๋‹ค. ST๋Š” State ๋ชจ๋‚˜๋“œ์˜ ๋” ๊ฐ•๋ ฅํ•œ ๋ฒ„์ „์ด๋‹ค. ST์˜ ๊ตฌ์กฐ๋Š” ํ›จ์”ฌ ๋ณต์žกํ•˜๊ณ  ๊ณ ๊ธ‰ ์ฃผ์ œ๋“ค์„ ์ˆ˜๋ฐ˜ํ•œ๋‹ค. ST๋Š” ์›๋ž˜ ํ•˜์Šค์ผˆ์— IO๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑ๋˜์—ˆ๋‹ค. ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ ์žฅ์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ IO๋Š” ์‹ค์ œ ์„ธ๊ณ„์— ๊ด€ํ•œ ๋ชจ๋“  ์ •๋ณด๋ผ๋Š” ํ™˜๊ฒฝ์ด ์žˆ๋Š” State ๋ชจ๋‚˜๋“œ๋‹ค. ์‚ฌ์‹ค GHC ๋‚ด์—์„œ๋Š” ST๊ฐ€ ์“ฐ์ด๋Š”๋ฐ ๊ทธ ํ™˜๊ฒฝ์€ RealWorld๋ผ๋Š” ํƒ€์ž…์ด๋‹ค. runState๋ฅผ ์‚ฌ์šฉํ•ด State ๋ชจ๋‚˜๋“œ๋ฅผ ๋ฒ—๊ฒจ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ST์—์„œ๋Š” ๋น„์Šทํ•œ ๊ฒƒ์œผ๋กœ runST๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ทธ ํƒ€์ž…์ด ์‚ฌ๋ญ‡ ํŠน์ดํ•˜๋‹ค. ์˜ˆ์ œ: runST ํ•จ์ˆ˜ runST :: forall a. (forall s. ST s a) -> a ์ด๊ฒƒ์€ ์‚ฌ์‹ค ๋žญํฌ-2 ๋‹คํ˜•์„ฑ์ด๋ผ๋Š” ๋” ๋ณต์žกํ•œ ์–ธ์–ด ํŠน์„ฑ์œผ๋กœ์„œ ์ง€๊ธˆ์€ ๊นŠ๊ฒŒ ํŒŒ๊ณ ๋“ค์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ์œ„ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋‹ค๋Š” ์ ์ด ์ค‘์š”ํ•˜๋‹ค. ๋˜ํ•œ ST๋Š” ์ƒํƒœ๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ๋ฐฉ์‹์ด State์™€ ๋‹ค๋ฅด๋‹ค. State๊ฐ€ ํ˜„ ์ƒํƒœ๋ฅผ get ํ•˜๊ณ  put ํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜๋Š” ๋ฐ˜๋ฉด, ST๋Š” ์ฐธ์กฐ์— ๋Œ€ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ STRef ํƒ€์ž…์˜ ์ฐธ์กฐ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด newSTRef :: a -> ST s (STRef s a)์— ์ดˆ๊นƒ๊ฐ’์„ ์ œ๊ณตํ•˜๊ณ , ์กฐ์ž‘์„ ์œ„ํ•ด readSTRef :: STRef s a -> ST s a์™€ writeSTRef :: STRef s a -> a -> ST s ()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ST ๊ณ„์‚ฐ์˜ ๋‚ด๋ถ€ ํ™˜๊ฒฝ์€ ์–ด๋–ค ํŠน์ • ๊ฐ’์ด ์•„๋‹ˆ๋ผ ์ฐธ์กฐ์—์„œ ๊ฐ’์œผ๋กœ ๊ฐ€๋Š” ๋งคํ•‘์ด๋‹ค. runST์— ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ์ œ๊ณตํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒƒ์€, ์ดˆ๊ธฐ ์ƒํƒœ๊ฐ€ ์ฐธ์กฐ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๋นˆ ๋งคํ•‘์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ทธ๋ ‡๊ฒŒ ๋‹จ์ˆœํ•œ ์ผ์ด ์•„๋‹ˆ๋‹ค. ํ•œ ST ๊ณ„์‚ฐ ์•ˆ์—์„œ ์ฐธ์กฐ๋ฅผ ๋งŒ๋“ค๋ฉด, ๊ทธ๊ฒƒ์„ ๋‹ค๋ฅธ ST ๊ณ„์‚ฐ ์•ˆ์—์„œ ์“ฐ๋Š” ๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ๋ง‰์„๊นŒ? ์ด๋Ÿฐ ์ผ์ด ์ผ์–ด๋‚˜๋ฉด ์•ˆ ๋˜๋Š” ์ด์œ ๋Š” (์Šค๋ ˆ๋“œ ์•ˆ์ •์„ฑ์„ ์œ„ํ•ด) ์–ด๋–ค ST ๊ณ„์‚ฐ์ด๋“  ์ดˆ๊ธฐ ๋‚ด๋ถ€ ํ™˜๊ฒฝ์ด ํŠน์ • ์ฐธ์กฐ๋ฅผ ํฌํ•จํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด์„œ๋Š” ์•ˆ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ณด๋‹ค ๊ตฌ์ฒด์ ์œผ๋กœ๋Š”, ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค. ์˜ˆ์ œ: ์ข‹์ง€ ์•Š์€ ST ์ฝ”๋“œ let v = runST (newSTRef True) in runST (readSTRef v) ๋ฌด์—‡์ด ์ด๋Ÿฐ ์ผ์„ ๋ง‰๋Š”๊ฐ€? runST์˜ ํƒ€์ž…์ด ๋žญํฌ-2 ๋‹คํ˜•์„ฑ์ธ ๊ฒƒ์˜ ์˜ํ–ฅ์œผ๋กœ ํƒ€์ž… ๋ณ€์ˆ˜ s์˜ ๋ฒ”์œ„๋Š” ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‚ด๋ถ€๋กœ ํ•œ์ •๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ, ํƒ€์ž… ๋ณ€์ˆ˜ s๊ฐ€ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜ ์•ˆ์— ๋‚˜ํƒ€๋‚˜๋ฉด ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜ ์•ˆ์—์„œ๋„ ๋‚˜ํƒ€๋‚  ์ˆ˜๋Š” ์—†๋‹ค. ์ผ์ด ์ •ํ™•ํžˆ ์–ด๋–ป๊ฒŒ ๋Œ์•„๊ฐ€๋Š”์ง€ ์‚ดํŽด๋ณด์ž. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ์—์„œ ์˜ˆ: ์ข‹์ง€ ์•Š์€ ST ์ฝ”๋“œ์˜ ์ผ๋ถ€ ... runST (newSTRef True) ... ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํƒ€์ž…๋“ค์„ ๋ผ์›Œ ๋งž์ถ”๋ ค๊ณ  ๋…ธ๋ ฅํ•œ๋‹ค. ์˜ˆ: ์ปดํŒŒ์ผ๋Ÿฌ์˜ ํƒ€์ž… ๊ฒ€์‚ฌ ๋‹จ๊ณ„ newSTRef True :: forall s. ST s (STRef s Bool) runST :: forall a. (forall s. ST s a) -> a ํ•ฉ์น˜๋ฉด forall a. (forall s. ST s (STRef s Bool)) -> STRef s Bool ์ฒซ ๋ฒˆ์งธ ๊ด„ํ˜ธ ์•ˆ์˜ forall์˜ ์š”์ ์€ s์˜ ์ด๋ฆ„์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰ ์ด๋ ‡๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: ํƒ€์ž… ๋ถˆ์ผ์น˜ ํ•ฉ์น˜๋ฉด forall a. (forall s'. ST s' (STRef s' Bool)) -> STRef s Bool ๋ง์ด ๋œ๋‹ค. ์ˆ˜ํ•™์—์„œ x x 5 โˆ€. > ์™€ ๊ฐ™๋‹ค. ๋ณ€์ˆ˜์— ๋‹ค๋ฅธ ์ด๋ฆ„์„ ๋ถ™์ผ ๋ฟ์ด๋‹ค. ํ•˜์ง€๋งŒ ์œ„ ์ฝ”๋“œ์—๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. forall์ด runST์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…๊นŒ์ง€ ๋ฎ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜ํ™˜ ํƒ€์ž…์—์„œ๋Š” s์˜ ์ด๋ฆ„์ด ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํƒ€์ž…์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค! ์ฒซ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‚ด์˜ ST ๊ณ„์‚ฐ ๊ฒฐ๊ณผ์˜ ํƒ€์ž…์€ runST์˜ ๊ฒฐ๊ณผ ํƒ€์ž…๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ด์ œ๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค. ์กด์žฌ์ž์˜ ํ•ต์‹ฌ์€ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‚ด์˜ ์ƒํƒœ์˜ ํƒ€์ž…์„ ์ผ๋ฐ˜ํ™”ํ•˜์—ฌ, ๊ฒฐ๊ณผ ํƒ€์ž…์ด ์ด๊ฒƒ์— ์˜์กดํ•  ์ˆ˜ ์—†๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜์กด์„ฑ ๋ฌธ์ œ๋ฅผ ์˜๋ฆฌํ•˜๊ฒŒ ๋น„๊ปด๋‚˜๊ฐ€ runST์˜ ๋งค ํ˜ธ์ถœ๋งˆ๋‹ค ์นธ๋ง‰์ด๋ฅผ ์ณ ๊ณ ์œ ์˜ ์ž‘์€ ํžˆํ”„๋ฅผ ๋งŒ๋“ค๊ณ , ์ฐธ์กฐ๋“ค์€ ํ˜ธ์ถœ ์‚ฌ์ด์—์„œ ๊ณต์œ ๋  ์ˆ˜ ์—†๋‹ค. ์›์‹œํ˜•์œผ๋กœ์„œ์˜ ํ•œ์ • ๋ณดํŽธ ํ•œ์ •์€ ์ด์ „์— ์ •์˜๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ •์˜ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ํ•˜์Šค์ผˆ์— ์Œ(pair)์ด ๋‚ด์žฅ๋˜์ง€ ์•Š์•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด์ž. ํ•œ์ •์œผ๋กœ ์Œ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. {-# LANGUAGE ExistentialQuantification, RankNTypes #-} newtype Pair a b = Pair (forall c. (a -> b -> c) -> c) makePair :: a -> b -> Pair a b makePair a b = Pair $ \f -> f a b GHCi์—์„œ๋Š” ฮป> :bro newtype Pair a b = Pair {runPair :: forall c. (a -> b -> c) -> c} makePair :: a -> b -> Pair a b ฮป> let pair = makePair "a" 'b' ฮป> :t pair pair :: Pair [Char] Char ฮป> runPair pair (\x y -> x) "a" ฮป> runPair pair (\x y -> y) 'b' ๋” ์ฝ์„๊ฑฐ๋ฆฌ GHC์˜ ์‚ฌ์šฉ์ž ์ง€์นจ์— existential์— ๋Œ€ํ•œ ์œ ์šฉํ•œ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค. Simon Peyton-Jones์™€ John Launchbury์˜ Lazy Functional State Threads๋Š” ST์˜ ์•„์ด๋””์–ด๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋…ผ๋ฌธ์ด๋‹ค. ์‚ฌ์‹ค ํƒ€์ž…์ด forall a. a -> R์ธ ํ•จ์ˆ˜๋“ค์—๋Š” ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. R์€ ์ž„์˜ ํƒ€์ž…์ด๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์€ ์–ด๋–ค ํƒ€์ž…์˜ ๊ฐ’๋„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„๋“ค์ธ๋‹ค. ์˜ˆ๋กœ id, const k, seq ๋“ฑ์ด ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ, ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ์›์†Œ๋“ค์„ WHNF์œผ๋กœ ํ™˜์›ํ•˜๋Š” ๊ฒƒ ์™ธ์—๋Š” ์–ด๋–ค ์œ ์šฉํ•œ ์ผ์„ ํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค. โ†ฉ 3 ๊ณ ๊ธ‰ ํƒ€์ž… ํด๋ž˜์Šค ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Advanced_type_classes ๋ณต์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž… ํด๋ž˜์Šค ํ•จ์ˆ˜ํ˜• ์˜์กด์„ฑ ์˜ˆ์ œ ํ–‰๋ ฌ๊ณผ ๋ฒกํ„ฐ ํƒ€์ž… ํด๋ž˜์Šค๋Š” ์ง€๋ฃจํ•œ ๋‚ด์šฉ ๊ฐ™์ง€๋งŒ ์—ฐ๊ตฌ ๊ฒฐ๊ณผ ๋ช‡ ๊ฐ€์ง€ ๋ฐœ์ „๊ณผ ์ผ๋ฐ˜ํ™”๋ฅผ ์ด๋ฃจ์–ด, ๋งค์šฐ ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ๊ฐ€ ๋˜์—ˆ๋‹ค. ๋ณต์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž… ํด๋ž˜์Šค ๋ณต์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž… ํด๋ž˜์Šค๋Š” ๋‹จ์ผ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž… ํด๋ž˜์Šค์˜ ์ผ๋ฐ˜ํ™”๋กœ์„œ ์—ฌ๋Ÿฌ ํ•˜์Šค ์ผˆ ๊ตฌํ˜„์—์„œ ์ง€์›ํ•œ๋‹ค. ๋‹ค์–‘ํ•œ ๊ตฌ์ฒด์  ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ์“ธ ์ˆ˜ ์žˆ๊ณ  ๋‘ ๊ฐ€์ง€ ์—ฐ์‚ฐ์„ ์ง€์›ํ•˜๋Š” 'Collection' ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋ ค ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ๊ทธ ์—ฐ์‚ฐ์€ ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” 'insert'์™€ ์†Œ์†์„ ๊ฒ€์‚ฌํ•˜๋Š” 'member'์ด๋‹ค. ์ฒ˜์Œ ์‹œ๋„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์˜ˆ: (์ž˜๋ชป๋œ) Collection ํƒ€์ž… ํด๋ž˜์Šค class Collection c where insert :: c -> e -> c member :: c -> e -> Bool -- ๋ฆฌ์ŠคํŠธ๋ฅผ Collection์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ ๋‹ค instance Collection [a] where insert xs x = x : xs member = flip elem ํ•˜์ง€๋งŒ ์ด๊ฒƒ์€ ์ปดํŒŒ์ผ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋ฌธ์ œ๋Š” Collection ์—ฐ์‚ฐ๋“ค์˜ e ํƒ€์ž… ๋ณ€์ˆ˜๊ฐ€ ๊ทผ๋ณธ์ด ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. Collection์˜ ์ธ์Šคํ„ด์Šค์˜ ํƒ€์ž…์—๋Š” e๊ฐ€ ์‹ค์ œ๋กœ ๋ฌด์—‡์ธ์ง€ ๋งํ•ด์ฃผ๋Š” ์š”์†Œ๊ฐ€ ์ „ํ˜€ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” ์ด ๋ฉ”์„œ๋“œ๋“ค์˜ ๊ตฌํ˜„์„ ์ ˆ๋Œ€๋กœ ์ •์˜ํ•  ์ˆ˜ ์—†๋‹ค. ๋ณต์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž… ํด๋ž˜์Šค๋Š” e๋ฅผ ํด๋ž˜์Šค์˜ ํƒ€์ž…์œผ๋กœ ์ง‘์–ด๋„ฃ์–ด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค. ๋‹ค์Œ ์˜ˆ์‹œ๋Š” ์‹ค์ œ๋กœ ์ปดํŒŒ์ผํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: (์˜ฌ๋ฐ”๋ฅธ) Collection ํƒ€์ž… ํด๋ž˜์Šค {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} class Eq e => Collection c e where insert :: c -> e -> c member :: c -> e -> Bool instance Eq a => Collection [a] a where insert = flip (:) member = flip elem ํ•จ์ˆ˜ํ˜• ์˜์กด์„ฑ ์œ„ ์˜ˆ์ œ์—์„œ ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์€ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋ชจ๋ฅด๋Š” ์—ฌ๋ถ„์˜ ์ •๋ณด๋ฅผ ์šฐ๋ฆฌ๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” ์ž˜๋ชป๋œ ๋ชจํ˜ธํ•จ๊ณผ ๊ณผํ•˜๊ฒŒ ์ผ๋ฐ˜ํ™”๋œ ํ•จ์ˆ˜ ์‹œ๊ทธ๋„ˆ์ฒ˜๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ์ปฌ๋ ‰์…˜์˜ ํƒ€์ž…์ด ์›์†Œ๋“ค์˜ ํƒ€์ž…์„ ํ•ญ์ƒ ๊ฒฐ์ •ํ•จ์„ ์ง๊ด€์ ์œผ๋กœ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ c๊ฐ€ [a] ๋ฉด e๋Š” a๋‹ค. c๊ฐ€ Hashmap a ๋ฉด e๋Š” a๋‹ค. (์—ญ์€ ์ฐธ์ด ์•„๋‹ˆ๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ์ปฌ๋ ‰์…˜ ํƒ€์ž…๋“ค์ด ๊ฐ™์€ ํƒ€์ž…์˜ ์›์†Œ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์›์†Œ ํƒ€์ž…์ด ๊ฐ€๋ น Int๋ผ๋Š” ๊ฒƒ์„ ์•Œ์•„๋„ ์ปฌ๋ ‰์…˜ ํƒ€์ž…์€ ์•Œ ์ˆ˜ ์—†๋‹ค) ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ ์ด ์ •๋ณด๋ฅผ ์•Œ๋ ค์ฃผ๋ ค๋ฉด ํ•จ์ˆ˜ํ˜• ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•˜์—ฌ, ํด๋ž˜์Šค ์„ ์–ธ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊พผ๋‹ค. ์˜ˆ: ํ•จ์ˆ˜ํ˜• ์˜์กด์„ฑ class Eq e => Collection c e | c -> e where ... ํ•จ์ˆ˜ํ˜• ์˜์กด์„ฑ์€ ํƒ€์ž… ํด๋ž˜์Šค ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ์ œ์•ฝ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์ถ”๊ฐ€๋œ | c -> e๋Š” 'c๊ฐ€ e๋ฅผ ์œ ์ผํ•œ ๊ฒƒ์œผ๋กœ ํ™•์ •ํ•œ๋‹ค'๋ผ๊ณ  ์ฝ๋Š”๋‹ค. ์ฆ‰ c๊ฐ€ ์ฃผ์–ด์ง€๋ฉด e๋Š” ๋‹จ ํ•˜๋‚˜๋งŒ ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ํ•œ ํด๋ž˜์Šค ์•ˆ์—์„œ ์—ฌ๋Ÿฌ ํ•จ์ˆ˜ํ˜• ์˜์กด์„ฑ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ c -> e, e -> c๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ณต์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํด๋ž˜์Šค์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋‘ ๊ฐœ๋ณด๋‹ค ๋งŽ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ์ œ ํ–‰๋ ฌ๊ณผ ๋ฒกํ„ฐ ๊ฐ„๋‹จํ•œ ์„ ํ˜• ๋Œ€์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์˜ˆ: Vector์™€ Matrix ๋ฐ์ดํ„ฐ ํƒ€์ž… data Vector = Vector Int Int deriving (Eq, Show) data Matrix = Matrix Vector Vector deriving (Eq, Show) ์ด๊ฒƒ๋“ค์ด ์ตœ๋Œ€ํ•œ ์ˆซ์ž์ฒ˜๋Ÿผ ํ–‰๋™ํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค. ์‹œ์ž‘์€ ํ•˜์Šค์ผˆ์˜ Num ํด๋ž˜์Šค๋ฅผ ์˜ค๋ฒ„ ๋กœ๋”ฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ: Vector์™€ Matrix์˜ ์ธ์Šคํ„ด์Šค ์„ ์–ธ instance Num Vector where Vector a1 b1 + Vector a2 b2 = Vector (a1+a2) (b1+b2) Vector a1 b1 - Vector a2 b2 = Vector (a1-a2) (b1-b2) {- ... and so on ... -} instance Num Matrix where Matrix a1 b1 + Matrix a2 b2 = Matrix (a1+a2) (b1+b2) Matrix a1 b1 - Matrix a2 b2 = Matrix (a1-a2) (b1-b2) {- ... and so on ... -} ๋‘ ๊ฐ’์„ ๊ณฑํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋‹ค๋ฅธ ํƒ€์ž…๋“ค์„ ์˜ค๋ฒ„๋กœ๋“œ ํ•˜๋Š” ๊ณฑ์…ˆ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์˜ˆ: ์šฐ๋ฆฌ์—๊ฒŒ ํ•„์š”ํ•œ ๊ฒƒ (*) :: Matrix -> Matrix -> Matrix (*) :: Matrix -> Vector -> Vector (*) :: Matrix -> Int -> Matrix (*) :: Int -> Matrix -> Matrix {- ... and so on ... -} ์ด๊ฒƒ๋“ค์„ ๋ชจ๋‘ ํ—ˆ์šฉํ•˜๋Š” ํƒ€์ž… ํด๋ž˜์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ๊ธฐ์ˆ ํ•  ๊ฒƒ์ธ๊ฐ€? ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹œ๋„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: ๋น„ํšจ์œจ์ ์ธ ์‹œ๋„ (๋„ˆ๋ฌด ์ผ๋ฐ˜ํ™”๋จ) class Mult a b c where (*) :: a -> b -> c instance Mult Matrix Matrix Matrix where {- ... -} instance Mult Matrix Vector Vector where {- ... -} ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋˜ ๋ฐ”๊ฐ€ ์•„๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋‹จ์ˆœํ•œ ํ‘œํ˜„ ์‹๋„ ์ค‘๊ฐ„ ํ‘œํ˜„์‹์— ํƒ€์ž… ์„ ์–ธ์„ ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด ํƒ€์ž…์ด ๋ชจํ˜ธํ•ด์ง„๋‹ค. ์˜ˆ: ๋ชจํ˜ธํ•จ์œผ๋กœ ์ธํ•ด ๋” ์ง€์ €๋ถ„ํ•ด์ง„ ์ฝ”๋“œ m1, m2, m3 :: Matrix (m1 * m2) * m3 -- type error; type of (m1*m2) is ambiguous (m1 * m2) :: Matrix * m3 -- this is ok ๋ˆ„๊ตฐ๊ฐ€ ๋‚˜์ค‘์— ์™€์„œ๋Š” ๋˜ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋ฅผ ์ถ”๊ฐ€ํ• ์ง€๋„ ๋ชจ๋ฅด๋Š” ์ผ์ด๋‹ค. ์˜ˆ: ์—‰ํ„ฐ๋ฆฌ Mult ์ธ์Šคํ„ด์Šค instance Mult Matrix Matrix (Maybe Char) where {- whatever -} ๋ฌธ์ œ๋Š” c๊ฐ€ ์ž์œ  ํƒ€์ž… ๋ณ€์ˆ˜์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ณฑํ•˜๋Š” ๊ฒƒ๋“ค์˜ ํƒ€์ž…์„ ์•Œ๋ฉด ๊ฒฐ๊ณผ ํƒ€์ž…์„ ์ด ์ •๋ณด๋งŒ์œผ๋กœ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ํ•จ์ˆ˜ํ˜• ์˜์กด์„ฑ์œผ๋กœ ์ด๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: Mult์˜ ์˜ฌ๋ฐ”๋ฅธ ์ •์˜ class Mult a b c | a b -> c where (*) :: a -> b -> c c๊ฐ€ a์™€ b๋กœ๋ถ€ํ„ฐ ์œ ์ผํ•˜๊ฒŒ ๊ฒฐ์ •๋œ๋‹ค๊ณ  ํ•˜์Šค์ผˆ์—๊ฒŒ ์•Œ๋ฆฐ๋‹ค. 4 ํŒฌํ…€ ํƒ€์ž… ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Phantom_types ํŒฌํ…€ ํƒ€์ž…์€ ํ•˜์Šค์ผˆ์˜ ํƒ€์ž… ์‹œ์Šคํ…œ๋ณด๋‹ค ๊ฐ•๋ ฅํ•œ ํƒ€์ž… ์‹œ์Šคํ…œ์„ ๊ฐ€์ง„ ์–ธ์–ด๋ฅผ ๋ผ์›Œ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ํŒฌํ…€ ํƒ€์ž… ํ‰๋ฒ”ํ•œ ํƒ€์ž… data T = TI Int | TS String plus :: T -> T -> T concat :: T -> T -> T ํŒฌํ…€ ํƒ€์ž… ๋ฒ„์ „ data T a = TI Int | TS String ๋ณ€ํ•œ ๊ฒƒ์€ ์—†๋‹ค. ์ธ์ž a๋ฅผ ์ƒˆ๋กœ ๋„ฃ์—ˆ์„ ๋ฟ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งˆ๋ฒ•์ด ์ผ์–ด๋‚ฌ๋‹ค! plus :: T Int -> T Int -> T Int concat :: T String -> T String -> T String ์ด์ œ ๋” ๊ฐ•ํ•œ ์ œ์•ฝ์„ ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ํƒ€์ž… ์•ˆ์ „์„ฑ์„ ๋Š˜๋ฆฌ๊ณ  ์‹ถ์ง€๋งŒ ๋Ÿฐํƒ€์ž„ ์˜ค๋ฒ„ํ—ค๋“œ๋Š” ์›ํ•˜์ง€ ์•Š์„ ๋•Œ ํŒฌํ…€ ํƒ€์ž…์ด ์œ ์šฉํ•˜๋‹ค. -- Peano numbers at the type level. data Zero = Zero data Succ a = Succ a -- Example: 3 can be modeled as the type -- Succ (Succ (Succ Zero))) type D2 = Succ (Succ Zero) type D3 = Succ (Succ (Succ Zero)) data Vector n a = Vector [a] deriving (Eq, Show) vector2d :: Vector D2 Int vector2d = Vector [1,2] vector3d :: Vector D3 Int vector3d = Vector [1,2,3] -- vector2d == vector3d raises a type error -- at compile-time: -- Couldn't match expected type `Zero' -- with actual type `Succ Zero' -- Expected type: Vector D2 Int -- Actual type: Vector D3 Int -- In the second argument of `(==)', namely `vector3d' -- In the expression: vector2d == vector3d -- while vector2d == Vector [1,2,3] works 5 GADT ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/GADT ์„œ๋ก  GADT์˜ ์ดํ•ด ์‚ฐ์ˆ ์‹ ์–ธ์–ด ํ™•์žฅํ•˜๊ธฐ ํŒฌํ…€ ํƒ€์ž… GADT ์š”์•ฝ ๋ฌธ๋ฒ• ์ƒˆ๋กœ์šด ๊ฐ€๋Šฅ์„ฑ๋“ค ์˜ˆ์ œ ์•ˆ์ „ํ•œ ๋ฆฌ์ŠคํŠธ ๊ฐ„๋‹จํ•œ ํ‘œํ˜„์‹ ํ‰๊ฐ€์ž ๋…ผ์˜์‚ฌํ•ญ ํŒฌํ…€ ํƒ€์ž… ์กด์žฌ์  ํƒ€์ž… ์„œ๋ก  ์ผ๋ฐ˜ํ™”๋œ ๋Œ€์ˆ˜์  ์ž๋ฃŒํ˜•(Generalized algebraic datatype; GADT)๋Š” ์ต์ˆ™ํ•œ ๋Œ€์ˆ˜์  ์ž๋ฃŒํ˜•์„ ์ผ๋ฐ˜ํ™”ํ•œ ๊ฒƒ์ด๋‹ค. GADT๋Š” ์ƒ์„ฑ์ž์˜ ํƒ€์ž…์„ ๋ช…์‹œ์ ์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. ์ด ์žฅ์—์„œ๋Š” ์ด๊ฒƒ์ด ์™œ ์œ ์šฉํ•˜๊ณ , ์–ด๋–ป๊ฒŒ ์„ ์–ธํ•˜๋Š”์ง€ ๋ฐฐ์šธ ๊ฒƒ์ด๋‹ค. ์‹œ์ž‘์€ GADT์— ๋Œ€ํ•œ ํ›Œ๋ฅญํ•œ ๊ธฐ๋ฐ˜์ด ๋˜๋Š”, ๊ฐ„๋‹จํ•œ ์‚ฐ์ˆ ์‹์„ ์œ„ํ•œ ๋‚ด์žฅ๋œ ๋„๋ฉ”์ธ ํ•œ์ • ์–ธ์–ด(EDSL)๋ฅผ ์„ค๊ณ„ํ•˜๋Š” ์˜ˆ์ œ๋‹ค. ๊ทธ๋‹ค์Œ GADT์˜ ๋ฌธ๋ฒ•์„ ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•˜๊ณ , ๋˜ ๋‹ค๋ฅธ ์˜ˆ์ œ๋กœ์„œ head []๊ฐ€ ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ์‹คํŒจํ•˜๊ธฐ ๋•Œ๋ฌธ์— *** Exception: Prelude.head: empty list ๊ฐ™์€ ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”, ์•ˆ์ „ํ•œ ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์„ ๊ตฌ์ถ•ํ•  ๊ฒƒ์ด๋‹ค. GADT์˜ ์ดํ•ด GADT๋ž€ ๋ฌด์—‡์ด๊ณ  ์–ด๋””์— ์œ ์šฉํ•œ๊ฐ€? GADT๋Š” ์ฃผ๋กœ ๋„๋ฉ”์ธ ํ•œ์ • ์–ธ์–ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์“ฐ์ด๋ฏ€๋กœ ์ด ์ ˆ์—์„œ๋Š” ๊ทธ์— ๋งž๋Š” ์˜ˆ์ œ๋ฅผ ์†Œ๊ฐœํ•  ๊ฒƒ์ด๋‹ค. ์‚ฐ์ˆ ์‹ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์žˆ์„ ๋•Œ, ์‚ฐ์ˆ ์‹์„ ์œ„ํ•œ ์กฐ๊ทธ๋งŒ ์–ธ์–ด๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. data Expr = I Int -- integer constants | Add Expr Expr -- add two expressions | Mul Expr Expr -- multiply two expressions ์ด ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ์ถ”์ƒ์ ์ธ ๊ตฌ๋ฌธ ํŠธ๋ฆฌ์— ๋Œ€์‘ํ•œ๋‹ค. (5+1)*7 ๊ฐ™์€ ํ•ญ์€ (I 5AddI 1) MulI 7 :: Expr๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ถ”์ƒ ๊ตฌ๋ฌธ ํŠธ๋ฆฌ๊ฐ€ ์žˆ์œผ๋‹ˆ ๋ฌด์–ธ๊ฐ€ ํ•ด๋ณด์ž. ์ปดํŒŒ์ผํ•œ๋‹ค๊ฑฐ๋‚˜ ์ตœ์ ํ™”ํ•œ๋‹ค๊ฑฐ๋‚˜. ์ดˆ๋ณด์ž๋ฅผ ์œ„ํ•ด ํ‘œํ˜„์‹์„ ์ทจํ•ด ๊ทธ๊ฒƒ์ด ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ‰๊ฐ€ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. ๊ทธ ์ •์˜๋Š” ์ง๊ด€์ ์ด๋‹ค. eval :: Expr -> Int eval (I n) = n eval (Add e1 e2) = eval e1 + eval e2 eval (Mul e1 e2) = eval e1 * eval e2 ์–ธ์–ด ํ™•์žฅํ•˜๊ธฐ ์ด์ œ ์ด ์–ธ์–ด๋ฅผ ์ •์ˆ˜ ์ด์™ธ์˜ ๋‹ค๋ฅธ ํƒ€์ž…๋“ค๋กœ ํ™•์žฅํ•˜๋Š” ๊ฒƒ์„ ์ƒ์ƒํ•ด ๋ณด์ž. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•ญ๋“ฑ ๋น„๊ต๋ฅผ ๋‚˜ํƒ€๋‚ด๋ ค๋ฉด ์ง„์œ„ ๊ฐ’์ด ํ•„์š”ํ•˜๋‹ค. Expr ํƒ€์ž…์„ ์กฐ์ •ํ•˜์ž. data Expr = I Int | B Bool -- boolean constants | Add Expr Expr | Mul Expr Expr | Eq Expr Expr -- equality test 5+1 == 7์€ (I 5AddI 1) EqI 7๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ํ‘œํ˜„์‹ ํ‰๊ฐ€ ํ•จ์ˆ˜๋กœ ๋Œ์•„๊ฐ€๋ฉด, ํ‘œํ˜„์‹์ด ์ด์ œ ์ •์ˆ˜ ๋˜๋Š” ์ง„์œ„ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜ํ™˜ ํƒ€์ž…์— ๊ทธ๊ฒƒ์„ ๋ฐ˜์˜ํ•ด์•ผ ํ•œ๋‹ค. eval :: Expr -> Either Int Bool ์ฒ˜์Œ์˜ ๋‘ ๊ฒฝ์šฐ๋Š” ์ง๊ด€์ ์ด๋‹ค. eval (I n) = Left n eval (B b) = Right b ์ด๋‹ค์Œ์ด ๋ฌธ์ œ๋‹ค. eval (Add e1 e2) = eval e1 + eval e2 -- ??? ์ด๊ฒƒ์€ ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•œ๋‹ค. ๋”ํ•˜๊ธฐ ํ•จ์ˆ˜ +๋Š” ๋‘ ์ •์ˆ˜ ์ธ์ž๋ฅผ ๊ธฐ๋Œ€ํ•˜์ง€๋งŒ eval e1์˜ ํƒ€์ž…์€ Either Int Bool์ด๊ธฐ ๋•Œ๋ฌธ์— Int๋ฅผ ์ถ”์ถœํ•ด์•ผ ํ•œ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ e1์ด ์ง„์œ„ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๊ธฐ๋ผ๋„ ํ•œ๋‹ค๋ฉด? ๋‹ค์Œ์€ ํƒ€๋‹นํ•œ ํ‘œํ˜„์‹์ด๋‹ค. B True `Add` I 5 :: Expr ํ•˜์ง€๋งŒ ๋ง์ด ์•ˆ ๋œ๋‹ค. ์ง„์œ„ ๊ฐ’๊ณผ ์ •์ˆ˜๋ฅผ ๋”ํ•  ์ˆœ ์—†๋‹ค. ํ‰๊ฐ€๋Š” ์ •์ˆ˜ ๋˜๋Š” ์ง„์œ„ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ํ‘œํ˜„์‹์ด ๋ง์ด ์•ˆ ๋˜์–ด ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๊ฒƒ๋งˆ์ € ๋ฐ˜ํ™˜ ํƒ€์ž…์— ๋ฐ˜์˜ํ•˜๋ฉด eval :: Expr -> Maybe (Either Int Bool) ์ด ํ•จ์ˆ˜๋Š” ์ž˜ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์—ฌ์ „ํžˆ ๋งŒ์กฑ์Šค๋Ÿฝ์ง€ ๋ชปํ•˜๋‹ค. ์šฐ๋ฆฌ๋Š” ํ•˜์Šค์ผˆ์˜ ํƒ€์ž… ์‹œ์Šคํ…œ์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ํ‘œํ˜„์‹์„ ์•Œ์•„์„œ ๊ฑธ๋Ÿฌ๋‚ด๊ธธ ์›ํ•œ๋‹ค. ์ถ”์ƒ ๊ตฌ๋ฌธ ํŠธ๋ฆฌ๋ฅผ ๋ถ„ํ•ดํ•˜๋ฉด์„œ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ํƒ€์ž…์„ ๊ฒ€์‚ฌํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ์—ฐ์Šต๋ฌธ์ œ ๊ทธ๋Ÿผ์—๋„ ์œ„์˜ eval ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๊ต์œก์ ์ผ ์ˆ˜ ์žˆ๋‹ค. ํ•œ ๋ฒˆ ํ•ด๋ณด๋ผ. ์ถœ๋ฐœ์ : data Expr = I Int | B Bool -- boolean constants | Add Expr Expr | Mul Expr Expr | Eq Expr Expr -- equality test eval :: Expr -> Maybe (Either Int Bool) -- Your implementation here. ํŒฌํ…€ ํƒ€์ž… ์†Œ์œ„ ํŒฌํ…€ ํƒ€์ž…์ด๋ผ๋Š” ๊ฒƒ์ด ์šฐ๋ฆฌ ๋ชฉํ‘œ๋ฅผ ํ–ฅํ•œ ์ฒซ๊ฑธ์Œ์ด๋‹ค. Expr์— ํƒ€์ž… ๋ณ€์ˆ˜ a๋ฅผ ๋ง๋ถ™์ธ๋‹ค. data Expr a = I Int | B Bool | Add (Expr a) (Expr a) | Mul (Expr a) (Expr a) | Eq (Expr a) (Expr a) ํ‘œํ˜„์‹ Expr a๋Š” a ๊ฐ’์„ ์ „ํ˜€ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ž˜์„œ a๋ฅผ ํŒฌํ…€ ํƒ€์ž…์ด๋ผ ํ•œ๋‹ค. a๋Š” ์ž‰์—ฌ ๋ณ€์ˆ˜์ผ ๋ฟ์ด๋‹ค. a๋ฅผ ํ•œ ๋ฌด๋”๊ธฐ ํฌํ•จํ•˜๋Š” ๋ฆฌ์ŠคํŠธ [a] ๊ฐ™์€ ๊ฒƒ๊ณผ ๋น„๊ตํ•ด ๋ณด๋ผ. ํ•ต์‹ฌ์€ a๋ฅผ ์ด์šฉํ•ด ํ‘œํ˜„์‹์˜ ํƒ€์ž…์„ ์ถ”์ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ์˜ ์ƒ์„ฑ์ž๋ฅผ ์œ ์ €๋กœ๋ถ€ํ„ฐ ์ˆจ๊ธฐ๊ณ  Add :: Expr a -> Expr a -> Expr a ๋” ์ œํ•œ๋œ ํƒ€์ž…์˜ ์Šค๋งˆํŠธ ์ƒ์„ฑ์ž๋ฅผ ์ œ๊ณตํ•  ๊ฒƒ์ด๋‹ค. add :: Expr Int -> Expr Int -> Expr Int add = Add ๊ตฌํ˜„์€ ๊ฐ™์ง€๋งŒ ํƒ€์ž…์€ ๋‹ฌ๋ผ์กŒ๋‹ค. ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋“ค์—๋„ ๊ฐ™์€ ์ž‘์—…์„ ํ•œ๋‹ค. i :: Int -> Expr Int i = I b :: Bool -> Expr Bool b = B ์•ž์„œ ๋ฌธ์ œ๊ฐ€ ๋˜์—ˆ๋˜ ํ‘œํ˜„์‹์€ b True `add` i 5 ๋” ์ด์ƒ ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ์‹คํŒจํ•˜์ง€ ์•Š๋Š”๋‹ค! ๋ฌด์—‡๋ณด๋‹ค ์ฒซ ๋ฒˆ์งธ ์ธ์ž์˜ ํƒ€์ž…์€ Expr Bool์ธ๋ฐ add๋Š” Expr Int๋ฅผ ๊ธฐ๋Œ€ํ•œ๋‹ค. ํŒฌํ…€ ํƒ€์ž… a๋Š” ์˜๋„๋œ ํ‘œํ˜„์‹ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์Šค๋งˆํŠธ ์ƒ์„ฑ์ž๋งŒ ๊ณต๊ฐœํ•˜๋ฉด ์‚ฌ์šฉ์ž๋Š” ํƒ€์ž…์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ํ‘œํ˜„์‹์„ ๋งŒ๋“ค ์ˆ˜๊ฐ€ ์—†๋‹ค. ์•„์ง ํ‰๊ฐ€ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ์ƒˆ๋กœ์šด ํ‘œ์‹ a์„ ๋”ํ•˜์—ฌ ์ด๋Ÿฐ ํƒ€์ž…์„ ๊ธฐ๋Œ€ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. eval :: Expr a -> a ๊ทธ๋ฆฌ๊ณ  ์ฒซ ๋ฒˆ์งธ ๊ฒฝ์šฐ๋ฅผ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•œ๋‹ค. eval (I n) = n ์•„, ์• ์„ํ•˜์ง€๋งŒ ์•ˆ๋œ๋‹ค. ์ƒ์„ฑ์ž I๊ฐ€ a = Int๋ฅผ ๋œปํ•œ๋‹ค๋Š” ๊ฑธ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค. ๋ฌผ๋ก  ์šฐ๋ฆฌ ์–ธ์–ด๋ฅผ ์“ฐ๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋งŒ๋“  ํ‘œํ˜„์‹์—๋Š” ์Šค๋งˆํŠธ ์ƒ์„ฑ์ž๋งŒ ์“ฐ์˜€๋‹ค. ํ•˜์ง€๋งŒ ๋‚ด๋ถ€์ ์œผ๋กœ ์ด๋Ÿฐ ํ‘œํ˜„์‹์€ I 5 :: Expr String ์—ญ์‹œ ํƒ€๋‹นํ•˜๋‹ค. a๋Š” Int๋‚˜ Bool ์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋ฌด์—‡์ด๋“  ๋  ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ์—๊ฒ ์ƒ์„ฑ์ž์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์„ ์ œํ•œํ•  ์ˆ˜๋‹จ์ด ํ•„์š”ํ•˜๋‹ค. ์ผ๋ฐ˜ํ™”๋œ ์ž๋ฃŒํ˜•์ด ๋ฐ”๋กœ ๊ทธ๋Ÿฐ ์ผ์„ ํ•œ๋‹ค. GADT ์ƒ์„ฑ์ž์˜ ํƒ€์ž…์„ ์ œํ•œํ•˜๋Š” ๋ช…๋ฐฑํ•œ ํ‘œ๊ธฐ๋ฒ•์€ ๊ทธ ํƒ€์ž…์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. GADT๊ฐ€ ๊ทธ๋ ‡๊ฒŒ ์ •์˜๋œ๋‹ค. data Expr a where I :: Int -> Expr Int B :: Bool -> Expr Bool Add :: Expr Int -> Expr Int -> Expr Int Mul :: Expr Int -> Expr Int -> Expr Int Eq :: Expr Int -> Expr Int -> Expr Bool ์ฆ‰ ๋ชจ๋“  ์ƒ์„ฑ์ž์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜๋ฅผ ๋‹จ์ˆœํžˆ ๋‚˜์—ดํ•œ๋‹ค. ํŠนํžˆ ์Šค๋งˆํŠธ ์ƒ์„ฑ์ž์—์„œ ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ, ๋งˆ์ปค ํƒ€์ž… a๊ฐ€ ํ•„์š”์— ๋”ฐ๋ผ Int๋‚˜ Bool๋กœ ํŠนํ™”๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  GADT์˜ ๊ต‰์žฅํ•œ ์ ์€ ์ด์ œ ํƒ€์ž… ๋งˆ์ปค๋ฅผ ํ™œ์šฉํ•ด ํ‰๊ฐ€ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. eval :: Expr a -> a eval (I n) = n eval (B b) = b eval (Add e1 e2) = eval e1 + eval e2 eval (Mul e1 e2) = eval e1 * eval e2 eval (Eq e1 e2) = eval e1 == eval e2 ํŠนํžˆ ์ฒซ ๋ฒˆ์งธ ๊ฒฝ์šฐ์— eval (I n) = n ์ƒ์„ฑ์ž I๋ฅผ ๋งŒ๋‚˜๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ด์ œ a = Int์ด๊ณ  n :: Int๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ํƒ€๋‹นํ•˜๋‹ค๊ณ  ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋“ค๋„ ๋น„์Šทํ•˜๋‹ค. ์š”์•ฝํ•˜๋ฉด GADT๋Š” ์ƒ์„ฑ์ž์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์„ ์ œํ•œํ•˜๊ณ  ๋”ฐ๋ผ์„œ ํ•˜์Šค์ผˆ์˜ ํƒ€์ž… ์‹œ์Šคํ…œ์„ ๋„๋ฉ”์ธ ํ•œ์ • ์–ธ์–ด์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ๋”ฐ๋ผ์„œ ๋” ํ’๋ถ€ํ•œ ์–ธ์–ด๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ๊ทธ ๊ตฌํ˜„์€ ๋” ๊ฐ„๋‹จํ•ด์ง„๋‹ค. ์š”์•ฝ ๋ฌธ๋ฒ• ๋‹ค์Œ์€ GADT ์„ ์–ธ์— ๊ด€ํ•œ ๋ฌธ๋ฒ•์„ ์งง๊ฒŒ ์š”์•ฝํ•œ ๊ฒƒ์ด๋‹ค. ๋จผ์ € ํ‰๋ฒ”ํ•œ ๋Œ€์ˆ˜์  ์ž๋ฃŒํ˜•๋“ค์„ ๋ณด์ž. List, Maybe๋Š” ์ต์ˆ™ํ•˜๊ณ , ๊ฐ„๋‹จํ•œ ํŠธ๋ฆฌ ํƒ€์ž…์ธ RoseTree์ด ์žˆ๋‹ค. Maybe List RoseTree data Maybe a = Nothing | Just a data List a = Nil | Cons a (List a) data RoseTree a = RoseTree a [RoseTree a] ์œ„์˜ ์„ ์–ธ์—์„œ ์ƒ์„ฑ์ž๋“ค์€ ํŒจํ„ด ๋งค์นญ์„ ํ†ตํ•ด ๊ฐ’์„ ๋ถ„ํ•ดํ•˜๋Š” ๋ฐ ์“ธ ์ˆ˜๋„ ์žˆ๊ณ , ๊ฐ’์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜๋กœ์จ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Œ์„ ๊ธฐ์–ตํ•  ๊ฒƒ. (Nothing๊ณผ Nil์€ "์ธ์ž๊ฐ€ ์—†๋Š”" ํ•จ์ˆ˜๋‹ค) ํ›„์ž์˜ ๊ฒฝ์šฐ, ๊ทธ ํƒ€์ž…์„ ์งˆ์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. Maybe List RoseTree > :t Nothing Nothing :: Maybe a > :t Just Just :: a -> Maybe a > :t Nil Nil :: List a > :t Cons Cons :: a -> List a -> List a > :t RoseTree RoseTree :: a -> [RoseTree a] -> RoseTree a Maybe, List, RoseTree์˜ ์ƒ์„ฑ์ž์— ๊ด€ํ•œ ๊ฐ๊ฐ์˜ ํƒ€์ž… ์ •๋ณด๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์„ ์–ธํ•  ๋•Œ ์ปดํŒŒ์ผ๋Ÿฌ์— ์ œ๊ณตํ•œ ์ •๋ณด์™€ ๋™๋“ฑํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋‹จ์ˆœํžˆ ๋ชจ๋“  ์ƒ์„ฑ์ž์˜ ํƒ€์ž…์„ ์—ด๊ฑฐํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•ด ๋ณผ ๋ฒ• ํ•˜๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ GADT ๊ตฌ๋ฌธ์ด ํ•˜๋Š” ์ผ์ด๋‹ค. Maybe List RoseTree data Maybe a where Nothing :: Maybe a Just :: a -> Maybe a data List a where Nil :: List a Cons :: a -> List a -> List a data RoseTree a where RoseTree :: a -> [RoseTree a] -> RoseTree a ์ด ๋ฌธ๋ฒ•์€ ์–ธ์–ด ์˜ต์…˜ {-#LANGUAGE GADTs #-}์œผ๋กœ ํ™œ์„ฑํ™”ํ•œ๋‹ค. ํƒ€์ž… ์„ ์–ธ ๋ฌธ๋ฒ•๊ณผ ์ƒ๋‹นํžˆ ๋‹ฎ์•„์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์นœ์ˆ™ํ•  ๊ฒƒ์ด๋‹ค. ์ด๋ฏธ ์ƒ์„ฑ์ž๋ฅผ ํ•จ์ˆ˜๋กœ ์ƒ๊ฐํ•˜๊ธธ ์ข‹์•„ํ•œ๋‹ค๋ฉด ๊ธฐ์–ตํ•˜๊ธฐ๋„ ์‰ฌ์šธ ๊ฒƒ์ด๋‹ค. ๊ฐ ์ƒ์„ฑ์ž๊ฐ€ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ์— ์˜ํ•ด ์ •์˜๋  ๋ฟ์ด๋‹ค. ์ƒˆ๋กœ์šด ๊ฐ€๋Šฅ์„ฑ๋“ค GHCi์— Nothing๊ณผ Just์˜ ํƒ€์ž…์„ ์งˆ์˜ํ–ˆ์„ ๋•Œ Maybe a์™€ a -> Maybe a๋ฅผ ๋Œ๋ ค์ฃผ์—ˆ๋˜ ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. ์ด ๊ฒฝ์šฐ ๊ทธ ์ƒ์„ฑ์ž์™€ ์—ฐ๊ด€๋œ ํ•จ์ˆ˜์˜ ์ตœ์ข… ์ถœ๋ ฅ ํƒ€์ž…์€ ์šฐ๋ฆฌ๊ฐ€ ์• ์ดˆ์— ์ •์˜ํ–ˆ๋˜ ๊ทธ ํƒ€์ž…, Maybe a, List a, ๋˜๋Š” RoseTree a์ด๋‹ค. ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด ํ‘œ์ค€ ํ•˜์Šค์ผˆ์—์„œ Foo a์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ์ตœ์ข… ๋ฐ˜ํ™˜ ํƒ€์ž…์€ Foo a์ด๋‹ค. ์ƒˆ ๊ตฌ๋ฌธ์ด ๊ธฐ์กด ๊ตฌ๋ฌธ๊ณผ ์—„๋ฐ€ํžˆ ๋™๋“ฑํ•˜๋‹ค๋ฉด, ์˜ฌ๋ฐ”๋ฅธ ํƒ€์ž… ์„ ์–ธ์„ ์œ„ํ•ด ๊ทธ ์‚ฌ์šฉ์— ์ œํ•œ์„ ๋‘์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ GADT๊ฐ€ ์ฃผ๋Š” ์ด์ ์€ ๋ฌด์—‡์ธ๊ฐ€? ์ •ํ™•ํžˆ ์–ด๋–ค ์ข…๋ฅ˜์˜ Foo๋ฅผ ๋ฐ˜ํ™˜ํ• ์ง€๋ฅผ ์ œ์–ดํ•˜๋Š” ๋Šฅ๋ ฅ์ด๋‹ค. GADT๋ฅผ ํ†ตํ•˜๋ฉด Foo a์˜ ์ƒ์„ฑ์ž๊ฐ€ ๋ฐ˜๋“œ์‹œ Foo a๋ฅผ ๋ฐ˜ํ™˜ํ•  ์˜๋ฌด๊ฐ€ ์—†๋‹ค. ์ƒ์„ฑ์ž๋Š” ์ž„์˜์˜ Foo blah๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ GadtedFoo ์ƒ์„ฑ์ž๋Š” GatedFoo x ํƒ€์ž…์— ๋Œ€ํ•œ ์ƒ์„ฑ์ž์ž„์—๋„ GadtedFoo Int์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์˜ˆ์ œ: GADT๋Š” ๋” ๋งŽ์€ ์ œ์–ด๊ถŒ์„ ์ค€๋‹ค data FooInGadtClothing a where MkFooInGadtClothing :: a -> FooInGadtClothing a --which is no different from: data Haskell98Foo a = MkHaskell98Foo a , --by contrast, consider: data TrueGadtFoo a where MkTrueGadtFoo :: a -> TrueGadtFoo Int --This has no Haskell 98 equivalent. ํ•˜์ง€๋งŒ ์ผ๋ฐ˜ํ™”๋Š” ์—ฌ๊ธฐ๊นŒ์ง€๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์„ ์–ธํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด Foo๋ผ๋ฉด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ ์–ด๋–ค Foo๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. ๋‹ค๋ฅธ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ์˜ˆ์ œ: ์‹œ๋„ํ•ด ๋ณด๋ผ. ์•ˆ๋œ๋‹ค data Bar where BarNone :: Bar -- ๋œ๋‹ค data Foo where MkFoo :: Bar Int -- ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ์‹คํŒจํ•œ๋‹ค ์˜ˆ์ œ ์•ˆ์ „ํ•œ ๋ฆฌ์ŠคํŠธ ์„ ํ–‰์ง€์‹: ์ด ์ ˆ์—์„œ๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ํ•จ์ˆ˜ํ˜• ์–ธ์–ด์—์„œ List๊ฐ€ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„๋˜๋Š”์ง€ ์•ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค ๋…ธํŠธ: ์ด ์ ˆ์˜ ์˜ˆ์ œ๋“ค์€ EmptyDataDecls์™€ KindSignatures ํ™•์žฅ์„ ์ถ”๊ฐ€๋กœ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•œ๋‹ค GADT ๋ฌธ๋ฒ•์ด ์ฃผ๋Š” ์ถ”๊ฐ€์ ์ธ ์ œ์–ด๊ถŒ์„ ์‚ด์ง ๋ง›๋ดค๋‹ค. ์œ ์ผํ•˜๊ฒŒ ์ƒˆ๋กœ์› ๋˜ ์ ์€ ์–ด๋–ค ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ฐ˜ํ™˜ํ• ์ง€๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฑธ ์–ด๋”” ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ? ํ‰๋ฒ”ํ•œ ํ•˜์Šค ์ผˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. head []๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”๊ฐ€? ํ•˜์Šค์ผˆ์ด ํญ๋ฐœํ•œ๋‹ค. ์›์†Œ๊ฐ€ ์ ์–ด๋„ ํ•˜๋‚˜๋Š” ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋งŒ ๋ฐ›์•„๋“ค์ด๋Š”, ์ ˆ๋Œ€ ํ„ฐ์ง€์ง€ ์•Š๋Š” ๋งˆ๋ฒ•์˜ head๋ฅผ ๋ฐ”๋ž€ ์ ์ด ์žˆ๋Š”๊ฐ€? ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ ์ƒˆ๋กœ์šด ํƒ€์ž…์ธ SafeList x y๋ฅผ ์ •์˜ํ•˜์ž. ํ‰๋ฒ”ํ•œ ํ•˜์Šค ์ผˆ ๋ฆฌ์ŠคํŠธ [x]์™€ ๋น„์Šทํ•˜์ง€๋งŒ ํƒ€์ž…์— ์•ฝ๊ฐ„์˜ ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ๊ฐ€๋ฏธํ–ˆ๋‹ค. ์ด ์ถ”๊ฐ€ ์ •๋ณด(ํƒ€์ž… ๋ณ€์ˆ˜ y)๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋นˆ ๋ฆฌ์ŠคํŠธ๋Š” SafeList x Empty๋กœ, ๋น„์–ด์žˆ์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ๋Š” SafeList x NotEmpty๋กœ ํ‘œํ˜„๋œ๋‹ค. -- we have to define these types data Empty data NonEmpty -- the idea is that you can have either -- SafeList a Empty -- or SafeList a NonEmpty data SafeList a b where -- to be implemented ์ด ์—ฌ๋ถ„์˜ ์ •๋ณด ๋•์— ๋น„์–ด์žˆ์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด์„œ๋งŒ safeHead๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค! ๋นˆ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด safeHead๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํƒ€์ž… ๊ฒ€์‚ฌ๊ฐ€ ์‹คํŒจํ•œ๋‹ค. safeHead :: SafeList a NonEmpty -> a ์ด์ œ safeHead๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•  ๊ฒƒ์ธ๊ฐ€? GADT๊ฐ€ ๋‹ต์ด๋‹ค. ํ•ต์‹ฌ์€ GADT ํŠน์„ฑ์„ ํ™œ์šฉํ•ด Nil ์ƒ์„ฑ์ž์— ๋Œ€ํ•ด์„œ๋Š” SafeList a Empty, Cons ์ƒ์„ฑ์ž์— ๋Œ€ํ•ด์„œ๋Š” SafeList a NonEmpty ์ด๋ ‡๊ฒŒ ๋‘ ์ข…๋ฅ˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. data SafeList a b where Nil :: SafeList a Empty Cons :: a -> SafeList a b -> SafeList a NonEmpty ์ด๋Š” GADT๊ฐ€ ์•„๋‹ˆ๋ฉด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. GADT ์—†์ด๋Š” ๋ชจ๋“  ์ƒ์„ฑ์ž๊ฐ€ ๊ฐ™์€ ํƒ€์ž…์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด์ œ ์„œ๋กœ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์„œ๋กœ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์–ด์จŒ๋“  SafeHead์˜ ์‹ค์ œ ์ •์˜๊นŒ์ง€ ์ „๋ถ€ ๋ชจ์•„๋ณด์ž. ์˜ˆ์ œ: GADT๋ฅผ ์ด์šฉํ•œ ์•ˆ์ „ํ•œ ๋ฆฌ์ŠคํŠธ {-#LANGUAGE GADTs, EmptyDataDecls #-} -- (the EmptyDataDecls pragma must also appear at the very top of the module, -- in order to allow the Empty and NonEmpty datatype declarations.) data Empty data NonEmpty data SafeList a b where Nil :: SafeList a Empty Cons:: a -> SafeList a b -> SafeList a NonEmpty safeHead :: SafeList a NonEmpty -> a safeHead (Cons x _) = x ์ด ์ฝ”๋“œ๋ฅผ ํŒŒ์ผ๋กœ ๋ณต์‚ฌํ•ด ghci -fglasgow-exts๋กœ ๋กœ๋“œํ•˜๋ผ. safeHead๋ฅผ ๋น„์–ด์žˆ์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ์™€ ๋นˆ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ํ˜ธ์ถœํ•  ๋•Œ์˜ ์ฐจ์ด์ ์— ์ฃผ๋ชฉํ•  ๊ฒƒ. ์˜ˆ์‹œ: safeHead๋Š”... ์•ˆ์ „ํ•˜๋‹ค Prelude Main> safeHead (Cons "hi" Nil) "hi" Prelude Main> safeHead Nil <interactive>:1:9: Couldn't match `NonEmpty' against `Empty' Expected type: SafeList a NonEmpty Inferred type: SafeList a Empty In the first argument of `safeHead', namely `Nil' In the definition of `it': it = safeHead Nil ์ด๋Ÿฐ ๋ถˆํ‰์€ ์ข‹์€ ๊ฒƒ์ด๋‹ค. ์ด์ œ ์ปดํŒŒ์ผํ•  ๋•Œ safeHead๋ฅผ ์•Œ๋งž์€ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ํ˜ธ์ถœํ•œ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์—๋Š” ์œ„ํ—˜๋„ ์ž ์žฌํ•ด ์žˆ๋‹ค. ๋‹ค์Œ ํ•จ์ˆ˜๋Š” ๋ฌด์Šจ ํƒ€์ž…์ผ๊นŒ? ์˜ˆ์ œ: Trouble with GADTs silly False = Nil silly True = Cons () Nil ์ด ์˜ˆ์ œ๋ฅผ GHCi์—์„œ ๋ถˆ๋Ÿฌ์˜ค๋ ค ํ•˜๋ฉด ๋‹ค์Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. Example: Trouble with GADTs - the complaint Couldn't match `Empty' against `NonEmpty' Expected type: SafeList () Empty Inferred type: SafeList () NonEmpty In the application `Cons () Nil' In the definition of `silly': silly True = Cons () Nil silly ์ •์˜์˜ ๋‘ ๋ถ„๊ธฐ์—์„œ ๋ฆฌ์ŠคํŠธ๋“ค์˜ ํƒ€์ž…์ด ๋‹ฌ๋ผ์„œ ํƒ€์ž… ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. GADT์— ์˜ํ•œ ๋ถ€๊ฐ€์ ์ธ ์ œ์•ฝ ๋•Œ๋ฌธ์— ํ•œ ํ•จ์ˆ˜๊ฐ€ ๋นˆ ๋ฆฌ์ŠคํŠธ์™€ ๋น„์–ด์žˆ์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ ๋‘˜ ๋‹ค ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์ •๋ง๋กœ silly๋ฅผ ์ •์˜ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Cons์˜ ํƒ€์ž…์„ ์™„ํ™”ํ•˜์—ฌ ์•ˆ์ „ํ•œ ๋ฆฌ์ŠคํŠธ์™€ ์•ˆ์ „ํ•˜์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ ๋‘˜ ๋‹ค ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ์ œ: ๋‹ค๋ฅธ ์ ‘๊ทผ๋ฒ• {-#LANGUAGE GADTs, EmptyDataDecls, KindSignatures #-} -- here we add the KindSignatures pragma, -- which makes the GADT declaration a bit more elegant. -- Note the subtle yet revealing change in the phantom type names. data NotSafe data Safe data MarkedList :: * -> * -> * where Nil :: MarkedList t NotSafe Cons :: a -> MarkedList a b -> MarkedList a c safeHead :: MarkedList a Safe -> a safeHead (Cons x _) = x -- This function will never produce anything that can be consumed by safeHead, -- no matter that the resulting list is not necessarily empty. silly :: Bool -> MarkedList () NotSafe silly False = Nil silly True = Cons () Nil ์—ฌ๊ธฐ์—๋Š” ๋Œ€๊ฐ€๊ฐ€ ๋”ฐ๋ฅธ๋‹ค. Cons์— ๋Œ€ํ•œ ์ œ์•ฝ์„ ์™„ํ™”ํ•œ ๊ฒฐ๊ณผ ์šฐ๋ฆฌ๋Š” Cons๊ฐ€ ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์‚ฐํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์ •๋ณด๋ฅผ ๋ฒ„๋ ค๋ฒ„๋ ธ๋‹ค. ์•ˆ์ „ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซํŒ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ, SafeList a Empty ์ธ์ž๋ฅผ ์ทจํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ  Cons๊ฐ€ ์ƒ์‚ฐํ•˜๋Š” ๋ฌด์—‡์ด๋˜ ๋ฐ›์•„๋“ค์ด์ง€ ์•Š๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ์—ˆ๋‹ค. MarkedList a NotSafe์—๋Š” ์ด๊ฒƒ์ด ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ‹€๋ฆผ์—†์ด, ์ด ํƒ€์ž…์€ ๋œ ์ œํ•œ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋œ ์œ ์šฉํ•˜๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ํ•  ๋งŒํ•œ ๊ฒƒ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์†Œํ•œ ๋ฌธ์ œ๋กœ ๋ณด์ด์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์‹ฌ์‚ฌ์ˆ™๊ณ ํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ safeTail ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒ ๋Š”๊ฐ€? ์—ฌ๊ธฐ์„œ ์†Œ๊ฐœํ•œ ๋‘ ๋ฒ„์ „์ด๋‚˜ ๋‹ค๋ฅธ ๋ณ€ํ˜•๋“ค๋„ ์ข‹์€ ์‹œ์ž‘์ ์ด๋‹ค. ๊ฐ„๋‹จํ•œ ํ‘œํ˜„์‹ ํ‰๊ฐ€์ž Insert the example used in Wobbly Types paper... I thought that was quite pedagogical This is already covered in the first part of the tutorial. * ๋…ผ์˜์‚ฌํ•ญ ํŒฌํ…€ ํƒ€์ž… ํŒฌํ…€ ํƒ€์ž…์„ ๋ณผ ๊ฒƒ. ์กด์žฌ์  ํƒ€์ž… ์กด์žฌ ํ•œ์ • ํƒ€์ž…(Existentially quantified types)์„ ์ข‹์•„ํ•˜๋Š”๊ฐ€? ์ด๊ฒƒ์ด GADT์— ํฌํ•จ๋˜๋Š” ๊ฐœ๋…์ž„์— ์ฃผ๋ชฉํ•  ๊ฒƒ. GHC ๋งค๋‰ด์–ผ์ด ๋งํ•˜๋“ฏ์ด ๋‹ค์Œ์˜ ๋‘ ํƒ€์ž… ์„ ์–ธ์€ ๊ฐ™๋‹ค. data TE a = forall b. MkTE b (b->a) data TG a where { MkTG :: b -> (b->a) -> TG a } GADT๋ฅผ ์ด์šฉํ•œ ํ˜ผ์ข… ๋ฆฌ์ŠคํŠธ๋Š” ์ด๋Ÿฐ ์‹์ด๋‹ค. data TE2 = forall b. Show b => MkTE2 [b] data TG2 where MkTG2 :: Show b => [b] -> TG2 6 ํƒ€์ž… ์ƒ์„ฑ์ž & ์ข…(kind) ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Kinds C++ ์‚ฌ์šฉ์ž๋ฅผ ์œ„ํ•œ ์ข…(kind) *๋Š” ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜์—ฌ ์ž„์˜์˜ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์ด๋‹ค. ๋‹ค์Œ์€ ๋ชจ๋‘ * ์ข…(kind)์„ ๊ฐ€์ง„๋‹ค. type MyType = Int type MyFuncType = Int -> Int myFunc :: Int -> Int typedef int MyType; typedef int (*MyFuncType)(int); int MyFunc(int a); * -> *๋Š” ํƒ€์ž… ์ธ์ž๋ฅผ ํ•œ ๊ฐœ ์ทจํ•˜๋Š” ํ…œํ”Œ๋ฆฟ์ด๋‹ค. ํƒ€์ž…์—์„œ ํƒ€์ž…์œผ๋กœ ๊ฐ€๋Š” ํ•จ์ˆ˜์™€ ๋น„์Šทํ•˜๋‹ค. ์–ด๋–ค ํƒ€์ž…์„ ํ•˜๋‚˜ ๋„ฃ์œผ๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋Š” ์–ด๋–ค ํƒ€์ž…์ด๋‹ค. ๋‹ค์Œ์˜ MyData์˜ ๋‘ ์šฉ๋ฒ•์€ ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ๋‹ค. (๋‘˜์˜ ์ด๋ฆ„์„ ๋‹ค๋ฅด๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ) ์ฒซ ๋ฒˆ์งธ๋Š” ํƒ€์ž… ์ƒ์„ฑ์ž, ๋‘ ๋ฒˆ์งธ๋Š” ๋ฐ์ดํ„ฐ ์ƒ์„ฑ์ž๋‹ค. ๊ฐ๊ฐ C++์˜ ํด๋ž˜์Šค ํ…œํ”Œ๋ฆฟ๊ณผ ์ƒ์„ฑ์ž์— ๋Œ€์‘ํ•œ๋‹ค. ๋ฌธ๋งฅ์ด ๋ชจํ˜ธํ•จ์„ ํ•ด๊ฒฐํ•œ๋‹ค. ํ•˜์Šค์ผˆ์ด ํƒ€์ž…์„ ๊ธฐ๋Œ€ํ•˜๋Š” ๊ณณ(์˜ˆ: ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ฒ˜)์—์„œ MyData๋Š” ํƒ€์ž… ์ƒ์„ฑ์ž๋‹ค. ๊ฐ’์„ ๊ธฐ๋Œ€ํ•˜๋Š” ๊ณณ์—์„œ MyData๋Š” ๋ฐ์ดํ„ฐ ์ƒ์„ฑ์ž๋‹ค. data MyData t -- type constructor with kind * -> * = MyData t -- data constructor with type a -> MyData a *Main> :k MyData MyData :: * -> * *Main> :t MyData MyData :: a -> MyData a template <typename t> class MyData { t member; }; * -> * -> *๋Š” ํƒ€์ž… ์ธ์ž ๋‘ ๊ฐœ๋ฅผ ์ทจํ•˜๋Š” ํ…œํ”Œ๋ฆฟ์ด๋‹ค. data MyData t1 t2 = MyData t1 t2 template <typename t1, typename t2> class MyData { t1 member1; t2 member2; MyData(t1 m1, t2 m2) : member1(m1), member2(m2) { } }; (* -> *) -> *๋Š” (* -> *) ์ข…์˜ ํ…œํ”Œ๋ฆฟ ์ธ์ž ํ•˜๋‚˜๋ฅผ ์ทจํ•˜๋Š” ํ…œํ”Œ๋ฆฟ์ด๋‹ค. data MyData tmpl = MyData (tmpl Int) template <template <typename t> class tmpl> class MyData { tmpl<int> member1; MyData(tmpl<int> m) : member1(m) { } }; 3์—ฌ๋Ÿฌ ์ด๋ก ๋“ค ์—ฌ๋Ÿฌ ์ด๋ก ๋“ค ํ‘œ๊ธฐ ์˜๋ฏธ๋ก (Denotational semantics) Equational reasoning ์›๋ฌธ ์—†์Œ Program derivation ์›๋ฌธ ์—†์Œ ๋ฒ”์ฃผ๋ก  Curry-Howard ๋™ํ˜• fix์™€ ์žฌ๊ท€ 1 ํ‘œ๊ธฐ ์˜๋ฏธ๋ก  (๊ฒ€ํ†  ์ค‘) ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Denotational_semantics ์›๋ฌธ์—์„œ Interpretation as Powersets ์„น์…˜์˜ ๋‚ด์šฉ์ด ์˜ฌ๋ฐ”๋ฅธ์ง€์— ๋Œ€ํ•œ ๋…ผ์Ÿ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์„œ๋ฌธ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์€ ๋ฌด์—‡์ด๊ณ  ์–ด๋””์— ์“ฐ์ด๋Š”๊ฐ€? ์˜๋ฏธ ์—ญ์œผ๋กœ ๋ฌด์—‡์„ ์„ ํƒํ•  ๊ฒƒ์ธ๊ฐ€? ๋ฐ”ํ…€๊ณผ ๋ถ€๋ถ„ ํ•จ์ˆ˜ โŠฅ ๋ฐ”ํ…€ ๋ถ€๋ถ„ ํ•จ์ˆ˜์™€ ์˜๋ฏธ๋ก ์  ๊ทผ์‚ฌ ์ˆœ์„œ Partial Functions and the Semantic Approximation Order ๋‹จ์กฐ์„ฑ(Monotonicity) ๊ณ ์ •์  ๋ฐ˜๋ณต์œผ๋กœ์„œ์˜ ์žฌ๊ท€ ์ •์˜ (Recursive Definitions as Fixed Point Iterations) ๊ณ„์Šน(factorial) ํ•จ์ˆ˜์˜ ๊ทผ์‚ฌ ์ˆ˜๋ ด์„ฑ ๋ฐ”ํ…€์€ ๋น„์ข…๊ฒฐ์„ ๋‚ดํฌํ•œ๋‹ค Bottom includes Non-Termination ์ตœ์†Œ ๊ณ ์ •์ ์œผ๋กœ์„œ์˜ ํ•ด์„ ์—„๊ฒฉ ์˜๋ฏธ๋ก ๊ณผ ๋น„ ์—„๊ฒฉ ์˜๋ฏธ๋ก  ์—„๊ฒฉํ•œ ํ•จ์ˆ˜ ๋น„ ์—„๊ฒฉ ๋ฐ ์—„๊ฒฉํ•œ ์–ธ์–ด ๋‹ค์ธ์ž ํ•จ์ˆ˜ ๋Œ€์ˆ˜์  ์ž๋ฃŒํ˜• ์ƒ์„ฑ์ž ํŒจํ„ด ๋งค์นญ ์žฌ๊ท€์  ์ž๋ฃŒํ˜•๊ณผ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ ํ•˜์Šค ์ผˆ ํ•œ์ •: strictness annotation๊ณผ newtype ๋‹ค๋ฅธ ์ฃผ์ œ๋“ค abstract interpretation๊ณผ ์—„๊ฒฉํ•จ ๋ถ„์„ ๋ฉฑ์ง‘ํ•ฉ์œผ๋กœ์„œ์˜ ํ•ด์„ naive set์€ ์žฌ๊ท€์  ์ž๋ฃŒํ˜•์— ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค ์™ธ๋ถ€ ๋งํฌ ์„œ๋ฌธ ์ด ์žฅ์—์„œ๋Š” ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์˜ ์˜๋ฏธ๋ฅผ ๊ณต์‹ํ™”(formalize) ํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก (denotational semantics)์„ ์„ค๋ช…ํ•œ๋‹ค. "ํ”„๋กœ๊ทธ๋žจ square x = x*x์˜ ์˜๋ฏธ๋Š” ์–ด๋–ค ์ˆ˜๋ฅผ ๊ทธ ์ œ๊ณฑ์œผ๋กœ ์‚ฌ์ƒํ•˜๋Š” ์ˆ˜ํ•™์˜ ์ œ๊ณฑ ํ•จ์ˆ˜์™€ ๊ฐ™์€ ๋œป์ด๋‹ค"๋ผ๋Š” ๊ณต์‹์  ๊ธฐ์ˆ ์€ ํŠธ์ง‘ ์žก๊ธฐ์ฒ˜๋Ÿผ ๋ณด์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์˜์›ํžˆ ๋ฐ˜๋ณต๋˜๋Š” f x = f (x+1) ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ์˜ ์˜๋ฏธ๋Š” ์–ด๋–จ๊นŒ? ์•ž์œผ๋กœ ์ด ์งˆ๋ฌธ์— ๋Œ€ํ•ด Scott๊ณผ Strachey๊ฐ€ ์ฒ˜์Œ ์ทจํ•œ ์ ‘๊ทผ๋ฒ•์„ ์˜ˆ๋กœ ๋“ค๊ณ , ์ผ๋ฐ˜์ ์ธ, ํŠนํžˆ ์žฌ๊ท€ ์ •์˜๋œ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋žจ์˜ ์˜ฌ๋ฐ”๋ฆ„์„ ์ถ”๋ก ํ•˜๋Š” ๊ธฐ๋ฐ˜์„ ๋‹ฆ์„ ๊ฒƒ์ด๋‹ค. ๋ฌผ๋ก  ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์„ ์ดํ•ดํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ์ฃผ์ œ๋“ค์— ์ง‘์ค‘ํ•  ๊ฒƒ์ด๋‹ค. 1 ์ด ์žฅ์˜ ๋˜ ๋‹ค๋ฅธ ๋ชฉํ‘œ๋Š” ์—„๋ฐ€ํ•จ(strict)๊ณผ ์ง€์—ฐ(lazy) ์ฆ‰ ํ•จ์ˆ˜๊ฐ€ ์ž์‹ ์˜ ์ธ์ž๋“ค์„ ํ‰๊ฐ€ํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์€ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์˜ ํ‰๊ฐ€ ์ˆœ์„œ๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ์ด๋ฏ€๋กœ ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์ฃผ๋œ ๊ด€์‹ฌ์‚ฌ๋‹ค. ํฅ๋ฏธ๋กญ๊ฒŒ๋„ ์ด ๊ฐœ๋…๋“ค์€ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ๋งŒ์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๊ณต์‹ํ™”ํ•  ์ˆ˜ ์žˆ๊ณ  ์‹คํ–‰ ๋ชจ๋ธ์€ ๊ณ ๋ คํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ์ด๊ฒƒ๋“ค์€ ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ผ ๊ฒƒ์ด์ง€๋งŒ, ์–ด์จŒ๋“  ์ด ์žฅ์˜ ๋ชฉ์ ์€ ๋…์ž๊ฐ€ ํ‘œ๊ธฐ์  ์ •์˜์™€ โŠฅ(๋ฐ”ํ…€) ๊ฐ™์€ ๊ฐœ๋…์— ์ต์ˆ™ํ•ด์ง€๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์—„๋ฐ€ํ•จ์—๋งŒ ๊ด€์‹ฌ ์žˆ๋Š” ๋…์ž๋Š” Bottom and Partial Functions ์ ˆ์„ ํ›‘์–ด๋ณด๊ณ  Strict and Non-Strict Semantics๋กœ ๋น ๋ฅด๊ฒŒ ๋„˜์–ด๊ฐ€๋„ ์ข‹๋‹ค. ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์€ ๋ฌด์—‡์ด๊ณ  ์–ด๋””์— ์“ฐ์ด๋Š”๊ฐ€? ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์€ ๋ฌด์—‡์„ ์˜๋ฏธํ•˜๋Š”๊ฐ€? ์ด ์งˆ๋ฌธ์—๋Š” ํ•˜์Šค์ผˆ์˜ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์œผ๋กœ ๋‹ตํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์€ ๊ฐ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜ํ•™์  ๊ฐœ์ฒด(ํ‘œ๊ธฐ)๋กœ ์‚ฌ์ƒํ•œ๋‹ค. ์ด ์ˆ˜ํ•™์  ๊ฐœ์ฒด๋Š” ๊ทธ ํ”„๋กœ๊ทธ๋žจ์˜ ์˜๋ฏธ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ 10, 9+1, 2*5, sum [1.. 4]๋Š” ์ •์ˆ˜ 10์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ๋“ค์€ ๋ชจ๋‘ ์ •์ˆ˜ 10์„ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ์ˆ˜ํ•™์  ๊ฐœ์ฒด๋“ค์˜ ๋ชจ์Œ์„ ์˜๋ฏธ ์—ญ(semantic domain)์ด๋ผ ๋ถ€๋ฅธ๋‹ค. ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์—์„œ ์˜๋ฏธ ์—ญ์œผ๋กœ ๊ฐ€๋Š” ์‚ฌ์ƒ์€ ๋ณดํ†ต ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ๋ฅผ ์Œ๊ณฝ๊ด„ํ˜ธ("์˜ฅ์Šคํฌ๋“œ ๊ด„ํ˜ธ")๋กœ ๊ฐ์‹ธ์„œ ํ‘œํ˜„ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์ด๋Ÿฐ ๊ฒƒ์ด๋‹ค. [ [ โˆ— = 10 ] ] ํ‘œ๊ธฐ๋Š” ๊ตฌ์„ฑ์ (compositional)์ด๋‹ค. ์ฆ‰ 1+9 ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ์˜ ์˜๋ฏธ๋Š” ๊ตฌ์„ฑ ์„ฑ๋ถ„๋“ค์˜ ์˜๋ฏธ์—๋งŒ ์˜์กดํ•œ๋‹ค. [ [ + ] ] [ [ ] ] [ [ ] ] ํƒ€์ž…์—๋„ ๊ฐ™์€ ํ‘œ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. [ [ n e e ] ] Z ํ•˜์ง€๋งŒ ๊ฐ„๋‹จํ•จ์„ ์œ„ํ•ด ์•ž์œผ๋กœ๋Š” ํ‘œํ˜„์‹์„ ์˜๋ฏธ๋ก  ๊ฐœ์ฒด์™€ ์•”๋ฌต์ ์œผ๋กœ ๋™์ผ์‹œํ•˜๊ณ , ์ด๋Ÿฐ ํ‘œ๊ธฐ๋Š” ๋ช…ํ™•ํ•จ์ด ํ•„์š”ํ•  ๋•Œ๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค. ํ•˜์Šค ์ผˆ ๊ฐ™์€ ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์–ธ์–ด์˜ ํ•ต์‹ฌ์€ "1+9๋Š” 10์„ ์˜๋ฏธํ•œ๋‹ค" ๊ฐ™์€ ์ˆ˜ํ•™์  ํ•ด์„์ด ํ•จ์ˆ˜์— ๊ทธ๋Œ€๋กœ ์ ์šฉ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. Integer -> Integer ํƒ€์ž…์ธ ํ”„๋กœ๊ทธ๋žจ์˜ ๋ณธ์งˆ์  ์˜๋ฏธ๋Š” ์ •์ˆ˜ ๊ฐ„์˜ ์ˆ˜ํ•™์  ํ•จ์ˆ˜ ( โ†’ )์ด๋‹ค. ๋‚˜์ค‘์— ๋ณด๊ฒ ์ง€๋งŒ ์ด ํ‘œํ˜„์‹์€ ๋น„์ข…๊ฒฐ(non-termination)์„ ํฌํ•จํ•˜๋„๋ก ์ •์ œํ•ด์•ผ ํ•œ๋‹ค. ์–ด์จŒ๋“  ๋ช…๋ นํ˜• ์–ธ์–ด๋Š” ์ƒํ™ฉ์ด ๋” ๋‚˜์˜๋‹ค. ์ด ํƒ€์ž…์„ ๊ฐ€์ง„ ํ”„๋Ÿฌ์‹œ์ €๋Š” ๊ธฐ๊ณ„์˜ ์ƒํƒœ๋ฅผ ์˜๋„ํ•˜์ง€ ์•Š์€ ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌด์–ธ๊ฐ€๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋ช…๋ นํ˜• ์–ธ์–ด๋Š” ๊ธฐ๊ณ„์˜ ์‹คํ–‰ ๋ฐฉ์‹์„ ์„œ์ˆ ํ•˜๋Š” ๋ช…๋ น ์˜๋ฏธ๋ก (operational semantics)๊ณผ ๊ธด๋ฐ€ํžˆ ์—ฐ๊ด€๋œ๋‹ค. ๋ช…๋ นํ˜• ํ”„๋กœ๊ทธ๋žจ์˜ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์„ ์ •์˜ํ•˜๊ณ  ๊ทธ๋Ÿฐ ํ”„๋กœ๊ทธ๋žจ์— ๋Œ€ํ•ด ์ถ”๋ก ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๊ทธ ์˜๋ฏธ๋Š” ๋ช…๋ นํ˜•(operational) ์„ฑ์งˆ์„ ๋„๊ธฐ ๋งˆ๋ จ์ด๊ณ  ๊ทธ๋Ÿฌ๋ฉด ํ•จ์ˆ˜ํ˜• ์–ธ์–ด์˜ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์— ๋น„ํ•ด ํ™•์žฅ์„ ํ•ด์•ผ ํ•œ๋‹ค.2 ๋ฐ˜๋ฉด ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์–ธ์–ด์˜ ์˜๋ฏธ๋Š” ๊ธฐ๋ณธ์œผ๋กœ ์‹คํ–‰ ๋ฐฉ์‹๊ณผ ์™„์ „ํžˆ ๋ฌด๊ด€ํ•˜๋‹ค. Haskell98 ํ‘œ์ค€์€ ํ•˜์Šค์ผˆ์˜ ์—„๋ฐ€ํ•˜์ง€ ์•Š์€ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ๋งŒ์„ ๋ช…์‹œํ•˜์—ฌ, ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ์ง€๋Š” ์ž์œ ๋กœ ๋‚จ๊ฒจ๋‘์—ˆ๋‹ค. ๊ฒฐ๋ก ์€, ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์€ ์šฐ๋ฆฌ๊ฐ€ ์ˆ˜ํ•™์ ์œผ๋กœ ์›ํ•˜๋Š” ๋Œ€๋กœ ํ”„๋กœ๊ทธ๋žจ์ด ์ž‘๋™ํ•œ๋‹ค๋Š” ๊ฒƒ์„<NAME>์ ์œผ๋กœ ์ฆ๋ช…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์•„์ด๋Ÿฌ๋‹ˆํ•˜๊ฒŒ๋„ ํ•˜์Šค์ผˆ์—์„œ ์–ด๋–ค ํ”„๋กœ๊ทธ๋žจ ์„ฑ์งˆ์„ ์ฆ๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๋“ฑ์‹ ์ถ”๋ก (equational reasoning)์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด ๋ฐฉ๋ฒ•์€ ํ”„๋กœ๊ทธ๋žจ์„ ๋™๋“ฑํ•œ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉฐ ๊ธฐ์ €์˜ ์ˆ˜ํ•™์  ๊ฐœ์ฒด๋“ค์€ ๊ฑฐ์˜ ๋“ค์—ฌ๋‹ค๋ณด์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์€ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ ๊ฐ™์€ ๋น„์ข…๊ฒฐ ํ”„๋กœ๊ทธ๋žจ์— ๋Œ€ํ•ด ์ถ”๋ก ํ•ด์•ผ ํ•  ๋•Œ๋งˆ๋‹ค ๋“ฑ์žฅํ•œ๋‹ค. ๋ฌผ๋ก  ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์€ ํ”„๋กœ๊ทธ๋žจ์ด ๋ฌด์—‡์ธ์ง€๋งŒ ์„œ์ˆ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๊ทธ๋žจ์ด ์–ผ๋งˆ๋‚˜ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š”์ง€ ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์–ผ๋งˆ๋‚˜ ๋จน๋Š”์ง€๋Š” ๋‹ตํ•˜์ง€ ๋ชปํ•œ๋‹ค. ์ด๊ฒƒ์€ ์ปดํ“จํ„ฐ๊ฐ€ ํ‘œํ˜„์‹์˜<NAME>์„ ์–ด๋–ป๊ฒŒ ๊ณ„์‚ฐํ• ์ง€๋ฅผ ์ง€์‹œํ•˜๋Š” ํ‰๊ฐ€ ์ „๋žต(evaluation strategy)์— ์˜ํ•ด ์ขŒ์šฐ๋œ๋‹ค. ๋ฐ˜๋ฉด ๊ทธ ๊ตฌํ˜„์€ ์˜๋ฏธ๋ก ์„ ๋ฐ˜์˜ํ•ด์•ผ ํ•˜๊ณ , ์˜๋ฏธ๋ก ์€ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์ด ๊ธฐ๊ณ„ ์ƒ์—์„œ ์–ด๋–ป๊ฒŒ ํ‰๊ฐ€๋˜์–ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์–ด๋Š ์ •๋„ ๊ฒฐ์ •ํ•œ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ๋Š” Strict and Non-Strict Semantics์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ์˜๋ฏธ ์—ญ์œผ๋กœ ๋ฌด์—‡์„ ์„ ํƒํ•  ๊ฒƒ์ธ๊ฐ€? ์ด์ œ ๋ชจ๋“  ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์— ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ ํ•ฉํ•œ ์ˆ˜ํ•™์  ๊ฐœ์ฒด๋ฅผ ์ฐพ์•„๋ณด์ž. 10, 2*5, sum [1.. 4]์˜ ๊ฒฝ์šฐ ์ด๋“ค ํ‘œํ˜„์‹์€ ๋ชจ๋‘ ์ •์ˆ˜ 10์„ ์˜๋ฏธํ•œ๋‹ค. ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด Integer ํƒ€์ž…์˜ ๋ชจ๋“  ๊ฐ’ x๋Š” ์ง‘ํ•ฉ ( )์˜ ์›์†Œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. Bool ํƒ€์ž…์˜ ๊ฐ’๋„ ๋น„์Šทํ•˜๋‹ค. f :: Integer -> Integer ๊ฐ™์€ ํ•จ์ˆ˜์— ๋Œ€ํ•ด "ํ•จ์ˆ˜"์˜ ์ˆ˜ํ•™์  ์ •์˜๋ฅผ (์ธ์ž, ๊ฐ’) ์ง๋“ค์˜ ์ง‘ํ•ฉ ์ฆ‰ ๊ทธ๋ž˜ํ”„๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•จ์ˆ˜๋ฅผ ๊ทธ ํ•จ์ˆ˜์˜ ๊ทธ๋ž˜ํ”„๋กœ ํ•ด์„ํ•˜๋Š” ๊ฒƒ์€ ๋„ˆ๋ฌด ์„ฑ๊ธ‰ํ•˜๋‹ค. ์žฌ๊ท€์  ์ •์˜์— ์ž˜ ๋งž์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‹ค์Œ์˜ ์ •์˜๋ฅผ ๋ณด์ž. shaves :: Integer -> Integer -> Bool 1 `shaves` 1 = True 2 `shaves` 2 = False 0 `shaves` x = not (x `shaves` x) _ `shaves` _ = False 0, 1, 2๋ฅผ ๊ธด ์ˆ˜์—ผ์ด ๋‚œ ๋‚จ์„ฑ์œผ๋กœ ๊ฐ„์ฃผํ•˜๊ณ  ๋ˆ„๊ฐ€ ๋ˆ„๊ตฌ๋ฅผ ๋ฉด๋„ํ• ๊นŒ ์งˆ๋ฌธํ•ด ๋ณด์ž. ์‚ฌ๋žŒ 1์€ ์Šค์Šค๋กœ๋ฅผ ๋ฉด๋„ํ•˜๊ณ , ์„ธ ๋ฒˆ์งธ ์‹ 0 `shaves` 2 == True ๋•Œ๋ฌธ์— ์‚ฌ๋žŒ 2๋Š” ์ด๋ฐœ์‚ฌ 0์ด ๋ฉด๋„ํ•ด ์ค€๋‹ค. ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด ์„ธ ๋ฒˆ์งธ ์ค„์˜ ๋œป์€ ์Šค์Šค๋กœ ๋ฉด๋„ํ•˜์ง€ ์•Š๋Š” ์‚ฌ๋žŒ๋“ค์€ ์ด๋ฐœ์‚ฌ 0์ด ๋ฉด๋„ํ•ด ์ค€๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด๋ฐœ์‚ฌ 0์€? 0 `shaves` 0์ด ์ฐธ์ผ๊นŒ ๊ฑฐ์ง“์ผ๊นŒ? ์ฐธ์ด๋ผ๋ฉด ์„ธ ๋ฒˆ์งธ ๋“ฑ์‹์€ ๊ทธ๊ฒƒ์ด ๊ฑฐ์ง“์ด๋ผ ๋งํ•œ๋‹ค. ๊ฑฐ์ง“์ด๋ผ๋ฉด ์ด ๋“ฑ์‹์€ ๊ทธ๊ฒƒ์ด ์ฐธ์ด๋ผ ๋งํ•œ๋‹ค. ์šฐ๋ฆฌ๋Š” 0 `shaves` 0์— ๋‹จ์ˆœํžˆ True๋‚˜ Fales๋ฅผ ๋ถ€์—ฌํ•  ์ˆ˜ ์—†๋‹ค. ์ด ํ•จ์ˆ˜์— ๋Œ€ํ•œ ํ•ด์„์œผ๋กœ ์‚ฌ์šฉํ•  ๊ทธ๋ž˜ํ”„๋Š” ๋นˆ ๋ถ€๋ถ„์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค. ์šฐ๋ฆฌ์˜ ์˜๋ฏธ๋ก  ๊ฐœ์ฒด๋Š” ๋ถ€๋ถ„ ํ•จ์ˆ˜, ์ฆ‰ ํŠน์ • ์ธ์ž์— ๋Œ€ํ•ด ์ •์˜๋˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜๋ฅผ ํฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์ด ์œ ๋ช…ํ•œ ์˜ˆ์‹œ๋Š” ์ง‘ํ•ฉ ์ด๋ก ์˜ ๊ทผ๋ณธ์„ ๊ฑด๋“œ๋ฆฌ๋Š” ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๋กœ ๋– ์˜ฌ๋ž๋‹ค. ์ด๋Š” ๋น„๋‹จ ์ •์ (impredicative) ์ •์˜์˜ ํ•œ ์˜ˆ์‹œ๋‹ค. ๋น„๋‹จ ์ •์  ์ •์˜๋Š” ๊ทธ ์ž์‹ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœํ™˜ ๋…ผ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค. ํ•˜์ง€๋งŒ ์žฌ๊ท€์  ์ •์˜์—์„œ๋Š” ์ด๋Ÿฐ ์ˆœํ™˜์ด ๋ฌด์Šจ ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  ํ•˜๋‚˜์˜ ํŠน์„ฑ์ผ ๋ฟ์ด๋‹ค. ๋ฐ”ํ…€๊ณผ ๋ถ€๋ถ„ ํ•จ์ˆ˜ โŠฅ ๋ฐ”ํ…€ ๋ถ€๋ถ„ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ํŠน๋ณ„ํ•œ ๊ฐ’ โŠฅ๋ฅผ ๋„์ž…ํ•˜๊ฒ ๋‹ค. ์ด๊ฒƒ์˜ ์ด๋ฆ„์€ ๋ฐ”ํ…€(bottom)์ด๊ณ  ํƒ€์žํ•  ๋•Œ๋Š” ๋Œ€๊ฐœ _|_๋กœ ์“ด๋‹ค. โŠฅ๋Š” ์•„์˜ˆ "์ •์˜๋˜์ง€ ์•Š์€" ๊ฐ’ ๋˜๋Š” ํ•จ์ˆ˜๋‹ค. Integer๋‚˜ () ๊ฐ™์€ ๋ชจ๋“  ๊ธฐ๋ณธ์ ์ธ ์ž๋ฃŒํ˜•์€ โŠฅ๋ฅผ ์ผ๋ฐ˜์ ์ธ ์›์†Œ๋กœ์„œ ํฌํ•จํ•œ๋‹ค. ๋”ฐ๋ผ์„œ Integer ํƒ€์ž…์œผ๋กœ ๊ฐ€๋Šฅํ•œ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. , , 1 โˆ’ , 2 โˆ’ , 3 โˆ’ , ๊ฐ’๋“ค์˜ ์ง‘ํ•ฉ์— โŠฅ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ lifting์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค. โŠฅ ์ฒ˜๋Ÿผ ์•„๋ž˜ ์ฒจ์ž๋กœ ํ‘œ๊ธฐํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์ด๊ฒƒ์ด ์ˆ˜ํ•™์  ์ง‘ํ•ฉ "lifted integers"์˜ ์˜ฌ๋ฐ”๋ฅธ ํ‘œ๊ธฐ์ง€๋งŒ, ์šฐ๋ฆฌ๋Š” "Integer ํƒ€์ž…์˜ ๊ฐ’๋“ค"์— ๋Œ€ํ•ด ๋งํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•œ๋‹ค. โŠฅ ๋Š” "์ง„์งœ" ์ •์ˆ˜๋“ค์„ ์˜๋ฏธํ•˜์ง€๋งŒ ํ•˜์Šค์ผˆ์—์„œ "์ •์ˆ˜๋“ค"์€ Integer์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‹ค๋ฅธ ์˜ˆ์‹œ๋กœ ์›์†Œ๊ฐ€ ์˜ค์ง ํ•˜๋‚˜์ธ () ํƒ€์ž…์€ ์‚ฌ์‹ค ์›์†Œ๊ฐ€ ๋‘ ๊ฐœ๋‹ค. , ( ) ๋‹น์žฅ์€ Integer๋ฅผ ์ด์šฉํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ณ ์ˆ˜ํ•˜๊ฒ ๋‹ค. ์ž„์˜์˜ ๋Œ€์ˆ˜์  ์ž๋ฃŒํ˜•์€ Algebraic Data Types ์ ˆ์—์„œ ๋‹ค๋ฃฌ๋‹ค. โŠฅ๋ฅผ ์–ด๋–ป๊ฒŒ ํฌํ•จํ•˜๋Š๋ƒ์— ๋Œ€ํ•ด ์—„๋ฐ€ํ•œ ์–ธ์–ด์™€ ์—„๋ฐ€ํ•˜์ง€ ์•Š์€ ์–ธ์–ด์˜ ๋ฐฉ์‹์ด ๊ฐˆ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ํ‘œํ˜„์‹ undefined์€ โŠฅ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. undefined ๋•์— ์‹ค์ œ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์˜ ์˜๋ฏธ๋ก ์  ์„ฑ์งˆ์„ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋‹ค. undefined์˜ ๋‹คํ˜• ํƒ€์ž…์€ forall a. a๋กœ์„œ undefined :: Integer, undefined :: (), undefined :: Integer -> Integer ๋“ฑ์œผ๋กœ ํŠนํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์Šค ์ผˆ Prelude์— undefined๋Š” ์ด๋ ‡๊ฒŒ ์ •์˜๋˜์–ด ์žˆ๋‹ค. undefined = error "Prelude.undefined" ์ฒจ์–ธํ•˜์ž๋ฉด Curry-Howard ๋™ํ˜•์— ๋”ฐ๋ผ ๋‹คํ˜• ํƒ€์ž… forall a. a์˜ ๋ชจ๋“  ๊ฐ’์€ ๋ฐ˜๋“œ์‹œ โŠฅ๋ฅผ ํ‘œ๊ธฐ(denote) ํ•ด์•ผ ํ•œ๋‹ค. ๋ถ€๋ถ„ ํ•จ์ˆ˜์™€ ์˜๋ฏธ๋ก ์  ๊ทผ์‚ฌ ์ˆœ์„œ Partial Functions and the Semantic Approximation Order ์ด์ œ โŠฅ(๋ฐ”ํ…€ ํƒ€์ž…)์„ ์ด์šฉํ•ด ๋ถ€๋ถ„ ํ•จ์ˆ˜๋ฅผ ํ‘œ๊ธฐํ•  ์ˆ˜ ์žˆ๋‹ค. ( ) { if is โˆ’ if is โŠฅ else ์—ฌ๊ธฐ์„œ f(n)์€ n=0๊ณผ n=1์— ๋Œ€ํ•ด์„œ๋Š” ์ž˜ ์ •์˜๋œ ๊ฐ’์„ ์ œ๊ณตํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๋ชจ๋“  n์— ๋Œ€ํ•ด์„œ๋Š” โŠฅ์„ ๋‚ด๋ฑ‰๋Š”๋‹ค. โŠฅ ํƒ€์ž…์€ ๋ณดํŽธ์ (universal)์ธ๋ฐ, โŠฅ ํƒ€์ž…์˜ ๊ฐ’์€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•จ์ˆ˜ โŠฅ :: Integer -> Integer๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฃผ์–ด์ง„๋‹ค. โŠฅ(n) = โŠฅ for all n ์šฐ๋ณ€์˜ โŠฅ๋Š” Integer ํƒ€์ž…์˜ ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค. ๊ณต์‹ํ™”ํ•˜๋ฉด, ๋ถ€๋ถ„ ํ•จ์ˆ˜, ๊ฐ€๋ น Integer -> Integer ํƒ€์ž…์˜ ํ•จ์ˆ˜๋Š” lifted integers โŠฅ { , , 1 ยฑ, 3 โ€ฆ } ์—์„œ lifted integers๋กœ ๊ฐ€๋Š” ์‚ฌ์ƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฑธ๋กœ๋Š” ์ถฉ๋ถ„ํ•˜์ง€ ์•Š๋‹ค. โŠฅ์˜ ํŠน๋ณ„ํ•œ ์—ญํ• ์„ ๋ฐ˜์˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ์˜ ์ •์˜๋Š” ( ) { if is โŠฅ else ์ง๊ด€์ ์ด์ง€ ์•Š๊ณ  ์‹ค์ œ๋กœ๋„ ํ‹€๋ ธ๋‹ค. ์™œ g(โŠฅ)๋Š” ์ •์˜๋œ ๊ฐ’์„ ๋Œ๋ ค์ฃผ๋Š”๋ฐ g(1)์€ ์ •์˜๋˜์ง€ ์•Š๋Š”๊ฐ€? ๋ชจ๋“  ๋ถ€๋ถ„ ํ•จ์ˆ˜ g๋Š” ๋” ์ž˜ ์ •์˜๋œ ์ธ์ž์— ๋Œ€ํ•ด ๋” ์ž˜ ์ •์˜๋œ ๋‹ต์„ ๋Œ๋ ค์ค˜์•ผ ํ•œ๋‹ค. ๊ณต์‹ํ™”ํ•˜๋ฉด ๋ชจ๋“  ๊ตฌ์ฒด์ ์ธ ์ˆ˜๋Š” โŠฅ๋ณด๋‹ค ๋” ์ž˜ ์ •์˜๋˜์—ˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. โŠ, โŠฅ 2 , ์—ฌ๊ธฐ์„œ โŠ๋Š” b๊ฐ€ a๋ณด๋‹ค ๋” ์ž˜ ์ •์˜๋˜์—ˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ๋น„์Šทํ•˜๊ฒŒ โŠ‘๋Š” b๊ฐ€ a๋ณด๋‹ค ๋” ์ž˜ ์ •์˜๋˜์—ˆ๊ฑฐ๋‚˜ ์ •์˜๋œ ์ •๋„(definedness)๊ฐ€ ๊ฐ™๋‹ค๋Š” ๋œป์ด๋‹ค.๋ฅผ semantic approximation order๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋Š”๋ฐ ์ •์˜๋œ ๊ฐ’์„ ๋œ ์ •์˜๋œ ๊ฐ’์œผ๋กœ ๊ทผ์‚ฌํ•˜์—ฌ "๋” ์ž˜ ์ •์˜๋จ"์„ "๋” ์ž˜ ๊ทผ์‚ฌ ๋จ"์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ฌผ๋ก  โŠฅ๋Š” ์ž๋ฃŒํ˜•์˜ ์ตœ์†Œ ์›์†Œ๊ฐ€ ๋˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  x์— ๋Œ€ํ•ด โŠ ์ด ์„ฑ๋ฆฝํ•œ๋‹ค. (x๊ฐ€ โŠฅ ์ž์ฒด์ธ ๊ฒฝ์šฐ๋Š” ์ œ์™ธ) x โŠฅ โŠ ์–ด๋–ค ์ˆ˜๋„ ๋‹ค๋ฅธ ์ˆ˜๋ณด๋‹ค ๋” ์ž˜ ์ •์˜๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜ํ•™์  ๊ด€๊ณ„ ์€ ์ž„์˜์˜ ๋‘ ์ˆ˜์— ๋Œ€ํ•ด ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค. โŠ๋Š” ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค. โŠ ์™€ โŠ ๋ชจ๋‘ ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋Š” ์ž„์˜์˜ ๋‘ ์ˆ˜๋ฅผ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š” ์ผ๋ฐ˜์ ์ธ ์ˆœ์„œ ์ˆ ์–ด์‹ ์™€ ๋Œ€์กฐ๋œ๋‹ค. ์ด๊ฒƒ์„ ๊ธฐ์–ตํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด๋ ‡๋‹ค. "1๊ณผ 2๋Š” ๋‹ด๊ณ  ์žˆ๋Š” ์ •๋ณด์˜ ๋‚ด์šฉ์€ ๋‹ค๋ฅด์ง€๋งŒ ์ •๋ณด์˜ ์งˆ์€ ๊ฐ™๋‹ค." ๋˜ ๋‹ค๋ฅธ ๊ธฐํ˜ธ๋ฅผ ์“ฐ๋Š” ์ด์œ ๊ธฐ๋„ ํ•˜๋‹ค. โŠ‘ ์™€ โŠ‘ ๋ชจ๋‘ ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค. โŠ‘๋Š” ์„ฑ๋ฆฝํ•œ๋‹ค.๋ฅผ ๋ถ€๋ถ„ ์ˆœ์„œ, Integer ํƒ€์ž…์˜ ๊ฐ’๋“ค์„ ๋ถ€๋ถ„ ์ˆœ์„œ ์ง‘ํ•ฉ(partially ordered set ; poset)์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค. ๋ถ€๋ถ„ ์ˆœ์„œ๋Š” ์„ธ ๋ฒ•์น™์„ ๋งŒ์กฑํ•œ๋‹ค. Reflexivity. ๋ชจ๋“  ๊ฒƒ์€ ์ž์‹ ๊ณผ ๊ฐ™์€ ์ •๋„๋กœ ์ •์˜๋œ๋‹ค. โŠ‘ f r l x Transitivity. โŠ‘์ด๊ณ  โŠ‘ ์ด๋ฉด โŠ‘์ด๋‹ค. Antisymmetry. โŠ‘์ด๊ณ  โŠ‘ ์ด๋ฉด x = y ์ด์–ด์•ผ ํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์ •์ˆ˜๋“ค์€ ์ˆœ์„œ์— ๋Œ€ํ•ด poset์„ ํ˜•์„ฑํ•˜๋Š”๊ฐ€? Integer ํƒ€์ž…์˜ ๊ฐ’๋“ค์— ๋Œ€ํ•œ ์ˆœ์„œ๋ฅผ ๋‹ค์Œ ๊ทธ๋ž˜ํ”„๋กœ ๋ฌ˜์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๋…ธ๋“œ ๊ฐ„ ์—ฐ๊ฒฐ์€ ์œ„์˜ ๊ฒƒ์ด ์•„๋ž˜์˜ ๊ฒƒ๋ณด๋‹ค ๋” ์ž˜ ์ •์˜๋˜์—ˆ์Œ์„ ๋œปํ•œ๋‹ค. โŠฅ๋ฅผ ์ œ์™ธํ•˜๋ฉด ๋ ˆ๋ฒจ์ด ํ•˜๋‚˜๋ฟ์ด๋ฏ€๋กœ Integer๋Š” ํ‰์—ญ(flat domain)์ด๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ โŠฅ์˜ ์ด๋ฆ„๋„ ์„ค๋ช…ํ•œ๋‹ค. โŠฅ๋ฅผ ๋ฐ”ํ…€์ด๋ผ๊ณ  ํ•˜๋Š” ์ด์œ ๋Š” ํ•ญ์ƒ ๋งจ ๋ฐ‘์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‹จ์กฐ์„ฑ(Monotonicity) ๋ถ€๋ถ„ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์šฐ๋ฆฌ์˜ ์ง๊ด€์„ ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณต์‹ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“  ๋ถ€๋ถ„ ํ•จ์ˆ˜ f๋Š” ๋ถ€๋ถ„ ์ˆœ์„œ ์ง‘ํ•ฉ ๊ฐ„์˜ ๋‹จ์กฐ ์‚ฌ์ƒ์ด๋‹ค. ๋” ์ž˜ ์ •์˜๋œ ์ธ์ž๋Š” ๋” ์ž˜ ์ •์˜๋œ ๊ฐ’์„ ๋‚ด๋†“๋Š”๋‹ค. โŠ‘ โ‡’ ( ) f ( ) ํŠนํžˆ h(โŠฅ) = 1์„ ๋งŒ์กฑํ•˜๋Š” ํ•จ์ˆ˜ h๋Š” ์ƒ์ˆ˜๋‹ค. ๋ชจ๋“  n์— ๋Œ€ํ•ด h(n) = 1์ด๋‹ค. ์—ฌ๊ธฐ์„œ โŠ‘ ๊ฐ™์€ ๊ฒƒ์ด ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•  ๊ฒƒ. ํ•˜์Šค์ผˆ์‹์œผ๋กœ ๋งํ•˜์ž๋ฉด ๋‹จ์กฐ์„ฑ์˜ ์˜๋ฏธ๋Š” โŠฅ๋ฅผ ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ๋Š” โŠฅ ์ฆ‰ undefined์— ๋Œ€ํ•ด ํŒจํ„ด ๋งค์นญํ•  ์ˆ˜ ์—†๋‹ค. ๋งŒ์•ฝ ๊ฐ€๋Šฅํ–ˆ๋‹ค๋ฉด ์œ„์˜ g๋ฅผ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์—ˆ์„ ๊ฒƒ์ด๋‹ค. ํ›„์— ๋ณด๊ฒ ์ง€๋งŒ โŠฅ์€ ๋น„์ข…๊ฒฐ ํ”„๋กœ๊ทธ๋žจ๋„ ์˜๋ฏธํ•˜๋ฉฐ, ํ•˜์Šค์ผˆ์—์„œ โŠฅ๋ฅผ ๊ด€์ฐฐํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์€ ์ข…๋ฃŒ ๋ฌธ์ œ(halting problem)์™€ ์—ฐ๊ด€๋œ๋‹ค. ๋” ์ž˜ ์ •์˜๋จ์ด๋ผ๋Š” ๊ฐœ๋…์„ ๋ถ€๋ถ„ ํ•จ์ˆ˜๋กœ ํ™•์žฅํ•˜์—ฌ, ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์ธ์ž์— ๋Œ€ํ•ด ๋‹ค์Œ์„ ๋งŒ์กฑํ•  ๊ฒฝ์šฐ ํ•œ ํ•จ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ณด๋‹ค ๋” ์ž˜ ์ •์˜๋˜์—ˆ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. โŠ‘ if x f ( ) g ( ) ์ด์— ๋”ฐ๋ผ ๋ถ€๋ถ„ ํ•จ์ˆ˜๋“ค์€ undefined ํ•จ์ˆ˜ โŠฅ(x) = โŠฅ๋ฅผ ์ตœ์†Œ ์›์†Œ๋กœ ํ•˜๋Š” poset์„ ํ˜•์„ฑํ•œ๋‹ค. ๊ณ ์ •์  ๋ฐ˜๋ณต์œผ๋กœ์„œ์˜ ์žฌ๊ท€ ์ •์˜ (Recursive Definitions as Fixed Point Iterations) ๊ณ„์Šน(factorial) ํ•จ์ˆ˜์˜ ๊ทผ์‚ฌ ๋ถ€๋ถ„ ํ•จ์ˆ˜๋ฅผ ์„œ์ˆ ํ•  ์ˆ˜๋‹จ์ด ์ƒ๊ฒผ์œผ๋‹ˆ ์ด์ œ ์žฌ๊ท€ ์ •์˜๋ฅผ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ ๋ช…ํ•œ ์˜ˆ์‹œ์ธ ๊ณ„์Šน ํ•จ์ˆ˜ f(n) = n!๋ฅผ ์‚ดํŽด๋ณด์ž. ๊ณ„์Šน์˜ ์žฌ๊ท€ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ( ) if == then else โ‹… ( โˆ’ ) ์ด ์žฌ๊ท€ ์ •์˜๋ฅผ ๊ทธ๋Œ€๋กœ ์ง‘ํ•ฉ ํ˜•ํƒœ๋กœ ํ•ด์„ํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ดค์ง€๋งŒ, ์šฐ๋ฆฌ๋Š” f(n)์„ ๊ณ„์‚ฐํ•˜๋ ค๋ฉด ์šฐ๋ณ€์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ๊ณ„์‚ฐํ•ด์•ผ ํ•จ์„ ์ง๊ด€์ ์œผ๋กœ ์•ˆ๋‹ค. ์ด ๋ฐ˜๋ณต์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณต์‹ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ จ์˜ ํ•จ์ˆ˜ k ๋“ค์„ ๊ณ„์‚ฐํ•˜๋Š”๋ฐ, ๊ฐ k ๋Š” ์šฐ๋ณ€์„ ์ด์ „์˜ k ์— ์ ์šฉํ•œ ๊ฒƒ์ด๋‹ค. ์ฆ‰ 1 ( ) { if is โŠฅ else , f ( ) { if is 1 if is โŠฅ else , f ( ) { if is 1 if is 2 if is โŠฅ else ๋‹ค์Œ์€ ํ™•์‹คํ•˜๋‹ค. = 0 f โŠ‘ 2 โ€ฆ ๋”ฐ๋ผ์„œ ์ด ์ˆ˜์—ด์ด ๊ณ„์Šน ํ•จ์ˆ˜๋กœ ์ˆ˜๋ ดํ•œ๋‹ค๊ณ  ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„์˜ ๋ฐ˜๋ณต์€ ๊ณ ์ •์  ๋ฐ˜๋ณต(fixed point iteration)์ด๋ผ๋Š” ์ž˜ ์•Œ๋ ค์ง„ ์ฒด๊ณ„๋ฅผ ๋”ฐ๋ฅธ๋‹ค. 0 g ( 0 ) g ( ( 0 ) ) g ( ( ( 0 ) ) ) โ€ฆ ์œ„์˜ ๊ฒฝ์šฐ 0 ์€ ํ•จ์ˆ˜๊ณ  g๋Š” functional, ์ฆ‰ ํ•จ์ˆ˜ ๊ฐ„์˜ ์‚ฌ์ƒ์ด๋‹ค. 0 โŠฅ and ( ) n if == then else โˆ— ( โˆ’ ) 0 โŠฅ ์—์„œ ์‹œ์ž‘ํ•˜๋ฉด ์ด ๋ฐ˜๋ณต์€ ๊ณ„์Šน ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์ ์ฐจ ์ž˜ ์ •์˜๋˜๋Š” ๊ทผ์‚ฌ๋ฅผ ๋Œ๋ ค์ค€๋‹ค. โŠ‘ ( ) g ( ( ) ) g ( ( ( ) ) ) โ€ฆ (์ด ์ˆ˜์—ด์ด ์ฆ๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ์ฆ๋ช…: ์ฒซ ๋ฒˆ์งธ ๋ถ€๋“ฑ์‹ โŠ‘ ( ) โŠฅ ์ด ๋‹ค๋ฅธ ๋ชจ๋“  ๊ฒƒ๋ณด๋‹ค ๋œ ์ •์˜๋˜์—ˆ์Œ์—์„œ ๊ธฐ์ธํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ถ€๋“ฑ์‹์€ ์ฒซ ๋ถ€๋“ฑ์‹์˜ ์–‘๋ณ€์— ๋‹จ์กฐ์ธ g๋ฅผ ์ ์šฉํ•ด์„œ ์–ป๋Š”๋‹ค. ์ดํ›„์˜ ๋ถ€๋“ฑ์‹๋“ค๋„ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์–ป๋Š”๋‹ค.) ์ด ๋ฐ˜๋ณต ์ฒด๊ณ„๋ฅผ ํ•˜์Šค ์ผˆ๋กœ ํ‘œํ˜„ํ•ด ๋ณด์ž. functional์€ ํ‰๋ฒ”ํ•œ ๊ณ ์ฐจ ํ•จ์ˆ˜๋‹ค. g :: (Integer -> Integer) -> (Integer -> Integer) g x = \n -> if n == 0 then 1 else n * x (n-1) x0 :: Integer -> Integer x0 = undefined (f0:f1:f2:f3:f4:fs) = iterate g x0 ์ด์ œ f0, f1, ... ์„ ํ‰๊ฐ€ํ•˜์—ฌ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”์ง€ ์•„๋‹Œ์ง€ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. > f3 0 1 > f3 1 1 > f3 2 2 > f3 5 *** Exception: Prelude.undefined > map f3 [0..] [1,1,2, *** Exception: Prelude.undefined > map f4 [0..] [1,1,2,6, *** Exception: Prelude.undefined > map f1 [0..] [1, *** Exception: Prelude.undefined ๋ฌผ๋ก  ์ •๋ง f4๊ฐ€ ๋ชจ๋“  ์ธ์ž์— ๋Œ€ํ•ด ์ •์˜๋˜์ง€ ์•Š๋Š”์ง€ ์ด๋Ÿฐ ์‹์œผ๋กœ๋Š” ํ™•์ธํ•  ์ˆ˜๋Š” ์—†๋‹ค. ์ˆ˜๋ ด์„ฑ ์ˆ˜ํ•™์ž์—๊ฒŒ๋Š” ์ด ์ผ๋ จ์˜ ๊ทผ์‚ฌ๊ฐ€ ์ˆ˜๋ ดํ•˜๋Š”์ง€๋ฅผ ๋Œ€๋‹ตํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์•„์ง ๋‚จ์•„์žˆ๋‹ค. ์ด์™€ ๊ด€๋ จํ•ด poset์ด directed complete partial order (dcpo) ์ผ ํ•„์š”์ถฉ๋ถ„์กฐ๊ฑด์€ ์ฒด์ธ์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š”, ๋ชจ๋“  ๋‹จ์กฐ์ˆ˜์—ด 0 x โŠ‘ ์ด ์ตœ์†Œ ์ƒ๊ณ„(supremum)๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์ด๋‹ค. To the mathematician, the question whether this sequence of approximations converges is still to be answered. For that, we say that a poset is a directed complete partial order (dcpo) iff every monotone sequence {\displaystyle x_{0}\sqsubseteq x_{1}\sqsubseteq \dots } x_{0}\sqsubseteq x_{1}\sqsubseteq \dots (also called chain) has a least upper bound (supremum) $ \sup {{\sqsubseteq }}{x{0}\sqsubseteq x_{1}\sqsubseteq \dots }=x. $ ์˜๋ฏธ๋ก  ๊ทผ์‚ฌ ์ˆœ์„œ(semantic approximation order)์˜ ๊ฒฝ์šฐ ๊ณ„์Šน ํ•จ์ˆ˜๋ฅผ ๊ทผ์‚ฌํ•˜๋Š” ํ•จ์ˆ˜๋“ค์˜ ๋‹จ์กฐ์ˆ˜์—ด์€ ์œ ๊ณ„๋ฅผ ๊ฐ€์ง„๋‹ค๊ณ  ํ™•์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ์˜ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์—์„œ(์ด ๊ธ€์—์„œ)๋Š” ์ตœ์†Œ ์›์†Œ โŠฅ๋ฅผ ํฌํ•จํ•˜๋Š” dcpo๋งŒ์„ ๋ณผ ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ complete partial order(cpo)๋ผ๊ณ  ํ•œ๋‹ค. If that's the case for the semantic approximation order, we clearly can be sure that monotone sequence of functions approximating the factorial function indeed has a limit. For our denotational semantics, we will only meet dcpos which have a least element {\displaystyle \bot } \bot which are called complete partial orders (cpo). Integer๋“ค์€ ํ™•์‹คํžˆ (d) cpo๋ฅผ ํ˜•์„ฑํ•˜๋Š”๋ฐ, ์›์†Œ๊ฐ€ ์ ์–ด๋„ ํ•˜๋‚˜์ธ ๋‹จ์กฐ์ˆ˜์—ด์€ ๋‹ค์Œ์„ ํ˜•์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. The Integers clearly form a (d) cpo, because the monotone sequences consisting of more than one element must be of the form โŠ‘ โŠ‘ โŠฅ n n โ‹ฏ n ์—ฌ๊ธฐ์„œ n์€ ์ผ๋ฐ˜์ ์ธ ์ˆซ์ž๋‹ค. ์ฆ‰ n ์ž์ฒด๊ฐ€ ์ด๋ฏธ ์ตœ์†Œ ์ƒ๊ณ„๋‹ค. where {\displaystyle n} n is an ordinary number. Thus, {\displaystyle n} n is already the least upper bound. Integer -> Integer ํ•จ์ˆ˜๋“ค์˜ ๊ฒฝ์šฐ ์ด ์ฃผ์žฅ์€ ๊ฑฐ์ง“์ธ๋ฐ ๋‹จ์กฐ์ˆ˜์—ด์ด ๋ฌดํ•œํžˆ ๊ธธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ Integer๊ฐ€ (d) cpo์ด๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ๋ชจ๋“  ์  n์— ๋Œ€ํ•ด ์ตœ์†Œ ์ƒ๊ณ„๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•ˆ๋‹ค. For functions Integer -> Integer, this argument fails because monotone sequences may be of infinite length. But because Integer is a (d) cpo, we know that for every point {\displaystyle n} n, there is a least upper bound $ \sup {{\sqsubseteq }}{\bot =f{0}(n)\sqsubseteq f_{1}(n)\sqsubseteq f_{2}(n)\sqsubseteq \dots }=:f(n) $ ์˜๋ฏธ ๊ทผ์‚ฌ ์ˆœ์„œ๊ฐ€ ์ ๋ณ„๋กœ ์ •์˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜ f๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๋˜ supremum์ด๋‹ค. As the semantic approximation order is defined point-wise, the function {\displaystyle f} f is the supremum we looked for. ์ง€๊ธˆ๊นŒ์ง€๊ฐ€ ๊ณ„์Šน ํ•จ์ˆ˜์˜ ๋น„๋‹จ ์ •์  ์ •์˜๋ฅผ ๋” ์ž˜ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ์ •์ด์—ˆ๋‹ค. ์—ฌ์ „ํžˆ f(n)์ด ์‹ค์ œ๋กœ ๋ชจ๋“  n์— ๋Œ€ํ•ด ์ž˜ ์ •์˜๋œ ๊ฐ’์„ ๋Œ๋ ค์คŒ์„ ๋ณด์—ฌ์ค˜์•ผ ํ•˜์ง€๋งŒ ์ด๋Š” ์–ด๋ ต์ง€ ์•Š๊ณ  ๊ทธ ํ˜•ํƒœ๊ฐ€ ๊ธ€๋Ÿฌ๋จน์€ ์ •์˜๋ณด๋‹ค๋Š” ๋‚ซ๋‹ค. These have been the last touches for our aim to transform the impredicative definition of the factorial function into a well defined construction. Of course, it remains to be shown that {\displaystyle f(n)} f(n) actually yields a defined value for every {\displaystyle n} n, but this is not hard and far more reasonable than a completely ill-formed definition. ๋ฐ”ํ…€์€ ๋น„์ข…๊ฒฐ์„ ๋‚ดํฌํ•œ๋‹ค Bottom includes Non-Termination ์žฌ๊ท€์  ์ •์˜์— ๊ด€ํ•ด ์ƒˆ๋กญ๊ฒŒ ์–ป์€ ํ†ต์ฐฐ์„ ๋น„์ข…๊ฒฐ ์˜ˆ์‹œ์— ์ ์šฉํ•ด ๋ณด์ž. It is instructive to try our newly gained insight into recursive definitions on an example that does not terminate: ( ) f ( + ) ๊ทผ์‚ฌ ์ˆ˜์—ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด The approximating sequence reads 0 โŠฅ f = , โŠฅ๋กœ๋งŒ ์ด๋ค„์ง„๋‹ค. ๊ทธ ๊ทน๊ฐ’์˜ ๊ฒฐ๊ณผ๋Š” ๋ถ„๋ช…ํžˆ โŠฅ์ด๋‹ค. ์—ฐ์‚ฐ์˜ ๊ด€์ ์—์„œ ์ด ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋Š” ๊ธฐ๊ณ„๋Š” ๊ธฐ์•ฝ ์—†์ด ๋ฐ˜๋ณต๋  ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” โŠฅ๊ฐ€ ๋น„์ข…๊ฒฐ ํ•จ์ˆ˜๋‚˜ ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ๋ณธ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด ์ข…๋ฃŒ ๋ฌธ์ œ์— ๋Œ€ํ•ด, ํ•˜์Šค์ผˆ์—์„  โŠฅ์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. and consists only of {\displaystyle \bot } \bot . Clearly, the resulting limit is {\displaystyle \bot } \bot again. From an operational point of view, a machine executing this program will loop indefinitely. We thus see that {\displaystyle \bot } \bot may also denote a non-terminating function or value. Hence, given the halting problem, pattern matching on {\displaystyle \bot } \bot in Haskell is impossible. ์ตœ์†Œ ๊ณ ์ •์ ์œผ๋กœ์„œ์˜ ํ•ด์„ ์•ž์—์„œ ๊ทผ์‚ฌ ์ˆ˜์—ด์„ ๋„๋ฆฌ ์•Œ๋ ค์ง„ "๊ณ ์ •์  ๋ฐ˜๋ณต(fixed point iteration)" shceme์˜ ์˜ˆ์‹œ๋ผ๊ณ  ํ–ˆ๋‹ค. ๋ฌผ๋ก  ๊ณ„์Šน ํ•จ์ˆ˜ f์˜ ์ •์˜๋Š” functional g์˜ ๊ณ ์ •์ ์„ ๊ธฐ์ˆ ํ•œ ๊ฒƒ์œผ๋กœ ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. Earlier, we called the approximating sequence an example of the well known "fixed point iteration" scheme. And of course, the definition of the factorial function {\displaystyle f} f can also be thought as the specification of a fixed point of the functional {\displaystyle g} g: = ( ) n if == then else โ‹… ( โˆ’ ) ํ•˜์ง€๋งŒ ๊ณ ์ •์ ์€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ์ˆ˜ ์žˆ๋‹ค. ์œ„ ๋ช…์„ธ๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๋‹ค๋ฅธ f๊ฐ€ ์žˆ๋‹ค. However, there might be multiple fixed points. For instance, there are several {\displaystyle f} f which fulfill the specification = โ†ฆ if == then else ( + ) ์ด ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด f(1)์ด๋‚˜ f(2)์—์„œ ๋ฃจํ”„๋ฅผ ๋ฌดํ•œํžˆ ๋Œ๊ธฐ ๋•Œ๋ฌธ์— f(1)์˜ ๊ฐ’์— ๋Œ€ํ•ด์„œ ์–ด๋–ค ์œ ์˜๋ฏธํ•œ ์ •๋ณด๋ฅผ ์‚ฐ์ถœํ•˜์ง€ ๋ชปํ•œ๋‹ค. ์ด๋Š” ์ตœ์†Œ ์ •์˜(least defined) ๊ณ ์ •์ ์„ ์˜๋ฏธ๋ก  ๊ฐœ์ฒด๋กœ ์„ ํƒํ•˜๋Š” ๊ณ ์ „์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ์€ Of course, when executing such a program, the machine will loop forever on {\displaystyle f(1)} f(1) or {\displaystyle f(2)} f(2) and thus not produce any valuable information about the value of {\displaystyle f(1)} f(1). This corresponds to choosing the least defined fixed point as semantic object {\displaystyle f} f and this is indeed a canonical choice. Thus, we say that = ( ) g์˜ ์ตœ์†Œ ๊ณ ์ •์  f๋ฅผ ์ •์˜ํ•œ๋‹ค๊ณ  ๋งํ•œ๋‹ค. ์ตœ์†Œ(least)๋Š” ์šฐ๋ฆฌ์˜ ์˜๋ฏธ๋ก  ๊ทผ์‚ฌ ์ •๋ ฌ(semantic approximation order) ๊ณผ ์—ฐ๊ด€๋œ๋‹ค. ๊ฐ€ ์—ฐ์†("chain continuous"๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค)์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์šฐ๋ฆฌ์˜ iterative construction์— ์˜ํ•ด ์ตœ์†Œ ๊ณ ์ •์ ์˜ ์กด์žฌ๊ฐ€ ๋ณด์žฅ๋œ๋‹ค. ์ด๋Š” ๋‹จ์ˆœํžˆ ๊ฐ€ ๋‹จ์กฐ์ˆ˜์—ด์˜ suprema๋ฅผ ์ค€์ˆ˜ํ•จ์„ ๋œปํ•œ๋‹ค. $ \sup {{\sqsubseteq }}{g(x{0})\sqsubseteq g(x_{1})\sqsubseteq \dots }=g\left(\sup {{\sqsubseteq }}{x{0}\sqsubseteq x_{1}\sqsubseteq \dots }\right) $ ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ์— ์˜ํ•ด $ f=\sup {{\sqsubseteq }}{x{0}\sqsubseteq g(x_{0})\sqsubseteq g(g(x_{0}))\sqsubseteq \dots } $ ์ด๊ฒƒ์„ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. $ {\begin{array}{lcl}g(f)&=&g\left(\sup {{\sqsubseteq }}{x{0}\sqsubseteq g(x_{0})\sqsubseteq g(g(x_{0}))\sqsubseteq \dots }\right)\&=&\sup {{\sqsubseteq }}{g(x{0})\sqsubseteq g(g(x_{0}))\sqsubseteq \dots }\&=&\sup {{\sqsubseteq }}{x{0}\sqsubseteq g(x_{0})\sqsubseteq g(g(x_{0}))\sqsubseteq \dots }\&=&f\end{array}} $ ๊ทธ๋ฆฌ๊ณ  iteration limit๋Š”์˜ ๊ณ ์ •์ ์ด๊ธฐ๋„ ํ•˜๋‹ค. ๊ณ ์ •์  ๋ฐ˜๋ณต์ด ์ตœ์†Œ ๊ณ ์ •์ ์„ ๋‚ด๋†“๋Š”๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•ด ๋ด๋„ ์ข‹๋‹ค. ์—ฐ์Šต๋ฌธ์ œ 0 โŠฅ ์—์„œ ๊ณ ์ •์  ๋ฐ˜๋ณต์„ ์‹œ์ž‘ํ•ด ์–ป์€ ๊ณ ์ •์ ์ด ์ตœ์†Œ ๊ณ ์ •์ , ์ฆ‰ ๋‹ค๋ฅธ ๋ชจ๋“  ๊ณ ์ •์ ๋ณด๋‹ค ์ž‘๋‹ค๋Š” ๊ฒƒ์„ ์ฆ๋ช…ํ•˜๋ผ. (ํžŒํŠธ: ์€ ์šฐ๋ฆฌ์˜ cpo์˜ ์ตœ์†Œ ์›์†Œ๊ณ ๋Š” ๋‹จ์กฐ๋‹ค) ๊ทธ๊ฑด ๊ทธ๋ ‡๊ณ  ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๊ฐ€ ์—ฐ์†์ธ์ง€๋Š” ์–ด๋–ป๊ฒŒ ์•Œ์•„๋‚ผ๊นŒ? ๋‹จ์กฐ์„ฑ์ฒ˜๋Ÿผ ์ด๊ฒƒ๋„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์— ์˜ํ•ด ๊ฐ•์ œ๋˜์–ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ ์„ฑ์งˆ๋“ค์€ ๋งˆ์Œ๋จน๊ธฐ์— ๋”ฐ๋ผ ๊ฐ•์ œํ•  ์ˆ˜๋„ ๊นฐ ์ˆ˜๋„ ์žˆ์–ด์„œ ์ด ์งˆ๋ฌธ์€ ์กฐ๊ธˆ ํ—›๋˜๊ฒŒ ๋Š๊ปด์ง„๋‹ค. ํ•˜์ง€๋งŒ ์ง๊ด€์ ์œผ๋กœ ๋ณด์ž๋ฉด ๋‹จ์กฐ์„ฑ์€์— ํŒจํ„ด ๋งค์นญ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ์œผ๋กœ์จ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์†์„ฑ์˜ ๊ฒฝ์šฐ๋ฅผ ๋ณด๋ฉด, ์ž„์˜ ํƒ€์ž… a์— ๋Œ€ํ•ด ๋ชจ๋“  ํ•จ์ˆ˜ a -> Integer๋Š” ์ž๋™์œผ๋กœ ์—ฐ์†์ด๋‹ค. Integer๋“ค์˜ ๋‹จ์กฐ์ˆ˜์—ด์˜ ๊ธธ์ด๋Š” ์œ ํ•œํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. a ํƒ€์ž…์˜ ๊ฐ’๋“ค์˜ ๋ฌดํ•œ ์—ฐ์‡„๋Š” Integer๋“ค์˜ ์œ ํ•œ ์—ฐ์‡„์— ์‚ฌ์ƒ๋˜๊ณ  suprema๋ฅผ ์ค€์ˆ˜ํ•ด์„œ(?) ๋‹จ์กฐ์„ฑ์œผ๋กœ ๊ท€๊ฒฐ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํŠน์ˆ˜ ๊ฒฝ์šฐ Integer -> Integer์˜ ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ์—ฐ์†์ด์–ด์•ผ ํ•œ๋‹ค. g :: (Integer -> Integer) -> (Integer -> Integer) ๊ฐ™์€ functional์˜ ๊ฒฝ์šฐ ์—ฐ์†์„ฑ์€ ์ปค๋ง์— ์˜ํ•ด ์„ฑ๋ฆฝ๋˜๋Š”๋ฐ, ํƒ€์ž…์ด :: ((Integer -> Integer), Integer) -> Integer์™€ ๋™ํ˜•์ด๊ณ  a=((Integer -> Integer), Integer)๋ฅผ ์ทจํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. -> ๋จผ ๋ง์ž„??? ํ•˜์Šค์ผˆ์—์„œ ๊ณ„์Šน ํ•จ์ˆ˜์˜ fixed interpretation์€ ๋‹ค์Œ ์ฝ”๋“œ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. factorial = fix g ์—ฌ๊ธฐ์—๋Š” ๊ณ ์ •์  ์ปด๋น„๋„ค์ดํ„ฐ์˜ ๋„์›€์ด ํ•„์š”ํ•˜๋‹ค. fix :: (a -> a) -> a fix๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. fix f = let x = f x in x ๋จธ๋ฆฌ๊ฐ€ ์ข€ ๊ผฌ์ธ๋‹ค. factorial์„ ์ „๊ฐœํ•œ ๊ฒฐ๊ณผ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์• ์ดˆ์— ํ•˜์Šค์ผˆ์—์„œ factorial ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ ๊ฒƒ๊ณผ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค. ๋ฌผ๋ก  ์šฐ๋ฆฌ๊ฐ€ ์ด ์ ˆ์—์„œ ์Œ“์•„ ์˜ฌ๋ฆฐ ๋ชจ๋“  ๊ฒƒ์€ ์‹ค์ œ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•  ๋•Œ ์ „ํ˜€ ๋“ฑ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๊ฒƒ์€ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์˜ ์ˆ˜ํ•™์  ํ•ด์„์„ ํ™•๊ณ ํ•œ ๊ธฐ๋ฐ˜ ์œ„์— ์„ธ์šฐ๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์ผ ๋ฟ์ด๋‹ค. ๊ทธ๋ž˜๋„ undefined์˜ ๋„์›€์„ ๋ฐ›์•„ ํ•˜์Šค์ผˆ์—์„œ ์ด ์˜๋ฏธ๋ก ๋“ค์„ ํƒํ—˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์€ ๋ฉ‹์ง„ ์ผ์ด๋‹ค. ์—„๊ฒฉ ์˜๋ฏธ๋ก ๊ณผ ๋น„ ์—„๊ฒฉ ์˜๋ฏธ๋ก  ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์˜ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์„ ์ž์„ธํžˆ ์„ค๋ช…ํ•œ ํ›„์— semantic object์— ๋Œ€ํ•œ ์ˆ˜ํ•™์  ํ•จ์ˆ˜ ํ‘œ๊ธฐ ( ) ๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ๊ทธ์™€ ๋™๋“ฑํ•œ ํ•˜์Šค ์ผˆ ํ‘œ๊ธฐ์ธ f n์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. ์—„๊ฒฉํ•œ ํ•จ์ˆ˜ ์ธ์ž๊ฐ€ ํ•˜๋‚˜์ธ ํ•จ์ˆ˜ f๊ฐ€ ์—„๊ฒฉํ•˜๋‹ค๊ณ  ๋ถ€๋ฅผ ํ•„์š”์ถฉ๋ถ„์กฐ๊ฑด์€ f โŠฅ = โŠฅ ๋‹ค์Œ์€ ์—„๊ฒฉํ•œ ํ•จ์ˆ˜์˜ ์˜ˆ์‹œ๋‹ค. id x = x succ x = x + 1 power2 0 = 1 power2 n = 2 * power2 (n-1) ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์— ์˜ˆ์ƒ์„ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒƒ์€ ์—†๋‹ค. ํ•˜์ง€๋งŒ ์ด ํ•จ์ˆ˜๋“ค์ด ์™œ ์—„๊ฒฉํ•˜๋‹ค๋Š” ๊ฑธ๊นŒ? ์ •๋ง๋กœ ์—„๊ฒฉํ•œ์ง€ ์ฆ๋ช…ํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€ ์—ฐ์Šต์ด๋‹ค. id์˜ ๊ฒฝ์šฐ ์ •์˜๋กœ๋ถ€ํ„ฐ ์•Œ ์ˆ˜ ์žˆ๋‹ค. succ์˜ ๊ฒฝ์šฐ โŠฅ + 1๊ฐ€ โŠฅ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. โŠฅ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด โŠฅ + 1 = 2 ๋˜๋Š” ์ข€ ๋” ์ผ๋ฐ˜์ ์œผ๋กœ ์–ด๋–ค ๊ตฌ์ฒด์ ์ธ ์ˆซ์ž k์— ๋Œ€ํ•ด โŠฅ + 1 = k๊ฐ€ ์„ฑ๋ฆฝํ•  ๊ฒƒ์ด๋‹ค. ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ๋‹จ์กฐ์ž„์ด ๊ธฐ์–ต๋‚˜๋Š”๊ฐ€? ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ์ด ์„ฑ๋ฆฝํ•œ๋‹ค. 2 = โŠฅ + 1 โŠ‘ 4 + 1 = 5 ๊ทธ ์ด์œ ๋Š” โŠฅ โŠ‘ 4์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ 2 โŠ‘ 5, 2 = 5, 2 โŠ’ 5 ๋ชจ๋‘ k๊ฐ€ 2์ผ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์ฐธ์ด ์•„๋‹ˆ๋‹ค. ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชจ์ˆœ์„ ์–ป๋Š”๋‹ค. k = โŠฅ + 1 โŠ‘ k + 1 = k + 1. ๋”ฐ๋ผ์„œ ์œ ์ผํ•˜๊ฒŒ ๊ฐ€๋Šฅํ•œ ์„ ํƒ์ง€๋Š” succ โŠฅ = โŠฅ + 1 = โŠฅ ๊ทธ๋Ÿฌ๋ฏ€๋กœ succ์€ ์—„๊ฒฉํ•˜๋‹ค. ์—ฐ์Šต๋ฌธ์ œ power2๊ฐ€ ์—„๊ฒฉํ•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. power2 n์ด n ์ด๋ผ๋Š” "๋ช…๋ฐฑํ•œ" ์‚ฌ์‹ค์— ๊ทผ๊ฑฐํ•ด ์ฆ๋ช…ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๊ฐ€๊ธ‰์ ์ด๋ฉด ํ›„์ž๋ฅผ ๊ณ ์ •์  ๋ฐ˜๋ณต์„ ์ด์šฉํ•ด ์ฆ๋ช…ํ•˜๋ผ. ๋น„ ์—„๊ฒฉ ๋ฐ ์—„๊ฒฉํ•œ ์–ธ์–ด ๋น„ ์—„๊ฒฉ ํ•จ์ˆ˜๋ฅผ ์ฐพ๋‹ค ๋ณด๋ฉด Integer -> Integer ํƒ€์ž…์˜ ๋น„ ์—„๊ฒฉ ํ•จ์ˆ˜์˜ ํ”„๋กœํ† ํƒ€์ž…์€ ์˜ค์ง ํ•˜๋‚˜๋งŒ ์กด์žฌํ•œ๋‹ค. one x = 1 ๋ณ€์ข…์œผ๋กœ ๋ชจ๋“  ๊ตฌ์ฒด์ ์ธ ์ˆซ์ž k์— ๋Œ€ํ•ด constk x = k์ด ์žˆ๋‹ค. ์™œ ์ด๊ฒƒ๋“ค์ด ์œ ์ผํ•œ ๊ฒฝ์šฐ์ผ๊นŒ? one n์€ one โŠฅ๋ณด๋‹ค ๋œ ์ •์˜๋  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜๋ผ. Integer๋Š” ํ‰์—ญ(flat domain)์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋‘˜์€ ๊ฐ™์•„์•ผ๋งŒ ํ•œ๋‹ค. ์™œ one์ด ๋น„ ์—„๊ฒฉํ• ๊นŒ? ๊ทธ ์ด์œ ๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด ํ•˜์Šค ์ผˆ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์จ๋ณด์ž. > one (undefined :: Integer) โŠฅ์ด ์•„๋‹ˆ๋‹ค. one์€ ์ธ์ž๋ฅผ ์™„์ „ํžˆ ๋ฌด์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ์€ ํ•ฉ๋ฆฌ์ ์ด๋‹ค. โŠฅ๋ฅผ "๋น„์ข…๊ฒฐ"์ด๋ผ๋Š” ๋™์ž‘ ์˜๋ฏธ๋ก ์  ๊ฐœ๋…์œผ๋กœ ํ•ด์„ํ•  ๋•Œ one์˜ ๋น„ ์—„๊ฒฉํ•จ์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๊ทธ ์ธ์ž๋ฅผ ๊ฐ•์ œ๋กœ ํ‰๊ฐ€ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๊ณ , ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ธ์ž โŠฅ๋ฅผ ํ‰๊ฐ€ํ•  ๋•Œ ๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ํ”ผํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ฃผ์žฅํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ์— ์•ž์„œ ์ธ์ž๋ฅผ ํ‰๊ฐ€ํ•ด์•ผ ํ•˜๋ฏ€๋กœ one โŠฅ ์—ญ์‹œ โŠฅ์—ฌ์•ผ ํ•œ๋‹ค๊ณ  ์ฃผ์žฅํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ฆ‰ ๊ทธ ์ธ์ž๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด one๋„ ์ข…๋ฃŒํ•˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 3 ์‚ฌ์‹ค ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์œ„ํ•ด ์—ฌ๋Ÿฌ๋ถ„์€ ์ด ์„ค๊ณ„ ๋˜๋Š” ๋‹ค๋ฅธ ์„ค๊ณ„๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ˆ„๊ตฐ๊ฐ€๋Š” ํ•จ์ˆ˜๊ฐ€ ์—„๊ฒฉํ•œ์ง€ ๋น„ ์—„๊ฒฉํ•œ์ง€์— ๋”ฐ๋ผ ์–ธ์–ด๊ฐ€ ์—„๊ฒฉํ•˜๋‹ค ๋˜๋Š” ๋น„ ์—„๊ฒฉํ•˜๋‹ค๊ณ  ๋งํ•œ๋‹ค. ํ•˜์Šค์ผˆ์€ ๋น„ ์—„๊ฒฉํ•จ์„ ์„ ํƒํ•œ๋‹ค. ๋Œ€์กฐ์ ์œผ๋กœ ML๊ณผ Lisp๋Š” ์—„๊ฒฉํ•จ ์˜๋ฏธ๋ก ์„ ์„ ํƒํ•œ๋‹ค. ๋‹ค์ธ์ž ํ•จ์ˆ˜ ์—„๊ฒฉํ•จ์ด๋ผ๋Š” ๊ฐœ๋…์€ ๋ณ€์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ํ•จ์ˆ˜๋กœ ํ™•์žฅ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ธ์ž๊ฐ€ 2๊ฐœ์ธ ํ•จ์ˆ˜ f๊ฐ€ ๋‘ ๋ฒˆ์งธ ์ธ์ž์— ๊ด€ํ•ด ์—„๊ฒฉํ•  ํ•„์š”์ถฉ๋ถ„์กฐ๊ฑด์€ ๋ชจ๋“  x์— ๋Œ€ํ•ด ๋‹ค์Œ์ด ์„ฑ๋ฆฝํ•˜๋Š” ๊ฒƒ์ด๋‹ค. f x โŠฅ = โŠฅ ํ•˜์ง€๋งŒ ์ธ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ๋Š” ์—„๊ฒฉํ•จ ์œ ๋ฌด๊ฐ€ ๋‹ค๋ฅธ ์ธ์ž์˜ ๊ฐ’์— ๋‹ฌ๋ ค์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋” ํ”ํ•˜๋‹ค. ์กฐ๊ฑด์‹์ด ๊ทธ๋Ÿฐ ์˜ˆ์‹œ๋‹ค. cond b x y = if b then x else y ์ด ํ•จ์ˆ˜๊ฐ€ y์— ๊ด€ํ•ด ์—„๊ฒฉํ•œ์ง€๋Š” b๊ฐ€ True ์ธ์ง€ False ์ธ์ง€์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค. cond True x โŠฅ = x cond False x โŠฅ = โŠฅ x์˜ ๊ฒฝ์šฐ๋„ ๋น„์Šทํ•˜๋‹ค. ๋ณด์•„ํ•˜๋‹ˆ x์™€ y๊ฐ€ ๋‘˜ ๋‹ค โŠฅ๋ฉด cond๋Š” ํ™•์‹คํžˆ โŠฅ์ด์ง€๋งŒ, ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ •์˜๋œ๋‹ค๋ฉด cond๊ฐ€ ๋ฐ˜๋“œ์‹œ โŠฅ์ด์ง€๋Š” ์•Š๋‹ค. ์ด๋Ÿฐ ๋™์ž‘์„ joint strictness๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. cond๋Š” then๊ณผ else ๋ถ„๊ธฐ ์ค‘ ํ•˜๋‚˜๋งŒ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ if-then-else ๊ตฌ๋ฌธ์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค. if null xs then 'a' else head xs if n == 0 then 1 else 5 / n ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๊ฒฝ์šฐ else ๋ถ€๋ถ„์€ โŠฅ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋น„ ์—„๊ฒฉ ์–ธ์–ด์—์„œ๋Š” if-then-else ๊ฐ™์€ ์›์‹œ ์ œ์–ด๋ฌธ์„ cond ๊ฐ™์€ ํ•จ์ˆ˜๋กœ ๊ฐ์Œ€ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ์šฐ๋ฆฌ๋งŒ์˜ ์ œ์–ด ์—ฐ์‚ฐ์ž๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—„๊ฒฉํ•œ ์–ธ์–ด์—์„œ๋Š” cond๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋‘ ๋ถ„๊ธฐ๋ฅผ ๋ชจ๋‘ ํ‰๊ฐ€ํ•ด์•ผ ํ•˜๋ฏ€๋กœ cond์˜ ์“ธ๋ชจ๊ฐ€ ์—†์–ด์ง„๋‹ค. ์—ฌ๊ธฐ์„œ ๋น„ ์—„๊ฒฉํ•จ์ด ์—„๊ฒฉํ•จ๋ณด๋‹ค ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์— ๊ด€ํ•ด ๋” ์œ ์—ฐํ•˜๋‹ค๋Š” ์ผ๋ฐ˜ํ™”๋œ ๊ด€์ฐฐ์„ ๋ง›๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด ์ฃผ์ œ์— ๋Œ€ํ•ด์„œ๋Š” ์ง€์—ฐ์„ฑ 4๋ฅผ ๋ณผ ๊ฒƒ. ๋Œ€์ˆ˜์  ์ž๋ฃŒํ˜• Intege ๊ฐ„์˜ ๋ถ€๋ถ„ ํ•จ์ˆ˜๋“ค์˜ ๊ฒฝ์šฐ๋ฅผ ๋‹ค๋ฃจ๋ฉฐ ๋™๊ธฐ๋ฅผ ์–ป์—ˆ์œผ๋‹ˆ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์˜ ๋ฒ”์œ„๋ฅผ ์ž„์˜์˜ ํ•˜์Šค ์ผˆ ๋Œ€์ˆ˜์  ์ž๋ฃŒํ˜•์œผ๋กœ ํ™•๋Œ€ํ•ด ๋ณด์ž. TODO : ๋ฌด์Šจ ๋ง์ด์—ฌ ์ž ์‹œ ๋ช…๋ช…๋ฒ•์„ ์งš๊ณ  ๋„˜์–ด๊ฐ€์ž๋ฉด ํŠน์ • ํƒ€์ž…์— ๋Œ€ํ•œ semantic object๋“ค์˜ ๋ชจ์Œ์„ domain์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ด ์šฉ์–ด๋Š” ํŠน์ •ํ•œ ์ •์˜๋ผ๊ธฐ๋ณด๋‹จ ์ผ๋ฐ˜ํ™”๋œ ์ด๋ฆ„์ด๋ฉฐ ์šฐ๋ฆฌ๋Š” ์ด domain์ด cpos(complete partial orders), ์ฆ‰ ๊ฐ’๋“ค์˜ ์ง‘ํ•ฉ๊ณผ fixed point iteration์„ ์œ„ํ•œ ์กฐ๊ฑด๋“ค์„ ๋งŒ์กฑํ•˜๋„๋ก ๋” ์ž˜ ์ •์˜๋œ more defined ๊ด€๊ณ„๋ฅผ ํ•ฉํ•œ ๊ฒƒ์ด๋ผ๊ณ  ๋ณธ๋‹ค. ๋ณดํ†ต์€ cpos์— ์ถ”๊ฐ€ ์กฐ๊ฑด๋“ค์„ ๋‹ฌ์•„์„œ ์ •์˜์—ญ ์•ˆ์˜ ๊ฐ’๋“ค์ด ์ปดํ“จํ„ฐ ์ƒ์—์„œ ์œ ํ•œํ•œ ์ˆ˜๋‹จ์œผ๋กœ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ  ์…€ ์ˆ˜ ์—†๋Š” ๋ฌดํ•œ ์ง‘ํ•ฉ๋“ค์„ ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š์•„๋„ ๋˜๋„๋ก ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ ์ผ๋ฐ˜์ ์ธ domain theoretic ์ •๋ฆฌ๋“ค์„ ์ฆ๋ช…ํ•˜์ง€๋Š” ์•Š์„ ๊ฒƒ์ด๋ฏ€๋กœ ๊ทธ๋Ÿฌํ•œ ์กฐ๊ฑด๋“ค์ด ์ƒ์„ฑ์— ์˜ํ•ด ์„ฑ๋ฆฝํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. (?) A word about nomenclature: the collection of semantic objects for a particular type is usually called a domain. This term is more a generic name than a particular definition and we decide that our domains are cpos (complete partial orders), that is sets of values together with a relation more defined that obeys some conditions to allow fixed point iteration. Usually, one adds additional conditions to the cpos that ensure that the values of our domains can be represented in some finite way on a computer and thereby avoiding to ponder the twisted ways of uncountable infinite sets. But as we are not going to prove general domain theoretic theorems, the conditions will just happen to hold by construction. ์ƒ์„ฑ์ž ๋‹ค์Œ ํƒ€์ž…๋“ค์„ ์˜ˆ๋กœ ๋“ค์–ด๋ณด์ž. data Bool = True | False data Maybe a = Just a | Nothing ์—ฌ๊ธฐ์„œ True, False, Nothing์€ ์ธ์ž ์—†๋Š”(nullary) ์ƒ์„ฑ์ž์ธ ๋ฐ˜๋ฉด Just๋Š” ๋‹จํ•ญ ์ƒ์„ฑ์ž๋‹ค. Bool์˜ inhabitant๋“ค์€ ๋‹ค์Œ ๋„๋ฉ”์ธ์„ ํ˜•์„ฑํ•œ๋‹ค. โŠฅ๋Š” ๊ฐ’์˜ ์ง‘ํ•ฉ True์™€ False์— ์ตœ์†Œ ์›์†Œ๋กœ์„œ ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•  ๊ฒƒ. Bool ํƒ€์ž…์€ lift ๋˜์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค. 5 poset ๋„์‹์—์„œ ์ธต์ด ํ•˜๋‚˜๋ฟ์ธ ๋„๋ฉ”์ธ์„ flat domain์ด๋ผ ๋ถ€๋ฅธ๋‹ค. ์šฐ๋ฆฌ๋Š” Integer๊ฐ€ flat domain์ด๋ผ๋Š” ๊ฒƒ๋„ ์•Œ๊ณ  ์žˆ๋‹ค. ๋‹ค๋งŒ โŠฅ ์œ„์˜ ์ธต์— ๋ฌดํ•œํžˆ ๋งŽ์€ ์›์†Œ๊ฐ€ ์žˆ์„ ๋ฟ์ด๋‹ค. Maybe Bool์˜ inhabitant๋กœ๋Š” ๋‹ค์Œ ์›์†Œ๋“ค์ด ์žˆ๋‹ค. โŠฅ, Nothing, Just โŠฅ, Just True, Just False ์ผ๋ฐ˜์ ์ธ ๊ทœ์น™์€ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฐ’์„ ๋‹จํ•ญ(์ดํ•ญ, ์‚ผ ํ•ญ, ...) ์ƒ์„ฑ์ž๋“ค์— ๋„ฃ์œผ๋ฉด์„œ โŠฅ๋ฅผ ์žŠ์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค. ๋ถ€๋ถ„ ์ˆœ์„œ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์ƒ์„ฑ์ž๊ฐ€ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค์ฒ˜๋Ÿผ ๋‹จ์กฐ์—ฌ์•ผ ํ•œ๋‹ค๋Š” ์กฐ๊ฑด์„ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ถ€๋ถ„ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‹ ์ค‘ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด ์žˆ๋‹ค. ์™œ Just โŠฅ = โŠฅ๊ฐ€ ์•„๋‹๊นŒ? "Just undefined"๋Š” "undefined"๋งŒํผ์ด๋‚˜ ์ •์˜๋˜์ง€ ์•Š๋Š”๋ฐ ๋ง์ด๋‹ค. ๊ทธ ๋‹ต์€ ์–ธ์–ด๊ฐ€ ์—„๊ฒฉํ•œ์ง€ ๋น„ ์—„๊ฒฉํ•œ์ง€์— ๋‹ฌ๋ ค์žˆ๋‹ค. ์—„๊ฒฉํ•œ ์–ธ์–ด์—์„œ๋Š” ๋ชจ๋“  ์ƒ์„ฑ์ž๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ์—„๊ฒฉํ•˜๋ฏ€๋กœ Just โŠฅ = โŠฅ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„ ๋„์‹์€ ์ด๋ ‡๊ฒŒ ํ™˜์›๋œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์—„๊ฒฉํ•œ ์–ธ์–ด์˜ ๋ชจ๋“  ๋„๋ฉ”์ธ์€ flat ํ•˜๋‹ค. ํ•˜์ง€๋งŒ ํ•˜์Šค ์ผˆ ๊ฐ™์€ ๋น„ ์—„๊ฒฉ ์–ธ์–ด์—์„œ๋Š” ์ƒ์„ฑ์ž๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ๋น„ ์—„๊ฒฉ์ด๊ณ  Just โŠฅ๋Š” โŠฅ์™€ ๋‹ค๋ฅธ ์ƒˆ๋กœ์šด ์›์†Œ์ธ๋ฐ, ์ด ๊ฐ’๋“ค์— ๋Œ€ํ•ด ๋‹ค๋ฅด๊ฒŒ ๋ฐ˜์‘ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. f (Just _) = 4 f Nothing = 7 f๊ฐ€ Just ์ƒ์„ฑ์ž์˜ ๋‚ด์šฉ๋ฌผ์„ ๋ฌด์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์— f (Just โŠฅ)๋Š” 4์ด์ง€๋งŒ f โŠฅ๋Š” โŠฅ์ด๋‹ค. (์ง๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•˜๋ฉด f๊ฐ€ โŠฅ๋ฅผ ๋ฐ›์•˜์„ ๋•Œ Just ๋ถ„๊ธฐ๋ฅผ ํƒˆ์ง€ Nothing ๋ธŒ๋žœ์น˜๋ฅผ ํƒˆ์ง€ ๊ฒฐ์ •ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— โŠฅ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.) ํŒจํ„ด ๋งค์นญ ์—„๊ฒฉํ•œ ํ•จ์ˆ˜ ์ ˆ์—์„œ ๋ช‡ ํ•จ์ˆ˜์˜ ์—„๊ฒฉํ•จ์„ ์ฆ๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ์ž…๋ ฅ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์กฐ์‚ฌํ•˜๊ณ  ๋‹จ์กฐ์„ฑ์„ ๊ฐ•์š”ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋Œ€์ˆ˜์  ์ž๋ฃŒํ˜•์˜ ๊ฒฝ์šฐ ํ˜„์‹ค์ ์œผ๋กœ ์—„๊ฒฉํ•จ์˜ ์›์ธ์€ ๋‹จ ํ•˜๋‚˜์ธ๋ฐ ๋ฐ”๋กœ ํŒจํ„ด ๋งค์นญ, ์ฆ‰ case ํ‘œํ˜„์‹์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ์ƒ์„ฑ์ž์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ์ด ๊ทธ ํ•จ์ˆ˜๋ฅผ ์—„๊ฒฉํ•˜๋„๋ก ๊ฐ•์ œํ•œ๋‹ค. ์ฆ‰ ์ƒ์„ฑ์ž์— ๋Œ€ํ•ด โŠฅ๋ฅผ ๋งค์นญํ•˜๋ฉด ํ•ญ์ƒ โŠฅ๊ฐ€ ๋‚˜์˜จ๋‹ค. ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์‚ดํŽด๋ณด์ž. const1 _ = 1 const1' True = 1 const1' False = 1 const1์€ ๋น„ ์—„๊ฒฉํ•˜์ง€๋งŒ const1'์€ ์—„๊ฒฉํ•˜๋‹ค. ๊ฒฐ๊ณผ๊ฐ€ ์ธ์ž์— ์˜์กดํ•˜์ง€ ์•Š์•„๋„ ์ธ์ž๊ฐ€ True ์ธ์ง€ False ์ธ์ง€ ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•จ์ˆ˜ ์ธ์ž์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ์€ ๋‹ค์Œ case ํ‘œํ˜„์‹๊ณผ ๋™๋“ฑํ•˜๋‹ค. const1' x = case x of True -> 1 False -> 1 ์ด case ํ‘œํ˜„ ์‹๋„ x์— ๋Œ€ํ•ด ์—„๊ฒฉํ•จ์„ ๊ฐ•์ œํ•œ๋‹ค. case ํ‘œํ˜„์‹์— ๋Œ€ํ•œ ์ธ์ž๊ฐ€ โŠฅ๋ฅผ ํ‘œ๊ธฐํ•œ๋‹ค๋ฉด case ์ „์ฒด๊ฐ€ โŠฅ๋ฅผ ํ‘œ๊ธฐํ•œ๋‹ค. ํ•˜์ง€๋งŒ case ํ‘œํ˜„์‹์˜ ์ธ์ž๋Š” ์ข€ ๋” ๋ณต์žกํ•  ์ˆ˜ ์žˆ๊ณ  foo k table = case lookup ("Foo." ++ k) table of Nothing -> ... Just x -> ... ์ด๊ฒƒ์ด foo์˜ ์—„๊ฒฉํ•จ์— ๋Œ€ํ•ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋ฅผ ์ถ”์ ํ•˜๊ธฐ๋Š” ์–ด๋ ต๋‹ค. ๋“ฑ์‹ ์Šคํƒ€์ผ์˜ ๋‹ค์ค‘ ํŒจํ„ด ๋งค์นญ์˜ ์˜ˆ์‹œ๋กœ ๋…ผ๋ฆฌ or์ด ์žˆ๋‹ค. or True _ = True or _ True = True or _ _ = False ์ด ๋“ฑ์‹๋“ค์€ ์œ„์—์„œ ์•„๋ž˜๋กœ ๋งค์นญ๋œ๋‹ค. or ๋งค์นญ๋“ค ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋“ฑ์‹์€ ์ฒซ ์ธ์ž๋ฅผ True์— ๋งค์นญํ•˜๋ฏ€๋กœ or์€ ์ฒซ ๋ฒˆ์งธ ์ธ์ž์— ๋Œ€ํ•ด ์—„๊ฒฉํ•˜๋‹ค. ๊ฐ™์€ ๋“ฑ์‹์—์„œ or True x๊ฐ€ x์— ๋น„์—„๊ฒฉํ•œ ๊ฒƒ๋„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž๊ฐ€ False ์ด๋ฉด ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” True์— ๋งค์นญ๋˜๋ฏ€๋กœ or false x๋Š” x์— ๋Œ€ํ•ด ์—„๊ฒฉํ•˜๋‹ค. ์™€์ผ๋“œ์นด๋“œ๋Š” ๋ณดํ†ต์€ ๋น„ ์—„๊ฒฉํ•จ์„ ์•”์‹œํ•˜์ง€๋งŒ ์ƒ์„ฑ์ž์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ์—์„œ๋Š” ๊ทธ ์œ„์น˜์— ๋”ฐ๋ผ ๋น„ ์—„๊ฒฉํ•จ ์—ฌ๋ถ€๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋…ผ๋ฆฌ and์— ๋Œ€ํ•ด ๋™์ผํ•œ ๋…ผ์˜๋ฅผ ํ•ด๋ณด์ž. "๋ฐฐํƒ€์  ๋…ผ๋ฆฌํ•ฉ"(xor)์˜ ํ•œ ์ธ์ž๋ฅผ ์•Œ๋ฉด ๋‹ค๋ฅธ ์ธ์ž์— ๋Œ€ํ•ด ๋น„์—„๊ฒฉํ•  ์ˆ˜ ์žˆ์„๊นŒ? ๋ฌผ๊ฒฐ ๊ธฐํ˜ธ ~๊ฐ€ ๋ถ™๋Š” ๊ฑฐ๋ถ€ํ•  ์ˆ˜ ์—†๋Š” ํŒจํ„ด irrefutable pattern์ด๋ผ๋Š” ๋˜ ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ํŒจํ„ด ๋งค์นญ์ด ์กด์žฌํ•œ๋‹ค. f ~(Just x) = 1 f Nothing = 2 ๊ฑฐ๋ถ€ํ•  ์ˆ˜ ์—†๋Š” ํŒจํ„ด์€ (๊ทธ ์ด๋ฆ„๋Œ€๋กœ) ํ•ญ์ƒ ์„ฑ๊ณตํ•˜๋ฏ€๋กœ f โŠฅ = 1์ด๋‹ค. ํ•˜์ง€๋งŒ f์˜ ์ •์˜๋ฅผ ์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•˜๋ฉด, f ~(Just x) = x + 1 f Nothing = 2 -- this line may as well be left away ๋‹ค์Œ์„ ์–ป๋Š”๋‹ค. f โŠฅ = โŠฅ + 1 = โŠฅ f (Just 1) = 1 + 1 = 2 ์ธ์ž๊ฐ€ ํŒจํ„ด์— ์ผ์น˜ํ•˜๋ฉด x๋Š” ๋Œ€์‘ํ•˜๋Š” ๊ฐ’์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด x ๊ฐ™์€ ๋ชจ๋“  ๋ณ€์ˆ˜๋Š” โŠฅ์— ๋ฌถ์ธ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ let๊ณผ where ๋ฐ”์ธ๋”ฉ ์—ญ์‹œ ๋น„ ์—„๊ฒฉํ•˜๋‹ค. foo key map = let Just x = lookup key map in ... ์œ„ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๋™๋“ฑํ•˜๋‹ค. foo key map = case (lookup key map) of ~(Just x) -> ... ์—ฐ์Šต๋ฌธ์ œ ํ•˜์Šค ์ผˆ ์–ธ์–ด ์ •์˜๋Š” ํŒจํ„ด ๋งค์นญ์˜ ์˜๋ฏธ๋ก ์„ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๋ฉฐ ์—ฌ๋Ÿฌ๋ถ„์ด๋ผ๋ฉด ์ด์ œ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•œ๋ฒˆ ์ฝ์–ด๋ณด์ž. Boolean ์ธ์ž 2๊ฐœ๋ฅผ ๊ฐ€์ง€๋Š” or ํ•จ์ˆ˜์˜ ๋‹ค์Œ ์„ฑ์งˆ์„ ์ƒ๊ฐํ•ด ๋ณด์ž. or โŠฅ โŠฅ = โŠฅ or True โŠฅ = True or โŠฅ True = True or False y = y or x False = x ์ด ํ•จ์ˆ˜๋Š” joint strictness์˜ ๋˜ ๋‹ค๋ฅธ ์˜ˆ์‹œ๋‹ค. ๋‘ ์ธ์ž๊ฐ€ โŠฅ์ผ ๋•Œ๋งŒ(์ตœ์†Œํ•œ ์ธ์ž๋“ค์„ True ๋˜๋Š” โŠฅ๋กœ ํ•œ์ •ํ•˜๋Š” ๊ฒฝ์šฐ) ๊ทธ ๊ฒฐ๊ณผ๋„ โŠฅ์ด๋‹ค. ์ด๋Ÿฐ ํ•จ์ˆ˜๋ฅผ ํ•˜์Šค ์ผˆ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์žฌ๊ท€์  ์ž๋ฃŒํ˜•๊ณผ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ ์žฌ๊ท€ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ๊ธฐ๋ณธ์ ์ธ ๊ฒฝ์šฐ์™€ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค. ์œ ๋‹› ๊ฐ’๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. data List = [] | () : List ๋‹จ์ˆœํ•œ ํƒ€์ž…์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ โŠฅ๋ฅผ ์—ฌ๊ธฐ์ €๊ธฐ ๋ผ์›Œ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์€ ๋†€๋ผ์šธ ๋งŒํผ ๋งŽ๊ณ  ๋”ฐ๋ผ์„œ ์ด์— ๋Œ€์‘ํ•˜๋Š” ๊ทธ๋ž˜ํ”„๋„ ๋ณต์žกํ•˜๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ ์ด ๊ทธ๋ž˜ํ”„์˜ ๋ฐ”ํ…€์„ ๋ณด์—ฌ์ค€๋‹ค. ์ƒ๋žต ๋ถ€ํ˜ธ๋Š” ๊ทธ๋ž˜ํ”„๊ฐ€ ์ด ๋ฐฉํ–ฅ์œผ๋กœ ๊ณ„์† ์ด์–ด์ง„๋‹ค๋Š” ๋œป์ด๋‹ค. ์›์†Œ ๋’ค์˜ ๋นจ๊ฐ„ ์›์€ ์—ฌ๊ธฐ๊ฐ€ ์—ฐ์‡„์˜ ๋์ž„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์›์†Œ๋Š” normal form์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด์ œ๋Š” ๋ฌดํ•œํžˆ ๊ธด ์—ฐ์‡„๋„ ์žˆ๋‹ค. โŠฅโŠ‘ ( ) :โŠฅโŠ‘ ( ) ( ) :โŠฅโŠ‘. ์—ฌ๊ธฐ์—๋Š” ์ˆ˜๋ ด์„ฑ ์ ˆ์—์„œ ์ง€์ ํ–ˆ๋“ฏ์ด ๋ชจ๋“  ๋‹จ์กฐ์ˆ˜์—ด์ด ์ตœ์†Œ ์ƒ๊ณ„๋ฅผ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค๋Š” ์ ์—์„œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ์ด๊ฒƒ์€ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ—ˆ์šฉํ•ด์•ผ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ(์ŠคํŠธ๋ฆผ์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค)๋Š” ์‚ฌ์‹ค ์•„์ฃผ ์œ ์šฉํ•˜๊ณ  ๊ทธ ์šฉ๋ก€๋Š” ์ง€์—ฐ์„ฑ ์žฅ์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃฌ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ๊ฒƒ์˜ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์ด ๋ฌด์—‡์ด๊ณ  ์ด์— ๋Œ€ํ•ด ์ถ”๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค€๋‹ค. ์•ž์œผ๋กœ์˜ ๋…ผ์˜๋Š” ๋ฆฌ์ŠคํŠธ์— ํ•œ์ •๋˜์ง€๋งŒ ํŠธ๋ฆฌ ๊ฐ™์€ ์ž„์˜์˜ ์žฌ๊ท€ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์‰ฝ๊ฒŒ ์ผ๋ฐ˜ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•ž์œผ๋กœ๋Š” ํ‘œ์ค€ ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค. data [a] = [] | a : [a] ์ด๋Š” ํ•˜์Šค์ผˆ์—์„œ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‹ค์ œ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š” ๊ฒƒ๊ณผ ๋ฌธ๋ฒ•์  ์ฐจ์ด๋ฅผ ๋ฉ”๊พธ๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ [Bool]์— ์ƒ์‘ํ•˜๋Š” non-flat domain์„ ๊ทธ๋ฆฐ๋‹ค. [Integer]์˜ ๊ฒฝ์šฐ ๊ทธ ๋ชจ์–‘์ด ์–ด๋–ป๊ฒŒ ๋ฐ”๋€”๊นŒ? ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ์˜ ๊ณ„์‚ฐ์€ ์˜ˆ์ œ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ์ตœ์„ ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ•˜๋‚˜ ํ•„์š”ํ•˜๋‹ค. ones :: [Integer] ones = 1 : ones ์ด ์žฌ๊ท€ ์ •์˜์— ๊ณ ์ •์  ๋ฐ˜๋ณต์„ ์ ์šฉํ•˜๋ฉด ones๊ฐ€ ๋‹ค์Œ์˜ ์ƒํ•œ(supremum)์ž„์ด ๋“œ๋Ÿฌ๋‚œ๋‹ค. โŠ‘ 1 :โŠฅ โŠ‘ : :โŠฅ โŠ‘ : : :โŠฅ โŠ‘. , ์ด๊ฒƒ์€ 1์˜ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋‹ค. take 2 ones๊ฐ€ ๋ฌด์—‡์ด ๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ์ดํ•ดํ•ด ๋ณด์ž. take์˜ ์ •์˜์— ๋”ฐ๋ฅด๋ฉด take 0 _ = [] take n (x:xs) = x : take (n-1) xs take n [] = [] ones์˜ ๊ทผ์‚ฌ ์ˆ˜์—ด์˜ ์›์†Œ๋“ค์— take๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. take 2 โŠฅ ==> โŠฅ take 2 (1:โŠฅ) ==> 1 : take 1 โŠฅ ==> 1 : โŠฅ take 2 (1:1:โŠฅ) ==> 1 : take 1 (1:โŠฅ) ==> 1 : 1 : take 0 โŠฅ ==> 1 : 1 : [] take 2 (1:1:1:โŠฅ) ๋“ฑ์€ take 2 (1:1:โŠฅ) = 1:1:[] ์™€ ๊ฐ™์„ ์ˆ˜๋ฐ–์— ์—†๋Š”๋ฐ, ๊ทธ ์ด์œ ๋Š” 1:1:[]์ด ์™„์ „ํžˆ ์ •์˜๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ž…๋ ฅ ๋ฆฌ์ŠคํŠธ์˜ ์‹œํ€€์Šค์™€ ์ถœ๋ ฅ ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฐ๊ณผ ์‹œํ€€์Šค ์–‘์ชฝ์— ์ƒํ•œ์„ ์ทจํ•˜๋ฉด ๋‹ค์Œ ๊ฒฐ๋ก ์ด ๋‚˜์˜จ๋‹ค. take 2 ones = 1:1:[] ๊ทธ๋Ÿฌ๋ฏ€๋กœ ones์˜ ์ฒ˜์Œ ๋‘ ์ธ์ž๋ฅผ ์ทจํ•˜๋ฉด ์˜ˆ์ƒํ•œ ๋ฐ” ๊ทธ๋Œ€๋กœ ํ–‰๋™ํ•œ๋‹ค. ์œ„ ์˜ˆ์ œ๋ฅผ ์ผ๋ฐ˜ํ™”ํ•˜์ž๋ฉด ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ์ถ”๋ก ์€ ์ง„์งœ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ์˜ ๊ทผ์‚ฌ ์‹œํ€€์Šค์˜ ์ƒํ•œ์„ ์ทจํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜๋ฐ˜ํ•œ๋‹ค. ์•„์ง ํ™•์‹คํžˆ ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ, ํ•ด๋ฒ•์€ ์—ฐ์‡„ ์ „์ฒด ๊ทธ ์ž์ฒด์ธ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ทœ๋ช…ํ•˜๊ณ  ์šฐ๋ฆฌ์˜ domain์— ์ƒˆ๋กœ์šด ์›์†Œ๋กœ์„œ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฐ”๋กœ ์ž์‹ ์˜ ๊ทผ์‚ฌ ์‹œํ€€์Šค๋‹ค. ๋ฌผ๋ก  ones ๊ฐ™์€ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋Š” ์••์ถ•ํ•ด์„œ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ones = 1 : 1 : 1 : 1 : ... ๊ทธ ๋œป์€ ๊ทธ์ € ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ones = (โŠฅ โŠ‘ 1:โŠฅ โŠ‘ 1:1:โŠฅ โŠ‘ ...) ์—ฐ์Šต๋ฌธ์ œ ๋ฌผ๋ก  ones๋ณด๋‹ค ํฅ๋ฏธ๋กœ์šด ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋“ค์ด ์กด์žฌํ•œ๋‹ค. ๋‹ค์Œ์„ ํ•˜์Šค์ผˆ์˜ ์žฌ๊ท€ ์ •์˜๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€? ์ž์—ฐ์ˆ˜๋“ค nats = 1:2:3:4:... ์‚ฌ์ดํด cycle123 = 1:2:3: 1:2:3 : ... ํ”„๋ ๋ฅ˜๋“œ ํ•จ์ˆ˜ repeat์™€ iterate๋ฅผ ํ™œ์šฉํ•ด 1๋ฒˆ ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณธ๋‹ค. ์ด ๊ธ€์˜ ์˜ˆ์ œ๋ฅผ ํ™œ์šฉํ•ด ํ‘œํ˜„์‹ drop 3 nats์ด ํ‘œ๊ธฐํ•˜๋Š” ๊ฐ’์„ ๊ตฌํ•œ๋‹ค. ์—„๊ฒฉํ•œ ํ™˜๊ฒฝ์„ ๊ฐ€์ •ํ•œ๋‹ค. ์ฆ‰ [Integer]์˜ domain์€ flat ํ•˜๋‹ค. ๊ทธ domain์€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”๊ฐ€? ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋‚˜ ones๊ฐ€ ํ‘œ๊ธฐํ•˜๋Š” ๊ฐ’์€ ์–ด๋– ํ•œ๊ฐ€? ์ปดํ“จํ„ฐ๋Š” ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์„๊นŒ? ๋ฌดํ•œํ•œ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ์ง€ ์•Š์„๊นŒ? ๋งž๋Š” ๋ง์ด๋‹ค. ํ•˜์ง€๋งŒ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ์˜ ์œ ํ•œํ•œ ์ผ๋ถ€๋งŒ ๊ณ ๋ คํ•˜๋ฉด ์œ ํ•œํ•œ ์‹œ๊ฐ„ ์•ˆ์— ๋๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋Š” ์ž ์žฌ์ ์œผ๋กœ ๋ฌดํ•œํ•œ ๋ฆฌ์ŠคํŠธ๋ผ๊ณ  ๋ด์•ผ ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋Š” ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ์˜ ํ˜•ํƒœ๋ฅผ ์ทจํ•˜๋Š” ๋ฐ˜๋ฉด ์ตœ์ข… ๊ฐ’์€ ์œ ํ•œํ•˜๋‹ค. ํ”„๋กœ๊ทธ๋žจ์˜ correctness๋ฅผ ์ถ”๋ก ํ•  ๋•Œ ์ค‘๊ฐ„ ๊ณผ์ •์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ •๋ง๋กœ ๋ฌดํ•œํ•˜๋‹ค๊ณ  ์ทจ๊ธ‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์ด ์ฃผ๋Š” ์ด์  ์ค‘ ํ•˜๋‚˜๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋กœ์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ฒดํ—˜ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ์„ ์ฆ๋ช…ํ•˜๋ผ. take 3 (map (+1) nats) = take 3 (tail nats) map (+1) nats์— ํ•ด๋‹นํ•˜๋Š” ๋ฌดํ•œ ์ˆœ์—ด์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•˜๋ผ. 2. ๋ฌผ๋ก  ์ตœ์ข… ๊ฒฐ๊ณผ์— ๋ฌดํ•œํ•œ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š” ์˜ˆ์‹œ๋„ ์žˆ๋‹ค. filter (< 5) nats์ด ํ‘œ๊ธฐํ•˜๋Š” ๋ฐ”๋Š” ๋ฌด์—‡์ธ๊ฐ€? 3. ๋•Œ๋กœ๋Š” ์œ„ ์—ฐ์Šต๋ฌธ์ œ์˜ takeWhile์„ filter๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค. ์™œ ์–ด๋–ค ๊ฒฝ์šฐ์—๋งŒ ๊ฐ€๋Šฅํ•˜๊ณ , ๊ต์ฒดํ•  ๊ฒฝ์šฐ ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚˜๋Š”๊ฐ€? ๋งˆ์ง€๋ง‰์œผ๋กœ ์งš๊ณ  ๋„˜์–ด๊ฐ€์ž๋ฉด ํ•จ์ˆ˜์˜ ์žฌ๊ท€ ์ •์˜์™€ ๋น„์Šทํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ fixed point iteration์„ ํ†ตํ•ด ์žฌ๊ท€์  domain์„ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌดํ•œ ์—ฐ์‡„ ๋ฌธ์ œ๋Š” ์ง์ ‘ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค.<NAME>์  ๊ตฌ์ถ•์— ๊ด€ํ•ด์„œ๋Š” ์™ธ๋ถ€ ๋งํฌ์˜ ๋ฌธํ—Œ์„ ์ฐธ๊ณ ํ•  ๊ฒƒ. ํ•˜์Šค ์ผˆ ํ•œ์ •: strictness annotation๊ณผ newtype ํ•˜์Šค์ผˆ์€ strictness annotation์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ƒ์„ฑ์ž์˜ ๊ธฐ๋ณธ ๋น„ ์—„๊ฒฉ ํ–‰๋™์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ์„ ์–ธ์—์„œ data Maybe' a = Just' !a | Nothing' ์ƒ์„ฑ์ž ์ธ์ž ์•ž์˜ ๋Š๋‚Œํ‘œ!๋Š” ๊ทธ ์ธ์ž์— ๋Œ€ํ•ด ์—„๊ฒฉํ•ด์•ผ ํ•จ์„ ๋ช…์‹œํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๊ธฐ์„œ๋Š” Just' โŠฅ = โŠฅ์ด๋‹ค. ์ ๊ทน์„ฑ ์žฅ์—์„œ ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฐœ๋ช…ํ•˜๊ณ  ์‹ถ์„ ์ˆ˜๋„ ์žˆ๋‹ค. data Couldbe a = Couldbe (Maybe a) ํ•˜์ง€๋งŒ Couldbe a๋Š” โŠฅ์™€ Couldbe โŠฅ๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•œ๋‹ค. newtype ์ •์˜์˜ ๋„์›€์„ ๋ฐ›์œผ๋ฉด newtype Couldbe a = Couldbe (Maybe a) Couldbe a๊ฐ€ ๊ตฌ๋ฌธ์ ์œผ๋กœ๋Š” Maybe a์™€ ๋™๋“ฑํ•˜์ง€๋งŒ ํƒ€์ž… ๊ฒ€์‚ฌ ์‹œ์—๋Š” ๋‹ค๋ฅด๊ฒŒ ์ทจ๊ธ‰ํ•˜๋„๋ก ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ Couldbe ์ƒ์„ฑ์ž๋Š” ์—„๊ฒฉํ•˜๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์ด ์ •์˜๋Š” ๋‹ค์Œ ์ •์˜์™€ ๋ฏธ๋ฌ˜ํ•˜๊ฒŒ ๋‹ค๋ฅด๋‹ค. data Couldbe' a = Couldbe' !(Maybe a) ๊ทธ ์ด์œ ๋ฅผ ์„ค๋ช…ํ•˜๋ ค๋ฉด ๋‹ค์Œ ํ•จ์ˆ˜๋“ค์„ ์‚ดํŽด๋ด์•ผ ํ•œ๋‹ค. f (Couldbe m) = 42 f' (Couldbe' m) = 42 ์—ฌ๊ธฐ์„œ f' โŠฅ๋Š” Couldbe' ์ƒ์„ฑ์ž์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ์„ ์‹คํŒจํ•˜๊ฒŒ ๋งŒ๋“œ๋Š”๋ฐ, f' โŠฅ = โŠฅ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ newtype์˜ ๊ฒฝ์šฐ Couldbe์— ๋Œ€ํ•œ ๋งค์นญ์€ ์ ˆ๋Œ€ ์‹คํŒจํ•˜์ง€ ์•Š์œผ๋ฉฐ ์šฐ๋ฆฌ๋Š” f โŠฅ = 42๋ฅผ ์–ป๋Š”๋‹ค. โŠฅ์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ์€ ์‹คํŒจํ•˜์ง€๋งŒ Constructor โŠฅ์— ๋Œ€ํ•œ ๋งค์นญ์€ ์„ฑ๊ณตํ•œ๋‹ค๋Š” ์ ์—์„œ, ์ด๋Ÿฌํ•œ ์ฐจ์ด๋ฅผ ๋‹ค์Œ์ฒ˜๋Ÿผ ์„œ์ˆ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—„๊ฒฉํ•จ์˜ ๊ฒฝ์šฐ Couldbe' โŠฅ๋Š” โŠฅ์˜ ๋™์˜์–ด๋‹ค. newtype์˜ ๊ฒฝ์šฐ โŠฅ๋Š” Couldbe โŠฅ์˜ ๋™์˜์–ด๋‹ค. newtype์€ ์žฌ๊ท€ ํƒ€์ž…์„ ์ •์˜ํ•˜๋Š”๋ฐ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋Š” ๋ฆฌ์ŠคํŠธ ํƒ€์ž… [a]๋ฅผ ๋‹ค๋ฅธ ์‹์œผ๋กœ ์ •์˜ํ•œ๋‹ค. newtype List a = In (Maybe (a, List a)) ์ด๋ฒˆ์—๋„ ์š”์ ์€ ์ƒ์„ฑ์ž In ๊ฐ€ โŠฅ์— ์˜ํ•œ ์ถ”๊ฐ€์ ์ธ lift๋ฅผ ๋„์ž…ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ์€ newtype๊ณผ ๋น„ ์—„๊ฒฉ ๋ฐ ์—„๊ฒฉํ•œ data ์„ ์–ธ๋“ค์˜ ์ฐจ์ด๋ฅผ ์„ค๋ช…ํ•˜๋Š” ์˜ˆ์ œ๋“ค์ด๋‹ค. Prelude> data D = D Int Prelude> data SD = SD! Int Prelude> newtype NT = NT Int Prelude> (\(D _) -> 42) (D undefined) 42 Prelude> (\(SD _) -> 42) (SD undefined) *** Exception: Prelude.undefined [...] Prelude> (\(NT _) -> 42) (NT undefined) 42 Prelude> (\(D _) -> 42) undefined *** Exception: Prelude.undefined [...] Prelude> (\(SD _) -> 42) undefined *** Exception: Prelude.undefined [...] Prelude> (\(NT _) -> 42) undefined 42 ๋‹ค๋ฅธ ์ฃผ์ œ๋“ค abstract interpretation๊ณผ ์—„๊ฒฉํ•จ ๋ถ„์„ ์ง€์—ฐ ํ‰๊ฐ€๋Š” ๊ณ ์ •์ ์ธ ๊ณ„์‚ฐ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์œ ๋ฐœํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋น„ ์—„๊ฒฉํ•จ์„ ๋‚ด์žฌํ•  ํ•„์š”๊ฐ€ ์ „ํ˜€ ์—†๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฐœ๊ฒฌํ•ด์„œ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์—†์• ๊ธธ ์›ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ์˜๋ฏธ์—์„œ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์—„๊ฒฉํ•œ ํ•จ์ˆ˜ ์ ˆ์—์„œ ํ•จ์ˆ˜์˜ ์—„๊ฒฉํ•จ์„ ์ž…์ฆํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์—„๊ฒฉํ•จ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋ฌผ๋ก  ์šฐ๋ฆฌ์˜ cond ์˜ˆ์ œ์ฒ˜๋Ÿผ ์ธ์ž์˜ ์ •ํ™•ํ•œ ๊ฐ’์— ๊ธฐ๋ฐ˜ํ•œ ์—„๊ฒฉํ•จ ์œ ์ถ”๋Š” ๋ฒ”์œ„ ๋ฐ–์ด๋‹ค. (์ด๋Ÿฐ ๊ฑด ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฒฐ์ • ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค) ํ•˜์ง€๋งŒ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋Œ€๋žต์ ์ธ ์—„๊ฒฉํ•จ ์ •๋ณด๋ฅผ ์–ป์œผ๋ ค ๋…ธ๋ ฅํ•˜๋ฉฐ power2์ฒ˜๋Ÿผ ๋งŽ์€ ๊ฒฝ์šฐ ์ž˜ ์ž‘๋™ํ•œ๋‹ค. abstract interpretation์€ ์—„๊ฒฉํ•จ์„ ์ถ”๋ก ํ•˜๊ธฐ ์œ„ํ•œ ์–ด๋งˆ์–ด๋งˆํ•œ ์•„์ด๋””์–ด๋‹ค. TODO: ์›๋ฌธ ๋ฏธ์™„์„ฑ ์—„๊ฒฉํ•จ ๋ถ„์„์— ๊ด€ํ•ด์„œ๋Š” ํ•˜์Šค ์ผˆ ์œ„ํ‚ค์˜ ์—„๊ฒฉํ•จ ๋ถ„์„์— ๊ด€ํ•œ ์—ฐ๊ตฌ ๋…ผ๋ฌธ๋“ค์„ ๋ณผ ๊ฒƒ. ๋ฉฑ์ง‘ํ•ฉ์œผ๋กœ์„œ์˜ ํ•ด์„ TODO: ์›๋ฌธ ๋‚ด์šฉ์ด ์˜ฌ๋ฐ”๋ฅธ์ง€์— ๋Œ€ํ•œ ๋…ผ์Ÿ์ด ์žˆ์Šต๋‹ˆ๋‹ค. naive set์€ ์žฌ๊ท€์  ์ž๋ฃŒํ˜•์— ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค What to choose as Semantic Domain? ์ ˆ์—์„œ ๋‹จ์ˆœํ•œ ์ง‘ํ•ฉ์„ ํƒ€์ž…์˜ ํ‘œ๊ธฐ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ถ€๋ถ„ ํ•จ์ˆ˜์™€ ์ž˜ ๋งž์ง€ ์•Š๋Š”๋‹ค๊ณ  ๋…ผํ–ˆ๋‹ค. ์žฌ๊ท€์  ์ž๋ฃŒํ˜•์„ ๊ณ ๋ คํ•˜๋ฉด John C. Reynolds๊ฐ€ ๊ทธ์˜ ๋…ผ๋ฌธ Polymorphism is not set-theoretic7์—์„œ ๋ณด์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์ƒํ™ฉ์€ ๋” ๋‚˜๋น ์ง„๋‹ค. Reynolds๋Š” ๋‹ค์Œ์˜ ์žฌ๊ท€์  ํƒ€์ž…์„ ๊ณ ๋ คํ–ˆ๋‹ค. newtype U = In ((U -> Bool) -> Bool) Bool์„ ์ง‘ํ•ฉ {True, False}, ํ•จ์ˆ˜ ํƒ€์ž… A -> B๋ฅผ A์—์„œ B๋กœ ๊ฐ€๋Š” ํ•จ์ˆ˜๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ ํ•ด์„ํ•  ๋•Œ, ํƒ€์ž… U์€ ์ง‘ํ•ฉ์„ ํ‘œ๊ธฐํ•˜์ง€ ๋ชปํ•œ๋‹ค. ๊ทธ ์ด์œ ๋Š” (A -> Bool)์€ A์˜ ํ•˜์œ„ ์ง‘ํ•ฉ๋“ค์˜ ์ง‘ํ•ฉ (์ฆ‰ ๋ฉฑ์ง‘ํ•ฉ)์ธ๋ฐ Cantor์˜ ์ž์—ฐ์ˆ˜๋ณด๋‹ค ์‹ค์ˆ˜๊ฐ€ ๋” ๋งŽ๋‹ค๋Š” ๋…ผ์˜์™€ ๋น„์Šทํ•œ ๋Œ€๊ฐ์„  ๋…ผ๋ฒ•์— ์˜ํ•ด ์ด ์ง‘ํ•ฉ์˜ cardinality๋Š” ํ•ญ์ƒ A์˜ cardinality๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ (U -> Bool) -> Bool์€ U๋ณด๋‹ค ํฐ cardinality๋ฅผ ๊ฐ€์ง€๊ณ  U์™€ ๋™ํ˜•(isomorphic)์ผ ๋ฆฌ๊ฐ€ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ง‘ํ•ฉ U๋Š” ์กด์žฌํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š”๋ฐ ์ด๋Š” ๋ชจ์ˆœ์ด๋‹ค. ์šฐ๋ฆฌ์˜ ๋ถ€๋ถ„ ํ•จ์ˆ˜ ์„ธ๊ณ„์—์„œ ์ด ๋…ผ๋ฒ•์€ ์‹คํŒจํ•œ๋‹ค. U์˜ ์›์†Œ๋Š” ์ผ๋ จ์˜ ์ •์˜์—ญ์œผ๋กœ๋ถ€ํ„ฐ ์ทจํ•œ ์ผ๋ จ์˜ approximation์— ์˜ํ•ด ์ฃผ์–ด์ง„๋‹ค. โŠฅ, (โŠฅ -> Bool) -> Bool, (((โŠฅ -> Bool) -> Bool) -> Bool) -> Bool ๋“ฑ๋“ฑ ์—ฌ๊ธฐ์„œ โŠฅ๋Š” ๋‹จ์ผ inhabitant โŠฅ๋ฅผ ๊ฐ€์ง€๋Š” ์ •์˜์—ญ์„ ํ‘œ๊ธฐํ•œ๋‹ค. ์ด ๊ธ€์„ ์“ฐ๊ณ  ์žˆ๋Š” ๋‚˜๋Š” ์ด๊ฒƒ์ด ๋œปํ•˜๋Š” ๋ฐ”๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๋ชจ๋ฅธ๋‹ค. ์ƒ์„ฑ์ž๋Š” U๋ฅผ ์œ„ํ•ด ์™„๋ฒฝํžˆ ์ž˜ ์ •์˜๋œ ๊ฐ์ฒด๋ฅผ ๋Œ๋ ค์ค€๋‹ค. ๊ทธ ํƒ€์ž…์ธ (U -> Bool) -> Bool์€ shifted approximating sequence๋“ค๋กœ ๊ตฌ์„ฑ๋˜๋Š”๋ฐ, U์™€ ๋™ํ˜•์ž„์„ ๋œปํ•œ๋‹ค. ๋งˆ์ง€๋ง‰ ๋…ธํŠธ๋กœ์„œ Reynold๋Š” ์ด์ฐจ ๋‹คํ˜• ๋žŒ๋‹ค ๋Œ€์ˆ˜๋ฅผ ํ†ตํ•ด U์˜ ๋™์น˜๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. ๋ชจ๋“  ํ•ญ์€ normal form์„ ๊ฐ€์ง„๋‹ค. ์ฆ‰ primitive recursion ์—ฐ์‚ฐ์ž fix :: (a -> a) -> a๋ฅผ ๋„์ž…ํ•˜์ง€ ์•Š์œผ๋ฉด total function๋“ค๋งŒ์ด ์กด์žฌํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋ถ€๋ถ„ ํ•จ์ˆ˜์™€ โŠฅ๋Š” ํ•„์š” ์—†๊ณ , naรฏve set theoretic semantics๋Š” ์‹คํŒจํ•œ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ชจ๋“  ์ˆ˜ํ•™์  ํ•จ์ˆ˜๊ฐ€ ๊ณ„์‚ฐ ๊ฐ€๋Šฅํ•˜์ง€๋Š” ์•Š๋‹ค๋Š” ์‚ฌ์‹ค๋งŒ ์ง์ž‘ํ•  ๋ฟ์ด๋‹ค. ํŠนํžˆ ๊ณ„์‚ฐ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋“ค์˜ ์ง‘ํ•ฉ Nat -> Bool๋Š” Nat๋ณด๋‹ค ํฐ cardinality๋ฅผ ๊ฐ€์ ธ์„œ๋Š” ์•ˆ ๋œ๋‹ค. ์™ธ๋ถ€ ๋งํฌ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์— ๊ด€ํ•œ ์˜จ๋ผ์ธ ์ฑ…๋“ค Schmidt, David A. (1986). Denotational Semantics. A Methodology for Language Development. Allyn and Bacon. ์‚ฌ์‹ค ํ•˜์Šค์ผˆ์˜ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์€ ์–ด๋””์—๋„ ์™„๋ฒฝํ•˜๊ฒŒ ์ ํ˜€์žˆ์ง€ ์•Š๋‹ค. ๊ทธ๋Ÿฐ ์ž‘์—…์€ ์ƒˆ๋กœ์šด ์ง๊ด€์„ ๊ฐ€์ ธ๋‹ค์ฃผ์ง€ ๋ชปํ•˜๋Š” ์ง€๋ฃจํ•œ ์ž‘์—…์ผ ๋ฟ์ด๋ฏ€๋กœ ์ „ํ†ต์ ์œผ๋กœ ๋‚ด๋ ค์˜ค๋Š” ์ƒ์‹์ ์ธ ์˜๋ฏธ๋ก ์„ ๋ฐ›์•„๋“ค์ด์ž. โ†ฉ ๋ชจ๋‚˜๋“œ๋Š” ๋ช…๋ นํ˜• ํ”„๋กœ๊ทธ๋žจ์— ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฐ€์žฅ ์„ฑ๊ณต์ ์ธ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋‹ค. Haskell/Advanced monads๋„ ์ฝ์–ด๋ณผ ๊ฒƒ. (์˜ฎ๊ธด์ด: Advanced monads ํŽ˜์ด์ง€ ์›๋ฌธ์—๋„ ์“ฐ์—ฌ์žˆ๋“ฏ์ด ๋Œ€๋ถ€๋ถ„ ๋‚ด์šฉ์€ ๋ชจ๋‚˜๋“œ ์ดํ•ดํ•˜๊ธฐ ํŽ˜์ด์ง€์— ํ†ตํ•ฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.) โ†ฉ ํ•จ์ˆ˜ ์ธ์ž์˜ ์ด๋ฅธ ํ‰๊ฐ€๋กœ์„œ์˜ ์—„๊ฒฉํ•จ์€ ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ์žฅ์—์„œ ์ž์„ธํžˆ ์„ค๋ช…ํ•œ๋‹ค. โ†ฉ ์ง€์—ฐ์„ฑ์ด๋ผ๋Š” ์šฉ์–ด๋Š” ๋น„ ์—„๊ฒฉ ์–ธ์–ด๋“ค์—์„œ ํ”ํ•œ ๊ตฌํ˜„ ๊ธฐ๋ฒ•์ด ์ง€์—ฐ ํ‰๊ฐ€๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ฒƒ์— ๊ธฐ์ธํ•œ๋‹ค. โ†ฉ lifted๋ผ๋Š” ์šฉ์–ด๋Š” ๋œป์ด ์กฐ๊ธˆ ๋งŽ๋‹ค. Unboxed Types๋„ ์ฝ์–ด๋ณผ ๊ฒƒ. (์˜ฎ๊ธด์ด: ์›๋ฌธ ๋งํฌ๊ฐ€ ๊นจ์ง) โ†ฉ S. Peyton Jones, A. Reid, T. Hoare, S. Marlow, and F. Henderson. A semantics for imprecise exceptions. In Programming Languages Design and Implementation. ACM press, May 1999. โ†ฉ John C. Reynolds. Polymorphism is not set-theoretic. INRIA Rapports de Recherche No. 296. May 1984. โ†ฉ 4 ๋ฒ”์ฃผ๋ก  ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Category_theory source object์™€ target object์˜ ๋งˆ๋•…ํ•œ ๋ฒˆ์—ญ ์šฉ์–ด๋ฅผ ์ฐพ์ง€ ๋ชปํ•ด ๊ทธ๋Œ€๋กœ ํ‘œ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต์€ mapping์„ ์‚ฌ์ƒ์ด๋ผ๊ณ  ๋ฒˆ์—ญํ•˜๋Š”๋ฐ ์ด ํŽ˜์ด์ง€์—์„œ๋Š” morphism๊ณผ mapping์„ ๊ตฌ๋ถ„ํ•ด์„œ ์“ฐ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋‹จ ์—ฌ๊ธฐ ํ•œ์ •์œผ๋กœ morphism์„ ์‚ฌ์ƒ, mapping์„ ๊ทธ๋ƒฅ ๋งคํ•‘์ด๋ผ๊ณ  ๋ฒˆ์—ญํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋“ค์—์„œ๋Š” ๋‹ค mapping์„ ์‚ฌ์ƒ์ด๋ผ๊ณ  ๋ฒˆ์—ญํ•œ ๊ฒƒ์— ์œ ์˜ํ•ด ์ฃผ์„ธ์š”. ๋ฒ”์ฃผ(category) ์ž…๋ฌธ ๋ฒ”์ฃผ ๋ฒ•์น™ Hask: ํ•˜์Šค์ผˆ์˜ ๋ฒ”์ฃผ ํŽ‘ ํ„ฐ Hask์— ๋Œ€ํ•œ ํŽ‘ ํ„ฐ ๋ฒ” ์ฃผ๋ก ์  ๊ฐœ๋…๋“ค์„ ํ•˜์Šค ์ผˆ๋กœ ๋ฒˆ์—ญํ•˜๊ธฐ ๋ชจ๋‚˜๋“œ ์˜ˆ์ œ: ๋ฉฑ์ง‘ํ•ฉ ํŽ‘ํ„ฐ๋Š” ๋ชจ๋‚˜๋“œ๋‹ค ๋ชจ๋‚˜๋“œ ๋ฒ•์น™๊ณผ ๊ทธ ์ค‘์š”์„ฑ ์ฒซ ๋ฒˆ์งธ ๋ฒ•์น™ ๋‘ ๋ฒˆ์งธ ๋ฒ•์น™ ์„ธ ๋ฒˆ์งธ ๋ฒ•์น™๊ณผ ๋„ค ๋ฒˆ์งธ ๋ฒ•์น™ do ๋ธ”๋ก์— ๋Œ€ํ•œ ์‘์šฉ ์š”์•ฝ ์ด ๊ธ€์—์„œ๋Š” ๋ฒ”์ฃผ๋ก ์ด ๋ฌด์—‡์ด๊ณ  ํ•˜์Šค์ผˆ์— ์–ด๋–ป๊ฒŒ ์ ์šฉ๋˜๋Š”์ง€ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณธ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ˆ˜ํ•™์  ์ •์˜๋งˆ๋‹ค ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๊ฐ€ ๊ฐ™์ด ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค. ์ •๋ง ์—„๋ฐ€ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์€ ๋…์ž๋“ค์ด ๋ฒ”์ฃผ๋ก ์˜ ๊ฐœ๋…๋“ค์„ ์ง๊ด€์ ์œผ๋กœ ๋Š๋ผ๊ณ  ๊ทธ๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ํ•˜์Šค์ผˆ์— ์—ฐ๊ด€๋˜๋Š”์ง€ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. ๋ฒ”์ฃผ(category) ์ž…๋ฌธ ์„ธ ๊ฐœ์ฒด A, B, C, ์„ธ ๋™ํ˜• ์‚ฌ์ƒ d, d, d, ๋‘ ์‚ฌ์ƒ : โ†’, : โ†’ ์„ ๊ฐ€์ง€๋Š” ๊ฐ„๋‹จํ•œ ๋ฒ”์ฃผ. ์„ธ ๋ฒˆ์งธ ์š”์†Œ(์‚ฌ์ƒ๋“ค์„ ํ•ฉ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋ช…์„ธ)๋Š” ํ‘œ์‹œํ•˜์ง€ ์•Š์•˜๋‹ค. ๋ณธ์งˆ์ ์œผ๋กœ ๋ฒ”์ฃผ๋Š” ๊ทธ์ € ๋ชจ์Œ์ด๋‹ค. ๋ฒ”์ฃผ๋Š” ์„ธ ๊ตฌ์„ฑ์š”์†Œ(component)๋ฅผ ๊ฐ€์ง„๋‹ค. ๊ฐœ์ฒด(object)๋“ค์˜ ๋ชจ์Œ. ์‚ฌ์ƒ(morphism)๋“ค์˜ ๋ชจ์Œ. ํ•œ ์‚ฌ์ƒ์€ ๋‘ ๊ฐœ์ฒด(source object์™€ target object)๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค. (์‚ฌ์ƒ์„ ์• ๋กœ(arrow)๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•˜์ง€๋งŒ ํ•˜์Šค์ผˆ์—์„œ ์• ๋กœ๋Š” ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์—, ์• ๋กœ๋ผ๋Š” ์šฉ์–ด๋Š” ์“ฐ์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค) f๊ฐ€ source object๋กœ A, target object๋กœ B๋ฅผ ๊ฐ€์ง€๋Š” ์‚ฌ์ƒ์ด๋ฉด : โ†’๋กœ ํ‘œ๊ธฐํ•œ๋‹ค. ์‚ฌ์ƒ๋“ค์˜ ํ•ฉ์„ฑ(composition)์ด๋ผ๋Š” ๊ฐœ๋…. ๋‘ ์‚ฌ์ƒ : โ†’ ์™€ : โ†’ ์„ ํ•ฉ์„ฑํ•˜๋ฉด ์‚ฌ์ƒ โˆ˜ : โ†’๋ฅผ ์–ป๋Š”๋‹ค. ๋งŽ์€ ๊ฒƒ์ด ๋ฒ”์ฃผ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Set์€ ๋ชจ๋“  ์ง‘ํ•ฉ์˜ ๋ฒ”์ฃผ๋กœ์„œ ์‚ฌ์ƒ์€ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜, ํ•ฉ์„ฑ์€ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜ ํ•ฉ์„ฑ์ด ๋œ๋‹ค. (๋ฒ”์ฃผ์˜ ์ด๋ฆ„์€ ๊ตต๊ฒŒ ํ‘œ์‹œํ•˜๊ณค ํ•œ๋‹ค) Grp์€ ๋ชจ๋“  ๊ตฐ(group)์˜ ๋ฒ”์ฃผ๋‹ค. ๊ทธ๋Ÿฐ ๊ตฐ์—์„œ ์‚ฌ์ƒ์€ ๊ตฐ ์—ฐ์‚ฐ์„ ๋ณด์กดํ•˜๋Š” ํ•จ์ˆ˜๋‹ค(๊ตฐ ๋™ํ˜•์‚ฌ์ƒ). ์ฆ‰ ์—ฐ์‚ฐ *๋ฅผ ๊ฐ€์ง€๋Š” ๊ตฐ G์™€ ์—ฐ์‚ฐ ยท๋ฅผ ๊ฐ€์ง€๋Š” ๊ตฐ H์— ๋Œ€ํ•ด ๋‹ค์Œ์„ ๋งŒ์กฑํ•˜๋Š” ํ•จ์ˆ˜ : โ†’๋Š” Grp์˜ ์‚ฌ์ƒ์ด๋‹ค. ( โˆ— ) f ( ) f ( ) ์‚ฌ์ƒ์ด ํ•ญ์ƒ ํ•จ์ˆ˜์ธ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ถ€๋ถ„ ์ˆœ์„œ ( , ) ๊ฐ€ ์ •์˜ํ•˜๋Š” ๋ฒ”์ฃผ์—์„œ๋Š” P์˜ ์›์†Œ๋“ค์ด ๊ฐœ์ฒด๊ฐ€ ๋˜๊ณ , ๋‘ ๊ฐœ์ฒด A์™€ B ์‚ฌ์ด์— ์‚ฌ์ƒ์ด ์žˆ์„ ํ•„์š”์ถฉ๋ถ„์กฐ๊ฑด์€ โ‰ค์ด๋‹ค. ๋”์šฑ์ด ๋™์ผํ•œ source object, target object์— ๋Œ€ํ•ด ์‚ฌ์ƒ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ์ˆ˜ ์žˆ๋‹ค. Set์„ ์˜ˆ๋กœ ๋“ค๋ฉด sin๊ณผ cos ๋‘˜ ๋‹ค source object๊ฐ€ (์‹ค์ˆ˜ ์ง‘ํ•ฉ)์ด๊ณ  target object๋Š” [ 1 1 ] ์ธ ํ•จ์ˆ˜์ง€๋งŒ, ๋ถ„๋ช…ํžˆ ๊ฐ™์€ ์‚ฌ์ƒ์€ ์•„๋‹ˆ๋‹ค! ๋ฒ”์ฃผ ๋ฒ•์น™ ๋ฒ”์ฃผ๊ฐ€ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋Š” ์„ธ ๊ฐ€์ง€ ๋ฒ•์น™์ด ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๊ฒƒ์œผ๋กœ ์‚ฌ์ƒ์˜ ํ•ฉ์„ฑ์— ๊ฒฐํ•ฉ๋ฒ•์น™์ด ์„ฑ๋ฆฝํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ธฐํ˜ธ๋กœ ํ‘œํ˜„ํ•˜๋ฉด โˆ˜ ( โˆ˜ ) ( โˆ˜ ) h ์ˆ˜ํ•™์—์„œ ๋Œ€๊ฐœ ๊ทธ๋ ‡๋“ฏ์ด ํ•˜์Šค์ผˆ์—์„œ๋Š” ์‚ฌ์ƒ์ด ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ ์šฉ๋œ๋‹ค. ๋”ฐ๋ผ์„œ โˆ˜์—์„œ๋Š” g๊ฐ€ ๋จผ์ € ์ ์šฉ๋˜๊ณ  ๊ทธ๋‹ค์Œ f๊ฐ€ ์ ์šฉ๋œ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฒ•์น™์€ ๋ฒ”์ฃผ๊ฐ€ ํ•ฉ์„ฑ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด ๋‹ซํ˜€์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ : โ†’ ์™€ : โ†’ ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ ๋ฒ”์ฃผ ์•ˆ์— ์‚ฌ์ƒ : โ†’ ๊ฐ€ ๋ฐ˜๋“œ์‹œ ์กด์žฌํ•ด๊ณ , = โˆ˜ ์—ฌ์•ผ ํ•œ๋‹ค. ์ด๊ฒŒ ์–ด๋–ค ์‹์œผ๋กœ ์ž‘๋™ํ•˜๋Š”์ง€๋Š” ๋‹ค์Œ์˜ ๋ฒ”์ฃผ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. f์™€ g ๋‘˜ ๋‹ค ์‚ฌ์ƒ์ด๋ฏ€๋กœ ๋‘˜์„ ํ•ฉ์„ฑํ•ด ๊ทธ ๋ฒ”์ฃผ์— ์†ํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ์‚ฌ์ƒ์„ ์–ป์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์‚ฌ์ƒ โˆ˜๋Š” ๋ฌด์—‡์ธ๊ฐ€? ์œ ์ผํ•œ ํ›„๋ณด๋Š” d์ด๋‹ค. ๋น„์Šทํ•˜๊ฒŒ โˆ˜ = d์ด๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ๋ฒ”์ฃผ C ์•ˆ์˜ ๋ชจ๋“  ๊ฐœ์ฒด A์— ๋Œ€ํ•ด ๋™ํ˜• ์‚ฌ์ƒ d : โ†’ ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ด๊ฒƒ์€ ๋‹ค๋ฅธ ์‚ฌ์ƒ๊ณผ ํ•ฉ์„ฑํ•  ๋•Œ ํ•ญ๋“ฑ์›์ด ๋œ๋‹ค. ์ •ํ™•ํžˆ ๋งํ•˜๋ฉด ๋ชจ๋“  ์‚ฌ์ƒ : โ†’์— ๋Œ€ํ•ด ๋‹ค์Œ์ด ์„ฑ๋ฆฝํ•ด์•ผ ํ•œ๋‹ค. โˆ˜ d = d โˆ˜ = Hask: ํ•˜์Šค์ผˆ์˜ ๋ฒ”์ฃผ ์ด ๊ธ€์—์„œ๋Š” Hask ๋ฒ”์ฃผ๋ฅผ ์ž์ฃผ ๋ณด๊ฒŒ ๋œ๋‹ค. Hask๋Š” ํ•˜์Šค ์ผˆ ํƒ€์ž…์„ ๊ฐœ์ฒด๋กœ, ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์ƒ์œผ๋กœ, (.)์„ ํ•ฉ์„ฑ์œผ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค. ํƒ€์ž… A์™€ B์— ๋Œ€ํ•œ ํ•จ์ˆ˜ f :: A -> B๋Š” Hask ์•ˆ์—์„œ ์‚ฌ์ƒ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ๋ฒ•์น™์€ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. (.)๋Š” ์—ฐ๊ด€ ๋ฒ•์น™์ด ์„ฑ๋ฆฝํ•˜๋Š” ํ•จ์ˆ˜๊ณ  ๋ชจ๋“  f์™€ g์— ๋Œ€ํ•ด f. g๊ฐ€ ๋˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜์ž„์€ ์ž๋ช…ํ•˜๋‹ค. Hask์—์„œ ๋™ํ˜• ์‚ฌ์ƒ์€ id์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๋„ ์ž๋ช…ํ•˜๋‹ค. id. f = f. id = f 1 ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฒƒ์ด ์œ„ ๋ฒ•์น™์„ ์ •ํ™•ํžˆ ์˜ฎ๊ธด ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ์ฒจ์ž๊ฐ€ ๋น ์ ธ์žˆ๋‹ค. ํ•˜์Šค์ผˆ์˜ ํ•จ์ˆ˜ id๋Š” ๋‹คํ˜•์„ฑ์ด์–ด์„œ ์ •์˜์—ญ๊ณผ ์น˜์—ญ์œผ๋กœ ๋‹ค์–‘ํ•œ ํƒ€์ž…์„ ํ—ˆ์šฉํ•˜์—ฌ, ๋ฒ”์ฃผ๋ก ์˜ ์šฉ์–ด๋กœ ๋งํ•˜๋ฉด ๋‹ค์–‘ํ•œ source object์™€ target object๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฒ”์ฃผ๋ก ์—์„œ ์‚ฌ์ƒ์˜ ์ •์˜๋Š” ๋‹จ ํ˜•์„ฑ(monomorphic)์ด๋‹ค. ๊ฐ ์‚ฌ์ƒ์€ ๊ตฌ์ฒด์ ์ธ ํ•œ source object์™€ ํ•œ target object๋ฅผ ๊ฐ€์ง„๋‹ค. (์ „๋ฌธ๊ฐ€๋ฅผ ์œ„ํ•œ ๋…ธํŠธ: ์—ฌ๊ธฐ์„œ monomorphic์ด๋ผ๋Š” ๋‹จ์–ด๋Š” ๋ฒ”์ฃผ๋ก ์˜ ๊ทธ ์˜๋ฏธ๋กœ ์“ด ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค) ํ•˜์Šค์ผˆ์˜ ๋‹คํ˜• ํ•จ์ˆ˜๋Š” ๊ทธ ํƒ€์ž…์„ ๊ตฌ์ฒดํ™”ํ•˜์—ฌ (๋‹จํ˜• ํƒ€์ž…์œผ๋กœ ์ธ์Šคํ„ด์Šคํ™”ํ•˜์—ฌ) ๋‹จํ˜•์„ฑ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ํƒ€์ž… A์— ๋Œ€ํ•ด Hask์˜ ๋™ํ˜• ์‚ฌ์ƒ์€ (id :: A -> A)๋ผ๊ณ  ๋งํ•˜๋Š” ๊ฒŒ ์ข€ ๋” ์ •ํ™•ํ•˜๊ฒ ๋‹ค. ์ด๋ฅผ ์—ผ๋‘์— ๋‘๊ณ  ์œ„ ๋ฒ•์น™์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ค์‹œ ์“ธ ์ˆ˜ ์žˆ๋‹ค. (id :: B -> B) . f = f. (id :: A -> A) = f ํ•˜์ง€๋งŒ ๋œปํ•˜๋Š” ๋ฐ”๊ฐ€ ๋ช…ํ™•ํ•˜๋‹ค๋ฉด ๋‹จ์ˆœํ•จ์„ ์œ„ํ•ด ์ด๋Ÿฐ ๊ตฌ๋ถ„์„ ๋ฌด์‹œํ•  ๊ฒƒ์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์•ž์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ์ž„์˜์˜ ๋ถ€๋ถ„ ์ˆœ์„œ ( , ) ๋Š” P์˜ ์›์†Œ๋“ค์„ ๊ฐœ์ฒด๋กœ, ์›์†Œ a, b ์‚ฌ์ด์˜ ์‚ฌ์ƒ์ด โ‰ค ์ธ ๋ฒ”์ฃผ๋‹ค. ์œ„ ๋ฒ•์น™๋“ค ์ค‘ ์–ด๋Š ๊ฒƒ์ด์˜ ์ดํ–‰์„ฑ(transitivity)์„ ๋ณด์žฅํ•˜๋Š”๊ฐ€? (์–ด๋ ค์›€) ์œ„ ์˜ˆ์ œ์— ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋˜ ๋‹ค๋ฅธ ์‚ฌ์ƒ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฒ”์ฃผ๊ฐ€ ์•„๋‹ˆ๊ฒŒ ๋œ๋‹ค. ๊ทธ ์ด์œ ๋Š”? ํžŒํŠธ: ํ•ฉ์„ฑ ์—ฐ์‚ฐ์˜ ๊ฒฐํ•ฉ ๋ฒ•์น™์„ ์ƒ๊ฐํ•ด ๋ณด๋ผ. ํŽ‘ ํ„ฐ ๊ฐœ์ฒด๋“ค๊ณผ ๊ทธ ๊ฐœ์ฒด๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ์‚ฌ์ƒ์„ ๊ฐ€์ง€๋Š” ๋ฒ”์ฃผ๋ฅผ ์•Œ๊ฒŒ ๋˜์—ˆ์œผ๋‹ˆ ๋‹ค์Œ ํ™”์ œ๋Š” ๋ฒ”์ฃผ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ํŽ‘ํ„ฐ๋‹ค. ํŽ‘ํ„ฐ์˜ ๋ณธ์งˆ์€ ๋ฒ”์ฃผ ์‚ฌ์ด์˜ ๋ณ€ํ™˜์ด๋‹ค. ๋ฒ”์ฃผ C์™€ D์— ๋Œ€ํ•ด ํŽ‘ ํ„ฐ : โ†’๋Š” ๋‹ค์Œ์„ ๋งŒ์กฑํ•œ๋‹ค. C ์•ˆ์˜ ๊ฐœ์ฒด A๋ฅผ D ์•ˆ์˜ F(A)์— ๋งคํ•‘ํ•œ๋‹ค. C ์•ˆ์˜ ์‚ฌ์ƒ : โ†’๋ฅผ D ์•ˆ์˜ ( ) F ( ) F ( ) ์— ๋งคํ•‘ํ•œ๋‹ค. ํŽ‘ํ„ฐ์˜ ์ „ํ˜•์ ์ธ ์˜ˆ์‹œ๋Š” ์žŠ๊ธฐ ์‰ฌ์šด ํŽ‘ ํ„ฐ์ธ Grp โ†’ Set์ด๋‹ค. ์ด ํŽ‘ํ„ฐ๋Š” ๊ตฐ์„ ๊ทธ ๊ธฐ์ €์˜ ์ง‘ํ•ฉ์— ๋งคํ•‘ํ•˜๊ณ , ๊ตฐ ์‚ฌ์ƒ์„ ํ•จ์ˆ˜๋กœ ๋งคํ•‘ํ•˜๋Š”๋ฐ ์ด ํ•จ์ˆ˜๋Š” ํ•˜๋Š” ์ผ์€ ๊ฐ™์ง€๋งŒ ๊ตฐ ๋Œ€์‹  ์ง‘ํ•ฉ์— ๋Œ€ํ•ด ์ •์˜๋œ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋กœ ๋ฉฑ์ง‘ํ•ฉ ํŽ‘ ํ„ฐ Set -> Set์€ ์ง‘ํ•ฉ์„ ๊ทธ๋“ค์˜ ๋ฉฑ์ง‘ํ•ฉ์œผ๋กœ ๋งคํ•‘ํ•˜๊ณ , ํ•จ์ˆ˜ : โ†’๋ฅผ ํ•จ์ˆ˜ ( ) P ( ) ๋กœ ๋งคํ•‘ํ•œ๋‹ค. ์ด๋•Œ ์ž…๋ ฅ์€ โŠ†์ด๊ณ  ( ) ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ( ) ๋Š” f ํ•˜์—์„œ U์˜ ์ƒ(image)์œผ๋กœ์„œ ( ) { ( ) u U } ๋กœ ์ •์˜๋œ๋‹ค. ์ž„์˜์˜ ๋ฒ”์ฃผ C์— ๋Œ€ํ•ด ํ•ญ๋“ฑ ํŽ‘ ํ„ฐ C C C ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๊ฒƒ์€ ๋‹จ์ˆœํžˆ ๊ฐœ์ฒด๋ฅผ ์ž์‹ ์œผ๋กœ, ์‚ฌ์ƒ์„ ์ž์‹ ์œผ๋กœ ๋งคํ•‘ํ•˜๋Š” ์ผ์„ ํ•œ๋‹ค. ๋‚˜์ค‘์— ๋ชจ๋‚˜๋“œ ๋ฒ•์น™ ์ ˆ์—์„œ ์ด๊ฒƒ์ด ์–ด๋””์— ์œ ์šฉํ•œ์ง€ ๋ณผ ๊ฒƒ์ด๋‹ค. ํŽ‘ํ„ฐ์—๋„ ๋ช‡ ๊ฐ€์ง€ ๊ณต๋ฆฌ๊ฐ€ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋กœ ๊ฐœ์ฒด A์— ๋Œ€ํ•œ ํ•ญ๋“ฑ ์‚ฌ์ƒ d์— ๋Œ€ํ•ด ( d) F ( ) ์— ๋Œ€ํ•œ ํ•ญ๋“ฑ ์‚ฌ์ƒ์ด์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰ ( d) i F ( ) ๋‘ ๋ฒˆ์งธ๋กœ ํŽ‘ํ„ฐ๋Š” ์‚ฌ์ƒ ํ•ฉ์„ฑ์— ๋Œ€ํ•ด ๋ถ„๋ฐฐ๋ฒ•์น™์ด ์„ฑ๋ฆฝํ•ด์•ผ ํ•œ๋‹ค. ์ฆ‰ ( โˆ˜ ) F ( ) F ( ) ์—ฐ์Šต๋ฌธ์ œ ์•„๋ž˜ ๋‹ค์ด์–ด๊ทธ๋žจ์— ๋Œ€ํ•ด ์œ„ ํŽ‘ ํ„ฐ ๋ฒ•์น™๋“ค์„ ํ™•์ธํ•˜๋ผ. ๋‘ ๋ฒ”์ฃผ C์™€ D ์‚ฌ์ด์˜ ํŽ‘ํ„ฐ. ๊ฐœ์ฒด A์™€ B๋Š” D ๋‚ด์˜ ๊ฐ™์€ ๊ฐœ์ฒด๋กœ ๋งคํ•‘๋˜๋ฏ€๋กœ g๋Š” source object์™€ target object๊ฐ€ ๋™์ผํ•œ ์‚ฌ์ƒ์ด ๋œ๋‹ค. (ํ•˜์ง€๋งŒ ๊ผญ ํ•ญ๋“ฑ์ธ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค) d ์™€ d๋Š” ๋™์ผํ•œ ์‚ฌ์ƒ์ด๋‹ค. ๊ฐœ์ฒด ๊ฐ„ ๋งคํ•‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ™”์‚ดํ‘œ๋Š” ์˜…์€ ์ดˆ๋ก์ƒ‰ ์ ์„ ์œผ๋กœ ํ‘œ์‹œํ–ˆ๋‹ค. ์‚ฌ์ƒ ๊ฐ„ ๋งคํ•‘์€ ์˜…์€ ํŒŒ๋ž€์ƒ‰ ์ ์„ ์œผ๋กœ ํ‘œ์‹œํ–ˆ๋‹ค. Hask์— ๋Œ€ํ•œ ํŽ‘ ํ„ฐ ์—ฌ๋Ÿฌ๋ถ„์ด ํ•˜์Šค์ผˆ์—์„œ ๋ดค์„ Functor ํƒ€์ž… ํด๋ž˜์Šค๋Š” ํŽ‘ํ„ฐ์˜ ๋ฒ” ์ฃผ๋ก ์  ํ‘œ๊ธฐ์™€ ์ผ์น˜ํ•œ๋‹ค. ํŽ‘ํ„ฐ์—๋Š” ๋‘ ๋ถ€๋ถ„์ด ์žˆ์Œ์„ ๊ธฐ์–ตํ•  ๊ฒƒ: ํŽ‘ํ„ฐ๋Š” ํ•œ ๋ฒ”์ฃผ์˜ ๊ฐœ์ฒด๋ฅผ ๋‹ค๋ฅธ ๋ฒ”์ฃผ์˜ ๊ฐœ์ฒด๋กœ, ํ•œ ๋ฒ”์ฃผ์˜ ์‚ฌ์ƒ์„ ๋‹ค๋ฅธ ๋ฒ”์ฃผ์˜ ์‚ฌ์ƒ์œผ๋กœ ๋งคํ•‘ํ•œ๋‹ค. ํ•˜์Šค์ผˆ์˜ ํŽ‘ํ„ฐ๋Š” Hask์˜ func์— ํ•ด๋‹นํ•˜๋Š”๋ฐ, func๋Š” ๊ทธ ํŽ‘ํ„ฐ์˜ ํƒ€์ž…๋“ค์— ๋Œ€ํ•ด ์ •์˜๋œ Hask ํ•˜์œ„ ๋ฒ”์ฃผ๋‹ค. ์—๋ฅผ ๋“ค์–ด ๋ฆฌ์ŠคํŠธ ํŽ‘ํ„ฐ๋Š” Hask์—์„œ ๋‚˜์˜จ Lst๋กœ์„œ Lst๋Š” ๋ฆฌ์ŠคํŠธ ํƒ€์ž…๋“ค, ์ฆ‰ ํƒ€์ž… T์— ๋Œ€ํ•œ [T]๋งŒ์„ ํฌํ•จํ•˜๋Š” ๋ฒ”์ฃผ๋‹ค. Lst ๋‚ด์˜ ์‚ฌ์ƒ์€ ๋ฆฌ์ŠคํŠธ ํƒ€์ž…๋“ค์— ๋Œ€ํ•ด, ํƒ€์ž… T, U์— ๋Œ€ํ•œ [T] -> [U]๋กœ ์ •์˜๋œ๋‹ค. ์ด๊ฒƒ์ด ํ•˜์Šค์ผˆ์˜ Functor ํƒ€์ž… ํด๋ž˜์Šค์™€ ์–ด๋–ป๊ฒŒ ์ผ์น˜ํ•œ๋‹ค๋Š” ๊ฑธ๊นŒ? Functor์˜ ์ •์˜๋ฅผ ๋– ์˜ฌ๋ ค๋ณด์ž. class Functor (f :: * -> *) where fmap :: (a -> b) -> f a -> f b ๋‹ค์Œ์€ Functor์˜ ํ•œ ์ธ์Šคํ„ด์Šค๋‹ค. instance Functor Maybe where fmap f (Just x) = Just (f x) fmap _ Nothing = Nothing ํ•ต์‹ฌ์€ ์ด๊ฑฐ๋‹ค. ํƒ€์ž… ์ƒ์„ฑ์ž Maybe๋Š” ์ž„์˜ ํƒ€์ž… T๋ฅผ ์ƒˆ๋กœ์šด ํƒ€์ž… Maybe T๋กœ ๋ฐ๋ ค๊ฐ„๋‹ค. ๋˜ํ•œ Maybe ํƒ€์ž…๋“ค์— ํ•œ์ •๋œ fmap์€ ํ•จ์ˆ˜ a -> b๋ฅผ ํ•จ์ˆ˜ Maybe a -> Maybe b๋กœ ๋ฐ๋ ค๊ฐ„๋‹ค. ๋ฐ”๋กœ ์ด๊ฑฐ๋‹ค! ๋ฐฉ๊ธˆ ๋‘ ๊ฐ€์ง€๋ฅผ ์ •์˜ํ–ˆ๋Š”๋ฐ ํ•˜๋‚˜๋Š” Hask ์•ˆ์˜ ๊ฐœ์ฒด๋ฅผ ๋‹ค๋ฅธ ๋ฒ”์ฃผ์˜ ๊ฐœ์ฒด๋กœ ๋ฐ๋ ค๊ฐ€๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” Hask ์•ˆ์˜ ์‚ฌ์ƒ์„ ๋‹ค๋ฅธ ๋ฒ”์ฃผ์˜ ์‚ฌ์ƒ์œผ๋กœ ๋ฐ๋ ค๊ฐ„๋‹ค. ๋”ฐ๋ผ์„œ Maybe๋Š” ํŽ‘ํ„ฐ๋‹ค. ํ•˜์Šค ์ผˆ ํŽ‘ํ„ฐ์— ๋Œ€ํ•œ ์œ ์šฉํ•œ ์ง๊ด€์ด ์žˆ๋Š”๋ฐ, ํŽ‘ํ„ฐ๋Š” ๋งคํ•‘ ๊ฐ€๋Šฅํ•œ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ทธ ํƒ€์ž…์€ ๋ฆฌ์ŠคํŠธ, Maybe, ๋˜๋Š” ํŠธ๋ฆฌ ๊ฐ™์€ ๋” ๋ณต์žกํ•œ ๊ตฌ์กฐ์ฒด์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ๋ฌด์–ธ๊ฐ€ ๋งคํ•‘์„ ํ•˜๋Š” ํ•จ์ˆ˜๋Š” fmap์„ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ๊ทธ๋‹ค์Œ ์ž„์˜์˜ ํŽ‘ ํ„ฐ ๊ตฌ์กฐ์ฒด๋ฅผ ์ด ํ•จ์ˆ˜์— ์ „๋‹ฌํ•œ๋‹ค. ๊ฐ€๋ น ์—ฌ๋Ÿฌ๋ถ„์€ Data.List.Map, Data.Map.map, Data.Array.IArray.amap ๋“ฑ์„ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•˜๋Š” ์ผ๋ฐ˜ํ™”๋œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํŽ‘ ํ„ฐ ๊ณต๋ฆฌ๋Š” ์–ด๋– ํ•œ๊ฐ€? ๋‹คํ˜• ํ•จ์ˆ˜ id๋Š” ์ž„์˜์˜ A์— ๋Œ€ํ•ด d ์—ญํ• ์„ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ฒซ ๋ฒˆ์งธ ๋ฒ•์น™์ด ์„ฑ๋ฆฝํ•œ๋‹ค. fmap id = id ์œ„์˜ ์ง๊ด€์„ ์—ผ๋‘์— ๋‘๋ฉด ์ด ๋ฌธ์žฅ์ด ๋œปํ•˜๋Š” ๋ฐ”๋Š” "๊ตฌ์กฐ์ฒด์˜ ๊ฐ ์›์†Œ์— ๋Œ€ํ•ด ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋Š” ๋งคํ•‘์€ ์ „๋ฐ˜์ ์œผ๋กœ ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๊ณผ ๋™์น˜"๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ ์‚ฌ์ƒ ํ•ฉ์„ฑ์€ ๋‹จ์ˆœํžˆ (.)์ด๋‹ค. ๋”ฐ๋ผ์„œ fmap (f . g) = fmap f. fmap g ์ด ๋‘ ๋ฒˆ์งธ ๋ฒ•์น™์€ ์‹ค์ „์—์„œ ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค. ํŽ‘ํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธ๋‚˜ ๊ทธ ๋น„์Šทํ•œ ์ปจํ…Œ์ด๋„ˆ๋กœ ๋ณด๋ฉด ์šฐ๋ณ€์€ 2๋‹จ๊ณ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๊ตฌ์กฐ์ฒด ์ „์ฒด์— ๋Œ€ํ•ด g๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋งคํ•‘ํ•˜๊ณ  ๊ทธ๋‹ค์Œ f๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋‹ค์‹œ ๋งคํ•‘ํ•œ๋‹ค. ํŽ‘ ํ„ฐ ๊ณต๋ฆฌ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ด๊ฒƒ์„ f. g๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” 1๋‹จ๊ณ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์žฅํ•œ๋‹ค. ์ด๋Ÿฐ ์ ˆ์ฐจ๋ฅผ ํ“จ์ „(fusion)์ด๋ผ๊ณ  ํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ Maybe ํŽ‘ํ„ฐ์™€ ๋ฆฌ์ŠคํŠธ ํŽ‘ํ„ฐ์— ๋Œ€ํ•ด ์œ„ ๋ฒ•์น™๋“ค์„ ํ™•์ธํ•˜๋ผ. ๋ฒ” ์ฃผ๋ก ์  ๊ฐœ๋…๋“ค์„ ํ•˜์Šค ์ผˆ๋กœ ๋ฒˆ์—ญํ•˜๊ธฐ ํŽ‘ํ„ฐ๋Š” ๋ฒ”์ฃผ๋ก ์ด ํ•˜์Šค ์ผˆ๋กœ ์–ด๋–ป๊ฒŒ ๋ฒˆ์—ญ๋˜๋Š”์ง€๋ฅผ ์ž˜ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์‹œ๋‹ค. ํ•ต์‹ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์šฐ๋ฆฌ๋Š” Hask์™€ ๊ทธ ํ•˜์œ„ ๋ฒ”์ฃผ์—์„œ ์ž‘์—…ํ•œ๋‹ค. ๊ฐœ์ฒด๋Š” ํƒ€์ž…์ด๋‹ค. ์‚ฌ์ƒ์€ ํ•จ์ˆ˜๋‹ค. ํƒ€์ž…์„ ์ทจํ•ด ๋‹ค๋ฅธ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ๋“ค์€ ํƒ€์ž… ์ƒ์„ฑ์ž๋‹ค. ํ•จ์ˆ˜๋ฅผ ์ทจํ•ด ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ๋“ค์€ ๊ณ ์ฐจ ํ•จ์ˆ˜๋‹ค. ํƒ€์ž… ํด๋ž˜์Šค์™€ ๊ทธ๊ฒƒ์ด ์ œ๊ณตํ•˜๋Š” ๋‹คํ˜•์„ฑ์€ ๋ฒ”์ฃผ๋ก ์—์„œ ์ด๋Ÿฐ์ €๋Ÿฐ ๊ฒƒ๋“ค์ด ๋งŽ์€ ๊ฐœ์ฒด์— ๋Œ€ํ•ด ํ•œ ๋ฒˆ์— ์ •์˜๋œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ํฌ์ฐฉํ•˜๋Š” ํ›Œ๋ฅญํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ๋ชจ๋‚˜๋“œ ๋ชจ๋‚˜๋“œ๋Š” ๋ถ„๋ช… ํ•˜์Šค์ผˆ์—์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ๊ฐœ๋…์ด๊ณ , ์‚ฌ์‹ค ๋ฒ”์ฃผ๋ก ์—์„œ ์œ ๋ž˜ํ•œ ๊ฒƒ์ด๋‹ค. ๋ชจ๋‚˜๋“œ๋Š” ํŠน์ˆ˜ํ•œ ํŽ‘ํ„ฐ๋กœ์„œ ํ•œ ๋ฒ”์ฃผ๋ฅผ ๋™์ผํ•œ ๋ฒ”์ฃผ๋กœ ๋ฐ๋ ค๊ฐ€๋ฉฐ ์ถ”๊ฐ€์ ์ธ ๊ตฌ์กฐ์ฒด๋ฅผ ์ง€์›ํ•œ๋‹ค. ๊ทธ๋Ÿผ ์ •์˜๋ฅผ ๋ณด์ž. ๋ชจ๋‚˜๋“œ๋Š” ํŽ‘ ํ„ฐ : โ†’์ด๋ฉฐ C ์•ˆ์˜ ๋ชจ๋“  ๊ฐœ์ฒด X์— ๋Œ€ํ•ด ๋‘ ์‚ฌ์ƒ์„ 2 ๊ฐ€์ง„๋‹ค. n t M X M ( ) o n M M ( ( ) ) M ( ) ๋…ผ์˜ํ•˜๋ ค๋Š” ๋ชจ๋‚˜๋“œ๊ฐ€ ๋ช…ํ™•ํ•˜๋‹ค๋ฉด ์œ—์ฒจ์ž M์„ ๋–ผ์–ด๋‚ด๊ณ  ๋‹จ์ˆœํžˆ ์–ด๋–ค X์— ๋Œ€ํ•œ n t ์™€ o n๋ผ๊ณ  ํ•˜๊ฒ ๋‹ค. ์ฃผ์–ด์ง„ ๋ชจ๋‚˜๋“œ์˜ ๋ชจ๋“  ๊ฐœ์ฒด์— ๋Œ€ํ•ด ์กด์žฌํ•ด์•ผ ํ•˜๋Š” ๋‘ ์‚ฌ์ƒ unit๊ณผ join. ์ด๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ํ•˜์Šค ์ผˆ ํƒ€์ž… ํด๋ž˜์Šค Monad๋กœ ๋ฒˆ์—ญ๋˜๋Š”์ง€ ๋ณด์ž. class Functor m => Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b Functor m์ด๋ผ๋Š” ํด๋ž˜์Šค ์ œ์•ฝ์€ ์šฐ๋ฆฌ๊ฐ€ ์ด๋ฏธ ํŽ‘ ํ„ฐ ๊ตฌ์กฐ, ์ฆ‰ ๊ฐœ์ฒด์™€ ์‚ฌ์ƒ์˜ ๋งคํ•‘์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ์„ ๋ณด์žฅํ•œ๋‹ค. return์€ ์ž„์˜ X์— ๋Œ€ํ•ด n t ์™€ ๋น„์Šทํ•˜๋ฉฐ ๋‹คํ•ญ์ ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ œ๊ฐ€ ํ•˜๋‚˜ ์žˆ๋‹ค. return์˜ ํƒ€์ž…์ด unit์˜ ํƒ€์ž…๊ณผ ์ƒ๋‹นํžˆ ๋น„์Šทํ•ด ๋ณด์ด์ง€๋งŒ ๋‹ค๋ฅธ ํ•จ์ˆ˜ (>>=)๋Š”, ์ฃผ๋กœ ๋ฐ”์ธ๋“œ bind๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, join๊ณผ ํ•˜๋‚˜๋„ ๋‹ฎ์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋˜ ๋‹ค๋ฅธ ๋ชจ๋‚˜๋“œ ํ•จ์ˆ˜ join :: Monad m => m (m a) -> m a๋Š” ์ƒ๋‹นํžˆ ๋น„์Šทํ•ด ๋ณด์ธ๋‹ค. ์‚ฌ์‹ค join๊ณผ (>>=)๋Š” ์„œ๋กœ๋ฅผ ํ† ๋Œ€๋กœ ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. join :: Monad m => m (m a) -> m a join x = x >>= id (>>=) :: Monad m => m a -> (a -> m b) -> m b x >>= f = join (fmap f x) ๋”ฐ๋ผ์„œ ๋ชจ๋‚˜๋“œ์˜ return, fmap, join์„ ๋ช…์‹œํ•˜๋Š” ๊ฒƒ์€ return๊ณผ (>>=)์„ ๋ช…์‹œํ•˜๋Š” ๊ฒƒ๊ณผ ๋™๋“ฑํ•˜๋‹ค. ๋ฒ”์ฃผ๋ก ์—์„œ ๋ชจ๋‚˜๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์€ unit๊ณผ join์ด์ง€๋งŒ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์€ return๊ณผ (>>=)๋ฅผ ์„ ํ˜ธํ•œ๋‹ค. 3 ๋Œ€๊ฐœ ๋ฒ”์ฃผ๋ก ์˜ ๋ฐฉ๋ฒ•์ด ๋” ํƒ€๋‹นํ•˜๋‹ค. ์–ด๋–ค ๊ตฌ์กฐ M์ด ์žˆ๊ณ  ์ž„์˜ ๊ฐœ์ฒด X๋ฅผ M(X)๋กœ ๋ฐ๋ ค๊ฐ€๋Š” ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๋ฉด, ๊ทธ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ M(M(X))๋ฅผ M(X)๋กœ ๋ฐ๋ ค๊ฐ€๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค๋ฉด, ํ™•์‹คํžˆ ๋ชจ๋‚˜๋“œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ ์ด๊ฒƒ์„ ๋ณธ๋‹ค. ์˜ˆ์ œ: ๋ฉฑ์ง‘ํ•ฉ ํŽ‘ํ„ฐ๋Š” ๋ชจ๋‚˜๋“œ๋‹ค ์œ„์—์„œ ์„œ์ˆ ํ•œ ๋ฉฑ์ง‘ํ•ฉ ํŽ‘ ํ„ฐ : e โ†’ e ์€ ๋ชจ๋‚˜๋“œ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. ์ž„์˜ ์ง‘ํ•ฉ S์— ๋Œ€ํ•ด n t ( ) { } ์€ ์›์†Œ๋ฅผ ๋‹จ์œ„์ง‘ํ•ฉ์œผ๋กœ ๋งคํ•‘ํ•œ๋‹ค. ๊ฐ๊ฐ์˜ ๋‹จ์œ„์ง‘ํ•ฉ์€ S์˜ ์ž๋ช…ํ•œ ํ•˜์œ„ ์ง‘ํ•ฉ์ด๋ฏ€๋กœ n t๋Š” S์˜ ๋ฉฑ์ง‘ํ•ฉ์˜ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  o n๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž…๋ ฅ โˆˆ ( ( ) ) ์„ ๋ฐ›๋Š”๋‹ค. ์ด ์ž…๋ ฅ์€ S์˜ ๋ฉฑ์ง‘ํ•ฉ์˜ ๋ฉฑ์ง‘ํ•ฉ์˜ ๋ฉค๋ฒ„๋‹ค. ๋”ฐ๋ผ์„œ S์˜ ๋ชจ๋“  ํ•˜์œ„ ์ง‘ํ•ฉ์˜ ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ํ•˜์œ„ ์ง‘ํ•ฉ์˜ ์ง‘ํ•ฉ์˜ ๋ฉค๋ฒ„๋‹ค. ๋”ฐ๋ผ์„œ S์˜ ํ•˜์œ„ ์ง‘ํ•ฉ๋“ค์˜ ์ง‘ํ•ฉ์ด๋‹ค. ์ด์ œ ์ด ํ•˜์œ„ ์ง‘ํ•ฉ๋“ค์˜ ํ•ฉ์ง‘ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ S์˜ ๋˜ ๋‹ค๋ฅธ ํ•˜์œ„ ์ง‘ํ•ฉ์„ ์ œ๊ณตํ•œ๋‹ค. ๊ธฐํ˜ธ๋กœ ํ‘œํ˜„ํ•˜๋ฉด o n ( ) โ‹ƒ ๋”ฐ๋ผ์„œ P๋Š” ๋ชจ๋‚˜๋“œ๋‹ค. 4 ์‚ฌ์‹ค P๋Š” ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ์™€ ๊ฑฐ์˜ ๋™๋“ฑํ•˜๋‹ค. ์ง‘ํ•ฉ ๋Œ€์‹  ๋ฆฌ์ŠคํŠธ๋ผ๋Š” ๊ฒƒ๋งŒ ๋นผ๋ฉด ๊ฑฐ์˜ ๊ฐ™๋‹ค. ๋น„๊ตํ•ด ๋ณด์ž. ์ง‘ํ•ฉ์— ๋Œ€ํ•œ ๋ฉฑ์ง‘ํ•ฉ ํŽ‘ ํ„ฐ ํ•จ์ˆ˜ ํƒ€์ž… ์ •์˜ ์ง‘ํ•ฉ S์™€ ์‚ฌ์ƒ : โ†’์— ๋Œ€ํ•ด ( ) P ( ) P ( ) ( ( ) ) ( ) { ( ) a S } n t : โ†’ ( ) n t ( ) { } o n : ( ( ) ) P ( ) o n ( ) โ‹ƒ ํ•˜์Šค์ผˆ์˜ List ๋ชจ๋‚˜๋“œ ํ•จ์ˆ˜ ํƒ€์ž… ์ •์˜ ํƒ€์ž… T ์™€ ํ•จ์ˆ˜ f :: A -> B์— ๋Œ€ํ•ด fmap f :: [A] -> [B] fmap f xs = [ f a | a <- xs ] return :: T -> [T] return x = [x] join :: [[T]] -> [T] join xs = concat xs ๋ชจ๋‚˜๋“œ ๋ฒ•์น™๊ณผ ๊ทธ ์ค‘์š”์„ฑ ํŽ‘ํ„ฐ๊ฐ€ ํŽ‘ ํ„ฐ์ด๊ธฐ ์œ„ํ•ด ๋งŒ์กฑํ•ด์•ผ ํ•˜๋Š” ๊ณต๋ฆฌ๊ฐ€ ์žˆ์—ˆ๋“ฏ์ด ๋ชจ๋‚˜๋“œ๋„ ๋ชจ๋‚˜๋“œ์˜ ๊ณต๋ฆฌ๊ฐ€ ์žˆ๋‹ค. ๋จผ์ € ๊ทธ ๊ณต๋ฆฌ๋“ค์„ ๋‚˜์—ดํ•˜๊ณ , ํ•˜์Šค ์ผˆ๋กœ ๋ฒˆ์—ญํ•˜๊ณ , ์™œ ์ค‘์š”ํ•œ์ง€ ์‚ดํŽด๋ณด๊ฒ ๋‹ค. ๋‹ค์Œ์€ ๋ชจ๋‚˜๋“œ : โ†’ ์™€, , โˆˆ์— ๋Œ€ํ•œ ์‚ฌ์ƒ : โ†’์ด๋‹ค. o n M ( o n ) j i โˆ˜ o n o n M ( n t ) j i โˆ˜ n t i u i โˆ˜ = ( ) u i j i โˆ˜ ( ( ) ) M ( ) j i ์œ„์˜ ํ•˜์Šค ์ผˆ ๋ฒˆ์—ญํŒ๋“ค์ด ์ด์ œ๋Š” ์ฒ™ ๋ณด๋ฉด ์•Œ ์ •๋„๊ฐ€ ๋˜์—ˆ์„ ๊ฒƒ์ด๋‹ค. join . fmap join = join . join join . fmap return = join . return = id return . f = fmap f. return join . fmap (fmap f) = fmap f. join (fmap์€ ์‚ฌ์ƒ์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋Š” ํŽ‘ํ„ฐ์˜ ์ผ๋ถ€์ž„์„ ๊ธฐ์–ตํ•˜์ž.) ์ด ๋ฒ•์น™๋“ค์ด ์ฒ˜์Œ์—๋Š” ๋‚œํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฒ•์น™๋“ค์ด ๋ฌด์—‡์„ ๋œปํ•˜๊ณ  ๋ชจ๋‚˜๋“œ๊ฐ€ ์™œ ์ด๊ฒƒ๋“ค์„ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๋Š” ํƒํ—˜์„ ์‹œ์ž‘ํ•˜์ž. ์ฒซ ๋ฒˆ์งธ ๋ฒ•์น™ join . fmap join = join . join ์ด ๋ฒ•์น™์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค๊ฒ ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฒ•์น™์—๋Š” ๋‘ ํ•จ์ˆ˜ join . fmap join (์ขŒ๋ณ€)๊ณผ join . join (์šฐ๋ณ€)์ด ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์˜ ํƒ€์ž…์€ ๋ฌด์—‡์ผ๊นŒ? join์˜ ํƒ€์ž…์ด [[a]] -> [a] (์ง€๊ธˆ์€ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ๋งํ•˜๋Š” ์ค‘์ด๋‹ค) ์ž„์„ ๋– ์˜ฌ๋ ค๋ณด๋ฉด ๋‘ ํ•จ์ˆ˜์˜ ํƒ€์ž…์€ ๋˜‘๊ฐ™์ด [[[a]]] -> [a]์ด๋‹ค. (๋‘ ํƒ€์ž…์ด ๊ฐ™๋‹ค๋Š” ์‚ฌ์‹ค์€ ํŽธ๋ฆฌํ•˜๋‹ค. ๋ฌด์—‡๋ณด๋‹ค ์šฐ๋ฆฌ๋Š” ์ด ๋‘˜์ด ์™„์ „ํžˆ ๊ฐ™์€ ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ๋ณด์ด๋ ค๋Š” ์ค‘์ด๋‹ค!) ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ์—๊ฒ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค. ์ขŒ๋ณ€์€ ์ด 3์ค‘์ฒฉ ๋ฆฌ์ŠคํŠธ์— fmap join์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ์— join์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. fmap์€ ๋ฆฌ์ŠคํŠธ์—์„  ๋‹จ์ˆœํžˆ map์ด๋ฏ€๋กœ ์šฐ๋ฆฌ๋Š” ๊ฐ€์žฅ ๋ฐ”๊นฅ์˜ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์žˆ๋Š” ๊ฐ๊ฐ์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋“ค์— ๋งคํ•‘์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ฒฐ๊ตญ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ป๊ณ , ์—ฌ๊ธฐ์— join์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์š”์•ฝํ•˜๋ฉด ์šฐ๋ฆฌ๋Š” ๊ฐ€์žฅ ๋ฐ”๊นฅ ๋ ˆ๋ฒจ์— '๋“ค์–ด๊ฐ€์„œ' ๋‘ ๋ฒˆ์งธ ์ธต๊ณผ ์„ธ ๋ฒˆ์งธ ์ธต์„ ํ•ฉ์น˜๊ณ  ์ด ์ƒˆ๋กœ์šด ๋ ˆ๋ฒจ์„ ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ๊ณผ ํ•ฉ์นœ๋‹ค. ์šฐ๋ณ€์€ ์–ด๋– ํ•œ๊ฐ€? ๋จผ์ € ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ์— join์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ธต์ด 3๊ฐœ๊ณ , ๋ณดํ†ต์€ 2์ค‘์ฒฉ ๋ฆฌ์ŠคํŠธ์— join์„ ์ ์šฉํ•˜์ง€๋งŒ, ์ด ์ƒํ™ฉ์—์„œ๋„ ์ž‘๋™์„ ํ•œ๋‹ค. [[[a]]]๋Š” b = [a]์— ๋Œ€ํ•ด [[b]]์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์–ด๋–ค ์˜๋ฏธ์—์„œ 3์ค‘์ฒฉ ๋ฆฌ์ŠคํŠธ๋Š” 2์ค‘์ฒฉ ๋ฆฌ์ŠคํŠธ์ธ๋ฐ ๋งˆ์ง€๋ง‰ ์ธต์ด 'ํ‰ํ‰ํ•œ' ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋˜ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ์ธ ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ(์˜ ๋ฆฌ์ŠคํŠธ)๋ฅผ join์— ์ ์šฉํ•˜๋ฉด ๋ฐ”๊นฅ์˜ ๋‘ ์ธต์ด ํ•œ ์ธต์œผ๋กœ ํŽด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธต์ด ํ‰ํ‰ํ•˜์ง€ ์•Š๊ณ  ์„ธ ๋ฒˆ์งธ ์ธต์„ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ์ „ํžˆ ์šฐ๋ฆฌ์—๊ฒŒ ์žˆ๋Š” ๊ฒƒ์€ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๊ณ , ๋˜ ๋‹ค๋ฅธ join์ด ์ด๊ฒƒ์„ ํŽธ๋‹ค. ์š”์•ฝํ•˜๋ฉด ์ขŒ๋ณ€์€ ์•ˆ์ชฝ์˜ ๋‘ ๋ ˆ์ด์–ด๋ฅผ ์ƒˆ๋กœ์šด ๋ ˆ์ด์–ด๋กœ ํŽธ ๋‹ค์Œ ์ด๊ฒƒ์„ ๊ฐ€์žฅ ๋ฐ”๊นฅ์˜ ๋ฆฌ์ŠคํŠธ์™€ ํ•จ๊ป˜ ํŽธ๋‹ค. ์šฐ๋ณ€์€ ๋ฐ”๊นฅ์˜ ๋‘ ๋ ˆ์ด์–ด๋ฅผ ํŽธ ๋‹ค์Œ ๊ฐ€์žฅ ์•ˆ์ชฝ์˜ ๋ ˆ์ด์–ด๋ฅผ ํŽธ๋‹ค. ์ด ๋‘ ์—ฐ์‚ฐ์€ ๋™์น˜์—ฌ์•ผ ํ•œ๋‹ค. ์ผ์ข…์˜ join์„ ์œ„ํ•œ ์—ฐ๊ด€ ๋ฒ•์น™์ธ ์…ˆ์ด๋‹ค. Maybe๋„ ๋‹ค์Œ์„ ๋งŒ์กฑํ•˜๋Š” ๋ชจ๋‚˜๋“œ๋‹ค. return :: a -> Maybe a return x = Just x join :: Maybe (Maybe a) -> Maybe a join Nothing = Nothing join (Just Nothing) = Nothing join (Just (Just x)) = Just x 3์ค‘์ฒฉ Maybe(Nothing, Just Nothing, Just (Just Nothing), Just (Just (Just x))) ๋“ฑ)์— ๋Œ€ํ•ด, ์ฒซ ๋ฒˆ์งธ ๋ฒ•์น™์— ๋”ฐ๋ฅด๋ฉด ์•ˆ์ชฝ ๋‘ ์ธต์„ ๋จผ์ € ํ•ฉ์น˜๊ณ  ๊ฐ€์žฅ ๋ฐ”๊นฅ ์ธต์„ ํ•ฉ์น˜๋Š” ๊ฒƒ์€ ๋ฐ”๊นฅ ๋‘ ์ธต์„ ๋จผ์ € ํ•ฉ์น˜๊ณ  ๊ฐ€์žฅ ์•ˆ์ชฝ ์ธต์„ ํ•ฉ์น˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋ฆฌ์ŠคํŠธ์™€ Maybe ๋ชจ๋‚˜๋“œ๊ฐ€ ์ด ๋ฒ•์น™์„ ๋งŒ์กฑํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์ œ๋ฅผ ์ด์šฉํ•ด ๊ฒ€์ฆํ•˜๊ณ  ๋ ˆ์ด์–ด ํ•ฉ์น˜๊ธฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด์ž. ๋‘ ๋ฒˆ์งธ ๋ฒ•์น™ join . fmap return = join . return = id ์ด๋ฒˆ์—๋„ ๋ฆฌ์ŠคํŠธ ์˜ˆ์‹œ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฒ•์น™์ด ์–ธ๊ธ‰ํ•˜๋Š” ๋‘ ํ•จ์ˆ˜๋Š” [a] -> [a]์ด๋‹ค. ์ขŒ๋ณ€์€ ๋ฆฌ์ŠคํŠธ์— ์‚ฌ์ƒํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•˜๋Š”๋ฐ, ๊ฐ ์›์†Œ x๋ฅผ ์‹ฑ๊ธ€ํ„ด ๋ฆฌ์ŠคํŠธ [x]๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ์‹ฑ๊ธ€ํ„ด ๋ฆฌ์ŠคํŠธ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค. ์ด 2์ค‘ ๋ฆฌ์ŠคํŠธ๋ฅผ join์„ ์ด์šฉํ•ด ๋‹จ์ผ ๋ฆฌ์ŠคํŠธ๋กœ ์ง“๋ˆ„๋ฅธ๋‹ค. ๋ฐ˜๋ฉด ์šฐ๋ณ€์€ ์ „์ฒด ๋ฆฌ์ŠคํŠธ [x, y, z, ...]๋ฅผ ์ทจํ•ด ๋ฆฌ์ŠคํŠธ๋“ค์˜ ์‹ฑ๊ธ€ํ„ด ๋ฆฌ์ŠคํŠธ [[x, y, z, ...]]๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋‹ค์‹œ ๋‘ ์ธต์„ ํ•œ ์ธต์œผ๋กœ ์ง“๋ˆ„๋ฅธ๋‹ค. ์ด ๋ฒ•์น™์€ ํ•œ ๋งˆ๋””๋กœ ์š”์•ฝํ•˜๊ธฐ ์• ๋งคํ•˜์ง€๋งŒ, ๊ทธ ๊ทผ๊ฐ„์€ ๋ชจ๋‚˜ ๋”• ๊ฐ’์— return์„ ์ ์šฉํ•˜๊ณ  join ํ•˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋Š” return์„ ์ตœ์ƒ์œ„ ์ธต์˜ ๋ฐ–์—์„œ ํ•˜๋“  ์•ˆ์—์„œ ํ•˜๋“  ๋™์ผํ•˜๋‹ค๋Š” ๋œป์ด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ Maybe ๋ชจ๋‚˜๋“œ์— ๋Œ€ํ•ด ๋‘ ๋ฒˆ์งธ ๋ฒ•์น™์„ ์ฆ๋ช…ํ•ด ๋ณด์ž. ์„ธ ๋ฒˆ์งธ ๋ฒ•์น™๊ณผ ๋„ค ๋ฒˆ์งธ ๋ฒ•์น™ return . f = fmap f. return join . fmap (fmap f) = fmap f. join ์ด ๋‘ ๋ฒ•์น™์€ ์šฐ๋ฆฌ๊ฐ€ ๋ชจ๋‚˜๋“œ์˜ ํ–‰๋™์— ๊ธฐ๋Œ€ํ•˜๋Š” ๋ฐ”๋ฅผ ์ข€ ๋” ์Šค์Šค๋กœ ๋“œ๋Ÿฌ๋‚ธ๋‹ค. ์ด๊ฒƒ๋“ค์ด ์ฐธ์ž„์„ ๋ณด์ด๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ํ™•์žฅํ˜•์„ ์‚ฌ์šฉํ•ด ์‹์„ ํŽผ์น˜๋Š” ๊ฒƒ์ด๋‹ค. \x -> return (f x) = \x -> fmap f (return x) \x -> join (fmap (fmap f) x) = \x -> fmap f (join x) ์—ฐ์Šต๋ฌธ์ œ ์šฐ๋ฆฌ๊ฐ€ ์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ ๋ฒ•์น™์„ ์„ค๋ช…ํ•œ ๊ฒƒ๊ณผ ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ ์ด ๋ฒ•์น™๋“ค์ด ๋ฌด์—‡์„ ๋œปํ•˜๋Š”์ง€ ํƒ๊ตฌํ•˜๊ณ  ๋ชจ๋“  ๋ชจ๋‚˜๋“œ์— ๋Œ€ํ•ด ์ฐธ์ž„์„ ๋ณด์ด์ž. do ๋ธ”๋ก์— ๋Œ€ํ•œ ์‘์šฉ ๋ชจ๋‚˜๋“œ๊ฐ€ ๋ฐ˜๋“œ์‹œ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋Š” ๋ฒ•์น™๋“ค์„ ์•Œ์•„๋ณด๊ธด ํ–ˆ๋Š”๋ฐ ๋ญ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฑธ๊นŒ? ๊ทธ ๋‹ต์€ do ๋ธ”๋ก์„ ๊ณ ๋ คํ•˜๋ฉด ๋ช…ํ™•ํ•ด์ง„๋‹ค. do ๋ธ”๋ก์€ (>>=)๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฌธ(statement)๋“ค์˜ ์กฐํ•ฉ์„ ์œ„ํ•œ ํŽธ์˜ ๋ฌธ๋ฒ•์ผ ๋ฟ์ด๋ผ๋Š” ๊ฒƒ์„ ์ƒ๊ธฐํ•˜์ž. do { x } --> x do { let { y = v }; x } --> let y = v in do { x } do { v <- y; x } --> y >>= \v -> do { x } do { y; x } --> y >>= \_ -> do { x } ์ผ๋ฐ˜์ ์œผ๋กœ return๊ณผ (>>=)์„ ์ด์šฉํ•ด ์„œ์ˆ ๋˜๋Š” ๋ชจ๋‚˜๋“œ ๋ฒ•์น™์ด๋ผ๋Š” ๊ฒƒ๋“ค์„, ์œ„์˜ ๋ฒ•์น™๋“ค์„ ํ†ตํ•ด ์ฆ๋ช…ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. (๋ช‡ ๊ฐœ๋Š” ์ฆ๋ช…์ด ์กฐ๊ธˆ ๋นก์„ธ๋‹ˆ ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ€๋„ ๋œ๋‹ค) return x >>= f = f x. ์ฆ๋ช…: return x >>= f = join (fmap f (return x)) -- By the definition of (>>=) = join (return (f x)) -- By law 3 = (join . return) (f x) = id (f x) -- By law 2 = f x m >>= return = m. ์ฆ๋ช…: m >>= return = join (fmap return m) -- By the definition of (>>=) = (join . fmap return) m = id m -- By law 2 = m (m >>= f) >>= g = m >>= (\x -> f x >>= g). ์ฆ๋ช…: (fmap f. fmap g = fmap (f . g) ์ž„์„ ์ˆ™์ง€) (m >>= f) >>= g = (join (fmap f m)) >>= g -- By the definition of (>>=) = join (fmap g (join (fmap f m))) -- By the definition of (>>=) = (join . fmap g) (join (fmap f m)) = (join . fmap g. join) (fmap f m) = (join . join . fmap (fmap g)) (fmap f m) -- By law 4 = (join . join . fmap (fmap g) . fmap f) m = (join . join . fmap (fmap g. f)) m -- By the distributive law of functors = (join . join . fmap (\x -> fmap g (f x))) m = (join . fmap join . fmap (\x -> fmap g (f x))) m -- By law 1 = (join . fmap (join . (\x -> fmap g (f x)))) m -- By the distributive law of functors = (join . fmap (\x -> join (fmap g (f x)))) m = (join . fmap (\x -> f x >>= g)) m -- By the definition of (>>=) = join (fmap (\x -> f x >>= g) m) = m >>= (\x -> f x >>= g) -- By the definition of (>>=) return๊ณผ (>>=)๋ฅผ ์ด์šฉํ•œ ์ƒˆ ๋ชจ๋‚˜๋“œ ๋ฒ•์น™๋“ค์„ do ๋ธ”๋ก ํ‘œ๊ธฐ๋กœ ๋ฒˆ์—ญํ•  ์ˆ˜ ์žˆ๋‹ค. ์ธ์ž ์ƒ๋žต์‹ do ๋ธ”๋ก return x >>= f = f x do { v <- return x; f v } = do { f x } m >>= return = m do { v <- m; return v } = do { m } (m >>= f) >>= g = m >>= (\x -> f x >>= g) do { y <- do { x <- m; f x }; g y } = do { x <- m; y <- f x; g y } ๋ชจ๋‚˜๋“œ ๋ฒ•์น™๋“ค์€ ์ด์ œ do ๋ธ”๋ก์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์ƒ์‹์„ ์˜ ์„œ์ˆ ์ด๋‹ค. ์ด ๋ฒ•์น™๋“ค ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๊นจ์ง€๋ฉด ์‚ฌ์šฉ์ž๋“ค์€ ํ˜ผ๋ž€์Šค๋Ÿฌ์›Œํ•  ๊ฒƒ์ด๋‹ค. do ๋ธ”๋ก ์•ˆ์—์„œ ์ƒํ™ฉ์„ ์˜ˆ์ƒ๋Œ€๋กœ ์กฐ์ž‘ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ชจ๋‚˜๋“œ ๋ฒ•์น™๋“ค์€ ์‚ฌ์šฉ์„ฑ ์ง€์นจ์ธ ์…ˆ์ด๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์‚ฌ์‹ค ์šฐ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•œ ๋‘ ๋ฒ„์ „์˜ ๋ฒ•์น™๋“ค์€ -- Categorical: join . fmap join = join . join join . fmap return = join . return = id return . f = fmap f. return join . fmap (fmap f) = fmap f. join -- Functional: m >>= return = m return m >>= f = f m (m >>= f) >>= g = m >>= (\x -> f x >>= g) ์™„์ „ํžˆ ๋™์น˜๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ฒ”์ฃผ๋ก ์˜ ๋ฒ•์น™๋“ค๋กœ๋ถ€ํ„ฐ ํ•จ์ˆ˜ํ˜• ๋ฒ•์น™๋“ค์„ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์˜€๋‹ค. ๊ทธ ๋ฐ˜๋Œ€๋ฅผ ํ•ด๋ณผ ๊ฒƒ. ํ•จ์ˆ˜ํ˜• ๋ฒ•์น™๋“ค์—์„œ ์‹œ์ž‘ํ•ด ๋ฒ”์ฃผ๋ก  ๋ฒ•์น™๋“ค์ด ์„ฑ๋ฆฝํ•จ์„ ๋ณด์—ฌ๋ผ. ๋‹ค์Œ ์ •์˜๋“ค์„ ๊ธฐ์–ตํ•˜๋ฉด ๋„์›€์ด ๋  ๊ฒƒ์ด๋‹ค. join m = m >>= id fmap f m = m >>= return . f ์ด ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ์ œ์•ˆํ•œ ๊ฒƒ์— ๋Œ€ํ•ด Yitzchak Gale์—๊ฒŒ ๊ฐ์‚ฌํ•œ๋‹ค. ์š”์•ฝ ๊ธฐ๋‚˜๊ธด ์—ฌ์ •์„ ๊ฑธ์–ด์˜ค๋ฉฐ ๋ฒ”์ฃผ๋ž€ ๋ฌด์—‡์ด๊ณ  ํ•˜์Šค์ผˆ์— ์–ด๋–ป๊ฒŒ ์ ์šฉ๋˜๋Š”์ง€ ์‚ดํŽด๋ดค๋‹ค. ๋ฒ”์ฃผ๋ก ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…์ธ ํŽ‘ํ„ฐ์™€ ๋ชจ๋‚˜๋“œ์ฒ˜๋Ÿผ ์ข€ ๋” ๊ณ ๊ธ‰ ๊ฐœ๋…๋„ ์†Œ๊ฐœํ•˜๊ณ , ์ด๊ฒƒ๋“ค์ด ํ•˜์Šค์ผˆ์˜ ๊ด€์šฉ๊ตฌ์— ์–ผ๋งˆ๋‚˜ ์ค‘์š”ํ•œ์ง€ ์‚ดํŽด๋ดค๋‹ค. ์ž์—ฐ ๋ณ€ํ™˜(natural transformation)์ฒ˜๋Ÿผ ๋ฒ”์ฃผ๋ก ์˜ ๊ธฐ๋ณธ์ด์ง€๋งŒ ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์—๋Š” ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๋“ค์€ ๋‹ค๋ฃจ์ง€ ์•Š์•˜๋Š”๋ฐ, ๊ทธ ๋Œ€์‹  ํ•˜์Šค์ผˆ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฒ” ์ฃผ๋ก ์  ๊ธฐ๋ฐ˜์„ ์ง๊ด€์ ์œผ๋กœ ๋Š๊ปด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์กŒ๋‹ค. ์‚ฌ์‹ค ์—ฌ๊ธฐ์—๋Š” ๋ฏธ๋ฌ˜ํ•œ ์ ์ด ์žˆ๋‹ค. (.)๋Š” ์ง€์—ฐ ํ•จ์ˆ˜๊ธฐ ๋•Œ๋ฌธ์— f๊ฐ€ undefined ๋ฉด id. f = \_ -> โŠฅ๊ฐ€ ๋œ๋‹ค. ์ด๊ฒŒ ํ•ญ์ƒ โŠฅ์™€ ๋™์น˜์ผ ๊ฒƒ ๊ฐ™์ง€๋งŒ, ์—„๊ฒฉํ•จ ๊ฐ•์ œ ํ•จ์ˆ˜ seq๋ฅผ ์ด์šฉํ•ด ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ๋งˆ์ง€๋ง‰ ๋ฒ”์ฃผ๋ก  ๋ฒ•์น™์ด ๊นจ์ง„ ๊ฒƒ์ด๋‹ค. ์ƒˆ๋กญ๊ฒŒ ์—„๊ฒฉํ•œ ํ•ฉ์„ฑ ํ•จ์ˆ˜ f .! g = ((.) $! f) $! g๋ฅผ ์ •์˜ํ•ด Hask๋ฅผ ์™„๋ฒฝํ•œ ๋ฒ”์ฃผ๋กœ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ผ๋ฐ˜์ ์ธ (.)๋ฅผ ๊ณ„์† ์“ฐ๊ณ , ์ด ๊ฐ™์•„์•ผ ํ•  ๊ฒƒ๋“ค ์‚ฌ์ด์˜ ์ฐจ์ด๋Š” seq๊ฐ€ ๋งŽ์€ ์–ธ์–ด ์„ฑ์งˆ์„ ๊นจ๋œจ๋ฆฌ๋Š” ํƒ“์œผ๋กœ ๋Œ๋ฆฌ๊ฒ ๋‹ค. โ†ฉ ์ˆ™๋ จ๋œ ๋ฒ”์ฃผ๋ก ์ž๋“ค์€ ์šฐ๋ฆฌ๊ฐ€ ์—ฌ๊ธฐ์„œ ์ƒํ™ฉ์„ ์ข€ ๋‹จ์ˆœํ™”ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ˆˆ์น˜์ฑ˜์„ ๊ฒƒ์ด๋‹ค. unit๊ณผ join์„ ์ž์—ฐ ๋ณ€ํ™˜(natural transformation)์œผ๋กœ์„œ ๋„์ž…ํ•˜๋Š” ๋Œ€์‹  ์‚ฌ์ƒ์ด๋ผ๊ณ  ์ทจ๊ธ‰ํ•˜๊ณ , naturality๋Š” ํ‘œ์ค€ ๋ชจ๋‚˜๋“œ ๋ฒ•์น™๋“ค(๋ฒ•์น™ 3๊ณผ 4)๊ณผ ํ•จ๊ป˜ ์ถ”๊ฐ€์ ์ธ ๊ณต๋ฆฌ๋กœ ๋‚ด์„ธ์› ๋‹ค. ์ด์œ ๋Š” ๋‹จ์ˆœํ•จ์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ฒ”์ฃผ๋ก  ์ „์ฒด๋ฅผ ๊ฐ€๋ฅด์น˜๋ ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•˜์Šค์ผˆ์˜ ๊ตฌ์กฐ์˜ ๋ฒ” ์ฃผ๋ก ์  ๋ฐฐ๊ฒฝ์„ ์„ค๋ช…ํ•˜๋ ค๋Š” ๊ฒƒ๋ฟ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์‚ฌ์ƒ๋“ค์— ํ•˜์Šค์ผˆ์‹ ์ด๋ฆ„์„ ๋ถ™์ธ ๊ฒƒ๋„ ์™€๋Š” ๊ทธ๋‹ค์ง€ ๋งŽ์€ ๊ฒƒ์„ ๋งํ•ด์ฃผ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. โ†ฉ ์ด๊ฒƒ์€ ์•„๋งˆ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์ด ๋ณด๋Š” ๋ชจ๋‚˜๋“œ๋Š”, ์ผ๋ฐ˜์ ์ธ ํŠน์„ฑ์„<NAME>๋Š” computation๋“ค์„ ๋‚˜์—ดํ•˜๋Š” ์ˆ˜๋‹จ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ฒ”์ฃผ๋ก ์—์„œ๋Š” ์—ฌ๋Ÿฌ ๊ตฌ์กฐ๋“ค์˜ ์ปจํ…Œ์ด๋„ˆ๋ผ๋Š” ๊ด€์ ์„ ๊ฐ•์กฐํ•œ๋‹ค. join์€ ์ปจํ…Œ์ด๋„ˆ์™€ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์—ฐ๊ฒฐ๋œ๋‹ค. (์ปจํ…Œ์ด๋„ˆ์˜ ๋‘ ์ธต์„ ํ•˜๋‚˜๋กœ ํ•ฉ์นจ) ํ•˜์ง€๋งŒ (>>=)๋Š” ์ž์—ฐ์Šค๋Ÿฌ์šด ๋‚˜์—ด ์—ฐ์‚ฐ์ด๋‹ค. (๋ฌด์–ธ๊ฐ€๋ฅผ ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค๋ฅธ ๊ฒƒ์— ๊ณต๊ธ‰) โ†ฉ ์ด ๋ฒ•์น™๋“ค์ด ์„ฑ๋ฆฝํ•จ์„ ์ฆ๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด. ๋‹ค์Œ ์ ˆ์—์„œ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. โ†ฉ 5 Curry-Howard ๋™ํ˜• ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/ThE_Curry%E2% 80% 93Howard_isomorphism ์„œ๋ฌธ ํ˜•์‹๋…ผ๋ฆฌํ•™ ๋ฒผ๋ฝ์น˜๊ธฐ ๋ช…์ œ(proposition)๋Š” ํƒ€์ž…์ด๋‹ค โŠฅ์˜ ๋ฌธ์ œ์  ๋…ผ๋ฆฌ ์—ฐ์‚ฐ๋“ค๊ณผ ๊ทธ๊ฒƒ๋“ค์˜ ๋™์น˜ ๋…ผ๋ฆฌ๊ณฑ(conjunction)๊ณผ ๋…ผ๋ฆฌํ•ฉ(disjunction) ๊ฑฐ์ง“์„ฑ(falsity) ๋ถ€์ •(negation) ๊ณต๋ฆฌ ๋…ผ๋ฆฌํ•™(axiomatic logic)๊ณผ ์กฐํ•ฉ ๋Œ€์ˆ˜(combinatory calculus) ๊ณต๋ฆฌ ๋…ผ๋ฆฌํ•™ Combinator calculus ์˜ˆ์‹œ ์ฆ๋ช…๋“ค ์ง๊ด€ ๋…ผ๋ฆฌํ•™(intuitionistic logic) ๋Œ€ ๊ณ ์ „๋…ผ๋ฆฌํ•™ Curry-Howard ๋™ํ˜•(isomorphism)์€ ์ˆ˜ํ•™์˜ ์–ธ๋œป ์—ฐ๊ด€ ์—†์–ด ๋ณด์ด๋Š” ๋‘ ๋ถ„์•ผ์ธ ํƒ€์ž… ์ด๋ก (type theory)๊ณผ ๊ตฌ์กฐ ๋…ผ๋ฆฌํ•™(structural logic)์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋งค๋ ฅ์ ์ธ ๊ด€๊ณ„๋‹ค. ์„œ๋ฌธ Curry-Howard ๋™ํ˜•(์•ž์œผ๋กœ CH๋กœ ์ถ•์•ฝ)์€ ์–ด๋–ค ์ˆ˜ํ•™์  ์ •๋ฆฌ๋ฅผ ์ฆ๋ช…ํ•˜๋ ค๋ฉด ๊ทธ ์ด๋ก ์˜ ๋ณธ์งˆ์„ ๋ฐ˜์˜ํ•˜๋Š” ํƒ€์ž…์„ ๊ตฌ์ถ•ํ•˜๊ณ  ๊ทธ ํƒ€์ž…์„ ๊ฐ€์ง€๋Š” ๊ฐ’์„ ์ฐพ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค๊ณ  ์ฃผ์žฅํ•œ๋‹ค. ์ฒ˜์Œ ๋ณด๋ฉด ์ด๊ฒƒ์€ ๊ต‰์žฅํžˆ ์ด์ƒํ•ด ๋ณด์ธ๋‹ค. ํƒ€์ž…๊ณผ ์ •๋ฆฌ๊ฐ€ ๋ฌด์Šจ ์ƒ๊ด€์ด๋ž€ ๋ง์ธ๊ฐ€? ์•ž์œผ๋กœ ๋ณด๊ฒ ์ง€๋งŒ ์ด ๋‘˜์€ ๋ฐ€์ ‘ํ•˜๊ฒŒ ์—ฐ๊ด€๋˜์–ด ์žˆ๋‹ค. ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์ž ๊น, ์—ฌ๊ธฐ ์ž…๋ฌธ ๋‹จ๋ฝ๋“ค์—์„œ๋Š” error๋‚˜ undefined์ฒ˜๋Ÿผ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์ด โŠฅ์ธ ํ‘œํ˜„์‹๋“ค์˜ ์กด์žฌ๋ฅผ ๋ฌด์‹œํ•œ๋‹ค. ์ด๊ฒƒ๋“ค์˜ ์—ญํ• ์€ ์ค‘๋Œ€ํ•˜์ง€๋งŒ ๋‚˜์ค‘์— ๋”ฐ๋กœ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  unsafeCoerce#์ฒ˜๋Ÿผ ํƒ€์ž… ์‹œ์Šคํ…œ์„ ์šฐํšŒํ•˜๋Š” ํ•จ์ˆ˜๋“ค๋„ ๋ฌด์‹œํ•œ๋‹ค. ํ•˜์Šค์ผˆ์˜ ๊ณ ์ฐจ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋†€๋ž๋„๋ก ๋ณต์žกํ•œ ํƒ€์ž…์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ž„์˜์˜ ํƒ€์ž…์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ์–ด๋–ค ์กฐ๊ฑด ํ•˜์—์„œ ๊ทธ ํƒ€์ž…์„ ๊ฐ€์ง€๋Š” ๊ฐ’์ด ์กด์žฌํ• ๊นŒ? (๊ทธ๋Ÿฐ ํƒ€์ž…์€ ์ ์œ ๋˜์—ˆ๋‹ค๊ณ  inhabited ์นญํ•œ๋‹ค) ์–ธ๋œป ๋ณด๋ฉด 'ํ•ญ์ƒ' ์กด์žฌํ•  ๊ฒƒ ๊ฐ™์ง€๋งŒ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ ํ•˜์—์„œ ๋ฐ”๋กœ ๋ฌด๋„ˆ์ง„๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํƒ€์ž…์ด a -> b์ธ ํ•จ์ˆ˜๋Š” ์—†๋Š”๋ฐ, a ํƒ€์ž…์˜ ๋ฌด์–ธ๊ฐ€๋ฅผ ์™„์ „ํžˆ ๋ณ„๊ฐœ์˜ b ํƒ€์ž…์˜ ๋ฌด์–ธ๊ฐ€๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (ํƒ€์ž… a์™€ b๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๋ฏธ๋ฆฌ ์•Œ๊ณ  ์žˆ์ง€ ์•Š๋Š” ํ•œ. ord :: Char -> Int ๊ฐ™์€ ๋‹จํ˜• ํ•จ์ˆ˜๊ฐ€ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ๋‹ค.) ๋†€๋ž๊ฒŒ๋„ ํƒ€์ž…์ด ์ˆ˜๋ฆฌ๋…ผ๋ฆฌํ•™์˜ ์–ด๋–ค ์ •๋ฆฌ์— ๋Œ€์‘ํ•  ๋•Œ๋งŒ ๊ทธ ํƒ€์ž…์€ ์ ์œ ๋œ๋‹ค๋Š” ์‚ฌ์‹ค์€ ์ด๋ฏธ ๋ฐํ˜€์กŒ๋‹ค. ์ด๋Ÿฐ ๋Œ€์‘ ๊ด€๊ณ„์˜ ๋ณธ์งˆ์€ ๋ฌด์—‡์ผ๊นŒ? a -> b ๊ฐ™์€ ํƒ€์ž…์ด ๋…ผ๋ฆฌํ•™ ๋ฌธ๋งฅ์—์„œ ์–ด๋–ค ์˜๋ฏธ๋ฅผ ๊ฐ€์งˆ๊นŒ? ํ˜•์‹๋…ผ๋ฆฌํ•™ ๋ฒผ๋ฝ์น˜๊ธฐ ํ˜•์‹๋…ผ๋ฆฌํ•™๊ณผ ํƒ€์ž… ์ด๋ก ์˜ ๊ด€๊ณ„๋ฅผ ํƒํ—˜ํ•˜๊ธฐ ์ „์— ๋จผ์ €<NAME>๋…ผ๋ฆฌํ•™์— ๋Œ€ํ•œ ๋ฐฐ๊ฒฝ์ง€์‹์„ ์กฐ๊ธˆ ์•Œ์•„์•ผ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•˜๋Š” ๊ฒƒ์€ ์•„์ฃผ ๊ฐ„๋žตํ•œ ์†Œ๊ฐœ๋‹ค. ๋” ๊ธฐ๋ฐ˜์„ ๋‹ค์ง€๋ ค๋ฉด ์ด ์ฃผ์ œ์— ๋Œ€ํ•œ ์ž…๋ฌธ ๊ต์žฌ๋ฅผ ์ฝ์–ด๋ณผ ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. ์ผ์ƒ ์–ธ์–ด์—์„œ ์šฐ๋ฆฌ๋Š” '๋งŒ์•ฝ... ๊ทธ๋ ‡๋‹ค๋ฉด...' ๊ฐ™์€ ๋ฌธ์žฅ์„ ์ž์ฃผ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด '๋งŒ์•ฝ ์˜ค๋Š˜ ๋‚ ์”จ๊ฐ€ ๋ง‘์œผ๋ฉด ๋„์‹œ๊นŒ์ง€ ๊ฑธ์–ด๊ฐ€์•ผ์ง€.' ๊ฐ™์€ ๊ฒŒ ์žˆ๋‹ค. ์ˆ˜ํ•™์—์„œ๋„ ์ด๋Ÿฐ ์ข…๋ฅ˜์˜ ๋ฌธ์žฅ์ด ๋“ฑ์žฅํ•œ๋‹ค. '๋งŒ์•ฝ x๊ฐ€ ์–‘์ˆ˜๋ผ๋ฉด (์‹ค์ˆ˜) ์ œ๊ณฑ๊ทผ์„ ๊ฐ€์ง„๋‹ค' ๊ฐ™์€ ๊ฒƒ์ด ๊ทธ๋ ‡๋‹ค.<NAME> ๋…ผ๋ฆฌ๋Š” ์˜์–ด ๋œป์„ ๊ทผ์‚ฌํ•˜๋Š” ๋ฌธ์žฅ์„ ๊ณ„์‚ฐ ๊ฐ€๋Šฅ ๋ถˆ๋ฆฌ์–ธ ๋…ผ๋ฆฌ๋กœ ํ‘œํ˜„ํ•˜๋Š” ์ˆ˜๋‹จ์ด๋‹ค. A โ†’ B ('A๊ฐ€ B๋ฅผ ํ•จ์˜ํ•œ๋‹ค'๋ผ๊ณ  ์ฝ์Œ)์€ A๊ฐ€ ์ฐธ์ด๋ฉด ํ•ญ์ƒ B๋„ ์ฐธ์ด๋ผ๋Š” ๋œป์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ๋ฌธ์žฅ์€ 'x๊ฐ€ ์–‘์ˆ˜ โ†’ x๋Š” ์‹ค์ˆ˜ ์ œ๊ณฑ๊ทผ์„ ๊ฐ€์ง'์ด๋ผ๊ณ  ๋‹ค์‹œ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์ด ๋œปํ•˜๋Š” ๋ฐ”๋Š” ๊ทธ ์ˆ˜์˜ ์–‘์ˆ˜์„ฑ(positivity)์ด ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์ข…๋ฅ˜์˜ ๊ทผ์ด ์กด์žฌํ•จ์„ ํ•จ์˜ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์•ž์œผ๋กœ ๋ฌธ์žฅ ์ „์ฒด๋ฅผ ์ง€์นญํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž๋ฅผ ์ž์ฃผ ์“ธ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด W๊ฐ€ '๋‚ ์”จ๊ฐ€ ์ข‹๋‹ค'๋ผ๋Š” ๋ฌธ์žฅ์ด๊ณ  T๊ฐ€ '๋„์‹œ๊นŒ์ง€ ๊ฑธ์–ด๊ฐ„๋‹ค'๋ผ๋Š” ๋ฌธ์žฅ์ด๋ฉด W โ†’ T๋ผ๊ณ  ์“ธ ์ˆ˜ ์žˆ๋‹ค. P โ†’ Q์— ๋Œ€ํ•œ ์ด ์ •์˜๋Š” ๊ฒฐํ•จ์ด ์žˆ๋‹ค. Q๊ฐ€ ํ•ญ์ƒ ์ฐธ์ธ ๋ฌธ์žฅ์ด๋ฉด ์กฐ๊ฑด์ด ๋ฌด์—‡์ด๋“ ('ํƒœ์–‘์€ ๋œจ๊ฒ๋‹ค'์ฒ˜๋Ÿผ) ์ƒ๊ด€์ด ์—†์œผ๋ฉฐ ๊ทธ๋Ÿฌ๋ฉด P๊ฐ€ ๋ฌด์—‡์ธ์ง€๋„ ์ƒ๊ด€์ด ์—†๋‹ค. ์‹ฌ์ง€์–ด P๊ฐ€ ๊ฑฐ์ง“์ธ ๋ฌธ์žฅ์ด์–ด๋„ Q๋Š” ์—ฌ์ „ํžˆ ์ฐธ์ด๋ฉฐ ๋”ฐ๋ผ์„œ P โ†’ Q๋Š” ํ‹€๋ฆด ์ผ์ด ์—†๋‹ค. P๊ฐ€ ๊ฑฐ์ง“์ด๊ณ  Q๊ฐ€ ์ฐธ์ด๋ฉด P โ†’ Q๋Š” ์ฐธ์œผ๋กœ ์ •์˜๋œ๋‹ค. ๋”ฐ๋ผ์„œ โ†’๋Š” ์‹ค์ œ๋กœ๋Š” ์›์ธ-๊ฒฐ๊ณผ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š๋Š”๋‹ค. 'ํ•˜๋Š˜์ด ๋ถ„ํ™์ƒ‰์ด๋‹ค โ†’ ํƒœ์–‘์€ ๋œจ๊ฒ๋‹ค' ๊ฐ™์€ ๊ฒƒ๋„ ์ฐธ์œผ๋กœ ์ •์˜๋œ๋‹ค. ๋ถˆ๋ฆฌ์–ธ ๋…ผ๋ฆฌ ์™ธ์—๋„ ์ด๋Ÿฐ "๋ฌธ์ œ์ "1์„ ๊ณ ์น˜๋ ค ์‹œ๋„ํ•˜๋Š” ๋‹ค๋ฅธ ๋…ผ๋ฆฌ ๋Œ€์ˆ˜๋“ค์ด ์žˆ์œผ๋ฉฐ ํ•˜์Šค์ผˆ์—์„œ๋Š” ๊ทธ ๋ชจ๋“  ๊ฒƒ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋…ผ๋ฆฌ๊ณฑ(conjunction)๊ณผ ๋…ผ๋ฆฌํ•ฉ(disjunction) ์—ญ์‹œ ์ผ์ƒ ์–ธ์–ด์™€ ์ˆ˜ํ•™ ์–‘์ชฝ์—์„œ ํ”ํ•˜๊ฒŒ ๋“ฑ์žฅํ•œ๋‹ค. ์ „์ž๋Š” '๊ทธ๋ฆฌ๊ณ '๋ฅผ ์ˆ˜๋ฐ˜ํ•˜๋Š” ๋ฌธ์žฅ์„ ํ‘œํ˜„ํ•˜๊ณ  ํ›„์ž๋Š” '๋˜๋Š”'์„ ์ˆ˜๋ฐ˜ํ•˜๋Š” ๋ฌธ์žฅ์„ ํ‘œํ˜„ํ•œ๋‹ค. '๋‚˜๋Š” ์ด ์žก์ง€์˜ ์žฌ๊ณ ๊ฐ€ ์žˆ๊ณ  ๋‚ด๊ฒŒ ๋ˆ์ด ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด ์ด ์žก์ง€๋ฅผ ๊ตฌ๋งคํ•  ๊ฒƒ์ด๋‹ค'๋ผ๋Š” ๋ฌธ์žฅ์„ ( โˆง ) B ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ M = '๋‚˜๋Š” ๋ˆ์ด ์ถฉ๋ถ„ํ•˜๋‹ค', S = '์žก์ง€์˜ ์žฌ๊ณ ๊ฐ€ ์žˆ๋‹ค', B = '๋‚˜๋Š” ์ด ์žก์ง€๋ฅผ ๊ตฌ๋งคํ•  ๊ฒƒ์ด๋‹ค'์ด๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ๊ธฐํ˜ธ๋ฅผ ๋‹จ์ˆœํžˆ '๊ทธ๋ฆฌ๊ณ '๋ผ๊ณ  ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. ๋น„์Šทํ•˜๊ฒŒ ๊ธฐํ˜ธ๋Š” '๋˜๋Š”'์ด๋ผ๊ณ  ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ '๋‚˜๋Š” ์ถœ๊ทผํ•˜๊ธฐ ์œ„ํ•ด ๊ฑท๊ฑฐ๋‚˜ ๊ธฐ์ฐจ๋ฅผ ํƒ€๊ฑฐ๋‚˜ ๋‘˜ ๋‹ค ํ•  ๊ฒƒ์ด๋‹ค'๋ผ๋Š” ๋ฌธ์žฅ์€ โˆจ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์—ฌ๊ธฐ์„œ W = '๋‚˜๋Š” ๊ฑธ์–ด์„œ ์ถœ๊ทผํ•œ๋‹ค', T = '๋‚˜๋Š” ๊ธฐ์ฐจ๋ฅผ ํƒ€๊ณ  ์ถœ๊ทผํ•œ๋‹ค'์ด๋‹ค. ์ด๋Ÿฐ ๊ธฐํ˜ธ๋“ค๊ณผ ์•ž์œผ๋กœ ๋‚˜์˜ฌ ๊ธฐํ˜ธ๋“ค์„ ํ™œ์šฉํ•˜๋ฉด ์ž„์˜์˜ ๋ณต์žกํ•œ ๊ธฐํ˜ธ์—ด์„ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ธฐํ˜ธ์—ด์€ ๋‘ ์ข…๋ฅ˜๋กœ ๋‚˜๋‰œ๋‹ค. ํ•˜๋‚˜๋Š” ์ฐธ์ธ ๋ฌธ์žฅ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ ์ •๋ฆฌ theorem๋ผ๊ณ ๋„ ํ•œ๋‹ค. ๊ฑฐ์ง“ ๋ฌธ์žฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ๋“ค์€ ๋น„์ •๋ฆฌnontheorem๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๊ธฐํ˜ธ์—ด์ด ์ •๋ฆฌ์ธ์ง€ ๋น„์ •๋ฆฌ์ธ์ง€๋Š” ๋ฌธ์ž๋“ค์ด ๋ฌด์—‡์„ ๋œปํ•˜๋Š”์ง€์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜๋ผ. ์˜ˆ๋ฅผ ๋“ค์–ด P๊ฐ€ '์ง€๊ธˆ์€ ๋‚ฎ์ด๋‹ค'๋ผ๋Š” ๋ฌธ์žฅ์„, Q๋Š” '์ง€๊ธˆ์€ ๋ฐค์ด๋‹ค'๋ผ๋Š” ๋ฌธ์žฅ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค๋ฉด(๋‚ฎ๊ณผ ๋ฐค์˜ ์ค‘๊ฐ„์ง€๋Œ€๋Š” ๋ฌด์‹œํ•˜์ž) โˆจ๋Š” ์ •๋ฆฌ๋‹ค. ํ•˜์ง€๋งŒ P๊ฐ€ '๋‚˜๋ฌด๋Š” ํŒŒ๋ž—๋‹ค'์ด๊ณ  Q๋Š” '๋ชจ๋“  ์ƒˆ๋Š” ๋‚  ์ˆ˜ ์žˆ๋‹ค'๋ผ๋ฉด ๋น„์ •๋ฆฌ๋‹ค. ๊ธฐํ˜ธ์—ด์ด ์ •๋ฆฌ์ธ์ง€ ์•„๋‹Œ์ง€ ๋ชจ๋ฅผ ๋•Œ๋Š”<NAME> proposition๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค. ๋…ผ๋ฆฌํ•™์˜ ์ฃผ์ œ์—๋Š” ๋”์šฑ ๋ฏธ๋ฌ˜ํ•œ ๊ฒƒ๋“ค์ด ๋งŽ๋‹ค. (๊ฐ€๋ น ์šฐ๋ฆฌ๊ฐ€ '๋„ค๊ฐ€ ์ €๋…์„ ๋จน๋Š”๋‹ค๋ฉด ๋””์ €ํŠธ๋ฅผ ๋ฐ›์„ ๊ฒƒ์ด๋‹ค'๋ผ๊ณ  ๋งํ•˜๋Š” ๊ฒƒ์€ ์‚ฌ์‹ค์€ '์˜ค์ง ์ €๋…์„ ๋จน๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋””์ €ํŠธ๋ฅผ ๋ฐ›์„ ๊ฒƒ์ด๋‹ค'๋ฅผ ๋œปํ•˜๋Š” ๊ฒƒ์ด๋‹ค.) ์ด๋Ÿฐ ์ฃผ์ œ์— ํฅ๋ฏธ๊ฐ€ ๋™ํ•œ๋‹ค๋ฉด ์ด ์ฃผ์ œ๋ฅผ ํญ๋„“๊ฒŒ ๋‹ค๋ฃจ๋Š” ๊ต์žฌ๊ฐ€ ๋งŽ์ด ์žˆ๋‹ค. ๋ช…์ œ(proposition)๋Š” ํƒ€์ž…์ด๋‹ค ๊ทธ๋ž˜์„œ ๊ธฐํ˜ธ๋…ผ๋ฆฌํ•™์—์„œ๋Š” ํƒ€์ž… a -> b์ด ๋ฌด์—‡์„ ๋œปํ•˜๋Š” ๊ฑธ๊นŒ? ํŽธ๋ฆฌํ•˜๊ฒŒ๋„ ๊ทธ ๋œป์€ ๋‹จ์ˆœํžˆ a โ†’ b์ด๋‹ค. ๋ฌผ๋ก  ์ด๊ฒƒ์€ a์™€ b๊ฐ€ ๊ธฐํ˜ธ๋…ผ๋ฆฌํ•™์—์„œ ํ•ด์„ ๊ฐ€๋Šฅํ•œ ํƒ€์ž…์ผ ๋•Œ๋งŒ ๋ง์ด ๋œ๋‹ค. ์ด๊ฒƒ์ด CH์˜ ๋ณธ์งˆ์ด๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด a โ†’ b๊ฐ€ ์ •๋ฆฌ์ผ ํ•„์š”์ถฉ๋ถ„์กฐ๊ฑด์€ a -> b๊ฐ€ ์ ์œ  ํƒ€์ž…(inhabited type)์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์Šค์ผˆ์˜ ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ํ•จ์ˆ˜ ์ค‘ ํ•˜๋‚˜์ธ const๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž. const์˜ ํƒ€์ž…์€ a -> b -> a์ด๋‹ค. ๋…ผ๋ฆฌํ•™์—์„œ๋Š” a โ†’ b โ†’ a๊ฐ€ ๋œ๋‹ค. ์ด๊ฒƒ์€ ์ •๋ฆฌ์ž„์ด ๋ถ„๋ช…ํ•œ๋ฐ, ํƒ€์ž… a -> b -> a์€ const๋ผ๋Š” ๊ฐ’์— ์˜ํ•ด ์ ์œ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. a โ†’ b๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. 'a๊ฐ€ ์ฐธ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•œ๋‹ค๋ฉด b๋Š” ๋ฐ˜๋“œ์‹œ ์ฐธ์ด์–ด์•ผ ํ•œ๋‹ค'. ์ฆ‰ a โ†’ b โ†’ a๋Š” a๊ฐ€ ์ฐธ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ๋” ๋‚˜์•„๊ฐ€ b๊ฐ€ ์ฐธ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•œ๋‹ค๋ฉด a๋ผ๊ณ  ๊ฒฐ๋ก  ๋‚ด๋ฆด ์ˆ˜ ์žˆ์Œ์„ ๋œปํ•œ๋‹ค. ์ด๊ฒƒ์€ ๋ฌผ๋ก  ์ •๋ฆฌ๋‹ค. ์šฐ๋ฆฌ๋Š” a๋ฅผ ๊ฐ€์ •ํ–ˆ์œผ๋ฏ€๋กœ ์šฐ๋ฆฌ์˜ ๊ฐ€์ • ํ•˜์—์„œ a๋Š” ์ฐธ์ด๋‹ค. โŠฅ์˜ ๋ฌธ์ œ์  ์•ž์„œ ํƒ€์ž…์€ ์ ์œ ๋˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ •๋ฆฌ์— ๋Œ€์‘ํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ–ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ•˜์Šค์ผˆ์—์„œ ๋ชจ๋“  ํƒ€์ž…์€ ๊ฐ’ undefined์— ์˜ํ•ด ์ ์œ ๋œ๋‹ค. ๋” ๋‚˜์•„๊ฐ€ ๋ชจ๋“  forall a. a ํƒ€์ž…์— ๋Œ€ํ•ด โŠฅ์˜ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์„ ๊ฐ€์ง€๋Š” ๊ฐ’์€ ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค. ํƒ€์ž… ์ด๋ก ์—์„œ โŠฅ๋Š” ๋…ผ๋ฆฌ ๋‚ด์˜ ๋น„์ผ๊ด€์„ฑ inconsistency์— ๋Œ€์‘ํ•œ๋‹ค. ํ•˜์Šค ์ผˆ ํƒ€์ž…์„ ์ด์šฉํ•˜๋ฉด ์–ด๋–ค ์ •๋ฆฌ๋“  ์ฆ๋ช…ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋ชจ๋“  ํƒ€์ž…์€ ์ ์œ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํ•˜์Šค์ผˆ์˜ ํƒ€์ž… ์‹œ์Šคํ…œ์€ ์‚ฌ์‹ค์€ ๋น„์ผ๊ด€์  ๋…ผ๋ฆฌ ์ฒด๊ณ„์— ๋Œ€์‘ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํ•˜์Šค ์ผˆ ํƒ€์ž… ์‹œ์Šคํ…œ์˜ ํ•œ์ •๋œ ํ•˜์œ„ ์ง‘ํ•ฉ๋งŒ ๋‹ค๋ฃจ๋ฉด, ํŠนํžˆ ๋‹ค ํ˜•(polymorphic type)์„ ๊ธˆ์ง€ํ•˜๋ฉด ์ผ๊ด€๋œ ๋…ผ๋ฆฌ ์ฒด๊ณ„๋ฅผ ์–ป๊ฒŒ ๋˜๋ฉฐ ๊ทธ ์•ˆ์—์„œ ๋ฉ‹์ง„ ์ผ๋“ค์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•ž์œผ๋กœ๋Š” ๊ทธ๋Ÿฐ ํƒ€์ž… ์‹œ์Šคํ…œ์„ ๋‹ค๋ฃจ๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ์ด์ œ CH์˜ ๊ธฐ๋ณธ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์œผ๋‹ˆ ํƒ€์ž…๊ณผ<NAME>์˜ ๊ด€๊ณ„๋ฅผ ์ข€ ๋” ํ’€์–ดํ—ค์น  ์ˆ˜ ์žˆ๋‹ค. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ๋“ค๊ณผ ๊ทธ๊ฒƒ๋“ค์˜ ๋™์น˜ ๊ธฐํ˜ธ ๋…ผ๋ฆฌ์˜ ์ •์ˆ˜๋Š” ์ผ๋ จ์˜<NAME>๋“ค, ๊ฐ€๋ น P์™€ Q, ์ด๊ฒƒ๋“ค์„ Q โ†’ P ๋˜๋Š” โˆจ P Q ์ฒ˜๋Ÿผ ํ•ฉ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค์ด๋‹ค.<NAME>๋“ค์˜ ํ•ฉ์„ฑ์€<NAME>์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. CH์— ๋”ฐ๋ฅด๋ฉด<NAME>๋Š” ํƒ€์ž…์— ๋Œ€์‘ํ•˜๋ฏ€๋กœ ์ด๋Ÿฐ<NAME> ํ•ฉ์„ฑ์ž๋“ค์— ๋Œ€ํ•œ CH ๋Œ€์‘์€ ํƒ€์ž… ์—ฐ์‚ฐ๋“ค, ์ข€ ๋” ์ผ๋ฐ˜์ ์œผ๋กœ ํƒ€์ž… ์ƒ์„ฑ์ž๋ผ๊ณ  ์•Œ๋ ค์ง„ ๊ฒƒ๋“ค์ด๋‹ค. ๊ทธ ์˜ˆ์‹œ๋Š” ์ด๋ฏธ ๋ดค์—ˆ๋‹ค. ๋…ผ๋ฆฌ์—์„œ์˜ ํ•จ์˜ ์—ฐ์‚ฐ์ž โ†’๋Š” ํƒ€์ž… ์ƒ์„ฑ์ž (->)์— ๋Œ€์‘ํ•œ๋‹ค. ์ด๋ฒˆ ์ ˆ์—์„œ๋Š” ๋‹ค๋ฅธ<NAME> ํ•ฉ์„ฑ์ž๋“ค๊ณผ ๊ทธ๊ฒƒ๋“ค์˜ ๋Œ€์‘์„ ์‚ดํŽด๋ณธ๋‹ค. ๋…ผ๋ฆฌ๊ณฑ(conjunction)๊ณผ ๋…ผ๋ฆฌํ•ฉ(disjunction) โˆง A B ๊ฐ€ ์ •๋ฆฌ(theorem)์ด๋ ค๋ฉด A์™€ B๋Š” ๋ฐ˜๋“œ์‹œ ์ •๋ฆฌ์—ฌ์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ โˆง A B ์˜ ์ฆ๋ช…์€ A์™€ B๋ฅผ ๋ชจ๋‘ ์ฆ๋ช…ํ•˜๋Š” ๊ฒƒ์— ๋‹ฌ๋ ธ๋‹ค.<NAME> A๋ฅผ ์ฆ๋ช…ํ•˜๋ ค๋ฉด A ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐœ๊ฒฌํ•ด์•ผ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ A์™€ A๋Š” CH ๋Œ€์‘์ด๋‹ค. ์ด ๊ฒฝ์šฐ ๋‘ ํ•˜์œ„ ๊ฐ’(sub-value)์„ ํฌํ•จํ•˜๋Š” ๊ฐ’์„ ๋ฐœ๊ฒฌํ•ด์•ผ ํ•œ๋‹ค. ํ•˜๋‚˜๋Š” ๊ทธ ํƒ€์ž…์ด A์— ๋Œ€์‘ํ•˜๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜์˜ ํƒ€์ž…์€ B์— ๋Œ€์‘ํ•œ๋‹ค. ์ด๊ฑด ๋งˆ์น˜ ์Œ(pair)์„ ๋งํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋‹ค๋ฆ„์ด ์•„๋‹ˆ๋ผ ๊ธฐํ˜ธ์—ด โˆง A B ๋ฅผ (a, b)๋กœ ํ‘œํ˜„ํ•  ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ a๋Š” A์— b๋Š” B์— ๋Œ€์‘ํ•œ๋‹ค. ๋…ผ๋ฆฌํ•ฉ์€ ๋…ผ๋ฆฌ๊ณฑ์˜ ์ •๋ฐ˜๋Œ€๋‹ค. โˆจ A B ๊ฐ€ ์ •๋ฆฌ๊ฐ€ ๋˜๋ ค๋ฉด A ๋˜๋Š” B๊ฐ€ ์ •๋ฆฌ์—ฌ์•ผ ํ•œ๋‹ค. ์ด๋ฒˆ์—๋Š” A ํƒ€์ž…์˜ ๊ฐ’ ๋˜๋Š” B ํƒ€์ž…์˜ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ๊ฐ’์„ ํƒ์ƒ‰ํ•œ๋‹ค. ์ด๋Š” Either๋‹ค. Either A B๋Š”<NAME> โˆจ A B ์— ๋Œ€์‘ํ•˜๋Š” ํƒ€์ž…์ด๋‹ค. ๊ฑฐ์ง“์„ฑ(falsity) ๋…ผ๋ฆฌ ์ฒด๊ณ„์— ๊ฑฐ์ง“๋ฌธ(false statement)์„ ๋„์ž…ํ•˜๋ฉด ์œ ์šฉํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์ •์˜์— ๋”ฐ๋ฅด๋ฉด ๊ฑฐ์ง“๋ฌธ์€ ์ฆ๋ช…ํ•  ์ˆ˜ ์—†๋Š” ๋ฌธ์žฅ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๋Š” ๊ฒƒ์€ ์ ์œ ๋  ์ˆ˜ ์—†๋Š” ํƒ€์ž…์ด๋‹ค. ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” ๊ทธ๋Ÿฐ ํƒ€์ž…์ด ์กด์žฌํ•˜์ง€ ์•Š์ง€๋งŒ (์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” () ํƒ€์ž…๊ณผ ํ˜ผ๋™ํ•˜์ง€ ๋ง์ž) ๊ทธ๋Ÿฐ ํƒ€์ž…์„ ์ง์ ‘ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. (Haskell2010์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ์ด์ „ ๋ฒ„์ „์˜ GHC์—์„œ๋Š” -XEmptyDataDecls ํ”Œ๋ž˜๊ทธ๋ฅผ ์ผœ์•ผ ํ•œ๋‹ค) data Void ์ƒ์„ฑ์ž๋ฅผ ์ƒ๋žตํ•˜๋Š” ๊ฒƒ์€ Void๊ฐ€ ์ ์œ ๋˜์ง€ ์•Š๋Š” ํƒ€์ž…์ž„์„ ๋œปํ•œ๋‹ค. Void ํƒ€์ž…์€ ์šฐ๋ฆฌ์˜ ๋…ผ๋ฆฌ์—์„œ ๋น„์ •๋ฆฌ์— ๋Œ€์‘ํ•œ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๋ช‡ ๊ฐ€์ง€ ๋”ฐ๋ฆ„ ์ •๋ฆฌ(corollary)๊ฐ€ ์žˆ๋‹ค. ๋ชจ๋“  ํƒ€์ž… A์— ๋Œ€ํ•ด (Void, A)์™€ (A, Void)๋Š” ์ ์œ ๋˜์ง€ ์•Š๋Š” ํƒ€์ž…์ด๋ฉฐ ์ด๋Š” F๊ฐ€ ๋น„์ •๋ฆฌ๋ฉด โˆง F A A F โˆง ๊ฐ€ ๋น„์ •๋ฆฌ๋ผ๋Š” ์‚ฌ์‹ค์— ๋Œ€์‘ํ•œ๋‹ค. ๋ชจ๋“  ํƒ€์ž… A์— ๋Œ€ํ•ด Either Void A์™€ Either A Void๋Š” ๋ณธ์งˆ์ ์œผ๋กœ A์™€ ๋™์ผํ•˜๋‹ค.2 ์ด๋Š” ๋น„์ •๋ฆฌ F์— ๋Œ€ํ•ด โˆจ F A A F โˆจ ๊ฐ€ ์ •๋ฆฌ์ผ ํ•„์š”์ถฉ๋ถ„์กฐ๊ฑด์€ A๊ฐ€ ์ •๋ฆฌ๋ผ๋Š” ์‚ฌ์‹ค์— ๋Œ€์‘ํ•œ๋‹ค. ๋น„์ •๋ฆฌ์— ๋Œ€์‘ํ•˜๋Š” ๋ชจ๋“  ํƒ€์ž…์€ Void๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค. ๋น„์ •๋ฆฌ ํƒ€์ž…์€ ์ ์œ ๋˜์ง€ ์•Š์•„์•ผ ํ•˜๋ฉฐ ๋”ฐ๋ผ์„œ ๊ทธ๋Ÿฐ ๊ฒƒ์„ Void๋กœ ๋Œ€์ฒดํ•ด๋„ ์•„๋ฌด๊ฒƒ๋„ ๋ฐ”๋€Œ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. Void๋Š” ๋ชจ๋“  ๋น„์ •๋ฆฌ ํƒ€์ž…๊ณผ ๋™์น˜๋‹ค. 3 ์ฒซ ๋ฒˆ์งธ ์ ˆ์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด Q๊ฐ€ ์ฐธ์ด๋ฉด ํ•จ์˜ P โ†’ Q๋Š” P์˜ ์ง„์œ„์— ๋ฌด๊ด€ํ•˜๊ฒŒ ์ฐธ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” Void -> a ํƒ€์ž…์˜ ํ•ญ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์•ผ๋งŒ ํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒƒ์ด ์กด์žฌํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ ์„ค๋ช…ํ•˜๊ธฐ๊ฐ€ ๋ณต์žกํ•˜๋‹ค. ๊ทธ ๋‹ต์€ ๊ณตํ•จ์ˆ˜(empty function) ๋‹ค. ํ•จ์ˆ˜ f :: A -> B๋Š” ์ฒซ ๋ฒˆ์งธ ์›์†Œ๊ฐ€ A์˜ ์›์†Œ(์ •์˜์—ญ)์ด๊ณ  ๋‘ ๋ฒˆ์งธ ์›์†Œ๊ฐ€ f์˜ ์ถœ๋ ฅ ์ฆ‰ B์˜ ์›์†Œ(๊ณต์—ญ)์ธ ์Œ๋“ค์˜ (์•„๋งˆ ๋ฌดํ•œ์ผ) ์ง‘ํ•ฉ์œผ๋กœ์„œ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž์—ฐ์ˆ˜์— ๋Œ€ํ•œ ์—ฐ์†ํ•จ์ˆ˜(successor function)๋Š” {(0,1), (1,2), (2,3), ...}์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค. (์™„์ „ํ•˜๊ณ  ์ž˜ ์ •์˜๋œ) ํ•จ์ˆ˜๊ฐ€ ๋˜๋ ค๋ฉด A ํƒ€์ž…์˜ ๊ฐ ํ•ญ a์— ๋Œ€ํ•ด (a, f a)๊ฐ€ ์ •ํ™•ํžˆ ํ•˜๋‚˜๋งŒ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ณตํ•จ์ˆ˜ empty๊ฐ€ ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ณต์ง‘ํ•ฉ์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค๊ณ  ์น˜์ž. ํ•˜์ง€๋งŒ ์ •์˜์—ญ์˜ ๊ฐ ์›์†Œ์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ ์Œ์ด ์žˆ์–ด์•ผ ํ•˜๊ณ , ์šฐ๋ฆฌ์˜ ํ‘œํ˜„๋ฒ•์—๋Š” ์•„๋ฌด ์Œ๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ •์˜์—ญ ํƒ€์ž…๋„ ๋น„์–ด์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰ Void ์—ฌ์•ผ ํ•œ๋‹ค. ์น˜์—ญ์˜ ํƒ€์ž…์€ ์–ด๋–จ๊นŒ? empty๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์น˜์—ญ ํƒ€์ž…์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ฌด๋Ÿฐ ์ œ์•ฝ๋„ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์น˜์—ญ์˜ ํƒ€์ž…์€ ์•„๋ฌด ํƒ€์ž…์ด๋‚˜ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋„ ํ•ฉ๋‹นํ•˜๊ณ  empty :: forall a. Void -> a๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ถˆํ–‰ํžˆ๋„ ์ด๋Ÿฐ ํ•จ์ˆ˜๋Š” ํ•˜์Šค ์ผˆ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์—†๋‹ค. ์ด์ƒ์ ์œผ๋กœ๋Š” ์ด๋Ÿฐ ๊ฒƒ์„ ์›ํ•˜์ง€๋งŒ, empty :: Void -> a ์ž ๊น, ํ•˜์Šค์ผˆ์—์„œ ์ด๊ฒƒ์€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š๋‹ค. ๋‹ค์Œ์ด ๊ทธ๋‚˜๋งˆ ๊ฐ€์žฅ ๋น„์Šทํ•œ ๊ฒƒ์ด๋‹ค. empty :: Void -> a empty _ = undefined ์•„๋‹ˆ๋ฉด ์ด๋Ÿฐ ๊ฒƒ๋„ ์žˆ๋‹ค. empty :: Void -> a empty = empty ๋˜ ๋‹ค๋ฅธ ํ•ฉ๋ฆฌ์ ์ธ ๋ฐฉ๋ฒ•์€ ์ด๋ ‡๊ฒŒ ์“ฐ๋Š” ๊ฒƒ์ด๋‹ค. (EmptyCase ํ™•์žฅ์„ ์“ฐ๋ฉด GHC์—์„œ ์˜ฌ๋ฐ”๋ฅด๋‹ค) empty x = case x of { } ์ด case ๋ฌธ์€ ์™„๋ฒฝํžˆ well-formed์ธ๋ฐ, x์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ํ•จ์ˆ˜์˜ ์šฐ๋ณ€์€ ์ ˆ๋Œ€ ๋„๋‹ฌํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— (์•„๋ฌด๊ฒƒ๋„ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—) ์ด๊ฒƒ์€ ์™„๋ฒฝํžˆ ์•ˆ์ „ํ•˜๋‹ค. ์ด ๋ชจ๋“  ๊ฒƒ์˜ ๊ฒฐ๋ก ์€ P๊ฐ€ ๊ฑฐ์ง“์ด๋ฉด P โ†’ Q๊ฐ€ ์ฐธ์ธ ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Void -> a ๊ฐ€ ์ ์œ ๋œ ํƒ€์ž…์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ๋ถ€์ •(negation) ๋…ผ๋ฆฌ์˜ ยฌ ์—ฐ์‚ฐ์€ ์ •๋ฆฌ์™€ ๋น„์ •๋ฆฌ๋ฅผ ์„œ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. A๊ฐ€ ์ •๋ฆฌ๋ฉด ยฌ A๋Š” ๋น„์ •๋ฆฌ๋‹ค. A๊ฐ€ ๋น„์ •๋ฆฌ๋ฉด ยฌ A๋Š” ์ •๋ฆฌ๋‹ค. ์ด๊ฒƒ์„ ํ•˜์Šค ์ผˆ๋กœ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ• ๊นŒ? ๊ทธ ๋‹ต์€ ๊ต๋ฌ˜ํ•˜๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํƒ€์ž… ๋™์˜์–ด๋ฅผ ์ •์˜ํ•œ๋‹ค. type Not a = a -> Void ํƒ€์ž… A์— ๋Œ€ํ•ด Not A๋Š” ๋‹จ์ง€ A -> void์ด๋‹ค. ์ด๊ฒŒ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š” ๊ฑธ๊นŒ? A๊ฐ€ ์ •๋ฆฌ-ํƒ€์ž…์ด๋ฉด A -> Void๋Š” ์ ์œ ๋˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฐ ํ•จ์ˆ˜๊ฐ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๋ฐฉ๋ฒ•์€ ์—†๋‹ค. ๋ฆฌํ„ด ํƒ€์ž…์ธ Void๋Š” ๊ฐ’์„ ๊ฐ€์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค! (๊ทธ๋Ÿฐ ํ•จ์ˆ˜๋Š” A์˜ ๋ชจ๋“  inhabitant์— ๋Œ€ํ•ด ๊ฐ’์„ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค) ๋ฐ˜๋ฉด A๊ฐ€ ๋น„์ •๋ฆฌ๋ฉด ์•ž์ ˆ์—์„œ ์—ฐ๊ตฌํ–ˆ๋“ฏ์ด A๋Š” Void๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํ•จ์ˆ˜ id :: Void -> Void๋Š” Not A์˜ inhabitant์ด๋ฏ€๋กœ Not A๋Š” ์š”๊ตฌ๋Œ€๋กœ ์ •๋ฆฌ๊ฐ€ ๋œ๋‹ค. (์ด ํ•จ์ˆ˜๋Š” ์•„๋ฌด ๊ฐ’๋„ ์ œ๊ณตํ•  ํ•„์š”๊ฐ€ ์—†๋Š”๋ฐ, ์ •์˜์—ญ์— inhabitant๊ฐ€ ํ•˜๋‚˜๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿผ์—๋„ ์ด๊ฒƒ์€ ํ•จ์ˆ˜๊ฐ€ ๋งž๋‹ค) ๊ณต๋ฆฌ ๋…ผ๋ฆฌํ•™(axiomatic logic)๊ณผ ์กฐํ•ฉ ๋Œ€์ˆ˜(combinatory calculus) ์ง€๊ธˆ๊นŒ์ง€๋Š” ํ•˜์Šค์ผˆ์˜ ํƒ€์ž… ์‹œ์Šคํ…œ์˜ ์•„์ฃผ ๊ธฐ์ดˆ์ ์ธ ํŠน์ง•๋“ค๋งŒ์„ ํ™œ์šฉํ–ˆ๋‹ค. ์‚ฌ์‹ค ์šฐ๋ฆฌ๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€ ์–ธ๊ธ‰ํ•œ ๋…ผ๋ฆฌ์˜ ๋Œ€๋ถ€๋ถ„ ํŠน์ง•์€ ์•„์ฃผ ๊ธฐ์ดˆ์ ์ธ 'ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด'์ธ combinator calculus๋ฅผ ํ†ตํ•ด์„œ๋„ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค. CH๊ฐ€ ์ˆ˜ํ•™์˜ ์ด ๋‘ ์˜์—ญ์„ ์–ผ๋งˆ๋‚˜ ๋ฐ€์ ‘ํ•˜๊ฒŒ ์—ฎ๋Š”์ง€ ์™„์ „ํžˆ ์ดํ•ดํ•˜๋ ค๋ฉด ์šฐ๋ฆฌ์˜<NAME>๋…ผ๋ฆฌ์— ๋Œ€ํ•œ ๋…ผ์˜์™€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์— ๋Œ€ํ•œ ๋…ผ์˜๋ฅผ ๊ณต๋ฆฌํ™”(axiomatise) ํ•ด์•ผ ํ•œ๋‹ค. ๊ณต๋ฆฌ ๋…ผ๋ฆฌํ•™ โ†’ ์—ฐ์‚ฐ์ด ์–ด๋–ป๊ฒŒ ํ–‰๋™ํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ๊ณต๋ฆฌ ๋‘ ๊ฐœ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. (์ง€๊ธˆ๋ถ€ํ„ฐ โ†’๊ฐ€ ์šฐ๊ฒฐํ•ฉ ํ•จ์ˆ˜๋ผ๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ์ฆ‰ A โ†’ B โ†’ C๋Š” A โ†’ (B โ†’ C)๋ฅผ ์˜๋ฏธํ•œ๋‹ค. A โ†’ B โ†’ A (A โ†’ B โ†’ C) โ†’ (A โ†’ B) โ†’ A โ†’ C ์ฒซ ๋ฒˆ์งธ ๊ณต๋ฆฌ๋Š” ๋‘<NAME> A์™€ B๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ A์™€ B ๋ชจ๋‘ ์ฐธ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด A๋Š” ์ฐธ์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ๊ณต๋ฆฌ๋Š” B๊ฐ€ C๋ฅผ ํ•จ์˜ํ•œ๋‹ค๋Š” ๊ฒƒ์„ A๊ฐ€ ํ•จ์˜ํ•˜๋ฉด (๋‹ฌ๋ฆฌ ๋งํ•˜๋ฉด A์™€ B๊ฐ€ ์ฐธ์ผ ๋•Œ ํ•ญ์ƒ C๊ฐ€ ์ฐธ์ด๋ผ๋ฉด) A ์ž์ฒด๋Š” B๋ฅผ ํ•จ์˜ํ•˜๋ฏ€๋กœ, A๊ฐ€ ์ฐธ์ž„์„ ์•Œ๋ฉด C๊ฐ€ ์ฐธ์ด๋ผ๊ณ  ๊ฒฐ๋ก ์„ ๋‚ด๋ฆฌ๊ธฐ์— ์ถฉ๋ถ„ํ•˜๋‹ค. ๋ณต์žกํ•˜๊ฒŒ ๋“ค๋ฆฌ์ง€๋งŒ ์กฐ๊ธˆ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์ƒ์‹์ ์ด๋‹ค. ์—ฌ๋Ÿฌ ์ƒ‰๊น”์˜ ์ƒ์ž ๋ชจ์Œ์ด ์žˆ๋Š”๋ฐ ๊ทธ์ค‘์—๋Š” ๋ฐ”ํ€ด๊ฐ€ ๋‹ฌ๋ฆฐ ๊ฒƒ๋„ ์žˆ๊ณ  ๋šœ๊ป‘์ด ๋‹ฌ๋ฆฐ ๊ฒƒ๋„ ์žˆ๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ณด์ž. ๋ฐ”ํ€ด๊ฐ€ ๋‹ฌ๋ฆฐ ๋นจ๊ฐ„ ์ƒ์ž๋ผ๋ฉด ๋šœ๊ป‘๋„ ์žˆ๋Š”๋ฐ, ๋ชจ๋“  ๋นจ๊ฐ„ ์ƒ์ž๋Š” ๋ฐ”ํ€ด๊ฐ€ ๋‹ฌ๋ ค์žˆ๋‹ค. ์ƒ์ž๋ฅผ ํ•˜๋‚˜ ๊ณ ๋ฅธ๋‹ค. A = '์ด ์ƒ์ž๋Š” ๋นจ๊ฐ„์ƒ‰์ด๋‹ค', B = '์ด ์ƒ์ž๋Š” ๋ฐ”ํ€ด๊ฐ€ ๋‹ฌ๋ ธ๋‹ค', C = '์ด ์ƒ์ž๋Š” ๋šœ๊ป‘์ด ๋‹ฌ๋ ธ๋‹ค'๋ผ๊ณ  ํ•˜๋ฉด ๋‘ ๋ฒˆ์งธ ๋ฒ•์น™์€ ์ด๋ ‡๊ฒŒ ๋œ๋‹ค. A โ†’ B โ†’ C (๋ฐ”ํ€ด๊ฐ€ ๋‹ฌ๋ฆฐ ๋ชจ๋“  ๋นจ๊ฐ„ ์ƒ์ž๋Š” ๋šœ๊ป‘๋„ ๋‹ฌ๋ ธ๋‹ค)์™€ A โ†’ B(๋ชจ๋“  ๋นจ๊ฐ„ ์ƒ์ž๋Š” ๋ฐ”ํ€ด๊ฐ€ ๋‹ฌ๋ ธ๋‹ค)๊ฐ€ ์„ฑ๋ฆฝํ•˜๋ฏ€๋กœ, A(์ƒ์ž๊ฐ€ ๋นจ๊ฐ›๋‹ค) ๋ฉด C(์ƒ์ž์— ๋šœ๊ป‘์ด ๋‹ฌ๋ ธ๋‹ค)๋„ ๋ฐ˜๋“œ์‹œ ์ฐธ์ด๋‹ค. ์—ฌ๊ธฐ์— ๋”ํ•ด ๊ธ์ • ๋…ผ๋ฒ•(modus ponens)์ด๋ผ๋Š” ์ถ”๋ก  ๋ฒ•์น™์„ ํ—ˆ์šฉํ•œ๋‹ค. ๋งŒ์•ฝ A โ†’ B ์ด๋ฉด, ๊ทธ๋ฆฌ๊ณ  A๋ผ๋ฉด, ๊ทธ๋Ÿฌ๋ฏ€๋กœ B์ด๋‹ค. ์ด ๋ฒ•์น™์€ ๊ธฐ์กด ์ •๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์ƒˆ ์ •๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ๊ฐ€๋Šฅ์ผ€ ํ•œ๋‹ค. ์ด๊ฒƒ์˜ ์˜๋ฏธ๋Š” ๊ฝค๋‚˜ ๋ช…๋ฐฑํ•˜๊ณ  ๋ณธ์งˆ์ ์œผ๋กœ โ†’ ๊ฐ€ ๋ฌด์Šจ ๋œป์ธ๊ฐ€์— ๊ด€ํ•œ ์ •์˜๋‹ค. ์ด ์ž‘์€ ๊ธฐ๋ฐ˜์ด ์šฐ๋ฆฌ์˜ ๋…ผ์˜ ๋Œ€๋ถ€๋ถ„์„ ํ‘œํ˜„ํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•˜๋ฉด์„œ๋„ ๊ฐ„๋‹จํ•œ ๋…ผ๋ฆฌ ์ฒด๊ณ„๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋‹ค์Œ์€ ์šฐ๋ฆฌ์˜ ๋…ผ๋ฆฌ ์ฒด๊ณ„์—์„œ ๋ฒ•์น™ A โ†’ A๋ฅผ ์ฆ๋ช…ํ•˜๋Š” ์˜ˆ์‹œ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ •๋ฆฌ์˜ ๋‘ ๊ณต๋ฆฌ๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค. A โ†’ B โ†’ A (A โ†’ B โ†’ C) โ†’ (A โ†’ B) โ†’ A โ†’ C ๋‘ ๋ฒˆ์งธ ๊ณต๋ฆฌ์˜ ์ขŒ๋ณ€์€ ์ฒซ ๋ฒˆ์งธ ๊ณต๋ฆฌ์™€ ๋น„์Šทํ•ด ๋ณด์ธ๋‹ค. ๋‘ ๋ฒˆ์งธ ๊ณต๋ฆฌ๋Š” A โ†’ B โ†’ C ์ž„์„ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด (A โ†’ B) โ†’ A โ†’ C๋ผ๊ณ  ๊ฒฐ๋ก  ๋‚ด๋ฆด ์ˆ˜ ์žˆ์Œ์„ ๋ณด์žฅํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ C๋ฅผ A์™€ ๋™์ผํ•œ<NAME>๋กœ ๋‘๋ฉด, '๋งŒ์•ฝ A โ†’ B โ†’ A ์ด๋ฉด (A โ†’ B) โ†’ A โ†’ A์ด๋‹ค'๊ฐ€ ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ A โ†’ B โ†’ A๋Š” ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋“ฏ์ด ์ฒซ ๋ฒˆ์งธ ๊ณต๋ฆฌ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ (A โ†’ B) โ†’ A โ†’ A๋Š” ์ •๋ฆฌ๋‹ค. ๋˜ ๋‹ค๋ฅธ<NAME> C์— ๋Œ€ํ•ด B๋ฅผ<NAME> C โ†’ A์œผ๋กœ ์„ค์ •ํ•˜๋ฉด '๋งŒ์•ฝ A โ†’ C โ†’ A ์ด๋ฉด A โ†’ A์ด๋‹ค'๊ฐ€ ๋œ๋‹ค. ์ด๋ฒˆ์—๋„ A โ†’ C โ†’ A (์—ญ์‹œ ์ฒซ ๋ฒˆ์งธ ๊ณต๋ฆฌ)์ž„์€ ์•Œ๊ณ  ์žˆ์œผ๋ฏ€๋กœ A โ†’ A ๊ฐ€ ์„ฑ๋ฆฝํ•˜๊ณ  ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋˜ ๋Œ€๋กœ ๋œ๋‹ค. ์œ„ ์˜ˆ์‹œ๋Š” ๊ฐ„๋‹จํ•œ ๊ณต๋ฆฌ๋“ค ๊ทธ๋ฆฌ๊ณ  ๊ธฐ์กด ์ •๋ฆฌ๋กœ๋ถ€ํ„ฐ ์ƒˆ ์ •๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฐ„๋‹จํ•œ ์ˆ˜๋‹จ์ด ์ฃผ์–ด์ง€๋ฉด ๋” ๋ณต์žกํ•œ ์ •๋ฆฌ๋“ค์„ ์œ ๋„ํ•  ์ˆ˜ ์žˆ์Œ์„ ์‹œ์‚ฌํ•œ๋‹ค. ๊ฑฐ๊ธฐ๊นŒ์ง€ ๊ฐ€๊ธฐ์—๋Š” ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆด ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” A โ†’ A ๊ฐ€ ์ •๋ฆฌ๋ผ๋Š” ๋ช…๋ฐฑํ•œ ๋ฌธ์žฅ์„ ์ฆ๋ช…ํ•˜๋Š”๋ฐ๋„ ๋ช‡ ์ค„์— ๊ฑธ์นœ ์ถ”๋ก ์ด ํ•„์š”ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฒฐ๊ตญ์—๋Š” ๋ชฉ์ ์„ ์ด๋ค˜๋‹ค. ์ด๋Ÿฐ<NAME> ํ™”๊ฐ€ ๋งค๋ ฅ์ ์ธ ์ด์œ ๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋งค์šฐ ๊ฐ„๋‹จํ•œ ์ฒด๊ณ„๋ฅผ ์ •์˜ํ–ˆ๊ณ  ๊ทธ๋ž˜์„œ ๊ทธ ์ฒด๊ณ„๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์—ฐ๊ตฌํ•˜๊ธฐ๋„ ์•„์ฃผ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. Combinator calculus ๋žŒ๋‹ค ๋Œ€์ˆ˜๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•œ ๊ธฐ์ €๋กœ๋ถ€ํ„ฐ ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์ •์˜ํ•˜๋Š” ์ˆ˜๋‹จ์ด๋‹ค. ๋งํฌ๋œ ๋ฌธ์„œ๋ฅผ ์ฝ์ง€ ์•Š์•˜๋‹ค๋ฉด ์ตœ์†Œํ•œ untyped ๋Œ€์ˆ˜๋ฅผ ์†Œ๊ฐœํ•˜๋Š” ์ ˆ์€ ์ฝ๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. Here's a refresher in case you're feeling dusty. ๋žŒ๋‹ค ํ•ญ์€ ๋‹ค์Œ ์…‹ ์ค‘ ํ•˜๋‚˜๋‹ค. ๊ฐ’ v ๋žŒ๋‹ค ์ถ”์ƒ x t x t , ์—ฌ๊ธฐ์„œ t๋Š” ๋˜ ๋‹ค๋ฅธ ๋žŒ๋‹ค ํ•ญ ์ ์šฉ ( 1 2 ) ( 1 2 ) , ์—ฌ๊ธฐ์„œ 1 t๋Š” ๋žŒ๋‹ค ํ•ญ ๋ฒ ํƒ€-์†Œ๊ฑฐ(beta-reduction)๋ผ๋Š” ์†Œ๊ฑฐ ๋ฒ•์น™๋„ ํ•˜๋‚˜ ์žˆ๋‹ค. ( ( x t) 2 ) t [ := 2 ] 1 [ := 2 ] , ์—ฌ๊ธฐ์„œ 1 [ := 2 ] 1 [ := 2 ] ๋Š” x์˜ ๋ชจ๋“  free occurrence๋ฅผ ๊ฐ€์ง€๋Š” 1 t์œผ๋กœ ๋Œ€์ฒด๋จ์„ ๋œปํ•œ๋‹ค. (??) ๋žŒ๋‹ค ๋Œ€์ˆ˜ ๋ฌธ์„œ์—์„œ ์–ธ๊ธ‰ํ•˜๋“ฏ์ด ์‹๋ณ„์ž์˜ free occurence๋ผ๋Š” ๊ฐœ๋…์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ๋‚œ๊ด€์ด๋‹ค. (๋ฐ”๋กœ ์–ด๋–ค ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ์œ ๋ž˜์ธ) ๋ฏธ๊ตญ ์ˆ˜ํ•™์ž Haskell Curry๋Š” ๊ทธ๋Ÿฐ ๋‚œ๊ด€ ๋•Œ๋ฌธ์— combinator ๋Œ€์ˆ˜๋ฅผ ๋ฐœ๋ช…ํ–ˆ๋‹ค. ๊ธฐ์ดˆ์ ์ธ combinator ๋Œ€์ˆ˜์˜ ๋ณ€์ข…์ด ๋งŽ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๊ฒƒ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ดํŽด๋ณธ๋‹ค. combinator๋ผ ๋ถ€๋ฅด๋Š” ๊ฒƒ์ด 2๊ฐœ ์žˆ๋‹ค. K๋Š” ๊ฐ’ ๋‘ ๊ฐœ๋ฅผ ์ทจํ•ด์„œ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋žŒ๋‹ค ๋Œ€์ˆ˜์—์„œ๋Š” = x . x ์ด๋‹ค. S๋Š” ์ดํ•ญ ํ•จ์ˆ˜, ๋‹จํ•ญ ํ•จ์ˆ˜, ๊ฐ’ ํ•˜๋‚˜๋ฅผ ์ทจํ•ด์„œ ๊ทธ ๊ฐ’๊ณผ ๊ทธ ๊ฐ’์„ ๋‹จํ•ญ ํ•จ์ˆ˜์— ์ ์šฉํ•œ ๊ฐ’ ์ด๋ ‡๊ฒŒ ๋‘ ๊ฐ’์„ ์ดํ•ญ ํ•จ์ˆ˜์— ์ ์šฉํ•œ๋‹ค. ๋žŒ๋‹ค ๋Œ€์ˆ˜์—์„œ๋Š” = x z x ( z ) ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ•จ์ˆ˜๋Š” const๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ๋” ๋ณต์žกํ•œ๋ฐ ((->) e) ๋ชจ๋‚˜๋“œ์—์„œ์˜ ๋ชจ๋‚˜ ๋”• ํ•จ์ˆ˜ ap์ด๋‹ค. (์ฆ‰ Reader๋‹ค) ์ด ๋‘ combinator๊ฐ€ ๋žŒ๋‹ค ๋Œ€์ˆ˜ ์ „์ฒด๋ฅผ ์œ„ํ•œ ์™„์ „ํ•œ ๊ธฐ์ €๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. ๋ชจ๋“  ๋žŒ๋‹ค ๋Œ€์ˆ˜ ํ”„๋กœ๊ทธ๋žจ์€ ์ด ๋‘ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์‹œ ์ฆ๋ช…๋“ค ์›๋ฌธ ์—†์Œ ์ง๊ด€ ๋…ผ๋ฆฌํ•™(intuitionistic logic) ๋Œ€ ๊ณ ์ „๋…ผ๋ฆฌํ•™ ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์ฆ๋ช…ํ•œ ๋ชจ๋“  ๊ฒฐ๊ณผ๋Š” ์ง๊ด€ ๋…ผ๋ฆฌํ•™์˜ ์ •๋ฆฌ๋“ค์ด๋‹ค. ๊ณ ์ „๋…ผ๋ฆฌํ•™์˜ ๊ธฐ์ดˆ ์ •๋ฆฌ Not Not A -> A๋ฅผ ์ฆ๋ช…ํ•˜๋ ค ํ•˜๋ฉด ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ๋ณด์ž. ์ด๊ฒƒ์€ ((A -> Void) -> Void) -> A์œผ๋กœ ๋ฒˆ์—ญ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” A -> Void) -> Void ํƒ€์ž…์˜ ํ•จ์ˆ˜๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ A ํƒ€์ž…์˜ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ํƒ€์ž… A -> Void๊ฐ€ ์ ์œ ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ์ฆ‰ ํƒ€์ž… A๊ฐ€ ์ ์œ ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด (A -> Void) -> Void ํƒ€์ž…์˜ ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ž„์˜์˜ ์ ์œ ๋œ ํƒ€์ž…์„ ์ทจํ•ด์„œ ๊ทธ ํƒ€์ž…์˜ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ปดํ“จํ„ฐ์—์„œ๋Š” ๊ฐ„๋‹จํ•œ ์ผ์ด๋‹ค. ๊ทธ ํƒ€์ž…์˜ "๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ" ๋˜๋Š” "์ฒซ ๋ฒˆ์งธ" inhabitant๋ฅผ ์ฐพ์œผ๋ฉด ๋œ๋‹ค. ํ‘œ์ค€ ๋žŒ๋‹ค ๋Œ€์ˆ˜ ๋˜๋Š” combinator ๊ธฐ๋ฒ•์—๋Š” ๊ทธ๋Ÿฐ ์ˆ˜๋‹จ์ด ์—†๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๋‘ ๊ธฐ๋ฒ•์œผ๋กœ๋Š” ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋ฅผ ์ฆ๋ช…ํ•  ์ˆ˜ ์—†๊ณ  ๊ทธ ๊ธฐ์ €์˜ ๋…ผ๋ฆฌ๋Š” ๊ณ ์ „์ ์ด ์•„๋‹ˆ๋ผ ์ง๊ด€์ ์ด๋‹ค. ๋Œ€์‹  ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด throw๋ฅผ ํ˜ธ์ถœํ•ด์„œ computation์„ catch๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ณ ์ „์ ์ธ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด์ž. throw ํ•จ์ˆ˜๋Š” ์›๋ž˜ ํ•จ์ˆ˜์˜ ๋ชจ๋“  ๋ฐ˜ํ™˜๊ฐ’์„ ์ทจ์†Œํ•˜๋ฏ€๋กœ A -> Void ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค. ์—ฌ๊ธฐ์„œ A๋Š” ๊ทธ ์ธ์ž์˜ ํƒ€์ž…์ด๋‹ค. catch ํ•จ์ˆ˜๋Š” throw ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ทจํ•˜๊ณ  throw๊ฐ€ ๋ฐœ๋™ํ•˜๋ฉด(์ฆ‰ Void๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด) throw ํ•จ์ˆ˜์˜ ์ธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ catch์˜ ํƒ€์ž…์€ ((A -> Void) -> Void) -> A์ด๋‹ค. ์ด์— ๋Œ€ํ•œ ๋˜ ๋‹ค๋ฅธ ๊ด€์ ์€ ์šฐ๋ฆฌ๊ฐ€ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž โ†’ ๊ฐ€ ์ž์—ฐ์–ด์˜ "๋งŒ์•ฝ... ๊ทธ๋ ‡๋‹ค๋ฉด" ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์šฐ๋ฆฌ์˜ ์ง๊ด€์„ ํฌ์ฐฉํ•˜๋„๋ก ์ •์˜ํ•˜๋ ค ์‹œ๋„ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ฐ€๋ น ์ž์—ฐ์ˆ˜ x์— ๋Œ€ํ•ด "x๋Š” ์ง์ˆ˜" โ†’ "x+1์€ ํ™€์ˆ˜"๊ฐ€ ์ฐธ ๊ฐ™์€ ๋ฌธ์žฅ์„ ์›ํ•œ๋‹ค. ์ฆ‰ ๊ทธ ํ•จ์˜๋Š” x๋ฅผ ์•„๋ฌด ์ž์—ฐ์ˆ˜, ๊ฐ€๋ น 5๋กœ ์น˜ํ™˜ํ•ด๋„ ์„ฑ๋ฆฝํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ "5๋Š” ์ง์ˆ˜"์™€ "6์€ ํ™€์ˆ˜"๋Š” ๋‘˜ ๋‹ค ๊ฑฐ์ง“์ด๋ฏ€๋กœ False โ†’ False๋Š” ์ฐธ์ด์–ด์•ผ ํ•œ๋‹ค. ๋น„์Šทํ•˜๊ฒŒ ๋ชจ๋“  ์ž์—ฐ์ˆ˜ x > 3์— ๋Œ€ํ•ด "x๊ฐ€ ์†Œ์ˆ˜" โ†’ "x+1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค" ๊ฐ™์€ ๋ฌธ์žฅ์„ ๊ณ ๋ คํ•˜๋ฉด False โ†’ True๋กœ ์ฐธ์ด์–ด์•ผ ํ•œ๋‹ค. ๋‹น์—ฐํ•˜์ง€๋งŒ True โ†’ True๋Š” ์ฐธ์ด์–ด์•ผ ํ•˜๊ณ  True โ†’ False๋Š” ๊ฑฐ์ง“์ด์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ x โ†’ y๋Š” x๊ฐ€ ์ฐธ์ด๊ณ  y๊ฐ€ ๊ฑฐ์ง“์ธ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๋ฉด ์„ฑ๋ฆฝํ•œ๋‹ค. โ†ฉ ์—„๋ฐ€ํžˆ๋Š” Either Void A ํƒ€์ž…๊ณผ A ํƒ€์ž…์€ ๋™ํ˜•(isomorphic)์ด๋‹ค. Void ํƒ€์ž…์˜ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜๋Š” ์—†์œผ๋‹ˆ Either Void A ํƒ€์ž…์˜ ๋ชจ๋“  ๊ฐ’์€ Right๊ฐ€ ๋ถ™๋Š” ๊ฐ’์ด์–ด์•ผ ํ•˜๊ณ  ๋”ฐ๋ผ์„œ ๋ณ€ํ™˜ ๊ณผ์ •์—์„œ Right ์ƒ์„ฑ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ฒŒ ๋œ๋‹ค. โ†ฉ ์—ญ์‹œ ์—„๋ฐ€ํ•˜๊ฒŒ ๋งํ•˜๋ฉด Void๋Š” ๋น„์ •๋ฆฌ์ธ ๋ชจ๋“  ํƒ€์ž…๊ณผ ๋™ํ˜•์ด๋‹ค. โ†ฉ ์ด ๋…ผ์˜๋Š” Dan Piponi. "Adventures in Classical Land". The Monad Reader (6)์—์„œ ๊ฐ€์ ธ์˜จ ๊ฒƒ์ด๋‹ค. โ†ฉ 6 Fix์™€ ์žฌ๊ท€ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Fix_and_recursion fix ์†Œ๊ฐœ fix์™€ ๊ณ ์ •์  ์žฌ๊ท€ ํ˜•์‹ ๋žŒ๋‹ค ๋Œ€์ˆ˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ์„œ์˜ fix fix ํ•จ์ˆ˜๋ฅผ ์ฒ˜์Œ ๋ณด๋ฉด ๊ธฐ์ดํ•˜๊ณ  ์“ธ๋ฐ์—†๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. ํ•˜์ง€๋งŒ fix์˜ ์กด์žฌ์—๋Š” ์ด๋ก ์ ์ธ ๊ทผ๊ฑฐ๊ฐ€ ์žˆ๋‹ค. (ํ˜•์‹) ๋žŒ๋‹ค ๋Œ€์ˆ˜์— fix๋ฅผ<NAME>์œผ๋กœ์„œ ๋„์ž…ํ•˜๋ฉด ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. fix ์†Œ๊ฐœ fix๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹จ์ˆœํ•˜๊ฒŒ ์ •์˜๋œ๋‹ค. fix :: (a -> a) -> a fix f = let {x = f x} in x ์กฐ๊ธˆ... ๋งˆ์ˆ  ๊ฐ™์ง€ ์•Š์€๊ฐ€? fix f๋Š” f๋ฅผ ๋ฌดํ•œํžˆ ์ค‘์ฒฉ ์ ์šฉํ•ด์„œ x = f x = f (f x) = f (f (f ( ... )))๊ฐ€ ๋˜์ง€ ์•Š๋Š”๊ฐ€? ์ด๊ฒƒ์€ ์ง€์—ฐ ํ‰๊ฐ€์— ์˜ํ•ด ํ•ด๊ฒฐ๋œ๋‹ค. f๋ฅผ ๋ฌดํ•œํžˆ ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ ํšŒํ”ผํ•  ํ•„์š”์ถฉ๋ถ„์กฐ๊ฑด์€ f๊ฐ€ ๊ฒŒ์œผ๋ฅธ ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ์‹œ๋ฅผ ๋ณด์ž. ์˜ˆ์ œ: fix ์˜ˆ์‹œ๋“ค Prelude> :m Control.Monad.Fix Prelude Control.Monad.Fix> fix (2+) -- Example 1 *** Exception: stack overflow Prelude Control.Monad.Fix> fix (const "hello") -- Example 2 "hello" Prelude Control.Monad.Fix> fix (1:) -- Example 3 [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, ... ๋จผ์ € Control.Monad.Fix ๋ชจ๋“ˆ์„ ๋“ค์—ฌ์™€์„œ fix๋ฅผ ์Šค์ฝ”ํ”„ ์•ˆ์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค. (Data.Function ๋ชจ๋“ˆ๋„ fix๋ฅผ ๋‚ด๋ณด๋‚ธ๋‹ค) ๊ทธ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ์˜ˆ์‹œ๋ฅผ ์‹œ๋„ํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์˜ˆ์‹œ์—์„œ ์‹ค์ œ๋กœ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž. -- fix f = let {x = f x} in x fix (2+) = let {x = 2 + x} in x = let {x = 2 + x} in 2 + x = let {x = 2 + x} in 2 + (2 + x) = let {x = 2 + x} in 2 + (2 + (2 + x)) = let {x = 2 + x} in 2 + (2 + (2 + (2 + x))) = ... ์ฒซ ๋ฒˆ์งธ ์˜ˆ์‹œ๋Š” ์ ๊ทน์  ํ•จ์ˆ˜์ธ (+)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. (+)๋Š” ๋จผ์ € x์˜ ๊ฐ’์„ ์š”๊ตฌํ•œ๋‹ค... x์˜ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋ง์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๊ณ„์‚ฐ์€ ์ ˆ๋Œ€ ๋ฉˆ์ถ”์ง€ ์•Š๋Š”๋‹ค. ๋‘ ๋ฒˆ์งธ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž. fix (const "hello") = let {x = const "hello" x} in x = "hello" ์—ฌ๊ธฐ์„œ๋Š” ์ƒํ™ฉ์ด ์‚ฌ๋ญ‡ ๋‹ค๋ฅด๋‹ค. fix๋ฅผ ํ•œ๋ฒˆ ์ „๊ฐœํ•˜๋ฉด ํ‰๊ฐ€๊ฐ€ ๋๋‚˜๋Š”๋ฐ, const๋Š” ๋‘ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ๋ฌด์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋งˆ์ง€๋ง‰ ์˜ˆ์‹œ์˜ ํ‰๊ฐ€๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด์ง€๋งŒ ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ ์ „๊ฐœํ•  ์ˆ˜ ์žˆ๋‹ค. fix (1:) = let {x = 1 : x} in x = let {x = 1 : x} in 1 : x ์—ฌ๊ธฐ์„œ 1 : x๋Š” ์ด๋ฏธ WNHF์ด๋ฏ€๋กœ((:)๋Š” ๊ฒŒ์œผ๋ฅธ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ์ž) ์ „๊ฐœ๊ฐ€ ๋ฉˆ์ถ˜๋‹ค. ์ด๋Š” ์ˆœํ™˜ ๊ตฌ์กฐ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. ์†Œ๋น„์ž ํ•จ์ˆ˜๊ฐ€ ์ด ๋ฆฌ์ŠคํŠธ์˜ ์ƒˆ ์›์†Œ๋ฅผ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค x์˜ ์ •์˜๋ฅผ ์ฐพ์•„๋ณด๊ฒŒ ๋˜๋Š”๋ฐ ๊ทธ๊ฒƒ์€ ์ด๋ฏธ 1 : x์ž„์ด ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ์š”์ฒญํ•˜๋ฉด ๋์—†์ด ๋งค๋ฒˆ 1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ take 10 (fix (1:))๋Š” 1์„ 10๊ฐœ ๊ฐ€์ง€๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜์ง€๋งŒ GHCi์— fix (1:)์„ ์ž…๋ ฅํ•ด์„œ ๋ชจ๋“  ์›์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๋ ค ํ•˜๋ฉด ๋์—†๋Š” 1์˜ ์ˆ˜์—ด์ด ์ถœ๋ ฅ๋œ๋‹ค. ์ด ๊ฒฝ์šฐ ์‹ค์ œ๋กœ๋Š” show (fix (1:)) = "[" ++ intercalate "," (map show (fix (1:))) ++ "]"์˜ ํ‰๊ฐ€๋ฅผ ์ผ์œผํ‚ค๊ณ  map show (fix (1:))๋Š” ์ ˆ๋Œ€ ์ข…๋ฃŒํ•˜์ง€ ์•Š์ง€๋งŒ ๊ทธ ์ถœ๋ ฅ์„ ์ ์ง„์ ์œผ๋กœ ๋งŒ๋“ค์–ด๋‚ด์–ด ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ "1"์„ ์‚ฐ์ถœํ•œ๋‹ค. "[" ++ intercalate "," (map show (fix (1:))) ++ "]" = "[" ++ intercalate "," (map show (let {x = 1 : x} in x)) ++ "]" = "[" ++ intercalate "," (map show (let {x = 1 : x} in 1 : x)) ++ "]" = "[" ++ "1" ++ "," ++ intercalate "," (map show (let {x = 1 : x} in x)) ++ "]" = "[1, " ++ intercalate "," (map show (let {x = 1 : x} in 1 : x)) ++ "]" = "[1, " ++ "1" ++ "," ++ intercalate "," (map show (let {x = 1 : x} in x)) ++ "]" = "[1,1, " ++ intercalate "," (map show (let {x = 1 : x} in 1 : x)) ++ "]" = "[1,1, " ++ "1" ++ "," ++ intercalate "," (map show (let {x = 1 : x} in x)) ++ "]" = "[1,1,1, " ++ intercalate "," (map show (let {x = 1 : x} in 1 : x)) ++ "]" = ... ์—ฌ๊ธฐ์„œ๋Š” ์ง€์—ฐ ํ‰๊ฐ€๊ฐ€ ์ž‘๋™ํ•˜๊ณ  ์žˆ๋‹ค. ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์ถœ๋ ฅ์„ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์ž…๋ ฅ ๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ์†Œ๋น„ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ์ถœ๋ ฅ์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๋งŒํผ๋งŒ ์†Œ๋น„ํ•˜๋ฉด ๋œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹ค์Œ์€ ์ˆซ์ž์˜ ์ œ๊ณฑ๊ทผ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ๊ทผ์‚ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. -- fix f = let {x = f x} in x fix (\next guess tol val -> if abs(guess^2-val) < tol then guess else next ((guess + val / guess) / 2.0) tol val) 2.0 0.0001 25.0 = (let {next = (\next guess tol val -> if abs(guess^2-val) < tol then guess else next ((guess + val / guess) / 2.0) tol val) next} in next) 2.0 0.0001 25.0 = let {next guess tol val = if abs(guess^2-val) < tol then guess else next ((guess + val / guess) / 2.0) tol val} in next 2.0 0.0001 25.0 = 5.000000000016778 ์—ฐ์Šต๋ฌธ์ œ ๋‹ค์Œ ํ‘œํ˜„์‹๋“ค์€ ์ˆ˜๋ ดํ•œ๋‹ค๋ฉด ๋ฌด์—‡์— ์ˆ˜๋ ดํ•˜๋Š”๊ฐ€? fix ("hello"++) fix (\x -> cycle (1:x)) fix reverse fix id fix (\x -> take 2 $ cycle (1:x)) fix์™€ ๊ณ ์ •์  ํ•จ์ˆ˜ f์˜ ๊ณ ์ •์ ์€ f a == a์ธ ๊ฐ’ a์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 0์€ ํ•จ์ˆ˜ ( * 3)์˜ ๊ณ ์ •์ ์ธ๋ฐ 0 * 3 == 0์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. fix์˜ ์ด๋ฆ„์€ ์—ฌ๊ธฐ์„œ ์œ ๋ž˜ํ•œ๋‹ค. fix๋Š” ํ•จ์ˆ˜์˜ ์ตœ์†Œ ์ •์˜ ๊ณ ์ •์ (least-defined fixed point)์„ ๋ฐœ๊ฒฌํ•œ๋‹ค. ("์ตœ์†Œ ์ •์˜"์˜ ๋œป์€ ๊ณง ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค) ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์ˆ˜๋ ดํ•˜๋Š” ๋‘ ์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ ์ด๋ฅผ ์‰ฝ๊ฒŒ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค. const "hello" "hello" = "hello" (1:) [1,1, ..] = [1,1, ...] ๊ทธ๋ฆฌ๊ณ  2+x == x์ธ ์ˆซ์ž x ๊ฐ™์€ ๊ฒƒ์€ ์—†๊ธฐ ๋•Œ๋ฌธ์— fix (2+)๋Š” ๋ฐœ์‚ฐํ•œ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์œ„ ์—ฐ์Šต๋ฌธ์ œ์—์„œ fix f๊ฐ€ ์ˆ˜๋ ดํ•œ๋‹ค๊ณ  ํŒ๋‹จํ•œ ๊ฐ ํ•จ์ˆ˜ f์— ๋Œ€ํ•ด fix f๊ฐ€ ๊ณ ์ •์ ์„ ์ฐพ๋Š”๋‹ค๋Š” ๊ฒƒ์„ ๊ฒ€์ฆํ•˜๋ผ. ์‚ฌ์‹ค fix๊ฐ€ ๊ณ ์ •์ ์„ ์ฐพ๋Š”๋‹ค๋Š” ๊ฒƒ์€ fix์˜ ์ •์˜๋ฅผ ๋ณด๋ฉด ๋ช…๋ฐฑํ•˜๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํ•  ์ผ์€ fix์˜ ๋“ฑ์‹์„ ๋‹ค๋ฅด๊ฒŒ ์“ฐ๋Š” ๊ฒƒ๋ฟ์ด๋‹ค. f (fix f) = fix f ์ด๋Š” ๋‹ค๋ฆ„ ์•„๋‹Œ ๊ณ ์ •์ ์˜ ์ •์˜๋‹ค. ๋”ฐ๋ผ์„œ fix๊ฐ€ ํ•ญ์ƒ ๊ณ ์ •์ ์„ ์ฐพ์•„์•ผ ํ•  ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ฐ€๋”์€ fix๊ฐ€ ๊ทธ๋Ÿฌ๋Š” ๊ฒƒ์„ ์‹คํŒจํ•˜๋Š”๋ฐ, ๋ฐœ์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์„ ์กฐ๊ธˆ ๋„์ž…ํ•˜๋ฉด ์ด ์„ฑ์งˆ์„ ๊ณ ์น  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“  ํ•˜์Šค ์ผˆ ํƒ€์ž…์€ bottom์ด๋ผ ๋ถ€๋ฅด๊ณ  โŠฅ๋กœ ํ‘œ๊ธฐํ•˜๋Š” ํŠน๋ณ„ํ•œ ๊ฐ’์„ ํฌํ•จํ•œ๋‹ค. ์ฆ‰ Int ํƒ€์ž…์€ 1, 2, 3 ๊ฐ™์€ ๊ฐ’๋ฟ๋งŒ ์•„๋‹ˆ๋ผ โŠฅ๋„ ํฌํ•จํ•œ๋‹ค. ๋ฐœ์‚ฐํ•˜๋Š” ๊ณ„์‚ฐ์€ โŠฅ์˜ ๊ฐ’์œผ๋กœ ํ‘œ๊ธฐ๋œ๋‹ค. ์ฆ‰ fix (2+) = โŠฅ์ด๋‹ค. ํŠน๋ณ„ํ•œ ๊ฐ’์ธ undefined๋„ โŠฅ์— ์˜ํ•ด ํ‘œ๊ธฐ๋œ๋‹ค. ์ด์ œ fix๊ฐ€ ์–ด๋–ป๊ฒŒ (2+) ๊ฐ™์€ ํ•จ์ˆ˜์˜ ๊ณ ์ •์ ์„ ์ฐพ๋Š”์ง€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์‹œ: (2+)์˜ ๊ณ ์ •์  Prelude> (2+) undefined *** Exception: Prelude.undefined (2+)์— undefined(์ฆ‰ โŠฅ)์„ ๋„ฃ์œผ๋ฉด undefined๋ฅผ ๋Œ๋ ค๋ฐ›๋Š”๋‹ค. ๋”ฐ๋ผ์„œ โŠฅ๋Š” (2+)์˜ ๊ณ ์ •์ ์ด๋‹ค. (2+)์˜ ๊ฒฝ์šฐ์—๋Š” โŠฅ๊ฐ€ ์œ ์ผํ•œ ๊ณ ์ •์ ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ fix f๊ฐ€ ๋ฐœ์‚ฐํ•ด๋„ ์—ฌ๋Ÿฌ ๊ณ ์ •์ ์„ ๊ฐ€์ง€๋Š” ๊ทธ๋Ÿฐ ํ•จ์ˆ˜ f๋“ค์ด ์žˆ๋‹ค. fix (*3)์€ ๋ฐœ์‚ฐํ•˜์ง€๋งŒ ์•ž์„œ ๋ดค๋“ฏ์ด 0์€ ์ด ํ•จ์ˆ˜์˜ ๊ณ ์ •์ ์ด๋‹ค. ์—ฌ๊ธฐ์„œ "์ตœ์†Œ ์ •์˜"๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ํƒ€์ž…๋“ค์€ definedness๋ผ๋Š” ๋ถ€๋ถ„ ์ˆœ์„œ๋ฅผ ๊ฐ€์ง„๋‹ค. ์–ด๋–ค ํƒ€์ž…์ด๋“  โŠฅ๋Š” ์ตœ์†Œ ์ •์˜ ๊ฐ’์ด๋‹ค. (๊ทธ๋ž˜์„œ "bottom"์ด๋ผ ๋ถ€๋ฅด๋Š” ๊ฒƒ์ด๋‹ค) Int ๊ฐ™์€ ๋‹จ์ˆœํ•œ ํƒ€์ž…์˜ ๊ฒฝ์šฐ ๋ถ€๋ถ„ ์ˆœ์„œ๋ฅผ ๊ฐ€์ง€๋Š” ์œ ์ผํ•œ ์Œ๋“ค์€ โŠฅ โ‰ค 1, โŠฅ โ‰ค 2 ๋“ฑ์ด๋‹ค. bottom์ด ์•„๋‹Œ ์ž„์˜์˜ Int ๊ฐ’ m, n์— ๋Œ€ํ•ด m โ‰ค n๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค. ๋น„์Šทํ•œ ๋…ผ์ง€๊ฐ€ Bool์ด๋‚˜ () ๊ฐ™์€ ๊ธฐํƒ€ ๋‹จ์ˆœ ํƒ€์ž…๋“ค์—๋„ ์ ์šฉ๋œ๋‹ค. ๋ฆฌ์ŠคํŠธ๋‚˜ Maybe์ฒ˜๋Ÿผ "๊ณ„์ธต์ ์ธ" ๊ฐ’๋“ค์€ ์ƒํ™ฉ์ด ๋” ๋ณต์žกํ•˜๋ฉฐ ์ด์— ๊ด€ํ•ด์„œ๋Š” ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์—์„œ ๋‹ค๋ฃฌ๋‹ค. โŠฅ๊ฐ€ ๋ชจ๋“  ํƒ€์ž…์˜ ์ตœ์†Œ ์ •์˜ ๊ฐ’์ด๊ณ  fix๋Š” ์ตœ์†Œ ์ •์˜ ๊ณ ์ •์ ์„ ๋ฐœ๊ฒฌํ•˜๋ฏ€๋กœ f โŠฅ = โŠฅ๋ผ๋ฉด fix f = โŠฅ์ด๊ณ  ๊ทธ ์—ญ๋„ ์ฐธ์ด๋‹ค. ํ‘œ๊ธฐ ์˜๋ฏธ๋ก  ํŽ˜์ด์ง€๋ฅผ ์ฝ์—ˆ๋‹ค๋ฉด ์ด๊ฒƒ์ด ์—„๊ฒฉํ•œ ํ•จ์ˆ˜๋ฅผ ์œ„ํ•œ ๊ธฐ์ค€์ž„์„ ์•Œ์•„์ฑ˜์„ ๊ฒƒ์ด๋‹ค. fix f๊ฐ€ ๋ฐœ์‚ฐํ•  ํ•„์š”์ถฉ๋ถ„์กฐ๊ฑด์€ f๊ฐ€ ์—„๊ฒฉํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์žฌ๊ท€ ์ด๋ฏธ fix์— ๊ด€ํ•œ ์˜ˆ์ œ๋“ค์„ ๋ณธ ์ ์ด ์žˆ๋‹ค๋ฉด ์•„๋งˆ fix์™€ ์žฌ๊ท€์— ๊ด€ํ•œ ์˜ˆ์ œ์˜€์„ ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ์€ ๊ณ ์ „์ ์ธ ์˜ˆ์ œ๋‹ค. ์˜ˆ์‹œ: fix๋ฅผ ์ด์šฉํ•œ ์žฌ๊ท€ ํ‘œํ˜„ Prelude> let fact n = if n == 0 then 1 else n * fact (n-1) in fact 5 120 Prelude> fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5 120 ์—ฌ๊ธฐ์„œ๋Š” fix๋ฅผ ์‚ฌ์šฉํ•ด ํŒฉํ† ๋ฆฌ์–ผ ํ•จ์ˆ˜๋ฅผ "ํ‘œํ˜„"ํ–ˆ๋‹ค. fact์˜ ๋‘ ๋ฒˆ์งธ ์ •์˜๋Š” (fix๋ฅผ ์–ธ์–ด<NAME> ์ทจ๊ธ‰ํ•˜๋ฉด) ์žฌ๊ท€๋ฅผ ์ „ํ˜€ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ.<NAME> ๋žŒ๋‹ค ๋Œ€์ˆ˜ ๊ฐ™์€ ์–ธ์–ด์—์„œ๋Š” ์ด๊ฒƒ์ด ์žฌ๊ท€๊ฐ€ ์•„๋‹ˆ๋ฉฐ fix๋ฅผ ๋„์ž…ํ•ด ์ด๋Ÿฐ ์‹์œผ๋กœ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ์ถ”๊ฐ€์ ์ธ ์˜ˆ์ œ๋“ค์ด๋‹ค. ์˜ˆ์‹œ: ์ถ”๊ฐ€์ ์ธ fix ์˜ˆ์ œ๋“ค Prelude> fix (\rec f l -> if null l then [] else f (head l) : rec f (tail l)) (+1) [1.. 3] [2,3,4] Prelude> map (fix (\rec n -> if n == 1 || n == 2 then 1 else rec (n-1) + rec (n-2))) [1.. 10] [1,1,2,3,5,8,13,21,34,55] ์ด๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๊ฑธ๊นŒ? fact ํ•จ์ˆ˜๋ฅผ ํ‘œ๊ธฐ์˜ ๊ด€์ ์—์„œ ์ ‘๊ทผํ•ด ๋ณด์ž. ๊ฐ„๊ฒฐํ•จ์„ ์œ„ํ•ด ์ด๋ ‡๊ฒŒ ์ •์˜ํ•œ๋‹ค. fact' rec n = if n == 0 then 1 else n * rec (n-1) ์ด๊ฒƒ์€ ์ต๋ช… ํ•จ์ˆ˜์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•œ ๊ฒƒ์„ ๋นผ๋ฉด ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์˜ ํ•จ์ˆ˜์™€ ๊ฐ™๋‹ค. ์ด์ œ fix fact' 5๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. fix๋Š” fact'์˜ ๊ณ ์ •์  ์ฆ‰ f == fact' f์ธ ํ•จ์ˆ˜ f๋ฅผ ์ฐพ์„ ๊ฒƒ์ด๋‹ค. ์ด๊ฒŒ ๋ฌด์Šจ ๋œป์ธ์ง€ ์ „๊ฐœํ•ด ๋ณด์ž. f = fact' f = \n -> if n == 0 then 1 else n * f (n-1) ์—ฌ๊ธฐ์„œ ํ•œ ๊ฒƒ์€ fact'์˜ ์ •์˜์—์„œ rec์„ f๋กœ ์น˜ํ™˜ํ•œ ๊ฒƒ๋ฟ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฒƒ์€ ๋ฐ”๋กœ ํŒฉํ† ๋ฆฌ์–ผ ํ•จ์ˆ˜์˜ ์žฌ๊ท€์  ์ •์˜์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. fix๋Š” fact' ๊ทธ ์ž์ฒด๋ฅผ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๊ณต๊ธ‰ํ•ด์„œ ๊ณ ์ฐจ ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค. ๋ช…๋ นํ˜• ๊ด€์ ์—์„œ ์ ‘๊ทผํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. fix fact'์˜ ์ •์˜๋ฅผ ์‹ค์ œ๋กœ ์ „๊ฐœํ•ด ๋ณด์ž. fix fact' = fact' (fix fact') = (\rec n -> if n == 0 then 1 else n * rec (n-1)) (fix fact') = \n -> if n == 0 then 1 else n * fix fact' (n-1) = \n -> if n == 0 then 1 else n * fact' (fix fact') (n-1) = \n -> if n == 0 then 1 else n * (\rec n' -> if n' == 0 then 1 else n' * rec (n'-1)) (fix fact') (n-1) = \n -> if n == 0 then 1 else n * (if n-1 == 0 then 1 else (n-1) * fix fact' (n-2)) = \n -> if n == 0 then 1 else n * (if n-1 == 0 then 1 else (n-1) * (if n-2 == 0 then 1 else (n-2) * fix fact' (n-3))) = ... fix๋ฅผ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— fact'์˜ ์ •์˜๋ฅผ "ํ’€์–ดํ—ค์น " ์ˆ˜ ์žˆ๋‹ค. else ์ ˆ์„ ๋งˆ์ฃผ์น  ๋•Œ๋งˆ๋‹ค ํ‰๊ฐ€ ๊ทœ์น™ fix fact' = fact' (fix fact')์„ ํ†ตํ•ด fact'์˜ ๋˜ ๋‹ค๋ฅธ ์‚ฌ๋ณธ์„ ๋งŒ๋“ค์–ด๋‚ด๋ฉด ์žฌ๊ท€ ์‚ฌ์Šฌ์—์„œ ๊ทธ๋‹ค์Œ ํ˜ธ์ถœ๋กœ์„œ ๊ธฐ๋Šฅํ•œ๋‹ค. ๊ฒฐ๊ตญ then ์ ˆ์— ๋„๋‹ฌํ•ด์„œ ์ด ์—ฐ์‡„์˜ ๋์— ๋„๋‹ฌํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋‹ค๋ฅธ ๋‘ ์˜ˆ์‹œ๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ „๊ฐœํ•˜๋ผ. ํ”ผ๋ณด๋‚˜์น˜ ์˜ˆ์‹œ๋Š” ์ข…์ด๊ฐ€ ๋งŽ์ด ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค. filter์™€ foldr์˜ ๋น„์žฌ๊ท€์  ๋ฒ„์ „์„ ์ž‘์„ฑํ•˜๋ผ. ํ˜•์‹ ๋žŒ๋‹ค ๋Œ€์ˆ˜ ์ด๋ฒˆ ์ ˆ์—์„œ๋Š” ์•ž์„  ์ ˆ์—์„œ ๋ช‡ ์ฐจ๋ก€ ์–ธ๊ธ‰ํ–ˆ๋˜ ์‚ฌํ•ญ์ธ, fix๊ฐ€<NAME> ๋žŒ๋‹ค ๋Œ€์ˆ˜์—์„œ ์–ด๋–ป๊ฒŒ ์žฌ๊ท€๋ฅผ ํ‘œํ˜„ํ•˜๋Š”์ง€๋ฅผ ์ž์„ธํžˆ ์•Œ์•„๋ณธ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด<NAME> ๋žŒ๋‹ค ๋Œ€์ˆ˜๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ๋‹ค. ๋žŒ๋‹ค ๋Œ€์ˆ˜์—๋Š” let ์ ˆ๋„ ์ตœ์ƒ์œ„ ๋ฐ”์ธ๋”ฉ๋„ ์—†๋‹ค. ๋ชจ๋“  ํ”„๋กœ๊ทธ๋žจ์€ ๋žŒ๋‹ค ์ถ”์ƒํ™”, ์ ์šฉ, ๋ฆฌํ„ฐ๋Ÿด์˜ ๋‹จ์ˆœ ํŠธ๋ฆฌ๋‹ค. ์ด์ œ fact ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. ์ž์—ฐ์ˆ˜๋ฅผ ์œ„ํ•œ Nat์ด๋ผ๋Š” ํƒ€์ž…์„ ๊ฐ€์ •ํ•˜๋ฉด ์ด๋ ‡๊ฒŒ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ฮป n:Nat. if iszero n then 1 else n * <blank> (n-1) ๋ฌธ์ œ๋Š” <blank>๋ฅผ ์–ด๋–ป๊ฒŒ ์ฑ„์šธ ๊ฒƒ์ธ๊ฐ€๋‹ค. ์šฐ๋ฆฌ์˜ ํ•จ์ˆ˜์—๋Š” ์ด๋ฆ„์ด ์—†์œผ๋ฏ€๋กœ ์žฌ๊ท€ ํ˜ธ์ถœํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค. ํ•ญ์— ์ด๋ฆ„์„ ๋ฌถ๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์€ ๋žŒ๋‹ค ์ถ”์ƒํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•œ๋ฒˆ ํ•ด๋ณด์ž. (ฮป f:Nat โ†’ Nat. ฮป n:Nat. if iszero n then 1 else n * f (n-1)) (ฮป m:Nat. if iszero m then 1 else m * <blank> (m-1)) ์ด๋ ‡๊ฒŒ ์ „๊ฐœ๋œ๋‹ค. ฮป n:Nat. if iszero n then 1 else n * (if iszero n-1 then 1 else (n-1) * <blank> (n-2)) ์—ฌ์ „ํžˆ <blank>๊ฐ€ ๋‚จ์•„์žˆ๋‹ค. ํ•œ ๊ณ„์ธต์„ ๋” ์ถ”๊ฐ€ํ•ด ๋ณด์ž. (ฮป f:Nat โ†’ Nat. ฮป n:Nat. if iszero n then 1 else n * f (n-1) ((ฮป g:Nat โ†’ Nat. ฮป m:Nat. if iszero m then 1 else m * g (m-1)) (ฮป p:Nat. if iszero p then 1 else p * <blank> (p-1)))) -> ฮป n:Nat. if iszero n then 1 else n * (if iszero n-1 then 1 else (n-1) * (if iszero n-2 then 1 else (n-2) * <blank> (n-3))) ์ด๋ฆ„์„ ๋ช‡ ๊ณ„์ธต์„ ์ถ”๊ฐ€ํ•ด๋„ ์ด <blank>๋ฅผ ์˜์›ํžˆ ์ œ๊ฑฐํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด ๋ช…๋ฐฑํ•˜๋‹ค. fix๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•œ ์ ˆ๋Œ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. fix๋Š” ๊ฐœ์ฒด๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ์šฐ๋ฆฌ๋Š” ์ด ๊ฐœ์ฒด๋ฅผ ํ†ตํ•ด ์žฌ๊ท€๋ฅผ ํ•œ ์ธต ํ’€์–ดํ—ค์น˜๊ณ ๋„ ๊ธฐ์กด์— ์žˆ๋˜ ๊ฒƒ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. fix (ฮป f:Nat โ†’ Nat. ฮป n:Nat. if iszero n then 1 else n * f (n-1)) ์ด๊ฒƒ์€<NAME> ๋žŒ๋‹ค ๋Œ€์ˆ˜์— fix๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ์–ป์€ ์™„๋ฒฝํ•œ ํŒฉํ† ๋ฆฌ์–ผ ํ•จ์ˆ˜๋‹ค. fix๋Š”<NAME> ๋žŒ๋‹ค ๋Œ€์ˆ˜์˜ ๋ฌธ๋งฅ์„ ๋ฒ—์–ด๋‚˜๋ฉด ์ข€ ๋” ํฅ๋ฏธ๋กœ์šด ์ฃผ์ œ๋‹ค. ์–ธ์–ด์— fix๋ฅผ ๋„์ž…ํ•˜๋ฉด ๋ชจ๋“  ํƒ€์ž…์€ ์ ์œ ๋˜๋Š”๋ฐ(inhabited), ๊ตฌ์ฒด์ ์ธ ํƒ€์ž… T์— ๋Œ€ํ•ด ๋‹ค์Œ ํ‘œํ˜„์‹์ด ํƒ€์ž… T๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. fix (ฮป x:T. x) ํ•˜์Šค์ผˆ์—์„œ ์ด๊ฒƒ์€ fix id์ด๋ฉฐ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์ ์œผ๋กœ โŠฅ์ด๋‹ค. ๋”ฐ๋ผ์„œ fix๋ฅผ<NAME> ๋žŒ๋‹ค ๋Œ€์ˆ˜์— ๋„์ž…ํ•˜๋ฉด "๋ชจ๋“  well-typed term์€ ๊ฐ’์œผ๋กœ ํ™˜์›๋œ๋‹ค"๋ผ๋Š” ์„ฑ์งˆ์„ ์žƒ๊ฒŒ ๋œ๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ์„œ์˜ fix ํ•˜์Šค์ผˆ์—์„œ fix ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. fix๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. newtype Fix f = Fix (f (Fix f)) ๋˜๋Š” RankNTypes ํ™•์žฅ์„ ์ด์šฉํ•œ๋‹ค. newtype Mu f = Mu (forall a. (f a -> a) -> a) data Nu f = forall a. Nu a (a -> f a) Mu์™€ Nu๋Š” fold, unfold, refold๋ฅผ ์ผ๋ฐ˜ํ™”ํ•˜๋Š” ๋ฐ ์œ ์šฉํ•˜๋‹ค. fold :: (f a -> a) -> Mu f -> a fold g (Mu f) = f g unfold :: (a -> f a) -> a -> Nu f unfold f x = Nu x f refold :: (a -> f a) -> (g a -> a) -> Mu f -> Nu g refold f g = unfold g. fold f Mu์™€ Nu๋Š” Fix์˜ ์ œํ•œ๋œ ๋ฒ„์ „์ด๋‹ค. Mu๋Š” ๊ท€๋‚ฉ์  ๋น„๋ฌดํ•œ ์ž๋ฃŒ(inductive noninfinite data)๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉ๋˜๊ณ  Nu๋Š” ๊ณต๊ท€๋‚ฉ์  ๋ฌดํ•œ ์ž๋ฃŒ(coinductive infinite data)๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. newtype Stream a = Stream (Nu ((,) a)) -- exists b. (b, b -> (a, b)) newtype Void a = Void (Mu ((,) a)) -- forall b. ((a, b) -> b) -> b fix point ํ•จ์ˆ˜์™€ ๋‹ฌ๋ฆฌ fix point ํƒ€์ž…์€ bottom์œผ๋กœ ํ–ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ์—์„œ Bot์€ ์™„๋ฒฝํ•˜๊ฒŒ ์ •์˜๋œ๋‹ค. Bot์€ ์œ ๋‹› ํƒ€์ž… ()์™€ ๋™์น˜๋‹ค. newtype Id a = Id a newtype Bot = Bot (Fix Id) -- equals newtype Bot=Bot Bot -- There is only one allowable term. Bot $ Bot $ Bot $ Bot .., Fix ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๋ชจ๋“  ์ข…๋ฅ˜์˜ ์žฌ๊ท€๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ์ด๋Ÿฐ ์ผ๋ฐ˜์ ์ด์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๊ฒฝ์šฐ data Node a = Two a a | Three a a a data FingerTree a = U a | Up (FingerTree (Node a)) ์ด๊ฒƒ์„ Fix๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•˜๊ธฐ๋Š” ์‰ฝ์ง€ ์•Š๋‹ค. 4 ํ•˜์Šค ์ผˆ ์„ฑ๋Šฅ ํ•˜์Šค ์ผˆ ์„ฑ๋Šฅ ์ž…๋ฌธ ์„ฑ๋Šฅ ์˜ˆ์‹œ๋“ค ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ ์ง€์—ฐ์„ฑ Time and space profiling Strictness (์›๋ฌธ ๋ฏธ์™„์„ฑ) ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„ (์›๋ฌธ ๋ฏธ์™„์„ฑ) ์ž๋ฃŒ๊ตฌ์กฐ (์›๋ฌธ ์—†์Œ) Parallelism (์›๋ฌธ ์—†์Œ) 1 ์ž…๋ฌธ ์›๋ฌธ: http://en.wikibooks.org/wiki/Haskell/Performance_introduction ์‹คํ–‰ ๋ชจํ˜• ์ง€์—ฐ ํ‰๊ฐ€ ์ž…๋ฌธ ์˜ˆ์ œ: ์ ‘๊ธฐ(fold) ์‹œ๊ฐ„ ๊ณต๊ฐ„ ์ € ์ˆ˜์ค€ ์˜ค๋ฒ„ํ—ค๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ž๋ฃŒ๊ตฌ์กฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฃŒ๊ตฌ์กฐ ๋ณ‘๋ ฌ์„ฑParallelism ์‹คํ–‰ ๋ชจํ˜• ์ง€์—ฐ ํ‰๊ฐ€ ์ž…๋ฌธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์ž‘๋™ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋งŒ์ด ์•„๋‹ˆ๋ผ ์ปดํ“จํ„ฐ์—์„œ ์ ์€ ๋ฉ”๋ชจ๋ฆฌ์™€ ์‹คํ–‰ ์‹œ๊ฐ„์„ ์š”๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์— ๊ด€ํ•œ ๊ฒƒ์ด๊ธฐ๋„ ํ•˜๋‹ค. ์‹œ๊ฐ„๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์„ ๋ช…๋ นํ˜• ์–ธ์–ด๋‚˜ LISP, ML ๊ฐ™์€ ์ ๊ทน์ ์ธ(strict) ํ•จ์ˆ˜ํ˜• ์–ธ์–ด์—์„œ๋Š” ๋น„๊ต์  ์ง๊ด€์ ์œผ๋กœ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ํ•˜์Šค์ผˆ์—์„œ๋Š” ์ƒํ™ฉ์ด ๋‹ค๋ฅด๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ํ‘œํ˜„์‹์€ ํ•„์š”์— ๋”ฐ๋ผ ํ‰๊ฐ€๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ํ‘œํ˜„์‹์€ head (map (2 *) [1.. 10]) ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‰๊ฐ€๋œ๋‹ค. โ‡’ head (map (2 *) (1 : [2 .. 10])) ([1 .. 10]) โ‡’ head (2 * 1 : map (2 *) [2 .. 10]) (map) โ‡’ 2 * 1 (head) โ‡’ 2 (*) head ํ•จ์ˆ˜๋Š” ์˜ค์ง ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋งŒ ์š”๊ตฌํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์ธ (map (2 *) [2.. 10])๋Š” ๊ฒฐ์ฝ” ํ‰๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด ์ „๋žต์€ ํ•„์š”ํ•œ ๋งŒํผ๋งŒ ์ˆ˜ํ–‰ํ•œ๋‹ค ํ•ด์„œ ์ง€์—ฐ ํ‰๊ฐ€๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. [graph reduction] ์žฅ์—์„œ ์ƒ์ˆ ํ•˜๊ฒ ๋‹ค. ์ง€์—ฐ ํ‰๊ฐ€๊ฐ€ ํ•˜์Šค์ผˆ์„ ๊ตฌํ˜„ํ•  ๋•Œ ํ”ํžˆ ์ฑ„ํƒ๋˜๋Š” ๊ธฐ๋ฒ•์ด๊ธด ํ•˜์ง€๋งŒ ์–ธ์–ด ํ‘œ์ค€์€ ํ•˜์Šค์ผˆ์ด ํŠน์ • ์‹คํ–‰ ๋ชจํ˜•์— ๊ตญํ•œ๋˜์ง€ ์•Š์œผ๋ฉฐ ์ ๊ทน์ ์ด์ง€ ์•Š์€ ํ‘œ๊ธฐ์ƒ ์˜๋ฏธ๋ก ์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค๊ณ  ๋ชป ๋ฐ•์„ ๋ฟ์ด๋‹ค. ์ผ์ธ์ž ํ•จ์ˆ˜ f๊ฐ€ ์ ๊ทน์ ์ด๋ผ๋Š” ๊ฒƒ์€, ์ด ํ•จ์ˆ˜์˜ ์ธ์ž๋ฅผ ํ‰๊ฐ€ํ•  ๋•Œ ๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ๋Œ๊ฒŒ ๋  ๊ฒฝ์šฐ ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚ค์ง€ ์•Š๊ณ , ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋œป์ด๋‹ค. ๋ฌดํ•œ ๋ฃจํ”„์˜ "๊ฒฐ๊ณผ"๋ฅผ โŠฅ๋กœ ํ‘œ๊ธฐํ•œ๋‹ค๋ฉด ์ ๊ทน์„ฑ์˜ ์ •์˜๋Š” ์ด๋ ‡๋‹ค. ํ•จ์ˆ˜ f์— ๋Œ€ํ•ด f โŠฅ = โŠฅ์ด๋ฉด f๋Š” ์ ๊ทน์ ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์˜์›ํžˆ ๋ฃจํ”„๋ฅผ ๋„๋Š” ์ˆ˜์— 1์„ ๋”ํ•˜๋Š” ๊ฒƒ๋„ ์˜์›ํžˆ ๋ฃจํ”„๋ฅผ ๋Œ๊ธฐ ๋•Œ๋ฌธ์— โŠฅ + 1 = โŠฅ์ด๊ณ  ๋ง์…ˆ ํ•จ์ˆ˜ (+1)์€ ์ ๊ทน์ ์ด๋‹ค. head ํ•จ์ˆ˜๋„ ์ ๊ทน์ ์ธ๋ฐ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋Š” ๋ฆฌ์ŠคํŠธ ์ž์ฒด๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์œผ๋ฉด ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ฒซ ์›์†Œ๋Š” ์ž˜ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉด์„œ ๋ฆฌ์ŠคํŠธ์˜ ๋‚˜๋จธ์ง€๋Š” ์•ˆ ๋˜์–ด์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋˜๊ณ  head (x : โŠฅ) = x ์ด ๋“ฑ์‹์ด ๋œปํ•˜๋Š” ๋ฐ”๋Š” head๊ฐ€ ๋‚˜๋จธ์ง€ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ‰๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฉฐ ๊ทธ๋ ‡๊ฒŒ ํ•  ๊ฒฝ์šฐ ์—ญ์‹œ ๋ฃจํ”„๋ฅผ ๋Œ๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฌํ•œ ์ˆœ์ˆ˜ํžˆ ๋Œ€์ˆ˜์ ์ธ ์ ๊ทน์„ฑ ์„ฑ์งˆ์ด ์‹คํ–‰ ์‹œ๊ฐ„๊ณผ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ถ”๋ก ํ•˜๋Š” ๋ฐ ํฐ ๋„์›€์ด ๋œ๋‹ค. LISP๋‚˜ ML ๊ฐ™์€ ์ ๊ทน์  ์–ธ์–ด์—์„œ๋Š” ํ•ญ์ƒ head (x : โŠฅ) = x์ธ ๋ฐ˜๋ฉด ํ•˜์Šค์ผˆ์€ "๋น„์ ๊ทน์ "์ด์–ด์„œ ์œ„์˜ ์„ฑ์งˆ์„ ๊ฐ–๋Š” head๋‚˜ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ฒŒ๋Š” (const 1) โŠฅ = 1 ๊ฐ™์€ ๋น„์ ๊ทน์  ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. Prelude์— ์ •์˜๋œ ์ƒ์ˆ˜ undefined์˜ ๋„์›€์œผ๋กœ ์ด ์ ๊ทน์„ฑ์ด๋ผ๋Š” ๊ฒƒ์„ ๋Œ€ํ™”์‹์œผ๋กœ ํƒ๊ตฌํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. > head (1 : undefined) > head undefined *** Exception: Prelude.undefined ์ ๊ทน์„ฑ๊ณผ โŠฅ๋Š” ์ด์–ด์ง€๋Š” ์žฅ๋“ค์—์„œ๋„ ๊ณ„์† ์“ฐ์ผ ๊ฒƒ์ด๋‹ค. [graph reduction]์—์„œ๋Š” ๋ณด๋‹ค ์ž…๋ฌธ ๊ฒฉ์˜ ์˜ˆ์ œ๋“ค์„ ์ œ์‹œํ•˜๊ณ  [denotational semantic]์—์„œ๋Š” denotational ๊ด€์ ์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณธ๋‹ค. ์˜ˆ์ œ: ์ ‘๊ธฐ(fold) ์ง€์—ฐ ํ‰๊ฐ€๋ฅผ ๋ง›๋ณด๋Š” ์ตœ์„ ์˜ ๊ธธ์€ ์˜ˆ์ œ๋ฅผ ๊ณต๋ถ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ foldr๊ณผ ๊ทธ ๋ณ€์ข…๋“ค์˜ ๋ชจ๋ฒ”์ ์ธ ์šฉ๋ก€๋“ค์„ ๋ช‡ ๊ฐœ ์‚ดํŽด๋ณผ ๊ฒƒ์ด๋‹ค. ํ”„๋กœ๊ทธ๋žจ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„์™€ ๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์—ฐ๊ตฌํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๊ธฐ์ดˆ ์ง€์‹์€ [์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„] ์žฅ์—์„œ ๊ฐœ๊ด„ํ•œ๋‹ค. ์‹œ๊ฐ„ ์–ด๋–ค ์ˆซ์ž๊ฐ€ ์†Œ์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•˜๋Š” ๋‹ค์Œ์˜ isPrime ํ•จ์ˆ˜๋ฅผ ๋ณด์ž. isPrime n = not $ any (`divides` n) [2.. n-1] d `divides` n = n `mod` d == 0 any p = or. map p or = foldr (||) False ํ•˜์Šค ์ผˆ Prelude์˜ ๋ณด์กฐ ํ•จ์ˆ˜ any๋Š” ๋ฆฌ์ŠคํŠธ์— ์–ด๋–ค ์„ฑ์งˆ p๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์›์†Œ๊ฐ€ ์ ์–ด๋„ ํ•˜๋‚˜ ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. isPrime์˜ ๊ฒฝ์šฐ ๊ทธ ์„ฑ์งˆ์€ "n์˜ ์•ฝ์ˆ˜"๋‹ค. isPrime์ด ํ‘œํ˜„์‹์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์˜ ์–‘์€ ๋ฌผ๋ก  ์ถ•์†Œ ๋‹จ๊ณ„์˜ ๊ฐœ์ˆ˜๋ฅผ ํ†ตํ•ด ์ธก์ •๋œ๋‹ค. n์ด ์†Œ์ˆ˜๋ผ๋ฉด ์œ„์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ 2์—์„œ n-1๊นŒ์ง€์˜ ์ˆซ์ž ๋ฆฌ์ŠคํŠธ ์ „์ฒด๋ฅผ ๊ฒ€์‚ฌํ•˜๊ณ  ๋”ฐ๋ผ์„œ O(n) ์ถ•์†Œ๋ผ๋Š” ์ตœ์•…์˜ ์‹คํ–‰ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. ํ•˜์ง€๋งŒ n์ด ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋ชจ๋“  ์ˆ˜์— ๊ฑธ์ณ ๋ฃจํ”„๋ฅผ ๋Œ์ง€ ์•Š๊ณ  ์•ฝ์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ฐพ์ž๋งˆ์ž ๋ฉˆ์ถฐ์„œ n์ด ํ•ฉ์„ฑ์ˆ˜๋ผ๊ณ  ๋ณด๊ณ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ง€์—ฐ ํ‰๊ฐ€์˜ ์ฆ๊ฑฐ์›€์€ ์ด ๋™์ž‘์ด ์ด๋ฏธ ๋…ผ๋ฆฌํ•ฉ ||์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค! isPrime 42 โ‡’ not $ any (`divides` 42) [2.. 41] โ‡’ not ( or (map (`divides` 42) [2.. 41] ) ) โ‡’ not ( or ((42 `mod` 2 == 0) : map (`divides` 42) [3.. 41]) ) โ‡’ not ( (42 `mod` 2 == 0) || or (map (`divides` 42) [3.. 41]) ) โ‡’ not ( True || or (map (`divides` 42) [3.. 41]) ) โ‡’ not True โ‡’ False ์ด ํ•จ์ˆ˜๋Š” 42๊ฐ€ ์ง์ˆ˜๋ž€ ๊ฑธ ๋ณด์ž๋งˆ์ž False๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ||๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž๊ฐ€ True๋กœ ๊ฒฐ๋ก ๋‚˜๋ฉด ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ์ณ๋‹ค๋ณด์ง€๋„ ์•Š๋Š”๋‹ค. ์ฆ‰ ๋‹ค์Œ์˜ ์ ๊ทน์„ฑ์„ ์–ป๊ฒŒ ๋œ๋‹ค. True || โŠฅ = True ๋ฌผ๋ก  ์œ„์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์†์ˆ˜ ๋ฃจํ”„๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ง€์—ฐ ํ‰๊ฐ€์˜ ํ•ต์‹ฌ์€ ์šฐ๋ฆฌ๊ฐ€ ํ‘œ์ค€ foldr์„ ํ™œ์šฉํ•˜์—ฌ ํˆฌ๋ช…ํ•œ ๋ฐฉ์‹์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต์‹ํ™”ํ•˜๊ณ ๋„ ์—ฌ์ „ํžˆ ์กฐ๊ธฐ ํƒˆ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ฌ๋ฆฌ ๋ณด๋ฉด ์ง€์—ฐ ํ‰๊ฐ€๋Š” ๊ณ ์† ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ชจ๋“ˆํ™” ๋ฐฉ์‹์œผ๋กœ ๊ณต์‹ํ™”ํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ๊ฒƒ์ด๋‹ค. ๊ทธ ๊ทน์ ์ธ ์˜ˆ๊ฐ€ ๋ฌดํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํšจ์œจ์ ์œผ๋กœ ์ƒ์„ฑ & ๊ฐ€์ง€์น˜๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ชจ๋“ˆํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด ์™ธ์—๋„ ์ง€์—ฐ ํ‰๊ฐ€์™€ ๊ด€๋ จ๋œ ์˜๋ฆฌํ•œ ๊ธฐ๋ฒ•๋“ค์„ [์ง€์—ฐ์„ฑ] ์žฅ์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ์‹คํ–‰ ์‹œ๊ฐ„์„ ๋ถ„์„ํ•œ๋‹ต์‹œ๊ณ  ์‹œ์‹œ์ฝœ์ฝœํ•œ ๊ทธ๋ž˜ํ”„ ์ถ•์†Œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์€ ํ˜„์‹ค์ ์ด์ง€๋„, ํ•„์š”์น˜๋„ ์•Š๋‹ค. ๋น„์  ๊ทน์„ฑ์ด๋‚˜ ์ง€์—ฐ ํ‰๊ฐ€๊ฐ€ ์ •์—ด์ ์ธ ํ‰๊ฐ€๋ณด๋‹ค ํ•ญ์ƒ ์ ์€ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นœ๋‹ค๋Š” ์‚ฌ์‹ค์ฒ˜๋Ÿผ ์ง€๋ฆ„๊ธธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๊ฒƒ๋“ค์€ [๊ทธ๋ž˜ํ”„ ์ถ•์†Œ] ์žฅ์—์„œ ์†Œ๊ฐœํ•œ๋‹ค. ๊ณต๊ฐ„ ์‹คํ–‰ ์‹œ๊ฐ„์ด ์ถ•์†Œ ๋‹จ๊ณ„ ๊ฐœ์ˆ˜์— ์˜ํ•ด ๋ชจํ˜•ํ™”๋œ๋‹ค๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์€ ํ‰๊ฐ€ ์ค‘ ํ‘œํ˜„์‹์˜ ํฌ๊ธฐ์— ์˜ํ•ด ๋ชจํ˜•ํ™”๋œ๋‹ค. ๋ถˆํ–‰ํžˆ๋„, ??? ์ž์„ธํ•œ ๊ฒƒ์€ [๊ทธ๋ž˜ํ”„ ์ถ•์†Œ]์—์„œ. ์ผ๋‹จ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ณต๊ฐ„ ๋ฌธ์ œ์˜ ๋ชจ๋ฒ”์ ์ธ ์‚ฌ๋ก€๋ฅผ ์†Œ๊ฐœํ•˜๊ฒ ๋‹ค. Unfortunately, it's harder to predict and deviating from the normal course lazy evaluation by more strictness can ameliorate it. More details in Graph reduction. Here, we will present the prototypical example for unexpected space behavior. ๋ง‰๋Œ€ํ•œ ๊ฐœ์ˆ˜์˜ ์ •์ˆ˜๋ฅผ ๋ฌด์‹ํ•˜๊ฒŒ ๋”ํ•˜๋ฉด > foldr (+) 0 [1.. 1000000] *** Exception: stack overflow ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋ญ์ง€? ๊ทธ ํ‰๊ฐ€ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. foldr (+) 0 [1.. 1000000] โ‡’ 1+(foldr (+) 0 [2.. 1000000]) โ‡’ 1+(2+(foldr (+) 0 [3.. 1000000])) โ‡’ 1+(2+(3+(foldr (+) 0 [4.. 1000000])) ์ด๋ ‡๊ฒŒ ๊ณ„์†๋œ๋‹ค. ํ‘œํ˜„์‹์ด ์ ์  ์ปค์ง€๋ฉฐ ๋” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ด ๊ฒฝ์šฐ foldr์˜ ์žฌ๊ท€ ํ˜ธ์ถœ ํ›„ ๋ณด๋ฅ˜๋œ ๋ง์…ˆ๋“ค์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์Šคํƒ์— ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น๋œ๋‹ค. ์–ด๋Š ์‹œ์ ์—๋Š” ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ตœ๋Œ€ ๊ฐ€์šฉํ•œ ์Šคํƒ ํฌ๊ธฐ๋ฅผ ์ดˆ๊ณผํ•ด "์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ" ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚จ๋‹ค. ๊ทธ๊ฒŒ ์Šคํƒ์ด๋“  ํžˆํ”„์ด๋“  ๋ช…์‹ฌํ•  ๊ฒƒ์€ ํ‘œํ˜„์‹์˜ ํฌ๊ธฐ๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์‘ํ•˜๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ foldr (+) 0 [1.. n]์˜ ํ‰๊ฐ€์— O(n) ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ +๊ฐ€ ๊ฒฐํ•ฉ์„ฑ์ด๋ผ๋Š” ๊ฒƒ์— ์ฐฉ์•ˆํ•ด ์ง€๊ธˆ๊นŒ์ง€์˜ ์ˆซ์ž๋“ค์˜ ํ•ฉ์„ ๋ˆ„์ ํ•ด์„œ ์ด ์ผ์„ O(1) ๊ณต๊ฐ„์—์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. (์–ด๋–ค ๋…์ž๋“ค์€ ํ•จ์ˆ˜๋ฅผ ๊ผฌ๋ฆฌ ์žฌ๊ท€ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค๋Š” ๋ง์ž„์„ ๋ˆˆ์น˜์ฑ˜์„ ๊ฒƒ์ด๋‹ค) foldl์ด ๋ฐ”๋กœ ๊ทธ๋Ÿฐ ์ผ์„ ํ•œ๋‹ค. foldl (+) 0 [1.. n] โ‡’ foldl (+) (0+1) [2.. n] โ‡’ foldl (+) ((0+1)+2) [3.. n] โ‡’ foldl (+) (((0+1)+2)+3) [4.. n] ํ•˜์ง€๋งŒ ๋” ๋”์ฐํ•˜๊ฒŒ๋„ ๋ˆ„์  ํ•˜๋น„ ๋” ์ด์ƒ ์ถ•์†Œ๋˜์ง€ ์•Š๋Š”๋‹ค! ์ด ํ‘œํ˜„์‹์€ ๋ฆฌ์ŠคํŠธ์˜ ๋์— ๋‹ค๋‹ค๋ฅผ ๋•Œ๊นŒ์ง€ ํžˆํ”„ ์•ˆ์—์„œ ์ž๋ผ๋‚  ๊ฒƒ์ด๋‹ค. โ‡’ ... โ‡’ foldl (+) ((((0+1)+2)+3)+...) [] โ‡’ ((((0+1)+2)+3)+...) ๊ทธ๋ฆฌ๊ณ  ์ด ํ‰๊ฐ€๋˜์ง€ ์•Š์€ ๊ฑฐ๋Œ€ํ•œ ํ•ฉ์˜ ์ถ•์†Œ ์—ญ์‹œ ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ๋กœ ์‹คํŒจํ•  ๊ฒƒ์ด๋‹ค. (์ฆ‰ ๋‹จ์ง€ ๋ˆ„์  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋„์ž…ํ•œ๋‹ค๊ณ  ํ•จ์ˆ˜๊ฐ€ ๊ผฌ๋ฆฌ ์žฌ๊ท€๋ฅผ ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค) ๋ฌธ์ œ๋Š” ์ด ํ‰๊ฐ€๋˜์ง€ ์•Š์€ ํ•ฉ์ด ๋‹จ์ผ ์ •์ˆ˜์—๊ฒŒ๋Š” ๊ณผ๋„ํ•˜๊ฒŒ ํฐ ํ‘œํ˜„์ด๊ณ  ์ ๊ทน์ ์œผ๋กœ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ €๋ ดํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. foldl'์ด ๋ฐ”๋กœ ๊ทธ๋Ÿฐ ์ผ์„ ํ•œ๋‹ค. foldl' f z [] = z foldl' f z (x:xs) = z `seq` foldl' f (f z x) xs ์—ฌ๊ธฐ์„œ a `seq` b๋ฅผ ํ‰๊ฐ€ํ•˜๋ฉด a๋Š” b์˜ ์ถ•์†Œ์— ์•ž์„œ weak head normal form์œผ๋กœ ์ถ•์†Œ๋œ๋‹ค. ํ•ฉ์€ ์ด์ œ ์ƒ์ˆ˜ ๊ณต๊ฐ„ ๋‚ด์—์„œ ์ง„ํ–‰๋œ๋‹ค. foldl' (+) 0 [1.. n] โ‡’ foldl' (+) (0+1) [2.. n] โ‡’ foldl' (+) (1+2) [3.. n] โ‡’ foldl' (+) (3+3) [4.. n] โ‡’ foldl' (+) (6+4) [5.. n] โ‡’ ... ๋ณด๋‹ค ์ž์„ธํ•œ ์‚ฌํ•ญ๊ณผ ์ถ”๊ฐ€ ์˜ˆ์‹œ๋“ค์€ [๊ทธ๋ž˜ํ”„ ์ถ•์†Œ] ์žฅ์„ ๋ณผ ๊ฒƒ. ์ € ์ˆ˜์ค€ ์˜ค๋ฒ„ํ—ค๋“œ ์ ๊ทน์  ํ‰๊ฐ€์™€ ๋น„๊ตํ•ด์„œ ์ง€์—ฐ ํ‰๊ฐ€๋Š” ์ƒ๋‹นํ•œ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์œ ๋ฐœํ•˜๋Š”๋ฐ, ์ •์ˆ˜๋‚˜ ๋ฌธ์ž์กฐ์ฐจ๋„ โŠฅ์ผ ์ˆ˜ ์žˆ๊ธฐ์— ํฌ์ธํ„ฐ๋กœ์„œ ์ €์žฅ๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 1๋ฐ”์ดํŠธ ๋ฌธ์ž๋“ค์˜ ๋ฐฐ์—ด์€ ๊ฐ™์€ ๊ธธ์ด์˜ String = [Char]๋ณด๋‹ค ๋ช‡ ๋ฐฐ๋Š” ์••์ถ•์ ์ด๋‹ค. ์ ๊ทน์„ฑ ํ‘œ๊ธฐ, unboxed ํƒ€์ž…, ์ž๋™ ์ ๊ทน์„ฑ ๋ถ„์„์œผ๋กœ ์ด ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ์ € ์ˆ˜์ค€ ์„ธ๋ถ€์‚ฌํ•ญ์— ๋Œ€ํ•ด์„œ๋Š” ํ•˜์Šค ์ผˆ ์œ„ํ‚ค๊ฐ€ ์ ๊ฒฉ์ด๋‹ค.[1] ์œ„ํ‚ค ์ฑ…์—์„œ๋Š” ํ˜„์žฌ๋กœ์ฌ ์ด๊ฒƒ๋“ค์„ ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ž๋ฃŒ๊ตฌ์กฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด ์œ„ํ‚ค ์ฑ…์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๊ด€ํ•œ ํฌ๊ด„์ ์ธ ์ฑ…์€ ์•„๋‹ˆ์ง€๋งŒ ํšจ์œจ์  ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ์„ ์œ„ํ•œ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋งŒ์˜ ๊ธฐ๋ฒ•๋“ค์€ ๋งŽ๋‹ค. [์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„] ์žฅ์—์„œ๋Š” ๋น…-O ํ‘œ๊ธฐ๋ฅผ ๊ฐœ๊ด„ํ•˜๊ณ  ์‹ค์ œ ์‚ฌ๋ก€๋ฅผ ๋ช‡ ๊ฐœ ์ œ์‹œํ•œ๋‹ค. [์ง€์—ฐ์„ฑ] ์žฅ์—์„œ๋Š” ์ง€์—ฐ ํ‰๊ฐ€๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ชจ๋“ˆํ™” ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์— ์ดˆ์ ์„ ๋งž์ถ˜๋‹ค. program derivation๊ณผ equational reasoning์˜ ๊ณตํ†ต ๊ด€์‹ฌ์‚ฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋“ฑ์‹๋“ค์„ ์‘์šฉ ๋ฐ ์ฆ๋ช…ํ•˜์—ฌ ๋ช…์„ธ๋กœ๋ถ€ํ„ฐ ํšจ์œจ์ ์ธ ํ”„๋กœ๊ทธ๋žจ์„ ๋„์ถœํ•˜๋Š” ๊ฒƒ์ด๋‹ค. map f. reverse = reverse . map f filter p. map f = map f. filter (p . f) ์ด ๊ณผ์ œ๋Š” ๋‹ค์ด๋‚ด๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์ˆœ์ˆ˜ ๋Œ€์ˆ˜์  ์ ‘๊ทผ๋ฒ•์œผ๋กœ ๋ถ€์ƒํ•˜๋Š”๋ฐ, ๋‚˜์ค‘์— ์ ์ ˆํ•œ ์ด๋ฆ„์œผ๋กœ ์†Œ๊ฐœ๋  ๊ฒƒ์ด๋‹ค. This quest has given rise to a gemstone, namely a purely algebraic approach to dynamic programming which will be introduced in some chapter with a good name. fusion ๋˜๋Š” deforestration์œผ๋กœ ์•Œ๋ ค์ง„ ๋˜ ๋‹ค๋ฅธ ๋“ฑ์‹ ๊ธฐ๋ฒ•์€ ํ•จ์ˆ˜ ํ•ฉ์„ฑ์—์„œ ์ค‘๊ฐ„ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ๋‹ค. ๊ฐ€๋ น ๋‹ค์Œ ์ฝ”๋“œ์˜ ์ขŒ๋ณ€์— ์žˆ๋Š” ํ•ฉ์„ฑ์€ map f. map g = map (f . g) ์ค‘๊ฐ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์ถ• ๋ฐ ํŒŒ๊ดดํ•˜์ง€๋งŒ ์šฐ๋ณ€์€ ํ•œ ๋ฆฌ์ŠคํŠธ์— ๊ฑธ์นœ ๋‹จ์ผ ํŒจ์Šค๋‹ค. ์—ฌ๊ธฐ์„œ ์ผ๋ฐ˜์ ์ธ ์ฃผ์ œ๋Š” ์ƒ์„ฑ์ž-ํŒŒ๊ดด์ž ์ง์„ ์ด๋ ‡๊ฒŒ ์ ‘ํ•ฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. case (Just y) of { Just x -> f x; Nothing -> ...; } = f y ์ด์— ๊ด€ํ•ด์„œ๋Š” ๋‹ค์Œ์— ์•Œ๋งž์€ ๋ช…์นญ์œผ๋กœ ์ƒ์„ธํžˆ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ์ž๋ฃŒ๊ตฌ์กฐ ์˜ฌ๋ฐ”๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๊ณต์˜ ์—ด์‡ ๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ํ”ํ•˜๊ณ  ๋†€๋ž๋„๋ก ๊ธธ์–ด์งˆ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ณ ์† ์ ‘๊ทผ ๋ฐ ๊ฐฑ์‹ ์ด ๊ฐ€๋Šฅํ•œ ๊ณ ์ „์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ๋ผ๊ธฐ๋ณด๋‹จ ๋ฃจํ”„์˜ ๊ตฌ์ฒดํ™”๋œ ํ•œ ํ˜•ํƒœ๋‹ค. ๋งŽ์€ ์–ธ์–ด๊ฐ€ ์ด๋Ÿฐ ์œ ์˜ ํŠน์ • ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ํŠน๋ณ„ ์ง€์›ํ•˜๋Š”๋ฐ C๋Š” ๋ฐฐ์—ด(array), ํŽ„์€ ํ•ด์‹œ ํ…Œ์ด๋ธ”, LISP๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค. ํ•˜์Šค์ผˆ์€ ์„ ์ฒœ์ ์œผ๋กœ ์–ด๋–ค ์ข…๋ฅ˜์˜ ํŠธ๋ฆฌ๋„ ์žฅ๋ คํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋งค๊ฐœํ™” ๋‹คํ˜•์„ฑ(parametric polymorphism)๊ณผ ํƒ€์ž… ํด๋ž˜์Šค ๋•์— ๊ท ํ˜• ์ด์ง„ ํŠธ๋ฆฌ ๋“ฑ ์–ด๋–ค ์ถ”์ƒ ํƒ€์ž…๋„ ์‚ฌ์šฉ ๋ฐ ์žฌ์‚ฌ์šฉ์ด ์‰ฝ๋‹ค! [์ž๋ฃŒ๊ตฌ์กฐ] ์žฅ์—์„œ ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ œ๋“ค์„ ์œ„ํ•œ ์ž์—ฐ์Šค๋Ÿฌ์šด ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์„ ์ž์„ธํžˆ ๋‹ค๋ฃฌ๋‹ค. ํ•˜์Šค์ผˆ์€ ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์ด ์˜๊ตฌ์ ์ธ ๊ณตํ†ต๋ถ€๋ถ„์„ ๊ณต์œ ํ•œ๋‹ค. ์ฆ‰ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ตฌํŒ(old version)๋„ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. foo set = (newset, set) where newset = insert "bar" set ๋ฐ˜๋ฉด ๋ช…๋ นํ˜• ์–ธ์–ด์˜ ๋‹จ์ ์€ ๋‹จ๋ช…ํ•œ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ, ์ฆ‰ ์ž๋ฃŒ๊ฐ€ ๊ทธ ์ž๋ฆฌ์—์„œ ๊ฐฑ์‹ ๋˜์–ด ๊ตฌํŒ์€ ๋ฎ์–ด์”๋‹ค. ๊ฐ€๋ น ๋ฐฐ์—ด์€ ๋‹จ๋ช…ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ํ•˜์Šค์ผˆ์—์„œ ๋ฐฐ์—ด์„ ์“ฐ๋ ค๋ฉด ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•ด์•ผ ํ•˜๊ฑฐ๋‚˜ ๋ชจ๋‚˜๋“œ๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ์ด๋‹ค. ๋‹คํ–‰ํžˆ ํ๋‚˜ ํžˆํ”„ ๊ฐ™์€ ๋‹จ๋ช… ๊ตฌ์กฐ์— ๋Œ€์‘ํ•˜๋Š” ์˜๊ตฌ ๊ตฌ์กฐ๋“ค์ด ์žˆ๊ณ  [์ž๋ฃŒ๊ตฌ์กฐ] ์žฅ์—์„œ ๊ทธ๋Ÿฐ ์ชฝ์˜ ์›์น™, ๊ฐ€๋ น amortization ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ์„ค๊ณ„ ์›์น™๋“ค์„ ์ƒ๋ƒฅํ•˜๊ฒŒ ์†Œ๊ฐœํ•  ๊ฒƒ์ด๋‹ค. ๋ณ‘๋ ฌ์„ฑParallelism ๋ณ‘๋ ฌํ™”์˜ ๋ชฉ์ ์€ ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์—ฌ๋Ÿฌ ์ฝ”์–ด ๋˜๋Š” ์ปดํ“จํ„ฐ ์ƒ์—์„œ ์‹คํ–‰ํ•˜์—ฌ ๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์Šค์ผˆ์€ ๊ทธ ์ž์ฒด์˜ ์ˆœ์ˆ˜์„ฑ ๋•์— ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ๊ฐ•์ œํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ณ‘๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต์‹ํ™”ํ•˜๋Š” ๋ฐ ์ž˜ ๋“ค์–ด๋งž๋Š”๋‹ค. ํ•˜์Šค์ผˆ์—์„œ์˜ ๋ณ‘๋ ฌํ™”๋Š” ์•„์ง ์—ฐ๊ตฌ ์ฃผ์ œ์ด์ž ์‹คํ—˜ ๋Œ€์ƒ์ด๋‹ค. Control.Parallel.Strategies์— ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ์ œ์–ดํ•˜๋Š” ๊ฒฐํ•ฉ๊ธฐ ๋“ค ์ด ์žˆ์ง€๋งŒ ์กฐ๊ฐ์กฐ๊ฐ ํฉ์–ด์ ธ์žˆ๋‹ค. ๋œ ์•ผ๋ง์ ์ด์ง€๋งŒ ๋ณด๋‹ค ์‹ค์šฉ์ ์ธ ๋Œ€์•ˆ์ธ Data Parallel Haskell์— ๋Œ€ํ•œ ์—ฐ๊ตฌ๋„ ์ง„ํ–‰ ์ค‘์ด๋‹ค. [๋ณ‘๋ ฌํ™”] ์žฅ์€ ์•„์ง ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ํ•˜์Šค์ผˆ์—์„œ์˜ ๋ณ‘๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ํ˜„์‹œ์ ์—์„œ์˜ ๊ฐ€๋Šฅ์„ฑ์„ ์†Œ๊ฐœํ•  ๊ณ„ํš์ด๋‹ค. 2 ์„ฑ๋Šฅ ์˜ˆ์‹œ๋“ค ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Performance_examples ์˜ฎ๊ธด์ด: ์›๋ฌธ์ด ๋ฏธ์™„์„ฑ์ธ ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค. ๋ชฉํ‘œ: ์‹ค์ œ ์˜ˆ์‹œ๋ฅผ ๋“ค๋ฉฐ ์ตœ์ ํ™”๋ฅผ ๋‹จ๊ณ„ ๋ณ„๋กœ ์„ค๋ช…ํ•œ๋‹ค. ํƒ€์ดํŠธ ๋ฃจํ”„ ํ•˜์Šค์ผˆ์„ C ๋งŒํผ ๋น ๋ฅด๊ฒŒ ๋งŒ๋“ค๊ธฐ: ์—„๊ฒฉํ•จ, ์ง€์—ฐ์„ฑ, ์žฌ๊ท€์˜ ํ™œ์šฉ CSV ํŒŒ์‹ฑ haskell-cafe: ๋˜ ๋‹ค๋ฅธ ์ดˆ์งœ์˜ ์ˆ˜ํ–‰๋Šฅ๋ ฅ ์งˆ๋ฌธ type CSV = [[String]] main = do args <- getArgs file <- readFile (head args) writeFile (head args ++ "2") (processFile (args !! 1) file) processFile s = writeCSV . doInteraction s. readCSV doInteraction line csv = insertLine (show line) (length csv - 1) csv writeCSV = (\x -> x ++ "\n") . concat . intersperse "\n" . (map (concat . intersperse "," . (map show))) insertLine line pos csv = (take pos csv) ++ [readCSVLine line] ++ drop pos csv readCSVLine = read . (\x -> "["++x++"]") readCSV = map readCSVLine . lines ๋ฉ”์ผ๋ง ๋ฆฌ์ŠคํŠธ์— cvs ํŒŒ์‹ฑ์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๊ฐ€ ์žˆ๋˜ ๊ฑฐ ๊ฐ™์€๋ฐ ๋ชป ์ฐพ๊ฒ ๋‹ค. ๊ณต๊ฐ„ ๋ˆ„์ˆ˜ jkff๊ฐ€ ๋กœ๊ทธ ํŒŒ์ผ์„ ๋ถ„์„ํ•˜๋Š” ์ฝ”๋“œ์— ๊ด€ํ•ด ๋ฌผ์–ด๋ดค๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ํžˆ์Šคํ† ๊ทธ๋žจ์„ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ๋‹ค. foldl' (\m (x, y) -> insertWith' x (\[y] ys -> y:ys) [y] m) M.empty [(ByteString.copy foo, ByteString.copy bar) | (foo, bar) <- map (match regex) lines] ์ž…๋ ฅ์€ 1GB ๋กœ๊ทธ์˜€๊ณ  ํ”„๋กœ๊ทธ๋žจ์€ ByteString.copy ๋•Œ๋ฌธ์— ๊ฐ€์šฉ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ„ฐ๋œจ๋ ธ๋‹ค. ํ‰๊ฐ€๊ฐ€ ๊ฐ•์ œ๋˜์ง€ ์•Š์•„ ํŒŒ์ผ ์ „์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ”๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 3 ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Graph_reduction TODO reduction = ์†Œ๊ฑฐ or ํ™˜์› ์›๋ฌธ์ด ๋ฏธ์™„์„ฑ์ž…๋‹ˆ๋‹ค. ์„œ๋ฌธ ์ง€์—ฐ ํ‰๊ฐ€๋กœ ํ‘œํ˜„์‹ ํ‰๊ฐ€ํ•˜๊ธฐ ์†Œ๊ฑฐ ์†Œ๊ฑฐ ์ „๋žต ์ •๋ฆฌ (CUrch Roser II) ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ (์†Œ๊ฑฐ + ๊ณต์œ ) ํŒจํ„ด ๋งค์นญ ๊ณ ์ฐจ ํ•จ์ˆ˜ Weak Head Normal Form Weak Head Normal Form strict ํ•จ์ˆ˜์™€ non-strict ํ•จ์ˆ˜ ๊ณต๊ฐ„ ์ œ์–ด ๊ณต์œ ์™€ CSE ๊ผฌ๋ฆฌ ์žฌ๊ท€ ์‹œ๊ฐ„ ์ถ”๋ก  ์ง€์—ฐ ํ‰๊ฐ€ < ์ ๊ทน์  ํ‰๊ฐ€ ์ธ์ž ๋ฒ„๋ฆฌ๊ธฐ ์˜์†์„ฑ & ๊ฐ๊ฐ€์ƒ๊ฐ ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ์˜ ๊ตฌํ˜„ ๋ ˆํผ๋Ÿฐ์Šค ์„œ๋ฌธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ๋‹จ์ˆœํžˆ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก (denotational semantics)์— ์˜ํ•ด ๋„์ถœ๋œ ์˜ฌ๋ฐ”๋ฅธ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ์ผ์ด ์•„๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์ ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์“ฐ๋Š” ๋น ๋ฅธ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ์ผ์ด๊ธฐ๋„ ํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ๊ธฐ๊ณ„์—์„œ ์–ด๋–ป๊ฒŒ ์‹คํ–‰๋˜๋Š”์ง€๋ฅผ ์•Œ์•„์•ผ ํ•˜๋Š”๋ฐ, ๊ทธ ๊ธฐ๊ณ„๋Š” ์ฃผ๋กœ ๋™์ž‘ ์˜๋ฏธ๋ก (operational semantics) ์ ์œผ๋กœ ๊ธฐ์ˆ ๋œ๋‹ค. ์ด ์žฅ์—์„œ๋Š” ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์ด ์‹ค์ œ ์ปดํ“จํ„ฐ์—์„œ ์–ด๋–ป๊ฒŒ ์‹คํ–‰๋˜๋Š”์ง€๋ฅผ ์„ค๋ช…ํ•˜๋ฉฐ ์ด๋Š” ์‹œ๊ฐ„ ๋ฐ ๊ณต๊ฐ„์˜ ์‚ฌ์šฉ์„ ๋ถ„์„ํ•˜๋Š” ํ† ๋Œ€๊ฐ€ ๋œ๋‹ค. ํ•˜์Šค ์ผˆ ํ‘œ์ค€์€ ํŠน์ •ํ•œ ๋™์ž‘ ์˜๋ฏธ๋ก ์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์Šค ์ผˆ ๊ตฌํ˜„์ฒด๋Š” ์–ด๋–ค ์˜๋ฏธ๋ก ์ด๋“  ์ž์œ ๋กญ๊ฒŒ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„์ง๊นŒ์ง€ ๋ชจ๋“  ํ•˜์Šค ์ผˆ ๊ตฌํ˜„์€ ์ง€์—ฐ ํ‰๊ฐ€์˜ ์‹คํ–‰ ๋ชจํ˜•์„ ๋”ฐ๋ฅธ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์ง€์—ฐ ํ‰๊ฐ€๋ฅผ ๋ฉด๋ฐ€ํžˆ ์‚ดํŽด๋ณด๊ณ  ์ด ์‹คํ–‰ ๋ชจํ˜•์œผ๋กœ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹œ๊ฐ„ ๋ฐ ๊ณต๊ฐ„ ๋ณต์žก๋„์— ๋Œ€ํ•œ ์ถ”๋ก ์„ ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. ์ง€์—ฐ ํ‰๊ฐ€๋กœ ํ‘œํ˜„์‹ ํ‰๊ฐ€ํ•˜๊ธฐ ์†Œ๊ฑฐ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ํ•˜๊ธฐ, ์ฆ‰ ํ‘œํ˜„์‹ ํ‰๊ฐ€ํ•˜๊ธฐ๋Š” ๋ชจ๋“  ํ•จ์ˆ˜ ์ ์šฉ์ด ์ „๊ฐœ๋  ๋•Œ๊นŒ์ง€ ํ•จ์ˆ˜ ์ •์˜๋“ค์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. ๋‹ค์Œ ์ •์˜๋“ค๊ณผ ํ•จ๊ป˜ ํ‘œํ˜„์‹ pythagoras 3 4๋ฅผ ์‚ดํŽด๋ณด์ž. square x = x * x pythagoras x y = square x + square y ๋‹ค์Œ์€ ๊ฐ€๋Šฅํ•œ ์†Œ๊ฑฐ ์ ˆ์ฐจ ์ค‘ ํ•˜๋‚˜๋‹ค. pythagoras 3 4 โ‡’ square 3 + square 4 (pythagoras) โ‡’ (3*3) + square 4 (square) โ‡’ 9 + square 4 (*) โ‡’ 9 + (4*4) (square) โ‡’ 9 + 16 (*) โ‡’ 25 ๋ชจ๋“  ์†Œ๊ฑฐ๋Š” ํ•˜์œ„ ํ‘œํ˜„์‹์„ ๋™๋“ฑํ•œ ๋‹ค๋ฅธ ํ‘œํ˜„์‹์œผ๋กœ ์น˜ํ™˜ํ•œ๋‹ค. ๊ทธ๋Ÿฐ ํ•˜์œ„ ํ‘œํ˜„์‹์„ ์†Œ๊ฑฐ ๊ฐ€๋Šฅ ํ‘œํ˜„์‹(reducible expression), ์งง๊ฒŒ๋Š” redex๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์น˜ํ™˜์—๋Š” square ๊ฐ™์€ ํ•จ์ˆ˜ ์ •์˜๋‚˜ (+) ๊ฐ™์€ ๋‚ด์žฅ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค. redex ์—†๋Š” ํ‘œํ˜„์‹์„<NAME>(normal form)์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๋ฌผ๋ก  ์‹คํ–‰์€<NAME>์— ๋‹ค๋‹ค๋ฅด๋ฉด ๋ฉˆ์ถ”๊ณ  ์ด๊ฒƒ์ด ๊ณ„์‚ฐ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋œ๋‹ค. ๋‹น์—ฐํžˆ ์†Œ๊ฑฐ๋ฅผ ์ ๊ฒŒ ํ• ์ˆ˜๋ก ํ”„๋กœ๊ทธ๋žจ์€ ๋” ๋น ๋ฅด๊ฒŒ ์‹คํ–‰๋œ๋‹ค. ์‹ค์ œ ํ•˜๋“œ์›จ์–ด์—์„œ์˜ ๊ตฌํ˜„์€ ์•„์ฃผ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์†Œ๊ฑฐ ๋‹จ๊ณ„์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ๊ฐ™๋‹ค๊ณ  ๊ธฐ๋Œ€ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ํ•˜์ง€๋งŒ ์ ๊ทผ์ ์ธ ๋ณต์žก๋„ ์ธก๋ฉด์—์„œ ์†Œ๊ฑฐ ๊ฐœ์ˆ˜๋Š” ์ •ํ™•ํ•œ ์ธก์ •์น˜๋‹ค. ์†Œ๊ฑฐ ์ „๋žต ๊ฐ€๋Šฅํ•œ ์†Œ๊ฑฐ ์ ˆ์ฐจ๋Š” ๋‹ค์–‘ํ•˜๊ณ  ์†Œ๊ฑฐ ๊ฐœ์ˆ˜๋Š” ์ˆ˜ํ–‰๋˜๋Š” ์†Œ๊ฑฐ ์ˆœ์„œ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค. ํ‘œํ˜„์‹ fst (square 3, square 4)๋ฅผ ์˜ˆ๋กœ ๋“ค์ž. ํ•จ์ˆ˜ ์ •์˜๋ฅผ ์ ์šฉํ•˜๊ธฐ ์ „์— ๋ชจ๋“  ํ•จ์ˆ˜ ์ธ์ž๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. fst (square 3, square 4) โ‡’ fst (3*3, square 4) (square) โ‡’ fst ( 9 , square 4) (*) โ‡’ fst ( 9 , 4*4) (square) โ‡’ fst ( 9 , 16 ) (*) โ‡’ 9 (fst) ``` ์ด ์ „๋žต์€ **์ตœ๋‚ด ์šฐ์„  ์†Œ๊ฑฐ(innermost reduction)**๋ผ ๋ถ€๋ฅด๋ฉฐ **์ตœ๋‚ด redex(innermost redex)**์€ ๋‚ด๋ถ€์— ๋‹ค๋ฅธ ํ•˜์œ„ ํ‘œํ˜„์‹์„ redex๋กœ ๊ฐ€์ง€์ง€ ์•Š๋Š” redex์ด๋‹ค. ๋‹ค๋ฅธ ๊ฐ€๋Šฅ์„ฑ์œผ๋กœ ๋ชจ๋“  ํ•จ์ˆ˜ ์ •์˜๋ฅผ ์ ์šฉํ•˜๊ณ  ์ธ์ž๋งŒ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ``` fst (square 3, square 4) โ‡’ square 3 (fst) โ‡’ 3*3 (square) โ‡’ 9 (*) ``` ์ด๋ฅผ **์ตœ ์™ธ ์†Œ๊ฑฐ(outermost reduction)**๋ผ ๋ถ€๋ฅด๋ฉฐ ํ•ญ์ƒ ๋‹ค๋ฅธ redex ์•ˆ์— ์žˆ์ง€ ์•Š์€ **outermost redex**๋ฅผ ์†Œ๊ฑฐํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ตœ์™ธ ์†Œ๊ฑฐ๊ฐ€ ์ตœ๋‚ด ์†Œ๊ฑฐ๋ณด๋‹ค ๋‹จ๊ณ„ ์ˆ˜๊ฐ€ ์ ๋‹ค. `fst` ํ•จ์ˆ˜๋Š” ์ง์˜ ๋‘ ๋ฒˆ์งธ ์„ฑ๋ถ„์ด ํ•„์š”ํ•˜์ง€ ์•Š์•„์„œ `square 4`์˜ ์†Œ๊ฑฐ๊ฐ€ ํ•„์š” ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. #### ์ข…๋ฃŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ‘œํ˜„์‹์—์„œ๋Š” ``` loop = 1 + loop ``` ์–ด๋–ค ์†Œ๊ฑฐ ์ ˆ์ฐจ๋„ ์ข…๋ฃŒํ•˜์ง€ ์•Š๊ณ  ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์€ ๋๋‚˜์ง€ ์•Š๋Š” ๋ฃจํ”„์— ๋น ์ง„๋‹ค. ์ด๋Ÿฐ ํ‘œํ˜„์‹์€<NAME>์ด ์—†๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์–ด๋–ค ์†Œ๊ฑฐ ์ ˆ์ฐจ๋Š” ์ข…๋ฃŒํ•˜๊ณ  ์–ด๋–ค ๊ฑด ๊ทธ๋ ‡์ง€ ์•Š์€ ํ‘œํ˜„์‹๋“ค๋„ ์žˆ๋‹ค. fst (42, loop) โ‡’ 42 (fst) fst (42, loop) โ‡’ fst (42,1+loop) (loop) โ‡’ fst (42,1+(1+loop)) (loop) โ‡’ ... ``` ์ฒซ ๋ฒˆ์งธ ์†Œ๊ฑฐ ์ ˆ์ฐจ๋Š” ์ตœ์™ธ ์†Œ๊ฑฐ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ์ตœ๋‚ด ์†Œ๊ฑฐ์ธ๋ฐ fst๊ฐ€ ๋ฌด์‹œํ•  ํ…๋ฐ๋„ ์“ธ๋ฐ์—†์ด loop๋ฅผ ํ‰๊ฐ€ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์˜ค์ง ํ•„์š”ํ•  ๋•Œ๋งŒ ํ•จ์ˆ˜ ์ธ์ž๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋Šฅ๋ ฅ์ด ์ข…๋ฃŒ๋ฅผ ๋”ฐ์งˆ ๋•Œ๋Š” ์ตœ์™ธ ์†Œ๊ฑฐ๋ฅผ ์ตœ์ ์œผ๋กœ ๋งŒ๋“ ๋‹ค. ์ •๋ฆฌ (CUrch Roser II) ์ข…๋ฃŒํ•˜๋Š” ์†Œ๊ฑฐ๊ฐ€ ์ ์–ด๋„ ํ•˜๋‚˜ ์žˆ์œผ๋ฉด ์ตœ์™ธ ์†Œ๊ฑฐ ์—ญ์‹œ ์ข…๋ฃŒํ•œ๋‹ค. ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ (์†Œ๊ฑฐ + ๊ณต์œ ) ์ธ์ž๋ฅผ ๋ฒ„๋ฆฌ๋Š” ๋Šฅ๋ ฅ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ตœ์™ธ ์†Œ๊ฑฐ๊ฐ€ ํ•ญ์ƒ ์ตœ๋‚ด ์†Œ๊ฑฐ๋ณด๋‹ค ์†Œ๊ฑฐ ๋‹จ๊ณ„๊ฐ€ ์ ์€ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. square (1+2) โ‡’ (1+2)*(1+2) (square) โ‡’ (1+2)*3 (+) โ‡’ 3*3 (+) โ‡’ 9 (*) ์—ฌ๊ธฐ์„œ ์ธ์ž (1+2)๊ฐ€ ์ค‘๋ณต๋˜๋ฉฐ ๋‘ ๋ฒˆ ์†Œ๊ฑฐ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๋˜‘๊ฐ™์€ ์ธ์ž์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹ต์€ ์†Œ๊ฑฐ (1+2) โ‡’ 3๋ฅผ ๋ชจ๋“  ๊ฐ™์€ ์ธ์ž์—์„œ<NAME>๋Š” ๊ฒƒ์ด๋‹ค. ํ‘œํ˜„์‹์„ ๊ทธ๋ž˜ํ”„๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹ค์Œ์€ __________ | | โ†“ โ—Š * โ—Š (1+2) ํ‘œํ˜„์‹ (1+2)*(1+2)๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. square (1+2)์˜ ์ตœ์™ธ ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰๋œ๋‹ค. square (1+2) โ‡’ __________ (square) | | โ†“ โ—Š * โ—Š (1+2) โ‡’ __________ (+) | | โ†“ โ—Š * โ—Š 3 โ‡’ 9 (*) ๊ทธ๋ฆฌ๊ณ  ์ด ์ž‘์—…์€ ๊ณต์œ ๋œ๋‹ค. ์ฆ‰ ์ตœ์™ธ ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ๋Š” ๋ชจ๋“  ์ธ์ž๋ฅผ ์ตœ๋Œ€ ํ•œ ๋ฒˆ๋งŒ ์†Œ๊ฑฐํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ตœ์™ธ ์†Œ๊ฑฐ๋Š” ํ•ญ์ƒ ์ตœ๋‚ด ์†Œ๊ฑฐ๋ณด๋‹ค ์†Œ๊ฑฐ ๋‹จ๊ณ„๊ฐ€ ์ ์œผ๋ฉฐ, ์ด๋Š” ์†Œ๋ชจ ์‹œ๊ฐ„ ์ถ”๋ก ์—์„œ ์ฆ๋ช…ํ•œ๋‹ค. ํ‘œํ˜„์‹ ๊ณต์œ ๋Š” let๊ณผ where์—์„œ๋„ ์กด์žฌํ•œ๋‹ค. ๊ฐ€๋ น ๋ณ€์˜ ๊ธธ์ด a, b, c๋ฅผ ๊ฐ€์ง€๊ณ  ์‚ผ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ—ค๋ก ์˜ ๊ณต์‹์„ ๋ณด์ž. area a b c = let s = (a+b+c)/2 in sqrt (s*(s-a)*(s-b)*(s-c)) ์ •์‚ผ๊ฐํ˜•์— ์ ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์†Œ๊ฑฐ๋œ๋‹ค. area 1 1 1 โ‡’ _____________________ (area) | | | | โ†“ sqrt (โ—Š*(โ—Š-a)*(โ—Š-b)*(โ—Š-c)) ((1+1+1)/2) โ‡’ _____________________ (+),(+),(/) | | | | โ†“ sqrt (โ—Š*(โ—Š-a)*(โ—Š-b)*(โ—Š-c)) 1.5 โ‡’ ... โ‡’ 0.433012702 ๊ฒฐ๊ณผ๋Š” 4 ์ด๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜๋ฉด let ๋ฐ”์ธ๋”ฉ์€ ๊ทธ๋ž˜ํ”„์˜ ๋…ธ๋“œ์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์‚ฌ์‹ค graphical notation์„ ์™„์ „ํžˆ ์ƒ๋žตํ•˜๊ณ  let ๋งŒ์œผ๋กœ ๊ณต์œ ๋ฅผ ์ง€์ •ํ•ด ๊ทธ๋ž˜ํ”„ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 1 ํ•˜์Šค์ผˆ์˜ ๋ชจ๋“  ๊ตฌํ˜„์€ ์–ด๋Š ํ˜•ํƒœ๋กœ๋“  ์ตœ์™ธ ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ์— ๊ธฐ๋ฐ˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„๊ณผ ๊ณต๊ฐ„ ํ• ๋‹น์˜ ์ ๊ทผ์  ๋ณต์žก๋„๋ฅผ ์ถ”๋ก ํ•˜๊ธฐ ์ข‹์€ ๋ชจํ˜•์„ ์ œ๊ณตํ•œ๋‹ค.<NAME>์— ๋‹ค๋‹ค๋ฅด๊ธฐ ์œ„ํ•œ ์†Œ๊ฑฐ ๋‹จ๊ณ„ ๊ฐœ์ˆ˜๋Š” ์‹คํ–‰ ์‹œ๊ฐ„์—, ๊ทธ๋ž˜ํ”„ ๋‚ด ํ•ญ๋“ค์˜ ํฌ๊ธฐ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์— ๋Œ€์‘ํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋ฌธ์ œ 1. square (square 3)์„ ์ตœ๋‚ด/์ตœ์™ธ ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ๋ฅผ ์ด์šฉํ•ด<NAME>์œผ๋กœ ์†Œ๊ฑฐํ•˜๋ผ. ๋ฌธ์ œ 2. ๋‹ค์Œ์˜ ๊ณ ์†<NAME> ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ x์˜ n ์Šน์„ ๊ณ„์‚ฐํ•œ๋‹ค. power x 0 = 1 power x n = x' * x' * (if n `mod` 2 == 0 then 1 else x) where x' = power x (n `div` 2) power 2 5๋ฅผ ์ตœ๋‚ด/์ตœ์™ธ ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ๋ฅผ ์ด์šฉํ•ด ์†Œ๊ฑฐํ•˜๋ผ. ์†Œ๊ฑฐ๋ฅผ ๋ช‡ ๋ฒˆ ํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ์ผ๋ฐ˜์ ์ธ power 2 n์˜ ์ ๊ทผ์  ๋ณต์žก๋„๋Š” ๋ฌด์—‡์ธ๊ฐ€? "graphless" ์ตœ์™ธ ์†Œ๊ฑฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์–ด๋–ป๊ฒŒ ๋˜๋Š”๊ฐ€? ํŒจํ„ด ๋งค์นญ ์ง€๊ธˆ๊นŒ์ง€๋Š” ํŒจํ„ด ๋งค์นญ๊ณผ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ์„ ๋•Œ ์ตœ์™ธ ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€ ์„ค๋ช…ํ•˜์ง€ ์•Š์•˜๋‹ค. ์ด ๋ถ€๋ถ„์„ ์„ค๋ช…ํ•˜๋ฉด์„œ ํ•˜์Šค ์ผˆ ๊ฐ™์€ non-strict ํ•จ์ˆ˜ํ˜• ์–ธ์–ด๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ํ”ํžˆ ํ† ๋Œ€๊ฐ€ ๋˜๋Š” ์†Œ๊ฑฐ ์ „๋žต์˜ ๋Œ€๋ถ€๋ถ„์„ ์งš๊ณ  ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ call-by-need ๋˜๋Š” ์ง€์—ฐ ํ‰๊ฐ€(lazy evaluation)๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, ํ•จ์ˆ˜ ์ธ์ž์˜ ์†Œ๊ฑฐ๋ฅผ ์ตœํ›„์˜ ๋๊นŒ์ง€ "๊ฒŒ์œผ๋ฅด๊ฒŒ" ๋ฏธ๋ฃจ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์„ธ๋ถ€ ์‚ฌํ•ญ์€ ๋‹ค์Œ ์ฑ•ํ„ฐ๋“ค์—์„œ ๋‹ค๋ฃจ๊ฒ ๋‹ค. ํŒจํ„ด ๋งค์นญ์ด specification์„ ์–ด๋–ป๊ฒŒ ์š”๊ตฌํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์œ„ํ•ด ๋ถˆ๋ฆฌ์–ธ ๋…ผ๋ฆฌํ•ฉ์„ ์‚ดํŽด๋ณด์ž. or True y = True or False y = y ๊ทธ๋ฆฌ๊ณ  ํ‘œํ˜„์‹ or (1==1) loop loop = not loop๋Š” ์ข…๋ฃŒํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค์Œ ์†Œ๊ฑฐ ์ ˆ์ฐจ๋Š” or (1==1) loop โ‡’ or (1==1) (not loop) (loop) โ‡’ or (1==1) (not (not loop)) (loop) โ‡’ ... ์ตœ์™ธ redex๋งŒ์„ ์†Œ๊ฑฐํ•˜๊ณ  ๋”ฐ๋ผ์„œ ์ตœ์™ธ ์†Œ๊ฑฐ๋‹ค. ํ•˜์ง€๋งŒ or (1==1) loop โ‡’ or True loop (or) โ‡’ True ์ด ๋” ๋ง์ด ๋œ๋‹ค. ๋ฌผ๋ก  ์šฐ๋ฆฌ๋Š” or์˜ ์ •์˜๋ฅผ ์ ์šฉํ•˜๊ณ  ์–ด๋–ค ์‹์„ ์„ ํƒํ• ์ง€ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ์ธ์ž๋“ค์„ ์†Œ๊ฑฐํ•˜๊ณ  ์‹ถ์„ ๋ฟ์ด๋‹ค. ์ด ์˜๋„๋Š” ํ•˜์Šค์ผˆ์˜ ํŒจํ„ด ๋งค์นญ ๊ทœ์น™์— ์˜ํ•ด ํฌ์ฐฉ๋œ๋‹ค. ์ขŒ๋ณ€์€ ์œ„์—์„œ ์•„๋ž˜๋กœ ๋งค์นญ๋œ๋‹ค. ์ขŒ๋ณ€์„ ๋งค์นญํ•  ๋•Œ ์ธ์ž๋“ค์€ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋งค์นญ๋œ๋‹ค. ์ธ์ž๋“ค์˜ ๋งค์นญ ์—ฌ๋ถ€ ๊ฒฐ์ •์— ํ•„์š”ํ•œ ๋งŒํผ๋งŒ ์ธ์ž๋“ค์„ ํ‰๊ฐ€ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ์˜ or (1==1) loop์˜ ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋ฅผ True๋‚˜ False๋กœ ์†Œ๊ฑฐํ•œ ๋‹ค์Œ, ๋ณ€์ˆ˜ y ํŒจํ„ด์— ๋งค์นญํ•˜๊ธฐ ์œ„ํ•ด ๋‘ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ํ‰๊ฐ€ํ•˜๊ณ , ์ผ์น˜ํ•˜๋Š” ํ•จ์ˆ˜ ์ •์˜๋ฅผ ์ „๊ฐœํ•œ๋‹ค. ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๋งค์นญ์€ ํ•ญ์ƒ ์„ฑ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ์†Œ๊ฑฐ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์€ ์œ„ ์ ˆ์ฐจ์˜ ๋‘ ๋ฒˆ์งธ ๋‹จ๊ณ„๋‹ค. ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ์—ผ๋‘์— ๋‘๋ฉด ๋Œ€๋ถ€๋ถ„์˜ ํ•˜์Šค ์ผˆ ํ‘œํ˜„์‹์„ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ์—ฐ์Šต๋ฌธ์ œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋‹ค์Œ ํ‘œํ˜„์‹๋“ค์„ ์ง€์—ฐ ํ‰๊ฐ€ํ•˜์—ฌ normal form์œผ๋กœ ์†Œ๊ฑฐํ•˜๋ผ. Prelude์˜ ํ‘œ์ค€ ํ•จ์ˆ˜ ์ •์˜๋“ค์„ ์‚ฌ์šฉํ•  ๊ฒƒ. length [42,42+1,42-1] head (map (2*) [1,2,3]) head $ [1,2,3] ++ (let loop = tail loop in loop) zip [1.. 3] (iterate (+1) 0) head $ concatMap (\x -> [x, x+1]) [1,2,3] take (42-6*7) $ map square [2718.. 3146] ๊ณ ์ฐจ ํ•จ์ˆ˜ ๊ณ ์ฐจ ํ•จ์ˆ˜์™€ ์ปค๋ง์˜ ์†Œ๊ฑฐ๊ฐ€ ๋‚จ์•˜๋‹ค. ๋‹ค์Œ ์ •์˜๋“ค์—์„œ id x = x a = id (+1) 41 twice f = f. f b = twice (+1) (13*3) id์™€ twice ๋‘˜ ๋‹ค ์ธ์ž๊ฐ€ ํ•˜๋‚˜๋‹ค. ํ•ด๋‹ต์€ ์—ฌ๋Ÿฌ ์ธ์ž๋ฅผ ํ•œ ์ธ์ž์˜ ์—ฐ์ด์€ ์ ์šฉ์œผ๋กœ ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์„ ์ปค๋งcurrying์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. a = (id (+1)) 41 b = (twice (+1)) (13*3) ์ž„์˜์˜ ํ•จ์ˆ˜ ์ ์šฉ expression1 expression2๋ฅผ ์†Œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด call-by-need๋Š” ๋จผ์ € expression1์ด ํ•จ์ˆ˜๊ฐ€ ๋˜๊ณ  ์ด ํ•จ์ˆ˜๋ฅผ expression2๋ฅผ ์ด์šฉํ•ด unfold ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ๊นŒ์ง€ ์†Œ๊ฑฐํ•œ๋‹ค. ์†Œ๊ฑฐ ์ ˆ์ฐจ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. โ‡’ (id (+1)) 41 (a) โ‡’ (+1) 41 (id) โ‡’ 42 (+) โ‡’ (twice (+1)) (13*3) (b) โ‡’ ((+1).(+1) ) (13*3) (twice) โ‡’ (+1) ((+1) (13*3)) (.) โ‡’ (+1) ((+1) 39) (*) โ‡’ (+1) 40 (+) โ‡’ 41 (+) ์„ค๋ช…์ด ์กฐ๊ธˆ ๋ชจํ˜ธํ•œ๋ฐ, ๋‹ค์Œ ์ ˆ์—์„œ ๋ช…ํ™•ํ•˜๊ฒŒ ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. ํŒจํ„ด ๋งค์นญ์€ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ณ„์‚ฐ์„ ์œ„ํ•œ ๊ฒƒ์ด๊ณ  ๊ณ ์ฐจ ํ•จ์ˆ˜๋Š” ๋‹จ์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ณธ์งˆ์„ ๋ถ™์žก๊ธฐ ์œ„ํ•œ ๊ฒƒ์œผ๋กœ ๋ณด์ผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ํ•จ์ˆ˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋กœ์„œ๋„ ์œ ์šฉํ•˜๋‹ค. ๊ทธ ์˜ˆ ์ค‘ ํ•˜๋‚˜๋กœ O(1) ์‹œ๊ฐ„์— ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋Š” difference list ([a] -> [a])๊ฐ€ ์žˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋กœ fold๋ฅผ ์ด์šฉํ•œ ์ŠคํŠธ๋ฆผ ํ‘œํ˜„์ด ์žˆ๋‹ค. ์‚ฌ์‹ค ๋ชจ๋“  ํ•จ์ˆ˜ํ˜• ์–ธ์–ด์˜ ๋ฟŒ๋ฆฌ์ธ ์ˆœ์ˆ˜ ๋žŒ๋‹ค ๋Œ€์ˆ˜์—์„œ, ๋ชจ๋“  ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ํ•จ์ˆ˜๋กœ ํ‘œํ˜„๋œ๋‹ค. Weak Head Normal Form ์ง€์—ฐ ํ‰๊ฐ€๊ฐ€ ์†Œ๊ฑฐ ์ ˆ์ฐจ๋ฅผ ์–ด๋–ป๊ฒŒ ์„ ํƒํ•˜๋Š”์ง€ ์ •ํ™•ํžˆ ๊ณต์‹ํ™”ํ•˜๋ ค๋ฉด ๋“ฑ์‹์— ์˜ํ•œ ํ•จ์ˆ˜ ์ •์˜๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ํ‘œํ˜„์‹ ์œ„์ฃผ์˜ ์ ‘๊ทผ๋ฒ•์„ ์ทจํ•˜๋Š” ๊ฒƒ์ด ์ตœ์„ ์ด๋‹ค. f (x:xs) = ... ๊ฐ™์€ ํ•จ์ˆ˜ ์ •์˜๋ฅผ f = expression ํ˜•ํƒœ๋กœ ๋ฐ”๊พธ์ž๋Š” ๋ง์ด๋‹ค. ์ด ์ผ์„ case expression๊ณผ lambda abstraction์ด๋ผ๋Š” ๋‘ ๊ธฐ๋ณธํ˜•์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค. case ํ‘œํ˜„์‹์˜ ๊ธฐ๋ณธํ˜•์€ ์ตœ์™ธ ์ƒ์„ฑ์ž์˜ ๋ถ„ํ•ด๋งŒ ํ—ˆ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฆฌ์ŠคํŠธ์˜ ๊ธฐ๋ณธ case ํ‘œํ˜„์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ผด์ด๋‹ค. case expression of [] -> ... x:xs -> ... lambda abstraction์€ ์ธ์ž๊ฐ€ ํ•˜๋‚˜์ธ ํ•จ์ˆ˜์ด๋ฏ€๋กœ ๋‹ค์Œ์˜ ๋‘ ์ •์˜๋Š” ๋™๋“ฑํ•˜๋‹ค. f x = expression f = \x -> expression ๋‹ค์Œ์€ zip์˜ ์ •์˜๋‹ค. zip :: [a] -> [a] -> [(a, a)] zip [] ys = [] zip xs [] = [] zip (x:xs') (y:ys') = (x, y):zip xs' ys' ๊ทธ๋ฆฌ๊ณ  ์ด๊ฒƒ์€ case ํ‘œํ˜„์‹๊ณผ ๋žŒ๋‹ค abstraction์œผ๋กœ ๋ฒˆ์—ญํ•œ ๊ฒƒ์ด๋‹ค. zip = \xs -> \ys -> case xs of [] -> [] x:xs' -> case ys of [] -> [] y:ys' -> (x, y):zip xs' ys' ๋ชจ๋“  ์ •์˜๋ฅผ ์ด๋Ÿฌํ•œ ๊ธฐ๋ณธํ˜•๋“ค๋กœ ๋ฒˆ์—ญํ•˜๋ฉด ๋ชจ๋“  redex๋Š” ๋‹ค์Œ ์ค‘ ํ•œ ํ˜•ํƒœ๊ฐ€ ๋œ๋‹ค. ํ•จ์ˆ˜ ์ ์šฉ ((\variable->expression1) expression2) case ํ‘œํ˜„์‹ case expression of { ... } Weak Head Normal Form ํ‘œํ˜„์‹์ด WHNF ์ผ ํ•„์š”์ถฉ๋ถ„์กฐ๊ฑด์€ ๋‹ค์Œ ์ค‘ ํ•˜๋‚˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. True, Just (square 42), (:) 1 ๊ฐ™์€ (์•„๋งˆ ์ธ์ž์— ์ ์šฉ๋ ) ์ƒ์„ฑ์ž (+) 2, sqrt์ฒ˜๋Ÿผ ์ธ์ž๋ฅผ ์™„์ „ํžˆ ์ „๋‹ฌํ•˜์ง€ ์•Š์€ ๋‚ด์žฅ ํ•จ์ˆ˜ ์ ์šฉ ๋žŒ๋‹ค abstraction \x -> expression strict ํ•จ์ˆ˜์™€ non-strict ํ•จ์ˆ˜ non-strict ํ•จ์ˆ˜๋Š” ์ธ์ž๊ฐ€ ํ•„์š” ์—†๋‹ค. non-termination์˜ ๋‹ค๋ฅธ ํ˜•ํƒœ๋“ค์„ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด strict ํ•จ์ˆ˜๋Š” ์ธ์ž๊ฐ€ WHNF ์—ฌ์•ผ ํ•œ๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด f x = loop๋Š” ์ธ์ž๊ฐ€ ํ•„์š” ์—†๋‹ค) ๊ณต๊ฐ„ ์ œ์–ด ์—ฌ๊ธฐ์„œ "๊ณต๊ฐ„(space)"์€ ๊ทธ๋ž˜ํ”„ ์ˆœํšŒ๋กœ ๋” ์ž˜ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜ํ”„ ์ž๋ฃŒ๊ตฌ์กฐ๋‚˜ induced dependency ๊ทธ๋ž˜ํ”„๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ผ. ์˜ˆ๋ฅผ ๋“ค์–ด Fibonacci(N)์€ N = 0 ๋˜๋Š” N = 1์ผ ๊ฒฝ์šฐ ์•„๋ฌด๊ฒƒ์—๋„ ์˜์กดํ•˜์ง€ ์•Š๊ณ  ๊ทธ ์™ธ์—๋Š” Fibonacci(N-1)๊ณผ Fibonacci(N-2)์— ์˜์กดํ•œ๋‹ค. Fibonacci(N-1)์ด Fibonacci(N-2)์— ์˜์กดํ•˜๋ฏ€๋กœ ์ด induced graph๋Š” ํŠธ๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ตฌํ˜„ ๊ธฐ๋ฒ•๊ณผ ์ž๋ฃŒ๊ตฌ์กฐ ์ˆœํšŒ ๋ฐฉ๋ฒ•์ด ๋Œ€์‘ํ•œ๋‹ค. ๋Œ€์‘ํ•˜๋Š” ๊ตฌํ˜„ ๊ธฐ๋ฒ• ์ž๋ฃŒ๊ตฌ์กฐ ์ˆœํšŒ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ DFS (์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๋ฉ”๋ชจ๋ฆฌ์— ์œ ์ง€) ๋ณ‘๋ ฌ ํ‰๊ฐ€ BFS (์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๋ฉ”๋ชจ๋ฆฌ์— ์œ ์ง€) ๊ณต์œ  DAC ์ˆœํšŒ ("frontier"๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ์œ ์ง€) ์ผ๋ฐ˜์ ์ธ ์žฌ๊ท€ ํŠธ๋ฆฌ ์ˆœํšŒ (์Šคํƒ ์ฑ„์šฐ๊ธฐ) ๊ผฌ๋ฆฌ ์žฌ๊ท€ ๋ฆฌ์ŠคํŠธ ์ˆœํšŒ / ๊ทธ๋ฆฌ๋”” ์„œ์น˜ (์ƒ์ˆ˜ ๊ณต๊ฐ„) ๋‹ค์Œ์˜ ๊ณ ์ „์ ์ธ ํ”ผ๋ณด๋‚˜์น˜๋Š” fibo 0 = 1 fibo 1 = 1 fibo n = fibo (n-1) + fibo (n-2) DAC ๊ทธ๋ž˜ํ”„์— ์ ์šฉ๋œ ํŠธ๋ฆฌ ์ˆœํšŒ๋กœ์„œ ๊ฐ€์žฅ ์•ˆ ์ข‹๋‹ค. ์ตœ์ ํ™”๋œ ๋ฒ„์ „์€ fibo n = let f a b m = if m = 0 then a if m = 1 then b f b (a+b) (m-1) in f 1 1 n ``` DAG ์ˆœํšŒ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์šด ์ข‹๊ฒŒ๋„ frontier ํฌ๊ธฐ๊ฐ€ ์ƒ์ˆ˜์ด๋ฉฐ ๋”ฐ๋ผ์„œ ๊ผฌ๋ฆฌ ์žฌ๊ท€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋œ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ณต๊ฐ„์„ ๋จน์–ด์น˜์šฐ๋Š” fold ์˜ˆ์ œ๋ฅผ ๋ณด์ž. ``` foldl (+) 0 [1.. 10] ``` ๋‹ค์Œ์€ ๊ต๋ฌ˜ํ•œ ๊ณต๊ฐ„ ๋ˆ„์ˆ˜ ์˜ˆ์ œ๋‹ค. ``` (\xs -> head xs + last xs) [1.. n] (\xs -> last xs + head xs) [1.. n] ์ฒซ ๋ฒˆ์งธ ๊ฒƒ์€ O(1) ๊ณต๊ฐ„์—์„œ ์‹คํ–‰๋˜์ง€๋งŒ ๋‘ ๋ฒˆ์งธ ๊ฒƒ์€ O(n)์—์„œ ์‹คํ–‰๋œ๋‹ค. ๊ณต์œ ์™€ CSE ๋…ธํŠธ: ์‹œ๊ฐ„์— ๊ด€ํ•œ ์ ˆ๊ณผ ์ค‘๋ณต๋จ. ๋ณ„๋„์˜ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ์ ˆ ํ•„์š”? ์–ด๋–ป๊ฒŒ ๊ณต์œ ํ•  ๊ฒƒ์ธ๊ฐ€ foo x y = -- s is not shared foo x = \y -> s + y where s = expensive x -- s is shared "lambda-lifting", "full laziness". ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ full laziness ๋ฉด์•ˆ ๋œ๋‹ค. ๊ณต๊ฐ„๊ณผ ์‹œ๊ฐ„ ์‚ฌ์ด์˜ ์ ˆ์ถฉ์— ๋Œ€ํ•œ ๊ณ ์ „์ ์ด๊ณ  ์ค‘์š”ํ•œ ์˜ˆ์ œ sublists [] = [[]] sublists (x:xs) = sublists xs ++ map (x:) (sublists xs) sublists' (x:xs) = let ys = sublists' xs in ys ++ map (x:) ys ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ common subexpression elimination์„ ์ตœ์ ํ™”๋กœ์„œ ์ˆ˜ํ–‰ํ•˜๋ฉด ์•ˆ ๋˜๋Š” ์ด์œ  (GHC๋Š” ํ•˜๊ณ  ์žˆ๋‚˜?) ๊ผฌ๋ฆฌ ์žฌ๊ท€ NOTE: ๊ณต๊ฐ„ ์ ˆ์— ๋‘๋Š” ๊ฒŒ ๋งž๋Š”๊ฐ€? ์Šคํƒ ๊ณต๊ฐ„์— ๊ด€ํ•œ ๊ฑฐ๋‹ˆ๊นŒ ๋งž๋Š” ๋“ฏ. ํ•˜์Šค์ผˆ์˜ ๊ผฌ๋ฆฌ ์žฌ๊ท€๋Š” ์ข€ ๋‹ค๋ฅธ ๊ฒƒ ๊ฐ™๋‹ค. ์‹œ๊ฐ„ ์ถ”๋ก  ์ง€์—ฐ ํ‰๊ฐ€ < ์ ๊ทน์  ํ‰๊ฐ€ ์‹คํ–‰ ์‹œ๊ฐ„์„ ์ถ”๋ก ํ•  ๋•Œ ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ๋ฅผ ์†์ˆ˜ ํ•ด๋ณด๊ณ  ์–ด๋ฆผ์ง์ž‘ํ•˜๋Š” ๊ฒƒ์€ ๋Œ€๋ถ€๋ถ„ ํ†ตํ•˜์ง€ ์•Š๋Š”๋‹ค. ์‚ฌ์‹ค ์ง€์—ฐ ํ‰๊ฐ€์— ์˜ํ•œ ํ‰๊ฐ€ ์ˆœ์„œ๋Š” ์‚ฌ๋žŒ์ด ์˜ˆ์ธกํ•˜๊ธฐ ์–ด๋ ค์›Œ์„œ, ์ธ์ž๋ฅผ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ธฐ ์ „์— normal form์œผ๋กœ ์†Œ๊ฑฐํ•˜๋Š” ์ ๊ทน์  ํ‰๊ฐ€ ๋‹จ๊ณ„๋ฅผ ์ถ”์ ํ•˜๋Š” ๊ฒŒ ๋” ์‰ฝ๋‹ค. ์ง€์—ฐ ํ‰๊ฐ€๋Š” ํ•ญ์ƒ ์ ๊ทน์  ํ‰๊ฐ€๋ณด๋‹ค ์†Œ๊ฑฐ ๋‹จ๊ณ„๊ฐ€ ์ ๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๊ฐ€ ์ ๊ทน์ ์œผ๋กœ ํ‰๊ฐ€๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์—ฌ ์†Œ๊ฑฐ ํšŸ์ˆ˜์˜ ์ƒํ•œ์„ ์‰ฝ๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์‹œ: or = foldr (||) False isPrime n = not $ or $ map (\k -> n `mod` k == 0) [2.. n-1] => ์ ๊ทน์  ํ‰๊ฐ€๋Š” ํ•ญ์ƒ n ๋‹จ๊ณ„. ์ง€์—ฐ ํ‰๊ฐ€๋Š” ์ด๋ณด๋‹ค ๋งŽ์ง€ ์•Š๋‹ค. ์ธ์ž ๋ฒ„๋ฆฌ๊ธฐ ์ธ์ž๋ฅผ normal form์œผ๋กœ ํ‰๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜์˜ ์‹œ๊ฐ„ ๋ฐ”์šด๋“œ๋Š” ์ •ํ•ด์ ธ ์žˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ๊ทธ ์ธ์ž๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค๋Š” ์„ฑ์งˆ์€ ํ‘œ๊ธฐ ์˜๋ฏธ๋ก ์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํฌ์ฐฉํ•  ์ˆ˜ ์žˆ๋‹ค. f โŠฅ = โŠฅ ๋‹จ WKNF ํ˜•ํƒœ์˜ ์ธ์ž์— ํ•œ์ •๋œ๋‹ค. ๋™์ž‘ ์˜๋ฏธ๋ก ์ ์œผ๋กœ๋Š” ๋น„์ข…๊ฒฐ -> ๋น„์ข…๊ฒฐ์ด๋‹ค. (์ด๊ฒƒ์€ ๊ทผ์‚ฌ์ผ ๋ฟ์ธ๋ฐ f anything = โŠฅ ์ด ๊ทธ ์ธ์ž๋ฅผ "ํ•„์š”"๋กœ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค) ๋น„ ์—„๊ฒฉ ํ•จ์ˆ˜๋Š” ์ž์‹ ์˜ ์ธ์ž๋ฅผ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์œผ๋ฉฐ time bound๊ฐ€ ๋ช…ํ™•ํ•˜์ง€ ์•Š๋‹ค. ํ•˜์ง€๋งŒ ํ•จ์ˆ˜๊ฐ€ ์—„๊ฒฉํ•œ์ง€ ์•„๋‹Œ์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋งŒ์œผ๋กœ๋„ ๋ถ„์„์— ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ผ ์ˆ˜ ์žˆ๋‹ค. isPrime n = not $ or $ (n `mod` 2 == 0) : (n `mod` 3 == 0) : ... or True โŠฅ = True๋ฅผ ์•„๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค. ๋‹ค๋ฅธ ์˜ˆ์‹œ๋“ค: foldr (:) [] ๋Œ€ foldl (flip (:)) []์—์„œ โŠฅ์˜ ์‚ฌ์šฉ head . mergesort๋ฅผ โŠฅ๋งŒ์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ด ์˜ˆ์‹œ๋Š” ๋„ˆ๋ฌด ๋ณต์žกํ•˜๋ฉฐ ์ง€์—ฐ์„ฑ์—์„œ ๋‹ค๋ฃจ๋Š” ๊ฒŒ ์ ์ ˆํ•˜๋‹ค. ์˜์†์„ฑ & ๊ฐ๊ฐ€์ƒ๊ฐ NOTE: this section is better left to a data structures chapter because the subsections above cover most of the cases a programmer not focussing on data structures / amortization will encounter. Persistence = no updates in place, older versions are still there. Amortisation = distribute unequal running times across a sequence of operations. Both don't go well together in a strict setting. Lazy evaluation can reconcile them. Debit invariants. Example: incrementing numbers in binary representation. ๊ทธ๋ž˜ํ”„ ์†Œ๊ฑฐ์˜ ๊ตฌํ˜„ Small talk about G-machines and such. Main definition: closure = thunk = code/data pair on the heap. What do they do? Consider {\displaystyle (\lambda x.\lambda y.x+y) 2} (\lambda x.\lambda y.x+y) 2. This is a function that returns a function, namely {\displaystyle \lambda y.2+y} \lambda y.2+y in this case. But when you want to compile code, it's prohibitive to actually perform the substitution in memory and replace all occurrences of {\displaystyle x} x by 2. So, you return a closure that consists of the function code {\displaystyle \lambda y.x+y} \lambda y.x+y and an environment {\displaystyle {x=2}} {x=2} that assigns values to the free variables appearing in there. GHC (?, most Haskell implementations?) avoid free variables completely and use supercombinators. In other words, they're supplied as extra-parameters and the observation that lambda-expressions with too few parameters don't need to be reduced since their WHNF is not very different. Note that these terms are technical terms for implementation stuff, lazy evaluation happily lives without them. Don't use them in any of the sections above. ๋ ˆํผ๋Ÿฐ์Šค Bird, Richard (1998). Introduction to Functional Programming using Haskell. Prentice Hall. ISBN 0-13-484346-0. Peyton-Jones, Simon (1987). The Implementation of Functional Programming Languages. Prentice Hall. John Maraist, Martin Odersky, and Philip Wadler (May 1998). "The call-by-need lambda calculus". Journal of Functional Programming 8 (3): 257-317. โ†ฉ 4 Laziness https://en.wikibooks.org/wiki/Haskell/Laziness ์„œ๋ฌธ ๋น„ ์—„๊ฒฉ์„ฑ(nonstrictness) ๋Œ€ ์ง€์—ฐ์„ฑ ์ฝํฌ์™€ weak head normal form ์ง€์—ฐ ํ•จ์ˆ˜์™€ ์—„๊ฒฉ ํ•จ์ˆ˜ ๋ธ”๋ž™๋ฐ•์Šค ์—„๊ฒฉ์„ฑ ๋ถ„์„ ๋น„ ์—„๊ฒฉ ์˜๋ฏธ๋ก ์˜ ๋ฌธ๋งฅ์—์„œ ์‚ฌ๋ฌผ์— ๋Œ€ํ•œ ํ‘œ๊ธฐ์  ๊ด€์  ๊ฒŒ์œผ๋ฅธ ํŒจํ„ด ๋งค์นญ ์–ธ์ œ ์ง€์—ฐ ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ๋น„ ์—„๊ฒฉ ์˜๋ฏธ๋ก ์˜ ์žฅ์  ์‹œ๊ฐ„ ํŽ˜๋„ํ‹ฐ ์—†๋Š” ์ฃผ์˜ ๋ถ„์‚ฐ(Separation of concerns without time penalty) ๊ฐœ์„ ๋œ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ ๋ฌดํ•œ ์ž๋ฃŒ๊ตฌ์กฐ ํ”ํ•œ ๋น„ ์—„๊ฒฉ ๊ด€์šฉ๊ตฌ Tying the knot Memoization, Sharing and Dynamic Programming ์ง€์—ฐ์„ฑ์— ๋Œ€ํ•œ ๊ฒฐ๋ก  ๋…ธํŠธ ๋ ˆํผ๋Ÿฐ์Šค ๊ณ ์ƒํ•˜๋ฉด ๊ฒฐ๊ตญ ๋ณด์ƒ๋ฐ›์ง€๋งŒ ๊ฒŒ์œผ๋ฆ„์€ ์ง€๊ธˆ ๋ณ€์ƒํ•ด์•ผ ํ•œ๋‹ค! - Steven Wright ์„œ๋ฌธ ์ด์ œ ์—ฌ๋Ÿฌ๋ถ„์€ ํ•˜์Šค์ผˆ์ด ์ง€์—ฐ ํ‰๊ฐ€๋ฅผ ํ™œ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ๋‹ค. ์–ด๋–ค ๊ฒƒ๋„ ํ•„์š”ํ•˜๊ธฐ ์ „์—๋Š” ํ‰๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ "ํ•„์š”ํ•˜๊ธฐ ์ „"์ด ์ •ํ™•ํžˆ ๋ฌด์Šจ ๋œป์ธ๊ฐ€? ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ์ง€์—ฐ ํ‰๊ฐ€๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ (๊ทธ ์•ˆ์— ์–ด๋–ค ํ‘๋งˆ์ˆ ์ด ์žˆ๋Š”์ง€), ์ด๊ฒƒ์ด ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋œปํ•˜๋Š” ๋ฐ”๊ฐ€ ๋ฌด์—‡์ธ์ง€, ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ• ์ง€ ์•Œ์•„๋ณธ๋‹ค. ๋จผ์ € ์ง€์—ฐ ํ‰๊ฐ€๋ฅผ ํ•˜๋Š” ์ด์œ ์™€ ์ง€์—ฐ ํ‰๊ฐ€๊ฐ€ ๋‚ดํฌํ•˜๋Š” ๋ฐ”๋ฅผ ๊ณ ๋ คํ•ด ๋ณด์ž. ์–ธ๋œป ๋ณด๋ฉด ์ง€์—ฐ ํ‰๊ฐ€๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์„ ๋” ํšจ์œจ์ ์œผ๋กœ ๋งŒ๋“ ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์–ด์จŒ๋“  ์•„๋ฌด๊ฒƒ๋„ ์•ˆ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํšจ์œจ์ ์ผ ์ˆ˜๋Š” ์—†์ง€ ์•Š๋‚˜? ์‚ฌ์‹ค์€ ์ง€์—ฐ์„ฑ์ด ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ฝ”๋“œ๋ฅผ ๋” ์—„๊ฒฉํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์„ ๋ฐฉํ•ดํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์ง€์—ฐ์˜ ์ง„์งœ ์ด์ ์€ ์˜ฌ๋ฐ”๋ฅธ ๊ฒƒ์„ ์ถฉ๋ถ„ํžˆ ํšจ์œจ์ ์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐ ์žˆ๋‹ค. ์ง€์—ฐ ํ‰๊ฐ€๋Š” ์—„๊ฒฉํ•œ ํ™˜๊ฒฝ์—์„œ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋ณด๋‹ค ๋‹จ์ˆœํ•˜๊ณ  ์šฐ์•„ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. ๋น„ ์—„๊ฒฉ์„ฑ(nonstrictness) ๋Œ€ ์ง€์—ฐ์„ฑ ์ง€์—ฐ์„ฑ๊ณผ ๋น„ ์—„๊ฒฉ์„ฑ์€ ๋ฏธ๋ฌ˜ํ•˜๊ฒŒ ๋‹ค๋ฅด๋‹ค. ๋น„ ์—„๊ฒฉ ์˜๋ฏธ๋ก ์€ ์—ฌ๋Ÿฌ๋ถ„์ด ๋ฏฟ์„ ์ˆ˜ ์žˆ๋Š” ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์˜ ์–ด๋–ค ์„ฑ์งˆ์„ ์ง€์นญํ•œ๋‹ค. ์–ด๋–ค ๊ฒƒ๋„ ํ•„์š”ํ•˜๊ธฐ ์ „์—๋Š” ํ‰๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค. ์ง€์—ฐ ํ‰๊ฐ€๋Š” ๋‹ค์Œ ์ ˆ์—์„œ ์„ค๋ช…ํ•  ์ฝํฌthunk๋ผ๋Š” ์ˆ˜๋‹จ์„ ํ†ตํ•ด ๋น„ ์—„๊ฒฉ์„ฑ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ํ•˜์ง€๋งŒ ์ด ๋‘ ๊ฐœ๋…์€ ์›Œ๋‚™ ๋ฐ€์ ‘ํ•˜๊ฒŒ ์—ฐ๊ด€๋˜์–ด ์žˆ์–ด์„œ ํ•จ๊ป˜ ์„ค๋ช…ํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค. ์ฝํฌ๋ฅผ ์•Œ๋ฉด ๋น„ ์—„๊ฒฉ์„ฑ์„ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๊ณ  ๋น„ ์—„๊ฒฉ์„ฑ์˜ ์˜๋ฏธ๋ก ์€ ์• ์ดˆ์— ์™œ ์ง€์—ฐ ํ‰๊ฐ€๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€๋ฅผ ์„ค๋ช…ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๊ฐœ๋…๋“ค์„ ๋™์‹œ์— ์†Œ๊ฐœํ•˜๋ฉฐ ํ˜ผ์šฉ์„ ํ”ผํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. (์šฉ์–ด๋Š” ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ตฌ๋ถ„ํ•˜๊ฒ ์ง€๋งŒ) ์ฝํฌ์™€ weak head normal form ํ•˜์Šค์ผˆ์—์„œ ํ”„๋กœ๊ทธ๋žจ์ด ์–ด๋–ป๊ฒŒ ์‹คํ–‰๋˜๋Š”์ง€ ์•Œ๋ ค๋ฉด ๋‘ ์›์น™์„ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ๋น„ ์—„๊ฒฉ์„ฑ์˜ ์„ฑ์งˆ์ด๋‹ค. ํ‰๊ฐ€๋Š” ์ตœ์†Œํ•œ์œผ๋กœ ํ•˜๊ณ  ์ตœ๋Œ€ํ•œ ์ง€์—ฐํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ํ•˜์Šค์ผˆ์˜ ๊ฐ’๋“ค์€ ๊ณ ๋„๋กœ ์ธต์„ ์ด๋ฃจ์–ด ๊ฐ’์„ 'ํ‰๊ฐ€ํ•˜๋Š”' ๊ฒƒ์€ ์—ฌ๋Ÿฌ ์ธต ์ค‘ ํ•˜๋‚˜๋ฅผ ํ‰๊ฐ€ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. ์Œ์„ ์ด์šฉํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž. let (x, y) = (length [1.. 5], reverse "olleh") in ... in ๋ถ€๋ถ„์—์„œ x์™€ y๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์• ์ดˆ์— ์ด let ๋ฐ”์ธ๋”ฉ์„ ํ‰๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ์šฐ๋ณ€์ด undefined ์ผ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, in ๋ถ€๋ถ„์ด x ๋˜๋Š” y๋ฅผ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ์—๋„ ์ž‘๋™ํ–ˆ์„ ๊ฒƒ์ด๋‹ค. ์ด ๊ฐ€์ •์€ ์ด ์ ˆ์˜ ๋ชจ๋“  ์˜ˆ์ œ์— ์ ์šฉ๋œ๋‹ค. ์šฐ๋ฆฌ๋Š” x์— ๋Œ€ํ•ด ๋ฌด์—‡์„ ์•„๋Š”๊ฐ€? ๊ณ„์‚ฐํ•ด ๋ณด๋ฉด x๋Š” ๋ฐ˜๋“œ์‹œ 5์ด๊ณ  y๋Š” "hello"์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ฒซ ๋ฒˆ์งธ ์›์น™์„ ๊ธฐ์–ตํ•˜์ž. ํ•„์š”ํ•˜๊ธฐ ์ „์—๋Š” length์™€ reverse์˜ ํ˜ธ์ถœ์„ ํ‰๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ x์™€ y๊ฐ€ ์ฝํฌ๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ฆ‰ ์ด๊ฒƒ๋“ค์€ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์ฃผ๋Š” ์„ค๋ช…์„œ๊ฐ€ ๋™๋ด‰๋œ ํ‰๊ฐ€๋˜์ง€ ์•Š์€ ๊ฐ’ ๋“ค์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด x์˜ ์„ค๋ช…์„œ๋Š” 'length [1.. 5]๋ฅผ ํ‰๊ฐ€ํ•˜์‹œ์˜ค'์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์šฐ๋ฆฌ๋Š” ์ขŒ๋ณ€์—์„œ ์ผ์ข…์˜ ํŒจํ„ด ๋งค์นญ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ๊ฑธ ์ œ๊ฑฐํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? let z = (length [1.. 5], reverse "olleh") in ... ์šฐ๋ฆฌ์—๊ฒŒ๋Š” z๊ฐ€ ์Œ์ด๋ผ๋Š” ๊ฒƒ์ด ๋ช…๋ฐฑํ•˜์ง€๋งŒ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋ณด๊ธฐ์— ์šฐ๋ฆฌ๋Š” '=' ๊ธฐํ˜ธ ์šฐ๋ณ€์˜ ๊ฐ’์„ ๋ถ„ํ•ดํ•˜๋ ค๋Š” ๋…ธ๋ ฅ์„ ์ „ํ˜€ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๊ทธ๊ฒŒ ๋ฌด์—‡์ด๋“  ์ „ํ˜€ ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š๋Š”๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” z ์ž์ฒด๋ฅผ ์ฝํฌ๋กœ ๋งŒ๋“ ๋‹ค. ๋‚˜์ค‘์— ์šฐ๋ฆฌ๊ฐ€ z๋ฅผ ์‚ฌ์šฉํ•˜๋ ค ํ•œ๋‹ค๋ฉด ๋‘ ๊ตฌ์„ฑ์š”์†Œ ์ค‘ ํ•˜๋‚˜ ๋˜๋Š” ๋‘˜ ๋‹ค ํ•„์š”ํ•˜๊ฒŒ ๋  ํ…Œ๊ณ , ๋”ฐ๋ผ์„œ z๋ฅผ ํ‰๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ง€๊ธˆ ๋‹น์žฅ์€ z๊ฐ€ ์ฝํฌ์—ฌ๋„ ๋œ๋‹ค. ์•ž์„œ ํ•˜์Šค์ผˆ์˜ ๊ฐ’์€ ์ธต์„ ์ด๋ฃฌ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ z์— ๋Œ€ํ•ด ํŒจํ„ด ๋งค์นญ์„ ํ•˜๋ฉด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. let z = (length [1.. 5], reverse "olleh") (n, s) = z in ... ์ฒซ ๋ฒˆ์งธ ์ค„์ด ์‹คํ–‰๋œ ํ›„ z๋Š” ๋‹จ์ˆœํžˆ ์ฝํฌ๋‹ค. z๊ฐ€ ์–ด๋–ค ์ข…๋ฅ˜์˜ ๊ฐ’์ธ์ง€๋Š” ๋ชจ๋ฅด๋Š”๋ฐ, ์•„์ง ์•Œ์•„๋ณด๋ผ๋Š” ์š”์ฒญ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ๋‘ ๋ฒˆ์งธ ์ค„์—์„œ๋Š” ์Œ ํŒจํ„ด์„ ํ†ตํ•ด z์— ๋Œ€ํ•ด ํŒจํ„ด ๋งค์นญ์„ ํ•œ๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” 'z์— ํŒจํ„ด์ด ๋งค์นญํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ๊ฒ ๋Š”๋ฐ, ๊ทธ๋Ÿฌ๋ ค๋ฉด z๊ฐ€ ์Œ์ธ์ง€ ํ™•์‹คํžˆ ํ•ด์•ผ ํ•ด'๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์กฐ์‹ฌํ•˜์ž. ์šฐ๋ฆฌ๋Š” ์•„์ง ๊ตฌ์„ฑ์š”์†Œ ๋ถ€๋ถ„(length์™€ reverse ํ˜ธ์ถœ)์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š์•˜๋‹ค. ๋”ฐ๋ผ์„œ ์ด๊ฒƒ๋“ค์€ ํ‰๊ฐ€ํ•˜์ง€ ์•Š์€ ์ฑ„ ๋†”๋‘˜ ์ˆ˜ ์žˆ๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜์ž๋ฉด ๋ฐฉ๊ธˆ๊นŒ์ง€ ์ฝํฌ์˜€๋˜ z๋Š” (์ฝํฌ, ์ฝํฌ) ๊ฐ™์€ ๊ฑธ๋กœ ํ‰๊ฐ€๋œ ๊ฒƒ์ด๊ณ , n๊ณผ s๋Š” ์ฝํฌ์ธ๋ฐ ํ‰๊ฐ€๋˜๊ณ  ๋‚˜๋ฉด ์›๋ž˜ z์˜ ๊ตฌ์„ฑ์š”์†Œ ๋ถ€๋ถ„๋“ค๋กœ ๋ณ€ํ•  ๊ฒƒ์ด๋‹ค. ์ข€ ๋” ๋ณต์žกํ•œ ํŒจํ„ด ๋งค์นญ์„ ํ•ด๋ณด์ž. let z = (length [1.. 5], reverse "olleh") (n, s) = z 'h':ss = s in ... z์˜ ๋‘ ๋ฒˆ์งธ ์„ฑ๋ถ„์— ๋Œ€ํ•œ ํŒจํ„ด ๋งค์นญ์€ ํ‰๊ฐ€๋ฅผ ์œ ๋ฐœํ•œ๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” 'h':ss ํŒจํ„ด์ด ์Œ์˜ ๋‘ ๋ฒˆ์งธ ์„ฑ๋ถ„์— ๋งค์นญํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ ์ž ํ•˜๊ณ , ๋”ฐ๋ผ์„œ s์˜ ์ตœ์ƒ์œ„ ์ธต์„ ํ‰๊ฐ€ํ•ด์„œ cons ์…€ s = *thunk* : *thunk* ์ธ์ง€ ํ™•์ธํ•œ๋‹ค. (s๊ฐ€ ๋นˆ ๋ฆฌ์ŠคํŠธ์˜€๋‹ค๋ฉด ์ด ์‹œ์ ์—์„œ ํŒจํ„ด ๋งค์นญ ์‹คํŒจ ์˜ค๋ฅ˜๋ฅผ ๋งŒ๋‚ฌ์„ ๊ฒƒ์ด๋‹ค.) ๋ฐฉ๊ธˆ ๋ฐํ˜€๋‚ธ ์ฒซ ๋ฒˆ์งธ ์ฝํฌ๋ฅผ ํ‰๊ฐ€ํ•ด 'h':ss = 'h' : *thunk* ์ธ์ง€ ํ™•์ธํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋‚˜๋จธ์ง€๋Š” ํ‰๊ฐ€๋˜์ง€ ์•Š์€ ์ฑ„ ์œ ์ง€๋˜๊ณ  ss๋Š” ์ฝํฌ๊ฐ€ ๋˜๋ฉฐ, ํ‰๊ฐ€๋œ๋‹ค๋ฉด ์ด ๋ฆฌ์ŠคํŠธ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์ด ๋  ๊ฒƒ์ด๋‹ค. ๊ฐ’ (4, [1, 2])๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ํ‰๊ฐ€ํ•˜๋Š” ๊ณผ์ •. ์ฒซ ๋‹จ๊ณ„์—์„œ๋Š” ์ „ํ˜€ ํ‰๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค. ์—ฐ์ด์€ ํ˜•ํƒœ๋“ค์€ WHNF ํ˜•ํƒœ์ด๋ฉฐ ๋งˆ์ง€๋ง‰์€ normal form ํ˜•ํƒœ๋‹ค. ๊ฑฐ์˜ ๋ชจ๋“  ํ•˜์Šค ์ผˆ ๊ฐ’์€ '๋ถ€๋ถ„ ํ‰๊ฐ€'๋  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ ๋‹จ์œ„์— ๋Œ€ํ•œ ๊ฐœ๋…๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์Œ์˜ ์ฝํฌ๋ฅผ ์ตœ์†Œํ•œ๋งŒ ํ‰๊ฐ€ํ•˜๋ฉด ๋‘ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ํ‰๊ฐ€ํ•˜์ง€ ์•Š์€ ์Œ ์ƒ์„ฑ์ž (*thunk*, *thunk*) ๊ฐ€ ๋‚˜์˜จ๋‹ค. ๋ฆฌ์ŠคํŠธ๋ฅผ ์ตœ์†Œํ•œ ํ‰๊ฐ€ํ•˜๋ฉด cons cell *thunk* : *thunk* ๋˜๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ []๊ฐ€ ๋‚˜์˜จ๋‹ค. ๋นˆ ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ์—๋Š” ๊ทธ ๊ฐ’์„ ๋” ์ด์ƒ ํ‰๊ฐ€ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ normal form ํ˜•ํƒœ๋ผ๊ณ  ์นญํ•œ๋‹ค. ๊ทธ ์‚ฌ์ด์˜ ๋‹จ๊ณ„์—์„œ ๊ฐ’์˜ ์ผ๋ถ€๋ผ๋„ ํ‰๊ฐ€ํ•œ ์ƒํƒœ๋ฉด weak head normal form (WHNF) ํ˜•ํƒœ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ('head normal form'์ด๋ผ๋Š” ๊ฒƒ๋„ ์žˆ์œผ๋‚˜ ํ•˜์Šค์ผˆ์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค) WNHF ํ˜•ํƒœ์˜ ๋ฌด์–ธ๊ฐ€๋ฅผ ์™„์ „ํžˆ ํ‰๊ฐ€ํ•˜๋ฉด normal form์œผ๋กœ ํ™˜์›๋œ๋‹ค. ์–ด๋Š ์‹œ์ ์— z๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค๋ฉด z๋ฅผ ์™„์ „ํžˆ ํ‰๊ฐ€ํ•ด์„œ length์™€ reverse๋„ ํ˜ธ์ถœํ•˜์—ฌ normal form์ธ (5, "hello")๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๊ฐ’์„ ์กฐ๊ธˆ์ด๋ผ๋„ ํ‰๊ฐ€ํ•˜๋Š” ํ–‰์œ„๋ฅผ ๋‘๊ณ  ๊ทธ ๊ฐ’์„ ๊ฐ•์ œํ•œ๋‹ค(force)๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค. ์–ด๋–ค ๊ฐ’๋“ค์€ ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์˜ค์ง ํ•˜๋‚˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ •์ˆ˜๋ฅผ ๋ถ€๋ถ„ ํ‰๊ฐ€ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ์ •์ˆ˜๋Š” ์ฝํฌ์ด๊ฑฐ๋‚˜ normal form์ด๋‹ค. ์—„๊ฒฉํ•œ ๊ตฌ์„ฑ์š”์†Œ(data MaybeS a = NothingS | JustS ! a์ฒ˜๋Ÿผ ๋Š๋‚Œํ‘œ๊ฐ€ ๋ถ™์€ ๊ฒƒ)๋ฅผ ๊ฐ€์ง€๋Š” ์ƒ์„ฑ์ž์˜ ๊ฒฝ์šฐ, ์ด๋Ÿฐ ๊ตฌ์„ฑ์š”์†Œ๋Š” ์ƒ์œ„ ๋‹จ๊ณ„๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ์ˆœ๊ฐ„ ๊ทธ ์ž์‹ ๋„ ํ‰๊ฐ€๋œ๋‹ค. JustS *thunk* ๊ฐ™์€ ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด ๋‹จ๊ณ„์— ๋„๋‹ฌํ•˜๋ฉด JustS์˜ ๊ตฌ์„ฑ์š”์†Œ์— ๋Œ€ํ•œ ์—„๊ฒฉํ•จ ํ‘œ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๊ตฌ์„ฑ์š”์†Œ์˜ ํ‰๊ฐ€๋„ ๊ฐ•์ œ๋œ๋‹ค. ์ด๋ฒˆ ์ ˆ์—์„œ๋Š” ์ง€์—ฐ์„ฑ์˜ ๊ธฐ๋ณธ์„ ์•Œ์•„๋ดค๋‹ค. ์–ด๋–ค ๊ฒƒ๋„ ํ•„์š”ํ•˜๊ธฐ ์ „์—๋Š” ํ‰๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค. (์‚ฌ์‹ค ํ•˜์Šค ์ผˆ ๊ฐ’์ด ํ‰๊ฐ€๋˜๋Š” ์œ ์ผํ•œ ์žฅ์†Œ๋Š” ํŒจํ„ด ๋งค์นญ๊ณผ ํŠน์ • ์›์‹œ IO ํ•จ์ˆ˜๋“ค ๋‚ด๋ถ€๋ฟ์ด๋‹ค) ์ด ์›์น™์€ ๊ฐ’์„ ํ‰๊ฐ€ํ•  ๋•Œ๋„ ์ ์šฉ๋œ๋‹ค. ๊ฐ’์„ ํ‰๊ฐ€ํ•  ๋•Œ๋Š” ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ์ž‘์—…๋งŒ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ง€์—ฐ ํ•จ์ˆ˜์™€ ์—„๊ฒฉ ํ•จ์ˆ˜ ํ•จ์ˆ˜๋Š” '์ธ์ž์— ๊ด€ํ•ด์„œ' ๊ฒŒ์œผ๋ฅด๊ฑฐ๋‚˜ ์—„๊ฒฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํ•จ์ˆ˜๋Š” ์ธ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•ด์•ผ ํ•˜๋ฉฐ ๋”ฐ๋ผ์„œ ์ธ์ž๋“ค์„ ์—ฌ๋Ÿฌ ์ˆ˜์ค€์œผ๋กœ ํ‰๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด length๋Š” ์ „๋‹ฌ๋ฐ›์€ ์ธ์ž์˜ cons cell๋“ค๋งŒ ํ‰๊ฐ€ํ•˜๋ฉด ๋˜๋ฉฐ cons cell๋“ค์˜ ๋‚ด์šฉ๋ฌผ์€ ํ‰๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. length *thunk*๋Š” length (*thunk* : *thunk* : *thunk* : []) ๊ฐ™์€ ํ˜•ํƒœ๋กœ ํ‰๊ฐ€๋œ ๋‹ค์Œ 3์œผ๋กœ ํ‰๊ฐ€๋  ๊ฒƒ์ด๋‹ค. (length . show) ๊ฐ™์€ ํ•จ์ˆ˜๋Š” ๊ทธ ์ธ์ž๋“ค์„ ์™„์ „ํžˆ ํ‰๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. length $ show *thunk*์˜ ๊ฒฝ์šฐ ๊ทธ ์ฝํฌ๋ฅผ normal form์œผ๋กœ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ ์™ธ์—๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์—†๋‹ค. ์–ด๋–ค ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ณด๋‹ค ๊ทธ ์ธ์ž๋“ค์„ ๋” ๊นŠ์ด ํ‰๊ฐ€ํ•œ๋‹ค. ์ธ์ž๊ฐ€ ํ•˜๋‚˜์ธ ๋‘ ํ•จ์ˆ˜ f์™€ g์— ๋Œ€ํ•ด, f x๊ฐ€ g x๋ณด๋‹ค x๋ฅผ ๋” ๊นŠ์€ ์ˆ˜์ค€๊นŒ์ง€ ํ‰๊ฐ€ํ•œ๋‹ค๋ฉด f๋Š” g๋ณด๋‹ค ์—„๊ฒฉํ•˜๋‹ค. ๊ฐ€๋”์€ WNHF๋งŒ์„ ๊ณ ๋ คํ•˜์—ฌ ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ๊ทธ ์ธ์ž๋ฅผ ์ตœ์†Œํ•œ WNHF๋กœ ํ‰๊ฐ€ํ•œ๋‹ค๋ฉด ์—„๊ฒฉํ•˜๋‹ค๊ณ  ๋ถ€๋ฅด๊ณ  ์•„๋ฌด ํ‰๊ฐ€๋„ ํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜๋Š” ๊ฒŒ์œผ๋ฅด๋‹ค๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ธ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ์–ด๋–ค ์ธ์ž์— ๋Œ€ํ•ด์„œ๋Š” ์—„๊ฒฉํ•˜๊ณ  ๋‹ค๋ฅธ ์ธ์ž์— ๋Œ€ํ•ด์„œ๋Š” ๊ฒŒ์œผ๋ฅด๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜์—์„œ, f x y = length $ show x y๋Š” ํ‰๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์ „ํ˜€ ์—†์ง€๋งŒ x๋Š” normal form์œผ๋กœ ์™„์ „ํžˆ ํ‰๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ f๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž์— ๋Œ€ํ•ด์„œ๋Š” ์—„๊ฒฉํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ ์ธ์ž์— ๋Œ€ํ•ด์„œ๋Š” ๊ฒŒ์œผ๋ฅด๋‹ค. ์—ฐ์Šต๋ฌธ์ œ f x y = show x์—์„œ ์™œ x๋ฅผ ์™„์ „ํžˆ ํ‰๊ฐ€ํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ๋‹ค์Œ ์ค‘ ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ๋” ์—„๊ฒฉํ•œ๊ฐ€? f x = length [head x] g x = length (tail x) fold์— ๊ด€ํ•œ ๊ธฐ์กด ๋…ผ์˜์—์„œ foldl์˜ ๋ฉ”๋ชจ๋ฆฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ ๊ทน ํ‰๊ฐ€๋˜๋Š” foldl'์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ๊ด€ํ•ด ๋…ผ์˜ํ•œ ์ ์ด ์žˆ๋‹ค. ๋ณธ์งˆ์„ ๋ณด๋ฉด foldr (:) []์™€ foldl (flip (:)) []๋Š” ๊ทธ ์ธ์ž๋“ค์„ ๊ฐ™์€ ์ˆ˜์ค€์˜ ์—„๊ฒฉํ•จ์œผ๋กœ ํ‰๊ฐ€ํ•˜์ง€๋งŒ foldr์€ ๊ฐ’๋“ค์„ ๋ฐ”๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ฉด foldl์€ cons cell๋“ค์„ ๋๊นŒ์ง€ ํ‰๊ฐ€ํ•œ ๋‹ค์Œ์—์•ผ ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ค์–ด๋‚ด๊ธฐ ์‹œ์ž‘ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์—„๊ฒฉํ•จ์ด ๊ฐ€์น˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๋ธ”๋ž™๋ฐ•์Šค ์—„๊ฒฉ์„ฑ ๋ถ„์„ ์ธ์ž๊ฐ€ ํ•˜๋‚˜์ธ ํ•จ์ˆ˜ f๋ฅผ ์ƒ์ƒํ•ด ๋ณด์ž. f์˜ ์†Œ์Šค ์ฝ”๋“œ๋Š” ๋ณผ ์ˆ˜ ์—†์ง€๋งŒ f๊ฐ€ ์—„๊ฒฉํ•œ์ง€ ์•„๋‹Œ์ง€ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? ์•„๋งˆ ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ํ‘œ์ค€ ํ”„๋ ๋ฅ˜๋“œ ๊ฐ’์ธ undefined๋ฅผ ์จ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. undefined๋ฅผ ์กฐ๊ธˆ์ด๋ผ๋„ ํ‰๊ฐ€ํ•  ๊ฒƒ์„ ๊ฐ•์ œํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ๋ฉˆ์ถ”๊ณ  ์˜ค๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ์€ ๋ชจ๋‘ ์˜ค๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. let (x, y) = undefined in x length undefined head undefined JustS undefined -- ์•ž์„  ์ ˆ์—์„œ ์ •์˜ํ–ˆ๋˜ MaybeS ๋”ฐ๋ผ์„œ ์—„๊ฒฉํ•œ ํ•จ์ˆ˜์— undefined๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•  ๊ฒƒ์ด๋‹ค. ๊ฒŒ์œผ๋ฅธ ํ•จ์ˆ˜์— undefined๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•˜์ง€ ์•Š๊ณ  ์ •์ƒ์ ์œผ๋กœ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ์ค‘ ์–ด๋Š ๊ฒƒ๋„ ์˜ค๋ฅ˜๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค. let (x, y) = (4, undefined) in x length [undefined, undefined, undefined] head (4 : undefined) Just undefined ์ฆ‰ f๊ฐ€ ์—„๊ฒฉํ•œ ํ•จ์ˆ˜์ผ ํ•„์š”์ถฉ๋ถ„์กฐ๊ฑด์€ f undefined๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋น„ ์—„๊ฒฉ ์˜๋ฏธ๋ก ์˜ ๋ฌธ๋งฅ์—์„œ ์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•œ ๊ฒƒ์€ id ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ๊ณ ๋ คํ•˜๋ฉด ๋ง์ด ์•ˆ ๋˜๊ธฐ ์‹œ์ž‘ํ•œ๋‹ค. id๋Š” ์—„๊ฒฉํ•œ๊ฐ€? ์•„๋งˆ "์•„๋‹ˆ, id๋Š” ์ธ์ž๋ฅผ ํ‰๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ฒŒ์œผ๋ฅด๋‹ค" ๊ฐ™์€ ๋ฐ˜์‘์ด ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์•ž์˜ ๋ธ”๋ž™๋ฐ•์Šค ์—„๊ฒฉ์„ฑ ๋ถ„์„์„ id์— ์ ์šฉํ•ด ๋ณด์ž. ๋ถ„๋ช…ํžˆ id undefined๋Š” ์˜ค๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ค‘๋‹จ์‹œํ‚จ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด id๋Š” ์—„๊ฒฉํ•˜๋‹ค๊ณ  ํ•ด์•ผ ํ•˜์ง€ ์•Š์„๊นŒ? ์ด๋Ÿฐ ํ˜ผ๋ž€์€ ํ•˜์Šค์ผˆ์˜ ๋น„ ์—„๊ฒฉ ์˜๋ฏธ๋ก ์ด ์ด๋Ÿฐ ๋ฌธ์ œ ์ „์ฒด๋ฅผ ๋” ์• ๋งคํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋น„ ์—„๊ฒฉ์„ฑ์— ๋”ฐ๋ฅด๋ฉด ์–ด๋Š ๊ฒƒ๋„ ํ•„์š”ํ•˜๊ธฐ ์ „์—๋Š” ํ‰๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ์—์„œ length undefined๋Š” ํ‰๊ฐ€๋ ๊นŒ? [4, 10, length undefined, 12] ์ด ์ฝ”๋“œ๋ฅผ GHCi์— ์ณ๋ณด๋ฉด ์—„๊ฒฉํ•ด ๋ณด์ธ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ์˜ค๋ฅ˜๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ์˜ ์งˆ๋ฌธ์—๋Š” ํ•จ์ •์ด ์žˆ๋‹ค. ์ด ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•˜์ง€ ์•Š๋Š” ํ•œ, ์ด ๊ฐ’์ด ํ‰๊ฐ€๋˜๋Š”์ง€๋ฅผ ๋…ผ์˜ํ•˜๋Š” ๊ฒƒ์€ ๋ง์ด ๋˜์ง€ ์•Š๋Š”๋‹ค. GHCi์— head [1, 2, 3]์„ ์ž…๋ ฅํ•  ๋•Œ ์ด ๊ฐ’์„ ํ‰๊ฐ€ํ•  ์ด์œ ๋Š” GHCi๊ฐ€ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. [4, 10, length undefined, 12]๋ฅผ ์ž…๋ ฅํ•˜๋ฉด GHCi๋Š” ๊ทธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ normal form์œผ๋กœ ํ‰๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์ธ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” main์—์„œ IO๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š” ํ•œ ์•„๋ฌด๊ฒƒ๋„ ํ‰๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ์–ด๋–ค ๊ฒƒ์ด ํ‰๊ฐ€๋˜๋Š”์ง€ ์•ˆ๋˜๋Š”์ง€ ๋งํ•˜๋Š” ๊ฒƒ์€ ์ด๊ฒƒ์ด ์ „๋‹ฌ๋˜๋Š” ์ƒ์œ„ ์ˆ˜์ค€์„ ์ณ๋‹ค๋ณด์ง€ ์•Š์œผ๋ฉด ์˜๋ฏธ๊ฐ€ ์—†๋‹ค. ์—ฌ๊ธฐ์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ตํ›ˆ์€ GHCi๋ฅผ ๋งน์‹ ํ•˜์ง€ ๋ง๋ผ๋Š” ๊ฒƒ์ด๋‹ค. GHCi์˜ ๋ชจ๋“  ๊ฒƒ์€ IO๋ฅผ ๊ฑฐ์น˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์–ธ์ œ "f x๊ฐ€ x๋ฅผ ๊ฐ•์ œํ•œ๋‹ค"๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ด ๋ง์ด ์ •๋ง๋กœ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” "f x๋ฅผ ๊ฐ•์ œํ•  ๋•Œ x๋Š” ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ•์ œ๋˜๋Š”๊ฐ€"์ด๋‹ค. ์ด์ œ id๋กœ ์ฃผ์˜๋ฅผ ๋Œ๋ ค๋ณด์ž. id x๋ฅผ normal form์œผ๋กœ ๊ฐ•์ œํ•˜๋ฉด x๋Š” normal form์ด ๊ฐ•์ œ๋˜๋ฏ€๋กœ id๋Š” ์—„๊ฒฉํ•˜๋‹ค๊ณ  ๊ฒฐ๋ก  ๋‚ด๋ฆด ์ˆ˜ ์žˆ๋‹ค. id ์ž์ฒด๋Š” ์ธ์ž๋ฅผ ํ‰๊ฐ€ํ•˜์ง€ ์•Š๊ณ  ํ˜ธ์ถœ์ž์—๊ฒŒ ๊ทธ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋Š” ๊ทธ๋Ÿฐ ์ƒํ™ฉ์„ ๋ณด์—ฌ์ค€๋‹ค. -- We evaluate the right-hand of the let-binding to WHNF by pattern-matching -- against it. let (x, y) = undefined in x -- Error, because we force undefined. let (x, y) = id undefined in x -- Error, because we force undefined. id๋Š” ํ‰๊ฐ€์˜ ๊ฐ•์ œ๋ฅผ "์ค‘๋‹จ" ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ฒŒ์œผ๋ฅธ ํ•จ์ˆ˜์ธ const (3, 4)์™€ ๋น„๊ตํ•ด ๋ณด๋ฉด, let (x, y) = undefined in x -- Error, because we force undefined. let (x, y) = const (3, 4) undefined -- No error, because const (3, 4) is lazy. ์‚ฌ๋ฌผ์— ๋Œ€ํ•œ ํ‘œ๊ธฐ์  ๊ด€์  ์—ฌ๋Ÿฌ๋ถ„์ด ํ‘œ๊ธฐ ์˜๋ฏธ๋ก (denotational semantics)์— ์ต์ˆ™ํ•˜๋‹ค๋ฉด (ํ•ด๋‹น ์ฑ•ํ„ฐ๋ฅผ ์ฝ์–ด๋ณด์…จ๋‚˜์š”?) ํ•จ์ˆ˜์˜ ์—„๊ฒฉํ•จ์„ ์•„์ฃผ ๊ฐ„๋‹จ๋ช…๋ฃŒํ•˜๊ฒŒ ์š”์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค. f โŠฅ = โŠฅ โ‡” f is strict ์œ„์™€ ๊ฐ™์€ ๊ฐ€์ • ํ•˜์—์„œ, undefined, error "any string", throw ๋“ฑ์„ ํฌํ•จํ•ด ํƒ€์ž…์ด forall a. a์ธ ๋ชจ๋“  ๊ฒƒ์€ ํ‘œ๊ธฐ โŠฅ๋ฅผ ๊ฐ€์ง„๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒŒ์œผ๋ฅธ ํŒจํ„ด ๋งค์นญ ํ•˜์Šค ์ผˆ ์†Œ์Šค์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŒจํ„ด ๋งค์นญ์„ ๋ณธ ์ ์ด ์žˆ๋Š”๊ฐ€? -- From Control.Arrow (***) f g ~(x, y) = (f x, g y) ์œ„์˜ ํŒจํ„ด ๋งค์นญ์—์„œ ๋ฌผ๊ฒฐ ๊ธฐํ˜ธ(~)๋Š” ์–ด๋–ค ์˜๋ฏธ์ผ๊นŒ? ~๋Š” ๊ฒŒ์œผ๋ฅธ ํŒจํ„ด ์ฆ‰ ๊ฑฐ๋ถ€ํ•  ์ˆ˜ ์—†๋Š” ํŒจํ„ด irrefutable pattern์„ ํ˜•์„ฑํ•œ๋‹ค. ๋ณดํ†ต์€ ์ƒ์„ฑ์ž๋ฅผ ํŒจํ„ด ๋งค์นญ์˜ ์ผ๋ถ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๊ทธ ์ƒ์„ฑ์ž์— ์ „๋‹ฌ๋˜๋Š” ๋ชจ๋“  ์ธ์ž๋ฅผ ํ‰๊ฐ€ํ•ด์„œ ํŒจํ„ด์ด ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์œ„์™€ ๊ฐ™์€ ํ•จ์ˆ˜์—์„œ ์„ธ ๋ฒˆ์งธ ์ธ์ž๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ’์ด ํŒจํ„ด์— ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•  ๋•Œ ํ‰๊ฐ€๋œ๋‹ค. (์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ํ‰๊ฐ€๋˜์ง€ ์•Š๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•  ๊ฒƒ. ํŒจํ„ด f์™€ g๋Š” ์•„๋ฌด๊ฒƒ์—๋‚˜ ๋งค์นญ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํŠœํ”Œ์˜ ๊ตฌ์„ฑ์š”์†Œ๋“ค์€ ํ‰๊ฐ€๋˜์ง€ ์•Š๊ณ  ์ตœ์ƒ์œ„ ์ˆ˜์ค€๋งŒ ํ‰๊ฐ€๋˜๋Š” ์ ๋„ ๋ˆˆ์—ฌ๊ฒจ๋ณผ ๊ฒƒ. let f (Just x) = 1 in f (Just undefined)๋ฅผ ํ™•์ธํ•ด ๋ณด๋ผ.) ํŒจํ„ด์— ๋ฌผ๊ฒฐ ๊ธฐํ˜ธ๋ฅผ ๋ถ™์ด๋ฉด ๊ทธ ๊ตฌ์„ฑ์š”์†Œ ์ผ๋ถ€๊ฐ€ ์‹ค์ œ๋กœ ์‚ฌ์šฉ๋  ๋•Œ๊นŒ์ง€ ๊ฐ’์˜ ํ‰๊ฐ€๊ฐ€ ์ง€์—ฐ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋Ÿฌ๋ฉด ๊ฐ’์ด ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋Š” ์œ„ํ—˜์ด ์ƒ๊ธด๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ '๋‚  ๋ฏฟ์–ด. ๋ฌธ์ œ์—†์„ ๊ฑฐ์•ผ'๋ผ๊ณ  ๋งํ•˜๋Š” ์…ˆ์ด๋‹ค. (ํŒจํ„ด์ด ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค) ๊ทธ ์ฐจ์ด์ ์„ ํ™•์ธํ•ด ๋ณด์ž. Prelude> let f (x, y) = 1 Prelude> f undefined *** Exception: Prelude.undefined Prelude> let f ~(x, y) = 1 Prelude> f undefined ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์—์„œ๋Š” ๊ฐ’์ด ํŠœํ”Œ ํŒจํ„ด์— ์ผ์น˜ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ‰๊ฐ€๋œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ undefined๋ฅผ ํ‰๊ฐ€ํ•˜๊ณ  undefined๋ฅผ ์–ป์œผ๋ฉฐ ์ง„ํ–‰์ด ์ค‘๋‹จ๋œ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” ์ธ์ž๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ์ „์—๋Š” ํ‰๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋ฐ, ๊ทธ๋Ÿด ์ผ์ด ์ ˆ๋Œ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— undefined๋ฅผ ๋„˜๊ฒจ๋„ ์ƒ๊ด€์ด ์—†๋‹ค. ํ™”์ œ๋ฅผ ๋Œ๋ ค (***)๋กœ ๋Œ์•„๊ฐ€ ๋ณด๋ฉด, Prelude> (const 1 *** const 2) undefined (1,2) ๊ฑฐ๋ถ€ํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ„ด์ด์—ˆ๋‹ค๋ฉด ์ด ์˜ˆ์ œ๋Š” ์‹คํŒจํ–ˆ์„ ๊ฒƒ์ด๋‹ค. ์–ธ์ œ ์ง€์—ฐ ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ํŠœํ”Œ์ฒ˜๋Ÿผ ํ•ด๋‹น ํƒ€์ž…์„ ์œ„ํ•œ ์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜์ผ ๋•Œ๋งŒ ์ง€์—ฐ ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋ณต์ˆ˜ ๊ฐœ์˜ ๋“ฑ์‹์€ ๊ฑฐ๋ถ€ํ•  ์ˆ˜ ์—†๋Š” ํŒจํ„ด๊ณผ ์ž˜ ๋งž์ง€ ์•Š๋Š”๋‹ค. head์— ๊ฑฐ๋ถ€ํ•  ์ˆ˜ ์—†๋Š” ํŒจํ„ด์„ ๋„ฃ์–ด๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋‹ค. head' :: [a] -> a head' ~[] = undefined head' ~(x:xs) = x ๋ถ„๋ช…ํžˆ ํŒจํ„ด๋“ค ์ค‘ ํ•˜๋‚˜์—๋Š” ์ผ์น˜ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ธ์ž์˜ ์ตœ์ƒ์œ„ ์ˆ˜์ค€๋„ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฉด ํ‰๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ ์ธ์ž๊ฐ€ ๋นˆ ๋ฆฌ์ŠคํŠธ์ธ์ง€ cons ์…€ ์ธ์ง€๋„ ๋ชจ๋ฅธ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋“ฑ์‹์— ๊ฑฐ๋ถ€ํ•  ์ˆ˜ ์—†๋Š” ํŒจํ„ด์„ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์— ์ผ์น˜ํ•˜๊ณ , head๋Š” ํ•ญ์ƒ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์™œ head'์˜ ๋“ฑ์‹๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”๋„ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜์ง€ ์•Š์„๊นŒ? ์ฒซ ๋ฒˆ์งธ ๋“ฑ์‹์— ์ผ๋ฐ˜์ ์ธ ๊ฑฐ๋ถ€ํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด๋„ head'๋Š” ์—ฌ์ „ํžˆ head์™€ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ• ๊นŒ? ๋‹ค๋ฅด๋‹ค๋ฉด, ์™œ ๊ทธ๋Ÿด๊นŒ? ๋น„ ์—„๊ฒฉ ์˜๋ฏธ๋ก ์˜ ์žฅ์  ์• ์ดˆ์— ์™œ ํ•˜์Šค์ผˆ์„ ๋น„ ์—„๊ฒฉ ์–ธ์–ด๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•˜๊ณ , ๊ทธ๋Ÿฌ๋ฉด ์–ด๋–ค ์ด๋“์ด ์žˆ์„๊นŒ? ์‹œ๊ฐ„ ํŽ˜๋„ํ‹ฐ ์—†๋Š” ์ฃผ์˜ ๋ถ„์‚ฐ(Separation of concerns without time penalty) ์ง€์—ฐ ํ‰๊ฐ€๋Š” ์ฝ”๋”ฉํ•  ๋•Œ "๋ณด๋Š” ๊ทธ๋Œ€๋กœ๋ฅผ ์–ป๋Š”๋‹ค" ์‹์˜ ์‚ฌ๊ณ ๋ฅผ ์žฅ๋ คํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ธด ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž ์„ธ ๊ฐœ๋ฅผ ์ฐพ๋Š” ๊ฑธ ์ƒ๊ฐํ•ด ๋ณด์ž. ํ•˜์Šค์ผˆ์—์„œ ๊ทธ ๋ฐฉ๋ฒ•์€ ๊ทนํžˆ ์ž์—ฐ์Šค๋Ÿฝ๋‹ค. take 3 (sort xs). ํ•˜์ง€๋งŒ ์ด ์ฝ”๋“œ๋ฅผ ์—„๊ฒฉ ์–ธ์–ด๋กœ ๋‹จ์ˆœ ๋ฒˆ์—ญํ•˜๋Š” ๊ฑด ์•„์ฃผ ๋‚˜์œ ๋ฐœ์ƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋ฆฌ์ŠคํŠธ ์ „์ฒด๋ฅผ ์ •๋ ฌํ•˜๊ณ  ์ฒ˜์Œ ์„ธ ์›์†Œ๋ฅผ ์ž˜๋ผ๋‚ด๊ฒŒ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์ง€์—ฐ ํ‰๊ฐ€์˜ ๊ฒฝ์šฐ ์„ธ ๋ฒˆ์งธ ์›์†Œ๊นŒ์ง€๋งŒ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•ด ๋‚ด๋ฉด ๊ฑฐ๊ธฐ์„œ ์ค‘๋‹จํ•˜๋ฏ€๋กœ ์ด๋Ÿฐ ์ž์—ฐ์Šค๋Ÿฌ์šด ์ •์˜๊ฐ€ ํšจ์œจ์ ์ด๊ฒŒ ๋œ๋‹ค. (์ •๋ ฌ์˜ ๊ตฌํ˜„์— ๋”ฐ๋ผ) ๋‹ค๋ฅธ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณด๋ฉด Data.List์˜ isInfixOf ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์ด ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€์ธ์ง€ ํ™•์ธํ•œ๋‹ค. isInfixOf๋Š” ์‰ฝ๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. isInfixOf :: Eq a => [a] -> [a] -> Bool isInfixOf x y = any (isPrefixOf x) (tails y) ์—„๊ฒฉ ์–ธ์–ด์—์„œ ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฑด ์ž์‚ดํ–‰์œ„๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  tail๋“ค์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฑด ์•„์ฃผ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ tail์ด x๋ฅผ ์ „์น˜์‚ฌ๋กœ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•œ ๋งŒํผ๋งŒ ํ‰๊ฐ€ํ•˜๋ฉฐ ๊ทธ๋Ÿฌํ•œ tail์„ ์ฐพ์œผ๋ฉด ๋ฐ”๋กœ ์ค‘๋‹จํ•˜๊ณ  True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ์™ธ์—๋„ ๋งŽ์€ ์˜ˆ์‹œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ 1 ์‹œ๊ฐ„ ํŽ˜๋„ํ‹ฐ๋ฅผ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์œผ๋ฉฐ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณด์ด๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ปดํ“จํ„ฐ ๊ณผํ•™์ด ์œผ๋ ˆ ๊ทธ๋ ‡๋“ฏ์ด (์‚ถ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค) ํŠธ๋ ˆ์ด๋“œ์˜คํ”„(ํŠนํžˆ ๊ณต๊ฐ„-์‹œ๊ฐ„ ํŠธ๋ ˆ์ด๋“œ์˜คํ”„)๊ฐ€ ์žˆ๋Š” ๋ฒ•์ด๋‹ค. 2+2 ๊ฐ™์€ ์ž์ž˜ํ•œ ๊ณ„์‚ฐ์— ๋‹จ์ˆœํ•œ Int ๋Œ€์‹  ์ฝํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฑด ๋‚ญ๋น„๋‹ค. ๋” ๋งŽ์€ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ ค๋ฉด ์—„๊ฒฉ์„ฑ์— ๊ด€ํ•œ ํŽ˜์ด์ง€๋ฅผ ๋ณผ ๊ฒƒ. ๊ฐœ์„ ๋œ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์€ ์ž์ฃผ ์š”๊ตฌ๋˜๋Š” ์‚ฌํ•ญ์ด๋‹ค. Data.List์˜ isInfixOf๋ฅผ ์ด๋ฒˆ์—” ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณด์ž. ์™„์ „ํ•œ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. -- From the Prelude or = foldr (||) False any p = or. map p -- From Data.List isPrefixOf [] _ = True isPrefixOf _ [] = False isPrefixOf (x:xs) (y:ys) = x == y && isPrefixOf xs ys tails [] = [[]] tails xss@(_:xs) = xss : tails xs -- Our function isInfixOf :: Eq a => [a] -> [a] -> Bool isInfixOf x y = any (isPrefixOf x) (tails y) any, isPrefixOf, tails๋Š” Data.List ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๊ฐ€์ ธ์˜จ ํ•จ์ˆ˜๋‹ค. isInfixOf๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž x๊ฐ€ ๋‘ ๋ฒˆ์งธ ์ธ์ž y์˜ ๋ถ€๋ถ„ ์ˆ˜์—ด์ธ์ง€๋ฅผ ํ™•์ธํ•œ๋‹ค. String ์ฆ‰ [Char]์— ์ ์šฉ๋  ๊ฒฝ์šฐ์—๋Š” x๊ฐ€ y์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ธ์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. ์—„๊ฒฉํ•œ ๋ฐฉ์‹์œผ๋กœ ํ•ด์„ํ•˜๋ฉด isInfixOf๋Š” y์˜ ๋ชจ๋“  tail์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ์ค‘ ํ•˜๋‚˜๋ผ๋„ x๋ฅผ ์ „์น˜์‚ฌ๋กœ ๊ฐ€์ง€๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. ์—„๊ฒฉํ•œ ์–ธ์–ด์—์„œ ์ด ํ•จ์ˆ˜๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ (์ด๋ฏธ ์ž‘์„ฑ๋œ any, isPrefixOf, tails๋ฅผ ์ด์šฉํ•ด) ์ž‘์„ฑํ•˜๋Š” ๊ฑด ์–ด๋ฆฌ์„๋‹ค. ํ•„์š” ์ด์ƒ์œผ๋กœ ๋Š๋ฆด ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ๊ฒฐ๊ตญ ๋˜๋‹ค์‹œ ์ง์ ‘์ ์œผ๋กœ ์žฌ๊ท€๋ฅผ ํ•˜๊ฑฐ๋‚˜ ๋ช…๋ นํ˜• ์–ธ์–ด๋ผ๋ฉด ์ค‘์ฒฉ๋œ ๋ฃจํ”„๋ฅผ ์“ฐ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. isPrefixOf๋Š” ํ™œ์šฉํ•  ์—ฌ์ง€๊ฐ€ ์žˆ๊ฒ ์ง€๋งŒ tails๋ฅผ ์“ฐ์ง€๋Š” ์•Š์„ ๊ฒƒ์ด๋‹ค. any๋ฅผ ์จ์„œ ์กฐ๊ธฐ ์ข…๋ฃŒํ•  ์ˆ˜๋Š” ์žˆ๊ฒ ์ง€๋งŒ foldr์„ ์“ฐ๊ณ  ์‹ถ์ง€๋Š” ์•Š์„ ๊ฒƒ์ด๋‹ˆ ๋” ๋ฒˆ๊ฑฐ๋กœ์šด ์ผ์ด ๋  ๊ฒƒ์ด๋‹ค. ๊ฒŒ์œผ๋ฅธ ์–ธ์–ด์—์„œ๋Š” ๋ชจ๋“  ์ถ•์•ฝ์ด ์•Œ์•„์„œ ๋œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ํ•ด๋‹ต์„ ์ฐพ์•˜์„ ๋•Œ๋ฅผ ์œ„ํ•ด foldr์„ ์†์ˆ˜ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ tail ๋“ค์—์„œ ์ผ์–ด๋‚˜๋Š” ์žฌ๊ท€๋ฅผ ์กฐ๊ธฐ ์ข…๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด ๊ทธ ์žฌ๊ท€๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ฝ”๋“œ๋ฅผ ๋” ์ž˜ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ์ง€์—ฐ์„ฑ์€ ๋‹จ์ˆœํ•œ ์†๋„ ๊ด€๋ จ ์ƒ์ˆ˜ ์š”์ธ์ด ์•„๋‹ˆ๋‹ค. ์ง€์—ฐ์„ฑ์€ ์ฝ”๋“œ๋ฅผ ํ•ฉ๋ฆฌ์ ์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ํฐ ์˜ํ–ฅ์„ ์ค€๋‹ค. ์‚ฌ์‹ค ๋ฌดํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๊ณ ๋Š” ํ•„์š”ํ•œ ๋งŒํผ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ์–ด๋–ค ๋ถ€๋ถ„์ด ํ•„์š”ํ•œ์ง€ ๊ฒฐ์ •ํ•œ ๋‹ค์Œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋กœ์ง์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ณด๋‹ค ์ผ๋ฐ˜์ ์ด๋‹ค. ์ฝ”๋“œ ๋ชจ๋“ˆ์„ฑ ๋˜ํ•œ ํ–ฅ์ƒ๋˜๋Š”๋ฐ, ์ง€์—ฐ์„ฑ์€ ์ฝ”๋“œ๋ฅผ ๋” ์ž˜๊ฒŒ ์ชผ๊ฐœ์–ด ๊ฐ ์กฐ๊ฐ์ด ๋ฐ์ดํ„ฐ ์ƒ์„ฑ, ํ•„ํ„ฐ๋ง, ๊ธฐํƒ€ ์กฐ์ž‘ ๋“ฑ ๋‹จ์ˆœํ•œ ์ž‘์—…์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. Why Functional Programming Matters๋Š” ์ง€์—ฐ์„ฑ์ด ์ค‘์š”ํ•œ ์˜ˆ์‹œ๋“ค์— ์ดˆ์ ์„ ๋งž์ถ”๊ณ , ์ง€์—ฐ ํ‰๊ฐ€๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์‚ผ์•„์•ผ ํ•œ๋‹ค๊ณ  ๊ฐ•ํ•˜๊ฒŒ ์ฃผ์žฅํ•œ๋‹ค. ๋ฌดํ•œ ์ž๋ฃŒ๊ตฌ์กฐ (์›๋ฌธ ๋ฏธ์™„์„ฑ) ์˜ˆ์ œ: fibs = 1:1:zipWith (+) fibs (tail fibs) "rock-scissors-paper" example from Bird&Wadler prune . generate Infinite data structures usually tie a knot, too, but the Sci-Fi-Explanation of that is better left to the next section. One could move the next section before this one but I think that infinite data structures are simpler than tying general knots ํ”ํ•œ ๋น„ ์—„๊ฒฉ ๊ด€์šฉ๊ตฌ Tying the knot (์›๋ฌธ ๋ฏธ์™„์„ฑ) ๋” ์‹ค์šฉ์ ์ธ ์˜ˆ์‹œ? repMin Sci-Fi-Explanation: "You can borrow things from the future as long as you don't try to change them". Advanced: the "Blueprint"-technique. Examples: the one from the haskellwiki, the one from the mailing list. ์–ธ๋œป ๋ณด๋ฉด ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์–ธ์–ด๋Š” ์ˆœํ™˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ์— ๋ฌธ์ œ๊ฐ€ ์žˆ์–ด ๋ณด์ธ๋‹ค. ์ด๋Ÿฐ ์ž๋ฃŒํ˜•์„ ์ƒ๊ฐํ•ด ๋ณด์ž. data Foo a = Foo {value :: a, next :: Foo a} ๋‘ ๊ฐœ์ฒด x์™€ y๋ฅผ ์ƒ์„ฑํ•ด์„œ x์™€ y๊ฐ€ ์„œ๋กœ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ํฌํ•จํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ, ์ „ํ†ต์ ์ธ ์–ธ์–ด์—์„œ๋Š” ๊ทธ ๋ฐฉ๋ฒ•์ด ๋‹จ์ˆœํ•˜๋‹ค. ๋‘ ๊ฐœ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ๊ฐ์˜ ํ•„๋“œ๊ฐ€ ์„œ๋กœ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค. -- Not Haskell code x := new Foo; y := new Foo; x.value := 1; x.next := y; y.value := 2 y.next := x; ํ•˜์Šค์ผˆ์—์„  ์ด๋Ÿฐ ๊ฒƒ์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹  ์ง€์—ฐ ํ‰๊ฐ€์— ์˜์กดํ•œ๋‹ค. circularFoo :: Foo Int circularFoo = x where x = Foo 1 y y = Foo 2 x ์ด๊ฒƒ์€ Foo ์ƒ์„ฑ์ž๊ฐ€ ํ•จ์ˆ˜์ด๋ฉฐ ๋Œ€๋ถ€๋ถ„์˜ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ง€์—ฐ ํ‰๊ฐ€๋œ๋‹ค๋Š” ์‚ฌ์‹ค์— ์˜๊ฑฐํ•œ๋‹ค. ํ•„๋“œ๋“ค์€ ํ•„์š”ํ•œ ๋•Œ๋งŒ ํ‰๊ฐ€๋œ๋‹ค. ๋ฌด๋Œ€ ๋’ค์—์„œ ์–ด๋–ค ์ผ์ด ๋ฒŒ์–ด์ง€๋Š”์ง€ ์ดํ•ดํ•ด ๋ณด์ž. ์ง€์—ฐ ๊ฐ’์ด ์ƒ์„ฑ๋˜๋ฉด (์˜ˆ๋ฅผ ๋“ค์–ด Foo ํ˜ธ์ถœ์— ์˜ํ•ด) ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ๊ณผ ์ธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ์ฝํฌ๋ผ๋Š” ๋‚ด๋ถ€ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. ํ•จ์ˆ˜์˜ ๊ฐ’์ด ํ•„์š”ํ•ด์ง€๋ฉด ๊ทธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ฝํฌ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ๊ต์ฒด๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ ๊ฐ’์„ ์ฐธ์กฐํ•˜๋Š” ์–ด๋–ค ๊ฒƒ์ด๋“  ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ํ•„์š” ์—†์ด ๊ทธ ๊ฐ’์„ ๊ณง๋ฐ”๋กœ ์–ป๋Š”๋‹ค. (ํ•˜์Šค ์ผˆ ์–ธ์–ด ํ‘œ์ค€์€ ์ฝํฌ๋ฅผ ์ „ํ˜€ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์ฃผ์˜ํ•˜๋ผ. ์ฝํฌ๋Š” ๊ตฌํ˜„์„ ์œ„ํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ์ˆ˜ํ•™์  ๊ด€์ ์—์„œ ์ด๊ฒƒ์€ ์ƒํ˜ธ ์žฌ๊ท€์˜ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋‹ค.) ์ฆ‰ circularFoo๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ์ธ x๋Š” ์‚ฌ์‹ค ์ฝํฌ๋‹ค. ์ธ์ž๋“ค ์ค‘ ํ•˜๋‚˜๋Š” y๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ๋ฒˆ์งธ ์ฝํฌ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋‹ค. ์ด๊ฒƒ์€ ๋‹ค์‹œ x๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ฝํฌ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. next x๋ผ๋Š” ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด x ์ฝํฌ์˜ ํ‰๊ฐ€๊ฐ€ ๊ฐ•์ œ๋˜์–ด y ์ฝํฌ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. next $ next x๋ผ๋Š” ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‘ ์ฝํฌ์˜ ํ‰๊ฐ€๋ฅผ ๊ฐ•์ œํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด์ œ ๋‘ ์ฝํฌ๊ฐ€ ์‹ค์ œ Foo ๊ตฌ์กฐ์ฒด๋กœ ๊ต์ฒด๋˜์–ด ์„œ๋กœ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค. ๋ฐ”๋กœ ์šฐ๋ฆฌ๊ฐ€ ์›ํ–ˆ๋˜ ๊ฒฐ๊ณผ๋‹ค. ์ด๋Ÿฐ ๊ธฐ๊ต๋Š” ์ฃผ๋กœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋“ค์— ํ™œ์šฉ๋˜์ง€๋งŒ ์ƒ์„ฑ์ž์— ๊ตญํ•œ๋˜๋Š” ์ด์•ผ๊ธฐ๋Š” ์•„๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋ฉฐ, x = f y y = g x ๋…ผ๋ฆฌ๋Š” ๋™์ผํ•˜๋‹ค. Memoization, Sharing and Dynamic Programming Dynamic programming with immutable arrays. DP with other finite maps, Hinze's paper "Trouble shared is Trouble halved". Let-floating \x-> let z = foo x in \y -> ... . ์ง€์—ฐ์„ฑ์— ๋Œ€ํ•œ ๊ฒฐ๋ก  ์ง€์—ฐ์„ฑ์€... ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ํ–ฅ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์„ฑ๋Šฅ์„ ํ•ด์น˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ์–ด๋ ค์šด ๋ฌธ์ œ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ ๋‹ค. ๋ฐ์ดํ„ฐ ์ƒ์„ฑ๊ณผ ์ฒ˜๋ฆฌ๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. ๋…ธํŠธ ๋ ˆํผ๋Ÿฐ์Šค Laziness on the Haskell wiki Lazy evaluation tutorial on the Haskell wiki ์ผ๋ฐ˜์ ์œผ๋กœ prune . generate ๊ฐ™์€ ํ‘œํ˜„์‹์—์„œ generate๊ฐ€ ์–ด๋–ค ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  prune์ด ๊ทธ๊ฑธ ์ž˜๋ผ๋‚ด๋Š” ๊ฒฝ์šฐ ๋น„ ์—„๊ฒฉ ์–ธ์–ด์—์„œ๋Š” ํ›จ์”ฌ ํšจ์œจ์ ์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค. โ†ฉ 5 ์‹œ๊ฐ„ ๋ฐ ๊ณต๊ฐ„ ํ”„๋กœํŒŒ์ผ๋ง ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Time_and_space_profiling ์–ด๋Š ์–ธ์–ด๋“  ํ”„๋กœํŒŒ์ผ๋ง์€ ํผํฌ๋จผ์Šค ์ตœ์ ํ™”์˜ ์ค‘์š”ํ•œ ์ฒซ๊ฑธ์Œ์ด๋‹ค. ํ”„๋กœํŒŒ์ผ๋ง ์—†์ด๋Š” ๊ฒฝํ—˜์ด ๋งŽ์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋ผ๋„ ๋‚˜์œ ํผํฌ๋จผ์Šค์˜ ์›์ธ์ด ๋ฌด์—‡์ธ์ง€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ์ง€์—ฐ์— ์˜ํ•œ ๊ณต๊ฐ„ ๋ˆ„์ˆ˜๋Š” ๋ฌธ์ œ์˜<NAME>์ธ์ด๋‹ค. ์ฆ‰ ํ”„๋กœํŒŒ์ผ๋ง ์‚ฌ์šฉ๋ฒ•์„ ์ตํžˆ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ์ง€๊ธˆ์œผ๋กœ์„  ์ด ์ฃผ์ œ์— ๋Œ€ํ•œ ์™ธ๋ถ€ ์ž๋ฃŒ๋“ค์— ๋Œ€ํ•œ ๋งํฌ๋งŒ ์žˆ๋‹ค. ์ฐธ๊ณ  ์ž๋ฃŒ http://book.realworldhaskell.org/read/profiling-and-optimization.html https://www.haskell.org/ghc/docs/latest/html/users_guide/profiling.html http://www.haskell.org/haskellwiki/How_to_profile_a_Haskell_program 6 ์ ๊ทน์„ฑ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Strictness ์ ๊ทน ํ‰๊ฐ€์™€ ์ง€์—ฐ ํ‰๊ฐ€์˜ ์ฐจ์ด ์ง€์—ฐ์ด ์™œ ๋ฌธ์ œ์ธ๊ฐ€ ์ ๊ทน์„ฑ ํ‘œ๊ธฐ(annotation) seq ์ฐธ๊ณ  ์ž๋ฃŒ ์ ๊ทน ํ‰๊ฐ€์™€ ์ง€์—ฐ ํ‰๊ฐ€์˜ ์ฐจ์ด ์ ๊ทน ํ‰๊ฐ€(strict evaluation, or eager evaluation)๋Š” ํ‘œํ˜„์‹์ด ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉ ๋˜์ž๋งˆ์ž ํ‰๊ฐ€ํ•˜๋Š” ์ „๋žต์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด x = 3 * 7์„ ์ฝ์œผ๋ฉด 3 * 7์ด ์ฆ‰์‹œ ๊ณ„์‚ฐ๋˜์–ด x์— 21์ด ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. ๋ฐ˜๋Œ€๋กœ ์ง€์—ฐ ํ‰๊ฐ€์—์„œ๋Š” ๊ฐ’์ด ํ•„์š”ํ•  ๋•Œ๋งŒ ๊ณ„์‚ฐ๋œ๋‹ค. x = 3 * 7์˜ ๊ฒฝ์šฐ 3 * 7์€ ๊ฐ€๋ น x์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋“ฑ์˜ ์ด์œ ๋กœ ํ•„์š”ํ•ด์ง€๊ธฐ ์ „์—๋Š” ํ‰๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค. ์ง€์—ฐ์ด ์™œ ๋ฌธ์ œ์ธ๊ฐ€ ์ง€์—ฐ ํ‰๊ฐ€์—๋Š” ๋Œ€๊ฐœ ์ฝํฌ๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ ๊ด€์—ฌํ•œ๋‹ค. ์ฝํฌ๋Š” ์ผ์ข…์˜ ์ž๋ฆฌ ํ‘œ์‹œ์šฉ์œผ๋กœ์„œ ๋ฐ์ดํ„ฐ ๊ทธ ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ธฐ์ˆ ํ•œ๋‹ค. ์–ด๋–ค ๊ฐœ์ฒด๋Š” ๊ทธ๊ฒƒ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ์ฝํฌ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ๋ณต์‚ฌ๋  ๋•Œ ๊ทธ๊ฒƒ์ด ์ฝํฌ์ธ์ง€ ์•„๋‹Œ์ง€๋Š” ์ƒ๊ด€์—†๊ณ  ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌ๋œ๋‹ค. (๋Œ€๋ถ€๋ถ„์˜ ๊ตฌํ˜„์—์„œ๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค) ๊ทธ ๊ฐœ์ฒด๊ฐ€ ํ‰๊ฐ€๋  ๋•Œ๋Š” ๋จผ์ € ์ฝํฌ์ธ์ง€๋ฅผ ํ™•์ธํ•œ๋‹ค. ์ฝํฌ๋ผ๋ฉด ์‹คํ–‰๋˜๊ณ  ์•„๋‹ˆ๋ผ๋ฉด ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. ์ฝํฌ๋ผ๋Š” ๋งˆ์ˆ  ๋•์— ์ง€์—ฐ์„ฑ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ์ผ๋ฐ˜์ ์ธ ๊ตฌํ˜„์—์„œ ์ฝํฌ๋Š” ๋‹จ์ง€ (๋Œ€๊ฐœ ์ •์ ์ธ) ์ฝ”๋“œ ์กฐ๊ฐ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ์™€ ๊ทธ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋  ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋˜ ๋‹ค๋ฅธ ํฌ์ธํ„ฐ๋ฅผ ๋ชจ์•„๋†“์€ ๊ฒƒ์ด๋‹ค. ์ฝํฌ์— ์˜ํ•ด ๊ณ„์‚ฐ๋˜๋Š” ๊ฐœ์ฒด๊ฐ€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ์™€ ์—ฐ๊ด€ ๋ฐ์ดํ„ฐ๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์ฝํฌ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋ฉด์—์„œ ์ด๋“์ด๋‹ค. ํ•˜์ง€๋งŒ ์ฝํฌ์— ์˜ํ•ด ๊ณ„์‚ฐ๋˜๋Š” ๊ฐœ์ฒด๊ฐ€ ๊ทธ ์ฝํฌ๋ณด๋‹ค ์ž‘์œผ๋ฉด ์ฝํฌ๋Š” ๋ฉ”๋ชจ๋ฆฌ๋งŒ ๋” ๋งŽ์ด ์“ฐ๊ฒŒ ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ‘œํ˜„์‹ iterate (+ 1) 0์— ์˜ํ•ด ์ƒ์„ฑ๋œ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ์ด ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋Š” ๋ฌดํ•œํ•˜์ง€๋งŒ ๊ทธ ์ฝ”๋“œ๋Š” ๋‹จ์ง€ ๋”ํ•˜๊ธฐ ์—ฐ์‚ฐ์ด๊ณ  ๋‘ ๋ฐ์ดํ„ฐ ์กฐ๊ฐ 1๊ณผ 0์€ ์ •์ˆ˜์ผ๋ฟ์ด๋‹ค. ์ด ๊ฒฝ์šฐ ์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ฝํฌ๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ฌดํ•œํžˆ ์‚ฌ์šฉํ•  ์‹ค์ œ ๋ฆฌ์ŠคํŠธ๋ณด๋‹ค ํ›จ์”ฌ ์ ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํ‘œํ˜„์‹ 4 * 13 + 2์ด ์ƒ์„ฑํ•˜๋Š” ์ˆซ์ž๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ์ด ์ˆซ์ž์˜ ๊ฐ’์€ 54์ง€๋งŒ ์ฝํฌ ํ˜•ํƒœ๋กœ๋Š” ๊ณฑ์…ˆ, ๋ง์…ˆ, ์ˆซ์ž ์„ธ ๊ฐœ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ฝํฌ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋ฉด์—์„œ ์ข‹์ง€ ์•Š๋‹ค. ๋‘ ๋ฒˆ์งธ ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ์†Œ๋น„ํ•ด์„œ ์ „์ฒด ํžˆํ”„๋ฅผ ์†Œ๋น„ํ•˜๊ณ  ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ด๋Ÿฌ๋ฉด ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์ด ์‹ฌ๊ฐํ•˜๊ฒŒ ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋ฐ”๋กœ ์ด๊ฒƒ์ด ์ง€์—ฐ์„ฑ์ด ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ์ฃผ ์ด์œ ๋‹ค. ๋˜ํ•œ ๊ฒฐ๊ด๊ฐ’์ด ์‚ฌ์šฉ๋  ๊ฒƒ์ด๋ผ๋ฉด ์ ˆ์•ฝํ•  ๊ณ„์‚ฐ์€ ์—†๊ณ  ๋Œ€์‹  ์ฝํฌ๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ์˜ค๋ฒ„ํ—ค๋“œ๋งŒ ์ง€๋ถˆํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด ์˜ค๋ฒ„ํ—ค๋“œ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์‹ ๊ฒฝ ์“ธ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์ด๋ณด๋‹ค ๋” ํฐ ๊ฐœ์„ ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ์ค‘์š”ํ•œ ์š”์ธ๋“ค์€ ๋งŽ๋‹ค. GHC ๊ฐ™์€ ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” '์ •์  ๋ถ„์„'์„ ํ†ตํ•ด ์ด๋Ÿฐ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ ๊ทน์„ฑ ํ‘œ๊ธฐ(annotation) ์›๋ฌธ ๋ฏธ์™„์„ฑ seq ์›๋ฌธ ๋ฏธ์™„์„ฑ ์ฐธ๊ณ  ์ž๋ฃŒ ํ•˜์Šค ์ผˆ ์œ„ํ‚ค์˜ strictness 7 ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Algorithm_complexity ๋ณต์žก๋„ ์ด๋ก (Complexity Theory)์€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์ด ์ž…๋ ฅ์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ์–ผ๋งˆ๋‚˜ ๊ฑธ๋ฆด์ง€์— ๊ด€ํ•œ ์—ฐ๊ตฌ๋‹ค. ๋ณต์žก๋„ ์ด๋ก ์— ๋Œ€ํ•œ ์ข‹์€ ์ž…๋ฌธ์„œ๋Š” ๋งŽ๊ณ  ๋ชจ๋“  ํ›Œ๋ฅญํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„œ์ ์ด ๊ทธ ๊ธฐ์ดˆ๋ฅผ ์„ค๋ช…ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ตœ์†Œํ•œ์˜ ๋…ผ์˜๋งŒ ํ•œ๋‹ค. ํ•ต์‹ฌ์€ ํ”„๋กœ๊ทธ๋žจ์ด ๋” ๋งŽ์€ ๋ฐ์ดํ„ฐ์— ์–ผ๋งˆ๋‚˜ ์ž˜ ๋Œ€์‘ํ•˜๋Š”๊ฐ€์ด๋‹ค. ์•„์ฃผ ์ ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ์—๋Š” ๋น ๋ฅด๊ฒŒ ์‹คํ–‰๋˜์ง€๋งŒ ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ๋Š๋ฆฐ ํ”„๋กœ๊ทธ๋žจ์€ ๊ทธ๋‹ค์ง€ ์œ ์šฉํ•˜์ง€ ์•Š๋‹ค. (๋ฌผ๋ก  ์ ์€ ๋ฐ์ดํ„ฐ๋งŒ ๋‹ค๋ค„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ ๋•Œ๋งŒ.) ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋“ค์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋‹ค์Œ์˜ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋ฅผ ๋ณด์ž. sum [] = 0 sum (x:xs) = x + sum xs ์ด ํ•จ์ˆ˜๊ฐ€ ์™„๋ฃŒ๋˜๋ ค๋ฉด ์–ผ๋งˆ๋‚˜ ๊ฑธ๋ฆด๊นŒ? ์ด๋Š” ๋งค์šฐ ์–ด๋ ค์šด ์งˆ๋ฌธ์ด๋‹ค. ์—ฌ๊ธฐ์—๋Š” ํ”„๋กœ์„ธ์„œ ์†๋„, ๋ฉ”๋ชจ๋ฆฌ ์–‘, ๋ง์…ˆ์ด ์ˆ˜ํ–‰๋˜๋Š” ๋ฐฉ์‹, ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด, ์ปดํ“จํ„ฐ์—์„œ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์ด ๋ช‡ ๊ฐœ๋‚˜ ๋Œ์•„๊ฐ€๋Š”์ง€ ๋“ฑ ์˜จ๊ฐ– ๊ฒƒ์ด ๊ด€์—ฌํ•œ๋‹ค. ๊ณ ๋ คํ•  ๊ฒƒ์ด ๋„ˆ๋ฌด ๋งŽ์œผ๋ฏ€๋กœ ์šฐ๋ฆฌ์—๊ฒ ๋” ๊ฐ„๋‹จํ•œ ๋ชจ๋ธ์ด ํ•„์š”ํ•˜๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์€ ์ผ์ข…์˜ "machine step"์ด๋‹ค. ์งˆ๋ฌธ์€ "์ด ํ”„๋กœ๊ทธ๋žจ์ด ์™„๋ฃŒ๋˜๋ ค๋ฉด machine step์ด ๋ช‡ ๋ฒˆ ํ•„์š”ํ•œ๊ฐ€?"์ด๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ๊ทธ ๋‹ต์ด ์˜ค์ง ์ž…๋ ฅ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด์— ๋‹ฌ๋ ค์žˆ๋‹ค. ์ž…๋ ฅ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ 0์ด๋ฉด ํ•จ์ˆ˜๋Š” 0, 1, 2 ๋˜๋Š” ์•„์ฃผ ์ ์€ ์ˆ˜์˜ machine step๋งŒ ๋ฐŸ์„ ๊ฒƒ์ด๋‹ค. ์ด๋Š” machine step์„ ์–ด๋–ป๊ฒŒ ์„ธ๋Š”์ง€์— ๋‹ฌ๋ ธ๋‹ค. (ํŒจํ„ด ๋งค์นญ์— ํ•œ ๋‹จ๊ณ„, ๊ฐ’ 0์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ ํ•œ ๋‹จ๊ณ„ ๋“ฑ) ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ 1์ด๋ฉด ์–ด๋–จ๊นŒ? ์–ผ๋งˆ๋‚˜ ๊ฑธ๋ฆฌ๋“  ๊ฐ„์— ๊ธธ์ด 0์ผ ๋•Œ์˜ ๋‹จ๊ณ„ ์ˆ˜์™€ (์œ ์ผํ•œ) ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋‹จ๊ณ„ ์ˆ˜๋ฅผ ๋”ํ•œ ๋งŒํผ ๊ฑธ๋ฆด ๊ฒƒ์ด๋‹ค. ์ž…๋ ฅ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ n ์ผ ๋•Œ ๋นˆ ๋ฆฌ์ŠคํŠธ์— ๊ฑธ๋ฆฌ๋Š” ๋‹จ๊ณ„ ์ˆ˜๋ฅผ y๋ผ ํ•˜๊ณ  ๊ฐ ์›์†Œ๋งˆ๋‹ค ๋ง์…ˆ ๋ฐ ์žฌ๊ท€ ํ˜ธ์ถœ์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋‹จ๊ณ„ ์ˆ˜๋ฅผ x๋ผ ํ•˜์ž. ๋ง์…ˆ์„ n ๋ฒˆ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ด ํ•จ์ˆ˜์— ๊ฑธ๋ฆฌ๋Š” ์ด ์‹œ๊ฐ„์€ nx + y์ด๋‹ค. ์—ฌ๊ธฐ์„œ x์™€ y๋Š” ์ƒ์ˆ˜ ๊ฐ’๋“ค์ธ๋ฐ, n์— ๋ฌด๊ด€ํ•˜๊ณ  ์šฐ๋ฆฌ๊ฐ€ machine step์„ ์–ด๋–ป๊ฒŒ ์ •์˜ํ•˜๋Š”๊ฐ€์— ๋‹ฌ๋ ค์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”ํ•˜๊ฒŒ ์—ฌ๊ธฐ์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ์ด sum ํ•จ์ˆ˜์˜ ๋ณต์žก๋„๋Š” O(n) ("order n"์ด๋ผ ์ฝ๋Š”๋‹ค)์ด๋‹ค. ๋ฌด์–ธ๊ฐ€ O(n)์ด๋ผ๊ณ  ๋งํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–ค ์ƒ์ˆ˜ ์ธ์ž x, y์— ๋Œ€ํ•ด ์ด ํ•จ์ˆ˜๊ฐ€ ์™„๋ฃŒํ•˜๋Š”๋ฐ machine step์ด nx + y ํ•„์š”ํ•˜๋‹ค๋Š” ๋œป์ด๋‹ค. ๋‹ค์Œ์˜ ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ณด์ž. (ํ”ํžˆ "์‚ฝ์ž… ์ •๋ ฌ"์ด๋ผ ๋ถ€๋ฅธ๋‹ค) sort [] = [] sort [x] = [x] sort (x:xs) = insert (sort xs) where insert [] = [x] insert (y:ys) | x <= y = x : y : ys | otherwise = y : insert ys ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ด๋ ‡๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. ๋นˆ ๋ฆฌ์ŠคํŠธ๋‚˜ ์›์†Œ๊ฐ€ ํ•˜๋‚˜์ธ ๋ฆฌ์ŠคํŠธ๋Š” ์ด๋ฏธ ์ •๋ ฌ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ ๋ฆฌ์ŠคํŠธ๋Š” x:xs ๊ผด์ด๋‹ค. ์ด ๊ฒฝ์šฐ xs๋ฅผ ์ •๋ ฌํ•˜๊ณ  x๋ฅผ ์ ์ ˆํ•œ ์œ„์น˜์— ๋„ฃ์–ด์•ผ ํ•œ๋‹ค. insert ํ•จ์ˆ˜๊ฐ€ ๋ฐ”๋กœ ๊ทธ ์ผ์„ ํ•œ๋‹ค. insert๋Š” ์ด์ œ๋Š” ์ •๋ ฌ๋œ tail์„ ์ˆœํšŒํ•˜์—ฌ x๋ฅผ ์•Œ๋งž์€ ๊ณณ์— ์‚ฝ์ž…ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๊ฐ€ ์–ผ๋งˆ๋‚˜ ๊ฑธ๋ฆด์ง€ ๋ถ„์„ํ•ด ๋ณด์ž. ๊ธธ์ด n์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜๋Š”๋ฐ f(n) ๋‹จ๊ณ„๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ์›์†Œ๊ฐ€ n ๊ฐœ์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜๋ ค๋ฉด ๋จผ์ € ๊ทธ ๋ฆฌ์ŠคํŠธ์˜ tail์„ ์ •๋ ฌํ•ด์•ผ ํ•˜๊ณ  f(n-1) ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. ์ด์ œ ์ด ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— x๋ฅผ ์‚ฝ์ž…ํ•ด์•ผ ํ•œ๋‹ค. x๊ฐ€ ๋์— ์ถ”๊ฐ€๋œ๋‹ค๋ฉด O(n-1) = O(n) ๋‹จ๊ณ„๊ฐ€ ๊ฑธ๋ฆด ๊ฒƒ์ด๋‹ค. ์ „๋ถ€ ํ•ฉ์น˜๋ฉด O(n) ๋งŒํผ์˜ ์ž‘์—…์„ O(n) ๋ฒˆ๋งŒํผ ๋ฐ˜๋ณตํ•ด์•ผ ํ•˜๊ณ  ์ด๋Š” ์ด ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ด ๋ณต์žก๋„๊ฐ€ ( 2 ) ๋ผ๋Š” ๋œป์ด๋‹ค. ์ด ์ œ๊ณฑ์€ ์ƒ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ๋ฒ„๋ฆด ์ˆ˜ ์—†๋‹ค. ์ด๊ฒŒ ๋ฌด์Šจ ๋œป์ผ๊นŒ? ์•„์ฃผ ๊ธด ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด sum ํ•จ์ˆ˜ ์ž์ฒด๋Š” ๊ทธ๋ฆฌ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€ ์•Š์ง€๋งŒ sort ํ•จ์ˆ˜๋Š” ์‹œ๊ฐ„์ด ์ œ๋ฒ• ๊ฑธ๋ฆฐ๋‹ค. ๋ฌผ๋ก  ( 2 ) ๋ณด๋‹ค ๋Š๋ฆฌ๊ฒŒ ์‹คํ–‰๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค๋„ ์žˆ๊ณ  ( ) ๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ์‹คํ–‰๋˜๋Š” ๊ฒƒ๋“ค๋„ ์žˆ๋‹ค. (๋˜ํ•œ ( 2 ) ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ•œ ๋‹จ๊ณ„๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ ๊ฒŒ ๊ฑธ๋ฆฐ๋‹ค๋ฉด, ์‹ค์ œ๋กœ๋Š” ( ) ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ณด๋‹ค ๋น ๋ฅผ ์ˆ˜๋„ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์™€ ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๋žœ๋ค ์•ก์„ธ์Šค ํ•จ์ˆ˜๋“ค์„ ์ƒ๊ฐํ•ด ๋ณด์ž. ์ตœ์•…์˜ ๊ฒฝ์šฐ ๊ธธ์ด n์ธ ๋ฆฌ์ŠคํŠธ์˜ ์ž„์˜ ์›์†Œ์— ์ ‘๊ทผํ•˜๋Š” ์‹œ๊ฐ„์€ O(n)์ด๋‹ค. (๋งˆ์ง€๋ง‰ ์›์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค) ํ•˜์ง€๋งŒ ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ ์–ด๋–ค ์›์†Œ๋“  ์ฆ‰์‹œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด๊ฒƒ์„ ๋‘๊ณ  ์ƒ์ˆ˜ ์‹œ๊ฐ„ ๋˜๋Š” O(1)์ด ๊ฑธ๋ฆฐ๋‹ค๊ณ ๋“ค ๋งํ•œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ตœ๋Œ€ํ•œ ์ด๋งŒํผ๋งŒ ๋นจ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋ณต์žก๋„ ์ด๋ก ์—๋Š” ํ›จ์”ฌ ๋งŽ์€ ๊ฒƒ๋“ค์ด ์žˆ์ง€๋งŒ ์ด ํŠœํ† ๋ฆฌ์–ผ์˜ ๋…ผ์˜๋“ค์„ ์ดํ•ดํ•˜๋Š” ๋ฐ๋Š” ์ด ์ •๋„๋ฉด ์ถฉ๋ถ„ํ•  ๊ฒƒ์ด๋‹ค. O(1)์€ O(n)๋ณด๋‹ค ๋น ๋ฅด๊ณ  O(n)์€ ( 2 ) ๋ณด๋‹ค ๋น ๋ฅด๋‹ค๋Š” ๊ฒƒ๋งŒ ๊ธฐ์–ตํ•˜์ž. ์ตœ์ ํ™” ์›๋ฌธ ์—†์Œ 3 ํ•˜์Šค ์ผˆ ์‹ค์ „ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ดํ•ด, ๊ทธ๋ž˜ํ”ฝ ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์ถ•, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž‘์—… ๋“ฑ ์ผ์ƒ์ ์ธ ์ฃผ์ œ. ์ดˆ๊ธ‰๋ฐ˜์—์„œ ๋ฐ”๋กœ ์—ฌ๊ธฐ๋กœ ๋„˜์–ด์™€๋„ ๋œ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ ˆํผ๋Ÿฐ์Šค ์†Œ๊ฐœ Maybe - IO - Random ์ž๋ฃŒ๊ตฌ์กฐ ๊ธฐ์ดˆ ๋ฐฐ์—ด - ๋งต ์ผ๋ฐ˜์ ์ธ ์ž‘์—… ๋””๋ฒ„๊น… ํ…Œ์ŠคํŒ… ์†Œํ”„ํŠธ์›จ์–ด ํŒจํ‚ค์ง• (Cabal) Foreign Function Interface(FFI) ํ™œ์šฉํ•˜๊ธฐ ์ œ๋„ค๋ฆญ ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ์ •ํ˜•ํ™”๋œ ์ฝ”๋“œ๋Š” ๊ทธ๋งŒ ํŠน์ˆ˜ ์ž‘์—… ๊ทธ๋ž˜ํ”ฝ ์œ ์ € ์ธํ„ฐํŽ˜์ด์Šค (GUI) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค XML ๋‹ค๋ฃจ๊ธฐ ์ˆ˜ํ•™์‹์˜ ํŒŒ์‹ฑ ๊ธฐ๋ณธ์ ์ธ ํƒ€์ž… ๊ฒ€์‚ฌ๊ธฐ ์ž‘์„ฑ 1 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ ˆํผ๋Ÿฐ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ ˆํผ๋Ÿฐ์Šค ์†Œ๊ฐœ Maybe - IO - Random ์ž๋ฃŒ๊ตฌ์กฐ ๊ธฐ์ดˆ ๋ฐฐ์—ด - ๋งต 1 ์†Œ๊ฐœ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Libraries ํ•˜์Šค์ผˆ์˜ ํ’๋ถ€ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊ณ„์† ์ปค์ง€๊ณ  ์žˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ ๋ช‡ ๊ฐ€์ง€ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋‰œ๋‹ค. ("the Prelude"๋กœ ์นญํ•ด์ง€๋Š”) ํ‘œ์ค€ Prelude๋Š” ํ•˜์Šค ์ผˆ 2010 ํ‘œ์ค€์— ์ •์˜๋˜์–ด ์žˆ๊ณ  ์—ฌ๋Ÿฌ๋ถ„์ด ์ž‘์„ฑํ•˜๋Š” ๋ชจ๋“  ๋ชจ๋“ˆ์— ์ž๋™์œผ๋กœ ์ž„ํฌํŠธ ๋œ๋‹ค. ํ‘œ์ค€ Prelude๋Š” ๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ, ์ˆซ์ž ๊ฐ™์€ ํ‘œ์ค€ ํƒ€์ž…๊ณผ ์ด๋“ค์— ๋Œ€ํ•œ ์‚ฐ์ˆ , map, foldr ๋“ฑ ๊ธฐ๋ณธ์ ์ธ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ํ•œ๋‹ค. ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ์–ธ์–ด ํ‘œ์ค€์— ์ •์˜๋˜์–ด ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ž„ํฌํŠธ ํ•ด์•ผ ํ•œ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์˜ ๋ช…์„ธ๋Š” ํ•˜์Šค ์ผˆ 2010 ํ‘œ์ค€์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 1998๋…„๋ถ€ํ„ฐ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊พธ์ค€ํžˆ ๋Š˜์–ด๋‚˜ base ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๋Š” ์‚ฌ์‹ค์ƒ ํ‘œ์ค€์ด ๋˜์—ˆ๋‹ค. Hugs์™€ GHC ๋ชจ๋‘ ๋™์ผํ•œ base ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ์— ๋”ฐ๋ผ ์ถ”๊ฐ€์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋”ธ๋ ค์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ GHC๋Š” containers, text, bytestring ๊ฐ™์€ ๋„๋ฆฌ ์“ฐ์ด๋Š” ์œ ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ํฌํ•จํ•œ๋‹ค. 1 ์ด์™ธ์— ๋งŽ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ Hackage์—์„œ ๊ฐ€์ ธ์™€ cabal ์œ ํ‹ธ๋ฆฌํ‹ฐ๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์Šค ์ผˆ 98 ํ‘œ์ค€์ด ์ •ํ•ด์กŒ์„ ๋•Œ ๋ชจ๋“ˆ์—๋Š” 1์ฐจ์›์ ์ธ ์ด๋ฆ„ ๊ณต๊ฐ„์ด ๋ถ€์—ฌ๋˜์—ˆ๋‹ค. ์ด๊ฒƒ์ด ๊ทธ๋‹ค์ง€ ์ ์ ˆํ•˜์ง€ ์•Š์•„์„œ, ๋ชจ๋“ˆ ์ด๋ฆ„์— ์ (.)์„ ํ—ˆ์šฉํ•˜๋Š” ๊ณ„์ธต์  ์ด๋ฆ„ ๊ณต๊ฐ„์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. ์—ญํ˜ธํ™˜์„ฑ์„ ์œ„ํ•ด ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ ์—ฌ์ „ํžˆ ์ด์ „์˜ ๋น„๊ณ„์ธต์  ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ List์™€ Data.List ๋ชจ๋“ˆ์€ ๋™์ผํ•œ ํ‘œ์ค€ ๋ฆฌ์ŠคํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ์ž„ํฌํŠธ ํ•˜๋Š” ์ž์„ธํ•œ ๋ฐฉ๋ฒ•์€ Modules ์žฅ์„ ์ฐธ์กฐํ•˜๋ผ. ํ•˜์Šค ์ผˆ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ํŒจํ‚ค์ง• ํ•˜๊ธฐ ์œ„ํ•œ Cabal ์‹œ์Šคํ…œ์— ๋Œ€ํ•ด์„œ๋Š” Haskell/Packaging์„ ์ฐธ์กฐํ•˜๋ผ. Haddock ๋ฌธ์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ ˆํผ๋Ÿฐ์Šค ๋ฌธ์„œ๋Š” ๋Œ€๊ฐœ Haddock ๋„๊ตฌ๋ฅผ ์ด์šฉํ•ด ์ƒ์„ฑํ•œ๋‹ค. GHC์— ๋™๋ด‰๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค ์—ญ์‹œ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ฌธ์„œํ™”๋œ๋‹ค. ๋ฌธ์„œ๋ฅผ ์˜จ๋ผ์ธ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๊ณ  GHC๋ฅผ ์„ค์น˜ํ–ˆ๋‹ค๋ฉด ๋กœ์ปฌ ๋ณต์‚ฌ๋ณธ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. Haddock์€ ํ•˜์ดํผ๋งํฌ ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑํ•˜๋ฏ€๋กœ ๋ชจ๋“  ํ•จ์ˆ˜, ํƒ€์ž…, ํด๋ž˜์Šค ์ด๋ฆ„์€ ๋ˆ„๋ฅด๋ฉด ๊ทธ ์ •์˜๋กœ ์ด๋™ํ•œ๋‹ค. ๋„ˆ๋ฌด ๋งŽ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์งˆ๋ฆด ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์ค‘์š”ํ•œ ๊ฒƒ๋“ค๋งŒ ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ๋‹ค. Haddock์€ ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด ํƒ€์ž…๊ณผ ํด๋ž˜์Šค๋ฅผ ์ƒํ˜ธ ์ฐธ์กฐํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Data.Maybe ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Maybe ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ Ord์˜ ์ธ์Šคํ„ด์Šค๋กœ์„œ ๋‚˜์—ด๋œ๋‹ค. Ord a => Ord (Maybe a) Ord์˜ ์ธ์Šคํ„ด์Šค์ธ Foo ํƒ€์ž…์„ ์„ ์–ธํ•˜๋ฉด Maybe Foo ํƒ€์ž… ์—ญ์‹œ ์ž๋™์œผ๋กœ Ord์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋œ๋‹ค. ๋ฌธ์„œ์—์„œ ๋‹จ์–ด Ord๋ฅผ ๋ˆ„๋ฅด๋ฉด Ord ํด๋ž˜์Šค์˜ ์ •์˜๋กœ ์ด๋™ํ•˜์—ฌ ์•„์ฃผ ๊ธด ์ธ์Šคํ„ด์Šค ๋ชฉ๋ก์„ ๋ณผ ๊ฒƒ์ด๋‹ค. Maybe์˜ ์ธ์Šคํ„ด์Šค ์—ญ์‹œ ์ด ๋ชฉ๋ก์— ํฌํ•จ๋œ๋‹ค. ํŒจํ‚ค์ง€๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐฐํฌ ๋‹จ์œ„๋‹ค. ํŽธ๋ฆฌํ•œ ๋ฐฐํฌ์™€ ์„ค์น˜๋ฅผ ์œ„ํ•ด ๋ชจ๋“ˆ๋“ค์„ ๋ฌถ์€ ๊ฒƒ์ด๋‹ค. โ†ฉ 1 Maybe ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Libraries/Maybe ์ •์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ ์งˆ์˜ isJust isNothing ๋น ์ ธ๋‚˜์˜ค๊ธฐ maybe fromMaybe ๋ฆฌ์ŠคํŠธ์™€ Maybe listToMaybe maybeToList ๋ฆฌ์ŠคํŠธ ์กฐ์ž‘ ์–ด๋–ค ์‹คํŒจ๋Š” ๋ฌด์‹œํ•˜๊ณ  ๊ณ„์†ํ•˜๊ธฐ catMaybes mapMaybe ์‹คํŒจ์—์„œ ๋ฉˆ์ถ”๊ธฐ sequence Maybe ๋ฐ์ดํ„ฐ๋Š” ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์—์„œ ์“ฐ์ธ๋‹ค. ์™„์ „ํ•œ ์„ค๋ช…์€ Maybe ๋ชจ๋‚˜๋“œ์žฅ์— ์žˆ๋‹ค. ์ •์˜ ํ‘œ์ค€ Prelude๋Š” Maybe ํƒ€์ž…์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค. data Maybe a = Nothing | Just a ํƒ€์ž… a๋Š” ๋‹ค ํ˜•์ด๋ฏ€๋กœ ๋ณต์žกํ•œ ํƒ€์ž… ํ˜น์€ ์‹ฌ์ง€์–ด (IO () ํƒ€์ž… ๊ฐ™์€) ๋‹ค๋ฅธ ๋ชจ๋‚˜๋“œ๋ฅผ ํฌํ•จํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ Data.Maybe ๋ชจ๋“ˆ์€ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ผ๋ถ€๋กœ์„œ, Maybe ๊ฐ’์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋“ค์„ ํฌํ•จํ•œ๋‹ค. ์งˆ์˜ Maybe ๊ฐ’์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ฃผ๋Š” ์ž๋ช…ํ•œ ํ•จ์ˆ˜ ๋‘ ๊ฐœ๊ฐ€ ์žˆ๋‹ค. isJust isJust๋Š” ์ธ์ž๊ฐ€ Just _ ํ˜•ํƒœ๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. isJust :: Maybe a -> Bool isJust (Just _) = True isJust Nothing = False isNothing isNothing์€ ์ธ์ž๊ฐ€ Nothing ์ด๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. isNothing :: Maybe a -> Bool isNothing (Just _) = False isNothing Nothing = True ๋น ์ ธ๋‚˜์˜ค๊ธฐ Maybe ๊ฐ’์„ Maybe๊ฐ€ ์•„๋‹Œ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฟ ์žˆ๋‹ค. maybe maybe๋Š” Just ๋‚ด๋ถ€์˜ ๊ฐ’์— ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ Nothing์ด ์ฃผ์–ด์ง€๋ฉด ๊ธฐ๋ณธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. maybe :: b -> (a -> b) -> Maybe a -> b maybe _ f (Just x) = f x maybe z _ Nothing = z fromMaybe Just์— ์•„๋ฌด ํ•จ์ˆ˜๋„ ์ ์šฉํ•˜์ง€ ์•Š๊ณ  maybe๋ฅผ ์“ฐ๊ธธ ์›ํ•œ๋‹ค๋ฉด id ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ maybe๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค. Data.Maybe์—๋Š” ์ด๋ฏธ ๊ทธ๋Ÿฐ fromMaybe๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. fromMaybe :: a -> Maybe a -> a fromMaybe z = maybe z id ์ธ์ž ์ƒ๋žต์‹(point-free style)์„ ์“ด ๊ฒƒ์„ ์ฃผ๋ชฉํ•˜๋ผ. maybe z id๋Š” Maybe ๊ฐ’์„ ์ทจํ•˜๋Š” ํ•จ์ˆ˜๋กœ ํ‰๊ฐ€๋œ๋‹ค. ๋ฆฌ์ŠคํŠธ์™€ Maybe ๋ฆฌ์ŠคํŠธ์™€ Maybe ์‚ฌ์ด์˜ ๋งŽ์€ ์œ ์‚ฌ์ ์€ ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ์žฅ์—์„œ ๋‹ค๋ฃฌ ๋ฐ” ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์™€ Maybe๋ฅผ ์„œ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋“ค์ด ์žˆ๋‹ค. listToMaybe ์‹คํŒจํ•œ ๊ณ„์‚ฐ์€ ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ []๋ฅผ, Maybe์˜ ๊ฒฝ์šฐ Nothing์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. listToMaybe๋Š” ๋ฆฌ์ŠคํŠธ ๋ชจ๋‚˜๋“œ๋ฅผ Maybe ๋ชจ๋‚˜๋“œ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. Maybe๋Š” ๊ฐ’์„ ํ•˜๋‚˜๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ listToMaybe๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ•ด๋งŒ ์ทจํ•œ๋‹ค. listToMaybe :: [a] -> Maybe a listToMaybe [] = Nothing listToMaybe (x:_) = Just x maybeToList listToMaybe์˜ ๋ฐ˜๋Œ€๋Š” ๋‹น์—ฐํžˆ maybeToList์ด๋‹ค. maybeToList :: Maybe a -> [a] maybeToList Nothing = [] maybeToList (Just x) = [x] ๋ฆฌ์ŠคํŠธ ์กฐ์ž‘ ํ‰๋ฒ”ํ•œ Prelude ๋ฆฌ์ŠคํŠธ ์กฐ์ž‘ ํ•จ์ˆ˜๋“ค๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ Maybe ๊ฐ’์— ํŠนํ™”๋œ ํ•จ์ˆ˜๋“ค์ด ์žˆ๋‹ค. ์–ด๋–ค ์‹คํŒจ๋Š” ๋ฌด์‹œํ•˜๊ณ  ๊ณ„์†ํ•˜๊ธฐ ํ•œ ๋ถ€๋ถ„์ด ์‹คํŒจํ–ˆ๋‹ค๊ณ  ๋ชจ๋“  ๊ณ„์‚ฐ์ด ์‹คํŒจํ•˜์ง€๋Š” ์•Š๋Š” OR ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ๋‹ค. catMaybes catMaybes๋Š” Maybe ๊ฐ’๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด Just _ ํ˜•ํƒœ์˜ ๋ชจ๋“  ๊ฐ’์„ ์ถ”์ถœํ•˜๊ณ  Just ์ƒ์„ฑ์ž๋ฅผ ๋–ผ์–ด๋‚ธ๋‹ค. ๊ทธ ์ผ์€ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด ์ œ์‹œ์‹์ด ํ•œ๋‹ค. (ํŒจํ„ด ๋งค์นญ ์žฅ์—์„œ ๋ดค๋˜ ๊ฒƒ์ฒ˜๋Ÿผ) catMaybes :: [Maybe a] -> [a] catMaybes ms = [ x | Just x <- ms ] mapMaybe mapMaybe๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ ์šฉํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ˆ˜์ง‘ํ•œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์˜ ํ•ฉ์„ฑ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. mapMaybe :: (a -> Maybe b) -> [a] -> [b] mapMaybe f xs = catMaybes (map f xs) ์‚ฌ์‹ค Data.Maybe์— ๋“ค์–ด์žˆ๋Š” ์‹ค์ œ ์ •์˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋Š”๋ฐ ์ด๊ฒŒ ๋” ํšจ์œจ์ ์ด๋‹ค. mapMaybe :: (a -> Maybe b) -> [a] -> [b] mapMaybe _ [] = [] mapMaybe f (x:xs) = case f x of Just y -> y : mapMaybe f xs Nothing -> mapMaybe f xs ์‹คํŒจ์—์„œ ๋ฉˆ์ถ”๊ธฐ OR ๋Œ€์‹  ๋ชจ๋‘ ์„ฑ๊ณตํ–ˆ์„ ๋•Œ๋งŒ ๊ฐ’์„ ์ˆ˜์ง‘ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค๋ฉด sequence sequence :: [Maybe a] -> Maybe [a] sequence [] = Just [] sequence (Nothing:xs) = Nothing sequence (Just x:xs) = case sequence xs of Just xs' -> Just (x:xs') _ -> Nothing 2 IO ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Libraries/IO IO ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Bracket ํŒŒ์ผ ์ฝ๊ธฐ ํ”„๋กœ๊ทธ๋žจ IO ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์—ฌ๊ธฐ์„œ๋Š” System.IO ๋ชจ๋“ˆ์˜ ๊ฐ€์žฅ ํ”ํ•˜๊ฒŒ ์“ฐ์ด๋Š” ์š”์†Œ๋“ค์„ ์‚ดํŽด๋ณธ๋‹ค. data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode openFile :: FilePath -> IOMode -> IO Handle hClose :: Handle -> IO () hIsEOF :: Handle -> IO Bool hGetChar :: Handle -> IO Char hGetLine :: Handle -> IO String hGetContents :: Handle -> IO String getChar :: IO Char getLine :: IO String getContents :: IO String hPutChar :: Handle -> Char -> IO () hPutStr :: Handle -> String -> IO () hPutStrLn :: Handle -> String -> IO () putChar :: Char -> IO () putStr :: String -> IO () putStrLn :: String -> IO () readFile :: FilePath -> IO String writeFile :: FilePath -> String -> IO () ๋…ธํŠธ FilePath๋Š” String์˜ ํƒ€์ž… ๋™์˜์–ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด readFile ํ•จ์ˆ˜๋Š” String(์ฝ์„ ํŒŒ์ผ)์„ ๋ฐ›์•„์„œ ์•ก์…˜์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ ์ด ์•ก์…˜์„ ์‹คํ–‰ํ•˜๋ฉด ๊ทธ ํŒŒ์ผ์˜ ๋‚ด์šฉ๋ฌผ์„ ๋‚ด๋ฑ‰๋Š”๋‹ค. ํƒ€์ž… ๋™์˜์–ด์— ๊ด€ํ•ด์„œ๋Š” ํƒ€์ž… ์„ ์–ธ ์ฑ•ํ„ฐ๋ฅผ ๋ณผ ๊ฒƒ. ๋Œ€๋ถ€๋ถ„์˜ IO ํ•จ์ˆ˜๋Š” ๋ณด๊ธฐ๋งŒ ํ•ด๋„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. openFile๊ณผ hClose๋Š” ๊ฐ๊ฐ ํŒŒ์ผ์„ ์—ด๊ณ  ๋‹ซ๋Š”๋‹ค. IOMode ์ธ์ž๋Š” ํŒŒ์ผ์„ ์—ฌ๋Š” ๋ฐฉ์‹์„ ๊ฒฐ์ •ํ•œ๋‹ค. hIsEOF๋Š” ํŒŒ์ผ์˜ ๋์„ ๊ฒ€์‚ฌํ•œ๋‹ค. hGetChar์™€ hGetLine์€ ๊ฐ๊ฐ ํŒŒ์ผ์—์„œ ๋ฌธ์ž ํ•˜๋‚˜ ๋˜๋Š” ํ•œ ์ค„์„ ํ†ต์งธ๋กœ ์ฝ๋Š”๋‹ค. hGetContents๋Š” ํŒŒ์ผ ์ „์ฒด๋ฅผ ์ฝ๋Š”๋‹ค. ํŒŒ์ƒ๋œ ๋ฒ„์ „์ธ getChar, getLine, getContents๋Š” ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ฝ๋Š”๋‹ค. hPutChar๋Š” ํŒŒ์ผ์— ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. hPutStr์€ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. hPutStrLn์€ ๋์— ๊ฐœํ–‰ ๋ฌธ์ž๋ฅผ ๋ถ™์ธ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. h๊ฐ€ ์—†๋Š” ํŒŒ์ƒ ๋ฒ„์ „๋“ค์€ ํ‘œ์ค€ ์ถœ๋ ฅ์— ๋Œ€ํ•ด ์ž‘๋™ํ•œ๋‹ค. readFile๊ณผ writeFile ํ•จ์ˆ˜๋Š” ํŒŒ์ผ์„ ์—ด ํ•„์š” ์—†์ด ํŒŒ์ผ ์ „์ฒด๋ฅผ ์ฝ๊ฑฐ๋‚˜ ์“ด๋‹ค. Bracket bracket ํ•จ์ˆ˜๋Š” Control.Exception ๋ชจ๋“ˆ์— ๋“ค์–ด์žˆ๋‹ค. bracket์€ ์•ก์…˜์„ ์•ˆ์ „ํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฑธ ๋ณด์กฐํ•œ๋‹ค. bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c ํŒŒ์ผ์„ ์—ด์–ด ๋ฌธ์ž๋ฅผ ๊ธฐ๋กํ•˜๊ณ  ๊ทธ ํŒŒ์ผ์„ ๋‹ซ๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ๊ทธ๋Ÿฐ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” ๋„์ค‘์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ด๋„ ํŒŒ์ผ์ด ์„ฑ๊ณต์ ์œผ๋กœ ๋‹ซํžˆ๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•ด์•ผ ํ•œ๋‹ค. bracket ํ•จ์ˆ˜๋Š” ์ด๋Ÿฐ ์ผ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ ๋‹ค. bracket์€ ์„ธ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ์ฒ˜์Œ์— ์ˆ˜ํ–‰ํ•  ์•ก์…˜, ๋‘ ๋ฒˆ์งธ๋Š” ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋“  ์—†๋“  ๋งˆ์ง€๋ง‰์— ์ˆ˜ํ–‰ํ•  ์•ก์…˜, ์„ธ ๋ฒˆ์งธ๋Š” ์ค‘๊ฐ„์— ์ˆ˜ํ–‰๋˜๋ฉฐ ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜๋„ ์žˆ๋Š” ์•ก์…˜์ด๋‹ค. ์šฐ๋ฆฌ์˜ ๋ฌธ์ž๋ฅผ ๊ธฐ๋กํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. writeChar :: FilePath -> Char -> IO () writeChar fp c = bracket (openFile fp WriteMode) hClose (\h -> hPutChar h c) ์ด ํ•จ์ˆ˜๋Š” ํŒŒ์ผ์„ ์—ด์–ด ๋ฌธ์ž๋ฅผ ๊ธฐ๋กํ•˜๊ณ  ๊ทธ ํŒŒ์ผ์„ ๋‹ซ๋Š”๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ž ๊ธฐ๋ก์„ ์‹คํŒจํ•ด๋„ ์—ฌ์ „ํžˆ hClose๊ฐ€ ์‹คํ–‰๋˜๋ฉฐ ์˜ˆ์™ธ๋Š” ๋‚˜์ค‘์— ๋‹ค์‹œ ๋ฐœ์ƒํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์˜ˆ์™ธ๋ฅผ ์žก์•„์„œ ๋ชจ๋“  ํ•ธ๋“ค์„ ๋‹ซ๋Š” ๊ฒƒ์„ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ํŒŒ์ผ ์ฝ๊ธฐ ํ”„๋กœ๊ทธ๋žจ ์‚ฌ์šฉ์ž๊ฐ€ ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ๋ฒ•์€ ํ˜•ํŽธ์—†๊ณ  ๋ชจ๋“  ์˜ค๋ฅ˜๋ฅผ ์žก์•„๋‚ด์ง€๋„ ์•Š๋Š”๋‹ค. (์กด์žฌํ•˜์ง€ ์•Š๋Š” ํŒŒ์ผ์„ ์ฝ๋Š” ๋“ฑ) ๊ทธ๋ž˜๋„ IO๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๊ธฐ์—๋Š” ๊ฝค๋‚˜ ๊ดœ์ฐฎ์€ ์˜ˆ์‹œ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ "FileRead.hs"์— ์ž…๋ ฅํ•˜๊ณ  ์ปดํŒŒ์ผ ๋ฐ ์‹คํ–‰ํ•ด ๋ณด์ž. import System.IO import Control.Exception main = doLoop doLoop = do putStrLn "Enter a command rFN wFN or q to quit:" command <- getLine case command of 'q':_ -> return () 'r':filename -> do putStrLn ("Reading " ++ filename) doRead filename doLoop 'w':filename -> do putStrLn ("Writing " ++ filename) doWrite filename doLoop _ -> doLoop doRead filename = bracket (openFile filename ReadMode) hClose (\h -> do contents <- hGetContents h putStrLn "The first 100 chars:" putStrLn (take 100 contents)) doWrite filename = do putStrLn "Enter text to go into the file:" contents <- getLine bracket (openFile filename WriteMode) hClose (\h -> hPutStrLn h contents) ์ด ํ”„๋กœ๊ทธ๋žจ์€ ๋ฌด์Šจ ์ผ์„ ํ• ๊นŒ? ๋จผ์ € ์‚ฌ์šฉ๋ฒ•์„ ์งง๊ฒŒ ์•Œ๋ฆฌ๊ณ  ์ปค๋งจ๋“œ๋ฅผ ์ฝ๋Š”๋‹ค. ๊ทธ๋‹ค์Œ ์ปค๋งจ๋“œ์— ๋Œ€ํ•ด case ๋ถ„๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•ด์„œ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ 'q'์ธ์ง€ ํ™•์ธํ•œ๋‹ค. ๋งž๋Š”๋‹ค๋ฉด ์œ ๋‹› ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋…ธํŠธ return ํ•จ์ˆ˜๋Š” a ํƒ€์ž…์˜ ๊ฐ’์„ ์ทจํ•ด IO a ํƒ€์ž…์˜ ์•ก์…˜์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ return ()์˜ ํƒ€์ž…์€ IO ()์ด๋‹ค. ์ปค๋งจ๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ 'q'๊ฐ€ ์•„๋‹ˆ๋ฉด ์ด ํ”„๋กœ๊ทธ๋žจ์€ ๊ทธ ๋ฌธ์ž๊ฐ€ 'r'์ด๊ณ  ๋ณ€์ˆ˜ filename์— ๋ฐ”์ธ๋”ฉ ๋  ๋ฌธ์ž์—ด์ด ์ด์–ด์„œ ๋“ค์–ด์˜ค๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ ํŒŒ์ผ์„ ์ฝ๊ณ  ์žˆ๋‹ค๊ณ  ์•Œ๋ฆฌ๊ณ  ์ฝ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ doLoop๋ฅผ ๋‹ค์‹œ ์‹คํ–‰ํ•œ๋‹ค. w์— ๋Œ€ํ•œ ๊ฒ€์‚ฌ๋„ ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ ๋งŒ๋Šฅ ๋ฌธ์ž์ธ _์— ์ผ์น˜ํ•˜๊ณ  doLoop๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค. doRead ํ•จ์ˆ˜๋Š” bracket ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ํŒŒ์ผ ์ฝ๊ธฐ์— ๋ฌธ์ œ๊ฐ€ ์—†์Œ์„ ๋ณด์žฅํ•œ๋‹ค. doRead๋Š” ํŒŒ์ผ์„ ReadMode ๋ชจ๋“œ๋กœ ์—ด์–ด์„œ ๊ทธ ๋‚ด์šฉ๋ฌผ์„ ์ฝ๊ณ  ์ฒ˜์Œ 100๊ฐœ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. (take ํ•จ์ˆ˜๋Š” ์ •์ˆ˜ n๊ณผ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด์„œ ๊ทธ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ n ๊ฐœ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค) doWrite ํ•จ์ˆ˜๋Š” ํ…์ŠคํŠธ๋ฅผ ์š”๊ตฌํ•˜๊ณ  ํ‚ค๋ณด๋“œ๋กœ๋ถ€ํ„ฐ ๊ทธ ํ…์ŠคํŠธ๋ฅผ ์ฝ์–ด ์ง€์ •๋œ ํŒŒ์ผ์— ๊ทธ ํ…์ŠคํŠธ๋ฅผ ๊ธฐ๋กํ•œ๋‹ค. ๋…ธํŠธ doRead์™€ doWrite๋Š” readFile๊ณผ writeFile์„ ์‚ฌ์šฉํ•ด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ ๋” ๋ณต์žกํ•œ ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ํ’€์–ด์ผ๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ์€ ํฐ ๋ฌธ์ œ๊ฐ€ ํ•˜๋‚˜ ์žˆ๋‹ค. ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํŒŒ์ผ์„ ์ฝ๊ฑฐ๋‚˜ *\bs^#_@ ๊ฐ™์ด ์ด์ƒํ•œ ํŒŒ์ผ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ฃฝ๋Š”๋‹ค. doRead์™€ doWrite์— ๋“ค์–ด์žˆ๋Š” bracket ํ˜ธ์ถœ์ด ๊ทธ๋Ÿฐ ์ผ์„ ๋ฐฉ์ง€ํ•ด์•ผ ํ•  ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ๊ทธ๋ ‡์ง€ ์•Š๋‹ค. bracket์€ ๋ชธ์ฒด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋งŒ ์žก์œผ๋ฉฐ ๊ตฌ๋™ ํ•จ์ˆ˜์™€ ๋งˆ๋ฌด๋ฆฌ ํ•จ์ˆ˜(openFile๊ณผ hClose)์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋Š” ์žก์ง€ ์•Š๋Š”๋‹ค. ์™„์ „ํžˆ ์•ˆ์ „ํ•˜๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด openFile์ด ์ผ์œผํ‚ค๋Š” ์˜ˆ์™ธ๋ฅผ ์žก์„ ์ˆ˜๋‹จ์ด ํ•„์š”ํ•˜๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ์œ„ ํ”„๋กœ๊ทธ๋žจ์„ ๋ณ€ํ˜•ํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ํŒŒ์ผ ์ฝ๊ธฐ, ํŒŒ์ผ ์“ฐ๊ธฐ, ์ข…๋ฃŒ ์ค‘ ๋ฌด์—‡์„ ์›ํ•˜๋Š”์ง€๋ฅผ ๋จผ์ € ๋ฌผ์–ด๋ณด๋„๋ก ๋งŒ๋“ค ๊ฒƒ. ์‚ฌ์šฉ์ž๊ฐ€ "quit"์œผ๋กœ ์‘๋‹ตํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜์–ด์•ผ ํ•œ๋‹ค. "read"๋กœ ์‘๋‹ตํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์€ ํŒŒ์ผ ์ด๋ฆ„์„ ์š”์ฒญํ•˜๊ณ  ๊ทธ ํŒŒ์ผ์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋ผ. (ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ํฌ๋ž˜์‹œ๊ฐ€ ๋‚  ๊ฒƒ์ด๋‹ค) "write"๋กœ ์‘๋‹ตํ•˜๋ฉด ํŒŒ์ผ ์ด๋ฆ„์„ ์š”์ฒญํ•œ ๋‹ค์Œ ๊ทธ ํŒŒ์ผ์— ๊ธฐ๋กํ•  ํ…์ŠคํŠธ๋ฅผ "."๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€ ๋ฐ›๋Š”๋‹ค. ๋‹จ "." ์ž์ฒด๋„ ํŒŒ์ผ์— ๊ธฐ๋กํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผ ํ•œ๋‹ค. Do you want to [read] a file, [write] a file, or [quit]? read Enter a file name to read: foo ...contents of foo... Do you want to [read] a file, [write] a file, or [quit]? write Enter a file name to write: foo Enter text (dot on a line by itself to end): this is some text for foo Do you want to [read] a file, [write] a file, or [quit]? read Enter a file name to read: foo this is some text for foo Do you want to [read] a file, [write] a file, or [quit]? blech I don't understand the command blech. Do you want to [read] a file, [write] a file, or [quit]? quit Goodbye! 3 Random ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Libraries/Random Random์˜ ์˜ˆ์‹œ๋“ค ํ‘œ์ค€ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ QuickCheck๋ฅผ ํ™œ์šฉํ•ด ๋ฌด์ž‘์œ„ ๋ฐ์ดํ„ฐ ์ƒ์„ฑํ•˜๊ธฐ Random์˜ ์˜ˆ์‹œ๋“ค ๋‚œ์ˆ˜๋Š” ๋งŽ์€ ์“ฐ์ž„์ƒˆ๋ฅผ ๊ฐ€์ง„๋‹ค. ์˜ˆ์‹œ: ์ž„์˜์˜ ์ •์ˆ˜ 10๊ฐœ import System.Random main = do gen <- newStdGen let ns = randoms gen :: [Int] print $ take 10 ns IO ์•ก์…˜ newStdGen์€ ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” StdGen๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ด StdGen์€ ์˜์‚ฌ ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š” ํ•จ์ˆ˜๋“ค์— ์ „๋‹ฌ๋  ์ˆ˜ ์žˆ๋‹ค. (์‹œ์Šคํ…œ์— ์˜์กด์ ์ธ ๋ฐฉ์‹์œผ๋กœ ์ž๋™ ์ดˆ๊ธฐํ™”๋˜๋Š” ์ „์—ญ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ๋„ ์กด์žฌํ•œ๋‹ค. ์ด ์ƒ์„ฑ๊ธฐ๋Š” IO ๋ชจ๋‚˜๋“œ ๋‚ด์—์„œ ์œ ์ง€๋˜๋ฉฐ getStdGen์„ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฑด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๊น”๋”ํ•˜์ง€ ๋ชปํ•œ ๋ฉด์ด ์žˆ๋Š”๋ฐ, newStdGen๋งŒ ์žˆ์œผ๋ฉด ์ถฉ๋ถ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.) ํ˜น์€ mkStdGen์„ ํ™œ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ์‹œ: mkStdGen์„ ์ด์šฉํ•œ ๋ฌด์ž‘์œ„ float 10๊ฐœ import System.Random randomList :: (Random a) => Int -> [a] randomList seed = randoms (mkStdGen seed) main :: IO () main = do print $ take 10 (randomList 42 :: [Float]) ์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๋น„์Šทํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค. [0.110407025,0.8453985,0.3077821,0.78138804,0.5242582,0.5196911,0.20084688,0.4794773,0.3240164, 6.1566383e-2] ์˜ˆ์‹œ: ๋ฆฌ์ŠคํŠธ๋ฅผ ํ—ค์ง‘์–ด๋†“๊ธฐ (์™„๋ฒฝํ•˜์ง€ ์•Š์Œ) import Data.List ( sortBy) import Data.Ord ( comparing) import System.Random ( Random, RandomGen, randoms, newStdGen) main :: IO () main = do gen <- newStdGen interact $ unlines . unsort gen . lines unsort :: (RandomGen g) => g -> [x] -> [x] unsort g es = map snd . sortBy (comparing fst) $ zip rs es where rs = randoms g :: [Integer] randoms๋Š” ๋‚œ์ˆ˜ ์ƒ์„ฑ๋งŒ์„ ์œ„ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด random('s'๊ฐ€ ์—†์Œ)์„ ์ด์šฉํ•ด ๋‚œ์ˆ˜ ํ•˜๋‚˜์™€ ์ƒˆ๋กœ์šด StdGen์„ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ StdGen์„ ์ด์šฉํ•ด ๋‹ค์Œ StdGen์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ randomR๊ณผ randomRs๋Š” ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋Š” ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋‹ค. ๊ณ„์† ์ฝ์œผ๋ฉด ๋” ๋งŽ์€ ์˜๊ฐ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ํ‘œ์ค€ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ ํ•˜์Šค ์ผˆ ํ‘œ์ค€ ๋‚œ์ˆ˜ ํ•จ์ˆ˜ ๋ฐ ํƒ€์ž…์€ System.Random ๋ชจ๋“ˆ์— ์ •์˜๋˜์–ด ์žˆ๋‹ค. ๋ฌด์ž‘์œ„์˜ ์ •์˜๋Š” ๊ทธ ์ž์‹ ์„ ๋” ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํด๋ž˜์Šค๋“ค์„ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ๋‹ค์†Œ ๋‚œํ•ดํ•˜๋‹ค. ํ‘œ์ค€์— ๋”ฐ๋ฅด๋ฉด: ---------------- The RandomGen class ------------------------ class RandomGen g where genRange :: g -> (Int, Int) next :: g -> (Int, g) split :: g -> (g, g) ---------------- A standard instance of RandomGen ----------- data StdGen = ... -- Abstract ์—ฌ๊ธฐ์„œ๋Š” ํ‘œ์ค€ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ "๊ฐ์ฒด"์ธ StdGen์„ ๋„์ž…ํ•œ๋‹ค. ์ด๊ฒƒ์€ RandomGen ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ด๋ฉฐ ์ด ํด๋ž˜์Šค๋Š” System.Random ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ์—ฐ์‚ฐ๋“ค์„ ๊ธฐ์ˆ ํ•œ๋‹ค. r :: StdGen์— ๋Œ€ํ•ด ๋‹ค์Œ์„ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. (x, r2) = next r ์ด๋Ÿฌ๋ฉด ๋ฌด์ž‘์œ„ Int์ธ x์™€ ์ƒˆ๋กœ์šด StdGen์ธ r2๋ฅผ ์–ป๋Š”๋‹ค. next ํ•จ์ˆ˜๋Š” RandomGen ํด๋ž˜์Šค์— ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉฐ StdGen ํƒ€์ž…์˜ ๋ฌด์–ธ๊ฐ€์— next๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์•„๋ž˜์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ StdGen์€ RandomGen ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ‘œ์ค€์— ๋”ฐ๋ฅด๋ฉด: instance RandomGen StdGen where ... instance Read StdGen where ... instance Show StdGen where ... ์ด๋Š” StdGen๊ณผ ๋ฌธ์ž์—ด์„ ์„œ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ด๊ธฐ๋„ ํ•˜๋‹ค. (... ๋ถ€๋ถ„์€ ํ•˜์Šค ์ผˆ ๊ตฌ๋ฌธ์ด ์•„๋‹ˆ๋ผ ํ‘œ์ค€์€ ์ด ์ธ์Šคํ„ด์Šค๋“ค์˜ ๊ตฌํ˜„์„ ์ •์˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋œป์ด๋‹ค.) ํ‘œ์ค€์— ๋”ฐ๋ฅด๋ฉด: mkStdGen :: Int -> StdGen ์”จ๊ฐ’ Int์„ mkStdGen ํ•จ์ˆ˜์— ๋„ฃ์œผ๋ฉด ์ƒ์„ฑ๊ธฐ๋ฅผ ์–ป๋Š”๋‹ค. ํ•จ์ˆ˜ํ˜• ์–ธ์–ด๋กœ์„œ ํ•˜์Šค์ผˆ์€ next๋ฅผ ํ†ตํ•ด ์ƒˆ๋กœ์šด ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด๋“ค์—์„œ ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ ๋ฃจํ‹ด์€ ์ƒ์„ฑ๊ธฐ์˜ ์ƒํƒœ๋ฅผ ๋ฐ”๊พธ๋Š” ์ˆจ๊ฒจ์ง„ ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๋ฅผ ๊ฐ€์ง„๋‹ค. ํ•˜์Šค์ผˆ์€ ๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์Šค์ผˆ์—์„œ ์˜์‚ฌ ๋‚œ์ˆ˜ ์„ธ ๊ฐœ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•ด์•ผ ํ•œ๋‹ค. let (x1, r2) = next r (x2, r3) = next r2 (x3, r4) = next r3 ๋ฌด์ž‘์œ„ ๊ฐ’ x1, x2, x3์€ ๊ทธ ์ž์ฒด๋กœ ๋ฌด์ž‘์œ„ ์ •์ˆ˜๋‹ค. (0, 999) ๊ฐ™์€ ๊ตฌ๊ฐ„์œผ๋กœ ํ•œ์ •ํ•˜๋ ค๋ฉด ๊ทธ๊ฑธ ๊ณ ๋ คํ•˜์—ฌ ์ œ์ž‘๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฃจํ‹ด์ด ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ๊ทธ๋Ÿฐ ๋ฃจํ‹ด์ด ์‹ค์ œ๋กœ ์žˆ๋‹ค. ํ‘œ์ค€์— ๋”ฐ๋ฅด๋ฉด: ---------------- The Random class --------------------------- class Random a where randomR :: RandomGen g => (a, a) -> g -> (a, g) random :: RandomGen g => g -> (a, g) randomRs :: RandomGen g => (a, a) -> g -> [a] randoms :: RandomGen g => g -> [a] randomRIO :: (a, a) -> IO a randomIO :: IO a StdGen์€ RandomGen ํƒ€์ž…์˜ ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค์ž„์„ ๊ธฐ์–ตํ•˜๋ผ. (์—ฌ๋Ÿฌ๋ถ„์ด ์ง์ ‘ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š” ํ•œ) ๋”ฐ๋ผ์„œ ์œ„์˜ ํƒ€์ž…๋“ค ์ค‘ g๋ฅผ StdGen ์œผ๋กœ ์น˜ํ™˜ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. randomR :: (a, a) -> StdGen -> (a, StdGen) random :: StdGen -> (a, StdGen) randomRs :: (a, a) -> StdGen -> [a] randoms :: StdGen -> [a] ํ•˜์ง€๋งŒ ์ด ๋ชจ๋“  ๊ฒƒ์€ Random์ด๋ผ๋Š” ๋˜ ๋‹ค๋ฅธ ํด๋ž˜์Šค ์„ ์–ธ ๋‚ด๋ถ€์— ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์ž. ์ฆ‰ Random์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋Š” ์ด ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ‘œ์ค€์—์„œ Random์˜ ์ธ์Šคํ„ด์Šค์ธ ๊ฒƒ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. instance Random Integer where ... instance Random Float where ... instance Random Double where ... instance Random Bool where ... instance Random Char where ... ์ฆ‰ ์œ„์˜ ๋ชจ๋“  ํƒ€์ž…์— ๋Œ€ํ•ด ๋‚œ์ˆ˜์˜ ๊ตฌ๊ฐ„์„ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌด์ž‘์œ„ ์ •์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜๋„ ์žˆ๊ณ  (x1, r2) = randomR (0,999) r ๋ฌด์ž‘์œ„ ๋Œ€๋ฌธ์ž๋ฅผ ์–ป์„ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ (c2, r3) = randomR ('A', 'Z') r2 ์‹ฌ์ง€์–ด ๋ฌด์ž‘์œ„ ๋น„ํŠธ๋ฅผ ์–ป์„ ์ˆ˜๋„ ์žˆ๋‹ค. (b3, r4) = randomR (False, True) r3 ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ข‹๋‹ค. ํ•˜์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ ์ „์ฒด์— ๊ฑธ์ณ ๋‚œ์ˆ˜ ์ƒํƒœ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์€ ๊ณ ํ†ต์Šค๋Ÿฝ๊ณ  ์˜ค๋ฅ˜์— ์ทจ์•ฝํ•˜๋ฉฐ ํ”„๋กœ๊ทธ๋žจ์˜ ํ›Œ๋ฅญํ•˜๊ณ  ๊น”๋”ํ•œ ํ•จ์ˆ˜ํ˜• ์„ฑ์งˆ์„ ํŒŒ๊ดดํ•˜๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋‹ค. RandomGen ํด๋ž˜์Šค์˜ split ํ•จ์ˆ˜๋Š” ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ์–ด๋Š ์ •๋„๋Š” ํ•ด๊ฒฐํ•œ๋‹ค. split์€ ์ƒ์„ฑ๊ธฐ๋ฅผ ํ•˜๋‚˜ ๋ฐ›์•„์„œ ์ƒ์„ฑ๊ธฐ ๋‘ ๊ฐœ๋ฅผ ๋Œ๋ ค์ค€๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ, (r1, r2) = split r x = foo r1 ์—ฌ๊ธฐ์„œ๋Š” r1์„ foo ํ•จ์ˆ˜์— ์ „๋‹ฌํ•ด ๋ชจ์ข…์˜ ์ž‘์—…์„ ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ x๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด์ œ ๋‹ค์Œ์— ์˜ฌ ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ์—๋Š” r2๋ฅผ ์“ฐ๋ฉด ๋œ๋‹ค. split์ด ์—†์—ˆ๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ์จ์•ผ ํ–ˆ์„ ๊ฒƒ์ด๋‹ค. (x, r2) = foo r1 ๊ฐ€๋”์€ ์ด๊ฒƒ์กฐ์ฐจ ๊ท€์ฐฎ์„ ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋น ๋ฅด๋ฉด์„œ๋„ ์ง€์ €๋ถ„ํ•œ ๋ฐฉ๋ฒ•์€ ๋ชจ๋“  ๊ฒƒ์„ IO ๋ชจ๋‚˜๋“œ ์•ˆ์— ์ง‘์–ด๋„ฃ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ์–ธ์–ด๋“ค์ฒ˜๋Ÿผ ํ‘œ์ค€ ์ „์—ญ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ๋ฅผ ์–ป๊ฒŒ ๋œ๋‹ค. ํ‘œ์ค€์— ๋”ฐ๋ฅด๋ฉด: ---------------- The global random generator ---------------- newStdGen :: IO StdGen setStdGen :: StdGen -> IO () getStdGen :: IO StdGen getStdRandom :: (StdGen -> (a, StdGen)) -> IO a ์ด์ œ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. foo :: IO Int foo = do r1 <- getStdGen let (x, r2) = randomR (0,999) r1 setStdGen r2 return x ์ „์—ญ ์ƒ์„ฑ๊ธฐ๋ฅผ ํš๋“ํ•˜๊ณ , ์‚ฌ์šฉํ•˜๊ณ , ๊ฐฑ์‹ ํ•œ๋‹ค. (๊ทธ๋Ÿฌ์ง€ ์•Š์œผ๋ฉด ๋ชจ๋“  ๋‚œ์ˆ˜๊ฐ€ ๋˜‘๊ฐ™์„ ๊ฒƒ์ด๋‹ค) ํ•˜์ง€๋งŒ ๋งค๋ฒˆ ์ „์—ญ ์ƒ์„ฑ๊ธฐ๋ฅผ ํš๋“ํ•ด ๊ฐฑ์‹ ํ•˜๋Š” ๊ฒƒ์€ ๊ณ ํ†ต์Šค๋Ÿฝ๊ธฐ ๋•Œ๋ฌธ์— ๋ณดํ†ต์€ getStdRandom์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๊ฒƒ์˜ ์ธ์ž๋Š” ํ•จ์ˆ˜๋‹ค. ๊ทธ ํ•จ์ˆ˜์˜ ํƒ€์ž…์„ random๊ณผ randomR์˜ ํƒ€์ž…์— ๋Œ€์กฐํ•ด ๋ณด๋ฉด ์ž˜ ๋“ค์–ด๋งž๋Š”๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ IO ๋ชจ๋‚˜๋“œ ์•ˆ์—์„œ ๋ฌด์ž‘์œ„ ์ •์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. x <- getStdRandom $ randomR (1,999) randomR (1,999)์˜ ํƒ€์ž…์€ StdGen -> (Int, StdGen)์ด๋ฏ€๋กœ getStdRandom์˜ ์ธ์ž๋กœ์„œ ๊ผญ ๋“ค์–ด๋งž๋Š”๋‹ค. QuickCheck๋ฅผ ํ™œ์šฉํ•ด ๋ฌด์ž‘์œ„ ๋ฐ์ดํ„ฐ ์ƒ์„ฑํ•˜๊ธฐ IO ๋ชจ๋‚˜๋“œ๋งŒ์„ ํ†ตํ•ด์„œ ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑด ๊ณ ํ†ต์ด๋‹ค. ์ฝ”๋“œ ๊นŠ์ˆ™ํ•œ ๊ณณ์˜ ์–ด๋–ค ํ•จ์ˆ˜์— ๋‚œ์ˆ˜๊ฐ€ ํ•„์š”ํ•ด์กŒ๊ณ  ์—ฌ๋Ÿฌ๋ถ„์€ ์ด์ œ ํ”„๋กœ๊ทธ๋žจ ์ ˆ๋ฐ˜์„ ์ˆœ์ˆ˜ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ IO ์•ก์…˜์œผ๋กœ ์žฌ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ ๊ทธ๋ณด๋‹ค ์œ„์— ์žˆ๋Š” ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ๊ฑฐ์ณ StdGen ์ธ์ž๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค. ์ข€ ๋” ์ˆœ์ˆ˜ํ•œ ๋ฐฉ์‹์€ ์—†์„๊นŒ? State ๋ชจ๋‚˜๋“œ ์ฑ•ํ„ฐ๋ฅผ ๋– ์˜ฌ๋ ค๋ณด๋ฉด ์ด๋Ÿฐ ํŒจํ„ด์€ let (x1, r2) = next r (x2, r3) = next r2 (x3, r4) = next r3 do ํ‘œ๊ธฐ๋กœ๋„ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. do -- Not real Haskell x1 <- random x2 <- random x3 <- random ๋ฌผ๋ก  IO ๋ชจ๋‚˜๋“œ ์•ˆ์ด๋ผ๋ฉด ์ด๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋‚œ ์ˆ˜๋“ค์ด ๋‚œ์ˆ˜ ๊ณ„์‚ฐ์— ํŠนํ™”๋œ ์ž๊ธฐ๋งŒ์˜ ์ž‘์€ ๋ชจ๋‚˜๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋” ์ข‹๋‹ค. ๋‹คํ–‰ํžˆ๋„ ๊ทธ๋Ÿฐ ๋ชจ๋‚˜๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค. ๊ทธ ๋ชจ๋‚˜๋“œ๋Š” Test.QuickCheck ๋ชจ๋“ˆ์— ์žˆ์œผ๋ฉฐ Gen์ด๋ผ๊ณ  ํ•œ๋‹ค. Gen์ด Test.QuickCheck์— ๋“ค์–ด์žˆ๋Š” ๊ฒƒ์—๋Š” ์—ญ์‚ฌ์ ์ธ ์ด์œ ๊ฐ€ ์žˆ๋‹ค. Gen์€ ์—ฌ๊ธฐ์„œ ๋ฐœ๋ช…๋˜์—ˆ๋‹ค. QuickCheck์˜ ๋ชฉ์ ์€ ์ž„์˜์˜ ์œ ๋‹› ํ…Œ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ด ์—ฌ๋Ÿฌ๋ถ„์˜ ์ฝ”๋“œ์˜ ์—ฌ๋Ÿฌ ์„ฑ์งˆ์„ ๊ฒ€์ฆํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (์—ฌ๋‹ด์ด์ง€๋งŒ QuickCheck๋Š” ๋†€๋ž๋„๋ก ์ž˜ ์ž‘๋™ํ•˜๋ฉฐ ๋Œ€๋ถ€๋ถ„์˜ ํ•˜์Šค ์ผˆ ๊ฐœ๋ฐœ์ž๋Š” ํ…Œ์ŠคํŒ…์„ ์œ„ํ•ด QuickCheck๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.) ์„ธ๋ถ€์‚ฌํ•ญ์€ HaskellWiki์˜ [Introduction to QuickCheck](https://wiki.haskell.org/Introduction_to_QuickCheck2)๋ฅผ ๋ณผ ๊ฒƒ. ์—ฌ๊ธฐ์„œ๋Š” Gen ๋ชจ๋‚˜๋“œ๋ฅผ ์ด์šฉํ•œ ๋ฌด์ž‘์œ„ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ์— ์ง‘์ค‘ํ•˜๊ฒ ๋‹ค. QuickCheck ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด QuickCheck ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค. ์„ค์น˜ํ•˜๊ณ  ๋‚˜๋ฉด ์†Œ์Šค ํŒŒ์ผ์— ์ด๊ฒƒ๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค. import Test.QuickCheck Gen ๋ชจ๋‚˜๋“œ๋Š” ๋‚œ์ˆ˜ ๊ณ„์‚ฐ์— ๋Œ€ํ•œ ๋ชจ๋‚˜๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. Gen์€ ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐ„๋‹จํ•œ ๊ฐ’๋“ค๋กœ๋ถ€ํ„ฐ ๋ณต์žกํ•œ ๊ฐ’์„ ๊ตฌ์ถ•ํ•˜๋Š” ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด 0๊ณผ 999 ์‚ฌ์ด์˜ ์ž„์˜ ์ •์ˆ˜ 3๊ฐœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฃจํ‹ด์œผ๋กœ ์‹œ์ž‘ํ•ด ๋ณด์ž. randomTriple :: Gen (Integer, Integer, Integer) randomTriple = do x1 <- choose (0,999) x2 <- choose (0,999) x3 <- choose (0,999) return (x1, x2, x3) choose๋Š” QuickCheck์˜ ํ•จ์ˆ˜๋“ค ์ค‘ ํ•˜๋‚˜๋กœ์„œ randomR์— ๋Œ€์‘ํ•œ๋‹ค. choose :: Random a => (a, a) -> Gen a ์ฆ‰ Random์˜ ์ธ์Šคํ„ด์Šค์ธ ์–ด๋–ค ํƒ€์ž… a์— ๋Œ€ํ•ด choose๋Š” ๊ตฌ๊ฐ„์„ ์ƒ์„ฑ๊ธฐ๋กœ ์‚ฌ์ƒํ•œ๋‹ค. Gen ์•ก์…˜์„ ์–ป์—ˆ์œผ๋‹ˆ ์ด๊ฑธ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค. unGen ์•ก์…˜์€ ๊ทธ ์•ก์…˜์„ ์‹คํ–‰ํ•˜๊ณ  ๋ฌด์ž‘์œ„ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. unGen :: Gen a -> StdGen -> Int -> a ์„ธ ์ธ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ƒ์„ฑ๊ธฐ ์•ก์…˜ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ ๊ฒฐ๊ณผ์˜ "ํฌ๊ธฐ". ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” ์ด๊ฑธ ํ™œ์šฉํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ์—ฌ๋Ÿฌ๋ถ„์ด ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ณ€ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌ์ถ•ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์ด ์ธ์ž๋Š” ๊ทธ ์˜ˆ์ƒ๋˜๋Š” ํฌ๊ธฐ๋ฅผ ์ƒ์„ฑ๊ธฐ์— ์ „๋‹ฌํ•œ๋‹ค. ์ด์— ๋Œ€ํ•œ ์˜ˆ์‹œ๋Š” ๋‚˜์ค‘์— ์•Œ์•„๋ณด๊ฒ ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์ด๊ฒƒ์€ ์ž„์˜์˜ ์ˆซ์ž ์„ธ ๊ฐœ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. let triple = unGen randomTriple (mkStdGen 1) 1 ํ•˜์ง€๋งŒ ์ด ์ˆซ์ž๋“ค์€ ํ•ญ์ƒ ๋™์ผํ•œ๋ฐ, ๋˜‘๊ฐ™์€ ์”จ๊ฐ’์„ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ์ˆซ์ž๋“ค์„ ์›ํ•œ๋‹ค๋ฉด ๊ฐ๊ฐ ๋‹ค๋ฅธ StdGen ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ๋Š” ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ๋ฅผ ์ด์šฉํ•ด ๋‘ ์•ก์…˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ทจํ•œ๋‹ค. -- Not Haskell code r := random (0,1) if r == 1 then foo else bar QuickCheck๋Š” ๊ฐ™์€ ์ผ์„ ํ•˜์ง€๋งŒ ์ข€ ๋” ์„ ์–ธํ˜•์ธ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•œ๋‹ค. foo์™€ bar๊ฐ€ ๊ฐ™์€ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ƒ์„ฑ๊ธฐ๋ผ๋ฉด ์ด๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. oneof [foo, bar] ์ด๊ฒƒ์€ foo ๋˜๋Š” bar ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ฐ˜๋ฐ˜ ํ™•๋ฅ ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ™•๋ฅ ์„ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. frequency [ (30, foo), (70, bar) ] oneof๋Š” Gen ์•ก์…˜๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์•„์„œ ์ด ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ฌด์ž‘์œ„๋กœ ์„ ํƒํ•œ๋‹ค. frequency๋Š” ๋น„์Šทํ•œ ์ผ์„ ํ•˜์ง€๋งŒ ๊ฐ ํ•ญ๋ชฉ์„ ์„ ํƒํ•  ํ™•๋ฅ ์€ ๊ทธ์— ์—ฐ๊ด€๋œ ๊ฐ€์ค‘์น˜์— ์˜ํ•ด ๊ฒฐ์ •๋œ๋‹ค. oneof :: [Gen a] -> Gen a frequency :: [(Int, Gen a)] -> Gen a 2 ์ž๋ฃŒ๊ตฌ์กฐ ๊ธฐ์ดˆ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Libraries/Data_structures_primer ํŠธ๋ ˆ์ด๋“œ์˜คํ”„ ์กฐํšŒ: Data.Map ๊ณ„์—ด Map์˜ ๋ณ€ํ˜•๋“ค Data.Sequence๋ฅผ ํ†ตํ•ด ์–‘ ๋์—์„œ ์กฐํšŒํ•˜๊ธฐ ๋ฐฐ์—ด์„ ํ†ตํ•œ raw ์„ฑ๋Šฅ text, bytestring, ๊ทธ๋ฆฌ๊ณ  String์˜ ๋ฌธ์ œ์  ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. ๋ชจ๋“  ํ•˜์Šค ์ผˆ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐฐ์›Œ์•ผ ํ•˜๋Š” ์ผ๋ฐ˜์ ์ด๊ณ  ํŠนํžˆ ์œ ์šฉํ•œ ์˜ˆ์‹œ๋“ค์— ์ง‘์ค‘ํ•  ๊ฒƒ์ด๋‹ค. ํŠธ๋ ˆ์ด๋“œ์˜คํ”„ ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋‹จ์ ์„ ๊ณ„์† ๊ฐ•์กฐํ•˜์ง€๋งŒ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ทธ๋งŒ ์จ์•ผ ํ•œ๋‹ค๋Š” ๋œป์€ ์•„๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ•˜์Šค์ผˆ์˜ ๊ธฐ๋ณธ ์ž๋ฃŒ๊ตฌ์กฐ์ธ ๋ฐ๋Š” ์ด์œ ๊ฐ€ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ๋‹จ์ˆœํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋„˜์–ด ๊ฒŒ์œผ๋ฅด๊ณ  ์ˆœ์ˆ˜ํ•œ ํ•จ์ˆ˜ํ˜• ํ™˜๊ฒฝ์—์„œ ๋†’์€ ๋™๋ ฅ๋น„(power-to-weight ratio)๋ฅผ ๊ฐ€์ง„๋‹ค. ์ง€์—ฐ์„ฑ์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ŠคํŠธ๋ฆผ์œผ๋กœ์จ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ํ•„์š”์— ๋”ฐ๋ผ ์ƒ์„ฑ๋˜๋Š” ์›์†Œ๋“ค์„ ์ˆœ์ฐจ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ฒ˜๋ฆฌ๋Š” map, filter, foldr, takeWhile, zipWith ๊ฐ™์€ ํ•จ์ˆ˜๋“ค์ด ํ”ํ•œ ๋ฐ˜๋ณต์  ์ œ์–ด ๊ตฌ์กฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ๋‹จ์ˆœํ•œ ๋ฐ์ดํ„ฐ ์ €์žฅ๊ณผ ํš๋“๋ณด๋‹ค๋Š” ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ ๋ฐ˜๋ณต์˜ ์ œ์–ด ๊ฐ™์€ ํŒจํ„ด์— ๋” ์ž˜ ๋งž๋Š”๋‹ค. ๋ฌผ๋ก  ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๊ต์ฒดํ•˜๋Š” ๊ฒƒ์—๋Š” ํŠธ๋ ˆ์ด๋“œ์˜คํ”„๊ฐ€ ์ˆ˜๋ฐ˜๋œ๋‹ค. ๋ชจ๋“  ์ž๋ฃŒ๊ตฌ์กฐ์—๋Š” ๋‚˜๋ฆ„์˜ ์žฅ์ ๊ณผ ๋‹จ์ ์ด ์žˆ์œผ๋ฉฐ ์˜ฌ๋ฐ”๋ฅธ ์„ ํƒ์€ ๋‹น๋ฉดํ•ด ์žˆ๋Š” ๋ฌธ์ œ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค. ์กฐํšŒ: Data.Map ๊ณ„์—ด ํ”ํ•œ ๋ฌธ์ œ์ธ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์กฐํšŒ๋ฅผ ์ฒซ ๋ฒˆ์งธ๋กœ ์ƒ๊ฐํ•ด ๋ณด์ž. ์—ฐ๊ด€๋œ ํ‚ค์™€ ๊ฐ’์˜ ๋ชจ์Œ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ์–ด๋–ค ํ‚ค์— ๋Œ€์‘ํ•˜๋Š” ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ๊ทธ ๊ฐ’์„ ํš๋“ํ•˜๊ณ ์ž ํ•œ๋‹ค. ์ด ์—ฐ๊ด€ ๊ด€๊ณ„๋ฅผ ๋‹จ์ˆœํžˆ ์Œ์˜ ๋ฆฌ์ŠคํŠธ [(k, v)]๋กœ ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์•„๋‹ˆ๋‚˜ ๋‹ค๋ฅผ๊นŒ ํ”„๋ ๋ฅ˜๋“œ์—๋Š” lookup :: Eq k => k -> [(k, v)] -> Maybe v๊ฐ€ ์žˆ์ง€๋งŒ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ’์„ ํƒ์ƒ‰ํ•˜๋ ค๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ์Œ์„ ํ›‘์–ด๋ณด๋ฉฐ ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๋˜ ํ‚ค์— ๋„๋‹ฌํ•˜๊ฑฐ๋‚˜ ๋ฆฌ์ŠคํŠธ์˜ ๋์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํ‚ค์— ๋Œ€ํ•œ ํ•ญ๋“ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. ํ‰๋ฒ”ํ•œ ์—ฐ๊ด€ ๋ฆฌ์ŠคํŠธ์—์„œ ์กฐํšŒ๋Š” O(n) ์—ฐ์‚ฐ์œผ๋กœ์„œ ์˜ˆ์ƒ๋˜๋Š” ์ž‘์—… ์ˆ˜ํ–‰ ํšŸ์ˆ˜๊ฐ€ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด์— ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•œ๋‹ค. ์—ฐ๊ด€ ๊ด€๊ณ„๊ฐ€ ๋งŽ์œผ๋ฉด ์ด๊ฒƒ์ด ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑด ์•Œ๊ธฐ ์‰ฝ๋‹ค. ๋” ์•Œ๋งž์€ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๊ฐˆ์•„ํƒ€๋ฉด ์กฐํšŒ๋ฅผ ๋” ์ž˜ํ•  ์ˆ˜ ์žˆ๋‹ค. containers ํŒจํ‚ค์ง€์˜ Data.Map์— ์žˆ๋Š” Map ํƒ€์ž…์€ ๋ฒ”์šฉ์ ์œผ๋กœ ์“ฐ๊ธฐ์— ์ข‹์€ ์„ ํƒ์ด๋‹ค. Data.Map์€ ํ”„๋ ๋ฅ˜๋“œ ํ•จ์ˆ˜๋“ค๊ณผ ์ด๋ฆ„ ์ถฉ๋Œ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๋ณดํ†ต์€ ํ•œ์ •๋˜์–ด ์ž„ํฌํŠธ ๋œ๋‹ค. GHCi> import qualified Data.Map as M GHCi> :t M.empty M.empty :: M.Map k a Map์—์„œ ํ‚ค์™€ ๊ฐ’์€ (๊ท ํ˜• ์žกํžŒ ์ด์ง„) ํŠธ๋ฆฌ ์•ˆ์— ๋ฐฐ์—ด๋œ๋‹ค. ์ด ํŠธ๋ฆฌ ํ˜•ํƒœ์—์„œ ํ‚ค ํƒ์ƒ‰์€ ๋‹จ์ˆœํžˆ ํŠธ๋ฆฌ์˜ ํŠน์ • ๋ธŒ๋žœ์น˜๋ฅผ ๋”ฐ๋ผ ๋‚ด๋ ค๊ฐ€๋Š” ์ผ์ด์ง€๋งŒ ์ „์ ์œผ๋กœ ๋ฐฐํ›„์—์„œ ๋ฒŒ์–ด์ง€๋Š” ์ผ์ด๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Map์€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฑฐ์น˜๋Š” ์ถ”์ƒ ํƒ€์ž…์œผ๋กœ์„œ ์‚ฌ์šฉ๋˜๋ฉฐ ๊ทธ ๋’ค์˜ ํŠธ๋ฆฌ ๊ตฌํ˜„์€ ์–ธ๊ธ‰๋˜์ง€ ์•Š๋Š”๋‹ค. ํŠนํžˆ ์ƒ์„ฑ์ž๊ฐ€ ์ต์ŠคํฌํŠธ๋˜์ง€ ์•Š๋Š”๋ฐ, ์ƒˆ Map์„ ์ƒ์„ฑํ•˜๋ ค๋ฉด empty ๋งต์— ์—ฐ๊ด€ ๊ด€๊ณ„๋ฅผ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜ fromList๋ฅผ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค. GHCi> let foo = M.fromList [(1, "Robert"), (5, "Ian"), (6, "Bruce")] GHCi> :t foo foo :: M.Map Integer [Char] Data.Map ์ธํ„ฐํŽ˜์ด์Šค๋Š” O(log n) ํƒ์ƒ‰์„ ๋ณด์žฅํ•œ๋‹ค. GHCi> :t M.lookup M.lookup :: Ord k => k -> M.Map k a -> Maybe a GHCi> M.lookup 5 foo Just "Ian" GHCi> M.lookup 7 foo Nothing ๊ทธ ์™ธ์—๋„ ํ•ฉ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ, ์›์†Œ ์‚ญ์ œ ๋“ฑ ์œ ์šฉํ•œ ๋ช…๋ ๋“ค์„ ์ง€์›ํ•œ๋‹ค. Functor ๊ฐ™์€ ์ค‘์š”ํ•œ ํƒ€์ž… ํด๋ž˜์Šค๋“ค์— ๋Œ€ํ•œ ์ธ์Šคํ„ด์Šค๋„ ์ง€์›๋œ๋‹ค. GHCi> M.size $ M.union foo $ M.fromList [(11, "Andrew"), (17, "Mike")] GHCi> fmap reverse foo fromList [(1, "treboR"),(5, "naI"),(6, "ecurB")] Map์˜ ๋ณ€ํ˜•๋“ค ๋งต์ด๋‚˜ ๋งต๊ณผ ์œ ์‚ฌํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ง€์›ํ•˜๋Š” ๋‹ค๋ฅธ ๋ชจ๋“ˆ์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค. containers์˜ Data.IntMap์€ Int ํ‚ค์— ํ•œ์ •๋œ ๋” ํšจ์œจ์ ์ธ ๋งต ๊ตฌํ˜„์„ ์ œ๊ณตํ•œ๋‹ค. ์—ญ์‹œ containers์— ์žˆ๋Š” Data.Set์€ ์ง‘ํ•ฉ ๊ตฌํ˜„์„ ์ œ๊ณตํ•œ๋‹ค. ์ง‘ํ•ฉ์€ ๊ด€์‹ฌ ์žˆ๋Š” ์—ฐ์‚ฐ์ด ์–ด๋–ค ๊ฐ’์ด ์ปฌ๋ ‰์…˜์— ๋“ค์–ด์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๋Š” ๊ฒƒ์ผ ๋ฟ, ์ฃผ์–ด์ง„ ํ‚ค๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ํš๋“ํ•  ํ•„์š”๊ฐ€ ์—†์„ ๋•Œ ์ ํ•ฉํ•˜๋‹ค. ํ‚ค๋งŒ ์‹ ๊ฒฝ ์“ฐ๋ฉด ๋œ๋‹ค๋Š” ์ ์—์„œ ์ง‘ํ•ฉ์€ ๋งต๊ณผ ์œ ์‚ฌํ•˜๋ฉฐ ์„ฑ๋Šฅ๊ณผ ๊ตฌํ˜„์— ๊ด€ํ•œ ๋งŽ์€ ์‚ฌํ•ญ์ด ๋‘˜ ๋‹ค ๋˜‘๊ฐ™์ด ์ ์šฉ๋œ๋‹ค. unordered-containers ํŒจํ‚ค์ง€๋Š” ํ•ด์‹œ ๋งต ๋ฐ ์ง‘ํ•ฉ์„ ์ œ๊ณตํ•œ๋‹ค. ์ด๊ฒƒ๋“ค์€ ํ‚ค๋ฅผ Int ํƒ€์ž…์œผ๋กœ ํ•œ์ •ํ•˜์ง€ ์•Š์œผ๋ฉด์„œ๋„ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์ด๋ฃจ๋Š”๋ฐ(๊ฐ€๋ น ์กฐํšŒ๋Š” ๊ฑฐ์˜ ์ƒ์ˆ˜ ์‹œ๊ฐ„์ด๋‹ค) ๊ทธ ๋Œ€์‹  ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋” ์ œํ•œ๋˜๊ณ  container์˜ ํŠธ๋ฆฌ ๊ธฐ๋ฐ˜ ๋งต๊ณผ ๋‹ฌ๋ฆฌ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. Data.Sequence๋ฅผ ํ†ตํ•ด ์–‘ ๋์—์„œ ์กฐํšŒํ•˜๊ธฐ ๋ฆฌ์ŠคํŠธ์˜ ํŠน์„ฑ์€ ๋น„๋Œ€์นญ์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ํ—ค๋“œ์—์„œ (:)๋ฅผ ํ†ตํ•ด ๊ตฌ์ถ• ๋ฐ ๋ถ„ํ•ด๋ฅผ ํ•œ๋‹ค๋Š” ์ ์—์„œ ์—ฐ์‚ฐ์€ ํ—ค๋“œ์—์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฒŒ ํ…Œ์ผ์—์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํšจ์œจ์ ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด (:)๋ฅผ ํ†ตํ•ด ์›์†Œ๋ฅผ ์•ž์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ์ƒ์ˆ˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ์ง€๋งŒ xs x -> xs ++ [x]๋ฅผ ํ†ตํ•ด ๋์— ์›์†Œ ํ•˜๋‚˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ xs์˜ ๊ธธ์ด์— ๋น„๋ก€ํ•˜๋Š” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. ์ด๋Š” ๋ง๋ถ™์—ฌ์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์ถ•ํ•˜๋ ค๋ฉด ์›์†Œ ๊ฐœ์ˆ˜์˜ ์ œ๊ณฑ์— ๋น„๋ก€ํ•˜๋Š” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค๋Š” ๋œป์ด๋‹ค. ์•„์ฃผ ์ข‹์ง€ ์•Š๋‹ค. ์ค‘๊ฐ„ ๋˜๋Š” ๋์—์„œ ๋งŽ์€ ์—ฐ์‚ฐ์„ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์‹œํ€€์Šค๋Š” ๋ฆฌ์ŠคํŠธ์™€ ๋น„์Šทํ•˜๋ฉด์„œ๋„ ํ›Œ๋ฅญํ•œ ๋Œ€์•ˆ์ด๋‹ค. ์‹œํ€€์Šค๋Š” Data.Sequence ๋ชจ๋“ˆ์— ๋“ค์–ด์œผ๋ฉฐ ์ด ๋ชจ๋“ˆ ์—ญ์‹œ containers์˜ ์ผ๋ถ€๋‹ค. ์‹œํ€€์Šค์™€ ๋ฆฌ์ŠคํŠธ๋Š” ๊ทธ๋‹ค์ง€ ๋น„์Šทํ•˜์ง€ ์•Š์ง€๋งŒ ์นœ์ˆ™ํ•œ ๋ฆฌ์ŠคํŠธ ํ•จ์ˆ˜๋“ค์ด Data.Sequence์—๋„ ๋งŽ์ด ๋“ฑ์žฅํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ๊ฒŒ์œผ๋ฅด๊ณ  ๋ฌดํ•œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ฉด ์‹œํ€€์Šค๋Š” ์œ ํ•œํ•˜๊ณ  ์—„๊ฒฉํ•˜๋‹ค. ์‹œํ€€์Šค๋ฅผ ์œ ์šฉํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ํŠธ๋ ˆ์ด๋“œ์˜คํ”„๋Š” ๋ฆฌ์ŠคํŠธ์— ๋น„ํ•ด ์•ฝ๊ฐ„์˜ ์˜ค๋ฒ„ํ—ค๋“œ ๋น„์šฉ์„ ์น˜๋ฅด๋ฉด ๋ฆฌ์ŠคํŠธ์—์„œ ๋ฌธ์ œ ์žˆ๋˜ ๋งŽ์€ ์—ฐ์‚ฐ์ด ๋” ๋‚˜์•„์ง„๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ํŠนํžˆ ์•ž๊ณผ ๋’ค์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ ๋ชจ๋‘ ์ƒ์ˆ˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋ฉฐ, ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ๋„ ์ƒ์ˆ˜ ์‹œ๊ฐ„, ์—ฐ๊ฒฐ๊ณผ ์ž„์˜ ์ ‘๊ทผ์€ ๋กœ๊ทธ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. ์ด ๋ชจ๋“  ๊ฒƒ์€ ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์ด๋ค„์ง„๋‹ค. GHCi> import qualified Data.Sequence as S GHCi> import Data.Sequence((<|), (|>), (><), ViewL(..), ViewR(..)) GHCi> let foo = S.fromList [1, 3, 5, 2, 9] GHCi> :t foo foo :: S.Seq Integer (<|)๋Š” prepend, (|>)๋Š” append, (><)๋Š” concatenate๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. GHCi> 0 <| foo fromList [0,1,3,5,2,9] GHCi> foo |> 18 fromList [1,3,5,2,9,18] GHCi> foo >< foo fromList [1,3,5,2,9,1,3,5,2,9] ์–‘ ๋๋‹จ์— ๋Œ€ํ•ด ํŒจํ„ด ๋งค์นญ์„ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด viewl ๋˜๋Š” viewr์„ ์จ์„œ ์›ํ•˜๋Š” ๋ทฐ๋ฅผ ์–ป์€ ๋‹ค์Œ EmptyL ๋ฐ (:<)๋ฅผ ์ด์šฉํ•˜๊ฑฐ๋‚˜ EmptyR ๋ฐ (:>)๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งค์นญํ•œ๋‹ค. GHCi> S.viewl foo 1 :< fromList [3,5,2,9] GHCi> S.viewr foo fromList [1,3,5,2] :> 9 GHCi> let xs :> x = S.viewr foo GHCi> xs fromList [1,3,5,2] GHCi> x ๋ฐฐ์—ด์„ ํ†ตํ•œ raw ์„ฑ๋Šฅ ๊ฑฐ๋Œ€ ๊ทœ๋ชจ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋Š” ์„ฑ๋Šฅ ์š”๊ตฌ์‚ฌํ•ญ์ด ์ƒ๋‹นํžˆ ๋นก์„ธ์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์ง€์—ฐ์„ฑ๊ณผ ์ŠคํŠธ๋ฆฌ๋ฐ์€ ๊ณ ๋ ค ๋Œ€์ƒ์กฐ์ฐจ ์•„๋‹ˆ๋‹ค. ํ•˜์Šค์ผˆ์€ C์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์ง„์งœ ๋ฐฐ์—ด์„ ์ง€์›ํ•œ๋‹ค. ๋ฐฐ์—ด์€ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์—ฐ์†์ ์ด๋ฉฐ(compact) ์ž„์˜ ์ ‘๊ทผ์„ ์ƒ์ˆ˜ ์‹œ๊ฐ„์— ํ•ด๋‚ด๊ณ  ๊ธฐํƒ€ ๋งŽ์€ ์—ฐ์‚ฐ์„ ๊ต‰์žฅํžˆ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•œ๋‹ค. (์ฃผ๋œ ์˜ˆ์™ธ๋Š” ๋ถˆ๋ณ€ ๋ฐฐ์—ด์˜ ์—ฐ๊ฒฐ์ฒ˜๋Ÿผ ๋ฐฐ์—ด์˜ ๋ณต์‚ฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ) ๊ทธ ๋Œ€์‹  ๋ฐฐ์—ด๊ณผ ์šฐ๋ฆฌ๊ฐ€ ํ‰์†Œ์— ๋‹ค๋ฃจ๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์˜ ๊ทผ๋ณธ์ ์ธ ์ฐจ์ด์—์„œ ๋‚˜์˜ค๋Š” ๋ถˆํŽธํ•จ์„ ๊ฐ์ˆ˜ํ•ด์•ผ ํ•œ๋‹ค. ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊ทธ ์‚ฌ์šฉ๋ฒ•์ด ํ‰๋ฒ”ํ•œ ํ•˜์Šค ์ผˆ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ๊ทธ๋‹ค์ง€ ๋‹ค๋ฅด์ง€ ์•Š์€ ๋ฐฐ์—ด๋ถ€ํ„ฐ C ์Šคํƒ€์ผ์˜ ๊ฐ€๋ณ€ ์›์‹œ ๊ฐ’ ๋ฐฐ์—ด๊นŒ์ง€ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ๋ฐฐ์—ด์„ ์ œ๊ณตํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ๋ฐฐ์—ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ 3๊ฐœ๋ฅผ ์†Œ๊ฐœํ•˜๊ฒ ๋‹ค. vector๋Š” ๋ฐฐ์—ด์— ์ž…๋ฌธํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์ด ์ œ๊ณตํ•˜๋Š” ํŠนํ™”๋œ ์š”๊ตฌ์‚ฌํ•ญ์ด ํ•„์š” ์—†์„ ๋•Œ ๊ธฐ๋ณธ์œผ๋กœ ์„ ํƒํ•˜๊ธฐ ์ข‹๋‹ค. vector๋Š” containers ๊ฐ™์€ ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค์™€ ์œ ์‚ฌํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€์ง€๋Š” 1์ฐจ์› ๋ฐฐ์—ด์„ ์ œ๊ณตํ•œ๋‹ค. array์˜ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ข€ ๋” ์œ„์••๊ฐ์ด ๋“ ๋‹ค. ๊ธฐ๋Šฅ์€ ๋‹ค์ฐจ์› ๋ฐฐ์—ด๊ณผ ์ปค์Šคํ…€ ์ธ๋ฑ์‹ฑ์„ ์ง€์›ํ•œ๋‹ค. ์ค‘์š”ํ•œ ์ ์€ array๊ฐ€ ์–ธ์–ด ํ‘œ์ค€์˜ ์ผ๋ถ€์ด๋ฉฐ GHC์— ํฌํ•จ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” ์ถ”๊ฐ€์ ์ธ ์˜์กด์„ฑ์„ ๋‹ฌ๊ฐ€์›Œํ•˜์ง€ ์•Š๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž‘์„ฑ์ž๋“ค์—๊ฒŒ ์œ ์šฉํ•˜๋‹ค. ๋ณ„๊ฐœ์˜ ์žฅ์— ์žˆ๋Š” ํ‘œ์ค€ ๋ฐฐ์—ด์˜ ๊ฐœ์š”๋ฅผ ํ†ตํ•ด ๋ฐฐ์—ด๊ณผ ๊ด€๋ จ๋œ ์šฉ์–ด์— ์ž…๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค. repa๋Š” ์ตœ์‹  ๋‹ค์ฐจ์› ๋ณ‘๋ ฌํ™” ๊ฐ€๋Šฅ ๋ฐฐ์—ด์„ ์ œ๊ณตํ•˜๋Š” ์ •๊ตํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ๊ฐ™์€ ์ž‘์—…์— ์ ํ•ฉํ•˜๋‹ค. text, bytestring, ๊ทธ๋ฆฌ๊ณ  String์˜ ๋ฌธ์ œ์  base ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ํ›‘์–ด๋ณด๋ฉด ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ฐ์ดํ„ฐ์˜ ์ž…์ถœ๋ ฅ์— String์ด ์„ ํ˜ธ๋˜๋Š” ์ˆ˜๋‹จ์ด๋ผ๋Š” ์ธ์ƒ์„ ๋ฐ›๋Š”๋‹ค. ํ•˜์ง€๋งŒ String์—๋Š” ๊ทธ๋Ÿฐ ์—ญํ• ์„ ๋งก๊ธฐ์—๋Š” ์ข‹์ง€ ์•Š์€ ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ๊ฐ€์žฅ ๋ช…๋ฐฑํ•œ ๋ฌธ์ œ๋Š” ์„ฑ๋Šฅ์ด๋‹ค. String์€ Char์˜ ๋ฆฌ์ŠคํŠธ์ผ ๋ฟ์ด๋ฉฐ ์ ๋‹นํžˆ ํฐ ๊ทœ๋ชจ์˜ ํ…์ŠคํŠธ๋‚˜ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ์กฐ์ฐจ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ๋ฒ”์šฉ์„ฑ์ด ์ฃผ๋Š” ์ด์ ๋ณด๋‹ค๋Š” ํŠน๋ณ„ํ™”๋œ ๊ตฌํ˜„์œผ๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ํšจ์œจ์„ฑ์˜ ์†์‹ค์ด ํ›จ์”ฌ ํฌ๋‹ค. ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ Char ๊ธฐ๋ฐ˜ ํ‘œํ˜„์ด ์˜๋ฏธ๋ฅผ ์žƒ๋Š”๋ฐ, ์šฐ๋ฆฌ๋Š” ๋ง ๊ทธ๋Œ€๋กœ ๋‚  ๊ฒƒ์˜ ๋ฐ”์ดํŠธ๋ฅผ ๋‹ค๋ฃจ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•˜์Šค ์ผˆ Char๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์ธ๋ฐ base ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์—๋Š” ๋‹ค๋ฅธ ์ธ์ฝ”๋”ฉ์ด๋‚˜ ๊ตญ์ œํ™”์— ๋Œ€ํ•œ ์ง€์›์ด ๋‹ค์†Œ ๋ถ€์กฑํ•˜๋‹ค. String์˜ ์ด๋Ÿฐ ๋‹จ์ ๋“ค์€ text์™€ bytestring ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋‘˜์€ ์‚ฌ์‹ค์ƒ ํ‘œ์ค€์ด๋‹ค. ๋ง‰๋Œ€ํ•œ ์–‘์˜ ๋ฐ์ดํ„ฐ ์ž…์ถœ๋ ฅ์„ ๋‹ค๋ฃจ๋Š” ํ˜„๋Œ€์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„ ์ด ๋‘˜์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋‘˜์˜ ์“ฐ์ž„์ƒˆ๋Š” ํ™•์—ฐํžˆ ๋‚˜๋‰œ๋‹ค. text๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ํ…์ŠคํŠธ์˜ ํšจ์œจ์ ์ธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. ์ธ์ฝ”๋”ฉ ๊ฐ„ ๋ณ€ํ™˜์„ ์ง€์›ํ•˜๋ฉฐ ์œ ๋‹ˆ์ฝ”๋“œ ์„œ๋น„์Šค๋ฅผ ์œ„ํ•œ ์ข…ํ•ฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ text-icu์™€ ํ•จ๊ป˜ ์“ฐ์ธ๋‹ค. bytestring์€ ๋ชจ๋“  ํ˜•ํƒœ์˜ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ์˜ ํšจ์œจ์ ์ธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. ๋„คํŠธ์›Œํฌ ํŒจํ‚ท, raw ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ, ์ง๋ ฌํ™”(binary์™€ cereal ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด) ๋“ฑ์ด ์žˆ๋‹ค. Text, ByteString ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ•ต์‹ฌ ํƒ€์ž…๋“ค์€ ๊ฐ๊ฐ Char์™€ Word8 (์ฆ‰ raw byte)์˜ ํŠนํ™”๋œ ๋‹จํ˜•์„ฑ ์ปจํ…Œ์ด๋„ˆ๋กœ์„œ ๊ตฌํ˜„๋œ๋‹ค. ๋‚ด๋ถ€ ๊ตฌํ˜„์€ ๋ฐฐ์—ด์— ๊ธฐ๋ฐ˜ํ•˜๋ฉฐ ๋งค์šฐ ์••์ถ•์ ์ด๋‹ค. ๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ฝค๋‚˜ ์ง๊ด€์ ์ด๋‹ค. ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ๋‘˜ ๋‹ค ์—„๊ฒฉํ•œ ํƒ€์ž…๊ณผ ๊ฒŒ์œผ๋ฅธ ํƒ€์ž… ๋ณ€์ข…์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์—„๊ฒฉํ•œ ๋ฒ„์ „์€ ๊ฑฐ๋Œ€ ๋ณผ๋ฅจ๋“ค ๋ช‡ ๊ฐœ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ์•Œ๋งž๊ณ  ๊ฒŒ์œผ๋ฅธ ๋ฒ„์ „์€ ๋ฉ์–ด๋ฆฌ ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌ๋˜๋Š”, ๋”ฐ๋ผ์„œ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋ชจ ๊ฑฑ์ • ์—†์ด ๋‹จ์ผ ๋ฐ์ดํ„ฐ์˜ ๋งŽ์€ ์กฐ๊ฐ์˜ ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ ์ฒ˜๋ฆฌ์— ์ ํ•ฉํ•˜๋‹ค. String์˜ ๋Œ€์ฒด์žฌ๋“ค์„ ๋‹ค๋ฃฐ ๋•Œ ์•Œ์•„๋‘๋ฉด ์ข‹์€ ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์ด ์žˆ๋Š”๋ฐ, OverloadedStrings GHC ํ™•์žฅ์€ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ Text ๋˜๋Š” ByteString์œผ๋กœ ์•Œ์•„์„œ ํƒ€์ž…์— ๋งž์ถฐ ๋ณ€ํ™˜ํ•ด ์ค€๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ํŠนํžˆ Text์— ์œ ์šฉํ•˜๋‹ค. 1 ๋ฐฐ์—ด ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Libraries/Arrays ์›๋ฌธ์€ GHC 6.x ์‹œ์ ˆ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ณธ๋ฌธ ์‹œ์ ์— ์ตœ์‹  ๋ฆด๋ฆฌ์Šค๋Š” 2021-02-04 ๋ฐฐํฌ๋œ GHC 9.0.1์ž…๋‹ˆ๋‹ค. ๋” ์ด์ƒ ๋งž์ง€ ์•Š๋Š” ์„ค๋ช…์ด๋‚˜ ๊นจ์ง„ ๋งํฌ๊ฐ€ ์žˆ์–ด์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„๋“ค์€ ์ ์ ˆํžˆ ์ˆ˜์ •ํ•˜๊ณ  ์—ญ์ฃผ๋ฅผ ๋‹ฌ์•„๋†“์•˜์œผ๋‹ˆ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”. ์š”์•ฝํ‘œ ๋ถˆ๋ณ€ ๋ฐฐ์—ด ๊ฐ€๋ณ€ IO ๋ฐฐ์—ด ST ๋ชจ๋‚˜๋“œ ๋‚ด์˜ ๊ฐ€๋ณ€ ๋ฐฐ์—ด freeze์™€ thaw DiffArray Unboxed arrays StorableArray The Haskell Array Preprocessor (STPP) ArrayRef ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ unsafe operations and running over array elements GHC์— ํ•œ์ •๋œ ํ™”์ œ๋“ค ํ‰ํ–‰ ๋ฐฐ์—ด (GHC.PArr ๋ชจ๋“ˆ) Welcome to the machine Array#, MutableArray#, ByteArray#, MutableByteArray#, pinned and moveable byte arrays Mutable arrays and GC ํ•˜์Šค ์ผˆ 98์€ ๋ฐฐ์—ด ์ƒ์„ฑ์ž ํƒ€์ž…์„ ํ•˜๋‚˜๋งŒ ์ง€์›ํ•˜๋ฉฐ ๊ทธ ์ด๋ฆ„์€ Array์ด๋‹ค. ์ด๊ฒƒ์€ ๋ถˆ๋ณ€ boxed ๋ฐฐ์—ด์ธ๋ฐ "๋ถˆ๋ณ€"์€ ์ด ๋ฐฐ์—ด์ด ๋‹ค๋ฅธ ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ๋“ค์ฒ˜๋Ÿผ ์ƒ์„ฑ ์‹œ๊ฐ„์— ๊ทธ ๋‚ด์šฉ์ด ๊ณ ์ •๋œ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ด ๋ฐฐ์—ด์€ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ์งˆ์˜๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. "์ˆ˜์ •" ์—ฐ์‚ฐ์€ ์žˆ์ง€๋งŒ ์›๋ž˜ ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ Array๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜• ์ฝ”๋“œ์—์„œ ๋ฆฌ์ŠคํŠธ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. "boxed"๋Š” ๋ฐฐ์—ด ์›์†Œ๊ฐ€ ์ผ๋ฐ˜์ ์ธ ํ•˜์Šค ์ผˆ (๊ฒŒ์œผ๋ฅธ) ๊ฐ’์ด๋ฉฐ ํ•„์š”์— ๋”ฐ๋ผ ํ‰๊ฐ€๋˜๊ณ  ์‹ฌ์ง€์–ด bottom (undefined) ๊ฐ’์ผ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ด๋Ÿฐ ๋ฐฐ์—ด์˜ ์‚ฌ์šฉ๋ฒ•์€ https://www.haskell.org/tutorial/arrays.html์—์„œ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด ํŽ˜์ด์ง€๋ฅผ ๊ณ„์† ์ฝ๊ธฐ ์ „์— ์ฝ์–ด๋ณผ ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. ์š”์ฆ˜์€ ์ฃผ์š” ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ์ธ GHC์™€ Hugs๊ฐ€ Hierarchical Libraries์™€ ํ•จ๊ป˜ ๋ฐฐํฌ๋˜๋ฉฐ ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ ํ•˜์Šค ์ผˆ 98์˜ ๋ฐฐ์—ด๊ณผ ํ•˜ ํœ˜ ํ˜ธํ™˜๋˜์ง€๋งŒ ๊ธฐ๋Šฅ์ด ๋” ๋งŽ์œผ๋ฉฐ ์ƒˆ๋กœ ๊ตฌํ˜„ํ•œ ๋ฐฐ์—ด์„ ํฌํ•จํ•œ๋‹ค. ์ผ๋‹จ ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์ด 9๊ฐ€์ง€ ๋ฐฐ์—ด ์ƒ์„ฑ์ž๋ฅผ ์ง€์›ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋‘์ž. โ€  (Array, UArray, IOArray, IOUArray, STArray, STUArray, DiffArray, DiffUArray, StorableArray) ํ•˜์Šค ์ผˆ ์ž…๋ฌธ์ž์—๊ฒŒ ์ด ๋ฐฐ์—ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์€ ํ˜ผ๋ˆ์˜ ๊ทผ์›์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‚ฌ์‹ค์€ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๋‹ค. ๊ฐ๊ฐ ๋‘ ์ธํ„ฐํŽ˜์ด์Šค ์ค‘ ํ•˜๋‚˜๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„๋„ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. โ€  ์—ญ์ฃผ: GHC 9.0.1 ๊ธฐ์ค€ DiffArray์™€ DiffUArray๋Š” ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์š”์•ฝํ‘œ Immutable instance IArray a e IO monad instance MArray a e IO ST monad instance MArray a e ST ํ‘œ์ค€ Array DiffArray IOArray STArray Unboxed UArray DiffUArray IOUArray StorableArray STUArray ๋ถˆ๋ณ€ ๋ฐฐ์—ด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํƒ€์ž… ํด๋ž˜์Šค IArray์— ์˜ํ•ด ์ •์˜๋œ๋‹ค. ("๋ถˆ๋ณ€ ๋ฐฐ์—ด"์ด๋ผ๋Š” ๋œป์ด๋ฉฐ Data.Array.IArray ๋ชจ๋“ˆ์— ์ •์˜๋˜์–ด ์žˆ๋‹ค) IArray๋Š” ํ•˜์Šค ์ผˆ 98์˜ Array์™€ ๋™์ผํ•œ ์—ฐ์‚ฐ๋“ค์„ ์ •์˜ํ•œ๋‹ค. ๋‹ค์Œ์€ IArray๋ฅผ ์‚ฌ์šฉํ•ด (37,64)๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋‹ค. import Data.Array buildPair :: (Int, Int) buildPair = let arr = listArray (1,10) (repeat 37) :: Array Int Int arr' = arr // [(1, 64)] in (arr ! 1, arr' ! 1) main = print buildPair ์—ฌ๊ธฐ์„œ ํฐ ์ฐจ์ด์ ์€ IArray๊ฐ€ ์ด์ œ ํƒ€์ž… ํด๋ž˜์Šค์ด๋ฉฐ ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฐ์—ด ํƒ€์ž… ์ƒ์„ฑ์ž๊ฐ€ Array, UArray, DiffArray, DiffUArray ์ด๋ ‡๊ฒŒ 4๊ฐœ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋‚˜์ค‘์— ์ด๊ฒƒ๋“ค์˜ ์ฐจ์ด์ ๊ณผ ์˜ˆ์ „ ๋ฐฐ์—ด ๋Œ€์‹  ์“ฐ๊ธฐ ์ข‹์€ ์ƒํ™ฉ์„ ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  Array ํƒ€์ž… ์ƒ์„ฑ์ž๋ฅผ ์ƒˆ ๋ฐฐ์—ด ํƒ€์ž…๋“ค๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Data.Array๊ฐ€ ์•„๋‹ˆ๋ผ Data.Array.IArray ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜๋ผ. ๊ฐ€๋ณ€ IO ๋ฐฐ์—ด ๋‘ ๋ฒˆ์งธ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํƒ€์ž… ํด๋ž˜์Šค MArray์— ์˜ํ•ด ์ •์˜๋œ๋‹ค. ("๊ฐ€๋ณ€ ๋ฐฐ์—ด"์„ ๋œปํ•˜๋ฉฐ Data.Array.MArray ๋ชจ๋“ˆ์— ์ •์˜๋˜์–ด ์žˆ๋‹ค) MArray๋Š” ๊ทธ ์ž๋ฆฌ์—์„œ ๋ฐฐ์—ด ์›์†Œ๋ฅผ ๊ฐฑ์‹ ํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น๋“ค์„ ํฌํ•จํ•œ๋‹ค. ๊ฐ€๋ณ€ ๋ฐฐ์—ด์€ IORef์™€ ๋งค์šฐ ์œ ์‚ฌํ•˜์ง€๋งŒ ๊ฐ’์„ ์—ฌ๋Ÿฌ ๊ฐœ ํฌํ•จํ•œ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. ๊ฐ€๋ณ€ ๋ฐฐ์—ด์˜ ํƒ€์ž… ์ƒ์„ฑ์ž๋กœ๋Š” IOArray์™€ IOUArray๊ฐ€ ์žˆ์œผ๋ฉฐ (Data.Array.IO) ์ด ๋ฐฐ์—ด๋“ค์˜ ์ƒ์„ฑ, ๊ฐฑ์‹ , ์งˆ์˜ ๋ช…๋ น์€ ๋ชจ๋‘ IO ๋ชจ๋‚˜๋“œ์— ์†ํ•œ๋‹ค. import Data.Array.IO main = do arr <- newArray (1,10) 37 :: IO (IOArray Int Int) a <- readArray arr 1 writeArray arr 1 64 b <- readArray arr 1 print (a, b) ์ด ํ”„๋กœ๊ทธ๋žจ์€ ์›์†Œ 10๊ฐœ์˜ ์ดˆ๊นƒ๊ฐ’์ด ๋ชจ๋‘ 37์ธ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด์˜ ์ฒซ ์›์†Œ๋ฅผ ์ฝ๋Š”๋‹ค. ๊ทธ๋‹ค์Œ ๋ฐฐ์—ด์˜ ์ฒซ ์›์†Œ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ๋‹ค์‹œ ์ฝ๋Š”๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„์˜ ํƒ€์ž… ์„ ์–ธ์€ ํ”„๋กœ๊ทธ๋žจ์ด arr์˜ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์„ ๊ฒฐ์ •ํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•œ ๋ฌธ๋งฅ์„ ์ œ๊ณตํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•„์ˆ˜์ ์ด๋‹ค. ST ๋ชจ๋‚˜๋“œ ๋‚ด์˜ ๊ฐ€๋ณ€ ๋ฐฐ์—ด IORef์˜ ๋” ์ผ๋ฐ˜ํ™”๋œ ๋ฒ„์ „์ธ STRef๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ IOArray๋„ ๋” ์ผ๋ฐ˜ํ™”๋œ ๋ฒ„์ „์ธ STArray๊ฐ€ ์žˆ๋‹ค. (๋น„์Šทํ•˜๊ฒŒ IOUArray์— ๋Œ€ํ•œ STUArray๊ฐ€ ์žˆ์œผ๋ฉฐ ๋‘˜ ๋‹ค Data.Array.ST์— ์žˆ๋‹ค) ์ด ๋ฐฐ์—ด ํƒ€์ž…๋“ค์€ ST ๋ชจ๋‚˜๋“œ ๋‚ด์—์„œ ๊ฐ€๋ณ€ ๋ฐฐ์—ด์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. import Control.Monad.ST import Data.Array.ST buildPair = do arr <- newArray (1,10) 37 :: ST s (STArray s Int Int) a <- readArray arr 1 writeArray arr 1 64 b <- readArray arr 1 return (a, b) main = print $ runST buildPair ๋ฏฟ๊ธฐ์ง€ ์•Š๊ฒ ์ง€๋งŒ ์ด์ œ ์—ฌ๋Ÿฌ๋ถ„์€ ๋ชจ๋“  ๋ฐฐ์—ด ํƒ€์ž…์˜ ์‚ฌ์šฉ๋ฒ•์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์†๋„ ๋ฌธ์ œ์— ๊ด€์‹ฌ ์žˆ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ๊ทธ์ € ์ ์ ˆํ•œ ๊ณณ์— Array, IOArray, STArray๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. ์ด๋‹ค์Œ ์ฃผ์ œ๋“ค์€ ๋Œ€๋ถ€๋ถ„ ์ „์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ๋” ๋น ๋ฅด๊ฒŒ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์ ์ ˆํ•œ ๋ฐฐ์—ด ํƒ€์ž…์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์— ๊ด€ํ•œ ๋‚ด์šฉ์ด๋‹ค. freeze์™€ thaw ํ•˜์Šค์ผˆ์€ freeze์™€ thaw ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ถˆ๋ณ€ ๋ฐฐ์—ด๊ณผ ๊ฐ€๋ณ€ ๋ฐฐ์—ด ์‚ฌ์ด์˜ ๋ณ€ํ™˜์„ ํ—ˆ์šฉํ•œ๋‹ค. freeze :: (Ix i, MArray a e m, IArray b e) => a i e -> m (b i e) thaw :: (Ix i, IArray a e, MArray b e m) => a i e -> m (b i e) ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ์ฝ”๋“œ๋Š” Array๋ฅผ STArray๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ๊ทธ STArray๋ฅผ ๊ฐฑ์‹ ํ•˜๊ณ , ๋‹ค์‹œ Array๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. import Data.Array import Control.Monad.ST import Data.Array.ST buildPair :: (Int, Int) buildPair = let arr = listArray (1,10) (repeat 37) :: Array Int Int arr' = modifyAsST arr in (arr ! 1, arr' ! 1) modifyAsST :: Array Int Int -> Array Int Int modifyAsST arr = runST $ do starr <- thaw arr compute starr newarr <- freeze starr return newarr compute :: STArray s Int Int -> ST s () compute arr = do writeArray arr 1 64 main = print buildPair freeze์™€ thaw๋Š” ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋ณต์‚ฌํ•œ๋‹ค. freeze์™€ thaw ์ „ํ›„๋กœ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ผ์ข…์˜ ์ ‘๊ทผ ์ œํ•œ๋„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด unsafe operations and running over array elements ์ ˆ์„ ๋ณผ ๊ฒƒ. DiffArray ์•ž์„œ ๋งํ–ˆ๋“ฏ์ด ๋ถˆ๋ณ€ ๋ฐฐ์—ด(IArray)์— ๋Œ€ํ•œ ๊ฐฑ์‹ ์€ ๊ทธ ๋ฐฐ์—ด์˜ ์ƒˆ ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค์–ด์„œ ๋งค์šฐ ๋น„ํšจ์œจ์ ์ด์ง€๋งŒ ์ˆœ์ˆ˜ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ˆœ์ˆ˜ ์—ฐ์‚ฐ์ด๋‹ค. ๋ฐ˜๋ฉด์— ๊ฐ€๋ณ€ ๋ฐฐ์—ด(MArray)์— ๋Œ€ํ•œ ๊ฐฑ์‹ ์€ ํšจ์œจ์ ์ด์ง€๋งŒ ๋ชจ๋‚˜๋“œ ์ฝ”๋“œ ์•ˆ์—์„œ๋งŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. DiffArray(Data.Array.Diff์— ์ •์˜๋จ)๋Š” ์ด ๋‘˜์˜ ์žฅ์ ๋งŒ์„ ์ทจํ•œ๋‹ค. DiffArray๋Š” IArray ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ง€์›ํ•˜๋ฉด์„œ ์ˆœ์ˆ˜ ํ•จ์ˆ˜ํ˜•์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” MArray์˜ ํšจ์œจ์ ์ธ ๊ฐฑ์‹ ์„ ํ™œ์šฉํ•œ๋‹ค. โ€  โ€  ์—ญ์ฃผ: GHC 9.0.1 ๊ธฐ์ค€ DiffArray๋Š” ๋” ์ด์ƒ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋‹ค. ํ•˜์Šค ์ผˆ ์œ„ํ‚ค์— ๋”ฐ๋ฅด๋ฉด DiffArray๋Š” ์ด๋ก ์ƒ ์„ฑ๋Šฅ์„ ๋ณด์žฅํ•˜์ง€ ์•Š์œผ๋ฉฐ MArray๋ณด๋‹ค 10~100๋ฐฐ ๋Š๋ฆฌ๋‹ค. ์ด ํŠธ๋ฆญ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š” ๊ฑธ๊นŒ? DiffArray๋Š” ์ˆœ์ˆ˜ ์™ธ๋ถ€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€์ง€์ง€๋งŒ IOArray์— ๋Œ€ํ•œ ์ฐธ์กฐ๋กœ์„œ ํ‘œํ˜„๋œ๋‹ค. diff ๋ฐฐ์—ด์— // ์—ฐ์‚ฐ์ž๋ฅผ ์ ์šฉํ•˜๋ฉด ๊ทธ ๋‚ด์šฉ๋ฌผ์€ ์ œ์ž๋ฆฌ์—์„œ ๊ฐฑ์‹ ๋œ๋‹ค. ์ด์ „ ๋ฐฐ์—ด์€ ๋“œ๋Ÿฌ๋‚˜๋Š” ํ–‰๋™์„ ๋ฐ”๊พธ์ง€ ์•Š์œผ๋ฉด์„œ ์ž์‹ ์˜ ํ‘œํ˜„์„ ์กฐ์šฉํžˆ ๋ณ€๊ฒฝํ•œ๋‹ค. ์ฆ‰ ์ƒˆ๋กœ์šด ํ˜„ ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๋งํฌ์™€ ์ด์ „ ๋‚ด์šฉ๋ฌผ์„ ์–ป๊ธฐ ์œ„ํ•ด ์ ์šฉํ•  ์ฐจ์ด๋ฅผ ๋ณด๊ด€ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ diff ๋ฐฐ์—ด์„ ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ ์Šคํƒ€์ผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ์ฆ‰ //๋ฅผ ์ ์šฉํ•œ ํ›„ ์ด์ „ ๋ฒ„์ „์€ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด a! i๋Š” O(1) ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ณ  a // d๋Š” O(n) ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. ์ด์ „ ๋ฒ„์ „์˜ ์›์†Œ์— ์ ‘๊ทผํ•˜๋Š” ์‹œ๊ฐ„์€ ์ ์  ๋Š๋ ค์ง„๋‹ค. ํ˜„์žฌ ๋ฐฐ์—ด์ด ์•„๋‹Œ ๊ฒƒ์„ ๊ฐฑ์‹ ํ•˜๋ฉด ์ƒˆ๋กœ์šด ๋ฌผ๋ฆฌ์  ๋ณต์‚ฌ๋ณธ์ด ๋งŒ๋“ค์–ด์ง„๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ๋‚˜์˜ค๋Š” ๋ฐฐ์—ด์€ ์ด์ „ ๋ฐฐ์—ด๋“ค๊ณผ ์—ฐ๊ฒฐ์ด ๋Š์–ด์ง„๋‹ค. ๋”ฐ๋ผ์„œ ํ˜„์žฌ ๋ฒ„์ „์ž„์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด์„ ์–ป์„ ์ˆ˜ ์žˆ๊ณ  "ํ•ญ๋“ฑ ๊ฐฑ์‹ "์ธ old // []๋ฅผ ์ˆ˜ํ–‰ํ•ด์„œ ๋น ๋ฅด๊ฒŒ ์›์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‘ "differential" ๋ฐฐ์—ด ์ƒ์„ฑ์ž๋ฅผ ์ œ๊ณตํ•œ๋‹ค. DiffArray๋Š” IOArray์—์„œ ์œ ๋ž˜ํ•˜๊ณ  DiffUArray๋Š” IOUArray์— ๊ธฐ๋ฐ˜ํ•œ๋‹ค. ์ •๋ง ๊ทธ๋ž˜์•ผ ํ•œ๋‹ค๋ฉด IO ๋ชจ๋‚˜๋“œ ์•ˆ์—์„œ ์ž„์˜ MArray ํƒ€์ž…์œผ๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด "differential" ๋ฐฐ์—ด ํƒ€์ž…์„ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž์„ธํ•œ ๊ฒƒ์€ ๋ชจ๋“ˆ ๋ฌธ์„œํ™”๋ฅผ ๋ณผ ๊ฒƒ. DiffArray์˜ ์‚ฌ์šฉ๋ฒ•์€ Array์™€ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค. ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๋Ÿ‰๊ณผ ์†๋„๋‹ค. import Data.Array.Diff main = do let arr = listArray (1,1000) [1.. 1000] :: DiffArray Int Int a = arr ! 1 arr2 = arr // [(1,37)] b = arr2! 1 print (a, b) seq๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐฐ์—ด ๊ฐฑ์‹  ์ „์— ๋ฐฐ์—ด ์›์†Œ๋“ค์˜ ํ‰๊ฐ€๋ฅผ ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ๋‹ค. import Data.Array.Diff main = do let arr = listArray (1,1000) [1.. 1000] :: DiffArray Int Int a = arr ! 1 b = arr ! 2 arr2 = a `seq` b `seq` (arr // [(1,37),(2,64)]) c = arr2! 1 print (a, b, c) Unboxed arrays ๋Œ€๋ถ€๋ถ„์˜ ์ง€์—ฐ ํ‰๊ฐ€ ๊ตฌํ˜„์ฒด์—์„œ ๊ฐ’์€ ๋Ÿฐํƒ€์ž„์— ๊ทธ ๊ฐ’์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ ๋˜๋Š” ๊ทธ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ์ฝ”๋“œ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋กœ์„œ ํ‘œํ˜„๋œ๋‹ค. ์ด๋Ÿฐ ์—ฌ๋ถ„์˜ indirection๊ณผ ๋Ÿฐํƒ€์ž„์— ํ•„์š”ํ•œ ์ถ”๊ฐ€ ํƒœ๊ทธ๋“ค์„ ๋ชจ์•„์„œ ๋ฐ•์Šค๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๊ธฐ๋ณธ์ ์ธ "boxed" ๋ฐฐ์—ด์€ ์ด๋Ÿฐ ๋ฐ•์Šค๋“ค๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ ๊ฐ๊ฐ์˜ ๋ฐ•์Šค๋Š” ๊ทธ ๊ฐ’์„ ๋”ฐ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด๋กœ์จ ๋ฐฐ์—ด์˜ ์›์†Œ๋ฅผ ๋‹ค๋ฅธ ์›์†Œ๋ฅผ ์ด์šฉํ•ด ์ •์˜ํ•˜๊ฑฐ๋‚˜ ํ•„์š”ํ•œ ํŠน์ • ์›์†Œ๋“ค๋งŒ ๊ณ„์‚ฐํ•˜๋Š” ๋“ฑ ๋งŽ์€ ๋ฌ˜๊ธฐ๋ฅผ ๋ถ€๋ฆด ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ํฐ ๋ฐฐ์—ด์—์„œ ๋ฐ•์Šค๋Š” ๋งŽ์€ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์œ ๋ฐœํ•˜๋ฉฐ ๋ฐฐ์—ด ์ „์ฒด๊ฐ€ ํ•ญ์ƒ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋‚ญ๋น„์ผ ์ˆ˜ ์žˆ๋‹ค. Unboxed ๋ฐฐ์—ด(Data.Array.Unboxed์— ์ •์˜๋จ)์€ C์˜ ๋ฐฐ์—ด์— ๋” ๊ฐ€๊น๋‹ค. Unboxed ๋ฐฐ์—ด์€ ์ถ”๊ฐ€ indirection ์—†์ด ํ‰๋ฒ”ํ•œ ๊ฐ’์„ ๋ณด๊ด€ํ•˜๋ฏ€๋กœ ๊ฐ€๋ น Int32 ํƒ€์ž…์˜ ๊ฐ’ 1024๊ฐœ์˜ ๋ฐฐ์—ด์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ 4 kb๋งŒ ์‚ฌ์šฉํ•œ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ์ด๋Ÿฐ ๋ฐฐ์—ด์— ๋Œ€ํ•œ ์ธ๋ฑ์‹ฑ์€ ํ›จ์”ฌ ๋น ๋ฅด๋‹ค. ๋ฌผ๋ก  unboxed ๋ฐฐ์—ด๋„ ๋‹จ์ ์ด ์žˆ๋‹ค. ๋จผ์ € unboxed ๋ฐฐ์—ด์€ Int, Word, Char, Bool, Ptr, Double ๋“ฑ ๊ณ ์ • ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํ‰๋ฒ”ํ•œ ๊ฐ’์œผ๋กœ๋งŒ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. (UArray ํด๋ž˜์Šค ์ •์˜์— ๋‚˜์—ด๋˜์–ด ์žˆ์Œ) ์—ด๊ฑฐํ˜•์„ ํฌํ•จํ•ด ๊ฐ„๋‹จํ•œ ํƒ€์ž…์— ๋Œ€ํ•œ unboxed ๋ฐฐ์—ด์€ ์—ฌ๋Ÿฌ๋ถ„์ด ์ง์ ‘ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ Integer, String, ๊ธฐํƒ€ ๊ฐ€๋ณ€ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํƒ€์ž…๋“ค์€ unboxed ๋ฐฐ์—ด์˜ ์›์†Œ๊ฐ€ ๋  ์ˆ˜ ์—†๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ ์ถ”๊ฐ€ indirection์ด ์—†๋Š” unboxed ๋ฐฐ์—ด์„ ํ‰๊ฐ€ํ•  ๋•Œ๋Š” ๋ชจ๋“  ์›์†Œ๋ฅผ ํ‰๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ง€์—ฐ ํ‰๊ฐ€์˜ ์ด์ ์„ ์žƒ๋Š”๋‹ค. ๋ฐฐ์—ด์˜ ์›์†Œ ํ•˜๋‚˜๋งŒ ์ฝ๊ธฐ ์œ„ํ•œ ์ธ๋ฑ์‹ฑ๋„ ๋ฐฐ์—ด ์ „์ฒด์˜ ๊ตฌ์ถ•์„ ์œ ๋ฐœํ•œ๋‹ค. ๊ฒฐ๊ตญ ๋ฐฐ์—ด ์ „์ฒด๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด ๊ทธ๋‹ค์ง€ ์†์‹ค์€ ์•„๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐฐ์—ด์˜ ํ•œ ์›์†Œ๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์›์†Œ๋ฅผ ์žฌ๊ท€ ์ •์˜ํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜๋ฉฐ ํŠน์ • ๊ฐ’๋“ค๋งŒ ํ•„์š”ํ•  ๊ฒฝ์šฐ ๋น„์šฉ์ด ๋„ˆ๋ฌด ์ปค์งˆ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ unboxed ๋ฐฐ์—ด์€ ๋งค์šฐ ์œ ์šฉํ•œ ์ตœ์ ํ™” ์ˆ˜๋‹จ์ด๋ฉฐ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•  ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ชจ๋“  ์ฃผ ๋ฐฐ์—ด ํƒ€์ž…์—๋Š” ๋Œ€์‘ํ•˜๋Š” unboxed ๋ฒ„์ „์ด ์žˆ๋‹ค. Array - UArray (module Data.Array.Unboxed) IOArray - IOUArray (module Data.Array.IO) STArray - STUArray (module Data.Array.ST) DiffArray - DiffUArray (module Data.Array.Diff) ๊ทธ๋ž˜์„œ ํ”„๋กœ๊ทธ๋žจ์— ์žˆ๋Š” boxed ๋ฐฐ์—ด์„ unboxed ๋ฐฐ์—ด๋กœ ๋ฐ”๊พธ๋Š” ์ž‘์—…์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค. ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ์— 'U'๋งŒ ๋ถ™์ด๋ฉด ๋์ด๋‹ค. ๋ฌผ๋ก  Array๋ฅผ UArray๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์ž„ํฌํŠธ ๋ชฉ๋ก์— Data.Array.Unboxed๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. StorableArray storable ๋ฐฐ์—ด(Data.Array.Storable)์€ ๊ทธ ๋‚ด์šฉ๋ฌผ์„ C ํžˆํ”„ ๋‚ด์˜ ์—ฐ์†๋œ ๋ฉ”๋ชจ๋ฆฌ ๋ธ”๋ก์— ์ €์žฅํ•˜๋Š” IO-mutable ๋ฐฐ์—ด์ด๋‹ค. ์›์†Œ๋“ค์€ Storable ํด๋ž˜์Šค์— ์˜ํ•ด ์ €์žฅ๋œ๋‹ค. (?) ๋ฐฐ์—ด ๋‚ด์šฉ๋ฌผ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ์–ป์–ด์„œ C์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์›์†Œ๋“ค์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. storable ๋ฐฐ์—ด์€ (ํŠนํžˆ ๋™์ผํ•œ MArray ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์ ์—์„œ) IOUArray์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๋” ๋Š๋ฆฌ๋‹ค. ์žฅ์ ์€ FFI๋ฅผ ํ†ตํ•ด C์™€ ํ˜ธํ™˜ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. stroable ๋ฐฐ์—ด์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋Š” ๊ณ ์ •์ด๊ธฐ ๋•Œ๋ฌธ์— C ๋ฃจํ‹ด์— ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด ๋‚ด์šฉ๋ฌผ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋Š” withStorableArray๋ฅผ ํ†ตํ•ด ํš๋“ํ•œ๋‹ค. ์•„์ด๋””์–ด๋Š” ForeignPtr๊ณผ ๋น„์Šทํ•˜๋‹ค(๋‚ด๋ถ€์ ์œผ๋กœ๋„ ์‚ฌ์šฉํ•œ๋‹ค). ์ด ํฌ์ธํ„ฐ๋Š” withStorableArray์— ์ธ์ž๋กœ์„œ ์ „๋‹ฌํ•œ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” IO ์•ก์…˜์˜ ์‹คํ–‰ ๋„์ค‘์—๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. {-# OPTIONS_GHC -fglasgow-exts #-} import Data.Array.Storable import Foreign.Ptr import Foreign.C.Types main = do arr <- newArray (1,10) 37 :: IO (StorableArray Int Int) a <- readArray arr 1 withStorableArray arr (\ptr -> memset ptr 0 40) b <- readArray arr 1 print (a, b) foreign import ccall unsafe "string.h" memset :: Ptr a -> CInt -> CSize -> IO () ์ด ํฌ์ธํ„ฐ๋ฅผ ์ดํ›„์—๋„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํฌ์ธํ„ฐ๋ฅผ ๋งˆ์ง€๋ง‰์œผ๋กœ ์‚ฌ์šฉํ•œ ํ›„ touchStorableArray๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ด ๋ฐฐ์—ด์ด ์ผ์ฐ ํ•ด์ œ๋˜์ง€ ์•Š๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์ถ”๊ฐ€ ์ฝ”๋ฉ˜ํŠธ: GHC 6.6์€ StorableArray์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ๋‹ค๋ฅธ unboxed ๋ฐฐ์—ด๋งŒํผ ๋น ๋ฅด๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. StorableArray์™€ UArray์˜ ์œ ์ผํ•œ ์ฐจ์ด์ ์€ UArray๋Š” GHC ํžˆํ”„์˜ ์žฌ๋ฐฐ์น˜ ๊ฐ€๋Šฅํ•œ ์˜์—ญ์— ์žˆ๊ณ  StorableArray๋Š” ์žฌ๋ฐฐ์น˜ ๋ถˆ๊ฐ€ ์˜์—ญ์— ์žˆ์œผ๋ฉฐ ์ฃผ์†Œ๊ฐ€ ๊ณ ์ •๋˜์–ด์„œ ์ด ์ฃผ์†Œ๋ฅผ C ๋ฃจํ‹ด์— ์ „๋‹ฌํ•˜๊ณ  C ๋ฐ์ดํ„ฐ๊ตฌ์กฐ ์•ˆ์— ๊ทธ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ๋ฟ์ด๋‹ค. GHC 6.6์€ unsafeForeignPtrToStorableArray ์—ฐ์‚ฐ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ž„์˜์˜ Ptr์„ StorableArray์˜ ์ฃผ์†Œ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋ฉฐ ํŠนํžˆ C ๋ฃจํ‹ด์ด ๋ฐ˜ํ™˜ํ•œ ๋ฐฐ์—ด์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. ๋‹ค์Œ์€ ์ด ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ๋‹ค. import Data.Array.Storable import Foreign.Marshal.Alloc import Foreign.Marshal.Array import Foreign.ForeignPtr main = do ptr <- mallocArray 10 fptr <- newForeignPtr_ ptr arr <- unsafeForeignPtrToStorableArray (1,10) fptr :: IO (StorableArray Int Int) writeArray arr 1 64 a <- readArray arr 1 print a free ptr ์ด ์˜ˆ์‹œ๋Š” Int 10๊ฐœ๋ฅผ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๊ณ (C ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ๋ฐฐ์—ด์„ ํ‰๋‚ด) ๋ฐ˜ํ™˜๋ฐ›์€ Ptr Int๋ฅผ ForeignPtr Int๋กœ ๋ณ€ํ™˜ํ•œ ๋‹ค์Œ ForeignPtr Int๋ฅผ ๋‹ค์‹œ StorableArray Int Int๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ์“ฐ๊ณ  ์ฝ๋Š”๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐฐ์—ด์ด ์‚ฌ์šฉํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ free๋กœ ํ•ด์ œํ•ด์„œ C ๋ฃจํ‹ด์˜ ํ•ด์ œ๋ฅผ ํ‰๋‚ด ๋‚ธ๋‹ค. newForeignPtr_ ptr๋ฅผ newForeignPtr finalizerFree ptr๋กœ ๊ต์ฒดํ•˜๋ฉด ํ• ๋‹น๋œ ๋ธ”๋ก์„ ์ž๋™ ํ•ด์ œํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌ๋ฉด ๋‹ค๋ฅธ ํ•˜์Šค ์ผˆ ๊ฐ์ฒด๋“ค์ฒ˜๋Ÿผ ๋ฐฐ์—ด์„ ๋งˆ์ง€๋ง‰์œผ๋กœ ์‚ฌ์šฉํ•œ ์ดํ›„ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ž๋™ ํ•ด์ œ๋œ๋‹ค. The Haskell Array Preprocessor (STPP) ํ•˜์Šค์ผˆ์—์„œ ๊ฐ€๋ณ€ ๋ฐฐ์—ด(IO, ST ๋ฒ„์ „)์€ ์‚ฌ์šฉํ•˜๊ธฐ์— ๊ทธ๋‹ค์ง€ ํŽธํ•˜์ง€ ์•Š๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํŽธ์˜ ๋ฌธ๋ฒ•์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ทธ๋Ÿฐ ๋ฐฐ์—ด์˜ ์‚ฌ์šฉ๋ฒ•์„ ๋ช…๋ นํ˜• ์–ธ์–ด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋„๊ตฌ๊ฐ€ ์žˆ๋‹ค. ์ด ๋„๊ตฌ๋Š” Hal Daume III์ด ์ž‘์„ฑํ–ˆ๊ณ  http://hal3.name/STPP/STPP.tar.gz์—์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณต์žกํ•œ ํ‘œํ˜„์‹ ์•ˆ์—์„œ arr[|i|]๋กœ ๋ฐฐ์—ด ์›์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ  ์ „์ฒ˜๋ฆฌ๊ธฐ๊ฐ€ ๊ทธ๋Ÿฐ ๊ตฌ๋ฌธ์„ ์ž๋™์œผ๋กœ ์•Œ๋งž์€ readArray์™€ writeArray ํ˜ธ์ถœ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๋‹ค์ฐจ์› ๋ฐฐ์—ด๋„ arr[|i|][|j|] ํ˜•ํƒœ๋กœ ์ง€์›๋œ๋‹ค. ์ž์„ธํ•œ ๊ฒƒ์€ http://hal3.name/STPP/๋ฅผ ๋ณผ ๊ฒƒ. ArrayRef ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ArrayRef ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‹ค์Œ ํ™•์žฅ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•˜์—ฌ ๋ฐฐ์—ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์žฌ๊ตฌํ˜„ํ•œ๋‹ค. ๋™์  (ํฌ๊ธฐ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ) ๋ฐฐ์—ด ๋‹คํ˜• unboxed ๋ฐฐ์—ด ๊ทธ๋ฆฌ๊ณ  ํŽธ์˜ ๋ฌธ๋ฒ•์„ ์ถ”๊ฐ€ํ•ด ๋ฐฐ์—ด ์‚ฌ์šฉ๋ฒ•์„ ๊ฐ„์†Œํ™”ํ•œ๋‹ค. STPP์ฒ˜๋Ÿผ ์šฐ์•„ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ์ „์ฒ˜๋ฆฌ๊ธฐ ์—†์ด ํ•˜์Šค ์ผˆ ์–ธ์–ด๋งŒ์œผ๋กœ ๊ตฌํ˜„๋œ ๊ฒƒ์ด๋‹ค. unsafe operations and running over array elements ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ ๋™์ผ ํƒ€์ž…์˜ ๊ฐ€๋ณ€ ๋ฐฐ์—ด๊ณผ ๋ถˆ๋ณ€ ๋ฐฐ์—ด ์‚ฌ์ด๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ๋ช…๋ น์ด ์žˆ๋Š”๋ฐ ๋ฐ”๋กœ freeze(๊ฐ€๋ณ€ -> ๋ถˆ๋ณ€)์™€ thaw(๋ถˆ๋ณ€ -> ๊ฐ€๋ณ€)์ด๋‹ค. ์ด๊ฒƒ๋“ค์€ ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋ณต์‚ฌํ•œ๋‹ค. ๊ฐ€๋ณ€ ๋ฐฐ์—ด์ด ์ˆ˜์ •๋˜์ง€ ์•Š๊ฑฐ๋‚˜ ๋ถˆ๋ณ€ ๋ฐฐ์—ด์ด ๋ณ€ํ™˜ ํ›„์—๋Š” ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ํ™•์‹ ์ด ์žˆ๋‹ค๋ฉด unsafeFreeze/unsafeThaw๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ช…๋ น๋“ค์€ ์ž…๋ ฅ ๋ฐฐ์—ด๊ณผ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์˜ ๋ฉ”๋ชจ๋ฆฌ ํ‘œํ˜„์ด ๊ฐ™์œผ๋ฉด(์ฆ‰ ํƒ€์ž…๊ณผ boxing ์—ฌ๋ถ€๊ฐ€ ๊ฐ™์œผ๋ฉด) ๋ฐฐ์—ด์„ ์ œ์ž๋ฆฌ์—์„œ ๋ณ€ํ™˜ํ•œ๋‹ค. "unsafe*" ๋ช…๋ น์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜๋ผ. ๊ทธ๋Ÿฐ ๋ช…๋ น์€ ๋ฐฐ์—ด ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ ์—ฌ๋ถ€์— ๋Œ€ํ•œ ๋ฐฐ์—ด ํ—ค๋” ํ”Œ๋ž˜๊ทธ๋ฅผ ์„ค์ • ๋˜๋Š” ํ•ด์ œํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋Ÿฐ ๋ช…๋ น์€ ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ์ ‘๊ทผ(์Šค๋ ˆ๋“œ ๋˜๋Š” ์ฝ”๋ฃจํ‹ด)๊ณผ ํ•จ๊ป˜ ์“ธ ์ˆ˜ ์—†๋‹ค. unboxed ๋ฐฐ์—ด์„ ๋‹ค๋ฅธ ์›์†Œ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” castIOUArray์™€ castSTUArray๋„ ์žˆ๋‹ค. ์ด ๋ช…๋ น๋“ค์€ ๋ฉ”๋ชจ๋ฆฌ ๋‚ด ์‹ค์ œ ํƒ€์ž… ํ‘œํ˜„์— ์˜์กดํ•˜๋ฏ€๋กœ ๊ทธ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ์•„๋ฌด ๋ณด์žฅ๋„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํŠนํžˆ ์ด ๋ช…๋ น๋“ค์€ ์ž„์˜์˜ unboxed ๊ฐ’์„ ์ผ๋ จ์˜ ๋ฐ”์ดํŠธ๋กœ ๋˜๋Š” ๊ทธ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. AltBinary ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋ถ€๋™์†Œ์ˆ˜์  ๊ฐ’์„ ์ง๋ ฌํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์ด๊ฒƒ๋“ค์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ๋ช…๋ น๋“ค์€ ์›์†Œ ํฌ๊ธฐ์˜ ๋ณ€ํ™”์— ๋”ฐ๋ผ ๋ฐฐ์—ด์˜ ๋ฐ”์šด๋“œ๋ฅผ ์žฌ๊ณ„์‚ฐํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜๋ผ. ๊ทธ ์ผ์€ ์—ฌ๋Ÿฌ๋ถ„์ด sizeOf ๋ช…๋ น์„ ํ†ตํ•ด ์ง์ ‘ ํ•ด์•ผ ํ•œ๋‹ค. ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋Š” ์–ด๋–ค ํƒ€์ž…๋„ ๋  ์ˆ˜ ์žˆ์ง€๋งŒ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฐ”์šด๋“œ ๊ฒ€์‚ฌ ์ดํ›„ ๋ชจ๋“  ์ธ๋ฑ์Šค๋Š” ํ‰๋ฒ”ํ•œ Int ๊ฐ’(์œ„์น˜)๋กœ ๋ณ€ํ™˜๋˜๋ฉฐ ๋กœ์šฐ ๋ ˆ๋ฒจ ๋ช…๋ น์ธ unsafeAt, unsafeRead, unsafeWrite ์ค‘ ํ•˜๋‚˜๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. ์ด ๋ช…๋ น๋“ค์„ ์ง์ ‘ ์ƒ ์š”ํ•ด์„œ ๋ฐ”์šด๋“œ ๊ฒ€์‚ฌ๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ฑฐ๋‚˜ ํ”„๋กœ๊ทธ๋žจ์„ ๋น ๋ฅด๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ช…๋ น๋“ค์€ ๋ฐฐ์—ด ์ „์ฒด์— ์ ‘๊ทผํ•  ๋•Œ ํŠนํžˆ ์œ ์šฉํ•˜๋‹ค. -- | Returns a list of all the elements of an array, in the same order -- as their indices. elems arr = [ unsafeAt arr i | i <- [0 .. rangeSize (bounds arr)-1] ] ์ด๋Ÿฐ ๋ฃจํ”„ ์•ˆ์˜ "unsafe*" ๋ช…๋ น๋“ค์€ ์‹ค์ œ๋กœ๋Š” ์•ˆ์ „ํ•œ๋ฐ i๊ฐ€ ํ•ญ์ƒ ์กด์žฌํ•˜๋Š” ๋ฐฐ์—ด ์›์†Œ๋“ค์˜ ์œ„์น˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. GHC์— ํ•œ์ •๋œ ํ™”์ œ๋“ค ํ‰ํ–‰ ๋ฐฐ์—ด (GHC.PArr ๋ชจ๋“ˆ) ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ๋ฐฐ์—ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‘ ๋ฐฐ์—ด ๋ณ€์ข…์„ ์ง€์›ํ•œ๋‹ค. ๋ฐ”๋กœ ์ง€์—ฐ boxed ๋ฐฐ์—ด๊ณผ ์—„๊ฒฉํ•œ unboxed ๋ฐฐ์—ด์ด๋‹ค. ํ‰ํ–‰ ๋ฐฐ์—ด์€ ๊ทธ ๊ฒฝ๊ณ„์— ์žˆ๋Š” ๊ฒƒ, ์—„๊ฒฉํ•œ boxed ๋ถˆ๋ณ€ ๋ฐฐ์—ด์„ ๊ตฌํ˜„ํ•œ๋‹ค. ์–ด๋–ค ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋˜ ๋ฐฐ์—ด ์›์†Œ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์—ฐํ•จ์„ ๊ฐ–์ถ”๋ฉด์„œ ๋ฐฐ์—ด์˜ ์ƒ์„ฑ๊ณผ ์ ‘๊ทผ์„ ๋น ๋ฅด๊ฒŒ ๋งŒ๋“ ๋‹ค. ๋ฐฐ์—ด ์ƒ์„ฑ์€ ๋ชจ๋“  ๋ฐฐ์—ด ์›์†Œ๋ฅผ ์ฑ„์šฐ๋Š” ๋‹จ์ผ ๋ช…๋ นํ˜• ๋ฃจํ”„๋กœ์„œ ๊ตฌํ˜„๋˜๋ฉฐ ๋ฐฐ์—ด ์›์†Œ์— ๋Œ€ํ•œ ์ ‘๊ทผ์€ "box"๋ฅผ ๊ฒ€์‚ฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋ฐฐ์—ด ์›์†Œ์˜ ๊ณ„์‚ฐ์ด ๋ณต์žกํ•˜๊ณ  ๋Œ€๋ถ€๋ถ„์˜ ๋ฐฐ์—ด ์›์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” ํ‰ํ–‰ ๋ฐฐ์—ด์ด ๋น„ํšจ์œจ์ ์ž„์ด ๋ช…๋ฐฑํ•˜๋‹ค. ์‹ค์ „์—์„œ์˜ ๋˜ ๋‹ค๋ฅธ ๋‹จ์ ์€ ํ‰ํ–‰ ๋ฐฐ์—ด์ด IArray ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์•„์„œ Array์™€ ํ‰ํ–‰ ๋ฐฐ์—ด ์ƒ์„ฑ์ž ๋‘˜ ๋‹ค ์ง€์›ํ•˜๋Š” ์ผ๋ฐ˜ํ™”๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ž‘์„ฑํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋งŽ์€ GHC ํ™•์žฅ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด ํ™•์žฅ์€ Manuel M. T. Chakravarty์™€ Gabriele Keller์˜ ๋…ผ๋ฌธ An Approach to Fast Arrays in Haskell์— ๊ธฐ์ˆ ๋˜์–ด ์žˆ๋‹ค. GHC.PArr ๋ชจ๋“ˆ์˜ ์†Œ์Šค๋ฅผ ์‚ดํŽด๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. ์ฃผ์„์ด ๋งŽ์ด ๋‹ฌ๋ ค์žˆ๋‹ค. GHC 6.4.1์˜ ์‚ฌ์šฉ์ž ๋งค๋‰ด์–ผ์—๋Š” ๋ฌธ์„œํ™”๋˜์–ด์žˆ์ง€ ์•Š์ง€๋งŒ, ํ‰ํ–‰ ๋ฐฐ์—ด์„ ์œ„ํ•œ ํŠน์ˆ˜ ๋ฌธ๋ฒ•์€ "ghc -fparr" ๋˜๋Š” "ghci -fparr"๋ฅผ ํ†ตํ•ด ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. Welcome to the machine Array#, MutableArray#, ByteArray#, MutableByteArray#, pinned and moveable byte arrays GHC ํžˆํ”„๋Š” ๋‘ ์ข…๋ฅ˜์˜ ๊ฐœ์ฒด๋“ค์„ ๋ณด๊ด€ํ•œ๋‹ค. ํ•œ ์ข…๋ฅ˜๋Š” ๊ทธ์ € ๋ฐ”์ดํŠธ ์‹œํ€€์Šค๋“ค์ด๊ณ  ๋‹ค๋ฅธ ์ข…๋ฅ˜๋Š” ํƒ€ ๊ฐœ์ฒด๋“ค์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ("๋ฐ•์Šค")๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒƒ๋“ค์ด๋‹ค. ์ด๋Ÿฌํ•œ ๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ๋ ˆํผ๋Ÿฐ์Šค ์—ฐ์‡„๋ฅผ ์ฐพ๊ณ , ํžˆํ”„๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์••์ถ•๋˜์–ด ๊ฐœ์ฒด๋“ค์ด ์ƒˆ ์œ„์น˜๋กœ ์˜ฎ๊ฒจ์งˆ ๋•Œ ๊ทธ ํฌ์ธํ„ฐ๋“ค์„ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค. GHC์˜ ๋‚ด๋ถ€(raw) ํƒ€์ž…์ธ Array#๋Š” ๊ฐ์ฒด ํฌ์ธํ„ฐ๋“ค(๋ฐ•์Šค๋“ค)์˜ ์‹œํ€€์Šค๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ST ๋ชจ๋‚˜๋“œ์—๋Š” ํžˆํ”„์— ํŠน์ • ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ํ• ๋‹นํ•˜๋Š” ๋กœ์šฐ ๋ ˆ๋ฒจ ๋ช…๋ น์ด ์žˆ์œผ๋ฉฐ ๊ทธ ํƒ€์ž…์€ (Int -> ST s Array#)์ด๋‹ค. Array# ํƒ€์ž…์€ boxed ๋ถˆ๋ณ€ ๋ฐฐ์—ด์„ ๋‚˜ํƒ€๋‚ด๋Š” Array ํƒ€์ž…์˜ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋œ๋‹ค. MutableArray#๋Š” ๊ฐ€๋ณ€ boxed ๋ฐฐ์—ด(IOArray/STArray)์„ ์œ„ํ•œ ๋˜ ๋‹ค๋ฅธ ํƒ€์ž…์ด๋‹ค. ๊ฐ€๋ณ€ ๋ฐฐ์—ด์„ ์œ„ํ•œ ๋ณ„๋„์˜ ํƒ€์ž…์ด ํ•„์š”ํ•œ ์ด์œ ๋Š” 2๋‹จ๊ณ„ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ๋•Œ๋ฌธ์ด๋‹ค. Array#์™€ MutableArray#์˜ ๋‚ด๋ถ€ ํ‘œํ˜„์€ ํ—ค๋”์˜ ๋ช‡ ํ”Œ๋ž˜๊ทธ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ œ์ž๋ฆฌ์—์„œ MutableArray#์™€ Array#๋ฅผ ์ƒํ˜ธ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. (์ด๊ฒƒ์ด unsafeFreeze์™€ usnafeThaw๊ฐ€ ํ•˜๋Š” ์ผ์ด๋‹ค) unboxed ๋ฐฐ์—ด์€ ByteArray# ํƒ€์ž…์— ์˜ํ•ด ํ‘œํ˜„๋œ๋‹ค. ์ด๊ฒƒ์€ C ๋ฐฐ์—ด์ฒ˜๋Ÿผ ํžˆํ”„์˜ ํ‰๋ณŒํ•œ ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์ด๋‹ค. ํŠน์ • ํฌ๊ธฐ์˜ ByteArray#๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์›์‹œ ์—ฐ์‚ฐ์ด ๋‘ ๊ฐœ ์žˆ๋‹ค. ํ•˜๋‚˜๋Š” ์ผ๋ฐ˜ ํžˆํ”„์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๋ฉฐ ์ด ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์€ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์ด ์ผ์–ด๋‚  ๋•Œ๋งˆ๋‹ค ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ByteArray#๋Š” C ํ”„๋Ÿฌ์‹œ ์ €๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ‰๋ฒ”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ํฌ์ธํ„ฐ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋‹ค. (๊ทธ๋Ÿผ์—๋„ ํ˜„์žฌ ByteArray# ํฌ์ธํ„ฐ๋ฅผ "unsafe foreign" ํ”„๋Ÿฌ์‹œ์ €์— ์ „๋‹ฌํ•  ์ˆ˜๋Š” ์žˆ๋‹ค. ๊ทธ ํ”„๋Ÿฌ์‹œ ์ €๊ฐ€ ์ด ํฌ์ธํ„ฐ๋ฅผ ๋‹ค๋ฅธ ๊ณณ์— ์ €์žฅํ•˜๋ ค ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด.) Mutable arrays and GC GHC๋Š” ๊ณ ์† 2๋‹จ๊ณ„ GC๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. 256 kb๊ฐ€ ํ• ๋‹น๋  ๋•Œ๋งˆ๋‹ค ๋งˆ์ด๋„ˆ GC๊ฐ€ ์ผ์–ด๋‚˜๊ณ  "์‚ด์•„์žˆ๋Š”" ๋ฐ์ดํ„ฐ๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ ์ด ์˜์—ญ(์ตœ์‹  ์Šคํƒ ํ”„๋ ˆ์ž„๋„)๋งŒ ์Šค์บ”ํ•œ๋‹ค. ์ด ๋ฐฉ์นจ์€ ์ผ๋ฐ˜์ ์ธ ํ•˜์Šค ์ผˆ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถˆ๋ณ€์ด๋ฉฐ ๋”ฐ๋ผ์„œ ๋งˆ์ด๋„ˆ GC ์ด์ „์— ์ƒ์„ฑ๋œ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ์ด GC ์ดํ›„ ์ƒ์„ฑ๋œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์—†๋‹ค๋Š” ์ ์„ ํ™œ์šฉํ•œ๋‹ค. (๋ถˆ๋ณ€ ๋ฐ์ดํ„ฐ๋Š” "backward" ๋ ˆํผ๋Ÿฐ์Šค๋งŒ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ) ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ๋‹จ์ˆœํ•จ์€ ์–ธ์–ด์— ๊ฐ€๋ณ€ boxed ๋ ˆํผ๋Ÿฐ์Šค(IORef/STRef)์™€ ๋ฐฐ์—ด(IOArray/STArray)๋ฅผ ๋„์ž…ํ•˜๋ฉด ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋งˆ์ด๋„ˆ GC๋ฅผ ํฌํ•จํ•ด ๊ฐ GC๋งˆ๋‹ค ๊ฐ€๋ณ€ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ฐ ์›์†Œ๋ฅผ ์Šค์บ”ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๋งˆ์ง€๋ง‰ GC ์ดํ›„ ๊ทธ ์›์†Œ๊ฐ€ ๊ฐฑ์‹ ๋˜์–ด ์ด์ „ GC ์ดํ›„ ํ• ๋‹น๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ฐ€๋ณ€ boxed ๋ฐฐ์—ด/๋ ˆํผ๋Ÿฐ์Šค์— ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฒฝ์šฐ ์˜๋ฏธ ์žˆ๋Š” ๊ณ„์‚ฐ ์‹œ๊ฐ„๋ณด๋‹ค GC ์‹œ๊ฐ„์ด ๊ธธ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. ์•„์ด๋Ÿฌ๋‹ˆํ•˜๊ฒŒ๋„ GHC ์ž์ฒด๊ฐ€ ๊ทธ๋Ÿฌํ•œ ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. ์ด์— ๋Œ€ํ•œ ํ•ด๋ฒ•์€ "+RTS -A10m" ๊ฐ™์€ ์ปค๋งจ๋“œ ๋ผ์ธ ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์„œ ๋งˆ์ด๋„ˆ GC ๋ฉ์–ด๋ฆฌ์˜ ํฌ๊ธฐ๋ฅผ 256 kb์—์„œ 10 mb๋กœ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰ ๋งˆ์ด๋„ˆ GC๋ฅผ 40๋ฐฐ ๋œ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ผ์–ด๋‚˜๋„๋ก ๋งŒ๋“ ๋‹ค. ์ด๋Ÿฐ ๋ณ€๊ฒฝ์˜ ์˜ํ–ฅ์€ "+RTS -sstderr" ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. "%GC time"์ด ๊ทน์ ์œผ๋กœ ์ค„์—ˆ์„ ๊ฒƒ์ด๋‹ค. ์‹คํ–‰ ํŒŒ์ผ์— ์ด ์˜ต์…˜์„ ํฌํ•จํ•ด์„œ ๋งค ์‹คํ–‰๋งˆ๋‹ค ์ž๋™์œผ๋กœ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ํ”„๋กœ์ ํŠธ์— ๋‹ค์Œ ์ค„์„ ํฌํ•จํ•˜๋Š” C ์†Œ์Šค ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค. char *ghc_rts_opts = "-A10m"; ๋ฌผ๋ก  ํ•„์š”์— ๋”ฐ๋ผ ์ด ๊ฐ’์„ ๋Š˜๋ฆด ์ˆ˜๋„ ์ค„์ผ ์ˆ˜๋„ ์žˆ๋‹ค. "-A" ๊ฐ’์„ ๋Š˜๋ฆฌ๋Š” ๋น„์šฉ์€ ๊ณต์งœ๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์€ ๋‹น์—ฐํžˆ ์ฆ๊ฐ€ํ•˜๋ฉฐ (์˜๋ฏธ ์žˆ๋Š” ์ฝ”๋“œ์˜) ์‹คํ–‰ ์‹œ๊ฐ„๋„ ์ฆ๊ฐ€ํ•œ๋‹ค. "-A"์˜ ๊ธฐ๋ณธ๊ฐ’์€ ํ˜„๋Œ€ CPU ์บ์‹œ ์‚ฌ์ด์ฆˆ์— ๊ทผ์ ‘ํ•˜๋„๋ก ๋งž์ถฐ์ ธ์žˆ์–ด ๋Œ€๋ถ€๋ถ„์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฐธ์กฐ๊ฐ€ ์บ์‹œ ์•ˆ์— ๋“ค์–ด๊ฐ€๋„๋ก ํ•œ๋‹ค. GC๋ฅผ ํ•˜๊ธฐ ์ „์— 10 mb์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๋ฉด ๊ทธ๋Ÿฐ ๋ฐ์ดํ„ฐ ์ง€์—ญ์„ฑ์€ ๋” ์ด์ƒ ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ "-A"๋ฅผ ๋Š˜๋ฆฌ๋ฉด ํ”„๋กœ๊ทธ๋žจ ์†๋„๊ฐ€ ๋นจ๋ผ์งˆ ์ˆ˜๋„ ๋Š๋ ค์งˆ ์ˆ˜๋„ ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ "์ผ๋ฐ˜์ ์ธ" ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค๊ณผ ํ•จ๊ป˜ ์‹คํ–‰ํ•˜๋ฉด์„œ ์ด ์„ค์ •์— 64 kb์—์„œ 16 mb ์‚ฌ์ด์˜ ๋‹ค์–‘ํ•œ ๊ฐ’์„ ์‹œ๋„ํ•ด ๋ณด๊ณ  ์—ฌ๋Ÿฌ๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ๊ณผ cpu์— ์ตœ์ ์˜ ์„ค์ •์„ ์„ ํƒํ•  ๊ฒƒ. GC ์‹œ๊ฐ„์˜ ์ฆ๊ฐ€๋ฅผ ํ”ผํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. unboxed ๋˜๋Š” ๋ถˆ๋ณ€ ๋ฐฐ์—ด์„ ์“ฐ๋Š” ๊ฒƒ์ด๋‹ค. ๋ถˆ๋ณ€ ๋ฐฐ์—ด์€ ๊ฐ€๋ณ€ ๋ฐฐ์—ด๋กœ์„œ ์ƒ์„ฑ๋˜๊ณ  "freeze"๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑํ•  ๋•Œ๋Š” GC๊ฐ€ ๊ทธ ๋‚ด์šฉ๋ฌผ์„ ์Šค์บ”ํ•œ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜๋ผ. ๋‹คํ–‰ํžˆ๋„ GHC 6.6์€ ์ด ๋ฌธ์ œ๋ฅผ ๊ณ ์นœ๋‹ค. GHC 6.6์€ ๋งˆ์ง€๋ง‰ GC ์ดํ›„ ์–ด๋–ค ๋ ˆํผ๋Ÿฐ์Šค/๋ฐฐ์—ด์ด ๊ฐฑ์‹ ๋˜์—ˆ๋Š”์ง€ ๊ธฐ์–ตํ•˜๊ณ  ๊ทธ๊ฒƒ๋“ค๋งŒ ์Šค์บ”ํ•œ๋‹ค. ๋งค์šฐ ํฐ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ทธ๋Ÿฐ ๋ฌธ์ œ๋ฅผ ์—ฌ์ „ํžˆ ๊ฒช๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ์ถ”๊ฐ€ ์ •๋ณด: ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๋ฅผ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•œ RTS ์˜ต์…˜๋“ค Simon Marlow์˜ ๋ฌธ์ œ ์„ค๋ช…๊ณผ ์ด ๋ถ„์•ผ์—์„œ GHC 6.6์˜ ๊ฐœ์„ ์‚ฌํ•ญ์— ๋Œ€ํ•œ ๋ณด๊ณ ์„œ GHC ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ์— ๋Œ€ํ•œ ๋…ธํŠธ GHC ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ์— ๋Œ€ํ•œ ๋…ผ๋ฌธ๋“ค ์ด ํŽ˜์ด์ง€์˜ ์ผ๋ถ€๋Š” ํ•˜์Šค ์ผˆ ์œ„ํ‚ค์˜ Modern array libraries์—์„œ Simple Permissive ๋ผ์ด์„ ์Šค ํ•˜์— ๊ฐ€์ ธ์˜จ ๊ฒƒ์ด๋‹ค. ์ด ํŽ˜์ด์ง€๋ฅผ ์ˆ˜์ •ํ•˜๊ณ ์ž ํ•˜๋ฉฐ ๊ทธ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์ด ์œ„ํ‚ค์—๋„ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™๋‹ค๋ฉด ์—ฌ๊ธฐ ๋Œ€์‹  ์›๋ณธ ํŽ˜์ด์ง€๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•˜๋ผ. ํ•˜์Šค ์ผˆ ์œ„ํ‚ค์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์€ ์—ฌ๊ธฐ์— ๋ฐ˜์˜๋  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ ๋ฐ˜๋Œ€๋Š” ์ผ์–ด๋‚˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋˜๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ๊ธฐ์—ฌ ์‚ฌํ•ญ์„ Simple Permissive ๋ผ์ด์„ ์Šค ํ•˜์— ์ด์ค‘ ๋ผ์ด์„ ์‹ฑ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 2 ๋งต ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Libraries/Maps ๋™๊ธฐ ์™œ ๊ทธ๋ƒฅ [(a, b)]๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๊ฐ€? ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ ์˜ˆ์‹œ Data.Map ๋ชจ๋“ˆ์ด ์ œ๊ณตํ•˜๋Š” Map ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ํŠน์ • ํ‚ค์— ๋ถ€์ฐฉ๋œ ๊ฐ’์„ ๋ณด๊ด€ํ•œ๋‹ค. ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋Š” ๋ฃฉ์—… ํ…Œ์ด๋ธ”, ์‚ฌ์ „(dictionary), ์—ฐ๊ด€ ๋ฐฐ์—ด(associative array) ๋“ฑ์œผ๋กœ ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค. ๋™๊ธฐ ๊ฐ’ ๋˜๋Š” ๊ฐ’์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ํŠน์ • ํ‚ค์— ์—ฐ๊ด€ ์ง“๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์œ ์šฉํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์ด๋Ÿฐ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‹ค์ƒํ™œ์—์„œ ๋”ฐ์™€์„œ ์‚ฌ์ „์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ณ ๋Š” ํ•œ๋‹ค. ์‹ค์ œ ์‚ฌ์ „์€ ์ •์˜(๊ฐ’)์„ ๊ฐ ๋‹จ์–ด(ํ‚ค)์— ์—ฐ๊ด€ ์ง“๋Š”๋‹ค. ์ด ์‚ฌ์ „์€ ๋‹จ์–ด์—์„œ ์ •์˜๋กœ ๊ฐ€๋Š” ๋งต์ด๋‹ค. ํŒŒ์ผ์‹œ์Šคํ…œ ๋“œ๋ผ์ด๋ฒ„๋Š” ํŒŒ์ผ ์ด๋ฆ„์—์„œ ํŒŒ์ผ ์ •๋ณด๋กœ ๊ฐ€๋Š” ๋งต์„ ๋ณด๊ด€ํ•  ๊ฒƒ์ด๊ณ , ์ „ํ™”๋ฒˆํ˜ธ๋ถ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์—ฐ๋ฝ๋ช…์—์„œ ์ „ํ™”๋ฒˆํ˜ธ๋กœ ๊ฐ€๋Š” ๋งต์„ ๋ณด๊ด€ํ•  ๊ฒƒ์ด๋‹ค. ๋งต์€ ๋งค์šฐ ๋‹ค์žฌ๋‹ค๋Šฅํ•˜๊ณ  ์œ ์šฉํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค. ์™œ ๊ทธ๋ƒฅ [(a, b)]๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๊ฐ€? ๋‹ค๋ฅธ ์žฅ์—์„œ ์Œ์˜ ๋ฆฌ์ŠคํŠธ(๋˜๋Š” '๋ฃฉ์—… ํ…Œ์ด๋ธ”')๋ฅผ lookup :: a -> [(a, b)] -> Maybe b ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์ผ์ข…์˜ ๋งต์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ดค์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์™œ ํ•ญ์ƒ ๋ฃฉ์—… ํ…Œ์ด๋ธ”์„ ์“ฐ์ง€ ์•Š์„๊นŒ? ๊ทธ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋งต์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฃฉ์—… ํ…Œ์ด๋ธ”์„ ์“ฐ๋Š” ๊ฒƒ๋ณด๋‹ค ์œ ์šฉํ•œ ํ•จ์ˆ˜๋“ค์„ ๋งŽ์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งต์€ ๋ฃฉ์—… ํ…Œ์ด๋ธ”๋ณด๋‹ค ํšจ์œจ์ ์œผ๋กœ ๊ตฌํ˜„๋œ๋‹ค. ํŠนํžˆ ์กฐํšŒ ์†๋„์— ๊ด€ํ•ด์„œ๋Š”. 1 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ Data.Map ๋ชจ๋“ˆ์€ Map์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. ๊ทธ๋Ÿฐ ํ•จ์ˆ˜๋กœ๋Š” ํ•ฉ์ง‘ํ•ฉ๊ณผ ๊ต์ง‘ํ•ฉ ๋“ฑ ์ง‘ํ•ฉ ์—ฐ์‚ฐ ๋น„์Šทํ•œ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค. ์™„์ „ํ•œ ๋ชฉ๋ก์€ ์ฝ”์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฌธ์„œ 2์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์‹œ ๋‹ค์Œ ์˜ˆ์‹œ๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ์‹ ๋ขฐ๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฏ€๋กœ ์ธ์ฆ์ด ํ•„์š” ์—†๊ณ  ๋น„๋ฐ€๋ฒˆํ˜ธ ์กฐํšŒ์™€ ๋ณ€๊ฒฝ ๊ถŒํ•œ์„ ๊ฐ€์ง„๋‹ค. {- A quick note for the over-eager refactorers out there: This is (somewhat) intentionally ugly. It doesn't use the State monad to hold the DB because it hasn't been introduced yet. Perhaps we could use this as an example of How Monads Improve Things? -} module PassDB where import qualified Data.Map as M import System.Exit type User Name = String type Password = String type PassDB = M.Map User Name Password -- PassBD is a map from usernames to passwords -- | Ask the user for a user name and new password, and return the new PassDB changePass :: PassDB -> IO PassDB changePass db = do putStrLn "Enter a user name and new password to change." putStr "User name: " un <- getLine putStrLn "New password: " pw <- getLine if un `M.member` db -- if un is one of the keys of the map then return $ M.insert un pw db -- then update the value with the new password else do putStrLn $ "Can't find user name '" ++ un ++ "' in the database." return db -- | Ask the user for a user name, whose password will be displayed. viewPass :: PassDB -> IO () viewPass db = do putStrLn "Enter a user name, whose password will be displayed." putStr "User name: " un <- getLine putStrLn $ case M.lookup un db of Nothing -> "Can't find user name '" ++ un ++ "' in the database." Just pw -> pw -- | The main loop for interacting with the user. mainLoop :: PassDB -> IO PassDB mainLoop db = do putStr "Command [cvq]: " c <- getChar putStr "\n" -- See what they want us to do. If they chose a command other than 'q', then -- recurse (i.e. ask the user for the next command). We use the Maybe datatype -- to indicate whether to recurse or not: 'Just db' means do recurse, and in -- running the command, the old datbase changed to db. 'Nothing' means don't -- recurse. db' <- case c of 'c' -> fmap Just $ changePass db 'v' -> do viewPass db; return (Just db) 'q' -> return Nothing _ -> do putStrLn $ "Not a recognised command, '" ++ [c] ++ "'." return (Just db) maybe (return db) mainLoop db' -- | Parse the file we've just read in, by converting it to a list of lines, -- then folding down this list, starting with an empty map and adding the -- user name and password for each line at each stage. parseMap :: String -> PassDB parseMap = foldr parseLine M.empty . lines where parseLine ln map = let [un, pw] = words ln in M.insert un pw map -- | Convert our database to the format we store in the file by first converting -- it to a list of pairs, then mapping over this list to put a space between -- the user name and password showMap :: PassDB -> String showMap = unlines . map (\(un, pw) -> un ++ " " ++ pw) . M.toAscList main :: IO () main = do putStrLn $ "Welcome to PassDB. Enter a command: (c) hange a password, " ++ "(v) iew a password or (q) uit." dbFile <- readFile "passdb" db' <- mainLoop (parseMap dbFile) writeFile "passdb" (showMap db') Data.Map์˜ ๊ฒฝ์šฐ ๊ทธ ๊ตฌํ˜„์€ size balanced binary tree์— ๊ธฐ๋ฐ˜ํ•œ๋‹ค. โ†ฉ http://hackage.haskell.org/package/containers-0.6.0.1/docs/Data-Map.html โ†ฉ 2 ์ผ๋ฐ˜์ ์ธ ์ž‘์—… ์ผ๋ฐ˜์ ์ธ ์ž‘์—… ๋””๋ฒ„๊น… ํ…Œ์ŠคํŒ… ์†Œํ”„ํŠธ์›จ์–ด ํŒจํ‚ค์ง• (Cabal) Foreign Function Interface(FFI) ํ™œ์šฉํ•˜๊ธฐ ์ œ๋„ค๋ฆญ ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ์ •ํ˜•ํ™”๋œ ์ฝ”๋“œ๋Š” ๊ทธ๋งŒ 1 ๋””๋ฒ„๊น… ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Debugging Debug.Trace๋ฅผ ์ด์šฉํ•œ ๋””๋ฒ„๊ทธ ์ถœ๋ ฅ ์ถ”๊ฐ€ ์กฐ์–ธ ์œ ์šฉํ•œ ๊ด€์šฉ๊ตฌ GHCi๋ฅผ ์ด์šฉํ•œ ์ ์ง„์  ๊ฐœ๋ฐœ Hat์„ ์ด์šฉํ•œ ๋””๋ฒ„๊น… ์ผ๋ฐ˜์ ์ธ ํŒ Debug.Trace๋ฅผ ์ด์šฉํ•œ ๋””๋ฒ„๊ทธ ์ถœ๋ ฅ ๋””๋ฒ„๊ทธ ์ถœ๋ ฅ์€ ํ”„๋กœ๊ทธ๋žจ์„ ๋””๋ฒ„๊น…ํ•˜๋Š” ํ”ํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ๋ช…๋ นํ˜• ์–ธ์–ด์—์„œ๋Š” ์ฝ”๋“œ ๊ตฐ๋ฐ๊ตฐ๋ฐ ํ”„๋ฆฐํŠธ๋ฌธ์„ ๋ผ์›Œ ๋„ฃ์–ด ํ‘œ์ค€ ์ถœ๋ ฅ ๋˜๋Š” ๋กœ๊ทธ ํŒŒ์ผ์— ์ถœ๋ ฅํ•˜์—ฌ ๋””๋ฒ„๊ทธ ์ •๋ณด(์˜ˆ๋ฅผ ๋“ค๋ฉด ํŠน์ • ๋ณ€์ˆ˜์˜ ๊ฐ’์ด๋‚˜ ์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์‹œ์ง€)๋ฅผ ์ถ”์ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•˜์Šค์ผˆ์—์„œ๋Š” IO ๋ชจ๋‚˜๋“œ๋ฅผ ํ†ตํ•˜์ง€ ์•Š์œผ๋ฉด ์–ด๋–ค ์ •๋ณด๋„ ์ถœ๋ ฅํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ ‡๋‹ค๊ณ  ๋‹จ์ง€ ๋””๋ฒ„๊น…์„ ์œ„ํ•ด IO ๋ชจ๋‚˜๋“œ๋ฅผ ๋„์ž…ํ•˜๊ณ  ์‹ถ์ง€๋Š” ์•Š์„ ๊ฒƒ์ด๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Debug.Trace๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด ๋ชจ๋“ˆ์€ trace๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ต์ŠคํฌํŠธํ•˜๋Š”๋ฐ, trace๋Š” ํ”„๋กœ๊ทธ๋žจ ์–ด๋””์—์„œ๋“  ๋””๋ฒ„๊ทธ ์ถœ๋ ฅ๋ฌธ์„ ๋ถ™์ผ ์ˆ˜ ์žˆ๋Š” ํŽธ๋ฆฌํ•œ ์ˆ˜๋‹จ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด ํ”„๋กœ๊ทธ๋žจ์€ fib์— ์ „๋‹ฌํ•œ 0์ด๋‚˜ 1์ด ์•„๋‹Œ ๋ชจ๋“  ์ธ์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. module Main where import Debug.Trace fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib n = trace ("n: " ++ show n) $ fib (n - 1) + fib (n - 2) main = putStrLn $ "fib 4: " ++ show (fib 4) ๋‹ค์Œ์€ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋‹ค. n: 4 n: 3 n: 2 n: 2 fib 4: 3 trace๋กœ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ์ถ”์ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ฆ‰ ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ๋จผ์ € ํ˜ธ์ถœ๋˜๊ณ  ๊ทธ๋‹ค์Œ์€ ๋ฌด์—‡์ด ํ˜ธ์ถœ๋˜๋Š”์ง€ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด ๊ด€์‹ฌ ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์— ์ถ”์  ํ‘œ๋ฅผ ๋‹ฌ์•„๋†“๋Š”๋‹ค. module Main where import Debug.Trace factorial :: Int -> Int factorial n | n == 0 = trace ("branch 1") 1 | otherwise = trace ("branch 2") $ n * (factorial $ n - 1) main = do putStrLn $ "factorial 6: " ++ show (factorial 6) ์ด ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด ๊ผฌ๋ฆฌํ‘œ๋“ค์ด ์‹คํ–‰๋˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋””๋ฒ„๊ทธ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ทธ ์ถœ๋ ฅ์€ ๋น ํŠธ๋ฆฐ ๊ตฌ๋ฌธ ๊ฐ™์€ ๊ฒƒ์— ์˜ํ•ด ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋ฐ ์œ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ถ”๊ฐ€ ์กฐ์–ธ ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ trace๋Š” IO ๋ชจ๋‚˜๋“œ ๋ฐ–์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋ฅผ ๋ณด๋ฉด trace :: String -> a -> a ์ด๊ฒƒ์ด ์ˆœ์ˆ˜ ํ•จ์ˆ˜์ž„์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ถ„๋ช…ํžˆ trace๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋ฉฐ IO ์ž‘์—…์„ ํ•œ๋‹ค. ์ด๊ฒŒ ๋ฌด์Šจ ์ผ์ผ๊นŒ? ์‚ฌ์‹ค trace๋Š” IO์™€ ์ˆœ์ˆ˜ ํ•˜์Šค์ผˆ์˜ ๊ฒฉ๋ฆฌ๋ฅผ ํšŒํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์ง€์ €๋ถ„ํ•œ ๊ธฐ๊ต๋ฅผ ๋ถ€๋ฆฐ๋‹ค. ์ด๋Š” trace์˜ ๋ฌธ์„œ์—๋„ ๋ช…์‹œ๋˜์–ด ์žˆ๋‹ค. trace ํ•จ์ˆ˜๋Š” ์˜ค์ง ๋””๋ฒ„๊น… ๋˜๋Š” ์‹คํ–‰์˜ ๋ชจ๋‹ˆํ„ฐ๋ง์„ ์œ„ํ•ด์„œ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์ด ์—†๋‹ค. ๊ทธ ํƒ€์ž…์€ ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด์ง€๋งŒ ์ถ”์  ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๋ฅผ ๊ฐ€์ง„๋‹ค. trace๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ”ํ•œ ์‹ค์ˆ˜๋Š” ๋””๋ฒ„๊ทธ ์ถœ๋ ฅ์„ ๊ธฐ์กด ํ•จ์ˆ˜์— ๋ผ์›Œ ๋„ฃ๋‹ค๊ฐ€ trace๊ฐ€ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์‹œ์ง€ ์•ˆ์— ๊ฐ’ ํ‰๊ฐ€๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. let foo = trace ("foo = " ++ show foo) $ bar in baz ์ด๋Š” ๋ฌดํ•œ ์žฌ๊ท€๋ฅผ ์ผ์œผํ‚จ๋‹ค. trace ๋ฉ”์‹œ์ง€๊ฐ€ bar ํ‘œํ˜„์‹๋ณด๋‹ค ๋จผ์ € ํ‰๊ฐ€๋˜๋Š”๋ฐ ๊ทธ๋Ÿฌ๋ฉด foo์™€ bar์˜ ํ‰๊ฐ€๋ฅผ ์œ ๋ฐœํ•˜๊ณ  ๋‹ค์‹œ trace ๋ฉ”์‹œ์ง€์˜ ํ‰๊ฐ€๋ฅผ ์œ ๋ฐœํ•˜๋Š” ๊ฒŒ ๋˜ํ’€์ด๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” show foo ๋Œ€์‹  show bar๋ฅผ ์จ์•ผ ํ•œ๋‹ค. let foo = trace ("foo = " ++ show bar) $ bar in baz ์œ ์šฉํ•œ ๊ด€์šฉ๊ตฌ show๋ฅผ ํฌํ•จํ•˜๋Š” ํ—ฌํผ ํ•จ์ˆ˜๊ฐ€ ํŽธ๋ฆฌํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. traceThis :: (Show a) => a -> a traceThis x = trace (show x) x ๋น„์Šทํ•œ ๊ฒฐ์—์„œ Debug.Trace๋Š” traceShow ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ, ์ด๊ฒƒ์€ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋ฅผ "์ถœ๋ ฅ" ํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ํ‰๊ฐ€ํ•œ๋‹ค. traceShow :: (Show a) => a -> b -> b traceShow = trace . show ๋งˆ์ง€๋ง‰์œผ๋กœ debug ํ•จ์ˆ˜๋„ ์œ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. debug = flip trace debug๋ฅผ ์ด์šฉํ•˜๋ฉด ์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. main = (1 + 2) `debug` "adding" ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋””๋ฒ„๊น… ๊ตฌ๋ฌธ์˜ ์ฃผ์„ ์ฒ˜๋ฆฌ๊ฐ€ ์‰ฌ์›Œ์ง„๋‹ค. GHCi๋ฅผ ์ด์šฉํ•œ ์ ์ง„์  ๊ฐœ๋ฐœ ์›๋ฌธ ์—†์Œ Hat์„ ์ด์šฉํ•œ ๋””๋ฒ„๊น… ์›๋ฌธ ์—†์Œ ์ผ๋ฐ˜์ ์ธ ํŒ ์›๋ฌธ ์—†์Œ 2 ํ…Œ์ŠคํŒ… ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Testing Quickcheck ์ˆœ์ˆ˜์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ QuickCheck๋ฅผ ์ด์šฉํ•œ ํ…Œ์ŠคํŒ… take5์˜ ํ…Œ์ŠคํŒ… ๋˜ ๋‹ค๋ฅธ ์†์„ฑ ์ปค๋ฒ„๋ฆฌ์ง€ QuickCheck์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์ •๋ณด HUnit Quickcheck ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด์ž. getList = find 5 where find 0 = return [] find n = do ch <- getChar if ch `elem` ['a'..'e'] then do tl <- find (n-1) return (ch : tl) else find n ํ•˜์Šค์ผˆ์—์„œ ์ด ํ•จ์ˆ˜๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ผ๊นŒ? ๋ฆฌํŒฉํ† ๋ง๊ณผ QuickCheck๋ฅผ ํ™œ์šฉํ•ด ๋ณด์ž. ์ˆœ์ˆ˜์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ getList ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์–ด๋ ค์šด ์ด์œ ๋Š” getChar๊ฐ€ IO๋ฅผ ์ˆ˜ํ–‰ํ•ด์„œ ์ƒํ™ฉ์„ ๊ฒ€์ฆํ•  ๋‚ด๋ถ€์ ์ธ ์ˆ˜๋‹จ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. do ๋ธ”๋ก ์•ˆ์˜ ๋‹ค๋ฅธ ๊ตฌ๋ฌธ๋“ค์ด ๋ชจ๋‘ IO๋กœ ๊ฐ์‹ธ์ ธ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ ํ’€์–ดํ—ค์ณ์„œ ์ตœ์†Œํ•œ QuickCheck๋ฅผ ํ†ตํ•ด ์ฐธ์กฐ ํˆฌ๋ช…์„ฑ์ด๋ผ๋„ ํ…Œ์ŠคํŠธํ•ด ๋ณด์ž. ๋‹ฌ๊ฐ‘์ง€ ์•Š์€ ๋กœ์šฐ ๋ ˆ๋ฒจ IO ์ฒ˜๋ฆฌ๋ฅผ ๋ชจ๋‘ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๊ฒŒ์œผ๋ฅธ IO์˜ ์ด์ ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฒซ ๋‹จ๊ณ„๋Š” ํ•จ์ˆ˜์˜ IO ๋ถ€๋ถ„์„ ์–‡์€ "์Šคํ‚จ" ๋ ˆ์ด์–ด๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. -- A thin monadic skin layer getList :: IO [Char] getList = fmap take5 getContents -- The actual worker take5 :: [Char] -> [Char] take5 = take 5. filter (`elem` ['a'..'e']) QuickCheck๋ฅผ ์ด์šฉํ•œ ํ…Œ์ŠคํŒ… ์ด์ œ ์šฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ•ต์‹ฌ๋ถ€์ธ take5 ํ•จ์ˆ˜๋ฅผ ๋ณ„๋„ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋‹ค. QuickCheck๋ฅผ ํ™œ์šฉํ•ด ๋ณด์ž. ๋จผ์ € Char ํƒ€์ž…์˜ Arbitrary ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ด ์ธ์Šคํ„ด์Šค๋Š” ํ…Œ์ŠคํŠธ์— ์“ธ ์ž„์˜ Char์˜ ์ƒ์„ฑ์„ ๋‹ด๋‹นํ•œ๋‹ค. ๋‹จ์ˆœํ•จ์„ ์œ„ํ•ด ๊ทธ ๋ฒ”์œ„๋ฅผ ์ ์ ˆํ•œ ๋ฌธ์ž๋“ค๋กœ ํ•œ์ •ํ•˜์ž. import Data.Char import Test.QuickCheck instance Arbitrary Char where arbitrary = choose ('\32', '\128') coarbitrary c = variant (ord c `rem` 4) GHCi๋ฅผ ์ผœ๊ณ  ์ผ๋ฐ˜์ ์ธ ์†์„ฑ ๋ช‡ ๊ฐœ๋ฅผ ์‹คํ—˜ํ•ด ๋ณด์ž. (ํ•˜์Šค ์ผˆ REPL์—์„œ QuickCheck ํ…Œ์ŠคํŒ… ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋ฐ”๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.) ์‰ฌ์šด ๊ฒƒ๋ถ€ํ„ฐ ํ•ด๋ณด๋ฉด, [Char]๋Š” ๊ทธ ์ž์‹ ๊ณผ ๋™์ผํ•˜๋‹ค. *A> quickCheck ((\s -> s == s) :: [Char] -> Bool) OK, passed 100 tests. ๋ฐฉ๊ธˆ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚œ ๊ฑธ๊นŒ? QuickCheck๋Š” ๋ฌด์ž‘์œ„ [Char] ๊ฐ’์„ 100๊ฐœ ์ƒ์„ฑํ•˜๊ณ  ์šฐ๋ฆฌ๊ฐ€ ์ง€์ •ํ•œ ์†์„ฑ์„ ์ ์šฉํ•˜์—ฌ ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ๊ฒฐ๊ณผ๊ฐ€ True ์ž„์„ ํ™•์ธํ–ˆ๋‹ค. QuickCheck๊ฐ€ ํ…Œ์ŠคํŠธ ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•ด ์ค€ ๊ฒƒ์ด๋‹ค. ์ด์ œ ๋” ํฅ๋ฏธ๋กœ์šด ์†์„ฑ์„ ์‹œ๋„ํ•ด ๋ณด์ž. ๋‘ ๋ฒˆ์˜ ๋ฐ˜์ „์€ ์›๋ณธ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. *A> quickCheck ((\s -> (reverse.reverse) s == s) :: [Char] -> Bool) OK, passed 100 tests. ํ›Œ๋ฅญํ•˜๋‹ค. take5์˜ ํ…Œ์ŠคํŒ… QuickCheck๋ฅผ ์ด์šฉํ•œ ํ…Œ์ŠคํŒ…์˜ ์ฒซ ๋‹จ๊ณ„๋Š” ํ•จ์ˆ˜์˜ ๋ชจ๋“  ์ž…๋ ฅ์— ๋Œ€ํ•ด ์ฐธ์ธ ์†์„ฑ์„ ์ค€๋น„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰ ๋ถˆ๋ณ€์‹์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค. ๊ฐ„๋‹จํ•œ ๋ถˆ๋ณ€์‹์œผ๋กœ s l n t ( a e s ) 5 ๊ฐ™์€ ๊ฒƒ์ด ์žˆ๋‹ค. ์ด๊ฑธ QuickCheck ์†์„ฑ์œผ๋กœ ์ž‘์„ฑํ•ด ๋ณด์ž. \s -> length (take5 s) == 5 ์ด์ œ QuickCheck์—์„œ ์‹คํ–‰ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. *A> quickCheck (\s -> length (take5 s) == 5) Falsifiable, after 0 tests: "" ์ด๋Ÿฐ, QuickCheck๊ฐ€ ๋ฌธ์ œ์ ์„ ์žก์•„๋ƒˆ๋‹ค. ์ž…๋ ฅ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 5๋ณด๋‹ค ์ž‘์œผ๋ฉด ํ•„ํ„ฐ๋ง ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋„ 5 ์ด์ƒ์ผ ์ˆ˜ ์—†๋‹ค. ์†์„ฑ์„ ์กฐ๊ธˆ ๋„์ฐํ•˜๊ฒŒ ๋งŒ๋“ค์–ด๋ณด์ž. s l n t ( a e s ) 5 ์ฆ‰ take5๋Š” ๊ธธ์ด๊ฐ€ ์ตœ๋Œ€ 5์ธ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์‹คํ—˜ํ•ด ๋ณด์ž. *A> quickCheck (\s -> length (take5 s) <= 5) OK, passed 100 tests. ํ›Œ๋ฅญํ•˜๋‹ค. ๋˜ ๋‹ค๋ฅธ ์†์„ฑ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž๋“ค์ด ๋ฐ˜ํ™˜๋˜๋Š”์ง€๋„ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ๋ฐ˜ํ™˜๋˜๋Š” ๋ชจ๋“  ๋ฌธ์ž๋Š” ์ง‘ํ•ฉ ['a', 'b', 'c', 'd', 'e']์˜ ๊ตฌ์„ฑ์›์ด๋‹ค. ์ด๋ ‡๊ฒŒ ๊ธฐ์ˆ ํ•  ์ˆ˜ ์žˆ๋‹ค. s e ( โˆˆ a e s ) ( โˆˆ, , , , ) ๊ทธ๋ฆฌ๊ณ  QuickCheck์—์„œ๋Š” *A> quickCheck (\s -> all (`elem` ['a'..'e']) (take5 s)) OK, passed 100 tests. ๋ฉ‹์ง€๋‹ค. ์ด์ œ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ๋„ˆ๋ฌด ๊ธธ๊ฑฐ๋‚˜ ์ž˜๋ชป๋œ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ™•์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ปค๋ฒ„๋ฆฌ์ง€ [Char]๋ฅผ ํ…Œ์ŠคํŠธํ•  ๋•Œ QuickCheck์˜ ๊ธฐ๋ณธ ๊ตฌ์„ฑ์˜ ๋ฌธ์ œ์ ์€ ํ…Œ์ŠคํŠธ 100๊ฐœ๊ฐ€ ์šฐ๋ฆฌ์˜ ์ƒํ™ฉ์—๋Š” ์ถฉ๋ถ„ํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์‚ฌ์‹ค ์šฐ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•œ Arbitrary ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด QuickCheck๋Š” ๊ธธ์ด๊ฐ€ 5๋ณด๋‹ค ํฐ String ๊ฐ์ฒด๋ฅผ ์ ˆ๋Œ€๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ง์ ‘ ํ™•์ธํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. *A> quickCheck (\s -> length (take5 s) < 5) OK, passed 100 tests. ์šฐ๋ฆฌ์—๊ฒŒ ์ •๋ง๋กœ ํ•„์š”ํ•œ ๊ฒƒ์€ ๋” ๊ธด ๋ฌธ์ž์—ด์ด์ง€๋งŒ QuickCheck๋Š” ์—ฌ๋Ÿฌ Char๋“ค์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์„ ๋‚ญ๋น„ํ•œ๋‹ค. ์ด์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์€ QuickCheck์˜ ๊ธฐ๋ณธ ๊ตฌ์„ฑ์„ ๋ฐ”๊ฟ”์„œ ๋” ๊นŠ๊ฒŒ ํ…Œ์ŠคํŠธํ•˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. deepCheck p = check (defaultConfig { configMaxTest = 10000}) p ์ด๋Ÿฌ๋ฉด ์‹œ์Šคํ…œ์ด ์ด์ƒ ๋ฌด๋ผ๊ณ  ๊ฒฐ๋ก ๋‚ด๊ธฐ ์ „์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ 10000๊ฐœ ์ฐพ๋„๋ก ๋งŒ๋“ ๋‹ค. ๋” ๊ธด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž. *A> deepCheck (\s -> length (take5 s) < 5) Falsifiable, after 125 tests: ";:iD^*NNi~Y\\RegMob\DEL@krsx/=dcf7kub|EQi\DELD*" QuickCheck๊ฐ€ ์ƒ์„ฑํ•˜๋Š” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” verboseCheck ํ›…์„ ์ด์šฉํ•ด์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ์ •์ˆ˜ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์‹คํ—˜ํ•ด ๋ณธ ๊ฒƒ์ด๋‹ค. *A> verboseCheck (\s -> length s < 5) 0: [] 1: [0] 2: [] 3: [] 4: [] 5: [1,2,1,1] 6: [2] 7: [-2,4, -4,0,0] Falsifiable, after 7 tests: [-2,4, -4,0,0] QuickCheck์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์ •๋ณด http://haskell.org/haskellwiki/Introduction_to_QuickCheck http://haskell.org/haskellwiki/QuickCheck_as_a_test_set_generator HUnit ๊ฐ€๋”์€ ์ผ๋ฐ˜์ ์ธ ๊ทœ์น™์„ ์ •์˜ํ•˜๊ธฐ๋ณด๋‹ค๋Š” ํ…Œ์ŠคํŠธ์˜ ์˜ˆ์‹œ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒŒ ์‰ฌ์šธ ๋•Œ๊ฐ€ ์žˆ๋‹ค. HUnit์€ ๊ทธ๋Ÿฐ ์ผ์„ ๋„์™€์ฃผ๋Š” ์œ ๋‹› ํ…Œ์ŠคํŒ… ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. QuickCheck์— ์ผ๋ฐ˜์ ์ธ ๊ทœ์น™์„ ๋„˜๊ฒจ์„œ ๊ทธ๋Ÿฐ ์˜ˆ์‹œ๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” HUnit์„ ์“ฐ๋ฉด ํ•  ์ผ์ด ๋” ์ ์„ ๊ฒƒ์ด๋‹ค. TODO: HUnit ํ…Œ์ŠคํŠธ ์˜ˆ์‹œ, ์งง์€ ์†Œ๊ฐœ HUnit์˜ ์„ธ๋ถ€์‚ฌํ•ญ์€ ๊ทธ ์‚ฌ์šฉ์ž ๊ฐ€์ด๋“œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 3 ์†Œํ”„ํŠธ์›จ์–ด ํŒจํ‚ค์ง• (Cabal) (๊ฒ€ํ†  ์ค‘) ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Packaging TODO ์‹ค์ œ๋กœ ๋”ฐ๋ผ ํ•ด๋ณด๊ณ  ๋˜๋Š”์ง€ ํ™•์ธ ๊นจ์ง„ ๋งํฌ ์ฒ˜๋ฆฌ ์ถ”์ฒœ ๋„๊ตฌ ๋ฒ„์ „ ๊ด€๋ฆฌ (Revision control) ๋นŒ๋“œ ์‹œ์Šคํ…œ ๋ฌธ์„œํ™” ํ…Œ์ŠคํŒ… ๊ฐ„๋‹จํ•œ ํ”„๋กœ์ ํŠธ์˜ ๊ตฌ์กฐ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ ํ•˜์Šค ์ผˆ ์†Œ์Šค ์ž‘์„ฑ darcs์— ๋„ฃ๊ธฐ ๋นŒ๋“œ ์‹œ์Šคํ…œ ์ถ”๊ฐ€ ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ํ•˜๊ธฐ ์‹คํ–‰ haddock ๋ฌธ์„œ ๋นŒ๋“œ ํ•˜๊ธฐ ์ž๋™ํ™”๋œ ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€: QuickCheck darcs์—์„œ test suite ์‹คํ–‰ ์•ˆ์ • ๋ฒ„์ „ ํƒœ๊ทธ ํ•˜๊ธฐ, tarball ๋งŒ๋“ค๊ธฐ, ํŒ๋งคํ•˜๊ธฐ! ๊ณ ๊ธ‰ Darcs ๊ธฐ๋Šฅ: lazy get ๋ฐฐํฌ darcs๋ฅผ ์ด์šฉํ•œ tarball Cabal์„ ์ด์šฉํ•œ tarball ์š”์•ฝ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ณ„์ธต์  ์†Œ์Šค ์ฝ”๋“œ Cabal ํŒŒ์ผ ๋” ๋ณต์žกํ•œ ๋นŒ๋“œ ์‹œ์Šคํ…œ ๋‚ด๋ถ€ ๋ชจ๋“ˆ ์ž๋™ํ™” cabal init mkcabal ๋ผ์ด์„ ์Šค ๋ฐฐํฌ ํ˜ธ์ŠคํŒ… ์˜ˆ์ œ ๋…ธํŠธ ์ƒˆ ํ•˜์Šค ์ผˆ ํ”„๋กœ์ ํŠธ ๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ œ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์„ ์˜ ์‹ค์ฒœ ์ง€์นจ. ์ถ”์ฒœ ๋„๊ตฌ ๊ฑฐ์˜ ๋ชจ๋“  ํ•˜์Šค ์ผˆ ํ”„๋กœ์ ํŠธ๋Š” ๋‹ค์Œ ๋„๊ตฌ๋“ค์„ ํ™œ์šฉํ•œ๋‹ค. ๊ฐ์ž ๋‚˜๋ฆ„์˜ ์“ธ๋ชจ๊ฐ€ ์žˆ์ง€๋งŒ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ์ด์˜ ์ƒ์‚ฐ์„ฑ์ด ์ฆ๊ฐ€ํ•˜๊ณ  ์—ฌ๋Ÿฌ๋ถ„์ด ํŒจ์น˜๋ฅผ ๋ฐ›์„ ๊ฐ€๋Šฅ์„ฑ๋„ ๋†’์•„์ง„๋‹ค. ๋ฒ„์ „ ๊ด€๋ฆฌ (Revision control) ํŠน๋ณ„ํ•œ ์ด์œ ๊ฐ€ ์—†์œผ๋ฉด darcs๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. ์•„๋‹ˆ๋ฉด git๋„ ์žˆ๋‹ค. git์„ ์‹ซ์–ดํ•œ๋‹ค๋ฉด darcs๋ฅผ ์‚ดํŽด๋ณผ ๊ฒƒ. darcs๋Š” ํ•˜์Šค ์ผˆ๋กœ ์ž‘์„ฑ๋˜์—ˆ๊ณ  ๋งŽ์€ ํ•˜์Šค ์ผˆ ๊ฐœ๋ฐœ์ž๊ฐ€ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. ์ž…๋ฌธ์šฉ์œผ๋กœ๋Š” ์œ„ํ‚ค ๋ถ darcs ์ดํ•ดํ•˜๊ธฐ๋ฅผ ๋ณผ ๊ฒƒ. ๋นŒ๋“œ ์‹œ์Šคํ…œ Cabal์„ ์‚ฌ์šฉํ•˜๋ผ. ์ ์–ด๋„ Cabal User's Guide์˜ 2์ ˆ ์ฒซ ๋ถ€๋ถ„์€ ์ฝ์–ด์•ผ ํ•œ๋‹ค. ๋ฌธ์„œํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” Haddock์„ ์‚ฌ์šฉํ•˜๋ผ. ์ตœ์‹  ๋ฒ„์ „์„ ์“ธ ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. (2010๋…„ 12์›” ๊ธฐ์ค€์œผ๋กœ 2.8 ์ด์ƒ) ํ…Œ์ŠคํŒ… ์ˆœ์ˆ˜ ์ฝ”๋“œ๋Š” QuickCheck ๋˜๋Š” SmallCheck๋กœ, ๋น„์ˆœ์ˆ˜ ์ฝ”๋“œ๋Š” HUnit๋กœ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ„๋‹จํ•œ ํ”„๋กœ์ ํŠธ์˜ ๊ตฌ์กฐ ์ตœ์†Œํ•œ์˜ ํ•˜์Šค ์ผˆ ํ”„๋กœ์ ํŠธ์ธ HNop์—์„œ ์ƒˆ ํ•˜์Šค ์ผˆ ํ”„๋กœ์ ํŠธ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ฅผ ๋”ฐ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. HNop์€ ๋นˆ ํ”„๋กœ์ ํŠธ "haq"๋ฅผ ํฌํ•จ ๋‹ค์Œ ํŒŒ์ผ๋“ค๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ์˜ฎ๊ธด์ด: HNop ์›๋ณธ ๋งํฌ๊ฐ€ ๊นจ์ ธ์„œ hackage ๋งํฌ๋กœ ๋Œ€์ฒดํ•˜์˜€์ง€๋งŒ ์™„์ „ํžˆ ๊ฐ™์€ ํ”„๋กœ์ ํŠธ๋Š” ์•„๋‹™๋‹ˆ๋‹ค. Haq.hs -- ๋ฉ”์ธ ํ•˜์Šค ์ผˆ ์†Œ์Šค ํŒŒ์ผ haq.cabal -- cabal ๋นŒ๋“œ ๊ตฌ์„ฑ Setup.hs -- ๋นŒ๋“œ ์Šคํฌ๋ฆฝํŠธ _darcs ๋˜๋Š”. git -- ๋ฒ„์ „ ๊ด€๋ฆฌ README -- ์ •๋ณด LICENSE -- ๋ผ์ด์„ ์Šค ๋ฌผ๋ก  ์—ฌ๊ธฐ์— ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ๋‚˜ ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ "haq"๋ฅผ ์ œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด darcs์™€ cabal์„ ์‚ฌ์šฉํ•˜๋Š” ์ตœ์†Œํ•œ์˜ ํ•˜์Šค ์ผˆ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๊ณ , ๋นŒ๋“œํ•˜๊ณ , ์„ค์น˜ํ•˜๊ณ , ๋ฐฐํฌํ•˜๋Š” ์ ˆ์ฐจ๋‹ค. ์ปค๋งจ๋“œ ๋„๊ตฌ 'cabal init'๋Š” ์ด ๋ชจ๋“  ๊ฒƒ์„ ์ž๋™์œผ๋กœ ํ•ด์ฃผ์ง€๋งŒ ๋จผ์ € ์ด ์ ˆ์ฐจ๋ฅผ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค. ์ด์ œ ๊ฐ„๋‹จํ•œ ํ•˜์Šค ์ผˆ ์‹คํ–‰ ํ”„๋กœ๊ทธ๋žจ์„ ์œ„ํ•œ ๊ธฐ๋ฐ˜ ๊ตฌ์กฐ๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์กฐ์–ธ์€ ๊ทธ๋‹ค์Œ์— ๋‚˜์˜จ๋‹ค. ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ ์–ด๋”˜๊ฐ€์— ์†Œ์Šค ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. $ mkdir haq $ cd haq ํ•˜์Šค ์ผˆ ์†Œ์Šค ์ž‘์„ฑ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•œ๋‹ค. $ cat > Haq.hs -- -- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons -- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html) -- import System.Environment -- 'main' runs the main program main :: IO () main = getArgs >>= print . haqify . head haqify s = "Haq! " ++ s darcs์— ๋„ฃ๊ธฐ ์†Œ์Šค๋ฅผ ๋ฒ„์ „ ๊ด€๋ฆฌ์— ๋„ฃ๋Š”๋‹ค. $ darcs init $ darcs add Haq.hs $ darcs record addfile ./Haq.hs Shall I record this change? (1/?) [ynWsfqadjkc], or ? for help: y hunk ./Haq.hs 1 +-- +-- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons +-- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html) +-- +import System.Environment +-- | 'main' runs the main program +main :: IO () +main = getArgs >>= print . haqify . head +haqify s = "Haq! " ++ s Shall I record this change? (2/?) [ynWsfqadjkc], or ? for help: y What is the patch name? Import haq source Do you want to add a long comment? [yn] n Finished recording patch 'Import haq source' ์ด์ œ darcs๊ฐ€ ์ž‘๋™ํ•˜๊ณ  ์žˆ๋‹ค. $ ls Haq.hs _darcs git์˜ ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค. $ git config --global user.name "John Doe" $ git config --global user.email johndoe@example.com $ git init $ git add * $ git commit -m 'Import haq source' $ ls -A .git Haq.hs ๋นŒ๋“œ ์‹œ์Šคํ…œ ์ถ”๊ฐ€ ํ”„๋กœ์ ํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ๋นŒ๋“œ ํ• ์ง€ ์„œ์ˆ ํ•˜๋Š”. cabal ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค. $ cat > haq.cabal Name: haq Version: 0.0 Synopsis: Super cool mega lambdas Description: My super cool, indeed, even mega lambdas will demonstrate a basic project. You will marvel. License: GPL License-file: LICENSE Author: Don Stewart Maintainer: Don Stewart <dons@cse.unsw.edu.au> Build-Depends: base Executable: haq Main-is: Haq.hs ์—ฌ๋Ÿฌ๋ถ„์˜ ํŒจํ‚ค์ง€๊ฐ€ (array ๋“ฑ) ๋‹ค๋ฅธ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Build-Dependes: ํ•„๋“œ์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. ์‹ค์ œ๋กœ ๋นŒ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•  Setup.lhs๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. $ cat > Setup.lhs #! /usr/bin/env runhaskell > import Distribution.Simple > main = defaultMain cabal์€ ํฌ๋งท๋งŒ ์˜ฌ๋ฐ”๋ฅด๋ฉด Setup.hs๋„ Setup.lhs๋„ ํ—ˆ์šฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํ•ญ์ƒ #! /usr/bin/env runhaskell ๋ผ์ธ์„ ํฌํ•จํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ด ๋ผ์ธ์€ shebang ๊ด€๋ก€๋ฅผ ๋”ฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— runhaskell์„ ์ง์ ‘ ํ˜ธ์ถœํ•˜๋Š” ๋Œ€์‹  ์œ ๋‹‰์Šค ์…ธ์—์„œ Setup.hs๋ฅผ ๋ฐ”๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. (๋ฌผ๋ก  Setup ํŒŒ์ผ์ด ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ์ผ ๋•Œ๋งŒ) ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๊ธฐ๋กํ•œ๋‹ค. $ darcs add haq.cabal Setup.lhs $ darcs record --all What is the patch name? Add a build system Do you want to add a long comment? [yn] n Finished recording patch 'Add a build system' git์—์„œ๋Š”: $ git add haq.cabal Setup.lhs $ git commit -m 'Add a build system' ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ํ•˜๊ธฐ ์ด์ œ ๋นŒ๋“œ ํ•œ๋‹ค. $ runhaskell Setup.lhs configure --prefix=$HOME --user $ runhaskell Setup.lhs build $ runhaskell Setup.lhs install ์‹คํ–‰ ์ด์ œ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. $ haq me "Haq! me" ์„ค์น˜ ๋‹จ๊ณ„๋ฅผ ์ƒ๋žตํ•˜๊ณ  ๊ทธ ์ž๋ฆฌ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. $ dist/build/haq/haq you "Haq! you" haddock ๋ฌธ์„œ ๋นŒ๋“œ ํ•˜๊ธฐ dist/doc/*์— API ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. $ runhaskell Setup.lhs haddock dist/doc/์— ํŒŒ์ผ๋“ค์„ ์ƒ์„ฑํ•œ๋‹ค. $ w3m -dump dist/doc/html/haq/Main.html haq Contents Index Main Synopsis main :: IO () Documentation main :: IO () main runs the main program Produced by Haddock version 0.7 ์•„๋ฌด ์ถœ๋ ฅ๋„ ์—†๋Š”๊ฐ€? haddock์ด ์ •๋ง ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๋ผ. haddock์€ ๋ณ„๋„์˜ ํ”„๋กœ๊ทธ๋žจ์ด๋ฉฐ cabal์ฒ˜๋Ÿผ ํ•˜์Šค ์ผˆ ์ปดํŒŒ์ผ๋Ÿฌ์— ๋”ธ๋ ค์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์ž๋™ํ™”๋œ ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€: QuickCheck QuickCheck๋ฅผ ์‚ฌ์šฉํ•ด์„œ Haq.hs์˜ ์ฝ”๋“œ์˜ ์ผ๋ถ€ property๋ฅผ ํ™•์ธํ•œ๋‹ค. ํ…Œ์ŠคํŠธ ๋ชจ๋“ˆ Tests.hs๋ฅผ ๋งŒ๋“ค๊ณ  QuickCheck ๋ผˆ๋Œ€๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. $ cat > Tests.hs import Char import List import Test.QuickCheck import Text.Printf main = mapM_ (\(s, a) -> printf "%-25s: " s >> a) tests instance Arbitrary Char where arbitrary = choose ('\0', '\128') coarbitrary c = variant (ord c `rem` 4) ๊ฐ„๋‹จํ•œ property๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. $ cat >> Tests.hs -- reversing twice a finite list, is the same as identity prop_reversereverse s = (reverse . reverse) s == id s where _ = s :: [Int] -- and add this to the tests list tests = [("reverse.reverse/id", test prop_reversereverse)] ์ด ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  QuickCheck๊ฐ€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก ํ•œ๋‹ค. $ runhaskell Tests.hs reverse.reverse/id : OK, passed 100 tests. 'haqify' ํ•จ์ˆ˜์˜ ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜์ž. -- Dropping the "Haq! " string is the same as identity prop_haq s = drop (length "Haq! ") (haqify s) == id s where haqify s = "Haq! " ++ s tests = [("reverse.reverse/id", test prop_reversereverse) ,("drop.haq/id", test prop_haq)] ํ…Œ์ŠคํŠธํ•ด ๋ณธ๋‹ค. $ runhaskell Tests.hs reverse.reverse/id : OK, passed 100 tests. drop.haq/id : OK, passed 100 tests. darcs์—์„œ test suite ์‹คํ–‰ darcs๊ฐ€ ๋ชจ๋“  ์ปค๋ฐ‹์— ๋Œ€ํ•ด test suite๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. $ darcs setpref test "runhaskell Tests.hs" Changing value of test from '' to 'runhaskell Tests.hs' ์ด๋Ÿฌ๋ฉด ๋ชจ๋“  QuickCheck๋ฅผ ์‹คํ–‰ํ•  ๊ฒƒ์ด๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์˜ ํ…Œ์ŠคํŠธ๊ฐ€ ์š”๊ตฌํ•œ๋‹ค๋ฉด ๋‹ค๋ฅธ ๊ฒƒ๋“ค๋„ ๋นŒ๋“œ ๋จ์„ ๋ณด์žฅํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. (์˜ˆ: darcs setpref test "alex Tokens.x;happy Grammar.y;runhaskell Tests.hs") ์ƒˆ ํŒจ์น˜๋ฅผ ์ปค๋ฐ‹ ํ•ด๋ณด์ž. $ darcs add Tests.hs $ darcs record --all What is the patch name? Add testsuite Do you want to add a long comment? [yn] n Running test... reverse.reverse/id : OK, passed 100 tests. drop.haq/id : OK, passed 100 tests. Test ran successfully. Looks like a good patch. Finished recording patch 'Add testsuite' ํ›Œ๋ฅญํ•˜๋‹ค. ์ด์ œ ํŒจ์น˜๋Š” ์ปค๋ฐ‹ ์ „์— ํ…Œ์ŠคํŠธ suite์„ ํ†ต๊ณผํ•ด์•ผ ํ•œ๋‹ค. ์•ˆ์ • ๋ฒ„์ „ ํƒœ๊ทธ ํ•˜๊ธฐ, tarball ๋งŒ๋“ค๊ธฐ, ํŒ๋งคํ•˜๊ธฐ! ์•ˆ์ • ๋ฒ„์ „์„ ํƒœ๊ทธ ํ•œ๋‹ค. $ darcs tag What is the version name? 0.0 Finished tagging patch 'TAG 0.0' ๊ณ ๊ธ‰ Darcs ๊ธฐ๋Šฅ: lazy get ์—ฌ๋Ÿฌ๋ถ„์˜ ์ €์žฅ์†Œ์— ํŒจ์น˜๊ฐ€ ์Œ“์—ฌ๊ฐ์— ๋”ฐ๋ผ ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž๋“ค์€ ์ตœ์ดˆ darcs get์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์— ์ ์  ์งœ์ฆ์ด ๋‚  ๊ฒƒ์ด๋‹ค. (yi๋‚˜ GHC ๊ฐ™์€ ํ”„๋กœ์ ํŠธ๋Š” ํŒจ์น˜๊ฐ€ ์ˆ˜์ฒœ ๊ฐœ๋‹ค) Darcs๊ฐ€ ์ถฉ๋ถ„ํžˆ ๋น ๋ฅด๊ธด ํ•˜์ง€๋งŒ ํŒจ์น˜ ์ˆ˜์ฒœ ๊ฐœ๋ฅผ ์ผ์ผ์ด ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๊ฒƒ์€ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆฐ๋‹ค. ๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ? Darcs๋Š” darcs get์— --lazy ์˜ต์…˜์„ ์ œ๊ณตํ•œ๋‹ค. ์ด ์˜ต์…˜์€ ์ €์žฅ์†Œ์˜ ์ตœ์‹  ๋ฒ„์ „๋งŒ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋„๋ก ํ•œ๋‹ค. ํŒจ์น˜๋Š” ๋‚˜์ค‘์— ํ•„์š”ํ•  ๋•Œ ๋‹ค์šด๋กœ๋“œ๋œ๋‹ค. ๋ฐฐํฌ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์„ ๋ฐฐํฌํ•  ๋•Œ ๋Œ€๋žต ์„ธ ๊ฐ€์ง€ ์˜ต์…˜์ด ์žˆ๋‹ค. Darcs ์ €์žฅ์†Œ๋ฅผ ํ†ตํ•ด ๋ฐฐํฌ tarball์„ ํ†ตํ•ด ๋ฐฐํฌ Darcs tarball Cabal tarball Darcs ์ €์žฅ์†Œ๋ฅผ ์“ธ ๊ฒฝ์šฐ public ์ด๋ฉด ๊ทธ๊ฑธ๋กœ ๋์ด๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ๋ถ„์ด ์“ฐ๋Š” ์„œ๋ฒ„์— Darcs๊ฐ€ ์—†๊ฑฐ๋‚˜ ์ปดํ“จํ„ฐ๊ฐ€ ์‚ฌ๋žŒ๋“ค์ด darcs pull์„ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •๋˜์ง€ ์•Š์•˜์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ ์†Œ์Šค๋ฅผ tarball๋กœ ๋ฐฐํฌํ•ด์•ผ ํ•œ๋‹ค. darcs๋ฅผ ์ด์šฉํ•œ tarball darcs๋Š” ์••์ถ•๋œ tarball์„ ๋งŒ๋“œ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด tarball์€ darcs๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๋ชจ๋“  ํŒŒ์ผ์˜ ๋ณต์‚ฌ๋ณธ์„ ๋‹ด๋Š”๋‹ค. (_darcs์— ์žˆ๋Š” ๊ฒƒ๋“ค์€ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋ฆฌ๋น„์ „ ํžˆ์Šคํ† ๋ฆฌ ์—†์ด ์†Œ์Šค ํŒŒ์ผ๋งŒ ๋“ค์–ด๊ฐ„๋‹ค) $ darcs dist -d haq-0.0 Created dist as haq-0.0.tar.gz ์ด๊ฑธ๋กœ ๋์ด๋‹ค! Cabal์„ ์ด์šฉํ•œ tarball ์šฐ๋ฆฌ ์ฝ”๋“œ๊ฐ€ cabal์— ๊ธฐ๋ฐ˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Cabal์œผ๋กœ ์ง์ ‘ tarball์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. $ runhaskell Setup.lhs sdist Building source dist for haq-0.0... Source tarball created: dist/haq-0.0.tar.gz ์ด ๋ฐฉ๋ฒ•์€ Darcs๋กœ ์ƒ์„ฑํ•œ tarball์— ๋น„ํ•ด ์žฅ์ ๋„ ์žˆ๊ณ  ๋‹จ์ ๋„ ์žˆ๋‹ค. ์ฃผ๋œ ์žฅ์ ์€ Cabal์ด ์šฐ๋ฆฌ์˜ ์ €์žฅ์†Œ๋ฅผ ๊ฒ€์‚ฌํ•˜๊ณ  tarball์ด HackageDB์™€ cabal-install์ด ์š”๊ตฌํ•˜๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๋Š”์ง€ ํ™•์ธํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์ ๋„ ์žˆ๋‹ค. Cabal์€ ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ์— ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค๋งŒ ํŒจํ‚ค์ง€๋กœ ๋งŒ๋“ ๋‹ค. ์ €์žฅ์†Œ์˜ ๋‹ค๋ฅธ ํŒŒ์ผ๋“ค์ด ์–ธ์  ๊ฐ€ ํ•„์š”ํ•œ ์‹œ์ ์ด ์™€๋„, ๊ทธ ํŒŒ์ผ๋“ค์€ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. 1 ๊ธฐํƒ€ ํŒŒ์ผ๋“ค์„ ํฌํ•จํ•˜๋ ค๋ฉด (์œ„ ์˜ˆ์ œ์˜ Test.hs ๊ฐ™์€) cabal ํŒŒ์ผ์— ์ด๋Ÿฐ ์ค„์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. extra-source-files: Tests.hs AUTHORS๋‚˜ README ๊ฐ™์€ ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด ์—ญ์‹œ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค. data-files: AUTHORS, README ์š”์•ฝ ๋‹ค์Œ ํŒŒ์ผ๋“ค์ด ์ƒ์„ฑ๋˜์—ˆ๋‹ค. $ ls Haq.hs Tests.hs dist haq.cabal Setup.lhs _darcs haq-0.0.tar.gz ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•˜์Šค ์ผˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ์ž‘ํ•˜๋Š” ์ ˆ์ฐจ๋„ ๊ฑฐ์˜ ๊ฐ™๋‹ค. ๊ฐ€์ƒ์˜ "ltree" ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ์ฐจ์ด์ ์„ ๋ณด์ž. ๊ณ„์ธต์  ์†Œ์Šค ์ฝ”๋“œ ์†Œ์Šค๋Š” ๋ชจ๋“ˆ ๋ ˆ์ด์•„์›ƒ ๊ฐ€์ด๋“œ๋ฅผ ๋”ฐ๋ฅด๋Š” ๊ฒฝ๋กœ์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. Data.LTree ๋ชจ๋“ˆ์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ ๋‹ค. $ mkdir Data $ cat > Data/LTree.hs module Data.LTree where Data.LTree ๋ชจ๋“ˆ์€ Data/LTree.hs์— ์žˆ๊ฒŒ ๋œ๋‹ค. Cabal ํŒŒ์ผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์œ„ํ•œ Cabal ํŒŒ์ผ๋“ค์€ ๊ณต๊ฐœ ๋ชจ๋“ˆ๋“ค์„ ๋‚˜์—ดํ•˜๋ฉฐ ์‹คํ–‰ ํŒŒ์ผ ๋ถ€๋ถ„์€ ์—†๋‹ค. $ cat ltree.cabal Name: ltree Version: 0.1 Description: Lambda tree implementation License: BSD3 License-file: LICENSE Author: Don Stewart Maintainer: dons@cse.unsw.edu.au Build-Depends: base Exposed-modules: Data.LTree ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋นŒ๋“œ ํ•œ๋‹ค. $ runhaskell Setup.lhs configure --prefix=$HOME --user $ runhaskell Setup.lhs build Preprocessing library ltree-0.1... Building ltree-0.1... [1 of 1] Compiling Data.LTree ( Data/LTree.hs, dist/build/Data/LTree.o) /usr/bin/ar: creating dist/build/libHSltree-0.1.a ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์˜ค๋ธŒ์ ํŠธ ์•„์นด์ด๋ธŒ๋กœ์„œ ์ƒ์„ฑ๋˜์—ˆ๋‹ค. *nix ์‹œ์Šคํ…œ์—์„œ๋Š” configure ๋‹จ๊ณ„์— --user ํ”Œ๋ž˜๊ทธ๋ฅผ ๋ถ™์—ฌ์•ผ ํ•  ๊ฒƒ์ด๋‹ค. ์ด๋Š” ์„ค์น˜ ๋„์ค‘ ๋กœ์ปฌ ํŒจํ‚ค์ง€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธธ ์›ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ด์ œ ์„ค์น˜ํ•œ๋‹ค. $ runhaskell Setup.lhs install Installing: /home/dons/lib/ltree-0.1/ghc-6.6 & /home/dons/bin ltree-0.1... Registering ltree-0.1... Reading package info from ".installed-pkg-config" ... done. Saving old package config file... done. Writing new package config file... done. ์ด๊ฑธ๋กœ ๋์ด๋‹ค. ์ด์ œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ghci์—์„œ ์“ด๋‹ค๋˜๊ฐ€ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. $ ghci -package ltree Prelude> :m + Data.LTree Prelude Data.LTree> ์ƒˆ๋กœ์šด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์Šค์ฝ”ํ”„ ์•ˆ์— ์žˆ์œผ๋ฉฐ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ๋‹ค. ๋” ๋ณต์žกํ•œ ๋นŒ๋“œ ์‹œ์Šคํ…œ ํฐ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ์„œ๋ธŒ๋””๋ ‰ํ„ฐ๋ฆฌ๋“ค์— ์†Œ์Šค ํŠธ๋ฆฌ๋“ค์„ ๋‚˜๋ˆ  ๋‹ด๋Š” ๊ฒƒ์ด ์œ ์šฉํ•˜๋‹ค. ์ƒˆ ๋””๋ ‰ํ„ฐ๋ฆฌ, ๊ฐ€๋ น "src" ๊ฐ™์€ ๊ฒƒ์„ ๋งŒ๋“ค๊ณ  ์—ฌ๊ธฐ์— ์†Œ์Šค ํŠธ๋ฆฌ๋ฅผ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค. Cabal์ด ์ด ์ฝ”๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋„๋ก ํ•˜๋ ค๋ฉด Cabal ํŒŒ์ผ์— ๋‹ค์Œ ์ค„์„ ์ถ”๊ฐ€ํ•œ๋‹ค. hs-source-dirs: src Cabal์€ ์ด ์™ธ์—๋„ configure ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋“ฑ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. Cabal ๋ฌธ์„œ์—์„œ ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋‚ด๋ถ€ ๋ชจ๋“ˆ ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋…ธ์ถœ๋˜์ง€ ์•Š๋Š” ๋‚ด๋ถ€ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด other-modules ํ•„๋“œ์— ๋‚˜์—ดํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง ๊ฒƒ. other-modules: My.Own.Module ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด (GHC 6.8.3 ๊ธฐ์ค€) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์—๋Ÿฌ ์—†์ด ๋นŒ๋“œ ๋˜์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์“ธ ์ˆ˜ ์—†๋‹ค. ๋นŒ๋“œ ๋•Œ ๋ง์ปค ์—๋Ÿฌ๊ฐ€ ๋‚˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ž๋™ํ™” cabal init ํ•˜์Šค์ผˆ์˜ ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ ๋„๊ตฌ์ธ cabal-install์€ ๊ฐœ๋ฐœ์ž๋“ค์ด ๊ฐ„๋‹จํ•œ cabal ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ๋•๋Š” ์ปค๋งจ๋“œ ๋ผ์ธ ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์‹คํ–‰ํ•˜๊ณ  ๋ชจ๋“  ์งˆ๋ฌธ์— ๋Œ€๋‹ต๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. ๊ฐ๊ฐ ๊ธฐ๋ณธ๊ฐ’๋„ ์ฃผ์–ด์ง„๋‹ค. $ cabal init Package name [default "test"]? Package version [default "0.1"]? Please choose a license: ... mkcabal mkcabal์€ cabal init ์ด์ „์— ์žˆ๋˜ ๋„๊ตฌ๋กœ์„œ ์—ญ์‹œ ์ƒˆ cabal ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์„ ์ž๋™ํ™”ํ•œ๋‹ค. darcs get http://code.haskell.org/~dons/code/mkcabal ์ด ๋„๊ตฌ๋Š” ์œˆ๋„์šฐ์ฆˆ์—์„œ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์œˆ๋„์šฐ์ฆˆ ๋ฒ„์ „ GHC๋Š” ์ด ๋„๊ตฌ์— ํ•„์š”ํ•œ readline ํŒจํ‚ค์ง€๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค. ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. $ mkcabal Project name: haq What license ["GPL","LGPL","BSD3","BSD4","PublicDomain","AllRightsReserved"] ["BSD3"]: What kind of project [Executable, Library] [Executable]: Is this your name? - "Don Stewart " [Y/n]: Is this your email address? - "<dons@cse.unsw.edu.au>" [Y/n]: Created Setup.lhs and haq.cabal $ ls Haq.hs LICENSE Setup.lhs _darcs dist haq.cabal 'haq' ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•œ ์Šคํ… Cabal ํŒŒ์ผ๋“ค์„ ์ฑ„์›Œ ๋„ฃ๋Š”๋‹ค. ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ ํŠธ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด $ mkcabal --init-project Project name: haq What license ["GPL","LGPL","BSD3","BSD4","PublicDomain","AllRightsReserved"] ["BSD3"]: What kind of project [Executable, Library] [Executable]: Is this your name? - "Don Stewart " [Y/n]: Is this your email address? - "<dons@cse.unsw.edu.au>" [Y/n]: Created new project directory: haq $ cd haq $ ls Haq.hs LICENSE README Setup.lhs haq.cabal ๋ผ์ด์„ ์Šค base ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒจํ‚ค์ง€์˜ ์ฝ”๋“œ๋Š” BSD ๋ผ์ด์„ ์Šค ๋˜๋Š” ๋ณด๋‹ค ์ž์œ ๋กญ๊ณ  ๊ฐœ๋ฐฉ๋œ ๊ฒƒ์ด์–ด์•ผ ํ•œ๋‹ค. ๊ทธ ์™ธ์—๋Š” ๋ชจ๋‘ ๋‹น์‹ , ์ €์ž‘์ž์—๊ฒŒ ๋‹ฌ๋ ค์žˆ๋‹ค. ๋ผ์ด์„ ์Šค๋ฅผ ์„ ํƒํ•˜๋ผ. ์—ฌ๋Ÿฌ๋ถ„์ด ์‚ฌ์šฉํ•˜๋Š” ํ•˜์Šค ์ผˆ ํŒจํ‚ค์ง€์™€ C ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ผ์ด์„ ์Šค๋ฅผ ํ™•์ธํ•˜๋ผ. ์ด๊ฒƒ๋“ค์ด ์—ฌ๋Ÿฌ๋ถ„์˜ ์„ ํƒ์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐ๊ด€๋œ ํ”„๋กœ์ ํŠธ๋“ค๊ณผ ์ตœ๋Œ€ํ•œ ๋น„์Šทํ•œ ๋ผ์ด์„ ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. ํ•˜์Šค ์ผˆ ์ปค๋ฎค๋‹ˆํ‹ฐ๋Š” ๋Œ€๋žต ๋‘<NAME>์œผ๋กœ ๋‚˜๋‰œ๋‹ค. ํ•œ์ชฝ์€ ๋ชจ๋“  ๊ฒƒ์„ BSD ๋˜๋Š” ํผ๋ธ”๋ฆญ ๋„๋ฉ”์ธ์œผ๋กœ ๋ฐฐํฌํ•˜๊ณ  ๋‹ค๋ฅธ ์ชฝ์€ GPL/LGPL ํŒŒ๋‹ค. (๋Œ€๋žต ์ž์œ  ์†Œํ”„ํŠธ์›จ์–ด ์ปค๋ฎค๋‹ˆํ‹ฐ์˜ ์นดํ”ผ๋ ˆํ”„ํŠธ/๋…ผ ์นดํ”ผ๋ ˆํ”„ํŠธ ๋ถ„๋‹จ์„ ๋ฐ˜์˜ํ•œ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค) ์–ด๋–ค ํ•˜์Šค ์ผˆ๋Ÿฌ๋“ค์€ ํŠนํžˆ LGPL์„ ํ”ผํ•  ๊ฒƒ์„ ์ถ”์ฒœํ•˜๋Š”๋ฐ, cross-module optimization ๋ฌธ์ œ ๋•Œ๋ฌธ์ด๋‹ค. ๋งŽ์€ ๋ผ์ด์„ ์Šค ์งˆ๋ฌธ๋“ค์ด ๊ทธ๋ ‡๋“ฏ์ด ์ด ์กฐ์–ธ๋„ ๋…ผ๋ž€์˜ ์—ฌ์ง€๊ฐ€ ์žˆ๋‹ค. ๋ช‡ ํ•˜์Šค ์ผˆ ํ”„๋กœ์ ํŠธ๋Š” (wxHaskell, HaXml ๋“ฑ) LGPL์„ ์“ฐ๋Š”๋ฐ, cross-module optimization ๋ฌธ์ œ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€์ ์ธ ํ—ˆ์šฉ ์กฐํ•ญ์ด ์žˆ๋‹ค. ๋ฐฐํฌ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ์•ˆ์ •์ ์ด๊ณ  ํƒœ๊น… ๋œ tarball๋กœ ๋ฐฐํฌํ•ด์•ผ ํ•œ๋‹ค. ๋ฐฐํฌํ•  ๋•Œ ๋‹จ์ˆœํžˆ darcs์— ๊ธฐ๋Œ€์ง€ ๋ง ๊ฒƒ. darcs dist๋Š” darcs ์ €์žฅ์†Œ๋กœ๋ถ€ํ„ฐ ์ง์ ‘ tarball์„ ์ƒ์„ฑํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด $ cd fps $ ls Data LICENSE README Setup.hs TODO _darcs cbits dist fps.cabal tests $ darcs dist -d fps-0.8 Created dist as fps-0.8.tar.gz ์ด๋Ÿฌ๋ฉด ์ด์ œ ๊ทธ๋ƒฅ fps-0.8.tar.gz๋ฅผ ๊ฒŒ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. post-hook์„ ๊ฑธ์–ด์„œ darcs๊ฐ€ '์ผ๋ณ„ ์Šค๋ƒ…์ƒท'์„ ๋งŒ๋“ค๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹ค์Œ์„ _darcs/prefs/defaults์— ๋„ฃ๋Š”๋‹ค. apply posthook darcs dist apply run-posthook ์กฐ์–ธ: ๊ฐ ๋ฆด๋ฆฌ์Šค๋ฅผ darcs tag๋กœ ํƒœ๊น… ํ•  ๊ฒƒ. $ darcs tag 0.8 Finished tagging patch 'TAG 0.8' ์ด๋Ÿฌ๋ฉด ์‚ฌ๋žŒ๋“ค์ด darcs get --lazy --tag 0.8์„ ์ด์šฉํ•ด ์ „์ฒด ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋ผ ์ด ํƒœ๊ทธ ๋œ ๋ฒ„์ „๋งŒ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ํ˜ธ์ŠคํŒ… ๊ณต๊ฐœ ๋˜๋Š” ๋น„๊ณต๊ฐœ Darcs ์ €์žฅ์†Œ๋ฅผ http://patch-tag.com/์— ๋ฌด๋ฃŒ๋กœ ํ˜ธ์ŠคํŠธ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜๋Š” ๋‹จ์ˆœํžˆ Darcs ์ €์žฅ์†Œ๊ฐ€ ์›น ํŽ˜์ด์ง€์—์„œ ๋ณด์ด๊ฒŒ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. http://code.haskell.org/์˜ Haskell Community Server์— ํ˜ธ์ŠคํŠธ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค. http://community.haskell.org/admin/๋ฅผ ํ†ตํ•ด ๊ณ„์ •์„ ์š”์ฒญํ•  ์ˆ˜ ์žˆ๋‹ค. https://github.com/์—์„œ Git ํ˜ธ์ŠคํŒ…์„ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ์ œ ์ƒˆ ํ•˜์Šค ์ผˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ง€๊ธˆ๊นŒ์ง€์˜ ์ ˆ์ฐจ์— ๋”ฐ๋ผ ์ž‘์„ฑํ•˜๊ณ , ํŒจํ‚ค์ง•ํ•˜๊ณ , ๋ฐฐํฌํ•˜๋Š” ์™„์ „ํ•œ ์˜ˆ์ œ(๋งํฌ ๊นจ์ง) At least part of this page was imported from the Haskell wiki article How to write a Haskell program, in accordance to its Simple Permissive License. If you wish to modify this page and if your changes will also be useful on that wiki, you might consider modifying that source page instead of this one, as changes from that page may propagate here, but not the other way around. Alternately, you can explicitly dual license your contributions under the Simple Permissive License. Note also that the original tutorial contains extra information about announcing your software and joining the Haskell community, which may be of interest to you. ๋…ธํŠธ ์ด๋Š” ์‚ฌ์‹ค ์ข‹์€ ๊ฒƒ์ด๋‹ค. tarball์—๋Š” ํฌํ•จ๋˜์ง€ ์•Š๋Š” ์ •๋ฐ€ test suite์„ ๋งŒ๋“ ๋‹ค๋˜๊ฐ€ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ ์‹ ๊ฒฝ ์“ธ ํ•„์š”๊ฐ€ ์—†๋‹ค. ์šฐ๋ฆฌ์˜ ์ฝ”๋“œ์— ์ˆจ์€ ๊ฐ€์ •๊ณผ ์ƒ๋žต์„ ๋ฐํž ์ˆ˜๋„ ์žˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์˜ ์ฝ”๋“œ๋Š” ์šฐ์—ฐํžˆ ์ƒ์„ฑํ•œ ํŒŒ์ผ์ด ์žˆ์„ ๋•Œ๋งŒ ๋นŒ๋“œ์™€ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ–ˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. โ†ฉ 4 FFI ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/FFI ํ•˜์Šค์ผˆ์—์„œ C ํ˜ธ์ถœํ•˜๊ธฐ ๋งˆ์ƒฌ๋ง (ํƒ€์ž… ๋ณ€ํ™˜) ์ˆœ์ˆ˜ C ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๊ธฐ ๋น„์ˆœ์ˆ˜ C ํ•จ์ˆ˜ C ํฌ์ธํ„ฐ ๋‹ค๋ฃจ๊ธฐ C ๊ตฌ์กฐ์ฒด ๋‹ค๋ฃจ๊ธฐ Storable ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋งŒ๋“ค๊ธฐ C ํ•จ์ˆ˜ ๋“ค์—ฌ์˜ค๊ธฐ Bessel ํ•จ์ˆ˜ ๊ตฌํ˜„ ์˜ˆ์ œ ๊ณ ๊ธ‰ ์ฃผ์ œ ์ด์šฉ ๊ฐ€๋Šฅํ•œ C ํ•จ์ˆ˜์™€ ๊ตฌ์กฐ์ฒด import์™€ inclusion ์—ด๊ฑฐํ˜• Haskell Function Target ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋ฅผ C ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์ „๋‹ฌํ•˜๊ธฐ ๋ฏธ์ง€์˜ ๊ตฌ์กฐ์ฒด ๋‹ค๋ฃจ๊ธฐ ์™„์„ฑ๋œ ํ•จ์ˆ˜ ์Šค์Šค๋กœ ํ•ด์ œํ•˜๋Š” ํฌ์ธํ„ฐ C์—์„œ ํ•˜์Šค ์ผˆ ํ˜ธ์ถœํ•˜๊ธฐ ํ•˜์Šค ์ผˆ ์†Œ์Šค C ์†Œ์Šค ํ•˜์Šค์ผˆ์„ ์“ฐ๋Š” ๊ฑด ์ข‹๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‹ค๋ฅธ ์–ธ์–ด, ํŠนํžˆ C๋กœ ์ž‘์„ฑ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์•„์ฃผ ๋งŽ๋‹ค. ์ด๋Ÿฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ๊ทธ๋ฆฌ๊ณ  C ์ฝ”๋“œ๊ฐ€ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ ค๋ฉด FFI(Foreign Function Interface)๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ํ•˜์Šค์ผˆ์—์„œ C ํ˜ธ์ถœํ•˜๊ธฐ ๋งˆ์ƒฌ๋ง (ํƒ€์ž… ๋ณ€ํ™˜) C ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๋ฉด ํ•˜์Šค ์ผˆ ํƒ€์ž…์„ ์•Œ๋งž์€ C ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. Foreign.C.Types ๋ชจ๋“ˆ์— C ํƒ€์ž…๋“ค์ด ์žˆ๋‹ค. ๋‹ค์Œ ํ‘œ์— ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๊ฐ€ ์žˆ๋‹ค. ํ•˜์Šค ์ผˆ Foreign.C.Types C Double CDouble double Char CUChar unsigned char Int CLong long int ํ•˜์Šค ์ผˆ ํƒ€์ž…์„ C ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์„ ๋งˆ์ƒฌ๋ง์ด๋ผ๊ณ  ํ•œ๋‹ค. (๋ฐ˜๋Œ€ ์ž‘์—…์€ ์–ธ๋งˆ์ƒฌ๋ง์ด๋ผ๊ณ  ํ•œ๋‹ค) ๊ธฐ๋ณธ ํƒ€์ž…๋“ค์€ ์ง๊ด€์ ์ด๋‹ค. ๋ถ€๋™์†Œ์ˆ˜์  ํƒ€์ž…์€ realToFrac์„ ์“ฐ๊ณ  (Double๊ณผ CDouble ๋ชจ๋‘ Real๊ณผ Fractional ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค), ์ •์ˆ˜๋Š” fromIntegral์„ ์“ฐ๋Š” ์‹์ด๋‹ค. 6.12.x ์ด์ „์˜ GHC๋ฅผ ์“ฐ๊ณ  ์žˆ๋‹ค๋ฉด CLDouble ํƒ€์ž…์ด long double์ด ์•„๋‹ˆ๋ผ CDouble์˜ ๋™์˜์–ด์ž„์„ ์ฃผ์˜ํ•˜๋ผ. ๊ทธ๋ƒฅ ์ ˆ๋Œ€ ์‚ฌ์šฉํ•˜์ง€ ๋ง ๊ฒƒ. C ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ long double์„ double์˜ ๋™์˜์–ด๋กœ ์ƒ๊ฐํ•˜์ง€ ์•Š๋Š” ์ด์ƒ ์กฐ์šฉํžˆ ํƒ€์ž… ์˜ค๋ฅ˜๋ฅผ ๋‚ผ ๊ฒƒ์ด๋‹ค. 6.12.x ์ด๋ž˜๋กœ CLDouble์€ ์ œ๊ฑฐ๋˜์—ˆ๊ณ , ์•Œ๋งž์€ ๊ตฌํ˜„์„ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๋‹ค. ์ˆœ์ˆ˜ C ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๊ธฐ C๋กœ ๊ตฌํ˜„๋œ ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋Š” ํ•˜์Šค์ผˆ์—์„œ ํฐ ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ค์ง€ ์•Š๋Š”๋‹ค. C ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ sin ํ•จ์ˆ˜๊ฐ€ ์ข‹์€ ์˜ˆ์‹œ๋‹ค. {-# LANGUAGE ForeignFunctionInterface #-} import Foreign import Foreign.C.Types foreign import ccall unsafe "math.h sin" c_sin :: CDouble -> CDouble ์ฒซ ์ค„์— FFI๋ฅผ ์œ„ํ•œ GHC ํ™•์žฅ์„ ๋ช…์‹œํ•œ๋‹ค. ๊ทธ๋‹ค์Œ Foreign๊ณผ Foreign.C.Types ๋ชจ๋“ˆ์„ ๋“ค์—ฌ์˜จ๋‹ค. ํ›„์ž๋Š” C์˜ ๋ฐฐ์ •๋ฐ€๋„ ๋ถ€๋™์†Œ์ˆ˜์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” CDouble์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํฌํ•จํ•œ๋‹ค. ๋‹ค์Œ์—๋Š” ์™ธ๋ถ€ ํ•จ์ˆ˜๋ฅผ ๋“ค์—ฌ์˜จ๋‹ค๊ณ  ๋ช…์‹œํ•œ๋‹ค. "์•ˆ์ „ ์ˆ˜์ค€"์€ ํ‚ค์›Œ๋“œ safe (๊ธฐ๋ณธ๊ฐ’) ๋˜๋Š” unsafe๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ unsafe๊ฐ€ ๋” ํšจ์œจ์ ์ด๊ณ  safe๋Š” ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” C ์ฝ”๋“œ์—๋งŒ ํ•„์š”ํ•˜๋‹ค. ๋งค์šฐ ํŠน์ดํ•œ ๊ฒฝ์šฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋ถ€๋ถ„์€ unsafe ํ‚ค์›Œ๋“œ๋ฅผ ์จ๋„ ์•ˆ์ „ํ•˜๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ํ—ค๋”์™€ ํ•จ์ˆ˜ ์ด๋ฆ„์„ ๋ช…์‹œํ•œ๋‹ค. ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜ ์ด๋ฆ„์œผ๋กœ c_sin์„ ์ผ์ง€๋งŒ ๋ฌด์—‡์ด๋“  ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•จ์ˆ˜ ๋ช…์„ธ๋Š” ์˜ฌ ๋ฐ”๋ผ์•ผ ํ•œ๋‹ค. GHC๋Š” ํ•จ์ˆ˜๊ฐ€ ์‹ค์ œ๋กœ CDouble์„ ๋ฐ›๋Š”์ง€, ๋ญ˜ ๋ฐ˜ํ™˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด C ํ—ค๋”๋ฅผ ๊ฒ€์‚ฌํ•˜์ง€ ์•Š์œผ๋ฉฐ, ๋ช…์„ธ๊ฐ€ ํ‹€๋ฆฌ๋ฉด ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ ๋‹ค. ์ด์ œ CDouble์„ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์˜ ๋ž˜ํผ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ‰๋ฒ”ํ•œ ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. haskellSin :: Double -> Double haskellSin = realToFrac . c_sin . realToFrac C์˜ sin๋Š” double์„ ๋ฐ›์•„ double์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋“ค์—ฌ์˜ค๊ธฐ ๊ฐ„๋‹จํ•˜๋‹ค. C์˜ ๋ณต์žกํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋งŒ์—ฐํ•œ, ๋น„์ˆœ์ˆ˜ ํ•จ์ˆ˜์™€ ํฌ์ธํ„ฐ์˜ ๊ฒฝ์šฐ ์ƒํ™ฉ์ด ๋ณต์žกํ•ด์ง„๋‹ค. ๋น„์ˆœ์ˆ˜ C ํ•จ์ˆ˜ rand๋Š” ์˜์‚ฌ ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณ ์ „์ ์ธ ๋น„์ˆœ์ˆ˜ C ํ•จ์ˆ˜๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์–ด๋–ค C ๋ฃจํ‹ด์ด ์ถœ๋ ฅํ•˜๋Š” ์˜์‚ฌ ๋‚œ์ˆ˜์—ด์„ ์ •ํ™•ํžˆ ์žฌํ˜„ํ•˜๊ณ  ์‹ถ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜์Šค์ผˆ์˜ System.Random.randomIO๋ฅผ ์›์น˜ ์•Š๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ๊ทธ๋Ÿฌ๋ฉด rand๋ฅผ ์ด์ „์˜ sin์ฒ˜๋Ÿผ ๋“ค์—ฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. {-# LANGUAGE ForeignFunctionInterface #-} import Foreign import Foreign.C.Types foreign import ccall unsafe "stdlib.h rand" c_rand :: CUInt -- Oops! ์ด ์•„๋ฌด ์ƒ๊ฐ ์—†๋Š” ๊ตฌํ˜„์„ GHCI์—์„œ ์‹œ๋„ํ•ด ๋ณด๋ฉด c_rand๊ฐ€ ํ•ญ์ƒ ๊ฐ™์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. > c_rand 1714636915 > c_rand 1714636915 ์‹ค์€ GHC์—๊ฒŒ ์ด๊ฒŒ ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋ผ๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, GHC๋Š” ํ•œ ์ˆœ์ˆ˜ ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‘ ๋ฒˆ ๊ณ„์‚ฐํ•  ์ด์œ ๊ฐ€ ์—†๋‹ค. GHC๊ฐ€ ์–ด๋–ค ์˜ค๋ฅ˜๋‚˜ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋„ ๋„์šฐ์ง€ ์•Š์€ ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. GHC์—๊ฒŒ ์ด ํ•จ์ˆ˜๊ฐ€ ๋น„์ˆœ์ˆ˜์ž„์„ ์•Œ๋ฆฌ๋ ค๋ฉด IO ๋ชจ๋‚˜๋“œ๋ฅผ ์จ์•ผ ํ•œ๋‹ค. {-# LANGUAGE ForeignFunctionInterface #-} import Foreign import Foreign.C.Types foreign import ccall unsafe "stdlib.h rand" c_rand :: IO CUInt foreign import ccall "stdlib.h srand" c_srand :: CUInt -> IO () C ์˜์‚ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊ธฐ์˜ ์‹œ๋“œ(seed)๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด srand ํ•จ์ˆ˜๋„ ๋“ค์—ฌ์™”๋‹ค. > c_rand 1957747793 > c_rand 424238335 > c_srand 0 > c_rand 1804289383 > c_srand 0 > c_rand 1804289383 C ํฌ์ธํ„ฐ ๋‹ค๋ฃจ๊ธฐ ์œ ์šฉํ•œ C ํ•จ์ˆ˜๋“ค์€ ๋Œ€๊ฐœ ์—ฌ๋Ÿฌ ์ธ์ž๋ฅผ ๋ฐ›์•„ ๋ณต์žกํ•œ ๊ณ„์‚ฐ์„ ํ•˜๊ณ  ์ œ์–ด ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰ C ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์˜ ํŒจ๋Ÿฌ๋‹ค์ž„์€ ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋“ค์„ "ํƒ€๊นƒ"์œผ๋กœ ์ง€์ •ํ•ด ์—ฌ๊ธฐ์— ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋กํ•˜๊ณ , ํ•จ์ˆ˜ ์ž์ฒด๋Š” ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค(๊ณ„์‚ฐ์ด ์„ฑ๊ณตํ–ˆ์œผ๋ฉด 0, ์•„๋‹ˆ๋ฉด ํŠน์ • ๋ฌธ์ œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž). ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ค ๊ตฌ์กฐ์ฒด์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ์ด ๊ตฌ์กฐ์ฒด๋Š” ์•„๋งˆ ๊ตฌํ˜„์— ์ •์˜๋˜์–ด ์žˆ์–ด ์šฐ๋ฆฌ๋Š” ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅํ•  ๊ฒƒ์ด๋‹ค. ๊ต์œก์ ์ธ ์˜ˆ์‹œ๋กœ ๊ณผํ•™ ๊ณ„์‚ฐ์šฉ ์ž์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ GNU Scientific Library์˜ gsl_frexp ํ•จ์ˆ˜๋ฅผ ๋ณด์ž. ๋ช…์„ธ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ C ํ•จ์ˆ˜๋‹ค. double gsl_frexp (double x, int * e) ์ด ํ•จ์ˆ˜๋Š” double x๋ฅผ ์ทจํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •๊ทœํ™”๋œ ๋ถ„์ˆ˜ f์™€ ์ •์ˆ˜<NAME> e๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. = ร— e โˆˆ, 0.5 f 1 ์ด C ํ•จ์ˆ˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜์Šค์ผˆ์— ์ด์–ด์ค€๋‹ค. {-# LANGUAGE ForeignFunctionInterface #-} import Foreign import Foreign.Ptr import Foreign.C.Types foreign import ccall unsafe "gsl/gsl_math.h gsl_frexp" gsl_frexp :: CDouble -> Ptr CInt -> IO CDouble Ptr์ด ์ƒˆ๋กญ๊ฒŒ ๋“ฑ์žฅํ–ˆ๋‹ค. Ptr์€ Storable ํด๋ž˜์Šค์˜ ์–ด๋Š ์ธ์Šคํ„ด์Šค์™€๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“  C ํƒ€์ž… ๊ทธ๋ฆฌ๊ณ  ๋ช‡๋ช‡ ํ•˜์Šค ์ผˆ ํƒ€์ž…์ด ๊ฐ€๋Šฅํ•œ ๋Œ€์ƒ์ด๋‹ค. gsl_frexp์˜ ๊ฒฐ๊ณผ๊ฐ€ IO ๋ชจ๋‚˜๋“œ ์•ˆ์— ์žˆ๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. ํฌ์ธํ„ฐ๋ฅผ ์ž…์ถœ๋ ฅ์œผ๋กœ ์“ธ ๋•Œ๋Š” ์ด๋Ÿฐ ์ผ์ด ํ”ํ•˜๋‹ค. ๋‹จ์ˆœํžˆ CDouble๋ฅผ ์“ฐ๋ฉด ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€ ๊ณง ๋ณผ ๊ฒƒ์ด๋‹ค. frexp ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆœ์ˆ˜ ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„๋œ๋‹ค. frexp :: Double -> (Double, Int) frexp x = unsafePerformIO $ alloca $ \expptr -> do f <- gsl_frexp (realToFrac x) expptr e <- peek expptr return (realToFrac f, fromIntegral e) ์˜ฎ๊ธด์ด: unsafePerformIO๋ฅผ ์“ฐ๋ ค๋ฉด System.IO.Unsafe๋ฅผ ์ž„ํฌํŠธ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ์„ธ๋ถ€์‚ฌํ•ญ์€ ์น˜์›Œ๋†“๊ณ , ์ด ํ•จ์ˆ˜๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋‹ค. ๋ช…์„ธ๊ฐ€ f์™€ e๋ฅผ IO ๋ชจ๋‚˜๋“œ ๋ฐ–์œผ๋กœ ๊บผ๋‚ด์„œ ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ด์œ ๋‹ค. ํ•˜์ง€๋งŒ f๋Š” ๋ชจ๋‚˜๋“œ ๋‚ด๋ถ€์— ์žˆ๋‹ค. f๋ฅผ IO ๋ชจ๋‚˜๋“œ์—์„œ ๊บผ๋‚ด๋ ค๋ฉด unsafePerformIO ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ์ˆœ์ˆ˜ํ•˜๋‹ค๋Š” ๊ฑธ ์•Œ ๋•Œ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ, ๊ทธ๋Ÿฌ๋ฉด GHC๊ฐ€ ์•Œ๋งž๊ฒŒ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ํฌ์ธํ„ฐ๋ฅผ ํ• ๋‹นํ•˜๋ ค๋ฉด alloca ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•  ์ฑ…์ž„๋„ ์ง„๋‹ค. alloca๋Š” ์ธ์ž๋กœ Ptr a -> IO b ํƒ€์ž…์˜ ํ•จ์ˆ˜๋ฅผ ์ทจํ•˜๊ณ  IO b๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์‹ค์ „์—์„œ๋Š” ๋‹ค์Œ ๋žŒ๋‹ค ํ•จ์ˆ˜์™€ ๊ฐ™์€ ํŒจํ„ด์„ ๋”ฐ๋ฅธ๋‹ค. ... alloca $ \pointer -> do c_function argument pointer result <- peek pointer return result ํฌ์ธํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ํ•„์š”ํ•˜๋ฉด ์ด ํŒจํ„ด์„ ์ค‘์ฒฉํ•  ์ˆ˜ ์žˆ๋‹ค. ... alloca $ \firstPointer -> alloca $ \secondPointer -> do c_function argument firstPointer secondPointer first <- peek firstPointer second <- peek secondPointer return (first, second) frexp ํ•จ์ˆ˜๋กœ ๋Œ์•„๊ฐ€์ž. alloca์˜ ์ธ์ž์ธ ๋žŒ๋‹ค ํ•จ์ˆ˜๊ฐ€ ํ‰๊ฐ€๋˜์ž๋งˆ์ž peek๋กœ ํฌ์ธํ„ฐ๋ฅผ ์ฝ๋Š”๋‹ค. C ํ•จ์ˆ˜ gsl_frexp๊ฐ€ IO ๋ชจ๋‚˜๋“œ ์•ˆ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๊ฐ€ ์—ฌ๊ธฐ์„œ ๋“œ๋Ÿฌ๋‚œ๋‹ค. GHC๊ฐ€ f๋ฅผ ์–ธ์ œ ๊ณ„์‚ฐํ• ์ง€ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด, GHC๋Š” ํ•„์š”ํ•˜๊ธฐ ์ „๊นŒ์ง€ f๋ฅผ ๊ณ„์‚ฐํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. return์ด f๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋งˆ์ง€๋ง‰ ์ค„์ด ๋ฐ”๋กœ ๊ทธ๋•Œ์ด๋ฉฐ, ์ด๋•Œ๋Š” ํ• ๋‹น๋˜์—ˆ์ง€๋งŒ ์•„์ง ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•„ ์ž„์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์—์„œ e๋ฅผ ์ฝ์€ ํ›„์ด๋‹ค. ์งง๊ฒŒ ๋งํ•˜๋ฉด ์šฐ๋ฆฌ๊ฐ€ ๊ณ„์‚ฐ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•˜๊ธธ ์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— gsl_frexp๊ฐ€ ๋ชจ๋‚˜ ๋”• ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ์ถœ๋ ฅ์„ ์ €์žฅํ•˜๋Š” ๋Œ€์‹  ์ž…๋ ฅ์„ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค๋ฉด ํ•จ์ˆ˜๋ฅผ ํ‰๊ฐ€ํ•˜๊ธฐ ์ „์— poke ํ•จ์ˆ˜๋กœ ํฌ์ธํ„ฐ์— ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค. ... alloca $ \inputPointer -> alloca $ \outputPointer -> do poke inputPointer value c_function argument inputPointer outputPointer result <- peek outputPointer return result ๋งˆ์ง€๋ง‰ ์ค„์—์„œ๋Š” C ํƒ€์ž…์œผ๋กœ๋ถ€ํ„ฐ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ๋ฅผ ํŠœํ”Œ์— ๋„ฃ๊ณ  ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋ ค๋ฉด GHC๋ฅผ GSL์— ์—ฐ๊ฒฐํ•ด์•ผ ํ•œ๋‹ค. GHCi์—์„œ ๋‹ค์Œ์„ ์‹คํ–‰ํ•œ๋‹ค. $ ghci frexp.hs -lgsl (๋Œ€๋ถ€๋ถ„์˜ ์‹œ์Šคํ…œ์—๋Š” GSL์ด ๋ฏธ๋ฆฌ ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค. ๊ฐœ๋ฐœ ํŒจํ‚ค์ง€์—์„œ ๋‚ด๋ ค๋ฐ›์•„ ์„ค์น˜ํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.) ์˜ฎ๊ธด์ด: ์šฐ๋ถ„ํˆฌ๋Š” libgsl-dev ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. C ๊ตฌ์กฐ์ฒด ๋‹ค๋ฃจ๊ธฐ ๋งŽ์€ C ํ•จ์ˆ˜๊ฐ€ struct์˜ ํ˜•ํƒœ ๋˜๋Š” ๊ตฌ์กฐ์ฒด์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ตฌ์กฐ์ฒด๋ฅผ ์ง์ ‘ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋“œ๋ฌผ๊ณ  ๋Œ€๊ฐœ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฐ˜ํ™˜๊ฐ’์€ ๋Œ€๋ถ€๋ถ„ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์—ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” int์ด๋‹ค. ๋˜ ๋‹ค๋ฅธ GSL ํ•จ์ˆ˜์ธ gsl_sf_bessel_Jn_e๋ฅผ ์‚ดํŽด๋ณด์ž. ์ด ํ•จ์ˆ˜๋Š” ์ฐจ์ˆ˜ n์— ๋Œ€ํ•ด regular cylindrical Bessel ํ•จ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ gsl_sf_result ๊ตฌ์กฐ์ฒด์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ๊ตฌ์กฐ์ฒด๋Š” ๋‘ double์„ ํฌํ•จํ•˜๋Š”๋ฐ ํ•˜๋‚˜๋Š” ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๊ณ  ํ•˜๋‚˜๋Š” ์˜ค์ฐจ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ •์ˆ˜ ์—๋Ÿฌ ์ฝ”๋“œ๋Š” gsl_strerror๋ฅผ ์ด์šฉํ•ด C ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๊ฐ€ ์ถ”๊ตฌํ•˜๋Š” ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜์˜ ๋ช…์„ธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. BesselJn :: Int -> Double -> Either String (Double, Double) ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” cylindrical Bessel ํ•จ์ˆ˜์˜ ์ฐจ์ˆ˜, ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ํ•จ์ˆ˜์˜ ์ธ์ž, ๋ฐ˜ํ™˜๊ฐ’์€ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ๋˜๋Š” ๊ฒฐ๊ณผ์™€ ์˜ค์ฐจํ•œ๊ณ„์˜ ํŠœํ”Œ์ด๋‹ค. Storable ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋งŒ๋“ค๊ธฐ gsl_sf_result ๊ตฌ์กฐ์ฒด์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ํ• ๋‹นํ•˜๊ณ  ์ฝ์œผ๋ ค๋ฉด Storable ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฐ ์ผ์—๋Š” hsc2hs ํ”„๋กœ๊ทธ๋žจ์ด ์ ๊ฒฉ์ด๋‹ค. ๋จผ์ € Bessel.hsc ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ํ•˜์Šค ์ผˆ ๋ฌธ๋ฒ•๊ณผ C ๋งคํฌ๋กœ๋ฅผ ์„ž์–ด ์“ด๋‹ค. ๋‚˜์ค‘์— ๋‹ค์Œ ๋ช…๋ น์„ ํ†ตํ•ด ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋กœ ํ™•์žฅํ•œ๋‹ค. $ hsc2hs Bessel.hsc ๊ทธ๋‹ค์Œ Bessel.hs๋ฅผ GHC๋กœ ๋กœ๋“œํ•œ๋‹ค. ๋‹ค์Œ์€ Bessel.hsc์˜ ์ฒ˜์Œ ๋ชจ์Šต์ด๋‹ค. {-# LANGUAGE ForeignFunctionInterface #-} module Bessel (besselJn) where import Foreign import Foreign.Ptr import Foreign.C.String import Foreign.C.Types #include <gsl/gsl_sf_result.h> data GslSfResult = GslSfResult { gsl_value :: CDouble, gsl_error :: CDouble } instance Storable GslSfResult where sizeOf _ = (#size gsl_sf_result) alignment _ = alignment (undefined :: CDouble) peek ptr = do value <- (#peek gsl_sf_result, val) ptr error <- (#peek gsl_sf_result, err) ptr return GslSfResult { gsl_value = value, gsl_error = error } poke ptr (GslSfResult value error) = do (#poke gsl_sf_result, val) ptr value (#poke gsl_sf_result, err) ptr error hsc2hs์—๊ฒŒ gsl_sf_result์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ฐพ์„ ์œ„์น˜๋ฅผ ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด #include ์ง€์‹œ๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  GSL์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ฐ˜์˜ํ•˜๋Š”, CDouble 2๊ฐœ๋ฅผ ๊ฐ€์ง€๋Š” ํ•˜์Šค ์ผˆ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•œ๋‹ค. ์ด๊ฒƒ์„ Storable์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค. ์‚ฌ์‹ค ์ด ์˜ˆ์ œ์—๋Š” sizeOf, alignment, peek๋งŒ ํ•„์š”ํ•˜์ง€๋งŒ ์™„๋ฒฝํ•จ์„ ์œ„ํ•ด poke๋„ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค. sizeOf๋Š” ํ™•์‹คํžˆ ํ• ๋‹น ์ ˆ์ฐจ์— ํ•„์š”ํ•˜๊ณ , #size ๋งคํฌ๋กœ๋ฅผ ํ†ตํ•ด hsc2hs์— ์˜ํ•ด ๊ณ„์‚ฐ๋œ๋‹ค. alignment๋Š” ์ž๋ฃŒ๊ตฌ์กฐ alignment์˜ ๋ฐ”์ดํŠธ ํฌ๊ธฐ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ž๋ฃŒ๊ตฌ์กฐ ๋‚ด ์›์†Œ๋“ค์˜ alignment ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฒƒ์ธ๋ฐ, ์ด ๊ฒฝ์šฐ ๋ชจ๋‘ CDouble์ด๋ฏ€๋กœ ์ด๊ฒƒ์˜ alignment๋ฅผ ์“ด๋‹ค. alignment์˜ ์ธ์ž์˜ ๊ฐ’์€ ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค. ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ธ์ž์˜ ํƒ€์ž…์ด๋‹ค. peek๋Š” do ๋ธ”๋ก๊ณผ #peek ๋งคํฌ๋กœ๋ฅผ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„ํ•œ๋‹ค. val๊ณผ err๋Š” GSL ์†Œ์Šค ์ฝ”๋“œ์˜ ๊ตฌ์กฐ์ฒด ํ•„๋“œ๋“ค์˜ ์ด๋ฆ„์ด๋‹ค. poke๋Š” #poke ๋งคํฌ๋กœ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค. C ํ•จ์ˆ˜ ๋“ค์—ฌ์˜ค๊ธฐ foreign import ccall unsafe "gsl/gsl_bessel.h gsl_sf_bessel_Jn_e" c_besselJn :: CInt -> CDouble -> Ptr GslSfResult -> IO CInt foreign import ccall unsafe "gsl/gsl_errno.h gsl_set_error_handler_off" c_deactivate_gsl_error_handler :: IO () foreign import ccall unsafe "gsl/gsl_errno.h gsl_strerror" c_error_string :: CInt -> IO CString GSL ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ๋ถ€ํ„ฐ ๋ช‡ ๊ฐ€์ง€ ํ•จ์ˆ˜๋ฅผ ๋“ค์—ฌ์˜จ๋‹ค. ๋จผ์ € ์‹ค์ œ ๊ณ„์‚ฐ์„ ํ•˜๋Š” Bessel ํ•จ์ˆ˜, ๊ทธ๋‹ค์Œ gsl_set_error_handler_off ํ•จ์ˆ˜๋ฅผ ๋“ค์—ฌ์˜จ๋‹ค. ๊ธฐ๋ณธ GSL ์—๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ๋Š” ํ•˜์Šค์ผˆ์—์„œ ํ˜ธ์ถœํ•ด๋„ ํ”„๋กœ๊ทธ๋žจ์„ ์ฃฝ์ด๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์—๋Ÿฌ ์ฝ”๋“œ๋ฅผ ์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” C ๋ฌธ์ž์—ด๋กœ ๋ฒˆ์—ญํ•˜๋Š” GSL ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ๋“ค์—ฌ์˜จ๋‹ค. Bessel ํ•จ์ˆ˜ ๊ตฌํ˜„ ๋งˆ์ง€๋ง‰์œผ๋กœ n ์ฐจ GSL cylindrical Bessel ํ•จ์ˆ˜์˜ ํ•˜์Šค ์ผˆ ๋ฒ„์ „์„ ๊ตฌํ˜„ํ•œ๋‹ค. besselJn :: Int -> Double -> Either String (Double, Double) besselJn n x = unsafePerformIO $ alloca $ \gslSfPtr -> do c_deactivate_gsl_error_handler status <- c_besselJn (fromIntegral n) (realToFrac x) gslSfPtr if status == 0 then do GslSfResult val err <- peek gslSfPtr return $ Right (realToFrac val, realToFrac err) else do error <- c_error_string status error_message <- peekCString error return $ Left ("GSL error: "++error_message) ์ด๋ฒˆ์—๋„ unsafePerformIO๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋‚ด๋ถ€ ๊ตฌํ˜„์€ ๊ทธ๋ ‡์ง€ ์•Š์„์ง€๋ผ๋„ ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋กœ ์ทจ๊ธ‰๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. GSL ๊ฒฐ๊ณผ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ํ• ๋‹นํ•œ ํ›„ GSL ์—๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•ด์„œ, ๋ฌด์–ธ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์„ ๋•Œ ํฌ๋ž˜์‹œ๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์„ ๋ง‰๋Š”๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ GSL ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ์ด ์‹œ์ ์—์„œ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” status๊ฐ€ 0์ด๋ฉด ๊ฒฐ๊ณผ๋ฅผ unmarshal ํ•˜๊ณ  ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด GSL ์—๋Ÿฌ-๋ฌธ์ž์—ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์—๋Ÿฌ๋ฅผ Left ๊ฒฐ๊ณผ๋กœ ์ „๋‹ฌํ•œ๋‹ค. ์˜ˆ์ œ Bessel.hsc ํ•จ์ˆ˜ ์ž‘์„ฑ์„ ๋งˆ์ณค์œผ๋‹ˆ ํ•˜์Šค ์ผˆ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋กœ๋“œํ•œ๋‹ค. $ hsc2hs Bessel.hsc $ ghci Bessel.hs -lgsl ์ด์ œ Bessel ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. > besselJn 0 10 Right (-0.2459357644513483, 1.8116861737200453e-16) > besselJn 1 0 Right (0.0,0.0) > besselJn 1000 2 Left "GSL error: underflow" ๊ณ ๊ธ‰ ์ฃผ์ œ ์ด ์ ˆ์—์„œ๋Š” FFI์˜ ๋ณด๋‹ค ๋ณต์žกํ•œ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋Š” ๊ณ ๊ธ‰ ์˜ˆ์ œ๋ฅผ ๋‹ค๋ฃฌ๋‹ค. GSL์˜ ์ข€ ๋” ๋ณต์žกํ•œ ํ•จ์ˆ˜๋ฅผ ํ•˜์Šค ์ผˆ๋กœ ์ž„ํฌํŠธ ํ•  ๊ฒƒ์ด๋ฉฐ, ์ด ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๋‘ ์  ์‚ฌ์ด์—์„œ adaptive Gauss-Kronrod ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด ํ•จ์ˆ˜์˜ ์ ๋ถ„์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด GSL ํ•จ์ˆ˜์˜ ์ด๋ฆ„์€ gsl_integration_qag์ด๋‹ค. ์ด๋ฒˆ ์˜ˆ์ œ๋Š” ํ•จ์ˆ˜ ํฌ์ธํ„ฐ, ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋ฅผ C ๋ฃจํ‹ด์œผ๋กœ ์ต์ŠคํฌํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•, ์—ด๊ฑฐํ˜•, ์•Œ๋ ค์ง€์ง€ ์•Š์€ ๊ตฌ์กฐ์ฒด์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ๋‹ค. ์ด์šฉ ๊ฐ€๋Šฅํ•œ C ํ•จ์ˆ˜์™€ ๊ตฌ์กฐ์ฒด GSL์ด ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ qag์œผ๋กœ ์ ๋ถ„ํ•˜๋ ค๋ฉด ์„ธ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. gsl_integration_workspace * gsl_integration_workspace_alloc (size_t n); void gsl_integration_workspace_free (gsl_integration_workspace * w); int gsl_integration_qag (const gsl_function * f, double a, double b, double epsabs, double epsrel, size_t limit, int key, gsl_integration_workspace * workspace, double * result, double * abserr); ์ฒ˜์Œ ๋‘ ํ•จ์ˆ˜๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋ชจ๋ฅด๋Š” "์ž‘์—…์žฅ" ๊ตฌ์กฐ์ฒด์˜ ํ• ๋‹น๊ณผ ํ•ด์ œ๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค. ์šฐ๋ฆฌ๋Š” ํฌ์ธํ„ฐ๋งŒ ๋„˜๊ธธ ๋ฟ์ด๋‹ค. ์‹ค์ œ ์ผ์€ ๋งˆ์ง€๋ง‰ ํ•จ์ˆ˜๊ฐ€ ํ•˜๋Š”๋ฐ, ์ด ํ•จ์ˆ˜๋Š” ์ž‘์—…์žฅ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ์š”๊ตฌํ•œ๋‹ค. GSL์€ ์ด ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ C ๊ตฌ์กฐ์ฒด๋ฅผ ๋ช…์‹œํ•œ๋‹ค. struct gsl_function { double (* function) (double x, void * params); void * params; }; void ํฌ์ธํ„ฐ์ธ ์ด์œ ๋Š” ฮป ํ•จ์ˆ˜๋ฅผ C์—์„œ ์ •์˜ํ•  ์ˆ˜ ์—†์–ด์„œ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์€ ๋ฏธ์ง€์˜ ํƒ€์ž…์„ ๊ฐ€์ง„ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ํ•จ๊ป˜ ์ „๋‹ฌ๋œ๋‹ค. ํ•˜์Šค์ผˆ์—์„œ๋Š” params๊ฐ€ ํ•„์š” ์—†๊ธฐ ๋•Œ๋ฌธ์— ์•ž์œผ๋กœ ์ญ‰ ๋ฌด์‹œํ•˜๊ฒ ๋‹ค. import์™€ inclusion qag.hsc ํŒŒ์ผ์€ ์ด๋ ‡๊ฒŒ ์‹œ์ž‘ํ•œ๋‹ค. {-# LANGUAGE ForeignFunctionInterface, EmptyDataDecls #-} module Qag ( qag, gauss15, gauss21, gauss31, gauss41, gauss51, gauss61 ) where import Foreign import Foreign.Ptr import Foreign.C.Types import Foreign.C.String #include <gsl/gsl_math.h> #include <gsl/gsl_integration.h> foreign import ccall unsafe "gsl/gsl_errno.h gsl_strerror" c_error_string :: CInt -> IO CString foreign import ccall unsafe "gsl/gsl_errno.h gsl_set_error_handler_off" c_deactivate_gsl_error_handler :: IO () ์—ฌ๊ธฐ์„œ ์„ ์–ธํ•œ EmptyDataDecls pragma๋Š” ๋‚˜์ค‘์— Workspace ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. ์ด ํŒŒ์ผ์€ ๋ฐ”๊นฅ์„ธ์ƒ์— ๋ณด์—ฌ์„œ๋Š” ์•ˆ ๋  ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ํฌํ•จํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“ˆ๋กœ ์„ ์–ธํ•˜๊ณ  ์ตœ์ข… ํ•จ์ˆ˜์ธ qag์™€ gauss ํ”Œ๋ž˜๊ทธ๋“ค๋งŒ ๋‚ด๋ณด๋‚ธ๋‹ค. ๊ทธ๋ฆฌ๊ณ  GSL์˜ ๊ด€๋ จ ์žˆ๋Š” C ํ—ค๋”๋“ค์„ ํฌํ•จ์‹œํ‚จ๋‹ค. ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ์™€ ์—๋Ÿฌ ํ•ธ๋“ค๋Ÿฌ์˜ ๋น„ํ™œ์„ฑํ™”๋ฅผ ์œ„ํ•ด C ํ•จ์ˆ˜๋“ค์„ ๋“ค์—ฌ์˜ค๋Š” ๊ฒƒ์€ ์•ž์—์„œ ์„ค๋ช…ํ–ˆ๋‹ค. ์—ด๊ฑฐํ˜• gsl_integration_qag์˜ ์ธ์ž๋“ค ์ค‘ ํ•˜๋‚˜์ธ key๋Š” 1์—์„œ 6๊นŒ์ง€์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ •์ˆ˜๋กœ์„œ ์ ๋ถ„ ๊ทœ์น™์„ ์ง€์‹œํ•œ๋‹ค. GSL์€ ๊ฐ๊ฐ์˜ ๊ฐ’๋งˆ๋‹ค ๋งคํฌ๋กœ๋ฅผ ์ •์˜ํ•˜์ง€๋งŒ ํ•˜์Šค์ผˆ์—์„œ๋Š” ํƒ€์ž…์„ ํ•˜๋‚˜ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ž์—ฐ์Šค๋Ÿฝ๋‹ค. ์ด ํƒ€์ž…์„ IntegrationRule์ด๋ผ๊ณ  ํ•˜์ž. IntegrationRule์˜ ๊ฐ’๋“ค์„ hsc2hs๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๋„๋ก enum ๋งคํฌ๋กœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. newtype IntegrationRule = IntegrationRule { rule :: CInt } #{enum IntegrationRule, IntegrationRule, gauss15 = GSL_INTEG_GAUSS15, gauss21 = GSL_INTEG_GAUSS21, gauss31 = GSL_INTEG_GAUSS31, gauss41 = GSL_INTEG_GAUSS41, gauss51 = GSL_INTEG_GAUSS51, gauss61 = GSL_INTEG_GAUSS61 } hsc2hs๋Š” ๋งคํฌ๋กœ๋ฅผ ์œ„ํ•œ ํ—ค๋”๋“ค์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์•Œ๋งž์€ ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค. enum ์ง€์‹œ๋ฌธ์€ ๊ฐ enum ๊ฐ’๋งˆ๋‹ค ์•Œ๋งž์€ ํƒ€์ž… ๋ช…์„ธ๊ฐ€ ๋ถ™์€ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค. ์œ„ ์˜ˆ์ œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์‹์œผ๋กœ ๋ฒˆ์—ญ๋œ๋‹ค. (C ๋งคํฌ๋กœ๋Š” ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์œผ๋กœ ์น˜ํ™˜๋œ๋‹ค) newtype IntegrationRule = IntegrationRule { rule :: CInt } gauss15 :: IntegrationRule gauss15 = IntegrationRule GSL_INTEG_GAUSS15 gauss21 :: IntegrationRule gauss21 = IntegrationRule GSL_INTEG_GAUSS21 ... ์ด ๋ณ€์ˆ˜๋“ค์€ ์ˆ˜์ •๋  ์ˆ˜ ์—†๊ณ  ๋ณธ์งˆ์ ์œผ๋กœ ์ƒ์ˆ˜ ํ”Œ๋ž˜๊ทธ๋‹ค. ๋ชจ๋“ˆ ์„ ์–ธ์—์„œ IntegrationRule์˜ ์ƒ์„ฑ์ž๋Š” ๋นผ๊ณ  gauss ํ”Œ๋ž˜๊ทธ๋“ค๋งŒ ๋‚ด๋ณด๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์„ ๋งŒ๋“œ๋Š” ๊ฒƒ ์ž์ฒด๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฑฑ์ •๊ฑฐ๋ฆฌ๊ฐ€ ํ•˜๋‚˜ ์ค„์–ด๋“  ์…ˆ์ด๋‹ค! Haskell Function Target ์ด์ œ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ํ•จ์ˆ˜์˜ ๋ช…์„ธ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. qag :: IntegrationRule -- Algorithm type -> Int -- Step limit -> Double -- Absolute tolerance -> Double -- Relative tolerance -> (Double -> Double) -- Function to integrate -> Double -- Integration interval start -> Double -- Integration interval end -> Either String (Double, Double) -- Result and (absolute) error estimate ์ธ์ž๋“ค์˜ ์ˆœ์„œ๊ฐ€ C ๋ฒ„์ „๊ณผ ๋‹ค๋ฅธ ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. ์‚ฌ์‹ค C์—๋Š” ๋ถ€๋ถ„ ์ ์šฉ(partial application)์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•˜์Šค์ผˆ์—์„œ๋Š” ์ˆœ์„œ ๋ฐฐ์น˜๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์ด๋‹ค. ์ด์ „ ์˜ˆ์ œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์—๋Ÿฌ๋ฅผ Either String (Double, Double) ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. ํ•˜์Šค ์ผˆ ํ•จ์ˆ˜๋ฅผ C ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์ „๋‹ฌํ•˜๊ธฐ type CFunction = CDouble -> Ptr () -> CDouble data GslFunction = GslFunction (FunPtr CFunction) (Ptr ()) instance Storable GslFunction where sizeOf _ = (#size gsl_function) alignment _ = alignment (undefined :: Ptr ()) peek ptr = do function <- (#peek gsl_function, function) ptr return $ GslFunction function nullPtr poke ptr (GslFunction fun nullPtr) = do (#poke gsl_function, function) ptr fun makeCfunction :: (Double -> Double) -> (CDouble -> Ptr () -> CDouble) makeCfunction f = \x voidpointer -> realToFrac $ f (realToFrac x) foreign import ccall "wrapper" makeFunPtr :: CFunction -> IO (FunPtr CFunction) ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ์ถ•์•ฝ ํƒ€์ž… CFunction์„ ์ •์˜ํ•œ๋‹ค. void ํฌ์ธํ„ฐ๋Š” Ptr ()์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๋Š”๋ฐ, ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‹ค์Œ์€ gsl_function ๊ตฌ์กฐ์ฒด ์ฐจ๋ก€๋‹ค. ๋†€๋ž„ ๋งŒํ•œ ๊ฒƒ์€ ์—†๋‹ค. void ํฌ์ธํ„ฐ๋Š” peek์—์„œ๋„ poke์—์„œ๋„ null์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ์ฝ๊ธฐ๋„ ์“ฐ๊ธฐ๋„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์Šค ์ผˆ Double -> Double ํ•จ์ˆ˜๋ฅผ C ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด ๋‘ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นœ๋‹ค. ์ฒซ ๋ฒˆ์งธ, makeCfunction ์•ˆ์—์„œ ฮป ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ธ์ž๋“ค์„ ์žฌ๊ตฌ์„ฑํ•œ๋‹ค. ๊ทธ๋‹ค์Œ makeFunPtr์—์„œ ํ•จ์ˆ˜๋ฅผ ์žฌ์ •๋ ฌ๋œ ์ธ์ž๋“ค๊ณผ ํ•จ๊ป˜ ๋ฐ›์•„์„œ, poke์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ ํฌ์ธํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค. ์ด๋กœ์จ GslFunction ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ๋ฏธ์ง€์˜ ๊ตฌ์กฐ์ฒด ๋‹ค๋ฃจ๊ธฐ data Workspace foreign import ccall unsafe "gsl/gsl_integration.h gsl_integration_workspace_alloc" c_qag_alloc :: CSize -> IO (Ptr Workspace) foreign import ccall unsafe "gsl/gsl_integration.h gsl_integration_workspace_free" c_qag_free :: Ptr Workspace -> IO () foreign import ccall safe "gsl/gsl_integration.h gsl_integration_qag" c_qag :: Ptr GslFunction -- Allocated GSL function structure -> CDouble -- Start interval -> CDouble -- End interval -> CDouble -- Absolute tolerance -> CDouble -- Relative tolerance -> CSize -- Maximum number of subintervals -> CInt -- Type of Gauss-Kronrod rule -> Ptr Workspace -- GSL integration workspace -> Ptr CDouble -- Result -> Ptr CDouble -- Computation error -> IO CInt -- Exit code EmptyDataDecls pragma๋ฅผ ๋“ค์—ฌ์˜ค๋Š” ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์šฐ๋ฆฌ๋Š” Workspace ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ƒ์„ฑ์ž ์—†์ด ์„ ์–ธํ–ˆ๋‹ค. ์ด๋Š” Workspace๊ฐ€ ํ•ญ์ƒ ํฌ์ธํ„ฐ๋กœ์„œ ๋‹ค๋ฃจ์–ด์ง€๊ณ  ์ ˆ๋Œ€ ์ธ์Šคํ„ด์Šคํ™”๋˜์ง€ ์•Š๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉํŽธ์ด์—ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์šฐ๋ฆฌ๋Š” ๋ณดํ†ต ํ• ๋‹น, ํ•ด์ œ ๋ฃจํ‹ด์„ ๋“ค์—ฌ์˜จ๋‹ค. ์ด์ œ ๋ชจ๋“  ์กฐ๊ฐ(GslFunction๊ณผ Workspace)์„ ๊ฐ–์ถ”์—ˆ์œผ๋‹ˆ ์ ๋ถ„ ํ•จ์ˆ˜๋ฅผ ๋“ค์—ฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์™„์„ฑ๋œ ํ•จ์ˆ˜ ์ด์ œ GSL์˜ QAG ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. qag gauss steps abstol reltol f a b = unsafePerformIO $ do c_deactivate_gsl_error_handler workspacePtr <- c_qag_alloc (fromIntegral steps) if workspacePtr == nullPtr then return $ Left "GSL could not allocate workspace" else do fPtr <- makeFunPtr $ makeCfunction f alloca $ \gsl_f -> do poke gsl_f (GslFunction fPtr nullPtr) alloca $ \resultPtr -> do alloca $ \errorPtr -> do status <- c_qag gsl_f (realToFrac a) (realToFrac b) (realToFrac abstol) (realToFrac reltol) (fromIntegral steps) (rule gauss) workspacePtr resultPtr errorPtr c_qag_free workspacePtr freeHaskellFunPtr fPtr if status /= 0 then do c_errormsg <- c_error_string status errormsg <- peekCString c_errormsg return $ Left errormsg else do c_result <- peek resultPtr c_error <- peek errorPtr let result = realToFrac c_result let error = realToFrac c_error return $ Right (result, error) ๋ฌด์—‡๋ณด๋‹ค ๋จผ์ € GSL ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ์ž๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•œ๋‹ค. ์˜ค๋ฅ˜๋ฅผ ๋ณด๊ณ ํ•˜๋Š” ๋Œ€์‹  ํ”„๋กœ๊ทธ๋žจ์„ ์ฃฝ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋‹ค์Œ ์ž‘์—…์žฅ์„ ํ• ๋‹นํ•œ๋‹ค. ๋ฐ˜ํ™˜๋œ ํฌ์ธํ„ฐ๊ฐ€ null์ด๋ฉด ๋ณด๊ณ ํ•ด์•ผ ํ•˜๋Š” (๋ณดํ†ต์€ ํฌ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ํฌ๋‹ค๋Š”) ์˜ค๋ฅ˜๊ฐ€ ์žˆ์—ˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ž‘์—…์žฅ์„ ์ž˜ ํ• ๋‹นํ–ˆ์œผ๋ฉด ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ•จ์ˆ˜ ํฌ์ธํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  GslFunction ๊ตฌ์กฐ์ฒด๋ฅผ ํ• ๋‹นํ•œ๋‹ค. ์—ฌ๊ธฐ์— ํ•จ์ˆ˜ ํฌ์ธํ„ฐ๋ฅผ ๋„ฃ๋Š”๋‹ค. ๋ฉ”์ธ ๋ฃจํ‹ด ํ˜ธ์ถœ ์ „์— ๋งˆ์ง€๋ง‰ ์ค€๋น„๋กœ ๊ฒฐ๊ด๊ฐ’๊ณผ ๊ทธ ์˜ค์ฐจ ๋ฒ”์œ„๋ฅผ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•œ๋‹ค. ํ˜ธ์ถœ ํ›„์—๋Š” ์ผ์ข…์˜ ์ •๋ฆฌ ์ •๋ˆ์„ ํ•˜๊ณ  ์ž‘์—…์žฅ๊ณผ ํ•จ์ˆ˜ ํฌ์ธํ„ฐ์— ํ• ๋‹นํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•ด์•ผ ํ•œ๋‹ค. ForeignPtr์„ ์ด์šฉํ•ด ์ •๋ฆฌ ์ •๋ˆ์„ ๊ฑด๋„ˆ๋›ธ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐ ํ•„์š”ํ•œ ์ž‘์—…์€ ํด๋ฆฐ์—… ํ•œ ์ค„์„ ๊ธฐ์–ตํ•˜๋Š” ๋…ธ๊ณ  ์ด์ƒ์˜ ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋‹ค์Œ ๋ฐ˜ํ™˜ ๊ฐ’์„ ํ™•์ธํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. Bessel ํ•จ์ˆ˜์—์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ์Šค์Šค๋กœ ํ•ด์ œํ•˜๋Š” ํฌ์ธํ„ฐ ์•ž์˜ ์˜ˆ์ œ์—์„œ GSL ์ ๋ถ„์— ํ•„์š”ํ•œ ์ž‘์—…์žฅ(์šฐ๋ฆฌ๋Š” ๋ชจ๋ฅด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ)์˜ ํ•ด์ œ๋ฅผ C ํ• ๋‹น ํ•ด์ œ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•ด์„œ ์ฒ˜๋ฆฌํ–ˆ๋‹ค. ํ•˜์Šค ์ผˆ๋กœ ๋“ค์—ฌ์˜ค๋ ค๊ณ  ํ•˜๋Š” ์—ฌ๋Ÿฌ ์ ๋ถ„ ๋ฃจํ‹ด์ด ๊ฐ™์€ ์ž‘์—…์žฅ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ? ๊ฐ™์€ ํ• ๋‹น/ํ•ด์ œ ์ฝ”๋“œ๋ฅผ ๋งค๋ฒˆ ๋ณต์ œํ•˜๋ฉด ๋ˆ„๊ตฐ๊ฐ€ ํ•ด์ œ๋ฅผ ๊นœ๋นกํ–ˆ์„ ๋•Œ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ์ผ์–ด๋‚œ๋‹ค. ์—ฌ๊ธฐ์— ๋” ์ด์ƒ ํ•„์š” ์—†์œผ๋ฉด ์Šค์Šค๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•˜๋Š” ์ผ์ข…์˜ "์Šค๋งˆํŠธ ํฌ์ธํ„ฐ"๋ฅผ ๋„์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์„ ForeignPtr์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. (Foreign.Ptr๊ณผ ํ—ท๊ฐˆ๋ฆฌ์ง€ ๋ง ๊ฒƒ. ์‚ฌ์‹ค ์ด๊ฒƒ์˜ ํ•œ์ • ์ด๋ฆ„(qualified name)์€ Foreign.ForeignPtr์ด๋‹ค) ํ•ด์ œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋Š” finalizer๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ด ์ ˆ์—์„œ๋Š” GSL ์ž‘์—…์žฅ์„ ํ• ๋‹นํ•˜๊ณ  ForeignPtr๋กœ ์ ์ ˆํžˆ ๊พธ๋ฏธ๋Š” ๊ฐ„๋‹จํ•œ ๋ชจ๋“ˆ์„ ์ž‘์„ฑํ•˜์—ฌ, ์‚ฌ์šฉ์ž๋“ค์ด ํ• ๋‹น ํ•ด์ œ๋ฅผ ๊ฑฑ์ •ํ•  ํ•„์š”๋ฅผ ์—†์•จ ๊ฒƒ์ด๋‹ค. ์ด ๋ชจ๋“ˆ์€ GSLWorkspace.hs ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ๋œ๋‹ค. {-# LANGUAGE ForeignFunctionInterface, EmptyDataDecls #-} module GSLWorkSpace (Workspace, createWorkspace) where import Foreign.C.Types import Foreign.Ptr import Foreign.ForeignPtr data Workspace foreign import ccall unsafe "gsl/gsl_integration.h gsl_integration_workspace_alloc" c_ws_alloc :: CSize -> IO (Ptr Workspace) foreign import ccall unsafe "gsl/gsl_integration.h &gsl_integration_workspace_free" c_ws_free :: FunPtr( Ptr Workspace -> IO () ) createWorkspace :: CSize -> IO (Maybe (ForeignPtr Workspace) ) createWorkspace size = do ptr <- c_ws_alloc size if ptr /= nullPtr then do foreignPtr <- newForeignPtr c_ws_free ptr return $ Just foreignPtr else return Nothing ์ฒซ ๋ฒˆ์งธ๋กœ ์•ž ์ ˆ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋นˆ ์ž๋ฃŒ ๊ตฌ์กฐ Workspace๋ฅผ ์„ ์–ธํ•œ๋‹ค. gsl_integration_workspace_alloc๊ณผ gsl_integration_workspace_free ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ๋” ์ด์ƒ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. ํ• ๋‹น ํ•ด์ œ ํ•จ์ˆ˜๋Š” ์•ฐํผ์ƒŒ๋“œ(&)์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์— ์ฃผ์˜. finalizer๋กœ ์ง€์ •ํ•˜๋ ค๋ฉด ํ•จ์ˆ˜ ์ž์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ž‘์—…์žฅ ์ƒ์„ฑ ํ•จ์ˆ˜๋Š” IO (Maybe) ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ• ๋‹น์ด ์‹คํŒจํ•˜๊ณ  null ํฌ์ธํ„ฐ๊ฐ€ ๋ฐ˜ํ™˜๋  ๊ฐ€๋Šฅ์„ฑ์ด ์—ฌ์ „ํžˆ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. GSL์€ null ํฌ์ธํ„ฐ์— ๋Œ€ํ•ด ํ• ๋‹น ํ•ด์ œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚ ์ง€ ๋ช…์‹œํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์•ˆ์ „์„ ์œ„ํ•ด ์ด ๊ฒฝ์šฐ finalizer๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  IO Nothing์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์‚ฌ์šฉ์ž ์ฝ”๋“œ๋Š” ๋ฐ˜ํ™˜ ๊ฐ’์ด Just ์ธ์ง€๋ฅผ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค. ํ• ๋‹น ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ํฌ์ธํ„ฐ๊ฐ€ null์ด ์•„๋‹ˆ๋ฉด ํ• ๋‹น ํ•ด์ œ ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์™ธ๋ถ€ ํฌ์ธํ„ฐ๋ฅผ ์ œ์ž‘ํ•˜์—ฌ Maybe์— ๋„ฃ๊ณ , ๊ทธ๊ฒƒ์„ ๋‹ค์‹œ IO ๋ชจ๋‚˜๋“œ์— ๋„ฃ๋Š”๋‹ค. ์ด๊ฒŒ ๋์ด๋‹ค. ์ด์ œ ์ด ์™ธ๋ถ€ ํฌ์ธํ„ฐ๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ปดํŒŒ์ผ๋œ ๋ชฉ์  ์ฝ”๋“œ๋ฅผ ์š”๊ตฌํ•œ๋‹ค. GHCi์—์„œ ์ด ๋ชจ๋“ˆ์„ ๋กœ๋“œํ•  ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง€์‹œํ•ด์•ผ ํ•œ๋‹ค. $ ghci GSLWorkspace.hs -fobject-code ๋˜๋Š” GHCi ์•ˆ์—์„œ :set -fobject-code :load GSLWorkspace.hs qag.hsc ํŒŒ์ผ์ด ์ƒˆ๋กœ์šด ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋„๋ก ๊ณ ์ณ์•ผ ํ•œ๋‹ค. ๋ฐ”๋€ ๋ถ€๋ถ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. {-# LANGUAGE ForeignFunctionInterface #-} -- [...] import GSLWorkSpace import Data.Maybe(isNothing, fromJust) -- [...] qag gauss steps abstol reltol f a b = unsafePerformIO $ do c_deactivate_gsl_error_handler ws <- createWorkspace (fromIntegral steps) if isNothing ws then return $ Left "GSL could not allocate workspace" else do withForeignPtr (fromJust ws) $ \workspacePtr -> do -- [...] ํ™•์‹คํžˆ EmptyDataDecls ํ™•์žฅ์€ ๋” ์ด์ƒ ํ•„์š” ์—†๋‹ค. ๋Œ€์‹  GSLWorkSpace ๋ชจ๋“ˆ ๊ทธ๋ฆฌ๊ณ  Data.Maybe์—์„œ ์œ ์šฉํ•œ ํ•จ์ˆ˜ ๋ช‡ ๊ฐœ๋ฅผ ๋“ค์—ฌ์˜จ๋‹ค. ์ž‘์—…์žฅ ํ• ๋‹น๊ณผ ํ•ด์ œ ํ•จ์ˆ˜์˜ ์™ธ๋ถ€ ์„ ์–ธ๋„ ์ œ๊ฑฐํ•œ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ฐจ์ด์ ์€ ๋ฉ”์ธ ํ•จ์ˆ˜์— ์žˆ๋‹ค. ์ž‘์—…์žฅ ws๋ฅผ ํ• ๋‹นํ•˜๊ณ , Just ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ณ , ๋ชจ๋“  ๊ฒƒ์ด ์ •์ƒ์ด๋ฉด withForeignPtr ํ•จ์ˆ˜๋ฅผ ์จ์„œ ์ž‘์—…์žฅ ํฌ์ธํ„ฐ๋ฅผ ์ถ”์ถœํ•œ๋‹ค. ๋‹ค๋ฅธ ๊ฒƒ์€ ๋ชจ๋‘ ๋™์ผํ•˜๋‹ค. C์—์„œ ํ•˜์Šค ์ผˆ ํ˜ธ์ถœํ•˜๊ธฐ ์ง€์—ฐ ํ‰๊ฐ€์ฒ˜๋Ÿผ C๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ๋Š” ์ง€๋ฆฌ๋ฉธ๋ ฌํ•œ ๊ฒƒ๋“ค์„ ํ•˜์Šค ์ผˆ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด, C์—์„œ ํ•˜์Šค์ผˆ์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•  ๋•Œ๋„ ์žˆ๋‹ค. ๊ณ ์ „์ ์ธ ํ•˜์Šค ์ผˆ ์˜ˆ์ œ์ธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ์šฐ์•„ํ•˜๊ณ  ํ•˜์Šค์ผˆ์Šค๋Ÿฌ์šด ํ•œ ์ค„ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„๋œ๋‹ค. fibonacci = 0 : 1 : zipWith (+) fibonacci (tail fibonacci) ์šฐ๋ฆฌ๊ฐ€ ํ•  ์ผ์€ ํ•˜์Šค์ผˆ์˜ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ ๋Šฅ๋ ฅ์„ C๋กœ ๋‚ด๋ณด๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์Šค์ผˆ์—์„  ๋ณดํ†ต ์œ ๊ณ„ ์—†๋Š” Integer ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ๋‹ค. C์—๋Š” ์ด์— ๋Œ€์‘ํ•˜๋Š” ํƒ€์ž…์ด ์—†์–ด์„œ Integer๋ฅผ ๋‚ด๋ณด๋‚ผ ์ˆ˜ ์—†๋‹ค. ๋” ๋„“์€ ๋ฒ”์œ„์˜ ์ถœ๋ ฅ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด C ํ•จ์ˆ˜๊ฐ€ ์ •์ˆ˜ ํƒ€์ž…์˜ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„œ๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•  ๋•Œ ๋ถ€๋™์†Œ์ˆ˜์  ๊ทผ์‚ฌ์น˜๋ฅผ ์ œ๊ณตํ•˜๊ธฐ๋กœ ํ•œ๋‹ค. ๊ฒฐ๊ณผ๊ฐ€ ๋ถ€๋™์†Œ์ˆ˜์  ๋ฒ”์œ„๋งˆ์ € ๋„˜์–ด์„œ๋ฉด ๊ณ„์‚ฐ์€ ์‹คํŒจํ•œ๋‹ค. ๊ฒฐ๊ณผ์˜ ์ƒํƒœ(C ์ •์ˆ˜๋‚˜ ๋ถ€๋™์†Œ์ˆ˜์ ์œผ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅํ•œ์ง€, ์•„์˜ˆ ๋ถˆ๊ฐ€๋Šฅํ•œ์ง€)๋Š” ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ƒํƒœ ์ •์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ํ•จ์ˆ˜์˜ ๋ช…์„ธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. int fib( int index, unsigned long long* result, double* approx) ํ•˜์Šค ์ผˆ ์†Œ์Šค fibonacci.hs ํŒŒ์ผ์˜ ํ•˜์Šค ์ผˆ ์†Œ์Šค ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. {-# LANGUAGE ForeignFunctionInterface #-} module Fibonacci where import Foreign import Foreign.C.Types fibonacci :: (Integral a) => [a] fibonacci = 0 : 1 : zipWith (+) fibonacci (tail fibonacci) foreign export ccall fibonacci_c :: CInt -> Ptr CULLong -> Ptr CDouble -> IO CInt fibonacci_c :: CInt -> Ptr CULLong -> Ptr CDouble -> IO CInt fibonacci_c n intPtr dblPtr | badInt && badDouble = return 2 | badInt = do poke dblPtr dbl_result return 1 | otherwise = do poke intPtr (fromIntegral result) poke dblPtr dbl_result return 0 where result = fibonacci !! (fromIntegral n) dbl_result = realToFrac result badInt = result > toInteger (maxBound :: CULLong) badDouble = isInfinite dbl_result ๋‚ด๋ณด๋‚ผ ๋•Œ ํ•จ์ˆ˜๋“ค์„ ๋ชจ๋“ˆ๋กœ ๊ฐ์‹ธ์•ผ ํ•œ๋‹ค. (์ด๊ฒƒ์€ ์–ด์จŒ๋“  ์ข‹์€ ์Šต๊ด€์ด๋‹ค) ํ”ผ๋ณด๋‚˜์น˜ ๋ฌดํ•œ ๋ฆฌ์ŠคํŠธ๋Š” ์ด๋ฏธ ๋ดค์œผ๋‹ˆ ๋‚ด๋ณด๋‚ด๋Š” ํ•จ์ˆ˜์— ์ง‘์ค‘ํ•˜์ž. ์ด ํ•จ์ˆ˜๋Š” ์ธ์ž ํ•˜๋‚˜, unsigned long long๊ณผ double์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ์ทจํ•˜๊ณ  ์ƒํƒœ ๊ฐ’์„ IO ๋ชจ๋‚˜๋“œ๋กœ ๊ฐ์‹ธ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (ํฌ์ธํ„ฐ์— ๊ฐ’์„ ์“ฐ๋Š” ๊ฒƒ์€ ๋ถ€์ˆ˜ํšจ๊ณผ์ด๊ธฐ ๋•Œ๋ฌธ) ํ•จ์ˆ˜๋Š” input guard๋กœ ๊ตฌํ˜„๋˜๊ณ  ๋งจ ๋ฐ‘์—์˜ where ์ ˆ์—์„œ ์ •์˜๋œ๋‹ค. ์„ฑ๊ณต์ ์ธ ๊ณ„์‚ฐ์€ 0์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋ถ€๋ถ„์ ์ธ ์„ฑ๊ณต์€ 1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ ๋ถ€๋™์†Œ์ˆ˜์  ๊ฐ’์„ ๊ทผ์‚ฟ๊ฐ’์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์™„์ „ํžˆ ์‹คํŒจํ•˜๋ฉด 2๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” alloca๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค. ํฌ์ธํ„ฐ๋“ค์ด ์ด๋ฏธ ํ˜ธ์ถœ์ž C ํ•จ์ˆ˜์—์„œ ํ• ๋‹น๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ํ•˜์Šค ์ผˆ ์ฝ”๋“œ๋ฅผ GHC๋กœ ์ปดํŒŒ์ผํ•  ์ˆ˜ ์žˆ๋‹ค. ghc -c fibonacci.hs C ์†Œ์Šค fibonacci.hs๋ฅผ ์ปดํŒŒ์ผํ•˜๋ฉด ๋ช‡ ๊ฐ€์ง€ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค. ๊ทธ์ค‘ fibonacci_stub.h๊ฐ€ ์žˆ๋Š”๋ฐ, fib.c์—์„œ ํฌํ•จ์‹œํ‚จ๋‹ค. #include <stdio.h> #include <stdlib.h> #include "fibonacci_stub.h" int main(int argc, char *argv[]) { if (argc < 2) { printf("Usage: %s <number>\n", argv[0]); return 2; } hs_init(&argc, &argv); const int arg = atoi(argv[1]); unsigned long long res; double approx; const int status = fibonacci_c(arg, &res, &approx); hs_exit(); switch (status) { case 0: printf("F_%d: %llu\n", arg, res); break; case 1: printf("Error: result is out of bounds\n"); printf("Floating-point approximation: %e\n", approx); break; case 2: printf("Error: result is out of bounds\n"); printf("Floating-point approximation is infinite\n"); break; default: printf("Unknown error: %d\n", status); } return status; } ์ฃผ๋ชฉํ•  ๊ฒƒ์€ ํ•˜์Šค ์ผˆ ํ™˜๊ฒฝ์„ hs_init์— ๋ฉ”์ธ ํ•จ์ˆ˜์˜ ๋ช…๋ น ์ค„ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜๋ฉฐ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ผ์ด ๋๋‚˜๋ฉด hs_exit()๋กœ ํ•˜์Šค์ผˆ์„ ์ข…๋ฃŒํ•œ๋‹ค. ๋‚˜๋จธ์ง€๋Š” ํ• ๋‹น๊ณผ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํ†ต์ƒ์ ์ธ C ์ฝ”๋“œ๋‹ค. ์ด C ์ฝ”๋“œ๋Š” C ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์•„๋‹ˆ๋ผ GHC๋กœ ์ปดํŒŒ์ผํ•ด์•ผ ํ•œ๋‹ค. ghc -no-hs-main fib.c fibonacci.o -o fib ์ด์ œ ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ…Œ์ŠคํŠธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ./fib 42 F_42: 267914296 $ ./fib 666 Error: result is out of bounds Floating-point approximation: 6.859357e+138 $ ./fib 1492 Error: result is out of bounds Floating-point approximation is infinite ./fib -1 fib: Prelude.(!!): negative index 5 ์ œ๋„ค๋ฆญ ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ์ •ํ˜•ํ™”๋œ ์ฝ”๋“œ๋Š” ๊ทธ๋งŒ ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/SYB TODO ์›๋ฌธ์ด ๋ฏธ์™„์„ฑ 1์—์„œ ์„ค๋ช…ํ•˜๋Š” "Scrap your boilerplate" ์ ‘๊ทผ๋ฒ•์€ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์†Œ์œ„ "์ œ๋„ค๋ฆญ" ํ•จ์ˆ˜๊ฐ€ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ œ๋„ค๋ฆญ ํ•จ์ˆ˜๋ผ ํ•จ์€ ์ƒ์„ฑ ๋˜๋Š” ๋ณ€๊ฒฝ๋˜๋Š” ํŠน์ • ๋ฐ์ดํ„ฐ ์ƒ์„ฑ์ž๋“ค์— ๋Œ€ํ•ด ์ถ”์ƒํ™”ํ•˜๋ฉด์„œ ํŠน์ • ํƒ€์ž…์— ๋Œ€ํ•œ ์˜ˆ์™ธ์‚ฌํ•ญ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์›๋ฌธ์ด ์ดํ•ด๊ฐ€ ์•ˆ ๋จ... The "Scrap your boilerplate" approach, "described" in [1], is a way to allow your data structures to be traversed by so-called "generic" functions: that is, functions that abstract over the specific data constructors being created or modified, while allowing for the addition of cases for specific types. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์ฝ”๋“œ์˜ ๋ชจ๋“  ๊ตฌ์กฐ์ฒด๋ฅผ ์ง๋ ฌํ™” ํ•˜๊ณ  ์‹ถ์ง€๋งŒ Data.Data.Data ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค(-XDeriveDataTypeable๋กœ ํŒŒ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ)์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋Š” ๋‹จ ํ•˜๋‚˜์˜ ์ง๋ ฌํ™” ํ•จ์ˆ˜๋งŒ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๋‹ค. ์ง๋ ฌํ™” ์˜ˆ์‹œ ๋ชฉํ‘œ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์Œ<NAME>์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. data Tag = Con String | Val String ํ•˜์Šค ์ผˆ AST๋“ค์˜ ๋น„๊ต haskell-src-exts ํŒจํ‚ค์ง€๋Š” ํ•˜์Šค์ผˆ์„ ๊ฝค๋‚˜ ๋ณต์žกํ•œ ๊ตฌ๋ฌธ ํŠธ๋ฆฌ๋กœ ํŒŒ์‹ฑ ํ•œ๋‹ค. ๊ฑฐ์˜ ๋™์ผํ•œ ๋‘ ์†Œ์Šค ํŒŒ์ผ์ด ๋™๋“ฑํ•œ์ง€ ํ™•์ธํ•œ๋‹ค๊ณ  ์น˜์ž. ์ถœ๋ฐœ์ : import System.Environment import Language.Haskell.Exts main = do -- parse the filenames given by the first two command line arguments, -- proper error handling is left as an exercise [ParseOk moduleA, ParseOk moduleB] <- mapM parseFile . take 2 =<< getArgs putStrLn $ if moduleA == moduleB then "Your modules are equal" else "Your modules differ" ํ…Œ์ŠคํŠธ๋ฅผ ์กฐ๊ธˆ ํ•ด๋ณด๋ฉด ์ด๋ฆ„๋งŒ ๋‹ค๋ฅด๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋™์ผํ•œ ๋‘ ํŒŒ์ผ์€ ๊ฐ™์ง€(==) ์•Š๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ์‹ค์„ ์ •์ •ํ•˜๋ฉด ๋งŽ์€ ๋ณด์ผ๋Ÿฌ ํ”Œ๋ ˆ์ดํŠธ์— ์˜์กดํ•˜์ง€ ์•Š์•„๋„ ์ œ๋„ค๋ฆญ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. From a bit of testing, it will be apparent that identical files with different names will not be equal to (==). However, to correct the fact, without resorting to lots of boilerplate, we can use generic programming: TODO describe using Data.Generics.Twins.gzip*? to write a function to find where there are differences? Or use it to write a variant of geq that ignores the specific cases that are unimportant (the SrcLoc elements) (i.e. syb doesn't allow generic extension... contrast it with other libraries?). Or just explain this hack (which worked well enough) to run before (==), or geq:: everyWhere (mkT $ \ _ -> SrcLoc "" 0 0) :: Data a => a -> a Or can we develop this into writing something better than sim_mira (for hs code), found here: http://dickgrune.com/Programs/similarity_tester/ 3 ํŠน์ˆ˜ ์ž‘์—… ํŠน์ˆ˜ ์ž‘์—… ๊ทธ๋ž˜ํ”ฝ ์œ ์ € ์ธํ„ฐํŽ˜์ด์Šค (GUI) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค XML ๋‹ค๋ฃจ๊ธฐ ์ˆ˜ํ•™์‹์˜ ํŒŒ์‹ฑ ๊ธฐ์ดˆ์ ์ธ ํƒ€์ž… ๊ฒ€์‚ฌ๊ธฐ์˜ ์ž‘์„ฑ ์›๋ฌธ ์—†์Œ 1 GUI (๊ฒ€ํ†  ์ค‘) ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/GUI TODO ์›๋ฌธ์ด ๋ฏธ์™„์„ฑ wxHaskell์˜ ์„ค์น˜ ๋ฐ ์‹คํ–‰ Debian๊ณผ Ubuntu๋ฅผ ์œ„ํ•œ ์ง€๋ฆ„๊ธธ Hello World ์ปจํŠธ๋กค ํ…์ŠคํŠธ ๋ ˆ์ด๋ธ” ๋ฒ„ํŠผ ๋ ˆ์ด์•„์›ƒ ์†์„ฑ ์†์„ฑ ์„ค์ • ๋ฐ ์ˆ˜์ • ์†์„ฑ์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ• ์ด๋ฒคํŠธ ํ•˜์Šค์ผˆ์—๋Š” ๊ทธ๋ž˜ํ”ฝ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๊ธฐ ์œ„ํ•œ ํˆดํ‚ท์ด ์ ์–ด๋„ 4๊ฐœ๊ฐ€ ์žˆ๋‹ค. wxHaskell - Windows, OS X ๋ฐ GNU/Linux Gtk+ ๋“ฑ์„ ์ง€์›ํ•˜๋Š” ํฌ๋กœ์Šค ํ”Œ๋žซํผ wxWidgets ํˆดํ‚ท์˜ ํ•˜์Šค ์ผˆ ์ธํ„ฐํŽ˜์ด์Šค Gtk2Hs - GTK+ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ•˜์Šค ์ผˆ ์ธํ„ฐํŽ˜์ด์Šค hoc (๋ฌธ์„œํ™”๋Š” sourceforge์—) - MacOS X์˜ Cocoa ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ•˜์Šค ์ผˆ to Objective-C ๋ฐ”์ธ๋”ฉ qtHaskell - Qt Widget ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์œ„ํ•œ ํ•˜์Šค ์ผˆ ๋ฐ”์ธ๋”ฉ wxHaskell์˜ ์„ค์น˜ ๋ฐ ์‹คํ–‰ wxHaskell์„ ์„ค์น˜ํ•˜๋ ค๋ฉด GNU/Linux, Mac, Windows ์ค‘ ๋งž๋Š” ๋ฒ„์ „์˜ ์ง€์นจ์„ ๋ณผ ๊ฒƒ. ์•„๋‹ˆ๋ฉด wxHaskell ๋‹ค์šด๋กœ๋“œ ํŽ˜์ด์ง€๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์„ค์น˜ ์ง€์นจ์„ ๋”ฐ๋ผ๊ฐ€๋ฉด ๋œ๋‹ค. wxHaskell์„ GHC์— ๋“ฑ๋กํ•˜์ง€ ์•Š์œผ๋ฉด ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜๋ผ. (Cabal์ด ์ž๋™์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค) wxHaskell ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” source.hs๋ฅผ ์ปดํŒŒ์ผํ•˜๋ ค๋ฉด ์ปค๋งจ๋“œ ๋ผ์ธ์„ ์—ด์–ด ๋‹ค์Œ์„ ์ž…๋ ฅํ•œ๋‹ค. ghc -package wx source.hs -o bin GHCi์˜ ๊ฒฝ์šฐ๋„ ์ฝ”๋“œ๋Š” ๋น„์Šทํ•˜๋‹ค. ghci -package wx ์ด๋Ÿฌ๋ฉด GHCi ์ธํ„ฐํŽ˜์ด์Šค ์•ˆ์—์„œ ํŒŒ์ผ๋“ค์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค. ์ „๋ถ€ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•˜๋ ค๋ฉด $wxHaskellDir/samples/wx๋กœ ๊ฐ€์„œ ($wxHaskellDir๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด wxHaskell์„ ์„ค์น˜ํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ๋‹ค) HelloWorld.hs๋ฅผ ๋กœ๋“œํ•˜๊ฑฐ๋‚˜ ์ปดํŒŒ์ผํ•ด ๋ณด๋ฉด ๋œ๋‹ค. ํƒ€์ดํ‹€์€ "Hello World!", File๊ณผ About์„ ํฌํ•จํ•˜๋Š” ๋ฉ”๋‰ด ๋ฐ”, ๋ฐ‘์—๋Š” "Welcome to wxHaskell"์ด๋ผ ์จ์ง„ ์ƒํƒœ ์ฐฝ์„ ํฌํ•จํ•˜๋Š” ์ฐฝ์„ ํ•˜๋‚˜ ๋„์šธ ๊ฒƒ์ด๋‹ค. ์ž‘๋™ํ•˜์ง€ ์•Š์œผ๋ฉด $wxHaskellDir/lib์˜ ๋‚ด์šฉ๋ฌผ์„ ghc ์„ค์น˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๋ณต์‚ฌํ•ด ๋ณผ ๊ฒƒ. Debian๊ณผ Ubuntu๋ฅผ ์œ„ํ•œ ์ง€๋ฆ„๊ธธ ์—ฌ๋Ÿฌ๋ถ„์˜ ์šด์˜์ฒด์ œ๊ฐ€ ๋ฐ๋น„์•ˆ ๋˜๋Š” ์šฐ๋ถ„ํˆฌ๋ผ๋ฉด ํ„ฐ๋ฏธ๋„์—์„œ ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค. sudo apt-get install g++ sudo apt-get install libglu-dev sudo apt-get install libwxgtk2.8-dev Hello World ๋‹ค์Œ์€ ๊ธฐ๋ณธ์ ์ธ "Hello World" ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. module Main where main :: IO () main = putStr "Hello World!" ์ด ํ”„๋กœ๊ทธ๋žจ์€ ๋ฌธ์ œ์—†์ด ์ปดํŒŒ์ผ๋˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ GUI๊ฐ€ ์ž‘๋™ํ•˜๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? ์ฒซ ๋ฒˆ์งธ๋กœ ํ•  ์ผ์€ wxHaskell ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ Graphics.UI.WX๋ฅผ ์ž„ํฌํŠธ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Graphics.UI.WXCore์— ์ข€ ๋” ๋งŽ์€ ๊ฒƒ์ด ์žˆ์ง€๋งŒ ์ง€๊ธˆ์€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. GUI๋ฅผ ์‹œ์ž‘ํ•˜๋ ค๋ฉด start gui๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ gui๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ํ•จ์ˆ˜ ์ด๋ฆ„์ด๋‹ค. ์ด ํ•จ์ˆ˜๋Š” IO ํƒ€์ž…์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค. module Main where import Graphics.UI.WX main :: IO () main = start gui gui :: IO () gui = do --GUI stuff ํ”„๋ ˆ์ž„์„ ๋งŒ๋“ค๋ ค๋ฉด ํƒ€์ž…์ด [Prop (Frame ())] -> IO (Frame ())์ธ frame ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. frame์€ "ํ”„๋ ˆ์ž„ ์š”์†Œ๋“ค"์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์•„์„œ ์ด์— ๋Œ€์‘ํ•˜๋Š” ํ”„๋ ˆ์ž„์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋‚˜์ค‘์— ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์ง€๋งŒ ์š”์†Œ(property)๋Š” ๋Œ€๊ฐœ ์†์„ฑ(attribute)๊ณผ ๊ฐ’(value)์˜ ์กฐํ•ฉ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๊ฐ€ ๊ด€์‹ฌ ์žˆ๋Š” ๊ฒƒ์€ ํƒ€์ดํ‹€์ด๋‹ค. ํƒ€์ดํ‹€์€ text ์†์„ฑ ์•ˆ์— ์žˆ๊ณ  ๊ทธ ํƒ€์ž…์€ (Textual w) => Attr w String์ด๋‹ค. ์ด๊ฒƒ์ด String ์†์„ฑ์ด๋ผ๋Š” ๊ฒŒ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋ณด์ž. gui :: IO (Frame ()) gui = do frame [text := "Hello World!"] (:=) ์—ฐ์‚ฐ์ž๋Š” ์†์„ฑ๊ณผ ๊ฐ’์„ ๋ฐ›์•„ ์š”์†Œ๋กœ ๊ฒฐํ•ฉํ•œ๋‹ค. frame์ด IO (Frame ())์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜๋ผ. start ํ•จ์ˆ˜์˜ ํƒ€์ž…์€ IO a -> IO ()์ด๋‹ค. gui์˜ ํƒ€์ž…์„ IO (Frame ())๋กœ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋‹จ์ˆœํžˆ return ()์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒŒ ๋” ๋‚˜์„ ๊ฒƒ์ด๋‹ค. ์ด์ œ ํƒ€์ดํ‹€์ด "Hello World!"์ธ ํ”„๋ ˆ์ž„์œผ๋กœ ๊ตฌ์„ฑ๋œ GUI๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋˜์—ˆ๋‹ค. ์™„์ „ํ•œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. module Main where import Graphics.UI.WX main :: IO () main = start gui gui :: IO () gui = do frame [text := "Hello World!"] return () ๊ฒฐ๊ณผ๋ฌผ์€ ์ด ์Šคํฌ๋ฆฐ์ˆ์ฒ˜๋Ÿผ ๋ณด์ผ ๊ฒƒ์ด๋‹ค. (๋ฆฌ๋ˆ…์Šค๋‚˜ MacOS X์—์„œ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ๋ณด์ผ ๊ฒƒ์ด๋‹ค.) ์ปจํŠธ๋กค ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” wxHaskell ๋ฌธ์„œ๋ฅผ ๊ฐ™์ด ์—ด์–ด๋‘๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ด ๋ฌธ์„œ๋Š” $wxHaskellDir/doc/index.html์—์„œ๋„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ…์ŠคํŠธ ๋ ˆ์ด๋ธ” ๋‹จ์ˆœํ•œ ํ”„๋ ˆ์ž„์€ ๋ณ„์ผ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด ์ ˆ์—์„œ๋Š” ๋” ๋งŽ์€ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ด๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ ˆ์ด๋ธ”๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ž. wxHaskell์—๋Š” label์ด ์žˆ์ง€๋งŒ ์ด๊ฒƒ์€ ๋ ˆ์ด์•„์›ƒ์— ๊ด€ํ•œ ๊ฒƒ์ด๋‹ค. ๋ ˆ์ด์•„์›ƒ์€ ๋‹ค์Œ ์ ˆ์—์„œ๋‚˜ ๋‹ค๋ฃฌ๋‹ค. ์ง€๊ธˆ ์ฐพ๋Š” ๊ฒƒ์€ staticText๋กœ์„œ Graphics.UI.WX.Controls์— ๋“ค์–ด์žˆ๋‹ค. staticText ํ•จ์ˆ˜๋Š” Window ๋ฐ ์ผ๋ จ์˜ ์š”์†Œ๋ฅผ ์ธ์ž๋กœ ์ทจํ•œ๋‹ค. ์šฐ๋ฆฌ์—๊ฒŒ ์œˆ๋„๊ฐ€ ์žˆ๋‚˜? ์žˆ๋‹ค. Graphics.UI.WX.Frame์„ ๋ณด๋ฉด Frame์ด ๊ทธ์ € ํŠน๋ณ„ํ•œ ์ข…๋ฅ˜์˜ ์œˆ๋„์— ๋Œ€ํ•œ ํƒ€์ž… ๋™์˜์–ด์ธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. gui ์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ ๋ฐ”๊ฟ”์„œ ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค๊ฒ ๋‹ค. gui :: IO () gui = do f <- frame [text := "Hello World!"] staticText f [text := "Hello StaticText!"] return () staticText ๊ฐ์ฒด๋„ text ์†์„ฑ์„ ๊ฐ€์ง„๋‹ค. ํ•œ๋ฒˆ ์‹œ๋„ํ•ด ๋ณด์ž. Hello StaticText! (winXP) ๋ฒ„ํŠผ ์ƒํ˜ธ์ž‘์šฉ์„ ์ถ”๊ฐ€ํ•ด ๋ณด์ž. ์ด๋ฒˆ์—๋Š” ๋ฒ„ํŠผ์ด๋‹ค. ๋ฒ„ํŠผ์˜ ๊ธฐ๋Šฅ์€ ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์ ˆ์—์„œ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ด์ง€๋งŒ ์ง€๊ธˆ๋„ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ˆˆ์— ๋ณด์ด๋Š” ๋ฌด์–ธ๊ฐ€๊ฐ€ ์ผ์–ด๋‚  ๊ฒƒ์ด๋‹ค. button์€ staticText์ฒ˜๋Ÿผ ์ปจํŠธ๋กค์˜ ์ผ์ข…์ด๋‹ค. Graphics.UI.WX.Controls์— ๋“ค์–ด์žˆ๋‹ค. ์ด๋ฒˆ์—๋„ ์œˆ๋„์™€ ์š”์†Œ๋“ค์˜ ๋ชฉ๋ก์ด ํ•„์š”ํ•˜๋‹ค. ํ”„๋ ˆ์ž„์„ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค. ๋ฒ„ํŠผ๋„ text ์†์„ฑ์„ ๊ฐ€์ง„๋‹ค. gui :: IO () gui = do f <- frame [text := "Hello World!"] staticText f [text := "Hello StaticText!"] button f [text := "Hello Button!"] return () ๋ฒ„ํŠผ๊ณผ StaticText์˜ ๊ฒน์นจ (winXP) GHCi๋กœ ๋กœ๋“œํ•ด ๋ณด๋ฉด (๋˜๋Š” GHC๋กœ ์ปดํŒŒ์ผํ•ด ๋ณด๋ฉด) ์ด๊ฒŒ ๋ญ๋žŒ? ๋ ˆ์ด๋ธ”์ด ๋ฒ„ํŠผ์„ ๋ฎ๊ณ  ์žˆ๋‹ค. ๊ณ ์ณ๋ณด์ž. ๋ ˆ์ด์•„์›ƒ ๋ ˆ์ด๋ธ”๊ณผ ๋ฒ„ํŠผ์ด ๊ฒน์น˜๋Š” ์ด์œ ๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ”„๋ ˆ์ž„์— ๋ ˆ์ด์•„์›ƒ์„ ์ง€์ •ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋“ค์€ Graphics.UI.WXCore.Layout์˜ ๋ฌธ์„œ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Graphics.UI.WXCore๋ฅผ ์ž„ํฌํŠธ ํ•  ํ•„์š”๋Š” ์—†๋‹ค. ๋ฌธ์„œ์—๋Š” widget ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์œ„์ ฏ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋ฅผ ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์“ฐ์—ฌ์žˆ๋‹ค. ๋งˆ์นจ ์œˆ๋„๋Š” ์œ„์ ฏ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ž ๊น, ์šฐ๋ฆฌ์—๊ฒ ์œˆ๋„๊ฐ€ ํ”„๋ ˆ์ž„๋ฐ–์— ์—†์ง€ ์•Š๋‚˜? ์•„๋‹ˆ๋‹ค. Graphics.UI.WX.Controls์—์„œ Control์ด๋ผ๊ณ  ์ ํ˜€์žˆ๋Š” ๊ฒƒ์„ ํ•˜๋‚˜ ๋ˆŒ๋Ÿฌ๋ณด๋ผ. Graphics.UI.WXCore.WxcClassTypes๋กœ ์ด๋™ํ•  ํ…๋ฐ, ์—ฌ๊ธฐ์„œ ์ปจํŠธ๋กค์ด ์œˆ๋„์˜ ํŠนํ™”๋œ ํƒ€์ž…์— ๋Œ€ํ•œ ํƒ€์ž… ๋™์˜์–ด๋ผ๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ๋Š” ์กฐ๊ธˆ ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค. gui :: IO () gui = do f <- frame [text := "Hello World!"] st <- staticText f [text := "Hello StaticText!"] b <- button f [text := "Hello Button!"] return () ์ด์ œ widget st์™€ widget b๋ฅผ ์ด์šฉํ•ด์„œ staticText์™€ button์˜ ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. layout์€ ํ”„๋ ˆ์ž„์˜ ์†์„ฑ์ด๋ฏ€๋กœ ๊ฑฐ๊ธฐ์„œ ์„ค์ •ํ•œ๋‹ค. gui :: IO () gui = do f <- frame [text := "Hello World!"] st <- staticText f [text := "Hello StaticText!"] b <- button f [text := "Hello Button!"] set f [layout := widget st] return () ๋ ˆ์ด์•„์›ƒ์„ ์ง€์ •ํ•œ StaticText (winXP) set ํ•จ์ˆ˜๋Š” ๋‚˜์ค‘์— ์†์„ฑ์— ๊ด€ํ•œ ์ ˆ์—์„œ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด ๋ณด์ž. ๋ญ๊ฐ€ ์ž˜๋ชป๋๋Š”์ง€ ์ด๋ฒˆ์—๋Š” staticText๋งŒ ๋ณด์ด๊ณ  button์€ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค. ์ด ๋‘˜์„ ๋ฌถ์„ ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•œ๋ฐ, ์ด๋ฅผ ์œ„ํ•ด ๋ ˆ์ด์•„์›ƒ ํ•ฉ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. row์™€ column์ด ์ ์ ˆํ•ด ๋ณด์ธ๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์€ ์ •์ˆ˜์™€ ๋ ˆ์ด์•„์›ƒ ๋ชฉ๋ก์„ ์ทจํ•œ๋‹ค. button๊ณผ staticText์˜ ๋ ˆ์ด์•„์›ƒ๋“ค์˜ ๋ชฉ๋ก์€ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ •์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ ์›์†Œ ๊ฐ„ ๊ฐ„๊ฒฉ์„ ์ง€์ •ํ•œ๋‹ค. ํ•œ๋ฒˆ ํ•ด๋ณด์ž. gui :: IO () gui = do f <- frame [text := "Hello World!"] st <- staticText f [text := "Hello StaticText!"] b <- button f [text := "Hello Button!"] set f [layout := row 0 [widget st, widget b] ] return () ํ–‰ ๋ ˆ์ด์•„์›ƒ (winXP) ์ •์ˆซ๊ฐ’์„ ์กฐ์ •ํ•˜๊ฑฐ๋‚˜ row๋ฅผ column์œผ๋กœ ๋ฐ”๊ฟ”๋ณด์ž. ์›์†Œ๋“ค์˜ ์ˆœ์„œ๋„ ๋ฐ”๊ฟ”๋ด์„œ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š” ๊ฑด์ง€ ๊ฐ์„ ์žก์•„๋ณด์ž. ๋ฆฌ์ŠคํŠธ์— widget b๋ฅผ ๋ช‡ ๋ฒˆ ๋” ๋„ฃ์–ด๋ณด๋ฉด ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚ ๊นŒ? ์—ฌ๊ธฐ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ƒ์ƒ๋ ฅ์— ๋ถˆ์„ ์ง€ํ”ผ๊ธฐ ์œ„ํ•œ ์—ฐ์Šต๋ฌธ์ œ๋“ค์ด ์žˆ๋‹ค. ๋ฌธ์„œ๋ฅผ ํ™œ์šฉํ•˜์ž. ์—ฐ์Šต๋ฌธ์ œ ์ฒดํฌ๋ฐ•์Šค ์ปจํŠธ๋กค์„ ์ถ”๊ฐ€ํ•œ๋‹ค. ์•„๋ฌด ์ž‘๋™๋„ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ํ–‰ ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” staticText์™€ button ๋‹ค์Œ์— ๋ณด์ด๊ฑฐ๋‚˜ ์—ด ๋ ˆ์ด์•„์›ƒ์˜ ๊ฒฝ์šฐ ๊ทธ ๋ฐ‘์— ๋ณด์ด๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. ์ฒดํฌ๋ฐ•์Šค๋„ text ์†์„ฑ์„ ๊ฐ€์ง„๋‹ค. row์™€ column์€ ๋ ˆ์ด์•„์›ƒ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ทจํ•ด์„œ ๋ ˆ์ด์•„์›ƒ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•œ๋‹ค. ์ด ์‚ฌ์‹ค์„ ํ™œ์šฉํ•ด ์ฒดํฌ๋ฐ•์Šค๊ฐ€ staticText์™€ button์˜ ์™ผ์ชฝ์— ๋‚˜ํƒ€๋‚˜๋ฉฐ staticText์™€ button์€ ํ•œ ์—ด์ด ๋˜๋„๋ก ํ•˜๋ผ. ๋ผ๋””์˜ค๋ฐ•์Šค ์ปจํŠธ๋กค์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๊ฒ ๋Š”๊ฐ€? ์ด์ „ ์—ฐ์Šต๋ฌธ์ œ์˜ ๋ ˆ์ด์•„์›ƒ์„ ๊ธฐ๋ฐ˜์œผ๋กœ checkbox, staticText, button์˜ ๋ฐ‘์— ์˜ต์…˜์ด ๋‘ ๊ฐœ(๋˜๋Š” ๊ทธ ์ด์ƒ)์ธ ๋ผ๋””์˜ค๋ฐ•์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๋ผ. ๋ฌธ์„œ๋ฅผ ๋ณผ ๊ฒƒ! boxed ํ•ฉ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋„ค ์ปจํŠธ๋กค์„ ๊ฐ์‹ธ๋Š” ๋ณด๋”์™€ staticText, button์„ ๊ฐ์‹ธ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ณด๋”๋ฅผ ์ƒ์„ฑํ•˜๋ผ. (๋…ธํŠธ: boxed ํ•ฉ์„ฑ ์ž๋Š” MacOS X์—์„œ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์œ„์ ฏ์˜ ์ƒํ˜ธ์ž‘์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” wsHaskell์˜ ๋ฒ„๊ทธ๋กœ ๋ณด์ธ๋‹ค.) ์ด ์—ฐ์Šต๋ฌธ์ œ๋“ค์„ ์™„๋ฃŒํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์—ฐ์Šต๋ฌธ์ œ๋“ค์˜ ํ•ด๋‹ต ์†์„ฑ ์ด์ œ ์ด๋Ÿฐ ์˜๋ฌธ์ด ๋“ค ๊ฒƒ์ด๋‹ค. "set ํ•จ์ˆ˜๋Š” ์–ด๋””์„œ ํŠ€์–ด๋‚˜์˜จ ๊ฑธ๊นŒ?" ๊ทธ๋ฆฌ๊ณ  "text๊ฐ€ ๋ฌด์–ธ๊ฐ€์˜ ์†์„ฑ์ธ์ง€ ์–ด๋–ป๊ฒŒ ์•Œ์•„๋‚ผ ๊ฒƒ์ธ๊ฐ€?" ๋‘˜ ๋‹ค ๊ทธ ํ•ด๋‹ต์€ wxHaskell์˜ ์†์„ฑ ์‹œ์Šคํ…œ์— ์žˆ๋‹ค. ์†์„ฑ ์„ค์ • ๋ฐ ์ˆ˜์ • wxHaskell ํ”„๋กœ๊ทธ๋žจ์—์„œ ์œ„์ ฏ์˜ ์†์„ฑ๋“ค์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€๋‹ค. ์ƒ์„ฑํ•  ๋•Œ: f <- frame [ text := "Hello World!" ] set ํ•จ์ˆ˜ ์ด์šฉ: set f [ layout := widget st ] set ํ•จ์ˆ˜๋Š” ๋‘ ์ธ์ž๋ฅผ ์ทจํ•œ๋‹ค. ํ•˜๋‚˜๋Š” w ํƒ€์ž…์˜ ๋ฌด์–ธ๊ฐ€์ด๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” w์˜ ์†์„ฑ๋“ค์ด๋‹ค. wxHaskell์—์„œ ์ด ์ธ์ž๋“ค์€ ์œ„์ ฏ๊ณผ ๊ทธ ์œ„์ ฏ์˜ ์†์„ฑ๋“ค์ด๋‹ค. alignment๋‚˜ textEntry์ฒ˜๋Ÿผ ์ƒ์„ฑํ•  ๋•Œ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ๋“ค์ด ์žˆ์ง€๋งŒ ๋‹ค๋ฅธ ๊ฒƒ๋“ค์€ ๋Œ€๋ถ€๋ถ„ ํ”„๋กœ๊ทธ๋žจ์˜ ์–ด๋–ค IO ํ•จ์ˆ˜์—์„œ๋“  ๊ทธ ์†์„ฑ์— ๋Œ€ํ•œ ์ฐธ์กฐ(set f [stuff]์˜ f)๋งŒ ์žˆ๋‹ค๋ฉด ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์†์„ฑ์€ ์„ค์ •๋ฟ ์•„๋‹ˆ๋ผ ํš๋“๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ํš๋“์€ get ํ•จ์ˆ˜๋กœ ํ•œ๋‹ค. gui :: IO () gui = do f <- frame [ text := "Hello World!" ] st <- staticText f [] ftext <- get f text set st [ text := ftext] set f [ text := ftext ++ " And hello again!" ] get์˜ ํƒ€์ž… ์‹œ๊ทธ๋„ˆ์ณ๋Š” w -> Attr w a -> IO a์ด๋‹ค. text๋Š” String ์†์„ฑ์ด๋ฏ€๋กœ ์šฐ๋ฆฌ๋Š” IO String์„ ์–ป์–ด์„œ ftext์— ๋ฐ”์ธ๋“œ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์ค„์€ ํ”„๋ ˆ์ž„์˜ ํ…์ŠคํŠธ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค. wxHaskell์—์„œ๋Š” ํŒŒ๊ดด์  ๊ฐฑ์‹ ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. set๊ณผ (:=)์„ ์‚ฌ์šฉํ•˜๋ฉด ์–ธ์ œ๋“ ์ง€ ์†์„ฑ์„ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ modify ํ•จ์ˆ˜๋ฅผ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. modify :: w -> Attr w a -> (a -> a) -> IO () modify w attr f = do val <- get w attr set w [ attr := f val ] modify๋Š” ๋จผ์ € ๊ฐ’์„ ์–ป์–ด ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ ํ›„ ๋‹ค์‹œ ์„ค์ •ํ•œ๋‹ค. ๋ถ„๋ช…ํžˆ ์šฐ๋ฆฌ๊ฐ€ ์ด๋Ÿฐ ์ƒ๊ฐ์„ ์ฒ˜์Œ์œผ๋กœ ํ•œ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. (:~)๋ผ๋Š” ์—ฐ์‚ฐ์ž๋ฅผ ๋ณด์ž. (:~)๋Š” ์†์„ฑ๊ณผ ํ•จ์ˆ˜๋ฅผ ์ทจํ•˜๊ธฐ ๋•Œ๋ฌธ์— set ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” ์›๋ณธ ๊ฐ’์ด ํ•จ์ˆ˜์— ์˜ํ•ด ์ˆ˜์ •๋œ ํ•œ ์š”์†Œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. gui :: IO () gui = do f <- frame [ text := "Hello World!" ] st <- staticText f [] ftext <- get f text set st [ text := ftext ] set f [ text :~ ++ " And hello again!" ] ๋žŒ๋‹ค ํ‘œ๊ธฐ๋ฅผ ํ†ตํ•ด ์ต๋ช… ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹์€ ์ƒํ™ฉ์ด๋‹ค. ์š”์†Œ๋ฅผ ์„ค์ • ๋˜๋Š” ์ˆ˜์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ์ž๊ฐ€ ๋‘ ๊ฐœ ๋” ์žˆ๋‹ค. (::=)์™€ (::~)์ด๋‹ค. ์ด๊ฒƒ๋“ค์€ (:=), (:~)์™€ ๊ฑฐ์˜ ๊ฐ™์ง€๋งŒ w -> orig ํƒ€์ž…์˜ ํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. w๋Š” ์œ„์ ฏ ํƒ€์ž…์ด๊ณ  orig๋Š” ์›๋ž˜์˜ "๊ฐ’" ํƒ€์ž…์ด๋‹ค. ((:=)์˜ ๊ฒฝ์šฐ a์ด๊ณ  (:~)์˜ ๊ฒฝ์šฐ a -> a) ์•„์ง์€ IO ํƒ€์ž…์ด ์•„๋‹Œ ์†์„ฑ๋“ค๋งŒ ๋ดค๊ณ  ์ด ํ•จ์ˆ˜๋“ค์— ํ•„์š”ํ•œ ์œ„์ ฏ๋“ค์€ ๋Œ€๊ฐœ IO ๋ธ”๋ก ์•ˆ์—์„œ๋งŒ ์œ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ๋“ค์€ ์ง€๊ธˆ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ์†์„ฑ์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ• ๋‘ ๋ฒˆ์งธ ์งˆ๋ฌธ์˜ ์ฐจ๋ก€๋‹ค. text๊ฐ€ ๋ชจ๋“  ์ปจํŠธ๋กค์˜ ์†์„ฑ์ด๋ผ๋Š” ๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ํŒ๋‹จํ• ๊นŒ? ๋ฌธ์„œ๋ฅผ ๋ณธ๋‹ค. ๋ฒ„ํŠผ์ด ๋ฌด์Šจ ์†์„ฑ๋“ค์„ ๊ฐ€์ง€๋Š”์ง€ ๋ณด์ž. Graphics.UI.WX.Controls ์ด ๋งํฌ๋ฅผ ๋ˆ„๋ฅด๋ฉด "Button"์ด ๋‚˜์˜จ๋‹ค. Button์ด Control์˜ ํ•œ ์ข…๋ฅ˜์— ๋Œ€ํ•œ ํƒ€์ž… ๋™์˜์–ด๋ผ๋Š” ๊ฒƒ๊ณผ ๋ฒ„ํŠผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์˜ ๋ชฉ๋ก์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ•จ์ˆ˜๋งˆ๋‹ค "์ธ์Šคํ„ด์Šค" ๋ชฉ๋ก์ด ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ button ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ Commanding -- Textual, Literate, Dimensions, Colored, Visible, Child, Able, Tipped, Identity, Styled, Reactive, Paint๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋Š” ๋ฒ„ํŠผ์ด ์ด ํด๋ž˜์Šค๋“ค์˜ ์ธ์Šคํ„ด์Šค์ž„์„ ๋œปํ•œ๋‹ค. ํด๋ž˜์Šค์™€ ํƒ€์ž… ์žฅ์„ ์ฝ์–ด๋ณผ ๊ฒƒ. ์ด๋Š” ์ด ๋ฒ„ํŠผ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค ํŠนํ™” ํ•จ์ˆ˜๋“ค์ด ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Textual์€ text ํ•จ์ˆ˜์™€ appendText ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ์–ด๋–ค ์œ„์ ฏ์ด Textual ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉด ๊ทธ ์œ„์ ฏ์ด text ์†์„ฑ์„ ๊ฐ€์ง„๋‹ค๋Š” ๋œป์ด๋‹ค. StaticText์—๋Š” ์ธ์Šคํ„ด์Šค ๋ชฉ๋ก์ด ์—†์ง€๋งŒ StaticText๋Š” ์—ฌ์ „ํžˆ Control์ด๋ฉฐ, Control์€ Window์˜ ํŠน์ • ์œ ํ˜•์— ๋Œ€ํ•œ ํƒ€์ž… ๋™์˜์–ด๋‹ค. Textual ํด๋ž˜์Šค๋ฅผ ๋ณด๋ฉด Window๊ฐ€ Textual์˜ ์ธ์Šคํ„ด์Šค๋ผ๊ณ  ์“ฐ์—ฌ์žˆ๋‹ค. ๋ฌธ์„œ์— ์˜ค๋ฅ˜๊ฐ€ ์กฐ๊ธˆ ์žˆ๋‹ค. ํ”„๋ ˆ์ž„์˜ ์†์„ฑ๋“ค์€ Graphics.UI.WX.Frame์— ์žˆ๋‹ค. ์ด๋ฒˆ์—๋„ ๋ฌธ์„œ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋Š”๋ฐ, Frame์ด HasImage๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•œ๋‹ค๊ณ  ์“ฐ์—ฌ์žˆ๋‹ค. wxHaskell์˜ ๊ตฌ ๋ฒ„์ „์—์„œ๋Š” ๋งž๋Š” ๋ง์ด์ง€๋งŒ ์ง€๊ธˆ์€ HasImage๊ฐ€ Pictured๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค. ์ด์™ธ์—๋„ Form, Textual, Dimensions, Colored, Able ๋“ฑ์ด ์žˆ๋‹ค. Textual๊ณผ Form์€ ์ด๋ฏธ ๋ดค๋‹ค. Form์˜ ์ธ์Šคํ„ด์Šค๋Š” ๋ชจ๋‘ layout ์†์„ฑ์„ ๊ฐ€์ง„๋‹ค. Dimensions๋Š” clientSize ์†์„ฑ์„ ํฌํ•จํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ์ด ์†์„ฑ์˜ ํƒ€์ž…์€ Size๋กœ์„œ sz๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. layout ์†์„ฑ์€ ํฌ๊ธฐ๋„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•  ๊ฒƒ. clientSize๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด layout ์ดํ›„์— ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. Colored๋Š” color์™€ bgcolor ์†์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. Able์€ enabled๋ผ๋Š” ๋ถˆ๋ฆฌ์–ธ ์†์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. ์ด๊ฒƒ์€ ํŠน์ • ํผ ์š”์†Œ๋ฅผ ํ™œ์„ฑํ™” ๋˜๋Š” ๋น„ํ™œ์„ฑํ™”ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋น„ํ™œ์„ฑํ™”๋œ ์š”์†Œ๋Š” ๋Œ€๊ฐœ ํšŒ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ๋‹ค. ์ด ๋ฐ–์—๋„ ๋งŽ์€ ์†์„ฑ์ด ์žˆ์œผ๋‹ˆ ํด๋ž˜์Šค๋งˆ๋‹ค ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋ณผ ๊ฒƒ. ์ด๋ฒคํŠธ ํŠน๋ณ„ํžˆ ์ฃผ๋ชฉํ•  ๋งŒํ•œ ํด๋ž˜์Šค๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ๋‹ค. ๋ฐ”๋กœ Reactive ํด๋ž˜์Šค์™€ Commanding ํด๋ž˜์Šค๋‹ค. ์ด ํด๋ž˜์Šค๋“ค์˜ ๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด ์†์„ฑ(Attr w a ๊ผด)์ด ์•„๋‹ˆ๋ผ ์ด๋ฒคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. Commanding ํด๋ž˜์Šค๋Š” command ์ด๋ฒคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๋ฒ„ํŠผ์„ ์ด์šฉํ•˜๊ฒ ๋‹ค. ๋‹ค์Œ์€ button๊ณผ staticText๊ฐ€ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ GUI๋‹ค. gui :: IO () gui = do f <- frame [ text := "Event Handling" ] st <- staticText f [ text := "You haven\'t clicked the button yet." ] b <- button f [ text := "Click me!" ] set f [ layout := column 25 [ widget st, widget b ] ] ์ด์ „ ์ƒํƒœ (winXP) button์„ ๋ˆ„๋ฅด๋ฉด staticText๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•œ๋‹ค. on ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. b <- button f [ text := "Click me!" , on command := --stuff ] on์˜ ํƒ€์ž…์€ Event w a -> Attr w a์ด๋‹ค. command์˜ ํƒ€์ž…์€ Event w (IO ())์ด๋ฏ€๋กœ IO ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. gui :: IO () gui = do f <- frame [ text := "Event Handling" ] st <- staticText f [ text := "You haven\'t clicked the button yet." ] b <- button f [ text := "Click me!" , on command := set st [ text := "You have clicked the button!" ] ] set f [ layout := column 25 [ widget st, widget b ] ] ์ด๋ฒคํŠธ ํ•„ํ„ฐ์— ๋Œ€ํ•œ ๋‚ด์šฉ ์ถ”๊ฐ€ ์š”๋ง 2 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค (๊ฒ€ํ†  ์ค‘) ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/Database TODO ์›๋ฌธ์ด ๋ฏธ์™„์„ฑ ์„œ๋ฌธ ์„ค์น˜ PostgreSQL ๋˜๋Š” SQLite ๋„ค์ดํ‹ฐ๋ธŒ MySQL ODBC/MySQL ์ผ๋ฐ˜์ ์ธ ์›Œํฌํ”Œ๋กœ ์—ฐ๊ฒฐ๊ณผ ์—ฐ๊ฒฐ ํ•ด์ œ ์ฟผ๋ฆฌ ์‹คํ–‰ SQL ๋ฌธ์˜ ์‹คํ–‰ Select Insert Update Delete ํŠธ๋žœ์žญ์…˜ ํ”„๋Ÿฌ์‹œ์ € ํ˜ธ์ถœ ์„œ๋ฌธ HDBC๋Š” ํ•˜์Šค์ผˆ์—์„œ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชจ๋“ˆ์ด๋‹ค. HDBC๋Š” ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋žจ๊ณผ SQL ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์ด์˜ ์ถ”์ƒ ๊ณ„์ธต์„ ์ œ๊ณตํ•œ๋‹ค. ์ด๋กœ์จ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฝ”๋“œ๋ฅผ ํ•˜์Šค ์ผˆ๋กœ ํ•œ ๋ฒˆ๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋งŽ์€ ๋ฐฑ์—”๋“œ SQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ•จ๊ป˜ ์ž‘๋™ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. HDBC๋Š” Perl์˜ DBI ์ธํ„ฐํŽ˜์ด์Šค์— ์–ด๋Š ์ •๋„ ๊ธฐ๋ฐ˜ํ•˜์ง€๋งŒ ํŒŒ์ด์ฌ์˜ DB-API v2, ์ž๋ฐ”์˜ JDBC, ํ•˜์Šค์ผˆ์˜ HSQL์—๋„ ์˜ํ–ฅ์„ ๋ฐ›์•˜๋‹ค. Perl์—์„œ DBI๊ฐ€ DBD๋ฅผ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ HDBC๊ฐ€ ์ž‘๋™ํ•˜๋ ค๋ฉด ๊ธฐ์ €์˜ ๋“œ๋ผ์ด๋ฒ„ ๋ชจ๋“ˆ์ด ํ•„์š”ํ•˜๋‹ค. HDBC ๋ฐฑ์—”๋“œ ๋“œ๋ผ์ด๋ฒ„๋กœ๋Š” PostgreSQL, SQLite, ODBC(์œˆ๋„์šฐ์ฆˆ์™€ ์œ ๋‹‰์Šค/๋ฆฌ๋ˆ…์Šค/๋งฅ ์šฉ)์ด ์žˆ๋‹ค. MySQL์€ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ์˜คํ”ˆ์†Œ์Šค ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ์„œ ๋“œ๋ผ์ด๋ฒ„๊ฐ€ HDBC-mysql (๋„ค์ดํ‹ฐ๋ธŒ), HDBC-odbc (ODBC) ์ด๋ ‡๊ฒŒ 2๊ฐœ ์กด์žฌํ•œ๋‹ค. MySQL ์‚ฌ์šฉ์ž๋Š” ๋ฆฌ๋ˆ…์Šค๋ฅผ ํฌํ•จํ•ด MySQL์„ ์ง€์›ํ•˜๋Š” ๋ชจ๋“  ํ”Œ๋žซํผ์—์„œ ODBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ODBC๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์˜ ์ด์ ์€ SQL ๋ฌธ์˜ ๋ฌธ๋ฒ•์ด ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—”์ง„๋“ค์˜ ์ฐจ์ด์ ์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๊ฐˆ์•„ํƒˆ ๋•Œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด์‹์ด ์‰ฝ๋‹ค. ์ด๋Š” ๋‹ค๋ฅธ ์ƒ์—…์šฉ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(์˜ค๋ผํด์ด๋‚˜ DB2 ๊ฐ™์€)์˜ ๊ฒฝ์šฐ์—๋„ ODBC๋ฅผ ์„ ํ˜ธํ•  ๊ทผ๊ฑฐ๊ฐ€ ๋œ๋‹ค. ์„ค์น˜ PostgreSQL ๋˜๋Š” SQLite ์ž์„ธํ•œ ์ •๋ณด๋Š” HDBC FAQ๋ฅผ ๋ณผ ๊ฒƒ. ๋„ค์ดํ‹ฐ๋ธŒ MySQL ๋„ค์ดํ‹ฐ๋ธŒ ODBC-mysql ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ฒฝ์šฐ ๋จผ์ € C MySQL ํด๋ผ์ด์–ธํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ์„ ๋ž˜ํ•‘ ํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ODBC/MySQL ODBC๋ฅผ ํ†ตํ•ด MySQL์ด HDBC์™€ ์ž‘๋™ํ•˜๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด ์ ˆ์ฐจ๊ฐ€ ๋งŽ๋‹ค. ํŠนํžˆ ๋ฃจํŠธ ๊ถŒํ•œ์ด ์—†๋Š” ๊ฒฝ์šฐ๋Š” ๋”์šฑ ๊ทธ๋ ‡๋‹ค. ํ”Œ๋žซํผ์ด ODBC ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด(๋Œ€๋ถ€๋ถ„ ๊ทธ๋Ÿด ๊ฒƒ์ด๋‹ค) Unix-ODBC๋ฅผ ์„ค์น˜ํ•œ๋‹ค. ์ž์„ธํ•œ ์ •๋ณด๋Š” ์—ฌ๊ธฐ๋ฅผ ๋ณผ ๊ฒƒ. MySQL-ODBC Connector๋ฅผ ์„ค์น˜ํ•œ๋‹ค. ์ž์„ธํ•œ ์ •๋ณด๋Š” ์—ฌ๊ธฐ์—. Database.HDBC ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•œ๋‹ค. Database.HDBC.ODBC ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•œ๋‹ค. mysql ๋“œ๋ผ์ด๋ฒ„๋ฅผ odbcinst.ini ํŒŒ์ผ($ODBC_HOME/etc/์— ์žˆ์Œ)์— ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฐ์ดํ„ฐ ์†Œ์Šค๋Š” $HOME/.odbc.ini์— ์ถ”๊ฐ€ํ•œ๋‹ค. ํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ์„ ์ œ์ž‘ํ•œ๋‹ค. ODBC ๋“œ๋ผ์ด๋ฒ„๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์„ค์น˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. export LD_LIBRARY_PATH=$ODBC_HOME/lib ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒŒ ์‹ซ๋‹ค๋ฉด ์Šคํƒœํ‹ฑ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์˜ต์…˜์„ ํ™œ์„ฑํ™”ํ•˜๊ณ  ODBC๋ฅผ ์ปดํŒŒ์ผํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋‹ค์Œ์—๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์—ฐ๊ฒฐํ•ด์„œ ๋ชจ๋“  ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณธ๋‹ค. ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ์„ ๋ž˜ํ•‘ ํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. module Main where import Database.HDBC.ODBC import Database.HDBC main = do c <- connectODBC "DSN=PSPDSN" xs <- getTables c putStr $ "tables "++(foldr jn "." xs)++"\n" where jn a b = a++" "++b ์ผ๋ฐ˜์ ์ธ ์›Œํฌํ”Œ๋กœ ์—ฐ๊ฒฐ๊ณผ ์—ฐ๊ฒฐ ํ•ด์ œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž‘์—…์˜ ์ฒซ๊ฑธ์Œ์€ ๋ชฉํ‘œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” ๋“œ๋ผ์ด๋ฒ„์— ํŠนํ™”๋œ connect API๋ฅผ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง€๋ฉฐ ๊ทธ ํƒ€์ž…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. String -> IO Connection connect API๋Š” connect ๋ฌธ์ž์—ด์„ ๋ฐ›์•„์„œ IO ๋ชจ๋‚˜๋“œ ์•ˆ์˜ Connection์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ์€ ์Šค์ฝ”ํ”„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜ ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ์„ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜ ํ•˜์ง€๋งŒ ๋ช…์‹œ์ ์œผ๋กœ ์—ฐ๊ฒฐ์„ ํ•ด์ œํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€ ์Šต๊ด€์ด๋‹ค. conn->Disconnect ์ฟผ๋ฆฌ ์‹คํ–‰ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ ๋Œ€๊ฐœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ˆ์ฐจ๋ฅผ ๋”ฐ๋ฅธ๋‹ค. ๊ตฌ๋ฌธ์„ ์ค€๋น„ํ•œ๋‹ค bind ๋ณ€์ˆ˜๋“ค๊ณผ ํ•จ๊ป˜ ๊ตฌ๋ฌธ์„ ์‹คํ–‰ํ•œ๋‹ค ๊ฒฐ๊ณผ ์ง‘ํ•ฉ(์žˆ๋‹ค๋ฉด)์„ fetch ํ•œ๋‹ค ๊ตฌ๋ฌธ์„ ์ข…๋ฃŒํ•œ๋‹ค HDBC๋Š” bind ๋ณ€์ˆ˜์™€ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ์œ„ํ•œ ์ˆ˜๋‹จ์œผ๋กœ [ SqlValue ]์™€ [ Maybe String ]์„ ์ง€์›ํ•œ๋‹ค. [ SqlValue ] ๋Œ€์‹  [ Maybe String ]์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” s๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. [ SqlValue ]๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํƒ€์ž… ์•ˆ์ •์„ฑ์ด ์•„์ฃผ ์ค‘์š”ํ•œ ๊ฒฝ์šฐ ๊ฐ•ํ•œ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด [ Maybe String ]์ด ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฟผ๋ฆฌ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์ข€ ๋” ํŽธํ•˜๋‹ค. [ Maybe String ]์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์ž๋™์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์—๋Š” ํผํฌ๋จผ์Šค ๋น„์šฉ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜๋ผ. ๊ฐ€๋”์€ ์ฟผ๋ฆฌ๊ฐ€ ๊ฐ„๋‹จํ•œ ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๊ฐ„์†Œํ™”๋œ API๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Run๊ณผ sRun์€ "์ค€๋น„ ๋ฐ ์‹คํ–‰"์˜ ๋ž˜ํผ๋‹ค. quickQuery๋Š” "์ค€๋น„, ์‹คํ–‰, ๋ชจ๋“  ์—ด fetch"์˜ ๋ž˜ํผ๋‹ค. SQL ๋ฌธ์˜ ์‹คํ–‰ Select ์›๋ฌธ ์—†์Œ Insert ์›๋ฌธ ์—†์Œ Update ์›๋ฌธ ์—†์Œ Delete ์›๋ฌธ ์—†์Œ ํŠธ๋žœ์žญ์…˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŠธ๋žœ์žญ์…˜์€ commit๊ณผ rollback์œผ๋กœ ์ œ์–ดํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋“ค์€(mysql ๊ฐ™์€) ํŠธ๋žœ์žญ์…˜์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜๋ผ. ๋”ฐ๋ผ์„œ ๋ชจ๋“  ์ฟผ๋ฆฌ๋Š” ๊ทธ ์ž์ฒด๋กœ ์›์ž์  ํŠธ๋žœ์žญ์…˜ ๋‚ด์— ์žˆ๋‹ค. HDBC๋Š” ์ฟผ๋ฆฌ ๊ทธ๋ฃน์— ๋Œ€ํ•œ ํŠธ๋žœ์žญ์…˜์„ ์ž๋™ํ™”ํ•˜๊ธฐ ์œ„ํ•œ withTransaction์„ ์ œ๊ณตํ•œ๋‹ค. ํ”„๋Ÿฌ์‹œ์ € ํ˜ธ์ถœ ์›๋ฌธ ์—†์Œ 3 XML ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/XML XML ํŒŒ์‹ฑ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค XML ์ƒ์„ฑ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค๋ฅธ ์„ ํƒ์ง€๋“ค HXT์— ์ต์ˆ™ํ•ด์ง€๊ธฐ XML ์ž‘์—…์„ ์œ„ํ•œ ํ•˜์Šค ์ผˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ์œผ๋ฉฐ HTML์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ์žˆ๋‹ค. ์›น์— ํŠนํ™”๋œ ์ž‘์—…์˜ ๊ฒฝ์šฐ Haskell/Web programming ์žฅ์„ ๋ณผ ๊ฒƒ. XML ํŒŒ์‹ฑ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค Haskell XML Toolbox๋Š” XML ํŒŒ์‹ฑ์„ ์œ„ํ•œ ๋„๊ตฌ ๋ชจ์Œ์œผ๋กœ์จ ๋‹ค๋ฅธ ๋„๊ตฌ๋“ค๋ณด๋‹ค ์ผ๋ฐ˜ํ™”๋œ ์ ‘๊ทผ๋ฒ•์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค. HaXml์€ ํ•˜์Šค์ผˆ์„ ์ด์šฉํ•ด XML ๋ฌธ์„œ์˜ ํŒŒ์‹ฑ, ํ•„ํ„ฐ๋ง, ๋ณ€ํ™˜, ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ชจ์Œ์ด๋‹ค. HXML์€ ๋ฌด๊ฒ€์ฆ, ๊ฒŒ์œผ๋ฅธ, ๊ณต๊ฐ„ ํšจ์œจ์ ์ธ ํŒŒ์„œ๋กœ์„œ HaXml์˜ ๋‹จ์ˆœ ๋Œ€์ฒด์žฌ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. xml-conduit์€ XML์˜ ํŒŒ์‹ฑ๊ณผ ๋ Œ๋”๋ง์„ ์ œ๊ณตํ•œ๋‹ค. ํŠœํ† ๋ฆฌ์–ผ์€ 1์„ ๋ณผ ๊ฒƒ. XML ์ƒ์„ฑ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ HSXML์€ XML ๋ฌธ์„œ๋ฅผ ์ •์  ํƒ€์ž… ์•ˆ์ „ s-expression์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. ๋‹ค๋ฅธ ์„ ํƒ์ง€๋“ค tagsoup์€ ๊ตฌ์กฐํ™”๋˜์ง€ ์•Š์€ HTML์„ ํŒŒ์‹ฑ ํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. ์ฆ‰ ๋ฐ์ดํ„ฐ์˜<NAME>์ด๋‚˜ well-formed ์œ ๋ฌด๋ฅผ ๊ฐ€์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค. HXT์— ์ต์ˆ™ํ•ด์ง€๊ธฐ ์—ฌ๊ธฐ์„œ๋Š” ์˜ˆ์‹œ๋ฅผ ์œ„ํ•ด Haskell XML Toolbox๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค. ๋จผ์ € GHC, GHCi๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•˜๊ณ  ์ง€์นจ์— ๋”ฐ๋ผ HXT๋„ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค. ๋ชจ๋‘ ๊ฐ–์ท„์œผ๋ฉด HXT๋ฅผ ๋‹ค๋ฃฐ ์ค€๋น„๊ฐ€ ๋œ ๊ฒƒ์ด๋‹ค. XML ํŒŒ์„œ๋ฅผ ์Šค์ฝ”ํ”„ ์•ˆ์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ณ  ๊ฐ„๋‹จํ•œ XML ํฌ๋งท ๋ฌธ์ž์—ด์„ ํŒŒ์‹ฑ ํ•ด๋ณด์ž. Prelude> :m + Text.XML.HXT.Parser.XmlParsec Prelude Text.XML.HXT.Parser.XmlParsec> xread "<foo>abc<bar/>def</foo>" [NTree (XTag (QN {namePrefix = "", localPart = "foo", namespaceUri = ""}) []) [NTree (XText "abc") [],NTree (XTag (QN {namePrefix = "", localPart = "bar", namespaceUri = ""}) []) [],NTree (XText "def") []]] HXT๊ฐ€ XML ๋ฌธ์„œ๋ฅผ ํŠธ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋…ธ๋“œ๋Š” ์„œ๋ธŒ ํŠธ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌํ•จํ•˜๋Š” XTag ๋˜๋Š” ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š” XText๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. GHCi๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ž์„ธํ•˜๊ฒŒ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ๋‹ค. Prelude> :m + Data.Tree.NTree.TypeDefs Prelude Text.XML.HXT.Parser.XmlParsec Text.XML.HXT.DOM> :i NTree data NTree a = NTree a (NTrees a) -- Defined in Data.Tree.NTree.TypeDefs Prelude Text.XML.HXT.Parser.XmlParsec Text.XML.HXT.DOM> :i NTrees type NTrees a = [NTree a] -- Defined in Data.Tree.NTree.TypeDefs NTree๋Š” ๋ฒ”์šฉ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ์„œ ๊ฐ ๋…ธ๋“œ๊ฐ€ ๋ฆฌ์ŠคํŠธ์— ์ž์‹๋“ค์„ ๋ณด๊ด€ํ•œ๋‹ค. ์ข€ ๋” ๋‘˜๋Ÿฌ๋ณด๋ฉด XML ๋ฌธ์„œ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋˜๋Š” XNode ํƒ€์ž…์— ๋Œ€ํ•œ ํŠธ๋ฆฌ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. data XNode = XText String | XCharRef Int | XEntityRef String | XCmt String | XCdata String | XPi QName XmlTrees | XTag QName XmlTrees | XDTD DTDElem Attributes | XAttr QName | XError Int String ์šฐ๋ฆฌ ์˜ˆ์ œ๋กœ ๋Œ์•„์˜ค๋ฉด HXT๊ฐ€ ์ž…๋ ฅ์„ ์„ฑ๊ณต์ ์œผ๋กœ ํŒŒ์‹ฑ ํ–ˆ์ง€๋งŒ ์‚ฌ๋žŒ์ด ์ฝ๊ธฐ์— ๋” ๋ช…๋ฃŒํ•œ ํ‘œํ˜„์„ ์›ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹คํ–‰ํžˆ๋„ DOM ๋ชจ๋“ˆ์ด ๊ทธ๋Ÿฐ ๊ฑธ ์ œ๊ณตํ•œ๋‹ค. xread๋Š” ํŠธ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ ํฌ๋งคํŒ… ํ•จ์ˆ˜๋Š” ๋‹จ์ผ ํŠธ๋ฆฌ์— ๋Œ€ํ•ด ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•  ๊ฒƒ. Prelude Text.XML.HXT.Parser.XmlParsec> :m + Text.XML.HXT.DOM.FormatXmlTree Prelude Text.XML.HXT.Parser.XmlParsec Text.XML.HXT.DOM> putStrLn $ formatXmlTree $ head $ xread "<foo>abc<bar/>def</foo>" ---XTag "foo" | +---XText "abc" | +---XTag "bar" | +---XText "def" ์ด ํ‘œํ˜„๋ฒ•์€ ๊ทธ ๊ตฌ์กฐ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋งŒ๋“ค๋ฉฐ ์ž…๋ ฅ ๋ฌธ์ž์—ด๊ณผ์˜ ๊ด€๊ณ„๋ฅผ ๋” ์ž˜ ๋ณด์—ฌ์ค€๋‹ค. ์šฐ๋ฆฌ์˜ XML ๋ฌธ์„œ์— ์†์„ฑ(attribute)์„ ์ถ”๊ฐ€ํ•ด์„œ ๋” ํ™•์žฅํ•ด ๋ณด์ž. (๋”ฐ์˜ดํ‘œ์— ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์— ์ฃผ์˜) Prelude Text.XML.HXT.Parser.XmlParsec> xread "<foo a1=\"my\" b2=\"oh\">abc<bar/>def</foo>" [NTree (XTag (QN {namePrefix = "", localPart = "foo", namespaceUri = ""}) [NTree (XAttr (QN {namePrefix = "", localPart = "a1", namespaceUri = ""})) [NTree (XText "my") []],NTree (XAttr (QN {namePrefix = "", localPart = "b2", namespaceUri = ""})) [NTree (XText "oh") []]]) [NTree (XText "abc") [],NTree (XTag (QN {namePrefix = "", localPart = "bar", namespaceUri = ""}) []) [],NTree (XText "def") []]] ์†์„ฑ์€ NTree ๋…ธ๋“œ๋กœ์„œ ๋ณด๊ด€๋˜๋ฉฐ ๊ทธ ๋‚ด์šฉ๋ฌผ์€ XAttr ํƒ€์ž…์ด๊ณ  ๋ฌผ๋ก  ์ž์‹์€ ์—†๋‹ค. ์œ„์—์„œ ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์ด ํ‘œํ˜„์„ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ถœ๋ ฅํ•ด ๋ณด์ž. ๋‹ค์Œ์€ XPath๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋‹จ์ˆœํ•œ ์˜ˆ์‹œ๋‹ค. Prelude> :set prompt "> " > :m + Text.XML.HXT.Parser.XmlParsec Text.XML.HXT.XPath.XPathEval > let xml = "<foo><a>A</a><c>C</c></foo>" > let xmltree = head $ xread xml > let result = getXPath "//a" xmltree > result > [NTree (XTag (QN {namePrefix = "", localPart = "a", namespaceUri = ""}) []) [NTree (XText "A") []]] > :t result > result :: NTrees XNode 4 ์ˆ˜ํ•™์‹์˜ ํŒŒ์‹ฑ (๊ฒ€ํ†  ์ค‘) ์›๋ฌธ: https://en.wikibooks.org/wiki/Haskell/ParseExps TODO ์˜ˆ์ œ ์ฝ”๋“œ๊ฐ€ ์ด์ƒํ•˜๋‹ค. ๊ธ€ ๋„์ค‘์— ์—ฐ์‚ฐ์ž๋“ค์ด ๋ฐ”๋€๋‹ค. ์›๋ฌธ์ด ์ด์ƒํ•œ ๋ฌธ์žฅ ๋ช‡ ๊ฐœ ๋ชธํ’€๊ธฐ Adaptation Structure Emerges ๊ณต๋ฐฑ๊ณผ applicative ํ‘œ๊ธฐ ๋ชจ๋“ˆํ™”ํ•˜๊ธฐ ์ด๋ฒˆ ์žฅ์—์„œ๋Š” "3*sin x + y" ๊ฐ™์€ ํ…์ŠคํŠธ๋ฅผ Plus (Times (Number 3) (Apply "sin" (Variable "x"))) (Variable "y") ๊ฐ™์€ ์ถ”์ƒ ๊ตฌ๋ฌธ ํ‘œํ˜„์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์„ ๋…ผํ•œ๋‹ค. Text.Parser.Combinators.ReadP๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๊ฐ™์ด ์—ด์–ด๋‘์ž. ๋ชธํ’€๊ธฐ import Text.ParserCombinators.ReadP ๋ชธํ’€๊ธฐ์˜ ์ผํ™˜์œผ๋กœ ์ข€ ๋” ์‰ฌ์šด ๋ฌธ์ œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. ๊ธฐํ˜ธ๊ฐ€ ๋ฌธ์ž o, ์—ฐ์‚ฐ์ž &, ๊ด„ํ˜ธ๋งŒ ์žˆ๋Š” ์–ธ์–ด๊ฐ€ ์žˆ๋‹ค. ํŠธ๋ฆฌ๋“ค์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ •์˜ํ•œ๋‹ค. data Tree = Branch Tree Tree | Leaf deriving Show ๋ฆฌํ”„๋“ค์˜ ํŒŒ์„œ๋Š” ReadP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์ •์˜๋œ๋‹ค. leaf = do char 'o' return Leaf & ์—ฐ์‚ฐ์ž๋กœ ๊ตฌ์„ฑ๋˜๋Š” ๋ธŒ๋žœ์น˜๋“ค์˜ ํŒŒ์„œ๋ฅผ ์ •์˜ํ•˜๋ ค๋ฉด ์—ฐ๊ด€ ๋ฒ•์น™์„ ์„ ํƒํ•ด์•ผ ํ•œ๋‹ค. ์ฆ‰ o&o&o๊ฐ€ (o&o)&o ๋˜๋Š” o&(o&o) ์ค‘ ๋ฌด์—‡๊ณผ ๊ฐ™์•„์•ผ ํ• ๊นŒ? ์—ฌ๊ธฐ์„œ๋Š” ํ›„์ž๋ฅผ ๊ณ ๋ฅด๊ฒ ๋‹ค. ์ผ๋‹จ ๊ด„ํ˜ธ๋Š” ์žŠ๊ณ  ์ฒซ ๋ฒˆ์งธ "๋งˆ์ผ์Šคํ†ค" ์ดํ›„์— ๋„ฃ๊ธฐ๋กœ ํ•œ๋‹ค. branch = do a <- leaf char '&' b <- tree return (Branch a b) tree = leaf +++ branch ๋ช‡ ๊ฐ€์ง€ ์ž…๋ ฅ์— ์ œ๋Œ€๋กœ ๋ฐ˜์‘ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ด ๋ณธ๋‹ค. *Main> readP_to_S tree "o" [(Leaf,"")] *Main> readP_to_S tree "o&o" [(Leaf,"&o"),(Branch Leaf Leaf,"")] *Main> readP_to_S tree "o&o&o" [(Leaf,"&o&o"),(Branch Leaf Leaf,"&o"),(Branch Leaf (Branch Leaf Leaf),"")] ์ž˜ ๋˜๋‹ˆ ๋‹ค์Œ์—๋Š” ๊ด„ํ˜ธ๋ฅผ ์ง€์›ํ•ด ๋ณด์ž. ๊ด„ํ˜ธ๋Š” ๋‚˜์ค‘์— ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฒ”์šฉ์ ์œผ๋กœ ์ •์˜๋œ๋‹ค. brackets p = do char '(' r <- p char ')' return r ์ด์ œ ๋ธŒ๋žœ์น˜ ํŒŒ์„œ์™€ ํŠธ๋ฆฌ ํŒŒ์„œ๊ฐ€ ๊ด„ํ˜ธ๋ฅผ ์ง€์›ํ•˜๋„๋ก ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค. branch = do a <- leaf +++ brackets tree char '&' b <- tree return (Branch a b) tree = leaf +++ branch +++ brackets tree ํ…Œ์ŠคํŠธํ•ด ๋ณด๋ฉด ์ž˜ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. *Main> readP_to_S tree "((o&((o&o)))&o&((o&o)&o)&o)" [(Branch (Branch Leaf (Branch Leaf Leaf)) (Branch Leaf (Branch (Branch (Branch Leaf Leaf) Leaf) Leaf)),"")] Adaptation ์ด์ฏค์ด adaptation์„ ์œ„ํ•œ ์ข‹์€ ์ถœ๋ฐœ์ ์ด๋‹ค. ์ตœ์ข… ๋ชฉํ‘œ๋ฅผ ํ–ฅํ•œ ์ฒซ ์ˆ˜์ •์‚ฌํ•ญ์€ ๊ฝค ์‰ฌ์šด ์ž‘์—…์ธ๋ฐ ๋ฆฌํ”„๋“ค์„ o์—์„œ ์ž„์˜์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ Leaf์—์„œ Leaf String์œผ๋กœ ๋ฐ”๊พธ๊ณ  leaf ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. TODO: adaptation์„ ๋ฌด์Šจ ์˜๋ฏธ๋กœ ์“ด ๊ฑด์ง€... data Tree = Branch Tree Tree | Leaf String deriving Show leaf = do s <- many1 (choice (map char ['a'..'z'])) return (Leaf s) ๋‹ค์Œ adaptation์œผ๋กœ &๋ณด๋‹ค ์•ฝํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋˜๋Š” ์—ฐ์‚ฐ์ž์ธ |๋ฅผ ๋„์ž…ํ•œ๋‹ค. ์ฆ‰ foo&bar|baz๋Š” (foo&bar)|baz๋กœ ํŒŒ์‹ฑ ๋˜์–ด์•ผ ํ•œ๋‹ค. ๋จผ์ € ๊ตฌ๋ฌธ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฐฑ์‹ ํ•œ๋‹ค. data Operator = And | Or deriving Show data Tree = Branch Operator Tree Tree | Leaf String deriving Show branch ํ•จ์ˆ˜๋ฅผ ๋ณต์ œํ•ด์„œ andBranch์™€ orBranch๋ฅผ ๋งŒ๋“ค๊ณ  left choice operator์ธ <++๋ฅผ ์ด์šฉํ•ด ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค. andBranch = do a <- leaf +++ brackets tree char '&' b <- tree return (Branch And a b) orBranch = do a <- leaf +++ brackets tree char '|' b <- tree return (Branch Or a b) tree = leaf +++ (orBranch <++ andBranch) +++ brackets tree ๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•˜๋ฉด ์ž˜ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. a&b&c&d|e&f&g&h|i&j&k|l&m&n&o|p&q&r|s ๊ฐ™์€ ํ‘œํ˜„์‹์„ X|Y|Z|W|P|Q๋ผ๋Š” ํŠธ๋ฆฌ์ธ๋ฐ(์ด ํŠธ๋ฆฌ๋Š” ์ด๋ฏธ ํŒŒ์‹ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค) ๋‹จ์ง€ ๋ฆฌํ”„๋“ค์ด ์ข€ ๋” ๋ณต์žกํ•œ ํ˜•ํƒœ(์—ญ์‹œ ์–ด๋–ป๊ฒŒ ํŒŒ์‹ฑ ํ• ์ง€ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋‹ค)๋ผ๊ณ  ์ƒ๊ฐํ•ด ๋ณด๋ฉด, ์ž˜ ์ž‘๋™ํ•˜๋Š” ํŒŒ์„œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. andBranch = do a <- leaf +++ brackets tree char '&' b <- andTree return (Branch And a b) andTree = leaf +++ brackets tree +++ andBranch orBranch = do a <- andTree +++ brackets tree char '|' b <- orTree return (Branch Or a b) orTree = andTree +++ brackets tree +++ orBranch tree = orTree ์ด ์ ‘๊ทผ๋ฒ•์€ ์ž˜ ์ž‘๋™ํ•˜์ง€๋งŒ *Main> readP_to_S tree "(foo&bar|baz)" [(Leaf "","(foo&bar|baz)"),(Branch Or (Branch And (Leaf "foo") (Leaf "bar")) (Leaf "baz"),""),(Branch Or (Branch And (Leaf "foo") (Leaf "bar")) (Leaf "baz"),"")] *Main> readP_to_S tree "(foo|bar&baz)" [(Leaf "","(foo|bar&baz)"),(Branch Or (Leaf "foo") (Branch And (Leaf "bar") (Leaf "baz")),""),(Branch Or (Leaf "foo") (Branch And (Leaf "bar") (Leaf "baz")),"")] ํŒŒ์‹ฑ์„ ๋ชจํ˜ธํ•˜๊ฒŒ ํ•œ๋‹ค. ํšจ์œจ์„ฑ ์ธก๋ฉด์—์„œ ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š๊ณ  ์šฐ๋ฆฌ๊ฐ€ ๋ฌด์–ธ๊ฐ€ ๋ถ€์ž์—ฐ์Šค๋Ÿฌ์šด ์ผ์„ ํ–ˆ์Œ์„ ์•”์‹œํ•œ๋‹ค. andTree์™€ orTree ํ•จ์ˆ˜ ๋‘˜ ๋‹ค ์•ˆ์— brackets tree๊ฐ€ ์žˆ๋Š”๋ฐ, orTree๋Š” andTree๋ฅผ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ ๋ชจํ˜ธํ•จ์ด ๋ฐœ์ƒํ•œ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ๊ทธ์ € backets tree๋ฅผ orTree์—์„œ ์ œ๊ฑฐํ•˜๋ฉด ๋œ๋‹ค. orTree = andTree +++ orBranch Structure Emerges ์ง€๊ธˆ๊นŒ์ง€์˜ ์†Œ์ผ๊ฑฐ๋ฆฌ๋กœ ์‚ฌ์‹ค์€ ์ตœ์ข… ํ”„๋กœ๊ทธ๋žจ ๊ตฌ์กฐ๊ฐ€ ์ƒ๋‹น ๋ถ€๋ถ„ ๋ช…๋ฃŒํ•ด์กŒ๋‹ค. ์ž‘์„ฑํ–ˆ๋˜ ๊ฒƒ์„ ๋Œ์•„๋ณด๋ฉด ์ƒˆ๋กœ์šด ์—ฐ์‚ฐ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ํ™•์žฅํ•˜๋Š” ๊ฒƒ์ด ๊ฝค๋‚˜ ์‰ฌ์› ๋‹ค. (๋…์ž๋ฅผ ์œ„ํ•œ ์—ฐ์Šต๋ฌธ์ œ: ์•„์ง ์ด ์ž‘์—…์ด ๋ช…๋ฃŒํ•˜์ง€ ์•Š๋‹ค๋ฉด ์•Œ์•„๋‚ด๋ณผ ๊ฒƒ) ์ž ์‹œ ๊ณ ๋ฏผํ•˜๋ฉด ์ด ํŒจํ„ด์„ ์™„์„ฑํ•ด์„œ ์ž„์˜์˜ ์—ฐ์‚ฐ์ž๋“ค์˜ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ถ”์ƒํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. operators = [(Or,'|'),(And,'&')] ๋˜๋Š” data Operator = Add | Mul | Exp deriving Show operators = [(Add,'+'),(Mul,'*'),(Exp,'^')] ์˜ฎ๊ธด์ด: ์ง€๊ธˆ๊นŒ์ง€๋Š” &, |๋ฅผ ์“ฐ๋‹ค๊ฐ€ ๊ฐ‘์ž๊ธฐ +, *๋กœ ๋ฐ”๋€ ๊ฒƒ์— ์ฃผ์˜ํ•˜์„ธ์š” ํŒŒ์„œ๋Š” ์ด๊ฒƒ์œผ๋กœ๋ถ€ํ„ฐ ๊ณ„์‚ฐ๋˜๊ณ  ์ค‘์ฒฉ์„ ํ†ตํ•ด(์ด์ „์— ์ง์ ‘ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ) ํŒŒ์‹ฑ์ด ๋ชจํ˜ธํ•จ ์—†์ด ์ผ์–ด๋‚˜์•ผ ํ•œ๋‹ค. ๋Šฅ์ˆ™ํ•œ ํ•˜์Šค ์ผˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋ผ๋ฉด ์ด๋ฏธ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋จธ๋ฆฟ์†์— ๋– ์˜ฌ๋ ธ์„ ๊ฒƒ์ด๋‹ค. tree = foldr (\(op, name) p -> let this = p +++ do a <- p +++ brackets tree char name b <- this return (Branch op a b) in this) (leaf +++ brackets tree) operators ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. *Main> readP_to_S tree "(x^e*y+w^e*z^e)" [(Leaf "","(x^e*y+w^e*z^e)"),(Branch Add (Branch Mul (Branch Exp (Leaf "x") (Leaf "e")) (Leaf "y")) (Branch Mul (Branch Exp (Leaf "w") (Leaf "e")) (Branch Exp (Leaf "z") (Leaf "e"))),"")] ์ด์ฏค์ด ์‰ฌ์–ด๊ฐ€๊ธฐ ์ข‹์€ ์ง€์ ์ด๋‹ค. ์ฒ˜์Œ์˜ ๊ทธ ํŒŒ์„œ๋Š” ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋˜์—ˆ๋‹ค. import Text.ParserCombinators.ReadP brackets p = do char '(' r <- p char ')' return r data Operator = Add | Mul | Exp deriving Show operators = [(Add,'+'),(Mul,'*'),(Exp,'^')] data Tree = Branch Operator Tree Tree | Leaf String deriving Show leaf = do s <- many1 (choice (map char ['a'..'z'])) return (Leaf s) tree = foldr (\(op, name) p -> let this = p +++ do a <- p +++ brackets tree char name b <- this return (Branch op a b) in this) (leaf +++ brackets tree) operators ๊ณต๋ฐฑ๊ณผ applicative ํ‘œ๊ธฐ functional/applicative ํ‘œ๊ธฐ์™€ ๊ณต๋ฐฑ ๋ฌด์‹œ๋Š” ๋‘˜ ๋‹ค ๋™์ผ ๋ฌธ์ž(๊ณต๋ฐฑ ๋ฌธ์ž)์— ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ๊ฒƒ์„ ๋จผ์ € ๊ตฌํ˜„ํ• ์ง€, ํ˜น์€ ๋ฌด์—‡์„ ๋จผ์ € ๊ตฌํ˜„ํ•˜๋˜ ์ƒ๊ณ ๋‚˜ ์—†์„์ง€๋ฅผ ๊ณ ๋ฏผํ•ด ๋ณด์ž. f x๋ผ๋Š” ํ‘œํ˜„์‹์„ ๊ณ ๋ คํ•˜๋ฉด applicative ํ‘œํ˜„์— ์•ž์„œ ๊ณต๋ฐฑ์„ ํŒŒ์‹ฑ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค. ํ•จ์ˆ˜ application์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋‚˜๋ฉด ๋‹จ์ˆœ ๋ณ‘์น˜(juxtaposition)์— (์˜๋„ํ•œ ๋Œ€๋กœ) ๋Œ€์‘ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ง€๊ธˆ์˜ ํŒŒ์„œ๊ฐ€ ๊ณต๋ฐฑ์„ ๋ฌด์‹œํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐ๋Š” ๊ธฐ์ˆ ์ ์ธ ์–ด๋ ค์›€์ด ์žˆ๋‹ค. skipWhitespace ํŒŒ์„œ๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ์น˜๊ณ  ๊ณต๋ฐฑ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ณณ์— ์ด๊ฑธ ๋ผ์›Œ ๋„ฃ์œผ๋ฉด ํŒŒ์„œ์— ๋ชจํ˜ธํ•จ์ด ๋ฒ”๋žŒํ•  ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ํŠน์ •ํ•œ ์ฃผ์š” ์ง€์ ๋“ค์—์„œ๋งŒ ๊ณต๋ฐฑ์„ ๋ฌด์‹œํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๊ณต๋ฐฑ์€ ํ•ญ์ƒ ํ† ํฐ์„ ์ฝ๊ธฐ ์ „์— ๋ฌด์‹œ๋˜๋Š” ๊ทœ์•ฝ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํŒŒ์„œ๋Š” " a + b * c "๋ฅผ "[ a][ +][ b][ *][ c][ ]"์ฒ˜๋Ÿผ ์ž๋ฅผ ๊ฒƒ์ด๋‹ค. ์–ด๋–ค ๊ทœ์•ฝ์„ ์„ ํƒํ• ์ง€๋Š” ์šฐ๋ฆฌ ๋งˆ์Œ๋Œ€๋กœ์ง€๋งŒ ๊ณต๋ฐฑ์„ ์•ž์„œ ๋ฌด์‹œํ•˜๋Š” ๊ฒƒ์ด ์ข€ ๋” ์˜๋ฆฌํ•ด ๋ณด์ธ๋‹ค. ๋ถˆํŽธํ•จ ์—†์ด " a"๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค. skipWhitespace = do many (choice (map char [' ','\n'])) return () ๊ทธ๋ฆฌ๊ณ  ์•ž์„œ ์ž‘์„ฑํ–ˆ๋˜ ๋ชจ๋“  ํŒŒ์‹ฑ์„ ์ˆ˜์ •ํ•˜์—ฌ ์ƒˆ ๊ทœ์•ฝ์„ ๋”ฐ๋ฅด๋„๋ก ํ•œ๋‹ค. brackets p = do skipWhitespace char '(' r <- p skipWhitespace char ')' return r leaf = do skipWhitespace s <- many1 (choice (map char ['a'..'z'])) return (Leaf s) tree = foldr (\(op, name) p -> let this = p +++ do a <- p +++ brackets tree skipWhitespace char name b <- this return (Branch op a b) in this) (leaf +++ brackets tree) operators applicative๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ง€์›ํ•˜๋ ค๋ฉด ๊ตฌ๋ฌธ๋„ ๊ทธ๊ฑธ ํ—ˆ์šฉํ•˜๋„๋ก ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. data Tree = Apply Tree Tree | Branch Operator Tree Tree | Leaf String deriving Show ์ด ๊ตฌ๋ฌธ ํŠธ๋ฆฌ๋Š” "(x + y) foo" ๊ฐ™์€ ๋ฌธ์žฅ์„ ํ—ˆ์šฉํ•˜์ง€๋งŒ "(f . g) x"์ฒ˜๋Ÿผ ํ•˜์Šค์ผˆ์—์„  ํ”ํ•œ ๊ตฌ๋ฌธ์€ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ์–ด๋–ค ๊ฒƒ์ด ์˜๋ฏธ ์žˆ๊ณ  ์–ด๋–ค ๊ฒƒ์ด ์•„๋‹Œ์ง€๋Š” ํƒ€์ž… ๊ฒ€์‚ฌ๊ธฐ๊ฐ€ ๊ฒฐ์ •ํ•  ๋ฌธ์ œ๋‹ค. ์ด๋Ÿฐ ๊ด€์‹ฌ์˜ ๋ถ„๋ฆฌ๋Š” ์šฐ๋ฆฌ์˜ ๋ฌธ์ œ(ํŒŒ์‹ฑ)๋ฅผ ๋‹จ์ˆœํ•˜๊ณ  ํ†ต์ผ๋˜๊ฒŒ<NAME>๋‹ค. This syntax tree will allow for sentences such as "(x + y) foo", while this not correct other sentences like "(f . g) x" are commonplace in haskell ์ด๊ฒŒ ๋จผ ์†Œ๋ฆฌ์ž„? ์šฐ๋ฆฌ์˜ ํŒŒ์„œ์˜ ๋ณธ์งˆ์€ leaf์™€ tree๋ผ๋Š” ๋‘ ํ•จ์ˆ˜๋‹ค. (skipWhitespace์™€ brackets๋Š” "๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ" ๋˜๋Š” ํ—ฌํผ ํ•จ์ˆ˜๋“ค๋กœ ์ทจ๊ธ‰) tree ํ•จ์ˆ˜๋Š” ์†Œ๋น„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์†Œ๋น„ํ•˜๊ณ  ๊ทธ๊ฒƒ๋“ค์— ๋ฆฌํ”„๋“ค์„ ์ตœ๋Œ€ํ•œ ๋ถ™์ธ๋‹ค. leaf ํ•จ์ˆ˜๋Š” ์—ฐ์‚ฐ์ž๋“ค ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๋ชจ๋“  ๊ฒƒ์„ ์ฝ๋Š”๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ด€์ ์—์„œ ํ”„๋กœ๊ทธ๋žจ์„ ๋ณผ ๋•Œ applicative ํ‘œ๊ธฐ๋ฅผ ์ง€์›ํ•˜๋ ค๋ฉด ๋ฆฌํ”„๋ฅผ ํ•จ์ˆ˜ ์ ์šฉ์˜ ์—ฐ์‡„๋ฅผ ํŒŒ์‹ฑ ํ•˜๋Š” ๋ฌด์–ธ๊ฐ€๋กœ ๊ต์ฒดํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒŒ ๋ช…๋ฐฑํ•ด์ง„๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹œ๋„๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. leaf = chainl1 (do skipWhitespace s <- many1 (choice (map char ['a'..'z'])) return (Leaf s)) (return Apply) ์•ž์„œ ์–ธ๊ธ‰ํ•œ "ํ”ํ•œ" ํ•ฉ์„ฑ ๊ตฌ๋ฌธ์„ ์ง€์›ํ•˜๋„๋ก ํ™•์žฅํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ๋‹ค. leaf = chainl1 (brackets tree +++ do skipWhitespace s <- many1 (choice (map char ['a'..'z'])) return (Leaf s)) (return Apply) ์ด๊ฑธ๋กœ ๋ฌธ์ œ๋ฅผ ์™„๋ฒฝํžˆ ํ•ด๊ฒฐํ–ˆ๋‹ค. ์›๋ž˜ ๋ชฉํ‘œ๋ฅผ ๋‹ฌ์„ฑํ•ด์„œ ์›ํ•˜๋Š” ์—ฐ์‚ฐ์ž๋“ค์„ (์ˆœ์„œ๋Œ€๋กœ) ๊ธฐ์ˆ ํ•˜๊ณ  Tree๋ฅผ ์ผ์ข…์˜ ์ˆ˜ํ•™์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ˆœํšŒ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. ์•Œ๋ ค์ง€์ง€ ์•Š์€ ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค๋˜๊ฐ€ ํ•˜๊ณ . ๋ชจ๋“ˆํ™”ํ•˜๊ธฐ ์•ž์„œ ์ž‘์„ฑํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค๋ฅธ ์ƒํ™ฉ์—์„œ ์“ฐ๊ธฐ์—๋„ ์ถฉ๋ถ„ํ•˜๊ฒŒ ๋ฒ”์šฉ์ ์ด๋‹ค. ํ•œ ๊ฐ€์ง€ ๋ชฉ์ ์„ ์œ„ํ•œ ๊ฒƒ์ด์–ด๋„ ๋” ํฐ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•  ๊ณ„ํš์ด๋ผ๋ฉด ๋‚ด๋ถ€์™€ ์™ธ๋ถ€(์ธํ„ฐํŽ˜์ด์Šค)๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ํ•„์ˆ˜๋‹ค. module Parser ( Tree(..), parseExpression ) where import Data.Maybe import Text.ParserCombinators.ReadP skipWhitespace = do many (choice (map char [' ','\n'])) return () brackets p = do skipWhitespace char '(' r <- p skipWhitespace char ')' return r data Tree op = Apply (Tree op) (Tree op) | Branch op (Tree op) (Tree op) | Leaf String deriving Show parseExpression operators = listToMaybe . map fst . filter (null .snd) . readP_to_S tree where leaf = chainl1 (brackets tree +++ do skipWhitespace s <- many1 (choice (map char ['a'..'z'])) return (Leaf s)) (return Apply) tree = foldr (\(op, name) p -> let this = p +++ do a <- p +++ brackets tree skipWhitespace char name b <- this return (Branch op a b) in this) (leaf +++ brackets tree) operators<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ### ๋ณธ๋ฌธ: SAS๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ๋‹ค๋ฃจ๋Š” ์ฑ…์ž…๋‹ˆ๋‹ค. SAS๋ฅผ ์ด์šฉํ•ด์„œ ํ†ต๊ณ„๋ถ„์„์„ ๋‹ค๋ฃจ๋Š” ์ฑ…์€ ๋งŽ์ด ์žˆ์ง€๋งŒ, SAS ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ(ํ•ธ๋“ค๋ง)๋‚˜ SQL ํ”„๋Ÿฌ์‹œ์ €(PROC SQL)๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ๋‹ค๋ฃฌ ์ฑ…์€ ์ฐพ๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. SAS ์‚ฌ์šฉ ์‹œ ๊ฑฐ์˜ ํ™œ์šฉํ•˜์ง€ ์•Š๋Š” ๊ตฌ๋ฌธ์„ ์ œ์™ธํ•˜๊ณ  ํ™œ์šฉ๋„๊ฐ€ ๋†’๊ณ  ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜, ๋ฌธ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ตœ๋Œ€ํ•œ ์›๋ฆฌ๋ฅผ ์ƒ์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋””ํ…Œ์ผํ•œ ๋ถ€๋ถ„์—์„œ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ถ€๋ถ„์ด ์—†๋„๋ก ๊ฐ„๋‹จํ•œ ๋ฌธ์žฅ์œผ๋กœ ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ฑ…๋“ค๊ณผ์˜ ์ฐจ๋ณ„์„ฑ์„ ์–˜๊ธฐํ•˜์ž๋ฉด ์ด ์ฑ…์€ ๋Œ€๋ถ€๋ถ„ ๋ฌธ๋ฒ• ์„ค๋ช…์—์„œ ๋™์ผํ•œ ์˜ˆ์ œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค(SASHELP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ CLASS ํ…Œ์ด๋ธ”). ๊ทธ ์ด์œ ๋Š” ์ฝ”๋”ฉ์„ ํ•  ๋•Œ ํ…Œ์ด๋ธ”์˜ ํŒŒ์•…ํ•˜๊ณ  ๋จธ๋ฆฟ์†์œผ๋กœ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋Š” ๊ฒŒ ์ค‘์š”ํ•œ๋ฐ ๋™์ผํ•œ ์˜ˆ์ œ๋กœ ์—ฐ์Šต์„ ํ•  ๊ฒฝ์šฐ ๊ทธ ๊ณผ์ •์„ ๋น ๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฝ”๋”ฉ ์ดํ•ด๋„ ๋น ๋ฅผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. CLASS ํ…Œ์ด๋ธ”์€ SAS ์„ค์น˜ ์‹œ ๊ธฐ๋ณธ์œผ๋กœ ์„ค์น˜๋˜๋Š” ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ํ–‰์˜ ์ˆ˜๊ฐ€ 20๊ฐœ ์ •๋„๋ผ์„œ ํ•œ๋ˆˆ์— ํŒŒ์•…ํ•˜๊ธฐ ์‰ฌ์šฐ๋ฉฐ ๋‹ค์–‘ํ•œ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด ๋ณด์ด๊ธฐ์—๋„ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ดˆ๋ณด์ž๋“ค์ด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํ”ผ๋“œ๋ฐฑ์€ ์–ธ์ œ๋“  ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. 1.SAS์˜ ๊ธฐ์ดˆ SAS ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ ๊ฐœ๋…์€ โ€˜ํ…Œ์ด๋ธ”โ€™ ๊ณผ โ€˜๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌโ€™์ž…๋‹ˆ๋‹ค. SAS๋ฟ๋งŒ ์•„๋‹ˆ๋ผ R์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ํ•  ๋•Œ๋„ โ€˜ํ…Œ์ด๋ธ”โ€™๊ณผ โ€˜๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌโ€™ ๊ฐœ๋…์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. 1-1. ํ…Œ์ด๋ธ”๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ…Œ์ด๋ธ”: ์—‘์…€๊ณผ ๊ฐ™์ด ํ–‰๊ณผ ์—ด๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๋ฆฌ๋œ ํŒŒ์ผ๋ช… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ: ํ…Œ์ด๋ธ”์ด ์†ํ•œ ํด๋”๋ช… ๊ฐ„๋‹จํ•˜๊ฒŒ ์ด์•ผ๊ธฐํ•ด์„œ โ€˜ํ…Œ์ด๋ธ”โ€™=โ€˜ํŒŒ์ผ๋ช…โ€™, โ€˜๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌโ€™=โ€˜ํด๋”๋ช…โ€™์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด sashelp.class๋กœ ๋˜์–ด์žˆ๋‹ค๋ฉด sashelp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ class ํ…Œ์ด๋ธ”์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ต์ˆ™ํ•˜๊ฒŒ ์ ‘ํ•˜๋Š” ์œˆ๋„์—์„œ ํ™œ์šฉํ•˜๋Š” ํŒŒ์ผ๊ณผ ํด๋”๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์œˆ๋„์™€ ๊ฑฐ์˜ ์œ ์‚ฌํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถ„์„ ์„ธ๊ณ„์—์„œ๋Š” ํ…Œ์ด๋ธ”๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„์„ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ ๋ถ€๋ถ„์€ ํ…Œ์ด๋ธ” ์ƒ์„ฑ๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ƒ์„ฑ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ(ํ…Œ์ด๋ธ”)์ด ์žˆ์–ด์•ผ ๋ถ„์„ํ•  ๋Œ€์ƒ์ด ์žˆ๊ณ , ์ด ํŒŒ์ผ์„ ์ €์žฅํ•  ํด๋”(๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ) ๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์œˆ๋„๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ. doc, .hwp ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ณ , ์ด ํ…Œ์ด๋ธ”์„ ์ €์žฅํ•  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šฐ๊ฒ ์Šต๋‹ˆ๋‹ค. tip ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํŠน๋ณ„ํ•˜๊ฒŒ ์„ค์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ โ€˜WORKโ€™๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. tip SAS ํ”„๋กœ๊ทธ๋žจ์€ ๋ช…๋ น์–ด๋ฅผ ์ฝ์–ด๋“ค์ผ ๋•Œ ์œ„์—์„œ๋ถ€ํ„ฐ ์ฝ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ฝ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ฑ…์„ ์ฝ๋“ฏ์ด ์ˆœ์„œ๋Œ€๋กœ ๋ช…๋ น์–ด๋ฅผ ์ฝ์–ด๋“ค์ธ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฝ”๋”ฉ์„ ํ•˜์‹ค ๋•Œ ์ค‘์š”ํ•œ ์š”์†Œ์ž…๋‹ˆ๋‹ค. ๋จผ์ € ์ˆ˜ํ–‰ํ•˜๊ธธ ๋ฐ”๋ผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์œ„์ชฝ์— ๋ฐฐ์น˜ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด ์•Œ์•„๋ณด๊ธฐ ๋ช…๋ น์–ด * data xxx: ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. xxx๋ผ๋Š” ์ด๋ฆ„์˜ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. * set yyy: ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. yyy๋ผ๋Š” ์ด๋ฆ„์„ ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. * run: SAS ์ฝ”๋”ฉ์˜ ๋์„ ์•Œ๋ ค์ฃผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์ด ๋ช…๋ น์–ด๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ SAS๊ฐ€ ์ „์ฒด ์ฝ”๋”ฉ์ด ๋๋‚œ ๊ฒƒ์„ ์ธ์‹ํ•˜๊ณ  ๋ช…๋ น์–ด๋“ค์„ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. * โ€˜;โ€™ : ๊ฐ ๋ช…๋ น์–ด ๋ฌธ์žฅ์˜ ๋์„ ์•Œ๋ ค์ฃผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. โ€˜runโ€™์ด ์ „์ฒด ์ฝ”๋”ฉ์ด ๋๋‚œ ๊ฒƒ์„ ์•Œ๋ ค์ค€๋‹ค๋ฉด โ€˜;โ€™์€ ๋ฌธ์žฅ ์ฝ”๋”ฉ์ด ๋๋‚œ ๊ฒƒ์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ๋น„์œ ํ•˜์ž๋ฉด โ€˜runโ€™์€ ๋ฌธ๋‹จ์˜ ๋, โ€˜;โ€™์€ ๋ฌธ์žฅ์˜ ๋์„ ์•Œ๋ ค์ฃผ๋Š” ๋ช…๋ น์–ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. * โ€˜ โ€™(์ŠคํŽ˜์ด์Šค ๊ณต๊ฐ„): SAS๋Š” ๊ฐ ๋ช…๋ น์–ด๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ โ€˜ โ€™(๊ณต๋ฐฑ:์ŠคํŽ˜์ด์Šค)์œผ๋กœ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด ๊ฐ„ ๊ตฌ๋ถ„์„ ํ•  ๋•Œ ์ŠคํŽ˜์ด์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์—ฌ๋„ ๊ด€๊ณ„์—†์Šต๋‹ˆ๋‹ค. * /* */: ์ฃผ์„์ž…๋‹ˆ๋‹ค. ์ฃผ์„์€ ์ฝ”๋“œ์˜ ์•ž๋’ค์— ๋ถ™์–ด์„œ ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์„ค๋ช…ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ์ฝ”๋“œ ์‹คํ–‰์—๋Š” ์ „ํ˜€ ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ์—์„œ ์ฃผ์„์ด ๋ถ™์€ ์ฑ„๋กœ ์‹คํ–‰์„ ํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ DATA TEST; /*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•จ(DATA)*/ SET SASHELP.CLASS; /*๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ sashelp์˜ class๋ผ๋Š” ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ด*/ RUN; /*๋ชจ๋“  ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒ*/ ์ด๋ฆ„ ์„ฑ๋ณ„ ๋‚˜์ด ํ‚ค(๋‹จ์œ„: ์ธ์น˜) ๋ชธ๋ฌด๊ฒŒ(๋‹จ์œ„: ํŒŒ์šด๋“œ) ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์ž๋„ท์—ฌ 15 62.5 112.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กด ๋‚จ 12 59 99.5 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ฃผ๋””์—ฌ 14 64.3 90 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ช…๋ น์–ด๋Š” โ€˜๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ช….ํ…Œ์ด๋ธ”๋ช…โ€™ ์œผ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์šฐ๋ฆฌ๊ฐ€ ์ต์ˆ™ํ•œ ์œˆ๋„์—์„œ C:\SASHELP\CLASS.TXT ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์„ SAS์—์„œ ํ•˜์ž๋ฉด โ€˜SASHELP.CLASSโ€™ ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. SAS๋Š” โ€˜.TXT' ๋‚˜ '.HWPโ€™ ๊ฐ™์€ ํ™•์žฅ์ž๋ช…์ด ์—†๊ณ  ํ–‰๊ณผ ์—ด๋กœ ์ด๋ค„์ง„ ์—‘์…€์ฒ˜๋Ÿผ ๋‹จ์ผํ•œ ํ˜•ํƒœ๋กœ ์ด๋ค„์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ™•์žฅ์ž๋ช…์„ ์“ฐ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. tip ์˜ˆ์ œ์—์„œ ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ” TEST ์•ž์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด์ฒ˜๋Ÿผ ํ…Œ์ด๋ธ” ์•ž์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ช…์„ ์ง€์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ SAS๋Š” ์ž๋™์œผ๋กœ WORK ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ํ…Œ์ด๋ธ” TEST๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. 1-2. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ƒ์„ฑ ๋ฒ• ์ด์ œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์œˆ๋„ ํ™”๋ฉด์—์„œ ํด๋”๋ช…๊ณผ ๋™์ผํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ(ํ…Œ์ด๋ธ”)์„ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด ํด๋”(๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ)์— ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•˜๋ฉด ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ƒ์„ฑ์„ ์œ„ํ•œ ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด LIBNAME XXX YYY: ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ช… XXX์˜ ์œ„์น˜๋ฅผ YYY๋กœ ์ง€์ •ํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์˜ˆ์ œ LIBNAME SASTEST โ€œC:\SAS\BASEโ€; /*๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ช… โ€˜SASTESTโ€™๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ ๊ทธ ์œ„์น˜๋Š” โ€œC:\SAS\BASEโ€๋กœ ํ•จ*/ tip ๋งŒ์•ฝ SAS ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ๋ฅผ ์‚ฌ์šฉํ•˜์‹ ๋‹ค๋ฉด YYY์˜ ์œ„์น˜์— ๋„คํŠธ์›Œํฌ ์ƒ์˜ ํด๋”๋ช…์„ ์ž…๋ ฅํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค(์˜ˆ: \PGM\SASTEST) 2. PROC ์‚ฌ์šฉ๋ฒ• ์ด์ œ ๊ตฌ์ฒด์ ์ธ ํ…Œ์ด๋ธ” ํ™œ์šฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ–‰๊ณผ ์—ด๋กœ ์ด๋ค„์ง„ ์—‘์…€๊ณผ ๊ฐ™์€ ํŒŒ์ผ์ด ํ…Œ์ด๋ธ”์ด๋ผ๊ณ  ์ด์ „ ์žฅ์—์„œ ์„ค๋ช…์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. PROC ๋ช…๋ น์–ด๋Š” ํ…Œ์ด๋ธ”์„ ์ด๋ฆฌ์ €๋ฆฌ ์กฐํ•ฉํ•˜๊ณ  ๋‹ค๋ฃจ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. PROC์€ PROCEDURE(์ ˆ์ฐจ)๋ฅผ ์ค„์ธ ๋ง์ž…๋‹ˆ๋‹ค. โ€˜PROCโ€™ ๋’ค์— ์‚ฌ์šฉํ•  SAS ๊ธฐ๋Šฅ๋ช…์„ ๋ถ™์—ฌ์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜PROC PRINTโ€™, โ€˜PROC SQLโ€™์ฒ˜๋Ÿผ โ€˜PROC XXXโ€™ ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋”ฉ์„ ํ•ฉ๋‹ˆ๋‹ค. โ€˜PROC PRINTโ€™๋ผ๊ณ  ๋ช…๋ น์–ด๋ฅผ ์“ด๋‹ค๋ฉด ์ด๋Š”, โ€˜SAS์˜ PRINT ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค.โ€™๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ž์ฃผ ์“ฐ์ด๋Š” ๊ฐ„๋‹จํ•œ PROC์˜ ๊ธฐ๋Šฅ์„ ์‰ฌ์šด ์˜ˆ์ œ๋กœ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž์ฃผ ํ™œ์šฉ๋˜๋ฉด์„œ ๊ฐ„๋‹จํ•œ PROC ๋ช…๋ น์–ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. PROC SORT PROC PRINT PROC CONTENTS PROC FREQ 2-1. PROC SORT(์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ) ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด PROC SORT: SORT ํ”„๋Ÿฌ์‹œ์ €๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. DATA=XXX: XXX๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. OUT=YYY: ๊ฒฐ๊ด๊ฐ’์„ YYY๋ผ๋Š” ํ…Œ์ด๋ธ”๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. BY ZZZ: ๋ณ€์ˆ˜ ZZZ๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. DESCENDING: ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ PROC SORT DATA=SASHELP.CLASS OUT=TEST; /*SORT PROCEDURE๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ(PROC SORT) SASHELP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(SASHELP.)์˜ CLASS ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ (DATA=) ์ •๋ ฌํ•œ ๊ฒฐ๊ด๊ฐ’์€ WORK ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ โ€˜TESTโ€™ํ…Œ์ด๋ธ”๋กœ ์ €์žฅํ•จ.*/ BY AGE; /*AGE๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•จ(BY๋กœ ์ธํ•จ)*/ RUN; /*๋ชจ๋“  ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒ*/ ์ด๋ฆ„ ์„ฑ๋ณ„ ๋‚˜์ด ํ‚ค(๋‹จ์œ„: ์ธ์น˜) ๋ชธ๋ฌด๊ฒŒ(๋‹จ์œ„: ํŒŒ์šด๋“œ) ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์กด ๋‚จ 12 59 99.5 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ฃผ๋””์—ฌ 14 64.3 90 ์ž๋„ท์—ฌ 15 62.5 112.5 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋งŒ์•ฝ โ€˜OUT=TESTโ€™ ๋ช…๋ น์–ด๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค๋ฉด โ€˜DATA=SASHELP.CLASSโ€™์—์„œ ์ •๋ ฌ์„ ์‹œํ–‰ํ•˜๊ณ  ๊ทธ๋Œ€๋กœ CLASS ํ…Œ์ด๋ธ”์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค(๋‹จ, SAS ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ์—์„œ๋Š” SASHELP ํ…Œ์ด๋ธ”์€ ํŠน์ˆ˜ ๊ถŒํ•œ์ด ์žˆ์–ด์•ผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SASHELP ํ…Œ์ด๋ธ”์€ SAS ๋ช…๋ น์–ด๋ฅผ ์—ฐ์Šตํ•˜๊ธฐ ์ข‹์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์œผ๋ฏ€๋กœ ์ˆ˜์ •ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.) PROC SORT๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌํ•  ๋•Œ, ๋ณ€์ˆ˜ XXX๊ฐ€ ์ˆซ์ž ๋ณ€์ˆ˜์ด๋ฉด ์ž‘์€ ์ˆ˜๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž ๋ณ€์ˆ˜์ด๋ฉด ASCII ์ฝ”๋“œ์— ํ‘œ์‹œ๋œ ๊ฐ’์— ๋”ฐ๋ผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค. ํ•œ๊ธ€์ด๋ฉด ๊ฐ€๋‚˜๋‹ค์ˆœ์œผ๋กœ, ์˜์–ด๋ฉด ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค. ํ•œ๊ธ€๊ณผ ์˜์–ด๊ฐ€ ์„ž์—ฌ์žˆ๋‹ค๋ฉด ์˜์–ด๊ฐ€ ๋จผ์ € ๋‚˜์˜ค๊ณ  ๋‹ค์Œ์œผ๋กœ ํ•œ๊ธ€์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์˜ค๋ฆ„์ฐจ์ˆœ๊ณผ ๋‚ด๋ฆผ์ฐจ์ˆœ BY ์ดํ›„ ๋ณ€์ˆ˜ AGE ์•ž์— DESCENDING ๋ช…๋ น์–ด๋ฅผ ๋„ฃ๋Š”๋‹ค๋ฉด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. BY ์ดํ›„ ๋ณ€์ˆ˜ AGE ์•ž์— ASCENDING ๋ช…๋ น์–ด๋ฅผ ๋„ฃ์œผ๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค. ASCENDING ๋ช…๋ น์–ด๋Š” ๊ธฐ๋ณธ ์˜ต์…˜์œผ๋กœ ์„ค์ •๋ผ ์žˆ์–ด์„œ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค. BY์™€ AGE ์‚ฌ์ด์— ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ณ€์ˆ˜ AGE๋Š” ์ž๋™์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. PROC SORT DATA=TEST;/*SORT PROCEDURE๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ(PROC SORT) WORK ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(SASHELP.)์˜ CLASS ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ด(DATA=).*/ BY DESCENDING AGE;/*AGE๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•จ(BY, DESCENDING)*/ RUN;/*๋ชจ๋“  ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒ*/ ์ด๋ฆ„ ์„ฑ๋ณ„ ๋‚˜์ด ํ‚ค(๋‹จ์œ„: ์ธ์น˜) ๋ชธ๋ฌด๊ฒŒ(๋‹จ์œ„: ํŒŒ์šด๋“œ) ํ•„๋ฆฝ ๋‚จ 16 72 150 ์ž๋„ท์—ฌ 15 62.5 112.5 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ฃผ๋””์—ฌ 14 64.3 90 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์กด ๋‚จ 12 59 99.5 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 2-2. PROC PRINT(ํ…Œ์ด๋ธ” ๋ณด์—ฌ์ฃผ๊ธฐ) ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ช…๋ น์–ด PROC PRINT: PRINT ํ”„๋Ÿฌ์‹œ์ €๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. DATA=XXX: XXX๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. VAR XXX: ์ง€์ •๋œ ๋ณ€์ˆ˜ XXX๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ID XXX: ์ง€์ •๋œ ๋ณ€์ˆ˜๋ฅผ ๊ธฐ์ค€๊ฐ’์œผ๋กœ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์ค€๊ฐ’์ด๋ž€, ํ•ด๋‹น ํ–‰์˜ ๋Œ€ํ‘ฏ๊ฐ’์œผ๋กœ ํ™œ์šฉํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. BY XXX: ์ง€์ •๋œ ๋ณ€์ˆ˜๋ฅผ ๊ทธ๋ฃน์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. tip ๋‹จ, BY ๋ช…๋ น์–ด๋ฅผ ํ™œ์šฉํ•  ๋•Œ๋Š” BY๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋ผ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. PROC SORT๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) ์˜ˆ์ œ PROC PRINT DATA=SASHELP.CLASS;/*PRINT PROCEDURE๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ(PROC PRINT) SASHELP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(SASHELP.)์˜ CLASS ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ด(DATA=)*/ RUN;/*๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒ*/ | OBS|Name|Sex|Age|Height|Weight| | -------- | -------- | --------| -------- | --------| 1|์•Œํ”„๋ ˆ๋“œ|๋‚จ|14|69.0|112.5 2|์•จ๋ฆฌ์Šค|์—ฌ|13|56.5|84.0 3|๋ฐ”๋ฐ”๋ผ|์—ฌ|13|65.3|98.0 4|์บ๋Ÿด|์—ฌ|14|62.8|102.5 5|ํ—จ๋ฆฌ|๋‚จ|14|63.5|102.5 6|์ œ์ž„์Šค|๋‚จ|12|57.3|83.0 7|์ œ์ธ|์—ฌ|12|59.8|84.5 8|์ž๋„ท|์—ฌ|15|62.5|112.5 9|์ œํ”„๋ฆฌ|๋‚จ|13|62.5|84.0 10|์กด|๋‚จ|12|59.0|99.5 11|์กฐ์ด์Šค|์—ฌ|11|51.3|50.5 12|์ฃผ๋””|์—ฌ|14|64.3|90.0 13|๋ฃจ์ด์Šค|์—ฌ|12|56.3|77.0 14|๋ฉ”๋ฆฌ|์—ฌ|15|66.5|112.0 15|ํ•„๋ฆฝ|๋‚จ|16|72.0|150.0 16|๋กœ๋ฒ„ํŠธ|๋‚จ|12|64.8|128.0 17|๋กœ๋‚ ๋“œ|๋‚จ|15|67.0|133.0 18|ํ† ๋งˆ์Šค|๋‚จ|11|57.5|85.0 19|์œŒ๋ฆฌ์—„|๋‚จ|15|66.5|112.0 ์˜ˆ์ œ 2 PROC PRINT DATA=SASHELP.CLASS; /*PRINT PROCEDURE๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ(PROC PRINT) SASHELP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(SASHELP.)์˜ CLASS ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ด(DATA=).*/ VAR AGE NAME;/*๋ณ€์ˆ˜(AGE, NAME)๋ฅผ ๋ถˆ๋Ÿฌ์˜ด(VAR)*/ RUN;/*๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒ*/ OBS Age Name 1 14 ์•Œํ”„๋ ˆ๋“œ 2 13 ์•จ๋ฆฌ์Šค 3 13 ๋ฐ”๋ฐ”๋ผ 4 14 ์บ๋Ÿด 5 14 ํ—จ๋ฆฌ 6 12 ์ œ์ž„์Šค 7 12 ์ œ์ธ 8 15 ์ž๋„ท 9 13 ์ œํ”„๋ฆฌ 10 12 ์กด 11 11 ์กฐ์ด์Šค 12 14 ์ฃผ๋”” 13 12 ๋ฃจ์ด์Šค 14 15 ๋ฉ”๋ฆฌ 15 16 ํ•„๋ฆฝ 16 12 ๋กœ๋ฒ„ํŠธ 17 15 ๋กœ๋‚ ๋“œ 18 11 ํ† ๋งˆ์Šค 19 15 ์œŒ๋ฆฌ์—„ ์˜ˆ์ œ 3 PROC PRINT DATA=TEST;/*PRINT PROCEDURE๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ(PROC PRINT) SASHELP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(SASHELP.)์˜ CLASS ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ด(DATA=).*/ VAR AGE NAME;/*๋ณ€์ˆ˜(AGE, NAME)๋ฅผ ๋ถˆ๋Ÿฌ์˜ด(VAR)*/ ID AGE;/*๋ณ€์ˆ˜(AGE)๋ฅผ ๊ธฐ์ค€๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•จ*/ BY AGE;/*๋ณ€์ˆ˜(AGE)๋ฅผ ๊ทธ๋ฃน์œผ๋กœ ๋งŒ๋“ฆ(โ€ปBY ๊ตฌ๋ฌธ์ด ์ ์šฉ๋˜๋Š” ๋ณ€์ˆ˜๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋ผ ์žˆ์–ด์•ผ ํ•จ)*/ RUN;/*๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒ*/ tip ์˜ˆ์ œ 3์„ ํ•˜๊ธฐ ์ „์— ์•„๋ž˜ ๊ตฌ๋ฌธ์„ ์‹œํ–‰ํ•˜์„ธ์š”. PROC PRINT์—์„œ ๊ทธ๋ฃน์„ ์„ค์ •ํ•˜๋Š” BY ๊ตฌ๋ถ„์€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋ผ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. PROC SORT DATA=SASHELP.CLASS OUT=TEST; BY AGE; RUN; 2-3. PROC CONTENTS(ํ…Œ์ด๋ธ” ์†์„ฑ ๋ณด๊ธฐ) SAS ํ…Œ์ด๋ธ”๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ๊ด€์ธก์ง€๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ์–ด๋–ค ํด๋”์— ํ…Œ์ด๋ธ”์ด ์ €์žฅ๋ผ ์žˆ๋Š”์ง€ ๋“ฑ ํ…Œ์ด๋ธ”๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์†์„ฑ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์œˆ๋„์—์„œ ํŒŒ์ผ์ด๋‚˜ ํด๋”์˜ ์†์„ฑ์„ ์กฐํšŒํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด PROC CONTENTS: CONTENTS ํ”„๋Ÿฌ์‹œ์ €๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. DATA=XXX: ํƒ์ƒ‰ํ•  ํ…Œ์ด๋ธ”์ด๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. RUN: ์˜ˆ์ œ PROC CONTENTS DATA=SASHELP.CLASS;/*๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ SASHELP์˜ ํ…Œ์ด๋ธ” CLASS์˜ ์†์„ฑ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ด(CONTENTS)*/ RUN;/*SAS ๋ช…๋ น์–ด ์ข…๋ฃŒ*/ - - - - ๋ฐ์ดํ„ฐ ์…‹ ์ด๋ฆ„ SASHELP.CLASS ๊ด€์ธก์น˜ 19 ๋ฉค๋ฒ„ ์œ ํ˜• DATA ๋ณ€์ˆ˜ 5 ์—”์ง„ V9 ์ธ๋ฑ์Šค 0 ์ƒ์„ฑ์ผ 2015.05.28 14:08:14 ๊ด€์ธก์น˜ ๊ธธ์ด 40 ๋งˆ์ง€๋ง‰ ์ˆ˜์ •์ผ 2015.05.28 14:08:14 ์‚ญ์ œ๋œ ๊ด€์ธก์น˜ 0 ๋ณดํ˜ธ ์••์ถ• ์—ฌ๋ถ€ ์•„๋‹ˆ์š” ๋ฐ์ดํ„ฐ ์…‹ ์œ ํ˜• ์ •๋ ฌ ์•„๋‹ˆ์š” ๋ ˆ์ด๋ธ” ํ•™์ƒ ๋ฐ์ดํ„ฐ ๋ฐ์ดํ„ฐ ํ‘œํ˜„ HP_UX_64, RS_6000_AIX_64, SOLARIS_64, HP_IA64 ์ธ์ฝ”๋”ฉ euc-kr Korean (EUC) - - ์—”์ง„/ํ˜ธ์ŠคํŠธ ๊ด€๋ จ ์ •๋ณด ๋ฐ์ดํ„ฐ ์…‹ ํŽ˜์ด์ง€ ํฌ๊ธฐ 65536 ๋ฐ์ดํ„ฐ ์…‹ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ 1 ์ฒซ ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ ํŽ˜์ด์ง€ 1 ํŽ˜์ด์ง€ ๋‹น ์ตœ๋Œ€ ๊ด€์ธก ์น˜์ˆ˜ 1632 ์ฒซ ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ ํŽ˜์ด์ง€์˜ ๊ด€์ธก ์น˜์ˆ˜ 19 ๋ฐ์ดํ„ฐ ์…‹ ์ˆ˜๋ฆฌ์˜ ๋ฒˆํ˜ธ 0 ํŒŒ์ผ ์ด๋ฆ„ /sashelp/class.sas7bdat ์ƒ์„ฑ๋œ ๋ฆด๋ฆฌ์Šค 6.1204S4 ์ƒ์„ฑ๋œ ํ˜ธ์ŠคํŠธ AIX Inode ๋ฒˆํ˜ธ 2341333 ์•ก์„ธ์Šค ๊ถŒํ•œ rw-r--r-- ์†Œ์œ ์ž ์ด๋ฆ„ sas ํŒŒ์ผ ํฌ๊ธฐ 128KB ํŒŒ์ผ ํฌ๊ธฐ(๋ฐ”์ดํŠธ) 131072 ๋ณ€์ˆ˜ ์œ ํ˜• ๊ธธ์ด ๋ ˆ์ด๋ธ” 3 Age ์ˆซ์ž 8 4 Height ์ˆซ์ž 8 1 Name ๋ฌธ์ž 12 2 Sex ๋ฌธ์ž 4 5 Weight ์ˆซ์ž 8 tip SAS ์ž‘์—…์„ ํ•˜๋ฉด์„œ CONTENTS ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ฃผ๋กœ ํ™•์ธํ•˜๋Š” ์†์„ฑ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ์…‹ ์ด๋ฆ„: ํ…Œ์ด๋ธ” ์ด๋ฆ„๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ช…์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. 2. ์ƒ์„ฑ์ผ: ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์ผ์ž๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. 3. ๊ด€์ธก์น˜: ๊ด€์ธก ๊ฐ’์ด ๋ช‡ ๊ฐœ์˜ ํ–‰์œผ๋กœ ์ด๋ค„์ ธ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. 4. ๋ณ€์ˆ˜: ๋ณ€์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. 5. ํŒŒ์ผ ์ด๋ฆ„: ํ…Œ์ด๋ธ”์ด ์ €์žฅ๋œ ํด๋” ์ฃผ์†Œ์™€ ์‹ค์ œ ํŒŒ์ผ ์ด๋ฆ„์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. 6. ์ธ๋ฑ์Šค: ์ธ๋ฑ์Šค๊ฐ€ ๋ช‡ ๊ฐœ ์„ค์ •๋ผ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค(์ธ๋ฑ์Šค ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์— ๋ฐฐ์šฐ๊ฒ ์Šต๋‹ˆ๋‹ค). 7. ์ •๋ ฌ: ํ…Œ์ด๋ธ”์ด ํŠน์ • ๋ณ€์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋ผ ์žˆ๋Š”์ง€๋ฅผ โ€˜์˜ˆ/์•„๋‹ˆ์š”โ€™๋กœ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. 8. ํŒŒ์ผ ํฌ๊ธฐ: ํ…Œ์ด๋ธ”์˜ ์šฉ๋Ÿ‰์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ „์ฒด์˜ ์†์„ฑ ๋‹ค์Œ์œผ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ „์ฒด์˜ ์†์„ฑ ์ •๋ณด๋ฅผ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด DATA=XXX._ALL_: ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ XXX์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด๋ถ€์— ์žˆ๋Š” ํ…Œ์ด๋ธ” ์ „์ฒด์— ๋Œ€ํ•œ ์†์„ฑ์ •๋ณด๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค(_ALL_). ๋Œ€์ƒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ช… ๋’ค์— ๋ถ™์—ฌ์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ PROC CONTENTS DATA=WORK._ALL_;/*๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ XXX(XXX._ALL_)์— ๋Œ€ํ•œ ์†์„ฑ์ •๋ณด์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด๋ถ€์— ์žˆ๋Š” ํ…Œ์ด๋ธ” ์ „์ฒด(XXX._ALL_)์— ๋Œ€ํ•œ ์†์„ฑ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ด(CONTENTS).*/ RUN;/*SAS ๋ช…๋ น์–ด ์ข…๋ฃŒ*/ - - Directory ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ฐธ์กฐ WORK ์—”์ง„ V9 ๋ฌผ๋ฆฌ์  ๊ฒฝ๋กœ C:\ ํŒŒ์ผ ์ด๋ฆ„ C:\ Inode ๋ฒˆํ˜ธ 19868 ์•ก์„ธ์Šค ๊ถŒํ•œ rwx------ ์†Œ์œ ์ž ์ด๋ฆ„ sas ํŒŒ์ผ ํฌ๊ธฐ 8KB ํŒŒ์ผ ํฌ๊ธฐ(๋ฐ”์ดํŠธ) 8192 # ์ด๋ฆ„ ๋ฉค๋ฒ„ ์œ ํ˜• ํŒŒ์ผ ํฌ๊ธฐ ๋งˆ์ง€๋ง‰ ์ˆ˜์ •์ผ 1 REGSTRY ITEMSTOR 32KB 2020.07.24 09:46:49 2 SASGOPT CATALOG 12KB 2020.07.24 09:46:54 3 SASMAC1 CATALOG 100KB 2020.07.24 09:46:52 4 TEST DATA 192KB 2020.07.24 10:00:33 5 _PRODSAVAIL DATA 192KB 2020.07.24 09:46:50 ์œ„์˜ ํ‘œ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. 2-4. PROC FREQ(๋นˆ๋„ ์กฐํšŒ) ํ…Œ์ด๋ธ” ๋‚ด ๋ฐ์ดํ„ฐ์˜ ๋นˆ๋„๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด PROC FREQ: FREQ ํ”„๋Ÿฌ์‹œ์ €๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. DATA=XXX: XXX๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. TABLE YYY ZZZ: ๋ณ€์ˆ˜ YYY์™€ ZZZ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. tip ๋ณดํ†ต SAS์—์„œ๋Š” ๋ณ€์ˆ˜๋ช…์„ ์“ธ ๋•Œ โ€˜VAR(VARIANCE๋ฅผ ์ง€์นญํ•˜๋Š” ๋ช…๋ น์–ด)โ€™๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ FREQ์—์„œ๋Š” TABLE์ด๋ผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์ด์œ ๋Š” ์ž ์‹œ ํ›„์— ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ PROC FREQ DATA=SASHELP.CLASS; /*FREQ PROCEDURE๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ(PROC FREQ) SASHELP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ CLASS ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ด(DATA=SASHELP.CLASS).*/ TABLE AGE NAME;/*๋ณ€์ˆ˜ AGE์™€ NAME์„ ์กฐํšŒํ•จ*/ RUN;/*๋ชจ๋“  ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒ*/ Age(๋‚˜์ด) ๋นˆ๋„ ๋ฐฑ๋ถ„์œจ ๋ˆ„์  ๋นˆ๋„ ๋ˆ„์  ๋ฐฑ๋ถ„์œจ 11 2 10.53 2 10.53 12 5 26.32 7 36.84 13 3 15.79 10 52.63 14 4 21.05 14 73.68 15 4 21.05 18 94.74 16 1 5.26 19 100.00 Height(ํ‚ค(๋‹จ์œ„: ์ธ์น˜) ๋นˆ๋„ ๋ฐฑ๋ถ„์œจ ๋ˆ„์  ๋นˆ๋„ ๋ˆ„์  ๋ฐฑ๋ถ„์œจ 51.3 1 5.26 1 5.26 56.3 1 5.26 2 10.53 56.5 1 5.26 3 15.79 57.3 1 5.26 4 21.05 57.5 1 5.26 5 26.32 59 1 5.26 6 31.58 59.8 1 5.26 7 36.84 62.5 2 10.53 9 47.37 62.8 1 5.26 10 52.63 63.5 1 5.26 11 57.89 64.3 1 5.26 12 63.16 64.8 1 5.26 13 68.42 65.3 1 5.26 14 73.68 66.5 2 10.53 16 84.21 67 1 5.26 17 89.47 69 1 5.26 18 94.74 72 1 5.26 19 100.00 ๊ฒฐ๊ณผ๋กœ ๋ณ€์ˆ˜ AGE์™€ NAME์˜ ๋นˆ๋„ยท๋ฐฑ๋ถ„์œจยท๋ˆ„์  ๋นˆ๋„ยท๋ˆ„์  ๋ฐฑ๋ถ„์œจ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜์™€ ์ด๋ฆ„์„ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ๋ช…๋ น์–ด TABLE AGE * NAME: AGE์™€ NAME์˜ 2์ฐจ์› ํ‘œ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ PROC FREQ DATA=SASHELP.CLASS;/*FREQ PROCEDURE๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ(PROC FREQ) SASHELP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ CLASS ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ด(DATA=SASHELP.CLASS).*/ TABLE AGE*HEIGHT;/*๋ณ€์ˆ˜ AGE์™€ NAME์˜ 2์ฐจ์› ํ‘œ๋ฅผ ์กฐํšŒํ•จ*/ RUN;/*๋ชจ๋“  ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒ*/ ํ™”๋ฉด์— ๊ฒฐ๊ด๊ฐ’์„ ๋‹ค ๋‹ด์„ ์ˆ˜ ์—†์–ด ๊ฒฐ๊ณผ๋Š” ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ์‹œํ–‰์„ ํ•ด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. FREQ PROCEDURE๊ฐ€ ๋ณ€์ˆ˜๋ฅผ ๋œปํ•˜๋Š” VAR ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  TABLE ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. TABLE์€ ํ–‰๊ณผ ์—ด๋กœ ์ด๋ค„์ง„ ํ‘œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. TABLE ๋ณ€์ˆ˜ 1๋ณ€์ˆ˜ 2 ํ˜•ํƒœ์˜ ๋ช…๋ น์–ด๋ฅผ ์“ฐ๋ฉด ๋ณ€์ˆ˜ 1๋ณ€์ˆ˜ 2๋กœ ์ด๋ฃจ์–ด์ง„ ๊ต์ฐจ ํ‘œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ๋Š” ์—ฐ๋ น๋ณ„ ์ด๋ฆ„์„ ๋นˆ๋„ยท๋ฐฑ๋ถ„์œจยทํ–‰ ๋ฐฑ๋ถ„์œจยท์นผ๋Ÿผ ๋ฐฑ๋ถ„์œจ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(๋ณ€์ˆ˜ 1 * ๋ณ€์ˆ˜ 2 * ๋ณ€์ˆ˜ 3์œผ๋กœ ๋ช…๋ น์–ด๋ฅผ ์“ธ ๊ฒฝ์šฐ, ๋ณ€์ˆ˜ 1์— ๋”ฐ๋ฅธ ๋ณ€์ˆ˜ 2 * ๋ณ€์ˆ˜ 3์˜ ๊ต์ฐจ ํ‘œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด TABLE AGE * NAME * WEIGHT๋กœ ๋ช…๋ น์–ด๋ฅผ ์“ด๋‹ค๋ฉด AGE ๋ณ„ NAME * WEIGHT ๊ต์ฐจ ํ‘œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค). 3. ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ๋ฒ• SAS์—์„œ ์ง์ ‘ ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SAS๋Š” ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๋กœ ๋‹ค๋ฃจ๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ž‘์—…์œผ๋กœ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ์ผ์€ ๊ทธ๋‹ค์ง€ ๋งŽ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ธฐ์—…์—์„œ๋‚˜ ํ•™๊ต์—์„œ SAS๋ฅผ ํ™œ์šฉํ•  ๋•Œ๋Š” ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•  ๋•Œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๋ณ‘์›์—์„œ๋Š” ์˜๋ฃŒ ๋ฐ์ดํ„ฐ๋ฅผ, ๋…ธ๋™ ์—ฐ๊ตฌ์†Œ์—์„œ๋Š” ์†Œ๋“ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•˜๋Š” ์‹์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๊ณ  ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ๋ฒ•์„ ๋ชฐ๋ผ๋„ ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ƒ์„ฑ์€ SAS ๋ถ„์„์˜ ๊ธฐ์ดˆ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ณ€์ˆ˜ ์†์„ฑ์ฒ˜๋Ÿผ ์ดํ›„ ๋ถ„์„ ๊ณผ์ •์—์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์˜ ๊ธฐ์ดˆ ์ง€์‹์„ ์Šต๋“ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ๋Š” ๊ธฐ์ดˆ์ ์ธ ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šฐ๊ณ  ํ…Œ์ด๋ธ”์˜ ์†์„ฑ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 3-1. ์ง์ ‘ ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๋ฒ• ๋ฐ์ดํ„ฐ๋Š” ํฌ๊ฒŒ ์ˆซ์ž ๋ณ€์ˆ˜์™€ ๋ฌธ์ž ๋ณ€์ˆ˜๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. SAS๋Š” ์ˆซ์ž์™€ ๋ฌธ์ž๋ฅผ ๋ถ„๋ช…ํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜+ , - , * , / โ€™ ๊ฐ™์€ ์‚ฌ์น™์—ฐ์‚ฐ์€ ์ˆซ์ž ๋ณ€์ˆ˜์—๋งŒ ์ ์šฉ์ด ๋˜๊ณ  ๋ฌธ์ž ๋ณ€์ˆ˜์—๋Š” ์ ์šฉ์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์น™์—ฐ์‚ฐ์ด ๋ฌธ์ž ๋ณ€์ˆ˜์— ์ ์šฉ์ด ๋˜๋ฉด ์˜ค๋ฅ˜๋กœ SAS ๋ช…๋ น์–ด๊ฐ€ ๋ฐ”๋กœ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ˆซ์ž ๋ณ€์ˆ˜์™€ ๋ฌธ์ž ๋ณ€์ˆ˜์— ์œ ์˜ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด DATA XXX: ํ…Œ์ด๋ธ” XXX๋ฅผ ์ƒ์„ฑํ•จ INPUT YYY $ ZZZ: ๋ฌธ์ž ๋ณ€์ˆ˜ YYY์™€ ์ˆซ์ž ๋ณ€์ˆ˜ ZZZ๋ฅผ ์ƒ์„ฑํ•จ(์ˆซ์ž ๋ณ€์ˆ˜๋Š” ๋ณ€์ˆ˜๋ช…๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋˜๊ณ  ๋ฌธ์ž ๋ณ€์ˆ˜๋Š” ๋ณ€์ˆ˜๋ช… ๋’ค์— ํ•œ ์นธ์„ ๋„์šฐ๊ณ  $๋ฅผ ๋ถ™์ž„) CARDS; XX 1: XX์™€ 1์„ ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜์— ์ž…๋ ฅํ•จ(CARDS๋Š” DATALINES๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) RUN: SAS ๋ช…๋ น์–ด ์ข…๋ฃŒ ์˜ˆ์ œ DATA TEST;/*๋ฐ์ดํ„ฐ TEST๋ฅผ ์ƒ์„ฑํ•จ(DATA)*/ INPUT NAME $ AGE SEX $;/*๋ฌธ์ž ๋ณ€์ˆ˜ NAME๊ณผ SEX, ์ˆซ์ž ๋ณ€์ˆ˜ AGE๋ฅผ ์ƒ์„ฑํ•จ(INPUT)*/ CARDS;/*๋ณ€์ˆ˜๋ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•จ(CARDS)*/ OK 1 ๋‚จ YOU 2 ๋…€;/*๋ณ€์ˆ˜๋ณ„๋กœ ๋“ค์–ด๊ฐˆ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ*/ RUN;/*SAS ๋ช…๋ น์–ด ์ข…๋ฃŒ*/ NAME AGE SEX OK 1 ๋‚จ YOU 2 ๋…€ INPUT ๋ช…๋ น์–ด ์ดํ›„ ๋ณ€์ˆ˜ NAME ๋’ค์— ๋ฌธ์ž๋ฅผ ๋ถ™์ž„์œผ๋กœ์จ ์ด ๋ฌธ์ž ๋ณ€์ˆ˜๋ผ๋Š” ๊ฑธ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค ๋’ค์— ์žˆ๋Š” ์ž ๋ถ™ ์œผ ์จ A E ๋ฌธ ๋ณ€ ๋ผ ๊ฑธ ํƒ€ ๋„ค. E ๋’ค ์žˆ ๋ฌธ์ž๋„ ๋™์ผํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. AGE๋Š” ์ˆซ์ž ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ˆซ์ž ๋ณ€์ˆ˜๋กœ ์„ค์ •๋ผ ์žˆ๋Š”๋ฐ โ€˜๊ฐ€๋‚˜๋‹คโ€™์ฒ˜๋Ÿผ ๋ฌธ์ž ๋ณ€์ˆ˜๊ฐ€ ์ž…๋ ฅ๋  ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” โ€˜.โ€™์œผ๋กœ ํ‘œ์‹œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ณต๋ฐฑ(ํ†ต๊ณ„ํ•™์ ์œผ๋กœ NULL ๊ฐ’, ๊ฒฐ ์ธก ์น˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค)์œผ๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. CARDS ๋ช…๋ น์–ด๋Š” ํ…Œ์ด๋ธ” ๋‚ด๋ถ€์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. CARDS ๋ช…๋ น์–ด๋งŒ ์“ด ๋‹ค์Œ(โ€˜;โ€™ ์‚ฌ์šฉ) โ€˜OK 1 ๋‚จโ€™์„ ๋ณ€์ˆ˜๋ช… ์ˆœ์„œ์— ๋งž์ถฐ ์ž…๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. INPUT์— ์“ด ๋ณ€์ˆ˜๋ช… ์ˆœ์„œ๊ฐ€ 1) NAME 2) AGE 3) SEX์ด๋ฏ€๋กœ OK(NAME์˜ ๋ฐ์ดํ„ฐ)-1(AGE์˜ ๋ฐ์ดํ„ฐ)-๋‚จ(SEX)์„ ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์„ ๋งˆ์น˜๋ฉด โ€˜;โ€™๋ฅผ ์ž…๋ ฅํ•ด ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž ๋ณ€์ˆ˜ ๊ธธ์ด ์„ค์ • ๋ฌธ์ž ๋ณ€์ˆ˜์— ๊ธด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜ํ˜ธ๋‚ ๋‘ ํŒŒ์ดํŒ…โ€™ ๊ฐ™์ด ๊ธด ๊ธ€์ž๋ฅผ ์ž…๋ ฅํ•˜๋Š” ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ 1.์—์„œ ํ•œ ํ–‰์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ DATA TEST;/*๋ฐ์ดํ„ฐ TEST๋ฅผ ์ƒ์„ฑํ•จ(DATA)*/ INPUT NAME $ AGE SEX $;/*๋ฌธ์ž ๋ณ€์ˆ˜ NAME๊ณผ SEX, ์ˆซ์ž ๋ณ€์ˆ˜ AGE๋ฅผ ์ƒ์„ฑํ•จ(INPUT)*/ CARDS;/*๋ณ€์ˆ˜๋ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•จ(CARDS)*/ OK 1 ๋‚จ YOU 2 ๋…€ ํ˜ธ๋‚ ๋‘ ํŒŒ์ดํŒ… 3 ๋‚จ ;/*๋ณ€์ˆ˜๋ณ„๋กœ ๋“ค์–ด๊ฐˆ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ*/ RUN;/*SAS ๋ช…๋ น์–ด ์ข…๋ฃŒ*/ NAME AGE SEX OK 1 ๋‚จ YOU 2 ๋…€ ํ˜ธ๋‚ ๋‘ํ™” 3 ๋‚จ ์ด๋ ‡๊ฒŒ ์ž…๋ ฅ์„ ํ•  ๊ฒฝ์šฐ์— ๊ฒฐ๊ด๊ฐ’์— โ€˜ํ˜ธ๋‚ ๋‘ ํŒŒ์ดํŒ…โ€™์ด โ€˜ํ˜ธ๋‚ ๋‘ํ™”โ€™๊นŒ์ง€๋งŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” SAS๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž ๋ณ€์ˆ˜๋ฅผ 8๊ธ€์ž(์˜์–ด ๊ธฐ์ค€)๊นŒ์ง€ ์ฝ์–ด๋“ค์ด๋„๋ก ์„ค์ •๋ผ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•œ๊ธ€์€ 4๊ธ€์ž๊นŒ์ง€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์•ŒํŒŒ๋ฒณ 2๊ธ€์ž๊ฐ€ ํ•œ๊ธ€ 1๊ธ€์ž์™€ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ โ€˜ํ˜ธ๋‚ ๋‘ ํŒŒ์ดํŒ…โ€™์ด 6๊ธ€์ž์ด๋ฏ€๋กœ โ€˜ํ˜ธ๋‚ ๋‘ํ™”โ€™๋ฅผ ๋„˜์–ด์„œ๋Š” 2๊ธ€์ž๋Š” ์ œ์™ธํ•˜๊ณ  ์ฝ์–ด๋“ค์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฌธ์ž ๋ณ€์ˆ˜๋ฅผ ์ฝ์–ด๋“ค์ด๋Š” ๊ธธ์ด๋ฅผ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ „๋ฌธ์šฉ์–ด๋กœ โ€˜ํฌ๋งท์„ ๋ณ€๊ฒฝํ•œ๋‹คโ€™๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ๋ฌธ์žยท์ˆซ์ž ์—ฌ๋ถ€์™€ ๊ธธ์ด ๋“ฑ ์†์„ฑ์„ ํฌ๋งท์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํฌ๋งท์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 1) ๋ฌธ์ž ๋ณ€์ˆ˜ ๊ธธ์ด ๋ณ€๊ฒฝ ๋ฌธ์ž ๋ณ€์ˆ˜๊ฐ€ ์ฝ์–ด๋“ค์ผ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ธ€์ž ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ธ€์ž ์ˆ˜๋Š” ์˜์–ด ์•ŒํŒŒ๋ฒณ ๊ธฐ์ค€์ž…๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด INPUT NAME: $12. AGE SEX $;: ๋ณ€์ˆ˜ NAME์˜ ํฌ๋งท์„ ์•ŒํŒŒ๋ฒณ 10๊ธ€์ž๊นŒ์ง€ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๋„๋ก ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ DATA TEST;/*๋ฐ์ดํ„ฐ TEST๋ฅผ ์ƒ์„ฑํ•จ(DATA)*/ INPUT NAME: $12. AGE SEX $;/*๋ฌธ์ž ๋ณ€์ˆ˜ NAME์˜ ํฌ๋งท์„ ์ตœ๋Œ€ 12๊ธ€์ž(์•ŒํŒŒ๋ฒณ ๊ธฐ์ค€)๊นŒ์ง€ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๋„๋ก ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค(NAME: $12.) */ CARDS;/*๋ณ€์ˆ˜๋ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•จ(CARDS)*/ OK 1 ๋‚จ YOU 2 ๋…€ ํ˜ธ๋‚ ๋‘ ํŒŒ์ดํŒ… 3 ๋‚จ ;/*๋ณ€์ˆ˜๋ณ„๋กœ ๋“ค์–ด๊ฐˆ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ*/ RUN;/*SAS ๋ช…๋ น์–ด ์ข…๋ฃŒ*/ NAME AGE SEX OK 1 ๋‚จ YOU 2 ๋…€ ํ˜ธ๋‚ ๋‘ ํŒŒ์ดํŒ… 3 ๋‚จ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ๋Š˜๋ฆด ๋Œ€์ƒ์ธ ๋ณ€์ˆ˜ NAME ๋’ค์ชฝ์— (: $12.)์ด๋ผ๋Š” ๋ช…๋ น์–ด๋ฅผ ๋ง๋ถ™์ž…๋‹ˆ๋‹ค. ์ด ๋ช…๋ น์–ด๋กœ NAME์ด ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌธ์ž ๋ณ€์ˆ˜๋ฅผ 12๊ธ€์ž(์•ŒํŒŒ๋ฒณ ๊ธฐ์ค€)๊นŒ์ง€ ๋Š˜๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฌธ์ž ๋ณ€์ˆ˜์—๋„ ๋˜‘๊ฐ™์ด ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ์ž…๋ ฅ ๋Œ€์ƒ ๋ฐ์ดํ„ฐ ์œ„์น˜ ์„ค์ • ๋ฌธ์ž ๋ณ€์ˆ˜๋ฅผ ์ฝ์–ด๋“ค์ด๋Š” ์œ„์น˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ„์น˜๋ณ„๋กœ ์ผ์ •ํ•˜๊ฒŒ ์ •๋ ฌ๋ผ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•œ ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 1๋ฒˆ์งธ ์นธ๋ถ€ํ„ฐ 12๋ฒˆ์งธ ์นธ๊นŒ์ง€ ๋ณ€์ˆ˜ NAME์˜ ๋ฐ์ดํ„ฐ๋กœ, 14๋ฒˆ์งธ ์นธ์€ ๋ณ€์ˆ˜ AGE์˜ ๋ฐ์ดํ„ฐ๋กœ, 16๋ฒˆ์งธ ์นธ๋ถ€ํ„ฐ 17๋ฒˆ์งธ ์นธ๊นŒ์ง€๋Š” ๋ณ€์ˆ˜ SEX์˜ ๋ฐ์ดํ„ฐ๋กœ ์„ค์ •ํ•˜๋Š” ์‹์ž…๋‹ˆ๋‹ค. ๋ช…๋ น์–ด INPUT NAME $ 1-12 AGE 14 SEX $ 16-17;: 1๋ฒˆ์งธ ์นธ๋ถ€ํ„ฐ 12๋ฒˆ์งธ ์นธ๊นŒ์ง€๋Š” ๋ฌธ์ž ๋ณ€์ˆ˜ NAME์˜ ๋ฐ์ดํ„ฐ๋กœ, 14๋ฒˆ์งธ ์นธ์€ ๋ณ€์ˆ˜ AGE์˜ ๋ฐ์ดํ„ฐ๋กœ, 16๋ฒˆ์งธ ์นธ๋ถ€ํ„ฐ 17๋ฒˆ์งธ ์นธ๊นŒ์ง€๋Š” ๋ณ€์ˆ˜ SEX์˜ ๋ฐ์ดํ„ฐ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ DATA TEST;/*๋ฐ์ดํ„ฐ TEST๋ฅผ ์ƒ์„ฑํ•จ(DATA)*/ INPUT NAME $1-12 AGE 14 SEX $ 16-17;/*1~12๋ฒˆ์งธ ์นธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž ๋ณ€์ˆ˜ NAME์˜ ๋ฐ์ดํ„ฐ๋กœ(NAME $1-12), 14๋ฒˆ์งธ ์นธ์€ ์ˆซ์ž ๋ณ€์ˆ˜ AGE์˜ ๋ฐ์ดํ„ฐ๋กœ(AGE 14), 16~17๋ฒˆ์งธ ์นธ์€ ๋ฌธ์ž ๋ณ€์ˆ˜ SEX์˜ ๋ฐ์ดํ„ฐ๋กœ(SEX $ 16-17) ์„ค์ •ํ•จ*/ CARDS;/*๋ณ€์ˆ˜๋ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•จ(CARDS)*/ OK 1 ๋‚จ YOU 2 ๋…€ ํ˜ธ๋‚ ๋‘ ํŒŒ์ดํŒ… 3 ๋‚จ ;/*๋ณ€์ˆ˜๋ณ„๋กœ ๋“ค์–ด๊ฐˆ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ*/ RUN;/*SAS ๋ช…๋ น์–ด ์ข…๋ฃŒ*/ NAME AGE SEX OK 1 ๋‚จ YOU 2 ๋…€ ํ˜ธ๋‚ ๋‘ ํŒŒ์ดํŒ… 3 ๋‚จ ์œ„ ๋ฐฉ๋ฒ•์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ผ์ •ํ•œ ์œ„์น˜์— ์ •๋ ฌ๋ผ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ๊ฐ€ ์ผ์ •ํ•˜๊ฒŒ ์œ„์น˜์— ๋งž๊ฒŒ ์ •๋ ฌ๋ผ ์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ ์˜ฌ๋ฐ”๋ฅธ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. tip ํ•œ๊ธ€ ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ SAS ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ตœ์†Œ 2๊ฐœ ์œ„์น˜๋ฅผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ธ€ ํ•œ ๊ธ€์ž๋Š” ์˜์–ด ๋‘ ๊ธ€์ž์™€ ๋™์ผํ•œ ์œ„์น˜ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•œ๊ธ€ ํ•œ ๊ธ€์ž๋ฅผ ๋ณ€์ˆ˜ SEX์— ์ž…๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ ์–ด๋„ 2๊ฐœ ์นธ์„ ์„ค์ •ํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž ๋ณ€์ˆ˜์˜ ๊ฒฝ์šฐ ํ•œ ์ž๋‹น ํ•œ ๊ฐœ์˜ ์œ„์น˜๋ฅผ ์ฐจ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜ AGE๋ฅผ ์„ค์ •ํ•  ๋•Œ์ฒ˜๋Ÿผ(INPUT AGE 14) ํ•˜๋‚˜์˜ ์นธ๋งŒ ์„ค์ •ํ•ด ์ค˜๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค. 3-2. ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•˜๊ธฐ ๋ณดํ†ตํ•™๊ต๋‚˜ ๊ธฐ์—…์ฒด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•  ๋•Œ๋Š”. TXT ํŒŒ์ผ์ด๋‚˜. XLSX, .CSV ํŒŒ์ผ๋กœ ์›์‹œ๋ฐ์ดํ„ฐ(๊ฐ€๊ณตํ•˜์ง€ ์•Š์€ ์ดˆ์ฐฝ๊ธฐ ๋ฐ์ดํ„ฐ)๊ฐ€ ์ œ๊ณต๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. SAS์—์„œ ๋ถ„์„ํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋Œ€์šฉ๋Ÿ‰์ด๋ผ๋ฉด ์ˆ˜์ž‘์—…์œผ๋กœ ์ž‘์—…ํ•˜๋Š” ๊ฑด ๊ฑฐ์˜ ๋ถˆ๊ฐ€๋Šฅํ•˜์ฃ . ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ ค์„œ ํšจ์œจ์ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ถ„์„ ๋Œ€์ƒ ์›์‹œ๋ฐ์ดํ„ฐ๋Š” ๋Œ€์ฒด๋กœ ์œ„์™€ ๊ฐ™์€ ํŒŒ์ผ ํ˜•ํƒœ๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ์„ ์„ฑ๊ณต์ ์œผ๋กœ SAS์— ํƒ‘์žฌํ•ด์•ผ ๋ถ„์„ ์ž‘์—…์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์€ ํŒŒ์ผ ํ˜•ํƒœ๋ฅผ ์–ด๋–ป๊ฒŒ SAS์—์„œ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด INFILE โ€˜C:\SAS_TEST\TEST.TXTโ€™;: โ€˜C:\SAS_TEST\TEST.TXTโ€™ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค(INFILE) INPUT YYY $ ZZZ: ๋ฌธ์ž ๋ณ€์ˆ˜ YYY์™€ ์ˆซ์ž ๋ณ€์ˆ˜ ZZZ๋ฅผ ์ƒ์„ฑํ•จ DLM=โ€˜Xโ€™: ๋ฐ์ดํ„ฐ์˜ ๊ตฌ๋ถ„์ž๋Š” โ€˜,โ€™๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Œ์„ ์ž…๋ ฅ (์ˆซ์ž ๋ณ€์ˆ˜๋Š” ๋ณ€์ˆ˜๋ช…๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋˜๊ณ  ๋ฌธ์ž ๋ณ€์ˆ˜๋Š” ๋ณ€์ˆ˜๋ช… ๋’ค์— ํ•œ ์นธ์„ ๋„์šฐ๊ณ  $๋ฅผ ๋ถ™์ž„) ์˜ˆ์ œ TEST.TXT ํŒŒ์ผ ๋‚ด์šฉ OK, 1, ๋‚จ YOU, 2, ๋…€ ํ˜ธ๋‚ ๋‘ ํŒŒ์ดํŒ…, 3, ๋‚จ DATA TEST;/*๋ฐ์ดํ„ฐ TEST๋ฅผ ์ƒ์„ฑํ•จ(DATA)*/ INFILE 'C:/SAS_TEST/TEST.TXT' DLM=โ€˜,โ€™;/*'C:/SAS_TEST/TEST.TXT' ์œ„์น˜์— ์žˆ๋Š” ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๊ณ (INFILE) ํŒŒ์ผ์˜ ๊ตฌ๋ถ„์ž๋Š” โ€˜,โ€™๋กœ ์ด๋ค„์ ธ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๊ทœ์ •(DLM=โ€™,โ€˜)*/ INPUT NAME: $12. AGE SEX $;/*๋ฌธ์ž ๋ณ€์ˆ˜ NAME(์•ŒํŒŒ๋ฒณ 12๊ธ€์ž ๊ธธ์ด๋กœ ์ง€์ •)๊ณผ SEX, ์ˆซ์ž ๋ณ€์ˆ˜ AGE๋ฅผ ์ƒ์„ฑํ•จ(INPUT)*/ RUN;/*SAS ๋ช…๋ น์–ด ์ข…๋ฃŒ*/ NAME AGE SEX OK 1 ๋‚จ YOU 2 ๋…€ ํ˜ธ๋‚ ๋‘ ํŒŒ์ดํŒ… 3 ๋‚จ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. INFILE ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆ๋Ÿฌ์˜ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์›์‹œ๋ฐ์ดํ„ฐ. TXT๊ฐ€ ๋ณ€์ˆ˜๋ณ„๋กœ ๊ณต๋ฐฑ(๋„์–ด์“ฐ๊ธฐ)์œผ๋กœ ๊ตฌ๋ถ„๋ผ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. INFILE๋กœ ๋ถˆ์–ด์˜ฌ ์ˆ˜ ์žˆ๋Š” ํ™•์žฅ์ž๋กœ๋Š”. TXT, .XLSX, .CSV ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ™•์žฅ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€์ฒด๋กœ ์œ„ 3๊ฐ€์ง€๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. DLM=โ€˜,โ€™๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ตฌ๋ถ„์ž๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋ผ ์žˆ๋Š”์ง€๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. SAS๋Š” ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ โ€˜ โ€™์ฒ˜๋Ÿผ ๋นˆ์นธ์„ ๋ฐ์ดํ„ฐ ๊ฐ„ ๊ตฌ๋ถ„์ž๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ๊ผญ ๋นˆ์นธ์œผ๋กœ๋งŒ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ตฌ๋ถ„๋ผ ์žˆ๋‹ค๋Š” ๋ฒ•์€ ์—†์Šต๋‹ˆ๋‹ค. โ€˜,โ€™, โ€˜-โ€™์™€ ๊ฐ™์€ ๊ธฐํ˜ธ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ„ ๊ตฌ๋ถ„์ด ์ด๋ค„์ ธ ์žˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์— DLM=โ€˜,โ€™๋ฅผ ์ž…๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด SAS๋Š” ๋ฐ์ดํ„ฐ ๊ฐ„ ๊ตฌ๋ถ„์ž๊ฐ€ โ€˜,โ€™๋ผ๊ณ  ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ โ€˜,โ€™๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“  ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. INFILE ์ดํ›„์—๋Š” INPUT ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. INFILE์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ธฐ๋Šฅ๋งŒ ์žˆ์ง€ ๋ณ€์ˆ˜๋ช…์„ ๋งŒ๋“ค๊ณ  ์ด์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ์€ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. INFILE ๋ช…๋ น์–ด๋Š” CARDS์™€ ์œ ์‚ฌํ•œ ์—ญํ• ์„ ํ•˜๋Š” ๋ช…๋ น์–ด๋กœ ๋ณด์…”๋„ ๋ฉ๋‹ˆ๋‹ค. CARDS ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ์ง์ ‘ ์†์œผ๋กœ ์ž…๋ ฅํ•˜๊ธฐ์— ๊ณค๋ž€ํ•œ ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ INFILE ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 3-3. ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์™€์„œ ์ƒˆ ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•˜๊ธฐ ์•ž์—์„œ ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ํ…Œ์ด๋ธ”(SAS ๋ฐ์ดํ„ฐ)๋กœ ๋งŒ๋“œ๋Š” ์ž‘์—…์„ ํ–ˆ๋‹ค๋ฉด ์ด์ œ๋Š” ๋งŒ๋“ค์–ด์ง„ ํ…Œ์ด๋ธ”์„ ๊ฐ€๊ณตํ•˜๋Š” ๊ณผ์ •์„ ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์›์‹œ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ด๋ธ”๋กœ ๋งŒ๋“œ๋Š” ๊ฑด ํ•œ ๋ฒˆ์˜ ๊ณผ์ •์ด๋ฉด ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋งŒ๋“ค์–ด์ง„ ํ…Œ์ด๋ธ”์„ ๊ฐ€๊ณตํ•˜๋Š” ๊ฑด ๋ถ„์„ ๊ณผ์ •์—์„œ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋  ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ณผ์ •์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์„ ๋ณ€ํ™˜ํ•˜๊ณ  ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ํ†ต๊ณ„ ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „ ํ•„์ˆ˜ ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์„ ํ†ต๊ณ„ ๋ถ„์„ ๋ชฉ์ ๊ณผ ๋ฐฉ๋ฒ•์— ์•Œ๋งž๊ฒŒ ๊ฐ€๊ณตํ•ด์•ผ ์ œ๋Œ€๋กœ ๋œ ํ†ต๊ณ„ ๋ถ„์„์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ง€๊ธˆ๋ถ€ํ„ฐ ํ…Œ์ด๋ธ”์„ ๊ฐ€๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SET์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ธฐ ๊ฐ€์žฅ ์•ž์—์„œ ํ•™์Šตํ•œ ๋‚ด์šฉ์ด์ง€๋งŒ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SET ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด DATA XXX: ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ” XXX๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค SET YYY: ๊ธฐ์กด ํ…Œ์ด๋ธ” YYY๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์˜ˆ์ œ DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ NAME SEX AGE HEIGHT WEIGHT ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์ž๋„ท์—ฌ 15 62.5 112.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กด ๋‚จ 12 59 99.5 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ฃผ๋””์—ฌ 14 64.3 90 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ SASHELP์˜ ํ…Œ์ด๋ธ” CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” CLASS๋ฅผ ๊ทธ๋Œ€๋กœ TEST๋กœ ์˜ฎ๊ธฐ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. tip TEST ์•ž์—๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ช…์ด ์—†์œผ๋ฏ€๋กœ ์ž„์‹œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ WORK์— ๋ฐฐ์ •๋ฉ๋‹ˆ๋‹ค. 4. ํฌ๋งท(FORMAT)์˜ ๊ฐœ๋… SAS๋‚˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์—์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์นผ๋Ÿผ(๋ณ€์ˆ˜)์˜ ๋ฐ์ดํ„ฐ ์œ ํ˜•์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ํฌ๊ฒŒ 2๊ฐœ์˜ ์œ ํ˜•์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์ˆซ์ž ๋ฐ์ดํ„ฐ์™€ ๋ฌธ์ž ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋ ‡๊ฒŒ ๋‚˜๋ˆ„๋Š” ์ด์œ ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๊ฒ ๋Š”๋ฐ์š”, ๊ฐ€์žฅ ํฐ ์ด์œ ๋Š” ์ปดํ“จํ„ฐ์˜ ํ•œ๊ณ„ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์›๋ฆฌ๋Š” ์•„๋‹ˆ์ง€๋งŒ, ์‚ฌ๋ก€๋ฅผ ๋“ค์–ด ํฌ๋งท์˜ ์กด์žฌ ์ด์œ ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ํ”ํžˆ๋“ค ๋””์ง€ํ„ธ์ด๋ผ๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ ๋“ค์–ด๋ณด์…จ์„ ๊ฒ๋‹ˆ๋‹ค. ๋””์ง€ํ„ธ์˜ ํŠน์ง•์€ ์„ธ์ƒ์˜ ๋ชจ๋“  ๊ฒƒ์„ 0๊ณผ 1๋กœ ํ‘œํ˜„์„ ํ•˜์ฃ . ์ปดํ“จํ„ฐ๋Š” ๋””์ง€ํ„ธ๋กœ ์ด๋ค„์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ์ƒ์˜ ๋ชจ๋“  ๊ฒƒ์„ 0๊ณผ 1๋กœ ๋ณ€ํ™˜์„ ํ•ด์„œ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ปดํ“จํ„ฐ๋กœ ๋ณด๋Š” ๊ธ€์ž, ์‚ฌ์ง„๋“ค์„ ๊ทผ์›์œผ๋กœ๊นŒ์ง€ ํŒŒํ—ค์ณ ๊ฐ€๋ฉด ๋ชจ๋‘ 0๊ณผ 1๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ๊ฐ€ 0๊ณผ 1์„ ์ˆซ์ž๋กœ ํŒŒ์•…ํ•  ๊ฒƒ์ธ์ง€, ์•„๋‹ˆ๋ฉด ๋ฌธ์ž๋กœ ํŒŒ์•…ํ•  ๊ฒƒ์ธ์ง€ ์Šค์Šค๋กœ๋Š” ํŒ๋‹จ์„ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ปดํ“จํ„ฐ๊ฐ€ ๋ฌธ์ž โ€˜์•ˆ๋…•ํ•˜์„ธ์š”โ€™๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ 10110์ด๋ผ๋Š” ๋””์ง€ํ„ธ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋•Œ ์šฐ๋ฆฌ๊ฐ€ ์ง„์งœ ์ˆซ์ž 10110์„ ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์–ด์„œ 10110์„ ์ž…๋ ฅํ–ˆ๋‹ค๋ฉด ์ปดํ“จํ„ฐ๋Š” ๋‚ด๋ถ€์—์„œ 10110์„ ๊ตฌ๋ถ„ํ•  ๋ฐฉ๋ฒ•์ด ์—†์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ๋ฌธ์ž๋กœ ํ‘œํ˜„์„ ํ• ์ง€, ์ˆซ์ž๋กœ ํ‘œํ˜„์„ ํ• ์ง€ ๋ชจ๋ฅธ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ์นผ๋Ÿผ(๋ณ€์ˆ˜)์„ ์ž…๋ ฅํ•  ๋•Œ ๋ฌธ์ž์ธ์ง€ ์ˆซ์ž์ธ์ง€ ์ปดํ“จํ„ฐ์— ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ ํฌ๋งท์ด๋ผ๋Š” ๊ฒ๋‹ˆ๋‹ค. ์นผ๋Ÿผ์ด ๋ฌธ์ž์ธ์ง€ ์ˆซ์ž์ธ์ง€๋ฅผ ์ •์˜๋ฅผ ๋‚ด๋ ค์„œ 10110์„ ์ฒ˜๋ฆฌํ•  ๊ฒฝ์šฐ, ์ปดํ“จํ„ฐ๊ฐ€ ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š”์ง€ ๋ฏธ๋ฆฌ ์ •ํ•ด๋†“๋Š” ๊ฑฐ์ฃ . ํฌ๋งท์€ ์•ž์œผ๋กœ ๋ฐฐ์šธ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ์˜ ๊ธฐ์ดˆ์ด์ž ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์„ธ๋ถ€์ ์œผ๋กœ ๋ณด๋ฉด ํฌ๋งท์€ ๋งค์šฐ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ์ค‘์š”ํ•˜๊ณ  ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ํฌ๋งท๋“ค์„ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4-1. ์ˆซ์ž ํฌ๋งท ํฌ๋งท์€ ํฌ๊ฒŒ ๋ฌธ์ž์™€ ์ˆซ์ž๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์™€ ์ˆซ์ž๋กœ ๋‚˜๋‰œ ๋‹ค์Œ์—๋„ ๋‹ค์–‘ํ•˜๊ฒŒ ์„ธ๋ถ„ํ™”๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋กœ ์ด๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๊ฐ€ ํฌ๋งท์˜ ์ „๋ถ€๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ํฌ๋งท์˜ ์ข…๋ฅ˜๋Š” ๋” ๋‹ค์–‘ํ•˜๊ฒŒ ์กด์žฌํ•˜์ง€๋งŒ, ์šฐ๋ฆฌ๊ฐ€ ํ‰์†Œ์— SAS๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์•ž์œผ๋กœ ๋ฐฐ์šธ ํฌ๋งท ์ •๋„๋งŒ ์ˆ™์ง€ํ•˜์…”๋„ ์ถฉ๋ถ„ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ˆซ์ž ํฌ๋งท ํฌ๋งท ๋ช…๋ น์–ด ํฌ๋งท ์„ค๋ช… ํฌ๋งท ์˜ˆ์‹œ ์ž…๋ ฅ๊ฐ’ ํฌ๋งท ์ถœ๋ ฅ๊ฐ’ ์ˆซ์ž. ์ž๋ฆฟ์ˆ˜๋งŒํผ ์ •์ˆ˜ ์ˆซ์ž ๊ฐ’์„ ํ‘œ์‹œ 5. 324.5678 325 ์ˆซ์ž. ์ˆซ์ž ์•ž์ž๋ฆฌ ์ˆซ์ž๋งŒํผ ์ „์ฒด ๊ธธ์ด๋ฅผ ํ‘œํ˜„ํ•˜๊ณ , ๋’ท์ž๋ฆฌ ์ˆซ์ž๋งŒํผ ์†Œ์ˆ˜๋กœ ํ‘œํ˜„(๋งŒ์•ฝ ์ž…๋ ฅ๊ฐ’ ๊ธธ์ด๊ฐ€ ์•ž์ž๋ฆฌ ์ˆซ์ž๋ฅผ ์ดˆ๊ณผํ•  ๊ฒฝ์šฐ ์†Œ์ˆ˜์  ์ดํ•˜ ๊ธธ์ด๊ฐ€ ์ถ•์†Œ๋  ์ˆ˜ ์žˆ์Œ) 6.2 324.5678 324.57 BEST ์ˆซ์ž. โ€˜.โ€™์„ ํฌํ•จํ•˜์—ฌ ์ˆซ์ž ์ž๋ฆฟ์ˆ˜๋งŒํผ ์ˆ˜๋ฅผ ํ‘œํ˜„(์ตœ์ƒ์˜ ์ˆซ์ž ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ์•Œ๋ ค์ ธ ์žˆ์Œ) BEST5. 324.5678 324.6 COMMA ์ˆซ์ž. ์ˆซ์ž 3์ž๋ฆฌ ์ˆซ์ž ๊ฐ’๋งˆ๋‹ค โ€˜,โ€™๋ฅผ ํ‘œ์‹œ COMMA5. 3245.678 3,246 PERCENT ์ˆซ์ž. ์ˆซ์ž ์ฃผ์–ด์ง„ ์ˆซ์ž๋ฅผ %๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค(๋‹จ, ์•ž์ž๋ฆฌ ์ˆซ์ž๋Š” ์ „์ฒด ๊ธธ์ด, ๋’ท์ž๋ฆฌ ์ˆซ์ž๋Š” ์†Œ์ˆ˜ ๋ถ€๋ถ„์„ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ ์•ž์ž๋ฆฌ๋Š” 3๋ณด๋‹ค ์ปค์•ผ ํ•ฉ๋‹ˆ๋‹ค. โ€˜%โ€™๊ฐ€ 3์˜ ์œ„์น˜๋งŒํผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค.) PERCENT8.2 0.3245678 32.46% Z ์ˆซ์ž. ์ˆซ์ž ์•ž์ž๋ฆฌ ์ˆ˜๋งŒํผ ์ „์ฒด ์ˆซ์ž ๊ฐ’์„ ํ‘œํ˜„ํ•˜๊ณ  ๋นˆ ๊ธธ์ด๋งŒํผ 0์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. Z7.2 324.5678 0324.57 ์˜ˆ์ œ TEST ํ…Œ์ด๋ธ” ์ƒ์„ฑ DATA TEST; INPUT NAME $ AGE; CARDS; ๋ผ์ด์˜ฌ๋ผ 324.5678 RUN; PROC PRINT DATA=TEST; VAR AGE; FORMAT AGE 5.; /*1๋ฒˆ ์‚ฌ๋ก€*/ FORMAT AGE 6.2; /*2๋ฒˆ ์‚ฌ๋ก€*/ FORMAT AGE BEST5.; /*3๋ฒˆ ์‚ฌ๋ก€*/ FORMAT AGE COMMA5.; /*4๋ฒˆ ์‚ฌ๋ก€*/ FORMAT AGE PERCENT8.2; /*5๋ฒˆ ์‚ฌ๋ก€*/ FORMAT AGE Z7.2; /*6๋ฒˆ ์‚ฌ๋ก€*/ RUN; ์ˆซ์ž ํฌ๋งท์—์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ SAS๋Š” ํฌ๋งท์˜ ๊ธธ์ด๋ฅผ ์ธก์ •ํ•  ๋•Œ โ€˜.โ€™๋„ ๊ธธ์ด์— ํฌํ•จํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 324.56์ด๋ผ๋Š” ์ˆซ์ž๋ฅผ ํฌ๋งท์˜ ๊ฐœ๋…์œผ๋กœ ๋ณผ ๋•Œ ์ „์ฒด ๊ธธ์ด๋Š” โ€˜3โ€™,โ€˜2โ€™,โ€˜4โ€™,โ€˜.โ€™,โ€˜5โ€™,โ€˜6โ€™๊นŒ์ง€ 6๊ฐœ๋กœ ์ธ์‹์ด ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ „์ฒด ๊ธธ์ด๋ฅผ ์„ค์ •ํ•  ๋•Œ โ€˜.โ€™์„ ๊ณ ๋ คํ•ด์„œ ์„ค์ •ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋˜ ํฌ๋งท ์„ค์ • ์‹œ์—๋Š” โ€˜.โ€™์„ ๊ธฐ์ค€์œผ๋กœ ์•ž์ž๋ฆฌ ์ˆ˜์™€ ๋’ท์ž๋ฆฌ ์ˆ˜์˜ ๊ฐœ๋…์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์ž๋ฆฌ ์ˆ˜๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋’ท์ž๋ฆฌ ์ˆ˜๋Š” ์†Œ์ˆ˜์  ์ดํ•˜์˜ ๊ธธ์ด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์•ž์ž๋ฆฌ ์ˆ˜(์ „์ฒด ๊ธธ์ด)๊ฐ€ ์งง์•„ ๋’ท์ž๋ฆฌ ์ˆ˜(์†Œ์ˆ˜์  ์ดํ•˜ ๊ธธ์ด)๋ฅผ ์˜จ์ „ํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ๋’ท์ž๋ฆฌ ์ˆ˜๊ฐ€ ์ผ๋ถ€ ๋ˆ„๋ฝ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. tip ์ฃผ์˜: ํฌ๋งท์„ ์„ค์ •ํ•  ๋•Œ โ€˜.โ€™์„ ์ž…๋ ฅํ•ด ์ฃผ์‹œ๋Š” ๊ฑด ๋Œ€๋‹จํžˆ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. โ€˜5.โ€™, โ€˜BEST4.โ€™, โ€˜Z7.2โ€™์ฒ˜๋Ÿผ ์•ž์ž๋ฆฌ ์ˆ˜(์ „์ฒด ๊ธธ์ด)์˜ ๋‹ค์Œ์— โ€˜.โ€™์ด ์—†์œผ๋ฉด SAS๋Š” ์ด๋ฅผ ํฌ๋งท ๋ช…๋ น์–ด๋กœ ๋ฐ›์•„๋“ค์ด์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํฌ๋งท์„ ์„ค์ •ํ•  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ โ€˜.โ€™์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. 4-2. ๋ฌธ์ž ํฌ๋งท ๋ฌธ์ž ํฌ๋งท ํฌ๋งท ๋ช…๋ น์–ด ํฌ๋งท ์„ค๋ช… ํฌ๋งท ์˜ˆ์‹œ ์ž…๋ ฅ๊ฐ’ ํฌ๋งท ์ถœ๋ ฅ๊ฐ’ $์ˆซ์ž. ์ž๋ฆฟ์ˆ˜๋งŒํผ ๋ฌธ์ž๋ฅผ ํ‘œ์‹œ $6. ๋ผ์ด์˜ฌ๋ผ ๋ผ์ด์˜ฌ ์˜ˆ์ œ TEST ํ…Œ์ด๋ธ” ์ƒ์„ฑ DATA TEST; INPUT NAME $ AGE; CARDS; ๋ผ์ด์˜ฌ๋ผ 324.5678 RUN; PROC PRINT DATA=TEST; VAR NAME; FORMAT NAME $6.; /*1๋ฒˆ ์‚ฌ๋ก€*/ RUN; ๋ฌธ์ž ํฌ๋งท์€ ํ‘œ์‹œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆซ์ž ํฌ๋งท๊ณผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค ์‹œ ์ด ํ•˜ ์ˆซ ํฌ๊ณผ ๋ถ„ ๋„ค. 4., $5.์ฒ˜๋Ÿผ ํ‘œํ˜„์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ 4,5์™€ ๊ฐ™์€ ์ˆซ์ž๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ ๋ฌธ์ž ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒŒ ์•„๋‹™๋‹ˆ๋‹ค. ๊ธธ์ด๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ 1BYTE๋กœ ํ•ด์„์„ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜์–ด๋Š” 1BYTE์— ํ•œ ์ž๊ฐ€, ํ•œ๊ตญ์–ด๋Š” 2BYTE์— ํ•œ ๊ธ€์ž๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ธธ์ด๋ฅผ ๋„˜์–ด์„œ๋Š” ๋ฌธ์ž๋ถ€ํ„ฐ๋Š” ํ‘œํ˜„์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. tip ์ฃผ์˜: ํฌ๋งท์„ ์„ค์ •ํ•  ๋•Œ โ€˜.โ€™์„ ์ž…๋ ฅํ•ด ์ฃผ์‹œ๋Š” ๊ฑด ๋Œ€๋‹จํžˆ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. โ€˜ 6. , 7.โ€™์ฒ˜๋Ÿผ ํฌ๋งท<NAME>์˜ ๋งˆ์ง€๋ง‰์— โ€˜.โ€™์„ ์ฐ์–ด์ฃผ์„ธ์š”. ์ด๋Š” ๋ชจ๋“  ํฌ๋งท์— ๋™์ผํ•œ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. 4-3. ๋‚ ์งœ ํฌ๋งท ๋‚ ์งœ ํฌ๋งท ํฌ๋งท ๋ช…๋ น์–ด ํฌ๋งท ์„ค๋ช… ํฌ๋งท ์˜ˆ์‹œ ์ž…๋ ฅ๊ฐ’ ํฌ๋งท ์ถœ๋ ฅ๊ฐ’ DATE ์ˆซ์ž. ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์ผ์›” ๋…„) DATE9. 3 4JAN1960 YYMMDD ์ˆซ์ž. ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์—ฐ๋„-์›”-์ผ) YYMMDD10. 3 1960-01-04 WEEKDATE. ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์š”์ผ, ์›”์ผ, ์—ฐ๋„) WEEKDATE. 3 Monday, January 4, 1960 WORDDATE. ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์›”์ผ, ์—ฐ๋„) WORDDATE 3 January 4, 1960 NLDATE ์ˆซ์ž. ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์—ฐ๋„ ์›” ์ผ) NLDATE20. 3 1960๋…„ 01์›” 04์ผ YYMMN ์ˆซ์ž. ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์—ฐ๋„ ์›”) YYMMN6. 3 196001 MONYY ์ˆซ์ž. ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์›” ์—ฐ๋„) MONYY7. 3 JAN1960 YEAR ์ˆซ์ž. ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์—ฐ๋„) YEAR4. 3 1960 DATETIME ์ˆซ์ž. ์ˆซ์ž ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ, ์‹œ๊ฐ„ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์ผ์›” ๋…„:์‹œ:๋ถ„:์ดˆ) DATETIME20. 3 01JAN1960:00:00:03 TIME ์ˆซ์ž. ์ˆซ์ž ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ, ์‹œ๊ฐ„ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์‹œ:๋ถ„:์ดˆ) TIME8. 3333 0:55:03 HHMM ์ˆซ์ž. ์ˆซ์ž ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ, ์‹œ๊ฐ„ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์‹œ:๋ถ„) HHMM5. 3333 0:56 HOUR ์ˆซ์ž. ์ˆซ์ž ์ˆซ์ž ๊ฐ’์„ ๋‚ ์งœ, ์‹œ๊ฐ„ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ(์‹œ๊ฐ) HOUR5. 3333 1 ์˜ˆ์ œ TEST ํ…Œ์ด๋ธ” ์ƒ์„ฑ DATA TEST; INPUT NAME $ NUMBER; CARDS; ๋‚ ์งœ 3 RUN; PROC PRINT DATA=TEST; VAR NUMBER; FORMAT NUMBER DATE10.; /*1๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER YYMMDD10.; /*2๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER WEEKDATE.; /*3๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER WORDDATE.; /*4๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER NLDATE20.; /*5๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER YYMMN6.; /*6๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER MONYY7.; /*7๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER YEAR4.; /*8๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER DATETIME20. ; /*9๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER TIME8. ; /*10๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER HHMM5. ; /*11๋ฒˆ ์‚ฌ๋ก€*/ FORMAT NUMBER HOUR5. ; /*12๋ฒˆ ์‚ฌ๋ก€*/ RUN; SAS์—์„œ ๋‚ ์งœ ํฌ๋งท์€ ๊ธฐ์กด์˜ ์ˆซ์ž, ๋ฌธ์ž ํฌ๋งท๊ณผ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ฐœ๋…๋ถ€ํ„ฐ ์„ค๋ช…ํ•˜์ž๋ฉด SAS์—์„œ๋Š” 1960๋…„ 1์›” 1์ผ์„ ์ˆซ์ž 0์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. 1์€ 1960๋…„ 1์›” 2์ผ, 2๋Š” 1960๋…„ 1์›” 3์ผ์ธ ์‹์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ 1,2,3 ๊ฐ™์€ ์ˆซ์ž๊ฐ€ ์ˆซ์ž ํฌ๋งท์ธ์ง€ ๋‚ ์งœ ํฌ๋งท์ธ์ง€๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์€ ์ค‘์š”ํ•œ ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ 3์€ ์ˆซ์ž๋ฅผ ๋‚ ์งœ ํฌ๋งท์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” TEST์— ์นผ๋Ÿผ NUMBER์— 3์ด๋ผ๋Š” ์ˆซ์ž๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ SAS๋Š” ์ด๋ฅผ 1960๋…„ 1์›” 4์ผ๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด YYMMDD10.<NAME>์˜ ํฌ๋งท์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ โ€˜1960-01-04โ€™์™€ ๊ฐ™์ด ํ‘œํ˜„์ด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ฌธ์ž๋“ค์˜ ์ด ๊ธธ์ด 10์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋’ค์ชฝ์˜ ์ˆซ์ž๋ฅผ ๋Š˜์ด๊ธฐ๋„, ์ค„์ด๊ธฐ๋„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ํฌ๋งท์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— ์ˆซ์ž๋Š” ๊ทธ ํฌ๋งท์˜ ๊ธธ์ด๋ฅผ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๋‚ ์งœ ํฌ๋งท์€ ์ •ํ•ด์ง„ ๊ธธ์ด๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„์„œ ์›ํ•˜๋Š” ๋Œ€๋กœ ๊ธธ์ด๋ฅผ ์œ ๋™์ ์œผ๋กœ ์„ค์ •ํ•˜๊ธฐ๋Š” ์–ด๋ ค์šด ํŽธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด YYMMDD8.์œผ๋กœ ํฌ๋งท์„ ์„ค์ •ํ•  ๊ฒฝ์šฐ ๊ฒฐ๊ด๊ฐ’์€ โ€˜60-01-04โ€™๋กœ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„ ์ˆซ์ž๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ํ˜•ํƒœ์˜ ๋‚ ์งœ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ YYMMDD12.์ฒ˜๋Ÿผ ํฌ๋งท์˜ ํ•œ๊ณ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ธธ์ด๋ฅผ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‚ ์งœ ๊ฐ’์„ ์ˆซ์ž๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์€ ์ดํ›„์— ์žˆ์„ ๊ณผ์ •์—์„œ ์•Œ์•„๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. tip ์ฃผ์˜: ํฌ๋งท์„ ์„ค์ •ํ•  ๋•Œ โ€˜.โ€™์„ ์ž…๋ ฅํ•ด ์ฃผ์‹œ๋Š” ๊ฑด ๋Œ€๋‹จํžˆ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. โ€˜.โ€™์ด ๋งˆ์ง€๋ง‰์— ์ž…๋ ฅ์ด ๋ผ ์žˆ์–ด์•ผ SAS๋Š” ์ด๋ฅผ ๋‚ ์งœ ํฌ๋งท์œผ๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ํฌ๋งท์„ ์ž…๋ ฅํ•  ๋•Œ๋Š” โ€˜.โ€™์„ ์žŠ์ง€ ๋งˆ์„ธ์š”. 5. ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ SAS ๋ช…๋ น์–ด ์ง€๊ธˆ๋ถ€ํ„ฐ ๋ณธ๊ฒฉ์ ์œผ๋กœ SAS๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ๋ฌด์กฐ๊ฑด ์—‘์…€๊ณผ ๊ฐ™์ด ํ–‰๋ ฌ๋กœ ์ด๋ค„์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์—ด์„ '์นผ๋Ÿผ' ๋˜๋Š” ๋ณ€์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์ „์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 5-1. KEEP๊ณผ DROP ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•ด์„œ ํ…Œ์ด๋ธ”์—์„œ ์œ ์ง€ํ•  ์นผ๋Ÿผ์€ KEEP, ๋ฒ„๋ฆด ์นผ๋Ÿผ์€ DROP์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”์—์„œ ์„ ํƒํ•˜๊ณ ์ž ํ•˜๋Š” ์นผ๋Ÿผ์ด ์žˆ๊ฑฐ๋‚˜ ์ œ์™ธํ•˜๊ณ ์ž ํ•˜๋Š” ์นผ๋Ÿผ์ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ 3๊ฐœ์˜ ์˜ˆ์ œ๋Š” ๋น„์Šทํ•˜์ง€๋งŒ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋ชจ๋‘ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด DATA XXX: ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ” XXX๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค SET YYY: ๊ธฐ์กด ํ…Œ์ด๋ธ” YYY๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. KEEP ZZZ: ์นผ๋Ÿผ ZZZ๋ฅผ ํ…Œ์ด๋ธ”์—์„œ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. DROP PPP: ์นผ๋Ÿผ PPP๋ฅผ ํ…Œ์ด๋ธ”์—์„œ ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ์˜ˆ์ œ 1 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS(KEEP=NAME AGE);/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋˜ ์นผ๋Ÿผ AGE์™€ NAME๋งŒ์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ NAME AGE ์•Œํ”„๋ ˆ๋“œ 14 ์•จ๋ฆฌ์Šค 13 ๋ฐ”๋ฐ”๋ผ 13 ์บ๋Ÿด 14 ํ—จ๋ฆฌ 14 ์ œ์ž„์Šค 12 ์ œ์ธ 12 ์ž๋„ท 15 ์ œํ”„๋ฆฌ 13 ์กด 12 ์กฐ์ด์Šค 11 ์ฃผ๋”” 14 ๋ฃจ์ด์Šค 12 ๋ฉ”๋ฆฌ 15 ํ•„๋ฆฝ 16 ๋กœ๋ฒ„ํŠธ 12 ๋กœ๋‚ ๋“œ 15 ํ† ๋งˆ์Šค 11 ์œŒ๋ฆฌ์—„ 15 SET ๋ช…๋ น์–ด ๋‹ค์Œ์— KEEP ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ–ˆ์œผ๋ฏ€๋กœ SASHELP.CLASS ํ…Œ์ด๋ธ”์—์„œ ์นผ๋Ÿผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ NAME๊ณผ AGE๋งŒ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์˜ˆ์ œ 2 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS(DROP=NAME AGE);/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋˜ ์นผ๋Ÿผ AGE์™€ NAME์„ ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ SEX WEIGHT HEIGHT ๋‚จ 69 112.5 ์—ฌ 56.5 84 ์—ฌ 65.3 98 ์—ฌ 62.8 102.5 ๋‚จ 63.5 102.5 ๋‚จ 57.3 83 ์—ฌ 59.8 84.5 ์—ฌ 62.5 112.5 ๋‚จ 62.5 84 ๋‚จ 59 99.5 ์—ฌ 51.3 50.5 ์—ฌ 64.3 90 ์—ฌ 56.3 77 ์—ฌ 66.5 112 ๋‚จ 72 150 ๋‚จ 64.8 128 ๋‚จ 67 133 ๋‚จ 57.5 85 ๋‚จ 66.5 112 SET ๋ช…๋ น์–ด ๋‹ค์Œ์— DROP ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ SASHELP.CLASS ํ…Œ์ด๋ธ”์—์„œ ์นผ๋Ÿผ NAME๊ณผ AGE๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์นผ๋Ÿผ๋“ค์„ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 3 DATA TEST(KEEP=NAME AGE);/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•˜๋˜ ์นผ๋Ÿผ NAME๊ณผ AGE๋งŒ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ NAME AGE ์•Œํ”„๋ ˆ๋“œ 14 ์•จ๋ฆฌ์Šค 13 ๋ฐ”๋ฐ”๋ผ 13 ์บ๋Ÿด 14 ํ—จ๋ฆฌ 14 ์ œ์ž„์Šค 12 ์ œ์ธ 12 ์ž๋„ท 15 ์ œํ”„๋ฆฌ 13 ์กด 12 ์กฐ์ด์Šค 11 ์ฃผ๋”” 14 ๋ฃจ์ด์Šค 12 ๋ฉ”๋ฆฌ 15 ํ•„๋ฆฝ 16 ๋กœ๋ฒ„ํŠธ 12 ๋กœ๋‚ ๋“œ 15 ํ† ๋งˆ์Šค 11 ์œŒ๋ฆฌ์—„ 15 DATA ๋ช…๋ น์–ด ๋‹ค์Œ์— KEEP ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ SASHELP.CLASS ํ…Œ์ด๋ธ”์—์„œ ๋ชจ๋“  ์นผ๋Ÿผ๋ฅผ ๊ฐ€์ ธ์˜จ ๋‹ค์Œ, ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑ์‹œํ‚ฌ ๋•Œ ์นผ๋Ÿผ NAME๊ณผ AGE๋งŒ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. tip KEEP๊ณผ DROP ๋ช…๋ น์–ด๋กœ ์›ํ•˜๋Š” ์นผ๋Ÿผ๋งŒ์„ ์œ ์ง€ํ•˜๊ฑฐ๋‚˜, ์›ํ•˜์ง€ ์•Š๋Š” ์นผ๋Ÿผ์„ ๋ฒ„๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. KEEP๊ณผ DROP์€ SET ๋ช…๋ น์–ด๋‚˜ DATA ๋ช…๋ น์–ด ๋ชจ๋‘์— ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ KEEP ๋ช…๋ น์–ด๋ฅผ SET ๋‹จ๊ณ„์—์„œ ์“ฐ๋Š” ๊ฒƒ๊ณผ DATA ๋‹จ๊ณ„์—์„œ ์“ฐ๋Š” ๊ฒƒ์€ ์—„์—ฐํžˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋งŒ์•ฝ SET ๋‹จ๊ณ„๊นŒ์ง€๋Š” ์นผ๋Ÿผ WEIGHT, NAME, AGE๊ฐ€ ํ•„์š”ํ•˜๊ณ  ์ดํ›„ ๊ฒฐ๊ด๊ฐ’์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ณผ์ •์—์„œ NAME๊ณผ AGE๋งŒ ํ•„์š”ํ•˜๋‹ค๋ฉด SET ๋‹จ๊ณ„์—์„œ KEEP ๋ช…๋ น์–ด๋กœ WEIGHT, NAME, AGE๋ฅผ ์ง€์ •ํ•ด ์ฃผ๊ณ  DATA ๋‹จ๊ณ„์—์„œ KEEP ๋ช…๋ น์–ด๋กœ NAME, AGE๋งŒ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ฒ˜์Œ๋ถ€ํ„ฐ SET์—์„œ ์นผ๋Ÿผ NAME๊ณผ AGE๋งŒ ํ•„์š”ํ•˜๋‹ค๋ฉด SET ๋‹จ๊ณ„์—์„œ KEEP ๋ช…๋ น์–ด๋กœ ์นผ๋Ÿผ NAME, AGE๋งŒ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ์ค‘์š”ํ•˜์ง€ ์•Š์•„ ๋ณด์ด์ง€๋งŒ ์ด ์œ„์น˜๋Š” ์ถ”ํ›„ ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๊ณผ์ •์— ๋”ฐ๋ผ ์ค‘์š”ํ•œ ์˜๋ฏธ๋ฅผ ์ง€๋‹ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 5-2. ์ˆ˜์‹์„ ํ™œ์šฉํ•œ ๊ณ„์‚ฐ SAS์—์„œ ์ง์ ‘ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +,-,รท, ร— ์™€ ๊ฐ™์€ ์‚ฌ์น™์—ฐ์‚ฐ์„ ์‹œํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SAS๋Š” ๋ช…๋ น์–ด๋ฅผ ์œ„์—์„œ๋ถ€ํ„ฐ ์ฝ๊ณ , ๊ฐ™์€ ์ค„์ด๋ผ๋ฉด ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ฝ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ™์€ ์ ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์Šต๋„ ํ•จ๊ป˜ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด YYY=XXX+N;: ์นผ๋Ÿผ YYY๋ฅผ ์ˆซ์ž ์นผ๋Ÿผ XXX์— ์ˆซ์ž N์„ ๋”ํ•œ ๋ณ€์ˆ˜๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. YYY+N;: ์ˆซ์ž ์นผ๋Ÿผ YYY์— ์ˆซ์ž N์„ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 1 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ AGE2=AGE+1;/*์นผ๋Ÿผ AGE2๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ AGE2๋Š” ์นผ๋Ÿผ AGE์— 1์„ ๋”ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight AGE2 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 15 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 14 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 14 ์บ๋Ÿด์—ฌ 14 62.8 102.5 15 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 15 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 13 ์ œ์ธ์—ฌ 12 59.8 84.5 13 ์ž๋„ท์—ฌ 15 62.5 112.5 16 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 14 ์กด ๋‚จ 12 59 99.5 13 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 12 ์ฃผ๋””์—ฌ 14 64.3 90 15 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 13 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 16 ํ•„๋ฆฝ ๋‚จ 16 72 150 17 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 13 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 16 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 12 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 16 SASHELP.CLASS์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ๋‹ค์Œ์œผ๋กœ ์นผ๋Ÿผ AGE2๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ AGE2๋Š” ๊ธฐ์กด์— ์žˆ๋˜ ์นผ๋Ÿผ AGE์˜ ๊ฐ’์— 1์”ฉ์„ ๋”ํ•œ ๊ฒƒ์ด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 2 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ AGE+1;/*์นผ๋Ÿผ AGE์— 1์„ ๋”ํ•œ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.*/ AGE2=AGE+1;/*์นผ๋Ÿผ AGE2๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ AGE2๋Š” ์นผ๋Ÿผ AGE์— 1์„ ๋”ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight AGE2 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 15 69 112.5 15 ์•จ๋ฆฌ์Šค์—ฌ 14 56.5 84 14 ๋ฐ”๋ฐ”๋ผ์—ฌ 14 65.3 98 14 ์บ๋Ÿด์—ฌ 15 62.8 102.5 15 ํ—จ๋ฆฌ ๋‚จ 15 63.5 102.5 15 ์ œ์ž„์Šค ๋‚จ 13 57.3 83 13 ์ œ์ธ์—ฌ 13 59.8 84.5 13 ์ž๋„ท์—ฌ 16 62.5 112.5 16 ์ œํ”„๋ฆฌ ๋‚จ 14 62.5 84 14 ์กด ๋‚จ 13 59 99.5 13 ์กฐ์ด์Šค์—ฌ 12 51.3 50.5 12 ์ฃผ๋””์—ฌ 15 64.3 90 15 ๋ฃจ์ด์Šค์—ฌ 13 56.3 77 13 ๋ฉ”๋ฆฌ์—ฌ 16 66.5 112 16 ํ•„๋ฆฝ ๋‚จ 17 72 150 17 ๋กœ๋ฒ„ํŠธ ๋‚จ 13 64.8 128 13 ๋กœ๋‚ ๋“œ ๋‚จ 16 67 133 16 ํ† ๋งˆ์Šค ๋‚จ 12 57.5 85 12 ์œŒ๋ฆฌ์—„ ๋‚จ 16 66.5 112 16 ๋ณ€์ˆ˜ AGE2๋ฅผ ์ƒ์„ฑํ•œ ๋‹ค์Œ ์นผ๋Ÿผ AGE๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค. AGE+1๋กœ์จ ์นผ๋Ÿผ AGE์— 1์”ฉ์„ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. 5-3. ๋‹จ์ˆœ IF ๊ตฌ๋ฌธ IF๋ฅผ ํ™œ์šฉํ•œ ๊ตฌ๋ฌธ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. IF๋ž€ โ€˜๋งŒ์•ฝ~๋ผ๋ฉดโ€™์ด๋ผ๋Š” ์˜๋ฏธ์ธ๋ฐ SAS์—์„œ๋Š” ์ด๋ฅผ ์กฐ๊ฑด๋ถ€ ๋ฌธ์žฅ์œผ๋กœ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ํŽธ์ง‘ํ•˜๋Š” ํ•˜๋‚˜์˜ ๊ธฐ์ค€์„ ์ œ์‹œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด IF AGE=12๋ผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๋ฉด ์ด๋Š” โ€˜AGE๊ฐ€ 12์ธ ํ–‰๋“ค์„ ์„ ํƒํ•œ๋‹ค.โ€™๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์นผ๋Ÿผ์ด ์ˆซ์ž ์นผ๋Ÿผ์ด๋ผ๋ฉด 1,2,3 ๋“ฑ ์ˆซ์ž๋ฅผ ๊ทธ๋Œ€๋กœ ์ž…๋ ฅํ•˜๊ณ , ๋ฌธ์ž ์นผ๋Ÿผ์ด๋ผ๋ฉด โ€˜ํ•„๋ฆฝโ€™,โ€˜๋ฃจ์ด์Šคโ€™์ฒ˜๋Ÿผ โ€˜ โ€™์•ˆ์— ์…€์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ ์–ด์ค๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ํ™œ์šฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด IF XXX=N;: ์ˆซ์ž ์นผ๋Ÿผ XXX๊ฐ€ N์ธ ํ–‰๋“ค๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. IF YYY=โ€˜ZZโ€™;: ๋ฌธ์ž ์นผ๋Ÿผ YYY๊ฐ€ ZZ์ธ ํ–‰๋“ค๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. IF PPP^=N;: ์ˆซ์ž ์นผ๋Ÿผ PPP๊ฐ€ N์ด ์•„๋‹Œ ํ–‰๋“ค๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค(^=). IF XXX=N AND YYY=โ€˜ZZโ€™;: ์นผ๋Ÿผ XXX๊ฐ€ N์ด๊ณ  ์นผ๋Ÿผ YYY๊ฐ€ ZZ์ธ ํ–‰์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค(AND, OR). IF XXX IN (N1, N2);: ์นผ๋Ÿผ XXX๊ฐ€ N1์ด๊ฑฐ๋‚˜ N2์ธ ํ–‰์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค(IN, NOT IN). ์˜ˆ์ œ 1 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ IF AGE=12;/*์นผ๋Ÿผ AGE๊ฐ€ 12์ธ ํ–‰๋“ค๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. */ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์กด ๋‚จ 12 59 99.5 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ์˜ˆ์ œ. 1์—์„œ๋Š” IF ๋ฌธ์œผ๋กœ ์นผ๋Ÿผ AGE=12๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฐ๊ด๊ฐ’์œผ๋กœ AGE ๊ฐ’์ด 12์ธ ํ–‰๋“ค์ด ์„ ํƒ๋˜์–ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 2 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ IF NAME='ํ•„๋ฆฝ';/*์นผ๋Ÿผ NAME์ด 'ํ•„๋ฆฝ'์ธ ํ–‰๋“ค๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight ํ•„๋ฆฝ ๋‚จ 16 72 150 ์˜ˆ์ œ. 2์—์„œ๋Š” IF ๋ฌธ์œผ๋กœ ์นผ๋Ÿผ NAME=โ€˜ํ•„๋ฆฝโ€™์„ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์นผ๋Ÿผ NAME์ด โ€˜ํ•„๋ฆฝโ€™์ธ ํ–‰์ด ์ถœ๋ ฅ๋์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ 3 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ IF AGE^=12;/*์นผ๋Ÿผ AGE๊ฐ€ 12๊ฐ€ ์•„๋‹Œ ํ–‰๋“ค๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ž๋„ท์—ฌ 15 62.5 112.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ฃผ๋””์—ฌ 14 64.3 90 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ์˜ˆ์ œ 3.์—์„œ๋Š” ์นผ๋Ÿผ AGE์˜ ๊ฐ’์ด 12๊ฐ€ ์•„๋‹Œ ํ–‰๋“ค๋งŒ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. SAS์—์„œ๋Š” โ€˜๊ฐ™์ง€ ์•Š๋‹คโ€™๋ผ๋Š” ์˜๋ฏธ๋กœ โ€˜^=(NOT EQAUL)โ€™์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์–ด๋Š ๋ช…๋ น์–ด์—์„œ๋“  โ€˜^=โ€™์„ ๋ถ™์ด๋ฉด โ€˜๊ฐ™์ง€ ์•Š๋‹คโ€™๋ผ๋Š” ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 4 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ IF AGE=12 AND NAME='์กด';/*์นผ๋Ÿผ AGE๊ฐ€ 12์ด๊ณ  ์นผ๋Ÿผ NAME์ด โ€˜์กดโ€™์ธ ํ–‰์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight ์กด ๋‚จ 12 59 99.5 ์˜ˆ์ œ 4.์—์„œ๋Š” AND ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. โ€˜(1) AND (2)โ€™ ๊ตฌ์กฐ๋Š” โ€˜(1)์ด๋ฉด์„œ (2)์ธ ๊ฐ’๋“ค์„ ์„ ํƒโ€™ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ˆ˜ํ•™์—์„œ ๊ต์ง‘ํ•ฉ์˜ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ˆ์ œ 4์—์„œ๋Š” ์นผ๋Ÿผ AGE์˜ ๊ฐ’์ด 12์ด๋ฉด์„œ ์นผ๋Ÿผ NAME์˜ ๊ฐ’์ด โ€˜์กดโ€™์ธ ํ–‰๋งŒ ์„ ํƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 5 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ IF AGE=12 OR AGE=13;/*๋ณ€์ˆ˜ AGE๊ฐ€ 12์ด๊ฑฐ๋‚˜ 13์ธ ํ–‰์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กด ๋‚จ 12 59 99.5 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ์˜ˆ์ œ 5.๋Š” ๋ช…๋ น์–ด OR์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. โ€˜(1) OR (2)โ€™ ๊ตฌ์กฐ๋Š” โ€˜(1)์ด๊ฑฐ๋‚˜ (2)์ธ ๊ฐ’๋“ค์„ ์„ ํƒโ€™ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ช…๋ น์–ด โ€˜ANDโ€™๊ฐ€ ๊ต์ง‘ํ•ฉ์˜ ๊ฐœ๋…์ด๋ผ๋ฉด OR์€ ํ•ฉ์ง‘ํ•ฉ์˜ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์นผ๋Ÿผ AGE๊ฐ€ 12์ด๊ฑฐ๋‚˜ 13์ธ ํ–‰์„ ์„ ํƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 6 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ IF AGE IN (12,13);/*์นผ๋Ÿผ AGE๊ฐ€ 12์ด๊ฑฐ๋‚˜ 13์ธ ํ–‰์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กด ๋‚จ 12 59 99.5 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ์˜ˆ์ œ 6.์—์„œ๋Š” ๋ช…๋ น์–ด IN์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. IN์€ ์˜ˆ์ œ 5์—์„œ ๋ฐฐ์šด OR์˜ ๊ฐœ๋…๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ AGE์—์„œ 12์™€ 13์ธ ํ–‰์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์˜ˆ์ œ 5์™€ ์˜ˆ์ œ 6์˜ ๊ฒฐ๊ด๊ฐ’์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. OR๋กœ ํ•  ๋•Œ๋ณด๋‹ค ๋ช…๋ น์–ด ๊ธธ์ด๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์–ด์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์ˆซ์ž ์นผ๋Ÿผ์ด ์•„๋‹ˆ๋ผ ๋ฌธ์ž ์นผ๋Ÿผ์ด๋ผ๋ฉด โ€˜๋ฌธ์žโ€™ ์‹์œผ๋กœ ์ฝ”๋”ฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์˜ˆ์ œ 6์˜ ๊ฒฐ๊ณผ์™€๋Š” ๋ฐ˜๋Œ€๋กœ ์นผ๋Ÿผ AGE๊ฐ€ 12์™€ 13์ด ์•„๋‹Œ ํ–‰์„ ์„ ํƒํ•˜๋ ค๋ฉด โ€˜NOT INโ€™์œผ๋กœ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. tip SAS ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ์ˆซ์ž ์นผ๋Ÿผ ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•  ๋•Œ๋Š” ์ˆซ์ž๋ฅผ ๊ทธ๋Œ€๋กœ ์“ฐ๊ณ , ๋ฌธ์ž ์นผ๋Ÿผ ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•  ๋•Œ๋Š” โ€˜ โ€™๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ์ง€ ์•Š์„ ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋‹ˆ ์ฃผ์˜ํ•˜์„ธ์š”. 5-4. IF ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•œ ์นผ๋Ÿผ ๋ณ€ํ™˜ IF ๋ฌธ์—์„œ ํ•œ ๋ฐœ์ž๊ตญ ๋” ๋‚˜์•„๊ฐ€์„œ IF๋ฅผ ํ™œ์šฉํ•œ ์นผ๋Ÿผ ๋ณ€ํ™˜์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. IF ๋ฌธ์€ ์กฐ๊ฑด๋ถ€ ๋ช…๋ น์–ด์ธ๋ฐ THEN๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ž…๋‹ˆ๋‹ค. IF~THEN~๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ์กฐ๊ฑด(IF ๋ฌธ)์„ ์ฃผ๊ณ  ๊ทธ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ํ–‰์— ๋”ฐ๋ผ ๋ณ€ํ™”(THEN)๋ฅผ ์ฃผ๋Š” ๊ฑฐ์ฃ . ํ…Œ์ด๋ธ”์„ ๊ฐ€๊ณตํ•  ๋•Œ ํŠน์ • ์ƒํ™ฉ์—์„œ ์นผ๋Ÿผ์„ ๋ณ€ํ™˜ํ•  ๋•Œ ์ด์šฉํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด IF XXX=N THEN YYY+2;: ์นผ๋Ÿผ XXX๊ฐ€ N ์ด๋ฉด ์นผ๋Ÿผ YYY์— 2๋ฅผ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. IF XXX=N THEN DELETE;: ์นผ๋Ÿผ XXX๊ฐ€ N ์ด๋ฉด ํ•ด๋‹น ํ–‰์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. IF XXX=N THEN NEW=โ€˜์ƒˆ๋กœ์šด ์นผ๋Ÿผโ€™;: ์นผ๋Ÿผ XXX๊ฐ€ N ์ด๋ช… ์นผ๋Ÿผ NEW๋ฅผ ์ƒ์„ฑํ•˜๊ณ  โ€˜์ƒˆ๋กœ์šด ์นผ๋Ÿผโ€™์ด๋ผ๋Š” ๊ฐ’์ด ๋‚˜์˜ค๋„๋ก ๋งŒ๋“ญ๋‹ˆ๋‹ค. IF XXX=N THEN NEW=YYY+2; ELSE NEW=ZZZ+3;: ์นผ๋Ÿผ XXX๊ฐ€ N ์ด๋ฉด ์นผ๋Ÿผ YYY์— 2๋ฅผ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  XXX๊ฐ€ N์ด ์•„๋‹Œ ๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ ZZZ์— 3์„ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 1 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค*/ IF AGE=12 THEN HEIGHT+300;/*์นผ๋Ÿผ AGE๊ฐ€ 12์ด๋ฉด ์นผ๋Ÿผ HEIGHT ๊ฐ’์— 300์„ ๋”ํ•ฉ๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ œ์ž„์Šค ๋‚จ 12 357.3 83 ์ œ์ธ์—ฌ 12 359.8 84.5 ์ž๋„ท์—ฌ 15 62.5 112.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กด ๋‚จ 12 359 99.5 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ฃผ๋””์—ฌ 14 64.3 90 ๋ฃจ์ด์Šค์—ฌ 12 356.3 77 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 364.8 128 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ์˜ˆ์ œ 1์€ IF์™€ THEN์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์นผ๋Ÿผ AGE๊ฐ€ 12์ธ ํ–‰์ด ์žˆ๋‹ค๋ฉด ์นผ๋Ÿผ HEIGHT์— ๊ฐ๊ฐ 300์”ฉ์„ ๋”ํ•˜๋ผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์นผ๋Ÿผ HEIGHT ๋ชจ๋“  ๊ฐ’์— 300์„ ๋”ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ์นผ๋Ÿผ AGE๊ฐ€ 12์ธ ํ–‰์—๋งŒ 300์”ฉ์„ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 2 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ IF AGE=12 THEN DELETE;/*์นผ๋Ÿผ AGE๊ฐ€ 12์ด๋ฉด ํ•ด๋‹น ํ–‰์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ž๋„ท์—ฌ 15 62.5 112.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ฃผ๋””์—ฌ 14 64.3 90 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ์˜ˆ์ œ 2๋„ ๋™์ผํ•œ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์นผ๋Ÿผ AGE๊ฐ€ 12์ผ ๊ฒฝ์šฐ ํ•ด๋‹น ํ–‰์„ ์‚ญ์ œ(DELETE) ํ•˜๋„๋ก ๋ช…๋ น์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ถœ๋ ฅ๊ฐ’์„ ๋ณด๋ฉด ์นผ๋Ÿผ AGE๊ฐ€ 12์ธ ๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ 3 DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS;/*ํ…Œ์ด๋ธ” SASHELP.CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ IF AGE=12 THEN NEW='์ƒˆ๋กœ์šด ์นผ๋Ÿผ';/*์นผ๋Ÿผ AGE๊ฐ€ 12์ด๋ฉด ์ƒˆ๋กœ์šด ์นผ๋Ÿผ NEW์— โ€˜์ƒˆ๋กœ์šด ์นผ๋Ÿผโ€™์ด๋ผ๋Š” ๊ฐ’์„ ์ค๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight NEW ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ ์ œ์ธ์—ฌ 12 59.8 84.5 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ ์ž๋„ท์—ฌ 15 62.5 112.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กด ๋‚จ 12 59 99.5 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ฃผ๋””์—ฌ 14 64.3 90 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ์˜ˆ์ œ 3์€ ์ƒˆ๋กœ์šด ์นผ๋Ÿผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์นผ๋Ÿผ AGE๊ฐ€ 12์ผ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ์นผ๋Ÿผ NEW๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฑฐ๊ธฐ์— โ€˜์ƒˆ๋กœ์šด ์นผ๋Ÿผโ€™์ด๋ผ๋Š” ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ์˜ˆ์ œ 1๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์นผ๋Ÿผ NEW ์ „์ฒด์— โ€˜์ƒˆ๋กœ์šด ์นผ๋Ÿผโ€™ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ์นผ๋Ÿผ AGE๊ฐ€ 12์ธ ํ–‰์—๋งŒ โ€˜์ƒˆ๋กœ์šด ์นผ๋Ÿผโ€™๊ฐ’์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์นผ๋Ÿผ NEW ์ „์ฒด์— โ€˜์ƒˆ๋กœ์šด ์นผ๋Ÿผโ€™์ด๋ผ๋Š” ๊ฐ’์„ ์ž…๋ ฅํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด IF ๋ฌธ์„ ์‚ฌ์šฉํ•  ํ•„์š” ์—†์ด โ€˜NEW=โ€˜์ƒˆ๋กœ์šด ์นผ๋Ÿผโ€™โ€™๋ผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 4 DATA TEST; SET SASHELP.CLASS; IF AGE=12 THEN NEW='์ƒˆ๋กœ์šด ์นผ๋Ÿผ'; ELSE IF AGE=13 THEN NEW='์ƒˆ๋กœ์šด ์นผ๋Ÿผ 2'; ELSE NEW='์ƒˆ ์นผ๋Ÿผ'; RUN; Name Sex Age Height Weight NEW ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์ƒˆ ๋ณ€์ˆ˜ ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ 2 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ 2 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ์ƒˆ ์นผ๋Ÿผ ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ƒˆ ์นผ๋Ÿผ ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ ์ œ์ธ์—ฌ 12 59.8 84.5 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ ์ž๋„ท์—ฌ 15 62.5 112.5 ์ƒˆ ์นผ๋Ÿผ ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ 2 ์กด ๋‚จ 12 59 99.5 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ƒˆ ์นผ๋Ÿผ ์ฃผ๋””์—ฌ 14 64.3 90 ์ƒˆ ์นผ๋Ÿผ ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ์ƒˆ ์นผ๋Ÿผ ํ•„๋ฆฝ ๋‚จ 16 72 150 ์ƒˆ ์นผ๋Ÿผ ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ์ƒˆ๋กœ์šด ์นผ๋Ÿผ ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ์ƒˆ ์นผ๋Ÿผ ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์ƒˆ ์นผ๋Ÿผ ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ์ƒˆ ์นผ๋Ÿผ ์˜ˆ์ œ 4์ฒ˜๋Ÿผ IF AGE=12 THEN NEW=โ€˜์ƒˆ๋กœ์šด ์นผ๋Ÿผโ€™ ๋ช…๋ น์–ด๊ฐ€ ์ข…๋ฃŒ๋œ ๋‹ค์Œ ELSE ๋ช…๋ น์–ด์— ์˜ํ•ด AGE=13 THEN NEW=โ€˜์ƒˆ๋กœ์šด ์นผ๋Ÿผ 2โ€™๊ฐ€ ์ž…๋ ฅ๋์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ELSE ๋ช…๋ น์–ด์—๋Š” ์กฐ๊ฑด๋ฌธ์ด ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด์™ธ์˜ ๊ฐ’๋“ค์— ๋Œ€ํ•ด์„œ NEW=โ€˜์ƒˆ์นผ๋Ÿผโ€™๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ช…๋ น์–ด๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” AGE๊ฐ€ 12,13์ด ์•„๋‹Œ 11,14,15,16์ธ ํ–‰์— NEW=โ€˜์ƒˆ ์นผ๋Ÿผโ€™๋ผ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” ์œ„์˜ ํ‘œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ELSE ๋ช…๋ น์–ด๋Š” IF ๋ช…๋ น์–ด๋ฅผ ๋งˆ์นœ ๋‹ค์Œ์— ์„ธํŠธ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. IF ๋ช…๋ น์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜, IF ๋ช…๋ น์–ด ์ด์™ธ์˜ ๊ฐ’์— ๋Œ€ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. tip IF ๋ฌธ ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•œ ํ›„ ELSE ๋ฌธ์„ ํ†ตํ•ด ์ถ”๊ฐ€ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. IF~THEN~๋ช…๋ น์–ด๋ฅผ ๋งˆ์นœ ๋‹ค์Œ, ELSE ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด IF ๋ช…๋ น์–ด ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ELSE ๋ช…๋ น์–ด ๋‹ค์Œ ์•„๋ฌด๋Ÿฐ ์กฐ๊ฑด์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์€ ์ฑ„ ๊ฒฐ๊ด๊ฐ’๋งŒ์„ ์ž…๋ ฅํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์œ„์˜ IF ๋ช…๋ น์–ด๊ฐ€ ์ž…๋ ฅ๋œ ๊ฐ’ ์ด์™ธ์˜ ๋‚˜๋จธ์ง€์— ๋Œ€ํ•ด์„œ ELSE ๋ช…๋ น์–ด์— ์ ํžŒ ๋‚ด์šฉ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. 6. ํ…Œ์ด๋ธ” ๊ฒฐํ•ฉ ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ํ…Œ์ด๋ธ”์˜ ์—ฐ๊ฒฐ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ”๋ผ๋ฆฌ ์—ฐ๊ฒฐํ•  ์ผ์€ ์ƒ๋‹นํžˆ ์ž์ฃผ ์ผ์–ด๋‚˜๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฑด ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋กœ ์ด์–ด์„œ ๋ถ™์ด๋Š” ๋ฐฉ์‹๊ณผ ์˜†์œผ๋กœ ์ด์–ด์„œ ๋ถ™์ด๋Š” ๋ฐฉ์‹์ด ์žˆ์ฃ . ์˜ˆ๋ฅผ ๋“ค์–ด ํ…Œ์ด๋ธ” A์˜ ์•„๋ž˜์ชฝ์— ํ…Œ์ด๋ธ” B๋ฅผ ์ž‡๋Š”๋‹ค๋“ ์ง€(ํ–‰์„ ์ถ”๊ฐ€), ํ…Œ์ด๋ธ” A์˜ ์˜†์— ํ…Œ์ด๋ธ” C๋ฅผ ์ด์–ด์•ผ ํ•˜๋Š” ์ผ(์—ด์„ ์ถ”๊ฐ€)์ด ์ƒ๊น๋‹ˆ๋‹ค. ์ ์ ˆํ•œ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด์„œ ํ…Œ์ด๋ธ”์„ ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐํ•˜๋Š”์ง€ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 6-1. ํ…Œ์ด๋ธ” ์•„๋ž˜๋กœ ๋ถ™์ด๊ธฐ(SET) ํ…Œ์ด๋ธ”์„ ์•„๋ž˜์— ๋ถ™์ด๊ธฐ ์ „์—, ์šฐ์„  ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณผ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๊ฒฐํ•ฉํ•  ํ…Œ์ด๋ธ”์€ A์™€ B, ๋‘ ๊ฐœ์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ SASHELP์˜ ํ…Œ์ด๋ธ” CLASS์ด๊ณ  ์ด๋ฅผ ํ…Œ์ด๋ธ” A๋ผ๊ณ  ์นฉ์‹œ๋‹ค. ํ•˜๋‚˜๋Š” ์•„๋ž˜์— ๋งŒ๋“  ํ…Œ์ด๋ธ” โ€˜CLASS2โ€™๋กœ ํ•˜๊ณ  ์ด๋ฅผ ํ…Œ์ด๋ธ” B๋ผ๊ณ  ์—ฌ๊ธฐ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋Œ€์ƒ ํ…Œ์ด๋ธ” ์ƒ์„ฑ ํ…Œ์ด๋ธ” B ์ƒ์„ฑ DATA CLASS2; INPUT NAME $ SEX $ AGE HEIGHT WEIGHT; CARDS; ์ตœ์ฒ ๊ธฐ ๋‚จ 12 150 50 ์ฃผ์–‘ ์—ฌ 11 130 40 RUN; NAME SEX AGE HEIGHT WEIGHT ์ตœ์ฒ ๊ธฐ ๋‚จ 12 150 50 ์ฃผ์–‘ ์—ฌ 11 130 40 ๋ช…๋ น์–ด SET XXX YYY;: ํ…Œ์ด๋ธ” XXX์™€ YYY๋ฅผ ์—ฐ์†ํ•˜์—ฌ ๋ถˆ๋Ÿฌ์™€์„œ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์˜ˆ์ œ DATA TEST;/*ํ…Œ์ด๋ธ” TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.*/ SET SASHELP.CLASS CLASS2;/*ํ…Œ์ด๋ธ” CLASS์™€ CLASS๋ฅผ ์—ฐ์†์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.*/ RUN;/*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.*/ Name Sex Age Height Weight ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์ž๋„ท์—ฌ 15 62.5 112.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กด ๋‚จ 12 59 99.5 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ฃผ๋””์—ฌ 14 64.3 90 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ์ตœ์ฒ ๊ธฐ ๋‚จ 12 150 50 ์ฃผ์–‘ ์—ฌ 11 130 40 SET ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์‹ค์ƒ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์•„๋ž˜๋กœ ์ด์–ด ๋ถ™์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SET์€ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋งŒ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ช…๋ น์–ด๊ฐ€ ์•„๋‹ˆ๋ผ ๋ถˆ๋Ÿฌ์˜ฌ ํ…Œ์ด๋ธ”์„ ๋ชจ๋‘ ์ง€์ •ํ•˜๋Š” ๋ช…๋ น์–ด์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ ๋ณด๋“ฏ์ด ํ…Œ์ด๋ธ” SASHELP.CLASS ๋‹ค์Œ์œผ๋กœ ํ…Œ์ด๋ธ” CLASS2๊ฐ€ ์ด์–ด์ ธ ๋ถ™์–ด์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 6-1-A. ์นผ๋Ÿผ๋ช…์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ™์ผ ๋•Œ ๋ฐ˜๋“œ์‹œ ํ…Œ์ด๋ธ” A์™€ ํ…Œ์ด๋ธ” B์˜ ์นผ๋Ÿผ(์—ด)์ด ๊ฐ™์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” A์— ์žˆ๋Š” ์นผ๋Ÿผ์ด ํ…Œ์ด๋ธ” B์—๋Š” ์—†์„ ์ˆ˜๋„ ์žˆ๊ณ , ๊ทธ ๋ฐ˜๋Œ€์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํ…Œ์ด๋ธ”์„ ์•„๋ž˜๋กœ ๋ถ™์ผ ๋•Œ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ๋ˆ„๋ฝ ๋ฐ์ดํ„ฐ ๊ฑด์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” A์— ์—†๋Š” ์—ด์ด ํ…Œ์ด๋ธ” B์— ์žˆ๊ฑฐ๋‚˜, ํ…Œ์ด๋ธ” B์— ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ˆ„๋ฝ๋์„ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” A๋ฅผ SASHELP.CLASS๋กœ ํ•˜๊ณ , ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ” B๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” B ์ƒ์„ฑ DATA CLASS2; INPUT NAME $ AGE HEIGHT WEIGHT ADDR $; CARDS; ์ตœ์ฒ ๊ธฐ 12 150 50 ๋‚จ๋Œ€๋ฌธ ์ฃผ์–‘ 11 130 40 ์„œ์ดˆ RUN; NAME AGE HEIGHT WEIGHT ADDR ์ตœ์ฒ ๊ธฐ 12 150 50 ๋‚จ๋Œ€๋ฌธ ์ฃผ์–‘ 11 130 40 ์„œ์ดˆ ํ…Œ์ด๋ธ” A์™€ ํ…Œ์ด๋ธ” B๋Š” ๋น„์Šทํ•˜์ง€๋งŒ ์นผ๋Ÿผ์ด ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” A์˜ ์นผ๋Ÿผ์ด NAME, SEX, AGE, HEIGHT, WEIGHT์ด๊ณ  ํ…Œ์ด๋ธ” B์˜ ์นผ๋Ÿผ์€ NAME, AGE, HEIGHT, WEIGHT, ADDR์ž…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” A์— ์—†๋Š” ์นผ๋Ÿผ์€ ADDR์ด๊ณ  ๊ฐ ์นผ๋Ÿผ์˜ ์œ„์น˜๋„ ํ•œ ์นธ์”ฉ ๋ณ€๊ฒฝ๋์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ 2๊ฐœ์˜ ํ…Œ์ด๋ธ”์ด ์„œ๋กœ ๋‹ค๋ฅธ ํ˜•ํƒœ์ผ ๋•Œ ํ…Œ์ด๋ธ” ์ด์–ด๋ถ™์ด๊ธฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ง„ํ–‰๋˜๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด DATA TEST; SET SASHELP.CLASS CLASS2 RUN; Name Sex Age Height Weight ADDR ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5. ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84. ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98. ์บ๋Ÿด์—ฌ 14 62.8 102.5. ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5. ์ œ์ž„์Šค ๋‚จ 12 57.3 83. ์ œ์ธ์—ฌ 12 59.8 84.5. ์ž๋„ท์—ฌ 15 62.5 112.5. ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84. ์กด ๋‚จ 12 59 99.5. ์กฐ์ด์Šค์—ฌ 11 51.3 50.5. ์ฃผ๋””์—ฌ 14 64.3 90. ๋ฃจ์ด์Šค์—ฌ 12 56.3 77. ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112. ํ•„๋ฆฝ ๋‚จ 16 72 150. ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128. ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133. ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85. ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112. ์ตœ์ฒ ๊ธฐ. 12 150 50 ๋‚จ๋Œ€๋ฌธ ์ฃผ์–‘. 11 130 40 ์„œ์ดˆ ๋‘ ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ์ด ๋‹ค๋ฅธ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ ๊ฐ ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ์นผ๋Ÿผ๋“ค์„ ๋ชจ๋‘ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ณ ์„œ ์นผ๋Ÿผ๋ช…์ด ๋™์ผํ•œ ํ…Œ์ด๋ธ”์€ ์ •์ƒ์ ์œผ๋กœ ํ…Œ์ด๋ธ” A์™€ B๋ฅผ ์ด์–ด์„œ ๋ถ™์ž…๋‹ˆ๋‹ค. ๋™์ผํ•˜์ง€ ์•Š์€ ๋‚˜๋จธ์ง€ ์นผ๋Ÿผ์€ ๊ฐ๊ฐ ํ…Œ์ด๋ธ” A์™€ ํ…Œ์ด๋ธ” B์˜ ๋ฐ์ดํ„ฐ๋งŒ์„ ๋„ฃ์–ด์„œ ์ „์ฒด ํ…Œ์ด๋ธ”์„ ์™„์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ” A์— ์กด์žฌํ•˜์ง€ ์•Š์ง€๋งŒ ํ…Œ์ด๋ธ” B์—๋Š” ์žˆ๋Š” ADDR ์นผ๋Ÿผ์ด ์ƒ๊ฒผ๊ณ , ๊ฑฐ๊ธฐ์—” 19๊ฐœ ํ–‰์ด ๋นˆ์นธ์œผ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ด๋ธ” B์—๋Š” ์žˆ์ง€๋งŒ ํ…Œ์ด๋ธ” A์—๋Š” ์—†๋Š” SEX ์นผ๋Ÿผ์ด ์žˆ๊ณ  2๊ฐœ ํ–‰์ด ๋นˆ์นธ์œผ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. tip SET ๋ช…๋ น์–ด๋Š” ๋‘ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์นผ๋Ÿผ์„ ํ•ฉ์นฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์†ํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ๋ชจ๋‘ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋‹ค๋ฉด, ์—†๋Š” ๋Œ€๋กœ ๋นˆ์นธ์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. 6-1-B. ์นผ๋Ÿผ์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ ๋ชจ๋“  ํ…Œ์ด๋ธ”์ด ๋™์ผํ•œ ๊ธธ์ด๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ๋Š” ๊ฑด ์•„๋‹™๋‹ˆ๋‹ค. ์นผ๋Ÿผ ์ด๋ฆ„์ด ๋˜‘๊ฐ™์•„๋„ ์นผ๋Ÿผ์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์นผ๋Ÿผ๋ช…์€ ๋˜‘๊ฐ™์ง€๋งŒ, ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ ์–ด๋–ค ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š”์ง€๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์šฐ์„  ํ…Œ์ด๋ธ” A๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ SASHELP์˜ ํ…Œ์ด๋ธ” CLASS๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. CLASS ํ…Œ์ด๋ธ”์˜ ์†์„ฑ์„ ์•ž์—์„œ ๋ฐฐ์šด PROC CONTENTS ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. PROC CONTENTS DATA=SASHELP.CLASS; RUN; # ๋ณ€์ˆ˜ ์œ ํ˜• ๊ธธ์ด ๋ ˆ์ด๋ธ” 3 Age ์ˆซ์ž 8 ๋‚˜์ด 4 Height ์ˆซ์ž 8 ํ‚ค(๋‹จ์œ„: ์ธ์น˜) 1 Name ๋ฌธ์ž 12 ์ด๋ฆ„ 2 Sex ๋ฌธ์ž 4 ์„ฑ๋ณ„ 5 Weight ์ˆซ์ž 8 ๋ชธ๋ฌด๊ฒŒ(๋‹จ์œ„: ํŒŒ์šด๋“œ) PROC CONTENTS ๋ช…๋ น์–ด๋ฅผ ์‹œํ–‰ํ•˜๋ฉด ์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  AGE๋Š” ์ˆซ์žํ˜• ๋ณ€์ˆ˜์ด๋ฉด์„œ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 8๊นŒ์ง€ ์ง€์ •๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. HEIGHT๋Š” ์ˆซ์žํ˜• ๋ณ€์ˆ˜์ด๋ฉด์„œ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 8, NAME์€ ๋ฌธ์žํ˜• ๋ณ€์ˆ˜์ด๋ฉด์„œ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 12๊นŒ์ง€ ์ง€์ •์ด ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ธธ์ด 1์€ ์•ŒํŒŒ๋ฒณ ํ•˜๋‚˜๋กœ ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ณ€์ˆ˜ NAME์— ํ•œ๊ธ€์€ 6๊ธ€์ž๊นŒ์ง€ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‘ ํ…Œ์ด๋ธ”์˜ ๋™์ผ ์ด๋ฆ„ ์นผ๋Ÿผ ๊ฐ„ ๋ฐ์ดํ„ฐ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ ๋‘ ํ…Œ์ด๋ธ”์„ ์•„๋ž˜๋กœ ์ด์–ด ๋ถ™์ผ ๋•Œ ๋™์ผํ•œ ์ด๋ฆ„์˜ ์นผ๋Ÿผ ๊ฐ„ ๊ธธ์ด์˜ ์ฐจ์ด๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ…Œ์ด๋ธ” A(SASHELP.CLASS)์™€ ํ…Œ์ด๋ธ” B(CLASS2)์— ์žˆ๋Š” ์นผ๋Ÿผ NAME์„ ๊ธฐ์ค€์œผ๋กœ ๊ณต๋ถ€๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” B๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ…Œ์ด๋ธ”๋กœ ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” B ์ƒ์„ฑ DATA CLASS2; INPUT NAME: $14. SEX $ AGE HEIGHT WEIGHT; CARDS; ๋“œ๋ ˆ์ด ๋จผ๋“œ ๊ทธ๋ฆฐ 1 17 207 90 ๋“œ๋งˆํŠธ๋ผ์ปค์ฆŒ์Šค 2 21 210 100 RUN; ํ…Œ์ด๋ธ” CLASS2๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. CLASS2์˜ ๋ณ€์ˆ˜ ์†์„ฑ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์นผ๋Ÿผ NAME์„ ๋ณด์‹ญ์‹œ์˜ค. ํ…Œ์ด๋ธ” A(SASHELP.CLASS)์™€ ํ…Œ์ด๋ธ” B(CLASS2)์—์„œ์˜ ์นผ๋Ÿผ NAME์€ ์นผ๋Ÿผ๋ช…์ด ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ SAS๋Š” SET ๋ช…๋ น์–ด์—์„œ ํ…Œ์ด๋ธ” A์™€ B์˜ ์นผ๋Ÿผ NAME์„ ์ด์–ด์„œ ๋ถ™์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. NAME SEX AGE HEIGHT WEIGHT ๋“œ๋ ˆ์ด ๋จผ๋“œ ๊ทธ๋ฆฐ 1 17 207 90 ๋“œ๋งˆํŠธ๋ผ์ปค์ฆŒ์Šค 2 21 210 100 tip ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ๊ฒฝ์šฐ ํŠน๋ณ„ํ•œ ๋ช…๋ น์–ด๊ฐ€ ์—†๋Š” ์ด์ƒ ๊ธธ์ด๋Š” 8๋กœ ์„ค์ •์ด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ CLASS2์˜ ์†์„ฑ์„ PROC CONTENTS ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. PROC CONTENTS DATA=CLASS2; RUN; # ๋ณ€์ˆ˜ ์œ ํ˜• ๊ธธ์ด # ๋ณ€์ˆ˜ ์œ ํ˜• ๊ธธ์ด 3 AGE ์ˆซ์ž 8 4 HEIGHT ์ˆซ์ž 8 1 NAME ๋ฌธ์ž 14 2 SEX ๋ฌธ์ž 8 5 WEIGHT ์ˆซ์ž 8 ์ด์ œ ๊ทธ๋Ÿผ ๋‘ ํ…Œ์ด๋ธ”์„ SET ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ด์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด SET XXX YYY;: ํ…Œ์ด๋ธ” XXX์™€ YYY๋ฅผ ์—ฐ์†ํ•˜์—ฌ ๋ถˆ๋Ÿฌ์™€์„œ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์˜ˆ์ œ DATA TEST; SET SASHELP.CLASS CLASS2 RUN; Name Sex Age Height Weight ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์ž๋„ท์—ฌ 15 62.5 112.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กด ๋‚จ 12 59 99.5 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ฃผ๋””์—ฌ 14 64.3 90 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ๋“œ๋ ˆ์ด ๋จผ๋“œ ๊ทธ 1 17 207 90 ๋“œ๋งˆํŠธ๋ผ์ปค์ฆŒ 2 21 210 100 NAME ์นผ๋Ÿผ์˜ ๊ฐ€์žฅ ์•„๋ž˜์ชฝ์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” B์—์„œ๋Š” ์›๋ž˜ โ€˜๋“œ๋ ˆ์ด ๋จผ๋“œ ๊ทธ๋ฆฐโ€™๊ณผ โ€˜๋“œ๋งˆํŠธ๋ผ์ปค์ฆŒ์Šคโ€™์ด ์ž…๋ ฅ๋ผ ์žˆ์—ˆ์ง€๋งŒ ์ตœ์ข… ํ…Œ์ด๋ธ”์—๋Š” ๋งˆ์ง€๋ง‰ ํ•œ ๊ธ€์ž๊ฐ€ ์ž˜๋ ค์„œ ์ž…๋ ฅ๋์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ํ…Œ์ด๋ธ” A์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ์— ๋‚˜์˜จ ํ…Œ์ด๋ธ” A(SASHELP.CLASS)์—์„œ ์นผ๋Ÿผ NAME์˜ ๊ธธ์ด๊ฐ€ 12๋กœ ์ง€์ •์ด ๋ผ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . SET ๋ช…๋ น์–ด๋กœ ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ์ด์–ด๋ถ™์ผ ๋•Œ ๊ธฐ์ค€์ด ๋˜๋Š” ๊ฒƒ์€ ์ฒซ ๋ฒˆ์งธ๋กœ ์ง€์ •๋œ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ์•ž์—์„œ ์„ค๋ช…ํ–ˆ๋“ฏ์ด SAS๋Š” ๋ช…๋ น์–ด๋ฅผ ์œ„์—์„œ๋ถ€ํ„ฐ ์ฝ๊ณ  ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ฝ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ™์€ ๊ธฐ์ค€์— ๋”ฐ๋ผ SET ๋ช…๋ น์–ด์—์„œ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ํ…Œ์ด๋ธ”์„ ๋จผ์ € ์ฝ์–ด ๋“ค์ด๊ฒŒ ๋˜์ฃ . ์ด์— ๋”ฐ๋ผ ํ†ตํ•ฉ๋œ ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ NAME์€ ๊ธธ์ด๊ฐ€ 12์ธ ์ƒํƒœ๋กœ ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ์ƒํ™ฉ์—์„œ ๋‘ ๋ฒˆ์งธ๋กœ ๋‚˜์˜ค๋Š” ํ…Œ์ด๋ธ” B(CLASS2)์˜ ์นผ๋Ÿผ NAME์˜ ๊ธธ์ด๊ฐ€ 14๋ผ๋ฉด, ํ…Œ์ด๋ธ” B์˜ ๋’ค์ชฝ ๊ธธ์ด๊ฐ€ 2๋งŒํผ ์ž˜๋ฆฌ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์ฃ . ๊ทธ๋ ‡๊ธฐ์— ์นผ๋Ÿผ๋“ค์˜ ํ–‰์„ ์ด์–ด์ค„ ๋•Œ๋Š” ๊ธธ์ด๋ฅผ ์œ ์‹ฌํžˆ ์‚ดํ”ผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๊ฒฐ์ฑ… 1) ๊ธธ์ด๊ฐ€ ๊ธด ํ…Œ์ด๋ธ”์„ SET ๋ช…๋ น์–ด์—์„œ ์•ž์ชฝ์— ๋‚˜์˜ค๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์นผ๋Ÿผ์˜ ๊ธธ์ด๊ฐ€ ๊ธด ํ…Œ์ด๋ธ” B๋ฅผ A๋ณด๋‹ค ์•ž์— ๋‚˜์˜ค๋„๋ก ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๊ฒฝ์šฐ ๊ธธ์ด๊ฐ€ ๊ธด ํ…Œ์ด๋ธ” B๊ฐ€ ๊ธฐ์ค€์ด ๋˜๋ฏ€๋กœ ์นผ๋Ÿผ NAME์—์„œ ์ž˜๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. DATA TEST; SET CLASS2 SASHELP.CLASS RUN; 2) ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ๋•Œ ํ…Œ์ด๋ธ” A์˜ ๊ธธ์ด๋ฅผ ํ…Œ์ด๋ธ” B์— ๋งž์ถฐ์„œ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” A์˜ ๊ธธ์ด๋ฅผ ํ…Œ์ด๋ธ” B์— ๋งž์ถฐ์„œ ํ™•์žฅํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ธธ์ด๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜๋ฆฌ์ง€ ์•Š์„ ๋งŒํผ ํ™•์žฅํ•ด ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.(ํ•˜์ง€๋งŒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ SASHELP ๋‚ด๋ถ€์˜ ๋ฐ์ดํ„ฐ๋Š” ์ˆ˜์ •๋˜์ง€ ์•Š์œผ๋‹ˆ ์ด ๋ฐฉ๋ฒ•์„ ์“ฐ์ง€๋Š” ๋ชปํ•ฉ๋‹ˆ๋‹ค.) 3) ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ ํ…Œ์ด๋ธ”์˜ ๊ธธ์ด๋ฅผ ๋ณ€๊ฒฝํ•ด ์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์ดํ›„ ๊ณผ์ •์—์„œ ํ•™์Šตํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 6-1-C. ์นผ๋Ÿผ์˜ ์†์„ฑ์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ(์ „์ž์ฑ… ๋‚ด์šฉ) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์˜ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋งํฌ) SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ์ „์ž์ฑ… ๊ตฌ๋งค 6-2. ํ…Œ์ด๋ธ” ์˜†์œผ๋กœ ๋ถ™์ด๊ธฐ(MERGE) ํ…Œ์ด๋ธ”์„ ๋‹ค๋ฃจ๋ฉด์„œ ํ…Œ์ด๋ธ”๋ผ๋ฆฌ ์กฐํ•ฉ์„ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์ƒ๋‹นํžˆ ๋งŽ์€ ํŽธ์ž…๋‹ˆ๋‹ค. ํ˜„๋Œ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ๋Š” ์ฃผ๋กœ ํ…Œ์ด๋ธ”๊ณผ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ฒฐํ•ฉ์œผ๋กœ ์ด๋ค„์ง„๋‹ค๊ณ  ๋งํ•  ๋งŒํผ ์ƒ์ดํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ…Œ์ด๋ธ”์„ ์ด์–ด ๋ถ™์ด๋Š” ์ž‘์—…์€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํ…Œ์ด๋ธ”์„ ์˜†์œผ๋กœ ์ด์–ด๋ถ™์ด๋Š” ์ž‘์—…์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ์ค€์ด ๋  ํ…Œ์ด๋ธ” A๋Š” SASHELP.CLASS๋กœ ํ•˜๊ณ , ์˜†์œผ๋กœ ๋ถ™์ผ ๋Œ€์ƒ ํ…Œ์ด๋ธ” B๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋Œ€์ƒ ํ…Œ์ด๋ธ” ์ƒ์„ฑ ํ…Œ์ด๋ธ” B ์ƒ์„ฑ DATA CLASS3; INPUT LAST_NAME $ ADDR $ LINE; CARDS; ์กด ์„œ์šธ 5 ๋งˆ์ปค์Šค ๋ถ€์‚ฐ 6 RUN; LAST_NAME ADDR LINE ์กด ์„œ์šธ 5 ์กฐ๋˜ ๋ถ€์‚ฐ 6 ์œ„์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ด๋ธ” B๋ผ๊ณ  ์ง€์นญํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” A(SASHELP.CLASS)์™€ ํ…Œ์ด๋ธ” B(CLASS3)์„ ์˜†์œผ๋กœ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด MERGE XXX YYY;: XXX์™€ YYY๋ฅผ ์˜†์œผ๋กœ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ DATA TEST; MERGE SASHELP.CLASS CLASS3 RUN; Name Sex Age Height Weight LAST_NAME ADDR LINE ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์กด ์„œ์šธ 5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ์กฐ๋˜ ๋ถ€์‚ฐ 6 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98. . . ์บ๋Ÿด์—ฌ 14 62.8 102.5. . . ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5. . . ์ œ์ž„์Šค ๋‚จ 12 57.3 83. . . ์ œ์ธ์—ฌ 12 59.8 84.5. . . ์ž๋„ท์—ฌ 15 62.5 112.5. . . ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84. . . ์กด ๋‚จ 12 59 99.5. . . ์กฐ์ด์Šค์—ฌ 11 51.3 50.5. . . ์ฃผ๋””์—ฌ 14 64.3 90. . . ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 . . . ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112. . . ํ•„๋ฆฝ ๋‚จ 16 72 150. . . ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128. . . ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133. . . ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85. . . ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112. . . ์˜ˆ์ œ์˜ ๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์šฐ์„  ํ…Œ์ด๋ธ” A์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ ๋‚˜์˜ค๊ณ  ๊ทธ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ…Œ์ด๋ธ” B์˜ ๊ฒฐ๊ด๊ฐ’์ด ๋ถ™๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ๋ช…๋ น์–ด๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ํ…Œ์ด๋ธ” B์˜ ๊ฒฐ๊ด๊ฐ’์€ ์œ„์—์„œ๋ถ€ํ„ฐ ๋ณด์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 6-2-A. ํŠน์ • ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ(์ „์ž์ฑ… ๋‚ด์šฉ) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์˜ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋งํฌ) SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ์ „์ž์ฑ… ๊ตฌ๋งค 7. DO ๋ช…๋ น์–ด DO ๋ช…๋ น์–ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ช…๋ น์–ด๋ฅผ ํ•œ ๋ฒˆ์— ์‹œํ–‰๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 7-1. ๊ธฐ๋ณธ DO ๋ช…๋ น์–ด ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ํ˜•ํƒœ์˜ DO ๋ช…๋ น์–ด์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด IF AGE=12 THEN DO; NEW=โ€˜์—ด๋‘˜โ€™; NEW2=โ€˜์—ด๋‘ ์‚ดโ€™; END;: ๋ณ€์ˆ˜ AGE๊ฐ€ 12์ผ ๊ฒฝ์šฐ DO ์ดํ•˜์— ์žˆ๋Š” ๋ช…๋ น์–ด๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋ช…๋ น์–ด๋Š” ๋ณ€์ˆ˜ NEW๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ์ดํ„ฐ ๊ฐ’์œผ๋กœ โ€˜์—ด๋‘˜โ€™์„ ์ž…๋ ฅํ•˜๊ณ  ๋ณ€์ˆ˜ NEW2๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ์ดํ„ฐ ๊ฐ’์œผ๋กœ โ€˜์—ด๋‘ ์‚ดโ€™์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. END๋ฅผ ์”€์œผ๋กœ์จ DO ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ DATA TEST; SET SASHELP.CLASS; IF AGE=12 THEN DO; NEW='์—ด๋‘˜'; NEW2='์—ด๋‘ ์‚ด'; END; RUN; Name Sex Age Height Weight NEW NEW2 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5. . ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84. . ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98. . ์บ๋Ÿด์—ฌ 14 62.8 102.5. . ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5. . ์ œ์ž„์Šค ๋‚จ 12 57.3 83์—ด๋‘˜ ์—ด๋‘ ์‚ด ์ œ์ธ์—ฌ 12 59.8 84.5์—ด๋‘˜ ์—ด๋‘ ์‚ด ์ž๋„ท์—ฌ 15 62.5 112.5. . ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84. . ์กด ๋‚จ 12 59 99.5์—ด๋‘˜ ์—ด๋‘ ์‚ด ์กฐ์ด์Šค์—ฌ 11 51.3 50.5. . ์ฃผ๋””์—ฌ 14 64.3 90. . ๋ฃจ์ด์Šค์—ฌ 12 56.3 77์—ด๋‘˜ ์—ด๋‘ ์‚ด ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112. . ํ•„๋ฆฝ ๋‚จ 16 72 150. . ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128์—ด๋‘˜ ์—ด๋‘ ์‚ด ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133. . ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85. . ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112. . ์œ„์˜ ๊ฒฐ๊ณผ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋“ฏ์ด DO ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ•œ ๋ฒˆ์— ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ์‹œํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ IF ๋ช…๋ น์–ด์˜ ๊ฒฐ๊ณผ๋กœ ์นผ๋Ÿผ NEW์™€ NEW2๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๊ณ  ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ IF ์กฐ๊ฑด์— ๋งž๋Š” ํ–‰์—๋งŒ ์ž…๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ DO ๋ช…๋ น์–ด๋ฅผ ์“ฐ์ง€ ์•Š์„ ๊ฒฝ์šฐ IF THEN ์ดํ›„ ํ•˜๋‚˜์˜ ๋ช…๋ น์–ด๋งŒ IF ๋ช…๋ น์–ด๋ฅผ ๋ฐ›์•„์„œ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ๋Š” DO ๋ช…๋ น์–ด๋ฅผ ์“ฐ์ง€ ์•Š์€ ์‚ฌ๋ก€๋ฅผ ํ†ตํ•ด ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ 2(DO ๋ช…๋ น์–ด๋ฅผ ์“ฐ์ง€ ์•Š์€ ๊ฒฝ์šฐ) DATA TEST; SET SASHELP.CLASS; IF AGE=12 THEN NEW='์—ด๋‘˜'; NEW2='์—ด๋‘ ์‚ด'; RUN; Name Sex Age Height Weight NEW NEW2 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5. ์—ด๋‘ ์‚ด ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84. ์—ด๋‘ ์‚ด ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98. ์—ด๋‘ ์‚ด ์บ๋Ÿด์—ฌ 14 62.8 102.5. ์—ด๋‘ ์‚ด ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5. ์—ด๋‘ ์‚ด ์ œ์ž„์Šค ๋‚จ 12 57.3 83์—ด๋‘˜ ์—ด๋‘ ์‚ด ์ œ์ธ์—ฌ 12 59.8 84.5์—ด๋‘˜ ์—ด๋‘ ์‚ด ์ž๋„ท์—ฌ 15 62.5 112.5. ์—ด๋‘ ์‚ด ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84. ์—ด๋‘ ์‚ด ์กด ๋‚จ 12 59 99.5์—ด๋‘˜ ์—ด๋‘ ์‚ด ์กฐ์ด์Šค์—ฌ 11 51.3 50.5. ์—ด๋‘ ์‚ด ์ฃผ๋””์—ฌ 14 64.3 90. ์—ด๋‘ ์‚ด ๋ฃจ์ด์Šค์—ฌ 12 56.3 77์—ด๋‘˜ ์—ด๋‘ ์‚ด ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112. ์—ด๋‘ ์‚ด ํ•„๋ฆฝ ๋‚จ 16 72 150. ์—ด๋‘ ์‚ด ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128์—ด๋‘˜ ์—ด๋‘ ์‚ด ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133. ์—ด๋‘ ์‚ด ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85. ์—ด๋‘ ์‚ด ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112. ์—ด๋‘ ์‚ด DO ๋ช…๋ น์–ด๋ฅผ ์“ฐ์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ IF THEN ๋ช…๋ น์–ด๋ฅผ ๋ฐ›๋Š” ๋ฌธ์žฅ์€ ๋‹จ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์นผ๋Ÿผ NEW๋งŒ IF ๋ช…๋ น์–ด๋ฅผ ๋ฐ›์•„์„œ ์‹œํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. SAS๋Š” ์นผ๋Ÿผ NEW2๋Š” IF ๋ช…๋ น์–ด์™€๋Š” ๋ณ„๊ฐœ๋กœ ์ธ์‹ํ•˜์—ฌ ๋”ฐ๋กœ ํ•˜๋‚˜์˜ ์นผ๋Ÿผ์„ ๋งŒ๋“ค๊ณ  ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ โ€˜์—ด๋‘ ์‚ดโ€™๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์›ํ•˜๋˜ ์‚ฌ๋ก€์™€๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅธ ๊ฒฐ๊ด๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ์‚ฌ๋ก€์ฒ˜๋Ÿผ DO ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด IF ๋ช…๋ น์–ด ๋“ฑ์—์„œ ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 7-2. ๋ฐ˜๋ณต DO ๋ช…๋ น์–ด (์ „์ž์ฑ… ๋‚ด์šฉ) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์˜ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋งํฌ) SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ์ „์ž์ฑ… ๊ตฌ๋งค 7-3. DO UNTIL / WHILE ๋ช…๋ น์–ด (์ „์ž์ฑ… ๋‚ด์šฉ) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์˜ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋งํฌ) SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ์ „์ž์ฑ… ๊ตฌ๋งค 8. SAS๋กœ ํ•˜๋Š” SQL ๋ช…๋ น์–ด SQL์˜ ์—ญ์‚ฌ ์ด์ œ๋ถ€ํ„ฐ๋Š” PROC SQL์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SQL์€ ์‚ฌ์‹ค SAS์™€๋Š” ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. SAS์™€๋Š” ๋‹ค๋ฅด๊ฒŒ SQL์€ ๋ฏธ๊ตญ์˜ IBM ์‚ฌ์—์„œ 70๋…„๋Œ€์— ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. SQL์€ ์•ฝ์ž์ธ๋ฐ์š”, ํ’€๋„ค์ž„์€ โ€˜Structured English Query Languageโ€™์ž…๋‹ˆ๋‹ค. โ€˜์˜์–ด ์ฒด๊ณ„์˜ ์งˆ์˜ ์–ธ์–ดโ€™ ์ •๋„๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด๋กœ ์ปดํ“จํ„ฐ์— ์งˆ์˜(์งˆ๋ฌธ)๋ฅผ ๋˜์ง„๋‹ค๋Š” ์˜๋ฏธ์ธ๋ฐ์š”, ์ด๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ปดํ“จํ„ฐ๋กœ โ€˜์ด๋Ÿฌ์ด๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์•„์ฃผ์„ธ์š”โ€™๋ผ๊ณ  ์งˆ์˜๋ฅผ ํ•˜๋ฉด ์ปดํ“จํ„ฐ๊ฐ€ ์ด์— ์•Œ๋งž์€ ๋‹ต๋ณ€์„ ํ•œ๋‹ค๋Š” ์‹์œผ๋กœ ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. IBM ์‚ฌ์—์„œ SQL์„ ๊ฐœ๋ฐœํ•œ ์ด์œ ๋Š” ๋‹น์‹œ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๋ฐœ์ „ ์ดˆ๊ธฐ ๋‹จ๊ณ„์˜€๋Š”๋ฐ, ์ด๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์–ธ์–ด ์ฒด๊ณ„๊ฐ€ ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ƒˆ๋กœ์šด ์ปดํ“จํ„ฐ ์–ธ์–ด๋ฅผ ๊ฐœ๋ฐœํ–ˆ๋Š”๋ฐ์š”, ๊ทธ๊ฒƒ์ด ๋ฐ”๋กœ SQL์ž…๋‹ˆ๋‹ค. SQL์˜ ์œ ์šฉ์„ฑ SQL์€ ๋Œ€๋‹จํžˆ ์œ ์šฉํ•œ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋‹ค๋ฃจ๋Š” ์–ธ์–ด ์ค‘์— ๊ฐ€์žฅ ๋Œ€์ค‘์ ์ธ ๊ฒƒ์ด SQL์ž…๋‹ˆ๋‹ค. SQL์€ SAS์—์„œ๋งŒ ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์—…์—์„œ๋Š” SAS ํ”„๋กœ๊ทธ๋žจ ๋Œ€์‹ (๋˜๋Š” ๋ณ„๋„๋กœ) ORACLE์ด๋‚˜ MY-SQL ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ์„ ์“ฐ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ ORACLE์ด๋‚˜ MY-SQL์„ ๊ตฌ๋™ํ•˜๋Š” ์–ธ์–ด๊ฐ€ SQL์ž…๋‹ˆ๋‹ค. SQL์€ 1986๋…„ ANSI ํ‘œ์ค€ํ™”๊ฐ€ ์ด๋ค„์กŒ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ๋‹ฌ๋ผ๋„ SQL ์–ธ์–ด ์ฒด๊ณ„๋Š” ๋Œ€๋ถ€๋ถ„ ๋™์ผํ•ฉ๋‹ˆ๋‹ค(์ •ํ™•ํžˆ 100% ๋™์ผํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค). SQL ์–ธ์–ด๋ฅผ ์•Œ๋ฉด ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์„ ์šด์šฉํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๋งŒํผ, ์ œ๋Œ€๋กœ ์•Œ์•„๋‘๋ฉด ์ผ์„์ด์กฐ์˜ ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SQL์ด ๋Œ€์ค‘์ ์œผ๋กœ ํ™œ์šฉ๋˜์–ด์„œ SAS๋„ SQL์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ”„๋Ÿฌ์‹œ์ €(PROC XXX์™€ ๊ฐ™์€ ํ˜•ํƒœ)๋ฅผ ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ SAS๋ฅผ ํ™œ์šฉํ•˜๋‹ค ๋ณด๋ฉด SQL ํ”„๋Ÿฌ์‹œ์ €๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค ๋•Œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ๋ถ€ํ„ฐ ์œ ์šฉํ•œ SQL์˜ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SAS ๊ธฐ์กด ๋ฌธ๋ฒ•๊ณผ SQL ๋ฌธ๋ฒ•์˜ ์ฐจ์ด์  SQL์€ ๊ธฐ์กด์˜ SAS ๋ฌธ๋ฒ•๊ณผ๋Š” ์ฒด๊ณ„๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ SAS ๋ช…๋ น์–ด์™€ SQL ๋ช…๋ น์–ด๊ฐ€ ๋‹ค๋ฅธ ์ ์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 1) SAS์—์„œ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ฝ์„ ๋•Œ โ€˜์œ„์—์„œ๋ถ€ํ„ฐ ์ฝ๊ณ , ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ฝ๋Š”๋‹ค.โ€™ ๊ณ  ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ SQL์€ ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ฝ๋Š” ๊ฑด ๋™์ผํ•˜์ง€๋งŒ, ๋ฐ˜๋“œ์‹œ ์œ„์—์„œ๋ถ€ํ„ฐ ์ฝ๋Š” ๊ฑด ์•„๋‹™๋‹ˆ๋‹ค. ์•„๋ž˜์— ์žˆ๋Š” ๊ฒƒ์„ ๋จผ์ € ์ฝ์„ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. SQL์€ ๋ช…๋ น์–ด์— ๋”ฐ๋ผ ์ปดํ“จํ„ฐ๊ฐ€ ์ฝ์–ด๋“ค์ด๋Š” ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. 2) ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋ช…๋ น์–ด ๋ฌธ์žฅ์˜ ๋์— โ€˜;โ€™๋ฅผ ๋„ฃ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. SAS๋Š” ๊ฐ ๊ฐœ๋ณ„ ๋ช…๋ น์–ด์˜ ๋์— โ€˜;โ€™๋ฅผ ๋ถ™์—ฌ์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ SQL์€ ๋ชจ๋“  ๋ช…๋ น์–ด๊ฐ€ ๋๋‚ฌ์„ ๋•Œ๋งŒ โ€˜;โ€™๋ฅผ ๋ถ™์ด๋ฉด ๋ฉ๋‹ˆ๋‹ค. 8-01. SQL๋กœ ํ‘œ ๋งŒ๋“ค๊ธฐ SQL์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฌธ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด PROC SQL;: ํ”„๋Ÿฌ์‹œ์ € SQL์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. SELECT XXX: ์นผ๋Ÿผ XXX๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. FROM YYY: ํ…Œ์ด๋ธ” YYY๋กœ๋ถ€ํ„ฐ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ;: ๋ชจ๋“  ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. QUIT;: SQL์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ PROC SQL; SELECT NAME, AGE, HEIGHT FROM SASHELP.CLASS QUIT; ์ด๋ฆ„ ๋‚˜์ด ํ‚ค(๋‹จ์œ„: ์ธ์น˜) ์•Œํ”„๋ ˆ๋“œ 14 69 ์•จ๋ฆฌ์Šค 13 56.5 ๋ฐ”๋ฐ”๋ผ 13 65.3 ์บ๋Ÿด 14 62.8 ํ—จ๋ฆฌ 14 63.5 ์ œ์ž„์Šค 12 57.3 ์ œ์ธ 12 59.8 ์ž๋„ท 15 62.5 ์ œํ”„๋ฆฌ 13 62.5 ์กด 12 59 ์กฐ์ด์Šค 11 51.3 ์ฃผ๋”” 14 64.3 ๋ฃจ์ด์Šค 12 56.3 ๋ฉ”๋ฆฌ 15 66.5 ํ•„๋ฆฝ 16 72 ๋กœ๋ฒ„ํŠธ 12 64.8 ๋กœ๋‚ ๋“œ 15 67 ํ† ๋งˆ์Šค 11 57.5 ์œŒ๋ฆฌ์—„ 15 66.5 SQL ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์œ„์˜ ํ‘œ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  PROC SQL์„ ํ†ตํ•ด ํ”„๋Ÿฌ์‹œ์ € SQL์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ์–ด๋–ค ์นผ๋Ÿผ์„ ์‚ฌ์šฉํ• ์ง€ SELECT๋ฅผ ํ†ตํ•ด ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” SELECT๋ฅผ ํ†ตํ•ด ์นผ๋Ÿผ NAME, AGE, HEIGHT๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ FROM ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ SASHELP์˜ ํ…Œ์ด๋ธ” CLASS๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์ด์ „์˜ SAS ๋ฌธ๋ฒ•์—์„œ๋Š” ์šฐ์„  SET ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์–ด๋–ค ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒƒ์ธ์ง€๋ฅผ ์„ ํƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ SQL์€ ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” FROM ๋ช…๋ น์–ด๊ฐ€ ๋’ค์ชฝ์— ์žˆ์Šต๋‹ˆ๋‹ค. SQL์€ ์˜์–ด ๋ฌธ๋ฒ•๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ์˜์–ด ๋ฌธ์žฅ์—์„œ FROM ๊ฐ™์€ ์ „์น˜์‚ฌ๋กœ ์ด๋ค„์ง„ ๋‹จ์–ด๋Š” ๋ฌธ์žฅ์˜ ๋’ค์ชฝ์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. SQL๋„ ์œ ์‚ฌํ•˜๊ฒŒ FROM์ด ๋’ค์ชฝ์— ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ๋‹จ์–ด๋“ค๋„ ์‹ค์ œ ์‚ฌ์šฉํ•˜๋Š” ์˜์–ด์™€ ์œ ์‚ฌํ•œ ๋ถ€๋ถ„์ด ๋งŽ์Šต๋‹ˆ๋‹ค. FROM(~๋กœ๋ถ€ํ„ฐ)์ด๋ผ๋“ ์ง€ SELECT(์„ ํƒํ•˜๋‹ค) ๊ฐ™์€ ๊ฒฝ์šฐ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ ์‹ค์ œ ๋ฌธ์žฅ์œผ๋กœ ๋Œ€ํ™”๋ฅผ ํ•˜๋“ฏ์ด ๋ช…๋ น์–ด๊ฐ€ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค๋ฉด SQL ๋ฌธ์žฅ์„ ๋” ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜์‹ค ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. tip ๋งŒ์•ฝ ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ์ „์ฒด ์นผ๋Ÿผ์„ ๋ชจ๋‘ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ๋‹ค๋ฉด * ํ‘œ์‹œ๋ฅผ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 1.์ฒ˜๋Ÿผ SASHELP.CLASS์— ์žˆ๋Š” ๋ชจ๋“  ์นผ๋Ÿผ์„ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ๋‹ค๋ฉด โ€œSELECT * โ€๋กœ ํ‘œํ˜„์„ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. PROC SQL; SELECT * FROM SASHELP.CLASS QUIT; ์ด์™€ ๊ฐ™์ด ์‹คํ–‰์„ ํ•ด์ฃผ์‹œ๋ฉด SASHELP.CLASS์˜ ๋ชจ๋“  ์นผ๋Ÿผ๋“ค์ด ์„ ํƒ๋˜์–ด ํ‘œ์— ๋ณด์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 8-02. SQL๋กœ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ 8-1์˜ ๊ณผ์ •์€ ๋‹จ์ˆœํ•œ ํ‘œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฑฐ์˜€์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์ด๋Š” ๊ธฐ๋Šฅ๋งŒ ์žˆ๋Š” ํ‘œ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” 8-1์˜ ์˜ˆ์ œ ํ‘œ๋ฅผ ํ…Œ์ด๋ธ”๋กœ ๋งŒ๋“œ๋Š” ์ž‘์—…์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ ํ•œ ์ค„๋งŒ ์ถ”๊ฐ€๋ฅผ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด CREATE TABLE XXX AS: ํ…Œ์ด๋ธ” XXX๋ฅผ ๋งŒ๋“ค๊ณ  ์„ธ๋ถ€ ๋‚ด์šฉ์€ AS ์ดํ•˜๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ์ œ PROC SQL; CREATE TABLE TEST AS SELECT NAME, AGE, HEIGHT FROM SASHELP.CLASS QUIT; Name Age Height ์•Œํ”„๋ ˆ๋“œ 14 69 ์•จ๋ฆฌ์Šค 13 56.5 ๋ฐ”๋ฐ”๋ผ 13 65.3 ์บ๋Ÿด 14 62.8 ํ—จ๋ฆฌ 14 63.5 ์ œ์ž„์Šค 12 57.3 ์ œ์ธ 12 59.8 ์ž๋„ท 15 62.5 ์ œํ”„๋ฆฌ 13 62.5 ์กด 12 59 ์กฐ์ด์Šค 11 51.3 ์ฃผ๋”” 14 64.3 ๋ฃจ์ด์Šค 12 56.3 ๋ฉ”๋ฆฌ 15 66.5 ํ•„๋ฆฝ 16 72 ๋กœ๋ฒ„ํŠธ 12 64.8 ๋กœ๋‚ ๋“œ 15 67 ํ† ๋งˆ์Šค 11 57.5 ์œŒ๋ฆฌ์—„ 15 66.5 8-1์˜ ์˜ˆ์ œ์— ๋‚˜์™€ ์žˆ๋Š” ํ‘œ์™€ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ด ๊ฒฐ๊ณผํ‘œ๋Š” ํ…Œ์ด๋ธ”๋กœ ์ƒ์„ฑ์ด ๋์Šต๋‹ˆ๋‹ค. 8-1์˜ ์˜ˆ์ œ์™€ ์ง€๊ธˆ์˜ ์˜ˆ์ œ์˜ ์ฐจ์ด์ ์€ CREATE TABLE XXX AS ๋ฌธ์žฅ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์žฅ์ด ์žˆ์–ด์„œ ํ…Œ์ด๋ธ” XXX๊ฐ€ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  AS ์ดํ•˜์˜ ์นผ๋Ÿผ ๊ฐ’๋“ค์„ SELECT ํ•˜์—ฌ ๋ถˆ๋Ÿฌ์˜ค๊ณ  FROM์œผ๋กœ ๋Œ€์ƒ ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์ด ํ…Œ์ด๋ธ”์„ ๋งŒ๋“œ๋Š” SQL ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ SAS ๋ฌธ๋ฒ•์œผ๋กœ ์žฌํ˜„ํ•ด ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. tip ํ˜„ ์ฑ•ํ„ฐ์˜ ๋ช…๋ น์–ด๋ฅผ SAS ๋ฌธ๋ฒ•์œผ๋กœ ์žฌํ˜„ํ•ด ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. DATA TEST(KEEP=NAME AGE HEIGHT); SET SASHELP.CLASS; RUN; ์ง์ ‘ ์žฌํ˜„ํ•ด ๋ณด์‹œ๋ฉด ์œ„์˜ ์˜ˆ์ œ์™€ ๊ฐ™์€ ๊ฒฐ๊ด๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 8-03. SQL๋กœ ์‚ฌ์น™์—ฐ์‚ฐํ•˜๊ธฐ SQL์„ ํ™œ์šฉํ•ด์„œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ˆ˜์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์น™์—ฐ์‚ฐ์„ ํ™œ์šฉํ•˜์—ฌ ์ˆซ์ž๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๊ณ  ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์กฐํ•ฉ์„ ํ™œ์šฉํ•ด ์ƒˆ๋กœ์šด ์นผ๋Ÿผ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ง€๊ธˆ๋ถ€ํ„ฐ SQL์„ ๋‹ค์–‘ํ•˜๊ฒŒ ํ™œ์šฉํ•˜๋Š” ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) SQL์—์„œ ์‚ฌ์น™์—ฐ์‚ฐ ์‚ฌ์šฉํ•˜๊ธฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์น™์—ฐ์‚ฐ์˜ ํ™œ์šฉ๋ถ€ํ„ฐ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SQL์—์„œ๋„ ์‚ฌ์น™์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜์‹ ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์น™์—ฐ์‚ฐ์€ ์ˆซ์ž ์นผ๋Ÿผ๋“ค๋ผ๋ฆฌ๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž ์นผ๋Ÿผ์„ ์“ธ ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด SELECT XXX+YYY: ์นผ๋Ÿผ XXX์™€ ์นผ๋Ÿผ YYY๋ฅผ ๋”ํ•ฉ๋‹ˆ๋‹ค. (+,-,*,/๋ฅผ ์‚ฌ์šฉํ•œ ์‚ฌ์น™์—ฐ์‚ฐ ๊ฐ€๋Šฅ) ์˜ˆ์ œ 1 PROC SQL; CREATE TABLE TEST AS SELECT NAME, SEX, AGE, AGE+HEIGHT FROM SASHELP.CLASS QUIT; Name Sex Age Height _ TEMA001 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 83 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 69.5 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 78.3 ์บ๋Ÿด์—ฌ 14 62.8 76.8 ํ—จ๋ฆฌ ๋‚จ 14 63.5 77.5 ์ œ์ž„์Šค ๋‚จ 12 57.3 69.3 ์ œ์ธ์—ฌ 12 59.8 71.8 ์ž๋„ท์—ฌ 15 62.5 77.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 75.5 ์กด ๋‚จ 12 59 71 ์กฐ์ด์Šค์—ฌ 11 51.3 62.3 ์ฃผ๋””์—ฌ 14 64.3 78.3 ๋ฃจ์ด์Šค์—ฌ 12 56.3 68.3 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 81.5 ํ•„๋ฆฝ ๋‚จ 16 72 88 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 76.8 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 82 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 68.5 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 81.5 ๊ฒฐ๊ณผ๋ฅผ ๋ณด์‹œ๋ฉด ์นผ๋Ÿผ NAME, SEX, AGE, HEIGHT๊ฐ€ ์„ ํƒ๋˜์–ด ์ถœ๋ ฅ๋˜์—ˆ๊ณ  โ€˜_TEMA001โ€™๋ผ๋Š” ์นผ๋Ÿผ์ด ์ƒ๊ธด ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์นผ๋Ÿผโ€˜_TEMA001โ€™์€ ์นผ๋Ÿผ AGE์™€ ์นผ๋Ÿผ HEIGHT๋ฅผ ํ•ฉํ•œ ๊ฐ’์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. โ€˜AGE+HEIGHTโ€™ ๋ช…๋ น์–ด์— ์˜ํ•ด ๋‘ ์ˆซ์ž ์นผ๋Ÿผ์„ ํ•ฉํ•œ ๊ฐ’์ด ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์น™์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) SQL์—์„œ ์‚ฌ์น™์—ฐ์‚ฐ๊ณผ ํ‰๊ท , ๋ถ„์‚ฐ, ๊ฐ„๋‹จํ•œ ์ˆซ์ž ์„ธ๊ธฐ ๋ฐฉ๋ฒ• ๋“ฑ ํ™œ์šฉํ•˜๊ธฐ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์น™์—ฐ์‚ฐ์„ ๋„˜์–ด, SAS์—์„œ ์ œ๊ณตํ•˜๋Š” ์ˆ˜์‹ ํ™œ์šฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SAS์—์„œ๋Š” ๋ถ„์„์„ ์œ„ํ•ด ๋‹ค์–‘ํ•œ ์ˆ˜์‹ ํ™œ์šฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์•Œ์•„๋‘๋ฉด ์œ ์šฉํ•œ ๊ณ„์‚ฐ์‹์„ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด SELECT SUM(XXX), SUM(XXX, YYY), MEAN(XXX), MIDIAN(XXX), MIN(XXX), MAX(XXX), VAR(XXX), STD(XXX), COUNT(XXX), COUNT(XXX, YY) :SUM(XXX)๋Š” ์นผ๋Ÿผ XXX์˜ ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. SUM(XXX, YYY)๋Š” ์นผ๋Ÿผ XXX์™€ YYY์˜ ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. MEAN(XXX)๋Š” ์นผ๋Ÿผ XXX์˜ ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. MIDIAN(XXX)๋Š” ์นผ๋Ÿผ XXX์˜ ์ค‘์•™๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. MIN(XXX)๋Š” ์นผ๋Ÿผ XXX์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. MAX(XXX)๋Š” ์นผ๋Ÿผ XXX์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. VAR(XXX)๋Š” ์นผ๋Ÿผ XXX์˜ ๋ถ„์‚ฐ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. STD(XXX)๋Š” ์นผ๋Ÿผ XXX์˜ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 2 PROC SQL; CREATE TABLE TEST AS SELECT NAME, SEX, AGE, HEIGHT, SUM(AGE), SUM(HEIGHT, WEIGHT), MEAN(AGE), MEDIAN(AGE), MIN(AGE), MAX(AGE), VAR(AGE), STD(AGE) FROM SASHELP.CLASS QUIT; Name Sex Age Height _ TEMG001 _ TEMA008 _ TEMG002 _ TEMG003 _ TEMG004 _ TEMG005 _ TEMG006 _ TEMG007 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 253 181.5 13.31579 13 11 16 2.22807 1.492672 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 253 140.5 13.31579 13 11 16 2.22807 1.492672 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 253 163.3 13.31579 13 11 16 2.22807 1.492672 ์บ๋Ÿด์—ฌ 14 62.8 253 165.3 13.31579 13 11 16 2.22807 1.492672 ํ—จ๋ฆฌ ๋‚จ 14 63.5 253 166 13.31579 13 11 16 2.22807 1.492672 ์ œ์ž„์Šค ๋‚จ 12 57.3 253 140.3 13.31579 13 11 16 2.22807 1.492672 ์ œ์ธ์—ฌ 12 59.8 253 144.3 13.31579 13 11 16 2.22807 1.492672 ์ž๋„ท์—ฌ 15 62.5 253 175 13.31579 13 11 16 2.22807 1.492672 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 253 146.5 13.31579 13 11 16 2.22807 1.492672 ์กด ๋‚จ 12 59 253 158.5 13.31579 13 11 16 2.22807 1.492672 ์กฐ์ด์Šค์—ฌ 11 51.3 253 101.8 13.31579 13 11 16 2.22807 1.492672 ์ฃผ๋””์—ฌ 14 64.3 253 154.3 13.31579 13 11 16 2.22807 1.492672 ๋ฃจ์ด์Šค์—ฌ 12 56.3 253 133.3 13.31579 13 11 16 2.22807 1.492672 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 253 178.5 13.31579 13 11 16 2.22807 1.492672 ํ•„๋ฆฝ ๋‚จ 16 72 253 222 13.31579 13 11 16 2.22807 1.492672 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 253 192.8 13.31579 13 11 16 2.22807 1.492672 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 253 200 13.31579 13 11 16 2.22807 1.492672 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 253 142.5 13.31579 13 11 16 2.22807 1.492672 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 253 178.5 13.31579 13 11 16 2.22807 1.492672 SAS์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์น™์—ฐ์‚ฐ๊ณผ ํ†ต๊ณ„๋Ÿ‰์ด ๊ฒฐ๊ณผ๋กœ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. SELECT ์ดํ›„ ์ž…๋ ฅํ•œ ์นผ๋Ÿผ ์ˆœ์„œ๋Œ€๋กœ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์šฐ์„  SUM(AGE)์— ์˜ํ•ด์„œ ๋ชจ๋“  ํ–‰์˜ AGE ๊ฐ’์ด ํ•ฉํ•ด์กŒ์Šต๋‹ˆ๋‹ค. ์•Œํ”„๋ ˆ๋“œ, ์•จ๋ฆฌ์Šค... ์œŒ๋ฆฌ์—„๊นŒ์ง€ AGE์˜ ๋ชจ๋“  ๊ฐ’์„ ๋”ํ–ˆ์Šต๋‹ˆ๋‹ค. ์›๋ž˜ ํ…Œ์ด๋ธ” ๋ชจ์Šต์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ ์˜ค๋ฅธ์ชฝ์— TEMG001์นผ๋Ÿผ์ด ์ถ”๊ฐ€๋์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํ–‰์˜ ์นผ๋Ÿผ AGE๋ฅผ ๋”ํ–ˆ๊ธฐ์— ๋ชจ๋“  ์นผ๋Ÿผ์˜ ๊ฐ’์€ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ SUM(HEIGHT, WEIGHT)์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์นผ๋Ÿผ TEMA008์ด ์ƒ์„ฑ๋์Šต๋‹ˆ๋‹ค. ๊ฐ ํ–‰์—์„œ ์นผ๋Ÿผ HEIGHT์™€ ์นผ๋Ÿผ WEIGHT๋ฅผ ๋”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ๊ฐ์˜ ํ–‰์ด ๊ฐ’์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋‹ค๋งŒ ํŠน์ด์ ์ด ์šฐ๋ฆฌ๋Š” ๋ถ„๋ช… SELECT ๋ช…๋ น์–ด์—์„œ ์นผ๋Ÿผ WEIGHT๋ฅผ ์„ ํƒํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ SQL ๋ช…๋ น์–ด์˜ ํŠน์ง•์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ SELECT๋กœ ์นผ๋Ÿผ WEIGHT์„ ํ‘œํ˜„ํ•˜์ง€ ์•Š๋”๋ผ๋„, FROM ํ…Œ์ด๋ธ”์— ์นผ๋Ÿผ WEIGHT๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, SQL์€ ์ด๋ฅผ ์ธ์‹ํ•˜๊ณ  ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. SELECT ๋ช…๋ น์–ด๋Š” ์นผ๋Ÿผ์„ ํ‘œํ˜„ํ• ์ง€ ๋ง์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์—ญํ• ๋งŒ์„ ํ•˜๋Š” ์…ˆ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ๋‚˜์˜ค๋Š” ๋ช…๋ น์–ด๋“ค ๋ชจ๋‘ ๊ฐ ์นผ๋Ÿผ๋“ค์˜ ๋ชจ๋“  ํ–‰์„ ๊ณ„์‚ฐํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. ํ‰๊ท , ์ค‘์•™๊ฐ’, ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’, ๋ถ„์‚ฐ, ํ‘œ์ค€ํŽธ์ฐจ ๋“ฑ์˜ ๊ฐ’์„ ๊ฒฐ๊ณผ๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. 8-04. SQL๋กœ ์ƒˆ ์นผ๋Ÿผ ์ด๋ฆ„ ๋ถ€์—ฌํ•˜๊ธฐ(AS) ์•ž์˜ ์˜ˆ์ œ์—์„œ ์ƒˆ๋กœ์šด ์นผ๋Ÿผ์ด ์ƒ์„ฑ๋˜์—ˆ์ง€๋งŒ ์นผ๋Ÿผ์˜ ์ด๋ฆ„์ด โ€˜TEMA001โ€™ ๋“ฑ์œผ๋กœ ๋ถ€์—ฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ž„์‹œ ์ด๋ฆ„์œผ๋กœ ๋‚˜์ค‘์— ์ƒˆ ์นผ๋Ÿผ์„ ํ™œ์šฉํ•  ๋•Œ ๋ถˆํŽธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ AS๋ฅผ ํ™œ์šฉํ•ด ์นผ๋Ÿผ์— ์ƒˆ๋กœ์šด ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ผญ ์ƒˆ๋กœ์šด ์นผ๋Ÿผ์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ ๊ธฐ์กด ์นผ๋Ÿผ์˜ ์ด๋ฆ„์„ ๋ฐ”๊ฟ€ ๋•Œ๋„ ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด SELECT XXX AS YYY, ZZZ+PPP AS BBB: ์นผ๋Ÿผ XXX์˜ ์ด๋ฆ„์„ YYY๋กœ ๋ฐ”๊พธ๊ณ , ์นผ๋Ÿผ ZZZ์™€ PPP๋ฅผ ๋”ํ•œ ๊ฐ’์˜ ์นผ๋Ÿผ ์ด๋ฆ„์„ BBB๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ PROC SQL; CREATE TABLE TEST AS SELECT NAME, SEX AS MALE_FEMALE, AGE+HEIGHT AS SUM FROM SASHELP.CLASS QUIT; Name MALE_FEMALE SUM ์•Œํ”„๋ ˆ๋“œ ๋‚จ 83 ์•จ๋ฆฌ์Šค์—ฌ 69.5 ๋ฐ”๋ฐ”๋ผ์—ฌ 78.3 ์บ๋Ÿด์—ฌ 76.8 ํ—จ๋ฆฌ ๋‚จ 77.5 ์ œ์ž„์Šค ๋‚จ 69.3 ์ œ์ธ์—ฌ 71.8 ์ž๋„ท์—ฌ 77.5 ์ œํ”„๋ฆฌ ๋‚จ 75.5 ์กด ๋‚จ 71 ์กฐ์ด์Šค์—ฌ 62.3 ์ฃผ๋””์—ฌ 78.3 ๋ฃจ์ด์Šค์—ฌ 68.3 ๋ฉ”๋ฆฌ์—ฌ 81.5 ํ•„๋ฆฝ ๋‚จ 88 ๋กœ๋ฒ„ํŠธ ๋‚จ 76.8 ๋กœ๋‚ ๋“œ ๋‚จ 82 ํ† ๋งˆ์Šค ๋‚จ 68.5 ์œŒ๋ฆฌ์—„ ๋‚จ 81.5 AS๋Š” ์˜์–ด ๋‹จ์–ด โ€˜~๋กœ์„œโ€™๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด AS๋Š” ๊ธฐ์กด ์นผ๋Ÿผ์˜ ์ด๋ฆ„์„ ์ƒˆ๋กญ๊ฒŒ ๋ถ€์—ฌํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 2์˜ โ€˜AS MALE_FEMALEโ€™, โ€˜AS SUMโ€™์€ ํ•ด๋‹น ์นผ๋Ÿผ์˜ ์ด๋ฆ„์„ AS ์ดํ•˜๋กœ ๋ณ€๊ฒฝํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. SQL์„ ์‚ฌ์šฉํ•  ๋•Œ ์ž์ฃผ ์ด์šฉ๋˜๋Š” ๋ช…๋ น์–ด์ด๋‹ˆ ๊ธฐ์–ตํ•ด๋‘๋ฉด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. 8-05. SQL ๋ช…๋ น์–ด๋“ค SQL์—์„œ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋นˆ๋„๊ฐ€ ๋†’๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด๋“ค์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด ๋ช…๋ น์–ด ์†์„ฑ ์˜ˆ์‹œ COMPRESS ํŠน์ • ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. COMPRESS(์นผ๋Ÿผ,โ€˜๋ฌธ์žโ€™) COMPRESS ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. COMPRESS(์นผ๋Ÿผ) TRIM ๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. TRIM(์นผ๋Ÿผ) TRANSLATE ํŠน์ • ๋ฌธ์ž๋ฅผ ๋‹ค๋ฅธ ๋ฌธ์ž๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค TRANSLATE(์นผ๋Ÿผ,โ€˜๋ฌธ์ž 1โ€™,โ€˜๋ฌธ์ž 2โ€™) TRANWRD ํŠน์ • ๋ฌธ์ž๋ฅผ ๋‹ค๋ฅธ ๋ฌธ์ž๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. TRANSWRD(์นผ๋Ÿผ,โ€˜๋ฌธ์ž 1โ€™,โ€˜๋ฌธ์ž 2โ€™) LOWCASE ๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค LOWCASE(์นผ๋Ÿผ) UPCASE ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค UPCASE(์นผ๋Ÿผ) SUBSTR ํŠน์ • ์œ„์น˜์˜ ๋ฌธ์ž๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. SUBSTR(์นผ๋Ÿผ, ์ˆซ์ž 1, ์ˆซ์ž 2) SCAN ํŠน์ • ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ–ˆ์„ ๋•Œ ํŠน์ • ์ˆœ์„œ์˜ ๋ฌธ์ž๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค SCAN((์นผ๋Ÿผ, ์ˆœ์„œ,โ€˜ํŠน์ • ๋ฌธ์žโ€™) CATX ๋ฌธ์ž๋“ค ์‚ฌ์ด์— ์›ํ•˜๋Š” ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค CATX(โ€˜ํŠน์ • ๋ฌธ์žโ€™, ์นผ๋Ÿผ 1, ์นผ๋Ÿผ 2, ..., ์นผ๋Ÿผ n) FIND ํŠน์ • ๋ฌธ์ž์˜ ์œ„์น˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. FIND(์นผ๋Ÿผ,โ€˜ํŠน์ • ๋ฌธ์žโ€™, ์ˆœ์„œ) LEFT ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค LEFT(์นผ๋Ÿผ) RIGHT ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค RIGHT(์นผ๋Ÿผ) PUT ์ˆซ์ž ์นผ๋Ÿผ์„ ๋ฌธ์ž์นผ๋Ÿผ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. PUT(์นผ๋Ÿผ, ๋ฌธ์ž ํฌ๋งท) INPUT ๋ฌธ์ž ์นผ๋Ÿผ์„ ์ˆซ์ž์นผ๋Ÿผ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. INPUT(์นผ๋Ÿผ, ์ˆซ์ž ํฌ๋งท) ์˜ˆ์ œ PROC SQL; CREATE TABLE TEST AS SELECT NAME, AGE, COMPRESS(NAME,'์Šค') AS COMPRESS, TRANSLATE(NAME,'AA','์Šค') AS TRANS ,TRANWRD(NAME,'์Šค','AAAA') AS TRANSWRD, SUBSTR(NAME, 1,2) AS SUBSTR, CATX('-',NAME, SEX) AS CATX ,FIND(NAME,'์Šค') AS FIND, PUT(AGE, 5.) AS PUT, COMPRESS(PUT(AGE, 5.)) AS COM_PUT FROM SASHELP.CLASS QUIT; NAME AGE COMPRESS TRANS TRANSWRD SUBSTR CATX FIND PUT COM_PUT ์•Œํ”„๋ ˆ๋“œ 14 ์•Œํ”„๋ ˆ๋“œ ์•Œํ”„๋ ˆ๋“œ ์•Œํ”„๋ ˆ๋“œ ์•Œ ์•Œํ”„๋ ˆ๋“œ-๋‚จ 0 14 14 ์•จ๋ฆฌ์Šค 13 ์•จ๋ฆฌ ์•จ๋ฆฌ AA ์•จ๋ฆฌ AAAA ์•จ ์•จ๋ฆฌ์Šค-์—ฌ 5 13 13 ๋ฐ”๋ฐ”๋ผ 13 ๋ฐ”๋ฐ”๋ผ ๋ฐ”๋ฐ”๋ผ ๋ฐ”๋ฐ”๋ผ ๋ฐ” ๋ฐ”๋ฐ”๋ผ-์—ฌ 0 13 13 ์บ๋Ÿด 14 ์บ๋Ÿด ์บ๋Ÿด ์บ๋Ÿด ์บ ์บ๋Ÿด-์—ฌ 0 14 14 ํ—จ๋ฆฌ 14 ํ—จ๋ฆฌ ํ—จ๋ฆฌ ํ—จ๋ฆฌ ํ—จ ํ—จ๋ฆฌ-๋‚จ 0 14 14 ์ œ์ž„์Šค 12 ์ œ์ž„ ์ œ์ž„AA ์ œ์ž„AAAA ์ œ ์ œ์ž„์Šค-๋‚จ 5 12 12 ์ œ์ธ 12 ์ œ์ธ ์ œ์ธ ์ œ์ธ ์ œ ์ œ์ธ-์—ฌ 0 12 12 ์ž๋„ท 15 ์ž๋„ท ์ž๋„ท ์ž๋„ท ์ž ์ž๋„ท-์—ฌ 0 15 15 ์ œํ”„๋ฆฌ 13 ์ œํ”„๋ฆฌ ์ œํ”„๋ฆฌ ์ œํ”„๋ฆฌ ์ œ ์ œํ”„๋ฆฌ-๋‚จ 0 13 13 ์กด 12 ์กด ์กด ์กด ์กด ์กด-๋‚จ 0 12 12 ์กฐ์ด์Šค 11 ์กฐ์ด ์กฐ์ด AA ์กฐ์ด AAAA ์กฐ ์กฐ์ด์Šค-์—ฌ 5 11 11 ์ฃผ๋”” 14 ์ฃผ๋”” ์ฃผ๋”” ์ฃผ๋”” ์ฃผ ์ฃผ๋””-์—ฌ 0 14 14 ๋ฃจ์ด์Šค 12 ๋ฃจ์ด ๋ฃจ์ด AA ๋ฃจ์ด AAAA ๋ฃจ ๋ฃจ์ด์Šค-์—ฌ 5 12 12 ๋ฉ”๋ฆฌ 15 ๋ฉ”๋ฆฌ ๋ฉ”๋ฆฌ ๋ฉ”๋ฆฌ ๋ฉ” ๋ฉ”๋ฆฌ-์—ฌ 0 15 15 ํ•„๋ฆฝ 16 ํ•„๋ฆฝ ํ•„๋ฆฝ ํ•„๋ฆฝ ํ•„ ํ•„๋ฆฝ-๋‚จ 0 16 16 ๋กœ๋ฒ„ํŠธ 12 ๋กœ๋ฒ„ํŠธ ๋กœ๋ฒ„ํŠธ ๋กœ๋ฒ„ํŠธ๋กœ ๋กœ๋ฒ„ํŠธ-๋‚จ 0 12 12 ๋กœ๋‚ ๋“œ 15 ๋กœ๋‚ ๋“œ ๋กœ๋‚ ๋“œ ๋กœ๋‚ ๋“œ๋กœ ๋กœ๋‚ ๋“œ-๋‚จ 0 15 15 ํ† ๋งˆ์Šค 11 ํ† ๋งˆ ํ† ๋งˆ AA ํ† ๋งˆ AAAA ํ†  ํ† ๋งˆ์Šค-๋‚จ 5 11 11 ์œŒ๋ฆฌ์—„ 15 ์œŒ๋ฆฌ์—„ ์œŒ๋ฆฌ์—„ ์œŒ๋ฆฌ์—„ ์œŒ ์œŒ๋ฆฌ์—„-๋‚จ 0 15 15 SASHELP.CLASS ํ…Œ์ด๋ธ”์—์„œ ์นผ๋Ÿผ NAME๊ณผ AGE๋ฅผ ์ด์šฉํ•ด์„œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํŠน์ˆ˜ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. 1) ์šฐ์„  ์นผ๋Ÿผ COMPRESS๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. COMPRESS(NAME,โ€˜์Šคโ€™)์— ๋”ฐ๋ผ ์นผ๋Ÿผ NAME์—์„œ โ€˜์Šคโ€™๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ์ œ์ž„์Šค, ์กฐ์ด์Šค, ๋ฃจ์ด์Šค, ํ† ๋งˆ์Šค์—์„œ โ€˜์Šคโ€™๊ฐ€ ์ œ์™ธ๋œ ๊ฒฐ๊ด๊ฐ’์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค. 2) ๋‹ค์Œ์œผ๋กœ ์นผ๋Ÿผ TRANS๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. TRANSLATE(NAME,โ€˜AAโ€™,โ€˜์Šคโ€™)์— ๋”ฐ๋ผ ์นผ๋Ÿผ NAME์˜ โ€˜์Šคโ€™๊ฐ€ โ€˜AAโ€™๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 3) ๋‹ค์Œ์€ ์นผ๋Ÿผ TRAN0WRD๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. TRANWRD(NAME,โ€˜์Šคโ€™,โ€˜AAAAโ€™)์— ๋”ฐ๋ผ ์นผ๋Ÿผ NAME์˜ โ€˜์Šคโ€™๊ฐ€ โ€˜AAAAโ€™๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 4) TRANSLATE์™€ TRANWRD ๋ช…๋ น์–ด๋Š” ์–ผํ• ๋ณด๋ฉด ๋™์ผํ•œ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ณตํ†ต์ ์€ ๋Œ€์ƒ ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” ์ œํ•œ์ด ์—†๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. โ€˜์Šคโ€™๋ฅผ ์“ฐ๋“  โ€˜์ž„์Šคโ€™, โ€˜์ œ์ž„์Šคโ€™๋ฅผ ์“ฐ๋“  ๋Œ€์ƒ์ด ๋˜๋Š” ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” ์ž์œ ๋กญ๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‘˜ ์‚ฌ์ด๋Š” ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. TRANSLATE๋Š” ๋ฌธ์ž๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•œ๊ธ€๋กœ๋Š” 1๊ธ€์ž(1๊ธ€์ž=2BYTE), ์˜์–ด๋กœ๋Š” 2์ž(1์ž=1BYTE)๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2BYTE ๋งŒํผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด์— TRANWRD ๋ช…๋ น์–ด๋Š” ๋‹จ์–ด๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. 1๊ธ€์ž ์ด์ƒ ๋˜๋Š” ๋‹จ์–ด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ โ€˜AAAAโ€™์ฒ˜๋Ÿผ 4์ž์งœ๋ฆฌ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 5) ๋ช…๋ น์–ด SUBSTR์€ ์นผ๋Ÿผ์—์„œ ์œ„์น˜๋ฅผ ์ •ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด SUBSTR(NAME, 1,2) ์ผ ๊ฒฝ์šฐ ์นผ๋Ÿผ NAME์—์„œ ๋ฌธ์ž์˜ ์ฒซ ๋ฒˆ์งธ๋ถ€ํ„ฐ ๋‘ ๋ฒˆ์งธ๋ฅผ ์ถ”์ถœํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์—ฌ๊ธฐ์„œ โ€˜1,2โ€™ ์™€ ๊ฐ™์€ ์œ„์น˜์ •๋ณด๋Š” BYTE ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์•ŒํŒŒ๋ฒณ์„ ๊ธฐ์ค€์œผ๋กœ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ ๋ฐฐ์› ๋“ฏ์ด ์•ŒํŒŒ๋ฒณ์€ 1BYTE์— ํ•œ ์ž์ง€๋งŒ, ํ•œ๊ธ€์€ 2BYTE์— ํ•œ ๊ธ€์ž์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํ•œ๊ธ€ ๋ฐ์ดํ„ฐ ์ผ ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์งธ ํ•œ ๊ธ€์ž๋ฅผ ๋ฝ‘๊ณ  ์‹ถ๋‹ค๋ฉด SUBSTR(NAME, 1,2)๋ฅผ, ์ฒซ ๋ฒˆ์งธ๋ถ€ํ„ฐ ๋‘ ๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ๋ฝ‘๊ณ  ์‹ถ๋‹ค๋ฉด SUBSTR(NAME, 1,4)๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 6) CATX ๋ช…๋ น์–ด๋Š” ์—ฌ๋Ÿฌ ์นผ๋Ÿผ์„ ํ†ตํ•ฉํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. CATX(โ€˜-โ€™,NAME, SEX)๋Š” ์นผ๋Ÿผ NAME๊ณผ SEX ์‚ฌ์ด์— โ€˜-โ€™๋ฅผ ๋„ฃ์–ด์„œ ๋‘ ์นผ๋Ÿผ์„ ํ•˜๋‚˜์˜ ์นผ๋Ÿผ์œผ๋กœ ํ•ฉ์น˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์นผ๋Ÿผ์˜ ๊ฐœ์ˆ˜๋Š” 2๊ฐœ ์ด์ƒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 7) FIND ๋ช…๋ น์–ด๋Š” ์นผ๋Ÿผ ๋‚ด์—์„œ ํŠน์ •ํ•œ ๋ฌธ์ž์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. FIND(NAME,โ€˜์Šคโ€™)๋Š” ์นผ๋Ÿผ NAME์—์„œ โ€˜์Šคโ€™๋ผ๋Š” ๋ฌธ์ž๊ฐ€ ๋ช‡ ๋ฒˆ์งธ ์œ„์น˜์— ์žˆ๋Š”์ง€๋ฅผ ์ฐพ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์นผ๋Ÿผ NAME์—์„œ โ€˜์ œ์ž„์Šคโ€™๋ผ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด FIND ๋ช…๋ น์–ด์˜ ๊ฒฐ๊ด๊ฐ’์€ 5๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ•œ๊ธ€์€ ํ•œ ๊ธ€์ž๊ฐ€ 2๊ฐœ์˜ ์œ„์น˜๋ฅผ ์ฐจ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— โ€˜์Šคโ€™๊ฐ€ ๋‚˜์˜ค๋Š” ์ฒ˜์Œ ๋‚˜์˜ค๋Š” ์œ„์น˜๋Š” 5๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 8) PUT ๋ช…๋ น์–ด๋Š” ์ˆซ์ž ์นผ๋Ÿผ์„ ๋ฌธ์ž ์นผ๋Ÿผ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. PUT(AGE, 5.)์˜ ๊ฒฝ์šฐ ์นผ๋Ÿผ AGE๋ฅผ ํฌ๋งท ๊ธธ์ด๊ฐ€ 5์ธ ๋ฌธ์ž ์นผ๋Ÿผ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. SAS์—์„œ๋Š” ํฌ๋งท์˜ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” โ€˜.โ€™์„ ๋ถ™์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜5.โ€™์ฒ˜๋Ÿผ ํฌ๋งท ๊ธธ์ด(์œ ํ˜•) ๋’ค์— ๋ฐ˜๋“œ์‹œ โ€˜.โ€™์„ ๋ถ™์—ฌ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ SAS๋Š” ๋ช…๋ น์–ด๊ฐ€ ํฌ๋งท์„ ๋œปํ•จ์„ ์•Œ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํฌ๋งท ๊ธธ์ด๋Š” ๋ณธ๋ž˜ ์นผ๋Ÿผ ๋ฐ์ดํ„ฐ ๊ธธ์ด๋ณด๋‹ค ๊ธธ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ PUT(AGE, 1.)์œผ๋กœ ์›๋ž˜์˜ AGE ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด์ธ 2๋ณด๋‹ค ์ž‘๊ฒŒ ์ž…๋ ฅ์„ ํ•  ๊ฒฝ์šฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. PUT ๋ช…๋ น์–ด๋Š” ๋Œ€์ƒ ์นผ๋Ÿผ์ด ์ˆซ์ž ์นผ๋Ÿผ์ธ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 9) INPUT ๋ช…๋ น์–ด๋Š” ๋ฌธ์ž ์นผ๋Ÿผ์„ ์ˆซ์ž ์นผ๋Ÿผ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. INPUT(์นผ๋Ÿผ, N.)์˜ ๊ฒฝ์šฐ ๋Œ€์ƒ ์นผ๋Ÿผ์„ ํฌ๋งท ๊ธธ์ด๊ฐ€ N์ธ ์ˆซ์ž ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋ช…๋ น์–ด๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์†Œ์ˆ˜์  ์ดํ•˜ ์ˆซ์ž๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ โ€˜5.2โ€™์ฒ˜๋Ÿผ 5์ž๋ฆฌ ํฌ๋งท์ด๋˜ ์†Œ์ˆ˜์  ์•„๋ž˜ 2๊ธ€์ž๊นŒ์ง€ ํ‘œํ˜„์„ ํ•˜๋ผ๋Š” ์˜๋ฏธ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 8-06. SQL๋กœ ํŠน์ • ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๊ธฐ(WHERE) SQL๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•  ๋•Œ ํŠน์ •ํ•œ ์กฐ๊ฑด์„ ์ฃผ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ ๋งค์šฐ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. โ€˜๋‚˜์ด๊ฐ€ 20์‚ด์ธ ์‚ฌ๋žŒ์„ ๋ฝ‘๊ธฐโ€™, โ€˜์ด๋ฆ„์ด ์กด์ธ ์‚ฌ๋žŒ์„ ๋ฝ‘๊ธฐโ€™์ฒ˜๋Ÿผ ํŠน์ •ํ•œ ์กฐ๊ฑด์„ ์ค„ ์ˆ˜ ์žˆ์ฃ . ์ง€๊ธˆ๋ถ€ํ„ฐ๋Š” SQL์„ ํ™œ์šฉํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด WHERE XXX=YY: ์นผ๋Ÿผ XXX๊ฐ€ YY์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์˜ˆ์ œ 1 PROC SQL; CREATE TABLE TEST AS SELECT NAME, AGE, HEIGHT FROM SASHELP.CLASS WHERE AGE=12 QUIT; Name Age Height ์ œ์ž„์Šค 12 57.3 ์ œ์ธ 12 59.8 ์กด 12 59 ๋ฃจ์ด์Šค 12 56.3 ๋กœ๋ฒ„ํŠธ 12 64.8 ์˜ˆ์ œ 1์— ๋”ฐ๋ผ ๋‚˜์˜จ ๊ฒฐ๊ด๊ฐ’์—๋Š” ์นผ๋Ÿผ AGE=12์ธ ๊ฐ’๋“ค๋งŒ ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์นผ๋Ÿผ ๋ช…๋„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ณ  โ€˜=โ€™์ด์™ธ์— ๋‹ค๋ฅธ ๋ถ€๋“ฑํ˜ธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜WHERE 59<=HEIGHT<=61โ€™์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ HEIGHT๊ฐ€ 59~61์‚ฌ์ด์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. AND์™€ OR WHERE ๋ช…๋ น์–ด์—์„œ โ€˜ANDโ€™์™€ โ€˜ORโ€™๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1) AND๋Š” โ€˜๊ทธ๋ฆฌ๊ณ โ€™๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ค‘ํ•™๊ต ๋•Œ ํ•™์Šตํ•œ ์ง‘๋‹จ ๊ฐ„ ๋ฒค๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ๊ต์ง‘ํ•ฉ์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ถœ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. 2) OR๋Š” โ€˜๋˜๋Š”โ€™์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋ฒค๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ํ•ฉ์ง‘ํ•ฉ์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ช…๋ น์–ด๊ฐ€ OR์™€ ํ•จ๊ป˜ ์žˆ๋‹ค๋ฉด, ๊ฐ๊ฐ์˜ ์กฐ๊ฑด์— ํ•˜๋‚˜๋ผ๋„ ๋ถ€ํ•ฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋‘ ์ถœ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด WHERE XXX=YY AND ZZZ=AA: ์นผ๋Ÿผ XXX๊ฐ€ YY์ด๊ณ  ์นผ๋Ÿผ ZZZ๊ฐ€ AA์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์˜ˆ์ œ 2 PROC SQL; CREATE TABLE TEST AS SELECT NAME, AGE, HEIGHT FROM SASHELP.CLASS WHERE AGE=12 AND 59<=HEIGHT<=61 QUIT; Name Age Height ์ œ์ธ 12 59.8 ์กด 12 59 ์นผ๋Ÿผ AGE๊ฐ€ 12์ด๊ณ  HEIGHT๊ฐ€ 59~61 ์‚ฌ์ด์ธ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. AND๋ฅผ OR๋กœ ๋ฐ”๊ฟ”์„œ๋„ ์‹คํ–‰ํ•ด ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์˜ˆ์ œ 4. ์™€๋Š” ๋‹ค๋ฅธ ๊ฒฐ๊ด๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. tip AND์™€ OR๋ฅผ ์ด์šฉํ•˜๋ฉด WHERE ๋ช…๋ น์–ด๋ฅผ 2๊ฐœ ์ด์ƒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜AGE=12โ€™ ์ด๋ฉด์„œ โ€˜59<=HEIGHT<=61โ€™์ธ ๋‘ ๊ฐœ์˜ ์กฐ๊ฑด์„ ๋™์‹œ์— ์ ์šฉํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ โ€˜WHERE AGE=12 AND 59<=HEIGHT<=61โ€™์ด๋ผ๊ณ  ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค(๋ชจ๋“  ์ˆ˜์‹์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. =(EQ), ^=(NE, ๊ฐ™์ง€ ์•Š๋‹ค), >(GT), <(LT), >=(GE), <=(LE) ๋“ฑ์˜ ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด„ํ˜ธ ์•ˆ์˜ ์˜์–ด๋ฅผ ์“ฐ์…”๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.). ๊ทธ๋Ÿฌ๋ฉด ์นผ๋Ÿผ AGE์™€ HEIGHT์—์„œ ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ถœ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ํŠน์ • ๋ฌธ์ž์ธ ๊ฒฝ์šฐ ์นผ๋Ÿผ์ด ๋ฌธ์ž์ผ ๋•Œ๋Š” ๋ฐ์ดํ„ฐ์— โ€˜ โ€™์„ ์”Œ์šฐ๊ณ  ์ˆซ์ž์ผ ๋•Œ๋Š” ์”Œ์šฐ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. SAS๋Š” ๋ฌธ์ž ์นผ๋Ÿผ๊ณผ ์ˆซ์ž ์นผ๋Ÿผ์„ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฌธ์ž ์นผ๋Ÿผ์ผ ๊ฒฝ์šฐ์—๋Š” ๋ฐ์ดํ„ฐ์— โ€˜ โ€™๋ฅผ ์”Œ์›Œ์„œ ๊ตฌ๋ถ„์„ ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์นผ๋Ÿผ NAME์—์„œ ์กด์„ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด โ€˜WHERE NAME=โ€˜์กดโ€™โ€™์œผ๋กœ ์ž…๋ ฅ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฌธ์ž ์นผ๋Ÿผ์— โ€˜ โ€™์„ ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค๋ฉด SAS๋Š” ์ด๋ฅผ ์ˆซ์ž๋กœ ๋ฐ›์•„๋“ค์ด๊ณ  ์˜ค๋ฅ˜ ๊ฒฐ๊ด๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ช…๋ น์–ด WHERE XXX='์กด': ์นผ๋Ÿผ XXX๊ฐ€ '์กด'์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์˜ˆ์ œ 3 PROC SQL; CREATE TABLE TEST AS SELECT NAME, AGE, HEIGHT FROM SASHELP.CLASS WHERE NAME='์กด' QUIT; Name Age Height ์กด 12 59 ์นผ๋Ÿผ NAME์ด '์กด'์ธ ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœ๋์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. tip WHERE ์กฐ๊ฑด ์ด์™ธ์˜ ๊ฒฝ์šฐ์—๋„ ๋ฌธ์ž ์นผ๋Ÿผ์ธ ๊ฒฝ์šฐ ' '์„ ์”Œ์šฐ๊ณ  ์ˆซ์ž ์นผ๋Ÿผ์ธ ๊ฒฝ์šฐ ์”Œ์šฐ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž ์นผ๋Ÿผ์ž„์—๋„ ' '๋ฅผ ์”Œ์šฐ์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด WHERE NAME=์กด์œผ๋กœ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ SAS๋Š” ์กด์„ ์นผ๋Ÿผ์œผ๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ ์กด์€ ํ…Œ์ด๋ธ”์— ์—†์œผ๋ฏ€๋กœ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. 8-07. SQL๋กœ ๋ฐ์ดํ„ฐ ์ •๋ ฌํ•˜๊ธฐ(ORDER BY) ๋•Œ๋•Œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ์„ ํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ ์ผ๋ถ€ SAS ๋ช…๋ น์–ด๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๋ ฌ์ด ๋ผ ์žˆ์–ด์•ผ ์‹คํ–‰์ด ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ SQL์€ ์–ด๋–ป๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด ORDER BY XXX: ์นผ๋Ÿผ XXX์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ PROC SQL; CREATE TABLE TEST AS SELECT * FROM SASHELP.CLASS ORDER BY AGE QUIT; Name Sex Age Height Weight ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ์กด ๋‚จ 12 59 99.5 ์ œ์ธ์—ฌ 12 59.8 84.5 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์ฃผ๋””์—ฌ 14 64.3 90 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ์ž๋„ท์—ฌ 15 62.5 112.5 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋ฐ์ดํ„ฐ๊ฐ€ ์นผ๋Ÿผ AGE์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ORDER BY ๊ตฌ๋ฌธ์„ ํ†ตํ•ด์„œ ์ •๋ ฌ์„ ํ•  ๋•Œ ์นผ๋Ÿผ AGE๋งŒ ๋ฐ”๋€Œ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ์ „์ฒด ์นผ๋Ÿผ์ด ๋ชจ๋‘ ํ•จ๊ป˜ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ AGE๊ฐ€ 11์ธ โ€˜ํ† ๋งˆ์Šค, ๋‚จ, 11, 57.5, 85โ€™ ํ–‰์ด ๊ฐ€์žฅ ์œ„๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์—‘์…€ ์ •๋ ฌ๊ณผ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์—‘์…€์—์„œ ํ•„ํ„ฐ๋ฅผ ๊ฑธ๊ณ  ์ •๋ ฌ์„ ํ•  ๋•Œ, ์ „์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๋ ฌ ๊ธฐ์ค€์„ ๋”ฐ๋ผ ์ด๋™ํ•˜๋Š” ๊ฒƒ๋„ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. tip ๋งŒ์•ฝ ORDER BY๋กœ ์ •๋ ฌ์„ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด ์นผ๋Ÿผ ๋’ค์— โ€˜DESCโ€™๋ฅผ ๋ถ™์ด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด AGE์˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•˜๋ ค๋ฉด โ€˜ORDER BY AGE DESCโ€™๋กœ ์ ์œผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์นผ๋Ÿผ AGE ๊ฐ’์ด ๊ฐ€์žฅ ํฐ ํ–‰์ด ์œ„์—์„œ๋ถ€ํ„ฐ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. PROC SQL; CREATE TABLE TEST AS SELECT * FROM SASHELP.CLASS ORDER BY AGE DESC QUIT; ์—ฌ๋Ÿฌ ๊ฐœ ์นผ๋Ÿผ์„ ๋™์‹œ์— ์ •๋ ฌํ•˜๊ธฐ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์นผ๋Ÿผ์„ ๋™์‹œ์— ์ •๋ ฌํ•˜๋ ค๋ฉด ๋Œ€์ƒ ์นผ๋Ÿผ์„ ๋ชจ๋‘ ์ ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์นผ๋Ÿผ ์‚ฌ์ด์— โ€˜,โ€™๋ฅผ ๋ถ™์—ฌ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. SAS ๋ช…๋ น์–ด์—์„œ๋Š” ์นผ๋Ÿผ๋“ค ์‚ฌ์ด๋ฅผ โ€˜ โ€™(๊ณต๋ฐฑ)์œผ๋กœ ๊ตฌ๋ถ„ํ–ˆ๋‹ค๋ฉด, SQL ๋ช…๋ น์–ด์—์„œ๋Š” โ€˜,โ€™๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. SELECT ๋ช…๋ น์–ด์—์„œ ์นผ๋Ÿผ์„ โ€˜,โ€™๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฑธ ํ™•์ธํ•˜์…จ์„ ๊ฒ๋‹ˆ๋‹ค. SQL์—์„œ๋Š” ์นผ๋Ÿผ๋“ค์„ ๋‚˜์—ดํ•  ๋•Œ โ€˜,โ€™๋ฅผ ๋‘์–ด ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ AGE์™€ HEIGHT ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ช…๋ น์–ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด ORDER BY AGE, HEIGHT: ์นผ๋Ÿผ AGE๋ฅผ ์ •๋ ฌํ•˜๊ณ , AGE ๋ณ„๋กœ ์นผ๋Ÿผ HEIGHT๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค ์˜ˆ์ œ 2 PROC SQL; CREATE TABLE TEST AS SELECT * FROM SASHELP.CLASS ORDER BY AGE, HEIGHT QUIT; Name Sex Age Height Weight ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์กด ๋‚จ 12 59 99.5 ์ œ์ธ์—ฌ 12 59.8 84.5 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ฃผ๋””์—ฌ 14 64.3 90 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์ž๋„ท์—ฌ 15 62.5 112.5 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ•„๋ฆฝ ๋‚จ 16 72 150 ORDER BY ๋’ค์— ์˜ค๋Š” ์นผ๋Ÿผ์˜ ์ˆœ์„œ๋Œ€๋กœ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌ๋ฐ›์•„ ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 2์—์„œ๋Š” ์•ž์— ์žˆ๋Š” ์นผ๋Ÿผ AGE์˜ ์ˆœ์„œ๋Œ€๋กœ ์šฐ์„  ์ •๋ ฌ์„ ํ•œ ๋‹ค์Œ, ๊ทธ์— ์†ํ•ด ์žˆ๋Š” ์นผ๋Ÿผ HEIGHT๊ฐ€ ์ •๋ ฌ์ด ๋ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ AGE๊ฐ€ ์ •๋ ฌ์ด ๋œ ๋‹ค์Œ, ์นผ๋Ÿผ AGE=11์— ์†ํ•ด์žˆ๋Š” ์นผ๋Ÿผ HEIGHT๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ์ด ๋˜๋Š” ์‹์ž…๋‹ˆ๋‹ค. ํŠน์ • ์นผ๋Ÿผ AGE ๊ฐ’์— ์†ํ•œ ์นผ๋Ÿผ HEIGHT๋ผ๋ฆฌ ๋‹ค์‹œ ์žฌ์ •๋ ฌ์„ ํ•œ๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 8-08. SQL๋กœ ์กฐ๊ฑด๋ฌธ ๋งŒ๋“ค๊ธฐ(CASE WHEN) ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์นผ๋Ÿผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์นผ๋Ÿผ AGE๊ฐ€ 12์ผ ๊ฒฝ์šฐ โ€˜์—ด๋‘ ์‚ดโ€™์„, 13์ผ ๊ฒฝ์šฐ โ€˜์—ด์„ธ ์‚ดโ€™์ด ๋‚˜์˜ค๋„๋ก ์ƒˆ๋กœ์šด ์นผ๋Ÿผ์„ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ํŠน์ • ์กฐ๊ฑด์„ ์ฃผ๊ณ  ์ด์— ๋”ฐ๋ฅธ ๊ฒฐ๊ด๊ฐ’์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด CASE WHEN XXX=YY THEN ZZ END AS PPP: ์นผ๋Ÿผ XXX๊ฐ€ YY ์ผ ๋•Œ, ZZ ๊ฐ’์„ ๊ฐ–๋Š” ์นผ๋Ÿผ PPP๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 1 PROC SQL; CREATE TABLE TEST AS SELECT *, CASE WHEN AGE=12 THEN '์—ด๋‘ ์‚ด' WHEN AGE=13 THEN '์—ด์„ธ ์‚ด' END AS NEW_AGE FROM SASHELP.CLASS QUIT; Name Sex Age Height Weight NEW_AGE ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5. ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ์—ด์„ธ ์‚ด ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์—ด์„ธ ์‚ด ์บ๋Ÿด์—ฌ 14 62.8 102.5. ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5. ์ œ์ž„์Šค ๋‚จ 12 57.3 83์—ด๋‘ ์‚ด ์ œ์ธ์—ฌ 12 59.8 84.5์—ด๋‘ ์‚ด ์ž๋„ท์—ฌ 15 62.5 112.5. ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์—ด์„ธ ์‚ด ์กด ๋‚จ 12 59 99.5์—ด๋‘ ์‚ด ์กฐ์ด์Šค์—ฌ 11 51.3 50.5. ์ฃผ๋””์—ฌ 14 64.3 90. ๋ฃจ์ด์Šค์—ฌ 12 56.3 77์—ด๋‘ ์‚ด ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112. ํ•„๋ฆฝ ๋‚จ 16 72 150. ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128์—ด๋‘ ์‚ด ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133. ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85. ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112. ์นผ๋Ÿผ AGE ๊ฐ’๊ณผ ์นผ๋Ÿผ NEW_AGE๋ฅผ ๋น„๊ตํ•ด ๋ณด๋ฉด ๋™์ผํ•จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. WHEN ์ดํ›„๋กœ ์กฐ๊ฑด๊ณผ ๊ฒฐ๊ด๊ฐ’์„ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. WHEN ์‚ฌ์šฉ์ž์˜ ์ž„์˜๋Œ€๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์น™์—ฐ์‚ฐ ๊ฐ’์ด๋‚˜ ๋ถ€๋“ฑํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์กฐ๊ฑด์— ๋งž๋Š” ๊ฒฐ๊ด๊ฐ’์„ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค โ€˜CASE WHENโ€™๋ช…๋ น์–ด๋Š” ์•ž์„œ ๋ฐฐ์šด SAS ๋ช…๋ น์–ด์˜ โ€˜IF~THENโ€™ ๋ช…๋ น์–ด์™€ ๊ธฐ๋Šฅ์€ ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 1์˜ ๋ช…๋ น์–ด์™€ ๋™์ผํ•œ SAS ๋ช…๋ น์–ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. DATA TEST; SET SASHELP.CLASS; IF AGE=12 THEN NEW_AGE='์—ด๋‘ ์‚ด'; ELSE IF AGE=13 THEN NEW_AGE='์—ด์„ธ ์‚ด'; RUN; ELSE ๋ช…๋ น์–ด SQL์˜ โ€˜CASE WHENโ€™ ๋ช…๋ น์–ด์—์„œ ELSE ๋ช…๋ น์–ด๋ฅผ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. WHEN ๋ช…๋ น์–ด๋กœ ์ง€์ •ํ•œ ๊ฐ’ ์ด์™ธ ๋‹ค๋ฅธ ๋ชจ๋“  ๊ฐ’์„ ์ง€์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์นผ๋Ÿผ AGE=12 ์ด์™ธ์˜ ๊ฐ’์ผ ๊ฒฝ์šฐ โ€˜๋‚˜๋จธ์ง€โ€™๋ผ๋Š” ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ 2 PROC SQL; CREATE TABLE TEST AS SELECT *, CASE WHEN AGE=12 THEN '์—ด๋‘ ์‚ด' ELSE '๋‚˜๋จธ์ง€' END AS NEW_AGE FROM SASHELP.CLASS QUIT; Name Sex Age Height Weight NEW_AGE ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ๋‚˜๋จธ์ง€ ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋‚˜๋จธ์ง€ ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ๋‚˜๋จธ์ง€ ์บ๋Ÿด์—ฌ 14 62.8 102.5 ๋‚˜๋จธ์ง€ ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ๋‚˜๋จธ์ง€ ์ œ์ž„์Šค ๋‚จ 12 57.3 83์—ด๋‘ ์‚ด ์ œ์ธ์—ฌ 12 59.8 84.5์—ด๋‘ ์‚ด ์ž๋„ท์—ฌ 15 62.5 112.5 ๋‚˜๋จธ์ง€ ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ๋‚˜๋จธ์ง€ ์กด ๋‚จ 12 59 99.5์—ด๋‘ ์‚ด ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ๋‚˜๋จธ์ง€ ์ฃผ๋””์—ฌ 14 64.3 90 ๋‚˜๋จธ์ง€ ๋ฃจ์ด์Šค์—ฌ 12 56.3 77์—ด๋‘ ์‚ด ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ๋‚˜๋จธ์ง€ ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋‚˜๋จธ์ง€ ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128์—ด๋‘ ์‚ด ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ๋‚˜๋จธ์ง€ ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ๋‚˜๋จธ์ง€ ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ๋‚˜๋จธ์ง€ ELSE๋กœ ์ธํ•ด์„œ AGE=12 ์ด์™ธ์˜ ํ–‰์˜ ์นผ๋Ÿผ NEW_AGE์—๋Š” ๋ชจ๋‘ '๋‚˜๋จธ์ง€'๋ผ๋Š” ๊ฐ’์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. 8-09. SQL๋กœ ๊ทธ๋ฃน๋ณ„๋กœ ์—ฐ์‚ฐํ•˜๊ธฐ(GROUP BY) GROUP BY ๋ช…๋ น์–ด๋Š” SAS๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ORACLE์—์„œ๋„ ๋Œ€๋‹จํžˆ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. GROUP BY๋Š” ํŠน์ • ๊ฐ’๋“ค์„ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ๊ณ  ํ•ด๋‹น ๊ทธ๋ฃน ์•ˆ์—์„œ์˜ ์‚ฌ์น™์—ฐ์‚ฐ ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ GROUP BY๋กœ ๋‚˜์ด ๋ฐ์ดํ„ฐ๋ฅผ 3๊ฐœ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆˆ ๋‹ค์Œ ๋ง์…ˆ(+) ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๋ฉด, ๊ฐ ๊ทธ๋ฃน ์•ˆ์—์„œ๋งŒ ๋ง์…ˆ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด GROUP BY XXX: ์นผ๋Ÿผ XXX๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ ์„œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค ์˜ˆ์ œ PROC SQL; CREATE TABLE TEST AS SELECT *, SUM(AGE) AS SUM_AGE FROM SASHELP.CLASS GROUP BY SEX QUIT; Name Sex Age Height Weight SUM_AGE ์ œ์ž„์Šค ๋‚จ 12 57.3 83 134 ์กด ๋‚จ 12 59 99.5 134 ํ•„๋ฆฝ ๋‚จ 16 72 150 134 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 134 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 134 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 134 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 134 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 134 ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 134 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 134 ์ œ์ธ ์—ฌ 12 59.8 84.5 119 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 119 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 119 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 119 ์ฃผ๋””์—ฌ 14 64.3 90 119 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 119 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 119 ์บ๋Ÿด์—ฌ 14 62.8 102.5 119 ์ž๋„ท์—ฌ 15 62.5 112.5 119 GROUP BY ๋ช…๋ น์–ด๋Š” ํŠน์ • ์นผ๋Ÿผ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆ ์„œ ์—ฐ์‚ฐํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์˜ˆ์ œ์™€ ๊ฐ™์ด GROUP BY SEX๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๋ฉด SEX์˜ โ€˜๋‚จโ€™, โ€˜์—ฌโ€™ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ฐ๊ฐ ์—ฐ์‚ฐ ์ž‘์—…์„ ์‹ค์‹œํ•ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ SEX์˜ ๋ฐ์ดํ„ฐ๋Š” โ€˜๋‚จโ€™๊ณผ โ€˜์—ฌโ€™ ๋‘ ์ข…๋ฅ˜๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์— ์นผ๋Ÿผ AGE๋ฅผ ๋‘ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆˆ ๋‹ค์Œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ SUM(AGE)๋Š” ์นผ๋Ÿผ SEX์˜ โ€˜๋‚จโ€™, โ€˜์—ฌโ€™ ๊ฐ๊ฐ ๋”ฐ๋กœ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์นผ๋Ÿผ SEX๊ฐ€ โ€˜๋‚จโ€™์ธ ๊ฒฝ์šฐ SUM(AGE) ๊ฐ’์€ 134, ์นผ๋Ÿผ SEX๊ฐ€ โ€˜์—ฌโ€™์ธ ๊ฒฝ์šฐ SUM(AGE) ๊ฐ’์€ 119๋กœ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์นผ๋Ÿผ SEX์˜ ๊ฐ’์ด โ€˜๋‚จโ€™, โ€˜์—ฌโ€™, โ€˜์•„์ดโ€™๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์—ˆ๋‹ค๋ฉด, ์„ธ ๊ฐ€์ง€ ๊ธฐ์ค€์œผ๋กœ ๊ฐ๊ฐ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ–ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. tip SUM(AGE) ๋ช…๋ น์–ด๋Š” AGE์˜ ๋ชจ๋“  ๊ฐ’์„ ํ•ฉํ•˜๋ผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. 8-10. SQL๋กœ ๊ทธ๋ฃน๋ณ„ ํŠน์ • ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๊ธฐ(HAVING) (์ „์ž์ฑ… ๋‚ด์šฉ) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์˜ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋งํฌ) SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ์ „์ž์ฑ… ๊ตฌ๋งค 8-11. SQL ๋ช…๋ น์–ด ์ˆœ์„œ (์ „์ž์ฑ… ๋‚ด์šฉ) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์˜ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋งํฌ) SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ์ „์ž์ฑ… ๊ตฌ๋งค 8-12. SQL๋กœ ํ…Œ์ด๋ธ”์— ์—†๋Š” ์นผ๋Ÿผ ์ƒ์„ฑ ํ›„ ์—ฐ์‚ฐ(CALCULATED) (์ „์ž์ฑ… ๋‚ด์šฉ) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์˜ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋งํฌ) SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ์ „์ž์ฑ… ๊ตฌ๋งค 8-13. SQL๋กœ ๋‚ ์งœ ๋ช…๋ น์–ด ์ˆ˜ํ–‰ํ•˜๊ธฐ (์ „์ž์ฑ… ๋‚ด์šฉ) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์˜ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋งํฌ) SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ์ „์ž์ฑ… ๊ตฌ๋งค 8-๋ฒˆ์™ธ. SQL๋กœ ๋ฐ์ดํ„ฐ ์ง์ ‘ ์ž…๋ ฅํ•˜๊ธฐ SQL๋กœ ํ…Œ์ด๋ธ”์„ ์ง์ ‘ ์ƒ์„ฑํ•˜๊ณ , ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SQL์€ ์ฃผ๋กœ ์™„์„ฑ๋œ ํ…Œ์ด๋ธ”์„ ์ด์šฉํ•˜์—ฌ ์กฐํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•  ์ผ์€ ์ž์ฃผ ์ผ์–ด๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 1) SQL๋กœ ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•˜๊ธฐ SQL๋กœ ํ…Œ์ด๋ธ”์„ ์ง์ ‘ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ๋นˆ ํ…Œ์ด๋ธ”๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด PROC SQL;: ํ”„๋Ÿฌ์‹œ์ € SQL์„ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. CREATE TABLE XXX: ํ…Œ์ด๋ธ” XXX๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. (YYY CHAR(6)): ๋ฌธ์ž ๋ณ€์ˆ˜์ด๋ฉด์„œ ๊ธธ์ด๊ฐ€ 6์ธ ์นผ๋Ÿผ YYY๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ;: ์ „์ฒด SQL ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. QUIT;: ํ”„๋Ÿฌ์‹œ์ € SQL์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 1 PROC SQL; CREATE TABLE TEST (NAME CHAR(12) ,AGE NUM ,HEIGHT NUM ,WEIGHT NUM ,ADDR CHAR) QUIT; NAME AGE HEIGHT WEIGHT ADDR ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ๋นˆ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž ์นผ๋Ÿผ NAME, ADDR๊ณผ ์ˆซ์ž ์นผ๋Ÿผ AGE, HEIGHT, WEIGHT๊ฐ€ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ CHAR ๋’ค์— ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ธฐ๋ณธ ๊ธธ์ด๋กœ ์นผ๋Ÿผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ธธ์ด๋Š” 8BYTE์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž ์นผ๋Ÿผ์€ 4๊ธ€์ž๋งŒ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์นผ๋Ÿผ NAME์€ 12BYTE๋กœ ํ•œ๊ธ€ 6๊ธ€์ž ์˜์–ด 12๊ฐœ ์•ŒํŒŒ๋ฒณ, ์นผ๋Ÿผ ADDR์€ 8BYTE๋กœ ํ•œ๊ธ€ 4๊ธ€์ž ์˜์–ด 12๊ฐœ ์•ŒํŒŒ๋ฒณ๊นŒ์ง€ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ์ˆซ์ž ์นผ๋Ÿผ๋„ โ€˜( )โ€™๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค. ์ˆซ์ž ์นผ๋Ÿผ ๋˜ํ•œ ๊ธฐ๋ณธ ๊ธธ์ด๋Š” 8BYTE์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๋ฌธ์ž ์นผ๋Ÿผ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ 309์ž๊นŒ์ง€ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ถฉ๋ถ„ํžˆ ๊ธด ๊ธธ์ด์ด๋ฏ€๋กœ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ƒ์„ฑํ•˜์…”๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค. 2) SQL๋กœ ๋ฐ์ดํ„ฐ ์ž…๋ ฅํ•˜๊ธฐ SQL๋กœ ๋งŒ๋“ค์–ด์ง„ ๋นˆ ํ…Œ์ด๋ธ”์— ๊ฐ’์„ ๋„ฃ์–ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๊ณ , ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์šฐ์„  ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋งŒ๋“ค์–ด์ง„ ํ…Œ์ด๋ธ” TEST๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง์ ‘ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ๋ช…๋ น์–ด(1) INSERT INTO TEST SET XXX=โ€˜YYYโ€™, ZZZ=111: ํ…Œ์ด๋ธ” TEST์˜ ์นผ๋Ÿผ XXX์— ๋ฌธ์ž ๊ฐ’ YYY๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์นผ๋Ÿผ ZZZ์— ์ˆซ์ž ๊ฐ’ 111์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 2 PROC SQL; INSERT INTO TEST SET NAME='ํ˜ธ๋‚ ๋‘', AGE=27, HEIGHT=6, WEIGHT=70 SET NAME='๋ฉ”์‹œ', AGE=26, HEIGHT=5, WEIGHT=65, ADDR='๋ฐ”๋ฅด์…€๋กœ๋‚˜' QUIT; NAME AGE HEIGHT WEIGHT ADDR ํ˜ธ๋‚ ๋‘ 27 6 70. ๋ฉ”์‹œ 26 5 65 ๋ฐ”๋ฅด์…€๋กœ INSERT INTO๋กœ ๋Œ€์ƒ ํ…Œ์ด๋ธ”์„ ์ •ํ•ฉ๋‹ˆ๋‹ค. INSERT INTO๋Š” ์˜์–ด๋กœ โ€˜~์— ์‚ฝ์ž…ํ•˜๋‹คโ€™๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด ๋ช…๋ น์–ด๋กœ ํ…Œ์ด๋ธ” TEST์— ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•  ๊ฒƒ์ž„์„ ๋ช…๋ นํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ SET ๋ช…๋ น์–ด๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„  SAS ๋ช…๋ น์–ด์—์„œ SET์€ ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์—ญํ• ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ SQL์—์„œ๋Š” ์ด์™€๋Š” ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ SET ๋ช…๋ น์–ด๋Š” ์นผ๋Ÿผ์„ ์ง€๋ชฉํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. SET ๋ช…๋ น์–ด ๋‹ค์Œ์— ์นผ๋Ÿผ๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜์—ดํ•˜์—ฌ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. SET ๋ช…๋ น์–ด๋Š” ํ•œ ํ–‰์—๋งŒ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ 2๊ฐœ์˜ ํ–‰์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋ ค๋ฉด SET ๋ช…๋ น์–ด๋ฅผ ๋‘ ๋ฒˆ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 3.์—์„œ๋Š” 2๊ฐœ์˜ ํ–‰์„ ์ž…๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— SET ๋ช…๋ น์–ด๊ฐ€ ๋‘ ๋ฒˆ ์ž…๋ ฅ๋์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜์ง€๋งŒ SET ๋ช…๋ น์–ด์— ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์€ ์นผ๋Ÿผ์€ ๋ฌด์‹œํ•˜๊ณ  ์ž…๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ง์ ‘ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ๋ช…๋ น์–ด(2) SET ๋ช…๋ น์–ด์™€๋Š” ๋‹ค๋ฅธ VALUE ๋ช…๋ น์–ด๋ฅผ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SET ๋ช…๋ น์–ด๋Š” ๋งค ํ–‰๋งˆ๋‹ค ์ž…๋ ฅํ•  ์นผ๋Ÿผ๋ช…์„ ์ง€์ •ํ•ด์•ผ๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ VALUE ๋ช…๋ น์–ด๋Š” ์นผ๋Ÿผ๋ช…์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ์˜ ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ 1์— ์ด์–ด์„œ ์ง„ํ–‰์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. INSERT INTO TEST VALUE(โ€˜XXXโ€™,11,22,33, โ€˜YYYโ€™: ํ…Œ์ด๋ธ” TEST์˜ ์นผ๋Ÿผ์— ๋ฐ์ดํ„ฐ โ€˜XXXโ€™,11,22,33, โ€˜YYYโ€™๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 3 PROC SQL; INSERT INTO TEST VALUES ('๋ฒค์ œ๋งˆ', 30, 6, 72, '๋Ÿฐ๋˜') VALUES ('๊ทธ๋ฆฌ์ฆˆ๋งŒ',25, 5, 67, '๋งˆ๋“œ๋ฆฌ๋“œ') QUIT; NAME AGE HEIGHT WEIGHT ADDR ํ˜ธ๋‚ ๋‘ 27 6 70. ๋ฉ”์‹œ 26 5 65 ๋ฐ”๋ฅด์…€๋กœ ๋ฒค์ œ๋งˆ 30 6 72 ๋Ÿฐ๋˜ ๊ทธ๋ฆฌ์ฆˆ๋งŒ 25 5 67 ๋งˆ๋“œ๋ฆฌ๋“œ ์˜ˆ์ œ 3์˜ ํ…Œ์ด๋ธ”์—์„œ ์ƒˆ๋กœ์šด ํ–‰์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ 3์ฒ˜๋Ÿผ SET ๋ช…๋ น์–ด๋ฅผ ํ™œ์šฉํ•˜์ง€ ์•Š๊ณ  VALUE ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ–‰์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. VAULE ๋ช…๋ น์–ด๋„ SET ๋ช…๋ น์–ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•œ ํ–‰์”ฉ ์ถ”๊ฐ€๋ฅผ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ SET ๋ช…๋ น์–ด์™€ ๋‹ฌ๋ฆฌ ์นผ๋Ÿผ์„ ์ง€์ •ํ•˜์ง€ ์•Š๋Š” ๊ฒŒ ํŠน์ง•์ž…๋‹ˆ๋‹ค. ์นผ๋Ÿผ์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ์นผ๋Ÿผ์˜ ๊ฐœ์ˆ˜์™€ ๋˜‘๊ฐ™์€ ์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. SET ๋ช…๋ น์–ด์—์„œ๋Š” ์นผ๋Ÿผ์„ ์ง€์ •ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ˆ„๋ฝ ๊ฐ’์ด ์žˆ์–ด๋„ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ VALUE ๋ช…๋ น์–ด๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์นผ๋Ÿผ์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ์นผ๋Ÿผ ์ˆ˜๋งŒํผ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ ์˜ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. tip PROC SQL; INSERT INTO TEST VALUES ('ํฌ๊ทธ๋ฐ”', 21, 7, 78) QUIT; โ†’ ํ…Œ์ด๋ธ” TEST์˜ ์นผ๋Ÿผ์€ 5๊ฐœ์ธ๋ฐ VALUE ์•ˆ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ 4๊ฐœ์ด๋ฏ€๋กœ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ง์ ‘ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ๋ช…๋ น์–ด(3) VALUE ๋ช…๋ น์–ด๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์„ ํ•˜๋‚˜ ๋” ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ VALUE ๋ช…๋ น์–ด๋ฅผ ๋งŒ๋“ค์—ˆ์ง€๋งŒ ๋ฐ์ดํ„ฐ์— ๋ˆ„๋ฝ ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค๊ณ  ์„ค๋ช…ํ•œ ๋ฐ”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์• ์ดˆ์— ๋ˆ„๋ฝ ๊ฐ’์ด ์žˆ์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๊ฒฝ์šฐ SET ๋ช…๋ น์–ด๋ฅผ ์“ฐ๋ฉด ๋˜์ง€๋งŒ ๋งค๋ฒˆ ์นผ๋Ÿผ ์ด๋ฆ„์„ ์ ์–ด์•ผ ํ•˜๋Š” ๋“ฑ ๋ถˆํŽธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ ๋ˆ„๋ฝ ๊ฐ’์ด ์žˆ๋”๋ผ๋„ VALUE ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. INSERT INTO TEST (XXX, YYY, ZZZ) VALUE(โ€˜AAAโ€™,โ€˜BBBโ€™,111);: ํ…Œ์ด๋ธ” TEST์˜ XXX, YYY, ZZZ ์นผ๋Ÿผ์— ๋ฐ์ดํ„ฐ โ€˜AAAโ€™,โ€˜BBBโ€™,111 ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 4 PROC SQL; INSERT INTO TEST (NAME, AGE, ADDR) VALUES ('๋ฏ€ํ‚คํƒ€๋ฆฌ์•ˆ', 29, '๋ถ๋Ÿฐ๋˜') QUIT; NAME AGE HEIGHT WEIGHT ADDR ํ˜ธ๋‚ ๋‘ 27 6 70. ๋ฉ”์‹œ 26 5 65 ๋ฐ”๋ฅด์…€๋กœ ๋ฒค์ œ๋งˆ 30 6 72 ๋Ÿฐ๋˜ ๊ทธ๋ฆฌ์ฆˆ๋งŒ 25 5 67 ๋งˆ๋“œ๋ฆฌ๋“œ ๋ฏ€ํ‚คํƒ€๋ฆฌ์•ˆ 29. . ๋ถ๋Ÿฐ๋˜ INSERT INTO TEST ์•„๋ž˜์ชฝ์— ๊ด„ํ˜ธ๋กœ (NAME, AGE, ADDR)์ด ์ง€์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 3๊ฐœ์˜ ์นผ๋Ÿผ์—๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ VALUE ๋ช…๋ น์–ด์—๋Š” 3๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ NAME, AGE, ADDR์„ ๋Œ€์ƒ์œผ๋กœ ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์žˆ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ ์ˆœ์„œ์™€ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์ˆœ์„œ๋Š” ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€๋Š” ๋ˆ„๋ฝ ๊ฐ’์œผ๋กœ, ๋นˆ์นธ์œผ๋กœ ํ‘œํ˜„์ด ๋ฉ๋‹ˆ๋‹ค. 9. SAS MACRO(๋งคํฌ๋กœ) ๋ช…๋ น์–ด ๋งคํฌ๋กœ(Macro)๋Š” ํ•œ๋งˆ๋””๋กœ ์ž๋™ํ™” ํ”„๋กœ๊ทธ๋žจ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ณต์žกํ•œ ์ฝ”๋“œ ๋‹จ์–ด๋ฅผ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ฐ˜๋ณต๋˜๋Š” ์ž‘์—…๋„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งคํฌ๋กœ๋Š” ๋ฌธ์ž ๋ณ€์ˆ˜๋‚˜ ์ˆซ์ž ๋ณ€์ˆ˜๋กœ ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ์†์„ฑ์„ ๋ฐ”๊พธ๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด, ๋งคํฌ๋กœ ๋ณ€์ˆ˜ ์ƒ์„ฑ ์ดํ›„, ๊ฐ’์„ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งคํฌ๋กœ๋Š” SAS์—์„œ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๊ฐ€ ํŠน์ • ํ…์ŠคํŠธ๋ฅผ ์†์‰ฝ๊ฒŒ ๋ณ€๊ฒฝํ•  ๋•Œ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉ์ž ์ •์˜ ๋งคํฌ๋กœ ์นผ๋Ÿผ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ๋Š” ๋ฐ˜๋ณต ์ž‘์—…์„ ์‹œํ–‰ํ•  ๋•Œ์ž…๋‹ˆ๋‹ค. ์ด๋Š” SAS ์ฝ”๋“œ ๋ฐ˜๋ณต ์ƒ์„ฑ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋งคํฌ๋กœ๋Š” ๋ณต์žกํ•œ ์ž‘์—…์„ ์†์‰ฌ์šด ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ํŽธ์˜์„ฑ์„ ๋†’์ด๋Š” ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งคํฌ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ ๋ฌธ์žฅ์˜ ๊ธธ์ด๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์–ด์„œ SAS ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์šฐ์„  ์‚ฌ์šฉ์ž ์ •์˜ ๋งคํฌ๋กœ ์นผ๋Ÿผ ์‚ฌ์šฉ๋ฒ•๋ถ€ํ„ฐ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 9-1. SAS MACRO ์ง์ ‘ ์ž…๋ ฅ ๋ช…๋ น์–ด(LET) LET ๋ช…๋ น์–ด ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ LET ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. LET ๋ช…๋ น์–ด๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ๋งคํฌ๋กœ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋งคํฌ๋กœ ์ƒ์„ฑ ๋ฐฉ๋ฒ•๊ณผ ๋งคํฌ๋กœ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์„ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด %LET XXX=YYY; :๋งคํฌ๋กœ XXX๊ฐ€ YYY๋กœ ์ถœ๋ ฅ๋˜๋Š” ๋งคํฌ๋กœ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค &XXX. : ๋งคํฌ๋กœ XXX๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. %PUT &=XXX. : ๋งคํฌ๋กœ XXX์˜ ์ถœ๋ ฅ๊ฐ’์„ ๋กœ๊ทธ ์ฐฝ์„ ํ†ตํ•ด ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ %LET NAME=์กด; %PUT &=NAME; %PUT &NAME.; ----๋กœ๊ทธ๊ธฐ๋ก ์‹œ์ž‘---- %LET NAME=์กด; %PUT &=NAME; NAME=์กด %PUT &NAME.; ์กด ----๋กœ๊ทธ๊ธฐ๋ก ๋---- ์šฐ์„  %LET(๋งคํฌ๋กœ ์ƒ์„ฑ ๋ฐฉ๋ฒ•)๊ณผ &(ํŠธ๋ฆฌ๊ฑฐ)๊ณผ %PUT(๊ฒฐ๊ด๊ฐ’ ์ถœ๋ ฅ) ํ™œ์šฉ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ๋งคํฌ๋กœ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” %LET ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ โ€˜%โ€™๋ฅผ ๋ถ™์—ฌ์•ผ SAS๋Š” ์ด๋ฅผ ๋งคํฌ๋กœ ์นผ๋Ÿผ ์ƒ์„ฑ์œผ๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. โ€˜%โ€™๋Š” ํ‘œ๋กœ ์ด๋ค„์ง„ ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ SAS์— ๋ช…๋ น์–ด๋ฅผ ์ธ์‹์‹œํ‚ค๊ฑฐ๋‚˜ ๋กœ๊ทธ๊ธฐ๋ก์—๋งŒ ์ถœ๋ ฅ์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. โ€˜%โ€™๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๋ช…๋ น์–ด๋Š” ํ‘œ๋กœ ์ด๋ค„์ง„ ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. %LET์„ ํ†ตํ•ด ๋งคํฌ๋กœ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. %LET NAME=์กด์˜ ๊ฒฝ์šฐ ๋งคํฌ๋กœ ๋ณ€์ˆ˜ NAME์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ โ€˜์กดโ€™์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. 2) '&'๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด %LET ๋ช…๋ น์–ด๋กœ ์ƒ์„ฑ๋œ ๋งคํฌ๋กœ ๋ณ€์ˆซ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋งคํฌ๋กœ ํŠธ๋ฆฌ๊ฑฐ(๋ฐฉ์•„์‡ ๋ผ๋Š” ์˜๋ฏธ)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋งคํฌ๋กœ๋Š” โ€˜&โ€™์„ ํ†ตํ•ด ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ &NAME. ์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ SAS๋Š” ์ด๋ฅผ ๋งคํฌ๋กœ ์นผ๋Ÿผ NAME์„ ๋ถˆ๋Ÿฌ์˜ค๋ผ๋Š” ๋ช…๋ น์–ด๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ โ€˜์กดโ€™์ด๋ผ๋Š” ๊ฐ’์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. 3) %PUT ๋ช…๋ น์–ด์˜ ๊ฒฝ์šฐ ํ‘œ๋กœ ์ด๋ค„์ง„ ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ๋กœ๊ทธ ๊ธฐ๋ก์— ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. %PUT ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋งคํฌ๋กœ ์นผ๋Ÿผ๊ณผ ๊ฒฐ๊ด๊ฐ’์„ ๋กœ๊ทธ ์ฐฝ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. %PUT &=NAME. ์€ ๋งคํฌ๋กœ ์นผ๋Ÿผ NAME์˜ ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  %PUT &NAME. ์€ ๋งคํฌ๋กœ ์นผ๋Ÿผ NAME์˜ ๊ฒฐ๊ด๊ฐ’๋งŒ์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. &=NAME. ์€ ๋งคํฌ๋กœ ์นผ๋Ÿผ๊ณผ ๊ฒฐ๊ด๊ฐ’์„ ๋™์‹œ์— ์ถœ๋ ฅํ•˜๊ณ  &NAME. ์€ ๊ฒฐ๊ด๊ฐ’๋งŒ์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. tip ๋งคํฌ๋กœ ์นผ๋Ÿผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ๋งˆ์ง€๋ง‰์„ โ€˜.โ€™์„ ์ฐ์–ด์ฃผ๋Š” ๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค. โ€˜.โ€™์€ ๋งคํฌ๋กœ ์นผ๋Ÿผ์ด ๋๋‚˜๋Š” ์ง€์ ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜์กด ์Šคํƒํ„ดโ€™์ด๋ผ๋Š” ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ, โ€˜&NAME ์Šคํƒํ„ดโ€™์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด SAS์—์„œ๋Š” NAME ์Šคํƒํ„ด ์ „์ฒด๋ฅผ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋กœ ๋ณด๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งคํฌ๋กœ ์นผ๋Ÿผ ๋งˆ์ง€๋ง‰์— โ€˜.โ€™์„ ๋ถ™์—ฌ์„œ ๋งคํฌ๋กœ ์นผ๋Ÿผ์ด ๋๋‚จ์„ ์•Œ๋ ค์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. โ€˜&NAME. ์Šคํƒํ„ดโ€™์œผ๋กœ ๋งคํฌ๋กœ ์นผ๋Ÿผ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค๋ฉด ์—๋Ÿฌ ์—†์ด ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. SAS๊ฐ€ ๋งคํฌ๋กœ ์นผ๋Ÿผ NAME๊ณผ โ€˜์Šคํƒํ„ดโ€™์ด๋ผ๋Š” ๊ฐ’์„ ๋”ฐ๋กœ ์ธ์‹ํ•ด์„œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฒฐ๊ด๊ฐ’์€ ๋กœ๊ทธ๊ธฐ๋ก์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. LET ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•œ SAS SQL ๋ช…๋ น์–ด ์˜ˆ์ œ %LET NAME='์กด'; PROC SQL; CREATE TABLE TEST AS SELECT NAME, SEX, AGE FROM SASHELP.CLASS WHERE NAME=&NAME. QUIT; Name Sex Age ์กด ๋‚จ 12 ๋งคํฌ๋กœ๋ฅผ SQL์— ์ ์šฉ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  %LET ๋ช…๋ น์–ด๋กœ ๋งคํฌ๋กœ ์นผ๋Ÿผ NAME์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ๋งคํฌ๋กœ๋Š” ๊ฒฐ๊ด๊ฐ’์œผ๋กœ โ€˜์กดโ€™์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ SQL ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๊ณ  WHERE ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด์„œ ์นผ๋Ÿผ NAME=โ€˜์กดโ€™์ธ ํ–‰๋งŒ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. โ€˜ โ€™์„ ๋ถ™์ด๋Š” ์ด์œ ๋Š” SAS์—์„œ ๋ฌธ์ž ์นผ๋Ÿผ์€ โ€˜ โ€™์„ ๋ถ™์—ฌ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ถ™์ด์ง€ ์•Š์„ ๊ฒฝ์šฐ SAS๋Š” ์ด๋ฅผ ๋ฌธ์ž ๋ณ€์ˆซ๊ฐ’์ด ์•„๋‹Œ, ์นผ๋Ÿผ์œผ๋กœ ์ธ์‹์„ ํ•ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ ์กด์€ ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. tip SQL ๋ฌธ์žฅ์—์„œ WHERE NAME=โ€˜&NAME.โ€™์œผ๋กœ ์ž…๋ ฅ์„ ํ•˜๋ฉด ๋˜์ง€ ์•Š์„๊นŒ, ์ƒ๊ฐํ•˜์‹ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ โ€˜ โ€™์•ˆ์— ๋งคํฌ๋กœ ํŠธ๋ฆฌ๊ฑฐ(&)๊ฐ€ ์žˆ๋‹ค๋ฉด SAS๋Š” ์ด๋ฅผ ๋งคํฌ๋กœ ํŠธ๋ฆฌ๊ฑฐ๋กœ ์ธ์‹ํ•˜์ง€ ์•Š๊ณ  ๋‹จ์ˆœํ•œ ํ…์ŠคํŠธ&๋กœ ์ธ์‹์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋งคํฌ๋กœ๊ฐ€ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฒ˜์Œ ๋งคํฌ๋กœ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ โ€˜ โ€™๋ฅผ ๋ถ™์—ฌ์„œ ์ƒ์„ฑํ•˜๋Š” ๊ฒŒ ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. 9-2. SAS MACRO ์—ฐ๋™ ์ž…๋ ฅ ๋ช…๋ น์–ด(CALL SYMPUT) %LET ๋ช…๋ น์–ด๊ฐ€ ์ง์ ‘ ๋ช…๋ น์–ด๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด CALL SYMPUT์€ ์ฃผ๋กœ ํ…Œ์ด๋ธ”์„ ํ™œ์šฉํ•ด์„œ ์ž๋™์œผ๋กœ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ํ…Œ์ด๋ธ”์„ ํ™œ์šฉํ•˜์ง€ ์•Š๊ณ  ์ง์ ‘ ์ž…๋ ฅํ•ด์„œ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์šฐ์„  ๊ฐ„๋‹จํ•œ CALL SYMPUT ๋ช…๋ น์–ด ์‚ฌ์šฉ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์ง์ ‘ ์ž…๋ ฅ ๋ช…๋ น์–ด CALL SYMPUT(โ€˜XXXโ€™,โ€˜YYYโ€™): ๋งคํฌ๋กœ ๋ณ€์ˆ˜ XXX๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•˜์—ฌ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋งคํฌ๋กœ ๋ณ€์ˆ˜ XXX์˜ ๊ฐ’์€ YYY๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 1 DATA _NULL_; /*๋นˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค(ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค)*/ CALL SYMPUT('VALUE','๋‚˜์ด'); /*๋งคํฌ๋กœ ๋ณ€์ˆ˜ 'VALUE'์„ ๋งŒ๋“ค๊ณ  ์ด ๊ฐ’์„ ๋‚˜์ด๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค*/ RUN; /*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค*/ %PUT &=VALUE; /*๋งคํฌ๋กœ ๋ณ€์ˆ˜ VALUE์˜ ๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค*/ %PUT VALUE=&VALUE. /*๋งคํฌ๋กœ ๋ณ€์ˆ˜ VALUE์˜ ๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๋ช…๋ น์–ด์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค*/ ----๋กœ๊ทธ๊ธฐ๋ก---- %PUT &=VALUE; /๋งคํฌ๋กœ ๋ณ€์ˆ˜ VALUE์˜ ๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค/ VALUE=๋‚˜์ด %PUT VALUE=&VALUE. VALUE=๋‚˜์ด ----๋กœ๊ทธ๊ธฐ๋ก---- CALL SYMPUT ๋ช…๋ น์–ด๋Š” DATA ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  DATA ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  DATA ๋ช…๋ น์–ด๊ฐ€ ๋๋‚˜๋ฉด CALL SYMPUT(โ€˜๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ช…โ€™,โ€˜๊ฐ’โ€™)์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. SYMPUT ์•ˆ์—์„œ ์ฒซ ๋ฒˆ์งธ ์ž…๋ ฅํ•œ ๊ฐ’์€ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ช…์ด ๋˜๊ณ  ๋‘ ๋ฒˆ์งธ ์ž…๋ ฅํ•œ ๊ฐ’์€ ๋งคํฌ๋กœ์˜ ๊ฒฐ๊ด๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ ๋ณด์‹  ๊ฑฐ์™€ ๊ฐ™์ด SYMPUT(โ€˜VALUEโ€™,โ€˜๋‚˜์ดโ€™)๋ผ๊ณ  ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ, VALUE๋Š” ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ช…์ด, ๋‚˜์ด๋Š” ๊ทธ ๋งคํฌ๋กœ์˜ ๊ฒฐ๊ด๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ์ง์ ‘ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ, SYMPUT ๋‚ด๋ถ€์˜ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ช…๊ณผ ๊ฐ’์—๋Š” ๋ฐ˜๋“œ์‹œ โ€˜ โ€™๋ฅผ ์”Œ์›Œ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด CALL SYMPUT(VALUE, ๋‚˜์ด);์ฒ˜๋Ÿผ โ€˜ โ€™๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ, SAS๋Š” VALUE์™€ ๋‚˜์ด๋ฅผ ์นผ๋Ÿผ์œผ๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค(์นผ๋Ÿผ์œผ๋กœ ์ธ์‹ํ•˜์—ฌ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค). tip _NULL_์ด๋ž€, DATA ๋ช…๋ น์–ด์—์„œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. SAS ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋‹ค ๋ณด๋ฉด ๊ตณ์ด ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. SYMPUT ๋ช…๋ น์–ด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ, SAS๋Š” ๋‹จ๋… ๋ฌธ์žฅ๋งŒ์œผ๋กœ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ DATA ๋ช…๋ น์–ด๋ฅผ ํ•„์ˆ˜์ ์œผ๋กœ ํ•จ๊ป˜ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋Š” ๊ตณ์ด ํ…Œ์ด๋ธ”๋กœ ์ƒ์„ฑํ•ด์„œ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ…Œ์ด๋ธ”์€ ์ƒ์„ฑํ•˜์ง€ ์•Š๋˜ ๋งคํฌ๋กœ๋งŒ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ DATA ๋ช…๋ น์–ด ๋‹จ๊ณ„์—์„œ _NULL_์„ ์ ์–ด์ค๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด SAS๋Š” DATA ๋ช…๋ น์–ด ์ดํ›„์˜ ๋ช…๋ น์–ด๋งŒ ์ˆ˜ํ–‰ํ•˜๊ณ  ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 2) ์—ฐ๋™ ์ž…๋ ฅ ๋ช…๋ น์–ด CALL SYMPUT(XXX, YYY): ๋งคํฌ๋กœ ๋ณ€์ˆ˜ XXX๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์—์„œ ์—ฐ๋™ํ•œ ๊ฒฐ๊ด๊ฐ’์„ ๋งคํฌ๋กœ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋งคํฌ๋กœ ๋ณ€์ˆ˜ XXX๋Š” ์นผ๋Ÿผ XXX์˜ ๊ฐ ๊ฐ’์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ด๊ฐ’์€ ์นผ๋Ÿผ XXX์˜ ๊ฐ ๊ฐ’๊ณผ ๋™์ผํ•œ ํ–‰์— ์žˆ๋Š” ์นผ๋Ÿผ YYY์˜ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ๋ฒˆ์— ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šฐ์‹œ๋‹ค๋ฉด ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ 2 DATA _NULL_; /*๋นˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค(ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค)*/ SET SASHELP.CLASS; /*ํ…Œ์ด๋ธ” SASHELP.CLASS์„ ๋Œ€์ƒ์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.*/ CALL SYMPUT(NAME, AGE); /*๋งคํฌ๋กœ ๋ณ€์ˆ˜ ๋Œ€์ƒ ์นผ๋Ÿผ์€ NAME์ž…๋‹ˆ๋‹ค. ์นผ๋Ÿผ NAME์˜ ๊ฐ ๊ฐ’์ด ๋งคํฌ๋กœ ๋ณ€์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งคํฌ๋กœ ๋ณ€์ˆ˜์™€ ๊ฐ™์€ ํ–‰์˜ ์นผ๋Ÿผ AGE ๊ฐ’์„ ๋งคํฌ๋กœ ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค*/ RUN; /*SAS ๋ช…๋ น์–ด๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค*/ %PUT &=์•Œํ”„๋ ˆ๋“œ; /*๋งคํฌ๋กœ ๋ณ€์ˆ˜ ์•Œํ”„๋ ˆ๋“œ์˜ ๊ฒฐ๊ด๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค*/ %PUT &=์กด; /*๋งคํฌ๋กœ ๋ณ€์ˆ˜ ์กด์˜ ๊ฒฐ๊ด๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค*/ -----๋กœ ๊ทธ ๊ฒฐ๊ด๊ฐ’ %PUT &=์•Œํ”„๋ ˆ๋“œ; ์•Œํ”„๋ ˆ๋“œ= 14 %PUT &=์กด; ์กด= 12 -----๋กœ ๊ทธ ๊ฒฐ๊ด๊ฐ’ ์šฐ์„  SASHELP.CLASS ํ…Œ์ด๋ธ”์„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. NAME SEX AGE HEIGHT WEIGHT ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์ž๋„ท์—ฌ 15 62.5 112.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กด ๋‚จ 12 59 99.5 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ฃผ๋””์—ฌ 14 64.3 90 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 CALL SYMPUT ๋ช…๋ น์–ด(CALL SYMPUT(NAME, AGE)๋ฅผ ํ†ตํ•ด ์นผ๋Ÿผ NAME์˜ ๊ฐ ๊ฐ’์ด ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ๋œ ๋งคํฌ๋กœ ๋ณ€์ˆ˜์— ๋งค์นญ๋˜๋Š” ์นผ๋Ÿผ AGE์˜ ๊ฐ’์ด ๊ทธ ๊ฒฐ๊ด๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์นผ๋Ÿผ NAME์˜ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ์•Œํ”„๋ ˆ๋“œ๊ฐ€ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋กœ ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•Œํ”„๋ ˆ๋“œ์˜ ์นผ๋Ÿผ AGE์— ์žˆ๋Š” ๊ฐ’์ธ 14๊ฐ€ ๋งคํฌ๋กœ ๊ฒฐ๊ด๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ โ€œ&์•Œํ”„๋ ˆ๋“œ.โ€๋ผ๊ณ  ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ 14๋ผ๋Š” ๊ฐ’์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ์นผ๋Ÿผ NAME์— ์žˆ๋Š” ์•จ๋ฆฌ์Šค๊ฐ€ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋กœ ์ƒ์„ฑ์ด ๋˜๊ณ  โ€œ&์•จ๋ฆฌ์Šค.โ€์˜ ๊ฒฐ๊ด๊ฐ’์€ 13์ด ๋ฉ๋‹ˆ๋‹ค. ์นผ๋Ÿผ NAME์— ์žˆ๋Š” ๊ฐ ๋ณ€์ˆ˜๊ฐ€ ๋ชจ๋‘ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋กœ ์ƒ์„ฑ์ด ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ CALL SYMPUT ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋Š” 19๊ฐœ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. tip ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•  ๋•Œ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์นผ๋Ÿผ NAME์˜ ์กด๊ณผ ๊ฐ™์€ AGE(๋‚˜์ด)๋ฅผ ๊ฐ€์ง„ ํ–‰๋งŒ์„ ์ถ”์ถœํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. PROC SQL; CREATE TABLE SSS AS SELECT * FROM SASHELP.CLASS WHERE AGE=&์กด. QUIT; ์œ„์˜ ์ฝ”๋“œ์—์„œ WHERE AGE=&์กด. ์€ WHERE AGE=12์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค. WHERE AGE=&์กด.์—์„œ ์ด๋ฆ„๋งŒ ๋ฐ”๊ฟ”์ฃผ๋ฉด ์ด๋ฆ„์— ๋”ฐ๋ฅธ ๋‚˜์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 9-3. SAS MACRO ์—ฐ๋™ ์ž…๋ ฅ ๋ช…๋ น์–ด(PROC SQL์˜ INTO) (์ „์ž์ฑ… ๋‚ด์šฉ) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์˜ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋งํฌ) SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ์ „์ž์ฑ… ๊ตฌ๋งค 9-4. SAS MACRO ํ™œ์šฉํ•œ ๋ฌธ์žฅ์˜ ๋งคํฌ๋กœํ™”(%MACRO) ์ง€๊ธˆ๊นŒ์ง€๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ ํ•˜๋‚˜์˜ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ๋Š” ํ•˜๋‚˜์˜ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ฅผ ๋„˜์–ด์„œ ์ „์ฒด ๋ช…๋ น๋ฌธ์„ ๋งคํฌ๋กœํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ช…๋ น๋ฌธ์„ ๋งคํฌ๋กœํ™” ์‹œํ‚ค๋ฉด ์ „์ฒด ๋ช…๋ น๋ฌธ์„ ๋ฐ˜๋ณตํ•ด์„œ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์œผ๋กœ ๋Œ€์ฒด๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ฝ”๋“œ๊ฐ€ ์งง์•„์ง€๊ณ  ์—ฐ์‚ฐ ์†๋„๋„ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ง€๊ธˆ๋ถ€ํ„ฐ ์ „์ฒด ๋ช…๋ น์–ด๋ฅผ ๋งคํฌ๋กœํ™” ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ทธ ํ™œ์šฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. tip ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ ๋งคํฌ๋กœํ™”ํ•  ๊ฒฝ์šฐ ์ด๋ฅผ ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ฌธ์žฅ์„ ๋งคํฌ๋กœํ™”ํ•  ๊ฒฝ์šฐ ์ด๋ฅผ ๋งคํฌ๋กœ๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. MACRO ์ƒ์„ฑ ๋ช…๋ น์–ด 1 %MACRO XXX;: ๋งคํฌ๋กœ XXX๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ „์ฒด ๋ช…๋ น์–ด๋ฅผ ๋งคํฌ๋กœํ™” ์‹œํ‚ต๋‹ˆ๋‹ค. --DATA ๋˜๋Š” SQL ๋ช…๋ น์–ด ์ž…๋ ฅ-- %MEND;: ๋งคํฌ๋กœ XXX ์ƒ์„ฑ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. %XXX;: ๋งคํฌ๋กœ XXX๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 1 %MACRO TEST; /*๋งคํฌ๋กœ TEST๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค*/ PROC SQL; CREATE TABLE TEST_1 AS SELECT * FROM SASHELP.CLASS QUIT; /*๋งคํฌ๋กœ TEST์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค*/ %MEND; /*๋งคํฌ๋กœ TEST ์ƒ์„ฑ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค*/ %TEST; /*๋งคํฌ๋กœ TEST๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค*/ Name Sex Age Height Weight ์•Œํ”„๋ ˆ๋“œ ๋‚จ 14 69 112.5 ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์บ๋Ÿด์—ฌ 14 62.8 102.5 ํ—จ๋ฆฌ ๋‚จ 14 63.5 102.5 ์ œ์ž„์Šค ๋‚จ 12 57.3 83 ์ œ์ธ์—ฌ 12 59.8 84.5 ์ž๋„ท์—ฌ 15 62.5 112.5 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์กด ๋‚จ 12 59 99.5 ์กฐ์ด์Šค์—ฌ 11 51.3 50.5 ์ฃผ๋””์—ฌ 14 64.3 90 ๋ฃจ์ด์Šค์—ฌ 12 56.3 77 ๋ฉ”๋ฆฌ์—ฌ 15 66.5 112 ํ•„๋ฆฝ ๋‚จ 16 72 150 ๋กœ๋ฒ„ํŠธ ๋‚จ 12 64.8 128 ๋กœ๋‚ ๋“œ ๋‚จ 15 67 133 ํ† ๋งˆ์Šค ๋‚จ 11 57.5 85 ์œŒ๋ฆฌ์—„ ๋‚จ 15 66.5 112 ๊ฐ„๋‹จํ•œ ๋งคํฌ๋กœ TEST๋ฅผ ์ƒ์„ฑํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. %MACRO TEST;์˜ ์•„๋ž˜ ์ค„๋ถ€ํ„ฐ ๋ช…๋ น์–ด ์˜ˆ์ œ์— ์ ํžŒ ๋Œ€๋กœ ์ž…๋ ฅ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด %MEND๊ฐ€ ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ๋งคํฌ๋กœ ๋ช…๋ น์–ด๋กœ ์ธ์‹์„ ํ•ฉ๋‹ˆ๋‹ค. %MACRO TEST; ์ดํ›„๋ถ€ํ„ฐ %MEND;๊นŒ์ง€๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋งคํฌ๋กœ ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋งคํฌ๋กœ๋ฅผ ์‹œํ–‰ํ•˜๋Š” ๊ฑด %TEST์ž…๋‹ˆ๋‹ค. SAS์— %TEST;๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋งคํฌ๋กœ TEST๋ฅผ ์ฐพ์•„์„œ ์‹œํ–‰์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. PROC SQL;๋ถ€ํ„ฐ QUIT;๊นŒ์ง€์˜ ๋ช…๋ น์–ด๋ฅผ ํ•œ ์ค„์˜ ๋ช…๋ น์–ด(%TEST)์— ๋‹ด์€ ์…ˆ์ž…๋‹ˆ๋‹ค. MACRO ์•ˆ์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๋ช…๋ น์–ด์˜ ๊ธธ์ด๋Š” ์•„๋ฌด๋ฆฌ ๊ธธ์–ด๋„ ๊ด€๊ณ„๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ธด ๋ฌธ์žฅ์˜ ๋ช…๋ น์–ด๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์‹œํ–‰ํ•  ๋•Œ ์š”๊ธดํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. tip ์—ฌ๊ธฐ์„œ ๋ณด์‹œ๋ฉด ๋งคํฌ๋กœ ๋ช…๋ น์–ด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” โ€˜%โ€™๊ฐ€ ๋“ค์–ด๊ฐ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งคํฌ๋กœ๋Š” %๋ฅผ ํ†ตํ•ด์„œ ์ƒ์„ฑ๋˜๊ณ , ๋งคํฌ๋กœ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋„ %๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค(๋งคํฌ๋กœ ๋ณ€์ˆ˜๋Š” &์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค). ๋งคํฌ๋กœ๋Š” %์™€ ๋ฐ€์ ‘ํ•œ ์—ฐ๊ด€์ด ์žˆ์Œ์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. ๋งคํฌ๋กœ ์ƒ์„ฑ ๋ช…๋ น์–ด 2 ์ด๋ฒˆ์—๋Š” ํŠน์ • ์กฐ๊ฑด์„ ์ค€ ๋งคํฌ๋กœ ๋ช…๋ น์–ด ์ƒ์„ฑ์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์ˆœ ๋งคํฌ๋กœ ์ƒ์„ฑ๋ณด๋‹ค ์กฐ๊ฑด์„ ์ค€ ๋ช…๋ น์–ด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋งคํฌ๋กœ ๋ฌธ์žฅ์—์„œ ํŠน์ • ์กฐ๊ฑด๋งŒ ๋ฐ”๊ฟ”๊ฐ€๋ฉด์„œ ์ „์ฒด ๋ช…๋ น์–ด๋ฅผ ์‹œํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. %MACRO XXX(YYY);: ๋งคํฌ๋กœ XXX๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ „์ฒด ๋ช…๋ น์–ด๋ฅผ ๋งคํฌ๋กœํ™” ์‹œํ‚ต๋‹ˆ๋‹ค. ๋งคํฌ๋กœ ๋ณ€์ˆ˜ YYY๋ฅผ ํ•จ๊ป˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ex) WHERE AAA=&YYY. %MEND;: ๋งคํฌ๋กœ XXX ์ƒ์„ฑ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. %XXX(111);: ๋งคํฌ๋กœ XXX ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งคํฌ๋กœ ๋ณ€์ˆ˜ YYY์˜ ๊ฐ’์€ 111๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 2 %MACRO TEST2(CUT); /*๋งคํฌ๋กœ TEST2๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  CUT์ด๋ผ๋Š” ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋„ ํ•จ๊ป˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค*/ PROC SQL; CREATE TABLE TEST_2 AS SELECT * FROM SASHELP.CLASS WHERE AGE=&CUT. /*์นผ๋Ÿผ AGE์˜ ๊ฐ’์ด &CUT.์˜ ๊ฐ’๊ณผ ๋™์ผํ•œ ํ–‰๋งŒ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค*/ QUIT; /*๋งคํฌ๋กœ TEST2์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค*/ %MEND; /*๋งคํฌ๋กœ TEST2 ์ƒ์„ฑ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค*/ %TEST2(13); /*๋งคํฌ๋กœ TEST2๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋งคํฌ๋กœ ๋ณ€์ˆ˜ CUT์˜ ๊ฒฐ๊ด๊ฐ’์€ 13์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค*/ Name Sex Age Height Weight ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์ œํ”„๋ฆฌ ๋‚จ 13 62.5 84 ์•ž์„  ๋งคํฌ๋กœ์™€ ๋‹ค๋ฅธ ์ ์ด ์žˆ๋‹ค๋ฉด MACRO TEST2(CUT); ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. CUT์€ ๋งคํฌ๋กœ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. CUT์„ ํ™œ์šฉํ•ด์„œ ๋งคํฌ๋กœ ๋ช…๋ น์–ด ๋‚ด๋ถ€์— ์กฐ๊ฑด์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ 2์ฒ˜๋Ÿผ WHERE AGE=&CUT. ๋ถ€๋ถ„์—์„œ ๋งคํฌ๋กœ ๋ณ€์ˆ˜ CUT์„ ์ž…๋ ฅํ•˜๋ฉด, %TEST2(13);์œผ๋กœ ๋งคํฌ๋กœ๋ฅผ ์‹œํ–‰ํ•  ๋•Œ SAS๋Š” CUT์„ %LET CUT=13๊ณผ ๋™์ผํ•˜๊ฒŒ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ WHERE AGE=13์œผ๋กœ ์ธ์‹ํ•˜๊ฒŒ ๋˜๊ณ  ์นผ๋Ÿผ AGE๊ฐ€ 13์ธ ํ–‰๋งŒ ๊ฒฐ๊ณผ๋กœ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์€ ๋ณต์žกํ•œ ๋งคํฌ๋กœ ๋ช…๋ น์–ด์—์„œ ํŠน์ • ๋ถ€๋ถ„๋งŒ ๋ฐ”๊ฟ€ ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•  ๋•Œ ๋‚ด๋ถ€ ๋ช…๋ น์–ด๋ฅผ ์ผ์ผ์ด ์†๋ณผ ํ•„์š” ์—†์ด ๋งคํฌ๋กœ ์‹œํ–‰ ๋‹จ๊ณ„์—์„œ๋งŒ CUT ๊ฐ’์„ ์ˆ˜์ •ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. CUT ๊ฐ’์„ ๋ฐ”๊ฟ”๊ฐ€๋ฉด์„œ ์‹œํ–‰ํ•ด ๋ณด๋ฉด ๊ทธ ํšจ๊ณผ๋ฅผ ๋” ์ฒด๊ฐํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งคํฌ๋กœ ์ƒ์„ฑ ๋ช…๋ น์–ด 3 ์ด๋ฒˆ์—๋Š” ๋‘ ๊ฐœ์˜ ํŠน์ • ์กฐ๊ฑด์„ ์ค€ ๋งคํฌ๋กœ ๋ช…๋ น์–ด ์ƒ์„ฑ์„ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ๋Š” ํ•˜๋‚˜์˜ ์กฐ๊ฑด์„ ์ค€ ๋งคํฌ๋กœ ๋ช…๋ น์–ด๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค๋ฉด, ์ด์ œ๋Š” ๋‘ ๊ฐœ์˜ ์กฐ๊ฑด์„ ๊ฐ–๋Š” ๋งคํฌ๋กœ ์ƒ์„ฑ ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. %MACRO XXX(YYY, ZZZ);: ๋งคํฌ๋กœ XXX๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ „์ฒด ๋ช…๋ น์–ด๋ฅผ ๋งคํฌ๋กœํ™” ์‹œํ‚ต๋‹ˆ๋‹ค. ๋งคํฌ๋กœ ๋ณ€์ˆ˜ YYY์™€ ZZZ๋ฅผ ํ•จ๊ป˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ex) WHERE AAA=&YYY. AND BBB=&ZZZ. %MEND;: ๋งคํฌ๋กœ XXX ์ƒ์„ฑ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. %XXX(111,222);: ๋งคํฌ๋กœ XXX๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งคํฌ๋กœ ๋ณ€์ˆ˜ YYY์˜ ๊ฐ’์€ 111๋กœ ์ง€์ •ํ•˜๊ณ  ZZZ์˜ ๊ฐ’์€ 222๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ 3 %MACRO TEST3(CUT, CUT2); /*๋งคํฌ๋กœ TEST3๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  CUT๊ณผ CUT2๋ผ๋Š” ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋„ ํ•จ๊ป˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค*/ PROC SQL; CREATE TABLE TEST_3 AS SELECT * FROM SASHELP.CLASS WHERE AGE=&CUT. /*์นผ๋Ÿผ AGE์˜ ๊ฐ’์ด &CUT. ๊ณผ ๋™์ผํ•œ ํ–‰์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค*/ AND SEX=&CUT2./*์นผ๋Ÿผ AGE์˜ ๊ฐ’์ด &CUT2. ๊ณผ ๋™์ผํ•œ ํ–‰๋งŒ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค*/ QUIT; /*๋งคํฌ๋กœ TEST3์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค*/ %MEND; /*๋งคํฌ๋กœ TEST3 ์ƒ์„ฑ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค*/ %TEST3(13, '์—ฌ'); /*๋งคํฌ๋กœ TEST3๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค*/ Name Sex Age Height Weight ์•จ๋ฆฌ์Šค์—ฌ 13 56.5 84 ๋ฐ”๋ฐ”๋ผ์—ฌ 13 65.3 98 ์ด๋ฒˆ์—๋Š” ๋‘ ๊ฐœ์˜ ์กฐ๊ฑด์ด ๋ถ™์€ ๋งคํฌ๋กœ ๋ช…๋ น์–ด๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„  ์˜ˆ์ œ 2๊ฐ€ ํ•˜๋‚˜์˜ ์กฐ๊ฑด์ด ์žˆ๋Š” ๋งคํฌ๋กœ ๋ช…๋ น์–ด๋ผ๋ฉด, ์˜ˆ์ œ 3์€ ๋‘ ๊ฐœ์˜ ์กฐ๊ฑด์ด ์žˆ๋Š” ๋งคํฌ๋กœ ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์›๋ฆฌ๋Š” ์•ž์„  ์˜ˆ์ œ 2์˜ ๋ฐฉ์‹๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋งคํฌ๋กœ ๋ณ€์ˆ˜ CUT๊ณผ CUT2๋ฅผ %MACRO TEST3(CUT, CUT2);๋ฅผ ํ†ตํ•ด ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚ด๋ถ€ ๋ช…๋ น์–ด์— WHERE AGE=&CUT. AND SEX=&CUT2.๋กœ ์ž…๋ ฅ์„ ํ•ด๋‘ก๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋ฅผ ์‹œํ–‰ํ•  ๋•Œ %TEST3(13, โ€˜์—ฌโ€™);๋กœ ์ž…๋ ฅ์„ ํ•จ์œผ๋กœ์จ %LET CUT=13; %LET CUT2=โ€˜์—ฌโ€™ ์™€ ๋™์ผํ•œ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ ์…ˆ์ด ๋ฉ๋‹ˆ๋‹ค. ๋งคํฌ๋กœ๋ฅผ ์‹œํ–‰ํ•˜๋ฉด SAS๋Š” ์ด ๋ถ€๋ถ„์„ ์ž๋™์œผ๋กœ ์ž…๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋งคํฌ๋กœ %TEST3(13, โ€˜์—ฌโ€™)๋Š” AGE๊ฐ€ 13์ด๊ณ  SEX๊ฐ€ โ€˜์—ฌโ€™์ธ ํ–‰๋งŒ ์ถ”์ถœ์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. tip %MACRO XXX(AAA, BBB,.....,ZZZ); ๋“ฑ์˜ ๋ฐฉ์‹์œผ๋กœ ์กฐ๊ฑด์„ ์ฃผ๋Š” ๋งคํฌ๋กœ ๋ณ€์ˆ˜๋Š” ํ•„์š”ํ•œ ๋งŒํผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 9-5. SAS MACRO ํ™œ์šฉํ•œ ๋งคํฌ๋กœ์˜ ์ž๋™ํ™”(%DO) (์ „์ž์ฑ… ๋‚ด์šฉ) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๋งํฌ์˜ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋งํฌ) SAS๋กœ ํ•˜๋Š” ๊ธฐ์ดˆ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ํ•ธ๋“ค๋ง(Data handling) ์ „์ž์ฑ… ๊ตฌ๋งค<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ๊ณตํ•™์ž๋ฅผ ์œ„ํ•œ Python ### ๋ณธ๋ฌธ: ํŒŒ์ด์ฌ ๊ด€๋ จ ์ •๋ฆฌ ๋ฌธ์„œ. ์•„๋ž˜๋Š” ์ฐธ๊ณ  ์„œ์  ์ ํ”„ ํˆฌ ํŒŒ์ด์ฌ ํŒŒ์ด์ฌ์œผ๋กœ ๋ฐฐ์šฐ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŠธ๋ ˆ์ด๋”ฉ (4์‡„) Python Data Science Handbook 1. ํ™˜๊ฒฝ์„ค์ • Python์€ ๊ณต์‹ ์‚ฌ์ดํŠธ์ธ python.org์—์„œ ๋‚ด๋ ค๋ฐ›์•„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ณต์‹๋ฐฐํฌ๋ณธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ถ”๊ฐ€๋กœ ์—ฌ๋Ÿฌ ํŒจํ‚ค์ง€(๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์˜๋ฏธ)๋ฅผ ๋ณ„๋„๋กœ ์ธ์Šคํ†จํ•ด์•ผ ํ•œ๋‹ค. ๋ณดํ†ต์€ ํ•„์ˆ˜ ํŒจํ‚ค์ง€๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” Anaconda ๋ฐฐํฌ๋ณธ์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ตœ๊ทผ Anaconda ๋ฐฐํฌ๋ณธ์ด ๊ฐœ์ธ์—๊ฒŒ๋Š” ๋ฌด๋ฃŒ์ด์ง€๋งŒ ์ผ์ • ๊ทœ๋ชจ ์ด์ƒ์˜ ํšŒ์‚ฌ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์œ ๋ฃŒํ™”๋˜์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๊ธฐ์—์„œ๋Š” ๊ณต์‹๋ฐฐํฌ๋ณธ์œผ๋กœ ์ž‘์—…ํ•œ๋‹ค. Python์€ ๋ฒ„์ „ ๋ฒˆํ˜ธ 2.x์™€ 3.x์˜ ๋‘ ๊ฐ€์ง€ ๋ฒ„์ „์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค. ์ด๋ฅผ ๊ฐ๊ฐ Python 2, Python 3๋กœ ๋ถ€๋ฅด๋Š”๋ฐ ์ผ๋ถ€ ์–ธ์–ด ํ‘œ์ค€์ด ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„์ด ์žˆ์–ด ์†Œ์Šค๊ฐ€ ํ˜ธํ™˜๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” Python 3๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. Anaconda ๋ฐฐํฌ๋ณธ์ด๋ผ ํ•˜ ๋”ํ•˜๊ณ  ๋ชจ๋“  ํŒจํ‚ค์ง€๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ ํ•„์š”ํ•œ ์ถ”๊ฐ€ ํŒจํ‚ค์ง€๋Š” ์ฐพ์•„์„œ ์ธ์Šคํ†จํ•ด์•ผ ํ•œ๋‹ค. ์—๋””ํŒ…, ๋””๋ฒ„๊น…, ์‹คํ–‰ ๋“ฑ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ง€์›ํ•˜๋Š” ํ†ตํ•ฉ๊ฐœ๋ฐœ ํ™˜๊ฒฝ(IDE)์˜ ์„ ํƒ์ด ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ์ด๋Š” ์ทจํ–ฅ ๋ฌธ์ œ์ด๋‚˜ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํ†ตํ•ฉ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์€ Spyder, PyCharm, Visual Studio, VS Code ๋“ฑ์ด ์žˆ๋‹ค. 1.1 Python ์„ค์น˜์™€ ๊ตฌ๋™ 1.1.1 ์„ค์น˜ Python์€ www.python.org์—์„œ ๊ด€๋ฆฌํ•˜๋ฉฐ ํ‘œ์ค€ ๋ฐฐํฌ๋ณธ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค. ์‹ค์ œ Python ์‚ฌ์šฉ ์‹œ ํ•„์š”ํ•œ ๋‹ค์–‘ํ•œ ํŒจํ‚ค์ง€(๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์˜๋ฏธ)๋ฅผ ์ผ์ผ์ด ์ธ์Šคํ†จํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋ถ€๋ถ„ ํ•„์ˆ˜ ํŒจํ‚ค์ง€์™€ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” Anaconda ๋ฐฐํฌ๋ณธ์ด ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ตœ๊ทผ Anaconda๊ฐ€ ํšŒ์‚ฌ์—์„œ ์‚ฌ์šฉ ์‹œ ๋ผ์ด์„ ์Šค ์กฐ๊ฑด์ด ์ƒ์šฉ์œผ๋กœ ๋ณ€๊ฒฝ๋จ์— ๋”ฐ๋ผ ์—ฌ๊ธฐ์—์„œ๋Š” ํ‘œ์ค€๋ฐฐํฌ๋ณธ์„ ๊ธฐ์ค€์œผ๋กœ ๊ธฐ์ˆ ํ•˜์˜€๋‹ค. Python ํ‘œ์ค€ ๋ฐฐํฌ๋ณธ ์•ˆ์ •ํ™”๊ฐ€ ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์žฅ ์ตœ์‹  ๋ฒ„์ „์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ฆ‰, ์ตœ์‹  ๋ฒ„์ „์˜ ๋ฐ”๋กœ ์ด์ „ ๋ฒ„์ „์„ ์„ค์น˜ํ•œ๋‹ค ํ˜„์žฌ(2023.19.11)๋Š” Python 3.12๊ฐ€ ์ตœ์‹ ์ด๋‹ค. ๋”ฐ๋ผ์„œ 3.11๋ฒ„์ „ ์ค‘ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฆด๋ฆฌ์Šค๋ฅผ ๊ณ ๋ฅธ๋‹ค. 64๋น„ํŠธ๋ฅผ ๊ณ ๋ คํ•˜๋ฉด python-3.11.6-amd64.exe๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ์„ค์น˜ํ•œ๋‹ค. ์‹คํ–‰ํ•œ ํ›„ ์ฒซ ํ™”๋ฉด์—์„œ Customize installation ์„ ํƒํ•œ ํ›„ ๋‘ ๋ฒˆ์งธ ํ™”๋ฉด Next ๋งˆ์ง€๋ง‰ ํ™”๋ฉด์—์„œ ๋ชจ๋‘ check on(์ด ๊ฒฝ์šฐ ๋ชจ๋“  user ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ณ , ๋””ํดํŠธ ์„ค์น˜ ์œ„์น˜๋Š” C:\Program Files\Python311, ํ™˜๊ฒฝ ๋ณ€์ˆ˜ PATH ๋“ฑ๋ก ๋“ฑ์˜ ํŠน์ง•์„ ๊ฐ–๋Š”๋‹ค) ์ด์ „ ๋ฒ„์ „ ์ œ๊ฑฐ ์‹œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ PATH๊ฐ€ ์ž๋™์œผ๋กœ ์‚ญ์ œ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด ๊ฒฝ์šฐ ์ง์ ‘ ์กฐ์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ฃผ์˜ ๋ชจ๋“  user ์‚ฌ์šฉ ๊ฐ€๋Šฅ์— ON ํ•œ ๊ฒฝ์šฐ ๋””ํดํŠธ ์„ค์น˜ ์œ„์น˜๋Š” C:\Program Files\Python311์ด๋‚˜ ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์œผ๋กœ pip๋กœ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์•ผ ๊ทธ ํ•˜์œ„์˜ scripts, site-packages์— ์„ค์น˜๋œ๋‹ค. ๊ถŒ๋ฆฌ์ž ๊ถŒํ•œ์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” ์ถ”๊ฐ€ ์„ค์น˜ํ•˜๋Š” ํŒจํ‚ค์ง€๋Š” c:/users/์‚ฌ์šฉ์ž ์•„์ด๋””/appdata/roaming/python/python311ํ•˜์œ„์— ์„ค์น˜๋˜๋จ(๊ทธ ํ•˜์œ„์— scripts, site-packages ๋“ฑ).<NAME> scripts๊ฐ€ PATH์— ์žกํžˆ์ง€ ์•Š์œผ๋ฉฐ, ์‚ญ์ œ ์‹œ ์ˆ˜๋™ ์‚ญ์ œ๊ฐ€ ํ•„์š”ํ•จ ํ•„์ˆ˜ ํŒจํ‚ค์ง€ ์„ค์น˜ ์ปค๋งจํŠธํ”„๋žŒํ”„ํŠธ๋ฅผ ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค. > pip install wheel > pip install numpy > pip install scipy > pip install matplotlib > pip install pandas > pip install spyder > pip install jupyter ์ฐธ๊ณ  pip freeze > requirements.txt ๋‹ด์— ๊น” ๋•Œ ๋‹ค์Œ์œผ๋กœ ์ธ์Šคํ†จ pip install wheel pip install -r requirements.txt ์ถ”๊ฐ€ Python ํŒจํ‚ค์ง€ Anaconda์—๋Š” ๋Œ€๋ถ€๋ถ„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํŒจ์ง€์ง€๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ์ด์™ธ์˜ ํŒจํ‚ค์ง€๋ฅผ ๊น”๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ€๋Šฅํ•œ pip๋‚˜ conda ๋“ฑ์˜ ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฒƒ์„ ์ฐพ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์›๋ž˜ python์€ setup.py๋ฅผ ํ†ตํ•ด ํŒจํ‚ค์ง€ ์ธ์Šคํ†จ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ˆœ์ˆ˜ ํŒŒ์ด์ฌ ํŒŒ์ผ ์ด์™ธ์— DLL ๋“ฑ์„ ํฌํ•จํ•  ๊ฒฝ์šฐ ์ธ์Šคํ†จ์ด ์‰ฝ์ง€ ์•Š๋‹ค. ์ด๋ฅผ ์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ์ถœํ˜„ํ•œ ๊ฒƒ์ด Python ํ‘œ์ค€์˜ pip์ด๋‹ค. conda๋Š” Anaconda์—์„œ ์‚ฌ์šฉํ•˜๋Š” pip ์ •๋„๋กœ ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค. ์ด ๋ฌธ์„œ์—์„œ๋Š” Anaconda ๋ฐฐํฌ๋ณธ์— ๋ฏธ๋ฆฌ ํฌํ•จ๋˜์ง€ ์•Š์€ ๋‹ค์–‘ํ•œ ํŒจํ‚ค์ง€์˜ ์ธ์Šคํ†จ์ด ํ•„์š”ํ•˜๋‹ค. Anaconda ๋ช…๋ น์ฐฝ(Anaconda Prompt)์„ ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์œผ๋กœ ์—ฐ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด pip๋กœ ์ธ์Šคํ†จํ•œ๋‹ค. conda ๋Œ€์‹  pip๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” Tensorflow์™€ ๊ฐ™์ด ์ผ๋ถ€ ํŒจํ‚ค์ง€์—์„œ๋Š” ๊ณต์‹์ ์œผ๋กœ pip๋งŒ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค > pip install opencv-contrib-python > pip install h5py > pip install vtk > pip install pygame > pip install graphviz > pip install pydot > pip install theano > pip install tensorflow > pip install keras opencv-contrib-python์€ OpenCV์™€ OpenCV contrib ๋ชจ๋“ˆ์„ ํฌํ•จํ•œ ํŒจํ‚ค์ง€์ด๋‹ค. vtk๋Š” ๊ณผํ•™๊ธฐ์ˆ  ์‹œ๊ฐํ™”์šฉ ํŒจํ‚ค์ง€์ด๋‹ค. pygame์€ ๋น„๋””์˜ค ๊ฒŒ์ž„์„ ์ž‘์„ฑํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“ˆ์ด๋‹ค. graphviz์™€ pydot์€ ๋‹ค์ด์–ด๊ทธ๋žจ์„ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•œ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€์ด์ง€๋งŒ ์‹ค์ œ ์‹คํ–‰ํŒŒ์ผ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์ง€ ์•Š๋‹ค. ์ •์ƒ์ž‘๋™์„ ์œ„ํ•ด์„œ๋Š” www.grpahviz.org์—์„œ ์„ค์น˜ ํŒŒ์ผ(ํ˜„ ๋ฒ„์ „์€ graphviz-2.38.msi)์„ ๋‹ค์šด๋กœ๋“œ ํ›„ graphviz๋ฅผ ์„ค์น˜ํ•˜๊ณ  ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ž์„ธํ•œ ์‚ฌํ•ญ์€ Keras_Install_on_Windows์„ ์ฐธ๊ณ ํ•œ๋‹ค. theano, tensorflow, keras๋Š” Machine Learning ํŒจํ‚ค์ง€์ด๋‹ค. ์—…๊ทธ๋ ˆ์ด๋“œ ์‹œ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด --upgrade ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ธ์Šคํ†จํ•œ๋‹ค. > pip install --upgrade opencv-contrib-python 1.1.2 ๊ธฐ๋ณธ ๊ตฌ๋™ ๋ฒ• Python์€ ๋ผ์ธ ๋‹จ์œ„๋กœ ๊ตฌ๋™๋˜๋Š” ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์ด๋‹ค. ์ฆ‰, Python์„ ๊ตฌ๋™ํ•˜๋ ค๋ฉด ์ธํ„ฐํ”„๋ฆฌํ„ฐ(python.exe)๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€์ด๋‹ค. (1) ์ธํ„ฐํ”„๋ฆฌํ„ฐ(python.exe)์—์„œ ํ•œ ์ค„์”ฉ ์ง์ ‘ ํƒ€์ดํŒ…ํ•˜์—ฌ ์‹คํ–‰ cmd์—์„œ Python ์ธํ„ฐํ”„๋ฆฌํ„ฐ(python.exe)๋ฅผ ๊ตฌ๋™ํ•œ ํ›„ ํ•œ ์ค„์”ฉ ๊ตฌ๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. (2) ํ…์ŠคํŠธ ์—๋””ํ„ฐ์—์„œ ์†Œ์Šค ์ž‘์„ฑ ํ›„ ์ธํ„ฐํ”„๋ฆฌํ„ฐ(python.exe)๋กœ ์‹คํ–‰ ๋จผ์ € ์Šคํฌ๋ฆฝํŠธ(.py)๋ฅผ ์ž‘์„ฑํ•œ ํ›„ python HelloPython.py์™€ ๊ฐ™์ด ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. # HelloPython.py a = 1 b = 2 c = a+b print('Hello Python!!! You know %d+%d=%d?'%(a, b, c)) ์‹คํ–‰์€ ์ฝ˜์†” ์ฐฝ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. C:\> python HelloPython.py Hello Python!!! You know 1+2=3? Python์„ ์ฝ˜์†” ์ฐฝ์—์„œ ๋ณ„๋„์˜ ์—๋””ํ„ฐ๋กœ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํŽธ์ง‘ํ•˜๊ณ , ์ด๋ฅผ ์ฝ˜์†”์—์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ ์ž‘์—…ํšจ์œจ์ง€ ๋‚ฎ๋‹ค. ์ด๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ํ†ตํ•ฉ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์ด๋‹ค. ํ†ตํ•ฉ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์œผ๋กœ๋Š” Visual Studio, PyCharm, Spyder ๋“ฑ์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์ฐธ๊ณ . ์›ํ•˜๋Š” ์œ„์น˜์— CMD ์ฐฝ ๋„์šฐ๊ธฐ Windows์—์„œ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ(CMD) ์ฐฝ์„ ๋„์šฐ๋ฉด ์‚ฌ์šฉ์ž์˜ ๋ฐ”ํƒ•ํ™”๋ฉด ์œ„์น˜๋กœ ์ฐฝ์ด ๋œฌ๋‹ค. ์ดํ›„ ํ•„์š”ํ•œ ์œ„์น˜๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ฒƒ์€ cd ๋ช…๋ น์„ ํ™œ์šฉํ•ด์„œ ํƒ€์ดํ•‘ํ•ด์•ผ ํ•œ๋‹ค. ๋ณด๋‹ค ์ข‹์€ ๋ฐฉ๋ฒ•์€ ํƒ์ƒ‰๊ธฐ๋ฅผ ์›ํ•˜๋Š” ์œ„์น˜์— ๋‘๊ณ  ๊ทธ ์œ„์น˜์— CMD ์ฐฝ์„ ๋„์šฐ๋Š” ๊ฒƒ์ด๋‹ค. 1.2 ํ†ตํ•ฉ๊ฐœ๋ฐœ ํ™˜๊ฒฝ Python์„ ์œ„ํ•œ ๋‹ค์–‘ํ•œ IDE(Integrated Development Environment, ํ†ตํ•ฉ๊ฐœ๋ฐœ ํ™˜๊ฒฝ)๊ฐ€ ์กด์žฌํ•œ๋‹ค. ์–ด๋–ค IDE๋ฅผ ์„ ํƒํ• ์ง€๋Š” ๊ฐœ์ธ์˜ ์ทจํ–ฅ๊ณผ IDE์˜ ์žฅ๋‹จ์ ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค. Jupyter ํ†ตํ•ฉ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์ด๋ผ๊ธฐ ๋ณด๋‹ค ์›น์—์„œ Python ์ฝ”๋“œ ์ž‘์„ฑ ๋ฐ Markdown ๋ฌธ์„œ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์›น์ƒ์—์„œ Python ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”๋กœ ํ™•์ธ ๊ฐ€๋Šฅํ•จ. Anaconda ๋ฐฐํฌ๋ณธ์— ํฌํ•จ๋˜์–ด ์žˆ์Œ Spyder Anaconda ๋ฐฐํฌ๋ณธ ์‚ฌ์šฉ ์‹œ ์ œ๊ณต๋˜๋Š” IDE๋กœ ๊ณผํ•™๊ธฐ์ˆ  ๊ณ„์‚ฐ์šฉ์œผ๋กœ ํŠนํ™” Crossplaform์ด๋ฉฐ ๋ฌด๋ฃŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” IDE Visual Studio PTVS Visual Studio์— PTVS๋ผ๋Š” ํ™•์žฅ์„ ์ธ์Šคํ†จํ•˜๋ฉด Python ์šฉ IDE๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ. Windows ์šฉ์ด๋ฉฐ, ๊ฐœ์ธ ๊ฐœ๋ฐœ์ž๋Š” ๋ฌด๋ฃŒ์ด๋ฉฐ ๊ทธ ์™ธ๋Š” ์ƒ์šฉ ์ตœ๊ณ ์˜ Windows ์šฉ IDE PyCharm ๊ฐ€์žฅ ๋งŽ์€ ์‚ฌ์šฉ์ž๊ฐ€ ์žˆ๋Š” Python IDE Crossplaform์ด๋ฉฐ ๋ฌด๋ฃŒ ๋ฐ ์œ ๋ฃŒ ๋ฒ„์ „ ์žˆ์Œ. ๋‹ค์–‘ํ•œ ํ™•์žฅ๊ธฐ๋Šฅ์„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Œ ๊ฐ€์žฅ ๋งŽ์€ ์‚ฌ๋žŒ์˜ ์„ ํƒ์„ ๋ฐ›๋Š” IDE Visual Studio Code Visual Studio Code์— Python ํ™•์žฅ์„ ์ธ์Šคํ†จํ•˜๋ฉด Python ์šฉ IDE๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ. Crossplatform์ด๋ฉฐ ๋ฌด๋ฃŒ ๋‹ค์–‘ํ•œ ํ™•์žฅ ๊ธฐ๋Šฅ์„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Œ ์ตœ๊ทผ ์‚ฌ์šฉ์ž ์ธต์ด ๋Š˜์–ด๋‚˜๊ณ  ์žˆ๋Š” IDE 1.2.1 Jupyter Jupyter๋Š” IPython ์‰˜์„ ์ด์šฉํ•ด์„œ Python ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋œ ๋™์  ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด๋‹ค. ์ธ์Šคํ†จ์€ pip install jupyter๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค. ์‹œ์ž‘ ๋ฒ„ํ„ด์—์„œ Jupyter Notebook์„ ์‹คํ–‰ํ•˜๋ฉด ๋””ํดํŠธ๋กœ ์‚ฌ์šฉ์ž ๋ฐ”ํƒ•ํ™”๋ฉด์ด ํ™ˆ ๋””๋ ‰ํ„ฐ๋ฆฌ(startup folder)๊ฐ€ ๋˜๋ฏ€๋กœ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์‹คํ–‰ ์ดํ›„์—๋Š” ๊ธฐ์กด ๋…ธํŠธ๋ฅผ ํŽธ์ง‘ํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๋…ธํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํŽธ์ง‘ํ•  ์ˆ˜ ์žˆ๋‹ค. Jupyter notebook์€ ipynb๋ผ๋Š” ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์Šคํƒ€์ผ ๋ณ€๊ฒฝ C:\Users\user_id\.jupyter ํด๋”์— custom์ด๋ผ๋Š” ์ž์‹ ํด๋”(C:\Users\user_id\.jupyter\custom)๋ฅผ ๋งŒ๋“ค๊ณ  custom.css๊ณผ custom.js๋ฅผ ํ†ตํ•ด ์ปค์Šคํ„ฐ๋งˆ์ด์ง•์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์Šคํƒ€์ผ์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋Š” custom.css๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค์Œ์€ ์ „ํ˜•์ ์ธ ์˜ˆ(์ฐธ์กฐ:<NAME>๋„ค ์ง‘)์ด๋‹ค. #notebook-container { line-height:1.8em } .rendered_html pre, .rendered_html code, pre, .CodeMirror, .prompt { font-family: Consolas, "Andale Mono WT","Andale Mono","Lucida Console","Lucida Sans Typewriter","DejaVu Sans Mono","Bitstream Vera Sans Mono","Liberation Mono","Nimbus Mono L",Monaco,"Courier New",Courier, monospace;; } .rendered_html pre,.rendered_html code { background-color: #f5f5f5; margin: 1em 0em } .rendered_html pre { padding: 8.5px; border: 1px solid #ccc; border-radius: 2px; } .rendered_html p > code, .rendered_html ul li code { border: solid 1px #e1 e4e5; color: #E74C3C; padding: 0 5px; overflow-x: auto; } .rendered_html blockquote{ margin: 1em 0em } blockquote { background-color: #fcf2f2; border-color: #dFb5b4; border-left: 5px solid #dfb5b4; padding: 0.5em; } 1.2.2 Spyder Spyder๋Š” Anaconda ๋ฐฐํฌ๋ณธ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” IDE์ด๋‹ค. ์—๋””ํ„ฐ์™€ ์ฝ˜์†”์„ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ์ž‘์—…์„ ํŽธํ•˜๊ธฐ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ํ”„๋กœ์ ํŠธ, ๋””๋ฒ„๊น…, ํ—ฌํ”„, ๋ณ€์ˆ˜ ๋ณด๊ธฐ(variable explorere) ๋“ฑ ๋‹ค์–‘ํ•œ ํŽธ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. Spyder๋Š” ๋Œ€ํ™”ํ˜•์ฐฝ(์ฝ˜์†”)์œผ๋กœ ํ‘œ์ค€ Python ์ฝ˜์†”์„ ํ™•์žฅํ•œ IPython ์ฝ˜์†”์„ ์‚ฌ์šฉํ•œ๋‹ค. ํŽธ์ง‘๊ธฐ์—์„œ ํŽธ์ง‘ํ•˜๋‹ค๊ฐ€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ „์ฒด ์ˆ˜ํ–‰ํ•˜๊ฑฐ๋‚˜ ์ผ๋ถ€ ์˜์—ญ์„ ์„ ํƒํ•œ ํ›„ ๊ทธ ๋ถ€๋ถ„๋งŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. Spyder๋Š” ๋””ํดํŠธ๋กœ ํ•œ ๊ฐœ์˜ ์ธ์Šคํ„ด์Šค๋งŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. Multiple instance ์‹คํ–‰ ๋ฐฉ๋ฒ• : Prefereance > Application > Advanced settins์—์„œ Use a single instance๋ฅผ off ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ spyder ํ”„๋กœ์ ํŠธ๋กœ ์—ด๊ธฐ > spyder -p . 1.2.3 Visual Studio Code Visual Studio Code๋Š” Microsoft์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฐ€๋ฒผ์šด ์—๋””ํ„ฐ์ด์ž ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์ด๋‹ค. ์„ค์น˜ ํ›„ python extension ๋“ฑ ํ•„์š”ํ•œ extension์„ ์ธ์Šคํ†จํ•˜์—ฌ ๊ทธ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๋Š” ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์‚ฌ์ „ ์ค€๋น„ ์‚ฌํ•ญ Anaconda๋ฅผ ์„ค์น˜ํ•˜๊ณ , ์•„๋ž˜์™€ ๊ฐ™์ด PATH๋ฅผ ์„ค์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. PATH=C:\ProgramData\Anaconda3;C:\ProgramData\Anaconda3\Scripts;%PATH% ์œ„ ์„ค์น˜ ๊ฒฝ๋กœ๋Š” Anaconda ์ธ์Šคํ†จ ์‹œ ์˜ต์…˜์— ๋”ฐ๋ผ ์ƒ์ดํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ฏธ๋ฆฌ ๋“ฑ๋ก๋˜์–ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. VSCode ์šฉ extension ์›ํ™œํ•œ Python ๊ฐœ๋ฐœ์„ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ์˜ extension์„ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค. Python : Microsoft ์ œ๊ณต. Python ์‚ฌ์šฉ์„ ์œ„ํ•ด ํ•„์š” Jupyter : Jupyter ์‚ฌ์šฉ์„ ์œ„ํ•ด ํ•„์š” Material Icon Theme : ํŒŒ์ผ ์•„์ด์ฝ˜์„ ๋ฉ‹์ด ์„ธ๊ฒŒ ๋ณด์—ฌ์คŒ. ๋ฌธ์„œ๋Š” Markdown์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•œ๋ฐ ๊ด€๋ จ๋œ extension์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Markdown+Math : Markdown ๋ฌธ์„œ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ์—์„œ ์ˆ˜์‹ ์ถœ๋ ฅ์„ ์œ„ํ•ด ํ•„์š” vscode-pandoc : pandoc์œผ๋กœ markdown์„ ๊ทธ๋ฆด ๋•Œ. ๋ณดํ†ต MS word๋กœ export ๊ฐ€ ๊ฐ€๋Šฅํ•จ. markdonwlint : markdown linting (์˜ต์…˜) Markdown Shorcuts : ๋งˆํฌ๋‹ค์šด ๋ฌธ ๋ฒˆ์— ๋Œ€ํ•œ shortcut ์ œ๊ณต (์˜ต์…˜) Markdown Preview Enhanced : ๋‚ด์žฅ๋œ Markdown preview ๋ณด๋‹ค ์šฐ์ˆ˜(์˜ต์…˜) ๋งˆํฌ๋‹ค์šด ์ˆ˜์‹์„ latex ์ž…๋ ฅ ๋ฐฉ์‹์œผ๋กœ ์ง์ ‘ ํ•˜๋Š” ๊ฒƒ์€ ํ˜„์‹ค์ ์œผ๋กœ ์‰ฝ์ง€ ์•Š๋‹ค. ์ข‹์€ ๋ฐฉ๋ฒ•์€ MS Word์—์„œ ์ž‘์„ฑํ•˜๊ณ  ์ด๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” MS Word plugin์œผ๋กœ writage๋ผ๋Š” ๊ฒƒ์ด ์žˆ๋‹ค. writage : MS word๋ฅผ ์œ„ํ•œ ํ”Œ๋Ÿฌ๊ทธ์ธ Visual Studio Code๋ฅผ ์ด์šฉํ•œ Python ๊ฐœ๋ฐœ ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ code ํ”„๋กœ์ ํŠธ๋กœ ์—ด๊ธฐ > spyder . ์‹คํ–‰ํ•˜๋Š” ์„ธ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• Run Run with debug Run in interactive window [ ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ํด๋ฆญ์œผ๋กœ ipython ์‹คํ–‰] 1.3 ํ™˜๊ฒฝ ๋ณ€์ˆ˜ PATH ํ™˜๊ฒฝ ๋ณ€์ˆ˜ PATH๋Š” ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์‹œ์˜ ๊ฒฝ๋กœ๋Š” ํƒ์ƒ‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Windows์—์„œ prog.exe๋ผ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์ฐพ์•„ ๊ธฐ๋™ํ•˜๊ฒŒ ๋œ๋‹ค. ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ(Current Working Directory, CWD) Windows ๋””๋ ‰ํ„ฐ๋ฆฌ PATH์— ์„ค์ •๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ (1) Windows ์‹œ์Šคํ…œ์—์„œ PATH ๊ด€๋ฆฌ ์‹œ์Šคํ…œ ์†์„ฑ ๋‹ค์ด์–ผ๋กœ๊ทธ์—์„œ ๊ณ ๊ธ‰ ํƒญ์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ ํƒํ•˜๋ฉด ํ˜„์žฌ ์„ค์ •๋œ PATH๋ฅผ ์กฐํšŒํ•˜๊ณ  ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. (2) ๋ช…๋ นํ–‰(cmd)์—์„œ PATH ๊ด€๋ฆฌ ๋ช…๋ นํ–‰(cmd)์—์„œ set path๋กœ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. PATH ํ™•์ธ > set path Path=C:\ProgramData\Anaconda3;C:\ProgramData\Anaconda3\Scripts;C:\Program Files (x86)\Graphviz2.38\bin;... PATH ์ถ”๊ฐ€ > set path=d:\myProg;%PATH% ์œ„์—์„œ %PATH%๋Š” ๊ธฐ์กด ์„ค์ •๋œ PATH๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ ์‹คํ–‰๋œ ๋ช…๋ น์ฐฝ์— ๋Œ€ํ•ด์„œ๋งŒ ์œ ํšจํ•˜๋‹ค. (3) Python์—์„œ PATH ๊ด€๋ฆฌ Python์˜ os ๋ชจ๋“ˆ๋กœ PATH๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜๋„๋ก ํ•œ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ ์‹คํ–‰๋˜๋Š” Python ํ”„๋กœ์„ธ์Šค์—์„œ๋งŒ ์œ ํšจํ•œ๋‹ค. 2. Python ๊ธฐ๋ณธ ํŠน์ง•๊ณผ ์ฒ ํ•™ Python์€ Guido von Rossum์ด 1991๋…„ ๋ฐœํ‘œํ•œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋ฐฉ์‹์˜ ๊ณ ๊ธ‰ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค. ๊ทธ ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋ฐฉ์‹์˜ ์–ธ์–ด(interpreted language) : C/C++์€ ์ปดํŒŒ์ผ์„ ํ†ตํ•ด ์‹คํ–‰ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋‚ด์ง€๋งŒ Python์€ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ๋ผ์ธ ๋‹จ์œ„๋กœ ์‹คํ–‰๋œ๋‹ค. ๋‹ค์–‘ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฒ• ์ง€์›(multi-paradigm programming language) : ๊ฐ์ฒด์ง€ํ–ฅ, ์ ˆ์ฐจ ์ง€ํ–ฅ ๋“ฑ๋“ฑ ๋‹ค์–‘ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฒ•์„ ์ง€์›ํ•˜๋‹ค. ์ฆ‰ ์“ฐ๊ธฐ ๋‚˜๋ฆ„์ด๋‹ค. ๋™์  ํƒ€์ดํ•‘(dynamic typing) : ์‹คํ–‰ํ•  ๋•Œ ์ž๋ฃŒํ˜•์„ ๊ฒ€์‚ฌํ•˜๋ฉฐ, ํฌ์ธํ„ฐ ์—ฐ์‚ฐ ์—†๋‹ค(์ž๋™ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜). ํ’๋ถ€ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•œ๊ธ€ ์œ„ํ‚คํ”ผ๋””์•„์— ์†Œ๊ฐœ๋œ Python์˜ ํ•ต์‹ฌ ์ฒ ํ•™์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. "์•„๋ฆ„๋‹ค์šด ๊ฒŒ ์ถ”ํ•œ ๊ฒƒ๋ณด๋‹ค ๋‚ซ๋‹ค." (Beautiful is better than ugly) "๋ช…์‹œ์ ์ธ ๊ฒƒ์ด ์•”์‹œ์ ์ธ ๊ฒƒ๋ณด๋‹ค ๋‚ซ๋‹ค." (Explicit is better than implicit) "๋‹จ์ˆœํ•จ์ด ๋ณต์žกํ•จ๋ณด๋‹ค ๋‚ซ๋‹ค." (Simple is better than complex) "๋ณต์žกํ•จ์ด ๋‚œํ•ดํ•œ ๊ฒƒ๋ณด๋‹ค ๋‚ซ๋‹ค." (Complex is better than complicated) "๊ฐ€๋…์„ฑ์€ ์ค‘์š”ํ•˜๋‹ค." (Readability counts) ๊ฒฐ๊ตญ Python์€ ์‰ฝ๊ฒŒ ์•Œ๊ธฐ ์‰ฌ์šด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋„๋ก ๊ณ ์•ˆ๋˜์—ˆ๋‹ค. IPython Python ํ‘œ์ค€ ์‰˜์€ python.exe๋ฅผ ๊ตฌ๋™์‹œํ‚ค๋ฉด ๋œ๋‹ค. IPython์€ ipython.org์—์„œ ๋ฐฐํฌํ•˜๋Š” ์‰˜๋กœ์„œ, ๋Œ€ํ™”ํ˜• ์ž‘์—…์„ ์œ„ํ•ด ํ‘œ์ค€์‰˜์„ ํ™•์žฅํ•œ ๊ฒƒ์ด๋‹ค. ๋ณดํ†ต ํ†ตํ•ฉ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋Œ€ํ™”ํ˜• ์‰˜์€ ํ‘œ์ค€์‰˜ ๋ณด๋‹ค๋Š” IPython์„ ์‚ฌ์šฉํ•œ๋‹ค. ์‹คํ–‰ํ•˜๋ฉด ํ”„๋กฌํ”„ํŠธ์—์„œ >>> ๋Œ€์‹  [1] ๋“ฑ๊ณผ ๊ฐ™์ด ๋ผ์ธ ๋ฒˆํ˜ธ๊ฐ€ ํ”„๋กฌํ”„ํŠธ ๋œ๋‹ค. CPython, JPython, ... Python์„ ์‹ค์ œ ์‹คํ–‰ํ•˜๋Š” ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋กœ๋Š” C๋กœ ๊ตฌํ˜„๋œ CPython, ์ž๋ฐ” ๊ฐ€์ƒ๋จธ์‹ ์šฉ JPython, ๋‹ท๋„ท ํ”Œ๋ž˜ํผ์šฉ IronPython ๋“ฑ๋“ฑ์ด ์žˆ๋‹ค. ๋ณดํ†ต์€ CPython์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. C/C++์™€์˜ ๊ตฌ๋ฌธ ๋น„๊ต ์ตœ์‹ ์˜ ๋‹ค๋ฅธ ์–ธ์–ด๊ฐ€ ๊ทธ๋ ‡๋“ฏ Python์€ C/C++์˜ ์˜ํ–ฅ์„ ๋งŽ์ด ๋ฐ›์•˜์œผ๋ฉฐ, ํ‚ค์›Œ๋“œ๋‚˜ ๊ตฌ๋ฌธ ๋“ฑ์ด ๋งค์šฐ ์œ ์‚ฌํ•˜๋‹ค. ๋‹ค์Œ์€ ๋ช‡๋ช‡ ์ฐจ์ด์ ์„ ๋‚˜์—ดํ•œ ๊ฒƒ์ด๋‹ค. ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•œ๋‹ค( C/C++๊ณผ ๋™์ผ) ๊ฐ ๋ผ์ธ์—์„œ # ์ดํ›„์˜ ๋ฌธ์ž์—ด์„ ๋ฌด์‹œํ•œ๋‹ค(C/C++์—์„œ๋Š” /* ... */ ๋˜๋Š” //๋ฅผ ์ด์šฉ) ํ•œ ๋ผ์ธ์— ํ•œ ๊ฐœ ๊ตฌ๋ฌธ์„ ์“ธ ๋•Œ๋Š” ๋ผ์ธ ๋ธŒ๋ ˆ์ดํฌ(;) ๊ฐ€ ํ•„์š” ์—†์œผ๋‚˜(์žˆ์–ด๋„ ๋ฌด๋ฐฉ), ๋‘ ๊ฐœ ์ด์ƒ์˜ ๊ตฌ๋ฌธ์ธ ๊ฒฝ์šฐ ;๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค(C/C++์€ ํ•ญ์ƒ ;๊ฐ€ ํ•„์š”) ํ•œ ๊ฐœ ๊ตฌ๋ฌธ์ด ๊ธธ์–ด ๋‹ค์Œ ๋ผ์ธ๊นŒ์ง€ ์—ฐ๊ฒฐํ•  ๊ฒฝ์šฐ ๊ฒฝ์šฐ \๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค(C/C++์™€ ๋™์ผ) ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด(string literal)์„ '...``, "...", '''...''', """...""" ๋“ฑ ๋„ค ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค(C/C++์€ "..." ๋งŒ ๊ฐ€๋Šฅ) ๋“ค์—ฌ ์“ฐ๊ธฐ(indentation)์„ ํ†ตํ•ด ๋ธ”๋ก์„ ๊ตฌ์„ฑํ•œ๋‹ค(C/C++์—์„œ๋Š” {...}๋ฅผ ์ด์šฉ) if, while ๋ฌธ์— ์‚ฌ์šฉ๋˜๋Š” ์กฐ๊ฑด๋ฌธ์— ๊ด„ํ˜ธ((...))๊ฐ€ ํ•„์š” ์—†๋‹ค(์žˆ์–ด๋„ ๋ฌด๊ด€)(C/C++์€ ๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผ ํ•จ) ์œ„์—์„œ C/C++ ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฐ€์žฅ ์–ด์ƒ‰ํ•˜๊ณ , ์˜ค๋ฅ˜๋ฅผ ์œ ๋ฐœํ•˜๋Š” ๊ฒƒ์ด ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ†ตํ•œ ๋ธ”๋ก ๊ตฌ์„ฑ์ด๋‹ค. ์–ด๋–ค ๊ตฌ๋ฌธ์ด :๋กœ ๋๋‚˜๋ฉด ๊ทธ ํ•˜์œ„ ์ˆ˜์ค€์˜ ๋ธ”๋ก์€ ๋™์ผํ•œ ๋“ค์—ฌ ์“ฐ๊ธฐ๊ฐ€ ๋˜๋ฉด ๋œ๋‹ค. if ์กฐ๊ฑด๋ฌธ: a = a b = c ์œ„์—์„œ if ๋ฌธ์€ :๋กœ ๋๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ๋ธ”๋ก ๊ตฌ์กฐ๊ฐ€ ์ด์–ด์ ธ์•ผ ํ•œ๋‹ค. ๋ธ”๋ก ๊ตฌ์กฐ๋Š” ๊ฐ™์€ ๊ฐœ์ˆ˜์˜ ๊ณต๋ฐฑ๋ฌธ์ž(์ŠคํŽ˜์ด์Šค๋‚˜ ํƒญ)๋กœ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•˜๋ฉด ๋œ๋‹ค. ๋ณดํ†ต 2๊ฐœ์˜ ์ŠคํŽ˜์ด์Šค๋‚˜ 4๊ฐœ์˜ ์ŠคํŽ˜์ด์Šค๊ฐ€ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ํƒญ ๋ฌธ์ž๋ฅผ ์ž˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” ํƒญ๊ณผ ์ŠคํŽ˜์ด์Šค๊ฐ€ ์„ž์—ฌ ์žˆ์„ ๊ฒฝ์šฐ ๋ˆˆ์œผ๋กœ ๋ณด๊ธฐ์—๋Š” ๊ฐ™์•„ ๋ณด์ด์ง€๋งŒ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‹ค์Œ ์˜ˆ๋Š” www.python.org์— ์†Œ๊ฐœ๋œ Python์œผ๋กœ ํ”ผ๋ณด๋‚˜์น˜์ˆ˜์—ด์„ ๊ณ„์‚ฐํ•˜๋Š” ์ฝ”๋“œ์™€ C/C++์—์„œ ๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค. Python์ด ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•˜๊ณ  ์ฝ๊ธฐ ์‰ฌ์šด ์ฝ”๋“œ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. Python์„ ํ†ตํ•œ ํ”ผ๋ณด๋‚˜์น˜์ˆ˜์—ด ๊ณ„์‚ฐ def fib(n): a, b = 0, 1 while a < n: a = b b = a+b print("%s ! = %d"%(n, a)) fib(1000) C/C++์—์„œ ํ”ผ๋ณด๋‚˜์น˜์ˆ˜์—ด ๊ณ„์‚ฐ void fib(int n) { int a = 0, b = 1; while(a<n) { a = b; b = a+b; } printf("%d ! = %d ",n, a) } int main() { fib(1000); return 0; } 2.1 ์ž๋ฃŒํ˜• Python์€ ๋ถˆ๋ฆฐ(bool), ์ •์ˆ˜(int), ์‹ค์ˆ˜(float), ๋ณต์†Œ์ˆ˜(complex) ๋“ฑ์˜ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•, ๋ฆฌ์ŠคํŠธ(list), ํŠœํ”Œ(tuple), ๋”•์…”๋„ˆ๋ฆฌ(dict), ์ง‘ํ•ฉ(set) ๋“ฑ์˜ ์ปจํ…Œ์ด๋„ˆ ์ž๋ฃŒํ˜•, ์œ ๋„ค ์ฝ”๋“œ ๋ฌธ์ž์—ด(str) ๋ฐ ANSI ๋ฌธ์ž์—ด(bytes) ๋“ฑ์˜ ๋ฌธ์ž์—ด ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ์ž๋ฃŒํ˜•์„ ์ œ๊ณตํ•œ๋‹ค. ๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ ์ž๋ฃŒํ˜• ์ •์˜ ๋ฐฉ๋ฒ•๊ณผ type(x)๋ฅผ ํ†ตํ•ด ์ž๋ฃŒํ˜•์„ ํ™•์ธํ•œ ๊ฒฐ๊ณผ ์˜ˆ์‹œ์ด๋‹ค. bool์€ True, False๋งŒ์„ ์ €์žฅํ•˜๋Š” ๋ถˆ๋ฆฐ ํ˜•์ด๋‹ค. == ๋“ฑ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด ๋ฌธ์˜ ๊ฒฐ๊ณผ์ด๋‹ค. >>> isCheck = True # True or False >>> type(isCheck) <class 'bool'> int, float, complex๋Š” ์ˆ˜์น˜์ž๋ฃŒํ˜•์ด๋‹ค. ์‚ฌ์น™์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. >>> a = 12 >>> type(a) <class 'int'> >>> f = 12.3 >>> type(f) <class 'float'> >>> c = 1+2*1j >> type(c) <class 'complex'> Python์—์„œ ์ œ๊ณตํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ๋กœ๋Š” list, tuple, dict, set ๋“ฑ์ด ์žˆ๋‹ค. ์ด์ค‘ list์™€ tuple์€ ์ˆœ์ฐจ ์ปจํ…Œ์ด๋„ˆ(๋˜๋Š” sequence)์ด๊ณ , dict์™€ set์€ ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค. list๋Š” C++ STL์˜ vector๋‚˜ list์™€ ๋™๋“ฑํ•˜๋‹ค. Python์—๋Š” ๋ฐฐ์—ด์ด ์—†๋Š”๋ฐ list๊ฐ€ ๊ทธ ์—ญํ• ์„ ๋Œ€์‹ ํ•œ๋‹ค. C++ STL์˜ vector๋‚˜ list์™€ ๋‹ค๋ฅธ ์ ์€ ์ €์žฅํ•˜๋Š” ์š”์†Œ์˜ ์ž๋ฃŒํ˜•์ด ๋™์ผํ•œ ์ž๋ฃŒํ˜•์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ์ ์ด๋‹ค. >>> x = [1, 'ones', 1.2] >>> type(x) <class 'list'> >>> x[0] >>> x[1] 'ones' tuple์€ ์ฝ๊ธฐ ์ „์šฉ list์ด๋‹ค. >>> y = (1,2, 'one') >>> y[0] >>> y[2] 'one' >>> type(y) <class 'tuple'> dict๋Š” C++ STL์˜ map๊ณผ ๋™๋“ฑํ•˜๋‹ค. ์ฆ‰, <Key, Value>์˜ ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ž๋ฃŒํ˜•์ด๋‹ค. C++ STL์˜ map๊ณผ ๋‹ค๋ฅธ ์ ์€ Key๋‚˜ Value์˜ ์ž๋ฃŒํ˜•์ด ๋™์ผ ์ž๋ฃŒํ˜•์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. >>> dic = {'name':'pey', 'phone':'0119993323', 'birth': '1118'} >>> dic['name'] >>> dic = {1:'pey',1.2:'good', 'test':5} set์€ ๊ฐ๊ฐ C++ STL์˜ set๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค. ์ฆ‰, ์ˆœ์„œ ์—†์ด ์ €์žฅํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค. ์ด ๋˜ํ•œ C++ STL์˜ set๊ณผ ๋‹ฌ๋ฆฌ ๋™์ผํ•œ ์ž๋ฃŒํ˜•์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค. >>> myset = {1,2,3} >>> myset {1, 2, 3} >>> type(myset) <class 'set'> C์—์„œ๋Š” 1๊ฐœ ๋ฌธ์ž๋ฅผ ํ‘œ์‹œํ•˜๋Š” char ์„ ๊ธฐ๋ณธ ํƒ€์ž…์œผ๋กœ ์ œ๊ณตํ•˜๊ณ , char์˜ ๋ฐฐ์—ด๋กœ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃฌ๋‹ค. ํ•˜์ง€๋งŒ Python์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๋ฐฉ์‹ ๋Œ€์‹  ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž์—ด์„ 1๊ฐœ์˜ ๊ฐ์ฒด๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค. ํ•œ๋ฌธ์ž๋ฅผ 2๋ฐ”์ดํŠธ ํฌ๊ธฐ๋กœ ํ‘œ์‹œํ•˜๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด์€ str์ด๊ณ (๊ทธ๋ƒฅ ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ํ•œ๋‹ค), 1๋ฐ”์ดํŠธ์˜ ๋ฌธ์ž์—ด์ธ ANSI ๋ฌธ์ž์—ด์€ bytes์ด๋‹ค. str๊ณผ bytes๋Š” ๊ณ ์ • ํญ, ๊ณ ์ • ๊ธธ์ด์˜ ํŠน์ˆ˜ํ•œ ์ˆœ์ฐจ ์ปจํ…Œ์ด๋„ˆ(์‹œํ€€์Šค)๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•œ๋ฒˆ ์ƒ์„ฑํ•˜๋ฉด ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅ(์ฝ๊ธฐ ์ „์šฉ) ํ•˜๋‹ค. >>> word = 'Hello' >>> word 'Hello' >>> type(word) <class 'str'> >>> bword = b'Hello' >>> bword b'Hello' >>> type(bword) <class 'bytes'> 2.1.1 ์ˆ˜์น˜ ์ž๋ฃŒํ˜• ์ˆ˜์น˜์ž๋ฃŒํ˜•์œผ๋กœ๋Š” int, float, complex๊ฐ€ ์žˆ๋‹ค. int์™€ float๋Š” ์ง€์ • ์‹œ ์†Œ์ˆ˜์ ์˜ ์œ ๋ฌด์— ๋”ฐ๋ผ ๊ฒฐ์ •๋œ๋‹ค. ์‚ฌ์น™์—ฐ์‚ฐ์—์„œ๋Š” ๊ฐ€์žฅ ํฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ๊ฒฐ์ •๋˜๋Š” ๋ฐ ์ฃผ์˜ํ•  ์ ์€ int์™€ int์˜ ์—ฐ์‚ฐ ์—ญ์‹œ ํ•ญ์ƒ float๋ผ๋Š” ์ ์ด๋‹ค. >>> x = 10 >>> type(x) <class 'int'> >>> y = 10. >>> type(y) <class 'float'> >>> 10/2 # int/int but result always float 5.0 ์ผ๋ฐ˜์ ์ธ ์‚ฌ์น™์—ฐ์‚ฐ ์ด์™ธ์—<NAME> ๊ณ„์‚ฐ์€ **์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋˜๋Š” pow(x, r)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. >>> 2.1**2 4.41 >>> pow(2.1,2) 4.41 complex์€ ๋ฏธ๋ฆฌ ์ •์˜๋œ 1j๋กœ ๋ณต์†Œ ๋‹จ์œ„๋ฅผ ์ด์šฉํ•˜๊ฑฐ๋‚˜ complex(re, im)์„ ํ†ตํ•ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. >>> x = 1-2*1j >>> y = complex(2,3) >>> x+y (3+1j) ์‹ค์ˆ˜๋ถ€์™€ ํ—ˆ์ˆ˜๋ถ€๋Š” c.real, c.imag๋กœ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๊ณ , c.conjugate()๋Š” ๊ณต์•ก ๋ณต์†Œ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. >>> x = complex(2,3) >>> x.real 2.0 >>> x.imag 3.0 >>> x.conjugate() (2-3j) ์—ฐ์‚ฐ์ž ๋‹ค์Œ์€ ์—ฐ์‚ฐ์ž๋ฅผ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. x+y, x-y, x*y, x/y : ์‚ฌ์น™ ์—ฐ์‚ฐ -x, +x : ๋‹จํ•ญ ์—ฐ์‚ฐ pow(x, y), x**y :<NAME> ์—ฐ์‚ฐ x//y, x%y, divmode(x, y) : ๋ชซ, ๋‚˜๋จธ์ง€, (๋ชซ, ๋‚˜๋จธ์ง€) abs(x), int(x), float(x), complex(re, im), c.conjugate(), Integer type์— ๋Œ€ํ•œ bitwise operation x|y, x^y, x&y, x << n, x >> n, ~x ์œ„ ์—ฐ์‚ฐ์€ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž and, or, not๊ณผ ๊ตฌ๋ถ„๋˜์–ด์•ผ ํ•œ๋‹ค. x and y, x or y, not x 2.1.2 ๋ถˆ๋ฆฐ ์ž๋ฃŒํ˜• bool ์€ ๋ฏธ๋ฆฌ ์ •์˜๋œ ์ƒ์ˆ˜ True, False๋งŒ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. >>> a = 1 >>> b = 2 >>> is_same = (a == b) >>> is_same False 2.1.3 ์ˆœ์ฐจ ์ปจํ…Œ์ด๋„ˆ (1) ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ list๋Š” ๋‹ค๋ฅธ ์–ธ์–ด์˜ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒํ˜•์ด๋‹ค. list๋ผ๊ณ  ๋ช…๋ช…ํ•œ ์ด์œ ๋Š” ๊ตฌ์„ฑ์š”์†Œ์˜ ํƒ€์ž…์ด ๊ฐ™์„ ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ƒ์„ฑ์€ [] ๊ธฐํ˜ธ๋กœ ๋‘˜๋Ÿฌ์‹ธ๋ฉด ๋œ๋‹ค. >>> x = [1,2,3] >>> y = ['Hello', 'World', 2] >>> z = [1,2, [3,5]] >>> q = [] # null list. same to q=list() ์œ„์—์„œ 'Hello'๋Š” ๋ฌธ์ž์—ด(str) ๊ฐ์ฒด์ด๋‹ค. ์—ฌ๊ธฐ์— ๋ณด๋‹ค ์ž์„ธํžˆ ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค. ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ list, tuple, str์€ x[i] ๋“ฑ๊ณผ ๊ฐ™์€<NAME>์œผ๋กœ ์ธ๋ฑ์‹ฑ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ์ธ๋ฑ์Šค i๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค(0-based index). ํŠน์ˆ˜ํ•˜๊ฒŒ x[-1]์€ ๋งˆ์ง€๋ง‰ ์š”์†Œ์ด๋‹ค. x[i:j] ๊ธฐํ˜ธํ˜ธ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, i๋ถ€ํ„ฐ j-1์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•ด ์ค€๋‹ค. >>> x = [0,1,2,3,4,5] >>> x[1] >>> x[1] = 10 >>> x [0,10,2,3,4,5] >>> x[-1] >>> x[1:3] [10,2] >>> x[1:6] [10, 2, 3, 4, 5] >>> x[3:-1] # same to x[3:5] [3, 4] ์—ฐ์‚ฐ list๋Š” + ์—ฐ์‚ฐ์œผ๋กœ ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์—ฐ๊ฒฐ(concatenation) ํ•  ์ˆ˜ ์žˆ๊ณ , * ์—ฐ์‚ฐ์œผ๋กœ ๋ฐ˜๋ณตํ•ด์„œ ๋งŒ๋“ค์–ด ๋‚ด๋‹ค. >>> x=[1,2,3] >>> y = [4,5,6] >>> x+y [1, 2, 3, 4, 5, 6] >>> x*2 [1, 2, 3, 1, 2, 3] >>> x*y # ERROR ์š”์†Œ ์ถ”๊ฐ€์™€ ์‚ญ์ œ x.append(v)๋Š” list x์˜ ๋์— v๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. x.insert(i, v)๋Š” i ์œ„์น˜์— v๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์€ x[i] = [], x[i:j]=[] ๋“ฑ๊ณผ ๊ฐ™์ด null list๋ฅผ ๋Œ€์ž…ํ•˜๊ฑฐ๋‚˜, del x[i], del x[i:j] ๋“ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. >>> x = [] >>> x.append(1) >>> x.append(2) >>> x.append(3) >>> x.insert(1,10) >>> x [1, 10, 2, 3] >>> x[1:3] = [] # del x[1:3] >>> x [1, 3] ํŠœํ”Œ ์ƒ์„ฑ tuple๋Š” ์ฝ๊ธฐ ์ „์šฉ ๋ฆฌ์ŠคํŠธ์ด๋‹ค. ์ƒ์„ฑ์€ () ๊ธฐํ˜ธ๋กœ ๋‘˜๋Ÿฌ์‹ธ๋ฉด ๋˜๋ฉฐ, ํ•œ๋ฒˆ ์ƒ์„ฑํ•˜๋ฉด ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค. >>> x = (1,2,3) >>> y = ('Hello', 'World', 2) >>> z = (1,2, [3,5]) ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ ์ธ๋ฑ์‹ฑ ๋ฐ ์Šฌ๋ผ์ด์‹ฑ ๋ฐฉ๋ฒ•์€ list์™€ ๋™์ผํ•˜๋‹ค. ๋‹ค๋งŒ ์ฝ๊ธฐ ์ „์šฉ์ด๋ฏ€๋กœ ์š”์†Ÿ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. >>> x = (0,1,2,3,4,5) >>> x[1] >>> x[1] = 10 # ERROR >>> x [0,1,2,3,4,5] >>> x[-1] >>> x(1:3) (1,2) >>> x(1:6) (1, 2, 3, 4, 5) >>> x[3:-1] # same to x[3:5] (3, 4) ์—ฐ์‚ฐ list์ฒ˜๋Ÿผ + ์—ฐ์‚ฐ์œผ๋กœ ์—ฐ๊ฒฐ(concatenation) ํ•  ์ˆ˜ ์žˆ๊ณ , * ์—ฐ์‚ฐ์œผ๋กœ ๋ฐ˜๋ณตํ•ด์„œ ๋งŒ๋“ค์–ด ๋‚ด๋‹ค. >>> x=(1,2,3) >>> y = (4,5,6) >>> x+y (1, 2, 3, 4, 5, 6) >>> x*2 (1, 2, 3, 1, 2, 3) >>> x*y # ERROR ๋ฆฌ์ŠคํŠธ์™€์˜ ํ˜• ๋ณ€ํ™˜ tuple๊ณผ list๋Š” ๋‹จ์ง€ ์“ธ ์ˆ˜ ์žˆ๋Š๋ƒ์˜ ์ฐจ์ด์ ๋งŒ ์žˆ๋‹ค. ์ด ๋‘˜์˜ ๋ณ€ํ™˜์„ tuple(x) ๋˜๋Š” list(x) ๋“ฑ์„ ํ†ตํ•ด ๊ฐ€๋Šฅํ•˜๋‹ค. >>> x = (1,2,3) >>> y = list(x) >>> y.append(4) >>> z = tuple(y) ์–ธ ํŒจํ‚น(unpacking) tuple์€ ์–ธ ํŒจํ‚น์„ ํ†ตํ•ด ๊ฐ ์š”์†Œ๋ฅผ ์‰ฝ๊ฒŒ ๋ถ„ํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. list ์—ญ์‹œ ์‹์˜ ์˜ค๋ฅธ์ชฝ ๊ฐ’(R-value)๋กœ ์‚ฌ์šฉํ•˜๋ฉด tuple๊ณผ ๋™์ผํ•˜๋ฏ€๋กœ ๊ฐ™์€ ๋ฐฉ์‹์˜ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. >>> a, b = (1,2) # a=1, b=2 >>> x,*y, z = (1,2,3,4,5) # x=1, y=(2,3,4), z=5 ์œ„์—์„œ *y์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ starred expression์ด๋ผ๊ณ  ํ•œ๋‹ค. str์—๋„ unpacking์˜ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. >>> a, b, c, d = 'john' # a='j', b='o', c='h', d='n' >>> actor = 'James Dean' >>> x,*y, z = actor >>> x 'J' >>> y ['a', 'm', 'e', 's', ' ', 'D', 'e', 'a'] >>> z 'n' 2.1.4 ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ ์ง‘ํ•ฉ 2.1.5 ๋ฌธ์ž์—ด ๋ฌธ์ž 1๊ฐœ์„ 2๋ฐ”์ดํŠธ๋กœ ํ‘œํ˜„ํ•˜๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด์€ str์—, ๋ฌธ์ž 1๊ฐœ๋ฅผ 1๋ฐ”์ดํŠธ๋กœ ํ‘œํ˜„ํ•˜๋Š” ANSI ๋ฌธ์ž์—ด์€ bytes๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. Python์—์„œ๋Š” ํ‘œ์ค€ ๋ฌธ์ž์—ด์€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด์ด๋‹ค. ์ฆ‰, ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ํ•˜๋ฉด str์„ ์˜๋ฏธํ•˜๋ฉฐ, bytes๋Š” ๊ธธ์ด๊ฐ€ ๊ณ ์ •๋œ 1 byte ๋ฐฐ์—ด๋กœ ์ดํ•ดํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ๋ฌธ์ž์—ด์„ ์ •์˜ํ•˜๋Š” 4๊ฐ€์ง€ ๋ฐฉ๋ฒ• ๋ฌธ์ž์—ด(str)์€ ์ž‘์€๋”ฐ์˜ดํ‘œ๋‚˜ ํฐ๋”ฐ์˜ดํ‘œ๋กœ ๋‘˜๋Ÿฌ์‹ธ๋ฉด ๋˜๋Š”๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ •์˜ํ•œ๋‹ค. >>> movie = 'World War Z' >>> movie = "World War Z" >>> movie = '''World War Z''' >>> movie = """World War Z""" ๋งŒ์•ฝ ๋ฌธ์ž์—ด ๋‚ด์— โ€˜ ๋˜๋Š” โ€œ ๊ฐ€ ์žˆ์„ ๋‹ค๋ฅธ ์ข…๋ฅ ์˜ ๋”ฐ์˜ดํ‘œ๋กœ ๋‘˜๋Ÿฌ์‹ธ๋ฉด ๋œ๋‹ค. >>> sentense = "It's fine" >>> sentense = '"I like the word "good"' ๋”ฐ์˜ดํ‘œ ์„ธ๊ฐœ์ธ ๊ฒƒ์€ multiline์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ •์˜ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. >>> sentense = """Jane is good. Paul is bad""" ๋ฌผ๋ก  C/C++์ฒ˜๋Ÿผ ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋กœ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. >>> sentense = 'It\'s fine' raw ๋ฆฌํ„ฐ๋Ÿด ๊ฒฝ๋กœ๋ช…์€ ์ง€์ •ํ•  ๋•Œ๋Š” ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๊ฐ€ ์˜คํžˆ๋ ค ๋ถˆํŽธํ•˜๋‹ค. D:\Prog์™€ D:\newProg๋ผ๋Š” ๋‘ ํด๋”๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๊ธฐ๋กœ ํ•œ๋‹ค. >>> dir1 = "D:\Prog" >>> dir2 = "D:\newProg" >>> print(dir1) D:\Prog >>> print(dir2) D: ewProg ์œ„์—์„œ \๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ํ•ด์„๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” \\๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ r์„ ๋ฌธ์ž์—ด ์•ž์— ๋‘๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์ด๋•Œ r์€ raw string literal์„ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰ \๋ฅผ ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋กœ ์ธ์‹ํ•˜์ง€ ์•Š๋Š”๋‹ค. new_dir1 = r"D:\newProg" new_dir2 = "D:\\newDir" ์ฐธ๊ณ ๋กœ Python์€ ์šด์˜ ์ฒด๊ณ„์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ \์™€ / ๋ชจ๋‘ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ๋ถ„์ž๋กœ ์ธ์‹ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ /๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ฒฝ๋กœ๋ฅผ ์“ฐ๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋‹ค. bytes bytes๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ b๋ฅผ ์•ž์— ๋ถ™์ด๋Š” ๊ฒƒ์„ ์ œ์™ธํ•˜๋ฉฐ str๊ณผ ๋™์ผํ•˜๋‹ค. ์ถœ๋ ฅํ•ด๋„ str๊ณผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด b๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค. >>> movie = b'World War Z' >>> print(movie) b'World War Z' ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ ์—ญ์‹œ str๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•˜๋ฉฐ, 'r`๋ฅผ ์“ฐ๋ฉด ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋ฅผ ๋ฌด์‹œํ•˜๋Š” raw literal์ด ๋œ๋‹ค. >>> sentense = b'It\'s fine' >>> dir = br"d:\nprog" >>> print(dir) b'd:\nprog' ์ด์–ด์ง€๋Š” ์„ค๋ช…์—์„œ str๊ณผ bytes์˜ ์‚ฌ์šฉ์ƒ์— ์ฐจ์ด์ ์€ ์—†๋‹ค. bytes๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ €์žฅํ•  ๋•Œ encoding๋œ ์ƒํƒœ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์—ฐ์‚ฐ ๋ฌธ์ž์—ด์—๋Š” +์™€ *์—ฐ์‚ฐ์ž๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, [] ์—ฐ์‚ฐ์ž๋กœ ๋กœ ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ์ด๊ฐ€๋Šฅํ•˜๋‹ค. ๋˜ํ•œ ๋ฌธ์ž์—ด ์กฐ์ž‘ ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค. + ์—ฐ์‚ฐ์€ concatenation์ด๋‹ค. >>> head = "Python" >>> tail = " is fun!" >>> head + tail 'Python is fun!' * ์—ฐ์‚ฐ์€ ๋ฐ˜๋ณต์ด๋‹ค. ๋‹ค์Œ์€ ํ”ํžˆ ์‚ฌ์šฉํ•˜๋Š” ์‘์šฉ ์˜ˆ์ด๋‹ค. # script.py print("=" * 50) print("My Program") print("=" * 50) > python script.py ================================================== My Program ================================================== ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ [] ์—ฐ์‚ฐ์ž๋กœ ์ธ๋ฑ์‹ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋•Œ ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ณ  โ€“๋Š” ์ œ์ผ ๋’ค์ชฝ์—์„œ์˜ ์ธ๋ฑ์Šค๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์ฆ‰, -1์€ ์ œ์ผ ๋’ค, -2๋Š” ๋’ค์—์„œ ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. :๊ธฐํ˜ธ๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. >>> s = 'Python uses zero-based index' >>> s[0] 'P' >>> s[1] 'y' >>> s[-1] 'x' >>> s[-2] 'e' >>> s[7:11] 'uses' >>> s[7:-1] 'uses zero-based inde' >>> s[7:] 'uses zero-based index' ์ฃผ์˜ํ•  ์ ์€ ์ธ๋ฑ์‹ฑ์ด๋‚˜ ์Šฌ๋ผ์ด์‹ฑ ๋œ ๋ถ€๋ถ„์„ L-value๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์—†๋‹ค๋Š” ์ ์ด๋‹ค. >>> s[1] = 'T' # Error >>> s[1:3] = 'As' # Error ํฌ๋งทํŒ…(% ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• - C ์Šคํƒ€์ผ) ๋ฌธ์ž์—ด์˜ ํฌ๋งทํŒ…๋Š” % ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , C์–ธ์–ด์˜ printf() ํ•จ์ˆ˜์˜ %s, %f, %d ๋“ฑ์˜ ํฌ๋งทํŒ… ์ง€์‹œ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. >>> "I have %d apples" % 3 'I have 3 apples' ๋งŒ์•ฝ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ํฌ๋งทํŒ…ํ•  ๋•Œ๋Š” () ๊ธฐํ˜ธ ๋กค ํŠœํ”Œ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋ฉด ๋œ๋‹ค. >>> 'X = %d, y = %f\n' %(1.2,3.1) 'X = 1, y = 3.100000\n' ๋ณดํ†ต ๋ฌธ์ž์—ด ํฌ๋งทํŒ…์€ print() ๋ฌธ์„ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค. print('X = %d, y = %f\n' %(1.2,3.1)) ํฌ๋งทํŒ…(str.format() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• - Pythonic ์Šคํƒ€์ผ) ๋ณด๋‹ค ํŒŒ์ด์ฌ์Šค๋Ÿฌ์šด ๋ฐฉ๋ฒ•์€ str.format() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ํ•„์š”ํ•œ ์œ„์น˜์— {}๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค. >>> 'x = {}, y = {}\n'.format(1.2,3.1) 'x = 1.2, y = 3.1\n' {}์— 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ž…ํ•˜๊ฑฐ๋‚˜, ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. >>> 'y = {0}, y = {1}\n'.format(1.2,3.1) 'y = 1.2, y = 3.1\n' >>> 'x = {1}, y = {0}\n'.format(1.2,3.1) 'x = 3.1, y = 1.2\n' >>> 'x = {x}, y = {y} '.format(x=1.2, y=3.1) 'x = 1.2, y = 3.1\n' ํฌ ๋งค์นญํ•˜๋Š” ๊ฒƒ์€ {:format} ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. >>> 'x = {x:0.2f}, y = {y} '.format(x=1.212, y=3.1) 'x = 1.21, y = 3.1\n' ์œ„์—์„œ 0.2f๋Š” ์‹ค์ˆ˜ํ˜• ๋ฐ์ดํ„ฐ์˜ ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‚˜ ์ •๋ ฌ, ์ž๋ฆฟ์ˆ˜ ๋“ฑ๊ณผ ๊ด€๋ จํ•ด์„œ๋Š” ๋ ˆํผ๋Ÿฐ์Šค ๋งค๋‰ด์–ผ์„ ์ฐธ์กฐํ† ๋ก ํ•œ๋‹ค. ๋ฌธ์ž์—ด ์กฐ์ž‘ ๋ฌธ์ž์—ด ์กฐ์ž‘ ํ•จ์ˆ˜๋กœ๋Š” count(), find(), rfind(), index(), rindex(), replace(), join(), split(), lstrip(), rstrip(), strip(), upper(), lower() ๋“ฑ์ด ์žˆ๋‹ค. ์ด๋“ค ํ•จ์ˆ˜๋Š” ๋ชจ๋‘ ์ผ๋ฐ˜ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž์—ด์˜ ๋ฉ”์˜๋“œ์ด๋‹ค. count(s)๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ, find(s)๋Š” ์ตœ์ดˆ ์ฐพ์€ ๋ฌธ์ž์—ด์˜ ์œ„์น˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. rfind(s)๋Š” ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฐพ๋Š”๋‹ค. index(s), rindex(s)๋Š” find(s)์™€ rfind(s)์™€ ๋™์ผํ•˜์ง€๋งŒ ์ฐพ์ง€ ๋ชปํ•  ๋•Œ๋Š” ValueError๋ฅผ ์ด๋ฅดํ‚จ๋‹ค. >>> names = 'John Elis Python Elis Mike' >>> names.count('Elis') >>> names.find('Elis') >>> names.rfind('Elis') 17**** count(s), find(s), rfind(s), index(s), rindex(s) ๋“ฑ์˜ ํ•จ์ˆ˜๋Š” ์‹œ์ž‘๊ณผ ๋๋‹จ์˜ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. >>> names.find('Elis',10) 17 >>> names.find('Elis',10,24) 17 replace(old, new)๋Š” ์ฐพ์•„๋ฐ”๊พธ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. >>> names = 'John Elis Python Elis Mike' >>> names.replace('Elis','Jane') 'John Jane Python Jane Mike' replace(old, new, count)๋กœ ๋ฐ”๊พธ๊ธฐ ํšŒ์ˆ˜ count๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. >>> names = 'John Elis Python Elis Mike' >>> names.replace('Elis','Jane',1) 'John Jane Python Elis Mike' split()๋Š” white space๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜์—ฌ string list๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. split(sep)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด sep๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ถ„๋ฆฌํ•œ๋‹ค. >>> name = 'John Python Elis Mike' >>> name.split() ['John', 'Python', 'Elis', 'Mike'] >>> name = 'John\tPython\n Elis Mike' >>> name.split() ['John', 'Python', 'Elis', 'Mike'] ์œ„์˜ ์˜ˆ์ฒ˜๋Ÿผ split()์œผ๋กœ ์‚ฌ์šฉํ•  ๋•Œ ๊ณต๋ฐฑ๋ฌธ์ž๋Š” ' ', '\t', '\n', '\r' ์ค‘ ํ•˜๋‚˜์ด๋ฉด ๋œ๋‹ค. ๋ฐ˜๋ฉด์— split(sep)์œผ๋กœ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ์ฃผ์–ด์ง„ sep๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์œผ๋กœ๋งŒ ๋ถ„๋ฆฌ ๊ฐ€๋Šฅํ•˜๋‹ค. >>> name = 'John Python, Elis, Mike' >>> name.split(',') ['John Python', ' Elis', ' Mike'] join(iterable)์€ ์ธ์ž๋กœ ์ฃผ์–ด์ง„ iteration์ด ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด(๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ ๋“ฑ)๋ฅผ ์ž์‹ ์œผ๋กœ ์ด์–ด์„œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค. >>> a = ['Mike','John'] >>> '-'.join(a) 'Mike-John' >>> b = 'Mike' >>> '-'.join(b) 'M-i-k-e' iterable์ด๋ผ๋Š” ๊ฒƒ์€ ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•œ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋Š” ์˜๋ฏธ์ด๋‹ค. ์œ„์—์„œ ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฆฌ์ŠคํŠธ, ๋˜๋Š” ๋ฌธ์ž์—ด ๊ทธ ์ž์ฒด๋ฅผ ์ด์–ด์คŒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€,๋กœ ๊ตฌ๋ถ„๋˜๋Š” ์ˆซ์ž ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์€ ์ „ํ˜•์ ์ด ์ฝ”๋“œ์ด๋‹ค. ์ด์™ธ์— ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋กœ๋Š” ์™ผ์ชฝ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ด ์ฃผ๋Š” lstrip(), ์˜ค๋ฅธ์ชฝ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๋Š” rstrip(), ์–‘์ชฝ ๊ณต๋ฐฑ์„ ์ œ ๊ฑฐ ๋‚˜๋Š” strip(), ๋Œ€๋ฌธ์ž ๋˜๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ด ์ฃผ๋Š” upper(), lower() ๋“ฑ์ด ์žˆ๋‹ค. >>> line = ' This is test ' >>> line.rstrip() ' This is test' >>> s = "What's this?" >>> s.upper() "WHAT'S THIS?" >>> s.lower() "what's this?" 2.1.6 ํ˜• ๋ณ€ํ™˜๊ณผ ํƒ€์ž… ๊ฒ€์‚ฌ ํ˜• ๋ณ€ํ™˜ ์ž๋ฃŒํ˜•๋“ค์€ ์ž์‹ ๋“ค์˜ ์ด๋ฆ„์ธ bool, int, float, str, list, tuple, bytes๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜• ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค. >>> bool('True') True >>> bool('True') True >>> bool(1) True >>> bool(0) False >>> bool(-1) True >>> str(12) '12' >>> float('12.3') 12.3 ํƒ€์ž… ๊ฒ€์‚ฌ type(x)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ˜•์„ ๋ฆฌํ„ดํ•ด ์ค€๋‹ค. ์ด๋ฅผ ๊ฒ€ํ† ํ•˜๋ฉด ํƒ€์ž…์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. x = 1.0 if type(x) is int: print('type(x) is integer') a = [1,2,3] if type(a) is list: print('list') import numpy as np z = np.array([1,2,3]) if z is np.ndarray: print('numpy.ndarray') 2.1.7 mutable vs immutable mutable๊ณผ immutable ํŒŒ์ด์„ ์€ ๋ชจ๋“  ๊ฒƒ์ด ๊ฐ์ฒด(object)์ธ๋ฐ, ๊ทธ ์†์„ฑ์ด mutable(๊ฐ’์ด ๋ณ€ํ•œ๋‹ค) ๊ณผ immutable๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. Immutable : ์ˆซ์ž(number), ๋ฌธ์ž์—ด(string), ํŠœํ”Œ(tuple) Mutable : ๋ฆฌ์ŠคํŠธ(list), ๋”•์…”๋„ˆ๋ฆฌ(dictionary), NumPy์˜ ๋ฐฐ์—ด(ndarray) ์ฆ‰, ์ˆซ์ž, ๋ฌธ์ž์—ด, ํŠœํ”Œ์€ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ๋ชปํ•˜๊ณ , ๋ฆฌ์ŠคํŠธ์™€ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. >>> x = 1 >>> y = x >>> y += 3 >>> x >>> y ์œ„์—์„œ ๋‘ ๋ฒˆ์งธ ๋ผ์ธ๊นŒ์ง€๋Š” 1์ด๋ผ๋Š” ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ x์™€ y๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค. ์„ธ ๋ฒˆ์งธ์—์„œ y์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ˆœ๊ฐ„ y๋Š” 4๋ฅผ, x๋Š” 1์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค. C/C++ ๊ฐ™์€ ์–ธ์–ด ๊ด€์ ์—์„œ ๋ณด๋ฉด y=x๊ฐ€ ์‹คํ–‰ํ•˜๋Š” ์ˆœ๊ฐ„ ๊ฐ’์„ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์กฐ๊ธˆ ๋‹ค๋ฅธ ์ ์€ y=x๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ์‹œ์ ์—๋Š” ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋‹ค๊ฐ€ immutable ํƒ€์ž…์ธ y๋ฅผ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ ๋ณ€๊ฒฝ๋œ๋‹ค๋Š” ์ ์ด๋‹ค. id(obj)๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ณด๋‹ค ์ž์„ธํ•˜๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. >>> x = 1 >>> y = x >>> id(1) 140706521527120 >>> id(x) 140706521527120 >>> id(y) 140706521527120 >>> y += 3 >>> id(y) 140706521527216 id(obj)๋Š” ๊ฐ์ฒด์˜ ์œ ์ผํ•œ ์ˆซ์ž๋ฅผ ๋ฆฌํ„ดํ•˜๋Š”๋ฐ ํฌ์ธํ„ฐ๋กœ ์ดํ•ดํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค. ๋‹ค์Œ์€ ๋‹ค๋ฅธ immutable ํƒ€์ž…์ธ ๋ฌธ์ž์—ด(string)๊ณผ ํŠœํ”Œ( tuple)์˜ ์˜ˆ์ด๋‹ค. >>> x = 'abcd' >>> y = x >>> y += 'e' >>> x abcd >>> y abcde >>> x = (1,2,3) >>> y = x >>> y += (4, ) >>> x (1,2,3) >>> y (1,2,3,4) Mutable ํƒ€์ž…์€ ์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ปจํ…Œ์ด๋„ˆ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์•ž์„œ์—์„œ ํŠœํ”Œ(tuple)์€ ์ฝ๊ธฐ๋งŒ ๊ฐ€๋Šฅํ•œ ์ปจ ํ„ฐ์ด๋„ˆ์ด๊ธฐ ๋•Œ๋ฌธ์— immutable์ด๋‹ค. ๋‹ค์Œ์€ ๋ฆฌ์ŠคํŠธ์— ์ ์šฉํ•œ ์˜ˆ์ด๋‹ค. >>> x = [1,2,3] >>> y = x >>> y += [4, ] >>> x [1,2,3,4] >>> y [1,2,3,4] ์œ„์—์„œ ๋‘ ๋ฒˆ์งธ ์ค„๊นŒ์ง€ ์‹คํ–‰ํ•˜๋ฉด x, y๋Š” ๋ชจ๋‘ [1,2,3]์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค. ์ดํ›„ y๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด x ์—ญ์‹œ๋„ ๋ณ€๊ฒฝ๋˜๊ฒŒ ๋œ๋‹ค. ์ฆ‰, C/C++ ๊ด€์ ์—์„œ ๋ณด๋ฉด ํฌ์ธํ„ฐ ์—ฐ์‚ฐ ๋˜๋Š” ๋ ˆํ”„ ๋Ÿฐ์Šค ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•œ ๊ฒƒ๊ณผ ์œ ์‚ฌํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ปจํ…Œ์ด๋„ˆ๋Š” mutable์ด๋ฏ€๋กœ shallow copy ๊ฐ€ ๋˜๋Š” ๊ฒƒ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋ณต์‚ฌ Python์—์„œ immutable ์ž๋ฃŒํ˜•(์ˆซ์ž, ๋ฌธ์ž์—ด, ํŠœํ”Œ)์€ ์ง์ ‘ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋Š” deep copy๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ˜๋ฉด์— mutable ์ž๋ฃŒํ˜•(์ฆ‰, ์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ปจํ…Œ์ด๋„ˆ)๋Š” shallow copy(๋‚ด๋ถ€์ ์œผ๋กœ ํฌ์ธํ„ฐ๋งŒ ๋ณต์‚ฌ)๋ฅผ ์ ์šฉ๋œ๋‹ค. >>> a = [1,2,3] >>> b = a # b์™€ a๋Š” ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ด(shallow copy) >>> b is a True >>> b[1] = 10 # a = b = [10,11] >>> a = [5, 11] # a = [5,11] ์ด๋„๋ก ์ƒˆ๋กœ ์ง€์ •ํ•จ, b์™€ ์—ฐ๊ฒฐ์ด ๋Š์–ด์ง. >>> b is a False ์‹ค์ œ ๊ฐ’๊นŒ์ง€ ๋ณต์‚ฌ(deep copy) ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” object.copy()๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. >>> a = [1,2,3] >>> b = a.copy() >>> b is a False >>> b == a True ์œ„์—์„œ is ์™€ ==๋ฅผ ํ†ตํ•œ ๊ฒฐ๊ด๊ฐ’์ด ๋‹ค๋ฅธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. is๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์œ ์ง€ํ•˜๋Š” ํฌ์ธํ„ฐ ๊ฐ’์„ ๋น„๊ตํ•˜๊ณ , ==๋Š” list๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์„ฑ๋ถ„์„ ๋น„๊ตํ•œ๋‹ค. 2.2 ์ œ์–ด๋ฌธ C/C++ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ฒ˜๋Ÿผ Python์—๋„ if, for, while ๋“ฑ์˜ ์ œ์–ด ๊ตฌ๋ฌธ์„ ์ œ๊ณตํ•˜๋ฉฐ, break, continue์˜ ํ‚ค์›Œ๋“œ๋„ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค. 2.2.1 ์กฐ๊ฑด๋ฌธ if ๋ฌธ if a>0: print(a) else: print(a) if x < 0: print('negative') elif x < 10: print('0<=x<10') else: print('x>=10') switch ๋ฌธ Python์—๋Š” switch/case ๋ฌธ์ด ์—†๋‹ค. if ๋ฌธ์œผ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ์กฐ๊ฑด๋ฌธ if, for, while ๋ฌธ์—์„œ๋Š” ์กฐ๊ฑด๋ฌธ์ด ํฌํ•จ๋˜๊ฒŒ ๋œ๋‹ค. ์กฐ๊ฑด๋ฌธ์€ ๋น„๊ต์—ฐ์‚ฐ์ž์™€ ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž์˜ ์กฐํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ๋น„๊ต์—ฐ์‚ฐ์ž x < y, x > y, x == y, x is y, x != y, x >= y, x<=y ๋“ฑ์ด ์žˆ์œผ๋ฉฐ, C/C++์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค. ์œ ์ผํ•˜๊ฒŒ ๋‹ค๋ฅธ ๊ฒƒ์€ is ์—ฐ์‚ฐ์ž์ด๋‹ค. ==๊ฐ€ ๊ฐ’์ด ๊ฐ™์€ ๊ฒƒ์„ ๊ฒ€ํ† ํ•˜์ง€๋งŒ is๋Š” ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ๊ฐ™์€ ์ง€๋ฅผ ๋น„๊ตํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด numpy ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ ๊ทธ ๋ฐฐ์—ด์ด ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ๊ฒ€ํ† ํ•  ๋•Œ๋Š” is๋ฅผ, ๋‚ด๋ถ€์˜ ๊ฐ’์„ ๊ฒ€ํ† ํ•  ๋•Œ๋Š” ==๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. import numpy as np a = np.array([1,2,3]) b = a == 1 --> [True False False] if a is None: print('None array') ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž x or y, x and y, not x ๋“ฑ์ด ์žˆ๋‹ค. 2.2.2 ๋ฐ˜๋ณต๋ฌธ for ๋ฌธ range()๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ for i in range(10): print(i) ๋ฆฌ์ŠคํŠธ ๋ฉค๋ฒ„ ์ ‘๊ทผ xdata = [0.1, 4, 3] for x in xdata: print(x) ์ธ๋ฑ์Šค์™€ ๋ฆฌ์ŠคํŠธ ๋ฉค๋ฒ„ ๋™์‹œ ์ ‘๊ทผ : enumerate(list) ์‚ฌ์šฉ items = [9,5,4,10] for idx, val in enumerate(items): print(idx, val) while ๋ฌธ while True: key = input() if key == 'x': break print(key) list comprehesion size = 10 arr = [i * 2 for i in range(size)] 2.2.3 ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ 2.3 ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค ์–ด๋–ค ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์€ ๋ณดํ†ต ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด ์žฌ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. Python์—์„œ๋Š” def func(...): ํ˜•ํƒœ๋กœ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค. ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์ผ ๊ฒฝ์šฐ ํ•จ์ˆ˜๋งŒ์œผ๋กœ ํšจ์œจ์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด๋ฅผ ์ ˆ์ฐจ ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(procedure-oriented programming)์ด๋ผ๊ณ  ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ์˜ ๋ฉ์น˜๊ฐ€ ์ปค์ง€๊ณ  ๋ณต์žก ํ•ด์ง€๋งŒ ๊ฐ์ฒด์ง€ํ–ฅํ”„๋กœ๊ทธ๋ž˜๋ฐ(object-oriented programming)์ด ํ•„์š”ํ•ด์ง€๊ฒŒ ๋œ๋‹ค. ๊ฐ์ฒด์ง€ํ–ฅํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ•ด์„œ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ํ‹€์ธ ํด๋ž˜์Šค(class)๋ฅผ ์ •์˜ํ•  ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•˜๋‹ค. Python์—์„œ๋Š” class๋ผ๋Š” ํ‚ค์›Œ๋“œ๋กœ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. Python ๋‚ด์žฅ ํ•จ์ˆ˜ Python ์ž์ฒด์—์„œ ์ง์ ‘ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋‚ด์žฅํ•จ์ˆ˜(built-in function)์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์œ„ ํ•จ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ํ•จ์ˆ˜๋ฅผ ์™ธ์žฅ ํ•จ์ˆ˜๋ผ ๋ถ€๋ฅธ๋‹ค. 2.3.1 ํ•จ์ˆ˜ Python์—์„œ ํ•จ์ˆ˜๋Š” def function(โ€ฆ): ํ˜•ํƒœ๋กœ ์ •์˜ํ•œ๋‹ค. def add(a, b): return a+b def printSomething(a, b, c): print(a, b, c) def printLogo(): print('logo text ...') c=add(a, b) printSomething(1,2,3) printLogo() ์œ„์—์„œ add(a, b)๋Š” ๋ฆฌํ„ด ๊ฐ’์ด ์žˆ๋Š” ํ•จ์ˆ˜์ด๊ณ , printSomething()๊ณผ printLogo()๋Š” ์—†๋‹ค. printLogo()์ฒ˜๋Ÿผ ์ธ์ž๊ฐ€ ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค. ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด ๊ฐ’ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋ฆฌํ„ดํ•  ๋•Œ๋Š” ํŠœํ”Œ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค. ๊ฐ’์„ ๋ฐ›์„ ๋•Œ๋Š” ํ•œ ๊ฐœ์˜ ๋ณ€์ˆ˜๋กœ ๋ฐ›์„ ์ˆ˜๋„ ์žˆ๊ณ  unpack ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด ๋ฆฌํ„ด ๊ฐ’์œผ๋กœ ํŠœํ”Œ์„ ๋ชจ์•„๋„ ๋œ๋‹ค. def computeProps(n, p, q): ... return (a, b, c) r = computeProps(...) (a, b, c) = computeProps(...) # unpack ์ด์šฉ a, b, c = computeProps(...) # ์œ„์™€ ๋™์ผ Immutable๊ณผ Mutable ํƒ€์ž…์˜ ์ธ์ž Immutable ์ž๋ฃŒํ˜•( ์ˆซ์ž, ๋ฌธ์ž์—ด ๋“ฑ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•๊ณผ ์“ฐ๊ธฐ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ปจํ…Œ์ด๋„ˆ์ธ tuple)์€ ํ•จ์ˆ˜ ๋‚ด์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋”๋ผ๊ณ  ํ˜ธ์ถœ ์ธก์—์„œ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, call by value๋กœ ์ธ์ž๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค. def foo(a, b): a = 100 b = 200 a = 1 b = 2 foo(a, b) print(a, b) # 1,2 ์ถœ๋ ฅ ์œ„ ์ฝ”๋“œ์—์„œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ธ์ž์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋”๋ผ๊ณ  a, b ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค. Mutable ์ž๋ฃŒํ˜•(์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ปจํ…Œ์ด๋„ˆ์ธ list, dictionary, ndarray ๋“ฑ)์ด ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ ํ•จ์ˆ˜์•ˆ์—์„œ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. C/C++ ๊ด€์ ์—์„œ ๋ณด๋ฉด call by reference๋กœ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. def foo(x): x.append(100) x = [1,2,3] foo(x) print(x) # [1,2,3,100] ์œ„์—์„œ ์ธ์ž๋กœ ๋„˜๊ฒจ์ง„ x๋Š” mutable ์ž๋ฃŒํ˜•์ธ ๋ฆฌ์ŠคํŠธ์ด๋ฏ€๋กœ, ํ•จ์ˆ˜ ๋‚ด์—์„œ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ดํ›„์—๋„ ๊ฐ’์ด ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค. ์ธ์ž๋กœ mutable ์ž๋ฃŒํ˜•์„ ์ „๋‹ฌํ–ˆ์„ ๋•Œ๋Š” ํ•จ์ˆ˜ ๋‚ด์—์„œ ๊ฐ’์ด ๋ฐ”๋€Œ๋Š”์ง€๋ฅผ ์ฃผ์˜ํ•ด์•ผ ํ•˜์ง€๋งŒ, ๋‹ค๋ฅธ ํ•œํŽธ์œผ๋กœ๋Š” ๋ฆฌํ„ด ๊ฐ’์ด ์•„๋‹Œ ์ธ์ž๋กœ ๊ฐ’์„ ๋ฐ›์•„์˜ค๋„๋ก ์„ค๊ณ„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹ค์Œ์€ f(x, y) = x^2 + y^2๋ผ๋Š” ์ˆ˜ํ•™ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ํ•จ์ˆซ๊ฐ’๊ณผ ๊ทธ๋ž˜๋”” ์–ธํŠธ๋ฅผ ๋ฐ›์•„์˜ค๋„๋ก ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. import numpy as np def myfunc(x, grad): f = x[0]*x[0] + x[1]*x[1] grad[0] = 2*x[0] grad[1] = 2*x[1] return f x = np.array([0.5,0.1]) grad = np.zeros(2) f = myfunc(x, grad) print(f) # 0.26 print(grad) # array([1.,0.2]) ๋””ํดํŠธ ์ธ์ž์˜ ์ง€์ • ๋””ํดํŠธ ์ธ์ž์˜ ์ง€์ • ์—ญ์‹œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. def computeIgIcr(n, b, h, Ast, dt, Asc, dc, opt='exact'): โ€ฆ Ig = computeIg(n, b, h, h, Ast, dt, Asc, dt) ๋˜๋Š” Ig = computeIg(n, b, h, h, Ast, dt, Asc, dt,โ€™apprโ€™) *arg์™€ **kwargs ์ธ์ž์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ •ํ•ด์ง€์ง€ ์•Š์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด *arg์™€ **kwargs์ด๋‹ค. def add(*args): r = 0 for v in args: r = v+i return r >>> r = add(1,2,3) >>> print(r) *args์—์„œ *๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ธ์ž๋ฅผ ํŠœํ”Œ๋กœ ๋ฌถ์–ด args ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๊ฒŒ ๋œ๋‹ค. ์œ„์—์„œ arg=(1,2,3) ํ˜•ํƒœ๋กœ ์ „๋‹ฌ๋˜๊ฒŒ ๋œ๋‹ค. ์ผ๋ฐ˜ ์ธ์ž์™€๋„ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ํ•ญ์ƒ ๋’ค์— ์™€์•ผ ํ•œ๋‹ค. def foo(x,*args) ํ˜•ํƒœ์ด๊ณ , foo(1,2,3,4,5)๋กœ ํ˜ธ์ถœํ–ˆ๋‹ค๋ฉด x=1, args=(2,3,4,5)๋กœ ์ „๋‹ฌํ•˜๋Š” ์‹์ด๋‹ค. ๋น„์Šทํ•œ ๊ฒƒ์œผ๋กœ **kwargs๊ฐ€ ์žˆ๋‹ค. ์ด๋Š” keyword arguments๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ keyword1=value1, keyword2=value2, ... ํ˜•ํƒœ๋ฅผ dictionary๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค. def myfunc(**kwargs): print(kwargs) for key, value in kwargs.items(): print(key,'=',value) >>> myfunc(a=1, b=2, c=3) {'a': 1, 'b': 2, 'c': 3} a = 1 b = 2 c = 3 *args์™€ **kwargs๋Š” ์ผ๋ฐ˜ ์ธ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ˆœ์„œ๋Š” ํ•ญ์ƒ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. def some_func(fargs,*args,**kwargs): pass ๋ณ€์ˆ˜์˜ ๋ฒ”์œ„(scope) ํ•จ์ˆ˜ ๋‚ด์— ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜๋Š” local scope๋ฅผ ๊ฐ–๋Š”๋‹ค. ํ•จ์ˆ˜ ๋‚ด ํ•จ์ˆ˜ Python์€ C/C++๊ณผ ๋‹ฌ๋ฆฌ ํ•จ์ˆ˜ ๋‚ด์—์„œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•จ์ˆ˜ ๋‚ด ํ•จ์ˆ˜๋Š” ์ž์‹ ์„ ๋‘˜๋Ÿฌ์‹ผ ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜๋ฅผ ๋งˆ์น˜ ์ „์—ญ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜๊ธฐ์— ๋”ฐ๋ผ ๋งค์šฐ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. def outside(): outsideList = [1, 2] def nested(): outsideList.append(3) nested() print outsideList 2.3.2 ํด๋ž˜์Šค Python์˜ ํด๋ž˜์Šค๋Š” class ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •์˜ํ•œ ์˜ˆ์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€<NAME>์„ ์ง€๋‹Œ๋‹ค. class Simple: pass ์œ„์™€ ๊ฐ™์ด ์ •์˜ํ•œ ํ›„ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. >>> a = Simple() >>> a.name = 'Jane' >>> a.phone = '123-456-7890' ๋‹ค์Œ์€ ๋ณด๋‹ค ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ๋ฒ•์„ ์˜ˆ์‹œํ•œ ๊ฒƒ์ด๋‹ค. class Account: numOfAccount = 0 def __init__(self, name): self.name = name; self.balances = 0 Account.numOfAccount += 1 def withdraw(self, value): self.balances -= value return self.balances def deposit(self, value): self.balances += value return self.balances def __del__(self): Account.numOfAccount -= 0 >>> a1 = Account('John') >>> a1.deposit(10) 10 >>> a1.withdraw(2) >>> print(a1.balances) >>> a2 = Account('Jane') >>> print('no of Account : ',Account.numOfAccount) Account ํด๋ž˜์Šค๋Š” ํด๋ž˜์Šค ๋‹จ์œ„๋กœ ์ •์˜ํ•œ ๋ณ€์ˆ˜(ํด๋ž˜์Šค ๋ณ€์ˆ˜, C++์˜ static variable๊ณผ ๋™์ผ)์ธ numOfAccount, ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(C++์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜) name, balances, ์ƒ์„ฑ์ž __init__(), ์†Œ๋ฉธ์ž __del__(), ์ผ๋ฐ˜ ๋ฉ”์˜๋“œ withdraw(), deposit() ๋“ฑ์„ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. self๋Š” C++ ํด๋ž˜์Šค ์ •์˜ ์‹œ ์ƒ๋žต๋˜๋Š” this ํฌ์ธํ„ฐ์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค. ๋˜ํ•œ Python์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฉค๋ฒ„์™€ ๋ฉ”์˜๋“œ๊ฐ€ public ์†์„ฑ์„ ์ง€๋‹Œ๋‹ค. ๋งŒ์•ฝ private์œผ๋กœ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ๋‘ ๊ฐœ์˜ ๋ฐ‘์ค„ __๋กœ ์‹œ์ž‘ํ•˜๋„๋ก ์ด๋ฆ„์„ ์ •์˜ํ•˜๋ฉด ๋œ๋‹ค. ๋ฉ”์˜๋“œ์— self ์ธ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋Š” C++์˜ static member์™€ ๋™์ผํ•˜๋‹ค. ์ด ํ•จ์ˆ˜์—์„œ๋Š” ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์กฐ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋‹จ์ˆœํžˆ namespace๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. class Account: numOfAccount = 0 ... def makeZero(number): Account.numOfAccount = number Python์˜ ํด๋ž˜์Šค์—์„œ๋„ ์ƒ์†(inheritance)๊ณผ ๊ฐ€์ƒ ํ•จ์ˆ˜ ๋“ฑ์„ ์ง€์›ํ•œ๋‹ค. ๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ ์˜ˆ์ด๋‹ค. class Element: def __init__(self, id): self.id = id self.nodeIds = [] def computeStiffness(self): print('Element::computeStiffness') def printElement(self): print('id : %d'%self.id) class Q4Element(Element): def __init__(self, id, nodeIds): super().__init__(id) # or Element.__init__(self, id) self.nodeIds = nodeIds def computeStiffness(self): print('Q4Element::computeStiffness') >>> e = Q4Element(1, [1,2,3]) >>> e.printElement() id : 1 >>> e.computeStiffness() Q4Element::computeStiffness ์œ„์—์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์ธ __init__()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‘ ๊ฐ€์ง€์ด๋‹ค. super().__init__(id) ๋“ฑ๊ณผ ๊ฐ™์ด self๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ Element.__init__(self, id)์™€ ๊ฐ™์ด ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๊ณ  ๋ฉ”์˜๋“œ์— self๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์„ ์ผ๋ฐ˜ ๋ฉ”์˜๋“œ์—์„œ๋„ ์„ฑ๋ฆฝํ•œ๋‹ค. ๋˜ํ•œ ์ด๋ฆ„์ด ๊ฐ™์€ ๋ฉ”์˜๋“œ๊ฐ€ ๊ฐ€์ƒ ํ•จ์ˆ˜๊ฐ€ ๋œ๋‹ค. 2.3.3 ์ •์  ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ๋‹ค์ค‘ ์ƒ์„ฑ Python ํด๋ž˜์Šค๋Š” keyword ์ž…๋ ฅ์„ ํ—ˆ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์ค‘ ์ƒ์„ฑ์ž๋ฅผ ์“ฐ๊ธฐ ์‰ฝ์ง€ ์•Š๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” variable ์ž…๋ ฅ์„ ๋ฐ›๋“ ์ง€, ์ •์  ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฉ๋ฒ• 1 class A: def __init__(self, *arg,**karg): ... arg์™€ karg๋ฅผ ๋ถ„์„ ์ •์  ํ•จ์ˆ˜ class Rectangle def __init__(self): self.a = None self.b = None def fromWithHeight(a, b): r = A() r.a = a r.b = b return r def fromArea(a, area): r = Rectangle() r.a = a r.b = area/a return r 2.5 ๋ชจ๋“ˆ๊ณผ ํŒจํ‚ค์ง€ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ™œ์šฉ์ด ํ•„์š”ํ•˜๋ฉฐ, ํ”„๋กœ๊ทธ๋žจ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง์— ๋”ฐ๋ผ ์ž์‹ ์ด ์ง์ ‘ ์ž‘์„ฑํ•œ ๋ถ€๋ถ„๋„ ๋ช‡๋ช‡ ๋ถ€๋ถ„์œผ๋กœ ์†Œ์Šค๋ฅผ ๋‚˜๋ˆ„์–ด ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. Python์—์„œ ์—ฌ๊ธฐ์— ๋Œ€์‘ํ•˜๋Š” ๊ฐœ๋…์ด ๋ชจ๋“ˆ(module)๊ณผ ํŒจํ‚ค์ง€(package)์ด๋‹ค. ๋ชจ๋“ˆ(module)์€ ์žฌ์‚ฌ์šฉํ•  ๋ชฉ์ ์œผ๋กœ ์ž‘์„ฑ๋œ Python ์†Œ์Šค ํŒŒ์ผ์„ ์˜๋ฏธํ•˜๋ฉฐ, import ๋ฌธ์œผ๋กœ ๋กœ๋”ฉํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด os ๋ชจ๋“ˆ์€ os.py๋ผ๋Š” ํŒŒ์ผ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ ์‚ฌ์šฉ ์‹œ์—์„œ๋Š” import os ๋“ฑ์œผ๋กœ ๋กœ๋”ฉํ•œ๋‹ค. ๋‹ค์Œ์€ ์‚ฌ์šฉ ์˜ˆ์ด๋‹ค. >>> import os >>> os.getcwd() 'D:\\DevProg\\Python\\samples' >>> os.chdir(r'D:\DevProg\Python') abs(), complex(), int() , list(), ord() ๋“ฑ์˜ ๋‚ด์žฅํ•จ์ˆ˜(built-in function)๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋“ค์€ ํ•ญ์ƒ ๋ชจ๋“ˆ์— ํฌํ•จ๋œ๋‹ค. math ๋“ฑ์˜ ์ผ๋ถ€ ๋‚ด์žฅ ๋ชจ๋“ˆ(built-in module)์€ Python ์–ธ์–ด ์ž์ฒด์— ํฌํ•จ๋˜์–ด ์žˆ์ง€๋งŒ ๋‚˜๋จธ์ง€ ๋ชจ๋“ˆ/ํŒจํ‚ค์ง€๋Š” ๋ณ„๋„ ํŒŒ์ผ ํ˜•ํƒœ๋กœ ์กด์žฌํ•œ๋‹ค. ๋‹ค์Œ์€ ๋‚ด์žฅ ๋ชจ๋“ˆ์„ ํ™•์ธ ์ฝ”๋“œ์ด๋‹ค. >>> import sys >>> sys.builtin_module_names ('_ast', '_bisect', '_blake2', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_csv', '_datetime', '_findvs', '_functools', '_heapq', '_imp', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_opcode', '_operator', '_pickle', '_random', '_sha1', '_sha256', '_sha3', '_sha512', '_signal', '_sre', '_stat', '_string', '_struct', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', '_winapi', 'array', 'atexit', 'audioop', 'binascii', 'builtins', 'cmath', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'parser', 'sys', 'time', 'winreg', 'xxsubtype', 'zipimport', 'zlib') ํŒจํ‚ค์ง€(package)๋Š” ๋ชจ๋“ˆ์„ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ๋กœ ๋ฌฝ์–ด๋†“์€ ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด os ๋ชจ๋“ˆ์€ os.py๋ผ๋Š” ํŒŒ์ผ๋กœ ์กด์žฌํ•˜์ง€๋งŒ ๋Œ€ํ‘œ์  ์ˆ˜์น˜ ํŒจํ‚ค์ง€์ธ NumPy๋Š” numpy๋ผ๋Š” ํด๋” ๋‚ด์—์„œ ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์ด ์กด์žฌํ•œ๋‹ค. ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Python ์ธ์Šคํ†จ ์‹œ ์ œ๊ณต๋˜๋Š” ๋‚ด์žฅํ•จ์ˆ˜, ๋‚ด์žฅ๋ชจ๋“ˆ, ์™ธ์žฅ ๋ชจ๋“ˆ ๋“ฑ์„ ์˜๋ฏธํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. abs() : ๋‚ด์žฅ ํ•จ์ˆ˜ os.getcwd() : ์™ธ์žฅ ํ•จ์ˆ˜. ๋ชจ๋“ˆ os์— ์ •์˜๋œ ์™ธ์žฅ ํ•จ์ˆ˜ math ๋ชจ๋“ˆ : ๋‚ด์žฅ ๋ชจ๋“ˆ os ๋ชจ๋“ˆ : ์™ธ์žฅ ๋ชจ๋“ˆ์ด๋‚˜ ํ‘œ์ค€ ๋ชจ๋“ˆ. C:\ProgramData\Anaconda3\Lib\os.py๋กœ ์ œ๊ณต NumPy ํŒจํ‚ค์ง€ : ์™ธ์žฅ ํŒจํ‚ค์ง€์ด๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ธ์Šคํ†จํ•ด์•ผ ํ•จ. C:\ProgramData\Anaconda3\Lib\site-packages\numpy์— ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์ด ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ์Œ. 2.5.1 ๋ชจ๋“ˆ ๋ชจ๋“ˆ ์ž‘์„ฑ๊ณผ ์‚ฌ์šฉ ๋ชจ๋“ˆ(module)์€ ๋‹จ์ˆœํžˆ ์žฌ์‚ฌ์šฉ์„ ์—ผ๋‘์— ๋‘๊ณ  ์ž‘์„ฑํ•œ Python ์†Œ์ŠคํŒŒ์ผ์„ ์˜๋ฏธํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด mysolve.py๋ผ๋Š” ํŒŒ์ผ์— Newton ๋ฒ•๊ณผ bisection ๋ฒ•์œผ๋กœ ๋ฐฉ์ •์‹์˜ ํ•ด๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ธฐ๋กœ ํ•œ๋‹ค. mysolver.py tol = 1E-10 maxiter = 50 def solve_by_newton(func, der, x0): """ solve equation by newton method """ x = x0 for i in range(maxiter): f, df = func(x), der(x) if abs(f) < tol: return x x = x-f/df return None def solve_by_bisection(func, lb, ub): """ solve equation by bisection method """ for i in range(maxiter): x = (lb+ub)/2. if func(x) == 0 or (ub-lb)/2 < tol: return x elif func(lb)*func(x) < 0: ub = x else: lb = x return None mysolver.py๊ฐ€ ์žˆ๋Š” ํด๋”์—์„œ python์„ ์‹คํ–‰ํ–ˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. import mysolver def func(x): return x*x-2*x-4 def der(x): return 2*x-2 # f(x), f'(x) xNewton = mysolver.solve_by_newton(func, der, 10) xBisection = mysolver.solve_by_bisection(func, 0,10) print('tol = ',mysolver.tol) print('x = ',xNewton, ', ', xBisection) ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ™•์žฅ์ž. py๋ฅผ ๋บ€ ํŒŒ์ผ ์ด๋ฆ„์œผ๋กœ import module ํ˜•ํƒœ๋กœ ๋กœ๋”ฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ์ฝ”๋“œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋“ฏ์ด ๋ชจ๋“ˆ ๋‚ด์˜ ํ•จ์ˆ˜, ๋ณ€์ˆ˜, ํด๋ž˜์Šค ๋“ฑ์€ module.xxx ๋“ฑ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋•Œ module์€ namespace ์—ญํ•™์„ ํ•œ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ€์ง€ import ๋ฌธ์˜ ๋ณ€ํ˜•์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹ค์Œ์€ ๋‹ค์–‘ํ•œ ๋ณ€ํ˜•์„ ๋‚˜์—ดํ•œ ๊ฒƒ์ด๋‹ค. from mysolver import * # mysolve ๋ชจ๋“ˆ์˜ ๋ชจ๋“  ๊ฒƒ์„ ์ž„ํฌํŠธ xNewton = solve_by_newton(func, der, 10) from mysolver import solve_by_newton, tol # mysolve ๋ชจ๋“ˆ์—์„œ solve_by_newton๊ณผ tol๋งŒ ์ž„ํฌํŠธ xNewton = solve_by_newton(func, der, 10) print(tol) import mysolver as solver # ๋ชจ๋“ˆ์˜ ์ด๋ฆ„์„ solver๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‚ฌ์šฉํ•  ๋•Œ xNewton = solver.solve_by_newton(func, der, 10) from mysolver import solve_by_newton as solve # ๋ชจ๋“ˆ์˜ ์ผ๋ถ€ ํ•จ์ˆ˜๋ฅผ ์ž„ํฌํŠธ ํ•˜๋ฉด์„œ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•  ๋•Œ xNewton = solve(func, der, 10) ๋ชจ๋“ˆ ์ฐพ๊ธฐ ๊ฒฝ๋กœ Python์—์„œ import ๋ฌธ์œผ๋กœ ์ฃผ์–ด์ง„ ๋ชจ๋“ˆ์€ ๋‹ค์Œ์˜ ์ˆœ์„œ๋กœ ์ฐพ๋Š”๋‹ค. ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ(current working directory) Python ์ธ์Šคํ†จ ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ๊ทธ ํ•˜์œ„์˜ lib/site-packages ๋””๋ ‰ํ„ฐ๋ฆฌ (Python ์ธํ„ฐ ๋ฆฌํ”„ํ„ฐ๋งˆ๋‹ค ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฆ„) ํ™˜๊ฒฝ ๋ณ€์ˆ˜ PYTHONPATH์— ์ง€์ •๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด ๋ชจ๋“ˆ์„ ์ฐพ๋Š” ์œ„์น˜๋Š” sys ๋ชจ๋“ˆ์—์„œ sys.path๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. >>> import sys >>> sys.path ['', 'C:\\ProgramData\\Anaconda3\\Scripts', 'C:\\ProgramData\\Anaconda3\\python36.zip', 'C:\\ProgramData\\Anaconda3\\DLLs', 'C:\\ProgramData\\Anaconda3\\lib', 'C:\\ProgramData\\Anaconda3', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\Babel-2.5.0-py3.6.egg', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\jrcho\\.ipython'] ์œ„์—์„œ ์ฒซ ๋ฒˆ์งธ ๋ชจ๋“ˆ ์ฐพ๊ธฐ ๊ฒฝ๋กœ๋กœ ''์ธ ์ ์— ์ฃผ๋ชฉํ•˜์ž. ์ด ์œ„์น˜๋Š” python์˜ working directory๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰, python์„ ์‹คํ–‰ํ•œ ๊ฒฝ๋กœ์ด๋‹ค. ๋งŒ์•ฝ import os, os.chdir(somefolder) ๋“ฑ์œผ๋กœ working directry๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ os.getcwd()๋กœ ์กฐํšŒ๋˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋‹ค. ์ด ๋•Œ๋ฌธ์— ์•ž์„œ ์˜ˆ์ œ์—์„œ mpsolver.py๋ฅผ import mysolver๋กœ ๊ตฌ๋™ํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ์ด๋‹ค. ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ๋กœ ๋‚˜๋ˆ„์–ด ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ(1) ๋ช‡๋ช‡ ๋ชจ๋“ˆ(ํŒŒ์ผ)๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ํ•˜๋‚˜์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ผ๋ฐ˜์ ์ด๋‹ค. D:\Prog\MyProg - myprog.py - myutil.py - geom - show_geom.py - construct.py ์œ„์™€ ๊ฐ™์€ ์˜ˆ์—์„œ myprog.py๊ฐ€ main ํ•จ์ˆ˜ ์—ญํ• ์„ ํ•˜๊ณ , myutil.py๋‚˜ geom/show_geo.py ๋“ฑ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“ˆ์ด๋ผ๊ณ  ์ƒ๊ฐ๊ธฐ๋กœ ํ•œ๋‹ค. myprog.py๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ํ˜„ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ working directory์ด๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•˜๋ฉด ๋œ๋‹ค. import myutil from geom import construct .... ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ๋กœ ๋‚˜๋ˆ„์–ด ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ(2) ๋‘ ๋ฒˆ์งธ๋Š” ๋ฉ”์ธ ๋ชจ๋“ˆ์„ ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋‘๋Š” ๊ฒฝ์šฐ์ด๋‹ค. ์ด ๊ฒฝ์šฐ๋„ค๋Š” sys.path์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. D:\Prog\MyProg - main_module/myprog.py, myutil.py - geom/ show_geom.py, construct.py ์œ„์™€ ๊ฐ™์€ ์˜ˆ์—์„œ myprog.py๊ฐ€ main ํ•จ์ˆ˜ ์—ญํ• ์„ ํ•˜๊ณ , myutil.py๋‚˜ geom/show_geo.py ๋“ฑ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“ˆ์ด๋ผ๊ณ  ์ƒ๊ฐ๊ธฐ๋กœ ํ•œ๋‹ค. myprog.py๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ํ˜„ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ working directory์ด๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•˜๋ฉด ๋œ๋‹ค. import sys sys.path.append('../geom) import myutil from geom import construct .... ์œ„์—์„œ sys.path์—์„œ ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ์ ์— ์ฃผ๋ชฉํ•œ๋‹ค. ์ž์‹ ๋งŒ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌํ™”๋œ ๋ชจ๋“ˆ/ํŒจํ‚ค์ง€ ์ž‘์„ฑํ•œ ๋ชจ๋“ˆ/ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ปดํ“จํ„ฐ ๋‹จ์œ„์—์„œ ๊ณต์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ชจ๋“ˆ์˜ ์œ„์น˜๋ฅผ PYTHONPATH๋‚˜ sys.path์— ๋“ฑ๋กํ•ด์•ผ ํ•œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด D:\Prog\Lib ๋‚ด์— ์ž์‹ ๋งŒ์˜ ๋ชจ๋“ˆ์ด๋‚˜ ํŒจํ‚ค์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ธฐ๋กœ ํ•œ๋‹ค. D:\Prog\Lib - myutils.py - mygraphics.py - geom - __init__.py - show_geom.py - construct.py ์ด์ œ ๋ชจ๋“ˆ์˜ ์œ„์น˜๋ฅผ PYTHONPATH๋‚˜ sys.path์— ๋“ฑ๋กํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค. (1) ํ™˜๊ฒฝ ๋ณ€์ˆ˜ PYTHONPATH์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• : ํ•ญ์ƒ ์ถ”๊ฐ€๋œ๋‹ค. import myutils as utils area = utils.compute_area([0,0,1,0,2,1) (2) sys.path์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• : ํ˜„์žฌ Python ํ”„๋กœ์„ธ์Šค์—์„œ๋งŒ ์œ ํšจํ•˜๋‹ค. import sys sys.path.append(r'D:\Prog\Lib') import myutils as utils area = utils.compute_area([0,0,1,0,2,1) ์—ฌ๊ธฐ์—์„œ sys.path์— ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ์ ์— ์ฃผ๋ชฉํ•œ๋‹ค. 2.5.2 ํŒจํ‚ค์ง€ ํŒจํ‚ค์ง€(package)๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ํ˜•ํƒœ๋กœ ๊ด€๋ จ ๋ชจ๋“ˆ์„ ๋ชจ์•„๋‘” ๊ฒƒ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณด๋‹ค ์ „๋ฌธ์ ์œผ๋กœ ์ด์•ผ๊ธฐํ•˜๋ฉด ์ (.)์œผ๋กœ ๋ชจ๋“ˆ ์ด๋ฆ„์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์กฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์˜๋ฏธํ•œ๋‹ค. ๋‹ค์Œ์€ Python ๊ณต์‹ ํŠœํ† ๋ฆฌ์–ผ์— ์†Œ๊ฐœ๋œ ์„ค๋ช…์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. ์‚ฌ์šด๋“œ ๊ด€๋ จ ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ธฐ๋กœ ํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŒŒ์ผ ๊ตฌ์กฐ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. sound/ Top-level package __init__.py Initialize the sound package formats/ Subpackage for file format conversions __init__.py wavread.py wavwrite.py auread.py auwrite.py ... effects/ Subpackage for sound effects __init__.py echo.py surround.py reverse.py ... filters/ Subpackage for filters __init__.py equalizer.py vocoder.py karaoke.py ... ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ (.)์ด ์žˆ๋Š” ๋ชจ๋“ˆ๋ช… import ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด echo.py์— echofilter(...)๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. import sound.effects.echo sound.effects.echo.echofilter(input, output, delay=0.7, atten=4) from sound.effects import echo echo.echofilter(input, output, delay=0.7, atten=4) from sound.effects.echo import echofilter echofilter(input, output, delay=0.7, atten=4) from sound.effects.echo import * echofilter(input, output, delay=0.7, atten=4) ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ ๋ถ€ ํŒจํ‚ค์ง€(sub-package) ์ž์ฒด๋ฅผ ์ž„ํฌํŠธ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ฆ‰, ์›์น™์ ์œผ๋กœ from module import * ํ˜•ํƒœ ๋Œ€์‹  from package import * ๋“ฑ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ ค๋ฉด __init__.py์— __all__ ์†์„ฑ์— ๋ชจ๋“ˆ๋ช…์„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋‚˜์—ดํ•ด์•ผ๋งŒ ํ•œ๋‹ค. sound/effects/init.py __all__ = ["echo", "surround", "reverse"] ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด from package import *ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. from sound.effects import * echo.echofilter(input, output, delay=0.7, atten=4) 2.6 ํŒŒ์ผ ์ž…์ถœ๋ ฅ Python์˜ ํŒŒ์ผ ์ž…์ถœ๋ ฅ์€ C/C++์˜ stdio.h์— ์ •์˜๋œ ๋ฐฉ์‹๊ณผ ์œ ์‚ฌํ•˜๋‹ค. ๋‹ค์Œ์€ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ฝ์–ด strlist๋ผ๋Š” str์˜ list์— ์ €์žฅํ•œ ํ›„ ์ถœ๋ ฅํ•œ ์˜ˆ์ด๋‹ค. f = open('mytext.txt','rt') # f = open('mytext.txt') ์™€ ๋™์ผ strlist = f.readlines() f.close() print(strlist) f = open(file, mode='r',encoding=None,...)์œผ๋กœ ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ์ดํ›„ ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํŒŒ์ผ์„ ๋‹ค๋ฃฌ๋‹ค. mode๋Š” C์˜ fopen()์ฒ˜๋Ÿผ ์ฝ๊ธฐ/์“ฐ๊ธฐ/์ถ”๊ฐ€(๊ฐ๊ฐ 'r', 'w', 'a'), ํ…์ŠคํŠธ ํŒŒ์ผ/์ด์ง„ ํŒŒ์ผ(๊ฐ๊ฐ 't','b') ๋“ฑ๊ณผ ๊ฐ™์ด ํŒŒ์ผ ์˜คํ”ˆ ๋ชจ๋“œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ง€์ •ํ•œ๋‹ค. ๋””ํดํŠธ๋Š” ์ฝ๊ธฐ('r')์™€ ํ…์ŠคํŠธ ํŒŒ์ผ('t')์ด๋‹ค. ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ํŒŒ์ผ์„ ์—ด๋ฉด ํŒŒ์ผ์— ์ž…์ถœ๋ ฅ์€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด์ธ str ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋•Œ ํ…์ŠคํŠธ ์ธ์ฝ”๋”ฉ(encoding)๊ณผ ๋””์ฝ”๋”ฉ(decoding)์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค. ์ด์ง„ ํŒŒ์ผ๋กœ ํŒŒ์ผ์„ ์—ด๋ฉด ํŒŒ์ผ์— ์ž…์ถœ๋ ฅ์€ bytes ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค. bytes ๊ฐ์ฒด๋Š” ๋‹จ์ˆœํžˆ 1 ๋ฐ”์ดํŠธ ๋‹จ์œ„์˜ ๋ฐฐ์—ด(๋˜๋Š” ASCII ๋ฌธ์ž์—ด)๋กœ, ํŒŒ์ผ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ณ€ํ™˜ ์—†์ด ์ž…์ถœ๋ ฅํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. encoding์€ ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ํŒŒ์ผ์„ ์—ด ๋•Œ๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ์œผ๋ฉฐ, ๋””ํดํŠธ์ธ None์€ ์‹œ์Šคํ…œ์˜ ๋””ํดํŠธ ์ธ์ฝ”๋”ฉ(Windows์˜ ๊ฒฝ์šฐ cp949)์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ํŒŒ์ผ ์ž…์ถœ๋ ฅ ๊ด€๋ จ ์ฃผ์š” ํ•จ์ˆ˜ ์ด์ง„ ํŒŒ์ผ์€ io.BufferedReader ๋˜๋Š” io.BufferedWriter๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋ฉฐ, ํ…์ŠคํŠธ ํŒŒ์ผ์€ io.TextIOWrapper๋ผ๋Š” ํด๋ž˜์Šค ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋ฐ ๋ชจ๋‘ io.IOBase๋ผ๋Š” ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋‹ค. ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ๋•Œ ๋ณต์žกํ•œ ํด๋ž˜์Šค ์ƒ์†์— ๋Œ€ํ•œ ์ง€์‹์€ ํ•„์š” ์—†์œผ๋ฉฐ, ๋‹จ์ง€ File Object์— ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ฃผ์š”ํ•œ ํ•จ์ˆ˜์„ ๊ธฐ์–ตํ•˜๋ฉด ๋œ๋‹ค. f = open(file, mode='r',encoding=None,...) : ํŒŒ์ผ ์—ด๊ธฐ f.name : ํŒŒ์ผ ์ด๋ฆ„ f.mode : ํŒŒ์ผ ์—ด๊ธฐ ๋ชจ๋“œ ์กฐํšŒ f.encoding : ํ˜„์žฌ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ์ธ์ฝ”๋”ฉ(ํ…์ŠคํŠธ ํŒŒ์ผ์—์„œ๋งŒ ์œ ํšจ) f.close() : ๋‹ซ๊ธฐ. ๊ฐ•์ œ๋กœ ์‚ฌ์šฉ์ด ๋๋‚˜ ํŒŒ์ผ์„ ๋‹ซ๋Š”๋‹ค. ์ด๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„ ํŒŒ์ผ ๊ฐ์ฒด๊ฐ€ ํŒŒ๊ดด๋  ๋•Œ ํŒŒ์ผ์„ ๋‹ซ๋Š”๋‹ค. f.flush() : ๋ฒ„ํผ ์ฆ‰์‹œ ๋น„์šฐ๊ธฐ f.closed : ํŒŒ์ผ์ด ๋‹ซํ˜”๋Š”์ง€๋ฅผ True, False๋กœ ๋ฆฌํ„ด f.readable(), f.writable() : ์ฝ๊ฑฐ๋‚˜ ์“ธ ์ˆ˜ ์žˆ๋Š”์ง€ True, False๋กœ ๋ฆฌํ„ด f.seek(offset, whence=SEEK_SET), f.tell() : ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ ์„ค์ •. ๊ธฐ์ค€ ์œ„์น˜ whence์— SEEK_SET(0), SEEK_CUR(1),SEEK_END(2) ์ง€์ • ๊ฐ€๋Šฅ. offset์ด ์Œ์ˆ˜์ด๋ฉด ๊ธฐ์ค€ ์œ„์น˜๋กœ๋ถ€ํ„ฐ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ์œ„์น˜ ์ง€์ • f.tell() : ํŒŒ์ผ ํฌ์ธํ„ฐ ์œ„์น˜ ์กฐํšŒ f.read([size]),f.readline([size]), f.readlines([sizehint]): ์ฝ๊ธฐ ํ•จ์ˆ˜. f.write(str), f.writelines(sequenc) : ์“ฐ๊ธฐ ํ•จ์ˆ˜ with ๊ตฌ๋ฌธ์˜ ์‚ฌ์šฉ ์‚ฌ์šฉ์ด ๋๋‚œ ํŒŒ์ผ ๊ฐ์ฒด๋Š” f.close()๋กœ ๋‹ซ๊ณ  ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ with ๊ตฌ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๊ตฌ๋ฌธ ๋‚ด์—์„œ๋งŒ ์œ ํšจํ•œ ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. with open('test.txt') as f: strlist = f.readlines() process strlist... ์œ„์—์„œ f.close()๋กœ ๋ช…์‹œ์ ์œผ๋กœ ํŒŒ์ผ์„ ๋‹ซ์ง€ ์•Š์•„๋„ with ๋ธ”๋ก์ด ๋๋‚  ๋•Œ ํŒŒ์ผ ์ž๋™์œผ๋กœ ๋‹ซํžˆ๊ฒŒ ๋œ๋‹ค. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฃผ์–ด์ง„ ํŒŒ์ผ๋ช…์˜ ํŒŒ์ผ ์กด์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ์ธ์ฝ”๋”ฉ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋“ฑ๊ณผ ๊ฐ™์€ ์—ฌ๋Ÿฌ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋“ค ์˜ค๋ฅ˜๋Š” ๋ชจ๋‘ IOError๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํฌํ•จํ•œ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ ํ˜•ํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. try: f = open(filename,"rb") try: data = f.read() finally: f.close() except IOError: print("IOError occured") 2.6.1 ํ…์ŠคํŠธ ํŒŒ์ผ ํ…์ŠคํŠธ ํŒŒ์ผ ์ธ์ฝ”๋”ฉ ํ…์ŠคํŠธ ํŒŒ์ผ์€ ํ•ญ์ƒ 2๋ฐ”์ดํŠธ๋กœ ๋ฌธ์ž๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐฐ์—ด, ์ฆ‰ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด์ธ str๋กœ ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ฐ๊ฒŒ ๋œ๋‹ค. ๋ฌธ์ œ๋Š” ์‹ค์ œ ํŒŒ์ผ์€ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋ฅผ ์ €์žฅํ•œ๋‹ค๋Š” ์ ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด์ž eng = 'Hello' kor = '์•ˆ๋…•ํ•˜์„ธ์š”' ์œ„์—์„œ eng์™€ kor์€ ๋ชจ๋‘ 5๊ฐœ์˜ ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์—์„œ๋Š” ํ•œ ๋ฌธ์ž๋‹น 2๋ฐ”์ดํŠธ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ 10๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค(์‹ค์ œ๋กœ๋Š” str ๊ฐ์ฒด์˜ ์ •๋ณด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ ์†Œ์š”). ํŒŒ์ผ๋กœ ์ถœ๋ ฅํ•  ๋•Œ๋Š” ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ธ์ฝ”๋”ฉํ•ด์•ผ ํ• ์ง€ ๊ฒฐ์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์€ 'CP949'์™€ UTF-8๊ฐ€ ์žˆ๋‹ค. 'CP949' : Windows์˜ ๋””ํดํŠธ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹, ์˜๋ฌธ์—๋Š” ์•„์Šคํ‚ค์ฝ”๋“œ์— ๋”ฐ๋ผ 1๋ฐ”์ดํŠธ๋กœ, ํ•œ๊ธ€์—๋Š” 2๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ธ์ฝ”๋”ฉ. 'EUC-KR'๋ฅผ ๋‹ค์‹œ ํ™•์žฅํ•œ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์ด๋ฉฐ, ์—๋””ํ„ฐ์— ๋”ฐ๋ผ 'ANSI', 'EUC-KR' ๋“ฑ์œผ๋กœ๋„ ํ‘œ๊ธฐ๋จ. 'UTF-8' : ์œ ๋‹ˆ์ฝ”๋“œ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์˜ ํ•˜๋‚˜. Python 3์—์„œ. py ์†Œ์Šค ํŒŒ์ผ์— ๋Œ€ํ•œ ๋””ํดํŠธ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹. ์˜๋ฌธ์—๋Š” ์•„์Šคํ‚ค์ฝ”๋“œ์— ๋”ฐ๋ผ 1๋ฐ”์ดํŠธ๋กœ, ํ•œ๊ธ€์€ ์ดˆ์„ฑ, ์ค‘์„ฑ, ์ข…์„ฑ์„ ๊ฐ๊ฐ 1๋ฐ”์ดํŠธ๋กœ ์ €์žฅ(์ •ํ™•ํžˆ๋Š” ANSI ๋ฌธ์ž ์…‹์„ ์ œ์™ธํ•˜๋ฉด 2~4๋ฐ”์ดํŠธ๋กœ ํ‘œํ˜„). ๋‹ค๋ฅธ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฐฉ์‹์ธ 'UTF-16'์— ๋น„ํ•ด ์˜๋ฌธ์ด ๋งŽ์„ ๊ฒฝ์šฐ ํŒŒ์ผ ์šฉ๋Ÿ‰์„ ์ค„์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, 'ANSI'์™€์˜ ํ•˜์œ„ํ˜ธํ™˜์„ฑ์ด ๋ณด์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋จ. ๋‹ค์Œ ์•ž์„œ์˜ eng์™€ kor๋ฅผ ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์— ๋”ฐ๋ฅธ ํŒŒ์ผ ์šฉ๋Ÿ‰์ด๋‹ค. ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹ encoding ='CP949' encoding ='UTF-8' ๋น„๊ณ  eng = 'Hello' ์ €์žฅ ์‹œ 5 ๋ฐ”์ดํŠธ 5๋ฐ”์ดํŠธ ์ €์žฅ๋œ ํŒŒ์ผ์„ ์™„์ „ํžˆ ๋™์ผ kor = '์•ˆ๋…•ํ•˜์„ธ์š”' ์ €์žฅ ์‹œ 10 ๋ฐ”์ดํŠธ 15๋ฐ”์ดํŠธ ๋งŒ์•ฝ ์˜๋ฌธ๋งŒ์„ ์ €์žฅํ–ˆ๋‹ค๋ฉด ๋‘ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์€ ASCII ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด ํŒŒ์ผ์— ์“ฐ๊ธฐ ๋•Œ๋ฌธ์— ์™„์ „ํžˆ ๋™์ผํ•˜๋‹ค. ํ•œ๊ธ€์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์— ๋”ฐ๋ผ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ด ์™„์ „ํžˆ ๋‹ค๋ฅด๋‹ค. ์ด๋ฅผ ๋‹ค์‹œ ์ด์•ผ๊ธฐํ•˜๋ฉด ์˜๋ฌธ๋งŒ ์žˆ๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์€ ์œ„ ๋‘ ๊ฐœ์˜ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹ ์ค‘ ์•„๋ฌด ๋ฐฉ์‹์œผ๋กœ ์ง€์ •ํ•ด๋„ ์ฝ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•œ๊ธ€์ด ํฌํ•จ๋œ ํ…์ŠคํŠธ ๋ฌธ์„œ๋Š” ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์•Œ์•„์•ผ๋งŒ ์ •ํ™•ํ•˜๊ฒŒ ์ฝ์–ด ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋˜๊ธฐ๋„ ํ•œ๋‹ค. ์ด๋•Œ ํŒŒ์ผ์— ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์— ๋”ฐ๋ผ ์“ฐ๋Š” ์ž‘์—…์€ encoding, ๊ทธ ๋ฐ˜๋Œ€๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์„ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์„ decoding์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ฐธ๊ณ  ์—ฌ๊ธฐ์—์„œ๋Š” ์œ ๋‹ˆ์ฝ”๋“œ์˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ UTF-8๋งŒ ๋‹ค๋ฃจ์—ˆ์ง€๋งŒ ์œ ๋‹ˆ์ฝ”๋“œ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์—๋Š” 'UTF-8', 'UTF-8 with BOM', 'UTF-16', 'UTF-16 with BOM' ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์ด ์žˆ๋‹ค. BOM(Byte Order Mark)์€ ํŒŒ์ผ ๋งจ ์•ž์— ๋ช‡ ๊ฐœ์˜ ๋ฐ”์ดํŠธ๋ฅผ ์˜ˆ์•ฝํ•˜์—ฌ ํ‘œ๊ธฐํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ์ž์„ธํ•œ ์‚ฌํ•ญ์€ ์„ธ๋ชจ ๋…ธํŠธ-๋ฌธ์ž์—ด ์ธ์ฝ”๋”ฉ์˜ ๋ชจ๋“  ๊ฒƒ!๋ฅผ ์ฐธ์กฐํ•˜๊ธธ ๋ฐ”๋ž€๋‹ค. ์ธ์ฝ”๋”ฉ ํ™•์ธ๋ฒ• ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ์ธ์ฝ”๋”ฉ์€ notepad++ ๋“ฑ์˜ ์—๋””ํ„ฐ์—์„œ ๊ฐ€๋Šฅํ•˜๊ณ , ๋ณ€ํ™˜ ์—ญ์‹œ ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฐ€๋Šฅํ•˜๋ฉด UTF-8์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์—๋””ํ„ฐ์—์„œ ์ตœ์ดˆ ์ €์žฅํ•˜๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์€ UTF-8์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋งŒ์•ฝ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํŒŒ์ผ์ด๊ณ  ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์—๋””ํ„ฐ๊ฐ€ ์ธ์‹ํ–ˆ๋‹ค๋ฉด ๊ทธ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝ๋œ ์‚ฌํ•ญ์„ ์ €์žฅํ•œ๋‹ค. ๋ฌธ์ œ๋Š” Visual Studio์ธ๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์ด ์ ์šฉ๋œ๋‹ค. ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ํŒŒ์ผ(์ฒ˜์Œ์œผ๋กœ ์ €์žฅํ•˜๋Š” ํŒŒ์ผ)์ธ ๊ฒฝ์šฐ CP949๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํŒŒ์ผ์€ ์ž๋™์œผ๋กœ ์ธ์ฝ”๋”ฉ์„ ๊ฐ์ง€ํ•˜๊ณ  ๊ทธ ์ธ์ฝ”๋”ฉ์œผ๋กœ ์ €์žฅํ•œ๋‹ค. ๋”ฐ๋ผ์„œ Visual Studio๋กœ Python ์ฝ”๋“œ์— ํ•œ๊ธ€์„ ์“ธ ๋•Œ๋Š” ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ํ…์ŠคํŠธ ํŒŒ์ผ ์ฝ๊ธฐ ํ…์ŠคํŠธ ํŒŒ์ผ์€ rt ๋ชจ๋“œ๋กœ ์—ด์–ด์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋ฌธ์„œ์— ํ•œ๊ธ€์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ๋ฌธ์„œ์˜ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ๋ฏธ๋ฆฌ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค(๋ณดํ†ต ์‚ฌ์šฉํ•˜๋Š” 'CP949' ์™€ 'UTF-8'์—์„œ ์˜๋ฌธ์€ ๊ตฌ๋ถ„ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋‚˜ ํ•œ๊ธ€์ด ํฌํ•จ๋˜๋ฉด ๋ฏธ๋ฆฌ ์•Œ์•„์•ผ ํ•จ) ๋ณดํ†ต ํ…์ŠคํŠธ ํŒŒ์ผ์€ read(), readline(), readlines(), write() ๋“ฑ์˜ ๋ฉ”์˜๋“œ๋กœ ์ฝ๊ณ  ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. f.read()๋กœ ์ „์ฒด ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๊ธฐ f = open('SomeWord.txt','rt',encoding='utf-8') # Open file with 'UTF-8' ์ธ์ฝ”๋”ฉ text = f.read() f.close() lines = f.split('\n') # ๋ผ์ธ ๋‹จ์œ„๋กœ ๋ถ„ํ•ด f.readline()์œผ๋กœ ๋ผ์ธ ๋‹จ์œ„ ์ž‘์—… f = open('SomeWord.txt','rt',encoding='utf-8') # Open file with 'UTF-8' ์ธ์ฝ”๋”ฉ while True: line = f.readline() # read line-by-line using f.readline() if not line: break processing line .... f.close() # Close file f.readlines()์œผ๋กœ ๋ชจ๋“  ๋ผ์ธ์„ ์ผ๊ด„ ์ฝ์–ด์™€ ์ž‘์—… f = open('SomeWord.txt','rt',encoding='utf-8') # Open file with 'UTF-8' ์ธ์ฝ”๋”ฉ lines = f.readlines() # read all lines f.close() # Close file ... processing lines ํ…์ŠคํŠธ ์ถœ๋ ฅ f = open('SomeWordOutput.txt','wt',encoding='UTF-8') for line in lines: f.write(line) # Use f.write(line) instead of f.writeline(line) f.close() # Close file CSV ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ CSV ํŒŒ์ผ์ธ ๊ฒฝ์šฐ Python ๋นŒํŠธ์ธ ํŒจํ‚ค์ง€์ธ csv ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค. filename = './text.csv'; f = open(filename,'rt') reader = csv.reader(f, delimiter=',') next(reader) # ํ—ค๋” ๋ผ์ธ skip โ€ฆ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค. for line in reader: print(line) f.close() ์œ„์—์„œ line์€ line = [โ€˜firstโ€™, โ€˜secondโ€™,โ€™thirdโ€™] ๋“ฑ๊ณผ ๊ฐ™์ด ๋ฌธ์ž์—ด๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์ฝํžˆ๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ ์ˆซ์ž๋งŒ์„ ํฌํ•จํ•œ CSV ํŒŒ์ผ์ด๋ผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด float()๋ฅผ ํ†ตํ•ด ๋ณ€ํ™˜ํ•˜๋ฉด ๋œ๋‹ค. filename = './text.csv'; f = open(filename,'rt') reader = csv.reader(f, delimiter=',') next(reader) # ํ—ค๋” ๋ผ์ธ skip โ€ฆ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค. for line in reader: for word in line: print(float(word)) f.close() ์ˆซ์ž๋ฅผ ์ด๋ฃจ์–ด์ง„ ํ…์ŠคํŠธ ํŒŒ์ผ ์ˆซ์ž๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ(์—„๋ฐ€ํ•˜๊ฒŒ ๊ทธ๋Ÿด ํ•„์š”๋Š” ์—†์ง€๋งŒ), numpy ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ํ…Œ์ด๋ธ”<NAME>์˜ ํŒŒ์ผ์„ ์‰ฝ๊ฒŒ ์ฝ์œผ๋ ค๋ฉด NumPy์˜ loadtxt()๋‚˜ savetxt()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค. ์•„๋ž˜๋Š” ๊ณต๋ฐฑ๋ฌธ์ž๋กœ ๊ตฌ๋ถ„๋œ ํ…Œ์ด๋ธ” ํ˜•ํƒœ์˜ ์ˆซ์ž ํŒŒ์ผ์„ ์ฝ์–ด๋“ค์ด๊ณ  ์ €์žฅํ•œ๋‹ค. import numpy as np data = np.loadtxt('ttt.txt') np.savetxt('ttt.out',data,"# test") ๋งŒ์•ฝ header๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ skiprows ์ธ์ž๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ , ๋””ํดํŠธ๋กœ ๊ณต๋ฐฑ๋ฌธ์ž์ธ ๊ตฌ๋ถ„์ž๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด delimiter ์ธ์ž๋ฅผ ์ง€์ •ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ฒซ ๋ฒˆ์งธ ์ค„์„ ๋ฌด์‹œํ•˜๊ณ , ์ฝค๋จธ๋กœ ๊ตฌ๋ถ„๋œ ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. data = np.loadtxt(fname, skiprows=1, delimiter=',')) ์ด์™ธ์—๋„ ์ฃผ์„ ์ฒ˜๋ฆฌ, ๊ตฌ๋ถ„์ž ๋ณ€๊ฒฝ ๋“ฑ ๋‹ค์–‘ํ•œ ์‚ฌ์–‘์€ 3์žฅ NumPy ํŽธ ์ฐธ์กฐ ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ฝ๊ธฐ ์•ž์„œ์—์„œ ์†Œ๊ฐœํ•œ load_txt(), load_csv() ๋“ฑ์˜ ํ•จ์ˆ˜๋ฅผ ์ˆซ์ž ๋ฐ์ดํ„ฐ๊ฐ€ ํ–‰๋ ฌ ํ˜•ํƒœ์—ฌ์•ผ ํ•œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •ํ™•ํ•˜๊ฒŒ ํ–‰๋ ฌ ํ–‰ํƒœ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” ์ง์ ‘ ํŒŒ์ผ์„ ์ฝ์–ด์•ผ ํ•œ๋‹ค. PEER NGA STRONG MOTION DATABASE RECORD Loma Prieta, 10/18/1989, Gilroy - Gavilan Coll., UP ACCELERATION TIME SERIES IN UNITS OF G .1922907E-02 .1922299E-02 .1921739E-02 .1921233E-02 .1920794E-02 .1920399E-02 .1920027E-02 .1919639E-02 .1919272E-02 .1919053E-02 .1919245E-02 .1919835E-02 .1920353E-02 .1920558E-02 .1920871E-02 .1921351E-02 .1920903E-02 .1918933E-02 .1916462E-02 .1914326E-02 .1911934E-02 .1910234E-02 .1909185E-02 .1908185E-02 .1914254E-02 .1930378E-02 .1941617E-02 .1933759E-02 .1920388E-02 .1923397E-02 .1926134E-02 .1898869E-02 .1869943E-02 .1879737E-02 .1935748E-02 .2021556E-02 .2070567E-02 .2060695E-02 .2056042E-02 .2081995E-02 .2062717E-02 .1988024E-02 def loadData(file, skiprow=0): f = open(file,'rt',encoding='utf-8') lines = f.readlines() f.close() data = [] for i in range(skiprow, len(lines)): temp = lines[i].split() for t in temp: data.append(float(t)) return data 2.6.2 ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ ์ด์ง„ ํŒŒ์ผ๋กœ ํŒŒ์ผ์„ ์—ด๋ฉด ํ…์ŠคํŠธ ํŒŒ์ผ์ฒ˜๋Ÿผ ์ธ์ฝ”๋”ฉ ์ž‘์—…์ด๋‚˜ ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž์— ๋Œ€ํ•œ ๋ณ€ํ™˜์ด ์—†์ด ํ•ญ์ƒ 1๋ฐ”์ดํŠธ ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์ธ bytes ๊ฐ์ฒด๋กœ ์ฝ๊ณ  ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋‹ค์Œ์€ ์ด์ง„ ํŒŒ์ผ๋กœ ์—ด์–ด ํŒŒ์ผ์„ ๋ณต์‚ฌํ•œ ์˜ˆ์ด๋‹ค. ํŒŒ์ผ ๋ณต์‚ฌ f = open('ABBA.mp3','rb') data = f.read() # bytes f.close() f = open('ABBA-copy.mp3','wb') f.write(data) f.close() MP3 ํŒŒ์ผ ๊ณก๋ช… ํ™•์ธ ๋‹ค์Œ์€ Working with File Objects์— ์†Œ๊ฐœ๋œ ์ฝ”๋“œ๋ฅผ ๋ฐœ์ทŒํ•œ ๊ฒƒ์œผ๋กœ mp3 ํŒŒ์ผ์—์„œ ๊ณก๋ช…์„ ํ™•์ธํ•œ ์˜ˆ์ด๋‹ค. mp3 ํŒŒ์ผ์€ ํŒŒ์ผ ๋งˆ์ง€๋ง‰์˜ 128 ๋ฐ”์ดํŠธ์— ๊ณก๋ช…, ์žฅ๋ฅด ๋“ฑ๋“ฑ ์—ฌ๋Ÿฌ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค(ํฌ๋งท ์ •๋ณด๋Š” ํ•œ๊ธ€ ์œ„ํ‚คํ”ผ๋””์•„ ์ฐธ์กฐ). >>> f = open('ABBA.mp3','rb') >>> f.seek(-128,2) # ๋์—์„œ 128 ๋ฐ”์ดํŠธ๋กœ ์œ„์น˜ ์ด๋™ >>> tagdata = f.read(128) >>> title = tagdata[3:33].decode() >>> title 'I Do I Do I Do I Do I Do ' >>> f.close() ์œ„์—์„œ bytes.decode()๋ฅผ ์ด์šฉํ•ด ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์—ด์ธ str๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์žˆ๋‹ค. ๋ฌธ์ž์—ด ์ฝ๊ณ  ์“ฐ๊ธฐ ๋ฌธ์ž์—ด์„ ์ฝ๊ณ  ์“ฐ๋Š” ๊ฒƒ์€ ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ํŒŒ์ผ์„ ์—ด๊ณ  ์ฝ๊ณ  ์“ฐ๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค. ๋งŒ์•ฝ ์ด์ง„ ํŒŒ์ผ๋กœ ํŒŒ์ผ์„ ์—ด์–ด ๋‹ค๋ฃฌ ๋•Œ๋Š” str.encode(encoding='utf-8')๊ณผ bytes.decode(encoding='utf-8')์„ ์ ์ ˆํžˆ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋‹ค์Œ์€ ๋ฌธ์ž์—ด์„ ์ด์ง„ ํŒŒ์ผ์— ์ฝ๊ณ  ์“ด ์˜ˆ์ด๋‹ค. # writing str to binary file mytext = '์ด ์ผ์€ ์‰ฌ์šด ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค.' f = open('mydata.bin','wb') f.write(mytext.encode()) f.close() # reading str from binary file f = open('mydata.bin','rb') bdata = f.read() mytext = bdata.decode() f.close() ์ˆ˜์น˜ ๋ฐ์ดํ„ฐ ์ฝ๊ณ  ์“ฐ๊ธฐ int, float ๊ฐ™์€ ์ˆ˜์น˜๋ฐ์ดํ„ฐ๋Š” struct ๋ชจ๋“ˆ์—์„œ ์ œ๊ณตํ•˜๋Š” struct.pack(fmt, v1, v2,...)์œผ๋กœ bytes๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ(์ด๋ฅผ packing์ด๋ผ๊ณ  ํ•จ), ๋ฐ˜๋Œ€๋กœ (v1, v2,...) = struct.unpack(fmt)๋กœ ์ˆ˜์น˜๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•˜๋‹ค(์ด๋ฅผ unpacking์ด๋ผ๊ณ  ํ•จ). ์ด๋•Œ v1, v2 ๋“ฑ์ด ์ˆ˜์น˜๋ฐ์ดํ„ฐ์ด๊ณ , fmt๋Š” ํฌ๋งท ๋ฌธ์ž์—ด์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด fmt๊ฐ€ "idd"์ด๋ฉด int, float, float ์ˆœ์„œ๋กœ packing์ด๋‚˜ unpacking์„ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. import struct # packing numerical data into bytes data = struct.pack("idd",1,10.3, -11.3) # int, float, float # unpacking bytes to numerical data (i, x, y) = struct.unpack("idd",data) # i=1, x = 10.3, y=-11.3 ํŒŒ์ผ์— ์ฝ๊ณ  ์“ฐ๋Š” ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. import struct # writing data age = 27 # int height = 175.2 # float weight = 71.3 # float data = struct.pack('idd',age, height, weight) f = open('mydata.bin','wb') f.write(data) f.close() # reading data f = open('mydata.bin','rb') data = f.read() (age, height, weight) = struct.unpack('idd',data) ๋ฌธ์ž์—ด๊ณผ ์ˆ˜์น˜๋ฐ์ดํ„ฐ๋ฅผ ๋™์‹œ์— ์ฝ๊ณ  ์“ฐ๊ธฐ ๋ฌธ์ž์—ด์€ str.encode(), bytes.decode() ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๊ณ , ์ˆ˜์น˜๋ฐ์ดํ„ฐ๋Š” data=struct.pack(fmt, v1, v2,...)์™€ (v1, v2,...)=struct.unpack(fmt, data)๋ฅผ ์ ์ ˆํžˆ ํ™œ์šฉํ•œ๋‹ค. ์ด๋•Œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ง์ ‘ ๊ณ„์‚ฐํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์— ์ฃผ์˜ํ•œ๋‹ค. import struct # writing data name = 'ํ™๊ธธ๋™' # str age = 27 # int height = 175.2 # float weight = 71.3 # float name_bytes = name.encode() name_data = struct.pack('i',len(name_bytes)) + name_bytes numeric_data = struct.pack('idd',age, height, weight) data = name_data+numeric_data f = open('mydata.bin','wb') f.write(data) f.close() # reading data f = open('mydata.bin','rb') data = f.read() strlen, = struct.unpack('i',data[0:4]) name = data[4:(4+strlen)].decode() (age, height, weight) = struct.unpack('idd',data[4+strlen:]) ์ฐธ๊ณ  ์ด์ง„ ํŒŒ์ผ์— ๋Œ€ํ•œ ์ž…์ถœ๋ ฅ์€ ์ƒ๋‹นํžˆ ๋ณต์žกํ•˜๋‹ค. ๋ณด๋‹ค ์ž์„ธํ•œ ์‚ฌํ•ญ์€ Working binary data python์„ ์ฐธ๊ณ ํ•œ๋‹ค. 2.7 ์‹คํ–‰ ํ™˜๊ฒฝ ๊ด€๋ฆฌ Python์€ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค. 2.7.1 os์™€ glob ๋ชจ๋“ˆ ์ด์šฉํ•˜๊ธฐ Python์˜ ๊ธฐ๋ณธ ๋ชจ๋“ˆ ์ค‘ os ๋ชจ๋“ˆ์€ ์šด์˜ ์ฒด๊ณ„์™€ ๊ด€๋ จ๋œ ๊ฐ์ข… ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ ๋ฆฌ์ŠคํŒ… import os os.listdir() os.listdir('c:') os.mkdir('NewDir') os.rmdir('NewDir') os.unlink('ttt.txt') os.rename('from.txt','to.txt') import glob files = glob.glob('*.*') ํŒŒ์ผ, ํด๋” ์—ฌ๋ถ€ ํ™•์ธ import os os.path.isdir(r'D:\DevProg\Python\test.txt') os.path.isfile(r'D:\DevProg\Python\test.txt') ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ working directory๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ฒƒ์€ os.getcwd(), ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ os.chdir(dir)์„ ์‚ฌ์šฉํ•œ๋‹ค. >>> import os >>> os.getcwd() >>> os.chdir(r'D:\DaelimCS\Report\Analysis\Now') ํ™˜๊ฒฝ ๋ณ€์ˆ˜ PATH ๊ด€๋ฆฌ PATH ํ™•์ธ >>> import os >>> os.environ['PATH'] 'C:\\ProgramData\\Anaconda3\\Library\\bin;C:\\ProgramData\\Anaconda3;C:\\ProgramData\\Anaconda3\\Scripts;C:\\Program Files (x86)\\Graphviz2.38\\bin;... PATH ์ถ”๊ฐ€ >>> import os >>> env = os.environ >>> newpath = r'D:\DevProg\HFC3.0\resource\Distrib-x64-2017-08-21;'+env['PATH'] >>> env['PATH'] = newpath ์ด ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ ์‹คํ–‰๋œ ๋ช…๋ น์ฐฝ์— ๋Œ€ํ•ด์„œ๋งŒ ์œ ํšจํ•˜๋‹ค. ์™ธ๋ถ€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ os.system('notepad') os.system('notepad') 2.7.2 ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ์ œ์–ด ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ํ•˜๊ธฐ Python ์Šคํฌ๋ฆฝํŠธ ๋‚ด์—์„œ ๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ Python 2์—์„œ execfile('script.py') ํ˜•ํƒœ๋กœ ๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜์˜€๋‹ค. Python 3์—์„œ๋Š” ๋” ์ด์ƒ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹  ํŒŒ์ผ์„ ์ฝ์€ ๋‹ค์Œ exec(commandLines)์™€ ๊ฐ™์ด ๋ผ์ธ ๋‹จ์œ„๋กœ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค. f = open('myRCPile.py') exec(f.read()) f.close() ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰์„ ์ค‘๋‹จํ•˜๊ธฐ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰์„ ์ค‘๋‹จํ•˜๋Š” ๊ฒƒ์€ sys.exit(msg)๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ raise SystemExit(msg)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. import sys ... if something == None: sys.exit('something is wrong') ... ... if something == None: raise SystemExit('something is wrong') ... ๋””๋ฒ„๊น… ์šฉ๋„๋กœ ํŠน์ • ๋ผ์ธ๊นŒ์ง€๋งŒ ์‹คํ–‰ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•˜๋‹ค. 2.7.3 ์™ธ๋ถ€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ subprocess๋ฅผ ์ด์šฉํ•œ ์™ธ๋ถ€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์™ธ๋ถ€ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰์€ os ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋Š๋‚˜ ๋ณด๋‹ค ์ „๋ฌธ์ ์œผ๋กœ๋Š” subprocess ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค. import subprocess subprocess.run('notepad') ์œ„ ์ฝ”๋“œ์—์„œ subprocess.run(,...)๋Š” ์‹คํ–‰ํ•˜๊ณ  ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค(wait). run()์€ Python 3.5๋ถ€ํ„ฐ ๋„์ž…๋˜์—ˆ์œผ๋ฉฐ ์˜ˆ์ „์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด Popen ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  wait()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•˜์˜€๋‹ค. import subprocess subprocess.Popen('notepad').wait() ๋‹ค์Œ์€ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์ด๋‹ค. import os import subprocess env = os.environ newpath = r'D:\Earthquake\myStudy\OpenSees3.3.0\bin;'+env['PATH'] env['PATH'] = newpath r = subprocess.run('Opensees.exe push1.tcl',shell=True, capture_output=True, text=True) # CompletedProcess returned print(r.args) print(r.returncode) print(r.stderr) print(r.stdout) os ๋ชจ๋“ˆ์„ ํ†ตํ•ด ์™ธ๋ถ€ ์‹คํ–‰ํŒŒ์ผ์„ ์œ„ํ•œ PATH๋ฅผ ์ง€์ •ํ•˜์˜€๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด env=... ์ธ์ž๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ๋„ ๋™๋“ฑํ•˜๋‹ค. mport os import subprocess env = os.environ newpath = r'D:\Earthquake\myStudy\OpenSees3.3.0\bin;'+env['PATH'] r = subprocess.run('Opensees.exe push1.tcl',shell=True, capture_output=True, text=True, env={'PATH':newPATH}) ... ์™ธ๋ถ€ ์‹คํ–‰ํŒŒ์ผ์— ์ „๋‹ฌํ•œ ๋ช…๋ นํ–‰ ์ธ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ž์‹ ์„ ํฌํ•จํ•ด์„œ ์ธ์ž๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ์ฃผ์–ด์•ผ ํ•œ๋‹ค.(์œ„์—์„œ๋Š” ['Opensees.exe', 'push1.tcl']). ๋ช…๋ นํ–‰์— ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ฌธ์ž์—ด๋กœ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด shell=True ์˜ต์…˜์„ ์“ด๋‹ค. capture_output=True์€ ํ‘œ์ค€ ์ž…์ถœ๋ ฅ์„ ๋ฐ›์•„์˜ค๊ณ  text=True๋Š” ํ…์ŠคํŠธ๋กœ ๋ฐ›์•„์˜ค๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. subprocess.run()์„ ์‹คํ–‰ํ•˜๋ฉด CompletedProcess ๊ฐ์ฒด๊ฐ€ ๋ฆฌํ„ด๋œ๋‹ค. ์ด ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด args, ๋ฆฌํ„ด ๊ฐ’, ํ‘œ์ค€ ์ž…์ถœ๋ ฅ ๋“ฑ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์™ธ๋ถ€ ํ”„๋กœ๊ทธ๋žจ ๋ณ‘๋ ฌ ์‹คํ–‰ concurrent.futures.ProcessPoolExecutor๋ฅผ ์ด์šฉํ•˜์—ฌ subprocess.run์„ ์‹คํ–‰ํ•˜๋ฉด ์™ธ๋ถ€ ํ”„๋กœ๊ทธ๋žจ์„ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ณ‘๋ ฌ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ˜๋“œ์‹œ __main__ ๋ชจ๋“ˆ ๋‚ด์—์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•จ์„ ์ฃผ์˜ํ•œ๋‹ค. import subprocess import concurrent.futures import time import os def main_single_run(): env = os.environ newpath = r'D:\Earthquake\myStudy\OpenSees3.3.0\bin;'+env['PATH'] env['PATH'] = newpath r = subprocess.run('Opensees.exe push1.tcl',shell=True, capture_output=True, text=True) # CompletedProcess returned print(r.args) print(r.returncode) print(r.stderr) print(r.stdout) def main_serial_run(): env = os.environ newpath = r'D:\Earthquake\myStudy\OpenSees3.3.0\bin;'+env['PATH'] env['PATH'] = newpath start = time.time() runs = ['Opensees.exe push1.tcl', 'Opensees.exe push2.tcl', 'Opensees.exe push3.tcl', 'Opensees.exe push4.tcl', 'Opensees.exe push5.tcl', 'Opensees.exe push6.tcl', 'Opensees.exe push7.tcl', 'Opensees.exe push8.tcl'] for i, single_run in enumerate(runs): r = subprocess.run(single_run, shell=True, capture_output=True, text=True) if r.returncode != 0: print('--Error at ' + p.result().args) end = time.time() print("Serial ์ฒ˜๋ฆฌ ์ˆ˜ํ–‰ ์‹œ๊ฐ", end-start, 's') def main_parallel_run(): env = os.environ newpath = r'D:\Earthquake\myStudy\OpenSees3.3.0\bin;'+env['PATH'] env['PATH'] = newpath start = time.time() executor = concurrent.futures.ProcessPoolExecutor(max_workers=10) runs = ['Opensees.exe push1.tcl', 'Opensees.exe push2.tcl', 'Opensees.exe push3.tcl', 'Opensees.exe push4.tcl', 'Opensees.exe push5.tcl', 'Opensees.exe push6.tcl', 'Opensees.exe push7.tcl', 'Opensees.exe push8.tcl'] procs = [] for i, single_run in enumerate(runs): procs.append(executor.submit(subprocess.run, single_run , shell=True, capture_output=True, text=True)) # ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค ์‹คํ–‰์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค. # concurrent.futures.wait(procs) # ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค ์‹คํ–‰์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๊ณ , ์ข…๋ฃŒ๋œ ์ˆœ์„œ๋Œ€๋กœ p๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. # p.result()๊ฐ€ CompletedProcess ๊ฐ์ฒด์ด๋ฏ€๋กœ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. for p in concurrent.futures.as_completed(procs): print(p.result().args + '... compoleted') if p.result().returncode != 0: print('--Error at ' + p.result().args) end = time.time() print("๋ณ‘๋ ฌ์ฒ˜๋ฆฌ ์ˆ˜ํ–‰ ์‹œ๊ฐ", end-start, 's') if __name__ == '__main__': print('-------single run---') main_single_run(); print('-------serial run---') main_serial_run() print('-------parallel run---') main_parallel_run() ์ฐธ๊ณ  : ์ ํ”„ ํˆฌ ํŒŒ์ด์ฌ - ๋ฆฌ๋ธŒ๋Ÿฌ๋ฆฌ ์˜ˆ์ œ ํŽธ - 12-03 concurent.futures 2.7.4 ์‹œ๊ฐ„ ๋‹ค๋ฃจ๊ธฐ Python์—์„œ ์‹œ๊ฐ„์„ ๋‹ค๋ฃจ๋Š” ๋ชจ๋“ˆ์€ time, datetime , locale, calendar ๋“ฑ์ด ์žˆ๋‹ค. ์ด์ค‘ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” ๋ชจ๋“ˆ์ด time์ด๋‹ค. ๋‹ค์Œ์€ time ๋ชจ๋“ˆ์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ ํ•จ์ˆ˜ 2๊ฐœ๋ฅผ ์„ค๋ช…ํ•œ ๊ฒƒ์ด๋‹ค. sec=time.time() : ํ˜„์žฌ์˜ ์‹œ๊ฐ์„ ์‹ค์ˆ˜๋กœ ๋ฆฌํ„ด. ์—„๋ฐ€ํ•˜๊ฒŒ๋Š” ์—ํฌํฌ(epoch, ์‹œ๊ฐ„์˜ ๊ธฐ์ค€์ , 1970๋…„ 1์›” 1์ผ 0์‹œ 0๋ถ„ 0์ดˆ)๋กœ๋ถ€ํ„ฐ ํ˜„์žฌ๊นŒ์ง€ ๊ฒฝ๊ณผ๋œ ์‹œ๊ฐ์ด๋ฉฐ, ์ดˆ ๋‹จ์œ„์ด๋‹ค. ์‹ค์ˆ˜๋กœ ๋ฆฌํ„ดํ•˜๋Š” ํ•˜๋Š” ์ดˆ ์ดํ•˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. time.sleep(t) : ์ฃผ์–ด์ง„ t ์ดˆ ๋™์•ˆ ๋ฉˆ์ถ˜๋‹ค. time.time() ํ˜ธ์ถœ๋กœ ๋ฆฌํ„ด๋œ ์ˆ˜๋Š” epoch๋กœ๋ถ€ํ„ฐ์˜ ์‹œ๊ฐ„์ด๋ฏ€๋กœ ์‚ฌ๋žŒ์ด ์ดํ•ดํ•˜๊ธฐ ํž˜๋“ค๋‹ค. ์ด๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋กœ time.gmtime(t)๊ณผ time.localtime(t)๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ „์ž๋Š” UTC(์˜ˆ์ „์— ๊ทธ๋ฆฌ๋‹ˆ์น˜ ํ‘œ์ค€์‹œ๋กœ ๋ถˆ๋ ธ๋˜)์™€ ์ง€์—ญ ์‹œ๊ฐ„์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š”๋ฐ, time.struct_time ํด๋ž˜์Šค๋กœ ๋ฆฌํ„ดํ•ด ์ค€๋‹ค. >>> import time >>> t = time.time() >>> t 1554030353.9675264 >>> time_utc = time.gmtime(t) >>> time_utc time.struct_time(tm_year=2019, tm_mon=3, tm_mday=31, tm_hour=11, tm_min=6, tm_sec=21, tm_wday=6, tm_yday=90, tm_isdst=0) >>> time_local = time.localtime(t) >>> time_local time.struct_time(tm_year=2019, tm_mon=3, tm_mday=31, tm_hour=20, tm_min=6, tm_sec=43, tm_wday=6, tm_yday=90, tm_isdst=0) ๋งŒ์•ฝ time.gmtime() ๋˜๋Š” time.localtime()์™€ ๊ฐ™์ด ์ธ์ž๊ฐ€ None ์ด๋ฉด, ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋ฆฌํ„ดํ•œ๋‹ค. struct_time์˜ ๋ฉค๋ฒ„๋Š”.` ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์•ก์„ธ์Šค ํ•  ์ˆ˜ ์žˆ๋‹ค. >>> time_local.tm_year 2019 struct_time ํด๋ž˜์Šค๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ time.asctime(time)์ด๋‹ค. >>> time.asctime(time_utc) 'Sun Mar 31 11:06:21 2019' >>> time.asctime(time_local) 'Sun Mar 31 20:06:43 2019' time.ctime([secs])๋Š” asctime(localtime(secs))๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. secs๊ฐ€ None ์ด๋ฉด ํ˜„์žฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. >>> time.ctime() 'Sun Mar 31 20:23:44 2019' >>> time.ctime(2400.) 'Thu Jan 1 09:40:00 1970' ๋‹จ์ผ ์‹ค์ˆ˜์ธ ์‹œ๊ฐ์€ struct_time์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ time.localtime(sec)์™€ time.gmtime(sec)์ธ๋ฐ ๊ทธ ๋ฐ˜๋Œ€์˜ ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค. time.mktime(t)์€ time.localtime(sec)์˜ ์—ญํ•จ์ˆ˜์ด๊ณ , calendar ๋ชจ๋“ˆ์˜ calendar.timegm(t)๋Š” time.gmtime(sec)์˜ ์—ญํ•จ์ˆ˜์ด๋‹ค. ๋ฌธ์ž์—ด๋กœ ํ‘œ์‹œ๋œ ์‹œ๊ฐ„ ์ฒ˜๋ฆฌ ์‹คํ—˜์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์„ผ์„œ์—์„œ ์ธก์ •๋œ ๋ฐ์ดํ„ฐ๋Š” ๋ฌธ์ž์—ด ํ˜•ํƒœ์˜ ์‹œ๊ฐ„ ๊ฐ’์ด ๋™์‹œ์— ์ €์žฅ๋˜๊ฒŒ ๋œ๋‹ค. ์ด๋ฅผ struct_time์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด time.strptime(str, format)๋ฅผ ์ด์šฉํ•œ๋‹ค. ์ดํ›„ sec์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ์ž‘์—…์ด ๊ฐ€๋Šฅํ•˜๋‹ค. >>> import time >>> str_time = '2016-04-25 13:03:17' >>> t_in_time = time.strptime(str_time,'%Y-%m-%d %H:%M:%S') >>> t_in_time time.struct_time(tm_year=2016, tm_mon=4, tm_mday=25, tm_hour=13, tm_min=3, tm_sec=17, tm_wday=0, tm_yday=116, tm_isdst=-1) >>> t_in_secs = time.mktime(t_in_time) >>> t_in_secs 1461556997.0 ๋‹ค์Œ์ด ์œ„ ์ฝ”๋“œ๋ฅผ ์‘์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์‹œ๊ฐ„์„ ์ฒ˜๋ฆฌํ•œ ์˜ˆ์ด๋‹ค. import time str_times = ['2016-04-25 13:03:17', '2016-04-25 13:03:18', '2016-04-25 13:03:19'] t_floats = [] for str_time in str_times: date = time.strptime(str_time,'%Y-%m-%d %H:%M:%S') t_floats.append(time.mktime(date)) ์ž‘์—…์— ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„์„ ์ธก์ •ํ•  ๋•Œ ์•ž์„œ์—์„œ ์†Œ๊ฐœํ•œ time.time()์€ ์‹œ๊ณ„์—์„œ ์ธก์ •๋˜๋Š” ์‹œ๊ฐ„ ์ •๋„๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ์–ด๋–ค ์ž‘์—…์— ๊ฑธ๋ฆฌ ์‹œ๊ฐ„์„ ์ •๋ฐ€ํ•˜๊ฒŒ ์ธก์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๊ธฐ๋Š” ํž˜๋“ค๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” time.perf_counter() ๋˜๋Š” time.process_time() ์„ ์‚ฌ์šฉํ•œ๋‹ค(์˜ˆ์ „์—๋Š” time.clock()๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ depreciated ๋˜์—ˆ๋‹ค.) ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•˜๋„๋ก ํ•œ๋‹ค. 5. SciPy : Timing 2.7.5 ๋‚œ์ˆ˜ Python์—์„œ๋Š” random ๋ชจ๋“ˆ๋กœ ๋‚œ์ˆ˜ ์ƒ์„ฑ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค(์ •ํ™•ํžˆ๋Š” pseudo-random number). ๋‚œ์ˆ˜ ๋ฐœ์ƒ random.random()์€ [0,1) ์‚ฌ์ด์˜ ์‹ค์ˆ˜๋กœ ๋‚œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉฐ, random.ranint(a, b)๋Š” [a, b] ์‚ฌ์ด์˜ ์ •์ˆ˜๋กœ ๋‚œ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค. >>> import random >>> random.random() 0.0705821259174515 >>> random.random() 0.9647091416741986 >>> random.randint(0,10) >>> random.randint(0,10) random.randint(a, b)์™€ ๋น„์Šทํ•œ ํ•ฉ์ˆ˜๋กœ random.randrange(a, b)๊ฐ€ ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” [a, b) ์‚ฌ์ด์ด ์ •์ˆ˜๋ฅผ ๋‚œ์ˆ˜๋กœ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. random.uniform(a, b)๋Š” random.random()์„ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜๋กœ [a, b] ์‚ฌ์ด์— ์‹ค์ˆ˜ํ˜• ๋‚œ์ˆ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค( a+(b-a)*random.random()์œผ๋กœ ๊ณ„์‚ฐ). ๋ ๊ฐ’ b๋Š” ๋ผ์šด๋”ฉ์— ์˜ํ•ด ํฌํ•จ๋  ์ˆ˜๋„ ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค. random.random()๊ณผ random.uniform(a, b)์€ uniform distribution์— ๋”ฐ๋ฅด๋Š” ๋‚œ์ˆ˜๋ฐœ์ƒ์ด๋ฉฐ, ์ด์™ธ์—๋„ random.triagular(low, high, mode), random.betavariate(alpha, beta), random.expovariate(lambd) ๋“ฑ ๋‹ค์–‘ํ•œ ๋ถ„ํฌ์— ๋”ฐ๋ฅธ ๋‚œ์ˆ˜ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ํŽธ์˜ ํ•จ์ˆ˜ random ๋ชจ๋“ˆ์„ ์ด์šฉํ•œ ํŽธ์˜ ํ•จ์ˆ˜๋กœ random.choice(seq), shuffle(seq) random.sample(seq, k) ๋“ฑ์ด ์žˆ๋‹ค. ๋ชจ๋‘ list๋‚˜ tuple ๋“ฑ ์‹œํ€€์Šค๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ž„์˜๋กœ ์„ ํƒํ•˜๊ฑฐ๋‚˜, ๋’ค ์„๊ฑฐ๋‚˜(in-place ์—ฐ์‚ฐ์— ์ฃผ์˜), ์ƒ˜ํ”Œ๋ง(k๋Š” ์ƒ˜ํ”Œ๋งํ•  ์‹œํ€€์Šค์˜ ํฌ๊ธฐ) ํ•œ๋‹ค. >>> actors = ['James', 'Jane', 'Bruce'] >>> random.choice(actors) 'Jane' >>> random.shuffle(actors) >>> actors ['Jane', 'James', 'Bruce'] >>> random.sample(actors, 2) ['James', 'Bruce'] ํŠนํžˆ random.shuffle(seq)๋Š” ๋ฆฌํ„ด ๊ฐ’์ด ์—†๊ณ , ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ง์ ‘ ๋ฐ”๊พธ๋Š” in-place ์—ฐ์‚ฐ์ž„์— ์ฃผ์˜ํ•œ๋‹ค. ์‹œ๋“œ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ(๋ณดํ†ต ๋””๋ฒ„๊น… ๋“ฑ์„ ์œ„ํ•ด ) ๋™์ผํ•œ ์ˆœ์„œ๋กœ ๋‚œ์ˆ˜๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๋‚œ์ˆ˜ ๋ฐœ์ƒ์„ ์œ„ํ•ด์„œ๋Š” ์ ์ ˆํ•œ ์‹œ๋“œ(seed)๋ฅผ ๋‚œ์ˆ˜๋ฐœ์ƒ๊ธฐ์— ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ์‹œ๋“œ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๋™์ผํ•œ ๋‚œ์ˆ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ฒŒ ๋œ๋‹ค. random.seed(a=None)์„ ํ†ตํ•ด ์‹œ๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ์‹œ๋“œ๋ฅผ 100, 50 ๋“ฑ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉฐ ๋‚œ์ˆ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ ์˜ˆ์ด๋‹ค. ์‹œ๋“œ๊ฐ€ 100์œผ๋กœ ๊ฐ™์œผ๋ฉด ๋™์ผํ•œ ๊ฐ’์ด ๋‚œ์ˆ˜๋กœ ๋ฐœ์ƒ๋จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค(random.random()์€ ํ˜„์žฌ์˜ ์‹œ๋“œ์— ๊ทผ๊ฑฐ์— ๋‹ค์Œ ๋‚œ์ˆ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ํ•จ์ˆ˜) >>> import random >>> random.seed(100) >>> random.random() 0.1456692551041303 >>> random.random() 0.45492700451402135 >>> random.seed(50) >>> random.random() 0.4975365687586023 >>> random.random() 0.2661737230725406 >>> random.seed(100) >>> random.random() 0.1456692551041303 >>> random.random() 0.45492700451402135 C/C++์—์„œ๋Š” ์‹œ๋“œ๋ฅผ ํ•ญ์ƒ ์ค˜์•ผ ํ–ˆ๋Š”๋ฐ... C/C++์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ญ์ƒ ์‹œ๋“œ๋ฅผ ๋จผ์ € ์„ค์ •ํ•˜๊ณ  ์ดํ›„์— ๋‚œ์ˆ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. #include <cstdlib> #include <iostream> #include <ctime> int main() { std::srand(std::time(0)); //use current time as seed for random generator int random_variable = std::rand(); std::cout << "Random value on [0 " << RAND_MAX << "]: " << random_variable << '\n'; } ๋งŒ์•ฝ C/C++ ์ฝ”๋“œ์—์„œ srand(seed)๋ฅผ ํ†ตํ•ด ์‹œ๋“œ๋ฅผ ์ฃผ์ง€ ์•Š์œผ๋ฉฐ ํ•ญ์ƒ rand()๋Š” srand(1)์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๊ณ„์‚ฐํ•˜๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ผ๋ฐ˜์ ์œผ๋กœ ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ํ˜„์žฌ์˜ ์‹œ๊ฐ„์„ ๊ตฌํ•ด ์‹œ๋“œ๋ฅผ ์ฃผ๊ฒŒ ๋œ๋‹ค. Python์˜ random ๋ชจ๋“ˆ์—์„œ๋Š” random.seed(a=None)๋กœ ์‹œ๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ์ธ์ž ์—†์ด ํ˜ธ์ถœํ•˜๋ฉด(a=None), ํ˜„์žฌ์˜ ์‹œ๊ฐ„์ด ์‹œ๋“œ๋กœ ์„ค์ •๋œ๋‹ค. ๋˜ํ•œ random ๋ชจ๋“ˆ์€ ์ž„ํฌํŠธ ๋  ๋•Œ ๊ทธ ์‹œ๊ฐ„์œผ๋กœ ์‹œ๋“œ๊ฐ€ ์„ค์ •๋˜๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ C/C++์—์„œ์™€ ๊ฐ™์ด ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด random.seed()๋ฅผ ๋ณ„๋„๋กœ ํ˜ธ์ถœํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. 3. NumPy NumPy๋Š” ๊ณผํ•™ ์ปดํ“จํŒ…์„ ์œ„ํ•œ ๊ธฐ๋ณธ ํŒจํ‚ค์ง€์ด๋‹ค. ๋‹ค์ฐจ์› ๋ฐฐ์—ด ๊ฐ์ฒด, ์ด๋กœ๋ถ€ํ„ฐ ์œ ๋„ํ•œ ๋งˆ์Šคํฌ ๋œ ๋ฐฐ์—ด ๋ฐ ํ–‰๋ ฌ ๋“ฑ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด, ๋…ผ๋ฆฌ, ๋ฐฐ์—ด ํ˜•ํƒœ ์กฐ์ž‘, ์ •๋ ฌ, ์„ ํƒ, I/O๋ฅผ ๋น„๋กฏํ•œ ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๋น ๋ฅธ ์ž‘์—…์„ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋ฃจํ‹ด์„ ์ œ๊ณตํ•œ๋‹ค. ์ด์™ธ์—๋„ ์ด์‚ฐ ํ‘ธ๋ฆฌ์— ๋ณ€ํ™˜, ๊ธฐ๋ณธ ์„ ํ˜• ๋Œ€์ˆ˜ํ•™, ๊ธฐ๋ณธ ํ†ต๊ณ„ ์—ฐ์‚ฐ, ๋ฌด์ž‘์œ„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋“ฑ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. 3.1 ndarray ๊ฐœ๋…๊ณผ ์ƒ์„ฑ NumPy์™€ ํŒจํ‚ค์ง€์˜ ํ•ต์‹ฌ์€ ndarray ๊ฐ์ฒด์ด๋‹ค. ndarray๋Š” fixed-size homogeneous multidimensional array ์ •๋„๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ vectorization๊ณผ broadcasting์„ ์ง€์›ํ•œ๋‹ค. Python์—์„œ ์ œ๊ณตํ•˜๋Š” list, tuple ๋“ฑ์˜ ์‹œํ€€์Šค ์ž๋ฃŒํ˜•์€ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ณ (heterogeneous sequence), ํฌ๊ธฐ๊ฐ€ ์ž๋™์œผ๋กœ ์ปค์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋ฐ˜๋ฉด์— ndarray๋Š” ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์œ„ํ•ด ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋งŒ์„ ์š”์†Œ๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ณ , ํฌ๊ธฐ ์—ญ์‹œ ๊ณ ์ •๋˜์–ด ์žˆ๋‹ค. ๋งŒ์•ฝ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ์ƒˆ๋กœ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น๋˜๊ณ  ์ด์ „ ๊ฐ’์€ ์‚ญ์ œ๋œ๋‹ค. ndarray๋Š” list๋‚˜ tuple ๊ฐ™์€ ์‹œํ€€์Šค ์ž๋ฃŒํ˜•์œผ๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑํ•œ๋‹ค. >>> import numpy as np >>> x = np.array((0.1,0.2,0.3)) # np.array([0.1,0.2,0.3])๋„ ๊ฐ€๋Šฅ >>> x array([0.1, 0.2, 0.3]) >>> x.shape (3, ) >>> x.dtype dtype('float64') >>> y = np.array(((1,2,3),(4,5,6))) # [(1,2,3),(4,5,6)],[[1,2,3],[4,5,6]] ๋“ฑ๋„ ๊ฐ€๋Šฅ >>> y array([[1, 2, 3], [4, 5, 6]]) >>> y.dtype dtype('int32') >>> y.shape (2, 3) x๋Š” float64๋ฅผ ์š”์†Œ ํƒ€์ž…์œผ๋กœ ๊ฐ–๋Š” ํฌ๊ธฐ 3์˜ 1์ฐจ์› ๋ฐฐ์—ด์ด๋‹ค. y๋Š” int32๋ฅผ ์š”์†Œ ํƒ€์ž…์œผ๋กœ ํ•˜๋Š” (2,3) ํฌ๊ธฐ์˜ 2์ฐจ์› ๋ฐฐ์—ด์ด๋‹ค. ์š”์†Œ ํƒ€์ž…์„ dtype ๋ฉค๋ฒ„์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฐจ์›์€ shape์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 1์ฐจ์› ๋ฐฐ์—ด์˜ shape์€ (m,) ํ˜•ํƒœ์ด๊ณ , 2์ฐจ์› ๋ฐฐ์—ด์€ (m, n) ํ˜•ํƒœ์ด๋‹ค. 3์ฐจ์›์€ (p, q, r) ๋“ฑ๊ณผ ๊ฐ™๋‹ค. ์ƒ์„ฑ ์‹œ ์ž…๋ ฅ๋œ ๊ฐ’์„ ํ†ตํ•ด dtype์„ ์ถ”์ •ํ•˜๋Š”๋ฐ, ๊ฐ•์ œ๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. z = np.array([1,2,3],dtype='float64') ndarray์˜ ์ค‘์š” ์†์„ฑ์„ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. shape : ๋ฐฐ์—ด์˜ ํ˜•ํƒœ dtype : ์š”์†Œ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…, int32, float32 ๋“ฑ๋“ฑ ndim : ์ฐจ์›์ˆ˜. x.ndim = 1, y.ndim=2 ๋“ฑ์ด๋ฉฐ len(x.shape) ์™€ ๋™์ผ size : ์š”์†Œ์˜ ๊ฐœ์ˆ˜. shape์˜ ๋ชจ๋“  ๊ฐ’์˜ ๊ณฑ. x.size = 3, y.size=6 ๋“ฑ itemsize : ์š”์†Œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ํฌ๊ธฐ(byte ๋‹จ์œ„), x.itemsize=8 ๋“ฑ data : ์‹ค์ œ ๋ฐ์ดํ„ฐ. ์ง์ ‘ ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘๊ทผํ•  ํ•„์š”๋Š” ์—†์Œ ์ดˆ๊ธฐํ™” ๊ด€๋ จ ํŽธ์˜ ํ•จ์ˆ˜ ์ดˆ๊ธฐํ™”์™€ ๊ด€๋ จ๋œ ๋ช‡๋ช‡ ํŽธ์˜ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. zeros(..)๋Š” ๋ชจ๋‘ 0์œผ๋กœ, ones(...)๋Š” ๋ชจ๋“  ์„ฑ๋ถ„์„ 1๋กœ, empty(...)๋Š” ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š๊ณ (๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฐ’์ด ๋“ค์–ด๊ฐ) ์ƒ์„ฑํ•œ๋‹ค. ์ธ์ž๋กœ ์ƒ์„ฑํ•  ๋ฐฐ์—ด์˜ ์ฐจ์›์„ list๋‚˜ tuple๋กœ ์ง€์ •ํ•œ๋‹ค. dtype์€ ๋””ํดํŠธ๊ฐ€ float64์ด๊ณ , ๋ณ€๊ฒฝ์„ ์›ํ•˜๋ฉด ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. >>> Y = np.zeros((3,3)) >>> Y array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]]) >>> Y = np.ones((3,3),dtype='int32') >>> Z = np.empty((3,3)) ๋ฏธ๋ฆฌ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ์ˆœ์ฐจ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋•Œ๋Š” ํฌ๊ธฐ 0์ธ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ณ  append()๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋œ๋‹ค. import numpy as np A = np.array([]) for i in range(3): A = np.append(A,[1,2,3]) >>> A array([ 1., 2., 3., 1., 2., 3., 1., 2., 3.]) ๋‹จ์ˆœํ•œ ์‹œํ€€์Šค๋Š” range() ํ•จ์ˆ˜์˜ ์‹ค์ˆ˜ ๋ฒ„์ „์ธ arange(from, to, step)์ด๋‚˜ linspace(from, to, npoints)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค. ๋˜ํ•œ ๋‹จ์œ„ํ–‰๋ ฌ์„ ์œ„ํ•œ eye(n) ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. >>> np.arange(1,2,0.1) array([ 1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9]) >>> np.arange(10) # start, step ์ƒ๋žต ๊ฐ€๋Šฅ. ์ •์ˆ˜๋กœ ์ƒ์„ฑ array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.arange(10.) # start, step ์ƒ๋žต ๊ฐ€๋Šฅ. ์‹ค์ˆ˜๋กœ ์ƒ์„ฑ array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) >>> np.linspace(0.,20.,11) array([ 0., 2., 4., ..., 16., 18., 20.]) >>> np.eye(3) array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]) shape๊ณผ dtype ๋ณ€๊ฒฝ ndarray๋Š” ๊ณ ์ •๋œ ํฌ๊ธฐ๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ shape์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํฌ๊ธฐ 9์˜ 1์ฐจ์› ๋ฐฐ์—ด์„ 3*3 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋Š” reshape()์ธ๋ฐ ํ•จ์ˆ˜ ๋˜๋Š” ๋ฉ”์˜๋“œ ํ˜•ํƒœ๋กœ ์ œ๊ณตํ•œ๋‹ค. >>> X = np.arange(0,9,1.) >>> X array([ 0., 1., 2., 3., 4., 5., 6., 7., 8.]) >>> Y = np.reshape(X,(3,3)) # ๋˜๋Š” Y=X.reshape((3,3)) >>> Y array([[ 0., 1., 2.], [ 3., 4., 5.], [ 6., 7., 8.]]) ๋งŒ์•ฝ ์ž๊ธฐ ์ž์‹ ์„ ๋Œ€์ƒ์„ ๋ณ€๊ฒฝํ•˜๋ฉด shape ์†์„ฑ์„ ๊ฐ•์ œ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค. >>> X.shape = (3,3) >>> X array([[ 0., 1., 2.], [ 3., 4., 5.], [ 6., 7., 8.]]) astype() ๋ฉ”์˜๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด์—์„œ dtype์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. >>> a = np.arange(3); >>> a.astype(int) # a.astype('int34') ์™€ ๋™์ผ >>> a.astype('int34') >>> a.astype('int64') >>> a.astype(float) # a.astype('float64') >>> a.astype('float32') >>> a.astype('float64') ์ธ์ž๋กœ๋Š” ๋ฌธ์ž์—ด๋กœ ์ •ํ™•ํ•œ ํ˜•๊ณผ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ int, float ๋“ฑ๊ณผ ๊ฐ™์ด ๋Œ€ํ‘œ ์ƒ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์œ„ ์˜ˆ์—์„œ 32 bit ์ •์ˆ˜์™€ 64bit ์‹ค์ˆ˜ํ˜•์ด ๋””ํดํŠธ์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 3.2 ์ธ๋ฑ์‹ฑ๊ณผ ํ•ฉ์น˜๊ธฐ ์ธ๋ฑ์‹ฑ ndarray์—์„œ ์ธ๋ฑ์‹ฑํ•˜๋Š” ๊ฒƒ์€ A[2], A[2,3] ๋“ฑ๊ณผ ๊ฐ™์€ [] ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. A[1:3,1:2] ๋“ฑ๊ณผ ๊ฐ™์ด :๋ฅผ ์ด์šฉํ•œ ๋ฒ”์œ„ ์ง€์ •์„ ํ†ตํ•ด ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค. A[-1] ๋“ฑ๊ณผ ๊ฐ™์ด ์Œ์˜ ์ •์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋’ค์—์„œ๋ถ€ํ„ฐ ์ธ๋ฑ์‹ฑ ๋œ๋‹ค. >>> a = np.array([1.2, -1.3,2.2,5.3,3.7]) >>> a array([ 1.2, -1.3, 2.2, 5.3, 3.7]) >>> a[0] 1.2 >>> a[0:3] array([ 1.2, -1.3, 2.2]) >>> a[-1] 3.7000000000000002 >>> a[-2] 5.2999999999999998 >>> a[0:-1] array([ 1.2, -1.3, 2.2, 5.3]) ์ธ๋ฑ์Šค ๋ฐฐ์—ด ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ index array๋ฅผ [] ๊ธฐํ˜ธ์— ๋„ฃ์–ด ์ธ๋ฑ์Šค๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. >>> a = np.array([1.2, -1.3,2.2,5.3,3.7]) >>> idx = [0,3] >>> a[idx] array([ 1.2, 5.3]) ๋ถˆ๋ฆฐ ๋ฐฐ์—ด True์™€ False๋กœ ๊ตฌ์„ฑ๋œ boolean array ์—ญ์‹œ ์ธ๋ฑ์Šค ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ x > 2์™€ ๊ฐ™์ด ndarray์— ์กฐ๊ฑด์‹์„ ๋ถ€๊ณผํ•˜๋ฉด boolean array๊ฐ€ ๊ณ„์‚ฐ๋œ๋‹ค. ์ด boolean array๋ฅผ ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. >>> x = np.array([1.2, -1.3, 0., 2.2, 0., 5.3, 3.7]) >>> x > 2 array([False, False, False, True, False, True, True]) >>> idx = x>2 >>> idx array([False, False, False, True, False, True, True]) >>> x[idx] array([2.2, 5.3, 3.7]) ์œ„ ์˜ˆ์ œ์—์„œ์˜ ์„ฑ์งˆ์„ ์ด์šฉํ•˜๋ฉด ์–ด๋–ค ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์‰ฝ๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. >>> x = np.array([1.2, -1.3, 0., 2.2, 0., 5.3, 3.7]) >>> x[x > 2] array([2.2, 5.3, 3.7]) np.nonzero(x)๋Š” ๋ฐฐ์—ด x์˜ ์›์†Œ๊ฐ€ 0์ด ์•„๋‹Œ ์ธ๋ฑ์Šค๋ฅผ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ํ•ฉ์ˆ˜์ด๋‹ค. >>> x = np.array([1.2, -1.3, 0., 2.2, 0., 5.3, 3.7]) >>> np.nonzero(x) array([0, 1, 3, 5, 6], dtype=int64),) np.nonzero(x)์—์„œ x๊ฐ€ ๋ถˆ๋ฆฐ ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ True์ธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ดํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. >>> x = np.array([1.2, -1.3, 0., 2.2, 0., 5.3, 3.7]) >>> np.nonzero(x>2) (array([3, 5, 6], dtype=int64),) np.where(x>2)๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ np.nonzero(x>2)์™€ ๋™์ผํ•˜๋‹ค. ํ•ฉ์น˜๊ธฐ concatenate((A, B,...),axis=0), hstack((A, B,...)), vstack((A, B,...))๋ฅผ ํ†ตํ•ด ๋ฐฐ์—ด์„ ํ•ฉ์น  ์ˆ˜ ์žˆ๋‹ค. >>> a = np.array([[1, 2], [3, 4]]) >>> b = np.array([[5, 6]]) >>> np.concatenate((a, b), axis=0) array([[1, 2], [3, 4], [5, 6]]) >>> np.concatenate((a, b.T), axis=1) array([[1, 2, 5], [3, 4, 6]]) concatenate((A, B,...),axis=0)๋Š” axis ๋ฐฉํ–ฅ์œผ๋กœ ํ•ฉ์น˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค(axis=0์€ row, aixs=2๋Š” column ๋ฐฉํ–ฅ ๋“ฑ) ์œ„์—์„œ b๊ฐ€ (1,2) 2-d ๋ฐฐ์—ด์ด๋‹ค. ๋งŒ์•ฝ b๊ฐ€ (2, )์˜ 1-d ๋ฐฐ์—ด์ด๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋งŒ์•ฝ 2์ฐจ์› ํ–‰๋ ฌ์„ ๋Œ€์ƒ์œผ๋กœ ํ•œ๋‹ค๋ฉด hstack((A, B,...))๊ณผ vstack((A, B,...)) ์ด๋ณด๋‹ค ํŽธ๋ฆฌํ•˜๋‹ค. ์ด ๋“ค ํ•จ์ˆ˜๋Š” (n,) ํ˜•ํƒœ์˜ 1D ๋ฐฐ์—ด์„ (1, n) ๋ฐฐ์—ด๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค. np.append(obj, val)์„ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค. >>> a = np.array([1,2,3]) >>> b = np.array([4,5,6]) >>> np.vstack((a, b)) array([[1, 2, 3], [4, 5, 6]]) >>> np.hstack((a, b)) array([1, 2, 3, 4, 5, 6]) >>> c = np.append(a, b) array([1, 2, 3, 4, 5, 6]) 3.3 ์—ฐ์‚ฐ ์š”์†Œ ๋‹จ์œ„ ์—ฐ์‚ฐ ndarray ๊ฐ์ฒด์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ elementwise๋กœ ์ง„ํ–‰๋œ๋‹ค. >>> a = np.array([1,2,3,4]) >>> b = np.array([4,5,6,7]) >>> a+b array([ 5, 7, 9, 11]) >>> a*b array([ 4, 10, 18, 28]) >>> a**2 array([ 1, 4, 9, 16]) >>> a+2 # ์ƒ์ˆ˜ 2๋Š” ๊ฐ™์€ ํฌ๊ธฐ์˜ ๋ฐฐ์—ด๋กœ ์ธ์‹ array([3, 4, 5, 6]) >>> 10*np.sin(a) # NumPy์˜ universal ํ•จ์ˆ˜ sin() ์ ์šฉ array([ 8.41470985, 9.09297427, 1.41120008, -7.56802495]) >>> a<3 array([ True, True, False, False], dtype=bool) >>> a *= b # a = a*b์™€ ๋™์ผ >>> a array([ 4, 10, 18, 28]) ์œ„์™€ ๊ฐ™์ด ๋ฐฐ์—ด์˜ ๊ฐœ๋ณ„ ์š”์†Œ๊ฐ€ ์•„๋‹Œ ๋ฐฐ์—ด์— ๋Œ€ํ•ด ์—ฐ์‚ฐ์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ vectorization์ด๋ผ ํ•˜๋ฉฐ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ์—๋„ ์ž‘๋™ํ•˜๋„๋ก ํ•œ ๊ฒƒ(์˜ˆ๋ฅผ ๋“ค์–ด a=a+3)์„ broadcasting์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ์ธ๋ฑ์‹ฑํ•˜๋Š” ๊ฒƒ์€ [] ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ :๋ฅผ ์ด์šฉํ•ด ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. >>> A[1,2] >>> A[1:3, :] # ๋˜๋Š” A[1:3, ] Universal ํ•จ์ˆ˜ NumPy์—์„œ๋Š” sin(), cos() ๋‹ค์–‘ํ•œ ์ˆ˜ํ•™ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์Šค์นผ๋ผ ๊ฐ’์„ ์ž…๋ ฅ์œผ๋กœ ํ•˜๋Š” math ํŒจํ‚ค์ง€์™€ ๋‹ฌ๋ฆฌ ๋ฐฐ์—ด์— ๋Œ€ํ•ด elementwise๋กœ ์ ์šฉ๋œ๋‹ค. >>> x = np.arange(0.,2*np.pi, 0.1) >>> y = np.sin(x) >>> y array([ 0. , 0.09983342, 0.19866933, ..., -0.2794155 , -0.1821625 , -0.0830894 ]) ํ–‰๋ ฌ ์—ฐ์‚ฐ ํ–‰๋ ฌ ์—ฐ์‚ฐ ์ค‘ +, - ์—ฐ์‚ฐ ๋ฐ ์Šค์นผ๋ผ์™€ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์€ ์ผ๋ฐ˜ ์ˆ˜ํ•™์‹๊ณผ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ํ–‰๋ ฌ ์š”์†Œ์— ๋Œ€ํ•ด element-wise ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. D = A-2*B+C ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์€ dot(), matmul(), @ ์—ฐ์‚ฐ์ž ๋“ฑ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ถ”์ฒœ ๋ฐฉ๋ฒ•์€ @์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. # -*- coding: utf-8 -*- """ Created on Wed Aug 7 10:58:34 2019 @author: ์กฐ์ •๋ž˜ """ import numpy as np A = np.arange(9).reshape(3,3) # (3,3) B = np.arange(11,11+9).reshape(3,3) # (3,3) x = np.arange(3) # (3, ) y = np.arange(3).reshape(3,1) # (3,1) z = np.arange(3).reshape(1,3) # (1,3) # C = A*B ... same results C1 = np.dot(A, B) C2 = np.matmul(A, B) C3 = A@B # A*x ... 2d*1d ... same results Ax1 = np.dot(A, x) # array([ 5, 14, 23]) Ax2 = np.matmul(A, x) Ax3 = A@x # A*y ... 2d*(2d, but 1d vector) ... same results Ay1 = np.dot(A, x) # array([[ 5],[14],[23]]) Ay2 = np.matmul(A, x) Ay3 = A@y # A*z ... 2d*(2d, but 1d vector) ... all dimension error Az1 = np.dot(A, z) # array([[ 5],[14],[23]]) Az2 = np.matmul(A, z) Az3 = A@z # left make (row vector), right make (column vector) xy = x@y # array([5]) ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ @์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋‹ค์Œ ๊ทœ์น™์„ ๊ธฐ์–ตํ•˜๋„๋ก ํ•œ๋‹ค. @์˜ ์ขŒ์ธก์— ์˜ค๋Š” 1์ฐจ์› ๋ฐฐ์—ด์€ row vector๋กœ ์ทจ๊ธ‰( (3, )์ธ ๊ฒฝ์šฐ (1,3)์œผ๋กœ ์ทจ๊ธ‰) @์˜ ์ขŒ์ธก์— ์˜ค๋Š” 1์ฐจ์› ๋ฐฐ์—ด์€ column vector๋กœ ์ทจ๊ธ‰( (3, )์ธ ๊ฒฝ์šฐ (3,1)์œผ๋กœ ์ทจ๊ธ‰) ์ „์น˜ ํ–‰๋ ฌ์€ A.T์™€ ๊ฐ™์€ ์†์„ฑ์„ ์ด์šฉํ•˜๊ฑฐ๋‚˜ transpose() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. >>> A.T array([[0, 3, 6], [1, 4, 7], [2, 5, 8]]) >>> np.transpose(A) array([[0, 3, 6], [1, 4, 7], [2, 5, 8]]) ์ฃผ์š” ์„ ํ˜•๋Œ€์ˆ˜ํ•จ์ˆ˜๋กœ๋Š” linalg.eig(A), linalg.norm(x), linalg.cond(x), linalg.det(a), linalg.solve(A, b), linalg.inv(a) ๋“ฑ์ด ์žˆ๋‹ค. >>> A = np.array([[1,2, -1],[3,7,0],[0,4, -1]]) >>> [D, V] = np.linalg.eig(A) # D๋Š” eigenvalues, V๋Š” vector >>> D array([ 7.69041576, 1. , -1.69041576]) >>> V array([[-0.20463281, 0.66666667, 0.44197763], [-0.88917212, -0.33333333, -0.15257416], [-0.40926563, -0.66666667, 0.88395526]]) ์ •๋ ฌ๊ณผ ํƒ์ƒ‰ ๋ฐฐ์—ด์—์„œ ์ฐพ๋Š” ๊ฒƒ์€ ์ตœ์†Ÿ๊ฐ’ ๋˜๋Š” ์ตœ๋Œ“๊ฐ’์€ np.amin(x), np.amax(x)๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค. ๋งŒ์•ฝ ์ตœ์†Ÿ๊ฐ’ ๋˜๋Š” ์ตœ๋Œ“๊ฐ’์ด ์žˆ๋Š” ์œ„์น˜๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด np.argmin(x), np.argmax(x)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ •๋ ฌ์€ np.sort(x)๋ฅผ, ์ •๋ ฌํ–ˆ์„ ๋•Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด np.argsort(x)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. import numpy as np >>> x = np.array([9.1,8.2,2.3,3,3,7.6,5.2]) >>> np.amin(x) 2.2999999999999998 >>> np.argmin(x) >>> np.sort(x) array([ 2.3, 3. , 3. , 5.2, 7.6, 8.2, 9.1]) >>> np.argsort(x) array([2, 3, 4, 6, 5, 1, 0], dtype=int64) ๋งŒ์•ฝ ๋‘ ๋ฒˆ์งธ๋กœ ํฐ ๊ฐ’์˜ ์œ„์น˜๋ฅผ ๊ตฌํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. >>> imax2 = np.argsort(x)[-2] ์ด์ง„ ํƒ์ƒ‰ ์ด๋ฏธ ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด์„ ๋Œ€์ƒ์œผ๋กœ ํƒ์ƒ‰ํ•  ๋•Œ๋Š” ์ด์ง„ ํƒ์ƒ‰(binary search)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ณ„์‚ฐ ํšจ์œจ์„ ๋Œ€ํญ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค. Numpy๋Š” np.searchsorted(array, value, side='left')๋ฅผ ํ†ตํ•ด ์ด์ง„ ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. side ์ธ์ž๋กœ๋Š” 'left'์™€ 'right'๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. side='left'๋Š” array[i-1] < value <= array[i]์ด๊ณ , side='right'๋Š” array[i-1] <= value < array[i]์ด๋‹ค. ์˜์—ญ์„ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ๊ทธ๋ฆผ์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋‹ค. import numpy as np x = np.array([0.,13.,26.,30.]) print(x) print(-1, ':',np.searchsorted(x,-1.),',',np.searchsorted(x,-1.,side='right')) print( 0, ':',np.searchsorted(x, 0.),',',np.searchsorted(x, 0.,side='right')) print( 5, ':',np.searchsorted(x, 5.),',',np.searchsorted(x, 5.,side='right')) print(13, ':',np.searchsorted(x, 13.),',',np.searchsorted(x, 13.,side='right')) print(15, ':',np.searchsorted(x,15.),',',np.searchsorted(x, 15.,side='right')) print(30, ':',np.searchsorted(x, 30.),',',np.searchsorted(x, 30.,side='right')) print(35, ':',np.searchsorted(x, 35.),',',np.searchsorted(x, 35.,side='right')) ๊ฒฐ๊ด๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. [ 0. 13. 26. 30.] -1 : 0 , 0 0 : 0 , 1 5 : 1 , 1 13 : 1 , 2 15 : 2 , 2 30 : 3 , 4 35 : 4 , 4 np.searchsorted(a, v, side='left')๋Š” C++ STL์˜ lower_bound(), np.searchsorted(a, v, side='right')๋Š” upper_bound()์™€ ๊ฑฐ๋™์ด ๋™์ผํ•˜๋‹ค. #include <vector> #include <algorithm> template<typename T> size_t searchsorted_left(std::vector<T> x, const T val) { return std::lower_bound(x.begin(), x.end(), val) - x.begin(); } template<typename T> size_t searchsorted_right(std::vector<T> x, const T val) { return std::upper_bound(x.begin(), x.end(), val) - x.begin(); } int main() { std::vector<double> xx = { 0, 13, 26, 30 }; //, 34, 47, 60}; std::cout << "value : lowerbound, upperbound\n"; std::cout << "-1.: " << searchsorted_left(xx, -1.) << " " << searchsorted_right(xx, -1.) << "\n"; std::cout << " 0.: " << searchsorted_left(xx, 0.) << " " << searchsorted_right(xx, 0.) << "\n"; std::cout << " 5.: " << searchsorted_left(xx, 5.) << " " << searchsorted_right(xx, 5.) << "\n"; std::cout << "13.: " << searchsorted_left(xx, 13.) << " " << searchsorted_right(xx, 13.) << "\n"; std::cout << "15.: " << searchsorted_left(xx, 15.) << " " << searchsorted_right(xx, 15.) << "\n"; std::cout << "30.: " << searchsorted_left(xx, 30.) << " " << searchsorted_right(xx, 30.) << "\n"; std::cout << "35.: " << searchsorted_left(xx, 35.) << " " << searchsorted_right(xx, 35.) << "\n"; return 1; } ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์ด๋ฏธ ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด์„ ๋Œ€์ƒ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. a = np.array([1,1,2,4,7,7,11,13,13,13,15,20,25,26,27,30,45,70]) b = np.array([5,7,9,45]) ii = np.searchsorted(a, b) a = np.insert(a, ii, b) 3.4 ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ… ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ… NumPy์—์„œ ์ฐจ์›์ด ๋งž์ง€ ์•Š์€ ๊ฐ์ฒด๋ผ๋ฆฌ ์—ฐ์‚ฐ๋˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์„ broadcasting์ด๋ผ ํ•œ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด 2์ฐจ์› ๋ฐฐ์—ด์— ์Šค์นผ๋ผ๋‚˜ 1์ฐจ์› ๋ฐฐ์—ด์„ ์—ฐ์‚ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด (3,3)์˜ 2์ฐจ์› ๋ฐฐ์—ด A, (3, )์ธ 1์ฐจ์› ๋ฐฐ์—ด x, (1,3)๊ณผ (3,1)์ธ 2์ฐจ์› ๋ฐฐ์—ด y, z๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ธฐ๋กœ ํ•œ๋‹ค. >>> import numpy as np >>> A = np.arange(9.).reshape(3,3) # 2d array : (3,3) >>> x = np.array([1.,0,0]) # 1d array : (3, ) >>> y = x.reshape(1,3) # 2d array : (1,3) >>> z = x.reshape(3,1) # 2d array : (3,1) >>> A array([[ 0., 1., 2.], [ 3., 4., 5.], [ 6., 7., 8.]]) >>> x array([ 1., 0., 0.]) >>> y array([[ 1., 0., 0.]]) >>> z array([[ 1.], [ 0.], [ 0.]]) 2์ฐจ์› ๋ฐฐ์—ด๊ณผ ์Šค์นผ๋ผ์˜ ์—ฐ์‚ฐ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ ์Šค์นผ๋ผ 1์˜ ์ฐจ์›์„ (3,3)์œผ๋กœ ํ™•์žฅํ•˜๊ณ  ๊ทธ ์„ฑ๋ถ„์˜ ๊ฐ’์€ ์Šค์นผ๋ผ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์“ด๋‹ค. >>> A+1 # (3,3) + scalar ==> (3,3) + scalar*I array([[ 1., 2., 3.], [ 4., 5., 6.], [ 7., 8., 9.]]) 2์ฐจ์› ๋ฐฐ์—ด๊ณผ 1์ฐจ์› ๋ฐฐ์—ด์˜ ์—ฐ์‚ฐ์—์„œ๋Š” 1์ฐจ์› ๋ฐฐ์—ด (3, )์„ 2์ฐจ์› ๋ฐฐ์—ด (3,3)์œผ๋กœ ํ™•์žฅํ•˜๊ฒŒ ๋œ๋‹ค. # (3,3) + (3, ) ==> (3,3) # x = [1,0,0] --> expand with (3,3) 1 0 0 # 1 0 0 # 1 0 0 >>> A+x array([[ 1., 1., 2.], [ 4., 4., 5.], [ 7., 7., 8.]]) ์ฐจ์›์ด (1,3)์ธ ๋ฐฐ์—ด์„ ์ ์šฉํ•  ๋•Œ๋Š” ์œ„์™€ ํ†ต์ผํ•˜๋‹ค. # (3,3) + (1,3) ==> (3,3) # y = [[1,0,0]] --> expand with (3,3) 1 0 0 # 1 0 0 # 1 0 0 >>> A+y array([[ 1., 1., 2.], [ 4., 4., 5.], [ 7., 7., 8.]]) ์ฐจ์›์ด (3,1)์ผ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™•์žฅ๋œ๋‹ค. # (3,3) + (3,1) # z = 1 --> expand with (3,3) 1 1 1 # 0 0 0 0 # 0 0 0 0 >>> A+z array([[ 1., 2., 3.], [ 3., 4., 5.], [ 6., 7., 8.]]) ๋งˆ์ง€๋ง‰์œผ๋กœ (1,3)์™€ (3,1) ๋ฐฐ์—ด์ด ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด (3,3) ๋ฐฐ์—ด์ด ๋œ๋‹ค. # (1,3) + (3,1) --> each dimensions are expanded # y = [1,0,0] --> (3,3) 1 0 0 z = 1 1 1 1 # 1 0 0 0 0 0 0 # 1 0 0 0 0 0 0 >>> y+z array([[ 2., 1., 1.], [ 1., 0., 0.], [ 1., 0., 0.]]) ํ–‰๋ ฌ ์—ฐ์‚ฐ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ฃผ์˜์‚ฌํ•ญ matmul(A, b), dot(A, B), solve(A, b) ๋“ฑ์˜ ํ•จ์ˆ˜๋กœ ์„ ํ˜•๋Œ€์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ๋ฐฐ์—ด์˜ ์ฐจ์›๊ณผ ๊ด€๋ จํ•ด์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์ด ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜์˜€๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ธฐ๋กœ ํ•œ๋‹ค. >>> A = np.arange(9.).reshape(3,3) # (3,3) 2d array >>> x = np.array([2,0,1]) # (3, ) 1d array >>> y = x.reshape(1,3) # (1,3) 2d array >>> z = x.reshape(3,1) # (3,1) 2d array >>> A array([[ 0., 1., 2.], [ 3., 4., 5.], [ 6., 7., 8.]]) >>> x array([2, 0, 1]) >>> y array([[2, 0, 1]]) >>> z array([[2], [0], [1]]) Ay ์—ฐ์‚ฐ์ด๋‚˜ Az ์—ฐ์‚ฐ์€ ์˜ˆ์ƒํ•œ ๋Œ€๋กœ ์˜ค๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ์ •์ƒ์ ์ด๋กœ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. >>> np.matmul(A, y) # (3,3)*(1,3) -> Dimension Error >>> np.matmul(A, z) # (3,3)*(3,1) -> (3,1) array([[ 2.], [ 11.], [ 20.]]) ๋ฌธ์ œ๋Š” Ax์™€ ๊ฐ™์ด 2์ฐจ์› ๋ฐฐ์—ด๊ณผ 1์ฐจ์› ๋ฐฐ์—ด์— ์—ฐ์‚ฐ์„ ์ ์šฉํ•  ๋•Œ์ด๋‹ค. >>> np.matmul(A, x) # (3,3)*(3, ) -> (3, ) array([ 2., 11., 20.]) ์œ„ ๊ฒฐ๊ณผ์—์„œ Ax์™€ ๊ฐ™์€ ์—ฐ์‚ฐ์€ x๋ฅผ (3,1) ๋ฐฐ์—ด๋กœ ์ทจ๊ธ‰ํ•œ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•˜์ง€๋งŒ ์ถœ๋ ฅ๋  ๋•Œ๋Š” ๋‹ค์‹œ (3, ) ํ–‰ํƒœ๋กœ ๋ณ€ํ™˜ํ•ด ์คŒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ์•ž์„œ์˜ broadcasting๊ณผ ๋น„๊ตํ•  ๋•Œ ์ƒ๋‹นํžˆ ํ˜ผ๋ˆ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. 3.5 ๋ณต์‚ฌ Python์—์„œ list ๋“ฑ์˜ ์ปจํ…Œ์ด๋„ˆ๋Š” shallow copy๊ฐ€ ๋œ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ NumPy์˜ ndarray์™€ ๋Œ€์ž… ์—ฐ์‚ฐ์ž =๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด shallow copy๊ฐ€ ๋˜๋ฉฐ, ์‹ค์ œ ๋ณต์‚ฌ(deep copy) ํ•˜๋ ค๋ฉด object.copy()๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. >>> import numpy as np >>> a = [1,2,3] >>> x = np.array(a) >>> y = x # shallow copy >>> y[0]=10 >>> a [1, 2, 3] >>> x array([10, 2, 3]) >>> y array([10, 2, 3]) >>> y is x True >>> z = x.copy() # deep copy >>> z is x False ์œ„์—์„œ x์™€ y๋ฅผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— y๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด x๊นŒ์ง€ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋œ๋‹ค. a๋Š” ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” x๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๊ทธ ๊ฐ’์— ๋ณต์‚ฌ๊ฐ€ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ถ€๋ถ„ ํ–‰๋ ฌ ์—ญ์‹œ shallow copy ๋˜๋Š” ์ ์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค(list์˜ ์Šฌ๋ผ์ด์‹ฑ์€ deep copy๋จ!!!) >>> import numpy as np >>> x = np.array([1,2,3]) >>> y = x[0:2] >>> y[0] = 10 >>> y array([10, 2]) >>> x array([10, 2, 3]) 3.6 ๋ฐ์ดํ„ฐ ์ฝ๊ณ  ์“ฐ๊ธฐ ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ์ฝ๊ณ  ์“ฐ๊ธฐ np.loadtxt()์™€ np.savetxt()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๋นŒํŠธ์ธ ํŒจํ‚ค์ง€์ธ csv์™€ ๋น„๊ตํ•  ๋•Œ ํ—ค๋”๋ฅผ ์ฝ์ง€ ๋ชปํ•˜์ง€๋งŒ ์ฝค๋งˆ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ณต๋ฐฑ์œผ๋กœ ๋ถ„๋ฆฌ๋œ ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์“ธ ๋•Œ ํŽธ๋ฆฌํ•˜๋‹ค. ๋‹ค์Œ์€ ์ค‘์š” ์ธ์ž๋งŒ ํฌํ•จ๋œ ๋‘ ํ•จ์ˆ˜์˜ ์›ํ˜•์ด๋‹ค. ๋‘ ํ•จ์ˆ˜๋Š” ๋””ํดํŠธ๋กœ ์ŠคํŽ˜์ด์Šค๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๋งŒ์•ฝ ์ฝค๋งˆ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด delimiter=','๋กœ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. np.loadtxt()๊ฐ€ skiprows(์ฒ˜์Œ ๋ช‡ ๊ฐœ์˜ ์—ด์„ ๋ฌด์‹œํ•  ๊ฑด์ง€)์™€ comment ๋ฌธ์ž๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ์ถ”๊ฐ€๋˜์–ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. np.savetxt(fname, X, delimiter=' ', ...) X= np.loadtxt(fname, delimiter=' ', skiprows=0, comments='#', ...) ๋‹ค์Œ์€ 2์ฐจ์› ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ ์˜ˆ์ด๋‹ค. import numpy as np x = np.arange(9).reshape(3,3) np.savetxt('3by3.txt',x) y = np.loadtxt('3by3.txt') ์ €์žฅ๋œ ํŒŒ์ผ์ธ 3by3.txt์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 0.000000000000000000e+00 1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00 4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00 7.000000000000000000e+00 8.000000000000000000e+00 ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ํ•  ์ ์€ 1์ฐจ์› ๋ฐฐ์—ด(์ฆ‰, ๋ฒกํ„ฐ)์€ n*1 ํ˜•ํƒœ๋กœ ์ €์žฅ๋œ๋‹ค๋Š” ์ ๊ณผ ํŒŒ์ผ์—์„œ n*1์ด๋‚˜ 1*n์€ ๋ชจ๋‘ 1์ฐจ์› ๋ฒกํ„ฐ๋กœ ์ฝํžŒ๋‹ค๋Š” ์ ์ด๋‹ค. import numpy as np x1 = np.array([1.1,2.2,3.1]) np.savetxt('x1.txt', x1) x2 = np.array([1.1,2.2,3.1]).reshape(1,3) np.savetxt('x2.txt', x2) x3 = np.array([1.1,2.2,3.1]).reshape(3,1) np.savetxt('x3.txt', x3) y1 = np.loadtxt('x1.txt') # array([1.1, 2.2, 3.1]) y2 = np.loadtxt('x1.txt') # array([1.1, 2.2, 3.1]) y3 = np.loadtxt('x1.txt') # array([1.1, 2.2, 3.1]) x1.txt 1.100000000000000089e+00 2.200000000000000178e+00 3.100000000000000089e+00 x2.txt 1.100000000000000089e+00 2.200000000000000178e+00 3.100000000000000089e+00 x3.txt 1.100000000000000089e+00 2.200000000000000178e+00 3.100000000000000089e+00 ndarray ํŒŒ์ผ ์ €์žฅ๊ณผ ๋ณต์› ndarray.tofile()๊ณผ np.fromfile()๋กœ Matlab์—์„œ ํ–‰๋ ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋กœ๋“œํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ... A์— ๋Œ€ํ•œ ์ž‘์—… A.tofile(โ€˜backupโ€™) # ์ €์žฅ ... Anew = np.fromfile(โ€˜backupโ€™) # ์ €์žฅํ–ˆ๋˜ ๊ฐ’์„ A_new๋กœ loading Binary ํŒŒ์ผ NumPy์—์„œ ์ง€์›ํ•˜๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ์„. npy, .npz๋ผ๊ณ  ํ•œ๋‹ค. .npy๋Š” 1๊ฐœ์˜ ndarray๋ฅผ, .npz๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ndarray๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. save(file, arr,...): arr์„. npy ํฌ๋งท์œผ๋กœ file์— ์”€ savez(file,*args,**kwds): args์™€ kwgs์— ์ฃผ์–ด์ง„ ์—ฌ๋Ÿฌ ๋ฐฐ์—ด์„. npz ํฌ๋ฑƒ์œผ๋กœ file์— ์”€. **kwds๋กœ ์ €์žฅํ•  ๋•Œ ์ฃผ์–ด์ง„ ํ‚ค์›Œ๋“œ๊ฐ€ ์‚ฌ์šฉ๋˜๊ณ , args๋กœ ์ €์žฅํ•˜๋Š” ๊ฒฝ์šฐ arr_0, arr_1, ๋“ฑ์„ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๊ฐ™์Œ. savez_compresseds(file,*args,*kwds): savez(...) ์™€ ๋™์ผํ•˜๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์••์ถ•ํ•จ load(file,...) : ์œ„ ์„ธ ํ•จ์ˆ˜๋กœ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Œ ๋‹ค์Œ์€ ๊ฐ€์†๋„ ์„ผ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ธก์ • ๊ฐ„๊ฒฉ๊ณผ ๊ณ„์ธก ์Šคํ…Œ์ด์…˜ ๋ฒˆํ˜ธ์™€ ํ•จ๊ป˜ ์ €์žฅํ•œ ์˜ˆ์ด๋‹ค. >>> import numpy as np >>> acc = np.random.random(1024) # 1024 points in [0,1.0) >>> dt = 60.0 >>> station = 3 >>> np.savez('acceleration.npz',acc=acc, dt=dt, station=station) >>> data = np.load('acceleration.npz') >>> data["acc"] array([0.74681048, 0.33561388, 0.0677092 , ..., 0.67783846, 0.01131057, 0.62417015]) >>> data["dt"] array(60.) >>> data["station"] array(3) 3.7 ํƒ€์ž…๊ณผ structured array ๋ฐ์ดํ„ฐ ํƒ€์ž… NumPy ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด dtype์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ •์ˆ˜ํ˜•์€ np.int32, ์‹ค์ˆ˜ํ˜•์€ np.float64๊ฐ€ ์ง€์ •๋œ๋‹ค. >>> x = np.array([1,2,3],dtype=np.int16) >>> xf = np.array([1,2,3],dtype=np.float64) ํŒŒ์ด์ฌ์—์„œ ๋ฆฌ์ŠคํŠธ ๋“ฑ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ œ์™ธํ•œ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์€ bool, int, float, str ๋“ฑ์ด๋‹ค. ์ด๋“ค์€ C/C++์™€ ๊ฐ™์€ ์ผ๋ฐ˜ ์–ธ์–ด์™€ ๋‹ฌ๋ฆฌ ๋ชจ๋‘ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœํžˆ ๋ช‡ ๋ฐ”์ดํŠธ์˜ ํฌ๊ธฐ๋ฅผ<NAME>๋‹ค ๋“ฑ์œผ๋กœ ์ด์•ผ๊ธฐํ•  ์ˆ˜ ์—†๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” ์ •์ˆ˜(int)์— ๋Œ€ํ•œ ๊ฐ’์˜ ํฌ๊ธฐ ์ œํ•œ ์—†์ด, ๊ฐ€๋Šฅํ•œ ๋ฉ”๋ชจ๋ฆฌ ํ•œ๋„๊นŒ์ง€ ํ™•์žฅ๋œ๋‹ค. >>> x = 10000000000000000000000000000000000000000000 >>> x = x + 1 >>> print (x) 10000000000000000000000000000000000000000000 ๋ฐ˜๋ฉด์— NumPy์—์„œ๋Š” ์ˆ˜์น˜๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์ธ ndarray๋ฅผ ๋‹ค๋ฃจ๊ธฐ ๋•Œ ๊ทธ ๊ตฌ์„ฑ์š”์†Œ์˜ ํฌ๊ธฐ๊ฐ€ C/C++์ฒ˜๋Ÿผ ๊ณ ์ •๋˜์–ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ NumPy๋Š” np.int32, np.float64 ๋“ฑ๊ณผ ๊ฐ™์€ ํƒ€์ž… ๊ฐ์ฒด์•ผ np.dtype('int32') ๋“ฑ๊ณผ ๊ฐ™์ด ๋ฌธ์ž์—ด์„ ์ธ์ž๋กœ ํ•˜๋Š” np.dtype ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์•„๋ž˜ ์˜ˆ์—์„œ np.int32์™€ np.dtype('int32')๊ฐ€ ์„œ๋กœ ๋™์ผํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. >>> np.int32 numpy.int32 >>> np.dtype('int32') dtype('int32') >>> np.dtype('int32') == np.int32 True ์ฃผ์˜ํ•  ์ ์€ np.int๋Š” NumPy์˜ ์ •์ˆ˜ํ˜•์ด ์•„๋‹Œ Python ์ •์ˆ˜๋ผ๋Š” ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ np.int32๋„ np.int64๋„ ์•„๋‹ˆ๋‹ค. >>> np.int # ๊ทธ๋ƒฅ Python int์ž„ int >>> np.int == np.int32 False >>> np.int == np.int64 False ๋‹ค์Œ์€ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ •์˜๋œ NumPy ๊ฐ์ฒด์ด๋‹ค. ๋ถˆ๋ฆฐ ํ˜• : np.bool_ ์ •์ˆ˜ํ˜• : np.int8, np.int16, np.int32, np.int64 ์‹ค์ˆ˜ํ˜• : np.float32, np.float64 ๋ณต์†Œ์ˆ˜ : np.complex64, np.complex128 np.dtype('str')์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ”์ดํŠธ ์˜ค๋”(bite order)๋ฅผ ํฌํ•จํ•œ ๋ณด๋‹ค ๋‹ค์–‘ํ•œ ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ์˜ˆ์ด๋‹ค. np.dtype('int32') : 32๋น„ํŠธ LE ์ •์ˆ˜, np.int32์™€ ๋™์ผ np.dtype('i4') : 4๋ฐ”์ดํŠธ LE ์ •์ˆ˜, np.int32์™€ ๋™์ผ np.dtype('f8') : 8๋ฐ”์ดํŠธ LE ์‹ค์ˆ˜, np.float64์™€ ๋™์ผ np.dtype('>f8') : 8๋ฐ”์ดํŠธ BE ์‹ค์ˆ˜ ์ •์ˆ˜ํ˜•์˜ ๊ฒฝ์šฐ i{byte}, int{bit}, <i{byte}, >i{byte} ํ˜•ํƒœ๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ <๋Š” LE(๋ฆฌํ‹€ ์—”๋””์•ˆ)์œผ๋ฏธํ•˜๋ฉฐ ๋””ํดํŠธ์ด๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‹ค์ˆ˜ํ˜•์˜ ๊ฒฝ์šฐ f{byte}, float{bit}, <f{byte}, >f{byte} ํ˜•ํƒœ๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌํ‹€ vs ๋น… ์—”๋””์–ธ ์ปดํ“จํ„ฐ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ๋‚˜ ๋””์Šคํฌ์— ์ €์žฅํ•  ๋•Œ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด ์ €์žฅํ•œ๋‹ค. ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋Œ€๊ฒŒ 4๋ฐ”์ดํŠธ๋‚˜ 8๋ฐ”์ดํŠธ๋กœ ๊ตฌ์„ฑ๋˜๋ฏ€๋กœ, ์—ฐ์†๋˜๋Š” ๋ฐ”์ดํŠธ๋ฅผ ์–ด๋–ค ์ˆœ์„œ๋กœ ์ €์žฅํ•ดํ•˜๋Š”๋ฐ ์ด๋ฅผ ๋ฐ”์ดํŠธ ์ €์žฅ ์ˆœ์„œ(byte order)๋ผ๊ณ  ํ•œ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๋น… ์—”๋””์–ธ(big endian) : ๋†’์€ ๋ฐ”์ดํŠธ๋ถ€ํ„ฐ ๋‚ฎ์€ ๋ฐ”์ดํŠธ๋กœ ์ €์žฅ. ์˜ˆ๋ฅผ ๋“ค์–ด 0x12345678์ธ ๊ฒฝ์šฐ 0x12, 0x34, 0x56, 0x78 ์ˆœ์„œ๋กœ ์ €์žฅ ๋ฆฌํ‹€ ์—”๋””์•ˆ(little endian) : ๋‚ฎ์€ ๋ฐ”์ดํŠธ๋ถ€ํ„ฐ ๋‚ฎ์€ ๋ฐ”์ดํŠธ๋กœ ์ €์žฅ. ์˜ˆ๋ฅผ ๋“ค์–ด 0x12345678์ธ ๊ฒฝ์šฐ 0x78, 0x56, , 0x34, 0x12 ์ˆœ์„œ๋กœ ์ €์žฅ ์ธํ…” ๊ณ„์—ด CPU(์ธํ…”์ด๋‚˜ AMD)๋Š” ๋ฆฌํ‹€ ์—”๋””์•ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค๋งŒ ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ๋Š” ๋น…์—”๋””์•ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค. Python์€ ํ”Œ๋žซํผ์„ ๋”ฐ๋ฅธ๋‹ค. ์ฆ‰ little endian์ด๋‹ค. ๋‹ค์Œ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. >>> import sys >>> sys.byteorder little ์ปดํŒŒ์šด๋“œ ํƒ€์ž…๊ณผ structured array NumPy๋Š” C์˜ ๊ตฌ์กฐ์ฒด(structure)๋กœ ์ด์šฐ๋Ÿฌ์ง„ ๋ฐฐ์—ด์— ํ•ด๋‹นํ•˜๋Š” structured array๋ฅผ ์ง€์›ํ•œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด C์—์„œ ๊ตฌ์กฐ์ฒด์˜ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๊ธฐ๋กœ ํ•œ๋‹ค. struct info { char name[10]; int age; float height; }; struct info data[3]; strcpy(data[0].name, "Alice"); data[0].age = 19; data[1].height = 171.; ... ์œ„์— ๋Œ€์‘ํ•˜๋Š” ์ž‘์—…์„ NumPy์—์„œ ์ง€์›ํ•˜๋Š” ๋ฐ ์ด๋ฅผ structured array๋ผ๊ณ  ํ•œ๋‹ค. Structured array์˜ ๊ฐœ๋ณ„ ๊ตฌ์„ฑ ์š”์†Œ๋Š” ์—ฌ๋Ÿฌ ํƒ€์ž…์„ ๋ชจ์€ compound type์ด๋ผ๊ณ  ํ•˜๋ฉฐ (name, type)์˜ ์Œ์œผ๋กœ ๊ตฌ์„ฑํ•œ๋‹ค. >>> import numpy as np >>> dtype = [('name','S10'),('age','<i4'),('height','<f4')] >>> x = np.array([('Alice',19,173.),('Bob',20, '181.')],dtype=dtype) >>> x array([(b'Alice', 19, 173.), (b'Bob', 20, 181.)], dtype=[('name', 'S10'), ('age', '<i4'), ('height', '<f4')]) >>> x['name'] array([b'Alice', b'Bob'], dtype='|S10') >>> x['age'] array([19, 20]) >>> x['height'] array([173., 181.], dtype=float32) >>> x[0] (b'Alice', 19, 173.) ์œ„ ์ฝ”๋“œ์—์„œ dtype์ธ ์ž๋กœ (name, type)์˜ ์Œ์ด ์‚ฌ์šฉ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. name์€ ์ธ๋ฑ์‹ฑ์— ์‚ฌ์šฉ๋˜๋ฉฐ, type์€ np.dtype(str)์—์„œ ์‚ฌ์šฉ๋œ str๊ณผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ NumPy์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ๊ตฌ์„ฑ์š”์†Œ๋Š” ๊ทธ ํฌ๊ธฐ๊ฐ€ ๋ฏธ๋ฆฌ ์ •ํ•ด์ ธ์•ผ ํ•˜๋ฏ€๋กœ ๋ฌธ์ž์—ด์€ S10 ๋“ฑ๊ณผ ๊ฐ™์ด ๊ทธ ํฌ๊ธฐ๊ฐ€ ๋ฏธ๋ฆฌ ์ •ํ•ด์ง„๋‹ค๋Š” ๊ฒƒ์ด๋‹ค(S10์€ 10๋ฐ”์ดํŠธ ํฌ๊ธฐ์˜ ๋ฌธ์ž์—ด์„ ์˜๋ฏธ). dtype์„ ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„์—์„œ type์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ž์—ด๋„ ์ž‘๋™ํ•œ๋‹ค. dtype = [('name','S10'),('age','int32'),('weight','float64')] # ์œ„ ์˜ˆ์™€ ๋™์ผ ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์žˆ๋Š” ์ž๋ฃŒ๋Š” structured array๋กœ ํ‘œํ˜„ํ•œ ์˜ˆ์ด๋‹ค. import numpy as np name = ['Alice', 'Bob', 'Doug'] age = [25, 45, 37] height = [173.,181.,165.] dtype = [('name','S10'),('age','<i4'),('height','<f4')] x = np.zeros(3, dtype=dtype) x['name'] = name x['age'] = age x['height'] = height 3.8 ๊ธฐํƒ€ nan, inf ํ™œ์šฉ๋ฒ• NumPy์—์„œ๋Š” nan (not a number)์™€ inf (infinite)์„ ๋ณ„๋„์˜ ์ƒ์ˆ˜๋กœ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. nan์„ ์ด์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ํŽธ๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆผ์„ ๊ทธ๋ฆด ๋•Œ nan์ด ์žˆ์œผ๋ฉด ๊ทธ ํฌ์ธํŠธ๋Š” ๊ทธ๋ฆฌ์ง€ ์•Š๊ฒŒ ๋œ๋‹ค. x = [1,2,3,4] y1 = [4, 7,, 8, 9] y2 = [2,3,1, np.nan] plt.plot(x, y1) plt.plot(x, y2) inf๋Š” ๋น„๊ต ์‹œ ์ดˆ๊นƒ๊ฐ’์œผ๋กœ ํ™œ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค. maximum = -np.inf for x in data: if x > maximum: maximum = x 4. Matplotlib Matplotlib์€ python์—์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ‚ค์ง€์ด๋‹ค. Numpy์™€ ๋”๋ถˆ์–ด ์—†์–ด์„œ๋Š” ์•ˆ ๋  ํŒจํ‚ค์ง€๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. 4.1 ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• ๊ฐ„๋‹จํ•œ 2์ฐจ์› ํ”Œ๋กฏ matplotlib์€ 2์ฐจ์› ๊ทธ๋ž˜ํ”ฝ ํŒจํ‚ค์ง€์ด๋‹ค. Matlab๊ณผ ๊ฐ™์ด ์ปค๋งจ๋“œ ๋ฐฉ์‹(matplotlib์—์„œ๋Š” Pyplot API๋ผ๊ณ  ํ•œ๋‹ค)์œผ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ปค๋งจ๋“œ ํ•จ์ˆ˜์˜ ์ด๋ฆ„๋„ ์œ ์‚ฌ๋„ ๋ก ์„ค๊ณ„๋˜์–ด ์žˆ๋‹ค. ๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ ์˜ˆ์ด๋‹ค. # example 1 import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,50) y1 = np.cos(4*np.pi*x) y2 = np.cos(4*np.pi*x)*np.exp(-2*x) plt.plot(x, y1) plt.plot(x, y2) plt.show() plt.plot(x, y)๋กœ ์ด์ฐจ์› ์„  ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. plt.show()๋Š” ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š”๋ฐ Jupyter๋‚˜ IPython์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ž๋™์œผ๋กœ ํ‘œ์‹œ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜ธ์ถœํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๊พธ๋ฏธ๊ธฐ ๋‹ค์Œ์€ ๋ณด๋‹ค ๋ณด๊ธฐ ์ข‹๊ฒŒ ๊พธ๋ฏผ ๊ฒƒ์ด๋‹ค. # example 2 import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,50) y1 = np.cos(4*np.pi*x) y2 = np.cos(4*np.pi*x)*np.exp(-2*x) plt.plot(x, y1,'r-*', label=r'$sin(4 \pi x)$',lw=1) plt.plot(x, y2,'b--o', label=r'$ e^{-2x} sin(4\pi x) $',lw=1) plt.title(r'$sin(4 \pi x)$ vs. $ e^{-2x} sin(4\pi x)$') plt.xlabel('x') plt.ylabel('y') plt.text(0.5, -1.0, r'This is sample') plt.axis([0,1, -1.5,1.5]) plt.grid(True) plt.legend(loc='upper left') plt.tight_layout() plt.show() plt.plot(x, y,'r-*',label='sin',lw=1)์—์„œ 'r-*'์€ ๋ฌธ์ž์—ด์€ ์ƒ‰์ƒ, ์„ ํƒ€์ž…, ๋งˆ์ปค ๋“ฑ์˜ ์†์„ฑ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. 'r-*'๋Š” red, solid line, * ๋งˆ์ปค๋ฅผ ์˜๋ฏธํ•œ๋‹ค. label์€ ๋ ˆ์ „๋“œ์— ํ‘œ์‹œ๋  ๋‚ด์šฉ์ด๊ณ , lw๋Š” line width์ด๋‹ค. plt.title(title), plt.xlabel(xlabel), plt.ylabel(ylabel)์€ ์ œ๋ชฉ, X์™€ Y ์ถ• ์ œ๋ชฉ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. plt.text(x, y, text)๋กœ (x, y) ์œ„์น˜์— text๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค plt.axis([xmin, xmax, ymin, ymax])๋Š” ์ถ•์˜ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•œ๋‹ค. plt.xlim([xmin, xmax]), plt.ylim([ymin, ymax])์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. plt.grid(True)๋Š” ๊ฒฉ์ž๋ฅผ ๊ทธ๋ฆฌ๊ณ , plt.legend()๋Š” ๋ ˆ์ „๋“œ๋ฅผ ํ‘œ์‹œํ•œ๋‹ค. plt.tight_layout()์€ ์—ฌ๋ฐฑ์„ ์กฐ์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. latex ์ˆ˜์‹์„ title(), xlabel(), ylabel(), text() ๋“ฑ์— ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ rโ€™textโ€™ ๋“ฑ๊ณผ ๊ฐ™์€ r์„ ์•ž์— ๊ธฐ์ž…ํ•˜์—ฌ raw ๋ฌธ์ž์—ด์ด์–ด์•ผ ํ•œ๋‹ค. Example 1 Example 2 ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” plt.text(x, y, text, ...)๋Š” ์ถ”๊ฐ€ ์ธ์ž๋ฅผ ํ†ตํ•ด ๋ณด๋‹ค ์„ธ๋ฐ€ํ•˜๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” text์˜ ์ •๋ ฌ ์†์„ฑ์„ center, center๋กœ, ์ขŒํ‘œ๊ณ„๋Š” 0~1 ์‚ฌ์ด์˜ relative coordinate๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด๋‹ค. plt.text(20,30, "test') # ์ผ๋ฐ˜ plt.text(0.2,0.3, horizontalalignment='center',verticalalignment='center',transform=plt.gca().transAxes) # transform=ax.transAxes ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์„ ๊ณผ ๋งˆ์ปค ๋‹ค์Œ์€ ์„ ์˜ ์ƒ‰์ƒ ๋ฐ ์Šคํƒ€์ผ, ๋งˆ์ปค ๋“ฑ์˜ ์˜ต์…˜์ด๋‹ค. Subplot Subplot์€ Matlab์ฒ˜๋Ÿผ plt.subplot(nrow, ncol, inum)์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,50) y1 = np.cos(4*np.pi*x) y2 = np.cos(4*np.pi*x)*np.exp(-2*x) plt.subplot(2,1,1) plt.plot(x, y1,'r-*',lw=1) plt.grid(True) plt.ylabel(r'$sin(4 \pi x)$') plt.axis([0,1, -1.5,1.5]) plt.subplot(2,1,2) plt.plot(x, y2,'b--o',lw=1) plt.grid(True) plt.xlabel('x') plt.ylabel(r'$ e^{-2x} sin(4\pi x) $') plt.axis([0,1, -1.5,1.5]) plt.tight_layout() plt.show() ์ฃผ์š” ํ•จ์ˆ˜ ์š”์•ฝ ์ปค๋งจ๋“œ ๋ฐฉ์‹์˜ ํ•จ์ˆ˜์ธ Pyplot API ์ค‘ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. plot() subplot() title() xlabel() ylabel() axis() xlim() ylim() tight_layout() grid() legend() show() figure() text() subplots() xscale(...) : xscale("log")์ด๋ฉด ๋กœ๊ทธ ์Šค์ผ€์ผ minorticks_on(), minorticks_off() grid(True, which='both') yscale(...) : ํ•œ๊ธ€ ์„ค์ • import matplotlib.pyplot as plt # plt.rc('font', family='NanumGothicOTF') # For MacOS plt.rc('font', family='NanumGothic') # For Windows ... 4.2 IPython Python ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์ธํ„ฐ๋ž™ํ‹ฐ๋ธŒํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋ ค๋ฉด python ์ฝ˜์†”๋ณด๋‹ค๋Š” ipython ์ฝ˜์†”์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ipython์˜ %matplotlib์ด๋ผ๋Š” magic command ์™€ matplotlib.pyplot.ion(), matplotlib.pyplot.ioff()๋ฅผ ํ†ตํ•ด ์ œ์–ดํ•œ๋‹ค. ๋‹ค์Œ์˜ ๋‘ ๊ฐ€์ง€๋ฅผ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฝ˜์†” ๋‚ด์— ์ถœ๋ ฅํ•˜๋„๋ก ํ•  ๊ฒฝ์šฐ [1] %matplotlib inline ... plt.plot() ๊ฐ™์€ ๋ช…๋ น์ด ์žˆ์„ ๋•Œ๋งˆ๋‹ค ์ฝ˜์†” ๋‚ด์— ์ถœ๋ ฅ plt.show()๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„ ๋จ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋ณ„๋„์ฐฝ์— ์ถœ๋ ฅํ•˜๋ฉด์„œ, ์ธํ„ฐ๋ž™ํ‹ฐ๋ธŒํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ [1] %matplotlib qt5 [2] matplotlib.pyplot.ion() ... plt.plot() ๋“ฑ์„ ํ˜ธ์ถœํ•˜๋ฉด ๋ฐ”๋กœ ๊ทธ๋ž˜ํ”„๊ฐ€ ์—…๋ฐ์ดํŠธ plt.show()๋ฅผ ํ˜ธ์ถœํ•  ํ•„์š”๊ฐ€ ์—†์Œ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋ณ„๋„์ฐฝ์— ์ถœ๋ ฅํ•˜๋ฉด์„œ, ์ธํ„ฐ๋ž™ํ‹ฐ๋ธŒํ•˜์ง€ ์•Š๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ [1] %matplotlib qt5 [2] matplotlib.pyplot.ioff() ... plt.plot() ๋“ฑ์„ ํ˜ธ์ถœ ํ›„ plt.show()๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ๊ทธ๋ž˜ํ”„๊ฐ€ ์—†๋ฐ์ดํŠธ Python IDE๋งˆ๋‹ค ๋””ํดํŠธ๋กœ ์„ค์ •๋˜์–ด ์žˆ๋Š” ๊ฐ’์ด ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅด๋‹ค. 4.3 Matplotlib์˜ ์ดํ•ด Matplotlib์˜ ๊ตฌ๋™ ๋ฐฉ์‹ Matplotlib์€ ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์˜ API๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค. Pyplot API : ์ด์ „์ ˆ์— ์†Œ๊ฐœํ•œ Matlab๊ณผ ๊ฐ™์ด ์ปค๋งจ๋“œ ๋ฐฉ์‹. matplotlib.pyplot ๋ชจ๋“ˆ์— ํ•จ์ˆ˜๋กœ ์ •์˜๋˜์–ด ์žˆ์Œ. ๊ฐ์ฒด์ง€ํ–ฅ API : matplotlib์ด ๊ตฌํ˜„๋œ ๊ฐ์ฒด์ง€ํ–ฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ง์ ‘ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹. Pyplot API๋Š” ๊ฒฐ๊ตญ ๊ฐ์ฒด์ง€ํ–ฅ API๋กœ ํŽธ์˜ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒƒ์— ๋ถˆ๊ณผํ•˜๋ฉฐ, ์„ธ๋ฐ€ํ•œ ์ œ์–ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ๊ฐ์ฒด์ง€ํ–ฅ API๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. matplotlib ๊ฐ์ฒด์ง€ํ–ฅ API์—๋Š” FigureCanvas, Renderer, Artist๋ผ๋Š” ์„ธ ๊ฐ€์ง€ ๊ฐ์ฒด๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. FigureCanvas: ๊ทธ๋ฆผ์„ ๊ทธ๋ฆด ์˜์—ญ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด Renderer: ์บ”๋ฒ„์Šค(FigureCanvas)์— ๊ทธ๋ฆฌ๋Š” ๋„๊ตฌ ๊ฐ์ฒด Artist : Renderer๊ฐ€ FigureCanvas์— ์–ด๋–ป๊ฒŒ ๊ทธ๋ฆด ๊ฒƒ์ธ๊ฐ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด FigureCanvas์™€ Renderer๋Š” wxPython, PostScript ๋“ฑ๊ณผ ๊ฐ™์€ ์‚ฌ์šฉ์ž ์ดํ„ฐ ํŽ˜์ด์Šค ํˆดํ‚ท๊ณผ ์—ฐ๊ณ„๋˜๋Š” ๋‚ฎ์€ ์ˆ˜์ค€์˜ ์ œ์–ด๋ฅผ ๋‹ด๋‹นํ•˜๋ฉฐ, Artist๋Š” figure, text, line, patch ๋“ฑ์„ ํ‘œ์‹œํ•˜๋Š” ๋†’์€ ์ˆ˜์ค€์„ ๋‹ด๋‹นํ•˜๊ฒŒ ๋œ๋‹ค(patch๋Š” rectange, spline, path ๋“ฑ์„ ๋ชจ๋‘ ์ด๋ฅด๋Š” ์šฉ์–ด). ๋”ฐ๋ผ์„œ matplotlib ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ๋Š” Artist ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๋Š”๋ฐ ์ง‘์ค‘ํ•˜๋ฉด ๋œ๋‹ค. Artist๋Š” primitives์™€ containers๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ์œ ํ˜•์œผ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. Primitives: Line2D, Rectangle, Text, AxesImage, Patch ๋“ฑ๊ณผ ๊ฐ™์ด ์บ”๋ฒ„์Šค์— ๊ทธ๋ ค์ง€๋Š” ํ‘œ์ค€ ๊ทธ๋ž˜ํ”ฝ ๊ฐ์ฒด Containers: Axis, Axes, Figure ๋“ฑ๊ณผ ๊ฐ™์ด ์ด๋“ค primitives๊ฐ€ ์œ„์น˜ํ•˜๊ฒŒ ๋  ๋Œ€์ƒ ์ปค๋งจ๋“œ ๋ฐฉ์‹์ด ์•„๋‹Œ ๊ฐ์ฒด์ง€ํ–ฅ ๋ฐฉ์‹์œผ๋กœ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋Š” ํ‘œ์ค€์ ์ธ ๋ฐฉ๋ฒ•์€ Figure ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ด๋ฅผ ์ด์šฉํ•ด ํ•˜๋‚˜ ์ด์ƒ์˜ Axes ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , Axes ๊ฐ์ฒด์˜ ํ—ฌํผ ํ•จ์ˆ˜๋กœ primitives๋ฅผ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋Š” ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๋น„๊ตํ•œ ๊ฒƒ์ด๋‹ค. Method 1 : Pyplot API(์ปค๋งจ๋“œ ๋ฐฉ์‹)๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ์‹ import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,50) y1 = np.cos(4*np.pi*x) y2 = np.cos(4*np.pi*x)*np.exp(-2*x) plt.plot(x, y1,'r-*',lw=1) plt.plot(x, y2,'b--',lw=1) Method 2 :๊ฐ์ฒด์ง€ํ–ฅ API๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ์‹ ๊ฒƒ์ด๋‹ค. import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,50) y1 = np.cos(4*np.pi*x) y2 = np.cos(4*np.pi*x)*np.exp(-2*x) fig = plt.figure() # ์ง์ ‘ Figure ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ ax = fig.subplots() # ์ง์ ‘ axes๋ฅผ ์ƒ์„ฑ ax.plot(x, y1,'r-*',lw=1) # ์ƒ์„ฑ๋œ axes์— ๋Œ€ํ•œ plot() ๋ฉค๋ฒ„ ์ง์ ‘ ํ˜ธ์ถœ ax.plot(x, y2,'b--',lw=1) Method 3 :์ด ๋‘˜์„ ์กฐํ•ฉํ•˜์—ฌ Figure์™€ Axes๋ฅผ plt.subpolots()๋ผ๋Š” ํŽธ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด๋‹ค. import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,50) y1 = np.cos(4*np.pi*x) y2 = np.cos(4*np.pi*x)*np.exp(-2*x) fig, ax = plt.subplots() # plt.subplots() ํŽธ์˜ ํ•จ์ˆ˜๋Š” Figure ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Figure.subplots()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฆฌํ„ด ax.plot(x, y1,'r-*',lw=1) ax.plot(x, y2,'b--',lw=1) ์‚ฌ์‹ค plt.plot(x, y, โ€ฆ) Pyplot ํ•จ์ˆ˜๋Š” Line2D๋ผ๋Š” Axes์— ํฌํ•จ๋˜๋Š” primitive๋ฅผ ํ˜„์žฌ์˜ Axes๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋งŒ์•ฝ Figure, Axes ๊ฐ์ฒด๊ฐ€ ์—†๋‹ค๋ฉด ์ด๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค. ๋”ฐ๋ผ์„œ (2)์™€ ๊ฐ™์€ ๋ฐฉ์‹์ด matplotlib์˜ ๋‚ด๋ถ€์˜ ๊ตฌ๋™ ์ƒํ™ฉ์„ ์ž˜ ํ‘œํ˜„ํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์˜ˆ์ธ (3)์—์„œ plt.subplots()๋Š” Figure ๊ฐ์ฒด์™€ Axes ๊ฐ์ฒด๋ฅผ ๋™์‹œ์— ๋ฆฌํ„ดํ•œ๋‹ค. ๋˜ํ•œ plt.subplots(2,1) ๋“ฑ๊ณผ ๊ฐ™์ด ํ˜ธ์ถœํ•œ ๋ฉด 2*1 subplot์— ๋Œ€์‘ํ•˜๋Š” Figure ๊ฐ์ฒด์™€ Axes ๊ฐ์ฒด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜์—ฌ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ํŽธ๋ฆฌํ•˜๋‹ค. ๋งŽ์€ ์“ฐ๋Š” ํ•จ์ˆ˜๋กœ gca()๊ฐ€ ์žˆ๋Š” ํ˜„์žฌ์˜ Axes ๊ฐ์ฒด๋ฅผ ๊ตฌํ•ด์ค€๋‹ค. Axes์™€ Axis Artist ๊ฐ์ฒด์—์„œ Figure, Axes, Axis์˜ ๊ฐœ๋…์ด ์ค‘์‹ฌํ•œ๋ฐ ๋‹ค์Œ์€ ์ด๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. Axes์™€ Axis๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. Subplot์„ ๊ทธ๋ฆฌ๋Š” ๋ฐฉ์‹ ๋‹ค์Œ ์ฝ”๋“œ๋Š” subplot์„ ๊ทธ๋ฆฐ ์˜ˆ์ด๋‹ค. ์™ผ์ชฝ์˜ ๊ธฐ์กด ๋ฐฉ์‹๊ณผ ๋น„๊ตํ•  ๋•Œ subplots()๋กœ Figure ๊ฐ์ฒด์™€ Axes ๊ฐ์ฒด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ , Axes ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ plot(), grid(), set_xlabel() ๋“ฑ์˜ ๋ฉ”์˜๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์˜€๋‹ค. ๋ฉ”์˜๋“œ ์ด๋ฆ„์—์„œ ์ผ๋ถ€ ์ฐจ์ด ๋‚˜๋Š” ๊ฒƒ์„ ์ œ์™ธํ•˜๋ฉด ๋‘ ๋ฐฉ์‹์ด ๋งค์šฐ ์œ ์‚ฌํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. Method 1 : subplot()์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,50) y1 = np.cos(4*np.pi*x) y2 = np.cos(4*np.pi*x)*np.exp(-2*x) plt.subplot(2,1,1) plt.plot(x, y1,'r-*',lw=1) plt.grid(True) plt.ylabel(r'$sin(4 \pi x)$') plt.axis([0,1, -1.5,1.5]) plt.subplot(2,1,2) plt.plot(x, y2,'b--o',lw=1) plt.grid(True) plt.xlabel('x') plt.ylabel(r'$ e^{-2x} sin(4\pi x) $') plt.axis([0,1, -1.5,1.5]) plt.tight_layout() plt.show() Method 2 :๊ฐ์ฒด์ง€ํ–ฅ API๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ์‹ ๊ฒƒ์ด๋‹ค. import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,50) y1 = np.cos(4*np.pi*x) y2 = np.cos(4*np.pi*x)*np.exp(-2*x) fig = plt.figure() ax = fig.add_subplot(2,1,1) ax.plot(x, y1,'r-*',lw=1) ax.grid(True) ax.set_ylabel(r'$sin(4 \pi x)$') ax.axis([0,1, -1.5,1.5]) ax = fig.add_subplot(2,1,2) ax.plot(x, y2,'b--o',lw=1) ax.grid(True) ax.set_xlabel('x') ax.set_ylabel(r'$ e^{-2x} sin(4\pi x) $') ax.axis([0,1, -1.5,1.5]) fig.tight_layout() plt.show() Method 3 : subplots()๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,1,50) y1 = np.cos(4*np.pi*x) y2 = np.cos(4*np.pi*x)*np.exp(-2*x) fig, ax = plt.subplots(2,1) ax[0].plot(x, y1,'r-*',lw=1) ax[0].grid(True) ax[0].set_ylabel(r'$sin(4 \pi x)$') ax[0].axis([0,1, -1.5,1.5]) ax[1].plot(x, y2,'b--o',lw=1) ax[1].grid(True) ax[1].set_xlabel('x') ax[1].set_ylabel(r'$ e^{-2x} sin(4\pi x) $') ax[1].axis([0,1, -1.5,1.5]) plt.tight_layout() plt.show() 4.4 ์ฝ”๋“œ ์กฐ๊ฐ getp()๋ฅผ ์ด์šฉํ•œ ์ •๋ณด ์•Œ์•„๋‚ด๊ธฐ gca(), gcf()์™€ axis() Figure ํฌ๊ธฐ์™€ Face color ๊ทธ๋ฆผ ์ €์žฅํ•˜๊ธฐ ์ข…ํšก๋น„์™€ ์ถ• ์—†์• ๊ธฐ ๋‘ ๊ฐœ์˜ Y ์ถ• ๋‹ค์–‘ํ•œ ๋„ํ˜• ๊ทธ๋ฆฌ๊ธฐ Subplot ์œ„์น˜ ๋ฐ ํฌ๊ธฐ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ๋Œ€์ˆ˜์ถ•๊ณผ ๋ณด์กฐํ‹ฑ plt.grid(True, which="both") getp()๋ฅผ ์ด์šฉํ•œ ์ •๋ณด ์•Œ์•„๋‚ด๊ธฐ matplotlib ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ •๋ณด๋Š” plt.getp() ํ•จ์ˆ˜๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. >>> plt.getp(fig) agg_filter = None alpha = None animated = False axes = [<matplotlib.axes._subplots.AxesSubplot object at ... children = [<matplotlib.patches.Rectangle object at 0x0000013... clip_box = None clip_on = True ... gca(), gcf()์™€ axis() gca()๋กœ ํ˜„์žฌ์˜ Axes๋ฅผ, gcf()๋กœ ํ˜„์žฌ์˜ Figure ๊ฐ์ฒด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค. ์ด ๋‘ ํ•จ์ˆ˜๋Š” ๋งŒ์•ฝ ํ˜„์žฌ์˜ Axes๋‚˜ Figure๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค. import matplotlib.pyplot as plt plt.gca().plot([1,2,3]) # ์ด์ „ ๊ทธ๋ฆผ์ด ์—†์œผ๋ฉด ์ƒ์„ฑ plt.gca().plot([7,8,9]) # ํ˜„์žฌ ๊ทธ๋ฆผ์— ๊ทธ๋ฆผ axis()๋Š” ์ถ•๊ณผ ๊ด€๋ จ๋œ ํŽธ์˜ ํ•จ์ˆ˜์ด๋‹ค. axis() : ์ถ•์˜ [xmin, xmax, ymin, ymax]๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. axis([xmin, xmax, ymin, ymax]) : ์ถ•์˜ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•œ๋‹ค. axis(โ€˜offโ€™) : ์ถ•๊ณผ ๋ผ๋ฒจ์„ ์—†์•ค๋‹ค. axis(โ€˜equalโ€™) axis(โ€˜scaledโ€™) axis(โ€˜tightโ€™) axis(โ€˜imageโ€™) axis(โ€˜autoโ€™) axis(โ€˜normalโ€™) axis(โ€˜squareโ€™) Figure ํฌ๊ธฐ์™€ Face color Figure์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•˜๊ฑฐ๋‚˜ face color๋ฅผ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋ฐฉ๋ฒ• 1 : figure()๋กœ ์ƒ์„ฑํ•  ๋•Œ ์ธ์ž๋กœ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• plt.figure(figsize=(10,5),facecolor=โ€™yellowโ€™) ๋ฐฉ๋ฒ• 2: ์ด๋ฏธ ์ƒ์„ฑ๋˜๋Š” ์žˆ๋Š” figure๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ• # already constructed โ€ฆ. # for example : fig, ax = plt.subplots() # or fig = plt.gcf() fig.set_size_inches(10,5) fig.patch.set_facecolor('white') ์ฐธ๊ณ ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ™˜๊ฒฝ์— ๋”ฐ๋ผ figure์˜ face color๊ฐ€ gray๋กœ ์„ค์ •๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ•์žฌ๋กœ white๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ๋ณด๊ธฐ ์ข‹๋‹ค. ๊ทธ๋ฆผ ์ €์žฅํ•˜๊ธฐ ๊ทธ๋ฆผ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” figure()๋กœ ๊ทธ๋ฆผ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“  ํ›„ savefig() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. >>> f = plt.figure(); โ€ฆ # plt.plot(...) # ... >>> f.savefig('AAA.png') fig, ax = plt.subplots(2,2) fig.set_size_inches(14,8) fig.set_facecolor('white') ์ข…ํšก๋น„์™€ ์ถ• ์—†์• ๊ธฐ Matplotlib์„ ์ผ์ข…์˜ ์บ”๋ฒ„์Šค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆด ๋•Œ๋Š” ๊ฐ€๋กœ์„ธ๋กœ ์ถ•, ํ”„๋ ˆ์ž„ ๋“ฑ์ด ์—†์–ด์•ผ ํ•œ๋‹ค. import matplotlib.pyplot as plt import numpy as np t = np.arange(0.0, 1.0 + 0.01, 0.01) s = np.cos(2*2*np.pi*t) plt.plot(t, s, '-', lw=2) # aspect ratio plt.axes().set_aspect('equal', 'datalim') # axis ์—†์• ๊ธฐ plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) # frame์„  ์—†์• ๊ธฐ plt.axes().set_frame_on(False) plt.tight_layout() ์•„๋ž˜์˜ ์„ธ ๋ผ์ธ ๋Œ€์‹  plt.axes().axis('off') ์„ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค. ๋งŒ์•ฝ ์ถ• ๋ผ๋ฒจ๋งŒ ์—†์• ๊ณ  ์‹ถ๋‹ค๋ฉด set_ticklabels([])๋ฅผ ์ ์šฉํ•œ๋‹ค. import numpy as np import matplotlib.pyplot as plt theta = np.linspace(22,65,100) h = theta*np.pi/180 # in radian f2bzV = 1/( np.sin(h)* np.cos(h))/2 av = np.sin(h)/np.cos(h) f = plt.figure() plt.subplot(2,1,1) plt.plot(theta, f2bzV) plt.axis([22,65,0,2]) plt.ylabel(r'normalized $f_2$ for given $V$') plt.gca().xaxis.set_ticklabels([]) plt.grid() plt.subplot(2,1,2) plt.plot(theta, 1/f2bzV) # av) plt.axis([22,65,0,2]) plt.ylabel(r'normalized $V$ for given $f_2$') plt.xlabel(r'$\theta$') plt.grid() plt.tight_layout() plt.savefig('compare.png') ๋‘ ๊ฐœ์˜ Y ์ถ• ๋‘ ๊ฐœ์˜ Y ์ถ•์„ ๊ฐ–๋Š” ๊ทธ๋ž˜ํ”„๋Š” Axes ๊ฐ์ฒด๋ฅผ x์ถ•์„<NAME>๋„๋ก ๊ฒน์น˜๋ฉด ๋œ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์˜๋“œ๊ฐ€ Axes.twinx()์ด๋‹ค. import matplotlib.pyplot as plt import numpy as np fig, ax1 = plt.subplots() t = np.arange(0.01, 10.0, 0.01) s1 = np.exp(t) ax1.plot(t, s1, 'b-') ax1.set_xlabel('time (s)') # Make the y-axis label, ticks and tick labels match the line color. ax1.set_ylabel('exp', color='b') ax1.tick_params('y', colors='b') ax2 = ax1.twinx() s2 = np.sin(2 * np.pi * t) ax2.plot(t, s2, 'r.') ax2.set_ylabel('sin', color='r') ax2.tick_params('y', colors='r') fig.tight_layout() plt.show() ์•„๋ž˜๋ž˜๋Š” pyplot API๋ฅผ ํ™œ์šฉํ•œ ์ฝ”๋“œ๋กœ ๋™์ผํ•œ ์ผ์„ ํ•œ๋‹ค. import matplotlib.pyplot as plt import numpy as np t = np.arange(0.01, 10.0, 0.01) s1 = np.exp(t) plt.plot(t, s1, 'b-') plt.xlabel('time (s)') # Make the y-axis label, ticks and tick labels match the line color. plt.xlabel('exp', color='b') plt.gca().tick_params('y', colors='b') plt.gca().twinx() s2 = np.sin(2 * np.pi * t) plt.plot(t, s2, 'r.') plt.ylabel('sin', color='r') plt.gca().tick_params('y', colors='r') plt.tight_layout() plt.show() ๋‹ค์–‘ํ•œ ๋„ํ˜• ๊ทธ๋ฆฌ๊ธฐ plot() ๋ช…๋ น์€ ๋‚ด๋ถ€์ ์œผ๋กœ Line2D ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ Axes ๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค. ๋น„์Šทํ•˜๊ฒŒ ๋‹ค์–‘ํ•œ ๋„ํ˜• ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ Axes ๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋„ํ˜•์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๋„ํ˜•์€ Patch import matplotlib.pyplot as plt import matplotlib.patches as patches from matplotlib.path import Path # circle circle = patches.Circle((0,0),radius=1.,color = '.75') plt.gca().add_patch(circle) #rectangle rect = patches.Rectangle((2.5, -.5), 2., 1., color = '.75') plt.gca().add_patch(rect) # Ellipse ellipse = patches.Ellipse((0, -2.), 2., 1., angle = 45., color ='.75') plt.gca().add_patch(ellipse) # Path verts = [ (3., -2), # left, bottom (3., -1.), # left, top (4., -1.), # right, top (4., -2), # right, bottom (3., -2.), # ignored ] codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY, ] path = Path(verts, codes) patch = patches.PathPatch(path, color='.75') # facecolor='orange', lw=2, plt.gca().add_patch(patch) plt.gca().set_xlim(-2,2) plt.gca().set_ylim(-2,2) plt.axis('scaled') plt.grid(True) plt.show() Subplot ์œ„์น˜ ๋ฐ ํฌ๊ธฐ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• gridspec์„ ํ†ตํ•ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋Œ€์ˆ˜์ถ•๊ณผ ๋ณด์กฐํ‹ฑ ... plt.plot(r1.Tp, r1.Sap,'ro', r1.Tv, r1.Sav,'r') plt.plot(r2.Tp, r2.Sap,'bo', r2.Tv, r2.Sav,'b--') plt.grid(True, which="both") ... plt.xlabel('Period') plt.xlim([0.01,10]) plt.xscale("log") plt.ylabel('Spectral acceleration [g]') plt.minorticks_on() plt.grid(True, which='both') plt.show() 4.5 3์ฐจ์› ๊ทธ๋ž˜ํ”ฝ Matplotlib์€ mpl_tookits๋ผ๋Š” ๋ชจ๋“ˆ๋กœ 3์ฐจ์› ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ์ปค๋งจ๋“œ ๋ฐฉ์‹์ธ Pyplot API๋Š” ์—†๊ณ , ํ•ญ์ƒ ๊ฐ์ฒด์ง€ํ–ฅ API๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. Axes3D ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ ํ›„ 3์ฐจ์› ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ๋‹ค์Œ์€ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” 3์ฐจ์› ๊ทธ๋ž˜ํ”„์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ์ฝ”๋“œ ๊ณจ๊ฒฉ์ด๋‹ค. import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # or ax = fig.gca(projection='3d') # line/scatter plot : x, y, z๋Š” 1์ฐจ์› ๋ฐฐ์—ด ax.plot(x, y, z,...) ax.scatter(x, y, z,...) # surface, wireframe plot : X, Y, Z๋Š” 2์ฐจ์› ๋ฐฐ์—ด ax.plot_surface(X, Y, Z,...) ax.plot_wireframe(X, Y, Z,...) # bar3d plot : x, y, z๋Š” 1์ฐจ์› ๋ฐฐ์—ด ax.bar3d(x, y, width,depth, z,...) from mpl_toolkits.mplot3d import Axes3D๋Š” ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š์ง€๋งŒ ๋ฐ˜๋“œ์‹œ ์ž„ํฌํŠธ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค. fig.add_subplot(111, projection='3d')์—์„œ Axes3D ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค. ax = fig.gca(projection='3d')๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค. Axes3D ๊ฐ์ฒด์˜ ์—ฌ๋Ÿฌ ๊ทธ๋ฆฌ๊ธฐ ๋ฉค๋ฒ„ ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉ๋˜๋Š” x, y, z ๋ฐฐ์—ด์˜ ์ฐจ์›์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. 4.4.1 Line and scatter plot ๋‹ค์Œ line plot๊ณผ scatter plot์˜ ์˜ˆ์ด๋‹ค. import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np # line plot fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # ax = fig.gca(projection='3d') theta = np.linspace(-4*np.pi, 4*np.pi, 100) z = np.linspace(-2,2,100) r = z**2+1 x = r*np.sin(theta) y = r*np.cos(theta) ax.plot(x, y, z, label='parametric curve') ax.scatter(x, y, z) ax.legend() ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') fig.tight_layout() plt.show() 4.4.2 Surface plot ๋‹ค์Œ์€ surface plot๊ณผ wireframe plot์˜ ์˜ˆ์ด๋‹ค. import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np fig = plt.figure() ax = fig.gca(projection='3d') X=np.arange(-5,5,0.25) Y=np.arange(-5,5,0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2+Y**2) Z = np.sin(R) surf = ax.plot_surface(X, Y, Z, cmap='coolwarm',linewidth=0, antialiased=False) wire = ax.plot_wireframe(X, Y, Z, color='r',linewidth=0.1) fig.colorbar(surf,shrink=0.5, aspect=5) fig.tight_layout() plt.show() 4.4.3 bar3d plot ๋‹ค์Œ์€ bar3d plot์˜ ์˜ˆ์ด๋‹ค. import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # setup the figure and axes fig = plt.figure(figsize=(8, 3)) ax1 = fig.add_subplot(121, projection='3d') ax2 = fig.add_subplot(122, projection='3d') # fake data _x = np.arange(4) _y = np.arange(5) _xx, _yy = np.meshgrid(_x, _y) x, y = _xx.ravel(), _yy.ravel() top = x + y bottom = np.zeros_like(top) width = depth = 1 ax1.bar3d(x, y, bottom, width, depth, top, shade=True) ax1.set_title('Shaded') ax2.bar3d(x, y, bottom, width, depth, top, shade=False) ax2.set_title('Not Shaded') plt.show() 5. NumPy + SciPy ํ™œ์šฉ SciPy๋ž€? SciPy('์‚ฌ์ดํŒŒ์ด'๋ผ๊ณ  ์ฝ์Œ)๋Š” ๊ณผํ•™๊ธฐ์ˆ  ๊ณ„์‚ฐ์„ ์œ„ํ•œ Python ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. NumPy, Matplotlib, pandas, SymPy์™€ ์—ฐ๊ณ„๋˜์–ด ์žˆ๋‹ค(ํŠนํžˆ NumPy์™€). ๊ฐ€๋Šฅํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ด€๋ จ ๋ถ€ํŒจ ํ‚ค์ง€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Clustering package (scipy.cluster) Constants (scipy.constants) Discrete Fourier transforms (scipy.fftpack) Integration and ODEs (scipy.integrate) Interpolation (scipy.interpolate) Input and output (scipy.io) Linear algebra (scipy.linalg) Miscellaneous routines (scipy.misc) Multi-dimensional image processing (scipy.ndimage) Orthogonal distance regression (scipy.odr) Optimization and root finding (scipy.optimize) Signal processing (scipy.signal) Sparse matrices (scipy.sparse) Sparse linear algebra (scipy.sparse.linalg) Compressed Sparse Graph Routines (scipy.sparse.csgraph) Spatial algorithms and data structures (scipy.spatial) Special functions (scipy.special) Statistical functions (scipy.stats) Statistical functions for masked arrays (scipy.stats.mstats) Low-level callback functions ์„ค์น˜ Anaconda ์‚ฌ์šฉ์ž๋Š” ๋ณ„๋„์˜ ์ธ์Šคํ†จ ์—†์ด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. NumPy์™€์˜ ๊ด€๊ณ„์™€ ์„ค์ • ํ™•์ธ SciPy๋Š” NumPy ์ƒ์œ„์—์„œ ๊ตฌ๋™๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ •๋„๋กœ ์ดํ•ดํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค. SciPy๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ NumPy์˜ ndarray๋ฅผ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์ผ๋ถ€ ํŒจํ‚ค์ง€๋Š” ์ค‘๋ณต๋˜์ง€๋งŒ(์˜ˆ, ์„ ํ˜•๋Œ€์ˆ˜ - numpy.linalg vs. scipy.linalg, ์ด์‚ฐํ‘ธ๋ฆฌ์—๋ณ€ํ™˜ - numpy.fft vs. scipy.fftpack) SciPy๊ฐ€ ๋ณด๋‹ค ํ’๋ถ€ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. NumPy, SciPy ๋“ฑ์€ ์ˆ˜์น˜๊ณ„์‚ฐ์„ ์œ„ํ•œ ํŒจํ‚ค์ง€์ด๋ฏ€๋กœ ์„ฑ๋Šฅ์ด ์ค‘์š”ํ•˜๋‹ค. ํŠนํžˆ BLAS์™€ LAPACK ๋“ฑ ๊ธฐ๋ณธ ์ˆ˜์น˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์–ด๋–ค ๊ฒƒ์„ ์‚ฌ์šฉํ–ˆ๋Š”๊ฐ€๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. ์ตœ๊ทผ Intel MKL์„ ๋„์ž…ํ•˜์˜€๋‹ค. numpy.show_config()์™€ scipy.show_config()๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. >>> import numpy >>> numpy.show_config() mkl_info: libraries = ['mkl_rt'] library_dirs = ['C:/ProgramData/Anaconda3\\Library\\lib'] define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)] include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl', ...] blas_mkl_info: <์œ„์™€ ๋™์ผ> blas_opt_info: <์œ„์™€ ๋™์ผ> lapack_mkl_info: <์œ„์™€ ๋™์ผ> lapack_opt_info: <์œ„์™€ ๋™์ผ> >>> import scipy >>> scipy.show_config() openblas_lapack_info: NOT AVAILABLE lapack_mkl_info: libraries = ['mkl_rt'] library_dirs = ['C:/ProgramData/Anaconda3\\Library\\lib'] define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)] include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2016.4.246\\windows\\mkl', ...] lapack_opt_info: <์œ„์™€ ๋™์ผ> blas_mkl_info: <์œ„์™€ ๋™์ผ> blas_opt_info: <์œ„์™€ ๋™์ผ> >>> Timing ๊ณผํ•™๊ธฐ์ˆ  ๊ณ„์‚ฐ ์—ฐ์‚ฐ์€ ๊ณ„์‚ฐ ์†๋„๊ฐ€ ์ค‘์š”ํ•œ ์ฒ™๋„์ด๋‹ค. ์–ด๋–ค ์ž‘์—…์ด ์ˆ˜ํ–‰๋œ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๋Š” ๊ฒƒ์€ CPU time๊ณผ wall time์œผ๋กœ ๋ณดํ†ต ๊ตฌ๋ถ„ํ•œ๋‹ค. CPU time : ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ํ”„๋กœ์„ธ์„œ์˜ ์‹œ๊ฐ„์„ ํ•ฉ์‚ฐํ•œ ์‹œ๊ฐ„. time.process_time()์—์„œ ๋ฆฌํ„ด๋œ ๊ฐ’์˜ ์ฐจ์ด๋กœ ๊ณ„์‚ฐ wall time : ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ์‹œ๊ฐ„. time.perf_counter()์ด๋‚˜ time.time()์—์„œ ๋ฆฌํ„ด๋˜๋Š” ๊ฐ’์˜ ์ฐจ์ด. time_perf_counter()๊ฐ€ ๋ณด๋‹ค ์ •ํ™•ํ•œ ๊ฐ’์„ ์ œ์‹œ ์˜ˆ๋ฅผ ๋“ค์–ด A๋ผ๋Š” ์ž‘์—…์„ 2๊ฐœ ํ”„๋กœ์„ธ์„œ(์ฝ”์–ด)์—์„œ ๋™์‹œ์— ์‰ฌ์ง€์ง€์•Š๊ณ  ์ž‘์—…ํ–ˆ๋Š” ๋ฐ 2์ดˆ๊ฐ€ ๊ฑธ๋ ธ๋‹ค๋ฉด, CPU time์€ 4์ดˆ, wall time์€ 2์ดˆ์ด๋‹ค. ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐœ๋ณ„ ํ”„๋กœ์„ธ์„œ๋Š” ์ค‘๊ฐ„์— ์‰ด ๋•Œ๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ ๋‹จ์ˆœํžˆ (wall time) * (ํ”„๋กœ์„ธ์„œ ์ˆ˜)๋กœ CPU time์ด ๊ณ„์‚ฐ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค. import numpy as np import scipy.linalg import linalg A = np.random.random(size=(10000,10000)) cpuTime = time.process_time() # CPU time wallTime1 = time.perf_counter() # wall time wallTime2 = time.time() # wall time linalg.norm(A) cpuTime = time.process_time()-cpuTime # CPU time wallTime1 = time.perf_counter()-wallTime1 # wall time wallTime2 = time.time()-wallTime2 # wall time print('cpuTime : ', cpuTime) print('wallTime1 : ',wallTime1) print('wallTime2 : ',wallTime2) ์ฃผ์š” ์ฐธ๊ณ  ์‚ฌ์ดํŠธ SciPy Tutorial Python Scientific Lecture Notes 5.1 ์„ ํ˜•๋Œ€์ˆ˜ np.linalg์™€ scipy.linalg์—์„œ ์„ ํ˜•๋Œ€์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. scipy.linalg๊ฐ€ np.linalg์™€ ๋น„๊ตํ•  ๋•Œ ์ผ๋ถ€ ๊ธฐ๋Šฅ์ด ์ค‘๋ณต๋˜๋‚˜ ๋ณด๋‹ค ํ’๋ถ€ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ import numpy as np import scipy.linalg as linalg A = np.array([[1,2, -1], [2,7,4], [0,4, -1]]) b = np.array([1,0,1.2]) # matrix-vector multiplication y1 = np.matmul(A, b) y2 = np.dot(A, b) y3 = A.dot(b) # matrix-matrix multiplication B = np.array([[1,2,3,4], [-1,2,3,1], [3, -2,5,9]]) C1 = np.matmul(A, B) C2 = np.dot(A, B) C3 = A.dot(B) determinant, solve, inverse # determinant det = linalg.det(A) # solve x = linalg.solve(A, b) r = A.dot(x) - b # check 0 vector # inverse Ainv = linalg.inv(A) # vector norm norm1 = linalg.norm(x, 1) # L1 norm == sum(np.abs(x)) norm2 = linalg.norm(x) # L2 norm == np.sqrt(sum(x*x)) # normp = linalg.norm(x, p) # p norm normMax = linalg.norm(x, np.inf) # max norm == np.max(abs(x)) # matrix norm LLS(Linear Least Square) # least-square import numpy as np from scipy import linalg import matplotlib.pyplot as plt c1, c2 = 5.0, 2.0 x = np.linspace(0.1,1,100) y = c1*np.exp(-x) + c2*x z = y + 0.05 * np.max(y) * np.random.randn(len(y)) A = np.vstack((np.exp(-x),x)).T c, resid, rank, sigma = linalg.lstsq(A, z) x2 = np.linspace(0.1,1,100) y2 = c[0]*np.exp(-x2) + c[1]*x2 plt.plot(x, z,'x', x2, y2) plt.axis([0,1.1,3.0,5.5]) plt.xlabel('$x$') plt.title('Data fitting with linalg.lstsq') plt.show() ๊ณ ์œ ์น˜ ๋ฌธ์ œ # eigen D, V = linalg.eigh(A) # for real sym or complex hermitian D, V = linalg.eig(A) ๊ธฐํƒ€ pseudo-inverse generalized inverse svd lu decompositon cholesky QR schur matrix functions special matrix 5.2 ํฌ์†Œ ํ–‰๋ ฌ ๊ณตํ•™์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋งŽ์€ ๋ฌธ์ œ๋“ค์ด ๊ฝ‰ ์ฐฌ ํ–‰๋ ฌ(dense matrix)๊ฐ€ ์•„๋‹Œ ๋Œ€๋ถ€๋ถ„ 0์˜ ๊ฐ’์„ ๊ฐ–๋Š” ํฌ์†Œ ํ–‰๋ ฌ(sparse matrix)์ด๋‹ค. ๊ณ„์‚ฐ์˜ ํšจ์œจ์„ ์œ„ํ•ด์„œ ํฌ์†Œ ํ–‰๋ ฌ์€ ๊ณฑ์…ˆ, ์—ญํ–‰๋ ฌ ๋“ฑ๋“ฑ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ๋ณ„๋„์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ ์šฉ๋˜์–ด์•ผ ํ•œ๋‹ค. scipy.sparse์—์„œ๋Š” ํฌ์†Œ ํ–‰๋ ฌ์˜ ์ƒ์„ฑ, ์—ฐ์‚ฐ ๋“ฑ์„ ์œ„ํ•œ ํ•จ์ˆ˜, ๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ์„ ํ˜•๋Œ€์ˆ˜๋Š” scipy.sparse.linalg์—์„œ ์ œ๊ณตํ•œ๋‹ค. 5.3 ์ตœ์ ํ™” ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ์—ฌ๊ธฐ์—์„œ ์„ค๋ช…ํ•  ์ผ๋ถ€ ํ•จ์ˆ˜๋Š” scipy 1.2.1๋ถ€ํ„ฐ ์ง€์›๋˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฒ„์ „ ํ™•์ธ์„ ํ•˜๋„๋ก ํ•œ๋‹ค. > python >>> import scipy >>> scipy.__version__ '1.2.1' ๋งŒ์•ฝ ๋ฒ„์ „์ด ๋‚ฎ๋‹ค๋ฉด scipy๋ฅผ ์—…๊ทธ๋ ˆ์ด๋“œํ•œ๋‹ค. ๊ด€๋ฆฌ์ž๋กœ cmd๋ฅผ ์‹คํ–‰ํ•œ ํ›„ ๋‹ค์Œ์„ ์‹คํ–‰ํ•œ๋‹ค. # ์ฃผ์˜: ๊ด€๋ฆฌ์ž๋กœ cmd ์‹คํ–‰ > pip install scipy --upgrade ๊ฐœ์š” ์—ฌ๋Ÿฌ ๊ณตํ•™ ๋ฌธ์ œ๋Š” ์ตœ์ ํ™”(optimization) ๋ฌธ์ œ๋กœ ๊ท€๊ฒฐ๋œ๋‹ค. scipy.optimize ํŒจํ‚ค์ง€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ตœ์ ํ™” ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‹ค. Local optimization: ๋น„๊ตฌ์† ๋ฐ ๊ตฌ์† ์กฐ๊ฑดํ•˜์˜ multivariate scalar function์˜ ์ตœ์†Œํ™” ๋ฌธ์ œ, 'minimize' Global optimizaiton : bashinhopping, differential_evolution Least-squares minimization๊ณผ curve fitting : least_squares, curve_fit Scalar univariate function์˜ ์ตœ์†Œํ™” ๋˜๋Š” ํ•ด ์ฐพ๊ธฐ : minimizer_scalar, root_scalar Multivariate equation system์˜ ํ•ด ์ฐพ๊ธฐ : root Linear Programming : linprog ์Šค์นผ๋ผ ํ•จ์ˆ˜์˜ ํ•ด ์ฐพ๊ธฐ Univariate scalar function f(x)=0 (x๋Š” ์Šค์นผ๋ผ, ํ•จ์ˆซ๊ฐ’๋„ ์Šค์นผ๋ผ)์˜ ํ•ด๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ๋Š” scipy.optimize.root_scalar(...)๋ฅผ ์ด์šฉํ•œ๋‹ค. optimize.root_scalar(f, args=(), method=None, bracket=None, fprime=None, fprime2=None, x0=None, x1=None, xtol=None, rtol=None, maxiter=None, options=None) ์œ„์—์„œ f๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ œ๊ณตํ•ด์•ผ ํ•˜๋Š” ํ•จ์ˆ˜์ธ๋ฐ, def f(x,...) ํ˜•ํƒœ๋กœ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๊ฐ€ x์ด๊ณ , ๋‚˜๋จธ์ง€ ๋ณด์กฐ์ธ์ž๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, args๋Š” ๊ทธ ๋ณด์กฐ์ธ์ž๋ฅผ ํŠœํ”Œ๋กœ ๋„˜๊ฒจ์ค€๋‹ค. method๋Š” bisect, brentq, newton ๋“ฑ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ธฐ์ž…ํ•œ๋‹ค. ํ•ด๊ฐ€ ์กด์žฌํ•˜๋Š” ๋ฒ”์œ„๋ฅผ ์•„๋Š” ๊ฒฝ์šฐ(bracketing method)์™€ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ(non-bracketing method)๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. Bracketing method๋Š” Brent's method๋ฅผ non-bracketing method๋กœ๋Š” newton method๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. Brent's method๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ํ•ด๋ฅผ ์ฐพ์•„์•ผ ํ•˜๋Š” ๋ฒ”์œ„๋ฅผ bracket ์ธ์ž๋กœ ์ฃผ์–ด์•ผ ํ•˜๋ฉฐ ํ•ด๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ํ•ญ์ƒ ํ•ด๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. from scipy import optimize def func(x): return x**3-1 sol = optimize.root_scalar(func, bracket=[0, 3], method='brentq') if sol.converged == True: print('Solution = ', sol) Newton method๋Š” ์ดˆ๊นƒ๊ฐ’์„ x0 ์ธ์ž๋กœ fprime ์ธ์ž๋กœ๋Š” ๋ฏธ๋ถ„ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค. fprime์˜ ๋ณด์กฐ์ธ์ž๋Š” f ์ธ์ž์— ์‚ฌ์šฉ๋œ ํ•จ์ˆ˜์˜ ๋ณด์กฐ์ธ์ž์™€ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค. from scipy import optimize def func(x): return x**3-1 def derivative(x): return 3*x**2 sol = optimize.root_scalar(func, x0=1.5, method='newton',fprime=derivative) if sol.converged == True: print('Solution = ', sol) Inner function์œผ๋กœ ์ตœ์ ํ™” ๋Œ€์ƒ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ธฐ ์ตœ์ ํ™” ๋ฃจํ‹ด์€ ๋Œ€๋ถ€๋ถ„ ์‚ฌ์šฉ์ž๊ฐ€ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ตœ์ ํ™” ๋ฃจํ‹ด์— ์ธ์ž๋กœ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค. ๋ณดํ†ต์€ ์ด ํ•จ์ˆ˜๋Š” ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ ์ •๋ณด๋ฅผ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•˜๋ฏ€๋กœ, ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋ณต์žกํ•  ์—ฌ์ง€๊ฐ€ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ inner function์œผ๋กœ ๋Œ€์ƒ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ฐ„๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํด๋ž˜์Šค ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž๊ฐ€ self์ด๋‹ค. ๋”ฐ๋ผ์„œ ํด๋ž˜์Šค์˜ ์–ด๋–ค ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ตœ์ ํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” ์ •์  ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ๋‹ค์‹œ ์ •์˜ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ inner function์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Local optimization ์ตœ์ ํ™” ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์ด๋‹ค. min ( ) subject to g ( ) 0 i 1 โ€ฆ m j ( ) 0 j 1 โ€ฆ p ( ) ๋Š” ์ตœ์†Œํ™”์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ์Šค์นผ๋ผ ํ•จ์ˆ˜๋กœ ๋ชฉ์ ํ•จ์ˆ˜๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค.๋Š” ๋ฒกํ„ฐ์ด๋‹ค. i ( ) h ( ) ๋Š” ๊ตฌ์† ์กฐ๊ฑด์ด๋‹ค. ๊ตฌ์† ์กฐ๊ฑด์ด ์—†๋Š” ๊ฒฝ์šฐ๋Š” ๋น„๊ตฌ์† ์ตœ์ ํ™”(unconstrained optimization), ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ตฌ์† ์ตœ์ ํ™”(constrained optimization) ๋ฌธ์ œ๋ผ๊ณ  ํ•œ๋‹ค. scipy.optimize์—์„œ๋Š” ๋‹ค์–‘ํ•œ ์ตœ์ ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š”๋ฐ minimize(fun, x0,method=method,...) ํ•จ์ˆ˜๋กœ ์ผ์›ํ™”ํ•ด์„œ ํ’€๋„๋ก ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜์€ OptimizeResult๋ผ๋Š” ๊ฐ์ฒด์— ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์•„ ๋ฆฌํ„ดํ•œ๋‹ค. scipy.optimize.minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None)` fun, x0 : ๋ชฉ์ ํ•จ์ˆ˜์™€ ์ดˆ๊ธฐ๊ฐ’(initial guess) method : ์‚ฌ์šฉํ•  ์•Œ๊ณ ๋ฆฌ์ฆ˜(์†”๋ฒ„)๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด. Nelder-Mead, Powell, CG, BFGS, trust-ncg ๋“ฑ๋“ฑ jac, hess, hessp : ๋ชฉ์ ํ•จ์ˆ˜์˜ ์ž์ฝ”๋น„์–ธ, ํ—ค์‹œ ์•ˆ, ํ—ค์‹œ ์•ˆ๊ณผ ์ž„์˜ ๋ฒกํ„ฐ์˜ ๊ณฑ. ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋”ฐ๋ผ ํ•„์š”ํ•  ์ˆ˜๋„, ํ•„์š”ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Œ bounds, constraints : ๊ตฌ์† ์ตœ์ ํ™” ๋ฌธ์ œ์—์„œ์˜ ๊ตฌ์†์กฐ๊ฑด์„ ๋ถ€๊ณผํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ tol:: ๋ฐ˜๋ณต ๊ณ„์‚ฐ ์‹œ ์‚ฌ์šฉ๋˜๋Š” ์ข…๋ฃŒ๋ฅผ ์œ„ํ•œ ํ—ˆ์šฉ์น˜(tolerance for termination) options : dict ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง€๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ณ„ ์˜ต์…˜. maxiter์™€ disp๋Š” ๊ณตํ†ต์ด๊ณ  show_options(solver='minimize',method-'nelder-mead') ๋“ฑ๊ณผ ๊ฐ™์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ณ„ ์˜ต์…˜ ์กฐํšŒ ๊ฐ€๋Šฅ maxiter : ์ •์ˆ˜๋กœ ์ตœ๋Œ€ ๋ฐ˜๋ณตํšŒ์ˆ˜ disp : ์ˆ˜๋ ด ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ• ์ง€ ์—ฌ๋ถ€(True ๋˜๋Š” False) ์ตœ์ ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋”ฐ๋ผ ๋น„๊ตฌ์†, ๊ตฌ์† ์กฐ๊ฑด์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์ฃผ๊ฐ€ ๋‹ค๋ฅด๋ฉฐ, ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ ์ž์ฝ”๋น„์•ˆ, ํ—ค์‹œ ์•ˆ ๋“ฑ์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ๊ทธ ํŠน์ง•์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. ๋น„๊ตฌ์† ์ตœ์ ํ™”(unconstrained minimization) method jac hess, hessp bounds constraints Nelder-Mead X X X X Powell X X CG O X BFGS O X Newton-CG O O L-BFGS-B O X O TNC O X O COBYLA X X X O SLSQP O X O O dogleg O O trust-ncg O O trust-exact O O trust-krylov O O ๋น„๊ตฌ์† ์ตœ์ ํ™” ์˜ˆ๋ฅผ ๋“ค์–ด `BFGS import numpy as np from scipy.optimize import minimize def rosen(x): """The Rosenbrock function""" return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0) def rosen_der(x): xm = x[1:-1] xm_m1 = x[:-2] xm_p1 = x[2:] der = np.zeros_like(x) der[1:-1] = 200*(xm-xm_m1**2) - 400*(xm_p1 - xm**2)*xm - 2*(1-xm) der[0] = -400*x[0]*(x[1]-x[0]**2) - 2*(1-x[0]) der[-1] = 200*(x[-1]-x[-2]**2) return der def rosen_hess(x): x = np.asarray(x) H = np.diag(-400*x[:-1],1) - np.diag(400*x[:-1],-1) diagonal = np.zeros_like(x) diagonal[0] = 1200*x[0]**2-400*x[1]+2 diagonal[-1] = 200 diagonal[1:-1] = 202 + 1200*x[1:-1]**2 - 400*x[2:] H = H + np.diag(diagonal) return H def rosen_hess_p(x, p): x = np.asarray(x) Hp = np.zeros_like(x) Hp[0] = (1200*x[0]**2 - 400*x[1] + 2)*p[0] - 400*x[0]*p[1] Hp[1:-1] = -400*x[:-2]*p[:-2]+(202+1200*x[1:-1]**2-400*x[2:])*p[1:-1] \ -400*x[1:-1]*p[2:] Hp[-1] = -400*x[-2]*p[-2] + 200*p[-1] return Hp x0 = np.array([1.3,0.7,0.8,1.9,1.2]) #x0 = np.zeros([-1,2,3,1,5]) res = minimize(rosen, x0,method='nelder-mead', options={'xtol':1e-8, 'disp':True}) print('nelder-mead:',res.x) res = minimize(rosen, x0, method='BFGS', jac=rosen_der, options={'disp': True}) print('BFGS:',res.x) res = minimize(rosen, x0, method='Newton-CG', jac=rosen_der, hess=rosen_hess, options={'xtol': 1e-8, 'disp': True}) print('Newton-CG with Hessian:',res.x) res = minimize(rosen, x0, method='Newton-CG', jac=rosen_der, hessp=rosen_hess_p, options={'xtol': 1e-8, 'disp': True}) print('Newton-CG with Hessian product:',res.x) res = minimize(rosen, x0, method='trust-ncg', jac=rosen_der, hess=rosen_hess, options={'gtol': 1e-8, 'disp': True}) print('trust-ncg with Hessian:',res.x) res = minimize(rosen, x0, method='trust-ncg', jac=rosen_der, hessp=rosen_hess_p, options={'gtol': 1e-8, 'disp': True}) print('trust-ncg with Hessian product:',res.x) res = minimize(rosen, x0, method='trust-krylov', jac=rosen_der, hess=rosen_hess, options={'gtol': 1e-8, 'disp': True}) res = minimize(rosen, x0, method='trust-krylov', jac=rosen_der, hessp=rosen_hess_p, options={'gtol': 1e-8, 'disp': True}) import scipy.optimize as optimize optimize.show_options(solver='minimize',method='nelder-mead') 5.4 ์ด์‚ฐ ํ‘ธ๋ฆฌ์—๋ณ€ํ™˜ NumPy์—์„œ๋Š” numpy.fft์—์„œ Matlab๊ณผ ๊ฑฐ์˜ ๋™์ผํ•œ<NAME>์œผ๋กœ DFT๋ฅผ ์ง€์›ํ•œ๋‹ค. ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋Š” fft(x), ifft(x), fftfreq(n), fftshift(x) ๋“ฑ์ด๋‹ค. ๋˜ํ•œ matplotlib์˜ stem(x)๋ฅผ stem plot์„ ๊ทธ๋ฆฌ๋Š” ๋ฐ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. FFT์˜ ์ •์˜๋Š” Matlab๊ณผ ๋™์ผํ•˜๋‹ค. ๋‹ค๋งŒ ์ธ๋ฑ์Šค๊ฐ€ 0๋ถ€ํ„ฐ๋ผ๋Š” ์ ์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. 3.6.1 FFT์˜ ์ •์˜์™€ ์‹ค์ˆ˜ ์‹ ํ˜ธ FFT์˜ ์ •์˜ ๋‹ค์Œ์€ m X์ด๊ณ  ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๊ฐ€ N ์ผ ๋•Œ FFT์™€ IFFT์˜ ์ •์˜์ด๋‹ค. FFT n โˆ‘ = N 1 m exp ( j ฯ€ n) where = , , , โˆ’ Inverse FFT m 1 โˆ‘ = N 1 n exp ( 2 m N ) where = , , , โˆ’ ์‹ค์ˆ˜ ์‹ ํ˜ธ์˜ FFT ์‹ค์ˆ˜ ์‹ ํ˜ธ์˜ FFT๋Š” ํ•ญ์ƒ ์‹ ํ˜ธ์˜ 1/2 ์ ์„ ๊ธฐ์ค€์œผ๋กœ ๊ณต์•ก ๊ด€๊ณ„๋ฅผ ์ด๋ฃฌ๋‹ค. import numpy as np import matplotlib.pyplot as plt plt.ion() x = np.array([1.0,0.8,0.1,0.2,0.5,0.1,0,0.2]) # even number # x = np.array([1.0,0.8,0.1,0.2,0.5,0.1,0.2]) # odd number xf = np.fft.fft(x) plt.subplot(3,1,1) plt.stem(x); plt.ylabel('x') plt.title('Even number real signal') # plt.title('Odd number real signal') plt.subplot(3,1,2) plt.stem(np.abs(xf)); plt.ylabel('abs(X)') plt.subplot(3,1,3) plt.stem(np.angle(xf)); plt.ylabel('angle(X)') plt.tight_layout() Even number real signal Odd number real signal 3.6.2 Fourier transform ๊ทผ์‚ฌ์™€ ์‹ ํ˜ธ๋ถ„์„ Fourier transform์˜ ์ •์˜์™€ DFT ๊ทผ์‚ฌ Fourier transform์€ ํ•™์ž์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•˜๊ฒŒ ์ •์˜๋œ๋‹ค. ( ) X ( ) ์Œ์— ๋Œ€ํ•ด ๋ณดํ†ต ์‚ฌ์šฉํ•˜๋Š” ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Forward Fourier Transform ( ) 1 ฯ€ โˆ’ โˆž ( ) exp ( j t ) ฯ‰ Inverse Fourier Transform ( ) โˆซ โˆž x ( ) exp ( j t ) t ์œ„ ์ •์˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ DFT๋ฅผ ์ด์šฉํ•ด ๊ทผ์‚ฌ์ ์œผ๋กœ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Forward Fourier Transform : X = fft(x)*dt Inverse Fourier Transform : x = ifft(X)/dt ์—ฌ๊ธฐ์—์„œ dt๋Š” ์ƒ˜ํ”Œ๋ง ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ ๋“ฑ์ด๋‹ค. DFT๋ฅผ ์ด์šฉํ•œ ์‹ ํ˜ธ๋ถ„์„ ๋‹ค์Œ์€ 60 Hz ์™€ 120 Hz์˜ ์‚ฌ์ธ ๊ณก์„ ์ด ์ค‘์ฒฉ๋œ ์‹ ํ˜ธ ( ) 0.7 sin ( 120 t ) sin ( 240 t ) N ( = , = ) ๋ฅผ ๋”ฐ๋ฅด๋Š” ๋…ธ์ด์ฆˆ๊ฐ€ ํฌํ•จ๋œ ์‹ ํ˜ธ์— ๋Œ€ํ•ด ์ฃผํŒŒ์ˆ˜ ๋ถ„์„์„ ํ•œ ์˜ˆ์ด๋‹ค. ์ƒ˜ํ”Œ๋ง ์ฃผํŒŒ์ˆ˜๋Š” 100 Hz์ด๊ณ  1500๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ธฐ๋กœ ํ•œ๋‹ค. import numpy as np import matplotlib.pyplot as plt fmax = 1000 # sampling frequency 1000 Hz dt = 1/fs # sampling period N = 1500 # length of signal t = np.arange(0, N)*dt # time = [0, dt, ..., (N-1)*dt] s = 0.7*np.sin(2*np.pi*60*t) + np.sin(2*np.pi*120*t) x = s+2*np.random.randn(N) # random number Normal distn, N(0,2)... N(0,2*2) plt.subplot(2,1,1) plt.plot(t[0:51],s[0:51],label='s') plt.plot(t[0:51],x[0:51],label='x') plt.legend() plt.xlabel('time'); plt.ylabel('x(t)'); plt.grid() # Fourier spectrum df = fmax/N # df = 1/N = fmax/N f = np.arange(0, N)*df # frq = [0, df, ..., (N-1)*df] xf = np.fft.fft(x)*dt plt.subplot(2,1,2) plt.plot(f[0:int(N/2+1)],np.abs(xf[0:int(N/2+1)])) plt.xlabel('frequency(Hz)'); plt.ylabel('abs(xf)'); plt.grid() plt.tight_layout() ์ฃผํŒŒ์ˆ˜ ๋ถ„์„ ๊ฒฐ๊ณผ 60 Hz ๋ฐ 120 Hz์—์„œ ํ”ผํฌ๋ฅผ ๋ณด์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 3.6.3 ์ฃผํŒŒ์ˆ˜ ํ•ด์„ ์˜ˆ ์„ ํ˜•๊ณ„์˜ ๊ฒฝ์šฐ ๋™์  ํ•ด์„์€ ์ฃผํŒŒ์ˆ˜ ์˜์—ญ์—์„œ ํ•ด์„์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹ค์Œ์€ ์Šคํ”„๋ง, ๋Œํผ, ์งˆ๋Ÿ‰์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋‹จ ์ž์œ  ๋„๊ณ„์— ๋Œ€ํ•œ ์ฃผํŒŒ์ˆ˜ ํ•ด์„ ์ฝ”๋“œ์ด๋‹ค. ์งˆ๋Ÿ‰ 1, ๊ณ ์œ ์ฃผ๊ธฐ 2 sec, ๊ฐ์‡ ๋น„ 0.1 ์ธ ์‹œ์Šคํ…œ์— ๋Œ€ํ•ด Half-sine ํ•จ์ˆ˜๊ฐ€ ์™ธ๋ถ€ ํ•˜์ค‘์œผ๋กœ ์ฃผ์–ด์งˆ ๊ฒฝ์šฐ 1024๊ฐœ์˜ ๋ฐ์ดํ„ฐ, ์ด ์‹œ๊ฐ„ 40 sec ์— ๋Œ€ํ•œ ํ•ด์„์„ ์ˆ˜ํ–‰ํ•œ ๊ฒƒ์ด๋‹ค. import numpy as np import matplotlib.pyplot as plt # Signal properties N=1024; T=40; dt =T/N t = np.arange(0, N*dt, dt) # [0, dt, ..., (N-1)*dt] # System properties Tn = 2.; xi = 0.1; wn = 2*np.pi/Tn m = 1 c = 2*m*wn*xi k = wn*wn # Excitation force p0 = 1; td = 2; p = np.zeros(N) p[t<td] = np.sin(np.pi*t[t<td]/td) # Frequency response df = 1/T f = np.arange(0, N*df, df) #[0, df,...,(N-1)*df] w = 2*np.pi*f pf = np.fft.fft(p)*dt H = 1/(-w*w*m+c*m*1j+k) uf = pf*H for i in range(0, int(N/2)): uf[N-i-1] = np.conjugate(uf[i+1]) ut = np.real(np.fft.ifft(uf)/dt) plt.subplot(2,2,1) plt.plot(t, p) plt.title('Excitation p(t)'); plt.xlabel('time[sec]') plt.grid() plt.subplot(2,2,2) plt.plot(f[0:int(N/2+1)],abs(pf[0:int(N/2+1)])) plt.title(r'Excitation p($\omega$)'); plt.xlabel('frq [Hz]') plt.grid() plt.subplot(2,2,3) plt.plot(t, ut) plt.title('Response u(t)'); plt.xlabel('time[sec]') plt.grid() plt.subplot(2,2,4) plt.plot(f[0:int(N/2+1)],np.abs(uf[0:int(N/2+1)])) plt.title(r'Reponse u($\omega$)'); plt.xlabel('frq [Hz]') plt.grid() plt.tight_layout() 6. ์œ ์šฉํ•œ ํŒจํ‚ค์ง€์™€ ํ•จ์ˆ˜ ๊ฐ™์€ ๋ผ์ธ ์ถœ๋ ฅํ•˜๊ธฐ print()๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ์ค„์„ ๋ฐ”๊พธ๊ฒŒ ๋œ๋‹ค. ๊ฐ™์€ ์ค„์— ์“ฐ๋ ค๋ฉด '\r'์„ ์ ์šฉํ•˜๊ณ , ํ”Œ๋Ÿฌ์‹ฑ์„ ํ•˜๋ฉด ๋œ๋‹ค. ์ € ์ˆ˜์ค€์˜ stdout.write()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. from sys import stdout from time import sleep for i in range(10): stdout.write('\r%d' % i) stdout.flush() sleep(1) stdout.write('\n') print() ๋ฌธ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. from time import sleep for i in range(10): print(' %d'%i, end='',flush=True) sleep(1) print('\n') ์œ„ ์ฝ”๋“œ๋Š” cmd์—์„œ ์‚ฌ์šฉ๊ฑฐ๋‚˜ spyder์—์„œ๋Š” ์ •์ƒ์ž‘๋™ํ•˜๋‚˜, Visual Studio 2017์—์„œ๋Š” ์ค„๋ฐ”๊ฟˆ์„ ํ•˜๊ฒŒ ๋œ๋‹ค(๋ฒ„๊ทธ๋กœ ๋ณด์ž„) ๋‹ค์Œ์€ keras์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์‹ค์ œ ์ฝ”๋“œ์ด๋‹ค. import sys import time for i in range(10): s = '{:03d} / 100'.format(i) sys.stdout.write(s) time.sleep(1) sys.stdout.write('\b' * len(s)) sys.stdout.write('\r') Findpeak ๋ฐ์ดํ„ฐ ๋ถ„์„ ์‹œ ์ฒจ๋‘ ๊ฐ’์„ ๊ตฌํ•ด์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์—ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ•˜๋ฉด Python์—์„œ ์‚ฌ์šฉ๋œ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ์ œ์‹œํ•˜๊ณ  ์žˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” peakdectect๋ผ๋Š” matlab์„ ํ‰๋‚ด ๋‚ธ ์ฝ”๋“œ๊ฐ€ ๊ฐ€์žฅ ํšจ์œจ์ ์ด๋ผ๊ณ  ์ œ์‹œํ•œ๋‹ค. import numpy as np import matplotlib.pyplot as plt from peakdetect import * from scipy import signal x = np.arange(0, 10*np.pi, 0.05) y = np.sin(x)+0.5*np.cos(2*x) #ipeak = signal.find_peaks_cwt(y, np.arange(1,2)) #ipeak, x[ipeak], y[ipeak] #([32], array([ 1.6]), array([ 0.9995736])) ipeaks = peakdetect(y, lookahead=1) ipeaksmax = ipeaks[0]; ipeaksmin = ipeaks[1]; ipeakmax = [ipeaksmax[i][0] for i in range(len(ipeaksmax)) ] ipeakmin = [ipeaksmin[i][0] for i in range(len(ipeaksmin)) ] plt.plot(x, y, x[ipeakmax],y[ipeakmax],'o',x[ipeakmin],y[ipeakmin],'*') 6.1 Excel ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ(OpenPyXL) Python์—์„œ Excel์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ COM์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ COM ๋ฌด๊ด€ํ•œ ๋…๋ฆฝ ํŒจํ‚ค์ง€๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ๋…๋ฆฝ ํŒจํ‚ค์ง€๋กœ๋Š” xlwt, xlsxwriter, openpyxl์ด ์žˆ์œผ๋ฉฐ, OpenPyXL์ด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค. Anaconda ๋ฐฐํฌ๋ณธ์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋ณ„๋„์˜ ์ธ์Šคํ†จ ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ณ„๋„ ์ธ์Šคํ†จ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. > pip install openpyxl OpenPyXL์€ xlsx ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋”ฉํ•œ ํ›„ ์ฝ๊ฑฐ๋‚˜ ์ˆ˜์ • ๋”ฐ์œ„์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ํ•˜๋‹ค. ์ดํ›„ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์›๋ณธ ํŒŒ์ผ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์ฃผํ•˜๋Š” ํŒŒ์ผ์„ ์ƒˆ๋กœ์šด ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ž„์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ๋‹ค์Œ์€ ๊ธฐ์กด ํŒŒ์ผ์—์„œ ์ฝ์–ด์„œ ์ž‘์—… ํ›„ ๋‹ค์‹œ ์ €์žฅํ•˜๋Š” ์ƒ˜ํ”Œ์ด๋‹ค. # Tips : ascii to int, and back # >>> ord('a') # 97 # >>> chr(97) # 'a' from openpyxl import Workbook from openpyxl import load_workbook import numpy as np filename = 'SW-Top.xlsx' sheetName = 'S' startCol = 'F' # .... startCol = 'F' # .... F G H I J K # .... Sxx Syy, Szz Sxy Syz Szx outputFileName = filename[0:filename.rfind('.')] + '-new.xlsx' ns = ord('F')-ord('A') wb = load_workbook(filename) # load to memory # wb.get_sheet_names() ws = wb[sheetName] # selet sheet max_row = ws.max_row ws[chr(ord('F')+6)+str(1)] = 'P1' ws[chr(ord('F')+7)+str(1)] = 'P2' ws[chr(ord('F')+8)+str(1)] = 'P3(Zero)' for i in range(2, max_row+1): Sxx = ws[chr(ord('F')+0)+str(i)].value Syy = ws[chr(ord('F')+1)+str(i)].value Szz = ws[chr(ord('F')+2)+str(i)].value Sxy = ws[chr(ord('F')+3)+str(i)].value Syz = ws[chr(ord('F')+4)+str(i)].value Szx = ws[chr(ord('F')+5)+str(i)].value S = np.array([ [Sxx, Sxy, Szx], [Sxy, Syy, Syz], [Szx, Syz, Szz]]) (P, V) = np.linalg.eig(S) # allway (-neg, +pos, 0) ws[chr(ord('F')+6)+str(i)] = P[1] ws[chr(ord('F')+7)+str(i)] = P[0] ws[chr(ord('F')+8)+str(i)] = P[2] wb.save(outputFileName) Workbook์—์„œ worksheet๋ฅผ iteration ํ•˜๋Š” ๊ฒƒ์€ workbook.worksheets ๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. from openpyxl import Workbook from openpyxl import load_workbook import numpy as np filename = '๋‹จ์–ด์žฅ-2013.10.xlsx' wb = load_workbook(filename) for ws in wb.worksheets: print(ws.title) ws = wb.worksheets[0] # first worksheet ... ws = wb.active # active worksheet ... Excel ๋ฐ์ดํ„ฐ๋ฅผ clipboard๋ฅผ ๋ณต์‚ฌํ•œ ํ›„ ์‚ฌ์šฉํ•˜๊ธฐ import numpy as np import pandas as pd df= pd.read_clipboard() # If you have selected the headers # df = pd.read_clipboard(header=None) # If you haven't selected the headers df[โ€˜Tag1โ€™] # Pandas Series๋กœ ๋“ค์–ด์˜ค๋Š” ๋ฐ numpy 1d arry์™€ ๋™์ผ x = df[โ€˜Tag1โ€™].as_matrix() # ๊ฐ•์žฌ๋กœ numpy๋กœ ๋ฐ”๊ฟ€ ๊ฒฝ์šฐ x = np.array(df['Tag1']) ์ด๋ฉด (n,) ํ˜•ํƒœ์˜ 1์ฐจ์› ๋ฐฐ์—ด x = df['Tag1']).as_matrxi() ์ด๋ฉด (1, n) ํ˜•ํƒœ์˜ 2์ฐจ์› ๋ฐฐ์—ด 6.2 ์‹ฌ๋ฒŒ๋ฆญ ๋งค์Šค(SymPy) SymPy ํŒจํ‚ค์ง€๋Š” Matlab์˜ Symbolic Math Toolbox์ฒ˜๋Ÿผ Symbolic math๋ฅผ ์ง€์›ํ•œ๋‹ค. Anaconda ๋ฐฐํฌ๋ณธ์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋ณ„๋„์˜ ์ธ์Šคํ†จ์ด ํ•„์š” ์—†๋‹ค. ๋‹ค๋งŒ ์ˆ˜์‹์„ ๋ณด๊ธฐ ์ข‹๊ฒŒ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” TeX๋ฅผ ์„ค์น˜ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. MikTeX ์ธ์Šคํ†จ basic-miktex-2.9.6236-x64.exe ์ฃผ์˜ํ•  ์ ์€ init_printing()์„ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ˆ˜์‹์„ ์ถœ๋ ฅํ•  ๊ฐ€์žฅ ์ข‹์€ backend๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๊ตฌ๋™ํ•˜๊ฒŒ ํ•œ๋‹ค. ๋งŒ์•ฝ TeX๊ฐ€ ์ธ์Šคํ†จ ๋˜์–ด ์žˆ๋‹ค๋ฉด ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์—†๋‹ค๋ฉด matplotlib์„ ์‚ฌ์šฉํ•œ๋‹ค. Matplotlib์€ ํ–‰๋ ฌ์„ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ถœ๋ ฅ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— MikTeX ๋“ฑ TeX๋ฅผ ์ธ์Šคํ†จํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 6.3 ์Œ์„ฑํ•ฉ์„ฑ Python์œผ๋กœ TTS(Text-to-Speech, ๋˜๋Š” ์Œ์„ฑํ•ฉ์„ฑ, Speech Synthesis)๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” ์ธํ„ฐ๋„ท์ด ์—ฐ๊ฒฐ๋œ ์ƒํƒœ์—์„œ ์ž์—ฐ์Šค๋Ÿฌ์šด ํ•ฉ์„ฑ ์Œ์„ฑ์„ ์ œ๊ณตํ•˜๋Š” ๋‹ค์Œ์˜ ๋ฌด๋ฃŒ ์„œ๋น„์Šค๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. ๋‹ค์Œ google TTS ์„œ๋น„์Šค์™€ ๋„ค์ด๋ฒ„ TTS๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ฐ„๋žตํ•˜๊ฒŒ ์ •๋ฆฌํ•œ๋‹ค. Google Text to Speech API : Google์—์„œ ์ œ๊ณตํ•˜๋Š” TTS ์„œ๋น„์Šค. gTTS๋ผ๋Š” ๋ชจ๋“ˆ์„ ์ธ์Šคํ†จํ•ด์•ผ ํ•จ Clova Speech Synthesis API : ๋„ค์ด๋ฒ„์—์„œ ์ œ๊ณตํ•˜๋Š” TTS ์„œ๋น„์Šค Google Text to Speech API Python์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด gTTS์„ ์ธ์Šคํ†จํ•ด์•ผ ํ•œ๋‹ค. > pip install gTTS ๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ ์˜ˆ์ด๋‹ค. from gtts import gTTS text ="Hi, everybody. Playing with Python is fun!!!" tts = gTTS(text=text, lang='en') tts.save("helloEN.mp3") gTTS ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , save(mp3file)๋กœ ์ €์žฅํ•˜๋ฉด mp3 ํŒŒ์ผ์ด ํ•ฉ์„ฑ๋œ๋‹ค. gTTS ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ text ์ธ์ž์—๋Š” ๋ณ€ํ™˜ํ•  ๋ฌธ์ž์—ด, lang์—๋Š” ์–ธ์–ด๋ฅผ ์ง€์ •ํ•œ๋‹ค. 'en'์€ ์˜์–ด, 'ko'๋Š” ํ•œ๊ตญ์–ด ๋“ฑ์ด๋‹ค. from gtts import gTTS text ="์•ˆ๋…•ํ•˜์„ธ์š”, ์—ฌ๋Ÿฌ๋ถ„. ํŒŒ์ด์ฌ์œผ๋กœ ๋…ธ๋Š” ๊ฒƒ์€ ์žฌ๋ฏธ์žˆ์Šต๋‹ˆ๋‹ค!!!" tts = gTTS(text=text, lang='ko') tts.save("helloKO.mp3") 'en'์œผ๋กœ ์ง€์ •ํ–ˆ์„ ๋•Œ ํ•œ๊ธ€์ด text์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด ์ด๋ฅผ ๋ฌด์‹œํ•˜์ง€๋งŒ, 'ko'์ผ ๋•Œ text ๋‚ด์˜ ์˜๋ฌธ์€ ๋ฌด์‹œ๋˜์ง€ ์•Š๊ณ  ์Œ์„ฑํ•ฉ์„ฑ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค(์•„์ฃผ ์ด์ƒํ•จ) ์˜์–ด๋Š” ์—ฌ์ž<NAME>, ํ•œ๊ธ€์€ ๋‚จ์ž<NAME>์ด๋‹ค(๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ) ์ƒ์„ฑ๋œ ์Œ์„ฑ ํŒŒ์ผ์„ "๋ชจ๋…ธ, 24000 Hz"์ด๋‹ค. ์˜์–ด, ํ•œ๊ธ€์„ ์„ž๊ฑฐ๋‚˜ ๋ฌธ์ž์—ด์„ ์ด์–ด์„œ mp3์— ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. from gtts import gTTS tts_en = gTTS(text=text, lang='en') tts_kr = gTTS(text='์•ˆ๋…•ํ•˜์„ธ์š”',lang='ko') f = open(tempFileName,'wb') tts_en.write_to_fp(f) # ์˜์–ด๋กœ ๋„ค ๋ฒˆ ๋งํ•˜๊ณ  tts_en.write_to_fp(f) tts_en.write_to_fp(f) tts_en.write_to_fp(f) tts_kr.write_to_fp(f) # ํ•œ๊ธ€๋กœ ํ•œ๋ฒˆ ๋งํ•˜๊ธฐ f.close() Clova Speech Synthesis API (๋„ค์ด๋ฒ„ TTS) ๋„ค์ด๋ฒ„ ๊ฐœ๋ฐœ์ž ์‚ฌ์ดํŠธ์—์„œ ์˜คํ”ˆ API ์ด์šฉ ์‹ ์ฒญ์„ ํ•ด์•ผ ํ•˜๋ฉฐ, ํ•˜๋ฃจ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์šฉ๋Ÿ‰์€ "10,000๊ธ€์ž/์ผ"์ด๋‹ค. ์ž์„ธํ•œ ์‚ฌ์šฉ๋ฒ•์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ๋œ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” ๊ฐœ๋žต์ ์œผ๋กœ, ์˜ค๋ฅ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์„ค๋ช…ํ•œ๋‹ค. import os import sys import urllib.request client_id = "YOUR_CLIENT_ID" client_secret = "YOUR_CLIENT_SECRET" encText = urllib.parse.quote("๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค ๋„ค์ด๋ฒ„") data = "speaker=mijin&speed=0&text=" + encText url = "https://openapi.naver.com/v1/voice/tts.bin" request = urllib.request.Request(url) request.add_header("X-Naver-Client-Id",client_id) request.add_header("X-Naver-Client-Secret",client_secret) response = urllib.request.urlopen(request, data=data.encode('utf-8')) rescode = response.getcode() if(rescode==200): print("TTS mp3 ์ €์žฅ") response_body = response.read() with open('1111.mp3', 'wb') as f: f.write(response_body) else: print("Error Code:" + rescode) "YOUR_CLIENT_ID"์™€ "YOUR_CLIENT_SECRET"์€ ๋„ค์ด๋ฒ„ OpenAPI ์‹ ์ฒญ ์‹œ ์ฃผ์–ด์ง€๋Š” ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•ดํ– ํ•œ๋‹ค. Python ์†Œ์Šค ํŒŒ์ผ์ด ๋ฐ˜๋“œ์‹œ UTF-8๋กœ ์ธ์ฝ”๋”ฉ๋˜์–ด์•ผ ํ•œ๋‹ค(ํ•œ๊ธ€์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ. ์‚ฌ์šฉํ•˜๋Š” ์—๋””ํ„ฐ์— ๋”ฐ๋ผ ANSI(์—๋””ํ„ฐ์— ๋”ฐ๋ผ EUC-KR, CP949 ๋“ฑ์œผ๋กœ ํ‘œ์‹œ)๋ฅผ ๋””ํดํŠธ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜ํ•œ๋‹ค. ์ฝ”๋“œ์—์„œ data = "speaker=mijin&speed=0&text=" + encText๊ฐ€ ์Œ์„ฑ์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. speaker์—์„œ<NAME>๋ฅผ ์ง€์ •ํ•œ๋‹ค. ํ•œ๊ตญ ๋‚จ๋…€ : jinho, mijin ์˜์–ด ๋‚จ๋…€ : matt, clara ์ผ๋ณธ์–ด ๋‚จ๋…€ : shinji, yuri ์ค‘๊ตญ์–ด ๋‚จ๋…€ : liangliang, meimei ์ŠคํŽ˜์ธ์–ด ๋‚จ๋…€ : jose, carmen ํ•œ๊ตญ<NAME>(jinho, mijin)์€ ์˜๋ฌธ์„ ์ฝ์„ ์ˆ˜ ์žˆ์ง€๋งŒ(๋ฐœ์Œ ์•ˆ ์ข‹์Œ), ์˜์–ด<NAME>(matt, clara)๋Š” ํ•œ๊ธ€์„ ์ž…๋ ฅํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ํ•œ๊ธ€ ๋ถ€๋ถ„์„ ์ฝ์ง€ ๋ชปํ•œ๋‹ค. speed์—์„œ -5~5 ์‚ฌ์ด์˜ ์ •์ˆ˜๋กœ ์Œ์„ฑ ์žฌ์ƒ์†๋„๋ฅผ ์ง€์ •ํ•œ๋‹ค. -5์ด๋ฉด 0.5๋ฐฐ ๋น ๋ฅธ, 5์ด๋ฉด 0.5๋ฐฐ ๋Š๋ฆฐ, 0์ด๋ฉด ์ •์ƒ ์†๋„์ด๋‹ค. text๋กœ ์Œ์„ฑ ํ•ฉ์„ฑํ•  ๋ฌธ์ž์—ด์„ ์ง€์ •ํ•œ๋‹ค. ์› ๋ฌธ์ž์—ด์ด ๋ฐ˜๋“œ์‹œ UTF-8๋กœ ์ธ์ฝ”๋”ฉ๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ์ด๋ฅผ ๋‹ค์‹œ URL encoding์œผ๋กœ ๋ณ€ํ™˜ํ•œ ๊ฐ’์„ ์ „์†กํ•ด์•ผ ํ•œ๋‹ค. ์ด ๋•Œ๋ฌธ์— Python ์†Œ์Šค๋Š” ๋ฐ˜๋“œ์‹œ UTF-8๋กœ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค. ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 5000์ž์˜ ํ…์ŠคํŠธ๋ฅผ ํ•ฉ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. text ํ•„๋“œ์— ์‰ผํ‘œ(,)๋ฅผ ๋„ฃ์œผ๋ฉด ๊ณ ๊ธˆ ์‰ฌ์—ˆ๋‹ค๊ฐ€ ๋งํ•˜๊ณ , .\n์„ ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ๋ถ„๋œ ๋ฌธ์žฅ์œผ๋กœ ํ•ฉ์„ฑ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋‚ด์ค€๋‹ค. (์˜ˆ, '๊ทธ๋Š” ๋ฐฉ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ณ , ๊ทธ๋…€๋Š” ์ง‘ ๋ฐ–์œผ๋กœ ๋‚˜๊ฐ”๋‹ค. ์ด์œ ๋Š” ์•Œ ์ˆ˜ ์—†์—ˆ๋‹ค.') ํ•œ๊ธ€๊ณผ ์˜๋ฌธ ์—ฌ๋ถ€ ๊ฒ€ํ†  ๋ฌธ์ž์—ด์— ํ•œ๊ธ€๊ณผ ์˜์–ด๊ฐ€ ์„ž์—ฌ ์žˆ๋Š”์ง€ ๊ฒ€ํ† ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. import re pkor = re.compile('[ใ„ฑ-ใ…Ž|ใ…-ใ…ฃ|๊ฐ€-ํžฃ]') peng = re.compile('[a-z|A-Z]') text = "Hello world ํ•œ๊ธ€ ์ธ๊ฐ€" text = "Hello world" text = "ํ•œ๊ธ€" text = "123-12" mkor = pkor.search(text) meng = peng.search(text) if mkor and meng: print("KOR + ENG") elif mkor and not meng: print("KOR only") elif not mkor and meng: print("ENG only") else: print("no KOR or ENG") 6.4 mp3 ํŒŒ์ผ ํ”Œ๋ ˆ์ด Python์€ wav ํŒŒ์ผ์— ๋Œ€ํ•œ ์ง€์›์€ ํ‘œ์ค€๋ฐฐํฌ๋ณธ ์ฐจ์›์—์„œ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” mp3 ํŒŒ์ผ์— ๋Œ€ํ•œ ์ง€์›์„ ์œ„ํ•ด์„œ๋Š” ์™ธ๋ถ€ ํŒจํ‚ค์ง€์˜ ์„ค์น˜ ๋ฐ ์‚ฌ์šฉ๋ฒ• ํ™•์ธ์ด ํ•„์š”ํ•˜๋‹ค. ์˜ค๋””์˜ค ํŒจํ‚ค์ง€์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ •๋ณด๋Š” ๋‹ค์Œ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. Audio in Python Python Audio Material ์œ„์˜ ๋ฌธ์„œ๋งŒ์œผ๋กœ๋Š” MP3 ํŒŒ์ผ ํ”Œ๋ ˆ์ด๊ฐ€ ๊ฐ€๋Šฅํ•œ์ง€ ๋“ฑ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ž˜ ๋‚˜์™€ ์žˆ์ง€ ์•Š๋‹ค. ๋‹ค์Œ์€ ์ง์ ‘ ํ…Œ์ŠคํŠธํ•ด ๋ณธ ๋ช‡๋ช‡ ๊ฒฐ๊ณผ์ด๋‹ค. PyAudio : ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์ด๋‚˜ mp3 ํŒŒ์ผ์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค. pyglet : AvBin์„ ์ถ”๊ฐ€๋กœ ์ธ์Šคํ†จํ•˜๋ฉด mp3 ํŒŒ์ผ์„ ์ง€์›ํ•œ๋‹ค. ๋‹จ์ ์œผ๋กœ๋Š” ipython ์‰˜ ๋‚ด์—์„œ๋Š” ์‹คํ–‰์ด ์•ˆ ๋œ๋‹ค๋Š” ์ ์ด๋‹ค. playsound : ํ•œ๋ฒˆ play ํ•œ ํŒŒ์ผ์„ ์‚ญ์ œํ•  ์ˆ˜ ์—†๋‹ค(๊ณ„์† ํŒŒ์ผ์„ ์žก๊ณ  ์žˆ์Œ) Pygame : ๋น„๋””์˜ค ๊ฒŒ์ž„์šฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‚˜, ์ œํ•œ์ ์œผ๋กœ MP3 ํŒŒ์ผ์„ ์ง€์›ํ•œ๋‹ค. libVLC :<NAME>์ƒ ํ”Œ๋ ˆ์ด์–ด VLC์˜ ๋ผ์ด๋ธŒ๋Ÿฌ์ด๋‹ค. VLC๋ฅผ ์ธ์Šคํ†จ ํ›„, ํŒŒ์ด์ฌ ๋ฐ”์ธ๋”ฉ์ธ vlc-python์„ pip๋กœ ์ธ์Šคํ†จํ•˜๋ฉด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. VLC ์ธ์Šคํ†จ ์‹œ Python์˜ 32๋น„ํŠธ ๋ฐ 64๋น„ํŠธ์— ์ผ์น˜ํ•˜๋„๋ก VLC๋ฅผ ์ธ์Šคํ†จํ•ด์•ผ ํ•œ๋‹ค. QtMultimedia : Qt์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ€ํ‹ฐ๋ฏธ๋””์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ. PyQt5๋‚˜ PySide2 ๋“ฑ์„ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. pyglet pyglet ํŒจํ‚ค์ง€๋ฅผ ์ธ์Šคํ†จ(์ฝ˜์†” ์ฐฝ์—์„œ > pip install pyglet) ํ•˜๊ณ , AvBin์„ ์ง์ ‘ ์ธ์Šคํ†จํ•ด์•ผ ํ•œ๋‹ค(AvBin์€ mp3 ํŒŒ์ผ ์ง€์›์„ ์œ„ํ•ด) ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. import pyglet song = pyglet.media.load('thesong.ogg') song.play() pyglet.app.run() ์ฝ˜์†” ์ฐฝ์—์„œ > python testPyglet.py์™€ ๊ฐ™์ด ์‹คํ–‰ํ•  ๋•Œ๋Š” ์œ„์™€ ๊ฐ™์ด ํ•˜๋ฉด ๋œ๋‹ค. Python ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋‚ด์—์„œ ์‹คํ–‰ํ•  ๋•Œ๋Š” ๋งˆ์ง€๋ง‰ pyglet.app.run()์„ ์‹คํ–‰ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. ๋‹จ์ ์€ IPython ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋‚ด์—์„œ๋Š” ์‹คํ–‰์ด ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ด๋‹ค. Pygame Pygame ํŒจํ‚ค์ง€๋ฅผ pip๋กœ ์ธ์Šคํ†จ(์ฝ˜์†” ์ฐฝ์—์„œ > pip install pygame) ํ•˜๋ฉด ์‚ฌ์šฉ ์ค€๋น„๊ฐ€ ๋๋‚œ๋‹ค. ๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ ์˜ˆ์ด๋‹ค. import pygame music_file = "1.mp3" # mp3 or mid file freq = 16000 # sampling rate, 44100(CD), 16000(Naver TTS), 24000(google TTS) bitsize = -16 # signed 16 bit. support 8, -8,16, -16 channels = 1 # 1 is mono, 2 is stereo buffer = 2048 # number of samples (experiment to get right sound) # default : pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096) pygame.mixer.init(freq, bitsize, channels, buffer) pygame.mixer.music.load(music_file) pygame.mixer.music.play() clock = pygame.time.Clock() while pygame.mixer.music.get_busy(): clock.tick(30) pygame.mixer.quit() pygame.mixer.init(freq, bitsize, channels, buffer)์—์„œ ์ดˆ๊ธฐํ™”ํ•˜๋Š”๋ฐ ๋””ํดํŠธ๋กœ ํ˜ธ์ถœํ•˜๋ฉด ์‚ฌ์šด๋“œ์˜ ์†๋„๊ฐ€ ๋น„์ •์ƒ์ผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ์ค‘์š”ํ•œ ๊ฒƒ์€ sampling rate๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ, Pygame์€ mp3 ์ •๋ณด ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—†๊ณ , Audicity๋‚˜ VLC ๋“ฑ์„ ํ†ตํ•ด ์‚ฌ์šฉํ•˜๋ ค๋Š” mp3 ํŒŒ์ผ์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค. ๋ณดํ†ต CD์—์„œ ์ง์ ‘ ๋ฝ‘์€ mp3๋Š” 44100์ด๊ณ , Clova Speech Synthesis API (๋„ค์ด๋ฒ„ TTS)์˜ ์Œ์„ฑํ•ฉ์„ฑ๋Š” 16000, Google Text to Speech API๋Š” 24000 ๋“ฑ์ด๋‹ค. pygame.mixer.music.play() ํ˜ธ์ถœ ํ›„ while ๋ฃจํ”„๊ฐ€ ์—†๋‹ค๋ฉด ์ฝ˜์†”์—์„œ๋Š” mp3 ํŒŒ์ผ์ด ํ”Œ๋ ˆ์ด๋˜์ง€ ์•Š๋Š”๋‹ค(๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๊ธฐ ๋•Œ๋ฌธ). ํ•˜์ง€๋งŒ Python interpreter๋กœ ์‹คํ–‰ํ•  ๋•Œ๋Š” ์—†์–ด๋„ ๋ฌด๊ด€ํ•˜๋‹ค. pygame.mixer.quit()๋ฅผ ํ˜ธ์ถœํ•ด์•ผ mp3 ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๋Š” ๋“ฑ๊ณผ ๊ฐ™์€ ํŒŒ์ผ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค(ํ˜ธ์ถœ ์ „์—๋Š” ํŒŒ์ผ์„ mixer์—์„œ ๊ณ„์† ์‚ฌ์šฉํ•˜๋Š” ์ค‘) ์œ„ ์˜ˆ๋Š” ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ์˜ˆ์ด๋ฉฐ, Pygame์€ ๋ฉˆ์ถ”๊ธฐ( pygame.mixer.music.stop() ), ๋ณผ๋ฅจ ์„ค์ •(์˜ˆ pygame.mixer.music.set_volume(0.8)), ํŽ˜์ด๋“œ์•„์›ƒ(์˜ˆ pygame.mixer.music.fadeout(1000)) ๋“ฑ ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ž์„ธํ•œ ์„ค๋ช…์€ Pygame ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. libVLC ์„ค์น˜ ๋ฐฉ๋ฒ•์€ ๋จผ์ € VLC๋ฅผ ๊น”๊ณ  python-vlc๋ฅผ ์ธ์Šคํ†จํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ œ๋Š” 64๋น„ํŠธ Python ์‚ฌ์šฉ ์‹œ VLC ์—ญ์‹œ 64 ๋น„ํŠธ๋ฅผ ์ธ์Šคํ†จํ•ด์•ผ ํ•˜๋Š”๋ฐ ํ˜„์žฌ ๋ฒ„์ „์ธ 3.0.0์˜ 64๋น„ํŠธ VLC ์ธ์Šคํ†จ๋Ÿฌ๊ฐ€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค. 32๋น„ํŠธ Python ์ด๋ฉด ๊ณต์‹ ์‚ฌ์ดํŠธ์ธ www.videolan.org์—์„œ VLC 32๋น„ํŠธ 3.0.0๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ์„ค์น˜ํ•œ๋‹ค. 64๋น„ํŠธ Python ์ด๋ฉด nightlies.videolan.org์— ์ ‘์†ํ•˜์—ฌ ์•„์ง ๋ฆด๋ฆฌ์Šค๋˜์ง€ ์•Š์€ 64๋น„ํŠธ VLC 4.0.0์„ ๋‹ค์šด๋กœ๋“œํ•ด ์„ค์น˜ํ•œ๋‹ค. VLC ์„ค์น˜ ์œ„์น˜(C:\Program Files\VideoLAN\VLC)๋ฅผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ PATH์— ์ถ”๊ฐ€ํ•œ๋‹ค. python-vlc ํŒจํ‚ค์ง€๋ฅผ ์ธ์Šคํ†จ(์ฝ˜์†” ์ฐฝ์—์„œ > pip install pyton-vlc) ํ•œ๋‹ค. ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค import vlc import time instance = vlc.Instance() #Create a MediaPlayer with the default instance player = instance.media_player_new() #Load the media file media = instance.media_new('test.mp3') #Add the media to the player player.set_media(media) #Play for 10 seconds then exit player.play() time.sleep(10) ๋งˆ์ง€๋ง‰์— time.sleep(10)์€ player.play() ๋˜๋Š” ๋™์‹œ์— ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ Python ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์‹คํ–‰ํ•  ๋•Œ >>> player.play(); time.sleep(10) ๋“ฑ๊ณผ ๊ฐ™์ด ํ•œ ๋ผ์ธ์—์„œ ์‹คํ–‰ํ•˜๋„๋ก ํ•œ๋‹ค. ๋ณด๋‹ค ์ƒ์„ธํ•œ ์‚ฌ์šฉ๋ฒ•์€ Playing audio in Python with libVLC์™€ libVLC ์œ„ํ‚ค๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. ์˜ค๋””์˜ค ํŽธ์ง‘ ํ”„๋กœ๊ทธ๋žจ mp3๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ ์˜ค๋””์˜ค ํŽธ์ง‘ ํ”„๋กœ๊ทธ๋žจ์ด ํ•„์š”ํ•˜๋‹ค. ๋‹ค์Œ์€ ๋ฌด๋ฃŒ ์˜ค๋””์˜ค ํŽธ์ง‘ ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. Audicity 6.5 PySide2 ์ด ์ฑ…์˜ ์ž๋งค ์ฑ…์ธ ๊ณตํ•™์ž๋ฅผ ์œ„ํ•œ PySide2 ์ฐธ์กฐ 6.6 Markdown+MathJax+Pandoc ์ˆ˜์‹์ด ํฌํ•จ๋œ ๊ณตํ•™ ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์€ Markdown+MathJax๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋•Œ ์ž‘์„ฑ๋œ ๋ฌธ์„œ๋Š” Visual Studio Code ๋”ฐ์œ„์—์„œ Preview๋ฅผ ํ†ตํ•ด ํฌ๋งทํŒ…๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, Pandoc์„ ์‚ฌ์šฉํ•˜๋ฉด docx๋‚˜ html ๋“ฑ์˜ ๋ฌธ์„œ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. Markdown : html ๋“ฑ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ์ œ์‹œ๋œ ๊ฐ„๋‹จํ•œ ํ…์ŠคํŠธ ๋ฌธ์„œ ํฌ๋งท MathJax : html์—์„œ latex ์ˆ˜์‹์„ ํ‘œ์‹œํ•˜๋Š” js ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Pandoc : ๋‹ค์–‘ํ•œ ๋ฌธ์„œ ํฌ๋งท ๋ณ€ํ™˜ ์œ ํ‹ธ๋Ÿฌํ‹ฐ. ์˜ˆ๋ฅผ ๋“ค์–ด Markdown ๋ฌธ์„œ๋ฅผ ์›Œ๋“œ(docx), ์›น(html) ๋“ฑ์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฐธ๊ณ ๋กœ wikidocs ์‚ฌ์ดํŠธ ์—ญ์‹œ Markdown + Mathjax ์กฐํ•ฉ์œผ๋กœ ๋งˆํฌ๋‹ค์šด ๋ฌธ์„œ ๋‚ด์— Latex ์ˆ˜์‹์„ ์“ฐ๊ณ  ์ด๋ฅผ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚ธ๋‹ค. HTML ๋ฌธ์„œ์— MathJax ์‚ฌ์šฉํ•˜๊ธฐ https://www.mathjax.org/ HTML์—์„œ latex ์ˆ˜์‹์„ ํ‘œ์‹œํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” js ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์•‹. ์ตœ์‹  ๋ฒ„์ „์€ Version 3์—์„œ๋Š” 3.2.2, Version 2์—์„œ๋Š” 2.7.9์ด๋‹ค. MathJax๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด HTML ๋ฌธ์„œ์˜ header์— ํฌํ•จ ์‹œ๋ฉด ๋œ๋‹ค. ๋ฒ„์ „ 2 <head> ... <script async src="https://cdn.jsdelivr.net/npm/MathJax@2/MathJax.js? config=TeX-AMS-MML_CHTML"></script> ๋˜๋Š” <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" type="text/javascript"></script> ... </head> ... ๋ฒ„์ „ 3 <head> ... <script src="https://polyfill.io/v3/polyfill.min.js? features=es6"></script> <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script> ๋˜๋Š” <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script> <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-mml-chtml.js"></script> ... </head> ... MathJax 2.7.5๋ถ€ํ„ฐ๋Š” MathJax.js ๋Œ€์‹  latest.js ์„ ์“ธ ๊ฒƒ์„ ์ถ”์ฒœํ•˜๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ 2.x.x ๋Œ€์˜ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ์ž๋™ ๋ณ€ํ™˜ํ•ด ์ค€๋‹ค. ๋ฒ„์ „ 3๋„ ๋น„์Šทํ•˜๊ฒŒ mathjax@3๋Š” ์ตœ์‹  ๋ฒ„์ „, mathjax@3.2.2๋Š” ์ง€์ •ํ•œ ๋ฒ„์ „์„ ์˜๋ฏธํ•œ๋‹ค. MathJax๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ off-line์—์„œ๋„ ๊ตฌ๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ๋Š” latest.js๋ฅผ ์“ฐ๋ฉด ์•ˆ ๋œ๋‹ค. https://github.com/mathjax/MathJax์—์„œ Version 2 ๋˜๋Š” Version 3 ์ตœ์‹  ๋ฒ„์ „์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์••์ถ• ํ•ด์ œํ•œ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค. (MathJax-2.7.9.zip, MathJax-3.2.2.zip) ๋ฒ„์ „ 2 <head> ... <script src="file:///D:/MathJax-2.7.9/MathJax.js? config=TeX-MML-AM_CHTML" type="text/javascript"></script> ... </head> ... ๋ฒ„์ „ 3 <head> ... <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script> <script src="file:///D:/MathJax-3.2.2/es5/tex-mml-chtml.js"></script> ... </head> ... ์ฐธ๊ณ ๋กœ ์ธํ„ฐ๋„ท์ด 1๋ฒˆ ์—ฐ๊ฒฐ๋˜๋ฉด ๋ธŒ๋ผ์šฐ์ € ์บ์‹œ๊ฐ€ ์ž‘๋™ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์™„์ „ํžˆ ์ธํ„ฐ๋„ท์ด ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ํŠน์ˆ˜ ์กฐ๊ฑด์—์„œ๋งŒ off-line MathJax๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. Pandoc ๊ธฐ์ดˆ Pandoc์€ ๋ฌธ์„œ ๋ณ€ํ™˜ ์œ ํ‹ธ๋Ÿฌํ‹ฐ ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. ์ž์„ธํ•œ ์‚ฌํ•ญ์€ ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•œ๋‹ค. Pandoc, Markdown, KaTeX, Visual Studio Code๋กœ ๋˜‘๋˜‘ํ•˜๊ฒŒ ๋…ผ๋ฌธ ์“ฐ๊ธฐ Anaconda๋ฅผ ์ธ์Šคํ†จํ•˜๋ฉด C:\ProgramData\Anaconda3\Scripts\pandoc.exe ๋“ฑ๊ณผ ๊ฐ™์ด ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฐ Scripts์— ์„ค์น˜๋œ๋‹ค. PATH๊ฐ€ ์ž๋™์œผ๋กœ ์žกํžˆ๊ธฐ ๋•Œ๋ฌธ์— ๋„์Šค ์ฐฝ์—์„œ ์ง์ ‘ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. https://pandoc.org/์—์„œ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ˜„๋ฒ„์ „์€ 3.1.6.1์ด๋‹ค(pandoc-3.1.6.1-windows-x86_64.zip). ๋‹ค์šด๋กœ๋“œ ํ›„ ์••์ถ• ํ•ด์ œํ•˜๋ฉด ๋œ๋‹ค. ์••์ถ•์„ ํ•ด์ œํ•˜๋ฉด pandoc.exe๋ผ๋Š” ๋‹จ๋… ์‹คํ–‰ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๋ฉฐ ์ด ํŒŒ์ผ๋งŒ ์žˆ์œผ๋ฉด ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค(๋‹ค๋ฅธ dll ๋“ฑ ํ•„์š”์กฐ๊ฑด์ด ์—†์Œ) ์ฐธ๊ณ ๋กœ ์œˆ๋„ ์ธ์Šคํ†จ๋Ÿฌ windows ์ธ์Šคํ†จ๋Ÿฌ(pandoc-3.1.6.1-windows-x86_64.msi๋ฅผ ์ œ๊ณตํ•œ๋‹ค. Path๋ฅผ ์„ค์ •๋œ๋‹ค๋Š” ์  ์ด์™ธ์—๋Š” ์••์ถ• ๋ณธ๊ณผ ์ฐจ์ด๊ฐ€ ์••์„œ๋‹ค. ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• > pandoc -s -o fft.docx fft.md ์œ„์—์„œ -s ์˜ต์…˜์€ standalone ์‹คํ–‰(์ด ์˜ต์…˜์ด ์—†์œผ๋ฉด ํƒ€์ดํ•‘์œผ๋กœ ๋ฌธ์„œ๋ฅผ ๋ฐ›์Œ)์„ ์˜๋ฏธํ•˜๋ฉฐ -o๋Š” ๋ณ€ํ™˜ํ•˜์—ฌ ์ €์žฅํ•  ๋ฌธ์„œ ์ด๋ฆ„, ๋งˆ์ง€๋ง‰์— ๋Œ€์ƒ ๋ฌธ์„œ๋ฅผ ๊ธฐ์ž…ํ•œ๋‹ค. ๋‹ค์Œ์€ Python ์Šคํฌ๋ฆฝํŠธ๋กœ ์‹คํ–‰ํ•œ ๊ฒƒ์ด๋‹ค. import os os.system('pandoc -s fft.md -o fft.docx') ๋งŒ์•ฝ pandoc์„ ๋ณ„๋„๋กœ ์„ค์น˜ํ•  ๊ฒฝ์šฐ Pandoc ์‚ฌ์ดํŠธ์—์„œ ์ตœ์‹  ๋ฒ„์ „์„ ์ธ์Šคํ†จํ•  ์ˆ˜ ์žˆ๋‹ค. ์œˆ๋„์šฐ์ฆˆ์šฉ์€ C:\Program Files\Pandoc์— ์„ค์น˜๋œ๋‹ค. Markdown ๋ฌธ์„œ ์ž‘์„ฑ ์‹œ \rm ๋Œ€์‹  ํ•ญ์ƒ \textrm์„ ์‚ฌ์šฉํ•˜๋ผ : \rm ์€ obsolute์ด๋‹ค. ๋”ฐ๋ผ์„œ Pandoc์—์„œ ํŒŒ์‹ฑ ํ•  ๋•Œ(ํŠนํžˆ, md -> docx๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ) ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. Markdown to Docx Docx ๋ฌธ์„œ๋ฅผ ํฌ๋งคํŒ…ํ•˜๋Š” ๊ฒƒ์€ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ž‘์„ฑํ•œ ํ›„ ํ•œ๋‹ค. pandoc -s -o design1.docx design1.md --reference-docx=my-reference.docx Markdown to HTML MathJax 2๋ฅผ ์˜จ๋ผ์ธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ./pandoc-3.1.6.1/pandoc.exe -s -o design.html design.md --metadata pagetitle="Design" --css=pandoc.css --mathjax=https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js?config=TeX-MML-AM_CHTML MathJax 2๋ฅผ Offline์—์„œ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒฝ์šฐ ./pandoc-3.1.6.1/pandoc.exe -s -o design.html design.md --metadata pagetitle="Design" --css=pandoc.css --mathjax=file:///D:/DevProg/HFC4.0/EXAMPLE/DigitalTwin/web/MathJax-2.7.9/MathJax.js?config=TeX-MML-AM_CHTML MathJax 3๋ฅผ ์˜จ๋ผ์ธ์œผ๋กœ ./pandoc-3.1.6.1/pandoc.exe -s -o design.html design.md --metadata pagetitle="Design" --css=pandoc.css --mathjax=https://cdn.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-mml-chtml.js MathJax 3๋ฅผ ์˜คํ”„๋ผ์ธ์œผ๋กœ ./pandoc-3.1.6.1/pandoc.exe -s -o design.html design.md --metadata pagetitle="Design" --css=pandoc.css --mathjax=file:///D:/DevProg/HFC4.0/EXAMPLE/DigitalTwin/web/MathJax-3.2.2/es5/tex-mml-chtml.js ์ˆ˜์‹์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด --mathjax ์˜ต์…˜์— MathJax์˜ CDN์„ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ offline์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•œ๋‹ค. --metadata๋Š” HTML5 ๋ฌธ์„œ์—์„œ ๊ผญ ํ•„์š”ํ•œ pagetitle ๋˜๋Š” title ์ œ๊ณต์„ ์œ„ํ•ด(์—†์œผ๋ฉด ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€ ๋ฐœ์ƒ) --css๋Š” css ์„œ์‹ ํŒŒ์ผ์„ ์ง€์ •ํ•œ๋‹ค. ์ฃผ์˜ : mathjax์™€ css์— ๋กœ์ปฌ ํŒŒ์ผ์„ ์ง€์ •ํ•  ๋•Œ ๊ณต๋ฐฑ ๋ฌธ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฒฝ๋กœ๋ช…์„ ""๋กœ ๋ฌถ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค ์ถ”์ฒœ css (pandoc.css) /* * I add this to html files generated with pandoc. */ html { font-size: 100%; overflow-y: scroll; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; } body { color: #444; font-family: Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif; font-size: 12px; line-height: 1.7; padding: 1em; margin: auto; max-width: 42em; background: #fefefe; } a { color: #0645ad; text-decoration: none; } a:visited { color: #0b0080; } a:hover { color: #06e; } a:active { color: #faa700; } a:focus { outline: thin dotted; } *::-moz-selection { background: rgba(255, 255, 0, 0.3); color: #000; } *::selection { background: rgba(255, 255, 0, 0.3); color: #000; } a::-moz-selection { background: rgba(255, 255, 0, 0.3); color: #0645ad; } a::selection { background: rgba(255, 255, 0, 0.3); color: #0645ad; } p { margin: 1em 0; } img { max-width: 100%; } h1, h2, h3, h4, h5, h6 { color: #111; line-height: 125%; margin-top: 2em; font-weight: normal; } h4, h5, h6 { font-weight: bold; } h1 { font-size: 2.5em; } h2 { font-size: 2em; } h3 { font-size: 1.5em; } h4 { font-size: 1.2em; } h5 { font-size: 1em; } h6 { font-size: 0.9em; } blockquote { color: #666666; margin: 0; padding-left: 3em; border-left: 0.5em #EEE solid; } hr { display: block; height: 2px; border: 0; border-top: 1px solid #aaa; border-bottom: 1px solid #eee; margin: 1em 0; padding: 0; } pre, code, kbd, samp { color: #000; font-family: Consolas, monospace, serif; font-size: 0.98em; background-color:#f8f8f8 } pre { white-space: pre; white-space: pre-wrap; word-wrap: break-word; } b, strong { font-weight: bold; } dfn { font-style: italic; } ins { background: #ff9; color: #000; text-decoration: none; } mark { background: #ff0; color: #000; font-style: italic; font-weight: bold; } sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; } sup { top: -0.5em; } sub { bottom: -0.25em; } ul, ol { margin: 1em 0; padding: 0 0 0 2em; } li p:last-child { margin-bottom: 0; } ul ul, ol ol { margin: .3em 0; } dl { margin-bottom: 1em; } dt { font-weight: bold; margin-bottom: .8em; } dd { margin: 0 0 .8em 2em; } dd:last-child { margin-bottom: 0; } img { border: 0; -ms-interpolation-mode: bicubic; vertical-align: middle; } figure { display: block; text-align: center; margin: 1em 0; } figure img { border: none; margin: 0 auto; } figcaption { font-size: 0.8em; font-style: italic; margin: 0 0 .8em; } table { margin-bottom: 2em; border-bottom: 1px solid #ddd; border-right: 1px solid #ddd; border-spacing: 0; border-collapse: collapse; } table th { padding: .2em 1em; background-color: #eee; border-top: 1px solid #ddd; border-left: 1px solid #ddd; } table td { padding: .2em 1em; border-top: 1px solid #ddd; border-left: 1px solid #ddd; vertical-align: top; } .author { font-size: 1.2em; text-align: center; } @media only screen and (min-width: 480px) { body { font-size: 14px; } } @media only screen and (min-width: 768px) { body { font-size: 16px; } } @media print { * { background: transparent ! important; color: black ! important; filter: none ! important; -ms-filter: none ! important; } body { font-size: 12pt; max-width: 100%; } a, a:visited { text-decoration: underline; } hr { height: 1px; border: 0; border-bottom: 1px solid black; } a[href]:after { content: " (" attr(href) ")"; } abbr[title]:after { content: " (" attr(title) ")"; } .ir a:after, a[href^="javascript:"]:after, a[href^="#"]:after { content: ""; } pre, blockquote { border: 1px solid #999; padding-right: 1em; page-break-inside: avoid; } tr, img { page-break-inside: avoid; } img { max-width: 100% !important; } @page :left { margin: 15mm 20mm 15mm 10mm; } @page :right { margin: 15mm 10mm 15mm 20mm; } p, h2, h3 { orphans: 3; widows: 3; } h2, h3 { page-break-after: avoid; } } ์ƒ˜ํ”Œ md #### ์„ค๊ณ„ ์กฐ๊ฑด * ๋‘๊ป˜ $h = [h] ~\textrm{mm}$ * ํญ $b = [b] ~\textrm{mm}$ * ์ฝ˜ํฌ๋ฆฌํŠธ * ์ฝ˜ํฌ๋ฆฌํŠธ ๊ธฐ์ค€ ์••์ถ•๊ฐ•๋„ $f_{ck} = [fck] ~\textrm{MPa}$ * ์ฝ˜ํฌ๋ฆฌํŠธ ํ‰๊ท  ์••์ถ•๊ฐ•๋„ [fcm] * ์ฝ˜ํฌ๋ฆฌํŠธ ๋‹จ์œ„์งˆ๋Ÿ‰ $m_c = 2,300 ~ \textrm{kg/m}^3$, * ์ฝ˜ํฌ๋ฆฌํŠธ ํƒ„์„ฑ๊ณ„์ˆ˜ $E_c=0.077m_c ^{1.5} \sqrt[3]{f_{cm}} = [Ec] ~ \textrm{MPa}$ * ์ฝ˜ํฌ๋ฆฌํŠธ ํ‰๊ท ์ธ ์žฅ๊ฐ•๋„ $f_{ctm} = 0.3f_{cm} ^{2/3} = [fctm]~\textrm{MPa}$ * ์ฝ˜ํฌ๋ฆฌํŠธ ๊ธฐ์ค€์ธ ์žฅ๊ฐ•๋„ $f_{ctk} = 0.70f_{ctm} = [fctk] ~\textrm{MPa}$ * ์ฝ˜ํฌ๋ฆฌํŠธ ๊ด€๋ จ ์ƒ์ˆ˜ [KDS 24 14 21 3.1.2.5] * ์ƒ์Šน๊ณก์„  ๋ถ€ ํ˜•์ƒ<NAME> $n = \textrm{min}(1.2+1.5( \frac{100-f_{ck}}{60})^4,2.0) = [n]$ * ์ตœ๋Œ€ ์‘๋ ฅ ๋„๋‹ฌ ์‹œ ๋ณ€ํ˜•๋ฅ  $\epsilon_{co} = \textrm{max}(0.002+\frac{f_{ck}-40}{100,000},0.002) = [eco]$ * ๊ทนํ•œ ๋ณ€ํ˜•๋ฅ  $\epsilon_{cu} = \textrm{min} (0.0033-\frac{f_{ck}-40}{100,000},0.0033) = [ecu]$ * p-r ๋‹จ๋ฉด ํ•ด์„ ๊ณ„์ˆ˜ * ์••์ถ• ํ•ฉ๋ ฅ ํฌ๊ธฐ ๊ณ„์ˆ˜ $\alpha = 1-\frac{1}{n+1} \frac{\epsilon_{co}}{\epsilon_{cu}} = [alpha]$ * ์ž‘์šฉ์  ์œ„์น˜ ๊ณ„์ˆ˜ $\beta = 1 - \frac{1}{\alpha} (0.5- \frac{1}{(1+n)(2+n)} (\frac{\epsilon_{co}}{\epsilon_{cu}})^2 ) = [beta]$ * ์ฒ ๊ทผ * ํ•ญ๋ณต๊ฐ•๋„ $f_y = [fy] ~\textrm{MPa}$ * ์ฒ ๊ทผ ํƒ„์„ฑ๊ณ„์ˆ˜ $E_s = [Es] ~\textrm{MPa}$ * ์ฃผ์ฒ ๊ทผ D[dbNomial]@[spacing], ํ”ผ๋ณต $c=[cover] ~\textrm{mm}$ ์„ ํƒ * ์ฃผ์ฒ ๊ทผ ์ง๊ฒฝ $d_b = [db] ~\textrm{mm}$ * ์ฃผ์ฒ ๊ทผ ๋ฉด์  $A_s = [Ask] \times [b]/[spacing] = [As] ~\textrm{mm}^2$ * ๊นŠ์ด $d = [h] - [cover] + [db]/2 = [d] ~\textrm{mm}$ * ์ฃผ์ฒ ๊ทผ๋น„ $\rho = A_s / bd = [rho]$ * ํœญ์ฒ ๊ทผ : D[dbTNomial]@[spacingT] ์„ ํƒ * ํšก์ฒ ๊ทผ ๋ฉด์  $A_{st} = [AskT] \times [b]/[spacingT] = [AsT] ~\textrm{mm}^2$ * ์ „๋‹จ ์ฒ ๊ทผ : D[dbSNomial]-[legShear]@[spacingS] ์„ ํƒ * ๋‹จ์œ„ํญ๋‹น ๋‹ค๋ฆฌ ์ˆ˜ : [legShear] * ์ „๋‹จ ์ฒ ๊ทผ ๋ฉด์  $A_{v} = [AskS] \times [legShear] = [Av] ~\textrm{mm}^2$ * ๋…ธ์ถœ ๋“ฑ๊ธ‰ : [exposureClass] * ํ•˜์ค‘ * ๊ทนํ•œํ•œ๊ณ„์ƒํƒœ $M_u^{ULS} = [MuULS] ~\textrm{kN-m}~~ V_u^{ULS} = [VuULS] ~\textrm{kN}$ * ๊ทน๋‹จ ํ•œ๊ณ„ ์ƒํƒœ $M_u^{ELS} = [MuELS] ~\textrm{kN-m}~~ V_u^{ELS} = [VuELS] ~\textrm{kN}$ * ์‚ฌ์šฉ ํ•œ๊ณ„ ์ƒํƒœ $Ms = [Ms] ~\textrm{kN-m}$ * ํ•œ๊ณ„๊ท ์—ดํญ $w_{lim} = [wlim] ~\textrm{mm}$ 6.7 MkDocs MkDocs๋Š” ๋งˆํฌ๋‹ค์šด(.md)๋กœ๋ถ€ํ„ฐ ์ •์  ์‚ฌ์ดํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํˆด์ด๋‹ค. ๋ณดํ†ต ํ”„๋กœ์ ํŠธ ์„ค๋ช…์„œ๋ฅผ ์›น์œผ๋กœ ๊ตฌ์ถ•ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. MkDocs : MkDocs ์„ค๋ช…์„œ MkDocs ํŠœํ† ๋ฆฌ์–ผ : ํ•œ๊ธ€ MkDocs ์„ค๋ช…์„œ MkDocs GitHub : MkDocs์˜ ์†Œ์Šค ๋ฐ ์œ„ํ‚ค MkDocs Wiki : MkDocs ์œ„ํ‚ค. ํ…Œ๋งˆ/ํ™•์žฅ/ํ”Œ๋Ÿฌ๊ทธ์ธ ๋“ฑ์„ ์ œ๊ณต Material for MkDocs : Material ํ…Œ๋งˆ ์ œ๊ณต PyMdown : Material ํ…Œ๋งˆ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ™•์žฅ(extension) ๊ด€๋ จ ํ†ตํ•ฉ ํŒจํ‚ค์ง€. ํ…Œ๋งˆ์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ReadTheDocs-Dropdown : ReadTheDocs ํ…Œ๋งˆ๋ฅผ ์ˆ˜์ •ํ•œ ๋“œ๋กญ ๋‹ค์šด ํ˜•ํƒœ์˜ ํ…Œ๋งˆ https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference ์„ค์น˜ ์„ค์น˜ ( PYTHON ์‹คํ–‰ ๊ฒฝ๋กœ๊ฐ€ ํ™˜๊ฒฝ๋ณ€์ˆ˜ PATH๋กœ ์„ค์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰) > pip install mkdocs # mkdocs --version๋กœ ํ™•์ธํ•  ๋•Œ ํ˜„ ๋ฒ„์ „์€ 1.1.2 > pip install pymdown-extensions > pip install mkdocs-material > pip install mkdocs-rtd-dropdown mkdocs๋Š” MkDocs๋ฅผ, pymdown-extensions๋Š” PyMdown์„, mkdocs-material์€ material ํ…Œ๋งˆ, mkdocs-rtd-dropdown๋Š” rtd-dropdown ํ…Œ๋งˆ๋ฅผ ์„ค์น˜ํ•œ๋‹ค. ์ฃผ์š” ๋ช…๋ น > mkdocs new myProject # myProject๋ผ๋Š” ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ > cd myProject > mkdocs serve # ๋กœ์ปฌ ์„œ๋ฒ„ ๋„์šฐ๊ธฐ -> ๋ธŒ๋ผ์šฐ์ €์—์„œ `http://localhost:8000/` ๋˜๋Š”`http://127.0.0.1:8000/`๋กœ ํ™•์ธ ๊ฐ€๋Šฅ > mkdocs build # html ๋ฌธ์„œ ๋งŒ๋“ค๊ธฐ mkdocs new <projectName>์œผ๋กœ ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ ๊ทธ ํ•˜๋ถ€์— ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. <projectName> + docs - index.md - mkdocs.yml ๋งˆํฌ๋‹ค์šด ํŒŒ์ผ์€ docs ํด๋” ์•„๋ž˜(์ด ์•ˆ์—์„œ ํด๋” ๊ตฌ์กฐ์—ฌ๋„ ๋œ๋‹ค) ๋„ฃ๊ณ  mkdocs.yml์„ ํ†ตํ•ด ์„ค์ •์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ docs์˜ ๊ตฌ์กฐ์™€ mkdocs.yml ์˜ˆ์ด๋‹ค. docs - index.md - about.md + chapter1 - 1-1.md - 1-2.md site_name: My Docs nav: - Home: index.md - About: about.md - chapter1: - 1-1 : chaper1/1-1.md - 1-2 : chaper1/1-2.md theme : material use_directory_urls: false pages ์•„๋ž˜์— ๊ณ„์ธต๊ตฌ์กฐ๋กœ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค. pages๊ฐ€ ์—†์–ด๋„ docs ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๊ณ„์ธต๊ตฌ์กฐ์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋‚˜, ๋ฌธ์„œ์˜ #(์—†์œผ๋ฉด ํŒŒ์ผ๋ช…)์ด ์ œ๋ชฉ์œผ๋กœ ์„ค์ •๋œ๋‹ค. ๋”ฐ๋ผ์„œ pages๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ„๋„์˜ ๋ชฉ์ฐจ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํŒŒ์ผ ๋ช…์— ๊ณต๋ฐฑ ๋“ฑ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌถ์–ด ์ค€๋‹ค - 1-1 : 'chaper1/1-1.md' theme์€ ํ…Œ๋งˆ๋ฅผ ์ง€์ •ํ•˜๋Š”๋ฐ ์ง€์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ mkdocs ํ…Œ๋งˆ๊ฐ€ ์ง€์ •๋œ๋‹ค. mkdocs, readthedocs ํ…Œ๋งˆ๋ฅผ ์ œ์™ธํ•œ ํ…Œ๋งˆ๋Š” MkDocs Wiki์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์˜ ์„ธ ํ…Œ๋งˆ๊ฐ€ ์“ธ๋งŒํ•˜๋‹ค. readthedocs : MkDocs์—์„œ ์ถ”๊ฐ€๋กœ ์ œ๊ณตํ•˜๋Š” readthedocs ์‚ฌ์ดํŠธ๋ฅผ ํ‰๋‚ด ๋‚ธ ํ…Œ๋งˆ. ์ธ์Šคํ†จ ๋ถˆํ•„์š”. ๋ฉ€ํ‹ฐ ๋ ˆ๋ฒจ ๋‚ด๋น„๊ฒŒ์ด์…˜ ์‹œ ์ธ๋ดํ…Œ์ด์…˜์ด ๋งž์ง€ ์•Š๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค. rtd-dropdown : readthedocs์˜ ๋‚ด๋น„๊ฒŒ์ด์…˜์„ ๋“œ๋กญ ๋‹ค์šด ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝํ•œ ํ…Œ๋งˆ. material : material ํ…Œ๋งˆ. ์ตœ๊ทผ์— ๋งŽ์ด ์„ธ๋ จ๋๋‹ค. ์ถ”์ฒœ use_directory_urls: false์€ html ๋ฌธ์„œ๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋„๋ก ์ง€์ •ํ•œ๋‹ค. ์ˆ˜์‹ ์„ค์ • PyMdown์„ ์„ค์น˜ํ•œ ํ›„ mkdocs.yml์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. markdown_extensions: - pymdownx.arithmatex - pymdownx.betterem: smart_enable: all - pymdownx.caret - pymdownx.critic - pymdownx.details - pymdownx.emoji: emoji_generator:!! python/name:pymdownx.emoji.to_svg - pymdownx.inlinehilite - pymdownx.magiclink - pymdownx.mark - pymdownx.smartsymbols - pymdownx.superfences - pymdownx.tasklist: custom_checkbox: true - pymdownx.tilde extra_javascript: - 'https://cdnjs.cloudflare.com/ajax/libs/MathJax/2.7.5/MathJax.js? config=TeX-MML-AM_CHTML' ์ด์ œ ๋งˆํฌ๋‹ค์šด ๋ฌธ์„œ์—์„œ $...$์ด๋‚˜ $$...$$์œผ๋กœ Latex ์ˆ˜์‹์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. MathJax์˜ CDN ์„œ๋ฒ„๋Š” ๋ฒ„์ „์— ๋”ฐ๋ผ ์กฐ๊ธˆ์”ฉ ๋ฐ”๋€๋‹ค(์œ„์˜ 2.7.5๋Š” 2020.1.14๊ธฐ์ค€์ด๋ฉฐ, mathJax ํ™ˆํŽ˜์ด์ง€์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค) Markdown ์ˆ˜์‹์—์„œ \rm ๋Œ€์‹  \textrm์„ ์“ฐ๋„๋ก ํ•œ๋‹ค(Markdown ๋ฌธ์„œ๋ฅผ Pandoc์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ๊ฒฝ์šฐ ๋”ฐ๋ผ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜ ์œ ๋ฐœ ๋ฐฉ์ง€) 7. HDF5 (ํŒŒ์ด์ฌ) ์†Œ๊ฐœ hdf5(Hierarchical Data Format version 5)๋Š” ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํŒŒ์ผ ํฌ๋งท์ด๋‹ค. ํŠน์ง•์€ ๋‹ค์Œ์œผ๋กœ ์š”์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค. Easy sharing Cross platform Fast IO Big Data Heterogeneous data ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ์ผ์ข…์˜ ๊ณ ์„ฑ๋Šฅ DB๋ผ๊ณ  ์ดํ•ดํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฃผ๋กœ ๊ณผํ•™๊ธฐ์ˆ  ๋ฐ์ดํ„ฐ์˜ ํฌ๋งท์œผ๋กœ ์ ๋‹นํ•˜๋‹ค. BSD ์Šคํƒ€์ผ์˜ ๋ผ์ด์„ ์Šค๋ฅผ ์ฑ„ํƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ •, ๋ฐฐํฌ, ์ƒ์šฉ ํ”„๋กœ๊ทธ๋žจ ์‚ฌ์šฉ ๋“ฑ์— ์ž์œ ๋กญ๋‹ค. ํŒŒ์ด์ฌ ๋ฐ”์ธ๋”ฉ h5py h5py์™€ PyTables ๋‘ ๊ฐ€์ง€ ํŒŒ์ด์ฌ ๋ฐ”์ธ๋”ฉ์ด ์žˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” h5py๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. Anaconda์˜ ๊ฒฝ์šฐ h5py๊ฐ€ ๋ฏธ๋ฆฌ ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋ณ„๋„์˜ ์ถ”๊ฐ€ ์ธ์Šคํ†จ์ด ํ•„์š” ์—†๋‹ค. ์‚ฌ์šฉํ•  ๋ฒ„์ „์€ 2.10.0์ด๋ฏ€๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ๋‹ค. ์„ค์น˜ ์‹œ(ํ•„์š”ํ•œ ๊ฒฝ์šฐ) > pip install h5py ์„ค์น˜ ์‹œ(์—…๊ทธ๋ ˆ์ด๋“œ์˜ ๊ฒฝ์šฐ) > pip install h5py --upgrade ๋ฒ„์ „ ํ™•์ธ >>> import h5py >>> h5py.__version__ '2.10.0' ๊ด€๋ จ ์‚ฌ์ดํŠธ https://www.hdfgroup.org/ : HDF5 https://www.h5py.org/ : HDF5์˜ ํŒŒ์ด์ฌ ๋ฐ”์ธ๋”ฉ์ธ h5py ํŒจํ‚ค์ง€ https://www.pytables.org/ : HDF5์˜ ํŒŒ์ด์ฌ ๋ฐ”์ธ๋”ฉ์ธ PyTables ํŒจํ‚ค์ง€ https://www.nexusformat.org/ : h5py๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ํฌ๋งท ์˜ˆ์‹œ 7.1 HDF5์˜ ํŠน์ง• HDF5์˜ ํŠน์ง• HDF5๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฐœ๋…์€ ๊ทธ๋ฃน(Group), ๋ฐ์ดํ„ฐ ์…‹(Dataset), ์†์„ฑ(attribute)์ด๋‹ค. ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ์™€ ๋น„์Šทํ•œ๋ฐ, ๊ทธ๋ฃน=๋””๋ ‰ํ„ฐ๋ฆฌ, ๋ฐ์ดํ„ฐ ์…‹=ํŒŒ์ผ๋กœ ์ดํ•ดํ•˜๋ฉด ์‰ฝ๋‹ค. ์†์„ฑ์€ ์ผ์ข…์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋กœ ๊ทธ๋ฃน์ด๋‚˜ ๋ฐ์ดํ„ฐ ์…‹์„ ๋ถ€์—ฐ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. HDF5 ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉด ๋จผ์ € /๋ผ๋Š” ๋ฃจํŠธ ๊ทธ๋ฃน์ด ์ƒ์„ฑ๋˜๊ณ  ๊ทธ ํ•˜์œ„์— ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ ๋‹ค๋ฅธ ๊ทธ๋ฃน์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฃน ํ•˜์œ„์— ๋‹ค๋ฅธ ๊ทธ๋ฃน์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๊ณ , ๋ฐ์ดํ„ฐ ์…‹์ด ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ฆ‰ ์™„์ „ํžˆ ์šด์˜ ์ฒด๊ณ„์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ-ํŒŒ์ผ ๊ตฌ์กฐ์™€ ์ผ์น˜ํ•œ๋‹ค. ๋˜ ๋‹ค๋ฅธ ํŠน์ง•์€ ์†์„ฑ์ธ๋ฐ ์†์„ฑ์€ ๋ฐ์ดํ„ฐ ์…‹์ด๋‚˜ ๊ทธ๋ฃน์„ ์„ค๋ช…ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ์ด๋ฅผ ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•˜๊ฒŒ ๋œ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด HDF5๋Š” Hierarchical Data Format์ด๋ฉฐ self-describing์ด ๋˜๋Š” ๊ณ ์„ฑ๋Šฅ ๋ฐ์ดํ„ฐ ํฌ๋งท ๋˜๋Š” DB ์ •๋„๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์šด์˜ ์ฒด๊ณ„์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ import numpy as np import matplotlib.pyplot as plt import h5py ######################### # write # station 15 temperature15 = np.random.random(1024) wind15 = np.random.random(2048) # station 20 temperature20 = np.random.random(1024) wind20 = np.random.random(2048) f = h5py.File('weather.hdf5','w') # 'a' f.attrs['descr'] = 'Temperature and wind data at HK mall' f['/15/temperature'] = temperature15 f['/15/temperature'].attrs['dt'] = 10.0 f['/15/temperature'].attrs['start_time'] = 1375204299 f['/15/wind'] = wind15 f['/15/wind'].attrs['dt'] = 5.0 f['/20/temperature'] = temperature20 f['/20/temperature'].attrs['dt'] = 10.0 f['/20/temperature'].attrs['start_time'] = 1375204299 f['/20/wind'] = wind20 f['/20/wind'].attrs['dt'] = 5.0 f.close() ######################### # read f = h5py.File('weather.hdf5','r') temp = f['15/temperature'][:] dt = f['15/temperature'].attrs['dt'] f.close() npoint = len(temp) t = np.arange(0.,npoint*dt, dt) plt.plot(t, temp) plt.tight_layout() plt.show() ์ƒ์„ฑ๋œ HDF5 ํŒŒ์ผ์€ HDFView์—์„œ ํŽธ๋ฆฌํ•˜๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 7.2 ํŒŒ์ผ๊ณผ ๊ทธ๋ฃน ํŒŒ์ผ hdf ํŒŒ์ผ h5py.File(name, mode=None, ...) ํ˜•ํƒœ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํŒŒ์ผ์„ ์—ด๊ฒŒ ๋œ๋‹ค. f = h5py.File('test.h5','w') ... f.close() ๋ชจ๋“œ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ 5๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. r Readonly, file must exist r+ Read/write, file must exist w Create file, truncate if exists w- or x Create file, fail if exists a Read/write if exists, create otherwise (default) ๊ทธ๋ฃน(group) ๊ทธ๋ฃน์€ ์ปจํ…Œ์ด๋„ˆ ์—ญํ• ์„ ํ•œ๋‹ค. HDF5 ํŒŒ์ผ์—๋Š” ํ•ญ์ƒ ๋ฃจํŠธ ๊ทธ๋ฃน / ์ด ์กด์žฌํ•˜๋ฉฐ, ํŒŒ์ผ ๊ฐ์ฒด๋Š” ๋ฃจํŠธ ๊ทธ๋ฃน์œผ๋กœ ์ทจ๊ธ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฃน์— ํ•˜์œ„ ๊ทธ๋ฃน์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ group.create_group(name, ...)๋กœ ์ƒ์„ฑํ•œ๋‹ค. ์ด๋•Œ name์€ /๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์ ˆ๋Œ€ ๊ฒฝ๋กœ๊ฐ€, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉฐ ์ƒ๋Œ€ ๊ฒฝ๋กœ๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. import numpy as np import h5py fdata = np.arange(10.) f = h5py.File('group.h5','w') f.create_group('1') f.create_group('/2/1') f['/2'].create_group('2') f['/2'].create_group('/3/4/1') # f.create_group('/3/4/1') f['/2'].create_group('3/4/1') f['/4/data'] = fdata # f.create_dataset('/4/data',data=fdata) f.close() ์œ„์—์„œ ์ˆœ์ฐจ์ ์œผ๋กœ ๊ทธ๋ฃน์„ ๋งŒ๋“ค์ง€ ์•Š๋”๋ผ๋„ ์€์—ฐ์ค‘์— ๊ทธ๋ฃน์ด ์ƒ์„ฑ๋จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ group.create_dataset(name, data=data)๋กœ ๋ฐ์ดํ„ฐ ์…‹์„ ์ƒ์„ฑํ•  ๋•Œ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค. ์œ„์—์„œ ์ƒ์„ฑํ•œ ํŒŒ์ผ์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด h5ls๋‚˜ h5dump ์œ ํ‹ธ๋Ÿฌํ‹ฐ๋กœ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋˜๋Š” HDFView๋ฅผ ํ†ตํ•ด ์‹œ๊ฐ์ ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฃน์˜ ์ค‘์š” ๋ฉค๋ฒ„๋Š” [key], name, attrs ๋“ฑ์ด๋‹ค. >>> f = h5py.File('group.h5','r') >>> f['/1'] <HDF5 group "/1" (0 members)> >>> f['/1'].name Out[94]: '/1' ์ˆœํšŒ๊ฐ€ ํ•„์š”ํ•˜๋ฉด keys(), items(), values() ์„ ์‚ฌ์šฉํ•œ๋‹ค. for k in f.keys(): print(f[k].name) for k, v in f.items(): print(k, v.name) for v in f.values(): print(v.name) ๊ฒฐ๊ด๊ฐ’ keys() /1 /2 /3 /4 items() 1 /1 2 /2 3 /3 4 /4 values() /1 /2 /3 /4 7.3 ๋ฐ์ดํ„ฐ ์…‹ - ๊ธฐ์ดˆ ๊ธฐ๋ณธ ์‚ฌํ•ญ ๋ฐ์ดํ„ฐ ์…‹์€ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฐ–๋Š” ์ˆ˜์น˜๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด(1์ฐจ๋˜, 2์ฐจ๋˜, ๊ณ ์ฐจ๋˜)์„ ์ €์žฅํ•œ๋‹ค. numpy์˜ ndarray์— ์ด๋ฆ„์„ ๋ถ™์—ฌ ์ €์žฅํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ์„ฑ์€ ๊ทธ๋ฃน์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜์ธ create_dataset(...)๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. dataset = group.create_dataset(name, shape=None, dtype=None, data=None,...) ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ ๋ฐ์ดํ„ฐ ์…‹์˜ ์ค‘์š” ์†์„ฑ์€ name, shape, dtype์ด๋‹ค. ndarray๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•  ๋•Œ๋Š” shape๊ณผ dtype์„ ์ง€์ •ํ•  ํ•„์š” ์—†๋‹ค. import numpy as np import h5py fdata = np.arange(5.) f = h5py.File('dataset.h5','w') data1 = f.create_dataset('data1',data=fdata) # same to f['data'] = fdata print(data1.name) # /data1 print(data1.shape) # (5, ) print(data1.dtype) #[ 0. 500. 2. 3. 4.] ... ๋ฐ์ดํ„ฐ ์…‹์„ ์•ก์„ธ์Šคํ•˜๋Š” ๊ฒƒ์€ ndarray์™€ ๋™์ผํ•˜๊ฒŒ []๋ฅผ ์ด์šฉํ•œ๋‹ค(์˜ˆ์ „์—์„œ๋Š” dataset.value๋กœ ์ง์ ‘ ๋‚ด๋ถ€์˜ ndarray๋ฅผ ์ ‘๊ทผํ•˜์˜€์œผ๋‚˜ depreciated ๋จ)๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฐ”๋กœ ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ๋‹ค. data1[1] = 500 data1[2:5] = -1.4 print(data1[:]) ์ €์žฅํ•  ๊ณต๊ฐ„๋งŒ ๋ฏธ๋ฆฌ ์ •ํ•ด ๋†“๊ณ  ๋‚˜์ค‘์— ๊ฐ’์„ ๋Œ€์ž…ํ•˜๋Š” ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. flush()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๋กœ ์“ฐ๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค(ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ํŒŒ์ผ ๋‹ซํž ๋•Œ ๋ถˆ๋ฆผ) d = f.create_dataset('data2',shape=(5, ),dtype='float') d[:] = fdata f.flush() ์œ„์—์„œ dtype์„ ์ง์ ‘ ์ง€์ •ํ•  ๋•Œ๋Š” numpy์˜ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๊ฐ€๋Šฅํ•˜๋‹ค( dtype = numpy.float32, dtype = np.dtype('int') ๋“ฑ๋“ฑ). NumPy ๋ฐฐ์—ด์„ ๋ฐ์ดํ„ฐ๋กœ ์ง€์ •ํ•˜๋ฉด์„œ dtype์„ ์ง€์ •ํ•˜๋ฉด ํ˜• ๋ณ€ํ™˜์ด ์ด๋ฃจ์–ด์ง€๋ฉฐ ๋ณต์‚ฌ๋˜๊ฒŒ ๋œ๋‹ค. f.create_data('data',data=fdata, dtype=np.dtype('float32')) ์œ„์—์„œ fdata์˜ dtype์€ float64 (64๋น„ํŠธ ์‹ค์ˆ˜)์ธ๋ฐ, ๋ฐ์ดํ„ฐ ์…‹์˜ dtype์„ float32๋กœ ์ง€์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— 32 ๋น„ํŠธ ํฌ๊ธฐ์˜ ๋ฐฐ์—ด๋กœ ์ €์žฅ๋œ๋‹ค. ํฌ๊ธฐ๊ฐ€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ ์…‹ ๋ฐ์ดํ„ฐ ์…‹์— ์ €์žฅ๋˜๋Š” ๋ฐฐ์—ด์€ dataset.resize(tuple)๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ƒ์„ฑ ์‹œ ๋””ํดํŠธ๋Š” ํฌ๊ธฐ๊ฐ€ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. data = f.create_dataset('dset',(2,2),dtype='int') print(data.shape) # (2,2) print(data.maxshape) # (2,2) d.resize((2,10)) # error ... ํฌ๊ธฐ๊ฐ€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด maxshape ์ธ์ž๋ฅผ ๋ฐ์ดํ„ฐ ์…‹์„ ์ƒ์„ฑํ•  ๋•Œ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. import numpy as np import h5py data = np.array([[1,2],[3,4]]) f = h5py.File('datasetresize.h5','w') d = f.create_dataset('dset',(2,2),dtype='int',maxshape=(2, None)) print(d.shape) # (2,2) print(d.maxshape) # (2, None) d[:] = data print(d[:]) d.resize((2,10)) print(d[:]) f.close() ์œ„ ์ฝ”๋“œ๋Š” ์ฒ˜์Œ ๋ฐ์ดํ„ฐ ์…‹์„ ์ƒ์„ฑํ•  ๋•Œ (2,2)๋กœ ์ƒ์„ฑํ•˜๊ณ , maxshape ์ธ์ž์—์„œ None ์ง€์ •๋œ ์ถ•์œผ๋กœ ํฌ๊ธฐ๊ฐ€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ์ฆ‰, (2, any) ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. 7.4 ๋ฐ์ดํ„ฐ ์…‹ - ๊ณ ๊ธ‰ ๊ธฐ๋ณธ ์‚ฌํ•ญ dataset์„ ์ƒ์„ฑํ•  ๋•Œ dtype ์ธ์ž๊ฐ€ ์ฃผ์–ด์ง€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ฃผ์–ด์ง„ numpy array์˜ dtype์œผ๋กœ ์„ค์ •๋˜๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ ๋ณด๋‹ค ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋ฉด dtype์„ ์ธ์ž๋กœ ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ •์ˆ˜ : np.dtype("i") , np.dtype("i8"), np.dtype("int"), np.dtype("<i8"), ... ์‹ค์ˆ˜ : np.dtype("f"), np.dtype("float64"), ... ๊ณ ์ •๊ธธ์ด ๋ฌธ์ž์—ด : np.dtype('S10') ๊ฐ€๋ณ€ ๊ธธ์ด ๋ฌธ์ž์—ด : h5py.string_dtype(encoding='utf-8') ๊ฐ€๋ณ€๊ธธ์ด ๋ฐ์ดํ„ฐ : h5py.vlen_dtype(np.dtype('int32')), ... ์ปดํŒŒ์šด๋“œ(C์˜ ๊ตฌ์กฐ์ฒด, np์˜ structured array) : dtype = [('name','S10'),('age','<i4'),('height','<f4')] ๊ฐ€๋ณ€๊ธธ์ด ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•œ ์ปดํŒŒ์šด๋“œ : vdtype = h5py.vlen_dtype(np.dtype("float64")); dtype=[("no", np.dtype("int")), ("nodes",vdtype)] ์ด์™ธ์—๋„ ์—ด๊ฑฐํ˜• ๋“ฑ ๋‹ค์–‘ํ•œ ํƒ€์ž…์„ ์ง€์›ํ•œ๋‹ค. ๊ฐ€๋ณ€ ๊ธธ์ด ๋ฌธ์ž์—ด NumPy์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ชจ๋‘ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— NumPy ๋ฐฐ์—ด์„ dataset์œผ๋กœ ์ €์žฅํ•˜๋Š” ๋ฐ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. ํ•˜์ง€๋งŒ NumPy์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์•„๋‹Œ ๊ฒฝ์šฐ ํŠน์ˆ˜ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๋Œ€ํ‘œ์  ์˜ˆ๊ฐ€ ๋ฌธ์ž์—ด์ด๋‹ค. ๋ฌธ์ž์—ด์€ string_dtype(encoding=encoding)์œผ๋กœ ํŠน์ˆ˜ ํƒ€์ž…์„ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•œ๋‹ค(์ฃผ์˜ ์˜ˆ์ „ ๋ฒ„์ „์€ special_dtype(vlen=str)์„ ์‚ฌ์šฉํ•˜์˜€์Œ) import numpy as np import h5py data = np.array([1,2,3]) tags = ['James','Cook','Linda'] f = h5py.File('datasetstr.h5','w') f['data'] = data # f.create_dataset('data',data=data) sdtype = h5py.string_dtype(encoding='utf-8') # (3, ) array of string dset = f.create_dataset('strdata',(3, ),dtype=sdtype) dset[0] = 'James' dset[1] = 'Cook' dset[2] = 'Michael' # only one string : (1, ) string f.create_dataset('strdata2',data='This is special type',dtype=sdtype) f.close() f = h5py.File('datasetstr.h5','r') A = f['strdata'][0] B = f['strdata2'][()] # zero-dimension case 'CAUTION' f.close() ๊ฐ€๋ณ€๊ธธ์ด ๋ฐ์ดํ„ฐ(vlen data) NumPy ๋ฐฐ์—ด์€ ๊ฐ ์š”์†Œ์˜ ํฌ๊ธฐ๊ฐ€ ์ผ์ •ํ•œ ๊ฒƒ์ด ํŠน์ง•์ด๋‹ค. h5py๋Š” ๋ฐฐ์—ด ์š”์†Œ์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ€๋ณ€์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์›ํ•œ๋‹ค(h5py ๋ฒ„์ „ 2.10.0๋ถ€ํ„ฐ ์ง€์›) import h5py import numpy as np f = h5py.File('vlen.h5','w') dtype = h5py.vlen_dtype(np.dtype('int32')) dataset = f.create_dataset('vlen_int',(3, ),dtype=dtype) dataset[0] = [1,2,3] dataset[1] = [1,2,3,4,5] f.close() ๊ฐ€๋ณ€๊ธธ์ด ๋ฐ์ดํ„ฐ๋ฅผ ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•  ๊ฒฝ์šฐ ์—ด์˜ ์ˆ˜๊ฐ€ 1์ธ dataset๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ์ •์ˆ˜, ์‹ค์ˆ˜ ๋“ฑ์ด ํ˜ผํ•ฉ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ๋’ค์— ์„ค๋ช…ํ•  vlen ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•œ compound๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. ์ปดํŒŒ์šด๋“œ C์˜ ๊ตฌ์กฐ์ฒด๋ฅผ ์„ฑ๋ถ„์œผ๋กœ ๊ฐ–๋Š” ๋ฐฐ์—ด์€ NumPy์—์„œ structured array์ด๋‹ค. HDF5 ์—ญ์‹œ ์ด๋ฅผ ์ง€์›ํ•˜๋‹ค. import numpy as np import h5py name = ['Alice', 'Bob', 'Doug'] age = [25, 45, 37] height = [173.,181.,165.] dtype = [('name','S10'),('age','<i4'),('height','<f4')] data = np.zeros(3, dtype=dtype) data['name'] = name data['age'] = age data['height'] = height f = h5py.File('compound.h5','w') f['data'] = data # d = f.create_dataset('data',dtype=dtype) # d[:] = data f.close() ๊ฐ€๋ณ€๊ธธ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๋Š” ์ปดํŒŒ์šด๋“œ ์ปดํŒŒ์šด๋“œ๋ฅผ ๊ตฌ์„ฑํ•  ๋•Œ ๊ฐ€๋ณ€๊ธธ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑ์š”์†Œ๋กœ ์ง€์ •ํ•˜๋ฉด ๋‹ค์–‘ํ•œ<NAME>์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. import h5py import numpy as np # np.dtype("<i8") == np.dtype('int64') vlen_dtype = h5py.vlen_dtype(np.dtype("float64")) f = h5py.File('compound-vlen.h5', "w") dset = f.create_dataset( "elem_nodes", shape=(5, ), maxshape=(None,), chunks=True, compression="gzip", dtype=[("no", np.dtype("int")), ("nodes", vlen_dtype)], # dtype=[("no", np.dtype("<i8")), ("nodes", np.dtype("<i8"))], # dtype=dt ) dset[0] = (1, np.array([1, 2], dtype=np.dtype("float64")) ) dset[1] = ( 2, np.array([2, 3, 4])) dset[2] = ( 3, np.array([2, 3, 4], dtype="float64")) f.close() ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ถ”๊ฐ€์‚ฌํ•ญ h5dump๋กœ ์ถœ๋ ฅ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด H5T_STD_I32LE๋ผ๋Š” ํ‘œํ˜„์ด ์žˆ๋‹ค. ์ด๋Š” HDF5์˜ C์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํƒ€์ž…๋ช…์ธ๋ฐ ์ •์ˆ˜ํ˜•, 32๋น„ํŠธ, ๋ฆฌํ‹€์—”๋””์–ธ์„ ์˜๋ฏธํ•œ๋‹ค. ๋ฏธ๋ฆฌ ์ •์˜๋˜์–ด ์žˆ๋Š” ํƒ€์ž…์€ HDF5 Predefined Datatypes์„ ์ฐธ์กฐํ•˜๋ฉด ๋œ๋‹ค. ์ผ๋ถ€๋งŒ ์†Œ๊ฐœํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. IEEE floating point datatypes H5T_IEEE_F32BE H5T_IEEE_F32LE H5T_IEEE_F64BE H5T_IEEE_F64LE ์ •์ˆ˜ H5T_STD_I8BE H5T_STD_I8LE ... H5T_STD_I64BE H5T_STD_I64LE h5py์˜ ์ € ์ˆ˜์ค€ API์—์„œ๋Š” C์—์„œ ์ •์˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ f.create_dataset("fdata",data=fdata) # ๋””ํดํŠธ์ธ 64๋น„ํŠธ LE ์‚ฌ์šฉ f.create_dataset("fdata",data=fdata, dtype=h5py.h5t.IEEE_F64LE) # 64๋น„ํŠธ LE ์‚ฌ์šฉ f.create_dataset("fdata",data=fdata, dtype=h5py.h5t.IEEE_F64BE) # 64๋น„ํŠธ BE ์‚ฌ์šฉ f.create_dataset("idata",data=idata) # ๋””ํดํŠธ์ธ 32๋น„ํŠธ LE ์‚ฌ์šฉ f.create_dataset("idata",data=idata, dtype=h5py.h5t.STD_I32LE) # 32๋น„ํŠธ LE f.create_dataset("idata",data=idata, dtype=h5py.h5t.STD_I32BE) # 32๋น„ํŠธ BE 7.5 ์†์„ฑ ๊ธฐ๋ณธ HDF5์˜ ๊ฐ€์žฅ ํฐ ์žฅ์  ์ค‘ ํ•˜๋‚˜๊ฐ€ ๊ทธ๋ฃน์ด๋‚˜ ๋ฐ์ดํ„ฐ ์…‹์„ ๋ถ€์—ฐ ์„ค๋ช…ํ•˜๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ์†์„ฑ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š ์ ์ด๋‹ค. ์†์„ฑ(attribute)๋Š” ๊ทธ๋ฃน์ด๋‚˜ ๋ฐ์ดํ„ฐ ์…‹ ๊ฐ์ฒด์˜ attrs ๋ฉค๋ฒ„๋กœ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ dictionary ํƒ€์ž…์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. import numpy as np import h5py sensor = np.random.random(100) f = h5py.File('attr.h5','w') f['sensor'] = sensor f['sensor'].attrs['dt'] = 5. f['sensor'].attrs['time'] = '2019.12.3' f.close() #--------------------------- f = h5py.File('attr.h5','r') sensor_data = f['sensor'] sensor_attrs = sensor_data.attrs dt = sensor_attrs['dt'] print('dt = ',dt) print('---attrs.keys()') for key in sensor_attrs.keys(): print(key, ':', sensor_attrs[key]) print('---attrs.values()') for value in sensor_attrs.values(): print(value) print('---attrs.items()') for key, value in sensor_attrs.items(): print(key,':', value) ์ถœ๋ ฅ dt = 5.0 ---attrs.keys() dt : 5.0 time : 2019.12.3 ---attrs.values() 5.0 2019.12.3 ---attrs.items() dt : 5.0 time : 2019.12.3 7.6 ๋งํฌ HDF5๋Š” hard link์™€ soft link(=symbolic link)๋ฅผ ์ง€์›ํ•œ๋‹ค. ์ด๋Š” UNIX์—์„œ ํŒŒ์ผ์˜ ์‡ผํŠธ์ปคํŠธ๋กœ ์ œ์•ˆ๋˜์—ˆ์œผ๋ฉฐ, symbolic link๋Š” Windows์—์„œ๋„ ์ง€์›ํ•œ๋‹ค. UNIX์—์„œ์˜ ๊ตฌ๋ถ„์€ ๋‹ค์Œ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ a.txt๋ผ๋Š” ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ์ด ํŒŒ์ผ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” hard link๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด, hard link๋ฅผ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์› ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๋”๋„ ์‹ค์ œ ํŒŒ์ผ์ด ์‚ญ์ œ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ reference counter๋ฅผ ์œ ์ง€ํ•˜๋ฉฐ ์ด๊ฐ€ 0์ด ๋  ๋•Œ ์‹ค์ œ๋กœ ์‚ญ์ œํ•˜๊ฒŒ ๋œ๋‹ค(ํŒŒ์ผ์„ ์ƒ์„ฑ๋˜์ง€ ๋งˆ์ž ๊ทธ ์ž์ฒด๊ฐ€ hard link, ์ฆ‰ reference counter ๊ฐ€ 1์ด๋‹ค). ์ฆ‰, smart pointer์™€ ์œ ์‚ฌํ•œ ๊ฐœ๋…์ด๋‹ค. Soft link๋Š” ๋‹จ์ˆœํžˆ ๊ฒฝ๋กœ๋งŒ ์ €์žฅํ•œ๋‹ค. a.txt๋ฅผ ์‚ญ์ œํ•˜๋ฉด ์‹ค์ œ๋กœ ๊ทธ ํŒŒ์ผ์ด ์‚ญ์ œ๋˜๋ฉฐ, ์—ฐ๊ฒฐ๋œ soft link๋Š” ์˜๋ฏธ ์—†๊ฒŒ ๋œ๋‹ค(dangling). HDF5๋Š” ๊ทธ๋ฃน๊ณผ ๋ฐ์ดํ„ฐ ์…‹์„ ๋Œ€์ƒ์œผ๋กœ hard link, soft link๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ๋‹จ์ˆœํžˆ ๊ทธ๋ฃน์— ๋Œ€ํ•œ hard link๋ฅผ ์ƒ์„ฑํ•˜๊ณ  h5dump๋กœ ๊ด€์ฐฐํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. import numpy as np import h5py f = h5py.File('link1.h5','w') g1 = f.create_group('g1') f['g2'] = g1 f.close() >h5dump link1.h5 HDF5 "link1.h5" { GROUP "/" { GROUP "g1" { } GROUP "g2" { HARDLINK "/g1" } } } ๋‹ค์Œ์€ ๋‹จ์ˆœํžˆ ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•œ hard link๋ฅผ ์ƒ์„ฑํ•˜๊ณ  h5dump๋กœ ๊ด€์ฐฐํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. import numpy as np import h5py f = h5py.File('link2.h5','w') d1 = f.create_dataset('d1',shape=(2,2),dtype=np.int32) g1 = f.create_group('g1') f['g2'] = g1 g1['d2'] = d1 f.close() >h5dump link2.h5 HDF5 "link2.h5" { GROUP "/" { DATASET "d1" { DATATYPE H5T_STD_I32LE DATASPACE SIMPLE { ( 2, 2 ) / ( 2, 2 ) } DATA { (0,0): 0, 0, (1,0): 0, 0 } } GROUP "g1" { DATASET "d2" { HARDLINK "/d1" } } GROUP "g2" { HARDLINK "/g1" } } } 7.7 ์‹ค์ œ ์˜ˆ์ œ ์œ ํ•œ ์š”์†Œ ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ ์ €์žฅ import numpy as np import h5py inp = ''' *Title test *Node 1, 0, 0, 1 2, 2, 1, 0 *Element, TYPE=Element 1,2,1 *Step 0.1 ''' nodes = [ [1,3,1,0,0], [2,3,0,21], [3,1,2,1,3], [4,5,1,2,3,1,4]] elms = [ [1, 0,0, 10,0], [2, 10,0,12,1], [3, 1,2,3, -4,2,1]] f = h5py.File('fem.h5','w') sdtype = h5py.string_dtype(encoding='utf-8') vdtype = h5py.vlen_dtype(np.dtype('float64')) #inpGroup = f.create_group('/inp') #inpGroup.create_dataset('inp',data=inp, dtype=sdtype) # only one string : (1, ) string f.attrs['inp'] = inp step1 = f.create_group('/Step1') # /2/1 step1.attrs['TargetElement'] = 'Elset1, Elset2, Elset3' step1.attrs['TargetLoad'] = 'LC1, LC2' step1.attrs['TargetConstraint'] = 'C1, C2' step1.attrs['FieldOutputs'] = 'D, SF' dofs = np.zeros((len(nodes),2),dtype='int') for i, n in enumerate(nodes): dofs[i, 0] = n[0] dofs[i, 1] = n[1] step1.attrs['dofs'] = dofs frame1 = step1.create_group('frame1') frame1.attrs['time'] = 0.1 disp = frame1.create_dataset('D',shape=(4, ),dtype=[("iD",np.dtype('int')),("data",vdtype)]) for i, n in enumerate(nodes): disp[i] = (n[0],np.array(n[2:])) sf = frame1.create_dataset('SF',shape=(3, ),dtype=[("id",np.dtype('int')),("data",vdtype)]) for i, e in enumerate(elms): sf[i] = (e[0],np.array(e[1:])) f.close() f = h5py.File('fem.h5','r') inputStream = f.attrs['inp'] print(inputStream) f.close() 8. Embedding Python in C++ program Extending๊ณผ Embedding Python๊ณผ C/C++๋ฅผ ์—ฐ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” (1) Python์—์„œ C/C++๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ(extending), (2) C/C++์—์„œ Python ์‚ฌ์šฉํ•˜๊ธฐ(embedding) ๊ฐ€ ์žˆ์œผ๋ฉฐ, ์ด๋•Œ ๋‹ค์Œ์˜ ๋‘ ๊ฐ€์ง€๋กœ ์ด๋ฅผ ๊ตฌํ˜„ํ•  ์žˆ๋‹ค. Python์—์„œ ์ œ๊ณตํ•˜๋Š” API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• pybind11, PythonQt, Boost.Python ๋“ฑ ํŒจํ‚ค์ง€๋ฅผ ์ด์šฉํ•ด ๋ณด๋‹ค ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ์‚ดํŽด๋ณด๋ฉด ์ตœ๊ทผ์—๋Š” pybind11์„ ๋งŽ์„ ์“ฐ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. pybind11์„ ์“ฐ๊ธฐ ์œ„ํ•ด์„œ๋Š” C++ 11์„ ์ง€์›ํ•ด์•ผ ํ•œ๋‹ค. pybind11 ์ฐธ๊ณ  ์‚ฌ์ดํŠธ https://github.com/pybind/pybind11 https://pybind11.readthedocs.io/en/stable/ Visual Studio์—์„œ ์„ค๋ช…๋œ pybind11 pybind11 ์„ค์น˜ > pip install pybind11 ์„ค์น˜ํ•œ ํ›„ ๋‹ค์Œ์„ ํ†ตํ•ด ์„ค์น˜๋œ ํ—ค๋” ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. >python -m pybind11 --includes -IC:\Python\Python38\Include -IC:\Python\Python38\lib\site-packages\pybind11\include ์œ„์—์„œ -I๋ฅผ ๋บ€ ๊ฒฝ๋กœ๊ฐ€ C++ ์ฝ”๋“œ ์ปดํŒŒ์ผ ์‹œ ํ•„์š”ํ•œ ์ธํด๋ฃจ๋“œ ๊ฒฝ๋กœ์ด๋‹ค. Visual Studio 2019์—์„œ ๋นˆ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค. ํ”„๋กœ์ ํŠธ ์†์„ฑ ์„ค์ • ์ถ”๊ฐ€ ํฌํ•จ ๋””๋ ‰ํ„ฐ๋ฆฌ : C:\Python\Python38\Include;C:\Python\Python38\lib\site-packages\pybind11\include ์ถ”๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋””๋ ‰ํ„ฐ๋ฆฌ : C:\Python\Python38\libs ์ถ”๊ฐ€ ์ข…์†์„ฑ : python38.lib ์ฐธ๊ณ  : ``` 8.1 ๊ธฐ๋ณธ Python Embedding ๊ธฐ๋ณธ ๋ฐฉ๋ฒ• ์ค€๋น„ CMake๋ฅผ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ embedding ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด ๋ณธ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์ค€๋น„ํ•œ๋‹ค. HelloEmbed/main.cpp CMakeLIsts.txt #define PY_SSIZE_T_CLEAN #include <Python.h> int main(int argc, char *argv[]) { wchar_t *program = Py_DecodeLocale(argv[0], NULL); if (program == NULL) { fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); exit(1); } Py_SetProgramName(program); /* optional but recommended */ Py_Initialize(); PyRun_SimpleString("from time import time, ctime " "print('Today is', ctime(time()))\n"); if (Py_FinalizeEx() < 0) { exit(120); } PyMem_RawFree(program); return 0; } cmake_minimum_required(VERSION 3.12) project(HelloEmbed) # set(Python_ROOT "/path/to/python") # Set the path to the desired Python installation # Find the Python package find_package(Python REQUIRED COMPONENTS Development) set(SOURCES main.cpp ) add_executable(HelloEmbed ${SOURCES}) target_include_directories(HelloEmbed PRIVATE ${Python_INCLUDE_DIRS}) target_li ์™ธ๋ถ€ CMake ์‹คํ–‰ ์•„๋ž˜์™€ ๊ฐ™์ด HelloEmbed> mkdir build HelloEmbed> cd build HelloEmbed/build> cmake .. ์ด์ œ build ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‚ด์— *.sln ํŒŒ์ผ์„ ์—ด์–ด์„œ ์ปดํŒŒ์ผํ•œ๋‹ค. Visual Studio CMake Integration ์ ์šฉ HelloEmbed๋ฅผ ํด๋” ์—ด๊ธฐ๋กœ ์—ด๊ณ  CMake ์„ค์ •ํ•˜๊ณ , ์ปดํŒŒ์ผํ•œ๋‹ค. ๊ธฐํƒ€ ์‚ฌํ•ญ Debug ๋ฒ„์ „์œผ๋กœ ์ปดํŒŒ์ผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Python ์ธ์Šคํ†จ ์‹œ ๋””๋ฒ„๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ธ์Šคํ†จํ•ด์•ผ ํ•œ๋‹ค. numpy ๋“ฑ๊ณผ ๊ฐ™์€ ์™ธ๋ถ€ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” debug ๋ฒ„์ „์ด ์•ˆ๋œ๋‹ค. 8.2 ๋ฐฐํฌ Embedable package๋ž€ ํŒŒ์ด์ฌ ๊ณต์‹ ์‚ฌ์ดํŠธ(www.python.org)์—์„œ๋Š” windows ํ”Œ๋žซํผ์„ ๋Œ€์ƒ์œผ๋กœ full installer ์ด์™ธ์— embeddable package๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ํŒŒ์ด์ฌ 3.5๋ถ€ํ„ฐ ๋„์ž…๋œ ๊ฒƒ์ด๋ฐ ํŒŒ์ด์ฌ์„ ๊ตฌ๋™์‹œํ‚ค๋Š” ์ตœ์†Œํ•œ์˜ ๊ฒƒ๋“ค์„ ํ•œ ๊ฐœ์˜ zip ํŒŒ์ผ๋กœ ๋ฌถ์–ด ์ œ๊ณตํ•˜๋Š” ๋ฐฐํฌ๋ณธ์ด๋‹ค. ์ง์ ‘ ์ตœ์ข…์‚ฌ์šฉ์ž๊ฐ€ ์ ‘๊ทผํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค๋Š” ํŒŒ์ด์ฌ์„ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์˜ ์ผ๋ถ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์œ„ํ•ด์„œ ์ œ๊ณต๋˜๋Š” ๊ฒƒ์ด๋‹ค. python-3.11.6-amd64.exe : ์œˆ๋„์šฐ์ฆˆ 64๋น„ํŠธ ์šฉ full installer python-3.11.6-embed-amd64.zip : ์œˆ๋„์šฐ์ฆˆ 64๋น„ํŠธ ์šฉ embedable package Embeddable package์˜ ์••์ถ•์„ ํ’€๊ฒŒ ๋˜๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด python3x.dll, python.exe ๋“ฑ๊ณผ ๊ฐ๊ป˜ zip ํŒŒ์ผ์—. pyc๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค. ์ •๊ทœ ํŒŒ์ด์ฌ์—์„œ ํŒจํ‚ค์ง€๊ฐ€ ์ €์žฅ๋˜๋Š” Lib ๋Œ€์‹  ์ด๋“ค ์ •๋ณด๊ฐ€ python310.zip์— ์••์ถ•๋˜์–ด ์žˆ๋‹ค. ๋ฐฐํฌํ•  ๋•Œ๋Š” ์ด ํŒจํ‚ค์ง€๋ฅผ sub-directory์— ๋„ฃ์–ด ๋ฐฐํฌํ•˜๋„๋ก ํ•œ๋‹ค. ํ…Œ์ŠคํŠธํ•ด๋ณด๊ธฐ Embeddable Package์˜ ์••์ถ•์„ ํ‘ผ ํ›„ ๊ทธ ํด๋”์—์„œ embed_python_folder> python >>> import numpy --> numpyd์•„ ์—†์œผ๋ฏ€๋กœ error >>> exit() --> ์—๋Ÿฌ >>> import sys >>> sys.exit() --> ์ด์ œ ์ œ๋Œ€๋กœ ๋น ์ ธ๋‚˜์˜ด embed_python_folder> ์•„๋ž˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋””ํดํŠธ ๋ชจ๋“ˆ ๊ฒฝ๋กœ๊ฐ€ ์ผ๋ฐ˜ python๊ณผ ๋‹ค๋ฆ„์„ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ working directory๊ฐ€ ์ž๋™์œผ๋กœ ๊ฒฝ๋กœ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ด๋‹ค. >>> import sys >>> sys.path ['E:\\TestPython\\embed\\python311.zip', 'E:\\TestPython\\embed'] Embeded python์—๋Š” ์••์ถ• ํ•ด์ œ๋œ ๊ฒฝ๋กœ์— ์žˆ๋Š” python3x._pth ํŒŒ์ผ์— ๊ฒฝ๋กœ ์ •๋ณด๋ฅผ ์ฝ์–ด์˜ค๊ฒŒ ๋œ๋‹ค. python311.zip # Uncomment to run site.main() automatically #import site ๋งŒ์•ฝ python3x._pth ์—†๋‹ค๋ฉด ์ปดํ“จํ„ฐ์— ์ธ์Šคํ†จ ๋œ python์ฒ˜๋Ÿผ ๊ฒฝ๋กœ๋ฅผ ์ฝ๊ฒŒ ๋œ๋‹ค(์ด ๊ฒฝ์šฐ ๋ฒ„์ „์ด ๋งž์ง€ ์•Š์œผ๋ฉด ํฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•จ). ๋‹ค์‹œ ๋งํ•˜๋ฉด python3x._pth์ด ์กด์žฌํ•˜๋Š” ํ•œ working directory๋ฅผ ๋ชจ๋“ˆ ์ฐพ๊ธฐ ๊ฒฝ๋กœ์— ๋„ฃ์„ ์ˆ˜๋Š” ์—†๋‹ค. ์ถ”๊ฐ€ ํŒจํ‚ค์ง€ ์ธ์Šคํ†จ ์ •๊ทœ ๋ฐฐํฌ ๋ณธ๊ณผ ๋‹ฌ๋ฆฌ pip๊ฐ€ ์—†์œผ๋ชฐ pip๋ถ€ํ„ฐ ๊น”์•„์•ผ ํ•œ๋‹ค. pip ์‚ฌ์ดํŠธ(https://pip.pypa.io/en/stable/installation/ )์—์„œ get-pip.py ๋‹ค์šด๋กœ๋“œ ํ›„ ์•„๋ž˜๋ฅผ ์‹คํ–‰ embedded_python> python get-pip.py ์œ„๋ฅผ ์‹คํ–‰ํ•˜๋ฉด Lib, Scripts ํด๋”๊ฐ€ ์ถ”๊ฐ€๋กœ ์ƒ๊ธด๋‹ค. python3x._pth์—์„œ Lib\site-packages๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. python311.zip # Uncomment to run site.main() automatically #import site Lib\site-packages ์ฃผ์˜ > pip ... ๋“ฑ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ์‹œ์Šคํ…œ์— ๊น”๋ฆฐ python๊ณผ ์—ฐ๊ฒฐ๋œ pip๊ฐ€ ์‹คํ–‰๋˜๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ > python -m pip ... ๋“ฑ์œผ๋กœ ์‹คํ–‰ ํ•„์š” ํŒจํ‚ค์ง€ ์ธ์Šคํ†จ ๋‹ค์€์€ numpy, scipy, matplotlib, pandas, openpyxl, h5py์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ ์˜ˆ์ด๋‹ค. embed> python -m pip install numpy embed> python -m pip install scipy embed> python -m pip install matplotlib embed> python -m pip install pandas embed> python -m pip install openpyxl embed> python -m pip install h5py embed> python -m pip install PyQt5 ๋‹ค์Œ์€ ํ…Œ์ŠคํŠธ์ด๋‹ค. embed> python >>> import numpy as np >>> import matplotlib.pyplot as plt >>> x = np.arnge(0,10,0.1) >>> y = np.sin(y) >>> plt.plot(x, y) >>> plt.show() >>> matplotlib ๋ฐฑ์—”๋“œ ์ˆ˜์ • matplotlib ์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ์ƒ์—…์ ์œผ๋กœ ๋ฐฐํฌํ•˜๋ ค๋ฉด backend๋กœ ์‚ฌ์šฉํ•˜๋Š” PyQt5๋ฅผ TkInter๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค. numpy, scipy, matplotlib. pandas, openpyxl, h5py์˜ ์„ค์น˜๋Š” ์ด์ „๊ณผ ๋™์ผํ•˜๋‹ค. embed> python -m pip install numpy embed> python -m pip install scipy embed> python -m pip install matplotlib embed> python -m pip install pandas embed> python -m pip install openpyxl embed> python -m pip install h5py TkInter๋Š” embeddable package์— ํฌํ•จ๋˜์ง€ ์•Š๊ณ , pip๋กœ ์ธ์Šค ํ†จ๋ฆฌ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฐฉ๋ฒ•์€ embeddable package์™€ ๋™์ผํ•œ full installation์—์„œ ์ง์ ‘ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜์—ฌ ํ•ด๊ฒฐํ•œ๋‹ค. FullInstall/tcl --> Embeddable/tcl [ํด๋”] FullInstall/Lib/tkinter --> Embeddable/tkinter [ํด๋”] FullInstall/DLLs/tcl86t.dll --> Embeddalbe/tcl86t.dll [ํŒŒ์ผ] FullInstall/DLLs/tk86t.dll --> Embeddalbe/tk86t.dll [ํŒŒ์ผ] FullInstall/DLLs/_tkinter.pyd --> Embeddalbe/_tkinter.pyd [ํŒŒ์ผ] ๋‹ค์Œ์€ tkinter ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์ด๋‹ค. import tkinter root = tkinter.Tk() root.title("TEST") root.mainloop() ๋‹ค์Œ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด Matplotlib ๋ฐฑ์—”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด ์ค€๋‹ค. embed/Lib/site-packages/matplotlib/mpl-data/matplotlibrc ... #backend: TkAgg ... bokeh bokeh๋งŒ ์ธ์Šคํ†จํ•˜๋ฉด, numpy, pandas๋Š” ๊น”๋ฆผ. > python -m pip install bokeh<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ์‹ค์šฉ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์œ ๊ฒฝํ—˜์ž๋ฅผ ์œ„ํ•œ ๊ฐ•์ขŒ ### ๋ณธ๋ฌธ: <NAME>์˜ ใ€Š Practical Python Programming ใ€‹์„ ๋ฒˆ์—ญํ•œ ์ฑ…์ž…๋‹ˆ๋‹ค. ๊ถ๊ธˆํ•œ ์ ์€ ๋Œ“๊ธ€์„ ๋‚จ๊ฒจ ์ฃผ์‹œ๊ณ , ์ฑ—๋ด‡์—๊ฒŒ๋„ ๋ฌผ์–ด๋ณด์„ธ์š”. ํŒŒ์ด์ฌ ์•Œ๋ ค์ฃผ๋Š” ๋ด‡ 2(GPT-3.5 Turbo) ๋จธ๋ฆฌ๋ง ์ด ์ฑ…์€ ๋ฐ์ด๋น„๋“œ M. ๋น„์ฆ๋ฆฌ(David Beazley)์˜ ใ€Š Practical Python Programming ใ€‹์„ ๋ฒˆ์—ญํ•œ ๊ฒƒ์œผ๋กœ, ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์ ‘ํ•ด๋ณด์‹  ๋ถ„์ด ํŒŒ์ด์ฌ์„ ๋น ๋ฅด๊ฒŒ ์ตํž ์ˆ˜ ์žˆ๊ฒŒ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฉ์–ด ์„ ํƒ์— ๋„์›€ ์ฃผ์‹  ๋ถ„๋“ค๊ป˜ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค('๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌ ํ—จ ์…˜', '์ œ๋„ˆ๋ ˆ์ดํ„ฐ'). ํ™˜์˜ํ•œ๋‹ค! ๋‚ด๊ฐ€ ํŒŒ์ด์ฌ(Python)์„ ๊ฑฐ์˜ 25๋…„ ์ „์— ์ฒ˜์Œ ๋ฐฐ์› ์„ ๋•Œ, ์˜จ๊ฐ– ์ง€์ €๋ถ„ํ•œ ํ”„๋กœ์ ํŠธ์˜ ์ƒ์‚ฐ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์— ์ถฉ๊ฒฉ์„ ๋ฐ›์•˜๋‹ค. ๊ทธ๋กœ๋ถ€ํ„ฐ ์‹ญ ๋…„์ด ํ๋ฅธ ๋’ค์—๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๊ทธ๋Ÿฌํ•œ ์žฌ๋ฏธ๋ฅผ ๊ฐ€๋ฅด์น˜๊ฒŒ ๋๋‹ค. 2007๋…„๋ถ€ํ„ฐ ์ง€๊ธˆ๊นŒ์ง€ 400๊ฐœ๊ฐ€ ๋„˜๋Š” ๊ทธ๋ฃน์— ์ด ์ฝ”์Šค๋ฅผ ๊ฐ€๋ฅด์ณค๋‹ค. ํŠธ๋ ˆ์ด๋”, ์‹œ์Šคํ…œ ๊ด€๋ฆฌ์ž, ์ฒœ๋ฌธํ•™์ž, ํŒ…์ปค๋Ÿฌ, ์‹ฌ์ง€์–ด ํ™”์„ฑ์— ๋กœ๋ฒ„๋ฅผ ์ฐฉ๋ฅ™์‹œํ‚ค๋Š” ๋ฐ ์ฐธ์—ฌํ•œ ์ˆ˜๋ฐฑ ๋ช…์˜ ๋กœ์ผ“ ๊ณผํ•™์ž๊ฐ€ ์ด ์ฝ”์Šค๋ฅผ ๋“ค์—ˆ๋‹ค. ์ด์ œ ์ด ์ฝ”์Šค์— ํฌ๋ฆฌ์—์ดํ‹ฐ๋ธŒ ์ปค๋จผ์ฆˆ ๋ผ์ด์„ ์Šค๋ฅผ ์ ์šฉํ•˜๊ฒŒ ๋˜์–ด ๊ธฐ์˜๋‹ค. ํ•จ๊ป˜ ์ฆ๊ธฐ์ž! ๊นƒํ—ˆ๋ธŒ ํŽ˜์ด์ง€ | ๊นƒํ—ˆ๋ธŒ ์ €์žฅ์†Œ. --David Beazley (https://dabeaz.com), @dabeaz ์ด๊ฒƒ์€ ๋ฌด์—‡์ธ๊ฐ€? ์ด ์ž๋ฃŒ๋Š” ๊ธฐ์—… ํŠธ๋ ˆ์ด๋‹๊ณผ ์ „๋ฌธ์ ์ธ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ, ๊ฐ•์‚ฌ๊ฐ€ ์ด๋„๋Š” ํŒŒ์ด์ฌ ๊ต์œก ์ฝ”์Šค์˜ ํ•ต์‹ฌ์ด๋‹ค. 2007๋…„๋ถ€ํ„ฐ ์ง€์†์ ์œผ๋กœ ๊ฐœ๋ฐœํ–ˆ์œผ๋ฉฐ ์‹ค์ œ ๊ต์žฌ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ ๊ฒ€์ฆ์„ ๊ฑฐ์ณค๋‹ค. ์ด ์ฝ”์Šค๋ฅผ ๊ฐ€๋ฅด์น˜๋Š” ๋ฐ ๋ณดํ†ต ์‚ฌ๋‚˜ํ˜์ด ๊ฑธ๋ฆฌ๋ฉฐ ์‹œ๊ฐ„์œผ๋กœ๋Š” 25~35์‹œ๊ฐ„์˜ ๊ฐ•๋„ ๋†’์€ ๊ต์œก์ด ํ•„์š”ํ•˜๋‹ค. ๋˜ํ•œ, ์•ฝ 130 ๊ฐ€์ง€ ์ฝ”๋”ฉ ์‹ค์Šต์„ ์™„๋ฃŒํ•ด์•ผ ํ•œ๋‹ค. ๋Œ€์ƒ ๋…์ž ์ด ์ฝ”์Šค์˜ ์ˆ˜๊ฐ•์ƒ์€ ์ „๋ฌธ์ ์ธ ๊ณผํ•™์ž, ๊ธฐ์ˆ ์ž, ํ”„๋กœ๊ทธ๋ž˜๋จธ๋กœ ์ตœ์†Œ ํ•œ ๊ฐ€์ง€ ์ด์ƒ์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์— ๊ฒฝํ—˜์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ํŒŒ์ด์ฌ์— ๋Œ€ํ•œ ์‚ฌ์ „ ์ง€์‹์€ ํ•„์š”ํ•˜์ง€ ์•Š์ง€๋งŒ, ์ผ๋ฐ˜์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฃผ์ œ์— ๋Œ€ํ•œ ์ง€์‹์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ์ฐธ๊ฐ€์ž ๋Œ€๋ถ€๋ถ„์—๊ฒŒ ์ด ์ฝ”์Šค๋Š” ๋„์ „์ ์ผ ๊ฒƒ์ด๋‹ค. ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ์–ด๋Š ์ •๋„ ๊ฒฝํ—˜์ด ์žˆ๋‹ค ํ•˜๋”๋ผ๋„ ๊ทธ๋ ‡๋‹ค. ํ•™์Šต ๋ชฉํ‘œ ์ด ์ฝ”์Šค์˜ ๋ชฉํ‘œ๋Š” ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ธฐ์ดˆ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์ด๋ฉฐ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ, ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, ํ”„๋กœ๊ทธ๋žจ ์กฐ์งํ™”์— ์ค‘์ ์„ ๋‘”๋‹ค. ์ด ์ฝ”์Šค๋ฅผ ๋งˆ์น  ๋ฌด๋ ต์—๋Š” ์œ ์šฉํ•œ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์Šค์Šค๋กœ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ ๋™๋ฃŒ๊ฐ€ ์ž‘์„ฑํ•œ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์š”๊ตฌ์‚ฌํ•ญ ์ด ์ฝ”์Šค๋ฅผ ๋งˆ์น˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์ธ ํŒŒ์ด์ฌ 3.6 ์ด์ƒ์˜ ๋ฒ„์ „๋งŒ ์žˆ์œผ๋ฉด ๋œ๋‹ค. ์ด ์ฝ”์Šค๋Š”โ€ฆ ์ด ์ฝ”์Šค๋Š” ์ดˆ๋ณด์ž์—๊ฒŒ ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ฐ€๋ฅด์น˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ํ•™์Šต์ž๊ฐ€ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋‚˜ ํŒŒ์ด์ฌ ์ž์ฒด์— ๊ฒฝํ—˜์ด ์žˆ์„ ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ๋˜ํ•œ, ์ด ์ฝ”์Šค๋Š” ์›น ๊ฐœ๋ฐœ์„ ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๋‹ค. ์›น ๊ฐœ๋ฐœ์€ ์ „ํ˜€ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์„œ์ปค์Šค๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ, ์ด ์„œ์ปค์Šค๋ฅผ ๊ณ ์ง‘ํ•˜๋”๋ผ๋„ ํฅ๋ฏธ๋กœ์šด ํ™œ๋™์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ๋™๋ฌผ์ด ๋‚˜์˜ค์ง€ ์•Š์„ ๋ฟ์ด๋‹ค. ์ด ์ฝ”์Šค๋Š” ๋ฐฑ๋งŒ ์ค„์งœ๋ฆฌ ํŒŒ์ด์ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ ์œ ์ง€ํ•˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด ๊ธฐ์ˆ ์ž๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ๋‚˜๋Š” ๊ทธ๋Ÿฐ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์ง€๋„ ์•Š์œผ๋ฉฐ, ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์—… ๋Œ€๋ถ€๋ถ„์€ ๊ทธ๋Ÿฐ ์‹์œผ๋กœ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์–ด์„œ ์ง€์›Œ๋ผ! ์ปค๋ฎค๋‹ˆํ‹ฐ ๋…ผ์˜ ์ด ์ฝ”์Šค์— ๋Œ€ํ•ด ์˜๋…ผํ•˜๊ณ  ์‹ถ์€๊ฐ€? Gitter์—์„œ ๋Œ€ํ™”์— ์ฐธ์—ฌํ•˜๋ผ. ๊ฐœ๋ณ„์ ์ธ ์‘๋‹ต์„ ์•ฝ์†ํ•  ์ˆ˜ ์—†์ง€๋งŒ, ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ๋„์™€์ค„์ง€ ๋ชจ๋ฅธ๋‹ค. ๊ฐ์‚ฌ์˜ ๋ง Llorenรง Muntaner๋Š” ์ด ์ฝ”์Šค์˜ ๋‚ด์šฉ์„ ์• ํ”Œ ํ‚ค๋…ธํŠธ์—์„œ ์ด ์˜จ๋ผ์ธ ๋ฌธ์„œ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ–ˆ๋‹ค. ์ง€๋‚œ 12๋…„ ๋™์•ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—ฌ๋Ÿฌ ๊ฐ•์‚ฌ๋“ค์ด ์ด ์ฝ”์Šค๋ฅผ ํ•œ ๋ฒˆ ์ด์ƒ ๋ฐœํ‘œํ–ˆ๋‹ค(์•ŒํŒŒ๋ฒณ์ˆœ). Ned Batchelder, Juan Pablo Claude, Mark Fenner, Michael Foord, Matt Harrison, Raymond Hettinger, Daniel Klein, Travis Oliphant, James Powell, Michael Selik, Hugo Shi, Ian Stokes-Rees, Yarko Tymciurak, Bryan Van de ven, Peter Wang, Mark Wiebe. ์ด ์ฝ”์Šค์— ์ฐธ๊ฐ€ํ•œ ์ˆ˜์ฒœ ๋ช…์˜ ์ˆ˜๊ฐ•์ƒ์—๊ฒŒ, ์˜๊ฒฌ๊ณผ ๋…ผ์˜๋ฅผ ํ†ตํ•ด ์„ฑ๊ณต์ ์ธ ์ฝ”์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ๋งŽ์€ ๋„์›€์„ ์ค€ ๊ฒƒ์— ๋Œ€ํ•ด ๊ฐ์‚ฌํ•œ๋‹ค. ์งˆ๋ฌธ๊ณผ ๋‹ต๋ณ€ ์งˆ๋ฌธ: ๋น„๋””์˜ค๊ฐ€ ์žˆ๋‚˜? ์—†๋‹ค. ์ด ์ฝ”์Šค๋Š” ๋‹น์‹ ์ด ์ง์ ‘ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด์ง€, ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ํ•˜๋Š” ๊ฒƒ์„ ๊ตฌ๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์งˆ๋ฌธ: ์ฝ”์Šค์˜ ๋ผ์ด์„ ์Šค๋Š”? Practical Python Programming์€ ํฌ๋ฆฌ์—์ดํ‹ฐ๋ธŒ ์ปค๋จผ์ฆˆ ์ €์ž‘์ž ํ‘œ์‹œ-๋™์ผ ์กฐ๊ฑด ๋ณ€๊ฒฝ ํ—ˆ๋ฝ 4.0 ๊ตญ์ œ(Attribution ShareAlike 4.0 International) ๋ผ์ด์„ ์Šค๊ฐ€ ์ ์šฉ๋œ๋‹ค. ์งˆ๋ฌธ: ์ด ์ž๋ฃŒ๋ฅผ ๊ฐ€์ง€๊ณ  ํŒŒ์ด์ฌ ์ฝ”์Šค๋ฅผ ๊ฐ€๋ฅด์ณ๋„ ๋˜๋‚˜? ๊ทธ๋ ‡๋‹ค. ๋‹จ, ์ €์ž‘์ž๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค. ์งˆ๋ฌธ: 2์ฐจ ์ €์ž‘๋ฌผ์„ ๋งŒ๋“ค์–ด๋„ ๋˜๋‚˜? ๊ทธ๋ ‡๋‹ค. ๋‹จ, ๊ฐ™์€ ๋ผ์ด์„ ์Šค๋ฅผ ์ ์šฉํ•˜๊ณ  ์ €์ž‘์ž ํ‘œ์‹œ๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. ์งˆ๋ฌธ: ๋‹ค๋ฅธ ๋‚˜๋ผ๋ง๋กœ ๋ฒˆ์—ญํ•ด๋„ ๋˜๋‚˜? ๊ทธ๋ ‡๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด ์ •๋ง ์ข‹์„ ๊ฒƒ์ด๋‹ค. ์™„๋ฃŒํ•˜๋ฉด ๋งํฌ๋ฅผ ๋ณด๋‚ด ๋‹ฌ๋ผ. ์งˆ๋ฌธ: ์ด ์ฝ”์Šค์˜ ๋ผ์ด๋ธŒ ๋ฐฉ์†ก์„ ํ•˜๊ฑฐ๋‚˜ ์˜์ƒ์„ ๋งŒ๋“ค์–ด๋„ ๋˜๋‚˜? ์ข‹๋‹ค! ๊ทธ ๊ณผ์ •์—์„œ ํŒŒ์ด์ฌ์„ ๋งŽ์ด ๋ฐฐ์šธ ์ˆ˜ ์žˆ๋‹ค. ์งˆ๋ฌธ: ์™œ ํŠน์ • ์ฃผ์ œ๋ฅผ ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๊ฐ€? ์‚ฌ๋‚˜ํ˜ ๋งŒ์— ๋‹ค๋ฃจ๊ธฐ์—๋Š” ๋ถ„๋Ÿ‰์ด ๋„ˆ๋ฌด ๋งŽ๋‹ค. ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃจ์ง€ ์•Š์•˜๋‹ค๋ฉด, ์ด๋ฏธ ์‹œ๋„ํ•ด ๋ดค์ง€๋งŒ ์‚ฌ๋žŒ๋“ค์ด ๋จธ๋ฆฌ๊ฐ€ ํ„ฐ์ ธ๋ฒ„๋ ธ๊ฑฐ๋‚˜ ์ด ์ฝ”์Šค์—์„œ ๋‹ค๋ฃฐ ๋งŒํ•œ ์‹œ๊ฐ„์ด ๋ถ€์กฑํ•ด์„œ ๊ทธ๋Ÿฐ ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ, ์ด๊ฒƒ์€ ๊ต์œก ์ฝ”์Šค์ด์ง€ ํŒŒ์ด์ฌ ๋ ˆํผ๋Ÿฐ์Šค ๋งค๋‰ด์–ผ์ด ์•„๋‹ˆ๋‹ค. ์งˆ๋ฌธ: ํ’€(pull) ์š”์ฒญ์„ ๋ฐ›๋‚˜? ์ด์Šˆ ํŠธ๋ž˜์ปค๋ฅผ ํ†ตํ•œ ๋ฒ„๊ทธ ๋ฆฌํฌํŠธ๋Š” ํ™˜์˜ํ•œ๋‹ค. ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ ์™ธ์— ํ’€ ์š”์ฒญ์€ ์ˆ˜๋ฝํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด์Šˆ๋ฅผ ๋จผ์ € ๋“ฑ๋กํ•ด ๋‹ฌ๋ผ. ์‹ค์Šต ์ค€๋น„ Practical Python Programming ์ฝ”์Šค์— ์˜จ ๊ฒƒ์„ ํ™˜์˜ํ•œ๋‹ค! ์ด ํŽ˜์ด์ง€๋Š” ์ฝ”์Šค ํ™˜๊ฒฝ ๊ตฌ์„ฑ๊ณผ ์ค€๋น„๋ฌผ์— ๋Œ€ํ•œ ์ค‘์š” ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค. ์†Œ์š” ์‹œ๊ฐ„ ์ด ์ฝ”์Šค๋Š” ๊ฐ•์‚ฌ๊ฐ€ 3~4์ผ ๋™์•ˆ ๊ฐ€๋ฅด์น˜๋Š” ๊ฒƒ์œผ๋กœ ์„ค๊ณ„ํ–ˆ๋‹ค. ์ด ์ฝ”์Šค๋ฅผ ์™„์ „ํžˆ ๋งˆ์น˜๋Š” ๋ฐ ์ตœ์†Œ 25~35 ์‹œ๊ฐ„์„ ๋“ค์ผ ๊ฒƒ์œผ๋กœ ๊ณ„ํšํ•ด์•ผ ํ•œ๋‹ค. ์ฐธ๊ฐ€์ž ๋Œ€๋ถ€๋ถ„์— ์žˆ์–ด, ํ•ด๋‹ต ์ฝ”๋“œ(์•„๋ž˜๋ฅผ ์ฐธ์กฐ)๋ฅผ ๋ณด์ง€ ์•Š๊ณ  ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ๋งŒ๋งŒ์น˜ ์•Š์•˜๋‹ค. ์ค€๋น„ ๋ฐ ํŒŒ์ด์ฌ ์„ค์น˜ ๊ธฐ๋ณธ์ ์ธ Python 3.6 ๋˜๋Š” ์ƒ์œ„ ๋ฒ„์ „๋งŒ ์žˆ์œผ๋ฉด ๋œ๋‹ค. ์šด์˜ ์ฒด์ œ, ํŽธ์ง‘๊ธฐ, IDE, ๊ธฐํƒ€ ํŒŒ์ด์ฌ ๊ด€๋ จ ๋„๊ตฌ๋Š” ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. ์„œ๋“œ ํŒŒํ‹ฐ์— ์˜์กดํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ, ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š” ๋ฐ ํ•„์š”ํ•œ ์Šคํฌ๋ฆฝํŠธ์™€ ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฒ•์ด ์ด ์ฝ”์Šค์— ํฌํ•จ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํŒŒ์ผ์„ ๋‹ค๋ฃจ๊ธฐ ์‰ฌ์šด ํ™˜๊ฒฝ์„ ์ค€๋น„ํ•ด์•ผ ํ•œ๋‹ค. ํŽธ์ง‘๊ธฐ์—์„œ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๊ณ  ์…ธ/ํ„ฐ๋ฏธ๋„์—์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ(Jupyter Notebook)๊ณผ ๊ฐ™์ด ์ข€ ๋” ์ƒํ˜ธ์ž‘์šฉ์ ์ธ ํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ•˜๊ธฐ๋ฅผ ์›ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค! ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ ๊ฐ™์€ ๊ฒƒ์€ ์‹คํ—˜์— ๋งค์šฐ ์œ ์šฉํ•˜์ง€๋งŒ, ์ด ์ฝ”์Šค์—์„œ ๊ฐ€๋ฅด์น˜๋Š” ๊ฐœ๋… ์ค‘ ๋งŽ์€ ๋ถ€๋ถ„์ด ํ”„๋กœ๊ทธ๋žจ์˜ ์กฐ์งํ™”(organization)์™€ ๊ด€๋ จ๋œ๋‹ค. ์—ฌ๋Ÿฌ ํŒŒ์ผ์— ๊ฑธ์ณ ํ•จ์ˆ˜, ๋ชจ๋“ˆ, ์ž„ํฌํŠธ ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉฐ ํ”„๋กœ๊ทธ๋žจ ๋ฆฌ ํŒฉํ„ฐ๋ง ์ž‘์—…์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ๋‚ด ๊ฒฝํ—˜์ƒ, ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ ๋“ฑ์—์„œ๋Š” ๊ทธ๋Ÿฐ ์ž‘์—…์„ ์žฌํ˜„ํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ์ €์žฅ์†Œ๋ฅผ ๋ณต์ œํ•˜๊ธฐ(Forking/Cloning) ์ด ์ฝ”์Šค๋ฅผ ์ˆ˜๊ฐ•ํ•  ์ค€๋น„๋กœ, ๊นƒํ—ˆ๋ธŒ https://github.com/dabeaz-course/practical-python ์ €์žฅ์†Œ๋ฅผ ํฌํฌ ํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ, ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ๋กœ์ปฌ ๋จธ์‹ ์— ํด๋ก  ํ•œ๋‹ค. bash % git clone https://github.com/yourname/practical-python bash % cd practical-python bash % ๋ชจ๋“  ์ž‘์—…์„ practical-python/ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์ˆ˜ํ–‰ํ•˜๋ผ. ํฌํฌ ํ•œ ์ €์žฅ์†Œ์— ์Šค์Šค๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์ปค๋ฐ‹ ํ•˜๋ฉด, ์ฝ”๋“œ๋ฅผ ํ•œ๊ณณ์— ๋ชจ์•„๋‘˜ ์ˆ˜ ์žˆ๊ณ , ์ฝ”์Šค๋ฅผ ๋งˆ์ณค์„ ๋•Œ ๋ฉ‹์ง„ ๊ธฐ๋ก์œผ๋กœ ๋‚จ์„ ๊ฒƒ์ด๋‹ค. ํฌํฌ ํ•˜๋Š” ๊ฒƒ์ด ๋‚ดํ‚ค์ง€ ์•Š๊ฑฐ๋‚˜ ๊นƒํ—ˆ๋ธŒ ๊ณ„์ •์ด ์—†๋‹ค๋ฉด, ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ์ด ์ฝ”์Šค์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์—ฌ๋Ÿฌ๋ถ„์˜ ๋จธ์‹ ์— ํด๋ก  ํ•ด๋„ ๋œ๋‹ค. bash % git clone https://github.com/dabeaz-course/practical-python bash % cd practical-python bash % ๋‹จ, ์ด ๊ฒฝ์šฐ๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ๋กœ์ปฌ ๋จธ์‹  ์™ธ์—๋Š” ์ปค๋ฐ‹ ํ•  ์ˆ˜ ์—†๋‹ค. ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ ๋ชจ๋“  ์ฝ”๋”ฉ ์ž‘์—…์€ Work/ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์ˆ˜ํ–‰ํ•˜๋ผ. ์ด ๋””๋ ‰ํ„ฐ๋ฆฌ์—๋Š” Data/ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์žˆ๋‹ค. Data/ ๋””๋ ‰ํ„ฐ๋ฆฌ์—๋Š” ์ด ์ฝ”์Šค์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ ํŒŒ์ผ๊ณผ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์žˆ๋‹ค. Data/์— ์žˆ๋Š” ํŒŒ์ผ์„ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค. ์ฝ”์Šค์˜ ์—ฐ์Šต ๋ฌธ์ œ๋Š” Work/ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ํ”„๋กœ๊ทธ๋žจ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ํ•™์Šต ์ˆœ์„œ ๊ต์žฌ๋Š” ์„น์…˜ 1๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์™„๋ฃŒํ•ด์•ผ ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ๋Š” ์ด์ „ ์„น์…˜์˜ ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ๋‹ค. ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ์”ฉ ๋ฆฌ ํŒฉํ„ฐ๋ง ํ•˜๋Š” ์—ฐ์Šต์ด ๋งŽ๋‹ค. ํ•ด๋‹ต ์ฝ”๋“œ ์—ฐ์Šต ๋ฌธ์ œ์˜ ์™„์ „ํ•œ ํ•ด๋‹ต ์ฝ”๋“œ๊ฐ€ Solutions/ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋‹ค. ํžŒํŠธ๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋Š” ์ž์œ ๋กญ๊ฒŒ ์‚ดํŽด๋ณด๋ผ. ๊ทธ๋Ÿฌ๋‚˜ ์ตœ๋Œ€์˜ ํ•™์Šตํšจ๊ณผ๋ฅผ ์œ„ํ•ด์„œ๋Š” ์Šค์Šค๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๋ผ. 1. ํŒŒ์ด์ฌ ์†Œ๊ฐœ ์ฒซ ์„น์…˜์˜ ๋ชฉํ‘œ๋Š” ํŒŒ์ด์ฌ ๊ธฐ์ดˆ๋ฅผ ์†Œ๊ฐœํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ž‘์€ ํ”„๋กœ๊ทธ๋žจ์„ ํŽธ์ง‘ํ•˜๊ณ , ์‹คํ–‰ํ•˜๊ณ , ๋””๋ฒ„๊ทธ ํ•˜๋Š” ๋ฒ•์„ ๋ฐฐ์šด๋‹ค. ๋์œผ๋กœ, CSV ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ์ฝ์–ด ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์„ ํ•˜๋Š” ์งง์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. 1.1 ํŒŒ์ด์ฌ ์†Œ๊ฐœ 1.2์ฒซ ๋ฒˆ์งธ ํ”„๋กœ๊ทธ๋žจ 1.3 ์ˆซ์ž 1.4 ๋ฌธ์ž์—ด(string) 1.5 ๋ฆฌ์ŠคํŠธ(list) 1.6 ํŒŒ์ผ 1.7 ํ•จ์ˆ˜ 1.1 ํŒŒ์ด์ฌ ํŒŒ์ด์ฌ์€ ๋ฌด์—‡์ธ๊ฐ€? ํŒŒ์ด์ฌ์€ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋ฐฉ์‹์˜ ๊ณ ์ˆ˜์ค€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋‹ค. ํŒŒ์ด์ฌ์€ ํŽ„(Perl), ํ‹ฐํด(Tcl), ๋ฃจ๋น„(Ruby) ๊ฐ™์€ "์Šคํฌ๋ฆฝํŒ… ์–ธ์–ด"๋กœ ๋ถ„๋ฅ˜๋œ๋‹ค. ํŒŒ์ด์ฌ์˜ ๊ตฌ๋ฌธ์€ C ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์š”์†Œ์— ์–ด๋Š ์ •๋„ ์˜ํ–ฅ์„ ๋ฐ›์•˜๋‹ค. ํŒŒ์ด์ฌ์€ 1990๋…„ ๋ฌด๋ ต Guido van Rossum์ด ์ฐฝ์‹œํ–ˆ์œผ๋ฉฐ ์˜๊ตญ์˜ ์ฝ”๋ฏธ๋”” ๊ทธ๋ฃน Monty Python์—์„œ ์ด๋ฆ„์„ ๋•„๋‹ค. ํŒŒ์ด์ฌ์„ ์–ด๋””์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‚˜? Python.org์—์„œ ํŒŒ์ด์ฌ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์ฝ”์Šค๋ฅผ ์ˆ˜๊ฐ•ํ•˜๋Š” ๋ฐ๋Š” ๊ธฐ๋ณธ ์„ค์น˜๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค. ํŒŒ์ด์ฌ 3.6 ๋˜๋Š” ๊ทธ ์ด์ƒ์˜ ๋ฒ„์ „์„ ์„ค์น˜ํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. ๊ฐ•์˜ ๋…ธํŠธ์™€ ์—ฐ์Šต ๋ฌธ์ œ ํ•ด๋‹ต์€ ํŒŒ์ด์ฌ 3.6์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ํŒŒ์ด์ฌ์„ ์™œ ๋งŒ๋“ค์—ˆ๋‚˜? ํŒŒ์ด์ฌ์˜ ์ฐฝ์‹œ์ž ๊ฐ€๋ผ์‚ฌ๋Œ€, Amoeba [์šด์˜ ์ฒด์ œ] ํ”„๋กœ์ ํŠธ์— ์‚ฌ์šฉํ•  ๊ณ ์ˆ˜์ค€ ์–ธ์–ด๊ฐ€ ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•œ ๊ฒƒ์ด ํŒŒ์ด์ฌ์„ ์ฒ˜์Œ ๋งŒ๋“ค๊ฒŒ ๋œ ๋™๊ธฐ๋‹ค. ์‹œ์Šคํ…œ ๊ด€๋ฆฌ ์œ ํ‹ธ๋ฆฌํ‹ฐ๋ฅผ C๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ๋งŽ์ด ๊ฑธ๋ฆฐ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์•˜๋‹ค. ๋”๊ตฌ๋‚˜ ๋ณธ ์…ธ(Bourne shell)๋กœ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ๋„ ๋ช‡ ๊ฐ€์ง€ ๋‚œ์ ์ด ์žˆ์—ˆ๋‹ค. ... ๊ทธ๋ž˜์„œ C์™€ ์…ธ์˜ ์ค‘๊ฐ„์ฏค ๋˜๋Š” ์–ธ์–ด๋ฅผ ๋งŒ๋“ค๊ธฐ๋กœ ํ–ˆ๋‹ค. Guido van Rossum ๋‚ด ์ปดํ“จํ„ฐ์˜ ์–ด๋””์— ํŒŒ์ด์ฌ์ด ์žˆ๋‚˜? ์‹คํ–‰ํ•˜๋Š” ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๋‹ค๋ฅด์ง€๋งŒ, ํŒŒ์ด์ฌ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ„ฐ๋ฏธ๋„ ๋˜๋Š” ๋ช…๋ น ์…ธ์—์„œ ์‹คํ–‰๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ์„œ ์„ค์น˜๋œ๋‹ค. ํ„ฐ๋ฏธ๋„์—์„œ python์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘๋™ํ•œ๋‹ค. bash $ python Python 3.8.1 (default, Feb 20 2020, 09:29:22) [Clang 10.0.0 (clang-1000.10.44.4)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("hello world") hello world >>> ์…ธ์ด๋‚˜ ํ„ฐ๋ฏธ๋„์„ ์ฒ˜์Œ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ผ๋‹จ ๋ฉˆ์ถ”๊ณ , ๊ทธ์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์ž์Šต์„œ๋ฅผ ๋ณธ ๋‹ค์Œ์— ๋Œ์•„์˜ค๊ธฐ ๋ฐ”๋ž€๋‹ค. ์…ธ์ด ์•„๋‹Œ ํ™˜๊ฒฝ์—์„œ ํŒŒ์ด์ฌ ์ฝ”๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ๋งŽ์ด ์žˆ์ง€๋งŒ, ํ„ฐ๋ฏธ๋„์—์„œ ํŒŒ์ด์ฌ์„ ์‹คํ–‰, ๋””๋ฒ„๊ทธ, ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ๋” ๋‚˜์€ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์ด ํŒŒ์ด์ฌ์˜ ๊ธฐ๋ณธ ํ™˜๊ฒฝ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์…ธ์ด๋‚˜ ํ„ฐ๋ฏธ๋„์—์„œ ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•  ์ค„ ์•Œ๋ฉด ๋‹ค๋ฅธ ํ™˜๊ฒฝ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 1.1: ํŒŒ์ด์ฌ์„ ๊ณ„์‚ฐ๊ธฐ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์ปดํ“จํ„ฐ์—์„œ ํŒŒ์ด์ฌ์„ ์‹คํ–‰ํ•ด ๋‹ค์Œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์ž. ํ–‰์šด์•„ ๋ž˜๋ฆฌ๋Š” ๊ตฌ๊ธ€ ์ฃผ์‹์„ ์ฃผ๋‹น 235.14 ๋‹ฌ๋Ÿฌ์— 75์ฃผ ์ƒ€๋‹ค. ์˜ค๋Š˜ ๊ตฌ๊ธ€ ์ฃผ๊ฐ€๋Š” 711.25 ๋‹ฌ๋Ÿฌ๋‹ค. ํŒŒ์ด์ฌ์˜ ์ƒํ˜ธ์ž‘์šฉ ๋ชจ๋“œ๋ฅผ ๊ณ„์‚ฐ๊ธฐ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•ด, ๋ž˜๋ฆฌ๊ฐ€ ์ฃผ์‹์„ ๋ชจ๋‘ ํŒ”๋ฉด ์–ผ๋งˆ๋ฅผ ๋ฒŒ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ณ„์‚ฐํ•ด ๋ณด์ž. >>> (711.25 - 235.14) * 75 35708.25 >>> ์ „๋ฌธ๊ฐ€์˜ ํŒ: ๋งˆ์ง€๋ง‰ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ‘์ค„(_) ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์•…ํ•œ ์ฃผ์‹ ์ค‘๊ฐœ์ธ์ด 20%๋ฅผ ์ˆ˜์ˆ˜๋ฃŒ๋กœ ๊ฐ€์ ธ๊ฐ€๋Š” ๊ฒฝ์šฐ ๋ž˜๋ฆฌ๋Š” ์–ผ๋งˆ์˜ ์ˆ˜์ต์„ ์–ป๋Š”๊ฐ€? >>> _ * 0.80 28566.600000000002 >>> ์—ฐ์Šต ๋ฌธ์ œ 1.2: ๋„์›€๋ง help() ๋ช…๋ น์„ ์‚ฌ์šฉํ•ด abs() ํ•จ์ˆ˜์— ๋Œ€ํ•œ ๋„์›€๋ง์„ ์ถœ๋ ฅํ•ด ๋ณด์ž. ๊ทธ๋Ÿฐ ๋‹ค์Œ help()๋ฅผ ์‚ฌ์šฉํ•ด round() ํ•จ์ˆ˜์˜ ๋„์›€๋ง๋„ ์ถœ๋ ฅํ•˜์ž. ๊ทธ๋ƒฅ ์•„๋ฌด ๊ฐ’ ์—†์ด help()๋ผ๊ณ  ์ž…๋ ฅํ•˜๋ฉด ์ƒํ˜ธ์ž‘์šฉ์ ์ธ ๋„์›€๋ง์ด ๋‚˜ํƒ€๋‚œ๋‹ค. ํ•œ ๊ฐ€์ง€ ์ฃผ์˜ํ•  ์ ์œผ๋กœ, ํŒŒ์ด์ฌ์˜ for, if, while ๊ฐ™์€ ๊ธฐ๋ณธ ๋ฌธ์žฅ์— ๋Œ€ํ•ด help()๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ help(for)์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜๋ฉด ๊ตฌ๋ฌธ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๊ทธ ๋Œ€์‹  ๋”ฐ์˜ดํ‘œ๋ฅผ ์จ์„œ help("for")๋ฅผ ์‹คํ–‰ํ•ด ๋ณด๋ผ. ๊ทธ๋ ‡๊ฒŒ ํ•ด๋„ ๋„์›€๋ง์„ ๋ณผ ์ˆ˜ ์—†์œผ๋ฉด ์ธํ„ฐ๋„ท์—์„œ ๊ฒ€์ƒ‰ํ•˜๋ผ. ์‹ฌํ™”: http://docs.python.org์—์„œ abs() ํ•จ์ˆ˜์˜ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด๋ผ(ํžŒํŠธ: ๋นŒํŠธ์ธ ํ•จ์ˆ˜ ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ ˆํผ๋Ÿฐ์Šค์— ์žˆ๋‹ค). ์—ฐ์Šต ๋ฌธ์ œ 1.3: ์ž˜๋ผ ๋ถ™์ด๊ธฐ ์ด ์ฝ”์Šค๋Š” ์ „ํ†ต์ ์ธ ์›น ํŽ˜์ด์ง€๋กœ ๊ตฌ์„ฑํ–ˆ์œผ๋ฉฐ, ์ƒํ˜ธ์ž‘์šฉ์ ์ธ ํŒŒ์ด์ฌ ์ฝ”๋“œ ์˜ˆ์ œ๋ฅผ ์ง์ ‘ ์†์œผ๋กœ ํƒ€์ดํ•‘ํ•ด ๋ณผ ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. ํŒŒ์ด์ฌ์„ ์ฒ˜์Œ์œผ๋กœ ๋ฐฐ์šด๋‹ค๋ฉด ์ด์™€ ๊ฐ™์ด "์ฒœ์ฒœํžˆ" ํ•™์Šตํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. ์†๋„๋ฅผ ๋Šฆ์ถ”๊ณ  ์ง์ ‘ ์ž…๋ ฅํ•˜๋ฉด์„œ ์–ธ์–ด๋ฅผ ์Œ๋ฏธํ•˜๊ณ , ๋ฌด์—‡์„ ํ•˜๊ณ  ์žˆ๋Š”์ง€ ์Šค์Šค๋กœ ์ƒ๊ฐํ•  ์‹œ๊ฐ„์„ ๊ฐ–๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ "๋ณต์‚ฌํ•ด์„œ ๋ถ™์—ฌ๋„ฃ๊ธฐ"ํ•ด์•ผ ํ•œ๋‹ค๋ฉด, >>> ํ”„๋กฌํ”„ํŠธ ๋’ค๋ถ€ํ„ฐ ์„ ํƒํ•˜๋˜ ๊ณต๋ฐฑ ํ–‰์ด๋‚˜ ๋‹ค์Œ ๋ฒˆ >>> ํ”„๋กฌํ”„ํŠธ(๋‘ ๊ฐ€์ง€ ์ค‘ ๋จผ์ € ๋‚˜์˜ค๋Š” ๊ฒƒ)๋ฅผ ๋„˜์–ด๊ฐ€์ง€ ์•Š๊ฒŒ ํ•œ๋‹ค. ๋ธŒ๋ผ์šฐ์ €์—์„œ "๋ณต์‚ฌ"๋ฅผ ์„ ํƒํ•˜๊ณ , ํŒŒ์ด์ฌ ์ฐฝ์œผ๋กœ ๊ฐ€์„œ "๋ถ™์—ฌ๋„ฃ๊ธฐ"๋ฅผ ์„ ํƒํ•ด ํŒŒ์ด์ฌ ์…ธ์— ๋ณต์‚ฌํ•œ๋‹ค. ๋ถ™์—ฌ ๋„ฃ์€ ํ›„์— "์—”ํ„ฐ" ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ์•ผ ์‹คํ–‰๋œ๋‹ค. ์ด ์„ธ์…˜์—์„œ ํŒŒ์ด์ฌ ๊ตฌ๋ฌธ์„ ๋ณต์‚ฌํ•ด์„œ ๋ถ™์—ฌ ๋„ฃ์–ด ์‹คํ–‰ํ•ด ๋ณด๋ผ. >>> 12 + 20 32 >>> (3 + 4 + 5 + 6) 18 >>> for i in range(5): print(i) 1 3 >>> ๊ฒฝ๊ณ : ๊ธฐ๋ณธ ํŒŒ์ด์ฌ ์…ธ์— ํŒŒ์ด์ฌ ๋ช…๋ น(>>> ์ดํ›„์˜ ๋ฌธ์žฅ)์„ ๋™์‹œ์— ์—ฌ๋Ÿฌ ๊ฐœ ๋ถ™์—ฌ ๋„ฃ๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฐ ๋ช…๋ น์„ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ๋ถ™์—ฌ ๋„ฃ์–ด๋ผ. ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๋ดค์œผ๋‹ˆ, ์•ž์œผ๋กœ ์ด ์ˆ˜์—…์—์„œ๋Š” ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ๋ถ™์—ฌ ๋„ฃ์ง€ ๋ง๊ณ  ์ฒœ์ฒœํžˆ ํƒ€์ดํ•‘ํ•ด์•ผ ํ•จ์„ ๋ช…์‹ฌํ•˜์ž. ์—ฐ์Šต ๋ฌธ์ œ 1.4: ๋ฒ„์Šค๊ฐ€ ์–ธ์ œ ์˜ค๋ ค๋‚˜? ์ข€ ๋” ์ˆ˜์ค€ ๋†’์€ ๊ฒƒ์„ ์‹œ๋„ํ•ด ๋ณด์ž. ์‹œ์นด๊ณ ์˜ ํ•œ ๋ฒ„์Šค ์ •๋ฅ˜์žฅ(Clark & Balmoral)์—์„œ ๋ถ์ชฝ์œผ๋กœ ๊ฐ€๋Š” CTA 22๋ฒˆ ๋ฒ„์Šค๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์‚ฌ๋žŒ๋“ค์ด ์–ผ๋งˆ๋‚˜ ๋” ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์ž. >>> import urllib.request >>> u = urllib.request.urlopen('http://ctabustracker.com/bustime/map/getStopPredictions.jsp?stop=14791&route=22') >>> from xml.etree.ElementTree import parse >>> doc = parse(u) >>> for pt in doc.findall('.//pt'): print(pt.text) 6 MIN 18 MIN 28 MIN >>> ๊ทธ๋ ‡๋‹ค. ๋ฐฉ๊ธˆ ๋‹จ 6์ค„์˜ ์ฝ”๋“œ๋กœ ์›นํŽ˜์ด์ง€ ํ•˜๋‚˜๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด XML ๋ฌธ์„œ๋ฅผ ํŒŒ์‹ฑํ•˜๊ณ  ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ถ”์ถœํ–ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ์‹ค์ œ ์›น์‚ฌ์ดํŠธ http://ctabustracker.com/bustime/home.jsp์— ์ œ๊ณต๋œ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด ์˜ˆ์ƒ ์‹œ๊ฐ„์ด ๋ฐ”๋€Œ๋Š”์ง€ ์ง€์ผœ๋ณด๋ผ. ์ฐธ๊ณ : ์ด ์„œ๋น„์Šค๋Š” 30๋ถ„ ๋‚ด์— ์˜ค๋Š” ๋ฒ„์Šค์˜ ๋„์ฐฉ ์‹œ๊ฐ„๋งŒ ์ œ๊ณตํ•œ๋‹ค. ๋งŒ์•ฝ ๋‹น์‹ ์ด ๋‹ค๋ฅธ ์‹œ๊ฐ„๋Œ€์— ์‚ด๊ณ  ์žˆ์–ด ์‹œ์นด๊ณ ๊ฐ€ ๋ฐค๋Šฆ์€ ์‹œ๊ฐ„์ด๋ผ๋ฉด ๋ฒ„์Šค๊ฐ€ ๋‹ค๋‹ˆ์ง€ ์•Š์•„ ์ถœ๋ ฅ์ด ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์œ„์˜ ๋งํฌ๋ฅผ ํ™•์ธํ•˜๋ผ. ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์ธ import urllib.request๊ฐ€ ์‹คํŒจํ–ˆ๋‹ค๋ฉด, ์•„๋งˆ ํŒŒ์ด์ฌ 2๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ทธ๋Ÿด ๊ฒƒ์ด๋‹ค. ์ด ๊ฐ•์ขŒ๋Š” ํŒŒ์ด์ฌ 3.6 ์ดํ›„ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•œ๋‹ค. ํ•„์š”ํ•˜๋‹ค๋ฉด https://www.python.org์—์„œ ๋‹ค์šด๋กœ๋“œํ•˜๋ผ. HTTP ํ”„๋ฝ์‹œ ์„œ๋ฒ„๊ฐ€ ํ•„์š”ํ•œ ์ž‘์—… ํ™˜๊ฒฝ์ด๋ผ๋ฉด ์ด ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด HTTP_PROXY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ: >>> import os >>> os.environ['HTTP_PROXY'] = 'http://yourproxy.server.com' >>> ์ž˜ ์•ˆ๋˜๋”๋ผ๋„ ๊ฑฑ์ •ํ•  ํ•„์š”๋Š” ์—†๋‹ค. ์ด ์ฝ”์Šค์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ XML ํŒŒ์‹ฑ์„ ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๋‹ค. 1.2์ฒซ ๋ฒˆ์งธ ํ”„๋กœ๊ทธ๋žจ ์ด ์„น์…˜์—์„œ๋Š” ์ฒ˜์Œ์œผ๋กœ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋กœ ์‹คํ–‰ํ•˜๊ณ , ๊ธฐ๋ณธ์ ์ธ ๋””๋ฒ„๊น…์„ ํ•ด๋ณธ๋‹ค. ํŒŒ์ด์ฌ ์‹คํ–‰ํ•˜๊ธฐ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์€ ํ•ญ์ƒ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์‹คํ–‰๋œ๋‹ค. ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” "์ฝ˜์†” ๊ธฐ๋ฐ˜" ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์œผ๋กœ, ๋ช…๋ น ์…ธ์—์„œ ์‹คํ–‰๋œ๋‹ค. python3 Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> ์ „๋ฌธ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ์ „ํ˜€ ์–ด๋ ค์›€์ด ์—†๊ฒ ์ง€๋งŒ, ์ดˆ๋ณด์ž์—๊ฒŒ๋Š” ๊ทธ๋ฆฌ ์นœํ™”์ ์ด์ง€ ์•Š๋‹ค. ๋‹ค๋ฅธ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ๋„ ์žˆ๋‹ค. ๊ทธ๊ฒƒ๋„ ์ข‹์ง€๋งŒ, ํŒŒ์ด์ฌ ํ„ฐ๋ฏธ๋„์„ ์‹คํ–‰ํ•˜๋Š” ๋ฒ•์„ ๋ฐฐ์›Œ๋‘๋ฉด ์“ธ๋ชจ๊ฐ€ ์žˆ๋‹ค. ์ƒํ˜ธ์ž‘์šฉ ๋ชจ๋“œ ํŒŒ์ด์ฌ์„ ์‹œ์ž‘ํ•  ๋•Œ ์ƒํ˜ธ์ž‘์šฉ(interactive) ๋ชจ๋“œ๋ฅผ ์„ ํƒํ•ด ์‹คํ—˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์žฅ์„ ํƒ€์ดํ•‘ํ•ด์„œ ์ฆ‰์‹œ ์‹คํ–‰ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํŽธ์ง‘/์ปดํŒŒ์ผ/์‹คํ–‰/๋””๋ฒ„๊ทธ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. >>> print('hello world') hello world >>> 37*42 1554 >>> for i in range(5): ... print(i) ... 1 3 >>> ์ด๊ฒƒ์„ ์ฝ๊ธฐ-ํ‰๊ฐ€-ํ”„๋ฆฐํŠธ-๋ฃจํ”„(read-eval-print-loop, REPL)๋ผ ํ•œ๋‹ค. REPL์€ ๋””๋ฒ„๊ทธ์™€ ํƒ์ƒ‰์— ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค. ์ž ๊น! ํŒŒ์ด์ฌ์„ ์ƒํ˜ธ์ž‘์šฉ ๋ชจ๋“œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์„ ๋ชจ๋ฅด๊ฒ ์œผ๋ฉด ์ง„๋„๋ฅผ ๋‚˜๊ฐ€์ง€ ๋ง๊ณ  ์‚ฌ์šฉ๋ฒ•๋ถ€ํ„ฐ ์ตํ˜€๋ผ. IDE๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋ฉ”๋‰ด ์˜ต์…˜์ด๋‚˜ ๋‹ค๋ฅธ ์ฐฝ์— ์ˆจ๊ฒจ์ ธ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ด ์ฝ”์Šค๋Š” ํ•™์Šต์ž๊ฐ€ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. REPL์„ ์ข€ ๋” ์‚ดํŽด๋ณด์ž. >>>์€ ๋ฌธ์žฅ์„ ์ƒˆ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ธํ„ฐํ”„๋ฆฌํ„ฐ ํ”„๋กฌํ”„ํŠธ๋‹ค. ...์€ ๋ฌธ์žฅ์„ ์ด์–ด๊ฐ€๋Š” ์ธํ„ฐํ”„๋ฆฌํ„ฐ ํ”„๋กฌํ”„ํŠธ๋‹ค. ํƒ€์ดํ•‘์„ ๋งˆ์น˜๋ ค๋ฉด ๊ณต๋ฐฑ ํ–‰์„ ์ž…๋ ฅํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ž…๋ ฅํ•œ ๊ฒƒ์ด ์‹คํ–‰๋œ๋‹ค. ... ํ”„๋กฌํ”„ํŠธ๋Š” ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๋ณด์ผ ์ˆ˜๋„ ์žˆ๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด ์ฝ”์Šค๋Š” ์ฝ”๋“œ ์˜ˆ์ œ๋ฅผ ์‰ฝ๊ฒŒ ๋ณต์‚ฌ/๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ด๊ฒƒ์„ ๊ณต๋ฐฑ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๋ฐ‘์ค„(_)์€ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•œ๋‹ค. >>> 37 * 42 1554 >>> _ * 2 3108 >>> _ + 50 3158 >>> ๋‹จ, ์ƒํ˜ธ์ž‘์šฉ ๋ชจ๋“œ์—์„œ๋งŒ ์ด๋ ‡๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์— _๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑํ•˜๊ธฐ ํ”„๋กœ๊ทธ๋žจ์€. py ํŒŒ์ผ์— ์ €์žฅํ•œ๋‹ค. # hello.py print('hello world') ์„ ํ˜ธํ•˜๋Š” ํ…์ŠคํŠธ ํŽธ์ง‘๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ด ์ด๋Ÿฌํ•œ ํŒŒ์ผ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ํ•˜๊ธฐ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ–ˆ์œผ๋ฉด, ํ„ฐ๋ฏธ๋„์—์„œ python ๋ช…๋ น์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด ์‹คํ–‰ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ ๋‹‰์Šค(Unix) ๋ช…๋ นํ–‰์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•œ๋‹ค. bash % python hello.py hello world bash % ์œˆ๋„(Windows) ์…ธ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•œ๋‹ค. C:\SomeFolder>hello.py hello world C:\SomeFolder>c:\python36\python hello.py hello world ์ฐธ๊ณ : ์œˆ๋„์—์„œ๋Š” ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์˜ ์ „์ฒด ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค(์˜ˆ: c:\python36\python). ๊ทธ๋ ‡์ง€๋งŒ, ํŒŒ์ด์ฌ์„ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ์‹์œผ๋กœ ์„ค์น˜ํ–ˆ๋‹ค๋ฉด hello.py์™€ ๊ฐ™์ด ํ”„๋กœ๊ทธ๋žจ ์ด๋ฆ„๋งŒ ํƒ€์ดํ•‘ํ•ด๋„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์˜ˆ์ œ ํ”„๋กœ๊ทธ๋žจ ๋‹ค์Œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด ๋ณด์ž. ์–ด๋Š ๋‚  ์•„์นจ, ๋‹น์‹ ์€ ์‹œ์นด๊ณ ์˜ ์‹œ์–ด์Šค ํƒ€์›Œ(Sears tower) ๊ทผ์ฒ˜๋ฅผ ๊ฑฐ๋‹๋‹ค๊ฐ€ ๋ณด๋„์— 1 ๋‹ฌ๋Ÿฌ ์ง€ํ๋ฅผ ํ•œ ์žฅ ์˜ฌ๋ ค๋’€๋‹ค. ๊ทธ ํ›„ ๋งค์ผ ์™ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ๊ทธ ์œ„์— ์ง€ํ๋ฅผ ์–น์–ด ํƒ‘์„ ์Œ“์œผ๋ฉฐ, ๋†’์ด๋Š” ๋งค์ผ ๋‘ ๋ฐฐ๋กœ ๋ถˆ์–ด๋‚œ๋‹ค. ๋ˆ์œผ๋กœ ์Œ“์€ ํƒ‘์˜ ๋†’์ด๊ฐ€ ์‹œ์–ด์Šค ํƒ€์›Œ์˜ ๋†’์ด์™€ ๊ฐ™์•„์ง€๋ ค๋ฉด ์‹œ๊ฐ„์ด ์–ผ๋งˆ๋‚˜ ๊ฑธ๋ฆด๊นŒ? ํ’€์ด: # sears.py bill_thickness = 0.11 * 0.001 # ์ง€ํ์˜ ๋‘๊ป˜(0.11 mm)๋ฅผ ๋ฏธํ„ฐ๋กœ ํ™˜์‚ฐ sears_height = 442 # ๋†’์ด(๋ฏธํ„ฐ) num_bills = 1 day = 1 while num_bills * bill_thickness < sears_height: print(day, num_bills, num_bills * bill_thickness) day = day + 1 num_bills = num_bills * 2 print('Number of days', day) print('Number of bills', num_bills) print('Final height', num_bills * bill_thickness) ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋œ๋‹ค. bash % python3 sears.py 1 1 0.00011 2 2 0.00022 3 4 0.00044 4 8 0.00088 5 16 0.00176 6 32 0.00352 ... 21 1048576 115.34336 22 2097152 230.68672 Number of days 23 Number of bills 4194304 Final height 461.37344 ์ด ํ”„๋กœ๊ทธ๋žจ์—์„œ ํŒŒ์ด์ฌ์˜ ํ•ต์‹ฌ ๊ฐœ๋…์„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์žฅ(Statement) ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์€ ์ผ๋ จ์˜ ๋ฌธ์žฅ์œผ๋กœ ์ด๋ค„์ง„๋‹ค. a = 3 + 4 b = a * 2 print(b) ๊ฐ ๋ฌธ์žฅ์€ ๊ฐœํ–‰(newline)์œผ๋กœ ๋๋‚œ๋‹ค. ํŒŒ์ผ์˜ ๋์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํ•œ ๋ฌธ์žฅ์”ฉ ์‹คํ–‰๋œ๋‹ค. ๋ถ€์—ฐ ์„ค๋ช… ์ฃผ์„์€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ํ…์ŠคํŠธ๋‹ค. a = 3 + 4 # ์ด๊ฒƒ์ด ์ฃผ์„์ด๋‹ค b = a * 2 print(b) ์ฃผ์„์€ #์œผ๋กœ ํ‘œ์‹œํ•˜๋ฉฐ, ํ•ด๋‹น ํ–‰์˜ ๋๊นŒ์ง€๋‹ค. ๋ณ€์ˆ˜(Variable) ๋ณ€์ˆ˜๋Š” ๊ฐ’(value)์˜ ์ด๋ฆ„(name)์ด๋‹ค. a์—์„œ z๊นŒ์ง€(์†Œ๋ฌธ์ž ๋ฐ ๋Œ€๋ฌธ์ž)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ‘์ค„(_) ๋ฌธ์ž๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ˆซ์ž๋„ ๋ณ€์ˆ˜ ๋ช…์˜ ์ผ๋ถ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ณ€์ˆ˜ ๋ช…์˜ ์ฒซ ๊ธ€์ž์—๋Š” ์ˆซ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. height = 442 # ๊ฐ€๋Šฅ _height = 442 # ๊ฐ€๋Šฅ height2 = 442 # ๊ฐ€๋Šฅ 2height = 442 # ๋ถˆ๊ฐ€ ํƒ€์ž…(Type) ๋ณ€์ˆซ๊ฐ’์˜ ํƒ€์ž…์„ ์„ ์–ธํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋ณ€์ˆ˜์™€ ๊ด€๋ จ๋œ ํƒ€์ž…์€ ๋ณ€์ˆ˜๋ช…์ด ์•„๋‹ˆ๋ผ ์˜ค๋ฅธ์ชฝ์— ๋ฌด์—‡์ด ์˜ค๋Š๋ƒ์— ๋”ฐ๋ผ ๊ฒฐ์ •๋œ๋‹ค. height = 442 # ์ •์ˆ˜ height = 442.0 # ๋ถ€๋™์†Œ์ˆ˜์  height = 'Really tall' # ๋ฌธ์ž์—ด ํŒŒ์ด์ฌ์€ ๋™์ ์œผ๋กœ ํƒ€์ž…์ด ๊ฒฐ์ •(dynamically typed) ๋œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋จ์— ๋”ฐ๋ผ ๋ณ€์ˆ˜ "ํƒ€์ž…"์ด ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•œ ์ธ์‹์ด ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค. ํƒ€์ž…์€ ํ˜„์žฌ ํ• ๋‹น๋œ ๊ฐ’์ด ๋ฌด์—‡์ธ์ง€์— ๋‹ฌ๋ ธ๋‹ค. ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ํŒŒ์ด์ฌ์€ ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•œ๋‹ค. ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๋‹ค๋ฅธ ๊ธ€์ž๋กœ ๊ฐ„์ฃผํ•œ๋‹ค. ๋‹ค์Œ ์„ธ ๋ณ€์ˆ˜๋Š” ๋ชจ๋‘ ์„œ๋กœ ๋‹ค๋ฅด๋‹ค. name = 'Jake' Name = 'Elwood' NAME = 'Guido' ์–ธ์–ด์˜ ํ‚ค์›Œ๋“œ๋Š” ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋‹ค. while x < 0: # OK WHILE x < 0: # ์˜ค๋ฅ˜ ๋ฃจํ•‘(Looping) while ๋ฌธ์€ ๋ฃจํ”„(loop)๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. while num_bills * bill_thickness < sears_height: print(day, num_bills, num_bills * bill_thickness) day = day + 1 num_bills = num_bills * 2 print('Number of days', day) while ์•„๋ž˜์— ๋“ค์—ฌ ์“ด ๋ฌธ์žฅ์€ while ๋’ค์— ์˜ค๋Š” ํ‘œํ˜„์‹์ด true์ธ ํ•œ ๊ณ„์† ์‹คํ–‰๋œ๋‹ค. ๋“ค์—ฌ ์“ฐ๊ธฐ(Indentation) ๋“ค์—ฌ ์“ฐ๊ธฐ๋Š” ํ•จ๊ป˜ ์žˆ๋Š” ๋ฌธ์žฅ๋“ค์˜ ๊ทธ๋ฃน์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ์—์„œ, while num_bills * bill_thickness < sears_height: print(day, num_bills, num_bills * bill_thickness) day = day + 1 num_bills = num_bills * 2 print('Number of days', day) ๋‹ค์Œ ๋ฌธ์žฅ๋“ค์€ ๋“ค์—ฌ ์“ฐ๊ธฐ๊ฐ€ ๊ฐ™์œผ๋ฏ€๋กœ ํ•จ๊ป˜ ๋ฌถ์—ฌ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜ํ–‰๋œ๋‹ค. print(day, num_bills, num_bills * bill_thickness) day = day + 1 num_bills = num_bills * 2 ๊ทธ๋ ‡์ง€๋งŒ ๋งˆ์ง€๋ง‰ ํ–‰์˜ print() ๋ฌธ์€ ๋“ค์—ฌ์“ฐ๊ธฐ ํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ๋ฃจํ”„์— ์†ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋นˆ ํ–‰์€ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•œ ๊ฒƒ์œผ๋กœ, ์‹คํ–‰์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค. ๋“ค์—ฌ์“ฐ๊ธฐ ๋ชจ๋ฒ” ์‚ฌ๋ก€(best practice) ํƒญ(tab) ๋Œ€์‹  ๊ณต๋ฐฑ(space)์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ˆ˜์ค€ ๋‹น 4๊ฐœ์˜ ๊ณต๋ฐฑ์„ ์‚ฌ์šฉํ•œ๋‹ค. ํŒŒ์ด์ฌ ๊ตฌ๋ฌธ์„ ์ธ์‹ํ•˜๋Š” ํŽธ์ง‘๊ธฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ํŒŒ์ด์ฌ์€ ๊ฐ™์€ ๋ธ”๋ก ๋‚ด์˜ ๋“ค์—ฌ ์“ฐ๊ธฐ์— ๋Œ€ํ•ด์„œ๋งŒ ์ผ๊ด€์ ์ธ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ์š”๊ตฌํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚จ๋‹ค. while num_bills * bill_thickness < sears_height: print(day, num_bills, num_bills * bill_thickness) day = day + 1 # ์˜ค๋ฅ˜ num_bills = num_bills * 2 ์กฐ๊ฑด(Conditional) if ๋ฌธ์€ ์กฐ๊ฑด๋ถ€ ์‹คํ–‰์— ์‚ฌ์šฉํ•œ๋‹ค. if a > b: print('Computer says no') else: print('Computer says yes') ์กฐ๊ฑด์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์„ ๋•Œ๋Š” elif๋ฅผ ์‚ฌ์šฉํ•ด ์ถ”๊ฐ€๋กœ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค. if a > b: print('Computer says no') elif a == b: print('Computer says yes') else: print('Computer says maybe') ํ”„๋ฆฐํŒ…(Printing) print ํ•จ์ˆ˜๋Š” ์ „๋‹ฌํ•œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋‹จ ์ผํ–‰์˜ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. print('Hello world!') # ํ…์ŠคํŠธ 'Hello world!'๋ฅผ ํ”„๋ฆฐํŠธ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ํ”„๋ฆฐํŠธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณ€์ˆ˜๋ช…์ด ์•„๋‹ˆ๋ผ ๋ณ€์ˆซ๊ฐ’์ด ํ…์ŠคํŠธ๋กœ ํ”„๋ฆฐํŠธ๋œ๋‹ค. x = 100 print(x) # ํ…์ŠคํŠธ '100'์„ ํ”„๋ฆฐํŠธ ๋ณต์ˆ˜์˜ ๊ฐ’์„ print์— ์ „๋‹ฌํ•˜๋ฉด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ํ”„๋ฆฐํŠธ๋œ๋‹ค. name = 'Jake' print('My name is', name) # 'My name is Jake'๋ฅผ ํ”„๋ฆฐํŠธ print()๋Š” ํ•ญ์ƒ ๋์— ๊ฐœํ–‰์„ ๋„ฃ๋Š”๋‹ค. print('Hello') print('My name is', 'Jake') ์œ„ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ”„๋ฆฐํŠธํ•œ๋‹ค. Hello My name is Jake ๊ฐœํ–‰ ๋ฌธ์ž๋ฅผ ํ”„๋ฆฐํŠธํ•˜๊ณ  ์‹ถ์ง€ ์•Š์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•œ๋‹ค. print('Hello', end=' ') print('My name is', 'Jake') ์œ„์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ”„๋ฆฐํŠธํ•œ๋‹ค. Hello My name is Jake ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์‚ฌ์šฉ์ž๊ฐ€ ํƒ€์ดํ•‘ํ•œ ์ž…๋ ฅ์˜ ํ–‰์„ ์ฝ์œผ๋ ค๋ฉด input() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. name = input('Enter your name:') print('Your name is', name) input์€ ์‚ฌ์šฉ์ž์—๊ฒŒ ํ”„๋กฌํ”„ํŠธ๋ฅผ ํ”„๋ฆฐํŠธํ•˜๊ณ  ์‚ฌ์šฉ์ž์˜ ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๊ฒƒ์€ ์ž‘์€ ํ”„๋กœ๊ทธ๋žจ, ์—ฐ์Šต ๋ฌธ์ œ, ๊ฐ„๋‹จํ•œ ๋””๋ฒ„๊น… ๋“ฑ์— ์ ํ•ฉํ•˜๋‹ค. ์‹ค์ œ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ๊ทธ๋ฆฌ ๋งŽ์ด ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. pass ๋ฌธ ๋•Œ๋กœ๋Š” ๋นˆ ์ฝ”๋“œ ๋ธ”๋ก์„ ์ง€์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ‚ค์›Œ๋“œ๊ฐ€ pass๋‹ค. if a > b: pass else: print('Computer says false') ์ด๊ฒƒ์„ "no-op" ๋ฌธ์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค. ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋‚˜์ค‘์— ๋ฌธ์žฅ์„ ์ž‘์„ฑํ• ์ง€๋„ ๋ชจ๋ฅด๋Š” ์ž๋ฆฌ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์—ญํ• ์ด๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ํŒŒ์ด์ฌ ํŒŒ์ผ์„ ์ž‘์„ฑํ•ด ์‹คํ–‰ํ•˜๋Š” ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์ž. ์ง€๊ธˆ๋ถ€ํ„ฐ ๋ชจ๋“  ์—ฐ์Šต ๋ฌธ์ œ๋Š” practical-python/Work/ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ํŒŒ์ผ์„ ํŽธ์ง‘ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ์ ์ ˆํ•œ ์œ„์น˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ์„ ๋•๊ธฐ ์œ„ํ•ด, ์ ๋‹นํ•œ ํŒŒ์ผ๋ช…์œผ๋กœ ๋นˆ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด๋‘์—ˆ๋‹ค. Work/bounce.py ํŒŒ์ผ์„ ์ฐพ์•„๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 1.5: ์–Œ์ฒด๊ณต ๊ณ ๋ฌด๊ณต์„ 100๋ฏธํ„ฐ ๋†’์ด์—์„œ ๋–จ์–ด๋œจ๋ฆฐ๋‹ค. ์ด ๊ณต์€ ๋•…์— ๋‹ฟ์„ ๋•Œ๋งˆ๋‹ค ์›๋ž˜ ๋†’์ด์˜ 3/5๋งŒํผ ํŠ€์–ด ์˜ค๋ฅธ๋‹ค. ๊ณต์ด ์—ด ๋ฒˆ ํŠˆ ๋™์•ˆ, ๊ทธ๋•Œ๋งˆ๋‹ค ๋†’์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ…Œ์ด๋ธ”์„ ํ”„๋ฆฐํŒ… ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ bounce.py๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ํ”„๋กœ๊ทธ๋žจ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. 1 60.0 2 36.0 3 21.599999999999998 4 12.959999999999999 5 7.775999999999999 6 4.6655999999999995 7 2.7993599999999996 8 1.6796159999999998 9 1.0077695999999998 10 0.6046617599999998 ์ฐธ๊ณ : round() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ถœ๋ ฅ์„ ์ •๋ˆํ•  ์ˆ˜ ์žˆ๋‹ค. ์†Œ์ˆ˜์  ์•„๋ž˜ ๋„ค ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•ด ๋ณด๋ผ. 1 60.0 2 36.0 3 21.6 4 12.96 5 7.776 6 4.6656 7 2.7994 8 1.6796 9 1.0078 10 0.6047 ์—ฐ์Šต ๋ฌธ์ œ 1.6: ๋””๋ฒ„๊น… ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์‹œ์–ด์Šค ํƒ€์›Œ ๋ฌธ์ œ์—์„œ ๊ฐ€์ ธ์™”๋‹ค. ์ด ์ฝ”๋“œ์—๋Š” ๋ฒ„๊ทธ๊ฐ€ ์žˆ๋‹ค. # sears.py bill_thickness = 0.11 * 0.001 # ๋ฏธํ„ฐ(0.11 mm) sears_height = 442 # ๋†’์ด(๋ฏธํ„ฐ) num_bills = 1 day = 1 while num_bills * bill_thickness < sears_height: print(day, num_bills, num_bills * bill_thickness) day = days + 1 num_bills = num_bills * 2 print('Number of days', day) print('Number of bills', num_bills) print('Final height', num_bills * bill_thickness) ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด sears.py๋ผ๋Š” ์ด๋ฆ„์˜ ์ƒˆ ํ”„๋กœ๊ทธ๋žจ์— ๋ถ™์—ฌ ๋„ฃ์–ด๋ผ. ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ๋œจ๋ฉฐ ํ”„๋กœ๊ทธ๋žจ์ด ์ถฉ๋Œํ•œ๋‹ค(crash). Traceback (most recent call last): File "sears.py", line 10, in <module> day = days + 1 NameError: name 'days' is not defined ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ฝ๋Š” ๊ฒƒ์€ ํŒŒ์ด์ฌ ์ฝ”๋“œ์—์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ์ถฉ๋Œํ•œ๋‹ค๋ฉด ํŠธ๋ ˆ์ด์Šค ๋ฐฑ(traceback) ๋ฉ”์‹œ์ง€์—์„œ ๋งˆ์ง€๋ง‰ ํ–‰์„ ์ฝ์œผ๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ถฉ๋Œํ•œ ์ง„์งœ ์ด์œ ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์œ„์—๋Š” ์†Œ์Šค ์ฝ”๋“œ์˜ ์ผ๋ถ€๊ฐ€ ์žˆ์œผ๋ฉฐ ํŒŒ์ผ๋ช…๊ณผ ํ–‰ ๋ฒˆํ˜ธ๊ฐ€ ์žˆ๋‹ค. ์ฝ”๋“œ์˜ ๋ช‡ ๋ฒˆ์งธ ํ–‰์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋Š”๊ฐ€? ๋ฌด์—‡์ด ์ž˜๋ชป๋˜์—ˆ๋Š”๊ฐ€? ์˜ค๋ฅ˜๋ฅผ ์ˆ˜์ •ํ•˜๋ผ ํ”„๋กœ๊ทธ๋žจ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋ผ 1.3 ์ˆซ์ž ์ด ์„น์…˜์€ ์ˆ˜ํ•™ ๊ณ„์‚ฐ์„ ๋…ผ์˜ํ•œ๋‹ค. ์ˆซ์ž ํƒ€์ž… ํŒŒ์ด์ฌ์—๋Š” ๋„ค ๊ฐ€์ง€ ์ˆซ์ž ํƒ€์ž…์ด ์žˆ๋‹ค. ๋ถˆ๋ฆฐ ์ •์ˆ˜ ๋ถ€๋™์†Œ์ˆ˜์  ๋ณต์†Œ์ˆ˜(ํ—ˆ์ˆ˜) ๋ถˆ๋ฆฐ(bool) ๋ถˆ๋ฆฐ ๊ฐ’์€ True์™€ False ์ค‘ ํ•˜๋‚˜๋‹ค. a = True b = False ์ˆ˜ํ•™ ๊ณ„์‚ฐ์—์„œ๋Š” ๊ฐ๊ฐ 1๊ณผ 0์œผ๋กœ ํ‰๊ฐ€ํ•œ๋‹ค. c = 4 + True # 5 d = False if d == 0: print('d is False') ๊ทธ๋ ‡์ง€๋งŒ, ์ฝ”๋“œ๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ž‘์„ฑํ•˜์ง€ ๋ง์ž. ์ด์ƒํ•˜๋‹ˆ๊นŒ. ์ •์ˆ˜(int) ์ž„์˜์˜ ํฌ๊ธฐ์— ๋ฐ‘์ˆ˜(base)๊ฐ€ ์žˆ๋Š” ๋ถ€ํ˜ธ ์žˆ๋Š”(signed) ๊ฐ’์ด๋‹ค. a = 37 b = -299392993727716627377128481812241231 c = 0x7fa8 # 16์ง„์ˆ˜ d = 0o253 # 8์ง„์ˆ˜ e = 0b10001111 # 2์ง„์ˆ˜ ์ผ๋ฐ˜์ ์ธ ์—ฐ์‚ฐ: x + y ๋ง์…ˆ x - y ๋บ„์…ˆ x * y ๊ณฑ์…ˆ x / y ๋‚˜๋ˆ—์…ˆ(๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜๋ฅผ ์ƒ์„ฑ) x // y Floor ๋‚˜๋ˆ—์…ˆ(์ •์ˆ˜๋ฅผ ์ƒ์„ฑ) x % y ๋ชจ๋“ˆ๋กœ(๋‚˜๋จธ์ง€) x ** y ์ œ๊ณฑ x << n ์™ผ์ชฝ์œผ๋กœ ๋น„ํŠธ ์‹œํ”„ํŠธ(shift) x >> n ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋น„ํŠธ ์‹œํ”„ํŠธ x & y AND ๋น„ํŠธ ์—ฐ์‚ฐ x | y OR ๋น„ํŠธ ์—ฐ์‚ฐ x ^ y XOR ๋น„ํŠธ ์—ฐ์‚ฐ ~x NOT ๋น„ํŠธ ์—ฐ์‚ฐ abs(x) ์ ˆ๋Œ“๊ฐ’ ๋ถ€๋™์†Œ์ˆ˜์ (float) ์‹ญ์ง„์ˆ˜ ๋˜๋Š”<NAME> ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด ๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค. a = 37.45 b = 4e5 # 4 x 10**5 ๋˜๋Š” 400,000 c = -1.345e-10 ๋ถ€๋™์†Œ์ˆ˜์ ์€ ๋„ค์ดํ‹ฐ๋ธŒ CPU ํ‘œํ˜„ IEEE 754๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐฐ์ •๋ฐ€๋„(double precision)๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. C ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ double ํƒ€์ž…๊ณผ ๊ฐ™๋‹ค. ์ •๋ฐ€๋„๋Š” 17 ์ž๋ฆฌ -308์—์„œ 308๊นŒ์ง€ ๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜๋ฅผ ์‹ญ์ง„์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ผ ๊ฒฝ์šฐ ๋ถ€์ •ํ™•ํ•จ์— ์œ ์˜ํ•˜๋ผ. >>> a = 2.1 + 4.2 >>> a == 6.3 False >>> a 6.300000000000001 >>> ์ด๊ฒƒ์€ ํŒŒ์ด์ฌ ์ด์Šˆ๊ฐ€ ์•„๋‹ˆ๋ผ, CPU์˜ ๋ถ€๋™์†Œ์ˆ˜์  ํ•˜๋“œ์›จ์–ด ๋•Œ๋ฌธ์ด๋‹ค. ์ผ๋ฐ˜์ ์ธ ์—ฐ์‚ฐ: x + y ๋ง์…ˆ x - y ๋บ„์…ˆ x * y ๊ณฑ์…ˆ x / y ๋‚˜๋ˆ—์…ˆ x // y Floor ๋‚˜๋ˆ—์…ˆ x % y ๋ชจ๋“ˆ๋กœ x ** y ์ œ๊ณฑ abs(x) ์ ˆ๋Œ“๊ฐ’ ๋น„ํŠธ ์—ฐ์‚ฐ์„ ์ œ์™ธํ•˜๋ฉด, ์ด ์—ฐ์‚ฐ๋“ค์€ ์ •์ˆ˜ ์—ฐ์‚ฐ๊ณผ ๊ฐ™๋‹ค. ์ถ”๊ฐ€์ ์ธ ์ˆ˜ํ•™ ํ•จ์ˆ˜๊ฐ€ math ๋ชจ๋“ˆ์— ์žˆ๋‹ค. import math a = math.sqrt(x) b = math.sin(x) c = math.cos(x) d = math.tan(x) e = math.log(x) ๋น„๊ต ์ˆซ์ž์— ๋Œ€ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋น„๊ต/๊ด€๊ณ„ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. x < y ์ž‘์Œ x <= y ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ x > y ํผ x >= y ํฌ๊ฑฐ๋‚˜ ๊ฐ™์Œ x == y ๊ฐ™์Œ x != y ๊ฐ™์ง€ ์•Š์Œ ๋‹ค์Œ์„ ์‚ฌ์šฉํ•ด ๋ณต์žกํ•œ ๋ถˆ๋ฆฐ ํ‘œํ˜„์‹์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. and, or, not ์˜ˆ: if b >= a and b <= c: print('b is between a and c') if not (b < a or b > c): print('b is still between a and c') ์ˆซ์ž ๋ณ€ํ™˜ํ•˜๊ธฐ ํƒ€์ž…๋ช…์„ ์‚ฌ์šฉํ•ด ์ˆซ์ž๋ฅผ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. a = int(x) # x๋ฅผ int๋กœ ๋ณ€ํ™˜ b = float(x) # x๋ฅผ float๋กœ ๋ณ€ํ™˜ ํ•œ๋ฒˆ ํ•ด๋ณด์ž. >>> a = 3.14159 >>> int(a) >>> b = '3.14159' # ์ˆซ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๊ฐ€์ง€๊ณ ๋„ ๊ฐ€๋Šฅ >>> float(b) 3.14159 >>> ์—ฐ์Šต ๋ฌธ์ œ ์ฐธ๊ณ : ์—ฐ์Šต ๋ฌธ์ œ๋Š” practical-python/Work ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. mortgage.py ํŒŒ์ผ์„ ์ฐพ์•„๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 1.7: ๋ฐ์ด๋ธŒ์˜ ์ฃผํƒ ๋‹ด๋ณด ๋Œ€์ถœ ๋ฐ์ด๋ธŒ๋Š” 500,000 ๋‹ฌ๋Ÿฌ์˜ 30๋…„ ๊ณ ์ • ์ด์œจ ์ฃผํƒ ๋‹ด๋ณด ๋Œ€์ถœ(mortgage)์„ ๋ฐ›๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ๋‹ค. ์ด์œจ์€ 5%์ด๊ณ  ๋งค๋‹ฌ ๋‚ฉ๋ถ€ํ•  ๊ธˆ์•ก์€ 2684.11 ๋‹ฌ๋Ÿฌ๋‹ค. ๋‹ค์Œ์€ ๋Œ€์ถœ ๊ธฐ๊ฐ„ ๋™์•ˆ ์ง€๋ถˆํ•  ์ด์•ก์„ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. # mortgage.py principal = 500000.0 rate = 0.05 payment = 2684.11 total_paid = 0.0 while principal > 0: principal = principal * (1+rate/12) - payment total_paid = total_paid + payment print('Total paid', total_paid) ์ด ํ”„๋กœ๊ทธ๋žจ์„ ์ž…๋ ฅํ•ด ์‹คํ–‰ํ•˜๋ผ. ๋‹ต์ด 966,279.6๋กœ ๋‚˜์™€์•ผ ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 1.8: ์ถ”๊ฐ€ ๋‚ฉ์ž… ๋ฐ์ด๋ธŒ๊ฐ€ ์ฒ˜์Œ 12๊ฐœ์›” ๋™์•ˆ ๋งค๋‹ฌ 1000 ๋‹ฌ๋Ÿฌ๋ฅผ ์ถ”๊ฐ€๋กœ ๋‚ฉ์ž…ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? ์ด ์ถ”๊ฐ€ ๋‚ฉ์ž…๊ธˆ ๊ณ„์‚ฐ์„ ํฌํ•จํ•˜๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•ด ์ด ๋‚ฉ์ž…๊ธˆ์•ก๊ณผ ์†Œ์š” ์›”์ˆ˜๋ฅผ ํ”„๋ฆฐํŠธํ•ด ๋ณด์ž. ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์ด ๋‚ฉ์ž…๊ธˆ์ด 929,965.62, ์†Œ์š” ์›”์ˆ˜๋Š” 342๋กœ ๋‚˜์™€์•ผ ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 1.9: ์ถ”๊ฐ€ ๋‚ฉ์ž…๊ธˆ ๊ณ„์‚ฐ๊ธฐ ์ถ”๊ฐ€ ๋‚ฉ์ž…๊ธˆ์„ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•˜์ž. ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค์Œ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. extra_payment_start_month = 61 extra_payment_end_month = 108 extra_payment = 1000 ํ”„๋กœ๊ทธ๋žจ์ด ์ด ๋ณ€์ˆซ๊ฐ’์„ ์ฝ๊ณ  ์ด ๋‚ฉ์ž…์•ก์„ ๊ณ„์‚ฐํ•˜๊ฒŒ ํ•ด ๋ณด์ž. ๋ฐ์ด๋ธŒ๊ฐ€ ๋Œ€์ถœ ์‹œ์ž‘ 5๋…„ ํ›„๋ถ€ํ„ฐ 4๋…„๊ฐ„ ๋งค๋‹ฌ 1000 ๋‹ฌ๋Ÿฌ๋ฅผ ์ถ”๊ฐ€๋กœ ์ง€๋ถˆํ•  ๊ฒฝ์šฐ ์ด ๋‚ฉ์ž…์•ก์€ ์–ผ๋งˆ์ธ๊ฐ€? ์—ฐ์Šต ๋ฌธ์ œ 1.10: ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ ์›”์ˆ˜, ํ˜„์žฌ๊นŒ์ง€์˜ ๋‚ฉ๋ถ€์•ก, ๋‚จ์€ ์›๊ธˆ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ…Œ์ด๋ธ”์„ ํ”„๋ฆฐํŠธํ•˜๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•ด ๋ณด๋ผ. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅํ•œ๋‹ค. 1 2684.11 499399.22 2 5368.22 498795.94 3 8052.33 498190.15 4 10736.44 497581.83 5 13420.55 496970.98 ... 308 874705.88 3478.83 309 877389.99 809.21 310 880074.1 -1871.53 Total paid 880074.1 Months 310 ์—ฐ์Šต ๋ฌธ์ œ 1.11: ๋ณด๋„ˆ์Šค ๋งˆ์ง€๋ง‰ ๋‹ฌ์— ์ดˆ๊ณผ ๋‚ฉ๋ถ€ํ•˜๋Š” ๊ธˆ์•ก์ด ์ƒ๊ธฐ์ง€ ์•Š๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•ด ๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 1.12: ๋ฏธ์Šคํ„ฐ๋ฆฌ int()์™€ float()๋ฅผ ์‚ฌ์šฉํ•ด ์ˆซ์ž๋ฅผ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: >>> int("123") 123 >>> float("1.23") 1.23 >>> ๊ทธ๋ ‡๋‹ค๋ฉด, ์™œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘๋™ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€? >>> bool("False") True >>> 1.4 ๋ฌธ์ž์—ด ์ด ์„น์…˜์€ ํ…์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์ž‘์—…ํ•˜๋Š” ๋ฒ•์„ ์†Œ๊ฐœํ•œ๋‹ค. ๋ฆฌํ„ฐ๋Ÿด ํ…์ŠคํŠธ(Literal Text) ํ‘œํ˜„ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. # ์ž‘์€๋”ฐ์˜ดํ‘œ a = 'Yeah but no but yeah but...' # ํฐ๋”ฐ์˜ดํ‘œ b = "computer says no" # ๋”ฐ์˜ดํ‘œ ์„ธ ๊ฐœ c = ''' Look into my eyes, look into my eyes, the eyes, the eyes, the eyes, not around the eyes, don't look around the eyes, look into my eyes, you're under. ''' ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฌธ์ž์—ด์€ ํ•œ ์ค„์„ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ์˜ดํ‘œ ์„ธ ๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ํฌ๋งคํŒ…์„ ํฌํ•จํ•ด ์—ฌ๋Ÿฌ ํ–‰์— ๊ฑธ์นœ ํ…์ŠคํŠธ๊ฐ€ ๋ฌธ์ž์—ด์ด ๋œ๋‹ค. ์ž‘์€๋”ฐ์˜ดํ‘œ(')์™€ ํฐ๋”ฐ์˜ดํ‘œ(") ์‚ฌ์ด์— ์ฐจ์ด๋Š” ์—†๋‹ค. ํ•˜์ง€๋งŒ, ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘๊ณผ ๋์— ๊ฐ™์€ ์ข…๋ฅ˜์˜ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋ฌธ์ž์—ด ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ(escape code) ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋Š” ์ œ์–ด ๋ฌธ์ž๋‚˜ ํ‚ค๋ณด๋“œ์—์„œ ํƒ€์ดํ•‘ํ•˜๊ธฐ ํž˜๋“  ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ์˜ ์˜ˆ: '\n' ๋ผ์ธํ”ผ๋“œ(Line feed) '\r' ์บ๋ฆฌ์ง€ ๋ฆฌํ„ด(Carriage return) '\t' ํƒญ(Tab) '\'' ์ž‘์€๋”ฐ์˜ดํ‘œ(Literal single quote) '\"' ํฐ๋”ฐ์˜ดํ‘œ(Literal double quote) '\\' ๋ฐฑ์Šฌ๋ž˜์‹œ(Literal backslash) ๋ฌธ์ž์—ด ํ‘œํ˜„ ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์œ ๋‹ˆ์ฝ”๋“œ(Unicode) ์ฝ”๋“œ ํฌ์ธํŠธ(code-point)๋กœ ์ €์žฅ๋œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค(escape sequence)๋ฅผ ์‚ฌ์šฉํ•ด ์ •ํ™•ํ•œ ์ฝ”๋“œ ํฌ์ธํŠธ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. a = '\xf1' # a = 'รฑ' b = '\u2200' # b = 'โˆ€' c = '\U0001D122' # ๋‚ฎ์€ ์Œ์ž๋ฆฌํ‘œ d = '\N{FOR ALL}' # d = 'โˆ€' ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ „์ฒด ์บ๋ฆญํ„ฐ ์ฝ”๋“œ๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(Unicode Character Database)๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. ๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ(String Indexing) ๋ฌธ์ž์—ด์€ ๋ฌธ์ž์— ์•ก์„ธ์Šคํ•จ์— ์žˆ์–ด์„œ ๋ฐฐ์—ด(array)๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ •์ˆ˜ ์ธ๋ฑ์Šค(index)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์Œ์ˆ˜ ์ธ๋ฑ์Šค๋Š” ๋ฌธ์ž์—ด์˜ ๋์—์„œ๋ถ€ํ„ฐ ์ƒ๋Œ€์  ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. a = 'Hello world' b = a[0] # 'H' c = a[4] # 'o' d = a[-1] # 'd'(๋ฌธ์ž์—ด์˜ ๋) ๋˜ํ•œ ์ฝœ๋ก (:)์œผ๋กœ ์ธ๋ฑ์Šค ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•จ์œผ๋กœ์จ ์Šฌ๋ผ์ด์‹ฑ(๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์„ ํƒ) ํ•  ์ˆ˜ ์žˆ๋‹ค. d = a[:5] # 'Hello' e = a[6:] # 'world' f = a[3:8] # 'lo wo' g = a[-5:] # 'world' ๋ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๋ฌธ์ž๋Š” (์ธ๋ฑ์‹ฑ ๊ฒฐ๊ณผ์—) ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ƒ๋žต๋œ ์ธ๋ฑ์Šค๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ๋˜๋Š” ๋์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค. ๋ฌธ์ž์—ด ์—ฐ์‚ฐ ์ด์–ด๋ถ™์ด๊ธฐ(concatenation), ๊ธธ์ด(length), ๋ฉค๋ฒ„์‹ญ(membership), ๋ณต์ œ(replication). # ์ด์–ด๋ถ™์ด๊ธฐ(+) a = 'Hello' + 'World' # 'HelloWorld' b = 'Say ' + a # 'Say HelloWorld' # ๊ธธ์ด(len) s = 'Hello' len(s) # 5 # ๋ฉค๋ฒ„์‹ญ ํ…Œ์ŠคํŠธ(`in`, `not in`) t = 'e' in s # True f = 'x' in s # False g = 'hi' not in s # True # ๋ณต์ œ(s * n) rep = s * 5 # 'HelloHelloHelloHelloHello' ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ ๋ฌธ์ž์—ด์—๋Š” ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค. ์˜ˆ: ๋งจ ์•ž์ด๋‚˜ ๋งจ ๋’ค์˜ ํ™”์ดํŠธ ์ŠคํŽ˜์ด์Šค ์ œ๊ฑฐ. s = ' Hello ' t = s.strip() # 'Hello' ์˜ˆ: ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜ s = 'Hello' l = s.lower() # 'hello' u = s.upper() # 'HELLO' ์˜ˆ: ํ…์ŠคํŠธ ๊ต์ฒด. s = 'Hello world' t = s.replace('Hello' , 'Hallo') # 'Hallo world' ๊ธฐํƒ€ ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ ๋ฌธ์ž์—ด์—๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค. ๊ทธ์ค‘ ์ผ๋ถ€๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. s.endswith(suffix) # ๋ฌธ์ž์—ด์ด suffix๋กœ ๋๋‚˜๋Š”์ง€ ํ™•์ธ s.find(t) # s์—์„œ t๊ฐ€ ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ๊ณณ s.index(t) # s์—์„œ t๊ฐ€ ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ๊ณณ s.isalpha() # ๋ฌธ์ž๊ฐ€ ์˜๋ฌธ์ž์ธ์ง€ s.isdigit() # ๋ฌธ์ž๊ฐ€ ์ˆซ์ž์ธ์ง€ s.islower() # ๋ฌธ์ž๊ฐ€ ์†Œ๋ฌธ์ž์ธ์ง€ s.isupper() # ๋ฌธ์ž๊ฐ€ ๋Œ€๋ฌธ์ž์ธ์ง€ s.join(slist) # s๋ฅผ ๊ตฌ๋ถ„์ž(delimiter)๋กœ ์‚ผ์•„ ๋ฌธ์ž์—ด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ถ™์ด๊ธฐ(join) s.lower() # ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ s.replace(old, new) # ํ…์ŠคํŠธ ๊ต์ฒด s.rfind(t) # ๋ฌธ์ž์—ด์˜ ๋์—์„œ๋ถ€ํ„ฐ t๋ฅผ ๊ฒ€์ƒ‰ s.rindex(t) # ๋ฌธ์ž์—ด์˜ ๋์—์„œ๋ถ€ํ„ฐ t๋ฅผ ๊ฒ€์ƒ‰ s.split([๊ตฌ๋ถ„์ž]) # ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•ด ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ฆ s.startswith(prefix) # ๋ฌธ์ž์—ด์ด prefix๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ํ™•์ธ s.strip() # ์•ž๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐ s.upper() # ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ ๋ฌธ์ž์—ด์˜ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ(Mutability) ๋ฌธ์ž์—ด์€ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ(immutable), ์ฆ‰ ์ฝ๊ธฐ ์ „์šฉ์ด๋‹ค. ํ•œ ๋ฒˆ ์ƒ์„ฑํ•˜๋ฉด ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค. >>> s = 'Hello World' >>> s[1] = 'a' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>> ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์—ฐ์‚ฐ๊ณผ ๋ฉ”์„œ๋“œ๋Š” ํ•ญ์ƒ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•œ๋‹ค. ๋ฌธ์ž์—ด ๋ณ€ํ™˜ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด str()์„ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋กœ print() ๋ฌธ์ด ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ํ…์ŠคํŠธ๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค. >>> x = 42 >>> str(x) '42' >>> ๋ฐ”์ดํŠธ ์—ด(Byte String) 8๋น„ํŠธ ๋ฐ”์ดํŠธ๊ฐ€ ๋ˆ(string)์ฒ˜๋Ÿผ ์ด์–ด์ง„ ๊ฒƒ์œผ๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ์ € ์ˆ˜์ค€ ์ž…์ถœ๋ ฅ(I/O)์— ์‚ฌ์šฉ๋˜๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์“ด๋‹ค. data = b'Hello World\r\n' ์ฒซ ๋ฒˆ์งธ ๋”ฐ์˜ดํ‘œ ๋ฐ”๋กœ ์•ž์— ์†Œ๋ฌธ์ž b๋ฅผ ๋ถ™์ด๋ฉด, ํ…์ŠคํŠธ ์—ด(text string)์ด ์•„๋‹Œ ๋ฐ”์ดํŠธ ์—ด๋กœ ์ง€์ •๋œ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์€ ๋Œ€๋ถ€๋ถ„ ์ž‘๋™ํ•œ๋‹ค. len(data) # 13 data[0:5] # b'Hello' data.replace(b'Hello', b'Cruel') # b'Cruel World\r\n' ์ธ๋ฑ์‹ฑ์€ ์ข€ ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๋Š”๋ฐ, ๋ฐ”์ดํŠธ ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. data[0] # 72 ('H'์˜ ASCII ์ฝ”๋“œ) ํ…์ŠคํŠธ ์—ด๊ณผ์˜ ๋ณ€ํ™˜. text = data.decode('utf-8') # ๋ฐ”์ดํŠธ ์—ด -> ํ…์ŠคํŠธ ์—ด data = text.encode('utf-8') # ํ…์ŠคํŠธ ์—ด -> ๋ฐ”์ดํŠธ ์—ด 'utf-8' ์ธ์ž(argument)๋Š” ๋ฌธ์ž ์ธ์ฝ”๋”ฉ์„ ์ง€์ •ํ•œ๋‹ค. 'ascii'์™€ 'latin1'๋„ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ’์ด๋‹ค. ์›์‹œ ๋ฌธ์ž์—ด(Raw String) ์›์‹œ ๋ฌธ์ž์—ด์€ ๋ฐฑ์Šฌ๋ž˜์‹œ๋ฅผ ํ•ด์„ํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์ด๋‹ค. ์†Œ๋ฌธ์ž "r"์„ ์•ž์— ๋ถ™์—ฌ ์›์‹œ ๋ฌธ์ž์—ด์ž„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. >>> rs = r'c:\newdata\test' # ์›์‹œ(๋ฐฑ์Šฌ๋ž˜์‹œ๋ฅผ ํ•ด์„ํ•˜์ง€ ์•Š์Œ) >>> rs 'c:\\newdata\\test' ๋ฌธ์ž์—ด์€ ์ž…๋ ฅํ•œ ๊ทธ๋Œ€๋กœ์˜ ๋ฆฌํ„ฐ๋Ÿด ํ…์ŠคํŠธ๋‹ค. ๋ฐฑ์Šฌ๋ž˜์‹œ๊ฐ€ ํŠน๋ณ„ํžˆ ์ค‘์š”ํ•  ๋•Œ ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค. ์˜ˆ: ํŒŒ์ผ๋ช…, ์ •๊ทœ ํ‘œํ˜„์‹(regular expression) ๋“ฑ f ๋ฌธ์ž์—ด(f-String) ํฌ๋งคํŒ…๋œ ํ‘œํ˜„์‹ ๋Œ€์ฒด๊ฐ€ ์žˆ๋Š” ๋ฌธ์ž์—ด์ด๋‹ค. >>> name = 'IBM' >>> shares = 100 >>> price = 91.1 >>> a = f'{name:>10s} {shares:10d} {price:10.2f}' >>> a ' IBM 100 91.10' >>> b = f'Cost = ${shares*price:0.2f}' >>> b 'Cost = $9110.00' >>> ์ฐธ๊ณ : f ๋ฌธ์ž์—ด์€ ํŒŒ์ด์ฌ 3.6 ์ด์ƒ์—์„œ ์ง€์›ํ•œ๋‹ค. ํฌ๋งท ์ฝ”๋“œ์˜ ์˜๋ฏธ๋Š” ๋‚˜์ค‘์— ๋‹ค๋ฃฌ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ๋Š” ํŒŒ์ด์ฌ์˜ ๋ฌธ์ž์—ด ํƒ€์ž…์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์„ ์‹คํ—˜ํ•œ๋‹ค. ํŒŒ์ด์ฌ์˜ ์ƒํ˜ธ์ž‘์šฉ์ ์ธ ํ”„๋กฌํ”„ํŠธ์—์„œ ์‰ฝ๊ฒŒ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ค‘์š”: ์ธํ„ฐํ”„๋ฆฌํ„ฐ์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ๋•Œ, >>>๋Š” ํŒŒ์ด์ฌ์ด ์ƒˆ๋กœ์šด ๋ฌธ์žฅ ์ž…๋ ฅ์„ ์š”๊ตฌํ•˜๋Š” ์ธํ„ฐํ”„๋ฆฌํ„ฐ ํ”„๋กฌํ”„ํŠธ๋‹ค. ์ด ์—ฐ์Šต ๋ฌธ์ œ์˜ ๋ช‡๋ช‡ ๋ฌธ์žฅ์€ ์—ฌ๋Ÿฌ ํ–‰์— ๊ฑธ์ณ ์ž‘์„ฑ๋˜๋ฏ€๋กœ, ๊ทธ๊ฒƒ๋“ค์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด '์—”ํ„ฐ(return)'๋ฅผ ๋ช‡ ๋ฒˆ ๋ˆŒ๋Ÿฌ์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์˜ˆ์ œ๋ฅผ ์‹ค์Šตํ•  ๋•Œ >>>๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฃผ์‹ ํ‹ฐ์ปค ์‹ฌ๋ฒŒ์„ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ํ•ด ๋ณด์ž. >>> symbols = 'AAPL, IBM, MSFT, YHOO, SCO' >>> ์—ฐ์Šต ๋ฌธ์ œ 1.13: ๊ฐœ๋ณ„ ๋ฌธ์ž์™€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•˜๊ธฐ ๋ฌธ์ž์—ด์€ ๋ฌธ์ž์˜ ๋ฐฐ์—ด์ด๋‹ค. ๋ฌธ์ž๋ฅผ ๋ช‡ ๊ฐœ ์ถ”์ถœํ•ด ๋ณด์ž. >>> symbols[0] >>> symbols[1] >>> symbols[2] >>> symbols[-1] # ๋งˆ์ง€๋ง‰ ๋ฌธ์ž >>> symbols[-2] # ์Œ์ˆ˜ ์ธ๋ฑ์Šค๋Š” ๋ฌธ์ž์—ด ๋์—์„œ๋ถ€ํ„ฐ ์„ผ๋‹ค >>> ํŒŒ์ด์ฌ์—์„œ ๋ฌธ์ž์—ด์€ ์ฝ๊ธฐ ์ „์šฉ์ด๋‹ค. ์ด๊ฒƒ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด symbols์˜ ์ฒซ ๊ธ€์ž๋ฅผ ์†Œ๋ฌธ์ž 'a'๋กœ ๋ฐ”๊ฟ”๋ณด์ž. >>> symbols[0] = 'a' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>> ์—ฐ์Šต ๋ฌธ์ œ 1.14: ๋ฌธ์ž์—ด ์ด์–ด๋ถ™์ด๊ธฐ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ฝ๊ธฐ ์ „์šฉ์ด๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ๋ฌธ์ž์—ด์— ๋ณ€์ˆ˜๋ฅผ ์žฌํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด symbols์˜ ๋์— ์ƒˆ๋กœ์šด ์‹ฌ๋ฒŒ "GOOG"๋ฅผ ์ด์–ด๋ถ™์—ฌ ๋ณด๋ผ. >>> symbols = symbols + 'GOOG' >>> symbols 'AAPL, IBM, MSFT, YHOO, SCOGOOG' >>> ์•„์ฐจ! ์ด๊ฑธ ์›ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. symbols ๋ณ€์ˆ˜์— 'AAPL, IBM, MSFT, YHOO, SCO, GOOG'๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ์ˆ˜์ •ํ•ด ๋ณด์ž. >>> symbols = ? >>> symbols 'AAPL, IBM, MSFT, YHOO, SCO, GOOG' >>> ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์— 'HPQ'๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด์ž. >>> symbols = ? >>> symbols 'HPQ, AAPL, IBM, MSFT, YHOO, SCO, GOOG' >>> ๋ฌธ์ž์—ด์ด ์ฝ๊ธฐ ์ „์šฉ์ด๋ผ๊ณ  ํ–ˆ๋Š”๋ฐ, ์ด ์˜ˆ์—์„œ๋Š” ์›๋ž˜์˜ ๋ฌธ์ž์—ด์„ ์ˆ˜์ •ํ•ด ๊ทœ์น™์„ ์œ„๋ฐ˜ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์€ ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋งค๋ฒˆ ์ƒ์„ฑํ•œ๋‹ค. ๋ณ€์ˆ˜๋ช… ์‹ฌ๋ฒŒ(symbol)์ด ์žฌํ• ๋‹น๋˜๋ฉด, ๊ทธ๊ฒƒ์€ ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๋ฌธ์ž์—ด์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ž์—ด์€ ์‚ญ์ œ๋œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 1.15: ๋ฉค๋ฒ„์‹ญ ํ…Œ์ŠคํŒ…(๋ถ€๋ถ„ ๋ฌธ์ž์—ด ํ…Œ์ŠคํŒ…) in ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ํ™•์ธํ•ด ๋ณด์ž. ์ƒํ˜ธ์ž‘์šฉ์ ์ธ ํ”„๋กฌํ”„ํŠธ์—์„œ ๋‹ค์Œ ์—ฐ์‚ฐ์„ ํ•ด ๋ณด๋ผ. >>> 'IBM' in symbols >>> 'AA' in symbols True >>> 'CAT' in symbols >>> 'AA'๋ฅผ ํ™•์ธํ•  ๋•Œ ์™œ True๊ฐ€ ๋ฐ˜ํ™˜๋ ๊นŒ? ์—ฐ์Šต ๋ฌธ์ œ 1.16: ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ ํŒŒ์ด์ฌ ํ”„๋กฌํ”„ํŠธ์—์„œ ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์‹œํ—˜ ์‚ผ์•„ ์‚ฌ์šฉํ•ด ๋ณด์ž. >>> symbols.lower() >>> symbols >>> ๋ฌธ์ž์—ด์€ ํ•ญ์ƒ ์ฝ๊ธฐ ์ „์šฉ์ž„์„ ๋ช…์‹ฌํ•˜์ž. ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๋ณ€์ˆ˜์— ๋„ฃ์–ด๋ผ. >>> lowersyms = symbols.lower() >>> ๋ช‡ ๊ฐ€์ง€ ์—ฐ์‚ฐ์„ ๋” ํ•ด ๋ณด์ž. >>> symbols.find('MSFT') >>> symbols[13:17] >>> symbols = symbols.replace('SCO','DOA') >>> symbols >>> name = ' IBM \n' >>> name = name.strip() # ๋‘˜๋Ÿฌ์‹ธ๋Š” ๊ณต๋ฐฑ์„ ์ œ๊ฑฐ >>> name >>> ์—ฐ์Šต ๋ฌธ์ œ 1.17: f ๋ฌธ์ž์—ด(f-strings) ๋ณ€์ˆซ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ f ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ: >>> name = 'IBM' >>> shares = 100 >>> price = 91.1 >>> f'{shares} shares of {name} at ${price:0.2f}' '100 shares of IBM at $91.10' >>> ์—ฐ์Šต ๋ฌธ์ œ 1.10์˜ mortgage.py ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•ด, f ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•ด ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๊ฒŒ ํ•ด ๋ณด์ž. ์ถœ๋ ฅ์„ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ •๋ ฌํ•ด ๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 1.18: ์ •๊ทœ ํ‘œํ˜„์‹(Regular Expressions) ๊ธฐ๋ณธ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์€ ๊ณ ๊ธ‰ ํŒจํ„ด ๋งค์นญ์„ ์ง€์›ํ•˜์ง€ ์•Š์ง€๋งŒ, ํŒŒ์ด์ฌ์˜ re ๋ชจ๋“ˆ์„ ๊ฐ€์ง€๊ณ  ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์€ ๊ทธ ์ž์ฒด๋กœ ํฐ ์ฃผ์ œ์ด์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ๋“ค๊ฒ ๋‹ค. >>> text = 'Today is 3/27/2018. Tomorrow is 3/28/2018.' >>> # ๋‚ ์งœ๊ฐ€ ์žˆ๋Š” ๊ณณ์„ ๋ชจ๋‘ ์ฐพ์Œ >>> import re >>> re.findall(r'\d+/\d+/\d+', text) ['3/27/2018', '3/28/2018'] >>> # ๋‚ ์งœ๊ฐ€ ์žˆ๋Š” ๊ณณ์„ ๋ชจ๋‘ ์ฐพ์•„ text๋กœ ๊ต์ฒด >>> re.sub(r'(\d+)/(\d+)/(\d+)', r'\3-\1-\2', text) 'Today is 2018-3-27. Tomorrow is 2018-3-28.' >>> re ๋ชจ๋“ˆ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋Š” ๊ณต์‹ ๋ฌธ์„œ https://docs.python.org/library/re.html์„ ์ฐธ์กฐํ•˜๋ผ. ๋ถ€์—ฐ ์„ค๋ช… ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์‹คํ—˜์„ ํ•˜๋‹ค ๋ณด๋ฉด, ๋‹ค๋ฅธ ๊ฐ์ฒด(object)๊ฐ€ ์ง€์›ํ•˜๋Š” ์—ฐ์‚ฐ์— ๋Œ€ํ•ด ๋” ์•Œ๊ณ  ์‹ถ์„ ๊ฒƒ์ด๋‹ค. ๊ฐ€๋ น, ๋ฌธ์ž์—ด์— ์–ด๋–ค ์—ฐ์‚ฐ์ด ์žˆ๋Š”์ง€ ์–ด๋–ป๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์„๊นŒ? ํŒŒ์ด์ฌ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ, ํƒญ ์ž๋™์™„์„ฑ์„ ํ†ตํ•ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด ๋ณด๋ผ. >>> s = 'hello world' >>> s.<tab key> >>> ํƒญ์„ ๋ˆŒ๋ €์„ ๋•Œ ๋ฐ˜์‘์ด ์—†๋‹ค๋ฉด, ๋นŒํŠธ์ธ dir() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ: >>> s = 'hello' >>> dir(s) ['__add__', '__class__', '__contains__', ..., 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] >>> dir()์€ (.) ์ดํ›„์— ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋Š” ์ „์ฒด ์—ฐ์‚ฐ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ํŠน์ • ์—ฐ์‚ฐ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ์•Œ๊ณ  ์‹ถ์œผ๋ฉด help() ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜๋ผ. >>> help(s.upper) Help on built-in function upper: upper(...) S.upper() -> string Return a copy of the string S converted to uppercase. >>> 1.5 ๋ฆฌ์ŠคํŠธ ์ด ์„น์…˜์€ ํŒŒ์ด์ฌ์—์„œ ๊ฐ’์˜ ์ˆœ์„œ๊ฐ€ ์œ ์ง€๋˜๋Š” ์ปฌ๋ ‰์…˜(ordered collection)์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธฐ๋ณธ ํƒ€์ž…์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฆฌ์ŠคํŠธ ๋ฆฌํ„ฐ๋Ÿด์„ ์ •์˜ํ•œ๋‹ค. names = [ 'Elwood', 'Jake', 'Curtis' ] nums = [ 39, 38, 42, 65, 111] ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, split() ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. >>> line = 'GOOG, 100,490.10' >>> row = line.split(',') >>> row ['GOOG', '100', '490.10'] >>> ๋ฆฌ์ŠคํŠธ ์—ฐ์‚ฐ ์–ด๋–ค ํƒ€์ž…์ด๋“  ๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ(item)์ด ๋  ์ˆ˜ ์žˆ๋‹ค. append()๋ฅผ ์‚ฌ์šฉํ•ด ์ƒˆ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. names.append('Murphy') # ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ names.insert(2, 'Aretha') # ์ค‘๊ฐ„์— ์‚ฝ์ž… ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์–ด๋ถ™์ด๋ ค๋ฉด +๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. s = [1, 2, 3] t = ['a', 'b'] s + t # [1, 2, 3, 'a', 'b'] ๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์Šค์—๋Š” ์ •์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. names = [ 'Elwood', 'Jake', 'Curtis' ] names[0] # 'Elwood' names[1] # 'Jake' names[2] # 'Curtis' ์Œ์ˆ˜ ์ธ๋ฑ์Šค๋Š” ๋’ค์—์„œ๋ถ€ํ„ฐ ์„ผ๋‹ค. names[-1] # 'Curtis' ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. names[1] = 'Joliet Jake' names # [ 'Elwood', 'Joliet Jake', 'Curtis' ] ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด. names = ['Elwood','Jake','Curtis'] len(names) # 3 ๋ฉค๋ฒ„์‹ญ ํ…Œ์ŠคํŠธ(in, not in). 'Elwood' in names # True 'Britney' not in names # True ๋ณต์ œ(s * n). s = [1, 2, 3] s * 3 # [1, 2, 3, 1, 2, 3, 1, 2, 3] ๋ฆฌ์ŠคํŠธ ์ดํ„ฐ๋ ˆ์ด์…˜(Iteration)๊ณผ ๊ฒ€์ƒ‰ for๋ฅผ ์‚ฌ์šฉํ•ด ๋ฆฌ์ŠคํŠธ ๋‚ด์šฉ์„ ํ›‘๋Š”๋‹ค. for name in names: # name์„ ์‚ฌ์šฉ # ์˜ˆ: print(name) ... ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ foreach ๋ฌธ๊ณผ ๋น„์Šทํ•˜๋‹ค. ๋ฌด์—‡์˜ ์œ„์น˜๋ฅผ ์žฌ๋นจ๋ฆฌ ์ฐพ์œผ๋ ค๋ฉด index()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. names = ['Elwood','Jake','Curtis'] names.index('Curtis') # 2 ์›์†Œ(element)๊ฐ€ ํ•œ ๋ฒˆ ์ด์ƒ ๋‚˜ํƒ€๋‚˜๋ฉด, index()๋Š” ํ•ด๋‹น ์›์†Œ๊ฐ€ ์ฒ˜์Œ ๋‚˜ํƒ€๋‚œ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์›์†Œ๋ฅผ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด ValueError ์˜ˆ์™ธ๋ฅผ ์ผ์œผํ‚จ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ œ๊ฑฐ ํŠน์ • ๊ฐ’ ๋˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ๊ฐ–๋Š” ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. # ๊ฐ’์„ ์‚ฌ์šฉ names.remove('Curtis') # ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉ del names[1] ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•˜๋”๋ผ๋„ ๋นˆ์ž๋ฆฌ๊ฐ€ ์ƒ๊ธฐ์ง€๋Š” ์•Š๋Š”๋‹ค. ๋‹ค์Œ ํ•ญ๋ชฉ์ด ๋”ฐ๋ผ๋ถ™๋Š”๋‹ค. ์›์†Œ๊ฐ€ ํ•œ ๋ฒˆ ์ด์ƒ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒฝ์šฐ, remove()๋Š” ์ฒซ ๋ฒˆ์งธ ๋‚˜ํƒ€๋‚œ ๊ฒƒ๋งŒ ์ œ๊ฑฐํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ "์ œ์ž๋ฆฌ์—์„œ(in-place)" ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค. s = [10, 1, 7, 3] s.sort() # [1, 3, 7, 10] # ์—ญ์ˆœ s = [10, 1, 7, 3] s.sort(reverse=True) # [10, 7, 3, 1] # ์ˆœ์„œ๊ฐ€ ์œ ์ง€๋˜๋Š” ์ž๋ฃŒํ˜•์— ๋™์ผํ•˜๊ฒŒ ์ ์šฉ๋œ๋‹ค s = ['foo', 'bar', 'spam'] s.sort() # ['bar', 'foo', 'spam'] ์ƒˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์œผ๋ฉด sorted()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. t = sorted(s) # s๋Š” ๋ฐ”๋€Œ์ง€ ์•Š์œผ๋ฉฐ, t๋Š” ์ •๋ ฌ๋œ ๊ฐ’์„ ๊ฐ€์ง ๋ฆฌ์ŠคํŠธ์™€ ์ˆ˜ํ•™ ์ฃผ์˜: ๋ฆฌ์ŠคํŠธ๋Š” ์ˆ˜ํ•™ ์—ฐ์‚ฐ์„ ์œ„ํ•ด ์„ค๊ณ„ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. >>> nums = [1, 2, 3, 4, 5] >>> nums * 2 [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] >>> nums + [10, 11, 12, 13, 14] [1, 2, 3, 4, 5, 10, 11, 12, 13, 14] ํŠนํžˆ ๋ฆฌ์ŠคํŠธ๋Š” ๋งคํŠธ๋žฉ(MATLAB), ์˜ฅํƒ€๋ธŒ(Octave), R ๋“ฑ์˜ ๋ฒกํ„ฐ๋‚˜ ํ–‰๋ ฌ์„ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿฐ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€๊ฐ€ ๋”ฐ๋กœ ์žˆ๋‹ค.(์˜ˆ: numpy) ์—ฐ์Šต ๋ฌธ์ œ ์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ๋Š” ํŒŒ์ด์ฌ์˜ ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์„ ์‹คํ—˜ํ•œ๋‹ค. ์ง€๋‚œ ์„น์…˜์—์„œ ์ฃผ์‹ ์‹ฌ๋ฒŒ์„ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋‹ค๋ค˜๋‹ค. >>> symbols = 'HPQ, AAPL, IBM, MSFT, YHOO, DOA, GOOG' ๋ฌธ์ž์—ด์˜ split() ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•ด ์ด๋ฆ„์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค. >>> symlist = symbols.split(',') ์—ฐ์Šต ๋ฌธ์ œ 1.19: ๋ฆฌ์ŠคํŠธ ์›์†Œ๋ฅผ ์ถ”์ถœํ•ด ์žฌํ• ๋‹น ์กฐํšŒ๋ฅผ ํ•ด ๋ณด์ž. >>> symlist[0] 'HPQ' >>> symlist[1] 'AAPL' >>> symlist[-1] 'GOOG' >>> symlist[-2] 'DOA' >>> ๊ฐ’์„ ์žฌํ• ๋‹นํ•ด ๋ณด์ž. >>> symlist[2] = 'AIG' >>> symlist ['HPQ', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'DOA', 'GOOG'] >>> ์Šฌ๋ผ์ด์Šค๋ฅผ ํ•ด ๋ณด์ž. >>> symlist[0:3] ['HPQ', 'AAPL', 'AIG'] >>> symlist[-2:] ['DOA', 'GOOG'] >>> ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•ด ๋ณด์ž. >>> mysyms = [] >>> mysyms.append('GOOG') >>> mysyms ['GOOG'] ๋ฆฌ์ŠคํŠธ์˜ ์ผ๋ถ€๋ฅผ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ์— ์žฌํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: >>> symlist[-2:] = mysyms >>> symlist ['HPQ', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'GOOG'] >>> ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์™ผ์ชฝ์˜ ๋ฆฌ์ŠคํŠธ(symlist)๋Š” ์˜ค๋ฅธ์ชฝ ๋ฆฌ์ŠคํŠธ(mysyms)์— ๋งž์ถฐ ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ•œ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ, symlist์˜ ๋งˆ์ง€๋ง‰ ๋‘ ๊ฐœ ํ•ญ๋ชฉ์ด mysyms์˜ ํ•œ ๊ฐœ ํ•ญ๋ชฉ์œผ๋กœ ๊ต์ฒด๋œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 1.20: ๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ์„ ๋ฃจํ•‘ for ๋ฃจํ”„๋Š” ๋ฆฌ์ŠคํŠธ ๊ฐ™์€ ์‹œํ€€์Šค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฃจํ•‘ ํ•œ๋‹ค. ๋‹ค์Œ ๋ฃจํ”„๋ฅผ ํƒ€์ดํ•‘ํ•˜๊ณ  ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ์ง€์ผœ๋ณด๋ผ. >>> for s in symlist: print('s =', s) # ์ถœ๋ ฅ์„ ์‚ดํŽด๋ณด๋ผ ์—ฐ์Šต ๋ฌธ์ œ 1.21: ๋ฉค๋ฒ„์‹ญ ํ…Œ์ŠคํŠธ in์ด๋‚˜ not in ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด, ์‹ฌ๋ฒŒ ๋ฆฌ์ŠคํŠธ์— 'AIG','AA', 'CAT'๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ผ. >>> # 'AIG'๊ฐ€ `symlist`์— ์žˆ๋Š”๊ฐ€?(in) True >>> # 'AA'๊ฐ€ `symlist`์— ์žˆ๋Š”๊ฐ€?(in) False >>> # 'CAT'๊ฐ€ `symlist` ์—†๋Š”๊ฐ€?(not in) True >>> ์—ฐ์Šต ๋ฌธ์ œ 1.22: ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€, ์‚ฝ์ž…, ์‚ญ์ œ append() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์‹ฌ๋ฒŒ 'RHT'๋ฅผ symlist์˜ ๋์— ์ถ”๊ฐ€ํ•˜๋ผ. >>> # 'RHT'๋ฅผ ์ถ”๊ฐ€ >>> symlist ['HPQ', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'GOOG', 'RHT'] >>> insert() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์‹ฌ๋ฒŒ 'AA'๋ฅผ ๋ฆฌ์ŠคํŠธ์˜ ๋‘ ๋ฒˆ์งธ ํ•ญ๋ชฉ์œผ๋กœ ์‚ฝ์ž…ํ•˜๋ผ. >>> # 'AA'๋ฅผ ๋ฆฌ์ŠคํŠธ์˜ ๋‘ ๋ฒˆ์งธ ํ•ญ๋ชฉ์œผ๋กœ >>> symlist ['HPQ', 'AA', 'AAPL', 'AIG', 'MSFT', 'YHOO', 'GOOG', 'RHT'] >>> remove() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด 'MSFT'๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ญ์ œํ•˜๋ผ. >>> # 'MSFT'๋ฅผ ์‚ญ์ œ >>> symlist ['HPQ', 'AA', 'AAPL', 'AIG', 'YHOO', 'GOOG', 'RHT'] >>> ๋ฆฌ์ŠคํŠธ ๋์— 'YHOO'๋ฅผ ํ•œ ๊ฐœ ๋” ์ถ”๊ฐ€ํ•˜๋ผ. ์ฐธ๊ณ : ๋ฆฌ์ŠคํŠธ์—๋Š” ์ค‘๋ณต๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋„ ์ „ํ˜€ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. >>> # 'YHOO'๋ฅผ ์ถ”๊ฐ€ >>> symlist ['HPQ', 'AA', 'AAPL', 'AIG', 'YHOO', 'GOOG', 'RHT', 'YHOO'] >>> ๋ฆฌ์ŠคํŠธ์—์„œ 'YHOO'์˜ ์œ„์น˜๋ฅผ ๋นจ๋ฆฌ ์ฐพ์œผ๋ ค๋ฉด index() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. >>> # 'YHOO'์˜ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค ์ฐพ๊ธฐ >>> symlist[4] 'YHOO' >>> ๋ฆฌ์ŠคํŠธ์— 'YHOO'๊ฐ€ ๋ช‡ ๋ฒˆ ๋‚˜์˜ค๋Š”์ง€ ์„ผ๋‹ค. >>> symlist.count('YHOO') >>> ์ฒซ ๋ฒˆ์งธ ๋‚˜ํƒ€๋‚˜๋Š” 'YHOO'๋ฅผ ์‚ญ์ œํ•œ๋‹ค. >>> # ์ฒซ ๋ฒˆ์งธ ๋‚˜์˜ค๋Š” 'YHOO'๋ฅผ ์‚ญ์ œ >>> symlist ['HPQ', 'AA', 'AAPL', 'AIG', 'GOOG', 'RHT', 'YHOO'] >>> ๋ชจ๋“  ํ•ญ๋ชฉ์„ ์ฐพ๊ฑฐ๋‚˜, ๋ชจ๋“  ํ•ญ๋ชฉ์„<NAME>๋Š” ๋ฉ”์„œ๋“œ๋Š” ์—†๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ์šฐ์•„ํ•œ ๋ฐฉ๋ฒ•์„ ์„น์…˜ 2์—์„œ ์•Œ์•„๋ณธ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 1.23: ์ •๋ ฌ(Sorting) ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์€๊ฐ€? sort() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. ํ•œ๋ฒˆ ์‚ฌ์šฉํ•ด ๋ณด์ž. >>> symlist.sort() >>> symlist ['AA', 'AAPL', 'AIG', 'GOOG', 'HPQ', 'RHT', 'YHOO'] >>> ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด? ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด ๋ณด๋ผ. >>> symlist.sort(reverse=True) >>> symlist ['YHOO', 'RHT', 'HPQ', 'GOOG', 'AIG', 'AAPL', 'AA'] >>> ์ฐธ๊ณ : ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜๋ฉด ๋‚ด์šฉ์ด '์ œ์ž๋ฆฌ์—์„œ(in-place)' ์ˆ˜์ •๋œ๋‹ค. ๊ทธ ๋ง์€ ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ์€ ์ด๋ฆฌ์ €๋ฆฌ ์˜ฎ๊ฒจ์ง€์ง€๋งŒ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค๋Š” ๋œป์ด๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 1.24: ๋ชจ๋‘ ํ•ฉ์น˜๊ธฐ ๋ฌธ์ž์—ด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•ญ๋ชฉ๋“ค์„ ํ•ฉ์ณ์„œ(join) ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค๊ณ  ์‹ถ์€๊ฐ€? ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌธ์ž์—ด์˜ join() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ(์ฐธ๊ณ : ์ฒ˜์Œ ๋ณด๋ฉด ์ข€ ์›ƒ๊ธฐ๋‹ค). >>> a = ','.join(symlist) >>> a 'YHOO, RHT, HPQ, GOOG, AIG, AAPL, AA' >>> b = ':'.join(symlist) >>> b 'YHOO:RHT:HPQ:GOOG:AIG:AAPL:AA' >>> c = ''.join(symlist) >>> c 'YHOORHTHPQGOOGAIGAAPLAA' >>> ์—ฐ์Šต ๋ฌธ์ œ 1.25: ์˜จ๊ฐ– ๊ฒƒ๋“ค์˜ ๋ฆฌ์ŠคํŠธ ๋ฆฌ์ŠคํŠธ๋Š” ์–ด๋–ค ์ข…๋ฅ˜์˜ ๊ฐ์ฒด๋“  ๋‹ด์„ ์ˆ˜ ์žˆ๊ณ , ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ด์„ ์ˆ˜๋„ ์žˆ๋‹ค(์˜ˆ: ์ค‘์ฒฉ๋œ ๋ฆฌ์ŠคํŠธ). ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹œ๋„ํ•ด ๋ณด๋ผ. >>> nums = [101, 102, 103] >>> items = ['spam', symlist, nums] >>> items ['spam', ['YHOO', 'RHT', 'HPQ', 'GOOG', 'AIG', 'AAPL', 'AA'], [101, 102, 103]] ์ถœ๋ ฅ์„ ์ž˜ ๋“ค์—ฌ๋‹ค๋ณด๋ผ. items๋Š” ์„ธ ๊ฐœ์˜ ์›์†Œ๋ฅผ ๊ฐ€์ง„ ๋ฆฌ์ŠคํŠธ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋Š” ๋ฌธ์ž์—ด์ด์ง€๋งŒ, ๋‚˜๋จธ์ง€ ๋‘ ๊ฐœ์˜ ์›์†Œ๋Š” ๋ฆฌ์ŠคํŠธ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ธ๋ฑ์‹ฑ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ์ค‘์ฒฉ๋œ ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค. >>> items[0] 'spam' >>> items[0][0] 's' >>> items[1] ['YHOO', 'RHT', 'HPQ', 'GOOG', 'AIG', 'AAPL', 'AA'] >>> items[1][1] 'RHT' >>> items[1][1][2] 'T' >>> items[2] [101, 102, 103] >>> items[2][1] 102 >>> ๋งค์šฐ ๋ณต์žกํ•œ ๋ฆฌ์ŠคํŠธ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๊ธฐ์ˆ ์ ์œผ๋กœ๋Š” ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋‹จ์ˆœํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ์€ ๋ชจ๋‘ ๊ฐ™์€ ์ข…๋ฅ˜์˜ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ˆซ์ž๋งŒ์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๊ฑฐ๋‚˜, ํ…์ŠคํŠธ ๋ฌธ์ž์—ด๋งŒ์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ์— ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์„ž๋‹ค ๋ณด๋ฉด ๋จธ๋ฆฌ๊ฐ€ ํ„ฐ์งˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋  ์ˆ˜ ์žˆ์œผ๋ฉด ํ”ผํ•˜์ž. 1.6 ํŒŒ์ผ ๊ด€๋ฆฌ ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ์€ ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ์ฝ์–ด์•ผ ํ•œ๋‹ค. ์ด ์„น์…˜์€ ํŒŒ์ผ ์•ก์„ธ์Šค๋ฅผ ๋…ผ์˜ํ•œ๋‹ค. ํŒŒ์ผ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ ํŒŒ์ผ์„ ์—ฐ๋‹ค. f = open('foo.txt', 'rt') # ์ฝ๊ธฐ๋ฅผ ์œ„ํ•ด ์—ด๊ธฐ(ํ…์ŠคํŠธ) g = open('bar.txt', 'wt') # ์“ฐ๊ธฐ๋ฅผ ์œ„ํ•ด ์—ด๊ธฐ(ํ…์ŠคํŠธ) ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š”๋‹ค. data = f.read() # 'maxbytes' ๋ฐ”์ดํŠธ๊นŒ์ง€๋งŒ ์ฝ์Œ data = f.read([maxbytes]) ํ…์ŠคํŠธ๋ฅผ ๊ธฐ๋กํ•œ๋‹ค. g.write('some text') ๋งˆ์ณค์œผ๋ฉด ํŒŒ์ผ์„ ๋‹ซ๋Š”๋‹ค. f.close() g.close() ํŒŒ์ผ์„ ์—ด์—ˆ์œผ๋ฉด ์ œ๋Œ€๋กœ ๋‹ซ์•„์•ผ ํ•˜๋Š”๋ฐ, ์ด ๋‹จ๊ณ„๋ฅผ ์žŠ์–ด๋ฒ„๋ฆฌ๊ธฐ ์‰ฝ๋‹ค. ๋”ฐ๋ผ์„œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด with ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค. with open(filename, 'rt') as file: # `file` ํŒŒ์ผ์„ ์‚ฌ์šฉ ... # ๋ช…์‹œ์ ์œผ๋กœ ๋‹ซ์ง€ ์•Š์•„๋„ ๋œ๋‹ค ...๋ฌธ์žฅ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋“ค์—ฌ ์“ด ์ฝ”๋“œ ๋ธ”๋ก์—์„œ ๋ฒ—์–ด๋‚  ๋•Œ ํŒŒ์ผ์ด ์ž๋™์œผ๋กœ ๋‹ซํžŒ๋‹ค. ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ• ํŒŒ์ผ ์ „์ฒด๋ฅผ ํ•œ ๋ฒˆ์— ์ฝ์–ด ๋ฌธ์ž์—ด๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. with open('foo.txt', 'rt') as file: data = file.read() # `data`๋Š” `foo.txt`์˜ ํ…์ŠคํŠธ ์ „์ฒด๋กœ ๋œ ๋ฌธ์ž์—ด์ด๋‹ค ํŒŒ์ผ์„ ํ•œ ํ–‰์”ฉ ์ฝ์–ด ๋‚ด๋ ค๊ฐ€๊ธฐ. with open(filename, 'rt') as file: for line in file: # ํ–‰์„ ์ฒ˜๋ฆฌ ํŒŒ์ผ์— ์“ฐ๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ• ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋กํ•œ๋‹ค. with open('outfile', 'wt') as out: out.write('Hello World\n') ... print ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ์„ ์žฌ์ง€์ •(redirect) ํ•œ๋‹ค. with open('outfile', 'wt') as out: print('Hello World', file=out) ... ์—ฐ์Šต ๋ฌธ์ œ ์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ๋Š” Data/portfolio.csv ํŒŒ์ผ์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ํŒŒ์ผ์€ ์ฃผ์‹์˜ ํฌํŠธํด๋ฆฌ์˜ค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ–‰๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค. practical-python/Work/ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ํ™•์‹คํ•˜์ง€ ์•Š์œผ๋ฉด ํŒŒ์ด์ฌ์˜ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ(current working directory)๋ฅผ ํ™•์ธํ•ด ๋ณด์ž. >>> import os >>> os.getcwd() '/Users/beazley/Desktop/practical-python/Work' # ์ถœ๋ ฅ์€ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค >>> ์—ฐ์Šต ๋ฌธ์ œ 1.26: ํŒŒ์ผ ๊ธฐ์ดˆ ๋จผ์ €, ์ „์ฒด ํŒŒ์ผ์„ ํ•œ ๋ฒˆ์— ์ฝ์–ด ํฐ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์–ด ๋ณด์ž. >>> with open('Data/portfolio.csv', 'rt') as f: data = f.read() >>> data 'name, shares, price "AA",100,32.20 "IBM",50,91.10 "CAT",150,83.44 "MSFT",200,51.23 "GE",95,40.37 "MSFT",50,65.10\n"IBM",100,70.44\n' >>> print(data) name, shares, price "AA",100,32.20 "IBM",50,91.10 "CAT",150,83.44 "MSFT",200,51.23 "GE",95,40.37 "MSFT",50,65.10 "IBM",100,70.44 >>> ์œ„์˜ ์˜ˆ์—์„œ ํŒŒ์ด์ฌ์— ๋‘ ๊ฐ€์ง€ ์ถœ๋ ฅ ๋ชจ๋“œ๊ฐ€ ์žˆ์Œ์— ์œ ์˜ํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ชจ๋“œ์—์„œ๋Š” ํ”„๋กฌํ”„ํŠธ์— data๋ฅผ ํƒ€์ดํ•‘ํ•˜๋ฉด ๋”ฐ์˜ดํ‘œ์™€ ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋œ ์›์‹œ ๋ฌธ์ž์—ด์ด ๋‚˜ํƒ€๋‚œ๋‹ค. print(data)๋ฅผ ํƒ€์ดํ•‘ํ•˜๋ฉด ์‹ค์ œ ํฌ๋งคํŒ…๋œ ๋ฌธ์ž์—ด ์ถœ๋ ฅ์ด ๋‚˜ํƒ€๋‚œ๋‹ค. ํŒŒ์ผ์„ ํ•œ ๋ฒˆ์— ์ฝ๋Š” ๊ฒƒ์ด ๊ฐ„๋‹จํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ ๋•Œ์— ๋”ฐ๋ผ์„œ๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค. ํŠนํžˆ ํŒŒ์ผ์ด ์•„์ฃผ ํฌ๊ฑฐ๋‚˜, ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ๊ฐ€ ๊ทธ๋ ‡๋‹ค. ํŒŒ์ผ์„ ํ–‰ ๋‹จ์œ„๋กœ ์ฝ์œผ๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด for ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. >>> with open('Data/portfolio.csv', 'rt') as f: for line in f: print(line, end='') name, shares, price "AA",100,32.20 "IBM",50,91.10 ... >>> ์ฝ”๋“œ๋ฅผ ์œ„์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด, ํ–‰์„ ์ฐจ๋ก€๋กœ ์ฝ๋‹ค๊ฐ€ ํŒŒ์ผ์˜ ๋์— ๋„๋‹ฌํ•˜๋ฉด ๋ฃจํ”„๊ฐ€ ์ข…๋ฃŒ๋œ๋‹ค. ํŠน์ •ํ•œ ์ƒํ™ฉ์—์„œ, ํ…์ŠคํŠธ์˜ ๋‹จ ์ผํ–‰์„ ์ฝ๊ฑฐ๋‚˜ ๊ฑด๋„ˆ๋›ธ ์ˆ˜ ์žˆ๋‹ค(์˜ˆ: ์ฒซ ํ–‰์˜ ์นผ๋Ÿผ ํ—ค๋”๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  ์‹ถ์„ ๊ฒƒ์ด๋‹ค). >>> f = open('Data/portfolio.csv', 'rt') >>> headers = next(f) >>> headers 'name, shares, price ' >>> for line in f: print(line, end='') "AA",100,32.20 "IBM",50,91.10 ... >>> f.close() >>> next()๋Š” ํŒŒ์ผ์—์„œ ๋‹ค์Œ ๋ฒˆ ํ–‰์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๊ฒƒ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ ํ–‰์„ ์—ฐ์†์ ์œผ๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ, ์•Œ๋‹ค์‹œํ”ผ for ๋ฃจํ”„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„ ๋•Œ next()๋ฅผ ์ด๋ฏธ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ๋‹จ ์ผํ–‰์„ ๋ช…์‹œ์ ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ฑฐ๋‚˜ ์ฝ์„ ๋•Œ ์™ธ์—๋Š” ์ง์ ‘์ ์œผ๋กœ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค. ํŒŒ์ผ์˜ ํ–‰์„ ์ฝ์—ˆ์œผ๋ฉด ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: >>> f = open('Data/portfolio.csv', 'rt') >>> headers = next(f).split(',') >>> headers ['name', 'shares', 'price\n'] >>> for line in f: row = line.split(',') print(row) ['"AA"', '100', '32.20\n'] ['"IBM"', '50', '91.10\n'] ... >>> f.close() ์ฐธ๊ณ : ์ด ์˜ˆ์—์„œ๋Š” with ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ f.close()๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ–ˆ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 1.27: ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ฝ๊ธฐ ํŒŒ์ผ์„ ์ฝ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•˜์œผ๋‹ˆ ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์ž. portfolio.csv์˜ ๊ฐ ์นผ๋Ÿผ์€ ๋ณด์œ  ์ข…๋ชฉ์˜ ์ด๋ฆ„, ์ฃผ์‹ ์ˆ˜, ๋งค์ˆ˜ ๊ฐ€๊ฒฉ์— ํ•ด๋‹นํ•œ๋‹ค. ์ด ํŒŒ์ผ์„ ์—ด์–ด ์ „์ฒด ํ–‰์„ ์ฝ์€ ๋’ค, ํฌํŠธํด๋ฆฌ์˜ค์˜ ์ „์ฒด ์ฃผ์‹์„ ๋งค์ˆ˜ํ•˜๋Š” ๋ฐ ๋“œ๋Š” ๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜๋Š” pcost.py ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•œ๋‹ค. ํžŒํŠธ: ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด int(s)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋ฌธ์ž์—ด์„ ๋ถ€๋™์†Œ์ˆ˜์ ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด float(s)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. Total cost 44671.15 ์—ฐ์Šต ๋ฌธ์ œ 1.28: ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ "ํŒŒ์ผ" ์ผ๋ฐ˜ ํ…์ŠคํŠธ ํŒŒ์ผ์ด ์•„๋‹Œ, gzip ์••์ถ•๋œ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ์ฝ๊ณ  ์‹ถ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? ๋นŒํŠธ์ธ open() ํ•จ์ˆ˜๋Š” ์—ฌ๊ธฐ์„œ ๋„์›€์ด ๋˜์ง€ ์•Š์ง€๋งŒ, ํŒŒ์ด์ฌ์—๋Š” gzip ์••์ถ•๋œ ํŒŒ์ผ์„ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” gzip ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ์ด ์žˆ๋‹ค. ํ•œ๋ฒˆ ํ•ด ๋ณด์ž. >>> import gzip >>> with gzip.open('Data/portfolio.csv.gz', 'rt') as f: for line in f: print(line, end='') ... ์ถœ๋ ฅ์„ ์‚ดํŽด๋ณด๋ผ ... >>> ์ฐธ๊ณ : ํŒŒ์ผ ๋ชจ๋“œ๋ฅผ 'rt'๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ์ด๊ฒƒ์„ ์žŠ์œผ๋ฉด ์ผ๋ฐ˜ ํ…์ŠคํŠธ ์—ด์ด ์•„๋‹Œ ๋ฐ”์ดํŠธ ์—ด์„ ์–ป๊ฒŒ ๋œ๋‹ค. ๋ถ€์—ฐ ์„ค๋ช…: ํŒ๋‹ค์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜์ง€ ์•Š๋‚˜? ๋ฐ์ดํ„ฐ ๊ณผํ•™์ž๋ผ๋ฉด CSV ํŒŒ์ผ์„ ์ฝ๋Š” ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ํŒ๋‹ค์Šค(Pandas, https://pandas.pydata.org) ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค๊ณ  ์ง€์ ํ•  ๊ฒƒ์ด๋‹ค. ๋งž๋Š” ๋ง์”€์ด๋‹ค. ์ž˜ ์ž‘๋™ํ•œ๋‹ค. ํ•˜์ง€๋งŒ, ์ด ์ฝ”์Šค๋Š” ํŒ๋‹ค์Šค๋ฅผ ๋ฐฐ์šฐ๋Š” ์ฝ”์Šค๊ฐ€ ์•„๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์ฝ๋Š” ๊ฒƒ์€ CSV ํŒŒ์ผ์ด๋ผ๋Š” ๊ตญ์†Œ์ ์ธ ๋ฌธ์ œ์— ๋น„ํ•ด ์ข€ ๋” ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ œ๋‹ค. ์—ฌ๊ธฐ์„œ CSV ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ์ฃผ๋œ ์ด์œ ๋Š” ๊ทธ๊ฒƒ์ด ์ž˜ ์•Œ๋ ค์ง„<NAME>์ด๊ณ  ์ง์ ‘ ๋‹ค๋ฃจ๊ธฐ ์‰ฌ์šด ํŽธ์ด๋ฏ€๋กœ, ๊ทธ๊ฒƒ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์„ ํ†ตํ•ด ํŒŒ์ด์ฌ์˜ ๊ธฐ๋Šฅ์„ ์†Œ๊ฐœํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ, ํŒ๋‹ค์Šค๋Š” ์ผํ„ฐ๋กœ ๋Œ์•„๊ฐ€๋ฉด ๋งˆ์Œ๊ป ์จ๋ผ. ์ด ์ฝ”์Šค์—์„œ๋Š” ํ‘œ์ค€ ํŒŒ์ด์ฌ ๊ธฐ๋Šฅ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค. 1.7 ํ•จ์ˆ˜ ํ”„๋กœ๊ทธ๋žจ์ด ์ปค์งˆ์ˆ˜๋ก ์กฐ์งํ™”๊ฐ€ ํ•„์š”ํ•ด์ง„๋‹ค. ์ด ์„น์…˜์€ ํ•จ์ˆ˜์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ์„ ๊ฐ„๋žตํžˆ ์†Œ๊ฐœํ•œ๋‹ค. ์˜ˆ์™ธ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๋ฒ•๋„ ์†Œ๊ฐœํ•œ๋‹ค. ์ปค์Šคํ…€ ํ•จ์ˆ˜ ์žฌ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์œ„ํ•ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. ํ•จ์ˆ˜ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. def sumcount(n): ''' ์ •์ˆ˜ 1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ ''' total = 0 while n > 0: total += n n -= 1 return total ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. a = sumcount(100) ํ•จ์ˆ˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์žฅ์œผ๋กœ ์ด๋ค„์ง„๋‹ค. ํ•จ์ˆ˜๊ฐ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด return ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ช…์‹œํ•ด์•ผ ํ•œ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ ํŒŒ์ด์ฌ์—๋Š” ํฐ ๊ทœ๋ชจ์˜ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(standard library)๊ฐ€ ์žˆ๋‹ค. import๋ฅผ ์‚ฌ์šฉํ•ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ์— ์•ก์„ธ์Šคํ•œ๋‹ค. ์˜ˆ: import math x = math.sqrt(10) import urllib.request u = urllib.request.urlopen('http://www.python.org/') data = u.read() ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๋ชจ๋“ˆ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‚˜์ค‘์— ๋‹ค๋ฃฌ๋‹ค. ์˜ค๋ฅ˜์™€ ์˜ˆ์™ธ ํ•จ์ˆ˜๋Š” ์˜ค๋ฅ˜๋ฅผ ์˜ˆ์™ธ๋กœ์„œ ๋ณด๊ณ ํ•œ๋‹ค. ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ•จ์ˆ˜์˜ ์‹คํ–‰์ด ์ค‘๋‹จ๋˜๋ฉฐ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์œผ๋ฉด ํ”„๋กœ๊ทธ๋žจ ์ „์ฒด๊ฐ€ ์ค‘๋‹จ๋  ์ˆ˜ ์žˆ๋‹ค. ํŒŒ์ด์ฌ REPL์—์„œ ๋‹ค์Œ์„ ์‹œ๋„ํ•ด ๋ณด๋ผ. >>> int('N/A') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'N/A' >>> ์‹คํŒจ๋ฅผ ์ผ์œผํ‚จ ๋‹ค๋ฅธ ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ๋ณด์—ฌ์ฃผ๋Š” ํŠธ๋ ˆ์ด์Šค ๋ฐฑ๊ณผ ํ•จ๊ป˜, ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚ฌ์œผ๋ฉฐ ์–ด๋””์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์„ค๋ช…ํ•˜๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค. ์ด ์ •๋ณด๋Š” ๋””๋ฒ„๊น…์— ๋„์›€์ด ๋œ๋‹ค. ์˜ˆ์™ธ๋ฅผ ๋ถ™์žก์•„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์˜ˆ์™ธ๋ฅผ ๋ถ™์žก์•„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด try - except ๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค. for line in f: fields = line.split() try: shares = int(fields[1]) except ValueError: print("Couldn't parse", line) ... ValueError๋ผ๋Š” ์ด๋ฆ„์ด ๋‹น์‹ ์ด ๋ถ™์žก์œผ๋ ค๋Š” ์˜ค๋ฅ˜์˜ ์ข…๋ฅ˜์™€ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค. ์–ด๋–ค ์ข…๋ฅ˜์˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š”์ง€, ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— ๋ฏธ๋ฆฌ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ๊ธฐ ์–ด๋ ค์šธ ๋•Œ๋„ ์žˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ์˜ˆ์ƒ์น˜ ๋ชปํ•˜๊ฒŒ ์ถฉ๋Œํ•œ ์ดํ›„์— ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ์ถ”๊ฐ€๋˜๊ณค ํ•œ๋‹ค(์˜ˆ: "์•„์ฐจ, ๊ทธ ์˜ค๋ฅ˜๋ฅผ ๋ถ™์žก๋Š” ๊ฑธ ๊นœ๋นกํ–ˆ๋„ค. ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์•ผ๊ฒ ์–ด!"). ์˜ˆ์™ธ๋ฅผ ์ผ์œผํ‚ค๊ธฐ ์˜ˆ์™ธ๋ฅผ ์ผ์œผํ‚ค๋ ค๋ฉด raise ๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค. raise RuntimeError('What a kerfuffle') ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์˜ˆ์™ธ ํŠธ๋ ˆ์ด์Šค ๋ฐฑ๊ณผ ํ•จ๊ป˜ ํ”„๋กœ๊ทธ๋žจ์ด ์ค‘๋‹จ๋œ๋‹ค. try-except ๋ธ”๋ก์œผ๋กœ ๋ถ™์žก์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ง์ด๋‹ค. % python3 foo.py Traceback (most recent call last): File "foo.py", line 21, in <module> raise RuntimeError("What a kerfuffle") RuntimeError: What a kerfuffle ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 1.29: ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ธฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹จ์ˆœํ•œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด์ž. >>> def greeting(name): 'Issues a greeting' print('Hello', name) >>> greeting('Guido') Hello Guido >>> greeting('Paula') Hello Paula >>> ํ•จ์ˆ˜์˜ ์ฒซ ๋ฌธ์žฅ์ด ๋ฌธ์ž์—ด์ด๋ฉด ๊ทธ๊ฒƒ์„ ๋ฌธ์„œ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. help(greeting) ๋ช…๋ น์„ ํƒ€์ดํ•‘ํ•ด ๋ฌธ์„œ๊ฐ€ ํ‘œ์‹œ๋˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 1.30: ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ•จ์ˆ˜๋กœ ํƒˆ๋ฐ”๊ฟˆ ์—ฐ์Šต ๋ฌธ์ œ 1.27์˜ pcost.py ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ๋‹ค๊ฐ€ portfolio_cost(filename) ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๋ผ. ์ด ํ•จ์ˆ˜๋Š” ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ , ํฌํŠธํด๋ฆฌ์˜ค์˜ ์ด๋น„์šฉ์„ ๋ถ€๋™์†Œ์ˆ˜์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜์ž. def portfolio_cost(filename): ... # ์—ฌ๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ... cost = portfolio_cost('Data/portfolio.csv') print('Total cost:', cost) ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด ์ด์ „๊ณผ ๋˜‘๊ฐ™์€ ์ถœ๋ ฅ์ด ๋‚˜ํƒ€๋‚˜์•ผ ํ•œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•œ ํ›„, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํƒ€์ดํ•‘ํ•ด ํ•จ์ˆ˜๋ฅผ ์ƒํ˜ธ์ž‘์šฉ์ ์œผ๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. bash $ python3 -i pcost.py ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ƒํ˜ธ์ž‘์šฉ ๋ชจ๋“œ์—์„œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. >>> portfolio_cost('Data/portfolio.csv') 44671.15 >>> ์ฝ”๋“œ๋ฅผ ์ƒํ˜ธ์ž‘์šฉ์ ์œผ๋กœ ์‹คํ—˜ํ•  ์ˆ˜ ์žˆ์–ด ํ…Œ์ŠคํŠธ์™€ ๋””๋ฒ„๊น…ํ•˜๊ธฐ ์ข‹๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 1.31: ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ํ•„๋“œ๊ฐ€ ๋ˆ„๋ฝ๋œ ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”๊ฐ€? >>> portfolio_cost('Data/missing.csv') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "pcost.py", line 11, in portfolio_cost nshares = int(fields[1]) ValueError: invalid literal for int() with base 10: '' >>> ์ด ์‹œ์ ์— ๊ฒฐ์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ž…๋ ฅ ํŒŒ์ผ์—์„œ ์ž˜๋ชป๋œ ํ–‰์„ ์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜, ์ž˜๋ชป๋œ ํ–‰์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ค๋ฅ˜๋ฅผ ์žก์•„์„œ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ํ”„๋ฆฐํŠธํ•˜๊ณ , ํŒŒ์ผ์˜ ๋๊นŒ์ง€ ๊ณ„์† ์ฒ˜๋ฆฌํ•˜๊ฒŒ pcost.py ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•ด ๋ณด์ž. ์—ฐ์Šต ๋ฌธ์ œ 1.32: ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ ํŒŒ์ด์ฌ์—๋Š” ์œ ์šฉํ•œ ํ•จ์ˆ˜์˜ ๋ฐฉ๋Œ€ํ•œ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” csv ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค. CSV ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ์ผ์ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•ด๋ผ. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค. >>> import csv >>> f = open('Data/portfolio.csv') >>> rows = csv.reader(f) >>> headers = next(rows) >>> headers ['name', 'shares', 'price'] >>> for row in rows: print(row) ['AA', '100', '32.20'] ['IBM', '50', '91.10'] ['CAT', '150', '83.44'] ['MSFT', '200', '51.23'] ['GE', '95', '40.37'] ['MSFT', '50', '65.10'] ['IBM', '100', '70.44'] >>> f.close() >>> csv ๋ชจ๋“ˆ์€ ๋”ฐ์˜ดํ‘œ๋ผ๋“ ์ง€, ์ฝค๋งˆ๋ฅผ ์‚ฌ์šฉํ•œ ๋ถ„ํ•  ๋“ฑ ์ € ์ˆ˜์ค€์˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์„ธ๋ถ€์‚ฌํ•ญ์„ ์ฒ˜๋ฆฌํ•ด ์ค€๋‹ค. ์œ„ ์ถœ๋ ฅ์˜ ์ฒซ ๋ฒˆ์งธ ์นผ๋Ÿผ์—์„œ ํฐ๋”ฐ์˜ดํ‘œ๊ฐ€ ์ œ๊ฑฐ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. csv ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด ํŒŒ์‹ฑ์„ ํ•˜๋„๋ก pcost.py ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•œ ๋‹ค์Œ, ์ด์ „์˜ ์˜ˆ์ œ๋ฅผ ์‹คํ–‰ํ•ด ๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 1.33: ๋ช…๋ นํ–‰์—์„œ ์ฝ๊ธฐ pcost.py ํ”„๋กœ๊ทธ๋žจ์€ ์ž…๋ ฅ ํŒŒ์ผ๋ช…์„ ํ•˜๋“œ์ฝ”๋”ฉํ–ˆ๋‹ค. # pcost.py def portfolio_cost(filename): ... # ์—ฌ๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ... cost = portfolio_cost('Data/portfolio.csv') print('Total cost:', cost) ๋ฐฐ์šฐ๊ณ  ํ…Œ์ŠคํŠธํ•  ๋•Œ๋Š” ๊ดœ์ฐฎ์ง€๋งŒ ์‹ค์ œ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. ์Šคํฌ๋ฆฝํŠธ์˜ ์ธ์ž๋กœ ํŒŒ์ผ๋ช…์„ ์ „๋‹ฌํ•˜๋ผ. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ”„๋กœ๊ทธ๋žจ์„ ๋ฐ”๊ฟ”๋ผ. # pcost.py import sys def portfolio_cost(filename): ... # ์—ฌ๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ... if len(sys.argv) == 2: filename = sys.argv[1] else: filename = 'Data/portfolio.csv' cost = portfolio_cost(filename) print('Total cost:', cost) ๋ช…๋ นํ–‰์—์„œ ์ „๋‹ฌ๋ฐ›์€ ์ธ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ sys.argv ๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์ด ํ”„๋กœ๊ทธ๋žจ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ(์œ ๋‹‰์Šค bash์—์„œ ์‹คํ–‰): bash % python3 pcost.py Data/portfolio.csv Total cost: 44671.15 bash % 2. ๋ฐ์ดํ„ฐ๋กœ ์ž‘์—…ํ•˜๊ธฐ ์œ ์šฉํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ ค๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ด ์„น์…˜์€ ํŒŒ์ด์ฌ์˜ ํ•ต์‹ฌ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ธ ํŠœํ”Œ, ๋ฆฌ์ŠคํŠธ, ์„ธํŠธ, ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์†Œ๊ฐœํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์„ ๋…ผ์˜ํ•œ๋‹ค. ์ด ์„น์…˜์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์€ ํŒŒ์ด์ฌ์˜ ๊ธฐ๋ณธ ๊ฐ์ฒด ๋ชจ๋ธ์„ ๋” ๊นŠ์ด ๋‹ค๋ฃฌ๋‹ค. 2.1 ์ž๋ฃŒํ˜•๊ณผ ์ž๋ฃŒ ๊ตฌ์กฐ 2.2 ์ปจํ…Œ์ด๋„ˆ 2.3 ์ถœ๋ ฅ ํฌ๋งคํŒ… 2.4 ์‹œํ€€์Šค 2.5 collections ๋ชจ๋“ˆ 2.6 ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌ ํ—จ ์…˜(list comprehension) 2.7 ๊ฐ์ฒด ๋ชจ๋ธ 2.1 ์ž๋ฃŒํ˜•๊ณผ ์ž๋ฃŒ ๊ตฌ์กฐ ์ด ์„น์…˜์—์„œ๋Š” ํŠœํ”Œ๊ณผ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์„ ์†Œ๊ฐœํ•œ๋‹ค. ๊ธฐ๋ณธ ์ž๋ฃŒํ˜• ํŒŒ์ด์ฌ์—๋Š” ๋ช‡ ๊ฐ€์ง€ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์ด ์žˆ๋‹ค. ์ •์ˆ˜ ๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜ ๋ฌธ์ž์—ด(ํ…์ŠคํŠธ) ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ๋„์ž…์—์„œ ๋ฐฐ์› ๋‹ค. None ํƒ€์ž… email_address = None None์€ ์ฃผ๋กœ ์„ ํƒ์ ์ธ ๊ฐ’์ด๋‚˜ ๋ˆ„๋ฝ ๊ฐ’(missing value)์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ์กฐ๊ฑด๋ฌธ์—์„œ๋Š” False๋กœ ํ‰๊ฐ€ํ•œ๋‹ค. if email_address: send_email(email_address, msg) ์ž๋ฃŒํ˜• ์‹ค์ œ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ๋” ๋ณต์žกํ•œ ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ฃผ์‹ ๋ณด์œ ์— ๋Œ€ํ•œ ์ •๋ณด์˜ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์ž. ๊ตฌ๊ธ€(GOOG) ์ฃผ์‹์„ 490.10 ๋‹ฌ๋Ÿฌ์— 100์ฃผ ๋ณด์œ  ์ด "๊ฐ์ฒด"๋Š” ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ์ข…๋ชฉ๋ช… ๋˜๋Š” ์‹ฌ๋ฒŒ(๋ฌธ์ž์—ด "GOOG") ์ฃผ์‹ ์ˆ˜(์ •์ˆ˜ 100) ๊ฐ€๊ฒฉ(๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜ 490.10) ํŠœํ”Œ(Tuple) ํŠœํ”Œ์€ ํ•จ๊ป˜ ๋ฌถ์ธ ๊ฐ’์˜ ์ปฌ๋ ‰์…˜์ด๋‹ค. ์˜ˆ: s = ('GOOG', 100, 490.1) ๋‹ค์Œ๊ณผ ๊ฐ™์ด ()๋ฅผ ์ƒ๋žตํ•˜๊ธฐ๋„ ํ•œ๋‹ค. s = 'GOOG', 100, 490.1 ํŠน์ˆ˜ํ•œ ์‚ฌ๋ก€(0-ํŠœํ”Œ, 1-ํŠœํ”Œ). t = () # ๋นˆ ํŠœํ”Œ w = ('GOOG', ) # ํ•ญ๋ชฉ์ด ํ•œ ๊ฐœ ์žˆ๋Š” ํŠœํ”Œ ํŠœํ”Œ์€ ์ฃผ๋กœ ๋‹จ์ˆœํ•œ ๋ ˆ์ฝ”๋“œ ๋˜๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์—ฌ๋Ÿฌ ๋ถ€๋ถ„์œผ๋กœ ๋œ ๋‹จ์ผ ๊ฐ์ฒด๋‹ค. ์ข‹์€ ๋น„์œ : ํŠœํ”Œ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์˜ ๋‹จ ์ผํ–‰๊ณผ ๋น„์Šทํ•˜๋‹ค. ํŠœํ”Œ์˜ ํ•ญ๋ชฉ์€ ์ˆœ์„œ๊ฐ€ ์žˆ๋‹ค(๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•จ). s = ('GOOG', 100, 490.1) name = s[0] # 'GOOG' shares = s[1] # 100 price = s[2] # 490.1 ๊ทธ๋Ÿฌ๋‚˜, ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค. >>> s[1] = 75 TypeError: object does not support item assignment ํ˜„์žฌ ํŠœํ”Œ์„ ๊ฐ€์ง€๊ณ  ์ƒˆ๋กœ์šด ํŠœํ”Œ์„ ๋งŒ๋“ค ์ˆ˜๋Š” ์žˆ๋‹ค. s = (s[0], 75, s[2]) ํŠœํ”Œ๋กœ ๋ฌถ๊ธฐ(packing) ํŠœํ”Œ์€ ์„œ๋กœ ๊ด€๋ จ๋œ ํ•ญ๋ชฉ๋“ค์„ ๋‹จ์ผ ์—”ํ‹ฐํ‹ฐ(entity)๋กœ ๋ฌถ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. s = ('GOOG', 100, 490.1) ํŠœํ”Œ์„ ๋‹จ์ผ ๊ฐ์ฒด๋กœ์„œ ํ”„๋กœ๊ทธ๋žจ์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„์œผ๋กœ ์‰ฝ๊ฒŒ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠœํ”Œ์„ ํ’€๊ธฐ(unpacking) ํŠœํ”Œ์„ ์ „๋‹ฌ๋ฐ›์€ ํ›„์—๋Š” ํ’€์–ด์„œ ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. name, shares, price = s print('Cost', shares * price) ์™ผ์ชฝ์˜ ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜๊ฐ€ ํŠœํ”Œ ๊ตฌ์กฐ์™€ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค. name, shares = s # ์˜ค๋ฅ˜ Traceback (most recent call last): ... ValueError: too many values to unpack ํŠœํ”Œ vs. ๋ฆฌ์ŠคํŠธ ํŠœํ”Œ์€ ์ฝ๊ธฐ ์ „์šฉ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ํŠœํ”Œ์˜ ์ฃผ๋œ ์šฉ๋„๋Š” ์—ฌ๋Ÿฌ ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋‹จ์ผ ํ•ญ๋ชฉ์œผ๋กœ์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ๊ณ ์œ ํ•œ ํ•ญ๋ชฉ๋“ค์˜ ์ปฌ๋ ‰์…˜์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ด๋•Œ ๋ชจ๋“  ํ•ญ๋ชฉ์ด ๊ฐ™์€ ํƒ€์ž…์ธ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. record = ('GOOG', 100, 490.1) # ํฌํŠธํด๋ฆฌ์˜ค์˜ ํ•œ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํŠœํ”Œ symbols = [ 'GOOG', 'AAPL', 'IBM' ] # ์„ธ ๊ฐ€์ง€ ์ฃผ์‹ ์‹ฌ๋ฒŒ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฆฌ์ŠคํŠธ ๋”•์…”๋„ˆ๋ฆฌ(Dictionary) ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ‚ค(key)๋ฅผ ๊ฐ’(value)์— ๋Œ€์‘(mapping) ์‹œํ‚จ๋‹ค. ํ•ด์‹œ ํ…Œ์ด๋ธ”(hash table) ๋˜๋Š” ์—ฐ๊ด€ ๋ฐฐ์—ด(associative array)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค. ํ‚ค๋Š” ๊ฐ’์— ์ ‘๊ทผํ•˜๋Š” ์ธ๋ฑ์Šค ์—ญํ• ์„ ํ•œ๋‹ค. s = { 'name': 'GOOG', 'shares': 100, 'price': 490.1 } ๊ณตํ†ต์ ์ธ ์—ฐ์‚ฐ ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ๊ฐ’์„ ์–ป์œผ๋ ค๋ฉด ํ‚ค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค. >>> print(s['name'], s['shares']) GOOG 100 >>> s['price'] 490.10 >>> ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ๋•Œ ํ‚ค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค. >>> s['shares'] = 75 >>> s['date'] = '6/6/2007' >>> ๊ฐ’์„ ์‚ญ์ œํ•˜๋ ค๋ฉด del ๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค. >>> del s['date'] >>> ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์ด ๋งŽ์ด ์žˆ๊ณ  ๊ทธ ๊ฐ’๋“ค์„ ์ˆ˜์ • ๋˜๋Š” ์กฐ์ž‘ํ•ด์•ผ ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ง„๋‹ค. s['price'] # vs s[2] ์—ฐ์Šต ๋ฌธ์ œ ์ง€๋‚œ ๋ช‡ ๊ฐœ์˜ ์—ฐ์Šต ๋ฌธ์ œ์—์„œ, ๋ฐ์ดํ„ฐํŒŒ์ผ Data/portfolio.csv๋ฅผ ์ฝ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ–ˆ๋‹ค. csv ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์ผ์„ ํ–‰ ๋‹จ์œ„๋กœ ์‰ฝ๊ฒŒ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. >>> import csv >>> f = open('Data/portfolio.csv') >>> rows = csv.reader(f) >>> next(rows) ['name', 'shares', 'price'] >>> row = next(rows) >>> row ['AA', '100', '32.20'] >>> ํŒŒ์ผ์„ ์ฝ๋Š” ๊ฒƒ์€ ์‰ฝ์ง€๋งŒ, ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์€ ๋‹ค์Œ์—๋Š” ๊ทธ๊ฒƒ์„ ๊ฐ€์ง€๊ณ  ๋” ๋งŽ์€ ์ผ์„ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ฝ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์„ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ, ์›์‹œ "ํ–‰(row)" ๋ฐ์ดํ„ฐ๋งŒ์œผ๋กœ๋Š” ์ž‘์—…ํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•˜์ง€ ์•Š๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐ„๋‹จํ•œ ์ˆ˜ํ•™ ๊ณ„์‚ฐ๋„ ํ•  ์ˆ˜ ์—†๋‹ค. >>> row = ['AA', '100', '32.20'] >>> cost = row[1] * row[2] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't multiply sequence by non-int of type 'str' >>> ์›์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์ข€ ๋” ์œ ์šฉํ•œ ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”์•ผ ๋‚˜์ค‘์— ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠœํ”Œ์ด๋‚˜ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 2.1: ํŠœํ”Œ ์ƒํ˜ธ์ž‘์šฉ์ ์ธ ํ”„๋กฌํ”„ํŠธ์—์„œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์œ„์˜ ํ–‰์„ ๋‚˜ํƒ€๋‚ด๋Š” ํŠœํ”Œ์„ ์ƒ์„ฑํ•˜๋˜, ์ˆซ์ž ์นผ๋Ÿผ์€ ์ ์ ˆํ•œ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. >>> t = (row[0], int(row[1]), float(row[2])) >>> t ('AA', 100, 32.2) >>> ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•ด, ์ฃผ์‹ ์ˆ˜์™€ ๊ฐ€๊ฒฉ์„ ๊ณฑํ•œ ์ด๋น„์šฉ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. >>> cost = t[1] * t[2] >>> cost 3220.0000000000005 >>> ํŒŒ์ด์ฌ์—์„œ ์ˆ˜ํ•™ ๊ณ„์‚ฐ์ด ํ‹€๋ฆฐ๊ฐ€? ๋‹ต์ด ์™œ 3220.0000000000005๋ผ๊ณ  ๋‚˜์˜ฌ๊นŒ? ์ด๊ฒƒ์€ ์ปดํ“จํ„ฐ์˜ ๋ถ€๋™์†Œ์ˆ˜์  ํ•˜๋“œ์›จ์–ด๋กœ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ๋‹ค. ์‹ญ์ง„์ˆ˜(decimal)๋ฅผ ์‹ญ์ง„๋ฒ•(Base-10)์ด ์•„๋‹ˆ๋ผ ์ด์ง„๋ฒ•(Base-2)์œผ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”๋‹ค. ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์ด๋ผ ํ• ์ง€๋ผ๋„, ์‹ญ์ง„์ˆ˜์™€ ๊ด€๋ จ๋œ ๊ณ„์‚ฐ์—๋Š” ์ž‘์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ฒ˜์Œ ๋ณธ๋‹ค๋ฉด ๋†€๋ผ์šธ ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ์ด๊ฒƒ์€ ์ •์ƒ์ ์ธ ์ž‘๋™์ด๋‹ค. ๋ถ€๋™์†Œ์ˆ˜์  ์‹ญ์ง„์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ผ์ด์ง€๋งŒ, ํ”„๋ฆฐํŒ… ๊ณผ์ •์—์„œ ์ข…์ข… ์ˆจ๊ฒจ์ง„๋‹ค. ์˜ˆ: >>> print(f'{cost:0.2f}') 3220.00 >>> ํŠœํ”Œ์€ ์ฝ๊ธฐ ์ „์šฉ์ด๋‹ค. ํ™•์ธํ•ด ๋ณด๊ณ  ์‹ถ์œผ๋ฉด ์ฃผ์‹ ์ˆ˜๋ฅผ 75๋กœ ๋ฐ”๊ฟ”๋ณด๋ผ. >>> t[1] = 75 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> ํŠœํ”Œ์˜ ๋‚ด์šฉ์„ ๋ฐ”๊ฟ€ ์ˆ˜๋Š” ์—†์ง€๋งŒ, ํ•ญ์ƒ ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ํŠœํ”Œ์„ ์ƒ์„ฑํ•ด ๊ธฐ์กด ๊ฒƒ์„ ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค. >>> t = (t[0], 75, t[2]) >>> t ('AA', 75, 32.2) >>> ๊ธฐ์กด ๋ณ€์ˆ˜๋ช…์„ ์žฌํ• ๋‹นํ•  ๋•Œ๋งˆ๋‹ค ๊ธฐ์กด ๊ฐ’์€ ๋ฒ„๋ ค์ง„๋‹ค. ์œ„์˜ ํ• ๋‹น๋ฌธ์ด ํŠœํ”Œ์„ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” ์ƒˆ๋กœ์šด ํŠœํ”Œ์„ ์ƒ์„ฑํ•˜๊ณ  ๊ธฐ์กด ๊ฒƒ์„ ๋ฒ„๋ ธ๋‹ค. ํŠœํ”Œ์€ ํŒจํ‚น์„ ํ•˜๊ณ  ๋ณ€์ˆ˜๋กœ ์–ธ ํŒจํ‚นํ•˜๋Š” ๋ฐ ์ข…์ข… ์‚ฌ์šฉ๋œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด ๋ณด์ž. >>> name, shares, price = t >>> name 'AA' >>> shares 75 >>> price 32.2 >>> ์œ„ ๋ณ€์ˆ˜๋“ค์„ ํŠœํ”Œ๋กœ ํŒจํ‚นํ•˜์ž. >>> t = (name, 2*shares, price) >>> t ('AA', 150, 32.2) >>> ์—ฐ์Šต ๋ฌธ์ œ 2.2: ์ž๋ฃŒ ๊ตฌ์กฐ๋กœ์„œ์˜ ๋”•์…”๋„ˆ๋ฆฌ ํŠœํ”Œ ๋Œ€์‹  ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. >>> d = { 'name' : row[0], 'shares' : int(row[1]), 'price' : float(row[2]) } >>> d {'name': 'AA', 'shares': 100, 'price': 32.2 } >>> ์ด๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜์ž. >>> cost = d['shares'] * d['price'] >>> cost 3220.0000000000005 >>> ์œ„์˜ ํŠœํ”Œ์— ๋Œ€ํ•ด์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ณ„์‚ฐ์„ ํ–ˆ๋‹ค. ์ฃผ์‹ ์ˆ˜๋ฅผ 75๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. >>> d['shares'] = 75 >>> d {'name': 'AA', 'shares': 75, 'price': 32.2 } >>> ํŠœํ”Œ๊ณผ ๋‹ฌ๋ฆฌ, ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ž์œ ๋กญ๊ฒŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜์ž. >>> d['date'] = (6, 11, 2007) >>> d['account'] = 12345 >>> d {'name': 'AA', 'shares': 75, 'price':32.2, 'date': (6, 11, 2007), 'account': 12345} >>> ์—ฐ์Šต ๋ฌธ์ œ 2.3: ์ถ”๊ฐ€์ ์ธ ๋”•์…”๋„ˆ๋ฆฌ ์—ฐ์‚ฐ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด ์ „์ฒด ํ‚ค๋ฅผ ์–ป๋Š”๋‹ค. >>> list(d) ['name', 'shares', 'price', 'date', 'account'] >>> ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๋”•์…”๋„ˆ๋ฆฌ์— ๋Œ€ํ•ด for ๋ฌธ์„ ์‚ฌ์šฉํ•ด ์ดํ„ฐ๋ ˆ์ด์…˜์„ ํ•ด๋„ ํ‚ค๋ฅผ ์–ป๋Š”๋‹ค, >>> for k in d: print('k =', k) k = name k = shares k = price k = date k = account >>> ๋‹ค์Œ ์ฝ”๋“œ๋Š” ํ‚ค์™€ ๊ฐ’์„ ๋™์‹œ์— ์กฐํšŒํ•œ๋‹ค. >>> for k in d: print(k, '=', d[k]) name = AA shares = 75 price = 32.2 date = (6, 11, 2007) account = 12345 >>> keys() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์ „์ฒด ํ‚ค๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. >>> keys = d.keys() >>> keys dict_keys(['name', 'shares', 'price', 'date', 'account']) >>> keys()๋Š” ํŠน์ˆ˜ํ•œ dict_keys ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ์ ์—์„œ ๋…ํŠนํ•˜๋‹ค. ์ด๊ฒƒ์€ ์›๋ž˜ ๋”•์…”๋„ˆ๋ฆฌ์— ๋Œ€ํ•œ ์˜ค๋ฒ„๋ ˆ์ด๋กœ, ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋”๋ผ๋„ ํ•ญ์ƒ ํ˜„์žฌ ํ‚ค๋ฅผ ์–ป๊ฒŒ ํ•ด์ค€๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด ๋ณด์ž. >>> del d['account'] >>> keys dict_keys(['name', 'shares', 'price', 'date']) >>> d.keys()๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•˜์Œ์—๋„ 'account'๊ฐ€ keys์—์„œ ์‚ฌ๋ผ์ง„ ๊ฒƒ์— ์œ ์˜ํ•˜๋ผ. ํ‚ค์™€ ๊ฐ’์„ ํ•จ๊ป˜ ๋‹ค๋ฃจ๋Š” ๋”์šฑ ์šฐ์•„ํ•œ ๋ฐฉ๋ฒ•์€ items() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด (ํ‚ค, ๊ฐ’) ํŠœํ”Œ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. >>> items = d.items() >>> items dict_items([('name', 'AA'), ('shares', 75), ('price', 32.2), ('date', (6, 11, 2007))]) >>> for k, v in d.items(): print(k, '=', v) name = AA shares = 75 price = 32.2 date = (6, 11, 2007) >>> items ๊ฐ™์€ ํŠœํ”Œ์ด ์žˆ์œผ๋ฉด dict() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•œ๋ฒˆ ํ•ด ๋ณด์ž. >>> items dict_items([('name', 'AA'), ('shares', 75), ('price', 32.2), ('date', (6, 11, 2007))]) >>> d = dict(items) >>> d {'name': 'AA', 'shares': 75, 'price':32.2, 'date': (6, 11, 2007)} >>> 2.2 ์ปจํ…Œ์ด๋„ˆ ์ด ์„น์…˜์€ ๋ฆฌ์ŠคํŠธ, ๋”•์…”๋„ˆ๋ฆฌ, ์„ธํŠธ๋ฅผ ๋…ผ์˜ํ•œ๋‹ค. ๊ฐœ์š” ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋งŽ์€ ์ˆ˜์˜ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ค„์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค ์ฃผ์‹ ๊ฐ€๊ฒฉ ํ…Œ์ด๋ธ” ์„ธ ๊ฐ€์ง€ ์„ ํƒ์‚ฌํ•ญ์ด ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ(list): ์ˆœ์„œ๊ฐ€ ์œ ์ง€๋˜๋Š”(ordered) ๋ฐ์ดํ„ฐ. ๋”•์…”๋„ˆ๋ฆฌ(dict): ์ˆœ์„œ๊ฐ€ ์—†๋Š”(unordered) ๋ฐ์ดํ„ฐ. ์„ธํŠธ(set): ๊ณ ์œ ํ•œ ํ•ญ๋ชฉ์˜ ์ง‘ํ•ฉ์ด๋ฉฐ ์ˆœ์„œ๊ฐ€ ์—†์Œ. ์ปจํ…Œ์ด๋„ˆ๋กœ์„œ์˜ ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•  ๋•Œ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. ๋ฆฌ์ŠคํŠธ๋Š” ์–ด๋–ค ์œ ํ˜•์˜ ๊ฐ์ฒด๋“  ๋‹ด์„ ์ˆ˜ ์žˆ์Œ์„ ๋ช…์‹ฌํ•˜๋ผ. ์˜ˆ: ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ. portfolio = [ ('GOOG', 100, 490.1), ('IBM', 50, 91.3), ('CAT', 150, 83.44) ] portfolio[0] # ('GOOG', 100, 490.1) portfolio[2] # ('CAT', 150, 83.44) ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ(construction) ๋‹ค์Œ์€ ๋ฆฌ์ŠคํŠธ์— ํ•ญ๋ชฉ์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์ฑ„์›Œ ๋„ฃ๋Š” ์˜ˆ์ด๋‹ค. records = [] # ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ์ดˆ๊ธฐํ™” # .append()๋ฅผ ์‚ฌ์šฉํ•ด ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ records.append(('GOOG', 100, 490.10)) records.append(('IBM', 50, 91.3)) ... ๋‹ค์Œ์€ ํŒŒ์ผ์—์„œ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ฝ์–ด ๋ฆฌ์ŠคํŠธ์— ์ฑ„์šฐ๋Š” ์˜ˆ์ด๋‹ค. records = [] # ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ์ดˆ๊ธฐํ™” with open('Data/portfolio.csv', 'rt') as f: next(f) # ํ—ค๋”๋ฅผ ๊ฑด๋„ˆ๋œ€ for line in f: row = line.split(',') records.append((row[0], int(row[1]), float(row[2]))) ์ปจํ…Œ์ด๋„ˆ๋กœ์„œ์˜ ๋”•์…”๋„ˆ๋ฆฌ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๋น ๋ฅธ ์ž„์˜ ์กฐํšŒ(ํ‚ค ์ด๋ฆ„์„ ์‚ฌ์šฉ)์— ์œ ์šฉํ•˜๋‹ค. ์˜ˆ: ์ฃผ์‹ ๊ฐ€๊ฒฉ์˜ ๋”•์…”๋„ˆ๋ฆฌ. prices = { 'GOOG': 513.25, 'CAT': 87.22, 'IBM': 93.37, 'MSFT': 44.12 } ๊ฐ„๋‹จํžˆ ์กฐํšŒํ•ด ๋ณด์ž. >>> prices['IBM'] 93.37 >>> prices['GOOG'] 513.25 >>> ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ ๋‹ค์Œ์€ ๋”•์…”๋„ˆ๋ฆฌ์— ํ•ญ๋ชฉ์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์ฑ„์šฐ๋Š” ์˜ˆ์ด๋‹ค. prices = {} # ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ดˆ๊ธฐํ™” # ์ƒˆ ํ•ญ๋ชฉ์„ ์‚ฝ์ž… prices['GOOG'] = 513.25 prices['CAT'] = 87.22 prices['IBM'] = 93.37 ๋‹ค์Œ์€ ํŒŒ์ผ ๋‚ด์šฉ์œผ๋กœ๋ถ€ํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ฑ„์šฐ๋Š” ์˜ˆ์ด๋‹ค. prices = {} # ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ดˆ๊ธฐํ™” with open('Data/prices.csv', 'rt') as f: for line in f: row = line.split(',') prices[row[0]] = float(row[1]) ์ฐธ๊ณ : Data/prices.csv ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์ด๊ฒƒ์„ ์‹œ๋„ํ•˜๋ฉด ์ž˜ ๋˜๋‹ค๊ฐ€ ๋งˆ์ง€๋ง‰์— ๊ณต๋ฐฑ ํ–‰์ด ์žˆ์–ด ์ถฉ๋Œํ•œ๋‹ค. ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค(์—ฐ์Šต ๋ฌธ์ œ 2.6์„ ์ฐธ์กฐ). ๋”•์…”๋„ˆ๋ฆฌ ์กฐํšŒ ํ‚ค๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋‹ค. if key in d: # YES else: # NO ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’์„ ์ฐพ์œผ๋ ค ํ•  ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค. name = d.get(key, default) ์˜ˆ: >>> prices.get('IBM', 0.0) 93.37 >>> prices.get('SCOX', 0.0) 0.0 >>> ๋ณตํ•ฉ ํ‚ค(composite keys) ํŒŒ์ด์ฌ์—์„œ๋Š” ์–ด๋–ค ํƒ€์ž…์˜ ๊ฐ’์ด๋“  ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ ํ‚ค๋Š” ๋ฐ˜๋“œ์‹œ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•œ ํƒ€์ž…์ด์–ด์•ผ ํ•œ๋‹ค. ํŠœํ”Œ์˜ ์˜ˆ: holidays = { (1, 1) : 'New Years', (3, 14) : 'Pi day', (9, 13) : "Programmer's day", } ์•ก์„ธ์Šค: >>> holidays[3, 14] 'Pi day' >>> ๋ฆฌ์ŠคํŠธ, ์„ธํŠธ, ๋‹ค๋ฅธ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ(mutable) ํ•˜๋ฏ€๋กœ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ์„ธํŠธ ์„ธํŠธ๋Š” ๊ณ ์œ  ํ•ญ๋ชฉ์˜ ๋ชจ์Œ์ด๋ฉฐ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค. tech_stocks = { 'IBM','AAPL','MSFT' } # ๋Œ€์ฒด ๊ตฌ๋ฌธ tech_stocks = set(['IBM', 'AAPL', 'MSFT']) ์„ธํŠธ๋Š” ๋ฉค๋ฒ„์‹ญ ํ…Œ์ŠคํŠธ์— ์œ ์šฉํ•˜๋‹ค. >>> tech_stocks set(['AAPL', 'IBM', 'MSFT']) >>> 'IBM' in tech_stocks True >>> 'FB' in tech_stocks False >>> ์„ธํŠธ๋Š” ์ค‘๋ณต ์ œ๊ฑฐ์—๋„ ์œ ์šฉํ•˜๋‹ค. names = ['IBM', 'AAPL', 'GOOG', 'IBM', 'GOOG', 'YHOO'] unique = set(names) # unique = set(['IBM', 'AAPL','GOOG','YHOO']) ๊ทธ ์™ธ์˜ ์ง‘ํ•ฉ(set) ์—ฐ์‚ฐ: names.add('CAT') # ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ names.remove('YHOO') # ํ•ญ๋ชฉ์„ ์ œ๊ฑฐ s1 | s2 # ํ•ฉ์ง‘ํ•ฉ s1 & s2 # ๊ต์ง‘ํ•ฉ s1 - s2 # ์ฐจ์ง‘ํ•ฉ ์—ฐ์Šต ๋ฌธ์ œ ์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ๋Š” ์ด ์ฝ”์Šค์—์„œ ๊ณ„์† ์‚ฌ์šฉํ•  ์ฃผ๋œ ํ”„๋กœ๊ทธ๋žจ ์ค‘ ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ค๊ธฐ ์‹œ์ž‘ํ•œ๋‹ค. Work/report.py ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์ž‘์—…ํ•˜๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 2.4: ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ Data/portfolio.csv ํŒŒ์ผ์—๋Š” ํฌํŠธํด๋ฆฌ์˜ค์— ํŽธ์ž…๋œ ์ฃผ์‹ ๋ชฉ๋ก์ด ์žˆ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 1.30์—์„œ, ์šฐ๋ฆฌ๋Š” ์ด ํŒŒ์ผ์„ ์ฝ๊ณ  ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” portfolio_cost(filename) ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ์ฝ”๋“œ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค. # pcost.py import csv def portfolio_cost(filename): '''ํฌํŠธํด๋ฆฌ์˜ค ํŒŒ์ผ์˜ ์ด๋น„์šฉ(์ฃผ์‹ ์ˆ˜*๊ฐ€๊ฒฉ)์„ ๊ณ„์‚ฐ''' total_cost = 0.0 with open(filename, 'rt') as f: rows = csv.reader(f) headers = next(rows) for row in rows: nshares = int(row[1]) price = float(row[2]) total_cost += nshares * price return total_cost ์ด ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด, ์ƒˆ๋กœ์šด report.py ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ผ. ๊ทธ ํŒŒ์ผ์—์„œ, ์ฃผ์–ด์ง„ ํฌํŠธํด๋ฆฌ์˜ค ํŒŒ์ผ์„ ์—ด๊ณ  ๋‚ด์šฉ์„ ์ฝ์–ด ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” read_portfolio(filename) ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด, ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์•ฝ๊ฐ„ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. ๋จผ์ €, total_cost = 0๋ฅผ ์ •์˜ํ•˜๋Š” ๋Œ€์‹ , ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์˜ˆ: portfolio = [] ๋‹ค์Œ์œผ๋กœ, ์ด๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋Œ€์‹ , ์ด์ „ ์—ฐ์Šต ๋ฌธ์ œ์—์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ ํ–‰์„ ํŠœํ”Œ๋กœ ๋ฐ”๊ฟ”์„œ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์˜ˆ: for row in rows: holding = (row[0], int(row[1]), float(row[2])) portfolio.append(holding) ๋์œผ๋กœ, ๊ฒฐ๊ณผ portfolio ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ž‘์„ฑํ•œ ํ•จ์ˆ˜๋ฅผ ์ƒํ˜ธ์ž‘์šฉ์ ์œผ๋กœ ์‹คํ—˜ํ•ด ๋ณด๋ผ(๊ทธ๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ report.py๋ถ€ํ„ฐ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค). ํžŒํŠธ: ํŒŒ์ผ์„ ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ํ•  ๋•Œ๋Š” -i๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. >>> portfolio = read_portfolio('Data/portfolio.csv') >>> portfolio [('AA', 100, 32.2), ('IBM', 50, 91.1), ('CAT', 150, 83.44), ('MSFT', 200, 51.23), ('GE', 95, 40.37), ('MSFT', 50, 65.1), ('IBM', 100, 70.44)] >>> >>> portfolio[0] ('AA', 100, 32.2) >>> portfolio[1] ('IBM', 50, 91.1) >>> portfolio[1][1] 50 >>> total = 0.0 >>> for s in portfolio: total += s[1] * s[2] >>> print(total) 44671.15 >>> ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ๋Š” 2์ฐจ์› ๋ฐฐ์—ด๊ณผ ๋งค์šฐ ๋น„์Šทํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, portfolio[row][column]์™€ ๊ฐ™์ด ์กฐํšŒํ•ด ํŠน์ • row์™€ column์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค(row์™€ column์€ ์ •์ˆ˜). ๋”ฐ๋ผ์„œ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋งˆ์ง€๋ง‰ for ๋ฃจํ”„๋ฅผ ์žฌ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. >>> total = 0.0 >>> for name, shares, price in portfolio: total += shares*price >>> print(total) 44671.15 >>> ์—ฐ์Šต ๋ฌธ์ œ 2.5: ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ ์—ฐ์Šต ๋ฌธ์ œ 2.4์—์„œ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด, ํฌํŠธํด๋ฆฌ์˜ค์— ์žˆ๋Š” ์ฃผ์‹์„ ํŠœํ”Œ ๋Œ€์‹  ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋‚˜ํƒ€๋‚ด๊ฒŒ ํ•ด ๋ณด์ž. ์ด ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ์ž…๋ ฅ ํŒŒ์ผ์˜ ์นผ๋Ÿผ๋“ค์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด, ๊ฐ๊ฐ "name", "shares", "price"๋ผ๋Š” ํ•„๋“œ๋ช…์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ ์—ฐ์Šต ๋ฌธ์ œ 2.4์—์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‹คํ—˜ํ•ด ๋ณด๋ผ. >>> portfolio = read_portfolio('Data/portfolio.csv') >>> portfolio [{'name': 'AA', 'shares': 100, 'price': 32.2}, {'name': 'IBM', 'shares': 50, 'price': 91.1}, {'name': 'CAT', 'shares': 150, 'price': 83.44}, {'name': 'MSFT', 'shares': 200, 'price': 51.23}, {'name': 'GE', 'shares': 95, 'price': 40.37}, {'name': 'MSFT', 'shares': 50, 'price': 65.1}, {'name': 'IBM', 'shares': 100, 'price': 70.44}] >>> portfolio[0] {'name': 'AA', 'shares': 100, 'price': 32.2} >>> portfolio[1] {'name': 'IBM', 'shares': 50, 'price': 91.1} >>> portfolio[1]['shares'] 50 >>> total = 0.0 >>> for s in portfolio: total += s['shares']*s['price'] >>> print(total) 44671.15 >>> ์—ฌ๊ธฐ์„œ ๊ฐ ํ•ญ๋ชฉ์˜ ํ•„๋“œ์— ์ ‘๊ทผํ•  ๋•Œ๋Š” ์ˆซ์ž๋กœ ๋œ ์นผ๋Ÿผ ๋ฒˆํ˜ธ ๋Œ€์‹  ํ‚ค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋‚˜์ค‘์— ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ์ˆ˜์›”ํ•˜๋ฏ€๋กœ ์ด ๋ฐฉ๋ฒ•์„ ์„ ํ˜ธํ•œ๋‹ค. ํฐ ๋”•์…”๋„ˆ๋ฆฌ์™€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ฒƒ์€ ์ง€์ €๋ถ„ํ•ด์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋””๋ฒ„๊น…์„ ์œ„ํ•ด ์ถœ๋ ฅ์„ ์ •๋ˆํ•˜๋ ค๋ฉด pprint ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•˜๋ผ. >>> from pprint import pprint >>> pprint(portfolio) [{'name': 'AA', 'price': 32.2, 'shares': 100}, {'name': 'IBM', 'price': 91.1, 'shares': 50}, {'name': 'CAT', 'price': 83.44, 'shares': 150}, {'name': 'MSFT', 'price': 51.23, 'shares': 200}, {'name': 'GE', 'price': 40.37, 'shares': 95}, {'name': 'MSFT', 'price': 65.1, 'shares': 50}, {'name': 'IBM', 'price': 70.44, 'shares': 100}] >>> ์—ฐ์Šต ๋ฌธ์ œ 2.6: ์ปจํ…Œ์ด๋„ˆ๋กœ์„œ์˜ ๋”•์…”๋„ˆ๋ฆฌ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ•ญ๋ชฉ์„ ์ •์ˆ˜ ์ด์™ธ์˜ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด ์กฐํšŒํ•˜๊ณ ์ž ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ํŒŒ์ด์ฌ ์…ธ์—์„œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ๋†€์•„๋ณด์ž. >>> prices = { } >>> prices['IBM'] = 92.45 >>> prices['MSFT'] = 45.12 >>> prices ... ๊ฒฐ๊ณผ๋ฅผ ์‚ดํŽด ๋ณด๋ผ ... >>> prices['IBM'] 92.45 >>> prices['AAPL'] ... ๊ฒฐ๊ณผ๋ฅผ ์‚ดํŽด ๋ณด๋ผ ... >>> 'AAPL' in prices False >>> Data/prices.csv ํŒŒ์ผ์€ ์—ฌ๋Ÿฌ ํ–‰์— ๊ฑธ์ณ ์ฃผ์‹ ๊ฐ€๊ฒฉ์„ ๋‹ด๊ณ  ์žˆ๋‹ค. ํŒŒ์ผ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. "AA",9.22 "AXP",24.85 "BA",44.85 "BAC",11.27 "C",3.72 ... ์ด์™€ ๊ฐ™์€ ๊ฐ€๊ฒฉ ์ •๋ณด๋ฅผ ์ฝ์–ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” read_prices(filename) ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ๋”•์…”๋„ˆ๋ฆฌ ํ‚ค๋Š” ์ข…๋ชฉ๋ช…์œผ๋กœ, ๊ฐ’์€ ์ฃผ์‹ ๊ฐ€๊ฒฉ์œผ๋กœ ํ•œ๋‹ค. ์œ„์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋จผ์ € ๋งŒ๋“ค๊ณ  ๋‚˜์„œ ๊ฐ’์„ ์‚ฝ์ž…ํ•˜๋ผ. ์ด๋ฒˆ์—๋Š” ํŒŒ์ผ์—์„œ ๊ฐ’์„ ์ฝ๋Š”๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. ์ด ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข…๋ชฉ๋ช…์„ ์žฌ๋นจ๋ฆฌ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ํŒ์ด ์žˆ๋‹ค. ๋จผ์ €, ์•ž์—์„œ ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ csv ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ผ. ๋ฐ”ํ€ด๋ฅผ ์žฌ๋ฐœ๋ช…ํ•  ํ•„์š”๋Š” ์—†๋‹ค. >>> import csv >>> f = open('Data/prices.csv', 'r') >>> rows = csv.reader(f) >>> for row in rows: print(row) ['AA', '9.22'] ['AXP', '24.85'] ... [] >>> ๋˜ ๋‹ค๋ฅธ ๋ณต์žก์„ฑ์€ Data/prices.csv ํŒŒ์ผ์— ๊ณต๋ฐฑ ํ–‰์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค. ์œ„์—์„œ ๋ฐ์ดํ„ฐ์˜ ๋งˆ์ง€๋ง‰ ํ–‰์€ ๋นˆ ๋ฆฌ์ŠคํŠธ๋‹ค. ์ฆ‰, ๊ทธ ํ–‰์—๋Š” ์•„๋ฌด ๋ฐ์ดํ„ฐ๋„ ํ‘œ์‹œํ•˜์ง€ ์•Š์•˜๋‹ค. ๊ทธ๊ฒƒ ๋•Œ๋ฌธ์— ํ”„๋กœ๊ทธ๋žจ์ด ์˜ˆ์™ธ๋ฅผ ๋‚ด๋ฉฐ ์ฃฝ์–ด๋ฒ„๋ฆด ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค. try์™€ except ๋ฌธ์„ ์‚ฌ์šฉํ•ด ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜์ž. ์ƒ๊ฐํ•  ๊ฑฐ๋ฆฌ: ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋Œ€์‹  if ๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋‚˜์œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจ๋‹จํ•˜๋Š” ๊ฒƒ์ด ๋‚˜์„๊นŒ? read_prices() ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ–ˆ์œผ๋ฉด, ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ด ๋ณด๋ผ. >>> prices = read_prices('Data/prices.csv') >>> prices['IBM'] 106.28 >>> prices['MSFT'] 20.89 >>> ์—ฐ์Šต ๋ฌธ์ œ 2.7: ์€ํ‡ดํ•ด๋„ ๋˜๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๊ฒƒ์„ ๋ชจ๋‘ ํ™œ์šฉํ•ด report.py ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด ๋ณด์ž. ์—ฐ์Šต ๋ฌธ์ œ 2.5์˜ ์ฃผ์‹ ๋ชฉ๋ก๊ณผ ์—ฐ์Šต ๋ฌธ์ œ 2.6์˜ ๊ฐ€๊ฒฉ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ›์•„์„œ ํฌํŠธํด๋ฆฌ์˜ค์˜ ํ˜„์žฌ ๊ฐ€์น˜์™€ ์†์ต์„ ๊ณ„์‚ฐํ•œ๋‹ค. 2.3 ํฌ๋งคํŒ… ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์ž‘์—…ํ•  ๋•Œ ๊ตฌ์กฐํ™”๋œ ์ถœ๋ ฅ(ํ…Œ์ด๋ธ” ๋“ฑ)์„ ์ƒ์„ฑํ•˜๊ณ ์ž ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์˜ˆ: Name Shares Price ---------- ---------- ----------- AA 100 32.20 IBM 50 91.10 CAT 150 83.44 MSFT 200 51.23 GE 95 40.37 MSFT 50 65.10 IBM 100 70.44 ๋ฌธ์ž์—ด ํฌ๋งคํŒ… ํŒŒ์ด์ฌ 3.6 ์ด์ƒ์—์„œ๋Š” ๋ฌธ์ž์—ด ํฌ๋งคํŒ…์— f ๋ฌธ์ž์—ด(f-string)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. >>> name = 'IBM' >>> shares = 100 >>> price = 91.1 >>> f'{name:>10s} {shares:>10d} {price:>10.2f}' ' IBM 100 91.10' >>> {ํ‘œํ˜„์‹:ํฌ๋งท} ๋ถ€๋ถ„์ด ๋Œ€์ฒด๋œ๋‹ค. ์ด๊ฒƒ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด print ๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค. print(f'{name:>10s} {shares:>10d} {price:>10.2f}') ํฌ๋งท ์ฝ”๋“œ ํฌ๋งท ์ฝ”๋“œ({} ๋‚ด์˜ : ์ดํ›„)๋Š” C์˜ printf()์™€ ๋น„์Šทํ•˜๋‹ค. ์ผ๋ฐ˜์ ์ธ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. d ์‹ญ์ง„ ์ •์ˆ˜ b ์ด์ง„ ์ •์ˆ˜ x 16์ง„ ์ •์ˆ˜ f ๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜ [-] m.dddddd e ๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜ [-] m.dddddde+-xx g E ํ‘œ๊ธฐ๋ฅผ ์„ ํƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋™์†Œ์ˆ˜์  s ๋ฌธ์ž์—ด c ๋ฌธ์ž(์ •์ˆ˜๋กœ๋ถ€ํ„ฐ) ํ•„๋“œ ํญ๊ณผ ์ •๋ฐ€๋„๋ฅผ ์กฐ์ •ํ•˜๋Š” ๊ณตํ†ต ์ˆ˜์ •์ž. ์ด๊ฒƒ์€ ๋ถ€๋ถ„์ ์ธ ๋ชฉ๋ก์ด๋‹ค. :>10d ์ •์ˆ˜๋ฅผ 10์ž๋ฆฌ ํ•„๋“œ์— ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ :<10d ์ •์ˆ˜๋ฅผ 10์ž๋ฆฌ ํ•„๋“œ์— ์™ผ์ชฝ ์ •๋ ฌ :^10d ์ •์ˆ˜๋ฅผ 10์ž๋ฆฌ ํ•„๋“œ์— ๊ฐ€์šด๋ฐ ์ •๋ ฌ :0.2f ๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜๋ฅผ ๋‘ ์ž๋ฆฌ ์ •๋ฐ€๋„๋กœ ๋‚˜ํƒ€๋ƒ„ ๋”•์…”๋„ˆ๋ฆฌ ํฌ๋งคํŒ… format_map() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ’๋“ค์˜ ๋”•์…”๋„ˆ๋ฆฌ์— ๋ฌธ์ž์—ด ํฌ๋งคํŒ…์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. >>> s = { 'name': 'IBM', 'shares': 100, 'price': 91.1 } >>> '{name:>10s} {shares:10d} {price:10.2f}'.format_map(s) ' IBM 100 91.10' >>> ๊ทธ๊ฒƒ์€ f ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋˜, ์ œ๊ณต๋œ ๋”•์…”๋„ˆ๋ฆฌ๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ์ทจํ•œ๋‹ค. format() ๋ฉ”์„œ๋“œ format() ๋ฉ”์„œ๋“œ๋Š” ์ธ์ž ๋˜๋Š” ํ‚ค์›Œ๋“œ ์ธ์ž์— ๋Œ€ํ•œ ํฌ๋งคํŒ…์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. >>> '{name:>10s} {shares:10d} {price:10.2f}'.format(name='IBM', shares=100, price=91.1) ' IBM 100 91.10' >>> '{:10s} {:10d} {:10.2f}'.format('IBM', 100, 91.1) ' IBM 100 91.10' >>> ์†”์งํžˆ, format()์€ ๋‹ค์†Œ ์žฅํ™ฉํ•˜๋‹ค. ๋‚˜๋Š” f ๋ฌธ์ž์—ด์„ ์„ ํ˜ธํ•œ๋‹ค. C ์Šคํƒ€์ผ ํฌ๋งคํŒ… ํฌ๋งคํŒ… ์—ฐ์‚ฐ์ž %๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. >>> 'The value is %d' % 3 'The value is 3' >>> '% 5d %-5d % 10d' % (3,4,5) ' 3 4 5' >>> '% 0.2f' % (3.1415926, ) '3.14' ์ด๋•Œ ์˜ค๋ฅธ์ชฝ์—๋Š” ๋‹จ์ผ ํ•ญ๋ชฉ ๋˜๋Š” ํŠœํ”Œ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ํฌ๋งท ์ฝ”๋“œ๋Š” C์˜ printf()์™€ ์œ ์‚ฌํ•˜๋‹ค. ์ฐธ๊ณ : ์ด๊ฒƒ์€ ๋ฐ”์ดํŠธ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํฌ๋งคํŒ…์ด๋‹ค. >>> b'%s has %n messages' % (b'Dave', 37) b'Dave has 37 messages' >>> ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 2.8: ์ˆซ์ž๋ฅผ ํฌ๋งทํ•˜๋Š” ๋ฒ• ์ˆซ์ž๋ฅผ ํ”„๋ฆฐํŒ… ํ•  ๋•Œ์˜ ๊ณตํ†ต์ ์ธ ๋ฌธ์ œ๋Š” ์‹ญ์ง„์ˆ˜์˜ ์ž๋ฆฟ์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์„ ํ•ด๊ฒฐํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ f ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ ์˜ˆ๋ฅผ ์‹œ๋„ํ•ด ๋ณด์ž. >>> value = 42863.1 >>> print(value) 42863.1 >>> print(f'{value:0.4f}') 42863.1000 >>> print(f'{value:>16.2f}') 42863.10 >>> print(f'{value:<16.2f}') 42863.10 >>> print(f'{value:*>16, .2f}') *******42,863.10 >>> f ๋ฌธ์ž์—ด์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํฌ๋งคํŒ… ์ฝ”๋“œ์— ๋Œ€ํ•œ ์™„์ „ํ•œ ๋ฌธ์„œ๋Š” ์—ฌ๊ธฐ์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ž์—ด์˜ % ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ํฌ๋งคํŒ…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. >>> print('% 0.4f' % value) 42863.1000 >>> print('% 16.2f' % value) 42863.10 >>> % ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ๋ฌธ์„œ๋ฅผ ์—ฌ๊ธฐ์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ž์—ด ํฌ๋งคํŒ…์„ print์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ๋ฐ˜๋“œ์‹œ ๊ทธ๋ ‡๊ฒŒ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ํฌ๋งคํŒ…ํ•œ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ๋œ๋‹ค. >>> f = '% 0.4f' % value >>> f '42863.1000' >>> ์—ฐ์Šต ๋ฌธ์ œ 2.9: ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ 2.7์—์„œ ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค์˜ ์†์ต์„ ๊ณ„์‚ฐํ•˜๋Š” report.py๋ผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ–ˆ๋‹ค. ์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ๋Š” ๊ทธ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•œ๋‹ค. Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 ์ด ๋ณด๊ณ ์„œ์—์„œ "Price"๋Š” ์ฃผ์‹์˜ ํ˜„์žฌ ๊ฐ€๊ฒฉ์ด๋ฉฐ "Change"๋Š” ์ฒ˜์Œ ๋งค์ˆ˜ํ•œ ๊ฐ€๊ฒฉ๊ณผ์˜ ์ฐจ์ด๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๋ณด๊ณ ์„œ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด, ์šฐ์„  ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์ˆ˜์ง‘ํ•ด์•ผ ํ•œ๋‹ค. ์ข…๋ชฉ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ€๊ฒฉ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ์ทจํ•ด์„œ ์œ„์˜ ํ…Œ์ด๋ธ”๊ณผ ๊ฐ™์€ ํ–‰์„ ํฌํ•จํ•˜๋Š” ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” make_report() ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์ž. ์ด ํ•จ์ˆ˜๋ฅผ report.py ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ƒํ˜ธ์ž‘์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘๋™ํ•ด์•ผ ํ•œ๋‹ค. >>> portfolio = read_portfolio('Data/portfolio.csv') >>> prices = read_prices('Data/prices.csv') >>> report = make_report(portfolio, prices) >>> for r in report: print(r) ('AA', 100, 9.22, -22.980000000000004) ('IBM', 50, 106.28, 15.180000000000007) ('CAT', 150, 35.46, -47.98) ('MSFT', 200, 20.89, -30.339999999999996) ('GE', 95, 13.48, -26.889999999999997) ... >>> ์—ฐ์Šต ๋ฌธ์ œ 2.10: ํฌ๋งคํŒ…๋œ ํ…Œ์ด๋ธ”์„ ํ”„๋ฆฐํŒ… ํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ 2.9์˜ for ๋ฃจํ”„์—์„œ print ๋ฌธ์„ ์ˆ˜์ •ํ•ด ํŠœํ”Œ์„ ํฌ๋งคํŒ…ํ•œ๋‹ค. >>> for r in report: print('% 10s % 10d % 10.2f % 10.2f' % r) AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 ... >>> ๊ฐ’์„ ํ™•์žฅํ•ด f ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ: >>> for name, shares, price, change in report: print(f'{name:>10s} {shares:>10d} {price:>10.2f} {change:>10.2f}') AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 ... >>> ์œ„๋ฌธ์žฅ์„ report.py ํ”„๋กœ๊ทธ๋žจ์— ์ถ”๊ฐ€ํ•˜์ž. make_report() ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ์„ ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ ์ข‹๊ฒŒ ํฌ๋งคํŒ…๋œ ํ…Œ์ด๋ธ”์„ ํ”„๋ฆฐํŠธํ•˜๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 2.11: ํ—ค๋” ์ถ”๊ฐ€ํ•˜๊ธฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ—ค๋”์˜ ํŠœํ”Œ์ด ์žˆ๋‹ค๊ณ  ํ•˜์ž. headers = ('Name', 'Shares', 'Price', 'Change') ์œ„์™€ ๊ฐ™์€ ํ—ค๋”์˜ ํŠœํ”Œ์„ ๊ฐ€์ง€๊ณ  ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ํ”„๋กœ๊ทธ๋žจ์— ์ถ”๊ฐ€ํ•˜๋ผ. ํ—ค๋” ํญ์€ 10์ž๋ฆฌ, ํ—ค๋” ์ด๋ฆ„์€ ์˜ค๋ฅธ์ชฝ ์ •๋ ฌํ•˜๊ณ , ๊ฐ ํ•„๋“œ๋Š” ๊ณต๋ฐฑ ํ•œ ๊ฐœ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค. ' Name Shares Price Change' ํ—ค๋”๋ฅผ ๊ฐ€์ง€๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ—ค๋”์™€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ์ด ๋ฌธ์ž์—ด์€ ๊ฐ ํ•„๋“œ๋ช… ์•„๋ž˜์— "-" ๋ฌธ์ž๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด๋‹ค. ์˜ˆ: '---------- ---------- ---------- -----------' ์™„๋ฃŒ๋œ ํ”„๋กœ๊ทธ๋žจ์€ ์ด ์—ฐ์Šต์˜ ์ฒ˜์Œ์— ๋ณด์ธ ๊ฒƒ๊ณผ ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค. Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 ์—ฐ์Šต ๋ฌธ์ œ 2.12: ํฌ๋งคํŒ… ๋„์ „ ๊ฐ€๊ฒฉ์— ํ†ตํ™” ๊ธฐํ˜ธ($)๋ฅผ ํฌํ•จํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๊ฒŒ ์ˆ˜์ •ํ•ด ๋ณด๋ผ. Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 $9.22 -22.98 IBM 50 $106.28 15.18 CAT 150 $35.46 -47.98 MSFT 200 $20.89 -30.34 GE 95 $13.48 -26.89 MSFT 50 $20.89 -44.21 IBM 100 $106.28 35.84 2.4 ์‹œํ€€์Šค ์‹œํ€€์Šค ์ž๋ฃŒํ˜• ํŒŒ์ด์ฌ์—๋Š” ์„ธ ๊ฐ€์ง€ ์‹œํ€€์Šค(sequence) ์ž๋ฃŒํ˜•์ด ์žˆ๋‹ค. ๋ฌธ์ž์—ด: 'Hello'. ๋ฌธ์ž์—ด(string)์€ ๋ฌธ์ž(character)๋“ค์˜ ์‹œํ€€์Šค๋‹ค. ๋ฆฌ์ŠคํŠธ: [1, 4, 5]. ํŠœํ”Œ: ('GOOG', 100, 490.1). ๋ชจ๋“  ์‹œํ€€์Šค๋Š” ์ˆœ์„œ๊ฐ€ ์œ ์ง€๋˜๊ณ , ์ •์ˆ˜๋กœ ์ธ๋ฑ์‹ฑํ•˜๋ฉฐ, ๊ธธ์ด๊ฐ€ ์žˆ๋‹ค. a = 'Hello' # ๋ฌธ์ž์—ด b = [1, 4, 5] # ๋ฆฌ์ŠคํŠธ c = ('GOOG', 100, 490.1) # ํŠœํ”Œ # ์ธ๋ฑ์‹ฑ๋œ ์ˆœ์„œ a[0] # 'H' b[-1] # 5 c[1] # 100 # ์‹œํ€€์Šค์˜ ๊ธธ์ด len(a) # 5 len(b) # 3 len(c) # 3 ์‹œํ€€์Šค๋ฅผ ๋ณต์ œํ•  ์ˆ˜ ์žˆ๋‹ค(s * n). >>> a = 'Hello' >>> a * 3 'HelloHelloHello' >>> b = [1, 2, 3] >>> b * 2 [1, 2, 3, 1, 2, 3] >>> ๊ฐ™์€ ํƒ€์ž…์˜ ์‹œํ€€์Šค๋ผ๋ฆฌ ์ด์–ด๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค(s + t). >>> a = (1, 2, 3) >>> b = (4, 5) >>> a + b (1, 2, 3, 4, 5) >>> >>> c = [1, 5] >>> a + c Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate tuple (not "list") to tuple ์Šฌ๋ผ์ด์‹ฑ(Slicing) ์‹œํ€€์Šค์˜ ์ผ๋ถ€(subsequence)๋ฅผ ์ทจํ•˜๋Š” ๊ฒƒ์„ ์Šฌ๋ผ์ด์‹ฑ์ด๋ผ ํ•œ๋‹ค. s[start:end] ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค. start์™€ end๋Š” ์–ป๊ณ ์ž ํ•˜๋Š” ์„œ๋ธŒ ์‹œํ€€์Šค์˜ ์ธ๋ฑ์Šค๋‹ค. a = [0,1,2,3,4,5,6,7,8] a[2:5] # [2,3,4] a[-5:] # [4,5,6,7,8] a[:3] # [0,1,2] start์™€ end ์ธ๋ฑ์Šค๋Š” ๋ฐ˜๋“œ์‹œ ์ •์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค. ์Šฌ๋ผ์ด์Šค๋Š” end ๊ฐ’์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ˆ˜ํ•™์—์„œ ํ•œ์ชฝ๋งŒ ์—ด๋ฆฐ ๊ตฌ๊ฐ„(interval)๊ณผ ๋น„์Šทํ•˜๋‹ค. ์ธ๋ฑ์Šค๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘์ด๋‚˜ ๋์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์Šฌ๋ผ์ด์Šค ์žฌํ• ๋‹น(re-assignment) ๋ฆฌ์ŠคํŠธ์—์„œ ์Šฌ๋ผ์ด์Šค๋ฅผ ๋‹ค์‹œ ํ• ๋‹นํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. # ์žฌํ• ๋‹น a = [0,1,2,3,4,5,6,7,8] a[2:4] = [10,11,12] # [0,1,10,11,12,4,5,6,7,8] ์ฐธ๊ณ : ์žฌํ• ๋‹น๋œ ์Šฌ๋ผ์ด์Šค๋Š” ๊ธธ์ด๊ฐ€ ๋˜‘๊ฐ™์„ ํ•„์š”๊ฐ€ ์—†๋‹ค. # ์‚ญ์ œ a = [0,1,2,3,4,5,6,7,8] del a[2:4] # [0,1,4,5,6,7,8] ์‹œํ€€์Šค ์ถ•์†Œ(Reduction) ์‹œํ€€์Šค๋ฅผ ๋‹จ์ผ ๊ฐ’์œผ๋กœ ์ค„์ด๋Š” ๋ช‡ ๊ฐ€์ง€ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. >>> s = [1, 2, 3, 4] >>> sum(s) 10 >>> min(s) >>> max(s) >>> t = ['Hello', 'World'] >>> max(t) 'World' >>> ์‹œํ€€์Šค์— ๋Œ€ํ•œ ์ดํ„ฐ๋ ˆ์ด์…˜ for ๋ฃจํ”„๋Š” ์‹œํ€€์Šค์˜ ์›์†Œ์— ๋Œ€ํ•ด ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. >>> s = [1, 4, 9, 16] >>> for i in s: ... print(i) ... 4 16 >>> ๊ฐ ์ดํ„ฐ๋ ˆ์ด์…˜์—์„œ ์ƒˆ ํ•ญ๋ชฉ์„ ์–ป์–ด ์ž‘์—…ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒˆ ๊ฐ’์ด ์ดํ„ฐ๋ ˆ์ด์…˜ ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ๋‹ค. ์ด ์˜ˆ์—์„œ ์ดํ„ฐ๋ ˆ์ด์…˜ ๋ณ€์ˆ˜๋Š” x๋‹ค. for x in s: # `x`๋Š” ์ดํ„ฐ๋ ˆ์ด์…˜ ๋ณ€์ˆ˜ ...๋ฌธ์žฅ ๊ฐ ์ดํ„ฐ๋ ˆ์ด์…˜์—์„œ, ์ดํ„ฐ๋ ˆ์ด์…˜ ๋ณ€์ˆ˜์— ์žˆ๋Š” ์ด์ „ ๊ฐ’์„ ๋ฎ์–ด์“ด๋‹ค. ๋ฃจํ”„๊ฐ€ ๋๋‚œ ํ›„ ๋ณ€์ˆ˜๋Š” ์ตœ์ข… ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค. break ๋ฌธ break ๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋ฃจํ”„์—์„œ ์ผ์ฐ ๋น ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. for name in namelist: if name == 'Jake': break ... ... ๋ฌธ์žฅ ๋ฌธ์žฅ์˜ ์‹คํ–‰์„ break ํ•˜๋ฉด ๋ฃจํ”„์—์„œ ๋น ์ ธ๋‚˜๊ฐ€์„œ ๋‹ค์Œ ๋ฌธ์žฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. break ๋ฌธ์€ ๊ฐ€์žฅ ์•ˆ์ชฝ ๋ฃจํ”„์—๋งŒ ์ ์šฉ๋œ๋‹ค. ๋งŒ์•ฝ ์ด ๋ฃจํ”„๊ฐ€ ๋‹ค๋ฅธ ๋ฃจํ”„ ์•ˆ์— ์žˆ๋‹ค๋ฉด, ๋ฐ”๊นฅ์ชฝ ๋ฃจํ”„๋Š” ๋น ์ ธ๋‚˜์ง€ ์•Š๋Š”๋‹ค. continue ๋ฌธ ํ•œ ์›์†Œ๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋‹ค์Œ์œผ๋กœ ๊ฐ€๋ ค๋ฉด continue ๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค. for line in lines: if line == '\n': # ๊ณต๋ฐฑ ํ–‰์„ ๊ฑด๋„ˆ๋œ€ continue # ๋‚˜๋จธ์ง€ ๋ฌธ์žฅ๋“ค ... ํ˜„์žฌ ํ•ญ๋ชฉ์— ๊ด€์‹ฌ์ด ์—†๊ฑฐ๋‚˜ ์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ ๋ฌด์‹œํ•ด์•ผ ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. ์ •์ˆ˜์— ๋Œ€ํ•ด ๋ฃจํ•‘ ์นด์šดํŠธ๋ฅผ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด range()๋ฅผ ์จ๋ผ. for i in range(100): # i = 0,1, ...,99 ๊ตฌ๋ฌธ์€ range([start,] end [,step])์ด๋‹ค. for i in range(100): # i = 0,1, ...,99 for j in range(10,20): # j = 10,11, ..., 19 for k in range(10,50,2): # k = 10,12, ...,48 # step์ด 1์ด ์•„๋‹ˆ๋ผ 2์ผ ๋•Œ ์–ด๋–ค ์‹์œผ๋กœ ์นด์šดํŠธํ•˜๋Š”์ง€ ๋ˆˆ์—ฌ๊ฒจ ๋ณด๋ผ. end ๊ฐ’์€ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋Š” ์Šฌ๋ผ์ด์Šค์˜ ์ž‘๋™๊ณผ ๊ฐ™๋‹ค. start๋Š” ์„ ํƒ์ ์ด๊ณ , ๊ธฐ๋ณธ๊ฐ’์ด 0์ด๋‹ค. step๋„ ์„ ํƒ์ ์ด๋ฉฐ, ๊ธฐ๋ณธ๊ฐ’์ด 1์ด๋‹ค. range()๋Š” ํ•„์š”์— ๋”ฐ๋ผ ๊ฐ’์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์‹ค์ œ๋กœ ๋งŽ์€ ์ˆซ์ž๋ฅผ ์ €์žฅํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. enumerate() ํ•จ์ˆ˜ enumerate ํ•จ์ˆ˜๋Š” ์ดํ„ฐ๋ ˆ์ด์…˜์— ์นด์šดํ„ฐ ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค. names = ['Elwood', 'Jake', 'Curtis'] for i, name in enumerate(names): # i = 0, name = 'Elwood'๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฃจํ”„ # i = 1, name = 'Jake' # i = 2, name = 'Curtis' ์ผ๋ฐ˜์  ํ˜•ํƒœ๋Š” enumerate(์‹œํ€€์Šค [, start = 0])์ด๋‹ค. start๋Š” ์„ ํƒ์ ์ด๋‹ค. enumerate()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ข‹์€ ์˜ˆ๋Š” ํŒŒ์ผ์„ ์ฝ์œผ๋ฉด์„œ ํ–‰ ๋ฒˆํ˜ธ๋ฅผ ์ถ”์ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. with open(filename) as f: for lineno, line in enumerate(f, start=1): ... enumerate๋Š” ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ถ•์•ฝํ•˜๋Š” ์…ˆ์ด๋‹ค. i = 0 for x in s: ๋ฌธ์žฅ i += 1 enumerate๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒ€์ดํ•‘์„ ์ ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ณ  ์†๋„๋„ ์•ฝ๊ฐ„ ๋” ๋น ๋ฅด๋‹ค. for์™€ ํŠœํ”Œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ดํ„ฐ๋ ˆ์ด์…˜ ๋ณ€์ˆ˜(iteration variable)๋ฅผ ์‚ฌ์šฉํ•ด ๋ฃจํ•‘์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. points = [ (1, 4),(10, 40),(23, 14),(5, 6),(7, 8) ] for x, y in points: # x = 1, y = 4๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฃจํ•‘ # x = 10, y = 40 # x = 23, y = 14 # ... ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ๊ฐ ํŠœํ”Œ์€ ์ดํ„ฐ๋ ˆ์ด์…˜ ๋ณ€์ˆ˜์˜ ์„ธํŠธ๋กœ ์–ธํŒฉ(unpacked) ๋œ๋‹ค. ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋Š” ๊ฐ ํŠœํ”Œ์˜ ํ•ญ๋ชฉ ์ˆ˜์™€ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค. zip() ํ•จ์ˆ˜ zip ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ์‹œํ€€์Šค๋ฅผ ๊ฒฐํ•ฉํ•ด ์ดํ„ฐ ๋ ˆ์ดํ„ฐ(iterator)๋ฅผ ๋งŒ๋“ ๋‹ค. columns = ['name', 'shares', 'price'] values = ['GOOG', 100, 490.1 ] pairs = zip(columns, values) # ('name','GOOG'), ('shares',100), ('price',490.1) ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ ์ดํ„ฐ๋ ˆ์ด์…˜์„ ํ•ด์•ผ ํ•œ๋‹ค. ์•ž์„œ ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ํŠœํ”Œ์„ ์–ธํŒฉ ํ•  ์ˆ˜ ์žˆ๋‹ค. for column, value in pairs: ... zip์˜ ์ผ๋ฐ˜์ ์ธ ์šฉ๋„๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํ‚ค/๊ฐ’ ์Œ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. d = dict(zip(columns, values)) ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 2.13: ์นด์šดํŒ… ๊ธฐ๋ณธ์ ์ธ ์นด์šดํŒ…์„ ํ•ด ๋ณด์ž. >>> for n in range(10): # 0 ... 9๋ฅผ ์นด์šดํŠธ print(n, end=' ') 0 1 2 3 4 5 6 7 8 9 >>> for n in range(10,0, -1): # 10 ... 1์„ ์นด์šดํŠธ print(n, end=' ') 10 9 8 7 6 5 4 3 2 1 >>> for n in range(0,10,2): # 0, 2, ... 8์„ ์นด์šดํŠธ print(n, end=' ') 0 2 4 6 8 >>> ์—ฐ์Šต ๋ฌธ์ œ 2.14: ๋” ๋งŽ์€ ์‹œํ€€์Šค ์—ฐ์‚ฐ ์‹œํ€€์Šค ์ถ•์†Œ ์—ฐ์‚ฐ์„ ์ƒํ˜ธ์ž‘์šฉ์ ์œผ๋กœ ์‹คํ—˜ํ•ด ๋ณด์ž. >>> data = [4, 9, 1, 25, 16, 100, 49] >>> min(data) >>> max(data) 100 >>> sum(data) 204 >>> ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๋ฃจํ•‘์„ ํ•ด ๋ณด์ž. >>> for x in data: print(x) 9 ... >>> for n, x in enumerate(data): print(n, x) 0 4 1 9 2 1 ... >>> ํŒŒ์ด์ฌ ์ดˆ๋ณด์ž๋“ค์€ for ๋ฌธ, len(), range()๋ฅผ ์‚ฌ์šฉํ•ด C ํ”„๋กœ๊ทธ๋žจ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ๋”์ฐํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณค ํ•œ๋‹ค. >>> for n in range(len(data)): print(data[n]) 9 ... >>> ์ด๋Ÿฌ์ง€ ๋งˆ๋ผ! ๊ฐ€๋…์„ฑ๋„ ๋–จ์–ด์ง€๋Š” ๋ฐ๋‹ค ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋น„ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ ์‹คํ–‰ ์†๋„๋„ ๋Š๋ ค์ง„๋‹ค. ํ‰๋ฒ”ํ•œ for ๋ฃจํ”„๋ฅผ ์จ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ดํ„ฐ ๋ ˆ์ดํŠธ ํ•˜๋ผ. ์ธ๋ฑ์Šค๋ฅผ ์จ์•ผ ํ•  ํ•ฉ๋‹นํ•œ ์ด์œ ๊ฐ€ ์žˆ์„ ๋•Œ๋Š” enumerate()๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 2.15: ์‹ค์šฉ์ ์ธ enumerate() ์˜ˆ์ œ ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” Data/missing.csv์˜ ์ผ๋ถ€ ํ–‰์—๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋ˆ„๋ฝ๋˜์–ด ์žˆ๋‹ค. enumerate()๋ฅผ ์‚ฌ์šฉํ•ด pcost.py ํ”„๋กœ๊ทธ๋žจ์ด ์ž˜๋ชป๋œ ์ž…๋ ฅ์„ ๋งŒ๋‚˜๋ฉด ํ–‰ ๋ฒˆํ˜ธ์™€ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ํ”„๋ฆฐํŠธํ•˜๊ฒŒ ํ•ด ๋ณด๋ผ. >>> cost = portfolio_cost('Data/missing.csv') Row 4: Couldn't convert: ['MSFT', '', '51.23'] Row 7: Couldn't convert: ['IBM', '', '70.44'] >>> ์ด๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด ์ฝ”๋“œ๋ฅผ ๋ช‡ ๊ตฐ๋ฐ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. ... for rowno, row in enumerate(rows, start=1): try: ... except ValueError: print(f'Row {rowno}: Bad row: {row}') ์—ฐ์Šต ๋ฌธ์ œ 2.16: zip() ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ Data/portfolio.csv ํŒŒ์ผ์˜ ์ฒซ ํ–‰์—๋Š” ์นผ๋Ÿผ ํ—ค๋”๊ฐ€ ์žˆ๋‹ค. ์ด์ „ ์ฝ”๋“œ์—์„œ๋Š” ๊ทธ ๋ถ€๋ถ„์„ ๋ฒ„๋ ธ๋‹ค. >>> f = open('Data/portfolio.csv') >>> rows = csv.reader(f) >>> headers = next(rows) >>> headers ['name', 'shares', 'price'] >>> ํ—ค๋”๋ฅผ ์ž˜ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ข‹์ง€ ์•Š์„๊นŒ? zip() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ณด์ž. ๋จผ์ €, ํŒŒ์ผ ํ—ค๋”๋ฅผ ๋ฐ์ดํ„ฐ ํ–‰๊ณผ ์ง์„ ์ง“๋Š”๋‹ค. >>> row = next(rows) >>> row ['AA', '100', '32.20'] >>> list(zip(headers, row)) [ ('name', 'AA'), ('shares', '100'), ('price', '32.20') ] >>> zip()์ด ์–ด๋–ป๊ฒŒ ์นผ๋Ÿผ ํ—ค๋”์™€ ์นผ๋Ÿผ ๊ฐ’์˜ ์ง์„ ๋งŒ๋“œ๋Š”์ง€ ๋ˆˆ์—ฌ๊ฒจ ๋ณด๋ผ. ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด list()๋ฅผ ์‚ฌ์šฉํ–ˆ์œผ๋ฏ€๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. zip()์œผ๋กœ ์ดํ„ฐ ๋ ˆ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ for ๋ฃจํ”„์—์„œ ์†Œ๋น„ํ•˜๋Š” ๊ฒƒ์ด ๋ณดํ†ต์ด๋‹ค. ์ด๋Ÿฌํ•œ ์ง์ง“๊ธฐ๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์ค‘๊ฐ„ ๋‹จ๊ณ„๋‹ค. ์ด๋ฒˆ์—๋Š” ์ด๋ ‡๊ฒŒ ํ•ด ๋ณด์ž. >>> record = dict(zip(headers, row)) >>> record {'price': '32.20', 'name': 'AA', 'shares': '100'} >>> ์ด๋Ÿฌํ•œ ๋ณ€ํ™˜ ๋ฐฉ์‹์€ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ๋งŽ์ด ์ฒ˜๋ฆฌํ•  ๋•Œ ๋งค์šฐ ์œ ์šฉํ•œ ํŠธ๋ฆญ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, pcost.py ํ”„๋กœ๊ทธ๋žจ์ด ๋‹ค์–‘ํ•œ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•˜๋˜, ์ข…๋ชฉ๋ช…, ์ฃผ์‹ ์ˆ˜, ๊ฐ€๊ฒฉ์ด ๋‚˜ํƒ€๋‚˜๋Š” ์‹ค์ œ ์นผ๋Ÿผ ๋ฒˆํ˜ธ๋ฅผ ์ฐธ๊ณ ํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ํ•˜์ž. pcost.py์˜ portfolio_cost() ํ•จ์ˆ˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค. # pcost.py def portfolio_cost(filename): ... for rowno, row in enumerate(rows, start=1): record = dict(zip(headers, row)) try: nshares = int(record['shares']) price = float(record['price']) total_cost += nshares * price # ์œ„์˜ int()์™€ float() ๋ณ€ํ™˜ ์‹œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋ฅผ ์ฒ˜๋ฆฌ except ValueError: print(f'Row {rowno}: Bad row: {row}') ... ์ด์ œ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ํ•จ์ˆ˜์— ์ ์šฉํ•ด ๋ณด์ž. Data/portfoliodate.csv๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. name, date, time, shares, price "AA","6/11/2007","9:50am",100,32.20 "IBM","5/13/2007","4:20pm",50,91.10 "CAT","9/23/2006","1:30pm",150,83.44 "MSFT","5/17/2007","10:30am",200,51.23 "GE","2/1/2006","10:45am",95,40.37 "MSFT","10/31/2006","12:05pm",50,65.10 "IBM","7/9/2006","3:15pm",100,70.44 >>> portfolio_cost('Data/portfoliodate.csv') 44671.15 >>> ๋ฐ์ดํ„ฐ ํŒŒ์ผ์ด ์•ž์—์„œ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ์™„์ „ํžˆ ๋‹ฌ๋ผ์ง€๋”๋ผ๋„ ํ”„๋กœ๊ทธ๋žจ์ด ์—ฌ์ „ํžˆ ์ž‘๋™ํ•  ๊ฒƒ์ด๋‹ค. ๋ฉ‹์ง€์ง€ ์•Š์€๊ฐ€! ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ๋ฐ”๊พธ์ง€ ์•Š์•˜์ง€๋งŒ ๊ทธ ํšจ๊ณผ๋Š” ์ƒ๋‹นํ•˜๋‹ค. ์ƒˆ๋กœ์šด ๋ฒ„์ „์˜ portfolio_cost() ํ•จ์ˆ˜์—์„œ๋Š” ๊ณ ์ •๋œ ํŒŒ์ผ<NAME>์„ ํ•˜๋“œ์ฝ”๋”ฉํ•˜๋Š” ๋Œ€์‹ , ์–ด๋Š CSV ํŒŒ์ผ์ด ๋“ค์–ด์˜ค๋”๋ผ๋„ ์›ํ•˜๋Š” ๊ฐ’์„ ๊ณจ๋ผ๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ๋๋‹ค. ํ•„์š”ํ•œ ์นผ๋Ÿผ์ด ํŒŒ์ผ์— ์žˆ๊ธฐ๋งŒ ํ•˜๋ฉด ์ฝ”๋“œ๋Š” ์ž˜ ์ž‘๋™ํ•œ๋‹ค. ์„น์…˜ 2.3์—์„œ ์ž‘์„ฑํ•œ report.py ํ”„๋กœ๊ทธ๋žจ์—, ์นผ๋Ÿผ ํ—ค๋”๋ฅผ ์„ ํƒํ•˜๋Š” ๊ธฐ์ˆ ์„ ์ ์šฉํ•ด ๋ณด์ž. Data/portfoliodate.csv ํŒŒ์ผ์— ๋Œ€ํ•ด report.py ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•ด, ์ด์ „๊ณผ ๊ฐ™์€ ๋‹ต์„ ๋งŒ๋“ค์–ด๋‚ด๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 2.17: ๋”•์…”๋„ˆ๋ฆฌ ๋’ค์ง‘๊ธฐ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ‚ค๋ฅผ ๊ฐ’์— ๋งค ํŒฝํ•œ๋‹ค. ๋‹ค์Œ์€ ์ฃผ์‹ ๊ฐ€๊ฒฉ ๋”•์…”๋„ˆ๋ฆฌ์˜ ์˜ˆ๋‹ค. >>> prices = { 'GOOG' : 490.1, 'AA' : 23.45, 'IBM' : 91.1, 'MSFT' : 34.23 } >>> items() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด (ํ‚ค, ๊ฐ’) ์Œ์„ ์–ป๋Š”๋‹ค. >>> prices.items() dict_items([('GOOG', 490.1), ('AA', 23.45), ('IBM', 91.1), ('MSFT', 34.23)]) >>> ๊ทธ๋Ÿฐ๋ฐ, ๋งŒ์•ฝ (๊ฐ’, ํ‚ค)์˜ ์Œ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? ํžŒํŠธ: zip()์„ ์‚ฌ์šฉํ•œ๋‹ค. >>> pricelist = list(zip(prices.values(),prices.keys())) >>> pricelist [(490.1, 'GOOG'), (23.45, 'AA'), (91.1, 'IBM'), (34.23, 'MSFT')] >>> ์ด๋ ‡๊ฒŒ ํ•ด์•ผ ํ•  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ? ๋”•์…”๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ํŠน์ •ํ•œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ํ•œ ๊ฐ€์ง€ ์ด์œ ๋กœ ๋“ค ์ˆ˜ ์žˆ๋‹ค. >>> min(pricelist) (23.45, 'AA') >>> max(pricelist) (490.1, 'GOOG') >>> sorted(pricelist) [(23.45, 'AA'), (34.23, 'MSFT'), (91.1, 'IBM'), (490.1, 'GOOG')] >>> ๋˜ํ•œ ์ด๊ฒƒ์€ ํŠœํ”Œ์˜ ์ค‘์š” ๊ธฐ๋Šฅ์„ ๋ณด์—ฌ์ค€๋‹ค. ํŠœํ”Œ์„ ๋น„๊ตํ•˜๋ฉด, ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ๋ถ€ํ„ฐ ์›์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๋น„๊ตํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋Š” ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๋‹ค. zip()์€ ์ด์™€ ๊ฐ™์ด ์„œ๋กœ ๋‹ค๋ฅธ ์ž๋ฆฌ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์ง์„ ์ง€์–ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์— ์ข…์ข… ์‚ฌ์šฉ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ช…๋ช…๋œ ๊ฐ’์˜ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด, ์นผ๋Ÿผ๋ช…๊ณผ ์นผ๋Ÿผ ๊ฐ’์˜ ์Œ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ฐธ๊ณ ๋กœ, zip()์œผ๋กœ 1:1 ์Œ๋งŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž…๋ ฅ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ ๋„ ๊ทธ๋Ÿฐ ์ผ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. >>> a = [1, 2, 3, 4] >>> b = ['w', 'x', 'y', 'z'] >>> c = [0.2, 0.4, 0.6, 0.8] >>> list(zip(a, b, c)) [(1, 'w', 0.2), (2, 'x', 0.4), (3, 'y', 0.6), (4, 'z', 0.8))] >>> ๋˜ํ•œ, zip()์€ ๊ฐ€์žฅ ์งง์€ ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ์—์„œ ๋ฉˆ์ถ˜๋‹ค. >>> a = [1, 2, 3, 4, 5, 6] >>> b = ['x', 'y', 'z'] >>> list(zip(a, b)) [(1, 'x'), (2, 'y'), (3, 'z')] >>> 2.5 collections ๋ชจ๋“ˆ collections ๋ชจ๋“ˆ์—๋Š” ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์œ ์šฉํ•œ ๊ฐ์ฒด๊ฐ€ ๋งŽ์ด ์žˆ๋‹ค. ๊ทธ์ค‘ ๋ช‡ ๊ฐ€์ง€๋ฅผ ๊ฐ„๋žตํžˆ ์†Œ๊ฐœํ•œ๋‹ค. ์˜ˆ: ์นด์šดํŠธํ•˜๊ธฐ ๋ณด์œ ํ•œ ์ฃผ์‹์ด ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๋•Œ, ์ข…๋ชฉ๋ณ„๋กœ ํ•ฉ์‚ฐํ•ด ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ๋‹ค๊ณ  ํ•˜์ž. portfolio = [ ('GOOG', 100, 490.1), ('IBM', 50, 91.1), ('CAT', 150, 83.44), ('IBM', 100, 45.23), ('GOOG', 75, 572.45), ('AA', 50, 23.15) ] ์œ„ ๋ฆฌ์ŠคํŠธ์— IBM๊ณผ GOOG๊ฐ€ ๋‘ ๊ฐœ์”ฉ ์žˆ๋‹ค. ์ข…๋ชฉ๋ณ„๋กœ ํ•ฉ์‚ฐํ•ด ๋ณด์ž. Counter ํ•ด๋ฒ•: Counter๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. from collections import Counter total_shares = Counter() for name, shares, price in portfolio: total_shares[name] += shares total_shares['IBM'] # 150 ์˜ˆ: ์ผ๋Œ€๋‹ค(One-Many) ๋งคํ•‘ ๋ฌธ์ œ: ํ•˜๋‚˜์˜ ํ‚ค๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์— ๋งคํ•‘ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. portfolio = [ ('GOOG', 100, 490.1), ('IBM', 50, 91.1), ('CAT', 150, 83.44), ('IBM', 100, 45.23), ('GOOG', 75, 572.45), ('AA', 50, 23.15) ] ์•ž์˜ ์˜ˆ์™€ ๊ฐ™์ด, IBM์„ ํ‚ค๋กœ ์‚ผ์œผ๋ฉด ๋‘ ๊ฐœ์˜ ํŠœํ”Œ์ด ์žˆ๋‹ค. ํ•ด๋ฒ•: defaultdict๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. from collections import defaultdict holdings = defaultdict(list) for name, shares, price in portfolio: holdings[name].append((shares, price)) holdings['IBM'] # [ (50, 91.1), (100, 45.23) ] defaultdict์„ ์‚ฌ์šฉํ•˜๋ฉด ํ‚ค์— ์•ก์„ธ์Šคํ•  ๋•Œ๋งˆ๋‹ค ๊ธฐ๋ณธ๊ฐ’์„ ์–ป๋Š”๋‹ค. ์˜ˆ: ์ด๋ ฅ์„ ์œ ์ง€ํ•˜๊ธฐ ๋ฌธ์ œ: ๋งˆ์ง€๋ง‰ N ๊ฐœ์˜ ์ด๋ ฅ(history)์„ ์œ ์ง€ํ•˜๊ณ  ์‹ถ๋‹ค. ํ•ด๋ฒ•: deque๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. from collections import deque history = deque(maxlen=N) with open(filename) as f: for line in f: history.append(line) ... ์—ฐ์Šต ๋ฌธ์ œ collections ๋ชจ๋“ˆ์€ ๊ฐ€์žฅ ์œ ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ ์ค‘ ํ•˜๋‚˜๋กœ, ํ‘œ์™€ ์ธ๋ฑ์‹ฑ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ํŠนํžˆ ์œ ์šฉํ•˜๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด์ž. report.py ํ”„๋กœ๊ทธ๋žจ์„ ์ƒํ˜ธ์ž‘์šฉ ๋ชจ๋“œ๋กœ ์‹คํ–‰ํ•ด ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์ ์žฌํ•˜์ž. bash % python3 -i report.py ์—ฐ์Šต ๋ฌธ์ œ 2.18: Counter๋กœ ํ‘œ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ ๊ฐ ์ข…๋ชฉ์˜ ์ด ์ฃผ์‹ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ‘œ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๊ณ  ํ•˜์ž. Counter ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•œ๋ฒˆ ํ•ด ๋ณด์ž. >>> portfolio = read_portfolio('Data/portfolio.csv') >>> from collections import Counter >>> holdings = Counter() >>> for s in portfolio: holdings[s['name']] += s['shares'] >>> holdings Counter({'MSFT': 250, 'IBM': 150, 'CAT': 150, 'AA': 100, 'GE': 95}) >>> portfolio์˜ MSFT์™€ IBM์„ ์–ด๋–ป๊ฒŒ ํ•œ๊ณณ์— ๋ชจ์•˜๋Š”์ง€ ์ฃผ์˜ ๊นŠ๊ฒŒ ๊ด€์ฐฐํ•˜๋ผ. Counter๋ฅผ ์‚ฌ์ „์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•ด, ๊ฐ๊ฐ์˜ ๊ฐ’์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. >>> holdings['IBM'] 150 >>> holdings['MSFT'] 250 >>> ์ˆœ์œ„๋ฅผ ๋งค๊ธฐ๊ณ  ์‹ถ์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•œ๋‹ค. >>> # ๊ฐ€์žฅ ๋งŽ์ด ๋ณด์œ ํ•œ ์ข…๋ชฉ 3๊ฐ€์ง€ >>> holdings.most_common(3) [('MSFT', 250), ('IBM', 150), ('CAT', 150)] >>> ๋‹ค๋ฅธ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๊ฐ€์ง€๊ณ  ์ƒˆ๋กœ์šด Counter๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž. >>> portfolio2 = read_portfolio('Data/portfolio2.csv') >>> holdings2 = Counter() >>> for s in portfolio2: holdings2[s['name']] += s['shares'] >>> holdings2 Counter({'HPQ': 250, 'GE': 125, 'AA': 50, 'MSFT': 25}) >>> ๋ชจ๋“  ๋ณด์œ  ์ข…๋ชฉ์„ ๊ฐ„๋‹จํ•œ ์—ฐ์‚ฐ ํ•˜๋‚˜๋กœ ๊ฒฐํ•ฉํ•ด ๋ณด์ž. >>> holdings Counter({'MSFT': 250, 'IBM': 150, 'CAT': 150, 'AA': 100, 'GE': 95}) >>> holdings2 Counter({'HPQ': 250, 'GE': 125, 'AA': 50, 'MSFT': 25}) >>> combined = holdings + holdings2 >>> combined Counter({'MSFT': 275, 'HPQ': 250, 'GE': 220, 'AA': 150, 'IBM': 150, 'CAT': 150}) >>> Counter์˜ ๋Šฅ๋ ฅ์„ ์•„์ฃผ ์กฐ๊ธˆ๋งŒ ๋ง›๋ณด์•˜๋‹ค. ํ‘œ ํ˜•ํƒœ์˜ ๊ฐ’์„ ๋‹ค๋ฃฐ ์ผ์ด ์žˆ์„ ๋•Œ, ์ด๊ฒƒ์„ ์‚ฌ์šฉํ• ์ง€ ๊ณ ๋ คํ•ด ๋ณด๋ผ. ๋ถ€์—ฐ ์„ค๋ช…: collections ๋ชจ๋“ˆ collections ๋ชจ๋“ˆ์€ ํŒŒ์ด์ฌ์—์„œ ๊ฐ€์žฅ ์œ ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ ์ค‘ ํ•˜๋‚˜๋‹ค. ์‚ฌ์‹ค ๊ทธ๊ฒƒ์„ ์ข€ ๋” ์„ค๋ช…ํ•  ์ˆ˜๋„ ์žˆ์—ˆ์ง€๋งŒ, ์ง€๊ธˆ์€ ์ฃผ์˜๊ฐ€ ๋ถ„์‚ฐ๋  ์ˆ˜ ์žˆ์–ด ๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์•˜๋‹ค. ๋‚˜์ค‘์— ํ•œ๊ฐ€ํ•  ๋•Œ collections์— ๋Œ€ํ•ด ์ฝ์–ด๋ณด๋ผ. 2.6 ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌ ํ—จ ์…˜ ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์ธ ์ž‘์—…์ด๋‹ค. ์ด ์„น์…˜์€ ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ์ธ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌ ํ—จ ์…˜(list comprehension)์„ ์†Œ๊ฐœํ•œ๋‹ค. ์ƒˆ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑํ•˜๊ธฐ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์€ ์‹œํ€€์Šค์˜ ๊ฐ ์›์†Œ์— ์—ฐ์‚ฐ์„ ์ ์šฉํ•จ์œผ๋กœ์จ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. >>> a = [1, 2, 3, 4, 5] >>> b = [2*x for x in a ] >>> b [2, 4, 6, 8, 10] >>> ๋‹ค๋ฅธ ์˜ˆ: >>> names = ['Elwood', 'Jake'] >>> a = [name.lower() for name in names] >>> a ['elwood', 'jake'] >>> ์ผ๋ฐ˜์ ์ธ ๊ตฌ๋ฌธ: [ <ํ‘œํ˜„์‹> for <๋ณ€์ˆ˜๋ช…> in <์‹œํ€€์Šค> ]. ํ•„ํ„ฐ๋ง ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ํ†ตํ•ด ์›์†Œ๋ฅผ ๊ฑฐ๋ฅผ ์ˆ˜ ์žˆ๋‹ค. >>> a = [1, -5, 4, 2, -2, 10] >>> b = [2*x for x in a if x > 0 ] >>> b [2, 8, 4, 20] >>> ์šฉ๋ก€ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์€ ์—„์ฒญ๋‚˜๊ฒŒ ์œ ์šฉํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํŠน์ • ๋”•์…”๋„ˆ๋ฆฌ ํ•„๋“œ์˜ ๊ฐ’์„ ์ˆ˜์ง‘ํ•  ์ˆ˜ ์žˆ๋‹ค. stocknames = [s['name'] for s in stocks] ์‹œํ€€์Šค์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์งˆ์˜์™€ ๋น„์Šทํ•œ ๊ฒƒ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. a = [s for s in stocks if s['price'] > 100 and s['shares'] > 50 ] ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜๊ณผ ์‹œํ€€์Šค ์ถ•์†Œ๋ฅผ ์กฐํ•ฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. cost = sum([s['shares']*s['price'] for s in stocks]) ์ผ๋ฐ˜์ ์ธ ๊ตฌ๋ฌธ [ <ํ‘œํ˜„์‹> for <๋ณ€์ˆ˜๋ช…> in <์‹œํ€€์Šค> if <์กฐ๊ฑด>] ์ด๊ฒƒ์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. result = [] for ๋ณ€์ˆ˜๋ช… in ์‹œํ€€์Šค: if ์กฐ๊ฑด: result.append(ํ‘œํ˜„์‹) ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์˜ ์œ ๋ž˜ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์€ ์ˆ˜ํ•™(์กฐ๊ฑด์ œ์‹œ๋ฒ•)์—์„œ ์œ ๋ž˜ํ–ˆ๋‹ค. ํŒŒ์ด์ฌ: a = [ x * x for x in s if x > 0 ] ์ˆ˜ํ•™: ์ด ๊ธฐ๋Šฅ์€ ๋ช‡๋ช‡ ์–ธ์–ด์— ๊ตฌํ˜„๋ผ ์žˆ๋‹ค. ์ฝ”๋” ๋Œ€๋ถ€๋ถ„์€ ์ˆ˜ํ•™ ์‹œ๊ฐ„์— ๋ฐฐ์šด ๊ฒƒ๊นŒ์ง€ ๋– ์˜ฌ๋ฆฌ์ง€๋Š” ์•Š์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ, ๋ฆฌ์ŠคํŠธ๋ฅผ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์ฟจํ•œ ๊ตฌ๋ฌธ์œผ๋กœ ๋ฐ›์•„๋“ค์—ฌ๋„ ์ข‹๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ report.py ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•ด ์ƒํ˜ธ์ž‘์šฉ ๋ชจ๋“œ์—์„œ ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์ ์žฌํ•œ๋‹ค. bash % python3 -i report.py ์ด์ œ, ํŒŒ์ด์ฌ ์ƒํ˜ธ์ž‘์šฉ ํ”„๋กฌํ”„ํŠธ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์„ ํƒ€์ดํ•‘ํ•œ๋‹ค. ์ด ์—ฐ์‚ฐ๋“ค์€ ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๋ฐ์ดํ„ฐ ์ถ•์†Œ(reduction), ๋ณ€ํ™˜(transform), ์งˆ์˜๋ฅผ ๋‹ค์–‘ํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 2.19: ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌ ํ—จ ์…˜ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌ ํ—จ ์…˜ ๊ตฌ๋ฌธ์— ์ต์ˆ™ํ•ด์ง€๋„๋ก ์—ฐ์Šตํ•ด ๋ณด์ž. >>> nums = [1,2,3,4] >>> squares = [ x * x for x in nums ] >>> squares [1, 4, 9, 16] >>> twice = [ 2 * x for x in nums if x > 2 ] >>> twice [6, 8] >>> ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ํ†ตํ•ด ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋™์‹œ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ ๋ฐ ํ•„ํ„ฐ๋งํ–ˆ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 2.20: ์‹œํ€€์Šค ์ถ•์†Œ ๋‹จ ํ•˜๋‚˜์˜ ํŒŒ์ด์ฌ ๋ฌธ์žฅ์„ ์‚ฌ์šฉํ•ด ํฌํŠธํด๋ฆฌ์˜ค์˜ ์ด๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜์ž. >>> portfolio = read_portfolio('Data/portfolio.csv') >>> cost = sum([ s['shares'] * s['price'] for s in portfolio ]) >>> cost 44671.15 >>> ๋”˜ ์ผ ๋ฌธ์žฅ์œผ๋กœ ํฌํŠธํด๋ฆฌ์˜ค์˜ ํ˜„์žฌ ๊ฐ€๊ฒฉ๋„ ๊ณ„์‚ฐํ•ด ๋ณด์ž. >>> value = sum([ s['shares'] * prices[s['name']] for s in portfolio ]) >>> value 28686.1 >>> ์œ„์˜ ๋‘ ์—ฐ์‚ฐ์€ ๋ชจ๋‘ ๋งต ์ถ•์†Œ(map-reduction)์˜ ์˜ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์€ ๋ฆฌ์ŠคํŠธ์— ๊ฑธ์นœ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด ๋งคํ•‘ํ•œ๋‹ค. >>> [ s['shares'] * s['price'] for s in portfolio ] [3220.0000000000005, 4555.0, 12516.0, 10246.0, 3835.1499999999996, 3254.9999999999995, 7044.0] >>> sum() ํ•จ์ˆ˜๋Š” ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ์ถ•์†Œ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. >>> sum(_) 44671.15 >>> ์ด ์ง€์‹์„ ๊ฐ€์ง€๊ณ , ๋‹น์‹ ์€ ๋น…๋ฐ์ดํ„ฐ ์Šคํƒ€ํŠธ์—… ํšŒ์‚ฌ๋ฅผ ์‹œ์ž‘ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 2.21: ๋ฐ์ดํ„ฐ ์งˆ์˜ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์งˆ์˜์˜ ์˜ˆ๋ฅผ ๋”ฐ๋ผ ํ•ด ๋ณด์ž. ๋จผ์ €, ํฌํŠธํด๋ฆฌ์˜ค์—์„œ 100์ฃผ ์ด์ƒ ๋ณด์œ ํ•œ ์ข…๋ชฉ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฝ‘์•„๋ณด์ž. >>> more100 = [ s for s in portfolio if s['shares'] > 100 ] >>> more100 [{'price': 83.44, 'name': 'CAT', 'shares': 150}, {'price': 51.23, 'name': 'MSFT', 'shares': 200}] >>> ๋‹ค์Œ์€ ๋ณด์œ ํ•œ MSFT์™€ IBM ์ฃผ์‹ ์ „์ฒด ๋ฆฌ์ŠคํŠธ๋‹ค. >>> msftibm = [ s for s in portfolio if s['name'] in {'MSFT','IBM'} ] >>> msftibm [{'price': 91.1, 'name': 'IBM', 'shares': 50}, {'price': 51.23, 'name': 'MSFT', 'shares': 200}, {'price': 65.1, 'name': 'MSFT', 'shares': 50}, {'price': 70.44, 'name': 'IBM', 'shares': 100}] >>> ๋‹ค์Œ์€ ๋ณด์œ  ์ข…๋ชฉ ์ค‘ ๊ฐ€๊ฒฉ์ด 10,000๋‹ฌ๋Ÿฌ๊ฐ€ ๋„˜๋Š” ๊ฒƒ์˜ ๋ฆฌ์ŠคํŠธ๋‹ค. >>> cost10k = [ s for s in portfolio if s['shares'] * s['price'] > 10000 ] >>> cost10k [{'price': 83.44, 'name': 'CAT', 'shares': 150}, {'price': 51.23, 'name': 'MSFT', 'shares': 200}] >>> ์—ฐ์Šต ๋ฌธ์ œ 2.22: ๋ฐ์ดํ„ฐ ์ถ”์ถœ portfolio์—์„œ ์–ป์€ name๊ณผ shares๋ฅผ ๊ฐ€์ง€๊ณ  ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ (name, shares)๋ฅผ ๋งŒ๋“ค์ž. >>> name_shares =[ (s['name'], s['shares']) for s in portfolio ] >>> name_shares [('AA', 100), ('IBM', 50), ('CAT', 150), ('MSFT', 200), ('GE', 95), ('MSFT', 50), ('IBM', 100)] >>> ๋Œ€๊ด„ํ˜ธ([,])๋ฅผ ์ค‘๊ด„ํ˜ธ({, })๋กœ ๋ฐ”๊พธ๋ฉด ์„ธํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์ด ๋œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” ๊ณ ์œ ํ•œ ๊ฐ’๋“ค์˜ ์„ธํŠธ๊ฐ€ ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ์€ portfolio์—์„œ ๊ณ ์œ ํ•œ ์ข…๋ชฉ ๋ช…์˜ ์„ธํŠธ๋ฅผ ๊ตฌํ•œ๋‹ค. >>> names = { s['name'] for s in portfolio } >>> names { 'AA', 'GE', 'IBM', 'MSFT', 'CAT' } >>> key:value ์Œ์„ ์ง€์ •ํ•˜๋ฉด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ์ข…๋ชฉ๋ช…์„ ๋ณด์œ  ์ฃผ์‹ ์ˆ˜์™€ ๋งคํ•‘ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š” ์˜ˆ๋‹ค. >>> holdings = { name: 0 for name in names } >>> holdings {'AA': 0, 'GE': 0, 'IBM': 0, 'MSFT': 0, 'CAT': 0} >>> ์ด ๊ธฐ๋Šฅ์„ ๋”•์…”๋„ˆ๋ฆฌ ์ปดํ”„๋ฆฌ ํ—จ ์…˜(dictionary comprehension)์ด๋ผ ํ•œ๋‹ค. ํ…Œ์ด๋ธ”๋กœ ๋‚˜ํƒ€๋‚ด์ž. >>> for s in portfolio: holdings[s['name']] += s['shares'] >>> holdings { 'AA': 100, 'GE': 95, 'IBM': 150, 'MSFT':250, 'CAT': 150 } >>> prices ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ•ญ๋ชฉ ์ค‘ ํฌํŠธํด๋ฆฌ์˜ค์— ํฌํ•จ๋œ ์ข…๋ชฉ๋งŒ ์ถ”๋ ค๋ณด์ž. >>> portfolio_prices = { name: prices[name] for name in names } >>> portfolio_prices {'AA': 9.22, 'GE': 13.48, 'IBM': 106.28, 'MSFT': 20.89, 'CAT': 35.46} >>> ์—ฐ์Šต ๋ฌธ์ œ 2.23: CSV ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๊ธฐ ๋ฆฌ์ŠคํŠธ, ์„ธํŠธ, ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋‹ค์–‘ํ•˜๊ฒŒ ์กฐํ•ฉํ•ด ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. CSV ํŒŒ์ผ์—์„œ ์„ ํƒ๋œ ์นผ๋Ÿผ์„ ์ถ”์ถœํ•˜๋Š” ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์ž. ๋จผ์ €, CSV ํŒŒ์ผ์—์„œ ํ—ค๋” ์ •๋ณด๊ฐ€ ์žˆ๋Š” ํ–‰์„ ์ฝ๋Š”๋‹ค. >>> import csv >>> f = open('Data/portfoliodate.csv') >>> rows = csv.reader(f) >>> headers = next(rows) >>> headers ['name', 'date', 'time', 'shares', 'price'] >>> ๋‹ค์Œ์œผ๋กœ, ์‹ค์ œ ๋ฐ์ดํ„ฐ ์นผ๋Ÿผ์„ ๋‚˜์—ดํ•˜๋Š” ๋ณ€์ˆ˜๋ช…์„ ์ •์˜ํ•œ๋‹ค. >>> select = ['name', 'shares', 'price'] >>> ์ด์ œ, ์ž…๋ ฅ CSV ํŒŒ์ผ์—์„œ ์นผ๋Ÿผ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š”๋‹ค. >>> indices = [ headers.index(colname) for colname in select ] >>> indices [0, 3, 4] >>> ๋์œผ๋กœ, ๋ฐ์ดํ„ฐ์˜ ํ–‰์„ ์ฝ์–ด ๋”•์…”๋„ˆ๋ฆฌ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค. >>> row = next(rows) >>> record = { colname: row[index] for colname, index in zip(select, indices) } # ๋”•์…”๋„ˆ๋ฆฌ ์ปดํ”„๋ฆฌ ํ—จ ์…˜ >>> record {'price': '32.20', 'name': 'AA', 'shares': '100'} >>> ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚ฌ๋Š”์ง€ ์ดํ•ดํ–ˆ์œผ๋ฉด, ํŒŒ์ผ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„๋„ ์ฝ์–ด๋ณด์ž. >>> portfolio = [ { colname: row[index] for colname, index in zip(select, indices) } for row in rows ] >>> portfolio [{'price': '91.10', 'name': 'IBM', 'shares': '50'}, {'price': '83.44', 'name': 'CAT', 'shares': '150'}, {'price': '51.23', 'name': 'MSFT', 'shares': '200'}, {'price': '40.37', 'name': 'GE', 'shares': '95'}, {'price': '65.10', 'name': 'MSFT', 'shares': '50'}, {'price': '70.44', 'name': 'IBM', 'shares': '100'}] >>> ์„ธ์ƒ์—, read_portfolio() ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์ค„์˜€๋‹ค. ๋ถ€์—ฐ ์„ค๋ช… ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์€ ํŒŒ์ด์ฌ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜, ํ•„ํ„ฐ๋ง, ์ˆ˜์ง‘ํ•˜๋Š” ๋ฐ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ๊ทธ ๊ตฌ๋ฌธ์ด ๋ณต์žกํ•˜๋ฏ€๋กœ, ๊ฐ€๋Šฅํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ๋‹จ์ˆœํ•˜๊ฒŒ ์œ ์ง€ํ•˜๋ผ. ์—ฌ๋Ÿฌ ๋‹จ๊ณ„์˜ ์ฝ”๋“œ๋กœ ๋‚˜๋ˆ  ๊ตฌํ˜„ํ•ด๋„ ๊ดœ์ฐฎ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋งˆ์ง€๋ง‰ ์˜ˆ์ œ๋ฅผ ๋‹น์‹ ์˜ ์ˆœ์ง„ํ•œ ๋™๋ฃŒ์—๊ฒŒ ๋ถˆ์‘ฅ ๋‚ด๋ฐ€์–ด๋„ ๊ดœ์ฐฎ์„์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์–ด์จŒ๋“ , ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์กฐ์ž‘ํ•˜๋Š” ๋ฒ•์„ ์•Œ์•„๋‘๋ฉด ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค. ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ, ๋‚ด๋ณด๋‚ด๊ธฐ, ์ถ”์ถœ ๋“ฑ ์ผํšŒ์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋งŽ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์ˆ™๋‹ฌํ•˜๋ฉด ํ•ด๊ฒฐ์ฑ…์„ ๊ณ ์•ˆํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์ƒ๋‹นํžˆ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. collections ๋ชจ๋“ˆ๋„ ์žŠ์ง€ ๋ง์ž. 2.7 ๊ฐ์ฒด ์ด ์„น์…˜์€ ํŒŒ์ด์ฌ์˜ ๋‚ด๋ถ€ ๊ฐ์ฒด ๋ชจ๋ธ์„ ์ž์„ธํžˆ ์†Œ๊ฐœํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ, ๋ณต์‚ฌ, ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ๋…ผ์˜ํ•œ๋‹ค. ํ• ๋‹น(Assignment) ํŒŒ์ด์ฌ์˜ ๋งŽ์€ ์—ฐ์‚ฐ์ž๋Š” ๊ฐ’์˜ ํ• ๋‹น(assigning) ํ˜น์€ ์ €์žฅ(storing)๊ณผ ๊ด€๋ จ์ด ์žˆ๋‹ค. a = value # ๋ณ€์ˆ˜์— ํ• ๋‹น s[n] = value # ๋ฆฌ์ŠคํŠธ์— ํ• ๋‹น s.append(value) # ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ d['key'] = value # ๋”•์…”๋„ˆ๋ฆฌ์— ์ถ”๊ฐ€ ์ฃผ์˜: ํ• ๋‹น ์—ฐ์‚ฐ์€ ํ• ๋‹น๋  ๊ฐ’์˜ ์‚ฌ๋ณธ์„ ์ ˆ๋Œ€ ๋งŒ๋“ค์ง€ ์•Š๋Š”๋‹ค. ๋ชจ๋“  ํ• ๋‹น์€ ๋ ˆํผ๋Ÿฐ์Šค ๋ณต์‚ฌ(ํ˜น์€ ํฌ์ธํ„ฐ ๋ณต์‚ฌ) ์ผ๋ฟ์ด๋‹ค. ํ• ๋‹น์˜ ์˜ˆ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. a = [1,2,3] b = a c = [a, b] ๋ฉ”๋ชจ๋ฆฌ ์—ฐ์‚ฐ์˜ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค ๋ณด์ž. ์ด ์˜ˆ์—๋Š” ๋‹จ ํ•œ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด [1,2,3]์ด ์žˆ์ง€๋งŒ, ๊ทธ๊ฒƒ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ ˆํผ๋Ÿฐ์Šค(reference)๋Š” 4๊ฐœ๊ฐ€ ์žˆ๋‹ค. ์ด๊ฒƒ์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋ฉด ๋ชจ๋“  ๋ ˆํผ๋Ÿฐ์Šค์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. >>> a.append(999) >>> a [1,2,3,999] >>> b [1,2,3,999] >>> c [[1,2,3,999], [1,2,3,999]] >>> ์›๋ž˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜์ž ๋ชจ๋“  ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฐ”๋€Œ์–ด ๋ฒ„๋ ธ๋‹ค(์œผ์•…!). ์‚ฌ๋ณธ์„ ๋งŒ๋“ค์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ชจ๋“  ๊ฒƒ์ด ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์—ˆ๋˜ ๊ฒƒ์ด๋‹ค. ๊ฐ’์„ ์žฌํ• ๋‹น ๊ฐ’์„ ์žฌํ• ๋‹นํ•œ๋‹ค๊ณ  ํ•ด์„œ, ์ด์ „ ๊ฐ’์—์„œ ์‚ฌ์šฉํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ฎ์–ด์“ฐ๋Š” ๊ฒƒ์ด ์ ˆ๋Œ€ ์•„๋‹ˆ๋‹ค. a = [1,2,3] b = a a = [4,5,6] print(a) # [4, 5, 6] print(b) # [1, 2, 3] ์›๋ž˜ ๊ฐ’์ด ์žˆ์Œ ๊ธฐ์–ตํ•ด ์ค˜: ๋ณ€์ˆ˜๋Š” ์ด๋ฆ„์ผ ๋ฟ, ๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ์œ„ํ—˜์„ฑ ์ด ๊ณต์œ  ๋ฐฉ์‹์„ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๋ฉด ์ž๊ธฐ ๋ฐœ๋“ฑ์„ ์ฐ๋Š” ์ผ์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ์‹œ๋‚˜๋ฆฌ์˜ค. ์–ด๋–ค ๋ฐ์ดํ„ฐ๊ฐ€ ํ”„๋ผ์ด๋น—(private) ์‚ฌ๋ณธ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ์ˆ˜์ •ํ–ˆ๋‹ค๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์˜ค์—ผ์‹œํ‚ค๋Š” ์‚ฌ๊ณ ๊ฐ€ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋‹ค. ์„ค๋ช…: ์ด๊ฒƒ์ด ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•(int, float, string)์ด ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ(์ฝ๊ธฐ ์ „์šฉ)์ธ ํ•œ ๊ฐ€์ง€ ์ด์œ ๋‹ค. id์™€ ๋ ˆํผ๋Ÿฐ์Šค is ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ๋‘ ๊ฐ’์ด ์ •ํ™•ํžˆ ๊ฐ™์€ ๊ฐ์ฒด์ธ์ง€ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค. >>> a = [1,2,3] >>> b = a >>> a is b True >>> is๋Š” ๊ฐ์ฒด์˜ id(์ •์ˆ˜)๋ฅผ ๋น„๊ตํ•œ๋‹ค. id๋Š” id()๋ฅผ ์‚ฌ์šฉํ•ด ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. >>> id(a) 3588944 >>> id(b) 3588944 >>> ์ฐธ๊ณ : ๊ฐ์ฒด ๊ฒ€์‚ฌ์—๋Š” ๋Š˜ ==๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค. is๋Š” ์ข…์ข… ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์ž‘๋™์„ ํ•œ๋‹ค. >>> a = [1,2,3] >>> b = a >>> c = [1,2,3] >>> a is b True >>> a is c False >>> a == c True >>> ์–•์€ ๋ณต์‚ฌ(shallow copy) ๋ฆฌ์ŠคํŠธ์™€ ๋”•์…”๋„ˆ๋ฆฌ์—๋Š” ๋ณต์‚ฌ๋ฅผ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค. >>> a = [2,3, [100,101],4] >>> b = list(a) # ์‚ฌ๋ณธ์„ ๋งŒ๋“ฆ >>> a is b False ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ง€๊ธด ํ–ˆ์ง€๋งŒ ๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ์€ ๊ณต์œ ๋œ๋‹ค. >>> a[2].append(102) >>> b[2] [100,101,102] >>> >>> a[2] is b[2] True >>> ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‚ด๋ถ€ ๋ฆฌ์ŠคํŠธ [100, 101, 102]์ด ๊ณต์œ ๋œ๋‹ค. ์ด๊ฒƒ์„ ์–•์€ ๋ณต์‚ฌ๋ผ ํ•œ๋‹ค. ๊ทธ๋ฆผ์„ ๋ณด์ž. ๊นŠ์€ ๋ณต์‚ฌ(deep copy) ๊ฐ์ฒด์™€ ๊ทธ ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  ๊ฐ์ฒด์˜ ์‚ฌ๋ณธ์ด ํ•„์š”ํ•  ๋•Œ๊ฐ€ ์ข…์ข… ์žˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด copy ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. >>> a = [2,3, [100,101],4] >>> import copy >>> b = copy.deepcopy(a) >>> a[2].append(102) >>> b[2] [100,101] >>> a[2] is b[2] False >>> ์ด๋ฆ„, ๊ฐ’, ํƒ€์ž… ๋ณ€์ˆ˜๋ช…์€ ํƒ€์ž…์„ ๊ฐ–์ง€ ์•Š๋Š”๋‹ค. ๊ทธ์ € ์ด๋ฆ„์ผ ๋ฟ์ด๋‹ค. ํ•˜์ง€๋งŒ, ๊ฐ’์—๋Š” ํƒ€์ž…์ด ์žˆ๋‹ค. >>> a = 42 >>> b = 'Hello World' >>> type(a) <type 'int'> >>> type(b) <type 'str'> type()์„ ์‹คํ–‰ํ•˜๋ฉด ๊ฐ’์˜ ํƒ€์ž…์„ ์•Œ๋ ค์ค€๋‹ค. ํƒ€์ž…๋ช…์€ ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉ๋˜์–ด ํ•ด๋‹น ํƒ€์ž…์˜ ๊ฐ’์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๊ฐ’์„ ํ•ด๋‹น ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ํƒ€์ž… ๊ฒ€์‚ฌ(Type Checking) ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ํŠน์ • ํƒ€์ž…์ด ๋งž๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. if isinstance(a, list): print('a is a list') ๊ฐ€๋Šฅํ•œ ์—ฌ๋Ÿฌ ํƒ€์ž… ์ค‘ ํ•˜๋‚˜์— ๋Œ€ํ•ด ํ™•์ธํ•œ๋‹ค. if isinstance(a, (list, tuple)): print('a is a list or tuple') *์ฃผ์˜: ํƒ€์ž… ๊ฒ์‚ฌ๋ฅผ ์ง€๋‚˜์น˜๊ฒŒ ์‚ฌ์šฉํ•˜์ง€ ๋ง ๊ฒƒ. ๊ณผ๋„ํ•œ ์ฝ”๋“œ ๋ณต์žก์„ฑ์„ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๋‹น์‹ ์˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ €์ง€๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ผ๋ฐ˜์ ์ธ ์‹ค์ˆ˜๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ๋งŒ ์‚ฌ์šฉํ•˜๋ผ. * ๋ชจ๋“  ๊ฒƒ์ด ๊ฐ์ฒด๋‹ค ์ˆซ์ž, ๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ, ํ•จ์ˆ˜, ์˜ˆ์™ธ, ํด๋ž˜์Šค, ์ธ์Šคํ„ด์Šค(instance) ๋“ฑ ๋ชจ๋“  ๊ฒƒ์€ ๊ฐ์ฒด๋‹ค. ๋ชจ๋“  ๊ฐ์ฒด์— ๋Œ€ํ•ด ์ด๋ฆ„์„ ๋ถ™์ด๊ณ , ๋ฐ์ดํ„ฐ๋กœ ์ „๋‹ฌํ•˜๊ณ , ์ปจํ…Œ์ด๋„ˆ์— ๋„ฃ๋Š” ๋“ฑ์˜ ์ผ์„ ์•„๋ฌด ์ œ์•ฝ ์—†์ด ํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠน๋ณ„ ๋Œ€์šฐ๋ฅผ ๋ฐ›๋Š” ๊ฐ์ฒด๋Š” ์—†๋‹ค. '๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์ผ๊ธ‰'์ด๋ผ๊ณ ๋„ ํ‘œํ˜„ํ•œ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ๋ณด์ž. >>> import math >>> items = [abs, math, ValueError ] >>> items [<built-in function abs>, <module 'math' (builtin)>, <type 'exceptions.ValueError'>] >>> items[0](-45) 45 >>> items[1].sqrt(2) 1.4142135623730951 >>> try: x = int('not a number') except items[2]: print('Failed!') Failed! >>> ์—ฌ๊ธฐ์„œ items๋Š” ํ•จ์ˆ˜, ๋ชจ๋“ˆ, ์˜ˆ์™ธ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋‹ค. ์›๋ž˜ ์ด๋ฆ„ ๋Œ€์‹ ์— ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ์„ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. items[0](-45) # abs items[1].sqrt(2) # math except items[2]: # ValueError ๊ฐ•๋ ฅํ•จ์—๋Š” ์ฑ…์ž„์ด ๋”ฐ๋ฅธ๋‹ค. ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ด์„œ ํ•จ๋ถ€๋กœ ์‚ฌ์šฉํ•˜์ง€๋Š” ๋ง์ž. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด ์ผ๊ธ‰ ๊ฐ์ฒด์˜ ๊ฐ•๋ ฅํ•จ์„ ๋Š๊ปด๋ณด์ž. ์—ฐ์Šต ๋ฌธ์ œ 2.24: ์ผ๊ธ‰ ๋ฐ์ดํ„ฐ Data/portfolio.csv ํŒŒ์ผ์—์„œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์นผ๋Ÿผ์œผ๋กœ ์กฐ์งํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š”๋‹ค. name, shares, price "AA",100,32.20 "IBM",50,91.10 ... ์ด์ „ ์ฝ”๋“œ์—์„œ, ์šฐ๋ฆฌ๋Š” ํŒŒ์ผ์„ ์ฝ๋Š” ๋ฐ csv ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, ์—ฌ์ „ํžˆ ์ˆ˜์ž‘์—…์œผ๋กœ ํ˜• ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ–ˆ๋‹ค. ์˜ˆ: for row in rows: name = row[0] shares = int(row[1]) price = float(row[2]) ์•ฝ๊ฐ„์˜ ๋ฆฌ์ŠคํŠธ ๊ธฐ๋ณธ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•ด ์ด๋Ÿฌํ•œ ์œ ํ˜•์˜ ๋ณ€ํ™˜์„ ์ข€ ๋” ์˜๋ฆฌํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ ์นผ๋Ÿผ์„ ์ ์ ˆํ•œ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ๋‹ด๋Š” ํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์ž. >>> types = [str, int, float] >>> ์ด๋Ÿฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์กฐ์ฐจ ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” ํŒŒ์ด์ฌ์—์„œ๋Š” ๋ชจ๋“  ๊ฒƒ์ด ์ผ๊ธ‰(first-class)์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•จ์ˆ˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด ์–ผ๋งˆ๋“ ์ง€ ๊ทธ๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ์€ ๊ฐ’ x๋ฅผ ์ฃผ์–ด์ง„ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋“ค์ด๋‹ค(์˜ˆ: str(x), int(x), float(x)). ์ด์ œ, ์œ„ ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ์˜ ํ–‰์„ ์ฝ๋Š”๋‹ค. >>> import csv >>> f = open('Data/portfolio.csv') >>> rows = csv.reader(f) >>> headers = next(rows) >>> row = next(rows) >>> row ['AA', '100', '32.20'] >>> ์ด ๊ฐ’๋“ค์€ ํƒ€์ž…์ด ์ž˜๋ชป๋˜์–ด ๊ณ„์‚ฐ์— ๋ถ€์ ํ•ฉํ•˜๋‹ค. ์˜ˆ: >>> row[1] * row[2] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't multiply sequence by non-int of type 'str' >>> ํ•˜์ง€๋งŒ, ๋ฐ์ดํ„ฐ๋ฅผ types์— ์ง€์ •ํ•œ ํƒ€์ž…๊ณผ ์ง์ง€์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: >>> types[1] <type 'int'> >>> row[1] '100' >>> ๊ฐ’ ํ•˜๋‚˜๋ฅผ ๋ณ€ํ™˜ํ•ด ๋ณด์ž. >>> types[1](row[1]) # int(row[1])์™€ ๊ฐ™์Œ 100 >>> ๋‹ค๋ฅธ ๊ฐ’๋„ ๋ณ€ํ™˜ํ•ด ๋ณด์ž. >>> types[2](row[2]) # float(row[2])์™€ ๊ฐ™์Œ 32.2 >>> ๋ณ€ํ™˜ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๊ณ„์‚ฐํ•ด ๋ณด์ž. >>> types[1](row[1])*types[2](row[2]) 3220.0000000000005 >>> zip์œผ๋กœ ์นผ๋Ÿผ ํƒ€์ž…๊ณผ ํ•„๋“œ๋ฅผ ๋ฌถ์€ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. >>> r = list(zip(types, row)) >>> r [(<type 'str'>, 'AA'), (<type 'int'>, '100'), (<type 'float'>,'32.20')] >>> ํƒ€์ž… ๋ณ€ํ™˜๊ณผ ๊ฐ’์ด ์ง ์ง€์–ด์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, int๋Š” ๊ฐ’ '100'๊ณผ ์ง์„ ์ด๋ฃฌ๋‹ค. ๋ฆฌ์ŠคํŠธ์— zip์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ๊ฐ’์„ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์–ด ์œ ์šฉํ•˜๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด ๋ณด๋ผ. >>> converted = [] >>> for func, val in zip(types, row): converted.append(func(val)) ... >>> converted ['AA', 100, 32.2] >>> converted[1] * converted[2] 3220.0000000000005 >>> ์œ„ ์ฝ”๋“œ์—์„œ ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค. ๋ฃจํ”„์—์„œ, func ๋ณ€์ˆ˜๋Š” ํƒ€์ž… ๋ณ€ํ™˜ ํ•จ์ˆ˜๋“ค(์˜ˆ: str, int ๋“ฑ) ์ค‘ ํ•˜๋‚˜์ด๋ฉฐ, val ๋ณ€์ˆ˜๋Š” 'AA', '100'๊ณผ ๊ฐ™์€ ๊ฐ’๋“ค ์ค‘ ํ•˜๋‚˜๋‹ค. ํ‘œํ˜„์‹ func(val) ์€ ๊ฐ’์„ ๋ณ€ํ™˜ํ•œ๋‹ค(ํƒ€์ž… ์บ์ŠคํŠธ์™€ ๋น„์Šทํ•˜๋‹ค). ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋‹จ์ผ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์œผ๋กœ ์••์ถ•ํ•  ์ˆ˜ ์žˆ๋‹ค. >>> converted = [func(val) for func, val in zip(types, row)] >>> converted ['AA', 100, 32.2] >>> ์—ฐ์Šต ๋ฌธ์ œ 2.25: ๋”•์…”๋„ˆ๋ฆฌ ๋งŒ๋“ค๊ธฐ ํ‚ค ์ด๋ฆ„๊ณผ ๊ฐ’์˜ ์‹œํ€€์Šค๋ฅผ ๊ฐ–๊ณ  ์žˆ์„ ๋•Œ dict() ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜๋Š”๊ฐ€? ์นผ๋Ÿผ ํ—ค๋”๋กœ๋ถ€ํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์ž. >>> headers ['name', 'shares', 'price'] >>> converted ['AA', 100, 32.2] >>> dict(zip(headers, converted)) {'price': 32.2, 'name': 'AA', 'shares': 100} >>> ๋ฌผ๋ก , ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์— ๋Šฅํ†ตํ•˜๋‹ค๋ฉด, ๋”•์…”๋„ˆ๋ฆฌ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•ด ์ „ ๊ณผ์ •์„ ํ•œ ๋ฒˆ์— ๋๋‚ผ ์ˆ˜ ์žˆ๋‹ค. >>> { name: func(val) for name, func, val in zip(headers, types, row) } {'price': 32.2, 'name': 'AA', 'shares': 100} >>> ์—ฐ์Šต ๋ฌธ์ œ 2.26: ํฐ ๊ทธ๋ฆผ ์ด ์—ฐ์Šต ๋ฌธ์ œ์˜ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด, ์นผ๋Ÿผ ์œ„์ฃผ์˜ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์˜ ํ•„๋“œ๋ฅผ ํŒŒ์ด์ฌ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์‰ฝ๊ฒŒ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์„ค๋ช…์„ ์œ„ํ•ด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. >>> f = open('Data/dowstocks.csv') >>> rows = csv.reader(f) >>> headers = next(rows) >>> row = next(rows) >>> headers ['name', 'price', 'date', 'time', 'change', 'open', 'high', 'low', 'volume'] >>> row ['AA', '39.48', '6/11/2007', '9:36am', '-0.18', '39.67', '39.69', '39.45', '181800'] >>> ๋น„์Šทํ•œ ํŠธ๋ฆญ์„ ์‚ฌ์šฉํ•ด ํ•„๋“œ๋ฅผ ๋ณ€ํ™˜ํ•˜์ž. >>> types = [str, float, str, str, float, float, float, float, int] >>> converted = [func(val) for func, val in zip(types, row)] >>> record = dict(zip(headers, converted)) >>> record {'volume': 181800, 'name': 'AA', 'price': 39.48, 'high': 39.69, 'low': 39.45, 'time': '9:36am', 'date': '6/11/2007', 'open': 39.67, 'change': -0.18} >>> record['name'] 'AA' >>> record['price'] 39.48 >>> ๋ณด๋„ˆ์Šค: date๋ฅผ (6, 11, 2007)๊ณผ ๊ฐ™์€ ํŠœํ”Œ๋กœ ํŒŒ์‹ฑ ํ•˜๋ ค๋ฉด ์ด ์˜ˆ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ˆ˜์ •ํ•ด์•ผ ํ• ๊นŒ? ์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ ํ•œ ๊ฒƒ์„ ๊ณฐ๊ณฐ ์ƒ๊ฐํ•ด ๋ณด๋ผ. ์ด ๊ฐœ๋…์„ ๋‚˜์ค‘์— ๋‹ค์‹œ ์‚ดํŽด๋ณธ๋‹ค. 3. ํ”„๋กœ๊ทธ๋žจ ์กฐ์งํ™” ์ง€๊ธˆ๊นŒ์ง€ ํŒŒ์ด์ฌ ๊ธฐ์ดˆ๋ฅผ ๋ฐฐ์› ์œผ๋ฉฐ ์งง์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋” ํฐ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ ค๋ฉด ์กฐ์งํ™”ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ์ด ์„น์…˜์€ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์˜ค๋ฅ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž์„ธํžˆ ๋‹ค๋ฃจ๋ฉฐ ๋ชจ๋“ˆ์„ ์†Œ๊ฐœํ•œ๋‹ค. ๋์— ๊ฐ€์„œ๋Š” ์—ฌ๋Ÿฌ ํŒŒ์ผ์— ๊ฑธ์ณ ํ•จ์ˆ˜๋กœ ๋ถ„ํ• ๋œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ์œ ์šฉํ•œ ์ฝ”๋“œ ํ…œํ”Œ๋ฆฟ๋„ ์ œ๊ณตํ•œ๋‹ค. 3.1 ํ•จ์ˆ˜์™€ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ 3.2 ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ 3.3 ์˜ˆ์™ธ ์ฒ˜๋ฆฌ 3.4 ๋ชจ๋“ˆ 3.5 ๋ฉ”์ธ ๋ชจ๋“ˆ 3.6 ์œ ์šฉ์„ฑ์„ ์œ„ํ•œ ์„ค๊ณ„ ๋…ผ์˜ 3.1 ์Šคํฌ๋ฆฝํŒ… ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด์ž. ์Šคํฌ๋ฆฝํŠธ(Script)๋ž€? ์Šคํฌ๋ฆฝํŠธ๋Š” ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ์‹คํ–‰ํ•œ ๋‹ค์Œ ๋ฉˆ์ถ”๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. # program.py ๋ฌธ์žฅ 1 ๋ฌธ์žฅ 2 ๋ฌธ์žฅ 3 ... ์œ„์˜ ํ”„๋กœ๊ทธ๋žจ์€ ์Šคํฌ๋ฆฝํŠธ์— ๊ฐ€๊น๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ œ์  ์œ ์šฉํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•ด ์‚ฌ์šฉํ•˜๋‹ค ๋ณด๋ฉด ๊ธฐ๋Šฅ์ด ์ ์ฐจ ๋Š˜์–ด๋‚œ๋‹ค. ๊ด€๋ จ๋œ ๋‹ค๋ฅธ ๋ฌธ์ œ์—๋„ ์ ์šฉํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๋‹ค. ์‹œ๊ฐ„์ด ํ๋ฆ„์— ๋”ฐ๋ผ, ์Šคํฌ๋ฆฝํŠธ๋Š” ์ ์  ๋” ์ค‘์š”ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋˜์–ด ๋ฒ„๋ฆด ์ˆ˜๋„ ์žˆ๋‹ค. ์ด์ œ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ด์ง€ ์•Š์œผ๋ฉด ํฐ ๋ฌธ์ œ๊ฐ€ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ฒด๊ณ„๋ฅผ ์„ธ์›Œ์•ผ ํ•œ๋‹ค. ์ •์˜ํ•˜๊ธฐ ๋‚˜์ค‘์— ์žฌ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ด๋ฆ„์„ ๋ถ™์—ฌ๋‘ฌ์•ผ ํ•œ๋‹ค. def square(x): return x*x a = 42 b = a + 2 # `a`๊ฐ€ ์ •์˜๋ผ ์žˆ์–ด์•ผ ํ•จ z = square(b) # `square`์™€ `b`๊ฐ€ ์ •์˜๋ผ ์žˆ์–ด์•ผ ํ•จ ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜์˜ ์ •์˜๋ฅผ ํ•ญ์ƒ ์œ„์ชฝ์— ๋‘ฌ์•ผ ํ•œ๋‹ค. ํ•จ์ˆ˜ ์ •์˜ํ•˜๊ธฐ ๋‹จ์ผ ์ž‘์—…์— ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ํ•œ๊ณณ์— ๋ชจ์•„๋‘๋Š” ๊ฒƒ์ด ํ˜„๋ช…ํ•˜๋‹ค. ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ. def read_prices(filename): prices = {} with open(filename) as f: f_csv = csv.reader(f) for row in f_csv: prices[row[0]] = float(row[1]) return prices ํ•จ์ˆ˜๋Š” ๋ฐ˜๋ณต์ ์ธ ์—ฐ์‚ฐ์„ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ๋„ ํ•œ๋‹ค. oldprices = read_prices('oldprices.csv') newprices = read_prices('newprices.csv') ํ•จ์ˆ˜๋ž€ ๋ฌด์—‡์ธ๊ฐ€? ํ•จ์ˆ˜๋Š” ๋ฌธ์žฅ์˜ ์‹œํ€€์Šค์— ์ด๋ฆ„์„ ๋ถ™์ธ ๊ฒƒ์ด๋‹ค. def funcname(args): ๋ฌธ์žฅ ๋ฌธ์žฅ ... return ๊ฒฐ๊ณผ ํ•จ์ˆ˜ ๋‚ด์— ๋ชจ๋“  ํŒŒ์ด์ฌ ๋ฌธ์žฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. def foo(): import math print(math.sqrt(2)) help(math) ํŒŒ์ด์ฌ์—๋Š” ํŠน์ˆ˜ํ•œ ๋ฌธ์žฅ์ด ์—†๋‹ค(๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ธฐ์–ตํ•˜๊ธฐ ์‰ฝ๋‹ค). ํ•จ์ˆ˜ ์ •์˜ ํ•จ์ˆ˜๋Š” ์–ด๋– ํ•œ ์ˆœ์„œ๋กœ๋“  ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. def foo(x): bar(x) def bar(x): ๋ฌธ์žฅ # ๋˜๋Š” def bar(x): ๋ฌธ์žฅ def foo(x): bar(x) ํ•จ์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘ ์‚ฌ์šฉ(ํ˜ธ์ถœ) ํ•˜๊ธฐ ์ „์— ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. foo(3) # foo๊ฐ€ ์ •์˜๋ผ ์žˆ์–ด์•ผ ํ•œ๋‹ค ํ•จ์ˆ˜๋Š” ์ƒํ–ฅ์‹(bottom-up)์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ์ƒํ–ฅ์‹ ํ•จ์ˆ˜๋Š” ๋นŒ๋”ฉ ๋ธ”๋ก๊ณผ ๊ฐ™์ด ๋‹ค๋ค„์ง„๋‹ค. ์ž‘๊ณ  ๋‹จ์ˆœํ•œ ๋ธ”๋ก๋ถ€ํ„ฐ ๋จผ์ € ๋งŒ๋“ ๋‹ค. # myprogram.py def foo(x): ... def bar(x): ... foo(x) # ์œ„์— ์ •์˜๋จ ... def spam(x): ... bar(x) # ์•„๋ž˜์— ์ •์˜๋จ ... spam(42) # ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋์— ๋‚˜ํƒ€๋‚จ ๋‚˜์ค‘์— ๋‚˜์˜ค๋Š” ํ•จ์ˆ˜๋Š” ์•ž์— ๋‚˜์˜จ ํ•จ์ˆ˜๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํ•œ๋‹ค. ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, ์ด๊ฒƒ์€ ์Šคํƒ€์ผ์ผ ๋ฟ์ด๋‹ค. ์œ„ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ spam(42) ํ˜ธ์ถœ์„ ๋งˆ์ง€๋ง‰์— ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ๋ฟ์ด๋‹ค. ํ•จ์ˆ˜ ์„ค๊ณ„ ํ•จ์ˆ˜๋Š” ๋ธ”๋ž™๋ฐ•์Šค(black box)๋กœ ๊ฐ„์ฃผํ•˜๋Š” ๊ฒƒ์ด ์ด์ƒ์ ์ด๋‹ค. ์ „๋‹ฌ๋œ ์ž…๋ ฅ๋งŒ ๊ฐ€์ง€๊ณ  ์—ฐ์‚ฐ์„ ํ•˜๋ฉฐ ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜์˜ ์‚ฌ์šฉ์ด๋‚˜ ์‹ ๋น„ํ•œ ๋ถ€์ž‘์šฉ์„ ๋ฐฐ์ œํ•ด์•ผ ํ•œ๋‹ค. ๋ชจ๋“ˆํ™”(Modularity)์™€ ์˜ˆ์ธก ๊ฐ€๋Šฅ์„ฑ(Predictability)์„ ๋ชฉํ‘œ๋กœ ํ•˜๋ผ. ๋ฌธ์„œ ๋ฌธ์ž์—ด(Doc Strings) ๋ฌธ์„œ๋ฅผ doc-string ํ˜•ํƒœ๋กœ ์ฝ”๋“œ์— ํฌํ•จํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. doc-strings์€ ํ•จ์ˆ˜ ์ด๋ฆ„ ๋ฐ”๋กœ ๋’ค์— ์ง์ ‘ ์“ด ๋ฌธ์ž์—ด์ด๋‹ค. help(), IDE ๋ฐ ๊ธฐํƒ€ ๋„๊ตฌ์—์„œ ์ธ์‹ํ•œ๋‹ค. def read_prices(filename): ''' CSV ํŒŒ์ผ์—์„œ ์ด๋ฆ„๊ณผ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Œ ''' prices = {} with open(filename) as f: f_csv = csv.reader(f) for row in f_csv: prices[row[0]] = float(row[1]) return prices ๋ฌธ์„œ ๋ฌธ์ž์—ด์„ ์ž‘์„ฑํ•  ๋•Œ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋ฌด์Šจ ์ผ์„ ํ•˜๋Š”์ง€๋ฅผ ์งง์€ ๋ฌธ์žฅ์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ถ”๊ฐ€ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋Š” ์งง์€ ์‚ฌ์šฉ ์˜ˆ์ œ์™€ ํ•จ๊ฒŒ ์ธ์ž์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์„ ๋„ฃ์–ด๋ผ. ํƒ€์ž… ์• ๋„ˆ ํ…Œ์ด์…˜(Type Annotations) ํ•จ์ˆ˜ ์ •์˜์— ์„ ํƒ์ ์ธ ํƒ€์ž… ํžŒํŠธ(type hint)๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. def read_prices(filename: str) -> dict: ''' CSV ํŒŒ์ผ์—์„œ ์ด๋ฆ„๊ณผ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์Œ ''' prices = {} with open(filename) as f: f_csv = csv.reader(f) for row in f_csv: prices[row[0]] = float(row[1]) return prices ํžŒํŠธ๋Š” ์‹ค์ œ๋กœ ์—ฐ์‚ฐ์„ ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ์ˆœ์ „ํžˆ<NAME>์ด๋‹ค. ํ•˜์ง€๋งŒ, IDE, ์ฝ”๋“œ ๊ฒ€์‚ฌ๊ธฐ, ๊ธฐํƒ€ ๋„๊ตฌ์—์„œ ์‚ฌ์šฉ๋œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์„น์…˜ 2์—์„œ ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค์˜ ์„ฑ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณด๊ณ ์„œ๋ฅผ ํ”„๋ฆฐํŠธํ•˜๋Š” report.py ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ–ˆ๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ์—๋Š” ๋ช‡ ๊ฐ€์ง€ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ์˜ˆ: # report.py import csv def read_portfolio(filename): ''' ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค ํŒŒ์ผ์„ ์ฝ์–ด ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ. name, shares, price๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉ. ''' portfolio = [] with open(filename) as f: rows = csv.reader(f) headers = next(rows) for row in rows: record = dict(zip(headers, row)) stock = { 'name' : record['name'], 'shares' : int(record['shares']), 'price' : float(record['price']) } portfolio.append(stock) return portfolio ... ํ•˜์ง€๋งŒ, ์Šคํฌ๋ฆฝํŠธ๋กœ ๋œ ์ผ๋ จ์˜ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ๋ถ€๋ถ„๋„ ์žˆ๋‹ค. ์ด ์ฝ”๋“œ๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ๋๋ถ€๋ถ„ ๊ทผ์ฒ˜์— ์žˆ๋‹ค. ์˜ˆ: ... # ๋ณด๊ณ ์„œ๋ฅผ ์ถœ๋ ฅ headers = ('Name', 'Shares', 'Price', 'Change') print('% 10s % 10s % 10s % 10s' % headers) print(('-' * 10 + ' ') * len(headers)) for row in report: print('% 10s % 10d % 10.2f % 10.2f' % row) ... ์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ, ์ด ํ”„๋กœ๊ทธ๋žจ์˜ ํ•จ์ˆ˜ ์‚ฌ์šฉ ๋ถ€๋ถ„์„ ์ข€ ๋” ๊ฐ•๋ ฅํ•˜๊ฒŒ ์กฐ์งํ™”ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 3.1: ํ”„๋กœ๊ทธ๋žจ์„ ํ•จ์ˆ˜์˜ ์ปฌ๋ ‰์…˜์œผ๋กœ ๊ตฌ์กฐํ™” ๊ณ„์‚ฐ๊ณผ ์ถœ๋ ฅ์„ ํฌํ•จํ•œ ๋ชจ๋“  ์ฃผ์š” ์—ฐ์‚ฐ์„ ํ•จ์ˆ˜์˜ ์ปฌ๋ ‰์…˜์— ์˜ํ•ด ์ˆ˜ํ–‰ํ•˜๋„๋ก report.py ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•˜์ž. ํŠนํžˆ, ๋ณด๊ณ ์„œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” print_report(report) ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ด ์ผ๋ จ์˜ ํ•จ์ˆ˜๋งŒ ๋‚จ๊ธฐ๊ณ  ๊ณ„์‚ฐ ์ฝ”๋“œ๋ฅผ ์—†์• ์ž. ์—ฐ์Šต ๋ฌธ์ œ 3.2: ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์„ ์œ„ํ•œ ์ตœ์ƒ์œ„ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑ ํ”„๋กœ๊ทธ๋žจ์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์„ ๊ฐ€์ง€๊ณ  ๋‹จ์ผ ํ•จ์ˆ˜ portfolio_report(portfolio_filename, prices_filename)๋กœ ํŒจํ‚ค์ง• ํ•˜๋ผ. ๊ทธ ํ•จ์ˆ˜์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ˜ธ์ถœํ•ด, ์ด์ „๊ณผ ๋˜‘๊ฐ™์€ ๋ณด๊ณ ์„œ๋ฅผ ์ƒ์„ฑํ•˜์ž. portfolio_report('Data/portfolio.csv', 'Data/prices.csv') ์ด ์ตœ์ข… ๋ฒ„์ „์—๋Š” ์ผ๋ จ์˜ ํ•จ์ˆ˜ ์ •์˜์™€ ํ•จ๊ป˜, ๋งจ ๋งˆ์ง€๋ง‰์— portfolio_report() ํ•จ์ˆ˜ ํ˜ธ์ถœ๋งŒ ์œ ์ผํ•˜๊ฒŒ ๋‚จ๊ธด๋‹ค(์ด ํ•จ์ˆ˜๊ฐ€ ์ด ํ”„๋กœ๊ทธ๋žจ๊ณผ ๊ด€๋ จ๋œ ๋ชจ๋“  ๋‹จ๊ณ„๋ฅผ ์‹คํ–‰). ํ”„๋กœ๊ทธ๋žจ์„ ๋‹จ์ผ ํ•จ์ˆ˜๋กœ ๋ฐ”๊พธ๋ฉด ์ž…๋ ฅ์ด ๋ฐ”๋€Œ๋”๋ผ๋„ ์‰ฝ๊ฒŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ƒํ˜ธ์ž‘์šฉ ๋ชจ๋“œ์—์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•œ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์„ ์‹คํ–‰ํ•ด ๋ณด๋ผ. >>> portfolio_report('Data/portfolio2.csv', 'Data/prices.csv') ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> files = ['Data/portfolio.csv', 'Data/portfolio2.csv'] >>> for name in files: print(f'{name:-^43s}') portfolio_report(name, 'prices.csv') print() ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> ๋ถ€์—ฐ ์„ค๋ช… ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•ด ๋ฌธ์žฅ์„ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ•œ ๋น„๊ตฌ ์กฐ์ ์ธ ์Šคํฌ๋ฆฝํŒ… ์ฝ”๋“œ๋ฅผ ์•„์ฃผ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ํฐ ๊ทธ๋ฆผ์—์„œ, ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์–ธ์  ๊ฐ€๋Š” ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ปค์ ธ์„œ ์ง„์ž‘ ์กฐ์งํ™”ํ•ด๋‘์ง€ ์•Š์€ ๊ฒƒ์„ ์•„์‰ฌ์›Œํ•˜๊ฒŒ ๋œ๋‹ค. ๋˜ํ•œ, ํŒŒ์ด์ฌ์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ๋นจ๋ฆฌ ์‹คํ–‰๋œ๋‹ค. 3.2 ํ•จ์ˆ˜์˜ ์ž‘๋™ ์•ž์—์„œ ์ด๋ฏธ ํ•จ์ˆ˜๋ฅผ ์†Œ๊ฐœํ–ˆ์ง€๋งŒ, ์‹ค์ œ๋กœ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ข€ ๋” ๊นŠ์ด ์•Œ์•„๋ณด์ž. ์ด ์„น์…˜์˜ ๋ชฉ์ ์€ ์•ฝ๊ฐ„์˜ ๊ฐญ์„ ๋ฉ”์šฐ๊ณ  ํ˜ธ์ถœ ๊ด€๋ก€, ์Šค์ฝ”ํ•‘ ๊ทœ์น™ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ๋…ผ์˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๊ธฐ ์ด ํ•จ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. def read_prices(filename, debug): ... ์œ„์น˜ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. prices = read_prices('prices.csv', True) ํ‚ค์›Œ๋“œ ์ธ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋‹ค. prices = read_prices(filename='prices.csv', debug=True) ๊ธฐ๋ณธ ์ธ์ž ์ธ์ž๋ฅผ ์„ ํƒ ์‚ฌํ•ญ์œผ๋กœ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ํ•จ์ˆ˜ ์ •์˜์— ๊ธฐ๋ณธ๊ฐ’์„ ์ •ํ•ด๋‘๋ผ. def read_prices(filename, debug=False): ... ๊ธฐ๋ณธ๊ฐ’์ด ํ• ๋‹น๋˜๋ฉด ์ธ์ž๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์„ ํƒ์‚ฌํ•ญ์ด ๋œ๋‹ค. d = read_prices('prices.csv') e = read_prices('prices.dat', True) ์ฐธ๊ณ : ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๋Š” ์ธ์ž๋Š” ๋ฐ˜๋“œ์‹œ ์ธ์ž ๋ฆฌ์ŠคํŠธ์˜ ๋์— ๋‘์–ด์•ผ ํ•œ๋‹ค(์„ ํƒ์ ์ด์ง€ ์•Š์€ ์ธ์ž๊ฐ€ ๋ชจ๋‘ ์•ž์ชฝ์— ์™€์•ผ ํ•œ๋‹ค). ์„ ํƒ์  ์ธ์ž๋ฅผ ํ‚ค์›Œ๋“œ ์ธ์ž๋กœ ํ•˜๋ฉด ์ข‹๋‹ค. ๋‘ ๊ฐ€์ง€ ํ˜ธ์ถœ ๋ฐฉ์‹์„ ๋น„๊ต, ๋Œ€์กฐํ•ด ๋ณด์ž. parse_data(data, False, True) # ????? parse_data(data, ignore_errors=True) parse_data(data, debug=True) parse_data(data, debug=True, ignore_errors=True) ํ‚ค์›Œ๋“œ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋Œ€์ฒด๋กœ ๊ฐ€๋…์„ฑ์ด ๋†’๋‹ค. ํ”Œ๋ž˜๊ทธ ์—ญํ• ์„ ํ•˜๋Š” ์ธ์ž๋ผ๋“ ์ง€, ์„ ํƒ์  ๊ธฐ๋Šฅ๊ณผ ๊ด€๋ จ๋œ ๊ฒฝ์šฐ ํŠนํžˆ ๊ทธ๋ ‡๋‹ค. ์„ค๊ณ„ ๋ชจ๋ฒ” ์‚ฌ๋ก€ ํ•จ์ˆ˜ ์ธ์ž์—๋Š” ํ•ญ์ƒ ์งง๊ณ  ์˜๋ฏธ ์žˆ๋Š” ์ด๋ฆ„์„ ๋ถ™์ธ๋‹ค. ํ•จ์ˆ˜๋ฅผ ํ‚ค์›Œ๋“œ ํ˜ธ์ถœ ์Šคํƒ€์ผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ์ˆ˜๋„ ์žˆ๋‹ค. d = read_prices('prices.csv', debug=True) ํŒŒ์ด์ฌ ๊ฐœ๋ฐœ ๋„๊ตฌ๋Š” ๋„์›€๋ง ๊ธฐ๋Šฅ๊ณผ ๋ฌธ์„œ์—์„œ ์ด๋ฆ„์„ ํ‘œ์‹œํ•œ๋‹ค. ๊ฐ’์„ ๋ฐ˜ํ™˜ return ๋ฌธ์„ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. def square(x): return x * x ๋ฐ˜ํ™˜๊ฐ’์ด ์ฃผ์–ด์ง€์ง€ ์•Š๊ฑฐ๋‚˜ return ๋ฌธ์ด ์—†์œผ๋ฉด None์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. def bar(x): ๋ฌธ์žฅ return a = bar(4) # a = None # ๋˜๋Š” def foo(x): statements # `return`์ด ์—†์Œ b = foo(4) # b = None ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ˜ํ™˜๊ฐ’ ํ•จ์ˆ˜๋Š” ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ, ํŠœํ”Œ์— ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. def divide(a, b): q = a // b # ๋ชซ r = a % b # ๋‚˜๋จธ์ง€ return q, r # ํŠœํ”Œ์„ ๋ฐ˜ํ™˜ ์šฉ๋ก€: x, y = divide(37,5) # x = 7, y = 2 x = divide(37, 5) # x = (7, 2) ๋ณ€์ˆ˜ ์Šค์ฝ”ํ”„ ํ”„๋กœ๊ทธ๋žจ์€ ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค. x = value # ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜ def foo(): y = value # ๋กœ์ปฌ ๋ณ€์ˆ˜ ๋ณ€์ˆ˜ ํ• ๋‹น์€ ํ•จ์ˆ˜ ์ •์˜์˜ ์•ˆํŒŽ์—์„œ ์ผ์–ด๋‚œ๋‹ค. ๋ฐ”๊นฅ์—์„œ ์ •์˜๋œ ๋ณ€์ˆ˜๋Š” '๊ธ€๋กœ๋ฒŒ'์ด๋‹ค. ํ•จ์ˆ˜ ๋‚ด์˜ ๋ณ€์ˆ˜๋Š” '๋กœ์ปฌ'์ด๋‹ค. ๋กœ์ปฌ ๋ณ€์ˆ˜ ํ•จ์ˆ˜ ๋‚ด์— ํ• ๋‹น๋œ ๋ณ€์ˆ˜๋Š” ํ”„๋ผ์ด๋น—์ด๋‹ค. def read_portfolio(filename): portfolio = [] for line in open(filename): fields = line.split(',') s = (fields[0], int(fields[1]), float(fields[2])) portfolio.append(s) return portfolio ์ด ์˜ˆ์—์„œ, filename, portfolio, line, fields, s๋Š” ๋กœ์ปฌ ๋ณ€์ˆ˜๋‹ค. ์ด ๋ณ€์ˆ˜๋“ค์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ดํ›„์— ๋ณด์กด๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ด๊ฒƒ๋“ค์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์—†๋‹ค. >>> stocks = read_portfolio('portfolio.csv') >>> fields Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'fields' is not defined >>> ๋กœ์ปฌ์€ ๋‹ค๋ฅธ ๊ณณ์˜ ๋ณ€์ˆ˜์™€ ์ถฉ๋Œํ•  ์ผ์ด ์—†๋‹ค. ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜ ํ•จ์ˆ˜๋Š” ๊ฐ™์€ ํŒŒ์ผ์— ์ •์˜๋œ ๊ธ€๋กœ๋ฒŒ๋“ค์˜ ๊ฐ’์— ์ž์œ ๋กญ๊ฒŒ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค. name = 'Dave' def greeting(): print('Hello', name) # ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜ `name`์„ ์‚ฌ์šฉ ๋‹จ, ํ•จ์ˆ˜๋Š” ๊ธ€๋กœ๋ฒŒ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค. name = 'Dave' def spam(): name = 'Guido' spam() print(name) # 'Dave'๋ฅผ ํ”„๋ฆฐํŠธ ๊ธฐ์–ตํ•ด ์ค˜: ํ•จ์ˆ˜ ๋‚ด์˜ ๋ชจ๋“  ํ• ๋‹น์€ ๋กœ์ปฌ์ด๋‹ค. ๊ธ€๋กœ๋ฒŒ์„ ์ˆ˜์ •ํ•˜๊ธฐ ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด์•ผ๋งŒ ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ ์–ธํ•œ๋‹ค. name = 'Dave' def spam(): global name name = 'Guido' # ์œ„ ๊ธ€๋กœ๋ฒŒ name์„ ๋ณ€๊ฒฝ ๊ธ€๋กœ๋ฒŒ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ์„ ์–ธ์ด ๋จผ์ € ๋‚˜ํƒ€๋‚˜์•ผ ํ•˜๋ฉฐ, ๊ด€๋ จ๋œ ๋ณ€์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ ํ•จ์ˆ˜์™€ ๊ฐ™์€ ํŒŒ์ผ์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ธ€๋กœ๋ฒŒ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์„ ์•Œ์•„๋ณด๊ธฐ๋Š” ํ–ˆ์ง€๋งŒ, ๋ณ„๋กœ ์ข‹์€ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. global์„ ์•„์˜ˆ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ƒ์ฑ…์ด๋‹ค. ํ•จ์ˆ˜๊ฐ€ ์™ธ๋ถ€์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค๋ฉด, ์ฐจ๋ผ๋ฆฌ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด๋ผ(๋‚˜์ค‘์— ๋‹ค๋ฃฌ๋‹ค). ์ธ์ž ์ „๋‹ฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, ์ธ์ž ๋ณ€์ˆ˜๋Š” ์ „๋‹ฌ๋œ ๊ฐ’์„ ์ฐธ์กฐํ•˜๋Š” ์ด๋ฆ„์ด๋‹ค. ์ด ๋ณ€์ˆ˜๋“ค์€ ์‚ฌ๋ณธ์ด ์•„๋‹ˆ๋‹ค(์„น์…˜ 2.7์„ ์ฐธ์กฐ). ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ์ž๋ฃŒํ˜•(์˜ˆ: list, dict)์„ ์ „๋‹ฌํ•˜๋ฉด ์ œ์ž๋ฆฌ์—์„œ ์ˆ˜์ •๋  ์ˆ˜ ์žˆ๋‹ค. def foo(items): items.append(42) # ์ž…๋ ฅ ๊ฐ์ฒด๋ฅผ ์ˆ˜์ • a = [1, 2, 3] foo(a) print(a) # [1, 2, 3, 42] ํ•ต์‹ฌ: ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ ์ธ์ž์˜ ์‚ฌ๋ณธ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค. ์žฌํ• ๋‹น vs ์ˆ˜์ • ๋ณ€์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๋ณ€์ˆ˜๋ช…์„ ์žฌํ• ๋‹นํ•˜๋Š” ๊ฒƒ์˜ ๋ฏธ๋ฌ˜ํ•œ ์ฐจ์ด๋ฅผ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค. def foo(items): items.append(42) # ์ž…๋ ฅ ๊ฐ์ฒด๋ฅผ ์ˆ˜์ • a = [1, 2, 3] foo(a) print(a) # [1, 2, 3, 42] # VS def bar(items): items = [4,5,6] # ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ๋กœ์ปฌ `items` ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝ b = [1, 2, 3] bar(b) print(b) # [1, 2, 3] ๊ธฐ์–ตํ•ด ์ค˜: ๋ณ€์ˆ˜ ํ• ๋‹น์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ๋Œ€ ๋ฎ์–ด์“ฐ์ง€ ์•Š๋Š”๋‹ค. ์ด๋ฆ„์€ ์ƒˆ ๊ฐ’์— ๋ฐ”์šด๋“œ๋œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์ด ์—ฐ์Šต ๋ฌธ์ œ๋“ค์€ ์ด ์ฝ”์Šค์—์„œ ๊ฐ€์žฅ ๊ฐ•๋ ฅํ•˜๊ณ  ์–ด๋ ค์šด ๋ถ€๋ถ„์„ ๊ตฌํ˜„ํ•œ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€์˜ ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด ์ตํžŒ ์—ฌ๋Ÿฌ ๊ฐœ๋…์„ ํ•œ๊ณณ์— ๋ชจ์€๋‹ค. ์ตœ์ข…์ ์ธ ํ•ด๋‹ต์€ 25์ค„๊ฐ€๋Ÿ‰์˜ ์ฝ”๋“œ์— ๋ถˆ๊ณผํ•˜์ง€๋งŒ, ์‹œ๊ฐ„์„ ๋“ค์—ฌ ๊ฐ ๋ถ€๋ถ„์„ ์ดํ•ดํ•˜๋„๋ก ๋…ธ๋ ฅํ•˜๋ผ. report.py ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ CSV ํŒŒ์ผ์„ ์ฝ๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด read_portfolio() ํ•จ์ˆ˜๋Š” ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ์˜ ํ–‰์„ ํฌํ•จํ•œ ํŒŒ์ผ์„ ์ฝ์œผ๋ฉฐ, read_prices() ํ•จ์ˆ˜๋Š” ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ์˜ ํ–‰์„ ํฌํ•จํ•œ ํŒŒ์ผ์„ ์ฝ๋Š”๋‹ค. ๋‘ ํ•จ์ˆ˜ ๋ชจ๋‘ ์ € ์ˆ˜์ค€์˜ "fiddly" ๋น„ํŠธ๊ฐ€ ์žˆ๊ณ  ๊ธฐ๋Šฅ์ด ์„œ๋กœ ๋น„์Šทํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํŒŒ์ผ์„ ์—ด๊ณ  csv ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด ๊ฐ์‹ธ์„œ ๊ฐ’์„ ์ƒˆ๋กœ์šด ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ํŒŒ์ผ ํŒŒ์‹ฑ์„ ์‹ค์ œ๋กœ ๋งŽ์ด ํ•œ๋‹ค๋ฉด ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•ด ์ข€ ๋” ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ด๊ฒƒ์ด ์šฐ๋ฆฌ ๋ชฉํ‘œ๋‹ค. Work/fileparse.py ํŒŒ์ผ์„ ์—ด์–ด ์ด ์—ฐ์Šต์„ ์‹œ์ž‘ํ•˜์ž. ์ด ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์ž‘์—…ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 3.3: CSV ํŒŒ์ผ ์ฝ๊ธฐ ๋จผ์ € CSV ํŒŒ์ผ์„ ์ฝ์–ด ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ๋Š” ๋ฌธ์ œ๋ถ€ํ„ฐ ํ•ด๊ฒฐํ•˜์ž. fileparse.py ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค. # fileparse.py import csv def parse_csv(filename): ''' CSV ํŒŒ์ผ์„ ํŒŒ์‹ฑ ํ•ด ๋ ˆ์ฝ”๋“œ์˜ ๋ชฉ๋ก์„ ์ƒ์„ฑ ''' with open(filename) as f: rows = csv.reader(f) # ํ—ค๋”๋ฅผ ์ฝ์Œ headers = next(rows) records = [] for row in rows: if not row: # ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ํ–‰์„ ๊ฑด๋„ˆ๋œ€ continue record = dict(zip(headers, row)) records.append(record) return records ์ด ํ•จ์ˆ˜๋Š” CSV ํŒŒ์ผ์„ ์ฝ์–ด ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋˜, ํŒŒ์ผ์„ ์—ด๊ณ  csv ๋ชจ๋“ˆ์„ ๊ฐ€์ง€๊ณ  ๊ฐ์‹ธ๊ณ , ๊ณต๋ฐฑ ํ–‰์„ ๊ฑด๋„ˆ๋›ฐ๋Š” ๋“ฑ์˜ ์„ธ๋ถ€์‚ฌํ•ญ์„ ์ˆจ๊ธด๋‹ค. ํ•œ๋ฒˆ ์‚ฌ์šฉํ•ด ๋ณด์ž. ํžŒํŠธ: python3 -i fileparse.py. >>> portfolio = parse_csv('Data/portfolio.csv') >>> portfolio [{'price': '32.20', 'name': 'AA', 'shares': '100'}, {'price': '91.10', 'name': 'IBM', 'shares': '50'}, {'price': '83.44', 'name': 'CAT', 'shares': '150'}, {'price': '51.23', 'name': 'MSFT', 'shares': '200'}, {'price': '40.37', 'name': 'GE', 'shares': '95'}, {'price': '65.10', 'name': 'MSFT', 'shares': '50'}, {'price': '70.44', 'name': 'IBM', 'shares': '100'}] >>> ์ด ํ•จ์ˆ˜๋Š” ์ข‹๊ธด ํ•˜์ง€๋งŒ, ๋ชจ๋“  ๊ฒƒ์„ ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณ„์‚ฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์•„์‰ฌ์›€์ด ์žˆ๋‹ค. ์ด ์ ์„ ๊ณง ์ˆ˜์ •ํ•˜๊ฒ ์ง€๋งŒ, ์ผ๋‹จ์€ ๊ณ„์† ๋งŒ๋“ค์–ด ๋‚˜๊ฐ€์ž. ์—ฐ์Šต ๋ฌธ์ œ 3.4: ์นผ๋Ÿผ ์„ ํƒ๊ธฐ ๊ตฌ์ถ•ํ•˜๊ธฐ CSV ํŒŒ์ผ์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ ์ผ๋ถ€์—๋งŒ ๊ด€์‹ฌ์ด ์žˆ๋Š” ๋•Œ๊ฐ€ ๋งŽ์„ ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด, ๊ฐ€์ ธ์˜ฌ ์นผ๋Ÿผ์„ ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ฒŒ parse_csv() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด์ž. >>> # ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ธฐ >>> portfolio = parse_csv('Data/portfolio.csv') >>> portfolio [{'price': '32.20', 'name': 'AA', 'shares': '100'}, {'price': '91.10', 'name': 'IBM', 'shares': '50'}, {'price': '83.44', 'name': 'CAT', 'shares': '150'}, {'price': '51.23', 'name': 'MSFT', 'shares': '200'}, {'price': '40.37', 'name': 'GE', 'shares': '95'}, {'price': '65.10', 'name': 'MSFT', 'shares': '50'}, {'price': '70.44', 'name': 'IBM', 'shares': '100'}] >>> # ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋งŒ ์ฝ๊ธฐ >>> shares_held = parse_csv('Data/portfolio.csv', select=['name','shares']) >>> shares_held [{'name': 'AA', 'shares': '100'}, {'name': 'IBM', 'shares': '50'}, {'name': 'CAT', 'shares': '150'}, {'name': 'MSFT', 'shares': '200'}, {'name': 'GE', 'shares': '95'}, {'name': 'MSFT', 'shares': '50'}, {'name': 'IBM', 'shares': '100'}] >>> ์นผ๋Ÿผ ์„ ํƒ๊ธฐ์˜ ์˜ˆ๊ฐ€ ์—ฐ์Šต ๋ฌธ์ œ 2.23์— ์žˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ, ์—ฌ๊ธฐ ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. # fileparse.py import csv def parse_csv(filename, select=None): ''' CSV ํŒŒ์ผ์„ ํŒŒ์‹ฑ ํ•ด ๋ ˆ์ฝ”๋“œ์˜ ๋ชฉ๋ก์„ ์ƒ์„ฑ ''' with open(filename) as f: rows = csv.reader(f) # ํ—ค๋”๋ฅผ ์ฝ์Œ headers = next(rows) # ์นผ๋Ÿผ ์„ ํƒ๊ธฐ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ์ง€์ •ํ•œ ์นผ๋Ÿผ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š”๋‹ค. # ๋˜ํ•œ ๊ฒฐ๊ณผ ๋”•์…”๋„ˆ๋ฆฌ์— ์‚ฌ์šฉํ•  ํ—ค๋”์˜ ์ง‘ํ•ฉ์„ ์ขํžŒ๋‹ค if select: indices = [headers.index(colname) for colname in select] headers = select else: indices = [] records = [] for row in rows: if not row: # ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ํ–‰์„ ๊ฑด๋„ˆ๋œ€ continue # ํŠน์ • ์นผ๋Ÿผ์ด ์„ ํƒ๋˜์—ˆ์œผ๋ฉด ํ•„ํ„ฐ๋ง if indices: row = [ row[index] for index in indices ] # ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ฆ record = dict(zip(headers, row)) records.append(record) return records ์ด ๋ถ€๋ถ„์— ๋ช‡ ๊ฐ€์ง€ ํŠธ๋ฆญ์ด ์žˆ๋‹ค. ์นผ๋Ÿผ ์„ ํƒ์„ ํ–‰ ์ธ๋ฑ์Šค์— ๋งคํ•‘ํ•˜๋Š” ๋ถ€๋ถ„์ด ๊ฐ€์žฅ ์ค‘์š”ํ•  ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ž…๋ ฅ ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ—ค๋”๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. >>> headers = ['name', 'date', 'time', 'shares', 'price'] >>> ์„ ํƒํ•œ ์นผ๋Ÿผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๊ณ  ํ•˜์ž. >>> select = ['name', 'shares'] >>> ์ ์ ˆํ•œ ์„ ํƒ์„ ์œ„ํ•ด, ์„ ํƒํ•œ ์นผ๋Ÿผ๋ช…์„ ํŒŒ์ผ์˜ ์นผ๋Ÿผ ์ธ๋ฑ์Šค์— ๋งคํ•‘ํ•ด์•ผ ํ•œ๋‹ค. ์ด ๋‹จ๊ณ„์—์„œ ๊ทธ ์ผ์„ ํ•œ๋‹ค. >>> indices = [headers.index(colname) for colname in select ] >>> indices [0, 3] >>> ๋‹ฌ๋ฆฌ ๋งํ•ด, "name"์€ ์นผ๋Ÿผ 0์ด๊ณ  "shares"๋Š” ์นผ๋Ÿผ 3์ด๋‹ค. ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ ํ–‰์„ ์ฝ์„ ๋•Œ, ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด ์›ํ•˜๋Š” ์นผ๋Ÿผ๋งŒ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. >>> row = ['AA', '6/11/2007', '9:50am', '100', '32.20' ] >>> row = [ row[index] for index in indices ] >>> row ['AA', '100'] >>> ์—ฐ์Šต ๋ฌธ์ œ 3.5: ํ˜• ๋ณ€ํ™˜ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ์— ํ˜• ๋ณ€ํ™˜์„ ์ ์šฉํ• ์ง€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ฒŒ parse_csv() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด์ž. ์˜ˆ: >>> portfolio = parse_csv('Data/portfolio.csv', types=[str, int, float]) >>> portfolio [{'price': 32.2, 'name': 'AA', 'shares': 100}, {'price': 91.1, 'name': 'IBM', 'shares': 50}, {'price': 83.44, 'name': 'CAT', 'shares': 150}, {'price': 51.23, 'name': 'MSFT', 'shares': 200}, {'price': 40.37, 'name': 'GE', 'shares': 95}, {'price': 65.1, 'name': 'MSFT', 'shares': 50}, {'price': 70.44, 'name': 'IBM', 'shares': 100}] >>> shares_held = parse_csv('Data/portfolio.csv', select=['name', 'shares'], types=[str, int]) >>> shares_held [{'name': 'AA', 'shares': 100}, {'name': 'IBM', 'shares': 50}, {'name': 'CAT', 'shares': 150}, {'name': 'MSFT', 'shares': 200}, {'name': 'GE', 'shares': 95}, {'name': 'MSFT', 'shares': 50}, {'name': 'IBM', 'shares': 100}] >>> ์ด๊ฒƒ์„ ์—ฐ์Šต ๋ฌธ์ œ 2.24์—์„œ ์ด๋ฏธ ํƒ๊ตฌํ–ˆ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋‹น์‹ ์˜ ์ฝ”๋“œ์— ํฌํ•จ์‹œํ‚ค์ž. ... if types: row = [func(val) for func, val in zip(types, row) ] ... ์—ฐ์Šต ๋ฌธ์ œ 3.6: ํ—ค๋”๋ฅผ ๋‹ค๋ฃจ๊ธฐ CSV ํŒŒ์ผ์— ํ—ค๋” ์ •๋ณด๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค ์˜ˆ๋ฅผ ๋“ค์–ด, prices.csv๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋˜์–ด ์žˆ๋‹ค. "AA",9.22 "AXP",24.85 "BA",44.85 "BAC",11.27 ... ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•จ์œผ๋กœ์จ ์ด๋Ÿฐ ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ parse_csv() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด์ž. ์˜ˆ: >>> prices = parse_csv('Data/prices.csv', types=[str, float], has_headers=False) >>> prices [('AA', 9.22), ('AXP', 24.85), ('BA', 44.85), ('BAC', 11.27), ('C', 3.72), ('CAT', 35.46), ('CVX', 66.67), ('DD', 28.47), ('DIS', 24.22), ('GE', 13.48), ('GM', 0.75), ('HD', 23.16), ('HPQ', 34.35), ('IBM', 106.28), ('INTC', 15.72), ('JNJ', 55.16), ('JPM', 36.9), ('KFT', 26.11), ('KO', 49.16), ('MCD', 58.99), ('MMM', 57.1), ('MRK', 27.58), ('MSFT', 20.89), ('PFE', 15.19), ('PG', 51.94), ('T', 24.79), ('UTX', 52.61), ('VZ', 29.26), ('WMT', 49.74), ('XOM', 69.35)] >>> ์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ๋ฐ์ดํ„ฐ์˜ ์ฒซํ–‰์ด ํ—ค๋” ํŒŒ์ผ๋กœ ์ธ์‹๋˜์ง€ ์•Š๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ, ์นผ๋Ÿผ๋ช…์„ ํ‚ค๋กœ ์‚ผ์•„ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ฒŒ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 3.7: ๋‹ค๋ฅธ ์นผ๋Ÿผ ๊ตฌ๋ถ„์ž(delimitier)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ฝค๋งˆ๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด์ง€๋งŒ, ํƒญ์ด๋‚˜ ๊ณต๋ฐฑ์„ ์นผ๋Ÿผ ๊ตฌ๋ถ„์ž๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ๊ทธ ์˜ˆ๋กœ, Data/portfolio.dat์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. name shares price "AA" 100 32.20 "IBM" 50 91.10 "CAT" 150 83.44 "MSFT" 200 51.23 "GE" 95 40.37 "MSFT" 50 65.10 "IBM" 100 70.44 csv.reader() ํ•จ์ˆ˜๋Š” ๊ตฌ๋ถ„์ž๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค. rows = csv.reader(f, delimiter=' ') ๊ตฌ๋ถ„์ž๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒŒ parse_csv() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด์ž. ์˜ˆ: >>> portfolio = parse_csv('Data/portfolio.dat', types=[str, int, float], delimiter=' ') >>> portfolio [{'price': '32.20', 'name': 'AA', 'shares': '100'}, {'price': '91.10', 'name': 'IBM', 'shares': '50'}, {'price': '83.44', 'name': 'CAT', 'shares': '150'}, {'price': '51.23', 'name': 'MSFT', 'shares': '200'}, {'price': '40.37', 'name': 'GE', 'shares': '95'}, {'price': '65.10', 'name': 'MSFT', 'shares': '50'}, {'price': '70.44', 'name': 'IBM', 'shares': '100'}] >>> ๋ถ€์—ฐ ์„ค๋ช… ์—ฌ๊ธฐ๊นŒ์ง€ ์ž˜ ์ˆ˜ํ–‰ํ–ˆ๋‹ค๋ฉด ์ •๋ง ์œ ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“  ์…ˆ์ด๋‹ค. ์ด๊ฒƒ์„ ๊ฐ€์ง€๊ณ  ์ž„์˜์˜ CSV ํŒŒ์ผ์—์„œ ์›ํ•˜๋Š” ์นผ๋Ÿผ์„ ์„ ํƒํ•ด ํ˜• ๋ณ€ํ™˜์„ ํ•ด๋ณด๋ผ. ํŒŒ์ผ์˜ ๋‚ด๋ถ€ ์ž‘๋™์ด๋‚˜ csv ๋ชจ๋“ˆ ์‚ฌ์šฉ์— ํฌ๊ฒŒ ์‹ ๊ฒฝ ์“ธ ํ•„์š”๊ฐ€ ์—†๋‹ค. 3.3 ์˜ค๋ฅ˜ ๊ฒ€์‚ฌ ์˜ˆ์™ธ๋ฅผ ์ด๋ฏธ ๋‹ค๋ค˜์ง€๋งŒ, ์ด ์„น์…˜์—์„œ ์˜ค๋ฅ˜ ๊ฒ€์‚ฌ์™€ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์— ๋Œ€ํ•ด ์ข€ ๋” ์•Œ์•„๋ณด์ž. ํ”„๋กœ๊ทธ๋žจ์€ ์–ด๋–ป๊ฒŒ ์‹คํŒจํ•˜๋Š”๊ฐ€ ํŒŒ์ด์ฌ์€ ์ธ์ž ํƒ€์ž…์ด๋‚˜ ๊ฐ’์— ๋Œ€ํ•œ ๊ฒ€์‚ฌ๋‚˜ ๊ฒ€์ฆ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์–ด๋–ค ๋ฐ์ดํ„ฐ๋“  ํ•จ์ˆ˜์— ๊ธฐ์ˆ ํ•œ ๊ฒƒ๊ณผ ํ˜ธํ™˜๋˜๊ธฐ๋งŒ ํ•˜๋ฉด ํ•จ์ˆ˜๊ฐ€ ์ž‘๋™ํ•œ๋‹ค. def add(x, y): return x + y add(3, 4) # 7 add('Hello', 'World') # 'HelloWorld' add('3', '4') # '34' ํ•จ์ˆ˜์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉด ์‹คํ–‰ ์‹œ๊ฐ„์— ์˜ˆ์™ธ๋กœ์„œ ๋‚˜ํƒ€๋‚œ๋‹ค. def add(x, y): return x + y >>> add(3, '4') Traceback (most recent call last): ... TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> ์ฝ”๋“œ๋ฅผ ๊ฒ€์ฆํ•˜๊ธฐ ์œ„ํ•ด ํ…Œ์ŠคํŒ…์„ ๋งค์šฐ ์ค‘์š”์‹œํ•œ๋‹ค(๋‚˜์ค‘์— ๋‹ค๋ฃฌ๋‹ค). ์˜ˆ์™ธ ์˜ˆ์™ธ๋Š” ์˜ค๋ฅ˜์— ๋Œ€ํ•œ ์‹ ํ˜ธ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์˜ˆ์™ธ๋ฅผ ์ผ์œผํ‚ค๊ณ  ์‹ถ์œผ๋ฉด raise ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ผ. if name not in authorized: raise RuntimeError(f'{name} not authorized') ์˜ˆ์™ธ๋ฅผ ๋ถ™์žก์œผ๋ ค๋ฉด try-except๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. try: authenticate(user name) except RuntimeError as e: print(e) ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์˜ˆ์™ธ๋Š” ์ฒ˜์Œ ์ผ์น˜ํ•˜๋Š” except๊นŒ์ง€ ์ „ํŒŒ๋œ๋‹ค. def grok(): ... raise RuntimeError('Whoa!') # ์—ฌ๊ธฐ์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ def spam(): grok() # ํ˜ธ์ถœํ•˜๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ def bar(): try: spam() except RuntimeError as e: # ์˜ˆ์™ธ๋ฅผ ์—ฌ๊ธฐ์„œ ์žก์Œ ... def foo(): try: bar() except RuntimeError as e: # ์˜ˆ์™ธ๋Š” ์—ฌ๊ธฐ ๋„๋‹ฌํ•˜์ง€ ์•Š์Œ ... foo() ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด except ๋ธ”๋ก์— ๋ฌธ์žฅ์„ ๋„ฃ๋Š”๋‹ค. ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์žฅ์„ ๋ฌด์—‡์ด๋“  ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. def grok(): ... raise RuntimeError('Whoa!') def bar(): try: grok() except RuntimeError as e: # ์˜ˆ์™ธ๋ฅผ ์—ฌ๊ธฐ์„œ ์žก์Œ statements # ์ด ๋ฌธ์žฅ์„ ์‚ฌ์šฉ ๋ฌธ์žฅ ... bar() ์ฒ˜๋ฆฌํ•œ ๋‹ค์Œ, try-except ์ดํ›„ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์œผ๋กœ ์‹คํ–‰์„ ์žฌ๊ฐœํ•œ๋‹ค. def grok(): ... raise RuntimeError('Whoa!') def bar(): try: grok() except RuntimeError as e: # ์˜ˆ์™ธ๋ฅผ ์—ฌ๊ธฐ์„œ ์žก์Œ ๋ฌธ์žฅ ๋ฌธ์žฅ ... ๋ฌธ์žฅ # ์˜ˆ์™ธ๋ฅผ ์—ฌ๊ธฐ์„œ ์žฌ๊ฐœ ๋ฌธ์žฅ # ์—ฌ๊ธฐ์„œ ๊ณ„์† ... bar() ๋นŒํŠธ์ธ ์˜ˆ์™ธ ์Šค๋ฌด ๊ฐ€์ง€๊ฐ€ ๋„˜๋Š” ๋นŒํŠธ์ธ ์˜ˆ์™ธ๊ฐ€ ์žˆ๋‹ค. ์˜ˆ์™ธ ์ด๋ฆ„์€ ๋ฌด์—‡์ด ์ž˜๋ชป๋๋Š”์ง€ ์ง€์‹œํ•œ๋‹ค(์˜ˆ: ์ž˜๋ชป๋œ ๊ฐ’์„ ์ œ๊ณตํ•˜๋ฉด ValueError๊ฐ€ ์ผ์–ด๋‚œ๋‹ค). ์ด ๋ชฉ๋ก์— ์žˆ๋Š” ๊ฒƒ ์™ธ์—๋„ ๋” ์žˆ๋‹ค. ๋” ์•Œ๊ณ  ์‹ถ์œผ๋ฉด ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜๋ผ. ArithmeticError AssertionError EnvironmentError EOFError ImportError IndexError KeyboardInterrupt KeyError MemoryError NameError ReferenceError RuntimeError SyntaxError SystemError TypeError ValueError ์˜ˆ์™ธ ๊ฐ’ ์˜ˆ์™ธ์—๋Š” ๊ด€๋ จ ๊ฐ’์ด ์žˆ๋‹ค. ๋ฌด์—‡์ด ์ž˜๋ชป๋๋Š”์ง€ ๊ตฌ์ฒด์ ์œผ๋กœ ์•Œ๋ ค์ฃผ๋Š” ์ •๋ณด๊ฐ€ ํฌํ•จ๋œ๋‹ค. raise RuntimeError('Invalid user name') ์ด ๊ฐ’์€ ์˜ˆ์™ธ ์ธ์Šคํ„ด์Šค์˜ ์ผ๋ถ€์ด๋ฉฐ except์— ์ œ๊ณต๋œ ๋ณ€์ˆ˜์— ๋ฐฐ์น˜๋œ๋‹ค. try: ... except RuntimeError as e: # ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋ฅผ `e`๋กœ ๊ฐ€๋ฆฌํ‚จ๋‹ค ... e๋Š” ์˜ˆ์™ธ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๋‹ค. ํ•˜์ง€๋งŒ, ์ด๊ฒƒ์„ ํ”„๋ฆฐํŠธํ•˜๋ฉด ๋ฌธ์ž์—ด์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. except RuntimeError as e: print('Failed : Reason', e) ์—ฌ๋Ÿฌ ์˜ค๋ฅ˜๋ฅผ ์žก๊ธฐ except ๋ธ”๋ก์„ ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉํ•ด์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ๋ฅผ ๋ถ™์žก์„ ์ˆ˜ ์žˆ๋‹ค. try: ... except LookupError as e: ... except RuntimeError as e: ... except IOError as e: ... except KeyboardInterrupt as e: ... ๋˜๋Š”, ์ฒ˜๋ฆฌํ•˜๋Š” ๋ช…๋ น๋ฌธ์ด ๋™์ผํ•˜๋‹ค๋ฉด ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. try: ... except (IOError, LookupError, RuntimeError) as e: ... ๋ชจ๋“  ์˜ค๋ฅ˜๋ฅผ ๋ถ™์žก๊ธฐ ์˜ˆ์™ธ๋ฅผ ๋ถ™์žก์œผ๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด Exception์„ ์‚ฌ์šฉํ•œ๋‹ค. try: ... except Exception: # ์œ„ํ—˜! ์•„๋ž˜๋ฅผ ์ฐธ์กฐ print('An error occurred') ์ผ๋ฐ˜์ ์œผ๋กœ, ์ฝ”๋“œ๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€ ์ƒ๊ฐ์ด ์•„๋‹ˆ๋‹ค. ์‹คํŒจ ์ด์œ ๋ฅผ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์˜ค๋ฅ˜๋ฅผ ๋ถ™์žก๋Š” ์ž˜๋ชป๋œ ๋ฐฉ์‹ ์˜ˆ์™ธ๋ฅผ ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š์€ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์ž. try: go_do_something() except Exception: print('Computer says no') ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์˜ค๋ฅ˜๋ฅผ ๋ถ™์žก์•„ ๋ฒ„๋ฆฌ๋ฏ€๋กœ, ์ „ํ˜€ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์ด์œ ๋กœ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ(์˜ˆ: ํŒŒ์ด์ฌ ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ๋“ฑ) ๋””๋ฒ„๊น…์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋” ๋‚˜์€ ๋ฐฉ๋ฒ• ๋ชจ๋“  ์˜ค๋ฅ˜๋ฅผ ๋‹ค ๋ถ™์žก๊ณ  ์‹ถ์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. try: go_do_something() except Exception as e: print('Computer says no. Reason :', e) ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‹คํŒจํ•œ ์ด์œ ๋ฅผ ๊ตฌ์ฒด์ ์œผ๋กœ ๋ณด๊ณ ํ•œ๋‹ค. ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์˜ค๋ฅ˜๋ฅผ ๋ถ™์žก๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” ์ด์™€ ๊ฐ™์ด ์˜ค๋ฅ˜๋ฅผ ๋ณด๊ณ ํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๊ฐ–์ถฐ๋‘๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์˜ค๋ฅ˜์˜ ๋ฒ”์œ„๋ฅผ ์ขํžˆ๋Š” ๊ฒƒ์ด๋‹ค. ์‹ค์ œ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฅ˜๋งŒ ์žก์•„๋ผ. ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๋Š” ๋‹ค๋ฅธ ์ฝ”๋“œ์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋‚ด๋ฒ„๋ ค ๋‘๋ผ. ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ์ผ์œผํ‚ค๊ธฐ ๋ถ™์žก์€ ์˜ค๋ฅ˜๋ฅผ ์ „ํŒŒํ•˜๋ ค๋ฉด raise๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. try: go_do_something() except Exception as e: print('Computer says no. Reason :', e) raise ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ–‰๋™์„ ์ทจํ•˜๊ณ (์˜ˆ: ๋กœ๊น…) ํ˜ธ์ถœ์ž์—๊ฒŒ ์˜ค๋ฅ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์™ธ ๋ชจ๋ฒ” ์‚ฌ๋ก€ ์˜ˆ์™ธ๋ฅผ ๋ถ™์žก์ง€ ์•Š๋Š”๋‹ค. ์ผ์ฐ ํฐ ์†Œ๋ฆฌ๋ฅผ ๋‚ด๋ฉฐ ์‹คํŒจํ•œ๋‹ค. ์ค‘์š”ํ•œ ๋ฌธ์ œ๋ผ๋ฉด ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์‹ ๊ฒฝ ์จ ์ค„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•ด ์ค„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์—†์„ ๋•Œ๋งŒ ์˜ˆ์™ธ๋ฅผ ๋ถ™์žก์•„๋ผ. ํšŒ๋ณตํ•ด์„œ ์ง„ํ–‰์ด ๊ฐ€๋Šฅํ•œ ์˜ค๋ฅ˜๋งŒ ๋ถ™์žก์œผ๋ผ๋Š” ๋œป์ด๋‹ค. finally ๋ฌธ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€์˜ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„์—†์ด ์‹คํ–‰ํ•ด์•ผ ํ•  ์ฝ”๋“œ๋ฅผ ์ง€์ •ํ•œ๋‹ค. lock = Lock() ... lock.acquire() try: ... finally: lock.release() # ์ด๊ฒƒ์€ ํ•ญ์ƒ ์‹คํ–‰๋œ๋‹ค! ์˜ˆ์™ธ ๋ฐœ์ƒ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„์—†์ด. ๋ฆฌ์†Œ์Šค๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค(์ž ๊ธˆ, ํŒŒ์ผ ๋“ฑ). with ๋ฌธ ์š”์ฆ˜์€ ์ฝ”๋“œ์—์„œ try-finally๋ฅผ with ๋ฌธ์œผ๋กœ ๋Œ€์‹ ํ•˜๊ณค ํ•œ๋‹ค. lock = Lock() with lock: # ์ž ๊ธˆ์„ ํš๋“ ... # ์ž ๊ธˆ ํ•ด์ œ ์ข€ ๋” ์นœ์ˆ™ํ•œ ์˜ˆ๋ฅผ ๋ณด์ž. with open(filename) as f: # ํŒŒ์ผ์„ ์‚ฌ์šฉ ... # ํŒŒ์ผ์„ ๋‹ซ์Œ with๋Š” ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ ์ฝ˜ํ…์ŠคํŠธ(context)์„ ์ •์˜ํ•œ๋‹ค. ์˜ˆ์™ธ๊ฐ€ ์ฝ˜ํ…์ŠคํŠธ์—์„œ ๋ฒ—์–ด๋‚˜๋ฉด ๋ฆฌ์†Œ์Šค๋Š” ํ•ด์ œ๋œ๋‹ค. with ๋ฌธ์€ ํ•ด๋‹น ๊ตฌ๋ฌธ์„ ์ง€์›ํ•˜๋„๋ก ํŠน๋ณ„ํžˆ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋œ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ๋งŒ ์ž‘๋™ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 3.8: ์˜ˆ์™ธ ์ผ์œผํ‚ค๊ธฐ ์ง€๋‚œ ์„น์…˜์—์„œ ์ž‘์„ฑํ•œ parse_csv() ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ์นผ๋Ÿผ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ์ง€๋งŒ, ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์— ์นผ๋Ÿผ ํ—ค๋”๊ฐ€ ์žˆ์–ด์•ผ๋งŒ ์˜ฌ๋ฐ”๋กœ ์ž‘๋™ํ•œ๋‹ค. select์™€ has_headers=False ์ธ์ž๊ฐ€ ํ•จ๊ป˜ ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ ์˜ˆ์™ธ๋ฅผ ์ผ์œผํ‚ค๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด๋ผ. ์˜ˆ: >>> parse_csv('Data/prices.csv', select=['name','price'], has_headers=False) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "fileparse.py", line 9, in parse_csv raise RuntimeError("select argument requires column headers") RuntimeError: select argument requires column headers >>> ์ด ๊ฒ€์‚ฌ ์™ธ์—, ํ•จ์ˆ˜์—์„œ ๋˜ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์ž…๋ ฅ๊ฐ’ ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹Œ์ง€ ๊ถ๊ธˆํ•  ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํŒŒ์ผ๋ช…์ด ๋ฌธ์ž์—ด์ธ์ง€, ํƒ€์ž…์ด ๋ฆฌ์ŠคํŠธ์ธ์ง€, ์•„๋‹ˆ๋ฉด ๋‹ค๋ฅธ ๊ฒƒ์ธ์ง€ ํ™•์ธํ•ด์•ผ ํ• ๊นŒ? ์ผ๋ฐ˜์ ์ธ ๊ทœ์น™์œผ๋กœ, ๊ทธ๋Ÿฌํ•œ ํ…Œ์ŠคํŠธ๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ , ์ž˜๋ชป๋œ ์ž…๋ ฅ์ด ๋“ค์–ด์˜ค๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํŒจํ•˜๊ฒŒ ๋‚ด๋ฒ„๋ ค ๋‘๋Š” ๊ฒƒ์ด ์ตœ์„ ์ผ ๋•Œ๊ฐ€ ๋งŽ๋‹ค. ํŠธ๋ ˆ์ด์Šค ๋ฐฑ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฌธ์ œ๋ฅผ ์ง€๋ชฉํ•˜์—ฌ ๋””๋ฒ„๊น…์„ ๋„์™€์ค„ ๊ฒƒ์ด๋‹ค. ์œ„์—์„œ ํ™•์ธ์„ ์ถ”๊ฐ€ํ•œ ์ด์œ ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ๋ฌด์˜๋ฏธํ•˜๊ฒŒ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋‹ค(์˜ˆ: ์นผ๋Ÿผ ํ—ค๋”๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•จ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ , ๊ทธ์™€ ๋™์‹œ์— ์•„๋ฌด ํ—ค๋”๋„ ์—†๋‹ค๊ณ  ์ง€์ •ํ•˜๋Š” ๊ฒƒ). ์ด๋Š” ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ์— ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์Œ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. "๋ฐœ์ƒํ•ด์„œ๋Š” ์•ˆ ๋˜๋Š”" ์ผ€์ด์Šค๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€ ์ƒ๊ฐ์ด๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 3.9: ์˜ˆ์™ธ๋ฅผ ๋ถ™์žก๊ธฐ ์•ž์—์„œ ์ž‘์„ฑํ•œ parse_csv() ํ•จ์ˆ˜๋Š” ํŒŒ์ผ์˜ ์ „์ฒด ๋‚ด์šฉ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ ์„ธ๊ณ„์—์„œ๋Š” ์ž…๋ ฅ ํŒŒ์ผ์ด ์†์ƒ๋˜๊ฑฐ๋‚˜, ๊ฐ’์ด ๋ˆ„๋ฝ๋˜๊ฑฐ๋‚˜, ์˜ค๋ฅ˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์„ž์—ฌ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ—˜ํ•ด ๋ณด์ž. >>> portfolio = parse_csv('Data/missing.csv', types=[str, int, float]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "fileparse.py", line 36, in parse_csv row = [func(val) for func, val in zip(types, row)] ValueError: invalid literal for int() with base 10: '' >>> ๋ ˆ์ฝ”๋“œ ์ƒ์„ฑ ๋„์ค‘ ๋ฐœ์ƒํ•œ ๋ชจ๋“  ValueError ์˜ˆ์™ธ๋ฅผ ๋ถ™์žก์•„, ๋ณ€ํ™˜์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ํ–‰์— ๋Œ€ํ•ด ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ํ”„๋ฆฐํŠธํ•˜๋„๋ก parse_csv() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๋ผ. ๋ฉ”์‹œ์ง€์— ํ–‰ ๋ฒˆํ˜ธ์™€ ์‹คํŒจ ์ด์œ ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •๋ณด๋ฅผ ํฌํ•จํ•œ๋‹ค. ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด, ์œ„์˜ Data/missing.csv ํŒŒ์ผ์„ ์ฝ๋Š”๋‹ค. ์˜ˆ: >>> portfolio = parse_csv('Data/missing.csv', types=[str, int, float]) Row 4: Couldn't convert ['MSFT', '', '51.23'] Row 4: Reason invalid literal for int() with base 10: '' Row 7: Couldn't convert ['IBM', '', '70.44'] Row 7: Reason invalid literal for int() with base 10: '' >>> >>> portfolio [{'price': 32.2, 'name': 'AA', 'shares': 100}, {'price': 91.1, 'name': 'IBM', 'shares': 50}, {'price': 83.44, 'name': 'CAT', 'shares': 150}, {'price': 40.37, 'name': 'GE', 'shares': 95}, {'price': 65.1, 'name': 'MSFT', 'shares': 50}] >>> ์—ฐ์Šต ๋ฌธ์ œ 3.10: ์˜ค๋ฅ˜ ์–ต์ œ ์‚ฌ์šฉ์ž๊ฐ€ ๋ช…์‹œ์ ์œผ๋กœ ์›ํ•˜๋Š” ๊ฒฝ์šฐ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๊ฒŒ parse_csv() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๋ผ. ์˜ˆ: >>> portfolio = parse_csv('Data/missing.csv', types=[str, int, float], silence_errors=True) >>> portfolio [{'price': 32.2, 'name': 'AA', 'shares': 100}, {'price': 91.1, 'name': 'IBM', 'shares': 50}, {'price': 83.44, 'name': 'CAT', 'shares': 150}, {'price': 40.37, 'name': 'GE', 'shares': 95}, {'price': 65.1, 'name': 'MSFT', 'shares': 50}] >>> ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๋Š” ๊ฐ€์žฅ ์–ด๋ ค์šด ๋ถ€๋ถ„์ด๋‹ค. ์ผ๋ฐ˜์ ์ธ ๊ทœ์น™์€ ์˜ค๋ฅ˜๋ฅผ ์กฐ์šฉํžˆ ๋ฌด์‹œํ•ด ๋ฒ„๋ฆฌ์ง€ ๋ง๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š”, ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ ํ•˜๋˜ ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•  ๋•Œ์— ํ•œ ํ•ด ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ˆจ๊ธฐ๋Š” ๊ฒƒ์ด ๋‚ซ๋‹ค. 3.4 ๋ชจ๋“ˆ ์ด ์„น์…˜์€ ๋ชจ๋“ˆ์˜ ๊ฐœ๋…์„ ์†Œ๊ฐœํ•˜๊ณ  ์—ฌ๋Ÿฌ ํŒŒ์ผ์— ๊ฑธ์ณ ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฃฌ๋‹ค. ๋ชจ๋“ˆ๊ณผ ์ž„ํฌํŠธ ๋ชจ๋“  ํŒŒ์ด์ฌ ์†Œ์Šค ํŒŒ์ผ์€ ๋ชจ๋“ˆ์ด๋‹ค. # foo.py def grok(a): ... def spam(b): ... import ๋ฌธ์€ ๋ชจ๋“ˆ์„ ์ ์žฌ(load) ํ•˜๊ณ  ์‹คํ–‰(execute) ํ•œ๋‹ค. # program.py import foo a = foo.grok(2) b = foo.spam('Hello') ... ๋„ค์ž„์ŠคํŽ˜์ด์Šค ๋ชจ๋“ˆ์€ ์ด๋ฆ„์ด ์žˆ๋Š” ๋ณ€์ˆ˜๋“ค์˜ ์ปฌ๋ ‰์…˜์ด๋ฉฐ, ๋„ค์ž„์ŠคํŽ˜์ด์Šค(namespace)๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค. ์ด๋ฆ„์€ ๋ชจ๋‘ ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜์ด๋ฉฐ ํ•จ์ˆ˜๋Š” ์†Œ์Šค ํŒŒ์ผ์— ์ •์˜๋œ๋‹ค. ์ž„ํฌํŠธ ํ•œ ๋’ค, ๋ชจ๋“ˆ๋ช…์€ ํ”„๋ฆฌํ”ฝ์Šค(prefix)๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๊ฒƒ์€ ๊ทธ ๋•Œ๋ฌธ์ด๋‹ค. import foo a = foo.grok(2) b = foo.spam('Hello') ... ๋ชจ๋“ˆ๋ช…์€ ํŒŒ์ผ๋ช…๊ณผ ์ง์ ‘ ์—ฐ๊ด€๋œ๋‹ค(foo -> foo.py). ๊ธ€๋กœ๋ฒŒ ์ •์˜ ๊ธ€๋กœ๋ฒŒ(global) ์Šค์ฝ”ํ”„์— ์ •์˜๋œ ๋ชจ๋“  ๊ฒƒ์€ ๋ชจ๋“ˆ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์กด์žฌํ•œ๋‹ค. ๊ฐ™์€ ๋ณ€์ˆ˜ x๋ฅผ ์ •์˜ํ•˜๋Š” ๋‘ ๋ชจ๋“ˆ์„ ์ƒ๊ฐํ•ด ๋ณด์ž. # foo.py x = 42 def grok(a): ... # bar.py x = 37 def spam(a): ... ๋‘ ๊ฒฝ์šฐ x ์ •์˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. ํ•˜๋‚˜๋Š” foo.x์ด๊ณ , ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” bar.x๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์ด ๊ฐ™์€ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋•Œ ์ด๋ฆ„์€ ์„œ๋กœ ์ถฉ๋Œํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ชจ๋“ˆ์€ ๋…๋ฆฝ์ ์ด๋‹ค(isolated). ํ™˜๊ฒฝ์œผ๋กœ์„œ์˜ ๋ชจ๋“ˆ ๋ชจ๋“ˆ์€ ๋‚ด๋ถ€์— ์ •์˜๋œ ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ๊ฐ์‹ธ๋Š” ํ™˜๊ฒฝ์„ ํ˜•์„ฑํ•œ๋‹ค. # foo.py x = 42 def grok(a): print(x) ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜๋“ค์€ ํ•ญ์ƒ ๊ฐ์‹ธ๋Š” ๋ชจ๋“ˆ(๊ฐ™์€ ํŒŒ์ผ)์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. ๊ฐ ์†Œ์Šค ํŒŒ์ผ์€ ๊ฐœ๋ณ„์ ์ธ ์†Œ์šฐ์ฃผ๋‹ค. ๋ชจ๋“ˆ ์‹คํ–‰ ๋ชจ๋“ˆ์ด ์ž„ํฌํŠธ ๋  ๋•Œ, ํŒŒ์ผ์˜ ๋์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๋ชจ๋“ˆ์˜ ๋ชจ๋“  ๋ฌธ์žฅ์ด ์ฐจ๋ก€๋Œ€๋กœ ์‹คํ–‰๋œ๋‹ค. ๋ชจ๋“ˆ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์˜ ๋‚ด์šฉ์€ ๋ชจ๋‘ ๊ธ€๋กœ๋ฒŒ ์ด๋ฆ„์ด๋ฉฐ ์‹คํ–‰ ๊ณผ์ •์˜ ๋์— ์ •์˜๋œ๋‹ค. ๊ธ€๋กœ๋ฒŒ ์Šค์ฝ”ํ”„์—์„œ ์ž‘์—…(ํ”„๋ฆฐํŒ…, ํŒŒ์ผ ์ƒ์„ฑ ๋“ฑ)์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์Šคํฌ๋ฆฝํŒ… ๋ฌธ์žฅ์ด ์žˆ๋‹ค๋ฉด, ์ž„ํฌํŠธ ํ•  ๋•Œ ์ˆ˜ํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. import as ๋ฌธ ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•˜๋ฉด์„œ ๊ทธ ์ด๋ฆ„์„ ๋ฐ”๊ฟ” ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. import math as m def rectangular(r, theta): x = r * m.cos(theta) y = r * m.sin(theta) return x, y ๊ทธ๋ƒฅ ์ž„ํฌํŠธ ํ•  ๋•Œ์™€ ๋˜‘๊ฐ™์ด ์ž‘๋™ํ•œ๋‹ค. ํ•œ ํŒŒ์ผ ๋‚ด์—์„œ ๋ชจ๋“ˆ ์ด๋ฆ„์„ ๋ฐ”๊ฟ” ๋ถ€๋ฅผ ๋ฟ์ด๋‹ค. from ๋ชจ๋“ˆ import ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋“ˆ์—์„œ ์‹ฌ๋ฒŒ์„ ์„ ํƒํ•ด ๋กœ์ปฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. from math import sin, cos def rectangular(r, theta): x = r * cos(theta) y = r * sin(theta) return x, y ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋“ˆ ํ”„๋ฆฌํ”ฝ์Šค ์—†์ด๋„ ๋ชจ๋“ˆ์˜ ์ผ๋ถ€๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์œ ์šฉํ•œ ๋ฐฉ์‹์ด๋‹ค. ์ž„ํฌํŠธ์— ๋Œ€ํ•œ ์„ค๋ช… ์ž„ํฌํŠธ๋ฅผ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋“ , ๋ชจ๋“ˆ์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์€ ๋‹ฌ๋ผ์ง€์ง€ ์•Š๋Š”๋‹ค. import math # vs import math as m # vs from math import cos, sin ... ํŠนํžˆ, import๋Š” ํ•ญ์ƒ ํŒŒ์ผ ์ „์ฒด๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉฐ ๋ชจ๋“ˆ์€ ํ™˜๊ฒฝ๊ณผ ์—ฌ์ „ํžˆ ๋ถ„๋ฆฌ๋œ๋‹ค. import ๋ชจ๋“ˆ as ๋ฌธ์€ ๋กœ์ปฌ์˜ ์ด๋ฆ„๋งŒ ๋ฐ”๊พผ๋‹ค. from math import cos, sin ๋ฌธ์€ ์—ฌ์ „ํžˆ math ๋ชจ๋“ˆ ์ „์ฒด๋ฅผ ์ ์žฌํ•œ๋‹ค. ์ด๊ฒƒ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ชจ๋“ˆ์— ์žˆ๋Š” cos์™€ sin์ด ๋กœ์ปฌ ์ŠคํŽ˜์ด์Šค์— ๋ณต์‚ฌ๋  ๋ฟ์ด๋‹ค. ๋ชจ๋“ˆ ์ ์žฌ ๊ฐ ๋ชจ๋“ˆ์€ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ์ ์žฌ ๋ฐ ์‹คํ–‰๋œ๋‹ค. ์ฐธ๊ณ : ์ž„ํฌํŠธ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ํ•˜๋”๋ผ๋„ ์ด์ „์— ์ ์žฌํ•œ ๋ชจ๋“ˆ์˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. sys.modules์€ ์ ์žฌ๋œ ๋ชจ๋“ˆ ์ „์ฒด์˜ ๋”•์…”๋„ˆ๋ฆฌ๋‹ค. >>> import sys >>> sys.modules.keys() ['copy_reg', '__main__', 'site', '__builtin__', 'encodings', 'encodings.encodings', 'posixpath', ...] >>> ์ฃผ์˜: ๋งŒ์•ฝ ๋ชจ๋“ˆ์˜ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•œ ํ›„์— import ๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ํ˜ผ๋™์ด ์ƒ๊ธด๋‹ค. ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•˜๋ฉด sys.modules์— ์บ์‹œ ๋˜๋ฏ€๋กœ, ์ž„ํฌํŠธ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ด์ „์— ์ ์žฌ๋œ ๋ชจ๋“ˆ์ด ๋ฐ˜ํ™˜๋˜๋ฉฐ, ๋ชจ๋“ˆ์„ ์ˆ˜์ •ํ•˜๋”๋ผ๋„ ๋ฐ˜์˜๋˜์ง€ ์•Š๋Š”๋‹ค. ์ˆ˜์ •ํ•œ ๋ชจ๋“ˆ์„ ์ ์žฌํ•˜๋Š” ๊ฐ€์žฅ ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ•์€ ํŒŒ์ด์ฌ์„ ์ข…๋ฃŒํ•˜๊ณ  ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ชจ๋“ˆ ์œ„์น˜ ํŒŒ์ด์ฌ์€ ๋ชจ๋“ˆ์„ ์ฐพ์„ ๋•Œ ๊ฒฝ๋กœ ๋ฆฌ์ŠคํŠธ(sys.path)๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค. >>> import sys >>> sys.path [ '', '/usr/local/lib/python36/python36.zip', '/usr/local/lib/python36', ... ] ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ์˜จ๋‹ค. ๋ชจ๋“ˆ ๊ฒ€์ƒ‰ ๊ฒฝ๋กœ ์–ธ๊ธ‰ํ•œ ๋ฐ”์™€ ๊ฐ™์ด, sys.path์—๋Š” ๊ฒ€์ƒ‰ ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋‹ค. ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ˆ˜์ž‘์—…์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. import sys sys.path.append('/project/foo/pyfiles') ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฒฝ๋กœ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. % env PYTHONPATH=/project/foo/pyfiles python3 Python 3.6.0 (default, Feb 3 2017, 05:53:21) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] >>> import sys >>> sys.path ['','/project/foo/pyfiles', ...] ์ผ๋ฐ˜์ ์œผ๋กœ, ๋ชจ๋“ˆ ๊ฒ€์ƒ‰ ๊ฒฝ๋กœ๋ฅผ ์ˆ˜์ž‘์—…์œผ๋กœ ์กฐ์ •ํ•  ํ•„์š”๋Š” ์—†๋‹ค. ํ•˜์ง€๋งŒ, ์ผ๋ฐ˜์ ์ด์ง€ ์•Š์€ ์œ„์น˜์— ์žˆ๋‹ค๋“ ์ง€ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์•ก์„ธ์Šคํ•  ์ค€๋น„๊ฐ€ ๋˜์ง€ ์•Š์€ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ์ž„ํฌํŠธ ํ•  ์ผ์ด ์ƒ๊ธฐ๊ณค ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์ด ์—ฐ์Šต ๋ฌธ์ œ๋Š” ๋ชจ๋“ˆ๊ณผ ๊ด€๋ จ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํŒŒ์ด์ฌ์„ ์ ์ ˆํ•œ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์ด ๊ฒช๋Š” ๋ชจ๋“ˆ๊ณผ ๊ด€๋ จ๋œ ๋ฌธ์ œ๋Š” ๋Œ€๋ถ€๋ถ„ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ ๋˜๋Š” ํŒŒ์ด์ฌ ๊ฒฝ๋กœ ์„ค์ •๊ณผ ๊ด€๋ จ๋œ๋‹ค. ์ด ์ฝ”์Šค์—์„œ๋Š” ์ž‘์„ฑํ•˜๋Š” ๋ชจ๋“  ์ฝ”๋“œ๊ฐ€ Work/ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ์ตœ์„ ์˜ ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด, ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋„ ๊ทธ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์‹คํ–‰ํ•˜๋ผ. ๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด sys.path์— practical-python/Work๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 3.11: ๋ชจ๋“ˆ ์ž„ํฌํŠธ ์„น์…˜ 3์—์„œ ์šฐ๋ฆฌ๋Š” CSV ๋ฐ์ดํ„ฐ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ํŒŒ์‹ฑ ํ•˜๋Š” ๋ฒ”์šฉ ํ•จ์ˆ˜ parse_csv()๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ์ด์ œ ๊ทธ ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์„ ์•Œ์•„๋ณด์ž. ๋จผ์ €, ์ƒˆ๋กœ์šด ์…ธ ์ฐฝ์„ ์‹œ์ž‘ํ•œ๋‹ค. ํŒŒ์ผ์ด ์žˆ๋Š” ํด๋”๋กœ ์ด๋™ํ•œ๋‹ค. ๊ทธ ํŒŒ์ผ๋“ค์„ ์ž„ํฌํŠธ ํ•  ๊ฒƒ์ด๋‹ค. ํŒŒ์ด์ฌ ์ƒํ˜ธ์ž‘์šฉ ๋ชจ๋“œ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค. bash % python3 Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> ์ด์ „์— ์ž‘์„ฑํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž„ํฌํŠธ ํ•ด๋ณด์ž. ์ด์ „๊ณผ ๋˜‘๊ฐ™์ด ์ถœ๋ ฅ์ด ํ‘œ์‹œ๋œ๋‹ค. ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. >>> import bounce ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> import mortgage ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> import report ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> ์œ„์™€ ๊ฐ™์ด ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํŒŒ์ด์ฌ์„ ์ž˜๋ชป๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์‹คํ–‰ํ•˜๊ณ  ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค. ์ด์ œ fileparse ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธํ•˜๊ณ  ๋„์›€๋ง์„ ํ‘œ์‹œํ•ด ๋ณด์ž. >>> import fileparse >>> help(fileparse) ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> dir(fileparse) ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> ์ด ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š”๋‹ค. >>> portfolio = fileparse.parse_csv('Data/portfolio.csv',select=['name','shares','price'], types=[str, int, float]) >>> portfolio ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> pricelist = fileparse.parse_csv('Data/prices.csv',types=[str, float], has_headers=False) >>> pricelist ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> prices = dict(pricelist) >>> prices ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> prices['IBM'] 106.11 >>> ๋ชจ๋“ˆ ์ด๋ฆ„์„ ํฌํ•จํ•˜์ง€ ์•Š๋„๋ก ํ•จ์ˆ˜๋ฅผ ์ž„ํฌํŠธ ํ•ด ๋ณด์ž. >>> from fileparse import parse_csv >>> portfolio = parse_csv('Data/portfolio.csv', select=['name','shares','price'], types=[str, int, float]) >>> portfolio ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> ์—ฐ์Šต ๋ฌธ์ œ 3.12: ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ ์‚ฌ์šฉํ•˜๊ธฐ ์„น์…˜ 2์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฃผ์‹ ๋ณด๊ณ ์„œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” report.py ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ–ˆ๋‹ค. Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 ๋ชจ๋“  ์ž…๋ ฅ ํŒŒ์ผ์˜ ์ฒ˜๋ฆฌ์— fileparse ๋ชจ๋“ˆ์— ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•˜๋ผ. ์ด ์ž‘์—… ์œ„ํ•ด fileparse๋ฅผ ๋ชจ๋“ˆ๋กœ์„œ ์ž„ํฌํŠธํ•˜๊ณ  read_portfolio() ์™€ read_prices() ํ•จ์ˆ˜์—์„œ parse_csv() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ์ˆ˜์ •ํ•œ๋‹ค. ์ด ์—ฐ์Šต ๋ฌธ์ œ์˜ ์ฒ˜์Œ์— ์˜ˆ๋กœ ๋“  ์ƒํ˜ธ์ž‘์šฉ์ ์ธ ์˜ˆ์ œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ผ. ์ˆ˜์ • ํ›„์—๋„ ์ด์ „๊ณผ ๋˜‘๊ฐ™์ด ์ถœ๋ ฅ๋˜์–ด์•ผ ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 3.14: ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ๋ฅผ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ธฐ ์„น์…˜ 1์—์„œ, ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์ฝ์–ด ๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜๋Š” pcost.py ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ–ˆ๋‹ค. >>> import pcost >>> pcost.portfolio_cost('Data/portfolio.csv') 44671.15 >>> report.read_portfolio() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ pcost.py ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜๋ผ. ๋ถ€์—ฐ ์„ค๋ช… ์ด ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ๋งžํžˆ๋ฉด ์„ธ ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐ–๊ฒŒ ๋œ๋‹ค. fileparse.py์—๋Š” ๋ฒ”์šฉ parse_csv() ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. report.py๋Š” ํ›Œ๋ฅญํ•œ ๋ณด๊ณ ์„œ๋ฅผ ์ƒ์„ฑํ•˜๋ฉฐ, read_portfolio()์™€ read_prices() ํ•จ์ˆ˜๋„ ๊ฐ–๋Š”๋‹ค. ๋์œผ๋กœ, pcost.py๋Š” ํฌํŠธํด๋ฆฌ์˜ค ๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜๋ฉฐ, report.py๋ฅผ ์œ„ํ•ด ์ž‘์„ฑ๋œ read_portfolio() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 3.5 ๋ฉ”์ธ ๋ชจ๋“ˆ ์ด ์„น์…˜์€ ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ ํ˜น์€ ๋ฉ”์ธ ๋ชจ๋“ˆ์˜ ๊ฐœ๋…์„ ์†Œ๊ฐœํ•œ๋‹ค. ๋ฉ”์ธ ํ•จ์ˆ˜ ์—ฌ๋Ÿฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—๋Š” ๋ฉ”์ธ(main) ํ•จ์ˆ˜ ๋˜๋Š” ๋ฉ”์„œ๋“œ ๊ฐœ๋…์ด ์žˆ๋‹ค. // c / c++ int main(int argc, char *argv[]) { ... } // ์ž๋ฐ” class myprog { public static void main(String args[]) { ... } } ์ด๊ฒƒ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ•  ๋•Œ ์ฒ˜์Œ ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. ํŒŒ์ด์ฌ ๋ฉ”์ธ ๋ชจ๋“ˆ ํŒŒ์ด์ฌ์—๋Š” ๋ฉ”์ธ ํ•จ์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์—†๋‹ค. ๊ทธ ๋Œ€์‹  ๋ฉ”์ธ ๋ชจ๋“ˆ์ด ์žˆ๋‹ค. ์ฒ˜์Œ ์‹คํ–‰ํ•˜๋Š” ์†Œ์Šค ํŒŒ์ผ์ด ๋ฉ”์ธ ๋ชจ๋“ˆ์ด๋‹ค. bash % python3 prog.py ... ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— ๋ฌด์—‡์„ ์ „๋‹ฌํ•˜๋“ , ๊ทธ๊ฒƒ์ด ๋ฉ”์ธ ๋ชจ๋“ˆ์ด ๋œ๋‹ค. ์ด๋ฆ„์ด ๋ฌด์—‡์ด๋“  ์ƒ๊ด€์—†๋‹ค. __main__ ํ™•์ธ ๋ชจ๋“ˆ์ด ๋ฉ”์ธ ์Šคํฌ๋ฆฝํŠธ๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๋Š” ๊ด€๋ก€์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค. # prog.py ... if __name__ == '__main__': # ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ์„œ ์‹คํ–‰ ... ๋ฌธ์žฅ ... ์œ„ if ๋ฌธ์— ์†ํ•œ ๋ฌธ์žฅ์ด ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ์ด ๋œ๋‹ค. ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ vs. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ ๋ชจ๋“  ํŒŒ์ด์ฌ ํŒŒ์ผ์€ ๋ฉ”์ธ์œผ๋กœ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ์„œ ์ž„ํฌํŠธ ๋œ๋‹ค. bash % python3 prog.py # ๋ฉ”์ธ์œผ๋กœ์„œ ์‹คํ–‰ import prog # ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ ๋˜์–ด ์‹คํ–‰ ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ __name__์€ ๋ชจ๋“ˆ์˜ ์ด๋ฆ„์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฉ”์ธ์œผ๋กœ์„œ ์‹คํ–‰ํ•  ๋•Œ๋งŒ __main__์ด ๋œ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ๋กœ์„œ ์‹คํ–‰ํ•  ๋•Œ๋Š” ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ์„œ ์‹คํ–‰ํ•˜๋Š” ๋ฌธ์žฅ์„ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์–ด๋–ค ์“ฐ์ž„์ƒˆ์ธ์ง€๋ฅผ if๋กœ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. if __name__ == '__main__': # ์ž„ํฌํŠธ ๋  ๋•Œ๋Š” ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค ... ํ”„๋กœ๊ทธ๋žจ ํ…œํ”Œ๋ฆฟ ๋‹ค์Œ์€ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ณตํ†ต์ ์ธ ํ”„๋กœ๊ทธ๋žจ ํ…œํ”Œ๋ฆฟ์ด๋‹ค. # prog.py # Import ๋ฌธ(๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ) import modules # ํ•จ์ˆ˜ def spam(): ... def blah(): ... # ๋ฉ”์ธ ํ•จ์ˆ˜ def main(): ... if __name__ == '__main__': main() ๋ช…๋ นํ–‰ ๋„๊ตฌ ํŒŒ์ด์ฌ์€ ์ข…์ข… ๋ช…๋ นํ–‰ ๋„๊ตฌ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. bash % python3 report.py portfolio.csv prices.csv ๊ทธ ๋ง์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์…ธ ๋˜๋Š” ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.<NAME>๋„๋Š” ์ž๋™ํ™”, ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ๋“ฑ์ด๋‹ค. ๋ช…๋ นํ–‰ ์ธ์ž ๋ช…๋ นํ–‰์€ ํ…์ŠคํŠธ ์—ด์˜ ๋ฆฌ์ŠคํŠธ๋‹ค. bash % python3 report.py portfolio.csv prices.csv ์ด๋Ÿฌํ•œ ํ…์ŠคํŠธ ์—ด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ sys.argv์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. # ์œ„ bash ๋ช…๋ น์—์„œ sys.argv # ['report.py, 'portfolio.csv', 'prices.csv'] ์ธ์ž๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์ž. import sys if len(sys.argv) != 3: raise SystemExit(f'Usage: {sys.argv[0]} ' 'portfile pricefile') portfile = sys.argv[1] pricefile = sys.argv[2] ... ํ‘œ์ค€ ์ž…์ถœ๋ ฅ ํ‘œ์ค€ ์ž…์ถœ๋ ฅ(stdio)์€ ์ผ๋ฐ˜ ํŒŒ์ผ๊ณผ ๋˜‘๊ฐ™์ด ์ž‘๋™ํ•˜๋Š” ํŒŒ์ผ์ด๋‹ค. sys.stdout sys.stderr sys.stdin print๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ sys.stdout๋กœ ์ง€์ •(direct) ๋œ๋‹ค. ์ž…๋ ฅ์€ sys.stdin์œผ๋กœ๋ถ€ํ„ฐ ์ฝ๋Š”๋‹ค. ํŠธ๋ ˆ์ด์Šค ๋ฐฑ(Traceback)๊ณผ ์˜ค๋ฅ˜(error)๋Š” sys.stderr๋กœ ์ง€์ •๋œ๋‹ค. stdio๋Š” ํ„ฐ๋ฏธ๋„, ํŒŒ์ผ, ํŒŒ์ดํ”„ ๋“ฑ์— ์—ฐ๊ฒฐ๋  ์ˆ˜ ์žˆ๋‹ค. bash % python3 prog.py > results.txt # ๋˜๋Š” bash % cmd1 | python3 prog.py | cmd2 ํ™˜๊ฒฝ ๋ณ€์ˆ˜(Environment Variable) ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋Š” ์…ธ์—์„œ ์„ค์ •๋œ๋‹ค. bash % setenv NAME dave bash % setenv RSH ssh bash % python3 prog.py os.environ์€ ์ด๋Ÿฌํ•œ ๊ฐ’์„ ๋‹ด๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋‹ค. import os name = os.environ['NAME'] # 'dave' ๋ณ€๊ฒฝ์€ ํ”„๋กœ๊ทธ๋žจ์ด ๋‚˜์ค‘์— ์‹คํ–‰์‹œํ‚ค๋Š” ์„œ๋ธŒ ํ”„๋กœ์„ธ์Šค์— ๋ฐ˜์˜๋œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ(Program Exit) ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ๋Š” ์˜ˆ์™ธ๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌ๋œ๋‹ค. raise SystemExit raise SystemExit(exitcode) raise SystemExit('Informative message') ๋Œ€์•ˆ. import sys sys.exit(exitcode) 0์ด ์•„๋‹Œ ์ข…๋ฃŒ ์ฝ”๋“œ(exit code)๋Š” ์˜ค๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. #! ํ–‰ ์œ ๋‹‰์Šค์—์„œ #! ํ–‰์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํŒŒ์ด์ฌ์œผ๋กœ์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ์ฒซํ–‰์— ์ถ”๊ฐ€ํ•œ๋‹ค. #!/usr/bin/env python3 # prog.py ... ์‹คํ–‰ ๊ถŒํ•œ์ด ํ•„์š”ํ•˜๋‹ค. bash % chmod +x prog.py # Then you can execute bash % prog.py ... ์ถœ๋ ฅ ... ์ฐธ๊ณ : ์œˆ๋„์˜ Python Launcher๋„ ์–ธ์–ด ๋ฒ„์ „์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด #! ํ–‰์„ ์ฐพ๋Š”๋‹ค. ์Šคํฌ๋ฆฝํŠธ ํ…œํ”Œ๋ฆฟ ๋์œผ๋กœ, ๋ช…๋ นํ–‰ ์Šคํฌ๋ฆฝํŠธ๋กœ์„œ ์‹คํ–‰๋˜๋Š” ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์œ„ํ•œ ๊ณตํ†ต์ ์ธ ์ฝ”๋“œ ํ„ฐ๋ฏธ๋„์ด ์žˆ๋‹ค. #!/usr/bin/env python3 # prog.py # Import statements (libraries) import modules # Functions def spam(): ... def blah(): ... # ๋ฉ”์ธ ํ•จ์ˆ˜ def main(argv): # ๋ช…๋ นํ–‰ ์ธ์ž, ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋“ฑ์„ ํŒŒ์‹ฑ. ... if __name__ == '__main__': import sys main(sys.argv) ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 3.15: main() ํ•จ์ˆ˜ report.py ํŒŒ์ผ์— ๋ช…๋ นํ–‰ ์˜ต์…˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์•„ ์ด์ „๊ณผ ๊ฐ™์€ ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๋Š” main() ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์ž. ๊ทธ๊ฒƒ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒํ˜ธ์ž‘์šฉ์ ์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. >>> import report >>> report.main(['report.py', 'Data/portfolio.csv', 'Data/prices.csv']) Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 >>> ๋น„์Šทํ•œ main() ํ•จ์ˆ˜๋ฅผ ๊ฐ–๋„๋ก pcost.py ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์ž. >>> import pcost >>> pcost.main(['pcost.py', 'Data/portfolio.csv']) Total cost: 44671.15 >>> ์—ฐ์Šต ๋ฌธ์ œ 3.16: ์Šคํฌ๋ฆฝํŠธ ๋งŒ๋“ค๊ธฐ report.py์™€ pcost.py ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•ด ๋ช…๋ นํ–‰์—์„œ ์Šคํฌ๋ฆฝํŠธ๋กœ์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ๋ณด์ž. bash $ python3 report.py Data/portfolio.csv Data/prices.csv Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 bash $ python3 pcost.py Data/portfolio.csv Total cost: 44671.15 3.6 ์„ค๊ณ„์— ๊ด€ํ•œ ๋…ผ์˜ ์ด ์„น์…˜์—์„œ๋Š” ์•ž์—์„œ ๊ฒฐ์ •ํ•œ ์„ค๊ณ„๋ฅผ ์žฌ๊ฒ€ํ† ํ•œ๋‹ค. ํŒŒ์ผ๋ช… vs ์ดํ„ฐ๋Ÿฌ๋ธ” ๊ฐ™์€ ์ถœ๋ ฅ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋‹ค์Œ ๋‘ ํ”„๋กœ๊ทธ๋žจ์„ ๋น„๊ตํ•ด ๋ณด์ž. # ํŒŒ์ผ๋ช…์„ ์ œ๊ณต def read_data(filename): records = [] with open(filename) as f: for line in f: ... records.append(r) return records d = read_data('file.csv') # ํ–‰์„ ์ œ๊ณต def read_data(lines): records = [] for line in lines: ... records.append(r) return records with open('file.csv') as f: d = read_data(f) ์–ด๋Š ํ•จ์ˆ˜๋ฅผ ์„ ํ˜ธํ•˜๋Š”๊ฐ€? ์ด์œ ๋Š”? ์ด ํ•จ์ˆ˜ ์ค‘ ์–ด๋Š ๊ฒƒ์ด ๋” ์œ ์—ฐํ•œ๊ฐ€? ์‹ฌ์˜คํ•œ ์•„์ด๋””์–ด: "๋• ํƒ€์ดํ•‘" ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋• ํƒ€์ดํ•‘(Duck Typing)์ด๋ž€, ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ํŠน์ • ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ๊ณผ ๊ด€๋ จ๋œ ๊ฐœ๋…์ด๋‹ค. ๋• ํ…Œ์ŠคํŠธ(duck test)์˜ ์˜ˆ๋ฅผ ๋“ค์–ด ์„ค๋ช…ํ•œ๋‹ค. ๋งŒ์•ฝ ์–ด๋–ค ๊ฒƒ์ด ์˜ค๋ฆฌ์ฒ˜๋Ÿผ ์ƒ๊ฒผ๊ณ , ์˜ค๋ฆฌ์ฒ˜๋Ÿผ ํ—ค์—„์น˜๊ณ , ์˜ค๋ฆฌ์ฒ˜๋Ÿผ ๊ฝฅ๊ฝฅ๊ฑฐ๋ฆฐ๋‹ค๋ฉด, ๊ทธ๊ฒƒ์€ ์•„๋งˆ๋„ ์˜ค๋ฆฌ์ผ ๊ฒƒ์ด๋‹ค. ์œ„ read_data()์˜ ๋‘ ๋ฒˆ์งธ ๋ฒ„์ „์—์„œ, ํ•จ์ˆ˜๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ” ๊ฐ์ฒด๋ฅผ ์˜ˆ์ƒํ•œ๋‹ค. ํŒŒ์ผ์˜ ํ–‰์— ๊ตญํ•œ๋˜์ง€ ์•Š๋Š”๋‹ค. def read_data(lines): records = [] for line in lines: ... records.append(r) return records ์ด๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋‹ค๋ฅธ lines๋ฅผ ๊ฐ€์ง€๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•œ๋‹ค. # CSV ํŒŒ์ผ lines = open('data.csv') data = read_data(lines) # ZIP ์••์ถ•๋œ ํŒŒ์ผ lines = gzip.open('data.csv.gz','rt') data = read_data(lines) # ํ‘œ์ค€ ์ž…๋ ฅ lines = sys.stdin data = read_data(lines) # ๋ฌธ์ž์—ด์˜ ๋ฆฌ์ŠคํŠธ lines = ['ACME, 50,91.1','IBM, 75,123.45', ... ] data = read_data(lines) ์ด ์„ค๊ณ„๋Š” ์ƒ๋‹นํžˆ ์œ ์—ฐํ•˜๋‹ค. ์งˆ๋ฌธ: ์ด๋Ÿฌํ•œ ์œ ์—ฐ์„ฑ์„ ํ™˜์˜ํ•ด์•ผ ํ• ๊นŒ, ์•„๋‹ˆ๋ฉด ๊บผ๋ ค์•ผ ํ• ๊นŒ? ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค๊ณ„ ๋ชจ๋ฒ” ์‚ฌ๋ก€ ์œ ์—ฐํ•œ ์ฝ”๋“œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋” ๋‚˜์€ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ต์…˜์„ ์ œํ•œํ•˜์ง€ ๋ง์ž. ์œ ์—ฐ์„ฑ์ด ๋†’์„์ˆ˜๋ก ๋” ๊ฐ•๋ ฅํ•ด์ง„๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 3.17: ํŒŒ์ผ๋ช…์—์„œ ํŒŒ์ผ ๋น„์Šทํ•œ ๊ฐ์ฒด๋กœ ์•ž์—์„œ parse_csv() ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š” fileparse.py ํŒŒ์ผ์„ ์ž‘์„ฑํ–ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘๋™ํ•œ๋‹ค. >>> import fileparse >>> portfolio = fileparse.parse_csv('Data/portfolio.csv', types=[str, int, float]) >>> ์ด ํ•จ์ˆ˜๋Š” ํŒŒ์ผ๋ช…์ด ์ „๋‹ฌ๋  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•œ๋‹ค. ์ด ์ฝ”๋“œ์˜ ์œ ์—ฐ์„ฑ์„ ์ข€ ๋” ๋†’์ด๋ฉด ์ข‹์„ ๊ฒƒ์ด๋‹ค. ํŒŒ์ผ๊ณผ ์œ ์‚ฌํ•˜๊ฑฐ๋‚˜ ์ดํ„ฐ๋Ÿฌ๋ธ”ํ•œ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด๋ผ. ์˜ˆ: >>> import fileparse >>> import gzip >>> with gzip.open('Data/portfolio.csv.gz', 'rt') as file: ... port = fileparse.parse_csv(file, types=[str, int, float]) ... >>> lines = ['name, shares, price', 'AA, 100,34.23', 'IBM, 50,91.1', 'HPE, 75,45.1'] >>> port = fileparse.parse_csv(lines, types=[str, int, float]) >>> ์ƒˆ๋กœ์šด ์ฝ”๋“œ์— ์˜ˆ์ „์ฒ˜๋Ÿผ ํŒŒ์ผ๋ช…์„ ์ „๋‹ฌํ•˜๋ฉด ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”๊ฐ€? >>> port = fileparse.parse_csv('Data/portfolio.csv', types=[str, int, float]) >>> port ... ์ถœ๋ ฅ์„ ์‚ดํŽด๋ณด๋ผ(๋ฏธ์ณค๋‹ค) ... >>> ๊ทธ๋ ‡๋‹ค. ์กฐ์‹ฌํ•ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ ์ผ์ด ์ƒ๊ธฐ์ง€ ์•Š๊ฒŒ ์•ˆ์ „์žฅ์น˜๋ฅผ ๋‘˜ ์ˆ˜๋Š” ์—†์„๊นŒ? ์—ฐ์Šต ๋ฌธ์ œ 3.18: ๊ธฐ์กด ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ parse_csv()์˜ ์ˆ˜์ •๋œ ๋ฒ„์ „๊ณผ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๊ฒŒ, report.py ํŒŒ์ผ์˜ read_portfolio()์™€ read_prices() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด๋ผ. ์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ๋งŒ ๊ณ ์ณ์•ผ ํ•œ๋‹ค. ์ˆ˜์ •ํ•œ ํ›„์—๋Š” report.py์™€ pcost.py ํ”„๋กœ๊ทธ๋žจ์ด ์ด์ „๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•ด์•ผ ํ•œ๋‹ค. 4. ํด๋ž˜์Šค์™€ ๊ฐ์ฒด ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ํ”„๋กœ๊ทธ๋žจ์€ ํŒŒ์ด์ฌ ๋นŒํŠธ์ธ ์ž๋ฃŒํ˜•๋งŒ ์‚ฌ์šฉํ–ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ํด๋ž˜์Šค์™€ ๊ฐ์ฒด ๊ฐœ๋…์„ ๋„์ž…ํ•œ๋‹ค. ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” class ๋ฌธ์„ ๋ฐฐ์šด๋‹ค. ์ƒ์† ๊ฐœ๋…๋„ ์†Œ๊ฐœํ•˜๋ฉฐ, ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋„๊ตฌ๋ฅผ ๋‹ค๋ฃฌ๋‹ค. ๋์œผ๋กœ, ํŠน์ˆ˜ํ•œ ๋ฉ”์„œ๋“œ, ๋™์  ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์ฐพ๊ธฐ, ์ƒˆ๋กœ์šด ์˜ˆ์™ธ ์ •์˜ ๋“ฑ ํด๋ž˜์Šค์™€ ๊ด€๋ จํ•œ ๊ธฐ๋Šฅ์„ ์‚ดํŽด๋ณธ๋‹ค. 4.1 ํด๋ž˜์Šค 4.2 ์ƒ์† 4.3 ํŠน์ˆ˜ ๋ฉ”์„œ๋“œ 4.4 ์ƒˆ๋กœ์šด ์˜ˆ์™ธ ์ •์˜ํ•˜๊ธฐ 4.1 ํด๋ž˜์Šค ์ด ์„น์…˜์€ class ๋ฌธ์„ ์†Œ๊ฐœํ•˜๊ณ  ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์•„์ด๋””์–ด๋ฅผ ์ œ์‹œํ•œ๋‹ค. ๊ฐ์ฒด ์ง€ํ–ฅ(Object Oriented, OO) ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฝ”๋“œ๋ฅผ ๊ฐ์ฒด์˜ ๋ชจ์Œ์œผ๋กœ์จ ์กฐ์งํ™”ํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฒ•์ด๋‹ค. ๊ฐ์ฒด๋Š” ๋‹ค์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ๋ฐ์ดํ„ฐ(Data). ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ(Attribute) ํ–‰์œ„(Behavior). ๋ฉ”์„œ๋“œ(method) - ๊ฐ์ฒด์— ์ ์šฉ๋˜๋Š” ํ•จ์ˆ˜. ์ด ์ฝ”์Šค์—์„œ ์ด๋ฏธ ๊ฐ์ฒด์ง€ํ–ฅ์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์กฐ์ž‘ํ•  ๋•Œ, >>> nums = [1, 2, 3] >>> nums.append(4) # ๋ฉ”์„œ๋“œ >>> nums.insert(1,10) # ๋ฉ”์„œ๋“œ >>> nums [1, 10, 2, 3, 4] # ๋ฐ์ดํ„ฐ >>> nums๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋‹ค. ๋ฉ”์„œ๋“œ(append()์™€ insert())๋Š” ์ธ์Šคํ„ด์Šค(nums)์— ๋ถ™์–ด ์žˆ๋‹ค. class ๋ฌธ class ๋ฌธ์„ ์‚ฌ์šฉํ•ด ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•œ๋‹ค. class Player: def __init__(self, x, y): self.x = x self.y = y self.health = 100 def move(self, dx, dy): self.x += dx self.y += dy def damage(self, pts): self.health -= pts ๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ ํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šค๋ผ ๋ถ€๋ฅด๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ์—ฌ๋Ÿฌ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜์˜ ์ง‘ํ•ฉ์ฒด๋‹ค. ์ธ์Šคํ„ด์Šค ์ธ์Šคํ„ด์Šค๋Š” ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋‹ค๋ฃจ๋Š” ์‹ค์ œ ๊ฐ์ฒด๋‹ค. ํด๋ž˜์Šค๋ฅผ ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœํ•˜์—ฌ ์ƒ์„ฑํ•œ๋‹ค. >>> a = Player(2, 3) >>> b = Player(10, 20) >>> a์™€ b๋Š” Player์˜ ์ธ์Šคํ„ด์Šค๋‹ค. ๊ฐ•์กฐ: class ๋ฌธ์€ ์ •์˜(definition) ์ผ๋ฟ์ด๋‹ค(๊ทธ ์ž์ฒด๋กœ๋Š” ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š๋Š”๋‹ค). ํ•จ์ˆ˜ ์ •์˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ์ธ์Šคํ„ด์Šค ๋ฐ์ดํ„ฐ ์ธ์Šคํ„ด์Šค์—๋Š” ์ž์ฒด ๋กœ์ปฌ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค. >>> a.x >>> b.x 10 ์ด ๋ฐ์ดํ„ฐ๋Š” __init__()์— ์˜ํ•ด ์ดˆ๊ธฐํ™”๋œ๋‹ค. class Player: def __init__(self, x, y): # `self`์— ์ €์žฅ๋œ ๊ฐ’์€ ์ธ์Šคํ„ด์Šค ๋ฐ์ดํ„ฐ๋‹ค self.x = x self.y = y self.health = 100 ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ์˜ ๊ฐœ์ˆ˜๋‚˜ ์œ ํ˜•์—๋Š” ์ œํ•œ์ด ์—†๋‹ค. ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค์— ์ ์šฉ๋˜๋Š” ํ•จ์ˆ˜๋‹ค. class Player: ... # `move`๋Š” ๋ฉ”์„œ๋“œ๋‹ค def move(self, dx, dy): self.x += dx self.y += dy ๊ฐ์ฒด ๊ทธ ์ž์ฒด๊ฐ€ ํ•ญ์ƒ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ๋‹ค. >>> a.move(1, 2) # `a`๋ฅผ `self`์™€ ๋งค์น˜ # `1`์„ `dx`์™€ ๋งค์น˜ # `2`๋ฅผ `dy`์™€ ๋งค์น˜ def move(self, dx, dy): ๊ด€๋ก€์ ์œผ๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ self๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ๋กœ ์–ด๋–ค ์ด๋ฆ„์„ ์“ฐ๋Š”์ง€๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค. ๊ฐ์ฒด๋Š” ํ•ญ์ƒ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ๋‹ค. ์ด ์ธ์ž๋ฅผ self๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๊ฒƒ์€ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์Šคํƒ€์ผ์ผ ๋ฟ์ด๋‹ค. ํด๋ž˜์Šค ์Šค์ฝ”ํ•‘ ํด๋ž˜์Šค๋Š” ์ด๋ฆ„์˜ ์Šค์ฝ”ํ”„๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. class Player: ... def move(self, dx, dy): self.x += dx self.y += dy def left(self, amt): move(-amt, 0) # NO. ๊ธ€๋กœ๋ฒŒ `move` ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ self.move(-amt, 0) # YES. ์œ„์— ์ •์˜ํ•œ `move` ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ. ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์„ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด, ํ•ญ์ƒ ๋ช…์‹œ์ ์œผ๋กœ ์ฐธ์กฐํ•œ๋‹ค(์˜ˆ: self). ์—ฐ์Šต ๋ฌธ์ œ ์ง€๊ธˆ๋ถ€ํ„ฐ ๋‚˜์˜ค๋Š” ์—ฐ์Šต ๋ฌธ์ œ๋Š” ์ด์ „ ์„น์…˜์—์„œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 3.18์˜ ์ฝ”๋“œ๊ฐ€ ์˜ฌ๋ฐ”๋กœ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ๊ฐ€์ง€๊ณ  ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ๋ชปํ•  ๊ฒฝ์šฐ, Solutions/3_18 ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ํ•ด๋‹ต ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์‹ค์Šตํ•˜๋ผ. ๋ณต์‚ฌํ•ด์„œ ์จ๋„ ๋œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 4.1: ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ์„œ์˜ ๊ฐ์ฒด ์„น์…˜ 2์™€ 3์—์„œ, ํŠœํ”Œ๊ณผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ์„œ ํ‘œํ˜„๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์ž‘์—…ํ–ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ณด์œ  ์ฃผ์‹์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŠœํ”Œ๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค. s = ('GOOG',100,490.10) ๋˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. s = { 'name' : 'GOOG', 'shares' : 100, 'price' : 490.10 } ๊ทธ๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ: def cost(s): return s['shares'] * s['price'] ๊ทธ๋ ‡์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ์ด ์ปค์ ธ๊ฐ์— ๋”ฐ๋ผ ์กฐ์งํ™”๋ฅผ ๋” ์ž˜ ํ•˜๊ณ  ์‹ถ์–ด์งˆ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•จ์œผ๋กœ์จ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์ ‘๊ทผ์„ ์ทจํ•  ์ˆ˜ ์žˆ๋‹ค. stock.py๋ผ๋Š” ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋ณด์œ  ์ฃผ์‹ ํ•œ ์ข…๋ชฉ์„ ํ‘œํ˜„ํ•˜๋Š” Stock ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์ž. Stock ์ธ์Šคํ„ด์Šค์—๋Š” name, shares, price ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๊ฐ€ ์žˆ๋‹ค. ์˜ˆ: >>> import stock >>> a = stock.Stock('GOOG',100,490.10) >>> a.name 'GOOG' >>> a.shares 100 >>> a.price 490.1 >>> Stock ๊ฐ์ฒด๋ฅผ ๋ช‡ ๊ฐœ ๋” ์ƒ์„ฑํ•ด ์กฐ์ž‘ํ•ด ๋ณด์ž. ์˜ˆ: >>> b = stock.Stock('AAPL', 50, 122.34) >>> c = stock.Stock('IBM', 75, 91.75) >>> b.shares * b.price 6117.0 >>> c.shares * c.price 6881.25 >>> stocks = [a, b, c] >>> stocks [<stock.Stock object at 0x37d0b0>, <stock.Stock object at 0x37d110>, <stock.Stock object at 0x37d050>] >>> for s in stocks: print(f'{s.name:>10s} {s.shares:>10d} {s.price:>10.2f}') ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> ์—ฌ๊ธฐ์„œ ๊ฐ•์กฐํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์€ Stock ํด๋ž˜์Šค๊ฐ€ ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํŒฉํ† ๋ฆฌ์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ, ๊ทธ๊ฒƒ์„ ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœํ•ด ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ๊ฐ ๊ฐ์ฒด๋Š”<NAME>๋‹ค๋Š” ์ ์ด ์ค‘์š”ํ•˜๋‹ค. ๊ฐ์ฒด๊ฐ€ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๊ฐ–๊ฒŒ ๋˜๋Š” ๋ฐ์ดํ„ฐ์™€ ๋ณ„๊ฐœ๋‹ค. ํด๋ž˜์Šค์— ์˜ํ•ด ์ •์˜๋œ ๊ฐ์ฒด๋Š”, ๊ตฌ๋ฌธ์€ ์ข€ ๋‹ค๋ฅด์ง€๋งŒ ๋”•์…”๋„ˆ๋ฆฌ์™€๋„ ๋น„์Šทํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, s['name']๊ณผ s['price'] ๋Œ€์‹ , s.name๊ณผ s.price๋ผ๊ณ  ์ž‘์„ฑํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 4.2: ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ํด๋ž˜์Šค์—์„œ๋Š” ๊ฐ์ฒด์— ํ•จ์ˆ˜๋ฅผ ๋ถ™์ผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋ฅผ ๋ฉ”์„œ๋“œ๋ผ ํ•œ๋‹ค. ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด ๋‚ด์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. Stock ๊ฐ์ฒด์— cost()์™€ sell() ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์ž. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘๋™ํ•œ๋‹ค. >>> import stock >>> s = stock.Stock('GOOG', 100, 490.10) >>> s.cost() 49010.0 >>> s.shares 100 >>> s.sell(25) >>> s.shares 75 >>> s.cost() 36757.5 >>> ์—ฐ์Šต ๋ฌธ์ œ 4.3: ์ธ์Šคํ„ด์Šค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋กœ๋ถ€ํ„ฐ Stock ์ธ์Šคํ„ด์Šค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด๋น„์šฉ์„ ๊ณ„์‚ฐํ•œ๋‹ค. >>> import fileparse >>> with open('Data/portfolio.csv') as lines: ... portdicts = fileparse.parse_csv(lines, select=['name','shares','price'], types=[str, int, float]) ... >>> portfolio = [ stock.Stock(d['name'], d['shares'], d['price']) for d in portdicts] >>> portfolio [<stock.Stock object at 0x10c9e2128>, <stock.Stock object at 0x10c9e2048>, <stock.Stock object at 0x10c9e2080>, <stock.Stock object at 0x10c9e25f8>, <stock.Stock object at 0x10c9e2630>, <stock.Stock object at 0x10ca6f748>, <stock.Stock object at 0x10ca6f7b8>] >>> sum([s.cost() for s in portfolio]) 44671.15 >>> ์—ฐ์Šต ๋ฌธ์ œ 4.4: ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์ฝ์–ด ์—ฐ์Šต ๋ฌธ์ œ 4.3์— ๋ณด์ธ ๊ฒƒ๊ณผ ๊ฐ™์€ Stock ์ธ์Šคํ„ด์Šค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๋„๋ก report.py ํ”„๋กœ๊ทธ๋žจ์˜ read_portfolio() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜๋ผ. ์™„๋ฃŒํ•˜๋ฉด, ๋”•์…”๋„ˆ๋ฆฌ ๋Œ€์‹  Stock ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ report.py์™€ pcost.py์˜ ์ฝ”๋“œ๋ฅผ ๋ชจ๋‘ ์ˆ˜์ •ํ•œ๋‹ค. ํžŒํŠธ: ์ฝ”๋“œ๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ์ˆ˜์ •ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ ์•ก์„ธ์Šค s['shares']๋ฅผ s.shares๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ์†Œ์†Œํ•œ ๋ณ€๊ฒฝ์„ ํ•œ๋‹ค. ์ด์ „๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. >>> import pcost >>> pcost.portfolio_cost('Data/portfolio.csv') 44671.15 >>> import report >>> report.portfolio_report('Data/portfolio.csv', 'Data/prices.csv') Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 >>> 4.2 ์ƒ์† ์ƒ์†์€ ํ™•์žฅ์„ฑ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋„๊ตฌ๋‹ค. ์ด ์„น์…˜์—์„œ ๊ทธ ๊ฐœ๋…์„ ์•Œ์•„๋ณด์ž. ๋„์ž… ์ƒ์†์€ ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ํŠน์ˆ˜ํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. class Parent: ... class Child(Parent): ... ์œ„์˜ Child ํด๋ž˜์Šค์™€ ๊ฐ™์€ ๊ฒƒ์„ ํŒŒ์ƒ ํด๋ž˜์Šค(derived class) ๋˜๋Š” ํ•˜์œ„ ํด๋ž˜์Šค(subclass)๋ผ ํ•œ๋‹ค. ์œ„์˜ Parent ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค(base class) ๋˜๋Š” ์ƒ์œ„ ํด๋ž˜์Šค(superclass)๋ผ ํ•œ๋‹ค. class Child(Parent):์—์„œ ํด๋ž˜์Šค๋ช… ๋’ค์˜ ()์— Parent๋ฅผ ์ง€์ •ํ–ˆ๋‹ค. ํ™•์žฅ(Extending) ์ƒ์†์„ ํ†ตํ•ด ๊ธฐ์กด ํด๋ž˜์Šค๋ฅผ ์ทจํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒˆ๋กœ์šด ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ ๊ธฐ์กด ๋ฉ”์„œ๋“œ ์ผ๋ถ€๋ฅผ ์žฌ์ •์˜(redefine) ์ธ์Šคํ„ด์Šค์— ์ƒˆ๋กœ์šด ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ถ”๊ฐ€ ๊ทธ๋ฆฌํ•˜์—ฌ ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ํ™•์žฅํ•˜๊ฒŒ ๋œ๋‹ค. ์ด ํด๋ž˜์Šค์—์„œ ์‹œ์ž‘ํ•œ๋‹ค๊ณ  ํ•˜์ž. class Stock: def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price def cost(self): return self.shares * self.price def sell(self, nshares): self.shares -= nshares ์ƒ์†์„ ํ†ตํ•ด ์ด๊ฒƒ์˜ ์–ด๋Š ๋ถ€๋ถ„์ด๋“  ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒˆ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ class MyStock(Stock): def panic(self): self.sell(self.shares) ์šฉ๋ก€: >>> s = MyStock('GOOG', 100, 490.1) >>> s.sell(25) >>> s.shares 75 >>> s.panic() >>> s.shares >>> ๊ธฐ์กด ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ class MyStock(Stock): def cost(self): return 1.25 * self.shares * self.price ์šฉ๋ก€: >>> s = MyStock('GOOG', 100, 490.1) >>> s.cost() 61262.5 >>> ์ƒˆ ๋ฉ”์„œ๋“œ๋Š” ๊ธฐ์กด ๊ฒƒ์„ ๋Œ€์ฒดํ•œ๋‹ค. ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋Š” ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค. ๊ต‰์žฅํ•˜๋‹ค. ์˜ค๋ฒ„๋ผ์ด๋”ฉ(Overriding) ๋•Œ๋กœ๋Š” ํด๋ž˜์Šค๊ฐ€ ๊ธฐ์กด ๋ฉ”์„œ๋“œ๋ฅผ ํ™•์žฅํ•˜๋˜, ์›๋ž˜ ๊ตฌํ˜„์„ ์žฌ์ •์˜์— ํฌํ•จํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด super()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. class Stock: ... def cost(self): return self.shares * self.price ... class MyStock(Stock): def cost(self): # `super`์— ๋Œ€ํ•œ ํ˜ธ์ถœ์„ ํ™•์ธ actual_cost = super().cost() return 1.25 * actual_cost ์ด์ „ ๋ฒ„์ „์„ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด super()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ฃผ์˜: ํŒŒ์ด์ฌ 2๋Š” ์ด๊ฒƒ๋ณด๋‹ค ๊ตฌ๋ฌธ์ด ๋ณต์žกํ•˜๋‹ค. actual_cost = super(MyStock, self).cost() __init__์™€ ์ƒ์† __init__๋ฅผ ์žฌ์ •์˜ํ•˜๋ ค๋ฉด ๋ถ€๋ชจ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค. class Stock: def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price class MyStock(Stock): def __init__(self, name, shares, price, factor): # `super`์™€ `__init__`์— ๋Œ€ํ•œ ํ˜ธ์ถœ์„ ํ™•์ธ super().__init__(name, shares, price) self.factor = factor def cost(self): return self.factor * super().cost() super์— __init__() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. ์ด๊ฒƒ์ด ์•ž์—์„œ ๋ณธ ๊ฒƒ๊ณผ ๊ฐ™์ด ์ด์ „ ๋ฒ„์ „์„ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ƒ์†์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ƒ์†์€ ๊ด€๋ จ ๊ฐ์ฒด๋ฅผ ์กฐ์งํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๊ณค ํ•œ๋‹ค. class Shape: ... class Circle(Shape): ... class Rectangle(Shape): ... ๋…ผ๋ฆฌ์  ๊ณ„์ธต ๊ตฌ์กฐ๋‚˜ ๋ถ„๋ฅ˜๋ฒ•์„ ์ƒ๊ฐํ•ด ๋ณด๋ผ. ๊ทธ๋ ‡์ง€๋งŒ ์ข€ ๋” ์ผ๋ฐ˜์ ์ด๊ณ ๋„ ์‹ค์šฉ์ ์ธ ์šฉ๋„๋Š” ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ ํ˜น์€ ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ”„๋ ˆ์ž„์›Œํฌ์— ์ •์˜๋œ ๊ธฐ๋ณธ ํด๋ž˜์Šค๋ฅผ ๋‹น์‹ ์ด ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆ ํ•  ์ˆ˜ ์žˆ๋‹ค. class CustomHandler(TCPHandler): def handle_request(self): ... # ์ปค์Šคํ…€ ์ฒ˜๋ฆฌ ๊ธฐ๋ณธ ํด๋ž˜์Šค๋Š” ์ผ๋ฐ˜ ๋ชฉ์  ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•œ๋‹ค. ๋‹น์‹ ์˜ ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ด ํŠน์ˆ˜ํ•œ ๋ถ€๋ถ„์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆ ํ•œ๋‹ค. "is a" ๊ด€๊ณ„ ์ƒ์†์€ ํƒ€์ž… ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•œ๋‹ค. class Shape: ... class Circle(Shape): ... ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ ํ™•์ธํ•˜๋ผ. >>> c = Circle(4.0) >>> isinstance(c, Shape) True >>> ์ค‘์š”: ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹ค๋ฃจ๋Š” ์ฝ”๋“œ๋Š” ์ž์‹ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด์„œ๋„ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์ด ์ด์ƒ์ ์ด๋‹ค. object ๊ธฐ๋ณธ ํด๋ž˜์Šค ๋งŒ์•ฝ ํด๋ž˜์Šค์— ๋ถ€๋ชจ๊ฐ€ ์—†์œผ๋ฉด object๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์‚ผ์„ ์ˆ˜ ์žˆ๋‹ค. class Shape(object): ... ํŒŒ์ด์ฌ์—์„œ object๋Š” ๋ชจ๋“  ๊ฐ์ฒด์˜ ๋ถ€๋ชจ๋‹ค. *์ฐธ๊ณ : ๊ธฐ์ˆ ์ ์œผ๋กœ ์ด๊ฒƒ์ด ํ•„์š”ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ, ํŒŒ์ด์ฌ 2์—์„œ ํ•„์š”ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‚จ๊ฒจ๋‘” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. object๋ฅผ ์ƒ๋žตํ•˜๋”๋ผ๋„ ์•”์‹œ์ ์œผ๋กœ ์ƒ์†ํ•œ๋‹ค. ๋‹ค์ค‘ ์ƒ์† ์—ฌ๋Ÿฌ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†ํ•˜๋„๋ก ํด๋ž˜์Šค์— ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. class Mother: ... class Father: ... class Child(Mother, Father): ... Child ํด๋ž˜์Šค๋Š” Mother์™€ Father์˜ ํŠน์ง•์„ ๋ชจ๋‘ ์ƒ์†๋ฐ›๋Š”๋‹ค. ๋‹ค์ค‘ ์ƒ์†์€ ์„ธ๋ถ€์ ์œผ๋กœ ๋“ค์–ด๊ฐ€๋ฉด ๊ณจ์น˜๊ฐ€ ์•„ํŒŒ์ง„๋‹ค. ์ •ํ™•ํžˆ ์ดํ•ดํ•˜๊ธฐ ์ „์—๋Š” ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ. ๋‹ค์Œ ์„น์…˜์—์„œ ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๊ฒ ์ง€๋งŒ, ์ด ์ฝ”์Šค์—์„œ ๋‹ค์ค‘ ์ƒ์†์„ ์‚ฌ์šฉํ•  ์ผ์€ ์—†๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์ƒ์†์˜ ์ฃผ ์šฉ๋„๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ™•์žฅ ๋˜๋Š” ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆ ๋œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์— ์žˆ๋‹ค(ํŠนํžˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ). ์„ค๋ช…์„ ์œ„ํ•ด report.py ํ”„๋กœ๊ทธ๋žจ์˜ print_report() ํ•จ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ๊ทธ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ผ ๊ฒƒ์ด๋‹ค. def print_report(reportdata): ''' (name, shares, price, change) ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ๋ณด๊ธฐ ์ข‹๊ฒŒ ํฌ๋งคํŒ…ํ•œ ํ…Œ์ด๋ธ”์„ ํ”„๋ฆฐํŒ…. ''' headers = ('Name','Shares','Price','Change') print('% 10s % 10s % 10s % 10s' % headers) print(('-'*10 + ' ')*len(headers)) for row in reportdata: print('% 10s % 10d % 10.2f % 10.2f' % row) report ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅํ•œ๋‹ค. >>> import report >>> report.portfolio_report('Data/portfolio.csv', 'Data/prices.csv') Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 ์—ฐ์Šต ๋ฌธ์ œ 4.5: ํ™•์žฅ์„ฑ ๋ฌธ์ œ print_report() ํ•จ์ˆ˜๊ฐ€ ์ผ๋ฐ˜ ํ…์ŠคํŠธ, HTML, CSV, XML ๊ฐ™์ด ๋‹ค์–‘ํ•œ ์ถœ๋ ฅ ํฌ๋งท์„ ์ง€์›ํ•˜๊ฒŒ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ํ•˜์ž. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ ค๊ณ  ๋ชจ๋“  ์ผ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์•„์ฃผ ํฐ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ ค๊ณ  ์‹œ๋„ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ํ–ˆ๋‹ค๊ฐ€๋Š” ์œ ์ง€ ๋ณด์ˆ˜๋ฅผ ํ•˜๊ธฐ ํž˜๋“ค ๋งŒํผ ์ง€์ €๋ถ„ํ•ด์งˆ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ๊ฐ€ ์ƒ์†์„ ์‚ฌ์šฉํ•˜๊ธฐ์— ์•„์ฃผ ์ข‹์€ ๊ธฐํšŒ๋‹ค. ์‹œ์ž‘ํ•˜๋ ค๋ฉด ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ๋‹จ๊ณ„์— ์ง‘์ค‘ํ•˜์ž. ํ…Œ์ด๋ธ”์˜ ์ฒซ ํ–‰์—๋Š” ํ—ค๋”๊ฐ€ ์žˆ๋‹ค. ๊ทธ ํ›„, ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ํ–‰๋“ค์ด ๋‚˜ํƒ€๋‚œ๋‹ค. ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ผ ์ž์ฒด ํด๋ž˜์Šค์— ์ง‘์–ด๋„ฃ์ž. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋Š” tableformat.py ํŒŒ์ผ์„ ์ž‘์„ฑํ•œ๋‹ค. # tableformat.py class TableFormatter: def headings(self, headers): ''' ํ…Œ์ด๋ธ” ํ—ค๋”ฉ์„ ๋ฐ˜ํ™˜. ''' raise NotImplementedError() def row(self, rowdata): ''' ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ์˜ ๋‹จ ์ผํ–‰์„ ๋ฐ˜ํ™˜. ''' raise NotImplementedError() ์ด ํด๋ž˜์Šค๋Š” ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š์ง€๋งŒ, ๊ณง ์ •์˜ํ•  ์ถ”๊ฐ€ ํด๋ž˜์Šค์˜ ์„ค๊ณ„ ์‚ฌ์–‘๊ณผ ์—ญํ• ์„ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ํด๋ž˜์Šค๋ฅผ "์ถ”์ƒ ๊ธฐ๋ณธ ํด๋ž˜์Šค(abstract base class)"๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์€ TableFormatter ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๋„๋ก print_report() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜์ž. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. # report.py ... def print_report(reportdata, formatter): ''' (name, shares, price, change) ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ๋ณด๊ธฐ ์ข‹๊ฒŒ ํฌ๋งคํŒ…ํ•œ ํ…Œ์ด๋ธ”์„ ํ”„๋ฆฐํŒ…. ''' formatter.headings(['Name','Shares','Price','Change']) for name, shares, price, change in reportdata: rowdata = [ name, str(shares), f'{price:0.2f}', f'{change:0.2f}' ] formatter.row(rowdata) print_report()์— ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ–ˆ์œผ๋ฏ€๋กœ, portfolio_report() ํ•จ์ˆ˜๋„ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. TableFormatter๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋ณ€๊ฒฝํ•œ๋‹ค. # report.py import tableformat ... def portfolio_report(portfoliofile, pricefile): ''' ์ฃผ์–ด์ง„ ํฌํŠธํด๋ฆฌ์˜ค์™€ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์ฃผ์‹ ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑ. ''' # ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ฝ๊ธฐ portfolio = read_portfolio(portfoliofile) prices = read_prices(pricefile) # ๋ณด๊ณ ์„œ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ report = make_report_data(portfolio, prices) # ํ”„๋ฆฐํŠธ formatter = tableformat.TableFormatter() print_report(report, formatter) ์ƒˆ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์ž. >>> ================================ RESTART ================================ >>> import report >>> report.portfolio_report('Data/portfolio.csv', 'Data/prices.csv') ... ์ถฉ๋Œ ... ๊ณง๋ฐ”๋กœ NotImplementedError ์˜ˆ์™ธ๋ฅผ ๋‚ด๋ฉฐ ์ถฉ๋Œํ•œ๋‹ค. ์ฉ ์ฆ๊ฒ์ง€๋Š” ์•Š์ง€๋งŒ, ์ด๊ฒƒ์€ ์˜ˆ์ƒํ–ˆ๋˜ ๋ฐ”๋‹ค. ๋‹ค์Œ ๋ถ€๋ถ„์œผ๋กœ ์ด์–ด ๊ฐ€์ž. ์—ฐ์Šต ๋ฌธ์ œ 4.6: ์ƒ์†์„ ํ†ตํ•ด ๋‹ค๋ฅธ ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๊ธฐ (a) ๋ถ€๋ถ„์—์„œ ์ •์˜ํ•œ TableFormatter ํด๋ž˜์Šค๋Š” ์ƒ์†์„ ํ†ตํ•ด ํ™•์žฅํ•  ์˜๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์ •์˜ํ–ˆ๋‹ค. ์‚ฌ์‹ค, ๊ทธ๊ฒƒ์ด ์•„์ด๋””์–ด์˜ ์ „๋ถ€๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด TextTableFormatter ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค. # tableformat.py ... class TextTableFormatter(TableFormatter): ''' ํ…Œ์ด๋ธ”์„ ์ผ๋ฐ˜ ํ…์ŠคํŠธ ํฌ๋งท์œผ๋กœ ์ถœ๋ ฅ ''' def headings(self, headers): for h in headers: print(f'{h:>10s}', end=' ') print() print(('-'*10 + ' ')*len(headers)) def row(self, rowdata): for d in rowdata: print(f'{d:>10s}', end=' ') print() portfolio_report() ํ•จ์ˆ˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜๊ณ  ์‚ฌ์šฉํ•ด ๋ณด์ž. # report.py ... def portfolio_report(portfoliofile, pricefile): ''' ์ฃผ์–ด์ง„ ํฌํŠธํด๋ฆฌ์˜ค์™€ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์ฃผ์‹ ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑ. ''' # ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ฝ๊ธฐ portfolio = read_portfolio(portfoliofile) prices = read_prices(pricefile) # ๋ณด๊ณ ์„œ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ report = make_report_data(portfolio, prices) # ํ”„๋ฆฐํŠธ formatter = tableformat.TextTableFormatter() print_report(report, formatter) ์ด์ „๊ณผ ๊ฐ™์€ ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค. >>> ================================ RESTART ================================ >>> import report >>> report.portfolio_report('Data/portfolio.csv', 'Data/prices.csv') Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 >>> ํ•˜์ง€๋งŒ, ์ถœ๋ ฅ์„ ๋‹ค๋ฅธ ๊ฒƒ์œผ๋กœ ๋ฐ”๊พธ์ž. ์ถœ๋ ฅ์„ CSV ํฌ๋งท์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ์ƒˆ๋กœ์šด ํด๋ž˜์Šค CSVTableFormatter๋ฅผ ์ •์˜ํ•œ๋‹ค. # tableformat.py ... class CSVTableFormatter(TableFormatter): ''' ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ๋ฅผ CSV ํฌ๋งท์œผ๋กœ ์ถœ๋ ฅ. ''' def headings(self, headers): print(','.join(headers)) def row(self, rowdata): print(','.join(rowdata)) ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค. def portfolio_report(portfoliofile, pricefile): ''' ์ฃผ์–ด์ง„ ํฌํŠธํด๋ฆฌ์˜ค์™€ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์ฃผ์‹ ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑ. ''' # ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ฝ๊ธฐ portfolio = read_portfolio(portfoliofile) prices = read_prices(pricefile) # ๋ณด๊ณ ์„œ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ report = make_report_data(portfolio, prices) # ํ”„๋ฆฐํŠธ formatter = tableformat.CSVTableFormatter() print_report(report, formatter) ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ CSV ์ถœ๋ ฅ์„ ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. >>> ================================ RESTART ================================ >>> import report >>> report.portfolio_report('Data/portfolio.csv', 'Data/prices.csv') Name, Shares, Price, Change AA, 100,9.22, -22.98 IBM, 50,106.28,15.18 CAT, 150,35.46, -47.98 MSFT, 200,20.89, -30.34 GE, 95,13.48, -26.89 MSFT, 50,20.89, -44.21 IBM, 100,106.28,35.84 ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๋Š” HTMLTableFormatter ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค. <tr><th>Name</th><th>Shares</th><th>Price</th><th>Change</th></tr> <tr><td>AA</td><td>100</td><td>9.22</td><td>-22.98</td></tr> <tr><td>IBM</td><td>50</td><td>106.28</td><td>15.18</td></tr> <tr><td>CAT</td><td>150</td><td>35.46</td><td>-47.98</td></tr> <tr><td>MSFT</td><td>200</td><td>20.89</td><td>-30.34</td></tr> <tr><td>GE</td><td>95</td><td>13.48</td><td>-26.89</td></tr> <tr><td>MSFT</td><td>50</td><td>20.89</td><td>-44.21</td></tr> <tr><td>IBM</td><td>100</td><td>106.28</td><td>35.84</td></tr> CSVTableFormatter ๊ฐ์ฒด ๋Œ€์‹  HTMLTableFormatter ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•ด ์ฝ”๋“œ๋ฅผ ํ…Œ์ŠคํŠธํ•˜์ž. ์—ฐ์Šต ๋ฌธ์ œ 4.7: ๋‹คํ˜•์„ฑ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์ฃผ๋œ ํŠน์ง•์€ ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•  ํ•„์š” ์—†์ด ๊ฐ์ฒด๋ฅผ ํ”„๋กœ๊ทธ๋žจ์— ๋ผ์›Œ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, TableFormatter ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ–ˆ๋‹ค๊ณ  ํ•˜๋ฉด, ์–ด๋–ค ์ข…๋ฅ˜์˜ TableFormatter๋ฅผ ์ฃผ๋Š”์ง€์— ๊ด€๊ณ„์—†์ด ์ž‘๋™ํ•  ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฌํ•œ ํ–‰์œ„๋ฅผ '๋‹คํ˜•์„ฑ(polymorphism)'์ด๋ผ ํ•œ๋‹ค. ํ•œ ๊ฐ€์ง€ ์ž ์žฌ์ ์ธ ๋ฌธ์ œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ํฌ๋งคํ„ฐ๋ฅผ ์„ ํƒํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. TextTableFormatter ๊ฐ™์€ ํด๋ž˜์Šค๋ช…์„ ์ง์ ‘ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์€ ์ข…์ข… ์„ฑ๊ฐ€์‹œ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹จ์ˆœํ•œ ์ ‘๊ทผ์„ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ์— if ๋ฌธ์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. def portfolio_report(portfoliofile, pricefile, fmt='txt'): ''' ์ฃผ์–ด์ง„ ํฌํŠธํด๋ฆฌ์˜ค์™€ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์ฃผ์‹ ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑ. ''' # ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ฝ๊ธฐ portfolio = read_portfolio(portfoliofile) prices = read_prices(pricefile) # ๋ณด๊ณ ์„œ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ report = make_report_data(portfolio, prices) # ํ”„๋ฆฐํŠธ if fmt == 'txt': formatter = tableformat.TextTableFormatter() elif fmt == 'csv': formatter = tableformat.CSVTableFormatter() elif fmt == 'html': formatter = tableformat.HTMLTableFormatter() else: raise RuntimeError(f'Unknown format {fmt}') print_report(report, formatter) ์ด ์ฝ”๋“œ์—์„œ, ์‚ฌ์šฉ์ž๋Š” 'txt'๋‚˜ 'csv' ๊ฐ™์ด ๋‹จ์ˆœํ™”๋œ ์ด๋ฆ„์„ ์ง€์ •ํ•จ์œผ๋กœ์จ ํฌ๋งท์„ ์„ ํƒํ•œ๋‹ค. ํ•˜์ง€๋งŒ, portfolio_report() ํ•จ์ˆ˜์— ์ปค๋‹ค๋ž€ if ๋ฌธ์„ ๋„ฃ๋Š” ๊ฒƒ์ด ์ตœ์„ ์ธ๊ฐ€? ๊ทธ ์ฝ”๋“œ๋ฅผ ๋‹ค๋ฅธ ๋ฒ”์šฉ ํ•จ์ˆ˜๋กœ ์˜ฎ๊ธฐ๋Š” ๊ฒƒ์ด ๋‚˜์„ ๊ฒƒ์ด๋‹ค. 'txt', 'csv', 'html' ๊ฐ™์€ ์ถœ๋ ฅ ์ด๋ฆ„์— ๋”ฐ๋ผ ์‚ฌ์šฉ์ž๊ฐ€ ํฌ๋งคํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๋Š” create_formatter(name) ํ•จ์ˆ˜๋ฅผ tableformat.py ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•œ๋‹ค. portfolio_report()๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜์ž. def portfolio_report(portfoliofile, pricefile, fmt='txt'): ''' ์ฃผ์–ด์ง„ ํฌํŠธํด๋ฆฌ์˜ค์™€ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์ฃผ์‹ ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑ. ''' # ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ฝ๊ธฐ portfolio = read_portfolio(portfoliofile) prices = read_prices(pricefile) # ๋ณด๊ณ ์„œ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ report = make_report_data(portfolio, prices) # ํ”„๋ฆฐํŠธ formatter = tableformat.create_formatter(fmt) print_report(report, formatter) ์—ฌ๋Ÿฌ ํฌ๋งท์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ํ•จ์ˆ˜๊ฐ€ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž. ์—ฐ์Šต ๋ฌธ์ œ 4.8: ๋ชจ๋‘ ํ•ฉ์น˜๊ธฐ portfolio_report() ํ•จ์ˆ˜๊ฐ€ ์ถœ๋ ฅ ํฌ๋งท์„ ์ง€์ •ํ•˜๋Š” ์„ ํƒ์  ์ธ์ž๋ฅผ ๋ฐ›๊ฒŒ report.py ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•˜๋ผ. ์˜ˆ: >>> report.portfolio_report('Data/portfolio.csv', 'Data/prices.csv', 'txt') Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 >>> ๋ช…๋ นํ–‰์—์„œ ํฌ๋งท์„ ์ง€์ •ํ•˜๊ฒŒ ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•œ๋‹ค. bash $ python3 report.py Data/portfolio.csv Data/prices.csv csv Name, Shares, Price, Change AA, 100,9.22, -22.98 IBM, 50,106.28,15.18 CAT, 150,35.46, -47.98 MSFT, 200,20.89, -30.34 GE, 95,13.48, -26.89 MSFT, 50,20.89, -44.21 IBM, 100,106.28,35.84 bash $ ๋…ผ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ํ™•์žฅ์„ฑ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์ƒ์†์˜ ์ฃผ๋œ ์šฉ๋„๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ๋‹น์‹ ์€ ๊ทธ๊ฒƒ์„ ์ƒ์†ํ•ด ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ฑ„์šด๋‹ค. ๋˜ ํ•œ ๊ฐ€์ง€ ์‹ฌ์˜คํ•œ ๊ฐœ๋…์€ '์ถ”์ƒํ™”๋ฅผ ์ง์ ‘ ์ž‘์„ฑ'ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ ํ…Œ์ด๋ธ”์„ ํฌ๋งคํŒ…ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ง์ ‘ ์ •์˜ํ–ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋“ค์—ฌ๋‹ค๋ณด๊ณ  'ํฌ๋งคํŒ… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๋“ ์ง€ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์ด๋ฏธ ๋งŒ๋“ค์–ด๋†“์€ ๊ฒƒ์„ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค'๋ผ๊ณ  ์ƒ๊ฐํ• ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. ํ•˜์ง€๋งŒ ์ง์ ‘ ๋งŒ๋“  ํด๋ž˜์Šค์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์Šค์Šค๋กœ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋ฉด ์ปคํ”Œ๋ง์ด ์ค„์–ด๋“ค๊ณ  ๋” ์œ ์—ฐํ•ด์ง„๋‹ค. ๋‹น์‹ ์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋‹น์‹ ์˜ ํด๋ž˜์Šค์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•œ, ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ์™„์ „ํ•œ ๋งž์ถค ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๋งŒ๋“  ์„œ๋“œ ํŒŒํ‹ฐ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉํ•˜๋˜ ์„œ๋“œ ํŒŒํ‹ฐ ํŒจํ‚ค์ง€๋ณด๋‹ค ๋” ์ข‹์€ ๊ฒƒ์„ ์ฐพ๊ฒŒ ๋˜๋ฉด ๋‹ค๋ฅธ ๊ฒƒ์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. ํŒจํ‚ค์ง€๋ฅผ ๊ต์ฒดํ•˜๋Š” ๊ฒƒ์ด ๋ณ„๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์œ ์ง€ํ•˜๋Š” ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ๊ฐ€ ์ „ํ˜€ ๊นจ์ง€์ง€ ์•Š์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๊ฒƒ์€ ๊ฐ•๋ ฅํ•œ ์•„์ด๋””์–ด์ด๋ฉฐ, ์ด์™€ ๊ฐ™์€ ์ƒ์†์„ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋‹ค. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋žจ ์„ค๊ณ„๋Š” ๋งค์šฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๋‹ค. ๋” ์•Œ๊ณ  ์‹ถ์œผ๋ฉด ๋””์ž์ธ ํŒจํ„ด์„ ๋‹ค๋ฃจ๋Š” ์ฑ…์„ ์‚ดํŽด๋ณด๊ธฐ ๋ฐ”๋ž€๋‹ค(์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ ์ผ์–ด๋‚œ ์ผ์„ ์ดํ•ดํ•˜๋ฉด ๊ฐ์ฒด๋ฅผ ์‹ค์šฉ์ ์œผ๋กœ ๋‹ค๋ฃจ๋Š” ๋ฐ ์ƒ๋‹นํ•œ ๋„์›€์ด ๋œ๋‹ค). 4.3 ํŠน์ˆ˜ํ•œ ๋ฉ”์„œ๋“œ ํŠน์ˆ˜ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ, ํŒŒ์ด์ฌ์˜ ํ–‰์œ„๋ฅผ ์—ฌ๋Ÿฌ ๋ฉด์—์„œ ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์ˆ˜ ๋ฉ”์„œ๋“œ๋ฅผ โ€œ๋งˆ๋ฒ•(magic)โ€ ๋ฉ”์„œ๋“œ๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค. ์ด ์„น์…˜์—์„œ ๊ทธ ๊ฐœ๋…์„ ์•Œ์•„๋ณด์ž. ๋™์  ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์•ก์„ธ์Šค์™€ ๋ฐ”์šด๋“œ ๋ฉ”์„œ๋“œ๋„ ๋…ผ์˜ํ•œ๋‹ค. ๋„์ž… ํด๋ž˜์Šค์— ํŠน์ˆ˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ์ด๋Ÿฌํ•œ ๋ฉ”์„œ๋“œ๋“ค์„ ํŠน๋ณ„ํ•˜๊ฒŒ ๋‹ค๋ฃฌ๋‹ค. ํ•ญ์ƒ ์•ž์— __๊ฐ€ ๋ถ™๋Š”๋‹ค. (์˜ˆ: __init__) class Stock(object): def __init__(self): ... def __repr__(self): ... ํŠน์ˆ˜ ๋ฉ”์„œ๋“œ๋Š” ๋งŽ์ด ์žˆ์ง€๋งŒ, ๊ทธ์ค‘ ๋ช‡ ๊ฐ€์ง€๋งŒ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด์ž. ๋ฌธ์ž์—ด ๋ณ€ํ™˜์„ ์œ„ํ•œ ํŠน์ˆ˜ ๋ฉ”์„œ๋“œ ๊ฐ์ฒด์—๋Š” ๋‘ ๊ฐ€์ง€ ๋ฌธ์ž์—ด ํ‘œํ˜„์ด ์žˆ๋‹ค. >>> from datetime import date >>> d = date(2012, 12, 21) >>> print(d) 2012-12-21 >>> d datetime.date(2012, 12, 21) >>> str() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ฉ‹์ง€๊ฒŒ ํ”„๋ฆฐํŠธํ•  ์ˆ˜ ์žˆ๋Š” ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•œ๋‹ค. >>> str(d) '2012-12-21' >>> repr() ํ•จ์ˆ˜๋Š” ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ์ƒ์„ธํ•œ ํ‘œํ˜„์— ์‚ฌ์šฉ๋œ๋‹ค. >>> repr(d) 'datetime.date(2012, 12, 21)' >>> ์ด๋Ÿฌํ•œ str()๊ณผ repr() ํ•จ์ˆ˜๋Š” ํŠน์ˆ˜ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด, ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•œ๋‹ค. class Date(object): def __init__(self, year, month, day): self.year = year self.month = month self.day = day # Used with `str()` def __str__(self): return f'{self.year}-{self.month}-{self.day}' # `repr()`๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ def __repr__(self): return f'Date({self.year},{self.month},{self.day})' ์ฐธ๊ณ : __repr__()์ด ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ eval()์— ์ „๋‹ฌํ•˜๋ฉด ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ก€๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด, ๊ทธ ๋Œ€์‹  ์ฝ๊ธฐ ์‰ฌ์šด ํ‘œํ˜„์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ˆ˜ํ•™์„ ์œ„ํ•œ ํŠน์ˆ˜ ๋ฉ”์„œ๋“œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆ˜ํ•™ ์—ฐ์‚ฐ์— ์‚ฌ์šฉ๋œ๋‹ค. a + b a.__add__(b) a - b a.__sub__(b) a * b a.__mul__(b) a / b a.__truediv__(b) a // b a.__floordiv__(b) a % b a.__mod__(b) a << b a.__lshift__(b) a >> b a.__rshift__(b) a & b a.__and__(b) a | b a.__or__(b) a ^ b a.__xor__(b) a ** b a.__pow__(b) -a a.__neg__() ~a a.__invert__() abs(a) a.__abs__() ํ•ญ๋ชฉ ์ ‘๊ทผ์„ ์œ„ํ•œ ํŠน์ˆ˜ ๋ฉ”์„œ๋“œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. len(x) x.__len__() x[a] x.__getitem__(a) x[a] = v x.__setitem__(a, v) del x[a] x.__delitem__(a) ์ด๊ฒƒ๋“ค์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. class Sequence: def __len__(self): ... def __getitem__(self, a): ... def __setitem__(self, a, v): ... def __delitem__(self, a): ... ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์€ ๋‘ ๋‹จ๊ณ„ ๊ณผ์ •์œผ๋กœ ์ด๋ค„์ง„๋‹ค. ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ(lookup): . ์—ฐ์‚ฐ์ž ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ: () ์—ฐ์‚ฐ์ž >>> s = Stock('GOOG',100,490.10) >>> c = s.cost # ๋ฉ”์„œ๋“œ ํ™•์ธ >>> c <bound method Stock.cost of <Stock object at 0x590d0>> >>> c() # ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ 49010.0 >>> ๋ฐ”์šด๋“œ ๋ฉ”์„œ๋“œ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์—ฐ์‚ฐ์ž ()์— ์˜ํ•ด ํ˜ธ์ถœ๋˜์ง€ ์•Š์€ ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ”์šด๋“œ ๋ฉ”์„œ๋“œ(bound method)๋ผ ํ•œ๋‹ค. ๋ฐ”์šด๋“œ ๋ฉ”์„œ๋“œ๋Š” ๊ทธ๊ฒƒ์ด ์–ด๋Š ์ธ์Šคํ„ด์Šค์—์„œ ์™”๋Š”์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. >>> s = Stock('GOOG', 100, 490.10) >>> s <Stock object at 0x590d0> >>> c = s.cost >>> c <bound method Stock.cost of <Stock object at 0x590d0>> >>> c() 49010.0 >>> ๋ฐ”์šด๋“œ ๋ฉ”์„œ๋“œ๋Š” ๋ถ€์ฃผ์˜ํ•˜๊ณ  ๋ช…๋ฐฑํ•˜์ง€ ์•Š์€ ์˜ค๋ฅ˜์˜ ์›์ธ์ด ๋˜๊ณค ํ•œ๋‹ค. ์˜ˆ: >>> s = Stock('GOOG', 100, 490.10) >>> print('Cost : % 0.2f' % s.cost) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: float argument required >>> ๋˜๋Š” ๋””๋ฒ„๊น…ํ•˜๊ธฐ ํž˜๋“  ์ž˜๋ชป๋œ ํ–‰๋™์„ ํ•œ๋‹ค. f = open(filename, 'w') ... f.close # ์•„์ฐจ, ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š์•˜๋‹ค. `f`๋Š” ์—ฌ์ „ํžˆ ์—ด๋ ค ์žˆ๋‹ค. ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ ๊ด„ํ˜ธ๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์„ ์žŠ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ์˜ˆ: s.cost() ๋˜๋Š” f.close(). ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์•ก์„ธ์Šค ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ์— ์•ก์„ธ์Šค, ์กฐ์ž‘, ๊ด€๋ฆฌํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. getattr(obj, 'name') # obj.name๊ณผ ๊ฐ™์Œ setattr(obj, 'name', value) # obj.name = value์™€ ๊ฐ™์Œ delattr(obj, 'name') # del obj.name๊ณผ ๊ฐ™์Œ hasattr(obj, 'name') # ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธ ์˜ˆ: if hasattr(obj, 'x'): x = getattr(obj, 'x'): else: x = None ์ฐธ๊ณ : getattr()์—๋„ ์œ ์šฉํ•œ ๊ธฐ๋ณธ๊ฐ’ arg๊ฐ€ ์žˆ๋‹ค. x = getattr(obj, 'x', None) ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 4.9: ๊ฐ์ฒด๋ฅผ ํ”„๋ฆฐํŒ… ํ•˜๊ธฐ ์œ„ํ•œ ๋” ๋‚˜์€ ์ถœ๋ ฅ stock.py์— ์ •์˜ํ•œ Stock ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•ด __repr__() ๋ฉ”์„œ๋“œ๊ฐ€ ๋” ์œ ์šฉํ•œ ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๊ฒŒ ํ•ด ๋ณด์ž. ์˜ˆ: >>> goog = Stock('GOOG', 100, 490.1) >>> goog Stock('GOOG', 100, 490.1) >>> ์ด๋Ÿฌํ•œ ๋ณ€๊ฒฝ์„ ํ•œ ํ›„์— ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์ฝ๊ณ  ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณผ ๋•Œ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ํ™•์ธํ•˜๋ผ. ์˜ˆ: >>> import report >>> portfolio = report.read_portfolio('Data/portfolio.csv') >>> portfolio ... see what the output is ... >>> ์—ฐ์Šต ๋ฌธ์ œ 4.10: getattr()์„ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ getattr()์€ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ฝ๋Š” ๋Œ€์ฒด ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด๋‹ค. ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•ด ๊ทน๋„๋กœ ์œ ์—ฐํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋ถ€ํ„ฐ ์‹œ๋„ํ•˜์ž. >>> import stock >>> s = stock.Stock('GOOG', 100, 490.1) >>> columns = ['name', 'shares'] >>> for colname in columns: print(colname, '=', getattr(s, colname)) name = GOOG shares = 100 >>> ์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ columns ๋ณ€์ˆ˜์— ๋‚˜์—ด๋œ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์ด๋ฆ„์— ์˜ํ•ด ์™„์ „ํžˆ ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ์„ ์ฃผ์˜ ๊นŠ๊ฒŒ ๊ด€์ฐฐํ•˜๋ผ. ์ด ์•„์ด๋””์–ด๋ฅผ tableformat.py ํŒŒ์ผ์˜ ์ผ๋ฐ˜ํ™”๋œ ํ•จ์ˆ˜ print_table()์— ์ ์šฉํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ž„์˜์˜ ๊ฐ์ฒด์˜ ๋ฆฌ์ŠคํŠธ์˜ ์‚ฌ์šฉ์ž ์ •์˜ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ํ…Œ์ด๋ธ”์„ ํ”„๋ฆฐํŠธํ•œ๋‹ค. ์ด์ „์˜ print_report() ํ•จ์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, print_table()์€ ์ถœ๋ ฅ ํฌ๋งท์„ ์ง€์ •ํ•˜๋Š” TableFormatter ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ›๋Š”๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•œ๋‹ค. >>> import report >>> portfolio = report.read_portfolio('Data/portfolio.csv') >>> from tableformat import create_formatter, print_table >>> formatter = create_formatter('txt') >>> print_table(portfolio, ['name','shares'], formatter) name shares ---------- ---------- AA 100 IBM 50 CAT 150 MSFT 200 GE 95 MSFT 50 IBM 100 >>> print_table(portfolio, ['name','shares','price'], formatter) name shares price ---------- ---------- ---------- AA 100 32.2 IBM 50 91.1 CAT 150 83.44 MSFT 200 51.23 GE 95 40.37 MSFT 50 65.1 IBM 100 70.44 >>> 4.4 ์˜ˆ์™ธ ์ •์˜ํ•˜๊ธฐ ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ๋Š” ํด๋ž˜์Šค์— ์˜ํ•ด ์ •์˜๋œ๋‹ค. class NetworkError(Exception): pass ์˜ˆ์™ธ๋Š” ํ•ญ์ƒ Exception์œผ๋กœ๋ถ€ํ„ฐ ์ƒ์†ํ•œ๋‹ค. ๋ณดํ†ต ๊ทธ๊ฒƒ๋“ค์€ ๋นˆ ํด๋ž˜์Šค๋‹ค. ๋ชธ์ฒด์— pass๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ์™ธ ๊ณ„์ธต์„ ์ง์ ‘ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. class AuthenticationError(NetworkError): pass class ProtocolError(NetworkError): pass ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 4.11: ์ปค์Šคํ…€ ์˜ˆ์™ธ ์ •์˜ํ•˜๊ธฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ž์ฒด์ ์ธ ์˜ˆ์™ธ๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ณตํ†ต์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์˜ค๋ฅ˜๋กœ ์ธํ•ด ๋ฐœ์ƒํ•˜๋Š” ํŒŒ์ด์ฌ ์˜ˆ์™ธ์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฐœ์ƒํ•ด ํŠน์ •ํ•œ ์‚ฌ์šฉ์ƒ์˜ ๋ฌธ์ œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์˜ˆ์™ธ๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์‰ฌ์›Œ์ง„๋‹ค. ์ง€๋‚œ ์—ฐ์Šต ๋ฌธ์ œ์˜ create_formatter() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด, ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜๋ชป๋œ ํฌ๋งท ์ด๋ฆ„์„ ์ œ๊ณตํ–ˆ์„ ๋•Œ ์ปค์Šคํ…€ FormatError ์˜ˆ์™ธ๋ฅผ ์ผ์œผํ‚ค๊ฒŒ ํ•ด ๋ณด์ž. ์˜ˆ: >>> from tableformat import create_formatter >>> formatter = create_formatter('xls') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "tableformat.py", line 71, in create_formatter raise FormatError('Unknown table format %s' % name) FormatError: Unknown table format xls >>> 5. ํŒŒ์ด์ฌ ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ์ž‘๋™ ์ด ์„น์…˜์€ ํŒŒ์ด์ฌ ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ์ž‘๋™์„ ๋‹ค๋ฃฌ๋‹ค. ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ๋‹ค๋ค„๋ณธ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋ผ๋ฉด ํŒŒ์ด์ฌ์˜ ํด๋ž˜์Šค ๊ฐœ๋…์ด ๋ถ€์กฑํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•˜๊ณค ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ ‘๊ทผ ์ œ์–ด(์˜ˆ: private, protected)๊ฐ€ ์—†๊ณ , self ์ธ์ž๋„ ์ด์ƒํ•ด ๋ณด์ด๊ณ , ๊ฐ์ฒด ๊ด€๋ จ ์ž‘์—…์€ "๋ฌด์—‡์ด๋“  ์ž์œ ๋กญ๊ฒŒ" ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋Š๊ปด์ง„๋‹ค. ๊ทธ๊ฒƒ์ด ์‚ฌ์‹ค์ผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ผ๋ฐ˜์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์—์„œ๋ถ€ํ„ฐ ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๋” ๋‚˜์€ ์บก์Šํ™”๊นŒ์ง€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์•Œ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ๋‚ด๋ถ€๋ฅผ ์ƒ์„ธํžˆ ์•Œ์ง€ ๋ชปํ•œ๋‹ค๊ณ  ํ•ด์„œ ์ƒ์‚ฐ์„ฑ์ด ๋–จ์–ด์ง€๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ, ํŒŒ์ด์ฌ ์ฝ”๋” ๋Œ€๋ถ€๋ถ„์€ ํด๋ž˜์Šค๊ฐ€ ์ž‘๋™ํ•˜๋Š” ๊ธฐ๋ณธ ์›๋ฆฌ๋ฅผ ์ดํ•ดํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์šฐ๋ฆฌ๋„ ํ•œ๋ฒˆ ์•Œ์•„๋ณด์ž. 5.1 ๋”•์…”๋„ˆ๋ฆฌ ํ†บ์•„๋ณด๊ธฐ(๊ฐ์ฒด ๊ตฌํ˜„) 5.2 ์บก์Šํ™” ๊ธฐ๋ฒ• 5.1 ๋”•์…”๋„ˆ๋ฆฌ ํ†บ์•„๋ณด๊ธฐ ํŒŒ์ด์ฌ ๊ฐ์ฒด ์‹œ์Šคํ…œ์€ ๋”•์…”๋„ˆ๋ฆฌ์™€ ๊ด€๋ จ๋œ ๊ตฌํ˜„์— ๋งŽ์€ ๋ถ€๋ถ„ ๊ธฐ๋ฐ˜์„ ๋‘”๋‹ค. ์ด ์„น์…˜์—์„œ ๊ทธ์— ๋Œ€ํ•ด ๋…ผ์˜ํ•œ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ์ด๋ฆ„ ๋ถ™์€ ๊ฐ’๋“ค์˜ ์ปฌ๋ ‰์…˜์ด๋ผ๋Š” ๊ฒƒ์„ ์ƒ๊ธฐํ•˜์ž. stock = { 'name' : 'GOOG', 'shares' : 100, 'price' : 490.1 } ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ฃผ๋กœ ๋‹จ์ˆœํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. ํ•˜์ง€๋งŒ, ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์ค‘์š”ํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋ฉฐ ํŒŒ์ด์ฌ์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ž๋ฃŒํ˜•์ด๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ์™€ ๋ชจ๋“ˆ ๋ชจ๋“ˆ์—์„œ, ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๋ชจ๋“  ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ๋ณด์œ ํ•œ๋‹ค. # foo.py x = 42 def bar(): ... def spam(): ... foo.__dict__๋‚˜ globals()๋ฅผ ์กฐ์‚ฌํ•ด ๋ณด๋ฉด ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ๋ณด์ผ ๊ฒƒ์ด๋‹ค. { 'x' : 42, 'bar' : <function bar>, 'spam' : <function spam> } ๋”•์…”๋„ˆ๋ฆฌ์™€ ๊ฐ์ฒด ์‚ฌ์šฉ์ž ์ •์˜ ๊ฐ์ฒด๋„ ์ธ์Šคํ„ด์Šค ๋ฐ์ดํ„ฐ์™€ ํด๋ž˜์Šค๋ฅผ ์œ„ํ•ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์‚ฌ์‹ค, ๊ฐ์ฒด ์‹œ์Šคํ…œ ๋Œ€๋ถ€๋ถ„์ด ๋”•์…”๋„ˆ๋ฆฌ ์œ„์— ์„ธ์›Œ์ง„ ์ถ”๊ฐ€์ ์ธ ๊ณ„์ธต์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ฐ์ดํ„ฐ __dict__๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋‹ค. >>> s = Stock('GOOG', 100, 490.1) >>> s.__dict__ {'name' : 'GOOG', 'shares' : 100, 'price': 490.1 } self์— ํ• ๋‹นํ•  ๋•Œ ์ด ๋”•์…”๋„ˆ๋ฆฌ์™€ ์ธ์Šคํ„ด์Šค๋ฅผ ์ฑ„์šด๋‹ค. class Stock: def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price ์ธ์Šคํ„ด์Šค ๋ฐ์ดํ„ฐ self.__dict__๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ธ๋‹ค. { 'name': 'GOOG', 'shares': 100, 'price': 490.1 } ์ธ์Šคํ„ด์Šค๋งˆ๋‹ค ์ž์ฒด์ ์ธ ํ”„๋ผ์ด๋น— ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ์ƒ๊ธด๋‹ค. s = Stock('GOOG', 100, 490.1) # {'name' : 'GOOG','shares' : 100, 'price': 490.1 } t = Stock('AAPL', 50, 123.45) # {'name' : 'AAPL','shares' : 50, 'price': 123.45 } ์–ด๋–ค ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ 100๊ฐœ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ํ•˜๋ฉด, ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š” ๋”•์…”๋„ˆ๋ฆฌ 100๊ฐœ๊ฐ€ ์žˆ๋Š” ์…ˆ์ด๋‹ค. ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด์œ ํ•˜๋Š” ๋…๋ฆฝ์ ์ธ ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ์žˆ๋‹ค. class Stock: def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price def cost(self): return self.shares * self.price def sell(self, nshares): self.shares -= nshares ์ด ๋”•์…”๋„ˆ๋ฆฌ๋Š” Stock.__dict__์— ์žˆ๋‹ค. { 'cost': <function>, 'sell': <function>, '__init__': <function> } ์ธ์Šคํ„ด์Šค์™€ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์™€ ํด๋ž˜์Šค๋Š” ์„œ๋กœ ์—ฐ๊ฒฐ๋ผ ์žˆ๋‹ค. __class__ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋Š” ํด๋ž˜์Šค๋ฅผ ๋‹ค์‹œ ์ฐธ์กฐํ•œ๋‹ค. >>> s = Stock('GOOG', 100, 490.1) >>> s.__dict__ { 'name': 'GOOG', 'shares': 100, 'price': 490.1 } >>> s.__class__ <class '__main__.Stock'> >>> ์ธ์Šคํ„ด์Šค ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๊ฐ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด ๊ณ ์œ ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๋Š”๋‹ค. ํด๋ž˜์Šค ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ „์ฒด ์ธ์Šคํ„ด์Šค์—์„œ<NAME>๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๋Š”๋‹ค. ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์•ก์„ธ์Šค ๊ฐ์ฒด๋กœ ์ž‘์—…ํ•  ๋•Œ๋Š”. ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ์™€ ๋ฉ”์„œ๋“œ์— ์•ก์„ธ์Šคํ•œ๋‹ค. x = obj.name # ์–ป๊ธฐ obj.name = value # ์„ค์ • del obj.name # ์‚ญ์ œ ์ด ์—ฐ์‚ฐ์ž๋Š” ๋”•์…”๋„ˆ๋ฆฌ์— ์ง์ ‘ ์—ฐ๊ฒฐ๋œ๋‹ค. ์ธ์Šคํ„ด์Šค ์ˆ˜์ • ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ์—ฐ์‚ฐ์€ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค. >>> s = Stock('GOOG', 100, 490.1) >>> s.__dict__ { 'name':'GOOG', 'shares': 100, 'price': 490.1 } >>> s.shares = 50 # ์„ค์ • >>> s.date = '6/7/2007' # ์„ค์ • >>> s.__dict__ { 'name': 'GOOG', 'shares': 50, 'price': 490.1, 'date': '6/7/2007' } >>> del s.shares # ์‚ญ์ œ >>> s.__dict__ { 'name': 'GOOG', 'price': 490.1, 'date': '6/7/2007' } >>> ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ฝ๊ธฐ ์ธ์Šคํ„ด์Šค์—์„œ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ฝ๋Š”๋‹ค๊ณ  ํ•˜์ž. x = obj.name ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋Š” ๋‘ ๊ณณ์— ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋กœ์ปฌ ์ธ์Šคํ„ด์Šค ๋”•์…”๋„ˆ๋ฆฌ. ํด๋ž˜์Šค ๋”•์…”๋„ˆ๋ฆฌ. ๋‘ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ชจ๋‘ ํ™•์ธํ•œ๋‹ค. ๋จผ์ € ๋กœ์ปฌ __dict__๋ฅผ ํ™•์ธํ•œ๋‹ค. ๊ฑฐ๊ธฐ์„œ ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด __class__๋ฅผ ํ†ตํ•ด ํด๋ž˜์Šค์˜ __dict__์—์„œ ์ฐพ๋Š”๋‹ค. >>> s = Stock(...) >>> s.name 'GOOG' >>> s.cost() 49010.0 >>> ์ด๋Ÿฌํ•œ ์กฐํšŒ ๋ฐฉ์‹์œผ๋กœ ์ธํ•ด class์˜ ๋ฉค๋ฒ„๊ฐ€ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์—์„œ ๊ณต์œ ๋œ๋‹ค. ์ƒ์†์ด ์ด๋ค„์ง€๋Š” ์›๋ฆฌ ํด๋ž˜์Šค๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†ํ•  ์ˆ˜ ์žˆ๋‹ค. class A(B, C): ... ๊ธฐ๋ณธ ํด๋ž˜์Šค๋Š” ๊ฐ ํด๋ž˜์Šค์˜ ํŠœํ”Œ์— ์ €์žฅ๋œ๋‹ค. >>> A.__bases__ (<class '__main__.B'>, <class '__main__.C'>) >>> ์ด๊ฒƒ์€ ๋ถ€๋ชจ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๋งํฌ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ƒ์†๊ณผ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์ฝ๊ธฐ ๋…ผ๋ฆฌ์ ์œผ๋กœ, ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ฐพ๋Š” ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋จผ์ €, ๋กœ์ปฌ __dict__๋ฅผ ํ™•์ธํ•œ๋‹ค. ์ฐพ์ง€ ๋ชปํ•˜๋ฉด, ํด๋ž˜์Šค์˜ __dict__๋ฅผ ํ™•์ธํ•œ๋‹ค. ํด๋ž˜์Šค์—์„œ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด, __bases__๋ฅผ ํ†ตํ•ด ๊ธฐ๋ณธ ํด๋ž˜์Šค๋“ค์—์„œ ํ™•์ธํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์—๋Š” ๋ฏธ๋ฌ˜ํ•œ ๊ตฌ์„์ด ์žˆ๋‹ค. ๋‹จ์ผ ์ƒ์†๊ณผ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์ฝ๊ธฐ ์—ฌ๋Ÿฌ ๊ณ„์ธต์— ๊ฑธ์ณ ์ƒ์†์ด ์ด๋ค„์ง€๋Š” ๊ฒฝ์šฐ, ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์ƒ์† ํŠธ๋ฆฌ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ˆœํšŒํ•œ๋‹ค. class A: pass class B(A): pass class C(A): pass class D(B): pass class E(D): pass ๋‹จ์ผ ์ƒ์†์—์„œ๋Š” ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ํ•˜๋‚˜๋ฐ–์— ์—†๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์—์„œ ๋ฉˆ์ถ˜๋‹ค. ๋ฉ”์„œ๋“œ ์ฐพ๊ธฐ ์ˆœ์„œ(MRO) ํŒŒ์ด์ฌ์€ ์ƒ์† ์‚ฌ์Šฌ์„ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด ํด๋ž˜์Šค์˜ MRO ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ์— ์ €์žฅํ•œ๋‹ค. ๊ทธ๊ฒƒ์„ ์ง์ ‘ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. >>> E.__mro__ (<class '__main__.E'>, <class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <type 'object'>) >>> ์ด ์‚ฌ์Šฌ์„ ๋ฉ”์„œ๋“œ ์ฐพ๊ธฐ ์ˆœ์„œ(Method Resolution Order)๋ผ ํ•œ๋‹ค. ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ํŒŒ์ด์ฌ์€ MRO๋ฅผ ์ˆœํšŒํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์„ ํƒ๋œ๋‹ค. ๋‹ค์ค‘ ์ƒ์†์˜ MRO ๋‹ค์ค‘ ์ƒ์†์—์„œ๋Š” ์ตœ์ƒ์œ„์— ์ด๋ฅด๋Š” ๋‹จ์ผ ๊ฒฝ๋กœ๊ฐ€ ์—†๋‹ค. ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์ž. class A: pass class B: pass class C(A, B): pass class D(B): pass class E(C, D): pass ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ์— ์•ก์„ธ์Šคํ•  ๋•Œ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”๊ฐ€? e = E() e.attr ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฒ€์ƒ‰ ๊ณผ์ •์ด ์ˆ˜ํ–‰๋˜๋Š”๋ฐ, ๊ทธ ์ˆœ์„œ๋Š” ์–ด๋–ป๊ฒŒ ์ด๋ค„์ง€๋Š”๊ฐ€? ๊ทธ๊ฒƒ์ด ๋ฌธ์ œ๋‹ค. ํŒŒ์ด์„ ์—์„œ๋Š” ํด๋ž˜์Šค ์ˆœ์„œ๋ฅผ ์ •ํ•  ๋•Œ ํ˜‘๋™ ๋‹ค์ค‘ ์ƒ์†(cooperative multiple inheritance)์ด๋ผ๋Š” ๊ทœ์น™์„ ๋”ฐ๋ฅธ๋‹ค. ํ•ญ์ƒ ์ž์‹์„ ๋ถ€๋ชจ๋ณด๋‹ค ๋จผ์ € ํ™•์ธํ•œ๋‹ค. ๋ถ€๋ชจ๊ฐ€ ๋‘˜ ์ด์ƒ์ธ ๊ฒฝ์šฐ ํ•ญ์ƒ ๋ฆฌ์ŠคํŠธ ๋œ ์ˆœ์„œ๋Œ€๋กœ ํ™•์ธํ•œ๋‹ค. ๊ณ„์ธต์˜ ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ ์ด ๊ทœ์น™์— ๋”ฐ๋ผ ์ •๋ ฌํ•จ์œผ๋กœ์จ MRO๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. >>> E.__mro__ ( <class 'E'>, <class 'C'>, <class 'A'>, <class 'D'>, <class 'B'>, <class 'object'>) >>> ์ด๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ๋“ฌ์€ โ€˜C3 ์„ ํ˜•ํ™”(Linearization)โ€™๋ผ๋Š” ๊ฒƒ์œผ๋กœ, ์„ธ๋ถ€์ ์ธ ๋‚ด์šฉ์€ ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค. ์ง‘์— ๋ถˆ์ด ๋‚ฌ์„ ๋•Œ ์ž์‹์„ ๋จผ์ € ๋Œ€ํ”ผ์‹œํ‚จ ๋‹ค์Œ ๋ถ€๋ชจ๊ฐ€ ๋Œ€ํ”ผํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ํด๋ž˜์Šค ๊ณ„์ธต์ด ๊ฐ™์€ ์ˆœ์„œ ๊ทœ์น™์„ ๋”ฐ๋ฅธ๋‹ค๋Š” ์ ๋งŒ ๊ธฐ์–ตํ•˜๋ฉด ๋œ๋‹ค. ์ด์ƒํ•œ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ(๋‹ค์ค‘ ์ƒ์†์ด ๊ด€๋ จ๋จ) ๋‹ค์Œ ๋‘ ์˜ˆ์ œ๋ฅผ ๋ณด์ž. class Dog: def noise(self): return 'Bark' def chase(self): return 'Chasing!' class LoudDog(Dog): def noise(self): # LoudBike์™€์˜ ์ฝ”๋“œ ๊ณตํ†ต์„ฑ(์•„๋ž˜) return super().noise().upper() ์œ„ ์˜ˆ์ œ์˜ ๊ฐ์ฒด๋“ค์€ ์•„๋ž˜ ์˜ˆ์ œ์˜ ๊ฐ์ฒด๋“ค๊ณผ ์•„๋ฌด ๊ด€๋ จ์ด ์—†๋‹ค. class Bike: def noise(self): return 'On Your Left' def pedal(self): return 'Pedaling!' class LoudBike(Bike): def noise(self): # LoudDog ๊ณผ์˜ ์ฝ”๋“œ ๊ณตํ†ต์„ฑ(์œ„) return super().noise().upper() ๊ทธ๋Ÿฐ๋ฐ LoudDog.noise()์™€ LoudBike.noise() ๊ตฌํ˜„์—๋Š” ๊ณตํ†ต์ ์ธ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค. ์‚ฌ์‹ค ์ฝ”๋“œ๊ฐ€ ๋˜‘๊ฐ™๋‹ค. ์†Œํ”„ํŠธ์›จ์–ด ๊ธฐ์ˆ ์ž๋ผ๋ฉด ์ด๋Ÿฐ ์ฝ”๋“œ์— ๋Œ๋ฆฌ๊ธฐ ๋งˆ๋ จ์ด๋‹ค. ๋ฏน์Šค์ธ ํŒจํ„ด ๋ฏน์Šค์ธ(Mixin) ํŒจํ„ด์€ ์ฝ”๋“œ ์ผ๋ถ€๋ฅผ ๊ฐ–๋Š” ํด๋ž˜์Šค๋‹ค. class Loud: def noise(self): return super().noise().upper() ์ด ํด๋ž˜์Šค๋Š” ๋™๋–จ์–ด์ ธ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ์ด๊ฒƒ์€ ์ƒ์†์„ ํ†ตํ•ด ๋‹ค๋ฅธ ํด๋ž˜์Šค๋“ค์„ ๋’ค์„ž๋Š”๋‹ค. class LoudDog(Loud, Dog): pass class LoudBike(Loud, Bike): pass noise ๋ฉ”์„œ๋“œ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ๊ตฌํ˜„ํ•ด, ์„œ๋กœ ๊ด€๊ณ„์—†๋Š” ๋‘ ํด๋ž˜์Šค์— ์žฌ์‚ฌ์šฉํ–ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ ๋‹ค์ค‘ ์ƒ์†์„ ์‚ฌ์šฉํ•˜๋Š” ์ฃผ ์šฉ๋„๊ฐ€ ์ด๋Ÿฐ ๊ฒƒ์ด๋‹ค. super()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ๋•Œ๋Š” ํ•ญ์ƒ super()๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. class Loud: def noise(self): return super().noise().upper() super()๋Š” MRO์˜ ๋‹ค์Œ ํด๋ž˜์Šค์— ์œ„์ž„ํ•œ๋‹ค. ๊นŒ๋‹ค๋กœ์šด ๋ถ€๋ถ„์€ ๋‹น์‹ ์ด ๊ทธ๊ฒƒ์„ ์•Œ ์ˆ˜ ์—†๋‹ค๋Š” ์ ์ด๋‹ค. ํŠนํžˆ ๋‹ค์ค‘ ์ƒ์†์ด ์ผ์–ด๋‚  ๋•Œ๋Š” ๊ทธ๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค. ์ฃผ์˜ ์‚ฌํ•ญ ๋‹ค์ค‘ ์ƒ์†์€ ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ๋‹ค. ํž˜์—๋Š” ์ฑ…์ž„์ด ๋”ฐ๋ฅธ๋‹ค๋Š” ์ ์„ ๋ช…์‹ฌํ•˜๋ผ. ํ”„๋ ˆ์ž„์›Œํฌ/๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ปดํฌ๋„ŒํŠธ์˜ ๊ตฌ์„ฑ๊ณผ ๊ด€๋ จ๋œ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ์„ ์œ„ํ•ด ๊ทธ๊ฒƒ์„ ์ข…์ข… ์‚ฌ์šฉํ•œ๋‹ค. ์ด์ œ, ๋ฐฉ๊ธˆ ๋ณธ ๊ฒƒ์„ ์žŠ์–ด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ ์„น์…˜ 4์—์„œ, ๋ณด์œ  ์ฃผ์‹์„ ๋‚˜ํƒ€๋‚ด๋Š” Stock ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ–ˆ๋‹ค. ์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ๋Š” ๊ทธ ํด๋ž˜์Šค๋ฅผ ์žฌ์‚ฌ์šฉํ•œ๋‹ค. ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ช‡ ๊ฐœ ๋งŒ๋“ค์ž. >>> ================================ RESTART ================================ >>> from stock import Stock >>> goog = Stock('GOOG',100,490.10) >>> ibm = Stock('IBM',50, 91.23) >>> ์—ฐ์Šต ๋ฌธ์ œ 5.1: ์ธ์Šคํ„ด์Šค ํ‘œํ˜„ ์ƒํ˜ธ์ž‘์šฉ์ ์ธ ์…ธ์—์„œ, ๋‹น์‹ ์ด ์ƒ์„ฑ์„ฑ ๋‘ ์ธ์Šคํ„ด์Šค์˜ ๋‚ด๋ถ€ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์กฐ์‚ฌํ•ด ๋ณด๋ผ. >>> goog.__dict__ ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> ibm.__dict__ ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> ์—ฐ์Šต ๋ฌธ์ œ 5.2: ์ธ์Šคํ„ด์Šค ๋ฐ์ดํ„ฐ ์ˆ˜์ • ์œ„ ์ธ์Šคํ„ด์Šค๋“ค ์ค‘ ํ•˜๋‚˜์— ์ƒˆ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์„ค์ •ํ•ด ๋ณด๋ผ. >>> goog.date = '6/11/2007' >>> goog.__dict__ ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> ibm.__dict__ ... ์ถœ๋ ฅ์„ ๋ณด๋ผ ... >>> ์œ„์˜ ์ถœ๋ ฅ์—์„œ goog ์ธ์Šคํ„ด์Šค๋Š” date ์†์„ฑ์„ ๊ฐ–๋Š” ๋ฐ˜๋ฉด ibm ์ธ์Šคํ„ด์Šค๋Š” ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํŒŒ์ด์ฌ์€ ์†์„ฑ์— ์•„๋ฌด ์ œํ•œ์„ ๋‘์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์— ์œ ์˜ํ•˜์ž. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ธ์Šคํ„ด์Šค์˜ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋Š” __init__() ๋ฉ”์„œ๋“œ์—์„œ ์…‹์—… ํ•œ ๊ฒƒ์— ๊ตญํ•œ๋˜์ง€ ์•Š๋Š”๋‹ค. ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋Œ€์‹ , __dict__ ๊ฐ์ฒด์— ์ƒˆ ๊ฐ’์„ ์ง์ ‘ ๋„ฃ์–ด๋ผ. >>> goog.__dict__['time'] = '9:45am' >>> goog.time '9:45am' >>> ์—ฌ๊ธฐ์„œ ์ธ์Šคํ„ด์Šค๋Š” ๋”•์…”๋„ˆ๋ฆฌ ์œ„์˜ ๊ณ„์ธต์— ๋ถˆ๊ณผํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ฐธ๊ณ : ๋ณดํ†ต์€ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ง์ ‘ ์กฐ์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•ญ์ƒ (.) ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ž. ์—ฐ์Šต ๋ฌธ์ œ 5.3: ํด๋ž˜์Šค์˜ ์—ญํ•  ํด๋ž˜์Šค ์ •์˜๋ฅผ ๋งŒ๋“œ๋Š” ์ •์˜๋Š” ํ•ด๋‹น ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์— ์˜ํ•ด ๊ณต์œ ๋œ๋‹ค. ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ด€๋ จ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๋งํฌ๋ฅผ ๊ฐ–๋Š”๋‹ค. >>> goog.__class__ ... ์ถœ๋ ฅ์„ ์‚ดํŽด๋ณด๋ผ ... >>> ibm.__class__ ... ์ถœ๋ ฅ์„ ์‚ดํŽด๋ณด๋ผ ... >>> ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ๋ณด์ž. >>> goog.cost() 49010.0 >>> ibm.cost() 4561.5 >>> 'cost'๋ผ๋Š” ์ด๋ฆ„์€ goog.__dict__๋‚˜ ibm.__dict__์— ์ •์˜๋ผ ์žˆ์ง€ ์•Š๋‹ค. ํด๋ž˜์Šค ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ์ œ๊ณตํ•œ ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด ๋ณด๋ผ. >>> Stock.__dict__['cost'] ... ์ถœ๋ ฅ์„ ์‚ดํŽด๋ณด๋ผ ... >>> ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ†ตํ•ด cost() ๋ฉ”์„œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•ด ๋ณด์ž. >>> Stock.__dict__['cost'](goog) 49010.0 >>> Stock.__dict__['cost'](ibm) 4561.5 >>> ํด๋ž˜์Šค ์ •์˜์— ์ •์˜๋œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒƒ์ด๋‹ค. self ์ธ์ž๊ฐ€ ์ธ์Šคํ„ด์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ์–ป๋Š”์ง€๋„ ์œ ์˜ํ•˜์ž. Stock ํด๋ž˜์Šค์— ์ƒˆ๋กœ์šด ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด์ž. >>> Stock.foo = 42 >>> ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•œ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๊ฐ€ ์ด์ œ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์— ๋‚˜ํƒ€๋‚œ๋‹ค. >>> goog.foo 42 >>> ibm.foo 42 >>> ๊ทธ๋ ‡์ง€๋งŒ, ์ด๋Š” ์ธ์Šคํ„ด์Šค ๋”•์…”๋„ˆ๋ฆฌ์— ์†ํ•œ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. >>> goog.__dict__ ... ์ถœ๋ ฅ์„ ์‚ดํŽด๋ณด๋ฉด 'foo' ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๊ฐ€ ์—†๋‹ค ... >>> ์ธ์Šคํ„ด์Šค์—์„œ foo ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š”, ํŒŒ์ด์ฌ์—์„œ๋Š” ์ธ์Šคํ„ด์Šค ์ž์ฒด์—์„œ ๋ญ”๊ฐ€๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด ํ•ญ์ƒ ํด๋ž˜์Šค ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ™•์ธํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฐธ๊ณ : ์—ฐ์Šต ๋ฌธ์ œ์˜ ์ด ๋ถ€๋ถ„์€ ํด๋ž˜์Šค ๋ณ€์ˆ˜๋ผ๊ณ  ์•Œ๋ ค์ง„ ๊ฒƒ์„ ๋ฌ˜์‚ฌํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. class Foo(object): a = 13 # ํด๋ž˜์Šค ๋ณ€์ˆ˜ def __init__(self, b): self.b = b # ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์ด ํด๋ž˜์Šค์—์„œ ๋ณ€์ˆ˜ a๋Š” ํด๋ž˜์Šค ์ž์ฒด์˜ ๋ชธ์ฒด์— ํ• ๋‹น๋œ 'ํด๋ž˜์Šค ๋ณ€์ˆ˜'๋‹ค. ์ƒ์„ฑ๋˜๋Š” ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๊ฐ€ ์ด๊ฒƒ์„ ๊ณต์œ ํ•œ๋‹ค. ์˜ˆ: >>> f = Foo(10) >>> g = Foo(20) >>> f.a # ํด๋ž˜์Šค ๋ณ€์ˆ˜๋ฅผ ์กฐ์‚ฌ(๋‘ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด ๊ฐ™์Œ) 13 >>> g.a 13 >>> f.b # ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์กฐ์‚ฌ(๋‹ค๋ฆ„) 10 >>> g.b 20 >>> Foo.a = 42 # ํด๋ž˜์Šค ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ >>> f.a 42 >>> g.a 42 >>> ์—ฐ์Šต ๋ฌธ์ œ 5.4: ๋ฐ”์šด๋“œ ๋ฉ”์„œ๋“œ ํŒŒ์ด์ฌ์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ ์‹ค์ œ๋กœ๋Š” ๋‘ ๋‹จ๊ณ„๋กœ ์ด๋ค„์ง€๋ฉฐ ๋ฐ”์šด๋“œ ๋ฉ”์„œ๋“œ๋ผ๋Š” ๊ฒƒ์ด ๊ด€์—ฌํ•œ๋‹ค. ์˜ˆ: >>> s = goog.sell >>> s <bound method Stock.sell of Stock('GOOG', 100, 490.1)> >>> s(25) >>> goog.shares 75 >>> ๋ฐ”์šด๋“œ ๋ฉ”์„œ๋“œ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๊ฒƒ์„ ๋ชจ๋‘ ํฌํ•จํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํ•จ์ˆ˜์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ<NAME>๋‹ค. >>> s.__func__ <function sell at 0x10049af50> >>> Stock ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ์ด๊ฒƒ๊ณผ ๊ฐ™์€ ๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. >>> Stock.__dict__['sell'] <function sell at 0x10049af50> >>> ๋ฐ”์šด๋“œ ๋ฉ”์„œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค, ์ฆ‰ self ์ธ์ž๋„ ๊ธฐ๋กํ•œ๋‹ค. >>> s.__self__ Stock('GOOG',75,490.1) >>> ()๋ฅผ ์‚ฌ์šฉํ•ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋ชจ๋“  ๊ฒƒ์ด ํ•ฉ์ณ์ง„๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, s(25)๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ ์‹ค์ œ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ์„ ํ•œ๋‹ค. >>> s.__func__(s.__self__, 25) # s(25)์™€ ๊ฐ™์Œ >>> goog.shares 50 >>> ์—ฐ์Šต ๋ฌธ์ œ 5.5: ์ƒ์† Stock์œผ๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›๋Š” ์ƒˆ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. >>> class NewStock(Stock): def yow(self): print('Yow!') >>> n = NewStock('ACME', 50, 123.45) >>> n.cost() 6172.50 >>> n.yow() Yow! >>> ์ƒ์†์€ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์œ„ํ•œ ๊ฒ€์ƒ‰ ํ”„๋กœ์„ธ์Šค๋ฅผ ํ™•์žฅํ•จ์œผ๋กœ์จ ๊ตฌํ˜„๋œ๋‹ค. __bases__ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋Š” ์ง๊ณ„ ๋ถ€๋ชจ์˜ ํŠœํ”Œ์„ ๊ฐ–๋Š”๋‹ค. >>> NewStock.__bases__ (<class 'stock.Stock'>,) >>> __mro__ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋Š” ์ „์ฒด ๋ถ€๋ชจ์˜ ํŠœํ”Œ์„ ๊ฐ€์ง€๋ฉฐ, ๊ทธ ์ˆœ์„œ์— ๋”ฐ๋ผ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค. >>> NewStock.__mro__ (<class '__main__.NewStock'>, <class 'stock.Stock'>, <class 'object'>) >>> ์œ„์˜ n ์ธ์Šคํ„ด์Šค์˜ cost() ๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. >>> for cls in n.__class__.__mro__: if 'cost' in cls.__dict__: break >>> cls <class '__main__.Stock'> >>> cls.__dict__['cost'] <function cost at 0x101aed598> >>> 5.2 ํด๋ž˜์Šค์™€ ์บก์Šํ™” ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” ๋‚ด๋ถ€์ ์ธ ์„ธ๋ถ€์‚ฌํ•ญ์„ ์บก์Šํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ํ”„๋ผ์ด๋น— ๋ณ€์ˆ˜์™€ ํ”„๋กœํผํ‹ฐ(property)๋ฅผ ํฌํ•จํ•ด, ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๋ช‡ ๊ฐ€์ง€ ๊ด€๋ก€๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. ํผ๋ธ”๋ฆญ vs ํ”„๋ผ์ด๋น— ํด๋ž˜์Šค์˜ ์ฃผ์—ญํ•  ์€ ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ์™€ ๋‚ด๋ถ€ ๊ตฌํ˜„ ์„ธ๋ถ€์‚ฌํ•ญ์„ ์บก์Šํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ์™ธ๋ถ€ ์„ธ๊ณ„๊ฐ€ ๊ฐ์ฒด๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ํผ๋ธ”๋ฆญ(public) ์ธํ„ฐํŽ˜์ด์Šค๋„ ํด๋ž˜์Šค์— ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค. ๊ตฌํ˜„ ์„ธ๋ถ€์‚ฌํ•ญ๊ณผ ํผ๋ธ”๋ฆญ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ๋ฌธ์ œ์  ํŒŒ์ด์ฌ์—์„œ๋Š” ํด๋ž˜์Šค์™€ ๊ฐ์ฒด์˜ ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒƒ์ด ์˜คํ”ˆ๋˜์–ด ์žˆ๋‹ค. ๊ฐ์ฒด์˜ ๋‚ด๋ถ€๋ฅผ ์‰ฝ๊ฒŒ ์กฐ์‚ฌํ•  ์ˆ˜ ์žˆ๊ณ , ์›ํ•˜๋Š” ๋Œ€๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. ์•ก์„ธ์Šค ์ œ์–ด์— ๋Œ€ํ•œ ๊ฐ•๋ ฅํ•œ ๊ฐœ๋…์€ ์—†๋‹ค(์˜ˆ: ํ”„๋ผ์ด๋น— ํด๋ž˜์Šค ๋ฉค๋ฒ„). ์ด๋Š” ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ๊ฒฉ๋ฆฌํ•˜๊ณ ์ž ํ•  ๋•Œ ์ด์Šˆ๊ฐ€ ๋œ๋‹ค. ํŒŒ์ด์ฌ์˜ ์บก์Šํ™”(Encapsulation) ํŒŒ์ด์ฌ์€ ์˜๋„๋œ ์‚ฌ์šฉ๋ฒ•์„ ์ง€์‹œํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ด€๋ก€๋ฅผ ๋”ฐ๋ฅธ๋‹ค. ์ด๋Ÿฌํ•œ ๊ด€๋ก€๋Š” ๋ช…๋ช…(naming)์— ๊ธฐ๋ฐ˜์„ ๋‘”๋‹ค. ์–ธ์–ด์—์„œ ๊ทœ์น™์„ ๊ฐ•์š”ํ•˜๊ธฐ๋ณด๋‹ค๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ž๋ฐœ์ ์œผ๋กœ ์ค€์ˆ˜ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ํ”„๋ผ์ด๋น— ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์ด๋ฆ„์ด _๋กœ ์‹œ์ž‘ํ•˜๋Š” ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋Š” ํ”„๋ผ์ด๋น—(private)์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค. class Person(object): def __init__(self, name): self._name = 0 ์•ž์—์„œ ์–ธ๊ธ‰ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด, ์ด๊ฒƒ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์Šคํƒ€์ผ์ผ ๋ฟ์ด๋‹ค. ์—ฌ์ „ํžˆ ๊ทธ๊ฒƒ์— ์•ก์„ธ์Šคํ•˜๊ณ  ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. >>> p = Person('Guido') >>> p._name 'Guido' >>> p._name = 'Dave' >>> _๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์€, ๊ทธ๊ฒƒ์ด ๋ณ€์ˆ˜๋“  ํ•จ์ˆ˜๋“  ๋ชจ๋“ˆ๋ช…์ด ๋“ , ๋ชจ๋‘ ๋‚ด๋ถ€ ๊ตฌํ˜„์œผ๋กœ ๊ฐ„์ฃผํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ๊ทœ์น™์ด๋‹ค. ๊ทธ๋Ÿฐ ์ด๋ฆ„์„ ์ง์ ‘ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๋ญ”๊ฐ€ ์ž˜๋ชปํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ๊ณ ์ˆ˜์ค€ ๊ธฐ๋Šฅ์„ ์ฐพ์•„๋ผ. ๋‹จ์ˆœ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๋‹ค์Œ ํด๋ž˜์Šค๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. class Stock: def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price ๋†€๋ž๊ฒŒ๋„, ์–ด๋–ค ๊ฐ’์—๋“ ์ง€ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. >>> s = Stock('IBM', 50, 91.1) >>> s.shares = 100 >>> s.shares = "hundred" >>> s.shares = [1, 0, 0] >>> ์ด๊ฒƒ์„ ๋ณด๊ณ  ๋ญ”๊ฐ€ ์ถ”๊ฐ€์ ์ธ ๊ฒ€์‚ฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์„์ง€ ๋ชจ๋ฅธ๋‹ค. s.shares = '50' # ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ TypeError๋ฅผ ์ผ์œผํ‚ค๊ณ  ์‹ถ๋‹ค ์ด๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ํ•  ์ˆ˜ ์žˆ์„๊นŒ? ๊ด€๋ฆฌ(Managed) ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์ ‘๊ทผ์ž(accessor) ๋ฉ”์„œ๋“œ๋ฅผ ๋„์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ธด ํ•˜๋‹ค. class Stock: def __init__(self, name, shares, price): self.name = name self.set_shares(shares) self.price = price # "get" ์—ฐ์‚ฐ์„ ๊ณ„์ธตํ™”ํ•˜๋Š” ๊ธฐ๋Šฅ def get_shares(self): return self._shares # "set" ์—ฐ์‚ฐ์„ ๊ณ„์ธตํ™”ํ•˜๋Š” ๊ธฐ๋Šฅ def set_shares(self, value): if not isinstance(value, int): raise TypeError('Expected an int') self._shares = value ์ด๊ฒƒ ๋•Œ๋ฌธ์— ๊ธฐ์กด ์ฝ”๋“œ๊ฐ€ ๊นจ์ง€๋Š” ๊ฒƒ์ด ๋„ˆ๋ฌด ๋‚˜์˜๋‹ค. s.shares = 50์ด s.set_shares(50)์ด ๋œ๋‹ค. ํ”„๋กœํผํ‹ฐ ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. class Stock: def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price @property def shares(self): return self._shares @shares.setter def shares(self, value): if not isinstance(value, int): raise TypeError('Expected int') self._shares = value ์ด์ œ ํ‰๋ฒ”ํ•œ ํ”„๋กœํผํ‹ฐ ์•ก์„ธ์Šค๋Š” @property์™€ @shares.setter ํ•˜์—์„œ getter์™€ setter ๋ฉ”์„œ๋“œ๋ฅผ ํŠธ๋ฆฌ๊ฑฐ ํ•œ๋‹ค. >>> s = Stock('IBM', 50, 91.1) >>> s.shares # @property๋ฅผ ํŠธ๋ฆฌ๊ฑฐ 50 >>> s.shares = 75 # @shares.setter๋ฅผ ํŠธ๋ฆฌ๊ฑฐ >>> ์ด ํŒจํ„ด์„ ์ ์šฉํ•˜๋ฉด ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ€ ํ•„์š”๊ฐ€ ์—†๋‹ค. ์ƒˆ๋กœ์šด setter๋„ ํด๋ž˜์Šค ๋‚ด์— ํ• ๋‹น๋˜๋ฉด ๋‚ด๋ถ€ __init__() ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•ด ํ˜ธ์ถœ๋œ๋‹ค. class Stock: def __init__(self, name, shares, price): ... # ์ด ํ• ๋‹น์€ ์•„๋ž˜ setter๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค self.shares = shares ... ... @shares.setter def shares(self, value): if not isinstance(value, int): raise TypeError('Expected int') self._shares = value ํ”„๋กœํผํ‹ฐ์™€ ํ”„๋ผ์ด๋น— ์ด๋ฆ„์ด ํ˜ผ๋™๋˜๊ณค ํ•œ๋‹ค. ํ”„๋กœํผํ‹ฐ๋Š” ํ”„๋ผ์ด๋น— ์ด๋ฆ„์„ _shares ๊ฐ™์ด ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ํด๋ž˜์Šค์˜ ๋‚˜๋จธ์ง€(ํ”„๋กœํผํ‹ฐ๊ฐ€ ์•„๋‹Œ ๊ฒƒ)๋Š” shares์™€ ๊ฐ™์€ ์ด๋ฆ„์„ ๊ณ„์† ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ”„๋กœํผํ‹ฐ๋Š” ๋˜ํ•œ ๊ณ„์‚ฐ๋œ ๋ฐ์ดํ„ฐ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ์— ์œ ์šฉํ•˜๋‹ค. class Stock: def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price @property def cost(self): return self.shares * self.price ... ์ด๊ฒƒ์€ ์ถ”๊ฐ€์ ์ธ ๊ด„ํ˜ธ๋ฅผ ๋ฒ„๋ฆด ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ์–ด, ๊ทธ๊ฒƒ์ด ์‹ค์ œ๋กœ ๋ฉ”์„œ๋“œ์ž„์„ ์ˆจ๊ธด๋‹ค. >>> s = Stock('GOOG', 100, 490.1) >>> s.shares # ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ 100 >>> s.cost # ๊ณ„์‚ฐ๋œ ๋ณ€์ˆ˜ 49010.0 >>> ํ†ต์ผ๋œ ์•ก์„ธ์Šค ๋งˆ์ง€๋ง‰ ์˜ˆ์ œ๋Š” ๊ฐ์ฒด์— ์ข€ ๋” ํ†ต์ผ๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค€๋‹ค. ์ด๊ฒƒ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ฐ์ฒด๋Š” ์‚ฌ์šฉํ•˜๊ธฐ ํ˜ผ๋™๋  ์ˆ˜ ์žˆ๋‹ค. >>> s = Stock('GOOG', 100, 490.1) >>> a = s.cost() # ๋ฉ”์„œ๋“œ 49010.0 >>> b = s.shares # ๋ฐ์ดํ„ฐ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ 100 >>> ์™œ cost์—๋Š” ()๊ฐ€ ๋ถ™์–ด์•ผ ํ•˜๊ณ  share๋Š” ๊ทธ๋ ‡์ง€ ์•Š์€๊ฐ€? ํ”„๋กœํผํ‹ฐ๋Š” ์ด๊ฒƒ์„ ๊ณ ์น  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ(Decorator) ๊ตฌ๋ฌธ @ ๊ตฌ๋ฌธ์„ "๋ฐ์ปค๋ ˆ์ด์…˜(decoration)"์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๋ฐ์ปค๋ ˆ์ด์…˜์€ ๋ฐ”๋กœ ๋’ค๋”ฐ๋ผ์˜ค๋Š” ํ•จ์ˆ˜ ์ •์˜์— ์ ์šฉ๋˜๋Š” ์ˆ˜์ •์ž๋ฅผ ์ง€์ •ํ•œ๋‹ค. ... @property def cost(self): return self.shares * self.price ์ž์„ธํ•œ ์‚ฌํ•ญ์€ 7์žฅ์—์„œ ๋‹ค๋ฃฌ๋‹ค. __slots__ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์ด๋ฆ„์˜ ์ง‘ํ•ฉ์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค. class Stock: __slots__ = ('name','_shares','price') def __init__(self, name, shares, price): self.name = name ... ๊ทธ ์™ธ์˜ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ์— ๋Œ€ํ•ด์„œ๋Š” ์˜ค๋ฅ˜๊ฐ€ ์ผ์–ด๋‚œ๋‹ค. >>> s.price = 385.15 >>> s.prices = 410.2 Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'Stock' object has no attribute 'prices' ์ด๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ  ๊ฐ์ฒด ์‚ฌ์šฉ์„ ์ œํ•œํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” ์„ฑ๋Šฅ์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋ฉฐ ํŒŒ์ด์ฌ์ด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•ด์ค€๋‹ค. ์บก์Šํ™”์— ๋Œ€ํ•œ ์ตœ์ข… ์˜๊ฒฌ ํ”„๋ผ์ด๋น— ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ, ํ”„๋กœํผํ‹ฐ, ์Šฌ๋กฏ ๋“ฑ์„ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ. ์ด๋Ÿฐ ๊ฒƒ๋“ค์€ ํŠน์ˆ˜ํ•œ ๋ชฉ์ ์ด ์žˆ์œผ๋ฉฐ, ๋‹ค๋ฅธ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ์ฝ๋‹ค ๋ณด๋ฉด ๋ˆˆ์— ๋Œ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ์ƒ์ ์ธ ์ฝ”๋”ฉ์—๋Š” ๊ฑฐ์˜ ํ•„์š” ์—†๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 5.6: ๋‹จ์ˆœ ํ”„๋กœํผํ‹ฐ ํ”„๋กœํผํ‹ฐ๋Š” '๊ณ„์‚ฐ๋œ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ'๋ฅผ ๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•˜๋Š” ์œ ์šฉํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค. stock.py์—์„œ Stock ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค. ๊ทธ ๊ฐ์ฒด๋Š” ๋ฐ์ดํ„ฐ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ์ถ”์ถœ ๋ฐฉ๋ฒ•์ด ์ผ๊ด€์ ์ด์ง€ ์•Š๋‹ค. >>> from stock import Stock >>> s = Stock('GOOG', 100, 490.1) >>> s.shares 100 >>> s.price 490.1 >>> s.cost() 49010.0 >>> ํŠนํžˆ, cost๋Š” ๋ฉ”์„œ๋“œ์ด๋ฏ€๋กœ ()๋ฅผ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ cost()๋ฅผ ํ”„๋กœํผํ‹ฐ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ด„ํ˜ธ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ cost ๊ณ„์‚ฐ์ด ์ด๋ค„์ง€๊ฒŒ Stock ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด๋ผ. >>> ================================ RESTART ================================ >>> from stock import Stock >>> s = Stock('GOOG', 100, 490.1) >>> s.cost 49010.0 >>> cost๊ฐ€ ํ”„๋กœํผํ‹ฐ๋กœ ์ •์˜๋˜์—ˆ์œผ๋ฏ€๋กœ, s.cost()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ž‘๋™ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. >>> s.cost() ... ์‹คํŒจ ... >>> ์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•˜๋ฉด ์•ž์˜ pcost.py ํ”„๋กœ๊ทธ๋žจ์ด ๊นจ์ง„๋‹ค. ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๊ฒŒ cost() ๋ฉ”์„œ๋“œ์˜ ()๋ฅผ ์ œ๊ฑฐํ•˜๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 5.7: ํ”„๋กœํผํ‹ฐ์™€ Setter ๊ฐ’์ด ํ”„๋ผ์ด๋น— ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ์— ์ €์žฅ๋˜๊ณ , ํ”„๋กœํผํ‹ฐ ํ•จ์ˆ˜์˜ ์Œ์„ ์‚ฌ์šฉํ•ด ํ•ญ์ƒ ์ •์ˆซ๊ฐ’์ด ์„ค์ •๋จ์„ ๋ณด์ฆํ•˜๊ฒŒ shares ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค. ์˜ˆ์ƒ๋˜๋Š” ํ–‰์œ„์˜ ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. >>> ================================ RESTART ================================ >>> from stock import Stock >>> s = Stock('GOOG',100,490.10) >>> s.shares = 50 >>> s.shares = 'a lot' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: expected an integer >>> ์—ฐ์Šต ๋ฌธ์ œ 5.8: ์Šฌ๋กฏ ์ถ”๊ฐ€ํ•˜๊ธฐ Stock ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•ด __slots__ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ, ์ƒˆ๋กœ์šด ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†์Œ์„ ํ™•์ธํ•œ๋‹ค. >>> ================================ RESTART ================================ >>> from stock import Stock >>> s = Stock('GOOG', 100, 490.10) >>> s.name 'GOOG' >>> s.blah = 42 ... ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ๋ณด๋ผ ... >>> __slots__๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์ด์ฌ์€ ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ํ‘œํ˜„์„ ๋” ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. s์˜ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์กฐ์‚ฌํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚ ๊นŒ? >>> s.__dict__ ... ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ๋ณด๋ผ ... >>> ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค์˜ ์ตœ์ ํ™”๋กœ __slots__๋ฅผ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์Šฌ๋กฏ์„ ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ›จ์”ฌ ์ ๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  ๋” ๋นจ๋ฆฌ ์‹คํ–‰๋œ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ์™ธ์˜ ํด๋ž˜์Šค์—์„œ๋Š” __slots__๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 6. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์ดํ„ฐ๋ ˆ์ด์…˜(for ๋ฃจํ”„)์€ ํŒŒ์ด์ฌ์˜ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจํ„ด์ด๋‹ค. ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค๋ฃจ๊ณ , ํŒŒ์ผ์„ ์ฝ๊ณ , ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์งˆ์˜ํ•˜๋Š” ๋“ฑ์˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ผ์— ์ดํ„ฐ๋ ˆ์ด์…˜์ด ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค. ํŒŒ์ด์ฌ์˜ ๊ฐ€์žฅ ํฐ ์žฅ์  ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฐ”๋กœ "์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜"๋ผ๋Š” ํ˜•ํƒœ๋กœ ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆํ•˜๊ณ  ์ƒˆ๋กญ๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์ด๋‹ค. ์ด ์„น์…˜์€ ์ด ์ฃผ์ œ๋ฅผ ๋‹ค๋ฃฌ๋‹ค. ๋์œผ๋กœ, ์‹ค์‹œ๊ฐ„ ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํฅ๋ฏธ๋กœ์šด ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•œ๋‹ค. 6.1 ์ดํ„ฐ๋ ˆ์ด์…˜ ํ”„๋กœํ† ์ฝœ 6.2 ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆ ํ•˜๊ธฐ 6.3 ์ƒ์‚ฐ์ž/์†Œ๋น„์ž ๋ฌธ์ œ์™€ ํ๋ฆ„ 6.4 ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹ 6.1 ์ดํ„ฐ๋ ˆ์ด์…˜ ํ”„๋กœํ† ์ฝœ ์ด ์„น์…˜์€ ์ดํ„ฐ๋ ˆ์ด์…˜์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณธ๋‹ค. ๋„ˆ๋„๋‚˜๋„ ์ดํ„ฐ๋ ˆ์ด์…˜ ์˜จ๊ฐ– ๊ฐ์ฒด๋“ค์ด ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ง€์›ํ•œ๋‹ค. a = 'hello' for c in a: # a์˜ ๋ฌธ์ž๋ฅผ ๋ฃจํ•‘ ... b = { 'name': 'Dave', 'password':'foo'} for k in b: # ๋”•์…”๋„ˆ๋ฆฌ ํ‚ค๋ฅผ ๋ฃจํ•‘ ... c = [1,2,3,4] for i in c: # ๋ฆฌ์ŠคํŠธ/ํŠœํ”Œ์˜ ํ•ญ๋ชฉ์„ ๋ฃจํ•‘ ... f = open('foo.txt') for x in f: # ํŒŒ์ผ์˜ ํ–‰์„ ๋ฃจํ•‘ ... ์ดํ„ฐ๋ ˆ์ด์…˜: ํ”„๋กœํ† ์ฝœ for ๋ฌธ์„ ์ƒ๊ฐํ•ด ๋ณด์ž. for x in obj: # ๋ฌธ์žฅ ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”๊ฐ€? _iter = obj.__iter__() # ์ดํ„ฐ ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๋ฅผ ์–ป์Œ while True: try: x = _iter.__next__() # ๋‹ค์Œ ํ•ญ๋ชฉ์„ ์–ป์Œ except StopIteration: # ๋‚จ์€ ํ•ญ๋ชฉ์ด ์—†์Œ break # ๋ฌธ์žฅ ... for ๋ฌธ์—์„œ ์ž‘๋™ํ•˜๋Š” ๋ชจ๋“  ๊ฐ์ฒด๋Š” ์ด๋Ÿฌํ•œ ์ € ์ˆ˜์ค€ ์ดํ„ฐ๋ ˆ์ด์…˜ ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด๋‹ค. ์˜ˆ์ œ: ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ˆ˜์ž‘์—…์œผ๋กœ ์ดํ„ฐ๋ ˆ์ด์…˜. >>> x = [1,2,3] >>> it = x.__iter__() >>> it <listiterator object at 0x590b0> >>> it.__next__() >>> it.__next__() >>> it.__next__() >>> it.__next__() Traceback (most recent call last): File "<stdin>", line 1, in? StopIteration >>> ์ดํ„ฐ๋ ˆ์ด์…˜ ์ง€์›ํ•˜๊ธฐ ์ดํ„ฐ๋ ˆ์ด์…˜์˜ ์œ ์šฉ์„ฑ์„ ์ดํ•ดํ•œ๋‹ค๋ฉด ์Šค์Šค๋กœ ์ž‘์„ฑํ•œ ๊ฐ์ฒด๋„ ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ง€์›ํ•˜๊ฒŒ ํ•˜๊ณ  ์‹ถ์„ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ปค์Šคํ…€ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. class Portfolio: def __init__(self): self.holdings = [] def __iter__(self): return self.holdings.__iter__() ... port = Portfolio() for s in port: ... ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 6.1: ์ดํ„ฐ๋ ˆ์ด์…˜ ๋œฏ์–ด๋ณด๊ธฐ ๋‹ค์Œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. a = [1,9,4,25,16] ์ด ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์ˆ˜์ž‘์—…์œผ๋กœ ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ˆ˜ํ–‰ํ•˜๋ผ. __iter__()๋ฅผ ํ˜ธ์ถœํ•ด ์ดํ„ฐ ๋ ˆ์ดํ„ฐ๋ฅผ ์–ป์€ ๋‹ค์Œ, __next__() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ๋‹ค์Œ ๋ฒˆ ์›์†Œ๋ฅผ ์–ป๋Š”๋‹ค. >>> i = a.__iter__() >>> i <listiterator object at 0x64c10> >>> i.__next__() >>> i.__next__() >>> i.__next__() >>> i.__next__() 25 >>> i.__next__() 16 >>> i.__next__() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> ๋นŒํŠธ์ธ ํ•จ์ˆ˜ next()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ดํ„ฐ ๋ ˆ์ดํ„ฐ์˜ __next__() ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ ๋„ ๊ฐ™์€ ์ผ์„ ํ•ด ๋ณด์ž. >>> f = open('Data/portfolio.csv') >>> f.__iter__() # ์ฐธ๊ณ : ์ด๊ฒƒ์€ ํŒŒ์ผ ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•จ <_io.TextIOWrapper name='Data/portfolio.csv' mode='r' encoding='UTF-8'> >>> next(f) 'name, shares, price ' >>> next(f) '"AA",100,32.20\n' >>> next(f) '"IBM",50,91.10\n' >>> ํŒŒ์ผ์˜ ๋์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ next(f)๋ฅผ ๊ณ„์† ํ˜ธ์ถœํ•˜๋ผ. ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”์ง€ ๊ด€์ฐฐํ•˜๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 6.2: ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ง€์›ํ•˜๊ธฐ ๋‹น์‹ ์ด ์ง์ ‘ ๋งŒ๋“  ๊ฐ์ฒด๊ฐ€ ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ง€์›ํ•˜๊ฒŒ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ๋‹ค. ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ๋‚˜ ๋‹ค๋ฅธ ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ๊ฐ์‹ธ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ์„ ๋•Œ๊ฐ€ ํŠนํžˆ ๊ทธ๋ ‡๋‹ค. ์ƒˆ ํŒŒ์ผ portfolio.py์— ๋‹ค์Œ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์ž. # portfolio.py class Portfolio: def __init__(self, holdings): self._holdings = holdings @property def total_cost(self): return sum([s.cost for s in self._holdings]) def tabulate_shares(self): from collections import Counter total_shares = Counter() for s in self._holdings: total_shares[s.name] += s.shares return total_shares ์ด ํด๋ž˜์Šค๋Š” ๋ฆฌ์ŠคํŠธ ์ฃผ์œ„์— ๊ณ„์ธต์„ ๋ง์”Œ์šฐ๋˜, total_cost ํ”„๋กœํผํ‹ฐ์™€ ๊ฐ™์€ ์ถ”๊ฐ€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ–๋Š”๋‹ค. report.py์˜ read_portfolio() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ Portfolio ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ํ•˜์ž. # report.py ... import fileparse from stock import Stock from portfolio import Portfolio def read_portfolio(filename): ''' ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค ํŒŒ์ผ์„ ์ฝ์–ด ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ. name, shares, price๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉ. ''' with open(filename) as file: portdicts = fileparse.parse_csv(file, select=['name','shares','price'], types=[str, int, float]) portfolio = [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ] return Portfolio(portfolio) ... report.py ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•ด ๋ณด๋ผ. Portfolio ์ธ์Šคํ„ด์Šค๊ฐ€ ์ดํ„ฐ๋Ÿฌ๋ธ”ํ•˜์ง€ ์•Š๋‹ค๋Š” ์‚ฌ์‹ค๋กœ ์ธํ•ด ์š”๋ž€ํ•˜๊ฒŒ ์‹คํŒจํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด๊ฒŒ ๋œ๋‹ค. >>> import report >>> report.portfolio_report('Data/portfolio.csv', 'Data/prices.csv') ... ์ถฉ๋Œํ•œ๋‹ค ... Portfolio ํด๋ž˜์Šค๊ฐ€ ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ง€์›ํ•˜๊ฒŒ ์ˆ˜์ •ํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด ๋ณด์ž. class Portfolio: def __init__(self, holdings): self._holdings = holdings def __iter__(self): return self._holdings.__iter__() @property def total_cost(self): return sum([s.shares*s.price for s in self._holdings]) def tabulate_shares(self): from collections import Counter total_shares = Counter() for s in self._holdings: total_shares[s.name] += s.shares return total_shares ์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•˜๊ณ  ๋‚˜๋ฉด report.py ํ”„๋กœ๊ทธ๋žจ์ด ๋‹ค์‹œ ์ž˜ ์ž‘๋™ํ•  ๊ฒƒ์ด๋‹ค. pcost.py ํ”„๋กœ๊ทธ๋žจ์ด ์ƒˆ๋กœ์šด Portfolio ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ์ˆ˜์ •ํ•˜์ž. ์ด๋ ‡๊ฒŒ ๋ง์ด๋‹ค. # pcost.py import report def portfolio_cost(filename): ''' ํฌํŠธํด๋ฆฌ์˜ค ํŒŒ์ผ์˜ ์ด๋น„์šฉ(์ฃผ์‹ ์ˆ˜ * ๊ฐ€๊ฒฉ)์„ ๊ณ„์‚ฐ ''' portfolio = report.read_portfolio(filename) return portfolio.total_cost ... ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ์‹œํ—˜ํ•ด ๋ณด์ž. >>> import pcost >>> pcost.portfolio_cost('Data/portfolio.csv') 44671.15 >>> ์—ฐ์Šต ๋ฌธ์ œ 6.3: ๋” ์ ์ ˆํ•œ ์ปจํ…Œ์ด๋„ˆ ๋งŒ๋“ค๊ธฐ ์ปจํ…Œ์ด๋„ˆ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋‹ค ๋ณด๋ฉด ์ดํ„ฐ๋ ˆ์ด์…˜ ์™ธ์—๋„ ๋„ฃ๊ณ  ์‹ถ์€ ๊ธฐ๋Šฅ์ด ๋” ์žˆ์„ ๊ฒƒ์ด๋‹ค. Portfolio ํด๋ž˜์Šค๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ด ํŠน์ˆ˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด์ž. class Portfolio: def __init__(self, holdings): self._holdings = holdings def __iter__(self): return self._holdings.__iter__() def __len__(self): return len(self._holdings) def __getitem__(self, index): return self._holdings[index] def __contains__(self, name): return any([s.name == name for s in self._holdings]) @property def total_cost(self): return sum([s.shares*s.price for s in self._holdings]) def tabulate_shares(self): from collections import Counter total_shares = Counter() for s in self._holdings: total_shares[s.name] += s.shares return total_shares ์ƒˆ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์‹คํ—˜์„ ํ•ด ๋ณด์ž. >>> import report >>> portfolio = report.read_portfolio('Data/portfolio.csv') >>> len(portfolio) >>> portfolio[0] Stock('AA', 100, 32.2) >>> portfolio[1] Stock('IBM', 50, 91.1) >>> portfolio[0:3] [Stock('AA', 100, 32.2), Stock('IBM', 50, 91.1), Stock('CAT', 150, 83.44)] >>> 'IBM' in portfolio True >>> 'AAPL' in portfolio False >>> ์ด์™€ ๊ด€๋ จํ•ด ์ค‘์š”ํ•œ ์ ์„ ๊ด€์ฐฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ๊ฐ€ ํŒŒ์ด์ฌ์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์–ดํœ˜๋ฅผ ๋งํ•œ๋‹ค๋ฉด 'ํŒŒ์ด์ฌ ๋‹ค์šด(Pythonic)' ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ๋œ๋‹ค. ์ปจํ…Œ์ด๋„ˆ ๊ฐ์ฒด๋ฅผ ํŒŒ์ด์ฌ๋‹ต๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด ์ดํ„ฐ๋ ˆ์ด์…˜, ์ธ๋ฑ์‹ฑ, containment, ๊ธฐํƒ€ ์—ฐ์‚ฐ์ž๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. 6.2 ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ์ด ์„น์…˜์€ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆ ํ•˜๋Š” ๋ฒ•์„ ์‚ดํŽด๋ณธ๋‹ค. ๋ฌธ์ œ ์ปค์Šคํ…€ ์ดํ„ฐ๋ ˆ์ด์…˜ ํŒจํ„ด์„ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๊ณ  ํ•˜์ž. ์˜ˆ(์นด์šดํŠธ๋‹ค์šด): >>> for x in countdown(10): ... print(x, end=' ') ... 10 9 8 7 6 5 4 3 2 1 >>> ์ด๊ฒƒ์„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ •์˜ํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. def countdown(n): while n > 0: yield n n -= 1 ์˜ˆ: >>> for x in countdown(10): ... print(x, end=' ') ... 10 9 8 7 6 5 4 3 2 1 >>> yield ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์™€ ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ์ฆ‰์‹œ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. def countdown(n): # print ๋ฌธ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค print('Counting down from', n) while n > 0: yield n n -= 1 >>> x = countdown(10) # print ๋ฌธ์ด ์—†๋‹ค! >>> x # x๋Š” generator ๊ฐ์ฒด๋‹ค <generator object at 0x58490> >>> ์ด ํ•จ์ˆ˜๋Š” __next__() ํ˜ธ์ถœ์— ์˜ํ•ด์„œ๋งŒ ์‹คํ–‰๋œ๋‹ค. >>> x = countdown(10) >>> x <generator object at 0x58490> >>> x.__next__() Counting down from 10 10 >>> yield๋Š” ๊ฐ’์„ ์ƒ์‚ฐํ•˜๊ณ , ํ•จ์ˆ˜ ์‹คํ–‰์„ ์ผ์‹œ<NAME>๋‹ค. ๋‹ค์Œ๋ฒˆ __next__() ํ˜ธ์ถœ ์‹œ ํ•จ์ˆ˜๊ฐ€ ์žฌ๊ฐœ๋œ๋‹ค. >>> x.__next__() >>> x.__next__() ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๊ฐ€ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ๋•Œ, ์ดํ„ฐ๋ ˆ์ด์…˜์€ ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚จ๋‹ค. >>> x.__next__() >>> x.__next__() Traceback (most recent call last): File "<stdin>", line 1, in? StopIteration >>> ๊ด€์ฐฐ: ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ๋”•์…”๋„ˆ๋ฆฌ, ํŒŒ์ผ ๋“ฑ์— ์‚ฌ์šฉํ•˜๋Š” for ๋ฌธ๊ณผ ๋˜‘๊ฐ™์€ ์ € ์ˆ˜์ค€ ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 6.4: ๋‹จ์ˆœํ•œ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ์—๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ ๋– ์˜ฌ๋ ค๋ผ. ์ž‘์„ฑํ•˜๊ธฐ ์‰ฝ๋‹ค---์›ํ•˜๋Š” ์ดํ„ฐ๋ ˆ์ด์…˜ ๋กœ์ง์„ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜๊ณ , yield ๋ฌธ์œผ๋กœ ๊ฐ’์„ ๋‚ด๋ณด๋‚ด๋ฉด ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ํŒŒ์ผ์—์„œ ํŠน์ • ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด ์ผ์น˜ํ•˜๋Š” ํ–‰์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค. >>> def filematch(filename, substr): with open(filename, 'r') as f: for line in f: if substr in line: yield line >>> for line in open('Data/portfolio.csv'): print(line, end='') name, shares, price "AA",100,32.20 "IBM",50,91.10 "CAT",150,83.44 "MSFT",200,51.23 "GE",95,40.37 "MSFT",50,65.10 "IBM",100,70.44 >>> for line in filematch('Data/portfolio.csv', 'IBM'): print(line, end='') "IBM",50,91.10 "IBM",100,70.44 >>> ํ•จ์ˆ˜์—์„œ ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆ ํ•œ ๋ถ€๋ถ„์„ ์ˆจ๊ธฐ๊ณ  for ๋ฃจํ”„์— ๋จน์ด๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ๊ฝค ํฅ๋ฏธ๋กญ๋‹ค. ์ข€ ๋” ํŠน์ดํ•œ ์‚ฌ๋ก€๋ฅผ ์‚ดํŽด๋ณด์ž. ์—ฐ์Šต ๋ฌธ์ œ 6.5: ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋ง ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ๋กœ๊ทธ ํŒŒ์ผ์ด๋ผ๋“ ์ง€ ์ฃผ์‹ ์‹œ์žฅ ํ”ผ๋“œ์™€ ๊ฐ™์€ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๋Š” ํฅ๋ฏธ๋กœ์šด ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด ๊ฐœ๋…์„ ํƒ์ƒ‰ํ•ด ๋ณด์ž. ์‹œ์ž‘ํ•˜๋ ค๋ฉด ๋‹ค์Œ ์ง€์นจ์„ ์ฃผ์˜ ๊นŠ๊ฒŒ ๋”ฐ๋ฅด๊ธฐ ๋ฐ”๋ž€๋‹ค. Data/stocksim.py๋Š” ์ฃผ์‹ ์‹œ์žฅ ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ์€ ์ถœ๋ ฅ์œผ๋กœ์„œ, Data/stocklog.csv ํŒŒ์ผ์— ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์†์ ์œผ๋กœ ๊ธฐ๋กํ•œ๋‹ค. ๋ณ„๋„์˜ ๋ช…๋ น ์ฐฝ์—์„œ Data/ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๊ฐ€์„œ ์ด ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•œ๋‹ค. bash % python3 stocksim.py ์œˆ๋„์—์„œ๋Š” ํƒ์ƒ‰๊ธฐ์—์„œ stocksim.py๋ฅผ ๋”๋ธ”ํด๋ฆญํ•˜๋ฉด ์‹คํ–‰๋œ๋‹ค. ์ด์ œ ์ด ํ”„๋กœ๊ทธ๋žจ์€ ์žŠ์–ด๋ฒ„๋ฆฌ์ž(์‹คํ–‰๋˜๊ฒŒ ๋‚ด๋ฒ„๋ ค ๋‘”๋‹ค). ๋‹ค๋ฅธ ์ฐฝ์—์„œ, ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ์— ์˜ํ•ด Data/stocklog.csv ํŒŒ์ผ์ด ๊ธฐ๋ก๋˜๋Š” ๊ฒƒ์„ ์‚ดํŽด๋ณด์ž. ๋ช‡ ์ดˆ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ํ…์ŠคํŠธ ํ–‰์ด ํŒŒ์ผ์— ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, ์ด ํ”„๋กœ๊ทธ๋žจ์€ ๋ช‡ ์‹œ๊ฐ„์ด๊ณ  ์‹คํ–‰๋˜๊ฒŒ ๋‚ด๋ฒ„๋ ค ๋‘”๋‹ค(๊ฑฑ์ •ํ•  ํ•„์š”๋Š” ์—†๋‹ค). ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋ฉด, ํŒŒ์ผ์„ ์—ด๊ณ  ํŒŒ์ผ์˜ ๋์— ์ƒˆ๋กœ์šด ์ถœ๋ ฅ์ด ์žˆ๋Š”์ง€ ๊ฐ์‹œ(watch) ํ•˜๋Š” ์ž‘์€ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์ž. follow.py ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. # follow.py import os import time f = open('Data/stocklog.csv') f.seek(0, os.SEEK_END) # ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ํŒŒ์ผ์˜ ๋์œผ๋กœ๋ถ€ํ„ฐ 0 ๋ฐ”์ดํŠธ๋กœ ์ด๋™ while True: line = f.readline() if line == '': time.sleep(0.1) # ์งง๊ฒŒ ์‰ฌ์—ˆ๋‹ค๊ฐ€ ์žฌ์‹œ๋„ continue fields = line.split(',') name = fields[0].strip('"') price = float(fields[1]) change = float(fields[4]) if change < 0: print(f'{name:>10s} {price:>10.2f} {change:>10.2f}') ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด ์‹ค์‹œ๊ฐ„ ์ฃผ์‹ ํ‹ฐ์ปค๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด ์ฝ”๋“œ๋Š” ์œ ๋‹‰์Šค์˜ tail -f ๋ช…๋ น์ฒ˜๋Ÿผ ๋กœ๊ทธ ํŒŒ์ผ์„ ๊ฐ์‹œํ•œ๋‹ค. ์ฐธ๊ณ : ํŒŒ์ผ์—์„œ ํ–‰์„ ์ฝ๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋ผ๋Š” ์ ์—์„œ, ์ด ์˜ˆ์˜ readline() ๋ฉ”์„œ๋“œ์˜ ์‚ฌ์šฉ์€ ๊ทธ๋ฆฌ ์ผ๋ฐ˜์ ์ด์ง€ ์•Š๋‹ค(๋ณดํ†ต์€ for ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค). ๊ทธ๋ ‡์ง€๋งŒ ์ด ๊ฒฝ์šฐ ํŒŒ์ผ์˜ ๋์— ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€๋˜๋Š”์ง€ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ™•์ธํ•œ๋‹ค(readline()์€ ์ƒˆ ๋ฐ์ดํ„ฐ ๋˜๋Š” ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค). ์—ฐ์Šต ๋ฌธ์ œ 6.6: ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์‚ฐ ์—ฐ์Šต ๋ฌธ์ œ 6.5์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด, ์ฝ”๋“œ์˜ ์ฒซ ๋ถ€๋ถ„์€ ๋ฐ์ดํ„ฐ์˜ ํ–‰๋“ค์„ ์ƒ์‚ฐํ•˜๊ณ , while ๋ฃจํ”„์˜ ๋ ๋ฌธ์žฅ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์†Œ๋น„ํ•œ๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜์˜ ์ฃผ์š” ๊ธฐ๋Šฅ์€ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์ƒ์‚ฐ ์ฝ”๋“œ๋ฅผ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜๋กœ ์ด์ „ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜ follow(filename) ์ด ํŒŒ์ผ ์ฝ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์—ฐ์Šต ๋ฌธ์ œ 6.5์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ž. ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ์ž‘๋™ํ•˜๊ฒŒ ํ•˜๋ผ. >>> for line in follow('Data/stocklog.csv'): print(line, end='') ... ์ถœ๋ ฅ ํ–‰์ด ์—ฌ๊ธฐ ๋งŒ๋“ค์–ด์ ธ์•ผ ํ•œ๋‹ค ... ์ฃผ์‹ ํ‹ฐ์ปค ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜์ž. if __name__ == '__main__': for line in follow('Data/stocklog.csv'): fields = line.split(',') name = fields[0].strip('"') price = float(fields[1]) change = float(fields[4]) if change < 0: print(f'{name:>10s} {price:>10.2f} {change:>10.2f}') ์—ฐ์Šต ๋ฌธ์ œ 6.7: ํฌํŠธํด๋ฆฌ์˜ค ๊ฐ์‹œํ•˜๊ธฐ ์ฃผ์‹ ๋ฐ์ดํ„ฐ์˜ ์ŠคํŠธ๋ฆผ์„ ๊ฐ์‹œํ•ด, ํฌํŠธํด๋ฆฌ์˜ค์— ์žˆ๋Š” ์ฃผ์‹์˜ ์ •๋ณด๋งŒ ํ‹ฐ์ปค๋ฅผ ํ”„๋ฆฐํŠธํ•˜๋„๋ก follow.py ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•˜์ž. ์˜ˆ: if __name__ == '__main__': import report portfolio = report.read_portfolio('Data/portfolio.csv') for line in follow('Data/stocklog.csv'): fields = line.split(',') name = fields[0].strip('"') price = float(fields[1]) change = float(fields[4]) if name in portfolio: print(f'{name:>10s} {price:>10.2f} {change:>10.2f}') ์ฐธ๊ณ : ์ด ์ž‘์—…์„ ์œ„ํ•ด, Portfolio ํด๋ž˜์Šค๋Š” ๋ฐ˜๋“œ์‹œ in ์—ฐ์‚ฐ์ž๋ฅผ ์ง€์›ํ•ด์•ผ ํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 6.3์„ ์ฐธ๊ณ ํ•ด์„œ __contains__() ์—ฐ์‚ฐ์ž๋ฅผ ๊ตฌํ˜„ํ•˜์ž. ๋…ผ์˜ ๋งค์šฐ ๊ฐ•๋ ฅํ•œ ๊ฒƒ์„ ๋งŒ๋“ค์—ˆ๋‹ค. ํฅ๋ฏธ๋กœ์šด ์ดํ„ฐ๋ ˆ์ด์…˜ ํŒจํ„ด(ํŒŒ์ผ์˜ ๋งˆ์ง€๋ง‰ ํ–‰์„ ์ฝ๊ธฐ)์„ ๊ทธ๊ฒƒ์˜ ์ž‘์€ ํ•จ์ˆ˜์— ์˜ฎ๊ฒผ๋‹ค. ์ด์ œ follow() ํ•จ์ˆ˜๋Š” ์™„์ „ํžˆ ์ผ๋ฐ˜์ ์ธ ์œ ํ‹ธ๋ฆฌํ‹ฐ๊ฐ€ ๋˜์—ˆ์œผ๋ฉฐ, ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์„œ๋ฒ„ ๋กœ๊ทธ, ๋””๋ฒ„๊น… ๋กœ๊ทธ, ๊ธฐํƒ€ ๊ทธ์™€ ๋น„์Šทํ•œ ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. ํ›Œ๋ฅญํ•˜๋‹ค. 6.3 ์ƒ์‚ฐ์ž, ์†Œ๋น„์ž, ํŒŒ์ดํ”„๋ผ์ธ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ์ƒ์‚ฐ์ž/์†Œ๋น„์ž ๋ฌธ์ œ์™€ ๋ฐ์ดํ„ฐ ํ๋ฆ„ ํŒŒ์ดํ”„๋ผ์ธ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐ ์œ ์šฉํ•œ ๋„๊ตฌ๋‹ค. ์ด ์„น์…˜์€ ๊ทธ์— ๋Œ€ํ•ด ๋…ผ์˜ํ•œ๋‹ค. ์ƒ์‚ฐ์ž-์†Œ๋น„์ž ๋ฌธ์ œ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ์ƒ์‚ฐ์ž-์†Œ๋น„์ž(producer-consumer) ๋ฌธ์ œ์™€ ๋ฐ€์ ‘ํ•œ ์—ฐ๊ด€์ด ์žˆ๋‹ค. # ์ƒ์‚ฐ์ž def follow(f): ... while True: ... yield line # ์•„๋ž˜ `line`์— ๊ฐ’์„ ์ƒ์‚ฐ ... # ์†Œ๋น„์ž for line in follow(f): # ์œ„์—์„œ `yield`ํ•œ ๊ฐ’์„ ์†Œ๋น„ ... yield๊ฐ€ ์ƒ์‚ฐํ•œ ๊ฐ’์„ for ๋ฌธ์—์„œ ์†Œ๋น„ํ•œ๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํŒŒ์ดํ”„๋ผ์ธ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์˜ ์ด๋Ÿฌํ•œ ์ธก๋ฉด์„ ์‚ฌ์šฉํ•ด ์ฒ˜๋ฆฌ ํŒŒ์ดํ”„๋ผ์ธ(Unix ํŒŒ์ดํ”„ ๊ฐ™์€ ๊ฒƒ)์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ์‚ฐ์ž โ†’ ์ฒ˜๋ฆฌ โ†’ ์ฒ˜๋ฆฌ โ†’ ์†Œ๋น„์ž ์ฒ˜๋ฆฌ ํŒŒ์ดํ”„๋Š” ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ ์ƒ์‚ฐ์ž, ์ค‘๊ฐ„ ์ฒ˜๋ฆฌ ๋‹จ๊ณ„, ์ตœ์ข… ์†Œ๋น„์ž๋กœ ์ด๋ค„์ง„๋‹ค. ์ƒ์‚ฐ์ž โ†’ ์ฒ˜๋ฆฌ โ†’ ์ฒ˜๋ฆฌ โ†’ ์†Œ๋น„์ž def producer(): ... yield item ... ์ƒ์‚ฐ์ž๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋‹ค. ๋‹ค๋ฅธ ์‹œํ€€์Šค์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๋‹ค. yield๋Š” ํŒŒ์ดํ”„๋ผ์ธ์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต๊ธ‰ํ•œ๋‹ค. ์ƒ์‚ฐ์ž โ†’ ์ฒ˜๋ฆฌ โ†’ ์ฒ˜๋ฆฌ โ†’ ์†Œ๋น„์ž def consumer(s): for item in s: ... ์†Œ๋น„์ž๋Š” for ๋ฃจํ”„๋‹ค. ํ•ญ๋ชฉ์„ ์–ป์–ด ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•œ๋‹ค. ์ƒ์‚ฐ์ž โ†’ ์ฒ˜๋ฆฌ โ†’ ์ฒ˜๋ฆฌ โ†’ ์†Œ๋น„์ž def processing(s): for item in s: ... yield newitem ... ์ค‘๊ฐ„ ์ฒ˜๋ฆฌ ๋‹จ๊ณ„์—์„œ๋Š” ํ•ญ๋ชฉ์˜ ์†Œ๋น„์™€ ์ƒ์‚ฐ์ด ๋™์‹œ์— ์ด๋ค„์ง„๋‹ค. ๊ทธ๊ฒƒ๋“ค์€ ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆผ์„ ์ˆ˜์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๊ฒƒ๋“ค์€ ํ•„ํ„ฐ๋ง(ํ•ญ๋ชฉ์„ ๋ฒ„๋ฆผ)์„ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ƒ์‚ฐ์ž โ†’ ์ฒ˜๋ฆฌ โ†’ ์ฒ˜๋ฆฌ โ†’ ์†Œ๋น„์ž def producer(): ... yield item # yield ํ•œ item์„ `processing`์ด ๋ฐ›์Œ ... def processing(s): for item in s: # `producer`๋กœ๋ถ€ํ„ฐ ์˜จ ๊ฒƒ ... yield newitem # newitem์„ yield ... def consumer(s): for item in s: # `processing`์œผ๋กœ๋ถ€ํ„ฐ ์˜จ ๊ฒƒ ... ํŒŒ์ดํ”„๋ผ์ธ์„ ์„ค์ •ํ•˜๋Š” ์ฝ”๋“œ a = producer() b = processing(a) c = consumer(b) ๋ฐ์ดํ„ฐ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค์„ ํ†ตํ•ด ์ ์ง„์ ์œผ๋กœ ํ๋ฅธ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์ด ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ์œ„ํ•ด stocksim.py ํ”„๋กœ๊ทธ๋žจ์„ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ๊ณ„์† ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ์ด์ „ ์—ฐ์Šต ๋ฌธ์ œ์—์„œ ์ž‘์„ฑํ•œ follow() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 6.8: ๋‹จ์ˆœํ•œ ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ ๊ฐœ๋…์˜ ์‹ค์ œ ๊ตฌํ˜„์„ ์‚ดํŽด๋ณด์ž. ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ. >>> def filematch(lines, substr): for line in lines: if substr in line: yield line >>> ์ด ํ•จ์ˆ˜๋Š” ์ง€๋‚œ ์—ฐ์Šต ๋ฌธ์ œ์˜ ์ฒซ ๋ฒˆ์งธ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์˜ˆ์ œ์™€ ๊ฑฐ์˜ ๊ฐ™์ง€๋งŒ, ํŒŒ์ผ์„ ์—ด์ง€ ์•Š๊ณ  ์ธ์ž๋กœ ๋ฐ›์€ ํ–‰๋“ค์˜ ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ๋งŒ ์—ฐ์‚ฐ์„ ํ•œ๋‹ค. ๋‹ค์Œ์„ ์‹œ๋„ํ•ด ๋ณด์ž. >>> lines = follow('Data/stocklog.csv') >>> ibm = filematch(lines, 'IBM') >>> for line in ibm: print(line) ... ์ถœ๋ ฅ์„ ๊ธฐ๋‹ค๋ฆฐ๋‹ค ... ์ถœ๋ ฅ์ด ๋‚˜ํƒ€๋‚˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๊ฒฐ๊ตญ IBM์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ํ–‰์ด ๋‚˜ํƒ€๋‚œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 6.9: ์ข€ ๋” ๋ณต์žกํ•œ ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ์˜ ์•„์ด๋””์–ด๋ฅผ ๋ฐœ์ „์‹œ์ผœ์„œ ๋” ๋งŽ์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด ๋ณด์ž. >>> from follow import follow >>> import csv >>> lines = follow('Data/stocklog.csv') >>> rows = csv.reader(lines) >>> for row in rows: print(row) ['BA', '98.35', '6/11/2007', '09:41.07', '0.16', '98.25', '98.35', '98.31', '158148'] ['AA', '39.63', '6/11/2007', '09:41.07', '-0.03', '39.67', '39.63', '39.31', '270224'] ['XOM', '82.45', '6/11/2007', '09:41.07', '-0.23', '82.68', '82.64', '82.41', '748062'] ['PG', '62.95', '6/11/2007', '09:41.08', '-0.12', '62.80', '62.97', '62.61', '454327'] ... ํฅ๋ฏธ๋กญ๋‹ค. follow() ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ์ด csv.reader() ํ•จ์ˆ˜๋กœ ์ „๋‹ฌ๋˜์–ด ํ–‰์ด ๋ถ„ํ• ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 6.10: ๋” ํฐ ํŒŒ์ดํ”„๋ผ์ธ ์ปดํฌ๋„ŒํŠธ ํŒŒ์ดํ”„๋ผ์ธ์„ ๋” ํ‚ค์›Œ๋ณด์ž. ๋ณ„๋„์˜ ํŒŒ์ผ ticker.py์— CSV ํŒŒ์ผ์„ ์ฝ๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. # ticker.py from follow import follow import csv def parse_stock_data(lines): rows = csv.reader(lines) return rows if __name__ == '__main__': lines = follow('Data/stocklog.csv') rows = parse_stock_data(lines) for row in rows: print(row) ํŠน์ • ์นผ๋Ÿผ์„ ์„ ํƒํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กœ ์ž‘์„ฑํ•œ๋‹ค. # ticker.py ... def select_columns(rows, indices): for row in rows: yield [row[index] for index in indices] ... def parse_stock_data(lines): rows = csv.reader(lines) rows = select_columns(rows, [0, 1, 4]) return rows ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์‹œ ์‹คํ–‰ํ•˜์ž. ์„ ํƒํ•œ ์นผ๋Ÿผ๋งŒ ์ถœ๋ ฅ๋œ๋‹ค. ['BA', '98.35', '0.16'] ['AA', '39.63', '-0.03'] ['XOM', '82.45','-0.23'] ['PG', '62.95', '-0.12'] ... ์ž๋ฃŒํ˜•์„ ๋ณ€ํ™˜ํ•ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์ž. ์˜ˆ: # ticker.py ... def convert_types(rows, types): for row in rows: yield [func(val) for func, val in zip(types, row)] def make_dicts(rows, headers): for row in rows: yield dict(zip(headers, row)) ... def parse_stock_data(lines): rows = csv.reader(lines) rows = select_columns(rows, [0, 1, 4]) rows = convert_types(rows, [str, float, float]) rows = make_dicts(rows, ['name', 'price', 'change']) return rows ... ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์‹œ ์‹คํ–‰ํ•˜์ž. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ŠคํŠธ๋ฆฌ๋ฐ ํ•˜๊ฒŒ ๋๋‹ค. { 'name':'BA', 'price':98.35, 'change':0.16 } { 'name':'AA', 'price':39.63, 'change':-0.03 } { 'name':'XOM', 'price':82.45, 'change': -0.23 } { 'name':'PG', 'price':62.95, 'change':-0.12 } ... ์—ฐ์Šต ๋ฌธ์ œ 6.11: ๋ฐ์ดํ„ฐ๋ฅผ ํ•„ํ„ฐ๋งํ•˜๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•„ํ„ฐ๋งํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์ž. ์˜ˆ: # ticker.py ... def filter_symbols(rows, names): for row in rows: if row['name'] in names: yield row ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•ด ํฌํŠธํด๋ฆฌ์˜ค์— ์žˆ๋Š” ์ฃผ์‹๋งŒ ๋‚˜์˜ค๊ฒŒ ํ•ด๋ณด์ž. import report portfolio = report.read_portfolio('Data/portfolio.csv') rows = parse_stock_data(follow('Data/stocklog.csv')) rows = filter_symbols(rows, portfolio) for row in rows: print(row) ์—ฐ์Šต ๋ฌธ์ œ 6.12: ๋ชจ๋‘ ํ•ฉ์น˜๊ธฐ ์ฃผ์–ด์ง„ ํฌํŠธํด๋ฆฌ์˜ค, ๋กœ๊ทธ ํŒŒ์ผ, ํ‘œ<NAME>๋Œ€๋กœ ์‹ค์‹œ๊ฐ„ ์ฃผ์‹ ํ‹ฐ์ปค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ticker(portfile, logfile, fmt) ํ•จ์ˆ˜๋ฅผ ticker.py ํ”„๋กœ๊ทธ๋žจ์— ์ž‘์„ฑํ•œ๋‹ค. ์˜ˆ: >>> from ticker import ticker >>> ticker('Data/portfolio.csv', 'Data/stocklog.csv', 'txt') Name Price Change ---------- ---------- ---------- GE 37.14 -0.18 MSFT 29.96 -0.09 CAT 78.03 -0.49 AA 39.34 -0.32 ... >>> ticker('Data/portfolio.csv', 'Data/stocklog.csv', 'csv') Name, Price, Change IBM, 102.79, -0.28 CAT, 78.04, -0.48 AA, 39.35, -0.31 CAT, 78.05, -0.47 ... ๋…ผ์˜ ๋ฐฐ์šด ๊ฒƒ: ๋‹ค์–‘ํ•œ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ทธ๊ฒƒ๋“ค์„ ํ•จ๊ป˜ ์—ฎ์–ด์„œ ๋ฐ์ดํ„ฐ ํ๋ฆ„ ํŒŒ์ดํ”„๋ผ์ธ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋‹จ๊ณ„๋“ค์„ ํŒจํ‚ค์ง• ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋‹จ์ผ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค(์˜ˆ: parse_stock_data() ํ•จ์ˆ˜). 6.4 ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์‹ฌํ™” ์ด ์„น์…˜์€ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹๊ณผ itertools ๋ชจ๋“ˆ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ์ถ”๊ฐ€์ ์ธ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๊ด€๋ จ ์ฃผ์ œ๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์˜ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๋ฒ„์ „. >>> a = [1,2,3,4] >>> b = (2*x for x in a) >>> b <generator object at 0x58760> >>> for i in b: ... print(i, end=' ') ... 2 4 6 8 >>> ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜๊ณผ์˜ ์ฐจ์ด์ . ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค. ์œ ์ผํ•œ ์šฉ๋„๋Š” ์ดํ„ฐ๋ ˆ์ด์…˜์ด๋‹ค. ์†Œ๋น„๋˜๊ณ  ๋‚˜๋ฉด ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ์ผ๋ฐ˜์ ์ธ ๊ตฌ๋ฌธ. (<ํ‘œํ˜„์‹> for i in s if <์กฐ๊ฑด>) ํ•จ์ˆ˜ ์ธ์ž๋กœ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. sum(x*x for x in a) ๋ชจ๋“  ์ดํ„ฐ๋Ÿฌ๋ธ”์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. >>> a = [1,2,3,4] >>> b = (x*x for x in a) >>> c = (-x for x in b) >>> for i in c: ... print(i, end=' ') ... -1 -4 -9 -16 >>> ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹์˜<NAME>๋„๋Š” ์‹œํ€€์Šค์—์„œ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋˜ ๊ฒฐ๊ณผ๋ฅผ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํŒŒ์ผ์—์„œ ๋ชจ๋“  ์ฃผ์„์„ ์ œ๊ฑฐํ•œ๋‹ค. f = open('somefile.txt') lines = (line for line in f if not line.startswith('#')) for line in lines: ... f.close() ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋” ๋นจ๋ฆฌ ์ž‘๋™ํ•˜๋ฉฐ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค. ์ŠคํŠธ๋ฆผ์— ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  ๋งŽ์€ ํ”„๋กœ๊ทธ๋žจ์€ ์ดํ„ฐ๋ ˆ์ด์…˜์œผ๋กœ ๊น”๋”ํ•˜๊ฒŒ ํ‘œํ˜„๋œ๋‹ค. ํ•ญ๋ชฉ๋“ค์˜ ์ปฌ๋ ‰์…˜์„ ๋ฃจํ•‘ ํ•˜๋ฉฐ ๋ช‡ ๊ฐ€์ง€ ์—ฐ์‚ฐ(๊ฒ€์ƒ‰, ๊ต์ฒด, ์ˆ˜์ • ๋“ฑ)์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ๋” ๋„“์€ ๋ฒ”์œ„์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฌธ์ œ๊ฐ€ ์ ์šฉ๋œ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ํ•„์š”ํ•  ๋•Œ๋งŒ ๊ฐ’์„ ์ƒ์‚ฐํ•œ๋‹ค. ๊ฑฐ๋Œ€ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ๋Œ€๋น„๋œ๋‹ค. ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ ์ด‰์ง„ํ•œ๋‹ค ์ฝ”๋“œ์—์„œ ์ดํ„ฐ๋ ˆ์ด์…˜๊ณผ ์ดํ„ฐ๋ ˆ์ด์…˜ ์‚ฌ์šฉ์„ ๋ถ„๋ฆฌํ•œ๋‹ค. ํฅ๋ฏธ๋กœ์šด ์ดํ„ฐ๋ ˆ์ด์…˜ ํ•จ์ˆ˜์˜ ๋„๊ตฌ ๋ชจ์Œ์„ ๋งŒ๋“ค๊ณ  ๋ฏน์Šค ์•ค ๋งค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. itertools ๋ชจ๋“ˆ itertools๋Š” ์ดํ„ฐ ๋ ˆ์ดํ„ฐ/์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ๋•๋„๋ก ์„ค๊ณ„๋œ ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ์ด๋‹ค. itertools.chain(s1, s2) itertools.count(n) itertools.cycle(s) itertools.dropwhile(predicate, s) itertools.groupby(s) itertools.ifilter(predicate, s) itertools.imap(function, s1, ... sN) itertools.repeat(s, n) itertools.tee(s, ncopies) itertools.izip(s1, ... , sN) ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๋‹ค์–‘ํ•œ ์ดํ„ฐ๋ ˆ์ด์…˜ ํŒจํ„ด์„ ๊ตฌํ˜„ํ•œ๋‹ค. ์ž์„ธํ•œ ์ •๋ณด๋Š” 2008๋…„ ํŒŒ์ด์ฝ˜์˜ ์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋ฅผ ์œ„ํ•œ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํŠธ๋ฆญ ํŠœํ† ๋ฆฌ์–ผ์„ ์ฐธ์กฐํ•˜๋ผ. ์—ฐ์Šต ๋ฌธ์ œ ์ง€๋‚œ ์—ฐ์Šต ๋ฌธ์ œ์—์„œ, ๋กœ๊ทธ ํŒŒ์ผ์— ํ–‰์„ ๊ธฐ๋กํ•˜๊ณ  ํ–‰๋“ค์˜ ์‹œํ€€์Šค๋กœ ํŒŒ์‹ฑ ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ง€๋‚œ ๋ฌธ์ œ์™€ ์ด์–ด์ง„๋‹ค. Data/stocksim.py๊ฐ€ ๊ณ„์† ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธํ•˜์ž. ์—ฐ์Šต ๋ฌธ์ œ 6.13: ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹์€ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์˜ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๋ฒ„์ „์ด๋‹ค. ์˜ˆ: >>> nums = [1, 2, 3, 4, 5] >>> squares = (x*x for x in nums) >>> squares <generator object <genexpr> at 0x109207e60> >>> for n in squares: ... print(n) ... 4 16 25 ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹์€ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜๊ณผ ๋‹ฌ๋ฆฌ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ, ๋‹ค๋ฅธ for ๋ฃจํ”„๋ฅผ ์‹œ๋„ํ•˜๋ฉด ์•„๋ฌด๊ฒƒ๋„ ์–ป์ง€ ๋ชปํ•  ๊ฒƒ์ด๋‹ค. >>> for n in squares: ... print(n) ... >>> ์—ฐ์Šต ๋ฌธ์ œ 6.14: ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹์„ ํ•จ์ˆ˜ ์ธ์ž๋กœ ์‚ฌ์šฉ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹์„ ํ•จ์ˆ˜ ์ธ์ž๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์ฒ˜์Œ์—๋Š” ์ข€ ์ด์ƒํ•ด ๋ณด์ด๊ฒ ์ง€๋งŒ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด ๋ณด๋ผ. >>> nums = [1,2,3,4,5] >>> sum([x*x for x in nums]) # ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌ ํ—จ ์…˜ 55 >>> sum(x*x for x in nums) # ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹ 55 >>> ํฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ, ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ„์ „์ด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ›จ์”ฌ ์ ๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค. portfolio.py ํŒŒ์ผ์—์„œ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ–ˆ๋‹ค. ๊ทธ๊ฒƒ๋“ค์„ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹์œผ๋กœ ๋ฐ”๊ฟ”๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 6.15: ์ฝ”๋“œ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹์€ ์ž‘์€ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์–ด ์œ ์šฉํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. def filter_symbols(rows, names): for row in rows: if row['name'] in names: yield row ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. rows = (row for row in rows if row['name'] in names) ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋„๋ก ticker.py ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•˜์ž. 7. ๊ณ ๊ธ‰ ์ฃผ์ œ ์ด ์„น์…˜์—์„œ๋Š” ๊ณ ๊ธ‰ ์ฃผ์ œ ๊ฐ€์šด๋ฐ ์ผ์ƒ์ ์ธ ์ฝ”๋”ฉ์—์„œ ์ ‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ์‚ดํŽด๋ณธ๋‹ค. ์ด๋Ÿฌํ•œ ์ฃผ์ œ๋Š” ๋Œ€๋ถ€๋ถ„ ์ด์ „์˜ ์„น์…˜์—์„œ ๋‹ค๋ฃฐ ์ˆ˜๋„ ์žˆ์—ˆ์ง€๋งŒ, ์„ค๋ช…์ด ๋„ˆ๋ฌด ๋ณต์žกํ•ด์ง€๋Š” ๊ฒƒ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๋ฏธ๋ค„๋‘” ๊ฒƒ์ด๋‹ค. ์ด ์„น์…˜์€ ์ด๋Ÿฌํ•œ ๊ฐœ๋…์˜ ๊ทนํžˆ ๊ธฐ๋ณธ์ ์ธ ์†Œ๊ฐœ๋กœ์„œ ๋‹ค๋ฃฌ๋‹ค๋Š” ์ ์„ ๊ฐ•์กฐํ•ด์•ผ๊ฒ ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ž๋ฃŒ๋ฅผ ์ฐพ์•„๋ณด๋ผ. 7.1 ๊ฐ€๋ณ€ ์ธ์ž ํ•จ์ˆ˜ 7.2 ์ต๋ช… ํ•จ์ˆ˜์™€ ๋žŒ๋‹ค 7.3 ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ(ํด๋กœ์ €) 7.4 ํ•จ์ˆ˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ 7.5 ์ •์  ๋ฐ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ 7.1 ๊ฐ€๋ณ€ ์ธ์ž ์ด ์„น์…˜์€ *args์™€ **kwargs ๊ฐ™์€ ํ•จ์ˆ˜ ์ธ์ž๋ฅผ ๋‹ค๋ฃฌ๋‹ค. ์œ„์น˜ ๊ฐ€๋ณ€ ์ธ์ž(*args) ์ž„์˜์˜ ๊ฐœ์ˆ˜์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฐ€๋ฆฌ์ผœ ๊ฐ€๋ณ€ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ‘œํ˜„ํ•œ๋‹ค. ์˜ˆ: def f(x, *args): ... ํ•จ์ˆ˜ ํ˜ธ์ถœ. f(1,2,3,4,5) ์ถ”๊ฐ€์ ์ธ ์ธ์ž๋ฅผ ํŠœํ”Œ๋กœ ์ „๋‹ฌํ•œ๋‹ค. def f(x, *args): # x -> 1 # args -> (2,3,4,5) ํ‚ค์›Œ๋“œ ๊ฐ€๋ณ€ ์ธ์ž(**kwargs) ํ•จ์ˆ˜๋Š” ์ž„์˜์˜ ๊ฐœ์ˆ˜์˜ ํ‚ค์›Œ๋“œ ์ธ์ž๋„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: def f(x, y, **kwargs): ... ํ•จ์ˆ˜ ํ˜ธ์ถœ. f(2, 3, flag=True, mode='fast', header='debug') ์ถ”๊ฐ€์ ์ธ ํ‚ค์›Œ๋“œ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ „๋‹ฌํ•œ๋‹ค. def f(x, y, **kwargs): # x -> 2 # y -> 3 # kwargs -> { 'flag': True, 'mode': 'fast', 'header': 'debug' } ๋‘ ๊ฐ€์ง€๋ฅผ ํ˜ผํ•ฉ ํ•จ์ˆ˜๋Š” ์ž„์˜์˜ ๊ฐœ์ˆ˜์˜ ๊ฐ€๋ณ€ ์ธ์ž์™€ ํ‚ค์›Œ๋“œ ์—†๋Š”(non-keyword) ์ธ์ž๋“ค์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. def f(*args, **kwargs): ... ํ•จ์ˆ˜ ํ˜ธ์ถœ. f(2, 3, flag=True, mode='fast', header='debug') ์ธ์ž๋“ค์€ ์œ„์น˜ ๋ฐ ํ‚ค์›Œ๋“œ ์š”์†Œ๋กœ ๋ถ„ํ• ๋œ๋‹ค. def f(*args, **kwargs): # args = (2, 3) # kwargs -> { 'flag': True, 'mode': 'fast', 'header': 'debug' } ... ์ด ํ•จ์ˆ˜๋Š” ์œ„์น˜ ๋˜๋Š” ํ‚ค์›Œ๋“œ ์ธ์ž๋“ค์˜ ์–ด๋– ํ•œ ์กฐํ•ฉ์ด๋ผ๋„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ๋ž˜ํผ(wrapper)๋ฅผ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ ์ธ์ž๋ฅผ ๋‹ค๋ฅธ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ณค ํ•œ๋‹ค. ํŠœํ”Œ๊ณผ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ํŠœํ”Œ์„ ๊ฐ€๋ณ€ ์ธ์ž๋กœ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. numbers = (2,3,4) f(1, *numbers) # f(1,2,3,4)์™€ ๊ฐ™์Œ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ‚ค์›Œ๋“œ ์ธ์ž๋กœ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. options = { 'color' : 'red', 'delimiter' : ',', 'width' : 400 } f(data, **options) # f(data, color='red', delimiter=',', width=400)์™€ ๊ฐ™์Œ ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 7.1: ๊ฐ€๋ณ€ ์ธ์ž์˜ ๊ฐ„๋‹จํ•œ ์˜ˆ ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด์ž. >>> def avg(x,*more): return float(x+sum(more))/(1+len(more)) >>> avg(10,11) 10.5 >>> avg(3,4,5) 4.0 >>> avg(1,2,3,4,5,6) 3.5 >>> *more ๋งค๊ฐœ๋ณ€์ˆ˜(parameter)๊ฐ€ ์ถ”๊ฐ€์ ์ธ ์ธ์ž๋ฅผ ์–ด๋–ป๊ฒŒ ์ˆ˜์ง‘ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 7.2: ํŠœํ”Œ๊ณผ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ์ฝ์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠœํ”Œ์„ ์–ป์—ˆ๋‹ค๊ณ  ํ•˜์ž. >>> data = ('GOOG', 100, 490.1) >>> ์ด ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ Stock ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ ์ž ํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ data๋ฅผ ์ง์ ‘ ์ „๋‹ฌํ•˜๋ฉด ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. >>> from stock import Stock >>> s = Stock(data) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __init__() takes exactly 4 arguments (2 given) >>> ๊ทธ ๋Œ€์‹  *data๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํžˆ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด ๋ณด๋ผ. >>> s = Stock(*data) >>> s Stock('GOOG', 100, 490.1) >>> ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” **์„ ๋ถ™์ธ๋‹ค. ์˜ˆ: >>> data = { 'name': 'GOOG', 'shares': 100, 'price': 490.1 } >>> s = Stock(**data) Stock('GOOG', 100, 490.1) >>> ์—ฐ์Šต ๋ฌธ์ œ 7.3: ์ธ์Šคํ„ด์Šค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ report.py ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ธ์Šคํ„ด์Šค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค. def read_portfolio(filename): ''' ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค ํŒŒ์ผ์„ ์ฝ์–ด ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ. name, shares, price๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉ. ''' with open(filename) as lines: portdicts = fileparse.parse_csv(lines, select=['name','shares','price'], types=[str, int, float]) portfolio = [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ] return Portfolio(portfolio) Stock(**d)๋ฅผ ์‚ฌ์šฉํ•ด ์ฝ”๋“œ๋ฅผ ๋‹จ์ˆœํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋ฐ”๊ฟ”๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ 7.4: ์ธ์ž ํ†ต๊ณผ fileparse.parse_csv() ํ•จ์ˆ˜์—๋Š” ํŒŒ์ผ ๊ตฌ๋ถ„์ž๋ฅผ ๋ฐ”๊พธ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๋ฅผ ๋ณด๊ณ ํ•˜๋Š” ์˜ต์…˜์ด ์žˆ๋‹ค. ์œ„์˜ read_portfolio() ํ•จ์ˆ˜์— ๊ทธ๋Ÿฐ ๊ธฐ๋Šฅ์ด ์žˆ์œผ๋ฉด ์ข‹์„ ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊ฟ”๋ณด์ž. def read_portfolio(filename, **opts): ''' ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค ํŒŒ์ผ์„ ์ฝ์–ด ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ. name, shares, price๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉ. ''' with open(filename) as lines: portdicts = fileparse.parse_csv(lines, select=['name','shares','price'], types=[str, int, float], **opts) portfolio = [ Stock(**d) for d in portdicts ] return Portfolio(portfolio) ๋ณ€๊ฒฝ์„ ํ–ˆ์œผ๋ฉด, ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋Š” ํŒŒ์ผ์„ ์ฝ์–ด๋ณด์ž. >>> import report >>> port = report.read_portfolio('Data/missing.csv') Row 4: Couldn't convert ['MSFT', '', '51.23'] Row 4: Reason invalid literal for int() with base 10: '' Row 7: Couldn't convert ['IBM', '', '70.44'] Row 7: Reason invalid literal for int() with base 10: '' >>> ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๊ฒŒ ํ•ด ๋ณด์ž. >>> import report >>> port = report.read_portfolio('Data/missing.csv', silence_errors=True) >>> 7.2 ์ต๋ช… ํ•จ์ˆ˜์™€ ๋žŒ๋‹ค ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ ๋˜๋Œ์•„๋ณด๊ธฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ œ์ž๋ฆฌ์—์„œ(in-place) ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค. sort ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. s = [10,1,7,3] s.sort() # s = [1,3,7,10] ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค. s = [10,1,7,3] s.sort(reverse=True) # s = [10,7,3,1] ๊ฐ„๋‹จํ•ด ๋ณด์ธ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ •๋ ฌํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? [{'name': 'AA', 'price': 32.2, 'shares': 100}, {'name': 'IBM', 'price': 91.1, 'shares': 50}, {'name': 'CAT', 'price': 83.44, 'shares': 150}, {'name': 'MSFT', 'price': 51.23, 'shares': 200}, {'name': 'GE', 'price': 40.37, 'shares': 95}, {'name': 'MSFT', 'price': 65.1, 'shares': 50}, {'name': 'IBM', 'price': 70.44, 'shares': 100}] ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ? ํ‚ค(key) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ •๋ ฌ ๊ธฐ์ค€์„ ์•ˆ๋‚ดํ•  ์ˆ˜ ์žˆ๋‹ค. ํ‚ค ํ•จ์ˆ˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ›์•„์„œ ์ •๋ ฌ ๊ธฐ์ค€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. def stock_name(s): return s['name'] portfolio.sort(key=stock_name) ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. # ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ `name` ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋จ [ {'name': 'AA', 'price': 32.2, 'shares': 100}, {'name': 'CAT', 'price': 83.44, 'shares': 150}, {'name': 'GE', 'price': 40.37, 'shares': 95}, {'name': 'IBM', 'price': 91.1, 'shares': 50}, {'name': 'IBM', 'price': 70.44, 'shares': 100}, {'name': 'MSFT', 'price': 51.23, 'shares': 200}, {'name': 'MSFT', 'price': 65.1, 'shares': 50} ] ์ฝœ๋ฐฑ ํ•จ์ˆ˜(Callback Function) ์•ž์˜ ์˜ˆ์—์„œ ํ‚ค ํ•จ์ˆ˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ผ์ข…์ด๋‹ค. sort() ๋ฉ”์„œ๋“œ๋Š” ๋‹น์‹ ์ด ์ œ๊ณตํ•œ ํ•จ์ˆ˜๋ฅผ ์ฝœ๋ฐฑ ํ•œ๋‹ค. ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ์งง์€ ํ•œ ์ค„์งœ๋ฆฌ ํ•จ์ˆ˜๋กœ ์ž‘์„ฑํ•ด ๋‹จ ํ•˜๋‚˜์˜ ์˜คํผ๋ ˆ์ด์…˜๋งŒ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ์ข…์ข… ์ด๋Ÿฌํ•œ ์ถ”๊ฐ€์ ์ธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์ง€๋ฆ„๊ธธ์„ ์š”๊ตฌํ•œ๋‹ค. ๋žŒ๋‹ค(Lambda): ์ต๋ช… ํ•จ์ˆ˜ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋Œ€์‹  ๋žŒ๋‹ค๋ฅผ ์‚ฌ์šฉํ•˜์ž. ์•ž์˜ ์ •๋ ฌ ์˜ˆ์ œ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•ด ๋ณธ๋‹ค. portfolio.sort(key=lambda s: s['name']) ๋žŒ๋‹ค๋Š” ๋‹จ์ผ ํ‘œํ˜„์‹์„ ํ‰๊ฐ€ํ•˜๋Š” ์ด๋ฆ„ ์—†๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์œ„ ์ฝ”๋“œ๋Š” ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์—ˆ์„ ๋•Œ๋ณด๋‹ค ํ›จ์”ฌ ์งง๋‹ค. def stock_name(s): return s['name'] portfolio.sort(key=stock_name) # ๋žŒ๋‹ค์™€ ๋น„๊ต portfolio.sort(key=lambda s: s['name']) ๋žŒ๋‹ค ์‚ฌ์šฉํ•˜๊ธฐ ๋žŒ๋‹ค๋Š” ๋งค์šฐ ์ œํ•œ์ ์ด๋‹ค. ๋‹จ์ผ ํ‘œํ˜„์‹๋งŒ ํ—ˆ์šฉํ•œ๋‹ค. if, while ๊ฐ™์€ ๋ฌธ์žฅ์€ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. sort()์˜ ์˜ˆ์™€ ๊ฐ™์ด ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ์šฉ๋„๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜์ž. >>> import report >>> portfolio = list(report.read_portfolio('Data/portfolio.csv')) >>> for s in portfolio: print(s) Stock('AA', 100, 32.2) Stock('IBM', 50, 91.1) Stock('CAT', 150, 83.44) Stock('MSFT', 200, 51.23) Stock('GE', 95, 40.37) Stock('MSFT', 50, 65.1) Stock('IBM', 100, 70.44) >>> ์—ฐ์Šต ๋ฌธ์ œ 7.5: ํ•„๋“œ ์ •๋ ฌํ•˜๊ธฐ ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ๋ฅผ ์ข…๋ชฉ๋ช…์„ ๊ธฐ์ค€์œผ๋กœ ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ๋ณด์ž. >>> def stock_name(s): return s.name >>> portfolio.sort(key=stock_name) >>> for s in portfolio: print(s) ... ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๋ผ ... >>> ์œ„์˜ stock_name() ํ•จ์ˆ˜๋Š” portfolio ๋ฆฌ์ŠคํŠธ์˜ ๋‹จ์ผ ํ•ญ๋ชฉ์—์„œ ์ข…๋ชฉ๋ช…์„ ์ถ”์ถœํ•œ๋‹ค. sort()๋Š” ์ด ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•ด ์ •๋ ฌํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 7.6: ๋žŒ๋‹ค๋ฅผ ๊ฐ€์ง€๊ณ  ํ•„๋“œ ์ •๋ ฌํ•˜๊ธฐ lambda ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด ์ฃผ์‹ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์ •๋ ฌํ•ด ๋ณด์ž. >>> portfolio.sort(key=lambda s: s.shares) >>> for s in portfolio: print(s) ... ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๋ผ ... >>> ์ฃผ์‹ ๊ฐ€๊ฒฉ์„ ๊ธฐ์ค€์œผ๋กœ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์ •๋ ฌํ•ด ๋ณด์ž. >>> portfolio.sort(key=lambda s: s.price) >>> for s in portfolio: print(s) ... ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๋ผ ... >>> ์ฐธ๊ณ : lambda๋Š” ๋ณ„๋„์˜ ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ์ •์˜ํ•  ํ•„์š” ์—†์ด sort()๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ํŠน์ˆ˜ํ•œ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์–ด ์œ ์šฉํ•˜๋‹ค. 7.3 ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์ด ์„น์…˜์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์•„์ด๋””์–ด๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. ๋„์ž… ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. def add(x, y): def do_add(): print('Adding', x, y) return x + y return do_add add() ํ•จ์ˆ˜๋Š” ๋˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜์ธ do_add๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. >>> a = add(3,4) >>> a <function do_add at 0x6a670> >>> a() Adding 3 4 ๋กœ์ปฌ ๋ณ€์ˆ˜ ์™ธ๋ถ€ ํ•จ์ˆ˜๊ฐ€ ์ •์˜ํ•œ ๋ณ€์ˆ˜๋ฅผ ๋‚ด๋ถ€ ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ์ฐธ์กฐํ•˜๋Š”์ง€ ๊ด€์ฐฐํ•˜์ž. def add(x, y): def do_add(): # `x`์™€ `y`๋Š” `add(x, y)`์— ์ •์˜ print('Adding', x, y) return x + y return do_add ์ข€ ๋” ๊ด€์ฐฐํ•ด ๋ณด๋ฉด ์ด ๋ณ€์ˆ˜๋“ค์ด add()๊ฐ€ ์™„๋ฃŒ๋œ ํ›„์—๋„ ์‚ด์•„ ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. >>> a = add(3,4) >>> a <function do_add at 0x6a670> >>> a() Adding 3 4 # ์ด ๋ณ€์ˆ˜๋“ค์€ ์–ด๋””์„œ ์™”์„๊นŒ? ํด๋กœ์ € ๋‚ด๋ถ€ ํ•จ์ˆ˜๋ฅผ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•  ๋•Œ, ๊ทธ ๋‚ด๋ถ€ ํ•จ์ˆ˜๋ฅผ ํด๋กœ์ €(closure)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. def add(x, y): # `do_add`๋Š” ํด๋กœ์ €๋‹ค def do_add(): print('Adding', x, y) return x + y return do_add ํ•„์ˆ˜ ๊ธฐ๋Šฅ: ํด๋กœ์ €๋Š” ๋‚˜์ค‘์— ํ•จ์ˆ˜๊ฐ€ ์˜ฌ๋ฐ”๋กœ ์ž‘๋™ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“  ๋ณ€์ˆ˜์˜ ๊ฐ’์„<NAME>๋‹ค. ํด๋กœ์ €๋ฅผ ํ•จ์ˆ˜์™€ ๊ทธ ํ•จ์ˆ˜๊ฐ€ ์˜์กดํ•˜๋Š” ๋ณ€์ˆซ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ํ™˜๊ฒฝ์ด ํ•ฉ์ณ์ง„ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํด๋กœ์ € ์‚ฌ์šฉํ•˜๊ธฐ ํด๋กœ์ €๋Š” ํŒŒ์ด์ฌ์˜ ์ค‘์š” ๊ธฐ๋Šฅ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ทธ ์šฉ๋ฒ•์€ ๋ฏธ๋ฌ˜ํ•œ ๋ฐ๊ฐ€ ์žˆ๋‹ค. ๊ณตํ†ต์ ์ธ ์‘์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ์‚ฌ์šฉ. ์ง€์—ฐ๋œ ํ‰๊ฐ€. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜(๋’ค์—์„œ ์„ค๋ช…ํ•œ๋‹ค). ์ง€์—ฐ๋œ ํ‰๊ฐ€(Delayed Evaluation) ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. def after(seconds, func): time.sleep(seconds) func() ์šฉ๋ก€: def greeting(): print('Hello Guido') after(30, greeting) after๋Š” ์ œ๊ณต๋œ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค... ๋‚˜์ค‘์—. ํด๋กœ์ €๋Š” ์ถ”๊ฐ€์ ์ธ ์ •๋ณด๋ฅผ ๋ณด์œ ํ•œ๋‹ค. def add(x, y): def do_add(): print(f'Adding {x} + {y} -> {x+y}') return do_add def after(seconds, func): time.sleep(seconds) func() after(30, add(2, 3)) # `do_add`๋Š” ๋ ˆํผ๋Ÿฐ์Šค x -> 2์™€ y -> 3๋ฅผ ๊ฐ€์ง ์ฝ”๋“œ ๋ฐ˜๋ณต ๊ณผ๋„ํ•œ ์ฝ”๋“œ ๋ฐ˜๋ณต์„ ํ”ผํ•˜๋Š” ๊ธฐ๋ฒ•์œผ๋กœ์„œ ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 7.7: ๋ฐ˜๋ณต์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ํด๋กœ์ € ์‚ฌ์šฉํ•˜๊ธฐ ํด๋กœ์ €๋Š” ๋ฐ˜๋ณต์ ์ธ ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ๋Šฅ์ด ๋›ฐ์–ด๋‚˜๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 5.7์—์„œ ํƒ€์ž…์„ ๊ฒ€์‚ฌํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. class Stock: def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price ... @property def shares(self): return self._shares @shares.setter def shares(self, value): if not isinstance(value, int): raise TypeError('Expected int') self._shares = value ... ์ฝ”๋“œ๋ฅผ ์ผ์ผ์ด ํƒ€์ดํ•‘ํ•˜๋Š” ๋Œ€์‹ , ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•ด ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. typedproperty.py ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋„ฃ์ž. # typedproperty.py def typedproperty(name, expected_type): private_name = '_' + name @property def prop(self): return getattr(self, private_name) @prop.setter def prop(self, value): if not isinstance(value, expected_type): raise TypeError(f'Expected {expected_type}') setattr(self, private_name, value) return prop ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ด ๋ณด์ž. from typedproperty import typedproperty class Stock: name = typedproperty('name', str) shares = typedproperty('shares', int) price = typedproperty('price', float) def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ํƒ€์ž… ๊ฒ€์‚ฌ๊ฐ€ ์ž˜ ๋˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž. >>> s = Stock('IBM', 50, 91.1) >>> s.name 'IBM' >>> s.shares = '100' ... TypeError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค ... >>> ์—ฐ์Šต ๋ฌธ์ œ 7.8: ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ๋‹จ์ˆœํ™” ํžˆ๊ธฐ ์œ„์˜ ์˜ˆ์—์„œ typedproperty('shares', int)์™€ ๊ฐ™์ด ํƒ€์ดํ•‘ํ•˜์—ฌ ํ˜ธ์ถœํ•˜๊ธฐ๊ฐ€ ๊ท€์ฐฎ์•˜์„ ๊ฒƒ์ด๋‹ค. ๋ฐ˜๋ณต์ ์œผ๋กœ ํ•˜๋ ค๋ฉด ๋” ๋ถˆํŽธํ•˜๋‹ค. typedproperty.py ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ •์˜๋ฅผ ์ถ”๊ฐ€ํ•˜์ž. String = lambda name: typedproperty(name, str) Integer = lambda name: typedproperty(name, int) Float = lambda name: typedproperty(name, float) ์ด์ œ ์ด ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•˜๋„๋ก Stock ํด๋ž˜์Šค๋ฅผ ์žฌ์ž‘์„ฑํ•œ๋‹ค. class Stock: name = String('name') shares = Integer('shares') price = Float('price') def __init__(self, name, shares, price): self.name = name self.shares = shares self.price = price ์•„, ์ข€ ๋” ๋‚ซ๋‹ค. ์ด์™€ ๊ฐ™์ด ํด๋กœ์ €์™€ lambda๋ฅผ ์‚ฌ์šฉํ•ด ์ฝ”๋“œ๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๊ณ  ๊ฑฐ์Šฌ๋ฆฌ๋Š” ๋ฐ˜๋ณต์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ์ชฝ์ด ์ข‹์„ ๋•Œ๊ฐ€ ๋งŽ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 7.9: ์‹ค์ „์— ์‘์šฉ stock.py์˜ Stock ํด๋ž˜์Šค๋ฅผ ์žฌ์ž‘์„ฑํ•ด, ํƒ€์ž… ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•˜๋ผ. 7.4 ํ•จ์ˆ˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ด ์„น์…˜์€ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์˜ ๊ฐœ๋…์„ ์†Œ๊ฐœํ•œ๋‹ค. ์ด๊ฒƒ์€ ๊ณ ๊ธ‰ ์ฃผ์ œ์ด๋ฏ€๋กœ ์ด ์ฝ”์Šค์—์„œ๋Š” ๊ฒ‰ํ•ฅ๊ธฐ๋กœ ๋‹ค๋ฃฌ๋‹ค. ๋กœ๊น… ์˜ˆ์ œ ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. def add(x, y): return x + y ์ด ํ•จ์ˆ˜์— ๋กœ๊น…์„ ์ถ”๊ฐ€ํ•ด ๋ณด์ž. def add(x, y): print('Calling add') return x + y ์ด์ œ ๋‘ ๋ฒˆ์งธ ํ•จ์ˆ˜์—๋„ ๋กœ๊น…์„ ์ถ”๊ฐ€ํ•œ๋‹ค. def sub(x, y): print('Calling sub') return x - y ๊ด€์ฐฐ ๊ด€์ฐฐ: ์ฝ”๋“œ๊ฐ€ ๋ฐ˜๋ณต๋œ๋‹ค. ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์„ฑ๊ฐ€์‹  ์ผ์ด๋‹ค. ์ž‘์„ฑํ•˜๊ธฐ๋„ ๋ฒˆ๊ฑฐ๋กญ๊ณ  ์œ ์ง€ ๋ณด์ˆ˜๋„ ํž˜๋“ค๋‹ค. ํŠนํžˆ ์ž‘๋™์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค(์˜ˆ: ๋กœ๊น… ์ž‘๋™์„ ๋ฐ”๊พธ๊ธฐ). ๋กœ๊น…์„ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ ํ•จ์ˆ˜์— ๋กœ๊น…์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๋ž˜ํผ(wrapper) ๋ง์ด๋‹ค. def logged(func): def wrapper(*args, **kwargs): print('Calling', func.__name__) return func(*args, **kwargs) return wrapper ์ด์ œ ์‚ฌ์šฉํ•ด ๋ณด์ž. def add(x, y): return x + y logged_add = logged(add) logged๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๋Š”๊ฐ€? logged_add(3, 4) # ๋กœ๊น… ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค ์ด ์˜ˆ๋Š” ๋ž˜ํผ ํ•จ์ˆ˜(wrapper function)๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค€๋‹ค. ๋ž˜ํผ๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ๊ฐ์‹ธ๋Š” ํ•จ์ˆ˜๋‹ค. ์•ฝ๊ฐ„์˜ ์ฒ˜๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€๋งŒ, ๊ทธ ์™ธ์—๋Š” ์›๋ž˜ ํ•จ์ˆ˜์™€ ๋˜‘๊ฐ™์ด ์ž‘๋™ํ•œ๋‹ค. >>> logged_add(3, 4) Calling add # ๋ถ€๊ฐ€์ ์ธ ์ถœ๋ ฅ์ด ๋ž˜ํผ์— ์˜ํ•ด ์ถ”๊ฐ€๋จ >>> ์ฐธ๊ณ : logged() ํ•จ์ˆ˜๋Š” ๋ž˜ํผ๋ฅผ ์ƒ์„ฑํ•ด, ๊ทธ๊ฒƒ์„ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํŒŒ์ด์ฌ์—์„œ๋Š” ํ•จ์ˆ˜ ์ฃผ๋ณ€์— ๋ž˜ํผ๋ฅผ ๋‘๋Š” ์ผ์ด ๋งค์šฐ ํ”ํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ์•„์˜ˆ ์ „์šฉ ๊ตฌ๋ฌธ์ด ์žˆ๋‹ค. def add(x, y): return x + y add = logged(add) # ํŠน์ˆ˜ํ•œ ๊ตฌ๋ฌธ @logged def add(x, y): return x + y ์ด๋Ÿฌํ•œ ํŠน์ˆ˜ ๊ตฌ๋ฌธ์€ ์•ž์—์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์€ ์ผ์„ ํ•œ๋‹ค. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ์ƒˆ๋กœ์šด ๊ตฌ๋ฌธ์ด๋‹ค. ์ด๋ฅผ ๊ฐ€๋ฆฌ์ผœ ํ•จ์ˆ˜๋ฅผ ๋ฐ์ฝ” ๋ ˆ์ดํŠธ ํ•œ๋‹ค๊ณ (decorate) ํ•œ๋‹ค. ๋ถ€์—ฐ ์„ค๋ช… ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํžˆ ์‹œ์—ฐํ–ˆ์ง€๋งŒ, ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์™€ ๊ด€๋ จํ•ด ๋ฏธ๋ฌ˜ํ•œ ์„ธ๋ถ€์‚ฌํ•ญ์ด ๋งŽ๋‹ค. ํด๋ž˜์Šค์—์„œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์˜ˆ๋กœ ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋˜๋Š” ํ•จ์ˆ˜์— ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋ถ™์ด๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ๋Š” ํ•ด๋„, ์•ž์˜ ์˜ˆ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์ž˜ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ด‘๋ฒ”์œ„ํ•œ ํ•จ์ˆ˜ ์ •์˜์— ๋ฐ˜๋ณต์ ์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ฝ”๋“œ์— ๋Œ€์‘ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ์šฉ๋„๋‹ค. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ทธ๋Ÿฌํ•œ ์ฝ”๋“œ๋ฅผ ํ•œ๊ณณ์— ๋ชจ์•„๋‘˜ ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 7.10: ํƒ€์ด๋ฐ์„ ์œ„ํ•ด ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋ฉด, ๊ทธ ์ด๋ฆ„๊ณผ ๋ชจ๋“ˆ์ด __name__๊ณผ __module__ ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ์— ์ €์žฅ๋œ๋‹ค. ์˜ˆ: >>> def add(x, y): return x+y >>> add.__name__ 'add' >>> add.__module__ '__main__' >>> timethis.py ํŒŒ์ผ์—์„œ, ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์–ผ๋งˆ๋‚˜ ๊ฑธ๋ฆฌ๋Š”์ง€ ํ”„๋ฆฐํŠธํ•˜๋Š” ์ถ”๊ฐ€์ ์ธ ๋…ผ๋ฆฌ ๊ณ„์ธต์œผ๋กœ ๊ฐ์‹ธ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜ timethis(func)๋ฅผ ์ž‘์„ฑํ•˜์ž. ์ด๋ฅผ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํƒ€์ด๋ฐ ํ˜ธ์ถœ๋กœ ํ•จ์ˆ˜๋ฅผ ๊ฐ์‹ผ๋‹ค. start = time.time() r = func(*args,**kwargs) end = time.time() print('%s.%s: %f' % (func.__module__, func.__name__, end-start)) ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘๋™ํ•˜๊ฒŒ ํ•˜๋ผ. >>> from timethis import timethis >>> @timethis def countdown(n): while n > 0: n -= 1 >>> countdown(10000000) __main__.countdown : 0.076562 >>> ๋…ผ์˜: ์ด @timethis ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ์–ด๋Š ํ•จ์ˆ˜ ์ •์˜์˜ ์•ž์—๋“ ์ง€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์„ฑ๋Šฅ ํŠœ๋‹์„ ์œ„ํ•œ ์ง„๋‹จ ๋„๊ตฌ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 7.5 ๋ฐ์ฝ” ๋ ˆ์ดํŠธ ๋œ ๋ฉ”์„œ๋“œ ์ด ์„น์…˜์€ ๋ฉ”์„œ๋“œ ์ •์˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋นŒํŠธ์ธ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋ช‡ ๊ฐ€์ง€๋ฅผ ๋…ผ์˜ํ•œ๋‹ค. ๋ฏธ๋ฆฌ ์ •์˜๋œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํด๋ž˜์Šค ์ •์˜์— ํŠน์ˆ˜ํ•œ ๋ฉ”์„œ๋“œ ์œ ํ˜•์„ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ๋ฏธ๋ฆฌ ์ •์˜๋ผ์žˆ๋‹ค class Foo: def bar(self, a): ... @staticmethod def spam(a): ... @classmethod def grok(cls, a): ... @property def name(self): ... ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด์ž. ์ •์  ๋ฉ”์„œ๋“œ @staticmethod๋Š” ์ •์ (static) ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ(C++/Java์—์„œ ์œ ๋ž˜)๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. ์ •์  ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค์— ์†ํ•œ ํ•จ์ˆ˜์ด์ง€๋งŒ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด ์—ฐ์‚ฐ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. class Foo(object): @staticmethod def bar(x): print('x =', x) >>> Foo.bar(2) x=2 >>> ํด๋ž˜์Šค๋ฅผ ์œ„ํ•œ ๋‚ด๋ถ€ ์ง€์› ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ์ข…์ข… ์‚ฌ์šฉํ•œ๋‹ค. ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์˜ˆ๋กœ ๋“ค ์ˆ˜ ์žˆ๋‹ค(๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ, ์‹œ์Šคํ…œ ์ž์›, ์ง€์†์„ฑ, ์ž ๊ธˆ ๋“ฑ). ํŠน์ • ์„ค๊ณ„ ํŒจํ„ด์—์„œ๋„ ์‚ฌ์šฉํ•œ๋‹ค(์—ฌ๊ธฐ์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๋‹ค). ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ @classmethod๋Š” ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค ๋Œ€์‹  class ๊ฐ์ฒด๋ฅผ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค. class Foo: def bar(self): print(self) @classmethod def spam(cls): print(cls) >>> f = Foo() >>> f.bar() <__main__.Foo object at 0x971690> # ์ธ์Šคํ„ด์Šค `f` >>> Foo.spam() <class '__main__.Foo'> # `Foo` ํด๋ž˜์Šค >>> ์ƒ์„ฑ์ž(constructor)์˜ ๋Œ€์•ˆ์œผ๋กœ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์ข…์ข… ์‚ฌ์šฉํ•œ๋‹ค. class Date: def __init__(self, year, month, day): self.year = year self.month = month self.day = day @classmethod def today(cls): # ํด๋ž˜์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š”์ง€ ๋ณด๋ผ tm = time.localtime() # ์ƒˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋จ return cls(tm.tm_year, tm.tm_mon, tm.tm_mday) d = Date.today() ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋Š” ์ƒ์†๊ณผ ๊ฐ™์ด ๊นŒ๋‹ค๋กœ์šด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค. class Date: ... @classmethod def today(cls): # ์˜ฌ๋ฐ”๋ฅธ ํด๋ž˜์Šค๋ฅผ ์–ป์Œ(์˜ˆ: `NewDate`) tm = time.localtime() return cls(tm.tm_year, tm.tm_mon, tm.tm_mday) class NewDate(Date): ... d = NewDate.today() ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 7.11: ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ ์‹ค์Šต report.py์™€ portfolio.py ํŒŒ์ผ์—์„œ Portfolio ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ด ๋‹ค์†Œ ์• ๋งคํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, report.py ํ”„๋กœ๊ทธ๋žจ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค. def read_portfolio(filename, **opts): ''' ์ฃผ์‹ ํฌํŠธํด๋ฆฌ์˜ค ํŒŒ์ผ์„ ์ฝ์–ด ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ. name, shares, price๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉ. ''' with open(filename) as lines: portdicts = fileparse.parse_csv(lines, select=['name','shares','price'], types=[str, int, float], **opts) portfolio = [ Stock(**d) for d in portdicts ] return Portfolio(portfolio) ๊ทธ๋ฆฌ๊ณ  portfolio.py ํŒŒ์ผ์€ Portfolio()๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ, ์ดˆ๊ธฐํ™”๊ฐ€ ์•ฝ๊ฐ„ ์ด์ƒํ•˜๋‹ค. class Portfolio: def __init__(self, holdings): self.holdings = holdings ... ์†”์งํžˆ ์ฝ”๋“œ๊ฐ€ ์—ฌ๊ธฐ์ €๊ธฐ ํฉ์–ด์ ธ ์žˆ์–ด ์ฑ…์ž„ ๊ด€๊ณ„๊ฐ€ ํ—ท๊ฐˆ๋ฆฐ๋‹ค. Portfolio ํด๋ž˜์Šค์— Stock ์ธ์Šคํ„ด์Šค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‘˜ ๊ฒƒ์ด๋ผ๋ฉด, ํด๋ž˜์Šค๋ฅผ ์ข€ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ง์ด๋‹ค. # portfolio.py import stock class Portfolio: def __init__(self): self.holdings = [] def append(self, holding): if not isinstance(holding, stock.Stock): raise TypeError('Expected a Stock instance') self.holdings.append(holding) ... CSV ํŒŒ์ผ์—์„œ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์ฝ๊ณ  ์‹ถ๋‹ค๋ฉด, ๊ทธ๊ฒƒ์„ ์œ„ํ•œ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. # portfolio.py import fileparse import stock class Portfolio: def __init__(self): self.holdings = [] def append(self, holding): if not isinstance(holding, stock.Stock): raise TypeError('Expected a Stock instance') self.holdings.append(holding) @classmethod def from_csv(cls, lines, **opts): self = cls() portdicts = fileparse.parse_csv(lines, select=['name','shares','price'], types=[str, int, float], **opts) for d in portdicts: self.append(stock.Stock(**d)) return self ์ด ์ƒˆ๋กœ์šด ํฌํŠธํด๋ฆฌ์˜ค ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด, ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. >>> from portfolio import Portfolio >>> with open('Data/portfolio.csv') as lines: ... port = Portfolio.from_csv(lines) ... >>> Portfolio ํด๋ž˜์Šค์— ์ด๋Ÿฌํ•œ ๋ณ€๊ฒฝ์„ ๋ฐ˜์˜ํ•˜๊ณ  ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ report.py ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ผ. 8. ํ…Œ์ŠคํŒ…๊ณผ ๋””๋ฒ„๊น… ์ด ์ ˆ์€ ํ…Œ์ŠคํŒ…, ๋กœ๊น…, ๋””๋ฒ„๊น…๊ณผ ๊ด€๋ จํ•œ ์ƒˆ๋กœ์šด ๊ธฐ์ดˆ ์ฃผ์ œ๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. 8.1 ํ…Œ์ŠคํŒ… 8.2 ๋กœ๊น…, ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ๋ฐ ์ง„๋‹จ 8.3 ๋””๋ฒ„๊น… 8.1 ํ…Œ์ŠคํŒ… ๋””๋ฒ„๊น…๋ณด๋‹ค ํ…Œ์ŠคํŒ… ํŒŒ์ด์ฌ์€ ๋™์  ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋Œ€๋ถ€๋ถ„์— ์žˆ์–ด ํ…Œ์ŠคํŒ…์ด ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋กœ๋Š” ๋ฒ„๊ทธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค. ๋ฒ„๊ทธ๋ฅผ ์ฐพ๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์ ๊ฒ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. assert ๋ฌธ assert ๋ฌธ์€ ํ”„๋กœ๊ทธ๋žจ์˜ ๋‚ด๋ถ€ ์ ๊ฒ€์ด๋‹ค. ํ‘œํ˜„์‹์ด ์ฐธ์ด ์•„๋‹ˆ๋ฉด AssertionError ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. assert ๋ฌธ์˜ ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. assert <ํ‘œํ˜„์‹> [, '์ง„๋‹จ ๋ฉ”์‹œ์ง€'] ์˜ˆ: assert isinstance(10, int), 'Expected int' ์‚ฌ์šฉ์ž ์ž…๋ ฅ(์˜ˆ: ์›น ์–‘์‹ ๊ฐ™์€ ๊ฒƒ์„ ํ†ตํ•ด ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ)์„ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฐ assert๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. ๋‚ด๋ถ€์ ์ธ ์ ๊ฒ€๊ณผ ๋ถˆ๋ณ€ ์กฐ๊ฑด(ํ•ญ์ƒ ์ฐธ์ธ ์กฐ๊ฑด)์„ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋‹ค. ๊ณ„์•ฝ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Contract Programming) assert๋ฅผ ์•„๋‚Œ์—†์ด ํ™œ์šฉํ•˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด ์„ค๊ณ„ ์ ‘๊ทผ๋ฒ•์œผ๋กœ, ๊ณ„์•ฝ์— ์˜ํ•œ ์„ค๊ณ„(Design By Contract)๋ผ๊ณ ๋„ ํ•œ๋‹ค. ์†Œํ”„ํŠธ์›จ์–ด ์„ค๊ณ„์ž๊ฐ€ ์†Œํ”„ํŠธ์›จ์–ด ๊ตฌ์„ฑ์š”์†Œ์˜ ์ •ํ™•ํ•œ ์ธํ„ฐํŽ˜์ด์Šค ์‚ฌ์–‘์„ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๊ทœ์ •ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•จ์ˆ˜์˜ ๋ชจ๋“  ์ž…๋ ฅ์— assert๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. def add(x, y): assert isinstance(x, int), 'Expected int' assert isinstance(y, int), 'Expected int' return x + y ์ž…๋ ฅ๊ฐ’ ๊ฒ€์‚ฌ๋Š” ์ ์ ˆํ•œ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ํ˜ธ์ถœ์ž๋ฅผ ์ฆ‰์‹œ ์žก์•„๋‚ธ๋‹ค. >>> add(2, 3) >>> add('2', '3') Traceback (most recent call last): ... AssertionError: Expected int >>> ์ธ๋ผ์ธ ํ…Œ์ŠคํŠธ assert๋ฅผ ๊ฐ€์ง€๊ณ  ๋‹จ์ˆœํ•œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. def add(x, y): return x + y assert add(2,2) == 4 ์ด๋Ÿฐ ์‹์œผ๋กœ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ๊ฐ™์€ ๋ชจ๋“ˆ์— ํฌํ•จํ•œ๋‹ค. ์žฅ์ : ์ฝ”๋“œ๊ฐ€ ๋ช…๋ฐฑํžˆ ๋ง๊ฐ€์ง„ ๊ฒฝ์šฐ ์ž„ํฌํŠธ ์‹œ๋„ ์ž์ฒด๊ฐ€ ์‹คํŒจํ•œ๋‹ค. ํ…Œ์ŠคํŠธ๋ฅผ ์ œ๋Œ€๋กœ ํ•˜๋ ค๋ฉด ์ด ๋ฐฉ์‹๋งŒ์œผ๋กœ๋Š” ๋ถ€์กฑํ•˜๋‹ค. ์ด๊ฒƒ์€ ์‹ฌ๊ฐํ•œ ์˜ค๋ฅ˜๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ธฐ๋ณธ ์ ๊ฒ€(smoke test)์— ๋ถˆ๊ณผํ•˜๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋ชจ๋“  ์˜ˆ์ œ์—์„œ ์ž‘๋™ํ•˜๋Š”๊ฐ€? ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋ญ”๊ฐ€ ์ž˜๋ชป๋œ ๊ฒƒ์ด ๋ถ„๋ช…ํ•˜๋‹ค. unittest ๋ชจ๋“ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. # simple.py def add(x, y): return x + y ์ด์ œ ์ด๊ฒƒ์„ ํ…Œ์ŠคํŠธํ•œ๋‹ค๊ณ  ํ•˜์ž. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ…Œ์ŠคํŠธ ํŒŒ์ผ์„ ๋”ฐ๋กœ ์ƒ์„ฑํ•œ๋‹ค. # test_simple.py import simple import unittest ๊ทธ๋Ÿฐ ๋‹ค์Œ, ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค. # test_simple.py import simple import unittest # unittest.TestCase๋กœ๋ถ€ํ„ฐ ์ƒ์†ํ•จ์— ์œ ์˜ class TestAdd(unittest.TestCase): ... ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค๋Š” ๋ฐ˜๋“œ์‹œ unittest.TestCase๋กœ๋ถ€ํ„ฐ ์ƒ์†ํ•ด์•ผ ํ•œ๋‹ค. ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค์— ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค. # test_simple.py import simple import unittest # unittest.TestCase๋กœ๋ถ€ํ„ฐ ์ƒ์†ํ•จ์— ์œ ์˜ class TestAdd(unittest.TestCase): def test_simple(self): # ๋‹จ์ˆœํ•œ ์ •์ˆ˜ ์ธ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ํ…Œ์ŠคํŠธ r = simple.add(2, 2) self.assertEqual(r, 5) def test_str(self): # ๋ฌธ์ž์—ด์„ ๊ฐ€์ง€๊ณ  ํ…Œ์ŠคํŠธ r = simple.add('hello', 'world') self.assertEqual(r, 'helloworld') *์ค‘์š”: ๋ฉ”์„œ๋“œ ์ด๋ฆ„์ด test๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. unittest ์‚ฌ์šฉํ•˜๊ธฐ unittest ๋ชจ๋“ˆ์— ๋ช‡ ๊ฐ€์ง€ ๊ฒ€์‚ฌ ํ•จ์ˆ˜๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ๋“ค์–ด์žˆ๋‹ค. ๊ทธ๊ฒƒ๋“ค์€ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์ผ์„ ํ•œ๋‹ค. # ํ‘œํ˜„์ด ์ฐธ์ธ์ง€ ๊ฒ€์‚ฌ self.assertTrue(ํ‘œํ˜„) # x == y๋ฅผ ๊ฒ€์‚ฌ self.assertEqual(x, y) # x != y๋ฅผ ๊ฒ€์‚ฌ self.assertNotEqual(x, y) # x์™€ y๊ฐ€ ๋น„์Šทํ•œ์ง€ ๊ฒ€์‚ฌ self.assertAlmostEqual(x, y, ์ž๋ฆฟ์ˆ˜) # callable(์ธ์ž 1, ์ธ์ž 2, ...)์ด ์˜ˆ์™ธ๋ฅผ ์ผ์œผํ‚ค๋Š”์ง€ ๊ฒ€์‚ฌ self.assertRaises(์˜ˆ์™ธ, callable, ์ธ์ž 1, ์ธ์ž 2, ...) ์ด๊ฒƒ์ด ๋‹ค๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋ชจ๋“ˆ์— ๋‹ค๋ฅธ ๊ฒ€์‚ฌ๊ฐ€ ๋” ์žˆ๋‹ค. unittest ์‹คํ–‰ํ•˜๊ธฐ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์Šคํฌ๋ฆฝํŠธ๋กœ ๋ฐ”๊พธ์ž. # test_simple.py ... if __name__ == '__main__': unittest.main() ๊ทธ๋‹ค์Œ์— ํŒŒ์ด์ฌ์œผ๋กœ ํ…Œ์ŠคํŠธ ํŒŒ์ผ์„ ์‹คํ–‰ํ•œ๋‹ค. bash % python3 test_simple.py F. ======================================================== FAIL: test_simple (__main__.TestAdd) -------------------------------------------------------- Traceback (most recent call last): File "testsimple.py", line 8, in test_simple self.assertEqual(r, 5) AssertionError: 4 != 5 -------------------------------------------------------- Ran 2 tests in 0.000s FAILED (failures=1) ๋ถ€์—ฐ ์„ค๋ช… ํšจ๊ณผ์ ์ธ ํ…Œ์ŠคํŒ…์€ ์˜ˆ์ˆ ์— ๊ฐ€๊นŒ์šฐ๋ฉฐ, ๋Œ€๊ทœ๋ชจ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ๊ฝค ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ๋‹ค. unittest ๋ชจ๋“ˆ์€ ํ…Œ์ŠคํŠธ ์‹คํ–‰๊ณผ ๊ฒฐ๊ณผ ์ˆ˜์ง‘ ๋“ฑ ํ…Œ์ŠคํŒ…์˜ ์—ฌ๋Ÿฌ ์ธก๋ฉด์— ์œ ์šฉํ•œ ์˜ต์…˜์ด ์•„์ฃผ ๋งŽ๋‹ค. ์ž์„ธํ•œ ์‚ฌํ•ญ์€ ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜๋ผ. ์„œ๋“œ ํŒŒํ‹ฐ ํ…Œ์ŠคํŠธ ๋„๊ตฌ unittest ๋ชจ๋“ˆ์€ ํŒŒ์ด์ฌ์— ๋นŒํŠธ์ธ ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์–ด๋””์„œ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ, ๋„ˆ๋ฌด ์žฅํ™ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋งŽ๋‹ค. unittest ๋Œ€์‹  pytest๋„ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. pytest๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ…Œ์ŠคํŒ… ํŒŒ์ผ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹จ์ˆœํ•ด์ง„๋‹ค. # test_simple.py import simple def test_simple(): assert simple.add(2,2) == 4 def test_str(): assert simple.add('hello','world') == 'helloworld' ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋ช…๋ นํ–‰์—์„œ python -m pytest์™€ ๊ฐ™์ด ํƒ€์ดํ•‘ํ•˜๋ฉด ๋œ๋‹ค. ๋ชจ๋“  ํ…Œ์ŠคํŠธ๋ฅผ ์ฐพ์•„์„œ ์‹คํ–‰ํ•ด ์ค€๋‹ค. pytest ์˜ˆ์ œ๊ฐ€ ๋งŽ์ด ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๋‹ค. ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฌ์šฐ๋ฏ€๋กœ ํ•œ๋ฒˆ ์‹œ๋„ํ•ด ๋ณด๋ผ. ์—ฐ์Šต ๋ฌธ์ œ ์ด ์—ฐ์Šต ๋ฌธ์ œ์—์„œ๋Š” ํŒŒ์ด์ฌ unittest ๋ชจ๋“ˆ์˜ ๊ธฐ๋ณธ ์ž‘๋™ ์›๋ฆฌ๋ฅผ ํƒ๊ตฌํ•œ๋‹ค. ์•ž์˜ ์—ฐ์Šต ๋ฌธ์ œ์—์„œ Stock ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š” stock.py๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ์ด ์—ฐ์Šต ๋ฌธ์ œ๋Š” ํƒ€์ž… ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ์™€ ๊ด€๋ จ๋œ ์—ฐ์Šต ๋ฌธ์ œ 7.9๋ฅผ ํ’€์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ๋งŒ์•ฝ ์–ด๋–ค ์ด์œ ๋กœ๋“  ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด Solutions/7_9์˜ ํ•ด๋‹ต ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 8.1: ์œ ๋‹› ํ…Œ์ŠคํŠธ ์ž‘์„ฑํ•˜๊ธฐ Stock ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ test_stock.py ํŒŒ์ผ์— ๋”ฐ๋กœ ์ž‘์„ฑํ•œ๋‹ค. ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์„ ๊ฒ€์‚ฌํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. # test_stock.py import unittest import stock class TestStock(unittest.TestCase): def test_create(self): s = stock.Stock('GOOG', 100, 490.1) self.assertEqual(s.name, 'GOOG') self.assertEqual(s.shares, 100) self.assertEqual(s.price, 490.1) if __name__ == '__main__': unittest.main() ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜์ž. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋œ๋‹ค. ---------------------------------------------------------------------- Ran 1 tests in 0.000s OK ์ด๊ฒƒ์ด ์ž˜ ์ž‘๋™ํ•˜๋ฉด, ๋‹ค์Œ ์‚ฌํ•ญ์„ ์ ๊ฒ€ํ•˜๋Š” ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€๋กœ ์ž‘์„ฑํ•œ๋‹ค. s.cost ํ”„๋กœํผํ‹ฐ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’ (49010.0)์„ ๋ฐ˜ํ™˜ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. s.sell() ๋ฉ”์„œ๋“œ๊ฐ€ ์˜ฌ๋ฐ”๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. s.shares ๊ฐ’์ด ๊ฐ์†Œํ•ด์•ผ ํ•œ๋‹ค. s.shares ์–ด ํŠธ๋ฆฌ๋ทฐํŠธ์— ์ •์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฐ’์ด ์„ค์ •๋˜์ง€ ์•Š์Œ์„ ํ™•์ธํ•œ๋‹ค. ๋์œผ๋กœ, ์˜ˆ์™ธ๊ฐ€ ์ผ์–ด๋‚˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•˜๋ฉด ์‰ฝ๋‹ค. class TestStock(unittest.TestCase): ... def test_bad_shares(self): s = stock.Stock('GOOG', 100, 490.1) with self.assertRaises(TypeError): s.shares = '100' 8.2 ๋กœ๊น… ์ด ์„น์…˜์—์„œ๋Š” logging ๋ชจ๋“ˆ์„ ๊ฐ„๋‹จํžˆ ์†Œ๊ฐœํ•œ๋‹ค. logging ๋ชจ๋“ˆ logging ๋ชจ๋“ˆ์€ ์ง„๋‹จ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•œ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ์ด๋‹ค. ์ •๊ตํ•œ ๊ธฐ๋Šฅ์ด ๋งŽ์€, ๋งค์šฐ ํฐ ๋ชจ๋“ˆ์ด๊ธฐ๋„ ํ•˜๋‹ค. ์œ ์šฉํ•œ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์ž. ์˜ˆ์™ธ ๋˜๋Œ์•„๋ณด๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ parse() ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. # fileparse.py def parse(f, types=None, names=None, delimiter=None): records = [] for line in f: line = line.strip() if not line: continue try: records.append(split(line, types, names, delimiter)) except ValueError as e: print("Couldn't parse :", line) print("Reason :", e) return records try-except ๋ฌธ์— ์ง‘์ค‘ํ•˜์ž. except ๋ธ”๋ก์—์„œ ๋ฌด์Šจ ์ผ์„ ํ•ด์•ผ ํ• ๊นŒ? ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋‚˜? try: records.append(split(line, types, names, delimiter)) except ValueError as e: print("Couldn't parse :", line) print("Reason :", e) ์•„๋‹ˆ๋ฉด ์กฐ์šฉํžˆ ๋ฌด์‹œํ•ด์•ผ ํ•˜๋‚˜? try: records.append(split(line, types, names, delimiter)) except ValueError as e: pass ๋‘ ๊ฐ€์ง€ ํ–‰์œ„๋ฅผ ๋ชจ๋‘ ์›ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ(์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒ) ์–ด๋Š ํ•ด๊ฒฐ์ฑ…๋„ ๋งŒ์กฑ์Šค๋Ÿฝ์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. logging ๋ชจ๋“ˆ ์‚ฌ์šฉํ•˜๊ธฐ logging ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. # fileparse.py import logging log = logging.getLogger(__name__) def parse(f, types=None, names=None, delimiter=None): ... try: records.append(split(line, types, names, delimiter)) except ValueError as e: log.warning("Couldn't parse : %s", line) log.debug("Reason : %s", e) ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€ ๋˜๋Š” ํŠน์ˆ˜ํ•œ Logger ๊ฐ์ฒด๋ฅผ ๋ฐœํ–‰ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค. logging.getLogger(__name__)์œผ๋กœ ์ƒ์„ฑํ•œ ๊ฒƒ์ด๋‹ค. ๋กœ๊น… ๊ธฐ์ดˆ ๋กœ ๊ฑฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. log = logging.getLogger(name) # name์€ ๋ฌธ์ž์—ด์ด๋‹ค ๋กœ๊ทธ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐœํ–‰ํ•œ๋‹ค. log.critical(message [, args]) log.error(message [, args]) log.warning(message [, args]) log.info(message [, args]) log.debug(message [, args]) ๊ฐ ๋ฉ”์‹œ์ง€๋Š” ์‹ฌ๊ฐ๋„(severity) ์ˆ˜์ค€์ด ๊ฐ๊ธฐ ๋‹ค๋ฅด๋‹ค. ์ด๊ฒƒ๋“ค์€ ๋ชจ๋‘ ํฌ๋งคํŒ…๋œ ๋กœ๊ทธ ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด args๋ฅผ % ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค. logmsg = message % args # ๋กœ๊ทธ์— ๊ธฐ๋ก๋จ ๋กœ๊น… ์„ค์ • ๋กœ๊น… ์ž‘๋™์„ ๊ฐ๊ฐ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. # main.py ... if __name__ == '__main__': import logging logging.basicConfig( filename = 'app.log', # Log output file level = logging.INFO, # Output level ) ์ผ๋ฐ˜์ ์œผ๋กœ, ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘ํ•  ๋•Œ ํ•œ ๋ฒˆ๋งŒ ์„ค์ •ํ•œ๋‹ค. ์„ค์ •์€ logging์„ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ์™€ ๋ถ„๋ฆฌ๋œ๋‹ค. ๋ถ€์—ฐ ์„ค๋ช… ๋กœ๊น…์€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ํญ์ด ๋„“๋‹ค. ์ถœ๋ ฅ ํŒŒ์ผ, ์ˆ˜์ค€, ๋ฉ”์‹œ์ง€ ํฌ๋งท ๋“ฑ ๋ชจ๋“  ๊ฒƒ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ, logging์„ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋Š” ๊ทธ๋Ÿฐ ๊ฒƒ์— ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 8.2: ๋ชจ๋“ˆ์— logging ์ถ”๊ฐ€ํ•˜๊ธฐ fileparse.py์—๋Š” ์ž˜๋ชป๋œ ์ž…๋ ฅ์— ์˜ํ•ด ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ์™€ ๊ด€๋ จํ•ด ๋ช‡ ๊ฐ€์ง€ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๊ฐ€ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. # fileparse.py import csv def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', silence_errors=False): ''' CSV ํŒŒ์ผ์„ ํŒŒ์‹ฑ ๋ฐ ํ˜• ๋ณ€ํ™˜ํ•˜์—ฌ ๋ ˆ์ฝ”๋“œ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ. ''' if select and not has_headers: raise RuntimeError('select requires column headers') rows = csv.reader(lines, delimiter=delimiter) # ํŒŒ์ผ ํ—ค๋”๊ฐ€ ์žˆ์œผ๋ฉด ์ฝ์Œ headers = next(rows) if has_headers else [] # ํŠน์ • ์นผ๋Ÿผ์„ ์„ ํƒํ•œ ๊ฒฝ์šฐ, ํ•„ํ„ฐ๋ง์„ ์œ„ํ•œ ์ธ๋ฑ์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์ถœ๋ ฅ ์นผ๋Ÿผ์„ ์„ค์ • if select: indices = [ headers.index(colname) for colname in select ] headers = select records = [] for rowno, row in enumerate(rows, 1): if not row: # ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ํ–‰์„ ๊ฑด๋„ˆ๋œ€ continue # ํŠน์ • ์นผ๋Ÿผ ์ธ๋ฑ์Šค๊ฐ€ ์„ ํƒ๋˜์—ˆ์œผ๋ฉด ๊ทธ๊ฒƒ์„ ๊ณ ๋ฆ„ if select: row = [ row[index] for index in indices] # ํ–‰์— ํ˜• ๋ณ€ํ™˜์„ ์ ์šฉ if types: try: row = [func(val) for func, val in zip(types, row)] except ValueError as e: if not silence_errors: print(f"Row {rowno}: Couldn't convert {row}") print(f"Row {rowno}: Reason {e}") continue # ํŠœํ”Œ์˜ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ฆ if headers: record = dict(zip(headers, row)) else: record = tuple(row) records.append(record) return records ์ง„๋‹จ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐœํ–‰ํ•˜๋Š” print ๋ฌธ์— ์œ ์˜ํ•˜๋ผ. ์ด print๋ฅผ logging ์˜คํผ๋ ˆ์ด์…˜์œผ๋กœ ๊ต์ฒดํ•˜๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ•œ ํŽธ์ด๋‹ค. ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊พธ์ž. # fileparse.py import csv import logging log = logging.getLogger(__name__) def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', silence_errors=False): ''' CSV ํŒŒ์ผ์„ ํŒŒ์‹ฑ ๋ฐ ํ˜• ๋ณ€ํ™˜ํ•˜์—ฌ ๋ ˆ์ฝ”๋“œ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑ. ''' if select and not has_headers: raise RuntimeError('select requires column headers') rows = csv.reader(lines, delimiter=delimiter) # ํŒŒ์ผ ํ—ค๋”๊ฐ€ ์žˆ์œผ๋ฉด ์ฝ์Œ headers = next(rows) if has_headers else [] # ํŠน์ • ์นผ๋Ÿผ์„ ์„ ํƒํ•œ ๊ฒฝ์šฐ, ํ•„ํ„ฐ๋ง์„ ์œ„ํ•œ ์ธ๋ฑ์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์ถœ๋ ฅ ์นผ๋Ÿผ์„ ์„ค์ • if select: indices = [ headers.index(colname) for colname in select ] headers = select records = [] for rowno, row in enumerate(rows, 1): if not row: # ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ํ–‰์„ ๊ฑด๋„ˆ๋œ€ continue # ํŠน์ • ์นผ๋Ÿผ ์ธ๋ฑ์Šค๊ฐ€ ์„ ํƒ๋˜์—ˆ์œผ๋ฉด ๊ทธ๊ฒƒ์„ ๊ณ ๋ฆ„ if select: row = [ row[index] for index in indices] # ํ–‰์— ํ˜• ๋ณ€ํ™˜์„ ์ ์šฉ if types: try: row = [func(val) for func, val in zip(types, row)] except ValueError as e: if not silence_errors: log.warning("Row %d: Couldn't convert %s", rowno, row) log.debug("Row %d: Reason %s", rowno, e) continue # ํŠœํ”Œ์˜ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ฆ if headers: record = dict(zip(headers, row)) else: record = tuple(row) records.append(record) return records ์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ–ˆ์œผ๋ฏ€๋กœ, ์ž˜๋ชป๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ด ๋ณด์ž. >>> import report >>> a = report.read_portfolio('Data/missing.csv') Row 4: Bad row: ['MSFT', '', '51.23'] Row 7: Bad row: ['IBM', '', '70.44'] >>> ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ๋Š” ๊ฒฝ๊ณ (WARNING) ์ˆ˜์ค€ ์ด์ƒ์˜ ๋กœ๊น… ๋ฉ”์‹œ์ง€๋งŒ ์ถœ๋ ฅ๋œ๋‹ค. ์ถœ๋ ฅ์€ ๋‹จ์ˆœํ•œ print ๋ฌธ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ logging ๋ชจ๋“ˆ์„ ์„ค์ •ํ•˜๋Š” ๊ฒฝ์šฐ ๋กœ๊น… ์ˆ˜์ค€, ๋ชจ๋“ˆ ๋“ฑ์˜ ์ถ”๊ฐ€์ ์ธ ์ •๋ณด๊ฐ€ ์ œ๊ณต๋œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํƒ€์ดํ•‘ํ•˜์—ฌ ํ™•์ธํ•ด ๋ณด์ž. >>> import logging >>> logging.basicConfig() >>> a = report.read_portfolio('Data/missing.csv') WARNING:fileparse:Row 4: Bad row: ['MSFT', '', '51.23'] WARNING:fileparse:Row 7: Bad row: ['IBM', '', '70.44'] >>> log.debug()์˜ ์ถœ๋ ฅ์€ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š”๋‹ค. ๋””๋ฒ„๊ทธ(DEBUG) ๋ฉ”์‹œ์ง€๋„ ๋‚˜ํƒ€๋‚˜๋„๋ก ์ˆ˜์ค€์„ ๋ณ€๊ฒฝํ•ด ๋ณด์ž. >>> logging.getLogger('fileparse').level = logging.DEBUG >>> a = report.read_portfolio('Data/missing.csv') WARNING:fileparse:Row 4: Bad row: ['MSFT', '', '51.23'] DEBUG:fileparse:Row 4: Reason: invalid literal for int() with base 10: '' WARNING:fileparse:Row 7: Bad row: ['IBM', '', '70.44'] DEBUG:fileparse:Row 7: Reason: invalid literal for int() with base 10: '' >>> ์‹ฌ๊ฐ(CRITICAL) ๋ฉ”์‹œ์ง€๋งŒ ๋‚˜ํƒ€๋‚˜๊ฒŒ ํ•ด ๋ณด์ž. >>> logging.getLogger('fileparse').level=logging.CRITICAL >>> a = report.read_portfolio('Data/missing.csv') >>> ์—ฐ์Šต ๋ฌธ์ œ 8.3: ํ”„๋กœ๊ทธ๋žจ์— ๋กœ๊น…์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋กœ๊น…์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ๋ฉ”์ธ ๋ชจ๋“ˆ์—์„œ ๋กœ๊น… ๋ชจ๋“ˆ์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ํ•„์š”ํ•˜๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์…‹์—… ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค. # ์ด ํŒŒ์ผ์€ logging ๋ชจ๋“ˆ์˜ ๊ธฐ๋ณธ ๊ตฌ์„ฑ์„ ์„ค์ •ํ•œ๋‹ค. # ์ด ์„ค์ •์„ ๋ณ€๊ฒฝํ•จ์œผ๋กœ์จ ๋กœ๊น… ์ถœ๋ ฅ์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. import logging logging.basicConfig( filename = 'app.log', # ๋กœ๊ทธ ํŒŒ์ผ์˜ ์ด๋ฆ„(์ƒ๋žตํ•˜๋ฉด stderr์„ ์‚ฌ์šฉ) filemode = 'w', # ํŒŒ์ผ ๋ชจ๋“œ('a': ์ถ”๊ฐ€) # ๋กœ๊น… ์ˆ˜์ค€(DEBUG, INFO, WARNING, ERROR, CRITICAL) level = logging.WARNING, ) ๋‹น์‹ ์˜ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹œ์ž‘ ๋‹จ๊ณ„์— ์ด๊ฒƒ์„ ๋‘์–ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, report.py ํ”„๋กœ๊ทธ๋žจ์˜ ์–ด๋””์— ๋‘์–ด์•ผ ํ• ๊นŒ? 8.3 ๋””๋ฒ„๊น… ํ”„๋กœ๊ทธ๋žจ์ด ์ถฉ๋Œํ–ˆ๋‹ค... bash % python3 blah.py Traceback (most recent call last): File "blah.py", line 13, in ? foo() File "blah.py", line 10, in foo bar() File "blah.py", line 7, in bar spam() File "blah.py", 4, in spam line x.append(3) AttributeError: 'int' object has no attribute 'append' ์–ด๋–กํ•˜์ง€?! ํŠธ๋ ˆ์ด์Šค ๋ฐฑ ์ฝ๊ธฐ ๋งˆ์ง€๋ง‰ ํ–‰์ด ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚จ ์ฃผ๋ฒ”์ด๋‹ค. bash % python3 blah.py Traceback (most recent call last): File "blah.py", line 13, in ? foo() File "blah.py", line 10, in foo bar() File "blah.py", line 7, in bar spam() File "blah.py", 4, in spam line x.append(3) # ์ถฉ๋Œ ์›์ธ AttributeError: 'int' object has no attribute 'append' ๊ทธ๋ ‡์ง€๋งŒ ์ฝ๊ณ  ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์‰ฝ์ง€๋งŒ์€ ์•Š๋‹ค. ์ „๋ฌธ๊ฐ€์˜ ์กฐ์–ธ: ํŠธ๋ ˆ์ด์Šค ๋ฐฑ ์ „์ฒด๋ฅผ ๊ตฌ๊ธ€์— ๋ถ™์—ฌ ๋„ฃ์–ด ๊ฒ€์ƒ‰ํ•˜๋ผ. REPL ์‚ฌ์šฉํ•˜๊ธฐ -i ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํŒŒ์ด์ฌ์ด ์‚ด์•„์žˆ๋„๋ก ํ•œ๋‹ค. bash % python3 -i blah.py Traceback (most recent call last): File "blah.py", line 13, in ? foo() File "blah.py", line 10, in foo bar() File "blah.py", line 7, in bar spam() File "blah.py", 4, in spam line x.append(3) AttributeError: 'int' object has no attribute 'append' >>> ์ด๊ฒƒ์€ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์ƒํƒœ๋ฅผ<NAME>๋‹ค. ์ถฉ๋Œ ์ดํ›„์— ์—ฌ๊ธฐ์ €๊ธฐ ์ฐ”๋Ÿฌ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ณ€์ˆซ๊ฐ’๊ณผ ๊ธฐํƒ€ ์ƒํƒœ๋ฅผ ํ™•์ธํ•œ๋‹ค. ํ”„๋ฆฐํŠธ๋ฅผ ์‚ฌ์šฉํ•œ ๋””๋ฒ„๊น… print() ๋””๋ฒ„๊น…์€ ๊ฝค ์ผ๋ฐ˜์ ์ด๋‹ค. ์กฐ์–ธ: repr()์„ ์‚ฌ์šฉํ•˜๋ผ. def spam(x): print('DEBUG:', repr(x)) ... repr() ์€ ๊ฐ’์˜ ์ •ํ™•ํ•œ ํ‘œํ˜„(representation)์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ถœ๋ ฅ์„ ์˜ˆ์˜๊ฒŒ ํ”„๋ฆฐํŠธํ•˜๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ค๋ฅด๋‹ค. >>> from decimal import Decimal >>> x = Decimal('3.4') # `repr` ์—†์ด >>> print(x) 3.4 # `repr` ์‚ฌ์šฉ >>> print(repr(x)) Decimal('3.4') >>> ํŒŒ์ด์ฌ ๋””๋ฒ„๊ฑฐ ์ด ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋””๋ฒ„๊ฑฐ๋ฅผ ์ˆ˜์ž‘์—…์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. def some_function(): ... breakpoint() # ๋””๋ฒ„๊ฑฐ ์ง„์ž…(ํŒŒ์ด์ฌ 3.7 ์ด์ƒ) ... ์ด๊ฒƒ์€ breakpoint() ํ˜ธ์ถœ์—์„œ ๋””๋ฒ„๊ฑฐ๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค. ์ด์ „ ๋ฒ„์ „์—์„œ๋Š” ๋ฐฉ๋ฒ•์ด ์•ฝ๊ฐ„ ๋‹ค๋ฅด๋‹ค. ๋‹ค๋ฅธ ๋””๋ฒ„๊น… ์•ˆ๋‚ด์„œ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. import pdb ... pdb.set_trace() # `breakpoint()` ๋Œ€์‹  ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ผ ... ๋””๋ฒ„๊ฑฐ์—์„œ ์‹คํ–‰ํ•˜๊ธฐ ์ „์ฒด ํ”„๋กœ๊ทธ๋žจ์„ ๋””๋ฒ„๊ฑฐ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. bash % python3 -m pdb someprogram.py ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ ์ด์ „์— ๋””๋ฒ„๊ฑฐ์— ์ž๋™์œผ๋กœ ์ง„์ž…ํ•œ๋‹ค. ์ค‘๋‹จ์ (breakpoint)์„ ์„ค์ •ํ•˜๊ณ  ๊ตฌ์„ฑ์„ ๋ฐ”๊ฟ”๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๋””๋ฒ„๊ฑฐ ๋ช…๋ น: (Pdb) help # ๋„์›€๋ง (Pdb) w(here) # ์Šคํƒ ํŠธ๋ ˆ์ด์Šค(stack trace)๋ฅผ ํ”„๋ฆฐํŠธ (Pdb) d(own) # ํ•œ ์Šคํƒ ์ˆ˜์ค€ ์•„๋ž˜๋กœ ์ด๋™ (Pdb) u(p) # ํ•œ ์Šคํƒ ์ˆ˜์ค€ ์œ„๋กœ ์ด๋™ (Pdb) b(reak) loc # ์ค‘๋‹จ์  ์„ค์ • (Pdb) s(tep) # ํ•œ ์ธ์ŠคํŠธ๋Ÿญ์…˜(instruction)์„ ์‹คํ–‰ (Pdb) c(ontinue) # ๊ณ„์† ์‹คํ–‰ (Pdb) l(ist) # ์†Œ์Šค ์ฝ”๋“œ ๋ณด๊ธฐ (Pdb) a(rgs) # ํ˜„์žฌ ํ•จ์ˆ˜์˜ ์ธ์ž๋ฅผ ํ”„๋ฆฐํŠธ (Pdb) !statement # ๋ฌธ์žฅ(statement)์„ ์‹คํ–‰ ์ค‘๋‹จ์  ์œ„์น˜๋Š” ๋‹ค์Œ ์ค‘ ํ•˜๋‚˜๋‹ค. (Pdb) b 45 # ํ˜„์žฌ ํŒŒ์ผ์˜ 45ํ–‰ (Pdb) b file.py:34 # file.py์˜ 34ํ–‰ (Pdb) b foo # ํ˜„์žฌ ํŒŒ์ผ์˜ foo() ํ•จ์ˆ˜ (Pdb) b module.foo # module์˜ foo() ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 8.4: ๋ฒ„๊ทธ? ๋ฌด์Šจ ๋ฒ„๊ทธ? ์ž‘๋™ํ•œ๋‹ค. ์ถœ์‹œํ•ด๋ผ! 9. ํŒจํ‚ค์ง€ ์ด ๊ณผ์ •์˜ ๋งˆ์ง€๋ง‰ ์„น์…˜์€ ์ฝ”๋“œ๋ฅผ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ๋กœ ์กฐ์งํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋ช‡ ๊ฐ€์ง€ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ์•Œ์•„๋ณธ๋‹ค. ์„œ๋“œ ํŒŒํ‹ฐ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์—ฌ๋Ÿฌ๋ถ„์ด ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•ด ์ œ๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๋…ผ์˜ํ•œ๋‹ค. ํŒจํ‚ค์ง•์€ ๊ณ„์† ์ง„ํ™”ํ•˜๋ฉฐ, ํŒŒ์ด์ฌ ๊ฐœ๋ฐœ์— ์žˆ์–ด ๋„ˆ๋ฌด ๋ณต์žกํ•œ ์ฃผ์ œ๋‹ค. ํŠน์ • ๋„๊ตฌ์— ์ดˆ์ ์„ ๋งž์ถ”๊ธฐ๋ณด๋‹ค๋Š”, ์ฝ”๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜ ์˜์กด์„ฑ์„ ๊ด€๋ฆฌํ•จ์— ์žˆ์–ด ์–ด๋–ค ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ผ๋ฐ˜์ ์ธ ์ฝ”๋“œ ์กฐ์งํ™” ์›์น™์— ์ง‘์ค‘ํ•œ๋‹ค. 9.1 ํŒจํ‚ค์ง€ 9.2 ์„œ๋“œ ํŒŒํ‹ฐ ๋ชจ๋“ˆ 9.3 ์ฝ”๋“œ๋ฅผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์—๊ฒŒ ์ฃผ๊ธฐ 9.1 ํŒจํ‚ค์ง€ ๋” ํฐ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•œ๋‹ค๋ฉด, ๋…๋ฆฝ์ ์ธ ํŒŒ์ผ์˜ ๊ฑฐ๋Œ€ํ•œ ๋ชจ์Œ์„ ์ตœ์ƒ์œ„ ์ˆ˜์ค€์—์„œ ์กฐ์งํ™”ํ•˜๋Š” ๊ฒƒ์€ ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š๋‹ค. ์ด ์„น์…˜์€ ํŒจํ‚ค์ง€์˜ ๊ฐœ๋…์„ ์†Œ๊ฐœํ•œ๋‹ค. ๋ชจ๋“ˆ(Module) ๋ชจ๋“  ํŒŒ์ด์ฌ ์†Œ์Šค ํŒŒ์ผ์€ ๋ชจ๋“ˆ์ด๋‹ค. # foo.py def grok(a): ... def spam(b): ... import ๋ฌธ์€ ๋ชจ๋“ˆ์„ ์ ์žฌํ•˜๊ณ  ์‹คํ–‰ํ•œ๋‹ค. # program.py import foo a = foo.grok(2) b = foo.spam('Hello') ... ํŒจํ‚ค์ง€ vs ๋ชจ๋“ˆ ๋” ํฐ ์ฝ”๋“œ ๋ชจ์Œ์—์„œ๋Š” ๋ชจ๋“ˆ์„ ํŒจํ‚ค์ง€๋กœ ์กฐ์งํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. # ์ด๊ฒƒ์„ pcost.py report.py fileparse.py # ์ด๋ ‡๊ฒŒ porty/ __init__.py pcost.py report.py fileparse.py ์ ๋‹นํ•œ ์ด๋ฆ„์„ ๊ณจ๋ผ ์ตœ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด๋ผ. ์œ„์˜ ์˜ˆ์—์„œ๋Š” porty๋กœ ์ •ํ–ˆ๋‹ค(์ด๋ฆ„์„ ์ž˜ ๊ณ ๋ฅด๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ฒซ๊ฑธ์Œ์ด๋‹ค). __init__.py ํŒŒ์ผ์„ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ถ”๊ฐ€ํ•œ๋‹ค. ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ๋น„์–ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์†Œ์Šค ํŒŒ์ผ์„ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋„ฃ์–ด๋ผ. ํŒจํ‚ค์ง€ ์‚ฌ์šฉํ•˜๊ธฐ ํŒจํ‚ค์ง€๋Š” ์ž„ํฌํŠธ๋ฅผ ์œ„ํ•œ ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์—ญํ• ์„ ํ•œ๋‹ค. ์ฆ‰, ์—ฌ๋Ÿฌ ์ˆ˜์ค€์˜ ์ž„ํฌํŠธ๊ฐ€ ์กด์žฌํ•œ๋‹ค. import porty.report port = porty.report.read_portfolio('port.csv') import ๋ฌธ์—๋Š” ์—ฌ๋Ÿฌ ๋ณ€ํ˜•์ด ์žˆ๋‹ค. from porty import report port = report.read_portfolio('portfolio.csv') from porty.report import read_portfolio port = read_portfolio('portfolio.csv') ๋‘ ๊ฐ€์ง€ ๋ฌธ์ œ์  ์ด๋Ÿฌํ•œ ์ ‘๊ทผ์€ ๋‘ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ๊ฐ™์€ ํŒจํ‚ค์ง€์—์„œ ์—ฌ๋Ÿฌ ํŒŒ์ผ์„ ์ž„ํฌํŠธ ํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค. ํŒจํ‚ค์ง€์— ์žˆ๋Š” ๋ฉ”์ธ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๊นจ์ง„๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋“  ๊ฒƒ์ด ๊นจ์ง„๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๊ทธ ์™ธ์—๋Š” ์ž‘๋™ํ•œ๋‹ค. ๋ฌธ์ œ: ์ž„ํฌํŠธ ๊ฐ™์€ ํŒจํ‚ค์ง€์—์„œ ํŒŒ์ผ๋“ค์„ ์ž„ํฌํŠธ ํ•˜๋ ค๋ฉด ์ž„ํฌํŠธ์— ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ํฌํ•จํ•ด์•ผ ํ•œ๋‹ค. ๊ตฌ์กฐ๋ฅผ ๊ธฐ์–ตํ•˜์ž. porty/ __init__.py pcost.py report.py fileparse.py ์ˆ˜์ •ํ•œ ์ž„ํฌํŠธ ์˜ˆ. # report.py from porty import fileparse def read_portfolio(filename): return fileparse.parse_csv(...) ๋ชจ๋“  ์ž„ํฌํŠธ๋Š” ์ƒ๋Œ€๊ฐ€ ์•„๋‹Œ ์ ˆ๋Œ€ ์ž„ํฌํŠธ๋‹ค. # report.py import fileparse # ๊นจ์ง„๋‹ค. fileparse์„ ์ฐพ์ง€ ๋ชปํ•œ๋‹ค. ... ์ƒ๋Œ€ ์ž„ํฌํŠธ ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ , ํ˜„์žฌ ํŒจํ‚ค์ง€๋ฅผ.๋กœ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. # report.py from . import fileparse def read_portfolio(filename): return fileparse.parse_csv(...) ๊ตฌ๋ฌธ: from . import modname ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ๋ฐ”๊พธ๊ธฐ ์‰ฝ๋‹ค. ๋ฌธ์ œ์ : ๋ฉ”์ธ ์Šคํฌ๋ฆฝํŠธ ํŒจํ‚ค์ง€ ์„œ๋ธŒ ๋ชจ๋“ˆ์„ ๋ฉ”์ธ ์Šคํฌ๋ฆฝํŠธ๋กœ์„œ ์‹คํ–‰ํ•˜๋ฉด ๊นจ์ง„๋‹ค. bash $ python porty/pcost.py # ๊นจ์ง ... ์ด์œ : ํŒŒ์ด์ฌ์„ ๋‹จ์ผ ํŒŒ์ผ์—์„œ ์‹คํ–‰ํ•˜๋Š”๋ฐ, ํŒŒ์ด์ฌ์€ ๋‚˜๋จธ์ง€ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ๋ฅผ ์˜ฌ๋ฐ”๋กœ ๋ณผ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค(sys.path๊ฐ€ ์ž˜๋ชป๋จ). ๋ชจ๋“  ์ž„ํฌํŠธ๊ฐ€ ๊นจ์ง„๋‹ค. ์ด๊ฒƒ์„ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•  ๋•Œ -m ์˜ต์…˜์„ ์‚ฌ์šฉํ•œ๋‹ค. bash $ python -m porty.pcost # ์ž‘๋™ํ•จ ... __init__.py ํŒŒ์ผ ์ด ํŒŒ์ผ์˜ ์ฃผ๋ชฉ์ ์€ ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์„ ํ•œ๋ฐ ๋ชจ์œผ๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ: ํ•จ์ˆ˜๋“ค์„ ๊ฒฐ์†์‹œํ‚ค๊ธฐ # porty/__init__.py from .pcost import portfolio_cost from .report import portfolio_report ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ž„ํฌํŠธ ํ•  ๋•Œ ์ด๋ฆ„์ด ์ตœ์ƒ์œ„ ์ˆ˜์ค€์— ๋‚˜ํƒ€๋‚œ๋‹ค. from porty import portfolio_cost portfolio_cost('portfolio.csv') ๋‹ค์ค‘ ์ˆ˜์ค€ ์ž„ํฌํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ . from porty import pcost pcost.portfolio_cost('portfolio.csv') ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์œ„ํ•œ ์„ค๋ฃจ์…˜ ์ด์ œ ํŒจํ‚ค์ง€์—์„œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด -m package.module์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. bash % python3 -m porty.pcost portfolio.csv ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค. ์ƒˆ๋กœ์šด ์ตœ์ƒ์œ„ ์ˆ˜์ค€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. #!/usr/bin/env python3 # pcost.py import porty.pcost import sys porty.pcost.main(sys.argv) ์ด ์Šคํฌ๋ฆฝํŠธ๋Š” ํŒจํ‚ค์ง€ ๋ฐ”๊นฅ์— ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋ณด์ž. pcost.py # ์ตœ์ƒ์œ„ ์ˆ˜์ค€ ์Šคํฌ๋ฆฝํŠธ porty/ # ํŒจํ‚ค์ง€ ๋””๋ ‰ํ„ฐ๋ฆฌ __init__.py pcost.py ... ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ตฌ์กฐ ์ฝ”๋“œ์™€ ํŒŒ์ผ์˜ ๊ตฌ์กฐํ™”๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์œ ์ง€ ๋ณด์ˆ˜์„ฑ์˜ ํ•ต์‹ฌ์ด๋‹ค. ํŒŒ์ด์ฌ์—์„œ ๋ชจ๋“  ๋ฌธ์ œ์— ๋งž๋Š” ๋‹จ ํ•œ ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ์ง€๋Š” ์•Š๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ์— ์ ํ•ฉํ•˜๋‹ค. porty-app/ README.txt script.py # SCRIPT porty/ # ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ฝ”๋“œ __init__.py pcost.py report.py fileparse.py ์ตœ์ƒ์œ„ ์ˆ˜์ค€์˜ porty-app์€ ๋ฌธ์„œ, ์ตœ์ƒ์œ„ ์ˆ˜์ค€ ์Šคํฌ๋ฆฝํŠธ, ์˜ˆ์ œ ๋“ฑ ๋ชจ๋“  ๊ฒƒ์„ ๋‹ด๋Š”๋‹ค. ์žฌ์ฐจ ๋งํ•˜์ง€๋งŒ, ์ตœ์ƒ์œ„ ์ˆ˜์ค€ ์Šคํฌ๋ฆฝํŠธ๋Š” ์ฝ”๋“œ ํŒจํ‚ค์ง€ ์™ธ๋ถ€์— ๋‘์–ด์•ผ ํ•œ๋‹ค. ํ•œ ๋‹จ๊ณ„ ๋†’์ด์ž. #!/usr/bin/env python3 # porty-app/script.py import sys import porty porty.report.main(sys.argv) ์—ฐ์Šต ๋ฌธ์ œ ์ด์ œ ๋””๋ ‰ํ„ฐ๋ฆฌ์—๋Š” ํ”„๋กœ๊ทธ๋žจ ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์žˆ๋‹ค. pcost.py # ํฌํŠธํด๋ฆฌ์˜ค ๋น„์šฉ ๊ณ„์‚ฐ report.py # ๋ณด๊ณ ์„œ ์ž‘์„ฑ ticker.py # ์‹ค์‹œ๊ฐ„ ์ฃผ์‹ ์‹œ์„ธ ์ƒ์„ฑ ๊ทธ ์™ธ์˜ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋Š” ๋ชจ๋“ˆ๋„ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค. stock.py # Stock ํด๋ž˜์Šค portfolio.py # Portfolio ํด๋ž˜์Šค fileparse.py # CSV ํŒŒ์‹ฑ tableformat.py # ํ‘œ ์ž‘์„ฑ follow.py # ๋กœ๊ทธ ํŒŒ์ผ ์ถ”์  typedproperty.py # ํƒ€์ž… ์žˆ๋Š” ํด๋ž˜์Šค ํ”„๋กœํผํ‹ฐ ์—ฐ์Šต ๋ฌธ์ œ์—์„œ๋Š” ์ฝ”๋“œ๋ฅผ ์ •๋ฆฌํ•ด ๊ณตํ†ต ํŒจํ‚ค์ง€์— ๋„ฃ๋Š”๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ 9.1: ๋‹จ์ˆœํ•œ ํŒจํ‚ค์ง€ ๋งŒ๋“ค๊ธฐ porty/๋ผ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ  ์œ„์˜ ํŒŒ์ด์ฌ ํŒŒ์ผ๋“ค์„ ๋ชจ๋‘ ์ง‘์–ด๋„ฃ์–ด๋ผ. ๊ทธ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋นˆ __init__.py ํŒŒ์ผ๋„ ๋งŒ๋“ค์–ด๋ผ. ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒŒ์ผ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. porty/ __init__.py fileparse.py follow.py pcost.py portfolio.py report.py stock.py tableformat.py ticker.py typedproperty.py ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” __pycache__๋Š” ์‚ญ์ œํ•œ๋‹ค. ์•ž์„œ ํ”„๋ฆฌ ์ปดํŒŒ์ผํ•œ ํŒŒ์ด์ฌ ๋ชจ๋“ˆ์ด ๋“ค์–ด ์žˆ๋‹ค.<NAME>๊ณ  ์ƒˆ๋กœ ์‹œ์ž‘ํ•˜์ž. ํŒจํ‚ค์ง€ ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•ด๋ณด์ž. >>> import porty.report >>> import porty.pcost >>> import porty.ticker ์ž„ํฌํŠธ์— ์‹คํŒจํ•œ๋‹ค๋ฉด ํ•ด๋‹น ํŒŒ์ผ์˜ ๋ชจ๋“ˆ ์ž„ํฌํŠธ์— ํŒจํ‚ค์ง€ ์ƒ๋Œ€ ์ž„ํฌํŠธ๋ฅผ ํฌํ•จํ•˜๊ฒŒ ์ˆ˜์ •ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, import fileparse๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊พผ๋‹ค. # report.py from . import fileparse ... from fileparse import parse_csv ๊ฐ™์€ ๋ฌธ์žฅ์ด ๋ณด์ด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•œ๋‹ค. # report.py from .fileparse import parse_csv ... ์—ฐ์Šต ๋ฌธ์ œ 9.2: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋งŒ๋“ค๊ธฐ ์ฝ”๋“œ๋ฅผ ๋ชจ๋‘ 'ํŒจํ‚ค์ง€'์— ๋„ฃ๊ธฐ๋งŒ ํ•œ๋‹ค๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์ง€์› ํŒŒ์ผ, ๋ฌธ์„œ, ์Šคํฌ๋ฆฝํŠธ, ๊ทธ ์™ธ์˜ ๊ฒƒ๋“ค์ด ํ•„์š”ํ•˜๋‹ค. ์ด๋Ÿฌํ•œ ํŒŒ์ผ๋“ค์€ ์œ„์—์„œ ๋งŒ๋“  porty/ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋ฐ”๊นฅ์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. porty-app์ด๋ผ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์ž. ์—ฐ์Šต ๋ฌธ์ œ 9.1์—์„œ ์ƒ์„ฑํ•œ porty ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ทธ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ์˜ฎ๊ธด๋‹ค. Data/portfolio.csv์™€ Data/prices.csv ํ…Œ์ŠคํŠธ ํŒŒ์ผ์„ ์ด ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๋ณต์‚ฌํ•œ๋‹ค. ๋‹น์‹ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด์€ README.txt ํŒŒ์ผ๋„ ์ถ”๊ฐ€๋กœ ์ž‘์„ฑํ•œ๋‹ค. ์ด์ œ ์ฝ”๋“œ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋˜์–ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. porty-app/ portfolio.csv prices.csv README.txt porty/ __init__.py fileparse.py follow.py pcost.py portfolio.py report.py stock.py tableformat.py ticker.py typedproperty.py ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์ตœ์ƒ์œ„ porty-app/ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์ž‘์—…ํ•ด์•ผ ํ•œ๋‹ค. ํ„ฐ๋ฏธ๋„์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•œ๋‹ค. shell % cd porty-app shell % python3 >>> import porty.report >>> ์•ž์—์„œ ๋งŒ๋“  ์Šคํฌ๋ฆฝํŠธ๋“ค์„ ๋ฉ”์ธ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ์„œ ์‹คํ–‰ํ•ด ๋ณด์ž. shell % cd porty-app shell % python3 -m porty.report portfolio.csv prices.csv txt Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 shell % ์—ฐ์Šต ๋ฌธ์ œ 9.3: ์ตœ์ƒ์œ„ ์Šคํฌ๋ฆฝํŠธ python -m ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ข€ ์ด์ƒํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ํŒจํ‚ค์ง€์˜ ํŠน์„ฑ์„ ๋‹ค๋ฃจ๋Š” ์ตœ์ƒ์œ„ ์ˆ˜์ค€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์œ„์˜ ๋ณด๊ณ ์„œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” print-report.py ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. #!/usr/bin/env python3 # print-report.py import sys from porty.report import main main(sys.argv) ์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ตœ์ƒ์œ„ ์ˆ˜์ค€์˜ porty-app/ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋„ฃ๋Š”๋‹ค. ํ•ด๋‹น ์œ„์น˜์—์„œ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค. shell % cd porty-app shell % python3 print-report.py portfolio.csv prices.csv txt Name Shares Price Change ---------- ---------- ---------- ---------- AA 100 9.22 -22.98 IBM 50 106.28 15.18 CAT 150 35.46 -47.98 MSFT 200 20.89 -30.34 GE 95 13.48 -26.89 MSFT 50 20.89 -44.21 IBM 100 106.28 35.84 shell % ์ตœ์ข… ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๊ฐ€ ๋œ๋‹ค. porty-app/ portfolio.csv prices.csv print-report.py README.txt porty/ __init__.py fileparse.py follow.py pcost.py portfolio.py report.py stock.py tableformat.py ticker.py typedproperty.py 9.2 ์„œ๋“œ ํŒŒํ‹ฐ ๋ชจ๋“ˆ ํŒŒ์ด์ฌ์—๋Š” ๋นŒํŠธ์ธ ๋ชจ๋“ˆ์˜ ๋ฐฉ๋Œ€ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ๋‹ค(์ „์ž์ œํ’ˆ์— ๋ฐฐํ„ฐ๋ฆฌ๋ฅผ ๋ผ์›Œ ํŒŒ๋Š” ๊ฒƒ์— ๋น„์œ ํ•ด batteries included๋ผ๊ณ  ํ‘œํ˜„ํ•œ๋‹ค). ๊ฒŒ๋‹ค๊ฐ€ ์„œ๋“œ ํŒŒํ‹ฐ ๋ชจ๋“ˆ๋„ ์•„์ฃผ ๋งŽ๋‹ค. ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€ ์ธ๋ฑ์Šค(PyPi: Python Package Index)๋ฅผ ๋‘˜๋Ÿฌ๋ณด๋ผ. ์•„๋‹ˆ๋ฉด ํŠน์ • ์ฃผ์ œ๋ฅผ ๊ตฌ๊ธ€์—์„œ ๊ฒ€์ƒ‰ํ•ด๋„ ๋œ๋‹ค. ์„œ๋“œ ํŒŒํ‹ฐ ์˜์กด์„ฑ์„ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฃฐ ๊ฒƒ์ธ์ง€๊ฐ€ ํŒŒ์ด์ฌ์—์„œ ๋Š์ž„์—†์ด ์ง„ํ™”ํ•˜๋Š” ์ฃผ์ œ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ๊ธฐ๋ณธ ์›๋ฆฌ๋ฅผ ์†Œ๊ฐœํ•˜๋Š” ์„ ์—์„œ ๋‹ค๋ฃฌ๋‹ค. ๋ชจ๋“ˆ ๊ฒ€์ƒ‰ ๊ฒฝ๋กœ sys.path๋Š” ๋ชจ๋“  ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋ชฉ๋ก์„ ํฌํ•จํ•˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ, import ๋ฌธ์—์„œ ์ด๊ฒƒ์„ ํ™•์ธํ•˜๋‹ค. ํ•œ๋ฒˆ ์‚ดํŽด๋ณด์ž. >>> import sys >>> sys.path ... ๊ฒฐ๊ณผ๋ฅผ ์‚ดํŽด๋ณด๋ผ ... >>> ๋งŒ์•ฝ ์–ด๋–ค ๊ฒƒ์„ ์ž„ํฌํŠธ ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ๊ทธ๊ฒƒ์„ ์ด ๋””๋ ‰ํ„ฐ๋ฆฌ ์ค‘ ์–ด๋””์—์„œ๋„ ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด ImportError ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ ํŒŒ์ด์ฌ์˜ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์†ํ•œ ๋ชจ๋“ˆ์€ ๋ณดํ†ต /usr/local/lib/python3.6๊ณผ ๊ฐ™์€ ๊ณณ์— ์žˆ๋‹ค. ๊ฐ„๋‹จํ•œ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. >>> import re >>> re <module 're' from '/usr/local/lib/python3.6/re.py'> >>> REPL์—์„œ ๋ชจ๋“ˆ์„ ์ฐพ์•„๋ณด๊ธฐ๋งŒ ํ•ด๋„ ๋””๋ฒ„๊น…์— ๋„์›€์ด ๋˜๋Š” ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์€ ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. ์„œ๋“œ ํŒŒํ‹ฐ ๋ชจ๋“ˆ ์„œ๋“œ ํŒŒํ‹ฐ ๋ชจ๋“ˆ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ „์šฉ site-packages ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋‹ค. ์•ž์—์„œ ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ฐพ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค. >>> import numpy <module 'numpy' from '/usr/local/lib/python3.6/site-packages/numpy/__init__.py'> >>> import์™€ ๊ด€๋ จํ•ด ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋ฅผ ์ฐพ์œผ๋ ค๊ณ  ํ•  ๋•Œ, ๋ชจ๋“ˆ์„ ๋“ค์—ฌ๋‹ค๋ณด๋Š” ๊ฒƒ์€ ์ข‹์€ ๋””๋ฒ„๊น… ๋ฐฉ๋ฒ•์ด๋‹ค. ๋ชจ๋“ˆ ์„ค์น˜ํ•˜๊ธฐ ์„œ๋“œ ํŒŒํ‹ฐ ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•˜๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์€ pip๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ: bash % python3 -m pip install packagename ์ด ๋ช…๋ น์€ ํŒจํ‚ค์ง€๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด site-packages ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์„ค์น˜ํ•œ๋‹ค. ๋ฌธ์ œ์  ๋‹น์‹ ์ด ์ง์ ‘ ์ œ์–ดํ•˜์ง€ ์•Š๋Š” ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉ ์ค‘์ผ ์ˆ˜ ์žˆ๋‹ค. ํšŒ์‚ฌ ์Šน์ธ ์„ค์น˜ OS์— ํฌํ•จ๋œ ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ. ๋‹น์‹ ์€ ์ปดํ“จํ„ฐ์— ๊ธ€๋กœ๋ฒŒ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•  ๊ถŒํ•œ์ด ์—†์„ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋ฅธ ์˜์กด์„ฑ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€์ƒ ํ™˜๊ฒฝ(Virtual Environment) ํŒจํ‚ค์ง€ ์„ค์น˜ ์ด์Šˆ์˜ ์ผ๋ฐ˜์ ์ธ ํ•ด๋ฒ•์€ ์ž์‹ ์„ ์œ„ํ•œ "๊ฐ€์ƒ ํ™˜๊ฒฝ"์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์— '์™•๋„'๋Š” ์—†์œผ๋ฉฐ, ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์ด ๊ฒฝ์Ÿํ•œ๋‹ค. ํ•˜์ง€๋งŒ, ํ‘œ์ค€ ํŒŒ์ด์ฌ ์„ค์น˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋‹ค์Œ์˜ ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ•  ์ˆ˜ ์žˆ๋‹ค. bash % python -m venv mypython bash % ์ž ์‹œ ๊ธฐ๋‹ค๋ฆฌ๋ฉด mypython์ด๋ผ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค. ์ด๊ฒƒ์„ ๋‹น์‹ ๋งŒ์˜ ์ž‘์€ ํŒŒ์ด์ฌ ์„ค์น˜๋ณธ์ด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—์„œ bin/ ๋””๋ ‰ํ„ฐ๋ฆฌ(์œ ๋‹‰์Šค) ๋˜๋Š” Scripts/ ๋””๋ ‰ํ„ฐ๋ฆฌ(์œˆ๋„)๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ๊ฑฐ๊ธฐ ์žˆ๋Š” activate ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด, ํ•ด๋‹น ํŒŒ์ด์ฌ ๋ฒ„์ „์ด 'ํ™œ์„ฑํ™”(activate)'๋˜์–ด, ์…ธ์˜ ๋””ํดํŠธ python ๋ช…๋ น์ด ๋œ๋‹ค. ์˜ˆ: bash % source mypython/bin/activate (mypython) bash % ์ด๊ณณ์— ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ: (mypython) bash % python -m pip install pandas ... ์—ฌ๋Ÿฌ ํŒจํ‚ค์ง€๋ฅผ ์‹คํ—˜ํ•˜๊ณ  ์‚ฌ์šฉํ•ด ๋ณด๊ณ  ์‹ถ์„ ๋•Œ ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค. ํ•œํŽธ, ๊ฐœ๋ฐœํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํŠน์ • ํŒจํ‚ค์ง€ ์˜์กด์„ฑ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋Š” ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์„œ๋“œ ํŒŒํ‹ฐ ์˜์กด์„ฑ์„ ๋‹ค๋ฃจ๊ธฐ ๋งŒ์•ฝ ๋‹น์‹ ์ด ์ž‘์„ฑํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ํŠน์ • ์„œ๋“œ ํŒŒํ‹ฐ์— ์˜์กด์„ฑ์ด ์žˆ์œผ๋ฉด, ๋‹น์‹ ์˜ ์ฝ”๋“œ์™€ ์˜์กด์„ฑ์„ ํฌํ•จํ•˜๋Š” ํ™˜๊ฒฝ์„ ์ƒ์„ฑํ•˜๊ณ  ๋ณด์กดํ•˜๋Š” ๋ฐ ์–ด๋ ค์›€์ด ์žˆ๋‹ค. ์•ˆํƒ€๊น๊ฒŒ๋„, ํŒŒ์ด์ฌ์—์„œ ์ด ์˜์—ญ์€ ๋งค์šฐ ํ˜ผ๋ž€์Šค๋Ÿฝ๊ณ  ๋นˆ๋ฒˆํ•œ ๋ณ€ํ™”๊ฐ€ ์žˆ๋‹ค. ์ง€๊ธˆ๋„ ๊ณ„์† ์ง„ํ™”ํ•˜๊ณ  ์žˆ๋‹ค. ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋”๋ผ๋„ ๊ธˆ์„ธ ๊ตฌ์‹์ด ๋˜์–ด๋ฒ„๋ฆฌ๋ฏ€๋กœ, ํŒŒ์ด์ฌ ํŒจํ‚ค์ง• ์‚ฌ์šฉ์ž ์•ˆ๋‚ด์„œ(Python Packaging User Guide)๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ๋ฅผ ๊ถŒํ•œ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 9.4 : ๊ฐ€์ƒ ํ™˜๊ฒฝ์— ์„ค์น˜ ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค์–ด, ์œ„์—์„œ ์„ค๋ช…ํ•œ ํŒ๋‹ค์Šค ์„ค์น˜ ๊ณผ์ •์„ ์žฌํ˜„ํ•ด ๋ณด๋ผ. 9.3 ๋ฐฐํฌ ์—ฌ๋Ÿฌ๋ถ„์ด ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ๋™๋ฃŒ๋ผ๋“ ์ง€ ๋‹ค๋ฅธ ๋ˆ„๊ตฐ๊ฐ€์—๊ฒŒ ์ฃผ๊ณ  ์‹ถ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. ์ž์„ธํ•œ ์ •๋ณด๋Š” ํŒŒ์ด์ฌ ํŒจํ‚ค์ง• ์‚ฌ์šฉ์ž ์•ˆ๋‚ด์„œ(Python Packaging User Guide)๋ฅผ ์ฐธ์กฐํ•˜๋ผ. setup.py ํŒŒ์ผ ์ƒ์„ฑ ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ตœ์ƒ์œ„์— setup.py ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. # setup.py import setuptools setuptools.setup( name="porty", version="0.0.1", author="Your Name", author_email="you@example.com", description="Practical Python Code", packages=setuptools.find_packages(), ) MANIFEST.in ์ƒ์„ฑ ํ”„๋กœ์ ํŠธ์™€ ์—ฐ๊ด€๋œ ์ถ”๊ฐ€ ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด MANIFEST.in ํŒŒ์ผ์— ์ง€์ •ํ•œ๋‹ค. ์˜ˆ: # MANIFEST.in include *.csv MANIFEST.in ํŒŒ์ผ์„ setup.py์™€ ๊ฐ™์€ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋‘”๋‹ค. ์†Œ์Šค ๋ฐฐํฌํŒ ์ƒ์„ฑ ์ฝ”๋“œ ๋ฐฐํฌํŒ์„ ์ƒ์„ฑํ•˜๋ ค๋ฉด setup.py ํŒŒ์ผ์„ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ: bash % python setup.py sdist ์ด๊ฒƒ์€ dist/ ๋””๋ ‰ํ„ฐ๋ฆฌ์—. tar.gz ๋˜๋Š”. zip ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ ํŒŒ์ผ์„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์—๊ฒŒ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ๋‹น์‹ ์˜ ์ฝ”๋“œ๋ฅผ ์„ค์น˜ ๋‹น์‹ ์ด ์ž‘์„ฑํ•œ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด pip๋ฅผ ์‚ฌ์šฉํ•ด ์„ค์น˜ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์ „ ๋‹จ๊ณ„์—์„œ ์ƒ์„ฑํ•œ ํŒŒ์ผ์„ ์ œ๊ณตํ•˜๋ฉด ๋œ๋‹ค. ์˜ˆ: bash % python -m pip install porty-0.0.1.tar.gz ๋ถ€์—ฐ ์„ค๋ช… ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋‹จ๊ณ„๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ์—๊ฒŒ ์ค„ ์ˆ˜ ์žˆ๋Š” ํŒŒ์ด์ฌ ์ฝ”๋“œ์˜ ํŒจํ‚ค์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ตœ์†Œํ•œ์˜ ๊ธฐ์ดˆ๋งŒ ๋‹ค๋ค˜๋‹ค. ํ˜„์‹ค์—์„œ๋Š” ์„œ๋“œ ํŒŒํ‹ฐ ์˜์กด์„ฑ, ์™ธ๋ถ€ ์ฝ”๋“œ(์˜ˆ: C/C++) ํฌํ•จ ์—ฌ๋ถ€ ๋“ฑ์œผ๋กœ ์ธํ•ด ํ›จ์”ฌ ๋ณต์žกํ•˜๋‹ค. ๊ทธ๋Ÿฐ ์ผ๋“ค์„ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์€ ์ด ์ฝ”์Šค์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ๋‹ค. ์šฐ๋ฆฌ๋Š” ์•„์ฃผ ์ž‘์€ ์ฒซ๊ฑธ์Œ๋งŒ ๋””๋Ž ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 9.5: ํŒจํ‚ค์ง€ ๋งŒ๋“ค๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ 9.3์—์„œ ๋งŒ๋“  porty-app/ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์ด ์„น์…˜์—์„œ ์„ค๋ช…ํ•œ ๋‹จ๊ณ„๋ฅผ ์‹ค์Šตํ•ด ๋ณด๋ผ. setup.py ํŒŒ์ผ๊ณผ MANIFEST.in ํŒŒ์ผ์„ ์ตœ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ถ”๊ฐ€ํ•˜๋ผ. python setup.py sdist๋ฅผ ์‹คํ–‰ํ•ด ์†Œ์Šค ๋ฐฐํฌ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ผ. ๋์œผ๋กœ, ๋‹น์‹ ์ด ๋งŒ๋“  ํŒจํ‚ค์ง€๋ฅผ ํŒŒ์ด์ฌ ๊ฐ€์ƒ ํ™˜๊ฒฝ์— ์„ค์น˜ํ•ด ๋ณด๋ผ. ๋! ์ด ๊ณผ์ •์„ ๋ชจ๋‘ ๋งˆ์ณค๋‹ค. ์‹œ๊ฐ„๊ณผ ๋…ธ๋ ฅ์„ ๊ธฐ์šธ์ธ ๊ฒƒ์— ๊ฐ์‚ฌํ•œ๋‹ค. ์•ž์œผ๋กœ์˜ ํŒŒ์ด์ฌ ํ•ดํ‚น์ด ๋”์šฑ ์žฌ๋ฏธ์žˆ๊ณ  ์ƒ์‚ฐ์ ์ด ๋˜๊ธฐ๋ฅผ ๋ฐ”๋ž€๋‹ค! ์–ธ์ œ๋“  ์˜๊ฒฌ์„ ๊ณ ๋ง™๊ฒŒ ๋ฐ›๊ฒ ๋‹ค. ๋‚˜๋ฅผ https://dabeaz.com์ด๋‚˜ ํŠธ์œ„ํ„ฐ @dabeaz์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. - David Beazley. A. Practical Python Programming - ๊ฐ•์˜ ๋…ธํŠธ ์ €์ž: David Beazley ๊ฐœ์š” ์ด ๋ฌธ์„œ๋Š” ๋‚˜์˜ โ€œPractical Pythonโ€ ์ฝ”์Šค์˜ ๋‚ด์šฉ์„ ๊ฐ€๋ฅด์นจ์— ์žˆ์–ด ํ•™์Šต๋ชฉํ‘œ, ๋Œ€์ƒ์ž, ์ฃผ์˜์ ์„ ํฌํ•จํ•ด ์ผ๋ฐ˜์ ์ธ ์ฐธ๊ณ  ์‚ฌํ•ญ๊ณผ ์กฐ์–ธ์„ ์ œ๊ณตํ•œ๋‹ค. ์ด ์ง€์นจ์€ ๊ธฐ์—… ํŠธ๋ ˆ์ด๋‹ ํ™˜๊ฒฝ์˜ ์ „ํ˜•์ ์ธ 3์ผ ๊ต์œก ์ฝ”์Šค์—์„œ ์‚ฌ๋žŒ๋“ค์„ ๊ฐ€๋ฅด์น˜๋Š” ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. ์ž์‹ ๋งŒ์˜ ์ฝ”์Šค๋ฅผ ๋งŒ๋“ค์–ด ๊ฐ€๋ฅด์น˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ์ด ๋ฌธ์„œ๊ฐ€ ๋„์›€์ด ๋  ๊ฒƒ์ด๋‹ค. ๋Œ€์ƒ์ž ๋ฐ ์ผ๋ฐ˜์ ์ธ ์ ‘๊ทผ ์ด ์ฝ”์Šค๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฒฝํ—˜์ด ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•œ โ€œํŒŒ์ด์ฌ ์ž…๋ฌธโ€ ์ฝ”์Šค๋กœ ๊ธฐํšํ–ˆ๋‹ค. โ€œํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฒซ๊ฑธ์Œโ€ ์ฝ”์Šค๋ฅผ ๋“ฃ๋Š” ์‚ฌ๋žŒ๋“ค์„ ๊ฐ€๋ฅด์น˜๋„๋ก ์„ค๊ณ„ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ํ•„์ž์˜ ๊ฒฝํ—˜์ƒ, ํŒŒ์ด์ฌ ์ฝ”์Šค๋ฅผ ๋“ค์œผ๋Ÿฌ ์˜ค๋Š” ์ผ๋ฐ˜์ ์ธ ์ˆ˜๊ฐ•์ƒ์€ ๋†’์€ ์ˆ˜์ค€์˜ ์†Œํ”„ํŠธ์›จ์–ด ๊ธฐ์ˆ ์ž ํ˜น์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๊ธฐ์ˆ ์ž, ๊ณผํ•™์ž, ์›น ํ”„๋กœ๊ทธ๋ž˜๋จธ, ๊ฒฝํ—˜์ด ์ ์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋’ค์„ž์—ฌ ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค. ์ˆ˜๊ฐ•์ƒ์˜<NAME> ๋งค์šฐ ๋‹ค์–‘ํ•˜๋‹ค. C, C++, ์ž๋ฐ”๋ฅผ ๋‹ค๋ค„๋ณธ ํ•™์ƒ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๊ณ , PHP์™€ HTML์„ ์•Œ ์ˆ˜๋„ ์žˆ๊ณ , MATLAB ๊ฐ™์€ ๋„๊ตฌ๋ฅผ ๋‹ค๋ค„๋ดค์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์„ ์ˆ˜ ์กฐ๊ฑด์„ ๋ช…ํ™•ํžˆ ํ•˜๋ ค ๋…ธ๋ ฅํ–ˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ „ํ†ต์ ์ธ โ€œํ”„๋กœ๊ทธ๋ž˜๋ฐโ€ ๊ฒฝํ—˜์ด ์ „ํ˜€ ์—†๋Š” ์ˆ˜๊ฐ•์ƒ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋ฅผ ์—ผ๋‘์— ๋‘๊ณ , ์ด ์ฝ”์Šค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ œ(ํŠนํžˆ ์ฃผ์‹ ์‹œ์žฅ์˜ ๋ฐ์ดํ„ฐ)๋ฅผ ํ†ตํ•ด ํŒŒ์ด์ฌ์„ ๊ฐ€๋ฅด์นœ๋‹ค. ์ด ์˜์—ญ์„ ์„ ํƒํ•œ ์ด์œ ๋Š” ๋‹จ์ˆœํ•˜๋ฉด์„œ๋„ ์ˆ˜๊ฐ•์ƒ์˜ ๋ฐฐ๊ฒฝ์— ๊ด€๊ณ„์—†์ด ๋ˆ„๊ตฌ๋‚˜ ์•Œ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ˆ ์ด ๋ถ€์กฑํ•œ ํ•™์ƒ๋„ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ(์˜ˆ: Excel)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ์ผ๋ฐ˜์ ์ธ ๊ฒƒ์€ ์•Œ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ, ์ˆ˜๊ฐ•์ƒ์ด ํ—ค๋งค๊ณ  ์žˆ์„ ๋•Œ โ€œํŠœํ”Œ์€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ์˜ ๋ฐ์ดํ„ฐ ์—ด ๊ฐ™์€ ๊ฒƒโ€์ด๋ผ๋“ ์ง€ โ€œ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌ ํ—จ ์…˜(list comprehension)์€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ ์นผ๋Ÿผ์— ์—ฐ์‚ฐ์„ ์ ์šฉํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค๋ฅธ ์นผ๋Ÿผ์— ๋„ฃ๋Š” ๊ฒƒโ€์ด๋ผ๊ณ  ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•ต์‹ฌ ๊ฐœ๋…์€ ๋‚œํ•ดํ•œ โ€œ์ปดํ“จํ„ฐ ๊ณผํ•™โ€ ๋ฌธ์ œ์— ์–ฝ๋งค์ด์ง€ ์•Š๊ณ (์˜ˆ: โ€œํ”ผ๋ณด๋‚˜์น˜์ˆ˜์—ด์„ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹คโ€) ์‹ค์„ธ๊ณ„์— ๋ฐœ๋ถ™์ด๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๋ฌธ์ œ ์˜์—ญ์€ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฃผ์ œ๋ฅผ ์†Œ๊ฐœํ•  ๋•Œ๋„ ํšจ๊ณผ์ ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ณผํ•™์ž/๊ณตํ•™์ž๋Š” ๋ฐ์ดํ„ฐ ๋ถ„์„์ด๋‚˜ ํ”Œ๋กœํŒ…์— ๊ด€์‹ฌ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ, ๊ทธ๋“ค์—๊ฒŒ๋Š” matplotlib์„ ์‚ฌ์šฉํ•ด ํ”Œ๋กฏ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฒ•์„ ๋ณด์—ฌ์ค˜๋ผ. ์›น ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ์ฃผ์‹ ์‹œ์žฅ ๋ฐ์ดํ„ฐ๋ฅผ ์›นํŽ˜์ด์ง€์— ํ‘œ์‹œํ•˜๋Š” ๋ฒ•์„ ์•Œ๊ณ  ์‹ถ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์„ค๋ช…ํ•˜๋ฉด ๋œ๋‹ค. ์‹œ์Šคํ…œ ๊ด€๋ฆฌ์ž๋Š” ๋กœ๊ทธ ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ๋ฒ•์„ ์•Œ๊ณ  ์‹ถ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์‹ค์„ธ๊ณ„์˜ ์ฃผ์‹ ๋ฐ์ดํ„ฐ ๋กœ๊ทธ ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์ˆ˜์—…์„ ํ•˜๋ผ. ์†Œํ”„ํŠธ์›จ์–ด ๊ธฐ์ˆ ์ž๋Š” ์„ค๊ณ„์— ๊ด€์‹ฌ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ฃผ์‹ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด์— ์บก์Šํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ผ๋“ ์ง€ ํ”„๋กœ๊ทธ๋žจ์˜ ํ™•์žฅ์„ฑ์„ ๋†’์ด๋Š” ๋ฒ•์„ ์„ค๋ช…ํ•˜๋ผ(์˜ˆ: ์ด ํ”„๋กœ๊ทธ๋žจ์˜ ์ถœ๋ ฅ์„ ์—ด ๊ฐ€์ง€ ํ…Œ์ด๋ธ” ํฌ๋งท์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฒ•). ์•„์ด๋””์–ด๋ฅผ ๋‚ด ๋ณด๋ผ. ๋ฐœํ‘œ ์ง€์นจ ๋ฐœํ‘œ ์Šฌ๋ผ์ด๋“œ(๋…ธํŠธ)๋Š” ์ด์•ผ๊ธฐ ๊ตฌ์กฐ๋กœ ๋˜์–ด ์žˆ์œผ๋ฉฐ ์ˆ˜๊ฐ•์ƒ์ด ์—ฐ์Šต์„ ํ•  ๋•Œ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ œ๊ณตํ•œ๋‹ค. ๋ชจ๋“  ์Šฌ๋ผ์ด๋“œ์˜ ๋ชจ๋“  ํ•ญ๋ชฉ์„ ๋‹ค๋ฃจ๋ ค ์• ์“ธ ํ•„์š” ์—†๋‹ค. ์ˆ˜๊ฐ•์ƒ์ด ์ฝ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ฝ”๋”ฉํ•˜๋‹ค๊ฐ€ ๋‹ค์‹œ ๋ณผ ์‹œ๊ฐ„์ด ์žˆ์„ ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ๋‚˜๋Š” ์Šฌ๋ผ์ด๋“œ๋ฅผ ๋น ๋ฅด๊ฒŒ ํ›‘์œผ๋ฉด์„œ ์ƒํ˜ธ์ž‘์šฉ์ ์ธ ์งง์€ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ํŽธ์ด๋‹ค. ์‹œ์—ฐ์„ ํ•˜๋ฉด์„œ ์Šฌ๋ผ์ด๋“œ๋ฅผ ํ†ต์งธ๋กœ ๊ฑด๋„ˆ ๋›ฐ๊ธฐ๋„ ํ•œ๋‹ค. ๋ชจ๋“  ์Šฌ๋ผ์ด๋“œ๋ฅผ ๋‹ค๋ฃจ์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ๋ช‡ ๊ฐ€์ง€๋งŒ ๊ณจ๋ผ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์ง์ ‘ ์‹œ์—ฐํ•˜๋ผ. ๊ทœ์น™: ํŠน๋ณ„ํžˆ ๊นŒ๋‹ค๋กญ์ง€ ์•Š์€ ํ•œ, ์Šฌ๋ผ์ด๋“œ ๋‹น 1๋ถ„์„ ๋„˜๊ธฐ์ง€ ๋งˆ๋ผ. ์†”์งํžˆ ๋งํ•ด์„œ, ์Šฌ๋ผ์ด๋“œ ๋Œ€๋ถ€๋ถ„์€ ๋„˜๊ธฐ๊ณ  ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๊ฒƒ๋งŒ ๋ผ์ด๋ธŒ ๋ฐ๋ชจ ์œ„์ฃผ๋กœ ๊ฐ•์˜๋ฅผ ํ•ด๋„ ๋œ๋‹ค ๋‚˜๋Š” ์ข…์ข… ๊ทธ๋ ‡๊ฒŒ ํ•œ๋‹ค. ์ฝ”์Šค ์—ฐ์Šต ๋ฌธ์ œ ์ด ์ฝ”์Šค์—๋Š” ์•ฝ 130 ๊ฐœ์˜ ์—ฐ์Šต ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. If you do every single exercise and give students time to think and code, it will likely take them about 10-12 hours. In practice, you will probably find that students require more time on certain exercises. I have some notes about this below. You should repeatedly emphasize to students that solution code is available and that it is okay to look at it and copy it--especially due to time requirements. Prior to teaching the course, I would strongly advise that you go through and work every single course exercise so that there are no surprises. During course delivery, I usually work every single exercise from scratch, without looking at the solution, on my computer while the students also work. For this, I strongly advise you to have a printed copy of the exercises on hand that you can look at without having to pull it up on the computer screen (which is being projected). Near the end of the exercise time period, I will start discussing my solution code, emphasizes different bits on the screen and talking about them. If there are any potential problems with the solution (including design considerations), Iโ€™ll also talk about it. Emphasize to students that they may want to look at/copy solution code before going forward. Section 1: Introduction The major goal of this section is to get people started with the environment. This includes using the interactive shell and editing/run short programs. By the end of the section, students should be able to write short scripts that read data files and perform small calculations. They will know about numbers, strings, lists, and files. There will also be some exposure to functions, exceptions, and modules, but a lot of details will be missing. The first part of this course is often the longest because students are new to the tools and may have various problems getting things to work. It is absolutely critical that you go around the room and make sure that everyone can edit, run, and debug simple programs. Make sure Python is installed correctly. Make sure they have the course exercises downloaded. Make sure the internet works. Fix anything else that comes up. Timing: I aim to finish section 1 around lunch on the first day. Section 2 : Working with Data This section is probably the most important in the course. It covers the basics of data representation and manipulation including tuples, lists, dicts, and sets. Section 2.2 the most important. Give students as much time as they need to get exercises working within reason. Depending on audience, the exercises might last 45 minutes. In the middle of this exercise, I will often move forward to Section 2.3 (formatted printing) and give students more time to keep working. Together, Sections 2.2/2.3 might take an hour or more. Section 2.4 has people explore the use of enumerate(), and zip(). I consider these functions essential so donโ€™t skimp on it. Section 2.5 introduces the collections module. There is a LOT that could be said about collections, but it won't be fully appreciated by students at this time. Approach this more from the standpoint of "here's this cool module you should look at later. Here are a few cool examples." Section 2.6 introduces list comprehensions which are an important feature for processing list data. Emphasize to students that list comprehensions are very similar to things like SQL database queries. At the end of this exercise, I often do an interactive demo involving something more advanced. Maybe do a list comprehension and plot some data with matplotlib. Also an opportunity to introduce Jupyter if you're so inclined. Section 2.7 is the most sophisticated exercise. ๊ทธ๊ฒƒ์€ ํŒŒ์ด์ฌ์˜ ์ผ๊ธ‰(first-class) ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ๊ณผ ๊ด€๋ จ์ด ์žˆ์œผ๋ฉฐ, ์›ํ•˜๋Š” ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ ๊ฐ™์€ ์ž๋ฃŒ ๊ตฌ์กฐ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค๊ณผ ๊ด€๋ จ์ด ์žˆ๋‹ค. The exercises are related to parsing columns of data in CSV files and concepts are later reused in Section 3.2. Timing: Ideally, you want to be done with section 2 on the first day. However, it is common to finish with section 2.5 or 2.6. So, don't panic if you feel that you're a bit behind. 3. ํ”„๋กœ๊ทธ๋žจ ์กฐ์งํ™” The main goal of this section is to introduce more details about functions and to encourage students to use them. The section builds from functions into modules and script writing. Section 3.1 is about going from simple โ€œscriptingโ€ to functions. ์ˆ˜๊ฐ•์ƒ์ด ์กฐ์งํ™”๋˜์ง€ ์•Š์€ โ€œ์Šคํฌ๋ฆฝํŠธโ€๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ฒŒ ํ•˜๋ผ. ์ฝ”๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋“ˆํ™”ํ•ด์•ผ ํ•œ๋‹ค. ์กฐ์งํ™”๋œ ์ฝ”๋“œ๋Š” ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ณ , ๋‚˜์ค‘์— ๋ณ€๊ฒฝํ•˜๊ธฐ๋„ ์‰ฌ์šฐ๋ฉฐ, ์‹คํ–‰ ์†๋„๋„ ์ข€ ๋” ๋น ๋ฅด๋‹ค. ํ•จ์ˆ˜๋Š” ์ข‹๋‹ค. ์„น์…˜ 3.2์˜ ์—ฐ์Šต ๋ฌธ์ œ๋Š” ์ „์ฒด ์ฝ”์Šค์—์„œ ๊ฐ€์žฅ ๊ณ ๊ธ‰์ผ ๊ฒƒ์ด๋‹ค. ์ˆ˜๊ฐ•์ƒ์€ ์นผ๋Ÿผ ๊ธฐ๋ฐ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑ ํ•˜๋Š” ๋ฒ”์šฉ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜๊ณผ ํ•จ์ˆ˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค(์˜ˆ: ํ•จ์ˆ˜๋ฅผ ์ผ๊ธ‰ ๊ฐ์ฒด๋กœ์„œ ์‚ฌ์šฉ). ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ์ฝ”๋“œ์˜ ๊ฐ ๋‹จ๊ณ„๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ƒ์„ธํ•˜๊ฒŒ ์•ˆ๋‚ดํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค. The payoff is huge however---you can show people a short general purpose function that does something amazingly powerful and which would be virtually impossible to write in C, C++, or Java without having a LOT of very complicated code. There are a lot of possible design/discussion avenues for this code. Use your imagination. Section 3.3 adds error handling to the function created in Section 3.2 This is a good time to talk about exception handling generally. Definitely talk about the dangers of catching all exceptions. This might be a good time to talk about the โ€œErrors should never pass silentlyโ€ item on the โ€œZen of Python.โ€ *Note: Before Exercise 3.4, make sure students get fully working versions of report.py, pcost.py, and fileparse.py. Copy from Solutions folder if needed * Section 3.4 Introduces module imports. The file written in Section 3.2-3.3 is used to simplify code in Section 3.1. Be aware that you may need to help students fix issues with IDLE, sys.path, and other assorted settings related to import. Section 3.5 talks about __main__ and script writing. There's a bit about command line arguments. You might be inclined to discuss a module like argparse. However, be warned that doing so opens up a quagmire. It's usually better to just mention it and move on. Section 3.6 opens up a discussion about design more generally in Python. Is it better to write code that's more flexible vs code that's hardwired to only work with filenames? This is the first place where you make a code change and have to refactor existing code. Going forward from here, most of the exercises make small changes to code that's already been written. 4. ํด๋ž˜์Šค์™€ ๊ฐ์ฒด This section is about very basic object oriented programming. In general, it is not safe to assume that people have much background in OO. So, before starting this, I usually generally describe the OO โ€œstyleโ€ and how it's data and methods bundled together. Do some examples with strings and lists to illustrate that they are โ€œobjectsโ€ and that the methods (invoked via .) do things with the object. Emphasize how the methods are attached to the object itself. For example, you do items.append(x), you donโ€™t call a separate function append(items, x). Section 4.1 introduces the class statement and shows people how to make a basic object. Really, this just introduces classes as another way to define a simple data structure--relating back to using tuples and dicts for this purpose in section 2. Section 4.2 is about inheritance and how you use to create extensible programs. This set of exercises is probably the most significant in terms of OO programming and OO design. Give students a lot of time to work on it (30-45 minutes). Depending on interest, you can spend a LOT of time discussing aspects of OO. For example, different design patterns, inheritance hierarchies, abstract base classes, etc. Section 4.3 does a few experiments with special methods. I wouldn't spend too much time fooling around with this. Special methods come up a bit later in Exercise 6.1 and elsewhere. Timing: This is usually the end of the 2nd day. 5. Inside Objects This section takes students behind the scenes of the object system and how itโ€™s built using dictionaries, how instances and classes are tied together, and how inheritance works. However, most important part of this section is probably the material about encapsulation (private attributes, properties, slots, etc.) Section 5.1 just peels back the covers and has students observe and play with the underlying dicts of instances and classes. Section 5.2 is about hiding attributes behind get/set functions and using properties. I usually emphasize that these techniques are commonly used in libraries and frameworks--especially in situations where more control over what a user is allowed to do is desired. An astute Python master will notice that I do not talk about advanced topics such as descriptors, or attribute access methods (__getattr__, __setattr__) at all. I have found, through experience, that this is just too much mental overload for students taking the intro course. Everyoneโ€™s head is already on the verge of exploding at this point and if you go talk about how something like descriptors work, youโ€™ll lose them for the rest of the day, if not the rest of the course. Save it for an "Advanced Python" course. If you're looking at the clock thinking "There's no way I'm going to finish this course", you can skip section 5 entirely. 6. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ The main purpose of this section is to introduce generators as a way to define custom iteration and to use them for various problems related to data handling. The course exercises have students analyze streaming data in the form of stock updates being written to a log file. There are two big ideas to emphasize. First, generators can be used to write code based on incremental processing. This can be very useful for things like streaming data or huge datasets that are too large to fit into memory all at once. The second idea is that you can chain generators/iterators together to create processing pipelines (kind of like Unix pipes). Again, this can be a really powerful way to process and think about streams, large datasets, etc. Some omissions: Although the iteration protocol is described, the notes donโ€™t go into detail about creating iterable objects (i.e., classes with __iter__() and next()). In practice, Iโ€™ve found that itโ€™s not necessary to do this so often (generators are often better/easier). So, in the interest of time, Iโ€™ve made a conscious decision to omit it. Also not included are extended generators (coroutines) or uses of generators for concurrency (tasklets, etc.). Thatโ€™s better covered in advanced courses. 7. ๊ณ ๊ธ‰ ์ฃผ์ œ Basically this section is an assortment of more advanced topics that could have been covered earlier, but werenโ€™t for various reasons related to course flow and content of the course exercises. If you must know, I used to present this material earlier in the course, but found that students were already overloaded with enough information. Coming back to it later seems to work better---especially since by this point, everyone is much more familiar with working in Python and starting to get the hang of it. Topics include variadic function arguments (args, *kwargs), lambda, closures, and decorators. Discussion of decorators is only a tiny hint of whatโ€™s possible with metaprogramming. Feel free to say more about whatโ€™s possible, but Iโ€™d probably stay out of metaclasses! Lately, I have been demoing "numba" as an example of a more interesting decorator. If you're pressed for time, most of section 7 can be skipped or heavily compressed (you could skip exercises for instance). 8. Testing and Debugging The main purpose of this section is just to introduce various tools and techniques related to testing, debugging, and software development. Show everyone the unittest module. Introduce the logging module. Discuss assertions and the idea of โ€œcontracts.โ€ Show people the debugger and profiler. Most of this is self-explanatory. 9. ํŒจํ‚ค์ง€(Package) At this point, students have written an assortment of files (pcost.py, report.py, fileparse.py, tableformat.py, stock.py, portfolio.py, follow.py, etc.). Two main goals in this section. First, put all of the code into a Python package structure. This is only a gentle introduction to that, but they'll move the files into a directory and everything will break. They'll need to fix their import statements (package relative imports) and maybe fiddle with an __init__.py file. Second goal, write a simple setup.py file that they can use to package up the code and give it away to someone. That's it. End of the course. B. ๋ผ์ด์„ ์Šค creative commons Attribution-ShareAlike 4.0 International Creative Commons Corporation (โ€œCreative Commonsโ€) is not a law firm and does not provide legal services or legal advice. Distribution of Creative Commons public licenses does not create a lawyer-client or other relationship. Creative Commons makes its licenses and related information available on an โ€œas-isโ€ basis. Creative Commons gives no warranties regarding its licenses, any material licensed under their terms and conditions, or any related information. Creative Commons disclaims all liability for damages resulting from their use to the fullest extent possible. Using Creative Commons Public Licenses Creative Commons public licenses provide a standard set of terms and conditions that creators and other rights holders may use to share original works of authorship and other material subject to copyright and certain other rights specified in the public license below. The following considerations are for informational purposes only, are not exhaustive, and do not form part of our licenses. Considerations for licensors: Our public licenses are intended for use by those authorized to give the public permission to use material in ways otherwise restricted by copyright and certain other rights. Our licenses are irrevocable. Licensors should read and understand the terms and conditions of the license they choose before applying it. Licensors should also secure all rights necessary before applying our licenses so that the public can reuse the material as expected. Licensors should clearly mark any material not subject to the license. This includes other CC-licensed material, or material used under an exception or limitation to copyright. More considerations for licensors. Considerations for the public: By using one of our public licenses, a licensor grants the public permission to use the licensed material under specified terms and conditions. If the licensorโ€™s permission is not necessary for any reason โ€“ for example, because of any applicable exception or limitation to copyright โ€“ then that use is not regulated by the license. Our licenses grant only permissions under copyright and certain other rights that a licensor has authority to grant. Use of the licensed material may still be restricted for other reasons, including because others have copyright or other rights in the material. A licensor may make special requests, such as asking that all changes be marked or described. Although not required by our licenses, you are encouraged to respect those requests where reasonable. More considerations for the public. Creative Commons Attribution-ShareAlike 4.0 International Public License By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions. Section 1 โ€“ Definitions. a. Adapted Material means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image. b. Adapter's License means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License. c. BY-SA Compatible License means a license listed at creativecommons.org/compatiblelicenses, approved by Creative Commons as essentially the equivalent of this Public License. d. Copyright and Similar Rights means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. e. Effective Technological Measures means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements. f. Exceptions and Limitations means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material. g. License Elements means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution and ShareAlike. h. Licensed Material means the artistic or literary work, database, or other material to which the Licensor applied this Public License. i. Licensed Rights means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license. j. Licensor means the individual(s) or entity(ies) granting rights under this Public License. k. Share means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them. l. Sui Generis Database Rights means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world. m. You means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning. Section 2 โ€“ Scope. a. License grant. 1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to: A. reproduce and Share the Licensed Material, in whole or in part; and B. produce, reproduce, and Share Adapted Material. 2. __Exceptions and Limitations.__ For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions. 3. __Term.__ The term of this Public License is specified in Section 6(a). 4. __Media and formats; technical modifications allowed.__ The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material. 5. __Downstream recipients.__ A. __Offer from the Licensor โ€“ Licensed Material.__ Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License. B. __Additional offer from the Licensor โ€“ Adapted Material. Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapterโ€™s License You apply. C. __No downstream restrictions.__ You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material. 6. __No endorsement.__ Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i). b. Other rights. 1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise. 2. Patent and trademark rights are not licensed under this Public License. 3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties. Section 3 โ€“ License Conditions. Your exercise of the Licensed Rights is expressly made subject to the following conditions. a. Attribution. 1. If You Share the Licensed Material (including in modified form), You must: A. retain the following if it is supplied by the Licensor with the Licensed Material: i. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated); ii. a copyright notice; iii. a notice that refers to this Public License; iv. a notice that refers to the disclaimer of warranties; v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable; B. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and C. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License. 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information. 3. If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable. b. ShareAlike. In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply. The Adapterโ€™s License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-SA Compatible License. You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply. Section 4 โ€“ Sui Generis Database Rights. Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material: a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database; b. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and c. You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database. For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights. Section 5 โ€“ Disclaimer of Warranties and Limitation of Liability. a. Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You. b. To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You. c. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability. Section 6 โ€“ Term and Termination. a. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically. b. Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates: 1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or 2. upon express reinstatement by the Licensor. For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License. c. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License. d. Sections 1, 5, 6, 7, and 8 survive termination of this Public License. Section 7 โ€“ Other Terms and Conditions. a. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed. b. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License.t stated herein are separate from and independent of the terms and conditions of this Public License. Section 8 โ€“ Interpretation. a. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License. b. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions. c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor. d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority. Creative Commons is not a party to its public licenses. Notwithstanding, Creative Commons may elect to apply one of its public licenses to material it publishes and in those instances will be considered the โ€œLicensor.โ€ Except for the limited purpose of indicating that material is shared under a Creative Commons public license or as otherwise permitted by the Creative Commons policies published at [creativecommons.org/policies](http://creativecommons.org/policies), Creative Commons does not authorize the use of the trademark โ€œCreative Commonsโ€ or any other trademark or logo of Creative Commons without its prior written consent including, without limitation, in connection with any unauthorized modifications to any of its public licenses or any other arrangements, understandings, or agreements concerning use of licensed material. For the avoidance of doubt, this paragraph does not form part of the public licenses. Creative Commons may be contacted at creativecommons.org<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ๐Ÿค—Transformers (์‹ ๊ฒฝ๋ง ์–ธ์–ด๋ชจ๋ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ) ๊ฐ•์ขŒ ### ๋ณธ๋ฌธ: ํ˜„์žฌ ์ „ ์„ธ๊ณ„์ ์œผ๋กœ ๊ฐ€์žฅ ๋งŽ์ด ํ™œ์šฉ๋˜๊ณ  ์žˆ๋Š” ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ(Neural Language Models) ๊ธฐ๋ฐ˜ ๋”ฅ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ Hugging Face ์‚ฌ์˜ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ™œ์šฉ๋ฒ•์„ ์ƒ์„ธํžˆ ๊ธฐ์ˆ ํ•˜๊ณ  ์žˆ๋Š” Transformers Course๋ฅผ ํ•œ๊ตญ์–ด๋กœ ๋ฒˆ์—ญํ•œ ์ž๋ฃŒ๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ๋‚ด์šฉ ์ž์ฒด๊ฐ€ ์›Œ๋‚™ ์•Œ๊ธฐ ์‰ฝ๊ฒŒ ๊ธฐ์ˆ ๋˜์–ด ์žˆ๊ณ , Jupyter Notebook์ด๋‚˜ Google Colaboratory๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณธ์ธ์ด ์ง์ ‘ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ์ถ”๊ฐ€ ์„ค๋ช…์ด ๋งŽ์ด ํ•„์š”ํ•˜์ง€๋Š” ์•Š์•˜์œผ๋‚˜, ๊ทธ๋ž˜๋„ ์ผ๋ถ€ ๋‚ด์šฉ ์ค‘์—์„œ ์—ญ์ž๊ฐ€ ์ƒ๊ฐํ•˜๊ธฐ์— ์ดํ•ด๊ฐ€ ์‰ฝ์ง€ ์•Š๊ฑฐ๋‚˜ ๋ณต์žกํ•  ์ˆ˜๋„ ์žˆ๋Š” ๋ถ€๋ถ„์€ ๋ถ€๊ฐ€ ์„ค๋ช…์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ถ€๋”” ๋”ฅ๋Ÿฌ๋‹ ๊ธฐ๋ฐ˜ ์–ธ์–ด ์ฒ˜๋ฆฌ ๋ฐ ๋Œ€๊ทœ๋ชจ ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ ๊ณต๋ถ€์— ๋„์›€์ด ๋˜์…จ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ•์ขŒ ํ™œ์šฉ ๋ฐฉ๋ฒ• ๋ณธ ๊ฐ•์ขŒ๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๋ช‡ ๊ฐ€์ง€ ์•Œ๋ ค๋“œ๋ฆด ๋‚ด์šฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ๊ฐ•์ขŒ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๋‚ด์šฉ์ด ์ง์ ‘ ์‹ค์Šต์„ ํ•ด๊ฐ€๋ฉด์„œ ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๊ฐ€๊ธ‰์ ์ด๋ฉด Jupyter Notebook์ด๋‚˜ Colaboratory๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‹ค์Šต์„ ํ•˜์‹œ๋ฉด์„œ ๊ณต๋ถ€ํ•˜์‹œ๊ธธ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ € ์—ญ์‹œ๋„ ๊ฐ•์ขŒ ๋‚ด์˜ ๋ชจ๋“  ์ฝ”๋“œ๋“ค์„ ์‹คํ–‰ํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๋ฉด์„œ ๋ฒˆ์—ญ์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋”์”ฉ ๋…ธํŠธ๋ถ์ด๋‚˜ PC ๋“ฑ๊ณผ ๊ฐ™์€ ์ผ๋ฐ˜์ ์ธ ์ปดํ“จํ„ฐ ์‚ฌ์–‘์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š๊ฑฐ๋‚˜ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ์˜ˆ์ œ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ€์šฉํ•œ GPU ์„œ๋ฒ„๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ ์„œ๋ฒ„์—์„œ ์‹ค์Šตํ•˜์‹œ๊ธธ ๋ฐ”๋ผ๋ฉฐ, ๊ฐ€์šฉ ์„œ๋ฒ„๊ฐ€ ์—†๋‹ค๋ฉด Google Colaboratory ํ™œ์šฉ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. ๋ณธ ๊ฐ•์ขŒ๋Š” TensorFlow ๋ฐ PyTorch๋ฅผ ์„ ํƒ์ ์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค๋งŒ, ์ด ๋ฒˆ์—ญํŒ์€ PyTorch๋ฅผ ํ™œ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ•์ขŒ ๋‚ด์— ๋‚˜์˜ค๋Š” ๋ชจ๋“  ์ด๋ฏธ์ง€๋“ค์€ ์ง์ ‘ ๊ฐ€์ ธ์˜ค์ง€ ์•Š๊ณ  Transformers Course ์‚ฌ์ดํŠธ์˜ URL์„ ๋งํฌ๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋„ํ‘œ๋Š” ์ง์ ‘ ๋งˆํฌ๋‹ค์šด(Markdown)์œผ๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์งˆ๋ฌธ ์‚ฌํ•ญ์ด ์žˆ์œผ์‹œ๋ฉด ์–ธ์ œ๋“ ์ง€ ๋Œ“๊ธ€์„ ํ†ตํ•ด์„œ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ์„ฑ์‹ฌ์„ ๋‹คํ•ด์„œ ๋‹ต๋ณ€๋“œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‚ด์šฉ ์ „๊ฐœ ๊ณผ์ •์—์„œ ์•„์ด์ฝ˜์ด ๋งŽ์ด ๋“ฑ์žฅํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Hugging Face ์•„์ด์ฝ˜์ธ๋Š” ์ฃผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ช…์นญ ์•ž์— ์œ„์น˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๊ฐ•์ขŒ ๋‚ด์šฉ ๋ณธ ๊ฐ•์ขŒ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์žฅ์˜ ํ•ด๋‹น ์„น์…˜์˜ ๋งํฌ๋กœ ๋“ค์–ด๊ฐ€์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 0์žฅ. ๋ณธ ๊ฐ•์ขŒ๋ฅผ ๊ณต๋ถ€ํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ค€๋น„ํ•ด์•ผ ํ•  ์‚ฌํ•ญ๋“ค (Setup) 1์žฅ. ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ (Transformer models) 2์žฅ. Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ™œ์šฉ๋ฒ• (Using Transformers) 3์žฅ. ์‚ฌ์ „ํ•™์Šต ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ฏธ์„ธ์กฐ์ • (Fine-tuning a pretrained model) 4์žฅ. ๋ชจ๋ธ ๋ฐ ํ† ํฌ ๋‚˜์ด์ € ๊ณต์œ  (Sharing models and tokenizers) 5์žฅ. Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ (The Datasets library) 6์žฅ. Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ (The Tokenizers library) 7์žฅ. ์ฃผ์š” NLP ํƒœ์Šคํฌ (Main NLP Tasks) 8์žฅ. ๋„์›€ ์š”์ฒญ ๋ฐฉ๋ฒ• (How to ask for help) ํ–ฅํ›„ ๋ณด์™„ ์‚ฌํ•ญ ํ˜„์žฌ Hugging Face ๊ฐ•์ขŒ ์‚ฌ์ดํŠธ์—๋Š” 0์žฅ๋ถ€ํ„ฐ 8์žฅ๊นŒ์ง€๋งŒ ๊ณต๊ฐœ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 9์žฅ๋ถ€ํ„ฐ 12์žฅ์€ ํ˜„์žฌ ํ•ด๋‹น ์ €์ž๋“ค์ด ๋‚ด์šฉ ์ž‘์„ฑ ์ค‘์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์กฐ๋งŒ๊ฐ„ ๊ณต๊ฐœ๋  ์˜ˆ์ •์ด๋‹ˆ ๊ณต๊ฐœ๋˜๋ฉด ๋‹ค์‹œ ๋ฒˆ์—ญํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ•์ขŒ ๊ณผ์ • ์ค‘์— ๋ณด์ด๋Š” ์œ ํŠœ๋ธŒ<NAME>์ƒ์€ ์ผ๋‹จ ์ œ์™ธํ–ˆ์Šต๋‹ˆ๋‹ค. ์ถ”ํ›„ ์š”๊ตฌ๊ฐ€ ์žˆ์œผ๋ฉด ์ถ”๊ฐ€ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 0์žฅ(Chapter 0)์˜ ๋‚ด์šฉ์ด ๋งŽ์ด ๋ถ€์‹คํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์›๋ณธ ๊ฐ•์ขŒ์˜ ๋‚ด์šฉ์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜์˜ํ–ˆ์œผ๋‚˜ ์•„๋ฌด๋ž˜๋„ ๋‹ค์–‘ํ•œ ์„ค์ • ๋ฐฉ์‹์„ ํฌ๊ด„์ ์œผ๋กœ ์„ค๋ช…ํ•˜์ง€๋Š” ๋ชปํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ํ–ฅํ›„ ๋ณด์™„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์›๋ณธ ๊ฐ•์ขŒ์—๋Š” ๊ฐ ์žฅ์ด ๋๋‚˜๋ฉด ํ€ด์ฆˆ๋ฅผ ํ’€๊ฒŒ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค๋งŒ ์ด ๋ถ€๋ถ„๋„ ์ œ์™ธ๋ฅผ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 4์žฅ๊ณผ 8์žฅ์€ ํ˜„์žฌ ์ž‘์„ฑ ๊ณผ์ •์— ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋‚ด์šฉ์— ๋น„ํ•ด์„œ ์ƒ๋Œ€์ ์œผ๋กœ ์ค‘์š”๋„๊ฐ€ ๋‚ฎ๊ณ  ๋ถ€๊ฐ€์ ์ธ ์„ค๋ช…๋“ค์ด๋ผ ํ–ฅํ›„์— ์ง€์†์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. 0์žฅ. ํ™˜๊ฒฝ์„ค์ • (SETUP) Hugging Face ๊ฐ•์ขŒ์— ์˜ค์‹  ๊ฒƒ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค! ์—ฌ๊ธฐ์—์„œ๋Š” ์ž‘์—… ํ™˜๊ฒฝ์„ ์„ค์ •ํ•˜๋Š” ๊ณผ์ •์„ ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค. ๋ณธ ๊ณผ์ •์„ ์ด์ œ ๋ง‰ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋จผ์ € 1์žฅ์„ ์‚ดํŽด๋ณธ ๋‹ค์Œ ๋Œ์•„์™€์„œ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•ด ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์‹œ์Šคํ…œ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ•์ขŒ์—์„œ ์‚ฌ์šฉํ•  ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Python ํŒจํ‚ค์ง€๋กœ ์ œ๊ณต๋˜๋ฏ€๋กœ ์—ฌ๊ธฐ์—์„œ๋Š” Python ํ™˜๊ฒฝ์„ ์„ค์ •ํ•˜๊ณ  ํ•„์š”ํ•œ ํŠน์ • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋จผ์ € Colab ๋…ธํŠธ๋ถ ๋˜๋Š” Python ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์—… ํ™˜๊ฒฝ์„ ์„ค์ •ํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•๋“ค ์ค‘์—์„œ ์—ฌ๋Ÿฌ๋ถ„์ด ์ข‹์•„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ดˆ๋ณด์ž์˜ ๊ฒฝ์šฐ Colab ๋…ธํŠธ๋ถ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. Windows ์‹œ์Šคํ…œ์—์„œ์˜ ํ™˜๊ฒฝ ์„ค์ •์€ ๋‹ค๋ฃจ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Windows์—์„œ ๋ณธ ๊ฐ•์ขŒ๋ฅผ ๋ณด๊ณ  ๊ณ„์‹ ๋‹ค๋ฉด ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•œ Colab ๋…ธํŠธ๋ถ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. Linux ๋ฐฐํฌํŒ์ด๋‚˜ macOS๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์—ฌ๊ธฐ์— ์„ค๋ช…๋œ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ•์ขŒ๋Š” Hugging Face ๊ณ„์ •์ด ์žˆ์–ด์•ผ ํŽธํ•˜๊ฒŒ ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์ง€๊ธˆ ๋ฐ”๋กœ ๊ณ„์ •์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค: ๊ณ„์ • ์ƒ์„ฑ ๊ตฌ๊ธ€์˜ Colab ๋…ธํŠธ๋ถ ์‚ฌ์šฉํ•˜๊ธฐ Colab ๋…ธํŠธ๋ถ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค์ •์„ ๋งˆ์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋…ธํŠธ๋ถ์„ ๋ถ€ํŒ…ํ•˜๊ณ  ๋ฐ”๋กœ ์ฝ”๋”ฉ์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Colab์— ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์†Œ๊ฐœ(Introduction)๋ฅผ ํ•œ๋ฒˆ ๊ณต๋ถ€ํ•˜๊ณ  ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. Colab์„ ์‚ฌ์šฉํ•˜๋ฉด GPU ๋˜๋Š” TPU์™€ ๊ฐ™์€ ์ผ๋ถ€ ๊ฐ€์† ํ•˜๋“œ์›จ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์†Œ๊ทœ๋ชจ ์›Œํฌ ๋กœ๋“œ(workload)์—๋Š” ๋ˆ์ด ๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Colab์— ์–ด๋Š ์ •๋„ ์ต์ˆ™ํ•ด์ง€๋ฉด ์ƒˆ ๋…ธํŠธ๋ถ์„ ๋งŒ๋“ค๊ณ  ์„ค์ • ์ž‘์—…์„ ์‹œ์ž‘ํ•˜์„ธ์š”: ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผ ํ•  ์„ค์ • ์ž‘์—…์€ ์ด ๊ฐ•์ขŒ์—์„œ ์‚ฌ์šฉํ•  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Python ์šฉ ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž์ธ pip๋ฅผ ์„ค์น˜์— ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋…ธํŠธ๋ถ์—์„œ๋Š” ์‹œ์Šคํ…œ ๋ช…๋ น ์•ž์—! ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•˜์—ฌ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: !pip install transformers Python ๋Ÿฐํƒ€์ž„(Python runtime) ๋‚ด์—์„œ ํŒจํ‚ค์ง€๋ฅผ ์ž„ํฌํŠธ(import) ํ•˜์—ฌ ํŒจํ‚ค์ง€๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: import transformers ์ด ๊ณผ์ •์—์„œ Transformers์˜ ๋งค์šฐ ๊ฐ€๋ฒผ์šด ๋ฒ„์ „์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ PyTorch ๋˜๋Š” TensorFlow์™€ ๊ฐ™์€ ํŠน์ • ๊ธฐ๊ณ„ ํ•™์Šต ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ํ•จ๊ป˜ ์„ค์น˜๋˜์ง€๋Š” ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Š” ๋ณ„๋„๋กœ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ƒ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฑฐ์˜ ๋ชจ๋“  ํ™œ์šฉ ์‚ฌ๋ก€(use cases)์— ํ•„์š”ํ•œ ๋ชจ๋“  ์ข…์†์„ฑ(dependency)์„ ํฌํ•จํ•œ ๊ฐœ๋ฐœ์ž ๋ฒ„์ „(development version)์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค: !pip install transformers[sentencepiece] ์‹œ๊ฐ„์ด ์กฐ๊ธˆ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ์—†์ด ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด ๋ณธ ๊ฐ•์ขŒ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์„ ๊ณต๋ถ€ํ•  ์ค€๋น„๊ฐ€ ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Python ๊ฐ€์ƒ ํ™˜๊ฒฝ ์ด์šฉํ•˜๊ธฐ Python ๊ฐ€์ƒ ํ™˜๊ฒฝ(Python virtual environment)์„ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ์‹œ์Šคํ…œ์— Python์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ์‹œ์ž‘ํ•˜๋ ค๋ฉด ์ด ๊ฐ€์ด๋“œ๋ฅผ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. Python์ด ์„ค์น˜๋˜๋ฉด ํ„ฐ๋ฏธ๋„์—์„œ Python ๋ช…๋ น์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด python --version ๋ช…๋ น์„ ์‹คํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์ด์ œ ์‹œ์Šคํ…œ์—์„œ ๊ฐ€์šฉํ•œ Python ๋ฒ„์ „์ด ์ถœ๋ ฅ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ„ฐ๋ฏธ๋„์—์„œ python --version๊ณผ ๊ฐ™์€ Python ๋ช…๋ น์„ ์‹คํ–‰ํ•  ๋•Œ ์ด๋ฅผ ์‹คํ–‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์‹œ์Šคํ…œ์˜ "main" Python์œผ๋กœ ์ƒ๊ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ "main" ์„ค์น˜(installation)๋ฅผ ์–ด๋– ํ•œ ์ถ”๊ฐ€ ํŒจํ‚ค์ง€ ์—†์ด ์›๋ž˜๋Œ€๋กœ ์œ ์ง€ํ•˜๊ณ , ๋ณธ์ธ์ด ์ž‘์—…ํ•˜๋Š” ๊ฐ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์— ๋งž๋Š” ๋ณ„๋„์˜ ํ™˜๊ฒฝ์„ ์ถ”๊ฐ€์ ์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•ด๋‹น ํ™˜๊ฒฝ์—๋Š” ๊ฐ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์— ๊ณ ์œ ํ•œ ์ข…์†์„ฑ๊ณผ ๋ถ€๊ฐ€ ํŒจํ‚ค์ง€๋“ค์ด ์กด์žฌํ•˜๋ฉฐ, ๋‹ค๋ฅธ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ๊ณผ์˜ ์ž ์žฌ์ ์ธ ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ์— ๋Œ€ํ•ด ๊ฑฑ์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. Python์—์„œ๋Š” ์ด๋ฅผ ๊ฐ€์ƒ ํ™˜๊ฒฝ(virtual environment)์œผ๋กœ ์‹คํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์ƒ ํ™˜๊ฒฝ์€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด ํ•„์š”๋กœ ํ•˜๋Š” ๊ฐ์ข… ํŒจํ‚ค์ง€๋“ค์ด ํฌํ•จ๋œ ํŠน์ • ๋ฒ„์ „์˜ ํŒŒ์ด์ฌ์ด ์„ค์น˜๋˜์–ด ์žˆ๋Š” ์™„์ „ํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๋‹ค์–‘ํ•œ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์šฐ๋ฆฌ๋Š” venv๋ผ๊ณ  ํ•˜๋Š” ๊ณต์‹ Python ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋จผ์ € ๋Œ€์ƒ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ์ €์žฅํ•  ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ™ˆ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋ฃจํŠธ์— transformers-course๋ผ๋Š” ์ƒˆ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”: mkdir ~/transformers-course cd ~/transformers-course ์ด ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‚ด์—์„œ Python venv ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค: python -m vent .env ์ด์ œ ๋น„์–ด์žˆ๋Š” ํด๋”์—. env๋ผ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: ls -a activate ๋ฐ deactivate ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ƒ ํ™˜๊ฒฝ์— ๋“ค์–ด๊ฐ€๊ฑฐ๋‚˜ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ํ™œ์„ฑํ™” source .env/bin/activate # ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ๋น„ํ™œ์„ฑํ™” source .env/bin/deactivate which python ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ํ™˜๊ฒฝ์ด ํ™œ์„ฑํ™”๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒฝ์šฐ ์„ฑ๊ณต์ ์œผ๋กœ ํ™œ์„ฑํ™”ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค! which python /home//transformers-course/.env/bin/python Dependencies ์„ค์น˜ Google Colab ์ธ์Šคํ„ด์Šค ์‚ฌ์šฉ์— ๋Œ€ํ•œ ์ด์ „ ์„น์…˜์—์„œ์™€ ๊ฐ™์ด ์ด์ œ ๊ฐ•์ขŒ๋ฅผ ์ง„ํ–‰ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, pip ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Transformers์˜ ๊ฐœ๋ฐœ์ž ๋ฒ„์ „์„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: pip install "transformers[sentencepiece]" ์ด์ œ ๋ชจ๋“  ์„ค์ •์ด ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฉฐ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๊ณต๋ถ€ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! 1์žฅ. ํŠธ๋žœ์Šคํฌ๋จธ (Transformer) ๋ชจ๋ธ Welcome to the Course! ๋ณธ ๊ฐ•์ขŒ๋Š” Hugging Face ์ƒํƒœ๊ณ„(ecosystem)๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” Transformers, Datasets, Tokenizers, Accelerate ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค๊ณผ Hugging Face Hub์„ ํ™œ์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(NLP)์— ๋Œ€ํ•ด ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ์™„์ „ํžˆ ๋ฌด๋ฃŒ์ด๋ฉฐ ๊ด‘๊ณ ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ๋‚ด์šฉ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‚˜? ์ด ๊ฐ•์ขŒ์— ๋Œ€ํ•œ ๊ฐ„๋žตํ•œ ๊ฐœ์š”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: 1์žฅ์—์„œ 4์žฅ๊นŒ์ง€๋Š” Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ฃผ์š” ๊ฐœ๋…์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์ด ๋๋‚˜๋ฉด Transformer ๋ชจ๋ธ์˜ ์ž‘๋™ ๋ฐฉ์‹์— ์ต์ˆ™ํ•ด์ง€๊ณ , Hugging Face Hub์— ๊ณต๊ฐœ๋œ ๋ชจ๋ธ๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‚˜ ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•ด์„œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •(fin-tune) ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ Hub์—<NAME>๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค! 5์žฅ์—์„œ 8์žฅ๊นŒ์ง€๋Š” Datasets์™€ Tokenizers์˜ ๊ธฐ์ดˆ๋ฅผ ๋‹ค์ง€๊ณ  ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ž˜ ์•Œ๋ ค์ ธ ์žˆ๋Š” ์ฃผ์š” NLP ํƒœ์Šคํฌ(task) ๋“ค์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ๊ณต๋ถ€ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์ด ๋๋‚˜๋ฉด ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ด๊ณ  ๊ทธ๋ž˜์„œ ์ž์ฃผ ์ ‘ํ•  ์ˆ˜ ์žˆ๋Š” NLP ๋ฌธ์ œ๋“ค์„ ์Šค์Šค๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 9์žฅ์—์„œ 12์žฅ๊นŒ์ง€๋Š” ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจํ™”(memory efficiency) ๋ฐ ๊ธธ์ด๊ฐ€ ๊ธด ์‹œํ€€์Šค(long sequences) ๋ฌธ์ œ ๋“ฑ๊ณผ ๊ฐ™์€ ํŠน๋ณ„ํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ , ๋ณด๋‹ค ๋” ํŠนํ™”๋œ ์‚ฌ์šฉ ์‚ฌ๋ก€(use cases)๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ์ž ์ •์˜ ๊ฐœ์ฒด(custom objects)๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ฐ€๋ฅด์ณ ์ค๋‹ˆ๋‹ค. ์ด ํŒŒํŠธ๊ฐ€ ๋๋‚˜๋ฉด ๋ณต์žกํ•œ NLP ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ์ค€๋น„๋ฅผ ๊ฐ–์ถ”๊ฒŒ ๋˜๊ณ , Transformers์— ์ค‘์š”ํ•œ ๊ธฐ์—ฌ๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ•์ขŒ๋ฅผ ์ž˜ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”: Python์„ ์ž˜ ์•Œ๊ณ  ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. fast.ai์˜ Practical Deep Learning for Coders ๋˜๋Š” DeepLearning.AI์—์„œ ๋งŒ๋“  ๊ฐ•์ขŒ ํ”„๋กœ๊ทธ๋žจ ๋“ฑ๊ณผ ๊ฐ™์€ ์ž…๋ฌธ ๋”ฅ๋Ÿฌ๋‹ ๊ฐ•์ขŒ๋ฅผ ๊ณต๋ถ€ํ•œ ํ›„์— ์ˆ˜๊ฐ•ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. PyTorch ๋˜๋Š” TensorFlow์— ๋Œ€ํ•œ ์‚ฌ์ „ ์ง€์‹์„ ํ•„์š”๋กœ ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ๋‘˜ ์ค‘ ํ•˜๋‚˜์— ์ต์ˆ™ํ•˜๋ฉด ๋ถ„๋ช… ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์„ ๋งˆ์นœ ํ›„์—๋Š” ์•Œ์•„์•ผ ํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š” ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ(naive Bayes) ๋ฐ LSTM๊ณผ ๊ฐ™์€ ๊ธฐ์กด NLP ๋ชจ๋ธ๋“ค์„ ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ณ  ์žˆ๋Š” DeepLearning.AI์˜ Natural Language Processing Specialization์„ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ธฐ๋ฅผ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค! ์ €์ž ์†Œ๊ฐœ Matthew Carrigan ์€ Hugging Face์˜ ๊ธฐ๊ณ„ ํ•™์Šต ์—”์ง€๋‹ˆ์–ด์ž…๋‹ˆ๋‹ค. ์•„์ผ๋žœ๋“œ ๋”๋ธ”๋ฆฐ์— ๊ฑฐ์ฃผํ•˜๋ฉฐ ์ด์ „์—๋Š” Parse.ly์—์„œ ML ์—”์ง€๋‹ˆ์–ด๋กœ ๊ทผ๋ฌดํ–ˆ๊ณ  ๊ทธ์ „์—๋Š” Trinity College Dublin์—์„œ ๋ฐ•์‚ฌํ›„ ์—ฐ๊ตฌ์›์œผ๋กœ ๊ทผ๋ฌดํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Š” ์šฐ๋ฆฌ๊ฐ€ ๊ธฐ์กด ์•„ํ‚คํ…์ฒ˜๋ฅผ ํ™•์žฅํ•˜์—ฌ ๋ฒ”์šฉ ์ธ๊ณต ์ง€๋Šฅ(AGI, Artificial General Intelligence)์— ๋„๋‹ฌํ•  ๊ฒƒ์ด๋ผ๊ณ  ๋ฏฟ์ง€๋Š” ์•Š์ง€๋งŒ, Robot Immortality์— ๋Œ€ํ•œ ๊ฐ•ํ•œ ํฌ๋ง์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Lysandre Debut๋Š” Hugging Face์˜ ๊ธฐ๊ณ„ ํ•™์Šต ์—”์ง€๋‹ˆ์–ด์ด๋ฉฐ ์ดˆ๊ธฐ ๊ฐœ๋ฐœ ๋‹จ๊ณ„๋ถ€ํ„ฐ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐœ๋ฐœํ•ด ์™”์Šต๋‹ˆ๋‹ค. ๊ทธ์˜ ๋ชฉํ‘œ๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•œ API๋กœ ๋„๊ตฌ๋ฅผ ๊ฐœ๋ฐœํ•˜์—ฌ ๋ชจ๋“  ์‚ฌ๋žŒ์ด NLP์— ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Sylvain Gugger๋Š” Hugging Face์˜ ์—ฐ๊ตฌ์›(Research Engineer)์ด ์ž Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ•ต์‹ฌ ๊ด€๋ฆฌ์ž ์ค‘ ํ•œ ๋ช…์ž…๋‹ˆ๋‹ค. ์ด์ „์— ๊ทธ๋Š” fast.ai์—์„œ ์—ฐ๊ตฌ ๊ณผํ•™์ž(Research Scientist)๋กœ ๊ทผ๋ฌดํ–ˆ์œผ๋ฉฐ, Jeremy Howard์™€ ํ•จ๊ป˜ Deep Learning for Coders with fastai and PyTorch๋ฅผ ๊ณต๋™ ์ €์ˆ ํ–ˆ์Šต๋‹ˆ๋‹ค. Merve Noyan ์€ Hugging Face์˜ ๋””๋ฒจ๋กœํผ ์—๋“œ๋ณด์ผ€์ดํŠธ(developer advocate)๋กœ์„œ ๊ฐ์ข… ๋„๊ตฌ๋“ค์„ ๊ฐœ๋ฐœํ•˜๊ณ  ๊ฐ ๋„๊ตฌ๋“ค์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ฝ˜ํ…์ธ ๋“ค์„ ๊ตฌ์ถ•ํ•˜์—ฌ ๊ธฐ๊ณ„ ํ•™์Šต ๊ธฐ์ˆ ์ด ๋ชจ๋“  ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๊ณตํ‰ํ•˜๊ฒŒ ํ™œ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Lucile Saulnier๋Š” Hugging Face์˜ ๊ธฐ๊ณ„ ํ•™์Šต ์—”์ง€๋‹ˆ์–ด๋กœ ์˜คํ”ˆ ์†Œ์Šค ๋„๊ตฌ๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋…€๋Š” ๋˜ํ•œ collaborative training ๋ฐ BigScience์™€ ๊ฐ™์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ถ„์•ผ์˜ ๋งŽ์€ ์—ฐ๊ตฌ ํ”„๋กœ์ ํŠธ์— ์ ๊ทน์ ์œผ๋กœ ์ฐธ์—ฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Lewis Tunstall ์€ Hugging Face์˜ ๊ธฐ๊ณ„ ํ•™์Šต ์—”์ง€๋‹ˆ์–ด๋กœ ์˜คํ”ˆ ์†Œ์Šค ๋„๊ตฌ๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ๋” ๋งŽ์€ ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ์ด๋ฅผ ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š”๋ฐ ์ง‘์ค‘ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Š” ๋˜ํ•œ ๊ณง ์ถœ์‹œ๋  O'Reilly์˜ Transformers์— ๋Œ€ํ•œ ๋„์„œ์˜ ๊ณต๋™ ์ €์ž์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. Leandro von Werra๋Š” Hugging Face์˜ ์˜คํ”ˆ ์†Œ์Šค ํŒ€์˜ ๊ธฐ๊ณ„ ํ•™์Šต ์—”์ง€๋‹ˆ์–ด์ด์ž ๊ณง ์ถœ์‹œ๋  O'Reilly์˜ Transformers์— ๋Œ€ํ•œ ๋„์„œ์˜ ๊ณต๋™ ์ €์ž์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Š” ๊ธฐ๊ณ„ํ•™์Šต ๊ธฐ๋ฐ˜์˜ NLP ํ”„๋กœ์ ํŠธ๋ฅผ ์ œํ’ˆ์œผ๋กœ ์‹คํ˜„์‹œํ‚ค๋Š” ์ˆ˜๋…„๊ฐ„์˜ ์—…๊ณ„ ๊ฒฝํ—˜์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์žฅ(Chapter 1)์—์„œ๋Š” ๋‹ค์Œ์˜ ๋‚ด์šฉ์„ ๋ฐฐ์›๋‹ˆ๋‹ค: pipeline() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ์ƒ์„ฑ ๋ฐ ๋ถ„๋ฅ˜์™€ ๊ฐ™์€ NLP ์ž‘์—…์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ• Transformer ์•„ํ‚คํ…์ฒ˜ ์ธ์ฝ”๋”(encoder), ๋””์ฝ”๋”(decoder) ๋ฐ ์ธ์ฝ”๋”-๋””์ฝ”๋”(encoder-decoder) ์•„ํ‚คํ…์ฒ˜์˜ ๊ตฌ๋ถ„ ๋ฐ ๊ฐ ์•„ํ‚คํ…์ฒ˜์˜ ํ™œ์šฉ ๋ฐฉ์•ˆ 1. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ (Natural Language Processing) ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๊ณต๋ถ€ํ•˜๊ธฐ ์ „์—, ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(Natural Language Processing)๊ฐ€ ๋ฌด์—‡์ด๊ณ , ์™œ ์šฐ๋ฆฌ๊ฐ€ ์ด ๊ธฐ์ˆ ์— ๋Œ€ํ•ด์„œ ๊ด€์‹ฌ์„ ๊ฐ€์ ธ์•ผ ํ•˜๋Š”์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค. NLP๊ฐ€ ๋ฌด์—‡์ธ๊ฐ€์š”? NLP๋Š” ์ธ๊ฐ„์˜ ์–ธ์–ด(human language)์™€ ๊ด€๋ จ๋œ ๋ชจ๋“  ๊ฒƒ์„ ์ดํ•ดํ•˜๋Š” ๋ฐ ์ค‘์ ์„ ๋‘” ์–ธ์–ดํ•™(linguistics) ๋ฐ ๊ธฐ๊ณ„ ํ•™์Šต(machine learning)์˜ ํ•œ ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. NLP ์ž‘์—…์€ ๋‹จ์ผ ๋‹จ์–ด๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์€ ๋ฌผ๋ก  ํ•ด๋‹น ๋‹จ์–ด์˜ ์ปจํ…์ŠคํŠธ, ์ฆ‰ ์ฃผ๋ณ€ ๋ฌธ๋งฅ๋„ ํ•จ๊ป˜ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ผ๋ฐ˜์ ์ธ NLP ํƒœ์Šคํฌ ์ข…๋ฅ˜์™€ ๊ทธ ์˜ˆ์‹œ๋“ค์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ฌธ์žฅ์„ ๋ถ„๋ฅ˜ํ•˜๊ธฐ(Classifying whole sentences): ๋ฆฌ๋ทฐ(review)์˜ ๊ฐ์ •(sentiment)์„ ์‹๋ณ„ํ•˜๊ณ , ์ด๋ฉ”์ผ์ด ์ŠคํŒธ์ธ์ง€ ๊ฐ์ง€ํ•˜๊ณ , ๋ฌธ์žฅ์ด ๋ฌธ๋ฒ•์ ์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ์ง€ ๋˜๋Š” ๋‘ ๋ฌธ์žฅ์ด ๋…ผ๋ฆฌ์ ์œผ๋กœ ๊ด€๋ จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ผ ๋ฌธ์žฅ์—์„œ ๊ฐ ๋‹จ์–ด๋ฅผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ(Classifying each word in a sentence): ๋ฌธ์žฅ์˜ ๋ฌธ๋ฒ•์  ๊ตฌ์„ฑ์š”์†Œ(๋ช…์‚ฌ, ๋™์‚ฌ, ํ˜•์šฉ์‚ฌ) ๋˜๋Š” ๋ช…๋ช…๋œ ๊ฐœ์ฒด(๊ฐœ์ฒด๋ช…, e.g., ์‚ฌ๋žŒ, ์œ„์น˜, ์กฐ์ง) ์‹๋ณ„ ํ…์ŠคํŠธ ์ฝ˜ํ…์ธ  ์ƒ์„ฑํ•˜๊ธฐ(Generating text content): ์ž๋™ ์ƒ์„ฑ๋œ ํ…์ŠคํŠธ๋กœ ํ”„๋กฌํ”„ํŠธ ์™„์„ฑ(completing a prompt), ๋งˆ์Šคํ‚น ๋œ ๋‹จ์–ด(masked words)๋กœ ํ…์ŠคํŠธ์˜ ๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ ํ…์ŠคํŠธ์—์„œ ์ •๋‹ต ์ถ”์ถœํ•˜๊ธฐ(Extracting an answer from a text): ์งˆ๋ฌธ(question)๊ณผ ๋งฅ๋ฝ(context)์ด ์ฃผ์–ด์ง€๋ฉด, ๋งฅ๋ฝ์—์„œ ์ œ๊ณต๋œ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ถ”์ถœ ์ž…๋ ฅ ํ…์ŠคํŠธ์—์„œ ์ƒˆ๋กœ์šด ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๊ธฐ(Generating a new sentence from an input text): ํ…์ŠคํŠธ๋ฅผ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ๋ฒˆ์—ญ(translation), ํ…์ŠคํŠธ ์š”์•ฝ(summarization) NLP๋Š” ๋ฌธ์–ด์  ํ…์ŠคํŠธ(written text) ์ฒ˜๋ฆฌ์— ๊ตญํ•œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. NLP๋Š” ์˜ค๋””์˜ค ์ƒ˜ํ”Œ์˜ ์Šคํฌ๋ฆฝํŠธ(transcript) ๋˜๋Š” ์ด๋ฏธ์ง€ ์„ค๋ช…(image caption) ์ƒ์„ฑ๊ณผ ๊ฐ™์€ ์Œ์„ฑ ์ธ์‹(speech recognition) ๋ฐ ์ปดํ“จํ„ฐ ๋น„์ „(computer vision) ๋“ฑ์˜ ๋ณต์žกํ•œ ๋ฌธ์ œ๋„ ๋˜ํ•œ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. NLP๋Š” ์™œ ์–ด๋ ค์šด๊ฐ€์š”? ์ปดํ“จํ„ฐ๋Š” ์ธ๊ฐ„๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์šฐ๋ฆฌ๊ฐ€ โ€œ๋ฐฐ๊ณ ํ”„๋‹ค(I am hungry)โ€๋ผ๋Š” ๋ฌธ์žฅ์„ ์ฝ์œผ๋ฉด, ์ธ๊ฐ„์€ ๊ทธ ์˜๋ฏธ๋ฅผ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, "๋‚˜๋Š” ๋ฐฐ๊ณ ํ”„๋‹ค(I am hungry)"์™€ "๋‚˜๋Š” ์Šฌํ”„๋‹ค(I am sad)"์™€ ๊ฐ™์€ ๋‘ ๋ฌธ์žฅ์ด ์ฃผ์–ด์ง€๋ฉด ์šฐ๋ฆฌ๋Š” ๋‘ ๋ฌธ์žฅ์ด ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€๋ฅผ ์‰ฝ๊ฒŒ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ ํ•™์Šต(Machine Learning, ML) ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ ์ด๋Ÿฌํ•œ ์ผ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ๊ฐ€ ๋” ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, ํ…์ŠคํŠธ๊ฐ€ ๋ชจ๋ธ์ด ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์–ธ์–ด๊ฐ€ ๋ณต์žกํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ๊ฐ„์€ ์ด ์ฒ˜๋ฆฌ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ์‹ ์ค‘ํ•˜๊ฒŒ ์ƒ๊ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ํ…์ŠคํŠธ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•(how to represent text)์— ๋Œ€ํ•œ ๋งŽ์€ ์—ฐ๊ตฌ๊ฐ€ ์ง„ํ–‰๋˜์—ˆ์œผ๋ฉฐ, ๋‹ค์Œ ์„น์…˜์—์„œ ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. Transformers๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ๋“ค ์ด ์„น์…˜์—์„œ๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์ด ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๊ณ , Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ฒซ ๋ฒˆ์งธ ๋„๊ตฌ์ธ pipeline() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Transformers๋Š” ์–ด๋””์—๋“  ์กด์žฌํ•ฉ๋‹ˆ๋‹ค! ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์€ ์ด์ „ ์„น์…˜์—์„œ ์–ธ๊ธ‰ํ•œ ๋ชจ๋“  ์ข…๋ฅ˜์˜ NLP ์ž‘์—…์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ Hugging Face ๋ฐ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ช‡๋ช‡ ํšŒ์‚ฌ ๋ฐ ์กฐ์ง๋“ค์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ ์ž์‹ ๋“ค์ด ๋งŒ๋“  ๋ชจ๋ธ๋“ค์„<NAME>์—ฌ ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋‹ค์‹œ ๊ธฐ์—ฌํ•ฉ๋‹ˆ๋‹ค Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊ณต์œ ๋œ ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Model Hub์—๋Š” ๋ˆ„๊ตฌ๋‚˜ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜์ฒœ ๊ฐœ์˜ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ(pretrained models)๋“ค์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ž์‹ ์˜ ๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค! โš  The Hugging Face Hub๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์—๋งŒ ๊ตญํ•œํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ˆ„๊ตฌ๋‚˜ ์›ํ•˜๋Š” ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ชจ๋ธ์ด๋‚˜ ๋ฐ์ดํ„ฐ ์…‹(datasets)์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋ ค๋ฉด huggingface.co ๊ณ„์ •์„ ๋งŒ๋“œ์„ธ์š”! ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์ด ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์ „์— ๋ช‡ ๊ฐ€์ง€ ํฅ๋ฏธ๋กœ์šด NLP ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ(pipeline) ํ™œ์šฉํ•˜๊ธฐ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฐ์ฒด๋Š” pipeline() ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ํŠน์ • ๋ชจ๋ธ๊ณผ ๋™์ž‘์— ํ•„์š”ํ•œ ์ „์ฒ˜๋ฆฌ ๋ฐ ํ›„์ฒ˜๋ฆฌ ๋‹จ๊ณ„๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ๋‹ต๋ณ€์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from transformers import pipeline classifier = pipeline("sentiment-analysis") classifier("I've been waiting for a HuggingFace course my whole life.") ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ๋™์‹œ์— ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค! classifier(["I've been waiting for a HuggingFace course my whole life.", "I hate this so much!"]) ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด ํŒŒ์ดํ”„๋ผ์ธ์€ ์˜์–ด ๋ฌธ์žฅ์— ๋Œ€ํ•œ ๊ฐ์ • ๋ถ„์„(sentiment analysis)์„ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ •๋œ(fine-tuned) ์‚ฌ์ „ ํ›ˆ๋ จ ๋ชจ๋ธ(pretrained model)์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ classifier ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ•ด๋‹น ๋ชจ๋ธ์ด ๋‹ค์šด๋กœ๋“œ๋˜๊ณ  ์บ์‹œ ๋ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ๋œ classifier ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ์‹คํ–‰ํ•˜๋ฉด ์บ์‹œ ๋œ ๋ชจ๋ธ์ด ๋Œ€์‹  ์‚ฌ์šฉ๋˜๋ฉฐ ๋ชจ๋ธ์„ ๋‹ค์‹œ ๋‹ค์šด๋กœ๋“œํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์— ํ…์ŠคํŠธ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด 3๊ฐ€์ง€ ์ฃผ์š” ๋‹จ๊ณ„๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋Š” ๋ชจ๋ธ์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š”<NAME>์œผ๋กœ ์ „์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค(preprocessing). ์ „์ฒ˜๋ฆฌ ์™„๋ฃŒ๋œ ์ž…๋ ฅ ํ…์ŠคํŠธ๋Š” ๋ชจ๋ธ์— ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•œ ๊ฒฐ๊ณผ๋Š” ํ›„์ฒ˜๋ฆฌ๋˜์–ด(postprocessing) ์šฐ๋ฆฌ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ํ™œ์šฉ ๊ฐ€๋Šฅํ•œ ๋ช‡ ๊ฐ€์ง€ ํŒŒ์ดํ”„๋ผ์ธ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: feature-extraction (ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ๋ฒกํ„ฐ ํ‘œํ˜„ ์ œ๊ณต) fill-mask ner (named entity recognition, ๊ฐœ์ฒด๋ช… ์ธ์‹) question-answering sentiment-analysis summarization text-generation translation zero-shot-classification ์ด ์ค‘ ๋ช‡ ๊ฐ€์ง€๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! Zero-shot ๋ถ„๋ฅ˜ ์šฐ์„ , ๋ ˆ์ด๋ธ”์ด ์ง€์ •๋˜์ง€ ์•Š์€ ํ…์ŠคํŠธ๋ฅผ ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•˜๋Š” ๋” ์–ด๋ ค์šด ์ž‘์—…๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ์— ์ฃผ์„(annotation)์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆฌ๊ณ  ๋ถ„์•ผ ์ „๋ฌธ ์ง€์‹(domain expertise)์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ž‘์—…์€ ์‹ค์ œ ํ”„๋กœ์ ํŠธ์—์„œ ๋งค์šฐ ํ”ํ•œ ์‹œ๋‚˜๋ฆฌ์˜ค์ž…๋‹ˆ๋‹ค. ์ด ํ™œ์šฉ ์‚ฌ๋ก€์˜ ๊ฒฝ์šฐ, zero-shot-classification ํŒŒ์ดํ”„๋ผ์ธ์€ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ํ•ด๋‹น ๋ถ„๋ฅ˜์— ์‚ฌ์šฉํ•  ๋ ˆ์ด๋ธ”์„ ์ง์ ‘ ๋งˆ์Œ๋Œ€๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์˜ ๋ ˆ์ด๋ธ” ์ง‘ํ•ฉ์— ์˜์กดํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ ์šฐ๋ฆฌ๋Š” ํ•ด๋‹น ๋ชจ๋ธ์ด ๋‘ ๋ ˆ์ด๋ธ”(๊ธ์ •, ๋ถ€์ •)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์žฅ์„ ๊ธ์ • ๋˜๋Š” ๋ถ€์ •์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ด๋ฏธ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ชจ๋ธ์„ ์ด์šฉํ•ด์„œ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ ˆ์ด๋ธ” ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ๋ถ„๋ฅ˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. from transformers import pipeline classifier = pipeline("zero-shot-classification") classifier( "This is a course about the Transformers library", candidate_labels=["education", "politics", "business"], ) ์œ„์™€ ๊ฐ™์ด ์™„์ „ํžˆ ๋‹ค๋ฅธ ์ƒˆ๋กœ์šด ๋ ˆ์ด๋ธ” ์ง‘ํ•ฉ์œผ๋กœ ๋ฌธ์žฅ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ๋„ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— zero-shot ๋ถ„๋ฅ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์‹œ์—์„œ ๋ณด๋Š” ๋ฐ”์™€ ๊ฐ™์ด ์›ํ•˜๋Š” ๋ ˆ์ด๋ธ” ๋ชฉ๋ก์— ๋Œ€ํ•œ ํ™•๋ฅ  ์ ์ˆ˜๋ฅผ ์ง์ ‘ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค! ํ…์ŠคํŠธ ์ƒ์„ฑ ์ด์ œ ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ์š” ์•„์ด๋””์–ด๋Š” ์ž…๋ ฅ์œผ๋กœ ํŠน์ • ํ”„๋กฌํ”„ํŠธ(prompt)๋ฅผ ์ œ๊ณตํ•˜๋ฉด ๋ชจ๋ธ์ด ๋‚˜๋จธ์ง€ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž๋™ ์™„์„ฑํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ „ํ™”๊ธฐ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํ…์ŠคํŠธ ์˜ˆ์ธก ๊ธฐ๋Šฅ(predictive text feature)๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์ƒ์„ฑ์€ ๋žœ๋คํ•˜๊ฒŒ ์ˆ˜ํ–‰๋˜๋ฏ€๋กœ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ถœ๋ ฅ์ด ์•„๋ž˜ ๊ฒฐ๊ณผ์™€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์€ ์ •์ƒ์ž…๋‹ˆ๋‹ค. from transformers import pipeline generator = pipeline("text-generation") generator("In this course, we will teach you how to") ์œ„์˜ generator ๊ฐ์ฒด์— num_return_sequences ์ธ์ž(argument)๋ฅผ ์ง€์ •ํ•˜์—ฌ ์ƒ์„ฑ๋˜๋Š” ์‹œํ€€์Šค์˜ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๊ณ , max_length ์ธ์ž๋กœ๋Š” ์ถœ๋ ฅ ํ…์ŠคํŠธ์˜ ์ด ๊ธธ์ด๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ํ—ˆ๋ธŒ์˜ ๋ชจ๋“  ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค! ์ด์ „ ์˜ˆ์ œ์—์„œ๋Š” ํ˜„์žฌ ์ž‘์—…์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ๋ชจ๋ธ(default model)์„ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ํ—ˆ๋ธŒ์—์„œ ์—ฌ๋Ÿฌ๋ถ„์ด ์›ํ•˜๋Š” ๋ชจ๋ธ์„ ์„ ํƒํ•˜์—ฌ ํŠน์ • ์ž‘์—…(์˜ˆ: ํ…์ŠคํŠธ ์ƒ์„ฑ)์— ๋Œ€ํ•œ ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ ํ—ˆ๋ธŒ(Model Hub)๋กœ ์ด๋™ํ•˜์—ฌ ์™ผ์ชฝ์— ์žˆ๋Š” ํŠน์ • ํƒœ๊ทธ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๊ด€๋ จ ์ž‘์—…์„ ์ง€์›ํ•˜๋Š” ๋ชจ๋ธ๋งŒ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” ์ด ํŽ˜์ด์ง€์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž ์ด์ œ distilgpt2 ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค! ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ์ด ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. from transformers import pipeline generator = pipeline("text-generation", model="distilgpt2") # distilgpt2 ๋ชจ๋ธ์„ ๋กœ๋“œํ•œ๋‹ค. generator( "In this course, we will teach you how to", max_length=30, num_return_sequences=2, ) ์–ธ์–ด ํƒœ๊ทธ(language tags)๋ฅผ ํด๋ฆญํ•˜์—ฌ ๊ทธ ์–ธ์–ด์— ํŠนํ™”๋œ ๋ชจ๋ธ์„ ์„ธ๋ถ€์ ์œผ๋กœ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์„ ํƒํ•จ์œผ๋กœ์จ ์›ํ•˜๋Š” ์–ธ์–ด๋กœ ํ‘œํ˜„๋œ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Model Hub์—๋Š” ๋‹ค์ค‘ ์–ธ์–ด๋ฅผ ์ง€์›ํ•˜๋Š” ๋‹ค๊ตญ์–ด ๋ชจ๋ธ(multilingual models)์— ๋Œ€ํ•œ ์ฒดํฌํฌ์ธํŠธ๋„ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ๋ชจ๋ธ์„ ํด๋ฆญํ•˜์—ฌ ์„ ํƒํ•˜๋ฉด ์˜จ๋ผ์ธ์—์„œ ์ง์ ‘ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์ ฏ(widget)์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค์šด๋กœ๋“œํ•˜๊ธฐ ์ „์— ๊ทธ ๋ชจ๋ธ์˜ ๊ธฐ๋Šฅ์„ ๋น ๋ฅด๊ฒŒ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๋ก  API ๋ชจ๋“  ๋ชจ๋ธ์€ Hugging Face ์›น์‚ฌ์ดํŠธ์—์„œ ์ œ๊ณต๋˜๋Š” Inference API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ์ง์ ‘ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํŽ˜์ด์ง€์—์„œ ์ง์ ‘ ์ž„์˜์˜ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋ชจ๋ธ์ด ํ•ด๋‹น ํ…์ŠคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ์‚ดํŽด๋ณด๋ฉด์„œ ๋ชจ๋ธ๋“ค์„ ํ…Œ์ŠคํŠธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์ ฏ์„ ๊ตฌ๋™ํ•˜๋Š” Inference API๋Š” ์œ ๋ฃŒ ์ œํ’ˆ์œผ๋กœ๋„ ์ œ๊ณต๋˜๋ฏ€๋กœ ์‹ค๋ฌด์ ์œผ๋กœ๋„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๊ฐ€๊ฒฉ ์ฑ…์ • ํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. Mask filling ๋‹ค์Œ์œผ๋กœ ์‹œ๋„ํ•  ํŒŒ์ดํ”„๋ผ์ธ์€ fill-mask์ž…๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ์ฃผ์–ด์ง„ ํ…์ŠคํŠธ์˜ ๊ณต๋ฐฑ์„ ์ฑ„์šฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. from transformers import pipeline unmasker = pipeline("fill-mask") unmasker("This course will teach you all about <mask> models.", top_k=2) top_k ์ธ์ž(argument)๋Š” ์ถœ๋ ฅํ•  ๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ ์ข…๋ฅ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ชจ๋ธ์€ ๋งˆ์Šคํฌ ํ† ํฐ(mask token)์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ํŠน์ˆ˜ํ•œ <mask> ๋‹จ์–ด๋ฅผ ์ฑ„์›๋‹ˆ๋‹ค. ๋งˆ์Šคํฌ ์ฑ„์šฐ๊ธฐ(mask-filling) ๋ชจ๋ธ์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธ ๋งˆ์Šคํฌ ํ† ํฐ์„ ์š”๊ตฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋‹ค๋ฅธ ๋ชจ๋ธ์„ ํƒ์ƒ‰ํ•  ๋•Œ ํ•ญ์ƒ ํ•ด๋‹น ๋งˆ์Šคํฌ ํ† ํฐ์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ™•์ธํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ์œ„์ ฏ์— ์‚ฌ์šฉ๋œ ๋งˆ์Šคํฌ ํ† ํฐ์„ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐœ์ฒด๋ช… ์ธ์‹(Named entity recognition) ๊ฐœ์ฒด๋ช… ์ธ์‹(NER, Named Entity Recognition)์€ ์ž…๋ ฅ ํ…์ŠคํŠธ์—์„œ ์–ด๋Š ๋ถ€๋ถ„์ด ์‚ฌ๋žŒ, ์œ„์น˜ ๋˜๋Š” ์กฐ์ง๊ณผ ๊ฐ™์€ ๊ฐœ์ฒด๋ช…์— ํ•ด๋‹นํ•˜๋Š”์ง€ ์‹๋ณ„ํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from transformers import pipeline ner = pipeline("ner", grouped_entities=True) ner("My name is Sylvain and I work at Hugging Face in Brooklyn.") ์—ฌ๊ธฐ์„œ ๋ชจ๋ธ์€ "Sylvain"์ด ์‚ฌ๋žŒ(PER)์ด๊ณ  "Hugging Face"๊ฐ€ ์กฐ์ง(ORG)์ด๋ฉฐ "Brooklyn"์ด ์œ„์น˜(LOC) ์ž„์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์‹๋ณ„ํ–ˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ ์ƒ์„ฑ ํ•จ์ˆ˜์—์„œ grouped_entities=True ์˜ต์…˜์„ ์ „๋‹ฌํ•˜์—ฌ ํŒŒ์ดํ”„๋ผ์ธ์ด ๋™์ผํ•œ ์—”ํ‹ฐํ‹ฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์žฅ์˜ ๋ถ€๋ถ„(ํ† ํฐ ํ˜น์€ ๋‹จ์–ด)๋“ค์„ ๊ทธ๋ฃนํ™”ํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ชจ๋ธ์€ "Hugging"๊ณผ "Face"๋ฅผ ๋‹จ์ผ ์กฐ์ง(ORG)์œผ๋กœ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ทธ๋ฃนํ™”ํ–ˆ์ง€๋งŒ ์ด๋ฆ„ ์ž์ฒด๋Š” ์—ฌ๋Ÿฌ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค, ๋‹ค์Œ ์žฅ์—์„œ ๋ณด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ, ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ ์‹ฌ์ง€์–ด ์ผ๋ถ€ ๋‹จ์–ด๋ฅผ ๋” ์ž‘์€ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Sylvain์€ S, ##yl, ##va ๋ฐ ##in์˜ ๋„ค ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ํ›„์ฒ˜๋ฆฌ ๋‹จ๊ณ„์—์„œ ํŒŒ์ดํ”„๋ผ์ธ์€ ํ•ด๋‹น ์กฐ๊ฐ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์žฌ๊ทธ๋ฃนํ™”ํ–ˆ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋กœ "Sylvain"์ด ๋‹จ์ผ ๋‹จ์–ด๋กœ ์ถœ๋ ฅ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์งˆ์˜์‘๋‹ต(Question Answering) question-answering ํŒŒ์ดํ”„๋ผ์ธ์€ ์ฃผ์–ด์ง„ ์ปจํ…์ŠคํŠธ(context) ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ ์งˆ๋ฌธ์— ์‘๋‹ต์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. from transformers import pipeline question_answerer = pipeline("question-answering") question_answerer( question="Where do I work?", context="My name is Sylvain and I work at Hugging Face in Brooklyn", ) ์ด ํŒŒ์ดํ”„๋ผ์ธ์€ ์ œ๊ณต๋œ ์ปจํ…์ŠคํŠธ์—์„œ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์‘๋‹ต์„ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์ž๋™ ์š”์•ฝ(Summarization) ์š”์•ฝ(summarization)์€ ํ…์ŠคํŠธ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“ (๋˜๋Š” ๋Œ€๋ถ€๋ถ„์˜) ์ค‘์š”ํ•œ ๋‚ด์šฉ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ํ•ด๋‹น ํ…์ŠคํŠธ๋ฅผ ๋” ์งง์€ ํ…์ŠคํŠธ๋กœ ์ค„์ด๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from transformers import pipeline summarizer = pipeline("summarization") summarizer( """ America has changed dramatically during recent years. Not only has the number of graduates in traditional engineering disciplines such as mechanical, civil, electrical, chemical, and aeronautical engineering declined, but in most of the premier American universities engineering curricula now concentrate on and encourage largely the study of engineering science. As a result, there are declining offerings in engineering subjects dealing with infrastructure, the environment, and related issues, and greater concentration on high technology subjects, largely supporting increasingly complex scientific developments. While the latter is important, it should not be at the expense of more traditional engineering. Rapidly developing economies such as China and India, as well as other industrial countries in Europe and Asia, continue to encourage and advance the teaching of engineering. Both China and India, respectively, graduate six and eight times as many traditional engineers as does the United States. Other industrial countries at minimum maintain their output, while America suffers an increasingly serious decline in the number of engineering graduates and a lack of well-educated engineers. """ ) ํ…์ŠคํŠธ ์ƒ์„ฑ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์˜ต์…˜์œผ๋กœ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด max_length ๋˜๋Š” min_length ์ง€์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ(Translation) ๋ฒˆ์—ญ(Translation)์˜ ๊ฒฝ์šฐ ์ž‘์—…(task) ์ด๋ฆ„์— ์–ธ์–ด ์Œ(์˜ˆ: "translation_en_to_fr")์„ ์ง€์ •ํ•˜๋ฉด ์‹œ์Šคํ…œ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋ธ(default model)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ Model Hub์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๋ชจ๋ธ์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ์—์„œ ํ”„๋ž‘์Šค์–ด์—์„œ ์˜์–ด๋กœ ๋ฒˆ์—ญ์„ ์‹œ๋„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from transformers import pipeline translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en") translator("Ce cours est produit par Hugging Face.") ํ…์ŠคํŠธ ์ƒ์„ฑ ๋ฐ ์š”์•ฝ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์˜ต์…˜์œผ๋กœ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด max_length ๋˜๋Š” min_length ์ง€์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์‚ดํŽด๋ณธ ํŒŒ์ดํ”„๋ผ์ธ์€ ๋Œ€๋ถ€๋ถ„ ๋ฐ๋ชจ์šฉ์ž…๋‹ˆ๋‹ค. ํŠน์ • ์ž‘์—…(specific tasks)์„ ์œ„ํ•ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋˜์—ˆ์œผ๋ฉฐ ๋ณ€ํ˜•๋œ ์ž‘์—…์ด๋‚˜ ๋” ๋ณต์žกํ•œ ์ž‘์—…์€ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์„น์…˜์—์„œ๋Š” pipeline() ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์–ด๋– ํ•œ ๊ธฐ๋Šฅ ๋ฐ ๋™์ž‘๋“ค์ด ์กด์žฌํ•˜๊ณ  ๊ทธ๊ฒƒ๋“ค์„ ์–ด๋–ป๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3. Transformers๋Š” ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”๊ฐ€? ์ด ์„น์…˜์—์„œ๋Š” Transformer ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ฐœ๋žต์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Transformer์˜ ์งง์€ ์—ญ์‚ฌ ๋‹ค์Œ์€ Transformer ๋ชจ๋ธ์˜ (์งง์€) ์—ญ์‚ฌ์—์„œ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์ฃผ์š” ๋ชจ๋ธ๋“ค์ž…๋‹ˆ๋‹ค. Transformer ์•„ํ‚คํ…์ฒ˜๋Š” 2017๋…„ 6์›”์— ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์›๋ž˜ ์ด ์•„ํ‚คํ…์ฒ˜ ์—ฐ๊ตฌ์˜ ์ดˆ์ ์€ ๊ธฐ๊ณ„๋ฒˆ์—ญ(machine translation)์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ดˆ ๋ชจ๋ธ์ด ์†Œ๊ฐœ๋œ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ๊ฐ•๋ ฅํ•˜๊ณ  ์šฐ์ˆ˜ํ•œ ๋ชจ๋ธ๋“ค์ด ์ถ”๊ฐ€์ ์œผ๋กœ ๋„์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 2018๋…„ 6์›”: GPT, ์ตœ์ดˆ์˜ ์‚ฌ์ „ ํ•™์Šต๋œ(pretrained) Transformer ๋ชจ๋ธ. ๋‹ค์–‘ํ•œ NLP ์ž‘์—…์— ๋Œ€ํ•œ ๋ฏธ์„ธ ์กฐ์ •์— ์‚ฌ์šฉ๋˜์—ˆ๊ณ  ๊ทธ ๋‹น์‹œ ๋งŽ์€ ํƒœ์Šคํฌ์—์„œ ์ตœ๊ณ  ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑ 2018๋…„ 10์›”: BERT, ๋˜ ๋‹ค๋ฅธ ๋Œ€๊ทœ๋ชจ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ. ์ด ๋ชจ๋ธ์€ ํŠนํžˆ ๊ณ  ์ˆ˜์ค€์˜ ๋ฌธ์žฅ ์š”์•ฝ์„ ์ œ๊ณตํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค(๋‹ค์Œ ์žฅ์—์„œ ๋” ์ž์„ธํžˆ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค!) 2019๋…„ 2์›”: GPT-2, ์œค๋ฆฌ์ ์ธ ๋ฌธ์ œ๋กœ ์ธํ•ด ์ฆ‰์‹œ ๊ณต๊ฐœ๋˜์ง€ ์•Š์€, ๊ธฐ์กด GPT๋ณด๋‹ค ๊ทœ๋ชจ๊ฐ€ ๋” ํฌ๊ณ  ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋œ GPT ๋ฒ„์ „ 2019๋…„ 10์›”: DistillBERT, ์†๋„๊ฐ€ 60% ๋” ๋น ๋ฅด๊ณ  ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๋Š” 40% ์ค„์˜€์ง€๋งŒ ์—ฌ์ „ํžˆ BERT ์„ฑ๋Šฅ์˜ 97%๋ฅผ ์œ ์ง€ํ•˜๋Š” ์ฆ๋ฅ˜๋œ(distilled) BERT ๋ฒ„์ „ 2019๋…„ 10์›”: BART ๋ฐ T5, ์›๋ž˜ Transformer ๋ชจ๋ธ๊ณผ ๋™์ผํ•œ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋‘ ๊ฐœ์˜ ๋Œ€๊ทœ๋ชจ ์‚ฌ์ „ ํ•™์Šต ๋ชจ๋ธ(์ˆœ์ˆ˜ Transformer ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•œ ์ตœ์ดˆ์˜ ์‚ฌ์ „ ํ•™์Šต ๋ชจ๋ธ) 2020๋…„ 5์›”: GPT-3, ๋ฏธ์„ธ ์กฐ์ • ์—†์ด๋„ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ํ›Œ๋ฅญํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” GPT-2์˜ ๋” ํฐ ๋ฒ„์ „์œผ๋กœ ์ œ๋กœ ์ƒท ํ•™์Šต(zero-shot learning)์ด๋ผ๊ณ  ํ•จ ๊ทธ ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์‚ฌ์ „ ํ•™์Šต ๋ชจ๋ธ์ด ์กด์žฌํ•˜๋ฉฐ, ์ด ๋ชฉ๋ก์€ ๋‹จ์ง€ ๋ช‡ ๊ฐ€์ง€ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ Transformer ๋ชจ๋ธ์„ ๊ฐ•์กฐํ•˜์—ฌ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต ๋ชจ๋ธ์€ ๋Œ€์ฒด๋กœ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ๋ฒ”์ฃผ๋กœ ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: GPT-like ๋ชจ๋ธ(auto-regressive Transformer ๋ชจ๋ธ) BERT-like ๋ชจ๋ธ(auto-encoding Transformer ๋ชจ๋ธ) BART/T5-like ๋ชจ๋ธ(__sequence-to_sequence __Transformer ๋ชจ๋ธ) ๋‚˜์ค‘์— ๊ฐ๊ฐ์˜ ๊ทธ๋ฃน๋ณ„๋กœ ์ข€ ๋” ์ƒ์„ธํ•˜๊ฒŒ ์•Œ์•„๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Transformers๋Š” ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๋ชจ๋“  Transformer ๋ชจ๋ธ(GPT, BERT, BART, T5 ๋“ฑ)์€ ์–ธ์–ด ๋ชจ๋ธ(language model)๋กœ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ทธ ๋ชจ๋ธ๋“ค์ด ์ž๊ฐ€ ์ง€๋„(self-supervised) ํ•™์Šต ๋ฐฉ์‹์œผ๋กœ ๋งŽ์€ ์–‘์˜ ์›์‹œ ํ…์ŠคํŠธ์— ๋Œ€ํ•ด ํ•™์Šต๋˜์—ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ž๊ฐ€ ์ง€๋„ ํ•™์Šต(self-supervised learning)์€ ๋ชฉ์  ํ•จ์ˆ˜(objectives)๊ฐ€ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์—์„œ ์ž๋™์œผ๋กœ ๊ณ„์‚ฐ๋˜๋Š” ํ•™์Šต ์œ ํ˜•์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์‚ฌ๋žŒ์ด ๋ฐ์ดํ„ฐ์— ๋ ˆ์ด๋ธ”์„ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค! ์ด๋Ÿฌํ•œ ์œ ํ˜•์˜ ๋ชจ๋ธ์€ ํ•™์Šต๋œ ์–ธ์–ด์— ๋Œ€ํ•œ ํ†ต๊ณ„์ ์ธ ์ดํ•ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜์ง€๋งŒ ์‹ค์ œ ํƒœ์Šคํฌ์—๋Š” ๊ทธ๋‹ค์ง€ ์œ ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ด์œ  ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์€ ์ „์ด ํ•™์Šต(transfer learning)์ด๋ผ๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฑฐ์น˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ํ”„๋กœ์„ธ์Šค ๋™์•ˆ ๋ชจ๋ธ์€ ์ฃผ์–ด์ง„ ์ž‘์—…์— ๋Œ€ํ•ด ๊ฐ๋…(supervised) ๋ฐฉ์‹ ์ฆ‰, ์‚ฌ๋žŒ์ด ์ฃผ์„์œผ๋กœ ์ถ”๊ฐ€ํ•œ ๋ ˆ์ด๋ธ”์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, n ๊ฐœ์˜ ์ด์ „ ๋‹จ์–ด๋ฅผ ์ฝ์€ ๋ฌธ์žฅ์—์„œ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•œ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ถœ๋ ฅํ•  ์˜ˆ์ธก๊ฐ’์€ ๊ณผ๊ฑฐ ๋ฐ ํ˜„์žฌ ์ž…๋ ฅ๊ฐ’์— ์˜์กดํ•˜์ง€๋งŒ ๋ฏธ๋ž˜ ์ž…๋ ฅ๊ฐ’์—๋Š” ์˜์กดํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ์„ causal language modeling์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋Š” ๋ชจ๋ธ์ด ๋ฌธ์žฅ์—์„œ ๋งˆ์Šคํฌ ๋œ ๋‹จ์–ด(masked word)๋ฅผ ์˜ˆ์ธกํ•˜๋Š” masked language modeling์ž…๋‹ˆ๋‹ค. Transformers๋Š” ๊ทœ๋ชจ๊ฐ€ ํฐ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์™ธ ๋ชจ๋ธ(์˜ˆ: DistilBERT)์„ ์ œ์™ธํ•˜๊ณ , ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ผ๋ฐ˜์ ์ธ ์ „๋žต์€ ๋ชจ๋ธ์˜ ํฌ๊ธฐ์™€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ถˆํ–‰ํžˆ๋„ ์‚ฌ์ „ ํ•™์Šต ๋ชจ๋ธ(pretrained model), ํŠนํžˆ ํฐ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋ ค๋ฉด ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์‹œ๊ฐ„๊ณผ ์ปดํ“จํŒ… ๋ฆฌ์†Œ์Šค ๋ฉด์—์„œ ๊ณ ๋น„์šฉ์„ ์ดˆ๋ž˜ํ•ฉ๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ์ด๋Ÿฌํ•œ ๋Œ€๊ทœ๋ชจ ๋ชจ๋ธ ํ•™์Šต์€ ๋‹ค์Œ ๊ทธ๋ž˜ํ”„์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ํ™˜๊ฒฝ์ ์ธ ๋ฌธ์ œ(environmental impact)๋ฅผ ์•ผ๊ธฐํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ž˜ํ”„์˜ ๋งจ ์•„๋ž˜์—์„œ ์‚ฌ์ „ ํ•™์Šต์˜ ํ™˜๊ฒฝ์  ์˜ํ–ฅ์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ์˜์‹์ ์œผ๋กœ ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ๋Š” ํ•œ ์—ฐ๊ตฌํŒ€์— ์˜ํ•ด์„œ ์ง„ํ–‰๋˜๋Š” ํ”„๋กœ์ ํŠธ์—์„œ ๋Œ€๊ทœ๋ชจ์˜ ์‚ฌ์ „ ํ•™์Šต์„ ์‹คํ–‰ํ•  ๋•Œ ๋ฐฐ์ถœ๋˜๋Š” ์ด์‚ฐํ™”ํƒ„์†Œ์˜ ์–‘์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ ์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ(hyperparameter)๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ๋งŽ์€ ํ•™์Šต ์‹œ๋„๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ํƒ„์†Œ ๋ฐœ์ž๊ตญ(carbon footprint)์€ ํ›จ์”ฌ ๋” ๋†’์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ์—ฐ๊ตฌ ํŒ€, ํ•™์ƒ ์กฐ์ง ๋˜๋Š” ํšŒ์‚ฌ๊ฐ€ ์ž์‹ ๋“ค์˜ ๋ชจ๋ธ์„ ์ง์ ‘ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‚ฌ์ „ ํ•™์Šตํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ณด์‹ญ์‹œ์˜ค. ์ด๋Š” ๊ด‘๋ฒ”์œ„ํ•˜๊ณ  ๋ถˆํ•„์š”ํ•œ ๊ธ€๋กœ๋ฒŒ ๋น„์šฉ์œผ๋กœ ์ด์–ด์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค! ์ด๊ฒƒ์ด ์–ธ์–ด ๋ชจ๋ธ์„ ๊ณต์œ ํ•ด์•ผ๋งŒ ํ•˜๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ด์œ ์ž…๋‹ˆ๋‹ค. ํ•™์Šต๋œ ๊ฐ€์ค‘์น˜(weights)๋ฅผ<NAME>๊ณ  ์ด๋ฏธ ํ•™์Šต๋œ ๊ฐ€์ค‘์น˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•˜์—ฌ ๋ชจ๋ธ์„ ๋งŒ๋“ค๋ฉด ์ปค๋ฎค๋‹ˆํ‹ฐ์˜ ์ „์ฒด ์ปดํ“จํŒ… ๋น„์šฉ๊ณผ ํƒ„์†Œ ๋ฐœ์ž๊ตญ(carbon footprint)์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ด ํ•™์Šต(Transfer Learning) ์‚ฌ์ „ ํ•™์Šต(Pretraining)์€ ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜(weight)๋Š” ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋˜๊ณ , ์‚ฌ์ „ ์ง€์‹(prior knowledge)์ด ์—†์ด ํ•™์Šต์ด ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค. ์ด ์‚ฌ์ „ ํ•™์Šต(pretraining)์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋งค์šฐ ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋งค์šฐ ํฐ ๊ทœ๋ชจ์˜ ๋ฐ์ดํ„ฐ ์ฝ”ํผ์Šค(corpus)๊ฐ€ ํ•„์š”ํ•˜๋ฉฐ ํ•™์Šต์—๋Š” ์ตœ๋Œ€ ๋ช‡ ์ฃผ๊ฐ€ ์†Œ์š”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด์—, ๋ฏธ์„ธ ์กฐ์ •(fine-tuning)์€ ๋ชจ๋ธ์ด ์‚ฌ์ „ ํ•™์Šต๋œ ํ›„์— ์ˆ˜ํ–‰๋˜๋Š” ํ•™์Šต์ž…๋‹ˆ๋‹ค. ๋ฏธ์„ธ ์กฐ์ •(fine-tuning)์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๋จผ์ € ์‚ฌ์ „ ํ•™์Šต๋œ ์–ธ์–ด ๋ชจ๋ธ(pretrained language model)์„ ํ™•๋ณดํ•œ ๋‹ค์Œ, ํŠน์ • ํƒœ์Šคํฌ์— ๋งž๋Š” ๋ฐ์ดํ„ฐ ์…‹(dataset)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๊ฐ€ ํ•™์Šต์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ž ๊น๋งŒ์š”! ์ตœ์ข… ํƒœ์Šคํฌ(task)๋ฅผ ์œ„ํ•ด ์ฒ˜์Œ๋ถ€ํ„ฐ ์ง์ ‘ ํ•™์Šตํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? ๋ช‡ ๊ฐ€์ง€ ์ด์œ ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ(pretrained model)์€ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning)์— ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ ์…‹๊ณผ ์œ ์‚ฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ด๋ฏธ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ๊ณผ์ •์—์„œ, ์‚ฌ์ „ ํ•™์Šต ๊ณผ์ •์—์„œ ์–ป์€ ์ง€์‹์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด, NLP ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์€ ์›ํ•˜๋Š” ํƒœ์Šคํฌ์— ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด์— ๋Œ€ํ•œ ์ผ์ข…์˜ ํ†ต๊ณ„์  ์ดํ•ด๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.) ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์€ ์ด๋ฏธ ๋งŽ์€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ํ•™์Šต๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฏธ์„ธ ์กฐ์ • ๊ณผ์ •์—์„œ ํ›จ์”ฌ ์ ์€ ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜๋”๋ผ๋„ ์ข‹์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ์ด์œ ๋กœ, ์ข‹์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„๊ณผ ์ž์›์€ ํ›จ์”ฌ ์ ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์˜์–ด๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ๊ฐ€์ง€๊ณ , arXiv ๋ง๋ญ‰์น˜์—์„œ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•˜์—ฌ ๊ณผํ•™/์—ฐ๊ตฌ(science/research) ๋ถ„์•ผ ๋ฐ์ดํ„ฐ์— ํŠนํ™”๋œ ๋ชจ๋ธ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ์„ธ ์กฐ์ •(fine-tuning)์—๋Š” ์ œํ•œ๋œ ์–‘์˜ ๋ฐ์ดํ„ฐ๋งŒ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์ด ํš๋“ํ•œ ์ง€์‹์€ "์ „์ด(transferred)"๋˜๋ฏ€๋กœ ์ „์ด ํ•™์Šต(transfer learning)์ด๋ผ๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•˜๋ฉด ์‹œ๊ฐ„(time), ๋ฐ์ดํ„ฐ(data), ์žฌ์ •(financial) ๋ฐ ํ™˜๊ฒฝ(environmental) ๋น„์šฉ์ด ์ ˆ๊ฐ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•™์Šต ๊ณผ์ •์ด ์ „์ฒด ์‚ฌ์ „ ํ›ˆ๋ จ(pretraining)๋ณด๋‹ค ์ œ์•ฝ์ด ์ ๊ธฐ ๋•Œ๋ฌธ์—, ๋ณด๋‹ค ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ๋‹ค์–‘ํ•œ ๋ฏธ์„ธ ์กฐ์ • ์ž‘์—…์„ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋งŒ์ผ ๋ณธ์ธ์ด ์›ํ•˜๋Š” ์ตœ์ข… ํƒœ์Šคํฌ์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถฉ๋ถ„์น˜ ์•Š์„ ๊ฒฝ์šฐ, ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ๊ณผ์ •์—์„œ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•๋ณด๋‹ค ๋” ๋‚˜์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•ญ์ƒ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ํ™œ์šฉํ•˜๊ณ  ์ด๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ์•„ํ‚คํ…์ฒ˜ ์—ฌ๊ธฐ์„œ๋Š” Transformer ๋ชจ๋ธ์˜ ์ผ๋ฐ˜์ ์ธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ผ๋ถ€ ๊ฐœ๋…์„ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๋”๋ผ๋„ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ๋‚˜์ค‘์— ๊ฐ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ž์„ธํžˆ ๋‹ค๋ฃจ๋Š” ์„น์…˜(section)์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ์š” (Introduction) ๋ชจ๋ธ์€ ์ฃผ๋กœ ๋‘ ๊ฐœ์˜ ๋ธ”๋ก์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”(Encoder) (์™ผ์ชฝ): ์ธ์ฝ”๋”๋Š” ์ž…๋ ฅ์— ๋Œ€ํ•œ ํ‘œํ˜„(representation) ํ˜น์€ ์ž์งˆ(feature)์„ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ดํ•ด๋ฅผ ์–ป๋„๋ก(acquire understanding from the input), ๋‹ค์‹œ ๋งํ•ด์„œ, ์ตœ์ข… ๋ชฉ์  ํƒœ์Šคํฌ๋ฅผ ์œ„ํ•ด์„œ ์ž…๋ ฅ์— ๋Œ€ํ•œ ํ‘œํ˜„ ํ˜•ํƒœ๊ฐ€ ์ตœ์ ํ™”๋˜์—ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋”(Decoder) (์˜ค๋ฅธ์ชฝ): ๋””์ฝ”๋”๋Š” ์ธ์ฝ”๋”๊ฐ€ ๊ตฌ์„ฑํ•œ ํ‘œํ˜„(representation) ํ˜น์€ ์ž์งˆ(feature)์„ ๋‹ค๋ฅธ ์ž…๋ ฅ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€์ƒ ์‹œํ€€์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด ์ถœ๋ ฅ ์ƒ์„ฑ(generating outputs)์— ์ตœ์ ํ™”๋˜์–ด ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋“ค ๊ฐ๊ฐ์˜ ๋ธ”๋ก์€ ์ž‘์—…์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๊ฐœ๋ณ„์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋” ์ „์šฉ ๋ชจ๋ธ(Encoder-only models): ๋ฌธ์žฅ ๋ถ„๋ฅ˜(sentence classification) ๋ฐ ๊ฐœ์ฒด๋ช… ์ธ์‹(named-entity recognition)๊ณผ ๊ฐ™์ด ์ž…๋ ฅ์— ๋Œ€ํ•œ ๋ถ„์„ ๋ฐ ์ดํ•ด(understanding)๊ฐ€ ํ•„์š”ํ•œ ํƒœ์Šคํฌ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋” ์ „์šฉ ๋ชจ๋ธ(Decoder-only models): ํ…์ŠคํŠธ ์ƒ์„ฑ(text generation) ๋“ฑ๊ณผ ๊ฐ™์€ ์ƒ์„ฑ ํƒœ์Šคํฌ(generative tasks)์— ์ข‹์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ(Encoder-Decoder models) ํ˜น์€ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋ธ(sequence-to-sequence model): ๋ฒˆ์—ญ(translation)์ด๋‚˜ ์š”์•ฝ(summarization)๊ณผ ๊ฐ™์ด ์ž…๋ ฅ์ด ์ˆ˜๋ฐ˜๋˜๋Š” ์ƒ์„ฑ ํƒœ์Šคํฌ(generative tasks)์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์•„ํ‚คํ…์ฒ˜๋Š” ์ดํ›„ ์„น์…˜์—์„œ ๊ฐœ๋ณ„์ ์œผ๋กœ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๊ณ„์ธต (Attention layers) Transformer ๋ชจ๋ธ์˜ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํŠน์ง•์€ ์–ดํ…์…˜ ๋ ˆ์ด์–ด(attention layers)๋ผ๋Š” ํŠน์ˆ˜ ๋ ˆ์ด์–ด๋กœ ๊ตฌ์ถ•๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌ์‹ค, Transformer ์•„ํ‚คํ…์ฒ˜๋ฅผ ์†Œ๊ฐœํ•˜๋Š” ๋…ผ๋ฌธ์˜ ์ œ๋ชฉ์ด "Attention Is All You Need"์˜€์Šต๋‹ˆ๋‹ค! ์ด ๊ฐ•์ขŒ์˜ ๋’ท๋ถ€๋ถ„์—์„œ ์–ดํ…์…˜ ๋ ˆ์ด์–ด(attention layer)์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ๋‚ด์šฉ์„ ์‚ดํŽด๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ˜„์žฌ๋Š” ์ด ๋ ˆ์ด์–ด๊ฐ€ ๊ฐ ๋‹จ์–ด์˜ ํ‘œํ˜„์„ ์ฒ˜๋ฆฌํ•  ๋•Œ, ๋ฌธ์žฅ์˜ ํŠน์ • ๋‹จ์–ด๋“ค์— ํŠน๋ณ„ํ•œ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” ๊ฑฐ์˜ ๋ฌด์‹œํ•˜๋„๋ก ๋ชจ๋ธ์— ์ง€์‹œํ•  ๊ฒƒ์ด๋ผ๋Š” ์ ๋งŒ ์•Œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด์„œ, ์˜์–ด๋กœ ๋œ ํ…์ŠคํŠธ๋ฅผ ํ”„๋ž‘์Šค์–ด๋กœ ๋ฒˆ์—ญํ•˜๋Š” ์ž‘์—…์„ ๊ณ ๋ คํ•ด ๋ด…์‹œ๋‹ค. "You like this course"๋ผ๋Š” ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด ๋ฒˆ์—ญ ๋ชจ๋ธ์€ "like"๋ผ๋Š” ๋‹จ์–ด์— ๋Œ€ํ•œ ์ ์ ˆํ•œ ๋ฒˆ์—ญ์„ ์–ป๊ธฐ ์œ„ํ•ด ์ธ์ ‘ ๋‹จ์–ด "You"์—๋„ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ํ”„๋ž‘์Šค์–ด์—์„œ ๋™์‚ฌ "like"๋Š” ์ฃผ์–ด(subject)์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ํ™œ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์žฅ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„("this course")์€ ํ•ด๋‹น ๋‹จ์–ด("like")์˜ ๋ฒˆ์—ญ์— ๊ทธ๋‹ค์ง€ ์œ ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ๋งฅ๋ฝ์—์„œ, "this"๋ฅผ ๋ฒˆ์—ญํ•  ๋•Œ, ๋ชจ๋ธ์€ "course"๋ผ๋Š” ๋‹จ์–ด์—๋„ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. "this"๋Š” ์—ฐ๊ฒฐ๋œ ๋ช…์‚ฌ๊ฐ€ ๋‚จ์„ฑ(masculine)์ธ์ง€ ์—ฌ์„ฑ(feminine)์ธ์ง€์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋ฒˆ์—ญ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์œ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๋ฌธ์žฅ์˜ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค("You", "like")์€ "this"์˜ ๋ฒˆ์—ญ์— ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋” ๋ณต์žกํ•œ ๋ฌธ์žฅ์ด๋‚˜ ๋” ๋ณต์žกํ•œ ๋ฌธ๋ฒ• ๊ทœ์น™์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ์€ ๊ฐœ๋ณ„ ๋‹จ์–ด๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ๋ฒˆ์—ญํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์žฅ์—์„œ ํ•ด๋‹น ๋‹จ์–ด์™€ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ๋‹จ์–ด์—๋„ ํŠน๋ณ„ํ•œ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ๊ฐœ๋…์ด ์ž์—ฐ์–ด์™€ ๊ด€๋ จ๋œ ๋ชจ๋“  ํƒœ์Šคํฌ์— ์ ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ž์ฒด๊ฐ€ ๊ณ ์œ ํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ ๊ทธ ์˜๋ฏธ๋Š” ์ฃผ๋ณ€ ๋ฌธ๋งฅ, ์ฆ‰ ์ปจํ…์ŠคํŠธ(context)์— ์˜ํ•ด ํฌ๊ฒŒ ์˜ํ–ฅ์„ ๋ฐ›์œผ๋ฉฐ, ์ปจํ…์ŠคํŠธ๋Š” ์ฒ˜๋ฆฌ ์ค‘์ธ ๋‹จ์–ด ์•ž์ด๋‚˜ ๋’ค์— ์กด์žฌํ•˜๋Š” ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ ˆ์ด์–ด(attention layer)๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ดํ•ดํ–ˆ์œผ๋ฏ€๋กœ ์ด์ œ Transformer ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ค๋ฆฌ์ง€๋„ ์•„ํ‚คํ…์ฒ˜ Transformer ์•„ํ‚คํ…์ฒ˜๋Š” ์›๋ž˜ ๋ฒˆ์—ญ์šฉ์œผ๋กœ ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์ด ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ ์ธ์ฝ”๋”(encoder)๋Š” ํŠน์ • ์–ธ์–ด๋กœ ํ‘œ๊ธฐ๋œ ์ž…๋ ฅ(๋ฌธ์žฅ)์„ ์ˆ˜์‹ ํ•˜๊ณ  ๋””์ฝ”๋”(decoder)๋Š” ์›ํ•˜๋Š” ๋Œ€์ƒ ์–ธ์–ด๋กœ ํ‘œ๊ธฐ๋œ ๋™์ผํ•œ ์˜๋ฏธ์˜ ๋ฌธ์žฅ์„ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์—์„œ ์–ดํ…์…˜ ๋ ˆ์ด์–ด(attention layer)๋Š” ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด์— ์ฃผ์˜(attention)๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด, ์šฐ๋ฆฌ๊ฐ€ ๋ฐฉ๊ธˆ ๋ณด์•˜๋“ฏ์ด ํ˜„์žฌ ๋‹จ์–ด์— ๋Œ€ํ•œ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋Š” ๋ฌธ์žฅ์—์„œ ํ•ด๋‹น ๋‹จ์–ด์˜ ์•ž๋ถ€๋ถ„๊ณผ ๋’ท๋ถ€๋ถ„์˜ ๋‚ด์šฉ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋””์ฝ”๋”๋Š” ์ˆœ์ฐจ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋ฉฐ ์ด๋ฏธ ๋ฒˆ์—ญ๋œ ๋ฌธ์žฅ์˜ ๋‹จ์–ด๋“ค์—๋งŒ, ์ฆ‰ ํ˜„์žฌ ์ƒ์„ฑ๋˜๊ณ  ์žˆ๋Š” ๋‹จ์–ด ์•ž์˜ ๋‹จ์–ด๋“ค์—๋งŒ ์ฃผ์˜(attention)๋ฅผ ๊ธฐ์šธ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฒˆ์—ญ ๋Œ€์ƒ(target sentence)์˜ ์ฒ˜์Œ ์„ธ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ, ๋””์ฝ”๋”์— ์ด๋ฅผ ์ž…๋ ฅํ•œ ๋‹ค์Œ ์ธ์ฝ”๋”์˜ ๋ชจ๋“  ์ž…๋ ฅ(์›๋ณธ ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด๋“ค)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋„ค ๋ฒˆ์งธ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋ ค๊ณ  ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๋„์ค‘ ์†๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด, ๋””์ฝ”๋”(decoder)๋Š” ์ „์ฒด ๋Œ€์ƒ ๋ฌธ์žฅ(target sentences)์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์ง€๋งŒ, ์ด ์ค‘์—์„œ ๋ฏธ๋ž˜ ๋‹จ์–ด(ํ˜„์žฌ ๋””์ฝ”๋”ฉ ๋Œ€์ƒ ๋‹จ์–ด์˜ ์ดํ›„์— ๋‚˜ํƒ€๋‚˜๋Š” ๋‹จ์–ด๋“ค)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์œ„์น˜์— ๋‚˜ํƒ€๋‚˜๋Š” ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋ ค๊ณ  ํ•  ๋•Œ, ๋‘ ๋ฒˆ์งธ ์œ„์น˜์˜ ์ •๋‹ต ๋‹จ์–ด๋ฅผ ๋ฐ”๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ํ•™์Šต์ด ์ œ๋Œ€๋กœ ์ง„ํ–‰๋˜์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋„ค ๋ฒˆ์งธ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์–ดํ…์…˜ ๊ณ„์ธต์€ ์ฒซ ๋ฒˆ์งธ์—์„œ ์„ธ ๋ฒˆ์งธ๊นŒ์ง€์˜ ๋‹จ์–ด๋“ค์—๋งŒ ์ฃผ์˜๋ฅผ ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›๋ž˜ Transformer ์•„ํ‚คํ…์ฒ˜๋Š” ์™ผ์ชฝ์— ์ธ์ฝ”๋”๊ฐ€ ์žˆ๊ณ  ์˜ค๋ฅธ์ชฝ์— ๋””์ฝ”๋”๊ฐ€ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ๋””์ฝ”๋”(decoder) ๋ธ”๋ก์˜ ์ฒซ ๋ฒˆ์งธ ์–ดํ…์…˜ ๊ณ„์ธต(attention layer)์€ ๋””์ฝ”๋”(decoder)์— ๋Œ€ํ•œ ๋ชจ๋“  (๊ณผ๊ฑฐ) ์ž…๋ ฅ์— ์ฃผ์˜๋ฅผ ์ง‘์ค‘ํ•˜์ง€๋งŒ, ๋‘ ๋ฒˆ์งธ ์–ดํ…์…˜ ๊ณ„์ธต์€ ์ธ์ฝ”๋”์˜ ์ถœ๋ ฅ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ˜„์žฌ ๋‹จ์–ด๋ฅผ ๊ฐ€์žฅ ์ž˜ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ์ „์ฒด ์ž…๋ ฅ ๋ฌธ์žฅ(input/source sentence)์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋Œ€์ƒ ์–ธ์–ด(target language)๊ฐ€ ์›๋ณธ ์–ธ์–ด(source language)์™€ ๋น„๊ตํ•˜์—ฌ ์ƒ๋‹นํžˆ ๋‹ค๋ฅธ ๋‹จ์–ด ์ˆœ์„œ(words in different orders)๋กœ ๋ฌธ์žฅ์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฌธ๋ฒ• ๊ทœ์น™(grammatical rule)์„ ๊ฐ€์ง€๊ฑฐ๋‚˜, ์›๋ณธ ๋ฌธ์žฅ(input/source sentence)์˜ ๋’ท๋ถ€๋ถ„์— ๋‚˜ํƒ€๋‚œ ์ปจํ…์ŠคํŠธ(context)๊ฐ€ ํ˜„์žฌ ๋‹จ์–ด์— ๋Œ€ํ•œ ์ตœ์ƒ์˜ ๋ฒˆ์—ญ์„ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋งˆ์Šคํฌ(Attention mask)๋Š” ์ธ์ฝ”๋”/๋””์ฝ”๋”์—์„œ ๋ชจ๋ธ์ด ํŠน์ • ๋‹จ์–ด์— ์ฃผ์˜๋ฅผ ์ง‘์ค‘ํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์žฅ์„ ์ผ๊ด„ ์ฒ˜๋ฆฌ(batching) ํ•  ๋•Œ ๋ชจ๋“  ์ž…๋ ฅ์„ ๋™์ผํ•œ ๊ธธ์ด๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜๋Š” ํŠน์ˆ˜ ํŒจ๋”ฉ ๋‹จ์–ด(padding word)์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„ํ‚คํ…์ฒ˜(architectures) vs. ์ฒดํฌํฌ์ธํŠธ(checkpoints) ์ด ๊ฐ•์ขŒ์—์„œ ํ–ฅํ›„ Transformer ๋ชจ๋ธ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์•„ํ‚คํ…์ฒ˜(architecture) ์™€ ์ฒดํฌํฌ์ธํŠธ(checkpoint), ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋ธ(model)์ด๋ผ๋Š” ์šฉ์–ด๋ฅผ ์ž์ฃผ ์ ‘ํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์šฉ์–ด๋“ค์€ ๊ฐ๊ฐ ์„œ๋กœ ๋‹ค๋ฅธ ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์•„ํ‚คํ…์ฒ˜(Architectures): ์ด ์šฉ์–ด๋Š” ๋ชจ๋ธ์˜ ๋ผˆ๋Œ€(skeleton)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ๋‚ด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๊ฐ ๋ ˆ์ด์–ด(layer)์™€ ์˜คํผ๋ ˆ์ด์…˜(operation, ์—ฐ์‚ฐ) ๋“ฑ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ฒดํฌํฌ์ธํŠธ(Checkpoints): ํ•ด๋‹น ์•„ํ‚คํ…์ฒ˜์—์„œ ๋กœ๋“œ๋  ๊ฐ€์ค‘์น˜ ๊ฐ’๋“ค์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ชจ๋ธ(Model): ์ด๊ฒƒ์€ "์•„ํ‚คํ…์ฒ˜(architecture)" ๋˜๋Š” "์ฒดํฌํฌ์ธํŠธ(checkpoint)"๋ณด๋‹ค๋Š” ๋œ ๋ช…ํ™•ํ•œ ํฌ๊ด„์ ์ธ ์šฉ์–ด(umbrella term)์ž…๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ๋ชจ๋‘๋ฅผ ์˜๋ฏธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ๊ฐ•์ขŒ์—์„œ๋Š” ํ‘œ๊ธฐ์˜ ๋ช…ํ™•์„ฑ์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ ๋ชจ๋ธ์ด๋ผ๋Š” ์šฉ์–ด๋ณด๋‹ค๋Š” ์•„ํ‚คํ…์ฒ˜(architecture) ๋˜๋Š” ์ฒดํฌํฌ์ธํŠธ(checkpoint)๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, BERT๋Š” ์•„ํ‚คํ…์ฒ˜(architecture)์ด๊ณ  BERT์˜ ์ฒซ ๋ฒˆ์งธ ๋ฆด๋ฆฌ์Šค๋ฅผ ์œ„ํ•ด Google ํŒ€์—์„œ ํ•™์Šตํ•œ ๊ฐ€์ค‘์น˜ ์„ธํŠธ(set of weights)์ธ bert-base-cased๋Š” ์ฒดํฌํฌ์ธํŠธ(checkpoint)์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, "BERT ๋ชจ๋ธ(BERT model)"๊ณผ "bert-base-cased"๋„ ๋ชจ๋ธ์ด๋ผ๊ณ  ๋งํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 4. ์ธ์ฝ”๋” ๋ชจ๋ธ (Encoder Models) ์ธ์ฝ”๋” ๋ชจ๋ธ(encoder models)์€ Transformers ๋ชจ๋ธ์˜ ์ธ์ฝ”๋” ๋ชจ๋“ˆ๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋‹จ๊ณ„์—์„œ ์–ดํ…์…˜ ๊ณ„์ธต(attention layer)์€ ์ดˆ๊ธฐ/์›๋ณธ ์ž…๋ ฅ ๋ฌธ์žฅ(initial sentence)์˜ ๋ชจ๋“  ๋‹จ์–ด์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์€ ์ข…์ข… "์–‘๋ฐฉํ–ฅ(bi-directional)" ์ฃผ์˜ ์ง‘์ค‘(attention)์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ํŠน์ง•์ด๋ฉฐ, auto-encoding model์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์˜ ์‚ฌ์ „ ํ•™์Šต(pretraining) ๊ณผ์ •์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ ์ฃผ์–ด์ง„ ์ดˆ๊ธฐ ๋ฌธ์žฅ์„ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์†์ƒ์‹œํ‚ค๊ณ (์˜ˆ: ์ž„์˜์˜ ๋‹จ์–ด๋ฅผ masking ํ•˜์—ฌ), ์†์ƒ์‹œํ‚จ ๋ฌธ์žฅ์„ ๋‹ค์‹œ ์›๋ž˜ ๋ฌธ์žฅ์œผ๋กœ ๋ณต์›ํ•˜๋Š” ๊ณผ์ •์„ ํ†ตํ•ด์„œ ๋ชจ๋ธ ํ•™์Šต์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋” ๋ชจ๋ธ(encoder models)์€ ๋ฌธ์žฅ ๋ถ„๋ฅ˜(sentence classification), ๊ฐœ์ฒด๋ช… ์ธ์‹(named-entity recognition), ํ˜น์€ ๋” ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹จ์–ด ๋ถ„๋ฅ˜(word classification) ๋ฐ ์ถ”์ถœํ˜• ์งˆ์˜์‘๋‹ต(extractive question answering) ๋“ฑ๊ณผ ๊ฐ™์ด ์ „์ฒด ๋ฌธ์žฅ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•œ ์ž‘์—…(task)์— ๊ฐ€์žฅ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชจ๋ธ์ด ์žˆ์Šต๋‹ˆ๋‹ค: ALBERT BERT DistilBERT ELECTRA RoBERTa 5. ๋””์ฝ”๋” ๋ชจ๋ธ (Decoder Models) ๋””์ฝ”๋” ๋ชจ๋ธ(decoder models)์€ Transformer ๋ชจ๋ธ์˜ ๋””์ฝ”๋” ๋ชจ๋“ˆ๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋‹จ๊ณ„์—์„œ ์ฃผ์–ด์ง„ ๋‹จ์–ด์— ๋Œ€ํ•ด ์–ดํ…์…˜ ๋ ˆ์ด์–ด(attention layer)๋Š” ๋ฌธ์žฅ์—์„œ ํ˜„์žฌ ์ฒ˜๋ฆฌ ๋‹จ์–ด ์•ž์ชฝ์— ์œ„์น˜ํ•œ ๋‹จ์–ด๋“ค์—๋งŒ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์„ ์ผ๋ฐ˜์ ์œผ๋กœ ์ž๋™ ํšŒ๊ท€ ๋ชจ๋ธ(auto-regressive models)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋” ๋ชจ๋ธ(decoder models)์˜ ์‚ฌ์ „ ํ•™์Šต(pretraining)์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฌธ์žฅ์˜ ๋‹ค์Œ ๋‹จ์–ด ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•จ์œผ๋กœ์จ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์€ ํ…์ŠคํŠธ ์ƒ์„ฑ(text generation)๊ณผ ๊ด€๋ จ๋œ ์ž‘์—…(task)์— ๊ฐ€์žฅ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ๋ชจ๋ธ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: CTRL GPT GPT-2 Transformer XL 6. ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ (Sequence-to-sequence models) ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ(sequence-to-sequence ๋ชจ๋ธ์ด๋ผ๊ณ ๋„ ํ•จ)์€ Transformer ์•„ํ‚คํ…์ฒ˜์˜ ๋‘ ๋ถ€๋ถ„, ์ฆ‰ ์ธ์ฝ”๋”(encoder)์™€ ๋””์ฝ”๋”(decoder) ๋ชจ๋‘๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋‹จ๊ณ„์—์„œ ์–ดํ…์…˜ ๊ณ„์ธต(attention layer)์€ ์ดˆ๊ธฐ/์›๋ณธ ์ž…๋ ฅ ๋ฌธ์žฅ(initial sentence)์˜ ๋ชจ๋“  ๋‹จ์–ด์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ฉด, ๋””์ฝ”๋”์˜ ์–ดํ…์…˜ ๋ ˆ์ด์–ด(attention layer)๋Š” ๋ฌธ์žฅ์—์„œ ํ˜„์žฌ ์ฒ˜๋ฆฌ ๋‹จ์–ด ์•ž์ชฝ์— ์œ„์น˜ํ•œ ๋‹จ์–ด๋“ค์—๋งŒ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์˜ ์‚ฌ์ „ ํ•™์Šต(pretraining)์€ ์ธ์ฝ”๋” ๋˜๋Š” ๋””์ฝ”๋” ๋ชจ๋ธ์˜ ๋ชฉ์  ํ•จ์ˆ˜(objectives)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰๋  ์ˆ˜ ์žˆ์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ์•ฝ๊ฐ„ ๋” ๋ณต์žกํ•œ ์ฒ˜๋ฆฌ ๊ณผ์ •์ด ์ˆ˜๋ฐ˜๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, T5๋Š” ์ž„์˜์˜ ํ…์ŠคํŠธ ์ผ๋ถ€๋ถ„(text span, ์—ฌ๋Ÿฌ ๋‹จ์–ด๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Œ)์„ ํ•˜๋‚˜์˜ ๋งˆ์Šคํฌ ํŠน์ˆ˜ ๋‹จ์–ด(mask special word)๋กœ ๋Œ€์ฒดํ•˜์—ฌ ์‚ฌ์ „ ํ•™์Šต๋˜๋ฉฐ, ํ•™์Šต ๋ชฉํ‘œ(objective)๋Š” ์ด ๋งˆ์Šคํฌ ๋‹จ์–ด๊ฐ€ ๋Œ€์ฒดํ•  ํ…์ŠคํŠธ๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Sequence-to-sequence ๋ชจ๋ธ์€ ์š”์•ฝ(summarization), ๋ฒˆ์—ญ(translation) ๋˜๋Š” ์ƒ์„ฑํ˜• ์งˆ์˜์‘๋‹ต(generative question answering) ๋“ฑ๊ณผ ๊ฐ™์ด ์ฃผ์–ด์ง„ ์ž…๋ ฅ์— ๋”ฐ๋ผ ์ƒˆ๋กœ์šด ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๋Š” ์ž‘์—…(task)์— ๊ฐ€์žฅ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชจ๋ธ์ด ์žˆ์Šต๋‹ˆ๋‹ค: BART mBART Marian T5 7. ํŽธ๊ฒฌ๊ณผ ํ•œ๊ณ„ (Bias and Limitations) ๋งŒ์ผ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์ด๋‚˜ ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์„ ์ƒ์šฉ ์‹œ์Šคํ…œ์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ์ œ์•ฝ ์‚ฌํ•ญ(limitations)์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ์ค‘์—์„œ ๊ฐ€์žฅ ํฌ๊ฒŒ ์ด์Šˆ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์‹ค์€ ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์‚ฌ์ „ ํ•™์Šต์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด, ์ธํ„ฐ๋„ท์— ์กด์žฌํ•˜๋Š” ์ข‹์€ ๋ฐ์ดํ„ฐ๋Š” ๋ฌผ๋ก  ์ตœ์•…์˜ ๋ฐ์ดํ„ฐ๋“ค๋„ ๋ฌด์กฐ๊ฑด ์ˆ˜์ง‘ํ•˜์—ฌ ํ™œ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ๋น ๋ฅธ ์„ค๋ช…์„ ์œ„ํ•ด BERT ๋ชจ๋ธ์„ ํ™œ์šฉํ•œ fill-mask ํŒŒ์ดํ”„๋ผ์ธ์˜ ์˜ˆ๋ฅผ ๋‹ค์‹œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from transformers import pipeline unmasker = pipeline("fill-mask", model="bert-base-uncased") result = unmasker("This man works as a [MASK].") print([r["token_str"] for r in result]) result = unmasker("This woman works as a [MASK].") print([r["token_str"] for r in result]) ์œ„ ๋‘ ๋ฌธ์žฅ์—์„œ ๋ˆ„๋ฝ๋œ ๋‹จ์–ด๋ฅผ ์ฑ„์šฐ๋ผ๋Š” ์š”์ฒญ์„ ๋ฐ›์•˜์„ ๋•Œ ๋ชจ๋ธ์€ ์„ฑ๋ณ„๊ณผ ์ƒ๊ด€์—†๋Š”(gender-free) ๋Œ€๋‹ต(waiter/waitress)์€ ์˜ค์ง ํ•˜๋‚˜๋งŒ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํŠน์ • ์„ฑ๋ณ„๊ณผ ๋ฐ€์ ‘ํ•œ ๊ด€๋ จ์ด ์žˆ๋Š” ์ง์—…๋“ค์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งค์ถ˜๋ถ€(prostitute)๋Š” ๋ชจ๋ธ์ด "์—ฌ์„ฑ" ๋ฐ "์ง์—…"๊ณผ ์—ฐ๊ด€๋˜๋Š” ์ƒ์œ„ 5๊ฐœ ๋‹จ์–ด์— ์†ํ•ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ˜„์ƒ์€ BERT๊ฐ€ ์ธํ„ฐ๋„ท ์ „์ฒด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜์—ฌ ํ•™์Šต๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์˜คํžˆ๋ ค ์ค‘๋ฆฝ์ ์ธ ๋ฐ์ดํ„ฐ ์ฆ‰, English Wikipedia ์™€ BookCorpus๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต๋œ ๋“œ๋ฌธ Transformer ๋ชจ๋ธ ์ค‘ ํ•˜๋‚˜์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. 8. 1์žฅ ์š”์•ฝ (Summary) ์ด ์žฅ์—์„œ๋Š” Transformers์˜ ๊ณ ์ˆ˜์ค€(high-level) pipeline() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ NLP ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ํ—ˆ๋ธŒ์—์„œ ๋ชจ๋ธ์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ Inference API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง์ ‘ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋˜ํ•œ Transformer ๋ชจ๋ธ์ด ๊ณ ์ˆ˜์ค€(high-level)์—์„œ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋…ผ์˜ํ•˜๊ณ  ์ „์ด ํ•™์Šต(transfer learning)๊ณผ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning)์˜ ์ค‘์š”์„ฑ์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํ•ต์‹ฌ์€ ๋Œ€์ƒ ์ž‘์—…์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ์ „์ฒด ์•„ํ‚คํ…์ฒ˜(full architecture)๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ธ์ฝ”๋”(encoder) ๋˜๋Š” ๋””์ฝ”๋”(decoder)๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ํ‘œ๋Š” ์ด๋ฅผ ์š”์•ฝํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ ์˜ˆ์‹œ ํƒœ์Šคํฌ(Tasks) ์ธ์ฝ”๋”(Encoder) ALBERT, BERT, DistilBERT, ELECTRA, RoBERTa ๋ฌธ์žฅ ๋ถ„๋ฅ˜(sentence classification), ๊ฐœ์ฒด ๋ช…์ธ์‹(named-entity recognition), ์ถ”์ถœํ˜• ์งˆ์˜์‘๋‹ต(extractive question answering) ๋””์ฝ”๋”(Decoder) CTRL, GPT, GPT-2, Transformer-XL ํ…์ŠคํŠธ ์ƒ์„ฑ(Text generation) ์ธ์ฝ”๋”-๋””์ฝ”๋”(Encoder-Decoder) BART, T5, Marian, mBART ์š”์•ฝ(summarization), ๋ฒˆ์—ญ(translation), ์ƒ์„ฑํ˜• ์งˆ์˜์‘๋‹ต(generative question answering) 2์žฅ. Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ 1์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด Transformer ๋ชจ๋ธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ทœ๋ชจ๊ฐ€ ๋งค์šฐ ํฝ๋‹ˆ๋‹ค. ์ˆ˜๋ฐฑ๋งŒ์—์„œ ์ˆ˜์ฒœ์–ต ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํฌํ•จ๋œ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ณ  ๋ฐฐํฌํ•˜๋Š” ์ผ์€ ๋งค์šฐ ๋ณต์žกํ•œ ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ์ƒˆ๋กœ์šด ๋ชจ๋ธ์ด ๊ฑฐ์˜ ๋งค์ผ ์ถœ์‹œ๋˜๊ณ  ๊ฐ๊ฐ ๊ณ ์œ ํ•œ ๊ตฌํ˜„ ๋ฐฉ์‹์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ด ๋ชจ๋“  ๋ชจ๋ธ๋“ค์„ ์‹œํ—˜ํ•ด ๋ณด๋Š” ๊ฒƒ ๋˜ํ•œ ์‰ฌ์šด ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค. Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ชฉํ‘œ๋Š” ๋ชจ๋“  Transformer ๋ชจ๋ธ๋“ค์„ ์ ์žฌํ•˜๊ณ , ํ•™์Šตํ•˜๊ณ , ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์ผ API๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์šฉ์ด์„ฑ(Ease of use): ์ตœ์‹  NLP ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ถ”๋ก  ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ, ํ•ด๋‹น ๋ชจ๋ธ์„ ๋‹ค์šด๋กœ๋“œ, ์ ์žฌ ๋ฐ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๋‹จ ๋‘ ์ค„์˜ ์ฝ”๋“œ๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ ์—ฐ์„ฑ(Flexibility): ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ๋ชจ๋ธ์€ PyTorch์˜ nn.Module ๋˜๋Š” TensorFlow์˜ tf.keras.Model ํด๋ž˜์Šค๋กœ ํ‘œํ˜„๋˜๋ฉฐ ๊ฐ ๊ธฐ๊ณ„ ํ•™์Šต(ML) ํ”„๋ ˆ์ž„์›Œํฌ(framework, e.g., PyTorch, Tensorflow) ๋‚ด์—์„œ์˜ ๋‹ค๋ฅธ ๋ชจ๋ธ๋“ค๊ณผ ๋™์ผํ•˜๊ฒŒ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค. ๋‹จ์ˆœ์„ฑ(Simplicity): ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ „์ฒด์—์„œ ์ถ”์ƒํ™”(abstraction)๊ฐ€ ๊ฑฐ์˜ ์ด๋ฃจ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. "All in one file"์€ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ•ต์‹ฌ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ, ๋ชจ๋ธ์˜ ์ˆœ์ „ํŒŒ(forward pass)๊ฐ€ ๋‹จ์ผ ํŒŒ์ผ์— ์™„์ „ํžˆ ์ •์˜๋˜์–ด ํ•ด๋‹น ์ฝ”๋“œ ์ž์ฒด๋ฅผ ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ณ  ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋งˆ์ง€๋ง‰ ํŠน์ง•์€ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋‹ค๋ฅธ ๊ธฐ๊ณ„ํ•™์Šต ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๊ตฌ๋ณ„๋˜๋Š” ์ฐจ๋ณ„์„ฑ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ชจ๋ธ์€ ํŒŒ์ผ ๊ฐ„์— ๊ณต์œ ๋˜๋Š” ๋ชจ๋“ˆ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๊ตฌํ˜„๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  ๊ฐ ๋ชจ๋ธ์—๋Š” ์ž์ฒด ๋ ˆ์ด์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ๋” ์ ‘๊ทผํ•˜๊ธฐ ์‰ฝ๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ ์™ธ์—๋„, ๋‹ค๋ฅธ ๋ชจ๋ธ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ณ  ํŠน์ • ๋ชจ๋ธ์—์„œ ์‰ฝ๊ฒŒ ์‹คํ—˜์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์žฅ์—์„œ๋Š” 1์žฅ์—์„œ ์†Œ๊ฐœํ•œ pipeline() ํ•จ์ˆ˜๋ฅผ ๋Œ€์ฒดํ•˜๊ธฐ ์œ„ํ•ด, ์ง์ ‘ ๋ชจ๋ธ(model)๊ณผ ํ† ํฌ ๋‚˜์ด์ €(tokenizer)๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ์ข…๋‹จ ๊ฐ„(end-to-end) ์˜ˆ์ œ๋ฅผ ๊ฐ€์ง€๊ณ  ์ด๋ฒˆ ์žฅ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ๋ชจ๋ธ API์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ํด๋ž˜์Šค ๋ฐ ์„ค์ •(configuration) ํด๋ž˜์Šค๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ณ , ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋ชจ๋ธ์ด ์˜ˆ์ธก(prediction)์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜์น˜์  ์ž…๋ ฅ(numerical input)์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ, pipeline() ํ•จ์ˆ˜์˜ ๋˜ ๋‹ค๋ฅธ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ์ธ ํ† ํฌ ๋‚˜์ด์ €(tokenizer) API๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๋Š” ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ…์ŠคํŠธ ์ž…๋ ฅ์„ ์ˆ˜์น˜ ๋ฐ์ดํ„ฐ(numerical data)๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ํ•„์š”์‹œ ๋ณ€ํ™˜๋œ ์ˆ˜์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ํ…์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ๋ณด๋ฉด, ์ „์ฒด ํ”„๋กœ์„ธ์Šค์˜ ์ฒ˜์Œ๊ณผ ๋งˆ์ง€๋ง‰์„ ๋‹ด๋‹นํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ๋ฐฐ์น˜(batch) ํ˜•ํƒœ๋กœ ์—ฌ๋Ÿฌ ๋ฌธ์žฅ๋“ค์„ ๋ชจ๋ธ๋กœ ํ•œ๊บผ๋ฒˆ์— ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ณ , ์ƒ์œ„ ์ˆ˜์ค€์˜ tokenizer() ํ•จ์ˆ˜๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ด„์œผ๋กœ์จ ์ด๋ฒˆ ์žฅ์„ ๋งˆ๋ฌด๋ฆฌํ•ฉ๋‹ˆ๋‹ค. โš  Model Hub ๋ฐ Transformers์—์„œ ์ œ๊ณต๋˜๋Š” ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋ ค๋ฉด ๊ณ„์ •์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. 1. Pipeline ๋‚ด๋ถ€ ์‹คํ–‰ ๊ณผ์ • ๋ณธ ํ•œ๊ธ€ ๊ฐ•์ขŒ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ PyTorch๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์™„์ „ํ•œ ์˜ˆ์ œ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„ , 1์žฅ์—์„œ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚ฌ๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from transformers import pipeline classifier = pipeline("sentiment-analysis") classifier( [ "I've been waiting for a HuggingFace course my whole life.", "I hate this so much!", ] ) 1์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ์ด ํŒŒ์ดํ”„๋ผ์ธ์€ ์ „์ฒ˜๋ฆฌ(preprocessing), ๋ชจ๋ธ๋กœ ์ž…๋ ฅ ์ „๋‹ฌ ๋ฐ ํ›„์ฒ˜๋ฆฌ(postprocessing)์˜ 3๋‹จ๊ณ„๋ฅผ ํ•œ ๋ฒˆ์— ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋“ค ๊ฐ๊ฐ์— ๋Œ€ํ•ด ๋น ๋ฅด๊ฒŒ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ด์šฉํ•œ ์ „์ฒ˜๋ฆฌ ๋‹ค๋ฅธ ์‹ ๊ฒฝ๋ง(neural networks)๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Transformer ๋ชจ๋ธ์€ ์›์‹œ ํ…์ŠคํŠธ๋ฅผ ์ง์ ‘ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ํŒŒ์ดํ”„๋ผ์ธ์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ํ…์ŠคํŠธ ์ž…๋ ฅ์„ ๋ชจ๋ธ์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด, ๋‹ค์Œ ๊ธฐ๋Šฅ๋“ค์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ† ํฌ ๋‚˜์ด์ €(tokenizer)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: ์ž…๋ ฅ์„ ํ† ํฐ(token)์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๋‹จ์–ด(word), ํ•˜์œ„ ๋‹จ์–ด(subword) ๋˜๋Š” ๊ธฐํ˜ธ(symbol)(์˜ˆ: ๊ตฌ๋‘์ )๋กœ ๋ถ„ํ•  ๊ฐ ํ† ํฐ(token)์„ ์ •์ˆ˜(integer)๋กœ ๋งคํ•‘(mapping) ๋ชจ๋ธ์— ์œ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๊ฐ€์ ์ธ ์ž…๋ ฅ(additional inputs)์„ ์ถ”๊ฐ€ ์ด ๋ชจ๋“  ์ „์ฒ˜๋ฆฌ(preprocessing)๋Š” ๋ชจ๋ธ์ด ์‚ฌ์ „ ํ•™์Šต(pretraining) ๋  ๋•Œ์™€ ์ •ํ™•ํžˆ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜ํ–‰๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ๋จผ์ € Model Hub์—์„œ ํ•ด๋‹น ์ •๋ณด๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด AutoTokenizer ํด๋ž˜์Šค์™€ from_pretrained() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ์ฒดํฌํฌ์ธํŠธ(checkpoint) ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ ํ† ํฌ ๋‚˜์ด์ €(tokenizer)์™€ ์—ฐ๊ฒฐ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋™์œผ๋กœ ๊ฐ€์ ธ์™€ ์บ์‹œ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ฒ˜์Œ ์‹คํ–‰ํ•  ๋•Œ๋งŒ ํ•ด๋‹น ์ •๋ณด๊ฐ€ ๋‹ค์šด๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค. sentiment-analysis ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋””ํดํŠธ ์ฒดํฌํฌ์ธํŠธ(default checkpoint)๋Š” distilbert-base-uncased-finetuned-sst-2-english(์ด ๋ชจ๋ธ์— ๋Œ€ํ•œ model card๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค)์ด๋ฏ€๋กœ ๋‹ค์Œ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. from transformers import AutoTokenizer checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(checkpoint) ์ผ๋‹จ ์œ„์™€ ๊ฐ™์ด ํ† ํฌ ๋‚˜์ด์ €(tokenizer)๋ฅผ ์ƒ์„ฑํ•˜๋ฉด, ์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ ๋ณด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ์ด ํ† ํฌ ๋‚˜์ด์ €์— ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์—ฌ ๋ชจ๋ธ์— ๋ฐ”๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ด์ฌ ๋”•์…”๋„ˆ๋ฆฌ(dictionary) ์ •๋ณด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ดํ›„ ํ•ด์•ผ ํ•  ์ผ์€ input IDs ๋ฆฌ์ŠคํŠธ๋ฅผ ํ…์„œ(tensors)๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ๋ฟ์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„๋“ค์€ PyTorch, TensorFlow ๋˜๋Š” Flax ๋“ฑ, ์ด๋“ค ์ค‘ ์–ด๋–ค ML ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ๋ฐฑ์—”๋“œ(backend)๋กœ ์‚ฌ์šฉ๋˜๋Š”์ง€ ๊ฑฑ์ •ํ•  ํ•„์š”๊ฐ€ ์—†์ด Transformers๋ฅผ ๋งˆ์Œ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Transformer ๋ชจ๋ธ์€ ํ…์„œ(tensor) ์ž…๋ ฅ๋งŒ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๋งŒ์ผ ์—ฌ๋Ÿฌ๋ถ„์ด ํ…์„œ(tensor)์— ๋Œ€ํ•ด ์ฒ˜์Œ ์ ‘ํ•œ๋‹ค๋ฉด, NumPy ๋ฐฐ์—ด(array)์„ ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. NumPy ๋ฐฐ์—ด์€ ์Šค์นผ๋ผ(0D), ๋ฒกํ„ฐ(1D), ํ–‰๋ ฌ(2D) ํ˜น์€ ๋” ๋งŽ์€ ์ฐจ์›์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์‚ฌ์‹ค์ƒ ํ…์„œ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๊ธฐ๊ณ„ํ•™์Šต ํ”„๋ ˆ์ž„์›Œํฌ์˜ ํ…์„œ๋„ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ NumPy ๋ฐฐ์—ด๋งŒํผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ์„ฑ(instantiate) ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ…์„œ์˜ ์œ ํ˜•(PyTorch, TensorFlow ๋˜๋Š” ์ผ๋ฐ˜ NumPy)์„ ์ง€์ •ํ•˜๋ ค๋ฉด return_tensors ์ธ์ˆ˜(argument)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. raw_inputs = [ "I've been waiting for a HuggingFace course my whole life.", "I hate this so much!", ] inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors="pt") print(inputs) ์•„์ง ํŒจ๋”ฉ(padding)๊ณผ truncation์— ๋Œ€ํ•ด ์‹ ๊ฒฝ ์“ฐ์ง€ ๋งˆ์„ธ์š”. ๋‚˜์ค‘์— ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ธฐ์–ตํ•ด์•ผ ํ•  ์ฃผ์š” ์‚ฌํ•ญ์€ ๋‹จ์ผ ๋ฌธ์žฅ ๋˜๋Š” ๋‹ค์ค‘ ๋ฌธ์žฅ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ† ํฌ ๋‚˜์ด์ € ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ถœ๋ ฅ ํ…์„œ ์œ ํ˜•์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ…์„œ ์œ ํ˜•์ด ์ง€์ •๋˜์ง€ ์•Š์œผ๋ฉด ๊ฒฐ๊ณผ๋กœ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ(list of list)๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. PyTorch ํ…์„œ ์œ ํ˜•์˜ ๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œ„ ๊ฒฐ๊ณผ์—์„œ ๋ณด๋“ฏ์ด, ์ถœ๋ ฅ์€ ๋‘ ๊ฐœ์˜ ํ‚ค(key) ์ฆ‰, input_ids ๋ฐ attention_mask๋ฅผ ๊ฐ€์ง€๋Š” ํŒŒ์ด์ฌ ๋”•์…”๋„ˆ๋ฆฌ์ž…๋‹ˆ๋‹ค. input_ids์—๋Š” ๊ฐ ๋ฌธ์žฅ์— ์žˆ๋Š” ํ† ํฐ์˜ ๊ณ ์œ  ์‹๋ณ„์ž๋กœ ๊ตฌ์„ฑ๋œ ๋‘ ํ–‰์˜ ์ •์ˆ˜(๊ฐ ๋ฌธ์žฅ์— ํ•˜๋‚˜์”ฉ)๊ฐ€ ๊ฐ’(value)์œผ๋กœ ๋“ค์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์žฅ์˜ ๋’ท๋ถ€๋ถ„์—์„œ attention_mask ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ์‚ดํŽด๋ณด๊ธฐ ํ† ํฌ ๋‚˜์ด์ €์™€ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ(pretrained model)์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Transformers๋Š” ์œ„์˜ AutoTokenizer ํด๋ž˜์Šค์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, from_pretrained() ๋ฉ”์„œ๋“œ๊ฐ€ ํฌํ•จ๋œ AutoModel ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. from transformers import AutoModel checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" model = AutoModel.from_pretrained(checkpoint) ์œ„ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ(code snippet)์—์„œ๋Š” ์ด์ „์— ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ (์‹ค์ œ๋กœ ์ด๋ฏธ ์บ์‹œ ๋˜์–ด ์žˆ์–ด์•ผ ํ•จ) ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”(instantiate) ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์•„ํ‚คํ…์ฒ˜์—๋Š” ๊ธฐ๋ณธ Transformer ๋ชจ๋“ˆ๋งŒ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด ์ž์งˆ(feature)์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” hidden states๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ชจ๋ธ ์ž…๋ ฅ์— ๋Œ€ํ•ด Transformer ๋ชจ๋ธ์— ์˜ํ•ด์„œ ์ˆ˜ํ–‰๋œ ํ•ด๋‹น ์ž…๋ ฅ์˜ ๋ฌธ๋งฅ์  ์ดํ•ด(contextual understanding) ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ณ ์ฐจ์› ๋ฒกํ„ฐ(high-dimensional vector)๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์ด ์ดํ•ด๊ฐ€ ๊ฐ€์ง€ ์•Š๋”๋ผ๋„ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์„ธ์š”. ๋‚˜์ค‘์— ๋ชจ๋‘ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ hidden states๋Š” ๊ทธ ์ž์ฒด๋กœ๋„ ์œ ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ head๋ผ๊ณ  ์•Œ๋ ค์ง„ ๋ชจ๋ธ์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. 1์žฅ์—์„œ, ๋™์ผํ•œ ์•„ํ‚คํ…์ฒ˜๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ํƒœ์Šคํฌ(task)๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ ์ด๋Ÿฌํ•œ ๊ฐ ํƒœ์Šคํฌ(task)์—๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํ—ค๋“œ(head)๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณ ์ฐจ์› ๋ฒกํ„ฐ? Transformer ๋ชจ๋“ˆ์˜ ๋ฒกํ„ฐ ์ถœ๋ ฅ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ทœ๋ชจ๊ฐ€ ํฝ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์„ธ ๊ฐ€์ง€ ์ฐจ์›์ด ์žˆ์Šต๋‹ˆ๋‹ค: ๋ฐฐ์น˜ ํฌ๊ธฐ(Batch size): ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌ๋˜๋Š” ์‹œํ€€์Šค(sequence)์˜ ๊ฐœ์ˆ˜(์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” 2๊ฐœ). ์‹œํ€€์Šค ๊ธธ์ด(Sequence length): ์‹œํ€€์Šค ์ˆซ์ž ํ‘œํ˜„์˜ ๊ธธ์ด(์ด ์˜ˆ์—์„œ๋Š” 16). ์€๋‹‰ ํฌ๊ธฐ(Hidden size): ๊ฐ ๋ชจ๋ธ ์ž…๋ ฅ์˜ ๋ฒกํ„ฐ ์ฐจ์›. ์œ„์—์„œ ๋งˆ์ง€๋ง‰ ๊ฐ’ ๋•Œ๋ฌธ์— "๊ณ ์ฐจ์›(high-dimensional)" ๋ฒกํ„ฐ๋ผ๊ณ  ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Hidden size๋Š” ๋งค์šฐ ํด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(768์€ ์ž‘์€ ๋ชจ๋ธ์— ์ผ๋ฐ˜์ ์ด๊ณ  ํฐ ๋ชจ๋ธ์—์„œ๋Š” 3072 ์ด์ƒ์ผ ์ˆ˜๋„ ์žˆ์Œ). ์‚ฌ์ „ ์ฒ˜๋ฆฌํ•œ ์ž…๋ ฅ์„ ๋ชจ๋ธ์— ๋„˜๊ธฐ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. outputs = model(**inputs) print(outputs.last_hidden_state.shape) Transformers ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์€ namedtuple ๋˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ(dictionary)์ฒ˜๋Ÿผ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ ์†์„ฑ ๋˜๋Š” ํ‚ค(outputs["last_hidden_state"])๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ฐพ๊ณ  ์žˆ๋Š” ํ•ญ๋ชฉ์ด ์–ด๋””์— ์žˆ๋Š”์ง€ ์ •ํ™•ํžˆ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ ์ธ๋ฑ์Šค(outputs[0])๋กœ๋„ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ ํ—ค๋“œ(model heads): ์ˆซ์ž ์ดํ•ดํ•˜๊ธฐ ๋ชจ๋ธ ํ—ค๋“œ(model head)๋Š” hidden states์˜ ๊ณ ์ฐจ์› ๋ฒกํ„ฐ(high-dimensional vector)๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ๋‹ค๋ฅธ ์ฐจ์›์— ํˆฌ์˜(project) ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ—ค๋“œ(head)๋Š” ํ•˜๋‚˜ ๋˜๋Š” ๋ช‡ ๊ฐœ์˜ ์„ ํ˜• ๋ ˆ์ด์–ด(linear layers)๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. Transformer ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์€ ์ฒ˜๋ฆฌํ•  ๋ชจ๋ธ ํ—ค๋“œ(model head)๋กœ ์ง์ ‘ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ๋ชจ๋ธ์€ ์ž„๋ฒ ๋”ฉ ๋ ˆ์ด์–ด(embeddings layer)์™€ ํ›„์† ๋ ˆ์ด์–ด(subsequent layers)๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ๋ ˆ์ด์–ด(embeddings layer)๋Š” ํ† ํฐํ™”๋œ ์ž…๋ ฅ(tokenized input)์˜ ๊ฐ ์ž…๋ ฅ ID๋ฅผ ํ•ด๋‹น ํ† ํฐ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฒกํ„ฐ(embeddings vector)๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ดํ›„์˜ ํ›„์† ๋ ˆ์ด์–ด๋Š” ์ฃผ์˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜(attention mechanism)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋“ค ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(embeddings vector)๋ฅผ ์กฐ์ž‘ํ•˜์—ฌ ๋ฌธ์žฅ์˜ ์ตœ์ข… ํ‘œํ˜„(final representation)์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. Transformers์—๋Š” ๋‹ค์–‘ํ•œ ์•„ํ‚คํ…์ฒ˜๊ฐ€ ์žˆ์œผ๋ฉฐ ๊ฐ ์•„ํ‚คํ…์ฒ˜๋Š” ํŠนํ™”๋œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ผ๋ถ€ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค: *Model (hidden states๋ฅผ ๋ฆฌํ„ด) *ForCausalLM *ForMaskedLM *ForMultipleChoice *ForQuestionAnswering *ForSequenceClassification *ForTokenClassification and others ์ด ์„น์…˜์—์„œ์˜ ์˜ˆ์‹œ์—์„œ๋Š” ์‹œํ€€์Šค ๋ถ„๋ฅ˜ ํ—ค๋“œ(sequence classification head)๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ชจ๋ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค(๋ฌธ์žฅ์„ ๊ธ์ • ๋˜๋Š” ๋ถ€์ •์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ). ๋”ฐ๋ผ์„œ ์‹ค์ œ๋กœ AutoModel ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋Œ€์‹  AutoModelForSequenceClassification๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoModelForSequenceClassification checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" model = AutoModelForSequenceClassification.from_pretrained(checkpoint) outputs = model(**inputs) ์ด์ œ ์ถœ๋ ฅ์˜ ๋ชจ์–‘(shape)์„ ๋ณด๋ฉด ์ฐจ์›์ด ํ›จ์”ฌ ๋‚ฎ์•„์ง‘๋‹ˆ๋‹ค. ๋ชจ๋ธ ํ—ค๋“œ(model head)๋Š” ๊ณ ์ฐจ์› ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ๋‘ ๊ฐœ์˜ ๊ฐ’(๋ ˆ์ด๋ธ”๋‹น ํ•˜๋‚˜์”ฉ)์„ ํฌํ•จํ•˜๋Š” ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. print(outputs.logits.shape) ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ๊ณผ ๋‘ ๊ฐœ์˜ ๋ ˆ์ด๋ธ”๋งŒ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋ธ์—์„œ ์–ป์€ ๊ฒฐ๊ณผ์˜ ๋ชจ์–‘(shape)์€ 2 x 2์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ ํ›„์ฒ˜๋ฆฌํ•˜๊ธฐ ๋ชจ๋ธ์—์„œ ์ถœ๋ ฅ์œผ๋กœ ์–ป์€ ๊ฐ’์€ ๋ฐ˜๋“œ์‹œ ๊ทธ ์ž์ฒด๋กœ ์˜๋ฏธ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋‹ค์Œ์„ ํ•œ๋ฒˆ ๋ณด์‹œ์ง€์š”. print(outputs.logits) ์šฐ๋ฆฌ ๋ชจ๋ธ์€ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์— ๋Œ€ํ•ด [-1.5607, 1.6123], ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์— ๋Œ€ํ•ด [4.1692, -3.3464]๋ฅผ ์˜ˆ์ธกํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ™•๋ฅ ์ด ์•„๋‹ˆ๋ผ ๋ชจ๋ธ์˜ ๋งˆ์ง€๋ง‰ ๊ณ„์ธต์—์„œ ์ถœ๋ ฅ๋œ ์ •๊ทœํ™”๋˜์ง€ ์•Š์€ ์›์‹œ ์ ์ˆ˜์ธ logits์ž…๋‹ˆ๋‹ค. ์ด๋“ค ๊ฐ’์„ ํ™•๋ฅ ๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด SoftMax ๊ณ„์ธต์„ ํ†ต๊ณผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  Transformers ๋ชจ๋ธ์€ ์ด logits ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ•™์Šต์„ ์œ„ํ•œ ์†์‹ค ํ•จ์ˆ˜(loss function)๋Š” ์ตœ์ข… ํ™œ์„ฑํ™” ํ•จ์ˆ˜(activation function, e.g., SoftMax)์™€ ์‹ค์ œ ์†์‹ค ํ•จ์ˆ˜(actual loss function, e.g., cross entropy)๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. import torch predictions = torch.nn.functional.softmax(outputs.logits, dim=-1) print(predictions) ์ด์ œ ๋ชจ๋ธ์ด ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์— ๋Œ€ํ•ด [0.0402, 0.9598], ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์— ๋Œ€ํ•ด [0.9995, 0.0005]๋ฅผ ์˜ˆ์ธกํ–ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ ์šฐ๋ฆฌ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ™•๋ฅ  ์ ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ฐ ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” ๋ ˆ์ด๋ธ”์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด, model.config์˜ id2label ์†์„ฑ๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ ์„น์…˜์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. model.config.id2label ์ด์ œ ๋ชจ๋ธ์ด ์•„๋ž˜ ๋‚ด์šฉ์„ ์˜ˆ์ธกํ–ˆ๋‹ค๋Š” ๊ฒฐ๋ก ์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ : NEGATIVE: 0.0402, POSITIVE: 0.9598 ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ : NEGATIVE: 0.9995, POSITIVE: 0.0005 ์ง€๊ธˆ๊นŒ์ง€ ํŒŒ์ดํ”„๋ผ์ธ(pipeline)์˜ ๋‚ด๋ถ€์—์„œ ์‹คํ–‰๋˜๋Š” 3๋‹จ๊ณ„์ธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•œ ์ „์ฒ˜๋ฆฌ(preprocessing), ๋ชจ๋ธ์„ ํ†ตํ•œ ์ž…๋ ฅ ์ „๋‹ฌ(passing the inputs through the model) ๋ฐ ํ›„์ฒ˜๋ฆฌ(postprocessing)๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰ํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. โœ Try it out! ๋ณธ์ธ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ…์ŠคํŠธ๋ฅผ ๋‘ ๊ฐœ(๋˜๋Š” ๊ทธ ์ด์ƒ) ์„ ํƒํ•˜๊ณ  sentiment analysis ํŒŒ์ดํ”„๋ผ์ธ์„ ํ†ตํ•ด ์‹คํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์—ฌ๊ธฐ์—์„œ ์„ค๋ช…ํ•œ ๋Œ€๋กœ ์ง์ ‘ ์‹คํ–‰ํ•ด ๋ณด๊ณ , ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์„ธ์š”! 2. ๋ชจ๋ธ (Models) ์ด ์„น์…˜์—์„œ๋Š” ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด, ์šฐ๋ฆฌ๋Š” ์ง€์ •๋œ ์ฒดํฌํฌ์ธํŠธ(checkpoint)๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•  ๋•Œ ํŽธ๋ฆฌํ•œ AutoModel ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. AutoModel ํด๋ž˜์Šค์™€ ์ด์™€ ๊ด€๋ จ๋œ ๋ชจ๋“  ํ•ญ๋ชฉ๋“ค์€ ์‹ค์ œ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋‹จ์ˆœํ•œ ๋ž˜ํผ(wrapper)์ž…๋‹ˆ๋‹ค. ๋‹น์‹ ์ด ์„ ํƒํ•œ ์ฒดํฌํฌ์ธํŠธ(checkpoint)์— ์ ํ•ฉํ•œ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜(model architecture)๋ฅผ ์ž๋™์œผ๋กœ ์ถ”์ธกํ•œ ๋‹ค์Œ ์ด ์•„ํ‚คํ…์ฒ˜๋กœ ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์˜๋ฆฌํ•œ ๋ž˜ํผ(wrapper)๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์ง€์š”. ๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๋ชจ๋ธ์˜ ์œ ํ˜•์„ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ง์ ‘ ์ •์˜ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด BERT ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ ์ƒ์„ฑํ•˜๊ธฐ BERT ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผ ํ•  ์ผ์€ ์„ค์ •(configuration) ๊ฐ์ฒด๋ฅผ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: from transformers import BertConfig, BertModel # config(์„ค์ •)์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. config = BertConfig() # ํ•ด๋‹น config์—์„œ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. model = BertModel(config) ์ด ์„ค์ •(configuration) ๊ฐ์ฒด์—๋Š” ๋ชจ๋ธ์„ ๋นŒ๋“œ ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋งŽ์€ ์†์„ฑ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค: print(config) BertConfig { "attention_probs_dropout_prob": 0.1, "classifier_dropout": null, "hidden_act": "gelu", "hidden_dropout_prob": 0.1, "hidden_size": 768, "initializer_range": 0.02, "intermediate_size": 3072, "layer_norm_eps": 1e-12, "max_position_embeddings": 512, "model_type": "bert", "num_attention_heads": 12, "num_hidden_layers": 12, "pad_token_id": 0, "position_embedding_type": "absolute", "transformers_version": "4.12.5", "type_vocab_size": 2, "use_cache": true, "vocab_size": 30522 } ์•„์ง ์ด ๋ชจ๋“  ์†์„ฑ๋“ค์ด ๋‹ด๋‹นํ•˜๋Š” ์ผ๋“ค์„ ์‚ดํŽด๋ณด์ง€๋Š” ์•Š์•˜์ง€๋งŒ, ๊ทธ์ค‘ ์ผ๋ถ€๋Š” ์ด๋ฏธ ์•Œ ์ˆ˜๋„ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. hidden_size ์†์„ฑ์€ hidden_states ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•˜๊ณ  num_hidden_layers๋Š” Transformer ๋ชจ๋ธ์˜ ๊ณ„์ธต(layers) ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋กœ๋”ฉ ๋ฉ”์„œ๋“œ๋“ค(loading methods) ๊ธฐ๋ณธ ์„ค์ •(configuration)์—์„œ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๋ฉด ํ•ด๋‹น ๋ชจ๋ธ์„ ์ž„์˜์˜ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. from transformers import BertConfig, BertModel config = BertConfig() model = BertModel(config) # ๋ชจ๋ธ์€ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. ์ด ์ƒํƒœ์—์„œ ๋ชจ๋ธ์„ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ํ•ด๋‹น ์ถœ๋ ฅ์€ ๊ทธ์•ผ๋ง๋กœ ํšก์„ค์ˆ˜์„ค์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋จผ์ € ํ•™์Šต์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ์ž‘์—…(task)์„ ์œ„ํ•ด์„œ ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ(from scratch) ํ•™์Šตํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, 1์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ์˜ค๋žœ ์‹คํ–‰ ์‹œ๊ฐ„๊ณผ ๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๋ฉฐ ๊ฒŒ๋‹ค๊ฐ€ ํ™˜๊ฒฝ์— ๋ฏธ์น˜๋Š” ์˜ํ–ฅ์€ ๋ฌด์‹œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ถˆํ•„์š”ํ•˜๊ณ  ์ค‘๋ณต๋˜๋Š” ๋…ธ๋ ฅ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด๋ฏธ ํ•™์Šต๋œ ๋ชจ๋ธ์„<NAME>๊ณ  ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ ์‚ฌ์ „ ํ•™์Šต๋œ Transformer ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. from_pretrained() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import BertModel model = BertModel.from_pretrained("bert-base-cased") Some weights of the model checkpoint at bert-base-cased were not used when initializing BertModel: ['cls.predictions.bias', 'cls.seq_relationship.weight', 'cls.predictions.decoder.weight', 'cls.predictions.transform.dense.bias', 'cls.predictions.transform.LayerNorm.bias', 'cls.seq_relationship.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.dense.weight'] - This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model). - This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model). ์•ž์„œ ๋ณด์•˜๋“ฏ์ด, BertModel์„ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” AutoModel ํด๋ž˜์Šค๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฒดํฌํฌ์ธํŠธ(checkpoint)์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ง€๊ธˆ๋ถ€ํ„ฐ ์ด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๊ฐ€ ํŠน์ • ์ฒดํฌํฌ์ธํŠธ์—์„œ ์ž‘๋™ํ•œ๋‹ค๋ฉด ๋‹ค๋ฅธ ์ฒดํฌํฌ์ธํŠธ์—์„œ๋„ ์›ํ™œํ•˜๊ฒŒ ์ž‘๋™ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์‹ฌ์ง€์–ด ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜(architecture)๊ฐ€ ๋‹ค๋ฅด๋”๋ผ๋„, ๋ณ€๊ฒฝํ•  ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ํ˜„์žฌ ์ฒดํฌํฌ์ธํŠธ์™€ ์œ ์‚ฌํ•œ ์ž‘์—…(task), ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐ์„ฑ ๋ถ„์„ ํƒœ์Šคํฌ(sentiment analysis task)๋กœ ํ•™์Šต๋˜์—ˆ๋‹ค๋ฉด ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ ์ƒ˜ํ”Œ์—์„œ๋Š” BertConfig๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋Œ€์‹  bert-base-cased ์‹๋ณ„์ž๋ฅผ ํ†ตํ•ด ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ(pretrained model)์„ ๋กœ๋“œํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ BERT ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ํ•™์Šตํ•œ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ(model checkpoint)์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ ์นด๋“œ(model card)์—์„œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ•ด๋‹น ๋ชจ๋ธ์€ ์ฒดํฌํฌ์ธํŠธ์˜ ๋ชจ๋“  ๊ฐ€์ค‘์น˜๋กœ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต๋œ ์ž‘์—…(task)์— ๋Œ€ํ•œ ์ถ”๋ก (inference)์— ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ƒˆ๋กœ์šด ์ž‘์—…(task)์— ๋Œ€ํ•ด ๋ฏธ์„ธ ์กฐ์ •ํ• (fine-tune) ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹ ๊ทœ๋กœ ํ•™์Šตํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๊ฐ€์ค‘์น˜๋กœ ํ•™์Šตํ•˜๋ฉด ์ข‹์€ ๊ฒฐ๊ณผ๋ฅผ ๋น ๋ฅด๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž๋™์œผ๋กœ ๊ฐ€์ค‘์น˜๊ฐ€ ๋‹ค์šด๋กœ๋“œ๋˜๊ณ  ์บ์‹œ ๋˜์–ด(๋”ฐ๋ผ์„œ from_pretrained() ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•ด๋„ ๊ฐ€์ค‘์น˜๊ฐ€ ๋‹ค์‹œ ๋‹ค์šด๋กœ๋“œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค) ์บ์‹œ ํด๋”์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์บ์‹œ ํด๋” ์œ„์น˜๋Š” ~/.cache/huggingface/transformers์ž…๋‹ˆ๋‹ค. HF_HOME ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์—ฌ ์บ์‹œ ํด๋”๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์‹๋ณ„์ž(checkpoint ๋ช…์นญ)๋Š” BERT ์•„ํ‚คํ…์ฒ˜(BERT architecture)์™€ ํ˜ธํ™˜๋˜๋Š” ๋ชจ๋“  Model Hub ๋‚ด์˜ ์‹๋ณ„์ž๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ BERT ์ฒดํฌํฌ์ธํŠธ(checkpoint)์˜ ์ „์ฒด ๋ชฉ๋ก์€ ์—ฌ๊ธฐ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์žฅ ๋ฉ”์„œ๋“œ(Saving methods) ๋ชจ๋ธ์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์€ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ๋งŒํผ ์‰ฝ์Šต๋‹ˆ๋‹ค โ€” from_pretrained() ๋ฉ”์„œ๋“œ์™€ ์œ ์‚ฌํ•œ save_pretrained() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: model.save_pretrained("saving_folder") ls saving_folder config.json pytorch_model.bin config.json ํŒŒ์ผ ๋‚ด์šฉ์„ ๋ณด๋ฉด ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜(model architecture)๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋‹ค์–‘ํ•œ ์†์„ฑ๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ์—๋Š” ๋˜ํ•œ ๋ช‡๋ช‡ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ(metadata), ์ฆ‰ ํ•ด๋‹น ์ฒดํฌํฌ์ธํŠธ(checkpoint)๋ฅผ ๊ตฌ์ถ•ํ•œ ์ถœ์ฒ˜, ์ฒดํฌํฌ์ธํŠธ(checkpoint)๋ฅผ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋˜ Transformers ๋ฒ„์ „ ๋“ฑ๊ณผ ๊ฐ™์€ ์ •๋ณด๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค. pytorch_model.bin ํŒŒ์ผ์€ state dictionary๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๋ชจ๋ธ์˜ ๋ชจ๋“  ๊ฐ€์ค‘์น˜๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‘ ํŒŒ์ผ์€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์„ค์ • ๊ฐ์ฒด(configuration objects)๋Š” ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜(model architecture)๋ฅผ ํŒŒ์•…ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ฐ˜๋ฉด, ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋Š” ๋ชจ๋ธ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜(parameters)์ž…๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์„ ํ™œ์šฉํ•œ ์ถ”๋ก (inference) ์ด์ œ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•˜์œผ๋ฏ€๋กœ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์ธก์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Transformer ๋ชจ๋ธ์€ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์ƒ์„ฑํ•˜๋Š” ์ˆซ์ž๋งŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ† ํฌ ๋‚˜์ด์ €์— ๋Œ€ํ•ด ๋…ผ์˜ํ•˜๊ธฐ ์ „์— ๋ชจ๋ธ์ด ํ—ˆ์šฉํ•˜๋Š” ์ž…๋ ฅ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ์‹œํ€€์Šค๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: sequences = ["Hello!", "Cool.", "Nice!"] ํ† ํฌ ๋‚˜์ด์ €๋Š” ์ด๋ฅผ ์ผ๋ฐ˜์ ์œผ๋กœ input IDs๋ผ๊ณ  ํ•˜๋Š” ์–ดํœ˜ ์ธ๋ฑ์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ ์‹œํ€€์Šค๋Š” ์ˆซ์ž์˜ ๋ฆฌ์ŠคํŠธ(list)์ž…๋‹ˆ๋‹ค! ๊ฒฐ๊ณผ ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: encoded_sequences = [ [101, 7592, 999, 102], [101, 4658, 1012, 102], [101, 3835, 999, 102], ] ์ด๋Š” ์ธ์ฝ”๋”ฉ๋œ ์‹œํ€€์Šค์˜ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ(list of list)์ž…๋‹ˆ๋‹ค. ํ…์„œ(tensor)๋Š” ์ง์‚ฌ๊ฐํ˜•(rectangular) ๋ชจ์–‘(shape)๋งŒ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค(ํ–‰๋ ฌ์„ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ด๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค). encoded_sequences๋Š” ์ด๋ฏธ "๋ฐฐ์—ด(array)" ํ˜•ํƒœ์˜ ์ง์‚ฌ๊ฐํ˜•(rectangular) ๋ชจ์–‘์ด๋ฏ€๋กœ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์Šต๋‹ˆ๋‹ค. import torch model_inputs = torch.tensor(encoded_sequences) ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ํ…์„œ ํ™œ์šฉ ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ํ…์„œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•˜์—ฌ ๋ชจ๋ธ์„ ํ˜ธ์ถœํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. output = model(model_inputs) ๋ชจ๋ธ์€ ๋‹ค์–‘ํ•œ ์ถ”๊ฐ€์ ์ธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” input IDs๋งŒ ์žˆ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ค ๊ฒƒ์ธ์ง€, ์–ธ์ œ ํ•„์š”ํ•œ์ง€์— ๋Œ€ํ•ด์„œ๋Š” ๋‚˜์ค‘์— ์„ค๋ช…ํ•˜๊ฒ ์ง€๋งŒ ๋จผ์ € Transformer ๋ชจ๋ธ์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ž…๋ ฅ(inputs)์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํ† ํฌ ๋‚˜์ด์ €(tokenizer)๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณผ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ํ† ํฌ ๋‚˜์ด์ € (Tokenizer) ํ† ํฌ ๋‚˜์ด์ €๋Š” NLP ํŒŒ์ดํ”„๋ผ์ธ์˜ ํ•ต์‹ฌ ๊ตฌ์„ฑ ์š”์†Œ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋‹จ์ง€ 1๊ฐ€์ง€ ๋ชฉ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ๋ฅผ ๋ชจ๋ธ์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ์ˆซ์ž๋งŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ…์ŠคํŠธ ์ž…๋ ฅ์„ ์ˆซ์ž ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ํ† ํฐํ™” ํŒŒ์ดํ”„๋ผ์ธ(tokenization pipeline)์—์„œ ์ •ํ™•ํžˆ ์–ด๋–ค ์ผ์ด ๋ฐœ์ƒํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. NLP ์ž‘์—…์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ์›์‹œ ํ…์ŠคํŠธ(raw text)์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์›์‹œ ํ…์ŠคํŠธ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค: Jim Henson was a puppeteer ๊ทธ๋Ÿฌ๋‚˜ ๋ชจ๋ธ์€ ์ˆซ์ž๋งŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์›์‹œ ํ…์ŠคํŠธ๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ํ•˜๋Š” ์ผ์ด๋ฉฐ, ์ด๋ฅผ ์œ„ํ•ด์„œ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €์˜ ๋ชฉํ‘œ๋Š” ๊ฐ€์žฅ ์˜๋ฏธ ์žˆ๋Š” ํ‘œํ˜„(meaningful representation), ์ฆ‰ ๋ชจ๋ธ์— ๊ฐ€์žฅ ์ ํ•ฉํ•˜๋ฉด์„œ ์ตœ๋Œ€ํ•œ ๊ฐ„๊ฒฐํ•œ ํ‘œํ˜„์„ ์ฐพ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ† ํฐ ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ณ  ํ† ํฐํ™”์— ๋Œ€ํ•ด ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์งˆ๋ฌธ์— ๋‹ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ๊ธฐ๋ฐ˜ ํ† ํฐํ™” (Word-based Tokenization) ๊ฐ€์žฅ ๋จผ์ € ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ํ† ํฐํ™”(tokenization) ํ˜•ํƒœ๋Š” ๋‹จ์–ด ๊ธฐ๋ฐ˜ (word-based)์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ช‡ ๊ฐ€์ง€ ๊ทœ์น™๋งŒ ๊ฐ€์ง€๊ณ ๋„ ์„ค์ • ๋ฐ ์‚ฌ์šฉ์ด ๋งค์šฐ ์‰ฝ๊ณ , ์ข…์ข… ๊ดœ์ฐฎ์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ์˜ ํ† ํฐํ™” ๊ณผ์ •์€ ์›์‹œ ํ…์ŠคํŠธ๋ฅผ ๋‹จ์–ด๋กœ ๋‚˜๋ˆ„๊ณ  ๊ฐ๊ฐ์— ๋Œ€ํ•œ ์ˆซ์ž ํ‘œํ˜„์„ ์ฐพ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: ํ…์ŠคํŠธ๋ฅผ ๋ถ„ํ• ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Python์˜ split() ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ๋‹จ์–ด๋กœ ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. tokenized_text = "Jim Henson was a puppeteer".split() print(tokenized_text) ['Jim', 'Henson', 'was', 'a', 'puppeteer'] ๊ตฌ๋‘์ ์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ๊ทœ์น™์ด ์žˆ๋Š” ๋‹จ์–ด ๊ธฐ๋ฐ˜ ํ† ํฌ ๋‚˜์ด์ €(word-based tokenization)์˜ ๋ณ€ํ˜•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ง๋ญ‰์น˜์— ์กด์žฌํ•˜๋Š” ๋…๋ฆฝ์ ์ธ ํ† ํฐ๋“ค์˜ ์ดํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š” ๊ฝค ํฐ ๊ทœ๋ชจ์˜ vocabulary๋“ค์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์—๋Š” 0์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์–ดํœ˜์ง‘(vocabulary) ํฌ๊ธฐ(๊ฐœ์ˆ˜) ์‚ฌ์ด์˜ ID(์‹๋ณ„์ž)๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ์ด๋Ÿฌํ•œ ID๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋‹จ์–ด๋ฅผ ์‹๋ณ„ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด ๊ธฐ๋ฐ˜ ํ† ํฌ ๋‚˜์ด์ €๋กœ ํŠน์ • ์–ธ์–ด๋ฅผ ์™„์ „ํžˆ ์ปค๋ฒ„ํ•˜๋ ค๋ฉด, ํ•ด๋‹น ์–ธ์–ด์˜ ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•œ ์‹๋ณ„์ž๊ฐ€ ํ•„์š”ํ•˜๊ณ , ์ด๋Š” ์—„์ฒญ๋‚œ ์–‘์˜ ํ† ํฐ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์˜์–ด์—๋Š” 500,000๊ฐœ ์ด์ƒ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๊ฐœ๋ณ„ ๋‹จ์–ด์— ๋Œ€ํ•œ input ID(์ž…๋ ฅ ์‹๋ณ„์ž)๋กœ์˜ ๋งคํ•‘์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ทธ๋งŒํผ์˜ ์‹๋ณ„์ž๋“ค์„ ๊ฐ๋‹นํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€, "dog"์™€ ๊ฐ™์€ ๋‹จ์–ด๋Š” "dogs"์™€ ๊ฐ™์€ ๋‹จ์–ด์™€ ๋‹ค๋ฅด๊ฒŒ ํ‘œํ˜„๋˜๋ฉฐ, ๋ชจ๋ธ์€ ์ฒ˜์Œ์—๋Š” "dog"์™€ "dogs"๊ฐ€ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์ธ์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๋‘ ๋‹จ์–ด๋ฅผ ๊ด€๋ จ์ด ์—†๋Š” ๊ฒƒ์œผ๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” "run"๊ณผ "running"๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ์–ดํœ˜์ง‘(vocabulary)์— ์—†๋Š” ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž ์ •์˜ ํ† ํฐ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” "unknown" ํ† ํฐ์œผ๋กœ ์•Œ๋ ค์ ธ ์žˆ์œผ๋ฉฐ, ์ข…์ข… "[UNK]" ๋˜๋Š” ""๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์ด๋Ÿฌํ•œ "unknown" ํ† ํฐ์„ ๋งŽ์ด ์ƒ์„ฑํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ํ•ด๋‹น ๋‹จ์–ด์˜ ํ•ฉ๋‹นํ•œ ํ‘œํ˜„(sensible representation)์„ ์ฐพ์„ ์ˆ˜ ์—†๊ณ , ๊ทธ ๊ณผ์ •์—์„œ ์ •๋ณด๋ฅผ ์žƒ์–ด๋ฒ„๋ฆฐ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ ๋‚˜์œ ์ง•์กฐ์ž…๋‹ˆ๋‹ค. ์–ดํœ˜์ง‘(vocabulary)์„ ๋งŒ๋“ค ๋•Œ, ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์ด๋Ÿฌํ•œ "unknown" ํ† ํฐ๋“ค์„ ์ตœ๋Œ€ํ•œ ์ ๊ฒŒ ์ถœ๋ ฅํ•˜๊ฒŒ๋” ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. "Unknown" ํ† ํฐ์˜ ์–‘์„ ์ค„์ด๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ๋ฌธ์ž ๊ธฐ๋ฐ˜ (character-based) ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ๋‹จ๊ณ„ ๋” ๊นŠ์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž ๊ธฐ๋ฐ˜ ํ† ํฐํ™” (Character-based Tokenization) ๋ฌธ์ž ๊ธฐ๋ฐ˜ ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ…์ŠคํŠธ๋ฅผ ๋‹จ์–ด(words)๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž(characters)๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค: ์–ดํœ˜์ง‘(vocabulary)์˜ ํฌ๊ธฐ๊ฐ€ ๋งค์šฐ ์ž‘์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋‹จ์–ด๋“ค์ด ๋ฌธ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, out-of-vocabulary (OOV, unknown) ํ† ํฐ์ด ํ›จ์”ฌ ์ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ๊ธฐ์—์„œ๋„ ๊ณต๋ฐฑ๊ณผ ๊ตฌ๋‘์ ์— ๊ด€ํ•œ ๋ช‡ ๊ฐ€์ง€ ์˜๋ฌธ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค: ์ด ๋ฐฉ์‹ ์—ญ์‹œ ์™„๋ฒฝํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ถ„๋ฆฌ๋œ ํ† ํฐ ํ‘œํ˜„ ์ž์ฒด๊ฐ€ ๋‹จ์–ด๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž ๊ธฐ๋ฐ˜์ด๋ฏ€๋กœ ์ง๊ด€์ ์œผ๋กœ ๋ณผ ๋•Œ ๊ฐ ํ† ํฐ์˜ ์˜๋ฏธ ํŒŒ์•…์ด ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋˜ํ•œ ์–ธ์–ด์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ค‘๊ตญ์–ด์—์„œ ๊ฐ ๋ฌธ์ž(ํ•œ์ž)๋Š” ๋ผํ‹ด(Latin) ์–ธ์–ด์˜ ๋ฌธ์ž๋ณด๋‹ค ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๊ณ ๋ คํ•ด์•ผ ํ•  ๋˜ ๋‹ค๋ฅธ ์‚ฌํ•ญ์€ ๋ชจ๋ธ์—์„œ ์ฒ˜๋ฆฌํ•  ๋งค์šฐ ๋งŽ์€ ์–‘์˜ ํ† ํฐ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด ๊ธฐ๋ฐ˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ ๋‹จ์–ด๋Š” ํ•˜๋‚˜์˜ ๋‹จ์ผ ํ† ํฐ์ด์ง€๋งŒ, ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด 10๊ฐœ ์ด์ƒ์˜ ํ† ํฐ์œผ๋กœ ์‰ฝ๊ฒŒ ๋ณ€ํ™˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์˜ ์žฅ์ ์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฅผ ๊ฒฐํ•ฉํ•œ ์„ธ ๋ฒˆ์งธ ๊ธฐ๋ฒ•์ธ subword tokenization(ํ•˜์œ„ ๋‹จ์–ด ํ† ํฐํ™”)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์œ„ ๋‹จ์–ด ํ† ํฐํ™” (Subword Tokenization) ํ•˜์œ„ ๋‹จ์–ด ํ† ํฐํ™”(subword tokenization) ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๋‹จ์–ด(frequently used words)๋Š” ๋” ์ž‘์€ ํ•˜์œ„ ๋‹จ์–ด(subword)๋กœ ๋ถ„ํ• ํ•˜์ง€ ์•Š๊ณ , ํฌ๊ท€ ๋‹จ์–ด(rare words)๋ฅผ ์˜๋ฏธ ์žˆ๋Š” ํ•˜์œ„ ๋‹จ์–ด(meaningful subwords)๋กœ ๋ถ„ํ• ํ•ด์•ผ ํ•œ๋‹ค๋Š” ์›์น™์— ๊ธฐ๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "annoyingly"๋Š” ํฌ๊ท€ ๋‹จ์–ด๋กœ ๊ฐ„์ฃผ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, "annoying"์™€ "ly"๋กœ ๋ถ„ํ•ด๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ ๋‘˜ ๋‹ค ๋…๋ฆฝ์ ์ธ ํ•˜์œ„ ๋‹จ์–ด(standalone subwords)๋กœ ๋” ์ž์ฃผ ์ถœํ˜„ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์œผ๋ฉฐ ๋™์‹œ์— "annoyingly"์˜ ์˜๋ฏธ๋Š” "annoying"์™€ "ly"์˜ ํ•ฉ์„ฑ ์˜๋ฏธ(composite meaning)๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ํ•˜์œ„ ๋‹จ์–ด ํ† ํฐํ™”(subword tokenization) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด "Let's do tokenization!" ์‹œํ€€์Šค๋ฅผ ํ† ํฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์ž…๋‹ˆ๋‹ค: ์œ„ ๊ทธ๋ฆผ์—์„œ์˜ ํ•˜์œ„ ๋‹จ์–ด๋“ค(subwords)์€ ์ถฉ๋ถ„ํ•œ ์–‘์˜ ์˜๋ฏธ ์ •๋ณด(semantic meaning)์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„์˜ ์˜ˆ์—์„œ "tokenization"๋Š” "token"๊ณผ "iztion"์œผ๋กœ ๋ถ„๋ฆฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ํ† ํฐ์€ ๊ฐ๊ฐ์ด ์˜๋ฏธ ์ •๋ณด(semantic meaning)์„ ๊ฐ€์ง€๋ฉด์„œ๋„ ๊ณต๊ฐ„ ํšจ์œจ์ (space-efficient)์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๊ธธ์ด๊ฐ€ ๊ธด ํ•œ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹จ ๋‘ ๊ฐœ์˜ ํ† ํฐ๋งŒ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์šฐ๋ฆฌ๋Š” ๊ทœ๋ชจ๊ฐ€ ์ž‘์€, ๋‹ค์‹œ ๋งํ•ด์„œ, ๊ตฌ์„ฑ ์–ดํœ˜๊ฐ€ ๋งŽ์ง€ ์•Š์€ ์–ดํœ˜์ง‘(vocabulary)์œผ๋กœ๋„ ์ถฉ๋ถ„ํžˆ ๋งŽ์€ ์ˆ˜์˜ ํ† ํฐ๋“ค์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ , "unknown" ํ† ํฐ์ด ๊ฑฐ์˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์€ ํ•˜์œ„ ๋‹จ์–ด(subwords)๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๊ธธ์ด๊ฐ€ ๊ธด ๋ณต์žกํ•œ ๋‹จ์–ด๋ฅผ ์ž„์˜๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ํ„ฐํ‚ค์–ด(Turkish)์™€ ๊ฐ™์€ ๊ต์ฐฉ ์–ธ์–ด(agglutinative languages)์—์„œ ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์„ธ๋ถ€ ๊ธฐ๋ฒ•๋“ค ์ง์ž‘์€ ํ–ˆ๊ฒ ์ง€๋งŒ, ์ด ํ•˜์œ„ ๋‹จ์–ด ํ† ํฐํ™”์™€ ๊ด€๋ จ๋œ ๋” ๋งŽ์€ ๊ธฐ๋ฒ•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: Byte-level BPE (GPT-2์— ์‚ฌ์šฉ๋จ) WordPiece (BERT์— ์‚ฌ์šฉ๋จ) SentencePiece, Unigram (๋ช‡๋ช‡ ๋‹ค๊ตญ์–ด ๋ชจ๋ธ์— ์‚ฌ์šฉ๋จ) ์ด์ œ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์— ๋Œ€ํ•œ ์ถฉ๋ถ„ํ•œ ์ง€์‹์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ๋ณธ๊ฒฉ์ ์œผ๋กœ API๋ฅผ ๊ณต๋ถ€ํ•ด ๋ด…์‹œ๋‹ค. ํ† ํฌ ๋‚˜์ด์ € ๋กœ๋”ฉ ๋ฐ ์ €์žฅ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ๊ฒƒ์€ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ, ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ์ €์žฅํ•  ๋•Œ์™€ ๊ฐ™์ด, from_pretrained() ๋ฐ save_pretrained() ๋ฉ”์„œ๋“œ(method)๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋“ค ๋ฉ”์„œ๋“œ(method)๋“ค์€ ํ† ํฌ ๋‚˜์ด์ €(๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜์™€ ์•ฝ๊ฐ„ ๋น„์Šทํ•จ)์™€ ์–ดํœ˜์ง‘(vocabulary, ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜(weights)์™€ ๋น„์Šทํ•จ)์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋กœ๋“œํ•˜๊ฑฐ๋‚˜ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. BERT์™€ ๋™์ผํ•œ ์ฒดํฌํฌ์ธํŠธ(checkpoint)๋กœ ํ•™์Šต๋œ BERT ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์€ BertTokenizer ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์„ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. from transformers import BertTokenizer tokenizer = BertTokenizer.from_pretrained("bert-base-cased") AutoModel ํด๋ž˜์Šค์™€ ์œ ์‚ฌํ•˜๊ฒŒ AutoTokenizer ํด๋ž˜์Šค๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ฒดํฌํฌ์ธํŠธ ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ํ† ํฌ ๋‚˜์ด์ € ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด์˜ ๋‹ค๋ฅธ ๋ชจ๋“  ์ฒดํฌํฌ์ธํŠธ์™€ ํ•จ๊ป˜ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") ์ด์ œ ์œ„์—์„œ์ฒ˜๋Ÿผ ๋™์ผํ•˜๊ฒŒ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. tokenizer("Using a Transformer network is simple") {'input_ids': [101, 7993, 170, 13809, 23763, 2443, 1110, 3014, 102], 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1]} ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์€ ๋ชจ๋ธ์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. tokenizer.save_pretrained("saving_folder") ('saving_folder/tokenizer_config.json', 'saving_folder/special_tokens_map.json', 'saving_folder/vocab.txt', 'saving_folder/added_tokens.json', 'saving_folder/tokenizer.json') ์œ„ ํ† ํฌ ๋‚˜์ด์ € ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ค‘์—์„œ token_type_ids์— ๋Œ€ํ•ด์„œ๋Š” 3์žฅ์—์„œ ๋” ์ด์•ผ๊ธฐํ•˜๊ณ , attention_mask ํ‚ค์— ๋Œ€ํ•ด์„œ๋Š” ์ž ์‹œ ํ›„์— ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € input_ids ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ์„ฑ๋˜๋Š”์ง€ ์‚ดํŽด๋ด…์‹œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ํ† ํฌ ๋‚˜์ด์ €์˜ ์ค‘๊ฐ„ ๋ฉ”์„œ๋“œ(intermediate methods)๋ฅผ ์‚ดํŽด๋ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”ฉ (Encoding) ํ…์ŠคํŠธ๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š”(translating text to numbers) ๊ณผ์ •์„ ์ธ์ฝ”๋”ฉ(encoding)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”ฉ(encoding)์€ ํ† ํฐํ™”์™€ ์ž…๋ ฅ ์‹๋ณ„์ž(input IDs)๋กœ์˜ ๋ณ€ํ™˜์ด๋ผ๋Š” 2๋‹จ๊ณ„ ํ”„๋กœ์„ธ์Šค๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋ณด์•˜๋“ฏ์ด, ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ํ…์ŠคํŠธ๋ฅผ ํ† ํฐ(tokens)์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๋‹จ์–ด(๋˜๋Š” ๋‹จ์–ด์˜ ์ผ๋ถ€, ๊ตฌ๋‘์  ๊ธฐํ˜ธ ๋“ฑ)๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ํ† ํฐํ™” ๋ฐฉ๋ฒ•๋“ค์ด ์กด์žฌํ•˜๊ณ  ๊ฐ ๋ชจ๋ธ๋“ค์ด ์‚ฌ์ „ํ•™์Šต๋  ๋•Œ ์‚ฌ์šฉํ•œ ํ† ํฐํ™” ๋ฐฉ๋ฒ•์ด ๋‹ค์–‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ณธ์ธ์ด ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๋ชจ๋ธ์˜ ์ด๋ฆ„์„ ์ด์šฉํ•˜์—ฌ ํ† ํฌ ๋‚˜์ด์ €๋„ ์ธ์Šคํ„ด์Šคํ™”(instantiate) ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ ํ•ด๋‹น ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉํ•œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ํ† ํฐํ™” ๊ฒฐ๊ณผ์ธ ํ† ํฐ๋“ค์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ…์„œ(tensor)๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ ๋ชจ๋ธ์— ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํ† ํฌ ๋‚˜์ด์ €๋Š” from_pretrained() ๋ฉ”์„œ๋“œ๋กœ ์ธ์Šคํ„ด์Šคํ™”ํ•  ๋•Œ ๋‹ค์šด๋กœ๋“œ๋˜๋Š” ํŒŒ์ผ ์ค‘์˜ ํ•˜๋‚˜๋กœ vocabulary๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„, ๋ชจ๋ธ์ด ์‚ฌ์ „ํ•™์Šต๋  ๋•Œ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•œ ์–ดํœ˜์ง‘(vocabulary)์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๋‘ ๋‹จ๊ณ„๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ† ํฐํ™” ํŒŒ์ดํ”„๋ผ์ธ(tokenization pipeline) ์ „์ฒด ๊ณผ์ •์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๋“ค์„ ์‹คํ–‰ํ•˜์—ฌ ์ค‘๊ฐ„ ๋‹จ๊ณ„์˜ ๊ฒฐ๊ณผ๋ฌผ๋“ค์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ๋กœ๋Š” ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•œ ๋ฒˆ์— ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ํ† ํฐํ™” ์ž‘์—… ํ† ํฐํ™” ํ”„๋กœ์„ธ์Šค๋Š” ํ† ํฌ ๋‚˜์ด์ €์˜ tokenize() ๋ฉ”์„œ๋“œ์— ์˜ํ•ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") sequence = "Using a Transformer network is simple" tokens = tokenizer.tokenize(sequence) print(tokens) ['Using', 'a', 'Trans', '##former', 'network', 'is', 'simple'] ์ด ๋ฉ”์„œ๋“œ์˜ ์ถœ๋ ฅ์€ ๋ฌธ์ž์—ด ๋˜๋Š” ํ† ํฐ๋“ค์˜ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ณด๋“ฏ์ด ์—ฌ๊ธฐ์„œ ์‹คํ–‰ํ•œ ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ•˜์œ„ ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €(subword tokenization)์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ์–ดํœ˜์ง‘(vocabulary)์— ์กด์žฌํ•˜๋Š” ํ† ํฐ๋“ค์„ ์–ป์„ ์ˆ˜ ์žˆ์„ ๋•Œ๊นŒ์ง€ ๋‹จ์–ด๋ฅผ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. Transformer๋Š” Trans ์™€ ##former๋ผ๋Š” ๋‘ ๊ฐœ์˜ ํ† ํฐ์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์ด๋“ค ํ† ํฐ๋“ค์€ ๋ชจ๋‘ ์–ดํœ˜์ง‘(vocabulary)์— ์กด์žฌํ•˜๋Š” ํ† ํฐ๋“ค์ž…๋‹ˆ๋‹ค. ํ† ํฐ์„ ์ž…๋ ฅ ์‹๋ณ„์ž๋กœ ๋ณ€ํ™˜ (From tokens to input IDs) ์ดํ›„ ๊ฐ ํ† ํฐ๋“ค์˜ ์ž…๋ ฅ ์‹๋ณ„์ž(input IDs)๋กœ์˜ ๋ณ€ํ™˜์€ convert_tokens_to_ids() ๋ฉ”์„œ๋“œ์— ์˜ํ•ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ids = tokenizer.convert_tokens_to_ids(tokens) print(ids) [7993, 170, 13809, 23763, 2443, 1110, 3014] ์œ„ ์ถœ๋ ฅ์€ ์ผ๋‹จ ์ ์ ˆํ•œ ํ”„๋ ˆ์ž„์›Œํฌ ํ…์„œ(framework tensor)๋กœ ๋ณ€ํ™˜๋˜๋ฉด, ์ด ์žฅ์˜ ์•ž๋ถ€๋ถ„์—์„œ ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ, ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”ฉ (Decoding) ๋””์ฝ”๋”ฉ(decoding)์€ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ๋ณ€ํ™˜๋œ ์ž…๋ ฅ ์‹๋ณ„์ž(input IDs)๋ฅผ ์ด์šฉํ•ด์„œ ์–ดํœ˜์ง‘(vocabulary)์—์„œ ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ์ฐพ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด decode() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. decoded_string = tokenizer.decode([7993, 170, 13809, 23763, 2443, 1110, 3014]) print(decoded_string) Using a Transformer network is simple decode() ๋ฉ”์„œ๋“œ๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋‹ค์‹œ ํ† ํฐ์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ•˜์œ„ ๋‹จ์–ด(subword)๋กœ ๋ถ„ํ• ๋œ ํ† ํฐ์„ ๋ณ‘ํ•ฉํ•˜์—ฌ, ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ์›๋ณธ ๋ฌธ์žฅ์„ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด ๋™์ž‘์€ ์ƒˆ๋กœ์šด ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋ธ, ๋‹ค์‹œ ๋งํ•ด์„œ, ํ”„๋กฌํ”„ํŠธ(prompt)์—์„œ์˜ ํ…์ŠคํŠธ ์ƒ์„ฑ, ๋ฒˆ์—ญ(translation), ์š”์•ฝ(summarization) ๋“ฑ๊ณผ ๊ฐ™์€ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค(sequence-to-sequence) ๋ฌธ์ œ ๋“ฑ์„ ๋‹ค๋ฃฐ ๋•Œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€, ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์ˆ˜ํ–‰ํ•˜๋Š” ์„ธ๋ถ€์ ์ธ ์ž‘์—…๋“ค ์ฆ‰, ํ† ํฐํ™”(tokenization), ์‹๋ณ„์ž(ID)๋กœ ๋ณ€ํ™˜(conversion to IDs), ์‹๋ณ„์ž(ID)๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋‹ค์‹œ ๋ณ€ํ™˜(converting IDs back to string) ์ž‘์—…๋“ค์„ ํŒŒ์•…ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Š” ๋‹จ์ง€ ๋น™์‚ฐ์˜ ์ผ๊ฐ์ผ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ์„น์…˜์—์„œ๋Š” ์œ„ ๋ฐฉ๋ฒ•๋“ค์˜ ํ•œ๊ณ„๋ฅผ ์•Œ์•„๋ณด๊ณ  ์ด๋ฅผ ๊ทน๋ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4. ๋‹ค์ค‘ ์‹œํ€€์Šค ์ฒ˜๋ฆฌ ์ด์ „ ์„น์…˜์—์„œ ์šฐ๋ฆฌ๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ™œ์šฉ ์‚ฌ๋ก€๋ฅผ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์ผ ์‹œํ€€์Šค์— ๋Œ€ํ•œ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ช‡ ๊ฐ€์ง€ ์˜๋ฌธ์ ์ด ๋ฒŒ์จ ๋จธ๋ฆฟ์†์— ๋‚จ์Šต๋‹ˆ๋‹ค: ๋‹ค์ค‘ ์‹œํ€€์Šค(multiple sequences)๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ๊นŒ? ๊ฐ๊ฐ์ด ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‹œํ€€์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ๊นŒ? ๋ชจ๋ธ์ด ์ž˜ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์–ดํœ˜์ง‘(vocabulary)์˜ ์ธ๋ฑ์Šค๋“ค๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋ ๊นŒ? ๊ธธ์ด๊ฐ€ ์—„์ฒญ๋‚˜๊ฒŒ ๊ธด ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ๋Š” ์ž˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์œ„ ์งˆ๋ฌธ๋“ค์ด ์–ด๋–ค ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ค๊ณ  ์ด๋ฅผ Transformers API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค. ๋ชจ๋ธ(model)์€ ์ž…๋ ฅ์˜ ๋ฐฐ์น˜(batch) ํ˜•ํƒœ๋ฅผ ์š”๊ตฌํ•œ๋‹ค. ์ด์ „ ์„น์…˜์—์„œ ์‹œํ€€์Šค๊ฐ€ ์ˆซ์ž ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜๋˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด ์ˆซ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ํ…์„œ(tensor)๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ชจ๋ธ์— ์ž…๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequence = "I've been waiting for a HuggingFace course my whole life." tokens = tokenizer.tokenize(sequence) ids = tokenizer.convert_tokens_to_ids(tokens) input_ids = torch.tensor(ids) # This line will fail model(input_ids) --------------------------------------------------------------------------- IndexError Traceback (most recent call last) /tmp/ipykernel_9651/1126667217.py in <module> 12 input_ids = torch.tensor(ids) 13 # This line will fail ---> 14 model(input_ids) ~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1101 or _global_forward_hooks or _global_forward_pre_hooks): -> 1102 return forward_call(*input, **kwargs) 1103 # Do not call functions when jit is used 1104 full_backward_hooks, non_full_backward_hooks = [], [] ~/anaconda3/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, labels, output_attentions, output_hidden_states, return_dict) 727 return_dict = return_dict if return_dict is not None else self.config.use_return_dict 728 --> 729 distilbert_output = self.distilbert( 730 input_ids=input_ids, 731 attention_mask=attention_mask, ~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1101 or _global_forward_hooks or _global_forward_pre_hooks): -> 1102 return forward_call(*input, **kwargs) 1103 # Do not call functions when jit is used 1104 full_backward_hooks, non_full_backward_hooks = [], [] ~/anaconda3/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, output_attentions, output_hidden_states, return_dict) 548 549 if inputs_embeds is None: --> 550 inputs_embeds = self.embeddings(input_ids) # (bs, seq_length, dim) 551 return self.transformer( 552 x=inputs_embeds, ~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1101 or _global_forward_hooks or _global_forward_pre_hooks): -> 1102 return forward_call(*input, **kwargs) 1103 # Do not call functions when jit is used 1104 full_backward_hooks, non_full_backward_hooks = [], [] ~/anaconda3/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids) 117 embeddings) 118 """ --> 119 seq_length = input_ids.size(1) 120 121 # Setting the position-ids to the registered buffer in constructor, it helps IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) ์ด๋Ÿฐ! ๋ญ๊ฐ€ ๋ฌธ์ œ์ผ๊นŒ์š”? ์œ„ ์ฝ”๋“œ์—์„œ ์šฐ๋ฆฌ๋Š” ์„น์…˜ 2์—์„œ์˜ ํŒŒ์ดํ”„๋ผ์ธ ๋‹จ๊ณ„๋ฅผ ๊ทธ๋Œ€๋กœ ๋”ฐ๋ž์Šต๋‹ˆ๋‹ค. ์œ„ ๋ฌธ์ œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋ชจ๋ธ์— ํ•˜๋‚˜์˜ ๋‹จ์ผ ์‹œํ€€์Šค๋ฅผ ์ž…๋ ฅํ•ด์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. Transformers ๋ชจ๋ธ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์ค‘ ๋ฌธ์žฅ(์‹œํ€€์Šค)์„ ํ•œ ๋ฒˆ์— ์ž…๋ ฅํ•˜๊ธฐ๋ฅผ ๊ธฐ๋Œ€ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ์šฐ๋ฆฌ๋Š” ์‹œํ€€์Šค์— ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ ์šฉํ•  ๋•Œ ์‹ค์ œ ๋‚ด๋ถ€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋“  ์ž‘์—…์„ ์‹œ๋„ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž์„ธํžˆ ๋ณด๋ฉด ์ž…๋ ฅ ์‹๋ณ„์ž(input IDs) ๋ฆฌ์ŠคํŠธ๋ฅผ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋™์‹œ์— ์ฐจ์›(dimension) ํ•˜๋‚˜๊ฐ€ ๊ทธ ์œ„์— ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenized_inputs = tokenizer(sequence, return_tensors="pt") print(tokenized_inputs["input_ids"]) tensor([[ 101, 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012, 102]]) ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ์ฝ”๋“œ์—์„œ input_ids์— ์ƒˆ๋กœ์šด ์ฐจ์›์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequence = "I've been waiting for a HuggingFace course my whole life." tokens = tokenizer.tokenize(sequence) ids = tokenizer.convert_tokens_to_ids(tokens) input_ids = torch.tensor([ids]) print("Input IDs:", input_ids) output = model(input_ids) print("Logits:", output.logits) Input IDs: tensor([[ 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012]]) Logits: tensor([[-2.7276, 2.8789]], grad_fn=<AddmmBackward0>) ์œ„ ์ฝ”๋“œ์—์„œ๋Š” ์ž…๋ ฅ ์‹๋ณ„์ž(input IDs)์™€ ๊ทธ ๊ฒฐ๊ณผ ๋กœ์ง“(logit) ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Batching ์ด๋ž€ ๋ชจ๋ธ์„ ํ†ตํ•ด ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋Š” ๋™์ž‘์ž…๋‹ˆ๋‹ค. ๋ฌธ์žฅ์ด ํ•˜๋‚˜๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹จ์ผ ์‹œํ€€์Šค๋กœ ๋ฐฐ์น˜(batch)๋ฅผ ๋นŒ๋“œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. batch_ids = [ids, ids] ์ด๊ฒƒ์€ ๋™์ผํ•œ ๋‘ ์‹œํ€€์Šค๋กœ ๊ตฌ์„ฑ๋œ ๋ฐฐ์น˜(batch)์ž…๋‹ˆ๋‹ค! ๋ฐฐ์น˜(batch) ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด์„œ ๋ชจ๋ธ์ด ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ๋™์‹œ์— ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์ค‘ ์‹œํ€€์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋‹จ์ผ ์‹œํ€€์Šค๋กœ ๋ฐฐ์น˜(batch)๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ๋งŒํผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‘ ๋ฒˆ์งธ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐœ(๋˜๋Š” ๊ทธ ์ด์ƒ) ๋ฌธ์žฅ์„ ํ•จ๊ป˜ ๋ฐฐ์น˜(batch) ์ฒ˜๋ฆฌํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๊ฐ ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ํ…์„œ(tensor)๋ฅผ ์‚ฌ์šฉํ•ด ๋ณธ ์ ์ด ์žˆ๋‹ค๋ฉด ํ•ญ์ƒ ๊ทธ ํ˜•ํƒœ๊ฐ€ ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋Ÿด ๊ฒฝ์šฐ์—๋Š” ์ž…๋ ฅ ์‹๋ณ„์ž(input IDs) ๋ฆฌ์ŠคํŠธ๋ฅผ ํ…์„œ๋กœ ์ง์ ‘ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ผ๋ฐ˜์ ์œผ๋กœ ์ž…๋ ฅ์„ ์ฑ„์›๋‹ˆ๋‹ค(padding). ์ž…๋ ฅ์„ ํŒจ๋”ฉ(padding) ํ•˜๊ธฐ ๋‹ค์Œ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ(ํ˜น์€ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ)๋Š” ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. batched_ids = [ [200, 200, 200], [200, 200], ] ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ํŒจ๋”ฉ(padding) ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์„œ๋ฅผ ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํŒจ๋”ฉ(padding)์€ ๊ธธ์ด๊ฐ€ ๋” ์งง์€ ๋ฌธ์žฅ์— ํŒจ๋”ฉ ํ† ํฐ(padding token)์ด๋ผ๋Š” ํŠน์ˆ˜ ๋‹จ์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ชจ๋“  ๋ฌธ์žฅ์ด ๋™์ผํ•œ ๊ธธ์ด๋ฅผ ๊ฐ–๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 10๊ฐœ์˜ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋œ 10๊ฐœ์˜ ๋ฌธ์žฅ๊ณผ 20๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ๋Š” 1๊ฐœ์˜ ๋ฌธ์žฅ์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ํŒจ๋”ฉ(padding)์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ๋ฌธ์žฅ์— 20๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ batched_ids๋ฅผ ํŒจ๋”ฉ(padding) ์ฒ˜๋ฆฌํ•˜๋ฉด ๊ฒฐ๊ณผ ํ…์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: padding_id = 100 batched_ids = [ [200, 200, 200], [200, 200, padding_id], ] ํŒจ๋”ฉ ํ† ํฐ(padding token)์˜ ์‹๋ณ„์ž(ID)๋Š” tokenizer.pad_token_id์— ์ง€์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‘ ๊ฐœ์˜ ์‹œํ€€์Šค๋ฅผ ํ•œ๋ฒˆ์€ ๊ฐœ๋ณ„์ ์œผ๋กœ ๋˜ ํ•œ ๋ฒˆ์€ ๋ฐฐ์น˜(batch) ํ˜•ํƒœ๋กœ ๋ชจ๋ธ์— ์ž…๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequence1_ids = [[200, 200, 200]] sequence2_ids = [[200, 200]] batched_ids = [ [200, 200, 200], [200, 200, tokenizer.pad_token_id], ] print(model(torch.tensor(sequence1_ids)).logits) print(model(torch.tensor(sequence2_ids)).logits) print(model(torch.tensor(batched_ids)).logits) tensor([[ 1.5694, -1.3895]], grad_fn=<AddmmBackward0>) tensor([[ 0.5803, -0.4125]], grad_fn=<AddmmBackward0>) tensor([[ 1.5694, -1.3895], [ 1.3374, -1.2163]], grad_fn=<AddmmBackward0>) ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ๋œ ์˜ˆ์ธก ๊ฒฐ๊ณผ์˜ ๋กœ์ง“(logits)์— ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ํ–‰์€ ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์˜ ๋กœ์ง“(logits)๊ณผ ๊ฐ™์•„์•ผ ํ•˜์ง€๋งŒ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ–์Šต๋‹ˆ๋‹ค! ์ด๋Š” ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ๋ชจ๋ธ์˜ ํ•ต์‹ฌ์ ์ธ ํŠน์ง•์ด ๊ฐ ํ† ํฐ์„ ์ปจ ํ…์ŠคํŠธํ™”(contextualize) ํ•˜๋Š” ์–ดํ…์…˜ ๋ ˆ์ด์–ด(attention layers)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ ˆ์ด์–ด(attention layers)๋Š” ์‹œํ€€์Šค์˜ ๋ชจ๋“  ํ† ํฐ์— ์ฃผ์˜ ์ง‘์ค‘(paying attention)์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŒจ๋”ฉ ํ† ํฐ๋„ ์—ญ์‹œ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์— ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฐœ๋ณ„ ๋ฌธ์žฅ๋“ค์„ ์ž…๋ ฅํ•  ๋•Œ๋‚˜ ๋™์ผํ•œ ๋ฌธ์žฅ์œผ๋กœ ๊ตฌ์„ฑ๋œ ํŒจ๋”ฉ์ด ์ ์šฉ๋œ ๋ฐฐ์น˜(batch)๋ฅผ ์ž…๋ ฅํ•  ๋•Œ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ์–ดํ…์…˜ ๋ ˆ์ด์–ด(attention layers)๊ฐ€ ํŒจ๋”ฉ ํ† ํฐ์„ ๋ฌด์‹œํ•˜๋„๋ก ์ง€์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์–ดํ…์…˜ ๋งˆ์Šคํฌ(attention mask)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋งˆ์Šคํฌ (attention masks) ์–ดํ…์…˜ ๋งˆ์Šคํฌ(attention mask)๋Š” 0๊ณผ 1๋กœ ์ฑ„์›Œ์ง„ ์ž…๋ ฅ ์‹๋ณ„์ž(input IDs) ํ…์„œ(tensor)์™€ ํ˜•ํƒœ๊ฐ€ ์ •ํ™•ํ•˜๊ฒŒ ๋™์ผํ•œ ํ…์„œ(tensor)์ž…๋‹ˆ๋‹ค. 1์€ ํ•ด๋‹น ํ† ํฐ์— ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•จ์„ ๋‚˜ํƒ€๋‚ด๊ณ  0์€ ํ•ด๋‹น ํ† ํฐ์„ ๋ฌด์‹œํ•ด์•ผ ํ•จ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ฆ‰, ๋ชจ๋ธ์˜ ์–ดํ…์…˜ ๋ ˆ์ด์–ด(attention layers)์—์„œ ๋ฌด์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋งˆ์Šคํฌ(attention mask)๋กœ ์ด์ „ ์˜ˆ์ œ๋ฅผ ์™„์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: batch_ids = [ [200, 200, 200], [200, 200, tokenizer.pad_token_id], ] attention_mask = [ [1, 1, 1], [1, 1, 0], ] outputs = model(torch.tensor(batch_ids), attention_mask=torch.tensor(attention_mask)) print(outputs.logits) tensor([[ 1.5694, -1.3895], [ 0.5803, -0.4125]], grad_fn=<AddmmBackward0>) ์ด์ œ ๋ฐฐ์น˜(batch)์˜ ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์— ๋Œ€ํ•ด ๋™์ผํ•œ ๋กœ์ง“(logits) ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์‹œํ€€์Šค์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์ด ํŒจ๋”ฉ ์‹๋ณ„์ž(padding ID)์ด๊ณ  ์ด์— ํ•ด๋‹นํ•˜๋Š” ์–ดํ…์…˜ ๋งˆ์Šคํฌ(attention mask)์˜ ๊ฐ’์ด 0์ธ ์ ์„ ์ฃผ์˜ํ•˜์„ธ์š”. ๊ธธ์ด๊ฐ€ ๋” ๊ธด ์‹œํ€€์Šค๋“ค ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ๋•Œ, ๋ชจ๋ธ์— ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์‹œํ€€์Šค์˜ ๊ธธ์ด์— ์ œํ•œ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ชจ๋ธ์€ ์ตœ๋Œ€ 512๊ฐœ ๋˜๋Š” 1024๊ฐœ์˜ ํ† ํฐ ์‹œํ€€์Šค๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ๊ทธ๋ณด๋‹ค ๋” ๊ธด ์‹œํ€€์Šค๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ผ๋Š” ์š”์ฒญ์„ ๋ฐ›์œผ๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ์— ๋Œ€ํ•œ ๋‘ ๊ฐ€์ง€ ์„ค๋ฃจ์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธธ์ด๊ฐ€ ๋” ๊ธด ์‹œํ€€์Šค๋ฅผ ์ง€์›ํ•˜๋Š” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค. ์‹œํ€€์Šค๋ฅผ ์ ˆ๋‹จํ•ฉ๋‹ˆ๋‹ค(truncation). ๋ชจ๋ธ ๋ณ„๋กœ ์ง€์›๋˜๋Š” ์‹œํ€€์Šค ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉฐ ์ผ๋ถ€ ๋ชจ๋ธ์€ ๋งค์šฐ ๊ธด ์‹œํ€€์Šค ์ฒ˜๋ฆฌ์— ํŠนํ™”๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. Longformer๊ฐ€ ํ•˜๋‚˜์˜ ์˜ˆ์ด๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” LED์ž…๋‹ˆ๋‹ค. ๋งค์šฐ ๊ธด ์‹œํ€€์Šค๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ํƒœ์Šคํฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ๋ชจ๋ธ์„ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด, max_sequence_length ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ์‹œํ€€์Šค๋ฅผ ์ ˆ๋‹จํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. max_sequence_length = 512 sequence = sequence[:max_sequence_length] 5. 2์žฅ ์š”์•ฝ (Summary) ์ง€๋‚œ ๋ช‡ ์„น์…˜์—์„œ ์šฐ๋ฆฌ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์ž‘์—…์„ ์ง์ ‘ ์„ธ๋ถ€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์„ ์„ ๋‹คํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ† ํฌ ๋‚˜์ด์ €์˜ ์ž‘๋™ ๋ฐฉ์‹๊ณผ ํ† ํฐํ™”(tokenization), ์ž…๋ ฅ ์‹๋ณ„์ž(input IDs)๋กœ์˜ ๋ณ€ํ™˜, ํŒจ๋”ฉ(padding), ์ ˆ๋‹จ(truncation) ๋ฐ ์–ดํ…์…˜ ๋งˆ์Šคํฌ(attention mask) ๋“ฑ์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์„น์…˜ 2์—์„œ ๋ณด์•˜๋“ฏ์ด Transformers API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๊ธฐ์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃฐ ์˜ˆ์ •์ธ ๊ณ ์ˆ˜์ค€ ํ•จ์ˆ˜๋“ค(high-level functions)๋กœ ์ด ๋ชจ๋“  ๊ฒƒ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์ง์ ‘ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ชจ๋ธ์— ์ „๋‹ฌ๋  ์ค€๋น„๊ฐ€ ๋œ ์ตœ์ข… ์ž…๋ ฅ ํ˜•ํƒœ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import AutoTokenizer checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(checkpoint) sequence = "I've been waiting for a HuggingFace course my whole life." model_inputs = tokenizer(sequence) ์—ฌ๊ธฐ์—์„œ model_inputs ๋ณ€์ˆ˜๋Š” ๋ชจ๋ธ์ด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“  ์ •๋ณด๋“ค์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. DistilBERT์˜ ๊ฒฝ์šฐ, model_inputs์—๋Š” ์ž…๋ ฅ ์‹๋ณ„์ž(input IDs)์™€ ์–ดํ…์…˜ ๋งˆ์Šคํฌ(attention mask)๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์— ๋”ฐ๋ผ์„œ tokenizer ๊ฐ์ฒด๋Š” ๋ชจ๋ธ์— ํ•„์š”ํ•œ ์ž…๋ ฅ ์ •๋ณด๋“ค์„ ์•Œ์•„์„œ ์ œ๊ณตํ•ด ์ค๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด tokenizer ๋ฉ”์„œ๋“œ๋Š” ๋งค์šฐ ๊ฐ•๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋‹จ์ผ ์‹œํ€€์Šค๋ฅผ ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: sequence = "I've been waiting for a HuggingFace course my whole life." model_inputs = tokenizer(sequence) ๋˜ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ ์—†์ด ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ์‹œํ€€์Šค๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค: sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] model_inputs = tokenizer(sequences) ๊ทธ๋ฆฌ๊ณ  ๋‹ค์–‘ํ•œ ๋ชจ๋“œ์— ๋”ฐ๋ผ ํŒจ๋”ฉ(padding) ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: # ํ•ด๋‹น ์‹œํ€€์Šค๋ฅผ ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ์ตœ๋Œ€ ์‹œํ€€์Šค ๊ธธ์ด๊นŒ์ง€ ํŒจ๋”ฉ(padding) ํ•ฉ๋‹ˆ๋‹ค. model_inputs = tokenizer(sequences, padding="longest") # ์‹œํ€€์Šค๋ฅผ ๋ชจ๋ธ ์ตœ๋Œ€ ๊ธธ์ด(model max length)๊นŒ์ง€ ํŒจ๋”ฉ(padding) ํ•ฉ๋‹ˆ๋‹ค. # (512 for BERT or DistilBERT) model_inputs = tokenizer(sequences, padding="max_length") # ์ง€์ •๋œ ์ตœ๋Œ€ ๊ธธ์ด๊นŒ์ง€ ์‹œํ€€์Šค๋ฅผ ํŒจ๋”ฉ(padding) ํ•ฉ๋‹ˆ๋‹ค. model_inputs = tokenizer(sequences, padding="max_length", max_length=8) ์‹œํ€€์Šค๋ฅผ ์ž๋ฅผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] # ๋ชจ๋ธ ์ตœ๋Œ€ ๊ธธ์ด(model max length)๋ณด๋‹ค ๊ธด ์‹œํ€€์Šค๋ฅผ ์ž๋ฆ…๋‹ˆ๋‹ค. # (512 for BERT or DistilBERT) model_inputs = tokenizer(sequences, truncation=True) # ์ง€์ •๋œ ์ตœ๋Œ€ ๊ธธ์ด๋ณด๋‹ค ๊ธด ์‹œํ€€์Šค๋ฅผ ์ž๋ฆ…๋‹ˆ๋‹ค. model_inputs = tokenizer(sequences, max_length=8, truncation=True) ํŠน์ˆ˜ ํ† ํฐ๋“ค (Special tokens) ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ์ž…๋ ฅ ์‹๋ณ„์ž(input IDs)๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์ด์ „๊ณผ ์•ฝ๊ฐ„ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: sequence = "I've been waiting for a HuggingFace course my whole life." model_inputs = tokenizer(sequence) print(model_inputs["input_ids"]) tokens = tokenizer.tokenize(sequence) ids = tokenizer.convert_tokens_to_ids(tokens) print(ids) [101, 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012, 102] [1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012] ์ƒˆ๋กœ์šด ํ† ํฐ ์‹๋ณ„์ž๊ฐ€ ์ฒ˜์Œ๊ณผ ๋งˆ์ง€๋ง‰์— ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋‘ ๊ฐ€์ง€ ์‹๋ณ„์ž ์‹œํ€€์Šค๋ฅผ ๋””์ฝ”๋”ฉ(decoding) ํ•˜์—ฌ ์ด๊ฒƒ์ด ๋ฌด์—‡์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ธ์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: print(tokenizer.decode(model_inputs["input_ids"])) print(tokenizer.decode(ids)) [CLS] i've been waiting for a huggingface course my whole life. [SEP] i've been waiting for a huggingface course my whole life. ํ† ํฌ ๋‚˜์ด์ €๋Š” ์‹œ์ž‘ ๋ถ€๋ถ„์— ํŠน์ˆ˜ ๋‹จ์–ด [CLS]๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋์— ํŠน์ˆ˜ ๋‹จ์–ด [SEP]๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด ํ•ด๋‹น ํŠน์ˆ˜ ํ† ํฐ๋“ค๋กœ ์‚ฌ์ „ ํ•™์Šต(pre-training) ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๋ก ์— ๋Œ€ํ•ด ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด ์ด๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ชจ๋ธ์€ ์ด๋“ค ํŠน์ˆ˜ ํ† ํฐ๋“ค์ด๋‚˜ ๋‹ค๋ฅธ ํŠน๋ณ„ํ•œ ๋‹จ์–ด๋“ค์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ ์–ด๋–ค ๋ชจ๋ธ์€ ์‹œ์ž‘ ๋ถ€๋ถ„์—๋งŒ ์ด๋Ÿฌํ•œ ํŠน์ˆ˜ ๋‹จ์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ํ˜น์€ ๋๋ถ€๋ถ„์—๋งŒ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด์จŒ๋“  ํ† ํฌ ๋‚˜์ด์ €๋Š” ์ž…๋ ฅ์ด ์˜ˆ์ƒ๋˜๋Š” ํ† ํฐ๋“ค์„ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ์œผ๋ฉฐ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งˆ๋ฌด๋ฆฌ: ํ† ํฌ ๋‚˜์ด์ €์—์„œ ๋ชจ๋ธ๋กœ... ์ด์ œ ํ† ํฌ ๋‚˜์ด์ € ๊ฐ์ฒด๊ฐ€ ํ…์ŠคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‹คํ–‰๋˜๋Š” ๋ชจ๋“  ๊ฐœ๋ณ„ ๋‹จ๊ณ„๋“ค์„ ์‚ดํŽด๋ณด์•˜์œผ๋ฏ€๋กœ, ์ฃผ์š” API๋กœ ๋‹ค์ค‘ ์‹œํ€€์Šค(ํŒจ๋”ฉ, padding!), ๋งค์šฐ ๊ธด ์‹œํ€€์Šค(์ ˆ๋‹จ, truncation!), ์—ฌ๋Ÿฌ ์œ ํ˜•์˜ ํ…์„œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ• ๋“ฑ์„ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•œ ๋ฒˆ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] tokens = tokenizer(sequences, padding=True, truncation=True, return_tensors="pt") output = model(**tokens) print(output) SequenceClassifierOutput(loss=None, logits=tensor([[-1.5607, 1.6123], [-3.6183, 3.9137]], grad_fn=<AddmmBackward0>), hidden_states=None, attentions=None) 3์žฅ. ์‚ฌ์ „ํ•™์Šต ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ฏธ์„ธ์กฐ์ • 2์žฅ์—์„œ๋Š” ํ† ํฌ ๋‚˜์ด์ €์™€ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณ ์œ ์˜ ๋ฐ์ดํ„ฐ ์…‹(dataset)์„ ๊ฐ€์ง€๊ณ  ๊ธฐ์กด์˜ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •(fine-tune) ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ์ด ์žฅ์˜ ๋‚ด์šฉ์ด ๋ฐ”๋กœ ๊ทธ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค! ์—ฌ๊ธฐ์„œ๋Š”, ํ—ˆ๋ธŒ(Hub)์—์„œ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ ์…‹์„ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. ๊ณ ๊ธ‰ Trainer API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •(fine-tune) ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณต๋ถ€ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ง€์ • ํ•™์Šต ๋ฃจํ”„(custom training loop)์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. Accelerate ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ถ„์‚ฐ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ์ž ์ง€์ • ํ•™์Šต ๋ฃจํ”„(custom training loop)์„ ์‰ฝ๊ฒŒ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณต๋ถ€ํ•ฉ๋‹ˆ๋‹ค. ๋ฏธ์„ธ ์กฐ์ •๋œ ์ฒดํฌํฌ์ธํŠธ(checkpoint)๋ฅผ Hugging Face Hub์— ์—…๋กœ๋“œํ•˜๋ ค๋ฉด huggingface.co ๊ณ„์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค: ๊ณ„์ • ์ƒ์„ฑ 1. ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ž‘์—… 2์žฅ์—์„œ ๊ณต๋ถ€ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด, PyTorch์—์„œ ๋‹จ์ผ ๋ฐฐ์น˜(batch)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‹œํ€€์Šค ๋ถ„๋ฅ˜๊ธฐ(sequence classifier)๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. import torch from transformers import AdamW, AutoTokenizer, AutoModelForSequenceClassification # 2์žฅ์˜ ์˜ˆ์ œ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. checkpoint = "bert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequences = [ "I've been waiting for a HuggingFace course my whole life.", "This course is amazing!", ] batch = tokenizer(sequences, padding=True, truncation=True, return_tensors="pt") # ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. batch["labels"] = torch.tensor([1, 1]) optimizer = AdamW(model.parameters()) loss = model(**batch).loss loss.backward() optimizer.step() Downloading: 0%| | 0.00/420M [00:00<?, ?B/s] Some weights of the model checkpoint at bert-base-uncased were not used when initializing BertForSequenceClassification: ['cls.predictions.transform.LayerNorm.bias', 'cls.predictions.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.dense.weight', 'cls.predictions.transform.dense.bias', 'cls.seq_relationship.weight', 'cls.predictions.decoder.weight', 'cls.seq_relationship.bias'] - This IS expected if you are initializing BertForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model). - This IS NOT expected if you are initializing BertForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model). Some weights of BertForSequenceClassification were not initialized from the model checkpoint at bert-base-uncased and are newly initialized: ['classifier.weight', 'classifier.bias'] You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference. ๋ฌผ๋ก  ๋‘ ๋ฌธ์žฅ๋งŒ์œผ๋กœ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์œผ๋กœ๋Š” ๊ทธ๋‹ค์ง€ ์ข‹์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋” ์ข‹์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด, ๋” ํฐ ๋ฐ์ดํ„ฐ ์…‹์„ ์ค€๋น„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” William B. Dolan๊ณผ Chris Brockett์˜ ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋œ MRPC(Microsoft Research Paraphrase Corpus) ๋ฐ์ดํ„ฐ ์…‹์„ ์˜ˆ์ œ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ ์…‹์€ 5,801๊ฑด์˜ ๋ฌธ์žฅ ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ ๊ฐ ๋ฌธ์žฅ ์Œ์˜ ๊ด€๊ณ„๊ฐ€ ์˜์—ญ(paraphrasing) ๊ด€๊ณ„์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ ˆ์ด๋ธ”์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค(์ฆ‰, ๋‘ ๋ฌธ์žฅ์ด ๋™์ผํ•œ ์˜๋ฏธ์ธ์ง€). ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ทœ๋ชจ๊ฐ€ ๊ทธ๋ฆฌ ํฌ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•™์Šต ๊ณผ์ •์„ ์‰ฝ๊ฒŒ ์‹คํ—˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ—ˆ๋ธŒ์—์„œ ๋ฐ์ดํ„ฐ ์…‹ ๋กœ๋”ฉ ํ—ˆ๋ธŒ(hub)์—๋Š” ๋ชจ๋ธ๋งŒ ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์–ธ์–ด๋กœ ๊ตฌ์ถ•๋œ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์…‹๋“ค๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์…‹์„ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ์„น์…˜์„ ์™„๋ฃŒํ•œ ํ›„์—๋Š” ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๊ณ  ์ฒ˜๋ฆฌํ•ด ๋ณด๊ธฐ๋ฅผ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค (์—ฌ๊ธฐ์—์„œ ์ผ๋ฐ˜ ๋ฌธ์„œ ์ฐธ์กฐ). ํ•˜์ง€๋งŒ ์ง€๊ธˆ์€ MRPC ๋ฐ์ดํ„ฐ ์…‹์— ์ง‘์ค‘ํ•ฉ์‹œ๋‹ค! ์ด ๋ฐ์ดํ„ฐ ์…‹์€ 10๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ๊ตฌ์„ฑ๋œ GLUE ๋ฒค์น˜๋งˆํฌ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. GLUE ๋ฒค์น˜๋งˆํฌ๋Š” 10๊ฐ€์ง€ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์„ ํ†ตํ•ด์„œ ๊ธฐ๊ณ„ํ•™์Šต ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•œ ํ•™์ˆ ์  ๋ฒค์น˜๋งˆํฌ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํ—ˆ๋ธŒ(hub)์—์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์บ์‹œ(cache) ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์‰ฌ์šด ๋ช…๋ น์–ด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด MRPC ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_dataset raw_datasets = load_dataset("glue", "mrpc") raw_datasets Downloading: 0%| | 0.00/7.78k [00:00<?, ?B/s] Downloading: 0%| | 0.00/4.47k [00:00<?, ?B/s] Downloading and preparing dataset glue/mrpc (download: 1.43 MiB, generated: 1.43 MiB, post-processed: Unknown size, total: 2.85 MiB) to /home/spasis/.cache/huggingface/datasets/glue/mrpc/1.0.0/dacbe3125aa31d7f70367a07a8a9e72a 5a0bfeb5fc42e75c9db75b96da6053ad... 0%| | 0/3 [00:00<?, ?it/s] Downloading: 0.00B [00:00, ?B/s] Downloading: 0.00B [00:00, ?B/s] Downloading: 0.00B [00:00, ?B/s] 0 examples [00:00, ? examples/s] 0 examples [00:00, ? examples/s] 0 examples [00:00, ? examples/s] Dataset glue downloaded and prepared to /home/spasis/.cache/huggingface/datasets/glue/mrpc/1.0.0/dacbe3125aa31d7f70367a07a8a9e72a 5a0bfeb5fc42e75c9db75b96da6053ad. Subsequent calls will reuse this data. 0%| | 0/3 [00:00<?, ?it/s] DatasetDict({ train: Dataset({ features: ['sentence1', 'sentence2', 'label', 'idx'], num_rows: 3668 }) validation: Dataset({ features: ['sentence1', 'sentence2', 'label', 'idx'], num_rows: 408 }) test: Dataset({ features: ['sentence1', 'sentence2', 'label', 'idx'], num_rows: 1725 }) }) ์œ„ ๊ฒฐ๊ณผ์—์„œ ๋ณด๋“ฏ์ด, ํ•™์Šต(training), ๊ฒ€์ฆ(validation) ๋ฐ ํ‰๊ฐ€(test) ์ง‘ํ•ฉ์ด ์ €์žฅ๋œ DatasetDict ๊ฐ์ฒด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค ๊ฐ๊ฐ์€ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ์—ด(columns)(sentence1, sentence2, label ๋ฐ idx)๊ณผ ํ–‰(row)์˜ ๊ฐœ์ˆ˜๋ฅผ ํฌํ•จํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ํ–‰(row)์˜ ๊ฐœ์ˆ˜๋Š” ๊ฐ ์ง‘ํ•ฉ์˜ ๋ฌธ์žฅ์Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ํ•™์Šต ์ง‘ํ•ฉ(training set)์—๋Š” 3,668๊ฐœ์˜ ๋ฌธ์žฅ ์Œ, ๊ฒ€์ฆ ์ง‘ํ•ฉ(validation set)์—๋Š” 408๊ฐœ, ํ‰๊ฐ€ ์ง‘ํ•ฉ(test set)์—๋Š” 1,725๊ฐœ์˜ ๋ฌธ์žฅ ์Œ์ด ์žˆ์Šต๋‹ˆ๋‹ค. load_dataset ๋ช…๋ น์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ~/.cache/huggingface/dataset์— ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์ž„์‹œ์ €์žฅ(์บ์‹œ, cache) ํ•ฉ๋‹ˆ๋‹ค. 2์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด, HF_HOME ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์—ฌ ์บ์‹œ ํด๋”๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ ๋”•์…”๋„ˆ๋ฆฌ(dictionary)์™€ ๊ฐ™์ด ํ‚ค๊ฐ’์œผ๋กœ raw_datasets ๊ฐœ์ฒด์˜ ๊ฐœ๋ณ„ ์ง‘ํ•ฉ(ํ•™์Šต, ๊ฒ€์ฆ, ํ‰๊ฐ€)์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: raw_train_dataset = raw_datasets["train"] raw_train_dataset[0] {'sentence1': 'Amrozi accused his brother , whom he called " the witness " , of deliberately distorting his evidence .', 'sentence2': 'Referring to him as only " the witness " , Amrozi accused his brother of deliberately distorting his evidence .', 'label': 1, 'idx': 0} ์œ„์˜ ์˜ˆ์—์„œ ๋ณด๋“ฏ์ด, ๋ ˆ์ด๋ธ”(label)์ด ์ด๋ฏธ ์ •์ˆ˜(integers)๋ผ์„œ ์ „์ฒ˜๋ฆฌ(preprocessing)๊ฐ€ ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ์ •์ˆ˜๊ฐ€ ์–ด๋–ค ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” raw_train_dataset์˜ features ์†์„ฑ์„ ์‚ดํŽด๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค: raw_train_dataset.features {'sentence1': Value(dtype='string', id=None), 'sentence2': Value(dtype='string', id=None), 'label': ClassLabel(num_classes=2, names=['not_equivalent', 'equivalent'], names_file=None, id=None), 'idx': Value(dtype='int32', id=None)} ์„ธ๋ถ€์ ์œผ๋กœ, ๋ ˆ์ด๋ธ”(label)์€ ClassLabel ํƒ€์ž…์ด๊ณ  ๋ ˆ์ด๋ธ” ์ด๋ฆ„์— ๋Œ€ํ•œ ์ •์ˆ˜ ๋งคํ•‘์€ names ํด๋”์— ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 0์€ not_equivalent๋ฅผ ์˜๋ฏธํ•˜๊ณ , 1์€ equivalent๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹ ์ „์ฒ˜๋ฆฌ ๋ฐ์ดํ„ฐ ์…‹ ์ „์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ๋Š” ์šฐ์„ ์ ์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ๋ชจ๋ธ์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ์ด๋Š” ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €์— ๋‹จ์ผ ๋ฌธ์žฅ ๋˜๋Š” ๋‹ค์ค‘ ๋ฌธ์žฅ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ ์Œ์˜ ๋ชจ๋“  ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ๊ณผ ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์„ ๊ฐ๊ฐ ์ง์ ‘ ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import AutoTokenizer checkpoint = "bert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(checkpoint) tokenized_sentences_1 = tokenizer(raw_datasets["train"]["sentence1"]) tokenized_sentences_2 = tokenizer(raw_datasets["train"]["sentence2"]) ํ•˜์ง€๋งŒ ๋‘ ๊ฐœ์˜ ์‹œํ€€์Šค๋ฅผ ๋ชจ๋ธ์— ๋ฐ”๋กœ ์ „๋‹ฌ(๊ฐ๊ฐ์˜ ๋ฌธ์žฅ์„ ๋ชจ๋ธ์— ๋ณ„๋„๋กœ ๋งค๊ฐœ๋ณ€ ์ˆ˜๋กœ ์ „๋‹ฌ) ํ•˜์—ฌ ๋‘ ๋ฌธ์žฅ์ด ์˜์—ญ์ธ์ง€ ์•„๋‹Œ์ง€์— ๋Œ€ํ•œ ์˜ˆ์ธก์„ ์–ป์„ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋‘ ์‹œํ€€์Šค๋ฅผ ์Œ(pair)์œผ๋กœ ์ฒ˜๋ฆฌ(๋‹จ์ผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฒ˜๋ฆฌ) ํ•˜๊ณ  ์ ์ ˆํ•œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ๋„ ํ† ํฌ ๋‚˜์ด์ €(tokenizer)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•œ ์Œ์˜ ์‹œํ€€์Šค๋ฅผ ๊ฐ€์ ธ์™€ BERT ๋ชจ๋ธ์ด ์š”๊ตฌํ•˜๋Š” ์ž…๋ ฅ ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: inputs = tokenizer("This is the first sentence.", "This is the second one.") inputs {'input_ids': [101, 2023, 2003, 1996, 2034, 6251, 1012, 102, 2023, 2003, 1996, 2117, 2028, 1012, 102], 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]} ์œ„ ์ฝ”๋“œ์—์„œ ๋งŒ์ผ ์ฒซ ๋ฒˆ์งธ ๋ฐ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋‹จ์ผ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๊ณ  ๋‹ค์ค‘ ๋ฌธ์ž์—ด์ด ๋‹ด๊ธด ํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ๋ผ๋ฉด ๊ฐ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด(๋ฌธ์žฅ)์˜ ์ˆœ์„œ๋Œ€๋กœ ํ•œ ์Œ์”ฉ ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ, ์ฒซ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด๊ณผ ๋‘ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์ด ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด ์Œ์œผ๋กœ ์ž…๋ ฅ๋˜๋Š” ๋ฐฉ์‹์ด์ฃ . 2์žฅ์—์„œ input_ids ๋ฐ attention_mask ํ‚ค๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ๋…ผ์˜ํ–ˆ์ง€๋งŒ, token_type_ids์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๋Š” ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ ๋ณด๋“ฏ์ด, token_type_ids๋Š” ์ „์ฒด ์ž…๋ ฅ(input_ids)์˜ ์–ด๋Š ๋ถ€๋ถ„์ด ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด๊ณ  ์–ด๋Š ๊ฒƒ์ด ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์ธ์ง€ ๋ชจ๋ธ์— ์•Œ๋ ค์ค๋‹ˆ๋‹ค. input_ids ๋‚ด๋ถ€์˜ ID๋“ค์„ ๋‹ค์‹œ ๋‹จ์–ด๋กœ ๋””์ฝ”๋”ฉ ํ•˜๋ฉด: tokenizer.convert_ids_to_tokens(inputs["input_ids"]) ['[CLS]', 'this', 'is', 'the', 'first', 'sentence', '.', '[SEP]', 'this', 'is', 'the', 'second', 'one', '.', '[SEP]'] ๋”ฐ๋ผ์„œ ๋ชจ๋ธ์€ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์œผ๋กœ ๊ตฌ์„ฑ๋˜๊ณ  ์ž…๋ ฅ์˜ ํ˜•ํƒœ๊ฐ€ "[CLS] ๋ฌธ์žฅ 1 [SEP] ๋ฌธ์žฅ 2 [SEP]"์™€ ๊ฐ™์ด ๋  ๊ฒƒ์œผ๋กœ ์ง์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ token_type_ids์™€ ์ •๋ ฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ['[CLS]', 'this', 'is', 'the', 'first', 'sentence', '.', '[SEP]', 'this', 'is', 'the', 'second', 'one', '.', '[SEP]'] [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] ์œ„์—์„œ ๋ณด๋Š” ๋ฐ”์™€ ๊ฐ™์ด, "[CLS] ๋ฌธ์žฅ 1 [SEP]"์— ํ•ด๋‹นํ•˜๋Š” ์ž…๋ ฅ ๋ถ€๋ถ„์€ token_type_id๊ฐ€ 0์ด๊ณ  "๋ฌธ์žฅ 2 [SEP]"์— ํ•ด๋‹นํ•˜๋Š” ๋‹ค๋ฅธ ๋ถ€๋ถ„์€ ๋ชจ๋‘ 1์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ฒดํฌํฌ์ธํŠธ(checkpoint)๋ฅผ ์„ ํƒํ•œ๋‹ค๋ฉด, ํ† ํฐํ™”๋œ ์ž…๋ ฅ(tokenized inputs)์— token_type_ids๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, DistilBERT ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” tokenizer๊ฐ€ token_type_ids๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์‚ฌ์ „ํ•™์Šต ๊ณผ์ •์—์„œ ์ด๋Ÿฌํ•œ ํ˜•ํƒœ์˜ ์ž…๋ ฅ<NAME>์œผ๋กœ ํ•™์Šต์„ ์ง„ํ–‰ํ–ˆ์„ ๊ฒฝ์šฐ์—๋งŒ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ, BERT๋Š” ํ† ํฐ ํƒ€์ž… IDs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์ „ ํ•™์Šต๋˜๋ฉฐ, 1์žฅ์—์„œ ์„ค๋ช…ํ•œ masked language modeling objectives ์™ธ์— ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก(next sentence prediction)์ด๋ผ๋Š” ์ถ”๊ฐ€ objectives๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์˜ ๋ชฉํ‘œ๋Š” ๋ฌธ์žฅ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋ชจ๋ธ๋ง ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต ๊ณผ์ •์—์„œ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก(next sentence prediction)์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ์— ๋ฌด์ž‘์œ„๋กœ ๋งˆ์Šคํ‚น ๋œ ํ† ํฐ(masked tokens)์ด ํฌํ•จ๋œ ๋ฌธ์žฅ ์Œ์ด ์ž…๋ ฅ๋˜๊ณ  ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์„ ๋”ฐ๋ฅด๋Š”์ง€๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ์š”๊ตฌ๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๊ณผ์ •์—์„œ ์ด ์ž‘์—…(next sentence prediction)์˜ ๋‚œ๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ, ์ž…๋ ฅ์˜ ์•ฝ 50% ์ •๋„๋Š” ๋‘ ๋ฌธ์žฅ์ด ์›๋ณธ ๋ฌธ์„œ์—์„œ ์—ฐ์†์ ์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์Œ ์ง‘ํ•ฉ์ด๋ฉฐ, ๋‚˜๋จธ์ง€ 50%๋Š” ๋ฌธ์žฅ ์Œ์„ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์„œ์—์„œ ์ถ”์ถœ๋œ ๋ฌธ์žฅ๋“ค๋กœ ๊ตฌ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ, ํ† ํฐํ™” ์™„๋ฃŒ๋œ ์ž…๋ ฅ์— token_type_ids๊ฐ€ ์žˆ๋Š”์ง€์— ๋Œ€ํ•ด ๊ฑฑ์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €์™€ ๋ชจ๋ธ ๋ชจ๋‘์— ๋™์ผํ•œ ์ฒดํฌํฌ์ธํŠธ(checkpoint)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•œ, ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋ชจ๋ธ์— ๋ฌด์—‡์„ ์ œ๊ณตํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ฌด๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ํ•œ ์Œ์˜ ๋ฌธ์žฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์•˜์œผ๋ฏ€๋กœ, ์ด๋ฅผ ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ํ† ํฐ ํ™”(tokenize) ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „ ์žฅ์—์„œ์ฒ˜๋Ÿผ, ์šฐ๋ฆฌ๋Š” ํ† ํฌ ๋‚˜์ด์ €์—๊ฒŒ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ๊ทธ๋‹ค์Œ ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ๋ฌธ์žฅ ์Œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ 2์žฅ์—์„œ ๋ณธ ํŒจ๋”ฉ(padding) ๋ฐ ์ ˆ๋‹จ(truncation) ์˜ต์…˜๊ณผ๋„ ํ˜ธํ™˜๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•™์Šต ๋ฐ์ดํ„ฐ ์…‹์„ ์ „์ฒ˜๋ฆฌํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: tokenized_dataset = tokenizer( raw_datasets["train"]["sentence1"], raw_datasets["train"]["sentence2"], padding=True, truncation=True, ) ์ด ๋ฐฉ๋ฒ•์€ ์ž˜ ์ž‘๋™ํ•˜์ง€๋งŒ, input_ids, attention_mask, token_type_ids ๋ฐ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ธด ๋‹ค์ฐจ์› ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ‚ค๋กœ ์ง€์ •๋œ tokenized_dataset์ด๋ผ๋Š” ๋ณ„๋„์˜ ํŒŒ์ด์ฌ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ๋ฐฉ๋ฒ•์€ ํ† ํฐํ™”ํ•˜๋Š” ๋™์•ˆ ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ์ €์žฅํ•  ์ถฉ๋ถ„ํ•œ ๊ณต๊ฐ„์˜ RAM์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ ์…‹๋“ค์€ ๋””์Šคํฌ์— ์ €์žฅ๋œ Apache Arrow ํŒŒ์ผ์ด๋ฏ€๋กœ, ์š”์ฒญํ•œ ์ƒ˜ํ”Œ๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ๋œ ์ƒํƒœ๋กœ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ dataset ๊ฐ์ฒด๋กœ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด Dataset.map() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ํ† ํฐํ™”(tokenization) ์™ธ์— ๋” ๋งŽ์€ ์ „์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์œ ์—ฐ์„ฑ์„ ๋ฐœํœ˜ํ•ฉ๋‹ˆ๋‹ค. map() ๋ฉ”์„œ๋“œ๋Š” ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ฐœ๋ณ„ ์š”์†Œ์— ํ•จ์ˆ˜(function)๋ฅผ ์ ์šฉํ•˜์—ฌ ์ž‘๋™ํ•˜๋ฏ€๋กœ ์ž…๋ ฅ์„ ํ† ํฐํ™”ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def tokenize_function(example): return tokenizer(example["sentence1"], example["sentence2"], truncation=True) ์ด ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ฐœ๋ณ„ ํ•ญ๋ชฉ์ด ๋‹ด๊ธด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์•„์„œ input_ids, attention_mask ๋ฐ token_type_ids ํ‚ค๊ฐ€ ์ง€์ •๋œ ์ƒˆ๋กœ์šด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „์— ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ํ† ํฌ ๋‚˜์ด์ €(tokenizer)๋Š” ๋ฌธ์žฅ ์Œ ๋ฆฌ์ŠคํŠธ์—์„œ ์ž‘๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— example ๋”•์…”๋„ˆ๋ฆฌ์— ์—ฌ๋Ÿฌ ์ƒ˜ํ”Œ(๊ฐ ํ‚ค๊ฐ€ ๋ฌธ์žฅ ๋ชฉ๋ก์ž„)์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ์—๋„ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด map() ํ˜ธ์ถœ์—์„œ batched=True ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด ํ† ํฐํ™” ์†๋„๊ฐ€ ํฌ๊ฒŒ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๋Š” Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Rust๋กœ ์ž‘์„ฑ๋œ ๋˜ ๋‹ค๋ฅธ ํ† ํฌ ๋‚˜์ด์ €์— ์˜ํ•ด ์ง€์›๋ฉ๋‹ˆ๋‹ค. ์ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋งค์šฐ ๋น ๋ฅผ ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•œ ๋ฒˆ์— ๋งŽ์€ ์ž…๋ ฅ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ์ผ๋‹จ ํ˜„์žฌ๋Š” ํ† ํฐํ™” ํ•จ์ˆ˜์—์„œ padding ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ƒ๋žตํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋“  ์ƒ˜ํ”Œ๋“ค์„ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ฑ„์šฐ๋Š” ๊ฒƒ(padding)์ด ํšจ์œจ์ ์ด์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฐฐ์น˜(batch) ํ˜•ํƒœ๋กœ ์‹คํ–‰ํ•  ๋•Œ ์ƒ˜ํ”Œ์„ ์ฑ„์šฐ๋Š” ๊ฒƒ(padding)์ด ํšจ๊ณผ๋ฅผ ๋ฐœํœ˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์—์„œ์˜ ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ ์•„๋‹ˆ๋ผ ํ•ด๋‹น ๋ฐฐ์น˜(batch) ๋‚ด์—์„œ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ฑ„์šฐ๊ธฐ๋งŒ(padding) ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ž…๋ ฅ์˜ ๊ธธ์ด๊ฐ€ ๋งค์šฐ ๊ฐ€๋ณ€์ ์ผ ๋•Œ ๋งŽ์€ ์‹œ๊ฐ„๊ณผ ์ฒ˜๋ฆฌ ๋Šฅ๋ ฅ์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๋‹ค์Œ์€ ํ•œ ๋ฒˆ์— ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์…‹์— ํ† ํฐํ™” ๊ธฐ๋Šฅ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. map ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์—์„œ batched=True๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ํ•จ์ˆ˜๊ฐ€ ๊ฐ ์š”์†Œ์— ๊ฐœ๋ณ„์ ์œผ๋กœ ์ ์šฉ๋˜์ง€ ์•Š๊ณ  ๋ฐ์ดํ„ฐ ์…‹์˜ ํ•˜๋ถ€ ์ง‘ํ•ฉ, ์ฆ‰ ๊ฐ ๋ฐฐ์น˜(batch) ๋‚ด์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋“ค์— ํ•œ๊บผ๋ฒˆ์— ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ๋” ๋น ๋ฅธ ์ „์ฒ˜๋ฆฌ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค: tokenized_datasets = raw_datasets.map(tokenize_function, batched=True) tokenized_datasets 0%| | 0/4 [00:00<?, ?ba/s] 0%| | 0/1 [00:00<?, ?ba/s] 0%| | 0/2 [00:00<?, ?ba/s] DatasetDict({ train: Dataset({ features: ['attention_mask', 'idx', 'input_ids', 'label', 'sentence1', 'sentence2', 'token_type_ids'], num_rows: 3668 }) validation: Dataset({ features: ['attention_mask', 'idx', 'input_ids', 'label', 'sentence1', 'sentence2', 'token_type_ids'], num_rows: 408 }) test: Dataset({ features: ['attention_mask', 'idx', 'input_ids', 'label', 'sentence1', 'sentence2', 'token_type_ids'], num_rows: 1725 }) }) Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ ์…‹(datasets)์— ์ƒˆ๋กœ์šด ํ•„๋“œ๋“ค์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•„๋“œ๋“ค์€ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜์—์„œ ๋ฐ˜ํ™˜๋œ ์‚ฌ์ „์˜ ๊ฐ ํ‚ค(input_ids, token_type_ids, attention_mask)์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. num_proc ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ map()์œผ๋กœ ์ „์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์ ์šฉํ•  ๋•Œ ๋‹ค์ค‘ ์ฒ˜๋ฆฌ(multi-processing)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ƒ˜ํ”Œ์„ ๋” ๋น ๋ฅด๊ฒŒ ํ† ํฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฏธ ๋‹ค์ค‘ ์Šค๋ ˆ๋“œ(multiple threads)๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ง€์›ํ•˜๋Š” "๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €(fast tokenizer)"๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ „์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ๋นจ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ tokenize_function์€ input_ids, attention_mask ๋ฐ token_type_ids ํ‚ค๊ฐ€ ์กด์žฌํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ์ด 3 ๊ฐœ์˜ ์ƒˆ๋กœ์šด ํ•„๋“œ๊ฐ€ ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ชจ๋“  ๋ถ„ํ• (ํ•™์Šต, ๊ฒ€์ฆ, ํ‰๊ฐ€)์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๊ฐ€ map()์„ ์ ์šฉํ•œ ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ธฐ์กด ํ‚ค๋“ค ์ฆ‰, idx, label ๋“ฑ์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ ๊ฒฝ์šฐ ๊ธฐ์กด ํ•„๋“œ(idx, label, sentence1, sentence2 ๋“ฑ)๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•ด์•ผ ํ•  ์ผ์€ ์ „์ฒด ์š”์†Œ๋“ค์„ ๋ฐฐ์น˜(batch)๋กœ ๋ถ„๋ฆฌํ•  ๋•Œ ๊ฐ€์žฅ ๊ธด ์š”์†Œ์˜ ๊ธธ์ด๋กœ ๋ชจ๋“  ์˜ˆ์ œ๋ฅผ ์ฑ„์šฐ๋Š”(padding) ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๋™์  ํŒจ๋”ฉ(dynamic padding)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋™์  ํŒจ๋”ฉ(Dynamic padding) ์ƒ˜ํ”Œ๋“ค์„ ํ•จ๊ป˜ ๋ชจ์•„์„œ ์ง€์ •๋œ ํฌ๊ธฐ์˜ ๋ฐฐ์น˜(batch)๋กœ ๊ตฌ์„ฑํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ฝœ๋ ˆ์ดํŠธ ํ•จ์ˆ˜(collate function)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” DataLoader๋ฅผ ๋นŒ๋“œ(build) ํ•  ๋•Œ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ ๋‹จ์ˆœํžˆ ์ƒ˜ํ”Œ๋“ค์„ PyTorch ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๊ฒฐํ•ฉํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋งŒ์ผ ๋Œ€์ƒ ์ƒ˜ํ”Œ๋“ค์ด ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ ํ˜น์€ ๋”•์…”๋„ˆ๋ฆฌ ๋ฉด ์žฌ๊ท€์ ์œผ๋กœ ์ด ์ž‘์—…์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ, ์ž…๋ ฅ๊ฐ’์ด ๋ชจ๋‘ ๋™์ผํ•œ ํฌ๊ธฐ(๊ธธ์ด)๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ž‘์—…์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๋Š” ์ผ๋ถ€๋Ÿฌ ํŒจ๋”ฉ(padding) ์ž‘์—…์„ ๋ฏธ๋ค„์™”๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์ด ์•„๋‹Œ ๊ฐœ๋ณ„ ๋ฐฐ์น˜(batch)์— ๋Œ€ํ•ด์„œ ๋ณ„๋„๋กœ ํŒจ๋”ฉ(padding)์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๊ณผ๋„ํ•˜๊ฒŒ ๊ธด ์ž…๋ ฅ์œผ๋กœ ์ธํ•œ ๊ณผ๋„ํ•œ ํŒจ๋”ฉ(padding) ์ž‘์—…์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•™์Šต ์†๋„๊ฐ€ ์ƒ๋‹นํžˆ ๋นจ๋ผ์ง€์ง€๋งŒ TPU์—์„œ ํ•™์Šตํ•˜๋Š” ๊ฒฝ์šฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. TPU๋Š” ์ถ”๊ฐ€์ ์ธ ํŒจ๋”ฉ(padding)์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋„ ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์ด ๊ณ ์ •๋œ ํ˜•ํƒœ๋ฅผ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด, ๋ฐฐ์น˜(batch)๋กœ ๋ถ„๋ฆฌํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ ์…‹์˜ ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•ํ•œ ์ˆ˜์˜ ํŒจ๋”ฉ(padding)์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ฝœ๋ ˆ์ดํŠธ ํ•จ์ˆ˜(collate function)๋ฅผ ์ •์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ๋„, Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” DataCollatorWithPadding์„ ํ†ตํ•ด ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์‚ฌ์šฉํ•˜๋ ค๋Š” ํŒจ๋”ฉ ํ† ํฐ(padding token)์ด ๋ฌด์—‡์ธ์ง€์™€ ๋ชจ๋ธ์ด ์ž…๋ ฅ์˜ ์™ผ์ชฝ ํ˜น์€ ์˜ค๋ฅธ์ฏ• ์ค‘ ์–ด๋Š ์ชฝ์— ํŒจ๋”ฉ(padding)์„ ์ˆ˜ํ–‰ํ• ์ง€๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ์ด ์ž…๋ ฅ ํ•˜๋‚˜๋ฉด ๋ชจ๋“  ๊ฒƒ์ด ํ•ด๊ฒฐ๋ฉ๋‹ˆ๋‹ค: from transformers import DataCollatorWithPadding data_collator = DataCollatorWithPadding(tokenizer=tokenizer) ์ด ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ํ•™์Šต ์ง‘ํ•ฉ์—์„œ ๋ฐฐ์น˜(batch)๋กœ ๋ฌถ์„ ๋ช‡ ๊ฐœ์˜ ์ƒ˜ํ”Œ๋“ค์„ ๊ฐ€์ ธ์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ•„์š”ํ•˜์ง€๋„ ์•Š์„๋ฟ๋”๋Ÿฌ ์‹ฌ์ง€์–ด ๋ฌธ์ž์—ด๊นŒ์ง€๋„ ํฌํ•จํ•˜๋Š” idx, sentence1 ๋ฐ sentence2 ์—ด์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค(๋ฌธ์ž์—ด๋กœ๋Š” ํ…์„œ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค). ์•„๋ž˜์—์„œ ๋ฐฐ์น˜(batch) ๋‚ด์˜ ๊ฐ ์š”์†Œ๋“ค์˜ ๊ธธ์ด๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”: samples = tokenized_datasets["train"][:8] samples = {k: v for k, v in samples.items() if k not in ["idx", "sentence1", "sentence2"]} [len(x) for x in samples["input_ids"]] [50, 59, 47, 67, 59, 50, 62, 32] ๋‹น์—ฐํžˆ, 32์—์„œ 67๊นŒ์ง€ ๋‹ค์–‘ํ•œ ๊ธธ์ด์˜ ์ƒ˜ํ”Œ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™์  ํŒจ๋”ฉ(dynamic padding)์€ ์ด ๋ฐฐ์น˜(batch) ๋‚ด์˜ ๋ชจ๋“  ์ƒ˜ํ”Œ๋“ค์ด ๋ฐฐ์น˜ ๋‚ด๋ถ€์—์„œ ์ตœ๋Œ€ ๊ธธ์ด์ธ 67 ๊ธธ์ด๋กœ ํŒจ๋”ฉ(padding) ๋˜์–ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋™์  ํŒจ๋”ฉ(dynamic padding)์ด ์—†์œผ๋ฉด ๋ชจ๋“  ์ƒ˜ํ”Œ๋“ค์€ ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์˜ ์ตœ๋Œ€ ๊ธธ์ด ๋˜๋Š” ๋ชจ๋ธ์ด ํ—ˆ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ฑ„์›Œ์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. data_collator๊ฐ€ ๋™์ ์œผ๋กœ ๋ฐฐ์น˜(batch)๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ํŒจ๋”ฉ(padding) ํ•˜๋Š”์ง€ ๋‹ค์‹œ ํ™•์ธํ•ฉ์‹œ๋‹ค: batch = data_collator(samples) {k: v.shape for k, v in batch.items()} {'attention_mask': torch.Size([8, 67]), 'input_ids': torch.Size([8, 67]), 'token_type_ids': torch.Size([8, 67]), 'labels': torch.Size([8])} ์ข‹์Šต๋‹ˆ๋‹ค! ์›์‹œ ํ…์ŠคํŠธ์—์„œ ๋ชจ๋ธ์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์น˜(batch) ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์œผ๋ฏ€๋กœ, ์ด์ œ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! 2. Trainer API๋ฅผ ์ด์šฉํ•œ ๋ชจ๋ธ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) Transformers๋Š” Trainer ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜์—ฌ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ(pretrained models)์„ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค. ์ด์ „ ์„น์…˜์—์„œ ์„ค๋ช…ํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ ์ž‘์—…์„ ์™„๋ฃŒํ–ˆ๋‹ค๋ฉด, Trainer๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ๋ช‡ ๊ฐ€์ง€ ๋‹จ๊ณ„๋งŒ ๊ฑฐ์น˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์–ด๋ ค์šด ๋ถ€๋ถ„์€ Trainer.train()์„ ์‹คํ–‰ํ•  ํ™˜๊ฒฝ์„ ์ค€๋น„ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ž‘์—… ์ž์ฒด๊ฐ€ CPU์—์„œ ๋งค์šฐ ๋Š๋ฆฌ๊ฒŒ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. GPU๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, Google Colab์—์„œ ๋ฌด๋ฃŒ GPU ๋˜๋Š” TPU์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ ์˜ˆ์ œ์—์„œ๋Š” ์ด์ „ ์„น์…˜์˜ ์˜ˆ์ œ๋ฅผ ๋ชจ๋‘ ์‹คํ–‰ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋Š” ์ด๋ฒˆ ์„น์…˜์˜ ์˜ˆ์ œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์‚ฌํ•ญ์„ ๊ฐ„๋žตํ•˜๊ฒŒ ์š”์•ฝํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค: from datasets import load_dataset from transformers import AutoTokenizer, DataCollatorWithPadding raw_datasets = load_dataset("glue", "mrpc") checkpoint = "bert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(checkpoint) def tokenize_function(example): return tokenizer(example["sentence1"], example["sentence2"], truncation=True) tokenized_datasets = raw_datasets.map(tokenize_function, batched=True) data_collator = DataCollatorWithPadding(tokenizer=tokenizer) ํ•™์Šต (Training) Trainer๋ฅผ ์ •์˜ํ•˜๊ธฐ ์ „์— ๋จผ์ € ์ˆ˜ํ–‰ํ•  ๋‹จ๊ณ„๋Š” Trainer๊ฐ€ ํ•™์Šต ๋ฐ ํ‰๊ฐ€์— ์‚ฌ์šฉํ•  ๋ชจ๋“  ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ(hyperparameters)๋ฅผ ํฌํ•จํ•˜๋Š” TrainingArguments ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•ด์•ผ ํ•  ์œ ์ผํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•™์Šต๋œ ๋ชจ๋ธ์ด ์ €์žฅ๋  ๋””๋ ‰ํ„ฐ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€๋Š” ๋ชจ๋‘ ๊ธฐ๋ณธ๊ฐ’(default values)์„ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning)์—๋Š” ์ด ์ •๋„๋ฉด ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. from transformers import TrainingArguments training_args = TrainingArguments("test-trainer") ๋‘ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ๋ชจ๋ธ์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ „ ์žฅ์—์„œ์™€ ๊ฐ™์ด, ๋‘ ๊ฐœ์˜ ๋ ˆ์ด๋ธ”์ด ์žˆ๋Š” AutoModelForSequenceClassification ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. from transformers import AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) 2์žฅ๊ณผ ๋‹ฌ๋ฆฌ, ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•œ ํ›„ ๊ฒฝ๊ณ (warnings)๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” BERT๊ฐ€ ๋ฌธ์žฅ ์Œ ๋ถ„๋ฅ˜์— ๋Œ€ํ•ด ์‚ฌ์ „ ํ•™์Šต๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ํ—ค๋“œ(model head)๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ์‹œํ€€์Šค ๋ถ„๋ฅ˜์— ์ ํ•ฉํ•œ ์ƒˆ๋กœ์šด ํ—ค๋“œ๋ฅผ ๋Œ€์‹  ์ถ”๊ฐ€ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ฒฝ๊ณ ์˜ ๋‚ด์šฉ์€ ์ผ๋ถ€ ๊ฐ€์ค‘์น˜(weights)๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์•˜์œผ๋ฉฐ(์ œ๊ฑฐ๋œ ์‚ฌ์ „ ํ•™์Šต ํ—ค๋“œ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ€์ค‘์น˜) ์ผ๋ถ€ ๊ฐ€์ค‘์น˜๊ฐ€ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋˜์—ˆ์Œ์„(์ƒˆ๋กœ์šด ํ—ค๋“œ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜) ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฌธ์žฅ ์Œ ๋ถ„๋ฅ˜์— ๋Œ€ํ•œ ๋ฏธ์„ธ ์กฐ์ •์„ ํ•ด๋„ ์ข‹๋‹ค๊ณ  ์„ค๋ช…ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ(model), training_args, ํ•™์Šต ์ง‘ํ•ฉ ๋ฐ ๊ฒ€์ฆ ์ง‘ํ•ฉ, data_collator ๋ฐ ํ† ํฌ ๋‚˜์ด์ € ๋“ฑ, ์ง€๊ธˆ๊นŒ์ง€ ๊ตฌ์„ฑ๋œ ๋ชจ๋“  ๊ฐœ์ฒด๋ฅผ ์ „๋‹ฌํ•˜์—ฌ Trainer๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import Trainer trainer = Trainer( model, training_args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, tokenizer=tokenizer, ) ์œ„์—์„œ ๋ณด๋“ฏ์ด, ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ „๋‹ฌํ•  ๋•Œ Trainer๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ data_collator๋Š” ์ด์ „์— ์ •์˜๋œ DataCollatorWithPadding์ด ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ํ˜ธ์ถœ์—์„œ data_collator=data_collator ํ–‰์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•˜๋ ค๋ฉด Trainer์˜ train() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: trainer.train() ๋ฏธ์„ธ ์กฐ์ •์ด ์‹œ์ž‘๋˜๊ณ (GPU์—์„œ ๋ช‡ ๋ถ„ ์ •๋„ ์†Œ์š”๋จ) 500๋‹จ๊ณ„๋งˆ๋‹ค ํ•™์Šต ์†์‹ค(training loss)์ด ๋ณด๊ณ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ์ข‹์€์ง€ ํ˜น์€ ๋‚˜์œ์ง€๋Š” ์•Œ๋ ค์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ํ•™์Šต ๊ณผ์ •์—์„œ ํ‰๊ฐ€๊ฐ€ ์ˆ˜ํ–‰๋˜๋„๋ก Trainer์—๊ฒŒ evaluation_strategy ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ "steps"(๋งค eval_steps๋งˆ๋‹ค ํ‰๊ฐ€)๋‚˜ "epoch"(๊ฐ epoch ๋งˆ์ง€๋ง‰์— ํ‰๊ฐ€) ๋“ฑ์œผ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ฐฉ๋ฒ• ํ˜น์€ ํ‰๊ฐ€ ์ฒ™๋„๋ฅผ ์ •์˜ํ•œ compute_metrics() ํ•จ์ˆ˜๋ฅผ Trainer์— ์ง€์ •ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ฐฉ๋ฒ• ์ง€์ •์ด ์•ˆ๋œ ์ƒํƒœ์—์„œ๋Š” ํ‰๊ฐ€ ๊ณผ์ •์—์„œ ์†์‹ค(loss)์„ ์ถœ๋ ฅํ–ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ง๊ด€์ ์ธ ๊ฐ’์€ ์•„๋‹ˆ์ง€์š”. ํ‰๊ฐ€ (Evaluation) ์œ ์šฉํ•œ compute_metrics() ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์ด๋ฅผ ํ•™์Šตํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” EvalPrediction ๊ฐ์ฒด(predictions ํ•„๋“œ์™€ label_ids ํ•„๋“œ๊ฐ€ ํฌํ•จ๋œ ๋„ค์ž„๋“œํŠœํ”Œ(named tuple))๋ฅผ ํ•„์š”๋กœ ํ•˜๋ฉฐ ๋ฌธ์ž์—ด๊ณผ ์‹ค์ˆซ๊ฐ’(floats)์„ ๋งคํ•‘ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ฌธ์ž์—ด์€ ๋ฐ˜ํ™˜๋œ ๋ฉ”ํŠธ๋ฆญ(metrics)์˜ ์ด๋ฆ„์ด๊ณ  ์‹ค์ˆซ๊ฐ’(floats)์€ ํ•ด๋‹น ๋ฉ”ํŠธ๋ฆญ์— ๊ธฐ๋ฐ˜ํ•œ ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ’์ž…๋‹ˆ๋‹ค. ์šฐ์„  ๋ชจ๋ธ์—์„œ ์˜ˆ์ธก์˜ ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด Trainer.predict() ๋ช…๋ น์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: predictions = trainer.predict(tokenized_datasets["validation"]) print(predictions.predictions.shape, predictions.label_ids.shape) predict() ๋ฉ”์„œ๋“œ์˜ ์ถœ๋ ฅ์€ 3๊ฐœ์˜ ํ•„๋“œ(predictions, label_ids ๋ฐ metrics)๊ฐ€ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ๋„ค์ž„๋“œํŠœํ”Œ(named tuple)์ž…๋‹ˆ๋‹ค. metrics ํ•„๋“œ์—๋Š” ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ ์…‹์˜ ์†์‹ค(loss)๊ณผ ์‹œ๊ฐ„ ๋ฉ”ํŠธ๋ฆญ(time metrics) ๊ฐ’๋งŒ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐ„ ๋ฉ”ํŠธ๋ฆญ(time metrics)์€ ์˜ˆ์ธก์— ๊ฑธ๋ฆฐ ์ „์ฒด ๋ฐ ํ‰๊ท  ์‹œ๊ฐ„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. compute_metrics() ํ•จ์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  Trainer์— ์ „๋‹ฌํ•˜๋ฉด ํ•ด๋‹น ํ•„๋“œ์—๋Š” compute_metrics()์—์„œ ๋ฐ˜ํ™˜ํ•œ ๋ฉ”ํŠธ๋ฆญ(metrics)๋„ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๋ณด์‹œ๋‹ค์‹œํ”ผ predictions์€ ๋ชจ์–‘์ด 408 x 2์ธ 2์ฐจ์› ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. 408์€ ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ธก์— ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ์…‹์˜ ์š”์†Œ ๊ฐœ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์šฐ๋ฆฌ๊ฐ€ predict()์— ์ „๋‹ฌํ•œ ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•œ ๋กœ์ง“(logit)๊ฐ’๋“ค์ž…๋‹ˆ๋‹ค. ์ด์ „ ์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ๋ชจ๋“  Transformer ๋ชจ๋ธ์€ ๋กœ์ง“(logit)๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋กœ์ง“(logit)๊ฐ’๋“ค์„ ๋ ˆ์ด๋ธ”๊ณผ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์ธก ๊ฒฐ๊ณผ๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด ๋‘ ๋ฒˆ์งธ ์ถ•(second axis)์— ์กด์žฌํ•˜๋Š” ํ•ญ๋ชฉ์—์„œ ์ตœ๋Œ“๊ฐ’์ด ์žˆ๋Š” ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค: import numpy as np preds = np.argmax(predictions.predictions, axis=-1) ์ด์ œ preds๋ฅผ ๋ ˆ์ด๋ธ”(labels)๊ณผ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. compute_metric() ํ•จ์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”ํŠธ๋ฆญ(metrics)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. load_metric() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์†์‰ฝ๊ฒŒ MRPC ๋ฐ์ดํ„ฐ ์…‹๊ณผ ๊ด€๋ จ๋œ ๋ฉ”ํŠธ๋ฆญ(metrics)์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ๋“œ๋œ ๊ฐ์ฒด์—๋Š” ๋ฉ”ํŠธ๋ฆญ(metrics) ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” compute() ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_metric metric = load_metric("glue", "mrpc") metric.compute(predictions=preds, references=predictions.label_ids) ๋ชจ๋ธ ํ—ค๋“œ๋ฅผ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋ฉด ๊ณ„์‚ฐ๋œ ๋ฉ”ํŠธ๋ฆญ์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ •ํ™•ํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ชจ๋ธ์ด ๊ฒ€์ฆ ์ง‘ํ•ฉ์—์„œ 86.76%์˜ ์ •ํ™•๋„(accuracy)์™€ 90.69์˜ F1 ์ ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์—ฌ๋Ÿฌ๋ถ„์˜ ๊ฒฐ๊ด๊ฐ’๊ณผ๋Š” ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.). ์ด๋Š” GLUE ๋ฒค์น˜๋งˆํฌ์˜ MRPC ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•œ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฉ”ํŠธ๋ฆญ(metrics)์ž…๋‹ˆ๋‹ค. BERT ๋…ผ๋ฌธ์˜ ํ…Œ์ด๋ธ”์€ ๊ธฐ๋ณธ ๋ชจ๋ธ์— ๋Œ€ํ•ด F1 ์ ์ˆ˜ 88.9๋ฅผ ๋ณด๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋…ผ๋ฌธ์—์„œ ์‚ฌ์šฉ๋œ ๋ชจ๋ธ์€ ์†Œ๋ฌธ์ž ๋ชจ๋ธ(uncased model)์ด์—ˆ๊ณ  ์—ฌ๊ธฐ์„œ๋Š” ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ๋ชจ๋ธ(cased model)์„ ํ™œ์šฉํ–ˆ์œผ๋ฏ€๋กœ ์„ฑ๋Šฅ ์ฐจ์ด๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•œ ๋ชจ๋“  ๊ฒƒ์„ ํ•จ๊ป˜ ์ข…ํ•ฉํ•˜๋ฉด compute_metrics() ํ•จ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: def compute_metrics(eval_preds): metric = load_metric("glue", "mrpc") logits, labels = eval_preds predictions = np.argmax(logits, axis=-1) return metric.compute(predictions=predictions, references=labels) ๊ฐ ์—ํฌํฌ(epoch)๊ฐ€ ๋๋‚  ๋•Œ ๋ฉ”ํŠธ๋ฆญ(metrics)์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ, compute_metrics() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ Trainer๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•„๋ž˜ ์ฝ”๋“œ์—์„œ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค: training_args = TrainingArguments("test-trainer", evaluation_strategy="epoch") model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) trainer = Trainer( model, training_args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, tokenizer=tokenizer, compute_metrics=compute_metrics, ) evaluation_strategy ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ "epoch"์œผ๋กœ ์„ค์ •๋˜๊ณ  ์ƒˆ๋กœ์šด TrainingArguments์™€ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ด๋ฏธ ์•ž์—์„œ ํ•™์Šต๋œ(fine-tuned) ๋ชจ๋ธ์˜ ํ•™์Šต์„ ๊ณ„์†ํ•ด์„œ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ํ•™์Šต ์‹คํ–‰์„ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค: trainer.train() ์ด๋ฒˆ์—๋Š” ํ•™์Šต ์†์‹ค(training loss) ์™ธ์— ๊ฐ epoch๊ฐ€ ๋๋‚  ๋•Œ ๊ฒ€์ฆ ์†์‹ค(validation loss) ๋ฐ ๋ฉ”ํŠธ๋ฆญ(metrics)์„ ๋ณด๊ณ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, ์ •ํ™•ํ•œ ์ •ํ™•๋„(accuracy)/F1 ์ ์ˆ˜๋Š” ๋ชจ๋ธ์˜ ๋ฌด์ž‘์œ„ ํ—ค๋“œ ์ดˆ๊ธฐํ™”๋กœ ์ธํ•ด ๊ฐ๊ฐ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ์•ฝ๊ฐ„ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์ง€๋งŒ ์œ ์‚ฌํ•œ ๋ฒ”์œ„ ๋‚ด์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Trainer๋Š” ๋‹ค์ค‘ GPU ๋˜๋Š” TPU์—์„œ ์ฆ‰์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ•™์Šต(mixed-precision training, ํ•™์Šต ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ fp16 = True ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ)๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์˜ต์…˜์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. 10์žฅ์—์„œ ์ง€์›ํ•˜๋Š” ๋ชจ๋“  ์˜ต์…˜๋“ค์„ ์‚ดํŽด๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์œผ๋กœ Trainer API๋ฅผ ์‚ฌ์šฉํ•œ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋งˆ์นฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ž์ฃผ ์ˆ˜ํ–‰๋˜๋Š” NLP ์ž‘์—…(tasks)์— ๋Œ€ํ•ด ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜ˆ๋Š” 7์žฅ์—์„œ ์ œ๊ณต๋˜์ง€๋งŒ, ๋‹ค์Œ ์„น์…˜์—์„œ ์ด API๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  PyTorch์—์„œ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3. ์ „์ฒด ํ•™์Šต (Full Training) ์ด์ œ Trainer ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ด์ „ ์„น์…˜์—์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, ์„น์…˜ 2์—์„œ ์ด๋ฏธ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์™„๋ฃŒํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์„น์…˜์„ ๊ณต๋ถ€ํ•  ๋•Œ ํ•„์š”ํ•œ ๋ชจ๋“  ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: from datasets import load_dataset from transformers import AutoTokenizer, DataCollatorWithPadding raw_datasets = load_dataset("glue", "mrpc") checkpoint = "bert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(checkpoint) def tokenize_function(example): return tokenizer(example["sentence1"], example["sentence2"], truncation=True) tokenized_datasets = raw_datasets.map(tokenize_function, batched=True) data_collator = DataCollatorWithPadding(tokenizer=tokenizer) ํ•™์Šต์„ ์œ„ํ•œ ์ค€๋น„ ์‹ค์ œ๋กœ ํ•™์Šต ๋ฃจํ”„(training loop)๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์ „์— ๋ช‡ ๊ฐ€์ง€ ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ๋ฐฐ์น˜(batch)๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  dataloaders์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด dataloaders๋ฅผ ์ •์˜ํ•˜๊ธฐ ์ „์— Trainer๊ฐ€ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•œ ๋ช‡ ๊ฐ€์ง€ ์ž‘์—…์„ ์ง์ ‘ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด tokenized_datasets์— ์•ฝ๊ฐ„์˜ ํ›„์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: ๋ชจ๋ธ์ด ํ•„์š”๋กœ ํ•˜์ง€ ์•Š๋Š” ๊ฐ’์ด ์ €์žฅ๋œ ์—ด(columns)์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. (sentence1, sentence2 ๋“ฑ) ์—ด ๋ ˆ์ด๋ธ”(column label)์˜ ์ด๋ฆ„์„ labels๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด labels๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ ๋Œ€์‹  PyTorch ํ…์„œ(tensors)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก datasets์˜<NAME>์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. tokenized_datasets์—๋Š” ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ์œ„ํ•œ ๋ณ„๋„์˜ ๋ฉ”์„œ๋“œ๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค: tokenized_datasets = tokenized_datasets.remove_columns(["sentence1", "sentence2", "idx"]) tokenized_datasets = tokenized_datasets.rename_column("label", "labels") tokenized_datasets.set_format("torch") tokenized_datasets["train"].column_names ์œ„์—์„œ ๋ณด๋“ฏ์ด ๊ฒฐ๊ณผ์ ์œผ๋กœ tokenized_datasets์—๋Š” ๋ชจ๋ธ์ด ํ—ˆ์šฉํ•˜๋Š” columns๋งŒ ์กด์žฌํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด ์ž‘์—…์ด ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฏ€๋กœ dataloader๋ฅผ ์‰ฝ๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from torch.utils.data import DataLoader train_dataloader = DataLoader( tokenized_datasets["train"], shuffle=True, batch_size=8, collate_fn=data_collator, ) eval_dataloader = DataLoader( tokenized_datasets["validation"], batch_size=8, collate_fn=data_collator, ) ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ์˜ค๋ฅ˜๊ฐ€ ์—†๋Š”์ง€ ๋น ๋ฅด๊ฒŒ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฐ์น˜(batch)๋ฅผ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: for batch in train_dataloader: break {k: v.shape for k, v in batch.items()} ์‹ค์ œ ๋ฐ์ดํ„ฐ ํ˜•ํƒœ(shapes)๊ฐ€ ์‚ด์ง ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋Š” ํ•™์Šต dataloader์— ๋Œ€ํ•ด shuffle=True๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋ฐฐ์น˜(batch) ๋‚ด์—์„œ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉ(padding) ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๊ฐ€ ์™„์ „ํžˆ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ํ•™์Šต ์‹ค๋ฌด์ž(ML practitioners)๋“ค์—๊ฒŒ๋Š” ๋งŒ์กฑ์Šค๋Ÿฝ๊ธฐ๋„ ํ•˜๊ฒ ์ง€๋งŒ ์ผ๋ถ€ ๋ช…ํ™•ํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„๋„ ์žˆ์„ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ๋กœ ๋Œ์•„๊ฐ€ ๋ด…์‹œ๋‹ค. ์ด์ „ ์„น์…˜์—์„œ ์ˆ˜ํ–‰ํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”(instantiate) ํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) ํ•™์Šต ๊ณผ์ •์—์„œ ๋ชจ๋“  ๊ฒƒ๋“ค์ด ์›ํ™œํ•˜๊ฒŒ ์ง„ํ–‰๋  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฐ์น˜(batch)๋ฅผ ์ด ๋ชจ๋ธ์— ํ•œ๋ฒˆ ์ „๋‹ฌํ•ด ๋ด…์‹œ๋‹ค: outputs = model(**batch) print(outputs.loss, outputs.logits.shape) ๋ชจ๋“  Transformers ๋ชจ๋ธ์€ ๋งค๊ฐœ๋ณ€์ˆ˜์— labels์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ์†์‹ค(loss)๊ณผ ํ•จ๊ป˜ logit ๊ฐ’(batch ๋‚ด ๊ฐ ์ž…๋ ฅ์— ๋Œ€ํ•ด logit ๊ฐ’์ด 2๊ฐœ์ด๋ฏ€๋กœ ํฌ๊ธฐ๊ฐ€ 8 x 2์ธ ํ…์„œ)๋„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๋ฃจํ”„(training loop)๋ฅผ ์ž‘์„ฑํ•  ์ค€๋น„๊ฐ€ ๊ฑฐ์˜ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฐ๋ฐ ์•„์ง ์ตœ์ ํ™” ํ•จ์ˆ˜(optimizer) ๋ฐ ํ•™์Šต๋ฅ  ์Šค์ผ€์ค„๋Ÿฌ(learning rate scheduler) ์ง€์ • ์ž‘์—…์ด ๋‚จ์•„ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์•ž์—์„œ ๋ฐฐ์šด Trainer์˜ ๊ธฐ๋ณธ ์„ค์ •์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Trainer๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์ตœ์ ํ™” ํ•จ์ˆ˜๋Š” AdamW์ด๋ฉฐ ์ด๋Š” Adam๊ณผ ๊ฑฐ์˜ ๋™์ผํ•˜์ง€๋งŒ weight decay regularization์„ ์ ์šฉํ–ˆ๋‹ค๋Š” ์‚ฌ์‹ค์— ์ฐจ์ด๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค: from transformers import AdamW optimizer = AdamW(model.parameters(), lr=5e-5) ๋งˆ์ง€๋ง‰์œผ๋กœ, Trainer์—์„œ ๋””ํดํŠธ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ•™์Šต๋ฅ  ์Šค์ผ€์ค„๋Ÿฌ(learning rate scheduler)๋Š” ์ตœ๋Œ“๊ฐ’(5e-5)์—์„œ 0๊นŒ์ง€ ์„ ํ˜• ๊ฐ์‡ (linear decay) ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ •์˜ํ•˜๋ ค๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰ํ•  ํ•™์Šต ๋‹จ๊ณ„์˜ ํšŸ์ˆ˜๋ฅผ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์‹คํ–‰ํ•˜๋ ค๋Š” ์—ํฌํฌ(epochs) ์ˆ˜์— ํ•™์Šต ๋ฐฐ์น˜(batch)์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณฑํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•™์Šต ๋ฐฐ์น˜์˜ ๊ฐœ์ˆ˜๋Š” ํ•™์Šต dataloader์˜ ๊ธธ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. Trainer๋Š” ๋””ํดํŠธ๋กœ 3๊ฐœ์˜ ์—ํฌํฌ(epochs)๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋‹ค์Œ์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค: from transformers import get_scheduler num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) print(num_training_steps) ํ•™์Šต ๋ฃจํ”„ (Training Loop) ๊ฐ€์šฉ GPU๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. CPU๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•™์Šต์€ ๋ช‡ ๋ถ„ ์ •๋„๊ฐ€ ์•„๋‹ˆ๋ผ ๋ช‡ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด, ๋ชจ๋ธ๊ณผ ๋ฐฐ์น˜(batch)๋ฅผ ์ ์žฌํ•  ์žฅ์น˜(device)๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค: import torch device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") model.to(device) device ์ด์ œ ํ•™์Šตํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ํ•™์Šต์ด ์–ธ์ œ ๋๋‚ ์ง€ ์ •๋ณด๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด tqdm ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ๋‹จ๊ณ„(training steps)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ง„ํ–‰ ํ‘œ์‹œ์ค„(progress bar)์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค: from tqdm.auto import tqdm progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: batch = {k: v.to(device) for k, v in batch.items()} outputs = model(**batch) loss = outputs.loss loss.backward() optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) ํ•™์Šต ๋ฃจํ”„(training loop)์˜ ์ฃผ์š” ๋ถ€๋ถ„์ด ๋ณธ ๊ฐ•์ขŒ์˜ ์†Œ๊ฐœ(Introduction) ์žฅ์—์„œ ์†Œ๊ฐœํ•œ ๋‚ด์šฉ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ๋Š” ์–ด๋– ํ•œ ์ค‘๊ฐ„ ์ถœ๋ ฅ๋„ ์—†์œผ๋ฏ€๋กœ, ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด๋‚˜ ์†์‹ค ๋“ฑ์— ๋Œ€ํ•ด ์•„๋ฌด๊ฒƒ๋„ ์•Œ๋ ค์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํ‰๊ฐ€ ๋ฃจํ”„(evaluation loop)๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ฃจํ”„ (Evaluation Loop) ์ด์ „์— ์ˆ˜ํ–‰ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ, Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ‰๊ฐ€ ๋ฉ”ํŠธ๋ฆญ(metrics)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ metric.compute() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ดํŽด๋ณด์•˜์ง€๋งŒ metric.add_batch() ๋ฉ”์„œ๋“œ๋กœ ํ‰๊ฐ€ ๋ฃจํ”„(evaluation loop)๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ๋ฐฐ์น˜(batch) ๋ณ„ ํ‰๊ฐ€ ๋ฉ”ํŠธ๋ฆญ(metrics) ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ˆ„์ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฐฐ์น˜(batch)๋ฅผ ๋ˆ„์ ํ•˜๊ณ  ๋‚˜๋ฉด metric.compute()๋กœ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ฃจํ”„์—์„œ ์ด ๋ชจ๋“  ๊ฒƒ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: from datasets import load_metric metric = load_metric("glue", "mrpc") model.eval() for batch in eval_dataloader: batch = {k: v.to(device) for k, v in batch.items()} with torch.no_grad(): outputs = model(**batch) logits = outputs.logits predictions = torch.argmax(logits, dim=-1) metric.add_batch(predictions=predictions, references=batch["labels"]) metric.compute() ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, ๋ชจ๋ธ ํ—ค๋“œ ์ดˆ๊ธฐํ™”(model head initialization) ๋ฐ ๋ฐ์ดํ„ฐ ์…”ํ”Œ๋ง์˜ ๋ฌด์ž‘์œ„์„ฑ(randomness) ์ธํ•ด ๊ฒฐ๊ณผ๊ฐ€ ์•ฝ๊ฐ„ ์ฐจ์ด๊ฐ€ ๋‚˜๊ธด ํ•˜์ง€๋งŒ ๊ทธ ์ฐจ์ด๊ฐ€ ํฌ๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค. Accelerate ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ ํ•™์Šต ๋ฃจํ”„ ๊ฐ€์†ํ™” ์•ž์—์„œ ์ •์˜ํ•œ ํ•™์Šต ๋ฃจํ”„(training loop)๋Š” ๋‹จ์ผ CPU ๋˜๋Š” ๋‹จ์ผ GPU์—์„œ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Accelerate ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€ ์„ค์ •๋งŒ ํ•˜๋ฉด ์—ฌ๋Ÿฌ GPU ๋˜๋Š” TPU์—์„œ ๋ถ„์‚ฐ ํ•™์Šต(distributed training)์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ๋ฐ ๊ฒ€์ฆ dataloader๋ฅผ ์ƒ์„ฑํ•œ ํ›„, ํ•™์Šต ๋ฃจํ”„(training loop)์˜ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") model.to(device) num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: batch = {k: v.to(device) for k, v in batch.items()} outputs = model(**batch) loss = outputs.loss loss.backward() optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) ์œ„ ํ•™์Šต ๋ฃจํ”„๋ฅผ ์ˆ˜์ •ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: from accelerate import Accelerator from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler accelerator = Accelerator() model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( train_dataloader, eval_dataloader, model, optimizer ) num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) ์ถ”๊ฐ€ํ•  ์ฒซ ๋ฒˆ์งธ ๋ผ์ธ์€ import ๋ผ์ธ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ถ”๊ฐ€ ๋ผ์ธ์—์„œ ์‹œ์Šคํ…œ ํ™˜๊ฒฝ ์„ค์ •์„ ํŒŒ์•…ํ•˜๊ณ  ์ ์ ˆํ•œ ๋ถ„์‚ฐ ์„ค์ •์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” Accelerator ๊ฐœ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•ฉ๋‹ˆ๋‹ค. Accelerate๊ฐ€ ์žฅ์น˜ ๋ฐฐ์น˜(device placement)๋ฅผ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ ์žฅ์น˜์— ๋ชจ๋ธ์„ ๋ฐฐ์น˜ํ•˜๋Š” ๋ผ์ธ(model.to(device))์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋‹ˆ๋ฉด ์›ํ•  ๊ฒฝ์šฐ, device ๋Œ€์‹  accelerator.device๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ dataloaders, ๋ชจ๋ธ(model) ๋ฐ ์ตœ์ ํ™” ํ•จ์ˆ˜(optimizer)๋ฅผ accelerator.prepare()๋กœ ์ž…๋ ฅํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ๋Œ€๋ถ€๋ถ„์˜ ์ž‘์—…์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ์ž…๋ ฅํ•œ ๊ฐ์ฒด๋“ค์„ ์ ์ ˆํ•œ ์ปจํ…Œ์ด๋„ˆ๋กœ ๊ฐ์‹ธ์„œ(wrapping) ๋ถ„์‚ฐ ํ•™์Šต(distributed training)์ด ์˜๋„๋Œ€๋กœ ์ž‘๋™๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ device์— ๋ฐฐ์น˜(batch)๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๋ผ์ธ์„ ์ œ๊ฑฐ(ํ•ด๋‹น ๋ผ์ธ์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด device ๋Œ€์‹ ์— accelerate.device๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.) ํ•˜๊ณ  loss.backward()๋ฅผ accelerator.backward()๋กœ ๋Œ€์น˜ํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. Cloud TPU๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์†๋„ ํ–ฅ์ƒ์˜ ์ด์ ์„ ์–ป์œผ๋ ค๋ฉด ํ† ํฌ ๋‚˜์ด์ €์˜ padding="max_length" ๋ฐ max_length ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ˜ํ”Œ์„ ๊ณ ์ • ๊ธธ์ด๋กœ ์ฑ„์šฐ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ „์ฒด ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Accelerate๋ฅผ ์‚ฌ์šฉํ•œ ๋‹ค์Œ์˜ ์™„์ „ํ•œ ํ•™์Šต ๋ฃจํ”„(training loop)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”: from accelerate import Accelerator from transforlers import AdamW, AutoModelForSequenceClassification, get_scheduler accelerator = Accelerator() model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) train_dl, eval_dl, model, optimizer = accelerator.prepare( train_dataloader, eval_dataloader, model, optimizer ) num_epochs = 3 num_training_steps = num_epochs * len(train_dl) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dl: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) ์œ„ ์ฝ”๋“œ๋ฅผ train.py ์Šคํฌ๋ฆฝํŠธ์— ๋ถ™์—ฌ๋„ฃ๊ธฐํ•˜์—ฌ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ถ„์‚ฐ ํ™˜๊ฒฝ(distributed setup)์—์„œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถ„์‚ฐ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•ด ๋ณด๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์‹ญ์‹œ์˜ค (๋…ธํŠธ๋ถ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ํ•˜๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ ์„œ๋ฒ„์— ๋กœ๊ทธ์ธ์„ ํ•ด์„œ ์‹คํ–‰ํ•ด์•ผ ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ๋ณด์ž…๋‹ˆ๋‹ค): accelerate config ๋ช‡ ๊ฐ€์ง€ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋ผ๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ๋œจ๊ณ  ์ด ๋ช…๋ น์ด ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ์„ฑ ํŒŒ์ผ์— ์ž…๋ ฅ๋œ ๋‹ต์„ ๋คํ”„ ํ•˜๋ผ๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค: accelerate launch train.py ์œ„ ๋ช…๋ น์–ด๋กœ ๋ถ„์‚ฐ ํ•™์Šต(distributed training)์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. Notebook์—์„œ ์ด๊ฒƒ์„ ์‹œ๋„ํ•˜๋ ค๋ฉด(์˜ˆ๋ฅผ ๋“ค์–ด, Colab์˜ TPU๋กœ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด) training_function()์— ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ ๋„ฃ๊ณ  ๋‹ค์Œ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งˆ์ง€๋ง‰ ์…€์„ ์‹คํ–‰ํ•˜์„ธ์š”: from accelerate import notebook_launcher notebook_launcher(training_function) Accelerate์— ๋Œ€ํ•œ ๋” ๋งŽ์€ ์˜ˆ์ œ๋Š” Accelerate repo๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. ์•„๋ž˜๋Š” ์ง€๊ธˆ๊นŒ์ง€ ๊ณต๋ถ€ํ•œ ๋ชจ๋“  ๋‚ด์šฉ(๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ, ๋ชจ๋ธ ์ •์˜, ํ•™์Šต ๋ฃจํ”„ ์ง€์ •, ๋‹จ, Accelerate๋Š” ์ œ์™ธ)์„ ์ด๋ง๋ผํ•˜์—ฌ ์ •๋ฆฌํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค: from datasets import load_dataset, load_metric from transformers import AutoTokenizer, DataCollatorWithPadding, AutoModelForSequenceClassification, AdamW, get_scheduler import torch from torch.utils.data import DataLoader from tqdm.auto import tqdm # ๋ฐ์ดํ„ฐ ์…‹ ์ ์žฌ raw_datasets = load_dataset("glue", "mrpc") # ์‚ฌ์ „ํ•™์Šต ์–ธ์–ด ๋ชจ๋ธ checkpoint ์ด๋ฆ„ ์ง€์ • checkpoint = "bert-base-uncased" # ์ง€์ •๋œ ์‚ฌ์ „ํ•™์Šต ์–ธ์–ด ๋ชจ๋ธ์—์„œ ํ† ํฌ๋‚˜์ด์ € ์ธ์Šคํ„ด์Šคํ™” tokenizer = AutoTokenizer.from_pretrained(checkpoint) # ํ† ํฌ ๋‚˜์ด์ € ํ•จ์ˆ˜ ์‚ฌ์šฉ์ž ์ •์˜ํ™” (sentence1, sentence2 ์นผ๋Ÿผ์— ๋Œ€ํ•ด์„œ๋งŒ ํ† ํฌ๋‚˜์ด์ง• ์ˆ˜ํ–‰) def tokenize_function(example): return tokenizer(example["sentence1"], example["sentence2"], truncation=True) # ํ† ํฌ๋‚˜์ด์ง• ์ˆ˜ํ–‰ tokenized_datasets = raw_datasets.map(tokenize_function, batched=True) # ๋ฐฐ์น˜(batch) ๋ณ„ ํŒจ๋”ฉ(padding)์„ ์œ„ํ•œ data collator ์ •์˜ data_collator = DataCollatorWithPadding(tokenizer=tokenizer) # ๋ถˆํ•„์š”ํ•œ ์ž…๋ ฅ ์นผ๋Ÿผ์„ ์ œ๊ฑฐํ•˜๊ณ  ์‚ฌ์ „ํ•™์Šต ์–ธ์–ด ๋ชจ๋ธ์— ํ•„์š”ํ•œ ์ž…๋ ฅ๋งŒ ๋‚จ๊น€. tokenized_datasets = tokenized_datasets.remove_columns(["sentence1", "sentence2", "idx"]) # ๋ฐ์ดํ„ฐ ์…‹์˜ label ์นผ๋Ÿผ๋ช…์„ labels๋กœ ๋ณ€๊ฒฝ tokenized_datasets = tokenized_datasets.rename_column("label", "labels") # ๋ฐ์ดํ„ฐ ์…‹์˜ ์œ ํ˜•์„ PyTorch tensor๋กœ ๋ณ€๊ฒฝ tokenized_datasets.set_format("torch") # ๋ณ€๊ฒฝ๋œ ์นผ๋Ÿผ ์ถœ๋ ฅ print(tokenized_datasets["train"].column_names) # ๊ฐ ์ข…๋ฅ˜๋ณ„ ๋ฐ์ดํ„ฐ ๋กœ๋” ์ƒ์„ฑ train_dataloader = DataLoader(tokenized_datasets["train"], shuffle=True, batch_size=8, collate_fn=data_collator) eval_dataloader = DataLoader(tokenized_datasets["validation"], shuffle=True, batch_size=8, collate_fn=data_collator) # ์‚ฌ์ „ํ•™์Šต ์–ธ์–ด ๋ชจ๋ธ ์ธ์Šคํ„ด์Šคํ™” model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) # ์ตœ์ ํ™” ํ•จ์ˆ˜ ์ •์˜ optimizer = AdamW(model.parameters(), lr=5e-5) # ์—ํฌํฌ ๊ฐœ์ˆ˜ ์„ค์ • num_epochs = 3 # ํ•™์Šต ์Šคํ… ์ˆ˜ ๊ณ„์‚ฐ num_training_steps = num_epochs * len(train_dataloader) # ํ•™์Šต ์Šค์ผ€์ค„๋Ÿฌ ์„ค์ • lr_scheduler = get_scheduler("linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps) # GPU๋กœ ๋ชจ๋ธ์„ ์ด๋™ device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") model.to(device) # ์ง„ํ–‰ ์ƒํ™ฉ๋ฐ” ์ •์˜ progress_bar = tqdm(range(num_training_steps)) # ๋ชจ๋ธ์„ ํ•™์Šต ๋ชจ๋“œ๋กœ ์ „ํ™˜ model.train() # ํ•™์Šต ๋ฃจํ”„ ์‹œ์ž‘ for epoch in range(num_epochs): for batch in train_dataloader: # ํ˜„์žฌ ๋ฐฐ์น˜ ์ค‘์—์„œ ์ž…๋ ฅ๊ฐ’์„ ๋ชจ๋‘ GPU๋กœ ์ด๋™. batch = {k: v.to(device) for k, v in batch.items()} # ๋ชจ๋ธ ์‹คํ–‰ outputs = model(**batch) # ์†์‹ค ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ loss = outputs.loss # ์—ญ์ „ํŒŒ ์ˆ˜ํ–‰ loss.backward() optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) # ํ‰๊ฐ€ ๋ฉ”ํŠธ๋ฆญ ๊ฐ€์ ธ์˜ค๊ธฐ metric = load_metric("glue", "mrpc") # ๋ชจ๋ธ์„ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์ „ํ™˜ model.eval() for batch in eval_dataloader: batch = {k: v.to(device) for k, v in batch.items()} with torch.no_grad(): outputs = model(**batch) logits = outputs.logits predictions = torch.argmax(logits, dim=-1) metric.add_batch(predictions=predictions, references=batch["labels"]) # ํ‰๊ฐ€ ๊ฒฐ๊ณผ ๊ณ„์‚ฐ ๋ฐ ์ถœ๋ ฅ metric.compute() 4. 3์žฅ ์š”์•ฝ (Summary) ์ง€๊ธˆ๊นŒ์ง€ ์žฌ๋ฏธ์žˆ์—ˆ๋‚˜์š”? 1์žฅ๊ณผ 2์žฅ์—์„œ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ–ˆ๊ณ  ๋˜ํ•œ ์ž์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ๊ฐ€์ง€๊ณ  ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด 3์žฅ์—์„œ๋Š” ๋‹ค์Œ์˜ ๋‚ด์šฉ์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค: Hub์˜ ๋ฐ์ดํ„ฐ ์…‹(datasets)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋™์  ํŒจ๋”ฉ(dynamic padding) ๋ฐ ์ฝœ๋ ˆ ์ดํ„ฐ(collator) ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ํฌํ•จํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๊ณ  ์ „์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ๋ฐ ํ‰๊ฐ€(evaluation) ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ € ์ˆ˜์ค€ ํ•™์Šต ๋ฃจํ”„(low-level training loop)๋ฅผ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. Accelerate๋ฅผ ์ด์šฉํ•ด์„œ ํ•™์Šต ๋ฃจํ”„(training loop)๊ฐ€ ๋‹ค์ค‘ GPU ๋˜๋Š” TPU์—์„œ ์ž‘๋™ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. 4์žฅ. ๋ชจ๋ธ ๋ฐ ํ† ํฌ ๋‚˜์ด์ € ๊ณต์œ  ์ฃผ์š” ์›น์‚ฌ์ดํŠธ์ธ Hugging Face Hub๋Š” ๋ˆ„๊ตฌ๋“ ์ง€ ์ƒˆ๋กœ์šด ์ตœ์ฒจ๋‹จ(state-of-the-art) ๋ชจ๋ธ ๋ฐ ๋ฐ์ดํ„ฐ ์…‹(datasets)์„ ๋ฐœ๊ฒฌํ•˜๊ณ , ์‚ฌ์šฉํ•˜๋ฉฐ ๋ฐ ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ํ•ต์‹ฌ์ ์ธ ํ”Œ๋žซํผ์ž…๋‹ˆ๋‹ค. ์ด ์‚ฌ์ดํŠธ๋Š” 10,000๊ฐœ ์ด์ƒ์˜ ๊ณต๊ฐœ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ชจ๋ธ์„ ํ˜ธ์ŠคํŒ…ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ๋ชจ๋ธ(models)์— ์ดˆ์ ์„ ๋งž์ถ”๊ณ  5์žฅ์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์…‹(datasets)์„ ์ฃผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Hub์— ํฌํ•จ๋œ ๋ชจ๋ธ๋“ค์€ Transformers ๋˜๋Š” NLP์— ๊ตญํ•œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. NLP ์šฉ Flair ๋ฐ AllenNLP, ์Œ์„ฑ(speech) ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ Asteroid ๋ฐ pyannote, ์‹œ๊ฐ(vision)์šฉ timm ๋“ฑ์˜ ๋ชจ๋ธ๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฐ ๋ชจ๋ธ๋“ค์€ ๋ฒ„์ „ ๊ด€๋ฆฌ(versioning) ๋ฐ ์žฌํ˜„์„ฑ(reproducibility)์„ ์ง€์›ํ•˜๋Š” Git ๋ฆฌํฌ์ง€ํ† ๋ฆฌ(repository)๋กœ ํ˜ธ์ŠคํŒ… ๋ฉ๋‹ˆ๋‹ค. Hub์—์„œ ๋ชจ๋ธ์„ ๊ณต์œ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์ปค๋ฎค๋‹ˆํ‹ฐ์— ๋ชจ๋ธ์„ ๊ณต๊ฐœํ•˜๊ณ  ์ด๋ฅผ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๋ชจ๋“  ์‚ฌ๋žŒ์ด ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ์œผ๋กœ์จ, ๋ชจ๋ธ์„ ์ง์ ‘ ํ•™์Šตํ•  ํ•„์š”๊ฐ€ ์—†์ด ๋ชจ๋ธ ๊ณต์œ  ๋ฐ ์‚ฌ์šฉ์„ ๋‹จ์ˆœํ™”ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ Hub์—์„œ ๋ชจ๋ธ์„<NAME>๋ฉด ํ•ด๋‹น ๋ชจ๋ธ์— ๋Œ€ํ•ด ํ˜ธ์ŠคํŒ… ๋œ ์ถ”๋ก  API(inference API)๊ฐ€ ์ž๋™์œผ๋กœ ๋ฐฐํฌ๋ฉ๋‹ˆ๋‹ค. ์ปค๋ฎค๋‹ˆํ‹ฐ์˜ ๋ˆ„๊ตฌ๋“ ์ง€ ์‚ฌ์šฉ์ž ์ •์˜ ์ž…๋ ฅ(custom inputs) ๋ฐ ์ ์ ˆํ•œ ์œ„์ ฏ(widgets)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ ํŽ˜์ด์ง€์—์„œ ์ง์ ‘ ๋ฌด๋ฃŒ๋กœ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ข‹์€ ์ ์€ Hub์—์„œ ๋ชจ๋“  ๊ณต๊ฐœ ๋ชจ๋ธ๋“ค์„<NAME>๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์™„์ „ํžˆ ๋ฌด๋ฃŒ๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค! ๋ชจ๋ธ์„ ๊ฐœ์ธ์ ์œผ๋กœ<NAME>๋ ค๋Š” ๊ฒฝ์šฐ ์œ ๋ฃŒ ํ”Œ๋žœ(paid plans)๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜<NAME>์ƒ์€ Hub๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. https://youtu.be/XvSGPZFEjDY ์ด๋ฒˆ ์žฅ์—์„œ๋Š” Hugging Face Hub์—์„œ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ด€๋ฆฌํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, huggingface.co ๊ณ„์ •์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: ๊ณ„์ • ๋งŒ๋“ค๊ธฐ 4์žฅ์€ ๋‹ค๋ฅธ ๋‚ด์šฉ์— ๋น„ํ•ด์„œ ๊ทธ ์ค‘์š”๋„๊ฐ€ ์กฐ๊ธˆ ๋‚ฎ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์žฅ๋ถ€ํ„ฐ ๋จผ์ € ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ณ  ํ–ฅํ›„์— ์ถ”๊ฐ€์ ์œผ๋กœ ํฌ์ŠคํŒ…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์กฐ๊ธˆ๋งŒ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š”. 5์žฅ. Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์šฐ๋ฆฌ๋Š” 3์žฅ์—์„œ Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฒ˜์Œ ์‚ดํŽด๋ณด์•˜๊ณ  ๋ชจ๋ธ์„ ๋ฏธ์„ธ์กฐ์ •(fine-tuning) ํ•˜๋Š” ๋ฐ 3๊ฐ€์ง€ ์ฃผ์š” ๋‹จ๊ณ„๊ฐ€ ์žˆ์Œ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค: Hugging Face Hub์—์„œ dataset์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. Dataset.map()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. Metric์„ ๋กœ๋“œํ•˜๊ณ  ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Š” Datasets๊ฐ€ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์˜ ๊ทนํžˆ ์ผ๋ถ€๋งŒ ์‚ดํŽด๋ณธ ๊ฒƒ์ผ ๋ฟ์ž…๋‹ˆ๋‹ค! ์ด ์žฅ์—์„œ๋Š” ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•ด ๋ณด๋‹ค ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ณผ์ •์—์„œ ๋‹ค์Œ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ ์…‹์ด Hub์— ์—†์œผ๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”? ๋ฐ์ดํ„ฐ ์…‹์„ ์–ด๋–ป๊ฒŒ ์Šฌ๋ผ์ด์‹ฑํ•˜๊ณ (slice) ๋‹ค์ด์‹ฑํ• (dice) ์ˆ˜ ์žˆ์„๊นŒ์š”? (๊ทธ๋ฆฌ๊ณ  Pandas๋ฅผ ๊ผญ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”?) ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹์ด ๋„ˆ๋ฌด ํฌ๊ณ  ๋…ธํŠธ๋ถ์˜ RAM์˜ ์šฉ๋Ÿ‰์ด ๋ถ€์กฑํ•  ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? "๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘(memory mapping)"๊ณผ Apache Arrow๋Š” ๋„๋Œ€์ฒด ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ๋‚˜๋งŒ์˜ ๋ฐ์ดํ„ฐ ์…‹์„ ์ƒ์„ฑํ•˜์—ฌ Hub์— ํ‘ธ์‹œ ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•ฉ๋‹ˆ๊นŒ? ์—ฌ๊ธฐ์—์„œ ๋ฐฐ์šฐ๋Š” ์„ธ๋ถ€ ๋‚ด์šฉ๋“ค์€ 6์žฅ๊ณผ 7์žฅ์˜ ๊ณ ๊ธ‰ ํ† ํฐํ™”(advanced tokenization) ๋ฐ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ์ž‘์—…์— ์‚ฌ์šฉ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์‹œ์ž‘ํ•ด ๋ด…์‹œ๋‹ค! 1. ๋งŒ์ผ ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ ์…‹์ด ํ—ˆ๋ธŒ์— ์—†๋‹ค๋ฉด? ์šฐ๋ฆฌ๋Š” ์ด์ œ Hugging Face Hub๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ  ์žˆ์ง€๋งŒ, ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ๋…ธํŠธ๋ถ์ด๋‚˜ ์›๊ฒฉ ์„œ๋ฒ„์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋กœ ์ž‘์—…ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” Datasets๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Hugging Face Hub์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋กœ์ปฌ ํ˜น์€ ์›๊ฒฉ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ์ž‘์—…ํ•˜๊ธฐ Datasets๋Š” ๋กœ์ปฌ ๋ฐ ์›๊ฒฉ ๋ฐ์ดํ„ฐ ์…‹์˜ ๋กœ๋”ฉ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ๋”ฉ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ์ผ๋ฐ˜์ ์ธ ๋ฐ์ดํ„ฐ<NAME>์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค: Data format Loading script Example CSV & TSV csv load_dataset("csv", data_files="my_file.csv") Text files text load_dataset("text", data_files="my_file.txt") JSON & JSON Lines json load_dataset("json", data_files="my_file.jsonl") Pickled DataFrames pandas load_dataset("pandas", data_files="my_dataframe.pkl") ํ‘œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, ๊ฐ ๋ฐ์ดํ„ฐ<NAME>์— ๋Œ€ํ•ด ํ•˜๋‚˜ ์ด์ƒ์˜ ํŒŒ์ผ์— ๋Œ€ํ•œ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๋Š” data_files ์ธ์ˆ˜์™€ ํ•จ๊ป˜ load_dataset() ํ•จ์ˆ˜์— ๋กœ๋”ฉํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜<NAME>์„ ์ง€์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋กœ์ปฌ ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์šฐ๋ฆฌ๋Š” ์›๊ฒฉ ํŒŒ์ผ๋กœ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋กœ์ปฌ ๋ฐ์ดํ„ฐ ์…‹ ๋กœ๋”ฉํ•˜๊ธฐ ์—ฌ๊ธฐ์„œ๋Š” ์˜ˆ์ œ ๋ฐ์ดํ„ฐ๋กœ ์ดํƒˆ๋ฆฌ์•„์–ด ์งˆ์˜์‘๋‹ต(question answering)์„ ์œ„ํ•œ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ ์…‹์ธ SQuAD-it dataset์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๋ฐ ํ‰๊ฐ€ ์ง‘ํ•ฉ์œผ๋กœ ๋ถ„ํ• ๋œ ํŒŒ์ผ๋“ค์€ GitHub์—์„œ ํ˜ธ์ŠคํŒ… ๋˜๋ฏ€๋กœ ๊ฐ„๋‹จํ•œ wget ๋ช…๋ น์œผ๋กœ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: !wget https://github.com/crux82/squad-it/raw/master/SQuAD_it-train.json.gz !wget https://github.com/crux82/squad-it/raw/master/SQuAD_it-test.json.gz ์œ„ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฆฌ๋ˆ…์Šค gzip ๋ช…๋ น์œผ๋กœ ์••์ถ•์„ ํ’€ ์ˆ˜ ์žˆ๋Š” SQuAD_it-train.json.gz ๋ฐ SQuAD_it-test.json.gz๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์••์ถ• ํŒŒ์ผ์ด ๋‹ค์šด๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค: !gzip -dkv SQuAD_it*.json.gz ์••์ถ•๋œ ํŒŒ์ผ์ด SQuAD_it-train.json, SQuAD_it-test.json ์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๊ณ  ๋ฐ์ดํ„ฐ๊ฐ€ JSON<NAME>์œผ๋กœ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. load_dataset() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ JSON ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๋ ค๋ฉด ๋Œ€์ƒ ํŒŒ์ผ์ด ์ผ๋ฐ˜์ ์ธ JSON<NAME>(ํŒŒ์ด์ฌ์˜ ์ค‘์ฒฉ ๋”•์…”๋„ˆ๋ฆฌ์™€ ์œ ์‚ฌ)์ธ์ง€ ์•„๋‹ˆ๋ฉด JSON Lines(์ค„๋กœ ๊ตฌ๋ถ„๋œ JSON)์ธ์ง€๋งŒ ์•Œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์งˆ์˜์‘๋‹ต ๋ฐ์ดํ„ฐ ์…‹๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ SQuAD-it๋„ ํ•˜๋‚˜์˜ data ํ•„๋“œ์— ๋ชจ๋“  ํ…์ŠคํŠธ๊ฐ€ ์ €์žฅ๋œ ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑ๋œ ์ค‘์ฒฉ<NAME>(nested format)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด field ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค: from datasets import load_dataset squad_it_dataset = load_dataset("json", data_files="SQuAD_it-train.json", field="data") ๊ธฐ๋ณธ์ ์œผ๋กœ ๋กœ์ปฌ ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๋ฉด ํ•™์Šต ๋ถ„ํ• (train split)์ด ์ €์žฅ๋œ DatasetDict ๊ฐœ์ฒด๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ squad_it_dataset ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: squad_it_dataset ์œ„์—์„œ ํ•™์Šต ๋ถ„ํ• ์— ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ํ–‰์˜ ์ˆ˜(num_rows)์™€ ์—ด์˜ ์ด๋ฆ„(features)์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•™์Šต ๋ถ„ํ• (train split)์„ ์ธ๋ฑ์‹ฑํ•˜์—ฌ ์˜ˆ์ œ ์ค‘ ํ•˜๋‚˜์˜ ๋‚ด์šฉ์„ ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: squad_it_dataset["train"][2] ์ข‹์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋กœ์ปฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ–ˆ์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ ๋‹จ์ผ DatasetDict ๊ฐ์ฒด์— ํ•™์Šต ๋ถ„ํ• (training split)๊ณผ ํ…Œ์ŠคํŠธ ๋ถ„ํ• (test split)์„ ๋ชจ๋‘ ํฌํ•จ์‹œ์ผœ ํ•œ๊บผ๋ฒˆ์— ๋‘ ์ง‘ํ•ฉ์— Dataset.map() ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด, data_files ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ ๋ถ„ํ•  ์ด๋ฆ„(split name)์„ ํ•ด๋‹น ์ง‘ํ•ฉ ํŒŒ์ผ๋ช…์— ๋งคํ•‘ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: data_files = {"train": "SQuAD_it-train.json", "test": "SQuAD_it-test.json"} squad_it_dataset = load_dataset("json", data_files=data_files, field="data") squad_it_dataset ์šฐ๋ฆฌ๊ฐ€ ์›ํ–ˆ๋˜ ๋ฐ”๋กœ ๊ทธ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋‹ค์–‘ํ•œ ์ „์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ฆฌํ•˜๊ณ  ํ† ํฐํ™”ํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. โœ load_dataset() ํ•จ์ˆ˜์˜ data_files ์ธ์ž๋Š” ๋‹จ์ผ ํŒŒ์ผ ๊ฒฝ๋กœ, ํŒŒ์ผ ๊ฒฝ๋กœ ๋ฆฌ์ŠคํŠธ ๋˜๋Š” ๋ถ„ํ•  ํŒŒ์ผ ์ด๋ฆ„์„ ํ•ด๋‹น ํŒŒ์ผ ๊ฒฝ๋กœ์— ๋งคํ•‘ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋„ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Unix ์…ธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ทœ์น™์— ๋”ฐ๋ผ ์ง€์ •๋œ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ํŒŒ์ผ์„ globํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, data_files="*.json"์„ ์„ค์ •ํ•˜์—ฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋ชจ๋“  JSON ํŒŒ์ผ์„ ๋‹จ์ผ ๋ถ„ํ• (split)๋กœ glob ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Datasets ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. Datasets์˜ load_dataset ํ•จ์ˆ˜๋Š” ์‹ค์ œ๋กœ ์ž…๋ ฅ ํŒŒ์ผ์˜ ์••์ถ• ํ•ด์ œ๋ฅผ ์ž๋™์œผ๋กœ ์ง€์›ํ•˜๋ฏ€๋กœ data_files ์ธ์ˆ˜์— ์••์ถ• ํŒŒ์ผ์„ ์ง์ ‘ ์ง€์ •ํ•˜์—ฌ gzip ์‚ฌ์šฉ์„ ๊ฑด๋„ˆ๋›ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: data_files = {"train": "SQuAD_it-train.json.gz", "test": "SQuAD_it-test.json.gz"} squad_it_dataset = load_dataset("json", data_files=data_files, field="data") ์ด ๊ธฐ๋Šฅ์€ ๋งŽ์€ GZIP ํŒŒ์ผ๋“ค์„ ์ˆ˜๋™์œผ๋กœ ์••์ถ• ํ•ด์ œํ•˜๋Š” ์ˆ˜๊ณ ๋ฅผ ๋œ๊ณ ์ž ํ•  ๊ฒฝ์šฐ์— ์œ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž๋™ ์••์ถ• ํ•ด์ œ(automatic decompression)๋Š” ZIP ๋ฐ TAR๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ์••์ถ•<NAME>์—๋„ ์ ์šฉ๋˜๋ฏ€๋กœ data_files์— ํ•ด๋‹น ์••์ถ• ํŒŒ์ผ๋“ค์„ ์ง€์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋…ธํŠธ๋ถ์ด๋‚˜ ๋ฐ์Šคํฌํ†ฑ์—์„œ ๋กœ์ปฌ ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•˜์œผ๋ฏ€๋กœ ์ด์ œ ์›๊ฒฉ ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์›๊ฒฉ ๋ฐ์ดํ„ฐ ์…‹ ๋กœ๋”ฉํ•˜๊ธฐ ํšŒ์‚ฌ์—์„œ ๋ฐ์ดํ„ฐ ๊ณผํ•™์ž๋‚˜ ๊ฐœ๋ฐœ์ž๋กœ ์ผํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, ๋ถ„์„ํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ ์…‹์ด ์›๊ฒฉ ์„œ๋ฒ„์— ์ €์žฅ๋˜์–ด ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ํฝ๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ๋„ ์›๊ฒฉ ํŒŒ์ผ(remote files)์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์€ ๋กœ์ปฌ ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ๋งŒํผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค! ๋กœ์ปฌ ํŒŒ์ผ์— ๋Œ€ํ•œ ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋Œ€์‹  load_dataset()์˜ data_files ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์›๊ฒฉ ํŒŒ์ผ์ด ์ €์žฅ๋œ ํ•˜๋‚˜ ์ด์ƒ์˜ URL์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, GitHub์—์„œ ํ˜ธ์ŠคํŒ… ๋˜๋Š” SQuAD-it ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด data_files ๋งค๊ฐœ๋ณ€์ˆ˜์— SQuAD_it-*.json.gz URL์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. url = "https://github.com/crux82/squad-it/raw/master/" data_files = { "train": url + "SQuAD_it-train.json.gz", "test": url + "SQuAD_it-test.json.gz", } squad_it_dataset = load_dataset("json", data_files=data_files, field="data") ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์œ„์—์„œ ์–ป์€ ๊ฒƒ๊ณผ ๋™์ผํ•œ DatasetDict ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€๋งŒ SQuAD_it-*.json.gz ํŒŒ์ผ์„ ์ˆ˜๋™์œผ๋กœ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์••์ถ•์„ ํ‘ธ๋Š” ๋‹จ๊ณ„๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์œผ๋กœ Hugging Face Hub์—์„œ ํ˜ธ์ŠคํŒ… ๋˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ๋งˆ์นฉ๋‹ˆ๋‹ค. ์ด์ œ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์—…ํ•  ๋ฐ์ดํ„ฐ ์…‹์ด ํ™•๋ณด๋˜์—ˆ์œผ๋ฏ€๋กœ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ๋žญ๊ธ€๋ง(data-wrangling, ๋ฐ์ดํ„ฐ ์กฐ์ž‘) ๊ธฐ์ˆ ์„ ์•Œ์•„๋ด…์‹œ๋‹ค. โœ GitHub ๋˜๋Š” UCI Machine Learning Repository์—์„œ ํ˜ธ์ŠคํŒ… ๋˜๋Š” ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์…‹์„ ์„ ํƒํ•˜๊ณ  ์œ„์—์„œ ์†Œ๊ฐœํ•œ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ์ปฌ ๋ฐ ์›๊ฒฉ์œผ๋กœ ๋กœ๋“œํ•ด ๋ณด์‹ญ์‹œ์˜ค. 2. ๋ฐ์ดํ„ฐ ์…‹ ์Šฌ๋ผ์ด์‹ฑ(slicing)๊ณผ ๋‹ค์ด์‹ฑ(dicing) ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์ž‘์—… ๋Œ€์ƒ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋ธ์˜ ํ•™์Šต์„ ์œ„ํ•ด ์™„๋ฒฝํ•˜๊ฒŒ ๊ฐ€๊ณต๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์…‹ ์ •์ œ(clean-up)๋ฅผ ์œ„ํ•ด ์ œ๊ณตํ•˜๋Š” Datasets์˜ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์Šฌ๋ผ์ด์‹ฑ(slicing)๊ณผ ๋‹ค์ด์‹ฑ(dicing) Pandas์™€ ์œ ์‚ฌํ•˜๊ฒŒ Datasets๋Š” Dataset ๋ฐ DatasetDict ๊ฐ์ฒด์˜ ๋‚ด์šฉ์„ ์กฐ์ž‘ํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ 3์žฅ์—์„œ Dataset.map() ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋‚˜๋ดค๊ณ , ์ด ์„น์…˜์—์„œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค์„ ์‚ดํŽด๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์„น์…˜์—์„œ ์˜ˆ์ œ๋กœ UC Irvine Machine Learning Repository์—์„œ ํ˜ธ์ŠคํŒ… ๋˜๋Š” Drug Review Dataset์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๋‹ค์–‘ํ•œ ์•ฝ๋ฌผ์— ๋Œ€ํ•œ ํ™˜์ž ๋ฆฌ๋ทฐ, ์น˜๋ฃŒ ์ƒํƒœ ๋ฐ ํ™˜์ž ๋งŒ์กฑ๋„์— ๋Œ€ํ•œ ๋ณ„ 10๊ฐœ ๋“ฑ๊ธ‰(10-star rating)์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € wget ๋ฐ unzip ๋ช…๋ น์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์••์ถ•์„ ํ’€์–ด๋ด…์‹œ๋‹ค: !wget "https://archive.ics.uci.edu/ml/machine-learning-databases/00462/drugsCom_raw.zip" !unzip drugsCom_raw.zip TSV๋Š” ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋กœ ์‰ผํ‘œ ๋Œ€์‹  ํƒญ์„ ์‚ฌ์šฉํ•˜๋Š” CSV์˜ ๋ณ€ํ˜•์ด๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด load_dataset() ํ•จ์ˆ˜์— ๊ตฌ๋ถ„ ๊ธฐํ˜ธ ๋งค๊ฐœ๋ณ€์ˆ˜(delimiter)๋ฅผ ์ง€์ •ํ•˜์—ฌ ํŒŒ์ผ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_dataset data_files = {"train": "drugsComTrain_raw.tsv", "test": "drugsComTest_raw.tsv"} # ๋Š” Python์—์„œ ํƒญ ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. drug_dataset = load_dataset("csv", data_files=data_files, delimiter="\t") ์–ด๋–ค ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๋“ ์ง€ ๊ฐ„์— ์ผ๋ถ€ ๋ฌด์ž‘์œ„ ์ƒ˜ํ”Œ์„ ์‚ดํŽด๋ด„์œผ๋กœ์จ ์ž‘์—… ์ค‘์ธ ๋ฐ์ดํ„ฐ์˜ ํŠน์ง•์ด๋‚˜ ์œ ํ˜•์„ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€ ์ ‘๊ทผ๋ฒ•์ž…๋‹ˆ๋‹ค. Datasets์—์„œ Dataset.shuffle() ๋ฐ Dataset.select() ํ•จ์ˆ˜๋ฅผ ํ•จ๊ป˜ ์—ฐ๊ฒฐ(chaining) ํ•˜์—ฌ ๋ฌด์ž‘์œ„ ์ƒ˜ํ”Œ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: drug_sample = drug_dataset["train"].shuffle(seed=42).select(range(1000)) # ์•ž์ชฝ์˜ ์ƒ˜ํ”Œ ๋ช‡ ๊ฐœ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. drug_sample[:3] ์žฌํ˜„์„ฑ(reproducibility)์„ ์œ„ํ•ด Dataset.shuffle()์˜ seed๋ฅผ ๊ณ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. Dataset.select()๋Š” ๋ฐ˜๋ณต ๊ฐ€๋Šฅ ์ธ๋ฑ์Šค(iterable indices)๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ฒ˜์Œ 1,000๊ฐœ์˜ ์˜ˆ์ œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด range(1000)์„ ์ „๋‹ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ƒ˜ํ”Œ๋“ค์—์„œ ์šฐ๋ฆฌ๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ช‡ ๊ฐ€์ง€ ๋‹จ์ (quirks)์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: "Unnamed: 0" ์นผ๋Ÿผ(column)์€ ํ™•์‹คํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ๊ฐ ํ™˜์ž์˜ ์ต๋ช… ID(anonymized ID)์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. "condition" ์นผ๋Ÿผ(column)์—๋Š” ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž ๋ ˆ์ด๋ธ”์ด ํ˜ผํ•ฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋ทฐ(review)์˜ ๊ธธ์ด๋Š” ๋‹ค์–‘ํ•˜๋ฉฐ Python ์ค„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ(\r\n)์™€ '์™€ ๊ฐ™์€ HTML ๋ฌธ์ž ์ฝ”๋“œ๊ฐ€ ํ˜ผํ•ฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. Datasets๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. "Unnamed: 0" ์นผ๋Ÿผ์ด ํ™˜์ž์˜ ์‹๋ณ„์ž(anonymous ID)๋ผ๋Š” ๊ฐ€์ •์ด ๋งž๋Š”์ง€ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด Dataset.unique() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ID์˜ ๊ฐœ์ˆ˜๊ฐ€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹ ๋ถ„ํ• ๋“ค(splits)์˜ ํ–‰(row)์˜ ์ˆ˜์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: for split in drug_dataset.keys(): assert len(drug_dataset[split]) == len(drug_dataset[split].unique("Unnamed: 0")) ์œ„ ์ฝ”๋“œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ์˜ˆ์ƒ์„ ํ™•์ธ์‹œ์ผœ์ฃผ๋Š” ๊ฒƒ ๊ฐ™์œผ๋ฏ€๋กœ, "Unnamed: 0" ์นผ๋Ÿผ์˜ ๋ช…์นญ์„ ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ๋ช…์นญ์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ์•ฝ๊ฐ„ ์ •์ œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. DatasetDict.rename_column() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ๋ฒˆ์— ๋‘ ๋ถ„ํ• (ํ•™์Šต ์ง‘ํ•ฉ ๋ฐ ํ‰๊ฐ€ ์ง‘ํ•ฉ)์—์„œ ์นผ๋Ÿผ๋ช…์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: drug_dataset = drug_dataset.rename_column( original_column_name="Unnamed: 0", new_column_name="patient_id" ) drug_dataset ๋‹ค์Œ์œผ๋กœ Dataset.map()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  condition ๋ ˆ์ด๋ธ”์„ ์ •๊ทœ ํ™”ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3์žฅ์˜ ํ† ํฐํ™”(tokenization)์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ, ์šฐ๋ฆฌ๋Š” drug_dataset์—์„œ ๊ฐ ๋ถ„ํ• (splits)์˜ ๋ชจ๋“  ํ–‰์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: def lowercase_condition(example): return {"condition": example["condition"].lower()} drug_dataset.map(lowercase_condition) ์˜ค ์ด๋Ÿฐ! map ํ•จ์ˆ˜์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค! ์ด ์˜ค๋ฅ˜๋ฅผ ํ†ตํ•ด์„œ condition ์นผ๋Ÿผ์˜ ์ผ๋ถ€ ํ•ญ๋ชฉ์ด ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋Š” None์ด๋ผ๋Š” ๊ฒƒ์„ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Dataset.map()๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•˜์ง€๋งŒ ๋ฐ์ดํ„ฐ ์…‹์˜ ๋‹จ์ผ ์˜ˆ์ œ(example)๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ํ•จ์ˆ˜๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋˜๋Š” Dataset.filter()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋Ÿฌํ•œ ํ–‰์„ ์‚ญ์ œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„ , ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช…์‹œ์  ํ•จ์ˆ˜, filter_nones๋ฅผ ์ž‘์„ฑํ•˜๊ณ : def filter_nones(x): return x["condition"] is not None drug_dataset.filter(filter_nones)๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋Œ€์‹ ์—, ๋žŒ๋‹ค ํ•จ์ˆ˜(lambda function)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ์ค„๋กœ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Python์—์„œ ๋žŒ๋‹ค ํ•จ์ˆ˜๋Š” ๋ช…์‹œ์ ์œผ๋กœ ์ด๋ฆ„์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ž‘์€ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ๋ฐ˜์ ์ธ ํ˜•ํƒœ๋ฅผ ์ทจํ•ฉ๋‹ˆ๋‹ค: lambda <arguments> : <expression> ์—ฌ๊ธฐ์„œ lambda๋Š” Python์˜ ํŠน์ˆ˜ ํ‚ค์›Œ๋“œ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. <arguments>๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„๋œ ๊ฐ’์˜ ๋ฆฌ์ŠคํŠธ/์„ธํŠธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, <expression>์€ ์‹คํ–‰ํ•˜๋ ค๋Š” ์ž‘์—…์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆซ์ž๋ฅผ ์ œ๊ณฑํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: lambda x : x * x ์ด ํ•จ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด ํ•จ์ˆ˜์™€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: (lambda x: x * x)(3) ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์—ฌ๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ผ๊ฐํ˜•์˜ ๋ฉด์ ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: (lambda base, height: 0.5 * base * height)(4, 8) ๋žŒ๋‹ค ํ•จ์ˆ˜๋Š” ์ž‘์€ ์ผํšŒ์„ฑ ํ•จ์ˆ˜(single-use function)๋ฅผ ์ •์˜ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋Š” Andre Burgaud์˜ Real Python ํŠœํ† ๋ฆฌ์–ผ์„ ์ฐธ๊ณ ํ•˜๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. Datasets๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ƒํ™ฉ์—์„œ๋Š” ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ๋งต(map) ๋ฐ ํ•„ํ„ฐ(filter) ์ž‘์—…์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ์ดํ„ฐ ์…‹์—์„œ None ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: drug_dataset = drug_dataset.filter(lambda x: x["condition"] is not None) None ํ•ญ๋ชฉ์ด ์ œ๊ฑฐ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด์ œ condition ์นผ๋Ÿผ(column)์„ ์ •๊ทœํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: drug_dataset = drug_dataset.map(lowercase_condition) # ์†Œ๋ฌธ์ž ํ™”๊ฐ€ ์ œ๋Œ€๋กœ ์ง„ํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธ. drug_dataset["train"]["condition"][:3] ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”๊ตฐ์š”! ์ด์ œ ๋ ˆ์ด๋ธ”์„ ์ •๋ฆฌํ–ˆ์œผ๋ฏ€๋กœ reviews ์ž์ฒด๋ฅผ ์ •์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ์นผ๋Ÿผ(column) ๋งŒ๋“ค๊ธฐ ๊ณ ๊ฐ ๋ฆฌ๋ทฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋งˆ๋‹ค, ๊ฐ ๋ฆฌ๋ทฐ์˜ ๋‹จ์–ด ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋ทฐ๋Š” "Great!"์™€ ๊ฐ™์€ ํ•œ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋˜๊ฑฐ๋‚˜ ๋˜๋Š” ์ˆ˜์ฒœ ๊ฐœ์˜ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋œ ๋ณธ๊ฒฉ์ ์ธ ์—์„ธ์ด(full-blown essays)๋กœ ์ž‘์„ฑ๋˜์—ˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋”ฐ๋ผ ์ด๋Ÿฌํ•œ ๊ทน๋‹จ์ ์ธ ์ƒํ™ฉ์„ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ฆฌ๋ทฐ์˜ ๋‹จ์–ด ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ ํ…์ŠคํŠธ๋ฅผ ๊ณต๋ฐฑ(whitespace)์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๋Œ€๋žต์ ์ธ ํœด๋ฆฌ์Šคํ‹ฑ(heuristics)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ฆฌ๋ทฐ์˜ ๋‹จ์–ด ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def compute_review_length(example): return {"review_length": len(example["review"].split())} lowercase_condition() ํ•จ์ˆ˜์™€ ๋‹ฌ๋ฆฌ compute_review_length()๋Š” ๊ธฐ์กด ๋ฐ์ดํ„ฐ ์…‹์˜ ์—ด(column) ์ด๋ฆ„๋“ค๊ณผ ๋‹ค๋ฅธ ์ƒˆ๋กœ์šด ์ด๋ฆ„์˜ ํ‚ค(review_length)๋ฅผ ๊ฐ€์ง„ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ compute_review_length()๊ฐ€ Dataset.map()์— ์ „๋‹ฌ๋˜๋ฉด ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ชจ๋“  ํ–‰์— ์ ์šฉ๋˜์–ด ์ด ์‹ ๊ทœ review_length ์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค: drug_dataset = drug_dataset.map(compute_review_length) # ์ฒซ ํ•™์Šต ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค. drug_dataset["train"][0] ์˜ˆ์ƒ๋Œ€๋กœ review_length ์—ด์ด ํ•™์Šต ์ง‘ํ•ฉ์— ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Dataset.sort()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑ๋œ ์—ด(column)์„ ์ •๋ ฌํ•˜์—ฌ ๊ทน๋‹จ๊ฐ’(extreme values)์ด ๋ฌด์—‡์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: drug_dataset["train"].sort("review_length")[:3] ์˜ˆ์ƒ๋Œ€๋กœ ์ผ๋ถ€ review์—๋Š” ๋‹จ์ผ ๋‹จ์–ด๋งŒ ํฌํ•จ๋˜์–ด ์žˆ๋Š”๋ฐ, ์ด๋Š” ๊ฐ์„ฑ ๋ถ„์„(sentiment analysis)์—๋Š” ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ condition์„ ์˜ˆ์ธกํ•˜๋ ค๋Š” ๊ฒฝ์šฐ์—๋Š” ํฌ๊ฒŒ ๋„์›€์ด ๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์— ์ƒˆ ์—ด์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ Dataset.add_column() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•˜๋Š” ์—ด(column)์„ Python ๋ฆฌ์ŠคํŠธ ๋˜๋Š” NumPy ๋ฐฐ์—ด๋กœ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ Dataset.map()์ด ๋ถ„์„์— ์ ํ•ฉํ•˜์ง€ ์•Š์€ ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Dataset.filter() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 30๋‹จ์–ด ๋ฏธ๋งŒ์œผ๋กœ ํ‘œํ˜„๋œ ๋ฆฌ๋ทฐ๋ฅผ ์ œ๊ฑฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. condition ์—ด์—์„œ ์ˆ˜ํ–‰ํ•œ ๊ฒƒ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ๋ฆฌ๋ทฐ์˜ ๊ธธ์ด๊ฐ€ ์ด ์ž„๊ณ—๊ฐ’(threshold)์„ ์ดˆ๊ณผํ•˜๋„๋ก ์š”๊ตฌํ•˜์—ฌ ๋งค์šฐ ์งง์€ ๋ฆฌ๋ทฐ๋ฅผ ํ•„ํ„ฐ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: drug_dataset = drug_dataset.filter(lambda x: x["review_length"] > 30) print(drug_dataset.num_rows) ์œ„ ๊ฒฐ๊ณผ์—์„œ ๋ณด๋‹ค์‹œํ”ผ, ์ด๊ฒƒ์€ ์›๋ณธ ํ•™์Šต ๋ฐ ํ‰๊ฐ€ ์ง‘ํ•ฉ์—์„œ ๋ฆฌ๋ทฐ์˜ ์•ฝ 15%๋ฅผ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ํ•ด๊ฒฐํ•ด์•ผ ํ•  ๋ฌธ์ œ๋Š” ๋ฆฌ๋ทฐ์— HTML ๋ฌธ์ž ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Python์˜ html ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋Ÿฌํ•œ ๋ฌธ์ž๋ฅผ ์ด์Šค์ผ€์ดํ”„ ํ•ด์ œ(unescape) ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: import html text = "I'm a transformer called BERT" html.unescape(text) Dataset.map()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ง๋ญ‰์น˜์˜ ๋ชจ๋“  HTML ๋ฌธ์ž๋ฅผ ์ด์Šค์ผ€์ดํ”„ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค: drug_dataset = drug_dataset.map(lambda x: {"review": html.unescape(x["review"])}) ์ง€๊ธˆ๊นŒ์ง€ ์‚ดํŽด๋ณธ ๋ฐ”์™€ ๊ฐ™์ด, Dataset.map() ๋ฉ”์„œ๋“œ๋Š” ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์•„์ง๋„ ์šฐ๋ฆฌ๋Š” ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์˜ ์ผ๋ถ€๋งŒ ์‚ดํŽด๋ดค์„ ๋ฟ์ž…๋‹ˆ๋‹ค. map() ๋ฉ”์„œ๋“œ์˜ ๋Œ€๋‹จํ•œ ๋Šฅ๋ ฅ Dataset.map() ๋ฉ”์„œ๋“œ์˜ batched ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ True๋กœ ์„ค์ •๋˜๋ฉด, ํ˜ธ์ถœ๋˜๋Š” ์ˆœ๊ฐ„๋งˆ๋‹ค ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์˜ˆ์ œ๋กœ ๊ตฌ์„ฑ๋œ ํ•˜๋‚˜์˜ ๋ฐฐ์น˜(batch)๊ฐ€ ํ•œ ๋ฒˆ์— map ํ•จ์ˆ˜์— ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ํฌ๊ธฐ(batch size)๋Š” ๋ณ„๋„๋กœ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๊ณ  ๋””ํดํŠธ ๊ฐ’์€ 1000์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจ๋“  HTML ํŠน์ˆ˜๋ฌธ์ž๋“ค์„ ์ด์Šค์ผ€์ดํ”„ ํ•ด์ œ(unescape) ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์œ„์—์„œ ์‹คํ–‰ํ•œ map ํ•จ์ˆ˜๋Š” ์‹คํ–‰ ์†๋„๊ฐ€ ์•ฝ๊ฐ„ ๋Š๋ฆฝ๋‹ˆ๋‹ค(์ง„ํ–‰ ํ‘œ์‹œ์ค„์—์„œ ์†Œ์š”๋œ ์‹œ๊ฐ„์„ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค). ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌ ํ—จ ์…˜(list comprehension, ๋‚ดํฌ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์‹œ์— ์—ฌ๋Ÿฌ ์˜ˆ์ œ๋ฅผ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜์—ฌ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. batched=True๊ฐ€ ์ง€์ •๋˜๋ฉด Dataset.map() ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋ฐ์ดํ„ฐ ์…‹์˜ ํ•„๋“œ๊ฐ€ ํฌํ•จ๋œ ํ•˜๋‚˜์˜ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ž…๋ ฅ๋ฐ›์ง€๋งŒ ์ด ๋”•์…”๋„ˆ๋ฆฌ ๋‚ด๋ถ€์˜ ๊ฐ ํ•„๋“œ๊ฐ’์€ ์ด์ œ ๋‹จ์ผ ๊ฐ’์ด ์•„๋‹ˆ๋ผ ๋ฆฌ์ŠคํŠธ(list of values)์ž…๋‹ˆ๋‹ค. Dataset.map()์˜ ๋ฐ˜ํ™˜ ๊ฐ’์€ ๋™์ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฐ์ดํ„ฐ ์…‹์— ์—…๋ฐ์ดํŠธํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ํ•„๋“œ๋“ค์ด ์กด์žฌํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ์™€ ๊ฐ’ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ ์ฝ”๋“œ๋Š” ๋ชจ๋“  HTML ๋ฌธ์ž๋ฅผ ์ด์Šค์ผ€์ดํ”„ ํ•ด์ œํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด์ง€๋งŒ batch=True๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: new_drug_dataset = drug_dataset.map( lambda x: {"review": [html.unescape(o) for o in x["review"]]}, batched=True ) ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ(jupyter notebook)์—์„œ ์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ด ๋ช…๋ น์ด ์ด์ „ ๋ช…๋ น๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฅด๊ฒŒ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” review๊ฐ€ ์ด๋ฏธ HTML ์ด์Šค์ผ€์ดํ”„ ํ•ด์ œ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด ์•„๋‹™๋‹ˆ๋‹ค. batched=True๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์€ ์ด์ „ ์„น์…˜์˜ ๋ช…๋ น์„ ๋‹ค์‹œ ์‹คํ–‰ํ•˜๋ฉด ์ด์ „๊ณผ ๊ฐ™์€ ์‹œ๊ฐ„์ด ์†Œ๋ชจ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฆฌ์ŠคํŠธ ๋‚ด ํฌ๋ฌธ(list comprehension)์ด for ๋ฃจํ”„์—์„œ ๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์˜ˆ์ œ ํ•˜๋‚˜์”ฉ์ด ์•„๋‹ˆ๋ผ ๋™์‹œ์— ๋งŽ์€ ์š”์†Œ์— ํ•œ๊บผ๋ฒˆ์— ์ ‘๊ทผํ•จ์œผ๋กœ์จ ์†๋„๊ฐ€ ๋นจ๋ผ์ง„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Dataset.map()์„ batched=True์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ 6์žฅ์—์„œ ์‚ดํŽด๋ณผ "๋น ๋ฅธ(fast)" ํ† ํฌ ๋‚˜์ด์ €์˜ ์†๋„๋ฅผ ์ž ๊ธˆ ํ•ด์ œํ•˜๋Š”(unlock) ๋ฐ ํ•„์ˆ˜์ ์ด๋ฉฐ, ์ด๋Š” ๊ทœ๋ชจ๊ฐ€ ํฐ ํ…์ŠคํŠธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋น ๋ฅด๊ฒŒ ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €(fast tokenizer)๋กœ ๋ชจ๋“  ์•ฝ๋ฌผ ๋ฆฌ๋ทฐ(drug review)๋ฅผ ํ† ํฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") def tokenize_function(examples): return tokenizer(examples["review"], truncation=True) 3์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ํ•˜๋‚˜ ํ˜น์€ ๊ทธ ์ด์ƒ์˜ ์˜ˆ์ œ๋ฅผ ํ† ํฌ ๋‚˜์ด์ €์— ํ•œ ๋ฒˆ์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด ํ•จ์ˆ˜๋ฅผ batched=True ์œ ๋ฌด์— ๊ด€๊ณ„์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋‹ค์–‘ํ•œ ์˜ต์…˜์˜ ์†๋„ ๋น„๊ต๋ฅผ ํ•œ๋ฒˆ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ ์†๋„๋ฅผ ์ธก์ •ํ•˜๋ ค๋Š” ์ฝ”๋“œ ์ค„ ์•ž์— %time์„ ์ถ”๊ฐ€ํ•˜์—ฌ ํ•œ ์ค„ ๋ช…๋ น์˜ ์‹œ๊ฐ„์„ ์ธก์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: %time tokenized_dataset = drug_dataset.map(tokenize_function, batched=True) ์…€์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— %%time์„ ๋„ฃ์–ด ์ „์ฒด ์…€์˜ ์‹คํ–‰ ์‹œ๊ฐ„์„ ์ธก์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•œ ํ•˜๋“œ์›จ์–ด์—์„œ๋Š” ์ด ๋ช…๋ น์–ด์˜ ์‹คํ–‰ ์‹œ๊ฐ„์ด 10.8์ดˆ๋กœ ์ธก์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค('Wall time' ๋’ค์— ์ ํžŒ ์ˆซ์ž๋ฅผ ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค). ๋‹ค์Œ ํ‘œ๋Š” ๋น ๋ฅด๊ณ (fast) ๋Š๋ฆฐ(slow) ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๊ด„ ์ฒ˜๋ฆฌ(batching)๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์–ป์€ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค: Options Fast tokenizer Slow tokenizer batched=True 10.8s 4min41s batched=False 59.2s 5min3s ์œ„ ๊ฒฐ๊ณผ๋Š” batched=True ์˜ต์…˜๊ณผ ํ•จ๊ป˜ ๋น ๋ฅธ(fast) ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๊ด„ ์ฒ˜๋ฆฌ๊ฐ€ ์—†๋Š” ๋Š๋ฆฐ ํ† ํฌ ๋‚˜์ด์ €๋ณด๋‹ค 30๋ฐฐ ๋” ๋น ๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ •๋ง ๋†€๋ผ์šด ์ผ์ž…๋‹ˆ๋‹ค! ์ด๊ฒƒ์ด AutoTokenizer๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋””ํดํŠธ๋กœ ์ง€์ •๋œ ์ด์œ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์™œ "๋น ๋ฅธ(fast)"์ด๋ผ๋Š” ์ˆ˜์‹์–ด๊ฐ€ ๋ถ™์—ˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ด์œ ๊ธฐ ๊ธฐ๋„ํ•˜์ง€์š”. ์ด๋Ÿฌํ•œ ์†๋„ ํ–ฅ์ƒ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” ํ•˜๋ถ€์—์„œ ํ† ํฐํ™” ์ฝ”๋“œ๊ฐ€ ์ฝ”๋“œ ์‹คํ–‰์„ ์‰ฝ๊ฒŒ ๋ณ‘๋ ฌํ™”ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์–ธ์–ด์ธ Rust์—์„œ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ณ‘๋ ฌํ™”(parallelization)๋Š” ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ(batching)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ 6๋ฐฐ ์ด์ƒ์˜ ์†๋„ ํ–ฅ์ƒ์„ ๊ฐ€์ ธ์˜ค๋Š” ์ด์œ ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ผ ํ† ํฐํ™” ์ž‘์—…์„ ๋ณ‘๋ ฌํ™”ํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋งŽ์€ ํ…์ŠคํŠธ๋ฅผ ๋™์‹œ์— ํ† ํฐํ™”ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ๊ฐ๊ฐ์˜ ํ…์ŠคํŠธ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๋กœ ์‹คํ–‰์„ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Dataset.map()์—๋Š” ์ž์ฒด ๋ณ‘๋ ฌํ™” ๊ธฐ๋Šฅ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Rust๊ฐ€ ์ง€์›๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋Š๋ฆฐ ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋”ฐ๋ผ์žก์„ ์ˆ˜ ์—†์ง€๋งŒ ์—ฌ์ „ํžˆ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(ํŠนํžˆ ๋น ๋ฅธ ๋ฒ„์ „์ด ์—†๋Š” ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋” ๊ทธ๋ ‡์ง€์š”). ๋‹ค์ค‘ ์ฒ˜๋ฆฌ๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด num_proc ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  Dataset.map() ํ˜ธ์ถœ์— ์‚ฌ์šฉํ•  ํ”„๋กœ์„ธ์Šค ์ˆ˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค: slow_tokenizer = AutoTokenizer.from_pretrained("bert-base-cased", use_fast=False) def slow_tokenize_function(examples): return slow_tokenizer(examples["review"], truncation=True) %time tokenized_dataset = drug_dataset.map(slow_tokenize_function, batched=True, num_proc=8) ์‚ฌ์šฉํ•  ์ตœ์ ์˜ ํ”„๋กœ์„ธ์Šค ์ˆ˜๋ฅผ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ์†๋„ ์ธก์ •์„ ํ†ตํ•ด ์•ฝ๊ฐ„์˜ ์‹คํ—˜์„ ์ง„ํ–‰ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” num_proc=8์ผ ๋•Œ ์ตœ๊ณ ์˜ ์†๋„ ํ–ฅ์ƒ์„ ๋ณด์ด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์˜€์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋‹ค์ค‘ ์ฒ˜๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์‹คํ—˜ํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค: Options Fast tokenizer Slow tokenizer batched=True 10.8s 4min41s batched=False 59.2s 5min3s batched=True, num_proc=8 6.52s 41.3s batched=False, num_proc=8 9.49s 45.2s ์ด ๊ฒฐ๊ณผ๋Š” ๋Š๋ฆฐ ํ† ํฌ ๋‚˜์ด์ €์˜ ๊ฒฝ์šฐ ํ›จ์”ฌ ๋” ํ•ฉ๋ฆฌ์ ์ธ ๊ฒฐ๊ณผ์ด์ง€๋งŒ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €์˜ ์„ฑ๋Šฅ๋„ ํฌ๊ฒŒ ํ–ฅ์ƒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•ญ์ƒ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. num_proc์„ 8์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ˆ˜๋กœ ์ง€์ •ํ•œ ๊ฒฝ์šฐ, ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ num_proc ์˜ต์…˜ ์—†์ด batched=True๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๋น ๋ฅธ ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ batched=True์ธ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €์—๋Š” Python ๋‹ค์ค‘ ์ฒ˜๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. num_proc์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ๋†’์ด๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ ์ค‘์ธ ํ•จ์ˆ˜๊ฐ€ ์ž์ฒด์ ์œผ๋กœ ๋‹ค์ค‘ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ข‹์€ ์•„์ด๋””์–ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ ์ฆ‰, Dataset.map()์œผ๋กœ ํ†ตํ•ฉํ•œ ๊ฒƒ์€ ์ด๋ฏธ ๊ทธ ์ž์ฒด๋กœ๋„ ํ›Œ๋ฅญํ•˜์ง€๋งŒ ๋” ๋งŽ์€ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค! Dataset.map() ๋ฐ batched=True๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์…‹์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํ•˜๋‚˜์˜ ์˜ˆ์ œ(example)์—์„œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ•™์Šต ์ž์งˆ(training features)์„ ์ถ”๊ฐ€์ ์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์—์„œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Š” 7์žฅ์—์„œ ์ˆ˜ํ–‰ํ•  ์—ฌ๋Ÿฌ NLP ์ž‘์—…์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ์˜ ์ผ๋ถ€๋กœ๋„ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ธฐ๊ณ„ ํ•™์Šต์—์„œ ํ•˜๋‚˜์˜ ์˜ˆ์ œ(example)๋Š” ๋ชจ๋ธ์— ์ œ๊ณตํ•˜๋Š” ์ž์งˆ(feature)์˜ ์ง‘ํ•ฉ์œผ๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ํŠน์ • ์ƒํ™ฉ์—์„œ ์ด๋Ÿฌํ•œ ์ž์งˆ(features)์€ Dataset ๋‚ด์˜ ์นผ๋Ÿผ(column) ์ง‘ํ•ฉ์œผ๋กœ ํ‘œํ˜„๋˜์ง€๋งŒ, ๋‹ค๋ฅธ ๋งฅ๋ฝ์—์„œ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž์งˆ(multiple features)์ด ํ•˜๋‚˜์˜ ๋‹จ์ผ ์˜ˆ์ œ(example)์—์„œ ์ถ”์ถœ๋˜์–ด ๋‹จ์ผ ์นผ๋Ÿผ(column)์— ์†ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ํ•œ๋ฒˆ ๋ด…์‹œ๋‹ค! ์—ฌ๊ธฐ์„œ๋Š” ์šฐ๋ฆฌ ์˜ˆ์ œ(examples)๋“ค์„ ํ† ํฐํ™”ํ•˜๊ณ  ์ตœ๋Œ€ ๊ธธ์ด 128๋กœ ์ž๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๊ณผ์ •์—์„œ ํ† ํฌ ๋‚˜์ด์ €์—๊ฒŒ ์ „์ฒด review์˜ ์•ž๋ถ€๋ถ„์— ์žˆ๋Š” 128๊ฐœ์˜ ํ† ํฐ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ฒญํฌ(chunk)๊ฐ€ ์•„๋‹ˆ๋ผ ํ…์ŠคํŠธ์˜ ๋ชจ๋“  ์ฒญํฌ(chunk)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์š”์ฒญํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” return_overflowing_tokens=True๋กœ ์ง€์ •ํ•จ์œผ๋กœ์จ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: def tokenize_and_split(examples): return tokenizer( examples["review"], truncation=True, max_length=128, return_overflowing_tokens=True, ) ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•ด์„œ Dataset.map()์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ์˜ˆ์ œ ํ•˜๋‚˜์—์„œ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: result = tokenize_and_split(drug_dataset["train"][0]) [len(inp) for inp in result["input_ids"]] result["input_ids"] ํ•™์Šต ์ง‘ํ•ฉ์˜ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ(example)๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ง€์ •ํ•œ ์ตœ๋Œ€ ํ† ํฐ ์ˆ˜(128)๋ณด๋‹ค ๋งŽ์ด ํ† ํฐํ™”๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๊ฐ€์ง€ ์ž์งˆ(feature)๋กœ ๊ตฌ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ž์งˆ์˜ ๊ธธ์ด๋Š” 128์ด๊ณ  ๋‘ ๋ฒˆ์งธ ์ž์งˆ์˜ ๊ธธ์ด๋Š” 49์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ชจ๋“  ์š”์†Œ์— ๋Œ€ํ•ด ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค! tokenized_dataset = drug_dataset.map(tokenize_and_split, batched=True) ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์œ ๊ฐ€ ๋ญ˜๊นŒ์š”? ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ฉด ๋‹จ์„œ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์นผ๋Ÿผ(column) ์ค‘ ํ•˜๋‚˜์˜ ๊ฐœ์ˆ˜์— ๋ถˆ์ผ์น˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ๊ฐœ์ˆ˜๊ฐ€ 1,463์ด๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ๊ธธ์ด๊ฐ€ 1,000์ž…๋‹ˆ๋‹ค. Dataset.map()์— ๋Œ€ํ•œ Documentation์„ ๋ณธ ์ ์ด ์žˆ๋‹ค๋ฉด, ์ด ์ˆซ์ž(1000)๋Š” ๋งคํ•‘ํ•˜๋Š” ํ•จ์ˆ˜(tokenize_and_split)์— ์ „๋‹ฌ๋œ ์ด ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•  ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ 1,000๊ฐœ์˜ ์˜ˆ์ œ๊ฐ€ ์ž…๋ ฅ๋˜์–ด 1,463๊ฐœ์˜ ์ƒˆ๋กœ์šด ์ž์งˆ๋“ค์„ ์ถœ๋ ฅํ•˜๋ฏ€๋กœ shape error๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋Š” ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์…‹์„ ํ˜ผํ•ฉํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์˜ค๋ฅ˜์—์„œ drug_dataset ์—ด์—๋Š” 1,000๊ฐœ์˜ ์˜ˆ์ œ๊ฐ€ ์žˆ์œผ๋‚˜ ์šฐ๋ฆฌ๊ฐ€ ์ƒˆ๋กญ๊ฒŒ ๊ตฌ์„ฑํ•˜๋ ค๋Š” tokenized_dataset์—๋Š” ๊ทธ๋ณด๋‹ค ๋งŽ์€ ์ˆ˜์˜ ์˜ˆ์ œ(1,463)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Dataset ๊ฐ์ฒด๋กœ์„œ๋Š” ์ œ๋Œ€๋กœ ์ž‘๋™ํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด์ „ ๋ฐ์ดํ„ฐ ์…‹์˜ ์—ด์„ ์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ์…‹๊ณผ ๋™์ผํ•œ ํฌ๊ธฐ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. remove_columns ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•จ์œผ๋กœ์จ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenized_dataset = drug_dataset.map( tokenize_and_split, batched=True, remove_columns=drug_dataset["train"].column_names ) ์ด์ œ ์˜ค๋ฅ˜ ์—†์ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๊ธธ์ด๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ƒˆ ๋ฐ์ดํ„ฐ ์…‹์— ์›๋ž˜ ๋ฐ์ดํ„ฐ ์…‹๋ณด๋‹ค ์–ผ๋งˆ๋‚˜ ๋” ๋งŽ์€ ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: len(tokenized_dataset["train"]), len(drug_dataset["train"]) ์•ž์—์„œ ์ด์ „ ์—ด์„ ์ƒˆ ์—ด๊ณผ ๊ฐ™์€ ํฌ๊ธฐ๋กœ ๋งŒ๋“ค์–ด ๊ธธ์ด ๋ถˆ์ผ์น˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ์–ธ๊ธ‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ, return_overflowing_tokens=True๋ฅผ ์„ค์ •ํ•  ๋•Œ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” overflow_to_sample_mapping ํ•„๋“œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์šฐ๋ฆฌ์—๊ฒŒ ์ƒˆ๋กœ์šด ์ž์งˆ ์ธ๋ฑ์Šค์—์„œ ๊ทธ๊ฒƒ์ด ์‹œ์ž‘๋œ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค๋กœ์˜ ๋งคํ•‘์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ž์งˆ์„ ์ƒ์„ฑํ•˜๋Š” ํšŸ์ˆ˜๋งŒํผ ๊ฐ ์˜ˆ์ œ์˜ ๊ฐ’์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์›๋ณธ ๋ฐ์ดํ„ฐ ์…‹์— ์žˆ๋Š” ๊ฐ ํ‚ค๋ฅผ ์˜ฌ๋ฐ”๋ฅธ ํฌ๊ธฐ์˜ ๊ฐ’ ๋ชฉ๋ก๊ณผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: def tokenize_and_split(examples): result = tokenizer( examples["review"], truncation=True, max_length=128, return_overflowing_tokens=True, ) # ์‹ ๊ทœ ์ธ๋ฑ์Šค์™€ ์ด์ „ ์ธ๋ฑ์Šค์™€์˜ ๋งคํ•‘ ์ถ”์ถœ sample_map = result.pop("overflow_to_sample_mapping") for key, values in examples.items(): result[key] = [values[i] for i in sample_map] return result ์ด๋กœ์จ ์ด์ „ ์—ด์„ ์ œ๊ฑฐํ•  ํ•„์š” ์—†์ด Dataset.map()๊ณผ ํ•จ๊ป˜ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenized_dataset = drug_dataset.map(tokenize_and_split, batched=True) tokenized_dataset ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ ์ฆ๊ฐ€๋œ ์ˆ˜์˜ ํ•™์Šต ์ž์งˆ๋“ค์„ ์–ป์—ˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๋ชจ๋“  ์ด์ „ ํ•„๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์ ์šฉํ•œ ํ›„์— ์ด๋“ค ์ด์ „ ํ•„๋“œ๋“ค์— ๋Œ€ํ•œ ํ›„์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ด ์ ‘๊ทผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€, Datasets๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์…‹์„ ์ „์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. Datasets์˜ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋“ค์€ ๋ชจ๋ธ ํ•™์Šต์— ํ•„์š”ํ•œ ๋Œ€๋ถ€๋ถ„์˜ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ˆ˜์šฉํ•˜์ง€๋งŒ, DataFrame.groupby() ๋˜๋Š” ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•œ ๊ณ ๊ธ‰ API์™€ ๊ฐ™์€ ๋ณด๋‹ค ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Pandas๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ Datasets๋Š” Pandas, NumPy, PyTorch, TensorFlow ๋ฐ JAX์™€ ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ์ƒํ˜ธ ์šด์šฉ ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Datasets๊ณผ DataFrames ๊ฐ„์˜ ์ƒํ˜ธ ๋ณ€ํ™˜ ๋‹ค์–‘ํ•œ ์„œ๋“œ ํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค ๊ฐ„์˜ ๋ณ€ํ™˜์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด Datasets๋Š” Dataset.set_format() ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ๋ฐ์ดํ„ฐ ์…‹์˜ ์ถœ๋ ฅ<NAME>(output format)๋งŒ ๋ณ€๊ฒฝํ•˜๋ฏ€๋กœ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํฌ๋งท(Apache Arrow)์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ<NAME>์œผ๋กœ ์‰ฝ๊ฒŒ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ ๋ฐ์ดํ„ฐ ์…‹์„ Pandas๋กœ ๋ณ€ํ™˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: drug_dataset.set_format("pandas") ์ด์ œ ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ฐ ์š”์†Œ์— ์•ก์„ธ์Šคํ•  ๋•Œ ๋”•์…”๋„ˆ๋ฆฌ ๋Œ€์‹  pandas.DataFrame์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: drug_dataset["train"][:3] drug_dataset["train"]์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์„ ํƒํ•˜์—ฌ ์ „์ฒด ํ•™์Šต ์ง‘ํ•ฉ์— ๋Œ€ํ•œ pandas.DataFrame์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: train_df = drug_dataset["train"][:] ๋‚ด๋ถ€์ ์œผ๋กœ Dataset.set_format()์€ ๋ฐ์ดํ„ฐ ์…‹์˜ __getitem__() ๋˜๋”(dunder, double under) ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ ๋ฐ˜ํ™˜<NAME>์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ "pandas"<NAME>์˜ Dataset์—์„œ train_df์™€ ๊ฐ™์€ ์ƒˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋Š” ๊ฒฝ์šฐ pandas.DataFrame์„ ์–ป๊ธฐ ์œ„ํ•ด ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ์Šฌ๋ผ์ด์‹ฑ(slicing)ํ•ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ<NAME>์— ๊ด€๊ณ„์—†์ด drug_dataset["train"]์˜ ์œ ํ˜•์ด Dataset ์ž„์„ ์ง์ ‘ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๋ชจ๋“  Pandas ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์šฐ๋ฆฌ๋Š” condition ํ•ญ๋ชฉ์˜ ํด๋ž˜์Šค ๋ถ„ํฌ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฉ‹์ง„ ์—ฐ๊ฒฐ(fancy chaining)์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: frequencies = ( train_df["condition"] .value_counts() .to_frame() .reset_index() .rename(columns={"index": "condition", "condition": "frequency"}) ) frequencies.head() Pandas ๋ถ„์„์ด ๋๋‚˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด Dataset.from_pandas() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ญ์ƒ ์ƒˆ๋กœ์šด Dataset ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import Dataset freq_dataset = Dataset.from_pandas(frequencies) freq_dataset ์ด๊ฒƒ์œผ๋กœ Datasets์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์ „์ฒ˜๋ฆฌ ๊ธฐ์ˆ ์— ๋Œ€ํ•œ ๋‘˜๋Ÿฌ๋ณด๊ธฐ๋ฅผ ๋งˆ์นฉ๋‹ˆ๋‹ค. ์„น์…˜์„ ๋งˆ๋ฌด๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ํ•™์Šตํ•  ๋ฐ์ดํ„ฐ ์…‹์„ ์ค€๋น„ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒ€์ฆ ์ง‘ํ•ฉ(validation set)์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ์ „์— "pandas"์—์„œ "arrow"๋กœ drug_dataset์˜ ์ถœ๋ ฅ<NAME>์„ ์žฌ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค: drug_dataset.reset_format() ๊ฒ€์ฆ ์ง‘ํ•ฉ(validation set) ์ƒ์„ฑ ๋ชจ๋ธ ์„ฑ๋Šฅ ํ‰๊ฐ€์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ‰๊ฐ€ ์ง‘ํ•ฉ(test set)์ด ์žˆ์ง€๋งŒ ๊ฐœ๋ฐœ ์ค‘์— ์ด ํ‰๊ฐ€ ์ง‘ํ•ฉ์„ ๊ทธ๋Œ€๋กœ ๋‘๊ณ  ๋ณ„๋„์˜ ๊ฒ€์ฆ ์ง‘ํ•ฉ(validation set)์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ฒ€์ฆ ์ง‘ํ•ฉ(validation set)์—์„œ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์— ๋งŒ์กฑํ•˜๋ฉด ํ‰๊ฐ€ ์ง‘ํ•ฉ(test set)์—์„œ ์ตœ์ข…์ ์ธ ์˜จ์ „์„ฑ ๊ฒ€์‚ฌ(sanity check)๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ ๋ชจ๋ธ์ด ํ‰๊ฐ€ ์ง‘ํ•ฉ(test set)์— ๊ณผ์ ํ•ฉ๋˜์–ด ์‹ค์ œ ๋ฐ์ดํ„ฐ์—์„œ๋Š” ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง€๋Š” ๋ชจ๋ธ์„ ๋ฐฐํฌํ•˜๋Š” ์œ„ํ—˜์„ฑ์„ ์™„ํ™”ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. Datasets๋Š” scikit-learn์˜ ์œ ๋ช…ํ•œ ๊ธฐ๋Šฅ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” Dataset.train_test_split() ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ์ง‘ํ•ฉ์„ ํ•™์Šต ๋ฐ ๊ฒ€์ฆ ์ง‘ํ•ฉ์œผ๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์žฌํ˜„์„ฑ(reproducibility) ์œ ์ง€๋ฅผ ์œ„ํ•ด seed ์ธ์ˆ˜๋ฅผ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค: drug_dataset_clean = drug_dataset["train"].train_test_split(train_size=0.8, seed=42) # ๊ธฐ๋ณธ "test" ๋ถ„ํ• ์„ "validation"์œผ๋กœ ๋ณ€๊ฒฝํ•จ. drug_dataset_clean["validation"] = drug_dataset_clean.pop("test") # 'DatasetDict'์— "test" ์ง‘ํ•ฉ์„ ์ถ”๊ฐ€. drug_dataset_clean["test"] = drug_dataset["test"] drug_dataset_clean ์ข‹์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ํ•™์Šตํ•  ์ค€๋น„๊ฐ€ ๋œ ๋ฐ์ดํ„ฐ ์…‹์„ ์™„์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค! ์„น์…˜ 5์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์…‹์„ Hugging Face Hub์— ์—…๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ์ง€๋งŒ ์ง€๊ธˆ์€ ๋กœ์ปฌ ์ปดํ“จํ„ฐ์— ๋ฐ์ดํ„ฐ ์…‹์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ณ  ๋งˆ๋ฌด๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹ ์ €์žฅ Datasets๋Š” ๋‹ค์šด๋กœ๋“œํ•œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์…‹๊ณผ ์ด์— ๋Œ€ํ•ด ์ˆ˜ํ–‰๋œ ์ž‘์—…์„ ์ž„์‹œ์ €์žฅํ•˜์ง€๋งŒ ๋ฐ์ดํ„ฐ ์…‹์„ ๋””์Šคํฌ์— ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค (์˜ˆ: ์บ์‹œ๊ฐ€ ์‚ญ์ œ๋œ ๊ฒฝ์šฐ). ์•„๋ž˜ ํ‘œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด Datasets๋Š” ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์–‘ํ•œ<NAME>์œผ๋กœ ์ €์žฅํ•˜๋Š” ์„ธ ๊ฐ€์ง€ ์ฃผ์š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Data format Function Arrow Dataset.save_to_disk() CSV Dataset.to_csv() JSON Dataset.to_json() ์˜ˆ๋ฅผ ๋“ค์–ด, ์ •๋ฆฌ๋œ ๋ฐ์ดํ„ฐ ์…‹์„ Arrow<NAME>์œผ๋กœ ์ €์žฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: drug_dataset_clean.save_to_disk("drug-reviews") ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค: drug-reviews/ โ”œโ”€โ”€ dataset_dict.json โ”œโ”€โ”€ test โ”‚ โ”œโ”€โ”€ dataset.arrow โ”‚ โ”œโ”€โ”€ dataset_info.json โ”‚ โ””โ”€โ”€ state.json โ”œโ”€โ”€ train โ”‚ โ”œโ”€โ”€ dataset.arrow โ”‚ โ”œโ”€โ”€ dataset_info.json โ”‚ โ”œโ”€โ”€ indices.arrow โ”‚ โ””โ”€โ”€ state.json โ””โ”€โ”€ validation โ”œโ”€โ”€ dataset.arrow โ”œโ”€โ”€ dataset_info.json โ”œโ”€โ”€ indices.arrow โ””โ”€โ”€ state.json ์—ฌ๊ธฐ์„œ ๊ฐ ๋ถ„ํ• ์€ ์ž์ฒด dataset.arrow ํ…Œ์ด๋ธ”๊ณผ dataset_info.json ๋ฐ state.json ๊ฐ™์€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Arrow<NAME>์€ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ ์…‹์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ „์†กํ•˜๋Š” ๊ณ ์„ฑ๋Šฅ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์ถ•ํ•˜๋Š”๋ฐ ์ตœ์ ํ™”๋œ ํ…Œ์ด๋ธ”๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์ด ์ €์žฅ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด load_from_disk() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_from_disk drug_dataset_reloaded = load_from_disk("drug-reviews") drug_dataset_reloaded CSV ๋ฐ JSON<NAME>์˜ ๊ฒฝ์šฐ ๊ฐ ๋ถ„ํ• ์„ ๋ณ„๋„์˜ ํŒŒ์ผ๋กœ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ DatasetDict ๊ฐœ์ฒด์˜ ํ‚ค์™€ ๊ฐ’์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: for split, dataset in drug_dataset_clean.items(): dataset.to_json(f"drug-reviews-{split}.jsonl") ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ๊ฐ ํ–‰์ด JSON์˜ ํ•œ ์ค„๋กœ ์ €์žฅ๋˜๋Š” JSON Lines<NAME>์œผ๋กœ ๊ฐ ๋ถ„ํ• ์ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์ €์žฅ๋œ ํ•™์Šต ์ง‘ํ•ฉ ํŒŒ์ผ์˜ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: !head -n 1 drug-reviews-train.jsonl ๊ทธ๋Ÿฐ ๋‹ค์Œ ์„น์…˜ 2์—์„œ ์„ค๋ช…ํ•œ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด JSON ํŒŒ์ผ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: data_files = { "train": "drug-reviews-train.jsonl", "validation": "drug-reviews-validation.jsonl", "test": "drug-reviews-test.jsonl", } drug_dataset_reloaded = load_dataset("json", data_files=data_files) Datasets๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ „์ฒ˜๋ฆฌ ๋ฐ ํ›„์ฒ˜๋ฆฌ ๊ธฐ๋ฒ• ๋“ฑ์„ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ ํ•™์Šต์„ ์œ„ํ•œ ์ •๋ฆฌ๋œ ๋ฐ์ดํ„ฐ ์…‹์ด ์žˆ์œผ๋ฏ€๋กœ ์‹œ๋„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์•„์ด๋””์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: 3์žฅ์˜ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์•ฝ๋ฌผ ๋ฆฌ๋ทฐ(drug review)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ™˜์ž ์ƒํƒœ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1์žฅ์˜ ์š”์•ฝ(summarization) ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ๋ทฐ ์š”์•ฝ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ, ์šฐ๋ฆฌ๋Š” Datasets๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜๋“œ์›จ์–ด์˜ ์ œ์•ฝ ์—†์ด ๊ทœ๋ชจ๊ฐ€ ํฐ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! 3. Datasets๊ฐ€ ๋น…๋ฐ์ดํ„ฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค! ํŠนํžˆ BERT ๋˜๋Š” GPT-2์™€ ๊ฐ™์€ ํŠธ๋žœ์Šคํฌ๋จธ(transformers)๋ฅผ ๋ฐ‘๋ฐ”๋‹ฅ๋ถ€ํ„ฐ ์‚ฌ์ „ ํ•™์Šต(pretraining) ํ•  ๊ณ„ํš์ด๋ผ๋ฉด, ๊ธฐ๊ฐ€๋ฐ”์ดํŠธ ๊ทœ๋ชจ์˜ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ์ด ๋“œ๋ฌธ ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์กฐ์ฐจ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, GPT-2๋ฅผ ์‚ฌ์ „ ํ•™์Šต(pretraining) ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” WebText ์ฝ”ํผ์Šค๋Š” 8๋ฐฑ๋งŒ ๊ฐœ ์ด์ƒ์˜ ๋ฌธ์„œ์™€ 40GB์˜ ํ…์ŠคํŠธ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ ์…‹์„ ๋…ธํŠธ๋ถ์˜ RAM์— ๋กœ๋“œํ•˜๋ฉด ๋…ธํŠธ๋ถ์ด ์‹ฌ์žฅ๋งˆ๋น„๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๋‹คํ–‰ํžˆ๋„, Datasets๋Š” ์ด๋Ÿฌํ•œ ํ•œ๊ณ„๋ฅผ ๊ทน๋ณตํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋งคํ•‘๋œ(memory-mapped) ํŒŒ์ผ๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ , ๋ง๋ญ‰์น˜์˜ ๊ฐ ํ•ญ๋ชฉ๋“ค์„ ์ŠคํŠธ๋ฆฌ๋ฐ ํ•˜์—ฌ ํ•˜๋“œ ๋””์Šคํฌ ์ œํ•œ์—์„œ ์šฐ๋ฆฌ๋ฅผ ํ•ด๋ฐฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” Pile๋กœ ์•Œ๋ ค์ง„ 825GB ๊ทœ๋ชจ์˜ ๊ฑฐ๋Œ€ํ•œ ๋ง๋ญ‰์น˜๋ฅผ ์ด์šฉํ•˜์—ฌ, ์œ„์—์„œ ์„ค๋ช…ํ•œ Datasets์˜ ๊ธฐ๋Šฅ๋“ค์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹œ์ž‘ํ•ด ๋ด…์‹œ๋‹ค! Pile์ด ๋ฌด์—‡์ธ๊ฐ€์š”? Pile์€ EleutherAI๊ฐ€ ๋Œ€๊ทœ๋ชจ ์–ธ์–ด ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“  ์˜์–ด ํ…์ŠคํŠธ ๋ง๋ญ‰์น˜์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ํ•™์ˆ  ๋…ผ๋ฌธ(scientific articles), GitHub ์ฝ”๋“œ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ๋ฐ ํ•„ํ„ฐ๋ง ๋œ ์›น ํ…์ŠคํŠธ์— ์ด๋ฅด๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์…‹์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ๋ฐ์ดํ„ฐ๋Š” 14GB ์ฒญํฌ๋กœ ์ œ๊ณต๋˜๋ฉฐ ๊ฐœ๋ณ„์ ์ธ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๊ฐ๊ฐ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € 1,500๋งŒ ๊ฑด์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ƒ์˜ํ•™ ์ถœํŒ๋ฌผ ์ดˆ๋ก ๋ชจ์Œ์ธ PubMed Abstracts ๋ฐ์ดํ„ฐ ์…‹์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์€ JSON Lines<NAME>์ด๊ณ  zstandard ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์••์ถ•๋˜๋ฏ€๋กœ ๋จผ์ € ๋‹ค์Œ์„ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: !pip install zstandard # ๋งŒ์ผ ์„ค์น˜๊ฐ€ ๋ฐ˜์˜์ด ์ œ๋Œ€๋กœ ์•ˆ๋œ๋‹ค๋ฉด, ๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•˜๊ณ  ์ปค๋„์„ ๋‹ค์‹œ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # import sys # !{sys.executable} -m pip install zstandard ๋‹ค์Œ์œผ๋กœ 5.2์—์„œ ๋ฐฐ์šด ์›๊ฒฉ ํŒŒ์ผ์— ๋Œ€ํ•œ ๋กœ๋”ฉ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_dataset # ์•„๋ž˜ ์ฝ”๋“œ๋Š” ์‹คํ–‰ํ•˜๋Š”๋ฐ ๋ช‡ ๋ถ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค. data_files = "https://the-eye.eu/public/AI/pile_preliminary_components/PUBMED_title_abstracts_2019_baseline.jsonl.zst" pubmed_dataset = load_dataset("json", data_files=data_files, split="train") pubmed_dataset ๋ฐ์ดํ„ฐ ์„ธํŠธ์— 15,518,009๊ฐœ์˜ ํ–‰๊ณผ 2๊ฐœ์˜ ์—ด์ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๋ง ๋งŽ์€ ์–‘์ž…๋‹ˆ๋‹ค! ๊ธฐ๋ณธ์ ์œผ๋กœ Datasets๋Š” ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ํŒŒ์ผ์˜ ์••์ถ•์„ ํ’‰๋‹ˆ๋‹ค. ํ•˜๋“œ๋””์Šคํฌ ๊ณต๊ฐ„์„ ์ ˆ์•ฝํ•˜๋ ค๋ฉด load_dataset()์˜ download_config ์ธ์ˆ˜์— DownloadConfig(delete_extracted=True)๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์˜ ๋‚ด์šฉ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: pubmed_dataset[0] ์ข‹์Šต๋‹ˆ๋‹ค. ์˜ํ•™ ๋…ผ๋ฌธ์˜ ์ดˆ๋ก์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด์ œ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ RAM์˜ ์–‘์„ ๋ด…์‹œ๋‹ค! ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘(memory-mapping)์˜ ๋งˆ๋ฒ• Python์—์„œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ธก์ •ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด pip๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” psutil ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: !pip install psutil ๋‹ค์Œ๊ณผ ๊ฐ™์ด, ํ˜„์žฌ ํ”„๋กœ์„ธ์Šค์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” Process ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค: import psutil # Process.memory_info๋Š” ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ํ‘œ์‹œ๋˜๋ฏ€๋กœ ์ด๋ฅผ ๋ฉ”๊ฐ€ ๋ฐ”์ดํŠธ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. print(f"RAM used: {psutil.Process().memory_info().rss / (1024 * 1024):.2f} MB") ์—ฌ๊ธฐ์—์„œ rss ์†์„ฑ์€ ํ”„๋กœ์„ธ์Šค๊ฐ€ RAM์—์„œ ์ฐจ์ง€ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋น„์œจ์ธ resident set size(์ƒ์ฃผ ์„ธํŠธ ํฌ๊ธฐ)๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ธก์ • ๊ณผ์ •์—์„œ Python ์ธํ„ฐํ”„๋ฆฌํ„ฐ์™€ ์šฐ๋ฆฌ๊ฐ€ ๋กœ๋“œํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์–‘๋„ ํฌํ•จ๋˜๋ฏ€๋กœ ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ์–‘์€ ์•ฝ๊ฐ„ ๋” ์ ์Šต๋‹ˆ๋‹ค. ๋น„๊ต๋ฅผ ์œ„ํ•ด dataset_size ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์ด ๋””์Šคํฌ์—์„œ ์–ด๋Š ์ •๋„ ํฌ๊ธฐ์ธ์ง€ ๋ด…์‹œ๋‹ค. ๊ฒฐ๊ณผ๋Š” ์ด์ „๊ณผ ๊ฐ™์ด ๋ฐ”์ดํŠธ๋กœ ํ‘œ์‹œ๋˜๋ฏ€๋กœ ์ˆ˜๋™์œผ๋กœ ๊ธฐ๊ฐ€๋ฐ”์ดํŠธ๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: print(f"Number of files in dataset : {pubmed_dataset.dataset_size}") size_gb = pubmed_dataset.dataset_size / (1024 ** 3) print(f"Dataset size (cache file) : {size_gb:.2f} GB") ์ข‹์Šต๋‹ˆ๋‹ค. ๊ฑฐ์˜ 20GB ํฌ๊ธฐ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ํ›จ์”ฌ ์ ์€ RAM์œผ๋กœ ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๊ณ  ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! Pandas์— ์ต์ˆ™ํ•˜๋‹ค๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ ์…‹ ํฌ๊ธฐ๋ณด๋‹ค 5~10๋ฐฐ ๋งŽ์€ RAM์ด ํ•„์š”ํ•˜๋‹ค๋Š” Wes Kinney์˜ ์œ ๋ช…ํ•œ ๊ฒฝํ—˜ ๋ฒ•์น™(rule of thumb) ๋•Œ๋ฌธ์— ์ด ๊ฒฐ๊ณผ๊ฐ€ ๋†€๋ผ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด Datasets๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ• ๊นŒ์š”? Datasets๋Š” ๊ฐ ๋ฐ์ดํ„ฐ ์…‹์„ ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘๋œ ํŒŒ์ผ(memory-mapped file)๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ์€ RAM๊ณผ ํŒŒ์ผ ์‹œ์Šคํ…œ ์Šคํ† ๋ฆฌ์ง€ ๊ฐ„์˜ ๋งคํ•‘์„ ์ œ๊ณตํ•˜์—ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ฐ์ดํ„ฐ ์…‹์„ ๋ฉ”๋ชจ๋ฆฌ์— ์™„์ „ํžˆ ๋กœ๋“œํ•  ํ•„์š” ์—†์ด ๊ฐ ์š”์†Œ์— ์•ก์„ธ์Šคํ•˜๊ณ  ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ๋งคํ•‘๋œ ํŒŒ์ผ(memory-mapped file)์€ ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค์—์„œ ๊ณต์œ ๋  ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ ๋ฐ์ดํ„ฐ ์…‹์„ ์ด๋™ํ•˜๊ฑฐ๋‚˜ ๋ณต์‚ฌํ•  ํ•„์š” ์—†์ด Dataset.map()๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ๋ณ‘๋ ฌํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ, ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์€ ๋ชจ๋‘ Apache Arrow ๋ฉ”๋ชจ๋ฆฌ<NAME> ๋ฐ pyarrow ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์˜ํ•ด ๊ตฌํ˜„๋˜์–ด ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ์ฒ˜๋ฆฌ๋ฅผ ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Apache Arrow ๋ฐ Pandas์™€์˜ ๋น„๊ต์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ Dejan Simic์˜ ๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๋ฌผ์„ ํ™•์ธํ•˜์„ธ์š”. ์‹ค์ œ๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด PubMed Abstracts ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ ์‹คํ–‰ ์†๋„ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: # (2022-05-25) ์ด ์ฝ”๋“œ์—์„œ ์‹คํ–‰ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์œ ๋‹ˆ์ฝ”๋“œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š”๋ฐ ์•„๋ฌด๋ž˜๋„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž์ฒด ์˜ค๋ฅ˜์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ณ„์† ์ฐพ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import timeit code_snippet = """batch_size = 1000 for idx in range(0, len(pubmed_dataset), batch_size): _ = pubmed_dataset[idx:idx + batch_size] """ time = timeit.timeit(stmt=code_snippet, number=1, globals=globals()) print( f"Iterated over {len(pubmed_dataset)} examples (about {size_gb:.1f} GB in " f"{time:.1f}s, i.e. {size_gb/time:.3f} GB/s" ) ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” Python์˜ timeit ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ code_snippet์˜ ์‹คํ–‰ ์‹œ๊ฐ„์„ ์ธก์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ˆ˜์‹ญ GB/s์—์„œ ์ˆ˜ GB/s์˜ ์†๋„๋กœ ๋ฐ์ดํ„ฐ ์…‹์„ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋Œ€๋ถ€๋ถ„์˜ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ ํ›Œ๋ฅญํ•˜๊ฒŒ ์ž‘๋™ํ•˜์ง€๋งŒ ๋•Œ๋กœ๋Š” ๋…ธํŠธ๋ถ์˜ ํ•˜๋“œ ๋“œ๋ผ์ด๋ธŒ์— ์ €์žฅํ•˜๊ธฐ์—๋„ ๋„ˆ๋ฌด ํฐ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ์ž‘์—…ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Pile ์ „์ฒด๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๋ ค๊ณ  ํ•˜๋ฉด 825GB์˜ ์—ฌ์œ  ๋””์Šคํฌ ๊ณต๊ฐ„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค! ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Datasets๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•  ํ•„์š” ์—†์ด ์ฆ‰์‹œ ์š”์†Œ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ์ŠคํŠธ๋ฆฌ๋ฐ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ ์…‹ (Streaming datasets) ๋ฐ์ดํ„ฐ ์…‹ ์ŠคํŠธ๋ฆฌ๋ฐ์„ ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด streaming=True ์ธ์ˆ˜๋ฅผ load_dataset() ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ŠคํŠธ๋ฆฌ๋ฐ ๋ชจ๋“œ์—์„œ PubMed Abstracts ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์‹œ ๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: pubmed_dataset_streamed = load_dataset( "json", data_files=data_files, split="train", streaming=True ) ์šฐ๋ฆฌ์—๊ฒŒ ์ด๋ฏธ ์นœ์ˆ™ํ•œ Dataset ๋Œ€์‹  streaming=True๋กœ ๋ฐ˜ํ™˜๋œ ๊ฐ์ฒด๋Š” IterableDataset์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด IterableDataset์˜ ์š”์†Œ์— ์•ก์„ธ์Šคํ•˜๋ ค๋ฉด ๋ฐ˜๋ณตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ŠคํŠธ๋ฆฌ๋ฐ ๋œ ๋ฐ์ดํ„ฐ ์…‹์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: next(iter(pubmed_dataset_streamed)) ์ŠคํŠธ๋ฆฌ๋ฐ ๋œ ๋ฐ์ดํ„ฐ ์…‹์˜ ์š”์†Œ๋Š” IterableDataset.map()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ํ•™์Šต ๊ณผ์ •์—์„œ ์ž…๋ ฅ์„ ํ† ํฐ ํ™”ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ 3์žฅ์—์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ํ† ํฐํ™”ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•๊ณผ ๋™์ผํ•˜์ง€๋งŒ ์ถœ๋ ฅ์ด ํ•˜๋‚˜์”ฉ ๋ฐ˜ํ™˜๋œ๋‹ค๋Š” ์ ๋งŒ ๋‹ค๋ฆ…๋‹ˆ๋‹ค: from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased") tokenized_dataset = pubmed_dataset_streamed.map(lambda x: tokenizer(x["text"])) next(iter(tokenized_dataset)) ์ŠคํŠธ๋ฆฌ๋ฐ์œผ๋กœ ํ† ํฐํ™” ์†๋„๋ฅผ ๋†’์ด๋ ค๋ฉด ์ด์ „ ์„น์…˜์—์„œ ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ batched=True๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋ฐฐ์น˜(batch) ๋ณ„๋กœ ์˜ˆ์ œ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 1,000์ด๋ฉฐ batch_size ์ธ์ˆ˜๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. IterableDataset.shuffle()์„ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆฌ๋ฐ ๋œ ๋ฐ์ดํ„ฐ ์…‹์„ ์…”ํ”Œ๋งํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ Dataset.shuffle()๊ณผ ๋‹ฌ๋ฆฌ ์‚ฌ์ „ ์ •์˜๋œ buffer_size ๊ฐœ์˜ ์š”์†Œ๋“ค๋งŒ ์…”ํ”Œ๋งํ•ฉ๋‹ˆ๋‹ค: shuffled_dataset = pubmed_dataset_streamed.shuffle(buffer_size=10000, seed=42) next(iter(shuffled_dataset)) ์œ„ ์˜ˆ์—์„œ๋Š” ๋ฒ„ํผ์˜ ์ฒ˜์Œ 10,000๊ฐœ ์˜ˆ์ œ์—์„œ ์ž„์˜๋กœ ์˜ˆ์ œ๋ฅผ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ผ๋‹จ ์˜ˆ์ œ์— ์ ‘๊ทผํ•˜๋ฉด ๋ฒ„ํผ์˜ ํ•ด๋‹น ์ง€์ ์ด ๋ง๋ญ‰์น˜์˜ ๋‹ค์Œ ์˜ˆ์ œ๋กœ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค (์ฆ‰, ์œ„์˜ ๊ฒฝ์šฐ 10,001๋ฒˆ์งธ ์˜ˆ์ œ). Dataset.select()์™€ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•˜๋Š” IterableDataset.take() ๋ฐ IterableDataset.skip() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆฌ๋ฐ ๋œ ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์š”์†Œ๋ฅผ ์„ ํƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, PubMed Abstracts ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ฒ˜์Œ 5๊ฐœ์˜ ์˜ˆ์ œ๋ฅผ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: dataset_head = pubmed_dataset_streamed.take(5) list(dataset_head) ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, IterableDataset.skip() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์…”ํ”Œ๋ง๋œ ๋ฐ์ดํ„ฐ ์…‹์—์„œ ํ•™์Šต ๋ฐ ๊ฒ€์ฆ ๋ถ„ํ• ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: # ์ฒ˜์Œ 1000๊ฐœ์˜ ์˜ˆ์ œ๋ฅผ ์Šคํ‚ตํ•˜๊ณ  ๋‚˜๋จธ์ง€๋ฅผ ํ•™์Šต ์ง‘ํ•ฉ์œผ๋กœ ํฌํ•จ์‹œํ‚จ๋‹ค. train_dataset = shuffled_dataset.skip(1000) # ์ฒ˜์Œ 1000๊ฐœ์˜ ์˜ˆ์ œ๋ฅผ ๊ฒ€์ฆ ์ง‘ํ•ฉ์œผ๋กœ ๊ตฌ์„ฑํ•œ๋‹ค. validation_dataset = shuffled_dataset.take(1000) ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๊ฒฐํ•ฉํ•˜์—ฌ ๋‹จ์ผ ์ฝ”ํผ์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•˜๋‚˜์˜ ์ผ๋ฐ˜์ ์ธ ์‘์šฉ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•จ์œผ๋กœ์จ ๋ฐ์ดํ„ฐ ์…‹ ์ŠคํŠธ๋ฆฌ๋ฐ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋งˆ๋ฌด๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Datasets๋Š” IterableDataset ๊ฐ์ฒด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹จ์ผ IterableDataset์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” interleave_datasets() ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ƒˆ ๋ฐ์ดํ„ฐ ์…‹์˜ ์š”์†Œ๋Š” ์†Œ์Šค ์˜ˆ์ œ๋ฅผ ๋ฒˆ๊ฐˆ์•„ ๊ฐ€๋ฉฐ ์–ป์Šต๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ ์…‹์„ ๊ฒฐํ•ฉํ•˜๋ ค๊ณ  ํ•  ๋•Œ ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฏธ๊ตญ ๋ฒ•์›(US courts)์˜ ๋ฒ•์  ์˜๊ฒฌ(legal opinions)์ด ๋‹ด๊ธด 51GB ๋ฐ์ดํ„ฐ ์…‹์ธ Pile์˜ FreeLaw ํ•˜์œ„ ์„ธํŠธ(subsets)๋“ค์„ ์ŠคํŠธ๋ฆฌ๋ฐ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: law_dataset_streamed = load_dataset( "json", data_files="https://the-eye.eu/public/AI/pile_preliminary_components/FreeLaw_Opinions.jsonl.zst", split="train", streaming=True, ) next(iter(law_dataset_streamed)) ์ด ๋ฐ์ดํ„ฐ ์…‹์€ ๋Œ€๋ถ€๋ถ„์˜ ๋žฉํ†ฑ์—์„œ RAM์— ๋ถ€๋‹ด์„ ์ฃผ๊ธฐ์— ์ถฉ๋ถ„ํ•˜์ง€๋งŒ ์–ด๋ ค์›€ ์—†์ด ๋กœ๋“œํ•˜๊ณ  ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ด์ œ FreeLaw ๋ฐ PubMed Abstracts ๋ฐ์ดํ„ฐ ์…‹์˜ ์˜ˆ์ œ๋ฅผ interleave_datasets() ํ•จ์ˆ˜์™€ ๊ฒฐํ•ฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: from itertools import islice from datasets import interleave_datasets combined_dataset = interleave_datasets([pubmed_dataset_streamed, law_dataset_streamed]) list(islice(combined_dataset, 2)) ์—ฌ๊ธฐ์—์„œ ์šฐ๋ฆฌ๋Š” Python์˜ itertools ๋ชจ๋“ˆ์—์„œ islice() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐํ•ฉ๋œ ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ฒ˜์Œ ๋‘ ์˜ˆ์ œ๋ฅผ ์„ ํƒํ–ˆ์œผ๋ฉฐ ๋‘ ์†Œ์Šค ๋ฐ์ดํ„ฐ ์…‹ ๊ฐ๊ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์™€ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ Pile์„ 825GB ์ „์ฒด๋กœ ์ŠคํŠธ๋ฆฌ๋ฐ ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ค€๋น„๋œ ๋ชจ๋“  ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: base_url = "https://the-eye.eu/public/AI/pile/" data_files = { "train": [base_url + "train/" + f"{idx:02d}.jsonl.zst" for idx in range(30)], "validation": base_url + "val.jsonl.zst", "test": base_url + "test.jsonl.zst", } pile_dataset = load_dataset("json", data_files=data_files, streaming=True) next(iter(pile_dataset["train"])) mc4 ๋˜๋Š” oscar์™€ ๊ฐ™์€ ๋Œ€๊ทœ๋ชจ Common Crawl ๋ง๋ญ‰์น˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ํƒํ•œ ๊ตญ๊ฐ€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด ๋น„์œจ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ŠคํŠธ๋ฆฌ๋ฐ ๋‹ค๊ตญ์–ด ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด, ์Šค์œ„์Šค์˜ 4๊ฐœ ๊ตญ์–ด๋Š” ๋…์ผ์–ด, ํ”„๋ž‘์Šค์–ด, ์ดํƒˆ๋ฆฌ์•„์–ด ๋ฐ ๋กœ๋งŒ ์‹œ์–ด(Romansh)์ด๋ฏ€๋กœ ์‚ฌ์šฉ ๋น„์œจ์— ๋”ฐ๋ผ Oscar ํ•˜์œ„ ์ง‘ํ•ฉ์„ ์ƒ˜ํ”Œ๋งํ•˜์—ฌ ์Šค์œ„์Šค ๋ง๋ญ‰์น˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์–ด๋– ํ•œ ๋ชจ์–‘๊ณผ ํฌ๊ธฐ์˜ ๋ฐ์ดํ„ฐ ์…‹์ด๋“  ์ด๋ฅผ ๋กœ๋“œํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“  ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์•„๋ฌด๋ฆฌ ์šด์ด ์ข‹๋”๋ผ๋„ NLP ์—ฌ์ •์—์„œ ๋‹น๋ฉดํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‹ค์ œ๋กœ ๋ณธ์ธ์˜ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ์‹œ์ ์ด ์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๋‹ค์Œ ์„น์…˜์˜ ์ฃผ์ œ์ž…๋‹ˆ๋‹ค! 4. ์ž์‹ ๋งŒ์˜ ๋ฐ์ดํ„ฐ ์…‹ ๋งŒ๋“ค๊ธฐ NLP ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์ถ•ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ์…‹์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” ์ง์ ‘ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๋ฒ„๊ทธ ๋˜๋Š” ํŠน์ง•์„ ์ถ”์ ํ•˜๋Š”๋ฐ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” GitHub issues ๋ง๋ญ‰์น˜๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด ๋ง๋ญ‰์น˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ๋ฏธํ•ด๊ฒฐ ์ด์Šˆ(Open issues) ๋˜๋Š” ํ’€ ๋ฆฌํ€˜์ŠคํŠธ(pull requests)๋ฅผ ์ข…๋ฃŒํ•˜๋Š” ๋ฐ ์–ผ๋งˆ๋‚˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ. ๋ฌธ์ œ์— ๋Œ€ํ•œ ์„ค๋ช…(issue's description)์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํ˜•ํƒœ์˜ ์ด์Šˆ์— ํƒœ๊ทธ("bug", "enhancement", ๋˜๋Š” "question")๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์ค‘ ๋ ˆ์ด๋ธ” ๋ถ„๋ฅ˜๊ธฐ(multilabel classifier) ํ•™์Šต ์‚ฌ์šฉ์ž์˜ ์งˆ์˜์™€ ์ผ์น˜ํ•˜๋Š” ์ด์Šˆ๋“ค์„ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ์‹œ๋งจํ‹ฑ ๊ฒ€์ƒ‰ ์—”์ง„(semantic search engine) ๋งŒ๋“ค๊ธฐ ์—ฌ๊ธฐ์—์„œ๋Š” ๋ง๋ญ‰์น˜์˜ ์‹ ๊ทœ ๊ตฌ์ถ•์— ์ดˆ์ ์„ ๋งž์ถ”๊ณ  ๋‹ค์Œ ์„น์…˜์—์„œ๋Š” ์‹œ๋งจํ‹ฑ ๊ฒ€์ƒ‰ ์—”์ง„์„ ๋‹ค๋ฃฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํŠนํžˆ, ์ธ๊ธฐ ์žˆ๋Š” ์˜คํ”ˆ ์†Œ์Šค ํ”„๋กœ์ ํŠธ์ธ Datasets์™€ ๊ด€๋ จ๋œ GitHub ์ด์Šˆ ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ•ด์„œ ๋ง๋ญ‰์น˜๋ฅผ ๊ตฌ์ถ•ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ด๋Ÿฌํ•œ ์ด์Šˆ๋“ค์— ํฌํ•จ๋œ ์ •๋ณด๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ Repository์˜ ์ด์Šˆ ํƒญ(Issues tab)์œผ๋กœ ์ด๋™ํ•˜์—ฌ Datasets ํ”„๋กœ์ ํŠธ์˜ ๋ชจ๋“  ์ด์Šˆ๋“ค์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์Šคํฌ๋ฆฐ์ˆ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ์ด ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•  ๋‹น์‹œ 331๊ฐœ์˜ ๋ฏธํ•ด๊ฒฐ ์ด์Šˆ๋“ค(open issues)๊ณผ 668๊ฐœ์˜ ํ•ด๊ฒฐ๋œ ์ด์Šˆ๋“ค(closed issues)์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค: ์ด๋Ÿฌํ•œ ์ด์Šˆ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ํด๋ฆญํ•˜๋ฉด ์ œ๋ชฉ(title), ์„ค๋ช…(description) ๋ฐ ์ด์Šˆ๋ฅผ ํŠน์ง•์ง“๋Š” ๋ ˆ์ด๋ธ” ์ง‘ํ•ฉ์ด ํฌํ•จ๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์Šคํฌ๋ฆฐ์ˆ์— ์˜ˆ๊ฐ€ ๋‚˜์™€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ์ด์Šˆ๋“ค์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด GitHub REST API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Issues endpoint๋ฅผ ํด๋ง ํ•ฉ๋‹ˆ๋‹ค. ์ด endpoint๋Š” ์ œ๋ชฉ๊ณผ ์„ค๋ช…์€ ๋ฌผ๋ก  ์ด์Šˆ ์ƒํƒœ์— ๋Œ€ํ•œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋งŽ์€ ํ•„๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฐœ์ฒด๋“ค๋กœ ๊ตฌ์„ฑ๋œ JSON ๊ฐœ์ฒด(objects) ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด์Šˆ(issues)๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ํŽธ๋ฆฌํ•œ ๋ฐฉ๋ฒ•์€ Python์—์„œ HTTP ์š”์ฒญ์„ ๋งŒ๋“œ๋Š” ํ‘œ์ค€ ๋ฐฉ๋ฒ•์ธ requests ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์„ ์‹คํ–‰ํ•˜์—ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: !pip install requests ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜๋ฉด requests.get() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ด์Šˆ ์—”๋“œ ํฌ์ธํŠธ(Issues endpoint)์— GET ์š”์ฒญ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ์ฒซ ํŽ˜์ด์ง€์˜ ์ฒซ ๋ฒˆ์งธ ์ด์Šˆ๋ฅผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: import requests url = "https://api.github.com/repos/huggingface/datasets/issues? page=1&per_page=1" response = requests.get(url) ์‘๋‹ต ๊ฐ์ฒด์—๋Š” HTTP ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•˜์—ฌ ์š”์ฒญ์— ๋Œ€ํ•œ ์œ ์šฉํ•œ ์ •๋ณด๊ฐ€ ๋งŽ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค: response.status_code ์—ฌ๊ธฐ์„œ 200 ์ƒํƒœ๋Š” ์š”์ฒญ์ด ์„ฑ๊ณตํ–ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค(์—ฌ๊ธฐ์—์„œ HTTP ์ƒํƒœ ์ฝ”๋“œ ๋ชฉ๋ก์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ). ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ๊ด€์‹ฌ ์žˆ๋Š” ๊ฒƒ์€ ๋ฐ”์ดํŠธ, ๋ฌธ์ž์—ด ๋˜๋Š” JSON๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ<NAME>์œผ๋กœ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” payload์ž…๋‹ˆ๋‹ค. ์ด์Šˆ๊ฐ€ JSON<NAME>์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŽ˜์ด๋กœ๋“œ(payload)๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: response.json() ์™€, ์ •๋ณด๊ฐ€ ๋งŽ๋„ค์š”! ์ด์Šˆ๋ฅผ ์„ค๋ช…ํ•˜๋Š” ์ œ๋ชฉ(title), ๋ณธ๋ฌธ(body) ๋ฐ ๋ฒˆํ˜ธ(number)์™€ ๊ฐ™์€ ์œ ์šฉํ•œ ํ•„๋“œ์™€ ํ•ด๋‹น ์ด์Šˆ๋ฅผ ์ œ๊ธฐํ•œ GitHub ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. GitHub ๋ฌธ์„œ์— ์„ค๋ช…๋œ ๋Œ€๋กœ ์ธ์ฆ๋˜์ง€ ์•Š์€ ์š”์ฒญ์€ ์‹œ๊ฐ„๋‹น 60๊ฐœ ์š”์ฒญ์œผ๋กœ ์ œํ•œ๋ฉ๋‹ˆ๋‹ค. per_page ์ฟผ๋ฆฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋Š˜๋ ค ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ์š”์ฒญ์˜ ์ด์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด์Šˆ๊ฐ€ ์ˆ˜์ฒœ ๊ฐœ ์ด์ƒ์ธ ์ €์žฅ์†Œ์—์„œ๋Š” ์—ฌ์ „ํžˆ ๋น„์œจ ์ œํ•œ์— ๋„๋‹ฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ ๋Œ€์‹  ์‹œ๊ฐ„๋‹น 5,000๊ฐœ ์š”์ฒญ์œผ๋กœ ์†๋„ ์ œํ•œ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋„๋ก ๊ฐœ์ธ ์•ก์„ธ์Šค ํ† ํฐ(personal access token) ์ƒ์„ฑ์— ๋Œ€ํ•œ GitHub์˜ ์ง€์นจ์„ ๋”ฐ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐ์ด ์žˆ์œผ๋ฉด ์š”์ฒญ ํ—ค๋”(request header)์˜ ์ผ๋ถ€๋กœ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: GITHUB_TOKEN = "ghp_..." ## ๋ณธ์ธ์˜ personal access token์„ ์ง€์ •ํ•˜์„ธ์š”. headers = {"Authorization": f"token {GITHUB_TOKEN}"} ์ด์ œ ์•ก์„ธ์Šค ํ† ํฐ์ด ์žˆ์œผ๋ฏ€๋กœ GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ๋ชจ๋“  ์ด์Šˆ๋“ค์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: import time import math from pathlib import Path import pandas as pd from tqdm.notebook import tqdm def fetch_issues( owner="huggingface", repo="datasets", num_issues=10000, rate_limit=5000, issues_path=Path(".") ): if not issues_path.is_dir(): issues_path.mkdir(exist_ok=True) batch = [] all_issues = [] per_page = 100 # ํŽ˜์ด์ง€๋‹น ๋ฆฌํ„ด ๋ฐ›๋Š” ์ด์Šˆ์˜ ๊ฐœ์ˆ˜ num_pages = math.ceil(num_issues / per_page) base_url = "https://api.github.com/repos" for page in tqdm(range(num_pages)): # state=all๋กœ ์งˆ์˜ํ•˜์—ฌ ๋ฏธํ•ด๊ฒฐ, ํ•ด๊ฒฐ ์ด์Šˆ๋“ค์„ ๋ชจ๋‘ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค. query = f"issues?page={page}&per_page={per_page}&state=all" issues = requests.get(f"{base_url}/{owner}/{repo}/{query}", headers=headers) batch.extend(issues.json()) if len(batch) > rate_limit and len(all_issues) < num_issues: all_issues.extend(batch) batch = [] # ๋‹ค์Œ ์ฃผ๊ธฐ๋ฅผ ์œ„ํ•ด์„œ batch๋ฅผ ๋น„์šด๋‹ค. print(f"Reached GitHub rate limit. Sleeping for one hour ...") time.sleep(60 * 60 + 1) all_issues.extend(batch) df = pd.DataFrame.from_records(all_issues) df.to_json(f"{issues_path}/{repo}-issues.jsonl", orient="records", lines=True) print(f"Downloaded all the issues for {repo}! Dataset stored at {issues_path}/{repo}-issues.jsonl") ์ด์ œ fetch_issues()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์‹œ๊ฐ„๋‹น ์š”์ฒญ ์ˆ˜์— ๋Œ€ํ•œ GitHub์˜ ์ œํ•œ์„ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋„๋ก ๋ชจ๋“  ์ด์Šˆ๋ฅผ ์ผ๊ด„์ ์œผ๋กœ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” repository_name-issues.jsonl ํŒŒ์ผ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ ํ–‰์€ ์ด์Šˆ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” JSON ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ Datasets์˜ ๋ชจ๋“  ์ด์Šˆ๋“ค์„ ํŒŒ์•…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: # ์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ ์ƒํƒœ์— ๋”ฐ๋ผ, ๋ช‡ ๋ถ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. fetch_issues() ์ด์Šˆ๋“ค์ด ๋ชจ๋‘ ๋‹ค์šด๋กœ๋“œ๋˜๋ฉด 5.2์—์„œ ์‚ดํŽด๋ณธ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ์ปฌ์—์„œ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_dataset issues_dataset = load_dataset("json", data_files="datasets-issues.jsonl", split="train") issues_dataset ์ข‹์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฐ๋ฐ Datasets ๋ฆฌํฌ์ง€ํ„ฐ๋ฆฌ์˜ Issues ํƒญ์— ์ด 1,000๊ฐœ ์ •๋„์˜ ์ด์Šˆ๋“ค๋งŒ ํ‘œ์‹œ๋˜๋Š”๋ฐ ์™œ ์ˆ˜์ฒœ ๊ฐœ์˜ ์ด์Šˆ๊ฐ€ ์žˆ๋Š” ๊ฑธ๊นŒ์š”? ๊ทธ๊ฒƒ์€ GitHub ๋ฌธ์„œ์— ์„ค๋ช…๋œ ๋Œ€๋กœ ๋ชจ๋“  pull ์š”์ฒญ๋„ ๋‹ค์šด๋กœ๋“œํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค: "GitHub์˜ REST API v3๋Š” ๋ชจ๋“  pull ์š”์ฒญ์„ ์ด์Šˆ๋“ค๋กœ ๊ฐ„์ฃผํ•˜์ง€๋งŒ ๋ชจ๋“  ์ด์Šˆ๊ฐ€ pull ์š”์ฒญ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ "Issues" endpoint๋Š” ์‘๋‹ต์—์„œ ์ด์Šˆ์™€ pull ์š”์ฒญ์„ ๋ชจ๋‘ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pull_request ํ‚ค๋กœ pull ์š”์ฒญ์„ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. "Issues" endpoint์—์„œ ๋ฐ˜ํ™˜๋œ pull ์š”์ฒญ์˜ ID๋Š” ์ด์Šˆ ID๊ฐ€ ๋ฉ๋‹ˆ๋‹ค." ์ด์Šˆ์™€ pull ์š”์ฒญ์˜ ๋‚ด์šฉ์ด ์ƒ๋‹นํžˆ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋“ค์„ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ์•ฝ๊ฐ„์˜ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ •์ œํ•˜๊ธฐ ์œ„์—์„œ ์ธ์šฉํ•œ GitHub ๋ฌธ์„œ ๋‚ด์šฉ์—์„œ๋Š” pull_request ์—ด(column)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด์Šˆ์™€ pull ์š”์ฒญ์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ์ฐจ์ด์ ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ๋ฌด์ž‘์œ„ ์ƒ˜ํ”Œ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 5.3์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ Dataset.shuffle()๊ณผ Dataset.select()๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์ž„์˜์˜ ์ƒ˜ํ”Œ ์ง‘ํ•ฉ์„ ๋งŒ๋“  ๋‹ค์Œ ๋‹ค์–‘ํ•œ URL์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋„๋ก html_url ๋ฐ pull_request ์—ด์„ zip์œผ๋กœ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค: sample = issues_dataset.shuffle(seed=777).select(range(3)) # URL๊ณผ pull request ์—”ํŠธ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. for url, pr in zip(sample["html_url"], sample["pull_request"]): print(f">> URL: {url}") print(f">> Pull request: {pr}\n") ์—ฌ๊ธฐ์—์„œ ๊ฐ pull ์š”์ฒญ์ด ๋‹ค์–‘ํ•œ URL๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋ฐ˜๋ฉด ์ผ๋ฐ˜ ์ด์Šˆ์—๋Š” None ํ•ญ๋ชฉ์ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ตฌ๋ถ„์„ ์‚ฌ์šฉํ•˜์—ฌ pull_request ํ•„๋“œ๊ฐ€ None ์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ์ƒˆ๋กœ์šด is_pull_request ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: issues_dataset = issues_dataset.map(lambda x: {"is_pull_request": False if x["pull_request"] is None else True}) ์ผ๋ถ€ ์—ด์„ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ์ถ”๊ฐ€๋กœ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ผ๋ฐ˜์ ์œผ๋กœ ์—ฌ๋Ÿฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ด ๋‹จ๊ณ„์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์…‹์„ ๊ฐ€๋Šฅํ•œ "์›๋ž˜(raw)" ์ƒํƒœ๋กœ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์„ Hugging Face Hub์— ํ‘ธ์‹œ ํ•˜๊ธฐ ์ „์— ๋ฐ์ดํ„ฐ ์…‹์—์„œ ๋ˆ„๋ฝ๋œ ํ•œ ๊ฐ€์ง€๋ฅผ ์ฒ˜๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๊ฐ ์ด์Šˆ ๋ฐ pull ์š”์ฒญ๊ณผ ๊ด€๋ จ๋œ ์ฃผ์„(comments)์ž…๋‹ˆ๋‹ค. ์ง์ž‘ํ–ˆ๊ฒ ์ง€๋งŒ GitHub REST API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜์—์„œ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์— ๋‚ด์šฉ ์ถ”๊ฐ€ ๋‹ค์Œ ์Šคํฌ๋ฆฐ์ˆ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, ์ด์Šˆ ๋˜๋Š” pull ์š”์ฒญ๊ณผ ๊ด€๋ จ๋œ ๋Œ“๊ธ€์€ ํ’๋ถ€ํ•œ ์ •๋ณด ์†Œ์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž ์ฟผ๋ฆฌ์— ์‘๋‹ตํ•˜๋Š” ๊ฒ€์ƒ‰ ์—”์ง„์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ๊ด€์‹ฌ์ด ์žˆ๋‹ค๋ฉด ์ด ์ •๋ณด๋Š” ๋งŽ์€ ๋„์›€์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. GitHub REST API๋Š” ์ด์Šˆ ๋ฒˆํ˜ธ์™€ ๊ด€๋ จ๋œ ๋ชจ๋“  ์ฃผ์„(comments)์„ ๋ฐ˜ํ™˜ํ•˜๋Š” Comments endpoint๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด endpoint๊ฐ€ ๋ฌด์—‡์„ ๋ฐ˜ํ™˜ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: issue_number = 2792 url = f"https://api.github.com/repos/huggingface/datasets/issues/{issue_number}/comments" response = requests.get(url, headers=headers) response.json() ์ฃผ์„(comment)์ด body ํ•„๋“œ์— ์ €์žฅ๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, response.json()์—์„œ ๊ฐ ์š”์†Œ์˜ body ๋‚ด์šฉ์„ ์„ ํƒํ•˜์—ฌ ์ด์Šˆ์™€ ๊ด€๋ จ๋œ ๋ชจ๋“  ์ฃผ์„(comments)์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def get_comments(issue_number): url = f"https://api.github.com/repos/huggingface/datasets/issues/{issue_number}/comments" response = requests.get(url, headers=headers) return[r["body"] for r in response.json()] get_comments(2792) ๊ฒฐ๊ณผ๊ฐ€ ๊ดœ์ฐฎ์•„ ๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด Dataset.map()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ฐ ์ด์Šˆ์— ์ƒˆ ์ฃผ์„(comments) ์—ด์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: # ์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ ์ƒํƒœ์— ๋”ฐ๋ผ ๋ช‡ ๋ถ„์ด ์†Œ์š”๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค... issues_with_comments_dataset = issues_dataset.map(lambda x: {"comments": get_comments(x["number"])}) ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” ์ฆ๊ฐ•๋œ ๋ฐ์ดํ„ฐ ์…‹(augmented dataset)์„ ์›์‹œ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ์ €์žฅํ•˜์—ฌ ๋‘˜ ๋‹ค Hub์— ํ‘ธ์‹œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. issues_with_comments_dataset.to_json("datasets-issues-with-comments.jsonl") Hugging Face Hub์— ๋ฐ์ดํ„ฐ ์…‹ ์—…๋กœ๋”ฉ ์ด์ œ ํ•„๋“œ๊ฐ€ ์ถ”๊ฐ€๋œ ๋ฐ์ดํ„ฐ ์…‹์„ ํ—ˆ๋ธŒ๋กœ ํ‘ธ์‹œ ํ•˜์—ฌ ์ปค๋ฎค๋‹ˆํ‹ฐ์™€ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๋ฐ์ดํ„ฐ ์…‹์„ ์—…๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด Hub ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Python API๋ฅผ ํ†ตํ•ด Hugging Face Hub์™€ ์ƒํ˜ธ ์ž‘์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Hub๋Š” Transformers์™€ ํ•จ๊ป˜ ์„ค์น˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, list_datasets() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ Hub์—์„œ ํ˜ธ์ŠคํŒ… ๋˜๋Š” ๋ชจ๋“  ๊ณต๊ฐœ ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from huggingface_hub import list_datasets all_datasets = list_datasets() print(f"Number of datasets on Hub: {len(all_datasets)}") print(all_datasets[0]) Hub์—๋Š” ํ˜„์žฌ ๊ฑฐ์˜ 1,500๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์…‹์ด ์žˆ๊ณ  list_datasets() ํ•จ์ˆ˜๋Š” ๊ฐ ๋ฐ์ดํ„ฐ ์…‹ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ๋Œ€ํ•œ ๋ช‡ ๊ฐ€์ง€ ๊ธฐ๋ณธ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋„ ์ œ๊ณตํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์„ ์œ„ํ•ด ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผ ํ•  ์ผ์€ Hub์— ์ƒˆ ๋ฐ์ดํ„ฐ ์…‹ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด ๋จผ์ € notebook_login() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Hugging Face Hub์— ๋กœ๊ทธ์ธํ•˜์—ฌ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ธ์ฆ ํ† ํฐ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค: from huggingface_hub import notebook_login notebook_login() ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‚ฌ์šฉ์ž ์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์ ฏ์ด ์ƒ์„ฑ๋˜๊ณ  API ํ† ํฐ์ด ~/.huggingface/token์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ํ„ฐ๋ฏธ๋„์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ CLI๋ฅผ ํ†ตํ•ด ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. huggingface-cli login ์ด ์ž‘์—…์„ ๋งˆ์น˜๋ฉด create_repo() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ๋ฐ์ดํ„ฐ ์…‹ ๋ฆฌํฌ์ง€ํ„ฐ๋ฆฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from huggingface_hub import create_repo repo_url = create_repo(name="datasets-github-issues", repo_type="dataset") repo_url ์ด ์˜ˆ์—์„œ๋Š” spasis ์‚ฌ์šฉ์ž ์ด๋ฆ„ ์•„๋ž˜์— github-issues๋ผ๋Š” ๋นˆ ๋ฐ์ดํ„ฐ ์…‹ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค(์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ์ž ์ด๋ฆ„์€ Hub ์‚ฌ์šฉ์ž ์ด๋ฆ„์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค!). ๋‹ค์Œ์œผ๋กœ ํ—ˆ๋ธŒ์—์„œ ๋กœ์ปฌ ๋จธ์‹ ์œผ๋กœ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋ณต์ œํ•˜๊ณ  ๋ฐ์ดํ„ฐ ์…‹ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Hub๋Š” ์ˆ˜๋งŽ์€ ๊ณตํ†ต Git ๋ช…๋ น์–ด๋“ค์„ ๋ž˜ํ•‘ ํ•˜๋Š” ํŽธ๋ฆฌํ•œ Repository ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๋ฏ€๋กœ ์›๊ฒฉ ์ €์žฅ์†Œ๋ฅผ ๋ณต์ œํ•˜๋ ค๋ฉด ๋ณต์ œํ•˜๋ ค๋Š” URL๊ณผ ๋กœ์ปฌ ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: from huggingface_hub import Repository repo = Repository(local_dir="datasets-github-issues", clone_from=repo_url) !cp datasets-issues-with-comments.jsonl datasets-github-issues/ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์–‘ํ•œ ํŒŒ์ผ ํ™•์žฅ์ž(์˜ˆ: .bin, .gz ๋ฐ. zip)๊ฐ€ Git LFS๋กœ ์ถ”์ ๋˜๋ฏ€๋กœ ๋™์ผํ•œ Git ์›Œํฌํ”Œ๋กœ ๋‚ด์—์„œ ๋Œ€์šฉ๋Ÿ‰ ํŒŒ์ผ์˜ ๋ฒ„์ „์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์žฅ์†Œ์˜. gitattributes ํŒŒ์ผ์—์„œ ์ถ”์ ๋œ ํŒŒ์ผ ํ™•์žฅ์ž ๋ชฉ๋ก์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชฉ๋ก์— JSON Lines<NAME>์„ ํฌํ•จํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: repo.lfs_track("*.jsonl") ๊ทธ๋Ÿฐ ๋‹ค์Œ Repository.push_to_hub()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ Hub๋กœ ํ‘ธ์‹œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. repo.push_to_hub() repo_url์— ํฌํ•จ๋œ URL๋กœ ์ด๋™ํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์…‹ ํŒŒ์ผ์ด ์—…๋กœ๋“œ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ๋ˆ„๊ตฌ๋‚˜ load_dataset()์— ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ID๋ฅผ ๊ฒฝ๋กœ ์ธ์ˆ˜๋กœ ์ œ๊ณตํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: remote_dataset = load_dataset("spasis/datasets-github-issues", split="train") remote_dataset ์ข‹์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์„ Hub๋กœ ํ‘ธ์‹œ ํ–ˆ์œผ๋ฉฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ํ•œ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ์ž‘์—…๋งŒ ๋‚จ์•˜์Šต๋‹ˆ๋‹ค. ๋ง๋ญ‰์น˜๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ์„ค๋ช…ํ•˜๊ณ  ์ปค๋ฎค๋‹ˆํ‹ฐ์— ์œ ์šฉํ•œ ๊ธฐํƒ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ ์…‹ ์นด๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. huggingface-cli์™€ ์œ ์šฉํ•œ Git ๊ธฐ๋Šฅ๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ํ„ฐ๋ฏธ๋„์—์„œ ์ง์ ‘ Hugging Face Hub์— ๋ฐ์ดํ„ฐ ์…‹์„ ์—…๋กœ๋“œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ Datasets ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ๋ฐ์ดํ„ฐ ์…‹ ์นด๋“œ ์ƒ์„ฑ ๋ฌธ์„œํ™”๊ฐ€ ์ž˜ ๋œ ๋ฐ์ดํ„ฐ ์…‹์€ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ์ดํ„ฐ ์…‹ ๊ฐ€ ์ž์‹ ์˜ ์ž‘์—…๊ณผ ๊ด€๋ จ์ด ์žˆ๋Š”์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ  ํ™œ์šฉ๊ณผ ๊ด€๋ จ๋œ ์ž ์žฌ์  ํŽธํ–ฅ์ด๋‚˜ ์œ„ํ—˜์„ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ถ€๊ฐ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค(๋ฏธ๋ž˜์˜ ์ž์‹ ์„ ํฌํ•จํ•˜์—ฌ)์—๊ฒŒ ๋” ์œ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Hugging Face Hub์—์„œ ์ด ์ •๋ณด๋Š” ๊ฐ ๋ฐ์ดํ„ฐ ์…‹ ์ €์žฅ์†Œ์˜ README.md ํŒŒ์ผ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ์„ ๋งŒ๋“ค๊ธฐ ์ „์— ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ๋‹จ๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: datasets-tagging ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ YAML<NAME>์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํƒœ๊ทธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํƒœ๊ทธ๋Š” Hugging Face Hub์˜ ๋‹ค์–‘ํ•œ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์— ์‚ฌ์šฉ๋˜๋ฉฐ ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ตฌ์„ฑ์›์ด ๋ฐ์ดํ„ฐ ์…‹์„ ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ์‚ฌ์šฉ์ž ์ •์˜ ๋ฐ์ดํ„ฐ ์…‹์„ ์ƒ์„ฑํ–ˆ์œผ๋ฏ€๋กœ ๋ฐ์ดํ„ฐ ์…‹ ํƒœ๊ทธ ์ง€์ • ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋ณต์ œํ•˜๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋กœ์ปฌ์—์„œ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ์œ ์šฉํ•œ ๋ฐ์ดํ„ฐ ์…‹ ์นด๋“œ ์ƒ์„ฑ์— ๋Œ€ํ•œ Datasets ๊ฐ€์ด๋“œ๋ฅผ ์ฝ๊ณ  ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค. Hub์—์„œ ์ง์ ‘ README.md ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ lewtun/github-issues ๋ฐ์ดํ„ฐ ์…‹ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ํ…œํ”Œ๋ฆฟ ๋ฐ์ดํ„ฐ ์…‹ ์นด๋“œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ„์›Œ์ง„ ๋ฐ์ดํ„ฐ ์…‹ ์นด๋“œ์˜ ์Šคํฌ๋ฆฐ์ˆ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ ์ข‹์€ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ƒ๋‹นํžˆ ๋ณต์žกํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์•˜์ง€๋งŒ ๋‹คํ–‰์Šค๋Ÿฝ๊ฒŒ๋„ ๊ทธ๊ฒƒ์„ ์—…๋กœ๋“œํ•˜๊ณ  ์ปค๋ฎค๋‹ˆํ‹ฐ์™€<NAME>๋Š” ๊ฒƒ์€ ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์„น์…˜์—์„œ๋Š” ์—ฌ๊ธฐ์„œ ๋งŒ๋“  ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ์…‹์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ์—ฐ๊ด€ ์ด์Šˆ(issues)์™€ ์ฃผ์„(comments)๋ฅผ ๋งค์น˜์‹œํ‚ค๋Š” ์˜๋ฏธ ๊ธฐ๋ฐ˜ ๊ฒ€์ƒ‰ ์—”์ง„์„ ๋งŒ๋“ค์–ด๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 5. FAISS๋ฅผ ์ด์šฉํ•œ ์‹œ๋งจํ‹ฑ ๊ฒ€์ƒ‰ ์ด์ „ ์„น์…˜์—์„œ๋Š” Datasets ์ €์žฅ์†Œ์˜ GitHub ์ด์Šˆ(issues) ๋ฐ ์˜๊ฒฌ(comments) ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค์–ด๋ดค์Šต๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ์ด ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ๊ฐ€์žฅ ์‹œ๊ธ‰ํ•œ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ฐพ๋Š” ๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒ€์ƒ‰ ์—”์ง„์„ ๊ตฌ์ถ•ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค! ์‹œ๋งจํ‹ฑ ๊ฒ€์ƒ‰์„ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ ์‚ฌ์šฉํ•˜๊ธฐ 1์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ๊ธฐ๋ฐ˜ ์–ธ์–ด ๋ชจ๋ธ์€ ํ…์ŠคํŠธ ๋‚ด์˜ ๊ฐ ํ† ํฐ์„ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(embedding vector)๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ฐœ๋ณ„ ์ž„๋ฒ ๋”ฉ์„ "ํ’€๋ง(pooling)" ํ•˜์—ฌ ์ „์ฒด ๋ฌธ์žฅ, ๋‹จ๋ฝ ๋˜๋Š” (๊ฒฝ์šฐ์— ๋”ฐ๋ผ) ๋ฌธ์„œ์— ๋Œ€ํ•œ ๋ฒกํ„ฐ ํ‘œํ˜„์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด๋Ÿฌํ•œ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์ž„๋ฒ ๋”ฉ ์‚ฌ์ด์˜ ๋‚ด์  ์œ ์‚ฌ๋„(dot-product similarity), ๋˜๋Š” ๋‹ค๋ฅธ ์œ ์‚ฌ๋„ ๋ฉ”ํŠธ๋ฆญ(similarity metric)์„ ๊ณ„์‚ฐํ•˜๊ณ  ๊ฐ€์žฅ ๋งŽ์ด ๊ฒน์น˜๋Š” ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ฝ”ํผ์Šค์—์„œ ์œ ์‚ฌ ๋ฌธ์„œ ๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๋งจํ‹ฑ ๊ฒ€์ƒ‰ ์—”์ง„์„ ๊ฐœ๋ฐœํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒ€์ƒ‰ ์—”์ง„์€ ์ฟผ๋ฆฌ์™€ ๋ฌธ์„œ์˜ ํ‚ค์›Œ๋“œ ๋งค์นญ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๊ธฐ์กด ์ ‘๊ทผ ๋ฐฉ์‹์— ๋น„ํ•ด ๋ช‡ ๊ฐ€์ง€ ์žฅ์ ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹ ๋กœ๋”ฉ ๋ฐ ์ค€๋น„ ์ž‘์—… ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผ ํ•  ์ผ์€ ์ด์ „ ์„น์…˜์—์„œ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  GitHub ์ด์Šˆ ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์— Hub ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Hugging Face Hub์—์„œ ํŒŒ์ผ์ด ์ €์žฅ๋œ URL์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค: from huggingface_hub import hf_hub_url data_files = hf_hub_url( repo_id="spasis/datasets-github-issues", filename="datasets-issues-with-comments.jsonl", repo_type="dataset", ) data_files data_files์— ์ €์žฅ๋œ URL์„ ์‚ฌ์šฉํ•˜์—ฌ 5.2์—์„œ ์†Œ๊ฐœํ•œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์›๊ฒฉ ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_dataset issues_dataset = load_dataset("json", data_files=data_files, split="train") issues_dataset ์—ฌ๊ธฐ์—์„œ load_dataset()์— ๊ธฐ๋ณธ train ๋ถ„ํ• (split)์„ ์ง€์ •ํ–ˆ์œผ๋ฏ€๋กœ DatasetDict ๋Œ€์‹  Dataset์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ž‘์—…์˜ ์ฒซ ๋ฒˆ์งธ ์ˆœ์„œ๋Š” pull requests๋ฅผ ํ•„ํ„ฐ๋งํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Pull requests๋Š” ์‚ฌ์šฉ์ž ์ฟผ๋ฆฌ(query)์— ์‘๋‹ตํ•˜๋Š”๋ฐ ๊ฑฐ์˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ  ๊ฒ€์ƒ‰ ์—”์ง„์— ๋…ธ์ด์ฆˆ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ์–ด๋Š ์ •๋„ ์ต์ˆ™ํ•ด์กŒ๊ฒ ์ง€๋งŒ, Dataset.filter() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ด๋Ÿฌํ•œ ํ–‰(rows)์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๋™์‹œ์— ์‚ฌ์šฉ์ž ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ œ๊ณตํ•  ์ˆ˜ ์—†๋Š” ์ฃผ์„์ด ์—†๋Š” ํ–‰๋“ค๋„ ํ•„ํ„ฐ๋งํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: issues_dataset = issues_dataset.filter( lambda x: (x["is_pull_request"] == False and len(x["comments"]) > 0) ) issues_dataset ๋ฐ์ดํ„ฐ ์…‹์— ๋งŽ์€ ์—ด(columns)์ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด๋“ค ๋Œ€๋ถ€๋ถ„์€ ๊ฒ€์ƒ‰ ๋Œ€์ƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰ ๊ด€์ ์—์„œ ๊ฐ€์žฅ ์œ ์ตํ•œ ์—ด์€ title, body ๋ฐ comments์ด๋ฉฐ html_url์€ ์†Œ์Šค ๋ฌธ์ œ์— ๋Œ€ํ•œ ๋งํฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Dataset.remove_columns() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ ์™ธ์˜ ๋‚˜๋จธ์ง€ ์—ด์„ ์‚ญ์ œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: columns = issues_dataset.column_names columns_to_keep = ["title", "body", "html_url", "comments"] columns_to_remove = set(columns_to_keep).symmetric_difference(columns) issues_dataset = issues_dataset.remove_columns(columns_to_remove) issues_dataset ์ž„๋ฒ ๋”ฉ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ ์ด์Šˆ์˜ comments์— ํ•ด๋‹น ์ด์Šˆ์˜ title๊ณผ body ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ•„๋“œ๋“ค์—๋Š” ์ข…์ข… ์œ ์šฉํ•œ ์ปจํ…์ŠคํŠธ ์ •๋ณด๊ฐ€ ํฌํ•จ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ๋ฐ์ดํ„ฐ ์…‹์˜ comments ์—ด(column)์€ ํ˜„์žฌ ๊ฐ ์ด์Šˆ์— ๋Œ€ํ•œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ comments ๋ฆฌ์ŠคํŠธ์ด๋ฏ€๋กœ ๊ฐ ํ–‰์ด (html_url, title, body, comment) ํŠœํ”Œ๋กœ ๊ตฌ์„ฑ๋˜๋„๋ก ํ•ด๋‹น ์—ด์„ "๋ถ„ํ•ด(explode, ๋‹จ ์ผํ–‰์˜ ํŠน์ • ํ•„๋“œ๊ฐ€ ์—ฌ๋Ÿฌ ํ•ญ๋ชฉ์„ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ, ์ด๋ฅผ ํ™•์žฅํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ–‰์œผ๋กœ ํ™•์žฅํ•˜๋Š” ๊ธฐ๋ฒ•)"์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. Pandas์—์„œ๋Š” DataFrame.explode() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑ๋œ ์—ด(list-like column)์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ƒˆ๋กœ์šด ํ–‰์„ ์ƒ์„ฑํ•˜๊ณ  ํ•ด๋‹น ์—ด์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๋ชจ๋“  ์—ด ๊ฐ’์„ ์ตœ์ดˆ ํ–‰์˜ ๊ฐ’์œผ๋กœ ๋ณต์ œํ•ฉ๋‹ˆ๋‹ค. ์ด ๋™์ž‘์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € Pandas DataFrame<NAME>์œผ๋กœ ์ „ํ™˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: issues_dataset.set_format("pandas") df = issues_dataset[:] ์šฐ์„  ์ด DataFrame์˜ ๋‹ค์„ฏ์งธ ํ–‰์„ ์‚ดํŽด๋ณด๋ฉด ํ•ด๋‹น ์ด์Šˆ์™€ ๊ด€๋ จ๋œ 4๊ฐœ์˜ comments๊ฐ€ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: len(df["comments"][5].tolist()) df๋ฅผ ๋ถ„ํ•ด(explode) ํ•  ๋•Œ ์ด๋Ÿฌํ•œ ๊ฐ comment์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ ํ–‰์ด ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑ๋  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์ธ์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. comments_df = df.explode("comments", ignore_index=True) comments_df.head(10) ๊ฐœ๋ณ„ comment๊ฐ€ ํฌํ•จ๋œ comments ์—ด๊ณผ ํ•จ๊ป˜ ํ–‰์ด ๋ณต์ œ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ด์ œ Pandas ์ž‘์—…์„ ๋งˆ์ณค์œผ๋ฏ€๋กœ DataFrame์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œํ•˜์—ฌ Dataset์œผ๋กœ ๋น ๋ฅด๊ฒŒ ๋‹ค์‹œ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import Dataset comments_dataset = Dataset.from_pandas(comments_df) comments_dataset ์ด์ œ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜์ฒœ ๊ฐœ์˜ comments๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ์ด์ œ ํ–‰๋‹น ํ•˜๋‚˜์˜ comment๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ comment ๋‹น ๋‹จ์–ด ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ์ƒˆ๋กœ์šด comments_length ์—ด์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: comments_dataset = comments_dataset.map( lambda x: {"comment_length": len(x["comments"].split())} ) ์ด ์‹ ๊ทœ ์นผ๋Ÿผ(column)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฒ€์ƒ‰์— ์œ ์šฉํ•˜์ง€ ์•Š์€ "cc @lewtun" ๋˜๋Š” "Thanks!"์™€ ๊ฐ™์€ ๋‚ด์šฉ์„ ํฌํ•จํ•˜๋Š” ์งง์€ ๋Œ“๊ธ€์„ ํ•„ํ„ฐ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•„ํ„ฐ์— ๋Œ€ํ•ด ์„ ํƒํ•  ์ •ํ™•ํ•œ ์ˆซ์ž๋Š” ์—†์ง€๋งŒ ์•ฝ 15๋‹จ์–ด๊ฐ€ ์ข‹์€ ์‹œ์ž‘์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค: comments_dataset = comments_dataset.filter(lambda x: x["comment_length"] > 15 and x["body"] is not None) comments_dataset ๋ฐ์ดํ„ฐ ์…‹์„ ์ผ๋ถ€ ์ •๋ฆฌํ–ˆ์œผ๋ฉด, title, body ๋ฐ comments์„ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์„œ ์ƒˆ ํ…์ŠคํŠธ ์นผ๋Ÿผ(column)์— ์ €์žฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด ๋ฐฉ๋ฒ•๋Œ€๋กœ Dataset.map()์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค: def concatenate_text(examples): return { "text": examples["title"] + " \n " + examples["body"] + " \n " + examples["comments"] } comments_dataset = comments_dataset.map(concatenate_text) ๋“œ๋””์–ด ์ž„๋ฒ ๋”ฉ์„ ๋งŒ๋“ค ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ํ•œ๋ฒˆ ์‚ดํŽด๋ด…์‹œ๋‹ค. ํ…์ŠคํŠธ ์ž„๋ฒ ๋”ฉ ์ƒ์„ฑ 2์žฅ์—์„œ AutoModel ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐ ์ž„๋ฒ ๋”ฉ์„ ์–ป์„ ์ˆ˜ ์žˆ์Œ์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํ•ด์•ผ ํ•  ์ผ์€ ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์ ์ ˆํ•œ ์ฒดํฌํฌ์ธํŠธ(checkpoint)๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ ์ž„๋ฒ ๋”ฉ์„ ๋งŒ๋“œ๋Š”๋ฐ ํŠนํ™”๋œ sentence-transformers๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฌธ์„œ์— ์„ค๋ช…๋œ ๋Œ€๋กœ ์ง€๊ธˆ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค๋ ค๊ณ  ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๊ฒฝ์šฐ๋Š” ๋น„๋Œ€์นญ ์˜๋ฏธ ๊ฒ€์ƒ‰(asymmetric semantic search)์˜ ํ•œ ์˜ˆ์ž…๋‹ˆ๋‹ค. ์ด์Šˆ comments๊ณผ ๊ฐ™์ด ๋” ๊ธด ๋ฌธ์„œ์—์„œ ๋‹ต์„ ์ฐพ๊ณ ์ž ํ•˜๋Š” ์งง์€ ์ฟผ๋ฆฌ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์„œ ๋‚ด์˜ ๋ชจ๋ธ ๊ฐœ์š” ํ…Œ์ด๋ธ”(model overview table)์—์„œ multi-qa-mpnet-base-dot-v1 ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ์˜๋ฏธ ๊ฒ€์ƒ‰์— ๊ฐ€์žฅ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜๋ฏ€๋กœ ์šฐ๋ฆฌ๋Š” ์ด๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋™์ผํ•œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฌ ๋‚˜์ด์ €๋„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoTokenizer, AutoModel model_ckpt = "sentence-transformers/multi-qa-mpnet-base-dot-v1" tokenizer = AutoTokenizer.from_pretrained(model_ckpt) model = AutoModel.from_pretrained(model_ckpt) ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์„ธ์Šค์˜ ์†๋„๋ฅผ ๋†’์ด๋ ค๋ฉด GPU ์žฅ์น˜์— ๋ชจ๋ธ๊ณผ ์ž…๋ ฅ์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ๋„์›€์ด ๋˜๋ฏ€๋กœ ์ง€๊ธˆ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: import torch device = torch.device("cuda") model.to(device) ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด GitHub ์ด์Šˆ ๋ง๋ญ‰์น˜์˜ ๊ฐ ํ•ญ๋ชฉ์„ ๋‹จ์ผ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ๋“  ํ† ํฐ ์ž„๋ฒ ๋”ฉ์„ "ํ’€๋ง(pooling)" ํ•˜๊ฑฐ๋‚˜ ํ‰๊ท ํ™”(average) ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•œ ๊ฐ€์ง€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ ‘๊ทผ ๋ฐฉ์‹์€ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์— ๋Œ€ํ•ด [CLS] ํ’€๋ง(pooling)์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํŠน์ˆ˜ [CLS] ํ† ํฐ์— ๋Œ€ํ•œ last_hidden_state๋ฅผ ์ˆ˜์ง‘ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ํ•จ์ˆ˜๊ฐ€ ๊ทธ ์ž‘์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค: def cls_pooling(model_output): return model_output.last_hidden_state[:, 0] ๋‹ค์Œ์œผ๋กœ, ๋ฌธ์„œ๋“ค์„ ํ† ํฐํ™”ํ•˜๊ณ , GPU์— ํ…์„œ(tensors)๋ฅผ ๋ฐฐ์น˜ํ•˜๊ณ , ์ด๋ฅผ ๋ชจ๋ธ์— ๊ณต๊ธ‰ํ•˜๊ณ , ๋งˆ์ง€๋ง‰์œผ๋กœ ์ถœ๋ ฅ์— CLS ํ’€๋ง์„ ์ ์šฉํ•˜๋Š” ํ•ผํผ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค: def get_embeddings(text_list): encoded_input = tokenizer(text_list, padding=True, truncation=True, return_tensors="pt") encoded_input = {k: v.to(device) for k, v in encoded_input.items()} model_output = model(**encoded_input) return cls_pooling(model_output) ๋ง๋ญ‰์น˜์˜ ์ฒซ ๋ฒˆ์งธ ํ…์ŠคํŠธ ํ•ญ๋ชฉ์„ ์ž…๋ ฅํ•˜๊ณ  ์ถœ๋ ฅ ๋ชจ์–‘์„ ๊ฒ€์‚ฌํ•˜์—ฌ ํ•จ์ˆ˜ ์ž‘๋™์„ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: embedding = get_embeddings(comments_dataset["text"][0]) embedding.shape ๋ง๋ญ‰์น˜์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์„ 768์ฐจ์› ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค! Dataset.map()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ง๋ญ‰์น˜์˜ ๊ฐ ํ–‰์— get_embeddings() ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒˆ ์ž„๋ฒ ๋”ฉ ์—ด์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: embeddings_dataset = comments_dataset.map( lambda x: {"embeddings": get_embeddings(x["text"]).detach().cpu().numpy()[0]} ) ์ž„๋ฒ ๋”ฉ์„ NumPy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ–ˆ์Œ์— ์ฃผ๋ชฉํ•˜์„ธ์š”. ๊ทธ ์ด์œ ๋Š” FAISS๋กœ ๋ฐ์ดํ„ฐ ์…‹์„ ์ธ๋ฑ์‹ฑํ•˜๋ ค๊ณ  ํ•  ๋•Œ Datasets์— ์ด<NAME>์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ์—์„œ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํšจ์œจ์ ์ธ ์‹œ๋งจํ‹ฑ ๊ฒ€์ƒ‰์„ ์œ„ํ•œ FAISS ์‚ฌ์šฉ ์ด์ œ ์ž„๋ฒ ๋”ฉ ๋ฐ์ดํ„ฐ ์…‹์ด ์žˆ์œผ๋ฏ€๋กœ ์ด๋ฅผ ๊ฒ€์ƒ‰ํ•  ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด FAISS ์ธ๋ฑ์Šค๋ผ๊ณ  ํ•˜๋Š”, Datasets ๋‚ด์—์„œ์˜ ํŠน๋ณ„ํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. FAISS(Facebook AI Similarity Search)๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ๊ฒ€์ƒ‰ํ•˜๊ณ  ํด๋Ÿฌ์Šคํ„ฐ๋ง ํ•˜๋Š” ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ œ๊ณตํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. FAISS์˜ ๊ธฐ๋ณธ ์•„์ด๋””์–ด๋Š” ์ž…๋ ฅ ์ž„๋ฒ ๋”ฉ๊ณผ ์œ ์‚ฌํ•œ ์ž„๋ฒ ๋”ฉ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ์ธ๋ฑ์Šค(index)๋ผ๋Š” ํŠน์ˆ˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Datasets์—์„œ FAISS ์ธ๋ฑ์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. Dataset.add_faiss_index() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์ธ๋ฑ์Šคํ•  ๋ฐ์ดํ„ฐ ์…‹์˜ ์—ด์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค: embeddings_dataset.add_faiss_index(column="embeddings") ์ด์ œ Dataset.get_nearest_examples() ํ•จ์ˆ˜๋กœ ๊ทผ์ ‘ ์ด์›ƒ ๊ฒ€์ƒ‰(nearest neighbor lookup)์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์ด ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์งˆ๋ฌธ์„ ์‚ฝ์ž…ํ•˜์—ฌ ์ด๋ฅผ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: question = "How can I load a dataset offline?" question_embedding = get_embeddings([question]).cpu().detach().numpy() question_embedding.shape ๋ฌธ์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด์ œ ํ•ด๋‹น ์ฟผ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” 768์ฐจ์› ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฒกํ„ฐ๋ฅผ ์ „์ฒด ์ฝ”ํผ์Šค์™€ ๋น„๊ตํ•˜์—ฌ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์ž„๋ฒ ๋”ฉ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: scores, samples = embeddings_dataset.get_nearest_examples("embeddings", question_embedding, k=5) Dataset.get_nearest_examples() ํ•จ์ˆ˜๋Š” ์ฟผ๋ฆฌ์™€ ๋ฌธ์„œ ๊ฐ„์˜ ์ค‘์ฒฉ(์œ ์‚ฌ๋„) ์ˆœ์œ„๋ฅผ ๋งค๊ธฐ๋Š” ์ ์ˆ˜ ํŠœํ”Œ(tuple)๊ณผ ํ•ด๋‹น ์ƒ˜ํ”Œ ์ง‘ํ•ฉ(์—ฌ๊ธฐ์„œ๋Š” ๊ฐ€์žฅ ์ž˜ ์ผ์น˜ํ•˜๋Š” 5๊ฐœ)์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋„๋ก pandas.DataFrame์—์„œ ์ˆ˜์ง‘ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: import pandas as pd samples_df = pd.DataFrame.from_dict(samples) samples_df["scores"] = scores samples_df.sort_values("scores", ascending=False, inplace=True) ์ด์ œ ์ฟผ๋ฆฌ๊ฐ€ ๊ฒ€์ƒ‰๋œ comments์™€ ์–ผ๋งˆ๋‚˜ ์ž˜ ์ผ์น˜ํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์œ„ํ•ด ์ฒ˜์Œ ๋ช‡ ๊ฐœ์˜ ํ–‰์„ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: for _, row in samples_df.iterrows(): print(f"COMMENT: {row.comments}") print(f"SCORE: {row.scores}") print(f"TITLE: {row.title}") print(f"URL: {row.html_url}") print("=" * 50) print() ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ ์ค‘์—์„œ ๋‘ ๋ฒˆ์งธ comment๊ฐ€ ์ฟผ๋ฆฌ์™€ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 6. 5์žฅ ์š”์•ฝ (Summary) Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ–ฅํ•œ ๊ต‰์žฅํ•œ ์—ฌํ–‰์ด์—ˆ์Šต๋‹ˆ๋‹ค! ์—ฌ๊ธฐ๊นŒ์ง€ ์˜จ ๊ฒƒ์„ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! ์ด ์žฅ์—์„œ ์–ป์€ ์ง€์‹์œผ๋กœ ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: Hugging Face Hub, ๋…ธํŠธ๋ถ ๋˜๋Š” ํšŒ์‚ฌ์˜ ์›๊ฒฉ ์„œ๋ฒ„ ๋“ฑ ์–ด๋””์—์„œ๋‚˜ ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋”ฉ Dataset.map() ๋ฐ Dataset.filter() ํ•จ์ˆ˜๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •, ํ•„ํ„ฐ๋ง ๋ฐ ์ฆ๊ฐ• Dataset.set_format()์„ ์‚ฌ์šฉํ•˜์—ฌ Pandas ๋ฐ NumPy์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ<NAME> ๊ฐ„์— ๋น ๋ฅด๊ฒŒ ์ „ํ™˜ ๋‚˜๋งŒ์˜ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค์–ด Hugging Face Hub์— ํ‘ธ์‹œ Transformer ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ์ž„๋ฒ ๋”ฉํ•˜๊ณ , FAISS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๋งจํ‹ฑ ๊ฒ€์ƒ‰ ์—”์ง„์„ ๊ตฌ์ถ• 7์žฅ์—์„œ ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ๋ชจ๋ธ์„ ํ™œ์šฉํ•˜์—ฌ ์ฃผ์š” NLP ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ ๋Œ€ํ•ด ์ž์„ธํžˆ ๊ณต๋ถ€ํ•  ๋•Œ, ์ด ๋ชจ๋“  ๊ฒƒ์„ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. 6์žฅ. Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ 3์žฅ์—์„œ ์šฐ๋ฆฌ๋Š” ๋Œ€์ƒ ์ž‘์—…์„ ์œ„ํ•ด์„œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋ฏธ์„ธ ์กฐ์ • ๊ณผ์ •์—์„œ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ๊ณผ ๋™์ผํ•œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์ข‹์„๊นŒ์š”? ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ๋„๋ฉ”์ธ์ด๋‚˜ ์–ธ์–ด์˜ ๋ง๋ญ‰์น˜๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ฐจ์„ ์ฑ…(suboptimal)์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์˜์–ด ๋ง๋ญ‰์น˜์— ๋Œ€ํ•ด ํ•™์Šต๋œ ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ณต๋ฐฑ๊ณผ ๊ตฌ๋‘์ ์˜ ์‚ฌ์šฉ์ด ๋งค์šฐ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ณธ์–ด ํ…์ŠคํŠธ์˜ ๋ง๋ญ‰์น˜์—์„œ ์ œ๋Œ€๋กœ ์ˆ˜ํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ์žฅ์—์„œ๋Š” ํ…์ŠคํŠธ ๋ง๋ญ‰์น˜์—์„œ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ด๋ฅผ ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์ „ ํ•™์Šตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณต๋ถ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋ชจ๋‘ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ "๋น ๋ฅธ(fast)" ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ œ๊ณตํ•˜๋Š” Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ณ  ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €(fast tokenizers)๊ฐ€ "๋Š๋ฆฐ(slow)" ๋ฒ„์ „๊ณผ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์žฅ์—์„œ ๋‹ค๋ฃฐ ์ฃผ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ์ƒˆ๋กœ์šด ํ…์ŠคํŠธ ๋ง๋ญ‰์น˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํŠน์ • ์ฒดํฌํฌ์ธํŠธ์˜ ํ† ํฌ ๋‚˜์ด์ €์™€ ์œ ์‚ฌํ•œ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šต์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ• ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €(fast tokenizers)์˜ ํŠน์ง• ์˜ค๋Š˜๋‚  NLP์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์„ธ ๊ฐ€์ง€ ์ฃผ์š” ๋‹จ์–ด ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ฐจ์ด์  Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ตฌ์ถ•ํ•˜๊ณ  ํŠน์ • ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ• ์ด ์žฅ์—์„œ ์†Œ๊ฐœ๋œ ๊ธฐ๋ฒ•๋“ค์€ 7์žฅ์—์„œ Python ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์œ„ํ•œ ์–ธ์–ด ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•  ๋•Œ ํ™œ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ํ† ํฌ ๋‚˜์ด์ €๋ฅผ "ํ•™์Šต(train)"์‹œํ‚ค๋Š” ๊ฒƒ์ด ๋ฌด์—‡์„ ์˜๋ฏธํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๊ธฐ์กด ํ† ํฌ ๋‚˜์ด์ €์—์„œ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ € ํ•™์Šต ํŠน์ • ์–ธ์–ด ๋ชจ๋ธ์ด ๋‹น์‹ ์ด ์›ํ•˜๋Š” ์–ธ์–ด๋ฅผ ์ œ๋Œ€๋กœ ์ง€์›ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, ํ˜„์žฌ ๋ณด์œ ํ•œ ๋ง๋ญ‰์น˜๊ฐ€ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ๋‹ค๋ฅธ ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ง๋ญ‰์น˜์— ์ ํ•ฉํ•˜๊ฒŒ ์ ์‘๋œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์ƒˆ๋กญ๊ฒŒ ํ•™์Šตํ•˜๊ธฐ ์›ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ๊ทธ ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๊ฒƒ์ด ์ •ํ™•ํ•˜๊ฒŒ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๊ฐ€ ๋ฌด์—‡์ผ๊นŒ์š”? 2์žฅ์—์„œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ฒ˜์Œ ์ ‘ํ–ˆ์„ ๋•Œ ๋Œ€๋ถ€๋ถ„์˜ ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ๋ชจ๋ธ์ด ํ•˜์œ„ ๋‹จ์–ด ํ† ํฐํ™”(subword tokenization) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ํ•˜์œ„ ๋‹จ์–ด(subword)๊ฐ€ ์ค‘์š”ํ•œ์ง€, ๊ทธ๋ฆฌ๊ณ  ์–ด๋–ค ํ•˜์œ„ ๋‹จ์–ด(subword)๊ฐ€ ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ฝ”ํผ์Šค์—์„œ ๋ฐœ์ƒํ•˜๋Š”์ง€ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด, ํ† ํฌ ๋‚˜์ด์ €๋Š” ์ฝ”ํผ์Šค์˜ ๋ชจ๋“  ํ…์ŠคํŠธ๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณผ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์šฐ๋ฆฌ๊ฐ€ "ํ•™์Šต(training)"์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ํ”„๋กœ์„ธ์Šค์ž…๋‹ˆ๋‹ค. ์ด ํ•™์Šต์„<NAME>๋Š” ์ •ํ™•ํ•œ ๊ทœ์น™๋“ค์€ ์‚ฌ์šฉ๋œ ํ† ํฌ ๋‚˜์ด์ €์˜ ์œ ํ˜•์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋ฉฐ ์ด ์žฅ์˜ ๋’ท๋ถ€๋ถ„์—์„œ ์„ธ ๊ฐ€์ง€ ์ฃผ์š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ € ํ•™์Šต์€ ๋ชจ๋ธ์— ๋Œ€ํ•œ ํ•™์Šต๊ณผ๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค! ๋ชจ๋ธ ํ•™์Šต์€ ํ™•๋ฅ ์  ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(stochastic gradient descent)์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ๊ฐ์˜ ๋ฐฐ์น˜(batch)์— ๋Œ€ํ•ด ์†์‹ค(loss)์„ ์กฐ๊ธˆ์”ฉ ๋” ์ž‘๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ ๋ณธ์งˆ์ ์œผ๋กœ ๋ฌด์ž‘์œ„์ž…๋‹ˆ๋‹ค(์ฆ‰, ๋™์ผํ•œ ํ•™์Šต์„ ๋‘ ๋ฒˆ ์ˆ˜ํ–‰ํ•  ๋•Œ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์œผ๋ ค๋ฉด ๊ณ ์ •๋œ seed๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•จ). ํ† ํฌ ๋‚˜์ด์ € ํ•™์Šต์€ ์ฃผ์–ด์ง„ ๋ง๋ญ‰์น˜์— ๋Œ€ํ•ด ์–ด๋–ค ํ•˜์œ„ ๋‹จ์–ด(subword)๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์€์ง€ ์‹๋ณ„ํ•˜๋ ค๋Š” ํ†ต๊ณ„์  ํ”„๋กœ์„ธ์Šค์ด๋ฉฐ, ์ด๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ •ํ™•ํ•œ ๊ทœ์น™์€ ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ ๊ฒฐ์ •๋ก ์ (deterministic)์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋™์ผํ•œ ๋ง๋ญ‰์น˜์—์„œ ๋™์ผํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ•™์Šตํ•˜๋ฉด ํ•ญ์ƒ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ง๋ญ‰์น˜ ๋ชจ์œผ๊ธฐ Transformers์—๋Š” ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ๊ฒƒ๋“ค๊ณผ ๋™์ผํ•œ ํŠน์„ฑ์„ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•œ API๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ AutoTokenizer.train_new_from_iterator()๊ฐ€ ๊ทธ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹ค์ œ๋กœ ์‹คํ–‰ํ•ด ๋ณด๊ธฐ ์œ„ํ•ด GPT-2๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ์˜์–ด๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ํ•™์Šตํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ž‘์—…์€ ํ•ด๋‹น ์–ธ์–ด๋กœ ํ‘œํ˜„๋œ ๋Œ€๊ทœ๋ชจ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜์—ฌ ํ•™์Šต ๋ง๋ญ‰์น˜๋กœ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ์‚ฌ๋žŒ์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์‹œ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๊ธฐ์„œ๋Š” ๋Ÿฌ์‹œ์•„์–ด๋‚˜ ์ค‘๊ตญ์–ด์™€ ๊ฐ™์€ ์–ธ์–ด๊ฐ€ ์•„๋‹ˆ๋ผ ํŠน์ˆ˜ํ•œ ์˜์–ด ํ…์ŠคํŠธ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋Š” Python ์†Œ์Šค์ฝ”๋“œ ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Python ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋ชจ์œผ๋Š” ๋ฐ ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ load_dataset() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ CodeSearchNet ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์บ์‹œ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ ์…‹์€ CodeSearchNet ์ฑŒ๋ฆฐ์ง€๋ฅผ ์œ„ํ•ด ์ƒ์„ฑ๋˜์—ˆ์œผ๋ฉฐ ์—ฌ๋Ÿฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋กœ ๋œ GitHub์˜ ์˜คํ”ˆ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ˆ˜๋ฐฑ๋งŒ ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์˜ˆ์‹œ์—์„œ๋Š” ์ด ๋ฐ์ดํ„ฐ ์…‹์˜ Python ๋ถ€๋ถ„์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค: from datasets import load_dataset # ๋กœ๋“œํ•˜๋Š”๋ฐ ๋ช‡ ๋ถ„์ด ์†Œ์š”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปคํ”ผ๋‚˜ ์ฐจ๋ฅผ ์ค€๋น„ํ•˜์„ธ์š”. raw_datasets = load_dataset("code_search_net", "python") ํ•™์Šต ๋ถ„ํ• (Training split)์„ ์‚ดํŽด๋ณด๊ณ  ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ์—ด(columns)์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. raw_datasets["train"] ์ด ๋ฐ์ดํ„ฐ ์…‹์ด ์ฝ”๋“œ์—์„œ ๋…์ŠคํŠธ๋ง(docstrings)์„ ๋ถ„๋ฆฌํ•˜๊ณ  ์žˆ๊ณ , ์ด ์ฝ”๋“œ์™€ ๋…์ŠคํŠธ๋ง์— ๋Œ€ํ•œ ํ† ํฐ ํ™”๊ฐ€ ํ•„์š”ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด whole_func_string ์—ด๋งŒ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ•™์Šต ๋ถ„ํ• (train split)์„ ์ธ๋ฑ์‹ฑํ•˜์—ฌ ํ•จ์ˆ˜๋“ค ์ค‘ ํ•˜๋‚˜์˜ ์˜ˆ์‹œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: print(raw_datasets["train"][123456]["whole_func_string"]) ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผ ํ•  ์ผ์€ ๋ฐ์ดํ„ฐ ์…‹์„ ํ…์ŠคํŠธ ๋ฆฌ์ŠคํŠธ์˜ ์ดํ„ฐ ๋ ˆ์ดํ„ฐ(iterator)๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ…์ŠคํŠธ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐœ๋ณ„ ํ…์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•˜๋Š” ๋Œ€์‹  ํ…์ŠคํŠธ ๋ฐฐ์น˜(batches)์— ๋Œ€ํ•œ ํ•™์Šต์„ ํ†ตํ•ด์„œ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋” ๋นจ๋ผ์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ชจ๋“  ๊ฒƒ์„ ํ•œ ๋ฒˆ์— ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋”ฉํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด ์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ดํ„ฐ ๋ ˆ์ดํ„ฐ(iterator)๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ง๋ญ‰์น˜์˜ ๊ทœ๋ชจ๊ฐ€ ํฌ๋‹ค๋ฉด Datasets๋Š” RAM์— ๋ชจ๋“  ๊ฒƒ์„ ๋กœ๋“œํ•˜์ง€ ์•Š๊ณ  ๋ฐ์ดํ„ฐ ์…‹์˜ ์š”์†Œ๋ฅผ ๋””์Šคํฌ์— ์ €์žฅํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๊ฐ๊ฐ 1,000๊ฐœ์˜ ํ…์ŠคํŠธ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋˜์ง€๋งŒ ๋ชจ๋“  ๊ฒƒ์ด ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค: # ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ทœ๋ชจ๊ฐ€ ์ž‘์ง€ ์•Š๋‹ค๋ฉด, ๋‹ค์Œ ๋ผ์ธ์˜ ์ฃผ์„์„ ์ œ๊ฑฐํ•˜์ง€ ๋งˆ์„ธ์š”. # training_corpus = [raw_datasets["train"][i: i + 1000]["whole_func_string"] for i in range(0, len(raw_datasets["train"]), 1000)] Python ์ œ๋„ˆ๋ ˆ์ดํ„ฐ(generator)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹ค์ œ๋กœ ํ•„์š”ํ•  ๋•Œ๊นŒ์ง€ Python์ด ๋ฉ”๋ชจ๋ฆฌ์— ์•„๋ฌด๊ฒƒ๋„ ๋กœ๋“œํ•˜์ง€ ์•Š๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒ์„ฑ๊ธฐ๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด ๊บพ์‡ ๊ด„ํ˜ธ(brackets)๋ฅผ ์†Œ๊ด„ํ˜ธ(parentheses)๋กœ ๋ฐ”๊พธ๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: training_corpus = ( raw_datasets["train"][i : i + 1000]["whole_func_string"] for i in range(0, len(raw_datasets["train"]), 1000) ) ์œ„ ์ฝ”๋“œ๋Š” ๋ฐ์ดํ„ฐ ์…‹์˜ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Python์˜ for ๋ฃจํ”„์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋ฟ์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋Š” ํ•„์š”ํ•  ๋•Œ๋งŒ ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋กœ๋“œ๋˜๋ฉฐ(์ฆ‰, ํ•ด๋‹น ํ…์ŠคํŠธ ์ง‘ํ•ฉ์ด ํ•„์š”ํ•œ for-loop ๋‹จ๊ณ„์— ์žˆ์„ ๋•Œ๋งŒ) ํ•œ ๋ฒˆ์— 1,000๊ฐœ์˜ ํ…์ŠคํŠธ๋งŒ ๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฑฐ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์…‹์„ ์ฒ˜๋ฆฌํ•˜๋”๋ผ๋„ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์™„์ „ํžˆ ์†Œ์ง„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ(generator) ๊ฐ์ฒด์˜ ๋ฌธ์ œ์ ์€ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์•„๋ž˜ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ƒํ•œ ๊ฒƒ์ฒ˜๋Ÿผ 10๊ฐœ์˜ ์ˆซ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‘ ๋ฒˆ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ: gen = (i for i in range(10)) print(list(gen)) print(list(gen)) ์ฒซ ๋ฒˆ์งธ print ๋ฌธ์€ 10๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•˜์ง€๋งŒ, ๊ทธ๋‹ค์Œ์€ ๋น„์–ด์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ(generator)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค: def get_training_corpus(): return ( raw_datasets["train"][i : i + 1000]["whole_func_string"] for i in range(0, len(raw_datasets["train"]), 1000) ) training_corpus = get_training_corpus() yield ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ for ๋ฃจํ”„ ๋‚ด์—์„œ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ(generator)๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: def get_training_corpus(): dataset = raw_datasets["train"] for start_idx in range(0, len(dataset), 1000): samples = dataset[start_idx : start_idx + 1000] yield sampels["whole_func_string"] ์ด์ „๊ณผ ๋™์ผํ•œ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์ง€๋งŒ ๋ฆฌ์ŠคํŠธ ๋‚ดํฌ(list comprehension)์—์„œ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๋ณต์žกํ•œ ๋กœ์ง์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ € ํ•™์Šต ์ด์ œ ํ…์ŠคํŠธ ๋ฐฐ์น˜(batch)์˜ ์ดํ„ฐ ๋ ˆ์ดํ„ฐ(iterator) ํ˜•ํƒœ๋กœ ๋ง๋ญ‰์น˜๋ฅผ ๊ตฌ์„ฑํ–ˆ์œผ๋ฏ€๋กœ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์„ ์œ„ํ•ด์„œ ๋จผ์ € ๋ชจ๋ธ๊ณผ ์ผ์น˜์‹œํ‚ค๋ ค๋Š” ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” GPT-2๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: from transformers import AutoTokenizer old_tokenizer = AutoTokenizer.from_pretrained("gpt2") ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•˜์ง€๋งŒ, ์™„์ „ํžˆ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‚˜ ์‚ฌ์šฉํ•˜๋ ค๋Š” ํŠน์ˆ˜ ํ† ํฐ(special tokens)์— ๋Œ€ํ•ด ์•„๋ฌด๊ฒƒ๋„ ์‹ ๊ฒฝ ์“ฐ๊ฑฐ๋‚˜ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋Š” GPT-2์™€ ์ •ํ™•ํžˆ ๋™์ผํ•  ๊ฒƒ์ด๋ฉฐ, ์šฐ๋ฆฌ ๋ง๋ญ‰์น˜๋ฅผ ์ด์šฉํ•œ ํ•™์Šต์„ ํ†ตํ•ด vocabulary๋งŒ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ๋จผ์ € ์ด ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์˜ˆ์ œ ํ•จ์ˆ˜(example function)๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: example = '''def add_numbers(a, b): """Add the two numbers `a` and `b`.""" return a + b''' tokens = old_tokenizer.tokenize(example) tokens ์ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ฐ๊ฐ ๊ณต๋ฐฑ๊ณผ ์ค„๋ฐ”๊ฟˆ์„ ๋‚˜ํƒ€๋‚ด๋Š” ฤŠ ๋ฐ ฤ ์™€ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ํŠน์ˆ˜ ๊ธฐํ˜ธ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, ์ด๋Š” ํšจ์œจ์ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ณต๋ฐฑ์ด ๋‚˜ํƒ€๋‚  ๋•Œ ํ† ํฌ ๋‚˜์ด์ €๋Š” ์ด๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ ๊ณต๋ฐฑ์„ ๊ฐœ๋ณ„ ํ† ํฐ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์†Œ์Šค์ฝ”๋“œ์—์„œ 4๊ฐœ ๋˜๋Š” 8๊ฐœ์˜ ๊ณต๋ฐฑ ๊ทธ๋ฃน์ด ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ _ ๋ฌธ์ž๊ฐ€ ์žˆ๋Š” ๋‹จ์–ด๊ฐ€ ์ต์ˆ™ํ•˜์ง€ ์•Š์€์ง€, ํ•จ์ˆ˜๋ช…์„ ์•ฝ๊ฐ„ ์ด์ƒํ•˜๊ฒŒ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•˜๊ณ  ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”์ง€ ๋ด…์‹œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” train_new_from_iterator() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: tokenizer = old_tokenizer.train_new_from_iterator(training_corpus, 52000) ์ด ๋ช…๋ น์€ ๋ง๋ญ‰์น˜๊ฐ€ ๋งค์šฐ ํฐ ๊ฒฝ์šฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ 1.6GB ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ฒฝ์šฐ์—๋Š” ๋งค์šฐ ๋น ๋ฆ…๋‹ˆ๋‹ค(12์ฝ”์–ด๊ฐ€ ์žˆ๋Š” AMD Ryzen 9 3900X CPU์—์„œ 1๋ถ„ 16์ดˆ). AutoTokenizer.train_new_from_iterator()๋Š” ์‚ฌ์šฉ ์ค‘์ธ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ "๋น ๋ฅธ(fast)" ํ† ํฌ ๋‚˜์ด์ €์ธ ๊ฒฝ์šฐ์—๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์„น์…˜์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” ๋‘ ๊ฐ€์ง€ ์œ ํ˜•์˜ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ์œ ํ˜•์€ ์ˆœ์ˆ˜ํ•˜๊ฒŒ Python์œผ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ๊ณ  ๋‹ค๋ฅธ ์œ ํ˜•(๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €)์€ Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋„์›€์„ ๋ฐ›์•„์„œ Rust ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋กœ ์ž‘์„ฑ๋œ ํ† ํฌ ๋‚˜์ด์ €์ž…๋‹ˆ๋‹ค. Python์€ ๋ฐ์ดํ„ฐ ๊ณผํ•™ ๋ฐ ๋”ฅ๋Ÿฌ๋‹ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์— ๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด์ด์ง€๋งŒ ๋น ๋ฅธ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจ๋ธ ๊ณ„์‚ฐ(model computation)์˜ ํ•ต์‹ฌ์ธ ํ–‰๋ ฌ ๊ณฑ์…ˆ(matrix multiplication)์€ GPU์— ์ตœ์ ํ™”๋œ C ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ CUDA๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆœ์ˆ˜ํ•œ Python์œผ๋กœ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์€ ์—„์ฒญ๋‚˜๊ฒŒ ๋Š๋ฆด ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์šฐ๋ฆฌ๊ฐ€ Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐœ๋ฐœํ•œ ์ด์œ ์ž…๋‹ˆ๋‹ค. GPU์— ๋กœ๋“œ๋œ ์ž…๋ ฅ ๋ฐฐ์น˜(input batch)์—์„œ ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด CUDA ์–ธ์–ด๋ฅผ ๋ฐฐ์šธ ํ•„์š”๊ฐ€ ์—†์—ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Rust๋ฅผ ๋ฐฐ์šธ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ Rust์˜ ์ผ๋ถ€ ์ฝ”๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋งŽ์€ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ Python ๋ฐ”์ธ๋”ฉ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ƒˆ ํ† ํฌ ๋‚˜์ด์ €์˜ ํ•™์Šต์„ ๋ณ‘๋ ฌํ™”ํ•˜๊ฑฐ๋‚˜ 3์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ์ž…๋ ฅ ๋ฐฐ์น˜(batch)์˜ ํ† ํฐํ™”๋ฅผ ๋ณ‘๋ ฌํ™”ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ๋ชจ๋ธ์—๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ "๋น ๋ฅธ(fast)" ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์žˆ์œผ๋ฉฐ(์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์™ธ๊ฐ€ ์žˆ์Œ) AutoTokenizer API๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ํ•ญ์ƒ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์„น์…˜์—์„œ๋Š” ํ† ํฐ ๋ถ„๋ฅ˜(token classification) ๋ฐ ์งˆ์˜์‘๋‹ต(question answering)๊ณผ ๊ฐ™์€ ์ž‘์—…์— ์ •๋ง ์œ ์šฉํ•œ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €์˜ ๋‹ค๋ฅธ ๋ช‡ ๊ฐ€์ง€ ํŠน์ˆ˜ ๊ธฐ๋Šฅ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ ์ „์— ์œ„ ์˜ˆ์ œ์—์„œ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: tokens = tokenizer.tokenize(example) tokens ์œ„ ๊ฒฐ๊ณผ์—์„œ ๊ณต๋ฐฑ(space)๊ณผ ์ค„๋ฐ”๊ฟˆ(newline)์„ ๋‚˜ํƒ€๋‚ด๋Š” ํŠน์ˆ˜ ๊ธฐํ˜ธ ฤŠ ๋ฐ ฤ ๋ฅผ ๋‹ค์‹œ ๋ณผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ƒˆ๋กญ๊ฒŒ ํ•™์Šต๋œ ํ† ํฌ ๋‚˜์ด์ €๋Š” Python ํ•จ์ˆ˜(function) ์ฝ”ํผ์Šค์— ๋งค์šฐ ํŠนํ™”๋œ ์ผ๋ถ€ ํ† ํฐ์„ ํ•™์Šตํ–ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ฤŠฤ ฤ ฤ  ํ† ํฐ๊ณผ ๋…์ŠคํŠธ๋ง์„ ์‹œ์ž‘ํ•˜๋Š” ์„ธ ๊ฐœ์˜ ๋”ฐ์˜ดํ‘œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ฤ """ ํ† ํฐ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๋Š” _ ๋ฌธ์ž๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ํ•จ์ˆ˜ ๋ช…๋„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋งค์šฐ ๊ฐ„๊ฒฐํ•œ(compact) ํ‘œํ˜„์ž…๋‹ˆ๋‹ค. ์ด์— ๋น„ํ•ด, ๋™์ผํ•œ ์˜ˆ์ œ์—์„œ ์ผ๋ฐ˜์ ์ธ ์˜์–ด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ๊ธด ๋ฌธ์žฅ(ํ˜น์€ ํ† ํฐ ์‹œํ€€์Šค)์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: print(len(tokens)) print(len(old_tokenizer.tokenize(example))) ๋‹ค๋ฅธ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: example = """class LinearLayer(): def __init__(self, input_size, output_size): self.weight = torch.randn(input_size, output_size) self.bias = torch.zeros(output_size) def __call__(self, x): return x @ self.weights + self.bias """ tokenizer.tokenize(example) ๋“ค์—ฌ ์“ฐ๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ํ† ํฐ ์™ธ์—๋„ ์—ฌ๊ธฐ์—์„œ๋Š” ์ด์ค‘ ๋“ค์—ฌ ์“ฐ๊ธฐ์— ๋Œ€ํ•œ ํ† ํฐ(ฤŠฤ ฤ ฤ ฤ ฤ ฤ ฤ )์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. class, init, call, self, return๊ณผ ๊ฐ™์€ ํŠน์ˆ˜ํ•œ Python ๋‹จ์–ด๋Š” ๊ฐ๊ฐ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ํ† ํฐํ™”๋˜๋ฉฐ _ ๋ฐ.์œผ๋กœ ๋ถ„ํ• ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๋Š” camel-cased name๋„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. LinearLayer๋Š” ["ฤ Linear", "Layer"]๋กœ ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต๋œ ํ† ํฌ ๋‚˜์ด์ € ์ €์žฅ ์ด์ œ ๋‚˜์ค‘์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ƒˆ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด ์ž‘์—…์€ save_pretrained() ๋ฉ”์„œ๋“œ๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค: tokenizer.save_pretrained("code-search-net-tokenizer") ๊ทธ๋Ÿฌ๋ฉด code-search-net-tokenizer๋ผ๋Š” ์ƒˆ๋กœ์šด ํด๋”๊ฐ€ ์ƒ์„ฑ๋˜๋ฉฐ ์—ฌ๊ธฐ์—๋Š” ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋‹ค์‹œ ๋กœ๋“œํ•  ๋•Œ ํ•„์š”๋กœ ํ•˜๋Š” ๋ชจ๋“  ํŒŒ์ผ์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์ด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋™๋ฃŒ ๋ฐ ์นœ๊ตฌ์™€<NAME>๋ ค๋ฉด ๊ณ„์ •์— ๋กœ๊ทธ์ธํ•˜์—ฌ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋…ธํŠธ๋ถ์—์„œ ์ž‘์—…ํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ๋„์™€์ฃผ๋Š” ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค: from huggingface_hub import notebook_login notebook_login() ๊ทธ๋Ÿฌ๋ฉด Hugging Face ๋กœ๊ทธ์ธ ์ž๊ฒฉ ์ฆ๋ช…์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์ ฏ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋…ธํŠธ๋ถ์—์„œ ์ž‘์—…ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ํ„ฐ๋ฏธ๋„์— ๋‹ค์Œ ์ค„์„ ์ž…๋ ฅํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: huggingface-cli login ๋กœ๊ทธ์ธํ•˜๋ฉด ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์—ฌ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ‘ธ์‹œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.push_to_hub("code-search-net-tokenizer", use_temp_dir=True) ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ† ํฌ ๋‚˜์ด์ € ํŒŒ์ผ์ด ํฌํ•จ๋œ code-search-net-tokenizer๋ผ๋Š” ์ด๋ฆ„์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค(namespace)์— ์ƒˆ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ from_pretrained() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋””์„œ๋‚˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: # ๋‹น์‹ ์ด ์ง์ ‘ ์ด ์„น์…˜์—์„œ ํ•™์Šตํ•œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ, # ์•„๋ž˜์˜ "spasis"๋ฅผ ๋‹น์‹ ์˜ ์‹ค์ œ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋กœ ๋ณ€๊ฒฝํ•˜์‹ญ์‹œ์˜ค. tokenizer = AutoTokenizer.from_pretrained("spasis/code-search-net-tokenizer") ์ด์ œ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฐ‘๋ฐ”๋‹ฅ๋ถ€ํ„ฐ ์ƒˆ๋กญ๊ฒŒ(from scratch) ํ•™์Šตํ•˜๊ณ  ํ˜„์žฌ ์ž‘์—…์—์„œ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ์ด๋Š” 7์žฅ์—์„œ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋จผ์ € ์ด ์žฅ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์—์„œ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ณ  train_new_from_iterator() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‹ค์ œ๋กœ ์–ด๋–ค ์ผ์ด ๋ฐœ์ƒํ•˜๋Š”์ง€ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. "๋น ๋ฅธ(fast)" ํ† ํฌ ๋‚˜์ด์ €์˜ ํŠน๋ณ„ํ•œ ๋Šฅ๋ ฅ ์ด ์„น์…˜์—์„œ๋Š” Transformers์—์„œ ํ† ํฌ ๋‚˜์ด์ €์˜ ๊ธฐ๋Šฅ์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ž…๋ ฅ์„ ํ† ํฐํ™”ํ•˜๊ฑฐ๋‚˜ ํ† ํฐ ์•„์ด๋””๋ฅผ ๋‹ค์‹œ ํ…์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉ ํ•˜๋Š” ๋ฐ๋งŒ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ํ† ํฌ ๋‚˜์ด์ €, ํŠนํžˆ Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ง€์›ํ•˜๋Š” ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ›จ์”ฌ ๋” ๋งŽ์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด 1์žฅ์—์„œ ์ฒ˜์Œ ์ ‘ํ•œ token-classification(NER) ๋ฐ question-answering ํŒŒ์ดํ”„๋ผ์ธ์˜ ๊ฒฐ๊ณผ๋ฅผ ์žฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค. ๋‹ค์Œ ๋…ผ์˜์—์„œ ์šฐ๋ฆฌ๋Š” ์ข…์ข… "๋Š๋ฆฐ(slow)" ํ† ํฌ ๋‚˜์ด์ €์™€ "๋น ๋ฅธ(fast)" ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. "๋Š๋ฆฐ(slow)" ํ† ํฌ ๋‚˜์ด์ €๋Š” Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด๋ถ€์—์„œ Python์œผ๋กœ ์ž‘์„ฑ๋œ ๊ฒƒ์ด๊ณ , ๋น ๋ฅธ ๋ฒ„์ „์€ Rust๋กœ ์ž‘์„ฑ๋˜์–ด Tokenizers์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•ฝ๋ฌผ ๊ฒ€ํ† (drug review) ๋ฐ์ดํ„ฐ ์…‹์„ ํ† ํฐํ™”ํ•˜๋Š”๋ฐ ๋น ๋ฅธ ํ˜น์€ ๋Š๋ฆฐ ํ† ํฌ ๋‚˜์ด์ €์˜ ์‹คํ–‰ ์†๋„๋ฅผ ๋ณด๊ณ ํ•œ 5์žฅ์˜ ํ‘œ๋ฅผ ๊ธฐ์–ตํ•œ๋‹ค๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์ด๋ฅผ ๋น ๋ฅด๊ณ  ๋Š๋ฆฐ ๊ฒƒ์œผ๋กœ ๋ถ€๋ฅด๋Š” ์ด์œ ๋ฅผ ์•Œ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹จ์ผ ๋ฌธ์žฅ์„ ํ† ํฐํ™”ํ•  ๋•Œ ๋™์ผํ•œ ํ† ํฌ ๋‚˜์ด์ €์˜ ๋Š๋ฆฐ ๋ฒ„์ „๊ณผ ๋น ๋ฅธ ๋ฒ„์ „ ๊ฐ„์˜ ์†๋„ ์ฐจ์ด๊ฐ€ ํ•ญ์ƒ ๋‚˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์‚ฌ์‹ค, ๋น ๋ฅธ ๋ฒ„์ „์€ ์‹ค์ œ๋กœ ๋” ๋Š๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๋งŽ์€ ํ…์ŠคํŠธ๋ฅผ ๋™์‹œ์— ํ† ํฐํ™”ํ•  ๋•Œ๋งŒ ์ฐจ์ด๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ธ์ฝ”๋”ฉ (Batch encoding) ํ† ํฌ ๋‚˜์ด์ €์˜ ์ถœ๋ ฅ์€ ๋‹จ์ˆœํ•œ Python ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์–ป๋Š” ๊ฒƒ์€ ์‹ค์ œ๋กœ ํŠน๋ณ„ํ•œ BatchEncoding ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ•˜์œ„ ํด๋ž˜์Šค์ด์ง€๋งŒ(์ด๊ฒƒ์ด ์ด์ „์— ์šฐ๋ฆฌ๊ฐ€ ๋ฌธ์ œ์—†์ด ํ•ด๋‹น ๊ฒฐ๊ณผ๋ฅผ ์ƒ‰์ธํ™”ํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ์ด์œ ์ž…๋‹ˆ๋‹ค), ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ถ”๊ฐ€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ‘๋ ฌํ™”(parallelization) ๊ธฐ๋Šฅ ์™ธ์—๋„, ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €์˜ ์ฃผ์š” ๊ธฐ๋Šฅ์€ ์ตœ์ข… ํ† ํฐ์ด ์›๋ณธ ํ…์ŠคํŠธ์—์„œ ์–ด๋””์— ์œ„์น˜ํ•˜๋Š”์ง€ ๋ฒ”์œ„(span)๋ฅผ ํ•ญ์ƒ ์ถ”์ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์˜คํ”„์…‹ ๋งคํ•‘(offset mapping)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ ๋‹จ์–ด๋ฅผ ์ƒ์„ฑ๋œ ํ† ํฐ์— ๋งคํ•‘ํ•˜๊ฑฐ๋‚˜ ์›๋ณธ ํ…์ŠคํŠธ์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ๋‚ด๋ถ€ ํ† ํฐ์— ๋งคํ•‘ํ•˜๊ฑฐ๋‚˜ ๊ทธ ๋ฐ˜๋Œ€๋กœ ๋งคํ•‘ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ๋“ค์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") example = "My name is Sylvain and I work at Hugging Face in Brooklyn." encoding = tokenizer(example) print(type(encoding)) ์œ„์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด, ํ† ํฌ ๋‚˜์ด์ €์˜ ์ถœ๋ ฅ์—์„œ BatchEncoding ๊ฐ์ฒด๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. AutoTokenizer ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์„ ํƒํ•˜๋ฏ€๋กœ ์ด BatchEncoding ๊ฐ์ฒด๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ถ”๊ฐ€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋น ๋ฅธ์ง€ ๋Š๋ฆฐ์ง€ ํ™•์ธํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„ , ํ† ํฌ ๋‚˜์ด์ €์˜ is_fast ์†์„ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.is_fast ๋˜๋Š” encoding์˜ is_fast ์†์„ฑ์„ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค: encoding.is_fast ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ฐ€์ง€๊ณ  ์šฐ๋ฆฌ๊ฐ€ ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ด…์‹œ๋‹ค. ์ฒซ์งธ, ํ† ํฐ ์•„์ด๋””๋ฅผ ๋‹ค์‹œ ํ† ํฐ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์ง€ ์•Š๊ณ ๋„ ํ† ํฐ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: encoding.tokens() ์ด ๊ฒฝ์šฐ ์ธ๋ฑ์Šค 5์˜ ํ† ํฐ์€ ##yl์ด๋ฉฐ, ์ด๋Š” ์›๋ž˜ ๋ฌธ์žฅ์—์„œ "Sylvain"์ด๋ผ๋Š” ๋‹จ์–ด์˜ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ word_ids() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํ† ํฐ์ด ์œ ๋ž˜๋œ ํ•ด๋‹น ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: encoding.word_ids() ํ† ํฌ ๋‚˜์ด์ €์˜ ํŠน์ˆ˜ ํ† ํฐ [CLS] ๋ฐ [SEP]๊ฐ€ None์œผ๋กœ ๋งคํ•‘๋œ ๋‹ค์Œ, ๊ฐœ๋ณ„ ํ† ํฐ๋“ค์ด ํ•ด๋‹น ํ† ํฐ์ด ์œ ๋ž˜ํ•œ ๋‹จ์–ด์— ๋งคํ•‘๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๋‘ ๊ฐœ์˜ ํ† ํฐ์ด ๊ฐ™์€ ๋‹จ์–ด์— ์žˆ๋Š”์ง€ ์•„๋‹ˆ๋ฉด ํ† ํฐ์ด ๋‹จ์–ด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š”๋ฐ ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ## ์ ‘๋‘์‚ฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๋Š” BERT์™€ ๊ฐ™์€ ์œ ํ˜•์˜ ํ† ํฌ ๋‚˜์ด์ €์—์„œ๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์†๋„๊ฐ€ ๋น ๋ฅธ ๋ชจ๋“  ์œ ํ˜•์˜ ํ† ํฌ ๋‚˜์ด์ €์—์„œ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ๋Š” ์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹(NER) ๋ฐ ํ’ˆ์‚ฌ(POS) ํƒœ๊น…๊ณผ ๊ฐ™์€ ์ž‘์—…์—์„œ ๊ฐ ๋‹จ์–ด์— ํ•ด๋‹นํ•˜๋Š” ๋ ˆ์ด๋ธ”์„ ํ† ํฐ์— ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง(masked language modeling), ์ฆ‰ ์ „์ฒด ๋‹จ์–ด ๋งˆ์Šคํ‚น(whole word masking)์ด๋ผ๊ณ  ํ•˜๋Š” ๊ธฐ๋ฒ•์—์„œ, ๋™์ผํ•œ ๋‹จ์–ด์—์„œ ๋ถ„๋ฆฌ๋œ ๋ชจ๋“  ํ† ํฐ๋“ค์„ ๋งˆ์Šคํ‚น ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด๊ฐ€ ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•œ ๊ฐœ๋…์€ ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "I'll"("I will"์˜ ์ถ•์•ฝํ˜•)์€ ํ•˜๋‚˜์˜ ๋‹จ์–ด์ผ๊นŒ์š”? ์•„๋‹ˆ๋ฉด ๋‘ ๊ฐœ์ผ๊นŒ์š”? ์ด๋Š” ์‹ค์ œ๋กœ ํ† ํฌ ๋‚˜์ด์ €์™€ ์ ์šฉ๋˜๋Š” ์‚ฌ์ „ ํ† ํฐํ™”(pre-tokenization) ์ž‘์—…์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ผ๋ถ€ ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ๋ถ„ํ• ํ•˜๋ฏ€๋กœ ์ด๋ฅผ ํ•œ ๋‹จ์–ด๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ํ† ํฌ ๋‚˜์ด ์ €๋“ค์€ ๊ณต๋ฐฑ ์œ„์— ๊ตฌ๋‘์ ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋‘ ๋‹จ์–ด๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ํ† ํฐ์„ ๊ฐ€์ ธ์˜จ ๋ฌธ์žฅ์— ํ•ด๋‹น ํ† ํฐ์„ ๋งคํ•‘ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” sentence_ids() ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค(์ด ๊ฒฝ์šฐ ํ† ํฌ ๋‚˜์ด์ €์—์„œ ๋ฐ˜ํ™˜๋œ token_type_ids๊ฐ€ ๋™์ผํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค). ๋งˆ์ง€๋ง‰์œผ๋กœ word_to_chars() ๋˜๋Š” token_to_chars() ๋ฐ char_to_word() ๋˜๋Š” char_to_token() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ๋‹จ์–ด ๋˜๋Š” ํ† ํฐ์„ ์›๋ณธ ํ…์ŠคํŠธ์˜ ๋ฌธ์ž์— ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ทธ ๋ฐ˜๋Œ€๋กœ๋„ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, word_ids() ๋ฉ”์„œ๋“œ๋Š” ##yl์ด ์ธ๋ฑ์Šค 3์— ์žˆ๋Š” ๋‹จ์–ด์˜ ์ผ๋ถ€๋ผ๊ณ  ์•Œ๋ ค์คฌ์ง€๋งŒ ์ •ํ™•ํžˆ ๋ฌธ์žฅ ๋‚ด์—์„œ ์–ด๋–ค ๋‹จ์–ด์— ํ•ด๋‹นํ•˜๋Š” ๊ฑธ๊นŒ์š”? ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: start, end = encoding.word_to_chars(3) example[start:end] ์ด์ „์— ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ์ด ๋ชจ๋“  ๊ฒƒ์€ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์˜คํ”„์…‹(offset) ๋ชฉ๋ก์—์„œ ๊ฐ ํ† ํฐ์ด ๊ฐ€์ ธ์˜จ ํ…์ŠคํŠธ ๋ฒ”์œ„(span)๋ฅผ ์ถ”์ ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๊ตฌ๋™๋ฉ๋‹ˆ๋‹ค. ํ™œ์šฉ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ์œผ๋กœ token-classification ํŒŒ์ดํ”„๋ผ์ธ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ˆ˜๋™์œผ๋กœ ๋ณต์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. token-classification ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋‚ด๋ถ€ ๋™์ž‘ 1์žฅ์—์„œ ์šฐ๋ฆฌ๋Š” Transformers์˜ pipeline() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ํ…์ŠคํŠธ์˜ ์–ด๋Š ๋ถ€๋ถ„์ด ์‚ฌ๋žŒ(person), ์œ„์น˜(location) ๋˜๋Š” ์กฐ์ง(organization)๊ณผ ๊ฐ™์€ ์—”ํ„ฐํ‹ฐ(entities)์— ํ•ด๋‹นํ•˜๋Š”์ง€ ์‹๋ณ„ํ•˜๋Š” ์ž‘์—…์ธ NER์„ ์ฒ˜์Œ์œผ๋กœ ์‚ดํŽด๋ดค์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ 2์žฅ์—์„œ ํŒŒ์ดํ”„๋ผ์ธ์ด ์›์‹œ ํ…์ŠคํŠธ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์˜ˆ์ธกํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์„ธ ๋‹จ๊ณ„ ์ฆ‰, ํ† ํฐํ™”(tokenization), ๋ชจ๋ธ์„ ํ†ตํ•œ ์ž…๋ ฅ ์ „๋‹ฌ, ํ›„์ฒ˜๋ฆฌ(post-processing)๋ฅผ ์–ด๋–ป๊ฒŒ ๊ทธ๋ฃนํ™”ํ•˜๋Š”์ง€๋ฅผ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. token-classification ํŒŒ์ดํ”„๋ผ์ธ์˜ ์ฒ˜์Œ ๋‘ ๋‹จ๊ณ„๋Š” ๋‹ค๋ฅธ ํŒŒ์ดํ”„๋ผ์ธ๊ณผ ๋™์ผํ•˜์ง€๋งŒ ํ›„์ฒ˜๋ฆฌ(post-processing)๋Š” ์กฐ๊ธˆ ๋” ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ์‚ดํŽด๋ด…์‹œ๋‹ค! ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ๊ธฐ๋ณธ ์‹คํ–‰ ๊ฒฐ๊ณผ ๋„์ถœํ•˜๊ธฐ ๋จผ์ €, ์ˆ˜์ž‘์—…์œผ๋กœ ๋น„๊ตํ•  ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋„๋ก token-classification ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋ธ์€ dbmdz/bert-large-cased-finetuned-conll03-english์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ๋ฌธ์žฅ์— ๋Œ€ํ•ด NER๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค: from transformers import pipeline token_classifier = pipeline("token-classification") token_classifier("My name is Sylvain and I work at Hugging Face in Brooklyn.") ๋ชจ๋ธ์€ "Sylvain"์—์„œ ๋ถ„๋ฆฌ๋œ ๊ฐ ํ† ํฐ๋“ค์„ ๋ชจ๋‘ ์‚ฌ๋žŒ(person)์œผ๋กœ, "Hugging Face"์—์„œ ๋ถ„๋ฆฌ๋œ ๊ฐ ํ† ํฐ๋“ค์„ ๋ชจ๋‘ ์กฐ์ง(organization)์œผ๋กœ, "Brooklyn" ํ† ํฐ์„ ์œ„์น˜(location)๋กœ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์‹๋ณ„ํ–ˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์— ๋™์ผํ•œ ์—”ํ„ฐํ‹ฐ์— ํ•ด๋‹นํ•˜๋Š” ํ† ํฐ์„ ๊ทธ๋ฃนํ™”ํ•˜๋„๋ก ์š”์ฒญํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import pipeline token_classifier = pipeline("token-classification", aggregation_strategy="simple") token_classifier("My name is Sylvain and I work at Hugging Face in Brooklyn.") aggregation_strategy๋ฅผ ์œ„์™€ ๊ฐ™์ด ์ง€์ •ํ•˜๋ฉด ํ† ํฐ๋“ค์ด ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ง„ ์—”ํ„ฐํ‹ฐ์— ๋Œ€ํ•ด ์ƒˆ๋กญ๊ฒŒ ๊ณ„์‚ฐ๋œ ์Šค์ฝ”์–ด๋ฅผ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. "simple"์˜ ๊ฒฝ์šฐ ์Šค์ฝ”์–ด๋Š” ํ•ด๋‹น ๊ฐœ์ฒด๋ช… ๋‚ด์˜ ๊ฐ ํ† ํฐ์— ๋Œ€ํ•œ ์Šค์ฝ”์–ด์˜ ํ‰๊ท ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "Sylvain"์˜ ์Šค์ฝ”์–ด๋Š” ์ด์ „ ์˜ˆ์—์„œ S, ##yl, ##va ๋ฐ ##in ํ† ํฐ์— ๋Œ€ํ•ด ๊ณ„์‚ฐ๋œ ์Šค์ฝ”์–ด์˜ ํ‰๊ท ์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋‹ค๋ฅธ ์ง€์ •์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: "first", ์—ฌ๊ธฐ์„œ ๊ฐ ๊ฐœ์ฒด ๋ช…์˜ ์Šค์ฝ”์–ด๋Š” ํ•ด๋‹น ๊ฐœ์ฒด ๋ช…์˜ ์ฒซ ๋ฒˆ์งธ ํ† ํฐ์˜ ์Šค์ฝ”์–ด์ž…๋‹ˆ๋‹ค(๋”ฐ๋ผ์„œ "Sylvain"์˜ ๊ฒฝ์šฐ ํ† ํฐ S์˜ ์ ์ˆ˜์ธ 0.993828์ด ๋จ). "max", ์—ฌ๊ธฐ์„œ ๊ฐ ์—”ํ„ฐํ‹ฐ์˜ ์Šค์ฝ”์–ด๋Š” ํ•ด๋‹น ์—”ํ„ฐํ‹ฐ๋‚ด์˜ ํ† ํฐ๋“ค ์ค‘์˜ ์ตœ๋Œ“๊ฐ’ ์Šค์ฝ”์–ด์ž…๋‹ˆ๋‹ค("Hugging Face"์˜ ๊ฒฝ์šฐ "Face"์˜ ์ ์ˆ˜๋Š” 0.98879766์ด ๋จ). "average", ์—ฌ๊ธฐ์„œ ๊ฐ ํ•ญ๋ชฉ์˜ ์Šค์ฝ”์–ด๋Š” ํ•ด๋‹น ํ•ญ๋ชฉ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋‹จ์–ด(ํ† ํฐ์ด ์•„๋‹™๋‹ˆ๋‹ค) ์Šค์ฝ”์–ด์˜ ํ‰๊ท ์ž…๋‹ˆ๋‹ค(๋”ฐ๋ผ์„œ "Sylvain"์˜ ๊ฒฝ์šฐ "simple" ์ง€์ •์ž์™€ ์ฐจ์ด๊ฐ€ ์—†์ง€๋งŒ "Hugging Face"์˜ ์ ์ˆ˜๋Š” 0.9819์ด๋ฉฐ "Hugging"์€ 0.975์ด๊ณ  "Face"๋Š” 0.98879์ž…๋‹ˆ๋‹ค). ์ด์ œ pipeline() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ด๋Ÿฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ์ž…๋ ฅ(inputs)์—์„œ ์˜ˆ์ธก(predictions)๊นŒ์ง€ ๋จผ์ € ์ž…๋ ฅ์„ ํ† ํฐํ™”ํ•˜๊ณ  ๋ชจ๋ธ์„ ํ†ตํ•ด ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” 2์žฅ์—์„œ ์„ค๋ช…ํ•œ ๋‚ด์šฉ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. AutoXxx ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฌ ๋‚˜์ด์ €์™€ ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•œ ํ›„์— ์ด๋ฅผ ์˜ˆ์ œ์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoTokenizer, AutoModelForTokenClassification model_checkpoint = "dbmdz/bert-large-cased-finetuned-conll03-english" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) model = AutoModelForTokenClassification.from_pretrained(model_checkpoint) example = "My name is Sylvain and I work at Hugging Face in Brooklyn." inputs = tokenizer(example, return_tensors="pt") outputs = model(**inputs) ์—ฌ๊ธฐ์—์„œ AutoModelForTokenClassification์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ฐ ํ† ํฐ์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ logits ์„ธํŠธ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค: print(inputs["input_ids"].shape) print(outputs.logits.shape) 19๊ฐœ์˜ ํ† ํฐ์œผ๋กœ ๊ตฌ์„ฑ๋œ 1๊ฐœ์˜ ์‹œํ€€์Šค๊ฐ€ ์žˆ๋Š” ๋ฐฐ์น˜(batch)๊ฐ€ ์žˆ๊ณ  ๋ชจ๋ธ์—๋Š” 9๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋ ˆ์ด๋ธ”์ด ์กด์žฌํ•˜๋ฏ€๋กœ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์€ 1 x 19 x 9์˜ ๋ชจ์–‘์„ ๊ฐ–์Šต๋‹ˆ๋‹ค. text-classification ํŒŒ์ดํ”„๋ผ์ธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ softmax ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น logits์„ ํ™•๋ฅ ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  argmax๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(softmax๋Š” ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— logits์— ๋Œ€ํ•ด์„œ argmax๋ฅผ ์ทจํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค): import torch probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)[0].tolist() predictions = outputs.logits.argmax(dim=-1)[0].tolist() print(probabilities) print(predictions) model.config.id2label ์†์„ฑ์—๋Š” ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค ๋งคํ•‘์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค: model.config.id2label ์œ„์—์„œ ๋ณด๋“ฏ์ด ์ด 9๊ฐœ์˜ ๋ ˆ์ด๋ธ”์ด ์žˆ์Šต๋‹ˆ๋‹ค. O๋Š” ๊ฐœ์ฒด๋ช…์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ํ† ํฐ์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ”("outside"๋ฅผ ๋‚˜ํƒ€๋ƒ„)์ด๊ณ  ๊ฐ ๊ฐœ์ฒด๋ช… ์œ ํ˜•, ์ฆ‰ ๊ธฐํƒ€(miscellaneous), ์ธ๋ช…(person), ๊ธฐ๊ด€๋ช…(organization), ์ง€๋ช…(location) ๊ฐ๊ฐ์— ๋Œ€ํ•ด ๋‘ ๊ฐœ์˜ ๋ ˆ์ด๋ธ”์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ” B-XXX๋Š” ํ† ํฐ์ด ๊ฐœ์ฒด๋ช… XXX์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๊ณ , ๋ ˆ์ด๋ธ” I-XXX๋Š” ํ† ํฐ์ด ๊ฐœ์ฒด๋ช… XXX์˜ ๋‚ด๋ถ€์— ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ด์ „ ์˜ˆ์‹œ์—์„œ ์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ์ด ํ† ํฐ "S"๋ฅผ B-PER(์ธ๋ช… ๊ฐœ์ฒด ๋ช…์˜ ์‹œ์ž‘)์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ  "##yl", "##va" ๋ฐ "##in" ํ† ํฐ์„ I-PER(์ธ๋ช… ๊ฐœ์ฒด ๋ช…์˜ ๋‚ด๋ถ€)๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ๋ฅผ ๊ธฐ๋Œ€ํ–ˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œ„ ๊ฒฐ๊ณผ์—์„œ ๋ณด๋Š” ๋ฐ”์™€ ๊ฐ™์ด 4๊ฐœ ํ† ํฐ ๋ชจ๋‘์— I-PER์ด๋ผ๋Š” ๋ ˆ์ด๋ธ”์„ ๋ถ€์—ฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์ธก์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ๋ ‡์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ด๋Ÿฌํ•œ B- ๋ฐ I- ๋ ˆ์ด๋ธ” ํ‘œ๊ธฐ ๋ฐฉ์‹์—๋Š” IOB1 ๋ฐ IOB2์˜ ๋‘ ๊ฐ€์ง€<NAME>์ด ์žˆ์Šต๋‹ˆ๋‹ค. IOB2<NAME>(์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ๋ถ„ํ™์ƒ‰ ํƒœ๊ทธ)์€ ์šฐ๋ฆฌ๊ฐ€ ๋„์ž…ํ•œ<NAME>์ธ ๋ฐ˜๋ฉด, IOB1<NAME>(ํŒŒ๋ž€์ƒ‰ ํƒœ๊ทธ)์—์„œ B-๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ ˆ์ด๋ธ”์€ ๋™์ผํ•œ ์œ ํ˜•์˜ ์ธ์ ‘ํ•œ ๋‘ ์—”ํ„ฐํ‹ฐ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐ๋งŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์€ IOB1<NAME>์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ ์…‹์—์„œ ๋ฏธ์„ธ ์กฐ์ •๋˜์—ˆ์œผ๋ฏ€๋กœ "S" ํ† ํฐ์— ๋ ˆ์ด๋ธ” I-PER์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด ๋งต์„ ์‚ฌ์šฉํ•˜์—ฌ token-classification ํŒŒ์ดํ”„๋ผ์ธ์˜ ๊ฒฐ๊ณผ๋ฅผ (๊ฑฐ์˜ ์™„์ „ํžˆ) ์žฌํ˜„ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. O๋กœ ๋ถ„๋ฅ˜๋˜์ง€ ์•Š์€ ๊ฐ ํ† ํฐ์˜ ์ ์ˆ˜์™€ ๋ ˆ์ด๋ธ”๋งŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: results = [] tokens = inputs.tokens() for idx, pred in enumerate(predictions): label = model.config.id2label[pred] if label != "O": results.append( {"entity": label, "score": probabilities[idx][pred], "word": tokens[idx]} ) print(results) ์ด ๊ฒฐ๊ณผ๋Š” ์ด์ „ ๊ฒฐ๊ณผ์™€ ๋งค์šฐ ์œ ์‚ฌํ•˜์ง€๋งŒ ํ•œ ๊ฐ€์ง€ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์€ ๋˜ํ•œ ์›๋ณธ ๋ฌธ์žฅ์—์„œ ๊ฐ ์—”ํ„ฐํ‹ฐ์˜ ์‹œ์ž‘๊ณผ ๋์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ์˜คํ”„์…‹ ๋งคํ•‘(offset mapping)์ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์˜คํ”„์…‹(offset)์„ ์–ป์œผ๋ ค๋ฉด ์ž…๋ ฅ์— ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ ์šฉํ•  ๋•Œ return_offsets_mapping=True๋ฅผ ์„ค์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: inputs_with_offsets = tokenizer(example, return_offsets_mapping=True) inputs_with_offsets["offset_mapping"] ๊ฐ ํŠœํ”Œ์€ ๊ฐ ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ํ…์ŠคํŠธ ๋ฒ”์œ„์ด๋ฉฐ, ์—ฌ๊ธฐ์„œ (0, 0)์€ ํŠน์ˆ˜ ํ† ํฐ์šฉ์œผ๋กœ ์˜ˆ์•ฝ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ์ธ๋ฑ์Šค 5์˜ ํ† ํฐ์ด "##yl"์ด๊ณ  ํ•ด๋‹น ์˜คํ”„์…‹์ด (12, 14)๋กœ ์ง€์ •๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด ์˜คํ”„์…‹์œผ๋กœ ์Šฌ๋ผ์ด์‹ฑ์„ ํ•˜๋ฉด: example[12:14] '##' ์—†์ด ์ ์ ˆํ•œ ํ…์ŠคํŠธ ๋ฒ”์œ„(text span)๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ œ ์ด์ „ ๊ฒฐ๊ณผ์˜ ์žฌํ˜„(reproduction)์„ ์™„๋ฃŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: results = [] inputs_with_offsets = tokenizer(example, return_offsets_mapping=True) tokens = inputs_with_offsets.tokens() offsets = inputs_with_offsets["offset_mapping"] for idx, pred in enumerate(predictions): label = model.config.id2label[pred] if label != 'O': start, end = offsets[idx] results.append( { "entity": label, "score": probabilities[idx][pred], "word": tokens[idx], "start": start, "end": end, } ) print(results) ์ด ๊ฒฐ๊ณผ๋Š” ์šฐ๋ฆฌ๊ฐ€ ํŒŒ์ดํ”„๋ผ์ธ ์‹คํ–‰์„ ํ†ตํ•ด ์–ป์€ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค! ์—”ํ„ฐํ‹ฐ ๊ทธ๋ฃนํ™” ์˜คํ”„์…‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์—”ํ„ฐํ‹ฐ์˜ ์‹œ์ž‘ ๋ฐ ๋ ํ‚ค๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์€ ํŽธ๋ฆฌํ•˜์ง€๋งŒ ํ•ด๋‹น ์ •๋ณด๊ฐ€ ๊ผญ ํ•„์š”ํ•œ ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—”ํ„ฐํ‹ฐ ํ† ํฐ์„ ๊ทธ๋ฃนํ™”ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ์˜คํ”„์…‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์ง€์ €๋ถ„ํ•œ ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Hu, ##gging ๋ฐ Face ํ† ํฐ์„ ํ•˜๋‚˜๋กœ ๊ทธ๋ฃนํ™”ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ์ฒ˜์Œ ๋‘ ํ† ํฐ์€ ## ์—†์ด ํ•ฉ์ณ์•ผ ํ•˜๊ณ  Face๋Š” ##๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์•ž์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ฒฐํ•ฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ํŠน์ˆ˜ ๊ทœ์น™์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ๊ทœ์น™๋“ค์€ ํŠน์ • ์œ ํ˜•์˜ ํ† ํฌ ๋‚˜์ด์ €์—์„œ๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. SentencePiece ๋˜๋Š” Byte-Pair-Encoding ํ† ํฌ ๋‚˜์ด์ €(์ด ์žฅ์˜ ๋’ท๋ถ€๋ถ„์—์„œ ์„ค๋ช…)์—์„œ๋Š” ๋˜ ๋‹ค๋ฅธ ๊ทœ์น™ ์ง‘ํ•ฉ์„ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜คํ”„์…‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ์‚ฌ์šฉ์ž ์ •์˜ ์ฝ”๋“œ๊ฐ€ ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ๋‹จ์ง€ ์ฒซ ๋ฒˆ์งธ ํ† ํฐ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ํ† ํฐ์œผ๋กœ ๋๋‚˜๋Š” ์›๋ณธ ํ…์ŠคํŠธ์˜ ๋ฒ”์œ„๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Hu, ##gging ๋ฐ Face ํ† ํฐ์˜ ๊ฒฝ์šฐ, ์ด๋ฅผ ํ•ฉ์น˜๊ธฐ ์œ„ํ•ด์„œ 33๋ฒˆ์งธ ๋ฌธ์ž(Hu์˜ ์‹œ์ž‘ ๋ถ€๋ถ„)์—์„œ ์‹œ์ž‘ํ•˜์—ฌ 45๋ฒˆ์งธ ๋ฌธ์ž(Face์˜ ๋๋ถ€๋ถ„) ์•ž๊นŒ์ง€ ์Šฌ๋ผ์ด์‹ฑ์„ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค: example[33:45] ํŠน์ • ์—”ํ„ฐํ‹ฐ์— ํฌํ•จ๋œ ํ† ํฐ๋“ค์„ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋™์•ˆ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ํ›„์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด B-XXX ๋˜๋Š” I-XXX๋กœ ๋ ˆ์ด๋ธ”์ด ์ง€์ •๋  ์ˆ˜ ์žˆ๋Š” ์ฒซ ๋ฒˆ์งธ ์—”ํ„ฐํ‹ฐ๋ฅผ ์ œ์™ธํ•˜๊ณ  ์—ฐ์†์ ์ด๊ณ  I-XXX๋กœ ๋ ˆ์ด๋ธ”์ด ์ง€์ •๋œ ์—”ํ„ฐํ‹ฐ๋ฅผ ํ•จ๊ป˜ ๊ทธ๋ฃนํ™”ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ O, ์ƒˆ๋กœ์šด ์œ ํ˜•์˜ ์—”ํ„ฐํ‹ฐ ๋˜๋Š” ๋™์ผํ•œ ์œ ํ˜•์˜ ์—”ํ„ฐํ‹ฐ๊ฐ€ ์‹œ์ž‘๋˜๊ณ  ์žˆ์Œ์„ ์•Œ๋ฆฌ๋Š” B-XXX๋ฅผ ๋ฐ›์œผ๋ฉด ์—”ํ„ฐํ‹ฐ ๊ทธ๋ฃนํ™”๋ฅผ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค: import numpy as np results = [] inputs_with_offsets = tokenizer(example, return_offsets_mapping=True) tokens = inputs_with_offsets.tokens() offsets = inputs_with_offsets["offset_mapping"] idx = 0 while idx < len(predictions): pred = predictions[idx] label = model.config.id2label[pred] if label != "O": # Remove the B- or I- label = label[2:] start, _ = offsets[idx] # Grab all the tokens labeled with I-label all_scores = [] while ( idx < len(predictions) and model.config.id2label[predictions[idx]] == f"I-{label}" ): all_scores.append(probabilities[idx][pred]) _, end = offsets[idx] idx += 1 # The score is the mean of all the scores of the tokens in that grouped entity score = np.mean(all_scores).item() word = example[start:end] results.append( { "entity_group": label, "score": score, "word": word, "start": start, "end": end, } ) idx += 1 print(results) ์ด๋ ‡๊ฒŒ ํŒŒ์ดํ”„๋ผ์ธ ์‹คํ–‰ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค! ์ด ์˜คํ”„์…‹์ด ๋งค์šฐ ์œ ์šฉํ•˜๊ฒŒ ํ™œ์šฉ๋˜๋Š” ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋Š” ์งˆ์˜์‘๋‹ต(question answering)์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ์„น์…˜์—์„œ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ž์„ธํžˆ ๊ณต๋ถ€ํ•˜๋ฉด์„œ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๋Š” ํ† ํฌ ๋‚˜์ด์ €์˜ ๋งˆ์ง€๋ง‰ ๊ธฐ๋Šฅ์ธ ์ž…๋ ฅ์„ ์ฃผ์–ด์ง„ ๊ธธ์ด๋กœ ์ž๋ฅผ ๋•Œ ๋„˜์น˜๋Š” ํ† ํฐ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3. QA ํŒŒ์ดํ”„๋ผ์ธ์—์„œ์˜ "๋น ๋ฅธ(fast)" ํ† ํฌ ๋‚˜์ด์ € ์ด์ œ question-answering ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ดํŽด๋ณด๊ณ , ์ด์ „ ์„น์…˜์—์„œ ๊ทธ๋ฃนํ™”๋œ ์—”ํ„ฐํ‹ฐ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ˆ˜ํ–‰ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์˜คํ”„์…‹์„ ํ™œ์šฉํ•˜์—ฌ ์ปจํ…์ŠคํŠธ์—์„œ ์ž…๋ ฅ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ง์ ‘ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ ˆ๋‹จ(truncation) ๋  ์ˆ˜๋ฐ–์— ์—†๋Š” ๋งค์šฐ ๊ธด ์ปจํ…์ŠคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์งˆ์˜์‘๋‹ต(question answering) ์ž‘์—…์— ๊ด€์‹ฌ์ด ์—†๋‹ค๋ฉด ์ด ์„น์…˜์„ ๊ฑด๋„ˆ๋›ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. question-answering ํŒŒ์ดํ”„๋ผ์ธ ์‚ฌ์šฉํ•˜๊ธฐ 1์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ์šฐ๋ฆฌ๋Š” ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์„ ์–ป๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ question-answering ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import pipeline question_answerer = pipeline("question-answering") context = """ Transformers is backed by the three most popular deep learning libraries โ€” Jax, PyTorch, and TensorFlow โ€” with a seamless integration between them. It's straightforward to train your models with one before loading them for inference with the other. """ question = "Which deep learning libraries back Transformers?" question_answerer(question=question, context=context) ๋ชจ๋ธ์ด ํ—ˆ์šฉํ•˜๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋ณด๋‹ค ๊ธด ํ…์ŠคํŠธ๋ฅผ ์ž๋ฅด๊ฑฐ๋‚˜ ๋ถ„ํ• ํ•  ์ˆ˜ ์—†๋Š”(๋”ฐ๋ผ์„œ ๋ฌธ์„œ ๋์— ์žˆ๋Š” ์ •๋ณด๋ฅผ ๋†“์น  ์ˆ˜ ์žˆ๋Š”) ๋‹ค๋ฅธ ํŒŒ์ดํ”„๋ผ์ธ๊ณผ ๋‹ฌ๋ฆฌ, ์ด ํŒŒ์ดํ”„๋ผ์ธ์€ ๋งค์šฐ ๊ธด ์ปจํ…์ŠคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์ด ์ปจํ…์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰์— ์žˆ๋”๋ผ๋„ ๊ทธ ๋‹ต๋ณ€์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: long_context = """ Transformers: State of the Art NLP Transformers provides thousands of pretrained models to perform tasks on texts such as classification, information extraction, question answering, summarization, translation, text generation and more in over 100 languages. Its aim is to make cutting-edge NLP easier to use for everyone. Transformers provides APIs to quickly download and use those pretrained models on a given text, fine-tune them on your own datasets and then share them with the community on our model hub. At the same time, each python module defining an architecture is fully standalone and can be modified to enable quick research experiments. Why should I use transformers? 1. Easy-to-use state-of-the-art models: - High performance on NLU and NLG tasks. - Low barrier to entry for educators and practitioners. - Few user-facing abstractions with just three classes to learn. - A unified API for using all our pretrained models. - Lower compute costs, smaller carbon footprint: 2. Researchers can share trained models instead of always retraining. - Practitioners can reduce compute time and production costs. - Dozens of architectures with over 10,000 pretrained models, some in more than 100 languages. 3. Choose the right framework for every part of a model's lifetime: - Train state-of-the-art models in 3 lines of code. - Move a single model between TF2.0/PyTorch frameworks at will. - Seamlessly pick the right framework for training, evaluation and production. 4. Easily customize a model or an example to your needs: - We provide examples for each architecture to reproduce the results published by its original authors. - Model internals are exposed as consistently as possible. - Model files can be used independently of the library for quick experiments. Transformers is backed by the three most popular deep learning libraries โ€” Jax, PyTorch and TensorFlow โ€” with a seamless integration between them. It's straightforward to train your models with one before loading them for inference with the other. """ question_answerer(question=question, context=long_context) ์ด ๋ชจ๋“  ์ž‘์—…์„ ์–ด๋–ป๊ฒŒ ์ˆ˜ํ–‰ํ•˜๋Š”์ง€ ๋ด…์‹œ๋‹ค! ์งˆ์˜์‘๋‹ต์„ ์œ„ํ•œ ์‚ฌ์ „ํ•™์Šต ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ๋‹ค๋ฅธ ํŒŒ์ดํ”„๋ผ์ธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์šฐ์„  ์ž…๋ ฅ์„ ํ† ํฐํ™”ํ•œ ๋‹ค์Œ ๋ชจ๋ธ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. question-answering ํŒŒ์ดํ”„๋ผ์ธ์— ๋””ํดํŠธ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ฒดํฌํฌ์ธํŠธ๋Š” distillbert-base-cased-distilled-squad์ž…๋‹ˆ๋‹ค. ์ฒดํฌํฌ์ธํŠธ ์ด๋ฆ„ ๋‚ด์˜ "squad"๋Š” ๋ชจ๋ธ์ด ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ช…์นญ์ž…๋‹ˆ๋‹ค. 7์žฅ์—์„œ SQuAD ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•ด ๋” ์ด์•ผ๊ธฐํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: from transformers import AutoTokenizer, AutoModelForQuestionAnswering model_checkpoint = "distilbert-base-cased-distilled-squad" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) model = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint) inputs = tokenizer(question, context, return_tensors="pt") outputs = model(**inputs) ์œ„ ์ฝ”๋“œ์—์„œ ์งˆ๋ฌธ๊ณผ ์ปจํ…์ŠคํŠธ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์น˜์‹œ์ผœ ์Œ(pair)์œผ๋กœ ํ† ํฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ๋น ๋ฅผ ๊ฒ๋‹ˆ๋‹ค. ์งˆ์˜์‘๋‹ต ๋ชจ๋ธ์€ ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ๋ชจ๋ธ๊ณผ ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์„ ์˜ˆ์‹œ๋กœ ๋ณด๋ฉด, ๋ชจ๋ธ์€ ์ •๋‹ต ์‹œ์ž‘ ํ† ํฐ์˜ ์ธ๋ฑ์Šค(์—ฌ๊ธฐ์„œ๋Š” 21)์™€ ์ •๋‹ต ๋งˆ์ง€๋ง‰ ํ† ํฐ์˜ ์ธ๋ฑ์Šค(์—ฌ๊ธฐ์„œ๋Š” 24)๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์ด ํ•˜๋‚˜์˜ ๋กœ์ง“(logits) ํ…์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ  ๋‘ ๊ฐœ์˜ ํ…์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ์ •๋‹ต์˜ ์‹œ์ž‘ ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ๋กœ์ง“(logit)์ด๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ์ •๋‹ต์˜ ๋งˆ์ง€๋ง‰ ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ๋กœ์ง“(logit)์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ 66๊ฐœ์˜ ํ† ํฐ์ด ํฌํ•จ๋œ ์ž…๋ ฅ์ด ํ•˜๋‚˜๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ ๋‹ค์Œ์„ ์–ป์Šต๋‹ˆ๋‹ค: start_logits = outputs.start_logits end_logits = outputs.end_logits print(start_logits.shape, end_logits.shape) ์ด๋Ÿฌํ•œ ๋กœ์ง“๋“ค์„ ํ™•๋ฅ ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด softmax ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•ด์•ผ ํ•˜๋‚˜, ๊ทธ์ „์— ์ปจํ…์ŠคํŠธ(context)๊ฐ€ ์•„๋‹Œ ํ† ํฐ ์ธ๋ฑ์Šค๋ฅผ ๋งˆ์Šคํ‚น(masking) ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ์ด [CLS] question [SEP] context [SEP]์ด๋ฏ€๋กœ ์งˆ๋ฌธ์— ํฌํ•จ๋œ ํ† ํฐ๊ณผ [SEP] ํ† ํฐ์„ ๋งˆ์Šคํ‚น ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ผ๋ถ€ ๋ชจ๋ธ์—์„œ๋Š” ์ปจํ…์ŠคํŠธ์— ๋‹ต์ด ์—†์Œ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ [CLS] ํ† ํฐ์€ ๋งˆ์Šคํ‚น ํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋‚˜์ค‘์— softmax๋ฅผ ์ ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์Šคํ‚น(masking) ํ•˜๋ ค๋Š” ๋กœ์ง“์„ ํฐ ์Œ์ˆ˜๋กœ ๋ฐ”๊พธ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” -10000์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: import torch sequence_ids = inputs.sequence_ids() # ์ปจํ…์ŠคํŠธ ํ† ํฐ๋“ค์„ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋‘ ๋งˆ์Šคํ‚น ํ•œ๋‹ค. mask = [i != 1 for i in sequence_ids] # [CLS] ํ† ํฐ์€ ๋งˆ์Šคํ‚น ํ•˜์ง€ ์•Š๋Š”๋‹ค. mask[0] = False mask = torch.tensor(mask)[None] start_logits[mask] = -10000 end_logits[mask] = -10000 ์ด์ œ ์˜ˆ์ธกํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” ๋กœ์ง“์„ ์ ์ ˆํ•˜๊ฒŒ ๋งˆ์Šคํ‚น ํ–ˆ์œผ๋ฏ€๋กœ softmax๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: start_probabilities = torch.nn.functional.softmax(start_logits, dim=-1)[0] end_probabilities = torch.nn.functional.softmax(end_logits, dim=-1)[0] ์ด ๋‹จ๊ณ„์—์„œ ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ ํ™•๋ฅ ์˜ argmax๋ฅผ ์ทจํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์‹œ์ž‘ ์ธ๋ฑ์Šค๊ฐ€ ์ข…๋ฃŒ ์ธ๋ฑ์Šค๋ณด๋‹ค ํด ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฐฉ ์กฐ์น˜๋ฅผ ๋” ์ทจํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. start_index <= end_index๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฐ€๋Šฅํ•œ start_index ๋ฐ end_index์˜ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•œ ๋‹ค์Œ ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ํŠœํ”Œ (start_index, end_index)์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. "The answer starts at start_index" ๋ฐ "The answer ends at end_index" ์ด๋ฒคํŠธ๊ฐ€ ๋…๋ฆฝ์ ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•  ๋•Œ, ๋‹ต๋ณ€์ด start_index์—์„œ ์‹œ์ž‘ํ•˜์—ฌ end_index์—์„œ ๋๋‚  ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: t r p o a i i i s [ t r i d x ] e d r b b l t e [ n i ๋”ฐ๋ผ์„œ ๋ชจ๋“  ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋ ค๋ฉด start_index <= end_index์„ ๋งŒ์กฑํ•˜๋Š” ๋ชจ๋“  t r p o a i i i s [ t r i d x ] e d r b b l t e [ n i d x ] ๊ณฑ์„ ๊ณ„์‚ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์šฐ์„ , ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ณฑ์„ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค: scores = start_probabilities[:, None] * end_probabilities[None, :] ๊ทธ๋Ÿฐ ๋‹ค์Œ start_index > end_index๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’๋“ค์„ 0์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ๊ฐ’์„ ๋งˆ์Šคํ‚น ํ•ฉ๋‹ˆ๋‹ค(๋‹ค๋ฅธ ํ™•๋ฅ ์€ ๋ชจ๋‘ ์–‘์ˆ˜์ž„). torch.triu() ํ•จ์ˆ˜๋Š” ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ 2D ํ…์„œ์˜ ์œ„์ชฝ ์‚ผ๊ฐํ˜• ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ํ•ด๋‹น ๋งˆ์Šคํ‚น์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: scores = torch.triu(scores) ์ด์ œ ์ตœ๋Œ“๊ฐ’์˜ ์ธ๋ฑ์Šค๋งŒ ๊ตฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. PyTorch๋Š” ํ‰ํƒ„ํ™”๋œ ํ…์„œ(flattened tensor)์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ๋‚˜๋จธ์ง€ ์—†๋Š” ๋‚˜๋ˆ„๊ธฐ, // ์™€ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ, %์„ ์‚ฌ์šฉํ•˜์—ฌ start_index ๋ฐ end_index๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค: max_index = scores.argmax().item() start_index = max_index // scores.shape[1] end_index = max_index % scores.shape[1] print(scores[start_index, end_index]) ์•„์ง ์™„๋ฃŒ๋˜์ง€ ์•Š์•˜์ง€๋งŒ ์ ์–ด๋„ ์ถ”์ถœ๋œ ์‘๋‹ต์— ๋Œ€ํ•œ ์ •ํ™•ํ•œ ์ ์ˆ˜๋Š” ๊ณ„์‚ฐํ–ˆ์Šต๋‹ˆ๋‹ค(์ด์ „ ์„น์…˜์˜ ์ฒซ ๋ฒˆ์งธ ๊ฒฐ๊ณผ์™€ ๋น„๊ตํ•˜์—ฌ ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค). โœ Try it out! ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ 5๊ฐœ์˜ ์‘๋‹ต์— ๋Œ€ํ•œ ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. ์‘๋‹ต๋“ค์˜ ํ† ํฐ ๋‹จ์œ„ start_index ๋ฐ end_index๋ฅผ ๊ตฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด์ œ ์ปจํ…์ŠคํŠธ ๋‚ด์—์„œ์˜ ๋ฌธ์ž ๋‹จ์œ„ ์ธ๋ฑ์Šค๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ์˜คํ”„์…‹(offset)์ด ๋งค์šฐ ์œ ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ† ํฐ ๋ถ„๋ฅ˜(token classification) ์ž‘์—…์—์„œ์ฒ˜๋Ÿผ ์ด๋“ค ์ธ๋ฑ์Šค๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: inputs_with_offsets = tokenizer(question, context, return_offsets_mapping=True) offsets = inputs_with_offsets["offset_mapping"] start_char, _ = offsets[start_index] _, end_char = offsets[end_index] answer = context[start_char:end_char] ์ด์ œ ๊ฒฐ๊ณผ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ถœ๋ ฅ<NAME>์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: result = { "answer": answer, "start": start_char, "end": end_char, "score": scores[start_index, end_index] } print(result) ์ข‹์Šต๋‹ˆ๋‹ค! ์œ„ ๊ฒฐ๊ณผ๋Š” ์•ž์—์„œ ์‹คํ–‰ํ–ˆ๋˜ ํŒŒ์ดํ”„๋ผ์ธ์˜ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. โœ Try it out! ์ด์ „์— ๊ณ„์‚ฐํ•œ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋Šฅ์„ฑ์ด ๊ฐ€์žฅ ๋†’์€ 5๊ฐœ์˜ ์‘๋‹ต์„ ํ‘œ์‹œํ•ด ๋ด…์‹œ๋‹ค. ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด์ „์˜ ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ๋Œ์•„๊ฐ€์„œ ํ˜ธ์ถœํ•  ๋•Œ top_k=5๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ธธ์ด๊ฐ€ ๊ธด ์ปจํ…์ŠคํŠธ ๋‹ค๋ฃจ๊ธฐ ์œ„์—์„œ ์˜ˆ์ œ๋กœ ์‚ฌ์šฉํ•œ ์งˆ๋ฌธ ๋ฐ ๊ธธ์ด๊ฐ€ ๊ธด ์ปจํ…์ŠคํŠธ๋ฅผ ํ† ํฐ ํ™”ํ•ด ๋ณด๋ฉด question-answering ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ์‚ฌ์šฉ๋œ ์ตœ๋Œ€ ๊ธธ์ด(384)๋ณด๋‹ค ๋” ๋งŽ์€ ํ† ํฐ๋“ค์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค: inputs = tokenizer(question, long_context) print(len(inputs["input_ids"])) ๋”ฐ๋ผ์„œ ์ตœ๋Œ€ ๊ธธ์ด๋งŒํผ ์ž…๋ ฅ์„ ์ ˆ๋‹จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์ง€๋งŒ ์šฐ์„ ์ฃผ์˜ํ•ด์•ผ ํ•  ๊ฒƒ์€ ์งˆ๋ฌธ์„ ์ ˆ๋‹จํ•ด์„œ๋Š” ์•ˆ ๋˜๊ณ  ์ปจํ…์ŠคํŠธ๋งŒ ์ ˆ๋‹จํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ปจํ…์ŠคํŠธ๋Š” ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด๋ฏ€๋กœ "only_second" ์ ˆ๋‹จ ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๋Š” ์งˆ๋ฌธ์— ๋Œ€ํ•œ ์ •๋‹ต์ด ์ž˜๋ ค ๋‚˜๊ฐ„ ์ปจํ…์ŠคํŠธ์— ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜ ์˜ˆ์‹œ์—์„œ ์ •๋‹ต์ด ์ปจํ…์ŠคํŠธ์˜ ๋๋ถ€๋ถ„์— ์žˆ๋Š” ์งˆ๋ฌธ์„ ์ž…๋ ฅํ–ˆ๋‹ค๋ฉด, ํ•ด๋‹น ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค: inputs = tokenizer(question, long_context, max_length=384, truncation="only_second") print(tokenizer.decode(inputs["input_ids"])) ์ด๊ฒƒ์€ ๋ชจ๋ธ์ด ์ •๋‹ต์„ ์„ ํƒํ•˜๋Š” ๋ฐ ์–ด๋ ค์›€์„ ๊ฒช์„ ๊ฒƒ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด question-answering ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ปจํ…์ŠคํŠธ๋ฅผ ๋” ์ž‘์€ ์ฒญํฌ๋กœ ๋ถ„ํ• ํ•˜์—ฌ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ์ปจํ…์ŠคํŠธ๋ฅผ ์ž˜๋ชป๋œ ์œ„์น˜์—์„œ ๋ถ„ํ• ํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ์ฒญํฌ ์‚ฌ์ด์— ์•ฝ๊ฐ„์˜ ๊ฒน์นจ(overlap)๋„ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. return_overflowing_tokens=True๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ† ํฌ ๋‚˜์ด์ €("๋น ๋ฅธ" ๋˜๋Š” "๋Š๋ฆฐ")๊ฐ€ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ stride ์ธ์ˆ˜๋กœ ์›ํ•˜๋Š” ๊ฒน์นจ ์ •๋„๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๊ธธ์ด๊ฐ€ ๋น„๊ต์  ์งง์€ ๋ฌธ์žฅ์„ ์ด์šฉํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค: sentence = "This sentence is not too long but we are going to split it anyway." inputs = tokenizer( sentence, truncation=True, return_overflowing_tokens=True, max_length=6, stride=2 ) for ids in inputs["input_ids"]: print(tokenizer.decode(ids)) ์šฐ๋ฆฌ๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, ์ž…๋ ฅ ๋ฌธ์žฅ์€ inputs["input_ids"]์˜ ๊ฐ ํ•ญ๋ชฉ์ด ์ตœ๋Œ€ 6๊ฐœ์˜ ํ† ํฐ์„ ๊ฐ–๋Š” ์ฒญํฌ๋“ค๋กœ ๋ถ„ํ• ๋˜์—ˆ์Šต๋‹ˆ๋‹ค(๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์ด ๋‹ค๋ฅธ ํ•ญ๋ชฉ๊ณผ ๊ฐ™์€ ํฌ๊ธฐ๊ฐ€ ๋˜๋„๋ก padding์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ) ๊ฐ ํ•ญ๋ชฉ ์‚ฌ์ด์— 2๊ฐœ์”ฉ์˜ ํ† ํฐ์ด ๊ฒน์นฉ๋‹ˆ๋‹ค. ํ† ํฐํ™” ๊ฒฐ๊ณผ๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: print(inputs.keys()) ์˜ˆ์ƒ๋Œ€๋กœ input_IDs์™€ attention_mask๊ฐ€ ๋‹ด๊ฒจ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ํ‚ค์ธ overflow_to_sample_mapping์€ ๊ฐ ๊ฒฐ๊ณผ๊ฐ€ ์–ด๋Š ๋ฌธ์žฅ์— ํ•ด๋‹นํ•˜๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ๋งต(map)์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ† ํฌ ๋‚˜์ด์ €๋กœ ์ „๋‹ฌํ•œ (์œ ์ผํ•œ) ๋ฌธ์žฅ์—์„œ ๋‚˜์˜จ 7๊ฐœ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: print(inputs["overflow_to_sample_mapping"]) ์ด๊ฒƒ์€ ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ํ•จ๊ป˜ ํ† ํฐํ™”ํ•  ๋•Œ ๋” ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, sentences = [ "This sentence is not too long but we are going to split it anyway.", "This sentence is shorter but will still get split.", ] inputs = tokenizer( sentences, truncation=True, return_overflowing_tokens=True, max_length=6, stride=2 ) print(inputs["overflow_to_sample_mapping"]) ๊ฒฐ๊ณผ๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด ์ด์ „๊ณผ ๊ฐ™์ด 7๊ฐœ์˜ ์ฒญํฌ๋กœ ๋ถ„ํ• ๋˜๊ณ  ๋‹ค์Œ 4๊ฐœ์˜ ์ฒญํฌ๊ฐ€ ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์—์„œ ์˜จ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๊ธธ์ด๊ฐ€ ๊ธด ์ปจํ…์ŠคํŠธ๋กœ ๋Œ์•„๊ฐ€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ question-answering ํŒŒ์ดํ”„๋ผ์ธ์€ ์•ž์—์„œ ์–ธ๊ธ‰ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์ตœ๋Œ€ ๊ธธ์ด 384์™€ ๋ชจ๋ธ์ด ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ฐฉ์‹๊ณผ ๋™์ผํ•œ 128์˜ ๋ณดํญ(stride)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์„ ํ˜ธ์ถœํ•  ๋•Œ max_seq_len ๋ฐ stride ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ํ•ด๋‹น ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ† ํฐํ™”ํ•  ๋•Œ ์ด๋Ÿฌํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํŒจ๋”ฉ(padding)์„ ์ถ”๊ฐ€ํ•˜๊ณ (ํ…์„œ๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ) ์˜คํ”„์…‹์„ ์š”์ฒญํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: inputs = tokenizer( question, long_context, stride=128, max_length=384, padding="longest", truncation="only_second", return_overflowing_tokens=True, return_offsets_mapping=True, ) ์œ„์—์„œ inputs์—๋Š” ๋ชจ๋ธ๋กœ ์ž…๋ ฅ๋˜๋Š” input_IDs์™€ attention_mask๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๋ฐฉ๊ธˆ ์–ธ๊ธ‰ํ•œ ์˜คํ”„์…‹(offset) ๋ฐ overflow_to_sample_mapping์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ๋‘ ๊ฐ€์ง€๋Š” ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์ „์— inputs์—์„œ ์ด๋ฅผ ์ œ๊ฑฐ(pop) ํ•ฉ๋‹ˆ๋‹ค: _ = inputs.pop("overflow_to_sample_mapping") offsets = inputs.pop("offset_mapping") inputs = inputs.convert_to_tensors("pt") print(inputs["input_ids"].shape) ๊ธธ์ด๊ฐ€ ๊ธด ์ปจํ…์ŠคํŠธ๋Š” ๋‘ ๊ฐœ๋กœ ๋ถ„ํ• ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์€ ๋‘ ๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ์‹œ์ž‘ ๋ฐ ๋งˆ์ง€๋ง‰ ๋กœ์ง“(logits)์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค: outputs = model(**inputs) start_logits = outputs.start_logits end_logits = outputs.end_logits print(start_logits.shape, end_logits.shape) ์ด์ „๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ softmax๋ฅผ ์ทจํ•˜๊ธฐ ์ „์— ์ปจํ…์ŠคํŠธ์˜ ์ผ๋ถ€๊ฐ€ ์•„๋‹Œ ํ† ํฐ์„ ๋จผ์ € ๋งˆ์Šคํ‚น ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ชจ๋“  ํŒจ๋”ฉ ํ† ํฐ์„ ๋งˆ์Šคํ‚น ํ•ฉ๋‹ˆ๋‹ค(attention_mask๋กœ ํ‘œ์‹œ๋œ ๋Œ€๋กœ): sequence_ids = inputs.sequence_ids() # Mask everything apart from the tokens of the context mask = [i != 1 for i in sequence_ids] # Unmask the [CLS] token mask[0] = False # Mask all the [PAD] tokens mask = torch.logical_or(torch.tensor(mask)[None], (inputs["attention_mask"] == 0)) start_logits[mask] = -10000 end_logits[mask] = -10000 ๊ทธ๋Ÿฐ ๋‹ค์Œ softmax๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ์ง“(logits)์„ ํ™•๋ฅ ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: start_probabilities = torch.nn.functional.softmax(start_logits, dim=-1) end_probabilities = torch.nn.functional.softmax(end_logits, dim=-1) ๋‹ค์Œ ๋‹จ๊ณ„๋Š” ์•ž์—์„œ ๊ธธ์ด๊ฐ€ ์งง์€ ์ปจํ…์ŠคํŠธ์— ๋Œ€ํ•ด ์ˆ˜ํ–‰ํ•œ ์ž‘์—…๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ์ฒญํฌ๊ฐ€ 2๊ฐœ์ด๋ฏ€๋กœ ์ด๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋‹ต๋ณ€(answer spans)์— ์ ์ˆ˜๋ฅผ ๋ถ€์—ฌํ•œ ๋‹ค์Œ ๊ฐ€์žฅ ์ข‹์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ๋‹ต๋ณ€์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค: candidates = [] for start_probs, end_probs in zip(start_probabilities, end_probabilities): scores = start_probs[:, None] * end_probs[None, :] idx = torch.triu(scores).argmax().item() start_idx = idx // scores.shape[0] end_idx = idx % scores.shape[0] score = scores[start_idx, end_idx].item() candidates.append((start_idx, end_idx, score)) print(candidates) ์œ„์—์„œ ์ถœ๋ ฅ๋œ 2๊ฐœ์˜ ํ›„๋ณด๋Š” ๋ชจ๋ธ์ด ๊ฐ ์ฒญํฌ(chunk, ๊ธธ์ด๊ฐ€ ๊ธธ์–ด์„œ ๋ถ„ํ• ๋œ ์ปจํ…์ŠคํŠธ)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ๋˜ ์ตœ์ƒ์˜ ๋‹ต๋ณ€์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ์ •๋‹ต์ด ๋‘ ๋ฒˆ์งธ๋ผ๊ณ  ํ™•์‹คํžˆ ๋” ํ™•์‹ ํ•ฉ๋‹ˆ๋‹ค(์ข‹์€ ์ง•์กฐ์ž…๋‹ˆ๋‹ค!). ์ด์ œ ๋‘ ํ† ํฐ ๋ฒ”์œ„(token spans)๋ฅผ ์ปจํ…์ŠคํŠธ์˜ ๋ฌธ์ž ๋ฒ”์œ„(character spans)์— ๋งคํ•‘ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ฐ€์žฅ ํ™•์‹คํ•œ ๋‹ต๋ณ€์„ ์–ป๊ธฐ ์œ„ํ•ด ๋‘ ๋ฒˆ์งธ ํ›„๋ณด๋งŒ ๋งคํ•‘ํ•˜๋ฉด ๋˜์ง€๋งŒ, ์ฒซ ๋ฒˆ์งธ ์ฒญํฌ์—์„œ ๋ชจ๋ธ์ด ์„ ํƒํ•œ ๋‹ต๋ณ€์„ ๋ณด๋Š” ๊ฒƒ๋„ ์žฌ๋ฏธ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. โœ Try it out! ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ 5๊ฐœ์˜ ๋‹ต๋ณ€์— ๋Œ€ํ•œ ์ ์ˆ˜์™€ ๋ฒ”์œ„๋ฅผ ๋ฐ˜ํ™˜ํ•ด ๋ณด์„ธ์š”(์ฒญํฌ๋ณ„๋กœ๊ฐ€ ์•„๋‹ˆ๋ผ ์ดํ•ฉ์ ์œผ๋กœ). ์šฐ๋ฆฌ๊ฐ€ ์ด์ „์— ๊ฐ€์ ธ์˜จ ์˜คํ”„์…‹์€ ์‹ค์ œ๋กœ๋Š” ์˜คํ”„์…‹ ๋ฆฌ์ŠคํŠธ(list of offsets)์ด๋ฉฐ ํ…์ŠคํŠธ ์ฒญํฌ๋‹น ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค: for candidate, offset in zip(candidates, offsets): start_token, end_token, score = candidate start_char, _ = offset[start_token] _, end_char = offset[end_token] answer = long_context[start_char:end_char] result = {"answer": answer, "start":start_char, "end":end_char, "score":score} print(result) ์ฒซ ๋ฒˆ์งธ ๊ฒฐ๊ณผ๋ฅผ ๋ฌด์‹œํ•˜๋ฉด ์ด ๊ธธ์ด๊ฐ€ ๊ธด ์ปจํ…์ŠคํŠธ(long_context)์— ๋Œ€ํ•œ ํŒŒ์ดํ”„๋ผ์ธ ์‹คํ–‰ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. โœ Try it out! ์ด์ „์— ๊ณ„์‚ฐํ•œ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ 5๊ฐœ์˜ ๋‹ต๋ณ€์„ ํ‘œ์‹œํ•ด ๋ณด์„ธ์š”(๊ฐ ์ฒญํฌ๊ฐ€ ์•„๋‹Œ ์ „์ฒด ์ปจํ…์ŠคํŠธ์— ๋Œ€ํ•ด). ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฒซ ๋ฒˆ์งธ ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ๋Œ์•„๊ฐ€์„œ ํ˜ธ์ถœํ•  ๋•Œ top_k=5๋ฅผ ์ „๋‹ฌํ•ด ๋ด…์‹œ๋‹ค. ์ด๊ฒƒ์œผ๋กœ ํ† ํฌ ๋‚˜์ด์ €์˜ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ์‹ฌ์ธต ๋ถ„์„์„ ๋งˆ์นฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ ์ผ๋ฐ˜์ ์ธ NLP ์ž‘์—…์—์„œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค„ ๋•Œ ์—ฌ๊ธฐ์„œ ๋ฐฐ์šด ๋ชจ๋“  ๊ฒƒ๋“ค์„ ์‹ค์ œ๋กœ ์ ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. 4. ์ •๊ทœํ™”(Normalization) ๋ฐ ์‚ฌ์ „ ํ† ํฐํ™”(Pre-tokenization) ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ 3๊ฐ€์ง€ ํ•˜์œ„ ๋‹จ์–ด(subwword) ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜(Byte-Pair Encoding[BPE], WordPiece, Unigram)์— ๋Œ€ํ•ด ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ ์ „์—, ๋จผ์ € ๊ฐ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ํ…์ŠคํŠธ์— ์ ์šฉํ•˜๋Š” ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ ํ† ํฐํ™” ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋‹จ๊ณ„์— ๋Œ€ํ•œ ์ƒ์œ„ ์ˆ˜์ค€์˜ ๊ฐœ์š”๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค: ํ…์ŠคํŠธ๋ฅผ ํ•˜์œ„ ํ† ํฐ(subtokens)์œผ๋กœ ๋ถ„ํ• ํ•˜๊ธฐ ์ „์—(๋ชจ๋ธ์— ๋”ฐ๋ผ), ํ† ํฌ ๋‚˜์ด์ €๋Š” ์ •๊ทœํ™”(normalization) ๋ฐ ์‚ฌ์ „ ํ† ํฐํ™”(pre-tokenization) ๋‘ ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ •๊ทœํ™”(Normalization) ์ •๊ทœํ™” ๋‹จ๊ณ„์—๋Š” ๋ถˆํ•„์š”ํ•œ ๊ณต๋ฐฑ ์ œ๊ฑฐ, ์†Œ๋ฌธ์ž ๋ณ€ํ™˜(lowercasing) ๋ฐ ์•…์„ผํŠธ ์ œ๊ฑฐ ๋“ฑ๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ์ผ๋ฐ˜์ ์ธ ์ •์ œ ์ž‘์—…์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. NFC ๋˜๋Š” NFKC์™€ ๊ฐ™์€ ์œ ๋‹ˆ์ฝ”๋“œ ์ •๊ทœํ™”(Unicode normalization) ์ž‘์—…๊ณผ ๊ฑฐ์˜ ๋™์ผํ•œ ์ž‘์—…์ด ์ด ๊ณผ์ •์—์„œ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. Transformers์˜ tokenizer๋Š” Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ํ•˜๋ถ€ ํ† ํฌ ๋‚˜์ด์ €์— ๋Œ€ํ•œ ์•ก์„ธ์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” backend_tokenizer๋ผ๋Š” ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") print(type(tokenizer.backend_tokenizer)) ํ† ํฌ ๋‚˜์ด์ € ๊ฐ์ฒด์˜ normalizer ์†์„ฑ์—๋Š” normalize_str() ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ์ •๊ทœํ™”๊ฐ€ ์ˆ˜ํ–‰๋˜๋Š” ๋ฐฉ์‹์„ ํ™•์ธํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: print(tokenizer.backend_tokenizer.normalizer.normalize_str("Hรฉllรฒ hรดw are รผ?")) ์ด ์˜ˆ์—์„œ๋Š” bert-base-uncased ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์„ ํƒํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •๊ทœํ™” ๊ณผ์ •์—์„œ ์†Œ๋ฌธ์žํ™”(lowercasing)๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์•…์„ผํŠธ๋ฅผ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค. โœ Try it out! bert-base-cased ์ฒดํฌํฌ์ธํŠธ์—์„œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•˜๊ณ  ๋™์ผํ•œ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•ด ๋ณด์„ธ์š”. ํ† ํฌ ๋‚˜์ด์ €์˜ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์ด ์žˆ๋Š” ๋ฒ„์ „๊ณผ ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์ด ๋œ ๋ฒ„์ „ ๊ฐ„์— ๋ณผ ์ˆ˜ ์žˆ๋Š” ์ฃผ์š” ์ฐจ์ด์ ์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ์‚ฌ์ „ ํ† ํฐ ํ™”(Pre-tokenization) ๋‹ค์Œ ์„น์…˜์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ์›์‹œ ํ…์ŠคํŠธ๋งŒ์œผ๋กœ๋Š” ํ•™์Šต๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋Œ€์‹ ์— ๋จผ์ € ํ…์ŠคํŠธ๋ฅผ ๋‹จ์–ด์™€ ๊ฐ™์€ ์ž‘์€ ๊ฐœ์ฒด๋“ค๋กœ ๋ถ„ํ• ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์ „ ํ† ํฐํ™”(pre-tokenization) ๋‹จ๊ณ„๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. 2์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ๋‹จ์–ด ๊ธฐ๋ฐ˜ ํ† ํฌ ๋‚˜์ด์ €๋Š” ์›์‹œ ํ…์ŠคํŠธ๋ฅผ ๋‹จ์ˆœํžˆ ๊ณต๋ฐฑ๊ณผ ๊ตฌ๋‘์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด๋กœ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‹จ์–ด๋“ค์€ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ํ•™์Šต ๊ณผ์ •์—์„œ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜์œ„ ํ† ํฐ(subtokens)์˜ ๊ฒฝ๊ณ„๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €(fast tokenizer)๊ฐ€ ์‚ฌ์ „ ํ† ํฐํ™”(pre-tokenization)๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ณผ์ •์„ ๋ณด๋ ค๋ฉด tokenizer ๊ฐ์ฒด์˜ pre_tokenizer ์†์„ฑ์ด ๊ฐ€์ง„ pre_tokenize_str() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str("Hello, how are you?") ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์˜คํ”„์…‹(offsets)์„ ์–ด๋–ป๊ฒŒ ์œ ์ง€ํ•˜๊ณ  ์žˆ๋Š”์ง€์— ์ฃผ๋ชฉํ•˜์„ธ์š”. ์ด๋Š” ์ด์ „ ์„น์…˜์—์„œ ์‚ฌ์šฉํ•œ ์˜คํ”„์…‹ ๋งคํ•‘์„ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋‘ ๊ฐœ์˜ ๊ณต๋ฐฑ("are"์™€ "you" ์‚ฌ์ด์— ์žˆ๋Š”)์„ ๋ฌด์‹œํ•˜๊ณ  ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊พธ์ง€๋งŒ, "are"์™€ "you" ์‚ฌ์ด์˜ ์˜คํ”„์…‹ ์ ํ”„(14์—์„œ 16)๋Š” ๊ณ„์† ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” BERT ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ „ ํ† ํฐํ™”(pre-tokenization)์—๋Š” ๊ณต๋ฐฑ(whitespace)๊ณผ ๊ตฌ๋‘์ (puntuation) ๋ถ„ํ• ์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ† ํฌ ๋‚˜์ด ์ €๋“ค์€ ์ด ๋‹จ๊ณ„์—์„œ ๋‹ค๋ฅธ ๊ทœ์น™์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด GPT-2 ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ: tokenizer = AutoTokenizer.from_pretrained("gpt2") tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str("Hello, how are you?") ์œ„ ์ฝ”๋“œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ์™€ ๊ฐ™์ด, ๊ณต๋ฐฑ๊ณผ ๊ตฌ๋‘์ ์—์„œ๋„ ๋ถ„ํ• ๋˜์ง€๋งŒ ๊ณต๋ฐฑ์€ ์—†์• ์ง€ ์•Š๊ณ  ฤ  ๊ธฐํ˜ธ๋กœ ๋Œ€์ฒดํ•˜๋ฏ€๋กœ ํ† ํฐ์„ ๋””์ฝ”๋”ฉ ํ•˜๋ฉด ์›๋ž˜ ๊ณต๋ฐฑ์„ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ BERT ํ† ํฌ ๋‚˜์ด์ €์™€ ๋‹ฌ๋ฆฌ ์ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ์ด์ค‘ ๊ณต๋ฐฑ์„ ๋ฌด์‹œํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์˜ˆ๋กœ SentencePiece ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” T5 ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: tokenizer = AutoTokenizer.from_pretrained("t5-small") tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str("Hello, how are you?") GPT-2 ํ† ํฌ ๋‚˜์ด์ €์™€ ๊ฐ™์ด T5 ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ณต๋ฐฑ์„ ์œ ์ง€ํ•˜๊ณ  ํŠน์ • ํ† ํฐ(_)์œผ๋กœ ๋Œ€์ฒดํ•˜์ง€๋งŒ ๊ตฌ๋‘์ ์ด ์•„๋‹Œ ๊ณต๋ฐฑ์—์„œ๋งŒ ํ† ํฐ์„ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์žฅ ์‹œ์ž‘ ๋ถ€๋ถ„("Hello" ์•ž๋ถ€๋ถ„)์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ  "are"์™€ "you" ์‚ฌ์ด์˜ ์ด์ค‘ ๊ณต๋ฐฑ์„ ๋ฌด์‹œํ–ˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ํ…์ŠคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ๊ฐ์˜ ๋ฐฉ๋ฒ•์„ ์กฐ๊ธˆ ์‚ดํŽด๋ณด์•˜์œผ๋ฏ€๋กœ ํ•˜๋ถ€์˜ ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด๋ฅผ ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ๋‹ค์–‘ํ•œ ๋ชฉ์ ์œผ๋กœ ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” SentencePiece๋ฅผ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ 3๊ฐœ์˜ ์„น์…˜์—์„œ ํ•˜์œ„ ๋‹จ์–ด(subword) ํ† ํฐํ™”์— ์‚ฌ์šฉ๋˜๋Š” ์„ธ ๊ฐ€์ง€ ์ฃผ์š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SentencePiece SentencePiece๋Š” ๋‹ค์Œ ์„ธ ์„น์…˜์—์„œ ๋ณด๊ฒŒ ๋  ๋ชจ๋“  ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋ฅผ ์ผ๋ จ์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๋“ค๋กœ ๊ฐ„์ฃผํ•˜๊ณ  ๊ณต๋ฐฑ์„ ํŠน์ˆ˜ ๋ฌธ์ž์ธ _๋กœ ์น˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. Unigram ์•Œ๊ณ ๋ฆฌ์ฆ˜(์„น์…˜ 7 ์ฐธ์กฐ)๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์ „ ํ† ํฐํ™”(pre-tokenization) ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ณต๋ฐฑ ๋ฌธ์ž๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ์–ธ์–ด(์˜ˆ: ์ค‘๊ตญ์–ด ๋˜๋Š” ์ผ๋ณธ์–ด)์— ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. SentencePiece์˜ ๋˜ ๋‹ค๋ฅธ ์ฃผ์š” ๊ธฐ๋Šฅ์€ ๊ฐ€์—ญ์  ํ† ํฐํ™”(reversible tokenization)์ž…๋‹ˆ๋‹ค. ๊ณต๋ฐฑ์— ๋Œ€ํ•œ ํŠน๋ณ„ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ† ํฐ ๋””์ฝ”๋”ฉ์€ ํ† ํฐ์„ ์—ฐ๊ฒฐํ•˜๊ณ  _s๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„๋‹จํžˆ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์ •๊ทœํ™”๋œ ํ…์ŠคํŠธ๊ฐ€ ๋„์ถœ๋ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ ๋ณด์•˜๋“ฏ์ด BERT ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋ฐ˜๋ณต๋˜๋Š” ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๋ฏ€๋กœ ํ† ํฐํ™”๋Š” ๋˜๋Œ๋ฆด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ์š” ์ดํ›„ ์„น์…˜์—์„œ๋Š” ์„ธ ๊ฐ€์ง€ ์ฃผ์š” ํ•˜์œ„ ๋‹จ์–ด ํ† ํฐํ™”(subword tokenization) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ BPE(GPT-2 ๋“ฑ์—์„œ ์‚ฌ์šฉ), WordPiece(์˜ˆ: BERT์—์„œ ์‚ฌ์šฉ) ๋ฐ Unigram(T5 ๋ฐ ๊ทธ ์™ธ.)์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๋ณธ๊ฒฉ์ ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๊ฐ๊ฐ์˜ ์ž‘๋™ ๋ฐฉ์‹์— ๋Œ€ํ•œ ๊ฐ„๋žตํ•œ ๊ฐœ์š”๋ฅผ ์•„๋ž˜ ํ‘œ์—์„œ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์„น์…˜์„ ๊ฐ๊ฐ ์ฝ์€ ํ›„ ๊ทธ๋ž˜๋„ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” ์ฃผ์ €ํ•˜์ง€ ๋ง๊ณ  ์ด ํ‘œ๋กœ ๋‹ค์‹œ ๋Œ์•„์˜ค์„ธ์š”. ๋ชจ๋ธ BPE WordPiece Unigram ํ•™์Šต ๊ณผ์ •(Training) ์†Œ๊ทœ๋ชจ vocabulary์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ํ† ํฐ ๋ณ‘ํ•ฉ ๊ทœ์น™์„ ๋ฐฐ์›๋‹ˆ๋‹ค. ์†Œ๊ทœ๋ชจ vocabulary์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ํ† ํฐ ๋ณ‘ํ•ฉ ๊ทœ์น™์„ ๋ฐฐ์›๋‹ˆ๋‹ค. ๋Œ€๊ทœ๋ชจ vocabulary์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ํ† ํฐ์„ ์ œ๊ฑฐํ•˜๋Š” ๊ทœ์น™์„ ๋ฐฐ์›๋‹ˆ๋‹ค. ํ•™์Šต ๋‹จ๊ณ„(Training step) ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐœ์ƒ๋˜๋Š” ํ† ํฐ ์Œ์„ ๋ณ‘ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐ ์Œ์˜ ๋นˆ๋„์ˆ˜์— ๊ธฐ๋ฐ˜ํ•œ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์Œ์— ํ•ด๋‹นํ•˜๋Š” ํ† ํฐ์„ ๋ณ‘ํ•ฉํ•˜๊ณ  ๊ฐ ๊ฐœ๋ณ„ ํ† ํฐ์˜ ๋นˆ๋„๊ฐ€ ๋‚ฎ์€ ์Œ์— ํŠน๊ถŒ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ์ฝ”ํผ์Šค์—์„œ ๊ณ„์‚ฐ๋œ ์†์‹ค(loss)์„ ์ตœ์†Œํ™”ํ•˜๋Š” vocabulary์˜ ๋ชจ๋“  ํ† ํฐ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๊ฒฐ๊ณผ(Learns) ํ† ํฐ ๋ณ‘ํ•ฉ ๊ทœ์น™ ๋ฐ vocabulary Vocabulary ๊ฐ ํ† ํฐ์— ๋Œ€ํ•œ ์ ์ˆ˜๊ฐ€ ์žˆ๋Š” vocabulary ์ธ์ฝ”๋”ฉ(Encoding) ๋‹จ์–ด๋ฅผ ๋ฌธ์ž๋“ค๋กœ ๋ถ„ํ• ํ•˜๊ณ  ํ•™์Šต ๊ณผ์ •์—์„œ ์Šต๋“ํ•œ ๋ณ‘ํ•ฉ ๊ทœ์น™ ์ ์šฉ Vocabulary์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ๊ฐ€์žฅ ๊ธด ํ•˜์œ„ ๋‹จ์–ด(longest subword)๋ฅผ ์ฐพ์€ ๋‹ค์Œ ๋‚˜๋จธ์ง€ ๋‹จ์–ด์— ๋Œ€ํ•ด ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ ํ•™์Šต ๊ณผ์ •์—์„œ ํš๋“ํ•œ ์ ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ํ† ํฐ ๋ถ„ํ• ์„ ์ฐพ์Œ ์ด์ œ BPE์— ๋Œ€ํ•ด์„œ ์ž์„ธํžˆ ์•Œ์•„๋ด…์‹œ๋‹ค! 5. Byte-Pair Encoding (BPE) ํ† ํฐํ™” BPE(Byte-Pair Encoding)๋Š” ์ดˆ๊ธฐ์— ํ…์ŠคํŠธ๋ฅผ ์••์ถ•ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ฐœ๋ฐœ๋œ ํ›„, GPT ๋ชจ๋ธ์„ ์‚ฌ์ „ ํ•™์Šตํ•  ๋•Œ ํ† ํฐํ™”๋ฅผ ์œ„ํ•ด OpenAI์—์„œ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. GPT, GPT-2, RoBERTa, BART ๋ฐ DeBERTa๋ฅผ ํฌํ•จํ•œ ๋งŽ์€ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ์ „์ฒด ๊ตฌํ˜„ ๊ณผ์ •์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ๊นŒ์ง€๋ฅผ ํฌํ•จํ•˜์—ฌ BPE๋ฅผ ์‹ฌ์ธต์ ์œผ๋กœ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ๊ฐœ์š”๋งŒ์„ ์›ํ•˜๋Š” ๊ฒฝ์šฐ ์ด ์žฅ์„ ๊ฑด๋„ˆ๋›ฐ์–ด๋„ ๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜ BPE ํ•™์Šต์€ ์ •๊ทœํ™” ๋ฐ ์‚ฌ์ „ ํ† ํฐํ™” ๋‹จ๊ณ„๊ฐ€ ์™„๋ฃŒ๋œ ํ›„, ๋ง๋ญ‰์น˜์— ์‚ฌ์šฉ๋œ ๊ณ ์œ ํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด๋Ÿฌํ•œ ๋‹จ์–ด๋“ค์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ ๋ชจ๋“  ๊ธฐํ˜ธ(๊ธ€์ž)๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ vocabulary๋ฅผ ๊ตฌ์ถ•ํ•ฉ๋‹ˆ๋‹ค. ์•„์ฃผ ๊ฐ„๋‹จํ•œ ์˜ˆ๋กœ์„œ ๋ง๋ญ‰์น˜๊ฐ€ ๋‹ค์Œ ๋‹ค์„ฏ ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค: "hug", "pug", "pun", "bun", "hugs" ๊ธฐ๋ณธ vocabulary๋Š” ["b", "g", "h", "n", "p", "s", "u"]๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋Š” ๊ธฐ๋ณธ vocabulary์—๋Š” ์ตœ์†Œํ•œ ๋ชจ๋“  ASCII ๋ฌธ์ž์™€ ์ผ๋ถ€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ† ํฐํ™”ํ•˜๋Š” ๋Œ€์ƒ์ด ํ•™์Šต ๋ง๋ญ‰์น˜์— ์—†๋Š” ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฌธ์ž๋Š” "์•Œ ์ˆ˜ ์—†๋Š” ํ† ํฐ(unknown token)"์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ๋งŽ์€ NLP ๋ชจ๋ธ์ด ์ด๋ชจํ‹ฐ์ฝ˜์ด ํฌํ•จ๋œ ์ฝ˜ํ…์ธ ๋ฅผ ๋ถ„์„ํ•˜๋Š”๋ฐ ์‹ฌ๊ฐํ•œ ์–ด๋ ค์›€์„ ๊ฒช๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค. GPT-2 ๋ฐ RoBERTa ํ† ํฌ ๋‚˜์ด์ €๋Š” ์ด ๋ฌธ์ œ๋ฅผ ๋งค์šฐ ์˜๋ฆฌํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด๋ฅผ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๊ตฌ์„ฑ๋œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ๊ธฐ๋ณธ vocabulary๋Š” ์ž‘์€ ํฌ๊ธฐ(256)๋ฅผ ๊ฐ–์ง€๋งŒ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋ฌธ์ž๋“ค์ด ์—ฌ์ „ํžˆ ํฌํ•จ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์•Œ ์ˆ˜ ์—†๋Š” ํ† ํฐ์œผ๋กœ ๋ณ€ํ™˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ํŠธ๋ฆญ(trick)์„ byte-level BPE๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋ณธ vocabulary๋ฅผ ๊ตฌํ•œ ํ›„, ๊ธฐ์กด vocabulary์˜ ๋‘ ์š”์†Œ๋ฅผ ์ƒˆ๋กœ์šด ๊ฒƒ์œผ๋กœ ๋ณ‘ํ•ฉํ•˜๋Š” ๊ทœ์น™์ธ merges๋ฅผ ํ•™์Šตํ•จ์œผ๋กœ์จ ์›ํ•˜๋Š” vocabulary ํฌ๊ธฐ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ์ƒˆ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฒ˜์Œ์—๋Š” ์ด๋Ÿฌํ•œ ๋ณ‘ํ•ฉ์œผ๋กœ ๋‘ ๊ฐœ์˜ ๋ฌธ์ž๊ฐ€ ์žˆ๋Š” ํ† ํฐ์ด ์ƒ์„ฑ๋˜๊ณ  ํ•™์Šต์ด ์ง„ํ–‰๋จ์— ๋”ฐ๋ผ ๋” ๊ธด ํ•˜์œ„ ๋‹จ์–ด(subwords)๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ € ํ•™์Šต ๊ณผ์ •์—์„œ ์–ด๋–ค ๋‹จ๊ณ„์—์„œ๋“  BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ถœํ˜„ํ•˜๋Š” ํ† ํฐ ์Œ์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค(์—ฌ๊ธฐ์„œ "์Œ"์€ ํ•œ ๋‹จ์–ด์—์„œ ๋‘ ๊ฐœ์˜ ์—ฐ์† ํ† ํฐ์„ ์˜๋ฏธํ•˜๊ณ  ํ† ํฐ์€ ์ฒ˜์Œ์—๋Š” ๋‹จ์ผ ๋ฌธ์ž์ž…๋‹ˆ๋‹ค). ๊ฒ€์ƒ‰๋œ ๊ณ ๋นˆ๋„ ํ† ํฐ ์Œ์ด ๋ณ‘ํ•ฉ๋˜๋ฉฐ ์ด๋Ÿฌํ•œ ๊ณผ์ •์ด ๊ณ„์† ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. ์ด์ „ ์˜ˆ์ œ๋กœ ๋Œ์•„๊ฐ€์„œ ๊ฐ ๋‹จ์–ด๋“ค์˜ ์ถœํ˜„ ๋นˆ๋„๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค: ("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5) ๋ง๋ญ‰์น˜ ๋‚ด์— "hug"๊ฐ€ 10๋ฒˆ, "pug"๊ฐ€ 5๋ฒˆ, "pun"์ด 12๋ฒˆ, "bun"์ด 4๋ฒˆ, "hugs"๊ฐ€ 5๋ฒˆ ์ถœํ˜„ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด๋ฅผ ํ† ํฐ์˜ ๋ชฉ๋ก์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๊ฐ ๋‹จ์–ด๋ฅผ ๋ฌธ์ž(์ดˆ๊ธฐ vocabulary๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฌธ์ž)๋กœ ๋ถ„ํ• ํ•˜์—ฌ ํ•™์Šต์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: ("h" "u" "g", 10), ("p" "u" "g", 5), ("p" "u" "n", 12), ("b" "u" "n", 4), ("h" "u" "g" "s", 5) ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ฐ ๋ฌธ์ž ์Œ๋“ค์„ ์‚ดํŽด๋ด…์‹œ๋‹ค. ("h", "u")์€ "hug" ๋ฐ "hugs"๋ผ๋Š” ๋‹จ์–ด์— ์กด์žฌํ•˜๋ฏ€๋กœ ๋ง๋ญ‰์น˜์—์„œ ์ด 15๋ฒˆ ์ถœํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•œ ์Œ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ถœํ˜„ํ•˜๋Š” ์Œ์€ "hug", "pug" ๋ฐ "hugs"์— ์žˆ๋Š” ("u", "g")์ด๋ฉฐ ์ด 20๋ฒˆ ์ถœํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ํ•™์Šตํ•œ ์ฒซ ๋ฒˆ์งธ ๋ณ‘ํ•ฉ ๊ทœ์น™์€ ("u", "g") -> "ug"์ด๋ฉฐ, ์ด๋Š” "ug"๊ฐ€ vocabulary์— ์ถ”๊ฐ€๋˜๊ณ  ์ฝ”ํผ์Šค ๋‚ด์˜ ๋ชจ๋“  ๋‹จ์–ด์—์„œ "u"์™€ "g"๊ฐ€ ๋ณ‘ํ•ฉ๋˜์–ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„๊ฐ€ ๋๋‚˜๋ฉด vocabulary์™€ ๋ง๋ญ‰์น˜๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค: Vocabulary: ["b", "g", "h", "n", "p", "s", "u", "ug"] Corpus: ("h" "ug", 10), ("p" "ug", 5), ("p" "u" "n", 12), ("b" "u" "n", 4), ("h" "ug" "s", 5) ์ด์ œ 2๊ฐœ์˜ ๋ฌธ์ž๋ณด๋‹ค ๋” ๊ธด ํ† ํฐ์ด ์ƒ์„ฑ๋˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์Œ์ด ์ฝ”ํผ์Šค ๋‚ด์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ("h", "ug")(๋ง๋ญ‰์น˜์— 15๋ฒˆ ์ถœํ˜„)๊ฐ€ ๊ทธ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„์—์„œ ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ถœํ˜„๋œ ์Œ์€ ("u", "n")๋กœ์„œ ๋ง๋ญ‰์น˜์— 16๋ฒˆ ๋‚˜ํƒ€๋‚˜๋ฏ€๋กœ ํ•™์Šต๋œ ๋‘ ๋ฒˆ์งธ ๋ณ‘ํ•ฉ ๊ทœ์น™์€ ("u", "n") -> "un"์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ vocabulary์— ์ถ”๊ฐ€ํ•˜๊ณ  ๊ธฐ์กด์˜ ๋ชจ๋“  ํ•ญ๋ชฉ์„ ๋ณ‘ํ•ฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉ๋‹ˆ๋‹ค: Vocabulary: ["b", "g", "h", "n", "p", "s", "u", "ug", "un"] Corpus: ("h" "ug", 10), ("p" "ug", 5), ("p" "un", 12), ("b" "un", 4), ("h" "ug" "s", 5) ์ด์ œ ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•œ ์Œ์€ ("h", "ug")์ด๋ฏ€๋กœ ๋ณ‘ํ•ฉ ๊ทœ์น™("h", "ug") -> "hug"์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์ฒ˜์Œ์œผ๋กœ 3๊ธ€์ž๋กœ ๊ตฌ์„ฑ๋œ ํ† ํฐ์ด ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ๋ณ‘ํ•ฉ ํ›„ ์ฝ”ํผ์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Vocabulary: ["b", "g", "h", "n", "p", "s", "u", "ug", "un", "hug"] Corpus: ("hug", 10), ("p" "ug", 5), ("p" "un", 12), ("b" "un", 4), ("hug" "s", 5) ์›ํ•˜๋Š” vocabulary ํฌ๊ธฐ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ์ด ์ž‘์—…์„ ๊ณ„์†ํ•ฉ๋‹ˆ๋‹ค. โœ Now your turn! ๋‹ค์Œ ๋ณ‘ํ•ฉ ๊ทœ์น™์€ ๋ฌด์—‡์ผ๊นŒ์š”? ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ† ํฐํ™”๋Š” ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ์ ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ž…๋ ฅ์„ ํ† ํฐํ™”ํ•œ๋‹ค๋Š” ์ ์—์„œ ์•ž์—์„œ ์‚ดํŽด๋ณธ ํ•™์Šต ํ”„๋กœ์„ธ์Šค์™€ ๋ฐ€์ ‘ํ•˜๊ฒŒ ์—ฐ๊ด€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค: ์ •๊ทœํ™” (Normalization) ์‚ฌ์ „ ํ† ํฐํ™” (Pre-tokenization) ๋‹จ์–ด๋ฅผ ๊ฐœ๋ณ„ ๋ฌธ์ž๋“ค๋กœ ๋ถ„ํ•  ํ•ด๋‹น ๋ถ„ํ• ์— ์ˆœ์„œ๋Œ€๋กœ ํ•™์Šต๋œ ๋ณ‘ํ•ฉ ๊ทœ์น™ ์ ์šฉ ์œ„์—์„œ ํ•™์Šต๋œ 3๊ฐ€์ง€ ๋ณ‘ํ•ฉ ๊ทœ์น™์„ ์ ์šฉํ•˜์—ฌ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: ("u", "g") -> "ug" ("u", "n") -> "un" ("h", "ug") -> "hug" "bug"๋ผ๋Š” ๋‹จ์–ด๋Š” ["b", "ug"]๋กœ ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ "mug"๋Š” ๊ธฐ๋ณธ vocabulary์— ๋ฌธ์ž "m"์ด ์—†์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ["[UNK]", "ug"]๋กœ ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ "thug"๋ผ๋Š” ๋‹จ์–ด๋Š” ["[UNK]", "hug"]๋กœ ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž "t"๋Š” ๊ธฐ๋ณธ vocabulary์— ์—†์œผ๋ฉฐ ๋ณ‘ํ•ฉ ๊ทœ์น™์„ ์ ์šฉํ•˜๋ฉด ๋จผ์ € "u"์™€ "g"๊ฐ€ ๋ณ‘ํ•ฉ๋œ ๋‹ค์Œ "hu"์™€ "g"๊ฐ€ ๋ณ‘ํ•ฉ๋ฉ๋‹ˆ๋‹ค. โœ Now your turn! "unhug"๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์–ด๋–ป๊ฒŒ ํ† ํฐํ™”๋ ๊นŒ์š”? BPE ๊ตฌํ˜„ ์ด์ œ BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์„ค๋ช…ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋Œ€๊ทœ๋ชจ ๋ง๋ญ‰์น˜์—์„œ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์ ํ™”๋œ ๋ฒ„์ „์ด ์•„๋‹™๋‹ˆ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑ๋œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๋จผ์ € ๋ง๋ญ‰์น˜๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ๋ช‡ ๋ฌธ์žฅ์œผ๋กœ ๊ฐ„๋‹จํ•œ ๋ง๋ญ‰์น˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: corpus = [ "This is the Hugging Face course.", "This chapter is about tokenization.", "This section shows several tokenizer algorithms.", "Hopefully, you will be able to understand how they are trained and generate tokens.", ] ๋‹ค์Œ์œผ๋กœ, ์œ„ ๋ง๋ญ‰์น˜๋ฅผ ๋‹จ์–ด ๋‹จ์œ„๋กœ ์‚ฌ์ „ ํ† ํฐ ํ™”(pre-tokenize) ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. GPT-2์—์„œ ์‚ฌ์šฉ๋œ BPE ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์ „ ํ† ํฐํ™”(pre-tokenization)์— gpt2 ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("gpt2") ๊ทธ๋Ÿฐ ๋‹ค์Œ ์‚ฌ์ „ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ๋ง๋ญ‰์น˜์— ์žˆ๋Š” ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„๋ฅผ ํ•จ๊ป˜ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค: from collections import defaultdict word_freqs = defaultdict(int) for text in corpus: words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) new_words = [word for word, offset in words_with_offsets] for word in new_words: word_freqs[word] += 1 print(word_freqs) ๋‹ค์Œ ๋‹จ๊ณ„๋Š” ๋ง๋ญ‰์น˜์— ์‚ฌ์šฉ๋œ ๋ชจ๋“  ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ๊ธฐ๋ณธ vocabulary๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: alphabet = [] for word in word_freqs.keys(): for letter in word: if letter not in alphabet: alphabet.append(letter) alphabet.sort() print(alphabet) ์ถ”๊ฐ€์ ์œผ๋กœ ํ•ด๋‹น vocabulary์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ๋ชจ๋ธ์ด ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. GPT-2์˜ ๊ฒฝ์šฐ ์œ ์ผํ•œ ํŠน์ˆ˜ ํ† ํฐ์€ "<|endoftext|>"์ž…๋‹ˆ๋‹ค: vocab = ["<|endoftext|>"] + alphabet.copy() ์ด์ œ ํ•™์Šต์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ ๋‹จ์–ด๋ฅผ ๊ฐœ๋ณ„ ๋ฌธ์ž๋กœ ๋ถ„ํ• ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: splits = {word: [c for c in word] for word in word_freqs.keys()} ์ด์ œ ํ•™์Šตํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์œผ๋ฏ€๋กœ ๊ฐ ์Œ์˜ ๋นˆ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์˜ ๊ฐ ๋‹จ๊ณ„์—์„œ ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: def compute_pair_freqs(splits): pair_freqs = defaultdict(int) for word, freq in word_freqs.items(): split = splits[word] if len(split) == 1: continue for i in range(len(split) - 1): pair = (split[i], split[i+1]) pair_freqs[pair] += freq return pair_freqs ์ดˆ๊ธฐ ๋ถ„ํ•  ํ›„ ์ด ๋”•์…”๋„ˆ๋ฆฌ(pair-freqs)์˜ ์ผ๋ถ€๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: pair_freqs = compute_pair_freqs(splits) for i, key in enumerate(pair_freqs.keys()): print(f"{key}: {pair_freqs[key]}") if i > 5: break ์ด์ œ ๊ฐ„๋‹จํ•œ ๋ฃจํ”„๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ถœํ˜„ํ•˜๋Š” ์Œ์„ ์ฐพ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: best_pair = "" max_freq = None for pair, freq in pair_freqs.items(): if max_freq is None or max_freq < freq: best_pair = pair max_freq = freq print(best_pair, max_freq) ๋”ฐ๋ผ์„œ ํ•™์Šตํ•  ์ฒซ ๋ฒˆ์งธ ๋ณ‘ํ•ฉ์€ ('ฤ ', 't') -> 'ฤ t'์ด๊ณ  vocabulary์— 'ฤ t'๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: merges = {("ฤ ", "t"): "ฤ t"} vocab.append("ฤ t") ๊ณ„์†ํ•˜๋ ค๋ฉด splits ๋”•์…”๋„ˆ๋ฆฌ์— ํ•ด๋‹น ๋ณ‘ํ•ฉ์„ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def merge_pair(a, b, splits): for word in word_freqs: split = splits[word] if len(split) == 1: continue i = 0 while i < len(split) - 1: if split[i] == a and split[i + 1] == b: split = split[:i] + [a + b] + split[i + 2 :] else: i += 1 splits[word] = split return splits ์ด์ œ ์ฒซ ๋ฒˆ์งธ ๋ณ‘ํ•ฉ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: splits = merge_pair("ฤ ", "t", splits) print(splits["ฤ trained"]) ์ด์ œ ์›ํ•˜๋Š” ๋ชจ๋“  ๋ณ‘ํ•ฉ์„ ํ•™์Šตํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋Š” ๋ชจ๋“ˆ์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Vocabulary์˜ ํฌ๊ธฐ๋ฅผ 50์œผ๋กœ ์ง€์ •ํ•ด ๋ด…์‹œ๋‹ค: vocab_size = 50 while len(vocab) < vocab_size: pair_freqs = compute_pair_freqs(splits) best_pair = "" max_freq = None for pair, freq in pair_freqs.items(): if max_freq is None or max_freq < freq: best_pair = pair max_freq = freq splits = merge_pair(*best_pair, splits) merges[best_pair] = best_pair[0] + best_pair[1] vocab.append(best_pair[0] + best_pair[1]) ๊ฒฐ๊ณผ์ ์œผ๋กœ 19๊ฐ€์ง€ ๋ณ‘ํ•ฉ ๊ทœ์น™์„ ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค(์ดˆ๊ธฐ vocabulary์˜ ํฌ๊ธฐ๋Š” ์•ŒํŒŒ๋ฒณ 31 - 30์ž, ํŠน์ˆ˜ ํ† ํฐ ํฌํ•จ): print(merges) ๊ทธ๋ฆฌ๊ณ  vocabulary๋Š” ํŠน์ˆ˜ ํ† ํฐ, ์ดˆ๊ธฐ ์•ŒํŒŒ๋ฒณ ๋ฐ ๋ณ‘ํ•ฉ์˜ ๋ชจ๋“  ๊ฒฐ๊ณผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค: print(vocab) ๋™์ผํ•œ ๋ง๋ญ‰์น˜์—์„œ train_new_from_iterator()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜‘๊ฐ™์€ vocabulary๊ฐ€ ๋„์ถœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ถœํ˜„ํ•œ ์Œ์„ ์„ ํƒํ•  ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ๋งˆ์ฃผ์น˜๋Š” ์Œ์„ ์„ ํƒํ•˜๋Š” ๋ฐ˜๋ฉด์—, Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‚ด๋ถ€ IDs๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ์Œ์„ ์„ ํƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  ์‚ฌ์ „ ํ† ํฐ ํ™”(pre-tokenize) ํ•˜๊ณ  ๋ถ„ํ• (split) ํ•œ ๋‹ค์Œ ํ•™์Šตํ•œ ๋ชจ๋“  ๋ณ‘ํ•ฉ ๊ทœ์น™(merge rules)์„ ์ ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: def tokenize(text): pre_tokenize_result = tokenizer._tokenizer.pre_tokenizer.pre_tokenize_str(text) pre_tokenized_text = [word for word, offset in pre_tokenize_result] splits = [[l for l in word] for word in pre_tokenized_text] for pair, merge in merges.items(): for idx, split in enumerate(splits): i = 0 while i < len(split) - 1: if split[i] == pair[0] and split[i + 1] == pair[1]: split = split[:i] + [merge] + split[i + 2 :] else: i += 1 splits[idx] = split return sum(splits, []) ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ชจ๋“  ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenize("This is not a token.") โš  ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์•Œ ์ˆ˜ ์—†๋Š” ๋ฌธ์ž(unknown character)๊ฐ€ ์žˆ์œผ๋ฉด ๊ตฌํ˜„์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. GPT-2์—๋Š” ์‹ค์ œ๋กœ ์•Œ ์ˆ˜ ์—†๋Š” ํ† ํฐ์ด ์—†์ง€๋งŒ(๋ฐ”์ดํŠธ ์ˆ˜์ค€ BPE๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์•Œ ์ˆ˜ ์—†๋Š” ๋ฌธ์ž๋ฅผ ์–ป๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค), ์—ฌ๊ธฐ์„œ๋Š” ์ดˆ๊ธฐ vocabulary์— ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ฐ”์ดํŠธ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์ด ์„น์…˜์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฏ€๋กœ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ์ƒ๋žตํ–ˆ์Šต๋‹ˆ๋‹ค. BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค! ๋‹ค์Œ์œผ๋กœ WordPiece๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 6. WordPiece ํ† ํฐํ™” WordPiece๋Š” Google์ด BERT๋ฅผ ์‚ฌ์ „ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ๊ฐœ๋ฐœํ•œ ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๊ทธ ์ดํ›„๋กœ DitilBERT, MobileBERT, Funnel Transformers ๋ฐ MPNET๊ณผ ๊ฐ™์€ BERT ๊ธฐ๋ฐ˜์˜ ์ƒ๋‹นํžˆ ๋งŽ์€ Transformer ๋ชจ๋ธ์—์„œ ์žฌ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ์ธก๋ฉด์—์„œ BPE์™€ ๋งค์šฐ ์œ ์‚ฌํ•˜์ง€๋งŒ ์‹ค์ œ ํ† ํฐํ™”๋Š” ๋‹ค๋ฅด๊ฒŒ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” WordPiece๋ฅผ ์‹ฌ์ธต์ ์œผ๋กœ ๋‹ค๋ฃจ๋ฉฐ ์ „์ฒด ๊ตฌํ˜„ ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ๊ฐœ์š”๋ฅผ ์›ํ•˜๋Š” ๊ฒฝ์šฐ ์ƒ๋žตํ•ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค. ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜ โš  Google์€ WordPiece์˜ ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์„ ์˜คํ”ˆ ์†Œ์Šค๋กœ ๊ณต๊ฐœํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์ด๋ฒˆ ์„น์…˜์—์„œ๋Š” ๋ฐœํ‘œ๋œ ๋…ผ๋ฌธ ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„ ๊ณผ์ •์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๊ฑฐ์˜ 100% ์ •ํ™•ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. BPE์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ WordPiece๋Š” ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ ํ† ํฐ๊ณผ ์ดˆ๊ธฐ ์•ŒํŒŒ๋ฒณ์„ ํฌํ•จํ•œ ์ž‘์€ vocabulary์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ ‘๋‘์‚ฌ(์˜ˆ: BERT์˜ ##)๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ•˜์œ„ ๋‹จ์–ด(subwords)๋ฅผ ์‹๋ณ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ๋‹จ์–ด๋Š” ์ฒ˜์Œ์— ํ•ด๋‹น ์ ‘๋‘์‚ฌ๋ฅผ ๋‹จ์–ด ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๋ฌธ์ž์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋ถ„ํ• ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "word"๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ„ํ• ๋ฉ๋‹ˆ๋‹ค. w ##o ##r ##d ๋”ฐ๋ผ์„œ ์ดˆ๊ธฐ ์•ŒํŒŒ๋ฒณ์—๋Š” ๋‹จ์–ด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ์žˆ๋Š” ๋ชจ๋“  ๋ฌธ์ž๋“ค(์˜ˆ: 'w')๊ณผ WordPiece ์ ‘๋‘์‚ฌ๊ฐ€ ์„ ํ–‰ํ•˜๋Š” ๋‹จ์–ด ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ฌธ์ž(์˜ˆ: 'o', 'r', 'd')๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ BPE์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ WordPiece๋„ ๋ณ‘ํ•ฉ ๊ทœ์น™์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์ฐจ์ด์ ์€ ๋ณ‘ํ•ฉํ•  ์Œ์ด ์„ ํƒ๋˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ถœํ˜„ํ•˜๋Š” ์Œ์„ ์„ ํƒํ•˜๋Š” ๋Œ€์‹  WordPiece๋Š” ๋‹ค์Œ ๊ณต์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์Œ์— ๋Œ€ํ•œ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค: c r = r q f a r r q f i s e e e t f e o s c n e e e t ์Œ์˜ ๋นˆ๋„๋ฅผ ๊ฐ ๋ถ€๋ถ„์˜ ๋นˆ๋„์˜ ๊ณฑ์œผ๋กœ ๋‚˜๋ˆ”์œผ๋กœ์จ, ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ ๊ฐœ๋ณ„ ๋ถ€๋ถ„๋“ค์˜ ๋นˆ๋„๊ฐ€ ๋‚ฎ์€ ์Œ์˜ ๋ณ‘ํ•ฉ์— ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, vocabulary ๋‚ด์—์„œ์˜ ์ถœํ˜„ ๋นˆ๋„๊ฐ€ ๋†’์€ ("un", "##able") ์Œ์„ ๊ตณ์ด ๋ณ‘ํ•ฉํ•  ํ•„์š”๋Š” ์—†๋Š”๋ฐ, ๊ทธ ์ด์œ ๋Š” "un"๊ณผ "##able" ๊ฐ๊ฐ์ด ๋‹ค๋ฅธ ๋‹จ์–ด ๋‚ด์—์„œ ๋งค์šฐ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ถœํ˜„ํ•˜์—ฌ ๋†’์€ ๋นˆ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด์—, "hu"์™€ "##gging"์€ ๊ฐ๊ฐ์ด ์ž์ฃผ ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ("hu", "##gging")๊ณผ ๊ฐ™์€ ์Œ์€ ์•„๋งˆ๋„ ๋” ๋นจ๋ฆฌ ๋ณ‘ํ•ฉ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค("hugging"์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์–ดํœ˜์— ์ž์ฃผ ๋“ฑ์žฅํ•œ๋‹ค๊ณ  ๊ฐ€์ •). ์•ž์„œ BPE ํ•™์Šต ์˜ˆ์‹œ์—์„œ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•œ vocabulary๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: ("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5) ๋ถ„ํ•  ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ("h" "##u" "##g", 10), ("p" "##u" "##g", 5), ("p" "##u" "##n", 12), ("b" "##u" "##n", 4), ("h" "##u" "##g" "##s", 5) ๋”ฐ๋ผ์„œ ์ดˆ๊ธฐ vocabulary๋Š” ["b", "h", "p", "##g", "##n", "##s", "##u"]๊ฐ€ ๋ฉ๋‹ˆ๋‹ค(ํŠน์ˆ˜ ํ† ํฐ์€ ์ผ๋‹จ ์žŠ์–ด๋ฒ„๋ฆฝ์‹œ๋‹ค). ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•œ ์Œ์€ ("##u", "##g")(ํ˜„์žฌ 20ํšŒ)์ด์ง€๋งŒ "##u"์˜ ๊ฐœ๋ณ„ ๋นˆ๋„๊ฐ€ ๋งค์šฐ ๋†’์•„ ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค(1/36). "##u"๊ฐ€ ํฌํ•จ๋œ ๋ชจ๋“  ์Œ์€ ์‹ค์ œ๋กœ ๋™์ผํ•œ ์ ์ˆ˜(1/36)๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ๊ฐ€์žฅ ์ข‹์€ ์ ์ˆ˜๋Š” ("##g", "##s")์ด ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค(1/20). ์ด๋Š” "##u"๊ฐ€ ์—†๋Š” ์œ ์ผํ•œ ์Œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต๋œ ์ฒซ ๋ฒˆ์งธ ๋ณ‘ํ•ฉ์€ ("##g", "##s") -> ("##gs")์ž…๋‹ˆ๋‹ค. ๋ณ‘ํ•ฉํ•  ๋•Œ ๋‘ ํ† ํฐ ์‚ฌ์ด์˜ ##์„ ์ œ๊ฑฐํ•˜๋ฏ€๋กœ vocabulary์— "##gs"๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ง๋ญ‰์น˜์˜ ๋ชจ๋“  ๋‹จ์–ด์— ํ•ด๋‹น ๋ณ‘ํ•ฉ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค: Vocabulary: ["b", "h", "p", "##g", "##n", "##s", "##u", "##gs"] Corpus: ("h" "##u" "##g", 10), ("p" "##u" "##g", 5), ("p" "##u" "##n", 12), ("b" "##u" "##n", 4), ("h" "##u" "##gs", 5) ์ด ์‹œ์ ์—์„œ "##u"๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์Œ์— ์žˆ์œผ๋ฏ€๋กœ ๋ชจ๋‘ ๋™์ผํ•œ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์งธ ์Œ์ด ๋ณ‘ํ•ฉ๋˜๋ฏ€๋กœ ("h", "##u") -> "hu"๊ฐ€ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: Vocabulary: ["b", "h", "p", "##g", "##n", "##s", "##u", "##gs", "hu"] Corpus: ("hu" "##g", 10), ("p" "##u" "##g", 5), ("p" "##u" "##n", 12), ("b" "##u" "##n", 4), ("hu" "##gs", 5) ์ด์ œ ์ตœ๊ณ  ์ ์ˆ˜๋Š” ("hu", "##g") ๋ฐ ("hu", "##gs")๊ฐ€ ๋™์ผํ•˜๊ฒŒ ๊ณ„์‚ฐ๋˜๋ฏ€๋กœ(๋‹ค๋ฅธ ๋ชจ๋“  ์Œ์˜ ๊ฒฝ์šฐ 1/21์ด๊ณ  ์ด ๋‘ ์Œ์€ 1/15) ๊ฐ€์žฅ ํฐ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ฒซ ๋ฒˆ์งธ ์Œ์ด ๋ณ‘ํ•ฉ๋ฉ๋‹ˆ๋‹ค. Vocabulary: ["b", "h", "p", "##g", "##n", "##s", "##u", "##gs", "hu", "hug"] Corpus: ("hug", 10), ("p" "##u" "##g", 5), ("p" "##u" "##n", 12), ("b" "##u" "##n", 4), ("hu" "##gs", 5) ์›ํ•˜๋Š” ์–ดํœ˜ ํฌ๊ธฐ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ์ด ๋‹จ๊ณ„๊ฐ€ ๊ณ„์† ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. โœ Now your turn! ๋‹ค์Œ ๋ณ‘ํ•ฉ ๊ทœ์น™์€ ๋ฌด์—‡์ผ๊นŒ์š”? ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ† ํฐํ™”๋Š” WordPiece๊ฐ€ ํ•™์Šต๋œ ๋ณ‘ํ•ฉ ๊ทœ์น™์€ ์ œ์™ธํ•˜๊ณ  ์ตœ์ข… vocabulary๋งŒ ์ €์žฅํ•œ๋‹ค๋Š” ์ ์—์„œ BPE์™€๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํ† ํฐํ™”ํ•  ๋‹จ์–ด์—์„œ ์‹œ์ž‘ํ•˜์—ฌ WordPiece๋Š” vocabulary์— ์žˆ๋Š” ๊ฐ€์žฅ ๊ธด ํ•˜์œ„ ๋‹จ์–ด๋ฅผ ์ฐพ์€ ๋‹ค์Œ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„์˜ ์˜ˆ์—์„œ ํ•™์Šตํ•œ vocabulary๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹จ์–ด "hugs"์˜ ๊ฒฝ์šฐ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ํ•˜์œ„ ๋‹จ์–ด๋Š” vocabulary ๋‚ด๋ถ€์— ์žˆ๋Š” "hug"์ด๋ฏ€๋กœ ๊ฑฐ๊ธฐ์—์„œ ๋ถ„ํ• ํ•˜์—ฌ ["hug", "##s"]๋กœ ๋ถ„ํ• ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ "##s"๊ฐ€ vocabulary์— ์กด์žฌํ•˜๊ณ  ์ด๋ฅผ ๊ณ„์† ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ "hugs"์˜ ํ† ํฐํ™” ๊ฒฐ๊ณผ๋Š” ["hug", "##s"]์ž…๋‹ˆ๋‹ค. BPE๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•™์Šต๋œ ๋ณ‘ํ•ฉ(merges)์„ ์ˆœ์„œ๋Œ€๋กœ ์ ์šฉํ•˜๊ณ  ์ด๋ฅผ ["hu", "##gs"]๋กœ ํ† ํฐํ™”ํ•˜๋ฏ€๋กœ ์ธ์ฝ”๋”ฉ์ด ๋‹ค๋ฅด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋กœ "bugs"๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์–ด๋–ป๊ฒŒ ํ† ํฐํ™”๋˜๋Š”์ง€ ๋ด…์‹œ๋‹ค. "b"๋Š” vocabulary์— ์กด์žฌํ•˜๋Š” ๋‹จ์–ด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ํ•˜์œ„ ๋‹จ์–ด์ด๋ฏ€๋กœ ๊ฑฐ๊ธฐ์„œ ๋ถ„ํ• ํ•˜์—ฌ ["b", "##ugs"]๋ผ๋Š” ์ค‘๊ฐ„ ๊ฒฐ๊ณผ๊ฐ€ ๋„์ถœ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ "##u"๋Š” vocabulary์— ์žˆ๋Š” "##ugs"์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ํ•˜์œ„ ๋‹จ์–ด์ด๋ฏ€๋กœ ๊ฑฐ๊ธฐ์—์„œ ๋ถ„ํ• ํ•˜์—ฌ ["b", "##u, "##gs"]๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ "##gs"๊ฐ€ vocabulary์— ์žˆ์œผ๋ฏ€๋กœ ["b", "##u, "##gs"]์ด "bugs"์˜ ํ† ํฐํ™” ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ํ† ํฐ ํ™”๊ฐ€ vocabulary์—์„œ ํ•˜์œ„ ๋‹จ์–ด(subword)๋ฅผ ๋” ์ด์ƒ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๋‹จ๊ณ„์— ๋„๋‹ฌํ•˜๋ฉด ์ „์ฒด ๋‹จ์–ด๋ฅผ "unknown"์œผ๋กœ ํ† ํฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "mug"๋Š” "bum"๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ["[UNK]"]๋กœ ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค("b"์™€ "##u"๋กœ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋”๋ผ๋„ "##m"์ด vocabulary์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ฒฐ๊ณผ ํ† ํฐํ™”๋Š” ["b", "##u", "[UNK]"]๊ฐ€ ์•„๋‹ˆ๋ผ ["[UNK]"]์ž…๋‹ˆ๋‹ค). ์ด๊ฒƒ์€ vocabulary์— ์—†๋Š” ๊ฐœ๋ณ„ ๋ฌธ์ž๋งŒ "unknwon"์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” BPE์™€์˜ ๋˜ ๋‹ค๋ฅธ ์ฐจ์ด์ ์ž…๋‹ˆ๋‹ค. โœ Now your turn! "pugs"๋ผ๋Š” ๋‹จ์–ด๋Š” ์–ด๋–ป๊ฒŒ ํ† ํฐํ™”๋ ๊นŒ์š”? WordPiece ๊ตฌํ˜„ ์ด์ œ WordPiece ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌํ˜„์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. BPE์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์•„๋ž˜ ์ฝ”๋“œ๋Š” ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด์„œ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด๋ฉฐ ๋Œ€๊ทœ๋ชจ ๋ง๋ญ‰์น˜์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” BPE ์˜ˆ์‹œ์—์„œ์™€ ๋™์ผํ•œ ๋ง๋ญ‰์น˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: corpus = [ "This is the Hugging Face course.", "This chapter is about tokenization.", "This section shows several tokenizer algorithms.", "Hopefully, you will be able to understand how they are trained and generate tokens.", ] ๋จผ์ € ๋ง๋ญ‰์น˜๋ฅผ ๋‹จ์–ด๋กœ ์‚ฌ์ „ ํ† ํฐ ํ™”(pre-tokenization) ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. BERT์™€ ๊ฐ™์€ WordPiece ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์ „ ํ† ํฐํ™”์— bert-base-cased ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") ๊ทธ๋Ÿฐ ๋‹ค์Œ ์‚ฌ์ „ ํ† ํฐํ™” ์ˆ˜ํ–‰ ๊ณผ์ •์—์„œ ๋ง๋ญ‰์น˜์— ์žˆ๋Š” ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค: from collections import defaultdict word_freqs = defaultdict(int) for text in corpus: words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) new_words = [word for word, offset in words_with_offsets] for word in new_words: word_freqs[word] += 1 word_freqs ์ด์ „์— ๋ณด์•˜๋“ฏ์ด ์•ŒํŒŒ๋ฒณ์€ ๋‹จ์–ด์˜ ๋ชจ๋“  ์ฒซ ๊ธ€์ž์™€ ## ์ ‘๋‘์‚ฌ๊ฐ€ ๋ถ™์€ ๋‹จ์–ด์— ๋‚˜ํƒ€๋‚˜๋Š” ๋‹ค๋ฅธ ๋ชจ๋“  ๊ธ€์ž๋กœ ๊ตฌ์„ฑ๋œ ๊ณ ์œ ํ•œ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค: alphabet = [] for word in word_freqs.keys(): if word[0] not in alphabet: alphabet.append(word[0]) for letter in word[1:]: if f"##{letter}" not in alphabet: alphabet.append(f"##{letter}") alphabet.sort() alphabet print(alphabet) ๋˜ํ•œ ํ•ด๋‹น vocabulary์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ๋ชจ๋ธ์ด ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. BERT์˜ ๊ฒฝ์šฐ ["[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]"]์ž…๋‹ˆ๋‹ค: vocab = ["[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]"] + alphabet.copy() ๋‹ค์Œ์œผ๋กœ vocabulary์— ์กด์žฌํ•˜๋Š” ์ ‘๋‘์‚ฌ๊ฐ€ ##์ด ์•„๋‹Œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋‹จ์–ด๋ฅผ ๋ถ„ํ• ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: splits = { word: [c if i == 0 else f"##{c}" for i, c in enumerate(word)] for word in word_freqs.keys() } ์ด์ œ ํ•™์Šตํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์œผ๋ฏ€๋กœ ๊ฐ ์Œ์˜ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์˜ ๊ฐ ๋‹จ๊ณ„์—์„œ ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: def compute_pair_scores(splits): letter_freqs = defaultdict(int) pair_freqs = defaultdict(int) for word, freq in word_freqs.items(): split = splits[word] if len(split) == 1: letter_freqs[split[0]] += freq continue for i in range(len(split) - 1): pair = (split[i], split[i + 1]) letter_freqs[split[i]] += freq pair_freqs[pair] += freq letter_freqs[split[-1]] += freq scores = { pair: freq / (letter_freqs[pair[0]] * letter_freqs[pair[1]]) for pair, freq in pair_freqs.items() } return scores ์ดˆ๊ธฐ ๋ถ„ํ•  ํ›„ pair_scores์˜ ์ผ๋ถ€๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: pair_scores = compute_pair_scores(splits) for i, key in enumerate(pair_scores.keys()): print(f"{key}: {pair_scores[key]}") if i >= 5: break ์ด์ œ ์ตœ๊ณ ์˜ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์Œ์„ ์ฐพ๋Š” ๊ฐ„๋‹จํ•œ ๋ฃจํ”„๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค: best_pair = "" max_score = None for pair, score in pair_scores.items(): if max_score is None or max_score < score: best_pair = pair max_score = score print(best_pair, max_score) ๋”ฐ๋ผ์„œ ํ•™์Šตํ•  ์ฒซ ๋ฒˆ์งธ ๋ณ‘ํ•ฉ์€ ('a', '##b') -> 'ab'์ด๊ณ  vocabulary์— 'ab'๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: vocab.append("ab") ๊ณ„์†ํ•˜๋ ค๋ฉด splits ๋”•์…”๋„ˆ๋ฆฌ์— ํ•ด๋‹น ๋ณ‘ํ•ฉ์„ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def merge_pair(a, b, splits): for word in word_freqs: split = splits[word] if len(split) == 1: continue i = 0 while i < len(split) - 1: if split[i] == a and split[i + 1] == b: merge = a + b[2:] if b.startswith("##") else a + b split = split[:i] + [merge] + split[i + 2 :] else: i += 1 splits[word] = split return splits ์ด์ œ ์ฒซ ๋ฒˆ์งธ ๋ณ‘ํ•ฉ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: splits = merge_pair("a", "##b", splits) splits["about"] ์ด์ œ ์›ํ•˜๋Š” ๋ชจ๋“  ๋ณ‘ํ•ฉ์„ ๋ชจ๋‘ ํ•™์Šตํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“  ๊ฒƒ์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ชฉํ‘œ vocabulary ํฌ๊ธฐ๋ฅผ 70์œผ๋กœ ํ•ฉ์‹œ๋‹ค: vocab_size = 70 while len(vocab) < vocab_size: scores = compute_pair_scores(splits) best_pair, max_score = "", None for pair, score in scores.items(): if max_score is None or max_score < score: best_pair = pair max_score = score splits = merge_pair(*best_pair, splits) new_token = ( best_pair[0] + best_pair[1][2:] if best_pair[1].startswith("##") else best_pair[0] + best_pair[1] ) vocab.append(new_token) ์ƒ์„ฑ๋œ vocabulary๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: print(vocab) ๋ณด์‹œ๋‹ค์‹œํ”ผ BPE์— ๋น„ํ•ด ์ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋‹จ์–ด์˜ ์ผ๋ถ€๋ฅผ ํ† ํฐ์œผ๋กœ ๋” ๋นจ๋ฆฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ๋ง๋ญ‰์น˜์—์„œ train_new_from_iterator()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜‘๊ฐ™์€ vocabulary๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํ•™์Šต์„ ์œ„ํ•ด WordPiece๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ (๋‚ด๋ถ€์— ๋Œ€ํ•ด ์™„์ „ํžˆ ํ™•์‹ ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์—) ๋Œ€์‹  BPE๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์ „ ํ† ํฐํ™”ํ•˜๊ณ (pre-tokenization), ๋ถ„ํ• ํ•œ ๋‹ค์Œ(split), ๊ฐ ๋‹จ์–ด์— ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ€์žฅ ํฐ ํ•˜์œ„ ๋‹จ์–ด๋ฅผ ์ฐพ์•„ ๋ถ„ํ• ํ•œ ๋‹ค์Œ, ๋‘ ๋ฒˆ์งธ ๋ถ€๋ถ„์—์„œ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋ฐ˜๋ณตํ•˜๊ณ  ๋‚˜๋จธ์ง€ ๋‹จ์–ด์™€ ํ…์ŠคํŠธ์˜ ๋‹ค์Œ ๋‹จ์–ด์— ๋Œ€ํ•ด ๊ณ„์† ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค: def encode_word(word): tokens = [] while len(word) > 0: i = len(word) while i > 0 and word[:i] not in vocab: i -= 1 if i == 0: return ["[UNK]"] tokens.append(word[:i]) word = word[i:] if len(word) > 0: word = f"##{word}" return tokens Vocabulary์— ์กด์žฌํ•˜๋Š” ๋‹จ์–ด์™€ ๊ทธ๋ ‡์ง€ ์•Š์€ ๋‹จ์–ด์— ๋Œ€ํ•ด ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: print(encode_word("Hugging")) print(encode_word("HOgging")) ์ด์ œ ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def tokenize(text): pre_tokenize_result = tokenizer._tokenizer.pre_tokenizer.pre_tokenize_str(text) pre_tokenized_text = [word for word, offset in pre_tokenize_result] encoded_words = [encode_word(word) for word in pre_tokenized_text] return sum(encoded_words, []) ์ด์ œ ์–ด๋–ค ํ…์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ ๋„ ํ…Œ์ŠคํŠธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenize("This is the Hugging Face course!") WordPiece ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์—ฌ๊ธฐ๊นŒ์ง€์ž…๋‹ˆ๋‹ค! ์ด์ œ Unigram์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 7. Unigram ํ† ํฐํ™” Unigram ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ AlBERT, T5, mBART, Big Bird ๋ฐ XLNet๊ณผ ๊ฐ™์€ ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ SentencePiece์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ์ „์ฒด ๊ตฌํ˜„์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์„ ํฌํ•จํ•˜์—ฌ Unigram์„ ๊นŠ์ด ์žˆ๊ฒŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ๊ฐœ์š”๋ฅผ ์›ํ•˜๋Š” ๊ฒฝ์šฐ ์ƒ๋žตํ•ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค. ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜ BPE ๋ฐ WordPiece์™€ ๋น„๊ตํ•˜์—ฌ Unigram์€ ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํฌ๊ธฐ๊ฐ€ ํฐ vocabulary์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์›ํ•˜๋Š” vocabulary ํฌ๊ธฐ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํ† ํฐ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ vocabulary๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์ „ ํ† ํฐํ™”๋œ ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ทจํ•˜๊ฑฐ๋‚˜ ํฐ ๊ทœ๋ชจ์˜ vocabulary๋ฅผ ๊ฐ€์ง„ ์ดˆ๊ธฐ ๋ง๋ญ‰์น˜์— BPE๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์˜ ๊ฐ ๋‹จ๊ณ„์—์„œ Unigram ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ˜„์žฌ vocabulary๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ์˜ ๋ง๋ญ‰์น˜์— ๋Œ€ํ•œ ์†์‹ค(loss)์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ, vocabulary์˜ ๊ฐ ๊ธฐํ˜ธ(symbol)์— ๋Œ€ํ•ด, ํ•ด๋‹น ๊ธฐํ˜ธ๊ฐ€ ์ œ๊ฑฐ๋˜๋ฉด ์ „์ฒด ์†์‹ค์ด ์–ผ๋งˆ๋‚˜ ์ฆ๊ฐ€ํ• ์ง€ ๊ณ„์‚ฐํ•˜๊ณ  ๊ฐ€์žฅ ์ ๊ฒŒ ์ฆ๊ฐ€ํ•˜๋Š” ๊ธฐํ˜ธ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ฐพ์€ ๊ธฐํ˜ธ๋“ค(symbols)์€ ๋ง๋ญ‰์น˜์— ๋Œ€ํ•œ ์ „์ฒด ์†์‹ค์— ๋” ์ ์€ ์˜ํ–ฅ์„ ๋ฏธ์น˜๋ฏ€๋กœ ์–ด๋–ค ์˜๋ฏธ์—์„œ๋Š” "๋œ ํ•„์š”(less needed)" ํ•˜๊ณ  ์ œ๊ฑฐ ๋Œ€์ƒ์œผ๋กœ ๊ฐ€์žฅ ์ ํ•ฉํ•œ ํ›„๋ณด์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋น„์šฉ์ด ๋งŽ์ด ๋“œ๋Š” ์ž‘์—…์ด๋ฏ€๋กœ ๊ฐ€์žฅ ๋‚ฎ์€ ์†์‹ค์„ ์ดˆ๋ž˜ํ•˜๋Š” ๊ธฐํ˜ธ ํ•˜๋‚˜๋งŒ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ , ์ด๋Ÿฌํ•œ ๊ธฐํ˜ธ๋“ค์˜ %(๋Š” ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ด๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ 10 ๋˜๋Š” 20์„ ์ง€์ •)๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ vocabulary๊ฐ€ ์›ํ•˜๋Š” ํฌ๊ธฐ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ธฐ๋ณธ ๋ฌธ์ž๋“ค์„ ์ œ๊ฑฐํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ๋„ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์•„์ง๋„ ์„ค๋ช…์ด ์•ฝ๊ฐ„ ๋ชจํ˜ธํ•œ ๋ถ€๋ถ„์ด ์žˆ์ง€์š”. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ•ต์‹ฌ์€ ๋ง๋ญ‰์น˜์— ๋Œ€ํ•œ ์†์‹ค์„ ๊ณ„์‚ฐํ•˜๊ณ  vocabulary์—์„œ ์ผ๋ถ€ ํ† ํฐ์„ ์ œ๊ฑฐํ•  ๋•Œ ์†์‹ค์ด ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด์ง€๋งŒ ์•„์ง ์–ด๋–ป๊ฒŒ ์ˆ˜ํ–‰ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์„ค๋ช…ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ Unigram ๋ชจ๋ธ์˜ ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๊ธฐ๋ฐ˜ํ•˜๋ฏ€๋กœ ๋‹ค์Œ์— ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ญ์‹œ ์ด์ „ ์˜ˆ์ œ์˜ ๋ง๋ญ‰์น˜๋ฅผ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: ("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5) ์ดˆ๊ธฐ vocabulary๋Š” ์œ„ ๋ง๋ญ‰์น˜์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋‹จ์–ด๋“ค์˜ ๋ชจ๋“  ํ•˜์œ„ ๋ฌธ์ž์—ด(substrings)๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ["h", "u", "g", "hu", "ug", "p", "pu", "n", "un", "b", "bu", "s", "hug", "gs", "ugs"] ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ Unigram ๋ชจ๋ธ์€ ๊ฐœ๋ณ„ ํ† ํฐ๋“ค์˜ ์ถœํ˜„ ๋ถ„ํฌ๊ฐ€ ์„œ๋กœ ๋…๋ฆฝ์ (i.i.d)์ด๋ผ๋Š” ๊ฐ€์ •์„ ํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ ์œ ํ˜•์ž…๋‹ˆ๋‹ค. ํ† ํฐ X์˜ ํ™•๋ฅ ์ด ๋ฌธ๋งฅ์— ์ƒ๊ด€์—†์ด ๋™์ผํ•˜๋‹ค๋Š” ์ ์—์„œ ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Unigram ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ํ•ญ์ƒ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ด๊ณ  ํ”ํ•œ(common) ํ† ํฐ์„ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ํ† ํฐ์˜ ํ™•๋ฅ ์€ ๋ง๋ญ‰์น˜ ๋‚ด์—์„œ์˜ ํ•ด๋‹น ํ† ํฐ ์ถœํ˜„ ๋นˆ๋„๋ฅผ vocabulary์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ํ† ํฐ๋“ค์˜ ์ถœํ˜„ ๋นˆ๋„์˜ ํ•ฉ์œผ๋กœ ๋‚˜๋ˆˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค(ํ™•๋ฅ ์˜ ํ•ฉ์ด 1์ด ๋˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด). ์˜ˆ๋ฅผ ๋“ค์–ด "ug"๋Š” "hug", "pug" ๋ฐ "hugs"์— ์žˆ์œผ๋ฏ€๋กœ ๋ง๋ญ‰์น˜์—์„œ์˜ ๋นˆ๋„๋Š” 20์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ vocabulary์—์„œ์˜ ๋ชจ๋“  ํ•˜์œ„ ๋‹จ์–ด(subwords)์˜ ๋นˆ๋„์ž…๋‹ˆ๋‹ค: ("h", 15) ("u", 36) ("g", 20) ("hu", 15) ("ug", 20) ("p", 17) ("pu", 17) ("n", 16) ("un", 16) ("b", 4) ("bu", 4) ("s", 5) ("hug", 15) ("gs", 5) ("ugs", 5) ๋”ฐ๋ผ์„œ ๋ชจ๋“  ๋นˆ๋„์˜ ํ•ฉ์€ 210์ด๊ณ  ํ•˜์œ„ ๋‹จ์–ด(subword) "ug"์˜ ํ™•๋ฅ ์€ 20/210์ž…๋‹ˆ๋‹ค. โœ Now your turn! ์œ„์˜ ๋นˆ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ํ‘œ์‹œ๋œ ๊ฒฐ๊ณผ์™€ ์ดํ•ฉ์ด ์˜ฌ๋ฐ”๋ฅธ์ง€ ๋‹ค์‹œ ํ™•์ธํ•ด ๋ณด์„ธ์š”. ์ด์ œ ์ฃผ์–ด์ง„ ๋‹จ์–ด๋ฅผ ํ† ํฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด, ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํ† ํฐ ๋ถ„ํ• ์„ ์‚ดํŽด๋ณด๊ณ  Unigram ๋ชจ๋ธ์— ๋”ฐ๋ผ ๊ฐ๊ฐ์˜ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ํ† ํฐ์˜ ์ถœํ˜„ ๋นˆ๋„๊ฐ€ ๋…๋ฆฝ์ ์ธ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ํ™•๋ฅ ์€ ๊ฐ ํ† ํฐ์˜ ํ™•๋ฅ ์˜ ๊ณฑ์ผ๋ฟ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "pug"์˜ ํ† ํฐํ™” ๊ฒฐ๊ณผ์ธ ["p", "u", "g"]์˜ ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค: ( [ p, u, g ] ) P ( p) P ( u) P ( g) 5 210 36 210 2 210 ์ด์— ๋น„ํ•ด ํ† ํฐํ™” ๊ฒฐ๊ณผ์ธ ["pu", "g"]์˜ ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ( [ p " " " ] ) P ( p " ) P ( g) 5 210 20 210 0.0022676 ๋”ฐ๋ผ์„œ, ["pu", "g"]์ด ํ›จ์”ฌ ๋” ์ž์ฃผ ์ถœํ˜„ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์ง€์š”. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€์žฅ ์ ์€ ์ˆ˜์˜ ํ•˜์œ„ ํ† ํฐ๋“ค๋กœ ๊ตฌ์„ฑ๋œ ํ† ํฐํ™” ๊ฒฐ๊ณผ๋Š” ๋น„๊ต์  ๋†’์€ ํ™•๋ฅ (๊ฐ ํ† ํฐ์— ๋Œ€ํ•ด ๋ฐ˜๋ณต๋˜๋Š” 210์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ ๋•Œ๋ฌธ์—)์„ ๊ฐ€์ง€๋ฉฐ, ์ด๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ง๊ด€์ ์œผ๋กœ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. Unigram ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ๋‹จ์–ด์˜ ํ† ํฐํ™”๋Š” ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถ„ํ•  ํ˜•ํƒœ๋กœ ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. "pug"์˜ ์˜ˆ์—์„œ ๊ฐ€๋Šฅํ•œ ๊ฐ ๋ถ„ํ• ์— ๋Œ€ํ•ด ์–ป์„ ์ˆ˜ ์žˆ๋Š” ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ["p", "u", "g"] : 0.000389 ["p", "ug"] : 0.0022676 ["pu", "g"] : 0.0022676 ๋”ฐ๋ผ์„œ "pug"๋Š” ์œ„ ๋ถ„ํ•  ๋ฐฉ๋ฒ• ์ค‘์—์„œ ["p", "ug"] ๋˜๋Š” ["pu", "g"]๋กœ ํ† ํฐํ™”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํฐ ๊ทœ๋ชจ์˜ ๋ง๋ญ‰์น˜์—์„œ๋Š” ๋ถ„ํ•  ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ํ™•๋ฅ  ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ๋งค์šฐ ๋“œ๋ญ…๋‹ˆ๋‹ค. ์œ„์˜ ๊ฒฝ์šฐ์—์„œ๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ถ„ํ• ์„ ์ฐพ๊ณ  ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด ์‰ฌ์› ์ง€๋งŒ, ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ๋” ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๊ณ ์ „์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ Viterbi ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ์งˆ์ ์œผ๋กœ, ์ฃผ์–ด์ง„ ๋‹จ์–ด์— ๋Œ€ํ•œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ถ„ํ• ๋“ค์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งŒ์ผ ์ฃผ์–ด์ง„ ๋‹จ์–ด ๋‚ด์˜ ๋ฌธ์ž a์—์„œ b๊นŒ์ง€์˜ ํ•˜์œ„ ๋‹จ์–ด(subword)๊ฐ€ vocabulary์— ์กด์žฌํ•œ๋‹ค๋ฉด, ์šฐ๋ฆฌ๋Š” ์ด ๊ทธ๋ž˜ํ”„ ๋‚ด์—์„œ a์—์„œ ์ถœ๋ฐœํ•˜์—ฌ b๊นŒ์ง€ ๊ฐ€๋Š” ๊ทธ๋ž˜ํ”„ ๋‚ด์˜ ๊ฐ€์ง€(branch)๊ฐ€ ์žˆ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ•˜์œ„ ๋‹จ์–ด์˜ ํ™•๋ฅ ์„ ํ•ด๋‹น ๊ฐ€์ง€(branch)์— ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„์—์„œ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ์–ป์„ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด Viterbi ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹จ์–ด ๋‚ด์˜ ๊ฐ ์œ„์น˜(๋ฌธ์ž)์— ๋Œ€ํ•ด ํ•ด๋‹น ์œ„์น˜์—์„œ ๋๋‚˜๋Š” ๊ฒฝ๋กœ์˜ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถ„ํ• (segmentation)์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ์ฒ˜์Œ ์œ„์น˜๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ด๋™ํ•˜๋ฉด์„œ, ํ˜„์žฌ ์œ„์น˜์—์„œ ๋๋‚˜๋Š” ๋ชจ๋“  ํ•˜์œ„ ๋‹จ์–ด๋ฅผ ๊ฒ€์‚ฌํ•œ ๋‹ค์Œ ์ด ํ•˜์œ„ ๋‹จ์–ด๊ฐ€ ์‹œ์ž‘ํ•˜๋Š” ์œ„์น˜์—์„œ ์ตœ๊ณ ์˜ ํ† ํฐํ™” ์ ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ƒ์˜ ์ ์ˆ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋์— ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์„ ํƒํ•œ ๊ฒฝ๋กœ๋ฅผ ํŽผ์น˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ ๊ตฌ์„ฑํ•œ vocabulary์™€ "unhug"๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋‹จ์–ด์˜ ๊ฐ ์œ„์น˜์— ๋Œ€ํ•ด ์ตœ๊ณ  ์ ์ˆ˜๋กœ ๋๋‚˜๋Š” ํ•˜์œ„ ๋‹จ์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: Character 0 (u): "u" (score 0.171429) Character 1 (n): "un" (score 0.076191) Character 2 (h): "un" "h" (score 0.005442) Character 3 (u): "un" "hu" (score 0.005442) Character 4 (g): "un" "hug" (score 0.005442) ์œ„์—์„œ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๊ธ€์ž('g')๊นŒ์ง€ ์ง„ํ–‰ํ•œ ๊ฒฐ๊ณผ, ["un", "hug"]๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜์ธ 0.005442๋ฅผ ๋‚˜ํƒ€๋ƒˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ "unhug"๋Š” ["un", "hug"]๋กœ ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. โœ Now your turn! "huggun"์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ํ† ํฐํ™”ํ•˜๊ณ  ํ•ด๋‹น ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด์„ธ์š”. ๋‹ค์‹œ ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด์ œ ํ† ํฐ ํ™”๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด์•˜์œผ๋ฏ€๋กœ ํ•™์Šต ๊ณผ์ •์—์„œ ์‚ฌ์šฉ๋œ ์†์‹ค(loss)์— ๋Œ€ํ•ด ์กฐ๊ธˆ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ฃผ์–ด์ง„ ๋‹จ๊ณ„์—์„œ ์ด ์†์‹ค์€ ๋ง๋ญ‰์น˜ ๋‚ด์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ํ† ํฐํ™”ํ•˜์—ฌ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ๊ณ„์‚ฐ ๊ณผ์ •์—์„œ ์•ž์—์„œ ์„ค๋ช…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ํ˜„์žฌ vocabulary์™€ ๋ง๋ญ‰์น˜์— ์žˆ๋Š” ๊ฐ ํ† ํฐ์˜ ๋นˆ๋„์— ์˜ํ•ด ๊ฒฐ์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ง๋ญ‰์น˜์˜ ๊ฐ ๋‹จ์–ด๋ณ„๋กœ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉฐ ์†์‹ค์€ ํ•ด๋‹น ์ ์ˆ˜์˜ ์Œ์˜ ๋กœ๊ทธ ์šฐ๋„(negative log likelihood)์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋ง๋ญ‰์น˜์— ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด์˜ l g ( ( o d ) ) ํ•ฉ๊ณ„์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋ง๋ญ‰์น˜๋ฅผ ๊ฐ€์ง€๊ณ  ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค: ("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5) ๊ฐ ๋‹จ์–ด์˜ ํ† ํฐํ™” ๊ฒฐ๊ณผ ๋ฐ ์ ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: "hug": ["hug"] (score 0.071428) "pug": ["pu", "g"] (score 0.007710) "pun": ["pu", "n"] (score 0.006168) "bun": ["bu", "n"] (score 0.001451) "hugs": ["hug", "s"] (score 0.001701) ๋”ฐ๋ผ์„œ ์†์‹ค(loss)์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: 10 * (-log(0.071428)) + 5 * (-log(0.007710)) + 12 * (-log(0.006168)) + 4 * (-log(0.001451)) + 5 * (-log(0.001701)) = 169.8 ์ด์ œ ๊ฐ ํ† ํฐ์„ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ด ์†์‹ค ๊ฐ’์— ์–ด๋–ค ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”์ง€ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ์ˆ˜์ž‘์—…์œผ๋กœ ํ•˜๊ธฐ์—๋Š” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋ฏ€๋กœ ์—ฌ๊ธฐ์—์„œ๋Š” ๋‘ ๊ฐœ์˜ ํ† ํฐ("pu", "hug")์— ๋Œ€ํ•ด ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚˜๋จธ์ง€ ํ”„๋กœ์„ธ์Šค๋Š” ์•„๋ž˜์—์„œ ํ•ด๋‹น ์ž‘์—…์— ๋Œ€ํ•œ ์‹ค์ œ ๊ตฌํ˜„์ด ์™„๋ฃŒ๋˜์—ˆ์„ ๋•Œ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ์‚ดํŽด๋ณด์•˜๋“ฏ์ด, ์ด ์‹œ์ ์—์„œ "pug"๋Š” ๋™์ผํ•œ ์ ์ˆ˜(0.0022676)๋ฅผ ๊ฐ€์ง„ ๋‘ ๊ฐœ์˜ ํ† ํฐํ™” ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์‹œ์ง€์š”? ๋ฐ”๋กœ ["p", "ug"]์™€ ["pu", "g"]๊ฐ€ ๊ทธ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ "pu" ํ† ํฐ์„ vocabulary์—์„œ ์ œ๊ฑฐํ•˜๋”๋ผ๋„ ํ† ํฐํ™” ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•œ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ["p", "ug"]๊ฐ€ ๋˜๋ฏ€๋กœ, ๊ฒฐ๋ก ์ ์œผ๋กœ๋Š” ์œ„์—์„œ ๊ณ„์‚ฐํ•œ ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์€ ์†์‹ค(loss) ๊ฐ’์ด ๋„์ถœ๋˜๊ฒ ์ง€์š”. ๋ฐ˜๋ฉด์—, "hug"๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ์†์‹ค ๊ฐ’์ด ๋” ์˜ฌ๋ผ๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” "hug"์™€ "hugs"์˜ ํ† ํฐํ™” ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๊ฒ ์ง€์š”: "hug": ["hu", "g"] (score 0.006802) # ์œ„์˜ ํ† ํฐํ™” ๊ฒฐ๊ณผ๋ณด๋‹ค ์ ์ˆ˜๊ฐ€ ๋‚ฎ์•„์ง. "hugs": ["hu", "gs"] (score 0.001701) ๊ทธ ๊ฒฐ๊ณผ ๋‹ค์Œ ๊ณ„์‚ฐ ๊ฒฐ๊ด๊ฐ’๋งŒํผ ์†์‹ค ๊ฐ’์ด ์˜ฌ๋ผ๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค: - 10 * (-log(0.071428)) + 10 * (-log(0.006802)) = 23.5 ๊ฒฐ๋ก ์ ์œผ๋กœ, ํ† ํฐ "pu"๋Š” vocabulary์—์„œ ์ œ๊ฑฐ๋˜๊ฒ ์ง€๋งŒ "hug"๋Š” ์ œ๊ฑฐ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Unigram ๊ตฌํ˜„ ์ด์ œ ์ง€๊ธˆ๊นŒ์ง€ ์‚ดํŽด๋ณธ ๋ชจ๋“  ๊ฒƒ์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. BPE ๋ฐ WordPiece์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๊ฒƒ์€ Unigram ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํšจ์œจ์ ์ธ ๊ตฌํ˜„์€ ์•„๋‹ˆ์ง€๋งŒ ์ „์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ดํ•ดํ•˜๋Š” ๋ฐ๋Š” ๋„์›€์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ „๊ณผ ๋™์ผํ•œ ๋ง๋ญ‰์น˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: corpus = [ "This is the Hugging Face course.", "This chapter is about tokenization.", "This section shows several tokenizer algorithms.", "Hopefully, you will be able to understand how they are trained and generate tokens.", ] ์ด๋ฒˆ์—๋Š” xlnet-base-cased๋ฅผ ๋ชจ๋ธ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("xlnet-base-cased") BPE ๋ฐ WordPiece์˜ ๊ฒฝ์šฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ง๋ญ‰์น˜์—์„œ ๊ฐ ๋‹จ์–ด์˜ ์ถœํ˜„ ๋นˆ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: from collections import defaultdict word_freqs = defaultdict(int) for text in corpus: words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) new_words = [word for word, offset in words_with_offsets] for word in new_words: word_freqs[word] += 1 word_freqs ๊ทธ๋ฆฌ๊ณ  ์ตœ์ข…์ ์œผ๋กœ ์›ํ•˜๋Š” ํฌ๊ธฐ๋ณด๋‹ค ๋” ํฌ๊ฒŒ vocabulary๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ดˆ๊ธฐํ™” ๊ณผ์ •์—์„œ vocabulary์— ๋ชจ๋“  ๊ธฐ๋ณธ ๋ฌธ์ž๋“ค์„ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ํ† ํฐํ™”ํ•  ์ˆ˜ ์—†๊ฒ ์ง€์š”. ๋˜ํ•œ ๊ธธ์ด๊ฐ€ ๋” ๊ธด ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ๋Š” ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ถœํ˜„ํ•˜๋Š” ๊ฒƒ๋“ค๋งŒ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์ผ๋‹จ ๋นˆ๋„์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค: char_freqs = defaultdict(int) subwords_freqs = defaultdict(int) for word, freq in word_freqs.items(): for i in range(len(word)): char_freqs[word[i]] += freq # ๊ธธ์ด๊ฐ€ ์ ์–ด๋„ 2 ์ด์ƒ์ธ subword๋“ค์„ ์ถ”๊ฐ€ํ•จ. for j in range(i + 2, len(word) + 1): subwords_freqs[word[i:j]] += freq # Subword๋“ค์„ ๋นˆ๋„ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ sorted_subwords = sorted(subwords_freqs.items(), key=lambda x: x[1], reverse=True) sorted_subwords[:10] ํฌ๊ธฐ๊ฐ€ 300์ธ ์ดˆ๊ธฐ vocabulary๋ฅผ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ž์—์„œ ๋งŒ๋“ค์–ด์ง„ sorted_subwords ์ค‘์—์„œ ๋นˆ๋„๊ฐ€ ๋†’์€ ํ•˜์œ„ ๋‹จ์–ด๋“ค์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค: token_freqs = list(char_freqs.items()) + sorted_subwords[: 300 - len(char_freqs)] token_freqs = {token: freq for token, freq in token_freqs} SentencePiece๋Š” ESA(Enhanced Suffix Array)๋ผ๋Š” ๋ณด๋‹ค ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ดˆ๊ธฐ ์–ดํœ˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ๋ชจ๋“  ๋นˆ๋„์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ๋นˆ๋„๋ฅผ ํ™•๋ฅ ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ ํ™•๋ฅ ์˜ ๋กœ ๊ทธ ๊ฐ’์„ ์ €์žฅํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ž‘์€ ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋กœ๊ทธ๋ฅผ ๋”ํ•˜๋Š” ๊ฒƒ์ด ์ˆ˜์น˜์ ์œผ๋กœ ๋” ์•ˆ์ •์ ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋ธ ์†์‹ค ๊ณ„์‚ฐ์ด ๋‹จ์ˆœํ™”๋ฉ๋‹ˆ๋‹ค: from math import log total_sum = sum([freq for token, freq in token_freqs.items()]) model = {token: -log(freq / total_sum) for token, freq in token_freqs.items()} ์ด์ œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ Viterbi ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์–ด๋ฅผ ํ† ํฐํ™”ํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์ด์ „์— ๋ณด์•˜๋“ฏ์ด ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹จ์–ด์˜ ๊ฐ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์ตœ์ƒ์˜ ๋ถ„ํ• ์„ ๊ณ„์‚ฐํ•˜๊ณ  ์ด๋ฅผ best_segmentations๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ๊ฐ ์œ„์น˜๋‹น ํ•˜๋‚˜์˜ ๋”•์…”๋„ˆ๋ฆฌ(0์—์„œ ์ „์ฒด ๊ธธ์ด๊นŒ์ง€)์„ ๋‘ ๊ฐœ์˜ ํ‚ค์™€ ํ•จ๊ป˜ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ€์žฅ ์ ์ˆ˜๊ฐ€ ๋†’์€ ๋ถ„ํ• (segmentation)์—์„œ ๋งˆ์ง€๋ง‰ ํ† ํฐ์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค์™€ ํ•ด๋‹น ์ ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ํ† ํฐ์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชฉ๋ก์ด ์™„์ „ํžˆ ์ฑ„์›Œ์ง€๋ฉด ์ „์ฒด ๋ถ„ํ• ์„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชฉ๋ก ์ฑ„์šฐ๊ธฐ๋Š” ๋‹จ 2๊ฐœ์˜ ๋ฃจํ”„๋กœ ์™„๋ฃŒ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋ฃจํ”„๋Š” ๊ฐ ์‹œ์ž‘ ์œ„์น˜๋กœ ์ด๋™ํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ ๋ฃจํ”„๋Š” ํ•ด๋‹น ์‹œ์ž‘ ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ํ•˜์œ„ ๋ฌธ์ž์—ด์„ ๊ฒ€ํ† ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์œ„ ๋ฌธ์ž์—ด์ด vocabulary์— ์žˆ๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ๋ ์œ„์น˜๊นŒ์ง€ ๋‹จ์–ด์˜ ์ƒˆ๋กœ์šด ๋ถ„ํ• ์ด ์žˆ์œผ๋ฉฐ ์ด๋ฅผ best_segmentations์— ์žˆ๋Š” ๊ฒƒ๊ณผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”์ธ ๋ฃจํ”„๊ฐ€ ๋๋‚˜๋ฉด ๋‹จ์–ด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๋์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ํŠน์ • ์‹œ์ž‘ ์œ„์น˜์—์„œ ๋‹ค์Œ ์œ„์น˜๋กœ ์ด๋™ํ•˜๋ฉด์„œ ํ† ํฐ์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค: def encode_word(word, model): best_segmentations = [{"start": 0, "score": 1}] + [ {"start": None, "score": None} for _ in range(len(word)) ] for start_idx in range(len(word)): # This should be properly filled by the previous steps of the loop best_score_at_start = best_segmentations[start_idx]["score"] for end_idx in range(start_idx + 1, len(word) + 1): token = word[start_idx:end_idx] if token in model and best_score_at_start is not None: score = model[token] + best_score_at_start # If we have found a better segmentation ending at end_idx, we update if ( best_segmentations[end_idx]["score"] is None or best_segmentations[end_idx]["score"] > score ): best_segmentations[end_idx] = {"start": start_idx, "score": score} segmentation = best_segmentations[-1] if segmentation["score"] is None: # We did not find a tokenization of the word -> unknown return ["<unk>"], None score = segmentation["score"] start = segmentation["start"] end = len(word) tokens = [] while start != 0: tokens.insert(0, word[start:end]) next_start = best_segmentations[start]["start"] end = start start = next_start tokens.insert(0, word[start:end]) return tokens, score ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๋“ค๋กœ ์œ„ ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: print(encode_word("Hopefully", model)) print(encode_word("This", model)) ์ด์ œ ๋ง๋ญ‰์น˜์—์„œ ๋ชจ๋ธ์˜ ์†์‹ค์„ ์‰ฝ๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! def compute_loss(model): loss = 0 for word, freq in word_freqs.items(): _, word_loss = encode_word(word, model) loss += freq * word_loss return loss ํ˜„์žฌ ๋ชจ๋ธ์—์„œ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: compute_loss(model) ๊ฐ ํ† ํฐ์˜ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ๋„ ๊ทธ๋ฆฌ ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ ํ† ํฐ์„ ์‚ญ์ œํ•˜์—ฌ ์–ป์€ ๋ชจ๋ธ์˜ ์†์‹ค์„ ๊ณ„์‚ฐํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: import copy def compute_scores(model): scores = {} model_loss = compute_loss(model) for token, score in model.items(): # We always keep tokens of length 1 if len(token) == 1: continue model_without_token = copy.deepcopy(model) _ = model_without_token.pop(token) scores[token] = compute_loss(model_without_token) - model_loss return scores ๊ฐ ํ† ํฐ์— ๋Œ€ํ•ด์„œ ์œ„ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค: scores = compute_scores(model) print(scores["ll"]) print(scores["his"]) "ll"์€ "Hopefully"์˜ ํ† ํฐํ™”์— ์‚ฌ์šฉ๋˜๋ฉฐ ์ด๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ํ† ํฐ "l"์„ ๋Œ€์‹  ๋‘ ๋ฒˆ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ์ถ”๊ฐ€์ ์ธ ์†์‹ค์ด ๋ฐœ์ƒํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค. "his"๋Š” ๊ทธ ์ž์ฒด๋กœ ํ† ํฐํ™”๋œ "This" ๋‹จ์–ด ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ ์†์‹ค์ด 0์ผ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ ๋งค์šฐ ๋น„ํšจ์œจ์ ์ด๋ฏ€๋กœ, SentencePiece๋Š” ํ† ํฐ X๊ฐ€ ์—†๋Š” ๋ชจ๋ธ ์†์‹ค์˜ ๊ทผ์‚ฌ์น˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋Œ€์‹  ๋‚จ์€ vocabulary์˜ ๋ถ„ํ• ๋กœ ํ† ํฐ X๋ฅผ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ชจ๋“  ์ ์ˆ˜๋Š” ๋ชจ๋ธ ์†์‹ค๊ณผ ๋™์‹œ์— ํ•œ ๋ฒˆ์— ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•ด์•ผ ํ•  ์ผ์€ ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ ํ† ํฐ์„ vocabulary์— ์ถ”๊ฐ€ํ•œ ๋‹ค์Œ ์›ํ•˜๋Š” ํฌ๊ธฐ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ vocabulary์—์„œ ํ† ํฐ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ œ๊ฑฐํ•ด ๋‚˜๊ฐ€๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: percent_to_remove = 0.1 while len(model) > 100: scores = compute_scores(model) sorted_scores = sorted(scores.items(), key=lambda x: x[1]) # Remove percent_to_remove tokens with the lowest scores. for i in range(int(len(model) * percent_to_remove)): _ = token_freqs.pop(sorted_scores[i][0]) total_sum = sum([freq for token, freq in token_freqs.items()]) model = {token: -log(freq / total_sum) for token, freq in token_freqs.items()} ์ž…๋ ฅ ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•˜๋ ค๋ฉด ์‚ฌ์ „ ํ† ํฐํ™”๋ฅผ ์ ์šฉํ•œ ๋‹ค์Œ encode_word() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: def tokenize(text, model): words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) pre_tokenized_text = [word for word, offset in words_with_offsets] encoded_words = [encode_word(word, model)[0] for word in pre_tokenized_text] return sum(encoded_words, []) tokenize("This is the Hugging Face course.", model) ์œ ๋‹ˆ๊ทธ๋žจ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ์ฏค์ด๋ฉด ํ† ํฌ ๋‚˜์ด์ €์— ๊ด€ํ•œ ์ „๋ฌธ๊ฐ€๊ฐ€ ๋˜์…จ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋‹ค์Œ ์„น์…˜์—์„œ๋Š” Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋นŒ๋”ฉ ๋ธ”๋ก์„ ํƒ๊ตฌํ•˜๊ณ  ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž์‹ ๋งŒ์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณต๋ถ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 8. ๋ธ”๋ก ๋‹จ์œ„๋กœ ํ† ํฌ ๋‚˜์ด์ € ๋นŒ๋”ฉ ํ•˜๊ธฐ ์ด์ „ ์„น์…˜์—์„œ ๋ณด์•˜๋“ฏ์ด ํ† ํฐํ™”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹จ๊ณ„๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค: ์ •๊ทœํ™” (๊ณต๋ฐฑ์ด๋‚˜ ์•…์„ผํŠธ ์ œ๊ฑฐ, ์œ ๋‹ˆ์ฝ”๋“œ ์ •๊ทœํ™” ๋“ฑ๊ณผ ๊ฐ™์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ์—ฌ๊ฒจ์ง€๋Š” ๋ชจ๋“  ํ…์ŠคํŠธ ์ •์ œ ์ž‘์—…) ์‚ฌ์ „ ํ† ํฐํ™” (์ž…๋ ฅ์„ ๋‹จ์–ด๋“ค๋กœ ๋ถ„๋ฆฌ) ๋ชจ๋ธ์„ ํ†ตํ•œ ์ž…๋ ฅ ์‹คํ–‰ (์‚ฌ์ „ ํ† ํฐํ™”๋œ ๋‹จ์–ด๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐ ์‹œํ€€์Šค ์ƒ์„ฑ) ํ›„์ฒ˜๋ฆฌ (ํ† ํฐ ๋‚˜์ด์ €์˜ ํŠน์ˆ˜ ํ† ํฐ ์ถ”๊ฐ€, attention mask ๋ฐ ํ† ํฐ ์œ ํ˜• ID ์ƒ์„ฑ) ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, ์ „์ฒด ํ”„๋กœ์„ธ์Šค๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์œ„์˜ ๊ฐœ๋ณ„ ๋‹จ๊ณ„์— ๋Œ€ํ•ด ์—ฌ๋Ÿฌ ์˜ต์…˜์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์กŒ์œผ๋ฉฐ, ์ด๋Ÿฌํ•œ ์˜ต์…˜๋“ค์€ ๋ชฉ์ ์— ๋”ฐ๋ผ ์งœ ๋งž์ถฐ์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ์„น์…˜ 2์—์„œ ์„ค๋ช…ํ–ˆ๋˜ ๊ธฐ์กด ํ† ํฌ ๋‚˜์ด์ €์—์„œ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•˜๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์•„์˜ˆ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด์„œ, ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ข…๋ฅ˜์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๋ณด๋‹ค ์ •ํ™•ํ•˜๊ฒŒ ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ค‘์‹ฌ์— Tokenizer ํด๋ž˜์Šค๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๋‹ค์–‘ํ•œ ํ•˜๋ถ€ ๋ชจ๋“ˆ๋“ค์ด ๊ธฐ๋Šฅ๋ณ„๋กœ ๊ฒฐํ•ฉ๋œ ๊ตฌ์„ฑ ์š”์†Œ(building blocks)๊ฐ€ ๊ฒฐํ•ฉ๋œ ํ˜•ํƒœ๋กœ ๊ตฌ์ถ•๋˜์—ˆ์Šต๋‹ˆ๋‹ค: normalizers์—๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ Normalizer ์œ ํ˜•์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค(์ „์ฒด ๋ชฉ๋ก์€ ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•˜์„ธ์š”). pre_tokenizers์—๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์œ ํ˜•์˜ PreTokenizer๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค (์ „์ฒด ๋ชฉ๋ก์€ ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•˜์„ธ์š”). models์—๋Š” BPE, WordPiece ๋ฐ Unigram๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ Model์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค(์ „์ฒด ๋ชฉ๋ก์€ ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•˜์„ธ์š”). trainer์—๋Š” ๋ง๋ญ‰์น˜์—์„œ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ Trainer๊ฐ€ ๋ชจ๋‘ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค(๋ชจ๋ธ ์œ ํ˜•๋‹น ํ•˜๋‚˜์”ฉ, ์ „์ฒด ๋ชฉ๋ก์€ ์—ฌ๊ธฐ). post_processors์—๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ PostProcessor๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค(์ „์ฒด ๋ชฉ๋ก์€ ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•˜์„ธ์š”). decoders์—๋Š” ํ† ํฐํ™” ์ถœ๋ ฅ์„ ๋””์ฝ”๋”ฉ ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ Decoder๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค(์ „์ฒด ๋ชฉ๋ก์€ ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•˜์„ธ์š”). ์—ฌ๊ธฐ์—์„œ ๊ตฌ์„ฑ ์š”์†Œ(building blocks)์˜ ์ „์ฒด ๋ชฉ๋ก์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ง๋ญ‰์น˜ ํ™•๋ณด ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์€ ํ…์ŠคํŠธ ๋ง๋ญ‰์น˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค(์˜ˆ์ œ ์‹คํ–‰ ์‹œ๊ฐ„์„ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ). ๋ง๋ญ‰์น˜๋ฅผ ํš๋“ํ•˜๋Š” ๋‹จ๊ณ„๋Š” ์ด ์žฅ์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ์ˆ˜ํ–‰ํ•œ ๋‹จ๊ณ„์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ์ด๋ฒˆ์—๋Š” WikiText-2 ๋ฐ์ดํ„ฐ ์…‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from datasets import load_dataset dataset = load_dataset("wikitext", name="wikitext-2-raw-v1", split="train") def get_training_corpus(): for i in range(0, len(dataset), 1000): yield dataset[i : i + 1000]["text"] get_training_corpus() ํ•จ์ˆ˜๋Š” 1,000๊ฐœ์˜ ํ…์ŠคํŠธ ๋ฐฐ์น˜(batch)๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ƒ์„ฑ์ž(generator)์ด๋ฉฐ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. Tokenizers๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์—์„œ ์ง์ ‘ ํ•™์Šต๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ์ปฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” WikiText-2์˜ ๋ชจ๋“  ํ…์ŠคํŠธ/์ž…๋ ฅ์„ ํฌํ•จํ•˜๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: with open("wikitext-2.txt", "w", encoding="utf-8") as f: for i in range(len(dataset)): f.write(dataset[i]["text"] + "\n") ์ด์ œ ๋ธ”๋ก ๋‹จ์œ„๋กœ BERT, GPT-2 ๋ฐ XLNet ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋นŒ๋“œ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ๊ฐ๊ฐ WordPiece, BPE ๋ฐ Unigram์˜ ์„ธ ๊ฐ€์ง€ ์ฃผ์š” ํ† ํฐํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ๊ฐ์˜ ์‹ค์ œ ์˜ˆ์‹œ๊ฐ€ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. BERT๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! WordPiece ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋นŒ๋”ฉ ํ•˜๊ธฐ Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋นŒ๋“œ ํ•˜๋ ค๋ฉด ๋จผ์ € model๋กœ Tokenizer ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•œ ๋‹ค์Œ normalizer, pre_tokenizer, post_processor ๋ฐ decoder ์†์„ฑ์„ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ์˜ˆ์—์„œ๋Š” WordPiece ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ Tokenizer๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค: from tokenizers import ( decoders, models, normalizers, pre_tokenizers, processors, trainers, Tokenizer, ) tokenizer = Tokenizer(models.WordPiece(unk_token="[UNK]")) ๋ชจ๋ธ์ด ์ด์ „์— ๋ณธ ์ ์ด ์—†๋Š” ๋ฌธ์ž๋“ค์„ ๋งŒ๋‚ฌ์„ ๋•Œ ๋ฌด์—‡์„ ๋ฐ˜ํ™˜ํ• ์ง€ ์•Œ ์ˆ˜ ์žˆ๋„๋ก unk_token์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ์ธ์ˆ˜์—๋Š” ๋ชจ๋ธ์˜ vocab(์—ฌ๊ธฐ์„œ๋Š” ๋ชจ๋ธ์„ ํ•™์Šตํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์ด๊ฒƒ์„ ์„ค์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค)๊ณผ ๊ฐ ๋‹จ์–ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ง€์ •ํ•˜๋Š” max_input_chars_per_word(์ด ๊ธธ์ด๋ณด๋‹ค ๋” ๊ธด ๋‹จ์–ด๋Š” ๋ถ„ํ• ๋จ)๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ํ† ํฐํ™”์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ์ •๊ทœํ™”(normalization)์ž…๋‹ˆ๋‹ค. BERT๊ฐ€ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— BERT์— ๋Œ€ํ•ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ์ ์ธ ์˜ต์…˜์ด ํฌํ•จ๋œ BertNormalizer๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์—๋Š” lowercase, strip_accents ๋“ฑ์„ ๋น„๋กฏํ•˜์—ฌ, ๋ชจ๋“  ์ œ์–ด ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜๋ณต๋˜๋Š” ๊ณต๋ฐฑ์„ ๋‹จ์ผ ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” clean_text ๊ทธ๋ฆฌ๊ณ  ํ•œ์ž(Chinese characters) ์ฃผ์œ„์— ๊ณต๋ฐฑ์„ ๋ฐฐ์น˜ํ•˜๋Š” handle_chinese_chars๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. bert-base-uncased ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋ณต์ œํ•˜๋ ค๋ฉด ์ด ๋…ธ๋ฉ€๋ผ์ด์ €(normalizer)๋ฅผ ์„ค์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: tokenizer.normalizer = normalizers.BertNormalizer(lowercase=True) ๊ทธ๋Ÿฌ๋‚˜ ์ผ๋ฐ˜์ ์œผ๋กœ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋นŒ๋“œ ํ•  ๋•Œ Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ด๋ฏธ ๊ตฌํ˜„๋œ ํŽธ๋ฆฌํ•œ ๋…ธ๋ฉ€๋ผ์ด์ €์— ์ ‘๊ทผํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ BERT ๋…ธ๋ฉ€๋ผ์ด์ €๋ฅผ ์ง์ ‘ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ๋Š” Lowercase ๋…ธ๋ฉ€๋ผ์ด์ €์™€ StripAccents ๋…ธ๋ฉ€๋ผ์ด์ €๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ Sequence๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋…ธ๋ฉ€๋ผ์ด์ €๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.normalizer = normalizers.Sequence( [normalizers.NFD(), normalizers.Lowercase(), normalizers.StripAccents()] ) ๋˜ํ•œ ์—ฌ๊ธฐ์„œ๋Š” NFD ์œ ๋‹ˆ์ฝ”๋“œ ๋…ธ๋ฉ€๋ผ์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด, StripAccents ๋…ธ๋ฉ€๋ผ์ด์ €๊ฐ€ ์•…์„ผํŠธ๊ฐ€ ์žˆ๋Š” ๋ฌธ์ž๋ฅผ ์ œ๋Œ€๋กœ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด์ „์— ๋ณด์•˜๋“ฏ์ด normalize_str() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฃผ์–ด์ง„ ํ…์ŠคํŠธ์˜ ๋ณ€ํ™” ์–‘์ƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: print(tokenizer.normalizer.normalize_str("Hรฉllรฒ hรดw are รผ?")) To go further ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž u"\u0085"๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์ž์—ด์—์„œ ์œ„ 2๊ฐ€์ง€ ๋…ธ๋ฉ€๋ผ์ด์ €๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ํ™•์‹คํžˆ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. normalizers.Sequence ๋ฒ„์ „์„ ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ฒŒ ๋งŒ๋“ค์ง€ ์•Š๊ธฐ ์œ„ํ•ด, clean_text ์ธ์ˆ˜๊ฐ€ ๊ธฐ๋ณธ ๋™์ž‘์ธ True๋กœ ์„ค์ •๋  ๋•Œ, BertNormalizer๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ์ •๊ทœ์‹ ๋Œ€์ฒด(Regex replacements)๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ๋‘ ๊ฐœ์˜ normalizer.Replace๋ฅผ ๋…ธ๋ฉ€๋ผ์ด์ € ์‹œํ€€์Šค์— ์ถ”๊ฐ€ํ•˜์—ฌ ํŽธํ•œ BertNormalizer๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ์ •ํ™•ํžˆ ๋™์ผํ•œ ์ •๊ทœํ™”๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์‚ฌ์ „ ํ† ํฐํ™” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ „ ๋นŒ๋“œ ๋œ BertPreTokenizer๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.pre_tokenizer = pre_tokenizers.BertPreTokenizer() ๋˜๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ๋นŒ๋“œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.pre_tokenizer = pre_tokenizers.Whitespace() Whitespace ์‚ฌ์ „ ํ† ํฌ ๋‚˜์ด์ €(pre-tokenizer)๋Š” ๊ณต๋ฐฑ์€ ๋ฌผ๋ก , ๋ฌธ์ž, ์ˆซ์ž ๋˜๋Š” ๋ฐ‘์ค„ ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ๋ถ„ํ• ํ•˜๋ฏ€๋กœ, ๊ฒฐ๊ตญ์€ ๊ณต๋ฐฑ๊ณผ ๊ตฌ๋‘์ ์œผ๋กœ ๋ถ„ํ• ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.pre_tokenizer.pre_tokenize_str("Let's test my pre-tokenizer.") ๊ณต๋ฐฑ๋งŒ์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„ํ• ํ•˜๋ ค๋ฉด WhitespaceSplit ์‚ฌ์ „ ํ† ํฌ ๋‚˜์ด์ €(pre-tokenizer) ๋Œ€์‹  ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: pre_tokenizer = pre_tokenizers.WhitespaceSplit() pre_tokenizer.pre_tokenize_str("Let's test my pre-tokenizer.") ๋…ธ๋ฉ€๋ผ์ด์ €์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Sequence๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์‚ฌ์ „ ํ† ํฌ ๋‚˜์ด์ €(pre-tokenizer)๋“ค์„ ๊ฒฐํ•ฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: pre_tokenizer = pre_tokenizers.Sequence( [pre_tokenizers.WhitespaceSplit(), pre_tokenizers.Punctuation()] ) pre_tokenizer.pre_tokenize_str("Let's test my pre-tokenizer.") ํ† ํฐํ™” ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋‹ค์Œ ๋‹จ๊ณ„๋Š” ๋ชจ๋ธ์„ ํ†ตํ•ด ์ž…๋ ฅ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„์—์„œ ๋ชจ๋ธ์„ ์ง€์ •ํ•˜๊ธด ํ–ˆ์ง€๋งŒ ์ด์ œ ํ•™์Šต์ด ํ•„์š”ํ•˜๋ฉฐ ์ด๋ฅผ ์œ„ํ•ด์„œ WordPieceTrainer๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Tokenizers์—์„œ ํŠธ๋ ˆ์ด๋„ˆ(trainer)๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•  ๋•Œ ๊ธฐ์–ตํ•ด์•ผ ํ•  ์ค‘์š”ํ•œ ์ ์€ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๋ชจ๋“  ํŠน์ˆ˜ ํ† ํฐ์„ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ด๋“ค ํ† ํฐ์ด ํ•™์Šต ์ฝ”ํผ์Šค์— ์—†๊ธฐ ๋•Œ๋ฌธ์— ์–ดํœ˜์ง‘(vocabulary)์— ์ถ”๊ฐ€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค: special_tokens = ["[UNK]", "[PAD]", "[CLS]", "[SEP]", "[MASK]"] trainer = trainers.WordPieceTrainer(vocab_size=25000, special_tokens=special_tokens) vocab_size ๋ฐ special_tokens๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ ์™ธ์—๋„ min_frequency(ํ† ํฐ์ด vocabulary์— ํฌํ•จ๋˜๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ์ถœํ˜„ ๋นˆ๋„)๋ฅผ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ continue_subword_prefix(##๋ง๊ณ  ๋‹ค๋ฅธ ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ)๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ ์ •์˜ํ•œ ๋ฐ˜๋ณต์ž(iterator)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: tokenizer.train_from_iterator(get_training_corpus(), trainer=trainer) ๋˜ํ•œ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์ฒ˜๋Ÿผ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ฏธ๋ฆฌ ๋น„์–ด์žˆ๋Š” WordPiece๋กœ ๋ชจ๋ธ์„ ๋‹ค์‹œ ์ดˆ๊ธฐํ™”ํ–ˆ์Šต๋‹ˆ๋‹ค: tokenizer.model = models.WordPiece(unk_token="[UNK]") tokenizer.train(["wikitext-2.txt"], trainer=trainer) ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ encode() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ…์ŠคํŠธ์—์„œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: encoding = tokenizer.encode("Let's test this tokenizer") print(encoding.tokens) ์œ„์—์„œ encoding์€ ๋‹ค์–‘ํ•œ ์†์„ฑ(ids, type_ids, tokens, offsets, attention_mask, special_tokens_mask, overflowing)์— ํ† ํฌ ๋‚˜์ด์ €์˜ ๋ชจ๋“  ํ•„์š”ํ•œ ์ถœ๋ ฅ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” Encoding ํด๋ž˜์Šค์˜ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ํ† ํฐํ™” ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” ํ›„์ฒ˜๋ฆฌ(post-processing)์ž…๋‹ˆ๋‹ค. ์‹œ์ž‘ ๋ถ€๋ถ„์— [CLS] ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋๋ถ€๋ถ„์— [SEP] ํ† ํฐ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•œ ์Œ์˜ ๋ฌธ์žฅ์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ฐ ๋ฌธ์žฅ ๋’ค์— [SEP]๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ, TemplateProcessor๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๋จผ์ € ์–ดํœ˜์ง‘(vocabulary)์—์„œ [CLS] ๋ฐ [SEP] ํ† ํฐ์˜ ID๋ฅผ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค: cls_token_id = tokenizer.token_to_id("[CLS]") sep_token_id = tokenizer.token_to_id("[SEP]") print(cls_token_id, sep_token_id) TemplateProcessor ์šฉ ํ…œํ”Œ๋ฆฟ์„ ์ž‘์„ฑํ•˜๋ ค๋ฉด ๋‹จ์ผ ๋ฌธ์žฅ๊ณผ ๋ฌธ์žฅ ์Œ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•˜๋ ค๋Š” ํŠน์ˆ˜ ํ† ํฐ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ(๋˜๋Š” ๋‹จ์ผ) ๋ฌธ์žฅ์€ $A๋กœ ํ‘œ์‹œ๋˜๊ณ  ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ(์Œ์„ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๊ฒฝ์šฐ)์€ $B๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด๋“ค ๊ฐ๊ฐ(ํŠน์ˆ˜ ํ† ํฐ ๋ฐ ๋ฌธ์žฅ)์— ๋Œ€ํ•ด ์ฝœ๋ก (colon) ๋’ค์— ํ•ด๋‹น ํ† ํฐ ์œ ํ˜• ID๋„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ณ ์ „์ ์ธ BERT ํ…œํ”Œ๋ฆฟ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค: tokenizer.post_processor = processors.TemplateProcessing( single=f"[CLS]:0 $A:0 [SEP]:0", pair=f"[CLS]:0 $A:0 [SEP]:0 $B:1 [SEP]:1", special_tokens=[("[CLS]", cls_token_id), ("[SEP]", sep_token_id)] ) ํŠน์ˆ˜ ํ† ํฐ์˜ ID๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์ด๋ฅผ ํ•ด๋‹น ID๋กœ ์ ์ ˆํ•˜๊ฒŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋‹จ ์ถ”๊ฐ€๋˜๋ฉด ์ด์ „ ์˜ˆ์ œ์˜ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: encoding = tokenizer.encode("Let's test this tokenizer.") print(encoding.tokens) ๋˜ํ•œ, 2๊ฐœ์˜ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค: encoding = tokenizer.encode("Let's test this tokenizer...", "on a pair of sentences.") print(encoding.tokens) print(encoding.type_ids) ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ฑฐ์˜ ์™„์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” ๋””์ฝ”๋”๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: tokenizer.decoder = decoders.WordPiece(prefix="##") ์œ„์˜ encoding์„ ์ด์šฉํ•ด์„œ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: tokenizer.decode(encoding.ids) ์ข‹์Šต๋‹ˆ๋‹ค! ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹จ์ผ JSON ํŒŒ์ผ์— ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.save("tokenizer.json") ๊ทธ๋Ÿฐ ๋‹ค์Œ from_file() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Tokenizer ๊ฐ์ฒด์—์„œ ํ•ด๋‹น ํŒŒ์ผ์„ ๋‹ค์‹œ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: new_tokenizer = Tokenizer.from_file("tokenizer.json") Transformers์—์„œ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด PreTrainedTokenizerFast๋กœ ๋ž˜ํ•‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ œ๋„ˆ๋ฆญ ํด๋ž˜์Šค(generic class)๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๊ธฐ์กด ๋ชจ๋ธ์— ํ•ด๋‹นํ•˜๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ํด๋ž˜์Šค(์—ฌ๊ธฐ์„œ๋Š” BertTokenizerFast)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ•์˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์งธ ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. PreTrainedTokenizerFast๋ฅผ ๊ฐ€์ง€๊ณ  ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋ž˜ํ•‘ ํ•˜๋ ค๋ฉด ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ํ† ํฌ ๋‚˜์ด์ €๋ฅผ tokenizer_object๋กœ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ tokenizer_file๋กœ ์ €์žฅํ•œ ํ† ํฌ ๋‚˜์ด์ € ํŒŒ์ผ์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์–ตํ•ด์•ผ ํ•  ์ค‘์š”ํ•œ ์ ์€ ๋ชจ๋“  ํŠน์ˆ˜ ํ† ํฐ์„ ์ˆ˜๋™์œผ๋กœ ์ง์ ‘ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. PreTrainedTokenizerFast ํด๋ž˜์Šค๋Š” tokenizer ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์–ด๋–ค ํ† ํฐ์ด ๋งˆ์Šคํฌ ํ† ํฐ์ธ์ง€ ์•„๋‹ˆ๋ฉด [CLS] ํ† ํฐ ๋“ฑ์ธ์ง€ ์ถ”๋ก ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค: from transformers import PreTrainedTokenizerFast wrapped_tokenizer = PreTrainedTokenizerFast( tokenizer_object=tokenizer, unk_token="[UNK]", pad_token="[PAD]", cls_token="[CLS]", sep_token="[SEP]", mask_token="[MASK]", ) ํŠน์ • ํ† ํฌ ๋‚˜์ด์ € ํด๋ž˜์Šค(์˜ˆ: BertTokenizerFast)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์ •๋œ ํ† ํฐ(์—ฌ๊ธฐ์„œ๋Š” ์—†์Œ)๊ณผ ๋‹ค๋ฅธ ํŠน์ˆ˜ ํ† ํฐ๋งŒ ์ง€์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: from transformers import BertTokenizerFast wrapped_tokenizer = BertTokenizerFast(tokenizer_object=tokenizer) ์ด์ œ ์œ„ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋‹ค๋ฅธ Transformers ํ† ํฌ ๋‚˜์ด์ €์ฒ˜๋Ÿผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, save_pretrained() ๋ฉ”์„œ๋“œ๋กœ ์ €์žฅํ•˜๊ฑฐ๋‚˜ push_to_hub() ๋ฉ”์„œ๋“œ๋กœ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ๊นŒ์ง€ WordPiece ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋นŒ๋“œ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์•˜์œผ๋ฏ€๋กœ BPE ํ† ํฌ ๋‚˜์ด์ €์— ๋Œ€ํ•ด์„œ๋„ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋‹จ๊ณ„๋ฅผ ์•Œ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๋” ๋น ๋ฅด๊ฒŒ ์ง„ํ–‰ํ•˜๊ณ  ์ฐจ์ด์ ๋งŒ ๊ฐ•์กฐ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. BPE ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋นŒ๋”ฉ ํ•˜๊ธฐ ์ด์ œ GPT-2 ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋นŒ๋“œ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. BERT ํ† ํฌ ๋‚˜์ด์ €์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ BPE ๋ชจ๋ธ๋กœ Tokenizer๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: tokenizer = Tokenizer(models.BPE()) ์•ž์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ธฐ์กด์— ์–ดํœ˜์ง‘(vocabulary)์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋ชจ๋ธ์„ ํ•ด๋‹น ์–ดํœ˜์ง‘(vocabulary)์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ(์ด ๊ฒฝ์šฐ vocab๊ณผ merges๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•จ) ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿด ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ GPT-2๋Š” byte-level BPE(๋ฐ”์ดํŠธ ์ˆ˜์ค€ BPE)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— unk_token์„ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. GPT-2๋Š” ๋…ธ๋ฉ€๋ผ์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํ•ด๋‹น ๋‹จ๊ณ„๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  ์‚ฌ์ „ ํ† ํฐํ™”(pre-tokenization)๋กœ ๋ฐ”๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค: tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=False) ์—ฌ๊ธฐ์—์„œ ByteLevel์— ์ถ”๊ฐ€ํ•œ ์˜ต์…˜์€ ๋ฌธ์žฅ ์‹œ์ž‘ ๋ถ€๋ถ„์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ์ด์ „๊ณผ ๋™์ผํ•œ ์‚ฌ์ „ ํ† ํฐํ™” ๊ฒฐ๊ณผ๋ฅผ ์•„๋ž˜์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.pre_tokenizer.pre_tokenize_str("Let's test pre-tokenization!") ๋‹ค์Œ์€ ํ•™์Šต์ด ํ•„์š”ํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. GPT-2์˜ ๊ฒฝ์šฐ, ์œ ์ผํ•œ ํŠน์ˆ˜ ํ† ํฐ์€ ํ…์ŠคํŠธ ๋(end-of-text) ํ† ํฐ์ž…๋‹ˆ๋‹ค: trainer = trainers.BpeTrainer(vocab_size=25000, special_tokens=["<|endoftext|>"]) tokenizer.train_from_iterator(get_training_corpus(), trainer=trainer) WordPieceTrainer์™€ vocab_size ๋ฐ special_tokens์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์›ํ•˜๋Š” ๊ฒฝ์šฐ min_frequency๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ๋‹จ์–ด ๋ ์ ‘๋ฏธ์‚ฌ(end-of-word suffix)๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ(์˜ˆ: </w>) end_of_word_suffix ์ธ์ˆ˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์— ๋Œ€ํ•ด์„œ๋„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.model = models.BPE() tokenizer.train(["wikitext-2.txt"], trainer=trainer) ์ƒ˜ํ”Œ ํ…์ŠคํŠธ๋กœ ํ† ํฐํ™” ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: encoding = tokenizer.encode("Let's test this tokenizer.") print(encoding.tokens) ๋‹ค์Œ๊ณผ ๊ฐ™์ด GPT-2 ํ† ํฌ ๋‚˜์ด์ €์— ๋Œ€ํ•œ ๋ฐ”์ดํŠธ ์ˆ˜์ค€(byte-level) ํ›„์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค: tokenizer.post_processor = processors.ByteLevel(trim_offsets=False) trim_offsets = False ์˜ต์…˜์€ ํ›„์ฒ˜๋ฆฌ๊ธฐ(post-processor)๊ฐ€ 'ฤ '๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ† ํฐ์˜ ์˜คํ”„์…‹์„ ๊ทธ๋Œ€๋กœ ๋‘์–ด์•ผ ํ•จ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์˜คํ”„์…‹์˜ ์‹œ์ž‘์€ ๋‹จ์–ด์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ์•„๋‹ˆ๋ผ ๋‹จ์–ด ์•ž์˜ ๊ณต๋ฐฑ์„ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๊ณต๋ฐฑ๋„ ๊ธฐ์ˆ ์ ์œผ๋กœ ํ† ํฐ์˜ ์ผ๋ถ€์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฐฉ๊ธˆ ์ธ์ฝ”๋”ฉํ•œ ํ…์ŠคํŠธ๋กœ ๊ฒฐ๊ณผ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ 'ฤ test'๋Š” ์ธ๋ฑ์Šค 4์˜ ํ† ํฐ์ž…๋‹ˆ๋‹ค: sentence = "Let's test this tokenizer." encoding = tokenizer.encode(sentence) start, end = encoding.offsets[4] sentence[start:end] ๋งˆ์ง€๋ง‰์œผ๋กœ, ๋ฐ”์ดํŠธ ์ˆ˜์ค€(byte-level) ๋””์ฝ”๋”๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: tokenizer.decoder = decoders.ByteLevel() ์ด๊ฒƒ์ด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ๋‹ค์‹œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.decode(encoding.ids) ์ด์ œ ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฏ€๋กœ, ์ด์ „๊ณผ ๊ฐ™์ด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•˜๊ณ , ์ด๋ฅผ Transformers์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋ฅผ ์›ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” PreTrainedTokenizerFast ๋˜๋Š” GPT2TokenizerFast๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ž˜ํ•‘(wrapping) ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import PreTrainedTokenizerFast wrapped_tokenizer = PreTrainedTokenizerFast( tokenizer_object=tokenizer, bos_token="<|endoftext|>", eos_token="<|endoftext|>", ) from transformers import GPT2TokenizerFast wrapped_tokenizer = GPT2TokenizerFast(tokenizer_object=tokenizer) ์ด์ œ ๋งˆ์ง€๋ง‰ ์˜ˆ์ œ๋กœ Unigram ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋นŒ๋“œ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. Unigram ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋นŒ๋”ฉ ํ•˜๊ธฐ ์ด์ œ XLNet ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋นŒ๋“œ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ „ ํ† ํฌ ๋‚˜์ด์ €์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Unigram ๋ชจ๋ธ๋กœ Tokenizer๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: tokenizer = Tokenizer(models.Unigram()) ์—ญ์‹œ ์—ฌ๊ธฐ์„œ๋„, ์–ดํœ˜์ง‘(vocabulary)์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋ชจ๋ธ์„ ์–ดํœ˜์ง‘์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœํ™”(normalization)๋ฅผ ์œ„ํ•ด XLNet์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ๋Œ€์ฒด ๊ทœ์น™(relpacements, SentencePiece์—์„œ ์ œ๊ณต)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from tokenizers import Regex tokenizer.normalizer = normalizers.Sequence( [ normalizers.Replace("``", '"'), normalizers.Replace("''", '"'), normalizers.NFKD(), normalizers.StripAccents(), normalizers.Replace(Regex(" {2, }"), " "), ] ) ์œ„ ๋Œ€์ฒด ๊ทœ์น™์€ โ€œ ๋ฐ โ€๋ฅผ โ€๋กœ ๋Œ€์ฒดํ•˜๊ณ , ๋‘˜ ์ด์ƒ์˜ ๊ณต๋ฐฑ ์‹œํ€€์Šค๋ฅผ ๋‹จ์ผ ๊ณต๋ฐฑ์œผ๋กœ ๋Œ€์ฒดํ•˜๋ฉฐ, ํ† ํฐํ™”ํ•  ํ…์ŠคํŠธ์˜ ์•…์„ผํŠธ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  SentencePiece ํ† ํฌ ๋‚˜์ด์ €์— ์‚ฌ์šฉ๋˜๋Š” ์‚ฌ์ „ ํ† ํฌ ๋‚˜์ด์ €(pre-tokenizer)๋Š” Metaspace์ž…๋‹ˆ๋‹ค. tokenizer.pre_tokenizer = pre_tokenizers.Metaspace() ์ด์ „์˜ ์˜ˆ์ œ ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ์‚ฌ์ „ ํ† ํฐํ™” ๊ฒฐ๊ณผ๋ฅผ ํ•œ๋ฒˆ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: tokenizer.pre_tokenizer.pre_tokenize_str("Let's test the pre-tokenizer!") ๋‹ค์Œ์œผ๋กœ ๋ชจ๋ธ ํ•™์Šต์ž…๋‹ˆ๋‹ค. XLNet์—๋Š” ๋ช‡ ๊ฐ€์ง€ ํŠน๋ณ„ํ•œ ํ† ํฐ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค: special_tokens = ["<cls>", "<sep>", "<unk>", "<pad>", "<mask>", "<s>", "</s>"] trainer = trainers.UnigramTrainer( vocab_size=25000, special_tokens=special_tokens, unk_token="<unk>" ) tokenizer.train_from_iterator(get_training_corpus(), trainer=trainer) UnigramTrainer์—์„œ ๋ปฌ๋จน์ง€ ๋ง์•„์•ผ ํ•  ๋งค์šฐ ์ค‘์š”ํ•œ ์ธ์ˆ˜๋Š” unk_token์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ํ† ํฐ์„ ์ œ๊ฑฐํ•˜๋Š” ๊ฐ ๋‹จ๊ณ„์— ๋Œ€ํ•œ shrinking_factor(๊ธฐ๋ณธ๊ฐ’์€ 0.75), ๋˜๋Š” ์ฃผ์–ด์ง„ ํ† ํฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•œ max_piece_length(๊ธฐ๋ณธ๊ฐ’์€ 16) ๋“ฑ๊ณผ ๊ฐ™์€ Unigram ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ํŠนํ™”๋œ ์ถ”๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์„ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ์—ญ์‹œ ํ…์ŠคํŠธ ํŒŒ์ผ์— ๋Œ€ํ•ด์„œ๋„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.model = models.Unigram() tokenizer.train(["wikitext-2.txt"], trainer=trainer) ์ƒ˜ํ”Œ ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ํ† ํฐํ™” ๊ฒฐ๊ณผ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: encoding = tokenizer.encode("Let's test this tokenizer.") print(encoding.tokens) XLNet์˜ ํŠน์ง•์€ ํ† ํฐ ํƒ€์ž… ID๊ฐ€ 2์ธ <cls> ํ† ํฐ์„ ๋ฌธ์žฅ ๋์— ์ถ”๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ขŒ์ธก ํŒจ๋”ฉ์ž…๋‹ˆ๋‹ค. BERT์™€ ๊ฐ™์ด ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ํŠน์ˆ˜ ํ† ํฐ๊ณผ ํ† ํฐ ํƒ€์ž… ID๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ์ „์— ๋จผ์ € <cls> ๋ฐ <sep> ํ† ํฐ์˜ ID๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค: cls_token_id = tokenizer.token_to_id("<cls>") sep_token_id = tokenizer.token_to_id("<sep>") print(cls_token_id, sep_token_id) ํ…œํ”Œ๋ฆฟ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค: tokenizer.post_processor = processors.TemplateProcessing( single="$A:0 <sep>:0 <cls>:2", pair="$A:0 <sep>:0 $B:1 <sep>:1 <cls>:2", special_tokens=[("<sep>", sep_token_id), ("<cls>", cls_token_id)], ) ์ด์ œ ํ•œ ์Œ์˜ ๋ฌธ์žฅ์— ๋Œ€ํ•œ ์ธ์ฝ”๋”ฉ์ด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: encoding = tokenizer.encode("Let's test this tokenizer...", "on a pair of sentences!") print(encoding.tokens) print(encoding.type_ids) ๋งˆ์ง€๋ง‰์œผ๋กœ Metaspace ๋””์ฝ”๋”๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: tokenizer.decoder = decoders.Metaspace() ํ† ํฌ ๋‚˜์ด์ € ๋นŒ๋“œ๋ฅผ ๋๋ƒˆ์Šต๋‹ˆ๋‹ค! ์ด์ „๊ณผ ๊ฐ™์ด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•˜๊ณ  Transformers ๋‚ด์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด PreTrainedTokenizerFast ๋˜๋Š” XLNetTokenizerFast๋กœ ๋ž˜ํ•‘ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. PreTrainedTokenizerFast๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ•œ ๊ฐ€์ง€ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ํŠน์ˆ˜ ํ† ํฐ ์ง€์ •๊ณผ ๋”๋ถˆ์–ด Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์™ผ์ชฝ์„ ์ฑ„์šฐ๋„๋ก ์ง€์‹œ(padding_side="left") ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: from transformers import PreTrainedTokenizerFast wrapped_tokenizer = PreTrainedTokenizerFast( tokenizer_object=tokenizer, bos_token="<s>", eos_token="</s>", unk_token="<unk>", pad_token="<pad>", cls_token="<cls>", sep_token="<sep>", mask_token="<mask>", padding_side="left", ) from transformers import XLNetTokenizerFast wrapped_tokenizer = XLNetTokenizerFast(tokenizer_object=tokenizer) ์ง€๊ธˆ๊นŒ์ง€ ๋‹ค์–‘ํ•œ ์š”์†Œ ๋ชจ๋“ˆ๋“ค์ด ๊ธฐ์กด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ƒˆ๋กญ๊ฒŒ ๋นŒ๋“œ ํ•˜๋Š” ๋ฐ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๋Š”์ง€ ๋ณด์•˜์œผ๋ฏ€๋กœ, ์ด ์„ค๋ช…์„ ํ™œ์šฉํ•˜์—ฌ Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์›ํ•˜๋Š” ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  Transformers์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 7์žฅ. ์ฃผ์š” NLP ํƒœ์Šคํฌ ์‹ค์ œ ๊ตฌํ˜„ ๋ฐฉ๋ฒ• ์šฐ๋ฆฌ๋Š” 3์žฅ์—์„œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜(text classification)๋ฅผ ์œ„ํ•œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด ์žฅ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฃผ์š” NLP ํƒœ์Šคํฌ๋“ค์„ ๋‹ค๋ฃฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ† ํฐ ๋ถ„๋ฅ˜ (Token Classification) ๋งˆ์Šคํ‚น ๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง (Masked Language Modeling) ์š”์•ฝ (Summarization) ๋ฒˆ์—ญ (Translation) ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง ์‚ฌ์ „ํ•™์Šต (Causal Language Modeling Pretraining like GPT-2) ์งˆ์˜์‘๋‹ต (Question Answering) ์ด๋ฅผ ์œ„ํ•ด์„œ, Trainer API๋ฅผ ๋น„๋กฏํ•˜์—ฌ, 3์žฅ์—์„œ ๋ฐฐ์šด Accelerate ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, 5์žฅ์˜ Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, 6์žฅ์—์„œ ์‚ดํŽด๋ณธ Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋‘๋ฅผ ํ™œ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ 4์žฅ์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ๊ฐ ์„น์…˜์—์„œ์˜ ๊ฒฐ๊ณผ๋ฅผ Model Hub์— ์—…๋กœ๋“œํ•  ๊ฒƒ์ด๋ฏ€๋กœ ๊ฒฐ๊ตญ์€ ์ง€๊ธˆ๊นŒ์ง€ ๊ณต๋ถ€ํ•œ ๋ชจ๋“  ๋‚ด์šฉ์ด ์„ค๋ช…๋˜๋Š” ์ด์ฒด์ ์ธ ์ฑ•ํ„ฐ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ ์„น์…˜์€ ๋…๋ฆฝ์ ์œผ๋กœ ์ฝ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ Accelerate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Trainer API ๋˜๋Š” ์ž์ฒด ํ•™์Šต ๋ฃจํ”„(training loop)๋กœ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์–ด๋Š ๋ถ€๋ถ„์ด๋“  ๊ฑด๋„ˆ๋›ฐ๊ณ  ๊ฐ€์žฅ ๊ด€์‹ฌ ์žˆ๋Š” ๋ถ€๋ถ„์— ์ง‘์ค‘ํ•ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. Trainer API๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์„ธ์„ธํ•œ ๋ถ€๋ถ„์— ๋Œ€ํ•ด ๊ฑฑ์ •ํ•˜์ง€ ์•Š๊ณ  ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ฑฐ๋‚˜ ํ•™์Šตํ•˜๋Š”๋ฐ ์ ํ•ฉํ•˜๋ฉฐ, Accelerate๊ฐ€ ์žˆ๋Š” ํ•™์Šต ๋ฃจํ”„(training loop)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ๋ถ€๋ถ„์„ ๋” ์‰ฝ๊ฒŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ ์žฅ์˜ ์„น์…˜์„ ์ˆœ์„œ๋Œ€๋กœ ์ฝ์œผ๋ฉด ์ฝ”๋“œ์™€ ๋‚ด์šฉ์— ๊ณตํ†ต์ ์ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐ˜๋ณต์€ ์˜๋„์ ์ด๋ฏ€๋กœ ๊ด€์‹ฌ ์žˆ๋Š” ์ž‘์—…์— ์ง‘์ค‘ํ•˜๊ณ (๋˜๋Š” ๋‚˜์ค‘์— ๋‹ค์‹œ ๋Œ์•„์™€์„œ) ์™„์ „ํ•œ ์‹คํ–‰ ์˜ˆ์‹œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. ํ† ํฐ ๋ถ„๋ฅ˜ (Token Classification) ์šฐ๋ฆฌ๊ฐ€ ์‚ดํŽด๋ณผ ์ฒซ ๋ฒˆ์งธ NLP ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์€ ํ† ํฐ ๋ถ„๋ฅ˜(token classification)์ž…๋‹ˆ๋‹ค. ์ด ํฌ๊ด„์ ์ธ ์ž‘์—…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด "๋ฌธ์žฅ์˜ ๊ฐ ํ† ํฐ์— ๋ ˆ์ด๋ธ”์„ ์ง€์ •" ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ •ํ˜•ํ™”๋  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค: Named entity recognition (NER): ๋ฌธ์žฅ์—์„œ ๊ฐœ์ฒด๋ช…(ํ˜น์€ ์—”ํ„ฐํ‹ฐ, ์˜ˆ: ์‚ฌ๋žŒ, ์œ„์น˜ ๋˜๋Š” ์กฐ์ง)์„ ์ฐพ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ๊ฐœ์ฒด ๋ช…๋‹น ํ•˜๋‚˜์˜ ํด๋ž˜์Šค์™€ "๊ฐœ์ฒด๋ช…์ด ์•„๋‹˜(no entity)"์— ๋Œ€ํ•œ ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํ† ํฐ์— ๋ ˆ์ด๋ธ”์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ณต์‹ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Part-of-speech tagging (POS): ๋ฌธ์žฅ์˜ ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ํŠน์ • ํ’ˆ์‚ฌ(๋ช…์‚ฌ, ๋™์‚ฌ, ํ˜•์šฉ์‚ฌ ๋“ฑ)๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. Chunking: ๋™์ผํ•œ ๊ฐœ์ฒด๋ช… ํ˜น์€ ์—”ํ„ฐํ‹ฐ์— ์†ํ•œ ํ† ํฐ์„ ์ฐพ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…(POS ๋˜๋Š” NER์™€ ๊ฒฐํ•ฉ ๊ฐ€๋Šฅ)์€ ์ฒญํฌ(chunk)์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ์žˆ๋Š” ํ† ํฐ์— ํ•œ ๋ ˆ์ด๋ธ”(๋ณดํ†ต B-)์„, ์ฒญํฌ ๋‚ด๋ถ€์— ์žˆ๋Š” ํ† ํฐ์— ๋‹ค๋ฅธ ๋ ˆ์ด๋ธ”(๋ณดํ†ต I-)์„, ๊ทธ๋ฆฌ๊ณ  ์ฒญํฌ์— ์†ํ•˜์ง€ ์•Š๋Š” ํ† ํฐ์— ๋Œ€ํ•ด์„œ๋Š” ์„ธ ๋ฒˆ์งธ ๋ ˆ์ด๋ธ”(๋ณดํ†ต O)์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ณต์‹ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋‹ค๋ฅธ ์œ ํ˜•์˜ ํ† ํฐ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋“ค๋„ ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„๋Š” ๋‹จ์ง€ ๋ช‡ ๊ฐ€์ง€ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ผ๋ฟ์ž…๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ์‚ฌ์ „ ํ•™์Šต ๋ชจ๋ธ(BERT)์„ NER ์ž‘์—…์— ๋Œ€ํ•ด ๋ฏธ์„ธ ์กฐ์ •ํ•˜์—ฌ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šตํ•  ๋ชจ๋ธ์„ ์ฐพ์•„ Hub์— ์—…๋กœ๋“œํ•˜๊ณ  ์—ฌ๊ธฐ์—์„œ ์˜ˆ์ธก์„ ๋‹ค์‹œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ค€๋น„ ๋จผ์ € ํ† ํฐ ๋ถ„๋ฅ˜์— ์ ํ•ฉํ•œ ๋ฐ์ดํ„ฐ ์…‹์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” Reuters์˜ ๋‰ด์Šค ๊ธฐ์‚ฌ๊ฐ€ ํฌํ•จ๋œ CoNLL-2003 ๋ฐ์ดํ„ฐ ์…‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์ด ํ•ด๋‹น ๋ ˆ์ด๋ธ”์ด ์žˆ๋Š” ๋‹จ์–ด๋กœ ๋ถ„ํ• ๋œ ํ…์ŠคํŠธ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ธฐ๋งŒ ํ•œ๋‹ค๋ฉด ์—ฌ๊ธฐ์— ์„ค๋ช…๋œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ ˆ์ฐจ๋ฅผ ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ ์…‹์— ๊ทธ๋Œ€๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Dataset์—์„œ ์‚ฌ์šฉ์ž ์ •์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋ณต์Šต์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ 5์žฅ์„ ๋‹ค์‹œ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. CoNLL-2003 ๋ฐ์ดํ„ฐ ์…‹ CoNLL-2003 ๋ฐ์ดํ„ฐ ์…‹์„ ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด, Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ load_dataset() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from datasets import load_dataset # CoNLL-2003 URL์ด ๋ณ€๊ฒฝ๋˜์—ˆ์Œ. raw_datasets = load_dataset("conll2003", revision="master") GLUE MRPC ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•ด 3์žฅ์—์„œ ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์บ์‹œ ํ•ฉ๋‹ˆ๋‹ค. raw_datasets ๊ฐ์ฒด๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์—ด(columns)์ด ์กด์žฌํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋˜ํ•œ ํ•™์Šต, ๊ฒ€์ฆ ๋ฐ ํ…Œ์ŠคํŠธ ์ง‘ํ•ฉ ๊ฐ„์˜ ๋ถ„ํ• ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค: raw_datasets ํŠนํžˆ ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์„ธ ๊ฐ€์ง€ ์ž‘์—…์ธ NER, POS ๋ฐ Chunking์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ”์ด ๋ฐ์ดํ„ฐ ์…‹์— ํฌํ•จ๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์…‹๊ณผ์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ๊ฐ€ ๋ฌธ์žฅ์ด๋‚˜ ๋ฌธ์„œ๋กœ ํ‘œํ˜„๋˜์ง€ ์•Š๊ณ  ๋‹จ์–ด์˜ ๋ชฉ๋ก์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์—ด์€ token์ด๋ผ๊ณ  ํ•˜์ง€๋งŒ ํ•˜์œ„ ๋‹จ์–ด ํ† ํฐํ™”(subword tokenization)๋ฅผ ์œ„ํ•ด ์—ฌ์ „ํžˆ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ†ต๊ณผํ•ด์•ผ ํ•˜๋Š” ์‚ฌ์ „ ํ† ํฐ ํ™”(pre-tokenization) ๋œ ์ž…๋ ฅ์ด๋ผ๋Š” ์˜๋ฏธ์—์„œ ๋‹จ์–ด(word)๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ์ง‘ํ•ฉ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: raw_datasets["train"][0]["tokens"] ๊ฐœ์ฒด๋ช… ์ธ์‹(named entity recognition)์„ ์ˆ˜ํ–‰ํ•  ์˜ˆ์ •์ด๋ฏ€๋กœ NER ํƒœ๊ทธ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: raw_datasets["train"][0]["ner_tags"] ์œ„ ๊ฐ’๋“ค์€ ํ•™์Šต์„ ์œ„ํ•ด ์ค€๋น„๋œ ์ •์ˆ˜ ๋ ˆ์ด๋ธ”์ด์ง€๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ด์šฉ์ ์œผ๋กœ ๊ฒ€ํ† ํ•  ๋•Œ ํ•ญ์ƒ ์œ ์šฉํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ์ดํ„ฐ ์…‹์˜ features ์†์„ฑ์„ ํ™•์ธํ•˜์—ฌ ํ•ด๋‹น ์ •์ˆ˜๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ ˆ์ด๋ธ”๋ช…์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ner_feature = raw_datasets["train"].features["ner_tags"] ner_feature ์ด ์—ด(column)์—๋Š” ClassLabel์˜ ์‹œํ€€์Šค๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์‹œํ€€์Šค ์š”์†Œ์˜ ํƒ€์ž…์€ ner_feature์˜ feature ์†์„ฑ ๋‚ด์— ์žˆ์œผ๋ฉฐ ํ•ด๋‹น feature์˜ names ์†์„ฑ์„ ํ†ตํ•ด์„œ ์ด๋ฆ„ ๋ชฉ๋ก์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: label_names = ner_feature.feature.names label_names 6์žฅ์—์„œ token-classification ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ณต๋ถ€ํ•  ๋•Œ ์ด๋ฏธ ์ด๋Ÿฌํ•œ ๋ ˆ์ด๋ธ”๋“ค์„ ์‚ดํŽด๋ดค์œผ๋‚˜ ๋ณต์Šต ์‚ผ์•„ ๊ฐ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋‹ค์‹œ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: O๋Š” ๋‹จ์–ด๊ฐ€ ์–ด๋–ค ๊ฐœ์ฒด๋ช…์—๋„ ํ•ด๋‹นํ•˜์ง€ ์•Š์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. B-PER/I-PER์€ ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ person ๊ฐœ์ฒด ๋ช…์˜ ์‹œ์ž‘/๋‚ด๋ถ€์— ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. B-ORG/I-ORG๋Š” ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ organization ๊ฐœ์ฒด ๋ช…์˜ ์‹œ์ž‘/๋‚ด๋ถ€์— ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. B-LOC/I-LOC๋Š” ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ location ๊ฐœ์ฒด ๋ช…์˜ ์‹œ์ž‘/๋‚ด๋ถ€์— ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. B-MISC/I-MISC๋Š” ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ miscellaneous ๊ฐœ์ฒด ๋ช…์˜ ์‹œ์ž‘/๋‚ด๋ถ€์— ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ ˆ์ด๋ธ”์„ ๋””์ฝ”๋”ฉํ•จ์œผ๋กœ์จ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: words = raw_datasets["train"][0]["tokens"] labels = raw_datasets["train"][0]["ner_tags"] line1 = "" line2 = "" for word, label in zip(words, labels): full_label = label_names[label] max_length = max(len(word), len(full_label)) line1 += word + " " * (max_length - len(word) + 1) line2 += full_label + " " * (max_length - len(full_label) + 1) print(line1) print(line2) ๋˜ํ•œ, B- ๋ฐ I- ๋ ˆ์ด๋ธ”์ด ํ•จ๊ป˜ ์กด์žฌํ•˜๋Š” ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด๊ธฐ ์œ„ํ•ด, ์ธ๋ฑ์Šค 4์— ์ €์žฅ๋œ ํ•™์Šต ์ง‘ํ•ฉ ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ง€์š”: words = raw_datasets["train"][4]["tokens"] labels = raw_datasets["train"][4]["ner_tags"] line1 = "" line2 = "" for word, label in zip(words, labels): full_label = label_names[label] max_length = max(len(word), len(full_label)) line1 += word + " " * (max_length - len(word) + 1) line2 += full_label + " " * (max_length - len(full_label) + 1) print(line1) print(line2) ๋ณด๋‹ค์‹œํ”ผ, "European Union" ๋ฐ "Werner Zwingmann"๊ณผ ๊ฐ™์€ ๋‘ ๋‹จ์–ด์— ๊ฑธ์ณ ์žˆ๋Š” ๊ฐœ์ฒด๋ช…์€ ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด์— ๋Œ€ํ•ด B-๋ ˆ์ด๋ธ”, ๋‘ ๋ฒˆ์งธ ๋‹จ์–ด์— ๋Œ€ํ•ด I-๋ ˆ์ด๋ธ”์ด ์ง€์ •๋ฉ๋‹ˆ๋‹ค. โœ Your turn! ์œ„์™€ ๋™์ผํ•œ ๋ฌธ์žฅ๋“ค์— ๋Œ€ํ•ด์„œ POS์™€ chunking ๋ ˆ์ด๋ธ”์„ ์ถœ๋ ฅํ•ด ๋ณด์„ธ์š”. ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์•ž์—์„œ ๊ณ„์† ๊ฐ•์กฐํ•œ ๋ฐ”์™€ ๊ฐ™์ด, ํ…์ŠคํŠธ๋ฅผ ํ† ํฐ ID๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ๋ชจ๋ธ์ด ํ•ด๋‹น ์ž…๋ ฅ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 6์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด, ํ† ํฐ ๋ถ„๋ฅ˜ ์ž‘์—…์˜ ๊ฒฝ์šฐ ํฐ ์ฐจ์ด์ ์€ ์‚ฌ์ „์— ํ† ํฐํ™”๋œ ์ž…๋ ฅ์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ๋„ ํ† ํฌ ๋‚˜์ด์ € API๋Š” ์ด๋ฅผ ๋งค์šฐ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน๋ณ„ํ•œ ํ”Œ๋ž˜๊ทธ๋กœ tokenizer์—๊ฒŒ ์ž…๋ ฅ์ด ๋ฏธ๋ฆฌ ํ† ํฐํ™”๋˜์–ด ์žˆ๋‹ค๊ณ  ํ†ต์ง€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋จผ์ € tokenizer ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ๋งํ–ˆ๋“ฏ์ด ์šฐ๋ฆฌ๋Š” ์‚ฌ์ „ ํ•™์Šต๋œ(pretrained) BERT ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ๊ด€๋ จ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์บ์‹ฑ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoTokenizer model_checkpoint = "bert-base-cased" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) ํ—ˆ๋ธŒ(Hub)์—์„œ ์„ ํ˜ธํ•˜๋Š” ๋‹ค๋ฅธ ๋ชจ๋ธ์„ ์ฐพ์•„์„œ model_checkpoint๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ด๋ฏธ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•œ ๋กœ์ปฌ ํด๋”๋กœ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ ์ผํ•œ ์ œ์•ฝ์‚ฌํ•ญ์€ "๋น ๋ฅธ(fast)" ๋ฒ„์ „์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ Tokenizers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋„์›€์„ ๋ฐ›์•„์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ํฐ ํ…Œ์ด๋ธ”์—์„œ "๋น ๋ฅธ(fast)" ํ† ํฌ ๋‚˜์ด์ € ๋ฒ„์ „๊ณผ ํ•จ๊ป˜ ์ œ๊ณต๋˜๋Š” ๋ชจ๋“  ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‚ฌ์šฉ ์ค‘์ธ tokenizer ๊ฐ์ฒด๊ฐ€ ์‹ค์ œ๋กœ Tokenizers์˜ ์ง€์›์„ ๋ฐ›๊ณ  ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ is_fast ์†์„ฑ์„ ํ™•์ธํ•ด ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค: tokenizer.is_fast ์‚ฌ์ „ ํ† ํฐํ™”๋œ(pre-tokenized) ์ž…๋ ฅ์„ ํ† ํฐํ™”ํ•˜๋ ค๋ฉด is_split_into_words=True๋ฅผ ์ง€์ •ํ•˜์—ฌ tokenizer๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: inputs = tokenizer(raw_datasets["train"][0]["tokens"], is_split_into_words=True) inputs.tokens() ๋ณด๋‹ค์‹œํ”ผ ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ ํ† ํฐ(์ฒ˜์Œ์— [CLS], ๋์— [SEP])์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋Œ€๋ถ€๋ถ„์˜ ๋‹จ์–ด๋ฅผ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ lamb๋ผ๋Š” ๋‹จ์–ด๋Š” la์™€ ##mb์˜ ๋‘ ํ•˜์œ„ ๋‹จ์–ด๋กœ ํ† ํฐํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ž…๋ ฅ๊ณผ ๋ ˆ์ด๋ธ” ์‚ฌ์ด์— ๋ถˆ์ผ์น˜๋ฅผ ์ดˆ๋ž˜ํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ” ๋ชฉ๋ก์—๋Š” 9๊ฐœ์˜ ์š”์†Œ๋งŒ ์žˆ๋Š” ๋ฐ˜๋ฉด ์ž…๋ ฅ์—๋Š” 12๊ฐœ์˜ ํ† ํฐ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ˆ˜ ํ† ํฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋Š” ์‰ฝ์ง€๋งŒ(์‹œ์ž‘๊ณผ ๋์— ์žˆ์Œ์„ ์•Œ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์ง€์š”.), ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ ํ•ฉํ•œ ๋ ˆ์ด๋ธ”๋กœ ์ •๋ ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ๋„ "๋น ๋ฅธ(fast)" ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Tokenizers์˜ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๊ฐ ํ† ํฐ์„ ํ•ด๋‹น ๋‹จ์–ด์— ์‰ฝ๊ฒŒ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(6์žฅ ์ฐธ์กฐ): inputs.word_ids() ์•ฝ๊ฐ„์˜ ๋ถ€๊ฐ€ ์ž‘์—…์œผ๋กœ ํ† ํฐ๊ณผ ์ผ์น˜๋˜๋„๋ก ๋ ˆ์ด๋ธ” ๋ชฉ๋ก์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ ์šฉํ•  ์ฒซ ๋ฒˆ์งธ ๊ทœ์น™์€ ํŠน์ˆ˜ ํ† ํฐ์˜ ๋ ˆ์ด๋ธ”์˜ ID๊ฐ€ -100์ด๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ -100์€ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ์†์‹ค ํ•จ์ˆ˜(cross entropy)์—์„œ ๋ฌด์‹œ๋˜๋Š” ์ธ๋ฑ์Šค์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ, ์œ„์˜ ํ† ํฐ ์‹๋ณ„์ž ๋ฆฌ์ŠคํŠธ์—์„œ ๋‘ ๋ฒˆ์งธ 7์— ํ•ด๋‹นํ•˜๋Š” ํ† ํฐ์˜ ๋ ˆ์ด๋ธ”์€ ์ฒซ ๋ฒˆ์งธ 7์— ํ•ด๋‹นํ•˜๋Š” ํ† ํฐ์˜ ๋ ˆ์ด๋ธ”๊ณผ ๋™์ผํ•œ ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ฒซ ๋ฒˆ์งธ 7์— ํ•ด๋‹นํ•˜๋Š” ํ† ํฐ์€ B-๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ๋Š” I-๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ ˆ์ด๋ธ”๋กœ ์ง€์ •๋ฉ๋‹ˆ๋‹ค: def align_labels_with_tokens(labels, word_ids): new_labels = [] current_word = None for word_id in word_ids: if word_id != current_word: # ์ƒˆ๋กœ์šด ๋‹จ์–ด์˜ ์‹œ์ž‘ ํ† ํฐ. current_word = word_id label = -100 if word_id is None else labels[word_id] new_labels.append(label) elif word_id is None: # ํŠน์ˆ˜ ํ† ํฐ. new_labels.append(-100) else: # ์ด์ „ ํ† ํฐ๊ณผ ๋™์ผํ•œ ๋‹จ์–ด์— ์†Œ์†๋œ ํ† ํฐ. label = labels[word_id] # ๋งŒ์•ฝ ๋ ˆ์ด๋ธ”์ด B-XXX ์ด๋ฉด ์ด๋ฅผ I-XXX๋กœ ๋ณ€๊ฒฝ. if label % 2 == 1: label += 1 new_labels.append(label) return new_labels ์œ„์—์„œ ์‚ฌ์šฉํ•œ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์œผ๋กœ ์ด ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธํ•ด ๋ด…์‹œ๋‹ค. labels = raw_datasets["train"][0]["ner_tags"] word_ids = inputs.word_ids() print(labels) print(align_labels_with_tokens(labels, word_ids)) ๋ณด์‹œ๋‹ค์‹œํ”ผ, ํ•จ์ˆ˜๋Š” ์‹œ์ž‘๊ณผ ๋์— ๋‘ ๊ฐœ์˜ ํŠน์ˆ˜ ํ† ํฐ์— ๋Œ€ํ•ด -100์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋‘ ๊ฐœ์˜ ํ† ํฐ์œผ๋กœ ๋ถ„ํ• ๋œ ํ† ํฐ์— ๋Œ€ํ•ด 0์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. โœ Your turn! ์ผ๋ถ€ ์—ฐ๊ตฌ์ž๋Š” ๋‹จ์–ด๋‹น ํ•˜๋‚˜์˜ ๋ ˆ์ด๋ธ”๋งŒ ์ง€์ •ํ•˜๊ณ  ํ•ด๋‹น ๋‹จ์–ด์˜ ๋‹ค๋ฅธ ํ•˜์œ„ ํ† ํฐ์— -100์„ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์†์‹ค(loss)์— ํฌ๊ฒŒ ๊ธฐ์—ฌํ•˜๋Š” ๋งŽ์€ ํ•˜์œ„ ํ† ํฐ์œผ๋กœ ๋ถ„ํ• ๋˜๋Š” ๊ธด ๋‹จ์–ด๋ฅผ ํšŒํ”ผํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ์ด ๊ทœ์น™์— ๋”ฐ๋ผ ๋ ˆ์ด๋ธ”์„ ์ž…๋ ฅ ID์™€ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ด์ „ ํ•จ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•ด ๋ด…์‹œ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด, ๋ชจ๋“  ์ž…๋ ฅ์„ ํ† ํฐํ™”ํ•˜๊ณ  ๋ชจ๋“  ๋ ˆ์ด๋ธ”์— align_labels_with_tokens()๋ฅผ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. "๋น ๋ฅธ(fast)" ํ† ํฌ ๋‚˜์ด์ €์˜ ์žฅ์ ์„ ํ™œ์šฉํ•˜๋ ค๋ฉด ๋‹ค๋Ÿ‰์˜ ํ…์ŠคํŠธ๋ฅผ ๋™์‹œ์— ํ† ํฐํ™”ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ˆ์ œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ณ  batched=True ์˜ต์…˜์„ ์ง€์ •ํ•˜์—ฌ Dataset.map() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์˜ˆ์ œ์™€ ๋‹ค๋ฅธ ์ ์€ ํ•œ ๋ฒˆ์— ๋‹ค์ค‘ ํ…์ŠคํŠธ๊ฐ€ ํฌํ•จ๋œ ๋ฐฐ์น˜(batch)๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ, word_ids() ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ˜„์žฌ ๋ฐฐ์น˜(batch)์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ฒจ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค: # examples๋Š” ๋‹จ์ผ ํ…์ŠคํŠธ(๋ฌธ์žฅ)๊ฐ€ ์•„๋‹ˆ๋ผ ๋‹ค์ค‘ ํ…์ŠคํŠธ์ž„. def tokenize_and_align_labels(examples): tokenized_inputs = tokenizer( examples["tokens"], truncation=True, is_split_into_words=True ) all_labels = examples["ner_tags"] new_labels = [] for i, labels in enumerate(all_labels): word_ids = tokenized_inputs.word_ids(i) # ๋ฐฐ์น˜(batch) ์ธ๋ฑ์Šค ์ง€์ • new_labels.append(align_labels_with_tokens(labels, word_ids)) tokenized_inputs["labels"] = new_labels return tokenized_inputs ์•„์ง ์ž…๋ ฅ์— ๋Œ€ํ•œ ํŒจ๋”ฉ(padding)์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ(data collator)๋กœ ๋ฐฐ์น˜(batch)๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ชจ๋“  ๋ถ„ํ• ์— ๋Œ€ํ•ด์„œ ํ•œ ๋ฒˆ์— ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenized_datasets = raw_datasets.map( tokenize_and_align_labels, batched=True, remove_columns=raw_datasets["train"].column_names, ) ๊ฐ€์žฅ ์–ด๋ ค์šด ๋ถ€๋ถ„์„ ์ด์ œ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค! ๋ฐ์ดํ„ฐ๊ฐ€ ์ „์ฒ˜๋ฆฌ๋˜์—ˆ์œผ๋ฏ€๋กœ ์‹ค์ œ ํ•™์Šต์€ 3์žฅ์—์„œ ๋‹ค๋ค˜๋˜ ๋‚ด์šฉ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. Trainer API๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ชจ๋ธ ๋ฏธ์„ธ์กฐ์ • Trainer๋ฅผ ํ™œ์šฉํ•˜๋Š” ์‹ค์ œ ์ฝ”๋“œ๋Š” ์ด์ „๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์œ ์ผํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐฐ์น˜(batch)๋กœ ์กฐํ•ฉ๋˜๋Š” ๋ฐฉ์‹๊ณผ ๋ฉ”ํŠธ๋ฆญ(metrics) ๊ณ„์‚ฐ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ฝœ ๋ ˆ์ด์…˜ (Data collation) ์•ž์„œ์„œ ์‚ดํŽด๋ดค๋˜, DataCollatorWithPadding์€ ์ž…๋ ฅ(input IDs, attention mask ๋ฐ token type IDs)์— ๋Œ€ํ•ด์„œ๋งŒ ํŒจ๋”ฉ(padding)์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ ˆ์ด๋ธ”๋„ ์ž…๋ ฅ๊ณผ ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํŒจ๋”ฉ(padding)์ด ๋˜์–ด์•ผ ๋™์ผํ•œ ํฌ๊ธฐ๋ฅผ ์œ ์ง€ํ•˜๊ณ  -100์„ ํŒจ๋”ฉ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์˜ˆ์ธก์ด ์†์‹ค ๊ณ„์‚ฐ์—์„œ ๋ฌด์‹œ๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋‘ DataCollatorForTokenClassification์— ์˜ํ•ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. DataCollatorWithPadding๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ž…๋ ฅ์„ ์ „์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” tokenizer๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import DataCollatorForTokenClassification data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer) ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์ œ์—์„œ ์ด๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ํ† ํฐํ™”๋œ ํ•™์Šต ์ง‘ํ•ฉ์˜ ์˜ˆ์ œ ๋ชฉ๋ก์„ ์ž…๋ ฅ์œผ๋กœ ์ถ”๊ฐ€ํ•ด ๋ด…๋‹ˆ๋‹ค: batch = data_collator([tokenized_datasets["train"][i] for i in range(2)]) batch["labels"] ๋ณด๋‹ค์‹œํ”ผ ๋‘ ๋ฒˆ์งธ ๋ ˆ์ด๋ธ” ์ง‘ํ•ฉ์€ -100์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ๋ ˆ์ด๋ธ”์˜ ๊ธธ์ด๋กœ ์ฑ„์›Œ์กŒ์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๊ธฐ์ค€ (Metrics) Trainer๊ฐ€ ๋งค ์—ํฌํฌ(epoch)๋งˆ๋‹ค ๋ฉ”ํŠธ๋ฆญ(metrics)์„ ๊ณ„์‚ฐํ•˜๋„๋ก ํ•˜๋ ค๋ฉด, ์˜ˆ์ธก(predictions) ๋ฐ ๋ ˆ์ด๋ธ”(labels) ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋ฉ”ํŠธ๋ฆญ ์ด๋ฆ„๊ณผ ํ•ด๋‹น ํ‰๊ฐ€ ๊ฒฐ๊ด๊ฐ’์ด ํฌํ•จ๋œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” compute_metrics() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐ ๋ถ„๋ฅ˜ ์˜ˆ์ธก์„ ํ‰๊ฐ€ํ•˜๋Š”๋ฐ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์ „ํ†ต์ ์ธ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” seqeval์ž…๋‹ˆ๋‹ค. ์ด ๋ฉ”ํŠธ๋ฆญ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋จผ์ € seqeval ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜ ์‹œ์— conda ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ถ„๋“ค์€ ์ฃผ์˜ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ฐ€์ƒํ™˜๊ฒฝ์— seqeval์„ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•˜๋ฉด, "conda install ..."์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํ™•์‹คํ•œ๋ฐ, ์•„๋‚˜์ฝ˜๋‹ค์—์„œ๋Š” seqeval์„ ์„ค์น˜ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•˜์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. # ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ... # !pip install seqeval # ์•„๋‚˜์ฝ˜๋‹ค ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ... # https://stackoverflow.com/questions/59997065/pip-python-normal-site-packages-is-not-writeable/65290638 !python3 -m pip install seqeval ๊ทธ๋Ÿฐ ๋‹ค์Œ 3์žฅ์—์„œ์ฒ˜๋Ÿผ load_metric() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_metric metric = load_metric("seqeval") ์ด ๋ฉ”ํŠธ๋ฆญ(metric)์€ ์šฐ๋ฆฌ๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์•Œ๊ณ  ์žˆ๋Š” ํ‘œ์ค€์ ์ธ ์ •ํ™•๋„(accuracy)๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋Š” ๋ ˆ์ด๋ธ” ๋ชฉ๋ก์„ ์ •์ˆ˜๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž์—ด๋กœ ๊ฐ€์ ธ์˜ค๋ฏ€๋กœ ์˜ˆ์ธก ๊ฒฐ๊ณผ์™€ ์ •๋‹ต ๋ ˆ์ด๋ธ”์„ ๋ฉ”ํŠธ๋ฆญ์— ์ „๋‹ฌํ•˜๊ธฐ ์ „์— ๋””์ฝ”๋”ฉ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ด…์‹œ๋‹ค. ๋จผ์ € ์ฒซ ๋ฒˆ์งธ ํ•™์Šต ์˜ˆ์ œ(training example)์˜ ๋ ˆ์ด๋ธ”์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค: labels = raw_datasets["train"][0]["ner_tags"] labels = [label_names[i] for i in labels] labels ๊ทธ๋Ÿฐ ๋‹ค์Œ ์œ„ ๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์Šค 2์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’("B-MISC")์„ ๋ณ€๊ฒฝํ•˜์—ฌ ๊ฐ€์งœ ์˜ˆ์ธก์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: predictions = labels.copy() predictions[2] = "O" metric.compute(predictions=[predictions], references=[labels]) ์ƒ๋‹นํžˆ ๋งŽ์€ ์ •๋ณด๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ „์ฒด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐœ๋ณ„ ๊ฐœ์ฒด ํƒ€์ž…์— ๋Œ€ํ•œ ์ •ํ™•๋„, ์žฌํ˜„์œจ ๋ฐ F1 ์ ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์œ„์˜ ๋ฉ”ํŠธ๋ฆญ ๊ณ„์‚ฐ ํ•จ์ˆ˜(metric.compute)๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ „์ฒด ์ ์ˆ˜๋“ค๋งŒ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. compute_metrics() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๊ฐ์ž ์›ํ•˜๋Š” ์ ์ˆ˜๋“ค์„ ์–ป์„ ์ˆ˜ ์žˆ๋„๋ก ์–ธ์ œ๋“ ์ง€ ์ˆ˜์ • ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์— ๊ตฌํ˜„๋œ compute_metrics() ํ•จ์ˆ˜๋Š” ๋จผ์ € ๋กœ์ง“(logits)์˜ argmax๋ฅผ ๊ฐ€์ ธ์™€ ์˜ˆ์ธก(predictions)์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋Š˜ ๊ทธ๋ ‡๋“ฏ์ด, ๋กœ์ง“๊ณผ ํ™•๋ฅ ์ด ๊ฐ™์€ ์ˆœ์„œ์ด๋ฏ€๋กœ softmax๋ฅผ ์ ์šฉํ•  ํ•„์š”๊นŒ์ง€๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ ˆ์ด๋ธ”๊ณผ ์˜ˆ์ธก์„ ์ •์ˆ˜์—์„œ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์ด -100์ธ ๋ชจ๋“  ๊ฐ’์„ ์ œ๊ฑฐํ•œ ๋‹ค์Œ ๊ฒฐ๊ณผ๋ฅผ metric.compute() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค: import numpy as np def compute_metrics(eval_preds): logits, labels = eval_preds predictions = np.argmax(logits, axis=-1) # ๋ฌด์‹œ๋œ ์ธ๋ฑ์Šค(ํŠน์ˆ˜ ํ† ํฐ๋“ค)๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ ˆ์ด๋ธ”๋กœ ๋ณ€ํ™˜ true_labels = [[label_names[l] for l in label if l != -100] for label in labels] true_predictions = [ [label_names[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] all_metrics = metric.compute(predictions=true_predictions, references=true_labels) return { "precision": all_metrics["overall_precision"], "recall": all_metrics["overall_recall"], "f1": all_metrics["overall_f1"], "accuracy": all_metrics["overall_accuracy"], } ์ด์ œ ์œ„ ํ•จ์ˆ˜๊ฐ€ ์™„์„ฑ๋˜์—ˆ์œผ๋ฏ€๋กœ Trainer๋ฅผ ์ •์˜ํ•  ์ค€๋น„๊ฐ€ ๊ฑฐ์˜ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ์„ธ ์กฐ์ •ํ•  model๋งŒ ์žˆ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค! ๋ชจ๋ธ ์ •์˜ํ•˜๊ธฐ ํ† ํฐ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์— ๋Œ€ํ•ด ์ž‘์—… ์ค‘์ด๋ฏ€๋กœ AutoModelForTokenClassification ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์„ ์ •์˜ํ•  ๋•Œ ๊ธฐ์–ตํ•ด์•ผ ํ•  ์ฃผ์š” ์‚ฌํ•ญ์€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ ˆ์ด๋ธ” ์ˆ˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ num_labels ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด์ง€๋งŒ, ์ด ์„น์…˜์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ์ฒ˜๋Ÿผ ์ž‘๋™ํ•˜๋Š” ๋ฉ‹์ง„ ์ถ”๋ก  ์œ„์ ฏ(inference widget)์„ ์›ํ•œ๋‹ค๋ฉด, ์˜ฌ๋ฐ”๋ฅธ ๋ ˆ์ด๋ธ” ๋Œ€์‘ ์ •๋ณด(label correspondences)๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ID์—์„œ ๋ ˆ์ด๋ธ”๋กœ ๋˜๋Š” ๊ทธ ๋ฐ˜๋Œ€๋กœ์˜ ๋งคํ•‘์„ ํฌํ•จํ•˜๋Š” ๋‘ ๊ฐœ์˜ ๋”•์…”๋„ˆ๋ฆฌ, ์ฆ‰ id2label ๋ฐ label2id๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ด ๋Œ€์‘ ์ •๋ณด๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: id2label = {str(i): label for i, label in enumerate(label_names)} label2id = {v: k for k, v in id2label.items()} ์ด์ œ AutoModelForTokenClassification.from_pretrained() ๋ฉ”์„œ๋“œ์— ์ด๋“ค์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ชจ๋ธ ๊ตฌ์„ฑ(model's configuration)์—์„œ ์„ค์ •ํ•œ ๋‹ค์Œ ์ ์ ˆํ•˜๊ฒŒ ์ €์žฅํ•˜๊ณ  ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoModelForTokenClassification model = AutoModelForTokenClassification.from_pretrained( model_checkpoint, id2label=id2label, label2id=label2id, ) 3์žฅ์—์„œ AutoModelForSequenceClassification์„ ์ •์˜ํ–ˆ์„ ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๋ฉด ์ผ๋ถ€ ๊ฐ€์ค‘์น˜(์‚ฌ์ „ ํ•™์Šต ํ—ค๋“œ์˜ ๊ฐ€์ค‘์น˜)๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜(์ƒˆ ํ† ํฐ ๋ถ„๋ฅ˜ ํ—ค๋“œ์˜ ๊ฐ€์ค‘์น˜)๊ฐ€ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋œ๋‹ค๋Š” ๊ฒฝ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ชจ๋ธ์€ ํ•™์Šต์ด ์ˆ˜ํ–‰๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ํ•™์Šต์„ ์ˆ˜ํ–‰ํ•˜๊ฒ ์ง€๋งŒ ๋จผ์ € ๋ชจ๋ธ์— ์˜ฌ๋ฐ”๋ฅธ ์ˆ˜์˜ ๋ ˆ์ด๋ธ”์ด ์ง€์ •๋˜์–ด ์žˆ๋Š”์ง€ ๋‹ค์‹œ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: model.config.num_labels โš  ๋ ˆ์ด๋ธ” ๊ฐœ์ˆ˜๊ฐ€ ์ž˜๋ชป ์ง€์ •๋œ ๋ชจ๋ธ์ธ ๊ฒฝ์šฐ ๋‚˜์ค‘์— Trainer.train() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋ชจํ˜ธํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค(์˜ˆ: "CUDA error: device-side assert triggered"). ์ด ๋ ˆ์ด๋ธ” ๊ฐœ์ˆ˜ ์ง€์ • ์˜ค๋ฅ˜๊ฐ€ ๊ทธ๋Ÿฌํ•œ ๋ชจํ˜ธํ•œ ์˜ค๋ฅ˜์— ๋Œ€ํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๊ณ ํ•œ ๋ฒ„๊ทธ์˜ ๊ฐ€์žฅ ํฐ ์›์ธ์ด๋ฏ€๋กœ ์ด ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ์˜ˆ์ƒํ•œ ์ˆ˜์˜ ๋ ˆ์ด๋ธ”์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. ๋ชจ๋ธ ๋ฏธ์„ธ์กฐ์ •(fine-tuning) ํ•˜๊ธฐ ์ด์ œ ๋ชจ๋ธ์„ ํ•™์Šตํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! Trainer๋ฅผ ์ •์˜ํ•˜๊ธฐ ์ „์— ๋งˆ์ง€๋ง‰ ๋‘ ๊ฐ€์ง€ ์ž‘์—…, ์ฆ‰ Hugging Face์— ๋กœ๊ทธ์ธํ•˜๊ณ  ํ•™์Šต ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ๋งŒ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Jupyter Notebook์—์„œ ์ž‘์—…ํ•˜๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ๋„์™€์ฃผ๋Š” ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค: from huggingface_hub import notebook_login notebook_login() ๊ทธ๋Ÿฌ๋ฉด Hugging Face ๋กœ๊ทธ์ธ ์ž๊ฒฉ ์ฆ๋ช…์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์ ฏ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋…ธํŠธ๋ถ์—์„œ ์ž‘์—…ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ํ„ฐ๋ฏธ๋„์— ๋‹ค์Œ ์ค„์„ ์ž…๋ ฅํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. huggingface-cli login ์ด ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด TrainingArguments๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import TrainingArguments args = TrainingArguments( "bert-finetuned-ner", evaluation_strategy="epoch", save_strategy="epoch", learning_rate=2e-5, num_train_epochs=3, weight_decay=0.01, push_to_hub=True, ) ๋Œ€๋ถ€๋ถ„ ์ด์ „์— ๋ณด์•˜๋˜ ๊ฒƒ๋“ค์ž…๋‹ˆ๋‹ค. ์ผ๋ถ€ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ(์˜ˆ: ํ•™์Šต๋ฅ , ํ•™์Šตํ•  ์—ํฌํฌ ์ˆ˜, ๊ฐ€์ค‘์น˜ ๊ฐ์†Œ)๋“ค์„ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  push_to_hub=True๋ฅผ ์ง€์ •ํ•˜์—ฌ ๋ชจ๋ธ์„ ์ €์žฅํ•˜๊ณ  ๋ชจ๋“  ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ Model Hub์— ์—…๋กœ๋“œํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. hub_model_id ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘ธ์‹œ ํ•˜๋ ค๋Š” ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ, ํŠน์ • organization์— ํ‘ธ์‹œ ํ•˜๋ ค๋ฉด ์ด ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจ๋ธ์„ huggingface-course organization์— ํ‘ธ์‹œ ํ•  ๋•Œ Hub_model_id="huggingface-course/bert-finetuned-ner"๋ฅผ TrainingArguments์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋””ํดํŠธ๋กœ, ์‚ฌ์šฉ๋˜๋Š” ์ €์žฅ์†Œ๋Š” ์ž์‹ ์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์žˆ๊ณ  ์„ค์ •ํ•œ ์ถœ๋ ฅ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ๋ช…๋ช…๋˜๋ฏ€๋กœ ์ด ๊ฒฝ์šฐ์—๋Š” "sgugger/bert-finetuned-ner"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์ค‘์ธ ์ถœ๋ ฅ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ํ‘ธ์‹œ ํ•˜๋ ค๋Š” ์ €์žฅ์†Œ์˜ ๋กœ์ปฌ ๋ณต์ œ๋ณธ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ Trainer๋ฅผ ์ •์˜ํ•  ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ ์ด๋ฆ„์„ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋“  ๊ฒƒ์„ Trainer์— ์ „๋‹ฌํ•˜๊ณ  ํ•™์Šต์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: from transformers import Trainer trainer = Trainer( model=model, args=args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, compute_metrics=compute_metrics, tokenizer=tokenizer, ) trainer.train() ํ•™์Šต์ด ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ ๋ชจ๋ธ์ด ์ €์žฅ๋  ๋•Œ๋งˆ๋‹ค(์—ฌ๊ธฐ์„œ๋Š” ๋ชจ๋“  ์—ํฌํฌ๋งˆ๋‹ค) ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ์„œ๋ฒ„์—์„œ ํ•™์Šต์„ ์žฌ๊ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์ด ์™„๋ฃŒ๋˜๋ฉด push_to_hub() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ ์ตœ์‹  ๋ฒ„์ „์„ ์—…๋กœ๋“œํ–ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค: trainer.push_to_hub(commit_message="Training complete") ๋˜ํ•œ Trainer๋Š” ๋ชจ๋“  ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ํฌํ•จ๋œ ๋ชจ๋ธ ์นด๋“œ(model card)์˜ ์ดˆ์•ˆ์„ ์ž‘์„ฑํ•˜์—ฌ ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„์—์„œ Model Hub์˜ ์ถ”๋ก  ์œ„์ ฏ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•˜๊ณ  ์นœ๊ตฌ์™€ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฐ ๋ถ„๋ฅ˜ ์ž‘์—…์—์„œ ๋ชจ๋ธ์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! ํ•™์Šต ๋ฃจํ”„์— ๋Œ€ํ•ด ์ข€ ๋” ์ž์„ธํžˆ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์ด์ œ Accelerate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ •์˜ ํ•™์Šต ๋ฃจํ”„ ์ด์ œ ์ „์ฒด ํ•™์Šต ๋ฃจํ”„(training loop)๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์ˆ˜์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€์— ๋Œ€ํ•œ ๋ช‡ ๊ฐ€์ง€ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ œ์™ธํ•˜๊ณ ๋Š” 3์žฅ์—์„œ ์ˆ˜ํ–‰ํ•œ ๊ฒƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต์„ ์œ„ํ•œ ์ „์ฒด ์ค€๋น„ ๋จผ์ € ๋ฐ์ดํ„ฐ ์…‹์—์„œ DataLoader๋ฅผ ๋นŒ๋“œ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. data_collator๋ฅผ collate_fn์œผ๋กœ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ํ•™์Šต ์ง‘ํ•ฉ์„ ์…”ํ”Œ๋งํ•˜์ง€๋งŒ ๊ฒ€์ฆ ์ง‘ํ•ฉ์€ ์…”ํ”Œ๋งํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค: from torch.utils.data import DataLoader train_dataloader = DataLoader( tokenized_datasets["train"], shuffle=True, collate_fn=data_collator, batch_size=8, ) eval_dataloader = DataLoader( tokenized_datasets["validation"], collate_fn=data_collator, batch_size=8, ) ๋‹ค์Œ์œผ๋กœ ์šฐ๋ฆฌ๋Š” ์•ž์„œ ์ˆ˜ํ–‰ํ•œ ๋ฏธ์„ธ ์กฐ์ •์„ ๊ณ„์†ํ•ด์„œ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์ „ ํ•™์Šต๋œ BERT ๋ชจ๋ธ์—์„œ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋„๋ก ๋ชจ๋ธ์„ ๋‹ค์‹œ ์ธ์Šคํ„ด์Šคํ™”ํ•ฉ๋‹ˆ๋‹ค: model = AutoModelForTokenClassification.from_pretrained( model_checkpoint, id2label=id2label, label2id=label2id, ) ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ตœ์ ํ™” ํ•จ์ˆ˜(optimizer)๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Adam๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ๊ฐ€์ค‘์น˜ ๊ฐ์†Œ(weight decay)๊ฐ€ ์ ์šฉ๋˜๋Š” ๋ฐฉ์‹์„ ์ˆ˜์ •ํ•œ ๊ณ ์ „์ ์ธ AdamW๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: from torch.optim import AdamW optimizer = AdamW(model.parameters(), lr=2e-5) ์œ„ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„์— ์ด๋ฅผ accelerator.prepare() ๋ฉ”์„œ๋“œ๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from accelerate import Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader ) TPU์—์„œ ํ•™์Šตํ•˜๋Š” ๊ฒฝ์šฐ ์œ„์˜ ์…€์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ์ „์šฉ ํ•™์Šต ํ•จ์ˆ˜๋กœ ์ด๋™ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ 3์žฅ์„ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. ์ด์ œ train_dataloader๋ฅผ accelerator.prepare()๋กœ ๋ณด๋ƒˆ์œผ๋ฏ€๋กœ ๊ทธ ํฌ๊ธฐ(ํฌํ•จ ๊ฐœ์ˆ˜)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ํšŸ์ˆ˜(training steps)๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. dataloader๋ฅผ ์ƒ์„ฑํ•œ ํ›„์—๋Š” ํ•ญ์ƒ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ณผ์ •์—์„œ ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ง€์ •๋œ ํ•™์Šต๋ฅ (learning rate)์ด 0๊นŒ์ง€ ์ค„์–ด๋“œ๋Š” ๊ณ ์ „์ ์ธ ์„ ํ˜• ์Šค์ผ€์ค„(linear schedule)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import get_scheduler num_train_epochs = 3 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋ธ์„ Hub๋กœ ํ‘ธ์‹œ ํ•˜๋ ค๋ฉด ์ž‘์—… ํด๋”์— Repository ๊ฐœ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„์ง ๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋จผ์ € Hugging Face์— ๋กœ๊ทธ์ธํ•˜์„ธ์š”. ๋ชจ๋ธ ID์—์„œ ๋ชจ๋ธ์— ๋ถ€์—ฌํ•˜๋ ค๋Š” ์ €์žฅ์†Œ ์ด๋ฆ„์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. repo_name์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ์ž์œ ๋กญ๊ฒŒ ๋ฐ”๊พธ์…”๋„ ๋ฉ๋‹ˆ๋‹ค. get_full_repo_name() ํ•จ์ˆ˜๊ฐ€ ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ์‚ฌ์šฉ์ž ์ด๋ฆ„๋งŒ ํฌํ•จ์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค: from huggingface_hub import Repository, get_full_repo_name model_name = "bert-finetuned-ner-accelerate" repo_name = get_full_repo_name(model_name) repo_name ๊ทธ๋Ÿฐ ๋‹ค์Œ ํ•ด๋‹น ์ €์žฅ์†Œ๋ฅผ ๋กœ์ปฌ ํด๋”์— ๋ณต์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์ด ๋กœ์ปฌ ํด๋”๋Š” ์ž‘์—… ์ค‘์ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๊ธฐ์กด ๋ณต์ œ๋ณธ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: output_dir = "bert-finetuned-ner-accelerate" repo = Repository(output_dir, clone_from=repo_name) ์ด์ œ repo.push_to_hub() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ output_dir์— ์ €์žฅํ•œ ๋ชจ๋“  ํ•ญ๋ชฉ๋“ค์„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ ์—ํฌํฌ๋งˆ๋‹ค ๊ทธ๋•Œ๊นŒ์ง€ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ๋ฃจํ”„ ์ด์ œ ์ „์ฒด ํ•™์Šต ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ถ€๋ถ„์„ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์œ„ํ•ด metric ๊ฐ์ฒด์˜ ์ž…๋ ฅ์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ ์˜ˆ์ธก๊ณผ ๋ ˆ์ด๋ธ”์„ ๊ฐ€์ ธ์™€ ๋ฌธ์ž์—ด ๋ชฉ๋ก์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋‹ค์Œ์˜ postprocess() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค: def postprocess(predictions, labels): predictions = predictions.detach().cpu().clone().numpy() labels = labels.detach().cpu().clone().numpy() # Remove ignored index (special tokens) and convert to labels true_labels = [[label_names[l] for l in label if l != -100] for label in labels] true_predictions = [ [label_names[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] return true_labels, true_predictions ์ด์ œ ํ•™์Šต ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ์ง„ํ–‰ ๋ฐฉ์‹์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์ง„ํ–‰๋ฅ  ํ‘œ์‹œ์ค„(progress bar)์„ ์ •์˜ํ•œ ํ›„, ๋ฃจํ”„๋Š” ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค: ํ•™์Šต(training): train_dataloader์—์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋ฐฐ์น˜(batch) ๊ฐ€์ ธ์˜ค๊ธฐ, ๋ชจ๋ธ์„ ํ†ตํ•œ ์ˆœ์ „ํŒŒ(forward pass), ์—ญ์ „ํŒŒ(backward pass) ๋ฐ ์ตœ์ ํ™” ๋‹จ๊ณ„. ํ‰๊ฐ€(evaluation): ํ•˜๋‚˜์˜ ๋ฐฐ์น˜(batch)์—์„œ ๋ชจ๋ธ์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ์–ป์€ ํ›„์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŠน์ˆ˜ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ž…๋ ฅ๊ณผ ๋ ˆ์ด๋ธ”์„ ๋‹ค๋ฅธ ๋ชจ์–‘์œผ๋กœ ํŒจ๋”ฉ(padding) ํ–ˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, gather() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „์— ์˜ˆ์ธก๊ณผ ๋ ˆ์ด๋ธ”์„ ๋™์ผํ•œ ๋ชจ์–‘์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด accelerator.pad_across_processes()๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ฉด ํ‰๊ฐ€์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ๋ฌดํ•œ์ • ๋ฐ˜๋ณต๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„, ๊ฒฐ๊ณผ๋ฅผ metric.add_batch()๋กœ ๋ณด๋‚ด๊ณ  ํ‰๊ฐ€ ๋ฃจํ”„๊ฐ€ ๋๋‚˜๋ฉด metric.compute()๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ €์žฅ(saving) ๋ฐ ์—…๋กœ๋“œ(uploading): ๋จผ์ € ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•œ ๋‹ค์Œ repo.push_to_hub()๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. Hub ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋น„๋™๊ธฐ ํ”„๋กœ์„ธ์Šค(asynchronous process)๋ฅผ ํ‘ธ์‹œํ•˜๋„๋ก ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด blocking=False ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•™์Šต์€ ์ •์ƒ์ ์œผ๋กœ ๊ณ„์†๋˜๊ณ  ์ด๋Ÿฌํ•œ (๊ธด) ๋ช…๋ น์€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๋ฃจํ”„์˜ ์ „์ฒด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: from tqdm.auto import tqdm import torch progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): # ํ•™์Šต (Training) model.train() for batch in train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) # ํ‰๊ฐ€ (Evaluation) model.eval() for batch in eval_dataloader: with torch.no_grad(): outputs = model(**batch) predictions = outputs.logits.argmax(dim=-1) labels = batch["labels"] # ์ทจํ•ฉ ๋Œ€์ƒ์ธ ์˜ˆ์ธก(predictions)๊ณผ ๋ ˆ์ด๋ธ”(labels)์„ ํŒจ๋”ฉ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•จ.. predictions = accelerator.pad_across_processes(predictions, dim=1, pad_index=-100) labels = accelerator.pad_across_processes(labels, dim=1, pad_index=-100) predictions_gathered = accelerator.gather(predictions) labels_gathered = accelerator.gather(labels) true_predictions, true_labels = postprocess(predictions_gathered, labels_gathered) metric.add_batch(predictions=true_predictions, references=true_labels) results = metric.compute() print( f"epoch {epoch}:", { key: results[f"overall_{key}"] for key in ["precision", "recall", "f1", "accuracy"] }, ) # ์ €์žฅ ๋ฐ ์—…๋กœ๋“œ accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message = f"Training in progress epoch {epoch}", blocking=False, ) Accelerate๋กœ ์ €์žฅ๋œ ๋ชจ๋ธ์„ ์ฒ˜์Œ ์ ‘ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•˜์—ฌ ์ž ์‹œ ์‹œ๊ฐ„์„ ๋‚ด์–ด ์œ„ ์ฝ”๋“œ์— ํฌํ•จ๋œ ์„ธ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) ์ฒซ ๋ฒˆ์งธ ์ค„์€ ์„ค๋ช…์ด ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ด ์ง€์ ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ €์žฅํ•˜๊ธฐ ์ „์— ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค์—์„œ ๋™์ผํ•œ ๋ชจ๋ธ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์šฐ๋ฆฌ๊ฐ€ ์ •์˜ํ•œ ๊ธฐ๋ณธ ๋ชจ๋ธ์ธ unwrapped_model์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. accelerator.prepare() ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋ธ์„ ๋ถ„์‚ฐ ํ•™์Šต(distributed training)์—์„œ ์ž‘๋™ํ•˜๋„๋ก ๋ณ€๊ฒฝํ•˜๋ฏ€๋กœ ๋ชจ๋ธ์— ๋” ์ด์ƒ save_pretrained() ๋ฉ”์„œ๋“œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. accelerator.unwrap_model() ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋ธ์„ ๋‹ค์‹œ save_pretrained()๊ฐ€ ์กด์žฌํ•˜๋Š” ์ผ๋ฐ˜ ๋ชจ๋ธ ๊ฐ์ฒด๋กœ ๋Œ๋ ค๋†“์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ save_pretrained()๋ฅผ ํ˜ธ์ถœํ•˜์ง€๋งŒ ๊ทธ ๋ฉ”์„œ๋“œ์— torch.save() ๋Œ€์‹  accelerate.save()๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด Trainer๋กœ ํ•™์Šต๋œ ๊ฒƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋ธ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šตํ•œ ๋ชจ๋ธ์€ huggingface-course/bert-finetuned-ner-accelerate์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต ๋ฃจํ”„์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ํ…Œ์ŠคํŠธํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ์œ„์— ํ‘œ์‹œ๋œ ์ฝ”๋“œ๋ฅผ ํŽธ์ง‘ํ•˜์—ฌ ์ง์ ‘ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๋ฏธ์„ธ์กฐ์ •๋œ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ์ด ์„น์…˜์˜ ์ฒซ ๋ถ€๋ถ„์—์„œ ์ถ”๋ก  ์œ„์ ฏ์„ ์‚ฌ์šฉํ•˜์—ฌ Model Hub์—์„œ ๋ฏธ์„ธ ์กฐ์ •ํ•œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ด๋ฏธ ๋ณด์—ฌ ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. pipeline์—์„œ ๋กœ์ปฌ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ ์ ˆํ•œ ๋ชจ๋ธ ์‹๋ณ„์ž๋ฅผ ์ง€์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: from transformers import pipeline # ์•„๋ž˜ ๋‚ด์šฉ์„ ๋ณธ์ธ์˜ checkpoint๋กœ ๋ณ€๊ฒฝํ•˜์‹œ์˜ค. model_checkpoint = "spasis/bert-finetuned-ner" token_classifier = pipeline( "token-classification", model=model_checkpoint, aggregation_strategy="simple" ) token_classifier("My name is Sylvain and I work at Hugging Face in Brooklyn.") ์ข‹์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ ๋ชจ๋ธ์€ ์ด์ œ ํŒŒ์ดํ”„๋ผ์ธ์˜ ๊ธฐ๋ณธ ๋ชจ๋ธ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค! 2. ๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ(Masked Language Model) ๋ฏธ์„ธ์กฐ์ • Transformer ๋ชจ๋ธ๊ณผ ๊ด€๋ จ๋œ ๋งŽ์€ NLP ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฒฝ์šฐ, Hugging Face Hub์—์„œ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ๊ฐ€์ ธ์™€์„œ ์›ํ•˜๋Š” ์ž‘์—…์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜์—ฌ ์ง์ ‘ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต์— ์‚ฌ์šฉ๋˜๋Š” ๋ง๋ญ‰์น˜๊ฐ€ ๋ฏธ์„ธ ์กฐ์ •์— ์‚ฌ์šฉ๋˜๋Š” ๋ง๋ญ‰์น˜์™€ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ์ „์ด ํ•™์Šต(transfer learning)์ด ์ข‹์€ ๊ฒฐ๊ณผ๋ฅผ ๋‚ณ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•œํŽธ, ์ž‘์—…์— ํŠนํ™”๋œ ํ—ค๋“œ(task-specific head)๋ฅผ ํ•™์Šตํ•˜๊ธฐ ์ „์—, ๋จผ์ € ๋ณด์œ ํ•œ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ํ•ด๋‹น ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ณธ์ธ์˜ ๋ฐ์ดํ„ฐ ์…‹์— ๋ฒ•์ ์ธ ๊ณ„์•ฝ(legal contracts)์ด๋‚˜ ํ•™์ˆ  ๋…ผ๋ฌธ(scientific articles) ํฌํ•จ๋œ ๊ฒฝ์šฐ BERT์™€ ๊ฐ™์€ ๊ธฐ๋ณธ Transformer ๋ชจ๋ธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹์— ํฌํ•จ๋œ ์ „๋ฌธ ์šฉ์–ด๋“ค์„ ํฌ๊ท€ ํ† ํฐ์œผ๋กœ ์ทจ๊ธ‰ํ•˜๋ฏ€๋กœ ๊ฒฐ๊ณผ ์„ฑ๋Šฅ์ด ๋งŒ์กฑ์Šค๋Ÿฝ์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ์ธ์ด ๋ณด์œ ํ•œ ๋ถ„์•ผ ํŠนํ™” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋ฉด ๋‹ค์–‘ํ•œ ์ถ”๊ฐ€์  ํ•˜๋ถ€ ์ž‘์—…๋“ค์˜ ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ์ด ๋ฏธ์„ธ ์กฐ์ • ์ž‘์—…์€ 1๋ฒˆ๋งŒ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค! ๋ถ„์•ผ ํŠนํ™” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์‚ฌ์ „ ํ•™์Šต๋œ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ์ด ํ”„๋กœ์„ธ์Šค๋ฅผ ์ผ๋ฐ˜์ ์œผ๋กœ ๋„๋ฉ”์ธ ์–ด๋Žํ…Œ์ด์…˜(domain adaptation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ 2018๋…„์— ULMFiT์— ์˜ํ•ด ๋Œ€์ค‘ํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ULMFiT๋Š” ์ „์ด ํ•™์Šต(transfer learning)์„ ์‹ค์งˆ์ ์œผ๋กœ NLP์— ์ ์šฉํ•œ ์ตœ์ดˆ์˜ ์‹ ๊ฒฝ๋ง ์•„ํ‚คํ…์ฒ˜(LSTM ๊ธฐ๋ฐ˜) ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ULMFiT๋ฅผ ์‚ฌ์šฉํ•œ ๋„๋ฉ”์ธ ์–ด๋Žํ…Œ์ด์…˜์˜ ์˜ˆ๋Š” ์•„๋ž˜ ์ด๋ฏธ์ง€์— ๋‚˜์™€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ์ด ๊ทธ๋ฆผ์˜ ๋‚ด์šฉ๊ณผ ์œ ์‚ฌํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์ง€๋งŒ LSTM ๋Œ€์‹  Transformer๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค! ์ด ์„น์…˜์ด ๋๋‚˜๋ฉด ๋ฌธ์žฅ์„ ์ž๋™ ์™„์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ(masked language model)์„ Hub์—์„œ ์„œ๋น„์Šคํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์„ธ๋ถ€์ ์œผ๋กœ ํ•œ๋ฒˆ ์‚ดํŽด๋ด…์‹œ๋‹ค! "๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง(masked language modeling)"๊ณผ "์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ(pretrained model)"์ด๋ผ๋Š” ์šฉ์–ด๊ฐ€ ์ƒ์†Œํ•˜๊ฒŒ ๋“ค๋ฆฐ๋‹ค๋ฉด 1์žฅ์œผ๋กœ ์ด๋™ํ•˜์—ฌ ๋ชจ๋“  ํ•ต์‹ฌ ๊ฐœ๋…์„ ์ฝ๊ณ  ํฌํ•จ๋œ<NAME>์ƒ์„ ์‹œ์ฒญํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค! ๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง(MLM)์„ ์œ„ํ•ด ์‚ฌ์ „ํ•™์Šต๋œ ๋ชจ๋ธ(pretrained model) ์„ ํƒํ•˜๊ธฐ ์šฐ์„  ๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง(masked language modeling)์— ์ ํ•ฉํ•œ ์‚ฌ์ „ ํ•™์Šต ์–ธ์–ด ๋ชจ๋ธ์„ ์„ ํƒํ•ด ๋ด…์‹œ๋‹ค. ๋‹ค์Œ ์Šคํฌ๋ฆฐ์ˆ๊ณผ ๊ฐ™์ด Hugging Face Hub์—์„œ "Fill-Mask" ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•˜์—ฌ ํ›„๋ณด ๋ชฉ๋ก์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: BERT ๋ฐ RoBERTa ๋ฅ˜์˜ ๋ชจ๋ธ๋“ค์ด ๊ฐ€์žฅ ๋งŽ์ด ๋‹ค์šด๋กœ๋“œ๋˜์—ˆ์ง€๋งŒ ๋‹ค์šด์ŠคํŠธ๋ฆผ(downstream) ์„ฑ๋Šฅ ์†์‹ค์ด ๊ฑฐ์˜ ๋˜๋Š” ์ „ํ˜€ ์—†์ด ํ›จ์”ฌ ๋น ๋ฅด๊ฒŒ ํ•™์Šต๋  ์ˆ˜ ์žˆ๋Š” DistilBERT๋ผ๋Š” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์ง€์‹ ์ฆ๋ฅ˜(knowledge distillation)๋ผ๊ณ  ํ•˜๋Š” ํŠน๋ณ„ํ•œ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต๋˜์—ˆ์œผ๋ฉฐ, ์ด ๊ธฐ๋ฒ•์˜ ํŠน์ง•์€ BERT์™€ ๊ฐ™์€ ํฐ "๊ต์‚ฌ ๋ชจ๋ธ(teacher model)"์ด ํ›จ์”ฌ ์ ์€ ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ "ํ•™์ƒ ๋ชจ๋ธ(student model)"์˜ ํ•™์Šต์„ ์•ˆ๋‚ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ง€์‹ ์ฆ๋ฅ˜(knowledge distillation)์˜ ์„ธ๋ถ€ ์‚ฌํ•ญ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃจ์ง€๋Š” ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค๋งŒ, ๊ด€์‹ฌ์ด ์žˆ๋‹ค๋ฉด ์ด์— ๋Œ€ํ•œ ๋ชจ๋“  ๋‚ด์šฉ์„ Natural Language Processing with Transformers(Transformers๋ฅผ ์‚ฌ์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ)์—์„œ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. AutoModelForMaskedLM ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DitilBERT๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: from transformers import AutoModelForMaskedLM model_checkpoint = "distilbert-base-uncased" model = AutoModelForMaskedLM.from_pretrained(model_checkpoint) num_parameters() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ด ๋ชจ๋ธ์— ๋ช‡ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: distilbert_num_parameters = model.num_parameters() / 1_000_000 print(f"'>>> DistilBERT number of parameters: {round(distilbert_num_parameters)}M'") print(f"'>>> BERT number of parameters: 110M'") ์•ฝ 6,700๋งŒ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” DistilBERT๋Š” BERT ๊ธฐ๋ณธ ๋ชจ๋ธ๋ณด๋‹ค ์•ฝ 2๋ฐฐ ์ž‘์œผ๋ฉฐ ๋”ฐ๋ผ์„œ ํ•™์Šต ์‹œ๊ฐ„์ด 2๋ฐฐ ๋น ๋ฅผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๊ฒ ์ง€์š”. ์ด์ œ ์•„๋ž˜์˜ ์ž‘์€ ํ…์ŠคํŠธ ์ƒ˜ํ”Œ์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด์„œ ๋ชจ๋ธ์ด ์–ด๋–ค ํ† ํฐ์„ ์ œ์‹œํ•˜๋Š”์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค: text = "This is a great [MASK]." ์šฐ๋ฆฌ๋Š” "๋‚ (day)", "๋†€์ด ๊ธฐ๊ตฌ(ride)", "painting(๊ทธ๋ฆผ)"๊ณผ ๊ฐ™์€ [MASK] ํ† ํฐ์„ ์ฑ„์šธ ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋‹จ์–ด๋“ค์„ ์ƒ์ƒํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€์š”. ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ ์˜ˆ์ธก์€ ํ•ด๋‹น ๋ชจ๋ธ์ด ํ•™์Šต๋œ ์ฝ”ํผ์Šค์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ํ†ต๊ณ„์  ํŒจํ„ด์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šฐ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. BERT์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ DistilBERT๋Š” ์˜์–ด Wikipedia ๋ฐ BookCorpus ๋ฐ์ดํ„ฐ ์…‹์„ ๋ฐ”ํƒ•์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋˜์—ˆ์œผ๋ฏ€๋กœ [MASK]์— ๋Œ€ํ•œ ์˜ˆ์ธก์ด ์ด๋Ÿฌํ•œ ๋„๋ฉ”์ธ์„ ๋ฐ˜์˜ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์Šคํฌ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋ ค๋ฉด ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ž…๋ ฅ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด DistilBERT์˜ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ํ—ˆ๋ธŒ์—์„œ๋„ ๋‹ค์šด๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) ํ† ํฌ ๋‚˜์ด์ €์™€ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ œ ํ…์ŠคํŠธ ์˜ˆ์ œ๋ฅผ ๋ชจ๋ธ์— ์ „๋‹ฌํ•˜๊ณ  ๋กœ์ง“(logits)์„ ์ถ”์ถœํ•˜๊ณ  ์ƒ์œ„ 5๊ฐœ ํ›„๋ณด๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: import torch inputs = tokenizer(text, return_tensors="pt") token_logits = model(**inputs).logits # [MASK]์˜ ์œ„์น˜๋ฅผ ์ฐพ๊ณ , ํ•ด๋‹น logits์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1] mask_token_logits = token_logits[0, mask_token_index, :] # ๊ฐ€์žฅ ํฐ logits ๊ฐ’์„ ๊ฐ€์ง€๋Š” [MASK] ํ›„๋ณด๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. top_5_tokens = torch.topk(mask_token_logits, 5, dim=1).indices[0].tolist() for token in top_5_tokens: print(f"'>>> {text.replace(tokenizer.mask_token, tokenizer.decode([token]))}'") ์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ์˜ ์˜ˆ์ธก์ด ์ผ์ƒ์ ์ธ ์šฉ์–ด๋ฅผ ๋„์ถœํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ถœ๋ ฅ์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์˜์–ด Wikipedia์˜ ๋‚ด์šฉ์„ ๊ณ ๋ คํ•  ๋•Œ ๋†€๋ผ์šด ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์„ฑ์„ ์ข€ ๋” ์„ธ๋ถ€์ ์ด๊ณ  ์ „๋ฌธ์ ์œผ๋กœ ๋ณ€ํ™”์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…์‹œ๋‹ค. ์ด์ œ ๊ทน๋„๋กœ ์–‘๊ทนํ™”๋œ(polarized) ์˜ํ™” ๋ฆฌ๋ทฐ(movie reviews)๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค! ๋ฐ์ดํ„ฐ ์…‹ ๋„๋ฉ”์ธ ์–ด๋Žํ…Œ์ด์…˜(domain adaptation) ๊ณผ์ •์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด์„œ, ๊ฐ์ • ๋ถ„์„ ๋ชจ๋ธ์„ ๋ฒค์น˜๋งˆํ‚นํ•˜๋Š”๋ฐ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์˜ํ™” ๋ฆฌ๋ทฐ ๋ชจ์Œ์ธ ์œ ๋ช…ํ•œ Large Movie Review Dataset(๋˜๋Š” ์ค„์—ฌ์„œ IMDb)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ง๋ญ‰์น˜๋ฅผ ์ด์šฉํ•˜์—ฌ DitilBERT๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•จ์œผ๋กœ์จ ์–ธ์–ด ๋ชจ๋ธ์ด ์‚ฌ์ „ ํ•™์Šต๋œ Wikipedia์˜ ์‚ฌ์‹ค์ ์ธ ๋ฐ์ดํ„ฐ์—์„œ ์˜ํ™” ๋ฆฌ๋ทฐ์˜ ๋ณด๋‹ค ์ฃผ๊ด€์ ์ธ ์š”์†Œ์— ๋งž๊ฒŒ ์–ดํœ˜๋ฅผ ์กฐ์ •ํ•  ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๋‹ค. Datasets์˜ load_dataset() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Hugging Face Hub์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_dataset imdb_dataset = load_dataset("imdb") imdb_dataset train ๋ฐ test ๋ถ„ํ• (splits)์€ ๊ฐ๊ฐ 25,000๊ฐœ์˜ ๋ฆฌ๋ทฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ๋ฐ˜๋ฉด unsupervised๋ผ๊ณ  ๋ช…๋ช…๋œ ๋ ˆ์ด๋ธ”์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๋ถ„ํ• ์—๋Š” 50,000๊ฐœ์˜ ๋ฆฌ๋ทฐ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ์ข…๋ฅ˜์˜ ํ…์ŠคํŠธ๋ฅผ ๋‹ค๋ฃจ๊ณ  ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ๋ช‡ ๊ฐ€์ง€ ์ƒ˜ํ”Œ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ „ ์žฅ์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ Dataset.shuffle() ๋ฐ Dataset.select() ํ•จ์ˆ˜๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์ž„์˜์˜ ์ƒ˜ํ”Œ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค: sample = imdb_dataset["train"].shuffle(seed=42).select(range(3)) for row in sample: print(f"\n'>>> Review: {row['text']}'") print(f"'>>> Label: {row['label']}'") ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์•Œ๊ฒ ์ง€๋งŒ, ์‹ค์ œ๋กœ ์˜ํ™” ๋ฆฌ๋ทฐ๋“ค์ž…๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ๋ง์— ๋ ˆ์ด๋ธ”์ด ํ•„์š”ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ 0์€ ๋ถ€์ •์ ์ธ ๋ฆฌ๋ทฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  1์€ ๊ธ์ •์ ์ธ ๋ฆฌ๋ทฐ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. โœ Try it out! unsupervised ๋ถ„ํ• ์˜ ๋ฌด์ž‘์œ„ ์ƒ˜ํ”Œ์„ ๋งŒ๋“ค๊ณ  ๋ ˆ์ด๋ธ”์ด 0๋„ 1๋„ ์•„๋‹Œ์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋™์•ˆ train ๋ฐ test ๋ถ„ํ• ์˜ ๋ ˆ์ด๋ธ”์ด ์‹ค์ œ๋กœ 0 ๋˜๋Š” 1์ธ์ง€ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋“  NLP ์‹ค๋ฌด์ž๊ฐ€ ์ƒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์™„์ „์„ฑ ๊ฒ€์‚ฌ(sanity check)์ž…๋‹ˆ๋‹ค! ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด์•˜์œผ๋ฏ€๋กœ ์ด์ œ ๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋ณด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ, 3์žฅ์—์„œ ์‚ดํŽด๋ณธ ์‹œํ€€์Šค ๋ถ„๋ฅ˜ ์ž‘์—…๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ถ”๊ฐ€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ๋‹จ๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ ์ž๋™ ํšŒ๊ท€ ๋ชจ๋ธ๋ง(auto-regressive modeling) ๋ฐ ๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง(masked language modeling) ๋ชจ๋‘์—์„œ ๊ณตํ†ต์ ์ธ ์ „์ฒ˜๋ฆฌ ๋‹จ๊ณ„๋Š” ๋ชจ๋“  ์˜ˆ์ œ๋ฅผ ํ†ตํ•ฉํ•œ ๋‹ค์Œ ์ „์ฒด ๋ง๋ญ‰์น˜๋ฅผ ๋™์ผํ•œ ํฌ๊ธฐ์˜ ์ฒญํฌ๋กœ ๋ถ„ํ• ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐœ๋ณ„ ์˜ˆ์ œ๋ฅผ ๋‹จ์ˆœํžˆ ํ† ํฐํ™”ํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์ ‘๊ทผ ๋ฐฉ์‹๊ณผ ๋งค์šฐ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์™œ ๋ชจ๋“  ๊ฒƒ์„ ํ•˜๋‚˜๋กœ ํ†ตํ•ฉํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ผ๊นŒ์š”? ๊ทธ ์ด์œ ๋Š” ๊ฐœ๋ณ„ ์˜ˆ์ œ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ๋ฉด ์ ˆ๋‹จ๋˜์–ด์„œ, ์–ธ์–ด ๋ชจ๋ธ๋ง ์ž‘์—…์— ์œ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๊ฐ€ ์†์‹ค๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค! ๋”ฐ๋ผ์„œ ๋จผ์ € ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ง๋ญ‰์น˜๋ฅผ ํ† ํฐํ™”ํ•˜์ง€๋งŒ ํ† ํฌ ๋‚˜์ด์ €์—์„œ truncation=True ์˜ต์…˜์„ ์„ค์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๋™์ผํ•˜๊ฒŒ word_ids๋ฅผ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค(6์žฅ์—์„œ ์„ค๋ช…ํ•œ ๋Œ€๋กœ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋‹จ์–ด ID๋Š” ์ „์ฒด ๋‹จ์–ด ๋งˆ์Šคํ‚น์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์ค‘์— ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค). ์ด ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•ด์„œ ํ† ํฌ๋‚˜์ด์ง•์„ ์ˆ˜ํ–‰ํ•œ ๋’ค์—๋Š”(์—„๋ฐ€ํžˆ ๋”ฐ์ง€๋ฉด ํ† ํฌ๋‚˜์ด์ง• ์ˆ˜ํ–‰ ๊ณผ์ •์—์„œ) ๋” ์ด์ƒ ๋ถˆํ•„์š”ํ•œ text์™€ label ์—ด์„ ์ œ๊ฑฐํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: def tokenize_function(examples): result = tokenizer(examples["text"]) if tokenizer.is_fast: result["word_ids"] = [result.word_ids(i) for i in range(len(result["input_ids"]))] return result # ๋น ๋ฅธ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋”ฉ์„ ์ž‘๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ, batched=True๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. tokenized_datasets = imdb_dataset.map( tokenize_function, batched=True, remove_columns=["text", "label"] ) tokenized_datasets DistilBERT๋Š” BERT์™€ ์œ ์‚ฌํ•œ ๋ชจ๋ธ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ธ์ฝ”๋”ฉ๋œ ํ…์ŠคํŠธ๋Š” ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋œ word_ids๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ด์ „ ์žฅ์—์„œ ๋ณธ input_ids ๋ฐ attention_mask๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ ์…‹ ์ „์ฒด๋ฅผ ํ† ํฐํ™”ํ–ˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ ๋‹จ๊ณ„๋Š” ๋ชจ๋“  ๋ฆฌ๋ทฐ๋ฅผ ํ•˜๋‚˜๋กœ ๊ฒฐํ•ฉํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ฒญํฌ(chunk)๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ์ฒญํฌ๋Š” ์–ผ๋งˆ๋‚˜ ์ปค์•ผ ํ• ๊นŒ์š”? ์ด๊ฒƒ์€ ๊ถ๊ทน์ ์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ GPU ๋ฉ”๋ชจ๋ฆฌ์˜ ์–‘์— ๋”ฐ๋ผ ๊ฒฐ์ •๋˜์ง€๋งŒ ์ข‹์€ ์ถœ๋ฐœ์ ์€ ๋ชจ๋ธ์˜ ์ตœ๋Œ€ ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ๊ฐ€ ์–ผ๋งˆ์ธ์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ† ํฌ ๋‚˜์ด์ €์˜ model_max_length ์†์„ฑ์„ ํ™•์ธํ•˜์—ฌ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.model_max_length ์ด ๊ฐ’์€ ์ฒดํฌํฌ์ธํŠธ์™€ ์—ฐ๊ฒฐ๋œ tokenizer_config.json ํŒŒ์ผ์— ์ง€์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ๊ฐ€ BERT์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 512 ํ† ํฐ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. โœ Try it out! BigBird ๋ฐ Longformer์™€ ๊ฐ™์€ ์ผ๋ถ€ Transformer ๋ชจ๋ธ์€ BERT ๋ฐ ๊ธฐํƒ€ ์ดˆ๊ธฐ Transformer ๋ชจ๋ธ๋ณด๋‹ค ์ปจํ…์ŠคํŠธ ๊ธธ์ด๊ฐ€ ํ›จ์”ฌ ๋” ๊น๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ฒดํฌํฌ์ธํŠธ ์ค‘ ํ•˜๋‚˜์— ๋Œ€ํ•ด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  model_max_length๊ฐ€ ๋ชจ๋ธ ์นด๋“œ์— ์ธ์šฉ๋œ ๋‚ด์šฉ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. ๋”ฐ๋ผ์„œ Google Colab์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ GPU์—์„œ ์‹คํ—˜์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ฉ”๋ชจ๋ฆฌ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์•ฝ๊ฐ„ ์ž‘์€ ์ˆ˜์น˜๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค: chunk_size = 128 ์ฒญํฌ ํฌ๊ธฐ๋ฅผ ์ž‘๊ฒŒ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์‹ค์ œ ์‹คํ–‰ ์‹œ๋‚˜๋ฆฌ์˜ค์—์„œ๋Š” ๋ถˆ์ด์ต์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ชจ๋ธ์„ ์ ์šฉํ•  ์‚ฌ์šฉ ์‚ฌ๋ก€์— ํ•ด๋‹นํ•˜๋Š” ํฌ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํƒ€๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์žฌ๋ฏธ์žˆ๋Š” ๋ถ€๋ถ„์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๊ฒฐํ•ฉ ๊ณผ์ •์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ํ† ํฐํ™”๋œ ํ•™์Šต ์ง‘ํ•ฉ์—์„œ ๋ช‡๋ช‡ ๋ฆฌ๋ทฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋ฆฌ๋ทฐ๋‹น ํ† ํฐ ์ˆ˜๋ฅผ ์ธ์‡„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: # Slicing produces a list of lists for each feature tokenized_samples = tokenized_datasets["train"][:3] for idx, sample in enumerate(tokenized_samples["input_ids"]): print(f"'>>> Review {idx} length: {len(sample)}'") ์œ„์—์„œ ๋งŒ๋“ค์–ด์ง„ tokenized_samples๋ฅผ ๊ฐ„๋‹จํ•œ ๋”•์…”๋„ˆ๋ฆฌ ๋‚ดํฌ(dictionary comprehension) ๋ฌธ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฒฐํ•ฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: concatenated_examples = { k: sum(tokenized_samples[k], []) for k in tokenized_samples.keys() } total_length = len(concatenated_examples["input_ids"]) print(f"'>>> Concatenated reviews length: {total_length}'") ์ข‹์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๊ธธ์ด๊ฐ€ ํ™•์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์—ฐ๊ฒฐ๋œ ๋ฆฌ๋ทฐ๋ฅผ block_size๋กœ ์ง€์ •๋œ ํฌ๊ธฐ์˜ ์ฒญํฌ๋กœ ๋ถ„ํ• ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด concatenated_examples์˜ ๋‚ด์šฉ(์ž์งˆ, feature)์„ ๋ฐ˜๋ณตํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ ๋‚ดํฌ(list comprehension) ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ด์šฉ(์ž์งˆ, feature)๋“ค์˜ ์Šฌ๋ผ์ด์Šค๋“ค์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” ๊ฐ ์ž์งˆ(feature)์— ๋Œ€ํ•œ ์ฒญํฌ ๋”•์…”๋„ˆ๋ฆฌ์ž…๋‹ˆ๋‹ค: chunks = { k: [t[i : i + chunk_size] for i in range(0, total_length, chunk_size)] for k, t in concatenated_examples.items() } for chunk in chunks["input_ids"]: print(f"'>>> Chunk length: {len(chunk)}'") ์ด ์˜ˆ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ๋งˆ์ง€๋ง‰ ์ฒญํฌ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ตœ๋Œ€ ์ฒญํฌ ํฌ๊ธฐ๋ณด๋‹ค ์ž‘์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ์ „๋žต์ด ์žˆ์Šต๋‹ˆ๋‹ค: chunk_size๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ์ฒญํฌ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ๊ธธ์ด๊ฐ€ chunk_size์™€ ๊ฐ™์•„์งˆ ๋•Œ๊นŒ์ง€ ๋งˆ์ง€๋ง‰ ์ฒญํฌ๋ฅผ ์ฑ„์›๋‹ˆ๋‹ค(padding). ์—ฌ๊ธฐ์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ์ ‘๊ทผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์œ„์—์„œ ์˜ˆ์ œ์— ๋Œ€ํ•ด ์ ์šฉ๋œ ๋ชจ๋“  ๋กœ์ง๋“ค์„ ํ† ํฐํ™”๋œ ๋ฐ์ดํ„ฐ ์…‹์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์ผ ํ•จ์ˆ˜๋กœ ๊ตฌ์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: def group_texts(examples): # ๋ชจ๋“  ํ…์ŠคํŠธ๋“ค์„ ๊ฒฐํ•ฉํ•œ๋‹ค. concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} # ๊ฒฐํ•ฉ๋œ ํ…์ŠคํŠธ๋“ค์— ๋Œ€ํ•œ ๊ธธ์ด๋ฅผ ๊ตฌํ•œ๋‹ค. total_length = len(concatenated_examples[list(examples.keys())[0]]) # `chunk_size`๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ์ฒญํฌ๋ฅผ ์‚ญ์ œ total_length = (total_length // chunk_size) * chunk_size # max_len ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” chunk ๋‹จ์œ„๋กœ ์Šฌ๋ผ์ด์Šค result = { k: [t[i : i + chunk_size] for i in range(0, total_length, chunk_size)] for k, t in concatenated_examples.items() } # ์ƒˆ๋กœ์šด ๋ ˆ์ด๋ธ” ์นผ๋Ÿผ์„ ์ƒ์„ฑ result["labels"] = result["input_ids"].copy() return result group_texts()์˜ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์—์„œ input_ids ์—ด์˜ ๋ณต์‚ฌ๋ณธ์ธ ์ƒˆ labels ์—ด์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ณง ๋ณด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ, ๋งˆ์Šคํฌ ๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง์˜ ๋ชฉํ‘œ๋Š” ์ž…๋ ฅ ๋ฐฐ์น˜(batch)์—์„œ ๋ฌด์ž‘์œ„๋กœ ๋งˆ์Šคํ‚น ๋œ ํ† ํฐ์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด๊ณ  labels ์—ด์„ ์ƒ์„ฑํ•˜์—ฌ ์–ธ์–ด ๋ชจ๋ธ์ด ํ•™์Šตํ•  ์ •๋‹ต(ground truth)์„ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์ œ Dataset.map() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™”๋œ ๋ฐ์ดํ„ฐ ์…‹์— group_text()๋ฅผ ์ ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: lm_datasets = tokenized_datasets.map(group_texts, batched=True) lm_datasets ํ…์ŠคํŠธ๋ฅผ ๊ทธ๋ฃนํ™”ํ•œ ๋‹ค์Œ ์ฒญํฌํ™”ํ•˜๋ฉด train ๋ฐ test ๋ถ„ํ• ์— ๋Œ€ํ•ด ์›๋ž˜ 25,000๊ฐœ๋ณด๋‹ค ํ›จ์”ฌ ๋” ๋งŽ์€ ์˜ˆ์ œ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ด์ œ ์›๋ณธ ๋ง๋ญ‰์น˜์˜ ์—ฌ๋Ÿฌ ์˜ˆ์ œ์— ๊ฑธ์ณ ์žˆ๋Š” ์—ฐ์†๋œ ํ† ํฐ๋“ค(contiguous tokens)์— ์˜ํ•œ ์˜ˆ์ œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฒญํฌ ์ค‘ ํ•˜๋‚˜์—์„œ ํŠน์ˆ˜ ํ† ํฐ์ธ [SEP] ๋ฐ [CLS] ํ† ํฐ์„ ์ฐพ์•„ ์ด๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenizer.decode(lm_datasets["train"][1]["input_ids"]) ์ด ์˜ˆ์—์„œ ๋‘ ๊ฐœ์˜ ๊ฒน์น˜๋Š” ์˜ํ™” ๋ฆฌ๋ทฐ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์Šคํฌ ๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง์˜ ๋ ˆ์ด๋ธ”์ด ์–ด๋–ป๊ฒŒ ๋ณด์ด๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: tokenizer.decode(lm_datasets["train"][1]["labels"]) ์œ„์˜ group_text() ํ•จ์ˆ˜์—์„œ ์˜ˆ์ƒํ•œ ๋Œ€๋กœ ๋””์ฝ”๋”ฉ ๋œ input_ids์™€ ๋™์ผํ•˜๊ฒŒ ๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์šฐ๋ฆฌ ๋ชจ๋ธ์ด ์–ด๋–ป๊ฒŒ ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ค‘์š”ํ•œ ๋‹จ๊ณ„๋ฅผ ํ•˜๋‚˜ ๋นผ๋จน์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ [MASK] ํ† ํฐ์„ ์ž…๋ ฅ์˜ ์ž„์˜ ์œ„์น˜์— ์‚ฝ์ž…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค! ํŠน์ˆ˜ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘๊ธฐ(data collator)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฏธ์„ธ ์กฐ์ • ์ค‘์— ์ด ์ž‘์—…์„ ์ฆ‰์„์—์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Trainer API๋ฅผ ์ด์šฉํ•˜์—ฌ DistilBERT ๋ฏธ์„ธ์กฐ์ • ๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์€ 3์žฅ์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์‹œํ€€์Šค ๋ถ„๋ฅ˜ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ํ…์ŠคํŠธ์˜ ๊ฐ ๋ฐฐ์น˜(batch)์—์„œ ์ผ๋ถ€ ํ† ํฐ์„ ๋ฌด์ž‘์œ„๋กœ ๋งˆ์Šคํ‚น ํ•  ์ˆ˜ ์žˆ๋Š” ํŠน์ˆ˜ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘๊ธฐ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ Transformers๋Š” ์ด ์ž‘์—…์„ ์œ„ํ•œ ์ „์šฉ DataCollatorForLanguageModeling ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ† ํฐํ™”์™€ ๋งˆ์Šคํ‚น ํ•  ํ† ํฐ์˜ ๋น„์œจ์„ ์ง€์ •ํ•˜๋Š” mlm_probability ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 15%๋กœ ์ง€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ BERT์— ์‚ฌ์šฉ๋˜๋Š” ์ˆ˜์น˜์ด๋ฉฐ ๋…ผ๋ฌธ์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ˆ˜์น˜์ž…๋‹ˆ๋‹ค: from transformers import DataCollatorForLanguageModeling data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=0.15) ๋ฌด์ž‘์œ„ ๋งˆ์Šคํ‚น(random masking)์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์œ„ํ•ด data_collator์— ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์‹œ๋ฅผ ์ž…๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. data_collator๊ฐ€ ์—ฐ์† ํ…์ŠคํŠธ์˜ ๋‹จ์ผ ์ฒญํฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” dict์˜ ๋ฆฌ์ŠคํŠธ์ธ dicts๋ฅผ ์š”๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์น˜๋ฅผ collator์— ๊ณต๊ธ‰ํ•˜๊ธฐ ์ „์— ๋จผ์ € ๋ฐ์ดํ„ฐ ์…‹์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. data_collator์˜ ์ž…๋ ฅ์œผ๋กœ word_ids ํ‚ค๋Š” ๋ถˆํ•„์š”ํ•˜๋ฏ€๋กœ ์ด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค: samples = [lm_datasets["train"][i] for i in range(2)] for sample in samples: _ = sample.pop("word_ids") for chunk in data_collator(samples)["input_ids"]: print(f"\n'>>> {tokenizer.decode(chunk)}'") ์ข‹์Šต๋‹ˆ๋‹ค. ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”๊ตฐ์š”! [MASK] ํ† ํฐ์ด ํ…์ŠคํŠธ์˜ ๋‹ค์–‘ํ•œ ์œ„์น˜์— ๋ฌด์ž‘์œ„๋กœ ์‚ฝ์ž…๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ ์šฐ๋ฆฌ ๋ชจ๋ธ์ด ํ•™์Šต ๊ณผ์ •์—์„œ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ํ† ํฐ์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ(data collator)์˜ ์žฅ์ ์€ ๋ฐฐ์น˜๋งˆ๋‹ค [MASK] ์‚ฝ์ž…์ด ๋ฌด์ž‘์œ„ํ™”๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค! โœ Try it out! ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•˜์—ฌ ๋ˆˆ์•ž์—์„œ ๋ฌด์ž‘์œ„ ๋งˆ์Šคํ‚น์ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค! ๋˜ํ•œ tokenizer.decode() ๋ฉ”์„œ๋“œ๋ฅผ tokenizer.convert_ids_to_tokens()๋กœ ๊ต์ฒดํ•˜์—ฌ ๋•Œ๋•Œ๋กœ ์ฃผ์–ด์ง„ ๋‹จ์–ด์˜ ๋‹จ์ผ ํ† ํฐ์ด ๋งˆ์Šคํ‚น ๋˜๊ณ  ๋‹ค๋ฅธ ํ† ํฐ์€ ๋งˆ์Šคํ‚น ๋˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋ฌด์ž‘์œ„ ๋งˆ์Šคํ‚น์˜ ํ•œ ๊ฐ€์ง€ ๋ถ€์ž‘์šฉ์€ ํ•™์Šต ๋ฐ ํ…Œ์ŠคํŠธ ์ง‘ํ•ฉ์— ๋Œ€ํ•ด ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ(data collator)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— Trainer๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ‰๊ฐ€ ๋ฉ”ํŠธ๋ฆญ์ด ๊ฒฐ์ •์ ์ด์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์šฐ๋ฆฌ๊ฐ€ Accelerate๋กœ ๋ฏธ์„ธ ์กฐ์ •์„ ํ•  ๋•Œ ์‚ฌ์šฉ์ž ์ง€์ • ํ‰๊ฐ€ ๋ฃจํ”„์˜ ์œ ์—ฐ์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž„์˜์„ฑ์„ ๊ณ ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งˆ์Šคํ‚น ๋œ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ํ•™์Šตํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ ๊ฐ€์ง€ ๊ธฐ๋ฒ•์€ ๊ฐœ๋ณ„ ํ† ํฐ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ „์ฒด ๋‹จ์–ด๋ฅผ ํ•จ๊ป˜ ๋งˆ์Šคํ‚น ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ ‘๊ทผ ๋ฐฉ์‹์„ ์ „์ฒด ๋‹จ์–ด ๋งˆ์Šคํ‚น(whole word masking)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ๋‹จ์–ด ๋งˆ์Šคํ‚น์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ(data collator)๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๋Š” ์ƒ˜ํ”Œ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์™€ ์ผ๊ด„ ์ฒ˜๋ฆฌ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ผ ๋ฟ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ๊ณ„์‚ฐ๋œ ๋‹จ์–ด ID๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์–ด ์ธ๋ฑ์Šค์™€ ํ•ด๋‹น ํ† ํฐ ์‚ฌ์ด์˜ ๋งต์„ ๋งŒ๋“  ๋‹ค์Œ ๋งˆ์Šคํฌ ํ•  ๋‹จ์–ด๋ฅผ ๋ฌด์ž‘์œ„๋กœ ๊ฒฐ์ •ํ•˜๊ณ  ์ž…๋ ฅ์— ํ•ด๋‹น ๋งˆ์Šคํฌ๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์€ ๋งˆ์Šคํฌ ๋‹จ์–ด์— ํ•ด๋‹นํ•˜๋Š” ๋ ˆ์ด๋ธ”์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ -100์ž…๋‹ˆ๋‹ค. import collections import numpy as np from transformers import default_data_collator wwm_probability = 0.2 def whole_word_masking_data_collator(features): for feature in features: word_ids = feature.pop("word_ids") # ๋‹จ์–ด์™€ ํ•ด๋‹น ํ† ํฐ ์ธ๋ฑ์Šค ๊ฐ„์˜ map ์ƒ์„ฑ mapping = collections.defaultdict(list) current_word_index = -1 current_word = None for idx, word_id in enumerate(word_ids): if word_id is not None: if word_id != current_word: current_word = word_id current_word_index += 1 mapping[current_word_index].append(idx) # ๋ฌด์ž‘์œ„๋กœ ๋‹จ์–ด ๋งˆ์Šคํ‚น mask = np.random.binomial(1, wwm_probability, (len(mapping),)) input_ids = feature["input_ids"] labels = feature["labels"] new_labels = [-100] * len(labels) for word_id in np.where(mask)[0]: word_id = word_id.item() for idx in mapping[word_id]: new_labels[idx] = labels[idx] input_ids[idx] = tokenizer.mask_token_id return default_data_collator(features) ๋‹ค์Œ์œผ๋กœ ์ด์ „๊ณผ ๋™์ผํ•œ ์ƒ˜ํ”Œ์—์„œ ์œ„ ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: samples = [lm_datasets["train"][i] for i in range(2)] batch = whole_word_masking_data_collator(samples) for chunk in batch["input_ids"]: print(f"\n'>>> {tokenizer.decode(chunk)}'") โœ Try it out! ์œ„์˜ ์ฝ”๋“œ ์กฐ๊ฐ์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•˜์—ฌ ๋ˆˆ์•ž์—์„œ ๋ฌด์ž‘์œ„ ๋งˆ์Šคํ‚น์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค! ๋˜ํ•œ tokenizer.decode() ๋ฉ”์„œ๋“œ๋ฅผ tokenizer.convert_ids_to_tokens()๋กœ ๊ต์ฒดํ•˜์—ฌ ์ฃผ์–ด์ง„ ๋‹จ์–ด์˜ ํ† ํฐ์ด ํ•ญ์ƒ ํ•จ๊ป˜ ๋งˆ์Šคํ‚น ๋˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋‘ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋‚˜๋จธ์ง€ ๋ฏธ์„ธ ์กฐ์ • ๋‹จ๊ณ„๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์šด์ด ์ข‹์ง€ ์•Š์€ ๊ฒฝ์šฐ Google Colab์—์„œ ํ•™์Šตํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋จผ์ € ํ•™์Šต ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ˆ˜์ฒœ ๊ฐœ์˜ ์˜ˆ์ œ ์ •๋„๋กœ ๋‹ค์šด ์ƒ˜ํ”Œ๋งํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์„ธ์š”. ๊ทธ๋ž˜๋„ ์šฐ๋ฆฌ๋Š” ์—ฌ์ „ํžˆ ๊ฝค ๊ดœ์ฐฎ์€ ์–ธ์–ด ๋ชจ๋ธ์„ ์–ป์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค! Datasets์—์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด ์ƒ˜ํ”Œ๋งํ•˜๋Š” ๋น ๋ฅธ ๋ฐฉ๋ฒ•์€ 5์žฅ์—์„œ ๋ณธ Dataset.train_test_split() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: train_size = 10_000 test_size = int(0.1 * train_size) downsampled_dataset = lm_datasets["train"].train_test_split( train_size=train_size, test_size=test_size, seed=42 ) downsampled_dataset ์œ„ ์ฝ”๋“œ๋Š” ํ•™์Šต ์ง‘ํ•ฉ ํฌ๊ธฐ๊ฐ€ 10,000๊ฐœ์˜ ์˜ˆ์ œ๋กœ ์„ค์ •๋˜๊ณ  ํ…Œ์ŠคํŠธ ์ง‘ํ•ฉ์ด ๊ทธ์ค‘ 10%๋กœ ์„ค์ •๋œ ์ƒˆ๋กœ์šด ํ•™์Šต ๋ฐ ํ…Œ์ŠคํŠธ ๋ถ„ํ• ์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ•๋ ฅํ•œ GPU๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ž์œ ๋กญ๊ฒŒ ๋Š˜๋ฆฌ์‹ญ์‹œ์˜ค! ๋‹ค์Œ์œผ๋กœ ํ•ด์•ผ ํ•  ์ผ์€ Hugging Face Hub์— ๋กœ๊ทธ์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋…ธํŠธ๋ถ์—์„œ ์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค from huggingface_hub import notebook_login notebook_login() ์ž๊ฒฉ ์ฆ๋ช…์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์ ฏ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋˜๋Š” "huggingface-cli login" ๋ช…๋ น์–ด๋ฅผ ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ํ•˜์—ฌ ๋กœ๊ทธ์ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋‹จ ๋กœ๊ทธ์ธํ•˜๋ฉด Trainer์— ๋Œ€ํ•œ ์ธ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import TrainingArguments batch_size = 64 # Show the training loss with every epoch logging_steps = len(downsampled_dataset["train"]) // batch_size model_name = model_checkpoint.split("/")[-1] training_args = TrainingArguments( output_dir=f"{model_name}-finetuned-imdb", overwrite_output_dir=True, evaluation_strategy="epoch", learning_rate=2e-5, weight_decay=0.01, per_device_train_batch_size=batch_size, per_device_eval_batch_size=batch_size, push_to_hub=True, fp16=True, logging_steps=logging_steps, ) ์—ฌ๊ธฐ์—์„œ logging_steps๋ฅผ ํฌํ•จํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€ ๊ธฐ๋ณธ ์˜ต์…˜์„ ์กฐ์ •ํ•˜์—ฌ ๊ฐ ์—ํฌํฌ์—์„œ ํ•™์Šต ์†์‹ค์„ ์ถ”์ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ fp16=True๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ›ˆ๋ จ์„ ํ™œ์„ฑํ™”ํ•˜์—ฌ ์†๋„๋ฅผ ํ•œ ๋ฒˆ ๋” ํ–ฅ์ƒ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. hub_model_id ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘ธ์‹œ ํ•˜๋ ค๋Š” ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(ํŠนํžˆ, ํŠน์ • ์กฐ์ง์— ํ‘ธ์‹œ ํ•˜๋ ค๋ฉด ์ด ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ). ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจ๋ธ์„ huggingface-course organization์— ํ‘ธ์‹œ ํ•  ๋•Œ Hub_model_id="huggingface-course/distilbert-finetuned-imdb"๋ฅผ TrainingArguments์— ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ €์žฅ์†Œ๋Š” ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์žˆ๊ณ  ์„ค์ •ํ•œ ์ถœ๋ ฅ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ๋ช…๋ช…๋˜๋ฏ€๋กœ ์ด ๊ฒฝ์šฐ์—๋Š” "spasis/distilbert-finetuned-imdb"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ Trainer๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๋ชจ๋“  ์ค€๋น„ ์ž‘์—…์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” ํ‘œ์ค€ data_collator๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์ „์ฒด ๋‹จ์–ด ๋งˆ์Šคํ‚น(whole word masking) ์ฝœ๋ ˆ ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import Trainer trainer = Trainer( model=model, args=training_args, train_dataset=downsampled_dataset["train"], eval_dataset=downsampled_dataset["test"], data_collator=data_collator, ) ์ด์ œ trainer.train()์„ ์‹คํ–‰ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ์ „์— ์–ธ์–ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์ง€ํ‘œ์ธ perplexity์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์„ ์œ„ํ•œ Perplexity ํ•™์Šตํ•  ๋ ˆ์ด๋ธ”์ด ํ‘œ๊ธฐ๋œ ๋ง๋ญ‰์น˜๊ฐ€ ์ œ๊ณต๋˜๋Š” ํ…์ŠคํŠธ ๋ถ„๋ฅ˜(text classification) ๋˜๋Š” ์งˆ์˜์‘๋‹ต(question answering)๊ณผ ๊ฐ™์€ ์ž‘์—…๊ณผ ๋‹ฌ๋ฆฌ, ์–ธ์–ด ๋ชจ๋ธ๋ง์—๋Š” ๋ช…์‹œ์  ๋ ˆ์ด๋ธ”์ด ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌด์—‡์ด ์ข‹์€ ์–ธ์–ด ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š”์ง€ ์–ด๋–ป๊ฒŒ ๊ฒฐ์ •ํ• ๊นŒ์š”? ํœด๋Œ€์ „ํ™”์˜ ์ž๋™ ๊ณ ์นจ ๊ธฐ๋Šฅ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ข‹์€ ์–ธ์–ด ๋ชจ๋ธ์€ ๋ฌธ๋ฒ•์ ์œผ๋กœ ์ •ํ™•ํ•œ ๋ฌธ์žฅ์— ๋†’์€ ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๊ณ  ๋ง๋„ ์•ˆ ๋˜๋Š” ๋ฌธ์žฅ์— ๋‚ฎ์€ ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด ๋ง๋„ ์•ˆ ๋˜๋Š” ๋ฌธ์žฅ์ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ๋Š”์ง€์— ๋Œ€ํ•œ ๋” ๋‚˜์€ ์•„์ด๋””์–ด๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์˜จ๋ผ์ธ์—์„œ "์ž๋™ ๊ณ ์นจ ์‹คํŒจ(autocorrect fails)"์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ ์…‹์—๋Š” ์‚ฌ๋žŒ์˜ ์ „ํ™”์— ์žˆ๋Š” ๋ชจ๋ธ์ด ๋‹ค์†Œ ์›ƒ๊ธฐ๊ณ  ์ข…์ข… ๋ถ€์ ์ ˆํ•œ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•œ ์˜ˆ์‹œ๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ์…‹์ด ๋Œ€๋ถ€๋ถ„ ๋ฌธ๋ฒ•์ ์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์žฅ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ์–ธ์–ด ๋ชจ๋ธ์˜ ํ’ˆ์งˆ์„ ์ธก์ •ํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ํ…Œ์ŠคํŠธ์…‹์˜ ๋ชจ๋“  ๋ฌธ์žฅ์—์„œ ๋‹ค์Œ ๋‹จ์–ด์— ํ• ๋‹นํ•  ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋†’์€ ํ™•๋ฅ ์€ ๋ชจ๋ธ์ด ์ƒˆ๋กœ์šด ์˜ˆ์‹œ์— "๋†€๋ผ๊ฑฐ๋‚˜(surprised)" "๋‹นํ™ฉํ•˜์ง€(perplexed)" ์•Š์•˜์Œ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ ์–ธ์–ด์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ํŒจํ„ด์„ ํ•™์Šตํ–ˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด perplexity์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ˆ˜ํ•™์  ์ •์˜๊ฐ€ ์žˆ์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ๊ฒƒ์€ ๊ต์ฐจ ์—”ํŠธ๋กœํ”ผ ์†์‹ค(cross-entropy loss)์˜<NAME>(exponential)๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Trainer.evaluate() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ์…‹์— ๋Œ€ํ•œ ๊ต์ฐจ ์—”ํŠธ๋กœํ”ผ ์†์‹ค์„ ๊ณ„์‚ฐํ•œ ๋‹ค์Œ<NAME>๋ฅผ ์ทจํ•˜์—ฌ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ๋ณต์žก๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: import math eval_results = trainer.evaluate() print(f">>> Perplexity: {math.exp(eval_results['eval_loss']):.2f}") ๋‚ฎ์€ perplexity ์ ์ˆ˜๋Š” ๋” ๋‚˜์€ ์–ธ์–ด ๋ชจ๋ธ์„ ์˜๋ฏธํ•˜๋ฉฐ, ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ์˜ ์‹œ์ž‘ ๋ชจ๋ธ์ด ๋‹ค์†Œ ํฐ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ์„ธ ์กฐ์ •์œผ๋กœ ์ด ์ ์ˆ˜๋ฅผ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ๋Š”์ง€ ๋ด…์‹œ๋‹ค! ์ด๋ฅผ ์œ„ํ•ด ๋จผ์ € ํ•™์Šต ๋ฃจํ”„๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค: trainer.train() ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด์ „๊ณผ ๊ฐ™์ด ํ…Œ์ŠคํŠธ์…‹์— ๋Œ€ํ•ด perplexity ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค: eval_results = trainer.evaluate() print(f">>> Perplexity: {math.exp(eval_results['eval_loss']):.2f}") ์ข‹์Šต๋‹ˆ๋‹ค! Perplexity ์ ์ˆ˜๊ฐ€ ๋งŽ์ด ๋‚ฎ์•„์กŒ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด ์˜ํ™” ๋ฆฌ๋ทฐ ๋„๋ฉ”์ธ์— ๋Œ€ํ•ด์„œ ๋ฌด์–ธ๊ฐ€๋ฅผ ๋ฐฐ์› ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ํ•™์Šต์ด ๋๋‚˜๋ฉด ํ•™์Šต ์ •๋ณด๊ฐ€ ์žˆ๋Š” ๋ชจ๋ธ ์นด๋“œ๋ฅผ Hub๋กœ ํ‘ธ์‹œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์ฒดํฌํฌ์ธํŠธ๋Š” ํ•™์Šต ์ค‘์— ์ €์žฅ๋จ): trainer.push_to_hub() โœ Your turn! ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๋ฅผ ์ „์ฒด ์›Œ๋“œ ๋งˆ์Šคํ‚น(whole word masking) ์ฝœ๋ ˆ ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝํ•œ ํ›„ ์œ„์˜ ํ•™์Šต์„ ์‹คํ–‰ํ•ด ๋ณด์„ธ์š”. ๋” ๋‚˜์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„๊นŒ์š”? ์ง€๊ธˆ๊นŒ์ง€ ์‚ดํŽด๋ณธ ์‚ฌ๋ก€์—์„œ๋Š” ํ•™์Šต ๋ฃจํ”„๋กœ ํŠน๋ณ„ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ์ง€๋งŒ ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ์ผ๋ถ€ ์‚ฌ์šฉ์ž ์ •์˜ ๋กœ์ง์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Accelerate๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ๊ฐ€ ๊ทธ๊ฒƒ์ด์ฃ . ํ•œ๋ฒˆ ์‚ดํŽด๋ด…์‹œ๋‹ค! Accelerate๋ฅผ ํ™œ์šฉํ•œ DistilBERT ๋ฏธ์„ธ์กฐ์ • Trainer์—์„œ ๋ณด์•˜๋“ฏ์ด ๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์€ 3์žฅ์˜ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์˜ˆ์ œ์™€ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค, ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ํŠน๋ณ„ํ•œ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ(data collator)์˜ ์‚ฌ์šฉ์ด๋ฉฐ, ์ด๋ฏธ ์ด ์„น์…˜์˜ ์•ž๋ถ€๋ถ„์—์„œ ๋‹ค๋ฃจ์—ˆ์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฌ๋‚˜ DataCollatorForLanguageModeling์ด ๋ชจ๋ธ ํ‰๊ฐ€(evaluation) ๊ณผ์ •์—์„œ ๋ฌด์ž‘์œ„ ๋งˆ์Šคํ‚น(random masking)๋„ ์ ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ ํ•™์Šต ์‹คํ–‰์—์„œ perplexity ์ ์ˆ˜์— ์•ฝ๊ฐ„์˜ ๋ณ€๋™์ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌด์ž‘์œ„์„ฑ์˜ ์›์ธ์„ ์ œ๊ฑฐํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ์ „์ฒด ํ…Œ์ŠคํŠธ ์ง‘ํ•ฉ์— ๋Œ€ํ•ด์„œ ๋งˆ์Šคํ‚น์„ ํ•œ ๋ฒˆ๋งŒ ์ ์šฉํ•œ ๋‹ค์Œ Transformers์˜ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‰๊ฐ€๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ์ ์šฉํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์œ„ํ•ด DataCollatorForLanguageModeling๋ฅผ ์ฒ˜์Œ ์ ‘ํ–ˆ์„ ๋•Œ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๊ฐœ๋ณ„ ๋ฐฐ์น˜(batch)์— ๋งˆ์Šคํ‚น์„ ์ ์šฉํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def insert_random_mask(batch): features = [dict(zip(batch, t)) for t in zip(*batch.values())] masked_inputs = data_collator(features) # ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ฐ ์นผ๋Ÿผ์— ๋Œ€ํ•ด์„œ ์ƒˆ๋กœ์šด "masked" ์นผ๋Ÿผ์„ ์ƒ์„ฑ return {"masked_" + k: v.numpy() for k, v in masked_inputs.items()} ๋‹ค์Œ์œผ๋กœ ์ด ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธ ์ง‘ํ•ฉ์— ์ ์šฉํ•˜๊ณ  ๋งˆ์Šคํฌ ๋˜์ง€ ์•Š์€ ์—ด์„ ์‚ญ์ œํ•˜์—ฌ ๋งˆ์Šคํฌ ๋œ ์—ด๋กœ ๊ต์ฒดํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ data_collator๋ฅผ ์ ์ ˆํ•œ ๋Œ€์ฒด ํ•จ์ˆ˜๋กœ ๊ต์ฒดํ•˜์—ฌ ์ „์ฒด ๋‹จ์–ด ๋งˆ์Šคํ‚น์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์•„๋ž˜ ์ฝ”๋“œ ์ค‘์—์„œ ์ฒซ ๋ฒˆ์งธ ๋ผ์ธ์„ ์ œ๊ฑฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: downsampled_dataset = downsampled_dataset.remove_columns(["word_ids"]) eval_dataset = downsampled_dataset["test"].map( insert_random_mask, batched=True, remove_columns=downsampled_dataset["test"].column_names, ) eval_dataset = eval_dataset.rename_columns( { "masked_input_ids": "input_ids", "masked_attention_mask": "attention_mask", "masked_labels": "labels", } ) ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด์ „๊ณผ ๊ฐ™์ด dataloaders๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋‹จ, ํ‰๊ฐ€ ์ง‘ํ•ฉ์— ๋Œ€ํ•ด Transformers์˜ default_data_collator๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from torch.utils.data import DataLoader from transformers import default_data_collator batch_size = 64 train_dataloader = DataLoader( downsampled_dataset["train"], shuffle=True, batch_size=batch_size, collate_fn=data_collator, ) eval_dataloader = DataLoader( eval_dataset, batch_size=batch_size, collate_fn=default_data_collator ) ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” Accelerate์˜ ํ‘œ์ค€ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ์ƒˆ๋กœ์šด ๋ฒ„์ „์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: model = AutoModelForMaskedLM.from_pretrained(model_checkpoint) ๊ทธ๋Ÿฐ ๋‹ค์Œ ์˜ตํ‹ฐ๋งˆ์ด์ €(optimizer)๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ‘œ์ค€ AdamW๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: from torch.optim import AdamW optimizer = AdamW(model.parameters(), lr=5e-5) ์ด์ œ Accelerator ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ํ•™์Šตํ•  ๋ชจ๋“  ์ค€๋น„๊ฐ€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค: from accelerate import Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader ) ์ด์ œ ๋ชจ๋ธ, ์˜ตํ‹ฐ๋งˆ์ด์ € ๋ฐ dataloaders๊ฐ€ ๊ตฌ์„ฑ๋˜์—ˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•™์Šต๋ฅ  ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import get_scheduler num_train_epochs = 3 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) ํ•™์Šต ์ „์— ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•ด์•ผ ํ•  ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค: Hugging Face Hub์— ๋ชจ๋ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”! Hub ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋จผ์ € ์ €์žฅ์†Œ์˜ ์ „์ฒด ์ด๋ฆ„์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from huggingface_hub import get_full_repo_name model_name = "distilbert-base-uncased-finetuned-imdb-accelerate" repo_name = get_full_repo_name(model_name) repo_name ๊ทธ๋Ÿฐ ๋‹ค์Œ Hub์˜ Repository ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ €์žฅ์†Œ๋ฅผ ๋งŒ๋“ค๊ณ  ๋ณต์ œํ•ฉ๋‹ˆ๋‹ค: from huggingface_hub import Repository import os os.environ["TOKENIZERS_PARALLELISM"] = "false" output_dir = model_name repo = Repository(output_dir, clone_from=repo_name) ์œ„ ์ž‘์—…์ด ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด ์ „์ฒด ํ•™์Šต ๋ฐ ํ‰๊ฐ€ ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: from tqdm.auto import tqdm import torch import math progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): # ํ•™์Šต model.train() for batch in train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) # ํ‰๊ฐ€ model.eval() losses = [] for step, batch in enumerate(eval_dataloader): with torch.no_grad(): outputs = model(**batch) loss = outputs.loss losses.append(accelerator.gather(loss.repeat(batch_size))) losses = torch.cat(losses) losses = losses[: len(eval_dataset)] try: perplexity = math.exp(torch.mean(losses)) except OverflowError: perplexity = float("inf") print(f">>> Epoch {epoch}: Perplexity: {perplexity}") # Save and upload accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message = f"Training in progress epoch {epoch}", blocking=False ) ์ข‹์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ฐ ์—ํฌํฌ์˜ perplexity๋ฅผ ํ‰๊ฐ€ํ•˜๊ณ  ๋‹ค์ค‘ ํ•™์Šต ์‹คํ–‰์ด ์žฌํ˜„๋  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ์„ธ์กฐ์ •๋œ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ์šฐ๋ฆฌ๊ฐ€ ๋„์ถœํ•œ ๋ฏธ์„ธ ์กฐ์ • ์™„๋ฃŒ๋œ ๋ชจ๋ธ์„ Hub์—์„œ ์œ„์ ฏ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ Transformers์˜ pipeline์„ ํ†ตํ•ด ๋กœ์ปฌ๋กœ ์ƒํ˜ธ ์ž‘์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›„์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ fill-mask ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: from transformers import pipeline mask_filler = pipeline( "fill-mask", model="huggingface-course/distilbert-base-uncased-finetuned-imdb" ) ๊ทธ๋Ÿฐ ๋‹ค์Œ ํŒŒ์ดํ”„๋ผ์ธ์— "This is a great [MASK]"๋ผ๋Š” ์ƒ˜ํ”Œ ํ…์ŠคํŠธ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์ƒ์œ„ 5๊ฐœ ์˜ˆ์ธก์ด ๋ฌด์—‡์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: text = "This is a great [MASK]" preds = mask_filler(text) for pred in preds: print(f">>> {pred['sequence']}") ์šฐ๋ฆฌ ๋ชจ๋ธ์€ ์˜ํ™”์™€ ๋” ๋ฐ€์ ‘ํ•˜๊ฒŒ ๊ด€๋ จ๋œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์กฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค! ์ด๊ฒƒ์œผ๋กœ ์–ธ์–ด ๋ชจ๋ธ ํ•™์Šต์— ๋Œ€ํ•œ ์ฒซ ๋ฒˆ์งธ ์‹คํ—˜์„ ๋งˆ์นฉ๋‹ˆ๋‹ค. ์„น์…˜ 6์—์„œ๋Š” GPT-2์™€ ๊ฐ™์€ ์ž๋™ ํšŒ๊ท€ ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›๋‹ˆ๋‹ค. ์ž์‹ ๋งŒ์˜ Transformer ๋ชจ๋ธ์„ ์‚ฌ์ „ ํ•™์Šต์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์ฐธ๊ณ ํ•˜๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. โœ Try it out! ๋„๋ฉ”์ธ ์–ด๋Žํ…Œ์ด์…˜์˜ ์žฅ์ ์„ ์ •๋Ÿ‰์ ์œผ๋กœ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์ „ ํ•™์Šต๋œ DistilBERT ์ฒดํฌํฌ์ธํŠธ์™€ ๋ฏธ์„ธ ์กฐ์ •๋œ DistilBERT ์ฒดํฌํฌ์ธํŠธ ๋ชจ๋‘์— ๋Œ€ํ•ด IMDb ๋ ˆ์ด๋ธ” ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•ด ๋ณด์„ธ์š”. ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์— ๋Œ€ํ•œ ๋ณต์Šต์ด ํ•„์š”ํ•˜๋ฉด 3์žฅ์„ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. 3. ๋ฒˆ์—ญ (Translation) ์ด์ œ ๋ฒˆ์—ญ(translation)์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค. ์ด๊ฒƒ์€ ๋˜ ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ sequence-to-sequence ํƒœ์Šคํฌ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํ•œ ์‹œํ€€์Šค์—์„œ ๋‹ค๋ฅธ ์‹œํ€€์Šค๋กœ ์ด๋™ํ•˜๋Š”(๋ณ€ํ˜•ํ•˜๋Š”) ๊ฒƒ์œผ๋กœ ๊ณต์‹ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ์˜๋ฏธ์—์„œ ์ด ๋ฌธ์ œ๋Š” ์š”์•ฝ(summarization)๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•˜๊ณ  ์—ฌ๊ธฐ์—์„œ ๋ณด๊ฒŒ ๋  ๋‚ด์šฉ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ sequence-to-sequence ๋ฌธ์ œ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ์Šคํƒ€์ผ ํŠธ๋žœ์Šคํผ(style transfer): ํŠน์ • ์Šคํƒ€์ผ๋กœ ์ž‘์„ฑ๋œ ํ…์ŠคํŠธ๋ฅผ ๋‹ค๋ฅธ ์Šคํƒ€์ผ๋กœ ๋ฒˆ์—ญํ•˜๋Š” ๋ชจ๋ธ ์ƒ์„ฑ(์˜ˆ: ๊ฒฉ์‹ ์Šคํƒ€์ผ์—์„œ ์บ์ฃผ์–ผ ์Šคํƒ€์ผ๋กœ ๋˜๋Š” ์…ฐ์ต์Šคํ”ผ์–ด ์˜์–ด์—์„œ ํ˜„๋Œ€ ์˜์–ด๋กœ) ์ƒ์„ฑ ๊ธฐ๋ฐ˜ ์งˆ์˜์‘๋‹ต(generative question answering): ์ฃผ์–ด์ง„ ๋งฅ๋ฝ(context)์—์„œ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋ธ ๋งŒ๋“ค๊ธฐ ๋‘ ๊ฐœ(๋˜๋Š” ๊ทธ ์ด์ƒ) ์–ธ์–ด๋กœ ๋œ ์ถฉ๋ถ„ํžˆ ํฐ ํ…์ŠคํŠธ ์ฝ”ํผ์Šค๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง(causal language modeling) ์„น์…˜์—์„œ ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด ๋ฒˆ์—ญ ๋ชจ๋ธ์„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ผ์ • ๊ทœ๋ชจ์˜ ํŠน์ • ์–ธ์–ด๋กœ ํ‘œํ˜„๋œ ๋ง๋ญ‰์น˜๋ฅผ ๊ฐ€์ง€๊ณ  mT5๋‚˜ mBART ๋“ฑ๊ณผ ๊ฐ™์€ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋‹ค๊ตญ์–ด ๋ฒˆ์—ญ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ํŽธ์ด ํ›จ์”ฌ ๋น ๋ฅผ ๊ฒ๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” KDE ์•ฑ์šฉ์œผ๋กœ ํ˜„์ง€ํ™”๋œ ํŒŒ์ผ ๋ฐ์ดํ„ฐ ์…‹์ธ KDE4 ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜์—ฌ ์˜์–ด์—์„œ ํ”„๋ž‘์Šค์–ด๋กœ ๋ฒˆ์—ญํ•˜๋„๋ก ์‚ฌ์ „ ํ•™์Šต๋œ Marian ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ๋ชจ๋ธ์€ ์‹ค์ œ๋กœ KDE4 ๋ฐ์ดํ„ฐ ์…‹์ด ํฌํ•จ๋œ Opus ๋ฐ์ดํ„ฐ ์…‹์—์„œ ๊ฐ€์ ธ์˜จ ๋Œ€๊ทœ๋ชจ ํ”„๋ž‘์Šค์–ด ๋ฐ ์˜์–ด ํ…์ŠคํŠธ ์ฝ”ํผ์Šค์—์„œ ์‚ฌ์ „ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์ด ํ•™์Šต ๊ณผ์ •์—์„œ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹์„ ์ด๋ฏธ ํ•™์Šตํ–ˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๋ฏธ์„ธ ์กฐ์ • ํ›„์— ๋” ๋‚˜์€ ๋ฒ„์ „์„ ์–ป์„ ์ˆ˜ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์™„๋ฃŒ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ˆ์ธก์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ์ด์ „ ์„น์…˜์—์„œ์™€ ๊ฐ™์ด ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šตํ•˜๊ณ  Hub์— ์—…๋กœ๋“œํ•  ์‹ค์ œ ๋ชจ๋ธ์„ ์ฐพ๊ณ  ์—ฌ๊ธฐ์—์„œ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ค€๋น„ ๋ฒˆ์—ญ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ฑฐ๋‚˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‚ฌ์ „ ํ•™์Šตํ•˜๋ ค๋ฉด ์ž‘์—…์— ์ ํ•ฉํ•œ ๋ฐ์ดํ„ฐ ์…‹์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „์— ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ์ด ์„น์…˜์—์„œ๋Š” KDE4 ๋ฐ์ดํ„ฐ ์…‹์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๋ฒˆ์—ญํ•˜๋ ค๋Š” ๋‘ ์–ธ์–ด์˜ ๋ฌธ์žฅ ์Œ์ด ์žˆ๋Š” ํ•œ ์ž์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ๋งค์šฐ ์‰ฝ๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Dataset์„ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๋ณต์Šต์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ 5์žฅ์„ ๋‹ค์‹œ ์‚ดํŽด๋ณด์„ธ์š”. KDE4 ๋ฐ์ดํ„ฐ ์…‹ ์ด์ „๊ณผ ๊ฐ™์ด load_dataset() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค: from datasets import load_dataset, load_metric raw_datasets = load_dataset("kde4", lang1="en", lang2="fr") ๋‹ค๋ฅธ ์–ธ์–ด ์Œ์œผ๋กœ ์ž‘์—…ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ์›ํ•˜๋Š” ์–ธ์–ด ์ฝ”๋“œ๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ ์…‹์—์„œ๋Š” ์ด 92๊ฐœ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹ ์นด๋“œ์˜ ์–ธ์–ด ํƒœ๊ทธ๋ฅผ ํ™•์žฅํ•˜๋ฉด ๋ชจ๋‘ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œํ•œ ๋ฐ์ดํ„ฐ ์…‹์„ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. raw_datasets ์ด 210,173๊ฑด์˜ ๋ฌธ์žฅ ์Œ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์ง€๋งŒ ๋‹จ์ผ ๋ถ„ํ• ์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ ๊ฒ€์ฆ ์ง‘ํ•ฉ์„ ๋ณ„๋„๋กœ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 5์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด Dataset์—๋Š” ์ด ์ž‘์—…์„ ์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” train_test_split() ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: split_datasets = raw_datasets["train"].train_test_split(train_size=0.9, seed=20) split_datasets ๋‹ค์Œ๊ณผ ๊ฐ™์ด "test" ํ‚ค์˜ ์ด๋ฆ„์„ "validation"์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: split_datasets["validation"] = split_datasets.pop("test") ์ด์ œ ๋ฐ์ดํ„ฐ ์…‹์˜ ํ•œ ์š”์†Œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: split_datasets["train"][1]["translation"] ์œ„์™€ ๊ฐ™์ด ์šฐ๋ฆฌ๊ฐ€ ์š”๊ตฌํ•œ ์–ธ์–ด์Œ(์˜์–ด, ๋ถˆ์–ด)์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์žฅ๋“ค์ด ํฌํ•จ๋œ ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ๊ธฐ์ˆ ์ ์ธ ์ปดํ“จํ„ฐ ๊ณผํ•™ ์šฉ์–ด๋กœ ๊ฐ€๋“ ์ฐฌ ์ด ๋ฐ์ดํ„ฐ ์…‹์˜ ํ•œ ๊ฐ€์ง€ ํŠน์ง•์€ ๋ชจ๋‘ ํ”„๋ž‘์Šค์–ด๋กœ ์™„์ „ํžˆ ๋ฒˆ์—ญ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ”„๋ž‘์Šค ์—”์ง€๋‹ˆ์–ด๋“ค์€ ์ข…์ข… ๊ฒŒ์„๋Ÿฌ์„œ ๋งํ•  ๋•Œ ๋Œ€๋ถ€๋ถ„์˜ ์ปดํ“จํ„ฐ ๊ณผํ•™ ๊ด€๋ จ ๋‹จ์–ด๋ฅผ ์˜์–ด๋กœ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„ ์˜ˆ์‹œ์—์„œ "threads"๋ผ๋Š” ๋‹จ์–ด๋Š” ๋ถˆ์–ด ๋ฌธ์žฅ, ํŠนํžˆ ๊ธฐ์ˆ ์ ์ธ ๋Œ€ํ™”์—์„œ ๋งŽ์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ๋Š” ๋” "ํšŒ์˜๋ฅผ ์œ„ํ•œ ํ† ๋ก (fils de discussion)"์œผ๋กœ ์ •ํ™•ํ•˜๊ฒŒ ๋ฒˆ์—ญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทœ๋ชจ๊ฐ€ ๋” ํฐ ํ”„๋ž‘์Šค์–ด ๋ฐ ์˜์–ด ๋ฌธ์žฅ ๋ง๋ญ‰์น˜์—์„œ ์‚ฌ์ „ ํ•™์Šต๋œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ชจ๋ธ์€ ๋‹จ์–ด๋ฅผ ๋ฒˆ์—ญํ•˜์ง€ ์•Š๊ณ  ์žˆ๋Š” ๊ทธ๋Œ€๋กœ ๊ทธ๋ƒฅ ๋‘๋Š” ๋” ์‰ฌ์šด ์˜ต์…˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import pipeline model_checkpoint = "Helsinki-NLP/opus-mt-en-fr" translator = pipeline("translation", model=model_checkpoint) translator("Default to expanded threads") ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋Š” "plugin"์ด๋ผ๋Š” ๋‹จ์–ด์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‹จ์–ด๋Š” ๊ณต์‹์ ์œผ๋กœ ํ”„๋ž‘์Šค์–ด๊ฐ€ ์•„๋‹ˆ์ง€๋งŒ ๋Œ€๋ถ€๋ถ„์˜ ์›์–ด๋ฏผ์ด ์ดํ•ดํ•˜๊ณ  ๋ฒˆ์—ญํ•˜๋Š”๋ฐ ์–ด๋ ต์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. KDE4 ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ด ๋‹จ์–ด๋Š” ํ”„๋ž‘์Šค์–ด๋กœ ๋” ๊ณต์‹์ ์ธ "module d'extension"์œผ๋กœ ๋ฒˆ์—ญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: split_datasets["train"][172]["translation"] ๊ทธ๋Ÿฌ๋‚˜ ์œ„์˜ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์€ ๊ฐ„๊ฒฐํ•˜๊ณ  ์นœ์ˆ™ํ•œ ์˜์–ด ๋‹จ์–ด๋ฅผ ๊ณ ์ˆ˜ํ•ฉ๋‹ˆ๋‹ค: translator( "Unable to import %1 using the OFX importer plugin. This file is not the correct format." ) ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ์ด ๋ฐ์ดํ„ฐ ์…‹์˜ ์ด๋Ÿฌํ•œ ํŠน์„ฑ์„ ํก์ˆ˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์€ ํฅ๋ฏธ๋กœ์šธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. โœ Your turn! ํ”„๋ž‘์Šค์–ด์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋˜ ๋‹ค๋ฅธ ์˜์–ด ๋‹จ์–ด๋Š” "email"์ž…๋‹ˆ๋‹ค. ์ด ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•™์Šต ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ฐพ์•„๋ด…์‹œ๋‹ค. ์–ด๋–ป๊ฒŒ ๋ฒˆ์—ญ๋ฉ๋‹ˆ๊นŒ? ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์€ ๋™์ผํ•œ ์˜์–ด ๋ฌธ์žฅ์„ ์–ด๋–ป๊ฒŒ ๋ฒˆ์—ญํ•ฉ๋‹ˆ๊นŒ? ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ง€๊ธˆ์ฏค์ด๋ฉด ๊ธฐ๋ณธ์ ์ธ ์ ˆ์ฐจ๋ฅผ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ํ…์ŠคํŠธ๋Š” ๋ชจ๋ธ์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ† ํฐ ID ์„ธํŠธ๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ์œ„ํ•ด ์ž…๋ ฅ๊ณผ ํƒ€๊นƒ์„ ๋ชจ๋‘ ํ† ํฐ ํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ž‘์—…์€ tokenizer ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด Marian English to French ์‚ฌ์ „ ํ•™์Šต ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์–ธ์–ด ์Œ์— ๋Œ€ํ•ด์„œ ์ž‘์—…ํ•˜๋ ค๋ฉด ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Helsinki-NLP ์กฐ์ง์€ ์—ฌ๋Ÿฌ ์–ธ์–ด๋กœ ์ฒœ ๊ฐœ ์ด์ƒ์˜ ๋ชจ๋ธ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. from transformers import AutoTokenizer model_checkpoint = "Helsinki-NLP/opus-mt-en-fr" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, return_tensors="pt") Hub์—์„œ ์„ ํ˜ธํ•˜๋Š” ๋‹ค๋ฅธ ๋ชจ๋ธ๋กœ model_checkpoint๋ฅผ ๋ฐ”๊พธ๊ฑฐ๋‚˜ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•œ ๋กœ์ปฌ ํด๋”๋กœ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. mBART, mBART-50 ๋˜๋Š” M2M100๊ณผ ๊ฐ™์€ ๋‹ค๊ตญ์–ด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ tokenizer.src_lang ๋ฐ tokenizer.tgt_lang์„ ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ํ† ํฌ ๋‚˜์ด์ €์—์„œ ์ž…๋ ฅ ๋ฐ ๋Œ€์ƒ์˜ ์–ธ์–ด ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ค€๋น„ ๊ณผ์ •์€ ๋งค์šฐ ์ง๊ด€์ ์ž…๋‹ˆ๋‹ค. ๊ธฐ์–ตํ•ด์•ผ ํ•  ๊ฒƒ์ด ํ•˜๋‚˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‰์†Œ์™€ ๊ฐ™์ด ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜์ง€๋งŒ ํƒ€๊นƒ(target)์˜ ๊ฒฝ์šฐ ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž ๋‚ด๋ถ€์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ as_target_tokenizer()๋กœ ๋ž˜ํ•‘ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Python์˜ ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž๋Š” with ๋ฌธ๊ณผ ํ•จ๊ป˜ ๋„์ž…๋˜์—ˆ์œผ๋ฉฐ ๋‘ ๊ฐœ์˜ ๊ด€๋ จ ์ž‘์—…์„ ์Œ์œผ๋กœ ์‹คํ–‰ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์˜ˆ๋Š” ํŒŒ์ผ์„ ์“ฐ๊ฑฐ๋‚˜ ์ฝ์„ ๋•Œ์ด๋ฉฐ, ์ด๋Š” ์ข…์ข… ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช…๋ น์–ด ๋‚ด์—์„œ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค: # with open(file_path) as f: # content = f.read() ์—ฌ๊ธฐ์„œ ์Œ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ๋‘ ๊ฐ€์ง€ ๊ด€๋ จ ์ž‘์—…์€ ํŒŒ์ผ์„ ์—ด๊ณ  ๋‹ซ๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์—ด๋ฆฐ ํŒŒ์ผ f์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด๋Š” with ์•„๋ž˜์˜ ๋“ค์—ฌ ์“ฐ๊ธฐ ๋œ ๋ธ”๋ก ๋‚ด๋ถ€์—๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์—ด๊ธฐ๋Š” ํ•ด๋‹น ๋ธ”๋ก๋ณด๋‹ค ๋จผ์ € ๋ฐœ์ƒํ•˜๊ณ  ๋ธ”๋ก ๋์—์„œ ๋‹ซ๊ธฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ์„ ์ „์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: en_sentence = split_datasets["train"][1]["translation"]["en"] fr_sentence = split_datasets["train"][1]["translation"]["fr"] inputs = tokenizer(en_sentence) with tokenizer.as_target_tokenizer(): targets = tokenizer(fr_sentence) ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž ๋‚ด์—์„œ ๋Œ€์ƒ์„ ํ† ํฐํ™”ํ•˜๋Š” ๊ฒƒ์„ ์žŠ์–ด๋ฒ„๋ฆฌ๋ฉด ์ž…๋ ฅ ํ† ํฌ ๋‚˜์ด์ €์— ์˜ํ•ด ํƒ€๊นƒ(target)์ด ํ† ํฐํ™”๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ Marian ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ์—๋Š” ํ† ํฌ๋‚˜์ด์ง•์ด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ๊ฒ๋‹ˆ๋‹ค: wrong_targets = tokenizer(fr_sentence) print(tokenizer.convert_ids_to_tokens(wrong_targets["input_ids"])) print(tokenizer.convert_ids_to_tokens(targets["input_ids"])) ์šฐ๋ฆฌ๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด, ์˜์–ด ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ์„ ์ „์ฒ˜๋ฆฌํ•˜๋ฉด ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ํ›จ์”ฌ ๋” ๋งŽ์€ ํ† ํฐ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. inputs๊ณผ targets์€ ๋ชจ๋‘ ์ผ๋ฐ˜์ ์ธ ํ‚ค(input IDs, attention mask ๋“ฑ)๊ฐ€ ์žˆ๋Š” ๋”•์…”๋„ˆ๋ฆฌ์ด๋ฏ€๋กœ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” ์ž…๋ ฅ ๋‚ด๋ถ€์— "labels" ํ‚ค๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ๋ฐ์ดํ„ฐ ์…‹์— ์ ์šฉํ•  ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜์—์„œ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค: max_input_length = 128 max_target_length = 128 def preprocess_function(examples): inputs = [ex["en"] for ex in examples["translation"]] targets = [ex["fr"] for ex in examples["translation"]] model_inputs = tokenizer(inputs, max_length=max_input_length, truncation=True) # ํƒ€๊นƒ์„ ์œ„ํ•œ ํ† ํฌ ๋‚˜์ด์ € ์…‹์—… with tokenizer.as_target_tokenizer(): labels = tokenizer(targets, max_length=max_target_length, truncation=True) model_inputs["labels"] = labels["input_ids"] return model_inputs ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ์— ๋Œ€ํ•ด ๋™์ผํ•œ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋‹ค๋ฃจ๊ณ  ์žˆ๋Š” ํ…์ŠคํŠธ๋Š” ๋งค์šฐ ์งง๊ธฐ ๋•Œ๋ฌธ์— 128์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. T5 ๋ชจ๋ธ(๋” ๊ตฌ์ฒด์ ์œผ๋กœ t5-xxx ์ฒดํฌํฌ์ธํŠธ ์ค‘ ํ•˜๋‚˜)์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๋ชจ๋ธ์€ ํ…์ŠคํŠธ ์ž…๋ ฅ์— translate: English to French:์™€ ๊ฐ™์ด ๋‹น๋ฉดํ•œ ์ž‘์—…์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ ‘๋‘์‚ฌ๊ฐ€ ์žˆ์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค. โš  ํƒ€๊นƒ์˜ ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋Š” ๋ชจ๋ธ์ด ์˜ˆ์ƒํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  ํŒจ๋”ฉ ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ ˆ์ด๋ธ”์„ -100์œผ๋กœ ์„ค์ •ํ•ด์•ผ ์†์‹ค ๊ณ„์‚ฐ์—์„œ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ๋™์  ํŒจ๋”ฉ์„ ์ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜์ค‘์— ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ์— ์˜ํ•ด ์ˆ˜ํ–‰๋˜์ง€๋งŒ ์—ฌ๊ธฐ์—์„œ ํŒจ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ํŒจ๋”ฉ ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ๋ ˆ์ด๋ธ”์„ -100์œผ๋กœ ์„ค์ •ํ•˜๋„๋ก ์ „์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์กฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ชจ๋“  ๋ถ„ํ• ์— ๋Œ€ํ•ด ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•œ ๋ฒˆ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: tokenized_datasets = split_datasets.map( preprocess_function, batched=True, remove_columns=split_datasets["train"].column_names, ) ์ด์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „์ฒ˜๋ฆฌ๋˜์—ˆ์œผ๋ฏ€๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! Trainer API๋กœ ๋ชจ๋ธ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ Trainer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‹ค์ œ ์ฝ”๋“œ๋Š” ์•ฝ๊ฐ„๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ์ด์ „๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” Seq2SeqTrainer๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Seq2SeqTrainer๋Š” Trainer์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋กœ์„œ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์ถœ๋ ฅ์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด generate() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‰๊ฐ€๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ๋ฉ”ํŠธ๋ฆญ(metric) ๊ณ„์‚ฐ์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•  ๋•Œ ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๋ฏธ์„ธ ์กฐ์ •ํ•  ์‹ค์ œ ๋ชจ๋ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ AutoModel API๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoModelForSeq2SeqLM model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint) ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ๋ฒˆ์—ญ ์ž‘์—…์— ๋Œ€ํ•ด์„œ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์ด๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๋ˆ„๋ฝ๋œ ๊ฐ€์ค‘์น˜ ๋˜๋Š” ์ƒˆ๋กœ ์ดˆ๊ธฐํ™”๋œ ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•œ ๊ฒฝ๊ณ ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ฝœ ๋ ˆ์ด์…˜ (Data Collation) ๋™์  ๋ฐฐ์น˜ ์ฒ˜๋ฆฌ(Dynamic batching)๋ฅผ ์œ„ํ•œ ํŒจ๋”ฉ์„ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ 3์žฅ์—์„œ์™€ ๊ฐ™์ด DataCollatorWithPadding์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ด ๋ฉ”์„œ๋“œ๋Š” ์ž…๋ ฅ(input IDs, attention mask ๋ฐ token type IDs)์— ๋Œ€ํ•ด์„œ๋งŒ ํŒจ๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ” ์—ญ์‹œ ๋ ˆ์ด๋ธ”์— ์žˆ๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ฑ„์›Œ์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ๋ ˆ์ด๋ธ”์„ ์ฑ„์šฐ๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํŒจ๋”ฉ ๊ฐ’์€ ํ† ํฌ ๋‚˜์ด์ €์˜ ํŒจ๋”ฉ ํ† ํฐ์ด ์•„๋‹ˆ๋ผ -100์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ ํŒจ๋”ฉ ๋œ ๊ฐ’์ด ์†์‹ค ๊ณ„์‚ฐ์—์„œ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ๋ชจ๋‘ DataCollatorForSeq2Seq์— ์˜ํ•ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. DataCollatorWithPadding๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, DataCollatorForSeq2Seq๋Š” ์ž…๋ ฅ์„ ์ „์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” tokenizer๋Š” ๋ฌผ๋ก  ๋ชจ๋ธ ์ž์ฒด๋„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ๋„ ์ž…๋ ฅ๋ฐ›๋Š” ์ด์œ ๋Š” ์ด ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๊ฐ€ ์‹œ์ž‘ ๋ถ€๋ถ„์— ํŠน์ˆ˜ ํ† ํฐ์ด ๋ถ™์–ด ์žˆ๋Š”, ๋ ˆ์ด๋ธ” ์‹œํ€€์Šค๋ฅผ ์šฐ์ธก์œผ๋กœ ์‹œํ”„ํŠธ(shift) ํ•œ ๋ฒ„์ „์ธ ๋””์ฝ”๋” input IDs๋ฅผ ์ค€๋น„ํ•˜๋Š” ์—ญํ• ๋„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ์‹œํ”„ํŠธ(shift) ๋ฐฉ๋ฒ•์€ ์•„ํ‚คํ…์ฒ˜๋งˆ๋‹ค ์•ฝ๊ฐ„์”ฉ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— DataCollatorForSeq2Seq๋Š” ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. from transformers import DataCollatorForSeq2Seq data_collator = DataCollatorForSeq2Seq(tokenizer, model=model) ๋ช‡ ๊ฐœ์˜ ์ƒ˜ํ”Œ์—์„œ ์ด๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ์ด ์ฝœ๋ ˆ ์ดํ„ฐ์— ํ† ํฐํ™”๋œ ํ•™์Šต ์ง‘ํ•ฉ์˜ ์˜ˆ์ œ ๋ชฉ๋ก์„ ์ž…๋ ฅํ•ด ๋ด…์‹œ๋‹ค: batch = data_collator([tokenized_datasets["train"][i] for i in range(1, 3)]) batch.keys() -100์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ ˆ์ด๋ธ”์ด ๋ฐฐ์น˜์˜ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ฑ„์›Œ์กŒ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: batch["labels"] ๋˜ํ•œ decoder_input_ids๋ฅผ ์‚ดํŽด๋ณด๊ณ  labels์— ์ €์žฅ๋œ ํ† ํฐ ๋ฆฌ์ŠคํŠธ์˜ ์‹œํ”„ํŠธ(shift) ๋ฒ„์ „์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: batch["decoder_input_ids"] ๋‹ค์Œ์€ ๋ฐ์ดํ„ฐ ์…‹์˜ ์ฒซ ๋ฒˆ์งธ ๋ฐ ๋‘ ๋ฒˆ์งธ ์š”์†Œ์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ”์ž…๋‹ˆ๋‹ค: for i in range(1, 3): print(tokenized_datasets["train"][i]["labels"]) ์ด data_collator๋ฅผ Seq2SeqTrainer์— ์ „๋‹ฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ํ‰๊ฐ€ ์ง€ํ‘œ(metric)๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€ ์ง€ํ‘œ (Metrics) Seq2SeqTrainer๊ฐ€ ์Šˆํผํด๋ž˜์Šค Trainer์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ์€ ํ‰๊ฐ€(evaluation) ๋˜๋Š” ์˜ˆ์ธก(prediction) ์ค‘์— generate() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ํ•™์Šตํ•˜๋Š” ๋™์•ˆ ๋ชจ๋ธ์€ ํ•™์Šต ์†๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด, ์˜ˆ์ธกํ•˜๋ ค๋Š” ํ† ํฐ ์ดํ›„์— ์กด์žฌํ•˜๋Š” ํ† ํฐ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋ ค๊ณ  ์–ดํ…์…˜ ๋งˆ์Šคํ‚น๊ณผ ํ•จ๊ป˜ decoder_input_ids๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ถ”๋ก ํ•˜๋Š” ๋™์•ˆ์—๋Š” ๋ ˆ์ด๋ธ”์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋™์ผํ•œ ์„ค์ •์œผ๋กœ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. 1์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ๋””์ฝ”๋”๋Š” ํ† ํฐ์„ ํ•˜๋‚˜์”ฉ ์˜ˆ์ธกํ•˜์—ฌ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” Transformers์—์„œ generate() ๋ฉ”์„œ๋“œ์— ์˜ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค. predict_with_generate=True๋กœ ์„ค์ •ํ•˜๋ฉด Seq2SeqTrainer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ชจ๋ธ ํ‰๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒˆ์—ญ์— ์‚ฌ์šฉ๋˜๋Š” ์ „ํ†ต์ ์ธ ํ‰๊ฐ€ ์ง€ํ‘œ(metric)๋Š” BLEU ์Šค์ฝ”์–ด๋กœ์„œ Kishore Papineni et al.์— ์˜ํ•ด 2002๋…„ ๋…ผ๋ฌธ์—์„œ ์†Œ๊ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. BLEU ์ ์ˆ˜๋Š” ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๊ฐ€ ๋ ˆ์ด๋ธ”์— ์–ผ๋งˆ๋‚˜ ๊ฐ€๊นŒ์šด์ง€๋ฅผ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์—์„œ ์ƒ์„ฑ๋œ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ์˜ ๋ช…๋ฃŒ์„ฑ ๋˜๋Š” ๋ฌธ๋ฒ•์  ์ •ํ™•์„ฑ์„ ์ธก์ •ํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๋‹จ์ง€ ํ†ต๊ณ„ ๊ทœ์น™์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ๋œ ์ถœ๋ ฅ ๋‚ด์˜ ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ํƒ€๊นƒ(target)์—๋„ ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ํƒ€๊นƒ(target)์—๋Š” ๋ฐ˜๋ณต๋˜์ง€ ์•Š์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋ฒˆ์—ญ ๊ฒฐ๊ณผ์—์„œ ๋™์ผํ•œ ๋‹จ์–ด๊ฐ€ ๊ณ„์† ๋ฐ˜๋ณต๋  ๊ฒฝ์šฐ์—๋„ ํŽ˜๋„ํ‹ฐ๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด "the the the the the"์™€ ๊ฐ™์€ ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํƒ€๊นƒ(target)๋ณด๋‹ค ์งง์€ ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์—๋„ ํŽ˜๋„ํ‹ฐ๋ฅผ ๋ถ€๊ณผํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” "the"์™€ ๊ฐ™์€ ๋ฌธ์žฅ์ด ์ถœ๋ ฅ๋˜๋Š” ๋ชจ๋ธ์„ ๋ฏธ์—ฐ์— ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค. BLEU ์ ์ˆ˜์˜ ํ•œ ๊ฐ€์ง€ ์•ฝ์ ์€ ์ด๋ฏธ ํ† ํฐํ™”๋œ ํ…์ŠคํŠธ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ ๊ฐ„์˜ ์ ์ˆ˜๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ค๋Š˜๋‚  ๋ฒˆ์—ญ ๋ชจ๋ธ์„ ๋ฒค์น˜๋งˆํ‚นํ•˜๋Š”๋ฐ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ‰๊ฐ€ ์ง€ํ‘œ(metric)๋Š” ํ† ํฐํ™” ๋‹จ๊ณ„๋ฅผ ํ‘œ์ค€ํ™”ํ•˜์—ฌ ์ด๋Ÿฌํ•œ ์•ฝ์ ์„ ํ•ด๊ฒฐํ•˜๋Š” SacreBLEU์ž…๋‹ˆ๋‹ค. ์ด ํ‰๊ฐ€ ์ง€ํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋จผ์ € SacreBLEU ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: !python3 -m pip install sacrebleu ๊ทธ๋Ÿฐ ๋‹ค์Œ 3์žฅ์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ load_metric()์„ ํ†ตํ•ด ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from datasets import load_metric metric = load_metric("sacrebleu") ์ด ๋ฉ”ํŠธ๋ฆญ์€ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ(inputs) ๋ฐ ํƒ€๊นƒ(targets)์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด ํ—ˆ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ ํ—ˆ์šฉ ๊ฐ€๋Šฅํ•œ ๋Œ€์ƒ์„ ์ž…๋ ฅ๋ฐ›๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ ์…‹์€ ํ•˜๋‚˜๋งŒ ์ œ๊ณตํ•˜์ง€๋งŒ NLP์—์„œ ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ๋ ˆ์ด๋ธ”๋กœ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ ์…‹๋“ค๋„ ๋งŽ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ˆ์ธก(predictions)์€ ๋ฌธ์žฅ ๋ฆฌ์ŠคํŠธ์—ฌ์•ผ ํ•˜์ง€๋งŒ ์ •๋‹ต(references)์€ ๋ฌธ์žฅ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ํ•œ๋ฒˆ ๋ด…์‹œ๋‹ค: predictions = [ "This plugin lets you translate web pages between several languages automatically." ] references = [ [ "This plugin allows you to automatically translate web pages between several languages." ] ] metric.compute(predictions=predictions, references=references) BLEU ์ ์ˆ˜๊ฐ€ 46.75๋กœ ๋‚˜์™”๊ณ  ๊ทธ ์ ์ˆ˜๊ฐ€ ๋‚˜์˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ "Attention Is All You Need" ๋…ผ๋ฌธ์˜ ์›๋ž˜ ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ๋ชจ๋ธ์€ ์˜์–ด์™€ ํ”„๋ž‘์Šค์–ด ๊ฐ„์˜ ๋น„์Šทํ•œ ๋ฒˆ์—ญ ์ž‘์—…์—์„œ BLEU ์ ์ˆ˜ 41.8์„ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค! counts ๋ฐ bp์™€ ๊ฐ™์€ ๊ฐœ๋ณ„ ํ‰๊ฐ€ ์ง€ํ‘œ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ SacreBLEU ์ €์žฅ์†Œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ๋ฐ˜๋ฉด์—, ๋ฒˆ์—ญ ๋ชจ๋ธ์—์„œ ์ž์ฃผ ๋‚˜์˜ค๋Š” ๋‘ ๊ฐ€์ง€ ์ž˜๋ชป๋œ ์œ ํ˜•์˜ ์˜ˆ์ธก(๋ฐ˜๋ณต์ด ๋งŽ๊ฑฐ๋‚˜ ๋„ˆ๋ฌด ์งง์Œ)์œผ๋กœ ์‹œ๋„ํ•˜๋ฉด ๋‹ค์†Œ ๋‚˜์œ BLEU ์ ์ˆ˜๋ฅผ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค: predictions = ["This This This This"] references = [ [ "This plugin allows you to automatically translate web pages between several languages." ] ] metric.compute(predictions=predictions, references=references) predictions = ["This plugin"] references = [ [ "This plugin allows you to automatically translate web pages between several languages." ] ] metric.compute(predictions=predictions, references=references) ์ ์ˆ˜๋Š” 0์—์„œ 100๊นŒ์ง€ ๊ณ„์‚ฐ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋†’์„์ˆ˜๋ก ์ข‹์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ ์ถœ๋ ฅ์—์„œ ํ‰๊ฐ€ ์ง€ํ‘œ(metric)๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ…์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ tokenizer.batch_decode() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”(์ •๋‹ต)์—์„œ๋Š” ๋ชจ๋“  -100์„ ์ œ๊ฑฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค(ํ† ํฌ ๋‚˜์ด์ €๋Š” ํŒจ๋”ฉ ํ† ํฐ์— ๋Œ€ํ•ด ์ž๋™์œผ๋กœ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•จ): import numpy as np def compute_metrics(eval_preds): preds, labels = eval_preds # ๋ชจ๋ธ์ด ์˜ˆ์ธก ๋กœ์ง“(logits)์™ธ์— ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒฝ์šฐ. if isinstance(preds, tuple): preds = preds[0] decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True) # -100์€ ๊ฑด๋„ˆ๋›ด๋‹ค. labels = np.where(labels != -100, labels, tokenizer.pad_token_id) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) # ๋‹จ์ˆœ ํ›„์ฒ˜๋ฆฌ decoded_preds = [pred.strip() for pred in decoded_preds] decoded_labels = [[label.strip()] for label in decoded_labels] result = metric.compute(predictions=decoded_preds, references=decoded_labels) return {"bleu": result["score"]} ์ด์ œ ์ด ์ž‘์—…์ด ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฏ€๋กœ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๊ธฐ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” Hugging Face์— ๋กœ๊ทธ์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋กœ์จ ๊ฒฐ๊ณผ๋ฅผ Model Hub์— ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—๋Š” ์ด๋ฅผ ๋„์™€์ฃผ๋Š” ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค: from huggingface_hub import notebook_login notebook_login() ๊ทธ๋Ÿฌ๋ฉด Hugging Face ๋กœ๊ทธ์ธ ์ž๊ฒฉ ์ฆ๋ช…์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์ ฏ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋…ธํŠธ๋ถ์—์„œ ์ž‘์—…ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ํ„ฐ๋ฏธ๋„์— ๋‹ค์Œ ์ค„์„ ์ž…๋ ฅํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: huggingface-cli login ์ด ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด Seq2SeqTrainingArguments๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Trainer์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ช‡ ๊ฐ€์ง€ ํ•„๋“œ๊ฐ€ ๋” ํฌํ•จ๋œ TrainingArguments์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import Seq2SeqTrainingArguments args = Seq2SeqTrainingArguments( f"marian-finetuned-kde4-en-to-fr", evaluation_strategy="no", save_strategy="epoch", learning_rate=2e-5, per_device_train_batch_size=32, per_device_eval_batch_size=64, weight_decay=0.01, save_total_limit=3, num_train_epochs=3, predict_with_generate=True, fp16=True, push_to_hub=True, ) ์ผ๋ฐ˜์ ์ธ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ(์˜ˆ: ํ•™์Šต๋ฅ , ์—ํฌํฌ ์ˆ˜, ๋ฐฐ์น˜ ํฌ๊ธฐ ๋ฐ ์ผ๋ถ€ ๊ฐ€์ค‘์น˜ ๊ฐ์†Œ)๋ฅผ ์ œ์™ธํ•˜๊ณ , ๋‹ค์Œ์€ ์ด์ „ ์„น์…˜์—์„œ ๋ณธ ๊ฒƒ๊ณผ ๋น„๊ตํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค: ํ‰๊ฐ€์— ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋ฏ€๋กœ ์ •๊ธฐ์ ์ธ ํ‰๊ฐ€๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ•™์Šต ์ „๊ณผ ํ›„์— ๋ชจ๋ธ์„ ํ•œ ๋ฒˆ๋งŒ ํ‰๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. fp16=True๋กœ ์„ค์ •ํ•˜์—ฌ ์ตœ์‹  GPU์—์„œ ํ•™์Šต ์†๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ predict_with_generate=True๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. push_to_hub=True๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์—ํฌํฌ(epoch)๊ฐ€ ๋๋‚  ๋•Œ Hub์— ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. hub_model_id ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘ธ์‹œ ํ•˜๋ ค๋Š” ์ €์žฅ์†Œ์˜ ์ „์ฒด ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(ํŠนํžˆ, ์กฐ์ง์— ํ‘ธ์‹œ ํ•˜๋ ค๋ฉด ์ด ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ). ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจ๋ธ์„ huggingface-course ์กฐ์ง์— ํ‘ธ์‹œ ํ•  ๋•Œ hub_model_id="huggingface-course/marian-finetuned-kde4-en-to-fr"์„ Seq2SeqTrainingArguments์— ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ €์žฅ์†Œ๋Š” ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์กด์žฌํ•˜๊ณ  ์„ค์ •ํ•œ ์ถœ๋ ฅ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ๋ช…๋ช…๋˜๋ฏ€๋กœ ์ด ๊ฒฝ์šฐ์—๋Š” "spasis/marian-finetuned-kde4-en-to-fr"(์šฐ๋ฆฌ๊ฐ€ ์ด ์„น์…˜์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ๋งํฌํ•œ ๋ชจ๋ธ)์ด ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ์ถœ๋ ฅ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ํ‘ธ์‹œ ํ•˜๋ ค๋Š” ์ €์žฅ์†Œ์˜ ๋กœ์ปฌ ํด๋ก ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ Seq2SeqTrainer๋ฅผ ์ •์˜ํ•  ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ์ด๋ฆ„์„ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋“  ๊ฒƒ์„ Seq2SeqTrainer์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. from transformers import Seq2SeqTrainer trainer = Seq2SeqTrainer( model, args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, tokenizer=tokenizer, compute_metrics=compute_metrics, ) ํ•™์Šตํ•˜๊ธฐ ์ „์— ๋จผ์ € ์ดˆ๊ธฐ ๋ชจ๋ธ์ด ์–ป๋Š” ์ ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด๊ณ , ๋ฏธ์„ธ ์กฐ์ •์œผ๋กœ ์ƒํ™ฉ์„ ์•…ํ™”์‹œํ‚ค๊ณ  ์žˆ์ง€ ์•Š์€์ง€ ๋‹ค์‹œ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ช…๋ น์€ ๋‹ค์†Œ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค: trainer.evaluate(max_length=max_target_length) BLEU ์ ์ˆ˜ 39๋Š” ๊ทธ๋ฆฌ ๋‚˜์˜์ง€ ์•Š์€๋ฐ, ์ด๋Š” ์šฐ๋ฆฌ๊ฐ€ ์„ ํƒํ•œ ๋ชจ๋ธ์ด ์ด๋ฏธ ์˜์–ด ๋ฌธ์žฅ์„ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ์œผ๋กœ ๋ฒˆ์—ญํ•˜๋Š”๋ฐ ํšจ๊ณผ์ ์ด๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ๋ณธ๊ฒฉ์ ์œผ๋กœ ํ•™์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ญ์‹œ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค: trainer.train() ํ•™์Šต์ด ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ ๋ชจ๋ธ์ด ์ €์žฅ๋  ๋•Œ๋งˆ๋‹ค(์—ฌ๊ธฐ์„œ๋Š” ๋ชจ๋“  ์—ํฌํฌ๋งˆ๋‹ค) ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ๋ชจ๋ธ์ด ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ๋จธ์‹ ์—์„œ ํ•™์Šต์„ ์žฌ๊ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์ด ์™„๋ฃŒ๋˜๋ฉด ๋ชจ๋ธ์„ ๋‹ค์‹œ ํ‰๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. BLEU ์ ์ˆ˜๊ฐ€ ๊ฐœ์„ ๋˜์—ˆ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค! trainer.evaluate(max_length=max_target_length) ๊ฑฐ์˜ 14ํฌ์ธํŠธ ๊ฐœ์„ ๋˜์—ˆ๋„ค์š”. ์ข‹์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, push_to_hub() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์‹  ๋ฒ„์ „์˜ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ–ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. Trainer๋Š” ๋ชจ๋“  ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ํฌํ•จ๋œ ๋ชจ๋ธ ์นด๋“œ์˜ ์ดˆ์•ˆ์„ ์ž‘์„ฑํ•˜์—ฌ ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ ์นด๋“œ์—๋Š” Model Hub๊ฐ€ ์ถ”๋ก  ๋ฐ๋ชจ์šฉ ์œ„์ ฏ์„ ์„ ํƒํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋ธ ํด๋ž˜์Šค์—์„œ ์˜ฌ๋ฐ”๋ฅธ ์œ„์ ฏ์„ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์•„๋ฌด๊ฒƒ๋„ ํ•  ํ•„์š”๊ฐ€ ์—†์ง€๋งŒ, ์ด ๊ฒฝ์šฐ ๋™์ผํ•œ ๋ชจ๋ธ ํด๋ž˜์Šค๋ฅผ ๋ชจ๋“  ์ข…๋ฅ˜์˜ sequence-to-sequence ๋ฌธ์ œ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ฒˆ์—ญ ๋ชจ๋ธ์ด๋ผ๊ณ  ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค: trainer.push_to_hub(tags="tanslation", commit_message="Training complete") ์œ„ ๋ช…๋ น์€ ๋ฐฉ๊ธˆ ์ˆ˜ํ–‰ํ•œ ์ปค๋ฐ‹์˜ URL์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. max_target_length ์„œ Model Hub์˜ ์ถ”๋ก  ์œ„์ ฏ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•˜๊ณ  ์นœ๊ตฌ์™€ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒˆ์—ญ ์ž‘์—…์„ ์œ„ํ•ด์„œ ๋ชจ๋ธ์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! ํ•™์Šต ๋ฃจํ”„์— ๋Œ€ํ•ด ์ข€ ๋” ์ž์„ธํžˆ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์ด์ œ Accelerate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งž์ถคํ˜• ํ•™์Šต ๋ฃจํ”„ (Custom Training Loop) ์ด์ œ ์ „์ฒด ํ•™์Šต ๋ฃจํ”„(full training loop)๋ฅผ ์‚ดํŽด๋ณด๊ณ  ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ์‰ฝ๊ฒŒ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 7.2์™€ 7.3์—์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•™์Šต์„ ์œ„ํ•œ ๋ชจ๋“  ์‚ฌํ•ญ ์ค€๋น„ํ•˜๊ธฐ ์•„๋ž˜ ๋‚ด์šฉ์€ ์ด๋ฏธ ๋ช‡ ๋ฒˆ์”ฉ ๊ณต๋ถ€ํ–ˆ์œผ๋ฏ€๋กœ ์ฝ”๋“œ๋ฅผ ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๋ฐ์ดํ„ฐ ์…‹์„ "torch"<NAME>์œผ๋กœ ์„ค์ •ํ•œ ํ›„ ๋ฐ์ดํ„ฐ ์…‹์—์„œ DataLoader๋ฅผ ๋นŒ๋“œ ํ•˜์—ฌ PyTorch ํ…์„œ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค: from torch.utils.data import DataLoader tokenized_datasets.set_format("torch") train_dataloader = DataLoader( tokenized_datasets["train"], shuffle=True, collate_fn=data_collator, batch_size=8, ) eval_dataloader = DataLoader( tokenized_datasets["validation"], collate_fn=data_collator, batch_size=8 ) ๋‹ค์Œ์œผ๋กœ ์ด์ „์˜ ๋ฏธ์„ธ ์กฐ์ •์„ ๊ณ„์†ํ•ด์„œ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์—์„œ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋„๋ก ๋ชจ๋ธ์„ ๋‹ค์‹œ ์ธ์Šคํ„ด์Šคํ™”ํ•ฉ๋‹ˆ๋‹ค: model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint) ์ด์ œ ์˜ตํ‹ฐ๋งˆ์ด์ €(optimizer)๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค: from transformers import AdamW optimizer = AdamW(model.parameters(), lr=2e-5) ์ง€๊ธˆ๊นŒ์ง€ ์ค€๋น„๋œ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ Accelerator.prepare() ๋ฉ”์„œ๋“œ์— ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Colab ๋…ธํŠธ๋ถ์—์„œ TPU๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•™์Šตํ•˜๋ ค๋ฉด ์ด ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ํ•™์Šต ํ•จ์ˆ˜๋กœ ์ด๋™ํ•ด์•ผ ํ•˜๋ฉฐ Accelerator๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ์…€์„ ์‹คํ–‰ํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. from accelerate import Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader ) ์ด์ œ train_dataloader๋ฅผ accelerator.prepare()๋กœ ๋ณด๋ƒˆ์œผ๋ฏ€๋กœ ๊ธธ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ๋‹จ๊ณ„ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. accelerator.prepare()๊ฐ€ DataLoader์˜ ๊ธธ์ด๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฏ€๋กœ ํ•ญ์ƒ dataloader๋ฅผ ์ค€๋น„ํ•œ ํ›„์— ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ง€์ •๋œ learning rate์—์„œ 0์œผ๋กœ ๋ณ€ํ™”๋˜๋Š” ์ „ํ†ต์ ์ธ ์„ ํ˜• ํ•™์Šต ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import get_scheduler num_train_epochs = 3 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋ธ์„ Hub๋กœ ํ‘ธ์‹œ ํ•˜๋ ค๋ฉด ์ž‘์—… ํด๋”์— Repository ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„์ง ๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋จผ์ € Hugging Face Hub์— ๋กœ๊ทธ์ธํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์— ๋ถ€์—ฌํ•˜๋ ค๋Š” ๋ชจ๋ธ ID๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ €์žฅ์†Œ ์ด๋ฆ„์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. repo_name์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ์ž์œ ๋กญ๊ฒŒ ์ง€์ •ํ•ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ด๋ฆ„๋งŒ ํฌํ•จํ•˜๋ฉด get_full_repo_name()์ด ์•Œ์•„์„œ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค: from huggingface_hub import Repository, get_full_repo_name model_name = "marian-finetuned-kde4-en-to-fr-accelerate" repo_name = get_full_repo_name(model_name) repo_name ๊ทธ๋Ÿฐ ๋‹ค์Œ ํ•ด๋‹น ์ €์žฅ์†Œ๋ฅผ ๋กœ์ปฌ ํด๋”์— ๋ณต์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์ด ๋กœ์ปฌ ํด๋”๋Š” ์ž‘์—… ์ค‘์ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๋ณต์ œ๋ณธ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: output_dir = "marian-finetuned-kde4-en-to-fr-accelerate" repo = Repository(output_dir, clone_from=repo_name) ์ด์ œ repo.push_to_hub() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ output_dir์— ์ €์žฅํ•œ ๋ชจ๋“  ๊ฒƒ์„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ ์ค‘๊ฐ„ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๋ฃจํ”„ (Training loop) ์ด์ œ ์ „์ฒด ํ•™์Šต ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ถ€๋ถ„์„ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์˜ˆ์ธก๊ณผ ๋ ˆ์ด๋ธ”์„ ๊ฐ€์ ธ์™€ ๋ฉ”ํŠธ๋ฆญ ๊ฐ์ฒด๊ฐ€ ์˜ˆ์ƒํ•˜๋Š” ๋ฌธ์ž์—ด ๋ชฉ๋ก์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” postprocess() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค: def postprocess(predictions, labels): predictions = predictions.cpu().numpy() labels = labels.cpu().numpy() decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True) # Replace -100 in the labels as we can't decode them labels = np.where(labels != -100, labels, tokenizer.pad_token_id) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) # Some simple post-processing decoded_preds = [pred.strip() for pred in decoded_preds] decoded_labels = [[label.strip()] for label in decoded_labels] return decoded_preds, decoded_labels ํ•™์Šต ๋ฃจํ”„๋Š” ํ‰๊ฐ€ ๋ถ€๋ถ„์—์„œ ๋ช‡ ๊ฐ€์ง€ ์ฐจ์ด์ ์„ ์ œ์™ธํ•˜๊ณ  ์„น์…˜ 2 ๋ฐ 3์˜ ๋ฃจํ”„์™€ ๋งค์šฐ ์œ ์‚ฌํ•˜๋ฏ€๋กœ ์ด ์ฐจ์ด์ ์— ์ดˆ์ ์„ ๋งž์ถ”๊ฒ ์Šต๋‹ˆ๋‹ค! ๊ฐ€์žฅ ๋จผ์ € ์ฃผ๋ชฉํ•ด์•ผ ํ•  ์ ์€ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด generate() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒƒ์€ ๊ธฐ๋ณธ ๋ชจ๋ธ์˜ ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. Accelerate๊ฐ€ prepare() ๋ฉ”์„œ๋“œ์—์„œ ์ƒ์„ฑํ•œ ๋ž˜ํ•‘ ๋œ ๋ชจ๋ธ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋จผ์ € ๋ชจ๋ธ์˜ ๋ž˜ํ•‘ ๋ถ€๋ถ„์„ ํ’€๊ณ  ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ฐจ์ด์ ์€ ํ† ํฐ ๋ถ„๋ฅ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‘ ๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ž…๋ ฅ๊ณผ ๋ ˆ์ด๋ธ”์„ ๋‹ค๋ฅธ ๋ชจ์–‘์œผ๋กœ ํŒจ๋”ฉ ํ–ˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ gather() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „์— Accelerator.pad_across_processes()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ธก ๊ฒฐ๊ณผ์™€ ๋ ˆ์ด๋ธ”์„ ๋™์ผํ•œ ๋ชจ์–‘์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ฉด ํ‰๊ฐ€์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์˜์›ํžˆ ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค. from tqdm.auto import tqdm import torch import numpy as np progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): # ํ•™์Šต model.train() for batch in train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) # ํ‰๊ฐ€ model.eval() for batch in tqdm(eval_dataloader): with torch.no_grad(): generated_tokens = accelerator.unwrap_model(model).generate( batch["input_ids"], attention_mask=batch["attention_mask"], max_length=128, ) labels = batch["labels"] # ์˜ˆ์ธก๊ณผ ๋ ˆ์ด๋ธ”์„ ๋ชจ์œผ๊ธฐ ์ „์— ํ•จ๊ป˜ ํŒจ๋”ฉ ์ˆ˜ํ–‰ generated_tokens = accelerator.pad_across_processes( generated_tokens, dim=1, pad_index=tokenizer.pad_token_id ) labels = accelerator.pad_across_processes(labels, dim=1, pad_index=-100) predictions_gathered = accelerator.gather(generated_tokens) labels_gathered = accelerator.gather(labels) decoded_preds, decoded_labels = postprocess(predictions_gathered, labels_gathered) metric.add_batch(predictions=decoded_preds, references=decoded_labels) results = metric.compute() print(f"epoch {epoch}, BLEU score: {results['score']:.2f}") # ์ €์žฅ ๋ฐ ์—…๋กœ๋“œ accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=f"Training in progress epoch {epoch}", blocking=False ) ์ด ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด Seq2SeqTrainer๋กœ ํ•™์Šต๋œ ๋ชจ๋ธ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ–๋Š” ๋ชจ๋ธ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. huggingface-course/marian-finetuned-kde4-en-to-fr-accelerate์—์„œ ์ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„ ์ฝ”๋“œ๋ฅผ ํŽธ์ง‘ํ•˜์—ฌ ํ•™์Šต ๋ฃจํ”„๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ์•ž์—์„œ ์ถ”๋ก  ์œ„์ ฏ์„ ์‚ฌ์šฉํ•˜์—ฌ Model Hub์—์„œ ๋ฏธ์„ธ ์กฐ์ •ํ•œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ด๋ฏธ ์‚ดํŽด๋ดค์Šต๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์„ ํ™œ์šฉํ•˜์—ฌ ๋กœ์ปฌ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ ์ ˆํ•œ ๋ชจ๋ธ ์‹๋ณ„์ž๋ฅผ ์ง€์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: from transformers import pipeline model_checkpoint = "huggingface-course/marian-finetuned-kde4-en-to-fr" translator = pipeline("translation", model=model_checkpoint) translator("Default to expanded threads") ์˜ˆ์ƒ๋Œ€๋กœ, ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์€ ํ•ด๋‹น ์ง€์‹์„ ์šฐ๋ฆฌ๊ฐ€ ๋ฏธ์„ธ ์กฐ์ •ํ•œ ๋ง๋ญ‰์น˜์— ์ ์šฉํ–ˆ์œผ๋ฉฐ ์˜์–ด ๋‹จ์–ด "threads"๋ฅผ ๊ทธ๋Œ€๋กœ ๋‘๋Š” ๋Œ€์‹  ์ด์ œ ํ”„๋ž‘์Šค์–ด ๊ณต์‹ ๋ฒ„์ „์œผ๋กœ ๋ฒˆ์—ญํ•ฉ๋‹ˆ๋‹ค. "plugin"์— ๋Œ€ํ•ด์„œ๋„ ๋™์ผํ•ฉ๋‹ˆ๋‹ค: translator( "Unable to import %1 using the OFX importer plugin. This file is not the correct format." ) ๋„๋ฉ”์ธ ์–ด๋Žํ…Œ์ด์…˜์˜ ๋˜ ๋‹ค๋ฅธ ์ข‹์€ ์˜ˆ์‹œ๋ฅผ ๋ดค์Šต๋‹ˆ๋‹ค! โœ Your turn! ์•ž์—์„œ ์‚ดํŽด๋ณธ "email"์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ํฌํ•จ๋œ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋ชจ๋ธ์€ ์–ด๋–ค ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋‚˜์š”? 4. ์š”์•ฝ (Summarization) ์ด ์„น์…˜์—์„œ๋Š” Transformer ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธด ๋ฌธ์„œ๋ฅผ ๊ฐ„๋žตํ•˜๊ฒŒ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•, ์ฆ‰ ํ…์ŠคํŠธ ์š”์•ฝ(text summarization) ํƒœ์Šคํฌ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ๊ฐ€์žฅ ์–ด๋ ค์šด NLP ์ž‘์—… ์ค‘ ํ•˜๋‚˜๋กœ ์•Œ๋ ค์ ธ ์žˆ๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” ๊ธธ์ด๊ฐ€ ๊ธด ๊ตฌ์ ˆ์„ ์ดํ•ดํ•˜๊ณ  ์ „์ฒด ๋ฌธ์„œ์˜ ํ•ต์‹ฌ ์ฃผ์ œ๋ฅผ ํฌ๊ด„ํ•˜๋Š” ์ผ๊ด€๋œ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋“ฑ ๋‹ค์–‘ํ•œ ๋Šฅ๋ ฅ์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ํ…์ŠคํŠธ ์š”์•ฝ๋งŒ ์ž˜ ๋œ๋‹ค๋ฉด ๊ธด ๋ฌธ์„œ๋ฅผ ์ž์„ธํžˆ ์ฝ์–ด์•ผ ํ•˜๋Š” ๋„๋ฉ”์ธ ์ „๋ฌธ๊ฐ€์˜ ๋ถ€๋‹ด์„ ๋œ์–ด์คŒ์œผ๋กœ์จ ๋‹ค์–‘ํ•œ ๋น„์ฆˆ๋‹ˆ์Šค ํ”„๋กœ์„ธ์Šค์˜ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. Hugging Face ํ—ˆ๋ธŒ์—๋Š” ์ด๋ฏธ ๋‹ค์–‘ํ•œ ๋ฏธ์„ธ์กฐ์ •๋œ ์š”์•ฝ ๋ชจ๋ธ์ด ์กด์žฌํ•˜์ง€๋งŒ ์ด๋“ค ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์ด ์˜์–ด ๋ฌธ์„œ๋งŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์ด ์„น์…˜์—์„œ ์šฐ๋ฆฌ๋Š” ์˜์–ด์™€ ์ŠคํŽ˜์ธ์–ด๋ฅผ ์œ„ํ•œ ์ด์ค‘ ์–ธ์–ด ๋ชจ๋ธ์„ ํ•™์Šตํ•˜์—ฌ ์˜์–ด ์™ธ์— ๋‹ค๋ฅธ ์–ธ์–ด์— ๋Œ€ํ•œ ์š”์•ฝ ์ž‘์—…์˜ ๊ฐ€๋Šฅ์„ฑ์„ ๋ณด์—ฌ์ฃผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์ด ๋๋‚˜๋ฉด ์•„๋ž˜ ํ‘œ์‹œ๋œ ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ณ ๊ฐ ๋ฆฌ๋ทฐ๋ฅผ ์š”์•ฝํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์ด ๋งŒ๋“ค์–ด์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค: ์•ž์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์ง€๋งŒ ์ด๋Ÿฌํ•œ ์š”์•ฝ ๊ฒฐ๊ณผ๋Š” ๊ณ ๊ฐ์ด ์ œํ’ˆ ๋ฆฌ๋ทฐ์—์„œ ์ œ๊ณตํ•˜๋Š” ์ œ๋ชฉ์„ ํ†ตํ•ด์„œ ํ•™์Šตํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์— ์ ํ•ฉํ•œ ์ด์ค‘ ์–ธ์–ด ๋ง๋ญ‰์น˜(bilingual corpus)๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์ค‘ ์–ธ์–ด ๋ง๋ญ‰์น˜ ์ค€๋น„ํ•˜๊ธฐ Multilingual Amazon Reviews Corpus๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ค‘ ์–ธ์–ด ์š”์•ฝ๊ธฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ง๋ญ‰์น˜๋Š” 6๊ฐœ ์–ธ์–ด๋กœ ๋œ Amazon ์ œํ’ˆ ๋ฆฌ๋ทฐ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค๊ตญ์–ด ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ๋ฒค์น˜๋งˆํ‚นํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฐ ๋ฆฌ๋ทฐ์—๋Š” ์งง์€ ์ œ๋ชฉ์ด ์ˆ˜๋ฐ˜๋˜๋ฏ€๋กœ ์ด ์ œ๋ชฉ์„ ๋ชจ๋ธ์ด ํ•™์Šตํ•  ๋Œ€์ƒ ์š”์•ฝ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์‹œ์ž‘ํ•˜๋ ค๋ฉด Hugging Face Hub์—์„œ ์˜์–ด ๋ฐ ์ŠคํŽ˜์ธ์–ด ํ•˜์œ„ ์ง‘ํ•ฉ์„ ๋‹ค์šด๋กœ๋“œํ•˜์„ธ์š”: from datasets import load_dataset spanish_dataset = load_dataset("amazon_reviews_multi", "es") english_dataset = load_dataset("amazon_reviews_multi", "en") english_dataset ๋ณด์‹œ๋‹ค์‹œํ”ผ, ๊ฐ ์–ธ์–ด์— ๋Œ€ํ•ด train ๋ถ„ํ• ์— ๋Œ€ํ•œ 200,000๊ฐœ์˜ ๋ฆฌ๋ทฐ์™€ validation ๋ฐ test ๋ถ„ํ• ์— ๋Œ€ํ•œ 5,000๊ฐœ์˜ ๋ฆฌ๋ทฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๊ด€์‹ฌ ์žˆ๋Š” ๋ฆฌ๋ทฐ ์ •๋ณด๋Š” review_body ๋ฐ review_title ์—ด์— ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 5์žฅ์—์„œ ๋ฐฐ์šด ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ์ง‘ํ•ฉ์—์„œ ๋ฌด์ž‘์œ„ ์ƒ˜ํ”Œ์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def show_samples(dataset, num_samples=3, seed=42): sample = dataset["train"].shuffle(seed=seed).select(range(num_samples)) for example in sample: print(f"\n'>> Title: {example['review_title']}'") print(f"'>> Review: {example['review_body']}'") show_samples(english_dataset) โœ Try it out! Dataset.shuffle() ๋ช…๋ น์—์„œ ๋žœ๋ค ์‹œ๋“œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์—ฌ ์ฝ”ํผ์Šค์˜ ๋‹ค๋ฅธ ๋ฆฌ๋ทฐ๋ฅผ ํƒ์ƒ‰ํ•ด ๋ด…์‹œ๋‹ค. ์ŠคํŽ˜์ธ์–ด ์‚ฌ์šฉ์ž๋ผ๋ฉด spanish_dataset์˜ ์ผ๋ถ€ ๋ฆฌ๋ทฐ๋ฅผ ์‚ดํŽด๋ณด๊ณ  ์ œ๋ชฉ๋„ ํ•ฉ๋‹นํ•œ ์š”์•ฝ์ฒ˜๋Ÿผ ๋ณด์ด๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”. ์œ„ ์ƒ˜ํ”Œ๋“ค์€ ๊ธ์ •์ ์ธ ์ƒ˜ํ”Œ๋ถ€ํ„ฐ ๋ถ€์ •์ ์ธ ๊ฒƒ๋“ค๊นŒ์ง€(๊ทธ๋ฆฌ๊ณ  ๊ทธ ์‚ฌ์ด์˜ ๋ชจ๋“  ๊ฒƒ๊นŒ์ง€!) ์ผ๋ฐ˜์ ์œผ๋กœ ์˜จ๋ผ์ธ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฆฌ๋ทฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋น„๋ก ์ œ๋ชฉ์ด "meh"์ธ ์˜ˆ์‹œ๋Š” ๊ทธ๋‹ค์ง€ ์œ ์šฉํ•˜์ง€ ์•Š์ง€๋งŒ ๋‹ค๋ฅธ ์ œ๋ชฉ์€ ๋ฆฌ๋ทฐ ์ž์ฒด์— ๋Œ€ํ•œ ์ ์ ˆํ•œ ์š”์•ฝ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. 400,000๊ฐœ์˜ ๋ชจ๋“  ๋ฆฌ๋ทฐ์— ๋Œ€ํ•œ ์š”์•ฝ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์€ ๋‹จ์ผ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋ฏ€๋กœ ํŠน์ • ์ œํ’ˆ ๋„๋ฉ”์ธ์— ๊ตญํ•œํ•˜์—ฌ ์š”์•ฝ์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์ง‘์ค‘ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋„๋ฉ”์ธ์— ๋Œ€ํ•œ ์ „์ฒด์ ์ธ ๋‚ด์šฉ์„ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด english_dataset์„ pandas.DataFrame์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์ œํ’ˆ ์นดํ…Œ๊ณ ๋ฆฌ๋‹น ๋ฆฌ๋ทฐ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: english_dataset.set_format("pandas") english_df = english_dataset["train"][:] # ์ตœ์ƒ์œ„ 20๊ฐœ์˜ ์ƒํ’ˆ์— ๋Œ€ํ•œ ๊ฐœ์ˆ˜ ๋ณด์—ฌ์ฃผ๊ธฐ... english_df["product_category"].value_counts()[:20] ์˜์–ด ๋ฐ์ดํ„ฐ ์…‹์—์„œ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ์ œํ’ˆ์€ ๊ฐ€์ •์šฉํ’ˆ(household items), ์˜๋ฅ˜(clothing) ๋ฐ ๋ฌด์„  ์ „์ž ์ œํ’ˆ(wireless electronics)์— ๊ด€ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Amazon์˜ ๋Œ€ํ‘œ์ ์ธ ํ…Œ๋งˆ ์ƒํ’ˆ์— ์ง‘์ค‘ํ•˜๊ธฐ ์œ„ํ•ด ์„œํ‰ ์š”์•ฝ(book review)์— ์ง‘์ค‘ํ•ฉ์‹œ๋‹ค. ์•„๋งˆ์กด์€ ๋„์„œ ํŒ๋งค์—์„œ๋ถ€ํ„ฐ ์ถœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค! ๋‘ ๊ฐ€์ง€ ์ œํ’ˆ ๋ฒ”์ฃผ(book ๋ฐ digital_e book_purchase)๊ฐ€ ๋„์„œ์— ํฌํ•จ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด ์ œํ’ˆ๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ ๋‘ ์–ธ์–ด๋กœ ๋œ ๋ฐ์ดํ„ฐ ์…‹์„ ํ•„ํ„ฐ๋งํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 5์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด Dataset.filter() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์…‹์„ ๋งค์šฐ ํšจ์œจ์ ์œผ๋กœ ์Šฌ๋ผ์ด์Šคํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def filter_books(example): return ( example["product_category"] == "book" or example["product_category"] == "digital_e book_purchase" ) ์ด์ œ ์ด ํ•จ์ˆ˜๋ฅผ eglish_dataset ๋ฐ spanish_dataset์— ์ ์šฉํ•˜๋ฉด ๊ฒฐ๊ณผ์— ๋„์„œ ์นดํ…Œ๊ณ ๋ฆฌ์™€ ๊ด€๋ จ๋œ ํ–‰๋งŒ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•˜๊ธฐ ์ „์— english_dataset<NAME>์„ "pandas"์—์„œ "arrow"๋กœ ๋‹ค์‹œ ์ „ํ™˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: english_dataset.reset_format() ๊ทธ๋Ÿฐ ๋‹ค์Œ ํ•„ํ„ฐ ๊ธฐ๋Šฅ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ์˜จ์ „์„ฑ ๊ฒ€์‚ฌ(sanity check)๋กœ ๋ฆฌ๋ทฐ ์ƒ˜ํ”Œ์„ ๊ฒ€์‚ฌํ•˜์—ฌ ์‹ค์ œ๋กœ ์ฑ…์— ๊ด€ํ•œ ๊ฒƒ์ธ์ง€ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: spanish_books = spanish_dataset.filter(filter_books) english_books = english_dataset.filter(filter_books) show_samples(english_books) ์ข‹์Šต๋‹ˆ๋‹ค. ๋„์„œ์— ๊ด€ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹ฌ๋ ฅ์ด๋‚˜ OneNote์™€ ๊ฐ™์€ ์ „์ž ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ๊ณผ ๊ฐ™์€ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ๋ฆฌ๋ทฐ๋„ ์กด์žฌํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ „์ฒด์ ์ธ ๋‚ด์šฉ์€ ์š”์•ฝ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ธฐ์— ๊ฑฐ์˜ ์ ํ•ฉํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์— ์ ํ•ฉํ•œ ๋‹ค์–‘ํ•œ ๋ชจ๋ธ์„ ์‚ดํŽด๋ณด๊ธฐ ์ „์— ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•ด์•ผ ํ•  ๋ฐ์ดํ„ฐ ์ค€๋น„ ์ž‘์—…์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด์™€ ์ŠคํŽ˜์ธ์–ด ๋ฆฌ๋ทฐ๋ฅผ ๋‹จ์ผ DatasetDict ๊ฐ์ฒด๋กœ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Datasets๋Š” (์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด) ๋‘ ๊ฐœ์˜ Dataset ๊ฐ์ฒด๋ฅผ ์œ„์•„๋ž˜๋กœ ์Œ“๋Š” ํŽธ๋ฆฌํ•œ concatenate_datasets() ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด์ค‘ ์–ธ์–ด ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๊ฐ ๋ถ„ํ• ์„ ๋ฐ˜๋ณตํ•˜๊ณ  ํ•ด๋‹น ๋ถ„ํ• ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์…‹์„ ์—ฐ๊ฒฐํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์„ž์–ด์„œ ๋ชจ๋ธ์ด ๋‹จ์ผ ์–ธ์–ด์— ๊ณผ์ ํ•ฉ๋˜์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค: from datasets import concatenate_datasets, DatasetDict books_dataset = DatasetDict() for split in english_books.keys(): books_dataset[split] = concatenate_datasets( [english_books[split], spanish_books[split]] ) books_dataset[split] = books_dataset[split].shuffle(seed=42) # ๋ช‡ ๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. show_samples(books_dataset) ์œ„ ๊ฒฐ๊ณผ๋Š” ํ™•์‹คํžˆ ์˜์–ด์™€ ์ŠคํŽ˜์ธ์–ด ๋ฆฌ๋ทฐ๊ฐ€ ํ˜ผํ•ฉ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค! ์ด์ œ ํ•™์Šต ๋ง๋ญ‰์น˜๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ™•์ธํ•ด์•ผ ํ•  ๊ฒƒ์€ ๋ฆฌ๋ทฐ์™€ ์ œ๋ชฉ์˜ ๋‹จ์–ด ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ์ดํ„ฐ ๋‚ด์— ์กด์žฌํ•˜๋Š” ๊ธธ์ด๊ฐ€ ์งง์€ ์ฐธ์กฐ ์š”์•ฝ๋“ค์ด, ํ•˜๋‚˜ ๋˜๋Š” ๋‘ ๋‹จ์–ด๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ์š”์•ฝ ๊ฒฐ๊ณผ๋งŒ์„ ์ถœ๋ ฅํ•˜๋„๋ก ๋ชจ๋ธ์„ ํŽธํ–ฅ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์š”์•ฝ ์ž‘์—…์— ํŠนํžˆ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„๋Š” ๋‹จ์–ด ๋ถ„ํฌ๋ฅผ ๋ณด์—ฌ์ฃผ๋ฉฐ ์ œ๋ชฉ์ด ๋‹จ 1-2๊ฐœ์˜ ๋‹จ์–ด๋กœ ์‹ฌํ•˜๊ฒŒ ์น˜์šฐ์นœ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ์ด ๋ฌธ์ œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ ๋ชจ๋ธ์ด ๋” ํฅ๋ฏธ๋กœ์šด ์š”์•ฝ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งค์šฐ ์งง์€ ์ œ๋ชฉ์„ ๊ฐ€์ง„ ๋ฆฌ๋ทฐ๋ฅผ ๊ฑธ๋Ÿฌ๋‚ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜์–ด์™€ ์ŠคํŽ˜์ธ์–ด ํ…์ŠคํŠธ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋žต์ ์ธ ํœด๋ฆฌ์Šคํ‹ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณต๋ฐฑ์—์„œ ์ œ๋ชฉ์„ ๋ถ„ํ• ํ•œ ํ›„์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” Dataset.filter() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: books_dataset = books_dataset.filter(lambda x: len(x["review_title"].split()) > 2) ์ด์ œ ๋ง๋ญ‰์น˜ ์ค€๋น„๋ฅผ ์™„๋ฃŒํ–ˆ์œผ๋ฏ€๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ๊ฐ€๋Šฅํ•œ Transformer ๋ชจ๋ธ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ํ…์ŠคํŠธ ์š”์•ฝ์„ ์œ„ํ•œ ๋ชจ๋ธ๋“ค ์ƒ๊ฐํ•ด ๋ณด๋ฉด ํ…์ŠคํŠธ ์š”์•ฝ์€ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ณผ ๋น„์Šทํ•œ ์ข…๋ฅ˜์˜ ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ, ๋ฆฌ๋ทฐ ํ…์ŠคํŠธ ๋ณธ๋ฌธ์„ ํ•ด๋‹น ๋ฆฌ๋ทฐ์˜ ๋‘๋“œ๋Ÿฌ์ง„ ํŠน์ง•์„ ํฌ์ฐฉํ•˜๋Š” ์งง์€ ๋ฒ„์ „์˜ ๋˜ ๋‹ค๋ฅธ ํ…์ŠคํŠธ๋กœ "๋ฒˆ์—ญ(translate)" ํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์š”์•ฝ์„ ์œ„ํ•œ ๋Œ€๋ถ€๋ถ„์˜ Transformer ๋ชจ๋ธ์€ 1์žฅ์—์„œ ์ฒ˜์Œ ๋งŒ๋‚œ ์ธ์ฝ”๋”-๋””์ฝ”๋” ์•„ํ‚คํ…์ฒ˜(encoder-decoder architecture)๋ฅผ ์ฑ„ํƒํ•˜์ง€๋งŒ, ํ“จ์ƒท(few-shot) ์„ธํŒ…์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์š”์•ฝ์— ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋Š” GPT ๋ชจ๋ธ ์ œํ’ˆ๊ตฐ๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์™ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ํ‘œ์—๋Š” ์š”์•ฝ์„ ์œ„ํ•ด ๋ฏธ์„ธ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์ธ๊ธฐ ์žˆ๋Š” ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์ด ๋‚˜์—ด๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ ์„ค๋ช… ๋‹ค๊ตญ์–ด ์—ฌ๋ถ€ GPT-2 ์ž๋™ ํšŒ๊ท€ ์–ธ์–ด ๋ชจ๋ธ(auto-regressive language model)๋กœ ํ•™์Šต๋˜์—ˆ์ง€๋งŒ ์ž…๋ ฅ ํ…์ŠคํŠธ ๋์— "TL;DR"์„ ์ถ”๊ฐ€ํ•˜์—ฌ GPT-2๊ฐ€ ์š”์•ฝ์„ ์ƒ์„ฑํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. โŒ PEGASUS ๋‹ค์ค‘ ๋ฌธ์žฅ ํ…์ŠคํŠธ์—์„œ ๋งˆ์Šคํ‚น ๋œ ๋ฌธ์žฅ์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ pretraining objective๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด pretraining objective๋Š” ๊ธฐ๋ณธ์ ์ธ ์–ธ์–ด ๋ชจ๋ธ๋ง๋ณด๋‹ค ์š”์•ฝ์— ๊ฐ€๊นŒ์šฐ๋ฉฐ ์ธ๊ธฐ ์žˆ๋Š” ๋ฒค์น˜๋งˆํฌ์—์„œ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. โŒ T5 Text-to-text ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๊ด€๋ จ๋œ ๋ชจ๋“  ํƒœ์Šคํฌ(task)๋ฅผ ์ง€์›ํ•˜๋Š” ๋ฒ”์šฉ Transformer ์•„ํ‚คํ…์ฒ˜. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์„œ๋ฅผ ์š”์•ฝํ•˜๋Š” ๋ชจ๋ธ์˜ ์ž…๋ ฅ<NAME>์€ summarize: ARTICLE์ž„. โŒ mT5 101๊ฐœ ์–ธ์–ด๋ฅผ ํฌํ•จํ•˜๋Š” ๋‹ค๊ตญ์–ด Common Crawl corpus(mC4)๋ฅผ ์ด์šฉํ•ด ์‚ฌ์ „ ํ•™์Šต๋œ T5์˜ ๋‹ค๊ตญ์–ด ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. โœ… BART BERT์™€ GPT-2์˜ ์‚ฌ์ „ ํ•™์Šต ๋ฐฉ์‹์„ ๊ฒฐํ•ฉํ•˜์—ฌ ์†์ƒ๋œ ์ž…๋ ฅ์„ ์žฌ๊ตฌ์„ฑํ•˜๋„๋ก ํ›ˆ๋ จ๋œ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋” ์Šคํƒ์ด ๋ชจ๋‘ ์žˆ๋Š” ์ƒˆ๋กœ์šด Transformer ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค. โŒ mBART-50 50๊ฐœ ์–ธ์–ด๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ BART์˜ ๋‹ค๊ตญ์–ด ๋ฒ„์ „. โœ… ์ด ํ‘œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ์š”์•ฝ์„ ์œ„ํ•œ ๋Œ€๋ถ€๋ถ„์˜ Transformer ๋ชจ๋ธ(๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ๋กœ ๋Œ€๋ถ€๋ถ„์˜ NLP ์ž‘์—…)์€ ๋‹จ์ผ ์–ธ์–ด๋งŒ์„ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์€ ๋Œ€์ƒ ์ž‘์—…์ด ์˜์–ด๋‚˜ ๋…์ผ์–ด์™€ ๊ฐ™์€ "๊ณ ์ž์›(high-resource)" ์–ธ์–ด๋กœ ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์— ์œ ์šฉํ•˜์ง€๋งŒ ์ „ ์„ธ๊ณ„์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ˆ˜์ฒœ ๊ฐœ์˜ ๋‹ค๋ฅธ ์–ธ์–ด์— ๋Œ€ํ•ด์„œ๋Š” ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ๋„ mT5 ๋ฐ mBART์™€ ๊ฐ™์€ ๋‹ค๊ตญ์–ด Transformer ๋ชจ๋ธ ํด๋ž˜์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ๋“ค์€ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์ „ ํ•™์Šต๋˜์ง€๋งŒ, ๋‹จ์ผ ์–ธ์–ด ๋ง๋ญ‰์น˜์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๋Š” ๋Œ€์‹  ํ•œ ๋ฒˆ์— 50๊ฐœ ์ด์ƒ์˜ ์–ธ์–ด๋กœ ๋œ ํ…์ŠคํŠธ์— ๋Œ€ํ•ด ๊ณต๋™์œผ๋กœ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค! ์šฐ๋ฆฌ๋Š” text-to-text ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์‚ฌ์ „ ํ•™์Šต๋œ T5๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ํฅ๋ฏธ๋กœ์šด ์•„ํ‚คํ…์ฒ˜์ธ mT5์— ์ดˆ์ ์„ ๋งž์ถœ ๊ฒƒ์ž…๋‹ˆ๋‹ค. T5์—์„œ ๋ชจ๋“  NLP ์ž‘์—…์€ summarize:์™€ ๊ฐ™์€ ์ž…๋ ฅ๋œ ์ ‘๋‘์‚ฌ ํ”„๋กฌํ”„ํŠธ(prefix prompt)์— ์˜ํ•ด์„œ ์„ค์ •๋˜๋ฉฐ, ๋ชจ๋ธ์ด ์ƒ์„ฑํ•˜๋Š” ํ…์ŠคํŠธ๋Š” ์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ์— ํ•ด๋‹นํ•˜๋Š” ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฌผ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด T5๋Š” ๋‹จ์ผ ๋ชจ๋ธ๋กœ ๋งŽ์€ ์ž‘์—…์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋งค์šฐ ๋‹ค์žฌ๋‹ค๋Šฅํ•ฉ๋‹ˆ๋‹ค! mT5๋Š” ์ ‘๋‘์‚ฌ ํ”„๋กฌํ”„ํŠธ(prefix prompt)๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์ง€๋งŒ T5์˜ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„<NAME>๊ณ  ๋‹ค๊ตญ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ์„ ํƒํ–ˆ์œผ๋ฏ€๋กœ ํ•™์Šต์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. โœ Try it out! ์ด ์„น์…˜์—์„œ ์„ค๋ช…ํ•˜๋Š” ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ mBART๋„ ๋™์ผํ•œ ๊ธฐ๋ฒ•์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ•˜์—ฌ mT5์˜ ๊ฒฐ๊ณผ์™€ ๋น„๊ตํ•ด ๋ณด์„ธ์š”. ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์˜์–ด์— ๋Œ€ํ•ด์„œ๋Š” T5๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€์š”. T5์—๋Š” ํŠน๋ณ„ํ•œ ์ ‘๋‘์‚ฌ ํ”„๋กฌํ”„ํŠธ(prefix prompt)๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์•„๋ž˜ ์ „์ฒ˜๋ฆฌ ๋‹จ๊ณ„์˜ ์ž…๋ ฅ ์˜ˆ์ œ์— summarize:๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ๋‹ค์Œ ์ž‘์—…์€ ๋ฆฌ๋ทฐ์™€ ์ œ๋ชฉ์„ ํ† ํฐํ™”ํ•˜๊ณ  ์ธ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ‰์†Œ์ฒ˜๋Ÿผ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ์™€ ์—ฐ๊ฒฐ๋œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๊ธ‰์  ์งง์€ ์‹œ๊ฐ„ ๋‚ด์— ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก mt5-small์„ ์ฒดํฌํฌ์ธํŠธ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: from transformers import AutoTokenizer model_checkpoint = "google/mt5-small" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) NLP ํ”„๋กœ์ ํŠธ์˜ ์ดˆ๊ธฐ ๋‹จ๊ณ„์—์„œ๋Š” ์ž‘์€ ๋ฐ์ดํ„ฐ ์ƒ˜ํ”Œ์—์„œ "์ž‘์€" ๋ชจ๋ธ ํด๋ž˜์Šค๋ฅผ ํ•™์Šต์‹œํ‚ค๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ข…๋‹จ ๊ฐ„ ์›Œํฌํ”Œ๋กœ(end-to-end workflow)๋ฅผ ๋” ๋น ๋ฅด๊ฒŒ ๋””๋ฒ„๊ทธํ•˜๊ณ  ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์— ํ™•์‹ ์ด ์ƒ๊ธฐ๋ฉด ๋ชจ๋ธ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ์–ธ์ œ๋“ ์ง€ ๋ชจ๋ธ์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ž‘์€ ์˜ˆ์ œ์—์„œ mT5 ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: inputs = tokenizer("I loved reading the Hunger Games!") inputs ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” 3์žฅ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฏธ์„ธ ์กฐ์ • ์‹คํ—˜์—์„œ ๋งŒ๋‚œ ์นœ์ˆ™ํ•œ input_ids์™€ attention_mask๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €์˜ convert_ids_to_tokens() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋Ÿฌํ•œ ์ž…๋ ฅ ID๋ฅผ ๋””์ฝ”๋”ฉ ํ•˜์—ฌ ์–ด๋–ค ์ข…๋ฅ˜์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋ชจ๋ธ์ด ์‹คํ–‰ํ•˜๊ณ  ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: tokenizer.convert_ids_to_tokens(inputs.input_ids) ํŠน์ˆ˜ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž์ธ _ ์™€ ์‹œํ€€์Šค ๋ ํ† ํฐ </s>์€ 6์žฅ์—์„œ ๋…ผ์˜ํ•œ Unigram ๋ถ„ํ•  ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” SentencePiece ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Unigram์€ SentencePiece๊ฐ€ ์•…์„ผํŠธ, ๊ตฌ๋‘์  ๋ฐ ์ผ๋ณธ์–ด์™€ ๊ฐ™์€ ๋งŽ์€ ์–ธ์–ด๋“ค์— ๊ณต๋ฐฑ ๋ฌธ์ž๊ฐ€ ์—†๋‹ค๋Š” ์‚ฌ์‹ค์— ๋Œ€ํ•ด์„œ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ฒŒ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๊ตญ์–ด ๋ง๋ญ‰์น˜์— ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ง๋ญ‰์น˜๋ฅผ ํ† ํฐํ™”ํ•˜๋ ค๋ฉด ์š”์•ฝ ์ž‘์—…๊ณผ ๊ด€๋ จ๋œ ํŠน์ˆ˜ํ•œ ์ƒํ™ฉ์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ, ๋ ˆ์ด๋ธ”๋„ ํ…์ŠคํŠธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋ธ์˜ ์ตœ๋Œ€ ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ๋ฅผ ์ดˆ๊ณผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๋ชจ๋ธ์— ์ง€๋‚˜์น˜๊ฒŒ ๊ธด ์ž…๋ ฅ์„ ์ „๋‹ฌํ•˜์ง€ ์•Š๋„๋ก ๋ฆฌ๋ทฐ์™€ ์ œ๋ชฉ ๋ชจ๋‘์— ์ ˆ๋‹จ ์ž‘์—…(truncation)์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Transformers์˜ ํ† ํฌ ๋‚˜์ด์ €๋Š” ์ž…๋ ฅ๊ณผ ๋ณ‘๋ ฌ๋กœ ๋ ˆ์ด๋ธ”์„ ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ‹์ง„ as_target_tokenizer() ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋จผ์ € ์ž…๋ ฅ์„ ์ธ์ฝ”๋”ฉํ•œ ๋‹ค์Œ ๋ ˆ์ด๋ธ”์„ ๋ณ„๋„์˜ ์—ด๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ mT5์— ๋Œ€ํ•ด์„œ ์ด ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์˜ˆ์ž…๋‹ˆ๋‹ค: max_input_length = 512 max_target_length = 30 def preprocess_function(examples): model_inputs = tokenizer( examples["review_body"], max_length=max_input_length, truncation=True ) # ํƒ€๊นƒ์„ ์œ„ํ•œ ํ† ํฌ ๋‚˜์ด์ € ์„ค์ • with tokenizer.as_target_tokenizer(): labels = tokenizer( examples["review_title"], max_length=max_target_length, truncation=True ) model_inputs["labels"] = labels["input_ids"] return model_inputs ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๊ณ  ์žˆ๋Š”์ง€ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์ด ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํ•œ ์ฒซ ๋ฒˆ์งธ ์ผ์€ max_input_length ๋ฐ max_target_length์— ๋Œ€ํ•œ ๊ฐ’์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ ๋ฆฌ๋ทฐ์™€ ์ œ๋ชฉ์˜ ๊ธธ์ด์— ๋Œ€ํ•œ ์ƒํ•œ์„ ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ๋ทฐ ๋ณธ๋ฌธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ œ๋ชฉ๋ณด๋‹ค ํ›จ์”ฌ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๊ฐ’์„ ์ ์ ˆํ•˜๊ฒŒ ์กฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ preprocess_function() ์ž์ฒด์—์„œ ๋ฆฌ๋ทฐ ๋ณธ๋ฌธ์ด ๋จผ์ € ํ† ํฐํ™”๋˜๊ณ  ๋ฆฌ๋ทฐ ์ œ๋ชฉ์ด as_target_tokenizer()๋ฅผ ์ด์šฉํ•ด์„œ ์ถ”๊ฐ€์ ์œผ๋กœ ํ† ํฐํ™”๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. preprocess_function()์„ ์‚ฌ์šฉํ•˜๋ฉด ์ „์ฒด ์ฝ”์Šค์—์„œ ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ์‚ฌ์šฉํ•œ ํŽธ๋ฆฌํ•œ Dataset.map() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „์ฒด ๋ง๋ญ‰์น˜๋ฅผ ํ† ํฐํ™”ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค: tokenized_datasets = books_dataset.map(preprocess_function, batched=True) ๋ง๋ญ‰์น˜๊ฐ€ ์ „์ฒ˜๋ฆฌ๋˜์—ˆ์œผ๋ฏ€๋กœ ์š”์•ฝ์— ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ช‡ ๊ฐ€์ง€ ํ‰๊ฐ€ ์ง€ํ‘œ(metrics)๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์ง€๋งŒ, ์ปดํ“จํ„ฐ์— ์˜ํ•ด์„œ ์ƒ์„ฑ๋œ ํ…์ŠคํŠธ(machine-generated text)์˜ ํ’ˆ์งˆ์„ ์ธก์ •ํ•˜๋Š”๋ฐ ๋ฌ˜์ฑ…(silver bullet)์€ ์—†์Šต๋‹ˆ๋‹ค. ์œ„์˜ Dataset.map() ํ•จ์ˆ˜์—์„œ batched=True๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์„ ๋ˆˆ์น˜์ฑ„์…จ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ 1,000๊ฐœ(๊ธฐ๋ณธ๊ฐ’)์˜ ๋ฐฐ์น˜(batch)๋กœ ์˜ˆ์ œ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ณ  Transformers์—์„œ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €์˜ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋”ฉ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด batched=True๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์‹ญ์‹œ์˜ค! ํ…์ŠคํŠธ ์š”์•ฝ์„ ์œ„ํ•œ ํ‰๊ฐ€ ์ง€ํ‘œ(metrics) ์ด ์ฝ”์Šค์—์„œ ๋‹ค๋ฃฌ ๋Œ€๋ถ€๋ถ„์˜ ๋‹ค๋ฅธ ์ž‘์—…๊ณผ ๋น„๊ตํ•  ๋•Œ ์š”์•ฝ ๋˜๋Š” ๋ฒˆ์—ญ๊ณผ ๊ฐ™์€ ํ…์ŠคํŠธ ์ƒ์„ฑ ์ž‘์—…์˜ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "๋‚˜๋Š” ํ—๊ฑฐ ๊ฒŒ์ž„์„ ์ฝ๋Š” ๊ฒƒ์„ ์ข‹์•„ํ–ˆ์Šต๋‹ˆ๋‹ค"์™€ ๊ฐ™์€ ๋ฆฌ๋ทฐ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด "๋‚˜๋Š” ํ—๊ฑฐ ๊ฒŒ์ž„์„ ์ข‹์•„ํ–ˆ์Šต๋‹ˆ๋‹ค" ๋˜๋Š” "ํ—๊ฑฐ ๊ฒŒ์ž„์€ ์ž˜ ์ฝ์—ˆ์Šต๋‹ˆ๋‹ค"์™€ ๊ฐ™์€ ์ ์ ˆํ•œ ์š”์•ฝ ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถ„๋ช…ํžˆ, ์ƒ์„ฑ๋œ ์š”์•ฝ๊ณผ ๋ ˆ์ด๋ธ” ์‚ฌ์ด์— ์™„์ „ ์ผ์น˜(exact match) ๊ธฐ๋ฐ˜์˜ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€ ํ•ด๊ฒฐ์ฑ…์ด ์•„๋‹™๋‹ˆ๋‹ค. ์šฐ๋ฆฌ ๋ชจ๋‘๋Š” ์ž์‹ ๋งŒ์˜ ์ž‘๋ฌธ ์Šคํƒ€์ผ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ์ธก์ • ๊ธฐ์ค€์—์„œ๋Š” ์ธ๊ฐ„์กฐ์ฐจ๋„ ์ข‹์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์š”์•ฝ ์ž‘์—…์„ ์œ„ํ•ด์„œ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ธก์ •ํ•ญ๋ชฉ ์ค‘ ํ•˜๋‚˜๋Š” ROUGE ์ ์ˆ˜(Recall-Oriented Understudy for Gisting Evaluation)์ž…๋‹ˆ๋‹ค. ์ด ํ‰๊ฐ€ ์ง€ํ‘œ์˜ ๊ธฐ๋ณธ์ ์ธ ์•„์ด๋””์–ด๋Š” ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ๋กœ ์ƒ์„ฑ๋œ ์š”์•ฝ์„ ์‚ฌ๋žŒ์ด ์ง์ ‘ ๋งŒ๋“  ์ฐธ์กฐ ์š”์•ฝ ์„ธํŠธ์™€ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ๋‘ ์š”์•ฝ์„ ๋น„๊ตํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค: generated_summary = "I absolutely loved reading the Hunger Games" reference_summary = "I loved reading the Hunger Games" ๋‘ ์š”์•ฝ์ด ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€ ๋น„๊ตํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ๊ฒน์น˜๋Š” ๋‹จ์–ด์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” 6์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ๋ฒ•์€ ์•ฝ๊ฐ„ ์กฐ์žกํ•˜๋ฏ€๋กœ ๊ทธ ๋Œ€์‹  ROUGE๋Š” ๊ฒน์นจ์— ๋Œ€ํ•œ ์ •๋ฐ€๋„ ๋ฐ ์žฌํ˜„์œจ ๊ณ„์‚ฐ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ์— ๋Œ€ํ•ด ์ฒ˜์Œ ๋“ค์–ด๋ณด๋”๋ผ๋„ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์„ธ์š”. ๋ชจ๋“  ๊ฒƒ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๋ช‡ ๊ฐ€์ง€ ๋ช…์พŒํ•œ ์˜ˆ๋ฅผ ํ•จ๊ป˜ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”ํŠธ๋ฆญ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ถ„๋ฅ˜ ์ž‘์—…์—์„œ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ํ•ด๋‹น ์ปจํ…์ŠคํŠธ์—์„œ ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ์ด ์ •์˜๋˜๋Š” ๋ฐฉ์‹์„ ์ดํ•ดํ•˜๋ ค๋ฉด scikit-learn ๊ฐ€์ด๋“œ๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ROUGE์˜ ๊ฒฝ์šฐ ์žฌํ˜„์œจ์€ ์ƒ์„ฑ๋œ ์ฐธ์กฐ ์š”์•ฝ์ด ์บก์ฒ˜ํ•œ ์ฐธ์กฐ ์š”์•ฝ์˜ ์–‘์„ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด๋งŒ ๋น„๊ตํ•œ๋‹ค๋ฉด ์žฌํ˜„์œจ์€ ๋‹ค์Œ ๊ณต์‹์— ๋”ฐ๋ผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ์žฌํ˜„์œจ ์ค‘์ฒฉ๋œ ๋‹จ์–ด๋“ค์˜ ๊ฐœ์ˆ˜ ์ฐธ์กฐ ์š”์•ฝ ๋‚ด์˜ ๋ชจ๋“  ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ ํ˜„ = ์ฒฉ ๋‹จ ๋“ค ๊ฐœ ์ฐธ ์š” ๋‚ด ๋ชจ ๋‹จ์˜ ์ˆ˜ ์œ„์˜ ๊ฐ„๋‹จํ•œ ์˜ˆ์—์„œ ์ด ๊ณต์‹์€ 6/6 = 1์˜ ์™„๋ฒฝํ•œ ์žฌํ˜„์œจ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ฐธ์กฐ ์š”์•ฝ์˜ ๋ชจ๋“  ๋‹จ์–ด๋Š” ๋ชจ๋ธ์— ์˜ํ•ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ›Œ๋ฅญํ•˜๊ฒŒ ๋“ค๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ ์ƒ์„ฑ๋œ ์š”์•ฝ์ด "I really really loved reading the Hunger Games all night"์˜€๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ณด์‹ญ์‹œ์˜ค. ์ด๊ฒƒ์€ ๋˜ํ•œ ์™„๋ฒฝํ•œ ์žฌํ˜„์œจ์„ ๋ณด์ด์ง€๋งŒ ์žฅํ™ฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„๋ช…ํžˆ ๋” ๋‚˜์œ ์š”์•ฝ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒํ™ฉ๋„ ํ•จ๊ป˜ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ROUGE ์ปจํ…์ŠคํŠธ๋Š” ์ƒ์„ฑ๋œ ์š”์•ฝ์ด ์–ผ๋งˆ๋‚˜ ๊ด€๋ จ์ด ์žˆ๋Š”์ง€ ์ธก์ •ํ•˜๋Š” ์ •๋ฐ€๋„๋„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค: ์ •๋ฐ€๋„ ์ค‘์ฒฉ๋œ ๋‹จ์–ด๋“ค์˜ ๊ฐœ์ˆ˜ ์ƒ์„ฑ๋œ ์š”์•ฝ ๋‚ด์˜ ๋ชจ๋“  ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ ๋ฐ€ = ์ฒฉ ๋‹จ ๋“ค ๊ฐœ ์ƒ๋œ ์•ฝ์˜ ๋“  ์–ด ๊ฐœ ์ด ์ˆ˜์‹์„ ์œ„์˜ ์žฅํ™ฉํ•œ ์š”์•ฝ์— ์ ์šฉํ•˜๋ฉด 6/10 = 0.6์˜ ์ •๋ฐ€๋„๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ๋” ์งง์€ ๊ฒƒ์œผ๋กœ ์–ป์€ 6/7 = 0.86์˜ ์ •๋ฐ€๋„๋ณด๋‹ค ์ƒ๋‹นํžˆ ๋‚˜์ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ์ด ๋ชจ๋‘ ๊ณ„์‚ฐ๋œ ๋‹ค์Œ F1 ์ ์ˆ˜(์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ์˜ ์กฐํ™” ํ‰๊ท )๊ฐ€ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. rouge_score ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๋ฉด Datasets์—์„œ ์ด ์ž‘์—…์„ ์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: !python3 -m pip install rouge_score ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ROUGE ๋ฉ”ํŠธ๋ฆญ์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค: from datasets import load_metric rouge_score = load_metric("rouge") ๊ทธ๋Ÿฐ ๋‹ค์Œ rouge_score.compute() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ๋ฉ”ํŠธ๋ฆญ์„ ํ•œ ๋ฒˆ์— ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: scores = rouge_score.compute( predictions=[generated_summary], references=[reference_summary] ) scores ์™€์šฐ, ๋งŽ์€ ์ •๋ณด๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ฒƒ์ด ๋ฌด์—‡์„ ์˜๋ฏธํ• ๊นŒ์š”? ์ฒซ์งธ, Datasets๋Š” ์‹ค์ œ๋กœ ์ •๋ฐ€๋„, ์žฌํ˜„์œจ ๋ฐ F1 ์ ์ˆ˜์— ๋Œ€ํ•œ ์‹ ๋ขฐ ๊ตฌ๊ฐ„์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” low, mid ๋ฐ high ์†์„ฑ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ Datasets๋Š” ์ƒ์„ฑ๋œ ์š”์•ฝ๊ณผ ์ฐธ์กฐ ์š”์•ฝ์„ ๋น„๊ตํ•  ๋•Œ ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ ํ…์ŠคํŠธ granularity(์ž…๋„)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๋‹ค์–‘ํ•œ ROUGE ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. rouge1์€ unigram์˜ ๊ฒน์นจ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋‹จ์ง€ ๋‹จ์–ด ๊ฒน์นจ์„ ๋ณด๊ธฐ ์ข‹๊ฒŒ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด๋ฉฐ ์œ„์—์„œ ๋…ผ์˜ํ•œ ๋ฐ”๋กœ ๊ทธ ๋ฉ”ํŠธ๋ฆญ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ ์ˆ˜์˜ ์ค‘๊ฐ„ ๊ฐ’์„ ์ถ”์ถœํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: scores["rouge1"].mid ์ข‹์Šต๋‹ˆ๋‹ค. ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ ์ˆซ์ž๊ฐ€ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค! ์ด์ œ ๋‹ค๋ฅธ ROUGE ์ ์ˆ˜๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? rouge2๋Š” bigram ๊ฐ„์˜ ๊ฒน์นจ(๋‹จ์–ด ์Œ์˜ ๊ฒน์นจ์„ ์ƒ๊ฐ)์„ ์ธก์ •ํ•˜๋Š” ๋ฐ˜๋ฉด rougeL ๋ฐ rougeLsum์€ ์ƒ์„ฑ๋œ ์š”์•ฝ ๋ฐ ์ฐธ์กฐ ์š”์•ฝ์—์„œ ๊ฐ€์žฅ ๊ธด ๊ณตํ†ต๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ฐพ์•„ ๊ฐ€์žฅ ๊ธด ์ผ์น˜ํ•˜๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค. rougeLsum์˜ "sum"์€ ์ด ๋ฉ”ํŠธ๋ฆญ์ด ์ „์ฒด ์š”์•ฝ ํ…์ŠคํŠธ์— ๋Œ€ํ•ด ๊ณ„์‚ฐ๋˜๋Š” ๋ฐ˜๋ฉด rougeL์€ ๊ฐœ๋ณ„ ๋ฌธ์žฅ์— ๋Œ€ํ•œ ํ‰๊ท ์œผ๋กœ ๊ณ„์‚ฐ๋œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. โœ Try it out! ์ง์ ‘ ์ƒ์„ฑ๋œ ์š”์•ฝ๊ณผ ์ฐธ์กฐ ์š”์•ฝ ์˜ˆ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฒฐ๊ณผ ROUGE ์ ์ˆ˜๊ฐ€ ์ •๋ฐ€๋„ ๋ฐ ์žฌํ˜„์œจ ๊ณต์‹์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ์ˆ˜๋™ ๊ณ„์‚ฐ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์„ธ์š”. ๋ณด๋„ˆ์Šค ์ ์ˆ˜๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ, ํ…์ŠคํŠธ๋ฅผ bigram์œผ๋กœ ๋ถ„ํ• ํ•˜๊ณ  rouge2 ์ธก์ • ํ•ญ๋ชฉ์˜ ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ์„ ๋น„๊ตํ•ด ๋ณด์„ธ์š”. ์šฐ๋ฆฌ๋Š” ์ด ROUGE ์ ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์šฐ๋ฆฌ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์ถ”์ ํ•  ๊ฒƒ์ด์ง€๋งŒ, ๊ทธ์ „์— ๋ชจ๋“  ํ›Œ๋ฅญํ•œ NLP ์‹ค๋ฌด์ž๊ฐ€ ํ•ด์•ผ ํ•  ์ผ์„ ํ•ฉ์‹œ๋‹ค. ๊ฐ•๋ ฅํ•˜์ง€๋งŒ ๋‹จ์ˆœํ•œ ๋ฒ ์ด์Šค๋ผ์ธ(baseline) ๋ชจ๋ธ์„ ๋งŒ๋“œ์‹ญ์‹œ์˜ค! ๊ฐ•๋ ฅํ•œ ๋ฒ ์ด์Šค๋ผ์ธ ๋ชจ๋ธ ๋งŒ๋“ค๊ธฐ ํ…์ŠคํŠธ ์š”์•ฝ์„ ์œ„ํ•œ ์ผ๋ฐ˜์ ์ธ ๋ฒ ์ด์Šค๋ผ์ธ ์‹œ์Šคํ…œ์€ ์ผ๋ฐ˜์ ์œผ๋กœ lead-3 ๋ฒ ์ด์Šค๋ผ์ธ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”, ๋ฌธ์„œ์˜ ์ฒซ ์„ธ ๋ฌธ์žฅ์„ ์ทจํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฌธ์žฅ ๊ฒฝ๊ณ„๋ฅผ ์ถ”์ ํ•˜๊ธฐ ์œ„ํ•ด ๋งˆ์นจํ‘œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ "U.S."๋‚˜ "U.N." ๋“ฑ๊ณผ ๊ฐ™์€ ์•ฝ์–ด์—์„œ๋Š” ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์‹  ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋” ๋‚˜์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํฌํ•จ๋œ nltk ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด pip๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: !python3 -m pip install nltk ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ๊ตฌ๋‘์  ๊ทœ์น™์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. import nltk nltk.download("punkt") ๋‹ค์Œ์œผ๋กœ, nltk์—์„œ ๋ฌธ์žฅ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋ฆฌ๋ทฐ์—์„œ ์ฒ˜์Œ ์„ธ ๋ฌธ์žฅ์„ ์ถ”์ถœํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์š”์•ฝ์—์„œ์˜ ๊ด€๋ก€๋Š” ๊ฐ ์š”์•ฝ์„ ์ค„ ๋ฐ”๊ฟˆ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ด ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ํฌํ•จ์‹œ์ผœ์„œ ํ•™์Šต ์˜ˆ์ œ์—์„œ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: from nltk.tokenize import sent_tokenize def three_sentence_summary(text): return "\n".join(sent_tokenize(text)[:3]) print(three_sentence_summary(books_dataset["train"][1]["review_body"])) ์ด ๋ฐฉ๋ฒ•์ด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ ๊ฐ™์œผ๋ฏ€๋กœ ์ด์ œ ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ด๋Ÿฌํ•œ "์ฒซ 3์ค„ ์š”์•ฝ"์„ ์ถ”์ถœํ•˜๊ณ  ์ด ๋ฒ ์ด์Šค๋ผ์ธ ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•œ ROUGE ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def evaluate_baseline(dataset, metric): summaries = [three_sentence_summary(text) for text in dataset["review_body"]] return metric.compute(predictions=summaries, references=dataset["review_title"]) ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆ ์ง‘ํ•ฉ(validation set)์— ๋Œ€ํ•œ ROUGE ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  Pandas๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ฝ๊ฐ„ ์˜ˆ์˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: import pandas as pd score = evaluate_baseline(books_dataset["validation"], rouge_score) rouge_names = ["rouge1", "rouge2", "rougeL", "rougeLsum"] rouge_dict = dict((rn, round(score[rn].mid.fmeasure * 100, 2)) for rn in rouge_names) rouge_dict rouge2 ์ ์ˆ˜๊ฐ€ ๋‚˜๋จธ์ง€ ์ ์ˆ˜๋ณด๋‹ค ํ›จ์”ฌ ๋‚ฎ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ฆฌ๋ทฐ ์ œ๋ชฉ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ„๊ฒฐํ•˜์—ฌ lead-3 ๋ฒ ์ด์Šค๋ผ์ธ์ด ๋„ˆ๋ฌด ๊ธธ๊ณ  ์žฅํ™ฉํ•˜๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋ฐ˜์˜ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋น„๊ตํ•  ์ข‹์€ ๋ฒ ์ด์Šค๋ผ์ธ์ด ์ƒ๊ฒผ์œผ๋ฏ€๋กœ mT5๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๋ฐ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ด๊ฒ ์Šต๋‹ˆ๋‹ค! Trainer API๋ฅผ ์ด์šฉํ•˜์—ฌ mT5 ๋ฏธ์„ธ์กฐ์ • ์š”์•ฝ์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์€ ์ด ์žฅ์—์„œ ๋‹ค๋ฃฌ ๋‹ค๋ฅธ ์ž‘์—…๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผ ํ•  ์ผ์€ mt5-small ์ฒดํฌํฌ์ธํŠธ์—์„œ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์š”์•ฝ์€ sequence-to-sequence ์ž‘์—…์ด๋ฏ€๋กœ AutoModelForSeq2SeqLM ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ์ž๋™์œผ๋กœ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์บ์‹œ ํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoModelForSeq2SeqLM model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint) ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์—์„œ ๋ชจ๋ธ ๋ฏธ์„ธ ์กฐ์ •์— ๋Œ€ํ•œ ๊ฒฝ๊ณ ๊ฐ€ ํ‘œ์‹œ๋˜์ง€ ์•Š๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” sequence-to-sequence ์ž‘์—…์˜ ๊ฒฝ์šฐ ๋„คํŠธ์›Œํฌ์˜ ๋ชจ๋“  ๊ฐ€์ค‘์น˜๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ 3์žฅ์˜ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๋ชจ๋ธ๊ณผ ๋น„๊ตํ•ด ๋ณด์„ธ์š”. ์—ฌ๊ธฐ์„œ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ํ—ค๋“œ(head)๋Š” ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋œ ๋„คํŠธ์›Œํฌ๋กœ ๋Œ€์ฒด๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ํ•ด์•ผ ํ•  ์ผ์€ Hugging Face Hub์— ๋กœ๊ทธ์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋…ธํŠธ๋ถ์—์„œ ์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from huggingface_hub import notebook_login notebook_login() ํ•™์Šต ๊ณผ์ •์—์„œ ROUGE ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋ ค๋ฉด ์š”์•ฝ์„ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹คํ–‰์Šค๋Ÿฝ๊ฒŒ๋„ Transformers๋Š” ์ž๋™์œผ๋กœ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ „์šฉ Seq2SeqTrainingArguments ๋ฐ Seq2SeqTrainer ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค! ์ด๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์œ„ํ•ด ๋จผ์ € ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ์‹คํ—˜์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ์ธ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: from transformers import Seq2SeqTrainingArguments batch_size = 8 num_train_epochs = 8 # ๋งค ์—ํฌํฌ๋งˆ๋‹ค training loss๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. logging_steps = len(tokenized_datasets["train"]) // batch_size model_name = model_checkpoint.split("/")[-1] args = Seq2SeqTrainingArguments( output_dir=f"{model_name}-finetuned-amazon-en-es", evaluation_strategy="epoch", learning_rate=5.6e-5, per_device_train_batch_size=batch_size, per_device_eval_batch_size=batch_size, weight_decay=0.01, save_total_limit=3, num_train_epochs=num_train_epochs, predict_with_generate=True, logging_steps=logging_steps, push_to_hub=True, ) ์—ฌ๊ธฐ์—์„œ predict_with_generate ์ธ์ˆ˜๋Š” ํ‰๊ฐ€ ์ค‘์— ์š”์•ฝ์„ ์ƒ์„ฑํ•˜์—ฌ ๊ฐ epoch์— ๋Œ€ํ•œ ROUGE ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 1์žฅ์—์„œ ๋…ผ์˜ํ•œ ๋ฐ”์™€ ๊ฐ™์ด ๋””์ฝ”๋”๋Š” ํ† ํฐ์„ ํ•˜๋‚˜์”ฉ ์˜ˆ์ธกํ•˜์—ฌ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ ์ด๋Š” ๋ชจ๋ธ์˜ generate() ๋ฉ”์„œ๋“œ๋กœ ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค. predict_with_generate=True๋กœ ์„ค์ •ํ•˜๋ฉด Seq2SeqTrainer๊ฐ€ ํ‰๊ฐ€๋ฅผ ์œ„ํ•ด ํ•ด๋‹น ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•™์Šต๋ฅ , ์—ํฌํฌ ์ˆ˜ ๋ฐ ๊ฐ€์ค‘์น˜ ๊ฐ์†Œ์™€ ๊ฐ™์€ ๊ธฐ๋ณธ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘ ์ผ๋ถ€๋ฅผ ์กฐ์ •ํ–ˆ์œผ๋ฉฐ, ํ•™์Šต ์ค‘ ์ตœ๋Œ€ 3๊ฐœ์˜ ์ฒดํฌํฌ์ธํŠธ๋งŒ ์ €์žฅํ•˜๋„๋ก save_total_limit ์˜ต์…˜์„ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” mT5์˜ "์†Œํ˜•" ๋ฒ„์ „๋„ ์•ฝ 1 GB์˜ ํ•˜๋“œ ๋“œ๋ผ์ด๋ธŒ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ๋ณต์‚ฌ๋ณธ ์ˆ˜๋ฅผ ์ œํ•œํ•˜์—ฌ ์•ฝ๊ฐ„์˜ ๊ณต๊ฐ„์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. push_to_hub=True ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•™์Šต ํ›„ ๋ชจ๋ธ์„ Hub๋กœ ํ‘ธ์‹œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. output_dir์— ์˜ํ•ด ์ •์˜๋œ ์œ„์น˜์˜ ์‚ฌ์šฉ์ž ํ”„๋กœํ•„ ์•„๋ž˜ ์ €์žฅ์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. hub_model_id ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘ธ์‹œ ํ•˜๋ ค๋Š” ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(ํŠนํžˆ, ์กฐ์ง์— ํ‘ธ์‹œ ํ•˜๋ ค๋ฉด ์ด ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ). ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจ๋ธ์„ huggingface-course organization์— ํ‘ธ์‹œ ํ•  ๋•Œ hub_model_id="huggingface-course/mt5-finetuned-amazon-en-es"๋ฅผ Seq2SeqTrainingArguments์— ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ํ•ด์•ผ ํ•  ์ผ์€ ํ•™์Šต ์ค‘์— ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋„๋ก trainer์—๊ฒŒ compute_metrics() ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์š”์•ฝ ์ž‘์—…์„ ์œ„ํ•ด์„œ๋Š” ROUGE ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์ „์— ์ถœ๋ ฅ๊ณผ ๋ ˆ์ด๋ธ”์„ ํ…์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋ธ์˜ ์˜ˆ์ธก์— ๋Œ€ํ•ด ๋‹จ์ˆœํžˆ rouge_score.compute()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์•ฝ๊ฐ„ ๋” ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ํ•จ์ˆ˜๋Š” ์ •ํ™•ํžˆ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉฐ nltk์˜ sent_tokenize() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์•ฝ ๋ฌธ์žฅ์„ ์ค„ ๋ฐ”๊ฟˆ์œผ๋กœ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค: import numpy as np def compute_metrics(eval_pred): predictions, labels = eval_pred # ์ƒ์„ฑ๋œ ์š”์•ฝ์„ ํ…์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉ decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True) # ๋ ˆ์ด๋ธ” ๋‚ด์˜ -100์„ ๊ต์ฒดํ•œ๋‹ค. labels = np.where(labels != -100, labels, tokenizer.pad_token_id) # ๋ ˆํผ๋Ÿฐ์Šค ์š”์•ฝ์„ ํ…์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉ decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) # ROUGE๋Š” ๊ฐ ๋ฌธ์žฅ ๋‹ค์Œ์— ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์š”๊ตฌํ•œ๋‹ค. decoded_preds = ["\n".join(sent_tokenize(pred.strip())) for pred in decoded_preds] decoded_labels = ["\n".join(sent_tokenize(label.strip())) for label in decoded_labels] # ROUGE ์ ์ˆ˜ ๊ณ„์‚ฐ result = rouge_score.compute( predictions=decoded_preds, references=decoded_labels, use_stemmer=True ) # ์ค‘๊ฐ„ ์ ์ˆ˜(median scores) ์ถ”์ถœ result = {key: value.mid.fmeasure * 100 for key, value in result.items()} return {k: round(v, 4) for k, v in result.items()} ๋‹ค์Œ์œผ๋กœ sequence-to-sequence ์ž‘์—…์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ(data collator)๋ฅผ ์ •์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. mT5๋Š” ์ธ์ฝ”๋”-๋””์ฝ”๋” ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์น˜(batches)๋ฅผ ์ค€๋น„ํ•  ๋•Œ ํ•œ ๊ฐ€์ง€ ๋ฏธ๋ฌ˜ํ•˜์ง€๋งŒ ์ค‘์š”ํ•œ ์ ์€ ๋””์ฝ”๋”ฉ ํ•˜๋Š” ๋™์•ˆ ๋ ˆ์ด๋ธ”์„ ํ•˜๋‚˜์”ฉ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋””์ฝ”๋”๊ฐ€ ๋ชจ๋ธ์ด ์‰ฝ๊ฒŒ ๊ธฐ์–ตํ•  ์ˆ˜ ์žˆ๋Š” ํ˜„์žฌ ๋˜๋Š” ๋ฏธ๋ž˜ ๋ ˆ์ด๋ธ”์ด ์•„๋‹Œ ์ด์ „ ์ •๋‹ต ๋ ˆ์ด๋ธ”๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง(causal language modeling)๊ณผ ๊ฐ™์€ ์ž‘์—…์˜ ์ž…๋ ฅ์— ๋งˆ์Šคํฌ ๋œ ์…€ํ”„ ์–ดํ…์…˜(masked self-attention)์ด ์ ์šฉ๋˜๋Š” ๋ฐฉ์‹๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ๋„, Transformers๋Š” ์ž…๋ ฅ๊ณผ ๋ ˆ์ด๋ธ”์„ ๋™์ ์œผ๋กœ ์ฑ„์šฐ๋Š” DataCollatorForSeq2Seq ์ฝœ๋ ˆ ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฝœ๋ ˆ ์ดํ„ฐ๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋ ค๋ฉด ํ† ํฌ ๋‚˜์ด์ €์™€ ๋ชจ๋ธ์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: from transformers import DataCollatorForSeq2Seq data_collator = DataCollatorForSeq2Seq(tokenizer, model=model) ์ž‘์€ ์˜ˆ์ œ ๋ฐฐ์น˜๊ฐ€ ์ž…๋ ฅ๋  ๋•Œ ์ด ์ฝœ๋ ˆ ์ดํ„ฐ๊ฐ€ ๋ฌด์—‡์„ ์ œ๊ณตํ•˜๋Š”์ง€ ๋ด…์‹œ๋‹ค. ๋จผ์ € ๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋œ ์—ด์„ ์ œ๊ฑฐํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด๋Š” ์ฝœ๋ ˆ ์ดํ„ฐ๊ฐ€ ์ด๋Ÿฌํ•œ ์š”์†Œ๋ฅผ ํŒจ๋”ฉ(padding) ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค: tokenized_datasets = tokenized_datasets.remove_columns( books_dataset["train"].column_names ) ์ฝœ๋ ˆ ์ดํ„ฐ๋Š” dicts์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•˜๊ณ  ๊ฐ dict๋Š” ๋ฐ์ดํ„ฐ ์…‹์˜ ๋‹จ์ผ ์˜ˆ์ œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฏ€๋กœ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ฝœ๋ ˆ ์ดํ„ฐ์— ์ „๋‹ฌํ•˜๊ธฐ ์ „์— ์š”๊ตฌํ•˜๋Š”<NAME>์œผ๋กœ ๋žญ๊ธ€๋ง(wrangle)ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: features = [tokenized_datasets["train"][i] for i in range(2)] data_collator(features) ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•ด์•ผ ํ•  ์ ์€ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ๊ฐ€ ๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ๋ณด๋‹ค ๊ธธ๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ์˜ input_ids์™€ attention_mask๊ฐ€ ์˜ค๋ฅธ์ชฝ์— [PAD] ํ† ํฐ(ID๊ฐ€ 0์ธ)์œผ๋กœ ์ฑ„์›Œ์ ธ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ labels์ด -100์œผ๋กœ ์ฑ„์›Œ์ ธ์„œ ํŒจ๋”ฉ ํ† ํฐ์ด ์†์‹ค ํ•จ์ˆ˜์— ์˜ํ•ด ๋ฌด์‹œ๋˜๋Š”์ง€๋ฅผ ํ™•์‹คํžˆ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์— [PAD] ํ† ํฐ์„ ์‚ฝ์ž…ํ•˜์—ฌ ๋ ˆ์ด๋ธ”์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•œ ์ƒˆ๋กœ์šด decoder_input_ids๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋“œ๋””์–ด ํ•™์Šต์— ํ•„์š”ํ•œ ๋ชจ๋“  ์ค€๋น„๊ฐ€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค! ์ด์ œ ํ‘œ์ค€ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค๋กœ trainer๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: from transformers import Seq2SeqTrainer trainer = Seq2SeqTrainer( model, args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, tokenizer=tokenizer, compute_metrics=compute_metrics, ) ํ•™์Šต์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: trainer.train() ํ•™์Šตํ•˜๋Š” ๋™์•ˆ ์†์‹ค์ด ๊ฐ์†Œํ•˜๊ณ  ROUGE ์ ์ˆ˜๊ฐ€ ๊ฐ ์—ํฌํฌ์— ๋”ฐ๋ผ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต์ด ์™„๋ฃŒ๋˜๋ฉด trainer.evaluate()๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ตœ์ข… ROUGE ์ ์ˆ˜๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: trainer.evaluate() ์šฐ๋ฆฌ ๋ชจ๋ธ์ด lead-3 ๋ฒ ์ด์Šค๋ผ์ธ์„ ์‰ฝ๊ฒŒ ๋Šฅ๊ฐ€ํ–ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ข‹์Šต๋‹ˆ๋‹ค! ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•  ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋ธ ๊ฐ€์ค‘์น˜๋ฅผ Hub๋กœ ํ‘ธ์‹œ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. trainer.push_to_hub(commit_message="Training complete", tags="summarization") ๋ชจ๋“  ํŒŒ์ผ์„ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•˜๊ธฐ ์ „์— ์œ„ ์ฝ”๋“œ๋Š” ์ฒดํฌํฌ์ธํŠธ ๋ฐ ๊ตฌ์„ฑ ํŒŒ์ผ์„ output_dir์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. tags ์ธ์ˆ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ—ˆ๋ธŒ์˜ ์œ„์ ฏ์ด mT5 ์•„ํ‚คํ…์ฒ˜์™€ ๊ด€๋ จ๋œ ๊ธฐ๋ณธ ํ…์ŠคํŠธ ์ƒ์„ฑ ์šฉ์ด ์•„๋‹ˆ๋ผ ์š”์•ฝ ํŒŒ์ดํ”„๋ผ์ธ์šฉ ์œ„์ ฏ์ด ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ tags์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ Hub ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. trainer.push_to_hub()์˜ ์ถœ๋ ฅ์€ Git ์ปค๋ฐ‹ ํ•ด์‹œ์— ๋Œ€ํ•œ URL์ด๋ฏ€๋กœ ๋ชจ๋ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ์ ์šฉ๋œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ด์ œ ์ด ์„น์…˜์„ ๋งˆ๋ฌด๋ฆฌํ•˜๊ธฐ ์ „์—, Accelerate์—์„œ ์ œ๊ณตํ•˜๋Š” ์ € ์ˆ˜์ค€ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ mT5๋ฅผ ๋ฏธ์„ธ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Accelerate๋ฅผ ์ด์šฉํ•œ mT5 ๋ฏธ์„ธ์กฐ์ • Accelerate๋กœ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์€ 3์žฅ์—์„œ ๋ณธ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์˜ˆ์ œ์™€ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ์ฐจ์ด์ ์€ ํ•™์Šต ์ค‘์— ์š”์•ฝ์„ ๋ช…์‹œ์ ์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ROUGE ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Seq2SeqTrainer๊ฐ€ ์ƒ์„ฑ ๊ณผ์ •์„ ์ฒ˜๋ฆฌํ–ˆ์Œ์„ ๊ธฐ์–ตํ•˜์‹ญ์‹œ์˜ค. Accelerate์—์„œ ์ด ๋‘ ๊ฐ€์ง€ ์š”๊ตฌ ์‚ฌํ•ญ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์„ ์œ„ํ•œ ๋ชจ๋“  ๊ฒƒ๋“ค์„ ์ค€๋น„ํ•˜๊ธฐ ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผ ํ•  ์ผ์€ ๊ฐ ๋ถ„ํ• (split)์— ๋Œ€ํ•œ DataLoader๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. PyTorch dataloaders๋Š” ํ…์„œ(tensors) ๋ฐฐ์น˜๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ ์…‹์—์„œ<NAME>์„ "torch"๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: tokenized_datasets.set_format("torch") ์ด์ œ ํ…์„œ๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ ์…‹์„ ์–ป์—ˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ์œผ๋กœ ํ•  ์ผ์€ DataCollatorForSeq2Seq๋ฅผ ๋‹ค์‹œ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋ชจ๋ธ์˜ ์ƒˆ๋กœ์šด ๋ฒ„์ „์„ ์ œ๊ณตํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์บ์‹œ์—์„œ ๋‹ค์‹œ ๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint) ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋กœ๋”๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from torch.utils.data import DataLoader batch_size = 8 train_dataloader = DataLoader( tokenized_datasets["train"], shuffle=True, collate_fn=data_collator, batch_size=batch_size, ) eval_dataloader = DataLoader( tokenized_datasets["validation"], collate_fn=data_collator, batch_size=batch_size ) ๋‹ค์Œ์œผ๋กœ ํ•  ์ผ์€ ์‚ฌ์šฉํ•  ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์˜ˆ์—์„œ์™€ ๊ฐ™์ด ๋Œ€๋ถ€๋ถ„์˜ ๋ฌธ์ œ์— ์ž˜ ์ž‘๋™ํ•˜๋Š” AdamW๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: from torch.optim import AdamW optimizer = AdamW(model.parameters(), lr=2e-5) ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋ธ, ์˜ตํ‹ฐ๋งˆ์ด์ € ๋ฐ dataloader๋ฅผ accelerator.prepare() ๋ฉ”์„œ๋“œ์— ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค: from accelerate import Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader ) TPU์—์„œ ํ•™์Šตํ•˜๋Š” ๊ฒฝ์šฐ ์œ„์˜ ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ์ „์šฉ ํ•™์Šต ํ•จ์ˆ˜๋กœ ์ด๋™ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ 3์žฅ์„ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. ์ด์ œ ๋ชจ๋“  ๊ฐ์ฒด๋“ค์„ ์ค€๋น„ํ–ˆ์œผ๋ฏ€๋กœ ์„ธ ๊ฐ€์ง€ ์ž‘์—…์ด ๋‚จ์•„ ์žˆ์Šต๋‹ˆ๋‹ค: ํ•™์Šต๋ฅ  ์Šค์ผ€์ค„(learning rate schedule)์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€๋ฅผ ์œ„ํ•ด ์š”์•ฝ์„ ํ›„์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ํ‘ธ์‹œ ํ•  ์ˆ˜ ์žˆ๋Š” ํ—ˆ๋ธŒ์— ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ•™์Šต๋ฅ  ์Šค์ผ€์ค„์˜ ๊ฒฝ์šฐ ์ด์ „ ์„น์…˜๊ณผ ๊ฐ™์ด ํ‘œ์ค€ ์„ ํ˜• ์ผ์ •์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import get_scheduler num_train_epochs = 10 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) ํ›„์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ƒ์„ฑ๋œ ์š”์•ฝ์„ ์ค„ ๋ฐ”๊ฟˆ์œผ๋กœ ๊ตฌ๋ถ„๋œ ๋ฌธ์žฅ์œผ๋กœ ๋ถ„ํ• ํ•˜๋Š” ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ROUGE ๋ฉ”ํŠธ๋ฆญ์ด ์š”๊ตฌํ•˜๋Š”<NAME>์ด๋ฉฐ ๋‹ค์Œ ์ฝ”๋“œ ์Šค๋‹ˆํŽซ์œผ๋กœ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: def postprocess_text(preds, labels): preds = [pred.strip() for pred in preds] labels = [label.strip() for label in labels] # ROUGE๋Š” ๊ฐ ๋ฌธ์žฅ๋งˆ๋‹ค ๊ฐœํ–‰๋ฌธ์ž๊ฐ€ ๋“ค์–ด๊ฐˆ ๊ฒƒ์„ ์š”๊ตฌํ•œ๋‹ค. preds = ["\n".join(nltk.sent_tokenize(pred)) for pred in preds] labels = ["\n".join(nltk.sent_tokenize(label)) for label in labels] return preds, labels Seq2SeqTrainer์˜ compute_metrics() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ ๋ฐฉ๋ฒ•์„ ๊ธฐ์–ตํ•˜๋ฉด ์ต์ˆ™ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ Hugging Face Hub์— ๋ชจ๋ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด Hub ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์žฅ์†Œ์˜ ์ด๋ฆ„์„ ์ •์˜ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” ์ €์žฅ์†Œ ID์™€ ์‚ฌ์šฉ์ž ํ”„๋กœํ•„์„ ๊ฒฐํ•ฉํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค: from huggingface_hub import get_full_repo_name model_name = "test-bert-finetuned-squad-accelerate" repo_name = get_full_repo_name(model_name) repo_name ์ด์ œ ์ด ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ์•„ํ‹ฐํŒฉํŠธ(artifacts)๋ฅผ ์ €์žฅํ•  ๊ฒฐ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋กœ์ปฌ ๋ฒ„์ „์„ ๋ณต์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from huggingface_hub import Repository output_dir = "results-mt5-finetuned-squad-accelerate" repo = Repository(output_dir, clone_from=repo_name) ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•™์Šต ์ค‘์— repo.push_to_hub() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์•„ํ‹ฐํŒฉํŠธ๋ฅผ ํ—ˆ๋ธŒ๋กœ ๋‹ค์‹œ ํ‘ธ์‹œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ด์ œ ํ•™์Šต ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๋ถ„์„์„ ๋งˆ๋ฌด๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ๋ฃจํ”„ (Training loop) ์š”์•ฝ์„ ์œ„ํ•œ ํ•™์Šต ๋ฃจํ”„๋Š” ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์‚ดํŽด๋ณธ ๋‹ค๋ฅธ Accelerate ์˜ˆ์ œ์™€ ์œ ์‚ฌํ•˜๋ฉฐ, ๋Œ€์ฒด๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹จ๊ณ„๋กœ ๊ตฌ๋ถ„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ๊ฐ epoch์— ๋Œ€ํ•ด train_dataloader์˜ ๋ชจ๋“  ์˜ˆ์ œ๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค. ๊ฐ ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค ๋ชจ๋ธ ์š”์•ฝ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ํ† ํฐ๋“ค์„ ์ƒ์„ฑํ•˜๊ณ  ์ด๋“ค์„ ํ…์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉ ํ•ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ ๋ณธ ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ ROUGE ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ชจ๋“  ๊ฒƒ์„ ํ—ˆ๋ธŒ๋กœ ํ‘ธ์‹œ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ Repository ๊ฐ์ฒด์— blocking=False๋ฅผ ์ง€์ •ํ•˜์—ฌ ์—ํฌํฌ๋‹น ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ํ‘ธ์‹œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ธฐ๊ฐ€๋ฐ”์ดํŠธ ํฌ๊ธฐ์˜ ๋ชจ๋ธ์„ ์—…๋กœ๋“œ ์™„๋ฃŒํ•˜๊ธฐ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆด ํ•„์š” ์—†์ด ํ•™์Šต์„ ๊ณ„์†ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์œ„ ๋‹จ๊ณ„๋Š” ๋‹ค์Œ ์ฝ”๋“œ ๋ธ”๋ก์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from tqdm.auto import tqdm import torch import numpy as np progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): # ํ•™์Šต model.train() for step, batch in enumerate(train_dataloader): outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) # ํ‰๊ฐ€ model.eval() for step, batch in enumerate(eval_dataloader): with torch.no_grad(): generated_tokens = accelerator.unwrap_model(model).generate( batch["input_ids"], attention_mask=batch["attention_mask"], ) generated_tokens = accelerator.pad_across_processes( generated_tokens, dim=1, pad_index=tokenizer.pad_token_id ) labels = batch["labels"] # ๋งŒ์ผ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉ ํ•˜์ง€ ์•Š์•˜์œผ๋ฉด ๋ ˆ์ด๋ธ”๋„ ์—ญ์‹œ ํŒจ๋”ฉ ํ•ด์•ผ ํ•œ๋‹ค. labels = accelerator.pad_across_processes( batch["labels"], dim=1, pad_index=tokenizer.pad_token_id ) generated_tokens = accelerator.gather(generated_tokens).cpu().numpy() labels = accelerator.gather(labels).cpu().numpy() # ๋ ˆ์ด๋ธ” ๋‚ด์˜ -100์„ ๋ชจ๋‘ ๊ต์ฒดํ•œ๋‹ค. labels = np.where(labels != -100, labels, tokenizer.pad_token_id) if isinstance(generated_tokens, tuple): generated_tokens = generated_tokens[0] decoded_preds = tokenizer.batch_decode( generated_tokens, skip_special_tokens=True ) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) decoded_preds, decoded_labels = postprocess_text( decoded_preds, decoded_labels ) rouge_score.add_batch(predictions=decoded_preds, references=decoded_labels) # ๋ฉ”ํŠธ๋ฆญ ๊ณ„์‚ฐ result = rouge_score.compute() # ์ค‘๊ฐ„๊ฐ’ ROUGE ์ ์ˆ˜๋ฅผ ์ถ”์ถœ result = {key: value.mid.fmeasure * 100 for key, value in result.items()} result = {k: round(v, 4) for k, v in result.items()} print(f"Epoch {epoch}:", result) # ์ €์žฅ ๋ฐ ์—…๋กœ๋“œ accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=f"Training in progress epoch {epoch}", blocking=False ) ์ด๊ฒŒ ๋์ž…๋‹ˆ๋‹ค! ์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด Trainer๋กœ ์–ป์€ ๊ฒƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ๋ชจ๋ธ๊ณผ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฏธ์„ธ ์กฐ์ •๋œ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ๋ชจ๋ธ์„ Hub๋กœ ํ‘ธ์‹œ ํ•œ ํ›„์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๋ก  ์œ„์ ฏ ๋˜๋Š” ํŒŒ์ดํ”„๋ผ์ธ ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import pipeline hub_model_id = "huggingface-course/mt5-small-finetuned-amazon-en-es" summarizer = pipeline("summarization", model=hub_model_id) ์šฐ๋ฆฌ๋Š” ์š”์•ฝ ๊ฒฐ๊ณผ ํ’ˆ์งˆ์ด ์–ด๋–ค์ง€ ์‚ดํŽด๋ณด๊ธฐ ์œ„ํ•ด ํ…Œ์ŠคํŠธ์…‹(๋ชจ๋ธ์ด ๋ณธ ์ ์ด ์—†๋Š”)์˜ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ํŒŒ์ดํ”„๋ผ์ธ์— ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๋ฆฌ๋ทฐ, ์ œ๋ชฉ ๋ฐ ์ƒ์„ฑ๋œ ์š”์•ฝ์„ ํ•จ๊ป˜ ํ‘œ์‹œํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: def print_summary(idx): review = books_dataset["test"][idx]["review_body"] title = books_dataset["test"][idx]["review_title"] summary = summarizer(books_dataset["test"][idx]["review_body"])[0]["summary_text"] print(f"'>>> Review: {review}'") print(f"\n'>>> Title: {title}'") print(f"\n'>>> Summary: {summary}'") ์˜์–ด ์˜ˆ์‹œ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print_summary(100) ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜์ง€ ์•Š๋„ค์š”! ์šฐ๋ฆฌ๋Š” ๋ชจ๋ธ์ด ์ƒˆ๋กœ์šด ๋‹จ์–ด๋กœ ๋ฆฌ๋ทฐ์˜ ์ผ๋ถ€๋ฅผ ๋ณด๊ฐ•ํ•จ์œผ๋กœ์จ ์‹ค์ œ๋กœ ์ถ”์ƒ์ ์ธ ์š”์•ฝ(abstractive summarization)์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•„๋งˆ๋„ ์šฐ๋ฆฌ ๋ชจ๋ธ์˜ ๊ฐ€์žฅ ๋ฉ‹์ง„ ์ธก๋ฉด์€ ์ด์ค‘ ์–ธ์–ด๋ฅผ ์ œ๊ณตํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ŠคํŽ˜์ธ์–ด ๋ฆฌ๋ทฐ์˜ ์š”์•ฝ์„ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค: print_summary(0) ์š”์•ฝ ๊ฒฐ๊ณผ๋Š” ์˜์–ด๋กœ "Very easy to read"๋กœ ๋ฒˆ์—ญ๋˜๋ฉฐ, ์ด ๊ฒฝ์šฐ์—๋Š” ๋ฆฌ๋ทฐ์—์„œ ์ง์ ‘ ์ถ”์ถœ๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ด ๊ฒฐ๊ณผ๋Š” mT5 ๋ชจ๋ธ์˜ ๋‹ค์žฌ๋‹ค๋Šฅํ•จ์„ ๋ณด์—ฌ์ฃผ๊ณ  ๋‹ค๊ตญ์–ด ๋ง๋ญ‰์น˜๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์ด ์–ด๋–ค ๊ฒƒ์ธ์ง€ ๋ง›๋ณด๊ฒŒ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค! ๋‹ค์Œ ์„น์…˜์—์„œ ์–ธ์–ด ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•˜๋Š”(from scratch) ์•ฝ๊ฐ„ ๋” ๋ณต์žกํ•œ ์ž‘์—…์— ๊ด€์‹ฌ์„ ๊ฐ€์ ธ๋ด…์‹œ๋‹ค. 5. ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ(Causal Language Model)์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•˜๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์ž‘์—… ๊ณผ์ •์—์„œ ์‚ฌ์ „ ํ•™์Šต๋œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‚ฌ์ „ ํ•™์Šต ๊ฐ€์ค‘์น˜๋ฅผ ์žฌ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์‚ฌ์šฉ ์‚ฌ๋ก€(use case)์— ๋งž๊ฒŒ ์ ์ ˆํ•˜๊ฒŒ ๋ฏธ์„ธ ์กฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. 1์žฅ์—์„œ ๋ณด์•˜๋“ฏ์ด ์ด๋ฅผ ์ผ๋ฐ˜์ ์œผ๋กœ ์ „์ด ํ•™์Šต(transfer learning)์ด๋ผ๊ณ  ํ•˜๋ฉฐ ๋ ˆ์ด๋ธ”์ด ์ง€์ •๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ€์กฑํ•œ ๋Œ€๋ถ€๋ถ„์˜ ์‹ค์ œ ์‚ฌ์šฉ ์‚ฌ๋ก€(use case)์— Transformer ๋ชจ๋ธ์„ ์ ์šฉํ•˜๋Š” ๋งค์šฐ ์„ฑ๊ณต์ ์ธ ์ „๋žต์ž…๋‹ˆ๋‹ค. ์ด ์žฅ์—์„œ๋Š” ์ด์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋‹น์‹ ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ถฉ๋ถ„ํžˆ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ๊ธฐ์กด ์‚ฌ์ „ ํ•™์Šต ๋ชจ๋ธ์— ์‚ฌ์šฉ๋œ ๋ฐ์ดํ„ฐ์™€ ๋งค์šฐ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ์ผ ๊ฒฝ์šฐ ์ข‹์€ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ธฐ์กด ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์ „ ํ•™์Šตํ•˜๋Š” ๋ฐ ํ›จ์”ฌ ๋” ๋งŽ์€ ์ปดํ“จํŒ… ๋ฆฌ์†Œ์Šค๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ ๋ชจ๋ธ์„ ์ƒˆ๋กญ๊ฒŒ ์‚ฌ์ „ ํ•™์Šต์‹œํ‚ค๋Š” ๊ฒƒ์ด ํ•ฉ๋ฆฌ์ ์ผ ์ˆ˜ ์žˆ๋Š” ์˜ˆ๋กœ๋Š” ์Œํ‘œ, DNA์™€ ๊ฐ™์€ ๋ถ„์ž ์„œ์—ด ๋˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฝ”๋“œ ๋“ฑ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ ์…‹๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ›„์ž๋Š” ๊ธด ์ฝ”๋“œ ์‹œํ€€์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” OpenAI์˜ Codex ๋ชจ๋ธ๋กœ ๊ตฌ๋™๋˜๋Š” TabNine ๋ฐ GitHub์˜ Copilot๊ณผ ๊ฐ™์€ ๋„๊ตฌ ๋•๋ถ„์— ์ตœ๊ทผ ์ฃผ๋ชฉ์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ…์ŠคํŠธ ์ƒ์„ฑ ์ž‘์—…์€ GPT-2์™€ ๊ฐ™์€ ์ž๋™ ํšŒ๊ท€(auto-regressive) ๋˜๋Š” ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ(causal language model)๋กœ ๊ฐ€์žฅ ์ž˜ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ์ฝ”๋“œ ์ƒ์„ฑ ๋ชจ๋ธ์˜ ์ถ•์†Œ ๋ฒ„์ „์„ ๊ตฌ์ถ•ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. Python ์ฝ”๋“œ์˜ ํ•˜์œ„ ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ „์ฒด ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค ๋Œ€์‹  ํ•œ ์ค„ ์™„์„ฑ(one-line completions)์— ์ค‘์ ์„ ๋‘˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Python์—์„œ ๋ฐ์ดํ„ฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ matplotlib, seaborn, pandas ๋ฐ scikit-learn ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๊ตฌ์„ฑ๋œ Python ๋ฐ์ดํ„ฐ ๊ณผํ•™ ์ฝ”๋“œ ์ง‘ํ•ฉ๊ณผ ์ž์ฃผ ์ ‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํŠน์ • ๋ช…๋ น์„ ์กฐํšŒํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋ฏ€๋กœ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋Ÿฌํ•œ ์š”๊ตฌ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ข‹์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 6์žฅ์—์„œ ์šฐ๋ฆฌ๋Š” Python ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํšจ์œจ์ ์ธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋งŒ๋“ค์—ˆ์ง€๋งŒ ์—ฌ์ „ํžˆ ํ•„์š”ํ•œ ๊ฒƒ์€ ๋ชจ๋ธ์„ ์‚ฌ์ „ ํ•™์Šตํ•  ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ ์…‹์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ํŒŒ์ƒ๋œ Python ์ฝ”๋“œ ๋ชจ์Œ์— ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ Trainer API์™€ Accelerate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ์‹œ๋„ํ•ด ๋ด…์‹œ๋‹ค! ์‹ค์ œ๋กœ ์ด ์„น์…˜์— ํ‘œ์‹œ๋œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต๋˜๊ณ  Hub์— ์—…๋กœ๋“œ๋œ ๋ชจ๋ธ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์ƒ์„ฑ์—์„œ ์•ฝ๊ฐ„์˜ ๋ฌด์ž‘์œ„ํ™”(rnadomization)๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ชจ์œผ๊ธฐ Python ์ฝ”๋“œ๋Š” GitHub์™€ ๊ฐ™์€ ์ฝ”๋“œ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ํ’๋ถ€ํ•˜๊ฒŒ ์ œ๊ณต๋˜๋ฉฐ, ๋ชจ๋“  Python ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์Šคํฌ๋žฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋Œ€ํ˜• GPT-2 ๋ชจ๋ธ์„ ์‚ฌ์ „ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•ด Transformers ๊ต๊ณผ์„œ์—์„œ ์ทจํ•œ ์ ‘๊ทผ ๋ฐฉ์‹์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ž‘์„ฑ์ž๋Š” codeparrot์ด๋ผ๋Š” ์•ฝ 2์ฒœ๋งŒ ๊ฐœ์˜ Python ํŒŒ์ผ์ด ํฌํ•จ๋œ ์•ฝ 180GB์˜ GitHub ๋คํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๊ตฌ์ถ•ํ•œ ๋‹ค์Œ Hugging Face Hub์—์„œ ๊ณต์œ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ „์ฒด ๋ง๋ญ‰์น˜์— ๋Œ€ํ•œ ํ•™์Šต์€ ๊ณ„์‚ฐ๋Ÿ‰๊ณผ ํ•™์Šต ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ๋งŽ์ด ๋“ค๊ธฐ ๋•Œ๋ฌธ์—, ์—ฌ๊ธฐ์„œ๋Š” Python ๋ฐ์ดํ„ฐ ๊ณผํ•™ ์Šคํƒ๊ณผ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ ์…‹์˜ ํ•˜์œ„ ์ง‘ํ•ฉ๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ์Šคํƒ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ชจ๋“  ํŒŒ์ผ์— ๋Œ€ํ•ด codeparrot ๋ฐ์ดํ„ฐ ์…‹์„ ํ•„ํ„ฐ๋งํ•˜์—ฌ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์˜ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์šด๋กœ๋“œ๋ฅผ ํ”ผํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  ์ŠคํŠธ๋ฆฌ๋ฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฆ‰์„์—์„œ ํ•„ํ„ฐ๋งํ•ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ ์–ธ๊ธ‰ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ ์ƒ˜ํ”Œ์„ ํ•„ํ„ฐ๋งํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋„๋ก ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: def any_keyword_in_string(string, keywords): for keyword in keywords: if keyword in string: return True return False ๋‘ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๊ฐ€์ง€๊ณ  ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: filters = ["pandas", "sklearn", "matplotlib", "seaborn"] example_1 = "import numpy as np" example_2 = "import pandas as pd" print(any_keyword_in_string(example_1, filters), any_keyword_in_string(example_2, filters)) ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ์ŠคํŠธ๋ฆฌ๋ฐํ•˜๊ณ  ์›ํ•˜๋Š” ์š”์†Œ๋ฅผ ํ•„ํ„ฐ๋งํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from collections import defaultdict from tqdm import tqdm from datasets import Dataset def filter_streaming_dataset(dataset, filters): filtered_dict = defaultdict(list) total = 0 for sample in tqdm(iter(dataset)): total += 1 if any_keyword_in_string(sample["content"], filters): for k, v in sample.items(): filtered_dict[k].append(v) print(f"{len(filtered_dict['content'])/total:.2%} of data after filtering.") return Dataset.from_dict(filtered_dict) ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด ํ•จ์ˆ˜๋ฅผ ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ ์…‹์— ๊ฐ„๋‹จํžˆ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: # ์ด ์ฝ”๋“œ ์…€์€ ์‹คํ–‰ ์‹œ๊ฐ„์ด ๋งค์šฐ ๊น๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ๋ƒฅ ์ƒ๋žตํ•˜๊ณ  ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ€์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค! from dataset import load_dataset split = "train" filters = ["pandas", "sklearn", "matplotlib", "seaborn"] data = load_dataset(f"transformersbook/codeparrot-{split}", split=split, streaming=True) filtered_data = filter_streaming_dataset(data, filters) ์ด๋ ‡๊ฒŒ ํ•„ํ„ฐ๋งํ•˜๋ฉด ์›๋ณธ ๋ฐ์ดํ„ฐ ์…‹์˜ ์•ฝ 3%๊ฐ€ ๋‚จ๊ฒŒ ๋˜๋‚˜ ์ด ์—ญ์‹œ๋„ ์—ฌ์ „ํžˆ ์ƒ๋‹นํ•œ ๊ทœ๋ชจ์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ ์…‹์€ 6GB์ด๊ณ  600,000๊ฐœ์˜ Python ์Šคํฌ๋ฆฝํŠธ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค! ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ํ•„ํ„ฐ๋งํ•˜๋Š” ๋ฐ ์ปดํ“จํ„ฐ์™€ ๋Œ€์—ญํญ์— ๋”ฐ๋ผ 2-3์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋งŽ์€ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์ง€ ์•Š๋‹ค๋ฉด ํ—ˆ๋ธŒ์—์„œ ํ•„ํ„ฐ๋ง ๋œ ๋ฐ์ดํ„ฐ ์…‹์„ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_dataset, DatasetDict ds_train = load_dataset("huggingface-course/codeparrot-ds-train", split="train") ds_valid = load_dataset("huggingface-course/codeparrot-ds-valid", split="validation") raw_datasets = DatasetDict( { "train": ds_train.shuffle().select(range(50000)), "valid": ds_valid.shuffle().select(range(500)) } ) raw_datasets ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์ „ ํ•™์Šตํ•˜๋Š” ๋ฐ๋Š” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. ๋จผ์ € ์œ„์˜ ๋‘ ๋ถ€๋ถ„ ํ–‰์˜ ์ฃผ์„์„ ์ œ๊ฑฐํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ƒ˜ํ”Œ์—์„œ ํ•™์Šต ๋ฃจํ”„๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ํ•™์Šต์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋˜๊ณ  ๋ชจ๋ธ์ด ์ €์žฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํด๋”๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ์žŠ์—ˆ๊ฑฐ๋‚˜ ํ•™์Šต ๋ฃจํ”„์˜ ๋์— ์˜คํƒ€๊ฐ€ ์žˆ์–ด์„œ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์—์„œ ํ•™์Šต ์‹คํ–‰์ด ์‹คํŒจํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ์‹ค๋ง์Šค๋Ÿฌ์šด ๊ฒƒ์€ ์—†์Šต๋‹ˆ๋‹ค! ์ด์ œ ๋‹ค์šด๋กœ๋“œํ•œ ๋ฐ์ดํ„ฐ ์…‹์˜ ๋‚ด์šฉ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ ํ•„๋“œ์˜ ์ฒ˜์Œ 200์ž๋งŒ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค: for key in raw_datasets["train"][0]: print(f"{key.upper()}: {raw_datasets['train'][0][key][:200]}") content ํ•„๋“œ์— ๋ชจ๋ธ์ด ํ•™์Šตํ•  ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ ์…‹์ด ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์ „ ํ•™์Šต์— ์ ํ•ฉํ•œ<NAME>์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ์ค€๋น„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹ ์ค€๋น„ํ•˜๊ธฐ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ† ํฐํ™”ํ•˜์—ฌ ํ•™์Šต์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชฉํ‘œ๋Š” ์ฃผ๋กœ ์งง์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์ž๋™ ์™„์„ฑํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ๋ฅผ ๋น„๊ต์  ์ž‘๊ฒŒ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์„ ํ›จ์”ฌ ๋น ๋ฅด๊ฒŒ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๊ณ  ํ›จ์”ฌ ์ ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค๋Š” ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋” ๋งŽ์€ ์ปจํ…์ŠคํŠธ๋ฅผ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ ๊ฒฝ์šฐ(์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจ๋ธ์ด ํ•จ์ˆ˜ ์ •์˜๊ฐ€ ์žˆ๋Š” ํŒŒ์ผ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์ž‘์„ฑํ•˜๋„๋ก ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ), ๊ทธ ์ˆ˜๋ฅผ ๋Š˜๋ ค์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒƒ์€ ๋” ํฐ ๊ทœ๋ชจ์˜ GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋ช…์‹ฌํ•˜์„ธ์š”. ์ง€๊ธˆ์€ ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ๋ฅผ GPT-2 ๋˜๋Š” GPT-3์—์„œ ๊ฐ๊ฐ ์‚ฌ์šฉ๋˜๋Š” 1,024 ๋˜๋Š” 2,048๊ณผ ๋‹ฌ๋ฆฌ 128 ํ† ํฐ์œผ๋กœ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ฌธ์„œ์—๋Š” 128๊ฐœ ์ด์ƒ์˜ ํ† ํฐ์ด ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์ž…๋ ฅ์„ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ž๋ฅด๋ฉด ๋ฐ์ดํ„ฐ ์…‹์˜ ๋งŽ์€ ๋ถ€๋ถ„์ด ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ๋Œ€์‹  6์žฅ์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ return_overflowing_tokens ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ „์ฒด ์ž…๋ ฅ์„ ํ† ํฐํ™”ํ•˜๊ณ  ์—ฌ๋Ÿฌ ์ฒญํฌ๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ return_length ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ๋œ ๊ฐ ์ฒญํฌ์˜ ๊ธธ์ด๋ฅผ ์ž๋™์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ข…์ข… ๋งˆ์ง€๋ง‰ ์ฒญํฌ๋Š” ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ๋ณด๋‹ค ์ž‘์œผ๋ฉฐ ํŒจ๋”ฉ ๋ฌธ์ œ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์ด๋Ÿฌํ•œ ์กฐ๊ฐ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์–ด์จŒ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ํฌ๊ฒŒ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์˜ ์ฒ˜์Œ ๋‘ ๊ฐœ์˜ ์˜ˆ์ œ๋ฅผ ๋ณด๊ณ  ์ด๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ •ํ™•ํžˆ ๋ด…์‹œ๋‹ค: from transformers import AutoTokenizer context_length = 128 tokenizer = AutoTokenizer.from_pretrained("huggingface-course/code-search-net-tokenizer") outputs = tokenizer( raw_datasets["train"][:2]["content"], truncation=True, max_length=context_length, return_overflowing_tokens=True, return_length=True, ) print(f"Input IDs length: {len(outputs['input_ids'])}") print(f"Input chunk lengths: {(outputs['length'])}") print(f"Chunk mapping: {outputs['overflow_to_sample_mapping']}") ์ด ๋‘ ๊ฐ€์ง€ ์˜ˆ์—์„œ ์ด 34๊ฐœ์˜ ์„ธ๊ทธ๋จผํŠธ๋ฅผ ์–ป์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒญํฌ ๊ธธ์ด๋ฅผ ๋ณด๋ฉด ๋‘ ๋ฌธ์„œ์˜ ๋์— ์žˆ๋Š” ์ฒญํฌ์— 128๊ฐœ ๋ฏธ๋งŒ์˜ ํ† ํฐ์ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(๊ฐ๊ฐ 117 ๋ฐ 41). ์ด ์ˆซ์ž๋Š” ์šฐ๋ฆฌ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ „์ฒด ์ฒญํฌ์˜ ์ผ๋ถ€๋ถ„์— ๋ถˆ๊ณผํ•˜๋ฏ€๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ๋ฒ„๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. overflow_to_sample_mapping ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ค ์ฒญํฌ๊ฐ€ ์–ด๋–ค ์ž…๋ ฅ ์ƒ˜ํ”Œ์— ์†ํ•˜๋Š”์ง€ ์žฌ๊ตฌ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์—์„œ ์šฐ๋ฆฌ๋Š” Datasets์—์„œ Dataset.map() ํ•จ์ˆ˜์˜ ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ผ๋Œ€์ผ ๋งต์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์„น์…˜ 3์—์„œ ๋ณด์•˜๋“ฏ์ด ์ž…๋ ฅ ๋ฐฐ์น˜๋ณด๋‹ค ๋งŽ๊ฑฐ๋‚˜ ์ ์€ ์š”์†Œ๋กœ ๋ฐฐ์น˜๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์š”์†Œ ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ฆ๋Œ€(augmentation) ๋˜๋Š” ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ ๊ฐ ์š”์†Œ๋ฅผ ์ง€์ •๋œ ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ์˜ ์ฒญํฌ๋กœ ํ† ํฐํ™”ํ•  ๋•Œ ๊ฐ ๋ฌธ์„œ์—์„œ ๋งŽ์€ ์ƒ˜ํ”Œ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํฌ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด ์—ด์„ ์‚ญ์ œํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์ผ ์ด๋“ค์„ ์œ ์ง€ํ•˜๋ ค๋ฉด ์ ์ ˆํ•˜๊ฒŒ ๋ฐ˜๋ณตํ•˜๊ณ  Dataset.map() ํ˜ธ์ถœ ๋‚ด์—์„œ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: def tokenize(element): outputs = tokenizer( element['content'], truncation=True, max_length=context_length, return_overflowing_tokens=True, return_length=True, ) input_batch = [] for length, input_ids in zip(outputs['length'], outputs['input_ids']): if length == context_length: input_batch.append(input_ids) return {"input_ids": input_batch} tokenized_datasets = raw_datasets.map( tokenize, batched=True, remove_columns=raw_datasets["train"].column_names) tokenized_datasets ์ด์ œ ๊ฐ๊ฐ 128๊ฐœ์˜ ํ† ํฐ์ด ์žˆ๋Š” 1,670๋งŒ ๊ฐœ์˜ ์˜ˆ์ œ๊ฐ€ ์žˆ์œผ๋ฉฐ ์ด๋Š” ์•ฝ 21์–ต ๊ฐœ์˜ ํ† ํฐ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ OpenAI์˜ GPT-3์™€ Codex ๋ชจ๋ธ์€ ๊ฐ๊ฐ 3,000์–ต ๊ฐœ, 1,000์–ต ๊ฐœ ํ† ํฐ์— ๋Œ€ํ•ด ํ•™์Šต์„ ํ•˜๋ฉฐ, Codex ๋ชจ๋ธ์€ GPT-3 ์ฒดํฌํฌ์ธํŠธ์—์„œ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. ์ด ์„น์…˜์˜ ๋ชฉํ‘œ๋Š” ๊ธธ์ด๊ฐ€ ๊ธธ๊ณ  ์ผ๊ด€๋œ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋Ÿฌํ•œ ๋ชจ๋ธ๊ณผ ๊ฒฝ์Ÿํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ ๊ณผํ•™์ž์—๊ฒŒ ๋น ๋ฅธ ์ž๋™ ์™„์„ฑ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ์ถ•์†Œ ๋ฒ„์ „์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ ์…‹์ด ์ค€๋น„๋˜์—ˆ์œผ๋ฏ€๋กœ ๋ชจ๋ธ์„ ์„ค์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! โœ Try it out! ์ž‘์€ ๋ฒ”์œ„์˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ๋ณด๋‹ค ์ž‘์€ ๋ชจ๋“  ์ฒญํฌ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์€ ํฐ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ปจํ…์ŠคํŠธ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๋ฉด(๋˜๋Š” ์งง์€ ๋ฌธ์„œ ๋ชจ์Œ์ด ์žˆ๋Š” ๊ฒฝ์šฐ) ๋ฒ„๋ ค์ง€๋Š” ์ฒญํฌ์˜ ๋น„์œจ๋„ ๋Š˜์–ด๋‚ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•˜๋Š” ๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์€ ์‚ฌ์ด์— eos_token_id ํ† ํฐ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์ผ ๋ฐฐ์น˜ ๋‚ด์˜ ๋ชจ๋“  ํ† ํฐํ™”๋œ ์ƒ˜ํ”Œ์„ ๊ฒฐํ•ฉํ•œ ๋‹ค์Œ ๊ฒฐํ•ฉ๋œ ์‹œํ€€์Šค์—์„œ ์ฒญํ‚น์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฐ์Šต ์‚ผ์•„ ํ•ด๋‹น ์ ‘๊ทผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋„๋ก tokenize() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด ๋ด…์‹œ๋‹ค. truncation=False๋ฅผ ์„ค์ •ํ•˜๊ณ  ํ† ํฌ ๋‚˜์ด์ €์—์„œ ๋‹ค๋ฅธ ์ธ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ํ† ํฐ ID์˜ ์ „์ฒด ์‹œํ€€์Šค๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ๋ชจ๋ธ์˜ ์ดˆ๊ธฐํ™” ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” GPT-2 ๋ชจ๋ธ์„ ์ƒˆ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์†Œํ˜• GPT-2 ๋ชจ๋ธ๊ณผ ๋™์ผํ•œ ์„ค์ •์„ ์šฐ๋ฆฌ ๋ชจ๋ธ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์‚ฌ์ „ ํ•™์Šต๋œ ์„ค์ •์„ ๋กœ๋“œํ•˜๊ณ  ํ† ํฌ ๋‚˜์ด์ € ํฌ๊ธฐ๊ฐ€ ๋ชจ๋ธ ์–ดํœ˜ ํฌ๊ธฐ์™€ ์ผ์น˜ํ•˜๊ณ  bos ๋ฐ eos(์‹œํ€€์Šค์˜ ์‹œ์ž‘ ๋ฐ ๋) ํ† ํฐ ID๋ฅผ ์ „๋‹ฌํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoTokenizer, GPT2LMHeadModel, AutoConfig config = AutoConfig.from_pretrained( "gpt2", vocab_size=len(tokenizer), n_ctx=context_length, bos_token_id=tokenizer.bos_token_id, eos_token_id=tokenizer.eos_token_id ) ํ•ด๋‹น ์„ค์ •์œผ๋กœ ์ƒˆ ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์‹ค์ œ๋กœ ๋ชจ๋ธ์„ ์ง์ ‘ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— from_pretrained() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์€ ์ด๋ฒˆ์ด ์ฒ˜์Œ์ž…๋‹ˆ๋‹ค: model = GPT2LMHeadModel(config) model_size = sum(t.numel() for t in model.parameters()) print(f"GPT-2 size: {model_size/1000**2:.1f}M parameters") ์šฐ๋ฆฌ ๋ชจ๋ธ์—๋Š” ์ตœ์ ํ™”ํ•ด์•ผ ํ•  1์–ต 2400๋งŒ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์„ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๋ฐฐ์น˜ ์ƒ์„ฑ์„ ์ฒ˜๋ฆฌํ•  ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์œ„ํ•ด ํŠน๋ณ„ํžˆ ์„ค๊ณ„๋œ DataCollatorForLanguageModeling ์ฝœ๋ ˆ ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด). ๋ฐฐ์น˜๋ฅผ ์Œ“๊ณ  ํŒจ๋”ฉ ํ•˜๋Š” ๊ฒƒ ์™ธ์—๋„ ์–ธ์–ด ๋ชจ๋ธ ๋ ˆ์ด๋ธ” ์ƒ์„ฑ๋„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง(causal language modeling)์—์„œ ์ž…๋ ฅ์€ ๋ ˆ์ด๋ธ” ์—ญํ• ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๋Š” ํ•™์Šต ์ค‘์— ์ฆ‰์‹œ ์ž…๋ ฅ์„ ์ƒ์„ฑํ•˜๋ฏ€๋กœ input_ids๋ฅผ ๋ณต์ œํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. DataCollatorForLanguageModeling์€ ๋งˆ์Šคํฌ ์–ธ์–ด ๋ชจ๋ธ๋ง(MLM)๊ณผ ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ๋ง(CLM)์„ ๋ชจ๋‘ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ MLM ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜์ง€๋งŒ mlm=False ์ธ์ˆ˜๋ฅผ ์„ค์ •ํ•˜์—ฌ CLM์œผ๋กœ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from transformers import DataCollatorForLanguageModeling tokenizer.pad_token = tokenizer.eos_token data_collator = DataCollatorForLanguageModeling(tokenizer, mlm=False) ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: out = data_collator([tokenized_datasets["train"][i] for i in range(5)]) for key in out: print(f"{key} shape: {out[key].shape}") ์˜ˆ์ œ๊ฐ€ ๋ฐฐ์น˜๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์Œ“์˜€๊ณ  ๋ชจ๋“  ํ…์„œ์˜ ๋ชจ์–‘์ด ๊ฐ™์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. โš  ์ž…๋ ฅ๊ณผ ๋ ˆ์ด๋ธ”์„ ์‹œํ”„ํŒ…(shifting)ํ•˜์—ฌ ์ •๋ ฌ(align) ํ•˜๋Š” ๊ฒƒ์€ ๋ชจ๋ธ ๋‚ด๋ถ€์—์„œ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๋Š” ์ž…๋ ฅ์„ ๋ณต์‚ฌํ•˜์—ฌ ๋ ˆ์ด๋ธ”์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ์‹ค์ œ๋กœ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ชจ๋“  ๊ฒƒ์ด ์ค€๋น„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๊ทธ๋ ‡๊ฒŒ ๋งŽ์€ ์ž‘์—…์ด ํ•„์š”ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค! ํ•™์Šต์„ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— Hugging Face์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋…ธํŠธ๋ถ์—์„œ ์ž‘์—…ํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from huggingface_hub import notebook_login notebook_login() ๊ทธ๋Ÿฌ๋ฉด Hugging Face ๋กœ๊ทธ์ธ ์ž๊ฒฉ ์ฆ๋ช…์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์ ฏ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋‚จ์€ ์ผ์€ ํ•™์Šต ์ธ์ž(training arguments)๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  Trainer๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•ฝ๊ฐ„์˜ ์›Œ๋ฐ์—…๊ณผ 256์˜ ํšจ๊ณผ์ ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ(per_device_train_batch_size * gradient_accumulation_steps)๋กœ ์ฝ”์‚ฌ์ธ ํ•™์Šต๋ฅ  ์ผ์ •์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์ ์€ ๋‹จ์ผ ๋ฐฐ์น˜๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ๋งž์ง€ ์•Š์„ ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ ์—ฌ๋Ÿฌ ์ •๋ฐฉํ–ฅ/์—ญ๋ฐฉํ–ฅ ํŒจ์Šค๋ฅผ ํ†ตํ•ด ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๋ฅผ ์ ์ง„์ ์œผ๋กœ ๊ตฌ์ถ•ํ•ฉ๋‹ˆ๋‹ค. Accelerate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ๋ฃจํ”„๋ฅผ ๋งŒ๋“ค ๋•Œ ์ด๊ฒƒ์ด ์‹ค์ œ๋กœ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from transformers import Trainer, TrainingArguments args = TrainingArguments( output_dir="codeparrot-ds", per_device_train_batch_size=32, per_device_eval_batch_size=32, evaluation_strategy="steps", eval_steps=5000, logging_steps=5000, gradient_accumulation_steps=8, num_train_epochs=1, weight_decay=0.1, warmup_steps=1000, lr_scheduler_type="cosine", learning_rate=5e-4, save_steps=5000, fp16=True, push_to_hub=True, ) trainer = Trainer( model=model, tokenizer=tokenizer, args=args, data_collator=data_collator, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["valid"], ) ์ด์ œ Trainer๋ฅผ ์‹œ์ž‘ํ•˜๊ณ  ํ•™์Šต์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต ์ง‘ํ•ฉ์˜ ์ „์ฒด ํ˜น์€ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์—์„œ ์‹คํ–‰ํ•˜๋Š”์ง€์— ๋”ฐ๋ผ ๊ฐ๊ฐ 20์‹œ๊ฐ„ ๋˜๋Š” 2์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋ฏ€๋กœ ์ปคํ”ผ ๋ช‡ ์ž”๊ณผ ์ฝ์„ ๋งŒํ•œ ์ข‹์€ ์ฑ…์„ ๊ฐ€์ ธ์˜ค์„ธ์š”! trainer.train() ํ•™์Šต์ด ์™„๋ฃŒ๋˜๋ฉด ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ—ˆ๋ธŒ๋กœ ํ‘ธ์‹œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. โœ Try it out! ์›์‹œ ํ…์ŠคํŠธ์—์„œ GPT-2๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐ์—๋Š” TrainingArguments ์™ธ์— ์•ฝ 30์ค„์˜ ์ฝ”๋“œ๋งŒ ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž์‹ ์˜ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ์‹œ๋„ํ•ด ๋ณด๊ณ  ์ข‹์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค! GPU๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ์ปดํ“จํ„ฐ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ์ปดํ“จํ„ฐ์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด ๋ณด์„ธ์š”. Trainer๋Š” ์—ฌ๋Ÿฌ ๋Œ€์˜ ๋จธ์‹ ์„ ์ž๋™์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ฏ€๋กœ ํ•™์Šต ์†๋„๋ฅผ ํฌ๊ฒŒ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์„ ์ด์šฉํ•œ ์ฝ”๋“œ ์ƒ์„ฑ ์ด์ œ ์ง„์‹ค์˜ ์ˆœ๊ฐ„์ž…๋‹ˆ๋‹ค. ํ•™์Šต๋œ ๋ชจ๋ธ์ด ์‹ค์ œ๋กœ ์–ผ๋งˆ๋‚˜ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ด…์‹œ๋‹ค! ๋กœ๊ทธ์—์„œ ์†์‹ค์ด ๊พธ์ค€ํžˆ ๊ฐ์†Œํ–ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ํŠน์ • ํ”„๋กฌํ”„ํŠธ์—์„œ ์–ผ๋งˆ๋‚˜ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” ํ…์ŠคํŠธ ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ๋ชจ๋ธ์„ ๋ž˜ํ•‘ํ•˜๊ณ  ๋น ๋ฅธ ์ƒ์„ฑ์„ ์œ„ํ•ด ๊ฐ€์šฉ GPU์— ์ด๋ฅผ ํƒ‘์žฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค: import torch from transformers import pipeline device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") pipe = pipeline( "text-generation", model="huggingface-course/codeparrot-ds", device=device ) ์‚ฐ์ ๋„(scatter plot)๋ฅผ ๋งŒ๋“œ๋Š” ๊ฐ„๋‹จํ•œ ์ž‘์—…๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: txt = """\ # create some data x = np.random.randn(100) y = np.random.randn(100) # create scatter plot with x, y """ print(pipe(txt, num_return_sequences=1)[0]["generated_text"]) ๊ฒฐ๊ณผ๊ฐ€ ์ •ํ™•ํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. pandas ์ž‘์—…์—์„œ๋„ ์ž˜ ์ž‘๋™ํ• ๊นŒ์š”? ๋‘ ๊ฐœ์˜ ๋ฐฐ์—ด์—์„œ DataFrame์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ๋ด…์‹œ๋‹ค: txt = """\ # create some data x = np.random.randn(100) y = np.random.randn(100) # create dataframe from x and y """ print(pipe(txt, num_return_sequences=1)[0]["generated_text"]) ์ข‹์Šต๋‹ˆ๋‹ค. ๋น„๋ก x ์นผ๋Ÿผ์„ ๋‹ค์‹œ ์‚ฝ์ž…ํ–ˆ์ง€๋งŒ ๊ทธ๋ž˜๋„ ์ •ํ™•ํ•œ ๋‹ต์ž…๋‹ˆ๋‹ค. ์ƒ์„ฑ๋˜๋Š” ํ† ํฐ์˜ ์ˆ˜๊ฐ€ ์ œํ•œ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ for ๋ฃจํ”„๊ฐ€ ์ž˜๋ฆฝ๋‹ˆ๋‹ค. ์ข€ ๋” ๋ณต์žกํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ณ  ๋ชจ๋ธ์ด ์šฐ๋ฆฌ๊ฐ€ groupby ์ž‘์—…์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค: txt = """\ # dataframe with profession, income and name df = pd.DataFrame({'profession': x, 'income':y, 'name': z}) # calculate the mean income per profession """ print(pipe(txt, num_return_sequences=1)[0]["generated_text"]) ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜์ง€ ์•Š๋„ค์š”. ๊ทธ๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ scikit-learn์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€, ๋˜ํ•œ Random Forest ๋ชจ๋ธ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ด…์‹œ๋‹ค: txt = """ # import random forest regressor from scikit-learn from sklearn.ensemble import RandomForestRegressor # fit random forest model with 300 estimators on X, y: """ print(pipe(txt, num_return_sequences=1)[0]["generated_text"]) ์œ„์˜ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋ณด๋ฉด ์šฐ๋ฆฌ ๋ชจ๋ธ์ด Python ๋ฐ์ดํ„ฐ ๊ณผํ•™ ์Šคํƒ์˜ ๊ตฌ๋ฌธ ์ค‘ ์ผ๋ถ€๋ฅผ ํ•™์Šตํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์‹ค์ œ ์„ธ๊ณ„์— ๋ชจ๋ธ์„ ๋ฐฐํฌํ•˜๊ธฐ ์ „์— ๋” ์ฒ ์ €ํ•˜๊ฒŒ ํ‰๊ฐ€ํ•ด์•ผ ํ•˜๊ฒ ์ง€์š”. ๊ทธ๋Ÿฌ๋‚˜ ๋•Œ๋กœ๋Š” ์ฃผ์–ด์ง„ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ํ•„์š”ํ•œ ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ ํ•™์Šต ์‹œ์— ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ๋„์ž…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ๋™์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•˜๊ฑฐ๋‚˜ ์ž˜๋ชป๋œ ์˜ˆ์ œ๋ฅผ ์ฆ‰์‹œ ๊ฑด๋„ˆ๋›ฐ๋Š” ์กฐ๊ฑด๋ถ€ ํ•™์Šต ๋ฃจํ”„๋ฅผ ๊ฐ–๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ํ•œ ๊ฐ€์ง€ ์„ ํƒ์ง€๋Š” Trainer๋ฅผ ์„œ๋ธŒ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค๊ณ  ํ•„์š”ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด์ง€๋งŒ ๋•Œ๋กœ๋Š” ์•„์˜ˆ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šต ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋” ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ Accelerate๊ฐ€ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. Accelerate๋ฅผ ์ด์šฉํ•œ ํ•™์Šต ์šฐ๋ฆฌ๋Š” ์ง€๊ธˆ๊นŒ์ง€ Trainer๋กœ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์—์„œ๋„ ๋ช‡ ๊ฐ€์ง€ ์‚ฌ์šฉ์ž ์ง€์ • ์„ค์ • ๋ณ€๊ฒฝ์„ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋•Œ๋•Œ๋กœ ์šฐ๋ฆฌ๋Š” ํ•™์Šต ๋ฃจํ”„์— ๋Œ€ํ•œ ์™„์ „ํ•œ ์ œ์–ด๋ฅผ ์›ํ•˜๊ฑฐ๋‚˜ ์•ฝ๊ฐ„์˜ ์˜ˆ์™ธ์ ์ธ ๋ณ€๊ฒฝ์„ ์›ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ Accelerate๋Š” ํ›Œ๋ฅญํ•œ ์„ ํƒ์ด๋ฉฐ ์ด ์„น์…˜์—์„œ๋Š” ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๋Š” ๋‹จ๊ณ„๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž‘์—…์„ ๋” ํฅ๋ฏธ๋กญ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” ๋˜ํ•œ ํ•™์Šต ๋ฃจํ”„์— ์•ฝ๊ฐ„์˜ ๋ณ€ํ˜•์„ ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ฃผ๋กœ ๋ฐ์ดํ„ฐ ๊ณผํ•™ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ํ•ฉ๋ฆฌ์ ์ธ ์ž๋™ ์™„์„ฑ์— ๊ด€์‹ฌ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํ•™์Šต ์ƒ˜ํ”Œ์— ๋” ๋งŽ์€ ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์ด ํ•ฉ๋ฆฌ์ ์ž…๋‹ˆ๋‹ค. matplotlib.pyplot, pandas, sklearn์˜ ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•œ import ์ด๋ฆ„์ธ plt, pd, sk ๋ฐ fit, predict์™€ ๊ฐ™์€ ํ‚ค์›Œ๋“œ์™€ fit/predict ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋Ÿฌํ•œ ์˜ˆ๋ฅผ ์‰ฝ๊ฒŒ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ๋“ค์ด ๊ฐ๊ฐ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค๋ฉด ์ž…๋ ฅ ์‹œํ€€์Šค์—์„œ ๋ฐœ์ƒํ•˜๋Š”์ง€ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฐ์—๋Š” ๊ณต๋ฐฑ ์ ‘๋‘์–ด๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ํ† ํฌ ๋‚˜์ด์ € ์–ดํœ˜์—์„œ๋„ ํ•ด๋‹น ๋ฒ„์ „์„ ํ™•์ธํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์ด ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ํ† ํฐ์œผ๋กœ ๋ถ„ํ• ๋˜์–ด์•ผ ํ•˜๋Š” ํ•˜๋‚˜์˜ ํ…Œ์ŠคํŠธ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: keytoken_ids = [] for keyword in [ "plt", "pd", "sk", "fit", "predict", " plt", " pd", " sk", " fit", " predict", "testtest", ]: ids = tokenizer([keyword]).input_ids[0] if len(ids) == 1: keytoken_ids.append(ids[0]) else: print(f"Keyword has not single token: {keyword}") ํ›Œ๋ฅญํ•ฉ๋‹ˆ๋‹ค. ์ž˜ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์ด์ œ ์ž…๋ ฅ ์‹œํ€€์Šค, ๋กœ์ง“ ๋ฐ ๋ฐฉ๊ธˆ ์ž…๋ ฅ์œผ๋กœ ์„ ํƒํ•œ ํ‚ค ํ† ํฐ์„ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž ์ง€์ • ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๋กœ์ง“๊ณผ ์ž…๋ ฅ์„ ์ •๋ ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ํ† ํฐ์ด ํ˜„์žฌ ํ† ํฐ์˜ ๋ ˆ์ด๋ธ”์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅธ์ชฝ์œผ๋กœ 1๋งŒํผ ์ด๋™ํ•œ ์ž…๋ ฅ ์‹œํ€€์Šค๊ฐ€ ๋ ˆ์ด๋ธ”์„ ํ˜•์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ์–ด์จŒ๋“  ์ฒซ ๋ฒˆ์งธ ํ† ํฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก์„ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๋‘ ๋ฒˆ์งธ ํ† ํฐ์—์„œ ๋ ˆ์ด๋ธ”์„ ์‹œ์ž‘ํ•˜์—ฌ ์ด๋ฅผ ๋‹ฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ „์ฒด ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ๋”ฐ๋ฅด๋Š” ํ† ํฐ์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ”์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰ ๋กœ์ง“์„ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ƒ˜ํ”Œ๋‹น ์†์‹ค์„ ๊ณ„์‚ฐํ•˜๊ณ  ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋ชจ๋“  ํ‚ค์›Œ๋“œ์˜ ๋ฐœ์ƒ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐœ์ƒ ํšŸ์ˆ˜๋ฅผ ๊ฐ€์ค‘์น˜๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์ƒ˜ํ”Œ์— ๋Œ€ํ•œ ๊ฐ€์ค‘ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ํ‚ค์›Œ๋“œ๊ฐ€ ์—†๋Š” ์ƒ˜ํ”Œ์„ ๋ชจ๋‘ ๋ฒ„๋ฆฌ๊ณ  ์‹ถ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์ค‘์น˜์— 1์„ ๋”ํ•ฉ๋‹ˆ๋‹ค: from torch.nn import CrossEntropyLoss import torch def keytoken_weighted_loss(inputs, logits, keytoken_ids, alpha=1.0): # Shift so that tokens < n predict n shift_labels = inputs[..., 1:].contiguous() shift_labels = logits[..., :-1, :].contiguous() # ํ† ํฐ๋‹น ์†์‹ค ๊ฐ’ ๊ณ„์‚ฐ loss_fct = CrossEntropyLoss(reduce=False) loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1)) # ์ƒ˜ํ”Œ๋‹น ์†์‹ค ๊ฐ’์„ resize ํ•˜๊ณ  ํ‰๊ท ํ™” loss_per_sample = loss.view(shift_logits.size(0), shift_logits.size(1)).mean(axis=1) # Calculate and scale weighting weights = torch.stack([(inputs == kt).float() for kt in keytoken_ids]).sum(axis=[0, 2]) weights = alpha * (1.0 + weights) # Calculate weighted average weighted_loss = (loss_per_sample * weights).mean() return weighted_loss ์ด ๋ฉ‹์ง„ ์ƒˆ๋กœ์šด ์†์‹ค ํ•จ์ˆ˜๋กœ ํ•™์Šต์„ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๋ช‡ ๊ฐ€์ง€๋ฅผ ์ค€๋น„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ผ๊ด„์ ์œผ๋กœ ๋กœ๋“œํ•˜๋ ค๋ฉด dataloaders๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ๊ฐ์†Œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋•Œ๋•Œ๋กœ ์šฐ๋ฆฌ๋Š” ํ‰๊ฐ€๋ฅผ ์›ํ•˜๋ฏ€๋กœ ํ‰๊ฐ€ ์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜๋กœ ๋ž˜ํ•‘ ํ•˜๋Š” ๊ฒƒ์ด ํ•ฉ๋ฆฌ์ ์ž…๋‹ˆ๋‹ค. dataloaders๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์˜<NAME>์„ "torch"๋กœ ์„ค์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ์ ์ ˆํ•œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋กœ PyTorch DataLoader์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from torch.utils.data.dataloader import DataLoader tokenized_dataset.set_format("torch") train_dataloader = DataLoader(tokenized_dataset["train"], batch_size=32, shuffle=True) eval_dataloader = DataLoader(tokenized_dataset["valid"], batch_size=32) ๋‹ค์Œ์œผ๋กœ, ์ตœ์ ํ™” ํ”„๋กœ๊ทธ๋žจ์ด ์ถ”๊ฐ€ ๊ฐ€์ค‘์น˜ ๊ฐ์†Œ๋ฅผ ์–ป์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ทธ๋ฃนํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋“  ํŽธํ–ฅ(bias) ๋ฐ LayerNorm ๊ฐ€์ค‘์น˜ ์กฐ๊ฑด์€ ์˜ˆ์™ธ์ž…๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: weight_decay = 0.1 def get_grouped_params(model, no_decay=["bias", "LayerNorm.weight"]): params_with_wd, params_without_wd = [], [] for n, p in model.named_parameters(): if any(nd in n for nd in no_decay): params_without_wd.append(p) else: params_with_wd.append(p) return [ {"params": params_with_wd, "weight_decay": weight_decay}, {"params": params_without_wd, "weight_decay": 0.0}, ] ํ•™์Šต ์ค‘์— ๊ฒ€์ฆ ์„ธํŠธ์—์„œ ๋ชจ๋ธ์„ ์ •๊ธฐ์ ์œผ๋กœ ํ‰๊ฐ€ํ•˜๊ธฐ๋ฅผ ์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋„ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€ dataloader๋ฅผ ํ†ตํ•ด ์‹คํ–‰๋˜๊ณ  ํ”„๋กœ์„ธ์Šค ์ „๋ฐ˜์— ๊ฑธ์ณ ๋ชจ๋“  ์†์‹ค์„ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค: def evaluate(): model.eval() losses = [] for step, batch in enumerate(eval_dataloader): with torch.no_grad(): outputs = model(batch["input_ids"], labels=batch["input_ids"]) losses.append(accelerator.gather(outputs.loss)) loss = torch.mean(torch.cat(losses)) try: perplexity = torch.exp(loss) except OverflowError: perplexity = float("inf") return loss.item(), perplexity.item() evaluate() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •๊ธฐ์ ์ธ ๊ฐ„๊ฒฉ์œผ๋กœ ์†์‹ค๊ณผ perplexity๋ฅผ ๋ณด๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ, ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ํ•™์Šต ๋ชจ๋ธ์„ ์žฌ์ •์˜ํ•ฉ๋‹ˆ๋‹ค: model = GPT2LMHeadModel(config) ๊ทธ๋Ÿฐ ๋‹ค์Œ, ๊ฐ€์ค‘์น˜ ๊ฐ์†Œ ๋Œ€์ƒ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ด์ „์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ ํ™” ํ”„๋กœ๊ทธ๋žจ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from torch.optim import AdamW optimizer = AdamW(get_grouped_params(model), lr=5e-4) ์ด์ œ ํ•™์Šต์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ชจ๋ธ, ์˜ตํ‹ฐ๋งˆ์ด์ € ๋ฐ dataloaders๋ฅผ ์ค€๋น„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค: from accelerate import Accelerator accelerator = Accelerator(fp16=True) model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader ) TPU์—์„œ ํ•™์Šตํ•˜๋Š” ๊ฒฝ์šฐ ์œ„์˜ ์…€์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ์ „์šฉ ํ•™์Šต ํ•จ์ˆ˜๋กœ ์ด๋™ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ 3์žฅ์„ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. ์ด์ œ train_dataloader๋ฅผ accelerator.prepare()๋กœ ๋ณด๋ƒˆ์œผ๋ฏ€๋กœ ๊ธธ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ๋‹จ๊ณ„ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ dataloader๋ฅผ ์ค€๋น„ํ•œ ํ›„์— ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธธ์ด๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•™์Šต๋ฅ ์—์„œ 0๊นŒ์ง€์˜ ๊ณ ์ „์ ์ธ ์„ ํ˜• ์Šค์ผ€์ค„๋ง์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import get_scheduler num_train_epochs = 1 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( name="linear", optimizer=optimizer, num_warmup_steps=1000, num_training_steps=num_training_steps, ) ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋ธ์„ Hub๋กœ ํ‘ธ์‹œ ํ•˜๋ ค๋ฉด ์ž‘์—… ํด๋”์— Repository ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„์ง ๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋จผ์ € Hugging Face Hub์— ๋กœ๊ทธ์ธํ•˜์„ธ์š”. ๋ชจ๋ธ์— ๋ถ€์—ฌํ•˜๋ ค๋Š” ๋ชจ๋ธ ID์—์„œ ์ €์žฅ์†Œ ์ด๋ฆ„์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. repo_name์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ์ž์œ ๋กญ๊ฒŒ ๋ฐ”๊พธ์‹ญ์‹œ์˜ค. get_full_repo_name() ํ•จ์ˆ˜๊ฐ€ ์ˆ˜ํ–‰ํ•˜๋Š” ์‚ฌ์šฉ์ž ์ด๋ฆ„๋งŒ ํฌํ•จํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: from huggingface_hub import Repository, get_full_repo_name model_name = "codeparrot-ds-accelerate" repo_name = get_full_repo_name(model_name) repo_name ๊ทธ๋Ÿฐ ๋‹ค์Œ ํ•ด๋‹น ์ €์žฅ์†Œ๋ฅผ ๋กœ์ปฌ ํด๋”์— ๋ณต์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์ด ๋กœ์ปฌ ํด๋”๋Š” ์ž‘์—… ์ค‘์ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๊ธฐ์กด ๋ณต์ œ๋ณธ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: output_dir = "codeparrot-ds-accelerate" repo = Repository(output_dir, clone_from=repo_name) ์ด์ œ repo.push_to_hub() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ output_dir์— ์ €์žฅํ•œ ๋ชจ๋“  ๊ฒƒ์„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ ์ค‘๊ฐ„ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ํ•™์Šตํ•˜๊ธฐ ์ „์— ํ‰๊ฐ€ ๊ธฐ๋Šฅ์ด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋น ๋ฅธ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: evaluate() ๋งค์šฐ ๋†’์€ ์†์‹ค๊ณผ perplexity ๊ฐ’์ด์ง€๋งŒ ์•„์ง ๋ชจ๋ธ์„ ํ•™์Šตํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋†€๋ผ์šด ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด๊ฒƒ์œผ๋กœ ํ•™์Šต ์Šคํฌ๋ฆฝํŠธ์˜ ํ•ต์‹ฌ ๋ถ€๋ถ„์ธ ํ•™์Šต ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•  ๋ชจ๋“  ์ค€๋น„๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ๋ฃจํ”„์—์„œ dataloader๋ฅผ ๋ฐ˜๋ณตํ•˜๊ณ  ๋ฐฐ์น˜๋ฅผ ๋ชจ๋ธ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋กœ์ง“์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •์˜ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ๋งŽ์€ ๋‹จ๊ณ„๋ฅผ ์ง‘๊ณ„ํ•  ๋•Œ ๋” ํฐ ์†์‹ค์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๋„๋ก ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๋ˆ„์  ๋‹จ๊ณ„์˜ ์ˆ˜๋งŒํผ ์†์‹ค์„ ์กฐ์ ˆํ•ฉ๋‹ˆ๋‹ค. ์ตœ์ ํ™”ํ•˜๊ธฐ ์ „์— ๋” ๋‚˜์€ ์ˆ˜๋ ด์„ ์œ„ํ•ด ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๋ฅผ ํด๋ฆฌํ•‘(clip) ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ช‡ ๋‹จ๊ณ„๋งˆ๋‹ค ์ƒˆ๋กœ์šด evaluate() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‰๊ฐ€ ์„ธํŠธ์˜ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค: from tqdm.notebook import tqdm gradient_accumulation_steps = 8 eval_steps = 5000 model.train() completed_steps = 0 for epoch in range(num_train_epochs): for step, batch in tqdm(enumerate(train_dataloader, start=1), total=num_training_steps): logits = model(batch["input_ids"]).logits loss = keytoken_weighted_loss(batch["input_ids"], logits, keytoken_ids) if step % 100 == 0: accelerator.print( { "lr": get_lr(), "samples": step * samples_per_step, "steps": completed_steps, "loss/train": loss.item() * gradient_accumulation_steps, } ) loss = loss / gradient_accumulation_steps accelerator.backward(loss) if step % gradient_accumulation_steps == 0: accelerator.clip_grad_norm_(model.parameters(), 1.0) optimizer.step() lr_scheduler.step() optimizer.zero_grad() completed_steps += 1 if (step % (eval_steps * gradient_accumulation_steps)) == 0: eval_loss, perplexity = evaluate() accelerator.print({"loss/eval": eval_loss, "perplexity": perplexity}) model.train() accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=f"Training in progress step {step}", blocking=False ) ๊ทธ๊ฒŒ ์ „๋ถ€์ž…๋‹ˆ๋‹ค. ์ด์ œ GPT-2์™€ ๊ฐ™์€ ์ธ๊ณผ์  ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•œ ๊ณ ์œ ํ•œ ์‚ฌ์šฉ์ž ์ง€์ • ํ•™์Šต ๋ฃจํ”„๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์ด๋ฅผ ํ•„์š”์— ๋”ฐ๋ผ ์ถ”๊ฐ€๋กœ ๋ณ€๊ฒฝํ•ด์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. โœ Try it out! ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋งž๋Š” ๋งž์ถคํ˜• ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ํ•™์Šต ๋ฃจํ”„์— ๋‹ค๋ฅธ ๋งž์ถคํ˜• ๋‹จ๊ณ„๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด์‹ญ์‹œ์˜ค. โœ Try it out! ๊ธด ํ•™์Šต ์‹คํ—˜์„ ์‹คํ–‰ํ•  ๋•Œ TensorBoard ๋˜๋Š” Weights & Biases์™€ ๊ฐ™์€ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘์š”ํ•œ ๋ฉ”ํŠธ๋ฆญ์„ ๊ธฐ๋กํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํ•™์Šต์ด ์–ด๋–ป๊ฒŒ ์ง„ํ–‰๋˜๊ณ  ์žˆ๋Š”์ง€ ํ•ญ์ƒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•™์Šต ๋ฃจํ”„์— ์ ์ ˆํ•œ ๋กœ๊น…์„ ์ถ”๊ฐ€ํ•˜์‹ญ์‹œ์˜ค. 6. ์งˆ์˜์‘๋‹ต (Question Answering) ์ด์ œ ์งˆ์˜์‘๋‹ต์„ ์‚ดํŽด๋ณผ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜๊ฐ€ ์žˆ์ง€๋งŒ ์ด ์„น์…˜์—์„œ ์ค‘์ ์ ์œผ๋กœ ๋‹ค๋ฃฐ ์ž‘์—…์€ ์ถ”์ถœํ˜• ์งˆ์˜์‘๋‹ต(extractive question answering)์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ์งˆ๋ฌธ์„ ์ œ์‹œํ•˜๊ณ  ๋ฌธ์„œ ์ž์ฒด์— ์กด์žฌํ•˜๋Š” ํ…์ŠคํŠธ ๋ฒ”์œ„(spans of text)๋ฅผ ํ•ด๋‹น ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์œผ๋กœ ์ถ”์ถœํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. Wikipedia ๊ธฐ์‚ฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํฌ๋ผ์šฐ๋“œ ์†Œ์‹ฑ ํ˜•ํƒœ๋กœ ์งˆ๋ฌธ๊ณผ ๋‹ต๋ณ€์„ ๊ตฌ์ถ•ํ•œ SQuAD ๋ฐ์ดํ„ฐ ์…‹์„ ๊ฐ€์ง€๊ณ  BERT ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ˆ์ธก์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์ด ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค: ์ด๊ฒƒ์€ ์‹ค์ œ๋กœ ์ด ์„น์…˜์— ํ‘œ์‹œ๋œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต๋˜๊ณ  Hub์— ์—…๋กœ๋“œ๋œ ๋ชจ๋ธ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ์˜ˆ์ธก์„ ์ฐพ์•„ ๋‹ค์‹œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BERT์™€ ๊ฐ™์€ ์ธ์ฝ”๋” ์ „์šฉ ๋ชจ๋ธ์€ "๋ˆ„๊ฐ€ Transformer ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ฐœ๋ช…ํ–ˆ์Šต๋‹ˆ๊นŒ?"์™€ ๊ฐ™์€ ์‚ฌ์‹ค์ ์ธ ์งˆ๋ฌธ(factoid questions)์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ถ”์ถœํ•˜๋Š” ๋ฐ ์šฐ์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ "ํ•˜๋Š˜์€ ์™œ ํŒŒ๋ž€์ƒ‰์ž…๋‹ˆ๊นŒ?"์™€ ๊ฐ™์€ ๊ฐœ๋ฐฉํ˜• ์งˆ๋ฌธ(open-ended questions)์ด ์ฃผ์–ด์ง€๋ฉด ์ œ๋Œ€๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋‹ต๋ณ€ํ•˜๊ธฐ ์–ด๋ ค์šด ์งˆ๋ฌธ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ์— T5 ๋ฐ BART์™€ ๊ฐ™์€ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ…์ŠคํŠธ ์š”์•ฝ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ •๋ณด๋ฅผ ํ•ฉ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์œ ํ˜•์˜ ์ƒ์„ฑ ๊ธฐ๋ฐ˜ ์งˆ์˜์‘๋‹ต(generative question answering)์— ๊ด€์‹ฌ์ด ์žˆ๋‹ค๋ฉด ELI5 ๋ฐ์ดํ„ฐ ์…‹์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๋ฐ๋ชจ๋ฅผ ํ™•์ธํ•ด ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ค€๋น„ ์ถ”์ถœํ˜• ์งˆ์˜์‘๋‹ต์˜ ํ•™๋ฌธ์  ๋ฒค์น˜๋งˆํฌ๋กœ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ ์…‹์€ SQuAD์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์—์„œ ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ •๋‹ต์ด ์—†๋Š” ์งˆ๋ฌธ์„ ํฌํ•จํ•˜๋Š” ๋” ์–ด๋ ค์šด SQuAD v2 ๋ฒค์น˜๋งˆํฌ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ์ธ์ด ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ ์…‹์— ์ปจํ…์ŠคํŠธ ์—ด, ์งˆ๋ฌธ ์—ด, ๋‹ต๋ณ€ ์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š” ํ•œ ์•„๋ž˜ ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SQuAD ๋ฐ์ดํ„ฐ ์…‹ ์ด์ „๊ณผ ๊ฐ™์ด load_dataset() ๋•๋ถ„์— ๋ฐ์ดํ„ฐ ์…‹์„ ํ•œ ๋ฒˆ์— ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์บ์‹œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_dataset raw_datasets = load_dataset("squad") ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด ๊ฐ์ฒด(raw_datasets)๋ฅผ ๋ณด๊ณ  SQuAD ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: raw_datasets ์ปจํ…์ŠคํŠธ, ์งˆ๋ฌธ ๋ฐ ๋‹ต๋ณ€ ํ•„๋“œ์— ํ•„์š”ํ•œ ๋ชจ๋“  ๊ฒƒ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์œผ๋ฏ€๋กœ ํ•™์Šต ์ง‘ํ•ฉ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: print("Context: ", raw_datasets["train"][0]["context"]) print("Question: ", raw_datasets["train"][0]["question"]) print("Answer: ", raw_datasets["train"][0]["answers"]) ์ปจํ…์ŠคํŠธ ๋ฐ ์งˆ๋ฌธ ํ•„๋“œ๋Š” ์‚ฌ์šฉํ•˜๊ธฐ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋‹ต๋ณ€ ํ•„๋“œ๋Š” ๋‘ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ ํ•„๋“œ๊ฐ€ ์žˆ๋Š” ์‚ฌ์ „์„ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๋” ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. ์ด<NAME>์€ ํ‰๊ฐ€ ๊ณผ์ •์—์„œ squad ๋ฉ”ํŠธ๋ฆญ์ด ์š”๊ตฌํ•˜๋Š”<NAME>์ž…๋‹ˆ๋‹ค. ๋ณธ์ธ ๊ณ ์œ ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ต๋ณ€์„ ๋™์ผํ•œ<NAME>์œผ๋กœ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ๊ฑฑ์ •ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. text ํ•„๋“œ๋Š” ๋‹ค์†Œ ๋ช…ํ™•ํ•˜๋ฉฐ answer_start ํ•„๋“œ์—๋Š” ์ปจํ…์ŠคํŠธ์—์„œ ๊ฐ ๋‹ต๋ณ€์˜ ์‹œ์ž‘ ๋ฌธ์ž ์ธ๋ฑ์Šค๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๊ณผ์ •์—์„œ ํ•˜๋‚˜์˜ ๋‹ต๋ณ€๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Dataset.filter() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฅผ ๋‹ค์‹œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: raw_datasets["train"].filter(lambda x: len(x["answers"]["text"]) != 1) ๊ทธ๋Ÿฌ๋‚˜ ํ‰๊ฐ€๋ฅผ ์œ„ํ•ด ๊ฐ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด ๋ช‡ ๊ฐ€์ง€ ๊ฐ€๋Šฅํ•œ ๋‹ต๋ณ€์ด ์žˆ์œผ๋ฉฐ ๋™์ผํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: print(raw_datasets["validation"][0]["answers"]) print(raw_datasets["validation"][2]["answers"]) ํ‰๊ฐ€ ์Šคํฌ๋ฆฝํŠธ๋Š” Datasets ๋ฉ”ํŠธ๋ฆญ์œผ๋กœ ๋ชจ๋‘ ๋งˆ๋ฌด๋ฆฌ๋˜๋ฏ€๋กœ ์ž์„ธํžˆ ๋‹ค๋ฃจ์ง€๋Š” ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•ด์„œ ์ผ๋ถ€ ์งˆ๋ฌธ์—๋Š” ๋ช‡ ๊ฐ€์ง€ ๊ฐ€๋Šฅํ•œ ๋‹ต๋ณ€์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์Šคํฌ๋ฆฝํŠธ(๋ฉ”ํŠธ๋ฆญ)๋Š” ์˜ˆ์ƒ ๋‹ต๋ณ€์„ ์ˆ˜์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋‹ต๋ณ€๊ณผ ๋น„๊ตํ•˜์—ฌ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ธ๋ฑ์Šค 2์˜ ์ƒ˜ํ”Œ์„ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: print(raw_datasets["validation"][2]["context"]) print(raw_datasets["validation"][2]["question"]) ์šฐ๋ฆฌ๋Š” ์œ„ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋Œ€๋‹ต์ด ์šฐ๋ฆฌ๊ฐ€ ๊ทธ์ „์— ๋ณธ ์„ธ ๊ฐ€์ง€ ๊ฐ€๋Šฅ์„ฑ ์ค‘ ํ•˜๋‚˜์ผ ์ˆ˜ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ด๋ ค์šด ๋ถ€๋ถ„์€ ์ปจํ…์ŠคํŠธ ๋‚ด์˜ ๋‹ต๋ณ€์— ํ•ด๋‹นํ•˜๋Š” ํ† ํฐ์˜ ์‹œ์ž‘ ๋ฐ ๋ ์œ„์น˜๊ฐ€ ๋  ์งˆ๋ฌธ์˜ ๋‹ต๋ณ€์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ผ๋‹จ ์„œ๋‘๋ฅด์ง€ ๋ง™์‹œ๋‹ค. ๋จผ์ € ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ ํ…์ŠคํŠธ๋ฅผ ๋ชจ๋ธ์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ID๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: from transformers import AutoTokenizer model_checkpoint = "bert-base-cased" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) ์ด์ „์— ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ์šฐ๋ฆฌ๋Š” BERT ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•  ๊ฒƒ์ด์ง€๋งŒ ๋น ๋ฅธ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๊ตฌํ˜„๋˜์–ด ์žˆ๊ธฐ๋งŒ ํ•œ๋‹ค๋ฉด ๋‹ค๋ฅธ ๋ชจ๋ธ ์œ ํ˜•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํฐ ํ…Œ์ด๋ธ”์—์„œ ๋น ๋ฅธ ๋ฒ„์ „์˜ ํ† ํฌ ๋‚˜์ด์ €์™€ ํ•จ๊ป˜ ์ œ๊ณต๋˜๋Š” ๋ชจ๋“  ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์‚ฌ์šฉ ์ค‘์ธ ํ† ํฌ ๋‚˜์ด์ € ๊ฐœ์ฒด๊ฐ€ ์‹ค์ œ๋กœ ์ง€์›๋˜๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด Tokenizers์˜ is_fast ์†์„ฑ์„ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: tokenizer.is_fast ํ† ํฌ ๋‚˜์ด์ €์— ์งˆ๋ฌธ๊ณผ ์ปจํ…์ŠคํŠธ๋ฅผ ํ•จ๊ป˜ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํŠน์ˆ˜ ํ† ํฐ์„ ์ ์ ˆํ•˜๊ฒŒ ์‚ฝ์ž…ํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์„ ํ˜•์„ฑํ•ฉ๋‹ˆ๋‹ค: [CLS] question [SEP] context [SEP] ๋‹ค์‹œ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. context = raw_datasets["train"][0]["context"] question = raw_datasets["train"][0]["question"] inputs = tokenizer(question, context) tokenizer.decode(inputs["input_ids"]) ๊ทธ๋Ÿฌ๋ฉด ๋ ˆ์ด๋ธ”์ด ๋‹ต๋ณ€์˜ ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ ํ† ํฐ ์ธ๋ฑ์Šค๊ฐ€ ๋˜๋ฉฐ, ๋ชจ๋ธ์€ ์ž…๋ ฅ์—์„œ ํ† ํฐ๋‹น ํ•˜๋‚˜์˜ ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ ๋กœ์ง“์„ ์˜ˆ์ธกํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ ์ด๋ก ์ ์ธ ๋ ˆ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: ์œ„ ๊ฒฝ์šฐ์—๋Š” ์ปจํ…์ŠคํŠธ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์ง€ ์•Š์ง€๋งŒ ๋ฐ์ดํ„ฐ ์…‹์˜ ์ผ๋ถ€ ์˜ˆ์‹œ์—๋Š” ์šฐ๋ฆฌ๊ฐ€ ์„ค์ •ํ•œ ์ตœ๋Œ€ ๊ธธ์ด(์ด ๊ฒฝ์šฐ 384)๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ๋งค์šฐ ๊ธด ์ปจํ…์ŠคํŠธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 6์žฅ์—์„œ question-answering ํŒŒ์ดํ”„๋ผ์ธ์˜ ๋‚ด๋ถ€๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ ๋ณด์•˜๋“ฏ์ด ๋ฐ์ดํ„ฐ ์…‹์˜ ํ•œ ์ƒ˜ํ”Œ์—์„œ ์—ฌ๋Ÿฌ ํ•™์Šต ์ž์งˆ๋“ค์„ ๋งŒ๋“ค๊ณ  ๊ทธ ์‚ฌ์ด์— ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธด ์ปจํ…์ŠคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ "์ž์งˆ(feature)"์˜ ์˜๋ฏธ๋Š” ๋‹จ์ผ ์ƒ˜ํ”Œ์˜ ์ผ๋ถ€๊ฐ€ ๋ถ„๋ฆฌ๋˜์–ด ๋งŒ๋“ค์–ด์ง„ ์ƒˆ๋กœ์šด ์ƒ˜ํ”Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ธธ์ด๊ฐ€ ๋„ˆ๋ฌด ๊ธด ์ปจํ…์ŠคํŠธ๋ฅผ ๊ฐ€์ง„ ๋‹จ์ผ ์ƒ˜ํ”Œ์„ ์ปจํ…์ŠคํŠธ ์ตœ๋Œ€ ๊ธธ์ด์— ๋งž์ถฐ์„œ ๋ถ„ํ• ํ•  ๋•Œ, ๋ถ„ํ• ๋œ ๊ฐ๊ฐ์˜ ์ƒ˜ํ”Œ๋“ค์„ ์ž์งˆ์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์˜ˆ์ œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด๊ธฐ ์œ„ํ•ด ๊ธธ์ด๋ฅผ 100์œผ๋กœ ์ œํ•œํ•˜๊ณ  50๊ฐœ ํ† ํฐ์˜ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ๋‹ค์Œ ์„ค์ •(๊ธฐ๋Šฅ)๋“ค์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. max_length๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค(์—ฌ๊ธฐ์„œ๋Š” 100). truncation="only_second"๋Š” ์ปจํ…์ŠคํŠธ๊ฐ€ ํฌํ•จ๋œ ์งˆ๋ฌธ์ด ๋„ˆ๋ฌด ๊ธธ ๋•Œ ์ปจํ…์ŠคํŠธ(๋‘ ๋ฒˆ์งธ ์œ„์น˜์— ์žˆ์Œ)๋ฅผ ์ž๋ฆ…๋‹ˆ๋‹ค. stride๋Š” ๋‘ ๊ฐœ์˜ ์—ฐ์† ์ฒญํฌ ์‚ฌ์ด์— ๊ฒน์น˜๋Š” ํ† ํฐ ์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค(์—ฌ๊ธฐ์„œ๋Š” 50๊ฐœ). return_overflowing_tokens=True๋ฅผ ์ง€์ •ํ•ด์„œ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋„˜์น˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ฆฝ๋‹ˆ๋‹ค. inputs = tokenizer( question, context, max_length=100, truncation="only_second", stride=50, return_overflowing_tokens=True, ) for ids in inputs["input_ids"]: print(tokenizer.decode(ids)) ์šฐ๋ฆฌ๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ์šฐ๋ฆฌ์˜ ์˜ˆ๋Š” 4๊ฐœ์˜ ์ž…๋ ฅ์œผ๋กœ ๋ถ„ํ• ๋˜์—ˆ์œผ๋ฉฐ ๊ฐ๊ฐ์—๋Š” ๋™์ผํ•œ ์งˆ๋ฌธ๊ณผ ๋ถ„ํ• ๋œ ์ปจํ…์ŠคํŠธ์˜ ์ผ๋ถ€๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€("Bernadette Soubirous")์€ ์„ธ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ์ปจํ…์ŠคํŠธ์—๋งŒ ๋‚˜ํƒ€๋‚˜๋ฏ€๋กœ ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ธด ์ปจํ…์ŠคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ ์ปจํ…์ŠคํŠธ์— ๋‹ต๋ณ€์ด ํฌํ•จ๋˜์ง€ ์•Š์€ ๋ช‡ ๊ฐ€์ง€ ํ•™์Šต ์˜ˆ์ œ๋“ค(์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ)์„ ์ถ”๊ฐ€๋กœ ๋งŒ๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์˜ˆ์—์„œ ๋ ˆ์ด๋ธ”์€ start_position = end_position = 0์ด ๋ฉ๋‹ˆ๋‹ค(๋”ฐ๋ผ์„œ [CLS] ํ† ํฐ์„ ์˜ˆ์ธกํ•จ). ๋˜ํ•œ ์ •๋‹ต์ด ์ž˜๋ฆฐ ๋ถˆํ–‰ํ•œ ๊ฒฝ์šฐ์— ํ•ด๋‹น ๋ ˆ์ด๋ธ”์„ ์„ค์ •ํ•˜์—ฌ ์ •๋‹ต์˜ ์‹œ์ž‘(๋˜๋Š” ๋)๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋‹ต๋ณ€์ด ์ปจํ…์ŠคํŠธ์— ์™„์ „ํžˆ ํฌํ•จ๋œ ์˜ˆ์˜ ๊ฒฝ์šฐ ๋ ˆ์ด๋ธ”์€ ๋‹ต๋ณ€์ด ์‹œ์ž‘๋˜๋Š” ํ† ํฐ์˜ ์ธ๋ฑ์Šค์™€ ๋‹ต๋ณ€์ด ๋๋‚˜๋Š” ํ† ํฐ์˜ ์ธ๋ฑ์Šค๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์€ ์ปจํ…์ŠคํŠธ์—์„œ ๋‹ต๋ณ€์˜ ์‹œ์ž‘ ๋ฌธ์ž๋ฅผ ์ œ๊ณตํ•˜๊ณ  ๋‹ต๋ณ€์˜ ๊ธธ์ด๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ปจํ…์ŠคํŠธ์—์„œ ๋ ๋ฌธ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฐ ์ธ๋ฑ์Šค์— ๋งคํ•‘ํ•˜๋ ค๋ฉด 6์žฅ์—์„œ ๊ณต๋ถ€ํ•œ ์˜คํ”„์…‹ ๋งคํ•‘์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. return_offsets_mapping=True๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: inputs = tokenizer( question, context, max_length=100, truncation="only_second", stride=50, return_overflowing_tokens=True, return_offsets_mapping=True, ) inputs.keys() ๋ณด์‹œ๋‹ค์‹œํ”ผ ์ผ๋ฐ˜์ ์ธ input IDs, token type IDs ๋ฐ attention mask๋Š” ๋ฌผ๋ก  ํ•„์š”ํ•œ ์˜คํ”„์…‹ ๋งคํ•‘๊ณผ ์ถ”๊ฐ€์ ์ธ overflow_to_sample_mapping์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฐ’์€ ๋™์‹œ์— ์—ฌ๋Ÿฌ ํ…์ŠคํŠธ๋ฅผ ํ† ํฐํ™”ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์šฐ๋ฆฌ์˜ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ Rust์˜ ์ง€์›์„ ๋ฐ›๋Š”๋‹ค๋Š” ์‚ฌ์‹ค๋กœ๋ถ€ํ„ฐ ์ด๋“์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ์€ ์—ฌ๋Ÿฌ ์ž์งˆ๋“ค์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ฐ ์ž์งˆ์„ ์›๋ž˜์˜ ์˜ˆ์ œ์— ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ•˜๋‚˜์˜ ์˜ˆ์ œ๋งŒ ํ† ํฐํ™”ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— 0 ๋ชฉ๋ก์„ ์–ป์Šต๋‹ˆ๋‹ค: inputs["overflow_to_sample_mapping"] ๊ทธ๋Ÿฌ๋‚˜ ๋” ๋งŽ์€ ์˜ˆ์ œ๋ฅผ ํ† ํฐํ™”ํ•˜๋ฉด ๋” ์œ ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. inputs = tokenizer( raw_datasets["train"][2:6]["question"], raw_datasets["train"][2:6]["context"], max_length=100, truncation="only_second", stride=50, return_overflowing_tokens=True, return_offsets_mapping=True, ) print(f"The 4 examples give {len(inputs['input_ids'])} features.") print(f"Here is where each comes from: {inputs['overflow_to_sample_mapping']}.") ์œ„์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ์ฒ˜์Œ ์„ธ ๊ฐ€์ง€ ์ƒ˜ํ”Œ(ํ•™์Šต ์ง‘ํ•ฉ์˜ ์ธ๋ฑ์Šค 2, 3, 4์— ํ•ด๋‹น)์€ ๊ฐ๊ฐ 4๊ฐœ์˜ ์ž์งˆ๋“ค์„ ์ œ๊ณตํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ์˜ˆ์ œ(ํ•™์Šต ์ง‘ํ•ฉ์˜ ์ธ๋ฑ์Šค 5)๋Š” 7๊ฐœ์˜ ์ž์งˆ๋“ค์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋Š” ๊ฐ ์ž์งˆ์„ ํ•ด๋‹น ๋ ˆ์ด๋ธ”์— ๋งคํ•‘ํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ํ•ด๋‹น ๋ ˆ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: (0, 0) ์ •๋‹ต์ด ์ปจํ…์ŠคํŠธ์˜ ํ•ด๋‹น ๋ฒ”์œ„์— ์—†๋Š” ๊ฒฝ์šฐ (start_position, end_position) ์ •๋‹ต์ด ์ปจํ…์ŠคํŠธ์˜ ํ•ด๋‹น ๋ฒ”์œ„์— ์žˆ๋Š” ๊ฒฝ์šฐ, start_position์ด ์ •๋‹ต ์‹œ์ž‘ ํ† ํฐ ์ธ๋ฑ์Šค(input IDs์—์„œ)์ด๊ณ , end_position๋Š” ์ •๋‹ต์˜ ๋งˆ์ง€๋ง‰ ํ† ํฐ ์ธ๋ฑ์Šค(input IDs์—์„œ)์ž„ ํ•ด๋‹น ์ปจํ…์ŠคํŠธ๊ฐ€ ์œ„ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ ์ค‘ ์–ด๋””์— ์†ํ•˜๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์šฐ์„  input IDs์—์„œ ์ปจํ…์ŠคํŠธ ์‹œ์ž‘ ๋ฐ ๋ ์ธํ…์Šค๋ฅผ ๊ตฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. token type IDs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋ชจ๋“  ๋ชจ๋ธ์— ๋Œ€ํ•ด ๋ฐ˜๋“œ์‹œ ์ด ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์—(์˜ˆ๋ฅผ ๋“ค์–ด, DistillBERT๋Š” ์ด ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค), ๋Œ€์‹  ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” BatchEncoding์˜ sequence_ids() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ† ํฐ ์ธ๋ฑ์Šค๊ฐ€ ์žˆ์œผ๋ฉด ์›๋ž˜ ์ปจํ…์ŠคํŠธ ๋‚ด์—์„œ ๋ฌธ์ž ๋ฒ”์œ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜์˜ ํŠœํ”Œ์ธ ํ•ด๋‹น ์˜คํ”„์…‹์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ์ž์งˆ์˜ ์ปจํ…์ŠคํŠธ ์ฒญํฌ๊ฐ€ ๋‹ต๋ณ€ ์ดํ›„์— ์‹œ์ž‘๋˜๋Š”์ง€ ๋‹ต๋ณ€์ด ์‹œ์ž‘๋˜๊ธฐ ์ „์— ๋๋‚˜๋Š”์ง€ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์ด ๊ฒฝ์šฐ์—๋Š” ๋ ˆ์ด๋ธ”์€ (0, 0)์ž…๋‹ˆ๋‹ค). ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ •๋‹ต์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ํ† ํฐ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋ฃจํ”„๋ฅผ ๋Œ๋ฆฝ๋‹ˆ๋‹ค: answers = raw_datasets["train"][2:6]["answers"] start_positions = [] end_positions = [] for i, offset in enumerate(inputs["offset_mapping"]): sample_idx = inputs["overflow_to_sample_mapping"][i] answer = answers[sample_idx] start_char = answer["answer_start"][0] end_char = answer["answer_start"][0] + len(answer["text"][0]) sequence_ids = inputs.sequence_ids(i) # ์ปจํ…์ŠคํŠธ์˜ ์‹œ์ž‘ ๋ฐ ๋งˆ์ง€๋ง‰์„ ์ฐพ๋Š”๋‹ค. idx = 0 while sequence_ids[idx] != 1: idx += 1 context_start = idx while sequence_ids[idx] == 1: idx += 1 context_end = idx - 1 # ๋งŒ์ผ ์ •๋‹ต์ด ์ปจํ…์ŠคํŠธ์— ์™„์ „ํžˆ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ๋ ˆ์ด๋ธ”์€ (0, 0)์ž„ if offset[context_start][0] > start_char or offset[context_end][1] < end_char: start_positions.append(0) end_positions.append(0) else: # ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ •๋‹ต์˜ ์‹œ์ž‘ ๋ฐ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค idx = context_start while idx <= context_end and offset[idx][0] <= start_char: idx += 1 start_positions.append(idx - 1) idx = context_end while idx >= context_start and offset[idx][1] >= end_char: idx -= 1 end_positions.append(idx + 1) start_positions, end_positions ์šฐ๋ฆฌ์˜ ์ ‘๊ทผ ๋ฐฉ์‹์ด ์˜ฌ๋ฐ”๋ฅธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋ช‡ ๊ฐ€์ง€ ๊ฒฐ๊ณผ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ž์งˆ์˜ ๊ฒฝ์šฐ ๋ ˆ์ด๋ธ”๋กœ (83, 85)๋ฅผ ์ฐพ์•˜์œผ๋ฏ€๋กœ ์ด๋ก ์ ์ธ ์ •๋‹ต์„ 83์—์„œ 85(ํฌํ•จ)๊นŒ์ง€์˜ ๋””์ฝ”๋”ฉ ๋œ ํ† ํฐ ๋ฒ”์œ„์™€ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: idx = 0 sample_idx = inputs["overflow_to_sample_mapping"][idx] answer = answers[sample_idx]["text"][0] start = start_positions[idx] end = end_positions[idx] labeled_answer = tokenizer.decode(inputs["input_ids"][idx][start : end + 1]) print(f"Theoretical answer: {answer}, labels give: {labeled_answer}") ์ผ์น˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ ˆ์ด๋ธ”์„ (0, 0)์œผ๋กœ ์„ค์ •ํ•œ ์ธ๋ฑ์Šค 4๋ฅผ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ •๋‹ต์ด ํ•ด๋‹น ์ž์งˆ์˜ ์ปจํ…์ŠคํŠธ ์ฒญํฌ์— ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค: idx = 4 sample_idx = inputs["overflow_to_sample_mapping"][idx] answer = answers[sample_idx]["text"][0] decoded_example = tokenizer.decode(inputs["input_ids"][idx]) print(f"Theoretical answer: {answer}, decoded example: {decoded_example}") ์œ„ ๋ฌธ๋งฅ ์•ˆ์—์„œ ์ •๋‹ต์„ ๋ณผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. โœ Your turn! XLNet ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์™ผ์ชฝ์— ํŒจ๋”ฉ์ด ์ ์šฉ๋˜๊ณ  ์งˆ๋ฌธ๊ณผ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ „ํ™˜๋ฉ๋‹ˆ๋‹ค. ๋ฐฉ๊ธˆ ๋ณธ ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ XLNet ์•„ํ‚คํ…์ฒ˜์— ์ ์šฉํ•ด ๋ด…์‹œ๋‹ค(๊ทธ๋ฆฌ๊ณ  padding=True ์ถ”๊ฐ€). ํŒจ๋”ฉ์ด ์ ์šฉ๋œ [CLS] ํ† ํฐ์€ 0 ์œ„์น˜์— ์žˆ์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹จ๊ณ„๋ณ„๋กœ ์‚ดํŽด๋ณด์•˜์œผ๋ฏ€๋กœ ์ „์ฒด ํ•™์Šต ๋ฐ์ดํ„ฐ ์…‹์— ์ ์šฉํ•  ํ•จ์ˆ˜๋กœ ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์ปจํ…์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ ๊ธธ ๊ฒƒ์ด๊ณ  ํ•ด๋‹น ์ƒ˜ํ”Œ์ด ์—ฌ๋Ÿฌ ์ž์งˆ๋“ค๋กœ ๋ถ„ํ• ๋˜๋ฏ€๋กœ ๋ชจ๋“  ์ž์งˆ์„ ์šฐ๋ฆฌ๊ฐ€ ์„ค์ •ํ•œ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ์ฑ„์šธ ๊ฒƒ์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์— ๋™์  ํŒจ๋”ฉ์„ ์ ์šฉํ•ด๋„ ์‹ค์งˆ์ ์ธ ์ด์ ์ด ์—†์Šต๋‹ˆ๋‹ค. max_length = 384 stride = 128 def preprocess_training_examples(examples): questions = [q.strip() for q in examples["question"]] inputs = tokenizer( questions, examples["context"], max_length=max_length, truncation="only_second", stride=stride, return_overflowing_tokens=True, return_offsets_mapping=True, padding="max_length", ) offset_mapping = inputs.pop("offset_mapping") sample_map = inputs.pop("overflow_to_sample_mapping") answers = examples["answers"] start_positions = [] end_positions = [] for i, offset in enumerate(offset_mapping): sample_idx = sample_map[i] answer = answers[sample_idx] start_char = answer["answer_start"][0] end_char = answer["answer_start"][0] + len(answer["text"][0]) sequence_ids = inputs.sequence_ids(i) idx = 0 while sequence_ids[idx] != 1: idx += 1 context_start = idx while sequence_ids[idx] == 1: idx += 1 context_end = idx - 1 if offset[context_start][0] > start_char or offset[context_end][1] < end_char: start_positions.append(0) end_positions.append(0) else: idx = context_start while idx <= context_end and offset[idx][0] <= start_char: idx += 1 start_positions.append(idx - 1) idx = context_end while idx >= context_start and offset[idx][1] >= end_char: idx -= 1 end_positions.append(idx + 1) inputs["start_positions"] = start_positions inputs["end_positions"] = end_positions return inputs ์‚ฌ์šฉ๋œ ์ตœ๋Œ€ ๊ธธ์ด์™€ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์˜ ๊ธธ์ด๋ฅผ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ๋‘ ๊ฐœ์˜ ์ƒ์ˆ˜๋ฅผ ์ •์˜ํ–ˆ์œผ๋ฉฐ ํ† ํฐํ™”ํ•˜๊ธฐ ์ „์— ์•ฝ๊ฐ„์˜ ์ •์ œ ์ž‘์—…์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. SQuAD ๋ฐ์ดํ„ฐ ์…‹์˜ ์ผ๋ถ€ ์งˆ๋ฌธ์—๋Š” ์‹œ์ž‘๊ณผ ๋์— ์ถ”๊ฐ€ ๊ณต๋ฐฑ์ด ์žˆ์œผ๋ฏ€๋กœ(RoBERTa์™€ ๊ฐ™์€ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ํ† ํฐํ™”ํ•  ๋•Œ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•จ) ์ด๋Ÿฌํ•œ ์ถ”๊ฐ€ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ ์ „์ฒด ํ•™์Šต ์ง‘ํ•ฉ์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด batched=True ํ”Œ๋ž˜๊ทธ์™€ ํ•จ๊ป˜ Dataset.map() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ผ ์ƒ˜ํ”Œ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž์งˆ๋“ค์ด ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: train_dataset = raw_datasets["train"].map( preprocess_training_examples, batched=True, remove_columns=raw_datasets["train"].column_names, ) len(raw_datasets["train"]), len(train_dataset) ๋ณด์‹œ๋‹ค์‹œํ”ผ, ์ „์ฒ˜๋ฆฌ๋Š” ๋Œ€๋žต 1,000๊ฐœ์˜ ์ž์งˆ๋“ค์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ•™์Šต ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ๊ฒ€์ฆ ์ง‘ํ•ฉ์˜ ์ „์ฒ˜๋ฆฌ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ๊ฒ€์ฆ ์ง‘ํ•ฉ ์ฒ˜๋ฆฌ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌ ํ•˜๋Š” ์ž‘์—…์€ ๋ ˆ์ด๋ธ”์„ ์ƒ์„ฑํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ข€ ์‰ฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์ผ ๊ฒ€์ฆ ์†์‹ค์„ ๊ณ„์‚ฐํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋ ˆ์ด๋ธ”์„ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๊ฒ€์ฆ ์†์‹ค์€ ๋ชจ๋ธ์ด ์–ผ๋งˆ๋‚˜ ์ข‹์€์ง€ ์ดํ•ดํ•˜๋Š” ๋ฐ ์‹ค์ œ๋กœ ๋„์›€์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ง„์ •ํ•œ ์ฆ๊ฑฐ์›€์€ ๋ชจ๋ธ์˜ ์˜ˆ์ธก์„ ์›๋ž˜ ์ปจํ…์ŠคํŠธ์˜ ๋ฒ”์œ„๋กœ ํ•ด์„ํ•˜์—ฌ ์‹ค์ œ ์ •๋‹ต์„ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์˜คํ”„์…‹ ๋งคํ•‘๊ณผ ์ƒ์„ฑ๋œ ๊ฐ ์ž์งˆ๋“ค์„ ์›๋ž˜ ์˜ˆ์ œ์™€ ์ผ์น˜์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋‘ ์ €์žฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์›๋ณธ ๋ฐ์ดํ„ฐ ์…‹์— ID ์—ด์ด ์žˆ์œผ๋ฏ€๋กœ ํ•ด๋‹น ID๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์ถ”๊ฐ€ํ•  ์œ ์ผํ•œ ์ž‘์—…์€ ์˜คํ”„์…‹ ๋งคํ•‘์„ ์•ฝ๊ฐ„ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ์งˆ๋ฌธ๊ณผ ์ปจํ…์ŠคํŠธ์— ๋Œ€ํ•œ ์˜คํ”„์…‹์ด ํฌํ•จ๋˜์ง€๋งŒ ํ›„์ฒ˜๋ฆฌ ๋‹จ๊ณ„์— ๋“ค์–ด๊ฐ€๋ฉด ์ž…๋ ฅ ID์˜ ์–ด๋Š ๋ถ€๋ถ„์ด ์ปจํ…์ŠคํŠธ์— ํ•ด๋‹นํ•˜๊ณ  ์–ด๋–ค ๋ถ€๋ถ„์ด ์งˆ๋ฌธ์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—†์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•œ sequence_ids() ๋ฉ”์„œ๋“œ๋Š” ํ† ํฌ ๋‚˜์ด์ €์˜ ์ถœ๋ ฅ์—๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์งˆ๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” ์˜คํ”„์…‹์„ None์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค: def preprocess_validation_examples(examples): questions = [q.strip() for q in examples["question"]] inputs = tokenizer( questions, examples["context"], max_length=max_length, truncation="only_second", stride=stride, return_overflowing_tokens=True, return_offsets_mapping=True, padding="max_length", ) sample_map = inputs.pop("overflow_to_sample_mapping") example_ids = [] for i in range(len(inputs["input_ids"])): sample_idx = sample_map[i] example_ids.append(examples["id"][sample_idx]) sequence_ids = inputs.sequence_ids(i) offset = inputs["offset_mapping"][i] inputs["offset_mapping"][i] = [ o if sequence_ids[k] == 1 else None for k, o in enumerate(offset) ] inputs["example_id"] = example_ids return inputs ์ด์ „๊ณผ ๊ฐ™์ด ์ „์ฒด ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์…‹์— ์ด ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: validation_dataset = raw_datasets["validation"].map( preprocess_validation_examples, batched=True, remove_columns=raw_datasets["validation"].column_names, ) len(raw_datasets["validation"]), len(validation_dataset) ์ด ๊ฒฝ์šฐ ๋ช‡ ๋ฐฑ ๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถ”๊ฐ€ํ–ˆ์œผ๋ฏ€๋กœ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์…‹์˜ ์ปจํ…์ŠคํŠธ๊ฐ€ ์•ฝ๊ฐ„ ๋” ์งง์€ ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌํ–ˆ์œผ๋ฏ€๋กœ ์ด์ œ ํ•™์Šต์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Trainer API๋ฅผ ์ด์šฉํ•œ ๋ชจ๋ธ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning) ์ด ์˜ˆ์ œ์˜ ํ•™์Šต ์ฝ”๋“œ๋Š” ์ด์ „ ์„น์…˜์˜ ์ฝ”๋“œ์™€ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์–ด๋ ค์šด ๊ฒƒ์€ compute_metrics() ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ์ƒ˜ํ”Œ์„ ์šฐ๋ฆฌ๊ฐ€ ์„ค์ •ํ•œ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉ ํ•œ ๊ด€๊ณ„๋กœ ์ •์˜ํ•  ๋ฐ์ดํ„ฐ ์ฝœ๋ ˆ ์ดํ„ฐ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ฉ”ํŠธ๋ฆญ ๊ณ„์‚ฐ์ด ์‹ค์ œ๋กœ ์šฐ๋ฆฌ๊ฐ€ ๊ฑฑ์ •ํ•ด์•ผ ํ•˜๋Š” ์œ ์ผํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์–ด๋ ค์šด ๋ถ€๋ถ„์€ ๋ชจ๋ธ ์˜ˆ์ธก์„ ์›๋ณธ ์˜ˆ์ œ์˜ ํ…์ŠคํŠธ ๋ฒ”์œ„(span)๋กœ ํ›„์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ผ๋‹จ ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ฉ”ํŠธ๋ฆญ์ด ๋Œ€๋ถ€๋ถ„์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ›„์ฒ˜๋ฆฌ (Post-processing) question-answering pipeline์„ ๊ณต๋ถ€ํ•˜๋Š” ๋™์•ˆ ์‚ดํŽด๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ชจ๋ธ์€ input IDs์—์„œ ๋‹ต๋ณ€์˜ ์‹œ์ž‘ ๋ฐ ๋ ์œ„์น˜์— ๋Œ€ํ•œ ๋กœ์ง“์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ํ›„์ฒ˜๋ฆฌ ๋‹จ๊ณ„๋Š” ์šฐ๋ฆฌ๊ฐ€ ๊ฑฐ๊ธฐ์„œ ํ•œ ๊ฒƒ๊ณผ ์œ ์‚ฌํ•˜๋ฏ€๋กœ ๋‹ค์Œ ๋‚ด์šฉ์€ ์šฐ๋ฆฌ๊ฐ€ ๊ทธ๋•Œ ์ทจํ–ˆ๋˜ ์กฐ์น˜๋ฅผ ๋น ๋ฅด๊ฒŒ ์ƒ๊ธฐ์‹œ์ผœ์ค๋‹ˆ๋‹ค: ์ปจํ…์ŠคํŠธ ์™ธ๋ถ€์˜ ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ ๋กœ์ง“์„ ๋งˆ์Šคํ‚น ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ softmax๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ ๋กœ์ง“์„ ํ™•๋ฅ ๋กœ ๋ณ€ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ•ด๋‹น ๋‘ ํ™•๋ฅ ์˜ ๊ณฑ์„ ์ทจํ•˜์—ฌ ๊ฐ (start_token, end_token) ์Œ์— ์ ์ˆ˜๋ฅผ ๋ถ€์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์œ ํšจํ•œ ๋‹ต๋ณ€(์˜ˆ: start_token์ด end_token๋ณด๋‹ค ๊ฐ’์ด ์ž‘์€ ๋‹ต๋ณ€๋“ค)์„ ์‚ฐ์ถœํ•œ ์ตœ๋Œ€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์Œ์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‹ค์ œ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด ํ”„๋กœ์„ธ์Šค๋ฅผ ์•ฝ๊ฐ„ ๋ณ€๊ฒฝํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค(์˜ˆ์ธก๋œ ๋‹ต๋ณ€์— ๋Œ€ํ•ด์„œ๋งŒ). ์ด๋Š” softmax ๋‹จ๊ณ„๋ฅผ ๊ฑด๋„ˆ๋›ธ ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋” ๋น ๋ฅด๊ฒŒ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  (start_token, end_token) ์Œ์„ ๊ธฐ๋กํ•˜์ง€ ์•Š๊ณ  ๊ฐ€์žฅ ๋†’์€ n_best ๋กœ์ง“(n_best=20)์— ํ•ด๋‹นํ•˜๋Š” ์Œ๋งŒ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ softmax๋ฅผ ๊ฑด๋„ˆ๋›ธ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์ ์ˆ˜๋Š” ๋กœ์ง“ ์ ์ˆ˜๊ฐ€ ๋  ๊ฒƒ์ด๋ฉฐ ์‹œ์ž‘๊ณผ ๋ ๋กœ์ง“์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ์–ป์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋“  ๊ณผ์ •๋“ค์„ ๋ณด์ด๋ ค๋ฉด ์•ฝ๊ฐ„์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์•„์ง ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— QA ํŒŒ์ดํ”„๋ผ์ธ์˜ ๊ธฐ๋ณธ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆ ์„ธํŠธ์˜ ์ž‘์€ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์˜ˆ์ธก์„ ์ƒ์„ฑํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ „๊ณผ ๋™์ผํ•œ ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์—ญ ์ƒ์ˆ˜ tokenizer์— ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์šฐ๋ฆฌ๊ฐ€ ์ž„์‹œ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๋ชจ๋ธ์˜ ํ† ํฌ ๋‚˜์ด์ €๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: small_eval_set = raw_datasets["validation"].select(range(100)) trained_checkpoint = "distilbert-base-cased-distilled-squad" tokenizer = AutoTokenizer.from_pretrained(trained_checkpoint) eval_set = small_eval_set.map( preprocess_validation_examples, batched=True, remove_columns=raw_datasets["validation"].column_names, ) ์ด์ œ ์ „์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฏ€๋กœ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์›๋ž˜ ์„ ํƒํ•œ ๊ฒƒ์œผ๋กœ ๋‹ค์‹œ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค: tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ชจ๋ธ์—์„œ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š๋Š” eval_set์˜ ์—ด์„ ์ œ๊ฑฐํ•˜๊ณ  eval_set ๋‚ด์šฉ ์ „์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ํ•˜๋‚˜์˜ ๋ฐฐ์น˜๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ๋ชจ๋ธ์„ ํ†ตํ•ด ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. GPU๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ๋” ๋น ๋ฅด๊ฒŒ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: import torch from transformers import AutoModelForQuestionAnswering eval_set_for_model = eval_set.remove_columns(["example_id", "offset_mapping"]) eval_set_for_model.set_format("torch") device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") batch = {k: eval_set_for_model[k].to(device) for k in eval_set_for_model.column_names} trained_model = AutoModelForQuestionAnswering.from_pretrained(trained_checkpoint).to(device) with torch.no_grad(): outputs = trained_model(**batch) Trainer๋Š” NumPy ๋ฐฐ์—ด๋กœ ์˜ˆ์ธก์„ ์ œ๊ณตํ•˜๋ฏ€๋กœ ์‹œ์ž‘ ๋ฐ ๋ ๋กœ์ง“์„ ๊ฐ€์ ธ์™€ ํ•ด๋‹น<NAME>์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค: start_logits = outputs.start_logits.cpu().numpy() end_logits = outputs.end_logits.cpu().numpy() ์ด์ œ small_eval_set์—์„œ ๊ฐ ์˜ˆ์‹œ์— ๋Œ€ํ•œ ์˜ˆ์ƒ ๋‹ต๋ณ€์„ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ผ ์˜ˆ์‹œ๊ฐ€ eval_set์˜ ์—ฌ๋Ÿฌ ์ž์งˆ๋“ค๋กœ ๋ถ„ํ• ๋˜์—ˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” small_eval_set์˜ ๊ฐ ์˜ˆ์‹œ๋ฅผ eval_set์˜ ํ•ด๋‹น ์ž์งˆ์— ๋งคํ•‘ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค: import collections example_to_features = collections.defaultdict(list) for idx, feature in enumerate(eval_set): example_to_features[feature["example_id"]].append(idx) ์ด๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ์˜ˆ์ œ์™€ ๊ฐ ์˜ˆ์ œ์™€ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ž์งˆ๋“ค์„ ํ†ตํ•ด ์‹ค์ œ๋กœ ์ž‘์—…์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ๋งํ–ˆ๋“ฏ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์œ„์น˜(positions)๋ฅผ ์ œ์™ธํ•˜๊ณ  n_best ์‹œ์ž‘ ๋กœ์ง“ ๋ฐ ์ข…๋ฃŒ ๋กœ์ง“์— ๋Œ€ํ•œ ๋กœ์ง“ ์ ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: ์ปจํ…์ŠคํŠธ ๋‚ด๋ถ€์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์‘๋‹ต ์Œ์ˆ˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ์‘๋‹ต ๊ธธ์ด๊ฐ€ ๋„ˆ๋ฌด ๊ธด ์‘๋‹ต (max_answer_length=30) ํ•˜๋‚˜์˜ ์˜ˆ์‹œ์— ๋Œ€ํ•ด ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์‘๋‹ต์„ ์–ป์—ˆ์œผ๋ฉด ๋กœ์ง“ ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ข‹์€ ๋‹ต์„ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: import numpy as np n_best = 20 max_answer_length = 30 predicted_answers = [] for example in small_eval_set: example_id = example["id"] context = example["context"] answers = [] for feature_index in example_to_features[example_id]: start_logit = start_logits[feature_index] end_logit = end_logits[feature_index] offsets = eval_set["offset_mapping"][feature_index] start_indexes = np.argsort(start_logit)[-1 : -n_best - 1 : -1].tolist() end_indexes = np.argsort(end_logit)[-1 : -n_best - 1 : -1].tolist() for start_index in start_indexes: for end_index in end_indexes: # ์ปจํ…์ŠคํŠธ์— ์ „์ฒด๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š์€ ์‘๋‹ต์€ ์Šคํ‚ต if offsets[start_index] is None or offsets[end_index] is None: continue # ๊ธธ์ด๊ฐ€ ์Œ์ˆ˜์ด๊ฑฐ๋‚˜ max_answer_length๋ณด๋‹ค ํฌ๋ฉด ์Šคํ‚ต if end_index < start_index or end_index - start_index + 1 > max_answer_length: continue answers.append( { "text": context[offsets[start_index][0] : offsets[end_index][1]], "logit_score": start_logit[start_index] + end_logit[end_index], } ) best_answer = max(answers, key=lambda x: x["logit_score"]) predicted_answers.append({"id": example_id, "prediction_text": best_answer["text"]}) ์˜ˆ์ƒ ๋‹ต๋ณ€์˜ ์ตœ์ข…<NAME>์€ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ๋ฉ”ํŠธ๋ฆญ์—์„œ ์š”๊ตฌํ•˜๋Š”<NAME>์ž…๋‹ˆ๋‹ค. ํ‰์†Œ์™€ ๊ฐ™์ด Datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: from datasets import load_metric metric = load_metric("squad") ์ด ๋ฉ”ํŠธ๋ฆญ์€ ์œ„์—์„œ ๋ณธ<NAME>(์˜ˆ์ œ์˜ ID์— ๋Œ€ํ•œ ํ•˜๋‚˜์˜ ํ‚ค์™€ ์˜ˆ์ธก๋œ ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ํ•˜๋‚˜์˜ ํ‚ค๊ฐ€ ์žˆ๋Š” ์‚ฌ์ „ ๋ชฉ๋ก)์˜ ์˜ˆ์ƒ ๋‹ต๋ณ€๊ณผ ๋‹ค์Œ<NAME>(์˜ˆ์ œ์˜ ID์— ๋Œ€ํ•œ ํ•˜๋‚˜์˜ ํ‚ค์™€ ๊ฐ€๋Šฅํ•œ ๋‹ต๋ณ€ ์ฆ‰, ์ •๋‹ต์— ๋Œ€ํ•œ ํ•˜๋‚˜์˜ ํ‚ค๊ฐ€ ์žˆ๋Š” ์‚ฌ์ „ ๋ชฉ๋ก)๊ณผ ๊ฐ™์€ ์ด๋ก ์  ๋‹ต๋ณ€์„ ์š”๊ตฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค: theoretical_answers = [ {"id": ex["id"], "answers": ex["answers"]} for ex in small_eval_set ] ์ด์ œ ๋‘ ๋ชฉ๋ก์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ณด๊ณ  ํ•ฉ๋ฆฌ์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ์–ป์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: print(predicted_answers[0]) print(theoretical_answers[0]) ๋‚˜์˜์ง€ ์•Š๋„ค์š”! ์ด์ œ ๋ฉ”ํŠธ๋ฆญ์ด ์ œ๊ณตํ•˜๋Š” ์ ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: metric.compute(predictions=predicted_answers, references=theoretical_answers) ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ, SQuAD์—์„œ ๋ฏธ์„ธ ์กฐ์ •๋œ DitilBERT์˜ ๋…ผ๋ฌธ์— ๋”ฐ๋ฅด๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ ์„ธํŠธ์—์„œ ํ•ด๋‹น ์ ์ˆ˜์— ๋Œ€ํ•ด 79.1 ๋ฐ 86.9๋ฅผ ์–ป์—ˆ๋‹ค๋Š” ์ ์„ ๊ณ ๋ คํ•˜๋ฉด ์ƒ๋‹นํžˆ ์„ฑ๋Šฅ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด์ œ Trainer์—์„œ ์‚ฌ์šฉํ•  compute_metrics() ํ•จ์ˆ˜์— ๋ฐฉ๊ธˆ ์ˆ˜ํ–‰ํ•œ ๋ชจ๋“  ๊ฒƒ์„ ๋„ฃ์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ•ด๋‹น compute_metrics() ํ•จ์ˆ˜๋Š” ๋กœ์ง“ ๋ฐ ๋ ˆ์ด๋ธ”์ด ์žˆ๋Š” eval_preds ํŠœํ”Œ๋งŒ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ์˜คํ”„์…‹์— ๋Œ€ํ•œ ์ž์งˆ ๋ฐ์ดํ„ฐ ์…‹๊ณผ ์›๋ณธ ์ปจํ…์ŠคํŠธ์— ๋Œ€ํ•œ ์˜ˆ์ œ ๋ฐ์ดํ„ฐ ์…‹์„ ์‚ดํŽด๋ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€ ๊ตฌํ˜„์ด ์ข€ ๋” ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•™์Šต ์ค‘์— ์ •๊ทœ ํ‰๊ฐ€ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ•™์Šต์ด ๋๋‚  ๋•Œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐ๋งŒ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. compute_metrics() ํ•จ์ˆ˜๋Š” ์ด์ „๊ณผ ๋™์ผํ•œ ๋‹จ๊ณ„๋ฅผ ๊ทธ๋ฃนํ™”ํ•ฉ๋‹ˆ๋‹ค. ์œ ํšจํ•œ ๋‹ต์ด ๋‚˜์˜ค์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•˜์—ฌ ์†Œ๊ทœ๋ชจ ๊ฒ€์‚ฌ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค(๋นˆ ๋ฌธ์ž์—ด์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€๋น„ํ•˜์—ฌ). from tqdm.auto import tqdm def compute_metrics(start_logits, end_logits, features, examples): example_to_features = collections.defaultdict(list) for idx, feature in enumerate(features): example_to_features[feature["example_id"]].append(idx) predicted_answers = [] for example in tqdm(examples): example_id = example["id"] context = example["context"] answers = [] # ํ•ด๋‹น ์˜ˆ์ œ์™€ ์—ฐ๊ด€๋œ ๋ชจ๋“  ์ž์งˆ๋“ค์— ๋Œ€ํ•ด์„œ... for feature_index in example_to_features[example_id]: start_logit = start_logits[feature_index] end_logit = end_logits[feature_index] offsets = features[feature_index]["offset_mapping"] start_indexes = np.argsort(start_logit)[-1 : -n_best - 1 : -1].tolist() end_indexes = np.argsort(end_logit)[-1 : -n_best - 1 : -1].tolist() for start_index in start_indexes: for end_index in end_indexes: # ์ปจํ…์ŠคํŠธ์— ์™„์ „ํžˆ ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๋‹ต๋ณ€์€ ์ƒ๋žต if offsets[start_index] is None or offsets[end_index] is None: continue # ๊ธธ์ด๊ฐ€ ์Œ์ˆ˜๊ฑฐ๋‚˜ max_answer_length๋ฅผ ๋„˜๋Š” ๋‹ต๋ณ€์€ ์ƒ๋žต if end_index < start_index or end_index - start_index + 1 > max_answer_length: continue answer = { "text": context[offsets[start_index][0] : offsets[end_index][1]], "logit_score": start_logit[start_index] + end_logit[end_index], } answers.append(answer) if len(answers) > 0: best_answer = max(answers, key=lambda x: x["logit_score"]) predicted_answers.append( {"id": example_id, "prediction_text": best_answer["text"]} ) else: predicted_answers.append({"id": example_id, "prediction_text": ""}) theoretical_answers = [{"id": ex["id"], "answers": ex["answers"]} for ex in examples] return metric.compute(predictions=predicted_answers, references=theoretical_answers) ์šฐ๋ฆฌ์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ์—์„œ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: compute_metrics(start_logits, end_logits, eval_set, small_eval_set) ์ข‹์•„ ๋ณด์ด๋„ค์š”. ์ด์ œ ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋ชจ๋ธ์„ ๋ฏธ์„ธ ์กฐ์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ ๋ฏธ์„ธ ์กฐ์ • ์ด์ œ ๋ชจ๋ธ์„ ํ•™์Šตํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ, AutoModelForQuestionAnswering ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋จผ์ € ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint) ์˜ˆ์ƒํ–ˆ๋˜ ๊ฒƒ๊ณผ ๊ฐ™์ด, ์ผ๋ถ€ ๊ฐ€์ค‘์น˜(์‚ฌ์ „ ํ•™์Šต ํ—ค๋“œ์˜ ๊ฐ€์ค‘์น˜)๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜(์งˆ๋ฌธ ๋‹ต๋ณ€ ํ—ค๋“œ์˜ ๊ฐ€์ค‘์น˜)๊ฐ€ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋œ๋‹ค๋Š” ๊ฒฝ๊ณ ๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ์ฏค์ด๋ฉด ์ด ๊ฒฝ๊ณ ์— ์ต์ˆ™ํ•ด์ ธ์•ผ ํ•˜์ง€๋งŒ, ์ด๋Š” ํ•ด๋‹น ๋ชจ๋ธ์ด ์•„์ง ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์ง€ ์•Š์•˜์œผ๋ฉฐ ๋ฏธ์„ธ ์กฐ์ •์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ Hub๋กœ ํ‘ธ์‹œ ํ•˜๋ ค๋ฉด Hugging Face์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋…ธํŠธ๋ถ์—์„œ ์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ๋กœ๊ทธ์ธ ์ž๊ฒฉ ์ฆ๋ช…์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์ ฏ์„ ํ‘œ์‹œํ•˜๋Š” ๋‹ค์Œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from huggingface_hub import notebook_login notebook_login() ์ด ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด TrainingArguments๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”ํŠธ๋ฆญ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ๋งํ–ˆ๋“ฏ์ด, compute_metrics() ํ•จ์ˆ˜์˜ ํŠน์ง• ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์ธ ํ‰๊ฐ€ ๋ฃจํ”„๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์ž์ฒด Trainer ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์ง€๋งŒ(์งˆ์˜์‘๋‹ต ์˜ˆ์ œ ์Šคํฌ๋ฆฝํŠธ์—์„œ์™€ ๊ฐ™์€ ์ ‘๊ทผ ๋ฐฉ์‹) ์ด ์„น์…˜์—์„œ ์„ค๋ช…ํ•˜๊ธฐ์—๋Š” ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๋งŽ์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  ์—ฌ๊ธฐ์—์„œ๋Š” ํ•™์Šต์ด ๋๋‚  ๋•Œ๋งŒ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๊ณ  ์•„๋ž˜์˜ "๋งž์ถคํ˜• ํ•™์Šต ๋ฃจํ”„(A custom training loop)"์—์„œ ์ •๊ธฐ์ ์ธ ํ‰๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์‹ค์ œ๋กœ Trainer API์˜ ํ•œ๊ณ„๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๊ณ  Accelerate ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋น›์„ ๋ฐœํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ํŠน์ • ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋งž๊ฒŒ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉ์ž ์ •์˜ํ•˜๋Š” ๊ฒƒ์€ ํž˜๋“  ์ž‘์—…์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ์™„์ „ํžˆ ๊ณต๊ฐœ๋œ ํ•™์Šต ๋ฃจํ”„๋ฅผ ๋ณ€ํ˜•ํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์Šต๋‹ˆ๋‹ค. TrainingArguments๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: from transformers import TrainingArguments args = TrainingArguments( "bert-finetuned-squad", evaluation_strategy="no", save_strategy="epoch", learning_rate=2e-5, num_train_epochs=3, weight_decay=0.01, fp16=True, push_to_hub=True, ) ์šฐ๋ฆฌ๋Š” ์ด์ „์— ์ด๋“ค ๋Œ€๋ถ€๋ถ„์„ ์‚ดํŽด๋ณธ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ถ€ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ(์˜ˆ: ํ•™์Šต๋ฅ , ํ›ˆ๋ จํ•  ์—ํฌํฌ ์ˆ˜, ์•ฝ๊ฐ„์˜ ๊ฐ€์ค‘์น˜ ๊ฐ์†Œ)๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋ชจ๋“  ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ ๋ชจ๋ธ์„ ์ €์žฅํ•˜๊ณ  ํ‰๊ฐ€๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ , ๊ฒฐ๊ณผ๋ฅผ Model Hub์— ์—…๋กœ๋“œํ•˜๊ฒ ๋‹ค๊ณ  ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ fp16=True๋กœ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ•™์Šต์„ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ตœ๊ทผ์— ์ถœ์‹œ๋œ GPU์—์„œ ํ•™์Šต ์†๋„๋ฅผ ํฌ๊ฒŒ ๋†’์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ €์žฅ์†Œ๋Š” ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์žˆ๊ณ  ์„ค์ •ํ•œ ์ถœ๋ ฅ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ๋ช…๋ช…๋˜๋ฏ€๋กœ ์ด ๊ฒฝ์šฐ์—๋Š” "spasis/bert-finetuned-squad"์— ์žˆ์Šต๋‹ˆ๋‹ค. hub_model_id๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ด๋ฅผ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจ๋ธ์„ huggingface_course ์กฐ์ง์œผ๋กœ ํ‘ธ์‹œ ํ•˜๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” hub_model_id="huggingface_course/bert-finetuned-squad"๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ด ์„น์…˜์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ์—ฐ๊ฒฐํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์ค‘์ธ ์ถœ๋ ฅ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ํ‘ธ์‹œ ํ•˜๋ ค๋Š” ์ €์žฅ์†Œ์˜ ๋กœ์ปฌ ๋ณต์ œ๋ณธ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค(๋”ฐ๋ผ์„œ Trainer๋ฅผ ์ •์˜ํ•  ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ƒˆ ์ด๋ฆ„์„ ์„ค์ •ํ•˜์‹ญ์‹œ์˜ค). ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋“  ๊ฒƒ์„ Trainer ํด๋ž˜์Šค์— ์ „๋‹ฌํ•˜๊ณ  ํ•™์Šต์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: from transformers import Trainer trainer = Trainer( model=model, args=args, train_dataset=train_dataset, eval_dataset=validation_dataset, tokenizer=tokenizer, ) trainer.train() ํ•™์Šต์ด ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ ๋ชจ๋ธ์ด ์ €์žฅ๋  ๋•Œ๋งˆ๋‹ค(์—ฌ๊ธฐ์„œ๋Š” ๋ชจ๋“  ์—ํฌํฌ) ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ๋จธ์‹ ์—์„œ ํ•™์Šต์„ ์žฌ๊ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ํ•™์Šต์€ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋ฏ€๋กœ(Titan RTX์—์„œ๋Š” 1์‹œ๊ฐ„ ๋‚จ์ง“) ์ปคํ”ผ๋ฅผ ๋งˆ์‹œ๊ฑฐ๋‚˜ ์ง„ํ–‰ํ•˜๋Š” ๋™์•ˆ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต๋‹ค๊ณ  ์ƒ๊ฐํ•œ ๊ณผ์ •์˜ ์ผ๋ถ€๋ฅผ ๋‹ค์‹œ ์ฝ์–ด๋ณด์‹œ๊ธฐ๋ฅผ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ฒซ ๋ฒˆ์งธ ์—ํฌํฌ๊ฐ€ ์™„๋ฃŒ๋˜๋Š” ์ฆ‰์‹œ ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ๋œ ์ผ๋ถ€ ๊ฐ€์ค‘์น˜๊ฐ€ ํ‘œ์‹œ๋˜๊ณ  ํ•ด๋‹น ํŽ˜์ด์ง€์—์„œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์ด ์™„๋ฃŒ๋˜๋ฉด ๋งˆ์นจ๋‚ด ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Trainer์˜ predict() ๋ฉ”์„œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ ๋ชจ๋ธ์˜ ์˜ˆ์ธก์ด ๋  ํŠœํ”Œ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค(์—ฌ๊ธฐ์„œ๋Š” ์‹œ์ž‘ ๋ฐ ๋ ๋กœ์ง“๊ณผ ์Œ). ์ด๊ฒƒ์„ ์šฐ๋ฆฌ์˜ compute_metrics() ํ•จ์ˆ˜๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค: predictions, _, _ = trainer.predict(validation_dataset) start_logits, end_logits = predictions compute_metrics(start_logits, end_logits, validation_dataset, raw_datasets["validation"]) ์ข‹์Šต๋‹ˆ๋‹ค! ๋น„๊ต๋ฅผ ์œ„ํ•ด์„œ ์ด ๋ชจ๋ธ์— ๋Œ€ํ•œ BERT ๋…ผ๋ฌธ์— ๋ณด๊ณ ๋œ ๊ธฐ์ค€ ์ ์ˆ˜๋Š” 80.8๊ณผ 88.5์ด๋ฏ€๋กœ ๋น„์Šทํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ push_to_hub() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์‹  ๋ฒ„์ „์˜ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ–ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. trainer.push_to_hub(commit_message="Training complete") ์œ„ ์‹คํ–‰ ๊ฒฐ๊ณผ ์ˆ˜ํ–‰ํ•œ ์ปค๋ฐ‹์˜ URL์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค: 'https://huggingface.co/sgugger/bert-finetuned-squad/commit/9dcee1fbc25946a6ed4bb32efb1bd71d5fa90b68 ๋˜ํ•œ Trainer๋Š” ๋ชจ๋“  ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ํฌํ•จ๋œ ๋ชจ๋ธ ์นด๋“œ์˜ ์ดˆ์•ˆ์„ ์ž‘์„ฑํ•˜์—ฌ ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„์—์„œ Model Hub์˜ ์ถ”๋ก  ์œ„์ ฏ(inference widget)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•˜๊ณ  ์นœ๊ตฌ, ๊ฐ€์กฑ ๋ฐ ์ข‹์•„ํ•˜๋Š” ์• ์™„๋™๋ฌผ๊ณผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์งˆ์˜์‘๋‹ต ํƒœ์Šคํฌ์—์„œ ๋ชจ๋ธ์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! โœ Your turn! ๋‹ค๋ฅธ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ด์šฉํ•ด ๋ณด๊ณ , ์ด ํƒœ์Šคํฌ์—์„œ ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š”์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค! ํ•™์Šต ๋ฃจํ”„์— ๋Œ€ํ•ด ์ข€ ๋” ์ž์„ธํžˆ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์ด์ œ Accelerate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ •์˜ ํ•™์Šต ๋ฃจํ”„ ์ด์ œ ์ „์ฒด ํ•™์Šต ๋ฃจํ”„๋ฅผ ์‚ดํŽด๋ณด๊ณ  ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ์‰ฝ๊ฒŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ฃจํ”„๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” 3์žฅ์˜ ํ•™์Šต ๋ฃจํ”„์™€ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. Trainer ํด๋ž˜์Šค ์‚ฌ์šฉ์— ์˜ํ•œ ์ œ์•ฝ์„ ๋” ์ด์ƒ ๋ฐ›์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋ธ์„ ์ •๊ธฐ์ ์œผ๋กœ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์„ ์œ„ํ•œ ์ „์ฒด ์ค€๋น„ ๋จผ์ € ๋ฐ์ดํ„ฐ ์…‹์—์„œ DataLoaders๋ฅผ ๋นŒ๋“œ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹์˜<NAME>์„ "torch"๋กœ ์„ค์ •ํ•˜๊ณ  ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒ€์ฆ ์ง‘ํ•ฉ(validation set) ์—ด์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ Transformers์—์„œ ์ œ๊ณตํ•˜๋Š” default_data_collator๋ฅผ collate_fn์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , ํ•™์Šต ์ง‘ํ•ฉ์€ ์…”ํ”Œ๋ง(shuffling)์„ ํ•˜๊ณ  ๊ฒ€์ฆ ์ง‘ํ•ฉ์€ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค: from torch.utils.data import DataLoader from transformers import default_data_collator train_dataset.set_format("torch") validation_set = validation_dataset.remove_columns(["example_id", "offset_mapping"]) validation_set.set_format("torch") train_dataloader = DataLoader( train_dataset, shuffle=True, collate_fn=default_data_collator, batch_size=8, ) eval_dataloader = DataLoader( validation_set, collate_fn=default_data_collator, batch_size=8 ) ๋‹ค์Œ์œผ๋กœ ์ด์ „์˜ ๋ฏธ์„ธ ์กฐ์ •์„ ๊ณ„์†ํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์ „ ํ•™์Šต๋œ BERT ๋ชจ๋ธ์—์„œ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋„๋ก ๋ชจ๋ธ์„ ์ƒˆ๋กญ๊ฒŒ ์ธ์Šคํ„ด์Šคํ™”ํ•ฉ๋‹ˆ๋‹ค. model = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint) ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ตœ์ ํ™” ํ”„๋กœ๊ทธ๋žจ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ, Adam๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์ด์ง€๋งŒ ๊ฐ€์ค‘์น˜ ๊ฐ์†Œ ์ ์šฉ ๋ฐฉ์‹์ด ์ˆ˜์ •๋œ ๊ณ ์ „์ ์ธ AdamW๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. from torch.optim import AdamW optimizer = AdamW(model.parameters(), lr=2e-5) ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๋ชจ๋‘ ์ƒ์„ฑํ•œ ๋‹ค์Œ ์ด๋“ค ๋ชจ๋‘๋ฅผ accelerator.prepare() ๋ฉ”์„œ๋“œ๋กœ ๋ณด๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Colab ๋…ธํŠธ๋ถ์—์„œ TPU๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•™์Šตํ•˜๋ ค๋ฉด ์ด ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ํ•™์Šต ํ•จ์ˆ˜๋กœ ์˜ฎ๊ฒจ์•ผ ํ•˜๋ฉฐ Accelerator๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ์…€์„ ์‹คํ–‰ํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. Accelerator์— fp16=True๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ํ˜ผํ•ฉ ์ •๋ฐ€๋„ ํ•™์Šต์„ ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์Šคํฌ๋ฆฝํŠธ๋กœ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ Accelerate ์„ค์ •์„ ์ ์ ˆํ•˜๊ฒŒ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค). from accelerate import Accelerator accelerator = Accelerator(fp16=True) model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader ) ์ด์ „ ์„น์…˜์—์„œ ๊ณต๋ถ€ํ•œ ๋ฐ”์™€ ๊ฐ™์ด, accelerator.prepare() ๋ฉ”์„œ๋“œ๋ฅผ ๊ฑฐ์นœ ํ›„์˜ train_dataloader ๊ธธ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ๋‹จ๊ณ„ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์„น์…˜์—์„œ์™€ ๋™์ผํ•œ ์„ ํ˜• ์Šค์ผ€์ฅด๋ง์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค: from transformers import get_scheduler num_train_epochs = 3 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) ๋ชจ๋ธ์„ Hub๋กœ ํ‘ธ์‹œ ํ•˜๋ ค๋ฉด ์ž‘์—… ํด๋”์— Repository ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„์ง ๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋จผ์ € Hugging Face Hub์— ๋กœ๊ทธ์ธํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์— ๋ถ€์—ฌํ•  ๋ชจ๋ธ ID์—์„œ ์ €์žฅ์†Œ ์ด๋ฆ„์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. repo_name์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ์ž์œ ๋กญ๊ฒŒ ๋ฐ”๊พธ์‹ญ์‹œ์˜ค. get_full_repo_name() ํ•จ์ˆ˜๊ฐ€ ํ˜„์žฌ ์‚ฌ์šฉ์ž ์ด๋ฆ„๋งŒ ํฌํ•จํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: from huggingface_hub import Repository, get_full_repo_name model_name = "bert-finetuned-squad-accelerate" repo_name = get_full_repo_name(model_name) repo_name ๊ทธ๋Ÿฐ ๋‹ค์Œ ํ•ด๋‹น ์ €์žฅ์†Œ๋ฅผ ๋กœ์ปฌ ํด๋”์— ๋ณต์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์ด ๋กœ์ปฌ ํด๋”๋Š” ์ž‘์—… ์ค‘์ธ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๋ณต์ œ๋ณธ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: output_dir = "bert-finetuned-squad-accelerate" repo = Repository(output_dir, clone_from=repo_name) ์ด์ œ repo.push_to_hub() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ output_dir์— ์ €์žฅํ•œ ๋ชจ๋“  ๊ฒƒ์„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ ์ค‘๊ฐ„ ๋ชจ๋ธ์„ ์—…๋กœ๋“œํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๋ฃจํ”„ ์ด์ œ ์ „์ฒด ํ•™์Šต ๋ฃจํ”„๋ฅผ ์ž‘์„ฑํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ์ง„ํ–‰ ๋ฐฉ์‹์„ ๋”ฐ๋ฅด๊ธฐ ์œ„ํ•ด ์ง„ํ–‰๋ฅ  ํ‘œ์‹œ์ค„์„ ์ •์˜ํ•œ ํ›„ ๋ฃจํ”„๋Š” ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค: ํ•™์Šต ๋ถ€๋ถ„์€ train_dataloader๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์ฐธ์กฐํ•˜๋ฉฐ ์ˆ˜ํ–‰๋˜๋ฉด์„œ ๋ชจ๋ธ์„ ํ†ตํ•ด ์ •๋ฐฉํ–ฅ ์ „๋‹ฌ(forward pass)์„ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ ์—ญ๋ฐฉํ–ฅ ์ „๋‹ฌ(backward pass) ๋ฐ ์ตœ์ ํ™” ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ถ€๋ถ„์—์„œ๋Š” start_logits ๋ฐ end_logits์— ๋Œ€ํ•œ ๋ชจ๋“  ๊ฐ’์„ ์ˆ˜์ง‘ํ•˜์—ฌ NumPy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ฃจํ”„๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ๋ชจ๋“  ๊ฒฐ๊ณผ๋ฅผ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, Accelerator๊ฐ€ ๊ฐ ํ”„๋กœ์„ธ์Šค์—์„œ ๋™์ผํ•œ ์ˆ˜์˜ ์˜ˆ์ œ๋ฅผ ๊ฐ–๋„๋ก ๋์— ๋ช‡ ๊ฐ€์ง€ ์ƒ˜ํ”Œ์„ ์ถ”๊ฐ€ํ–ˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ผ๋ถ€๋ฅผ ์ž˜๋ผ๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ €์žฅ ๋ฐ ์—…๋กœ๋“œ ๋‹จ๊ณ„๋กœ์„œ, ๋จผ์ € ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ €์žฅํ•œ ๋‹ค์Œ repo.push_to_hub()๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด์ „์— ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ, blocking=False ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Hub ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋น„๋™๊ธฐ ํ”„๋กœ์„ธ์Šค์—์„œ ํ‘ธ์‹œํ•˜๋„๋ก ๋ช…๋ นํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•™์Šต์€ ์ •์ƒ์ ์œผ๋กœ ๊ณ„์†๋˜๊ณ  ์ด (๊ธด) ๋ช…๋ น์€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๋ฃจํ”„์˜ ์ „์ฒด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: from tqdm.auto import tqdm import torch progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): # ํ•™์Šต model.train() for step, batch in enumerate(train_dataloader): outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) # ํ‰๊ฐ€ model.eval() start_logits = [] end_logits = [] accelerator.print("Evaluation!") for batch in tqdm(eval_dataloader): with torch.no_grad(): outputs = model(**batch) start_logits.append(accelerator.gather(outputs.start_logits).cpu().numpy()) end_logits.append(accelerator.gather(outputs.end_logits).cpu().numpy()) start_logits = np.concatenate(start_logits) end_logits = np.concatenate(end_logits) start_logits = start_logits[: len(validation_dataset)] end_logits = end_logits[: len(validation_dataset)] metrics = compute_metrics( start_logits, end_logits, validation_dataset, raw_datasets["validation"] ) print(f"epoch {epoch}:", metrics) # ์ €์žฅ ๋ฐ ์—…๋กœ๋“œ accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub(commit_message=f"Training in progress epoch {epoch}", blocking=False) Accelerate๋กœ ์ €์žฅ๋œ ๋ชจ๋ธ์„ ์ฒ˜์Œ ๋ณด๋Š” ๊ฒฝ์šฐ, ์ž ์‹œ ์‹œ๊ฐ„์„ ๋‚ด์–ด ํ•จ๊ป˜ ์ œ๊ณต๋˜๋Š” ์„ธ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค: acceleratorator.wiat_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) ์ฒซ ๋ฒˆ์งธ ์ค„์€ ์„ค๋ช…์ด ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ˜„์žฌ ๋ผ์ธ๊นŒ์ง€ ์‹คํ–‰๋˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ €์žฅํ•˜๊ธฐ ์ „์— ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค์—์„œ ๋™์ผํ•œ ๋ชจ๋ธ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์šฐ๋ฆฌ๊ฐ€ ์ •์˜ํ•œ ๊ธฐ๋ณธ ๋ชจ๋ธ์ธ unwrapped_model์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. accelerator.prepare() ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋ธ์„ ๋ถ„์‚ฐ ํ•™์Šต์—์„œ ์ž‘๋™ํ•˜๋„๋ก ๋ณ€๊ฒฝํ•˜๋ฏ€๋กœ ๋” ์ด์ƒ save_pretrained() ๋ฉ”์„œ๋“œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. accelerator.unwrap_model() ๋ฉ”์„œ๋“œ๋Š” ์ด ๊ณผ์ •์— ๋Œ€ํ•œ ์‹คํ–‰์„ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ save_pretrained()๋ฅผ ํ˜ธ์ถœํ•˜์ง€๋งŒ ๊ทธ ๋ฉ”์„œ๋“œ์— torch.save() ๋Œ€์‹  accelerator.save()๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด Trainer๋กœ ํ•™์Šต๋œ ๊ฒƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋ธ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šตํ•œ ๋ชจ๋ธ์€ huggingface-course/bert-finetuned-squad-accelerate์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต ๋ฃจํ”„์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ๋“ฑ์„ ์‹œ๋„ํ•˜๋ ค๋ฉด ์œ„์— ํ‘œ์‹œ๋œ ์ฝ”๋“œ๋ฅผ ํŽธ์ง‘ํ•˜์—ฌ ์ง์ ‘ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ๋ฏธ์„ธ์กฐ์ •๋œ ๋ชจ๋ธ ์‚ฌ์šฉํ•˜๊ธฐ ์ถ”๋ก  ์œ„์ ฏ์„ ์‚ฌ์šฉํ•˜์—ฌ Model Hub์—์„œ ๋ฏธ์„ธ ์กฐ์ •ํ•œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ด๋ฏธ ์‚ดํŽด๋ณธ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์„ pipeline์—์„œ ๋กœ์ปฌ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ชจ๋ธ ์‹๋ณ„์ž๋ฅผ ์ง€์ •ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค: from transformers import pipeline # ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ณธ์ธ ๊ฒƒ์œผ๋กœ ๊ต์ฒดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. model_checkpoint = "spasis/bert-finetuned-squad" question_answerer = pipeline("question-answering", model=model_checkpoint) context = """ Transformers is backed by the three most popular deep learning libraries โ€” Jax, PyTorch and TensorFlow โ€” with a seamless integration between them. It's straightforward to train your models with one before loading them for inference with the other. """ question = "Which deep learning libraries back Transformers?" question_answerer(question=question, context=context) ์ด์ œ ์šฐ๋ฆฌ ๋ชจ๋ธ์€ ์ด ํŒŒ์ดํ”„๋ผ์ธ์˜ ๊ธฐ๋ณธ ๋ชจ๋ธ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค! 8์žฅ. ๋„์›€ ์š”์ฒญ ๋ฐฉ๋ฒ• (How to ask for help) 8์žฅ์€ ํ˜„์žฌ ๋ฒˆ์—ญ ์ง‘ํ•„ ์ž‘์—… ์ค‘์— ์žˆ์Šต๋‹ˆ๋‹ค. ์™„๋ฃŒ๋˜๋Š” ๋Œ€๋กœ ์—…๋กœ๋“œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก  ### ๋ณธ๋ฌธ: ์ €์ž๋Š” IT ์„œ๋น„์Šค ์‚ฐ์—…์—์„œ ์ฒญ์ถ˜์„ ๋ณด๋‚ด๋ฉด์„œ ๊ฐœ๋ฐœ์ž, ๊ฒฝ์˜ ์ง„๋‹จ, ์ „๋žต๊ธฐํš, ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…, ํ•ด์™ธ ์‚ฌ์—… ๋“ฑ์„ ๊ฒฝํ—˜ํ•˜์˜€๊ณ  ์ด์ œ 60๋Œ€ ์ดˆ๋ฐ˜์„ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๋‹ค. ๋กœ๋ด‡๊ณผ ์ธ๊ณต์ง€๋Šฅ์ด ๋‚œ๋ฌดํ•  ๋“ฏํ•œ ๋„๋ž˜ํ•˜๋Š” ๋””์ง€ํ„ธ ์‹œ๋Œ€์— ๋Œ€ํ•œ ๋ฐ˜์ž‘์šฉ์ธ๊ฐ€ ์ €์ž๋Š” ์ตœ๊ทผ ๊ธ€ ์ฝ๊ธฐ์™€ ๊ธ€์“ฐ๊ธฐ์— ๊ด€์‹ฌ์ด ๋†’์•„์ ธ๊ฐ„๋‹ค. ๊ธ€ ์ฝ๋Š” ๊ฒƒ์ด์•ผ ์ €๋ช…์ธ์‚ฌ๋“ค์˜ ์ˆ˜๋งŽ์€ ์ถ”์ฒœ์ž‘๋“ค์ด ์žˆ์œผ๋‹ˆ ์‹œ๊ฐ„ ๋‚  ๋•Œ๋งˆ๋‹ค ์ฝ์„ ๊ฒƒ์ด์ง€๋งŒ ๊ธ€์“ฐ๊ธฐ๋Š” ๋ฌด์—‡๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ข‹์€ ๊ธ€์„ ํ•„์‚ฌํ•˜๋Š” ๊ฒƒ๋„ ํ•œ ๋ฐฉ๋ฒ•์ด๋ ค๋‹ˆ์™€ ์ €์ž๋Š” ๊ทธ๋™์•ˆ ๊ฒฝํ—˜ํ•œ ๊ฒƒ๋“ค์„ ์•„์ง ๋จธ๋ฆฟ์†์— ๋‚จ์•„ ์žˆ์„ ๋•Œ ์ •๋ฆฌํ•ด ๋ณด์ž๋Š” ๋ชฉ์ ์œผ๋กœ ์ €์ˆ ์„ ์‹œ์ž‘ํ•œ๋‹ค. ๊ทธ ๋ชฉ์ ์ด ๊ทธ๋ž˜์„œ์ผ๊นŒ ์—ฌ๋Ÿฌ ๊ถŒ์„ ์“ฐ๊ณ  ์žˆ์œผ๋‹ˆ ์ €์ˆ ์ด๋ผ๊ธฐ๋ณด๋‹ค๋Š” ์ •๋ฆฌ์˜ ๋Š๋‚Œ์ด ๋” ๊ฐ•ํ•œ๋ฐ ์กฐ๊ธˆ ๋” ์‹œ๊ฐ„์ด ํ๋ฅด๊ฑฐ๋‚˜ ์˜คํ”„๋ผ์ธ ์ถœํŒ์œผ๋กœ ์ด์–ด์ง€๋ฉด Case Study ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ €์ˆ ์˜ ๋ชจ์–‘์ƒˆ๋ฅผ ๊ฐ–์ถ”๊ฒŒ ๋  ์ˆ˜๋„ ์žˆ์œผ๋ฆฌ๋ผ ์ƒ๊ฐ๋œ๋‹ค. ์ด๋ฒˆ์—์„œ๋Š” ์ปจ์„คํŒ…์„ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ์ปจ์„คํŒ…์€ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ํฅ๋ฏธ๋กœ์›Œ ํ•˜๋Š” ์˜์—ญ์ž„๊ณผ ๋™์‹œ์— ๋งค์šฐ ์ „๋ฌธ์ ์ด์–ด์„œ ๋ฐฐํƒ€์ ์ธ ์˜์—ญ์ด๊ธฐ๋„ ํ•˜๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชฉ์ฐจ๋กœ ์ง„ํ–‰๋  ๊ณ„ํš์ธ๋ฐ ์ œ๋Œ€๋กœ ์ฃฝ ๋ณด๊ณ  ๋‚˜๋ฉด ์–ด๋”œ ๊ฐ€๋„ ๋น ์ง€์ง€ ์•Š๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…์— ๋Œ€ํ•ด ํ•œ ๋งˆ๋”” ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ฑ…์œผ๋กœ ๋„์›€์„ ์–ป๋Š” ๊ฒƒ์€ ๋”ฑ ๊ฑฐ๊ธฐ๊นŒ์ง€์ด๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ž์‹ ์ด ์ง์ ‘ ์‹ค์ œ๋กœ ํ•ด๋ณด๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ €์ž๋Š” ๋ญ๋“ ์ง€ ์ง์ ‘ ํ•ด๋ณด๋Š” ๊ฒƒ๋งŒํ•œ ๊ฒƒ์€ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๊ทธ๋Ÿฐ ์ฐจ์›์—์„œ ์ปจ์„คํŒ…์„ ๋‹ค๋ฃจ๋Š” ์ด ๋งค๊ฑฐ์ง„์€ ์™œ ์ปจ์„คํŒ… ํ™”๋‘์˜€๋Š”์ง€ ์•ž์œผ๋กœ๋Š” ์–ด๋–ป๊ฒŒ ๋ ์ง€ ์ด ์ผ์„ ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์€ ์–ด๋–ป๊ฒŒ ์ผํ•˜๊ณ  ์žˆ๋Š”์ง€ ์ดํ•ดํ•˜๋Š”๋ฐ ์ถฉ๋ถ„ํ•œ ๋„์›€์„ ์ค„ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•œ๋‹ค. ๋งŽ์€ ๋…์ž๋‹˜๋“ค๊ณผ ์˜ค๋‹ค๊ฐ€๋‹ค ๊ธ€์„ ๋ณด๊ฒŒ ๋˜๋Š” ๋…์ž๋‹˜๋“ค์˜ ์ข‹์€ ํ”ผ๋“œ๋ฐฑ์„ ๊ธฐ๋‹ค๋ฆฐ๋‹ค. 00.FrontPage Prologue 2016๋…„์— ์ €์ž๋Š” ๋‘ ๊ถŒ์˜ ์ฑ…์„ ์ €์ˆ ํ–ˆ๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ ์ฑ…์ด 'B2B ๋งˆ์ผ€ํŒ…/์˜์—…, 21์ผ์˜ ์—ฌํ–‰'์ด๋ผ๋Š” ์ œ๋ชฉ์ด์—ˆ๊ณ  ๋‘ ๋ฒˆ์งธ ์ฑ…์€ '์ปจ์„คํŒ… ๋‹ค์‹œ ๋ณด๊ธฐ'์˜€๋‹ค. ๋ชจ๋‘ ์ถœํŒ์„ ๋ชฉ์ ์œผ๋กœ ๊ธฐํšํ–ˆ๋˜ ๊ฒƒ๋“ค์ด์—ˆ๋Š”๋ฐ ์ด๋Ÿฐ์ €๋Ÿฐ ์‚ฌ์ •์œผ๋กœ ์•„์ง๊นŒ์ง€ ์˜คํ”„๋ผ์ธ ์ถœํŒ์œผ๋กœ ์ด์–ด์ง€์ง€ ์•Š์•˜๊ณ , ์šฐ์—ฐํ•œ ๊ธฐํšŒ์— ์•Œ๊ฒŒ ๋œ ๋ธŒ๋Ÿฐ์น˜๋ฅผ ํ†ตํ•ด ๊ทธ ๋‚ด์šฉ์„ ๊ณต๊ฐœํ•˜๊ณ  ์žˆ๋‹ค. ๊ฐœ์ธ์ ์œผ๋กœ ๊ธ€์„ ์“ฐ๋Š” ๊ฒƒ์€ ์ข‹์€ ํž๋ง์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. IT ์„œ๋น„์Šค ์‚ฐ์—…์—์„œ ์ฒญ์ถ˜์„ ๋ณด๋‚ด๋ฉด์„œ ๊ฐœ๋ฐœ์ž, ๊ฒฝ์˜ ์ง„๋‹จ, ์ „๋žต๊ธฐํš, ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…, ํ•ด์™ธ ์‚ฌ์—… ๋“ฑ์„ ๊ฒฝํ—˜ํ•˜์˜€๊ณ  ์ด์ œ 40๋Œ€ ์ค‘๋ฐ˜์„ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๋‹ค. ๋กœ๋ด‡๊ณผ ์ธ๊ณต์ง€๋Šฅ์ด ๋‚œ๋ฌดํ•  ๋“ฏํ•œ ๋„๋ž˜ํ•˜๋Š” ๋””์ง€ํ„ธ ์‹œ๋Œ€์— ๋Œ€ํ•œ ๋ฐ˜์ž‘์šฉ์ธ๊ฐ€ ์ €์ž๋Š” ์ตœ๊ทผ ๊ธ€ ์ฝ๊ธฐ์™€ ๊ธ€์“ฐ๊ธฐ์— ๊ด€์‹ฌ์ด ๋†’์•„์ ธ๊ฐ„๋‹ค. ์š”์ฆ˜์€ ์ฃผ๋ง์ด๋ฉด ์•„์ด์™€ ํ•จ๊ป˜ ๋„์„œ๊ด€์„ ๋งŽ์ด ์ฐพ๊ณ  ์žˆ๋‹ค. ๊ธ€ ์ฝ๋Š” ๊ฒƒ์ด์•ผ ์ €๋ช…์ธ์‚ฌ๋“ค์˜ ์ˆ˜๋งŽ์€ ์ถ”์ฒœ์ž‘๋“ค์ด ์žˆ์œผ๋‹ˆ ์‹œ๊ฐ„ ๋‚  ๋•Œ๋งˆ๋‹ค ์ฝ์„ ๊ฒƒ์ด์ง€๋งŒ ๊ธ€์“ฐ๊ธฐ๋Š” ๋ฌด์—‡๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ข‹์€ ๊ธ€์„ ํ•„์‚ฌํ•˜๋Š” ๊ฒƒ๋„ ํ•œ ๋ฐฉ๋ฒ•์ด๋ ค๋‹ˆ์™€ ์ €์ž๋Š” ๊ทธ๋™์•ˆ ๊ฒฝํ—˜ํ•œ ๊ฒƒ๋“ค์„ ์•„์ง ๋จธ๋ฆฟ์†์— ๋‚จ์•„ ์žˆ์„ ๋•Œ ์ •๋ฆฌํ•ด ๋ณด์ž๋Š” ๋ชฉ์ ์œผ๋กœ ์ €์ˆ ์„ ์‹œ์ž‘ํ•˜์˜€๋‹ค. ๊ทธ ๋ชฉ์ ์ด ๊ทธ๋ž˜์„œ์ผ๊นŒ ๋‘ ๊ถŒ์„ ์“ฐ๊ณ  ๋‚˜๋‹ˆ ์ €์ˆ ์ด๋ผ๊ธฐ๋ณด๋‹ค๋Š” ์ •๋ฆฌ์˜ ๋Š๋‚Œ์ด ๋” ๊ฐ•ํ•œ๋ฐ ์กฐ๊ธˆ ๋” ์‹œ๊ฐ„์ด ํ๋ฅด๊ฑฐ๋‚˜ ์˜คํ”„๋ผ์ธ ์ถœํŒ์œผ๋กœ ์ด์–ด์ง€๋ฉด Case Study ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ €์ˆ ์˜ ๋ชจ์–‘์ƒˆ๋ฅผ ๊ฐ–์ถ”๊ฒŒ ๋  ์ˆ˜๋„ ์žˆ์œผ๋ฆฌ๋ผ ์ƒ๊ฐ๋œ๋‹ค. ์ด๋ฒˆ ๋งค๊ฑฐ์ง„์—์„œ๋Š” ์ปจ์„คํŒ…์„ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ์ปจ์„คํŒ…์€ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ํฅ๋ฏธ๋กœ์›Œ ํ•˜๋Š” ์˜์—ญ์ž„๊ณผ ๋™์‹œ์— ๋งค์šฐ ์ „๋ฌธ์ ์ด์–ด์„œ ๋ฐฐํƒ€์ ์ธ ์˜์—ญ์ด๊ธฐ๋„ ํ•˜๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชฉ์ฐจ๋กœ ์ง„ํ–‰๋  ๊ณ„ํš์ธ๋ฐ ์ œ๋Œ€๋กœ ์ฃฝ ๋ณด๊ณ  ๋‚˜๋ฉด ์–ด๋”œ ๊ฐ€๋„ ๋น ์ง€์ง€ ์•Š๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…์— ๋Œ€ํ•ด ํ•œ ๋งˆ๋”” ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ฑ…์œผ๋กœ ๋„์›€์„ ์–ป๋Š” ๊ฒƒ์€ ๋”ฑ ๊ฑฐ๊ธฐ๊นŒ์ง€์ด๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ž์‹ ์ด ์ง์ ‘ ์‹ค์ œ๋กœ ํ•ด๋ณด๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ €์ž๋Š” ๋ญ๋“ ์ง€ ์ง์ ‘ ํ•ด๋ณด๋Š” ๊ฒƒ๋งŒํ•œ ๊ฒƒ์€ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๊ทธ๋Ÿฐ ์ฐจ์›์—์„œ ์ปจ์„คํŒ…์„ ๋‹ค๋ฃจ๋Š” ์ด ๋งค๊ฑฐ์ง„์€ ์™œ ์ปจ์„คํŒ… ํ™”๋‘์˜€๋Š”์ง€ ์•ž์œผ๋กœ๋Š” ์–ด๋–ป๊ฒŒ ๋ ์ง€ ์ด ์ผ์„ ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์€ ์–ด๋–ป๊ฒŒ ์ผํ•˜๊ณ  ์žˆ๋Š”์ง€ ์ดํ•ดํ•˜๋Š”๋ฐ ์ถฉ๋ถ„ํ•œ ๋„์›€์„ ์ค„ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•œ๋‹ค. ๋งŽ์€ ๋ธŒ๋Ÿฐ์น˜ ๋…์ž๋‹˜๋“ค๊ณผ ์˜ค๋‹ค๊ฐ€๋‹ค ๊ธ€์„ ๋ณด๊ฒŒ ๋˜๋Š” ๋…์ž๋‹˜๋“ค์˜ ์ข‹์€ ํ”ผ๋“œ๋ฐฑ์„ ๊ธฐ๋‹ค๋ฆฐ๋‹ค. 00. ๋ชฉ์ฐจ [๋ชฉ์ฐจ] Prologue Part I. ์ปจ์„คํŒ… ์‚ฐ์—…์€ ๋ถ€ํ™œํ• ๊นŒ? 1. ์ปจ์„คํŒ… ์ •์˜์™€ ์ข…๋ฅ˜ 2. ์ปจ์„คํŒ… ์‚ฐ์—…์˜ ํ˜„ํ™ฉ 3. ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์˜ ์ „์Ÿ Part II. ์ปจ์„คํŒ… ์Šคํ‚ฌ 1. ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  2. ๋ฌธ์ œ ํ•ด๊ฒฐ๊ธฐ๋ฒ• 3. ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ Part III. ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ• 1. ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„ 2. ๊ณ ๊ฐ ์š”๊ตฌ ๋ถ„์„ 3. ์ˆ˜์ต์„ฑ ๋ถ„์„ 4. ์—ญ๋Ÿ‰ ๋ถ„์„ 5. ์‹œ์‚ฌ์  ๋ฐ ๋Œ€์•ˆ ๋„์ถœ Part IV. ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก  1. ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ๋ฐฉ๋ฒ•๋ก  2. ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•๋ก  3. ํ”„๋กœ์„ธ์Šค ํ˜์‹  4. ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ 5. ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„ 6. ์ •๋ณด์ „๋žต ์ปจ์„คํŒ…(BPR/ISP) ๋ฐฉ๋ฒ•๋ก  Part V. ์ปจ์„คํŒ… ์‚ฌ์—… ๊ฐœ๋ฐœ ๋ฐ ์ดํ–‰ 1. ์ปจ์„คํŒ… ์‚ฌ์—… ๊ฐœ๋ฐœ 2. ์„ฑ๊ณตํ•˜๋Š” ์ปจ์„คํŒ… ์‚ฌ์—… ์ œ์•ˆ 3. ์ปจ์„คํŒ… ์ดํ–‰๊ณผ ์ง€์‹๊ฒฝ์˜ Epilogue 000 PART I. ์ปจ์„คํŒ… ์‚ฐ์—…์€ ๊ณผ์—ฐ ์œ ๋งํ•œ๊ฐ€? ์–ด๋–ค ์ผ์„ ์‹œ์ž‘ํ•  ๋•Œ๋Š” ๊ทธ ์ผ์— ๋Œ€ํ•œ ์ •์˜(ๅฎš็พฉ)๋ฅผ ์ž˜ ์ƒ๊ฐํ•ด ๋ณด๊ณ  ๋ฐ”๋ผ๋ณผ ์ค„ ์•Œ์•„์•ผ ํ•œ๋‹ค. ๊ทธ ์ •์˜์— ์ถฉ์‹คํ•  ๋•Œ ๋ณธ์งˆ(ๆœฌ่ณช)๊ณผ ์ด์–ด์ง€๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋„ ๋ช…ํ™•ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ตœ๊ทผ โ€˜์ปจ์„คํŒ…(Consulting)โ€™์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์—ฌ๋Ÿฌ ๋ถ„์•ผ์—์„œ ๋งŽ์ด ์“ฐ์ด๊ณ  ์žˆ๋‹ค. โ€˜๋ถ€๋™์‚ฐ ์ปจ์„คํŒ…โ€™, โ€˜์ทจ์—… ์ปจ์„คํŒ…โ€™. ์‹ฌ์ง€์–ด ๊ฐœ ํ‚ค์šฐ๋Š” ๊ฒƒ๋„ โ€˜Pet ์ปจ์„คํŒ…โ€™์ด๋ผ๋Š” ๋ง์„ ๋ถ™์ด๊ณ  ์žˆ๋‹ค. ์ปจ์„คํŒ…์˜ ์˜๋ฏธ๋ฅผ ์ „๋ฌธ์ ์ธ ์ง€์‹์„ ๊ฐ€์ง€๊ณ  ์ž๋ฌธ(่ซฎๅ•)์„ ํ•ด์ค€๋‹ค๋Š” ์ธก๋ฉด์—์„œ๋Š” ๊ทธ ๋ช…์นญ๋“ค์€ ํƒ€๋‹นํ•  ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ทธ๋Ÿฐ ์ƒํ™ฉ์„ ์ ‘ํ•˜๋ฉด์„œ ์ „์ง ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํ„ดํŠธ์˜€๋˜ ์ €์ž๋Š” ์•ฝ๊ฐ„ ์•„์‰ฌ์›€๋„ ๋งˆ์Œ ํ•œํŽธ์— ๊ฐ™์ด ๋“ค๊ณ  ์žˆ๋‹ค. ์˜คํžˆ๋ ค ์ปจ์„คํŒ…์— ๋Œ€ํ•œ ๋ณธ์งˆ(ๆœฌ่ณช)์€ ์žŠ์–ด๋ฒ„๋ฆฐ ์ฑ„ ๊ดœํžˆ ๋ฐ”๋žŒ๋งŒ ๋“ค์–ด๊ฐ€ ์žˆ์—ˆ๋˜ ๊ฒƒ์ผ๊นŒ? ์‚ฌ์‹ค ์ปจ์„คํŒ…์€ ํ•ด๋‹น ์—…(ๆฅญ)์— ๋Œ€ํ•œ ๋งŽ์€ ์ง€์‹๊ณผ ๊ฒฝํ—˜์ด ํ•„์š”ํ•œ ๊ณ ๊ธ‰ ๋น„์ฆˆ๋‹ˆ์Šค์ด๋‹ค. ๊ทธ ์—…๋ฌด ๊ฐ•๋„ ๋˜ํ•œ ๋งŒ๋งŒ์น˜ ์•Š์•„ ๊ฑด์„ค ํ˜„์žฅ์˜ ๋…ธ๋™๊ณผ ๊ณง์ž˜ ๋น„๊ต๋˜๊ธฐ๋„ ํ•œ๋‹ค. ๋˜ํ•œ, ํ•ญ์ƒ ์ƒˆ๋กœ์šด ๊ฒƒ์„ ์ ‘ํ•˜๊ณ  ์ง€์  ํƒ๊ตฌ๋ฅผ ํ†ตํ•ด ํ•ด๋ฒ•๊ณผ ์„ค๋ฃจ์…˜์„ ์ฐพ์•„๊ฐ€๋Š” ๊ฒƒ์ด ์ฃผ์š” ์—…๋ฌด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ์„ฑํ–ฅ์˜ ์ผ์„ ์ข‹์•„ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์ด๋ผ๋ฉด ์ปจ์„คํ„ดํŠธ(Consultants)๋Š” ๋งค์šฐ ์ข‹์€ ์ง์—…์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์—ญ์‹œ ์ปจ์„คํŒ… ์‚ฐ์—…์€ ์œ ๋งํ•˜๋‹ค๊ณ  ํ•  ๊ฒƒ์ด๋‹ค. ์ €์ž์˜ ๊ธฐ์–ต์— ์ปจ์„คํŒ…์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๊ฒŒ ๋œ ๊ฒƒ์ด 1996๋…„ ๋ฌด๋ ต โ€˜๋ถ€์ฆˆ ์•Œ๋žœ & ํ•ด๋ฐ€ํ„ดโ€™[1]์ด๋ผ๋Š” ์ปจ์„คํŒ… ํšŒ์‚ฌ์—์„œ ๋ฐœ๊ฐ„ํ•œ ํŒŒ๋ž€ ํ‘œ์ง€์˜ ์ฑ… ํ•œ ๊ถŒ์„ ์ ‘ํ•˜๋ฉด์„œ์˜€๋‹ค. 1996๋…„์€ ์„ธ๊ธฐ๋ง์„ ๋งž์ดํ•œ๋‹ค๋Š” ์Œ์šธํ•œ ๋ถ„์œ„๊ธฐ์™€ ํ•จ๊ป˜ ์•„์‹œ์•„ ๊ฒฝ์ œ๊ฐ€ ๊ธˆ์œต ์œ„๊ธฐ๋ฅผ ๋งž์ดํ•˜๊ณ  ์žˆ์–ด ์šฐ๋ฆฌ๋‚˜๋ผ๋„ ๊ฒฝ์ œ๊ฐ€ ๋งค์šฐ ์–ด๋ ค์šด ์ƒํ™ฉ์ด์—ˆ๋‹ค. ์ด๋“ฌํ•ด 1997๋…„์—๋Š” ์šฐ๋ฆฌ๋‚˜๋ผ๋Š” ๊ตญ๊ฐ€๋ถ€๋„ ์ƒํƒœ๊ฐ€ ๋˜์–ด ๊ตญ์ œํ†ตํ™”๊ธฐ๊ธˆ(IMF[2])์œผ๋กœ๋ถ€ํ„ฐ ๊ฒฝ์ œ ํšŒ์ƒ์„ ์œ„ํ•œ ๊ตฌ์ œ ๊ธˆ์œต์„ ์ œ๊ณต๋ฐ›๊ณ  ๊ด€๋ฆฌ ๋Œ€์ƒ์ด ๋˜์–ด์•ผ ํ–ˆ๋˜ ์‹œ๊ธฐ์˜€๋‹ค. ํŒŒ๋ž€ ํ‘œ์ง€์˜ ์ฑ…์€ ๋‹น์‹œ ๋Œ€ํ•œ๋ฏผ๊ตญ์ด ์ง๋ฉดํ•˜๊ณ  ์žˆ๋˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๊ตญ๊ฐ€์  ์ด์Šˆ๋ฅผ ์–ธ๊ธ‰ํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ๊ทธ๊ฒƒ๋“ค์€ ํ•™์ƒ์ด์—ˆ๋˜ ์ €์ž๊ฐ€ ์ทจ์—… ํ›„ ์ฒ˜์Œ ๋งž์ดํ•œ ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ์ถฉ๊ฒฉ์ ์ธ ๋ฏผ๋‚ฏ์ด์—ˆ๋‹ค. ํ•œ ํŽธ์œผ๋กœ๋Š” โ€˜์ด ์‚ฌ๋žŒ๋“ค์€ ๋ˆ„๊ตฌ์ด๊ธธ๋ž˜ ํ•œ ๋‚˜๋ผ์˜ ์ƒํ™ฉ์„ ์–ด๋–ค ๊ธฐ์ค€์—์„œ ๋ถ„์„ํ•˜๊ณ  ์ด๋Ÿฌ ์ €๋Ÿฐ ์กฐ์–ธ๊นŒ์ง€ ํ•˜๋Š”๊ฐ€?โ€™ ํ•˜๋Š” ๋†€๋ผ์›€๊ณผ ์˜๋ฌธ๋„ ๋“ค์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  5๋…„ ๋’ค ๋˜ ๋‹ค๋ฅธ ์ฑ… ํ•œ ๊ถŒ โ€˜๋งฅํ‚จ์ง€ ์›จ์ด(The Mckinsey Way)โ€™์„ ์ ‘ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. IMF ์‚ฌํƒœ ์ดํ›„ ๊ตญ๊ฐ€ ๊ฒฝ์˜์˜ ์ฒด๊ณ„๋ฅผ ๊ธ€๋กœ๋ฒŒ์Šคํƒ ๋”๋“œ(Global Standards)์— ๋งž์ถ”๊ธฐ ์‹œ์ž‘ํ•˜๋ฉด์„œ ๊ฒฝ์˜ ์ปจ์„คํŒ…์ด ๋ณธ๊ฒฉ์ ์œผ๋กœ ์šฐ๋ฆฌ๋‚˜๋ผ ๊ธฐ์—…์— ๋„์ž…๋˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋Š”๋ฐ ๊ทธ ์„ ๋‘์— ์žˆ๋˜ ์ปจ์„คํŒ… ๊ธฐ์—…์ด ๊ทธ ์œ ๋ช…ํ•œ ๋งฅํ‚จ์ง€(Mckinsey& Co.)[3]์˜€๋‹ค. ๋งฅํ‚จ์ง€ ์ปจ์„คํŒ…์ด ์ผํ•˜๋Š” ๋ฐฉ์‹์„ ์†Œ๊ฐœํ•œ ์ด ์ฑ…์€ ์ปจ์„คํ„ดํŠธ๋ž€ ์–ด๋–ค ์ง์—…์ธ์ง€, ๊ทธ๋“ค์€ ์–ด๋–ค ์ผ์„ ํ•˜๋Š”์ง€ ๋Œ€์ค‘๋“ค์—๊ฒŒ ์†Œ๊ฐœํ•œ ๋Œ€ํ‘œ์ ์ธ ์ฑ…์ด์—ˆ๋‹ค. ์ฑ…์€ ๋ฒ ์ŠคํŠธ์…€๋Ÿฌ๊ฐ€ ๋˜์—ˆ๊ณ  ๋‹น์‹œ ์ปจ์„คํ„ดํŠธ๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ๋Š” ํ•„๋…์„œ๊ฐ€ ๋˜๋‹ค์‹œํ”ผํ–ˆ๋‹ค. ๋˜ํ•œ, ๊ทธ ์ฑ…์€ ์ง์žฅ ์ดˆ๋…„์ƒ๋“ค์˜ MBA[4] ๋ถ ์กฐ์„ฑ์— ํ•œ๋ชซํ•˜๊ธฐ๋„ ํ–ˆ๋‹ค. ์ €์ž๋„ ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํ„ดํŠธ๋กœ์จ 6๋…„์„ ์ผํ–ˆ๊ณ  ์ˆ˜๋งŽ์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ–ˆ๋‹ค. ๊ธฐ์—…์˜ ๋น„์ „๊ณผ ์ „๋žต, ํ˜์‹ ์„ ๋‹ค๋ฃฌ๋‹ค๋Š” ์ธก๋ฉด์—์„œ ํ”„๋กœ์ ํŠธ ์ˆ˜ํ–‰ ํ›„ ๋†’์€ ์„ฑ์ทจ๊ฐ๋„ ์žˆ์—ˆ์ง€๋งŒ, ์—…๋ฌด ์ŠคํŠธ๋ ˆ์Šค์™€ ์น˜์—ดํ•œ ๊ฒฝ์Ÿ์„ ํ”ผํ•ด ๋งŽ์€ ์ปจ์„คํ„ดํŠธ๋“ค์ด ์ปจ์„คํŒ… ํšŒ์‚ฌ์—์„œ ๊ณ ๊ฐ ๊ธฐ์—… ์ฆ‰, ํ˜„์—…์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ๋„ ์ง€์ผœ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค. ์ปจ์„คํŒ… ๊ธฐ๋ฒ•๋“ค๋„ ๋” ์ด์ƒ ๋น„๋ฐ€์Šค๋Ÿฌ์šด ๊ทธ๋“ค๋งŒ์˜ ์ „์œ ๋ฌผ์ด ์•„๋‹ˆ๊ฒŒ ๋˜์—ˆ์œผ๋ฉฐ, ์ด์ œ๋Š” ๊ณ ๊ฐ๋“ค๋„ ์ด๋ฅผ ์ž˜ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ปจ์„คํŒ… ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ๋ถ€์ •์ ์ธ ์ธก๋ฉด๋„ ๋งŽ์ด ๋‚˜ํƒ€๋‚ฌ๋‹ค. โ€˜๊ฒฝ์˜์ง„์ด๋‚˜ ํšŒ์‚ฌ์˜ ์ž…์žฅ์—์„œ๋งŒ ๋ฐ”๋ผ๋ณธ๋‹คโ€™, โ€˜๊ตฌ์กฐ์กฐ์ •์˜ ์ฒจ๋ณ‘โ€™, โ€˜์ง์›๋“ค์„ ์œ„ํ•˜๋Š” ๋ฐฉํ–ฅ์ด ์—†๋‹คโ€™ ๋“ฑ์ด ๊ทธ๋Ÿฐ ๊ฒƒ๋“ค์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ €์ž๋Š” ์š”์ฆ˜ ์ปจ์„คํŒ… ์‚ฐ์—…์ด ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ๋ณด์ธ๋‹ค. ์˜ค๋Š˜๋‚  ์‚ฐ์—… ํ™˜๊ฒฝ์€ ๊ฑฐ์˜ ์ „๋ฐฉ์œ„์ ์œผ๋กœ ์ปจ์„คํŒ…์  ์‚ฌ๊ณ ๋ฐฉ์‹์„ ์š”๊ตฌํ•œ๋‹ค. ์ €์ž๋Š” ํ˜„์žฌ ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํ„ดํŠธ๋ฅผ ๊ทธ๋งŒ๋‘๊ณ  B2B ํšŒ์‚ฌ์—์„œ ๋งˆ์ผ€ํŒ…/์˜์—… ์ผ์„ ํ•˜๊ณ  ์žˆ์ง€๋งŒ ์ปจ์„คํŒ… ๊ฒฝํ—˜์ด ์ ์ง€ ์•Š์€ ๋„์›€์„ ์ฃผ๊ณ  ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ €์ž์˜ ๊ฒฝํ—˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ปจ์„คํŒ…์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋Œ์•„๋ณด๊ณ ์ž ํ•œ๋‹ค. ์ปจ์„คํŒ…์ด๋ž€ ๋ฌด์—‡์ด๊ณ  ๊ณผ๊ฑฐ์— ์–ด๋–ป๊ฒŒ ์ผํ–ˆ๊ณ  ์–ด๋–ค ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•, ๋ฐฉ๋ฒ•๋ก ๋“ค์ด ์œ ์šฉํ–ˆ์œผ๋ฉฐ ๊ธฐํšŒ๊ฐ€ ๋˜๋ฉด ์‚ฌ์—…์œผ๋กœ์„œ์˜ ์ปจ์„คํŒ…๊ณผ ์‚ฌ๋ก€์— ๋Œ€ํ•ด์„œ๋„ ๊ธฐ๋กํ•˜๊ณ  ๊ณต์œ ํ•ด ๋ณด๋ ค๊ณ  ํ•œ๋‹ค[5]. ์ €์ž๋Š” ๋…์ž๋“ค์ด ์ด ์ฑ…์„ ํ†ตํ•ด ๋‹ค์Œ ์„ธ ๊ฐ€์ง€๋ฅผ ์–ป๊ธฐ๋ฅผ ํฌ๋งํ•œ๋‹ค. ์ฒซ์งธ, ์ปจ์„คํŒ… ์‚ฐ์—…์„ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๋Š” ๊ฒƒ. ์‚ฌ์—…์€ ์ƒ๋ฌผ์ฒ˜๋Ÿผ ๊ณ„์† ๋ณ€ํ™”ํ•œ๋‹ค. ์ด ์ฑ…์„ ํ†ตํ•ด์„œ ์ปจ์„คํŒ…์ด๋ž€ ๋ฌด์—‡์ธ์ง€? ์ปจ์„คํ„ดํŠธ๋“ค์€ ์–ด๋–ค ์ผ์„ ํ•˜๋Š”์ง€ ์ง์—…์œผ๋กœ์„œ์˜ ์ปจ์„คํ„ดํŠธ๋ฅผ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ๋‘˜์งธ, ์ปจ์„คํŒ…์— ์‚ฌ์šฉ๋˜๋Š” ๋‹ค์–‘ํ•œ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•, ๋ฐฉ๋ฒ•๋ก ์„ ์ดํ•ดํ•˜๊ณ  ์‹ค๋ฌด์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ. ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ (Logical Thinking)๋ฅผ ์š”๊ตฌํ•˜๋Š” ๋‹ค์–‘ํ•œ ์ปจ์„คํŒ… ๊ธฐ๋ฒ•์€ ์ „๋žต๊ธฐํš, ๊ฒฝ์˜ํ˜์‹ , ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ, ๋งˆ์ผ€ํŒ…/์˜์—… ๋“ฑ ๋‹ค์–‘ํ•œ ์—…๋ฌด์—์„œ ํ™œ์šฉ ๊ฐ€๋Šฅํ•œ ๊ธฐ๋ฒ•๋“ค์ด ๋งŽ๋‹ค. ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํšจ์œจ์ ์ธ ์—…๋ฌด ์ˆ˜ํ–‰์— ๋„์›€์ด ๋˜๊ธฐ๋ฅผ ๋ฐ”๋ž€๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์…‹์งธ, ์ด ์ฑ…์ด ์ปจ์„คํ„ดํŠธ๋ฅผ ๋ฏธ๋ž˜์˜ ์ง์—…์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ  ๊ณ ๋ฏผํ•˜๋Š” ํ•™์ƒ๋“ค์ด๋‚˜ ์ปจ์„คํ„ดํŠธ๋กœ์˜ ์ „์ง์„ ์ƒ๊ฐํ•˜๋Š” ์ง์žฅ์ธ๋“ค์—๊ฒŒ ์‹ค์šฉ์ ์ธ ์ง€์นจ์„œ(Practical Guide)๊ฐ€ ๋˜์—ˆ์œผ๋ฉด ํ•œ๋‹ค. ์ €์ž๊ฐ€ ์ปจ์„คํ„ดํŠธ๊ฐ€ ๋˜๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋˜ ๋•Œ๋Š” ์ฐธ ๋ง‰์—ฐํ–ˆ์—ˆ๋‹ค. ์ •๋ณด๋„ ์—†์—ˆ๊ณ  ์–ด๋–ค ๊ฒฝ๋กœ๋กœ ๊ทธ๋ ‡๊ฒŒ ๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๊ธฐ๋„ ์–ด๋ ค์› ๋‹ค. ์ผํ•˜๋ฉด์„œ ์กฐ๊ธˆ์”ฉ ๊ตฌ์ฒดํ™”ํ•ด ๋‚˜๊ฐ”๊ณ  ๊ทธ ๊ณผ์ •์€ ์ข‹์€ ๊ฒฝํ—˜์ด์—ˆ์ง€๋งŒ ๋’ค๋Œ์•„๋ณด๋ฉด ํ›„ํšŒ๋˜๋Š” ์ผ๋„ ๋งŽ๋‹ค. ์ €์ž๊ฐ€ ๋„์ „ํ•˜๋˜ ์‹œ์ ˆ๊ณผ ์ง€๊ธˆ์˜ ์ปจ์„คํ„ดํŠธ์˜ ์˜๋ฏธ๋‚˜ ์œ„์ƒ์€ ๋ถ„๋ช…ํžˆ ๋‹ค๋ฅด์ง€๋งŒ ์ƒˆ๋กญ๊ฒŒ ์ „๊ฐœ๋˜๋Š” ์‹œ๋Œ€์— ํ›„๋ฐฐ๋“ค์ด ๊ฒช์„ ๊ทธ๋Ÿฐ ์‹œํ–‰์ฐฉ์˜ค๋ฅผ ์กฐ๊ธˆ์ด๋ผ๋„ ๋œ์–ด์ฃผ๊ณ  ์‹ถ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ํ•˜๋‚˜ํ•˜๋‚˜ ์ƒ์„ธํžˆ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž. [1] www.boozallen.com [2] International Monetary Fund, www.imf.org [3] www.mckinsey.com [4] Master of Business Administration ๊ฒฝ์˜ํ•™ ์„์‚ฌ. ๋Œ€๋ถ€๋ถ„์˜ ์ปจ์„คํŒ… ๊ธฐ์—…์€ MBA ํ•™์œ„๊ฐ€ ํ•„์ˆ˜์ด๋‹ค. [5] ์ปจ์„คํŒ…์€ ๊ธฐ์—… ๋น„๋ฐ€ ์‚ฌํ•ญ์„ ๋งŽ์ด ๋‹ค๋ฃฌ๋‹ค. ์ด ๋ถ€๋ถ„์€ ๊ณต๊ฐœํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์€ ์ƒ๋žตํ•  ๊ฒƒ์ด๋‹ค. ๊ฐ™์ด ์ฝ์œผ๋ฉด ์ข‹์€ ์ฑ… 'Management Consulting: A Guide to the Profession (3rd)', International Labour Office Publication, 1996 '๋งฅํ‚จ์ง€๋Š” ์ผํ•˜๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฅด๋‹ค(The Mckinsey Way)', ์—๋‹จ๋ผ์ง€์—˜, 1999 01. ์ปจ์„คํŒ… ์ •์˜์™€ ์ข…๋ฅ˜ ์ปจ์„คํŒ…(Consulting)์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ? ์‚ฌ์ „์  ์˜๋ฏธ๋กœ๋Š” โ€˜์กฐ์–ธ(ๅŠฉ่จ€)์„ ์ฃผ๋Š” ๊ฒƒโ€™์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ ์ปจ์„คํŒ… ๋น„์ฆˆ๋‹ˆ์Šค์—์„œ๋Š” ๋‹จ์ˆœํžˆ ์กฐ์–ธ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ๋„˜์–ด์„œ โ€˜์„ค๋ฃจ์…˜(Solution)์„ ์ œ์‹œโ€™ํ•œ๋‹ค. ๋ฌธ์ œ(Problems)๋ฅผ ํŒŒ์•…ํ•˜๊ณ  ๊ณ ๊ฐ์ด ํ•ด๊ฒฐํ•˜๊ธฐ ์–ด๋ ค์šด ๊ฒƒ ๋˜๋Š” ์ง์ ‘ ํ•ด๊ฒฐํ•˜๊ธฐ ์‹ซ์€ ๊ฒƒ๋“ค์„ ๋Œ€์‹ ํ•ด ์ฃผ๋ฉด์„œ ๊ทธ ๋Œ€๊ฐ€๋กœ ๋ˆ์„ ์ง€๋ถˆ ๋ฐ›๋Š” ๊ฒƒ์ด๋‹ค. ์„ค๋ฃจ์…˜์€ ์ง€์‹๊ณผ ๊ฒฝํ—˜์—์„œ ๋„์ถœ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณ ๊ฐ์œผ๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ์ด๋Œ์–ด๋‚ด๊ณ  ๊ณ ์œ ์˜ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ํšจ๊ณผ์ ์œผ๋กœ ์ „๋‹ฌํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ์ง€๊ธ‰๋ฐ›๋Š” ๋Œ€๊ฐ€๋„ ์ฒœ์ฐจ๋งŒ๋ณ„(ๅƒๅทฎ่ฌๅˆฅ)์ด๋‹ค. ๋›ฐ์–ด๋‚œ ์ŠคํŽ™(spec)์œผ๋กœ ๋ฌด์žฅํ•œ ์œ ํ•™ํŒŒ ์ปจ์„คํ„ดํŠธ๋‚˜ ํ•™์œ„๋Š” ์—†์ง€๋งŒ ์ˆ˜ ์‹ญ ๋…„ ๊ฐ„ ์‚ฌ์—… ํ˜„์žฅ์—์„œ ์ž”๋ผˆ๊ฐ€ ๊ตต์€ ์ง์›์ด ์ œ์‹œํ•œ ๋‹ต์ด ๋ณ„ ์ฐจ์ด๊ฐ€ ์—†์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์–ด๋–ป๊ฒŒ ๋ณด๊ณ ๋˜๋Š๋ƒ ํ•˜๋Š” ๊ฒƒ์— ๋”ฐ๋ผ ๊ทธ ๊ฐ€์น˜๋Š” ์„œ๋กœ ๋งŽ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ปจ์„คํ„ดํŠธ๋“ค์€ ๊ทธ๋“ค์˜ ์ง€์‹๊ณผ ๊ฒฝํ—˜์„ ์ œ๋Œ€๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ๋…ผ๋ฆฌ์™€ ๋ถ„์„, ๋ณด๊ณ ์„œ ์ž‘์„ฑ(Documentation)๊ณผ ํ”„๋ ˆ์  ํ…Œ์ด์…˜(Presentation)์— ๋งŽ์€ ๊ณต์„ ๋“ค์ธ๋‹ค. Part I์˜ ์ œ1์žฅ์—์„œ๋Š” ์ปจ์„คํŒ…์˜ ์ •์˜์™€ ์ปจ์„คํ„ดํŠธ์— ๋Œ€ํ•ด ์ƒ์„ธํžˆ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž. 1.1 ์ปจ์„คํŒ…์˜ ๊ฐœ๋… ์ตœ๊ทผ ๋”์šฑ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” โ€˜์ปจ์„คํŒ…โ€™์ด๋ผ๋Š” ์šฉ์–ด๋Š” ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์˜ ๋…ธ๋ ฅ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์‚ฌ์‹ค ๊ทธ ์ •์˜๋ฅผ ๋‚ด๋ฆฌ๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค. ์˜ค๋ž˜์ „์˜ ์ผ์ด์ง€๋งŒ ๊ฒฝ์˜ ์ปจ์„คํŒ…์„ ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋ฏธ๊ตญ ์• ๋ฆฌ์กฐ๋‚˜ ๊ณต์ธํšŒ๊ณ„์‚ฌ ํ˜‘ํšŒ์˜ ํ›„์›์œผ๋กœ ๊ฒฐ์„ฑ๋œ ๊ฒฝ์˜ ์ปจ์„คํŒ… ์œ„์›ํšŒ๋Š” โ€˜๊ฒฝ์˜ ์ปจ์„คํŒ…์€ ์ •์˜ํ•  ์ˆ˜ ์—†๋‹คโ€™๋ผ๋Š” ๊ฒฐ๋ก ๊นŒ์ง€ ๋‚ด๋ ธ๋‹ค๊ณ  ํ•œ๋‹ค. ์ปจ์„คํŒ…์€ ํšŒ๊ณ„๋‚˜ ๋ฒ•๋ฅ ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ๊ฒฝ์˜์— ์ด๋ฅด๋Ÿฌ ์‚ฌ์—…ํ™”๋˜์—ˆ๋‹ค๊ณ  ๋ณด๋Š” ์‹œ๊ฐ์ด ๋Œ€๋ถ€๋ถ„์ด๋ฏ€๋กœ ๊ทธ๋Ÿฐ ์„œ๋น„์Šค๊ฐ€ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐœ๋‹ฌํ•œ ๋ฏธ๊ตญ๊ณผ ์˜๊ตญ์„ ์˜ˆ๋กœ ๋“ค์–ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ •์˜๋“ค์ด ์žˆ๋‹ค. **"๊ฒฝ์˜ ์ปจ์„คํŒ…์ด๋ž€ ๊ธฐ์—…์œผ๋กœ ํ•˜์—ฌ๊ธˆ ๋‹น๋ฉดํ•œ ๋ฌธ์ œ๋“ค์„ ๋ถ„์„, ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋„๋ก ๋˜๋Š” ๊ธฐ์—…์˜ ์„ฑ๊ณต ์‚ฌ๋ก€๋ฅผ ํƒ€ ๊ธฐ์—…์— ์ ‘๋ชฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์ „๋ฌธ์ ์ธ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด๋‹ค." Management Consulting: A Guide to the Profession **"๊ฒฝ์˜ ์ปจ์„คํŒ…์€ ํŠน๋ณ„ํžˆ ํ›ˆ๋ จ๋ฐ›๊ณ  ๊ฒฝํ—˜์„ ์Œ“์€ ์‚ฌ๋žŒ๋“ค์ด ๊ธฐ์—… ๊ฒฝ์˜ ์ƒ์˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ์ ๋“ค์„ ๊ทœ๋ช…ํ•˜๊ณ  ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์‹ค์งˆ์ ์ธ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์„ ์ œ์‹œํ•˜๊ณ  ๊ทธ๋Ÿฌํ•œ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ๋“ค์ด ์ ๊ธฐ์— ์‹ค์‹œ๋  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๊ธฐ ์œ„ํ•œ ์ „๋ฌธ์ ์ธ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด๋‹ค." Association of Consulting Management Engineers **"๊ฒฝ์˜ ์ปจ์„คํŒ…์ด๋ž€ ํŠน๋ณ„ํ•œ ๋ถ„์•ผ์˜ ์ „๋ฌธ์„ฑ์„ ๊ฐ€์ง„ ์ „๋ฌธ๊ฐ€๋“ค์ด ์ž๊ธฐ๋“ค์˜ ์ง€์‹๊ณผ ๊ฒฝํ—˜์„ ํ™œ์šฉํ•˜์—ฌ ๊ฒฝ์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ , ๊ฐ๊ด€์ ์ด๊ณ  ์ „๋ฐ˜์ ์ธ ์‹œ๊ฐ์—์„œ ๊ธฐ์—…์˜ ๊ธฐํš ๊ณผ์ •์„ ์ง€์›ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค." Management Advisory Services Division **"๊ฒฝ์˜ ์ปจ์„คํŒ…์€ ๋…๋ฆฝ์ ์ด๊ณ  ๋Šฅ๋ ฅ์„ ๊ฐ–์ถ˜ ์‚ฌ๋žŒ(๋“ค) ์ด ์ •์ฑ…, ์กฐ์ง, ์ ˆ์ฐจ, ๋ฐฉ๋ฒ• ์ƒ์˜ ๋ฌธ์ œ์ ๋“ค์„ ์—ฐ๊ตฌ, ๋ถ„์„ํ•˜๊ณ  ์ ์ ˆํ•œ ํ•ด๊ฒฐ์ฑ…์„ ์ œ์‹œํ•˜๋ฉฐ ๋‚˜์•„๊ฐ€ ์ด๋Ÿฌํ•œ ํ•ด๊ฒฐ์ฑ…๋“ค์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ๊ฒƒ์ด๋‹ค." Institute of Management Consults, United Kingdom **"๊ฒฝ์˜ ์ปจ์„คํŒ…์ด๋ž€ ์กฐ์ง์˜ ๋ชฉ์ ์„ ๋‹ฌ์„ฑํ•˜๋Š” ๋ฐ ์žˆ์–ด์„œ ๊ฒฝ์˜, ์—…๋ฌด ์ƒ์˜ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ณ  ์ƒˆ๋กœ์šด ๊ธฐํšŒ๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ณ  ํฌ์ฐฉํ•˜์—ฌ, ํ•™์Šต์„ ์ด‰์ง„์‹œํ‚ค๊ณ  ๋ณ€ํ™”๋ฅผ ์‹คํ˜„ํ•˜๋Š” ์กฐ์ง ๋ฐ ๊ด€๋ฆฌ์ž๋ฅผ ์ง€์›ํ•˜๋Š” ๋…๋ฆฝ์ ์ธ ์ „๋ฌธ, ์ž๋ฌธ ์„œ๋น„์Šค" ๊ตญ์ œ๋…ธ๋™๊ธฐ๊ตฌ(International Labour Organization) ์œ„์—์„œ ์–ธ๊ธ‰๋œ ์ปจ์„คํŒ… ์ •์˜๋“ค์„ ์ฐธ๊ณ ํ•˜๋ฉด ์ปจ์„คํ„ดํŠธ๋“ค์˜ ์—…๋ฌด์™€ ์š”๊ตฌ ์—ญ๋Ÿ‰์„ ๋‹ค์Œ์˜ 5๊ฐ€์ง€๋กœ ์œ ์ถ”ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 1) ๋…๋ฆฝ๋œ ์ž…์žฅ 2) ํŠน๋ณ„ํ•œ ํ›ˆ๋ จ๊ณผ ์ž์งˆ 3) ์ž๋ฌธ์˜ ์ œ๊ณต 4) ๋ฌธ์ œ์˜ ๊ทœ๋ช…๊ณผ ๋ถ„์„ 5) ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ๊ณผ ์ˆ˜ํ–‰ ์ด๋ฅผ ์ปจ์„คํŒ…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ปจ์„คํ„ดํŠธ์˜ ๊ด€์ ์—์„œ ์ข€ ๋” ํ’€์–ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1) ์ปจ์„คํ„ดํŠธ๋Š” ์ œ3์ž ์‹œ๊ฐ(่ฆ–่ง’)์ด๋ผ๋Š” ๋…๋ฆฝ๋œ ์ž…์žฅ์„ ๋ณด์žฅ๋ฐ›์•„์•ผ ํ•œ๋‹ค. 2) ์ปจ์„คํ„ดํŠธ๋Š” ํŠน๋ณ„ํ•œ ํ›ˆ๋ จ๊ณผ ์ž์งˆ ์Šต๋“์„ ์œ„ํ•ด ์ „๋ฌธ ๋ถ„์•ผ์˜ ์ง€์‹ ๋˜๋Š” ์—…๋ฌด ๊ฒฝํ—˜์„ ์ถ•์ ํ•ด์•ผ ํ•˜๋ฉฐ, ์ด์˜ ์ž…์ฆ์„ ์œ„ํ•ด ๋ฐ•์‚ฌ๋‚˜ MBA ํ•™์œ„๋ฅผ ์š”๊ตฌํ•˜๊ธฐ๋„ ํ•œ๋‹ค 3) ์ปจ์„คํ„ดํŠธ๋Š” ์ž๋ฌธ์„ ์ œ๊ณตํ•œ๋‹ค๋Š” ์ธก๋ฉด์—์„œ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ(Communication Skills)์ด ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. 4) ์ปจ์„คํ„ดํŠธ๋Š” ๋ฌธ์ œ๋ฅผ ํŒŒ์•…ํ•˜๊ณ  ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด ์ „๋ฌธ์ ์ธ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉฐ 5) ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ๊ณผ ์ˆ˜ํ–‰์ด๋ผ๋Š” ์ธก๋ฉด์—์„œ ์ปจ์„คํ„ดํŠธ์—๊ฒŒ ์ฐฝ์˜์ ์ธ ์•„์ด๋””์–ด์™€ ๊ธฐ๊ฐ„ ๋‚ด์— ์—…๋ฌด๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ์ˆ˜ ์žˆ๋Š” ๋†’์€ ์—…๋ฌด ์ง‘์ค‘๋ ฅ์ด ์š”๊ตฌ๋œ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ๋ฏธ๋ฃจ์–ด๋ณผ ๋•Œ ๋ถ„๋ช…ํžˆ ์ปจ์„คํŒ…์€ ์•„๋ฌด๋‚˜ ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์€ ์•„๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ปจ์„คํŒ…์„ ๋ฐ›๋Š” ๊ณ ๊ฐ๋“ค์€ ์ด๋Ÿฐ ์ปจ์„คํ„ดํŠธ์—๊ฒŒ ๋ฌด์—‡์„ ๊ธฐ๋Œ€ํ•˜๊ณ  ์žˆ์„๊นŒ? ์ปจ์„คํ„ดํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ปจ์„คํŒ… ์„œ๋น„์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด 4๊ฐ€์ง€ ๊ด€์ ์—์„œ ๊ตฌ๋ถ„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. **1.2 ์ปจ์„คํŒ… ์„œ๋น„์Šค์˜ ๊ด€์  ์ปจ์„คํŒ… ์„œ๋น„์Šค์˜ 4๊ฐ€์ง€ ๊ด€์  ์ค‘ ๊ฐ€์žฅ ๋งŽ์€ ๋น„์ค‘์„ ์ฐจ์ง€ํ•˜๋Š” ๊ฒƒ์€ ์ฒซ ๋ฒˆ์งธ๋Š” ์˜์‚ฌ(Doctor)์˜ ์—ญํ• ์ด๋‹ค. ์ฆ‰, ๋งŽ์€ ๊ธฐ์—… ๊ณ ๊ฐ๋“ค์€ ๊ธฐ์—…์ด ์ œ๋Œ€๋กœ ์šด์˜๋˜๊ณ  ์žˆ๋Š”์ง€ ๋“ฑ ๊ฒฝ์˜์— ๋Œ€ํ•œ ์ „๋ฐ˜์ ์ธ ์ƒํ™ฉ์„ ํŒŒ์•…ํ•˜๊ณ  ์ฒ˜๋ฐฉ์„ ๋ฐ›๊ณ  ์‹ถ์–ด ํ•œ๋‹ค. ๋น„์ „(Vision)์ด๋‚˜ ์ค‘์žฅ๊ธฐ ๊ฒฝ์˜ ์ „๋žต(Corporate Strategy) ์ˆ˜๋ฆฝ, ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค(Business Portfolio) ์ •๋ฆฝ ๋ฐ ๊ด€๋ฆฌ, ๊ธฐ์—… ๊ตฌ์กฐ์กฐ์ •(Restructuring) ๋“ฑ ๊ธฐ์—… ๊ฒฝ์˜์˜ ํฐ ๊ทธ๋ฆผ(Big Pictures) ๊ตฌ์ƒ๊ณผ ๊ทธ ๋ชฉํ‘œ ๋‹ฌ์„ฑ์„ ์œ„ํ•œ ๊ณผ์ œ ์ˆ˜๋ฆฝ ๋“ฑ์ด ์ปจ์„คํŒ…์˜ ์ฃผ์š” ๋‚ด์šฉ์ด๋‹ค. ์ฃผ๋กœ ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์ด ์ด๋Ÿฐ ์ฃผ์ œ๋“ค์„ ๋งŽ์ด ๋‹ค๋ฃฌ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ํƒ์ •(Detective)์˜ ์—ญํ• ์ด๋‹ค. ๊ธฐ์—… ์ง„๋‹จ์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์˜์‚ฌ์˜ ์—ญํ• ์ด ๊ธฐ์—…์˜ ์ „๋ฐ˜์ ์ธ ๋ถ€๋ถ„์„ ํƒ์ƒ‰ํ•˜๊ณ  ๋‹ค๋ฃฌ๋‹ค๋ฉด, ํƒ์ •์˜ ์—ญํ• ์€ ํŠน์ • ๋ถ€๋ถ„์„ ํŒŒ๊ณ ๋“ค์–ด ๋ณด๋‹ค ์„ธ๋ฐ€ํ•˜๊ณ  ๊ทผ์›์ ์ธ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฃผ๋กœ ํ”„๋กœ์„ธ์Šค ์ปจ์„คํŒ…์ด ์ด์— ํ•ด๋‹นํ•œ๋‹ค. ์žฌ๋ฌด, ์ธ์‚ฌ, ๋งˆ์ผ€ํŒ…/์˜์—…, ๊ตฌ๋งค, ์กฐ๋‹ฌ, ์ƒ์‚ฐ, ๋ฌผ๋ฅ˜, IT ๋“ฑ ๊ธฐ์—…์˜ ๋‹ค์–‘ํ•œ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐœ์„  ๋˜๋Š” ํ˜์‹ ํ•˜๋Š” ๊ฒƒ์ด ์ด ์ปจ์„คํŒ… ์„œ๋น„์Šค ์˜์—ญ์ด๋‹ค. ์„ธ ๋ฒˆ์งธ๋Š”์„ธ์ผ๋งจ(Salesperson)์˜ ์—ญํ• ์ด๋‹ค. ์˜์‚ฌ๋‚˜ ํƒ์ • ์—ญํ• ์˜ ์ปจ์„คํŒ…์ด ์ˆ˜ํ–‰๋˜๋ฉด ๋‹ค์–‘ํ•œ ๊ณผ์ œ๋“ค์ด ๋„์ถœ๋˜๋Š”๋ฐ ์š”์ฆ˜์€ ๊ทธ ๊ณผ์ œ๋“ค์˜ ์ƒ์„ธํ•œ ๊ฐœ๋ฐœ์ด๋‚˜ ์‹ค์งˆ์ ์ธ ์ถ”์ง„์„ ์ปจ์„คํ„ดํŠธ๊ฐ€ ๋งก๊ธฐ๋„ ํ•œ๋‹ค. ํŠนํžˆ, ์‹ ๊ทœ ์‚ฌ์—… ๊ฐœ๋ฐœ์ด๋‚˜ ์‹ ๊ทœ ๊ณ ๊ฐ ๋ฐœ๊ตด๊ณผ ๊ฐ™์€ ์ƒˆ๋กœ์šด ์˜์—ญ์˜ ๊ฐœ์ฒ™์ด ๋งŽ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค์— ๊ด€์‹ฌ์ด ๋งŽ์€ ์ปจ์„คํ„ดํŠธ๋“ค์€ ์‹ ์‚ฌ์—… ๋ฐœ๊ตด ์ปจ์„คํŒ…์„ ํ•˜๊ณ  ๋‚˜์„œ ์•„์˜ˆ ๊ณ ๊ฐ ๊ธฐ์—…์œผ๋กœ ์˜ฎ๊ฒจ์„œ ํ•ด๋‹น ์‚ฌ์—…์„ ์ง์ ‘ ๋งก๊ณ  ํŒ€์žฅ ๋˜๋Š” ๋‹ด๋‹น ์ž„์›์œผ๋กœ ์ผํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. ๋„ค ๋ฒˆ์งธ๋Š” ๊ณ ๊ฐ์ด ํ•˜๊ธฐ ์‹ซ์€ ์ผ์„ ๋Œ€์‹ ํ•ด์ฃผ๋Š” ๊ฒฝ์šฐ ์ฆ‰ ์šฉ์—ญ ๋Œ€ํ–‰(Agent)์˜ ์—ญํ• ์ด๋‹ค. ๋ฌผ๋ก , ์•ž์—์„œ ์†Œ๊ฐœํ•œ ๊ฒƒ๋“ค ๋ชจ๋‘๊ฐ€ ๊ณ ๊ฐ ๋Œ€์‹  ์–ด๋–ค ๊ฒƒ์„ ํ•ด์ฃผ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ๊ณ ๊ฐ๊ณผ ์ปจ์„คํ„ดํŠธ๊ฐ€ ์„œ๋กœ ํ›Œ๋ฅญํ•œ ํŒŒํŠธ๋„ˆ๊ฐ€ ๋˜์–ด์„œ ํ˜‘์—…ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งค์šฐ ๋งŽ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋„ค ๋ฒˆ์งธ ๊ฒฝ์šฐ๋Š” ๊ทธ๋Ÿฐ ๊ด€๊ณ„๊ฐ€ ํ˜•์„ฑ๋˜๊ธฐ๋ณด๋‹ค๋Š” ํ•˜์œ„ ์ˆ˜์ค€์˜ ๋‹จ์ˆœ ์—…๋ฌด ๋Œ€ํ–‰์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ํ’์กฐ๋Š” ์ตœ๊ทผ ์ปจ์„คํ„ดํŠธ๋“ค์˜ ์œ„์ƒ๊ณผ๋„ ๊ด€๋ จ์ด ๊นŠ์€๋ฐ 2000๋…„๋Œ€ ์ค‘๋ฐ˜๊นŒ์ง€๋งŒ ํ•ด๋„ ์–ด๋–ค ์ผ์„ ํ•  ๋•Œ ์ „๋ฌธ์ ์ธ ์ง€์‹์ด ํ•„์š”ํ•˜๊ฑฐ๋‚˜ ๊ตฌ์กฐ์กฐ์ •๊ณผ ๊ฐ™์€ ์†Œ์œ„ ํ”ผ๋ฅผ ๋ฌปํžˆ๊ฒŒ ๋˜๋Š” ์ผ์„ ํ•  ๊ฒฝ์šฐ, ๊ธฐ์—…์€ ์ผ์˜ ๊ฐ๊ด€์„ฑ์ด๋‚˜ ์‚ฌ๋‚ด ์กฐ์ง๋ฌธํ™”๋ฅผ ์ƒ๊ฐํ•ด์„œ ๊ตฌ์กฐ์กฐ์ •ํŒ€์˜ ๋Œ€๋ถ€๋ถ„์„ ์™ธ๋ถ€ ์ „๋ฌธ๊ฐ€๋“ค๋กœ ๊ตฌ์„ฑํ•˜๊ณ  ์ปจ์„คํ„ดํŠธ๋“ค์„ ๊ณ ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜๋‹ค. ๊ทธ๋Ÿฐ ์ผ์—๋Š” ์ปจ์„คํ„ดํŠธ๋“ค๋„ ๋งค์šฐ ์‹ ์ค‘ํ•˜๊ฒŒ ์›€์ง์˜€์œผ๋ฉฐ ์ผ์— ๋Œ€ํ•œ ์˜๋ฏธ ๋ถ€์—ฌ๋„ ๋‚จ๋‹ฌ๋ž๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ 2007๋…„[1]์„ ์ „ํ›„๋กœ ์ปจ์„คํŒ…์˜ ๊ฐ€์น˜๊ฐ€ ์ข€ ํ‡ด์ƒ‰๋˜์–ด ๊ฐ€๋Š” ๋“ฏํ•œ ๋Š๋‚Œ๋„ ๋งŽ์ด ๋“ ๋‹ค. ์ปจ์„คํŒ… ์‚ฐ์—…์˜ ์นจ์ฒด์— ๋”ฐ๋ฅธ ๊ฒƒ์ธ์ง€ ์ปจ์„คํ„ดํŠธ๋“ค์˜ ์ž๋ถ€์‹ฌ์ด๋‚˜ ์‚ฌ๊ธฐ๋„ ๋งŽ์ด ์ €ํ•˜๋œ ๊ฒƒ ๊ฐ™๋‹ค. ์ด๋Ÿฐ ์นจ์ฒด๋œ ๋ถ„์œ„๊ธฐ๋Š” ์ปจ์„คํ„ดํŠธ๋“ค์ด ๋…๋ฆฝ์ ์ธ ์ปจ์„คํŒ… ๊ณผ์ œ ์ˆ˜ํ–‰๋ณด๋‹ค ํ˜„์—…์„ ๋Œ€์‹ ํ•ด ์ž„์‹œ ์šฉ์—ญ ๋Œ€ํ–‰์ฒ˜๋Ÿผ ์ผํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด ์•„๋‹๊นŒ ์ƒ๊ฐ๋œ๋‹ค. ์ปจ์„คํ„ดํŠธ๋“ค์ด ์ผ์„ ๋ชปํ•ด์„œ ๋‹จ์ˆœ ์šฉ์—ญ์„ ๋งก๊ฒŒ ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ  ๊ณผ๋‹น๊ฒฝ์Ÿ์œผ๋กœ ๊ทธ๋“ค์˜ Value์™€ ๋Œ€๊ฐ€๋ฅผ ์Šค์Šค๋กœ ๋‚ฎ์ถฐ์„œ ๊ทธ๋Ÿฐ ๊ฒƒ๋„ ์•„๋‹ ๊ฒƒ์ด์ง€๋งŒ ์‚ฌ์—… ํ™˜๊ฒฝ์€ ๊ทธ๋ ‡๊ฒŒ ๋ณ€ํ™”๋˜์–ด ๊ฐ€๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋ณ€ํ™”ํ•˜๋Š” ์‚ฌ์—… ํ™˜๊ฒฝ์€ ์ปจ์„คํŒ… ์„œ๋น„์Šค์˜ ๋˜ ๋‹ค๋ฅธ ๊ตฌ๋ถ„์ž์— ์˜ํ–ฅ์„ ์ฃผ๊ณ  ์žˆ๋‹ค. ์ „ํ†ต์ ์œผ๋กœ ๊ธฐ์—… ์ „๋žต์˜ ๊ตฌ์กฐ์— ๋”ฐ๋ผ์„œ ์ปจ์„คํŒ… ์„œ๋น„์Šค๋Š” ๋‚˜๋ฆ„์˜ ์˜์—ญ์„ ๊ตฌ์ถ•ํ•˜๊ณ  ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค ๊ฐ„์— ์ด๊ฒƒ์„ ์„œ๋กœ ๊ตฌ๋ถ„ํ•ด์™”๋Š”๋ฐ ์ด์ œ ์ด๊ฒƒ๋„ ๋ณ€ํ•˜๊ณ  ์žˆ๋‹ค. ๊ธฐ์—…์˜ ์ „๋žต์€ ํฌ๊ฒŒ ๊ฒฝ์˜์ „๋žต, ์‚ฌ์—…์ „๋žต, ๊ธฐ๋Šฅ ๋ฐ ์šด์˜ ์ „๋žต ๋“ฑ 3๊ฐ€์ง€ ์ธต์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์„ค๋ช…ํ•ด์™”๊ณ  ์ปจ์„คํŒ… ์„œ๋น„์Šค๋„ ์ด์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ 1990๋…„ ๋Œ€ ์ดํ›„ e-๋น„์ฆˆ๋‹ˆ์Šค๊ฐ€ ๊ธ‰์†ํ•˜๊ฒŒ ๋ฐœ๋‹ฌํ•˜๋ฉด์„œ ๊ธฐ์กด์˜ ๊ฐ€์น˜ ์‚ฌ์Šฌ(Value Chain)์ด ๋ถ•๊ดด๋˜๊ณ  ์ƒˆ๋กญ๊ฒŒ ์ •๋ฆฝ๋˜๋ฉด์„œ ๋ณ€ํ™”ํ•˜๊ณ  ์žˆ๋‹ค. Figure I-4์™€ ๊ฐ™์ด ์ „ํ†ต์ ์ธ ๋ชจ์Šต์€ ํ”ผ๋ผ๋ฏธ๋“œ ํ˜•ํƒœ์˜€๋Š”๋ฐ ์ง€๊ธˆ์€ ์ „๋žต ์ปจ์„คํŒ…์ด ๊ฒฝ์˜์ „๋žต ๋ถ€๋ถ„์„ ๋‹ค๋ฃจ๊ณ  ์ผ๋ถ€ ์‚ฌ์—… ์ „๋žต๊นŒ์ง€ ๋‚ด๋ ค๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  IT ์ปจ์„คํŒ…์œผ๋กœ ๋Œ€ํ‘œ๋˜๋Š” โ€˜ํ”„๋กœ์„ธ์Šค ์ปจ์„คํŒ…โ€™์€ ๊ธฐ๋Šฅ ๋ฐ ์šด์˜ ์ „๋žต๊ณผ ๊ด€๋ จ๋œ ๊ฒƒ์ธ๋ฐ ์ตœ๊ทผ์—๋Š” ์‚ฌ์—… ์ „๋žต์˜ ์ผ๋ถ€ ๋ฐ ๊ฒฝ์˜ ์ „๋žต๊นŒ์ง€๋„ ๋‹ค๋ฃจ๊ณ  ์žˆ๋‹ค. ํ•œ๋•Œ ๊ธฐ์—…์˜ ๋น„ํ•ต์‹ฌ์—…๋ฌด(Non-Core business) ์ค‘ ํ•˜๋‚˜์˜€๋˜ ์ „์‚ฐ ์—…๋ฌด๊ฐ€ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ํ˜์‹  ๋ฐ ์ปจ๋ฒ„์ „์Šค(convergence)์˜ ๋™์ธ(ๅ‹•ๅ› )์ด ๋˜์–ด ๋ฒ„๋ฆฐ ๊ฒƒ์ด๋‹ค. ๋ฉ”๊ฐ€ํŠธ๋ Œ๋“œ์˜ ๋ณ€ํ™”์™€ ๋”๋ถˆ์–ด ์ปจ์„คํŒ… ์„œ๋น„์Šค๋„ ๊ทธ ๋ฒ”์œ„๊ฐ€ ํ™•๋Œ€๋˜๊ณ  ๋ณ€๊ฒฝ๋˜๋ฉด์„œ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค๋„ ์ƒˆ๋กœ์šด ์˜ท์„ ๋น ๋ฅด๊ฒŒ ์ž…์–ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์ƒ๊ฒผ๊ณ  ์ด๋Š” ์šฐ์ˆ˜ํ•œ ์‹ ๊ทœ ์ธ๋ ฅ ์ฑ„์šฉ์„ ๋„˜์–ด์„œ์„œ ๊ธฐ์—… ๊ฐ„ ์ธ์ˆ˜ํ•ฉ๋ณ‘ ๊ฐ™์€ ๋™์ ์ธ ๋ณ€ํ™”๋„ ์•ผ๊ธฐํ•˜์˜€๋‹ค. ์•„์šธ๋Ÿฌ ์ปจ์„คํ„ดํŠธ๋“ค๋„ ๊ฐ™์ด ๋‹ค๋ณ€ํ™”๋˜์—ˆ๋‹ค. ์ด์ œ ๋” ์ด์ƒ ์ปจ์„คํŒ…์€ ๊ฒฝ์˜ ์ปจ์„คํŒ…๋งŒ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์ปจ์„คํŒ…์˜ ์˜๋ฏธ๋ฅผ ์ „ ์‚ฐ์—…์— ๊ฑธ์ณ์„œ ๊ณต์œ ํ•  ํ•„์—ฐ์ ์ธ ์ด์œ ๊ฐ€ ์ƒ๊ธด ๊ฒƒ์ด๋‹ค. **"์ปจ๋ฒ„์ „์Šค(Convergence)๋ž€ ๊ธฐ์ˆ  ํ˜น์€ ์ œํ’ˆ๋“ค์ด ์œ ์‚ฌํ™”[2]์™€ ๋ณตํ•ฉํ™”[3]๋ฅผ ํ†ตํ•ด ๊ธฐ์กด ์‹œ์žฅ ์˜์—ญ ๊ฐ„์˜ ๊ตฌ๋ถ„ ๋˜๋Š” ๊ฒฝ๊ณ„๊ฐ€ ๋ถˆ๋ถ„๋ช…ํ•ด์ง€๋Š” ํ˜„์ƒ์„ ์˜๋ฏธํ•œ๋‹ค." ์„œ์šธ๋Œ€ํ•™๊ต ๊ฒฝ์˜ ๋Œ€ํ•™<NAME> ๊ต์ˆ˜, LG ๊ฒฝ์˜์ „๋žต ์•„์นด๋ฐ๋ฏธ, 2004 [1] 2007๋…„ ์—”๋ก  ์‚ฌํƒœ๋กœ ์ธํ•ด ํšŒ๊ณ„ ์„œ๋น„์Šค์™€ ์ปจ์„คํŒ… ์„œ๋น„์Šค ๋“ฑ์— ๋Œ€ํ•œ ํฐ ๊ตฌ์กฐ์กฐ์ •์ด ์žˆ์—ˆ๊ณ  ๊ทธ ์ดํ›„ ์ •๋ณดํ†ต์‹ ๊ธฐ์ˆ ์˜ ๋ฐœ๋‹ฌ ๋“ฑ ๋‹ค์–‘ํ•œ ์™ธ๋ถ€ ์š”์ธ์œผ๋กœ ์ „ ์„ธ๊ณ„์ ์œผ๋กœ ์ปจ์„คํŒ… ์‚ฐ์—…์€ ์–‘์ , ์งˆ์  ๋ณ€ํ™”๋ฅผ ๊ฒช์—ˆ๋‹ค. [2] ๊ธฐ์กด์— ์„œ๋กœ ๋‹ค๋ฅธ ๊ณ ๊ฐ ๊ฐ€์น˜๋ฅผ ์ œ๊ณตํ•˜๋˜ ๊ธฐ์ˆ  ๋˜๋Š” ์ œํ’ˆ์œผ๋กœ๋ถ€ํ„ฐ ์œ ์‚ฌํ•œ ๊ณ ๊ฐ ๊ฐ€์น˜๊ฐ€ ์ œ๊ณต๋จ [3] ์„œ๋กœ ๋‹ค๋ฅธ ์ œํ’ˆ ๋ฐ ๊ธฐ์ˆ ๋“ค์ด ๊ฒฐํ•ฉ๋˜์–ด ์ƒˆ๋กœ์šด ๊ณ ๊ฐ ๊ฐ€์น˜๋ฅผ ์ œ๊ณตํ•จ 1.3 ์ปจ์„คํ„ดํŠธ๋ž€ ์–ด๋–ค ์‚ฌ๋žŒ์ธ๊ฐ€? ์ปจ์„คํ„ดํŠธ๋“ค์ด๋ผ๊ณ  ํ•˜๋ฉด ํ”ํžˆ ์ •์žฅ(suit and tie)์„ ์ฐจ๋ ค ์ž…๊ณ  ์ ๋ น๊ตฐ์ฒ˜๋Ÿผ ๊ธฐ์—…์— ๋“ค์–ด์™€ ๊ป„๋„๋Ÿฌ์šด ์ผ์„ ํ•˜๊ณ  ์†Œ๋ฆฌ ์†Œ๋ฌธ ์—†์ด ๋‚˜๊ฐ€๋Š” ์‚ฌ๋žŒ๋“ค์˜ ์ด๋ฏธ์ง€๋ฅผ ๋งŽ์ด ๋– ์˜ฌ๋ฆฌ๋Š”๋ฐ ์ด๋Š” ๊ณผ๊ฑฐ์— ๊ตฌ์กฐ์กฐ์ •์˜ ์ตœ์ „์„ ์—์„œ ์ปจ์„คํ„ดํŠธ๋“ค์ด ๋งŽ์ด ์ผํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฐ ์ด๋ฏธ์ง€๊ฐ€ ํ˜•์„ฑ๋œ ๊ฒƒ์ด๋ผ ์ƒ๊ฐ๋œ๋‹ค. ์•ฝ๊ฐ„ ๋ถ€์ •์ ์ธ ์ด๋ฏธ์ง€์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๊ณ ์—ฐ๋ด‰ ์ „๋ฌธ์ง์ด๋ผ๋Š” ์ธ์‹์œผ๋กœ ํ•œ๋•Œ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋ง‰์—ฐํžˆ ์ปจ์„คํ„ดํŠธ๋ผ๋Š” ์ง์—…์„ ๋™๊ฒฝํ•˜๊ธฐ๋„ ํ•˜์˜€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ปจ์„คํ„ดํŠธ๋ฅผ ์ง์—…์œผ๋กœ ๊ณ ๋ คํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์€ ์ด์ œ๋ถ€ํ„ฐ ์ €์ž๊ฐ€ ํ•˜๋Š” ์ด์•ผ๊ธฐ๋ฅผ ์ž˜ ์ƒˆ๊ฒจ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์•ž์„œ ์‚ดํŽด๋ณธ ์ปจ์„คํŒ…์˜ ์ •์˜ ๋˜๋Š” ๊ฐœ๋…์„ ๋‹ค์‹œ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์ปจ์„คํ„ดํŠธ๋ž€ ๊ฒฐ๊ตญ ์ปจ์„คํŒ…์„ ํ•˜๋Š” ์‚ฌ๋žŒ์ด๋‹ค. ์ด๋ฅผ ์กฐ๊ธˆ ๋‹ค๋“ฌ์–ด๋ณด๋ฉด โ€˜์ปจ์„คํ„ดํŠธ๋ž€ ํ•™๋ฌธ์  ์ง€์‹๊ณผ ํ˜„์žฅ ๊ฒฝํ—˜์„ ํ†ตํ•ด ์˜๋ขฐ์ž์˜ ๋ฌธ์ œ๋ฅผ ๋ถ„์„ํ•ด์„œ ํ˜„์žฌ(As-Is) ๋ณด๋‹ค ๋‚˜์€ ๋ฏธ๋ž˜(To-Be)๋ฅผ ๋ชจ๋ธ/๋ชจํ˜•์œผ๋กœ ์ œ์‹œํ•จ์œผ๋กœ์จ ๊ธฐ์—…์˜ ๊ฒฝ์Ÿ๋ ฅ ๊ฐ•ํ™”์— ๋„์›€์„ ์ฃผ๋Š” ์‚ฌ๋žŒโ€™์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฐ ์ปจ์„คํ„ดํŠธ๋“ค๋„ ๊ฐ™์ด ์ผ์„ ํ•˜๋‹ค ๋ณด๋ฉด ๊ฐœ์ธ์˜ ์„ฑํ–ฅ๊ณผ ๋”๋ถˆ์–ด ๊ฐ ๊ฐœ์ธ์ด ํ’๊ธฐ๋Š” ์ปจ์„คํ„ดํŠธ์˜ ํŠน์„ฑ(personalities of Consultants)์ด ์—ฌ์‹คํžˆ ๋“œ๋Ÿฌ๋‚˜๊ฒŒ ๋˜๋Š”๋ฐ, ๊ฐ€์žฅ ๋งŽ์ด ํ‘œํ˜„๋˜๋Š” ๋ชจ์Šต๋“ค์€ ์ •๋ฆฌํ•ด ๋ณด๋ฉด ๊ฐ๊ด€์  ์กฐ์–ธ์ž, ์„ ์˜์˜ ์ค‘์žฌ์ž, ๋ฌธ์ œ ํ•ด๊ฒฐ์‚ฌ, ์ฝ”์น˜/๊ต์œก๊ฐ€๋กœ ๊ตฌ๋ถ„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ตฌ๋ถ„์€ ์ข‹๋‹ค/๋‚˜์˜๋‹ค, ๋” ์šฐ์ˆ˜ํ•˜๋‹ค ๋“ฑ์˜ ์˜๋ฏธ๊ฐ€ ์•„๋‹ˆ๋ผ ์‚ฌ๋žŒ์ด ๊ฐ€์ง„ ๊ณ ์œ ์˜ ์„ฑํ’ˆ์— ๊ธฐ๋ฐ˜ํ•ด์„œ ๋‹ค์–‘ํ•œ ์ปจ์„คํŒ… ์—…๋ฌด ์ค‘์— ์ด๋Ÿฐ ๋ถ€๋ถ„์ด ํˆฌ์˜๋˜์–ด ํ•ด๋‹น ์ปจ์„คํ„ดํŠธ์—๊ฒŒ ๋‚˜ํƒ€๋‚œ๋‹ค๊ณ  ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„์šธ๋Ÿฌ ๊ทธ๋Ÿฐ ์„ฑํ–ฅ๊ณผ ์ž˜ ๋งค์นญ๋˜๋Š” ํ”„๋กœ์ ํŠธ์— ํˆฌ์ž…๋˜๋ฉด ๊ทธ ์„ฑ๊ณผ๋Š” 100%๋ฅผ ์ดˆ๊ณผ ๋‹ฌ์„ฑํ•œ๋‹ค. ์ด๋Ÿฐ ์ปจ์„คํ„ดํŠธ์˜ ์„ฑํ–ฅ์„ ๋™๋ฌผ๋กœ ์žฌ๋ฏธ๋‚˜๊ฒŒ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด ์žˆ์–ด ์†Œ๊ฐœํ•œ๋‹ค[1]. ์ฒซ ๋ฒˆ์งธ ์œ ํ˜•์€ ๋น„๋ฒ„(Beaver)์ด๋‹ค. ๋น„๋ฒ„์˜ ํŠน์„ฑ์„ ๋ณด์ด๋Š” ์ปจ์„คํ„ดํŠธ๋“ค์€ ๊ทธ์•ผ๋ง๋กœ ์—„์ฒญ๋‚œ ๋…ธ๋ ฅ(Efforts)์„ ๋ณด์ด๋Š” ์‚ฌ๋žŒ๋“ค๋กœ ๋Œ€์ฒด๋กœ ๊ณผ๊ฑฐ์ง€ํ–ฅ์ ์ด๊ณ  ํ”„๋กœ์„ธ์Šค ๊ฐœ์„ ์ด๋‚˜ ํ”„๋กœ์„ธ์Šค ํ˜์‹ (PI[2]) ๊ฐ™์€ ์ผ์„ ์ž˜ ์ˆ˜ํ–‰ํ•ด๋‚ธ๋‹ค. ๋‘ ๋ฒˆ์งธ ์œ ํ˜•์€ ์—ฌ์šฐ(Fox)์ด๋‹ค. ์—ฌ์šฐ์˜ ํŠน์„ฑ์„ ๋ณด์ด๋Š” ์ปจ์„คํ„ดํŠธ๋“ค์€ ๋ฌธ์ œ ํ•ด๊ฒฐ์— ๊ฐ•์ ์„ ์ง€๋‹Œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ํ˜„์žฌ์˜ ๋ฌธ์ œ์— ๊ด€์‹ฌ์ด ๋งŽ์œผ๋ฉฐ Trouble-shooting ๊ฐ™์€ ์ผ์„ ์ž˜ ์ˆ˜ํ–‰ํ•ด๋‚ธ๋‹ค. ์„ธ ๋ฒˆ์งธ ์œ ํ˜•์€ ์˜ฌ๋นผ๋ฏธ(Owl)์ด๋‹ค. ์˜ฌ๋นผ๋ฏธ์˜ ํŠน์„ฑ์„ ๋ณด์ด๋Š” ์ปจ์„คํ„ดํŠธ๋“ค์€ ํ†ต์ฐฐ๋ ฅ(Insight)์ด ๋›ฐ์–ด๋‚œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋ฉฐ ๋ฏธ๋ž˜์— ๊ด€์‹ฌ์ด ๋งŽ๊ณ  ์ „๋žต ์ˆ˜๋ฆฝ์ด๋‚˜ ๊ธฐ์—…์˜ ํฐ ๊ทธ๋ฆผ(Big Picture) ๊ตฌ์ƒ์— ๋Šฅํ•˜๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋„ค ๋ฒˆ์งธ๋Š” ๋Œ๊ณ ๋ž˜(Dolphin) ์œ ํ˜•์˜ ์ปจ์„คํ„ดํŠธ๋“ค์€ ๊ด€๊ณ„์ง€ํ–ฅ์ ์ด๋‹ค. ์ด๋“ค ์—ญ์‹œ ๋ฏธ๋ž˜์— ๊ด€์‹ฌ์ด ๋งŽ์œผ๋ฉฐ ์กฐ์ง๋ฌธํ™”, ์ฝ”์นญ, ๊ต์œก ์ชฝ์— ๊ด€์‹ฌ์ด ๋งŽ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋Ÿฐ ๋น„์œ ์ ์ธ ํ‘œํ˜„์— ๋ถ€ํ•ฉํ•˜๋Š” ํ›Œ๋ฅญํ•œ ์ปจ์„คํ„ดํŠธ๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ค ๋Šฅ๋ ฅ๊ณผ ์ž์งˆ(Skills), ๋˜๋Š” ์ง€์‹(Knowledge)๊ณผ ํƒœ๋„(Attitude)๊ฐ€ ํ•„์š”ํ• ๊นŒ? ๋‹ค์–‘ํ•œ ์˜๊ฒฌ์„ ์ข…ํ•ฉํ•ด ๋ณด๋ฉด ์ปจ์„คํ„ดํŠธ์˜ ๋Šฅ๋ ฅ๊ณผ ์ž์งˆ์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ 3๊ฐ€์ง€ ๋Šฅ๋ ฅ์ด ์ค‘์š”ํ•˜๋‹ค. **1) ๋ฌธ์ œํ•ด๊ฒฐ ๋Šฅ๋ ฅ(Problem Solving skills) 2) ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ๋Šฅ๋ ฅ(Project Management skills) 3) ์˜์‚ฌ์†Œํ†ต ๋Šฅ๋ ฅ(Communication skills) ์ฒซ ๋ฒˆ์งธ, ๋ฌธ์ œํ•ด๊ฒฐ๋Šฅ๋ ฅ์€ ๊ฐ๊ด€์„ฑ์˜ ์ •๋„, ๋์—†๋Š” ํ˜ธ๊ธฐ์‹ฌ, ๊ท€๋‚ฉ์  ์ถ”๋ฆฌ๋ ฅ, ๋ถ„์„ ๋ฐ ์ข…ํ•ฉ ๋Šฅ๋ ฅ ๋“ฑ์„ ์š”๊ตฌํ•œ๋‹ค. ์‚ฌ์‹ค์— ๊ธฐ๋ฐ˜ํ•œ(Fact-based) ์‚ฌ๊ณ , ๊ฐ€์„ค ์ง€ํ–ฅ์ ์ธ(Hypothesis-driven) ์‚ฌ๊ณ , MECE[3] ์  ์‚ฌ๊ณ , ๋กœ์ง ํŠธ๋ฆฌ(Logic Tree), ๋ฒค์น˜๋งˆํ‚น(Benchmarking) ๋“ฑ์€ ๋ฌธ์ œํ•ด๊ฒฐ ๋Šฅ๋ ฅ์„ ๋ฐœํœ˜ํ•˜๋Š”๋ฐ ์ค‘์š”ํ•œ ๊ฒƒ๋“ค์ด๋‹ค. ๋‘ ๋ฒˆ์งธ, ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ๋Šฅ๋ ฅ์€ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋Šฅ๋ ฅ์ด๋‹ค. ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ์™€ ๊ด€๋ จ๋œ ๊ตญ์ œ ํ‘œ์ค€[4]๋„ ์žˆ์œผ๋ฉฐ, ํŒ€ ๋‚ด ๋˜๋Š” ํŒ€ ๊ฐ„ ํ”Œ๋ ˆ์ด์™€ ๊ด€๊ณ„๋œ ํ˜‘์—…, ๊ณ ๊ฐ ๊ด€๊ณ„ ๋“ฑ์ด ์ค‘์š”ํ•˜๋‹ค. ์„ธ ๋ฒˆ์งธ๋Š” ์˜์‚ฌ์†Œํ†ต ๋Šฅ๋ ฅ์ด๋‹ค. ์ €์ž๋„ ๋งŽ์€ ์ปจ์„คํ„ดํŠธ๋“ค์„ ๋ณด์•„ ์™”๊ณ  ๊ฐ™์ด ์ผํ–ˆ์—ˆ๋Š”๋ฐ, ๋ฌธ์ œํ•ด๊ฒฐ ๋Šฅ๋ ฅ๊ณผ ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ๋Šฅ๋ ฅ์€ ๊ฐœ์ธ์˜ ๋…ธ๋ ฅ๊ณผ ๋ฐ˜๋ณต์  ํ›ˆ๋ จ์— ์˜ํ•ด ๋ฐœ์ „๋  ์ˆ˜ ์žˆ์ง€๋งŒ ์„ธ ๋ฒˆ์งธ ์˜์‚ฌ์†Œํ†ต ๋Šฅ๋ ฅ์€ ๊ฐœ์ธ์˜ ๋…ธ๋ ฅ๊ณผ ๋ฌด๊ด€ํ•˜๊ฒŒ ํƒ€๊ณ ๋‚œ ์„ฑ๊ฒฉ๋„ ๊ด€๊ณ„๊ฐ€ ๊นŠ์–ด ์ด ๋ถ€๋ถ„์ด ๋ถ€์กฑํ•  ๊ฒฝ์šฐ ๋” ๋งŽ์€ ๋…ธ๋ ฅ์ด ํ•„์š”ํ•˜๋‹ค. ์‹ฌ์ง€์–ด ๊ฐ๊ณ (ๅˆป่‹ฆ)์˜ ๋…ธ๋ ฅ์œผ๋กœ๋„ ์ด ๋ถ€๋ถ„ ํ–ฅ์ƒ์ด ์ž˜๋˜์ง€ ์•Š์œผ๋ฉด ์ปจ์„คํ„ดํŠธ๋ฅผ ์ง์—…์œผ๋กœ ์ทจํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ธํ„ฐ๋ทฐ(Interview)๋‚˜ ์ฒญ์ทจ(Listening)๋ฅผ ํ†ตํ•ด ๋ฏผ๊ฐํ•œ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์„ ํšจ๊ณผ์ ์œผ๋กœ ํ•ด์•ผ ํ•˜๋ฉฐ, ํƒ์›”ํ•œ ๋ฌธ์žฅ๋ ฅ์œผ๋กœ ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑ(Documentation) ํ•ด์•ผ ํ•œ๋‹ค. ํŠนํžˆ, ์–ธ์–ด์  ํ‘œํ˜„ ๋Šฅ๋ ฅ์€ ๋Œ€๋‹จํžˆ ์ค‘์š”ํ•˜๋‹ค(Presentation & Meeting). ๊ทธ๋Ÿฐ๋ฐ ์ปจ์„คํ„ดํŠธ์˜ ๊ธฐ๋ณธ ์—ญ๋Ÿ‰์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋Ÿฐ ๋Šฅ๋ ฅ๊ณผ ์ž์งˆ์— ๋”ํ•ด์„œ ๊ณ ๊ฐ์„ ๋ฆฌ๋”ฉ(leading) ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์—…(ๆฅญ)์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ง€์‹๊ณผ ๊ฒฝํ—˜๋„ ๊ฐ–์ถ”๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋ฐ•์‚ฌ๋‚˜ MBA ํ•™์œ„๊ฐ€ ๋„์›€์„ ์ฃผ๊ธฐ๋„ ํ•˜์ง€๋งŒ ์š”์ฆ˜๊ฐ™์ด ๊ธ‰๋ณ€ํ•˜๋Š” ์„ธ์ƒ์—์„œ๋Š” ๊ทธ ํšจ๊ณผ๊ฐ€ 2๋…„์„ ์ฑ„ ๊ฐ€์ง€ ๋ชปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ปจ์„คํŒ…ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ถ„์•ผ์— ๋Œ€ํ•œ ๋Š์ž„์—†๋Š” ํ•™์Šต์ด ์ค‘์š”ํ•˜๋‹ค. ๊ธฐ์—… ๊ฒฝ์˜๊ณผ ๊ด€๋ จ๋œ ์ปจ์„คํŒ…์„ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์‚ฐ์—… ์ง€์‹์ด๋‚˜ ์—…๋ฌด ์ง€์‹, ํ”„๋กœ์„ธ์Šค, ์ •๋ณดํ†ต์‹ ๊ธฐ์ˆ  ์ง€์‹ ๋“ฑ์„ ์ž˜ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋˜ํ•œ, ์ปจ์„คํ„ดํŠธ๋ฅผ ๋ฐ”๋ผ๋ณผ ๋•Œ ๋Šฅ๋ ฅ๊ณผ ์ž์งˆ, ์ง€์‹๋„ ์žˆ์–ด์•ผ ํ•˜์ง€๋งŒ ๋ฌด์—‡๋ณด๋‹ค๋„ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ž์„ธ(Attitude)์ด๋‹ค. ์ปจ์„คํ„ดํŠธ์˜ ์ž์งˆ๊ณผ ๊ด€๋ จํ•ด์„œ ์„ ๋ฐฐ๋“ค๋กœ๋ถ€ํ„ฐ ์ „ํ•ด์ง€๋Š” ์›Œ๋”ฉ๋“ค(wordings)์„ ์‚ดํŽด๋ณด๋ฉด, โ€˜๋ถˆํŽธ๋ถ€๋‹น์„ฑ(Impartiality)โ€™, โ€˜๊ณ ๊ฐ์˜ ์ด์ต์€ ๋‚˜์˜ ์ด์ตโ€™, โ€˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๊ณผ ํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒโ€™, โ€˜๋„“์€ ์‹œ๊ฐโ€™, โ€˜๊ธ์ •์ ์ด๊ณ  ์ ๊ทน์ ์ธ ์‚ฌ๊ณ โ€™, โ€˜์˜ˆ์˜ ๋ฐ”๋ฆ„โ€™, โ€˜๋ณ€ํ™”์˜ ์ˆ˜์šฉโ€™, โ€˜์ง€์†์ ์ธ ํ•™์Šตโ€™, โ€˜Know-whyโ€™, โ€˜Know-whatโ€™, โ€˜Know-whereโ€™, โ€˜Know-howโ€™ ๋“ฑ์€ ๋น ์ง€์ง€ ์•Š๋Š”๋‹ค. ์ปจ์„คํ„ดํŠธ์˜ ์ž์„ธ๋ฅผ ๋…ผํ•˜๋ฉด์„œ ๋“ฃ๊ฒŒ ๋˜๋Š” ๊ณต๊ฐ ๊ฐ€๋Š” ํ‘œํ˜„๋“ค์ด์ง€๋งŒ ๊ทธ์ค‘ โ€˜๋ถˆํŽธ๋ถ€๋‹น์„ฑโ€™, ์ด ์šฉ์–ด๋Š” โ€˜๊ฐ๊ด€์ ์ธ ๊ธฐ๋ก๋ฌผโ€™์„ ๋‹ค๋ฃจ๋Š” ์‚ฌ๋žŒ์ด๋ผ๋ฉด ๋ˆ„๊ตฌ๋‚˜ ๋Š๋‚„ ์ˆ˜ ์žˆ๋Š” ๋”œ๋ ˆ๋งˆ(Dilemma)์ด๊ธฐ๋„ ํ•˜์ง€๋งŒ, ์ปจ์„คํ„ดํŠธ๋ผ๋Š” ์ง์—…์˜ ๊ทผ๋ณธ์„ ๊ณ ๋ฏผํ•˜๊ฒŒ ํ•œ๋‹ค. โ€˜๋ถˆํŽธ๋ถ€๋‹น์„ฑโ€™์ด๋ผ๋Š” ์šฉ์–ด๋Š” ์–ด๋–ค ๊ฒƒ์„ ๊ธฐ๋กํ•  ๋•Œ ๊ฐ๊ด€์ ์ธ ์ž…์žฅ์„ ์ทจํ•˜์ง€๋งŒ, ๊ธฐ๋ก๋ฌผ ์ƒ์‚ฐ์ž์˜ ์ž…์žฅ์ด ๋ฐ˜์˜๋˜์–ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ์ฆ‰, ํ•œํŽธ์œผ๋กœ๋Š” ์ด๋ฏธ ๊ฐ๊ด€์ ์ด ์•„๋‹ˆ๋ผ๋Š” ๋ง๊ณผ๋„ ํ†ตํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ปจ์„คํŒ… ๊ฒฐ๊ณผ๋Š” ๋‹น์—ฐํžˆ ๋น„์šฉ์„ ์ง€๊ธ‰ํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์ด ๋˜์–ด์•ผ ํ•˜๊ฒ ์ง€๋งŒ, ๊ทธ๋ ‡๊ฒŒ ์ผํ•˜๋Š” ๊ฒƒ์ด ๋ฐ˜๋“œ์‹œ ์˜ณ์€ ๊ฒƒ์ธ์ง€๋Š” ์‚ฌ์•ˆ์— ๋”ฐ๋ผ ์–‘์‹ฌ์„ ๊ฑธ๊ณ  ์ƒ๊ฐํ•ด ๋ณด์•„์•ผ ํ•œ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด์„œ, ์ €์ž๊ฐ€ ์ปจ์„คํ„ดํŠธ๊ฐ€ ๋˜์—ˆ์„ ๋•Œ ๋ถ€๋ฌธ์žฅ์ด์ž ํŒŒํŠธ๋„ˆ์ด์…จ๋˜ ๋ถ„์ด ์‹ ์ž… ์ปจ์„คํ„ดํŠธ๋“ค์„ ๋ชจ์•„ ๋†“๊ณ  ๋“ค๋ ค์คฌ๋˜ ์ด์•ผ๊ธฐ๋ฅผ ๊ณต์œ ํ•œ๋‹ค. ์‹œ๊ฐ„์ด ๋งŽ์ด ํ˜๋ €์ง€๋งŒ ์—ฌ์ „ํžˆ ์œ ํšจํ•˜๋‹ค. **-Consultant has: ๋ชฉํ‘œ์™€ ๋น„์ „, ๋„์ „ ์ •์‹ , ์ฐฝ์˜๋ ฅ, ์—ด์ •, ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  -Consultant should have: ๊ณ ๊ฐ์ง€ํ–ฅ ๋งˆ์ธ๋“œ, ๋…๋ฆฝ์„ฑ, ๊ฐ๊ด€์„ฑ, ๋ฆฌ๋”์‹ญ, ์นœํ™”๋ ฅ, ํ˜‘๋™์‹ฌ -Consultant has above all: ๋ถˆ๊ตด์˜ ์ •์‹ ๋ ฅ,<NAME>ํ•œ ์ฒด๋ ฅ Break #1. ํ›Œ๋ฅญํ•œ ์ปจ์„คํ„ดํŠธ๊ฐ€ ๋˜๊ธฐ ์œ„ํ•œ ๋ ˆ์•Œ ํŒ(Real Tips) 1.3์žฅ์˜ ๋‚ด์šฉ์ด ํ›Œ๋ฅญํ•œ ์ปจ์„คํ„ดํŠธ๊ฐ€ ๋˜๊ธฐ ์œ„ํ•œ ์†Œ์œ„ ๋งํ•˜๋Š” '๋ชจ๋ฒ” ๋‹ต์•ˆ'์ด๋ผ๋ฉด ์ด์ œ๋ถ€ํ„ฐ ์ด์•ผ๊ธฐ๋Š” ์ˆ˜๋งŽ์€ ์ปจ์„คํ„ดํŠธ ์„ ๋ฐฐ๋“ค์˜ ์ „์–ธ(ๅ‚ณ่จ€)์ฒ˜๋Ÿผ ๋‚ด๋ ค์˜ค๋Š” ๋ ˆ์•Œ ํŒ์ด๋‹ค. **(1) ๋จผ์ € ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…์—์„œ 5๋…„ ์ด์ƒ ๊ทผ๋ฌดํ•˜๋ผ ์ด์ œ ๊ตญ๋‚ด์—๋„ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…๋“ค์ด ๋งŽ์ด ์ง„์ถœํ•˜์˜€๋‹ค. ๋˜ํ•œ, ์‚ผ์„ฑ์ „์ž๋‚˜ LG์ „์ž ๊ฐ™์€ ๋Œ€๊ธฐ์—…๋“ค์€ ํ˜„์žฌ ๊ธฐ์—…์˜ ๋งŽ์€ ๋ถ€๋ถ„๋“ค์ด ๊ธ€๋กœ๋ฒŒ ์ˆ˜์ค€์— ๋‹ฌํ–ˆ๋‹ค๊ณ  ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ•™๊ต ์กธ์—… ํ›„ ๋ฐ”๋กœ ์ปจ์„คํŒ… ๊ธฐ์—…์— ์ทจ์—…ํ•˜๊ธฐ๋ณด๋‹ค๋Š” ์ด๋Ÿฐ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…๋“ค์˜ ํ•œ๊ตญ ์ง€์‚ฌ๋‚˜ ๊ทธ๋Ÿฐ ์ˆ˜์ค€์˜ ์—…๋ฌด ๋ณต์žก์„ฑ์„ ๊ฐ–์ถ˜ ๋Œ€๊ธฐ์—…์—์„œ ์ „๋žต์ด๋‚˜ ๋งˆ์ผ€ํŒ…, ๊ตฌ๋งค ๋“ฑ ํ–ฅํ›„ ์ปจ์„คํŒ…ํ•˜๊ณ  ์‹ถ์€ ์—…๋ฌด ์˜์—ญ์—์„œ ๋ช‡ ๋…„ ๊ฐ„ ์ผํ•ด๋ณด๋ผ. ๊ฒฝ๋ ฅ์„ ์ธ์ •๋ฐ›์œผ๋ ค๋ฉด ํ•œ ๊ธฐ์—…์—์„œ ์ตœ์†Œ 3๋…„ ์ด์ƒ ์ผํ•ด์•ผ ํ•˜๋‹ˆ ๊ทธ ์ด์ƒ ๊ทผ๋ฌดํ•˜๋ฉด์„œ ํ˜„์žฅ์˜ ์ง€์‹์„ ์Šต๋“ํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๋ผ. ์‹ค์ œ ์ปจ์„คํŒ…์„ ํ•  ๋•Œ ์‚ด์•„ ์žˆ๋Š” ์ง€์‹์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ฐ˜์„ ๊ฐ–์ถ”๋Š” ๊ฒƒ์€ ์ปจ์„คํ„ดํŠธ์˜ ์ปจ์„คํŒ… ๊ฒฝ์Ÿ๋ ฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋Š” ๋งค์šฐ ์ค‘์š”ํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์‚ด์•„ ์žˆ๋Š” ํ˜„์žฅ์˜ ์ง€์‹๊ณผ ๊ฒฝํ—˜๋งŒ์ด ๊ณ ๊ฐ์˜ ๊นŠ์€ ๊ณต๊ฐ์„ ์‚ฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์‚ด์•„ ์žˆ๋Š” ์ง€์‹๊ณผ ๊ฒฝํ—˜์ด ์—†๋‹ค๋ฉด MBA๋‚˜ ๋ฐ•์‚ฌ ํ•™์œ„ ๋“ฑ์€ ์ด๋ ฅ์„œ ํ•œ ์ค„์— ์ง€๋‚˜์ง€ ์•Š๋Š”๋‹ค. **(2) ํ˜ธ์ˆ˜ ์œ„์˜ ์šฐ์•„ํ•œ, ๊ทธ๋Ÿฌ๋‚˜ ๋ถ€์ง€๋Ÿฐํ•œ ๋ฐฑ์กฐ๊ฐ€ ๋ผ๋ผ. ์ปจ์„คํ„ดํŠธ๋“ค์€ ๊ฒ‰๋ณด๊ธฐ์— ๋ฌด์–ธ๊ฐ€ ์žˆ์–ด ๋ณด์ธ๋‹ค. ๊ทธ๋Ÿฐ ์ด๋ฏธ์ง€๋ฉ”์ดํ‚น์ด ๋‚˜์œ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ทธ ๋ชจ์Šต ๋’ค์—๋Š” ๋Š์ž„์—†์ด ํ•™์Šตํ•˜๋ฉฐ ๋ฐค์ƒˆ์šฐ๊ณ  ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ๋Š” ์ปจ์„คํ„ดํŠธ์˜ ๋˜ ๋‹ค๋ฅธ ๋ฉด์ด ์žˆ์Œ์„ ํ•ญ์ƒ ์ƒ๊ฐํ•˜๋ผ. ๊ทธ๋ž˜์•ผ ์ด ๋ฐ”๋‹ฅ์—์„œ ์˜ค๋ž˜ ์‚ด์•„๋‚จ์„ ์ˆ˜ ์žˆ๋‹ค. ์ปจ์„คํŒ…์€ ์ง€์  ๋…ธ๋™์ด์ž ๋งค์šฐ ์†Œ๋ชจ์ ์ธ ์ผ์ด๋‹ค. ๋งŽ์€ ์‹œ๊ฐ„์„ ํˆฌ์žํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฒƒ์„ ํƒ๊ตฌํ•˜๋Š” ์—ฐ๊ตฌ ๊ฐœ๋ฐœ์€ ๊ทธ ๊ฒฐ๊ณผ๋ฌผ์ด ์œ ์˜๋ฏธํ•  ๋•Œ ๋‚จ๋Š” ๊ฒƒ์ด ์žˆ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ์ปจ์„คํŒ…์€ ๋‚ด๊ฐ€ ์•Œ๊ณ  ์žˆ๊ณ , ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ๊ณ ๊ฐ์—๊ฒŒ โ€˜ํผ์ฃผ๋Š” ํ–‰์œ„โ€™์ด๋‹ค. ์ปจ์„คํ„ดํŠธ์ธ ๋‚˜์˜ ์ง€์‹๊ณผ ๊ฒฝํ—˜์ด ๊ณ ๊ฐˆ๋˜๋ฉด ๋‚˜์˜ ๊ฐ€์น˜๋Š” ๋‹น์—ฐํžˆ ๋–จ์–ด์ง„๋‹ค. ํ˜ธ์ˆ˜ ์œ„๋ฅผ ๊ฑฐ๋‹ˆ๋Š” ์šฐ์•„ํ•œ ๋ฐฑ์กฐ์˜ ๋ชจ์Šต ๊ทธ๋Ÿฌ๋‚˜ ์ˆ˜๋ฉด ์•„๋ž˜์—์„œ๋Š” ๋ชป์ƒ๊ธด ๋ฌผ๊ฐˆํ€ด ๋ฐœ์„ ์‰ผ ์—†์ด ์›€์ง์ด๊ณ  ์žˆ์Œ์„ ๊ธฐ์–ตํ•˜๋ผ! **(3) ์ดˆ๊ธฐ์˜ ์„ฑ์‹คํ•จ์œผ๋กœ ๊ณ ๊ฐ์˜ ์‹ ๋ขฐ๋ฅผ ์–ป์–ด๋ผ. ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐ์— ๋ชจ๋“  ๊ฒƒ์ด ๊ฒฐ์ •๋œ๋‹ค. ์ €์ž์˜ ๊ฒฝํ—˜์œผ๋กœ๋Š” ํ•˜๋ฃจ, ๋Šฆ์–ด๋„ 3์ผ ๋‚ด์— ๋ชจ๋“  ๊ฒƒ์ด ๊ฒฐ์ •๋˜์—ˆ๋‹ค. ํ”„๋กœ์ ํŠธ์— ์ปจ์„คํ„ดํŠธ๋กœ์„œ ํˆฌ์ž…๋œ ์ฒซ๋‚ ๋ถ€ํ„ฐ ๊ณ ๊ฐ์„ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๋Š๋‚Œ์„ ์ค„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ณ ๊ฐ๊ณผ ๊ฐ™์ด ๋ฐค์„ ์ƒˆ์šฐ๋ฉด์„œ ์ผ์„ ํ•˜๋˜๊ฐ€, ๊ฐ™์ด ์ˆ  ํ•œ์ž”ํ•˜๋ฉด์„œ ๊ณ ๊ฐ์˜ ๊ณ ๋ฏผ์„ ๋“ค์–ด๋ณด๋˜๊ฐ€, ๊ฐ™์ด ์šด๋™์„ ํ•˜๋˜๊ฐ€ ๋ญ๋“ ์ง€ ์ข‹๋‹ค. ๋ถˆ๋ฒ•๋งŒ ์•„๋‹ˆ๋ผ๋ฉด. ๊ณ ๊ฐ์˜ ์‹ ๋ขฐ๋ฅผ ๋น ๋ฅธ ์‹œ๊ฐ„ ๋‚ด์— ํ™•๋ณดํ•˜๋Š” ๊ฒƒ์ด ์ผ์„ ๊ฐ€์žฅ ์‰ฝ๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. **(4) ์ฒด๋ ฅ์„ ๊ธธ๋Ÿฌ๋ผ. ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…์—์„œ ์ผํ–ˆ๊ณ , ๋Š์ž„์—†์ด ํ•™์Šตํ•˜๊ณ , ๊ณ ๊ฐ์˜ ์‹ ๋ขฐ๋ฅผ ๋นจ๋ฆฌ ์–ป๋”๋ผ๋„ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ฒด๋ ฅ์ด๋‹ค. ์ปจ์„คํŒ…์€ ๋ชธ๊ณผ ๋งˆ์Œ์„ 100% ์†Œ๋ชจํ•˜๋Š” ์ง์—… ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ๋‹จ๊ธฐ๊ฐ„์— ์—…๋ฌด ์ŠคํŠธ๋ ˆ์Šค๊ฐ€ ๊ณผ์ค‘ํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ๋๋‚ด๋ฉด ๊ทธ๋™์•ˆ์˜ ์•ผ๊ทผ๊ณผ ๋ฐค์ƒ˜์œผ๋กœ ์ธํ•ด ํฐ๋จธ๋ฆฌ๊ฐ€ ์ƒ๊ธฐ๊ณ  ํ”ผ๋ถ€๊ฐ€ ๋…ธํ™”๋˜์—ˆ์Œ์„ ์—ฌ์‹คํžˆ ๋Š๋‚„ ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฑธ ๊ทน๋ณตํ•˜๋Š” ๊ฒƒ์€ ๋†€๋ผ์šด ํšŒ๋ณต๋ ฅ์ด์š” ๊ทธ ๊ทผ๋ณธ์€ ์ฒด๋ ฅ์ด๋‹ค. ๋ณด์•ฝ๋„ ์ค‘์š”ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ฒด๋ ฅ ๊ด€๋ฆฌ๊ฐ€ ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์œผ๋ฉด ์ปจ์„คํ„ดํŠธ๋กœ ์˜ค๋ž˜ ์ผํ•˜๊ธด ์–ด๋ ต๋‹ค. **(5) ๊ธฐ์—…์˜ ๊ด€๋ฆฌํšŒ๊ณ„๋ฅผ ํŒŒ์•…ํ•˜๋ผ. ๊ธฐ์—…์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์„ ์ด์•ผ๊ธฐํ•˜๊ณ  ์ปจ์„คํŒ…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ˆ์˜ ํ๋ฆ„์„ ์ž˜ ์•Œ์•„์•ผ ํ•œ๋‹ค. ๋ณด๊ณ ์„œ๋งŒ ์˜ˆ์˜๊ฒŒ ๋งŒ๋“œ๋Š” ํ—ˆ๊นจ๋น„ ์žฅํ‘œ์Ÿ์ด๊ฐ€ ๋˜๊ธฐ๋ณด๋‹ค๋Š”, ์—…(ๆฅญ)์˜ ๋ณธ์งˆ์„ ๋ฐ”๋ผ๋ณผ ์ˆ˜ ์žˆ๋Š” ํ†ต์ฐฐ๋ ฅ์„ ํ‚ค์šฐ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ์—… ๊ณ ์œ ์˜ ๊ด€๋ฆฌํšŒ๊ณ„ ์ฒด๊ณ„๋ฅผ ๋ฐ˜๋“œ์‹œ ์ตํžˆ๊ธฐ ๋ฐ”๋ž€๋‹ค. ์˜์™ธ๋กœ ๋งŽ์€ ์ „๋žต ์ปจ์„คํ„ดํŠธ๋“ค์ด ์žฌ๋ฌด์™€ ํšŒ๊ณ„์— ๋ฌด์ง€ํ•˜๋‹ค. [1] ๋™๋ฌผ์— ๋Œ€ํ•œ ์ด๋ฏธ์ง€๊ฐ€ ๋™์–‘๊ณผ ์„œ์–‘์ด ๋‹ฌ๋ผ์„œ ๊ทธ ํŠน์„ฑ์„ ์„ค๋ช…ํ•œ ๊ฒƒ์— ๊ณต๊ฐ์ด ๊ฐ€์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค [2] Process Improvement ๋˜๋Š” Process Innovation [3] Mutually Exclusive Collectively Exhaustive ์ƒํ˜ธ๋ฐฐ์ œ์™€ ์ „์ฒด ํฌ๊ด„ [4] www.pmi.org PMP(Project Management Professional)๋ผ๋Š” ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ๊ณต์ธ ์ธ์ฆ์„ ๋ฐœ๊ธ‰ํ•œ๋‹ค. 02. ์ปจ์„คํŒ… ์‚ฐ์—…์˜ ํ˜„ํ™ฉ 1990๋…„๋Œ€ e-๋น„์ฆˆ๋‹ˆ์Šค๊ฐ€ ํ™œ์„ฑํ™”๋˜๋ฉด์„œ ์‚ฌ์—…์„ B2B๋‚˜ B2C[1]๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐฉ์‹์ด ๋„๋ฆฌ ํผ์กŒ๋‹ค. โ€˜B2Bโ€™๋Š” ์ฒ˜์Œ์—๋Š” ์ „์ž์ƒ๊ฑฐ๋ž˜(e-commerce)์— ํ•œ์ •๋œ ์šฉ์–ด์˜€๋‹ค๊ฐ€ ์ง€๊ธˆ์€ โ€˜๊ธฐ์—… ๊ฐ„ ๊ฑฐ๋ž˜โ€™๋ฅผ ๋œปํ•˜๋Š” ์˜๋ฏธ๋กœ ํ™•์žฅ๋˜์–ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค. B2B ์ œํ’ˆ์€ Table I-2์ฒ˜๋Ÿผ ํฌ๊ฒŒ 8๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ปจ์„คํŒ…์€ ๊ทธ์ค‘ ์„œ๋น„์Šค ์‚ฐ์—…์— ์†ํ•ด ์žˆ๋‹ค. ์ง์—…์„ ์„ ํƒํ•˜๋Š” ์‚ฌ๋žŒ ์ž…์žฅ์—์„œ ์ง์—…์ด ์ฃผ๋Š” ์„ฑ์ทจ๊ฐ๊ณผ ๋”๋ถˆ์–ด ๊ฒฝ์ œ์ ์ธ ์ธก๋ฉด์—์„œ ๋ˆ์„ ์–ผ๋งˆ๋‚˜ ๋ฒŒ ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ์‚ฐ์—…๋ณ„ ์ˆ˜์ต๋ฅ ์ด ๋†’์€ ์‚ฐ์—…์—์„œ ์ผํ•œ๋‹ค๋ฉด, ์ƒ๋Œ€์ ์œผ๋กœ ๋” ๋งŽ์€ ์ˆ˜์ž…์„ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋‹ค[2]. ํˆฌ์ž์€ํ–‰์ด๋‚˜ ์ฆ๊ถŒ์‚ฌ์—์„œ ์‚ฐ์—…๋ณ„ ์ˆ˜์ต๋ฅ  ๋˜๋Š” ์‹œ์žฅ ์ˆ˜์ต์„ฑ์— ๊ด€ํ•œ ๋ฆฌํฌํŠธ๋ฅผ ๋งŽ์ด ๋ฐœํ‘œํ•˜๋Š”๋ฐ ํŠน์ • ์‚ฐ์—…์ด๋‚˜ ์‹œ์žฅ ์œ„์ฃผ์˜ ๋ณด๊ณ ์„œ๊ฐ€ ์ž์ฃผ ์—…๋ฐ์ดํŠธ๋˜๋ฉฐ ์ „ ์‚ฐ์—…์„ ๋‹ค๋ฃจ๋Š” ๊ฒฝ์šฐ๋Š” ๋“œ๋ฌผ๋‹ค. Figure I-6์€ ๋ฏธ๊ตญ์˜ ์‚ฐ์—…๋ณ„ ์ˆ˜์ต๋ฅ ์„ ์ฐธ๊ณ  ์ž๋ฃŒ๋ฅผ ํ™œ์šฉํ•ด์„œ ๊ณ„์‚ฐํ•ด ๋ณธ ๊ฒƒ์ด๋‹ค[3]. ๋ฏธ๊ตญ์ด ๋‹น์‹œ ์„ธ๊ณ„ ๊ธˆ์œต์˜ ์ค‘์‹ฌ์ด๋ผ๋Š” ํŠน์ˆ˜์„ฑ์œผ๋กœ ์ฆ๊ถŒ ์ค‘๊ณ„ ๋ฐ ๊ฑฐ๋ž˜์˜ ์ˆ˜์ต์„ฑ์ด ๊ฐ€์žฅ ๋†’์€ ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ๋‹ค. ERP[4]๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ์˜คํ”ผ์Šค ์†Œํ”„ํŠธ์›จ์–ด๋กœ ์œ ๋ช…ํ•œ SAP๋‚˜ Oracle, Microsoft ๋“ฑ์ด ์†ํ•œ ํŒจํ‚ค์ง€ ์†Œํ”„ํŠธ์›จ์–ด ์‚ฐ์—…๋„ 37%์— ๋‹ฌํ•˜๋Š” ๋†’์€ ์ˆ˜์ต์„ฑ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค. ์ด์— ๋น„ํ•ด ์ปจ์„คํŒ… ์‚ฐ์—…์˜ ์ˆ˜์ต๋ฅ ์€ ์™ธ๋ถ€์— ์ž˜ ๊ณต๊ฐœ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์€๋ฐ ๊ฒฝ์˜ ์ปจ์„คํŒ… ํšŒ์‚ฌ์˜ ๋Œ€๋ถ€๋ถ„์ด ์ฃผ์‹ํšŒ์‚ฌ๊ฐ€ ์•„๋‹ˆ๋ผ ์œ ํ•œํšŒ์‚ฌ์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์‹ค์ ์„ ์™ธ๋ถ€์— ๊ณตํ‘œํ•  ์˜๋ฌด๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์œผ๋กœ ์ƒ๊ฐ๋œ๋‹ค. ์ œ2์žฅ์—์„œ๋Š” ์ปจ์„คํŒ… ์‚ฐ์—…์˜ ๊ทœ๋ชจ์™€ ํ˜„ํ™ฉ, ์ฃผ์š” ์‚ฌ์—…์ž๋“ค๊ณผ ๋ณ€ํ™” ๋ฐฉํ–ฅ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. **2.1 ์ปจ์„คํŒ… ์‚ฐ์—…์˜ ๊ทœ๋ชจ ์‹œ์žฅ์กฐ์‚ฌ๊ธฐ๊ด€์ธ IDC[4]์— ๋”ฐ๋ฅด๋ฉด ์ „ ์„ธ๊ณ„ ์ปจ์„คํŒ… ์‚ฐ์—…์€ 2015๋…„ 97.3์—์„œ ๋งค๋…„ [ ] ์„œ ๋…„ 8.0 B๊นŒ์ง€ ์„ฑ์žฅํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ธกํ•˜์˜€๋‹ค. ์ง€์—ญ๋ณ„ ์—ฐํ‰๊ท  ์„ฑ์žฅ๋ฅ ์€ ๋ฏธ์ฃผ์ง€์—ญ 8.6%, ์œ ๋Ÿฝ ๋ฐ ์ค‘๋™, ์•„ํ”„๋ฆฌ์นด ์ง€์—ญ 7.0%, ์•„์‹œ์•„ ํƒœํ‰์–‘ ์ง€์—ญ 6.9%์˜ ์„ฑ์žฅ์„ธ๋ฅผ ๋ณด์ด๊ณ  ์žˆ๋‹ค ๋˜ํ•œ, ์ปจ์„คํŒ… ์„œ๋น„์Šค ๊ธฐ์ค€์œผ๋กœ ์ „๋žต ์ปจ์„คํŒ…, ์šด์˜ ์ปจ์„คํŒ…, ์กฐ์ง ์ปจ์„คํŒ…, ์žฌ๋ฌดํšŒ๊ณ„, ๊ทœ์ œ ๋ฐ ์ค€์ˆ˜, ์œ„ํ—˜๊ด€๋ฆฌ ์ปจ์„คํŒ…, ๋ณ€ํ™”๊ด€๋ฆฌ ์ปจ์„คํŒ…, ์ง„๋‹จ ๋“ฑ์œผ๋กœ ๊ตฌ๋ถ„๋  ์ˆ˜ ์žˆ๋Š”๋ฐ ์กฐ์ง ์ปจ์„คํŒ…๊ณผ ์ „๋žต ์ปจ์„คํŒ…์ด 2020๋…„๊นŒ์ง€ ๋งค๋…„ ๊ฐ๊ฐ 8.9%, 8.8% ์ฆ๊ฐ€ํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. ์ตœ๊ทผ์—๋Š” ์šด์˜ ์ปจ์„คํŒ… ์ค‘ IT ์ปจ์„คํŒ…์ด ๋”์šฑ ๋ถ€๊ฐ๋˜๊ณ  ์žˆ๋‹ค. Figure I-9์™€ ๊ฐ™์ด IT ์ปจ์„คํŒ…์€ 2015๋…„ 33B์—์„œ ๋…„ ์„œ 2020 38๋กœ ๋งค๋…„ 3.9% P์”ฉ ์ฆ๊ฐ€ํ•  ๊ฒƒ์œผ๋กœ ์ถ”์ •๋˜๊ณ  ์žˆ๋‹ค. IT ์ปจ์„คํŒ…์€ ์ตœ๊ทผ ์ปจ๋ฒ„์ „์Šค(Convergence)๋‚˜ ๋””์ง€ํ„ธ ํ˜๋ช… ๋“ฑ๊ณผ ๊ด€๋ จ๋œ ์–ด์  ๋‹ค๋ฅผ ๋งก๊ฒŒ ๋˜๋ฉด์„œ ์ „๋žต ์ปจ์„คํŒ…๊ณผ ์ค‘์ฒฉ๋˜๊ณ  ์žˆ๋‹ค. ํŠนํžˆ, ์ „๋žต ์ปจ์„คํŒ… ํšŒ์‚ฌ๋“ค๊ณผ IT ์ปจ์„คํŒ… ํšŒ์‚ฌ๋“ค์ด ๊ณผ๊ฑฐ์—๋Š” ์‚ฌ์—… ๊ทœ๋ชจ๋‚˜ ์ผ์˜ ์†์„ฑ์„ ๋‘๊ณ  ๊ตฌ๋ถ„์„ ๋ช…ํ™•ํžˆ ํ•˜์˜€์œผ๋‚˜, ๋””์ง€ํ„ธ ์˜์—ญ์„ ๋‘๊ณ ๋Š” ๊ทธ ์—…๋ฌด์˜ ๊ฒฝ๊ณ„๋‚˜ ์ž‘์—…์˜ ๊ตฌ๋ถ„์ด ๋ชจํ˜ธํ•ด์ง€๊ณ  ์žˆ๋‹ค. **2.2 ์œ ํ˜•๋ณ„ ์ปจ์„คํŒ…์˜ ์ฃผ์š” ๋‚ด์šฉ ์œ ํ˜•๋ณ„ ์ปจ์„คํŒ…์˜ ๋‚ด์šฉ์— ๋Œ€ํ•ด ์ข€ ๋” ์•Œ์•„๋ณด๋ฉด ์šฐ์„ , ์šด์˜ ์ปจ์„คํŒ…์˜ ์ตœ๊ทผ ํŠธ๋ Œ๋“œ๋Š” ๋น„์šฉ ํ†ต์ œ, ๊ทธ์ค‘์—์„œ ๋น„์šฉ ์ ˆ๊ฐ(cost saving) ๋ถ€๋ถ„์— ์ฃผ๋ ฅ๋˜๊ณ  ์žˆ๋‹ค. ๊ธฐ์—… ๊ณ ๊ฐ๋“ค์€ ํ”„๋กœ์„ธ์Šค๋‚˜ ์šด์˜ ๊ธฐ๋ฒ•์„ ํ˜์‹ ํ•จ์œผ๋กœ์จ ๋†€๋ž„๋งŒํ•œ ๋น„์šฉ ํ˜์‹ ์„ ์ด๋ฃจ๊ธฐ๋ฅผ ์›ํ•œ๋‹ค. ๋‹ค์Œ์€ ๊ด€๋ จํ•ด์„œ ์ตœ๊ทผ ์ง‘์ค‘ํ•˜๋Š” ์˜์—ญ์ด๋‹ค. **-๊ณต๊ธ‰๋ง ๊ด€๋ฆฌ(Supply Chain Management: SCM)์˜ ํšจ์œจํ™” -ํšจ์œจ์ ์ธ ์„ธ๊ธˆ ๊ด€๋ฆฌ -ํšจ๊ณผ์ ์ธ ์•„์›ƒ์†Œ์‹ฑ(Outsourcing) ๋„์ž… ๊ณต๊ธ‰๋ง ๊ด€๋ฆฌ(SCM)๋Š” ํ”„๋กœ์„ธ์Šค ์„ค๊ณ„์™€ ๋”๋ถˆ์–ด IT์˜ ์—ญํ• ์ด ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ๊ธฐ์—… ์ž…์žฅ์—์„œ ์ตœ์ ํ™”๋œ ๊ณต๊ธ‰๋ง ๊ด€๋ฆฌ ์ฒด๊ณ„๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ๊ตฌ์ถ•ํ•˜๊ณ  ์šด์˜ํ•˜๋Š” ๊ฒƒ์ด ์ง€์ƒ ๊ณผ์ œ์ด๋‹ค. ๊ธ€๋กœ๋ฒŒ ๊ฒฝ์ œ ํ™˜๊ฒฝ์—์„œ ๊ณต๊ธ‰๋ง์ด ํ™•์žฅ๋˜๋‹ค ๋ณด๋‹ˆ ๋ฌด์—ญ์€ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ฐœ์ƒํ•˜๊ณ  ์ด์™€ ๊ด€๋ จํ•ด ๊ด€์„ธ๋ฅผ ํฌํ•จํ•œ ํšจ์œจ์ ์ธ ์„ธ๊ธˆ ๊ด€๋ฆฌ ๋ถ€๋ถ„์€ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…๋“ค์˜ ์ฃผ์š” ๊ด€์‹ฌ์‚ฌ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ๊ธฐ์—…์˜ ๊ตฌ๋งค ํ˜•ํƒœ๊ฐ€ ์ ์ฐจ ๊ธ€๋กœ๋ฒŒ ์†Œ์‹ฑ(Global Sourcing) ์ฒด๊ณ„๋กœ ์ง„ํ™”ํ•˜๋ฉด์„œ ๋‹ค์–‘ํ•œ ์„ธ๊ธˆ์„ ํšจ์œจ์ ์œผ๋กœ ๋‚ฉ๋ถ€ํ•˜๊ณ  ์ ˆ์„ธํ•˜๋Š” ๋ฐฉ์•ˆ์„ ์ฐพ๋Š” ๊ฒƒ์ด ๊ด€๋ จ ์ปจ์„คํŒ…์˜ ์ฃผ์š” ๋‚ด์šฉ์ด๋‹ค. ๋งˆ์ง€๋ง‰ ์•„์›ƒ์†Œ์‹ฑ ๋ถ€๋ถ„์€ ๊ธฐ์—…์˜ ๋น„ํ•ต์‹ฌ์—…๋ฌด(Non-core business)๋ฅผ ์ „๋ฌธ ๊ธฐ์—…์—๊ฒŒ ๋งก๊ธธ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ธฐ์—… ๋ณ€์‹ (Transformation)์˜ ๊ธฐํšŒ๋กœ ์‚ผ๊ธฐ ์œ„ํ•œ ์ „๋žต์  ์„ ํƒ์ง€(Strategic Option)๋กœ์„œ ์•„์›ƒ์†Œ์‹ฑ์„ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์•ˆ๊นŒ์ง€ ํฌํ•จํ•œ๋‹ค. ์˜คํ”„์‡ผ๋ง(Offshoring)[6]์ด๋ผ๋“ ๊ฐ€ ์‰์–ด๋“œ ์„œ๋น„์Šค(Shared Service)[7]๊ฐ™์€ ๊ฒƒ๋“ค์€ ๊ทธ๋Ÿฐ ๊ณ ๋ฏผ์˜ ์—ฐ์žฅ์„ ์— ์žˆ๋Š” ๊ฒƒ๋“ค์ด๋‹ค. ์กฐ์ง ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ, ์‚ฌ์—…์„ ๋ณด๋‹ค ์ž˜ ์ถ”์ง„ํ•˜๊ธฐ ์œ„ํ•œ ์กฐ์ง ๋ชจ๋ธ ์žฌ์„ค๊ณ„๋ฅผ ํฌํ•จํ•˜์—ฌ ํ•„์š”ํ•œ ์ธ์žฌ ํ™•๋ณด ๋ฐ ์กฐ์ง ๋ฌธํ™” ๊ตฌ์ถ•์— ์ฃผ๋ ฅํ•˜๊ณ  ์žˆ๋‹ค. ๋‹ค์Œ์€ ์ด์™€ ๊ด€๋ จํ•˜์—ฌ ์ตœ๊ทผ ์ฃผ๋ ฅํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. **-์ธ๋ ฅ ์ฑ„์šฉ ๋ฐ ์†Œ์‹ฑ -์ธ์žฌ ๊ด€๋ฆฌ๋ฅผ ํ†ตํ•œ ์„ฑ๊ณผ ์ฐฝ์ถœ -ํšจ์œจ์ ์ธ ์กฐ์ง ์šด์˜ -๋‹ค์–‘ํ•œ HR ์„ฑ๊ณผ ์ง€ํ‘œ์˜ ํšจ๊ณผ์  ๊ด€๋ฆฌ ์กฐ์ง ์ปจ์„คํŒ…์˜ ๋Œ€๋ถ€๋ถ„์€ ์ธ๋ ฅ ์ฑ„์šฉ ๋ฐ ์†Œ์‹ฑ์— ๊ด€ํ•œ ๊ฒƒ์ด๋‹ค. ์ตœ๊ทผ์—๋Š” HR๊ณผ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ์ธ์‚ฌ ์šด์˜ ๋ถ€๋ถ„์„ ์ „๋ฌธ์ ์ธ ์•„์›ƒ์†Œ์‹ฑ์œผ๋กœ ๋งŽ์ด ํ•ด๊ฒฐํ•˜๊ณ  ์žˆ๋‹ค. ์ธ์‚ฌ ์ „๋žต์ด๋‚˜ ์ธ์‚ฌ ๊ธฐํš ๋ถ€๋ถ„์„ ํ†ตํ•ด ๊ธฐ์—… ๊ณ ์œ ์˜ ๋ฌธํ™”๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ถ€๋ถ„์— ์ง‘์ค‘ํ•˜๊ณ  ๊ทธ ์™ธ ๋ถ€๋ถ„์€ ์—…๋ฌด ํšจ์œจ์„ฑ์„ ์ œ๊ณ ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์šด์˜ํ•˜๊ณ  ์žˆ๋‹ค. ์ฑ„์šฉ๋„ ๊ทธ๋Ÿฐ ์ธก๋ฉด์—์„œ ์›ํ•˜๋Š” ์ธ์žฌ์ƒ์— ๋งž์ถ”์–ด ์ฃผ๊ธฐ์ ์œผ๋กœ ๋˜๋Š” ์ˆ˜์‹œ๋กœ ์ฑ„์šฉ์„ ์ง„ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ์ง ์ปจ์„คํŒ…์€ ์ด๋Ÿฐ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์šฐ์ˆ˜๊ธฐ์—… ์‚ฌ๋ก€์˜ ์†Œ๊ฐœ ๋ฐ ์ ์šฉ ๋“ฑ์ด ์ฃผ์š” ๋‚ด์šฉ์„ ์ด๋ฃฌ๋‹ค. ์ „๋žต ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ, ๊ณผ๊ฑฐ ๋ช‡ ๋…„ ๊ฐ„ ๊ธ‰์†ํ•˜๊ฒŒ ์„ฑ์žฅํ•œ ์ปจ์„คํŒ… ์˜์—ญ์œผ๋กœ ๊ธ‰๋ณ€ํ•˜๋Š” ์‚ฌ์—… ํ™˜๊ฒฝ์—์„œ ๊ธฐ์—…์˜ ๋ฐฉํ–ฅ์„ ์ฐพ๊ธฐ ์œ„ํ•œ ๊ณ ๋ฏผ์œผ๋กœ ๋งŽ์ด ์ˆ˜ํ–‰๋˜์—ˆ๋‹ค. ๋น„์ „์ด๋‚˜ ์‚ฌ์—…์ „๋žต ์ˆ˜๋ฆฝ, ์‹ ์‚ฌ์—… ๋ฐœ๊ตด ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์ „๋žต ์ปจ์„คํŒ…์˜ ์ฃผ์š” ๋‚ด์šฉ์ด๋‹ค. ์ตœ๊ทผ์—๋Š” ์ข€ ๋” ๋‚˜์•„๊ฐ€์„œ ๋‹ค์Œ์˜ ๊ฒƒ๋“ค์— ์ดˆ์ ์„ ๋งž์ถ”๊ณ  ์žˆ๋‹ค. **-์šด์˜์ „๋žต -๋‚ด๋ถ€ ์กฐ์œจ -์„ฑ์žฅ ๋™๋ ฅ ๋ฐœ๊ตด ํฐ ๋ฐฉํ–ฅ ์ˆ˜๋ฆฝ์— ์ด์–ด ๊ณผ์ œ๋“ค(Initiatives) ๋ณ„๋กœ ์ƒ์„ธํ•œ ์šด์˜ ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๊ฑฐ๋‚˜ ์ผ๋ถ€๋Š” ์ปจ์„คํ„ดํŠธ๊ฐ€ ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์ค‘์žฅ๊ธฐ ๊ณผ์ œ์˜ ๊ฒฝ์šฐ, ํด๋ผ์ด์–ธํŠธ ๋‚ด๋ถ€์˜ ์กฐ์œจ์„ ์œ„ํ•ด ์ง€์†์ ์œผ๋กœ ์กฐ๋ ฅ์ž(facilitators) ์—ญํ• ์„ ํ•˜๊ธฐ๋„ ํ•˜๋ฉฐ, ๋‹จ์ˆœํ•œ ์‚ฌ์—… ์•„์ดํ…œ(Item) ๋ฐœ๊ตด๋ณด๋‹ค๋Š” ๊ธฐ์—…์˜ ์„ฑ์žฅ ๋™๋ ฅ ๋ฐœ๊ตด์„ ์œ„ํ•ด ๊ทผ๋ณธ์ ์ธ ๊ธฐ์—… ์žฌ์กฐ์ง(Rebuilding or Restructuring)๊นŒ์ง€ ๊ณ ๋ฏผํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. ์žฌ๋ฌดํšŒ๊ณ„ ์ปจ์„คํŒ…์˜ ์ตœ๊ทผ ํ™”๋‘๋Š” ํˆฌ๋ช…์„ฑ(Transparency)์˜ ํ–ฅ์ƒ๊ณผ ์˜์‚ฌ ๊ฒฐ์ •์˜ ํšจ์œจ์„ฑ ์ œ๊ณ ์ด๋‹ค. ์ง€๋‚œ 20์—ฌ ๋…„๊ฐ„ ERP ๊ตฌ์ถ•์„ ํ™”๋‘๋กœ ๋งŽ์€ ๊ธฐ์—…๋“ค์ด ์žฌ๋ฌดํšŒ๊ณ„ ๊ธฐ๋ฐ˜์„ ์ž๋™ํ™”ํ•˜์˜€์œผ๋ฉฐ, ์—…๋ฌด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๊ฐ์ข… ๋น„์šฉ ํ•ญ๋ชฉ๋“ค์˜ ํˆฌ๋ช…ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ์ถ”๊ตฌํ•˜๊ณ  ์žˆ๋‹ค. ๋˜ํ•œ, ์ตœ๊ทผ ๋น…๋ฐ์ดํ„ฐ(Big Data) ํ™œ์šฉ์˜ ๋ถ€์ƒ(ๆตฎไธŠ)์œผ๋กœ ๊ธฐ์—… ๋‚ด ์˜์‚ฌ๊ฒฐ์ • ๊ณผ์ •์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋„์ž…, ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋”์šฑ ํ™œ๋ฐœํ•ด์ง€๊ณ  ์žˆ๋‹ค. ๊ธฐ์—… ํ™œ๋™๊ณผ ๊ด€๋ จํ•ด์„œ๋Š” ๊ฒฝ์ œ๋‚˜ ํ™˜๊ฒฝ, ์‚ฐ์—…๊ณผ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ๊ทœ์ œ(regulation)๋‚˜ ๋ฒ•๊ทœ์˜ ์ค€์ˆ˜(compliance)์™€ ๊ด€๋ จ๋œ ์ปดํ”Œ๋ผ์ด์–ธ์Šค(Compliance), ๊ฑฐ๋ฒ„๋„Œ์Šค(Governance), ์œ„ํ—˜๊ด€๋ฆฌ(Risk Management) ์ปจ์„คํŒ…์ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฃผ์ œ๋ฅผ ๊ฐ€์ง€๊ณ  ์ง„ํ–‰๋˜๊ณ  ์žˆ๋‹ค. **-๊ทœ์ œ์™€ ๋ฒ•๋ฅ  -๊ธ€๋กœ๋ฒŒํ™” -ํ‰ํŒ ๋ฐ ๋Œ€์ค‘์  ์ธ์ง€ ๋ณ€ํ™”๊ด€๋ฆฌ ์ปจ์„คํŒ…์€ ์ตœ๊ทผ๊นŒ์ง€ ์ธ์ˆ˜ํ•ฉ๋ณ‘(Merger& Acquisition) ํ›„ ์กฐ์ง ๋ฐ ์ œ๋„, IT์˜ ํ†ตํ•ฉ๊ณผ ๊ด€๋ จ๋œ ๋ณ€ํ™”๊ด€๋ฆฌ ์ฆ‰, PMI[8] ํ”„๋กœ์ ํŠธ๊ฐ€ ํ™”๋‘์˜€์œผ๋‚˜ ๊ตญ๋‚ด์—์„œ๋Š” ์ธ์ˆ˜ํ•ฉ๋ณ‘์ด ํ™œ๋ฐœํ•˜๊ฒŒ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•„ ์‹ค์ œ PMI๋ฅผ ์ฃผ์ œ๋กœ ํ•œ ์ปจ์„คํŒ…์ด ๋งŽ์ด ์ง„ํ–‰๋˜์ง€๋Š” ๋ชปํ–ˆ๋‹ค. ๋‹ค๋งŒ, ํ”„๋กœ์ ํŠธ ๋‚ด์˜ ์—…๋ฌด ๋‹จ์œ„ ์„ฑ๊ฒฉ์œผ๋กœ๋Š” BPR/ISP์—์„œ๋„ ํฌํ•จ๋˜์–ด ์ง„ํ–‰๋˜๋Š” ๋“ฑ ๋ณ€ํ™”๊ด€๋ฆฌ์— ๋Œ€ํ•œ ๊ธ์ •์ ์ธ ์ธ์‹์€ ํ™•์‚ฐ๋˜์–ด๊ฐ€๊ณ  ์žˆ์œผ๋ฉฐ, ๊ทธ ๋‚ด์šฉ๋„ ๊ด‘๋ฒ”์œ„ํ•œ ์˜์—ญ์—์„œ ๋ณ€ํ™”์™€ ํ˜์‹ ์„ ์ฃผ์š” ๋Œ€์ƒ์œผ๋กœ ํ•˜๊ณ  ์žˆ๋‹ค. ์ตœ๊ทผ ๋ณ€ํ™”๊ด€๋ฆฌ ์ธก๋ฉด์—์„œ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์˜ ์ฃผ์š” ๊ด€์‹ฌ์‚ฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. **-๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ํ˜์‹  -์šด์˜์„ฑ๊ณผ ํ–ฅ์ƒ -์กฐ์ง ๋ฐ ์„ฑ๊ณผ๊ด€๋ฆฌ ์ œ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์ง„๋‹จ ์ปจ์„คํŒ…์€ ๊ธฐ์—…์˜ ์‚ฌ์—…๊ณผ ์šด์˜์„ ํฌํ•จํ•œ ๊ฒฝ์˜ ์ „๋ฐ˜์„ ์ง„๋‹จํ•˜๋Š” ๊ฒฝ์˜ ์ง„๋‹จ๊ณผ ์กฐ์ง ์—ญ๋Ÿ‰ ๋ฐ ์กฐ์ง ๋ฌธํ™” ๋“ฑ์œผ๋กœ ํ•œ์ •ํ•œ ์กฐ์ง ์ง„๋‹จ์œผ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ๊ฒฝ์˜ ์ง„๋‹จ์€ ์ฃผ์ œ์— ๋”ฐ๋ผ์„œ ์ „๋žต, ๋งˆ์ผ€ํŒ…, ๊ตฌ๋งค, IT ๋“ฑ ๋”์šฑ ์„ธ๋ถ„ํ™”๋  ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ, ์œค๋ฆฌ๊ฒฝ์˜[9]์ด ๊ฐ•์กฐ๋˜๋Š” ์˜ค๋Š˜๋‚  ๊ฐ•๋ ฅํ•œ ๋ถ€ํŒจ ์ฒ™๊ฒฐ ์˜์ง€๋ฅผ ํ‘œ๋ช…ํ•˜๊ณ  ์‹ค์ฒœํ•˜๋Š” ๊ฒƒ์€ ๊ธฐ์—…์˜ ํฐ ๊ฒฝ์Ÿ๋ ฅ์œผ๋กœ ์ธ์ •๋ฐ›๊ณ  ์žˆ๋‹ค. ๋‹ค์Œ์€ ์ตœ๊ทผ ๋””์ง€ํ„ธ ์–ด์  ๋‹ค์™€ ๋”๋ถˆ์–ด ๊ฐ€์žฅ ํ•ซํ•œ IT ์ปจ์„คํŒ…์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. [1] Business-to-Business Business-to-Consumer [2] ์‹ค์งˆ์ ์ธ ์ˆ˜์ž…์€ ์ˆ˜์ต๋ฅ (์˜์—…์ด์ต๋ฅ )๊ณผ ๋งค์ถœ ๊ทœ๋ชจ๋ฅผ ๊ฐ™์ด ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค. [3] ๊ธ€๋กœ๋ฒŒ ๊ธˆ์œต ์œ„๊ธฐ๋ฅผ ๋‘ ๋ฒˆ์ด๋‚˜ ๊ฒช์€ ํ˜„์‹œ์ ์˜ ์ •๋ณด์™€๋Š” ๋‹ค์†Œ ์ฐจ์ด๊ฐ€ ๋‚  ์ˆ˜ ์žˆ์–ด ์ˆœ์œ„ ๋ณ€๋™์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค [4] Enterprise Resource Planning ์ „์‚ฌ์  ์ž์›๊ด€๋ฆฌ [4] www.idc.com [5] Billion Dollars [6] ๊ธฐ์—… ์šด์˜์˜ ์ผ๋ถ€๋ฅผ ํ•ด์™ธ ๊ธฐ์—…์— ๋งก๊ฒจ ์ฒ˜๋ฆฌํ•จ. ๋ฏธ๊ตญ์˜ IT ์šด์˜ ์ผ๋ถ€๋ฅผ ์ธ๋„ IT ํšŒ์‚ฌ๊ฐ€ ์ฒ˜๋ฆฌํ•˜์—ฌ 24์‹œ๊ฐ„ ์„œ๋น„์Šค ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์ฒด๊ณ„ ๋“ฑ์ด ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ด๋‹ค [7] ์ธ์‚ฌ, ์žฌ๋ฌด, ํšŒ๊ณ„, ๊ตฌ๋งค ๋“ฑ์˜ ์„œ๋น„์Šค๋ฅผ A, B, C ํšŒ์‚ฌ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๋Œ€ํ–‰ํ•ด ์ฃผ๋Š” ํ”„๋กœ์„ธ์Šค ์•„์›ƒ์†Œ์‹ฑ์˜ ํ•œ ํ˜•ํƒœ. ๊ด€๋ จ ์ •๋ณด์‹œ์Šคํ…œ์ด ๊ฐ™๋‹ค๋Š” ์ ์„ ํ™œ์šฉํ•˜๋ฉฐ ์ตœ๊ทผ์—๋Š” SaaS(Software as a Service)์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋งŽ์ด ์„œ๋น„์Šค๋œ๋‹ค. [8] Post-Merger Integration [9] Ethical Management. ํšŒ์‚ฌ ๊ฒฝ์˜ ๋ฐ ๊ฑฐ๋ž˜์—์„œ ๊ธฐ์—… ์œค๋ฆฌ๋ฅผ ์ตœ์šฐ์„  ๊ฐ€์น˜๋กœ ์ƒ๊ฐํ•˜๋ฉฐ ํˆฌ๋ช…ํ•˜๊ณ  ๊ณต์ •ํ•˜๋ฉฐ ํ•ฉ๋ฆฌ์ ์ธ ๊ธฐ์—… ํ™œ๋™์„ ์ง€ํ–ฅํ•˜๋Š” ๊ฒฝ์˜ ๋ฐฉ์‹ **2.3 IT ์„œ๋น„์Šค ํ˜„ํ™ฉ๊ณผ IT ์ปจ์„คํŒ… ์ตœ๊ทผ ์‚ฐ์—… ์ „๋ฐ˜์—์„œ IT๊ฐ€ ๋งค์šฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•˜๊ฒŒ ๋จ์— ๋”ฐ๋ผ IT ์ปจ์„คํŒ…๋„ ๊ณผ๊ฑฐ์™€ ๋‹ฌ๋ฆฌ ์œ„์ƒ์ด ๋งŽ์ด ๋‹ฌ๋ผ์กŒ๋‹ค. IT ์ปจ์„คํŒ…์€ IT ์„œ๋น„์Šค์˜ ํ•œ ์˜์—ญ์œผ๋กœ ๊ณ ๊ฐ ๋งž์ถคํ˜• ๊ฐœ๋ฐœ์ด๋‚˜ ์‹œ์Šคํ…œ ํ†ตํ•ฉ(SI)[1] ์‚ฌ์—…๊ณผ ๊ด€๊ณ„๊ฐ€ ๊นŠ๋‹ค. SI ์‚ฌ์—…์€ ๊ณ ๊ฐ์˜ ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž์ถ”์–ด ๋งž์ถคํ˜• ์ •๋ณด์‹œ์Šคํ…œ์„ ๊ตฌ์ถ•ํ•ด ์ฃผ๋Š” ์‚ฌ์—…์„ ์ง€์นญํ•˜๋Š” ๊ฒƒ์ธ๋ฐ ์ตœ๊ทผ์—๋Š” ๋‹จ์ˆœ ๊ตฌ์ถ•์„ ๋„˜์–ด์„œ ์šด์˜ ๋ฐ ์œ ์ง€ ๋ณด์ˆ˜, ๊ต์œก ๋“ฑ์„ ์ „์ฒด IT ์„œ๋น„์Šค๋กœ ๊ทœ์ •ํ•˜๊ณ  ์ด ์ „์ฒด๋ฅผ IT ์•„์›ƒ์†Œ์‹ฑ ์‚ฌ์—…์œผ๋กœ ์ •์˜ํ•œ๋‹ค. IDC ์กฐ์‚ฌ์— ๋”ฐ๋ฅด๋ฉด 2014๋…„๋ถ€ํ„ฐ 2019๋…„๊นŒ์ง€ ํ•œ๊ตญ์˜ IT ์„œ๋น„์Šค ์‹œ์žฅ์€ ์—ฐ 2.0% P ์„ฑ์žฅํ•  ๊ฒƒ์œผ๋กœ ์ „๋งํ•˜๋ฉฐ ํŠนํžˆ IT ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ, ์—ฐ 2.7% P์”ฉ ์„ฑ์žฅํ•˜์—ฌ 2014๋…„ 1,800์–ต ๋Œ€ ๊ทœ๋ชจ์—์„œ 2019๋…„ 2,000์–ต ๋Œ€ ๊ทœ๋ชจ๊ฐ€ ๋  ๊ฒƒ์œผ๋กœ ์ถ”์ •ํ•˜๊ณ  ์žˆ๋‹ค. ํ•œ๊ตญ์˜ IT ์ปจ์„คํŒ… ์‚ฌ์—…์€ ํฌ๊ฒŒ ๊ณต๊ณต ์˜์—ญ์˜ ์‚ฌ์—…๊ณผ ๋ฏผ๊ฐ„ ์˜์—ญ์˜ ์‚ฌ์—…์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ณต๊ณต IT ์ปจ์„คํŒ…์€ IT ์‹œ์Šคํ…œ ๊ตฌ์ถ•์„ ์œ„ํ•œ BPR/ISP[2]๊ฐ€ ๋Œ€๋ถ€๋ถ„์„<NAME>๋‹ค. ์ด๋Š” โ€˜์†Œํ”„ํŠธ์›จ์–ด ์‚ฐ์—…์ง„ํฅ๋ฒ•โ€™ ๋“ฑ ๊ด€๊ณ„๋ฒ•์— ์˜๊ฑฐํ•˜์—ฌ ๊ณต๊ณต IT ์‹œ์Šคํ…œ์˜ ๊ตฌ์ถ• ์ „์— ์ด๋ฅผ ์˜๋ฌด์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.[3] ๋ฐ˜๋ฉด, ๋ฏผ๊ฐ„ IT ์ปจ์„คํŒ…์€ ๊ธฐ์—…์˜ ์—…๋ฌด ํ”„๋กœ์„ธ์Šค ์ž๋™ํ™”๋‚˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ํ˜์‹ ์„ ์œ„ํ•ด ๋„์ž…ํ•˜๋Š” IT ์„ค๋ฃจ์…˜ ์ปจ์„คํŒ…์ด ๋Œ€๋ถ€๋ถ„์ด๋‹ค. ERP๋‚˜ SCM ๋“ฑ ๊ธฐ์—…์˜ ์—…๋ฌด ํ”„๋กœ์„ธ์Šค์™€ ์ง๊ฒฐ๋˜๋Š” IT ์‹œ์Šคํ…œ์„ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…๋“ค์ด ํŒจํ‚ค์ง€ ์„ค๋ฃจ์…˜ ํ™”ํ•˜์—ฌ ์ „ํŒŒํ•จ์œผ๋กœ์จ ๋งŽ์€ ๊ธฐ์—…๋“ค์ด ์ด๋ฅผ ๋„์ž…ํ•˜์˜€๊ณ , ๊ทธ ๊ณผ์ •์—์„œ ํ•ด๋‹น ๊ธฐ์—…์— ๋งž๊ฒŒ ์ผ๋ถ€ ์ˆ˜์ •์ด ํ•„์š”ํ•œ๋ฐ ์„ค๋ฃจ์…˜ ์ปจ์„คํŒ…์€ ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์—์„œ ํ•ด๋‹น ์—…๋ฌด ํ”„๋กœ์„ธ์Šค ์ „๋ฌธ๊ฐ€๊ฐ€ ์ด๋ฅผ ์ปจ์„คํŒ…ํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐœ์„ ํ•˜๊ณ  ๊ตฌ์ถ•๊นŒ์ง€ ํ•˜๋Š” ํ˜•ํƒœ๋กœ ์ „๊ฐœ๋˜์—ˆ๋‹ค. ํ•œ๊ตญ IT ์„œ๋น„์Šค ํ˜‘ํšŒ[4]์—์„œ๋Š” IT ์„œ๋น„์Šค๋ฅผ Table I-4์™€ ๊ฐ™์ด ๊ตฌ๋ถ„ํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ตœ๊ทผ์—๋Š” ํด๋ผ์šฐ๋“œ(Cloud) IT ์„œ๋น„์Šค์˜ ํ™œ์„ฑํ™”๋กœ ์ธํ•ด Table I-4์˜ ๋ถ„๋ฅ˜์™€๋Š” ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ IT ์„œ๋น„์Šค ์‚ฌ์—…๋„ ๋“ฑ์žฅํ•˜๊ณ  ์žˆ๋‹ค. ํด๋ผ์šฐ๋“œ ์‚ฌ์—…์ž๋“ค์€ IT ์„œ๋น„์Šค์˜ ๋น„์šฉ ์ฒด๊ณ„๋ฅผ ์ข…๋Ÿ‰์ œ(Pay-per-Use)๋กœ ํ˜์‹ ํ•จ์œผ๋กœ์จ IT ์ธํ”„๋ผ๋ถ€ํ„ฐ ์‘์šฉ ์†Œํ”„ํŠธ์›จ์–ด๊นŒ์ง€ ์‚ฌ์šฉํ•œ ๋งŒํผ ๋น„์šฉ์„ ์ง€๋ถˆํ•˜๋Š” ํ˜•ํƒœ๋กœ ๋ฐ”๋€Œ๊ณ  ์žˆ๋‹ค.[5] ํด๋ผ์šฐ๋“œ ์„œ๋น„์Šค์˜ ๊ฒฝ์šฐ, ์ธํ”„๋ผ์ด๋ฉด์„œ ์„ค๋ฃจ์…˜์ธ๋ฐ๋„ ๋ช‡ ๊ฐ€์ง€ ํŒŒ๋ผ๋ฏธํ„ฐ (parameter) ์„ค์ •์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋„์ž… ๊ฐ€๋Šฅํ•˜์—ฌ IT ์ปจ์„คํŒ…์˜ ํ•„์š”์„ฑ์ด ํฌ๋ฐ•ํ•˜๋‹ค. ๋ฐ˜๋ฉด์— ์ตœ๊ทผ ์ „ ์„ธ๊ณ„์ ์ธ ๊ด€์‹ฌ์„ ๋ฐ›๊ณ  ์žˆ๋Š” ์ œ4์ฐจ ์‚ฐ์—… ํ˜๋ช…์˜ ๋„๋ž˜์™€ ๊ด€๋ จํ•˜์—ฌ ๋””์ง€ํ„ธ ํ˜๋ช…์ด ์‚ฐ์—… ์ „๋ฐ˜์—์„œ ์žฌ๊ณ ๋ ค๋จ์— ๋”ฐ๋ผ ๋””์ง€ํ„ธ ์ „๋žต ์ปจ์„คํŒ…์€ ๋‹ค์‹œ ๋ถ(boom)์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋””์ง€ํ„ธ์ด ์ฒ˜์Œ ๋“ฑ์žฅํ•œ 1980๋…„๋Œ€ ์ดˆ๋ฐ˜๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๋””์ง€ํ„ธ ๋งˆ์ผ€ํŒ…(Digital Marketing)์„ ํฌํ•จํ•œ ํด๋ผ์šฐ๋“œ, ๋น…๋ฐ์ดํ„ฐ, ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท(IoT)[6], ๋ชจ๋ฐ”์ผ, ๋กœ๋ด‡๊ณผ ์ธ๊ณต์ง€๋Šฅ ๋“ฑ์˜ ํ™œ์šฉ์„ ํ†ตํ•œ ๊ธฐ์—… ๋ณ€์‹ ์€ ๊ฒฝ์Ÿ๋ ฅ ๊ด€์ ์— ์ฃผ์ €ํ•  ํ•„์š”๊ฐ€ ์—†๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด๊ฒƒ๋“ค๋กœ ์ธํ•œ ์‚ฐ์—… ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ณ€ํ™”์™€ ๊ธฐ์—…์˜ ๋น„์ „/์ „๋žต์˜ ์ˆ˜์ •์€ ๋‹น๋ถ„๊ฐ„ ์ˆ˜๋งŽ์€ ์ปจ์„คํŒ… ์ˆ˜์š”๋ฅผ ์ƒ์‚ฐํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฉ”๊ฐ€ํŠธ๋ Œ๋“œ๊ฐ€ ๋ฐ”๋€œ์— ๋”ฐ๋ผ ์ปจ์„คํŒ… ํŒจ๋Ÿฌ๋‹ค์ž„๋„ ๋ฐ”๋€Œ๊ณ  ์žˆ๋‹ค. ์ „๋žต ์ปจ์„คํŒ…์ด๋‚˜ ์šด์˜ ์ปจ์„คํŒ…์„ ํ•  ๋•Œ ๊ณผ๊ฑฐ์—๋Š” ์•Œ ํ•„์š” ์—†๋˜ IT์— ๋Œ€ํ•œ ๋†’์€ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋˜๊ฐ€, ๋ฐ˜๋Œ€๋กœ IT ์ปจ์„คํŒ…์ธ๋ฐ๋„ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์ „๋žต๊ณผ ์šด์˜์— ๋Œ€ํ•œ ์ง€์‹์ด ํ’๋ถ€ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ ๋“ฑ์ด๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ด๋Ÿฐ ๋ณ€ํ™”๋Š” ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์˜ ์‚ฌ์—… ๊ฒฝ๊ณ„๋ฅผ ๋”์šฑ ํฌ๋ฏธํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์žˆ๋‹ค. **Break #2. ๋””์ง€ํ„ธ ๋งˆ์ผ€ํŒ…๊ณผ ๋ฒ„๋ฐ”๋ฆฌ ๋””์ง€ํ„ธ ์ „๋žต ์ปจ์„คํŒ…๊ณผ ๊ด€๋ จํ•ด์„œ ๋‹ค์–‘ํ•œ ์‚ฌ๋ก€๊ฐ€ ์žˆ์ง€๋งŒ ์˜๊ตญ์˜ ๋Œ€ํ‘œ์ ์ธ ๋ช…ํ’ˆ ๋ธŒ๋žœ๋“œ ๋ฒ„๋ฐ”๋ฆฌ(Burberry)์˜ ์‚ฌ๋ก€๋Š” ๊ฝค ์ธ์ƒ์ ์ด๋‹ค. ๋ฒ„๋ฐ”๋ฆฌ๋Š” ์ฒดํฌ๋ฌด๋Šฌ์™€ ํŠธ๋ Œ์น˜์ฝ”ํŠธ๋กœ ๋งค์šฐ ์œ ๋ช…ํ•œ ๋ช…ํ’ˆ ํŒจ์…˜ ๊ธฐ์—…์ธ๋ฐ โ€˜Luxuryโ€™๋ฅผ ์ถ”๊ตฌํ•˜๋Š” ๋ช…ํ’ˆ ์‚ฐ์—…์˜ ํŠน์„ฑ์ƒ ํ์‡„์ ์ด๊ณ  ๋Œ€๋ถ€๋ถ„์˜ ๋ช…ํ’ˆ๊ธฐ์—…๋“ค์ด ๊ทธ๋ ‡๋“ฏ ์ธํ„ฐ๋„ท์ด๋‚˜ ๋””์ง€ํ„ธ๊ณผ๋Š” ๊ด€๊ณ„๊ฐ€ ์—†๋Š” ํ–‰๋ณด๋ฅผ ์ตœ๊ทผ๊นŒ์ง€ ํ•ด์™”๋‹ค. Fig I-11. ๋ฒ„๋ฐ”๋ฆฌ ๋กœ๊ณ  ๋ฐ ํŠธ๋ Œ์น˜์ฝ”ํŠธ ๋ฒ„๋ฐ”๋ฆฌ์˜ ์ „(ๅ‰) CEO ์•ˆ์ ค๋ผ ์•„๋ Œ์ธ (Angela Ahrendts)[7]๋Š” ๋””์ง€ํ„ธ ์ „๋žต์„ ์ ๊ทน์ ์œผ๋กœ ํ™œ์šฉํ•˜์—ฌ ์“ฐ๋Ÿฌ์ ธ ๊ฐ€ Fig I-12. ๋ฒ„๋ฐ”๋ฆฌ ํšŒ์ƒ์˜ ์ฃผ์—ญ๋“ค ๋Š” ๋ฒ„๋ฐ”๋ฆฌ๋ฅผ ํšŒ์ƒ์‹œํ‚จ ๊ฒƒ์œผ๋กœ ์œ ๋ช…ํ•˜๋‹ค. โ€˜๋ฒ„๋ฐ”๋ฆฌโ€™๊ฐ€ ๋…ธ๋…„์„ ์œ„ํ•œ ์ด๋ฏธ์ง€๊ฐ€ ์ปค์ ธ๊ฐ€๊ณ  ์˜ฌ๋“œ(old) ํ•œ ํŒจ์…˜ ๋ธŒ๋žœ๋“œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๊ณ ์ž ๋‹ค๋ฐฉ๋ฉด์˜ ์‹œ์‚ฌ์ ์„ ์ ๊ฒ€ํ•˜์˜€๋Š”๋ฐ, ๊ทธ๋•Œ ์•ˆ์ ค๋ผ๋Š” ๋– ์˜ค๋ฅด๋Š” ์ƒˆ๋กœ์šด ๊ณ ๊ฐ๊ตฐ โ€˜๋ฐ€๋ ˆ๋‹ˆ์–ผ ์„ธ๋Œ€(Millennial Generation)โ€™์— ๋Œ€ํ•ด ์ธ์ง€ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. 1980๋…„ ~ 2004๋…„ ์‚ฌ์ด์— ํƒœ์–ด๋‚œ ๋ฐ€๋ ˆ๋‹ˆ์–ผ ์„ธ๋Œ€์˜ ์†Œ๋น„ ํŠน์„ฑ์€ ๋ฌผ์งˆ์ ์œผ๋กœ๋Š” ํ’์š”๋กญ์ง€ ์•Š์ง€๋งŒ ๊ทธ๋“ค์˜ ๊ฐœ์„ฑ์„ ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ์ผ์—๋Š” ์ง€๊ฐ‘์„ ๊ณผ๊ฐํ•˜๊ฒŒ ์—ฌ๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์—ˆ๊ณ  ๋ฌด์—‡๋ณด๋‹ค๋„ ๋””์ง€ํ„ธ ๊ธฐ๊ธฐ์— ๋งค์šฐ ์ต์ˆ™ํ•œ ๊ณ ๊ฐ๊ตฐ์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋˜ํ•œ, ๋„ˆ๋ฌด ์„ธ๋ถ„ํ™”๋œ ์ œํ’ˆ ๋ธŒ๋žœ๋“œ๋ฅผ ๊ฒฝ์Ÿ๋ ฅ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€๋กœ ํ†ต์ผํ•˜๊ณ  ์ „ ์„ธ๊ณ„์— ํผ์ ธ ์žˆ๋Š” ๋ธŒ๋žœ๋“œ ๊ด€๋ จ ์ œ๋ฐ˜ ์ผ์„ ์ง‘์ค‘ํ•˜๊ฒŒ ํ•˜์—ฌ ๋ธŒ๋žœ๋“œ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ (Brand Communication)์„ ๋‹จ์ผํ™”ํ•˜์˜€๋‹ค. ํฌ๋ฆฌ์—์ดํ‹ฐ๋ธŒ ๋””๋ ‰ํ„ฐ์ธ ํฌ๋ฆฌ์Šคํ† ํผ ๋ฒ ์ผ๋ฆฌ(Christoper Bailey)๋Š” ์ด๋Ÿฐ ์ผ์˜ ์ „๊ถŒ์„ ์œ„์ž„๋ฐ›๊ณ  ์•ˆ์ ค๋ผ์™€ ํ•จ๊ป˜ ๋””์ง€ํ„ธ ๋ฒ„๋ฐ”๋ฆฌ๋ฅผ ์‹คํ–‰์— ์˜ฎ๊ฒผ๋‹ค. Fig I-13. ๋ฒ„๋ฐ”๋ฆฌ-์ธ์Šคํƒ€๊ทธ๋žจ ํ˜‘์—… ์•ˆ์ ค๋ผ์˜ ๋””์ง€ํ„ธ ํ˜์‹  ์ „๋žต์€ 2009๋…„ ๊ฐ€์„๋ถ€ํ„ฐ ํŒจ์…˜์‡ผ๋ฅผ ์ธ์Šคํƒ€๊ทธ๋žจ(Instagram), ํŠธ์œ„ํ„ฐ(Twitter), ํŽ˜์ด์Šค๋ถ(Facebook) ๋“ฑ ์†Œ์…œ ๋ฏธ๋””์–ด๋ฅผ ํ†ตํ•ด ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ์ œ๊ณตํ•˜์˜€์œผ๋ฉฐ, ํŒจ์…˜์‡ผ๊ฐ€ ๋๋‚˜๋ฉด ์˜จ๋ผ์ธ ์Šคํ† ์–ด ๋ฒ„๋ฐ”๋ฆฌ๋‹ท์ปด(burberry.com)๊ณผ ์˜คํ”„๋ผ์ธ ๋งค์žฅ์—์„œ ์ฆ‰์‹œ ๊ทธ ์˜ท์„ ๊ตฌ๋งคํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€๋‹ค. ํŒŒ๋ฆฌ ๋“ฑ์—์„œ ์—ด๋ฆฌ๋Š” ์ „ํ†ต์ ์ธ ํŒจ์…˜์‡ผ๋Š” ๊ณ„์ ˆ์˜ ๊ฐ„๊ทน์ด ์žˆ์–ด ์—ฌ๋ฆ„์— ํŒจ์…˜์‡ผ๋ฅผ ํ•˜๋ฉด ๋‹ค์Œ ๊ณ„์ ˆ์ธ ๊ฐ€์„์— ํ•ด๋‹น ์˜ท์„ ํŒŒ๋Š” ํŒจํ„ด์„ ๋ณด์˜€๋Š”๋ฐ ๋ฒ„๋ฐ”๋ฆฌ๋Š” ๊ทธ๋Ÿฐ ๊ฒƒ์„ ์—†์• ๋ฒ„๋ฆฐ ๊ฒƒ์ด๋‹ค. Fig I-14. ๋ฒ„๋ฐ”๋ฆฌ ๋ฆฌํ…Œ์ผ์ˆ, ์˜๊ตญ ๋Ÿฐ๋˜ ์˜จ๋ผ์ธ ์Šคํ† ์–ด์™€ ์˜คํ”„๋ผ์ธ ๋งค์žฅ์˜ ๊ฒฐํ•ฉ์€ ๋ฒ„๋ฐ”๋ฆฌ์˜ ๋ฆฌํ…Œ์ผ ํ˜์‹  ์ „๋žต์˜ ์ •์ ์„ ์ฐ๋Š”๋ฐ, ์˜๊ตญ ๋Ÿฐ๋˜์˜ ํ”ผ์นด๋‹ค๋ฆฌ ์„œ์ปค์Šค ๋ฆฌ์  ํŠธ ๊ฑฐ๋ฆฌ์— ์žˆ๋Š” ํ”Œ๋ž˜๊ทธ์‹ญ(Flagship) ๋งค์žฅ์—์„œ ๊ทธ ๊ฒฐ์‹ค์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค(Figure I-14-โ‘ ). 2014๋…„ ํ•ด๋‹น ๋งค์žฅ์„ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์žˆ์—ˆ๋Š”๋ฐ ํ™”๋ คํ•˜๊ฒŒ ๊พธ๋ฉฐ์ง„ ๋ช…ํ’ˆ ๋งค์žฅ์˜ ์™ธํ˜•์„ ๋„˜์–ด์„œ์„œ ์˜ท ๊ทผ์ฒ˜์— ๊ฐ€๋ฉด ๊ฑฐ์šธ์—์„œ ํ•ด๋‹น ์˜ท์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์ •๋ณด๊ฐ€ ๋œฌ๋‹ค(Figure I-14-โ‘ก). ๋˜ํ•œ, ๊ตฌ๋งคํ•œ ์˜ท์— ์ƒˆ๊ฒจ์ง„ QR์ฝ”๋“œ[8]๋Š” ์Šค์บ” ์‹œ ์ƒ์‚ฐ ๊ณผ์ •์ด๋‚˜ ๋Ÿฐ์›จ์ด(runway) ๋ชจ์Šต์„ ๋ณด์—ฌ์ฃผ๋ฉฐ ๊ณ ๊ฐ์˜ ๊ฒฝํ—˜์„ ํ™•๋Œ€ํ•˜๋Š” ๋„๊ตฌ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค(Figure I-14-โ‘ฃ). ๋˜ํ•œ, ์• ํ”Œ๊ณผ๋„ ์ƒ๋‹นํžˆ ๋†’์€ ์ˆ˜์ค€์˜ ํ˜‘์—…์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋Š”๋ฐ ์˜คํ”„๋ผ์ธ ๋งค์žฅ์˜ ์ ์›๋“ค์€ ์•„์ดํŒจ๋“œ์— ํƒ‘์žฌ๋œ ๊ณ ๊ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(Customer One-to-One)๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋ผ์ธ/ ์˜คํ”„๋ผ์ธ ๊ตฌ๋งค ๋ชฉ๋ก, ์‚ฌ์ด์ฆˆ, ์ƒ‰์ƒ, ๋งค์žฅ ๋ฐฉ๋ฌธ ๊ธฐ๋ก, ์ถ”์ฒœ์ƒํ’ˆ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ํ”„๋กœํŒŒ์ผ๋ง ํ•˜์—ฌ ์‹ค์‹œ๊ฐ„์œผ๋กœ ํŒ๋งค์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€์œผ๋ฉฐ, ๊ฒฐ์ œ๋„ ์•„์ดํŒจ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์˜จ๋ผ์ธ ๊ฒฐ์ œ๋ฅผ ํ•˜๊ฒŒ ํ•˜์˜€๋‹ค(Figure I-14-โ‘ข). ์•„์ดํฐ 5/5S ๋ก ์นญ ๋•Œ๋Š” ์ผ๋ฐ˜ ์นด๋ฉ”๋ผ ๋Œ€์‹  ์•„์ดํฐ์œผ๋กœ ํŒจ์…˜์‡ผ๋ฅผ ์ค‘๊ณ„ํ•˜์˜€๊ณ , ์• ํ”Œ ๋ฎค์ง์— ๋ฒ„๋ฒ„๋ผ ์ฑ„๋„์„ ์˜คํ”ˆํ•˜์—ฌ ํŒจ์…˜์‡ผ์—์„œ ์‚ฌ์šฉ๋œ ์Œ์›์„ ํŒ”๊ฑฐ๋‚˜ ์• ํ”Œ ๋ฎค์ง ์‚ฌ์šฉ์ž๋“ค์˜ ์ถ”์ฒœ๊ณก์„ ํŒจ์…˜์‡ผ์— ์‚ฌ์šฉํ•˜์—ฌ โ€˜๊ณ ๊ฐ๊ณผ ์†Œํ†ตํ•˜๋Š” ์ Š์€ ๋ฒ„๋ฐ”๋ฆฌโ€™๋ผ๋Š” ์ด๋ฏธ์ง€๋ฅผ ํ™•๋ฆฝํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ์˜๊ตญ ๋Ÿฐ๋˜์˜ ์ฝ”๋ฒคํŠธ ๊ฐ€๋“  ๋งค์žฅ์€ ์ ‹์€์ด๋“ค์˜ ์œ ๋™ ์ธ๊ตฌ๊ฐ€ ๋งŽ์•„ ์ด๋Ÿฐ ๋ฒ„๋ฐ”๋ฆฌ์˜ ๋ณ€ํ™”๋ฅผ ์ข€ ๋” ๊ณ ๋ คํ•˜์˜€๋Š”๋ฐ ํ™”์žฅํ’ˆ ํŠนํ™” ๊ณต๊ฐ„์— ๋””์ง€ํ„ธ ๋ทฐํ‹ฐ ๋ฐ•์Šค(Beauty Box)๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ๋””์ง€ํ„ธ ๋ทฐํ‹ฐ ๋ฐ•์Šค์˜ ๊ฐ ๋„ค์ผ๋งˆ๋‹ค RFID[9]๊ฐ€ ๋ถ€์ฐฉ๋˜์–ด ์žˆ์–ด ํ™”๋ฉด ์œ„ ๊ณต๊ฐ„์— ์˜ฌ๋ ค๋†“์œผ๋ฉด ์‚ฌ์ง„ ์† ๋„ค์ผ์˜ ์ƒ‰์ƒ์ด ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ”๋€Œ์–ด ์–ด๋–ค ๋Š๋‚Œ์ธ์ง€๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค(Figure I-14-โ‘ฅ). ๋…ธ์‡ ํ•œ ๋ธŒ๋žœ๋“œ์—์„œ ์ƒˆ๋กœ์šด ๊ณ ๊ฐ๊ตฐ์„ ์ธ์ง€ํ•˜๊ณ  ๋””์ง€ํ„ธ ๋งˆ์ผ€ํŒ…์„ ํฌํ•จํ•œ ๋””์ง€ํ„ธ ํ˜์‹  ์ „๋žต์„ ํ†ตํ•ด ๋ธŒ๋žœ๋“œ์˜ ์—ฐ๋ น์ธต๋„ ๋‚ฎ์ถ”๊ณ  ์‹ ๊ทœ ๊ณ ๊ฐ๋„ ํ™•๋ณดํ•˜๊ฒŒ ๋œ ๋ฒ„๋ฒ„๋ฆฌ๋Š” ํ˜์‹  ์ „๋žต์˜ ์ฒซํ•ด์ธ 2013๋…„ ์ „๋…„ ๋Œ€๋น„ ๋งค์ถœ์ด 17% ์ฆ๊ฐ€ํ•˜์˜€๋‹ค๊ณ  ๋ฐœํ‘œํ•˜๊ธฐ๋„ ํ•˜์˜€๋‹ค. [1] System Integration. SI [2] Business Process Reengineering / Information Strategy Planning [3] ๋ฒ•๋ฅ ์— ์˜๊ฑฐํ•œ ์˜๋ฌด์  ์ˆ˜ํ–‰์ด๋‹ค ๋ณด๋‹ˆ ์‚ฌ์—… ๋Œ€๊ฐ€ ๋‚˜ ์ปจ์„คํŒ… ํ’ˆ์งˆ์˜ ์ด์Šˆ๊ฐ€ ์ œ๊ธฐ๋˜๊ธฐ๋„ ํ•œ๋‹ค [4] www.itsa.or.kr [5] Infrastructure as s Service:IaaS, Platform as a Service:PaaS, Softwareas a Service: SaaS [6] Internet of Thing. [7] ์•ˆ์ ค๋ผ๋Š” ๋ฒ„๋ฐ”๋ฆฌ CEO๋ฅผ ๊ทธ๋งŒ๋‘๊ณ  2016๋…„ ์• ํ”Œ์˜ ๋ฆฌํ…Œ์ผ ์Šคํ† ์–ด ๋ฐ ์˜จ๋ผ์ธ ์Šคํ† ์–ด ์ˆ˜์„ ๋ถ€์‚ฌ์žฅ์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์˜ฎ๊ฒผ๋‹ค. [8] Quick Response. 2์ฐจ์› ๋ฐ”์ฝ”๋“œ [9] Radio Frequency Identification ๊ทน์†Œํ˜•์นฉ์— ์ œํ’ˆ ์ •๋ณด ๋“ฑ์„ ๋‹ด์•„ ์•ˆํ…Œ๋‚˜๋กœ ๋ฌด์„  ์†ก์‹ ํ•˜๋Š” ์žฅ์น˜. ์ „์žํƒœ๊ทธ ๋“ฑ์œผ๋กœ ๋งŽ์ด ์“ฐ์ž„ 03. ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์˜ ์ „์Ÿ ์ตœ๊ทผ ๋‹ค์‹œ ๋ถ์ด ์ผ๊ณ  ์žˆ๋Š” ๋””์ง€ํ„ธ ํ˜๋ช…์— ๋”ฐ๋ฅธ ๊ธฐ์—…๋“ค์˜ ๋ณ€์‹ ์€ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์—๊ฒŒ๋Š” ๋˜ ๋‹ค๋ฅธ ํ˜ธ์žฌ๋ฅผ ์ฃผ๊ณ  ์žˆ๋‹ค. ์‚ฌ์‹ค ๊ตญ๋‚ด์™ธ ์ปจ์„คํŒ… ์‚ฐ์—…์€ 2010๋…„ ์ฆˆ์Œ์„ ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ทธ ์„ฑ์žฅ์ด ์ •์ฒด๋˜๊ฑฐ๋‚˜ ์ผ๋ถ€ ์˜์—ญ์€ ํ•˜๋ฝ์„ธ๋ฅผ ๋„๊ณ  ์žˆ๋‹ค. ๊ธˆ์œต ์œ„๊ธฐ์— ๋”ฐ๋ฅธ ๊ฒฝ์ œ ๋ถˆํ™ฉ์˜ ์—ฌํŒŒ๋„ ์žˆ์—ˆ๊ณ , ๋งŽ์€ ์ปจ์„คํ„ดํŠธ๋“ค์ด ๊ณ ๊ฐ์‚ฌ์— ํ•ฉ๋ฅ˜ํ•˜์—ฌ ์ปจ์„คํŒ… ์—ญ๋Ÿ‰ ์ž์ฒด๊ฐ€ ๊ณ ๊ฐ์—๊ฒŒ ๋งŽ์ด ์ด์ „๋œ ๊นŒ๋‹ญ๋„ ์žˆ์—ˆ๋‹ค. ๋˜ํ•œ, ๊ฒฝ์Ÿ์—์„œ ๋„ํƒœ๋œ ๊ธฐ์—…๋“ค์€ ์ธ์ˆ˜ํ•ฉ๋ณ‘(M&A)์„ ํ†ตํ•ด ๊ทธ ๊ฐ„ํŒ์„ ๋‚ด๋ฆฌ๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์•˜๋‹ค. ํŠนํžˆ, ์ปจ์„คํŒ… ์‚ฌ์—…์€ ๋Œ€ํ‘œ์ ์ธ ์ง€์‹ ์‚ฌ์—…(Knowledge Business)์œผ๋กœ ๋งŽ์€ ์„ค๋น„ ํˆฌ์ž๋‚˜ ์ž์‚ฐ ์—†์ด ์‚ฌ์—…์ด ๊ฐ€๋Šฅํ•˜์—ฌ ์ธ์ˆ˜ํ•ฉ๋ณ‘๋„ ์‰ฝ๊ฒŒ ๋ฐœ์ƒํ•œ๋‹ค. ์ฃผ์‹ํšŒ์‚ฌ๋ณด๋‹ค ์œ ํ•œํšŒ์‚ฌ๋“ค์ด ๋งŽ์œผ๋ฉฐ ์ปจ์„คํ„ดํŠธ๋“ค์ด ์Šน์ง„์„ ๊ฑฐ๋“ญํ•˜๋‹ค ํŒŒํŠธ๋„ˆ(Partner)๊ฐ€ ๋˜๋ฉด ํšŒ์‚ฌ์˜ ์ง€๋ถ„์„ ๊ธฐ์กด์˜ ํŒŒํŠธ๋„ˆ๋“ค๊ณผ ๋‚˜๋ˆ ๊ฐ€์ง€๋ฉด์„œ ์ฑ…์ž„๊ณผ ๊ถŒํ•œ์ด ๋”์šฑ ๊ฐ•ํ•ด์ง„๋‹ค. ์ด๋Ÿฐ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค๋„ 2000๋…„๋Œ€ ๋“ค์–ด ๋ช‡ ๊ฐ€์ง€ ํŠน์ง•์„ ๋ณด์ด๋ฉด์„œ ๋ณ€ํ™”์— ๋ณ€ํ™”๋ฅผ ๊ฑฐ๋“ญํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ๊ทธ ์‚ฌ์ •์„ ์ข€ ๋“ค์—ฌ๋‹ค๋ณด์ž. 3.1 ์„ธ๊ณ„ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์˜ ํŠน์ง• ์ „ ์„ธ๊ณ„๋ฅผ ๋ฌด๋Œ€๋กœ ๊ฐ์ถ•์„ ๋ฒŒ์ด๊ณ  ์žˆ์œผ๋ฉฐ ๋์—†์ด ๋ณ€์‹  ์ค‘์ธ ์†Œ์œ„ ๋งํ•˜๋Š” '์Ÿ์Ÿํ•œ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค'์—๊ฒ ๊ณตํ†ต๋œ ํŠน์ง•๋“ค์ด ์žˆ๋‹ค. ์ด ํŠน์ง•๋“ค์€ 20์„ธ๊ธฐ๋ฅผ ์ง€๋‚˜๋ฉด์„œ ๊ธ‰๊ฒฉํ•˜๊ฒŒ ๋ณ€ํ•œ ์‚ฌ์—… ํ™˜๊ฒฝ์„ ๋ฐ˜์˜ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ํฐ ์š”์ธ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ธฐ์—…๋“ค๋„ ๊ถ๊ทน์ ์œผ๋กœ ์ƒ์กด์„ ์œ„ํ•ด ๊ณผ๊ฐํ•œ ๋ณ€์‹ ์„ ํƒํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์„ธ๊ณ„ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์˜ ์ฃผ์š” ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํšŒ๊ณ„ ๋ถ€๋ฌธ๊ณผ ์ปจ์„คํŒ… ๋ถ€๋ฌธ์ด ๋ถ„๋ฆฌ๋จ ์ปจ์„คํŒ… ์˜์—ญ ๊ฐ„์˜ ๊ตฌ๋ถ„์ด ํฌ๋ฏธํ•ด์ง ์‚ฌ๊ณ  ๋ฆฌ๋”์‹ญ(Thought Leadership) ๊ฐ•ํ™”์— ์ง‘์ค‘ํ•จ ์ฒซ ๋ฒˆ์งธ, ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ํšŒ๊ณ„ ๋ถ€๋ฌธ๊ณผ ์ปจ์„คํŒ… ๋ถ€๋ฌธ์˜ ๋ถ„๋ฆฌ๋Š” 2001๋…„ ๋Œ€๊ทœ๋ชจ ํšŒ๊ณ„ ๋ถ€์ •์œผ๋กœ ์„ธ์ƒ์„ ๋†€๋ผ๊ฒŒ ์—”๋ก  ์‚ฌํƒœ(Enron Scandal)[1]๊ฐ€ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•˜์˜€๋‹ค. ์ด๋ฅผ ๊ณ„๊ธฐ๋กœ ๋งŽ์€ ์ปจ์„คํŒ… ํšŒ์‚ฌ์˜ ํšŒ๊ณ„ ๋ถ€๋ฌธ๊ณผ ์ปจ์„คํŒ… ๋ถ€๋ฌธ์ด ๋ถ„๋ฆฌํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, PwC์˜ ๊ฒฝ์šฐ ์ปจ์„คํŒ… ๋ถ€๋ฌธ์ด IBM์— 35์–ต ๋‹ฌ๋Ÿฌ์— ๋งค๊ฐ๋˜์–ด ํ˜„์žฌ IBM Global Business Service๋กœ ์ด์–ด์ง€๊ณ  ์žˆ์œผ๋ฉฐ, KPMG๋Š” Bearingpoint๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ปจ์„คํŒ… ๋ถ€๋ฌธ์ด ๋…๋ฆฝํ•˜์˜€๋‹ค. Ernst & Young์€ Cap Gemini์™€ ํ•จ๊ป˜ ์ปจ์„คํŒ… ๋ถ€๋ฌธ์„ ํ•ฉ๋ณ‘ํ•˜์—ฌ CGE&Y๋กœ ์šด์˜๋˜๋‹ค๊ฐ€ ๋‹ค์‹œ ๋ณธ๋ž˜์˜ ํšŒ๊ณ„ ๋ถ€๋ฌธ๊ณผ ๊ฐ๊ฐ ํ•ฉ๋ณ‘๋˜์—ˆ๋‹ค. Deloitte๋„ ํšŒ๊ณ„์™€ ์ปจ์„คํŒ… ๋ถ€๋ฌธ์„ ๋ถ„๋ฆฌ๋ฅผ ๊ณ ๋ฏผํ•˜๋‹ค 2013๋…„ 1์›” ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…์ธ ๋ชจ๋‹ˆํ„ฐ๊ทธ๋ฃน์„ ํ•ฉ๋ณ‘ํ•˜๊ธฐ๋„ ํ•˜์˜€๋‹ค. IT ์ปจ์„คํŒ… ์—…์ฒด์ธ Anderson Consulting์€ Accenture๋กœ ์‚ฌ๋ช…์„ ๋ฐ”๊พผ ํ›„ IT ๊ตฌ์ถ• ๋ฐ ์šด์˜ ์‚ฌ์—…๊นŒ์ง€ ์˜์—ญ์„ ํ™•์žฅํ•˜์—ฌ ์ „ ์„ธ๊ณ„ IT ์„œ๋น„์Šค 1์œ„ ๊ธฐ์—…์ด ๋˜์—ˆ๋‹ค.[2] ์ด๋“ค์— ๋น„ํ•ด ์‚ฌ์—… ๊ทœ๋ชจ๊ฐ€ ์ž‘์€ ์ค‘์†Œํ˜• ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์„œ๋กœ ํ•ฉ์ณ์ง€๊ฑฐ๋‚˜ IBM, EDS[3]๋“ฑ IT ์„œ๋น„์Šค ๊ธฐ์—…๋“ค์— ํ•ฉ๋ณ‘๋˜์—ˆ๋‹ค. ์ด๋Ÿฐ ํ•ฉ์ข…์—ฐํšก(ๅˆๅพžๆฉซ)์€ ๊ฐ ๊ธฐ์—…๋“ค ์ž…์žฅ์—์„œ ์ƒ์กด์„ ์œ„ํ•ด ๋˜๋Š” ์‹ ๊ทœ ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค(Business Portfolio)๋ฅผ ๊ตฌ์ถ•ํ•œ๋‹ค๋Š” ์ธก๋ฉด์—์„œ ์•„์ฃผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ง„ํ–‰๋˜์—ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ํŠน์ง•์€ ์ปจ์„คํŒ… ์˜์—ญ ๊ฐ„์˜ ๊ตฌ๋ถ„์ด ํฌ๋ฏธํ•ด์กŒ๋‹ค. 2013๋…„ ์ด์ฝ”๋…ธ๋ฏธ์ŠคํŠธ(Economist) ์ง€์— ๊ธฐ๊ณ ๋œ ๊ธฐ์‚ฌ[4]๋Š” ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์ด ์‚ฌ์—… ์˜์—ญ์— ๋Œ€ํ•ด ์ž„ํ•˜๋Š” ์ž์„ธ์˜ ๋ณ€ํ™”๋ฅผ ๊ทน๋ช…ํ•˜๊ฒŒ ๋ณด์—ฌ์ค€๋‹ค. Fig I-15. ๊ตญ๋‚ด์™ธ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค ํ•ด๋‹น ๊ธฐ์‚ฌ๋Š” ๊ธ€๋กœ๋ฒŒ Top 3 ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค๊ณผ ๊ธ€๋กœ๋ฒŒ Big 4 ํšŒ๊ณ„๋ฒ•์ธ ๊ฐ„์˜ ์ „์Ÿ์„ ๊ธฐ์ˆ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์ปจ์„คํŒ…, ํšŒ๊ณ„, ๋ฒ•, ์žฌ๋ฌด ์‹ฌ์ง€์–ด ๊ด‘๊ณ ๊นŒ์ง€ ํŠน์ • ์˜์—ญ์—์„œ ์ „๋ฌธ์ ์ธ ์—ญ๋Ÿ‰์„ ๋ณด์œ ํ•œ ํšŒ์‚ฌ๋“ค์ด ์ปจ๋ฒ„์ „์Šค(convergence) ์‹œ๋Œ€์— ๋” ์ด์ƒ ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ๋Š” ์ƒ์กดํ•  ์ˆ˜ ์—†์Œ์„ ์•Œ๊ณ  ์ƒ๋Œ€๋ฐฉ ์˜์—ญ์œผ๋กœ ์ง„์ž…ํ•˜๊ธฐ ์‹œ์ž‘ํ•œ ๊ฒƒ์ด๋‹ค. ์‹œ๋Œ€์ •์‹ (Zeitgeist)์ด ํ†ตํ•ฉ์ด์š” ๋ณตํ•ฉ์ด๊ณ , ์ž…์ฒด์ ์ด๋ฉฐ ํ†ต์„ญ(consilience)์„ ํ•„์š”๋กœ ํ•˜๋Š” ์ƒํ™ฉ์—์„œ ๋‹น์—ฐํ•œ ์ผ์ธ์ง€๋„ ๋ชจ๋ฅธ๋‹ค. ์ด์ฝ”๋…ธ๋ฏธ์ŠคํŠธ์˜ ๊ธฐ์‚ฌ๋ฅผ ์ธ์šฉํ•ด ๋ณด๋ฉด ์šฐ์„  Top 3 ์ „๋žต ์ปจ์„คํŒ… ํšŒ์‚ฌ๋“ค์€ ์ƒํ˜ธ ๊ฐ„์— ๋ฒค์น˜๋งˆํ‚น(benchmarking) ํ•˜์˜€๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์‹ ๊ทœ ์‚ฌ์—…์„ ์ˆ˜์ฃผํ•˜๊ธฐ ์œ„ํ•ด ํ•™๋งฅ์ด๋‚˜ ํ‡ด์ง์ž ๋„คํŠธ์›Œํฌ ๋“ฑ ๋™๋ฌธ(alumni) ๋„คํŠธ์›Œํฌ๋ฅผ ํ™œ์šฉํ–ˆ๋Š”๋ฐ ์‚ฌ์—… ์ˆ˜์ฃผ์— ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋งฅํ‚จ์ง€[5]๊ฐ€ ์œ ๋ช…ํ–ˆ๋‹ค. ๋˜, ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด ์ผ๋ถ€ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ ์„ฑ๊ณต ๋ณด์ˆ˜๋ฅผ ์ง€๊ธ‰ํ•˜์˜€๋Š”๋ฐ ์ด ๋ฐฉ๋ฒ•์€ ๋ฒ ์ธ[6]์ด ์‚ฌ์šฉํ•˜๋˜ ๋ฐฉ๋ฒ•์ด์—ˆ๊ณ , B2B ์‚ฌ์—…์„ ํ•จ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ํ˜„์žฌ ์„ธ ๊ณณ ๋ชจ๋‘ ์ž์‹ ๋“ค์˜ ์•„์ด๋””์–ด๋ฅผ ์ผ๋ฐ˜ ๋Œ€์ค‘์—๊ฒŒ ์•Œ๋ฆฌ๋Š” ๊ฒƒ์— ๋งค์šฐ ์ ๊ทน์ ์ธ๋ฐ ์ด๋ฅผ ์ฒ˜์Œ ์‹œ์ž‘ํ•œ ๊ณณ์€ BCG[7]์˜€๋‹ค. 1990๋…„๋Œ€๋งŒ ํ•ด๋„ 6~18๊ฐœ์›” ๋™์•ˆ ์ˆ˜ํ–‰ํ•˜๋˜ ์ปจ์„คํŒ…์„ ๊ฒฝ์ œ ๋ถˆํ™ฉ์œผ๋กœ ์ธํ•ด ์˜ˆ์‚ฐ๋„ ์ค„๊ฒŒ ๋˜๊ณ  ์ตœ๊ทผ์—๋Š” 3๊ฐœ์›” ์ดํ•˜์˜ ํ”„๋กœ์ ํŠธ๋„ ๋‹ค์ˆ˜ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ๋‹ค. ์ „๋žต ์ปจ์„คํŒ…์˜ ๊ฒฐ๊ณผ๋ฌผ๋„ CEO์˜ ์ƒ๊ฐ์„ ์ง€์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๋ณด๊ณ ์„œ๋ฅผ ๋„˜์–ด์„œ์„œ โ€˜์‹คํ–‰โ€™๊ณผ ๊ด€๋ จ๋˜๊ณ  ์ปจ์„คํ„ดํŠธ๋“ค๋„ ์ž๊ธฐ๊ฐ€ ์ œ์•ˆํ•œ ํ˜์‹ ์ด๋‚˜ ๊ฐœ์„  ๋“ฑ์˜ ์ผ์„ ์ง์ ‘ ๋งก๊ฒŒ ๋˜๋‹ค ๋ณด๋‹ˆ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ผ์˜ ์„ฑ๊ฒฉ์ด ์‹คํ–‰-์šด์˜ ์ปจ์„คํŒ… ์˜์—ญ์œผ๋กœ ์˜ฎ๊ฒจ๊ฐ€๊ฒŒ ๋˜์—ˆ๋‹ค. ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ฏธ๋“œ ํ‹ฐ์–ด(mid-tier)์—์„œ ์ „๋žต ์ปจ์„คํŒ… ์˜์—ญ์œผ๋กœ ์˜ฌ๋ผ์˜ค๋˜ ๊ฒฝ์Ÿ์ž๋“ค์„ ๋งŒ๋‚˜๊ฒŒ ๋˜์—ˆ๊ณ  ๊ทธ๋“ค์€ PwC, E&Y, Deloitte, KPMG์™€ ๊ฐ™์€ ๋Œ€ํ˜• ํšŒ๊ณ„๋ฒ•์ธ๋“ค์ด์—ˆ๋‹ค.[8] ์ด๋Ÿฐ ๋Œ€ํ˜• ํšŒ๊ณ„๋ฒ•์ธ๋“ค์€ ์ตœ๊ทผ์—๋Š” ๋งฅํ‚จ์ง€ ๊ฐ™์€ ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค๋ณด๋‹ค ๋” ๋งŽ์€ ์ปจ์„คํŒ…์„ ํ•˜๊ณ  ์žˆ๋‹ค. ๊ธฐ์ˆ ์ด๋‚˜ ์‹œ์Šคํ…œ ํ†ตํ•ฉ ์ปจ์„คํŒ…๋“ค๋„ ๋งŽ์•„์„œ ์‚ฌ๋žŒ๋“ค์ด ๋” ๋งŽ์ด ํ•„์š”ํ–ˆ๊ณ  ์ด๋Š” ์‚ฌ์„ธ ํ™•์žฅ์œผ๋กœ ์ด์–ด์กŒ๋‹ค. ๊ทธ๋ ‡๋‹ค๊ณ  ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์ด ์œ„์ถ•๋œ ๊ฒƒ์€ ์•„๋‹ˆ์—ˆ๋‹ค. ๊ทธ๋“ค๋„ ์˜์—ญ ํ™•์žฅ์— ๋”ฐ๋ผ ๊พธ์ค€ํžˆ ๋‘ ์ž๋ฆฟ์ˆ˜ ์„ฑ์žฅ๋ฅ ์„ ์ด์–ด ๋‚˜๊ฐ”๋‹ค. ํ•œํŽธ, IT ์˜์—ญ์— ์žˆ๋˜ Accenture๋‚˜ IBM[9]์€ ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ํ™•์žฅํ•˜๋ฉด์„œ ์˜คํžˆ๋ ค ๊ธฐ์ˆ ์—์„œ ๊ฒฝ์˜ ์ชฝ์œผ๋กœ ์ปจ์„คํŒ… ์˜์—ญ์„ ์˜ฎ๊ฒจ๊ฐ„ ๊ฒฝ์šฐ์ด๋‹ค. IT ์˜์—ญ์„ ํ•„๋‘๋กœ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค ๊ณต๊ธ‰์—…์ฒด๋“ค์ด ์ปจ์„คํŒ… ๊ธฐ์—…์„ ๋Œ€์‹ ํ•˜์—ฌ ์ปจ์„คํŒ… ์—…๋ฌด๋ฅผ ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„์กŒ๋‹ค. ์‹ฌ์ง€์–ด Accenture๋Š” ์ปจ์„คํŒ…์—์„œ ์ถœ๋ฐœํ•˜์—ฌ IT ์‹œ์Šคํ…œ ๊ตฌ์ถ• ๋ฐ ์•„์›ƒ์†Œ์‹ฑ๊นŒ์ง€ ์„œ๋น„์Šค๋ฅผ ํ™•๋Œ€ํ•˜์—ฌ ํ˜„์žฌ IT ์„œ๋น„์Šค ๊ธฐ์—…์˜ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์žฅ๋น„ ํšŒ์‚ฌ๋กœ ์ถœ๋ฐœํ•œ IBM์€ IBM GBS ์šด์˜์„ ํ†ตํ•ด ์„œ๋น„์Šค ๋ถ€๋ฌธ์˜ ๋งค์ถœ์„ ์ „์ฒด ๋งค์ถœ์˜ 50% ์ˆ˜์ค€์— ์ด๋ฅด๊ฒŒ ํ•˜์˜€๋‹ค. ๋ณต์žกํ•œ ๊ธฐ๊ณ„์˜ ์„ค์น˜๋‚˜ ์กฐ์ž‘, ๋…ธํ•˜์šฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ, ๊ณต๊ธ‰์—…์ฒด๊ฐ€ ์ปจ์„คํŒ…์„ ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์€ B2B ์„ค๋ฃจ์…˜ ์‚ฌ์—…์ด๋‚˜ B2B ์˜์—… ์ธก๋ฉด์—์„œ ์ผ์ข…์˜ ๊ต์ฐฉ ํšจ๊ณผ(Lock-in Effect)[10]๋ฅผ ์œ ๋ฐœํ•˜๋ฉด์„œ ๊ณ ๊ฐ ๊ด€๊ณ„๋ฅผ ์ง€์†์‹œํ‚ค๊ณ  ๊ฐ•ํ™”ํ•˜๋Š” ํšจ๊ณผ๋ฅผ ๊ฐ€์ ธ์™”๋‹ค. ๋งˆ์ง€๋ง‰ ์„ธ ๋ฒˆ์งธ ํŠน์ง•์œผ๋กœ ์ปจ์„คํŒ… ์‚ฐ์—…์€ ์ง€์‹ ์‚ฐ์—…์ด๋‹ค ๋ณด๋‹ˆ ์ƒˆ๋กœ์šด ์ง€์‹ ๊ฐœ๋ฐœ์„ ํ†ตํ•ด ์‚ฌ๊ณ  ๋ฆฌ๋”์‹ญ(Thought Leadership) ํ™•๋ณด์— ๋งŽ์€ ํˆฌ์ž๋ฅผ ํ•œ๋‹ค. ๋งŽ์€ ์ปจ์„คํŒ… ํšŒ์‚ฌ๋“ค์ด ์ด๋ฅผ ์œ„ํ•ด ํ™”์ดํŠธ ํŽ˜์ดํผ(white paper)์™€ ๊ฐ™์€ ๋ณด๊ณ ์„œ, ์ €์„œ ์ถœ๊ฐ„, ์ฝ˜ํผ๋Ÿฐ์Šค(conference)์— ํฐ ํˆฌ์ž๋ฅผ ํ•˜๊ณ  ์žˆ๋‹ค. ๋งฅํ‚จ์ง€์˜ ๊ฒฝ์šฐ, ๋…์ž์  ๋ฐ์ดํ„ฐ ํ™•๋ณด ๋ฐ ์ง€์‹ ๊ฐœ๋ฐœ์„ ์œ„ํ•ด ์—ฐ๊ฐ„ ์•ฝ 4,500์–ต ์›์„ ํˆฌ์žํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ์ด์™€ ๊ฐ™์ด ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์˜ ์˜คํผ๋ง(Offering) ๋ณ€ํ™”๋Š” ๊ธฐ์กด์˜ ์ปจ์„คํŒ… ๊ธฐ์—…๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ œ์กฐ์—…์ฒด์™€ ๊ธฐ์กด ์„œ๋น„์Šค ์—…์ฒด์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ๋˜ํ•œ ๋ฐ”๊ฟ”๋†“๊ณ  ์žˆ๋‹ค. 3.2 ๊ตญ๋‚ด ์ปจ์„คํŒ… ๊ธฐ์—… ํ˜„ํ™ฉ ๊ตญ๋‚ด ์ปจ์„คํŒ… ์‹œ์žฅ์€ ๋Œ€๊ธฐ์—…์„ ์ค‘์‹ฌ์œผ๋กœ ์›€์ง์ด๊ณ  ์žˆ๋‹ค. ์‚ผ์„ฑ์ „์ž๋‚˜ LG์ „์ž์™€ ๊ฐ™์€ ์ผ๋ถ€ ๋Œ€๊ธฐ์—…๋“ค์˜ ๊ฒฝ์šฐ ๊ธฐ์—…์˜ ๊ทœ๋ชจ๋‚˜ ํ”„๋กœ์„ธ์Šค์˜ ๋ณต์žก์„ฑ ์ธก๋ฉด์—์„œ ์ด๋ฏธ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…์˜ ๋ฐ˜์—ด์— ์˜ฌ๋ž๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. Top 3 ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ๋‹น์—ฐํžˆ ์ด๋Ÿฐ ๊ณ ๊ฐ๋“ค์„ ํ•ต์‹ฌ ํƒ€๊นƒ์œผ๋กœ ํ•˜๊ณ  ์žˆ๋Š” ๋ฐ˜๋ฉด, ๊ธˆ์œต ๊ธฐ๊ด€์ด๋‚˜ ์ค‘๊ฒฌ ๊ธฐ์—…๊ตฐ์€ Big 4 ํšŒ๊ณ„ ๋ฒ•์ธ๊ณผ ๋ผ์ด์„ ์‹ฑ ๋œ ๊ตญ๋‚ด ํšŒ๊ณ„๋ฒ•์ธ๋“ค์ด ์ปจ์„คํŒ…์„ ๋งŽ์ด ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ๊ทธ ๋‚ด์šฉ์€ ๊ธˆ์œต ๊ทœ์ œ(regulation)๋‚˜ ์ปดํ”Œ๋ผ์ด์–ธ์Šค(compliance) ๊ด€๋ จ ์ปจ์„คํŒ…์ด ๋Œ€๋ถ€๋ถ„์ด๋‹ค. ๊ตญ๋‚ด IT ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ๋Š” ์ข€ ํŠน์ดํ•œ๋ฐ ๋Œ€๊ธฐ์—… ๊ทธ๋ฃน์˜ ๊ฒฝ์šฐ, ๊ณ„์—ด์‚ฌ ์ค‘ IT ์„œ๋น„์Šค๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ์‚ผ์„ฑSDS๋‚˜ LG CNS์™€ ๊ฐ™์€ ํšŒ์‚ฌ๋“ค์ด ์žˆ์–ด์„œ ๊ฐ ๊ทธ๋ฃน ๊ณ„์—ด์‚ฌ์˜ IT ์ปจ์„คํŒ… ๋Œ€๋ถ€๋ถ„์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. LG์˜ ๊ฒฝ์šฐ, LG CNS์˜ ์ปจ์„คํŒ… ๋ถ€๋ฌธ์ธ ์—”ํŠธ๋ฃจ ์ปจ์„คํŒ…(Entrue consulting)[11]์ด ๊ทธ ์—ญํ• ์„ ๋งก๊ณ  ์žˆ์œผ๋ฉฐ, ์‚ผ์„ฑ์˜ ๊ฒฝ์šฐ, โ€˜์˜คํ”ˆํƒ€์ด๋“œ(Opentide)โ€™๋ผ๋Š” ์‚ผ์„ฑSDS ์ปจ์„คํŒ… ์žํšŒ์‚ฌ๊ฐ€ ์žˆ์—ˆ์œผ๋‚˜, ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์กฐ์ •ํ•˜๋ฉด์„œ ์—ญ์‹œ ์‚ผ์„ฑSDS์˜ ์žํšŒ์‚ฌ์ธ IT ์—”์ง€๋‹ˆ์–ด๋ง ๊ธฐ์—… ๋ฏธ๋ผ์ฝค ์•„์ด์•ค์”จ์— 2015๋…„ ํ•ฉ๋ณ‘๋˜์—ˆ๋‹ค. ๋ฏผ๊ฐ„ ๋ถ€๋ฌธ์ด ์•„๋‹Œ ๊ณต๊ณต ๋ถ€๋ฌธ IT ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ, 2008๋…„๊นŒ์ง€๋งŒ ํ•ด๋„ ๋Œ€๊ธฐ์—… ์ปจ์„คํŒ… ํšŒ์‚ฌ๋“ค์ด BPR/ISP๋ฅผ ๋งŽ์ด ์ˆ˜์ฃผํ•˜์˜€์œผ๋‚˜ ๊ณต๊ณต IT ์‚ฌ์—…์˜ ๋Œ€๊ธฐ์—… ์ฐธ์—ฌ ๊ธˆ์ง€ ์กฐํ•ญ์ด ๋ณธ๊ฒฉ์ ์œผ๋กœ ์ ์šฉ๋œ ์ดํ›„๋ถ€ํ„ฐ๋Š” ๋ณธ ๊ตฌ์ถ• ์‚ฌ์—… ์ฐธ์—ฌ์— ์ œ์•ฝ์ด ์ƒ๊ธฐ๋ฉด์„œ ๋Œ€๊ธฐ์—… ์ปจ์„คํŒ… ํšŒ์‚ฌ๋“ค์˜ ๊ณต๊ณต IT ์ปจ์„คํŒ… ์ฐธ๊ฐ€๋Š” ๊ฑฐ์˜ ์–ด๋ ต๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ ์™ธ ์ค‘์†Œ ๊ธฐ์—…๊ตฐ์˜ ๊ฒฝ์šฐ, ์™ธ๊ตญ๊ณ„ ์ปจ์„คํŒ…์ด๋‚˜ ํšŒ๊ณ„๋ฒ•์ธ๋“ค๊ณผ ๋”๋ถˆ์–ด ๋„ค๋ชจ ํŒŒํŠธ๋„ˆ์Šค[12] ๊ฐ™์€ ๋กœ์ปฌ ๊ธฐ์—…๋“ค๋„ ์„ ์ „ํ•˜๊ณ  ์žˆ๋‹ค. ๋˜ํ•œ, ํ•œ๊ตญ์ƒ์‚ฐ์„ฑ๋ณธ๋ถ€๋‚˜ ํ•œ๊ตญ๋Šฅ๋ฅ ํ˜‘ํšŒ[13] ๊ฐ™์€ ๊ณณ๋„ ๋‹ค์–‘ํ•œ ์ปจ์„คํŒ…์„ ์ˆ˜ํ–‰ ์ค‘์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ „๋ฐ˜์ ์œผ๋กœ ๊ตญ๋‚ด ์ปจ์„คํŒ… ์‹œ์žฅ์€ ๋งŽ์ด ์œ„์ถ•๋˜์—ˆ๋‹ค. ์ด๋Š” ์ „ํ†ต์ ์ธ ๋ฐฉ์‹์˜ ์ปจ์„คํŒ… ์˜คํผ๋ง(offering)์ด ํ•œ๊ณ„์— ๋‹ฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ ์•ž์„œ ์„ค๋ช…ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ๋‹จ์ˆœํžˆ ๋ฐฉํ–ฅ์ด๋‚˜ ๊ณผ์ œ๋ฅผ ์ œ์‹œํ•˜๊ณ  ๋๋‚˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ณผ์ œ ์ค‘ ์ผ๋ถ€๋ฅผ ์ปจ์„คํŒ… ๊ธฐ์—…์ด ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ๊ทธ ์„ฑ๊ณผ๋ฅผ ์ž…์ฆํ•ด์•ผ ํ•˜๋Š” ์‚ฌ๋ก€๊ฐ€ ๋งŽ์•„์ง์œผ๋กœ์จ ๋‹จ์ˆœํžˆ ๋ณด๊ณ ์„œ๋ฅผ ๋‚ด๋Š” ๊ฒƒ์œผ๋กœ ๊ทธ์น˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ์—ฐ๊ณ„ํ•˜๋Š” ์ปจ์„คํŒ…์„ ๋งŽ์ด ์š”๊ตฌํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๋Ÿฐ ์ด์œ ๋กœ IT ์„ค๋ฃจ์…˜ ๊ธฐ์—…๋“ค์€ ์ปจ์„คํ„ดํŠธ๋“ค์ด ์„ค๋ฃจ์…˜ ๊ตฌ์ถ•์— ์ง์ ‘ ์ฐธ์—ฌํ•˜๊ธฐ๋„ ํ•˜๊ณ , ์–ด๋–ค ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ์ปจ์„คํŒ… ๊ฒฐ๊ณผ๋ฅผ ์ธ์ฆ(certification) ์ฒด๊ณ„์™€ ์—ฐ๊ณ„ํ•˜์˜€๋‹ค. ํŠนํžˆ, ์ œ์กฐ๋‚˜ ์œ ํ†ต ๊ธฐ์—…๋“ค์˜ ํŠน์ • ์—…๋ฌด ํ”„๋กœ์„ธ์Šค๋“ค์˜ ๊ฒฝ์šฐ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค ๊ฒฝ์Ÿ๋ ฅ์„ ๊ฐ•ํ™”ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๊ธ€๋กœ๋ฒŒ ํ‘œ์ค€์˜ ๋„์ž… ์š”๊ตฌ๊ฐ€ ๋งŽ์ด ์ œ๊ธฐ๋˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ปจ์„คํŒ…ํ•˜๊ณ  ์ค€์ˆ˜ํ•  ๊ฒฝ์šฐ, ์ธ์ฆ์„ ์ˆ˜์—ฌํ•˜๋Š” ๊ฒƒ์„ ์‚ฌ์—… ๋ชจ๋ธ๋กœ ์‚ผ๊ณ  ์žˆ๋Š” ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค๋„ ๋งŽ์ด ์ƒ๊ฒจ๋‚˜๊ณ  ์žˆ๋‹ค. ์ด๋Š” ๊ณผ๊ฑฐ์— ์ธ๊ฑด๋น„ ์œ„์ฃผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋˜ ์ปจ์„คํŒ… ์‚ฌ์—…์˜ ์›๊ฐ€ ๋ชจ๋ธ์— ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋Š˜๋‚  ๊ณ ๊ฐ๋“ค์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ์‹ ๊ทœ ์˜คํผ๋ง์„ ๊ฐœ๋ฐœํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์˜ ์ƒ์กด๊ณผ ์ง๊ฒฐ๋˜๊ณ  ์žˆ๋‹ค. ๋˜, ์ปจ์„คํŒ… ์—ญ๋Ÿ‰์„ ์ ๊ทน ํ™œ์šฉํ•ด์„œ ๊ณ ๊ฐ์„ ๋ฆฌ๋”ฉ ํ•ด์•ผ ํ•˜๋Š” B2B ๊ธฐ์—… ์ž…์žฅ์—์„œ๋„ ์ปจ์„คํŒ… ๊ธฐ์—… ๋˜๋Š” ์ปจ์„คํŒ… ์—ญ๋Ÿ‰์€ ๋†“์น  ์ˆ˜ ์—†๋Š” ์นด๋“œ์ด๋‹ค. ์ด์™€ ๊ฐ™์€ ์ด์œ ๋กœ ๋งŽ์€ B2B ๊ธฐ์—…๋“ค์ด ์˜์—… ๋ฐฉ์‹์— ์žˆ์–ด ์ปจ์„คํŒ…์„ ํ™œ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์‹œ๋„๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ๋‹น์—ฐํ•˜๋‹ค. 3.3 ์ปจ์„คํŒ…, High-level Sales ์ปจ์„คํŒ… ์‚ฌ์—…์€ B2B ์‚ฌ์—…์ด๊ณ  ์ˆ˜์ฃผ ์‚ฌ์—…์ด๊ธฐ์— ์ž…์ฐฐ(bidding)์„ ํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ์‹œ๋‹ˆ์–ด(senior) ์ปจ์„คํ„ดํŠธ๋“ค์€ ์‚ฌ์—… ์ˆ˜์ฃผ๋ฅผ ์œ„ํ•ด B2B ์˜์—…๋Œ€ํ‘œ์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ๋ฌผ๋ก , B2B ๊ธฐ์—…์˜ ์˜์—…๋Œ€ํ‘œ(Sales Rep.)์™€ ๊ทธ ์—ญํ• ์ด 100% ๊ฐ™์ง€๋Š” ์•Š์ง€๋งŒ ์ตœ๊ทผ B2B ์˜์—…์˜ ๋ณ€ํ™”์ƒ์„ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์–ด๋Š ์ •๋„ ๊ต์ง‘ํ•ฉ์ด ๋ฐœ์ƒํ•˜๊ณ  ์žˆ๋‹ค. ์ „ํ†ต์ ์œผ๋กœ๋Š” ์ปจ์„คํŒ… ์˜์—…์€ ์ž…์ฐฐ ์—†์ด ์ˆ˜์˜๊ณ„์•ฝํ•˜๋Š” ๊ฒƒ์„ ์ตœ๊ณ ์˜ ์˜์—… ํ™œ๋™์œผ๋กœ ์ธ์ •๋ฐ›์•˜๋‹ค. ๊ทธ๊ฒƒ๋„ ๊ทธ๋Ÿด ๊ฒƒ์ด ๊ธฐ์—… ๊ฒฝ์˜์— ๋Œ€ํ•œ ์กฐ์–ธ์„ ํ•˜๋Š” ์ž…์žฅ์ด๋‹ˆ ์ปจ์„คํŒ…์ด๋ผ๋Š” ์ผ์€ ์‹œ์žฅ์ด๋‚˜ ํ•™๊ณ„์—์„œ ๋ช…์„ฑ์ด ์žˆ๋Š” ์ „๋ฌธ๊ฐ€ ์ง‘๋‹จ์„ ์ดˆ๋น™ํ•œ๋‹ค๋Š” ์„ฑ๊ฒฉ์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ์ƒ๊ฐ์ด์—ˆ๋‹ค. ์‹ค์ œ๋กœ 2000๋…„๋Œ€ ์ดˆ๋ฐ˜๊นŒ์ง€๋งŒ ํ•ด๋„ ์™ธ๊ตญ๊ณ„ ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ์กฐ์–ธ์€ ๋งค์šฐ ์˜ํ–ฅ๋ ฅ์ด ์žˆ์–ด์„œ ์ด์•ผ๊ธฐ๋ฅผ ๋“ฃ๊ณ ์ž ์ดˆ๋น™ํ•˜๋ฉด ๋ช‡ ์žฅ ์•ˆ๋˜๋Š” ํŒŒ์›Œํฌ์ธํŠธ ์ž๋ฃŒ๋งŒ์œผ๋กœ๋„ ์ˆ˜ ์‹ญ์–ต ์›์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์‰ฝ๊ฒŒ ๊ณ„์•ฝํ•˜๋˜ ์‹œ์ ˆ๋„ ์žˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ปจ์„คํŒ…์ด ๋ณดํŽธํ™”๋˜๊ณ  ์ˆ˜์ฃผ ๊ฒฝ์Ÿ์ด ์น˜์—ดํ•ด์ง€๋ฉด์„œ ํด๋ผ์ด์–ธํŠธ๋“ค์€ ๋” ์ด์ƒ ์ดˆ๋น™์˜<NAME>์„ ๊ฐ€์ง€๊ธฐ๋ณด๋‹ค๋Š” ๋ณต์ˆ˜๊ฐœ์˜ ์ปจ์„คํŒ… ์—…์ฒด๋ฅผ ๋†“๊ณ  ์„ ํƒํ•˜๊ณ  ์‹ถ์–ด ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ํŠนํžˆ, ๊ตญ๋‚ด์—์„œ๋Š” 2000๋…„๋Œ€ ์ดˆ๋ฐ˜ ์™ธ๊ตญ๊ณ„ ์ปจ์„คํŒ… ๊ธฐ์—…์ธ A์‚ฌ๊ฐ€ ๊ณ ๊ฐ ๊ธฐ์—…์˜ ํ•ต์‹ฌ ๋น„๋ฐ€์ธ ๋‚ด์šฉ์„ ์šฐ์ˆ˜ ์‚ฌ๋ก€ ๋ฐœ๊ตด ๋ฐ ๊ณต์œ ๋ผ๋Š” ๋ช…๋ชฉ์œผ๋กœ ๊ณ ๊ฐ ๊ธฐ์—…์˜ ๊ฒฝ์Ÿ์‚ฌ์—์„œ ๋ฒ ์ŠคํŠธ ํ”„๋ž™ํ‹ฐ์Šค(Best Practice)[14]๋กœ ๋ฐ์ดํ„ฐ์™€ ์ž๋ฃŒ๋ฅผ ํ™œ์šฉํ•œ ์ผ์ด ๋ฐœ์ƒํ•˜๋ฉด์„œ ์ƒ๋„๋• ๋“ฑ ์œค๋ฆฌ ๋ฌธ์ œ๊ฐ€ ์ œ๊ธฐ๋˜์—ˆ๊ณ , ๋Œ€๊ธฐ์—…์„ ์ค‘์‹ฌ์œผ๋กœ ๋‚ด๋ถ€์ ์œผ๋กœ ์ปจ์„คํŒ… ๊ทธ๋ฃน์„ ์œก์„ฑํ•˜๊ฑฐ๋‚˜ ์ปจ์„คํŒ… ์—ญ๋Ÿ‰์„ ํ™•๋ณดํ•˜๊ณ ์ž ํ•˜๋Š” ๋…ธ๋ ฅ์ด ๋”์šฑ ๊ฐ•ํ™”๋˜์—ˆ๋‹ค. ์˜ค๋ž˜์ „ ๊ธฐ์–ต์ด์ง€๋งŒ ์ €์ž ๊ธฐ์–ต์— ๊ทธ ์‚ฌ๊ฑด ์ดํ›„๋กœ ์ปจ์„คํŒ… ์‚ฌ์—…์€ ๊ธฐ์—… ๋‚ด ๋‹ค์–‘ํ•œ ๊ตฌ ๋งค๊ฑด ์ค‘์˜ ํ•˜๋‚˜๊ฐ€ ๋˜์—ˆ๋‹ค. ์š”์ฆ˜์€ ์™ธ๊ตญ๊ณ„์ด๋“  ๊ตญ๋‚ด ์ปจ์„คํŒ… ๊ธฐ์—…์ด๋“  ๋‚˜๋ฆ„์˜ ๊ฒฝ์Ÿ๋ ฅ์„ ๊ฐ€์ง€๊ณ  โ€˜ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์—…โ€™์„ ๋†“๊ณ  ์ž…์ฐฐ์— ์‘ํ•˜๊ณ  ์žˆ๋‹ค. ๋” ์ด์ƒ A4 ์šฉ์ง€ 5์žฅ์œผ๋กœ ์‚ฌ์—…์„ ์ˆ˜์ฃผํ•˜๊ธฐ๋Š” ์–ด๋ ค์šด ์„ธ์ƒ์ด ๋˜์—ˆ๋‹ค. ๊ณต๊ณต IT ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ, ์ œ์•ˆ์š”์ฒญ์„œ(Request For Proposal: RFP)์— ๋ถ€ํ•ฉํ•˜๋Š” A4 ์šฉ์ง€ ์ˆ˜์‹ญ~ ์ˆ˜ ๋ฐฑ ์žฅ์˜ ์ œ์•ˆ์„œ์™€ ํŒ€ ํ”„๋กœํŒŒ์ผ์„ ์ œ์ถœํ•˜๊ณ  ์ ํ•ฉํ•œ ๊ฐ€๊ฒฉ ์ œ์•ˆ๊นŒ์ง€ ํ†ต๊ณผ๋˜์–ด์•ผ ์‚ฌ์—…์„ ์ˆ˜์ฃผํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ์ „ํžˆ ๋ฏผ๊ฐ„ ๊ธฐ์—…์˜ ์ „๋žต์ด๋‚˜ ์šด์˜ ์ปจ์„คํŒ…์€ ๋™๋ฌธ(alumni) ๋„คํŠธ์›Œํฌ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ธ€๋กœ๋ฒŒ Top 3 ์ „๋žต ์ปจ์„คํŒ…์˜ ์˜ํ–ฅ๋ ฅ์ด ๊ฐ•ํ•˜๊ณ , ์ž…์ฐฐ์„ ํ•ด๋„ ๊ทธ ์˜ํ–ฅ๋ ฅ์ด ์ถฉ๋ถ„ํžˆ ๋ฐœํœ˜๊ฐ€ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ํฌ๊ฒŒ ๋ณด๋ฉด ๊ฒฐ๊ตญ ์–ด๋–ค ์˜์—…์„ ํ–ˆ๋Š๋ƒ์— ๋”ฐ๋ผ ์‚ฌ์—… ์ˆ˜์ฃผ๊ฐ€ ์‰ฌ์šฐ๋ƒ ์–ด๋ ค์šฐ๋ƒ๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค. ์‹ค์ œ๋กœ ๋งŽ์€ ์ปจ์„คํ„ดํŠธ ์ง€๋ง์ƒ๋“ค์ด MBA์—์„œ ์ปจ์„คํŒ… ์—ญ๋Ÿ‰์— ๋„์›€์ด ๋˜๋Š” ๋งŽ์€ ๊ฒƒ๋“ค์„ ๋ฐฐ์šฐ๊ธฐ๋„ ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ์˜์—… ํ™œ๋™์— ํ•„์š”ํ•œ ์ธ๋งฅ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์†Œ์œ„ ๋งํ•˜๋Š” โ€˜๋” ์ข‹์€ ํ•™๊ต์˜ MBAโ€™๋ฅผ ์„ ํ˜ธํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ ์ธ๋งฅ๋“ค์ด ๊ธฐ์—…์ด๋‚˜ ์ •๋ถ€์˜ ๊ณ ์œ„์ง์œผ๋กœ ์ง„์ถœํ•  ํ™•๋ฅ ์ด ๋†’๊ธฐ ๋•Œ๋ฌธ์ด๊ณ , ๊ทธ๋Ÿฐ ์ธ๋งฅ๋“ค์„ ํ™œ์šฉํ•ด์„œ ์˜์—… ํ™œ๋™์„ ์กฐ๊ธˆ ๋” ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฏผ์ฃผ๊ณตํ™”๊ตญ์—์„œ ์‹œ๊ฐ„์ด ๊ฐˆ์ˆ˜๋ก ๋งŽ์€ ๊ฒƒ๋“ค์ด ํˆฌ๋ช…ํ•˜๊ฒŒ ์ง„ํ–‰๋˜๊ณ  ์žˆ๊ณ  ํ–ฅํ›„ ๊ฑฐ์˜ ๋ชจ๋“  ์‚ฌ์—…๋“ค์€ ์ž…์ฐฐ์ด ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋•Œ๋Š” ์ง€์‹๊ณผ ๊ฒฝํ—˜, ์ „๋ฌธ์„ฑ์œผ๋กœ ๋ฌด์žฅํ•œ ์ปจ์„คํŒ… ์—ญ๋Ÿ‰์ด ์ •๋ง ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค. Break #3. ์ปจ์„คํ„ดํŠธํ˜• ์˜์—…(Consultative Sales) J ์ฐจ์žฅ์€ ๊ธ€๋กœ๋ฒŒ IT๊ธฐ์—…์ธ I์‚ฌ์˜ ์œ ๋Šฅํ•œ ์˜์—…๋งจ์ด๋‹ค. ๊ธฐ์—… ๊ณ ๊ฐ๋“ค์—๊ฒŒ ์ปดํ“จํ„ฐ ์žฅ๋น„๋ฅผ ๋‚ฉํ’ˆํ•˜๋Š” ๊ทธ๋Š” ์ ๊ทน์ ์ด๋ฉฐ ๋ฐ์€ ์„ฑ๊ฒฉ์œผ๋กœ ์ธ์ƒ๋„ ๋งค์šฐ ์ข‹์œผ๋ฉฐ, ํƒ€๊ณ ๋‚œ ์นœํ™”๋ ฅ์œผ๋กœ ์ฒ˜์Œ ๋งŒ๋‚˜๋Š” ์ƒ๋Œ€์™€๋„ ์‰ฝ๊ฒŒ ์นœํ•ด์ง„๋‹ค. ํŠน๋ณ„ํžˆ ๋Šฅ๋ ฅ์ด ๋›ฐ์–ด๋‚œ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ๋งค์šฐ ํ™œ๋™์ ์ด์–ด์„œ ์—…๋ฌด์™€ ์ง์ ‘์ ์ธ ๊ด€๊ณ„๊ฐ€ ์—†๋Š” ๊ณ ๊ฐ์˜ ์–ด๋ ค์›€๋„ ์ž˜ ํ•ด๊ฒฐํ•ด ์ฃผ๊ณ , ๊ณ ๊ฐ์ด ์–ด๋ ค์šธ ๋•Œ ์ˆ  ํ•œ์ž” ์ ‘๋Œ€๋„ ํ•˜๋Š” ๋“ฑ ์ธ๊ฐ„์ ์œผ๋กœ ๊ณ ๊ฐ์„ ๋Œ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜์—…๋Œ€ํ‘œ๋กœ์„œ ์žฅ์ ์ด ๋งŽ์€ ์‚ฌ๋žŒ์ด๋‹ค. ํšŒ์‚ฌ์˜ ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค์— ๋Œ€ํ•ด์„œ๋„ ์ž˜ ์•Œ๊ณ  ์žˆ์–ด ํ›Œ๋ฅญํ•œ ํ™”์ˆ ๊ณผ ์–ธ๋ณ€์œผ๋กœ ์ž๊ธฐ๊ฐ€ ํŒ๋งคํ•˜๋Š” ์ œํ’ˆ์˜ ์žฅ์ ์„ ์ž˜ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ์ž๊ธฐ๊ฐ€ ํŒ๋งคํ•˜๋Š” ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค์˜ ์ฃผ๋ณ€ ์ง€์‹๋„ ์ž˜ ์•Œ๊ณ  ์žˆ์–ด ๋‹ค์–‘ํ•œ ํ™”์ œ๋กœ๋ถ€ํ„ฐ ์…€๋ง ํฌ์ธํŠธ๋ฅผ ์ฐพ์•„๋‚ด๊ธฐ๋„ ํ•œ๋‹ค. ์ฆ‰, J ์ฐจ์žฅ์€ ์˜์—…์— ์ž์‹ ์ด ์žˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ตœ๊ทผ ๋ณธ์‚ฌ๋Š” ๊ธ€๋กœ๋ฒŒ ์‚ฌ์—… ์ „๋žต ์ฐจ์›์—์„œ ์˜์—… ์ „๋žต์„ ์ˆ˜์ •ํ–ˆ๊ณ  ๊ด€๊ณ„์ž๋กœ๋ถ€ํ„ฐ ์ž๊ธฐ์˜ ์˜์—… ๋ฐฉ์‹์ด ๋‚ก์•˜์œผ๋ฉฐ ํ•ฉ๋ฆฌ์ ์ด์ง€ ๋ชปํ•˜๋‹ค๋Š” ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ณ ๊ฐ๋“ค๋„ J ์ฐจ์žฅ์˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ˜ธ์˜์— ๊ฐ์‚ฌํ•˜์ง€๋งŒ ๋ฌด์–ธ๊ฐ€ ์•„์‰ฌ์šด ์ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์Šฌ์ฉ ๋น„์ถ”๊ธฐ๋„ ํ•œ๋‹ค. ๋ฌด์—‡์ด ์ž˜๋ชป๋˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ผ๊นŒ? ํฌ๋ ˆ์ŠคํŠธ ๋ฆฌ์„œ์น˜[15]๋Š” ํ–ฅํ›„ ๋„๋ž˜ํ•  ์‚ฌ์—… ํ™˜๊ฒฝ์—์„œ B2B ์˜์—…์˜ ์œ ํ˜•์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์†Œ๊ฐœํ•˜์˜€๋‹ค[16]. ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค์˜ ๋ณต์žก๋„์™€ ๊ตฌ๋งค์˜ ๋ณต์žก์„ฑ์„ ๊ธฐ์ค€์œผ๋กœ ์˜์—… ์œ ํ˜•์„ 4๊ฐ€์ง€ ์œ ํ˜•์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ์ •์˜ํ•˜์˜€๋Š”๋ฐ, ๋‹จ์ˆœํžˆ ๊ณ ๊ฐ์˜ ์š”์ฒญ์— ๋Œ€์‘ํ•˜๋Š” โ€˜Order Takersโ€™ํ˜•์ด๋‚˜ ์ œํ’ˆ์„ ์‹œ์—ฐํ•˜๊ณ  ์„ค๋ช…ํ•˜๋Š” โ€˜Explainersโ€™ํ˜•๋ณด๋‹ค๋Š” ๋ถˆํ™•์‹คํ•œ ์ƒํ™ฉ ์†์—์„œ ๊ณ ๊ฐ์„ ๊ฐ€์ด๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” โ€˜Navigatorsโ€™ํ˜•์ด๋‚˜ ๊ณ ๊ฐ์—๊ฒŒ ๋งŽ์€ ์•„์ด๋””์–ด๋ฅผ ์ œ๊ณตํ•˜๊ณ  ๊ฐ™์ด ๊ณ ๋ฏผํ•  ์ˆ˜ ์žˆ๋Š” โ€˜Consultantsโ€™ํ˜•์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ์ฃผ์žฅํ•˜์˜€๋‹ค. ๋˜ํ•œ, 2012๋…„ ๋Œ€๋น„ 2020๋…„๊นŒ์ง€ Consultantsํ˜•์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์œ ํ˜•์˜ ์˜์—…์ง๋“ค์€ ์ค„์–ด๋“ค ๊ฒƒ์œผ๋กœ ์˜ˆ์ธกํ•˜๊ณ  ์žˆ๋‹ค. ์ตœ๊ทผ ์†Œ์œ„ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…, ํŠนํžˆ B2B ์‚ฌ์—…์— ์ฃผ๋ ฅํ•˜๋Š” ๊ธฐ์—…๋“ค์˜ ์˜์—… ๋Œ€ํ‘œ์˜ ์ด๋ฏธ์ง€๋Š” ์ •๋ง ๋ณ€ํ™”ํ•˜๊ณ  ์žˆ๋‹ค. ๋น„์šฉ ํšจ์œจ์ ์ธ ๊ฒƒ์„ ๋„˜์–ด์„œ์„œ ๊ณ ๊ฐ์„ ๋ฆฌ๋”ฉ(Leading) ํ•ด์•ผ ํ•˜๋ฉฐ ๊ทธ ๊ณผ์ •์—์„œ ๊ณ ๊ฐ์˜ ์ด์Šˆ๋‚˜ ๋ฌธ์ œ์— ๋Œ€ํ•ด ์ข€ ๋” ๊ฐœ์ž…ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์ „๊ฐœ๋œ๋‹ค. ์ปจ์„คํ„ดํŠธํ˜• ์˜์—…์ด ๋˜๋ผ๊ณ  ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์€ ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์—์„œ ์‹œ์ž‘๋œ๋‹ค. ์ปจ์„คํ„ดํŠธํ˜• ์˜์—…(Consultative Sales)์ด๋ž€, ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๋ฅผ ์ฒญ์ทจํ•˜๋ฉฐ ๊ณ ๊ฐ์˜ ์ด์Šˆ์™€ ๋ฌธ์ œ๋ฅผ ์ฃผ๋„์ ์œผ๋กœ ํ•ด๊ฒฐํ•˜๊ณ  ๊ทธ ๊ณผ์ •์—์„œ ๊ธฐ์—…์˜ ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค ํŒ๋งค๋ฅผ ๊ทธ ๊ฒฐ๊ณผ๋กœ ์–ป๋Š” ์˜์—… ๋ฐฉ์‹์ด๋ผ๊ณ  ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๊ธฐ์กด์˜ ์˜์—… ๋ฐฉ์‹์— ๋…ผ๋ฆฌ์  ์ ‘๊ทผ๊ณผ ๊ณผํ•™์  ๋ฐฉ์‹์ด ๊ฒฐํ•ฉ๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ด€๊ณ„ ์ค‘์‹ฌํ˜•(Humane) ์˜์—…๊ณผ ์ปจ์„คํ„ดํŠธํ˜• ์˜์—…์˜ ํŠน์ง•์„ ๋น„๊ตํ•ด ๋ณด๋ฉด Table I-5์™€ ๊ฐ™๋‹ค. Table I-5. ์ปจ์„คํ„ดํŠธํ˜• ์˜์—…๊ณผ ๊ด€๊ณ„ ์ค‘์‹ฌ ์˜์—…์˜ ๋น„๊ต ์ปจ์„คํ„ดํŠธํ˜• ์˜์—…์˜ ์‚ฌ์—… ๊ฐœ๋ฐœ ๋ฐฉ์‹์€ ์‚ฌ์—…๊ธฐํšŒ ๋ฐœ๊ตด ์‹œ, โ€˜๋ฌธ์ œ ํ•ด๊ฒฐ๊ธฐ๋ฒ•โ€™์„ ํ™œ์šฉํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค. Fig I-16์€ ์ปจ์„คํ„ดํŠธํ˜• ์˜์—…๋ฐฉ์‹์˜ ์ „ํ˜•์ ์ธ ํ”„๋กœ์„ธ์Šค์ด๋‹ค. Fig. I-16 ์ปจ์„คํ„ดํŠธํ˜• ์˜์—…์˜ ์‚ฌ์—… ๋ฐฉ์‹ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ์‹์„ ๋„์ž…ํ•จ๊ณผ ๋”๋ถˆ์–ด ๊ธฐ์กด ๊ด€๊ณ„ ์ค‘์‹ฌํ˜• ์˜์—…๊ณผ์˜ ์ฐจ์ด์ ์€ ์˜คํผ๋ง(Offering) ์ „๋‹ฌ ์‹œ ์ž๊ธฐ๊ฐ€ ๋ณด์œ ํ•œ ๊ฒƒ ์™ธ์— ์‹œ์žฅ์˜ ๋ชจ๋“  ๊ฒƒ์„ ๊ฒ€ํ† ํ•˜์—ฌ ๋ฌธ์ œ ํ•ด๊ฒฐ์— ์ตœ์ ํ™”๋œ ์„ค๋ฃจ์…˜์„ ์ œ์‹œํ•œ๋‹ค๋Š” ์ ์ด๋‹ค(Best-in Class). ์ฆ‰, ์ž์‚ฌ์˜ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค ์™ธ ๊ณ ๊ฐ์˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ํƒ€์‚ฌ์˜ ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค, ์ƒํ’ˆ๋„ ๋ชจ๋‘ ์—ฎ์–ด์„œ ๊ณ ๊ฐ์˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์„ค๋ฃจ์…˜์œผ๋กœ ์ œ์‹œํ•œ๋‹ค. ๋˜ํ•œ, ๊ณ„์•ฝ ํ›„ ์‚ฌํ›„ ๊ด€๋ฆฌ ์ฐจ์›์—์„œ ์ž ์žฌ์ ์œผ๋กœ ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•œ ์œ„ํ—˜ ์š”์†Œ๋ฅผ<NAME>์—ฌ ๊ณ ๊ฐ๊ณผ ํ•จ๊ป˜ ๋Œ€์•ˆ์„ ์ฐพ๊ณ  ์ถ”๊ฐ€ ์‚ฌ์—… ๊ธฐํšŒ๋ฅผ ๋ฐœ๊ตดํ•˜๋Š” ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์น˜๊ธฐ๋„ ํ•œ๋‹ค. ์ตœ๊ทผ ์ปจ์„คํ„ดํŠธํ˜• ์˜์—…์˜ ์—ญํ• ์ด ๋ถ€์ƒํ•˜๊ฒŒ ๋œ ๊ฐ€์žฅ ํฐ ์š”์ธ์€ ์„ค๋ฃจ์…˜ ์‚ฌ์—…์˜ ํ™•๋Œ€์™€ ๊ด€๊ณ„๊ฐ€ ๊นŠ๋‹ค. ํŠนํžˆ, Value Sales ๋ฐฉ์‹์œผ๋กœ ์˜์—…์„ ํ•˜๋Š” ์‚ฐ์—…์€ ์ด๋Ÿฐ ๋ณ€ํ™”๋ฅผ ํ”ผํ•ด ๊ฐˆ ์ˆ˜ ์—†๋‹ค. ๋ฌผ๋ก , ์ง€๊ธˆ๊นŒ์ง€ ๊ด€๊ณ„ ์ค‘์‹ฌํ˜• ์˜์—…์— ์ง‘์ค‘ํ•ด์˜จ ์‚ฌ๋žŒ๋„ ๋ณธ์ธ์—๊ฒŒ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์„ ์ปจ์„คํ„ดํŠธ๋‚˜ ๊ธฐ์ˆ  ์ „๋ฌธ๊ฐ€์˜ ๋„์›€์„ ๋ฐ›์•„ ๋Œ€์‘ํ•  ์ˆ˜๋Š” ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ทธ ์ˆœ๊ฐ„๋ถ€ํ„ฐ ๋น„์šฉ ํšจ์œจ์ ์ธ ์‚ฌ์—…๊ฐœ๋ฐœ๊ณผ๋Š” ๋ฉ€์–ด์ง€๊ฒŒ ๋œ๋‹ค. B2B ์˜์—…๋Œ€ํ‘œ๋“ค์€ ๋‹น์žฅ ๋ฏธํกํ•˜๊ณ  ๋ถ€์กฑํ•˜๊ฒŒ ์‹œ์ž‘ํ•  ์ˆ˜๋ฐ–์— ์—†์ง€๋งŒ ์ด๋Ÿฐ ์Šคํ‚ฌ(Skill)๋“ค์„ ์ตํžˆ๋Š” ๋…ธ๋ ฅ์ด ํ•„์š”ํ•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด ๊ธฐ์กด์˜ ๊ด€๊ณ„ ์ค‘์‹ฌํ˜• ์˜์—…๋“ค์ฒ˜๋Ÿผ ๊ด€๊ณ„๋ฅผ ์ด์šฉํ•ด์„œ ์ผํšŒ์ ์œผ๋กœ ์ œํ’ˆ์„ ํŒ”๊ณ  ๋๋‚˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ณ ๊ฐ์˜ ์ด์Šˆ์™€ ๋ฌธ์ œ๋ฅผ ์ถฉ๋ถ„ํžˆ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ณ  ์ด๋ฅผ ํ•ด๊ฒฐํ•ด ์ค€๋‹ค๋Š” ์‚ฌ๊ณ  ๋ฆฌ๋”์‹ญ(Thoughts leadership)์„ ๋ฐœํœ˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ ๊ฐ ๊ด€๊ณ„๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๊ฒฐ๊ตญ ๊ทธ๋Ÿฐ ์—ญ๋Ÿ‰์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๊ณ ๊ฐ ๋Œ€์‘์—์„œ ๊ฑด๊ฐ•ํ•œ ์‚ฌ์—… ์„ฑ๊ณผ์™€ ๊ณ ๊ฐ ๊ด€๊ณ„๊ฐ€ ํƒœ๋™๋˜๋ฉฐ, ๊ณ ๊ฐ์€ B2B ์˜์—…๋Œ€ํ‘œ๋ฅผ ์—…๋ฌด์ ์œผ๋กœ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ๋” ์ด์ƒ ํ˜•๋‹˜๋งŒ ์ฐพ๋Š” ์„ธ์ƒ์ด ์•„๋‹˜์€ ๋ถ„๋ช…ํ•˜๋ฉฐ ์‹ค์ œ๋กœ ๋งŽ์€ ์ปจ์„คํ„ดํŠธ๋“ค์ด ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ๊ณผ ๊ฐ™์€ ์‚ฌ์—… ๊ด€๋ จ ์ปจ์„คํŒ…์„ ํ•˜๋ฉด ์ดํ›„ ํด๋ผ์ด์–ธํŠธ์‚ฌ๋กœ ์ด์งํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€๋ฐ ์ด๋Š” ๋ณธ์ธ๋“ค์ด ๊ฐ€์žฅ ์ž˜ ์•Œ๊ณ  ๋˜ ์ปจ์„คํ„ดํŠธํ˜• ์˜์—…์„ ์ œ์ผ ์ž˜ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์ด๋ผ๋Š” ์ƒ๊ฐ์ด ๊ฐ•ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 18์„ธ๊ธฐ ์‚ฐ์—… ํ˜๋ช…์—์„œ ๊ธฐ์—…์˜ ๋ชจ์Šต์ด ๊ตฌ์ฒดํ™”๋˜๊ณ  ํ…Œ์ผ๋Ÿฌ(Frederick W. Taylor. 1856 ~ 1915)์˜ ๊ณผํ•™์  ๊ด€๋ฆฌ๋ฒ•(The principles of Scientific Management) ์ดํ›„ ์ƒ์‚ฐ์˜ ๋น„๋Šฅ๋ฅ ์„ฑ์„ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ๋ชฉ์ ์œผ๋กœ ๊ฒฝ์˜ ์ž๋ฌธ์ด ์‹œ์ž‘๋˜๋ฉด์„œ ์ง€๊ธˆ๊นŒ์ง€ 100์—ฌ ๋…„ ์ด์ƒ ์ปจ์„คํŒ…์€ ์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ฒŒ ๋ชจ๋ฅด๊ฒŒ ๋งค์šฐ ์ฒด๊ณ„์ ์œผ๋กœ ๋น„์ฆˆ๋‹ˆ์Šค์˜ ๋ชจ์–‘์„ ๊ฐ–์ถ”๋ฉด์„œ ๋ฐœ์ „๋˜์—ˆ๋‹ค. Part I์„ ๋งˆ๋ฌด๋ฆฌํ•˜๋ฉด์„œ ๊ธ‰๋ณ€ํ•˜๋Š” ๊ฒฝ์˜ ํ™˜๊ฒฝ์œผ๋กœ ์ปจ์„คํŒ… ์‚ฐ์—…์˜ ๋ชจ์Šต์€ ์ง€๊ธˆ๋„ ๊ณ„์† ๋ณ€ํ™”ํ•ด๋‚˜๊ฐ€๊ณ  ์žˆ์œผ๋ฉฐ ๊ณผ์—ฐ ๋‹ค์‹œ ๋ถ€ํ™œํ•  ๊ฒƒ์ธ๊ฐ€๋ผ๊ณ  ์งˆ๋ฌธํ•ด ๋ณธ๋‹ค๋ฉด ์ €์ž ์ƒ๊ฐ์—๋Š” ๊ทธ๋Ÿด ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ทธ ๋ฐฉ์‹์€ ์ง€๊ธˆ๊ณผ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ธ๊ณต์ง€๋Šฅ๊ณผ ๋กœ๋ด‡์˜ ์—ดํ’์œผ๋กœ ๊ฑฐ์˜ ๋ชจ๋“  ์‚ฐ์—… ํ˜„์žฅ์—์„œ ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท(IoT)๋ฅผ ๋งค์šฐ ๋งŽ์ด ํ™œ์šฉํ•˜๊ฒŒ ๋˜๋Š” ์ˆœ๊ฐ„์ด ๊ณง ๋„๋ž˜ํ•  ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ๊ธฐ๋ฐ˜ ๊ตฌ์กฐ์˜ ๋ณ€ํ™”๋Š” ๋ถ„๋ช…ํžˆ ๋‹ค์–‘ํ•œ ํ”„๋กœ์„ธ์Šค์˜ ๋ณ€ํ™”๋ฅผ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ด๋‹ค. ๊ทธ๊ฒƒ์€ ์ปจ์„คํŒ… ์‚ฌ์—… ๊ด€์ ์—์„œ ๋ณด๋ฉด ํ”„๋กœ์„ธ์Šค ๊ฐœ์„  ๋˜๋Š” ํ”„๋กœ์„ธ์Šค ํ˜์‹  ์ฆ‰, PI ์ปจ์„คํŒ…์˜ ๋Œ€์ƒ์ด ๋œ๋‹ค. ๋ธ”๋ก์ฒด์ธ(Block Chain)[17]์ด 3 ~ 5๋…„ ์•ˆ์— ํ™œ์„ฑํ™”๋˜๋ฉด์„œ ๊ธˆ์œต ์‚ฐ์—…์˜ ์ธ์ฆ ์ฒด๊ณ„์™€ ๊ธˆ์œต ๊ฒฐ์ œ์˜ ์ „๋ฐ˜์ ์ธ ๋ชจ์Šต์ด ์™„์ „ํžˆ ๋ณ€ํ•œ๋‹ค. ์ด ๋˜ํ•œ ์ƒˆ๋กœ์šด ์‚ฌ์—… ๊ธฐํšŒ๊ฐ€ ๋˜๋ฉด์„œ PI ์ปจ์„คํŒ…์˜ ๋Œ€์ƒ์ด ๋œ๋‹ค. ํ˜„์žฌ ์„ธ๊ณ„ ๊ฒฝ์ œ๋Š” ํ˜์‹ ์„ ์œ ๋ฐœํ•˜๋Š” ๊ธฐ๋ฐ˜ ๊ธฐ์ˆ ๋“ค์ด ๋Š์ž„์—†์ด ์ ์šฉ๋˜๊ณ  ์žˆ์œผ๋ฉฐ ์ด๋Ÿฐ ๊ธฐ์ˆ ๋“ค์˜ ์•ˆ์ฐฉ์„ ์œ„ํ•ด์„œ๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…์ด ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์ œ๋Š” ๊ทธ๋Ÿฐ ์ปจ์„คํŒ…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ๊ณผ๊ฑฐ์˜ ๊ฐ™์€ ์˜คํผ๋ง, ๋˜๋Š” ๊ณผ๊ฑฐ์˜ ๊ฐ™์€ ๊ตฌ๋ถ„ ๋˜๋Š” ๊ธฐ์ค€์˜ ์ปจ์„คํ„ดํŠธ๋“ค์ด ์‚ฌ์—…๊ณผ ๊ธฐ์ˆ ์ด ์œตํ•ฉ๋˜์–ด ๋‚˜ํƒ€๋‚˜๋Š” ์ด๋Ÿฐ ์˜์—ญ์˜ ์ปจ์„คํŒ…์„ ์ œ๋Œ€๋กœ ์ˆ˜ํ–‰ํ•ด๋‚ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ธ๊ฐ€? ์ ์ •ํ•œ ์‚ฌ์—… ๋Œ€๊ฐ€๋ฅผ ๋ฐ›์œผ๋ฉด์„œ ์ด์œค์„ ์ถ”๊ตฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ธ๊ฐ€? ์ด๋Ÿฐ ๋ถ€๋ถ„์€ ๊ฐ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์—๊ฒŒ ์—ฌ์ „ํžˆ ์ˆ™์ œ๋กœ ๋‚จ์•„ ์žˆ๋‹ค. ๋ฌผ๋ก , ๋ชจ๋“  ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์ด ๊ทธ ๋ถ€๋ถ„์— ๊ด€์‹ฌ์„ ๊ฐ€์งˆ ํ•„์š”๋Š” ์—†์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฏธ๋ž˜์— ์ธ๊ณต์ง€๋Šฅ๊ณผ ๋กœ๋ด‡์„ ๋„˜์–ด์„œ์„œ ์‚ฌ๋žŒ์˜ ๋Šฅ๋ ฅ์ด ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๊ณณ์ด๋ผ๋ฉด ๋‹น์—ฐํžˆ ๋งˆ์ง„์ด ๋†’์ง€ ์•Š์„๊นŒ? ์ž๋ฌธ์ด๋‚˜ ์ง€์‹ ์„œ๋น„์Šค์˜ ๋Œ€๊ฐ€๋ฅผ ์ธ์ •๋ฐ›๋Š” ๊ฒƒ์€ ์•„์ง๋„ ๋‚˜๋ผ๋งˆ๋‹ค ๊ทธ ์ˆ˜์ค€์€ ๋‹ค๋ฅด์ง€๋งŒ ์ปจ์„คํŒ…์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜์—ˆ๋˜ ์ˆ˜๋งŽ์€ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•, ๋ฐฉ๋ฒ•๋ก ๋“ค์€ ๊ฑฐ๋“ญ ๋ฐœ๋‹ฌ๋˜์–ด ์˜ค๋Š˜๋‚  ๊ธฐ์—… ๊ฒฝ์˜์—์„œ ์ „๋žต๊ณผ ๋งˆ์ผ€ํŒ…, ํ˜์‹  ๋“ฑ ๋‹ค์–‘ํ•œ ์˜์—ญ์—์„œ ๋„๋ฆฌ ํ™œ์šฉ๋˜๊ณ  ์žˆ๋‹ค. ์•„์šธ๋Ÿฌ ์ปจ์„คํŒ…๋„ ์ŠคํŽ™ํŠธ๋Ÿผ์„ ๋”์šฑ ํ™•์žฅํ•˜์—ฌ ์ด์ œ๋Š” ๋‹จ์ผ ์‚ฌ์—… ์ด์ƒ์˜ ์˜๋ฏธ๋ฅผ ํ’ˆ๊ณ  ์žˆ๋‹ค. ์ฆ‰, ์ปจ์„คํŒ… ์‚ฌ์—…์€ 21์„ธ๊ธฐ๋ฅผ ๋งž์•„ โ€˜์ž๋ฌธโ€™์ด๋ผ๋Š” ์†์„ฑ์„ ๊ฐ„์งํ•  ๋ฟ ์•„๋‹ˆ๋ผ ํƒ€ ์‚ฐ์—… ๋ฐ ์‚ฌ์—…์— ์Šค๋ฉฐ๋“ค์–ด ์ง์ ‘ ๋น„์ฆˆ๋‹ˆ์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ํ˜์‹ ํ•˜๋Š” ๊ธฐ๋ฒ•์œผ๋กœ ๋ฐœ์ „ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. Part II์—์„œ๋Š” ์ด๋Ÿฐ ์ปจ์„คํŒ…์˜ ๊ทผ๊ฐ„์ด ๋˜๋ฉฐ ์ปจ์„คํ„ดํŠธ๋ผ๋ฉด ๋ˆ„๊ตฌ๋‚˜ ๊ณ ๋ฏผํ•˜๊ณ  ํ›ˆ๋ จ๋ฐ›๋Š” ๊ธฐ๋ฒ•๊ณผ ์‚ฌ๊ณ  ์ฒด๊ณ„๋“ค์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž. [1] ํฌ์ธˆ์ง€ ๋“ฑ์œผ๋กœ๋ถ€ํ„ฐ ์ˆ˜๋…„๊ฐ„ ํ˜์‹ ๊ธฐ์—…์œผ๋กœ ์นญ์†ก๋ฐ›์•˜๊ณ  ํ†ต์‹ , ๊ฐ€์Šค, ์ „๊ธฐ, ์ œ์ง€, ํ”Œ๋ผ์Šคํ‹ฑ, ์„์œ ํ™”ํ•™, ์ฒ ๊ฐ• ๋“ฑ์˜ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๊ฐ€์ง„ ์—๋„ˆ์ง€ ๊ธฐ์—… ์—”๋ก (Enron)์˜ ํšŒ๊ณ„ ๋ถ€์ • ์‚ฌ๊ฑด. ์—”๋ก ์˜ ํšŒ๊ณ„ ๊ฐ๋ฆฌ์‚ฌ์˜€๋˜ ์œ ๋ช… ํšŒ๊ณ„๋ฒ•์ธ ์•„๋”์•ค๋”์Šจ(Arthur Anderson)์€ ์ด ์‚ฌ๊ฑด์œผ๋กœ ์ธํ•ด ํŒŒ์‚ฐํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. [2] ๋ฐ˜๋ฉด์— 2016๋…„ 11์›” ์—‘์„ผ์ธ„์–ด ์ฝ”๋ฆฌ์•„๋Š” ํ•œ๊ตญ์˜ IT ์„œ๋น„์Šค ๊ธฐ์—…์ธ ๋ฉ”ํƒ€ ๋„ท์— ๋งค๊ฐ๋˜์—ˆ๋‹ค. ์—‘์„ผ์ธ„์–ด์˜ ํ•œ๊ตญ ์‹œ์žฅ ์ฒ ์ˆ˜๋Š” ์—…๊ณ„์— ์ถฉ๊ฒฉ์„ ์ฃผ์—ˆ๊ณ  ํ•œ๊ตญ ์ปจ์„คํŒ… ์‹œ์žฅ์˜ ์นจ์ฒด์™€ ๊ด€๊ณ„๊ฐ€ ๊นŠ๊ธฐ๋„ ํ•˜๊ณ  ๋Œ€๊ธฐ์—… ์ค‘์‹ฌ์˜ IT ์„œ๋น„์Šค ์‹œ์žฅ ๊ตฌ์„ฑ๊ณผ ๋Œ€๊ธฐ์—… ์ค‘์‹ฌ์˜ IT ์ปจ์„คํŒ… ์‚ฌ์—… ์ „๊ฐœ์— ๋”ฐ๋ฅธ ํ•œ๊ตญ ์‹œ์žฅ์˜ ๋…ํŠนํ•œ ํŠน์ง•์œผ๋กœ ์™ธ๊ตญ๊ณ„ ์ปจ์„คํŒ… ๊ธฐ์—…์ด ํ•œ๊ตญ ์‹œ์žฅ์—์„œ ์„ฑ์žฅ ํ•œ๊ณ„์— ๋ด‰์ฐฉํ•œ ๊ฒƒ์œผ๋กœ ๋ณด๋Š” ์‹œ๊ฐ๋„ ๋งŽ๋‹ค. [3] IT ์•„์›ƒ์†Œ์‹ฑ ๊ธฐ์—… EDS๋Š” ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…์ธ AT.Kearney๋ฅผ ์ธ์ˆ˜ํ–ˆ์œผ๋‚˜ ์กฐ์ง๋ฌธํ™” ์ฐจ์ด๋กœ ํฐ ์‹œ๋„ˆ์ง€๋ฅผ ๋ณด์ด์ง€ ๋ชปํ•˜๊ณ  ATK๋Š” ๋‹ค์‹œ ์ง€๋ถ„์„ ์‚ฌ์„œ EDS๋กœ๋ถ€ํ„ฐ ๋…๋ฆฝํ•˜์˜€๋‹ค. EDS๋Š” 2009๋…„ ์‚ฌ์„ธ๊ฐ€ ๊ธฐ์šธ์–ด HP์— ํ•ฉ๋ณ‘๋˜์—ˆ๋‹ค. [4] http://www.economist.com/news/business/21577376-world-grows-more-confusing-demand-clever-consultants-booming-brainy [5] http://www.mckinsey.com/ [6] http://www.bain.com/ [7] http://www.bcg.com/ [8] www.pwc.com; www.ey.com; www.deloitte.com; www.kpmg.com [9] www.accenture.com; www.ibm.com [10] ๊ธฐ์กด์˜ ์ œํ’ˆ๋ณด๋‹ค ๋” ๋›ฐ์–ด๋‚œ ๊ฒƒ์ด ๋‚˜์™€๋„ ์ด๋ฏธ ํˆฌ์ž๋œ ๋น„์šฉ์ด๋‚˜ ๊ธฐํšŒ๋น„์šฉ ๋ฐ ์ „ํ™˜ ๋น„์šฉ, ํ˜น์€ ๋ณต์žกํ•จ์ด๋‚˜ ๊ท€์ฐฎ์Œ์œผ๋กœ ์ธํ•ด ํƒ€์ œํ’ˆ์œผ๋กœ ์‰ฝ๊ฒŒ ์˜ฎ๊ฒจ๊ฐ€์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ [11] www.entrue.com [12] www.nemopartners.com [13] www.kpc.or.kr; www.kma.or.kr [14] ๊ธฐ์—… ๋‚ด ํŠน์ • ์—…๋ฌด ํ”„๋กœ์„ธ์Šค์˜ ์„ฑ๊ณผ๊ฐ€ ์„ธ๊ณ„ ์ตœ๊ณ ์˜ ์ˆ˜์ค€์ผ ๊ฒฝ์šฐ, ์ด๋ฅผ ํƒ€ ์‚ฐ์—… ๋˜๋Š” ํƒ€ ๋ถ„์•ผ๋กœ ํ™•์‚ฐํ•˜์—ฌ ๋™์ผํ•˜๊ฑฐ๋‚˜ ์œ ์‚ฌํ•œ ์„ฑ๊ณผ๋ฅผ ์–ป๊ณ ์ž ํ•˜๋Š” ๋ฐฉ๋ฒ• [15] www.forrester.com [16] โ€˜Death of A B2B Salesmanโ€™, Forrester Research [17] ๊ฐ€์ƒํ™”ํ์ธ ๋น„ํŠธ์ฝ”์ธ ๊ฑฐ๋ž˜๋‚ด์—ญ์„ ๊ธฐ๋กํ•œ ๊ณต๊ฐœ ์žฅ๋ถ€. ๊ฒฐ์ œ ๋ฐ ๊ฑฐ๋ž˜ ํŒจํ„ด์„ ๋ฐ”๊พธ๋ฉด์„œ ๊ธˆ์œต ์‚ฐ์—…์„ ์ค‘์‹ฌ์œผ๋กœ ๋งค์šฐ ํฐ ํŒŒ๊ธ‰ ํšจ๊ณผ๊ฐ€ ์˜ˆ์ƒ๋˜๋Š” ์˜์—ญ์ด๋‹ค. 030 PART II. ์ปจ์„คํŒ… ์Šคํ‚ฌ ์ปจ์„คํŒ… ์Šคํ‚ฌ(Consulting Skills)์€ ์ปจ์„คํ„ดํŠธ๋กœ์„œ ๊ฐ–์ถ”์–ด์•ผ ํ•  ๊ธฐ๋ณธ์ ์ธ ์—ญ๋Ÿ‰์ด๋‹ค. ์ปจ์„คํŒ… ์ง€์‹์ด๋ผ๊ณ  ๋ถ€๋ฅด์ง€ ์•Š๊ณ  ์ปจ์„คํŒ… ์Šคํ‚ฌ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๊ฒƒ์€ ๋จธ๋ฆฌ๋กœ๋งŒ ๊ธฐ์–ตํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ชธ์œผ๋กœ ์ตํ˜€์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค[1]. ์–ด๋–ค ์‚ฌ๋žŒ๋“ค์€ ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ์ƒ๋Œ€์ ์œผ๋กœ ๋” ์ž˜ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ „์  ์ž์งˆ์„ ๊ฐ€์ง€๊ณ  ํƒœ์–ด๋‚˜๋Š” ๊ฒฝ์šฐ๋„ ๋ถ„๋ช…ํžˆ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹จ์–ธํ•˜๊ฑด๋Œ€ ์ €์ž๋Š” ์ด๊ฒƒ๋“ค์€ ์ง€์‹(Knowledge)์ด ์•„๋‹ˆ๋ผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์žŠ์–ด๋ฒ„๋ฆฌ๊ฒŒ ๋˜๋Š” ์Šคํ‚ฌ(Skills)์ด๊ธฐ์— ๋ฐฐ์šฐ๊ณ  ์ตํ˜€์„œ ์ฒดํ™”(้ซ”ๅŒ–) ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ณง ๋ชจ๋‘ ์žŠ์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ•์กฐํ•œ๋‹ค. ์ฆ‰, ์•„๋Š” ๊ฒƒ(Knowing)๋ณด๋‹ค ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ(Doing)์ด ๋” ์ค‘์š”ํ•˜๋‹ค. Fig II-1. ์ปจ์„คํŒ… ์Šคํ‚ฌ์˜ ๊ตฌ๋ถ„ ์ปจ์„คํŒ… ์Šคํ‚ฌ์€ Figure II-1๊ณผ ๊ฐ™์ด ํฌ๊ฒŒ ์‚ฌ๊ณ  ์Šคํ‚ฌ(Thinking Skills)๊ณผ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ(Communication Skills)๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ปจ์„คํ„ดํŠธ์˜ ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  ์ฒด๊ณ„๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ฃผ๋Š” ์‚ฌ๊ณ  ์Šคํ‚ฌ์€ ๋น„์ฆˆ๋‹ˆ์Šค ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ํ•ด์„ํ•  ๋•Œ ์ •ํ™•ํ•œ ํŒ๋‹จ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋ฉฐ, ๋กœ์ง ํŠธ๋ฆฌ(Logic Tree) ์ž‘์„ฑ์ด๋‚˜ ๋ฌธ์ œ ํ•ด๊ฒฐ(Problem Solving) ๊ธฐ๋ฒ•์˜ ์›๋™๋ ฅ์ด ๋œ๋‹ค. ๋˜ํ•œ, ์ตœ๊ทผ ์ƒˆ๋กœ์šด ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ธฐ๋ฒ•์œผ๋กœ ๋ถ€๊ฐ๋˜๊ณ  ์žˆ๋Š” ๋””์ž์ธ ์‹ฑํ‚น(Design Thinking)์— ๋Œ€ํ•ด์„œ๋„ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ์€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ •๋ณด๋ฅผ ๋“ฃ๊ณ  ์ •๋ฆฌํ•˜๋ฉฐ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฌธ์„œํ™”ํ•˜๊ณ  ๋ฐœํ‘œํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Part II์—์„œ๋Š” ์ปจ์„คํŒ… ์Šคํ‚ฌ์—๋Š” ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๋Š”์ง€, ๋˜ ์–ด๋–ป๊ฒŒ ๋ฐฐ์šฐ๊ณ  ์ตํž ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด์ž [1] ์ง€์‹๊ฒฝ์˜(Knowledge Management)์˜ ๊ด€์ ์—์„œ ์šฐ๋ฆฌ๋ง ์ง€์‹์€ ์˜์–ด๋กœ โ€˜Knowledgeโ€™์™€ โ€˜Skillsโ€™๋กœ ๊ตฌ๋ถ„๋˜๋Š”๋ฐ โ€˜Knowledgeโ€™๋Š” ์ฑ…์ด๋‚˜ ๊ต์œก์„ ํ†ตํ•ด ๋จธ๋ฆฟ์†์—์„œ ๊ธฐ์–ต๋˜๋Š” ๊ฒƒ, โ€˜Skillsโ€™๋Š” ๋ชธ์œผ๋กœ ์›€์ง์ด๊ณ  ์‹ค์Šตํ•˜์—ฌ ์ฒด๋“ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž๋™์ฐจ ์šด์ „์ด๋‚˜ ์Šคํ‚ค ํƒ€๋Š” ๋ฒ• ๋“ฑ์€ โ€˜Knowledgeโ€™๊ฐ€ ์•„๋‹ˆ๋ผ โ€˜Skillsโ€™์ด๋‹ค. 04. ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  ์ปจ์„คํŒ… ์Šคํ‚ฌ(Consulting Skills)์€ ์ปจ์„คํ„ดํŠธ๋กœ์„œ ๊ฐ–์ถ”์–ด์•ผ ํ•  ๊ธฐ๋ณธ์ ์ธ ์—ญ๋Ÿ‰์ด๋‹ค. ์ปจ์„คํŒ… ์ง€์‹์ด๋ผ๊ณ  ๋ถ€๋ฅด์ง€ ์•Š๊ณ  ์ปจ์„คํŒ… ์Šคํ‚ฌ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๊ฒƒ์€ ๋จธ๋ฆฌ๋กœ๋งŒ ๊ธฐ์–ตํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ชธ์œผ๋กœ ์ตํ˜€์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค[1]. ์–ด๋–ค ์‚ฌ๋žŒ๋“ค์€ ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ์ƒ๋Œ€์ ์œผ๋กœ ๋” ์ž˜ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ „์  ์ž์งˆ์„ ๊ฐ€์ง€๊ณ  ํƒœ์–ด๋‚˜๋Š” ๊ฒฝ์šฐ๋„ ๋ถ„๋ช…ํžˆ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹จ์–ธํ•˜๊ฑด๋Œ€ ์ €์ž๋Š” ์ด๊ฒƒ๋“ค์€ ์ง€์‹(Knowledge)์ด ์•„๋‹ˆ๋ผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์žŠ์–ด๋ฒ„๋ฆฌ๊ฒŒ ๋˜๋Š” ์Šคํ‚ฌ(Skills)์ด๊ธฐ์— ๋ฐฐ์šฐ๊ณ  ์ตํ˜€์„œ ์ฒดํ™”(้ซ”ๅŒ–) ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ณง ๋ชจ๋‘ ์žŠ์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ•์กฐํ•œ๋‹ค. ์ฆ‰, ์•„๋Š” ๊ฒƒ(Knowing)๋ณด๋‹ค ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ(Doing)์ด ๋” ์ค‘์š”ํ•˜๋‹ค. Fig II-1. ์ปจ์„คํŒ… ์Šคํ‚ฌ์˜ ๊ตฌ๋ถ„ ์ปจ์„คํŒ… ์Šคํ‚ฌ์€ Figure II-1๊ณผ ๊ฐ™์ด ํฌ๊ฒŒ ์‚ฌ๊ณ  ์Šคํ‚ฌ(Thinking Skills)๊ณผ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ(Communication Skills)๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ปจ์„คํ„ดํŠธ์˜ ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  ์ฒด๊ณ„๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ฃผ๋Š” ์‚ฌ๊ณ  ์Šคํ‚ฌ์€ ๋น„์ฆˆ๋‹ˆ์Šค ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ํ•ด์„ํ•  ๋•Œ ์ •ํ™•ํ•œ ํŒ๋‹จ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋ฉฐ, ๋กœ์ง ํŠธ๋ฆฌ(Logic Tree) ์ž‘์„ฑ์ด๋‚˜ ๋ฌธ์ œ ํ•ด๊ฒฐ(Problem Solving) ๊ธฐ๋ฒ•์˜ ์›๋™๋ ฅ์ด ๋œ๋‹ค. ๋˜ํ•œ, ์ตœ๊ทผ ์ƒˆ๋กœ์šด ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ธฐ๋ฒ•์œผ๋กœ ๋ถ€๊ฐ๋˜๊ณ  ์žˆ๋Š” ๋””์ž์ธ ์‹ฑํ‚น(Design Thinking)์— ๋Œ€ํ•ด์„œ๋„ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ์€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ •๋ณด๋ฅผ ๋“ฃ๊ณ  ์ •๋ฆฌํ•˜๋ฉฐ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฌธ์„œํ™”ํ•˜๊ณ  ๋ฐœํ‘œํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Part II์—์„œ๋Š” ์ปจ์„คํŒ… ์Šคํ‚ฌ์—๋Š” ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ๋Š”์ง€, ๋˜ ์–ด๋–ป๊ฒŒ ๋ฐฐ์šฐ๊ณ  ์ตํž ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด์ž [1] ์ง€์‹๊ฒฝ์˜(Knowledge Management)์˜ ๊ด€์ ์—์„œ ์šฐ๋ฆฌ๋ง ์ง€์‹์€ ์˜์–ด๋กœ โ€˜Knowledgeโ€™์™€ โ€˜Skillsโ€™๋กœ ๊ตฌ๋ถ„๋˜๋Š”๋ฐ โ€˜Knowledgeโ€™๋Š” ์ฑ…์ด๋‚˜ ๊ต์œก์„ ํ†ตํ•ด ๋จธ๋ฆฟ์†์—์„œ ๊ธฐ์–ต๋˜๋Š” ๊ฒƒ, โ€˜Skillsโ€™๋Š” ๋ชธ์œผ๋กœ ์›€์ง์ด๊ณ  ์‹ค์Šตํ•˜์—ฌ ์ฒด๋“ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž๋™์ฐจ ์šด์ „์ด๋‚˜ ์Šคํ‚ค ํƒ€๋Š” ๋ฒ• ๋“ฑ์€ โ€˜Knowledgeโ€™๊ฐ€ ์•„๋‹ˆ๋ผ โ€˜Skillsโ€™์ด๋‹ค. ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ ์˜ ๋‘ ๋ฒˆ์งธ ์‹œ๊ฐ„์œผ๋กœ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋กœ์ง ํŠธ๋ฆฌ(Logic Tree)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. 4.2 ์ˆ˜ํ‰์  ๊ตฌ์กฐ์˜ ์ „๊ฐœ - ์—ฐ์—ญ์  vs. ๊ท€๋‚ฉ์  ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ์—์„œ ํ•˜์œ„ ์†Œ๊ทธ๋ฃน๋“ค์€ ์„œ๋กœ ์ˆ˜ํ‰์  ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๋ฉด์„œ ๊ทธ ๋…ผ๋ฆฌ ๊ตฌ์กฐ๋Š” ์—ฐ์—ญ์  ํ˜น์€ ๊ท€๋‚ฉ์  ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„๋‹ค๊ณ  ํ•˜์˜€๋‹ค. ์—ฐ์—ญ์ (Deductive) ๊ตฌ์กฐ๋ผ๋Š” ๊ฒƒ์€ ์˜์‹ฌํ•  ์ˆ˜ ์—†๋Š” ๋ช…ํ™•ํ•œ ๊ฒƒ์œผ๋กœ๋ถ€ํ„ฐ ์ด์„ฑ(ๆ€ง)์— ์˜ํ•ด ์ด๋Œ๋ฆฌ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์ฆ‰, ๋…ผ๋ฆฌ์  ๊ตฌ์กฐ์˜ ์„ ํ–‰ ์š”์†Œ๊ฐ€ ํ›„ํ–‰ ์š”์†Œ์˜ ์›์ธ์ด ๋˜์–ด ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š”<NAME>์ด๋‹ค. ๊ท€๋‚ฉ์ (Inductive) ๊ตฌ์กฐ๋Š” ๋งŽ์€ ์‚ฌ์‹ค ์‚ฌ์ด์˜ ๊ณตํ†ต์ ์ธ ๋ณธ์งˆ์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฒฐ๋ก ์„<NAME>๊ธฐ๊นŒ์ง€ ์ˆ˜์ง‘๋œ ๋งŽ์€ ์ž๋ฃŒ๋“ค์ด ๊ฑฐ์˜ MECE[1]์— ๊ฐ€๊น๋‹ค. Fig II-6. ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ์˜ ์ˆ˜ํ‰์  ๊ตฌ์กฐ ์˜ˆ๋ฅผ ๋“ค์–ด ์„ค๋ช…ํ•ด ๋ณด๋ฉด Figure II-7์€ ์ˆ˜ํ‰์  ๊ตฌ์กฐ ์ค‘์—์„œ ์—ฐ์—ญ์  ์ „๊ฐœ์˜ ์‚ฌ๋ก€์ด๋‹ค.[2] Figure II-7. ์ˆ˜ํ‰์  ๊ตฌ์กฐ์˜ ์—ฐ์—ญ์  ์ „๊ฐœ ์‚ฌ๋ก€ MECE ์ „๊ฐœ๋Š” ์•„๋‹ˆ์ง€๋งŒ ย(1), (2)ย‚, (3)ยƒ์€ ๋‚ด๊ฐ€ ์ƒˆ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚  ์ˆ˜ ์žˆ๋Š” ๋…ผ๊ฑฐ(arguments)๋“ค์„ ์ œ์‹œํ•˜์—ฌ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ•ต์‹ฌ ์ฃผ์žฅ์ธ ๊ฒฐ๋ก  ์ฆ‰, (4)์— ๋„๋‹ฌํ•˜๊ฒŒ ํ•œ๋‹ค. ย(1)์ด ์ „์ œ๋‚˜ ๊ทœ์น™(Rule)์„ ์ œ์‹œํ•˜๊ณ  (2)ย‚๊ฐ€ ๊ทธ๊ฒƒ์˜ ์‚ฌ๋ก€(Case)๊ฐ€ ๋˜๋ฉฐ, (3)์ด ๊ทœ์น™์˜ ์‚ฌ๋ก€์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ(Result)๊ฐ€ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ์ฃผ์žฅ์ธ (4)๋ฅผ ์ด๋Œ์–ด ๋‚ด๋Š” ํ๋ฆ„์ด๋‹ค. ๋”ฐ๋ผ์„œ ์—ฐ์—ญ์  ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ฒŒ ๋  ๊ฒฝ์šฐ์—๋Š” (1) ~ (4)์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ์˜ ์ง„์‹ค์„ฑ์„ ์šฐ์„ ์ ์œผ๋กœ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. Figure II-8. ์ˆ˜ํ‰์  ๊ตฌ์กฐ์˜ ๊ท€๋‚ฉ์  ์ „๊ฐœ ์‚ฌ๋ก€ ํ•œํŽธ, Figure II-8์€ ๊ท€๋‚ฉ์ (Inductive) ์ „๊ฐœ์˜ ์‚ฌ๋ก€๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. MECE ์ „๊ฐœ๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์›์ธ ย(1), (2)ย‚, (3)์œผ๋กœ ๋ณด์•„ ๊ฒฐ๊ณผ๊ฐ€ (4)๋ผ๋Š” ๊ฒƒ์„ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ๊น€ ์”จ์˜ ์ฒซ์งธ ์•„์ด, ๋‘˜์งธ ์•„์ด, ์…‹์งธ ์•„์ด๊ฐ€ ๋ชจ๋‘ ์•„๋“ค์ด๋ฏ€๋กœ ๊น€ ์”จ๋Š” ์•„๋“ค๋ฐ–์— ์—†๋‹ค๋Š” ๊ท€๊ฒฐ์„ ๋‚ณ๊ฒŒ ๋œ๋‹ค. (๋ฌผ๋ก , ์ด ๊ฒฝ์šฐ๋Š” ๊น€ ์”จ์˜ ์ž์‹์€ 3๋ช…๋ฟ์ด๋ผ๋Š” ์ „์ œ๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.) Figure II-9. ์ˆ˜ํ‰์  ๊ตฌ์กฐ๋ฅผ ํ†ตํ•œ ์ถ”๋ก  ๋˜ํ•œ, ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ์˜ ์ˆ˜ํ‰์  ๊ตฌ์กฐ์—์„œ ์—ฐ์—ญ์  ์ „๊ฐœ๊ฐ€ ๋…ผ๊ฑฐ๋ฅผ ๋‚˜์—ดํ•˜๋ฉด์„œ ๊ฒฐ๋ก ์„ ์œ ๋„ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๊ท€๋‚ฉ์  ๊ตฌ์กฐ๋Š” ๊ทผ๊ฑฐ๋“ค๋กœ๋ถ€ํ„ฐ ๊ฒฐ๋ก ์„ ์ถ”๋ก ํ•˜๊ฑฐ๋‚˜ ์›์ธ๊ณผ ์ด์œ (Causes and Effects)๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ์—ฐ์—ญ์  ์ „๊ฐœ์™€ ๊ท€๋‚ฉ์  ์ „๊ฐœ์˜ ์‚ฌ์šฉ์— ์žˆ์–ด๋„ ๊ฐ€์ง€ ๋…ผ๋ฆฌ ๊ตฌ์กฐ๊ฐ€ ์ „๋‹ฌํ•˜๋Š” ํšจ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ์ƒํ™ฉ์—์„œ ์ด๋ ‡๊ฒŒ ์ „๊ฐœํ•˜๋Š”์ง€ ๊ฒฐ์ •ํ•ด์•ผ ํ•˜๋Š” ๋•Œ๋„ ์žˆ๋‹ค. Figure II-10์€ ์–ด๋–ค ์ด์œ ๋กœ ๊น€ํฌ์˜ ๋ฌผ๋ฅ˜์ฐฝ๊ณ ๋ฅผ ๊ตฌ์ž…ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ฒผ๋Š”๋ฐ ์—ฐ์—ญ์  ์ „๊ฐœ์™€ ๊ท€๋‚ฉ์  ์ „๊ฐœ๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ ๊ฒฐ๋ก ์„ ๋„์ถœํ•˜์˜€๋‹ค. ์—ฐ์—ญ์  ์ „๊ฐœ๋Š” ๋น„์ฆˆ๋‹ˆ์Šค์˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์—์„œ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋งŒ์•ฝ, ๊ณ ๊ฐ์ด ์šฐ๋ฆฌ๊ฐ€ ์–ป์€ ๊ฒฐ๊ณผ์™€ ๊ทธ ๊ณผ์ • ๋ชจ๋‘์— ๊ด€์‹ฌ์ด ๋งŽ๋‹ค๋ฉด ์ด๋Š” ์ ์ ˆํ•œ ๋ฐฉ๋ฒ•์ด ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋…ผ๊ฑฐ๋ฅผ ์ „๊ฐœํ•ด ๋‚˜๊ฐ€๋Š” ๊ณผ์ •์—์„œ ์–ด๋Š ๋ถ€๋ถ„์ด๋“ ์ง€ ๊ณ ๊ฐ์ด ๋™์˜ํ•˜์ง€ ์•Š๊ณ  ๋ณ€๊ฒฝ์„ ์š”๊ตฌํ•œ๋‹ค๋ฉด ๋‹ค์Œ ๊ณผ์ • ๋ฐ ๊ทธ ๊ฒฐ๊ณผ๋Š” ์“ธ๋ชจ์—†๊ฒŒ ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค. ๋ฐ˜๋ฉด์— ๊ท€๋‚ฉ์  ์ „๊ฐœ๋Š” ๊ฒฐ๋ก ์„ ์ง€์ง€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ ๋…ผ๊ฑฐ๋“ค์ด ๊ฐ๊ฐ ๋…๋ฆฝ์ ์œผ๋กœ ์˜๋ฏธ๊ฐ€ ๋ถ€์—ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋Š ํ•˜๋‚˜๊ฐ€ ๊ณ ๊ฐ์—๊ฒŒ ์ง€์ง€ ๋ฐ›์ง€ ๋ชปํ•œ๋‹ค ํ• ์ง€๋ผ๋„ ์ „์ฒด์ ์ธ ๊ฒฐ๋ก ์— ์˜ํ–ฅ์„ ๋ผ์น˜๋Š” ์ผ์€ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ์–ด๋–ค ๋ฐฉ์‹์˜ ์ˆ˜ํ‰์  ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š๋ƒ๋Š” ๋…ผ๋ฆฌ๋ฅผ ๊ฐ–์ถ”๋Š” ๊ฒƒ ์™ธ ๋น„์ฆˆ๋‹ˆ์Šค ์˜์‚ฌ ๊ฒฐ์ •์—์„œ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๊ฒฐ๋ก ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. Figure II-10. ์ˆ˜ํ‰์  ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•œ ์ถ”๋ก  ์‚ฌ๋ก€ ์ปจ์„คํŒ…์„ ํ•˜๋ฉด์„œ ๊ณ ๊ฐ๊ณผ ๋งŽ์€ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ๋‹ค์–‘ํ•œ ์‚ฌ์‹ค๋“ค์„ ํ™•์ธํ•˜๊ฒŒ ๋˜๋ฉฐ ํ•„์š”์— ์˜ํ•ด ์ด๊ฒƒ๋“ค์„ ์š”์•ฝํ•ด์•ผ ํ•  ํ•„์š”๋„ ์ƒ๊ธด๋‹ค. '์ด๋Ÿฐ ์ƒํ™ฉ์„ ํƒ€๊ฐœํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ด๋‚˜ ๋ฐฉ์•ˆ์€ ์—†์„๊นŒ?โ€™๋ผ๊ณ  ์ž๋ฌธํ•˜๋Š” ์ปจ์„คํ„ดํŠธ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ(Figure II-11)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. Figure II-11. ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ ์ข…ํ•ฉ ๋ฐ”๋ฐ”๋ผ ๋ฏผํ† ๋Š” SCQA ํ”„๋ ˆ์ž„์›Œํฌ(Figure II-12)์„ ์†Œ๊ฐœํ•˜์˜€๋‹ค. SCQA ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ๋ฅผ ๋งŒ๋“ค๊ธฐ ์ „์˜ ์ƒํ™ฉ๊ณผ ์ „๊ฐœ, ์งˆ๋ฌธ์„ ์ •๋ฆฌํ•˜์—ฌ ๋…ผ๋ฆฌ์  ํ๋ฆ„์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์œผ๋กœ Table II-11๊ณผ ๊ฐ™์ด ์ƒํ™ฉ(Situation)๊ณผ ์ „๊ฐœ(Complication), ์งˆ๋ฌธ(Question)์œผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ์ƒํ™ฉ์€ ์ฃผ์ œ์— ๋Œ€ํ•œ ํ™•์ธ๋œ ์‚ฌ์‹ค๋กœ ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ ์ „๊ฐœ์˜ ์‹œ์ž‘์ (Starting Point)์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ณ , ์ „๊ฐœ๋Š” ์ƒํ™ฉ์—์„œ ํ™•์ธ๋œ ์‚ฌ์‹ค์— ๋Œ€ํ•ด ์ผ์ฐจ์ ์œผ๋กœ ์ œ์‹œ๋œ ์‚ฌํ•ญ๋“ค์ด๋ฉฐ, ์งˆ๋ฌธ์€ ๊ทธ ์‚ฌ์‹ค์— ๋Œ€ํ•œ ์ง„์‹ค์„ฑ์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ฏผํ†  ํ”ผ๋ผ๋ฏธ๋“œ(Minto Pyramid)๋Š” ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต(Answer)์ด ๋˜๋ฉด์„œ ์ƒˆ๋กœ์šด ์‹œ์ž‘์ ์ด ๋œ๋‹ค. Break #4. SCQA ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ Figure II-12. SCQA ์ข…ํ•ฉ ์‚ฌ์‹ค ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ๋ฅผ ์ตœ์ƒ์œ„ ์•„์ด๋””์–ด๋Š” ์–ด๋–ค ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์ด๋‹ค. ์ด๋ฏธ ์–ด๋–ค ์ƒํ™ฉ์ด ์ „๊ฐœ๋˜๊ณ  ์žˆ๊ณ  ๊ทธ๊ฒƒ์„ ์ธ์‹ํ•˜์˜€๊ณ  ๊ทธ๋ž˜์„œ ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์ข‹์„๊นŒ์— ๋Œ€ํ•œ ๋‹ต์ด๋ฉฐ ํ•˜์œ„ ์†Œ๊ทธ๋ฃน๋“ค๋„ ์ง€์†์ ์œผ๋กœ ์งˆ๋ฌธ๊ณผ ์‘๋‹ต์˜<NAME>์„ ๊ฐ–์ถ”๋ฉด์„œ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์ด ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ ์ฆ‰, ๋ฏผํ†  ํ”ผ๋ผ๋ฏธ๋“œ์ด๋‹ค. ๋ฐ”๋ฐ”๋ผ ๋ฏผํ† (Barbara Minto)๋Š” ๊ทธ๋…€์˜ ์ €์„œ์—์„œ ์ด๊ฒƒ์„ SCQA ํ”„๋ ˆ์ž„์›Œํฌ์ด๋ผ ์†Œ๊ฐœํ•˜์˜€๋‹ค. SCQA๋Š” Situation, Complication, Question, Answer์˜ ์•ฝ์ž์ธ๋ฐ ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ๋ฅผ ์ „๊ฐœํ•˜๊ธฐ ์œ„ํ•œ ๋„์ž…๋ถ€(Narrative or Lead-in)๋ฅผ ๋ฐ˜๋“œ์‹œ Table II-1๊ณผ ๊ฐ™์€ ์Šคํ† ๋ฆฌ๋กœ ์ •๋ฆฌํ•ด ๋ณผ ๊ฒƒ์„ ๊ถŒ๊ณ ํ•˜์˜€๋‹ค. Table II-1. SCQA ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๊ตฌ์„ฑ 4.3 ๋กœ์ง ํŠธ๋ฆฌ(Logic Tree) ๋งŒ๋“ค๊ธฐ ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ดํ•ดํ–ˆ๋‹ค๋ฉด ์ด์ œ ๋กœ์ง ํŠธ๋ฆฌ(Logic Tree)๋ฅผ ๊ตฌ์„ฑํ•ด ๋ณด์ž. ๋กœ์ง ํŠธ๋ฆฌ๋Š” ๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ์™€ ๊ฐ™์€ ๊ฒƒ์ด๋ฉฐ ์„ธ๋ถ„ํ™”์˜ ๊นŠ์ด๊ฐ€ ๊ธธ์–ด์ ธ์„œ ๋ณด๊ธฐ ํŽธํ•˜๊ฒŒ ์‹œ๊ณ„ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ 90๋„ ํšŒ์ „ํ•œ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. Figure II-13๊ณผ ๊ฐ™์ด ๋กœ์ง ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ค ๋•Œ์—๋Š” ์šฐ์„  ์—ฐ์—ญ์ ์œผ๋กœ ์‹œ์ž‘ํ•  ๊ฒƒ์ธ๊ฐ€ ๊ท€๋‚ฉ์ ์œผ๋กœ ์‹œ์ž‘ํ•  ๊ฒƒ์ธ๊ฐ€๋ฅผ ๋จผ์ € ๊ฒฐ์ •ํ•œ ํ›„ ๊ทธ์— ๋”ฐ๋ผ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋ฉด ๋œ๋‹ค. Figure II-13. ๋กœ์ง ํŠธ๋ฆฌ ์ž‘์„ฑ์˜ ํ๋ฆ„ ์—ฐ์—ญ์  ๋กœ์ง ํŠธ๋ฆฌ(deductive logic tree)์˜ ์ž‘์„ฑ์€ ๋ถ„์„์˜ ๋ฒ”์œ„์™€ ๋ฐฉํ–ฅ์ด ๋ช…ํ™•ํ•˜๊ฑฐ๋‚˜ ๋กœ์ง ํŠธ๋ฆฌ ์ž‘์„ฑ์˜ ๊ฒฝํ—˜์ด ํ’๋ถ€ํ•œ ๊ฒฝ์šฐ ๋งŽ์ด ํ™œ์šฉํ•˜๋ฉฐ ๊ทธ ์ž‘์„ฑ ์ ˆ์ฐจ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1) ์ดˆ๊ธฐ ์งˆ๋ฌธ์„ ์ž‘์„ฑํ•œ๋‹ค. ์ดˆ๊ธฐ ์งˆ๋ฌธ์€ ๋ถ„์„ ๋Œ€์ƒ๊ณผ ๋ฒ”์œ„๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ์ˆ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ฃผ์–ด์™€ ์ˆ ์–ด์˜ ํ˜•ํƒœ๋ฅผ ์ทจํ•œ๋‹ค. (์˜ˆ) A ๊ณต์žฅ ๊ฐ€๊ณต ๋ผ์ธ์˜ ํ•ต์‹ฌ ๋ถ€ํ’ˆ ๋ถˆ๋Ÿ‰๋ฅ ์ด ๋†’์€ ์ด์œ ๋Š” ๋ฌด์—‡์ธ๊ฐ€? 2) MECE์˜ ์›์น™์— ๋”ฐ๋ผ ํŠธ๋ฆฌ(Tree)๋ฅผ ์ „๊ฐœํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ 3์ฐจ์— ๊ฑธ์ณ ์„ธ๋ถ„ํ™” ๋ฐ ๊ทธ๋ฃนํ•‘ํ•˜๋ฉฐ ๊ทธ ์ดํ•˜๋กœ ๋‚ด๋ ค๊ฐˆ ๊ฒฝ์šฐ๋Š” MECE๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ณ ๋ คํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. Figure II-14๋Š” ์—ฐ์—ญ์  ๋กœ์ง ํŠธ๋ฆฌ์˜ ๊ตฌ์„ฑ์„ ๊ฐœ๋…์ ์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด๋‹ค. ์—ฐ์—ญ์  ๋กœ์ง ํŠธ๋ฆฌ๋ฅผ ๊ฒ€์ฆํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ตœ์ข… ๋ถ„๋ฅ˜๋œ ๊ฒƒ๋“ค์ด ์ฃผ์ œ์™€ ๊ด€๋ จ๋œ ๋ฌธ์ œ ์ „์ฒด๋ฅผ ํฌํ•จํ•˜๋Š”๊ฐ€? Sub ๋‚ด์šฉ๋“ค ๊ฐ„์— ์ค‘๋ณต์ด ๋˜์ง€ ์•Š๋Š”๊ฐ€? ๋“ฑ MECE์˜ ์›์น™์ด ์ถฉ๋ถ„ํžˆ ์ ์šฉ๋˜์—ˆ๋Š”๊ฐ€๋ฅผ ์‚ดํŽด๋ณธ๋‹ค. Figure II-14. ์—ฐ์—ญ์  ๋กœ์ง ํŠธ๋ฆฌ์˜ ๊ตฌ์„ฑ ํ•œํŽธ, ๊ท€๋‚ฉ์  ๋กœ์ง ํŠธ๋ฆฌ(Inductive logic tree)์˜ ๊ฒฝ์šฐ, ๋ถ„์„์˜ ๋ฒ”์œ„๋‚˜ ๋ฐฉํ–ฅ์ด ๋ถˆ๋ช…ํ™•ํ•˜๊ฑฐ๋‚˜ ๋กœ์ง ํŠธ๋ฆฌ ์ž‘์„ฑ์˜ ๊ฒฝํ—˜์ด ๋ถ€์กฑํ•œ ๊ฒฝ์šฐ ๋งŽ์ด ํ™œ์šฉํ•œ๋‹ค. ๋ฐ์ดํ„ฐ๋‚˜ ๋…ผ๊ฑฐ๋“ค์„ ์—ด๊ฑฐํ•˜๊ณ  ์ด๋ฅผ ๊ทธ๋ฃนํ•‘, ์œ„๊ณ„ํ™”ํ•˜์—ฌ ์ฃผ์ œ๋ฅผ ๋ถ„์„ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ท€๋‚ฉ์  ๋กœ์ง ํŠธ๋ฆฌ์˜ ๊ฒฝ์šฐ, ๊ฐ ์ˆ˜์ค€ ๊ฐ„์— โ€˜So Whatโ€™ ๋˜๋Š” โ€˜Why Soโ€™์™€ ๊ฐ™์€ ์˜๋ฏธ ์ „๋‹ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. โ€˜So Whatโ€™์˜ ๊ฒฝ์šฐ, ํ˜„์žฌ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ „์ฒด ๋˜๋Š” ๊ทธ๋ฃนํ•‘๋œ ๋ฐ์ดํ„ฐ์— ์˜ํ•˜์—ฌ ์–ด๋–ค ์ถ”๋ก ์ด ๊ฐ€๋Šฅํ•œ๊ฐ€๋ฅผ ๊ฒ€์ฆํ•˜๋Š” ๊ฒƒ์ด๋‹ค. โ€˜So Whatโ€™์„ ๋”ฐ๋ผ๊ฐ€๋Š” ๊ท€๋‚ฉ์  ๋กœ์ง ํŠธ๋ฆฌ๋Š” โ€˜๋”ฐ๋ผ์„œ ์–ด๋–ค ๊ฒƒ์ด ๋œ๋‹คโ€™๋ผ๋Š” ์˜๋ฏธ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ๋ฐ˜๋ฉด์— โ€˜Why Soโ€™๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋กœ์ง ํŠธ๋ฆฌ๋Š” ์–ด๋–ค ๊ฐœ๋…์˜ ํƒ€๋‹น์„ฑ์ด ํ˜„์žฌ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ „์ฒด ๋˜๋Š” ๊ทธ๋ฃนํ•‘๋œ ๋ฐ์ดํ„ฐ์— ์˜ํ•ด ์ฆ๋ช…๋œ๋‹ค๋Š” ๊ฒƒ์„ ๊ฒ€์ฆํ•˜๋Š” ๊ฒƒ์ด๋‹ค. โ€˜WhySoโ€™๋ฅผ ๋”ฐ๋ผ๊ฐ€๋Š” ๊ท€๋‚ฉ์  ๋กœ์ง ํŠธ๋ฆฌ๋Š” โ€˜์™œ ๊ทธ๋ ‡๊ฒŒ ๋œ๋‹คโ€™๋ผ๋Š” ์˜๋ฏธ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. Figure II-15๋Š” โ€˜So Whatโ€™๊ณผ โ€˜Why Soโ€™์˜ ์‚ฌ๋ก€๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ท€๋‚ฉ์  ๋กœ์ง ํŠธ๋ฆฌ์ด๋‹ค. Figure II-15. ๊ท€๋‚ฉ์  ๋กœ์ง ํŠธ๋ฆฌ ๊ตฌ์„ฑ์˜ ์‚ฌ๋ก€ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑ๋œ ๋กœ์ง ํŠธ๋ฆฌ๋Š” ๊ฒ€์ฆ์„ ํ•ด์•ผ ํ•˜๋ฉฐ, ๊ฒ€์ฆ๋œ ๋กœ์ง ํŠธ๋ฆฌ๋Š” โ€˜์ „๋žต์  ํŒ๋‹จโ€™์ด ๊ฐ€๋Šฅํ•˜๋‹ค. Figure II-16๊ณผ ๊ฐ™์ด ๊ณผ์ผ ํ†ต์กฐ๋ฆผ์„ ๋งŒ๋“œ๋Š” ๊ธฐ์—…์˜ ์˜ˆ๋ฅผ ๋“ค๋ฉด ์ƒํ’ˆ<NAME>๋ฃŒ์ธ ๊ณผ์ผ์„ ์˜๋ฏธ ์—†์ด ๊ตฌ๋ถ„ํ•˜๊ณ  ๋กœ์ง ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉด Figure II-16์˜ ์™ผ์ชฝ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑ๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ „๋žต์  ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌํ•˜๋ฉด Figure II-16์˜ ์˜ค๋ฅธ์ชฝ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ์‚ฌ๋žŒ๋“ค์ด ๊ป์งˆ์งธ ๋จน๊ธฐ์— ๋ถˆํŽธํ•œ ๊ณผ์ผ์„ ๊ฐ€๊ฒฉ์ด ๋†’์€ ๊ณ„์ ˆ์— ํ†ต์กฐ๋ฆผ์œผ๋กœ ๋งŒ๋“ค์–ด ํŒ๋งคํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ „๋žต์  ํŒ๋‹จ์ด ๋„์ถœ๋  ์ˆ˜ ์žˆ๋‹ค. Figure II-16. ๋กœ์ง ํŠธ๋ฆฌ์˜ ์ „๋žต์  ํŒ๋‹จ ๋˜ํ•œ, ๋‹ค์–‘ํ•œ ๊ด€์ ์—์„œ ๋กœ์ง ํŠธ๋ฆฌ์˜ ๊ฒ€์ฆ์ด ํ•„์š”ํ•˜๋‹ค. ์–ด๋–ค ๋ฌธ์ œ์— ๋Œ€ํ•ด ์›์ธ์„ ๋ชจ์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ ์™œ ๊ทธ๊ฒƒ์ด ์›์ธ์ธ์ง€ ๋Š์ž„์—†์ด ๋ฐ˜๋ณตํ•ด์„œ โ€˜Why?โ€™๋ผ๊ณ  ๋ฌผ์–ด๋ณด์•„์•ผ ํ•˜๊ณ , ํ•ด๊ฒฐ์ฑ…์„ ๊ตฌ์ฒดํ™”ํ•˜๋Š” ๊ฒฝ์šฐ โ€˜SoWhatโ€™, โ€˜So Howโ€™์™€ ๊ฐ™์ด ์‚ฌ๊ณ  ์‹คํ—˜์„ ์ง€์†์ ์œผ๋กœ ํ•ด์•ผ ํ•œ๋‹ค. Figure II-17. ๋กœ์ง ํŠธ๋ฆฌ์˜ ๊ฒ€์ฆ ๋กœ์ง ํŠธ๋ฆฌ๋Š” ์ •๋ง ๋งŽ์€ ์—ฐ์Šต์ด ํ•„์š”ํ•˜๋‹ค. ๋ชจ๋‘์—์„œ ๋งํ–ˆ๋“ฏ์ด ์ปจ์„คํŒ… ์Šคํ‚ฌ์€ ๋ง ๊ทธ๋Œ€๋กœ Knowledge๊ฐ€ ์•„๋‹ˆ๋ผ Skills์ด๋‹ค. ์–ด๋–ค ์ฃผ์ œ๋„ ์ข‹๋‹ค. ์˜๋ฌธ์— ๋Œ€ํ•ด ์ฒ˜์Œ 2๊ฐœ์˜ ๊ฐ€์ง€, ๊ทธ๋‹ค์Œ๋„ 2~3๊ฐœ์˜ ๊ฐ€์ง€, ๊ทธ๋‹ค์Œ์€ ๊ฐ€์ง€ ํ•˜๋‚˜ํ•˜๋‚˜์— ๋Œ€ํ•œ ์‚ฌ์œ ๋‚˜ ๊ทผ๊ฑฐ. ๋Š์ž„์—†์ด ์—ฐ์Šตํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ฃผ์ œ๊ฐ€ ์ƒ๊ฐ๋‚˜์ง€ ์•Š๋Š”๊ฐ€? ๊ทธ๋Ÿผ ๋‹ค์Œ ์งˆ๋ฌธ๋“ค์— ๋Œ€ํ•ด ๋กœ์ง ํŠธ๋ฆฌ๋ฅผ ํ•œ๋ฒˆ ๊ทธ๋ ค๋ณด๋ผ. ์ €์ž๊ฐ€ ๊ฐ•์˜ํ•  ๋•Œ ์•„๋ž˜ ์งˆ๋ฌธ๋“ค์€ ์‹ ์ž… ์‚ฌ์›๋“ค์—๊ฒŒ ํญ๋ฐœ์ ์ธ ์ง€์ง€๋ฅผ ๋ฐ›์•˜๋‹ค. ๋ฌผ๋ก , ์ •๋‹ต์€ ์—†๋‹ค. ๊ทธ(๊ทธ๋…€)์˜ ๋กœ์ง์ผ ๋ฟ์ด๋‹ค. ๊ทธ(๊ทธ๋…€)๋ฅผ ์™œ ๋งŒ๋‚˜์•ผ ํ•˜๋Š”๊ฐ€? ๊ทธ๋…€(๊ทธ)์™€ ๊ผญ ๊ฒฐํ˜ผํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ๋‚˜๋Š” ์–ด๋””์— ์ง‘์„ ๋งˆ๋ จํ•  ๊ฒƒ์ธ๊ฐ€? ์ด ํšŒ์‚ฌ๋ฅผ ์™œ ๋‹ค๋…€์•ผ ํ•  ๊ฒƒ์ธ๊ฐ€? [1] Mutually Exclusive Collectively Exhaustive ์ƒํ˜ธ ๋ฐฐ์ œ์™€ ์ „์ฒด ํฌ๊ด„. ์ค‘๋ณต๋˜์ง€ ์•Š๊ฒŒ ๋‚˜๋ˆ„์ง€๋งŒ ํ•˜๋‚˜๋„ ๋น ์ง์—†์ด. [2] ๋ฐ”๋ฐ”๋ผ ๋ฏผํ† ์˜ ์ €์„œ์—์„œ ์˜ˆ๋ฅผ ์ธ์šฉํ•˜์˜€๋‹ค. 05. ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ธฐ๋ฒ• ๋กœ์ง ํŠธ๋ฆฌ์— ๋Œ€ํ•ด ์ข€ ์ต์ˆ™ํ•ด์กŒ๋Š”๊ฐ€? ๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ์„ธ๊ณ„๋กœ ๋“ค์–ด๊ฐ€ ๋ณด์ž. ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  ๋ฐ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ธฐ๋ฒ•๊ณผ ๊ด€๋ จํ•ด์„œ ๊ฐ€์žฅ ๋งŽ์ด ์•Œ๋ ค์ง„ ๊ฒƒ์€ ๋งฅํ‚จ์ง€ ์ปจ์„คํŒ…์˜ โ€˜๋ฌธ์ œ ํ•ด๊ฒฐ 7 ๋‹จ๊ณ„ ํ”„๋กœ์„ธ์Šคโ€™์ด๋‹ค. ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ ์— ์ž…๊ฐํ•œ ์ด ๋ฌธ์ œ ํ•ด๊ฒฐ ํ”„๋กœ์„ธ์Šค๋Š” ๋ชจ๋“  ์ปจ์„คํŒ… ๊ธฐ๋ฒ•์˜ ๊ทผ๊ฐ„์„ ์ด๋ฃจ๊ณ  ์žˆ๋‹ค๊ณ  ํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋ฉฐ ๋งŽ์€ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…๋“ค์ด ์ด๋ฅผ ์ฐจ์šฉํ•˜์—ฌ ํ•ด๋‹น ๊ธฐ์—…์— ๋งž๊ฒŒ ์ˆ˜์ •, ๋ณด์™„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. Figure II-18์€ ๋ฌธ์ œ ํ•ด๊ฒฐ 7๋‹จ๊ณ„๋ฅผ ๋„์‹ํ™”ํ•œ ๊ฒƒ์ธ๋ฐ, ๋ฌธ์ œ๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ๊ตฌ์กฐ์ ์œผ๋กœ ์„ธ๋ถ„ํ™”ํ•œ ํ›„, ํ•ด๊ฒฐ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. ์„ ์ •๋œ ๋ฌธ์ œ๋“ค์˜ ์กฐ์‚ฌ ๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•˜๊ณ  ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘, ๋ถ„์„ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ข…ํ•ฉํ•˜๊ณ  ์‹œ์‚ฌ์ ์„ ํ† ๋Œ€๋กœ ํ•ด๊ฒฐ์•ˆ์„ ๊ฐœ๋ฐœํ•˜๋Š” ์ˆœ์„œ์ด๋‹ค. ์ด๋Ÿฐ ์ผ๋ จ์˜ ์—…๋ฌด ์ง„ํ–‰ ๊ณผ์ •์—์„œ ๊ณ ๊ฐ ๋ฐ ํ˜‘์—… ๊ด€๋ จ์ž๋“ค ๊ฐ„์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์€ ์ง€์†์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๋ฉฐ ๊ฐ•์กฐ๋œ๋‹ค. ์ œ5์žฅ์—์„œ๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ธฐ๋ฒ•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ๋” ์•Œ์•„๋ณด์ž. Figure II-18. Mckinsey Problem Solving 7 steps 5.1 ๋ฌธ์ œ์˜ ์ •์˜ ๋ฐ ์„ธ๋ถ„ํ™” ๋ฌธ์ œ์˜ ์ •์˜ ๋‹จ๊ณ„๋Š” ์ด์Šˆ๋‚˜ ๋ฌธ์ œ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ๊ทœ๋ช…ํ•ด์•ผ ํ•˜๋Š” ๋‹จ๊ณ„๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ์‹œ์ž‘์ ์ด๋‹ค. ๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ํ™˜๊ฒฝ์€ ๋Œ€์ฒด๋กœ ๋งค์šฐ ์—ด์•…ํ•˜๋‹ค. ์ €์ž์˜ ๊ฒฝํ—˜์„ ํšŒ์ƒํ•ด ๋ณด๋ฉด, ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์ •๋ณด๋Š” ๋ถ€์กฑํ•˜๊ณ , ๊ณ ๊ฐ์€ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋  ๊ฒƒ์ด๋ผ๋Š” ๋†’์€ ๊ธฐ๋Œ€์น˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ๋งก์€ ์ž์‹ ์ด๋‚˜ ๊ฐ™์ด ํˆฌ์ž…๋œ ์ปจ์„คํ„ดํŠธ๋“ค์€ ์ง€์‹๊ณผ ๊ฒฝํ—˜์ด ๋ถ€์กฑํ•˜๋‹ค. ์ƒˆ๋กœ์šด ํ™˜๊ฒฝ์—์„œ ๋ฌด์–ผ ์–ด๋–ป๊ฒŒ ์‹œ์ž‘ํ•ด์•ผ ํ• ์ง€ ์ž˜ ๋ชจ๋ฅด๋ฉฐ, ์ฃผ์–ด์ง„ ๊ณผ์ œ๋„ ๊ตฌ์ฒด์ ์ด์ง€ ์•Š์•„ ๋„๋Œ€์ฒด ๋ฌด์–ผ ์›ํ•˜๋Š” ๊ฒƒ์ธ์ง€๋„ ๋ช…ํ™•ํ•˜์ง€ ์•Š๋‹ค. ๊ทธ๋ ‡๋‹ค๊ณ  ์ฃผ์–ด์ง„ ์‹œ๊ฐ„์ด ์—ฌ์œ ๊ฐ€ ์žˆ๋Š” ๊ฒƒ๋„ ์•„๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ๊ตฌ์„ฑ์›๋“ค์˜ ์ดํ•ด๊ด€๊ณ„๋„ ์ฒจ์˜ˆํ•˜๊ณ  ์‚ฌ๋‚ด ์ •์น˜๋„ ๊ฑธ๋ ค ์žˆ์–ด์„œ ์ด๊ฒŒ ์–ด๋–ป๊ฒŒ ํ’€๋ฆฌ๋Š๋ƒ์— ๋”ฐ๋ผ ๊ณ ๊ฐ์‚ฌ์˜ ์–ด๋Š ๋ผ์ธ์€ ๋ชจ๋‘ ๋งํ•˜๊ฒŒ ๋  ์ˆ˜๋„ ์žˆ๋‹ค. ์ €์ž๊ฐ€ ๊ฒช์–ด๋ณธ ๋Œ€๋ถ€๋ถ„์˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ํ™˜๊ฒฝ์€ ์ด๋žฌ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์ปจ์„คํ„ดํŠธ๋“ค ๋˜๋Š” ์ „๋žต๊ธฐํš๊ฐ€๋“ค๋„ ๋น„์Šทํ•œ ํ™˜๊ฒฝ์— ์ง๋ฉดํ•ด ๋ณธ ์ ์ด ์žˆ์œผ๋ฆฌ๋ผ ์ƒ๊ฐ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ชจ๋“  ๊ฒƒ์„ ๊ทน๋ณตํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด ์ปจ์„คํ„ดํŠธ๋“ค ์•„๋‹ˆ๋˜๊ฐ€? ์–ด๋””์—์„œ ๋ฌด์—‡๋ถ€ํ„ฐ ์‹œ์ž‘ํ• ๊นŒ? ์šฐ์„ ์ ์œผ๋กœ ํ•ด์•ผ ํ•  ๊ฒƒ์€ ์ด์Šˆ๋‚˜ ๋ฌธ์ œ๋ฅผ ์ •์˜ํ•˜๋Š” ์ผ์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌธ์ œ๋ž€ ๋ฌด์—‡์ผ๊นŒ? ๋ฌธ์ œ๋ž€ ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ๋ชฉํ‘œ์™€ ํ˜„์ƒ์˜ ์ฐจ์ด๋กœ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Figure II-19. ๋ฌธ์ œ์˜ ๋ฐœ์ƒ ์›์ธ ๋ชฉํ‘œ๋Š” โ€˜์–ด๋–ป๊ฒŒ ๋˜๋ฉด ์ข‹๊ฒ ๋‹ค(Should be)โ€™๋ผ๋Š” ์ด์ƒ์ ์ธ ๋ชจ์Šต์œผ๋กœ ์กด์žฌํ•˜๊ณ  ์žˆ๊ณ , ์‹ค์ œ ํ˜„์‹ค์€ ๊ทธ ๋ชฉํ‘œ๋ฅผ ๋‹ฌ์„ฑํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ด๋‹ค. ์ด ๋‘ ๊ฐ€์ง€ ์ƒํƒœ์˜ ์‚ฌ์ด์— ์ธ์‹์˜ ์ฐจ์ด(Gap)๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ ๋ฌธ์ œ(Problem)๋ผ๋Š” ๊ฒƒ์€ ๊ทธ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ๊ทธ ์ฐจ์ด๋ฅผ ์—†์• ์ฃผ๋Š” ๊ฒƒ์ด '๋ฌธ์ œ ํ•ด๊ฒฐ(Problem Solving)'์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ? ์žˆ๋‹ค. ๋ชฉํ‘œ๋ฅผ ๋‚ฎ๊ฒŒ ์„ค์ •ํ•˜๊ณ  ํ˜„์ƒ๊ณผ ํ‹€์–ด์ง€์ง€ ์•Š๊ฒŒ ๋งž์ถ”๋ฉด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ด์œ ๊ฐ€ ์ „ํ˜€ ์—†๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ง์€ ๋‚œ์„ผ์Šค(Non-Sense)์ด๋‹ค. ๊ธฐ์—…์ด๋“  ์ •๋ถ€์ด๋“  ํ˜„์žฌ๋ณด๋‹ค ๋ฐœ์ „์ ์ธ ๋ชจ์Šต์ด ๋˜๊ณ  ์„ฑ์žฅํ•˜๊ธฐ๋ฅผ ์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ๊ธฐ์—…๋„ ๊ทธ๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ๋ชฉํ‘œ๋ฅผ ์„ค์ •ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ป๊ฒŒ ๋ณด๋ฉด ๋ฌธ์ œ์˜ ๋ฐœ์ƒ์€ ํ•„์—ฐ์ ์ด๋ผ๊ณ ๋„ ๋งํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋“ค์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ด€์ ์—์„œ ๋ถ„๋ฅ˜๋  ์ˆ˜ ์žˆ์ง€๋งŒ ๋ชฉํ‘œ์™€ ํ˜„์ƒ์„ ๋†“๊ณ  ์ƒ๊ฐํ•ด ๋ณด๋ฉด ํฌ๊ฒŒ โ€˜๋ฐœ์ƒํ˜• ๋ฌธ์ œโ€™์™€ โ€˜์„ค์ •ํ˜• ๋ฌธ์ œโ€™๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐœ์ƒํ˜• ๋ฌธ์ œ๋Š” ํ˜„์žฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ฃผ๋กœ ๊ณผ๊ฑฐ์˜ ์–ด๋–ค ์ด์œ ๋กœ ๋ฐœ์ƒ๋˜์–ด ํ˜„์žฌ๊นŒ์ง€ ์ง€์†๋˜๊ณ  ์žˆ๋Š” ๊ฒƒ๋“ค์ด๋‹ค. ๋ฌด์—‡์ด ๋ฏธ๋‹ฌ๋œ๋‹ค๋˜๊ฐ€ ์ดํƒˆ๋˜์—ˆ๋‹ค๋˜๊ฐ€ ํ•˜๋Š” ๊ฒƒ๋“ค์ด๋‹ค(Cause-oriented thinking). ๋ฐ˜๋ฉด, ์„ค์ •ํ˜• ๋ฌธ์ œ๋Š” ๋ฏธ๋ž˜์˜ ์–ด๋–ค ๋ชฉํ‘œ๋ฅผ ์„ค์ •ํ•˜๊ณ  ์ด๋ฅผ ์„ฑ์ทจํ•˜๊ธฐ ์œ„ํ•ด ์ •์˜๋˜๋Š” ๋ฌธ์ œ๋“ค์ด๋‹ค. โ€˜๋ฌด์—‡์„ ์„ฑ์ทจํ•ด์•ผ ํ•œ๋‹คโ€™๋˜์ง€ โ€˜์–ด๋–ค ๊ฒƒ์„ ๊ฐœ๋ฐœํ•ด์•ผ ํ•œ๋‹คโ€™๋˜์ง€ ํ•˜๋Š” ๊ฒƒ๋“ค์ด๋‹ค. ๋ฌธ์ œ๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ฒƒ๋“ค์„ ์ƒ๊ฐํ•ด ๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋Š”๋ฐ Table II-2์™€ ๊ฐ™์€ ๋ฌธ์ œ ์ •์˜ ์„œ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฒด๊ณ„์ ์œผ๋กœ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. Table II-2. ๋ฌธ์ œ ํ•ด๊ฒฐ ์ •์˜์„œ Template ์ฒซ ๋ฒˆ์งธ ํ•  ์ผ์€ ํ•ด๊ฒฐํ•  ๋ฌธ์ œ๋ฅผ ์งˆ๋ฌธ<NAME>์œผ๋กœ ์จ๋ณด๋Š” ๊ฒƒ์ด๋‹ค(Basic Question to be resolved). ํ•ด๊ฒฐํ•ด์•ผ ํ•  ๊ธฐ๋ณธ ์งˆ๋ฌธ์€ ํ–ฅํ›„ ๋ถ„์„ ์ž‘์—…์— ์ดˆ์ ์„ ๋งž์ถ”์–ด ์ž‘์„ฑํ•ด์•ผ ํ•˜๋ฉฐ, ๊ฐ„๊ฒฐํ•˜๊ณ  ์‹คํ–‰์— ์˜ฎ๊ธฐ๊ธฐ ์šฉ์ดํ•ด์•ผ ํ•œ๋‹ค. ๋ฌธ์ œ ์ •์˜๋Š” ์ƒ์„ธํ• ์ˆ˜๋ก ์ข‹์ง€๋งŒ ๋ฌธ์ œ์˜ ๋ฒ”์œ„๊ฐ€ ํ˜‘์†Œํ•ด์ ธ์„œ ์ž˜๋ชป๋œ ๋ฌธ์ œ๋ฅผ ๋‹ค๋ฃจ๊ฒŒ ๋˜๋Š” ์ผ์€ ์—†์–ด์•ผ ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ํ”„๋กœ์ ํŠธ์™€ ๊ด€๋ จ๋œ ์ƒํ™ฉ์„ ๊ธฐ์ˆ ํ•ด ๋ณด๋Š” ๊ฒƒ์ด๋‹ค(Perspective/Context).๋ฌธ์ œ๋ฅผ ๋‘˜๋Ÿฌ์‹ธ๊ณ  ์žˆ๋Š” ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์  ์š”์†Œ๋“ค์— ๋Œ€ํ•ด ์„œ์ˆ ํ•ด์•ผ ํ•œ๋‹ค. ์„ธ ๋ฒˆ์งธ๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ์„ฑ๊ณต์— ๋Œ€ํ•œ ๋ชจ์Šต, ๊ธฐ์ค€์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ •ํ•˜๋Š” ์ผ์ด๋‹ค(Criteria for success). ์„ฑ๊ณต์˜ ๊ธฐ์ค€์— ๋Œ€ํ•œ ๋ชจ์Šต์€ ํ”„๋กœ์ ํŠธ ์Šคํฐ์„œ(Project Sponsor)์˜ ์ƒ๊ฐ์ด ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค. ๊ธฐ์—…์—์„œ ์˜๋ขฐํ•œ ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ, ์–ด๋–ค ๋ชฉํ‘œ๋ฅผ ๊ฐ€์ ธ๊ฐˆ ๊ฒƒ์ด๋ฉฐ ๊ธฐ๋Œ€ ํšจ๊ณผ์™€ ํ”„๋กœ์ ํŠธ์˜ ์„ฑ๊ณต์— ๋Œ€ํ•œ ํŒ๊ฐ€๋ฆ„์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋‚ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ๋„ค ๋ฒˆ์งธ๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ์„ฑ๊ณต์ ์ธ ์ดํ–‰ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์‚ฌ๋žŒ์„ ๊ทœ๋ช…ํ•˜๋Š” ๊ฒƒ์ด๋‹ค(decision maker). ํ”„๋กœ์ ํŠธ ์Šคํฐ์„œ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ  ๊ทธ๊ฒƒ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ณ„๋„์˜ ํ‰๊ฐ€์ž๊ฐ€ ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋‹ค์„ฏ ๋ฒˆ์งธ๋„ ์˜์‚ฌ๊ฒฐ์ •์ž๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ ๋ณด์กฐ์  ์˜์‚ฌ๊ฒฐ์ •์ž(sub decision maker)๋ผ๊ณ ๋„ ์นญํ•œ๋‹ค. ๋ณด์กฐ์  ์˜์‚ฌ๊ฒฐ์ •์ž๋ฅผ ๊ฒฐ์ •ํ•  ๋•Œ๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ์ž(๋˜๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐํŒ€)์„ ์ง€์ง€ํ•˜๋Š” ์‚ฌ๋žŒ์„ ์„ ์ •ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์ง€๋งŒ ์ข€ ๋‹ค๋ฅธ ์ฐจ์›์˜ ํŒ(Tip)์œผ๋กœ ์˜คํžˆ๋ ค ๋ฌธ์ œ ํ•ด๊ฒฐ ๋…ธ๋ ฅ์„ ๋ฐฉํ•ดํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ์ธ๋ฌผ์„ ์„ ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์—ฌ์„ฏ ๋ฒˆ์งธ, ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์ผ๋ จ์˜ ํ•ด๊ฒฐ์ฑ…๊ณผ ๊ทธ๊ฒƒ์„ ๋ฐฉํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์š”์ธ๋“ค์„ ์ •๋ฆฌํ•ด ๋ณด๋Š” ๊ฒƒ์ด๋‹ค(constraints with solution space). ์ด ์žฅ์•  ์š”์ธ๋“ค์€ ๋ฌธ์ œ ํ•ด๊ฒฐ์ด ์ง„ํ–‰๋˜๋ฉด์„œ ์ œ๊ฑฐ๋˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. ๋งˆ์ง€๋ง‰ ์ผ๊ณฑ ๋ฒˆ์งธ๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ๋ฐฐ์ œ๋˜์–ด์•ผ ํ•  ์‚ฌํ•ญ๋“ค์„ ๋„์ถœํ•˜๊ณ  ์š”์ฒญ์‚ฌํ•ญ์œผ๋กœ ์ •๋ฆฌํ•ด ๋ณธ๋‹ค(scope of solution space). ๊ฒฐ๋ก ์ ์œผ๋กœ ๋ฌธ์ œ ์ •์˜ ์ •์˜์„œ ํ…œํ”Œ๋ฆฟ์˜ (2), (3), (7)์€ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์ถฉ์กฑ์š”๊ฑด์ด๊ณ  (4ย, (5)ย, (6)ย‘์€ ๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ๊ฑธ๋ฆผ๋Œ์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ œ์ •์˜์„œ์˜ ์ž‘์„ฑ์„ ์™„๋ฃŒํ–ˆ๋‹ค๋ฉด ์ด๊ฒƒ์„ ๊ธฐ๋ณธ์œผ๋กœ ๋กœ์ง ํŠธ๋ฆฌ๋ฅผ ๊ทธ๋ ค๋ณผ ์ˆ˜ ์žˆ๋‹ค. Figure II-20์€ ๋ฌธ์ œ ์ •์˜์„œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋กœ์ง ํŠธ๋ฆฌ๋กœ ์ „ํ™˜๋˜๋Š”์ง€ ๋ณด์—ฌ์ค€๋‹ค. ๋กœ์ง ํŠธ๋ฆฌ์˜ ๊ฐ ์ˆ˜์ค€ ๊ฐ„์—๋Š” ๊ด€๋ จ์„ฑ(Relevant)์ด ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ์†Œ๊ทธ๋ฃน ๊ฐ„์—๋Š” ์ผ๊ด€์„ฑ(Consistency)์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ, MECE์˜ ์›์น™์„ ์ค€์ˆ˜ํ•˜์—ฌ ๊ฐ ์‚ฌํ•ญ๋“ค์„ ์ž˜๊ฒŒ ๋‚˜๋ˆ„์–ด์•ผ ํ•œ๋‹ค(segmentation). ์ฆ‰, ์ „์ฒด๋ฅผ ๋ถ„์„ ๋˜๋Š” ํŒ๋‹จ์ด ์šฉ์ดํ•œ ์ž‘์€ ํ˜•ํƒœ๋กœ ๋‚˜๋ˆ„์–ด์•ผ ํ•œ๋‹ค(Divide and conquer). ๊ด‘๋ฒ”์œ„ํ•˜๊ณ  ๋ณต์žกํ•˜๊ฒŒ ์ •์˜๋œ ๋ฌธ์ œ๋“ค์„ ์ฒด๊ณ„ํ™”๋œ ๊ด€๋ จ ๋ฌธ์ œ์˜ ๋ชฉ๋ก์œผ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๊ฐ ์งˆ๋ฌธ๋“ค์€ ๊ตฌ์ฒด์ ์ด๋ฉฐ, ์™„์ „ํ•˜๊ณ , ์ง€์ ์œผ๋กœ ๊ด€๋ฆฌ ๊ฐ€๋Šฅํ•œ ์ˆ˜์ค€์˜ ์งˆ๋ฌธ๋“ค์ด ๋˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. Figure II-20. ๋ฌธ์ œ ์ •์˜์„œ์˜ ๋กœ์ง ํŠธ๋ฆฌ ๋ณ€ํ™˜ 2.2 ์šฐ์„ ์ˆœ์œ„ ์„ ์ • ๋ฐ ์ž‘์—…๊ณ„ํš ์ˆ˜๋ฆฝ ๋กœ์ง ํŠธ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ์ •์˜๋œ ๋ฌธ์ œ๋ฅผ ์„ธ๋ถ„ํ™”ํ•˜๊ณ  ๋‚˜๋ฉด ๊ทธ๊ฒƒ๋“ค ์ค‘์—์„œ ๋ฌด์—‡์„ ๋จผ์ € ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š”์ง€ ๊ฒฐ์ •ํ•ด์•ผ ํ•œ๋‹ค. ํŒŒ๋ ˆํ†  ๋ฒ•์น™(Paretoโ€™s Rule)์€ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ ์ ์ ˆํ•œ ํ•ด๋‹ต์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ์ดํƒˆ๋ฆฌ์•„์˜ ๊ฒฝ์ œํ•™์ž ๋นŒํ”„๋ ˆ๋„ ํŒŒ๋ ˆํ†  (Vilfredo Pareto. 1848 ~ 1923)๋Š” 19์„ธ๊ธฐ ์˜๊ตญ์˜ ๋ถ€(ๅฏŒ)์™€ ์†Œ๋“์˜ ์œ ํ˜•์„ ์—ฐ๊ตฌํ•˜๋‹ค๊ฐ€ ๋ถ€์˜ ๋ถˆ๊ท ํ˜• ํ˜„์ƒ์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•˜์˜€๋Š”๋ฐ ์˜๊ตญ ์ธ๊ตฌ์˜ 20%๊ฐ€ ๋ถ€์˜ 80%๋ฅผ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์ด์—ˆ๋‹ค. ํ˜„๋Œ€ ๊ฒฝ์˜์—์„œ ํŒŒ๋ ˆํ†  ๋ฒ•์น™์€ โ€˜์ „์ฒด ์„ฑ๊ณผ์˜ ๋Œ€๋ถ€๋ถ„(80%+)์ด ์†Œ์ˆ˜(20%)์— ์˜ํ•ด ์ฐฝ์ถœ๋œ๋‹คโ€™๋ผ๋Š” ์˜๋ฏธ๋กœ ๋” ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค[1]. ๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ๊ด€์ ์—์„œ๋Š” Figure II-21๊ณผ ๊ฐ™์ด ๋ถ„์„์˜ ์ •๊ตํ™”์™€ ๊ทธ ํšจ๊ณผ์˜ ๊ด€๊ณ„๋ฅผ ํŒŒ๋ ˆํ†  ๋ฒ•์น™์˜ ๊ด€์ ์—์„œ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. Figure II-21. ๋ฌธ์ œ ํ•ด๊ฒฐ์—์„œ์˜ ํŒŒ๋ž˜๋„ ๋ฒ•์น™ ์ ์šฉ ํ•œํŽธ, ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ๋ณด๋‹ค ๋” ์ค‘์š”ํ•œ์ง€ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฌธ์ œ์ ๋“ค์„ ๋‹ค์ฐจ์› ๊ด€์ (Multi-dimensional view)์—์„œ ํ‰๊ฐ€ํ•ด ๋ณด์•„์•ผ ํ•œ๋‹ค. Table II-3. ๋‹ค์ฐจ์› ํ‰๊ฐ€๋ฅผ ํ†ตํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ ์šฐ์„ ์ˆœ์œ„์˜ ์„ ์ • Table II-3์€ ๋ฌธ์ œ์ ์„ ์ค‘์š”์„ฑ, ๊ธด๊ธ‰๋„, ๋‚œ์ด๋„, ๊ฒฝ์ œ์„ฑ ๋“ฑ์œผ๋กœ ํ‰๊ฐ€ํ•˜๊ณ  ์ข…ํ•ฉํ‰๊ฐ€ํ•˜์—ฌ ์ˆœ์œ„๋ฅผ ๋งค๊ธด ๊ฒƒ์œผ๋กœ โ€˜์ƒ์‚ฐ ์™„๋ฃŒ ์ผ์ž๊ฐ€ ์˜์—…์˜ ์š”๊ตฌ ๋‚ฉ๊ธฐ๋ฅผ ๋งž์ถ”์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œโ€™๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ํ•ด๊ฒฐํ•ด์•ผ ํ•  ๋ฌธ์ œ๋กœ ๋„์ถœ๋˜์—ˆ๋‹ค. ์šฐ์„ ์ˆœ์œ„ ํ‰๊ฐ€์˜ ๊ธฐ์ค€์œผ๋กœ๋Š” ์‚ฐ์—…์ด๋‚˜ ๊ธฐ์—…์˜ ํ˜„ํ™ฉ, ๋ฌธ์ œ์˜ ์„ฑ๊ฒฉ์— ๋”ฐ๋ผ Table II-3์—์„œ ์˜ˆ๋กœ ๋“  ๊ฒƒ ์ด์™ธ์—๋„ ๋งค์šฐ ๋‹ค์–‘ํ•˜๊ฒŒ ์ •์˜๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋˜ ๊ฐ€์ค‘์น˜(weight)๋ฅผ ๋‘๊ณ  ํ‰๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ํ•  ๋ฌธ์ œ์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ  ๋‚˜๋ฉด ๊ฐ ๋ฌธ์ œ๋ณ„๋กœ ์–ด๋–ป๊ฒŒ ๋ถ„์„ํ• ์ง€ ์ž‘์—… ๊ณ„ํš(Work plan)์„ ์ˆ˜๋ฆฝํ•ด์•ผ ํ•œ๋‹ค. ์ž‘์—… ๊ณ„ํš์€ ๋‹ค์Œ 5๊ฐ€์ง€ ์›์น™์„ ์ค€์ˆ˜ํ•ด์„œ ์ˆ˜๋ฆฝํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์กฐ๊ธฐ์—(As soon as possible): ๋ฐ์ดํ„ฐ(ํŠนํžˆ, Critical Mass)๊ฐ€ ์ˆ˜์ง‘๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ๋ง๊ณ  ํ˜„์‹œ์ ์—์„œ ๋นจ๋ฆฌ ๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•จ ์ˆ˜์‹œ๋กœ(Frequently): ์ˆ˜๋ฆฝ๋œ ์ž‘์—… ๊ณ„ํš์€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ดํŽด๋ณด๋ฉด์„œ ๋ณด์™„, ๊ฐฑ์‹ , ๊ฐœ์„ ํ•ด์•ผ ํ•จ ๊ตฌ์ฒด์ ์œผ๋กœ(Specifically): ๋ถ„์„ ๋‚ด์šฉ๊ณผ ์ž๋ฃŒ์˜ ์ถœ์ฒ˜๋ฅผ ๋งค์šฐ ๊ตฌ์ฒด์ ์œผ๋กœ ๋ช…์‹œํ•จ ๊ณต๋™์œผ๋กœ(Collaboratively): ํŒ€์›๋“ค๊ณผ ๊ฒ€ํ† ํ•˜๊ณ  ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ€์„ค์„ ์‹œ๋„ํ•ด์•ผ ํ•จ ๋งˆ์ผ์Šคํ†ค(milestones)์„ ๋”ฐ๋ผ์„œ: ์ค‘์š”ํ•œ ๊ฒƒ๋ถ€ํ„ฐ ๋จผ์ € ์ถ”์ง„ํ•˜๋ฉฐ ์ฒ ์ €ํ•œ ์ผ์ •๊ด€๋ฆฌ ์ˆ˜ํ–‰ ์ž‘์—… ๊ณ„ํš(Work Plan)์„ ์ฒด๊ณ„์ ์œผ๋กœ ์ˆ˜๋ฆฝํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Table II-4์˜ ํ…œํ”Œ๋ฆฟ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. (๋ณดํ†ต ํ†ต๊ณ„์  ์ฒ˜๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์—‘์…€(Excel) ๊ฐ™์€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค) Table II-4. ์ž‘์—…๊ณ„ํš์„œ ํ…œํ”Œ๋ฆฟ ์ž‘์—… ๊ณ„ํš์„œ ํ…œํ”Œ๋ฆฟ์˜ ๊ฐ ์นผ๋Ÿผ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜๋ฏธ์ด๋‹ค. ์ด์Šˆ(Issue/Sub Issue): ์šฐ์„ ์ˆœ์œ„ํ™”(prioritization)์„ ํ†ตํ•ด ์„ ์ •๋œ ํ•ด๊ฒฐ๋˜์–ด์•ผ ํ•  ๋ฌธ์ œ ๊ฐ€์„ค(Hypothesis): ์ด์Šˆ์— ๋Œ€ํ•œ ์ดˆ๊ธฐ ๊ฐ€์„ค ๊ทผ๊ฑฐ(Supporting Rationale): ๊ฐ€์„ค์„ ์ง€์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๊ทผ๊ฑฐ ๋ถ„์„(Analysis): ๊ฐ€์„ค๊ณผ ๊ทผ๊ฑฐ์˜ ํƒ€๋‹น์„ฑ์„ ์ฆ๋ช…ํ•˜๊ธฐ ์œ„ํ•œ ์ž‘์—… ์ถœ์ฒ˜(Source): ๋ถ„์„์„ ํ•˜๊ธฐ ์œ„ํ•œ ์ •๋ณด ์›์ฒœ ์ตœ์ข… ์‚ฐ์ถœ๋ฌผ(End Products): ๋ถ„์„ ๊ฒฐ๊ณผ ์ƒ์„ฑ๋˜๋Š” ๋งˆ์ง€๋ง‰ ๊ฒฐ๊ณผ๋ฌผ ์ฑ…์ž„(Responsibility): ํ•ด๋‹น ์ž‘์—…(work)์˜ ์ฑ…์ž„์ž ๊ธฐํ•œ(Timing): ํ•ด๋‹น ์ž‘์—…(work)์„ ๋๋‚ด์•ผ ํ•˜๋Š” ์‹œ๊ฐ„ ๋˜ํ•œ, ์ž‘์—…๊ณ„ํš์„œ์—์„œ ๋ฌธ์ œ๋‚˜ ์ด์Šˆ์— ๋Œ€์‘ํ•˜๋Š” ์ข‹์€ ๊ฐ€์„ค์„ ์ˆ˜๋ฆฝํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ค‘์š”ํ•œ๋ฐ, ๋…ธ๋ ฅ (efforts)์„ ํšจ์œจ์ ์œผ๋กœ ๋ฐฐ๋ถ„ํ•˜์—ฌ ์ตœ๋Œ€์˜ ํšจ๊ณผ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ€์„ค ์ˆ˜๋ฆฝ์˜ 5๊ฐ€์ง€ ๊ณ ๋ ค ์‚ฌํ•ญ์„ ๊ฐ์•ˆํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋ช…ํ™•์„ฑ(Clarity) : ๊ฐ€์„ค ์ˆ˜๋ฆฝ ์‹œ ์ •์˜๋œ ๋ฌธ์ œ๋กœ๋ถ€ํ„ฐ ๋ถ„์„์˜ ๋Œ€์ƒ(Target)์„ ๋ช…ํ™•ํžˆ ํ•ด์•ผ ํ•œ๋‹ค. ์ •ํ™•์„ฑ(Accuracy) : ์–ด๋Š ์ •๋„ ์ •ํ™•ํ•ด์•ผ ํ• ์ง€ ์ˆ˜์ค€์„ ์‚ฌ์ „์— ์ •ํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋ฉฐ ๊ณผ๋„ํ•œ ์ •ํ™•์„ฑ์€ ์ง€์–‘ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ„๊ฒฐ์„ฑ(Simplicity) : ๊ฐ€์„ค์€ ๋…ผ๋ฆฌ์ ์ธ ์ด์„ฑ์œผ๋กœ ์ทจ๊ธ‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜์ค€์—์„œ ์ตœ๋Œ€ํ•œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์˜ค์ปด์˜ ๋ฉด๋„๋‚ [2] ์‹คํ˜„์„ฑ(Actionable) : ๊ฐ€์„ค์€ ์‚ฌ๊ณ (ๆ€่€ƒ) ์‹คํ—˜์— ๊ทธ์น˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐ€์„ค์ด ์ž…์ฆ๋˜๋ฉด ์–ธ์ œ๋“ ์ง€ ์‹คํ–‰ ๊ฐ€๋Šฅํ•ด์•ผ ํ•œ๋‹ค. ์‹œ๊ธฐ ์ ์ ˆ์„ฑ(Timeliness) : ๊ฐ€์„ค์€ ์ฃผ์–ด์ง„ ์‹œ๊ฐ„์„ ๊ณ ๋ คํ•ด์„œ ์ •ํ•ด์ง„ ๊ธฐ๊ฐ„ ์•ˆ์— ์ข…๋ฃŒํ•˜์—ฌ ํšจ์œจ์„ฑ์„ ์œ ์ง€ํ•ด์•ผ ํ•œ๋‹ค. ์œ„์™€ ๊ฐ™์€ 5๊ฐ€์ง€ ๊ณ ๋ ค ์‚ฌํ•ญ์„ ์œ ๋…ํ•˜์—ฌ ๊ฐ€์„ค์„ ์ˆ˜๋ฆฝํ•ด ๋ณด๋ฉด์„œ ๋ฐฉ๋ฒ•๋ก ์  ๊ด€์ ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์€ ๊ฐ€์„ค์„ ์ˆ˜๋ฆฝํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋œ๋‹ค. ์šฐ์„ ์ˆœ์œ„ ์„ ์ •์„ ํ†ตํ•ด ๋„์ถœ๋œ ํ•ต์‹ฌ ๋ฌธ์ œ์— ๋Œ€ํ•ด ์ปจ์„คํŒ… ํŒ€์›๋“ค๊ณผ ๊ฐ™์ด ์ด์•ผ๊ธฐํ•ด ๋ณธ๋‹ค. ๋‹ค์ฟ ์น˜ ๊ธฐ๋ฒ•(5 Whys)์„ ์ด์šฉํ•˜์—ฌ ๊ฐ€์žฅ ๊ทผ๋ณธ์ ์ธ ์›์ธ(root cause)์„ ์ฐพ์•„๋ณธ๋‹ค. ์ฃผ์–ด์ง„ ์ •๋ณด๋กœ ๊ฐ€์„ค์— ๋Œ€ํ•œ ๊ฒ€์ฆ์„ ์‹œ๋„ํ•ด ๋ณธ๋‹ค. ๊ทผ๊ฑฐ(rationale)๊ฐ€ ๋ถ€์กฑํ•˜๋”๋ผ๋„ ์™„๋ฒฝํ•œ ๊ฒ€์ฆ์ด ๋˜์—ˆ์„ ๊ฒฝ์šฐ, ์–ด๋–ค ๋ชจ์Šต์ผ์ง€ ์ƒ์ƒํ•˜๋ฉด์„œ ์ง€์†์ ์œผ๋กœ ๊ฐ€์„ค๊ณผ ๊ทผ๊ฑฐ๋ฅผ ๋ณด์™„ํ•œ๋‹ค. ๋ณ‘๋ ฌ์  ์‚ฌ๊ณ  ๋˜๋Š” ์ˆ˜ํ‰์  ์‚ฌ๊ณ  ์ฆ‰, ๋‹ค๋ฅธ ์ฐจ์›์ด๋‚˜ ๋‹ค๋ฅธ ์ธก๋ฉด์—์„œ ๋ฌธ์ œ๋ฅผ ์ƒ๊ฐํ•ด ๋ณธ๋‹ค.(Lateral Thinking) ํ˜์‹ ์ ์ธ ์•„์ด๋””์–ด๋ฅผ ์ฐพ์•„๋‚ด๊ธฐ ์œ„ํ•˜์—ฌ ์‹œ๋„ํ•˜๋Š” โ€˜๋ณ‘๋ ฌ์  ์‚ฌ๊ณ โ€™ ๋˜๋Š” โ€˜์ˆ˜ํ‰์  ์‚ฌ๊ณ โ€™๋Š” ํ”ํžˆ โ€˜์ˆฒ์†์—์„œ ๋‚˜๋ฌด๋ฅผ ๋ณด๋Š” ๋ฐฉ๋ฒ•โ€™์ด๋ผ๊ณ ๋„ ์ด์•ผ๊ธฐํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋ฉด, โ€œ๊นŠ์ด 5m์˜ ๊ตฌ๋ฉ์ด๋ฅผ ๋‘ ๋ช…์ด ํŒŒ๋ฉด 1์‹œ๊ฐ„ ๊ฑธ๋ฆฐ๋‹ค๊ณ  ํ•  ๋•Œ ์—ด ๋ช…์ด 2์‹œ๊ฐ„ ํŒŒ๋ฉด ์–ผ๋งˆ๋‚˜ ๊นŠ์ด ํŒ” ์ˆ˜ ์žˆ์„๊นŒ?โ€ ์ด๋Ÿฐ ์งˆ๋ฌธ์— ์ผ๋ฐ˜์ ์ธ ๋Œ€๋‹ต์€ 2๋ช…/1์‹œ๊ฐ„์ผ ๋•Œ 5m์ด๋‹ˆ 10๋ช…/2์‹œ๊ฐ„์ด๋ฉด 5๋ช…/1์‹œ๊ฐ„์œผ๋กœ 5m X 5๋ช…/1์‹œ๊ฐ„ = 25m ๋ญ ์ด๋Ÿฐ ๋‹ต์„ ํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Lateral Thinking์„ ํ•œ๋‹ค๋ฉด ๋Œ€๊ฒŒ ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆ˜๋งŽ์€ ์ด์•ผ๊ธฐ๋“ค์ด ๋‚˜์˜ค๋ฉฐ ์ƒํ™ฉ์— ๋งž๋Š” ๋…ผ์˜๋ฅผ ํ†ตํ•ด ์‚ฌ๊ณ ๋ฅผ ํ™•๋Œ€ํ•ด ๋‚˜๊ฐ€๋Š” ๊ณผ์ •์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ๋‹ค๋งŒ, ์ผ์ด ๋˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๋‚ฉ๊ธฐ๋ฅผ ๊ณ ๋ คํ•ด์„œ ์ƒ๊ฐํ•ด๋‚˜๊ฐ€์•ผ๋งŒ ํ•˜์ง€ ๊ทธ๋ ‡์ง€ ๋ชปํ•˜๋ฉด ๋ฐฐ๊ฐ€ ์‚ฐ์œผ๋กœ ๊ฐ„๋‹ค. ๊ตฌ๋ฉ์€ ํŠน์ • ๋ชจ์–‘ ๋˜๋Š” ํฌ๊ธฐ๋กœ ํŒŒ์•ผ ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๊นŠ์ด์˜ ์ œํ•œ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ตฌ๋ฉ์ด ๊นŠ์–ด์งˆ์ˆ˜๋ก ํž˜์ด ๋” ๋“ค๊ณ  ์‹œ๊ฐ„๋„ ๋” ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‚˜์˜ค๋Š” ํ™์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค๊ฑฐ๋‚˜, ์•”๋ฐ˜์ด๋‚˜ ์ง€ํ•˜์ˆ˜ ์ธต์ด ๋‚˜์˜ค๋Š” ์ƒํ™ฉ์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ํŒŒ๋‚ด์–ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด ํ™, ์ง„ํ™, ๋ชจ๋ž˜ ๋“ฑ ๋ฌด์—‡์ด๋ƒ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค. ์‚ฐ์—์„œ ๊ตฌ๋ฉ์„ ํŒ” ๋•Œ, ์ฒ˜์Œ ๋ช‡ ๋ฏธํ„ฐ๋Š” ๋‚˜๋ฌด๋ฟŒ๋ฆฌ ์ œ๊ฑฐ ๋“ฑ์œผ๋กœ ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆฌ๋‚˜ ๋‚˜์ค‘์—๋Š” ํ™๋งŒ ์žˆ์–ด ์‰ฌ์šธ ์ˆ˜๋„ ์žˆ๋‹ค. ์ž‘์—… ๊ณต๊ฐ„์˜ ์ œ์•ฝ์œผ๋กœ ์ธํ•ด ํ•œ ๋ฒˆ์— ๋™์‹œ์— ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์€ 3๋ช…์„ ๋„˜์ง€ ๋ชปํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์‚ฌ๋žŒ์ด ๋งŽ์•„์ ธ์„œ ์žก๋‹ด ๋“ฑ์œผ๋กœ ์ƒ์‚ฐ์„ฑ์ด ๋–จ์–ด์งˆ ์ˆ˜๋„ ์žˆ๋‹ค. ์‚ฌ๋žŒ์ด ๋งŽ์„์ˆ˜๋ก ํž˜์ด ๋น ์งˆ ๋•Œ ๊ต๋Œ€ํ•˜๊ธฐ ์‰ฌ์›Œ์„œ ๋” ๋งŽ์€ ์ƒ์‚ฐ์„ฑ์ด ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค. ๋‚ ์”จ์— ๋”ฐ๋ผ ์ƒ์‚ฐ์„ฑ์ด ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋น„๊ฐ€ ์˜จ๋‹ค๊ฑฐ๋‚˜ ๋‚ ์ด ๊ฐ‘์ž๊ธฐ ๋ฅ๊ฑฐ๋‚˜ ์ถ”์–ด์ง€๋Š” ์ƒํ™ฉ ๊ตฌ๋ฉ์„ ๋ช‡ ๊ฐœ ํŒŒ๋Š”๊ฐ€์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์งˆ ์ˆ˜๋„ ์žˆ๋‹ค. ์‚ฌ๋žŒ์˜ ์ฒด๋ ฅ ์ฐจ์ด๋กœ ์ธํ•ด ์ƒ์‚ฐ์„ฑ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ํ•œ ์‚ฌ๋žŒ์€ ๋งค๋‹ˆ์ €๋กœ์„œ ์ž‘์—…์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ...... 10 ์‚ฌ๋žŒ์ด ์ผํ•  ๋•Œ๋Š” ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋ถ„์—…์„ ํ†ตํ•ด ์ƒ์‚ฐ์„ฑ์„ ๋ณ€ํ™”์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ๋‹ค. Break #5. ์ œํผ์Šจ ๊ธฐ๋…๊ด€ ์ด์•ผ๊ธฐ ๋ฏธ๊ตญ ์›Œ์‹ฑํ„ด D.C.์—๋Š” ๋ฏธ๊ตญ ๋…๋ฆฝ์˜ ์˜์›…์ด์ž ์ œ3๋Œ€ ๋Œ€ํ†ต๋ น ํ† ๋งˆ์Šค ์ œํผ์Šจ(Thomas Jefferson. 1743 ~ 1826)์„ ๊ธฐ๋…ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“  ์ œํผ์Šจ ๊ธฐ๋…๊ด€(Thomas Jefferson Memorial)์ด ์žˆ๋‹ค. Figure II-22. ์ œํผ์Šจ ๊ธฐ๋…๊ด€ ์ „๊ฒฝ Figure II-22์™€ ๊ฐ™์ด ์ด์˜ค๋‹ˆ์•„์‹ ๋”(Dome) ๊ตฌ์กฐ์˜ ์›ํ˜• ๋Œ€๋ฆฌ์„ ๊ฑด์ถ•๋ฌผ๋กœ ์›Œ์‹ฑํ„ด์˜ ์œ ๋ช… ๊ฑด์ถ•๋ฌผ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ƒˆํ•˜์–€ ๋Œ€๋ฆฌ์„์œผ๋กœ ๋งŒ๋“ค์–ด์กŒ๊ณ  ์ฃผ์•ผ๋กœ ๋ถˆ์„ ๋ฐํžˆ๊ณ  ์žˆ์–ด ๊ทธ ํ’๊ฒฝ๋„ ๋งค์šฐ ์•„๋ฆ„๋‹ต๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ์ œํผ์Šจ ๊ธฐ๋…๊ด€์ด ์–ธ์ œ๋ถ€ํ„ฐ์ธ์ง€ ๋Œ€๋ฆฌ์„์ด ๋งค์šฐ ๋น ๋ฅธ ์†๋„๋กœ ๋ถ€์‹๋˜์–ด๊ฐ€๊ฒŒ ๋˜์–ด ํ•˜์–€ ๋Œ€๋ฆฌ์„์ด ๊ฒ€๊ฒŒ ๋ณ€ํ•˜์ž ๊ด€๋ฆฌ์†Œ์žฅ์€ ๊ณ ๋ฏผ์— ๋น ์ง€๊ฒŒ ๋œ๋‹ค. ์ฒญ์†Œ ๊ด€๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ์ง์›๋“ค๊ณผ ๊ด€๋ฆฌ์†Œ์žฅ์€ ์ด์— ๋Œ€ํ•ด ์„œ๋กœ ์งˆ์˜์‘๋‹ตํ•˜๋ฉฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์‹œ์ž‘ํ•˜์˜€๋‹ค. ์ด๋ฅผ ์ข€ ์ •๋ฆฌํ•ด ๋ณด๋ฉด Table II-5์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์งˆ๋ฌธ์˜ ๋‹จ๊ณ„๊ฐ€ ๊นŠ์–ด์งˆ์ˆ˜๋ก ์›์ธ ๋ถ„์„๋„ ์‹ฌํ™”๋˜์–ด๊ฐ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. Table II-5. ๊ด€๋ฆฌ์†Œ์žฅ๊ณผ ์ฒญ์†Œ๋‹ด๋‹น ์ง์›๋“ค ๊ฐ„์˜ ๋Œ€ํ™” ์žฌ๊ตฌ์„ฑ ๊ด€๋ฆฌ์†Œ์žฅ๊ณผ ์ง์›๋“ค์€ ๋ธŒ๋ ˆ์ธ์Šคํ† ๋ฐ์„ ํ†ตํ•ด ๋Œ€๋ฆฌ์„์ด ๋นจ๋ฆฌ ๋ถ€์‹๋˜๋Š” ๊ทผ๋ณธ์ ์ธ ์›์ธ์„ ๋งŽ์€ ๋‚˜๋ฐฉ์˜ ์ถœ๋ชฐ์ด๋ผ๊ณ  ๊ฒฐ๋ก ์ง“๊ณ  ์ ๋“ฑ ์‹œ๊ฐ„์„ ํ•ด ์งˆ ๋ฌด๋ ต์ด ์•„๋‹ˆ๋ผ ๊ทธ๋ณด๋‹ค 2์‹œ๊ฐ„ ๋Šฆ์ถ”์–ด ๋“ฑ์„ ์ผœ์„œ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค๊ณ  ํ•œ๋‹ค. ์ตœ์†Œํ•œ 5๋ฒˆ์˜ โ€˜์™œ?โ€™๋ผ๋Š” ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์„ ์ฐพ๋Š” ๊ณผ์ •(5-Whys. ํƒ€์ฟ ์น˜ ๊ธฐ๋ฒ•์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค)์„ ํ†ตํ•ด ๊ทผ๋ณธ์ ์ธ ๋ฌธ์ œ์ ์˜ ์›์ธ(Root Cause)์„ ์ฐพ๊ณ  ๊ทธ๊ฒƒ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์„ ์ฐพ์•„ ๋‚˜๊ฐ€๋Š” ์ด๋Ÿฐ ์‚ฌ๊ณ  ๊ธฐ๋ฒ•์€ ๋ฌธ์ œ๋ฅผ ๋งค์šฐ ํšจ์œจ์ ์œผ๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค€๋‹ค. ๋‹ค๋งŒ, ์ €์ž์˜ ๊ฒฝํ—˜์— ์˜ํ•˜๋ฉด 5-Whys๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ทธ ์ด์œ ๋ฅผ โ€˜ํŠน์ •์ธโ€™์— ์ง‘์ค‘ํ•˜๋ฉด ๋ฌธ์ œ์˜ ๊ทผ์›์  ํ•ด๊ฒฐ๋ณด๋‹ค๋Š” ๊ทธ ์‚ฌ๋žŒ์— ๋Œ€ํ•œ ๊ฐ์ •์  ๋ณด๋ณต์œผ๋กœ ๊ท€๊ฒฐ๋  ํ™•๋ฅ ์ด ํฌ๋‹ค. ๋ฌผ๋ก , ๊ฐ์ •์ ์œผ๋กœ ํœ˜๋‘˜๋ฆด ๋•Œ๋„ ์žˆ๊ณ  ์ •๋ง ๊ทธ๊ฒƒ์ด ์›์ธ์ด ๋  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ์‚ฌ๋žŒ์œผ๋กœ ๊ท€๊ฒฐ๋˜์–ด ๊ฐ์ •์ด ๋”ํ•ด์ง€๋ฉด ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜๋Š” ๋“ฏ์‹ถ์–ด๋„ ์‹œ๊ฐ„์ด ํ˜๋Ÿฌ ๋” ์•…ํ™”๋˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋” ๋งŽ๋‹ค. ์„ค์‚ฌ ๊ทธ โ€˜ํŠน์ •์ธโ€™์ด ์ •๋ง๋กœ ๋ฌธ์ œ์˜€๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๊ทธ๊ฐ€ ๋งก๊ณ  ์žˆ๋Š” ์ง€์œ„์™€ ์—ญํ• ์˜ ๋ฌธ์ œ์ ์œผ๋กœ ์ด๋ฅผ ๋ฐ”๋ผ๋ณด๊ณ  ๊ฐœ์„ ํ•˜๊ณ ์ž ๋…ธ๋ ฅํ•˜๋Š” ๊ฒƒ์ด ๋™์ผํ•œ ๋ฌธ์ œ๋ฅผ ์•ผ๊ธฐํ•˜์ง€ ์•Š๋Š” ํ˜„๋ช…ํ•œ ํ•ด๊ฒฐ์ฑ…์ด๋ผ๋Š” ์ ์„ ๋ช…์‹ฌํ•˜์ž. [1] ์—ฌ๊ธฐ์„œ ํ•ต์‹ฌ์ด ๋˜๋Š” ์†Œ์ˆ˜๋ฅผ โ€˜Vital Fewโ€™, ๋‚˜๋จธ์ง€๋ฅผ โ€˜Trivial Manyโ€™๋ผ๊ณ ๋„ ํ•œ๋‹ค. [2] Occam's razor. ์–ด๋–ค ํ˜„์ƒ์ด๋‚˜ ๋…ผ๋ฆฌ๋ฅผ ์„ค๋ช…ํ•  ๋•Œ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ๊ฒƒ์ด ์ง„๋ฆฌ์ผ ํ™•๋ฅ ์ด ๋†’๋‹ค๋Š” ์˜๋ฏธ. ๋‹จ์ˆœ์„ฑ์˜ ์›์น™(The Principle of Simplicity) ๋˜๋Š” ๋…ผ๋ฆฌ ์ ˆ์•ฝ์˜ ์›์น™(The Principle of Parsimony) ๋“ฑ์œผ๋กœ ๋ถˆ๋ฆฐ๋‹ค. ๋ฌธ์ œ ํ•ด๊ฒฐ๊ธฐ๋ฒ•์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๊ณ  ์žˆ๋‹ค. ๋ฌธ์ œ์˜ ์ •์˜๊ฐ€ ์ค‘์š”ํ•˜๋‹ค๊ณ  ํ•˜์˜€๊ณ  ๊ฐ€์„ค์„ ์ž˜ ์ˆ˜๋ฆฝํ•˜๊ณ  ๋ฌธ์ œ์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ  ๊ทธ๊ฒƒ์— ๋”ฐ๋ฅธ ์ž‘์—…๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•˜๋Š” ๊ฒƒ๊นŒ์ง€ ์‚ดํŽด๋ณด์•˜๋‹ค. ์ด์–ด์„œ ์ด๋ฒˆ์—๋Š” ํ•ด๋‹น ๊ฐ€์„ค์— ๋Œ€ํ•œ ๋ถ„์„ ๋ฐ ๊ฒฐ๊ณผ ์ข…ํ•ฉ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž. 5.3 ๋ถ„์„ ๋ฐ ๊ฒฐ๊ณผ ์ข…ํ•ฉ ์ž‘์—… ๊ณ„ํš(Work Plan) ์ˆ˜๋ฆฝ์ด ๋๋‚˜๋ฉด ๊ฐ ๋ฌธ์ œ๋ฅผ ๋‘๊ณ  ๋ถ„์„ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ฐจ๋ก€์ด๋‹ค. ์—ฌ๊ธฐ์„œ ๋ถ„์„ ์ž‘์—…์ด๋ผ ํ•จ์€ ๋…ผ๋ฆฌ์  ๋ถ„์„ ์ž‘์—…์ธ๋ฐ ์ด๋Š” ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ๊ทธ๊ฒƒ์˜ ๊ฒ€์ฆ์„ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๋Š” ๊ณผํ•™์  ๋ถ„์„ ์ž‘์—…๊ณผ ๋‹ฌ๋ฆฌ ๊ฐ€์„ค์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋…ผ๋ฆฌ์ ์ธ ๊ฒฐํ•จ์„ ๋ณด์™„ ๋ฐ ๊ฒ€์ฆํ•ด ๋‚˜๊ฐ€๋Š” ๋ถ„์„ ์ž‘์—…์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. Figure II-22๋Š” ๊ทธ๋Ÿฐ ์ž‘์—…์—์„œ ํ•„์š”ํ•œ ๋…ผ๋ฆฌ์  ๋ถ„์„ ์ž‘์—…์˜ ๋„๊ตฌ๋“ค์ด๋‹ค. Figure II-22. ๋ฌธ์ œ ๋ถ„์„์„ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋„๊ตฌ '๊ฐ„๋‹จํ•œ To Do List'๋Š” ์•„์ดํ…œ(Item)๊ณผ ์ฑ…์ž„์ž, ์™„๋ฃŒ์ผ ๋“ฑ์„ ๊ธฐ์ค€์œผ๋กœ ํ•ด์•ผ ํ•  ์ผ๋“ค์„ ๋‚˜์—ดํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ผ ๋‹จ์œ„๋กœ ํ•  ์ผ๋“ค์„ ์ •๋ฆฌํ•˜์—ฌ<NAME>๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 'Critical Path'๋Š” ๋‹จ๊ณ„์ ์œผ๋กœ ์ผ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•  ๊ฒƒ๋“ค์„ ๊ฒฝ๋กœ(Path)๋กœ ์—ฐ๊ฒฐํ–ˆ์„ ๋•Œ ์†Œ์š”๋˜๋Š” ๊ธฐ๊ฐ„์„ ํŒŒ์•…ํ•˜์—ฌ ์ „์ฒด ๊ณต์ •์„ ๊ด€๋ฆฌํ•˜๋Š” ๋„๊ตฌ์ด๋‹ค. '๋…ผ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ'์™€ '์Šคํ† ๋ฆฌ๋ณด๋“œ'๋Š” ๋…ผ๋ฆฌ์ ์ธ ๊ธ€์“ฐ๊ธฐ ๋ฐ ๋ณด๊ณ ์„œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ธฐ์ดˆ ์ž๋ฃŒ๊ฐ€ ๋˜๋ฉฐ 'Custom Format'์€ Table II-4 ์ž‘์—…๊ณ„ํš์„œ ํ…œํ”Œ๋ฆฟ์ฒ˜๋Ÿผ ๊ทธ๋•Œ๊ทธ๋•Œ ํ•„์š”ํ•œ ์–‘์‹์„ ์ƒํ™ฉ์— ๋งž๊ฒŒ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ๊ธฐ๋ณธ ํ‹€๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํŽ˜๋ฅด๋ฏธ ์ถ”์ •(Fermi Estimation) ๊ฐ™์€ ๊ธฐ๋ฒ•๋„ ๋…ผ๋ฆฌ์  ๋ถ„์„์—์„œ ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค Break #6. ํŽ˜๋ฅด๋ฏธ ์ถ”์ •(Fermi Problem or Fermi Estimate) ์—”๋ฆฌ์ฝ” ํŽ˜๋ฅด๋ฏธ(Enrico Fermi. 1901 ~ 1954)๋Š” ์ดํƒˆ๋ฆฌ์•„๊ณ„ ๋ฏธ๊ตญ์ธ ํ•ต๋ฌผ๋ฆฌํ•™์ž๋กœ ํ˜„๋Œ€ ๋ฌผ๋ฆฌํ•™์˜ ๊ฑฐ์žฅ์ด๋‹ค. ๊ทธ๋Š” ๊ธฐ์ดˆ์ ์ธ ์ง€์‹๊ณผ ๋…ผ๋ฆฌ์ ์ธ ์ถ”๋ก ์„ ํ†ตํ•ด ์งง์€ ์‹œ๊ฐ„ ์•ˆ์— ๋Œ€๋žต์˜ ๊ทผ์‚ฌ์น˜๋ฅผ ๊ณ„์‚ฐํ•ด ๋‚ด๋Š”๋ฐ ๋งค์šฐ ๋Šฅํ†ตํ–ˆ๋‹ค. ์ด๋ฅผ ๋”ฐ์„œ โ€˜ํŽ˜๋ฅด๋ฏธ ์ถ”์ •โ€™์ด๋ผ๋Š” ์šฉ์–ด๊ฐ€ ์ƒ๊ฒผ๋Š”๋ฐ ์ด๊ฒƒ์€ ๋งค๋…„ ์—ฌ๋ฆ„ ๋ถ€์‚ฐ ํ•ด์šด๋Œ€ ํ•ด์ˆ˜์š•์žฅ์˜ ์ธํŒŒ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด๋ผ๋“ ์ง€ ์šฐ๋ฆฌ์˜ ์ผ์ƒ์ƒํ™œ ๊ณณ๊ณณ์—์„œ ์˜์™ธ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. ๋…ผ๋ฆฌ์ ์ธ ์‚ฌ๊ณ ๋ฅผ ์›ํ•˜๋Š” ์ง์žฅ ํŠนํžˆ, ์ปจ์„คํŒ… ๊ธฐ์—…์— ์ž…์‚ฌํ•˜๊ฒŒ ๋˜๋ฉด ๋ฉด์ ‘์—์„œ 100% ๋ฌผ์–ด๋ณด๋Š” ๊ฒƒ์ด ํŽ˜๋ฅด๋ฏธ ์ถ”์ •์ด๋‹ค. ์งˆ๋ฌธ๋“ค์€ ์–ธ๋œป ๋“ค์–ด๋ณด๋ฉด ๋งค์šฐ ํ™ฉ๋‹นํ•˜๋‹ค. ์ •๋‹ต๋„ ์—†๋‹ค. ์‹ฌ์ง€์–ด ๋ฉด์ ‘๊ด€๋“ค๋„ ์ •๋‹ต์„ ๋ชจ๋ฅธ๋‹ค. ์งˆ๋ฌธ์˜ ์š”์ง€๋Š” ์ •๋‹ต์ด ์ค‘์š”ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  ๊ทธ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋‚˜๊ฐ€๋Š” ๊ณผ์ •์ด ์–ผ๋งˆ๋‚˜ ๋…ผ๋ฆฌ์ ์ด๊ณ  ํƒ€๋‹นํ•œ์ง€๋ฅผ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ์ˆ˜๋งŽ์€ ํŽ˜๋ฅด๋ฏธ ์ถ”์ • ๋ฌธ์ œ๋“ค์ด ์žˆ๊ฒ ์ง€๋งŒ Break #6์—์„œ๋Š” ๊ทธ ์œ ๋ช…ํ•œ 'ํ”ผ์•„๋…ธ ์กฐ์œจ์‚ฌ ๋ฌธ์ œ'๋ฅผ ๋‹ค๋ฃจ์–ด๋ณด์ž. ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. '๋ฏธ๊ตญ ์‹œ์นด๊ณ ์— ํ”ผ์•„๋…ธ ์กฐ์œจ์‚ฌ๊ฐ€ ๋ช‡ ๋ช… ์‚ด๊ณ  ์žˆ์„๊นŒ? (How many piano tuners are there in Chicago?)' ์ด๊ฒŒ ๋์ด๋‹ค. ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์€ ์ด์ œ๋ถ€ํ„ฐ๋Š” ์ด ๊ธ€์„ ์ฝ๋Š” ๋…์ž๋‹˜๋“ค ๋งˆ์Œ๋Œ€๋กœ ๋‹ตํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋กœ์ง ํŠธ๋ฆฌ(Logic Tree)๋ฅผ ์ ‘ํ•œ ์šฐ๋ฆฌ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ์ ‘๊ทผํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค. ์šฐ์„ , ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ๊ฐ€์ •์„ ํ•ด๋ณด์ž. ๋ฏธ๊ตญ ์‹œ์นด๊ณ ์—๋Š” ์•ฝ 500๋งŒ ๋ช…์ด ์‚ด๊ณ  ์žˆ๋‹ค. ํ•œ ๊ฐ€๊ตฌ์—๋Š” ํ‰๊ท  2๋ช…์ด ์‚ด๊ณ  ์žˆ๋‹ค. ์ •๊ธฐ์ ์œผ๋กœ ํ”ผ์•„๋…ธ ์กฐ์œจ์„ ๋ฐ›๋Š” ๊ฐ€๊ตฌ๋Š” ๋Œ€๋žต 20๊ณณ ์ค‘์˜ ํ•˜๋‚˜์ด๋‹ค. ์ •๊ธฐ์ ์œผ๋กœ ์กฐ์œจ ๋ฐ›๋Š” ํ”ผ์•„๋…ธ๋Š” ํ‰๊ท  ์ผ ๋…„์— ํ•œ ๋ฒˆ ์กฐ์œจํ•œ๋‹ค. ํ”ผ์•„๋…ธ ์กฐ์œจ์‚ฌ๊ฐ€ ํ”ผ์•„๋…ธ 1๋Œ€๋ฅผ ์กฐ์œจํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„์€ ์•ฝ 2์‹œ๊ฐ„์ด๋‹ค. ํ”ผ์•„๋…ธ ์กฐ์œจ์‚ฌ๋Š” ํ•˜๋ฃจ ํ‰๊ท  8์‹œ๊ฐ„, ์ผ์ฃผ์ผ์— 5์ผ, ์ผ ๋…„์— 50์ฃผ ์ผํ•œ๋‹ค. ์ด๋Ÿฐ ๊ฐ€์ •์„ ํ•˜๋ฉด ์‹œ์นด๊ณ ์— ๊ฑฐ์ฃผํ•˜๋Š” ํ”ผ์•„๋…ธ ์กฐ์œจ์‚ฌ์˜ ์ˆ˜๋Š” 125๋ช…์œผ๋กœ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์žฅ์œผ๋กœ๋งŒ ํ‘œํ˜„ํ•˜๋‹ˆ ์ž˜ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ์ด๊ฒƒ์„ ๋กœ์ง ํŠธ๋ฆฌ๋กœ ๊ทธ๋ ค๋ณด๋ฉด Figure II-23๊ณผ ๊ฐ™๋‹ค. Figure II-23. ํ”ผ์•„๋…ธ ์กฐ์œจ์‚ฌ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋กœ์ง ํŠธ๋ฆฌ ์œ„์—์„œ ์„ค๋ฆฝํ•œ ๊ฐ€์ •(Assumption)์— ๊ทผ๊ฑฐํ•˜์—ฌ ์ธ๊ตฌ ์ˆ˜(500๋งŒ ๋ช…)์—์„œ ๊ฐ€๊ตฌ๋‹น ์ธ๊ตฌ ์ˆ˜(2๋ช…)๋ฅผ ๋‚˜๋ˆ„๋ฉด ์ „์ฒด ๊ฐ€๊ตฌ ์ˆ˜(250๋งŒ ๊ฐ€๊ตฌ)๊ฐ€ ๋‚˜์˜จ๋‹ค. ์—ฌ๊ธฐ์— ๊ฐ€๊ตฌ๋‹น ํ”ผ์•„๋…ธ ๋ณด๊ธ‰๋ฅ  5%(=1/20)๋ฅผ ๊ณฑํ•˜๋ฉด ํ”ผ์•„๋…ธ ๋Œ€์ˆ˜๊ฐ€ ๋‚˜์˜ค๊ณ (12.5๋งŒ ๋Œ€), ํ”ผ์•„ ๋…ธ๋‹น ์—ฐ๊ฐ„ ์กฐ์œจ ํšŒ์ˆ˜๋ฅผ ๊ณฑํ•˜๋ฉด ์—ฐ๊ฐ„ ํ”ผ์•„๋…ธ ์กฐ์œจ ํšŒ์ˆ˜๊ฐ€ ๋‚˜์˜จ๋‹ค. ํ•œํŽธ, 1์ผ ์กฐ์œจ ์ˆ˜์™€ ์—ฐ๊ฐ„ ์ผํ•˜๋Š” ์ผ์ˆ˜๋ฅผ ๊ณฑํ•˜๋ฉด ์กฐ์œจ์‚ฌ๊ฐ€ ์—ฐ๊ฐ„ ์กฐ์œจํ•˜๋Š” ํšŒ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋ฉฐ ์•ž์„œ ๊ตฌํ•œ ์—ฐ๊ฐ„ ํ”ผ์•„๋…ธ ์กฐ์œจ ํšŒ์ˆ˜๋ฅผ ์กฐ์œจ์‚ฌ๊ฐ€ ์—ฐ๊ฐ„ ์กฐ์œจํ•˜๋Š” ํšŒ์ˆ˜๋กœ ๋‚˜๋ˆ„๋ฉด ์กฐ์œจ์‚ฌ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜จ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ฐ€์ •์— ์˜ํ•ด ์ถœ๋ฐœํ•œ ๊ฒƒ์ด์ง€๋งŒ ๋‚˜๋ฆ„์˜ ๋…ผ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ณ„์‚ฐํ•ด ๋‚ด๋Š” ๊ฒƒ์— ํŽ˜๋ฅด๋ฏธ ์ถ”์ •์€ ์˜์˜๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜๊ฒ ๋‹ค. ํŽ˜๋ฅด๋ฏธ ์ถ”์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์ด ์žˆ๋‹ค. ๋ฌธ์ œ์—๋Š” ์ œํ•œ๋œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง ์ฃผ์–ด์ง„ ์ œํ•œ๋œ ์ •๋ณด ๋•Œ๋ฌธ์— ๋‹ต์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์–ด๋ ค์›Œ ๋ณด์ด๊ณ  ๋”ฐ๋ผ์„œ ๋‹ค์–‘ํ•œ ๊ฐ€์ •๋“ค์„ ํ†ตํ•ด์„œ ๋‹ต์„ ์œ ์ถ”ํ•œ๋‹ค. ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•จ. (BOTE[1]) ๊ทผ์‚ฌ์ ์œผ๋กœ ๊ณ„์‚ฐํ•จ ๊ทผ์‚ฌ์  ๊ณ„์‚ฐ ๋ฐ ์ถ”์ •์„ ํ†ตํ•ด ๋ฌธ์ œ์— ๋Œ€ํ•œ ํ†ต์ฐฐ๋ ฅ์„ ์ œ๊ณตํ•จ ์–‘์˜ ๋Œ€๋žต์ ์ธ ํฌ๊ธฐ(Order of Magnitude)๊ฐ€ ์ค‘์š”ํ•จ ํฌ๊ธฐ๊ฐ€ 10์˜ ๋ช‡ ์Šน(Power of Ten)์ธ๊ฐ€๊ฐ€ ๊ฒฐ์ •ํ•จ ๊ฒ€์ฆ์„ ํ†ตํ•ด ๋‹ต์ด ํƒ€๋‹นํ•˜๋‹ค(reasonable) ๋Š” ๊ฒƒ์„ ๋ณด์ž„ ๋ณต์žกํ•œ ๋ฌธ์ œ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ ๊ฒ€ํ•˜๋Š”๋ฐ ์œ ์šฉํ•จ ๋” ์ •ํ™•ํ•˜๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ค ๋ถ€๋ถ„(๋˜๋Š” ์–ด๋–ค ๊ฐ’)์„ ๋” ์ž˜ ์ดํ•ดํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๊ฒŒ ๋จ ์ƒ์ƒ์˜ ๋‚˜๋ž˜๋ฅผ ํŽผ์น˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ต์–‘ ์žˆ๋Š” ์ถ”์ธก(educated guess)์„ ์ด์šฉํ•˜์—ฌ ๋…ผ๋ฆฌ์ ์ธ ์ƒ์ƒ(Logical Imagination)์„ ์ „๊ฐœํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•จ ์•ž์„œ ๋ฌธ์ œ์˜ ์šฐ์„ ์ˆœ์œ„(Priority)๋ฅผ ์ •ํ•˜๋ฉด์„œ๋„ ๋‹ค๋ฃจ์—ˆ์ง€๋งŒ ๋…ผ๋ฆฌ์  ๋ถ„์„์— ์žˆ์–ด์„œ ํšจ์œจ์ ์œผ๋กœ, ํšจ๊ณผ์ ์œผ๋กœ ์ผํ•ด์•ผ ํ•œ๋‹ค. ์˜๋ฏธ๊ถŒ ์„ ๋ฐฐ ์ปจ์„คํ„ดํŠธ๋“ค๋กœ๋ถ€ํ„ฐ ์ „ํ•ด์ง€๋Š” ๋‹ค์Œ ํŒ(Tips)์€ ๊ทธ๋ž˜์„œ ์˜๋ฏธ ์žˆ๋‹ค. Always Have An Answer: ์ฃผ์–ด์ง„ ๋ฌธ์ œ์— ๋Œ€ํ•œ ๊ฐ€์„ค์„ ์„ธ์šฐ๊ณ , ๊ทธ๊ฒƒ์ด ๋งž๋Š”์ง€ ๊ทผ๊ฑฐ๋ฅผ ์ฐพ๊ณ , ๋…ผ๋ฆฌ๋ฅผ ๋ณด์™„ํ•˜๋Š” ์ผ์ด ๋…ผ๋ฆฌ์  ๋ถ„์„์ด๋ฏ€๋กœ ๋„์ถœ๋œ ๋‹ค์–‘ํ•œ ์˜๊ฒฌ์— ์ค€ํ•ด ๋ฐœ์‚ฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ง€์†์ ์ด๊ณ  ๋ฐ˜๋ณต์ ์ธ ๊ณ ์ฐฐ์„ ํ†ตํ•ด ํ•˜๋‚˜์˜ ๋‹ต์ด ์‚ด์ฐŒ์›Œ์ง€๊ฒŒ ๋…ธ๋ ฅํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. KISS(Keep It Simple Stupid): ๊ฐ„๋‹จ ๋ช…๋ฃŒํ•˜๊ฒŒ ํ•˜๋ผ. ํŒŒ๋ ˆํ†  ๋ฒ•์น™์„ ๊ฐ์•ˆํ•˜์—ฌ ๊ฐ€์žฅ ํšจ๊ณผ๊ฐ€ ๋†’์„ ๊ฒƒ๋“ค์— ์ง‘์ค‘ํ•˜๋ผ. ๋ฐ์ดํ„ฐ๋Š” ์–ด๋””์—๋“  ์žˆ๋‹ค : ์ธํ„ฐ๋„ท์˜ ์‹œ๋Œ€์— ๋„คํŠธ์›Œํฌ์—์„œ ๋˜๋Š” ์˜† ์‚ฌ๋žŒ์˜ ์„œ๋ž ์†์—์„œ ๋ชจ๋“  ๋ฌธ์ œ์™€ ์ •๋ณด์™€ ๋‹ต์€ ํ˜„์žฅ ๊ณณ๊ณณ์— ์žˆ๋‹ค. '์ •ํ™•ํ•˜๋‹ค'๋ผ๋Š” ๋ง์€ ์ƒ๋Œ€์ ์ธ ๊ฐœ๋…์ด๋‹ค: โ€˜๋ณด๋‹ค ๋” ์ •ํ™•ํ•˜๊ฒŒโ€™๋ผ๋Š” ๊ด€์ ์€ ์ผ์„ ๋๋‚ด์ง€ ๋ชปํ•˜๊ฒŒ ํ•œ๋‹ค. ๋ฒ”์œ„์™€ ์‹œ๊ฐ„์˜ ์ œํ•œ์„ ๊ณ ๋ คํ•˜๋ฉด์„œ ๊ทธ ์†์—์„œ ์ตœ๋Œ€ํ•œ ์ •ํ™•์„ฑ์„ ๊ณ ๋ คํ•˜๋ผ. โ€˜Timeliness is next to godlinessโ€™. ์ด ํ‘œํ˜„์€ ์ฒญ๊ฒฐ์€ ์‹ ์•™์‹ฌ ๋‹ค์Œ์ด๋‹ค(Cleanlinessis next to godliness) ์ฆ‰, ์ฒญ๊ฒฐ์€ ์•„์ฃผ ์ค‘์š”ํ•˜๊ณ  ๊ณ ๊ท€ํ•œ ๋ฏธ๋•์ด๋ผ๋Š” ์˜์–ด๊ถŒ ๋ช…๊ตฌ๋ฅผ ํŒจ๋Ÿฌ๋””ํ•œ ๊ฒƒ์œผ๋กœ ๋‚ฉ๊ธฐ ์ค€์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค๋Š” ๋ง์ด๋‹ค. ์ด๋Ÿฐ ํŒ๋“ค์„ ๊ณ ๋ คํ•˜์—ฌ ์ˆ˜ํ–‰ํ•œ ๋ถ„์„ ์ž‘์—…์ด ๋๋‚˜๋ฉด ๊ฒฐ๊ณผ๋ฅผ ์ข…ํ•ฉํ•˜๊ณ  ํ•ด๊ฒฐ์•ˆ ์˜ต์…˜๋“ค(Options)์„ ๋„์ถœํ•ด์•ผ ํ•œ๋‹ค[2]. ํ•ด๊ฒฐ์•ˆ ์˜ต์…˜ ๋„์ถœ์€ FigureII-24์™€ ๊ฐ™์ด 5๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ์„œ ์ง„ํ–‰๋œ๋‹ค. Figure II-24. ํ•ด๊ฒฐ์•ˆ ์˜ต์…˜์˜ ๋„์ถœ ๋ฐ ๊ฒฐ์ • ์ฒซ ๋ฒˆ์งธ๋Š” ๋ชฉ์ ์„ ๋ช…ํ™•ํžˆ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ํ•ด๊ฒฐ์ฑ…์„ ์œ„ํ•ด ๋ณต์ˆ˜์˜ ์•„์ด๋””์–ด๋ฅผ ๊ฒ€ํ† ํ•˜๊ณ  ์„ธ ๋ฒˆ์งธ๋Š” ์„ฑ๊ณผ๋ฅผ ๋ช…ํ™•ํžˆ ํ•˜๊ธฐ ์œ„ํ•ด ํ‰๊ฐ€ ๊ธฐ์ค€์„ ๋ช…ํ™•ํžˆ ํ•ด์•ผ ํ•œ๋‹ค. ๋„ค ๋ฒˆ์งธ, ํ‰๊ฐ€๋Š” ์‚ฌ์‹ค์— ์ž…๊ฐํ•˜๊ณ  ํ•„์š”ํ•œ ์ •๋ณด์— ๊ทผ๊ฑฐํ•˜์—ฌ ๊ฐ๊ด€์ ์ธ ํ‰๊ฐ€๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ํ•˜๋ฉฐ ๋‹ค์„ฏ ๋ฒˆ์งธ, ๋ณต์ˆ˜๊ฐœ์˜ ์•ˆ(ๆกˆ)์— ๋Œ€ํ•œ ํ‰๊ฐ€ ๊ฒฐ๊ณผ๋ฅผ ๋†“๊ณ  ํ•ด๊ฒฐ์ฑ…์„ ์ตœ์ข…์˜ ์‚ฌ ๊ฒฐ์ •ํ•œ๋‹ค. ์ข‹์€ ํ•ด๊ฒฐ์ฑ…์˜ ์š”๊ฑด์€ ๋ชฉํ‘œ๋ฌผ(target)์„ ๋ฒ—์–ด๋‚˜์ง€ ๋ง์•„์•ผ ํ•˜๋ฉฐ, ๋ฐ”๋กœ ํ–‰๋™์— ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์ฒด์ ์ด์–ด์•ผ ํ•˜๊ณ  (Action-oriented), ์‹คํ–‰ ์ฃผ์ฒด์™€ ๋ˆˆ๋†’์ด๋ฅผ ๋งž์ถ”์–ด์•ผ ํ•˜๋ฉฐ, ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฐœ์„ ์˜ ๊ธฐ๋Œ€์น˜๊ฐ€ ๋†’์•„์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ ๊ณ ๋ ค ์‚ฌํ•ญ๋“ค์„ ํ†ตํ•ด์„œ ๋„์ถœ๋œ ํ•ด๊ฒฐ์ฑ…์ด ๊ฐ€์žฅ ์„ฑ๊ณผ๊ฐ€ ๋†’์„ ์ˆ˜ ์žˆ๋‹ค. Break #7. ๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ๋˜ ๋‹ค๋ฅธ ๊ด€์ , ECRS ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ ๋ฅผ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ์ •์˜ํ•˜๊ณ , ์„ธ๋ถ„ํ™”ํ•˜์—ฌ, ๋ฌธ์ œ๋ณ„๋กœ ๊ฐ€์„ค์„ ๋ถ„์„ํ•˜๊ณ , ๊ทธ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…๋“ค์„ ํ‰๊ฐ€ํ•˜์—ฌ, ๊ฐ€์žฅ ์„ฑ๊ณผ๊ฐ€ ํฐ ๋ฌธ์ œ ํ•ด๊ฒฐ์ฑ…์„ ์ฐพ๋Š” ๋ฐฉ์•ˆ์— ๋Œ€ํ•ด์„œ ์ง€๊ธˆ๊นŒ์ง€ ์‚ดํŽด๋ณด์•˜๋‹ค. ์ด๋Š” ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ (Logical Thinking)์— ๊ธฐ๋ฐ˜ํ•œ ๋งค์šฐ ์ „ํ†ต์ ์ด๊ณ ๋„ ๊ณ ์ „์ ์ธ ๋ฌธ์ œํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด๊ณ  ๊ทธ ์„ฑ๊ณผ๋„ ๋‚˜์˜์ง€ ์•Š๋‹ค. ๋กœ์ง ํŠธ๋ฆฌ(Logic Tree)๋ฅผ ํ™œ์šฉํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ์‹์€ ํญํฌ์ˆ˜(Waterfall) ๋ฐฉ์‹์œผ๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ๊ฐ ๋‹จ๊ณ„๊ฐ€ ์ง„ํ–‰๋˜๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ธฐ๋ฒ•์ด๋ผ๋ฉด, ์ง€๊ธˆ ์„ค๋ช…ํ•  ECRS ์  ์‚ฌ๊ณ ๋Š” ๋‹ค์–‘ํ•œ ๊ณ ๋ ค ์‚ฌํ•ญ๋“ค์„ ์ž…์ฒด์ ์œผ๋กœ ๊ณ ๋ คํ•˜์—ฌ ํ•œ ๋ฒˆ์— ๊ฒฐ๋ก ์„ ์ •๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ECRS๋Š” ์—…๋ฌด ์ƒ์˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•˜์—ฌ ์ œ๊ฑฐ(Eliminate), ๊ฒฐํ•ฉ(Combine), ์žฌ๋ฐฐ์—ด ๋˜๋Š” ๊ตํ™˜(Rearrange), ๊ฐ„๋žตํ™”(Simplify)์˜ 4๊ฐ€์ง€ ๊ด€์ ์—์„œ ์ƒ๊ฐํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค. ์ œ๊ฑฐ(Eliminate)๋Š” โ€˜๊ทธ๋งŒ๋‘˜ ์ˆ˜ ์—†๋Š”๊ฐ€?โ€™๋ฅผ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ˜„์žฌ์˜ ์ผ์„ ๊ทธ๋งŒ๋‘๊ธฐ ์œ„ํ•ด์„œ๋Š” '๊ทธ ์ผ์„ ์™œ(๋˜๋Š” ๋ฌด์—‡์„ ์œ„ํ•ด) ํ•˜๊ณ  ์žˆ๋Š”๊ฐ€?'๋ผ๋Š” ์—…๋ฌด์˜ ์ด์œ  ๋˜๋Š” ๋ชฉ์ ์„ ์ฒ ์ €ํžˆ ๊ทœ๋ช…ํ•ด์•ผ ํ•œ๋‹ค. ๊ฒฐํ•ฉ(Combine)์€ โ€˜ํ•จ๊ป˜ ํ•  ์ˆ˜ ์—†์„๊นŒโ€™๋ฅผ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ช‡ ๊ฐ€์ง€ ์ผ์ด๋‚˜ ๊ณต์ •์„ ํ•จ๊ป˜ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋Š” ๊ฒƒ์ด๋ฉฐ, ์žฌ๋ฐฐ์—ด ๋˜๋Š” ๊ตํ™˜์€ โ€˜์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋Š”๊ฐ€?'๋ฅผ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ผ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ์–ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฐ„๋žตํ™”(Simplify)๋Š” โ€˜๊ฐ„๋‹จํžˆ ํ•  ์ˆ˜ ์—†์„๊นŒ?โ€™๋ฅผ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์™œ ๊ทธ๋Ÿฐ ๋ฐฉ๋ฒ•์œผ๋กœ ์ผํ•˜๋Š”์ง€ ๋“ฑ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด๊ณ  ๊ฐ„์†Œํ™” ๊ฐ€๋Šฅ์„ฑ์„ ์ฐพ์•„๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค. Figure II-25. ECRS ๊ธฐ๋ฒ•์˜ ๊ฐœ๋… ECRS ๊ธฐ๋ฒ•์˜ ๊ธฐ๋ณธ ์ „์ œ๋Š” โ€˜๋‚ญ๋น„๊ฐ€ ๋งŽ๋‹คโ€™๋Š” ๊ฒƒ์ด๋‹ค. ๊ธฐ์—… ๋‚ด ํ’ˆ์งˆ ํ™œ๋™ ๋“ฑ๊ณผ ์—ฐ๊ณ„ํ•ด์„œ Table II-6๊ณผ ๊ฐ™์ด 5W1H ๊ด€์ ์—์„œ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ECRS์˜ ๊ฐ ํ•ญ์„ ์ข€ ๋” ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Table II-6. 5W1H ๊ด€์ ์˜ ECRS ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ธฐ๋ฒ•๊ณผ ๊ด€๋ จํ•ด์„œ ๋กœ์ง ํŠธ๋ฆฌ๋ฅผ ์ด์šฉํ•œ ์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ECRS๋ฅผ ํฌํ•จํ•˜์—ฌ ์ตœ๊ทผ์—๋Š” ๋””์ž์ธ ์‹ฑํ‚น(Design Thinking)์— ์ด๋ฅด๊ธฐ๊นŒ์ง€ ๋งค์šฐ ๋‹ค์–‘ํ•œ ์•„์ด๋””์–ด๋“ค์ด ๋‚˜์˜ค๊ณ  ์žˆ๋‹ค. ๋งฅํ‚จ์ง€ 7๋‹จ๊ณ„ ๊ธฐ๋ฒ•์ด ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์ˆœ์ฐจ์ ์ธ ์ƒ๊ฐ์— ๊ทผ๊ฑฐํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ์ •์˜ํ•˜๊ณ  ๋ถ„์„ํ•˜๊ณ  ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋ผ๋ฉด ECSR๋Š” ๋งˆ์น˜ ์†Œํ”„ํŠธ์›จ์–ด ๊ณตํ•™์˜ Spiral ๋ฐฉ๋ฒ•์ฒ˜๋Ÿผ ๋‹ค์–‘ํ•œ ๊ด€์ ์—์„œ ํŒŒ์•…ํ•˜๊ณ  ๋ฐ˜๋ณต์„ ํ†ตํ•ด ์ ์ง„์ ์œผ๋กœ ๊ฐœ์„ ํ•ด๋‚˜๊ฐ€๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋””์ž์ธ ์‹ฑํ‚น์€ ์ด์™€ ๋˜ ๋‹ค๋ฅด๊ฒŒ ๋ฌธ์ œ๋ฅผ ์กฐ๋งํ•˜๊ณ  ํ•ด๊ฒฐํ•ด๋‚˜๊ฐ€๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋‹ค์Œ ์žฅ์€ ๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ๋งˆ์ง€๋ง‰ ์žฅ์œผ๋กœ ๋””์ž์ธ ์‹ฑํ‚น์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž. [1] Back-of-the-envelope calculation ํŽธ์ง€๋ด‰ํˆฌ ๋’ท๋ฉด์— ์ ์œผ๋ฉด์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ [2] ํ•ด๊ฒฐ์•ˆ ์˜ต์…˜์—์„œ ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ(No Action)๋„ ์˜ต์…˜์ด ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ๋ช…์‹ฌํ•˜์ž ํŠธ๋ฆฌ์ฆˆ(TRIZ)? ๋ฌธ์ œ ํ•ด๊ฒฐ์— ๋””์ž์ธ ์‹ฑํ‚น(Design Thinking)์€ ๋ญ์•ผ? ๋กœ์ง ํŠธ๋ฆฌ๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ๊ณ ๋ฏผํ•˜๋‹ค ๋ณด๋ฉด ์ž์ฃผ ๋ถ€๋”ชํžˆ๋Š” ํ•œ๊ณ„๊ฐ€ ์‚ฌ๊ณ (ๆ€่€ƒ) ์‹คํ—˜์˜ ์ œํ•œ์„ฑ์ด๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ '๊ทธ๊ฒŒ ์ง„์งœ ๊ทธ๋ ‡๊ฒŒ ๋˜๋ƒ?'๋ผ๊ณ  ๋งํ•  ๊ฒฝ์šฐ ์ฃผ์ถคํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ปจ์„คํŒ… ์Šคํ‚ฌ์—์„œ ์†Œ๊ฐœํ•˜๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ธฐ๋ฒ•์€ ๋‹ค์–‘ํ•œ ํ†ต๊ณ„๋Ÿ‰๊ณผ ๋ถ„์„์— ๊ธฐ๋ฐ˜ํ•œ ๊ณผํ•™์  ๋ถ„์„์ด ์•„๋‹ˆ๋ผ ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ ์— ๊ทผ๊ฑฐํ•œ ์‚ฌ๊ณ  ์‹คํ—˜์ด ์ค‘์‹ฌ์ด์ง€๋งŒ ๊ทธ๋Ÿฐ ๋„์ „์ด ์˜ค๋žœ ์‹œ๊ฐ„ ์ง€์†๋˜๋ฉด์„œ ๋ณด๋‹ค ๋‹ค๋ฅธ ๊ด€์ ์—์„œ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉ๋ฒ•๋“ค์ด ๋งŽ์ด ๊ฐœ๋ฐœ๋˜์—ˆ๋‹ค. ํŠธ๋ฆฌ์ฆˆ(TRIZ)๋‚˜ ๋””์ž์ธ ์‹ฑํ‚น(Design Thinking)์€ ๋‹จ์ˆœํ•œ ๊ธฐ๋ฒ•์ด๋ผ๊ธฐ๋ณด๋‹ค๋Š” ํ•˜๋‚˜์˜ ๋ฐฉ๋ฒ•๋ก (Methodology)์œผ๋กœ ๋‚˜๋ฆ„์˜ ๋„๊ตฌ๋“ค๋„ ๊ตฌ๋น„ํ•˜๊ณ  ์žˆ๊ธฐ์— ์ปจ์„คํŒ… ์Šคํ‚ฌ์—์„œ ๋‹ค๋ฃจ๊ธฐ๋ณด๋‹ค๋Š” Part IV. ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ์—์„œ ๋‹ค๋ฃฐ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ '๋ฌธ์ œ ํ•ด๊ฒฐ์˜ ์นดํ…Œ๊ณ ๋ฆฌ' ์•ˆ์—์„œ ๊ฐ„๋žตํ•˜๊ฒŒ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•œ๋‹ค. ํŠธ๋ฆฌ์ฆˆ๋‚˜ ๋””์ž์ธ ์‹ฑํ‚น ๋ชจ๋‘ ์„ธ์ƒ์— ์•Œ๋ ค์ง„ ๊ฒƒ์€ ๊ฝค ์˜ค๋ž˜์ „์˜ ์ผ์ด๋‹ค. ๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์•„์ง ๋งˆ์ด๋„ˆ๋ฆฌํ‹ฐ(Minority)๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ ๊ฐ™๊ณ  ์š”์ฆ˜ ๊ฐ™์€ ์‹ค์‹œ๊ฐ„ ์ •๋ณด ๊ต๋ฅ˜๊ฐ€ ๋˜๋Š” ์„ธ์ƒ์—์„œ๋„ ํŠนํžˆ, ํ•œ๊ตญ์—์„œ๋Š” ๊ฑฐ์˜ ์ƒˆ๋กœ์šด ์–ด์  ๋‹ค์ฒ˜๋Ÿผ ๋ฐ›์•„๋“ค์—ฌ์ง€๊ณ  ์žˆ๋‹ค. ๋‹ค๋งŒ, ๊ธฐ์กด์˜ ๋ณต์žก๊ณ„(Complex System)์— ์‚ด๊ณ  ์žˆ๋Š” ์šฐ๋ฆฌ๋Š” ์ƒˆ๋กœ์šด ์„ธ์ƒ์„ ๋˜๋Š” ์‹œ์Šคํ…œ์„ ๋˜๋Š” ์‚ฌ์—…์„ ๋ฐ”๋ผ๋ณผ ๋•Œ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋ก ์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ ๊ทน ๊ณ ๋ฏผํ•ด ๋ณด๋Š” ๊ฒƒ๋„ ๋‚˜์˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ด ์žฅ์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ณ„์† ๊ฐ•์กฐํ•˜์ง€๋งŒ ์ด๊ฒƒ๋“ค์€ ์ง€์‹์ด ์•„๋‹ˆ๋ผ ์Šคํ‚ฌ์ด๋‹ค. ์ฝ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  ์ง์ ‘ ํ•ด๋ณด์ง€ ์•Š์œผ๋ฉด ๋ณ„ ๋„์›€์ด ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ํŠธ๋ฆฌ์ฆˆ์™€ ๋””์ž์ธ ์‹ฑํ‚น์„ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. 5.4 ํŠธ๋ฆฌ์ฆˆ(TRIZ), ๋ฏธ๊ตญ์˜ ์ฝ”๋ฅผ ๊บพ๋‹ค. Figure II-26. ๋‹ฌ ์ฐฉ๋ฅ™์„ ์˜ ์—”์ง„ ๋ถ„์‚ฌ ์ด๋Ÿฐ ์–˜๊ธฐ๊ฐ€ ์žˆ๋‹ค. 1969๋…„ ์•„ํด๋กœ 11ํ˜ธ์˜ ๋‹ฌ ์ฐฉ๋ฅ™์„ ์•ž๋‘๊ณ  NASA์˜ ๊ณผํ•™์ž๋“ค์€ ๊ณ ๋ฏผ์— ๋น ์กŒ๋‹ค. "์•ˆ์ „ํ•œ ๋‹ฌ ์ฐฉ๋ฅ™์„ ์œ„ํ•ด ๋‹ฌ ํ‘œ๋ฉด์„ ์ƒ์„ธํžˆ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์ฃผํƒ์‚ฌ์„  ํ•˜๋ถ€์— ๋งŽ์€ ๋ฐฑ์—ด์ „๊ตฌ๋ฅผ ๋‹ฌ์•„์„œ ๋ฐ๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ฌ ์ฐฉ๋ฅ™ ์‹œ ๋ฐœ์ƒํ•˜๋Š” ๊ฐ•ํ•œ ์ถฉ๊ฒฉ์„ ๊ฒฌ๋”œ ์ˆ˜ ์žˆ๋Š” ๊ฐ•ํ•œ ์œ ๋ฆฌ์™€ ์ „๊ตฌ ๋“ฑ์ด ํ•„์š”ํ•˜๋‹ค." NASA์˜ ์ˆ˜๋งŽ์€ ๊ณผํ•™์ž๋“ค์ด ๊ณ ๋ฏผํ•˜์˜€์ง€๋งŒ ๋‹ต์€ ๋‚˜์˜ค์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋Ÿฌ๋˜ ์™€์ค‘ ์†Œ๋ จ ์ถœ์‹  ๊ณผํ•™์ž๋ฅผ ์ดˆ๋น™ํ•˜์—ฌ ์˜๊ฒฌ์„ ๊ตฌํ–ˆ๋Š”๋ฐ ๊ทธ๊ฐ€ ํ•œ ๋ง, "์šฐ์ฃผ๋Š” ์ง„๊ณต์ƒํƒœ์—ฌ์„œ ๊ตณ์ด ์ „๊ตฌ์— ์œ ๋ฆฌ๋ฅผ ์”Œ์šฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค." Fact์— ๊ทผ๊ฑฐํ•œ ๋ฐœ์ƒ์˜ ์ „ํ™˜์ธ ๊ฒƒ์ด๋‹ค. ์‹ค์ œ๋กœ ์ด๋Ÿฐ ์ผ์ด ์žˆ์—ˆ๋Š”์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ TRIZ๊ฐ€ ๋ƒ‰์ „์ด ํ•œ์ฐฝ์ด๋˜ 1940๋…„ ๋Œ€ ์†Œ๋ จ(็พ ๋Ÿฌ์‹œ์•„)์—์„œ ๊ฐœ๋ฐœ๋œ ๊ฒƒ์ž„์„ ์•ˆ๋‹ค๋ฉด ์ถฉ๋ถ„ํžˆ ๊ทธ๋Ÿฐ ์ผํ™”๊ฐ€ ํšŒ์ž๋  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. TRIZ๋Š” '์ฐฝ์˜์  ๋ฌธ์ œ ํ•ด๊ฒฐ ์ด๋ก '์ด๋ผ๋Š” ๋Ÿฌ์‹œ์•„์–ด 'Teoriya Resheniya Izobretatelskikh Zadach'์—์„œ ์ฒซ ๊ธ€์ž๋ฅผ ๋”ฐ ์˜จ ๊ฒƒ์œผ๋กœ ์˜์–ด๋กœ๋Š” 'Theory of Inventive Problem Solving'์ด๋ผ๋Š” ๋œป์ด๋‹ค. ์†Œ๋ จ์˜ ๊ณผํ•™์ž ๊ฒ๋ฆฌํžˆ ์•Œ์ธ ์Š๋Ÿฌ(Genrich Altshuller. 1926 ~ 1998)์— ์˜ํ•ด ์ฐฝ์•ˆ๋œ ๊ฒƒ์œผ๋กœ ์•Œ๋ ค์ ธ ์žˆ๋Š” TRIZ๋Š” ์ฐฝ์˜์  ๋ฌธ์ œ๋ฅผ ์ •์˜ํ•˜๊ณ  ์ตœ์†Œ์˜ ์ž์›์„ ํˆฌ์ž…ํ•ด ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ฌด(็„ก)์—์„œ ์œ (ๆœ‰)๋ฅผ ์ฐฝ์กฐํ•˜๊ฑฐ๋‚˜ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์œ (ๆœ‰)์—์„œ ์œ (ๆœ‰)๋ฅผ ์ฐพ๋Š” ๋…ธ๋ ฅ์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ฃผ์ฐฝํ•˜๋Š” ์ด๋ก ์ด๋‹ค. ์ฆ‰, ์„ธ์ƒ์˜ ๋ชจ๋“  ๋ฌธ์ œ๋Š” ํ•ด๊ฒฐ๋ฒ•๋“ค์ด ์กด์žฌํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ์šฐ๋ฆฌ๋Š” ๊ทธ๊ฒƒ์„ ์ฐพ์•„๋‚ด๋ฉด ๋œ๋‹ค๋Š” ์ด๋ก ์ด๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ TRIZ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋…ํŠนํ•œ ๋‹จ๊ณ„๋“ค์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. (1) ๋ชจ์ˆœ(Contradiction)์˜ ์ •์˜ (2) ํŒŒ๋ผ๋ฏธํ„ฐ ๋ณ€ํ™˜ (3) ๋ชจ์ˆœ ํ–‰๋ ฌ(Contradiction Matrix) ํ…Œ์ด๋ธ” ์ด์šฉ (4) 40๊ฐ€์ง€ ํ•ด๊ฒฐ ์›๋ฆฌ ์ค‘ ์ตœ์  ์›๋ฆฌ ์„ ํƒ ์ฒซ ๋ฒˆ์งธ, ๋ชจ์ˆœ์˜ ์ •์˜๋Š” '๋ชจ๋“  ๋ฌธ์ œ๋Š” ์ตœ์†Œํ•œ ํ•œ ๊ฐ€์ง€ ์ด์ƒ์˜ ๋ชจ์ˆœ์ด ์žˆ๋‹ค'๋ผ๋Š” ์ „์ œ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ์ฆ‰, '๋งŒ์•ฝ A๋ฅผ ํ•˜๋ฉด B๊ฐ€ ๋‚˜๋น ์ง„๋‹ค'๋ผ๋Š” ๊ฒƒ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ณ  ๊ทธ ๋ชจ์ˆœ์„ ํ•ด๊ฒฐํ•˜๋ฉด ๋ฌธ์ œ๋Š” ํ•ด๊ฒฐ๋œ๋‹ค๋Š” ๊ฐœ๋…์ด๋‹ค. ์ด๋Ÿฐ ๋ชจ์ˆœ์—๋Š” ํฌ๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๋ฌผ๋ฆฌ์  ๋ชจ์ˆœ(Physical Contradiction) ๊ธฐ์ˆ ์  ๋ชจ์ˆœ(Technical Contradiction) ๋ฌผ๋ฆฌ์  ๋ชจ์ˆœ์€ '์–ด๋Š ํ•˜๋‚˜๊ฐ€ ์ด๋ž˜์•ผ ํ•˜์ง€๋งŒ ๋˜ ์ €๋ž˜์•ผ ํ•œ๋‹ค'๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด '์ž์ „๊ฑฐ ์ฒด์ธ์€ ๋ถ€๋“œ๋Ÿฝ์ง€๋งŒ ๋˜ ๋‹จ๋‹จํ•ด์•ผ ํ•œ๋‹ค.'๋ผ๋“ ๊ฐ€ '๋น„ํ–‰๊ธฐ ๋ฐ”ํ€ด๋Š” ์ฐฉ๋ฅ™์„ ์œ„ํ•ด ์žˆ์–ด์•ผ ํ•˜์ง€๋งŒ ๊ณต๊ธฐ์ €ํ•ญ์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ์—†์–ด์•ผ ํ•œ๋‹ค.'๊ฐ™์€ ๊ฒƒ๋“ค์ด๋‹ค. ๊ธฐ์ˆ ์  ๋ชจ์ˆœ์€ ์„œ๋กœ ๋‹ค๋ฅธ 2๊ฐœ๊ฐ€ ์ถฉ๋Œํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ค๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, '์–ด๋Š ํ•˜๋‚˜๊ฐ€ ์ข‹์•„์ง€๋ฉด ์–ด๋Š ํ•˜๋‚˜๊ฐ€ ๋‚˜๋น ์ง„๋‹ค'์™€ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด '์—ฐ๋น„๊ฐ€ ์ข‹์•„์ง€๋ฉด ์ถœ๋ ฅ์ด ๋‚˜๋น ์ง„๋‹ค', '๋น„์šฉ์ด ์ข‹์•„์ง€๋ฉด ํ’ˆ์งˆ์ด ๋‚˜๋น ์ง„๋‹ค.', '๋ฌผ๊ฐ€๊ฐ€ ์•ˆ์ •๋˜๋ฉด ๊ฒฝ์ œ์„ฑ์žฅ์ด ๋‘”ํ™”๋œ๋‹ค' ๋“ฑ๊ณผ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. TRIZ์—์„œ๋Š” ์ด๋Ÿฐ ๋ชจ์ˆœ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ถ„๋ฆฌ์˜ ์›๋ฆฌ(Seperation Principle)์™€ 40๊ฐ€์ง€ ํ•ด๊ฒฐ์˜ ์›๋ฆฌ(40 Inventive Principles) ๋ฐ ๋ชจ์ˆœ ํ–‰๋ ฌ์„ ํ™œ์šฉํ•œ๋‹ค. ๋ถ„๋ฆฌ์˜ ์›๋ฆฌ๋Š” ๋ฌผ๋ฆฌ์  ๋ชจ์ˆœ์—์„œ ์ฃผ๋กœ ํ™œ์šฉํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์— ์˜ํ•œ ๋ถ„๋ฆฌ(Seperation in Time), ๊ณต๊ฐ„์— ์˜ํ•œ ๋ถ„๋ฆฌ(Seperation in Space), ์ „์ฒด์™€ ๋ถ€๋ถ„์— ์˜ํ•œ ๋ถ„๋ฆฌ(Seperation in Scale), ์กฐ๊ฑด์— ์˜ํ•œ ๋ถ„๋ฆฌ(Seperation in Condition) ๋“ฑ์ด ์žˆ์œผ๋ฉฐ, 40๊ฐ€์ง€ ํ•ด๊ฒฐ์˜ ์›๋ฆฌ์™€ ๋ชจ์ˆœ ํ–‰๋ ฌ์€ ๊ธฐ์ˆ ์  ๋ชจ์ˆœ์—์„œ ํ™œ์šฉํ•œ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์˜ˆ์ œ๋ฅผ ๋ถ„๋ฆฌ์˜ ์›๋ฆฌ์— ์ ์šฉํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์‹œ๊ฐ„์— ์˜ํ•œ ๋ถ„๋ฆฌ: '๋น„ํ–‰๊ธฐ ๋ฐ”ํ€ด๋Š” ์ฐฉ๋ฅ™์„ ์œ„ํ•ด ์žˆ์–ด์•ผ ํ•˜์ง€๋งŒ ๊ณต๊ธฐ์ €ํ•ญ์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ์—†์–ด์•ผ ํ•œ๋‹ค.'๋ผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ„๋ฆฌ๋  ์ˆ˜ ์žˆ๋‹ค. '๋žœ๋”ฉ ๊ธฐ์–ด๋ฅผ ์ด์ฐฉ๋ฅ™ ์‹œ ๋™์ฒด์—์„œ ๋‚˜์˜ค๊ฒŒ ํ•˜๊ณ , ๋น„ํ–‰ ์‹œ, ๋™์ฒด ์•ˆ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ํ•จ.' ๊ณต๊ฐ„์— ์˜ํ•œ ๋ถ„๋ฆฌ: '๊ณ ์ธต ๊ฑด๋ฌผ์— ์—˜๋ฆฌ๋ฒ ์ดํ„ฐ๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ์„ค์น˜ํ•˜๋ฉด, ๊ทธ๋งŒํผ ์‚ฌ์šฉ ๊ณต๊ฐ„์ด ์ค„์–ด๋“ ๋‹ค'๋ผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ„๋ฆฌ๋  ์ˆ˜ ์žˆ๋‹ค. 'ํ•œ ๊ณต๊ฐ„์— ์ €์ธต์šฉ, ๊ณ ์ธต์šฉ 2๊ฐœ์˜ ์—˜๋ฆฌ๋ฒ ์ดํ„ฐ๋ฅผ ์„ค์น˜ํ•จ' ์ „์ฒด์™€ ๋ถ€๋ถ„์— ์˜ํ•œ ๋ถ„๋ฆฌ: '์ž์ „๊ฑฐ ์ฒด์ธ์€ ๋ถ€๋“œ๋Ÿฝ์ง€๋งŒ ๋˜ ๋‹จ๋‹จํ•ด์•ผ ํ•œ๋‹ค'๋ผ๋Š” '๋‹จ๋‹จํ•œ ์‡ ์‚ฌ์Šฌ๋กœ ๋งŒ๋“ค๋˜, ๋ถ„๋ฆฌ ๋ฐ ์—ฐ๊ฒฐ์€ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ํ•จ'๊ณผ ๊ฐ™์ด ๋ถ„๋ฆฌ๋  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ์ˆœ์„ ์ •์˜ํ•˜๊ณ  ์ด๋ฅผ ๋ถ„๋ฆฌํ•ด ๋‚ด๋ฉด ์ด์ œ ๋ชจ์ˆœ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ๋‘ ๋ฒˆ์งธ ๋‹จ๊ณ„์ธ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ณ€ํ™˜์„ ์ง„ํ–‰ํ•œ๋‹ค. ๋ชจ์ˆœ์˜ ์ •์˜์—์„œ '๋งŒ์•ฝ A ํ•˜๋ฉด B๊ฐ€ ๋‚˜๋น ์ง„๋‹ค'๋ผ๋Š” ๊ฒƒ์—์„œ A์™€ B์˜ ํŠน์„ฑ์„ ๊ฐ€์žฅ ์ž˜ ๋ฐ˜์˜ํ•˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ ํƒํ•œ๋‹ค. TRIZ์—์„œ๋Š” ์ด๋ฅผ ์œ„ํ•ด 39๊ฐ€์ง€์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•˜๊ณ  ์žˆ๊ณ  ์ด๋ฅผ ์„ ํƒํ•œ๋‹ค.[1] Table II-7. TRIZ ๊ธฐ๋ฒ•์˜ 39๊ฐ€์ง€ ํŒŒ๋ผ๋ฏธํ„ฐ ์„ธ ๋ฒˆ์งธ๋กœ ํ•  ์ผ์€ ์„ ํƒ๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ชจ์ˆœ ํ–‰๋ ฌ์—์„œ ์„ค๋ฃจ์…˜์„ ์ฐพ์•„๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ๋ชจ์ˆœ ํ–‰๋ ฌ์€ TRIZ ๊ธฐ๋ฒ• ์ž์ฒด์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์œผ๋กœ Figure II-27์€ ๊ทธ ์ผ๋ถ€์ด๋‹ค. Figure II-27. ๋ชจ์ˆœ ํ–‰๋ ฌ(Contradiction Matrix)์˜ ์ผ๋ถ€ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฌธ์ œ์˜ ํŠน์„ฑ์„ ๋ฐ˜์˜ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ 17๋ฒˆ ์˜จ๋„์™€ 1๋ฒˆ ์›€์ง์ด๋Š” ๋ฌผ์ฒด์˜ ๋ฌด๊ฒŒ๋ผ๋ฉด 6,22,36,38์ด๋ผ๋Š” ๋ฒˆํ˜ธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฒˆํ˜ธ๊ฐ€ TRIZ์˜ 40๊ฐ€์ง€ ํ•ด๊ฒฐ์ฑ…์ด๋‹ค. 6๋ฒˆ์€ Multifuction(ํ•˜๋‚˜์˜ ๋ฌผ๊ฑด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ•˜๊ธฐ), 22๋ฒˆ์€ Harmful to Useful(์•ˆ ์ข‹์€ ๊ฒƒ์„ ์ข‹์€ ๊ฒƒ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ), 36๋ฒˆ์€ Phase Change(์ƒํƒœ ์ „์ด), 38๋ฒˆ์€ Oxidant(๋ฐ˜์‘ ์†๋„๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ)์ด๋‹ค. Figure II-28. TRIZ์˜ 40๊ฐ€์ง€ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ฐพ์•„๋‚ธ TRIZ ํ•ด๋ฒ• (6,22,36,38)์„ ํ˜„์žฌ ์ƒํ™ฉ์— ์ตœ์ ํ™”ํ•˜๋ฉด ๋ฌธ์ œ ํ•ด๊ฒฐ์€ ์ข…๋ฃŒ๋œ๋‹ค. TRIZ๊ฐ€ ์ œ๊ณตํ•˜๋Š” 40๊ฐ€์ง€ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์€ ๋‹ค์–‘ํ•œ ๊ด€์ ์—์„œ ๊ทธ ์ง„๊ฐ€๋ฅผ ๋ฐœํœ˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„์—์„œ ์„ ํƒํ•œ ์„ค๋ฃจ์…˜๋“ค์„ ์ข€ ๋” ์‚ดํŽด๋ณด๋ฉด 6๋ฒˆ Multifunction์˜ ๊ฒฝ์šฐ, ํ•˜๋‚˜์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์ด ์—ฐ๊ณ„๋˜๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์นซ์†”์ด ์„คํƒœ๋„ ์ œ๊ฑฐํ•œ๋‹ค๋“ ์ง€, ๋‹ค๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ์Šค์œ„์Šค์˜ ๊ตฐ์šฉ ๋‚˜์ดํ”„, ์Šคํƒ€๋ฒ…์Šค ๋ฐ”๋ฆฌ์Šคํƒ€๋Š” ์ฃผ๋ฌธ, ์ œ์กฐ, ์„œ๋น„์Šค๋ฅผ ๋™์‹œ์— ํ•˜๋Š” ๊ฒƒ ๋“ฑ์ด ํ•ด๋‹น๋œ๋‹ค. 22๋ฒˆ Harmful to Useful (๋˜๋Š” Blessing in disguise)๋Š” ์œ ํ•ดํ•œ ๊ฒƒ์„ ์‚ฌ์šฉํ•ด์„œ ์œ ํ•ดํ•œ ๊ฒƒ์„ ์—†์• ๋Š” ๊ฒƒ, ์ด์ด์ œ์ด(ไปฅๅคทๅˆถๅคท) ๊ฐ™์€ ๊ฒƒ์ด ํ•ด๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ณ„์•ฝ ์‹œ ๊ฐ€๊ฒฉ์„ ๋‚ฎ์ถ”๋Š” ๋Œ€์‹  ์žฅ๊ธฐ๊ณ„์•ฝ์„ ํ•œ๋‹ค๋˜๊ฐ€, ๊ณ ๊ฐ์˜ ๋ถˆํŽธํ•จ์„ ์ฐฝ์•ˆํ•˜์—ฌ ๊ทธ ๋ถ€๋ถ„์„ ์ƒˆ๋กœ์šด ์‹œ์žฅ์œผ๋กœ ๊ฐœ์ฒ™ํ•˜๋Š” ๊ฒƒ ๋“ฑ์ด ๋  ์ˆ˜ ์žˆ๋‹ค. TRIZ๊ฐ€ ์ข€ ์ดํ•ด๋˜๋Š”๊ฐ€? ์•„์ฃผ ์ž˜ ์ •๋ฆฌ๋œ ๋ฌธ์ œ ํ•ด๊ฒฐ๊ธฐ๋ฒ•์ด๊ธด ํ•˜์ง€๋งŒ ์‚ฌ์‹ค ํŠธ๋ฆฌ์ฆˆ๋Š” ์‰ฝ์ง€ ์•Š๋‹ค. ๊ธฐ์ˆ  ๊ด€๋ จ ์ด์Šˆ์—์„œ ์ ์šฉํ•˜๊ธฐ ์šฉ์ดํ•˜๋ฉฐ ๋น„์ฆˆ๋‹ˆ์Šค๋กœ ํ™•์žฅํ•˜๋ฉด ์ข€ ๋” ์œ ์—ฐํ•œ ๋˜๋Š” ์‚ฌ์šฉ์„ฑ ๋†’์€ ์„ค๋ฃจ์…˜๋“ค์ด ํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ์ธก๋ฉด์—์„œ ์ด์ œ ์‚ดํŽด๋ณผ ๋””์ž์ธ ์‹ฑํ‚น์€ ๊ธฐ์กด์˜ ๋ธŒ๋ ˆ์ธ์Šคํ† ๋ฐ(Brainstorming)์„ ํ˜„์‹คํ™”ํ•˜๊ณ  ๋ฐ˜๋ณต์„ ํ†ตํ•ด ๊ตฌ์ฒดํ™” ๋‚˜๊ฐ์œผ๋กœ์จ ์Šคํƒ€ํŠธ์—…์ด๋‚˜ ๋น„์ฆˆ๋‹ˆ์Šค์˜ ๋น ๋ฅธ ํ”ผ๋“œ๋ฐฑ์„ ๊ธฐ๋Œ€ํ•˜๋Š” ์—…์ข…์—์„œ ๋งค์šฐ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 5.5 ์ƒ์ƒ์„ ํ˜„์‹ค๋กœ, ๋””์ž์ธ ์‹ฑํ‚น ์ €์ž๊ฐ€ ์ฒ˜์Œ ๋””์ž์ธ ์‹ฑํ‚น์„ ์ ‘ํ–ˆ์„ ๋•Œ '๋””์ž์ธ'์ด๋ผ๋Š” ์šฉ์–ด๋Š” ๋ฐ”์ด์–ด์Šค(Bias)๋ฅผ ๋ถˆ๋Ÿฌ์ผ์œผ์ผฐ๋‹ค. 'IT ์‹œ์Šคํ…œ์˜ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋‚˜ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜(UI/UX)๊ณผ ๊ด€๋ จ๋œ ๊ฒƒ์ธ๊ฐ€?'๋ผ๋Š” ์ƒ๊ฐ๋„ ๋“ค์—ˆ๊ณ  (๋ฌผ๋ก , ๋ฌด๊ด€ํ•˜์ง€ ์•Š๋‹ค) ์•„๋ฌดํŠผ ๊ทธ ๋ณธ์งˆ์„ ์•Œ๊ธฐ ์–ด๋ ค์› ๋‹ค. ๋””์ž์ธ ์‹ฑํ‚น์˜ ์ •์˜์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๋ง ๊ทธ๋Œ€๋กœ '๋””์ž์ธ(Design)์„ ์ œ๋Œ€๋กœ ํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กœ์„ธ์Šค'๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. TRIZ์™€ ๊ฐ™์ด ๋””์ž์ธ ์‹ฑํ‚น๋„ ์˜ค๋ž˜์ „๋ถ€ํ„ฐ ์ •์˜๋˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋Š”๋ฐ ๋‹ค์Œ์€ ์•Œ๋ ค์ง„ ๋ช‡ ๊ฐ€์ง€ ์ •์˜์ด๋‹ค. ์‚ฌํšŒ, ๋ฌธํ™”, ๊ฒฝ์ œ, ์ •์น˜ ํ™˜๊ฒฝ ๋“ฑ ์ธ๊ฐ„ ์ƒํ™œ์˜ ์ œ๋ฐ˜ ๋ฌธ์ œ๋ฅผ ํ•™์ œ์ ์ธ ํ˜‘๋™์„ ํ†ตํ•ด ๋””์ž์ธ์˜ ํ†ตํ•ฉ์ ์ด๊ณ  ์ข…ํ•ฉ์ ์ธ ๋ฌธ์ œํ•ด๊ฒฐ ๋Šฅ๋ ฅ๊ณผ ๋งž๋ฌผ๋ ค ํ•ด๊ฒฐํ•˜๋Š” ๊ณผ์ • - ํ—ˆ๋ฒ„ํŠธ ์‚ฌ์ด๋จผ(Herbert Simon, 1969) ๋””์ž์ด๋„ˆ์˜ ๊ฐ์ˆ˜์„ฑ๊ณผ ์ฐฝ์กฐ์  ์ž‘์—… ๋ฐฉ์‹์„ ํ™œ์šฉํ•˜์—ฌ ์‚ฌ๋žŒ๋“ค์˜ ๋‹ˆ์ฆˆ๋ฅผ ์‹คํ˜„ ๊ฐ€๋Šฅํ•œ ์ „๋žต์„ ํ†ตํ•ด ๋น„์ฆˆ๋‹ˆ์Šคํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์น˜์™€ ์‹œ์žฅ๊ธฐ ํšŒ๋กœ ๋งŒ๋“ค์–ด๋‚ด๋Š” ํ›ˆ๋ จ ๋ฐฉ๋ฒ• - ํŒ€ ๋ธŒ๋ผ์šด(Tim Brown, 2008) ํ˜์‹ ์„ ์œ„ํ•œ ์‚ฌ๊ณ  ๋ฒ•์œผ๋กœ ๋ถ„์„์  ์‚ฌ๊ณ ์˜ ์ˆ™๋ จ๊ณผ ์ง๊ด€์  ์‚ฌ๊ณ ์˜ ์ฐฝ์กฐ์„ฑ์ด ์—ญ๋™์ ์œผ๋กœ ๊ท ํ˜•์„ ์ด๋ฃฌ ๊ฒƒ - ๋กœ์ € ๋งˆํ‹ด(Roger Martin, 2009) ์„ธ์ƒ์— ๋„๋ฆฌ ์•Œ๋ ค์ง„ ๊ฒƒ์€ 2004๋…„ SAP[2]์˜ ํ›„์›์œผ๋กœ ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™์— d.school์ด ์ƒ๊ธฐ๋ฉด์„œ ์ฃผ๋ชฉ์„ ๋ฐ›๊ฒŒ ๋˜์—ˆ๊ณ  ํ™”๋‘๊ฐ€ ๋œ ๊ฒƒ์€ ์•„๋ฌด๋ž˜๋„ ๋””์ž์ธ ๊ธฐ์—… IDEO์˜ CEO ๋กœ์ € ๋งˆํ‹ด์˜ ๊ธฐ๊ณ ์™€ ์ €์„œ, ๊ทธ๋“ค์˜ ํ™œ๋™์ด ์„ธ์ƒ์— ์•Œ๋ ค์ง€๊ณ ๋ถ€ํ„ฐ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ดํ›„ ์›”์ŠคํŠธ๋ฆฌํŠธ์ €๋„์ด๋‚˜ ํฌ์ธˆ, ๋งฅํ‚จ์ง€, P&G, GE, IBM, Apple ๋“ฑ์—์„œ ์ด๋ฅผ ์ ๊ทน์ ์œผ๋กœ ๋‹ค๋ฃจ๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค์˜ ์„ฑ๊ณผ๋ฅผ ์ฃผ๋ชฉํ•˜์˜€๋‹ค. ๋””์ž์ธ ์‹ฑํ‚น์€ 4๊ฐ€์ง€ ํŠน์ง•์„ ๊ฐ€์ง€๋Š”๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ฒซ ๋ฒˆ์งธ, ์‚ฌ๋žŒ ์ค‘์‹ฌ(Human-centered)์ด๋‹ค. Figure II-29. Design Thinking์—์„œ ๋ฐ”๋ผ๋ณด๋Š” ํ˜์‹ ์˜ ์œ ํ˜• ๋””์ž์ธ ์‹ฑํ‚น์˜ ์ถœ๋ฐœ์€ ์‚ฌ๋žŒ์˜ ๊ด€์ ์—์„œ ์‹œ์ž‘ํ•œ๋‹ค. ์‚ฌ๋žŒ๋“ค, ์‚ฌ์šฉ์ž๋“ค, ์†Œ๋น„์ž๋“ค, ๊ณ ๊ฐ๋“ค์˜ ๋‹ˆ์ฆˆ ๋ฐ ๊ทธ๋“ค์˜ ํฌ๋งํ•˜๋Š” ๋ฐ”์—์„œ ์ถœ๋ฐœํ•˜๊ณ  ๊ทธ ๋™๊ธฐ์™€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฒƒ์ด ์‹œ์ž‘์ ์ด๋‹ค. ์ฆ‰, 'Empathy'๊ฐ€ ํ•ต์‹ฌ์œผ๋กœ ํƒ€์ธ์˜ Feeling์„ ๋Š๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉฐ '๊ณต๊ฐ'ํ•˜๋Š” ๋Šฅ๋ ฅ์ด ํ•„์š”ํ•˜๋‹ค. ์ด๋Š” ๊ณผ๊ฑฐ์˜ ๊ณต๊ธ‰์ž ์ค‘์‹ฌ(๋‚˜ ์ค‘์‹ฌ)์—์„œ ์ˆ˜์š”์ž ์ค‘์‹ฌ( ํƒ€์ธ ์ค‘์‹ฌ)์œผ๋กœ ์‚ฌ๊ณ ์˜ ์ „ํ™˜์„ ์š”๊ตฌํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด '๋‚˜ ์ค‘์‹ฌ'์—์„œ๋Š” '๋‚˜์˜ ๊ธฐ์ˆ ๊ณผ ์ง€์‹์œผ๋กœ ๋ฌด์—‡์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„๊นŒ?'๋ผ๋Š” ๋ณธ์›์  ์งˆ๋ฌธ์ด ์ค‘์š”ํ–ˆ๋‹ค๋ฉด 'ํƒ€์ธ ์ค‘์‹ฌ'์—์„œ๋Š” '์‚ฌ๋žŒ๋“ค์€ ๋ฌด์—‡์„ ์›ํ•˜๊ณ  ๋‚˜๋Š” ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ?'๋กœ ๋ณธ์›์  ์งˆ๋ฌธ์ด ์ „ํ™˜๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” Opportunity-based Strategy์— ๊ธฐ๋ฐ˜ํ•œ ์„ค๋ฃจ์…˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์˜ ์‚ฌ์ƒ๊ณผ ํ†ตํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. (์•„๋ž˜ ๊ธ€ ์ฐธ์กฐ) ๊ทธ๋ž˜์„œ ์„ค๋ฃจ์…˜ ์‚ฌ์—… ๊ณ ๋ฏผํ•œ๋‹ค. (1/2) ์„ค๋ฃจ์…˜์ด ๋ญ๋ผ๊ณ ? ์•„ ... ๋‚œ ์—ฌํƒœ๊นŒ์ง€ ์ž˜๋ชป ์•Œ๊ณ  ์žˆ์—ˆ๋„ค. | B2B ์˜์—…์€ ์˜์—… ๋ฐฉ์‹(Sales Motion)์— ๋”ฐ๋ผ Volume Sales์™€ Value Sales๋กœ ๊ตฌ๋ถ„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ Volume Sales๋Š” ๋Œ€๋Ÿ‰(Bulk)์˜ ์ œํ’ˆ์„ ์œ ํ†ต์ฑ„๋„์„ ํ†ตํ•ด ๊ธฐ์—…๊ณ ๊ฐ์—๊ฒŒ ํŒ๋งคํ•˜๋Š” ๊ฒƒ์œผ๋กœ, B2C ์„ธ์ผ์ฆˆ์ฒ˜๋Ÿผ ์ฑ„๋„ ๋ฐ ํŒŒํŠธ๋„ˆ ๊ด€๋ฆฌ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ Value Sales๋Š” B2C ์„ธ์ผ์ฆˆ ๋˜๋Š” Volume Sales์™€๋Š” ์ƒ๋‹นํžˆ brunch.co.kr/@flyingcity/8 ๋””์ž์ธ ์‹ฑํ‚น์˜ ๋‘ ๋ฒˆ์งธ ํŠน์ง•์€ ํ†ตํ•ฉ์  ์‚ฌ๊ณ (Highly Creative)์ด๋‹ค. ๋””์ž์ธ ์‹ฑํ‚น์€ ์ƒํ™ฉ์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ๊ด€์ ์˜ ๊ด€์ฐฐ์„ ํ†ตํ•ด ๊ธฐ์กด์˜ ๋Œ€์•ˆ์„ ๋›ฐ์–ด๋„˜๋Š” ์ƒˆ๋กœ์šด ํ•ด๊ฒฐ์ฑ…์„ ์ œ์•ˆํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋ฉฐ ๋ฌธ์ œ์˜ ๋ชจ๋“  ์ธก๋ฉด์„ ๋‹ค์–‘ํ•˜๊ฒŒ ๊ด€์ฐฐํ•˜๋Š” ํ†ตํ•ฉ์  ์‚ฌ๊ณ ๊ฐ€ ํ•ต์‹ฌ์ด๋‹ค. ์•ž์„œ ๋ฐฐ์› ๋˜ ๋กœ์ง ํŠธ๋ฆฌ์— ๊ธฐ๋ฐ˜ํ•œ ์ „ํ†ต์ ์ธ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ธฐ๋ฒ•์ด ๋ถ„์„์  ์‚ฌ๊ณ ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ์—ฐ์—ญ์ (Deductive) ๋ฐฉ๋ฒ• ๋ฐ ๊ท€๋‚ฉ์ (Inductive) ๋ฐฉ๋ฒ•์„ ์ฃผ๋กœ ํ™œ์šฉํ–ˆ๋‹ค๋ฉด, ๋””์ž์ธ ์‹ฑํ‚น์€ ๊ท€์ถ” ๋…ผ๋ฆฌ(abductive reasoning)[3]๋ฅผ ํ†ตํ•ด ๋ถ„์„์  ์‚ฌ๊ณ ์˜ ์ˆ™๋ จ๊ณผ ์ง๊ด€์  ์‚ฌ๊ณ ์˜ ์ฐฝ์กฐ์„ฑ์ด ์—ญ๋™์ ์œผ๋กœ ๊ท ํ˜•์„ ์ด๋ฃจ๊ณ  ์žˆ๋‹ค๊ณ  ๋งํ•œ๋‹ค. ์„ธ ๋ฒˆ์งธ ํŠน์ง•์€ ์‹œํ–‰์ฐฉ์˜ค๋ฅผ ํ†ตํ•œ ๋ฌด์ˆ˜ํ•œ ์‹คํ—˜์ด๋‹ค. (Hands-On) ํ† ์˜๋ฅผ ์ค‘๋‹จํ•˜๊ณ  ์ง์ ‘ ์†์„ ์‚ฌ์šฉํ•ด ๋จธ๋ฆฟ์†์˜ ์•„์ด๋””์–ด๋ฅผ ์‹คํ˜„์‹œํ‚ค๋Š” ๊ฒƒ์ด๋‹ค. ํ”„๋กœํ†  ํƒ€์ดํ•‘(Prototyping)์„ ํ†ตํ•ด ๋จธ๋ฆฟ์†์˜ ๊ฐ€์„ค์„ ํ…Œ์ŠคํŠธํ•˜๊ณ  ์‹คํ˜„์‹œ์ผœ๋ณธ๋‹ค. Figure II-30. ๋‹ค์–‘ํ•œ ํ”„๋กœํ†  ํƒ€์ดํ•‘ ํ™œ๋™ ๋„ค ๋ฒˆ์งธ, ํŠน์ง•์€ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (Iterative) ๋””์ž์ธ ์‹ฑํ‚น์€ ํ•œ ๋ฒˆ์— ์™„์„ฑ๋œ ๊ฒƒ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ง€์†์ ์ธ ๋ฐ˜๋ณต์„ ํ†ตํ•ด ์ ์ง„์ ์œผ๋กœ ์™„์„ฑ์˜ ๋ชจ์Šต์„ ๊ฐ–์ถ”์–ด ๋‚˜๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” ์Šคํƒ€ํŠธ์—…(Startup)์—์„œ MVP[4]๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ๋„ ๊ฐ™์€ ๋…ผ๋ฆฌ๋กœ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. Figure II-31. ์ ์ง„์ ์ด๊ณ  ๋ฐ˜๋ณต์ ์ธ ๊ฐœ๋ฐœ ์ด์™€ ๊ฐ™์€ ํŠน์ง•๋“ค์„ ๊ณ ๋ คํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค๋กœ ๊ตฌ์„ฑํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Figure II-32. Design Thinking ํ”„๋กœ์„ธ์Šค ๋ฌธ์ œ์˜ ๋ณธ์งˆ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋‚˜์˜ ๊ด€์ ์ด ์•„๋‹ˆ๋ผ ์‚ฌ๋žŒ์˜ ๊ด€์ ์—์„œ ๊ณต๊ฐํ•˜๊ณ  ์š”๊ตฌ๋ฅผ ์ •์˜ํ•˜๋ฉฐ ํ†ต์ฐฐ๋ ฅ์„ ์–ป๊ณ , ์ฐฝ์˜์ ์ธ ์•„์ด๋””์–ด๋ฅผ ํ†ตํ•ด ์ œ๊ณตํ•˜๊ณ ์ž ํ•˜๋Š” ์ œํ’ˆ/์„œ๋น„์Šค๋ฅผ ๊ตฌ์ƒํ•˜๊ณ  ์ด๋ฅผ ํ”„๋กœํ† ํŒŒ์ž…๊ณผ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ์™„์„ฑ๋„๋ฅผ ๋†’์—ฌ๊ฐ„๋‹ค. ๋””์ž์ธ ์‹ฑํ‚น์€ 2000๋…„ ๋Œ€ ํ›„๋ฐ˜ IDEO๋ฅผ ํ†ตํ•ด ๋„๋ฆฌ ์•Œ๋ ค์ง€๊ธฐ ์‹œ์ž‘ํ•˜๋ฉด์„œ ์‹ค๋ฆฌ์ฝ˜๋ฐธ๋ฆฌ์— ๋ถˆ๊ธฐ ์‹œ์ž‘ํ•œ ์Šคํƒ€ํŠธ์—… ๊ธฐ์—…๋“ค์˜ ์‚ฌ์—… ์ถ”์ง„ ๋ฐฉ๋ฒ•๋ก ์œผ๋กœ ๊ฐ๊ด‘๋ฐ›์•˜๋‹ค. ํŠนํžˆ, ๋ฆฐ ์Šคํƒ€ํŠธ์—…(Lean Startup)๊ณผ ์ ‘๋ชฉ๋˜๋ฉด์„œ ๋ฌธ์ œ์™€ ์„ค๋ฃจ์…˜์„ ๊ณ ๋ฏผํ•˜๊ณ , ์‹œ์žฅ ์ˆ˜์š”๋ฅผ ์ ๊ฒ€ํ•˜๊ณ  ์ œํ’ˆ์˜ ์™„์„ฑ๋„๋ฅผ ๋†’์ด๋ฉฐ ์‹œ์žฅ ๋ฐ ์‚ฌ์—… ๊ทœ๋ชจ๋ฅผ ํ™•์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ์‹คํšจ์„ฑ์„ ์ธ์ •๋ฐ›๊ธฐ ์‹œ์ž‘ํ•˜์˜€๊ณ  ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๊ธ€๋กœ๋ฒŒ ๋Œ€๊ธฐ์—…๋“ค๋„ ๊ด€์‹ฌ์„ ๊ฐ€์ง€๊ฒŒ ๋˜์—ˆ๋‹ค. ์ตœ๊ทผ์—๋Š” ๊ตญ๋‚ด ๊ธฐ์—…๋“ค๋„ ๋ฌธ์ œ ํ•ด๊ฒฐ์ด ๋‚˜ ์‹ ์‚ฌ์—… ์ถ”์ง„์˜ ๋ฐฉ๋ฒ•๋ก ์œผ๋กœ ๋””์ž์ธ ์‹ฑํ‚น์„ ์ ๊ทน ํ™œ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. TRIZ๋ฉฐ ๋””์ž์ธ ์‹ฑํ‚น์ด๋ฉฐ ๋งค์šฐ ๋ฐฉ๋Œ€ํ•˜๊ณ  ์ „๋ฌธ์ ์ธ ์ฃผ์ œ์ธ๋ฐ ๋ฌธ์ œ ํ•ด๊ฒฐ ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ ๊ฐ„๋žตํ•˜๊ฒŒ๋‚˜๋งˆ ๋‹ค๋ฃจ์–ด ๋ณด์•˜๋‹ค. ๋…์ž ์—ฌ๋Ÿฌ๋ถ„๋“ค์˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์— ๋Œ€ํ•œ ์ธ์‹๊ณผ ์ƒ๊ฐ์ด ์ข€ ๋” ํ™•์žฅ๋˜์—ˆ๊ธฐ๋ฅผ ํฌ๋งํ•œ๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ๋Š” ์ปจ์„คํŒ… ์Šคํ‚ฌ์˜ ๋งˆ์ง€๋ง‰์ด๋ฉด์„œ ์ •๋ง ์ค‘์š”ํ•œ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ - ์ธํ„ฐ๋ทฐ, ํšŒ์˜, ๋ฌธ์„œํ™”, ํ”„๋ ˆ์  ํ…Œ์ด์…˜์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ณด์ž. [1] TRIZ๋Š” ๋ฐœ๋ช…ํŠนํ—ˆ๋ฅผ ์—ฐ๊ตฌ, ์ •๋ฆฌํ•˜๋‹ค ๋ฐœ๊ฒฌ๋œ ๊ฒƒ์œผ๋กœ ์ฃผ๋กœ ๊ธฐ์ˆ  ์ค‘์‹ฌ์˜ ์—ฐ๊ตฌ๊ฐœ๋ฐœ์— ์œ ์šฉํ•˜์˜€๋‹ค. 2003๋…„ ์˜๊ตญ ๋ฐฐ์Šค ๋Œ€ํ•™์˜ Darrell Mann ๊ต์ˆ˜๋Š” ๊ธฐ์ˆ  ์—ฐ๊ตฌ๋ฅผ ์œ„ํ•œ 39๊ฐ€์ง€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋น„์ฆˆ๋‹ˆ์Šค ๊ด€์ ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” 31๊ฐ€์ง€๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ๋„ ํ•˜์˜€๋‹ค. [2] www.sap.com ๋…์ผ์˜ ์œ ๋ช… ERP ์ „๋ฌธ ๊ธฐ์—…. [3] ์–ด๋–ค ๋†€๋ž„๋งŒํ•œ ํ˜„์ƒ์ด ๋‚˜ํƒ€๋‚ฌ์„ ๋•Œ ๊ทธ ํ˜„์ƒ์„ ์„ค๋ช…ํ•ด ์ฃผ๋Š” ๊ฐ€์„ค์„ ์ถ”๋ก ํ•˜๋Š” ๊ณผ์ •. ์˜ˆ๋ฅผ ๋“ค์–ด ์š”ํ•˜๋„ค์Šค ์ผ€ํ”Œ๋Ÿฌ(Johannes Kepler. 1571 ~ 1630)๋Š” ํ™”์„ฑ ๊ถค๋„๊ฐ€ ํƒ€์›์ด๋ผ๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์„ ๋•Œ ์–ด๋–ค ์—ฐ์—ญ๋ฒ•์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ทธ์˜ ์Šค์Šน์ด์—ˆ๋˜ ํ‹ฐ์ฝ” ๋ธŒ๋ผํ˜œ(Tycho Brahe. 1546 ~ 1601)์˜ ํ–‰์„ฑ ๊ด€์ฐฐ ์ž๋ฃŒ๋ฅผ ํ† ๋Œ€๋กœ ํ™”์„ฑ ๊ถค๋„๊ฐ€ ํƒ€์›์ด๋ผ๋Š” ๊ฒƒ์„ ๊ถ๊ทน์  ๊ฐ€์„ค์— ๋„๋‹ฌํ•˜์˜€๋‹ค. [4] Minimum Viable Product. ์ตœ์†Œ ๊ธฐ๋Šฅ๋งŒ ๋™์ž‘ํ•˜๊ฒŒ ํ•˜์—ฌ ์‹œ๋‚˜๋ฆฌ์˜ค์˜ ์ˆ˜์šฉ ์—ฌ๋ถ€๋ฅผ ๋ณด๋ฉฐ ์™„์„ฑ๋„๋ฅผ ๋†’์—ฌ๊ฐ„๋‹ค ๊ฐ™์ด ์ฝ์–ด๋ณด๋ฉด ์ข‹์€ ์ฑ… kx โ–ถ 40 Principles: TRIZ Keys to Innovation, Genrich Altshuller, 2005 The Design of Business: Why Design Thinking is the Next Competitive Advantege, Roger L.Martin, 2009 The Lean Startup, Eric Ries, 2011 06. ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ(1/3) - ์ธํ„ฐ๋ทฐ โ€˜์ด๊ด„๋‹˜, ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์ปจ์„คํŒ…์„ ์ž˜ํ•  ์ˆ˜ ์žˆ๋‚˜์š”?โ€™, โ€˜๋ฌด์Šจ ์ข‹์€ ๊ธฐ๋ฒ•๊ณผ ๋„๊ตฌ๊ฐ€ ์žˆ๋‚˜์š”?โ€™, โ€˜์–ด๋–ค ๋ฐฉ๋ฒ•๋ก ์ด ๊ฐ€์žฅ ํšจ์œจ์ ์ธ๊ฐ€์š”?โ€™ ์‹ ์ž… ์ปจ์„คํ„ดํŠธ๋“ค์ด ์ž…์‚ฌํ•˜๊ณ  ๊ทธ๋“ค์„ ์ด๋Œ ์ง€์œ„์— ์˜ค๋ฅด๋ฉด ์ด๋Ÿฐ ์งˆ๋ฌธ๋“ค์„ ๊ณง์ž˜ ๋“ฃ๊ณค ํ•  ๊ฒƒ์ด๋‹ค. ์ €์ž์˜ ๊ฒฝ์šฐ๋Š” ๊ทธ๋Ÿฐ ์งˆ๋ฌธ์„ ๋“ค์—ˆ์„ ๋•Œ, '์šฐ์„  ๊ณ ๊ฐ์˜ ๋ชฉ์†Œ๋ฆฌ[1]์— ์ฃผ๋ชฉํ•˜๋ผ'๋ผ๊ณ  ์กฐ์–ธํ•ด ์ฃผ์—ˆ๋‹ค. ์‚ฌ์‹ค ๋ชจ๋“  ๋ฌธ์ œ์™€ ๋‹ต์ด ๊ฑฐ๊ธฐ์— ์žˆ๋‹ค. ๊ณ ๊ฐ๊ณผ์˜ ํšŒ์˜(meeting)์™€ ์ธํ„ฐ๋ทฐ(interview)๋Š” ๊ณ ๊ฐ์˜ ์š”๊ตฌ(demand or requirements)๋ฅผ ํŒŒ์•…ํ•ด ๋‚ด๊ธฐ ์œ„ํ•œ ๋Œ€ํ‘œ์ ์ธ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ๊ธฐ๋ฒ•๋“ค์ด๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์–ป์–ด์ง€๋Š” ์‹œ์‚ฌ์ ๋“ค์€ ๋ฌธ์„œํ™”(Documentation)์™€ ํ”„๋ ˆ์  ํ…Œ์ด์…˜(Presentation)์œผ๋กœ ๊ณ ๊ฐ์—๊ฒŒ ๊ณต์œ ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ œ๋Œ€๋กœ ๋œ ๊ธฐ๋ฒ•์˜ ์ดํ•ด ์—†์ด ์‚ฌ์šฉํ•˜๋ฉด ํšจ์œจ์ ์ด์ง€ ๋ชปํ•˜๋ฉฐ, ๊ทธ ์ˆจ์€ ๋ฉ”์‹œ์ง€๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ๋งŽ์€ ์‹œํ–‰์ฐฉ์˜ค๋ฅผ ๊ฒช๋Š”๋‹ค. ์ฝ˜ํ…์ธ ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ ์•„๋งˆ์ถ”์–ด๋Š” ์ž์‹ ์ด ํ•˜๊ณ  ์‹ถ์€ ๋ง์„ ์ „๋‹ฌํ•˜๊ณ , ํ”„๋กœ๋Š” ์ƒ๋Œ€๋ฐฉ์ด ์›ํ•˜๋Š” ๊ฒƒ์„ ์ „๋‹ฌํ•˜๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์–ด๋–ค ๊ฒฝ์šฐ์—๋“  ํšจ๊ณผ์ ์ธ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์€ โ€˜์ž์‹ ์˜ ์˜๋„๋Œ€๋กœ ์ƒ๋Œ€๋ฐฉ์ด ์ƒ๊ฐํ•˜๊ณ  ํ–‰๋™ํ•˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒโ€™์ž„์„ ์žŠ์–ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. 3์žฅ์—์„œ๋Š” ์ปจ์„คํŒ… ์Šคํ‚ฌ์˜ ์„ธ ๋ฒˆ์งธ๋กœ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ณด์ž. ๋„ˆ๋ฌด๋‚˜ ๋งŽ์ด ์ ‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์ด๋ผ โ€˜์ด๊ฒƒ๋„ ์Šคํ‚ฌ์ธ๊ฐ€?โ€™๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ํŠน๋ณ„ํ•˜์ง€ ์•Š๊ฒŒ ๋Š๋‚„์ง€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ์ด ์žฅ์ด ๋๋‚˜๋ฉด ๊ทธ ์ƒ๊ฐ์ด ๋ฐ”๋€”์ง€๋„ ๋ชจ๋ฅธ๋‹ค. 6.1 ์ธํ„ฐ๋ทฐ ์ž˜ํ•˜๊ธฐ ์ธํ„ฐ๋ทฐ(Interview)๋ž€ ๋ฌด์—‡์ผ๊นŒ? ํด๋ผ์ด์–ธํŠธ์™€ ๋งŒ๋‚˜์„œ ์ด์•ผ๊ธฐํ•˜๋Š” ๊ฒƒ? ์ธํ„ฐ๋ทฐ์— ๋™๋ฐ˜ํ•œ ๋‹ค๋ฅธ ํŒ€์›์€ ์˜†์—์„œ ๋ฌด์–ธ๊ฐ€๋ฅผ ์ ๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ๋งŒ ๋Œ€๊ฐ• ๋– ์˜ฌ๋ฆฐ๋‹ค๋ฉด ์ธํ„ฐ๋ทฐ๋ฅผ ์ž˜ ๋ชจ๋ฅด๋Š” ๊ฒƒ์ด๋ผ ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ์ธํ„ฐ๋ทฐ๋Š” ํ˜„์•ˆ์— ๋Œ€ํ•œ ์กฐ์‚ฌ๋ฅผ ํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ๋‚˜ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•จ๊ณผ ๋™์‹œ์— ๊ณ ๊ฐ ๊ด€๊ณ„(Customer Relationship)๋ฅผ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ€๋Š” ๊ณ ๊ธ‰ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ๊ธฐ๋ฒ•์ด๋‹ค. ๊ทธ๋ž˜์„œ ๋งค์šฐ ์กฐ์‹ฌ์Šค๋Ÿฝ๊ณ  ๊ทธ ๊ฒฐ๊ณผ์˜ ํŒŒ๊ธ‰ ํšจ๊ณผ๋ฅผ ์ž˜ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ์˜ ์„ฑ๊ฒฉ์— ๋”ฐ๋ผ ํŠน์ •ํ•œ ์ธํ„ฐ๋ทฐ์˜ ๋ชฉ์ ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ์— ๋”ฐ๋ผ ์ ์ •ํ•œ ์ธํ„ฐ๋ทฐ์˜ ์ฃผ์ œ, ์ธํ„ฐ๋ทฐ์˜ ๋Œ€์ƒ, ๊ฒฐ๊ณผ์˜ ๋ถ„์„ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๋Œ€์ฒด์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์œ ๋กœ ์ธํ„ฐ๋ทฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ํ˜„ํ™ฉ ๋ถ„์„์„ ์œ„ํ•œ ๊ธฐ์ดˆ ์ž๋ฃŒ ์ˆ˜์ง‘ ํ”„๋กœ์ ํŠธ ์ฃผ์š” ์ฑ…์ž„์ž๋“ค์˜ ์˜๊ฒฌ ์ฒญ์ทจ ํ”„๋กœ์ ํŠธ์˜ ๊ด€์‹ฌ์„ ๋†’์ด๊ณ  ์ ๊ทน์ ์ธ ์ฐธ์—ฌ ์œ ๋„ ์ธํ„ฐ๋ทฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ Figure II-33๊ณผ ๊ฐ™์ด ์ธํ„ฐ๋ทฐ ์‚ฌ์ „ ์ค€๋น„๋ฅผ ํ•˜๊ณ , ์ธํ„ฐ๋ทฐ๋ฅผ ์‹คํ–‰ํ•˜๋ฉฐ, ์ธํ„ฐ๋ทฐ ๊ฒฐ๊ณผ๋ฅผ ์š”์•ฝํ•˜๋Š” 3๋‹จ๊ณ„๋กœ ์ง„ํ–‰๋œ๋‹ค. Figure II-33. ์ธํ„ฐ๋ทฐ์˜ 3๋‹จ๊ณ„ ์ ˆ์ฐจ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„์ธ ์ธํ„ฐ๋ทฐ ์‚ฌ์ „ ์ค€๋น„๋Š” ์ธํ„ฐ๋ทฐ ์ง„ํ–‰ ์ „, ์ธํ„ฐ๋ทฐ ๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•˜๊ณ  ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์„ ์„ ์ •ํ•˜๋ฉฐ, ์„ธ๋ถ€ ์ผ์ •์„ ์žก๊ณ  ์ธํ„ฐ๋ทฐ ์งˆ์˜์„œ๋ฅผ ๋งŒ๋“œ๋Š” ์ผ ๋“ฑ์„ ํฌํ•จํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธํ„ฐ๋ทฐ ์‹คํ–‰ ๋‹จ๊ณ„๋Š” ๋ณดํ†ต 2์ธ 1์กฐ๋กœ ๊ตฌ์„ฑ๋œ ์ธํ„ฐ๋ทฐ ํŒ€์ด ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž๋“ค์„ ๋งŒ๋‚˜ ์ฃผ์ œ์™€ ๊ด€๋ จ๋œ ์งˆ์˜์‘๋‹ต์„ ์ง„ํ–‰ํ•˜๋ฉฐ ์ธํ„ฐ๋ทฐ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ๋งํ•˜๋ฉฐ, ์„ธ ๋ฒˆ์งธ ๋‹จ๊ณ„์ธ ์ธํ„ฐ๋ทฐ ๊ฒฐ๊ณผ, ๋„์ถœ๋œ ์‹œ์‚ฌ์ ๊ณผ ํ•ต์‹ฌ ํ˜„์•ˆ์„ ํŒŒ์•…ํ•˜์—ฌ ๋ฌธ์ œ์  ๋ฐ ๊ฐœ์„  ์‚ฌํ•ญ ๋ฐœ๊ฒฌ์— ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. ๋ชจ๋“  ์ผ์ด ๊ทธ๋ ‡์ง€๋งŒ ์ธํ„ฐ๋ทฐ๋Š” ์ฒด๊ณ„์ ์œผ๋กœ ์ค€๋น„ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ทธ๋ƒฅ ๋ช…ํ•จ ๋ฐ›์•„์˜ค๋Š” ๊ฒƒ์œผ๋กœ ๋๋‚˜๊ฒŒ ๋œ๋‹ค. ํšจ์œจ์ ์œผ๋กœ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? ํšจ์œจ์ ์ธ ์ธํ„ฐ๋ทฐ ๊ณ„ํš์„œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ์‚ฌํ•ญ๋“ค์„ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋ฌด์—‡์— ์ง‘์ค‘ํ•  ๊ฒƒ์ธ๊ฐ€? ์–ด๋–ค ์ •๋ณด๋ฅผ ์•Œ๊ณ , ์–ป๊ณ  ์‹ถ์€๊ฐ€? ํ•„์š”ํ•œ ์ •๋ณด๋Š” ์–ด๋–ป๊ฒŒ ํ™•๋ณดํ•  ๊ฒƒ์ธ๊ฐ€? ์ธํ„ฐ๋ทฐ์—๋Š” ์–ด๋–ค ์งˆ๋ฌธ์„ ์ ์ ˆํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ๋ˆ„๊ตฌ๋ฅผ ์ธํ„ฐ๋ทฐํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ์œ„ ๊ณ ๋ ค ์‚ฌํ•ญ๋“ค์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋ˆ„๊ตฌ๋ฅผ ์–ด๋””์—์„œ ๋งŒ๋‚˜์„œ ๋ฌด์—‡์„ ์–ด๋–ป๊ฒŒ[2] ์งˆ๋ฌธํ•  ๊ฒƒ์ธ์ง€ ๋“ฑ ์ธํ„ฐ๋ทฐ ๊ณ„ํš์„œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ๋˜, ์ธํ„ฐ๋ทฐ๋Š” ์ƒํ™ฉ์— ๋งž๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”๋ฐ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์€ ์ธํ„ฐ๋ทฐ ๋‹น์‚ฌ์ž์™€ ์ธํ„ฐ๋ทฐ ํŒ€์ด ๋งŒ๋‚˜์„œ ์ง์ ‘ ์ด์•ผ๊ธฐ๋ฅผ ๋‚˜๋ˆ„๋Š” ๋Œ€๋ฉด ์ธํ„ฐ๋ทฐ(Face-to-Face Interview)์ด๋‹ค. ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ด ์›๊ฒฉ์ง€์— ์žˆ์„ ๊ฒฝ์šฐ, ์ „ํ™”๋ฅผ ์‚ฌ์šฉํ•œ ์ „ํ™” ์ธํ„ฐ๋ทฐ๋„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ฉฐ, ์ƒ๋Œ€์™€ ๋งŒ๋‚˜์ง€ ์•Š๊ณ  ์„œ๋ฉด์œผ๋กœ ์ธํ„ฐ๋ทฐํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค. ๋˜ํ•œ, ํŠน์ • ์ง‘๋‹จ์— ๋Œ€ํ•ด ๊นŠ์ด ์žˆ๋Š” ์ธํ„ฐ๋ทฐ๋ฅผ ์ง„ํ–‰ํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ํฌ์ปค์Šค๊ทธ๋ฃน ์ธํ„ฐ๋ทฐ(FocusGroup Interview. FGI)๋ฅผ ํ™œ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค. Table II-8. ์ธํ„ฐ๋ทฐ์˜ ์œ ํ˜• ์ธํ„ฐ๋ทฐ ์งˆ์˜์„œ ๋˜ํ•œ ๋งค์šฐ ๊ด€์‹ฌ ๊นŠ๊ฒŒ ์ค€๋น„ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ ๊ทธ๋ƒฅ ์งˆ๋ฌธ ๋ชฉ๋ก์„ ๋งŒ๋“ค๊ณ  ๊ทธ์น  ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์งˆ๋ฌธ์ž๊ฐ€ ๋‹ต๋ณ€ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ๋‚ด์šฉ๋“ค์„ ์‚ฌ์ „์— ์ •๋ฆฌํ•ด ๋ณด๊ณ  ๊ทธ์™€ ๊ด€๋ จ๋œ ์ถ”๊ฐ€ ์งˆ๋ฌธ์ด๋‚˜ ๋Œ€์‘ ์ƒํ™ฉ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ •๋ฆฌํ•˜๋Š” ์ผ์ด ํ•„์š”ํ•˜๋‹ค. ์ผ์ข…์˜ ์‚ฌ์ „ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋Š” ๊ฒƒ์ธ๋ฐ ์ธํ„ฐ๋ทฐ ์ „์ฒด๋ฅผ ์ฃผ๋„์ ์œผ๋กœ ์ด๋Œ์–ด ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์›๋™๋ ฅ์ด ๋œ๋‹ค. ๊ฐ€์„ค์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž์˜ ๋‹ต๋ณ€์„ ๋ฏธ๋ฆฌ ์ฑ„์›Œ๋ณด๊ฑฐ๋‚˜ ์˜ˆ์ƒ๋˜๋Š” ์‹œ์‚ฌ์ ์˜ ์ •๋Ÿ‰ํ™”๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ํ‘œ๋‚˜ ๊ทธ๋ž˜ํ”„ ๊ฐ™์€ ์ฐจํŠธ๋ฅผ ๊ตฌ์„ฑํ•ด ๋ณด๋Š” ๊ฒƒ๋„ ์ข‹๋‹ค. ๋‹จ์ˆœํžˆ ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž๊ฐ€ ์ด์•ผ๊ธฐํ•ด ์ฃผ๋Š” ๊ฒƒ์„ ๋ฐ›์•„ ์ ๋Š”๋‹ค๋Š” ์ˆ˜๋™์ ์ธ ์ž์„ธ๋กœ ์ธํ„ฐ๋ทฐ๋ฅผ ์ž„ํ•˜๋ฉด ๋ฐ˜๋“œ์‹œ ์‹คํŒจํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฌ์ „์— ์ธํ„ฐ๋ทฐ ๋‚ด์šฉ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์„ค ๊ธฐ๋ฐ˜์œผ๋กœ ์˜ˆ์ธกํ•˜์—ฌ ์ •๋ฆฌํ•ด ๋ณด๋Š” ์ผ์€ ์ค‘์š”ํ•˜๋‹ค. ์‚ฌ๋žŒ๋“ค์„ ๋งŒ๋‚˜๋ณด๋ฉด ๋ถ„๋ช…ํžˆ ๋›ฐ์–ด๋‚œ ํ™”์ˆ ๊ณผ ์ž„๊ธฐ ์‘๋ณ€์— ๋Šฅํ•œ ์‚ฌ๋žŒ์ด ์ƒ๋Œ€์ ์œผ๋กœ ์ธํ„ฐ๋ทฐ ์ˆ˜ํ–‰์— ์œ ๋ฆฌํ•œ ๊ฒƒ์€ ์‚ฌ์‹ค์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ผ๋ฐ˜์ ์œผ๋กœ ์ธํ„ฐ๋ทฐ๋Š” Figure II-33๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ง„ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ˆ™์ง€ํ•˜๋ฉด ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ธํ„ฐ๋ทฐ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋„์ž…๋ถ€์—์„œ๋Š” ์•„์ด์Šค๋ธŒ๋ ˆ์ดํ‚น(Ice breaking)์ด๋‚˜ ์Šค๋ชฐ ํ† ํฌ(small talk)๋ฅผ ์ด์šฉํ•ด์„œ ์ฒซ ๋งŒ๋‚จ์˜ ์–ด์ƒ‰ํ•จ ๋ฐ ๋ถ„์œ„๊ธฐ๋ฅผ ํ‘ธ๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค. ๋˜ํ•œ, ์ธํ„ฐ๋ทฐ์˜ ๊ธฐ์ˆ ์ ์ธ ์ธก๋ฉด์„ ์ข€ ๋” ์‚ดํŽด๋ณด์ž๋ฉด Figure II-34๊ณผ ๊ฐ™์ด ์งˆ์˜ ๋‹จ๊ณ„, ์ •์ทจ ๋‹จ๊ณ„, ์ข…ํ•ฉ ๋‹จ๊ณ„์˜ 3๋‹จ๊ณ„ ๊ตฌ์กฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Figure II-34. ์ธํ„ฐ๋ทฐ์˜ ๊ตฌ์กฐ ์งˆ์˜์™€ ๊ด€๋ จํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ์„ ์œ ๋…ํ•˜์ž. ๋ฏผ๊ฐํ•œ ๋‚ด์šฉ์ด๋‚˜ ๋ณด์•ˆ์— ๊ด€๋ จ๋œ ์งˆ๋ฌธ์€ ์กฐ์‹ฌ์Šค๋Ÿฝ๊ฒŒ ์ด์•ผ๊ธฐํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž์˜ ์–ธ์–ด ์ฆ‰, ํ˜„์žฅ์˜ ์–ธ์–ด๋กœ ์งˆ๋ฌธํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. ์ปจ์„คํ„ดํŠธ๋“ค์ด ์ธํ„ฐ๋ทฐ์—์„œ ์‹คํŒจํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์š”์ธ์€ ํ˜„ํ•™์ (่ก’ๅญธ็š„)์ธ ํ‘œํ˜„์„ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋‚˜์˜ ์ง€์‹๊ณผ ๊ฒฝํ—˜์„ ๋ฝ๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž์˜ ์ด์•ผ๊ธฐ๋ฅผ ๋Œ์–ด๋‚ด์•ผ ํ•œ๋‹ค. ์ธํ„ฐ๋ทฐ ๋„์ค‘, ์ธํ„ฐ๋ทฐ ๋‚ด์šฉ์„ ์ง€์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์„œ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ํ™•๋ณดํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๊ฒƒ์„ ์œ„ํ•ด์„œ๋ผ๋„ ํ•ญ์ƒ ์˜ˆ์˜ ๋ฐ”๋ฅด๊ฒŒ ๋Œ€ํ•ด์•ผ ํ•œ๋‹ค. ์ธํ„ฐ๋ทฐ๋Š” ๊ฐ์‚ฌ๊ฐ€ ์•„๋‹ˆ๋‹ค. ๊ฐ„ํ˜น, ์ง„๋‹จ๊ณผ ์ผ๋ฐ˜ ํ”„๋กœ์ ํŠธ๋ฅผ ํ˜ผ๋™ํ•˜์—ฌ ์ธํ„ฐ๋ทฐ์˜ ์„ฑ๊ฒฉ์„ ์ž˜๋ชป ํŒŒ์•…ํ•˜๋Š” ์ปจ์„คํ„ดํŠธ๋“ค๋„ ์žˆ๋Š”๋ฐ ์ผ๋ฐ˜์ ์œผ๋กœ ์ธํ„ฐ๋ทฐ๋Š” ๊ด€๊ณ„๋ฅผ ๋งŒ๋“ค์–ด๋‚˜๊ฐ€๋Š” ๋ชฉ์ ์ด ๊ฐ€์žฅ ๊ฐ•ํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ์ธํ„ฐ๋ทฐ ์ข…๋ฃŒ ํ›„ ์ž๋ฆฌ๋ฅผ ๋– ๋‚˜๊ฐ„ ํ›„์—๋„ ์ธํ„ฐ๋ทฐ ์ˆ˜ํ–‰์ž๋“ค์— ๋Œ€ํ•ด ๊ธ์ •์ ์ธ ์ด๋ฏธ์ง€๋ฅผ ์ค„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. Figure II-35. ์ธํ„ฐ๋ทฐ ์งˆ์˜์‘๋‹ต ์ธํ„ฐ๋ทฐ์˜ ์งˆ๋ฌธ์€ ํฌ๊ฒŒ Open Questions๊ณผ Closed Questions์œผ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, Open Questions์€ ์˜ˆ, ์•„๋‹ˆ์˜ค(Yes or No)์™€ ๊ฐ™์€ ๋‹ต์ด ์•„๋‹Œ ์—ด๋ฆฐ ์‘๋‹ต์„ ํ†ตํ•ด ์‚ฌ์•ˆ์— ๋Œ€ํ•œ ์˜๊ฒฌ์„ ์ถฉ๋ถ„ํžˆ ๋“ฃ๋Š”<NAME>์˜ ์งˆ๋ฌธ์ด๋‹ค. Closed Questions๋Š” ๊ทธ์™€ ๋ฐ˜๋Œ€๋กœ ์˜ˆ, ์•„๋‹ˆ์˜ค๋ฅผ ์š”๊ตฌํ•˜๋Š” ์งˆ๋ฌธ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜๋ฐฅ ๋จน์—ˆ์–ด? ์‘โ€™ ์ด๋Ÿฐ ์งˆ๋ฌธ์€ ๋Œ€ํ™”๊ฐ€ ์‰ฝ๊ฒŒ ๋Š์–ด์ง€๊ณ  ์ง€์†์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์ง€ ์•Š๋Š”๋‹ค. โ€™์˜ค๋Š˜ ์ ์‹ฌ ๋ญ ๋จน์—ˆ์–ด์š”? ์„ค๋ ํƒ•์ด์š”. ์–ด๋””์„œ์š”? ์‚ฌ๊ฑฐ๋ฆฌ ์•ž์—์„œ ์ƒˆ๋กœ ์ƒ๊ธด ๊ณณ์ด์š”โ€˜, โ€˜๋‚˜๋„ ๊ฑฐ๊ธฐ ๋ดค๋Š”๋ฐ ๊ฑฐ๊ธฐ๊ฐ€ ์ •๋ง ๋ง›์žˆ๋‚˜ ๋ด์š”?โ€™ ๋“ฑ ๊ฐ™์€ ์ ์‹ฌ ๋จน์€ ๊ฒƒ์„ ํ™•์ธํ•˜๋Š” ์งˆ๋ฌธ์ด๋ผ๋„ Open Questions๋Š” ์ง€์†์ ์œผ๋กœ ๋Œ€ํ™”๋ฅผ ์ด์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์ปจ์„คํŒ… ์ธํ„ฐ๋ทฐ์—์„œ๋Š” ์ด ๋‘ ๊ฐ€์ง€<NAME>์„ ์ ์ ˆํ•˜๊ฒŒ ์„ž์–ด์„œ ํ™œ์šฉํ•œ๋‹ค. ์ธํ„ฐ๋ทฐ๋Š” ์„œ๋กœ ๊ทธ๋ƒฅ ์ด์•ผ๊ธฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์ •ํ•ด์ง„ ์‹œ๊ฐ„์— ์›ํ•˜๋Š” ํ•ต์‹ฌ ์ •๋ณด๋ฅผ ์–ป๊ณ  ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ ํŒ€์˜ ์šฐ๊ตฐ๊ณผ ์ ๊ตฐ๋„ ํŒŒ์•…ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ธํ„ฐ๋ทฐ ํŒ€์€ ๋ถ€๋“œ๋Ÿฝ๊ณ  ๋งค๋„๋Ÿฌ์šด ์ง„ํ–‰์„ ํ†ตํ•ด ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ณ ์ž ์ธํ„ฐ๋ทฐ ์ „๋žต ๊ด€์ ์—์„œ ์ƒ๋‹นํ•œ ๊ณ ๋ฏผ์„ ํ•ด์•ผ ํ•œ๋‹ค. ์ธํ„ฐ๋ทฐ ๊ณผ์ •์—์„œ ์‚ฌ์•ˆ์— ๋Œ€ํ•œ ์˜๊ฒฌ์„ ํ‘œ๋ช…ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ Open questions๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ํ•˜๋˜ ๊ณ ์‹ฌํ•˜๊ณ  ๋˜ ๊ณ ์‹ฌํ•˜๋ฉฐ Table II-9์™€ ๊ฐ™์€ ํ˜•ํƒœ์˜ ์งˆ๋ฌธ์€ ์ง€์–‘ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋Šช์— ๋น ์ง„๋‹ค. Table II-9. ์ง€์–‘ํ•ด์•ผ ํ•  ์ธํ„ฐ๋ทฐ ์งˆ๋ฌธ์˜ ํ˜•ํƒœ ๋˜ํ•œ, ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž๋กœ๋ถ€ํ„ฐ ์‘๋‹ต์„ ๋“ค์„ ๋•Œ์—๋Š” ์œ ๋Œ€๊ฐ์„ ํ˜•์„ฑํ•˜๊ณ  ๊ณต๊ฐ์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์—ฌ๊ธฐ์—๋Š” ๋น„์–ธ์–ด์  ์‹ ํ˜ธ๋„ ํฌํ•จ๋˜๋Š”๋ฐ ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž์™€ ๊ณต๊ฐํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ช‡ ๊ฐ€์ง€ ์˜์‹์ ์ธ ํƒœ๋„๋ฅผ ์†Œ๊ฐœํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์„ฑ์˜๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์ƒ๋Œ€๊ฐ€ ์ข‹์•„ํ•˜๋„๋ก ๋…ธ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ƒ๋Œ€์˜ ๋ˆˆ์„ ๋ณด๊ณ  ์ด์•ผ๊ธฐํ•ด์•ผ ํ•œ๋‹ค. Eye-Contact์€ ๊ณต๊ฐ์  ๊ฒฝ์ฒญ์„ ์˜๋ฏธํ•œ๋‹ค. ๊ฑฐ๋ฆฌ์— ์œ ์˜ํ•œ๋‹ค. ๋„ˆ๋ฌด ๊ฐ€๊นŒ์ด ๋˜๋Š” ๋„ˆ๋ฌด ๋ฉ€๋ฆฌ๋Š” ์ข‹์ง€ ์•Š๋‹ค. ์งˆ๋ฌธ์„ ํ•œ ํ›„, ํŒ”์งฑ ๋ผ๊ณ  ์˜์ž๋ฅผ ๋’ค๋กœ ์ –ํžˆ๋Š” ํ–‰์œ„๋Š” ์ตœ์•…์ด๋‹ค. ์ž์„ธ๋Š” ๋ฐ”๋ฅด๊ฒŒ, ๋ฐœ์„ ๊ผฌ์ง€ ์•Š๋Š”๋‹ค ์•„์šธ๋Ÿฌ ๋‹ค์Œ ์‚ฌํ•ญ๋“ค์€ ์ธํ„ฐ๋ทฐ ์ง„ํ–‰์˜ ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ์œ ๋…ํ•ด์•ผ ํ•œ๋‹ค. ์‹œ๊ฐ„ ๋ฐฐ๋ถ„์„ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ์ƒ๊ฐํ–ˆ๋˜ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์„ ์ „๋ถ€ ๋“ค์„ ํ•„์š”๋Š” ์—†๋‹ค. ์งˆ๋ฌธ์˜ ์šฐ์„ ์ˆœ์œ„(priority)๊ฐ€ ์žˆ๋“ฏ์ด ๋Œ€๋‹ต์˜ ์šฐ์„ ์ˆœ์œ„๋„ ์žˆ์Œ์„ ์œ ์˜ํ•˜๋ผ ์ธํ„ฐ๋ทฐ ๋„์ค‘, ์ƒˆ๋กœ์šด ๋ฐœ๊ฒฌ์ด ์žˆ์œผ๋ฉด ๊ทธ๊ฒƒ์„ ํŒŒ๊ณ ๋“ค์–ด๋„ ์ข‹๋‹ค. ๋ฉ”๋ชจ๊ฐ€ ์ค‘์š”ํ•˜์ง€๋งŒ ๋ชจ๋‘ ์ ์„ ํ•„์š”๋Š” ์—†๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ƒ๋Œ€๊ฐ€ ๋งํ•œ ๊ฒƒ, ๊ทธ ์ˆœ๊ฐ„ ๋‚˜์˜ ํŒ๋‹จ, ์ˆœ๊ฐ„์˜ ์ƒ๊ฐ ๋“ฑ์€ ๊ตฌ๋ถ„ํ•ด์„œ ์ ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ƒ๋Œ€์—๊ฒŒ ์ƒ๊ฐํ•  ์‹œ๊ฐ„์„ ์ฃผ๋ผ ๊ท€๋ฟ ์•„๋‹ˆ๋ผ ๋ˆˆ๊ณผ ๋งˆ์Œ์„ ์—ด์ž. ์ƒ๋Œ€์˜ ๊ฐ์ •์„ ์ค‘์‹œํ•ด์•ผ ํ•œ๋‹ค ์ธํ„ฐ๋ทฐ ์ƒ๋Œ€์— ๋”ฐ๋ผ ๊ฐ™์€ ์ฃผ์ œ์— ์˜๊ฒฌ์ด ์ƒ์ดํ•  ๊ฒฝ์šฐ ์ค‘์‹œํ•ด์•ผ ํ•œ๋‹ค ๊ทธ๋ฆฌ๊ณ  ์ธํ„ฐ๋ทฐ ๋‚ด์šฉ์˜ ํšจ๊ณผ์ ์ธ ์ˆ˜์ง‘ ๋ฐ ์ •๋ฆฌ๋Š” ์ „์ฒด ์ปจ์„คํŒ… ์ž‘์—…์˜ ํšจ์œจ์„ฑ๊ณผ ์ง๊ฒฐ๋œ๋‹ค. ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์€ ๋…ธํŠธ ํ•„๊ธฐ(Note Taking)๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ธํ„ฐ๋ทฐ ๋…ธํŠธ๋Š” ๋ฐฐ๊ฒฝ ์„ค๋ช…, ์ฃผ์š” ๊ฒฐ๊ณผ ์ •๋ฆฌ, ๋‹ค์Œ ๋‹จ๊ณ„ ์ •์˜์˜ ์ˆœ์œผ๋กœ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€๋ฐ ์ฒซ ๋ฒˆ์งธ, ๋ฐฐ๊ฒฝ ์„ค๋ช…์€ ์ธํ„ฐ๋ทฐ์˜ ๋ชฉ์ , ์ฐธ์„์ž์™€ ์žฅ์†Œ, ์‹œ๊ฐ„, ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž, ์ธํ„ฐ๋ทฐ ๋ถ„์œ„๊ธฐ ๋“ฑ์„ ๊ฐ„๋žตํ•˜๊ฒŒ ์š”์•ฝํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ์ฃผ์š” ๊ฒฐ๊ณผ ์ •๋ฆฌ๋Š” Action์„ ์ค‘์‹ฌ์œผ๋กœ ์ •๋ฆฌํ•˜๋˜ ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž์˜ ๊ณต์‹ ๋ฐœ์–ธ ๋ฐ ๊ฒฐ๋ก , ๊ฒฐ๋ก ์˜ ์ •๋ฆฌ, ๊ฒฐ๋ก ์˜ ๊ทผ๊ฑฐ๋ฅผ ์ •๋ฆฌํ•˜๋ฉฐ ๋งˆ์ง€๋ง‰ ๋‹ค์Œ ๋‹จ๊ณ„ ์ •๋ฆฌ๋Š” ์ธํ„ฐ๋ทฐ ์ดํ›„ ๊ฒฐ๊ณผ ์š”์•ฝ ๋ฐ ํŒ€์› ๊ณต์œ , ๊ณ ๊ฐ ์›Œํฌ์ˆ ๋“ฑ ์ถ”ํ›„ ๊ณ„ํš ๋“ฑ์„ ์ •์˜ํ•œ๋‹ค. A4 ๋…ธํŠธ๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด ์šฉ์ง€์˜ ์ ˆ๋ฐ˜์„ ์ ‘์–ด์„œ ์™ผ์ชฝ ๋ž€์—๋Š” ์ธํ„ฐ๋ทฐ ์งˆ์˜ ์‚ฌํ•ญ์„, ์˜ค๋ฅธ์ชฝ ๋ž€์—๋Š” ๊ทธ๋•Œ๊ทธ๋•Œ ๋Š๋‚Œ๊ณผ ํŒ๋‹จ, ์ค‘์š”ํ•œ ๊ฒƒ๋“ค์„ ์ ๋Š”๋‹ค. ์–ด์ฐจํ”ผ ์›Œ๋“œ์™€ ๊ฐ™์€ ๋ฌธ์„œ ์•ฑ์œผ๋กœ ์ •๋ฆฌํ•  ๊ฒƒ์ด๋‹ˆ ๋…ธํŠธ๋ถ ์ปดํ“จํ„ฐ๋ฅผ ์ด์šฉํ•˜์ž ํ•˜์—ฌ ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž ์•ž์—์„œ ๋…ธํŠธ๋ถ์„ ํŽด๋Š” ๊ฒƒ์€ ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž๋ฅผ ๊ธด์žฅํ•˜๊ฒŒ ๋งŒ๋“ค ํ™•๋ฅ ์ด ๋†’๋‹ค. ๋…น์Œ๊ธฐ๋Š” ๋”์šฑ๋” ๊ทธ๋ ‡๋‹ค. ๋”ฐ๋ผ์„œ ๋…ธํŠธ๋ถ์€ ์ง€์–‘ํ•˜๊ณ  ๋…น์Œ๊ธฐ๋Š” ํ•„์š”ํ•˜๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ์–‘ํ•ด๋ฅผ ๊ตฌํ•˜๊ณ  ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž๊ฐ€ ์ธ์ง€ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์ž. ๋˜ํ•œ, ์ธํ„ฐ๋ทฐ์— ์‚ฌ์šฉํ•˜๋Š” ํŽœ๋„ ์ƒ‰๊น” ์žˆ๋Š” ํŽœ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€๋ฐ ์˜ˆ๋ฅผ ๋“ค๋ฉด ๊ฒ€์€์ƒ‰์ด๋‚˜ ํŒŒ๋ž€์ƒ‰์€ ์ธํ„ฐ๋ทฐ ์ˆ˜ํ–‰์ž ์ž…์žฅ์—์„œ, ๋นจ๊ฐ„์ƒ‰์€ ์ด์Šˆ๋‚˜ ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๋‚ด์šฉ, ๋…น์ƒ‰์€ ๋ถˆํ™•์‹คํ•œ ๋‚ด์šฉ ๋“ฑ์„ ํ‘œ๊ธฐํ•˜์—ฌ ์ถ”๊ฐ€ ์กฐ์‚ฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ ๋“ฑ์„ ํ‘œ๊ธฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์†๋ชฉ์ด ์ข€ ์•„ํ”„๊ฒ ์ง€๋งŒ ๊ฐ€๊ธ‰์  ๋‹ค์–‘ํ•œ ์ƒ‰์˜ ํŽœ์„ ์ด์šฉํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ์„ ์ž˜ ์ƒ๊ฐํ•ด์„œ ์ธํ„ฐ๋ทฐ๋ฅผ ์ •๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž์˜ ๋ฐœ์–ธ์€ ์™„์ „ํ•œ ์ธ์šฉ๋ฌธ์œผ๋กœ ๊ธฐ๋กํ•œ๋‹ค ํ•„์š”ํ•  ๊ฒฝ์šฐ, ๋ฉ”๋ชจํ•  ์‹œ๊ฐ„์„ ์š”์ฒญํ•œ๋‹ค ์ธํ„ฐ๋ทฐ ๊ฐ€์ด๋“œ๋ฅผ ํ™•๋Œ€ํ•˜์—ฌ ๊ทธ ์œ„์— ๊ธฐ๋กํ•˜๋Š” ๊ฒƒ๋„ ์ข‹๋‹ค ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž์˜ ์ƒ๊ฐ์„ ํ‘œ๋‚˜ ๊ทธ๋ž˜ํ”„๋กœ ํ˜•์ƒํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ทธ๋•Œ๊ทธ๋•Œ ์ •๋ฆฌํ•˜์ž. ์ˆ˜์ง‘๋œ ์ •๋ณด๋Š” ๋‹ค์‹œ ํ™•์ธํ•˜๊ณ  ์‘๋‹ต์ž์˜ ๊ฒฌํ•ด์— ๊ด€์‹ฌ์„ ํ‘œ๋ช…ํ•œ๋‹ค ์ž˜ ๋ณด์ด๋Š” ๊ณณ์— ๋‹ค์Œ ๋‹จ๊ณ„(Next Step)๋ฅผ ์š”์•ฝํ•˜์—ฌ ์ธํ„ฐ๋ทฐ๋ฅผ ๋งˆ๋ฌด๋ฆฌํ•  ๋•Œ ๋‹ค์Œ ํ–‰๋™์„ ์‰ฝ๊ฒŒ ์š”์•ฝํ•ด ์ค„ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค ์ธํ„ฐ๋ทฐ๊ฐ€ ๋๋‚˜๋ฉด ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž์—๊ฒŒ ๊ฐ์‚ฌ ์ธ์‚ฌ๋ฅผ ํ•˜๊ณ  ๋Œ์•„๊ฐ„๋‹ค. ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž๊ฐ€ ์‚ฌ๋ฌด์‹ค์„ ๋ฐฉ๋ฌธํ•œ ๊ฒƒ์ด๋ผ๋ฉด ์ž˜ ๋„์ฐฉํ–ˆ๋Š”์ง€ ์ „ํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ปจ์„คํ„ดํŠธ์™€ ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž์˜ ๊ธ์ •์ ์ธ ๊ด€๊ณ„(Positive relationship)๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•จ์ด๋‹ค[3]. Figure II-36. ์ธํ„ฐ๋ทฐ ๊ฒฐ๊ณผ์˜ ๊ณต์œ  ์ธํ„ฐ๋ทฐ์˜ ๋งˆ์ง€๋ง‰์€ ๊ฒฐ๊ณผ ๊ณต์œ ์ด๋‹ค. ์‹œ์‚ฌ์ ์„ ์ •๋ฆฌํ•˜์—ฌ ํŒ€์›๋“ค๊ณผ<NAME>๊ณ  ํ•„์š”์‹œ, ๊ณ ๊ฐ์—๊ฒŒ ๋ณด๊ณ ํ•˜๊ฒŒ ๋œ๋‹ค. Figure II-36์€ ์ธํ„ฐ๋ทฐ ๊ฒฐ๊ณผ๋ฅผ<NAME>๋Š” ์ž‘์—…์„ 3๋‹จ๊ณ„๋กœ ์ •๋ฆฌํ•˜์—ฌ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ, ์ธํ„ฐ๋ทฐ ๋…ธํŠธ๋ฅผ ๋‹ค์‹œ ์ •๋ฆฌํ•˜๊ณ  ๋ˆ„๊ตฌ๋‚˜ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ์‚ฌ์‹ค๊ณผ ์˜๊ฒฌ์„ ํฌํ•จํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ํŒ€ ๋ธŒ๋ฆฌํ•‘(Team Briefing)์„ ํ†ตํ•ด ์ธํ„ฐ๋ทฐ ๊ฒฐ๊ณผ๋ฅผ ์ข…ํ•ฉ์ ์œผ๋กœ ๊ฒ€ํ† ํ•˜๊ณ  ์„ธ๋ถ€์ ์œผ๋กœ ์ž‘์—…ํ•  ๊ฒƒ๋“ค์„ ๊ฒฐ์ •ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์‹œ์‚ฌ์ (Key Findings or Implications)์„ ๊ฐ๊ด€์ ์ธ ๊ด€์ ์—์„œ ์ •๋ฆฌํ•˜๊ณ  ์ •๋Ÿ‰ํ™”ํ•˜๊ณ ์ž ๋…ธ๋ ฅํ•œ๋‹ค[4]. Break #8. ๊ธฐ์—… ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์˜ ๊ธฐ๋ณธ, ํšŒ์˜(Meeting) ์ง์žฅ ์ƒํ™œ์—์„œ ํšŒ์˜(ๆœƒ่ญฐ. Meeting)๋Š” ๋งค์šฐ ๋งŽ์ด ๋ฐœ์ƒํ•œ๋‹ค. 'ํšŒ์˜(ๆœƒ่ญฐ)๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ํ•ด์„œ ํšŒ์˜(ๆ‡ท็–‘)๊ฐ€ ์ƒ๊ธด๋‹ค'๋ผ๋Š” ์ž์กฐ(่‡ชๅ˜ฒ) ์„ž์ธ ๋†๋‹ด๋„ ์žˆ์„ ์ •๋„์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ตœ๊ทผ ๋งŽ์€ ๊ธฐ์—…๋“ค์ด ์—…๋ฌด ์ƒ์‚ฐ์„ฑ ํ–ฅ์ƒ ์ฐจ์›์—์„œ ํšŒ์˜์˜ ๋ฐฉ๋ฒ•์ด๋‚˜ ์„ฑ๊ฒฉ์— ๋Œ€ํ•ด ๊ณ ๋ฏผ์„ ๋งŽ์ด ํ•œ๋‹ค. ์ฆ‰, 'ํšŒ์˜๋Š” ์˜์‚ฌ ๊ฒฐ์ •๋งŒ์„ ์œ„ํ•ด์„œ ํ•˜์ž'๋ผ๊ณ  ํ•˜๋Š” ๊ณณ์ด ๋งŽ๋‹ค. ์ž๋ฃŒ ๊ฒ€ํ† ๋‚˜ ๊ณต์œ ๋ฅผ ์œ„ํ•œ ํšŒ์˜๋Š” ๊ฐ€๊ธ‰์  ์ง€์–‘ํ•˜๊ณ  ๋Œ€์‹  ์ด๋ฉ”์ผ์„ ํ™œ์šฉํ•˜๋„๋ก ๊ถŒ์žฅํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๊ทธ๋Ÿฐ ๋ชฉ์ ์˜ ํšŒ์˜๋Š” ์ƒ์‚ฐ์„ฑ ์ฐจ์›์—์„œ ์ข‹์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•ญ์ƒ ์‹œ๊ฐ„์— ์ซ“๊ธฐ๋Š” ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ์—์„œ ํšŒ์˜๋ฅผ ์‚ฌ์ „์— ์ž˜ ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. Figure II-37. ํšŒ์˜์˜ ์„ค๊ณ„ ๊ณผ์ • ํšŒ์˜์˜ ๋ชฉํ‘œ๋ฅผ ๋ช…ํ™•ํžˆ ํ•ด์•ผ ํ•˜๋ฉฐ ๋ˆ„๊ฐ€ ์ฐธ์„ํ•  ๊ฒƒ์ธ์ง€, ์‹œ๊ฐ„์ด๋‚˜ ์žฅ์†Œ๋Š” ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€, ํšŒ์˜์—์„œ ๋‹ค๋ฃจ์–ด์•ผ ํ•  ์ด์Šˆ๋Š” ๋ฌด์—‡์ธ์ง€, ์ด์Šˆ ํ•ด๊ฒฐ์„ ์œ„ํ•ด ๋ช‡ ๋ฒˆ์˜ ํšŒ์˜๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ํ•˜๋Š”์ง€, ์ฐธ์„์ž ๊ฐ„์˜ ์ดํ•ด๊ด€๊ณ„๋Š” ์–ด๋–ป๊ฒŒ ์‚ดํŽด์•ผ ํ•˜๋Š”์ง€ ๋“ฑ์„ ๊ณ ๋ คํ•˜์—ฌ ํšŒ์˜ ์–ด์  ๋‹ค์˜ ์ดˆ์•ˆ(Draft)์„ ๊ฐœ๋ฐœํ•œ๋‹ค. ๊ณต์œ ๋ฅผ ๋ชฉ์ ์œผ๋กœ ํ•˜๋Š” ํšŒ์˜๋Š” ์ƒ์‚ฐ์„ฑ ์ฐจ์›์—์„œ ์ง€์–‘ํ•˜๋Š” ํŽธ์ด๋‹ค. ์ปจ์„คํŒ…์—์„œ ์‹œ๊ฐ„์€ ๋”์šฑ ์ค‘์š”ํ•˜๋ฏ€๋กœ ํšŒ์˜๋ฅผ ์ž˜ ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ํšŒ์˜๋ฅผ ์œ„ํ•œ Draft๊ฐ€ ์™„์„ฑ๋˜๋ฉด ํŒ€ ๋ธŒ๋ฆฌํ•‘(Team Briefing)์„ ํ†ตํ•ด ์ƒํ˜ธ ์˜๊ฒฌ์„ ๊ตํ™˜ํ•˜์—ฌ ๋ณด์™„ํ•œ ํ›„ ํšŒ์˜๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. Figure II-37์€ ํšŒ์˜์˜ ์„ค๊ณ„ ๊ณผ์ •์„ ๋ณด์—ฌ์ค€๋‹ค. ์ด๋ ‡๊ฒŒ ์„ค๊ณ„๋œ ํšŒ์˜๋Š” ๋ช…ํ™•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•˜๊ธฐ ์œ„ํ•ด FISH ๊ธฐ๋ฒ• ๊ฐ™์€ ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค. FISH๋Š” ํšจ์œจ์ ์ธ ํšŒ์˜ ์ง„ํ–‰์„ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ๊ธฐ๋ฒ•์œผ๋กœ ๊ตฌ์กฐํ™”(Framing), ์กฐ์‚ฌ(Investigate), ํ˜•์ƒํ™”(Shaping), ์ •๋ฆฌ(Harvesting)์˜ ์˜๋ฌธ ์•ฝ์ž๋ฅผ ๋ชจ์•„ ๋งŒ๋“  ์šฉ์–ด์ด๋‹ค. ์ด์— ๋Œ€ํ•ด ์ข€ ๋” ์ƒ์„ธํžˆ ์•Œ์•„๋ณด์ž. ํšจ๊ณผ์ ์ด๊ณ  ์„ฑ๊ณผ์ง€ํ–ฅ์ ์ธ ํšŒ์˜๋ฅผ ์œ„ํ•œ FISH ๊ธฐ๋ฒ•์˜ ์ฒซ ๋ฒˆ์งธ, ๊ตฌ์กฐํ™” (Framing)๋Š” ํšŒ์˜ ๊ณ„ํš์˜ ๋ถ€๋ถ„ ๋˜๋Š” ์—ฐ์žฅ์„ ์—์„œ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์•„์ด์Šค๋ธŒ๋ ˆ์ดํ‚น[5]์œผ๋กœ ๊ทธ๋ƒฅ ์ง€๋‚˜์น˜๊ธฐ ์‰ฌ์šด ํšŒ์˜ ์ „๋ฐ˜๋ถ€๋ฅผ ๋ชฐ์ž…ํ•˜๊ฒŒ ํ•ด์ค€๋‹ค. ํšŒ์˜์˜ ํ”„๋ ˆ์ž„์€ Table II-10์˜ ํ•ญ๋ชฉ๋“ค์„ ๊ทœ์ •ํ•˜๋Š” ๊ฒƒ์—์„œ ์‹œ์ž‘ํ•œ๋‹ค. Table II-10. ํšŒ์˜ ํ”„๋ ˆ์ž„์˜ ๊ตฌ์„ฑ ํ•ญ๋ชฉ ํšŒ์˜ ํ”„๋ ˆ์ž„์„ ๊ตฌ์„ฑํ•˜๋ฉด ํšŒ์˜์˜ ๋ชฐ์ž…๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด ๋‹ค์Œ 4๊ฐ€์ง€๋ฅผ ์‹œ๋„ํ•œ๋‹ค. Melting: ํšŒ์˜ ์ฐธ์„์ž๋“ค์ด โ€˜์šฐ๋ฆฌโ€™๋ผ๋Š” ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋„๋ก ์œ ๋„ํ•œ๋‹ค. Expectation check: ํšŒ์˜ ์ฐธ์„์ž๋“ค์ด ํšŒ์˜์— ๊ธฐ๋Œ€ํ•˜๋Š” ๋ฐ”๋ฅผ ๋ฐœํ‘œํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ณ  ์„œ๋กœ ๊ณต์œ ํ•œ๋‹ค. Ground Rule ๊ฒฐ์ •: ํšŒ์˜ ์ฐธ์„์ž๋“ค์„ ์œ„ํ•œ ๊ธฐ๋ณธ ๊ทœ์น™์„ ์ •ํ•˜๊ณ  ๋ชจ๋‘ ํ•ฉ์˜ํ•œ๋‹ค. ์˜ˆ) ํšŒ์˜ ์‹œ๊ฐ„ ๋‚ด ์Šค๋งˆํŠธํฐ ๊บผ๋‘๊ธฐ I Talk: ํšŒ์˜๋ฅผ<NAME>๋Š” ์ž…์žฅ์—์„œ ์ด ํšŒ์˜๊ฐ€ ์™œ ํ•„์š”ํ•œ์ง€ ์ง€์†์ ์œผ๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค. FISH ๊ธฐ๋ฒ•์˜ ๋‘ ๋ฒˆ์งธ, ์กฐ์‚ฌ (Investigate)๋Š” ๋น„์ •๊ธฐ์ ์ธ ํšŒ์˜์—์„œ ๋งŽ์€ ์˜๊ฒฌ๊ณผ ์•„์ด๋””์–ด๋ฅผ ๋‚ด๋Š” ๋ฐฉ๋ฒ•๋“ค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ ํšŒ์˜ ์ง„ํ–‰์ž[6]์˜ ์šด์˜ ์‹ค๋ ฅ(Facilitating skills)์ด ์ค‘์š”ํ•˜๋‹ค. Table II-11์€ ์กฐ์‚ฌ ๋‹จ๊ณ„์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์•„์ด๋””์–ด ์ฐฝ์ถœ ๊ธฐ๋ฒ•๋“ค์„ ์†Œ๊ฐœํ•˜๊ณ  ์žˆ๋‹ค. Table II-11. ๋‹ค์–‘ํ•œ ์•„์ด๋””์–ด ๋„์ถœ ๊ธฐ๋ฒ• FISH ๊ธฐ๋ฒ•์˜ ์„ธ ๋ฒˆ์งธ, ํ˜•์ƒํ™” (Shaping)๋Š” ์ˆ˜์ง‘๋œ ๋‹ค์–‘ํ•œ ์•„์ด๋””์–ด ๋ฐ ์˜๊ฒฌ์„ ์ •๋Ÿ‰์  ๊ธฐ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ๊ฐ€์‹œํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์–ด๋–ค ๊ฒฐ๋ก ์„ ์ •๋Ÿ‰ํ™”ํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ค‘์š”ํ•œ๋ฐ ๋ณด๋‹ค ๊ฐ๊ด€์ ์ธ ํŒ๋‹จ๊ณผ ๊ฒฐ๋ก ์„ ์œ ๋„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ˆ˜์ง‘๋œ ์•„์ด๋””์–ด๋“ค์„ ํ˜•์ƒํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๋Œ€ํ‘œ์ ์ธ ๊ธฐ๋ฒ•์œผ๋กœ ๋งคํŠธ๋ฆญ์Šค(Matrix), ์Šค์ฝ”์–ด์นด๋“œ(Scorecard), ๋ฒ„๋ธ” ์†ŒํŠธ(Bubble sort)๊ฐ€ ์žˆ๋‹ค. ๋งคํŠธ๋ฆญ์Šค(Matrix)๋Š” ์ถ•์ด ๋˜๋Š” ๊ธฐ์ค€์„ ๋จผ์ € ์„ ์ •ํ•œ ํ›„, ์•„์ด๋””์–ด๋“ค์„ ํ•ด๋‹น ์ถ•์— ๋งž๊ฒŒ ํ‰๊ฐ€ํ•œ๋‹ค. ์Šค์ฝ”์–ด์นด๋“œ๋Š” ๊ฐ๊ฐ์˜ ๊ธฐ์ค€์— ์ ์ˆ˜๋ฅผ ๋‘๊ณ  ์ด๋ฅผ ํ•ฉ๊ณ„ํ•œ ์ข…ํ•ฉ ์ ์ˆ˜๋ฅผ ํ‰๊ฐ€ํ•˜์—ฌ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. ํ•œ ๊ฐ€์ง€ ์•„์ด๋””์–ด์”ฉ ์ง์ ‘ ๋น„๊ตํ•˜์—ฌ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. FISH์˜ ๋งˆ์ง€๋ง‰ ์ˆœ์„œ์ธ ์ •๋ฆฌ (Harvesting)๋Š” ํ˜•์ƒํ™”(Shaping) ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ์ค€์— ๋”ฐ๋ผ ์ ์ ˆํžˆ ๋ถ„๋ฅ˜ํ•˜์—ฌ ๊ฐ€์‹œ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์ด๊ฒƒ์„ ํŒŒ์›Œํฌ์ธํŠธ ์Šฌ๋ผ์ด๋“œ๋กœ ํ‘œํ˜„ํ•˜๋ฉด Figure II-38๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋ณด๊ณ ๋  ๊ฒƒ์ด๋‹ค. Figure II-38. ํšŒ์˜ ๋‚ด์šฉ์˜ ์ •๋ฆฌ ๋ชจ๋“  ํšŒ์˜์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ๊ทธ๋ฆฌ๊ณ  ๊ฒฐ๋ก ์ ์œผ๋กœ ๋„์ถœ๋˜์–ด์•ผ ํ•  ๊ฒƒ์€ ํšŒ์˜ ๊ฒฐ๊ณผ ๋ˆ„๊ฐ€ ๋ฌด์—‡์„ ํ•  ๊ฒƒ์ธ์ง€ ๊ฐ™์€ Action Item ๋“ค์ด๋‹ค. ๊ทธ๊ฒƒ์€ ์ด์–ด์ง€๋Š” ๋‹ค์Œ ํšŒ์˜์— ๋Œ€ํ•œ ์•ฝ์†์ผ ์ˆ˜๋„ ์žˆ๊ณ  ๋‹ค๋ฅธ ์ž‘์—…(Task)์„ ์ง€์‹œํ•˜๋Š” ๊ฒƒ์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค. ํšŒ์˜ ๊ฒฐ๊ณผ๋กœ ๋„์ถœ๋œ Action Item๋“ค์€ ํšŒ์˜ ์ฐธ์„์ž๋‚˜ ํšŒ์˜ ์ฐธ์„์ž๊ฐ€ ์†ํ•œ ๋ถ€์„œ์˜ ์ธ์›์ด ๋งก์•„์„œ ๋‚ฉ๊ธฐ ๋‚ด์— ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ค์–ด๋‚ด๊ณ  ๊ทธ๊ฒƒ์ด ๋‹ค์Œ ํšŒ์˜์—์„œ ๋‹ค์‹œ ์–ด์  ๋‹ค๋กœ ๋‹ค๋ฃจ์–ด์ง€๊ฒŒ ๋œ๋‹ค. Figure II-39. ๋ฌธ์ œ ํ•ด๊ฒฐ ํ”„๋กœ์„ธ์Šค์™€ ํšŒ์˜ ํ”„๋ ˆ์ž„์›Œํฌ Figure II-39๋Š” ์ง€๊ธˆ๊นŒ์ง€ ๋‹ค๋ฃฌ ํšŒ์˜ ๊ธฐ๋ฒ•์ธ FISH์™€ ๋งฅํ‚จ์ง€์˜ ๋ฌธ์ œ ํ•ด๊ฒฐ 7๋‹จ๊ณ„ ๊ธฐ๋ฒ•์„ ๊ฐ™์ด ๋งคํ•‘ํ•˜์—ฌ ๋ณธ ๊ฒƒ์ด๋‹ค. ํšŒ์˜์˜ ๋ชฉ์  ๋ฐ ์ƒ์‚ฐ์„ฑ์„ ๊ณ ๋ คํ•œ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ ์ ์šฉํ•˜์—ฌ ์˜๋ฏธ ์žˆ๋Š” ๊ฒฐ๊ณผ๋ฌผ์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ์˜ ํšŒ์˜์—์„œ๋Š” ํ•„์ˆ˜์ ์œผ๋กœ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์•„์šธ๋Ÿฌ ๊ณ„ํš(Initiate), ์‹คํ–‰(Execute), ํ‰๊ฐ€(Feedback)๋กœ ์ด์–ด์ง€๋Š” ํšŒ์˜๋Š” ๋น„์ •๊ธฐ์ ์ด๋ฉฐ ๋ฌธ์ œ ํ•ด๊ฒฐ ์„ฑ๊ฒฉ์— ๊ฐ€๊นŒ์šด ํšŒ์˜์ผ์ˆ˜๋ก ๊ณ„ํš ๋ถ€๋ถ„์„ ๊นŠ๊ฒŒ ๋‹ค๋ฃจ์–ด์•ผ ํ•œ๋‹ค. ์ด๋Š” ์ž˜ ์„ค๊ณ„๋œ ๊ณ„ํš์ด ์‹œํ–‰์ฐฉ์˜ค๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. [1] Voice of Customer [2] ์ธํ„ฐ๋ทฐ ๊ธฐ๋ก ์ธก๋ฉด์—์„œ ๋…ธํŠธ, ๋น„๋””์˜ค, ์˜ค๋””์˜ค, ์„ค๋ฌธ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋ก์˜ ํ˜•ํƒœ์— ๋Œ€ํ•œ ๊ณ ๋ ค๋„ ํ•„์š”ํ•˜๋‹ค. [3] ์ธํ„ฐ๋ทฐ ๋Œ€์ƒ์ž์—๊ฒŒ ๊ธ์ •์ ์ธ ์ด๋ฏธ์ง€๋ฅผ ์‹ฌ๋Š” ์ผ์€ ๋งค์šฐ ์ค‘์š”ํ•˜]์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ถ”ํ›„ ๋‹ค๋ฅธ ์ผ๋กœ ๋ฐ˜๋“œ์‹œ ์—ฐ๋ฝ์„ ์ฃผ๊ณ ๋ฐ›์„ ์ผ์ด ๋ฐœ์ƒํ•  ํ™•๋ฅ ์ด ๋†’๋‹ค [4] ์ •๋Ÿ‰ํ™”๋Š” ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ์˜ ๋ฐฉ๋ฒ•์—์„œ ๋งค์šฐ ์ง๊ด€์ ์œผ๋กœ ์‚ฌ์‹ค์„ ๋ณด์—ฌ์ฃผ๋Š” ์ค‘์š”ํ•œ ๊ธฐ๋ฒ•์ด๋‹ค. [5] Ice Breaking. ํšŒ์˜ ์‹œ ์„œ๋จน์„œ๋จนํ•œ ๊ด€๊ณ„๋ฅผ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๋‚ ์”จ, ํ™”์ ฏ๊ฑฐ๋ฆฌ ๋“ฑ ์†Œ์†Œํ•œ ์ด์•ผ๊ธฐ๋“ค์„ ๋‚˜๋ˆ„๋Š” ํ–‰์œ„ [6] ํšŒ์˜ ์ฃผ์ตœ์ž์™€ ํšŒ์˜ ์ง„ํ–‰์ž๋Š” ํšŒ์˜ ์–ด์  ๋‹ค์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ์กฐ์‚ฌ ๋‹จ๊ณ„์—์„œ๋Š” ํšŒ์˜ ์ง„ํ–‰์ž์˜ ์—ญํ• ์ด ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. 06. ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ(2/3)- ๋ฌธ์„œํ™” 6.2 ๋ฌธ์„œํ™”(Documentation) ์ปจ์„คํŒ… ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ์˜ ๋‘ ๋ฒˆ์งธ, ๋ฌธ์„œํ™”(Documentation)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ ์ž ํ•œ๋‹ค. ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ์˜ ๊ถ๊ทน์ ์ธ ์‚ฐ์ถœ๋ฌผ(Deliverables)์ด ํ”„๋กœ์ ํŠธ ๋ฌธ์„œ๋ผ๊ณ  ํ•  ๋•Œ ๋ฌธ์„œํ™”๋Š” ์‚ฌ์‹ค ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์„œํ™” ๋Šฅ๋ ฅ์€ ์ปจ์„คํ„ดํŠธ๊ฐ€ ์ง€๋…€์•ผ ํ•  ๊ถ๊ทน(็ชฎๆฅต)์˜ ํ•„์‚ด๊ธฐ ์ค‘์˜ ํ•˜๋‚˜์ด๋‹ค. ๋•Œ๋กœ๋Š” ์ˆ˜์‹ญ ์žฅ์˜ ๋ฌธ์„œ๋ฅผ 100์ž ์ด๋‚ด์˜ ๋‹จ๋ฌธ์œผ๋กœ, ๋•Œ๋กœ๋Š” ํ•œ ์ค„์˜ ๋ฌธ์žฅ์„ ์ˆ˜์‹ญ ์žฅ์˜ ๋ฌธ์„œ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์€ ๋ฌธ์„œํ™” ์ˆ˜์ค€์˜ ์ตœ๊ณ ๋ด‰์„ ๋งํ•ด์ค€๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐฐ๊ฒฝ์ง€์‹๊ณผ ๋ฉ”์‹œ์ง€๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋Š” ํ†ต์ฐฐ๋ ฅ์„ ์ง€๋…€์•ผ ํ•œ๋‹ค. ๋ฌธ์„œ๊ฐ€ ๋ช…ํ™•ํ•œ ์ฃผ์ œ๋ฅผ ๊ฐ€์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ช…ํ™•ํ•œ ๊ฒฐ๋ก  ์™€ ๋ช…ํ™•ํ•œ ๊ทผ๊ฑฐ, ๊ทธ๋ฆฌ๊ณ  ๋ช…ํ™•ํ•œ ๊ธฐ๋Œ€ ์‚ฌํ•ญ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.[1] ์ฒซ ๋ฒˆ์งธ, ๋ช…ํ™•ํ•œ ๊ฒฐ๋ก ์€ ์ฃผ์ œ์— ๋Œ€ํ•˜์—ฌ ๊ธ€ ์“ฐ๋Š” ์‚ฌ๋žŒ์ด ์˜๋„ํ•˜๋Š” ์‚ฌํ•ญ์˜ ํ•ต์‹ฌ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ‰๊ฐ€, ํŒ๋‹จ, ๋˜๋Š” ์ง€์‹œํ•˜๊ฒŒ ๋œ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ๊ฒฐ๋ก ์€ ์ฃผ์ œ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์˜ ์š”์•ฝ์œผ๋กœ ์ž์‹ ์ด ๋งํ•˜๊ณ  ์‹ถ์€ ์‚ฌํ•ญ์˜ ์š”์•ฝ์ด ์•„๋‹ˆ๋‹ค. ๋•Œ์— ๋”ฐ๋ผ์„œ ์ƒํ™ฉ์˜ ์ถ”์ด๋ฅผ ์ง€์ผœ๋ณด๋ฉด ๋ถ€๋Œ€์กฐ๊ฑด์„ ๋‹ฌ์•„์„œ๋Š” ์•ˆ ๋œ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ๋ฅผ ๋ณด์ž. "A ์‚ฌ์—…์€ ๋””์ง€ํ„ธ Signage ๋ถ„์•ผ์—์„œ ํš๊ธฐ์ ์œผ๋กœ ์„ฑ์žฅ์ด ์˜ˆ์ƒ๋˜๋Š” ์•„์ดํ…œ์œผ๋กœ ์ž์‚ฌ๋Š” ์‚ฌ์—…์˜ ์ˆ˜์ต์„ฑ๊ณผ ๊ฒฝ์Ÿ ๋™ํ–ฅ์„ ์ถฉ๋ถ„ํžˆ ๋ถ„์„ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค โ€ฆ" ์œ„์˜ ๋‚ด์šฉ์€ ๊ฒฐ๋ก ์ด๋ผ๊ณ  ๋งํ•˜๊ธฐ๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค. ์œ„์˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด์™„ํ•œ๋‹ค๋ฉด ์„ฑ์žฅ์˜ ์˜ˆ์ƒ ๊ทœ๋ชจ, ๋ถ„์„์„ ์œ„ํ•œ ํ–ฅํ›„ ํ™œ๋™ ๋“ฑ์ด ๊ฐ™์ด ํ‘œ๊ธฐ๋˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋‘ ๋ฒˆ์งธ, ๋ช…ํ™•ํ•œ ๊ทผ๊ฑฐ๋Š” ๊ฒฐ๋ก ์— ์ด๋ฅด๊ฒŒ ๋œ ํ•„์—ฐ์„ฑ์— ๋Œ€ํ•ด ์ƒ๋Œ€๋ฅผ ๋‚ฉ๋“์‹œํ‚ค๋Š” ๊ฒƒ์œผ๋กœ ์‚ฌ์‹ค(Fact) ๋˜๋Š” ์‚ฌ์‹ค์— ๊ทผ๊ฑฐํ•œ ํŒ๋‹จ(Decision)์„ ์ œ์‹œํ•ด์•ผ ํ•œ๋‹ค. ๋™์ „ ๋’ค์ง‘๊ธฐ ์‹์˜ ๊ทผ๊ฑฐ๋Š” ์„ค๋“๋ ฅ์ด ์—†์œผ๋ฉฐ, ์‚ฌ์‹ค ๋˜๋Š” ์‚ฌ์‹ค์— ๊ทผ๊ฑฐํ•œ ํŒ๋‹จ๋งŒ์ด ๋ช…ํ™•ํ•œ ๊ทผ๊ฑฐ๋กœ์„œ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค. โ€œ์ž์‚ฌ๋Š” ์‹œ๋Œ€์˜ ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€ํ•˜๋Š” ํ†ต์ฐฐ๋ ฅ์ด ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‹œ๊ธ‰ํ•˜๊ฒŒ ํ†ต์ฐฐ๋ ฅ์„ ๊ฐ–์ถ˜ ๋งˆ์ผ€ํŒ… ๋ถ€์„œ์›๋“ค์„ ๋ฝ‘์•„์•ผ ํ•ฉ๋‹ˆ๋‹คโ€ฆ." ์œ„์˜ ๋‚ด์šฉ์ด ์ง€์ง€๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ค ์‚ฌ์œ ๋กœ ํ†ต์ฐฐ๋ ฅ์ด ๋ถ€์กฑํ•œ์ง€ ์‚ฌ์‹ค๊ณผ ๊ทธ์™€ ๊ด€๋ จ๋œ ํŒ๋‹จ์„ ๊ฐ™์ด ์ œ์‹œํ•ด์•ผ ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ธฐ๋Œ€ ์‚ฌํ•ญ์€ ๊ฒฐ๋ก ์— ๋Œ€ํ•ด ์ƒ๋Œ€๋ฐฉ์œผ๋กœ๋ถ€ํ„ฐ ์–ป๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ๋Œ€๋ฐฉ์ด ์–ด๋–ค ํ–‰๋™์„ ํ•  ๊ฒƒ์ธ์ง€ ๋ช…ํ™•ํžˆ ํ•ด์•ผ ํ•œ๋‹ค. โ€œ์ž์‚ฌ์˜ ์‹œ์žฅ์ ์œ ์œจ์ด ๋–จ์–ด์ง„ ์ด์œ ๋Š” ์ง€๊ธˆ๊นŒ์ง€ ๋ง์”€๋“œ๋ ธ๋˜ ๋ฐ”์™€ ๊ฐ™์ด ์‹ ์ œํ’ˆ ๊ฐœ๋ฐœ ์Šคํ”ผ๋“œ์—์„œ ๊ฒฝ์Ÿ์‚ฌ์— ๋’ค์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์ƒ์œผ๋กœ ๋ณด๊ณ ๋ฅผ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค." ์œ„์˜ ๋‚ด์šฉ์€ ๋ณด๊ณ ๋ฅผ ๋“ฃ๋Š” ๊ฒฝ์˜์ž์—๊ฒŒ ๋ฌด์—‡์„ ๊ธฐ๋Œ€ํ•˜๋Š” ๊ฑธ์ผ๊นŒ? ์Šคํ”ผ๋“œ(speed)๋ผ๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ๊ตฌ์ฒดํ™”, ๊ทธ๋ฆฌ๊ณ  ๊ฐœ์„  ๋ฐฉํ–ฅ์— ๋Œ€ํ•œ ์‚ฌํ•ญ๊นŒ์ง€ ์ „๋‹ฌํ•ด ์ฃผ์–ด์•ผ ์ •์ƒ์ ์ธ ๋…ผ์˜๊ฐ€ ์‹œ์ž‘๋  ๊ฒƒ์„ ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ ๋ฌธ์„œ๋ฅผ ๋งŒ๋“ค ๋•Œ ๋‹ค์–‘ํ•œ ๋ฉ”์‹œ์ง€๋“ค์˜ ๋ช…ํ™•ํ™”๊ฐ€ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋ฉฐ ๋ช…ํ™•ํ•œ ๊ฒฐ๋ก ๊ณผ ๊ทผ๊ฑฐ, ๊ธฐ๋Œ€ ์‚ฌํ•ญ์„ ์ž˜ ์ •๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ๋ฅผ ์ž˜ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ 4๊ฐ€์ง€ ์งˆ๋ฌธ์— ๋‹ต์„ ์ƒ๊ฐํ•ด ๋ณด๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 1) ๋ˆ„๊ตฌ๋ฅผ ์œ„ํ•ด ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•˜๋Š”๊ฐ€? ๋ˆ„๊ตฌ์—๊ฒŒ ๋ฐœํ‘œํ•˜๋Š”๊ฐ€? (Who am I writing/speaking to?) 2) ๊ตฌ์ฒด์ ์ธ ๋ชฉ์ ์ด ๋ฌด์—‡์ธ๊ฐ€? (What is my specific objective?) 3) ์–ด๋–ค ์ •๋ณด๊ฐ€ ํ•„์ˆ˜์ ์ธ๊ฐ€? ์–ด๋–ค ์ •๋ณด๊ฐ€ ์ƒ๋žต๋˜์–ด์•ผ ํ•˜๋‚˜? (What information is essential? What information should be omitted?) 4) ์•„์ด๋””์–ด๋“ค์„ ์ข…ํ•ฉํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ธ๊ฐ€? (What would be the most effective way to organize my ideas?) ๊ฐ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ(business documents)๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ๋Š” ์ผ๋ฐ˜ ๋ฌธ์„œ์™€ ๋‹ฌ๋ฆฌ ๊ณ ์œ ์˜<NAME>์ด๋‚˜ ํ‘œํ˜„๋ฒ•์ด ์žˆ์œผ๋ฉฐ ์ž‘์„ฑ ๋ฐฉ๋ฒ•๋„ ๋งŽ์ด ๋‹ฌ๋ผ์„œ ํ›ˆ๋ จ๋ฐ›์ง€ ์•Š์œผ๋ฉด ์ œ๋Œ€๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์—†๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ๋Š” ๋ช…ํ™•ํ•œ ์ฃผ์ œ๊ฐ€ ์žˆ์œผ๋ฉฐ ๊ฒฐ๋ก , ๊ทผ๊ฑฐ, ๊ธฐ๋Œ€ ์‚ฌํ•ญ ๋“ฑ ๋ช…ํ™•ํ•œ ๋ฉ”์‹œ์ง€๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋…ผ๋ฆฌ์ ์ธ ๊ตฌ์„ฑ์ด ์š”๊ตฌ๋˜๋ฉฐ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ •ํ™•ํ•œ ํ‘œํ˜„์ด ์š”๊ตฌ๋œ๋‹ค. ์ด ๋ชจ๋“  ๊ฒƒ์ด ์ƒ๋Œ€๋ฐฉ์˜ ๊ด€์ ์—์„œ ์ •๋ฆฌ๋˜์–ด์•ผ ํ•œ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ์˜ ์ƒ๋Œ€๋ฐฉ์€ ํšŒ์‚ฌ์˜ ์ง€์œ„, ์ง๊ธ‰์— ๋”ฐ๋ผ ๊ฒฐ์ •๋˜๋Š”๋ฐ ์ง€์œ„๋ณ„ ์ฃผ์š” ๊ด€์‹ฌ ํฌ์ธํŠธ๋Š” Table II-12์™€ ๊ฐ™๋‹ค. ๊ฒฝ์˜์ง„๊ณผ ์ค‘๊ฐ„๊ด€๋ฆฌ์ž, ์‹ค๋ฌด์ง„์ด ๊ด€์‹ฌ์„ ๊ฐ€์ง€๋Š” ์‚ฌํ•ญ์€ ๋ชจ๋‘ ๋‹ค๋ฅด์ง€๋งŒ ๊ฒฐ๊ตญ ๋‚ด๊ฐ€ ์ฑ…์ž„์ง€๋Š” ๊ฒƒ์€ ๋ฌด์—‡์ธ๊ฐ€๋กœ ๊ท€๊ฒฐ๋œ๋‹ค. Table II-12. ์ง€์œ„๋ณ„ ์ฃผ์š” ๊ด€์‹ฌ ํฌ์ธํŠธ ๊ทธ๋Ÿฐ ์ฐจ์›์—์„œ ์ „๋‹ฌ๋˜๋Š” ์ฃผ์ œ๊ฐ€ ๋ฌด์—‡์ธ๊ฐ€๋Š” ๋ช…ํ™•ํ•˜๊ฒŒ ๊ทœ๋ช…๋˜์–ด์•ผ ํ•œ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ๋ฅผ ๋ฐ›๊ฒŒ ๋˜๋Š” ๋‹น์‚ฌ์ž์˜ ์ž…์žฅ์—์„œ ์ƒ๋Œ€๋ฐฉ์˜ ๋ฐฉํ–ฅ, ์˜๋„๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋ฉฐ ๋‚ด๊ฐ€ ๋งํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์„ ํ™•์ •ํ•˜์—ฌ ์ฃผ์ œ๋ฅผ ๋‹จ์ผํ™”ํ•˜๊ณ , ๋‚ด๊ฐ€ ๋งํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์„ ๋ช…ํ™•ํžˆ ํ•˜์—ฌ ์ฃผ์ œ์˜ ๋ฒ”์œ„๋ฅผ ๊ฒฐ์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ฃผ์ œ๋ฅผ ๊ตฌ์ฒดํ™”์‹œํ‚ฌ ๋•Œ ์˜๋ฏธ ์žˆ๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ๊ฐ€ ์ž‘์„ฑ๋œ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ๊ฐ€ ๋…ผ๋ฆฌ์ ์ด๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ”ผ๋ผ๋ฏธ๋“œ์™€ ๊ฐ™์€ ๋…ผ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ–์ถ”๋ฉด์„œ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€๋ฅผ ์ค€์ˆ˜ํ•ด์•ผ ํ•œ๋‹ค. 1. ํ”ผ๋ผ๋ฏธ๋“œ์˜ ๊ฐ€์žฅ ์•„๋žซ๋ถ€๋ถ„์€ ๊ทผ๊ฑฐ๊ฐ€ ์‚ฌ์‹ค์ž„ 2. ๋ˆ„๋ฝ๊ณผ ์ค‘๋ณต์ด ์—†์Œ 3. ๋ฌธ์„œ์˜ ๊ตฌ์„ฑ์ด ๊ฒฐ๋ก ๊ณผ ๊ทผ๊ฑฐ์˜ ์ฒด๊ณ„๋ฅผ ๊ฐ€์ง„ ํ”ผ๋ผ๋ฏธ๋“œ ๊ตฌ์„ฑ ๋ฌธ์„œ์˜ ์ฃผ์ œ ํ•˜์— ๊ฐ๊ฐ์˜ ์„œ๋ธŒ ๋ฉ”์‹œ์ง€๊ฐ€ ์กด์žฌํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ๊ตฌ์„ฑํ•˜๋Š” ์‚ฌ์‹ค(Facts)๊ณผ ๊ทธ์— ์ค€ํ•œ ํŒ๋‹จ๋“ค์ด ๋‚˜์—ด๋˜๋Š”๋ฐ, ์ด๊ฒƒ๋“ค์€ ์ค‘๋ณต๊ณผ ๋ˆ„๋ฝ์ด ์—†๊ณ  ๋…ผ๋ฆฌ์  ์ธ๊ณผ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ฒŒ ๊ตฌ์„ฑํ•ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ ํ”ผ๋ผ๋ฏธ๋“œ ๊ตฌ์กฐ์˜ ๋ฌธ์„œ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ์— ๋”ฐ๋ผ ๋ฉ”์‹œ์ง€๋ฅผ ๊ฒ€ํ† ํ•˜๊ณ  ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. 1. ๋ฉ”์‹œ์ง€ ๋‚˜์—ด(list-up) 2. ๋™์ผ ๊ฐœ๋…์˜ ์ •๋ณด๋ผ๋ฆฌ ๋ฌถ์Œ(grouping) 3. ๊ฐ ๊ทธ๋ฃน์˜ ๋Œ€ํ‘œ ๊ฐœ๋…์„ ํ•œ ๋‹จ๊ณ„ ์œ„์— ๋†“์Œ 4. ๊ฐ ๊ทธ๋ฃน์˜ ๋…ผ๋ฆฌ์  ๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๋ฐฐ์—ด 5. ๊ฐ ๊ทธ๋ฃน ์•ˆ์— ์žˆ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ๋…ผ๋ฆฌ์  ๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๋ฐฐ์—ด 6. ๋…ผ์ง€์˜ ์ค‘๋ณต, ๋ˆ„๋ฝ, ๋น„์•ฝ, ๋ฐ์ดํ„ฐ์˜ ์น˜์šฐ์นจ๊ณผ ๊ฒฐํ•จ ์œ ๋ฌด ํ™•์ธ ๋ฐ ์ˆ˜์ • 1 ~ 6์„ ์ˆ˜์ฐจ๋ก€ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ํ”ผ๋ผ๋ฏธ๋“œ ๊ตฌ์กฐ๋ฅผ ๊ฐ–์ถ˜ ๋ฌธ์„œ์˜ ์™„์„ฑ๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค. Figure II-40์€ ํ”ผ๋ผ๋ฏธ๋“œ ๊ตฌ์กฐ๋ฅผ ๊ฐ–์ถ˜ ๋ฌธ์„œ์˜ ์ž‘์„ฑ์˜ ๊ฐœ๋…์„ ๋‚˜ํƒ€๋‚ด๋ณด์•˜๋‹ค. Figure II-40. ๋ฌธ์„œ ์ž‘์„ฑ์˜ ํ”ผ๋ผ๋ฏธ๋“œ ๊ตฌ์กฐ ๋ฌธ์„œ ์ž‘์„ฑ์„ ์œ„ํ•œ ๋ชจ๋“  ๋ฉ”์‹œ์ง€๋“ค์ด ํ”ผ๋ผ๋ฏธ๋“œ ๊ตฌ์กฐ ํ•˜์—์„œ ์ž˜ ์ •๋ฆฌ๋˜์—ˆ๋‹ค๋ฉด ์ด์ œ ํŒŒ์›Œํฌ์ธํŠธ(MS powerpoint)๋‚˜ ํ‚ค๋…ธํŠธ(Apple Keynote)๊ณผ ๊ฐ™์€ ์ €์ž‘ ๋„๊ตฌ๋ฅผ ํ™œ์šฉํ•ด ๋ณด์ž. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด ๋„๊ตฌ๋“ค์€ ๋งค์šฐ ๋‹ค์–‘ํ•œ ํšจ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๊ตฌ์กฐ์ ์ธ ์ปจ์„คํŒ… ๋ฌธ์„œ, ์ฆ‰ ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ ์ž‘์„ฑ์— ์ง‘์ค‘ํ•˜๊ณ ์ž ํ•œ๋‹ค. ํŒŒ์›Œํฌ์ธํŠธ๋‚˜ ํ‚ค๋…ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์Šฌ๋ผ์ด๋“œ(slide)๋ผ๋Š” ๊ฐœ๋…์ด ์žˆ๋‹ค. ๋นˆ ๋„ํ™”์ง€๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋˜๋Š”๋ฐ ์ €์ž๋Š” ํ”ํžˆ ์ปจ์„คํŒ… ๋ณด๊ณ ์„œ์˜ ํ‘œ์ค€<NAME>์ด๋ผ๊ณ  ์•Œ๋ ค์ง„ ๋งฅํ‚จ์ง€ ์Šคํƒ€์ผ[2]์„ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•œ๋‹ค. Figure II-41์€ ๋งฅํ‚จ์ง€ ์Šคํƒ€์ผ์˜ ์Šฌ๋ผ์ด๋“œ ํฌ๋งท์ด๋‹ค. ๊ฐ ํ•ญ๋ชฉ๋ณ„๋กœ ์ข€ ๋” ๋ถ€์—ฐ ์„ค๋ช…ํ•˜๋ฉด Figure II-41. ๋ฌธ์„œ ์Šฌ๋ผ์ด๋“œ ํฌ๋งท - ๋งฅํ‚จ์ง€ ์Šคํƒ€์ผ โ‘  Governing Message: ๊ฑฐ๋ฒ„๋‹ ๋ฉ”์‹œ์ง€๋Š” ์Šฌ๋ผ์ด๋“œ์—์„œ ์ „๋‹ฌํ•˜๊ณ ์ž ํ•˜๋Š” ์ •๋ณด์˜ ํ•ต์‹ฌ์„ ๊ธฐ์ˆ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ๋‹จ์ •์ ์ด๋ฉฐ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜์—ฌ 1์ค„์ด ๋„˜์–ด๊ฐ€์ง€ ์•Š๋„๋ก ์ž‘์„ฑํ•œ๋‹ค. ์•„์šธ๋Ÿฌ ์ „/ํ›„ ํŽ˜์ด์ง€์˜ ์—ฐ๊ฒฐ์„ ๊ฐ์•ˆํ•˜์—ฌ ์ „์ฒด์ ์ธ ๋ฉ”์‹œ์ง€์˜ ๋ฌธ๋งฅ์„ ์กฐ์ •ํ•ด์•ผ ํ•œ๋‹ค. โ‘ก Title: ์ •๋ณด๋ฅผ ์ด๊ด„ํ•˜๋Š” ์–ด๊ตฌ๋กœ ๋ถ„์„ ๋ช…์ผ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. Title์„ ํ†ตํ•ด ์ฐจํŠธ๊ฐ€ ๋ฌด์—‡์„ ๋งํ•˜๋Š”์ง€ ์‹ ์† ์ •ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. โ‘ข Chart: ํ‘œ๋‚˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋œปํ•˜๋ฉฐ ๊ตฌ์ฒด์ ์ธ ์‚ฌ์‹ค์„ ๋ณด์—ฌ์ฃผ๋ฏ€๋กœ Governing Message๋ฅผ ์ง€์ง€ํ•˜๋Š” ์ •๋ณด ์ด๋‹ค. ์ƒ๋Œ€๋ฐฉ์„ ์ดํ•ด์‹œํ‚ค๋Š”๋ฐ ํ™œ์šฉ๋˜๋ฉฐ ์ •๋Ÿ‰ํ™”, ์‹œ๊ฐํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. โ‘ฃ ๊ฐ์ฃผ: ์ฐจํŠธ์— ๋‚˜ํƒ€๋‚œ ์ •๋ณด์˜ ๋ณด์ถฉ์ ์ธ ์ฃผ์„์œผ๋กœ ์ฐจํŠธ๊ฐ€ ๋ฐฉ๋Œ€ํ•˜๊ฒŒ ๊ธฐ์ˆ ๋˜๋ฉด ์ •๋ณด์˜ ํŒŒ์•…์ด๋‚˜ ๊ฐ€์‹œ์„ฑ์ด ์ข‹์ง€ ์•Š๊ฒŒ ๋˜๋ฏ€๋กœ ์ฃผ์š” ์ •๋ณด์˜ ์ „๋‹ฌ์„ ๋ฐฉํ•ดํ•˜์ง€ ์•Š๋„๋ก ๋ฐ์ดํ„ฐ์˜ ์•ˆ์ •์„ฑ, ์‹ ๋ขฐ์„ฑ์„ ์œ„ํ•ด ๋ณ„๋„ ํ‘œ๊ธฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. โ‘ค ์ž๋ฃŒ์›: ์ธ์šฉํ•œ ๋ฐ์ดํ„ฐ์˜ ์ถœ์ฒ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ ๋ฐ์ดํ„ฐ ์‹ ๋ขฐ์„ฑ์˜ ์ง€ํ‘œ๊ฐ€ ๋œ๋‹ค. ์ฐจํ›„ ๊ฒ€์ฆ์„ ์œ„ํ•ด์„œ๋„ ํ•„์š”ํ•˜๋‹ค โ‘ฅ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ: ์ž๋ฃŒ ์ž‘์„ฑ ์‹œ ์ž‘์—…์˜ ํšจ์œจํ™” ๋ฐ ์ž๋ฃŒ์˜ ๋ณด๊ด€์— ์œ ์šฉํ•˜๋‹ค. ๋ธŒ๋ฆฌํ•‘ ์‹œ์— ๋ถ„๋Ÿ‰์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค ์Šฌ๋ผ์ด๋“œ์—์„œ ์ฐจํŠธ์˜ ์‚ฝ์ž…์€ โ€˜์ผ๋„ ์ผ์‚ฌ(1ๅœ– 1ไบ‹)โ€™์˜ ์›์น™์„ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ฆ‰, ํ•œ ํŽ˜์ด์ง€์— ํ•˜๋‚˜์˜ ๊ฐœ๋…์ด ์ „๋‹ฌ๋˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ตœ๋Œ€ํ•œ ์ง๋…์„ฑ๊ณผ ๊ฐ„๊ฒฐ์„ฑ์„ ์ถ”๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฌธ์„œ ์ž‘์„ฑ์ด ๋๋‚˜๋ฉด ๋ฌธ์„œ ์š”์•ฝ์„ ํ•ด์•ผ ํ•œ๋‹ค. โ€˜Executive Summaryโ€™๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๋ฌธ์„œ ์š”์•ฝ์€ ํ”ผ๋ผ๋ฏธ๋“œ ๊ตฌ์กฐ์˜ ๋ฌธ์„œ์—์„œ ํ‘œํ˜„๋œ ๊ฑฐ๋ฒ„๋‹ ๋ฉ”์‹œ์ง€๋ฅผ ์Šฌ๋ผ์ด๋“œ ํ•œ ์žฅ์— ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. Figure II-42๋Š” ํ”ผ๋ผ๋ฏธ๋“œ ๊ตฌ์กฐ๋กœ ์ž‘์„ฑ๋œ ๋ฌธ์„œ์˜ ์š”์•ฝ์„ ๊ฐœ๋…์ ์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด๋‹ค. ๋ฌธ์„œ ์š”์•ฝ์€ ๋ฐ”์œ ๊ฒฝ์˜์ง„์ด ์ „์ฒด ๋ณด๊ณ ๋ฅผ ๋ฐ›๊ธฐ ์ „์— ๋น ๋ฅธ ์‹œ๊ฐ„ ์•ˆ์— ๋‚ด์šฉ์„ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ๊ทธ ๋ชฉ์ ์ด๋ฏ€๋กœ ๋Œ€๋‹จํžˆ ์••์ถ•์ ์œผ๋กœ ํ•ต์‹ฌ ๋ฉ”์‹œ์ง€๋งŒ์„ ๋‹ด๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋ณด๊ณ ์˜ ์ •์ˆ˜(็ฒพ้ซ“)๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. Figure II-42. ํ”ผ๋ผ๋ฏธ๋“œ ๊ตฌ์กฐ ๋ฌธ์„œ์˜ ์š”์•ฝ ๋ฐ ์‚ฌ๋ก€ ์ง€๊ธˆ๊นŒ์ง€ ์ปจ์„คํŒ… ๋ฌธ์„œ ์ž‘์„ฑ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์•˜๋‹ค. ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ์›จ์–ด์˜ ์˜คํ”ผ์Šค๊ฐ€ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ๋ฏผ๊ฐ„ ๊ธฐ์—…์—์„œ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์„œ ์ €์ž‘ ๋„๊ตฌ๋กœ๋Š” MS ์›Œ๋“œ์™€ ๊ฐ™์€ ๊ฒƒ์„ ์ˆ™์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ณ , ๊ตญ๋‚ด ์ •๋ถ€ ๋ฐ ๊ณต๊ณต๊ธฐ๊ด€์˜ ๋ฌธ์„œ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋Š” ํ•œ๊ธ€ ์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ค„ ์•Œ์•„์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ผ๋ถ€ ๋ฌธ์„œ์˜ ๊ฒฝ์šฐ, ํŒŒ์›Œํฌ์ธํŠธ๋‚˜ ํ‚ค๋…ธํŠธ๋กœ ์ž‘์—…ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋Š” ๊ฑฐ์˜ ํ”„๋ ˆ์  ํ…Œ์ด์…˜(Presentation)์„ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ๋Š” ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ฐฉ๋ฒ•๊ณผ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ์œ„ํ•œ ๋ฌธ์„œ ์ž‘์—…์— ๋Œ€ํ•ด ์ข€ ๋” ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. [1] ์—ฐ์—ญ์  ์ „๊ฐœ๋ฅผ ๊ณ ๋ คํ•˜์˜€๋‹ค. [2] ๋งฅํ‚จ์ง€ ์ปจ์„คํŒ…์ด ํ•œ๊ตญ ๊ธฐ์—…๋“ค์˜ ๋ณด๊ณ  ๋ฌธํ™”์— ๋ผ์นœ ์˜ํ–ฅ์€ ์ง€๋Œ€ํ•˜๋‹ค. ๋ณด๊ณ ์„œ ํฌ๋งท ๋ฐ ๋ณด๊ณ  ๋ฐฉ์‹ ๋“ฑ ๊ฑฐ์˜ ํ‘œ์ค€์„ ์ œ์‹œํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์š”์ฆ˜์˜ ๋งฅํ‚จ์ง€ ์ปจ์„คํŒ… ์‚ฐ์ถœ๋ฌผ์€ ๋ฐ˜๋“œ์‹œ ์ด ํฌ๋งท์„ ๋”ฐ๋ฅด์ง€๋Š” ์•Š๋Š”๋‹ค. 06. ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ(3/3)- ํ”„๋ ˆ์ก˜ํ…Œ์ด์„  ์ด ์žฅ์—์„œ๋Š” ์ปจ์„คํŒ… ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ์˜ ๋งˆ์ง€๋ง‰ ์ˆœ์„œ, ํ”„๋ ˆ์  ํ…Œ์ด์…˜(Presentation)์„ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. 6.3 ํ”„๋ ˆ์  ํ…Œ์ด์…˜(Presentation) ์ปจ์„คํŒ… ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ์—์„œ ๋“ฃ๊ธฐ์™€ ์ธํ„ฐ๋ทฐ, ์“ฐ๊ธฐ์™€ ๋ฌธ์„œํ™”๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋ฉด, ๋งํ•˜๊ธฐ์™€ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์ด ๋˜ ๊ทธ๋Ÿฐ ๊ด€๊ณ„์ด๋‹ค. Table II-13์€ ๋ฌธ์„œ์™€ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ์ƒํ™ฉ์„ ๋น„๊ตํ•ด ๋ณด์•˜๋‹ค. Table II-13. ๋ฌธ์„œ์™€ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ์ƒํ™ฉ ๋น„๊ต ์ฆ‰, ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ์— ๋น„ํ•ด ํ”„๋ ˆ์  ํ…Œ์ด์…˜์€ ํ•ธ๋””์บก(Handicap)์ด ์žˆ๊ณ  ์ด๋ฅผ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งค์šฐ ์ „๋žต์ ์ด์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ํ•ต์‹ฌ๊ณผ ๋ณธ์งˆ์— ์ง‘์ค‘ํ•ด์•ผ ํ•˜๋ฉฐ ๊ทธ๊ฒƒ์„ ํ‘œํ˜„ํ•จ์— ์žˆ์–ด ๊ตฌ์กฐ์ (Structural)์ด๋ฉฐ ๊ฐ„๊ฒฐํ•œ ์‹œ๊ฐํ™”(Visualization)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ๊ตฌ์„ฑํ•  ๋•Œ๋Š” Table II-14์™€ ๊ฐ™์ด ์„œ๋ก , ๋ณธ๋ก , ๊ฒฐ๋ก  3๋‹จ๊ณ„๋กœ ์ •๋ฆฌํ•œ๋‹ค. Table II-14. ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ๊ตฌ์„ฑ ๋‹จ๊ณ„ ๋˜ํ•œ, ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ๋ณธ๋ฌธ์—์„œ ์ „๋‹ฌํ•˜๊ณ ์ž ํ•˜๋Š” ๋‚ด์šฉ์€ ๋‹ค์Œ 3๊ฐ€์ง€ ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ํ•ต์‹ฌ ๋ฉ”์‹œ์ง€ (Key Messages) ์ด์•ผ๊ธฐ ๊ตฌ์กฐ (Storyline) ๋ณ€์ˆ˜(Arguments) ์ฒซ ๋ฒˆ์งธ, ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ํ•ต์‹ฌ ๋ฉ”์‹œ์ง€๋Š” ๊ทธ ๋ชฉ์ ์— ๋”ฐ๋ผ ์ฒญ์ž(Audience)์—๊ฒŒ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜, ์ฒญ์ž๋ฅผ ์„ค๋“ํ•˜๊ฑฐ๋‚˜, ์ฒญ์ž๊ฐ€ ์–ด๋–ค ํ–‰๋™์„ ์œ ๋ฐœํ•˜๊ฒŒ ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ์ด์•ผ๊ธฐ ๊ตฌ์กฐ๋Š” ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ํ๋ฆ„์„ ์ขŒ์ง€์šฐ์ง€ํ•œ๋‹ค. ๊ท€์— ์™์™ ๋“ค์–ด์˜จ๋‹ค๋Š” ํ‘œํ˜„์€ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์ด ๊ตฌ์กฐ์ ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ๊ณผ ์ผ๋งฅ์ƒํ†ต(ไธ€่„ˆ็›ธ้€š) ํ•œ๋‹ค. MECE ์›์น™์— ๋”ฐ๋ผ ์ด์•ผ๊ธฐํ•  ๊ฒƒ๋“ค์„ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์ „๊ฐœํ•˜๋Š” ๊ฒƒ์€ ๋ถ„๋ช… ๋„์›€์ด ๋˜์ง€๋งŒ ๋ถ„์„์„ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ ๋ฉ”์‹œ์ง€๋กœ ๊ท€๊ฒฐ๋˜๋Š” ํ˜•ํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•˜๋‚˜์˜ ์ฃผ์ œ์—์„œ ๊ฐ„๊ฒฐํ•˜๊ณ  ์—ฐ๊ด€์„ฑ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์‹œ์ง€๋“ค์ด ๋„์ถœ๋  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. ์„ธ ๋ฒˆ์งธ, ๋ณ€์ˆ˜๋Š” ์ด์•ผ๊ธฐ๋ฅผ ์ „๊ฐœํ•˜๋ฉด์„œ ๊ทธ ์ฃผ์žฅ์„ ๋ณด๋‹ค ๊ฐ๊ด€์ ์ธ ๊ด€์ ์—์„œ ์ง€์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. ๋‹จ์ˆœํ•œ ์ˆซ์ž์ผ ์ˆ˜๋„ ์žˆ๊ณ  ์ˆซ์ž๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ ํ‘œ๋‚˜ ๊ทธ๋ž˜ํ”„๋กœ ์š”์•ฝํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. Table II-15๋Š” ์ด์™€ ๊ฐ™์€ 3๊ฐ€์ง€ ์š”์†Œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์Šฌ๋ผ์ด๋“œ๋ฅผ ๊ตฌ์„ฑํ•ด ๋ณธ ๊ฒƒ์ด๋‹ค. Table II-15. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์Šฌ๋ผ์ด๋“œ ๊ตฌ์„ฑ ์‚ฌ๋ก€ ํŒŒ์›Œํฌ์ธํŠธ๋‚˜ ํ‚ค๋…ธํŠธ ๊ฐ™์€ ๋ฌธ์„œ ์ž‘์„ฑ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ์ž‘์„ฑํ•˜๊ธฐ ์ „์— Table II-16๊ณผ ๊ฐ™์€ ์ˆœ์„œ์™€ ๊ตฌ์„ฑ์œผ๋กœ ์‚ฌ์ „์— ์ž‘์„ฑํ•ด ๋ณด๋Š” ๊ฒƒ์€ ์ข‹์€ ์Šต๊ด€์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ์ €์ž์™€ ๊ฐ™์ด 4์žฅ์˜ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ค€๋น„ ํ…œํ”Œ๋ฆฟ์„ ํ™œ์šฉํ•ด ๋ณด์ž. ์‹œ๋‚˜๋ฆฌ์˜ค๋Š” ์ €์ž์—๊ฒŒ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—… A์‚ฌ์—์„œ ํ•œ๊ตญ๋ฒ•์ธ์žฅ ์ง์— ๋Œ€ํ•ด ์ œ์•ˆ์ด ์™€์„œ A์‚ฌ์˜ ์•„์‹œ์•„ ํƒœํ‰์–‘ ๋ณธ๋ถ€์žฅ, ์‚ฌ์—…์ „๋žต ๋‹ด๋‹น(๋‚˜์—๊ฒŒ ์ œ์•ˆ์„ ํ•˜์‹  ๋ถ„), ์ธ์‚ฌ ๋‹ด๋‹น๊ณผ ์ตœ์ข… ์ธํ„ฐ๋ทฐ๋ฅผ ์•ž๋‘๊ณ  ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋‹ค. Table II-16. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ค€๋น„ ํ…œํ”Œ๋ฆฟ - ์ƒํ™ฉ์˜ ์ •๋ฆฌ ์šฐ์„  ๋ฐฐ๊ฒฝ ๋ฐ ๋ชฉ์ , ํ™”์ž(Audience), ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ์ฃผ์ œ, ์ฝ˜ํ…์ธ ์˜ ์ฃผ์š” ๋‚ด์šฉ์„ Table II-16๊ณผ ๊ฐ™์€ ํ•œ ์žฅ์˜ ์‹œํŠธ(sheet)๋กœ ์ •๋ฆฌํ•œ๋‹ค. ๋‹ค์Œ์€ Table II-14์—์„œ ์†Œ๊ฐœํ•œ ๊ตฌ์กฐ๋Œ€๋กœ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ์„œ๋ก , ๋ณธ๋ก , ๊ฒฐ๋ก ์„ ์ƒ๊ฐํ•ด ๋ณธ๋‹ค. ์„œ๋ก , ๋ณธ๋ก , ๊ฒฐ๋ก ์„ ๊ตฌ์ฒด์ ์œผ๋กœ ์ƒ๊ฐํ•ด ๋ณด๋ฉด์„œ ๊ฐ๊ฐ ์†Œ์š”๋  ์‹œ๊ฐ„๋„ ๊ฐ™์ด ์ƒ๊ฐํ•ด ๋ณธ๋‹ค. ์ด๋Š” ์ „์ฒด ๋‚ด์šฉ์„ ์ „๋‹ฌํ•˜๋Š” ๊ฐ€์žฅ ์ ํ•ฉํ•œ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. Table II-17. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ค€๋น„ ํ…œํ”Œ๋ฆฟ - ์„œ๋ก  ์„œ๋ก  ๋ถ€๋ถ„์—์„œ๋Š” Table II-17๊ณผ ๊ฐ™์ด ์ž๊ธฐ์†Œ๊ฐœ์™€ ์ฃผ์˜ ํ™˜๊ธฐ, ๋™๊ธฐ ๋ถ€์—ฌ, ์ฃผ์š” Point ์†Œ๊ฐœ, ์งˆ์˜์‘๋‹ต ์•ˆ๋‚ด ๋“ฑ์„ ์ •๋ฆฌํ•œ๋‹ค. ์‹œ๊ฐ„์€ ์ž„์˜๋กœ ๋ถ€์—ฌํ–ˆ๊ณ  ์ƒํ™ฉ์— ๋งž๊ฒŒ ์ ์ ˆํžˆ ์ •ํ•˜๋ฉด ๋œ๋‹ค. ์ €์ž์—๊ฒŒ โ€˜๋ญ ์ด๋Ÿฐ ๊ฒƒ์„ ๋‹ค ๋งŒ๋“œ๋ƒ?โ€™๊ณ  ์งˆ๋ฌธํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ์ด๋ ‡๊ฒŒ ์ค€๋น„ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒƒ์€ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ์™„๊ฒฐ์„ฑ์—์„œ ์ƒ๋‹นํ•œ ์ฐจ์ด๊ฐ€ ๋‚œ๋‹ค. ๋˜ํ•œ, ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ „๋ฌธ๊ฐ€๋“ค๋„ ์ด๋Ÿฐ ์‹์˜ ์ค€๋น„๋ฅผ ์ˆ˜์—†์ด ์‹œํ–‰ํ•˜๊ณ  ๊ทธ๊ฒƒ์ด ๋จธ๋ฆฟ์†์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ์ ์ง€ ์•Š์„ ๋ฟ์ด๋‹ค. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์„œ๋ก ์„ ์œ„ํ•œ ์‹œํŠธ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ๋ณธ๋ฌธ์„ ์œ„ํ•œ ์‹œํŠธ๋„ ์ž‘์„ฑํ•ด ๋ณด์ž. ๋ณธ๋ฌธ์€ ์„œ๋ก  ์ค€๋น„ ์‹œํŠธ์—์„œ ์ •์˜ํ•œ ํ•ต์‹ฌ ๋‚ด์šฉ์„ ์ ์–ด๋ณด๋Š” ๊ฒƒ์ธ๋ฐ ๋‚˜์ค‘์— ์„ค๋ช…ํ•˜๊ฒ ์ง€๋งŒ ์ฐจํŠธ(ํ‘œ๋‚˜ ๊ทธ๋ž˜ํ”„)๋ฅผ ๊ทธ๋ ค์„œ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ์ด ๋‹จ๊ณ„์—์„œ๋Š” ๊ทธ๋ƒฅ ํ…์ŠคํŠธ๋กœ ์ฃฝ ์ ์–ด๋ณด๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. Table II-18. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ค€๋น„ ํ…œํ”Œ๋ฆฟ - ๋ณธ๋ก [1] ๋ณธ๋ฌธ์— ๋Œ€ํ•œ ์‹œํŠธ ์ •๋ฆฌ๊ฐ€ ๋๋‚˜๋ฉด ์ด์ œ ๋งˆ์ง€๋ง‰ ๊ฒฐ๋ก ์— ๋Œ€ํ•œ ์‹œํŠธ๋ฅผ ์ •๋ฆฌํ•ด ๋ณด์ž. Table II-19. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ค€๋น„ ํ…œํ”Œ๋ฆฟ - ๊ฒฐ๋ก  ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์‹œ๊ฐ„๊ณผ ๊ด€๋ จํ•ด์„œ๋Š” ์ค‘์š”ํ•œ ํ™ฉ๊ธˆ๋ฅ (Golden Rule)์ด ์žˆ๋‹ค. ๋ฐ”๋กœ 18๋ถ„์„ ๋„˜๊ธฐ์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์€ ์ผ์ข…์˜ ๊ณจ๋””๋ฝ์Šค(Goldilocks)[2] ํƒ€์ž„์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ทธ ์œ ๋ช…ํ•œ TED[3]๋„ ์–ด๋–ค ์—ฐ์‚ฌ๊ฐ€ ๋ฐœํ‘œํ•˜๋”๋ผ๋„ ์‹œ๊ฐ„์„ 18๋ถ„์— ๋งž์ถ”๊ณ  ์žˆ๋‹ค. ์ด๋Š” ์ผ์ข…์˜ ๋‡Œ๊ณผํ•™์—์„œ ์ด์•ผ๊ธฐํ•˜๋Š” ์ธ์ง€ ๋ฐ€๋ฆผ(Cognitive Backlog) ํ˜„์ƒ[4]์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ธ๋ฐ ๊ตณ์ด ๊ทธ๋Ÿฐ ์ด๋ก ์„ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š์•„๋„ ์ด ์‹œ๊ฐ„์„ ๋„˜์–ด์„œ๋ฉด ์ง€๊ฒจ์›Œ์„œ ์ง‘์ค‘ํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค. ๋งŽ์€ ์ด๋“ค์˜ ํ˜ธ์‘์„ ์–ป์€ ์„ธ๊ณ„์ ์œผ๋กœ ์•Œ๋ ค์ง„ ๋ช…์—ฐ์„ค, ํ”„๋ ˆ์  ํ…Œ์ด์…˜๋“ค์€ ๊ฑฐ์˜ 18๋ถ„์„ ๋„˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๋ ค์ ธ ์žˆ๋‹ค. Figure II-43. ๊ณจ๋””๋ฝ์Šค ๋™ํ™”์™€ TED ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ์œ„ํ•œ ์ „์ฒด ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์‹œํŠธ์™€ ํ•จ๊ป˜ ์ •๋ฆฌํ•˜์—ฌ ์Šคํ† ๋ฆฌ๋ผ์ธ์„ ๋ณด์™„ํ•œ ํ›„์—๋Š” ๋ฌธ์„œ ์ €์ž‘ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ์ž‘์„ฑํ•œ๋‹ค. ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ๋‚ด์šฉ์ด ๋งŽ์•„์ง€๋ฉด ํฌ์ŠคํŠธ์ž‡(PostIt)๊ณผ ๊ฐ™์€ ์ฐฉํƒˆ์‹ ๋ฉ”๋ชจ์ง€๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์Šคํ† ๋ฆฌ๋ผ์ธ์„ ๋ถ„ํ•ด, ์žฌ๋ฐฐ์น˜ํ•˜์—ฌ ์ •๋ฆฌํ•˜๊ณ  ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ๊ตฌ์กฐ๋ฅผ ์ฒด๊ณ„์ ์œผ๋กœ ์ƒ๊ฐํ•ด ๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋‹ค. ์ œ๋ชฉ๊ณผ ๋ฐฐ๊ฒฝ, ๋ชฉ์ ์ด ํฌํ•จ๋œ ์„œ๋ก , ๊ฐ ์ฃผ์ œ๋ณ„ ๋ฉ”์‹œ์ง€๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ณธ๋ก , ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์š”์•ฝ ๋ฐ ๋‹ค์Œ ๋‹จ๊ณ„์— ๋Œ€ํ•œ ์†Œ๊ฐœ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฒฐ๋ก ์˜ ๊ตฌ์„ฑ์€ ๋™์ผํ•˜๋‹ค. Figure II-44. ํฌ์ŠคํŠธ์ž‡์„ ์ด์šฉํ•œ ์Šคํ† ๋ฆฌ๋ผ์ธ ๊ตฌ์„ฑ ์Šคํ† ๋ฆฌ๋ผ์ธ์˜ ๋‚ด์šฉ ๋ณด์™„๊ณผ ํ๋ฆ„, ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ๊ณผ์˜ ๊ด€๊ณ„ ๋“ฑ์ด ๋งŒ์กฑ์Šค๋Ÿฌ์šฐ๋ฉด ์—ญ์‹œ ๋ฌธ์„œ ์ €์ž‘ ๋„๊ตฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ๋‹ค. ์Šฌ๋ผ์ด๋“œ๋Š” ํฌ๊ฒŒ 5๊ฐ€์ง€ ์ข…๋ฅ˜๋กœ ๊ตฌ์„ฑ๋˜๋Š”๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํ‘œ์ง€ ์Šฌ๋ผ์ด๋“œ ๋ชฉ์ฐจ ์Šฌ๋ผ์ด๋“œ ๊ฐ„์ง€ ์Šฌ๋ผ์ด๋“œ ๋‚ด์šฉ ์Šฌ๋ผ์ด๋“œ ๋ณด์กฐ ์Šฌ๋ผ์ด๋“œ ํ‘œ์ง€ ์Šฌ๋ผ์ด๋“œ๋Š” ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ œ๋ชฉ, ๋ฐœํ‘œ์ž, ๋‚ ์งœ, ๋ฐฐํฌ๋‚˜ ๊ณต์œ  ๋ฒ”์œ„ ๋“ฑ์ด ์„œ์ˆ ๋œ๋‹ค. ๋ชฉ์ฐจ ์Šฌ๋ผ์ด๋“œ๋Š” ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ๋ชฉ์ฐจ๋ฅผ ๋ณดํ†ต 3์ˆ˜์ค€๊นŒ์ง€ ํ‘œ์‹œํ•˜๋ฉฐ, ๊ฐ„์ง€ ์Šฌ๋ผ์ด๋“œ๋Š” ์žฅ์ด ๋ฐ”๋€” ๋•Œ ๋นˆ ๊ณต๋ฐฑ์˜ ์Šฌ๋ผ์ด๋“œ๋กœ ๋‹ค์Œ ์žฅ์œผ๋กœ ๋„˜์–ด๊ฐ์„ ์•Œ๋ฆฌ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰ ์žฅ์€ EOD(End of Documents)๋ผ๊ณ  ํ‘œ๊ธฐํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ๋‚ด์šฉ ์Šฌ๋ผ์ด๋“œ๋Š” ๊ฑฐ๋ฒ„๋‹ ๋ฉ”์‹œ์ง€(Governing Message)์™€ ํ‘œ๋‚˜ ๊ทธ๋ž˜ํ”„ ๊ฐ™์€ ์ฐจํŠธ(Charts)๊ฐ€ ํฌํ•จ๋˜๋ฉฐ ๋ณด์กฐ ์Šฌ๋ผ์ด๋“œ๋Š” ๋‚ด์šฉ ์Šฌ๋ผ์ด๋“œ์™€ ๋งํฌ(link) ์‹œ์ผœ ํ•„์š”์‹œ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. ๊ฒฐ๊ตญ ์ด๋Ÿฐ ๋‹ค์–‘ํ•œ ์Šฌ๋ผ์ด๋“œ๋Š” Table II-16๋ถ€ํ„ฐ Table II-19๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์ ์–ด๋ณด์•˜๋˜ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋‚ด์šฉ์„ ์˜ฎ๊ฒจ๋†“์€ ๊ฒƒ์œผ๋กœ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์Šคํ† ๋ฆฌ๋ฅผ ์ง€์ง€ํ•˜๋„๋ก ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•œ๋‹ค. Figure II-45. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์Šฌ๋ผ์ด๋“œ์˜ ๊ตฌ์„ฑ Break #9. ๋‚˜์ดํŒ…๊ฒŒ์ผ๊ณผ ๊ทธ๋ž˜ํ”„ ๊ทธ๋ž˜ํ”„(Graph)๋Š” ์ฐจํŠธ(Charts)์˜ ํ•œ ์ข…๋ฅ˜์ด๋‹ค. ์ •๋Ÿ‰์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๋ˆˆ์— ์•Œ์•„๋ณด๊ธฐ ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•˜๋Š” ์ผ์ข…์˜ ๊ทธ๋ฆผ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์‚ฌ์šฉํ•˜์—ฌ ์ •ํ˜•ํ™”๋œ ๊ฒƒ๋ถ€ํ„ฐ ์•„์ฃผ ์ฐฝ์˜์ ์ธ ํ˜•ํƒœ์˜ ๊ฒƒ๊นŒ์ง€ ๋งค์šฐ ๋‹ค์–‘ํ•˜๋‹ค. ๊ทธ๋ž˜ํ”„ ์‚ฌ์šฉ์˜ ๊ฐ€์žฅ ํฐ ์žฅ์ ์€ ๋Œ€์šฉ๋Ÿ‰์˜ ํ†ต๊ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ๊ฐ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์‹œ์‚ฌ์ ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค€๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์‹ค์ œ๋กœ ์ œ1์ฐจ ์„ธ๊ณ„ ๋Œ€์ „ ๋•Œ ๋‚˜์ดํŒ…๊ฒŒ์ผ(Florence Nightingale. 1820~ 1910)์€ Figure II-46๊ณผ ๊ฐ™์€ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ธ๋Š”๋ฐ ์ด๋ฅผ '๋กœ์ฆˆ ๋‹ค์ด์–ด๊ทธ๋žจ(Rose Diagram)'์ด๋ผ๊ณ  ํ•œ๋‹ค. Figure II-46. ๋‚˜์ดํŒ…๊ฒŒ์ผ์ด ์‚ฌ์šฉํ•œ ๊ทธ๋ž˜ํ”„ ํŒŒ์ด(Pie) ์ฐจํŠธ์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ๊ฐ๋„์˜ ํฌ๊ธฐ๋ฟ ์•„๋‹ˆ๋ผ ๋ฉด์ ์˜ ํฌ๊ธฐ์—๋„ ์˜๋ฏธ๊ฐ€ ๋ถ€์—ฌ๋จ์œผ๋กœ 2์ฐจ์› ๊ทธ๋ž˜ํ”„์ด์ง€๋งŒ ๋‹ค์ฐจ์› ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œํ˜„ํ•œ, ์—‘์…€์ด ์—†์—ˆ๋˜ ๋‹น์‹œ์˜ ์ƒํ™ฉ์œผ๋กœ ๋ณด๋ฉด ๋งค์šฐ ์ฐฝ์˜์ ์ธ ๊ทธ๋ž˜ํ”„๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‚˜์ดํŒ…๊ฒŒ์ผ์ด ์ด๋Ÿฐ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๊ฒŒ ๋œ ๋™๊ธฐ๋Š” ๋ณ‘์›์—์„œ ๊ฐ„ํ˜ธ ํ™œ๋™์„ ํ•˜๋Š” ๋„์ค‘ ์ „์Ÿ์—์„œ ๋ถ€์ƒ๋‹นํ•ด์„œ ์ฃฝ๋Š” ๊ฒƒ๋ณด๋‹ค ๋ณ‘์›์˜ ์—ด์•…ํ•œ ์œ„์ƒ ํ™˜๊ฒฝ์—์„œ ์ œ๋Œ€๋กœ ์น˜๋ฃŒ๋‚˜ ๊ด€๋ฆฌ๊ฐ€ ๋˜์ง€ ๋ชปํ•ด ์ฃฝ๋Š” ์‚ฌ๋žŒ๋“ค์ด ํ›จ์”ฌ ๋งŽ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์ด๋ฅผ ๊ฐœ์„ ํ•ด ๋ณด๊ณ ์ž ๋…ธ๋ ฅํ–ˆ์œผ๋‚˜ ์ œ๋Œ€๋กœ ์ง€์›์ด ๋˜์ง€ ์•Š์ž, ์ด๋ฅผ ์œ„ํ•œ ์˜ˆ์‚ฐ ๋งˆ๋ จ์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ถ•์ ํ•˜๊ณ  ์ด๋ฅผ ํ‘œ๋กœ ๋งŒ๋“ค์–ด ์˜์›๋“ค์„ ์„ค๋“ํ•˜์—ฌ ์ง€์›์„ ๋ฐ›์•˜๋‹ค๊ณ  ํ•œ๋‹ค. ์ด๋Ÿฐ ๋‚˜์ดํŒ…๊ฒŒ์ผ์˜ ๋…ธ๋ ฅ ๋•๋ถ„์ผ๊นŒ? ์˜ค๋Š˜๋‚  ๊ฒฝ์˜ ์ปจ์„คํŒ…์—์„œ๋Š” ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ทธ๋ž˜ํ”„๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋‹ค. ์ฆ‰, ๋ชฉ์ ๊ณผ ์šฉ๋„์— ๋งž๊ฒŒ ๊ทธ๋ž˜ํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ํ•˜๊ธฐ ํ›จ์”ฌ ์‰ฌ์šด ๊ฒƒ์ด๋‹ค. Table II-20์€ ๋น„์ฆˆ๋‹ˆ์Šค ๋ฌธ์„œ์—์„œ ๋งŽ์ด ํ™œ์šฉํ•˜๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ์†Œ๊ฐœํ•˜์˜€๋‹ค. ๋ถ„์„์˜ ์œ ํ˜•๊ณผ ํ™œ์šฉ ๋ฐฉ๋ฒ•, ์‚ฌ๋ก€ ๊ทธ๋ฆฌ๊ณ  ์ ์šฉ ๊ฐ€๋Šฅํ•œ ๊ทธ๋ž˜ํ”„ ํ˜•ํƒœ๋ฅผ ์ •๋ฆฌํ•œ ๊ฒƒ์œผ๋กœ ์ด๋ฅผ ์ˆ™์ง€ํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‹ค ์˜๋ฏธ ์žˆ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. Table II-20. ๋ณด๊ณ ์„œ์— ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๊ทธ๋ž˜ํ”„ ์ง€๊ธˆ๊นŒ์ง€ ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ , ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ธฐ๋ฒ•, ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ ๋“ฑ ๊ธฐ๋ณธ์ ์ธ ์ปจ์„คํŒ… ์Šคํ‚ฌ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์•˜๋‹ค. ์ปจ์„คํ„ดํŠธ ๊ฐœ์ธ์ด ์‚ฌ๊ณ ํ•˜๋Š” ๋ฐฉ๋ฒ•, ์ด์Šˆ๋‚˜ ๋ฌธ์ œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•, ๊ณ ๊ฐ์˜ ์ƒ๊ฐ์„ ๋“ฃ๊ณ , ๊ธ€๋กœ ์ ๊ณ , ๋งํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ปจ์„คํŒ… ์—…๋ฌด์˜ ์ „๋ถ€๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ํ›ˆ๋ จ๊ณผ ์—ฐ์Šต์ด ํ•„์š”ํ•˜๋ฉฐ ์ง€์†์ ์ธ ๋ฐ˜๋ณต์€ ๋ถ„๋ช…ํžˆ ์ปจ์„คํŒ… ์‹ค๋ ฅ์„ ํ–ฅ์ƒ์‹œ์ผœ์ค€๋‹ค. ์–ด๋–ค ๋กœ์ง ํŠธ๋ฆฌ๋“ค์€ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๊ทธ ํšจ์šฉ์„ฑ์„ ์ธ์ •๋ฐ›์•„ ๋„๊ตฌ๋‚˜ ๊ธฐ๋ฒ•์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•˜๊ณ  ์ง€์†์ ์œผ๋กœ ๋ฐœ์ „ํ•ด๋‚˜๊ฐ€๊ธฐ๋„ ํ•œ๋‹ค. Part III์—์„œ๋Š” ์ด๋Ÿฐ ์ปจ์„คํŒ… ์Šคํ‚ฌ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž. [1] ์—ฐ๋ด‰ ํ˜‘์ƒ์„ ์ด๋ ‡๊ฒŒ ํ•œ๋‹ค๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ํ”„๋ ˆ์  ํ…Œ์ด์…˜์—์„œ ๊ฐ ํฌ์ธํŠธ๋ฅผ ์ •๋Ÿ‰์  ์š”์†Œ์™€ ํ•จ๊ป˜ ๋ช…ํ™•ํžˆ ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ „๋‹ฌํ•˜๋Š” ๋‚ด์šฉ์ด๋‹ค. ์—ฐ๋ด‰ํ˜‘์ƒ ์Šคํ‚ฌ์€ ๊ด€๋ จ ์ „๋ฌธ๊ฐ€๋‚˜ ํ—ค๋“œํ—Œํ„ฐ์™€ ์ƒ๋‹ดํ•˜๊ธฐ๋ฅผ! [2] 1990๋…„๋Œ€์™€ 2000๋…„๋Œ€ ์ค‘๋ฐ˜๊นŒ์ง€์˜<NAME>ํ™ฉ์„ ์ด๋ˆ ์ฃผ์—ญ์ธ ๊ทธ๋ฆฌ์ŠคํŽ€(Alan Greenspan. 1926 ~ ํ˜„์žฌ)์ด ์ด๋ˆ ๋ฏธ๊ตญ์˜ ๊ฒฝ์ œ ํ˜ธํ™ฉ๊ธฐ๋ฅผ ๊ณจ๋””๋ฝ์Šค ์‹œ๋Œ€๋ผ๊ณ  ํ•จ. ์˜๋ฏธ๊ถŒ์—์„œ ๊ฝค ์œ ๋ช…ํ•œ '๊ณจ๋””๋ฝ์Šค์™€ ๊ณฐ ์„ธ ๋งˆ๋ฆฌ' ๋™ํ™”๊ฐ€ ์žˆ๋‹ค. '๊ณจ๋””๋ฝ์Šค'๋ผ๋Š” ์†Œ๋…€๊ฐ€ ์ˆฒ์† ํ†ต๋‚˜๋ฌด์ง‘์— ๋“ค์–ด๊ฐ”๋Š”๋ฐ ๊ณฐ ๊ฐ€์กฑ์ด ์ˆ˜ํ”„๋ฅผ ๋Œ ์—ฌ๋†“์•˜๊ณ  ๊ณจ๋””๋ฝ์Šค๊ฐ€ ์ด๊ฑธ ํฐ ์ ‘์‹œ, ์ค‘๊ฐ„ ์ ‘์‹œ, ์ž‘์€ ์ ‘์‹œ์— ๋‚˜๋ˆ„์–ด ๋‹ด๊ณ  ๋ง›์„ ๋ณด์•˜๋”๋‹ˆ ํ•˜๋‚˜๋Š” ๋„ˆ๋ฌด ๋œจ๊ฒ๊ณ , ํ•˜๋‚˜๋Š” ๋„ˆ๋ฌด ์ฐจ๊ฐ‘๊ณ  ์ค‘๊ฐ„ ์ ‘์‹œ ์ˆ˜ํ”„๊ฐ€ ๋”ฑ ๋จน๊ธฐ ์ข‹๋”๋ผ๋ผ๋Š” ๋‚ด์šฉ์˜ ๋™ํ™”์—์„œ ๋„ˆ๋ฌด ๋œจ๊ฒ์ง€๋„ ์ฐจ๊ฐ‘์ง€๋„ ์•Š์€ ์•ˆ์ •์ ์œผ๋กœ ์„ฑ์žฅํ•˜๋Š” ๊ฒฝ์ œ๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•จ. [3] www.ted.com ๊ธฐ์ˆ (Technology), ์˜ค๋ฝ(Entertainment), ๊ต์œก(Education)์„ ์ฃผ์ œ๋กœ ํฌ๋ฆฌ์Šค ์•ค๋”์Šจ(Chris Anderson. 1961 ~ ํ˜„์žฌ)์˜ ๊ธฐํš์œผ๋กœ ๋ฏธ๊ตญ์˜ ๋น„์˜๋ฆฌ์žฌ๋‹จ์ด ์šด์˜ํ•˜๋Š” ๊ฐ•์—ฐํšŒ. '์„ธ์ƒ์— ์•Œ๋ฆด ๊ฐ€์น˜ ์žˆ๋Š” ์•„์ด๋””์–ด(Ideas worth spreading)'๋ฅผ ๊ธฐ์น˜๋กœ ์ข‹์€ ์ฃผ์ œ๋ฅผ ๊ฐ€์ง„ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ฝ˜ํ…์ธ ๋ฅผ ์ธํ„ฐ๋„ท์„ ํ†ตํ•ด ๋ฌด๋ฃŒ๋กœ ๋ฐฐํฌํ•˜๊ณ  ์žˆ๋‹ค. ์˜์–ด ๊ณต๋ถ€ํ•˜๊ธฐ์—๋„ ์ข‹๋‹ค. [4] ์ผ์ • ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์ตœ๊ทผ์˜ ์ •๋ณด๊ฐ€ ๊ธฐ์กด์˜ ์ •๋ณด๋ฅผ ๋ฐ€์–ด๋‚ด๋Š” ํ˜„์ƒ 060 PART III. ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ• Part III์—์„œ๋Š” Part II๋ฅผ ํ†ตํ•ด ์Šต๋“ํ•œ ์ปจ์„คํŒ… ์Šคํ‚ฌ(Consulting Skills)์„ ์ข€ ๋” ๋ฐœ์ „์‹œํ‚จ ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•๋“ค์„ ๋‹ค๋ฃจ๊ณ ์ž ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์ปจ์„คํŒ…์ด ํ˜„ํ™ฉ์„ ๋ถ„์„ํ•˜์—ฌ ๋ฌธ์ œ์ ๊ณผ ๊ฐœ์„  ํฌ์ธํŠธ๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ณ  ๋น„์ „์ด๋‚˜ ์ „๋žต์˜ ๊ฐœ์„  ๋ฐฉํ–ฅ, ์ดํ–‰ ๊ณ„ํš์„ ์ œ์‹œํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์€ ๊ธฐ์—… ๋‚ด ์ „๋žต ์ˆ˜๋ฆฝ์ด๋‚˜ ๋งˆ์ผ€ํŒ…, ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ ๋“ฑ์—์„œ๋„ ๋‹ค์–‘ํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Figure III-1. ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ• ๋กœ๋“œ๋งต Figure III-1์€ Part III์—์„œ ๋‹ค๋ฃจ๊ณ ์ž ํ•˜๋Š” ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์˜ ๋กœ๋“œ๋งต(Roadmap)์œผ๋กœ ๊ธฐ์—…์ด ์†ํ•œ ์‚ฐ์—…๊ณผ ์‹œ์žฅ, ๊ฒฝ์Ÿ ํ™˜๊ฒฝ์„ ๋ถ„์„ํ•˜๊ณ  ๊ธฐ์—… ๊ณ ๊ฐ์ด๋‚˜ ์ผ๋ฐ˜ ์†Œ๋น„์ž์˜ Needs๋ฅผ ํŒŒ์•…ํ•˜๋ฉฐ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค์˜ ์ˆ˜์ต์„ฑ์„ ๊ฒ€ํ† ํ•˜๊ณ , ๊ธฐ์—… ๊ฒฝ์Ÿ๋ ฅ์˜ ํ† ๋Œ€๊ฐ€ ๋˜๋Š” ์—ญ๋Ÿ‰ ๋“ฑ์„ ๊ฒ€ํ† ํ•˜์—ฌ ์‹œ์‚ฌ์ ๊ณผ ํ–ฅํ›„ ๊ฐœ์„ ๋œ ๋ชจ์Šต์„ ์œ„ํ•œ ๋Œ€์•ˆ์ด๋‚˜ ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค„ ๊ฒƒ์ด๋‹ค. ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์˜ ํ™œ์šฉ๊ณผ ๊ด€๋ จํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์•ผ๊ธฐ๋“ค์ด ์žˆ๋‹ค. 1. ๋™์ผํ•œ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค. 2. ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์ด ๋งŒ๋ณ‘ํ†ต์น˜์•ฝ์€ ์•„๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฉ”์‹œ์ง€๋Š” ์ƒํ™ฉ์— ๋งž๋Š” ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•จ์„ ์˜๋ฏธํ•œ๋‹ค. ์ˆ˜๋งŽ์€ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์ด ์žˆ์ง€๋งŒ ๋ชจ๋‘ ๋‚˜๋ฆ„์˜ ๊ฐ€์ •(assumption)์ด ์žˆ์œผ๋ฉฐ ๊ทธ ๊ฐ€์ •์„ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ทธ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ, ์˜ˆ์ƒ๊ณผ ์ „ํ˜€ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฉ”์‹œ์ง€๋Š” ์ˆ˜๋งŽ์€ ์ปจ์„คํ„ดํŠธ, ๊ฒฝ์˜ํ•™์ž ๋“ฑ<NAME>๋“ค์ด ์ˆ˜ ์‹ญ ๋…„ ๊ฐ„ ์Œ“์•„์˜จ ์ง€์‹๊ณผ ๊ฒฝํ—˜์œผ๋กœ ์ •์ œ๋œ ๊ฒƒ์ด ์ด์ œ๋ถ€ํ„ฐ ์„ค๋ช…ํ•  ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•๋“ค์œผ๋กœ์จ ๋‚˜๋ฆ„์˜ ๋…ผ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์ž˜ ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์ด์ง€๋งŒ ์–ด๋–ค ๊ฒฝ์šฐ๋Š” ํˆฌ์ž…ํ•œ ๋…ธ๋ ฅ(Efforts) ๋Œ€๋น„ ๋งŒ์กฑ์Šค๋Ÿฝ์ง€ ๋ชปํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ์ €์ž์˜ ๊ฒฝ์šฐ, AHP[1]๊ฐ€ ๊ทธ๋Ÿฐ ๊ฒฝํ—˜์„ ์ฃผ์—ˆ๋‹ค. ์ „๋ฌธ๊ฐ€๋ฅผ ๋Œ€์ƒ์œผ๋กœ AHP๋ฅผ ์œ„ํ•œ ์„ค๋ฌธ๋„ ์ค€๋น„ํ•˜๊ณ  ์‹œ๊ฐ„์„ ๋“ค์—ฌ์„œ ์˜๊ฒฌ์„ ๋ฐ›์•„ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ๋Œ๋ ธ๊ณ  ๋ฐ”์œ ํ”„๋กœ์ ํŠธ ์ผ์ •์—์„œ ๋‹น์‹œ ๊ทธ ๋…ธ๋ ฅ์ด ์ƒ๋‹นํ–ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋…ธ๋ จํ•œ ๊ด€๋ จ ์—…๋ฌด ์ „๋ฌธ๊ฐ€๋“ค๊ณผ ์ธํ„ฐ๋ทฐ๋ฅผ ํ•˜์˜€๋Š”๋ฐ ๊ทธ ๋‚ด์šฉ์ด AHP ๊ฒฐ๊ณผ์™€ ํฌ๊ฒŒ ์ƒ์ดํ•˜์ง€ ์•Š์•„ ์—ญ์‹œ ์ „๋ฌธ๊ฐ€๋“ค์˜ ํ†ต์ฐฐ๋ ฅ(insight)์ด ๋” ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ตํ›ˆ์„ ์–ป๊ธฐ๋„ ํ•˜์˜€๋‹ค. Part II์—์„œ ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๋กœ์ง ํŠธ๋ฆฌ(Logic Tree)๋ฅผ ๋ฐฐ์› ๋‹ค. ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋กœ์ง ํŠธ๋ฆฌ๋Š” ์—…๋ฌด์™€ ๊ฒฐํ•ฉํ•˜์—ฌ MECE Pockets์ด๋ผ๋Š” ๊ฐ„๋‹จํ•œ ๋„๊ตฌ๋กœ ๋ฐœ์ „ํ•˜๊ธฐ๋„ ํ•˜์˜€๋‹ค. ๊ทธ๋Ÿฐ ๊ฒƒ๋“ค์ด ๋ถ„์„(Analysis)์˜ ๋„๊ตฌ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ๋‹ค์–‘ํ•œ ๋ถ„์„ ๋„๊ตฌ๋“ค์ด ํ•˜๋‚˜์˜ ์ฃผ์ œ์—์„œ ๊ฒฐํ•ฉ๋˜๋ฉด ๊ธฐ๋ฒ•(Analytics)์ด๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์–ด์ง€๋Š” Part IV์—์„œ ์ƒ์„ธํžˆ ๋‹ค๋ฃจ๊ฒ ์ง€๋งŒ ์ด๋Ÿฐ ๋‹ค์–‘ํ•œ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•๋“ค์ด ํ”„๋กœ์„ธ์Šค์™€ ์—ฐ๊ณ„ํ•˜์—ฌ ํ…œํ”Œ๋ฆฟ(Template)ํ™”๋˜๋ฉด ๊ทธ๊ฒƒ์„ ๋ฐฉ๋ฒ•๋ก (Methodology)์ด๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿฌํ•œ ์ปจ์„คํŒ… ์ˆ˜ํ–‰ ์ฒด๊ณ„๋ฅผ ์ตํžˆ๊ธฐ ์œ„ํ•ด ์ด์ œ๋ถ€ํ„ฐ ๋‹ค์–‘ํ•œ ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•๋“ค์— ๋Œ€ํ•ด ์ƒ์„ธํžˆ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. Break #10. MECE Pockets Part II์—์„œ ์‚ดํŽด๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ MECE ๊ฐœ๋…์€ ์ƒํ˜ธ ๊ฐ„์— ์ค‘๋ณต๋˜์ง€ ์•Š๊ณ  ์ „์ฒด๋กœ์„œ ๋ˆ„๋ฝ์ด ์—†๋Š” ์ƒํƒœ(Mutually Exclusive Collectively Exhaustive)๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ ํ˜„์‹ค ์„ธ๊ณ„์—์„œ MECE ํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ์€ ๊ฒฐ์ฝ” ์‰ฝ์ง€ ์•Š๋‹ค. ๊ทธ๋ž˜์„œ ๋“ฑ์žฅํ•œ ๊ฐœ๋…์ด LISS์ด๋‹ค. Figure III-2. MECE์™€ LISS์˜ ๊ฐœ๋… LISS๋Š” ์„ ํ˜•๋Œ€์ˆ˜ํ•™(Linear Algebra)์˜ ๊ฐœ๋…์ด๊ธฐ๋„ ํ•œ๋ฐ MECE๋˜ LISS๋˜ ๋‘ ๊ฐ€์ง€ ๋ชจ๋‘ ๋™์ผ ์ฐจ์›์—์„œ ์ค‘๋ณต์ด ์—†์–ด์•ผ ํ•จ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์œผ๋กœ MECE๋Š” ๋น ์ง„ ๊ฒƒ์ด ์—†๋Š”์ง€, LISS๋Š” ์ค‘์š” ๊ณผ์ œ๋ฅผ ๋ช…ํ™•ํžˆ ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ์ปจ์„คํŒ… ๋ถ„์„์—์„œ ์ด๋Ÿฐ MECE ๊ด€์ ๊ณผ LISS ๊ด€์ ์—์„œ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ๋“ค์„ 'MECE Pockets'์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ ๋‹ค์Œ ๋‚˜์—ด๋œ MECE Pockets๋“ค์€ ๊ธฐ์–ตํ•ด๋‘๋ฉด ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  ์ „๊ฐœ์— ๋งŽ์€ ๋„์›€์„ ์ค„ ๊ฒƒ์ด๋‹ค. 1. ์‚ฌ์—… ํ™˜๊ฒฝ๋ถ„์„(PEST): ์ •์น˜์ (Political), ๊ฒฝ์ œ์ (Economic), ์‚ฌํšŒ๋ฌธํ™”์ (Sociocultural), ๊ธฐ์ˆ ์  (Technological) 2. ์‚ฌ์—… ํ™˜๊ฒฝ๋ถ„์„(3C): ๊ณ ๊ฐ(Customer), ๊ฒฝ์Ÿ์‚ฌ(Competitors), ์ž์‚ฌ(Corporate) 3. ์ „๋žต ์ˆ˜๋ฆฝ SWOT: ๊ฐ•์ (Strength), ์•ฝ์ (Weakness), ๊ธฐํšŒ(Opportunity), ์œ„ํ˜‘(Threats) 4. ์ „๋žต ์ˆ˜๋ฆฝ 7S: Strategy, Structure, System, Staff, Skill, Shared Value, Style 5. ์ „๋žต ์ˆ˜๋ฆฝ ๋น„์ฆˆ๋‹ˆ์Šค ์‹œ์Šคํ…œ: ์—ฐ๊ตฌ๊ฐœ๋ฐœ, ์ƒ์‚ฐ, ์œ ํ†ต, ํŒ๋งค, ์„œ๋น„์Šค 6. ์ „๋žต ์ˆ˜๋ฆฝ SPOT: Strategy, People, Organization, Technology 7. ์ „๋žต ์ˆ˜๋ฆฝ STAR: Strategy, People, Process, Rewards, Structure 8. ๊ฒฝ์˜์ง„๋‹จ PPT: People, Process, Technology 9. ๋งˆ์ผ€ํŒ… 4P Mix: ์ œํ’ˆ(Product), ๊ฐ€๊ฒฉ(Price), ์œ ํ†ต(Place), ํ™๋ณด(Promotion) 10. ๋งˆ์ผ€ํŒ… 4C: ์†Œ๋น„์ž(Consumer), ๋น„์šฉ(Cost), ํŽธ์˜์„ฑ(Convenience), ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜(Communication) 11. ๋””์ง€ํ„ธ ๋น„์ฆˆ๋‹ˆ์Šค 4C: Contents, Community, Communication, Commerce 12. ์—ญํ•  ํ–‰๋™ CARE: Capability, Authority, Responsibility, Evaluation 13. ๊ฒฝ์Ÿ ๋ถ„์„ 5 ํฌ์Šค: ๊ฒฝ์Ÿ์ž, ๊ณต๊ธ‰์ž, ๊ตฌ๋งค์ž, ๋Œ€์ฒด์žฌ, ์‹ ๊ทœ ์ง„์ž…์ž 14. ํ”„๋กœ์„ธ์Šค ์š”๊ฑด: ๊ธฐ์ค€, ์ ˆ์ฐจ, ์ง€์‹/์Šคํ‚ฌ, ์žฅ๋น„/๋„๊ตฌ 15. ํ”„๋กœ์„ธ์Šค์˜ ๋‹จ๊ณ„ IPO: Input, Process, Output 16. ํ˜„์žฅ Trouble 4M: Man, Machine, Material, Method 17. ์ œํ’ˆ ๊ฒฝ์Ÿ์˜ 3์š”์†Œ QCD: Quality, Cost, Delivery [1] Analytic Hierarchy Process 07. ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—…๋ถ„์„(1/4) ์ปจ์„คํŒ…์—์„œ ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„์€ ์‚ฌ์—… ํ™˜๊ฒฝ ๋ฐ ๋ฐฐ๊ฒฝ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜ํ–‰ํ•˜๋Š” ์ค‘์š”ํ•œ ๋ถ„์„์ด๋‹ค. ํฌ๊ฒŒ ๊ฑฐ์‹œํ™˜๊ฒฝ๋ถ„์„ ์ฆ‰, ๋ฉ”๊ฐ€ํŠธ๋ ŒํŠธ(megatrend) ๋ถ„์„์„ ํ†ตํ•ด ๊ฑฐ์‹œ ๊ด€์ ์—์„œ ๊ธฐ์—…์—๊ฒŒ ์˜ํ–ฅ์„ ์ฃผ๋Š” ์ •์ฑ…, ๊ฒฝ์ œ๋™ํ–ฅ ๋ฐ ๊ธฐ์ˆ  ํŠธ๋ Œ๋“œ ๋“ฑ์„ ์กฐ๋งํ•˜๊ณ , 3C๋ผ๊ณ  ํ•˜์—ฌ ๊ณ ๊ฐ(Customer), ๊ฒฝ์Ÿ(Competitors), ์ž์‚ฌ(Corporate) ๊ด€์ ์—์„œ ์‹œ์‚ฌ์ ์„ ๋ถ„์„ํ•œ๋‹ค. ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์˜ ๊ด€์ ์—์„œ ๋ณด๋ฉด ์‹œ์žฅ์˜ ๊ทœ๋ชจ(Size)์™€ ์„ฑ์žฅ๋ฅ (Growth)์„ ํŒŒ์•…ํ•˜๋Š” ์‹œ์žฅ ๋ถ„์„, ๊ฒฝ์Ÿ์‚ฌ ํ”„๋กœํŒŒ์ผ๋ง(profiling) ๋ฐ ํฌ์ง€์…”๋‹(positioning)์„ ํ†ตํ•ด ๊ฒฝ์Ÿ์‚ฌ์˜ ํ˜„ํ™ฉ์„ ํŒŒ์•…ํ•˜๋Š” ๊ฒฝ์Ÿ ๋ถ„์„, ๊ธฐ์—… ๋‚ด ๊ฐ€์น˜์‚ฌ์Šฌ ๋ถ„์„๊ณผ ์žฌ๋ฌด๋น„์œจ ๋ถ„์„์„ ํ†ตํ•ด ๊ธฐ์—…์˜ ํ˜„์žฌ ๋ชจ์Šต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์˜ ๊ด€์ ์—์„œ ๋ถ„์„ ๊ฒฐ๊ณผ ๋„์ถœ๋˜๋Š” ์ •๋Ÿ‰์ /์ •์„ฑ์  ๋ฉ”์‹œ์ง€๋ฅผ ์ž˜ ์ •๋ฆฌํ•˜์—ฌ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋ฉฐ ํŠนํžˆ, ํ•ต์‹ฌ ๋ถ„์„(Core Analytics)์€ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ์—์„œ ๋ฐ˜๋“œ์‹œ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. Figure III-3์€ ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„์˜ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•๋“ค์ด ์‚ฌ์—… ํ™˜๊ฒฝ ๋ถ„์„์—์„œ ์–ด๋–ป๊ฒŒ ํ™œ์šฉ๋˜๊ณ  ์žˆ๋Š”์ง€ ๋ณด์—ฌ์ค€๋‹ค. Figure III-3. ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„ ๋กœ๋“œ๋งต 7.1 PEST ๋ถ„์„ ์‹œ์žฅ์ด๋‚˜ ์‚ฐ์—…์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜ํ–‰ํ•˜๋Š” ์™ธ๋ถ€ํ™˜๊ฒฝ ๋ถ„์„์€ ๊ฑฐ์‹œ์ (Macroscopic) ๊ด€์ ์—์„œ ์‹œ์žฅ ํ™˜๊ฒฝ์„ ์กฐ๋งํ•˜๋Š” ๊ฒƒ ์ฆ‰, ๋ฉ”๊ฐ€ํŠธ๋ Œ๋“œ(megatrend)๋ฅผ ๋‹ค๋ฃจ๊ฒŒ ๋˜๋Š”๋ฐ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ถ„์„ ๊ธฐ๋ฒ•์ด โ€˜PEST ๋ถ„์„โ€™์ด๋‹ค. PEST ๋ถ„์„์ด๋ž€ ์™ธ๋ถ€ ํ™˜๊ฒฝ์„ ์ •์น˜, ๊ฒฝ์ œ, ์‚ฌํšŒ๋ฌธํ™”, ๊ธฐ์ˆ  ๊ด€์ ์—์„œ ์กฐ๋งํ•˜๊ณ  ์‹œ์‚ฌ์ ์„ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์œผ๋กœ PEST๋Š” Table III-1๊ณผ ๊ฐ™์€ ๊ฐœ๋…์„ ๋‚ดํฌํ•œ๋‹ค. Table III-1. PEST์˜ ๊ฐœ๋… PEST ๋ถ„์„์€ ๋ฉ”๊ฐ€ํŠธ๋ Œ๋“œ ๋ถ„์„์„ ์œ„ํ•œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์งˆ๋ฌธ๋“ค์— ๋Œ€ํ•œ ๋‹ต์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ๊ตญ์˜ ์ •๋ถ€๊ฐ€ ์ฃผ์žฅํ•˜๋Š” ์ •์ฑ… ๊ธฐ์กฐ๋Š” ๋ฌด์—‡์ธ๊ฐ€? ๊ณตํ†ต์ ์ด ์žˆ๋Š”๊ฐ€? ๋ณ€ํ™”๋ฅผ<NAME>๋Š” ํŠธ๋ Œ๋“œ๋Š” ๋ฌด์—‡์ธ๊ฐ€? ๋ณ€ํ™”์˜ ์†๋„๋Š” ์–ผ๋งˆ๋‚˜ ๋น ๋ฅธ๊ฐ€? ํŠธ๋ Œ๋“œ๊ฐ€ ๊ธฐ์กด ์‚ฌ์—…์— ๋ผ์น˜๋Š” ์˜ํ–ฅ์€ ๋ฌด์—‡์ธ๊ฐ€? ์ƒˆ๋กญ๊ฒŒ ๋˜๋Š” ๊ธฐ์กด ์‚ฌ์—…์— ์ถ”๊ฐ€์ ์ธ ๊ธฐํšŒ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์žˆ๋Š”๊ฐ€? ํ˜„์žฌ์™€ ํ–ฅํ›„์˜ ํ•ต์‹ฌ๊ธฐ์ˆ ์€ ๋ฌด์—‡์ธ๊ฐ€? ๊ธฐ์ˆ  ๋กœ๋“œ๋งต[1]์€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ™”ํ•˜๊ณ  ์žˆ๋Š”๊ฐ€? ๋˜ํ•œ, PEST ๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฅ์ ๊ณผ ๋‹จ์ ์ด ์žˆ๋‹ค. Table III-2. PEST ๋ถ„์„์˜ ์žฅ์ ๊ณผ ๋‹จ์  Table III-3๊ณผ Figure III-4๋Š” PEST ๋ถ„์„ ์‚ฌ๋ก€์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ปจ์„คํŒ… PM์ด Table III-3๊ณผ ๊ฐ™์ด ๋ถ„์„ ๊ฐ€์ด๋“œ๋ฅผ ์ฃผ๋ฉด, ์ปจ์„คํ„ดํŠธ๋“ค์ด ๋ถ„์„ ์ž๋ฃŒ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ๋‚ด์šฉ์„ ๊ฒ€ํ† ํ•˜์—ฌ Figure III-4์™€ ๊ฐ™์€ ๊ด€์ ๋ณ„ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹œ์‚ฌ์ ์„ ๋„์ถœํ•œ๋‹ค. Figure III-4. PEST ๋ถ„์„ ๊ฐ€์ด๋“œ ๋ฐ ์‚ฌ๋ก€ PEST ๋ถ„์„ - ์ •์น˜์  ๊ด€์ (Political perspective) PEST ๋ถ„์„ - ๊ฒฝ์ œ์  ๊ด€์ (Economic perspective) PEST ๋ถ„์„ - ์‚ฌํšŒ๋ฌธํ™”์  ๊ด€์ (Sociocultural perspective) PEST ๋ถ„์„ - ๊ธฐ์ˆ ์  ๊ด€์ (Technological perspective) PEST ๋ถ„์„์€ ๊ฑฐ์‹œํ™˜๊ฒฝ๋ถ„์„ ํ”„๋ ˆ์ž„์œผ๋กœ ์œ ์šฉํ•˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ „๋žต๊ธฐํš์ด๋‚˜ ์ปจ์„คํŒ…์—์„œ ํ™˜๊ฒฝ๋ถ„์„ ์ž‘์—…์ฒ˜๋Ÿผ ์• ๋งคํ•œ ์ž‘์—…์ด ์—†๋‹ค. ๋งŽ์€ ๋…ธ๋ ฅ์„ ํˆฌ์ž…ํ–ˆ๋Š”๋ฐ ์‹œ์‚ฌ์ ์ด ๋‚ด๊ฐ€ ์˜๋„ํ•˜๋˜ ๊ฒƒ๊ณผ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๊ฐ€ ํ—ˆ๋‹คํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ๋ถ„์„ ๋Œ€์ƒ์ด๋‚˜ ๋‚ด์šฉ์ด ๋ฐœ์‚ฐํ•˜์ง€ ์•Š๋„๋ก ๊ฐ€์„ค์„ ์ž˜ ์ˆ˜๋ฆฝํ•˜๊ณ  ๋น„์šฉ ํšจ์œจ์ ์œผ๋กœ ์ผํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋‹ค์Œ์€ ๊ฑฐ์‹œํ™˜๊ฒฝ ๋ถ„์„์— ์ฐธ๊ณ ํ•  ๋งŒ ์‚ฌ์ดํŠธ์ด๋‹ค. ๋žœ๋“œ์—ฐ๊ตฌ์†Œ(The RANDCorporation. www.rand.org) ์ „๋ฏธ๊ฒฝ์ œ์กฐ์‚ฌ๊ตญ(NBER. www.nber.org) ํ•œ๊ตญ์€ํ–‰(www.bok.or.kr) ๊ธฐํš์žฌ์ •๋ถ€(www.mosf.go.kr) 1.2 ์‹œ์žฅ ๋ถ„์„ ์‹œ์žฅ์˜ ํฌ๊ธฐ(size) ๋ฐ ์ ์œ ์œจ(growth) ๋ถ„์„์ด๋ž€ ์‹œ์žฅ/์„ธ๊ทธ๋จผํŠธ(segment) ํฌ๊ธฐ์™€ ์„ฑ์žฅ์„ ์ •๋Ÿ‰ํ™”ํ•˜๋Š” ๊ฒƒ์œผ๋กœ Value ์ธก๋ฉด์—์„œ ํŠน์ • ๊ธฐ๊ฐ„ ๋™์•ˆ์˜ ๋งค์ถœ(Revenue)๊ณผ ์ˆ˜์ต์„ฑ(Profitability)์œผ๋กœ ํ‘œํ˜„ํ•˜๊ฑฐ๋‚˜ Volume ์ธก๋ฉด์—์„œ ํŠน์ • ๊ธฐ๊ฐ„ ๋™์•ˆ ์ƒ์‚ฐ๋œ ๋‹จ์œ„ ์ƒ์‚ฐ๋Ÿ‰์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…์—์„œ ์‹œ์žฅ์˜ ๋ณ€ํ™”ํ•˜๋Š” ๋ชจ์Šต์„ ํŒŒ์•…ํ•˜๊ณ  ์ด๋ฅผ ์ •๋ฆฌํ•˜์—ฌ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ค‘์š”ํ•œ๋ฐ ์‹œ์žฅ์˜ ํฌ๊ธฐ์™€ ์ ์œ ์œจ ๋“ฑ์ด ๋™์ ์œผ๋กœ ๋ณ€ํ™”ํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ๋ ค์ฃผ๊ณ , ์ˆ˜๋…„ ๋™์•ˆ์˜ ์‚ฌ์—…์ด๋‚˜ ์‹œ์žฅ ์„ธ๊ทธ๋จผํŠธ์˜ ๋ณ€ํ™” ๋‚ด์—ญ์„ ํ‘œํ˜„ํ•œ๋‹ค. ์ฆ‰, ๊ณผ๊ฑฐ ์ˆ˜ ๋…„์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ฃผ๊ณ (Historically), ๋ฏธ๋ž˜ ์ˆ˜ ๋…„์˜ ๋ชจ์Šต์„ ์ถ”์ •ํ•œ๋‹ค(Forecasting). ๊ธฐ์—… ์ž…์žฅ์—์„œ๋Š” ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ์–ผ๋งˆ๋‚˜ ์ ์œ ํ•˜๊ณ  ์žˆ๋Š๋ƒ๋Š” ๊ฒฝ์Ÿ์˜ ํŒ๋„๋ฅผ ํŒŒ์•…ํ•˜๋Š”๋ฐ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ์‹œ์žฅ์ ์œ ์œจ(Market Share)์ด๋ผ๋Š” ์ธก์ •์„ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•œ๋‹ค[2]. ์‹œ์žฅ ์ ์œ ์œจ์€ Value ์ธก๋ฉด์—์„œ ์ˆœ ๋งค์ถœ์ด๋‚˜ ์ˆœ์ด์ต์˜ ๋น„์œจ์„ ํ‰๊ฐ€ํ•˜๋ฉฐ Volume ์ธก๋ฉด์—์„œ ๋‹จ์œ„๋‹น ์ƒ์‚ฐ๋Ÿ‰์˜ ๋น„์œจ๋กœ ํ‰๊ฐ€ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์‚ฌ์—…์˜ ๊ฒฝ์Ÿ๋ ฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์ ˆ๋Œ€์ (Absolute) ํ˜น์€ ์ƒ๋Œ€์ (Relative) ์‹œ์žฅ ์ ์œ ์œจ์„ ์ธก์ •ํ•˜๊ณ ์ž ํ•œ๋‹ค. ์ด๋Š” ์‚ฌ์—…(ํ˜น์€ ์„ธ๊ทธ๋จผํŠธ)์˜ ๋งค๋ ฅ๋„(Attractiveness)๋ฅผ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•จ์ธ๋ฐ ์‚ฌ์—…์˜ ํฌ๊ธฐ(ํ˜น์€ ์„ธ๊ทธ๋จผํŠธ์˜ ํฌ๊ธฐ)๊ฐ€ ํด์ˆ˜๋ก ๋งค๋ ฅ์ ์ด๋ฉฐ, ์„ฑ์žฅ ์†๋„๋‚˜ ๊ธฐ์šธ๊ธฐ ๋“ฑ์€ ์‹œ์žฅ์˜ ์งˆ(Quality)์„ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ์žฃ๋Œ€๊ฐ€ ๋œ๋‹ค. ๋˜ํ•œ, RMS[3]๋“ฑ ๋‹ค๋ฅธ ๋ถ„์„ ๊ธฐ๋ฒ•๋“ค๊ณผ ๊ฒฐํ•ฉํ•˜์—ฌ ๊ฒฝ์Ÿ ์šฐ์œ„[4]๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ์ข‹์€ ๋„๊ตฌ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ์‹œ์žฅ๋ถ„์„ ๊ธฐ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฅ์ ๊ณผ ๋‹จ์ ์ด ์žˆ๋‹ค. Table III-4. ์‹œ์žฅ ๋ถ„์„์˜ ์žฅ๋‹จ์  ์ด์™€ ๊ฐ™์€ ์‹œ์žฅ ๋ถ„์„์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ์ž‘์—…์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ์ฒซ ๋ฒˆ์งธ, ๊ณต๊ฐœ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ๋ถ„์„ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ, ๊ฐ ๊ธฐ์—… ๊ณ ์œ ์˜ ์‹œ์žฅ๊ณผ ์‚ฌ์—…์˜ ํฌ๊ธฐ๋ฅผ ์‚ฐ์ •, ๋น„๊ตํ•˜๋Š” ์ผ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ, ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ์‚ฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฅธ๋‹ค. 1. ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ์ •ํ•˜๋Š” ๋ฐ์ดํ„ฐ ์›์ฒœ(Data Source)๋ฅผ ์ดํ•ดํ•œ๋‹ค. 2. ๋ฐ์ดํ„ฐ ์›์ฒœ์˜ ์‹ ๋ขฐ์„ฑ์„ ๊ฒ€์ฆํ•œ๋‹ค. 3. 2 ~ 3ํšŒ ์ƒํ˜ธ ๊ฒ€์ฆํ•˜์—ฌ ๋ณธ๋‹ค. 4. ์‹œ์žฅ ํฌ๊ธฐ์˜ ํŠธ๋ Œ๋“œ๋ฅผ ์ดํ•ดํ•œ๋‹ค. ํ•ต์‹ฌ ์›์ธ๋„ ๊ฐ™์ด ํŒŒ์•…ํ•œ๋‹ค 5. ๊ณ ๊ฐ ๋˜๋Š” ํ•ด๋‹น ์‚ฐ์—… ์ „๋ฌธ๊ฐ€๋“ค๊ณผ ๊ฐ™์ด ๊ฒ€์ฆํ•œ๋‹ค ์ด ์ค‘ ๋ฐ์ดํ„ฐ ์›์ฒœ์˜ ์‹ ๋ขฐ์„ฑ์„ ๊ฒ€์ฆํ•˜๋Š” ์ผ์ด ๊ฐ€์žฅ ์ค‘์š”ํ•œ๋ฐ, ๋งŽ์€ ์‹œ์žฅ์กฐ์‚ฌ์—…์ฒด๋“ค์ด ์ž์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ํ™•๋ณดํ•˜๊ธฐ ์œ„ํ•ด ์ทจํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น ์‚ฐ์—…์— ์†ํ•œ ๊ธฐ์—…๋“ค์„ ๋ฐฉ๋ฌธํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณต๋ฐ›๋Š” ๊ฒƒ์ด๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ๋Œ€๋ถ€๋ถ„ ๊ธฐ์—…์˜ ์‹ค์ ๊ณผ ๊ด€๊ณ„๋˜๋ฏ€๋กœ ๊ฐ ๊ธฐ์—…๋“ค์€ ๋ฒ•์ ์œผ๋กœ ๋˜๋Š” ์˜๋ฌด์ ์œผ๋กœ ์‹œ์žฅ์กฐ์‚ฌ์—…์ฒด๋“ค์—๊ฒŒ ๊ทธ๋Ÿฐ ์ค‘์š” ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ถ€ํ’€๋ ค์„œ ์ œ๊ณตํ•˜๊ธฐ ์‰ฌ์šฐ๋ฉฐ ์‹œ์žฅ์กฐ์‚ฌ์—…์ฒด๋„ ์ด๋ฅผ 100% ์‹ ๋ขฐํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜๋ฆ„์˜ ๊ฐ€์ค‘์น˜๋กœ ๋ณด์ •ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ๊ฐ ๊ธฐ์—… ๊ณ ์œ ์˜ ์‹œ์žฅ๊ณผ ์‚ฌ์—…์˜ ํฌ๊ธฐ๋ฅผ ์‚ฐ์ •ํ•˜๋Š” ๊ฒƒ์€ ๋‹ค์Œ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฅธ๋‹ค. 1. ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ์ •ํ•˜๋Š” ์ฃผ์š” ์š”์ธ๋“ค(drivers)์„ ์ธ์ง€ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ์ „์ฒด ์‚ฌ์—… ๋˜๋Š” ์„ธ๊ทธ๋จผํƒœ ๋‚ด์˜ ๊ณ ๊ฐ์ˆ˜, ์ œํ’ˆ ๋˜๋Š” ์„œ๋น„์Šค ๋‹จ์œ„์˜ ๊ฐœ์ˆ˜, ๊ณ ๊ฐ ์ธ๋‹น ํ‰๊ท  ๊ตฌ์ž… ํ•˜๋Š” ์ œํ’ˆ ๋˜๋Š” ์„œ๋น„์Šค ๊ฐœ์ˆ˜ ๋“ฑ 2. ๋ฏธ๋ž˜ ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์ฃผ์š” ์š”์ธ๋“ค(drivers)์„ ์ธ์ง€ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ๊ด€๋ จ ๊ฑฐ์‹œ๊ฒฝ์ œ ๋™ํ–ฅ, ๊ณ ๊ฐ ์ˆ˜์š”์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ์š”์ธ๋“ค์˜ ๋ณ€ํ™”, ๊ณ ๊ฐ ์ˆ˜์˜ ์ฆ๊ฐ, ์ธ๋‹น ๊ตฌ๋งค๋Ÿ‰ ์˜ ๋ณ€ํ™” ๋“ฑ 3. ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๋ก ์„ ์„ ์ •ํ•˜๋Š”๋ฐ, โ€˜Top-down ๋ฐฉ์‹โ€™๊ณผ โ€˜Bottom-up ๋ฐฉ์‹โ€™์ด ์žˆ๋‹ค. Top-down ๋ฐฉ์‹์€ ๊ฑฐ์‹œ ๋ณ€์ˆ˜(Macro variables) ์ฆ‰, ์œ ๊ด€ ์‚ฌ์—…์˜ ๊ทœ๋ชจ๋ฅผ ํ•ฉํ•˜์—ฌ ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ์ถ”์ • ํ•˜๋Š” ๋ฐฉ์‹์ด๊ณ  Bottom-up ๋ฐฉ์‹์€ ๋ฏธ์‹œ ๋ณ€์ˆ˜(Micro variables) ์ฆ‰, ๊ณ ๊ฐ์˜ ๊ฐœ์ˆ˜๋กœ๋ถ€ํ„ฐ ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ์ถ”์ •ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ๊ทธ ์™ธ ์ƒํ’ˆ ๋ฐ ์„œ๋น„์Šค์˜ ๊ฐœ์ˆ˜, ๊ณ ๊ฐ๋‹น ํ‰๊ท  ๊ตฌ๋งค๋‹จ๊ฐ€ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์‹œ์žฅ์˜ ๊ทœ๋ชจ๋ฅผ ํŒŒ์•… ํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ๋ฐฉ๋ฒ•๋ก  ์ฐจ์›์—์„œ Top-Down ๋˜๋Š” Bottom-Up ์ด์ง€ ์‹ค์ œ ์—…๋ฌด์—์„œ๋Š” 2๊ฐ€์ง€ ๋ฐฉ์‹์„ ๋ชจ๋‘ ์ ์šฉํ•˜์—ฌ ์ถ”์ •๋œ ์‹œ์žฅ ๊ทœ๋ชจ์˜ ์ •ํ™•์„ฑ์„ ๋ณด์™„ํ•œ๋‹ค. 4. ์ด์ œ ์‹œ๊ฐ„ ๋ณ€์ˆ˜๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์‹œ์žฅ์˜ ํฌ๊ธฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ™”ํ–ˆ๋Š”์ง€ ๊ณ„์‚ฐํ•œ๋‹ค. ๊ฐ€์žฅ ๋งŽ์ด ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์—ฐํ‰๊ท  ์„ฑ์žฅ๋ฅ (CompoundAnnual Growth Rate: CAGR)์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. GAGR์˜ ๊ฐœ๋…์€ ๋งค๋…„ ์„ฑ์žฅ๋ฅ ์€ ๋“ค์‘ฅ๋‚ ์‘ฅํ•˜๊ฒ ์ง€๋งŒ ์˜ค๋žœ ๊ธฐ๊ฐ„์„ ๋‘๊ณ  ์ผ์ •ํ•œ ๋น„์œจ๋กœ ์„ฑ์žฅํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์–ผ๋งˆ๋‚˜ ์„ฑ์žฅํ•˜์˜€๋Š”๊ฐ€ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด โ€˜์ž๋Œ€๊ณ  ์ค„๊ธ‹๊ธฐโ€™์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 2010๋…„ 1,500์–ต ์›, 2015๋…„ 2,100์–ต ์›์˜ ๋งค์ถœ์„ ์˜ฌ๋ฆฐ ๊ธฐ์—…์ด 5๋…„ ๋™์•ˆ ์–ผ๋งˆ๋‚˜ ์„ฑ์žฅํ–ˆ๋Š”์ง€ CAGR์„ ๊ตฌํ•ด๋ณธ๋‹ค๋ฉด ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋‹ค. 1500 *(1+CAGR)^5 = 2100; ์ด ์‹์„ CAGR ๊ด€์ ์—์„œ ํ’€์–ด๋ณด๋ฉด CAGR = (2100/1500)^(1/5) -1 ์ด๋ฅผ ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ์—‘์…€์˜ rate ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณด๋‹ค ์‰ฝ๊ฒŒ ๊ณ„์‚ฐ ๊ฐ€๋Šฅํ•œ๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค[5]. CAGR = rate(๊ธฐ๊ฐ„, 0, -์ฒซํ•ด ๊ฐ’, ๋งˆ์ง€๋ง‰ ํ•ด ๊ฐ’) = rate(5,0, -1500,2100) 5. ๊ทธ ์™ธ ๊ณ ๋ คํ•  ๋งŒํ•œ ๊ฒƒ์€ ๋ฌผ๊ฐ€์ง€์ˆ˜(CPI)[6] ๋“ฑ์ด๋‹ค. ๋ณด์ •์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ ๋ฐ˜์˜ํ•˜๋ฉด ๋œ๋‹ค. ๋‹ค์Œ์€ ์‹œ์žฅ ๋ถ„์„์„ ์œ„ํ•ด ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ์งˆ๋ฌธ๋“ค์ด๋‹ค. ๊ธฐ์—…์ด ์†ํ•œ ์‹œ์žฅ์˜ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ์™€ ํŠน์ง•์€ ๋ฌด์—‡์ธ๊ฐ€? ์˜ˆ. ๊ธฐ์—… ์ˆ˜, ๊ณ ๊ฐ์ˆ˜ ์‚ฐ์—…์— ์†ํ•œ ๊ฐ ๊ธฐ์—…๋“ค์˜ ๋งค์ถœ์•ก ๋ฐ ๊ทธ ์ข…ํ•ฉ์€ ์–ผ๋งˆ์ธ๊ฐ€? ์ œํ’ˆ์— ๋Œ€ํ•œ ๊ฐ ๊ธฐ์—…๋“ค์˜ ํ‰๊ท  ๊ตฌ๋งค๋‹จ๊ฐ€๋Š” ์–ผ๋งˆ์ธ๊ฐ€? ์˜ˆ. IT ํˆฌ์ž๋น„, ์ œํ’ˆ ๊ตฌ์ž…๋น„ ์‚ฐ์ •๋œ ์‹œ์žฅ์€ ์–ด๋–ป๊ฒŒ ์„ธ๋ถ„ํ™”ํ•  ๊ฒƒ์ธ๊ฐ€? ์˜ˆ. ๊ณ ๊ฐ๋ณ„(By Customer), ์ง€์—ญ๋ณ„(By Regions), ์ œํ’ˆ๋ณ„(By Products) Figure III-5๋Š” ๋ฏธ๊ตญ ์—๋„ˆ์ง€๋ถ€(DOE)์—์„œ ์ œ๊ณตํ•˜๋Š” ๋งค๋…„ ๊ตฌ์ถ•๋˜๋Š” ๊ธฐ์ €์ „๋ ฅ, ๋ถ„์‚ฐ๋ฐฐ ์ „ ํ˜„ํ™ฉ, ์‚ฐ์—…์ด๋‚˜ ์ƒ์—…์‹œ์„ค์˜ ์ „๋ ฅ ๊ตฌ๋งค๋Ÿ‰ ๋“ฑ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์‚ฐ์ •ํ•œ ๋ถ๋ฏธ ์ „๋ ฅ ์‹œ์žฅ์˜ ๊ทœ๋ชจ ์‚ฐ์ • ์‚ฌ๋ก€์ด๋‹ค. Figure III-5. ์‹œ์žฅ ๊ทœ๋ชจ์˜ ์‚ฐ์ • ์‚ฌ๋ก€ - ๋ถ๋ฏธ ์ „๋ ฅ์‚ฌ์—… ์˜ˆ์‹œ Break #11. ์‹œ์žฅ์˜ ์—ญ๋™์„ฑ ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…์—์„œ ์‹œ์žฅ์˜ ์—ญ๋™์„ฑ์„ ์ธ์ •ํ•˜๊ณ  ์ด๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์€ ๋Œ€๋‹จํžˆ ์ค‘์š”ํ•˜๋‹ค. ๋งŽ์€ ๋ถ„์„ ๊ธฐ๋ฒ•๋“ค์ด โ€˜์˜ˆ์˜๊ณ  ๋ฉ‹์ง€๊ฒŒโ€™ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ฆฌํ•ด๋‚ผ์ง€๋Š” ๋ชฐ๋ผ๋„ ์ˆœ๊ฐ„์˜ ๋‹จ๋ฉด(snapshot)์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์— ๊ทธ์น˜๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ ์‹ค์‹œ๊ฐ„ ๋ณ€ํ™”๋ฅผ ๊ฒช์œผ๋ฉฐ ์‚ฌ์—… ์ถ”์„ธ๋ฅผ ์‚ดํŽด์•ผ ํ•˜๋Š” ๊ฒฝ์˜์˜ ํ˜„์žฅ์—์„œ๋Š” ๋…ธ๋ ฅ ๋Œ€๋น„ ๊ทธ๋ฆฌ ํฐ ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ์‚ฐ์ •ํ•˜๋Š” ์ผ๋„ ์ด์™€ ๋น„์Šทํ•œ๋ฐ, ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ์‚ฐ์ •ํ•˜๊ณ  ๊ทธ ๋ณ€ํ™”๋ฅผ ํŒŒ์•…ํ•˜๋Š” ์ผ์€ ์ˆ˜์ต์˜ ์›์ฒœ์ด ๋˜๋Š” ์—…(ๆฅญ)์˜ ๊ธฐ๋ณธ ํŒ์„ ์•Œ๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ •๋ง ์ค‘์š”ํ•˜์ง€๋งŒ, ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๊ธฐ๊ฐ€ ๋งค์šฐ ์–ด๋ ต๋‹ค. ์ „๋ฌธ์‹œ์žฅ์กฐ์‚ฌ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฒฐ๊ตญ ๋ฆฌ์„œ์น˜ ํšŒ์‚ฌ๋“ค์ด ์ž์‚ฌ๋ฅผ ํฌํ•จํ•ด ๊ฒฝ์Ÿ์‚ฌ ๋“ฑ ํ•ด๋‹น ์‚ฐ์—…์˜ ์ •๋ณด๋ฅผ ์ทจํ•ฉํ•˜์—ฌ ์‚ฐ์ •ํ•ด ๋‚ด๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ์ˆ™๋œ ์‚ฐ์—…์˜ ๊ฒฝ์šฐ, ์—…๊ณ„ ๋ชจ์ž„์—์„œ ๊ณต์œ ๋˜๋Š” ๋ฐ์ดํ„ฐ์™€ ๋ณ„ ์ฐจ์ด๊ฐ€ ๋‚˜์ง€ ์•Š๋Š”๋‹ค(๋‹ค๋งŒ, ์ด ๊ฒฝ์šฐ๋Š” ๊ฒฝ์Ÿ์‚ฌ์™€์˜ ์ •๋ณด ๊ตํ™˜์ด ๋‹ดํ•ฉ์˜ ์˜คํ•ด๋ฅผ ์‚ด ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์˜ํ•ด์•ผ ํ•œ๋‹ค). ๋˜ํ•œ, ์‹œ์žฅ์˜ ๋งค๋ ฅ๋„(attractiveness)๋ฅผ ๊ตฌํ•  ๋•Œ ๋‹จ์ˆœํžˆ ์‹œ์žฅ์˜ ํฌ๊ธฐ์™€ ์ ์œ ์œจ๋งŒ์œผ๋กœ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ์ง€๊ทนํžˆ ์œ„ํ—˜ํ•œ ์ผ์ด๋‹ค. ์‹ค์ œ ๊ธฐ์—…์˜ ์ž…์žฅ์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋งค๋ ฅ๋„๋Š” ์ˆ˜์ต์ด๋ฉฐ ์‹œ์žฅ์˜ ์„ฑ์žฅ์„ฑ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์•„์šธ๋Ÿฌ ํŠน์ • ์‹œ์ ๋งŒ์˜ ๋น„๊ต๋Š” ๊ทธ ์‹œ์žฅ์˜ ์ž ์žฌ์„ฑ์„ ํŒŒ์•…ํ•˜๊ธฐ ์–ด๋ ต๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‹œ์žฅ ๋ถ„์„์„ ํ•  ๋•Œ์—๋Š” ์‹œ๊ฐ„ ๋ณ€์ˆ˜๋ฅผ ์ž˜ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. ๋งŒ์ผ ์‹œ๊ฐ„ ๋ณ€์ˆ˜๋ฅผ ๊ณ ๋ คํ•˜๊ธฐ ์–ด๋ ค์šด ๊ฒฝ์šฐ์—๋Š” ์ƒํ™ฉ๋ณ„ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ๊ณ ๋ คํ•œ๋‹ค. ์‹œ์žฅ์˜ ํฌ๊ธฐ์™€ ๋”๋ถˆ์–ด ์‚ฌ์—… ๋ชฉํ‘œ๋ฅผ ์ˆ˜๋ฆฝํ•  ๋•Œ์—๋Š” ์†Œ์œ„, TAM(Total Addressable Market or Total Available Market)์„ ์‚ฐ์ •ํ•ด์•ผ ํ•œ๋‹ค. TAM์€ ์ž ์ •์ ์œผ๋กœ ์‚ฐ์ •๋œ ์ „์ฒด ์‹œ์žฅ์˜ ํฌ๊ธฐ(Market Full Potential)๋ฅผ ๋†“๊ณ  ๋งˆ์ผ€ํŒ…๊ณผ ์˜์—… ํ™œ๋™์˜ ์ง‘์ค‘๋„๋ฅผ ๊ณต๊ฒฉ์ (Aggressive), ์ผ๋ฐ˜์ (Moderate), ๋ณด์ˆ˜์ (Conservative)์œผ๋กœ ๋ฐ˜์˜ํ•œ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์„ค์ •ํ•˜๊ณ  ์ด๋ฅผ ๋ฐ˜์˜ํ•œ ์‹œ์žฅ์˜ ํฌ๊ธฐ์™€ ์‚ฌ์—…๋ชฉํ‘œ๋ฅผ ์‚ฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์—…๋ชฉํ‘œ๋ฅผ ์ˆ˜๋ฆฝํ•  ๋•Œ, ์ „์ฒด ์‹œ์žฅ ๋Œ€๋น„ ์–ผ๋งˆ์˜ ์ ์œ ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋™์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด์„œ ์‚ฌ์—… ์˜์ง€๋„ ๊ฐ™์ด ๋ณด์—ฌ์ฃผ๋Š” ์šฉ๋„๋กœ ๋งŽ์ด ํ™œ์šฉํ•œ๋‹ค. ๋‹ค๋งŒ, ์‹œ๋‚˜๋ฆฌ์˜ค๊ฐ€ ์ •๊ตํ•ด์ง€๋ ค๋ฉด โ€˜๊ทธ๋ƒฅ ์—ด์‹ฌํžˆ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹คโ€™๋ฅผ โ€˜Aggressiveโ€™๋กœ ๋ฐ˜์˜ํ•˜๊ฑฐ๋‚˜ ํ•˜์ง€ ๋ง๊ณ  ๋งˆ์ผ€ํŒ…์ด๋‚˜ ์˜์—… ์ž์›์„ ์ •๋Ÿ‰ํ™”ํ•˜์—ฌ ๊ทธ๋“ค์˜ ํˆฌ์ž… ๊ทœ๋ชจ์— ๋น„๋ก€ํ•ด์„œ ์–ด๋–ป๊ฒŒ ์‹œ์žฅ ๊ทœ๋ชจ์™€ ์‚ฌ์—… ๋ชฉํ‘œ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š”์ง€ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ๋ฐ˜์˜ํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. Figure III-6. Total Addressible Market ์ ์šฉ ์‚ฌ์—…๋ชฉํ‘œ - ๋ฏธ๊ตญ ๋ฐœ์ „์‚ฌ์—… ์‚ฌ๋ก€ [1] IT ๋ฆฌ์„œ์น˜ ๊ธฐ์—…์ธ ๊ฐ€ํŠธ๋„ˆ๊ทธ๋ฃน(www.gartner.com)์€ ๊ธฐ์ˆ  ์„ฑ์ˆ™๋„์— ๋”ฐ๋ผ ๊ธฐ์ˆ ์˜ ํฌ์ง€์…”๋‹์„ ์ถ”์ ํ•˜๋Š” 'Hype Cycle'์ด๋ผ ๋ถ€๋ฅด๋Š” ๊ธฐ์ˆ  ๋กœ๋“œ๋งต์„ ๋งค๋…„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค. TableIII-4์˜ ๊ธฐ์ˆ ๋ถ„์„ ์‚ฌ๋ก€๋„ ๊ฐ€ํŠธ๋„ˆ๊ทธ๋ฃน์˜ Hype Cycle์„ ์˜ˆ์‹œ๋กœ ํ•˜์˜€๋‹ค [2] ์‹œ์žฅ ์ ์œ ์œจ์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ด€์ ์€ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ๊ฐ€ ์œ ์šฉํ•œ ์‹œ์žฅ ๋˜๋Š” ์‚ฐ์—…์ด๊ฑฐ๋‚˜ B2C ์‚ฌ์—…์ผ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. B2B ์‚ฌ์—…์—์„œ๋Š” ์‹œ์žฅ ์ ์œ ์œจ๋ณด๋‹ค๋Š” ๊ณ ๊ฐ ์ ์œ ์œจ์„ ๋” ์ค‘์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค. [3] Relative Market Share ์ƒ๋Œ€์  ์‹œ์žฅ ์ ์œ ์œจ [4] Competitive Advantage [5] ์ฒซํ•ด์˜ ๊ฐ’์— ์Œ์ˆ˜ ๋ถ€ํ˜ธ๋ฅผ ์žŠ์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค [6] Customer Price Index 07. ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—…๋ถ„์„(2/4) ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„์˜ ์ฒซ ๋ฒˆ์งธ ์ˆœ์„œ๋กœ ๋ฉ”๊ฐ€ํŠธ๋ Œ๋“œ ๋ถ„์„์„ ์œ„ํ•œ PEST ๋ถ„์„๊ณผ ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ์‚ฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•, ์„ฑ์žฅ๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ• ๋“ฑ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. ์˜ค๋Š˜์€ ๊ฒฝ์Ÿ ๋ถ„์„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. 1.3 ๊ฒฝ์Ÿ ๋ถ„์„ ๊ฒฝ์Ÿ ๋ถ„์„์˜ ๊ธฐ๋ณธ์€ '๊ฒฝ์Ÿ์‚ฌ ํ”„๋กœํŒŒ์ผ๋ง(Competitors Profiling)'๊ณผ '๊ฒฝ์Ÿ์‚ฌ ํฌ์ง€์…”๋‹(Competitors Positioning)'์ด๋‹ค. โ€˜๊ฒฝ์Ÿ์‚ฌ ํ”„๋กœํŒŒ์ผ๋งโ€™์ด๋ž€ ๊ฒฝ์Ÿ ๊ธฐ์—…์˜ ์ •๋ณด(Filmography)๋ฅผ ํ™•์ธํ•˜๊ณ  ์ด๋ฅผ ํ†ตํ•ด ์‹œ์‚ฌ์ ์„ ๋„์ถœํ•˜๋Š” ๊ฒƒ์„ ๋งํ•˜๋ฉฐ, โ€˜๊ฒฝ์Ÿ์‚ฌ ํฌ์ง€์…”๋‹โ€™์€ ๊ฐ ๊ธฐ์—… ๋‚˜๋ฆ„์˜ ๋ถ„๋ฅ˜ ๊ธฐ์ค€์„ ๊ฐ€์ง€๊ณ  ๊ฒฝ์Ÿ์‚ฌ๋ฅผ ์˜๋ฏธ ์žˆ๋Š” ๋งคํŠธ๋ฆญ์Šค์— ๋ฐฐ์น˜ํ•˜๊ณ  ํ•ด๋‹น ์ƒํ™ฉ์—์„œ ํ†ต์ฐฐ๋ ฅ์„ ์–ป๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ๋ณต์ˆ˜์˜ ๊ฒฝ์Ÿ์‚ฌ๋ฅผ ๋‹ค๋ฃฐ ๊ฒฝ์šฐ, ๊ฒฝ์Ÿ์‚ฌ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๋ถ„์„ํ•˜๋Š”๋ฐ BCG ๋งคํŠธ๋ฆญ์Šค[1]๋ฅผ ์‘์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ  ๊ด€๋ จํ•ด์„œ๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ๋ฒ•๋“ค์ด ์žˆ๋‹ค Figure III-7. ๊ฒฝ์Ÿ์‚ฌ ๋ถ„์„ ์‚ฌ๋ก€ โ€“ BCG ๋งคํŠธ๋ฆญ์Šค ํ™œ์šฉ ๊ฒฝ์Ÿ์‚ฌ ํ”„๋กœํŒŒ์ผ๋ง์€ ๊ฒฝ์Ÿ์‚ฌ ์ •๋ณด๋ฅผ ํ•œ๋ˆˆ์— ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด ์›์ฒœ(Source)๊ณผ ํ•จ๊ป˜ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ •๋ณด๋ฅผ ๋ฐ˜๋“œ์‹œ ํฌํ•จํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ธฐ์—…์˜ ๋ฐฐ๊ฒฝ(Background): ๊ฒฝ์Ÿ์‚ฌ์˜ ์ฐฝ๋ฆฝ ์‹œ๊ธฐ, ์ฃผ์š” ์—ญ์‚ฌ, ์ง€๋ฐฐ ๊ตฌ์กฐ ๋“ฑ ๊ธฐ์—…์˜ ์ „๋žต(Corporate Strategy): ๊ฒฝ์Ÿ์‚ฌ์˜ ๋น„์ „, ์ค‘์žฅ๊ธฐ ์ „๋žต, ์‚ฌ์—… ์ „๋žต ๋“ฑ ์žฌ๋ฌด ์ •๋ณด(Financials): ๋งค์ถœ, ์˜์—…์ด์ต ๋“ฑ ๊ฐ์ข… ์ˆ˜์ต์„ฑ ์ง€ํ‘œ ์กฐ์ง/์ธ์‚ฌ(Personnel): ์ธ๋ ฅ ํ˜„ํ™ฉ, ๋งจํŒŒ์›Œ ๋“ฑ ์ œํ’ˆ(Products): ์ œํ’ˆ ๋ผ์ธ์—…(line-up), ์—ฐ๊ตฌ๊ฐœ๋ฐœ ์—ญ๋Ÿ‰ ๋“ฑ ์‹œ์„ค(Facilities): ๊ณต์žฅ, ์ƒ์‚ฐ ์—ญ๋Ÿ‰ ๋“ฑ ๋งˆ์ผ€ํŒ…/์˜์—…(Marketing/Sales): ๋งˆ์ผ€ํŒ… ๋ฐ ์˜์—… ์ฃผ์š” ์ „๋žต, ์ตœ๊ทผ ์‹ค์  ๋“ฑ ๋˜ํ•œ, ๊ฒฝ์Ÿ์‚ฌ ํ”„๋กœํŒŒ์ผ๋ง์€ Table III-5์™€ ๊ฐ™์€ ์žฅ๋‹จ์ ์ด ์žˆ๋‹ค. Table III-5. ๊ฒฝ์Ÿ์‚ฌ ํ”„๋กœํŒŒ์ผ๋ง์˜ ์žฅ๋‹จ์ [2] ๊ฒฝ์Ÿ์‚ฌ ํ”„๋กœํŒŒ์ผ๋ง์„ ํ†ตํ•ด ๊ธฐ๋ณธ์ ์ธ ์ •๋ณด๊ฐ€ ์ทจํ•ฉ๋˜๋ฉด ์ด์–ด์„œ ๊ฒฝ์Ÿ์‚ฌ ํฌ์ง€์…”๋‹์„ ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ฒฝ์Ÿ์‚ฌ ํฌ์ง€์…”๋‹์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ง„ํ–‰ํ•œ๋‹ค. (1) ๊ฒฝ์Ÿ์‚ฌ์˜ ๊ธฐ๋ณธ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์Ÿ์‚ฌ ์ •๋ณด๋ฅผ ํ™•๋ณดํ•˜๋Š” ๋…ธ๋ ฅ์ด ํ•„์š”ํ•˜๋ฉฐ ๊ฒฝ์Ÿ์‚ฌ๊ฐ€ ์ƒ์žฅ ๊ธฐ์—…์ด๋ผ๋ฉด ๊ธฐ์—…๊ณต์‹œ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹๋‹ค. ํ•œ๊ตญ ๊ธฐ์—…์˜ ๊ฒฝ์šฐ, ๊ธˆ์œต๊ฐ๋…์›์—์„œ ์ œ๊ณตํ•˜๋Š” โ€˜์ „์ž๊ณต์‹œ์‹œ์Šคํ…œโ€™ [3]์„ ํ†ตํ•ด ๊ธฐ์—…์˜ ์‚ฌ์—… ํ˜„ํ™ฉ ์ฆ‰, ๋งค์ถœ ๋ฐ ์˜์—… ์ด์ต ๋“ฑ ์žฌ๋ฌด์ •๋ณด์™€ ์ฃผ์š” ์ด๋ฒคํŠธ์˜ ์ถ”์ง„ ํ˜„ํ™ฉ์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•œํŽธ, ์œ ํ•œํšŒ์‚ฌ๋‚˜ ๋น„์ƒ์žฅ๊ธฐ์—…์˜ ๊ฒฝ์šฐ, ํ•ด๋‹น ๊ธฐ์—…์˜ ํ™ˆํŽ˜์ด์ง€๋‚˜ ๋ณ„๋„์˜ ์ „๋ฌธ์กฐ์‚ฌ ๊ธฐ๊ด€์„ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค. ํ•ด์™ธ ์‚ฌ์—…์„ ์œ„ํ•ด ์™ธ๊ตญ ๊ธฐ์—…์˜ ์ •๋ณด๋ฅผ ์•Œ์•„๋ณด์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๊ธฐ์—…์ด ๋ฏธ๊ตญ ์ฆ์‹œ์— ์ƒ์žฅ๋˜์–ด ์žˆ๋‹ค๋ฉด ๋ฏธ๊ตญ ์ฆ๊ถŒ ๊ฑฐ๋ž˜์œ„์›ํšŒ[4]๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ •๋ณด๋“ค์ด ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค. ํŠนํžˆ Edgar[5]๋ฅผ ํ†ตํ•ด ์ œ๊ณต๋˜๋Š” 10K์™€ ๊ฐ™์€ ๊ธฐ๊ฐ„๋ณ„ ๋ณด๊ณ ์„œ๋“ค์€ ํ•ด๋‹น ๊ธฐ์—…์˜ ํ˜„ํ™ฉ์„ ์ƒ์„ธํžˆ ํŒŒ์•…ํ•˜๋Š”๋ฐ ๋งŽ์€ ๋„์›€์„ ์ค€๋‹ค. ๋‹ค๋งŒ, ์ด๋Ÿฐ ๊ณต์‹œ ์ •๋ณด์˜ ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ชจ๋‘ ๊ณผ๊ฑฐ์˜ ๋ฐ์ดํ„ฐ ๋˜๋Š” ์ •๋ณด๋ผ๋Š” ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๊ฒฝ์Ÿ์‚ฌ ์ •๋ณด๋Š” ๊ณต์‹œ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋˜ ์ˆ˜์‹œ๋กœ ์—…๋ฐ์ดํŠธ๋˜์–ด์•ผ ํ•œ๋‹ค. Figure III-8. ๋ฏธ๊ตญ ์ฆ๊ถŒ ๊ฑฐ๋ž˜์œ„์›ํšŒ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ์—…๊ณต์‹œ์ •๋ณด (2) ๊ณต์‹œ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ „๋žต์  ์˜์˜๋ฅผ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ํ•ญ๋ชฉ์„ ์ •์˜ํ•˜๊ณ  ์ถ”๊ฐ€ ์ƒ์„ธ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํฌ์ง€์…”๋‹ ๋งต(positioning map or perceptual map)์„ ๊ตฌ์„ฑํ•œ๋‹ค. Figure III-9๋Š” ์ž๋™์ฐจ์˜ ๊ฐ€๊ฒฉ๊ณผ ํ’ˆ์งˆ๋กœ ์‚ฌ๋ถ„๋ฉด์„ ๊ตฌ๋ถ„ํ•˜๊ณ  ์™„์„ฑ์ฐจ ๊ธฐ์—…๋“ค์˜ ํฌ์ง€์…”๋‹ ํ•œ ์‚ฌ๋ก€์ด๋‹ค. 2017๋…„ ํ˜„์žฌ๋Š” ๊ธฐ์—…์˜ ํฌ์ง€์…˜์ด ๋งŽ์ด ๋ณ€ํ–ˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ์—ฌ์ „ํžˆ ๋…๋ฆฝ ๋ธŒ๋žœ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ BMW์™€ MINI๋Š” ํ•ฉ๋ณ‘๋˜์–ด ํ•œ ํšŒ์‚ฌ ์†Œ์†์ด๋‹ค. Figure III-9. ๊ฒฝ์Ÿ์‚ฌ ํฌ์ง€์…”๋‹ - ์ž๋™์ฐจ ์‚ฐ์—… ์‚ฌ๋ก€ Part III์—์„œ ์„ค๋ช…ํ•  ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์—์„œ ๊ทธ๋Ÿฐ ์–ธ๊ธ‰์„ ๊ณ„์†ํ•˜๊ฒ ์ง€๋งŒ ํฌ์ง€์…”๋‹ ๋งต์˜ ์ž‘์„ฑ๋„ ๊ฐœ์ธ์ ์œผ๋กœ๋Š” ๊ทธ๋ ‡๊ฒŒ ๋น„์šฉ ํšจ์œจ์ ์ธ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐ๋˜์ง€ ์•Š๋Š”๋‹ค. ํ˜„์žฌ ๊ฒฝ์Ÿํ•˜๋Š” ์‚ฐ์—…์— ์–ด๋–ค ๊ธฐ์—…๋“ค์ด ์žˆ๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด ์ผํšŒ์ ์œผ๋กœ ํ™•์ธํ•  ๋ฟ์ด๊ณ  ์‹ค์‹œ๊ฐ„ ํ˜„ํ™ฉ์„ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ฌผ๋ก , ์ด ํฌ์ง€์…˜์ด ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ”๋€” ์ •๋„๋กœ ๋ถˆ์•ˆํ•œ ์‚ฐ์—…์„ ์—†์„ ๊ฒƒ์ด์ง€๋งŒ ๋…ธ๋ ฅ ๋Œ€๋น„ So-so๋ผ๊ณ  ๋ฐ–์— ์ƒ๊ฐ์ด ๋“ค์ง€ ์•Š๋Š”๋‹ค. ๋‹ค๋งŒ, ๊ตฌ๋ถ„ ๋˜๋Š” ๋น„๊ต ์ฒ™๋„๋ฅผ ์‹ค์  ๋“ฑ ์ •๋Ÿ‰์  ํ‰๊ฐ€๋กœ ๋Œ€์ฒดํ•˜๊ณ  ๋ฒ„๋ธ” ์ฐจํŠธ ๊ฐ™์€ ๊ฒƒ์œผ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์กฐ๊ธˆ ๋” ํ˜„์‹ค์ ์ด ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์‚ฌ์‹ค ๊ทธ๋Ÿฐ ๋…ธ๋ ฅ์œผ๋กœ BCG ๋งคํŠธ๋ฆญ์Šค ๊ฐ™์€ ๊ฒƒ์ด ๋‚˜์™”์ง€๋งŒ ํ•œ๊ณ„๋Š” ์—ฌ์ „ํ•˜๋‹ค. Break #12. Apple์˜ ํฌ์ง€์…”๋‹ ์ด์•ผ๊ธฐ ํฌ์ง€์…”๋‹์€ ์ธ์ง€(perception)์˜ ๋ฌธ์ œ์ด๋‹ค. โ€˜๊ณ ๊ฐ์ด ๊ธฐ์—…์˜ ์ œํ’ˆ์„ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜๋Š๋ƒ?โ€™๋Š” ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ์ „ํ˜•์ ์ธ B2C ํฌ์ง€์…”๋‹ ๊ฐ€์ด๋“œ[6]๊ฐ€ ์žˆ๊ณ , ๋ธŒ๋žœ๋“œ ์ „๋žต์ด๋‚˜ ๊ด‘๊ณ  ๋“ฑ์œผ๋กœ ๊ทธ๊ฒƒ์„ ์‹คํ˜„ํ•˜๊ณ ์ž ํ•œ๋‹ค. B2B ์‚ฌ์—…์—์„œ๋Š” ์ œํ’ˆ์˜ ์ฐจ๋ณ„ํ™”๋œ ํŠน์„ฑ์ด๋‚˜ ์ด๋ฏธ์ง€๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ธฐ์—…๊ฐ€์น˜ ์ œ์•ˆ(Value Proposition)์ด ๋งค์šฐ ์ค‘์š”ํ•˜๋ฉฐ, ๋งˆ์ผ€ํŒ… ๋ฏน์Šค(Marketing Mix)๋ฅผ ํ†ตํ•ด ์ง€์†์ ์œผ๋กœ ๊ณ ๊ฐ๊ณผ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์• ํ”Œ(Apple)์˜ ๊ฒฝ์šฐ, 1997๋…„ ๋‹ค์‹œ ๊ฒฝ์˜์— ๋ณต๊ท€ํ•œ ์Šคํ‹ฐ๋ธŒ ์žก์Šค(Steve Jobs. 1955 ~ 2011)๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ๊ณ ๋ฏผํ–ˆ๋˜ ๊ฒƒ์ด ์• ํ”Œ์˜ โ€˜ํฌ์ง€์…”๋‹(Positioning)โ€™์ด์—ˆ๋‹ค. ๋‹น์‹œ ์• ํ”Œ์€ IBM๊ณผ ๊ฒฝ์Ÿํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ์—… ๋‚ด๋ถ€ ํ”„๋กœ์„ธ์Šค ๋ฐ ๊ณต๊ธ‰๋ง, ๋ธŒ๋žœ๋“œ ์ด๋ฏธ์ง€ ๋“ฑ ๊ฑฐ์˜ ๋ชจ๋“  ๋ถ€๋ถ„์„ ์žก์Šค ์ด์ „๊ณผ ์™„์ „ํžˆ ๋‹ค๋ฅด๊ฒŒ ๊ตฌ์ถ•ํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ์Šคํ‹ฐ๋ธŒ ์žก์Šค๋Š” ๋ณต๊ท€ ํ›„ ์ด๊ฒƒ๋“ค์„ ๋ชจ๋‘ ๋ฐ”๊พธ๋ฉด์„œ ์• ํ”Œ์˜ ์กด์žฌ ์ด์œ ์— ๋Œ€ํ•ด ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ณ ๋ฏผํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋“ค์˜ ํ•˜๋“œ์›จ์–ด๋‚˜ ์šด์˜์ฒด์ œ(OS)๋Š” ๊ฒฝ์Ÿ ์ œํ’ˆ๋ณด๋‹ค ์šฐ์ˆ˜ํ–ˆ๊ณ  ๊ทธ๋ž˜ํ”ฝ๊ณผ ๊ฐ™์€ ํŠน์ • ๋ถ€๋ฌธ์—์„œ๋Š” ๋น„๊ตํ•  ์ˆ˜๋„ ์—†์ด ๊ฐ•๋ ฅํ–ˆ์ง€๋งŒ, ์Šคํ‹ฐ๋ธŒ ์žก์Šค๋Š” ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์–ดํ•„๋ณด๋‹ค๋Š” ๋ณด๋‹ค ๊ทผ๋ณธ์ ์ธ ์‚ฐ์—…์—์„œ์˜ ์• ํ”Œ์˜ ์กด์žฌ ์˜์˜์™€ ๊ธฐ์—…์˜ ๋ณธ์งˆ์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•˜๊ณ ์ž ํ•˜์˜€๋‹ค. ์ด ๊ณผ์ •์—์„œ ์Šคํ‹ฐ๋ธŒ ์žก์Šค๋Š” ๋งˆ์ผ€ํŒ…ํŒ€์—๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์งˆ๋ฌธ๋“ค์„ ๋˜์กŒ๋‹ค [7]. ์šฐ๋ฆฌ ๊ณ ๊ฐ๋“ค์€ ์šฐ๋ฆฌ๋ฅผ ์•Œ๊ธฐ ์›ํ•œ๋‹ค. ์• ํ”Œ(Apple)์€ ๋ˆ„๊ตฌ์ธ๊ฐ€? ๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๋Š” ๋ฌด์—‡์„ ๋Œ€ํ‘œํ•˜๋Š”๊ฐ€? ์šฐ๋ฆฌ๋Š” ์ด ์„ธ์ƒ ์–ด๋””์— ์†ํ•ด์žˆ๋Š”๊ฐ€? ๋‹ค์†Œ ์ฒ ํ•™์ ์œผ๋กœ ๋“ค๋ฆด ์ˆ˜๋„ ์žˆ๊ฒ ์œผ๋‚˜ ์Šคํ‹ฐ๋ธŒ ์žก์Šค์™€ ๋‹น์‹œ ์• ํ”Œ์˜ ๋งˆ์ผ€ํŒ…ํŒ€์€ ์œ„์˜ ์งˆ๋ฌธ์— ๋Œ€ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹ต์„ ๋„์ถœํ–ˆ๋‹ค. โ€œ์—ด์ •์„ ๊ฐ€์ง„ ์‚ฌ๋žŒ๋“ค์€ ์„ธ์ƒ์„ ๋ณด๋‹ค ๋‚˜์€ ๊ณณ์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹คโ€ ์ด ๋ฌธ์žฅ์„ ๊ธฐ์—…์˜ ํ•ต์‹ฌ๊ฐ€์น˜๋กœ ์ทจํ•˜์˜€๊ณ , ์ดํ›„ ๊ทธ ์œ ๋ช…ํ•œ ์บ์น˜ํ”„๋ ˆ์ด์ฆˆ โ€˜Think Different!โ€™๊ฐ€ ํƒ„์ƒํ•˜์˜€๋‹ค. ์ด๋ฅผ ๊ณ„๊ธฐ๋กœ ์• ํ”Œ์€ ์ œ2์˜ ๋„์•ฝ์„ ์ด๋ฃจ๊ฒŒ ๋œ๋‹ค. Steve jobs 'Think Different' and Ad. ์• ํ”Œ์ด๋ผ๋Š” ๊ธฐ์—…์˜ ํŠน์ง•, ๊ฐœ์ธ์— ๋Œ€ํ•œ ๊ฐ์ˆ˜์„ฑ์ด ๋ฏผ๊ฐํ•˜๊ฒŒ ์ž‘์šฉํ•˜๋Š” B2C ์‚ฌ์—…์ด๋ผ๋Š” ํŠน์„ฑ์„ ๊ฐ์•ˆํ•˜๋”๋ผ๋„ ํฌ์ง€์…”๋‹์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์ถฉ๋ถ„ํ•  ๊ฒƒ์ด๋‹ค. ์ด์™€ ์œ ์‚ฌํ•œ ํ†ต์ฐฐ๋ ฅ์„ ์–ป๊ธฐ ์œ„ํ•ด B2B ๊ธฐ์—…์˜ ํฌ์ง€์…”๋‹๋„ ๋‹ค์Œ ์งˆ๋ฌธ์„ ํ†ตํ•ด์„œ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ณ ๊ฐ์€ ์šฐ๋ฆฌ ๊ธฐ์—…์—๊ฒŒ ๋ฌด์—‡์„ ๊ธฐ๋Œ€ํ•˜๊ณ  ์žˆ๋Š”๊ฐ€? (= ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๋Š” ๋ฌด์—‡์ธ๊ฐ€?) ๊ฒฝ์Ÿ์‚ฌ์™€ ๋น„๊ตํ•˜์—ฌ ์šฐ์ˆ˜ํ•œ ์ ์€ ๋ฌด์—‡์ธ๊ฐ€? (= ์ œํ’ˆ์ด๋‚˜ ๋ธŒ๋žœ๋“œ ๋“ฑ ๋…ธ์ถœ๋œ ์ธก๋ฉด์—์„œ ์šฐ์ˆ˜ํ•œ ์ ์€ ๋ฌด์—‡์ธ๊ฐ€?) ๊ณ ๊ฐ์€ ์™œ ์šฐ๋ฆฌ๋ฅผ ์„ ํƒํ•ด์•ผ ํ•˜๋‚˜? (= ๊ฒฝ์Ÿ ๋Œ€๋น„ ์–ด๋–ค ๋ถ€๋ถ„์ด ๊ณ ๊ฐ์—๊ฒŒ ์–ดํ•„๋˜๊ณ  ์žˆ๋Š”๊ฐ€? ๊ณ ๊ฐ ๊ฐ€์น˜๋ฅผ ์ถฉ์กฑ์‹œํ‚ค๋Š”๊ฐ€? ๋“ฑ) ๊ฒฝ์Ÿ ๋ถ„์„์—์„œ ์œ„์™€ ๊ฐ™์€ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์„ ์–ป๊ธฐ ์œ„ํ•ด ํ•ต์‹ฌ ๊ณ ๊ฐ๊ณผ ํŒŒํŠธ๋„ˆ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์šฐ๋ฆฌ ์ œํ’ˆ์˜ ์–ด๋–ค ์†์„ฑ์ด ๋›ฐ์–ด๋‚œ์ง€, ๊ทธ ์†์„ฑ์ด ๊ณ ๊ฐ์—๊ฒŒ ์™œ ์ค‘์š”ํ•œ์ง€, ์šฐ๋ฆฌ ๊ธฐ์—…์€ ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์„ ์–ผ๋งˆ๋‚˜ ์ถฉ์กฑ์‹œํ‚ค๋Š”์ง€ ๋“ฑ์„ ์กฐ์‚ฌ[8]ํ•˜์—ฌ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ ์ง„์ •ํ•œ ๊ธฐ์—…์˜ ๊ฐ€์น˜(Core Value)๋ฅผ ์•Œ๋ฆฌ๊ณ  ์ด๋ฅผ ๊ณ ๊ฐ์—๊ฒŒ ์ œ๋Œ€๋กœ ์ œ์•ˆํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์ฆ‰, ์ œ๋Œ€๋กœ ํฌ์ง€์…”๋‹ ํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. 1.4 BCG/GE ๋งคํŠธ๋ฆญ์Šค ๋ถ„์„ ํฌ์ง€์…”๋‹์„ ํ†ตํ•ด ๊ฒฝ์Ÿ์„ ๊ณ ๋ฏผํ•˜๋ฉด์„œ ์ด์ œ ์ž์‚ฌ์˜ ์˜์—ญ์œผ๋กœ ๋„˜์–ด์˜ค๋ฉด ๋‚ด๊ฐ€ ๋ฌด์—‡์„ ์ž˜ ํ•˜๋Š”์ง€, ๋‚˜์˜ ์‚ฌ์—…์€ ๊ฑด์ „ํ•œ์ง€ ๋“ฑ์„ ์„ฑ์ฐฐํ•˜๊ฒŒ ๋œ๋‹ค. BCG ๋งคํŠธ๋ฆญ์Šค๋Š” ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์— ์˜์˜๋ฅผ ๋‘˜ ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ์ด๋‹ค. BCG ๋งคํŠธ๋ฆญ์Šค๋Š” ๋ณด์Šคํ„ด์ปจ์„คํŒ… ๊ทธ๋ฃน(BCG)์—์„œ ๊ธฐ์—…์˜ ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค(business portfolio)๋ฅผ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐœ๋ฐœํ•œ ๋„๊ตฌ๋กœ ์‚ฌ์—…์˜ ๊ทœ๋ชจ, ์ƒ๋Œ€์  ์‹œ์žฅ์ ์œ ์œจ๊ณผ ์‹œ์žฅ ์„ฑ์žฅ๋ฅ ๋กœ ๊ตฌ์„ฑ๋œ ๋ฒ„๋ธ” ์ฐจํŠธ(Bubble Chart)๋กœ ์‚ฌ์—…๋“ค์„ ๊ฐ ์‚ฌ๋ถ„๋ฉด์— ๋ฐฐ์น˜ํ•œ ํ›„, Dogs, Dilemma ์˜์—ญ์˜ ์‚ฌ์—…๋“ค์„ Star, Cash Cows๋กœ ํ‚ค์šฐ๊ธฐ ์œ„ํ•œ ์ „๋žต์  ํ™œ๋™(์‚ฌ์—… ์ฒ ์ˆ˜ ํฌํ•จ)์„ ๊ฐœ๋ฐœํ•œ๋‹ค. ๋˜ํ•œ, GE๋Š” BCG ๋งคํŠธ๋ฆญ์Šค๋ฅผ 3 X 3๋กœ ํ™•์žฅํ•˜์—ฌ ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค์˜ ๋ถ„์„์„ ๋ณด๋‹ค ๊นŠ๊ฒŒ ๋ณด๊ณ ์ž ํ•˜์˜€๋‹ค. Figure III-10. BCG ๋งคํŠธ๋ฆญ์Šค์˜ ๊ฐœ๋… BCG ๋งคํŠธ๋ฆญ์Šค์—์„œ ํ™œ์šฉ๋˜๋Š” ์‹œ์žฅ ์ ์œ ์œจ(Market share)[9]์€ ์ ˆ๋Œ€์  ๊ด€์ ๊ณผ ์ƒ๋Œ€์  ๊ด€์ ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ ˆ๋Œ€์  ์‹œ์žฅ์ ์œ ์œจ(์‚ฌ์—…) = ์ž์‚ฌ์˜ ์ด๋งค์ถœ / ์‹œ์žฅ์˜ ํฌ๊ธฐ ์ ˆ๋Œ€์  ์‹œ์žฅ์ ์œ ์œจ(์„ธ๊ทธ๋จผํŠธ) = ์ž์‚ฌ ์„ธ๊ทธ๋จผํŠธ ๋งค์ถœ / ์‹œ์žฅ์˜ ์„ธ๊ทธ๋จผํŠธ ๋งค์ถœ ์ƒ๋Œ€์  ์‹œ์žฅ์ ์œ ์œจ 10 = ์ž์‚ฌ์˜ ์ด๋งค์ถœ / ์ตœ๋Œ€ ๊ฒฝ์Ÿ์ž์˜ ์ด๋งค์ถœ ์ƒ๋Œ€์  ์‹œ์žฅ์ ์œ ์œจ(์„ธ๊ทธ๋จผํŠธ) = ์ž์‚ฌ ์„ธ๊ทธ๋จผํŠธ ๋งค์ถœ / ์ตœ๋Œ€ ๊ฒฝ์Ÿ์ž๋“ค์˜ ์„ธ๊ทธ๋จผํŠธ ๋งค์ถœ Fig III-11. BCG/GE ๋งคํŠธ๋ฆญ์Šค ์‚ฌ๋ก€ ๊ทธ๋Ÿฐ๋ฐ BCG/GE ๋งคํŠธ๋ฆญ์Šค๋Š” ์‚ฌ์—…์˜ ํ˜„ํ™ฉ์„ ์„ธ๋ จ๋˜๊ฒŒ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ๋„ ์žˆ์ง€๋งŒ ๋ฒ„๋ธ” ์ฐจํŠธ๋ฅผ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•ด ์‹œ์žฅ ์ ์œ ์œจ์„ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๋ฉฐ, ๊ฒฝ์Ÿ์‚ฌ ์ •๋ณด์— ๊ธฐ๋ฐ˜ํ•œ ์ƒ๋Œ€์  ์‹œ์žฅ์ ์œ ์œจ์„ ์ถ”์ •ํ•˜๊ธฐ ์–ด๋ ต๊ณ , ๋˜ ํŠน์ • ์‹œ์ ์˜ ์ •๋ณด์ด๋ฏ€๋กœ ๋ณ€ํ™”ํ•˜๋Š” ์ƒํ™ฉ์„ ์ ์‹œ์— ๋ฐ˜์˜ํ•˜๊ธฐ ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. ์ตœ๊ทผ์—๋Š” ๋งคํŠธ๋ฆญ์Šค์˜ ์ถ•์„ ์‹œ์žฅ ์ ์œ ์œจ ๋Œ€์‹  ๋งค์ถœ๊ณผ ์˜์—…์ด์ต๋ฅ  ๊ฐ™์€ ์‚ฌ์—… ์‹ค์ ์œผ๋กœ ํ‘œํ˜„ํ•˜์—ฌ ํŠน์ • ์‹œ์ ์˜ ์‚ฌ์—… ์‹ค์ ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• ๋“ฑ์œผ๋กœ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด Figure III-11๊ณผ ๊ฐ™์ด ์„ฑ์žฅ์„ฑ๊ณผ ์ˆ˜์ต์„ฑ์ด ๋†’์€ ์˜์—ญ์˜ ์‚ฌ์—…์„ ์ง‘์ค‘ ์œก์„ฑํ•˜๋˜ ์•ค์†Œํ”„ ๋งคํŠธ๋ฆญ์Šค[11]๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ ๊ฐ๊ณผ ์ œํ’ˆ ๊ด€์ ์—์„œ ์ฐจ๋ณ„์  ์ ์šฉ์„ ํ•˜๊ณ  ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ง€์†์ ์œผ๋กœ ์ˆ˜์ต ํ™•๋ณด๋ฅผ ์œ„ํ•œ ์ œํ’ˆ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์œก์„ฑํ•˜์ž๋Š” ํ†ต์ฐฐ๋ ฅ์„ ์–ป๋Š” ์‹์ด๋‹ค. Figure III-12. BCG ๋งคํŠธ๋ฆญ์Šค์™€ Ansoff ๋งคํŠธ๋ฆญ์Šค์˜ ํ™œ์šฉ BCG ๋งคํŠธ๋ฆญ์Šค๋Š” ์• ๋งคํ•œ ๋„๊ตฌ์ด๋‹ค. ์‚ฌ์—… ์šด์šฉ์—์„œ ํ•ด๋‹น ์‚ฌ์—… ๋˜๋Š” ์„ธ๊ทธ๋จผํŠธ์˜ ๊ทœ๋ชจ์™€ ํฌ์ง€์…˜์„ ๋ชจ๋‘ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ ์‚ฌ์—…์˜ ๋™์ ์ธ ๋ถ€๋ถ„์„ ์ ์‹œ์— ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ์ €์ž๊ฐ€ ๊ณผ๊ฑฐ ์ „๋žต๊ธฐํšํŒ€์—์„œ ์ผํ•  ๋•Œ๋Š” BCG ๋งคํŠธ๋ฆญ์Šค๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™œ์šฉํ•ด ๋ณด์•˜๋‹ค. ์—‘์…€์˜ index์™€ match ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๊ฐ€ ๋™์ ์œผ๋กœ ๋ณ€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ณ  ์Šคํฌ๋กค ๋ง‰๋Œ€๋ฅผ ์ด์šฉํ•ด์„œ ๊ทธ๊ฑธ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€๋‹ค. ๋ฐ˜์ž๋™์ด๋ผ๊ณ ๋‚˜ ํ• ๊นŒ? ์กฐ๊ฑด์— ๋”ฐ๋ผ ์‚ฌ์—… ์„ธ๊ทธ๋จผํŠธ์˜ ๋ณ€ํ™”๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์–ด์„œ ๋งค์šฐ ์ƒ์‚ฐ์ ์ธ ๋ณด๊ณ ๊ฐ€ ๋  ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€ํ–ˆ์—ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” ์–ด๋• ์„๊นŒ? ๊ฒฝ์˜ ํšŒ์˜์—์„œ ๋…ผ์˜๊ฐ€ ๋งค์šฐ ํ™œ๋ฐœํ•ด์กŒ๋‹ค. ์ˆ˜์ต์„ ์˜ฌ๋ฆฌ๋ฉด ํฌ์ง€์…”๋‹์ด ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ๋“ฑ์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋‹ˆ๊นŒ ๋ณด๊ณ ๋„ ์ƒ์‚ฐ์„ฑ์ด ๋”ํ•ด์กŒ๋‹ค. ํ•˜์ง€๋งŒ ํ•ด๋‹น ์‚ฌ์—…๋ถ€์žฅ๋‹˜๋“ค์€ CEO/CFO์™€ ๋‹ฌ๋ฆฌ ๋งค์šฐ ๋ถ€๋‹ด์Šค๋Ÿฌ์›Œํ•˜์˜€๋‹ค. ์š”์ฆ˜์€ ์ข‹์€ BI ๋„๊ตฌ๋“ค์ด ๋งŽ์ด ์žˆ์œผ๋‹ˆ ๋” ํšจ๊ณผ์ ์œผ๋กœ ๋ณด์—ฌ์ค„ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ์ตœ์‹  ์†Œํ”„ํŠธ์›จ์–ด๋“ค์ด BCG ๋งคํŠธ๋ฆญ์Šค์˜ ํ•œ๊ณ„๋ฅผ ์กฐ๊ธˆ์”ฉ ๊ทน๋ณตํ•ด ์ฃผ๊ณ  ์žˆ๋Š”์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค. [1] Boston Consulting Group์—์„œ ๋งŒ๋“  ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ๋ถ„์„ ๋งคํŠธ๋ฆญ์Šค. [2] ์ €์ž๋„ ๋น„์Šทํ•œ ๊ฒฝํ—˜์„ ํ•œ ์ ์ด ์žˆ๋Š”๋ฐ ์‹œ๊ฐ„๊ณผ ๋…ธ๋ ฅ์„ ๋“ค์—ฌ์„œ ๋งŒ๋“ค์—ˆ์Œ์—๋„ ํฐ ๊ฐํฅ์ด ์—†๋‹ค. ์ฆ‰, ์ •๋ฆฌ๋Š” ์ž˜ ๋˜์—ˆ๋Š”๋ฐ ๋‚ด์šฉ์ƒ ์—ฌ๊ธฐ์ €๊ธฐ์„œ ์ด๋ฏธ ์กฐ๊ธˆ์”ฉ ๋“ค์€ ์ด์•ผ๊ธฐ๋“ค์ด๋ผ ์ „๋žต์  ํ†ต์ฐฐ๋ ฅ์„ ๋ถˆ๋Ÿฌ์ผ์œผํ‚ค๊ธฐ์—๋Š” 2% ๋ถ€์กฑํ•œ ๋ณด๊ณ ์„œ๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฐ ๊ฒƒ์ด๋‹ค. ๊ฒฝ์˜์ง„๋“ค์€ ์ƒ๋‹นํžˆ ๋งŽ์€ ์ •๋ณด๋ฅผ ์ˆ˜์‹œ๋กœ ๋“ฃ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ์œ ํ˜•์˜ ๋ถ„์„ ๋ณด๊ณ ์„œ๋Š” ์ ์‹œ์„ฑ๋ณด๋‹ค๋Š” ์ฐจ๋ณ„์  ์‹œ์‚ฌ์ ์„ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. [3] dart.fss.or.kr [4] US Securities and Exchange Commission. http://www.sec.gov [5] https://www.sec.gov/edgar/searchedgar/companysearch.html [6] B2C ๋งˆ์ผ€ํŒ…์—์„œ ๊ฐ•์กฐํ•˜๋Š” 4๊ฐ€์ง€ ํฌ์ง€์…”๋‹(Positioning)์€ โ€˜์ฒซ ๋ฒˆ์งธ, ๊ณ ๊ฐ์˜ ๊ธฐ์–ต ์†์— ์ตœ์ดˆ๊ฐ€ ๋ผ๋ผ. ๋‘ ๋ฒˆ์งธ, ํ˜„์žฌ์˜ ์œ„์ƒ ํŠนํžˆ, ์ผ๋“ฑ ๊ทธ๋ฃน์— ์†ํ•ด ์žˆ์Œ์„ ๊ฐ•์กฐํ•˜๋ผ. ์„ธ ๋ฒˆ์งธ, ๊ฒฝ์Ÿ ์ƒ๋Œ€๋ฅผ ์žฌํฌ์ง€์…”๋‹(Repositioning)ํ•˜๋ผ. ๋„ค ๋ฒˆ์งธ, ์ง€์†์ ์œผ๋กœ ์ง‘์ค‘ํ•˜๋ผโ€™๋กœ ์ •๋ฆฌ๋  ์ˆ˜ ์žˆ๋‹ค [7] 1997๋…„ ์• ํ”Œ ๊ฐœ๋ฐœ์ž ํšŒ์˜ ๋ฐœํ‘œ ๋‚ด์šฉ [8] ๊ณ ๊ฐ์˜ ์ฃผ์š” ์˜์‚ฌ๊ฒฐ์ •์ž์™€ ์ธํ„ฐ๋ทฐ๋ฅผ ์ง„ํ–‰ํ•˜๊ฑฐ๋‚˜ ์˜คํ”ผ๋‹ˆ์–ธ ๋ฆฌ๋”๋“ค(Opinion Leaders)๋“ค์„ ๋Œ€์ƒ์œผ๋กœ FGI(Focus Group Interview)๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค [9] ํ˜น์ž๋Š” B2B ๋งˆ์ผ€ํŒ…์—์„œ ์‹œ์žฅ์ ์œ ์œจ๋ณด๋‹ค ๊ณ ๊ฐ ์ ์œ ์œจ ๋˜๋Š” ๊ธฐํšŒ ์ ์œ ์œจ์ด ๋” ์ค‘์š”ํ•˜๋‹ค๊ณ  ๋งํ•œ๋‹ค. ๊ทธ ์ด์œ ๋Š” B2B ์‹œ์žฅ ์ „์ฒด๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š๊ณ  ์‹œ์žฅ ์ „์ฒด๋ณด๋‹ค๋Š” ํŒŒ๋ ˆํ†  ๋ฒ•์น™์— ์˜ํ•ด ๋งค์ถœ์˜ ๋Œ€๋ถ€๋ถ„์„ ์ฐจ์ง€ํ•˜๋Š” ๊ณ ๊ฐ(๊ตฐ)์„ ์–ผ๋งˆ๋‚˜ ์ ์œ ํ•˜๋Š๋ƒ๊ฐ€ ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค [10] ์ƒ๋Œ€์  ์‹œ์žฅ์ ์œ ์œจ์˜ ๊ฒฝ์šฐ, ๊ฒฝ์Ÿ์‚ฌ์˜ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ณต์‹œ ์ •๋ณด ์™ธ์— ๊ฒฝ์Ÿ์‚ฌ์˜ ๊ฒฝ์˜์‹ค์ ์„ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์ˆ˜์‹œ๋กœ ํŒŒ์•…ํ•˜๋Š” ์ผ์€ ์‰ฝ์ง€ ์•Š๊ธฐ์— ํ™œ์šฉํ•˜๊ธฐ ์‰ฝ์ง€ ์•Š๋‹ค [11] Ansoff Matrix. ์‹œ์žฅ์„ ๊ณ ๊ฐ๊ณผ ์ œํ’ˆ์˜ ์‚ฌ๋ถ„๋ฉด์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ๋‹จ๊ธฐ, ์ค‘๊ธฐ, ์žฅ๊ธฐ ๊ด€์ ์—์„œ ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š”์ง€ ์„ค๋ช… 07. ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—…๋ถ„์„(3/4) ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„์˜ ์„ธ ๋ฒˆ์งธ ์‹œ๊ฐ„์ด๋‹ค. ์ง€๋‚œ๋ฒˆ BCG ๋งคํŠธ๋ฆญ์Šค์— ์ด์–ด ์ž์‚ฌ ๋ถ„์„์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋ฉฐ ๊ทธ๊ฒƒ์„ ํ™•์žฅํ•ด ์‚ฐ์—…์— ๋Œ€ํ•ด ๊ณ ์ฐฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ถ„์„ ๋ฒ•์„ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. 1.5 Five Forces Model ๋งˆ์ดํด ํฌํ„ฐ ๊ต์ˆ˜์™€ ๊ทธ์˜ ์ €์„œ '5 Forces Model'์€ ์‚ฌ์—… ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๊ธฐ ์œ„ํ•ด ์‹œ์žฅ๊ณผ ๊ฒฝ์Ÿ ์ฆ‰, ์‚ฐ์—… ํ™˜๊ฒฝ์„ ์กฐ๋งํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ถ„์„ ๋ฐฉ๋ฒ•์œผ๋กœ ๋„๋ฆฌ ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ๋งˆ์ดํด ํฌํ„ฐ(Michael E. Porter. 1947 ~ ํ˜„์žฌ)์˜ โ€˜5 forces modelโ€™ ๋ถ„์„ ์ด์™ธ์—๋„ โ€˜Value Chan ๋ถ„์„โ€™์„ ๋น„๋กฏํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์‚ฐ์—…๋ถ„์„ ๊ธฐ๋ฒ•๋“ค์„ ์ œ์•ˆํ•˜์˜€๊ณ  ๋„๋ฆฌ ์ด๊ฒƒ๋“ค์€ ์•Œ๋ ค์กŒ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ ์ด ๊ธฐ๋ฒ•๋“ค์€ ์˜ค๋Š˜๋‚  ์ปจ์„คํŒ… ํ˜„์žฅ์—์„œ ์ž˜ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํšจ๊ณผ์ ์ธ ๊ธฐ๋ฒ•์ด๋ผ๊ณ  ๊ถŒ๊ณ ํ•˜๊ธฐ๋Š” ์–ด๋ ค์šด '๊ณ ์ „์ ์ธ ๊ฒฝ์˜์ „๋žต ๋ถ„์„ ๊ธฐ๋ฒ•๋“ค'์ด๋‹ค. ๋…ธ๋ ฅ(Efforts) ๋Œ€๋น„ ํ™œ์šฉ์„ฑ์ด ๋–จ์–ด์ง€๋ฉฐ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ƒํ™ฉ์ด ๋ณ€ํ•˜๋ฉฐ ์ˆ˜๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์Ÿ์•„์ ธ ๋‚˜์˜ค๋Š” ์‹œ์žฅ๊ณผ ๊ฒฝ์Ÿ์˜ ๋™ํƒœ์ ์ธ(dynamic) ํŠน์„ฑ์„ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•˜๋Š” ์ •์ ์ธ(static) ์‚ฌ์—… ํ˜„ํ™ฉ์˜ ์Šค๋ƒ…์ƒท(snapshot)๋งŒ์„ ์ œ์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌผ๋ฆฌํ•™์—์„œ๋„ ์•„์ธ์Šˆํƒ€์ธ(Albert Einstein.1879 ~ 1955)์ด ์˜ณ์ง€๋งŒ ๊ฐ€์ •์— ๊ฐ€์ •์„ ๋”ํ•˜๋ฉด ๋‰ดํ„ด(Issac Newton. 1643 ~ 1727) ์—ญํ•™๋„ ์—ฌ์ „ํžˆ ํ˜„์‹ค์—์„œ ์ ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๊ฒฝ์˜ํ•™์˜ ํ•œ ์‹œ๋Œ€๋ฅผ ํ’๋ฏธํ•˜์˜€์œผ๋ฉฐ, ๊ฒฝ์Ÿ ์ „๋žต์˜ ์ง€ํ‰์„ ์—ด์—ˆ๊ณ  ํ•™๋ฌธ์ ์œผ๋กœ๋„ ํฐ ์˜์˜๋ฅผ ๋ถ€์—ฌํ•˜๊ธฐ์— ๊ฐ„๋žตํžˆ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•œ๋‹ค. 1980๋…„ ํ•˜๋ฒ„๋“œ ๋Œ€ํ•™์˜ ๋งˆ์ดํด ํฌํ„ฐ ๊ต์ˆ˜๋Š” ๊ทธ์˜ ์ €์„œ โ€˜Competitive Strategyโ€™์—์„œ ์‚ฐ์—…์˜ ๊ตฌ์กฐ์  ๋งค๋ ฅ ๋„์™€ ๊ทธ ์‚ฐ์—…์˜ ํ•ต์‹ฌ ์„ฑ๊ณต์š”์†Œ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด 5๊ฐ€์ง€ ์š”์†Œ๋“ค - ์ž ์žฌ์  ๊ฒฝ์Ÿ์ž์™€ ๋Œ€์ฒด์žฌ์˜ ์œ„ํ˜‘, ๊ธฐ์กด ์‚ฐ์—… ๊ฐ„์˜ ๊ฒฝ์Ÿ, ๊ณต๊ธ‰์ž์™€ ์†Œ๋น„์ž์˜ ํ˜‘์ƒ๋ ฅ-์„ ์‚ดํŽด๋ณด์•„์•ผ ํ•œ๋‹ค๊ณ  ์ฃผ์žฅํ•˜์˜€๊ณ  ์ด ์š”์†Œ๋“ค์„ โ€˜Five Forcesโ€™๋ผ๊ณ  ์นญํ•˜์˜€๋‹ค. ๊ธฐ์—…์„ ๋‘๊ณ  ์ƒ์‚ฐ์ž์™€ ๊ณต๊ธ‰์ž์˜ ํ˜‘์ƒ๋ ฅ์„ ๋ถ„์„ํ•˜๋Š” ๊ธฐ๋ณธ ๊ฐœ๋…(2-Model)์— ๊ฒฝ์Ÿ์ž๋‚˜ ๋Œ€์ฒด์žฌ์˜ ์œ„ํ˜‘์ด ์žˆ์œผ๋ฉด ์†Œ๋น„์ž์˜ ํ˜‘์ƒ๋ ฅ์ด ์˜ฌ๋ผ๊ฐ„๋‹ค๋Š” ๊ฐœ๋…๊นŒ์ง€ ํ™•๋Œ€๋  ์ˆ˜ ์žˆ๋‹ค. Figure III-12๋Š” ๋„๋ฆฌ ์•Œ๋ ค์ ธ ์žˆ๋Š” Five Forces Model์˜ ๊ฐœ๋…์ด๋‹ค. Figure III-13. Five Forces Model (1) ์‹ ๊ทœ ์ง„์ž…์ž์˜ ์œ„ํ˜‘(Threat of New Entrants) ์‹ ๊ทœ ์‚ฌ์—…์ž๋กœ์„œ ์–ด๋–ค ์‚ฐ์—…์— ์ง„์ถœํ•˜๊ณ ์ž ํ•  ๋•Œ์—๋Š” ๊ทธ ์‚ฐ์—…์˜ ์ง„์ž… ์žฅ๋ฒฝ(Entry Barriers)์„ ๊ทน๋ณตํ•ด์•ผ ํ•œ๋‹ค. ๊ธฐ์กด ์‚ฌ์—…์ž ์ž…์žฅ์—์„œ๋Š” ํƒ€ ์‚ฌ์—…์ž๊ฐ€ ํ•ด๋‹น ์‚ฐ์—…์— ์ง„์ถœํ•˜์ง€ ๋ชปํ•˜๋„๋ก ์ง„์ž… ์žฅ๋ฒฝ์„ ๋†’๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๊ทธ ์‚ฐ์—…์„ ๊ตฌ์กฐ์ ์œผ๋กœ ๋งค๋ ฅ์ ์ด๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ฆ‰, ๋…๊ณผ์  ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค์–ด ๋†’์€ ์ˆ˜์ต๋ฅ ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์™€๋„ ์ผ๋งฅ์ƒํ†ตํ•œ๋‹ค. ๋„๋ฆฌ ์•Œ๋ ค์ง„ ์ง„์ž… ์žฅ๋ฒฝ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค. ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ(Economies of Scale): ์ง„์ž… ์žฅ๋ฒฝ์„ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฐฉ์•ˆ์€ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์–ด๋–ค ์ œํ’ˆ์„ ๋Œ€๋Ÿ‰ ์ƒ์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ์‹œ์Šคํ…œ์„ ๊ฐ–์ถค์œผ๋กœ์จ ์ถ”๊ฐ€ ์ƒ์‚ฐ ์‹œ ํ‰๊ท  ์›๊ฐ€๋ฅผ ๋–จ์–ด๋œจ๋ฆด ์ˆ˜ ์žˆ๋Š” ์—ญ๋Ÿ‰์„ ํ™•๋ณดํ•˜๊ณ  ์žˆ์„ ๋•Œ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ๋ฅผ ํ™•๋ณดํ–ˆ๋‹ค๊ณ  ํ•œ๋‹ค. ํ‘œ์ค€ํ™”๋œ ์ œํ’ˆ์˜ ๋Œ€๋Ÿ‰ ์ƒ์‚ฐ(Mass Production)[1]์„ ํ†ตํ•ด ์›๊ฐ€๋ฅผ ์ ˆ๊ฐํ•˜๊ณ ,<NAME>๋ฃŒ๋‚˜ ๋ถ€ํ’ˆ์˜ ๋Œ€๋Ÿ‰ ๊ตฌ๋งค๋ฅผ ํ†ตํ•œ ์›์ž์žฌ ๋น„์šฉ ํ• ์ธ, ๋งŽ์€ ์ˆ˜์˜ ์ œํ’ˆ์— ๋Œ€ํ•œ ๊ณ ์ • ์›๊ฐ€ ๋ฐฐ๋ถ„, ์ƒ์‚ฐ ์„ค๋น„ ํ™œ์šฉ์˜ ๊ทน๋Œ€ํ™”, ๋Œ€๋Ÿ‰ ๊ด‘๊ณ ์˜ ํšจ๊ณผ ๋“ฑ์—์„œ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ๋ฅผ ์ถ”๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ธŒ๋žœ๋“œ ๋กœ์—ดํ‹ฐ(Brand Loyalty): ์‚ฐ์—… ๋‚ด ๊ธฐ์กด ๊ธฐ์—…์˜ ์ œํ’ˆ์— ๋Œ€ํ•œ ์„ ํ˜ธ๋„(preference)๋ฅผ ๋งํ•œ๋‹ค. ์„ ํ˜ธ๋„๊ฐ€ ๋†’์œผ๋ฉด ์‹ ๊ทœ ์ง„์ž… ๊ธฐ์—…์€ ๊ธฐ์กด ๊ธฐ์—…์˜ ์‹œ์žฅ ์ ์œ ๋ฅผ ๋นผ์•—๊ธฐ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ธŒ๋žœ๋“œ ๋กœ์—ดํ‹ฐ๊ฐ€ ์ง„์ž… ์žฅ๋ฒฝ์œผ๋กœ์„œ ์˜ํ–ฅ๋ ฅ์„ ๋ฐœํœ˜ํ•˜๋Š” ์‚ฐ์—…์€ R&D ์ง‘์•ฝ์  ์‚ฐ์—…๊ณผ ๊ด‘๊ณ  ์ง‘์•ฝ์  ์‚ฐ์—…์„ ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ด ๋“ค ์‚ฐ์—…์˜ ํŠน์ง•์€ ์‹ ๊ทœ ์ง„์ž…์ž๋„ ๋ง‰๋Œ€ํ•œ R&D๋‚˜ ๊ด‘๊ณ  ๋น„์šฉ์„ ์Ÿ์•„๋ถ€์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์—… ์‹คํŒจ ์‹œ ๋ฐœ์ƒํ•˜๋Š” ๋งค๋ชฐ ๋น„์šฉ(Sunk Cost)๋„ ์ปค์ง„๋‹ค. ์ฆ‰, ํ•œ ๋ฒˆ ๋ฐœ ๋‹ด๊ทธ๋ฉด ๋น ์ ธ๋‚˜์˜ค๊ธฐ ์–ด๋ ต๋‹ค. ์ ˆ๋Œ€์  ๋น„์šฉ ์šฐ์œ„(Absolute Cost Advantage): ์„ ํ–‰ ํˆฌ์ž๋กœ ์ธํ•œ ๋น„์šฉ ์šฐ์œ„๋ฅผ ๋œปํ•œ๋‹ค. ์ฆ‰, ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ๋ˆ์ด ๋งŽ์•„์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ธฐ์กด ์‚ฌ์—…์ž๊ฐ€ ํ•ต์‹ฌ ๊ธฐ์ˆ ์ด๋‚˜ ํŠนํ—ˆ๋ฅผ ๋ณด์œ ํ•˜๊ณ  ์žˆ์„ ํ™•๋ฅ ์ด ๋†’๊ธฐ ๋•Œ๋ฌธ์— ์ง„์ž…์ž๋Š” ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ์—ดํ‹ฐ๋ฅผ ์ง€๊ธ‰ํ•˜๊ฑฐ๋‚˜ ๋ผ์ด์„ ์‹ฑ์„ ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ๋ชปํ•  ๊ฒฝ์šฐ, ๋Œ€์ฒด ๊ธฐ์ˆ ์„ ์ง์ ‘ ๊ฐœ๋ฐœํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด ๊ฒฝ์šฐ ๋”์šฑ ๋งŽ์€ ๋น„์šฉ์ด ๋ฐœ์ƒํ•  ํ™•๋ฅ ์ด ๋†’๋‹ค.(First Mover's Advantage) ๋˜ํ•œ, ๊ธฐ์กด ์‚ฌ์—…์ž๊ฐ€ ์œ ํ†ต๋ง๋„ ์žฅ์•…ํ•˜๊ณ  ์žˆ์„ ๊ฒฝ์šฐ ์กฐ๋‹ฌ ๋น„์šฉ๋„ ์ฆ๊ฐ€ํ•˜๋ฏ€๋กœ ๊ฒฐ๊ตญ ์„ ํ–‰ํˆฌ์ž๋ฅผ ํ†ตํ•œ ๋น„์šฉ ์šฐ์œ„ ์š”์†Œ๊ฐ€ ๋˜ ๋‹ค๋ฅธ ์ง„์ž… ์žฅ๋ฒฝ์„ ๋งŒ๋“ค๊ฒŒ ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. [2] ์ •๋ถ€ ๊ทœ์ œ(Government Regulations): ์ •๋ถ€ ๊ทœ์ œ์˜ ํ™œ์šฉ์€ ๋ฒ•/์ œ๋„๋ฅผ ํ†ตํ•ด ํ•ฉ๋ฒ•์ ์œผ๋กœ ์ง„์ž…์žฅ๋ฒฝ์„ ๊ฐ€์žฅ ํ™•์‹คํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ตญ๊ฐ€ ๊ฒฝ์ œ์ •์ฑ…๊ณผ ๋งฅ๋ฝ์„ ๊ฐ™์ด ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€๋ฐ ์ตœ๊ทผ ํ™˜๊ฒฝ ๊ทœ์ œ์™€ ๊ด€๋ จํ•˜์—ฌ ์œ ๋Ÿฝ์—ฐํ•ฉ(EU)์—์„œ ์ž๋™์ฐจ ์‚ฐ์—…์— ์ ์šฉํ•˜๊ณ  ์žˆ๋Š” EURO 6 ๊ฐ™์€ ๊ฒƒ์ด ๋Œ€ํ‘œ์ ์ด๋‹ค. EURO 6๋Š” ๊ฒฝ์œ ์ฐจ ๋ฐฐ๊ธฐ๊ฐ€์Šค ๊ทœ์ œ ๋‹จ๊ณ„์˜ ๋ช…์นญ์œผ๋กœ 1992๋…„ EURO 1 ๋„์ž…๋ถ€ํ„ฐ 2013๋…„ EURO 6์— ์ด๋ฅด๊ธฐ๊นŒ์ง€ ์ง€์†์ ์œผ๋กœ ์งˆ์†Œ์‚ฐํ™”๋ฌผ์˜ ๋ฐฐ์ถœ์— ๋Œ€ํ•ด ๊ทœ์ œํ•˜๋Š” ๊ฒƒ์ธ๋ฐ, EURO 6๋Š” EURO 5๋ณด๋‹ค 30~50% ๋” ๊ฐ์†Œ์‹œ์ผœ์•ผ ํ•˜๋Š” ๊นŒ๋‹ญ์— ์ž๋™์ฐจ ํšŒ์‚ฌ๋“ค์€ ์‹ ํ˜• ์—”์ง„์„ ์žฅ์ฐฉํ•˜๊ฑฐ๋‚˜ ๋ณ„๋„์˜ ์ €๊ฐ์žฅ์น˜๋ฅผ ์žฅ์ฐฉํ•˜๊ณ , ์ฐจ๋Ÿ‰์˜ ๋ฌด๊ฒŒ๋ฅผ ์ค„์ด๋Š” ๋“ฑ ๋‹ค์–‘ํ•œ ๋…ธ๋ ฅ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์ด ๊ทœ์ œ๋ฅผ ์ถฉ์กฑ์‹œํ‚ค๋Š” ์ฐจ๋Ÿ‰๋งŒ์ด ์œ ๋Ÿฝ์—ฐํ•ฉ์— ์ฐจ๋Ÿ‰ ์ˆ˜์ถœ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๊ตญ๋‚ด ์ž๋™์ฐจ ์—…๊ณ„๋„ ๊ฑฐ์„ผ ๋„์ „์„ ๋ฐ›๊ณ  ์žˆ๋‹ค. ๊ฐ ๋‚˜๋ผ๋งˆ๋‹ค ์†Œ์œ„ ์‚ฐ์—…ํ™” ์‹œ๋Œ€๋ฅผ ์ง€์นญํ•˜๋Š” ์‹œ๊ธฐ๊ฐ€ ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ ๊ธฐ๊ฐ„ ๋™์•ˆ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ๋ฅผ ์ด๋ฃจ๊ฒŒ ๋œ ๊ธฐ์—…๋“ค์ด๋‚˜ ์ •๋ถ€ ๊ทœ์ œ๋ฅผ ์ ๊ทน ํ™œ์šฉํ•˜๋Š” ๊ธฐ์—…๋“ค์„ ๋ณด๋ฉด ๊ฑฐ์˜ ๊ธ€๋กœ๋ฒŒ ๋‹ค๊ตญ์  ๊ธฐ์—…, ํ•œ๊ตญ์˜ ๊ฒฝ์šฐ๋Š” ๋Œ€๊ธฐ์—…๋“ค์ด๋‹ค. (2) ๊ธฐ์กด ๊ธฐ์—… ๊ฐ„์˜ ๊ฒฝ์Ÿ(Rivalry among the existing firms) Five Forces์˜ ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋Š” ์‚ฐ์—… ๋‚ด ๊ธฐ์กด ๊ธฐ์—… ๊ฐ„์˜ ๊ฒฝ์Ÿ์œผ๋กœ ์‚ฐ์—…์˜ ๊ฒฝ์Ÿ ๊ตฌ์กฐ, ์ˆ˜์š” ์กฐ๊ฑด, ํ‡ด์ถœ ์žฅ๋ฒฝ ๋“ฑ์ด ์žˆ๋‹ค. ์‚ฐ์—…์˜ ๊ฒฝ์Ÿ ๊ตฌ์กฐ(Industry Competitive Structure): ์‚ฐ์—…์˜ ๊ฒฝ์Ÿ ๊ตฌ์กฐ๋Š” ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ๋น„์Šทํ•œ ์—…์„ ํ•˜๋Š” ๊ธฐ์—…์ด ์‚ฐ์—… ๋‚ด ์†Œ์ˆ˜์ด๋ƒ ๋‹ค์ˆ˜์ด๋ƒ๋ฅผ ํ†ตํ•ด ๊ฒฝ์Ÿ ๊ฐ•๋„๊ฐ€ ๋‹ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ์‚ฐ์—… ๋‚ด ํฌ์ง„ํ•œ ๊ธฐ์—…๋“ค์˜ ๋ฐ€์ง‘๋„์— ๋”ฐ๋ผ 'Fragmented ์‚ฐ์—…' ๋˜๋Š” 'Consolidated ์‚ฐ์—…'์ด๋ผ๊ณ  ๋งํ•˜๋Š”๋ฐ ์†Œ๊ทœ๋ชจ ๋‹ค์ˆ˜ ๊ธฐ์—…์ด ํฌ์ง„ํ•œ ์‚ฐ์—…์„ fragmented ์‚ฐ์—…, ๋…๋ณด์ ์ธ 2~3๊ฐœ ๊ธฐ์—…์ด ํฌ์ง„ํ•œ ์‚ฐ์—…์„ Consolidated ์‚ฐ์—…์ด๋ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด fragmented ์‚ฐ์—…์€ ๋…ธ๋ž˜๋ฐฉ, ๋ถ€๋™์‚ฐ ์ค‘๊ฐœ์†Œ ๊ฐ™์€ ๊ฒƒ์ด๊ณ  consolidated ์‚ฐ์—…์€ ์ž๋™์ฐจ, ๊ฐ€์ „๊ธฐ์—… ๊ฐ™์€ ๊ฒƒ์„ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ํ•œ๊ตญ์˜ ๊ณต์ •๊ฑฐ๋ž˜์œ„์›ํšŒ์—์„œ๋Š” '3์‚ฌ ์ง‘์ค‘๋„ ์ง€ํ‘œ(3 firms concentration)'๋ผ๋Š” ๊ฒƒ์„ ๋ฐœํ‘œํ•˜๋Š”๋ฐ ์ด๋Š” ์‚ฐ์—… ๋‚ด ๋…๊ณผ์  ๋˜๋Š” ๋‹ดํ•ฉ์˜ ๊ฐœ์—ฐ์„ฑ์„ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๋ ค๋Š” ์˜๋„์—์„œ ๋งŒ๋“  ๊ฒƒ์ด๋‹ค. ์‚ฐ์—… ๋‚ด ์ƒ์œ„ 3๊ฐœ ๊ธฐ์—…์˜ ์‹œ์žฅ์ ์œ ์œจ์ด ๋†’๋‹ค๋ฉด ๋…๊ณผ์ , ๋‹ดํ•ฉ์ด๋ผ ์˜์‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฐ€์ •์ด๋‹ค. ๊ฒฝ์Ÿ ๊ตฌ์กฐ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ์š”์ธ์œผ๋กœ '์ œํ’ˆ์ฐจ๋ณ„ํ™”'๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ์‚ฐ์—… ๋‚ด์—์„œ ๊ฒฝ์Ÿํ•˜๋Š” ๊ธฐ์—…๋“ค์ด ์ œํ’ˆ์˜ ๋””์ž์ธ์ด๋‚˜ ํ’ˆ์งˆ๋ฉด์—์„œ ๋™์ผํ• ์ˆ˜๋ก ์†Œ๋น„์ž๋“ค์€ ํŠน์ • ํšŒ์‚ฌ ์ œํ’ˆ์„ ์„ ํ˜ธํ•  ์ด์œ ๊ฐ€ ์—†์–ด์ง„๋‹ค. ๋”ฐ๋ผ์„œ ๊ธฐ์—… ์ž…์žฅ์—์„œ๋Š” ๋™์งˆํ™”๋œ ์ œํ’ˆ๋“ค์— ๋Œ€ํ•ด์„œ ๊ฐ€๊ฒฉ ์™ธ์—๋Š” ๊ฒฝ์Ÿํ•  ๋งŒํ•œ ์ˆ˜๋‹จ์ด ์—†์–ด์ง€๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋Ÿฌํ•œ ์ œํ’ˆ์„ ์ผ์ƒ์žฌ(commodity)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๊ธฐ์—…์˜ ์ œํ’ˆ์ด ์ผ์ƒ ์žฌ๊ฐ€ ๋˜๋Š” ์ˆœ๊ฐ„, ๊ฐ€๊ฒฉ ๊ฒฝ์Ÿ์— ๋Œ์ž…ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ํ•ด๋‹น ์ œํ’ˆ์˜ ์ˆ˜์ต๋ฅ ์€ ๋‚ฎ์•„์ง€๊ฒŒ ๋œ๋‹ค. Table III-6. ์‚ฐ์—…์˜ ๊ฒฝ์Ÿ ๊ตฌ์กฐ ์ˆ˜์š” ์กฐ๊ฑด(Demand Condition): ์ˆ˜์š” ์กฐ๊ฑด์€ '์ˆ˜์š”์— ๋”ฐ๋ฅธ ๊ฒฝ์Ÿ์˜ ์–‘์ƒ์„ ์•Œ์•„๋ณด๋Š” ๊ฒƒ'์œผ๋กœ ์ˆ˜์š”์™€ ๊ฒฝ์Ÿ์€ ๋ฐ˜๋น„๋ก€ ๊ด€๊ณ„์— ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค. ์ด๊ฒƒ์€ ์‹œ์žฅ์˜ ํฌ๊ธฐ๊ฐ€ ์ผ์ •ํ•˜๋‹ค๊ณ  ํ•˜๋Š” ์•”๋ฌต์  ๊ฐ€์ •์ด ์žˆ๋‹ค. ์‹ค์ œ๋กœ ์ˆ˜์š”๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด ์‹œ์žฅ๋„ ํ™•์žฅ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์น˜์—ดํ•œ ๊ฒฝ์Ÿ ์—†์ด ๊ธฐ์—…์˜ ๋งค์ถœ ์ฆ๊ฐ€๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ด์•ผ๊ธฐ๋„ ๋œ๋‹ค. ์ด๋Š” ์ „๋žต ๊ฒฝ์˜(Strategic Management)์˜ ๋ชฉ์ ์ด ๊ฐ€์น˜ ์ฐฝ์ถœ(Value Creation) ์ฆ‰, '์‹œ์žฅ์˜ ํฌ๊ธฐ(Pie)๋ฅผ ํ‚ค์šฐ์ž'๋ผ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด์œค ์ฐฝ์ถœ๋กœ ํ•œ์ •ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฐ ์ธก๋ฉด์—์„œ ์ˆ˜์š” ์กฐ๊ฑด์€ ์ œํ•œ์ ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ‡ด์ถœ ์žฅ๋ฒฝ(Exit Barriers): ํ‡ด์ถœ ์žฅ๋ฒฝ์€ ํ•œ ์‚ฐ์—…์—์„œ ์–ด๋–ค ๊ธฐ์—…์ด ์‚ฌ์—…์„ ์ฒ ์ˆ˜ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์ด๋ฅผ ๋ฐฉํ•ดํ•˜๋Š” ์žฅ๋ฒฝ์„ ๋งํ•œ๋‹ค. ํ‡ด์ถœ ์žฅ๋ฒฝ์ด ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์€ ์ˆ˜์š”๊ฐ€ ์ค„์–ด๋“œ๋Š” ์ˆœ๊ฐ„์ด๋‹ค. ์ˆ˜์š”๊ฐ€ ์ค„๊ฑฐ๋‚˜ ์ •์ฒด๋  ๋•Œ ํ‡ด์ถœ ์žฅ๋ฒฝ์ด ๋†’์œผ๋ฉด ํ•ด๋‹น ์‚ฐ์—…์—์„œ ์ดํƒˆํ•  ์ˆ˜๊ฐ€ ์—†์–ด ๊ณผ์ž‰๊ณต๊ธ‰ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€๊ฒฉ๊ฒฝ์Ÿ์„ ์ผ์œผํ‚ค๊ฒŒ ๋œ๋‹ค. ํ‡ด์ถœ ์žฅ๋ฒฝ์˜ ์ข…๋ฅ˜๋กœ ๋ณดํ†ต 3๊ฐ€์ง€๋ฅผ ๋งŽ์ด ์ด์•ผ๊ธฐํ•˜๋Š”๋ฐ ๊ฒฝ์ œ์  ํ‡ด์ถœ ์žฅ๋ฒฝ, ์ „๋žต์  ํ‡ด์ถœ ์žฅ๋ฒฝ, ๊ฐ์ •์  ํ‡ด์ถœ ์žฅ๋ฒฝ์ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๊ฒฝ์ œ์  ํ‡ด์ถœ ์žฅ๋ฒฝ์€ ๋Œ€๊ทœ๋ชจ ์‚ฐ์—… ์„ค๋น„์ฒ˜๋Ÿผ ํˆฌ์ž ์ž์‚ฐ์ด ํด ๊ฒฝ์šฐ ๋ฐœ์ƒํ•˜๋Š” ๋ฐ ์ด๋ฅผ ์‰ฝ๊ฒŒ ํŒ”๊ธฐ๋„ ์–ด๋ ต๊ณ  ๊ณ„์† ๋ณด์œ ํ•˜๋ฉด ๊ฐ€๋ณ€๋น„์šฉ๋งŒ ๋ฐœ์ƒํ•˜๊ณ  ์ƒ์‚ฐ์€ ํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜๋ฏ€๋กœ ๊ธฐ์กด ์žฌ๊ณ  ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ€๊ฒฉ ๊ฒฝ์Ÿ์„ ์‹œ๋„ํ•˜๊ฒŒ ๋œ๋‹ค. ์ „๋žต์  ํ‡ด์ถœ ์žฅ๋ฒฝ์€ ํƒ€ ์‚ฐ์—…๊ณผ์˜ ์‹œ๋„ˆ์ง€(Synergy)๊ฐ€ ๋งŽ์•„์„œ ํ•œ ์ชฝ ์‚ฐ์—…์„ ํฌ๊ธฐํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋ฉด๋„๊ธฐ, ์‰์ด๋น™ ํผ, ์• ํ”„ํ„ฐ ์‰์ด๋ธŒ ์‚ฌ์—…์˜ ๊ด€๊ณ„์ด๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ์‚ฐ์—…์ด์ง€๋งŒ ๊ต์ฐจํŒ๋งค(Cross-selling), ์—ฐ์‡„ํŒ๋งค(Up-selling) ๋“ฑ์„ ํ†ตํ•ด ์‚ฌ์—… ๊ฐ„ ์‹œ๋„ˆ์ง€๊ฐ€ ์ƒ๋‹นํžˆ ํฌ๋‹ค. ๊ฐ์ •์  ํ‡ด์ถœ ์žฅ๋ฒฝ์€ ๋ณดํ†ต ์ฒ˜์Œ ์‹œ์ž‘ํ•œ ์‚ฌ์—…์œผ๋กœ ์‰ฝ๊ฒŒ ์ฒ ์ˆ˜ํ•˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. '๊ณ ์กฐํ• ์•„๋ฒ„์ง€๊ฐ€ ์‹œ์ž‘ํ•˜์…”์„œ 5๋Œ€์— ๊ฑธ์ณ ํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์—… .... ' ๋ญ ์ด๋Ÿฐ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ ์—ฐ์œ ๋กœ ์‚ฌ์—…์ ์œผ๋กœ ๋งค๋ ฅ์ ์ด์ง€ ๋ชปํ•˜์ง€๋งŒ ์‰ฝ๊ฒŒ ํ‡ด์ถœ์‹œํ‚ค์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์ตœ๊ทผ์—๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ํ˜์‹  ๋˜๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ์žฌ์ฐฝ์กฐ(Business Reinvention)์„ ํ†ตํ•ด ๊ธฐ์กด๊ณผ ์ „ํ˜€ ๋‹ค๋ฅธ ์‚ฐ์—…์œผ๋กœ ํƒˆ๋ฐ”๊ฟˆํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. ์‚ผ์ง„ ์–ด๋ฌต ๊ฐ™์€ ๊ฒฝ์šฐ, ๋ถ€์‚ฐ์˜ ์œ ์„œ ๊นŠ์€ ์–ด๋ฌต ๊ณต์žฅ์ด ์–ด๋ฌต ์ž์ฒด์˜ ์ˆ˜์š”๊ฐ€ ๊ฐ์†Œํ•˜๋ฉด์„œ ๊ณต์žฅ์„ ์œ ์ง€ํ•˜๊ธฐ ์–ด๋ ค์›Œ์กŒ์„ ๋•Œ ์†Œ์œ ์ฃผ์˜ ์•„๋“ค์ด ๋ฒ ์ด์ปค๋ฆฌ ๊ฐœ๋…์„ ๋„์ž…ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์–ด๋ฌต ์ƒํ’ˆ์„ ์ œ์กฐ, ํŒ๋งคํ•˜๊ณ  ์ฒดํ—˜๊ด€์„ ๋งŒ๋“ค์–ด ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ์–ด๋ฌต์˜ ์—ญ์‚ฌ๋ฅผ ์•Œ๋ฆฌ๋Š” ํ™œ๋™๋„ ํ•˜๋ฉด์„œ ์œ ๋ช…ํ•ด์ ธ ๋ถ€์‚ฐ์˜ ์–ด๋ฌต ์‚ฐ์—…์„ ๋‹ค์‹œ ํ™œ์„ฑํ™”์‹œํ‚จ ์‚ฌ๋ก€๋กœ ์œ ๋ช…ํ•˜๋‹ค. (3) ๊ตฌ๋งค์ž์˜ ํ˜‘์ƒ๋ ฅ(Bargaining power of Buyers) ์‚ฐ์—… ๊ตฌ์กฐ์˜ ์„ธ ๋ฒˆ์งธ Force๋Š” ๊ตฌ๋งค์ž์˜ ํ˜‘์ƒ๋ ฅ์ด๋‹ค. ๊ตฌ๋งค๋ ฅ(Buying Power)์ด๋ผ๊ณ ๋„ ํ•˜๋Š”๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ 2๊ฐ€์ง€๊ฐ€ ๊ตฌ๋งค์ž์˜ ๊ต์„ญ๋ ฅ์„ ๊ฒฐ์ •ํ•œ๋‹ค. 1) ์ œํ’ˆ์ฐจ๋ณ„ํ™”๊ฐ€ ์‹ฌํ• ์ˆ˜๋ก ๊ตฌ๋งค์ž๋Š” ๊ฐ€๊ฒฉ์— ๋ฏผ๊ฐํ•˜์ง€ ์•Š๋‹ค. - ํ˜์‹ ์  ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ์•„์ดํฐ(i-Phone)์— ๋Œ€ํ•ด ๊ทธ๊ฒƒ์ด ํ•˜์ด์—”๋“œ(High-End) ์ œํ’ˆ์ž„์ด์–ด๋„ ๊ตฌ์ž…ํ•œ๋‹ค. 2) ์ด๋ฅผ B2B ์‚ฌ์—…์œผ๋กœ ์˜ฎ๊ฒจ๋ณด๋ฉด ํŒ๋งคํ•˜๋Š” ๊ธฐ์—…๊ณผ ๊ตฌ๋งคํ•˜๋Š” ๊ธฐ์—… ๊ฐ„์˜ ํ˜‘์ƒ๋ ฅ์˜ ์ฐจ์ด๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. ์ด ํž˜(Power)๋Š” ๊ตฌ๋งค์ž์˜ ๊ณต๊ธ‰์ž์— ๋Œ€ํ•œ ์ƒ๋Œ€์  ํฌ๊ธฐ์ด๋‹ค. ์‚ฐ์—… ๊ตฌ์กฐ๊ฐ€ ์ œํ’ˆ์„ ๊ณต๊ธ‰ํ•˜๋Š” ๋งŽ์€ ๊ธฐ์—…๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ  ์ˆ˜์š”๋Š” ์ž‘์ง€๋งŒ ๊ทธ ๊ตฌ๋งค ๊ทœ๋ชจ๊ฐ€ ํด ๋•Œ ๊ตฌ๋งค์ž๊ฐ€ ๋งŽ์€ ์–‘์„ ๊ตฌ๋งคํ•  ๋•Œ ๊ณต๊ธ‰ ์‚ฐ์—…์ด ์ „์ฒด ํŒ๋งค ๋ถ€๋ถ„์„ ํŠน์ • ๊ตฌ๋งค์ž์—๊ฒŒ ์˜์กดํ•  ๋•Œ ๊ตฌ๋งค์ž๊ฐ€ ๊ณต๊ธ‰์ž๋ฅผ ๋‚ฎ์€ ๋น„์šฉ์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์„ ๋•Œ (๋‚ฎ์€ Switching Cost) ๊ตฌ๋งค์ž๊ฐ€ ๊ณต๊ธ‰์ž์˜ ์ œํ’ˆ, ๊ฐ€๊ฒฉ, ๋น„์šฉ ๊ตฌ์กฐ ๋“ฑ ํ•ด๋‹น ์‚ฐ์—…์— ๋Œ€ํ•ด ์ž˜ ์•Œ๊ณ  ์žˆ์„ ๋•Œ ๊ตฌ๋งค์ž๊ฐ€ ํ›„๋ฐฉ ํ†ตํ•ฉ(๋˜๋Š” ์ˆ˜์งํ†ตํ•ฉ)์„ ํ•  ๋Šฅ๋ ฅ์ด ์žˆ์„ ๋•Œ, ๊ตฌ๋งค์ž์˜ ํ˜‘์ƒ๋ ฅ์€ ํฌ๊ฒŒ ์ฆ๊ฐ€ํ•œ๋‹ค (4) ๊ณต๊ธ‰์ž์˜ ํ˜‘์ƒ๋ ฅ(Bargaining power of Suppliers) ๊ณต๊ธ‰์ž์˜ ํ˜‘์ƒ๋ ฅ์€ ๊ตฌ๋งค์ž์˜ ํ˜‘์ƒ๋ ฅ์„ ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ๊ณต๊ธ‰์ž๊ฐ€ ๊ณต๊ธ‰ํ•˜๋Š” ์ œํ’ˆ์— ๋Œ€ํ•ด ๋Œ€์ฒดํ’ˆ์ด ์ ๊ณ  ๊ณต๊ธ‰๋ฐ›๋Š” ๊ธฐ์—…์—๊ฒŒ๋Š” ๋งค์šฐ ์ค‘์š”ํ•  ๋•Œ(Lock-in Effect) ๊ตฌ๋งค์ž๊ฐ€ ์†ํ•ด ์žˆ๋Š” ์‚ฐ์—…์ด ๊ณต๊ธ‰์ž์—๊ฒŒ๋Š” ์žฌ๋ฌด์  ํ˜น์€ ์ „๋žต์ ์œผ๋กœ ๋ณ„๋กœ ์ค‘์š”ํ•˜์ง€ ์•Š์„ ๋•Œ ๊ณต๊ธ‰์ž ์ œํ’ˆ์ด ์ฐจ๋ณ„ํ™”๋˜์–ด ๊ต์ฒด ๋น„์šฉ์ด ๋†“์„ ๋•Œ ๊ณต๊ธ‰์ž๊ฐ€ ์ „๋ฐฉ ํ†ตํ•ฉ(Forward Integration)์„ ํ•˜๊ฒ ๋‹ค๊ณ  ๋ฏฟ์„ ๋งŒํ•œ ์œ„ํ˜‘์„ ํ•  ๋•Œ ๊ตฌ๋งค์ž๊ฐ€ ๊ฐ€๊ฒฉ์„ ๋‚ฎ์ถ”๊ธฐ ์œ„ํ•ด ํ›„๋ฐฉ ํ†ตํ•ฉ(Backward Integration) ํ•˜๊ฒ ๋‹ค๋Š” ์œ„ํ˜‘์„ ํ•  ์ˆ˜ ์—†์„ ๋•Œ ๊ณต๊ธ‰์ž์˜ ํ˜‘์ƒ๋ ฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ๊ธฐ์—…๋“ค์€ ํ”ํžˆ ์นด๋ฅดํ…”(Cartel. ๊ธฐ์—… ์—ฐํ•ฉ ๋˜๋Š” ๊ธฐ์—… ๋‹ดํ•ฉ)์„ ํ˜•์„ฑํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ๊ฒƒ์ด OPEC*์ธ๋ฐ OPEC๋Š” ์‚ฐ์œ ๊ตญ๋“ค์˜ ๊ต์„ญ๋ ฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ๊ฒฐ์„ฑ๋œ ๊ฒƒ์ด๋‹ค. * OPEC (The Organization of the Petroleum Exporting Countries. ์„์œ ์ˆ˜์ถœ๊ตญ๊ธฐ๊ตฌ) (5) ๋Œ€์ฒด์žฌ์˜ ์œ„ํ˜‘(Threats of Substitute products) ์ด๋Ÿฌํ•œ ๊ณต๊ธ‰์ž์˜ ๋…ธ๋ ฅ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๊ฐ•๋ ฅํ•œ ๋Œ€์ฒด์žฌ๊ฐ€ ์กด์žฌํ•˜๋ฉด ๊ตฌ๋งค์ž์˜ ํ˜‘์ƒ๋ ฅ์ด ๋†’์•„์ ธ์„œ ํ•ด๋‹น ์‚ฐ์—…์˜ ์ˆ˜์ต์„ฑ์€ ๋‚ฎ์•„์ง€๊ฑฐ๋‚˜ ๋Œ€์ฒด๋˜์–ด ๋ฒ„๋ฆฐ๋‹ค. mp3 ํ”Œ๋ ˆ์ด์–ด๋Š” ํ•ด๋‹น ์‚ฌ๋ก€๋กœ ๋„๋ฆฌ ์–ธ๊ธ‰๋˜๋Š”๋ฐ ๊ณผ๊ฑฐ mp3 ํ”Œ๋ ˆ์ด์–ด๋ฅผ ์„ธ๊ณ„ ์ตœ์ดˆ๋กœ ๊ฐœ๋ฐœํ•˜์˜€๊ณ  ์‹œ์žฅ ์ง€๋ฐฐ์ ์ธ ์œ„์น˜์— ์žˆ์—ˆ๋˜ ํ•œ๊ตญ ๊ธฐ์—…๋“ค์ด mp3 ํ”Œ๋ ˆ์ด์–ด์˜ ๊ธฐ๋Šฅ์„ ์•ฑ์œผ๋กœ ๋Œ€์ฒดํ•ด๋ฒ„๋ฆฐ ์Šค๋งˆํŠธํฐ์ด ๋‚˜์˜ค๋ฉด์„œ ๋ชจ๋‘ ์‹œ์žฅ์—์„œ ์‚ฌ๋ผ์ง€๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋Œ€์ฒด์žฌ๊ฐ€ ๊ทธ ์‚ฐ์—…์˜ ๊ฐ€๊ฒฉ ๊ฒฐ์ •์— ์˜ํ–ฅ์„ ๋ผ์น  ์ˆ˜ ์žˆ๋Š” ์š”์ธ์€ ํฌ๊ฒŒ 2๊ฐ€์ง€์ธ๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์งˆ๋ฌธ์„ ํ†ตํ•ด ๋‹ต์„ ๊ณ ์ฐฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์†Œ๋น„์ž๋“ค์ด ๋Œ€์ฒด์žฌ๋กœ ์‰ฝ๊ฒŒ ์˜ฎ์•„๊ฐˆ ์ˆ˜ ์žˆ๋Š”๊ฐ€? ๋Œ€์ฒด์žฌ๋Š” ์–ผ๋งˆ๋‚˜ ์œ ์šฉํ•œ๊ฐ€? ์‹ค์ œ๋กœ 80๋…„๋Œ€์— ์„คํƒ• ๋Œ€์‹  ๊ฐ๋ฏธ๋ฃŒ๊ฐ€ ์ถœํ˜„ํ–ˆ์ง€๋งŒ ์†Œ๋น„์ž๋“ค์€ ์—ฌ์ „ํžˆ ์„คํƒ•์„ ์„ ํ˜ธํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ์ปคํ”ผ์™€ ์ฐจ์˜ ๊ฒฝ์šฐ ๊ธฐํ˜ธ ์‹ํ’ˆ(Favorite food)์œผ๋กœ ์ž๋ฆฌ๋งค๊น€ํ•˜์—ฌ ๋Œ€์ฒด ํšจ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธด ํ•˜์ง€๋งŒ ํ•œ๋ฒˆ ์ต์ˆ™ํ•ด์ง„ ๋ง›์„ ์†Œ๋น„์ž๋“ค์ด ์‰ฝ๊ฒŒ ๋ฐ”๊พธ์ง€๋Š” ์•Š๋Š”๋‹ค. ์‚ดํŽด๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ 5 Forces model์€ Table III-7๊ณผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฅ๋‹จ์ ์ด ์žˆ๋‹ค. Table III-7. Five Forece ๋ถ„์„์˜ ์žฅ๋‹จ์  ๋งˆ์ดํด ํฌํ„ฐ๋Š” ํ•ด๋‹น ๋ถ„์„ ๊ธฐ๋ฒ•์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ์ „๋žต ์ง‘๋‹จ(Strategic Group)์ด๋‚˜ ์‚ฐ์—… ์ˆ˜๋ช… ์ฃผ๊ธฐ(Industry Life Cycle) ๊ฐ™์€ ๊ฐœ๋…์„ ์ œ์‹œํ•˜๊ธฐ๋„ ํ•˜์˜€๋‹ค. ํ˜„์žฌ ํฌํ„ฐ๊ฐ€ ๋ฏธ๊ตญ ๊ฒฝ์˜ํ•™๊ณ„์˜ ์ฃผ๋ฅ˜๋Š” ์•„๋‹Œ๋“ฏํ•˜์ง€๋งŒ ์ „๋žต ๊ด€์ ์—์„œ ๊ฒฝ์˜ํ•™์„ ๋ณธ ์ตœ์ดˆ์˜ ์‚ฌ๋žŒ์ด ์•„๋‹๊นŒ ์ƒ๊ฐ์ด ๋“ ๋‹ค. ์ž๋ฃŒ๋ฅผ ์ฐพ์•„๋ณด๋‹ˆ ํฌํ„ฐ์˜ ๋ชจํ˜•์ด ๋ฐœํ‘œ๋˜๋˜ ์‹œ์ ˆ, ์ด๋ฏธ ๊ฒฝ์ œํ•™๊ณ„์—์„œ๋Š” ์Š˜ํŽ˜ํ„ฐ(Joseph Schumpeter. 1883 ~ 1950)๊ฐ€ ์ผ์ฐ์ด ๊ฒฝ์Ÿ๊ณผ ์‚ฐ์—… ๊ตฌ์กฐ์˜ ๋™ํƒœ์ ์ธ ์ƒํ˜ธ ์ž‘์šฉ์„ ์ธ์‹ํ•˜๊ณ  ์ด์— ์ฃผ๋ชฉํ•˜์˜€๋‹ค๊ณ  ํ•œ๋‹ค. ์Š˜ํŽ˜ํ„ฐ ์ง€์ง€์ž๋“ค์€ ํ˜„์žฌ ๋…์ ์ฒด์ œ๋ฅผ ์œ ์ง€ํ•˜๊ณ  ์žˆ๋Š” ๊ธฐ์—…๋“ค๋„ ์ƒˆ๋กœ์šด ๊ธฐ์ˆ , ์ƒˆ๋กœ์šด ์œ ํ†ต๋ง, ์ƒˆ๋กœ์šด ์ œํ’ˆ์„ ๊ฐ–๊ณ  ์ง„์ž…ํ•ด ์˜ค๋Š” ์ƒˆ๋กœ์šด ๊ฒฝ์Ÿ์ž์— ์˜ํ•ด์„œ ๋…์ ์  ์ง€์œ„๋ฅผ ๋นผ์•—๊ธฐ๊ณ  ์‹œ์žฅ์€ ์ ์ฐจ ๊ฒฝ์Ÿ์ ์ธ ์ฒด์ œ๋กœ ๋ฐ”๋€๋‹ค๊ณ  ๋ณด๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์–ด๋Š ์‚ฐ์—…์—์„œ ๊ตฌ์กฐ์ ์ธ ๊ธฐ์—…ํ˜์‹ ์œผ๋กœ ์‚ฐ์—…๊ตฌ์กฐ๊ฐ€ ๋น ๋ฅธ ์†๋„๋กœ ๋ฐ”๋€Œ๊ณ  ์žˆ๋‹ค๋ฉด, ๊ณ ์ •๋œ ์‹œ์ ์—์„œ ์‚ฐ์—…์„ ๋ถ„์„ํ•˜๋Š” ํฌํ„ฐ์˜ ๋ฐฉ๋ฒ•์€ ๊ธฐ์—…๋“ค์—๊ฒŒ ๋ณ„๋กœ ๋„์›€์„ ์ฃผ์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋ง์— ๋™์˜ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์Š˜ํŽ˜ํ„ฐ๊ฐ€ ๋งํ•˜๋Š” ๊ธฐ์—… ํ˜์‹ ์— ์˜ํ•œ ์ฐฝ์กฐ์  ํŒŒ๊ดด(Creative destruction) ๊ณผ์ •์€ ์ƒ๋‹นํžˆ ๋Š๋ฆฌ๊ฒŒ ์ง„ํ–‰๋œ๋‹ค. ์ด๋Ÿฐ ์ ์ด ํฌํ„ฐ์˜ ์ง€์ง€์ž๋“ค์ด<NAME>์  ๋ถ„์„์ด ์œ ํšจํ•˜๋‹ค๊ณ  ์ฃผ์žฅํ•˜๋Š” ๋‹จ๋ฉด์ด๊ธฐ๋„ ํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ตœ๊ทผ ํ™”๋‘๊ฐ€ ๋˜๊ณ  ์žˆ๋Š” 4์ฐจ ์‚ฐ์—… ํ˜๋ช…(4IR)์€ ์ด๋Ÿฐ ์ „์ œ์™€ ๊ฐ€์ •์„ ๋งˆ๊ตฌ๋งˆ๊ตฌ ๊นจ๊ณ  ์žˆ๊ธฐ์— ํฅ๋ฏธ๋กœ์šฐ๋ฉด์„œ๋„ ํ•œํŽธ ์ œ๋Œ€๋กœ ์ค€๋น„ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํ•œ์ˆœ๊ฐ„์— ์‚ฌ๋ผ์ ธ๋ฒ„๋ฆฌ๋Š” ๋‘๋ ค์šด ์ƒํ™ฉ์ด ์˜ˆ์ƒ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 1.6 Value Chain ๋ถ„์„ โ€˜๊ณต๊ธ‰ ์‚ฌ์Šฌ ๋ถ„์„โ€™ ๋˜๋Š” โ€˜๊ฐ€์น˜์‚ฌ์Šฌ ๋ถ„์„โ€™์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ์ด ๋ถ„์„ ๊ธฐ๋ฒ•๋„ ๋งˆ์ดํด ํฌํ„ฐ๊ฐ€ ์ œ์•ˆํ•œ ๊ฒƒ์ธ๋ฐ ๊ธฐ์—… ํ™œ๋™์„ ๋ณธ์›์  ํ™œ๋™(Primary Activities)๊ณผ ์ง€์› ํ™œ๋™(Support Activities)์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ณธ์›์  ํ™œ๋™์€ ๊ฐ€์น˜(Value)๋ฅผ ์ฐฝ์ถœํ•˜๊ณ , ์ง€์› ํ™œ๋™์€ ๊ฐ€์น˜ ์ฐฝ์ถœ ํ™œ๋™์„ ์ง€์›ํ•œ๋‹ค๊ณ  ์ฃผ์žฅํ•˜์˜€๋‹ค. ๋Œ€ํ‘œ์ ์ธ ๋ณธ์›์  ํ™œ๋™์€ ์กฐ๋‹ฌ, ์ƒ์‚ฐ, ์šด์˜, ์œ ํ†ต, ๋งˆ์ผ€ํŒ…, ํŒ๋งค, ์„œ๋น„์Šค ๋“ฑ์ด ํ•ด๋‹นํ•˜๋ฉฐ ์ง€์› ํ™œ๋™์€ ์ธ์‚ฌ, ์žฌ๋ฌด, ๊ตฌ๋งค, ์—ฐ๊ตฌ๊ฐœ๋ฐœ ๋“ฑ์ด ํ•ด๋‹นํ•œ๋‹ค. Figure III-14. ์ผ๋ฐ˜์ ์ธ ์ œ์กฐ๊ธฐ์—…์˜ Value Chain ๊ทธ๋Ÿฐ๋ฐ ์˜๋ฏธ ์žˆ๋Š” ๋ถ„์„์ด ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ณธ์›์  ํ™œ๋™์ด๋“  ์ง€์› ํ™œ๋™์ด๋“  ๊ธฐ์—… ํ™œ๋™์ด ๋ชจ๋‘ ์›๊ฐ€๊ฐ€ ๋ฐฐ๋ถ„๋˜์–ด์•ผ ํ•œ๋‹ค. ํ™œ๋™ ์›๊ฐ€ ๋ถ„์„(Activity Based Cost Analysis: ABC ๋ถ„์„)์ด๋ผ๊ณ  ํ•˜์—ฌ ํ•œ๋•Œ ๋งŽ์€ ๊ธฐ์—…๋“ค์ด ๊ธฐ์—… ํ™œ๋™์˜ ๋ชจ๋“  ๋น„์šฉ ๋ฐฐ๋ถ„์„ ํ™œ๋™ ์›๊ฐ€ ๊ธฐ์ค€์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ ์ž ์‹œ๋„ํ•œ ์ ์ด ์žˆ์—ˆ๋Š”๋ฐ ๋Œ€๋ถ€๋ถ„์˜ ๊ธฐ์—…๋“ค์ด ํฐ ์„ฑ๊ณผ๋ฅผ ์–ป์ง€ ๋ชปํ–ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ERP[3]๋ฅผ ํ™œ์šฉํ•œ๋‹ค ํ•˜๋”๋ผ๋„ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ํˆฌ์ž…๋˜๋Š” ๋…ธ๋ ฅ ๋Œ€๋น„ ํ™œ์šฉ ์„ฑ๊ณผ๊ฐ€ ๊ทธ๋‹ค์ง€ ๋†’์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํฌํ„ฐ์˜ ์‹œ๋„๋Š” ๊ธฐ์—…์˜ ๋ณธ์›์  ํ™œ๋™๊ณผ ์ง€์› ํ™œ๋™์„ ๊ตฌ๋ถ„ํ•˜๊ณ  Figure III-14์™€ ๊ฐ™์ด ๊ฐ ํ™œ๋™๋“ค์— ํˆฌ์ž…๋˜๋Š” ๋น„์šฉ ๋ถ„์„์„ ํ†ตํ•ด ์–ด๋–ค ๋ถ€๋ถ„์ด ๊ฐ€์žฅ ๋งŽ์€ ๊ฐ€์น˜๋ฅผ ์ฐฝ์ถœํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ทœ๋ช…ํ•˜๊ณ  ๋‚˜์•„๊ฐ€ ๊ฒฝ์Ÿ์‚ฌ์™€์˜ ๋น„๊ต๋ฅผ ํ†ตํ•ด ๊ฐ•ํ™”์‹œ์ผœ์•ผ ํ•  ๋ถ€๋ถ„ ๋ฐ ์กฐ์ • ๋˜๋Š” ์ถ•์†Œํ•ด์•ผ ํ•  ๋ถ€๋ถ„์„ ํŒŒ์•…ํ•˜๊ณ ์ž ํ•˜์˜€์œผ๋‚˜ ํ˜„์‹ค์ ์œผ๋กœ ๊ฒฝ์Ÿ์‚ฌ ์ œํ’ˆ์˜ ์›๊ฐ€ ์ •๋ณด๋ฅผ ์ž…์ˆ˜ํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์ง€ ์•Š์œผ๋ฉฐ ABC ๋ถ„์„ ์ž์ฒด๊ฐ€ ํšŒ๊ณ„ ๊ด€๋ฆฌ์˜ ์•„์ฃผ ์ด์ƒ์ ์ธ ์ง€ํ–ฅ์  ์ค‘์˜ ํ•˜๋‚˜์ด์–ด์„œ ๋งŽ์€ ๊ธฐ์—…๋“ค์ด ๊ทธ ์‚ฌ์ƒ์— ๊ณต๊ฐํ•˜๊ธด ํ•˜์ง€๋งŒ ์‹ค์งˆ์ ์œผ๋กœ ๊ทธ๋Œ€๋กœ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ํ™œ์šฉํ•˜์—ฌ ์„ฑ๊ณผ๋ฅผ ์ฐฝ์ถœํ•˜์ง€๋Š” ๋ชปํ–ˆ๋‹ค. Figure III-15. Value Chain ํ•ญ๋ชฉ๋ณ„ ์šด์˜ ๋น„์šฉ์˜ ๋ถ„ํฌ Table III-8. Value Chain ๋ถ„์„์˜ ์žฅ๋‹จ์  Table III-8์€ Value Chain ๋ถ„์„์˜ ์žฅ๋‹จ์ ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. ๋ถ€๊ฐ๋˜๋Š” ๋‹จ์ ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๊ฐ€์น˜์‚ฌ์Šฌ ๋ถ„์„์€ ํ•ด๋‹น ๊ธฐ์—…์ด ์†ํ•œ ์‚ฐ์—…์„ ๊ธฐ์ค€์œผ๋กœ ์ œํ’ˆ์˜ ์ˆ˜์š” ์ธก๋ฉด์—์„œ ์ „๋ฐฉ ์‚ฐ์—…(Downstream)์ด ์–ด๋–ป๊ฒŒ ๋ณ€ํ™”ํ•˜๋Š”์ง€ ๊ฐ ๋ถ€๋ฌธ๋ณ„ ์ˆ˜์š” ํŠน์„ฑ์„ ํŒŒ์•…ํ•˜๊ฑฐ๋‚˜, ํ›„๋ฐฉ ์‚ฐ์—…(Upstream) ๋ถ„์„์„ ํ†ตํ•ด ์ œํ’ˆ์˜ ์›๊ฐ€ ๋ฐ ์›์ž์žฌ ์ธก๋ฉด์˜ ๋ณ€๋™์„ฑ์„ ํŒŒ์•…ํ•˜๋Š”๋ฐ ํ™œ์šฉ๋˜๊ณค ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„ B2B ๊ธฐ์—…๋“ค์€ ์ค‘๊ฐ„์žฌ๋ฅผ ๊ตฌ์ž…ํ•˜์—ฌ ์ตœ์ข… ์ œํ’ˆ์„ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์ „/ํ›„๋ฐฉ ์‚ฐ์—… ์—ฐ์‡„ํšจ๊ณผ(Forward/Backward Linkage Effect)๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ฆ‰, ๊ฒฝ๊ธฐ๊ฐ€ ์ข‹์€ ๋•Œ๋Š” ๋™๋ฐ˜ํ•˜์—ฌ ๊ฐ™์ด ์„ฑ์žฅํ•˜๊ณ  ๊ทธ ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋Š” ๊ฐ™์ด ์‡ ๋ฝํ•œ๋‹ค. ๋•Œ๋•Œ๋กœ ์ „/ํ›„๋ฐฉ ์‚ฐ์—…์„ ํŒŒ์•…ํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ตœ์ข… ์†Œ๋น„์ž์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ์ „๋ฐฉ ์‚ฐ์—…,<NAME>๋ฃŒ ๊ณต๊ธ‰์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ํ›„๋ฐฉ ์‚ฐ์—…์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž๋™์ฐจ ์‚ฐ์—…์„ ์ƒ๊ฐํ•ด ๋ณผ ๋•Œ ํƒ€์ด์–ด ๋“ฑ ์ฐจ๋Ÿ‰ ๋ถ€ํ’ˆ ์‚ฐ์—…์€ ์ „๋ฐฉ ์‚ฐ์—…์ด๋ฉฐ, ์ œ์ฒ ์‚ฐ์—…์€ ์ฐจ๋Ÿ‰์˜ ์›์ž์žฌ๋ฅผ ๊ณต๊ธ‰ํ•˜๋Š” ํ›„๋ฐฉ ์‚ฐ์—…์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์งˆ๋ฌธ์œผ๋กœ ์ „/ํ›„๋ฐฉ ์‚ฐ์—… ๋ถ„์„์„ ์‹œ์ž‘ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์›์ž์žฌ๋ถ€ํ„ฐ ์ตœ์ข… ์ œํ’ˆ๊นŒ์ง€ ๊ณต๊ธ‰ ์‚ฌ์Šฌ์˜ ๊ตฌ์กฐ๋Š” ์–ด๋– ํ•œ๊ฐ€? ์–ด๋–ค ํ™œ๋™ ๋˜๋Š” ๋‹จ๊ณ„๊ฐ€ ๊ฐ€์žฅ ๋น„ํšจ์œจ์ ์ธ๊ฐ€? ๊ฐ€์น˜ ๊ทน๋Œ€ํ™”๋ฅผ ์œ„ํ•ด ์ „/ํ›„๋ฐฉ ํ†ตํ•ฉ์ด ํ•„์š”ํ•œ๊ฐ€? Break #13. ์ˆ˜์งํ†ตํ•ฉ(Vertical Integration)์˜ ์žฅ/๋‹จ์  ์ „/ํ›„๋ฐฉ ์‚ฐ์—…์„ ๋‹ค๋ฃจ๋‹ค ๋ณด๋ฉด ๋ฐ˜๋“œ์‹œ ๋‚˜์˜ค๋Š” ์ด์•ผ๊ธฐ๊ฐ€ ์‚ฐ์—… ํ†ตํ•ฉ(Integration)์ด๋‹ค. ๊ฒฝ์˜์ „๋žต ๊ด€์ ์—์„œ ํ†ตํ•ฉ์€ ์ˆ˜์ง ํ†ตํ•ฉ(Vertical Integration)๊ณผ ์ˆ˜ํ‰ ํ†ตํ•ฉ(Horizontal Integration)์ด ์žˆ๋‹ค. ์ˆ˜์งํ†ตํ•ฉ์€ ์ง์ ‘ ์ž์‹ ์˜ ํˆฌ์ž…๋ฌผ์„ ์ƒ์‚ฐํ•˜๊ฑฐ๋‚˜(Backward Integration), ์ƒ์‚ฐ๋ฌผ์„ ์œ ํ†ตํ•˜๋Š” ๊ฒƒ(Forward Integration)์„ ๋งํ•œ๋‹ค. Figure III-15๋Š” ์ˆ˜์ง ํ†ตํ•ฉ์˜ ๊ฐœ๋…์„ ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ทจํ•˜๋Š” ์ „๋žต์  ์ˆ˜๋‹จ์˜ ๋Œ€ํ‘œ์ ์ธ ๊ฒƒ์ด ์ธ์ˆ˜ํ•ฉ๋ณ‘์ด๋‚˜ ์žํšŒ์‚ฌ ์„ค๋ฆฝ์ด๋‹ค. ์‚ฐ์—… ๊ฐ„ Value Chain์˜ ํ†ตํ•ฉ์˜ ์žฅ๋‹จ์ ์— ๋Œ€ํ•ด์„œ ์ƒ์„ธํžˆ ์•Œ์•„๋ณด์ž. ์šฐ์„ ์ ์œผ๋กœ ์‚ดํŽด๋ณผ ๊ฒƒ์€ ์ˆ˜์ง ํ†ตํ•ฉ์˜ ๊ฐ€์น˜์ฐฝ์ถœ ์š”์ธ์ด๋‹ค. ์ˆ˜์งํ†ตํ•ฉ์€ ์ฃผ๋กœ ํ•ต์‹ฌ์‚ฌ์—…์—์„œ ๊ฒฝ์Ÿ์  ์ง€์œ„๋ฅผ ๊ฐ•ํ™”ํ•  ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์š”์ธ์„ ํ†ตํ•ด ๊ฐ€์น˜๋ฅผ ์ฐฝ์ถœํ•œ๋‹ค. ์ƒ์‚ฐ ์›๊ฐ€ ์ ˆ๊ฐ ์‹œ์žฅ ๋น„์šฉ ๊ฐ์†Œ ์ œํ’ˆ ํ’ˆ์งˆ์˜ ์œ ์ง€ ํŠนํ—ˆ ๊ธฐ์ˆ ์˜ ๋ณดํ˜ธ ์ฒซ ๋ฒˆ์งธ, ์ƒ์‚ฐ์›๊ฐ€ ์ ˆ๊ฐ์€ ์ˆ˜์ง ํ†ตํ•ฉ ์ค‘ ํ›„๋ฐฉ ํ†ตํ•ฉ(Backward Integration)์œผ๋กœ ์–ป์–ด์งˆ ์ˆ˜ ์žˆ๋Š”๋ฐ,<NAME>๋ฃŒ๋‚˜ ๋ถ€๋ถ„ํ’ˆ์„ ์ƒ์‚ฐ๊ณต์ •์— ์œ ๋ฆฌํ•œ ์กฐ๊ฑด์œผ๋กœ ํˆฌ์ž…ํ•จ์œผ๋กœ์จ ํšจ์œจ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค. ๋˜, ์ƒ์‚ฐ ๊ณต์ •์— ๋Œ€ํ•œ ๊ณ„ํš๊ณผ ์กฐ์ •์ด ์‰ฌ์›Œ์ง์œผ๋กœ์จ ์ƒ์‚ฐ ์›๊ฐ€๋ฅผ ์ ˆ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๋ฒˆ์งธ, ์‹œ์žฅ ๋น„์šฉ ๊ฐ์†Œ๋Š” ์ˆ˜์งํ†ตํ•ฉ์˜ ์ „๋ฐฉ ํ†ตํ•ฉ(Forward Integration)์œผ๋กœ ์–ป์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์œ ํ†ต ๊ณผ์ • (์ƒํ’ˆ ์ค‘๊ฐœ์—…์ž, ์ฐฝ๊ณ , ์ˆ˜์†ก ์—…์ž ๋“ฑ)์„ ์žฅ์•…ํ•จ์œผ๋กœ์จ ๊ด€๋ จ ๋น„์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ, ์†Œ์ˆ˜์˜ ๊ณต๊ธ‰์ž๋กœ๋ถ€ํ„ฐ<NAME>๋ฃŒ ๋ฐ ๋ถ€๋ถ„ํ’ˆ์„ ์ˆ˜๊ธ‰ ๋ฐ›์„ ๊ฒฝ์šฐ ํ›„๋ฐฉ ํ†ตํ•ฉ์„ ํ†ตํ•ด ๊ด€๋ จ ๋น„์šฉ์„ ์ ˆ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒฐ๊ตญ, ๊ตฌ๋งค์ž๊ฐ€ ๊ณต๊ธ‰์ž๊ฐ€ ๋˜๋ผ๋Š” ์ด์•ผ๊ธฐ์ด๋‹ค. ์ œํ•œ๋œ ์ˆ˜์˜ ๊ตฌ๋งค์ž๋„ ๊ฐ™์€ ๊ฒฝ์šฐ์ธ๋ฐ ์ด ๊ฒฝ์šฐ๋Š” ์ „๋ฐฉํ†ตํ•ฉ์„ ํ†ตํ•ด ๊ณต๊ธ‰์ž๊ฐ€ ํŒ๋งค์ƒ์ด ๋˜๋ผ๋Š” ์ด์•ผ๊ธฐ์ด๋‹ค. Figure III-16. ์ „ํ›„๋ฐฉ ์‚ฐ์—…ํ†ตํ•ฉ์˜ ๊ฐœ๋… ์„ธ ๋ฒˆ์งธ, ์ œํ’ˆ ํ’ˆ์งˆ์˜ ์œ ์ง€๋Š” ์ˆ˜์ง ํ†ตํ•ฉ์„ ํ†ตํ•ด ์ œํ’ˆ์˜ ๊ฒฝ์Ÿ๋ ฅ์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ธ๋ฐ ํ›„๋ฐฉ ํ†ตํ•ฉ์„ ํ†ตํ•ด ์ œํ’ˆ์˜ ํ’ˆ์งˆ ์ผ๊ด€์„ฑ์„, ์ „๋ฐฉ ํ†ตํ•ฉ์„ ํ†ตํ•ด ์‚ฌํ›„ ์„œ๋น„์Šค(A/S) ํ’ˆ์งˆ์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์ด๋‹ค. ๋„ค ๋ฒˆ์งธ, ํŠนํ—ˆ ๊ธฐ์ˆ ์˜ ๋ณดํ˜ธ๋Š” ์ˆ˜์ง ํ†ตํ•ฉ์„ ํ•จ์œผ๋กœ์จ ์ƒ์‚ฐ๋ถ€ํ„ฐ ํŒ๋งค๊นŒ์ง€ ๋‹จ์ผ ๊ธฐ์—… ๋˜๋Š” ๊ธฐ์—… ๊ณ„์—ด์ด ์ˆ˜ํ–‰ํ•จ์œผ๋กœ์จ ๊ด€๋ จ ๊ธฐ์ˆ ๊ณผ ํŠนํ—ˆ๊ฐ€ ์™ธ๋ถ€๋กœ ์œ ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ƒ๊ฐ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฐ ์ˆ˜์ง ํ†ตํ•ฉ๋„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹จ์ ์ด ์žˆ๋‹ค. ์›๊ฐ€ ์ƒ์˜ ๋ถˆ์ด์ต ์œ ์—ฐ์„ฑ์˜ ๋ถ€์กฑ ์ฒซ ๋ฒˆ์งธ, ์›๊ฐ€ ์ƒ์˜ ๋ถˆ์ด์ต์€ ์ž์‚ฌ ์†Œ์œ ์˜ ๊ณต๊ธ‰์›์œผ๋กœ๋ถ€ํ„ฐ ๊ณต๊ธ‰์„ ๋ฐ›์œผ๋ฉด์„œ ์˜คํžˆ๋ ค ์›๊ฐ€ ์ƒ์˜ ๋ถˆ์ด์ต์„ ๋ฐ›๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ์ธ๋ฐ ๋Œ€๋ถ€๋ถ„ ์žํšŒ์‚ฌ๊ฐ€ ์ด๋Ÿฐ ๊ณต๊ธ‰ ์—ญํ• ์„ ๋งก๊ฒŒ ๋จ์œผ๋กœ์จ ๋ชจ๊ธฐ์—…์ด๋ผ๋Š” ์•ˆ์ •์ ์ธ ๊ณต๊ธ‰์ฒ˜๋ฅผ ํ™•๋ณดํ•˜๊ฒŒ ๋˜์–ด ํ•ด๋‹น ๊ธฐ์—…์˜ ์›๊ฐ€๊ฒฝ์Ÿ๋ ฅ์ด ์•ฝํ™”๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋˜ํ•œ, ์˜ค๋ฒ„ํ—ค๋“œ(overhead) ๋น„์šฉ์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ ๊ฒฝ์Ÿ ์‹œ์žฅ(open market) ๋Œ€๋น„ ๊ฐ€๊ฒฉ๊ฒฝ์Ÿ๋ ฅ๋„ ๋–จ์–ด์ง€๋Š” ํ˜„์ƒ์ด ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐœ์ƒํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ์œ ์—ฐ์„ฑ์˜ ๋ถ€์กฑ์€ ์ˆ˜์งํ†ตํ•ฉ์˜ ๊ฐ•๋„๊ฐ€ ๊ฐ•ํ•˜๋ฉด ๊ฐ•ํ• ์ˆ˜๋ก ์ƒˆ๋กญ๊ฒŒ ๋“ฑ์žฅํ•˜๋Š” ๊ธฐ์ˆ ์ด๋‚˜ ์œ ํ†ต๋ง์˜ ํ˜์‹ ์— ๋Œ€์‘ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ์ง„๋‹ค. ํŠนํžˆ, ๋ชจ๊ธฐ์—… ์ฐจ์›์—์„œ ์œ ์—ฐ์„ฑ์ด ๊ทน๋„๋กœ ๋–จ์–ด์ง€๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋‹ค. ๋”๊ตฐ๋‹ค๋‚˜ ์žํšŒ์‚ฌ์˜ ๊ฒฝ์˜์ง„์€ ๋Œ€๋ถ€๋ถ„ ๋ชจ๊ธฐ์—…์—์„œ ๋‚™ํ•˜์‚ฐ์ฒ˜๋Ÿผ ์ž„๋ช…๋˜์–ด ๊ฐ€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋น„์ผ๋น„์žฌ(้žไธ€้žๅ†) ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ด€๋ฃŒํ™”๋ฅผ ๋‚ณ๊ฒŒ ๋˜๊ณ , ์ธ์‚ฌ ๋ฌธ์ œ๊นŒ์ง€ ์—ฐ๊ณ„๋˜์–ด ๊ธฐ์—…์˜ ์œ ์—ฐ์„ฑ์„ ๋”์šฑ ๋–จ์–ด๋œจ๋ฆฌ๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฐ ์ˆ˜์งํ†ตํ•ฉ์˜ ํํ•ด๋ฅผ ๊ทน๋ณตํ•˜๊ณ  ํ•ด๋‹น ์žฅ์ ์„ ์‚ด๋ฆฌ๊ธฐ ์œ„ํ•ด ์ž์ฃผ ์ œ์‹œ๋˜๋Š” ๋Œ€์•ˆ์€ โ€˜์žฅ๊ธฐ ๊ณ„์•ฝ(Long-term Contracting)โ€™์ด๋‹ค. ์žฅ๊ธฐ๊ณ„์•ฝ์€ ๊ณต๊ธ‰ ์—…์ž ๋˜๋Š” ํŒ๋งค์—…์ž๋“ค๊ณผ ์žฅ๊ธฐ๋กœ ๊ณ„์•ฝ์„ ๋งบ๊ณ  ๊ท ์ผํ•œ ํ’ˆ์งˆ ์ˆ˜์ค€ ์œ ์ง€, ์›๊ฐ€์ ˆ๊ฐ ๋“ฑ ๊ฒฝ์Ÿ ์šฐ์œ„ ํ™•๋ณด๋ฅผ ์œ„ํ•œ ์ƒํ˜ธ ํ˜‘๋ ฅ์„ ํ•ฉ์˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ํŒŒํŠธ๋„ˆ ๋˜๋Š” ํ˜‘๋ ฅ์—…์ฒด์™€์˜ โ€˜์‹ ๋ขฐ(Trust)โ€™์ธ๋ฐ ๊ณ„์•ฝ ์ž์ฒด๊ฐ€ ์‹ ๋ขฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋งบ์–ด์ง€๊ธด ํ•˜์ง€๋งŒ ๋ช…์‹œ์ ์ธ ๊ตฌ๋งค ์•ฝ์†์ด๋ผ๋“ ๊ฐ€ ๊ณต๋™ ํˆฌ์ž ๋“ฑ ๋Œ€๋‚ด์™ธ์ ์œผ๋กœ ์žฅ๊ธฐ๊ณ„์•ฝ ๊ด€๊ณ„๋ฅผ ์ฒœ๋ช…ํ•˜๋Š” ๋ณ„๋„์˜ ํ™œ๋™์„ ์ „๊ฐœํ•˜๊ธฐ๋„ ํ•œ๋‹ค. 1.7 SWOT ๋ถ„์„ ๊ฝค ๊ธธ๊ฒŒ ์‚ฐ์—…๊ตฌ์กฐ ๋ถ„์„์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์•˜๋‹ค. ์ด์ œ ์‚ฐ์—… ํ™˜๊ฒฝ๋„ ์‚ดํŽด๋ณด๋ฉด์„œ ๊ธฐ์—… ๋‚ด๋ถ€์˜ ์—ญ๋Ÿ‰๋„ ์‚ดํŽด๋ณผ ์ฐจ๋ก€์ด๋‹ค. โ€˜์ง€ํ”ผ์ง€๊ธฐ ๋ฐฑ์ „๋ฐฑ์Šน(็Ÿฅๅฝผ็Ÿฅๅทฑ็™พๆˆฐ็™พๅ‹)โ€™์ด๋ผ๊ณ  ๊ฒฝ์Ÿ์—์„œ ์‚ด์•„๋‚จ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฒฝ์Ÿ์‚ฌ๋ฟ ์•„๋‹ˆ๋ผ ์ž์‚ฌ์˜ ์—ญ๋Ÿ‰์„ ํŒŒ์•…ํ•˜๋Š” ์ผ๋„ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. SWOT ๋ถ„์„์€ ๊ธฐ์—…์˜ ๊ฐ•์ (Strength), ์•ฝ์  (Weakness), ๊ธฐํšŒ(Opportunities)์™€ ์œ„ํ˜‘(Threat) ์š”์ธ๋“ค์„ ํŒŒ์•…ํ•˜๊ณ  ์ด๋ฅผ ๊ฒฝ์Ÿ ์šฐ์œ„(Competitive Advantage) ๊ด€์ ์—์„œ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•  ๊ฒƒ์ธ์ง€ ์ „๋žต์  ์‹œ์‚ฌ์ ์„ ๋„์ถœํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋„๊ตฌ์ด๋‹ค. Figure III-17. SWOT ๋ถ„์„์˜ ๊ฐœ๋… ์ดˆ์ฐฝ๊ธฐ ์ „๋žต ๋ถ„์„ ํ”„๋ ˆ์ž„์›Œํฌ์ด์—ˆ๋˜ SWOT ๋ถ„์„์€ 1960๋…„ ๋Œ€ ํ•˜๋ฒ„๋“œ ๋น„์ฆˆ๋‹ˆ์Šค ์Šค์ฟจ์—์„œ ์ˆ˜ํ•™ํ•˜๋˜ Christensen, Andrew, Gut์— ์˜ํ•ด ๊ฐœ๋ฐœ๋˜์–ด ๊ตฌ์กฐ์  ์ „๋žต ๋ถ„์„์˜ ๊ธฐ๋ณธ ๋ฐฉํ–ฅ์„ ์ œ์‹œํ•˜์˜€๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ‚ค์›Œ๋“œ(keyword) ๋‚˜์—ด์— ๊ทธ์น˜์ง€ ์•Š์œผ๋ ค๋ฉด SWOT ๋ถ„์„ ์‹œ ๋ฐ˜๋“œ์‹œ ์ „๋žต ๋Œ€์•ˆ์„ ๊ฐœ๋ฐœํ•˜๊ณ  ์‹คํ–‰ ๊ณ„ํš์œผ๋กœ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ํ•ต์‹ฌ ์„ฑ๊ณต์š”์†Œ(CSF[4])๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. ๋‹ค์Œ์€ SWOT ๋ถ„์„์„ ์œ„ํ•œ ์งˆ๋ฌธ๋“ค์ด๋‹ค. ๊ฒฝ์Ÿ์‚ฌ์™€ ๋น„๊ตํ•˜์˜€์„ ๋•Œ ์ž์‚ฌ์˜ ๊ฐ•์ ๊ณผ ์•ฝ์  ์š”์ธ์€ ๋ฌด์—‡์ธ๊ฐ€? ์‹œ์žฅ์˜ ๋ณ€ํ™”๋ฅผ ๊ณ ๋ คํ•˜์˜€์„ ๋•Œ ์ž์‚ฌ์—๊ฒŒ ์œ„๊ธฐ์™€ ๊ธฐํšŒ๋Š” ๋ฌด์—‡์ธ๊ฐ€? ๊ฐ ์ „๋žต์  ์‹œ์‚ฌ์ ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ „๋žต ๋Œ€์•ˆ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ํ•ต์‹ฌ ์„ฑ๊ณต ์š”์†Œ๋“ค(CSFs or KSFs)์€ ๋ฌด์—‡์ธ๊ฐ€? Figure III-16๊ณผ ๊ฐ™์ด ๋งŒ๋“ค์–ด์ง„ SWOT ๋งคํŠธ๋ฆญ์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ 4๊ฐœ์˜ ์ „๋žต ์˜ต์…˜์ด ์žˆ๋‹ค. S-O ์ „๋žต W-O ์ „๋žต S-T ์ „๋žต W-T ์ „๋žต S-O ์ „๋žต์€ ํšŒ์‚ฌ์˜ ๊ฐ•์ ์„ ์ž˜ ํ™œ์šฉํ•˜์—ฌ ๊ธฐํšŒ์— ์ถฉ๋ถ„ํžˆ ๋Œ€์ฒ˜ํ•˜๊ธฐ ์œ„ํ•œ ์ „๋žต ๋Œ€์•ˆ์„ ๊ฐœ๋ฐœํ•ด์•ผ ํ•˜๊ณ , W-O ์ „๋žต์€ ๊ธฐํšŒ๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์•ฝ์ ์„ ๊ทน๋ณตํ•  ์ˆ˜ ์žˆ๋Š” ์ „๋žต์„ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, S-T ์ „๋žต์€ ์œ„๋ถ€์˜ ์œ„ํ˜‘์— ๋Œ€ํ•ด ์ž์‚ฌ์˜ ์ทจ์•ฝ์ ์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ์ž์‚ฌ์˜ ๊ฐ•์ ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์‹๋ณ„ํ•˜๋ฉฐ, W-T ์ „๋žต์€ ์™ธ๋ถ€์˜ ์œ„ํ˜‘์œผ๋กœ๋ถ€ํ„ฐ ์ž์‚ฌ์˜ ์•ฝ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ์–ด ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ์ฐจ์›์—์„œ ์ข…ํ•ฉ์ ์ธ ํŒ๋‹จ์„ ํ†ตํ•ด SWOT ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•ด์•ผ ์˜๋ฏธ ์žˆ๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. Figure III-18. SWOT ๋ถ„์„ ์ „๋žต์˜ ๋„์ถœ SWOT ๋ถ„์„์€ ๊ทธ๊ฒƒ๋งŒ์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ๋‹ค๋ฅธ ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„ ๋„๊ตฌ์™€ ๊ฐ™์ด ๋ณ‘ํ–‰ํ•˜์—ฌ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋Œ€๋ถ€๋ถ„ SWOT ๋ถ„์„์„ ํ–ˆ๋‹ค๊ณ  ํ•˜๋ฉด 4๊ฐœ์˜ Box ์•ˆ์— ๊ธฐ์—…์˜ ๊ฐ•์ ๊ณผ ์•ฝ์ , ์œ„๊ธฐ์™€ ๊ธฐํšŒ๋ฅผ ์„œ์ˆ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ทธ์น˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ์€ SWOT ๋ถ„์„์„ ์ž˜๋ชปํ•œ ๊ฒƒ์ด๋‹ค. SW(๊ฐ•์ ๊ณผ ์•ฝ์ )์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์œผ๋กœ๋ถ€ํ„ฐ ๊ธฐ์—… ๋‚ด๋ถ€์˜ ๋ถ„์„๊ณผ ๊ด€๊ณ„๋œ ์‹œ์‚ฌ์ ์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ๊ณ , OT(๊ธฐํšŒ์™€ ์œ„๊ธฐ)์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์œผ๋กœ๋ถ€ํ„ฐ ๊ธฐ์—… ์™ธ๋ถ€์˜ ํ™˜๊ฒฝ ๋ถ„์„๊ณผ ๊ด€๋ จ๋œ ์‹œ์‚ฌ์ ์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ธฐ์—… ๋‚ด/์™ธ๋ถ€ ํ™˜๊ฒฝ์„ ๊ณ ๋ คํ•œ ์ „๋žต์  ๋Œ€์•ˆ์„ ๊ฐœ๋ฐœํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Figure III-17๊ณผ ๊ฐ™์€ SWOT ๋งคํŠธ๋ฆญ์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ๊ทธ์— ๋”ฐ๋ฅธ ๋Œ€์•ˆ์„ ๊ณ ๋ฏผํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. ๋˜ํ•œ, Five Forces ๋ถ„์„์ด๋‚˜ BCG ๋งคํŠธ๋ฆญ์Šค ๊ธฐ๋ฒ•์ฒ˜๋Ÿผ ๊ณ ์ „์  ์ „๋žต ์ˆ˜๋ฆฝ ๊ธฐ๋ฒ•์˜ ํ•˜๋‚˜์ธ SWOT ๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฅ๋‹จ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. Table III-9. SWOT ๋ถ„์„์˜ ์žฅ/๋‹จ์  ๋„ˆ๋ฌด๋‚˜๋„ ์œ ๋ช…ํ•œ ๊ธฐ๋ฒ•์ด๋ผ ์ „๋žต ์ปจ์„คํŒ…์—์„œ ํ™œ์šฉํ•˜๋Š” ๋‹จ๊ณจ ์†Œ์žฌ์ด๊ธฐ๋„ ํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ ์ผํ•˜๋‹ค ๋ณด๋ฉด SWOT ๋งคํŠธ๋ฆญ์Šค์˜ ๋‚ด์šฉ์„ ์ œ๋Œ€๋กœ ์ฑ„์šฐ๊ธฐ ์–ด๋ ค์šด ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๊ทธ ๊ฒฝ์šฐ๋Š” ๋Œ€๋ถ€๋ถ„ ๊ฐ•์ ๊ณผ ์•ฝ์ , ์œ„๊ธฐ์™€ ๊ธฐํšŒ๋ฅผ ๋ช…ํ™•ํ™”ํ•˜๋Š”๋ฐ ์‹คํŒจํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๊ฒƒ์ด ๊ฐ•์ ์ธ์ง€ ๊ธฐํšŒ์ธ์ง€ ๋“ฑ๋“ฑ. ๋ถ„์„์ด ๋ชจํ˜ธํ•˜๋ฉด ์‹œ์‚ฌ์ ๋„ ๋ชจํ˜ธํ•˜๋‹ค. ๋˜ํ•œ, ๊ณ ์ „์ ์ธ ์ „๋žต๋ถ„์„ ๊ธฐ๋ฒ•์˜ ๊ณตํ†ต์ ์€ ๋ชจ๋‘ ์ •์ (static)์ด๊ฑฐ๋‚˜ ๋‹จ๋ฉด์˜ ๋ชจ์Šต๋งŒ ๋ถ€๊ฐ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋…์ž๋‹˜ ๋ชจ๋‘๊ฐ€ ์ž˜ ์•Œ ๋“ฏ์ด ์šฐ๋ฆฌ๊ฐ€ ๋งˆ์ฃผํ•˜๊ณ  ์žˆ๋Š” ํ˜„์‹ค์€ ๋งค์šฐ ๋™์ (dynamic)์ด๋ฉฐ ์ž…์ฒด์ ์ด๋ฉฐ ๋น„์„ ํ˜•์ ์ด๋‹ค. ํ•ญ์ƒ ์ด ๋ถ€๋ถ„์˜ ๊ดด๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž˜ ์ฑ„์šธ ๊ฒƒ์ธ๊ฐ€ ํ•˜๋Š” ๊ฒƒ์ด ํ›Œ๋ฅญํ•œ ๋ถ„์„์˜ ๊ธธ์žก์ด๊ฐ€ ๋œ๋‹ค. ๊ฒฝ์Ÿ๊ณผ ์‚ฐ์—…๋ถ„์„์˜ ์„ธ ๋ฒˆ์งธ ์‹œ๊ฐ„์—์„œ ์‚ฐ์—…๊ตฌ์กฐ ๋ถ„์„์„ ํ†ตํ•ด ํ•ด๋‹น ์‚ฐ์—…๊ณผ ๊ฒฝ์Ÿ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์•˜๊ณ , SWOT ๋ถ„์„์„ ํ†ตํ•ด ํ™˜๊ฒฝ๊ณผ ์ž์‚ฌ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์•˜๋‹ค. ๋‹ค์Œ์€ ๊ฒฝ์Ÿ๊ณผ ์‚ฐ์—… ๋ถ„์„ ๋งˆ์ง€๋ง‰ ์ˆœ์„œ๋กœ ๊ธฐ์—…์˜ ์žฌ๋ฌด์„ฑ๊ณผ ๋ถ„์„์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. [1] 2016๋…„๋ถ€ํ„ฐ ํ™”๋‘๊ฐ€ ๋˜๊ณ  ์žˆ๋Š” ์ธ๊ณต์ง€๋Šฅ๊ณผ ๋กœ๋ด‡์˜ ํ™œ์šฉ, ์›์ž์žฌ ํ˜์‹ , ์ƒ์‚ฐ ๊ธฐ๋ฒ•์˜ ์ฒจ๋‹จํ™” ๋“ฑ์€ '๋Œ€๋Ÿ‰ ์ƒ์‚ฐ(Mass Production)'์„ '๋Œ€๋Ÿ‰ ๋งž์ถค(Mass Customization)'์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์žˆ๋‹ค. ์ฆ‰, ๋Œ€๋Ÿ‰ ์ƒ์‚ฐ ์ œํ’ˆ๋“ค์„ ์‹ผ ๋ง›์— ๊ตฌ๋งคํ•ด์•ผ ํ–ˆ๋˜ ์‹œ๋Œ€๋ฅผ ๋„˜์–ด ์†Œ๋น„์ž์˜ ๋‹ค์–‘ํ•œ ๊ธฐํ˜ธ๋ฅผ ๋ฐ˜์˜ํ•œ ๋งž์ถคํ˜• ์ œํ’ˆ์„ ์‹ธ๊ฒŒ ์‚ด ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ฐ˜์„ ๊ตฌ์ถ•ํ•˜์˜€๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค. [2] ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฐ ๊ฐ€์ •๋„ ์‹œ์žฅ์˜ ๋™ํ–ฅ ๋ฐ ๋ฐฉํ–ฅ์„ ์ž˜ ํŒŒ์•…ํ•˜๊ณ  ํˆฌ์žํ•˜์—ฌ์•ผ ํ•œ๋‹ค. 2010๋…„ ์ดˆ๋ฐ˜ ๋งˆ์“ฐ์‹œ๋‹ค์˜ ์ˆ˜๋„(ๆฐด้“) ์ฒ ํ•™์— ์˜ํ•ด ์–ด๋ ค์›€์„ ๊ฒช์—ˆ๋˜ ํŒŒ๋‚˜์†Œ๋‹‰์˜ ์‚ฌ๋ก€๋ฅผ ์ƒ๊ฐํ•ด ๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค. Part I. B2B ์‚ฌ์—…, ๋ฌด์—‡์ด ๋‹ค๋ฅผ๊นŒ? ํŒŒ๋‚˜์†Œ๋‹‰(Panasonic)์˜ ํšŒ์ƒ, ๊ทธ๋ฆฌ๊ณ  CES 2017 | B2B ์‚ฌ์—…์— ๋Œ€ํ•œ ์ฒซ ๋ฒˆ์งธ ์ด์•ผ๊ธฐ๋Š” ์†Œ๋‹ˆ(SONY)์™€ ํ•จ๊ป˜ ์ผ๋ณธ์˜ ๊ฐ„ํŒ ์ „๊ธฐโˆ™์ „์ž ๊ธฐ์—…์˜ ์œ„์น˜๋ฅผ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ๋Š” โ€˜ํŒŒ๋‚˜์†Œ๋‹‰(PANASONIC)โ€™์˜ ํšŒ์ƒ ์ด์•ผ๊ธฐ๋กœ ์‹œ์ž‘ํ•ด ๋ณด์ž. ํŒŒ๋‚˜์†Œ๋‹‰์€ โ€˜๋งˆ์“ฐ์‹œ๋‹ค ๊ณ ๋…ธ์Šค์ผ€(ๆพไธ‹ๅนธไน‹ๅŠฉ. 1894~1989)โ€™์— ์˜ํ•ด ์ฐฝ๋ฆฝ๋˜์—ˆ๋Š”๋ฐ, ๋งˆ์“ฐ์‹œ๋‹ค ๊ณ ๋…ธ์Šค์ผ€๋Š” ์ž‡์‡ผ๊ฒ๋ฉ”์ด(ไธ€็”Ÿๆ‡ธๅ‘ฝ) ์ •์‹ ์œผ๋กœ ์ผ๋ณธ์˜ ๊ธฐ์—… ๋ฌธํ™”๋ฅผ ๋ฐ”๊พผ ์ „์„ค์ ์ธ ์ธ๋ฌผ์ด๋ฉฐ โ€˜๋‚˜์‡ผ brunch.co.kr/@flyingcity/2 [3] Enterprise Resource Planning. ์ „์‚ฌ์  ์ž์›๊ด€๋ฆฌ ์‹œ์Šคํ…œ [4] Critical Success Factors ๊ฐ™์ด ์ฝ์–ด๋ณด๋ฉด ์ข‹์€ ์ฑ…! ๊ฒฝ์Ÿ์ „๋žต(Competitive Strategy), ๋งˆ์ดํด ํฌํ„ฐ, 1980 07. ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—…๋ถ„์„(4/4) 7.8 ์žฌ๋ฌด๋น„์œจ ๋ถ„์„ ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„์˜ ๋งˆ์ง€๋ง‰ ์ˆœ์„œ๋กœ ์žฌ๋ฌด๋ถ„์„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. ์ •ํ™•ํžˆ ๋งํ•˜๋ฉด ์žฌ๋ฌด๋น„์œจ ๋ถ„์„์ด๋‹ค. ๊ฒฝ์Ÿ๋ ฅ์„ ํŒ๋‹จํ•˜๋Š” ์ค‘์š”ํ•œ ์žฃ๋Œ€๋กœ ๊ฒฐ๊ตญ '๊ธฐ์—…์ด ๋ˆ์„ ์ž˜ ๋ฒŒ๊ณ  ์žˆ๋Š”๊ฐ€?'ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ฌธ์ œ์ด๋‹ค. NGO[1]๊ฐ€ ์•„๋‹Œ ์ด์ƒ, ๋น„์ „๋„ ์ข‹๊ณ  ์ „๋žต๋„ ์ข‹์ง€๋งŒ ๊ฒฐ๊ตญ ์‚ฌ์—…์„ ์ž˜ ํ•ด์„œ ๋ˆ์„ ๋ฒ„๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋ฏ€๋กœ ๊ทธ ๋ชฉ์ ์— ์–ด๋Š ์ •๋„ ๋ถ€ํ•ฉํ•˜๊ณ  ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ์žฌ๋ฌด๋น„์œจ ๋ถ„์„์€ ๊ธฐ์—…์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๋Š” ๊ถ๊ทน์ ์ธ ๋„๊ตฌ์ด๋‹ค. ์‚ฌ์—…์˜ ์„ฑ๊ณผ ๋ฐ ๊ทธ์™€ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ์ง€ํ‘œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๊ธฐ์—… ๊ฒฝ์Ÿ๋ ฅ์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๊ณ , ๋‚˜์•„๊ฐ€ ๊ทธ ๊ธฐ์—…์ด ์†ํ•œ ์‚ฐ์—… ๋‚ด ์ˆ˜์ต๋„ ์ถ”์ •ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์‚ฐ์—… ์ˆ˜์ต์„ฑ์˜ ๊ฐœ๋…์€ ๋Œ€๋‹จํžˆ ์ค‘์š”ํ•œ๋ฐ ํ”ํžˆ ๊ธˆ๊ด‘๊ณผ ๋™๊ด‘์˜ ๋น„์œ ๋ฅผ ๋งŽ์ด ๋“ ๋‹ค. ๋‚ด๊ฐ€ ์ง€ํ•˜ ๊นŠ์ˆ™์ด ๋“ค์–ด๊ฐ€์„œ ํž˜๋“ค๊ฒŒ ๊ด‘๋ฌผ์„ ์บ์ง€๋งŒ ๊ธˆ์„ ์บ๋Š” ๊ด‘๋ถ€๊ณผ ๋™์„ ์บ๋Š” ๊ด‘๋ถ€๊ฐ€ ๊ฐ–๊ฒŒ ๋˜๋Š” ๋Œ€๊ฐ€๋Š” ์™„์ „ํžˆ ๋‹ค๋ฅด๋‹ค. ์ฆ‰, ์‚ฐ์—… ์ˆ˜์ต์„ฑ์ด ๋†’์€ ์‚ฐ์—…์— ์†ํ•œ ๊ธฐ์—…์— ๊ทผ๋ฌดํ•˜๋Š” ๊ทผ๋กœ์ž๊ฐ€ ๋‹น์—ฐํžˆ ์ˆ˜ํ˜œ๊ฐ€ ๋งŽ์Œ์„ ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ๋‹ค[2]. ๊ทธ๋ž˜์„œ ์ฒซ ์ง์žฅ์ด ์ค‘์š”ํ•˜๊ณ  ๋™์ผ ์ง๋ฌด๋ผ ํ•˜๋”๋ผ๋„ ์—ฐ๋ด‰ ๋†’์€ ์‚ฐ์—…์„ ์ฐพ๊ฒŒ ๋œ๋‹ค. ๋ˆ„๊ตฐ๊ฐ€ ์ง์žฅ/์ง์—…์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์„ ํ•  ๋•Œ ๊ทธ๋Ÿฐ Tip์„ ์•Œ๋ ค์ฃผ์—ˆ๋”๋ผ๋ฉด ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์˜ ์ธ์ƒ์ด ๋‹ฌ๋ผ์กŒ์„ ๊ฒƒ์ด๋ฆฌ๋ผ. ์ผ๋ฐ˜์ ์œผ๋กœ ์žฌ๋ฌด๋น„์œจ ๋ถ„์„์„ ์œ„ํ•ด์„œ๋Š” 3๊ฐ€์ง€ ์žฌ๋ฌด์ œํ‘œ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค.[3] ๋Œ€์ฐจ๋Œ€์กฐํ‘œ (The Balance Sheet) ์†์ต๊ณ„์‚ฐ์„œ (The Income Sheet. I/S or The Profit and Loss Account. P/L) ํ˜„๊ธˆํ๋ฆ„ํ‘œ (The Cash Flow statement. C/F) ์ฒซ ๋ฒˆ์งธ, ๋Œ€์ฐจ๋Œ€์กฐํ‘œ๋Š” ํŠน์ • ์‹œ์ (๊ฒฐ์‚ฐ์‹œ์ )์— ๊ธฐ์—…์ด ์–ด๋–ค ์ž์‚ฐ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š”์ง€, ๋นš์€ ์–ผ๋งˆ์ธ์ง€, ์ž๋ณธ์€ ์–ผ๋งˆ์ธ์ง€ ๋ณด์—ฌ์ฃผ๋Š” ์„œ๋ฅ˜๋กœ์„œ ๋ณต์‹๋ถ€๊ธฐ์˜ ์ขŒ๋ณ€๊ณผ ์šฐ๋ณ€ ์ฆ‰, ์ฐจ๋ณ€(Debtor.Dr)๊ณผ ๋Œ€๋ณ€(Creditor.Cr)์ด ๊ฐ™์•„์•ผ ํ•จ(Balance)์„ ์›์น™์œผ๋กœ ํ•œ๋‹ค. Table III-10์€ ๋Œ€์ฐจ๋Œ€์กฐํ‘œ์˜ ๊ตฌ์„ฑ์„ ๋ณด์—ฌ์ฃผ๋Š”๋ฐ[4], ์ด๋Š” ๊ธฐ์—… ์ž์‚ฐ์˜ ์ดํ•ฉ์€ ๋ถ€์ฑ„์˜ ์ดํ•ฉ๊ณผ ์ž๋ณธ์˜ ์ดํ•ฉ์„ ๋”ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์•„์•ผ ํ•œ๋‹ค๋Š” ์›์น™์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค. Table III-10. ๋Œ€์ฐจ๋Œ€์กฐํ‘œ์˜ ๊ตฌ์„ฑ ๋‘ ๋ฒˆ์งธ ์‚ดํŽด๋ณผ ์žฌ๋ฌด์ œํ‘œ๋Š” ์†์ต๊ณ„์‚ฐ์„œ์ด๋‹ค. ์†์ต๊ณ„์‚ฐ์„œ๋Š” ๊ธฐ์—…์ด ๋ฒŒ์–ด๋“ค์ด๋Š” ์ˆ˜์ต์„ ๊ฐ์ข… ๋น„์šฉ์„ ์ œ๊ฑฐํ•ด๋‚˜๊ฐ€๋ฉด์„œ ์ตœ์ข… ์ด์ต์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒƒ์„ ์„ค๋ช…ํ•˜๋Š” ์„œ๋ฅ˜๋กœ์„œ Figure III-18์„ ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ์ฝ์–ด๋‚˜๊ฐ€๋ฉด ๋œ๋‹ค. ๊ธฐ์—…์ด ๋งค์ถœ์•ก์„ ๋ฒŒ์–ด๋“ค์ด๋ฉด ๊ฑฐ๊ธฐ์„œ ์ง์ ‘ ๋น„์šฉ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์›๊ฐ€๋ฅผ ์ฐจ๊ฐํ•˜๋ฉด ๋งค์ถœ ์ด์ด์ต์ด ๋œ๋‹ค. Figure III-19. ์†์ต๊ณ„์‚ฐ์„œ์˜ ์ด์ต ์‚ฐ์ถœ[5] ๋งค์ถœ ์ด์ด์ต์—์„œ ํŒ๋งค๋น„์™€ ๊ด€๋ฆฌ๋น„๋ฅผ ์ฐจ๊ฐํ•œ ๊ฒƒ์ด ์˜์—…์ด์ต์ด๋‹ค. ์˜์—…์ด์ต์—์„œ ์˜์—…ํ™œ๋™ ์™ธ์˜ ์ด์ต ์˜ˆ๋ฅผ ๋“ค์–ด ๋ถ€๋™์‚ฐ ์ž„๋Œ€๋ฃŒ๋‚˜ ํŠนํ—ˆ๋ฃŒ ๋“ฑ ์˜์—… ์™ธ ์ˆ˜์ต์„ ๋”ํ•ด์ฃผ๊ณ , ์˜์—… ์™ธ ๋น„์šฉ์„ ์ฐจ๊ฐํ•˜๋ฉด ๊ณ„์† ์‚ฌ์—…์ด์ต์ด ๋œ๋‹ค. ๊ณ„์† ์‚ฌ์—…์ด์ต์€ IFRS[6] ๋„์ž… ์ด์ „์— ๊ฒฝ์ƒ์ด์ต์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋˜ ๊ฒƒ์œผ๋กœ ์ง€๋ถ„๋ฒ•ํ‰๊ฐ€์ด์ต๊ณผ ๊ฐ™์€ ํŠน๋ณ„์ด์ต์„ ๋”ํ•˜๊ณ  ํŠน๋ณ„์†์‹ค์„ ์ œํ•˜๋ฉด ๋ฒ•์ธ์„ธ๋น„์šฉ ์ฐจ๊ฐ์ „์ˆœ์ด์ต์ด ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ฒ•์ธ์„ธ๋ฅผ ์ฐจ๊ฐํ•˜๋ฉด ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹น๊ธฐ์ˆœ์ด์ต์ด ๋‚˜์˜จ๋‹ค. ์„ธ ๋ฒˆ์งธ, ํ˜„๊ธˆํ๋ฆ„ํ‘œ๋Š” ์‹ค์ œ ํ˜„๊ธˆํ๋ฆ„์„ ํ‘œ๊ธฐํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ๊ฐ€์ƒ๊ฐ๊ณผ ๊ฐ™์ด ๊ฐ€์ƒ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ ๊ฒƒ์„ ๋ชจ๋‘ ๋ฐ˜์˜ํ•œ๋‹ค. Figure III-19๋ฅผ ๋ณด๋ฉด ๋Œ€์ฐจ๋Œ€์กฐํ‘œ์™€ ์†์ต๊ณ„์‚ฐ์„œ๊ฐ€ ํ˜„๊ธˆํ๋ฆ„ํ‘œ์— ์–ด๋–ป๊ฒŒ ๋ฐ˜์˜๋˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ „๊ธฐ์— ์˜์—…ํ™œ๋™์„ ์ž˜ํ•˜๋ฉด ์ˆ˜์ต์„ ๋ฐœ์ƒ์‹œํ‚ค๊ฒŒ ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋น„์šฉ์„ ์ œํ•˜๋ฉด ์ˆœ์ด์ต์ด ๋‚จ๋Š”๋ฐ ์ด๊ฒƒ์€ ์ฃผ์ฃผ์—๊ฒŒ ๋ฐฐ๋‹น๊ธˆ์œผ๋กœ ๋Œ์•„๊ฐ€๊ฑฐ๋‚˜ ๋‹ค์Œ ํ•ด ๋‚˜์˜ ์ž๋ณธ์— ํฌํ•จ๋œ๋‹ค. ์ฆ‰, ์ „์ฒด์ ์ธ ์ž์‚ฐ์ด ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ฒฐ๊ตญ, ์žฅ์‚ฌ๋ฅผ ์ž˜ํ•˜๋ฉด ์ฃผ์ฃผ์—๊ฒŒ ์ด์ต๋„ ์ฃผ๊ฒŒ ๋˜๊ณ  ๋‚ด ์ž์‚ฐ๋„ ์ฆ๊ฐ€ํ•œ๋‹ค๋Š” ๊ฐœ๋…์ด๋‹ค. ์—ฌ๊ธฐ์„œ ํ˜„๊ธˆํ๋ฆ„ํ‘œ๋ฅผ ๋ณด๋ฉด ๋ฐฐ๋‹น๊ธˆ๊ณผ ์ด์ต์ž‰์—ฌ๊ธˆ์œผ๋กœ ๊ตฌ์„ฑ๋จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. Figure III-20. ํ˜„๊ธˆํ๋ฆ„ํ‘œ์˜ ๊ตฌ์„ฑ ์žฌ๋ฌด๋น„์œจ ๋ถ„์„์€ ์‚ดํŽด๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ์žฌ๋ฌด์ œํ‘œ์˜ ์ค‘์š” ๊ณ„์ • ๊ฐ’๋“ค์˜ ๋น„์œจ์„ ๊ตฌํ•ด ๊ธฐ์—…์˜ ์žฌ๋ฌด์  ํŠน์„ฑ์„ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋ฉฐ Table III-11๊ณผ ๊ฐ™์ด 4๊ฐ€์ง€ ๊ด€์ ์—์„œ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค. Table III-11. ์žฌ๋ฌด๋น„์œจ ๋ถ„์„์˜ ๊ด€์  ์ฒซ ๋ฒˆ์งธ, ์ˆ˜์ต์„ฑ ๋น„์œจ(Profitability Ratio) ๋ถ„์„์€ ๊ธฐ์—…์ด ์˜์—… ํ™œ๋™์„ ์–ผ๋งˆ๋‚˜ ์ž˜ ํ•˜๊ณ  ์žˆ๋Š”์ง€ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ Table III-12๋Š” ๊ธฐ์—…์˜ ์ˆ˜์ต์„ฑ์„ ๊ฐ€๋Š ํ•  ์ˆ˜ ์žˆ๋Š” ์ฃผ์š” ๋น„์œจ์„ ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋‹ค. Table III-12. ์ˆ˜์ต์„ฑ ๋น„์œจ ๋ถ„์„ - ์ฃผ์š” ๋น„์œจ ์ค‘์‹ฌ[7] ์ด ์ž๋ณธ์ˆœ์ด์ต๋ฅ (Return on Investment)์€ ์ˆœ์ด์ต์„ ์ด ์ž๋ณธ(๋˜๋Š” ์ด์ž์‚ฐ)์œผ๋กœ ๋‚˜๋ˆˆ ๋น„์œจ๋กœ ๊ธฐ์—…์— ํˆฌ์ž๋œ ์ด ์ž๋ณธ์ด ์ตœ์ข…์ ์œผ๋กœ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์ด์ต์„ ๋‚ด์—ˆ๋‚˜ ์‚ฐ์ถœํ•˜๋Š” ๋น„์œจ์ด๋‹ค. ์ฆ‰, ์ข…ํ•ฉ์ ์ธ ๊ฒฝ์˜ ์ƒํƒœ๋ฅผ ์š”์•ฝํ•ด์„œ ๋‚˜ํƒ€๋‚ด๋Š” ๋น„์œจ์ธ๋ฐ ๋ถ„๋ชจ๋Š” ์ฃผ์ฃผ์™€ ์ฑ„๊ถŒ์ž๊ฐ€ ์ œ๊ณตํ•œ ๊ธˆ์•ก์ด๊ณ  ๋ถ„์ž๋Š” ์ฃผ์ฃผ์—๊ฒŒ ๊ท€์†๋˜๋Š” ์ด์ต์ด๋ฏ€๋กœ ์„œ๋กœ ์ž˜ ๋Œ€์‘๋˜์ง€ ์•Š์ง€๋งŒ ๊ธฐ์—…์˜ ๋Œ€์ฐจ๋Œ€์กฐํ‘œ(BS)์™€ ์†์ต๊ณ„์‚ฐ์„œ(IS)๋ฅผ ์ข…ํ•ฉ์ ์œผ๋กœ ์ง‘์•ฝํ•œ ์„ฑ๊ณผ ์ง€ํ‘œ๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๋ฒˆ์งธ, ์œ ๋™์„ฑ ๋น„์œจ(Liquidity Ratio) ๋ถ„์„. ์—ฌ๊ธฐ์„œ ์œ ๋™์„ฑ์€ ํ˜„๊ธˆํ™”๋  ์ˆ˜ ์žˆ๋Š” ์„ฑ์งˆ์„ ๋งํ•˜๋ฉฐ ์œ ๋™์„ฑ์ด ๋†’๋‹ค๋Š” ๊ฒƒ์€ ํ˜„๊ธˆํ™”๊ฐ€ ์‰ฝ๋‹ค๋Š” ๋ง์ด๋‹ค. ์œ ๋™ ๋น„์œจ(Current Ratio)์€ ์€ํ–‰ ๋Œ€์ถœ ์‹œ ์ฑ„๋ฌด์ž์˜ ์ง€๊ธ‰ ๋Šฅ๋ ฅ์„ ํŒ๋‹จํ•˜๋Š” ์ง€ํ‘œ๋กœ ์ด์šฉ๋˜์–ด ์™”๊ธฐ ๋•Œ๋ฌธ์— ์€ํ–‰๊ฐ€ ๋น„์œจ(Bankerโ€™s Ratio)๋ผ๊ณ ๋„ ํ•œ๋‹ค. Table III-13. ์œ ๋™์„ฑ ๋น„์œจ ๋ถ„์„ - ์ฃผ์š” ๋น„์œจ ์ค‘์‹ฌ ์„ธ ๋ฒˆ์งธ, ๋ ˆ๋ฒ„๋ฆฌ์ง€ ๋น„์œจ ๋ถ„์„. ๋ ˆ๋ฒ„๋ฆฌ์ง€(Leverage)๋Š” ์ง€๋ ›๋Œ€ ์ž‘์šฉ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์†์ต ํ™•๋Œ€ ํšจ๊ณผ๋ฅผ ๊ฐ€์ ธ๋‹ค์ฃผ๋Š” ํƒ€์ธ์ž๋ณธ ์˜์กด๋„๋ฅผ ์ง€์นญํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ณผ๋„ํ•œ ๋ ˆ๋ฒ„๋ฆฌ์ง€ ํšจ๊ณผ ์˜์กด์€ ๋งํ•˜๋Š” ์ง€๋ฆ„๊ธธ์ด๊ธฐ๋„ ํ•˜๋‹ค. Table III-14. ๋ ˆ๋ฒ„๋ฆฌ์ง€ ๋น„์œจ ๋ถ„์„ - ์ฃผ์š” ๋น„์œจ ์ค‘์‹ฌ ๋„ค ๋ฒˆ์งธ, ์‹œ์žฅ๊ฐ€์น˜ ๋น„์œจ ๋ถ„์„์€ ํ•ด๋‹น ๊ธฐ์—…์˜ ์ฃผ๊ฐ€๋ฅผ ํ‰๊ฐ€ํ•จ์œผ๋กœ์จ ์‹œ์žฅ ๊ฐ€์น˜(Market Value)๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋Š” ์ฒ™๋„๊ฐ€ ๋œ๋‹ค. Table III-15. ์‹œ์žฅ๊ฐ€์น˜ ๋น„์œจ ๋ถ„์„ - ์ฃผ์š” ๋น„์œจ ์ค‘์‹ฌ[8] ๋˜ํ•œ, ํˆฌํ•˜์ž๋ณธ์˜ ์ˆœ์ด์ต๋ฅ  (ROIC)[9] ๋ถ„์„๋„ ์œ ์˜๋ฏธํ•œ๋ฐ ์‚ฌ์—…์˜ ์›๊ฐ€ ๋™์ธ(Cost drivers)์„ ํŒŒ์•…ํ•˜์—ฌ ์–ด๋–ค ์กฐ๊ฑด์ด ์›๊ฐ€ ๋™์ธ๋“ค์„ ๋ณ€๋™์‹œํ‚ค๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ๊ฒƒ์ด ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Value Driver ๋ถ„์„์€ ์ด๋Ÿฌํ•œ ๋‹ค์–‘ํ•œ ๊ฐ€์น˜ ๋™์ธ์„ ํŒŒ์•…ํ•˜์—ฌ ์ œํ’ˆ ๋˜๋Š” ์‚ฐ์—… ์ƒ๋ช…์ฃผ๊ธฐ ์†์—์„œ ์–ด๋–ป๊ฒŒ ๊ฐ€์น˜๋‚˜ ๋น„์šฉ์„ ๋ฐฐ๋ถ„ํ•ด์•ผ ํ•˜๋Š”์ง€ ๊ฒฝ์˜ ๊ด€์ ์˜ ํ†ต์ฐฐ๋ ฅ์„ ์ œ๊ณตํ•ด ์ค€๋‹ค. Figure III-21. Value Driver Tree ์‚ฌ๋ก€ ์ง€๊ธˆ๊นŒ์ง€ ๊ฒฝ์Ÿ๊ณผ ์‚ฐ์—… ๋ถ„์„์„ ํ†ตํ•ด ์‹œ์žฅ๊ณผ ๊ฒฝ์Ÿ, ์ž์‚ฌ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์•˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์†Œ๊ฐœํ•œ ๋‹ค์–‘ํ•œ ๋ถ„์„ ๊ธฐ๋ฒ•๋“ค์˜ ๋ฌธ์ œ ์ฆ‰, ๊ณ ์ „์ ์ธ ๋ถ„์„ ๋ฐฉ๋ฒ•์˜ ๋ฌธ์ œ๋“ค์€<NAME>๊ฐ™์ด<NAME>์  ๋ถ„์„์— ์น˜์ค‘ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.<NAME>์  ๋ถ„์„์ด ๋‚˜์˜๋‹ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์˜ค๋Š˜๋‚  ๊ธ‰๋ณ€ํ•˜๋Š” ์‚ฌ์—… ํ™˜๊ฒฝ ์†์—์„œ ์ •๋ณด ์ ์‹œ์„ฑ์ด ๋‹ด๋ณด ๋ฐ›์ง€ ๋ชปํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์–ด๋–ค ์‚ฐ์—…์€ ์‹œ์‹œ๊ฐ๊ฐ ๋ณ€ํ•˜๋ฉฐ ๋˜ ์–ด๋–ค ์‚ฐ์—…์€ ๋งค์šฐ ๋ณด์ˆ˜์ ์ด๋ผ ํŠธ๋ Œ๋“œ๋ผ๊ณ  ํ•  ๊ฒƒ๋„ ์—†๋Š” ์‚ฐ์—…๋„ ์žˆ๋‹ค. ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ปจ์„คํ„ดํŠธ๋“ค์€ ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์„ ๊ณ ๋ คํ•˜์—ฌ ์–ด๋–ค ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ๊ทธ๋ฆฌ๊ณ  ๋˜ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์„ ์–ด๋–ป๊ฒŒ ๋ณด์™„ํ•  ๊ฒƒ์ธ์ง€ ๊ณ ๋ฏผํ•ด์•ผ ํ•œ๋‹ค. ์ด์–ด์ง€๋Š” ์ œ8์žฅ์—์„œ๋Š” ์‚ฌ์—… ํ™˜๊ฒฝ์„ ํŒŒ์•…ํ•˜๋Š”๋ฐ ๊ฐ€์žฅ ์ค‘์š”ํ•œ '๊ณ ๊ฐ'์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. [1] Non-Government Organization. ์ •๋ถ€๋‚˜ ์ •๋ถ€ ๊ด€๊ณ„ ๊ธฐ๊ด€์ด ์•„๋‹Œ ์ˆœ์ˆ˜ ๋ฏผ๊ฐ„๋‹จ์ฒด๋ฅผ ์ง€์นญํ•˜๋Š” ๋ง๋กœ ๋น„์˜๋ฆฌ๋ฅผ ์ถ”๊ตฌํ•œ๋‹ค. [2] 2017๋…„ ๊ธฐ์ค€ ํ˜„์žฌ ํ•œ๊ตญ ์‚ฐ์—… ์ค‘์—์„œ๋Š” ๊ธˆ์œต, ์ •์œ , ๋ฐ˜๋„์ฒด, ํ†ต์‹  ๋“ฑ์ด ํƒ€ ์‚ฐ์—… ๋Œ€๋น„ ์‚ฐ์—… ์ˆ˜์ต์„ฑ์ด ๋†’๋‹ค. [3] ์ด์ต ์ž‰์—ฌ๋ถ„ ์ฒ˜๋ถ„ ๊ณ„์‚ฐ์„œ๋Š” 2011๋…„ IFRS ๋„์ž… ์ดํ›„๋ถ€ํ„ฐ ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. [4] ๊ฐ„ํ˜น ์˜์–ด ๋‹จ์–ด ๋˜๋Š” ๋ฌธ์žฅ์ด ํ›จ์”ฌ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋‹ค๋Š” ๋Š๋‚Œ์ด ๋“œ๋Š”๋ฐ Table III-10๋„ ๊ทธ๋ ‡๋‹ค [5] Cost of goods sold: COGS, Selling goods and administrative expenses, indirect costs, and depreciation: SG&A, Earnings Before Interests and Taxes [6] International Financial Reporting Standards ๊ธฐ์—…์˜ ํšŒ๊ณ„ ์ฒ˜๋ฆฌ์™€ ์žฌ๋ฌด์ œํ‘œ์— ๋Œ€ํ•œ ๊ตญ์ œ์  ํ†ต์ผ์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•œ ๊ตญ์ œํšŒ๊ณ„๊ธฐ์ค€ [7] Profit Before Interest and Tax; Return on Asset(ROA) / Return on Capital Employed(ROCE) [8] Earnings Per Share; Pricing Earning Ratio [9] Return On Invested Capital 08. ๊ณ ๊ฐ ์š”๊ตฌ ๋ถ„์„(1/3) ์•ž์„œ ์†Œ๊ฐœํ•œ ๋งˆ์ดํด ํฌํ„ฐ์˜ Value Chain ๋ถ„์„์„ ์ƒ๊ธฐํ•ด ๋ณด์ž. ๊ธฐ์—…์˜ Value Chain ๋‚ด ๋ชจ๋“  ํ™œ๋™์„ ์œ ๋ฐœํ•˜๋Š” ๊ทผ๋ณธ ์›์ธ์„ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๊ทธ๊ฒƒ์€ ๊ธฐ์—…์ด ์ƒ์‚ฐํ•˜๋Š” ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ๊ณ ๊ฐ์˜ ์ƒ๊ฐ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์ผ€ํŒ…์—์„œ๋Š” ๊ทธ ์ƒ๊ฐ์„ ๋‹จ๊ณ„์ ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋‹ˆ์ฆˆ(Needs), ์›์ธ (Wants), ์ˆ˜์š”(Demand)๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค. ์ˆ˜์š”๊ฐ€ ์ข€ ๋” ๊ตฌ์ฒดํ™”๋˜๋ฉด ์š”๊ตฌ(Requirements)๊ฐ€ ๋˜๊ณ  ๊ธฐ์—…์—๊ฒŒ ์š”๊ตฌ๋Š” ๋งค์šฐ ์‹ค์ œ์ ์ธ ํ•ญ๋ชฉ์ด ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์„œ์šธ์—์„œ ๋ถ€์‚ฐ๊นŒ์ง€ ๊ฐ€๊ณ  ์‹ถ์€ ์ƒ๊ฐ์ด ์žˆ๋Š”๋ฐ(๋‹ˆ์ฆˆ), ๋น„ํ–‰๊ธฐ๋ฅผ ํƒ€๊ณ  ๊ฐˆ์ง€ KTX๋ฅผ ํƒ€๊ณ  ๊ฐˆ์ง€ ๊ณ ์†๋ฒ„์Šค๋ฅผ ํƒ€๊ณ  ๊ฐˆ์ง€ ๋“ฑ ๊ตํ†ตํŽธ์„ ์ƒ๊ฐํ•ด ๋ณผ ํ•„์š”๊ฐ€ ์žˆ๊ณ (์›์ธ ), ๋น„์šฉ ์ธก๋ฉด์—์„œ ๋ณผ ๋•Œ ๋น„ํ–‰๊ธฐ๋ณด๋‹ค๋Š” ๊ฐ€๊ฒฉ์ด ์‹ธ๊ณ  ๊ณ ์†๋ฒ„์Šค๋ณด๋‹ค ๋นจ๋ฆฌ ๋„์ฐฉํ•˜๋Š” KTX๋ฅผ ํƒ€๊ณ  ๊ฐ€๊ฒ ๋‹ค๊ณ  ๊ฒฐ์ •ํ•˜๋ฉด ๊ทธ๊ฒƒ์ด โ€˜์ˆ˜์š”โ€™๊ฐ€ ๋œ๋‹ค. Figure III-22. ๊ณ ๊ฐ ๋‹ˆ์ฆˆ/์›์ธ /๋””๋งจ๋“œ ๋‹ˆ์ฆˆ๋ฅผ ์›์ธ , ์ˆ˜์š” ๋“ฑ์œผ๋กœ ์ „ํ™˜์‹œํ‚ค๋Š” ๋งˆ์ผ€ํŒ…์˜ ๋ณธ์งˆ์ ์ธ ์—ญํ• ์ด๋ผ๋ฉด ์ปจ์„คํŒ…์—์„œ๋Š” ๊ทธ๊ฑธ ์ž˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๊ฐ€์ด๋“œ ํ•˜๋Š” ๊ฒƒ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•œํŽธ, ๊ทธ๋Ÿฐ ๊ณ ๊ฐ์˜ ์š”๊ตฌ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ํŒŒ์•…ํ•˜๋Š” ์ผ์€ ์†Œ์œ„ ๋งํ•˜๋Š” ๊ณ ๊ฐ ์š”๊ตฌ์‚ฌํ•ญ ๋ถ„์„์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์–ด์  ๋‹ค๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๋Š” ๋ฌด์—‡์ธ์ง€? ๋˜ ๊ทธ๊ฒƒ์„ ์‚ฐ์—…์ด๋‚˜ ์‹œ์žฅ์—์„œ ์–ด๋–ป๊ฒŒ ํ‘œ์ถœํ•˜๊ณ  ์žˆ๋Š”์ง€ ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์„ ์•Œ์•„๋‚ด๋Š” ๊ฒƒ์ด ๊ณ ๊ฐ ๋ถ„์„, ๊ณ ๊ฐ ์š”๊ตฌ์‚ฌํ•ญ ๋ถ„์„์ด๋‹ค. ๊ณ ๊ฐ ์š”๊ตฌ ๋ถ„์„์˜ ๊ด€์ ์—์„œ ๋ณด๋ฉด ๊ฒฐ๋ก ์ ์œผ๋กœ ํ•ด์•ผ ํ•  ์ผ์€ ํฌ๊ฒŒ ๋‹ค์Œ 3๊ฐ€์ง€์ด๋‹ค. ์–ด๋–ค ๊ณ ๊ฐ์—๊ฒŒ ์ง‘์ค‘ํ•  ๊ฒƒ์ธ๊ฐ€? ๊ทธ ๊ณ ๊ฐ์„ ์–ด๋–ป๊ฒŒ ํ™•๋ณดํ•  ๊ฒƒ์ธ๊ฐ€? ํ™•๋ณด๋œ ๊ณ ๊ฐ์„ ์–ด๋–ป๊ฒŒ ์œ ์ง€ํ•  ๊ฒƒ์ธ๊ฐ€? ์‹œ์žฅ์ด๋‚˜ ๊ณ ๊ฐ์„ ์ƒ์„ธํ•˜๊ฒŒ ๋‚˜๋ˆ„์–ด ๊ธฐ์—…์˜ ์„ฑ๊ณผ ์ฐฝ์ถœ์— ์œ ๋ฆฌํ•œ ์‹œ์žฅ์ด๋‚˜ ๊ณ ๊ฐ์„ ์„ ์ •ํ•˜๊ณ , ์–ด๋–ค ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค๋ฅผ ํ†ตํ•ด ๊ณ ๊ฐ ๊ฐ€์น˜๋ฅผ ์‹คํ˜„ํ•  ๊ฒƒ์ธ์ง€, ํ˜•์„ฑ๋œ ๊ณ ๊ฐ ๊ด€๊ณ„๋ฅผ ์–ด๋–ป๊ฒŒ ์ง€์†์ ์œผ๋กœ ์œ ์ง€ํ•  ๊ฒƒ์ธ์ง€ ๋“ฑ์— ๋Œ€ํ•œ ์ „๋žต ๋Œ€์•ˆ์˜ ๋„์ถœ์ด ๊ณ ๊ฐ ์š”๊ตฌ ๋ถ„์„์˜ ํ•ต์‹ฌ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์ด์— ๋Œ€ํ•ด ์ข€ ๋” ์ƒ์„ธํžˆ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. Figure III-23. ๊ณ ๊ฐ ์š”๊ตฌ๋ถ„์„ ๋กœ๋“œ๋งต 8.1 ์„ธ๋ถ„ํ™”(Segmentation) ์ฒซ ๋ฒˆ์งธ ๋‹ค๋ฃฐ ๊ฒƒ์€ ์„ธ๋ถ„ํ™”(Segmentation)์ด๋‹ค. ์„ธ๋ถ„ํ™”๋Š” ์œ ์‚ฌํ•œ ํŠน์ง•์„ ๊ฐ€์ง„ ๊ฒƒ๋“ค๋ผ๋ฆฌ ๋ฌถ์–ด์„œ ๊ตฌ๋ถ„ ์ง“๋Š” ๊ฒƒ์„ ๋งํ•˜๋Š”๋ฐ, ๋งˆ์ผ€ํŒ… ์ „๋žต ์ˆ˜๋ฆฝ์—์„œ ์‹œ์žฅ์ด๋‚˜ ๊ณ ๊ฐ์„ ์˜๋ฏธ ์žˆ๊ฒŒ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ STP[1]์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„์ด๊ธฐ๋„ ํ•˜๋‹ค. ๋˜ํ•œ, B2C ์‚ฌ์—…๊ณผ B2B ์‚ฌ์—…์ด ์‹œ์žฅ์ด๋‚˜ ๊ณ ๊ฐ ์„ธ๋ถ„ํ™”๋ฅผ ๊ณ ๋ฏผํ•˜๋Š” ๊ด€์ ์€ ์„œ๋กœ ๋งŽ์ด ๋‹ค๋ฅด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์‹œ์žฅ ์„ธ๋ถ„ํ™”(Market Segmentation)์˜ ๋ชฉ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ •ํ™•ํ•œ ์‹œ์žฅ ์ƒํ™ฉ์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์€ ์š•๊ตฌ๋‚˜ ๊ตฌ๋งค ๋™๊ธฐ ๋“ฑ์„ ํŒŒ์•…ํ•˜์—ฌ ํ–ฅํ›„ ๋ณ€ํ™”ํ•˜๋Š” ์ˆ˜์š”์— ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ค€์ด ๋œ๋‹ค. ๊ธฐ์—…์˜ ๊ฒฝ์Ÿ ์ขŒํ‘œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์–ด๋–ค ์‹œ์žฅ์—์„œ ํ”Œ๋ ˆ์ดํ•  ๊ฒƒ์ธ์ง€ ๋˜๋Š” ์–ด๋–ค ๊ณ ๊ฐ์—๊ฒŒ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ๊ณต๊ธ‰ํ•  ๊ฒƒ์ธ์ง€ ๋“ฑ์— ๋”ฐ๋ผ ๊ฒฝ์Ÿ ๊ฐ•๋„๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค. ์„ธ๋ถ„ํ™”๋ฅผ ํ†ตํ•ด ๊ธฐ์—…์˜ ๊ฐ•์ ๊ณผ ๊ธฐํšŒ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์‹œ์žฅ ๋˜๋Š” ๊ณ ๊ฐ์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค. ์ •ํ™•ํ•œ ํ‘œ์ (target)์˜ ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค. ์„ธ๋ถ„ํ™”๋ฅผ ํ†ตํ•ด ๊ฐ ์„ธ๊ทธ๋จผํŠธ์˜ ๋งค๋ ฅ๋„๋ฅผ ํ‰๊ฐ€ํ•˜์—ฌ ๋งˆ์ผ€ํŒ… ํ™œ๋™์˜ ๋ฐฉํ–ฅ์„ ์„ค์ •ํ•˜๊ณ  ์ง‘์ค‘ํ•ด์•ผ ํ•œ๋‹ค. ์„ธ๋ถ„์‹œ์žฅ์— ๋Œ€ํ•œ ๋งˆ์ผ€ํŒ… ์„ฑ๊ณผ ๋ฐ ์†Œ๋น„์ž ๋ฐ˜์‘์„ ๋ถ„์„ํ•˜์—ฌ ๋งˆ์ผ€ํŒ… ์ž์›์„ ํšจ์œจ์ ์œผ๋กœ ๋ฐฐ๋ถ„ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. B2C ์‚ฌ์—…์˜ ์‹œ์žฅ ์„ธ๋ถ„ํ™”๋Š” ์†Œ๋น„์ž(Consumer) ๊ฐœ๊ฐœ์ธ์ด ์–ด๋–ค ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๊ฐ€๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์‹œ์žฅ์„ ๊ตฌ๋ถ„ํ•˜๋Š”๋ฐ ์ผ๋ฐ˜์ ์œผ๋กœ TableIII-16๊ณผ ๊ฐ™์€ ๊ธฐ์ค€์„ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. Table III-16. ์‹œ์žฅ์„ธ๋ถ„ํ™”์˜ ๋ณ€์ˆ˜ Table III-16์˜ ๋ณ€์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•ด ์ˆ˜์ฐจ๋ก€ ์„ธ๋ถ„ํ™” ์ž‘์—… ํ›„, ์˜๋ฏธ ์žˆ๋Š” ์„ธ๋ถ„ ์‹œ์žฅ(segment)์ด ๋„์ถœ๋˜๋Š”๋ฐ, ์„ธ๋ถ„ ์‹œ์žฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์š”๊ฑด์„ ๊ฐ–์ถ”์–ด์•ผ ํ•œ๋‹ค. ์„ธ๋ถ„์‹œ์žฅ์€ ์ •๋ณด์˜ ์ธก์ •๊ณผ ํš๋“์ด ์šฉ์ดํ•ด์•ผ ํ•œ๋‹ค ์„ธ๋ถ„์‹œ์žฅ์€ ์ˆ˜์ต์„ฑ์ด ๋ณด์žฅ๋˜์–ด์•ผ ํ•œ๋‹ค ์„ธ๋ถ„์‹œ์žฅ์€ ์ ‘๊ทผ ์šฉ์ด์„ฑ๊ณผ ์ „๋‹ฌ์„ฑ์ด ๋†’์•„์•ผ ํ•œ๋‹ค ์„ธ๋ถ„์‹œ์žฅ์€ ๋ช…ํ™•ํ•œ ๊ตฌ๋ถ„ ์„ฑ๊ณผ ์ฐจ๋ณ„๋œ ๋ฐ˜์‘์„ฑ์ด ๋†’์•„์•ผ ํ•œ๋‹ค ์„ธ๋ถ„์‹œ์žฅ์€ ์ผ๊ด€์„ฑ๊ณผ ์ง€์†์„ฑ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค ๋˜ํ•œ, ์‹œ์žฅ ์„ธ๋ถ„ํ™”์— ์ด์–ด ๊ณ ๊ฐ ์„ธ๋ถ„ํ™”(Customer Segmentation)๋ฅผ ํ•  ํ•„์š”๋„ ์žˆ๋Š”๋ฐ ์ด๋•Œ๋Š” ๋ณดํ†ต RFM์— ๊ธฐ์ค€ํ•˜์—ฌ ๋ถ„๋ฅ˜ํ•˜๊ฒŒ ๋œ๋‹ค. RFM ์ด๋ž€ Recency ์ตœ๊ทผ ์–ธ์ œ ๊ตฌ๋งคํ•˜์˜€๋‚˜? Frequently ์–ผ๋งˆ๋‚˜ ์ž์ฃผ ๊ตฌ๋งคํ•˜์˜€๋‚˜? Monetary Value ์–ผ๋งˆ๋‚˜ ๋งŽ์ด ๊ตฌ๋งคํ•˜์˜€๋‚˜? (๊ธˆ์ „์  ๊ด€์ ) ์œ„์˜ 3๊ฐ€์ง€ ์‚ฌํ•ญ์„ ๊ฐœ์ธ ์†Œ๋น„์ž๋ณ„๋กœ ํ‰๊ฐ€ํ•˜์—ฌ ์ง‘์ค‘์ ์œผ๋กœ ๋งˆ์ผ€ํŒ…์„ ํ•ด์•ผ ํ•  ๊ณ ๊ฐ ๊ตฐ์ด ๋ฌด์—‡์ธ์ง€ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์š”์ฆ˜์€ ๋‹ค์–‘ํ•œ ๋งˆ์ผ€ํŒ… ์ •๋ณด๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๊ฐ€์ƒ์˜ ํ‘œ๋ณธ ์ธ๋ฌผ์„ ๋งŒ๋“ค๊ณ  ๊ทธ์˜ ์ƒํ™œ ์†์—์„œ ๋‹ค์–‘ํ•œ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ์บ ํŽ˜์ธ์„ ์ „๊ฐœํ•˜๋Š” ๋ฐฉ์•ˆ๋„ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. ํ•œํŽธ, B2C ์‚ฌ์—…๊ณผ ๋‹ฌ๋ฆฌ B2B ์‚ฌ์—…์—์„œ๋Š” B2B ์‚ฌ์—…์˜ ์†์„ฑ[1] ์ƒ, ์‹œ์žฅ ์„ธ๋ถ„ํ™”๋ณด๋‹ค๋Š” ๊ณ ๊ฐ ์„ธ๋ถ„ํ™”์— ๋ณด๋‹ค ํฐ ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌํ•˜๋ฉฐ ์ฒ ์ €ํ•˜๊ฒŒ ํŒŒ๋ ˆํ†  ๋ฒ•์น™(Paretoโ€™s Law)์„ ๊ณ ๋ คํ•œ๋‹ค. B2B ์‚ฌ์—…์—์„œ ๊ณ ๊ฐ์€ ๊ธฐ์—… ๊ณ ๊ฐ์œผ๋กœ ์ผ๋ฐ˜ ์†Œ๋น„์ž์™€๋Š” ๊ทธ ํŠน์„ฑ์ด๋‚˜ ๋‹ˆ์ฆˆ๊ฐ€ ๋งค์šฐ ๋งŽ์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ณ ๊ฐ ์„ธ๋ถ„ํ™”๋Š” ๊ธฐ์—…์˜ ์ „๋žต์— ๋”ฐ๋ผ ๋งค์šฐ ๋‹ค์–‘ํ•˜๊ฒŒ ์‹œ๋„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ „ํ†ต์ ์ด๋ฉด์„œ๋„ ๊ฐ€์žฅ ์ผ์ฐจ์›์ ์ธ ์ ‘๊ทผ์€ ์‚ฐ์—… ๋ถ„๋ฅ˜๋ฅผ ๊ณ ๋ คํ•œ ๊ฒƒ์ด๋‹ค. ์‚ฐ์—…์˜ ๋ถ„๋ฅ˜ ๊ธฐ์ค€์— ๋”ฐ๋ผ ๊ณ ๊ฐ๋„ ์ƒ์‚ฐ์—…์ž(Producer), ์žฌํŒ๋งค์—…์ž(Distributor or Wholesaler), ์ •๋ถ€/์œ ๊ด€ ์กฐ์ง(Governmentor NGO)์˜ 3๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. Figure III-24. ์ „ํ†ต์ ์ธ ๊ณ ๊ฐ ๋ถ„๋ฅ˜ ๋ฐฉ๋ฒ• ์ฒซ ๋ฒˆ์งธ ์ƒ์‚ฐ์—…์ž๋Š” ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ์ƒ์‚ฐํ•˜๋Š” ๊ธฐ์—…๊ตฐ์œผ๋กœ ๋†์—…, ์ˆ˜์‚ฐ์—…, ์ž„์—…๊ณผ ๊ฐ™์€ 1์ฐจ ์‚ฐ์—… ๊ธฐ์—…๊ตฐ์„ ๋งํ•˜๋ฉฐ, ์ผ๋ฐ˜ ์†Œ๋น„์ž๋‚˜ ๊ธฐ์—…๋“ค์„ ๋Œ€์ƒ์œผ๋กœ ๋‹ค์–‘ํ•œ ์ œํ’ˆ์„ ์ œ์กฐ/์ƒ์‚ฐํ•˜๋Š” 2์ฐจ ์‚ฐ์—… ๊ธฐ์—…๊ตฐ, ๊ทธ๋ฆฌ๊ณ  ๊ธˆ์œต, ๊ตํ†ต, ์˜๋ฃŒ, ์š”์‹, ์ˆ™๋ฐ•, ๋ ˆ์ €, ์—”ํ„ฐํ…Œ์ธ๋จผํŠธ ๋“ฑ๊ณผ ๊ฐ™์€ ์„œ๋น„์Šค ์‚ฐ์—… ์ค‘์‹ฌ์˜ 3์ฐจ ์‚ฐ์—… ๊ธฐ์—…๊ตฐ์œผ๋กœ ๊ตฌ๋ถ„๋  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๋ฒˆ์งธ, ์žฌํŒ๋งค์—…์ž๋Š” 1/2/3์ฐจ ์‚ฐ์—…์— ์†ํ•œ ๊ธฐ์—…๋“ค์ด ์ƒ์‚ฐํ•œ ์žฌํ™”์™€ ์šฉ์—ญ์„ ์œ ํ†ตํ•˜๋Š” ๋„๋งค์ƒ๊ณผ ๋ถ„๋ฐฐ ์—…์ฒด, ์†Œ๋งค์ƒ ๋“ฑ์ด ์ด์— ํ•ด๋‹นํ•˜๋ฉฐ, ์„ธ ๋ฒˆ์งธ ์ •๋ถ€/์œ ๊ด€ ์กฐ์ง์€ ์—ฐ๋ฐฉ, ์ฃผ, ์‹œ ๋“ฑ์„ ํฌํ•จํ•œ ์ •๋ถ€ ๊ธฐ๊ด€๊ณผ ๊ต์œก ๊ธฐ๊ด€, ์ž์„ ๋‹จ์ฒด, ๊ณต์ต์„ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” ๋น„์˜๋ฆฌ ๋‹จ์ฒด ๋“ฑ์ด ํ•ด๋‹นํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‚ฐ์—… ๊ตฌ๋ถ„์— ์˜ํ•œ ๋ถ„๋ฅ˜ ๋ฐฉ๋ฒ•์€ ์ „๋žต์  ์‹œ์‚ฌ์ ์„ ์–ป๊ธฐ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ์ง€๋ฆฌ์  ์œ„์น˜(Geography), ์ฑ„๋„(Channel), ๊ธฐ์—… ํ†ต๊ณ„(Filmography) ๋“ฑ์„ ํ™œ์šฉํ•˜์—ฌ ์ •๋ณด์˜ ์งˆ์  ํ–ฅ์ƒ์„ ๋„๋ชจํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์ข€ ๋” ํ’๋ถ€ํ•œ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒ์„ธํ•œ ๊ณ ๊ฐ ํ”„๋กœํŒŒ์ผ๋ง(Customer Profiling)์„ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์‚ฌ์‹ค(Fact) ์ „๋‹ฌ ์ˆ˜์ค€์— ๊ทธ์น˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋Š๋‚Œ์ด ๋“ ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด ๋ณผ ๊ฒƒ์„ ๊ถŒ๊ณ ํ•œ๋‹ค. B2B ๊ณ ๊ฐ ๋ถ„๋ฅ˜ ์‹œ์—๋Š” ๊ฒฝ์ œ์  ๊ด€์ ์—์„œ ํฌ์ฐฉ๋œ ์‚ฌ์—… ๊ธฐํšŒ ๋ฐ ์‹ค์  ๋“ฑ์„ ๊ณ ๋ คํ•˜์—ฌ ๊ณ ๊ฐ์„ ๋ถ„๋ฅ˜ํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•˜๋ฉฐ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ โ€˜Value-Based Segmentation(VBS)โ€™๋ผ๊ณ  ํ•œ๋‹ค. VBS๋Š” ๊ณ ๊ฐ ๊ณ„์ธตํ™” ๊ด€๋ฆฌ(Tier Management), ํŒŒ๋ ˆํ†  ๋ฒ•์น™ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋ฒ•๋“ค์„ ์ ์šฉํ•˜์—ฌ ๊ณ ๊ฐ๊ตฐ์„ ๋ฐœ๊ตดํ•  ์ˆ˜ ์žˆ๋‹ค. Figure III-25. Value Based Segmentation ์‚ฌ๋ก€ ํŠนํžˆ, ๊ณ ๊ฐ ์ถฉ์„ฑ๋„(Customer Loyalty)์™€ ๊ณ ๊ฐ ๊ด€๋ฆฌ ๋น„์šฉ(Retention Cost)์˜ ๊ด€์ ์—์„œ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ๋งŒ๋“ค๋ฉด ํ•ต์‹ฌ ๊ณ ๊ฐ์€ ๊ณ ๊ฐ ์ถฉ์„ฑ๋„๊ฐ€ ๋†’์•„ ์žฌ๊ตฌ๋งค์œจ์ด ๋†’๊ณ  ๊ณ ๊ฐ ๊ด€๋ฆฌ๋น„ ์šฉ๋„ ์ ๊ฒŒ ๋“œ๋Š” ๊ณ ๊ฐ, ํŒŒํŠธ๋„ˆ ๊ณ ๊ฐ์€ ๊ณ ๊ฐ ์ถฉ์„ฑ๋„๋Š” ๋†’์ง€๋งŒ ๊ณ ๊ฐ ๊ด€๋ฆฌ ๋น„์šฉ์ด ๋งŽ์ด ๋“œ๋Š” ๊ณ ๊ฐ ๋“ฑ์œผ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‹ค์ œ ํŒŒํŠธ๋„ˆ ๊ณ ๊ฐ์„ ์ž˜ ๋Œ€์‘ํ•˜์—ฌ ๊ณ ๊ฐ ๊ด€๋ฆฌ ๋น„์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์‚ฌ์—…์„ฑ๊ณผ ์ฐฝ์ถœ ์ธก๋ฉด์—์„œ ๋งค์šฐ ์œ ์ตํ•˜๋ฉฐ, ๋ฒ”์šฉ์ƒํ’ˆ ๊ณ ๊ฐ์—๊ฒŒ๋Š” ๋ฒ”์šฉํ’ˆ(Commodity)์ด์ƒ์˜ ๊ฐ€์น˜๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๊ณ ๊ฐ์„ ํ•ต์‹ฌ ๊ณ ๊ฐ์œผ๋กœ ์ „ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์•„์šธ๋Ÿฌ ์ € ์„ฑ๊ณผ ๊ณ ๊ฐ์€ ์ ์ง„์ ์œผ๋กœ ๊ฑฐ๋ž˜๋ฅผ ์ค„์—ฌ๋‚˜๊ฐ€๋Š” ๊ฒƒ์ด ์žฅ๊ธฐ์  ๊ด€์ ์—์„œ ๊ธฐ์—… ๊ฒฝ์˜์— ๋„์›€์ด ๋œ๋‹ค. ๊ณ ๊ฐ ์ถฉ์„ฑ๋„์™€ ๊ณ ๊ฐ ๊ด€๋ฆฌ ๋น„์šฉ, ์ œํ’ˆ ๊ตฌ๋งค์•ก์„ ์ง€๋ฆ„์œผ๋กœ ํ•˜๋Š” ์›์„ ๊ฐ€์ง€๊ณ  ๋ฒ„๋ธ” ์ฐจํŠธ๋กœ ๊ทธ๋ ค๋ณด๋ฉด Figure III-25์™€ ๊ฐ™์ด ๊ทธ๋ ค์งˆ ์ˆ˜ ์žˆ๋‹ค. ์ƒ์œ„ Tier1, Tier 2์— ์†ํ•œ ๊ณ ๊ฐ๋“ค ์ฆ‰, ํ•ต์‹ฌ ๊ณ ๊ฐ ๊ตฐ(Key Customer Segment)์€ ์†Œ์ˆ˜์ด๋‚˜ ๊ธฐ์—…์˜ ์ œํ’ˆ ํŒ๋งค์•ก ์ค‘ ์ƒ๋‹น ๋น„์ค‘์„ ์ฐจ์ง€ํ•˜๋Š” ๊ณ ๊ฐ๋“ค์„ ๋œปํ•˜๋ฉฐ ๊ณผ๊ฑฐ์™€ ํ˜„์žฌ์˜ ๊ณ ๊ฐ์ธ ๊ธฐ์กด ๊ณ ๊ฐ ๊ตฐ์— ๋น„ํ•ด ์ž ์žฌ ๊ณ ๊ฐ ๊ตฐ์€ ์‹ ๊ทœ ๋ฐ ๋ฏธ๋ž˜์˜ ๊ณ ๊ฐ๋“ค๋กœ์„œ ๋งˆ์ผ€ํŒ…/์˜์—… ํ™œ๋™์„ ๋ณด๋‹ค ์ง‘์ค‘ํ•ด์•ผ ํ•  ํ•„์š”๋„ ์žˆ๋‹ค. ๊ณ ๊ฐ ์„ธ๋ถ„ํ™”์— ๋”ฐ๋ฅธ ๋งˆ์ผ€ํŒ…์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๋ฉด ๋ฒ”์šฉ์ƒํ’ˆ ๊ณ ๊ฐ์—๊ฒŒ ์–ด๋–ค ๋งˆ์ผ€ํŒ… ํ™œ๋™์„ ํ•  ๊ฒƒ์ธ์ง€? ์˜์—… ์ธก๋ฉด์—์„œ๋Š” ์–ด๋–ค ์ œ์•ˆ์„ ํ†ตํ•ด ํ•ต์‹ฌ ๊ณ ๊ฐ์œผ๋กœ ์ „ํ™˜ํ•  ๊ฒƒ์ธ์ง€? ํŒŒํŠธ๋„ˆ ๊ณ ๊ฐ์—๊ฒŒ ์†Œ์š”๋˜๋Š” ๊ณ ๊ฐ ๊ด€๋ฆฌ ๋น„์šฉ์€ ์–ด๋–ป๊ฒŒ ๊ฐ์†Œ์‹œํ‚ฌ ๊ฒƒ์ธ์ง€? ๋˜ํ•œ ๊ตฌ๋งค์•ก ๋˜๋Š” ๊ฑฐ๋ž˜๊ธˆ์•ก์€ ์–ด๋–ป๊ฒŒ ์ฆ๊ฐ€์‹œ์ผœ ๋ฒ„๋ธ”์˜ ํฌ๊ธฐ๋ฅผ ํ‚ค์šธ ๊ฒƒ์ธ์ง€ ๋“ฑ ๋งˆ์ผ€ํŒ…, ์˜์—…, ๋‚ด๋ถ€ ํ˜์‹ , ์‚ฌ์—…์ „๋žต ๊ด€์ ์˜ ๊ณผ์ œ ์ˆ˜๋ฆฝ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋œ๋‹ค. Table III-17์€ ์„ธ๋ถ„ํ™”์˜ ์žฅ๋‹จ์ ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. Table III-17. ์„ธ๋ถ„ํ™” ๋ถ„์„์˜ ์žฅ๋‹จ์  ์‹œ์žฅ์„ธ๋ถ„ํ™” ๋˜๋Š” ๊ณ ๊ฐ ์„ธ๋ถ„ํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ๋‹ค์–‘ํ•œ ์ƒํ™œ ์Šคํƒ€์ผ(Life Style)์„ ์—ฐ๊ตฌํ•˜๋Š” ๋งˆ์ผ€ํŒ… ๊ด€๋ จ ์ข…์‚ฌ์ž๋“ค์€ ๊ทธ๋•Œ๊ทธ๋•Œ ์‹œ๋Œ€์ƒ์„ ๋ฐ˜์˜ํ•œ ์žฌ๋ฏธ๋‚œ ๋ฒ„์ฆˆ(Buzzword)๋“ค์„ ๋งŽ์ด ๋งŒ๋“ค์–ด๋‚ด๊ธฐ๋„ ํ•œ๋‹ค. ๋‹ค์Œ์€ ๊ทธ๋Ÿฐ ์ฐจ์›์—์„œ ๋„๋ฆฌ ์•Œ๋ ค์ง„ ์šฉ์–ด๋“ค์ด๋‹ค. - DINK: Double Income No Kids - GLAM: Greying, Leisured And Moneyed - GUPPY: Gay, Upwardly Mobile, Prosperous, Professional - YUPPIE: Young Urban Prosperous Professional - SITKOM: Single Income, Two Kids, Oppressive Mortgage - WASP: White Anglo-Saxon Protestant. 8.2 ๊ตฐ์ง‘๋ถ„์„(Cluster Analysis) ํ•œ์ž์„ฑ์–ด์— ์œ ์œ ์ƒ์ข…(็›ธๅพž)์ด๋ผ๋Š” ๋ง์ด ์žˆ๋‹ค. ๋น„์Šทํ•œ ๋ถ€๋ฅ˜์˜ ์‚ฌ๋žŒ๋“ค์ด ์–ด์šธ๋ฆผ์„ ๋œปํ•˜๋Š” ๋ง์ธ๋ฐ ๊ตฐ์ง‘๋ถ„์„(Cluster Analysis)์€ ๊ฐœ์ธ ๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์ฒด ์ค‘์—์„œ ์œ ์‚ฌํ•œ ์†์„ฑ์„ ์ง€๋‹Œ ๋Œ€์ƒ์„ ๋ช‡ ๊ฐœ์˜ ์ง‘๋‹จ์œผ๋กœ ๊ทธ๋ฃนํ™”ํ•œ ๋‹ค์Œ, ๊ฐ ์ง‘๋‹จ์˜ ์„ฑ๊ฒฉ์„ ํŒŒ์•…ํ•จ์œผ๋กœ์จ ๋ฐ์ดํ„ฐ ์ „์ฒด ๊ตฌ์กฐ์˜ ์ดํ•ด์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ์œ ์‚ฌํ•œ ๊ฒƒ๋ผ๋ฆฌ ์ฐจ๋ก€๋Œ€๋กœ ํ•ฉ์ณ ๋‚˜๊ฐ€๋Š” ๊ฒƒ์„ ๋งํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ์œ ์‚ฌํ•œ ์ •๋„(similarity)๋ฅผ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•ด ๊ฑฐ๋ฆฌ๋‚˜ ์ƒ๊ด€๊ณ„์ˆ˜ ๋“ฑ ํ†ต๊ณ„์  ๊ธฐ๋ฒ•์ด<NAME>๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์นด๋“œ์‚ฌ์—์„œ VIP ๊ณ ๊ฐ๋“ค์„ ๊ตฐ์ง‘ํ™”ํ•˜์—ฌ ์ผ๋ฐ˜ ๊ณ ๊ฐ๊ตฐ๊ณผ ์–ด๋–ค ์ฐจ์ด์ ์ด ์žˆ๋Š”์ง€ ํŒŒ์•…ํ•˜์—ฌ ์ผ๋ฐ˜ ๊ณ ๊ฐ๋“ค ์ค‘ VIP ๊ณ ๊ฐ๋“ค์˜ ํŠน์„ฑ์„ ๋ค ๊ณ ๊ฐ(๊ตฐ)์„ ๋Œ€์ƒ์œผ๋กœ ๋งˆ์ผ€ํŒ… ์บ ํŽ˜์ธ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ตฐ์ง‘๋ถ„์„์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด 2๊ฐ€์ง€ ๋ฐฉ์•ˆ์ด ์žˆ๋‹ค. ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„ ๋น„๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„ ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„์€ N ๊ฐœ์˜ ๊ตฐ์ง‘์„ ๊ฐ€์ง€๊ณ  ์‹œ์ž‘ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ํ•˜๋‚˜๊ฐ€ ๋‚จ์„ ๋•Œ๊นŒ์ง€ ์œ ์‚ฌํ•œ ๊ฒƒ๋ผ๋ฆฌ ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ•(Bottom-up ๋ฐฉ์‹. ๋ณ‘ํ•ฉ๋ฐฉ๋ฒ•)๊ณผ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•œ ํ•˜๋‚˜์˜ ๊ตฐ์ง‘์—์„œ N ๊ฐœ์˜ ๊ตฐ์ง‘์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๋ฐฉ๋ฒ•(Top-Down ๋ฐฉ์‹, ๋ถ„ํ•  ๋ฐฉ๋ฒ•)์ด ์žˆ๋‹ค. ๋น„๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„์€ ์š”์ธ๋ถ„์„(factor analysis)์„ ํ†ตํ•ด ๋ฏธ๋ฆฌ ๊ตฐ์ง‘์„ ์˜ˆ์ƒํ•˜์—ฌ ํ•ฉ์ณ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. K-means ๋ถ„์„ ๊ฐ™์€ ๊ฒƒ์ด ๋Œ€ํ‘œ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ตฐ์ง‘๋ถ„์„์€ ๊ณ ๊ฐ ์„ธ๋ถ„ํ™” ๋˜๋Š” ์‹œ์žฅ ์„ธ๋ถ„ํ™”๋ฅผ ํฌํ•จํ•œ ๋‹ค์–‘ํ•œ ๋งˆ์ผ€ํŒ… ๋ฐ ์˜์—… ์ „๋žต์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ์ด๋ฅผ ์ด์šฉํ•ด ํ†ต๊ณ„์  ์˜์‚ฌ๊ฒฐ์ •์„ ํ•  ๋•Œ ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ๋Š” ๊ตฐ์ง‘ ์ˆ˜(number of clusters)๋ฅผ ๋ช‡ ๊ฐœ๋กœ ํ•  ๊ฒƒ์ธ๊ฐ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ํ•ญ์ƒ ์ด์Šˆ์ด๋‹ค. ํ•™์ž๋“ค์˜ ๋‹ค์–‘ํ•œ ์˜๊ฒฌ์ด ์žˆ์ง€๋งŒ ์•„์ง ๊ฒฐ๋ก ์ด ๋‚˜์ง€ ์•Š์•˜์œผ๋ฉฐ ๋ถ„์„์ž์˜ ์ฃผ๊ด€์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค. ๊ทธ ๋ง์€ ๋ชจ๋“  ํ†ต๊ณ„๊ฐ€ ๊ทธ๋ ‡๋“ฏ์ด ํ†ต๊ณ„์˜ ์œ ์˜๋ฏธ์„ฑ์„ ํ™•๋ณดํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋ฉฐ ๋น„์šฉ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด ๊ณ ๋ ค๋˜์–ด์•ผ ํ•œ๋‹ค. Table III-18์€ ๊ตฐ์ง‘๋ถ„์„์˜ ์žฅ๋‹จ์ ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. Table III-18. ๊ตฐ์ง‘๋ถ„์„์˜ ์žฅ๋‹จ์  ๊ตฐ์ง‘๋ถ„์„์€ ํ†ต๊ณ„ ์†Œํ”„ํŠธ์›จ์–ด์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์„ ํฌํ•จํ•˜์—ฌ ํ†ต๊ณ„์  ์ดํ•ด๊ฐ€ ๊ธฐ๋ฐ˜์ด ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ์ด ์žฅ์—์„œ๋Š” ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…๋งŒ ์†Œ๊ฐœํ•˜์˜€๋‹ค. ํ†ต๊ณ„ ์†Œํ”„ํŠธ์›จ์–ด๋Š” SPSS, SAS์™€ ๊ฐ™์€ ์ „๋ฌธ์ ์ธ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ์ž…ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์ง€๋งŒ ์ฃผ๋จธ๋‹ˆ ์‚ฌ์ •์ด ๋„‰๋„‰ํ•˜์ง€ ๋ชปํ•˜๋‹ค๋ฉด ์–ด๋‘ ์˜ ๊ฒฝ๋กœ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ถˆ๋ฒ• ์†Œํ”„ํŠธ์›จ์–ด์— ํƒ๋‹‰ํ•˜์ง€ ๋ง๊ณ  ๊ณต๊ฐœ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ์ด์šฉํ•ด ๋ณด์ž. ๊ทธ๋Ÿฐ ์ธก๋ฉด์—์„œ 'R'์€ ๋งค์šฐ ํ›Œ๋ฅญํ•œ ๋„๊ตฌ์ด๋‹ค. ์ตœ๊ทผ ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„์ด ๋œจ๋ฉด์„œ ๋Œ€์ค‘์—๊ฒŒ ๊ณต๊ฐœ๋˜๊ณ  ์žˆ๋Š” ๊ณต๊ณต ๋ฐ์ดํ„ฐ์˜ ๋ถ„์„๋„ ์ด๋Ÿฐ R์„ ํ†ตํ•ด ์‰ฝ๊ฒŒ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์œˆ๋„, macOS, ๋ฆฌ๋ˆ…์Šค ๋“ฑ ๋‹ค์–‘ํ•œ ํ”Œ๋žซํผ์—์„œ ๋™์ž‘ ๊ฐ€๋Šฅํ•˜๊ณ  R ์ž์ฒด๊ฐ€ ์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ R-Studio๋ฅผ ํ†ตํ•ด์„œ ์›ํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์—ฌ Presentation ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ด€์‹ฌ ์žˆ๋Š” ๋ถ„๋“ค์€ ์•„๋ž˜ ์‚ฌ์ดํŠธ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ผ. R ํ”„๋กœ์ ํŠธ : ๊ณต๊ฐœ์†Œํ”„ํŠธ์›จ์–ด์ธ R์„ ์†Œ๊ฐœํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค. https://www.r-project.org/ R Studio : R์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด ์†Œ์œ„ ๋งํ•˜๋Š” IDE(ํ†ตํ•ฉ๊ฐœ๋ฐœ ํ™˜๊ฒฝ)์„ ์ œ๊ณตํ•œ๋‹ค https://www.rstudio.com/ *์ฐธ๊ณ ๋กœ macOS๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ์€ Xcode๋„ ๊ฐ™์ด ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ IDE ํ™˜๊ฒฝ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋“ฏ [1] STP: Segmentation, Targeting, Positioning [2] B2B ์‚ฌ์—…์€ ๊ฑฐ๋ž˜๊ฐ€ ์ž์ฃผ ๋ฐœ์ƒํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ๊ทœ๋ชจ๋„ ํฌ๋ฉฐ, ๊ฑฐ๋ž˜๊ฐ€ ํ•œ๋ฒˆ ๋ฐœ์ƒํ•˜๋ฉด ์ง€์†๋˜๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋‹ค. 08. ๊ณ ๊ฐ ์š”๊ตฌ ๋ถ„์„(2/3) 8.3 ๊ณ ๊ฐ ๊ฒฝํ—˜ ๋ถ„์„ 2000๋…„ ์ดˆ๋ฐ˜ CRM ๋งˆ์ผ€ํŒ…์ด ํ•œ์ฐธ ์ด์•ผ๊ธฐ๋  ๋•Œ ๊ณ ๊ฐ ๊ด€๊ณ„(Customer Relationship)๋ผ๋Š” ์šฉ์–ด๊ฐ€ ๋งค์šฐ ์ค‘์š”ํ–ˆ๋‹ค. ๊ณ ๊ฐ ๊ฒฝํ—˜(Customer eXperience: CX)์€ ๊ณ ๊ฐ ๊ด€๊ณ„ ๋‹ค์Œ์˜ ๋‹จ๊ณ„๋กœ์„œ ์˜จ๋ผ์ธ์—์„œ๋Š” ์‚ฌ์šฉ์ž ๊ฒฝํ—˜(UX:User eXperience)์œผ๋กœ, ์˜คํ”„๋ผ์ธ์—์„œ๋Š” ๊ณ ๊ฐ์˜ ์ผ์ƒ์„ ๋‘๊ณ  ๋‹ค์–‘ํ•œ ๊ณ ๊ฐ ๊ฒฝํ—˜ ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์ด ์ตœ๊ทผ CX ํŠธ๋ Œ๋“œ์ด๋‹ค. ๊ทธ๋Ÿฐ ์ฐจ์›์—์„œ ๊ณ ๊ฐ์˜ ํ”ผ๋“œ๋ฐฑ์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ˆ˜์ง‘ํ•˜๊ณ ์ž ํ•˜๋Š” ๋‹ˆ์ฆˆ๊ฐ€ ์ปค์ ธ๊ฐ€๊ณ  ์žˆ๊ณ , IT ์„ค๋ฃจ์…˜ ์‚ฌ์—…์ž๋“ค์€ ๊ด€๋ จ๋œ ๋Œ€์‹œ๋ณด๋“œ(Dashboard)์˜ ๊ตฌ์ถ• ๊ฐ™์€ ๊ฒƒ์„ ์ฃผ์š” ์‚ฌ์—… ๊ธฐํšŒ๋กœ ๋ณด๊ณ  ์žˆ๋‹ค. ๊ณ ๊ฐ ๊ฒฝํ—˜ ๋ถ„์„์˜ ๊ด€๊ฑด์€ ์†Œ๋น„์ž๋‚˜ ๊ณ ๊ฐ[1]์ด ์–ด๋–ป๊ฒŒ ๊ธฐ์—…์˜ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ์ธ์žํ•˜๊ฒŒ ๋˜์—ˆ์œผ๋ฉฐ, ์–ด๋–ค ๊ณ„๊ธฐ๋กœ ๊ทธ๊ฒƒ์„ ๊ตฌ๋งค๋ฅผ ๊ฒฐ์ •ํ•˜๊ฒŒ ๋˜์—ˆ์œผ๋ฉฐ, ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๊ตฌ๋งคํ•˜๊ฒŒ ๋˜์—ˆ๋Š”์ง€, ๊ตฌ๋งค ์ดํ›„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋ฌด์—‡์ด ์ข‹์•˜๊ณ  ๋ฌด์—‡์ด ๋ถˆ๋งŒ์ด์—ˆ๋Š”์ง€, ํ–ฅํ›„ ์žฌ๊ตฌ๋งคํ•œ๋‹ค๋ฉด ์–ด๋–ค ๊ธฐ๋Šฅ์ด ๋ณด์™„๋œ ๊ฒƒ์„ ๊ตฌ๋งคํ• ์ง€ ๋“ฑ ์ œํ’ˆ์˜ ์ธ์ง€๋ถ€ํ„ฐ ์„œ๋น„์Šค ์ข…๋ฃŒ์— ์ด๋ฅด๋Š” ์ „ ๊ณผ์ •์—์„œ ์†Œ๋น„์ž๋‚˜ ๊ณ ๊ฐ์˜ ์ƒ๊ฐ์„ ์ฝ๊ณ  ํŒŒ์•…ํ•˜์—ฌ ๊ทธ์— ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๋Š” ๋˜๋Š” ๊ทธ๊ฒƒ๋“ค์„ ์‚ฌ์ „์— ์ถฉ์กฑ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ ์ฐจ์›์—์„œ ๊ณ ๊ฐ๊ณผ์˜ ๋ชจ๋“  ์ ‘์ ์„ ๊ฐ€์žฅ ์ค‘์‹œํ•ด์•ผ ํ•˜๋ฉฐ ๊ทธ ์ ‘์ ์—์„œ ๊ณ ๊ฐ์˜ ํ”ผ๋“œ๋ฐฑ์„ ์ ์‹œ์— ํŒŒ์•…ํ•˜์—ฌ ์ด๋ฅผ ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค์— ๋ฐ˜์˜ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค. Figure III-26. ๊ณ ๊ฐ ๊ฒฝํ—˜์˜ ํ๋ฆ„ Figure III-26์€ ๊ณ ๊ฐ์ด ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค๋ฅผ ๊ตฌ๋งคํ•˜๋Š” ๊ณผ์ •์„ ๋†“๊ณ  ๊ฐ ๋‹จ๊ณ„๋ณ„๋กœ ๊ณ ๊ฐ ๊ฒฝํ—˜์ด ์–ด๋–ป๊ฒŒ ์ „๋‹ฌ๋˜๋Š”์ง€๋ฅผ ๋„์‹ํ™”ํ•œ ๊ฒƒ์ด๋‹ค. ๊ฒฐ๊ตญ์€ ๊ณ ๊ฐ ๊ฒฝํ—˜์˜ ๊ฐ ๋‹จ๊ณ„์™€ ๊ธฐ์—… ๋‚ด Value Chain์ด ์ƒํ˜ธ์ž‘์šฉํ•˜๋ฉด์„œ ๊ณ ๊ฐ ๊ฒฝํ—˜์— ๋Œ€์ฒ˜ํ•˜๊ฒŒ ๋œ๋‹ค. ์ปจ์„คํŒ… ๊ด€์ ์—์„œ ์ด๋Ÿฐ CX๋ฅผ ํŒŒ์•…ํ•˜๊ณ  ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์งˆ๋ฌธ๋“ค์— ๋Œ€ํ•ด ๋‹ตํ•ด๋ณด์•„์•ผ ํ•œ๋‹ค. ์งˆ๋ฌธ์˜ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋‹ค๋ถ„ํžˆ ๊ณ ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๊ธฐ ์œ„ํ•œ ๊ด€์ ์ด๋‹ค. ๊ณ ๊ฐ ๊ฒฝํ—˜์„ ์–ด๋–ป๊ฒŒ ์ธก์ •ํ•  ๊ฒƒ์ธ์ง€, ํ”„๋กœ์„ธ์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•  ๊ฒƒ์ธ์ง€, ์–ด๋–ป๊ฒŒ ๋ถ„์„ํ•  ๊ฒƒ์ธ์ง€, ๋‚˜์•„๊ฐ€ ๊ฒฝ์˜์— ์–ด๋–ป๊ฒŒ ๋ฐ˜์˜ํ•ด์•ผ ํ•  ๊ฒƒ์ธ์ง€ ๊ถ๊ทน์ ์œผ๋กœ ์ด๊ฒƒ์„ ํ”Œ๋žซํผ์œผ๋กœ ๋งŒ๋“ค๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด ๊ณ ๊ฐ ๊ฒฝํ—˜ ๊ด€์ ์—์„œ ์ค‘์š”ํ•œ ํฌ์ธํŠธ์ด๋‹ค. ๊ทธ๋Ÿฐ ์ฐจ์›์—์„œ ๊ณ ๊ฐ ๊ฒฝํ—˜ ๋ถ„์„์„ ์œ„ํ•œ ์งˆ๋ฌธ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค. ๊ธฐ์—…๊ฐ€์น˜ ์ธก๋ฉด์—์„œ ๊ณ ๊ฐ ๊ฒฝํ—˜์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€? ์–ด๋–ค ๊ณ ๊ฐ ๊ฒฝํ—˜์ด ๊ธฐ์—… ์„ฑ๊ณผ์— ๊ธฐ์—ฌํ•˜๋Š”๊ฐ€? ๊ณ ๊ฐ ๊ฒฝํ—˜์„ ์–ด๋–ป๊ฒŒ ์ธก์ •ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€? ์–ด๋–ป๊ฒŒ ๊ณ ๊ฐ ๊ฒฝํ—˜์„ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€? ์˜ˆ. ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„ ๋“ฑ ๊ณ ๊ฐ ๊ฒฝํ—˜์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ IT ํ”Œ๋žซํผ์€ ์–ด๋–ค ๋ชจ์Šต์ธ๊ฐ€? ์ง€์†์ ์ด๊ณ  ๋‹ค์–‘ํ•œ ๊ณ ๊ฐ ๊ฒฝํ—˜์˜ ์ œ๊ณต์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ผ๊ด€๋œ ๊ฒฝํ—˜์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ์ „๋žต์€ ๋ฌด์—‡์ธ๊ฐ€? ๊ธฐ์—…์˜ ํ”„๋กœ์„ธ์Šค ๋˜๋Š” ์กฐ์ง๋ฌธํ™”์—์„œ ์ผ๊ด€๋œ ๊ณ ๊ฐ ๊ฒฝํ—˜์˜ ํ”ผ๋“œ๋ฐฑ์€ ์–ด๋–ป๊ฒŒ ๋ฐ˜์˜๋  ์ˆ˜ ์žˆ๋Š”๊ฐ€? ๊ณ ๊ฐ ๊ฒฝํ—˜ ์ง€ํ–ฅ์ ์ธ ๊ธฐ์—…์˜ ์กฐ์ง๋ฌธํ™”๋Š” ์–ด๋–ค ๋ชจ์Šต์ธ๊ฐ€? ์ตœ๊ทผ ๊ฑฐ์˜ ๋ชจ๋“  ๊ธฐ์—…๋“ค์˜ ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค๋Š” O2O[2]์˜ ํ˜•ํƒœ๋ฅผ ์ข…์ฐฉ์ง€๋กœ ํ”Œ๋žซํผ(Platform) ์„œ๋น„์Šค๋ฅผ ๊ฐ–์ถ”๊ณ ์ž ์ด๋ ฅ์„ ํŽผ์น˜๋Š” ํ˜•๊ตญ์ด๋‹ค. ์ด๋ฏธ ์†Œ๋น„์ž๋“ค์ด ์˜จ๋ผ์ธ๊ณผ ์˜คํ”„๋ผ์ธ์„ ๋„˜๋‚˜๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์˜จ๋ผ์ธ๊ณผ ์˜คํ”„๋ผ์ธ์˜ ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์„ ๋„˜๋‚˜๋“œ๋Š” ๊ณ ๊ฐ์˜ ํ”ผ๋“œ๋ฐฑ์„ ํŒŒ์•…ํ•˜๊ณ  ๋งค ์ˆœ๊ฐ„ ๊ณ ๊ฐ ๊ฒฝํ—˜์„ ์ œ๊ณตํ•ด์•ผ ํ•˜๋Š” ๊ธฐ์—…๋“ค ์ž…์žฅ์—์„œ๋Š” ๋งˆ์ผ€ํŒ… ์ „๋žต์˜ ๊ทผ๋ณธ๋ถ€ํ„ฐ ๊ณ ๋ฏผ์ด ๋งŽ์„ ์ˆ˜๋ฐ–์— ์—†๋‹ค. ๊ณ ๊ฐ์„ ๋‹จ์ˆœํžˆ ๋”ฐ๋ผ๊ฐ€๊ธฐ๋ณด๋‹ค๋Š” ๊ณ ๊ฐ ๊ฒฝํ—˜ ๋ถ„์„์„ ํ†ตํ•ด ๊ณ ๊ฐ์„ ์„ ๋„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์•ˆ์„ ์ฐพ๋Š” ๊ฒƒ์ด ๊ด€๊ฑด์ด๋ผ ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค. Table III-19๋Š” ๊ณ ๊ฐ ๊ฒฝํ—˜ ๋ถ„์„์˜ ์žฅ๋‹จ์ ์„ ์ •๋ฆฌํ•ด ๋ณธ ๊ฒƒ์ด๋‹ค. Table III-19. ๊ณ ๊ฐ ๊ฒฝํ—˜ ๋ถ„์„์˜ ์žฅ๋‹จ์  Break #14. NPS์™€ ๊ณ ๊ฐ ์ถฉ์„ฑ๋„ ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๊ณ ๊ฐ ๊ธฐ์—…์˜ ์ œํ’ˆ ๋ฐ ์„œ๋น„์Šค์˜ ์‹œ์žฅ ๋ฐ˜์‘์„ ์‚ดํŽด๋ณด๊ธฐ ์œ„ํ•ด ๊ณ ๊ฐ๋งŒ์กฑ๋„(CSAT. Customer SATisfaction scores)๋‚˜ ๊ณ ๊ฐ ์ถฉ์„ฑ๋„(Customer Loyalty)๋ฅผ ์‚ดํŽด๋ณด๊ฒŒ ๋œ๋‹ค. ๊ณ ๊ฐ๋งŒ์กฑ๋„๋Š” ๊ณ ๊ฐ ๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•ด์„œ ๋Œ€๋ถ€๋ถ„ ์ข‹๊ฒŒ ํ‰๊ฐ€ํ•˜๋Š” ๊นŒ๋‹ญ์— ๊ณ ๊ฐ๋งŒ์กฑ๋„๊ฐ€ ๋†’๋‹ค๊ณ  ํ•ด์„œ ์‹ค์ œ ์žฌ๊ตฌ๋งค๋‚˜ ์žฌ๊ณ„์•ฝ์ด ๋ฐœ์ƒํ•˜๋Š” ํ™•๋ฅ ์€ ๋‚ฎ๋‹ค[3]. ์ด๋Ÿฐ ๋ถ€๋ถ„์„ ๊ฐ์•ˆํ•˜์—ฌ ์‹ค์งˆ์ ์ธ ์žฌ๊ตฌ๋งค ์˜์‚ฌ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด ์ตœ๊ทผ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…๋“ค ์‚ฌ์ด์—์„œ ๋น ๋ฅด๊ฒŒ ํ™•์‚ฐ๋˜๋Š” ๊ธฐ๋ฒ•์œผ๋กœ โ€˜NPSโ€™๋ผ๋Š” ๊ฒƒ์ด ์žˆ๋‹ค. Figure III-27. NPS ํ‰๊ฐ€์˜ ๊ฐœ๋… NPS๋ž€, Net Promoter Score์˜ ์•ฝ์ž๋กœ 2003๋…„์— Bain & Company๊ฐ€ ์†Œ๊ฐœํ•˜์—ฌ ๋ถ๋ฏธ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๋„๋ฆฌ ํ™œ์šฉ๋˜๊ณ  ์žˆ๋‹ค[4]. NPS๋Š” ์ƒ๊ฐ๋ณด๋‹ค ์ƒ๋‹นํžˆ ์‰ฝ๊ฒŒ ๊ณ„์‚ฐ๋˜๋Š”๋ฐ ๋‹ค์Œ ์งˆ๋ฌธ์— ๋Œ€ํ•ด 10์  ์ฒ™๋„๋กœ ๋‹ตํ•˜์—ฌ ํ‰๊ฐ€ํ•œ๋‹ค. โ€œHow likely are you to recommend [product/service] to colleague or friend?โ€ ์ฆ‰, ๋™๋ฃŒ๋‚˜ ์นœ๊ตฌ์—๊ฒŒ ๋‹น์‹ ์ด ์“ฐ๊ณ  ์žˆ๋Š” ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ์ถ”์ฒœํ•  ๊ฒƒ์ธ๊ฐ€๋ฅผ ๋ฌผ์–ด๋ณด๋Š” ๊ฒƒ์ด๋‹ค. 0~6์ ์€ ๋น„์ถ” ์ฒœ์ž(detractors), 7~8์ ์€ ์†Œ๊ทน์ž(Passives), 9~10์ ์€ ํ™๋ณด์ž (Promoters)๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค. ๊ตฌ๋งค ์ถ”์ฒœ์„ ๋ฌธ์˜ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์˜ ๋งŒ์กฑ๋„ ์กฐ์‚ฌ์™€ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹ค๋ฅธ ์กฐ์‚ฌ์™€ ๋‹ฌ๋ฆฌ NPS๋Š” ์งˆ๋ฌธ ํ•˜๋‚˜๋กœ ๋ฐ”์ด์–ด์Šค(Bias) ์—†์ด ๋งค์šฐ ์‰ฝ๊ณ  ๋นจ๋ฆฌ ์ง๊ด€์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ๊ณ„์‚ฐ์€ ์œ„์˜ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์„ ์‘๋‹ต์ž ์ˆ˜๋กœ ๋‚˜๋ˆ„์–ด ๋ฐฑ๋ถ„์œจ(%)๋กœ ํ‘œ๊ธฐํ•˜๊ฒŒ ๋˜๋Š”๋ฐ -100%์—์„œ +100%๊นŒ์ง€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์Œ์ˆ˜ ๊ฐ’์ด ๋‚˜์˜ฌ ๊ฒฝ์šฐ๋Š” ๊ณ ๊ฐ ์ถฉ์„ฑ๋„๊ฐ€ ๋‚ฎ๋‹ค๋Š” ์˜๋ฏธ์ด๊ณ  ์กฐ์‚ฌ๋ฅผ ํ•˜๋Š” ๊ธฐ์—… ์ž…์žฅ์—์„œ๋Š” ๋ฌด์กฐ๊ฑด ์–‘์ˆ˜ ๊ฐ’์ด ๋‚˜์™€์•ผ ์žฌ๊ตฌ๋งค ๊ฐ€๋Šฅ์„ฑ์„ ๋ฐœ๊ฒฌํ•˜๊ฒŒ ๋œ๋‹ค[5]. ๋‹จ์ผ ๊ธฐ์—…์ด ์•„๋‹Œ ์‚ฐ์—… ๊ตฐ์„ ๋Œ€์ƒ์œผ๋กœ ์กฐ์‚ฌํ•˜๋ฉด NPS์˜ ์ƒํ•œ๊ณผ ํ•˜ํ•œ์„ ํฌํ•จํ•˜๋Š” ๋ฒ”์œ„๋„ ์‚ฐ์ • ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๊ฒฝ์Ÿ์‚ฌ์™€์˜ ๋น„๊ต๋ผ๋“ ๊ฐ€ ๋ฒค์น˜๋งˆํ‚น ๋„๊ตฌ๋กœ๋„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 8.4 ํ•ต์‹ฌ ๊ตฌ๋งค์š”์†Œ ๋ถ„์„ ์•ž์„œ ๋‹ˆ์ฆˆ(Needs)์™€ ์›์ธ (Wants), ๋””๋งจ๋“œ(Demand)์— ๋Œ€ํ•ด ์„ค๋ช…ํ–ˆ์—ˆ๋‹ค. ๋‹ˆ์ฆˆ์—์„œ ๋””๋งจ๋“œ๊นŒ์ง€ ๋ฐœ์ „ํ•˜๊ณ  ๊ตฌ๋งค ์š”๊ตฌ์‚ฌํ•ญ(Requirements)๋กœ ๊ตฌ์ฒดํ™”๋˜๋ฉด ๋น„๋กœ์†Œ ๊ณ ๊ฐ ๊ฐ€์น˜(Customer Value)์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ณ ๊ฐ์˜ ๊ตฌ๋งค ์š”๊ตฌ์‚ฌํ•ญ์˜ ๊ด€์ ์—์„œ ๊ณ ๊ฐ ๊ฐ€์น˜๋Š” '๋น„์šฉ ๋Œ€๋น„ ํšจ๊ณผ์˜ ์ฐจ์ด'๋กœ ๋งค์šฐ ๋‹จ์ˆœํ•˜๋‹ค. ๊ทธ ํšจ๊ณผ๋Š” ํŽธ์ต(Benefit)์˜ ๋ฌถ์Œ์ด๋ฉฐ ์ด๊ฒƒ์€ ๊ธˆ์ „์ ์ธ ์ธก๋ฉด ๋˜๋Š” ๋น„๊ธˆ์ „์ ์ธ ์ธก๋ฉด์œผ๋กœ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋‹ค.[6] ์ฆ‰ ๊ณ ๊ฐ ๊ฐ€์น˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. Value = Benefit โ€“ Total Cost ๊ณ ๊ฐ์ด ๊ฐ€์น˜๋ฅผ ๋Š๋ผ๋Š” ๊ฒƒ์€ ํˆฌ์ž…๋œ ๋น„์šฉ๋ณด๋‹ค ํŽธ์ต์ด ํด ๋•Œ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ณ ๊ฐ ๊ฐ€์น˜๋Š” ์ƒ๋Œ€์ ์ธ ๊ฐœ๋…์ด๋ผ ๊ฐ™์€ ์ œํ’ˆ์„ ๋†“๊ณ ๋„ ๊ณ ๊ฐ๋“ค์€ ์„œ๋กœ ๋‹ค๋ฅด๊ฒŒ ๋Š๋‚„ ์ˆ˜ ์žˆ๋‹ค. B2C ์ œํ’ˆ๋“ค์„ ์†Œ๋น„ํ•˜๋Š” ์ผ๋ฐ˜ ์†Œ๋น„์ž๋“ค์€ ๋””์ž์ธ์ด๋‚˜ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•ด์„œ ์ œํ’ˆ์„ ๊ตฌ์ž…ํ•œ ์žฅ์†Œ๋‚˜ ์ˆœ๊ฐ„ ๋“ฑ ๋งค์šฐ ๊ฐ์„ฑ์ ์ธ ๋ถ€๋ถ„๊นŒ์ง€ ํฌํ•จํ•˜์—ฌ ๋‹ค์–‘ํ•œ ๋ฉด์—์„œ ๊ฐ€์น˜๋ฅผ ๋Š๋ผ๋Š” ๋ฐ˜๋ฉด, B2B ์‚ฌ์—…์˜ ๊ณ ๊ฐ๋“ค์€ ์ œํ’ˆ ๊ตฌ๋งค ์‹œ ๋‹ค์Œ 5๊ฐ€์ง€ ์†์„ฑ์„ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๊ณ  ์žˆ๋‹ค.[7] ์‹ ๋ขฐ์„ฑ(Reliability) ์ „๋ฌธ์„ฑ(Expertise) ๊ฐ€์น˜(Value) ํšจ์œจ์„ฑ(Efficiency) ๋งž์ถคํ˜•(Customization) ์‹ ๋ขฐ์„ฑ์€ ์ œํ’ˆ์— ๋Œ€ํ•œ ์‹ ๋ขฐ๋‚˜ ๊ด€๊ณ„์— ๋Œ€ํ•œ ์‹ ๋ขฐ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์œผ๋กœ B2B ๊ณ ๊ฐ๋“ค์ด ๊ฑฐ๋ž˜์˜ ๊ธฐ๋ณธ์œผ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ์ด์œ ๋กœ B2B ์‚ฌ์—…์—์„œ ์‚ฌ์—… ์‚ฌ๋ก€(Reference)๊ฐ€ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋ฉฐ ์ค‘์š”ํ•˜๊ฒŒ ์–ธ๊ธ‰๋˜๋Š” ์ด์œ ์ด๊ธฐ๋„ ํ•˜๋‹ค.[8] ๊ธฐ๋ณธ ์†์„ฑ์ธ ์‹ ๋ขฐ์„ฑ์„ ๊ฐ–์ถ”๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ „๋ฌธ์„ฑ ๋ฐ ๊ฐ€์น˜๊ฐ€ ์ž…์ฆ๋˜์–ด์•ผ ํ•œ๋‹ค. ์ „๋ฌธ์„ฑ์ด๋ผ ํ•จ์€ ๊ณ ๊ฐ ์‚ฐ์—… ๋ฐ ์—…๋ฌด์— ๋Œ€ํ•œ ์ „๋ฌธ์„ฑ, ๊ธฐ์ˆ  ์ „๋ฌธ์„ฑ ๋“ฑ์ด ๊ทธ๊ฒƒ์ด๋ฉฐ ๊ทธ๋กœ ์ธํ•ด ์‹œ์žฅ์—์„œ ๊ฐ€์น˜๋ฅผ ์ธ์ •๋ฐ›๋Š” ์ œํ’ˆ์„ ๊ณต๊ธ‰๋ฐ›๊ณ  ๊ณ ๊ฐ์˜ ์ œํ’ˆ๋„ ๊ทธ๋กœ ์ธํ•ด ์‹œ์žฅ์—์„œ ๊ฒฝ์Ÿ๋ ฅ์„ ๊ฐ–์ถ”๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ํšจ์œจ์„ฑ์€ ๊ฐ€์น˜์™€ ์—ฐ๊ฒฐ๋˜๋Š” ์†์„ฑ์ธ๋ฐ ๋น„์šฉ์  ์ธก๋ฉด์ด ์ข€ ๋” ๊ฐ•์กฐ๋œ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ œํ’ˆ ๊ตฌ์ž… ์‹œ ์š”๊ตฌ๋˜๋Š” ๊ฐ์ข… ์š”๊ตฌ์‚ฌํ•ญ๋“ค์€ ์ค‘๊ฐ„์žฌ์ด๋“  ์™„์„ฑํ’ˆ์ด๋“  ๊ธฐ์—… ๊ณ ์œ ์˜ ๊ฒƒ๋“ค์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๋งž์ถคํ˜•์€ B2B ๊ณ ๊ฐ๋“ค์ด ๊ฐ€์žฅ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋งž์ถคํ˜• ์š”๊ตฌ์‚ฌํ•ญ์„ ์ถฉ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์–‘ํ•œ ๊ณ ๊ฐ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ž˜ ๋Œ€์‘ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ๋งŒํผ ๊ณ ๊ฐ๊ธฐ์—…์ด๋‚˜ ๊ณ ๊ฐ ์‚ฌ์—…์„ ์ž˜ ์•Œ๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ๊ณผ ์ผ๋งฅ ์ƒํ†ตํ•œ๋‹ค. ๋”ฐ๋ผ์„œ B2B ๊ธฐ์—… ์ž…์žฅ์—์„œ โ€˜๋งž์ถคํ˜•โ€™์€ ๊ณ ๊ฐ ๊ฐ€์น˜ ์‹คํ˜„์— ์žˆ์–ด์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์š”์†Œ์ด๋‹ค.[9] ์ด์ฒ˜๋Ÿผ ๊ณ ๊ฐ์ด ์ œํ’ˆ์„ ๊ตฌ๋งคํ•  ๋•Œ ์–ด๋–ค ๋ถ€๋ถ„์ด ๋งˆ์Œ์— ๋“ค์–ด์„œ, ์–ด๋–ค ์‚ฌ์œ ๋กœ ๊ตฌ๋งคํ•˜๋Š”์ง€ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ์„ โ€˜ํ•ต์‹ฌ ๊ตฌ๋งค์š”์†Œ(Key Buying Factors: KBF) ๋ถ„์„โ€™์ด๋ผ๊ณ  ํ•œ๋‹ค. ๊ณต๊ธ‰๋˜๋Š” ์ œํ’ˆ์˜ ์‚ฌ์šฉ ์šฉ๋„์™€ ๋”๋ถˆ์–ด ์–ด๋–ค ์š”์†Œ์™€ ํŠน์ง•๋“ค์ด ๊ทธ ์ œํ’ˆ์„ ๊ตฌ๋งคํ•˜๊ฒŒ ๋งŒ๋“œ๋Š”์ง€๋ฅผ ์กฐ์‚ฌํ•˜๋Š” ๊ฒƒ์ธ๋ฐ KBF ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์œ„์—์„œ ์„ค๋ช…ํ•œ 5๊ฐ€์ง€ ๋ฒ”์ฃผ์— ๊ฑฐ์˜ ๋“ค๊ฒŒ ๋˜๊ณ  ๋ณด๋‹ค ๊ตฌ์ฒด์ ์ธ ํ•ญ๋ชฉ์œผ๋กœ ์ƒ์ˆ ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Figure III-28๊ณผ ๊ฐ™์ด ๊ณ ๊ฐ A, ๊ณ ๊ฐ B, ๊ณ ๊ฐ C๋Š” ์ œํ’ˆ ๊ตฌ์ž… ์‹œ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ์š”์†Œ๋“ค์ด ๊ฐ๊ฐ ๋‹ค๋ฅด๋‹ค. Figure III-28. KBF ๋ถ„์„์— ๋”ฐ๋ฅธ ๊ฐ€์น˜ ์ œ์•ˆ ๋”ฐ๋ผ์„œ ๊ณ ๊ฐ A์—๊ฒŒ๋Š” ๊ฐ€๊ฒฉ์  ์ธก๋ฉด์—์„œ, ๊ณ ๊ฐ B๋Š” ๊ธฐ์ˆ ๋ ฅ ์ธก๋ฉด์—์„œ, ๊ณ ๊ฐ C๋Š” ๋‚ฉ๊ธฐ ์ธก๋ฉด์—์„œ ๋งˆ์ผ€ํŒ…๊ณผ ์˜์—…ํ™œ๋™์ด ์ง‘์ค‘๋˜์–ด์•ผ ์„ฑ๊ณต์ ์ธ ๊ฑฐ๋ž˜๊ฐ€ ์ด๋ฃจ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. ์ด์–ด์ง„ Table III-20์€ KBF ๋ถ„์„์˜ ์žฅ๋‹จ์ ์ด๋‹ค. Table III-20. KBF ๋ถ„์„์˜ ์žฅ๋‹จ์  [1] ๋ณธ๋ฌธ์—์„œ๋Š” B2B ๊ณ ๊ฐ์€ ๊ณ ๊ฐ, B2C ๊ณ ๊ฐ์€ ์†Œ๋น„์ž๋กœ ํ‘œ๊ธฐํ•˜์˜€๋‹ค. [2] Online-to-offline ์˜จ๋ผ์ธ๊ณผ ์˜คํ”„๋ผ์ธ์„ ํ†ตํ•ฉํ•˜๋Š” ๋งˆ์ผ€ํŒ… ๋ฐ ์„œ๋น„์Šค [3] ๋ฏธ๊ตญ ์†Œ๋น„์ž ์กฐ์‚ฌ ๊ธฐ๊ด€์ธ J.D.Power ์กฐ์‚ฌ์— ๋”ฐ๋ฅด๋ฉด ์•ฝ๊ฐ„ ๋งŒ์กฑํ•œ๋‹ค๊ณ  ๋‹ตํ•œ 90%์˜ ๊ณ ๊ฐ ๊ฐ€์šด๋ฐ 47.9%๋งŒ์ด ์ œํ’ˆ์„ ์žฌ๊ตฌ๋งคํ•œ ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์œผ๋ฉฐ Bain& Company์˜ ์—ฐ๊ตฌ์—์„œ๋„ ์ดํƒˆ ๊ณ ๊ฐ์˜ 65~85%๊ฐ€ โ€˜๋งŒ์กฑโ€™ ์ด์ƒ์˜ ์ ์ˆ˜๋ฅผ ์ค€ ๊ณ ๊ฐ์œผ๋กœ ํŒŒ์•…๋˜๊ณ  ์žˆ๋‹ค๊ณ  2011๋…„ ํ•œ๊ตญ๊ฒฝ์ œ์‹ ๋ฌธ์—์„œ ์ธ์šฉํ•˜์˜€๋‹ค. [4] 2003๋…„ Bain & Company์˜ Fred Reichheld๊ฐ€ ํ•˜๋ฒ„๋“œ ๋น„์ฆˆ๋‹ˆ์Šค ๋ฆฌ๋ทฐ(HBR)์— โ€˜The One Number You Need to Growโ€™โ€™๋ผ๋Š” ๊ธ€์„ ์‹ฃ๊ณ  ์ดํ›„ ์„ธ์ƒ์— ๋„๋ฆฌ ์•Œ๋ ค์กŒ๋‹ค [5] ์‹ค์ œ NPS ์กฐ์‚ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์ƒ๋‹นํžˆ ๋‚ฎ๊ฒŒ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค [6] ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ๋ฒ•์„ ํ†ตํ•ด ์ •๋Ÿ‰์ ์œผ๋กœ ๋ณ€ํ™˜ ํ›„, ๊ถ๊ทน์ ์œผ๋กœ ๋ชจ๋‘ ๋ˆ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š”๋ฐ ์ด๋Š” ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์ผ์›ํ™”๋ฅผ ์œ„ํ•จ์ด๋‹ค [7] ์ €์ž๊ฐ€ ์ปจ์„คํŒ… ๋“ฑ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ์ฒดํ—˜ํ•œ ๊ฒƒ์— ๊ธฐ๋ฐ˜ํ•œ ๊ฒƒ์ด๋ฉฐ ์„ค๋ฌธ์กฐ์‚ฌ ๋“ฑ์— ๊ทผ๊ฑฐํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ์•„๋งˆ Survey๋ฅผ ํ•ด๋„ ๋น„์Šทํ•˜๊ฒŒ ๋‚˜์˜ค์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•œ๋‹ค [8] B2B ์ œํ’ˆ์—์„œ๋Š” ๊ทธ๋ ‡๊ธฐ์— Pilot ์ˆ˜ํ–‰์ด๋‚˜ Proof of Concept ๊ฐ™์€ ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค [9] ๋งŽ์€ ๊ณ ๊ฐ๋“ค์ด ์˜์™ธ๋กœ ํ˜์‹ ์ด๋‚˜ ๋””์ž์ธ ๋“ฑ์„ ์œ„์˜ 5๊ฐ€์ง€๋ณด๋‹ค ์šฐ์„ ์œผ๋กœ ๋‘์ง€ ์•Š์•˜๋‹ค 08. ๊ณ ๊ฐ ์š”๊ตฌ ๋ถ„์„(3/3) ์ด๋ฒˆ ์ˆœ์„œ๋Š” ๊ณ ๊ฐ ์š”๊ตฌ ๋ถ„์„์˜ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ œํ’ˆ๊ณผ ๊ธฐ์ˆ  ๊ด€์ ์—์„œ ๋ถ„์„ํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž. 8.5 ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ ๋ถ„์„ (Product Life Cycle) ์‚ฌ๋žŒ์ด ํƒœ์–ด๋‚˜์„œ 70~80์„ธ๋ฅผ ์‚ฐ๋‹ค๊ณ  ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์ œํ’ˆ์—๋„ ์ˆ˜๋ช…์ด ์žˆ์–ด ์–ด๋–ค ์ œํ’ˆ์˜ ๋„์ž…๋ถ€ํ„ฐ ์‡ ํ‡ด๊นŒ์ง€ ๊ทธ ๊ธฐ๋Šฅ์ด๋‚˜ ํšจ์šฉ์˜ ์ƒ๋ช…๋ ฅ์ด ์žˆ๋‹ค๊ณ  ์ „์ œํ•˜๊ณ , ๊ทธ์— ๋งž๋Š” ๋งˆ์ผ€ํŒ…/์˜์—… ์ „๋žต์„ ๊ตฌ์‚ฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ ๋ถ„์„์ด๋‹ค. ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ(Product Life Cycel: PLC)๋Š” ๋ณดํ†ต ๋„์ž…๊ธฐ, ์„ฑ์žฅ๊ธฐ, ์„ฑ์ˆ™๊ธฐ, ์‡ ํ‡ด ๊ธฐ์™€๊ฐ™์ด 4๋‹จ๊ณ„ ๋˜๋Š” 5๋‹จ๊ณ„๋กœ ๊ตฌ๋ถ„๋˜๋ฉฐ ๊ทธ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Figure III-29. ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ ๊ทธ๋ž˜ํ”„ ๋„์ž…๊ธฐ(Introduction): ์ œํ’ˆ์˜ ๋„์ž…๊ธฐ๋Š” ์ „ํ˜€ ์ƒˆ๋กœ์šด ๋•Œ๋•Œ๋กœ ์ง„๋ณด์ ์ธ ์ œํ’ˆ์ด๋‚˜ ์ƒํ’ˆ์ด ์„ธ์ƒ์— ๋“ฑ์žฅํ•œ ๊ฒฝ์šฐ์ด๋‹ค. ์Šค๋งˆํŠธํฐ์˜ ์„ธ๊ณ„๋ฅผ ์—ด์—ˆ๋˜ ์• ํ”Œ์˜ ์•„์ดํฐ(iPhone)์„ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋„์ž…๊ธฐ์˜ ์ œํ’ˆ์€ ์ธ์ง€๋„๋ฅผ ํ™•๋ณดํ•˜๋Š” ๊ฒƒ์ด ์ง€์ƒ๊ณผ์ œ์ด๋‹ค. ์ œํ’ˆ์— ๋Œ€ํ•œ ํ™๋ณด์™€ ์†Œ๋น„์ž/๊ณ ๊ฐ์— ๋Œ€ํ•œ ๊ต์œก ๋“ฑ์ด ๋งˆ์ผ€ํŒ… ์ „๋žต ์ฐจ์›์—์„œ ํ•„์š”ํ•˜๋ฉฐ ์‹œ์žฅ ์ง„์ž…์„ ์œ„ํ•œ ์ ํ•ฉํ•œ ๊ฐ€๊ฒฉ ์ „๋žต ์˜ˆ๋ฅผ ๋“ค๋ฉด skimming pricing, penetrating pricing ๋“ฑ์„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. ์„ฑ์žฅ๊ธฐ(Growth): ์„ฑ์žฅ๊ธฐ๋Š” ๋„์ž…๊ธฐ๋ฅผ ํ†ตํ•ด ์ œํ’ˆ์— ๋Œ€ํ•œ ์ธ์‹์ด ํ™•์žฅ๋˜๊ณ  ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋งค์ถœ์ด๋‚˜ ์˜์—…์ด์ต์ด ์ฆ๊ฐ€ํ•˜๋Š” ๋‹จ๊ณ„๋ฅผ ๋งํ•œ๋‹ค. ์„ฑ์žฅ๊ธฐ์˜ ์ œํ’ˆ์— ๋Œ€ํ•ด์„œ๋Š” ์˜์—…์ด์ต์˜ ์žฌํˆฌ์ž๋ฅผ ํ†ตํ•ด ๊ฒฝ์Ÿ ๋Œ€๋น„ ๊ฐ•๋ ฅํ•œ ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ๋ฅผ ํ™•๋ณดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ์ด๋Š” ํ›„๋ฐœ ์‹œ์žฅ ์ง„์ž…์ž๋“ค์—๊ฒŒ ๊ฐ•๋ ฅํ•œ ์ง„์ž… ์žฅ๋ฒฝ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ณ„๊ธฐ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ฒฝ์Ÿ์ž๊ฐ€ ๋“ฑ์žฅํ•˜๊ธฐ ์ „๊นŒ์ง€๋Š” skimming pricing์„ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ์ˆ˜๋„ ์žˆ๊ณ , ๊ฒฝ์Ÿ์ž๊ฐ€ ๋“ฑ์žฅํ•˜์˜€๋‹ค๋ฉด ๊ทธ๋™์•ˆ ์ถ•์ ํ•œ ์ƒ์‚ฐ์ด๋‚˜ ํŒ๋งค ํ•™์Šตํšจ๊ณผ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ€๊ฒฉ ์ธํ•˜ ์ •์ฑ…์„ ์ทจํ•˜๋Š” ๊ฒƒ๋„ ์ข‹๋‹ค. ์„ฑ์ˆ™๊ธฐ(Maturity): ์„ฑ์ˆ™๊ธฐ๋Š” ๊ฒฝ์Ÿ์ž๋“ค๋„ ๋งŽ๊ณ  ์‹œ์žฅ์ ์œ ์œจ๋„ ๊ฑฐ์˜ ๊ณ ์ฐฉํ™”๋œ ์ƒํ™ฉ์ด๋‹ค. ํ˜„์ƒ ์œ ์ง€๊ฐ€ ์ค‘์š”ํ•˜๋ฉฐ ์›๊ฐ€ ์ ˆ๊ฐ์„ ์œ„ํ•œ ๋…ธ๋ ฅ, ์ œํ’ˆ ์™„๊ฒฐ์„ฑ ๋“ฑ์ด ์š”๊ตฌ๋œ๋‹ค. ์•„์šธ๋Ÿฌ ๊ณ ๊ฐ ๋งž์ถคํ˜• ์ƒํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค ๊ฐœ๋ฐœ ๋ฐ ๋‹ค์–‘ํ•œ ๊ฐ€๊ฒฉ ์ „๋žต์„ ํ†ตํ•ด ์„ฑ์ˆ™๊ธฐ๋ฅผ ์ง€์†์‹œํ‚ค๋Š” ๋ฐฉ์•ˆ์ด ํ•„์š”ํ•˜๋‹ค. ์‡ ํ‡ด๊ธฐ(Decline): ์‡ ํ‡ด๊ธฐ๋Š” ๊ธฐ์ˆ ์ ์œผ๋กœ ๋…ธํ™”๋˜๊ณ  ๊ณ ๊ฐ์˜ ํฅ๋ฏธ๋ฅผ ๋” ์ด์ƒ ๋Œ์ง€ ๋ชปํ•˜๋Š” ์ƒํƒœ๋ฅผ ๋งํ•œ๋‹ค. ๊ธฐ์กด ์ œํ’ˆ์˜ ์ƒˆ๋กœ์šด ์šฉ๋„๋ฅผ ์ฐพ์•„๋ณด๊ฑฐ๋‚˜ ๊ธฐ์กด ์ œํ’ˆ์˜ ๊ณ ๊ฐ ๊ฐ€์น˜๊ฐ€ ํ†ต์šฉ๋˜๋Š” ์ƒˆ๋กœ์šด ์‹œ์žฅ ๊ฐœ์ฒ™์ด ํ•„์š”ํ•˜๋‹ค. ์‡ ํ‡ด๊ธฐ ์ œํ’ˆ์€ ์‹œ์žฅ์—์„œ ์‚ฌ๋ผ์ง€๊ฑฐ๋‚˜ ํ˜์‹ ์„ ํ†ตํ•ด ์žฌ๋„์•ฝํ•˜๋Š๋ƒ์˜ ๊ธฐ๋กœ์— ์„œ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋ฉฐ ๊ธฐ์กด ์‹œ์žฅ์„ ํƒ€๊นƒ ํ•œ๋‹ค๋ฉด ๊ฐ€๊ฒฉ์€ ๋งŽ์ด ๋–จ์–ด์ง€๊ฒŒ ๋œ๋‹ค. ์ผ๋ถ€ ์ œํ’ˆ์˜ ๊ฒฝ์šฐ, ํฌ๊ท€ ์„ฑ ๋˜๋Š” ํฌ์†Œ์„ฑ์œผ๋กœ ์ถ”๊ฐ€ ์ด์ต๋„ ๋‹ฌ์„ฑํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด ๊ฒฝ์šฐ ์ƒ๋‹นํ•œ ๋ธŒ๋žœ๋“œ ๊ฒฝ์Ÿ๋ ฅ์ด ๋’ท๋ฐ›์นจ๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ PLC ๊ฐœ๋…์ด ํ•ญ์ƒ ์˜ณ์€ ๊ฒƒ์€ ์•„๋‹ˆ๋ฉฐ ์‹ค์ œ ์‚ฌ์—… ํ™˜๊ฒฝ์—์„œ๋Š” ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. ๋ธŒ๋žœ๋“œ ํŒŒ์›Œ์™€ ํ•ฉ์ณ์งˆ ๊ฒฝ์šฐ, ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ์ง€์†๋˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๊ณ  ์—†์–ด์งˆ ๋“ฏํ•œ ์ œํ’ˆ๋“ค๋„ ๋ธŒ๋žœ๋“œ๋ฅผ ์ž…์–ด ๋‹ค์‹œ ํƒœ์–ด๋‚˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. ์ผํšŒ์  ์‚ฌ์šฉ์ผ ๊ฒƒ์ด๋ผ๋Š” ์ „๊ตฌ๋‚˜ ์ปดํ“จํ„ฐ ์‚ฌ์šฉ์œผ๋กœ ์ธํ•ด ์—†์–ด์งˆ ๊ฒƒ์ด๋ผ๊ณ  ํ•œ ์ข…์ด๊ฐ€ ํ˜„์žฌ๊นŒ์ง€ ์กด์žฌํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ์•„์ง๋„ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์ด์œ ์ด๋‹ค. Table III-21์€ ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ์˜ ์žฅ๋‹จ์ ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. Table III-21. ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ์˜ ์žฅ๋‹จ์ ][1] 8.6 S-์ปค๋ธŒ ๋ถ„์„ S-์ปค๋ธŒ ๋ถ„์„์€ ๊ธฐ์ˆ  ๊ด€์ ์—์„œ ์‚ฌ์—…์„ ๋ณด๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ถ„์„ ๋ฐฉ๋ฒ•์œผ๋กœ ๋Œ€๋ถ€๋ถ„์˜ ํ•˜์ดํ…Œํฌ ์‚ฐ์—…(High-Tech Industry)์—์„œ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์‹ ๊ธฐ์ˆ  ๊ฐœ๋ฐœ์˜ ์„ฑ๊ณผ๊ฐ€ ์ดˆ๋ฐ˜์—๋Š” ๋ณ„ ๋ณผ์ผ ์—†๋Š”๋ฐ ๊ฐ‘์ž๊ธฐ ํญ๋ฐœ์ ์ธ ์„ฑ์žฅ ๊ถค๋„๋ฅผ ๊ทธ๋ฆฌ๋‹ค๊ฐ€ ์–ด๋Š ์‹œ์ ์— ์ •์ฒด๋˜์–ด ์‚ฌ๋ผ์ง€๋Š” ๋ชจ์–‘์ด ๋งˆ์น˜ ์•ŒํŒŒ๋ฒณ S์™€ ๊ฐ™๋‹ค๊ณ  ํ•˜์—ฌ S-Curve ๋ถ„์„์ด๋ผ ๋ถ€๋ฅธ๋‹ค. Figure III-30. S-Curve์˜ ๊ฐœ๋… S-์ปค๋ธŒ์˜ ์ฒซ ๋‹จ๊ณ„๋Š” ์ƒ์šฉํ™” ์ด์ „์˜ ์‹œํ—˜ ๋‹จ๊ณ„๋กœ ๊ธฐ์—…์ด ์‹ ์ œํ’ˆ์„ ์‹œ์žฅ์— ๋‚ด๋†“๊ณ  ์ฃผ๋ชฉ์„ ๋ฐ›์œผ๋ฉด์„œ ์™„๋งŒํžˆ ์„ฑ์žฅํ•˜๋Š” ๋‹จ๊ณ„์ด๋‹ค. ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด์„œ ์ œํ’ˆ์˜ ๊ฒฐํ•จ๋„ ๋…ธ์ถœ/๋ณด์™„์ด ๋˜๊ณ  ์ธ์ง€๋„๋„ ์ฆ๊ฐ€ํ•˜๋ฉด ๋Œ€์ค‘์˜ ๊ด€์‹ฌ์„ ๋Œ๊ฒŒ ๋˜๋ฉด, ๊ฐ‘์ž๊ธฐ ๊ฐ€ํŒŒ๋ฅธ ์„ฑ์žฅ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค ์†Œ๋น„์ž์˜ ์„ ํ˜ธ๋„๊ฐ€ ๋ณ€ํ™”ํ•˜๊ณ  ๊ฒฝ์Ÿ ์ œํ’ˆ(๋Œ€์ฒด์žฌ)์ด ๋“ฑ์žฅํ•˜๋ฉด์„œ ์„ฑ์žฅ์ด ์ •์ฒด๋˜๊ณ  ํ‡ด์กฐ๊ธฐ๋ฅผ ๋งž์ดํ•˜๊ฒŒ ๋œ๋‹ค. ํ‡ด์กฐ๊ธฐ๋ฅผ ๊ทน๋ณตํ•˜๊ณ  Figure III-31๊ณผ ๊ฐ™์ด ์ฃผ๊ธฐ์ ์œผ๋กœ ์ƒˆ S-์ปค๋ธŒ๋กœ ๊ฐˆ์•„ํƒ€๋ฉฐ ์„ฑ๊ณต์„ ๊ฑฐ๋“ญํ•˜๋Š” ๊ธฐ์—…์„ โ€˜ํ•˜์ด-ํผํฌ๋จผ์Šค ๊ธฐ์—…(High Performance Company)'์ด๋ผ๊ณ  ํ•œ๋‹ค. ํŽ˜์ด์Šค๋ถ์˜ ๊ฒฝ์šฐ, ์‚ฌ์šฉ์ž ์ˆ˜๊ฐ€ 2์–ต ๋ช…์— ๋„๋‹ฌํ•˜๊ธฐ ์ „๊นŒ์ง€ 5๋…„์ด ๊ฑธ๋ ธ๋Š”๋ฐ 8์–ต ๋ช…์— ๋„๋‹ฌํ•˜๋Š” ๋ฐ๋Š” 2๋…„ ๋ฐ–์— ๊ฑธ๋ฆฌ์ง€ ์•Š์•˜๋‹ค๊ณ  ํ•œ๋‹ค. ํฌ๋ฆฌ์Šคํ…์Šจ(Clayton M.Christensen. 1962 ~ ํ˜„์žฌ)์€ ๊ทธ์˜ ํ˜์‹  ์ด๋ก  ์ค‘ ์™€ํ•ด์„ฑ ๊ธฐ์ˆ (Disruptive Technologies)์„ ์„ค๋ช…ํ•˜๋ฉด์„œ ์šฐ๋Ÿ‰ ๊ธฐ์—…๋“ค์ด ์ฃผ๋ ฅ ์ œํ’ˆ์˜ ์ ์ง„์  ๊ฐœ์„ ์—๋งŒ ๋งค๋‹ฌ๋ฆฌ๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ์‹œ์žฅ ์ง€๋ฐฐ๋ ฅ์„ ์žƒ๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ง๋ฉดํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ๋Š” ์™€ํ•ด์„ฑ ๊ธฐ์ˆ ์„ ์ˆ˜์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ์‹œ์žฅ์„ ์ฐพ์•„๋‚ด์•ผ ํ•จ์„ ํ˜์‹ ์˜ ๋”œ๋ ˆ๋งˆ(Dilemma)๋กœ ์„ค๋ช…ํ•œ๋‹ค. ์ฆ‰, ์„ฑ์žฅ๊ณผ ์ด์ต์ด ๊ฐ€ํŒŒ๋ฅผ ๋•Œ ์ƒˆ๋กœ์šด ๊ฐ€์น˜๋ฅผ ์ฐพ์•„ ๋„์ „ํ•ด์•ผ ์ง€์†์ ์œผ๋กœ ์„ฑ์žฅ ๊ฐ€๋Šฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. Figure III-31. ๊ณ ์„ฑ๊ณผ ๊ธฐ์—…์˜ S-์ปค๋ธŒ ํ•œํŽธ, S-์ปค๋ธŒ๋Š” Figure III-32์™€ ๊ฐ™์ด ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ โ€“ ๋„์ž…๊ธฐ, ์„ฑ์žฅ๊ธฐ, ์„ฑ์ˆ™๊ธฐ, ์‡ ํ‡ด๊ธฐ ๋ถ„์„๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. S-์ปค๋ธŒ์˜ ์ œํ’ˆ ์‹œํ—˜ ๋‹จ๊ณ„๋Š” ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ๋กœ ๋ณด๋ฉด ๋„์ž…๊ธฐ์— ํ•ด๋‹นํ•œ๋‹ค. ๋˜ํ•œ, ์„ฑ์žฅ๊ธฐ์™€ ์„ฑ์ˆ™๊ธฐ๋Š” S-์ปค๋ธŒ์˜ ์„ฑ์žฅ๊ธฐ์— ํ•ด๋‹นํ•˜๊ณ  ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ์˜ ์‡ ํ‡ด๊ธฐ๋Š” S-์ปค๋ธŒ์˜ ์‡ ํ‡ด ๊ธฐ์™€ ๋งค์นญ๋  ์ˆ˜ ์žˆ๋‹ค. Figure III-32. ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ์™€ S-์ปค๋ธŒ์˜ ๊ฒฐํ•ฉ Table III-22๋Š” S-์ปค๋ธŒ ๋ถ„์„์˜ ์žฅ๋‹จ์ ์„ ์š”์•ฝํ•œ ๊ฒƒ์ด๋‹ค Table III-22. S-์ปค๋ธŒ ๋ถ„์„์˜ ์žฅ๋‹จ์ [2] ๊ฐœ์ธ์˜ ์ง€์‹ ์Šต๋“์ด๋‚˜ ํ•™์Šต ๊ด€์ ์—์„œ ๋ณด๋”๋ผ๋„ S-์ปค๋ธŒ๋Š” ์œ ์šฉํ•œ ์‹œ์‚ฌ์ ์„ ์ œ๊ณตํ•œ๋‹ค. Figure III-33์€ S-์ปค๋ธŒ์˜ ๋„์ž…๋ถ€๋ถ€ํ„ฐ ์„ฑ์žฅ์— ์ด๋ฅด๊ธฐ๊นŒ์ง€๋Š” ์„ ์ˆœํ™˜ ์‚ฌ์ดํด(Virtuous Cycle)์ด ๋ฐœ์ƒํ•œ๋‹ค. S-์ปค๋ธŒ์˜ ๋„์ž…๋ถ€์—๋Š” ์„ ์ˆœํ™˜ ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•˜์—ฌ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๊ธ‰๊ฒฉํ•œ ์„ฑ์žฅ(Hyper Growth)์„ ์•ผ๊ธฐํ•˜๊ณ  ์„ฑ์žฅ ์ •์ฒด๊ธฐ์— ์ ‘์–ด๋“ค๋ฉด ์•…์ˆœํ™˜(Vicious Cycle)์ด ๋ฐœ์ƒํ•˜์—ฌ ์ง€์‹ ์Šต๋“์ด๋‚˜ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ํ•œ๊ณ„์— ๋‹ฌํ•˜๊ฒŒ ๋œ๋‹ค. Figure III-33. S-์ปค๋ธŒ์˜ ์„ ์ˆœํ™˜ ๊ตฌ์กฐ์™€ ์•…์ˆœํ™˜ ๊ตฌ์กฐ 8.7 ๊ธฐ์ˆ  ์ˆ˜์šฉ ๊ณก์„ (Adoption Cycle) ๋ถ„์„ ์ฒจ๋‹จ ๊ธฐ์ˆ ์ด ๋งŒ๋“ค์–ด๋‚ด๋Š” ์ œํ’ˆ์„ ๋‹ค๋ฃจ๋Š” ๋งˆ์ผ€ํŒ…์„ ํ•˜์ดํ…Œํฌ(High-tech) ๋งˆ์ผ€ํŒ…์ด๋ผ ํ•˜๋Š”๋ฐ ์ด์™€ ๊ด€๋ จํ•ด์„œ๋Š” ์ œํ’ˆ ์ˆ˜๋ช…์ฃผ๊ธฐ(PLC) ๋ถ„์„์ด๋‚˜ S-์ปค๋ธŒ ๋ถ„์„, ๊ธฐ์ˆ  ์ˆ˜์šฉ ๊ณก์„  ๋ถ„์„ ๋“ฑ์„ ์ข…ํ•ฉํ•˜์—ฌ ๋ถ„์„ํ•˜๋Š” ์ผ์ด ๋งค์šฐ ์ผ๋ฐ˜์ ์ด๋‹ค. ๊ธฐ์ˆ  ์ˆ˜์šฉ ๊ณก์„ ์€ ์ฒจ๋‹จ ๊ธฐ์ˆ ์ด ์ ์šฉ๋œ ์‹ ์ œํ’ˆ๋“ค์˜ ํ˜์‹ ์„ฑ์„ ์–ด๋–ป๊ฒŒ ๋ฐ›์•„๋“ค์ด๋ฉฐ ์ด๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ๊ธฐ์—…์˜ ์บ์‹œ์นด์šฐ(Cash cow)๋กœ ์ „ํ™˜๋˜๋Š”์ง€๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.[3] ๊ธฐ์ˆ  ์ˆ˜์šฉ ๊ณก์„ ์—์„œ๋Š” Figure III-34์™€ ๊ฐ™์ด 5๊ฐ€์ง€ ์ง‘๋‹จ์ด ํ˜์‹ ์ ์ธ ์‹ ์ œํ’ˆ์— ๋Œ€ํ•ด ๋ฐ˜์‘ํ•˜๊ฒŒ ๋œ๋‹ค. ํ˜์‹  ์ˆ˜์šฉ์ž ๊ทธ๋ฃน(Innovators): ์ƒˆ๋กœ์šด ๊ธฐ์ˆ ์ด ๋‚˜์™”์„ ๋•Œ ๋ฌด์กฐ๊ฑด ์ˆ˜์šฉํ•˜๋Š” ๊ทธ๋ฃน์œผ๋กœ ์‹ ๊ธฐ์ˆ ์— ๋ฌธ์ œ๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ๋ถˆํŽธํ•˜๋”๋ผ๋„ ์•„๋ฌด๋Ÿฐ ๋ถˆ๋งŒ์„ ํ‘œ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค. ์„ ๊ฐ์  ์ˆ˜์šฉ์ž ๊ทธ๋ฃน(Early Adopters): ์‹ ๊ธฐ์ˆ ์˜ ์ง„๊ฐ€๋ฅผ ์•Œ๊ณ  ์ด๊ฒƒ์˜ ๊ฒฝ์ œ์  ๊ฐ€์น˜์™€ ์ „๋žต์  ์˜๋ฏธ๋ฅผ ๋†’๊ฒŒ ์‚ฐ๋‹ค ์ „๊ธฐ ๋‹ค์ˆ˜ ์ˆ˜์šฉ์ž ๊ทธ๋ฃน(Early Majority): ์‹ค์šฉ์  ๊ตฌ๋งค ๊ทธ๋ฃน์œผ๋กœ ์‹ ๊ธฐ์ˆ ์— ๊ด€์‹ฌ์ด ๋งŽ์ง€๋งŒ ๋ชจํ—˜์„ ์ง€์–‘ํ•˜๊ณ  ์‹ ๊ธฐ์ˆ ์ด ์„ฑ์ˆ™ํ™”, ์•ˆ์ •ํ™”๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ทธ๋ฃน์ด๋‹ค ํ›„๊ธฐ ๋‹ค์ˆ˜ ์ˆ˜์šฉ์ž ๊ทธ๋ฃน(Late Majority): ์ฒจ๋‹จ ๊ธฐ์ˆ ์— ๋ถ€์ •์ ์ธ ์‹œ๊ฐ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ ์‹ ๊ธฐ์ˆ ์ด ์—…๊ณ„ ํ‘œ์ค€์˜ ์ง€์œ„๋ฅผ ํ™•๋ณดํ•˜์ง€ ๋ชปํ•˜๋ฉด ์ˆ˜์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ง€๊ฐ ์ˆ˜์šฉ์ž ๊ทธ๋ฃน(Laggards): ์‹ ๊ธฐ์ˆ ์„ ๊ฒฐ๊ตญ ํ™œ์šฉํ•˜์ง€๋งŒ ๊ธฐ์ˆ ์˜ ์กด์žฌ๋‚˜ ๊ทผ์›์  ํŠน์„ฑ ๋“ฑ์— ๋Œ€ํ•ด์„œ๋Š” ์•Œ์ง€ ๋ชปํ•œ๋‹ค Figure III-34. ๋กœ์ €์Šค ํ˜์‹  ๊ณก์„ (Roger's Innovation Curve) ํ•˜์ดํ…Œํฌ ์‚ฐ์—…์—์„œ ๋“ฑ์žฅํ•˜๋Š” ์‹ ๊ธฐ์ˆ ์˜ ๋Œ€๋ถ€๋ถ„์€ ์‹คํŒจํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๋กœ์ €์Šค(Everett Rogers. 1931 ~ 2004)๋Š” ์‹ ๊ธฐ์ˆ ๋กœ ๋งŒ๋“ค์–ด์ง„ ์ œํ’ˆ๋“ค์ด ์‹œ์žฅ ์ ์œ ์œจ 16%๋ฅผ ๋„˜์œผ๋ฉด ์„ฑ๊ณตํ•  ๊ฒƒ์ด๋ผ๊ณ  ํ–ˆ๋˜ ๋ฐ˜๋ฉด, ์ œํ”„๋ฆฌ ๋ฌด์–ด(Geoffrey Moore. 1920 ~ ํ˜„์žฌ)๋Š” ์„ ๊ฐ์  ์ˆ˜์šฉ์ž ๊ทธ๋ฃน๊ณผ ์ „๊ธฐ ๋‹ค์ˆ˜ ์ˆ˜์šฉ์ž ๊ทธ๋ฃน์˜ ์‹ฌ๋ฆฌ์  ์ฐจ์ด ์ฆ‰, ์บ์ฆ˜(Chasm)[4]์„ ๊ทน๋ณตํ•˜์ง€ ๋ชปํ•˜๋ฉด ์‹คํŒจํ•  ๊ฒƒ์ด๋ผ๊ณ  ์ฃผ์žฅํ•˜์˜€๋‹ค. ๋‘ ์‚ฌ๋žŒ ์˜๊ฒฌ ๋ชจ๋‘ ํ˜„์žฅ ๋…ผ๋ฆฌ์ด๋ฏ€๋กœ ์ƒํ™ฉ์— ๋”ฐ๋ผ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค. ์–ด์ฐŒ ๋˜์—ˆ๋˜ ์„ ๊ฐ์  ์ˆ˜์šฉ์ž ๊ทธ๋ฃน(Visionaries)๊ณผ ์‹ค์šฉ ์ฃผ์˜์ž ๊ทธ๋ฃน(Pragmatists)์˜ ํŠน์„ฑ์„ ๊ตฌ๋ถ„ํ•˜๊ณ  ๋งˆ์ผ€ํŒ… ์ „๋žต์— ๊ทธ์— ๋งž๊ฒŒ ์ˆ˜๋ฆฝํ•ด์•ผ ์บ์ฆ˜์„ ๊ทน๋ณตํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด ์ค‘์š”ํ•˜๋‹ค. Table III-23์€ ๊ธฐ์ˆ  ์ˆ˜์šฉ ๊ณก์„ ์˜ ์žฅ๋‹จ์ ์„ ์š”์•ฝํ•œ ๊ฒƒ์ด๋‹ค Table III-23. ๊ธฐ์ˆ  ์ˆ˜์šฉ ๊ณก์„ ์˜ ์žฅ๋‹จ์  Break #15. ๊ฐ€ํŠธ๋„ˆ๊ทธ๋ฃน์˜ ํ•˜์ดํ”„ ์‚ฌ์ดํด ๊ณก์„ (Hype cycle curve) IT ๋ฆฌ์„œ์น˜ ๊ธฐ์—…์€ ๊ฐ€ํŠธ๋„ˆ ๊ทธ๋ฃน(www.gartner.com)์€ ๊ธฐ์ˆ  ์ˆ˜์šฉ ์ฃผ๊ธฐ ๊ณก์„ ์„ ์—ฐ๊ตฌํ•˜๋‹ค๊ฐ€ ์ด ๋ชจ๋ธ์˜ ๋‹จ์ ์„ ๋ฐœ๊ฒฌํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ธฐ์ˆ  ์ˆ˜์šฉ ๊ณก์„ ์˜ ํ™•๋ฅ  ๊ฐ’์„ ๋ˆ„์ ํ•˜๋ฉด Figure III-35์™€ ๊ฐ™์€ ํ˜•ํƒœ์˜ S-๊ณก์„ ์„ ๊ทธ๋ฆฌ๊ฒŒ ๋œ๋‹ค. Figure III-35. ๋ˆ„์  ํ˜•ํƒœ์˜ ๊ธฐ์ˆ  ์ˆ˜์šฉ ๊ณก์„  ์ดˆ๊ธฐ์˜ ํ˜์‹  ์ˆ˜์šฉ์ž ๊ทธ๋ฃน์ด๋‚˜ ์„ ๊ฐ์  ์ˆ˜์šฉ์ž ๊ทธ๋ฃน์„ ๊ฑฐ์ณ ์บ์ฆ˜(chasm)์„ ๊ทน๋ณตํ•˜๋ฉด ์ฃผ๋ฅ˜๋กœ ํ™•๋Œ€๋˜๋ฉด์„œ ์ˆ˜์šฉ ๋„๋Š” ๊ธ‰๊ฒฉํžˆ ์ฆ๊ฐ€ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค ์„ฑ์žฅ๋ฅ ์ด ์ •์ฒด๋˜๊ณ  ์„œ์„œํžˆ ์‡ ํ‡ดํ•˜๊ฒŒ ๋˜๋Š” ๋ชจ์–‘์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์ˆ˜์šฉ ๋„๋Š” ์‹œ์žฅ ๊ทœ๋ชจ๋ผ๊ณ  ์ƒ๊ฐํ•ด๋„ ๋˜๋Š”๋ฐ ์‹œ์žฅ์˜ ์„ฑ์žฅ ์ถ”์ด๋ฅผ ๋น„๊ต์  ์ •ํ™•ํ•˜๊ฒŒ ๋ชจ๋ธ๋งํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ œ๋Š” ๊ธฐ์ˆ  ๊ธฐ์—…๋“ค์ด ์ด ์ˆ˜์šฉ๋„๋ฅผ ์‹œ์žฅ ๊ทœ๋ชจ๋ณด๋‹ค๋Š” ๊ธฐ์ˆ ์— ๋Œ€ํ•œ ์‹œ์žฅ์˜ ๊ด€์‹ฌ๋„๋‚˜ ์ดํ•ด๋„๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์˜คํŒํ•˜๊ณ  ์‹œ์žฅ ์ง„์ž…์„ ์‹œ์ž‘ํ•˜๋ฉด์„œ ์‹คํŒจํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•˜์˜€๋‹ค. ์ฆ‰, ์‹ ๊ธฐ์ˆ ์— ๋Œ€ํ•œ ์‹œ์žฅ์˜ ๊ด€์‹ฌ๋„๊ฐ€ ์ตœ๊ณ ์กฐ์— ๋‹ฌํ–ˆ์„ ๋•Œ ์‹œ์žฅ์˜ ์„ฑ์ˆ™์ด ๊ทน์— ๋‹ฌํ–ˆ๋‹ค๊ณ  ํŒ๋‹จํ•˜๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฒ”ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ, ๊ด€์‹ฌ์ด ์‹์œผ๋ฉด ์‹œ์žฅ์ด ์ด๋ฏธ ์„ฑ์ˆ™๊ธฐ์— ๋“ค์–ด์„ฐ๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ฑฐ๋‚˜ ์‡ ํ‡ด๊ธฐ๋กœ ์ง„์ž…ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ ์‹œ์žฅ์„ ๊ฒฝ์Ÿ์ž๋“ค์—๊ฒŒ ๋‚ด์ฃผ๋Š” ๊ฒฝ์šฐ๋„ ๋ฐœ์ƒํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ€ํŠธ๋„ˆ๋Š” ์‹ ๊ธฐ์ˆ ์— ๋Œ€ํ•œ ์ˆ˜์šฉ ๋„์™€ ์‹œ์žฅ์˜ ๊ด€์‹ฌ๋„๋Š” ์„œ๋กœ ๋ณ„๊ฐœ๋ผ๋Š” ๊ฒƒ์„ โ€˜hype(๊ณผ๋Œ€ํ•œ, ๊ณผ์žฅ๋œ) cycle curveโ€™๋ผ๋Š” ๊ฒƒ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ๋‹ค. Figure III-36. Hype Cycle Curve๊ณผ ๋ˆ„์  S-Curve Figure III-36๊ณผ ๊ฐ™์ด ๊ฐ€ํŠธ๋„ˆ์˜ ํ•˜์ดํ”„ ์‚ฌ์ดํด ๊ณก์„ ์€ ์‹œ๊ฐ„์ด ์ง€๋‚จ์— ๋”ฐ๋ฅธ ์‹ ๊ธฐ์ˆ ์˜ ๊ด€์‹ฌ๋„๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์œผ๋กœ ์‹œ์žฅ์ด ์•„์ง ์ดˆ๊ธฐ์ž„์—๋„ ๊ด€์‹ฌ๋„๊ฐ€ ๊ทน์— ๋‹ฌํ•˜๋Š” ๊ฑฐํ’ˆ๊ธฐ๊ฐ€ ์žˆ๋‹ค๋Š” ์ ์ด ํŠน์ดํ•˜๋‹ค. ๊ธฐ์ˆ  ์ˆ˜์šฉ๋„๊ฐ€ 20%๊ฐ€ ๋˜๋Š” ์‹œ์ ์„ ์ง€๋‚˜๋ฉด ์‹œ์žฅ์˜ ๊ด€์‹ฌ์ด ์ ์  ์‚ฌ๊ทธ๋ผ์ง€๋ฉฐ ์‹ค์ œ์ ์ธ ์ฒซ ์ œํ’ˆ์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ๊ฐ€ํŠธ๋„ˆ๋Š” ์ด 20%๊ฐ€ ๋˜๋Š” ์ ์„ ์ฃผ๋ชฉํ•˜๊ณ  FigureIII-37๊ณผ ๊ฐ™์€ ์‹ ๊ธฐ์ˆ  ์ˆ˜๋ช…์ฃผ๊ธฐ ๊ณก์„ ์„ ๊ฐœ๋ฐœํ•˜๊ณ  ์ด๋ฅผ ๊ฐ€ํŠธ๋„ˆ ํ•˜์ดํ”„ ์‚ฌ์ดํด ๊ณก์„ (Hype Cycle Curve)์œผ๋กœ ์†Œ๊ฐœํ•˜๊ณ  ์žˆ๋‹ค. ๊ฑฐํ’ˆ๊ธฐ๊ฐ€ ์ง€๋‚˜๊ณ  ๋ณธ๊ฒฉ์ ์ธ ์‹œ์žฅ๊ทœ๋ชจ ํ™•์ถฉ๊ณผ ํฌ์ง€์…”๋‹์„ ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ๋‹ค. Figure III-37. ๊ฐ€ํŠธ๋„ˆ๊ทธ๋ฃน์˜ Hype Cycle Curve ์ง€๊ธˆ๊นŒ์ง€ ๊ณ ๊ฐ ์š”๊ตฌ ๋ถ„์„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. ๋งˆ์ง€๋ง‰์— ๊ธฐ์ˆ /์ œํ’ˆ์— ๊ด€ํ•œ ๋ถ„์„ ๋ฒ•์„ ๊ณ ๊ฐ ์š”๊ตฌ ๋ถ€๋ถ„์— ๋„ฃ์€ ๊ฒƒ์€ ๊ธฐ์ˆ  ์ˆ˜์šฉ์— ๋Œ€ํ•œ ๊ณ ๊ฐ์˜ ๊ด€์‹ฌ๋„๊ฐ€ ๋งค์šฐ ์ค‘์š”ํ•˜๊ณ  ์ „๋žต์ด๋‚˜ ๋งˆ์ผ€ํŒ…์— ์ ์ง€ ์•Š๊ฒŒ ์˜ํ–ฅ์„ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์–ด๋–ค ์‚ฐ์—…์€ ์ฒจ๋‹จ ๊ธฐ์ˆ ์ด ์ค‘์š”ํ•˜๊ณ , ์–ด๋–ค ์‚ฐ์—…์€ ์ฒจ๋‹จ ๊ธฐ์ˆ ๋ณด๋‹ค๋Š” ์•ˆ์ •์ ์ด๋ฉฐ ๊ฒ€์ฆ๋œ ๊ธฐ์ˆ ์ด ์ค‘์š”ํ•˜๋‹ค. ๊ทธ๋Ÿฐ ์‚ฐ์—…์ด๋‚˜ ์—…๋ฌด ํŠน์„ฑ์ด ๊ฒฐ๊ตญ ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ, ์›์ธ , ๋””๋งจ๋“œ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š”๋ฐ ์ค‘์š”ํ•œ ์˜ํ–ฅ์„ ๋ผ์นœ๋‹ค๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง์ž. ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„์—์„œ ์žฌ๋ฌด๋น„์œจ ๋ถ„์„์„ ์‚ดํŽด๋ณด์•˜๋Š”๋ฐ ๋‹ค์Œ ์žฅ์—์„œ๋Š” ๊ทธ ๋ถ€๋ถ„์„ ํ™•๋Œ€ํ•˜์—ฌ ์‚ฌ์—…์˜ ์ˆ˜์ต์„ฑ์„ ๊ฒ€ํ† ํ•˜๋Š” ์ˆ˜์ต์„ฑ ๋ถ„์„์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. [1] ์ปจ ์กฐ์ธํŠธ ๋ถ„์„์€ ์–ด๋–ค ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค ๋“ฑ์— ๋Œ€ํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋Œ€์•ˆ๋“ค์„ ๋งŒ๋“ค์–ด ๊ทธ ๋Œ€์•ˆ๋“ค์— ๋ถ€์—ฌํ•˜๋Š” ์†Œ๋น„์ž๋“ค์˜ ์„ ํ˜ธ๋„๋ฅผ ์ธก์ •ํ•˜๊ณ  ์†Œ๋น„์ž๊ฐ€ ๊ฐ ์†์„ฑ(attributes)์— ๋ถ€์—ฌํ•˜๋Š” ์ƒ๋Œ€์  ์ค‘์š”๋„(relative importance)์™€ ๊ฐ ์†์„ฑ ์ˆ˜์ค€์˜ ํšจ์šฉ(utility)์ด๋‚˜ ๋ถ€๋ถ„ ๊ฐ€์น˜(part-worth)๋ฅผ ์ถ”์ •ํ•˜๋Š” ๋ถ„์„ ๊ธฐ๋ฒ•์„ ๋งํ•จ [2] ์ˆ˜ํ‰์  ์‚ฌ๊ณ (Lateral Thinking)๋Š” ๊ธฐ์กด์— ํ™•๋ฆฝ๋œ ํŒจํ„ด์— ์˜ํ•ด ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ ๋ฅผ ์ „๊ฐœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ†ต์ฐฐ๋ ฅ๊ณผ ์ฐฝ์˜๋ ฅ์„ ๋ฐœํœ˜ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ธก๋ฉด์—์„œ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์‹œ๋„ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. [3] ํ˜์‹  ํ™•์‚ฐ ์ด๋ก ์ด๋ผ๋Š” ์šฉ์–ด๋กœ๋„ ๋ถˆ๋ฆฌ๋ฉฐ ์‚ฌํšŒ๊ณผํ•™์—์„œ๋„ ์‚ฌ์šฉํ•œ๋‹ค. [4] ์บ์ฆ˜์€ ์ง€์งˆํ•™์  ์šฉ์–ด๋กœ ์ง€์ธต์˜ ์›€์ง์ž„์œผ๋กœ ์ƒ๊ฒจ๋‚œ ๊ณจ์„ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰, ์†Œ๋น„๊ฐ€ ๋ง‰ ์‹œ์ž‘๋˜๋‹ค๊ฐ€ ๊ฐ‘์ž๊ธฐ ๋š ๊บผ์ง€๋Š” ํ˜„์ƒ์„ ์ƒ๊ฐํ•˜๋ฉด ๋˜๋Š”๋ฐ ํŠนํžˆ, ์‹ ๊ธฐ์ˆ ์„ ์ ์šฉํ•œ ์ œํ’ˆ์˜ ๊ฒฝ์šฐ์— ๋งŽ์ด ๋ชฉ๊ฒฉํ•  ์ˆ˜ ์žˆ๊ณ , ๊ทธ๋Ÿฐ ๊ธฐ์ˆ  ๊ธฐ๋ฐ˜์˜ ์Šคํƒ€ํŠธ์—…๋“ค์ด ์ด๋ฅผ ๊ทน๋ณตํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„ ๋งํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ตœ๊ทผ์—๋Š” ์Šคํƒ€ํŠธ์—…๋„ ์‹ ๊ธฐ์ˆ  ๊ธฐ๋ฐ˜์ด ์•„๋‹ˆ๋ผ ๊ธฐ์กด์˜ ์—…๋ฌด๋‚˜ ๊ธฐ์ˆ ์˜ ๋ถ€๋ถ„์„ ํ˜์‹ ํ•˜๋ฉด์„œ ๊ธธ์„ ์ฐพ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์„ฑ๊ณต ํ™•๋ฅ ์ด ๋†’๋‹ค. 09. ์ˆ˜์ต์„ฑ ๋ถ„์„(1/2) ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—…๋ถ„์„์˜ 4๋ฒˆ์งธ ์‹œ๊ฐ„์—์„œ ์ด๋ฏธ ์žฌ๋ฌด๋น„์œจ ๋ถ„์„์„ ๋‹ค๋ฃจ์–ด๋ณด์•˜๋‹ค. ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„ (4/4) ์žฌ๋ฌด๋น„์œจ ๋ถ„์„ .. ์žฅ์‚ฌ๋Š” ๋ˆ์„ ๋ฒŒ์–ด์•ผ์ง€! | 7.8 ์žฌ๋ฌด๋น„์œจ ๋ถ„์„ ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„์˜ ๋งˆ์ง€๋ง‰ ์ˆœ์„œ๋กœ ์žฌ๋ฌด๋ถ„์„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. ์ •ํ™•ํžˆ ๋งํ•˜๋ฉด ์žฌ๋ฌด๋น„์œจ ๋ถ„์„์ด๋‹ค. ๊ฒฝ์Ÿ๋ ฅ์„ ํŒ๋‹จํ•˜๋Š” ์ค‘์š”ํ•œ ์žฃ๋Œ€๋กœ ๊ฒฐ๊ตญ '๊ธฐ์—…์ด ๋ˆ์„ ์ž˜ ๋ฒŒ๊ณ  ์žˆ๋Š”๊ฐ€?'ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ฌธ์ œ์ด๋‹ค. NGO[1]๊ฐ€ ์•„๋‹Œ ์ด์ƒ, ๋น„์ „๋„ ์ข‹๊ณ  ์ „๋žต๋„ ์ข‹์ง€๋งŒ ๊ฒฐ๊ตญ ์‚ฌ์—…์„ ์ž˜ ํ•ด์„œ ๋ˆ์„ ๋ฒ„๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋ฏ€๋กœ ๊ทธ ๋ชฉ์ ์— ์–ด๋Š ์ • brunch.co.kr/@flyingcity/56 ์ด ์žฅ์—์„œ๋Š” ๊ธฐ์—…์˜ ์ „๋ฐ˜์ ์ธ ์‹ค์ ์„ ํ‰๊ฐ€ํ•˜์—ฌ ํ•ด๋‹น ๊ธฐ์—…์˜ ์žฌ๋ฌด๊ฑด์ „์„ฑ์„ ๋‹ค๋ฃจ๋Š” ์žฌ๋ฌด๋น„์œจ ๋ถ„์„์„ ๋„˜์–ด์„œ์„œ ํŠน์ • ์‚ฌ์—…์ด๋‚˜ ํ”„๋กœ์ ํŠธ์˜ ์ˆ˜์ต์„ฑ์„ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ๊นŒ์ง€ ์‚ดํŽด๋ณด๊ณ ์ž ํ•œ๋‹ค. ์ˆ˜์ต์„ฑ ๋ถ„์„์„ ์ œ๋Œ€๋กœ ๋๋‚ด๋ฉด ๊ธฐ์—…์˜ ์žฌ๋ฌด๊ฑด์ „์„ฑ์€ ๋ฌผ๋ก ์ด๊ณ  ํ•ด๋‹น ์‚ฌ์—… ๋˜๋Š” ํ”„๋กœ์ ํŠธ์—์„œ ์–ด๋–ค ํ•ญ๋ชฉ ๋˜๋Š” ๋™์ธ๋“ค(drivers)์ด ๋น„์šฉ์„ ์œ ๋ฐœํ•˜๋Š”์ง€ ๋˜๋Š” ๊ฐ€์น˜๋ฅผ ์ฐฝ์ถœํ•˜๋Š”์ง€๋„ ์•Œ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ปจ์„คํ„ดํŠธ ์ž…์žฅ์—์„œ๋Š” ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์„ ์ฐพ์•„๋‚ด์„œ ๋น„์šฉ์„ ๋งŽ์ด ์œ ๋ฐœํ•˜๋Š” ํ•ญ๋ชฉ์€ ์—†์• ๊ฑฐ๋‚˜ ๋‚ฎ์€ ๋น„์šฉ ์š”์†Œ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์„ ์ œ์‹œํ•˜๊ณ , ๊ฐ€์น˜๋ฅผ ์ฐฝ์ถœํ•˜๋Š” ๋ถ€๋ถ„์€ ๋”์šฑ๋” ํฐ ๊ฐ€์น˜๋ฅผ ์ฐฝ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก ์ œ์•ˆํ•˜๋Š” ์ผ์ด๋‹ค. ์‚ฌ์‹ค ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…์˜ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. Figure III-38์„ ๋”ฐ๋ผ ์ง€๊ธˆ๋ถ€ํ„ฐ ์ƒ์„ธํžˆ ์‚ดํŽด๋ณด์ž. Figure III-38. ์ˆ˜์ต์„ฑ ๋ถ„์„ ๋กœ๋“œ๋งต 9.1 ๋น„์šฉ๊ตฌ์กฐ ๋ถ„์„ ๊ฐ ์‚ฐ์—…์˜ ํŠน์„ฑ์„ ๊ณ ๋ คํ•ด ๊ทธ ์‚ฐ์—… ๋‚ด ์›๊ฐ€์˜ ํšŒ๊ณ„์  ์ธ์‹์ด ๋‹ค๋ฅด๋“ฏ[1], ๊ทธ ์›๊ฐ€๋ฅผ ์‚ฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๋งค์šฐ ๋‹ค์–‘ํ•˜๋‹ค. ์›๊ฐ€์˜ 3 ์š”์†Œ๋ผ ๋ถˆ๋ฆฌ๋Š” ์žฌ๋ฃŒ๋น„, ๋…ธ๋ฌด๋น„, ๊ฒฝ๋น„๋ฅผ ์ทจ๊ธ‰ํ•˜๋Š” ์ฐจ์ด๋กœ ์ธํ•ด ๋‹ค์–‘ํ•œ ์›๊ฐ€๊ณ„์‚ฐ๋ฒ•์ด ํŒŒ์ƒ๋˜๋Š”๋ฐ ์ œํ’ˆ ์›๊ฐ€ ๊ณ„์‚ฐ(Product Costing)์„ ํ†ตํ•ด ์ด๋ฅผ ์•Œ์•„๋ณด์ž. ์ œํ’ˆ ์›๊ฐ€ โ€˜๊ณ„์ƒ(่จˆไธŠ)โ€™์€ โ€˜๊ธฐ์—…์ด ์ƒ์‚ฐํ•˜๋Š” ์žฌํ™”๋‚˜ ์šฉ์—ญ์˜ ์›๊ฐ€๋ฅผ ์ธก์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์›๊ฐ€ ์ž๋ฃŒ๋ฅผ ํ™•์ธ, ์ง‘๊ณ„, ๋ถ„๋ฅ˜ํ•˜์—ฌ ํŠน์ • ์›๊ฐ€ ๋‹จ์œ„์™€ ์—ฐ๊ด€์‹œํ‚ค๋Š” ๊ฒƒโ€™์„ ๋œปํ•œ๋‹ค. ์‚ฐ์—…์— ๋”ฐ๋ผ ์กฐ๊ธˆ์”ฉ ์ฐจ์ด๋Š” ์žˆ์œผ๋‚˜ ์ผ๋ฐ˜์ ์œผ๋กœ ์ œ์กฐ ์›๊ฐ€๋Š” Figure III-39์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋œ๋‹ค. Figure III-39. ์ œํ’ˆ ์›๊ฐ€์˜ ์‚ฐ์ • ์‚ฌ๋ก€[2] ์ฆ‰, ์ œํ’ˆ์˜ ์ƒ์‚ฐ๊ณผ ๊ด€๋ จ๋œ ์›๊ฐ€ ์š”์†Œ - ์ง์ ‘์žฌ๋ฃŒ๋น„, ์ง์ ‘๋…ธ๋ฌด๋น„, ์ง์ ‘๊ฒฝ๋น„ ๋“ฑ์„ ์ง์ ‘์›๊ฐ€๋กœ ๋ฐ˜์˜ํ•˜๊ณ , ๊ฐ„์ ‘์žฌ๋ฃŒ๋น„, ๊ฐ„์ ‘๋…ธ๋ฌด๋น„, ๊ฐ„์ ‘๊ฒฝ๋น„ ๋“ฑ์„ ์ œ์กฐ๊ฐ„์ ‘๋น„๋กœ ๋ฐ˜์˜ํ•œ๋‹ค. ๊ทธ๊ฒƒ์ด ์ œ์กฐ์›๊ฐ€๊ฐ€ ๋˜๋ฉฐ ๊ฑฐ๊ธฐ์— ํŒ๋งค๋น„์™€ ์ผ๋ฐ˜๊ด€๋ฆฌ๋น„ ๋“ฑ์„ ๋”ํ•˜๋ฉด ์ด ์›๊ฐ€๊ฐ€ ๋œ๋‹ค. ๊ธฐ์—…ํšŒ๊ณ„์—์„œ๋Š” ์ œ์กฐ์›๊ฐ€๋งŒ ์›๊ฐ€๋ผ ์นญํ•˜๊ณ  ํŒ๋งค๋น„์™€ ์ผ๋ฐ˜๊ด€๋ฆฌ๋น„, ์ง€๊ธ‰์ด์ž ๋“ฑ์€ ๋น„์šฉ์œผ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ โ€˜๋งค์ถœ ๊ณ„ํš - ๋ชฉํ‘œ์ด์ต = ํ—ˆ์šฉ ๋น„์šฉ ๋˜๋Š” ํ—ˆ์šฉ ์›๊ฐ€โ€™๋กœ ๊ณ„์‚ฐ๋˜๋Š” ๊ณ„ํš ์†์ต์„ ๊ฐœ์„ ํ•˜๋ ค๋ฉด ๋งค์ถœ์„ ํ™•๋Œ€ํ•˜๋˜์ง€ ํ—ˆ์šฉ ๋น„์šฉ(์ œ์กฐ์›๊ฐ€, ํŒ๋งค๊ด€๋ฆฌ๋น„, ์˜์—…์™ธ๋น„์šฉ ๋“ฑ)์„ ์ ˆ๊ฐํ•ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ ๊ธฐ๋ณธ์ ์ธ ์›๊ฐ€ ์‚ฐ์ • ๊ฐœ๋…์€ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ™•๋Œ€๋  ์ˆ˜ ์žˆ๋Š”๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์›๊ฐ€๋ฅผ ์ง‘๊ณ„ํ•˜๋Š” ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ๋ถ„ํ•œ๋‹ค. ๊ฐœ๋ณ„์›๊ฐ€ ๊ณ„์‚ฐ(Job-Order Costing) ์ข…ํ•ฉ์›๊ฐ€ ๊ณ„์‚ฐ(Process Costing) ๊ฐœ๋ณ„์›๊ฐ€ ๊ณ„์‚ฐ์€ ๊ฐ ์ž‘์—…๋ณ„๋กœ ์›๊ฐ€๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ณ ๊ฐ์ด ์š”๊ตฌํ•œ ์ž‘์—…์— ๋Œ€ํ•ด ์ˆœ์ฐจ์ ์œผ๋กœ ์ผ์ด ์ง„ํ–‰๋˜๋ฉด์„œ ์›๊ฐ€๊ฐ€ ๋”ํ•ด์ง€๋Š” ๋ฐฉ์‹์ด๋‹ค. ์กฐ์„ ์—…์ด๋‚˜ ๊ฑด์„ค์—… ๋“ฑ์—์„œ ๋งŽ์ด ์“ฐ์ด๋ฉฐ ํŠนํžˆ, ๊ฑด์„ค์—…์˜ ๊ฒฝ์šฐ ๊ณต์‚ฌ์˜ ์ง„์ฒ™๋„์— ๋”ฐ๋ฅธ ๊ณต์ •์„ ์‚ฐ์ถœํ•ด ํ˜„์žฌ๊นŒ์ง€ ์‹œ๊ณต๋œ ๋ถ€๋ถ„๋งŒํผ์˜ ์†Œ์š”์ž๊ธˆ์„ ์˜๋ฏธํ•˜๋Š” ๊ธฐ์„ฑ๊ณ ๋ฅผ ๊ณ ๋ คํ•œ๋‹ค. ์ข…ํ•ฉ์›๊ฐ€ ๊ณ„์‚ฐ์€ ์ผ์ • ํšŒ๊ณ„ ๊ธฐ๊ฐ„ ๋™์•ˆ ํŠน์ • ๊ณต์ •์—์„œ ๋ฐœ์ƒํ•œ ๋ชจ๋“  ์›๊ฐ€๋ฅผ ์ง‘๊ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ •์œ ๋‚˜ ํ™”ํ•™์•ฝํ’ˆ ์ƒ์‚ฐ์ฒ˜๋Ÿผ ์—ฐ์†๊ณต์ •์— ์˜ํ•ด ์ œํ’ˆ์„ ๋Œ€๋Ÿ‰์œผ๋กœ ์ƒ์‚ฐํ•˜๋Š” ์‚ฐ์—…์—์„œ ๋งŽ์ด ํ™œ์šฉํ•œ๋‹ค. ๋˜, ์ œํ’ˆ ์›๊ฐ€์— ๊ณ ์ • ์ œ์กฐ ๊ฐ„์ ‘ ์›๊ฐ€์˜ ํฌํ•จ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ๋ถ„ํ•œ๋‹ค ์ „๋ถ€์›๊ฐ€ ๊ณ„์‚ฐ(Absorption Costing) ์ง์ ‘์›๊ฐ€ ๊ณ„์‚ฐ(Direct Costing) ์ „๋ถ€์›๊ฐ€ ๊ณ„์‚ฐ์€ ์ „ํ†ต์ ์ธ ์›๊ฐ€๊ณ„์‚ฐ ๋ฐฉ์‹์œผ๋กœ ๊ธฐ์—…ํšŒ๊ณ„ ๊ธฐ์ค€์— ๋”ฐ๋ผ ์ง์ ‘๋น„์™€ ๊ฐ„์ ‘๋น„๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฐฐ๋ถ€ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์™ธ๋ถ€ ๊ณต์‹œ๋ฅผ ์œ„ํ•œ ์žฌ๋ฌดํšŒ๊ณ„์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ‘œ์ค€์ ์ธ ์›๊ฐ€ ์‚ฐ์ • ๊ธฐ๋ฒ•์ด๋ฉฐ, ์ง์ ‘์›๊ฐ€ ๊ณ„์‚ฐ์€ ๋ณ€๋™๋น„๋Š” ์ œํ’ˆ ์›๊ฐ€๋กœ ๊ณ„์ƒํ•˜์ง€๋งŒ ์ œํ’ˆ ์ƒ์‚ฐ๊ณผ ๋ฌด๊ด€ํ•œ ๊ณ ์ •๋น„๋Š” ์ œํ’ˆ์— ๋ถ€๊ณผํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ๊ธฐ๊ฐ„ ์›๊ฐ€์— ์ผ๊ด„ ์ง‘๊ณ„ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์›๊ฐ€ ์ธก์ • ๊ธฐ์ค€์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ๋ถ„ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์‹ค์ œ์›๊ฐ€ ๊ณ„์‚ฐ(Actual Costing) ํ‘œ์ค€์›๊ฐ€ ๊ณ„์‚ฐ(Standard Costing) ํ‰์ค€ ํ™”์› ๊ฐ€ ๊ณ„์‚ฐ(Normal Costing) ์‹ค์ œ ์›๊ฐ€๊ณ„์‚ฐ์€ ์‹ค์ œ ์ œํ’ˆ ๊ตฌ์ž… ๊ฐ€๊ฒฉ ๋ฐ ์ˆ˜๋Ÿ‰์„ ๊ธฐ์ค€์œผ๋กœ ์ œํ’ˆ์˜ ์›๊ฐ€๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์„ ๋งํ•˜๋ฉฐ, ํ‘œ์ค€์›๊ฐ€ ๊ณ„์‚ฐ์€ ์‚ฌ์ „์— ์ •ํ•ด์ง„ ํ‘œ์ค€๊ฐ€๊ฒฉ ๋ฐ ํ‘œ์ค€์ˆ˜๋Ÿ‰์„ ๋ฐ”ํƒ•์œผ๋กœ ์ œํ’ˆ์˜ ์›๊ฐ€๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ๋˜ํ•œ, ํ‰์ค€ ํ™”์› ๊ฐ€ ๊ณ„์‚ฐ์€ ์ง์ ‘์žฌ๋ฃŒ ์›๊ฐ€์™€ ์ง์ ‘ ๋…ธ๋ฌด์› ๊ฐ€๋Š” ์‹ค์ œ์›๊ฐ€๋ฅผ ์ ์šฉํ•˜๊ณ , ์ œ์กฐ ๊ฐ„์ ‘ ์›๊ฐ€๋Š” ์„ค์ •๋œ ์˜ˆ์ • ๋ฐฐ๋ถ€์œจ์— ๋”ฐ๋ผ ๊ฒฐ์ •๋œ ์›๊ฐ€๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ๋”ฐ๋ผ์„œ ์–ด๋–ค ๋ฐฉ์‹์˜ ์›๊ฐ€๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ์žฌ๊ณ ์ž์‚ฐ์˜ ํ‰๊ฐ€๋‚˜ ๊ธฐ๊ฐ„ ์†์ต์ด ๋‹ฌ๋ผ์ง„๋‹ค. ๋˜ํ•œ, ์›๊ฐ€๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋”๋ผ๋„ ์‹ค์ œ ์›๊ฐ€๊ฐ€ ๋ฐœ์ƒ๋˜๋ฉด ์ฐจ์ด๊ฐ€ ์ƒ๊ธฐ๋Š”๋ฐ ๊ณ„ํš ๋Œ€๋น„ ์‹ค์  ๋งค์ถœ์˜ ์ฆ๊ฐ์— ์˜ํ•ด ๋˜๋Š” ๊ณ„ํš ๋Œ€๋น„ ์‹ค์ œ ํˆฌ์ž…์›๊ฐ€์˜ ์ฐจ์ด์— ์˜ํ•ด ์†์ต์— ๋ณ€๋™์ด ์ƒ๊ธด๋‹ค. ํšŒ๊ณ„์ ์œผ๋กœ ์ด๋Š” ์ •ํ•ฉ์„ฑ์„ ๊ฐ€์ ธ์•ผ ํ•˜๋ฏ€๋กœ ์ง€์†์ ์œผ๋กœ ํ†ต์ œ ๋ฐ ๊ด€๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๊ฑด์„ค์ด๋‚˜ ์‹œ์Šคํ…œ ๊ตฌ์ถ•๊ณผ ๊ฐ™์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ธฐ์—…๋“ค์˜ ๊ฒฝ์šฐ, ๊ณ ์œ ์˜ ์›๊ฐ€ ์ด์ต๋ชจ๋ธ(Cost& Price Model)์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ํ”„๋กœ์ ํŠธ PM์€ ๊ฒฝ์˜์ง„์ด ์Šน์ธํ•œ ๋ชฉํ‘œ ์žฌ๋ฌด์ˆ˜์น˜๋ฅผ ํ”„๋กœ์ ํŠธ ๊ธฐ๊ฐ„ ๋™์•ˆ ๋ณ€๋™์„ ์ตœ์†Œํ™”ํ•˜์—ฌ ์ž˜ ๊ด€๋ฆฌํ•˜๊ณ  ๋ชฉํ‘œ๋ฅผ ์ƒํšŒํ•  ๊ฒฝ์šฐ, ์ด์ต์˜ ์ผ๋ถ€๋ฅผ ์ธ์„ผํ‹ฐ๋ธŒ๋กœ ๋ฐ›๊ธฐ๋„ ํ•œ๋‹ค. ๋˜ํ•œ, 2000๋…„๋Œ€ ์ดˆ ์ „๋žต์  ๊ฒฝ์˜ ๊ด€๋ฆฌ ์ฆ‰, ๊ท ํ˜•์„ฑ๊ณผ ์ง€ํ‘œ(Balanced Scorecard. BSC)์™€ ๋”๋ถˆ์–ด ์ „ํ†ต์ ์ธ ์›๊ฐ€ ์‚ฐ์ • ๋ชจ๋ธ ๋ฌธ์ œ์ ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ํ™œ๋™๊ธฐ์ค€์›๊ฐ€ ๊ณ„์‚ฐ(Activity-Based Costing: ABC)์ด ์ฃผ์ฐฝ๋˜์—ˆ๋‹ค. ์ „ํ†ต์ ์ธ ์›๊ฐ€๊ณ„์‚ฐ์˜ ๋ฌธ์ œ์ ์ด๋ž€ ์ œ์กฐ๊ฐ„์ ‘๋น„ ๋“ฑ ๊ฐœ๋ณ„ ์ œํ’ˆ์˜ ์›๊ฐ€ ๋ฐฐ๋ถ€์— ์žˆ์–ด์„œ ์ „ํ†ต์ ์ธ ์›๊ฐ€๊ณ„์‚ฐ์ด ๋…ธ๋™์‹œ๊ฐ„ ๋˜๋Š” ๊ธฐ๊ณ„ ์‹œ๊ฐ„ ๋“ฑ ํ•œ ๊ฐ€์ง€ ์š”์ธ์„ ์ •ํ•˜๊ณ  ๊ทธ ์š”์ธ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ„์ ‘๋น„๋ฅผ ๋ฐฐ๋ถ€ํ•˜์—ฌ ์›๊ฐ€ ํ‰์ค€ํ™” ํ˜„์ƒ์„ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋˜์—ˆ๊ณ  ์ด๋Š” ์›๊ฐ€ ์™œ๊ณก์œผ๋กœ ์ด์–ด์ ธ ABC๋ฅผ ํ†ตํ•ด ํˆฌ์ž…์ž์›์ด ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋กœ ๋ณ€ํ™˜๋˜๋Š” ๊ณผ์ •์„ ๋ช…ํ™•ํžˆ ๋ฐํ˜€ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค์˜ ์›๊ฐ€๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ ์ž ํ•˜์˜€๋‹ค. ์ด๋Š” ์‹ค์ œ ์ œ๋Œ€๋กœ ๊ตฌ์ถ•๋  ๊ฒฝ์šฐ ๊ฒฝ์˜์ „๋žต ๊ด€์ ์—์„œ ๋งŽ์€ ์‹œ์‚ฌ์ ์„ ์–ป์„ ์ˆ˜ ์žˆ์œผ๋‚˜ ์˜ค๋Š˜๋‚  ์ œ๋Œ€๋กœ ์ ์šฉํ•˜๋Š” ๊ธฐ์—…์€ ์—†๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์•ž์„œ ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—…๋ถ„์„์—์„œ ์‚ดํŽด๋ณด์•˜๋˜ Value Chain ๋ถ„์„๋„ ABC๊ฐ€ ์ œ๋Œ€๋กœ ๊ตฌ์ถ•๋˜์–ด ์žˆ๋‹ค๋ฉด ํ›จ์”ฌ ๋” ๋งŽ์€ ํ†ต์ฐฐ๋ ฅ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. Figure III-40. ์†์ต๊ณ„์‚ฐ์„œ ์ „ํ™˜ ์‚ฌ๋ก€ ๊ทธ๋Ÿฌ๋‚˜ ์‚ดํŽด๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ์›๊ฐ€๋ฅผ ์‚ฐ์ •ํ•˜๋Š” ๊ฒƒ์€ ์‰ฌ์šด ์ž‘์—…์ด ์•„๋‹ˆ๋‹ค. ๋”๊ตฐ๋‹ค๋‚˜ ์™œ ์ด๋ ‡๊ฒŒ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด์•ผ๋งŒ ํ• ๊นŒ? โ€˜์ปจ์„คํ„ดํŠธ๊ฐ€ ์ด๋Ÿฐ ๊ฒƒ๊นŒ์ง€ ์•Œ์•„์•ผ ํ•˜๋‚˜?โ€™๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํšŒ๊ณ„์‚ฌ ์ˆ˜์ค€๊นŒ์ง€๋Š” ์•„๋‹ˆ๊ฒ ์ง€๋งŒ โ€˜๋ฐ˜๋“œ์‹œ ์•Œ์•„์•ผ ํ•œ๋‹คโ€™. ๊ทธ๋ž˜์•ผ ์ˆซ์ž๋กœ ํ”ผ๋“œ๋ฐฑ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์‚ฌ์—…์ด๋‚˜ ํ”„๋กœ์ ํŠธ์˜ ๊ฐœ์„ ์ ์„ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๊ณ , ์ œํ’ˆ ๊ฒฝ์Ÿ๋ ฅ ๊ฐ•ํ™” ์ฐจ์›์—์„œ๋„ ์•„์ด๋””์–ด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Figure III-40์—์„œ ํ‘œํ˜„ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์ „๋ถ€์›๊ฐ€ ๊ณ„์‚ฐ๋ฒ•์—์„œ๋Š” ์ž˜ ๋ณด์ด์ง€ ์•Š๋˜ ๊ฒƒ์ด ์ง์ ‘์›๊ฐ€ ๊ณ„์‚ฐ๋ฒ•์„ ์ ์šฉํ•˜๋ฉด ๋ณ€๋™๋น„์™€ ๊ณ ์ •๋น„๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋ณ€๋™ ๋น„์œจ์ด ๋‚ฎ์œผ๋ฉด ๊ณตํ—Œ์ด์ต๋ฅ ์ด ๋†’๋‹ค. ์ฆ‰, ์†์ต๋ถ„๊ธฐ์  ๋งค์ถœ์•ก์ด ๋‚ฎ์•„์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ด์ต์„ ๋‚ด๊ธฐ๊ฐ€ ์‰ฝ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ์›๊ฐ€๋ฅผ ์ข€ ์‰ฝ๊ฒŒ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ? ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค๋„ ๋งŽ๊ฒ ์ง€๋งŒ ์ €์ž๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ํ•œ ๊ฐ€์ง€ ์†Œ๊ฐœํ•˜๋ฉด โ€˜์›๊ฐ€์œจโ€™์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์›๊ฐ€์œจ์€ ๋งค์ถœ์›๊ฐ€๋ฅผ ๋งค์ถœ์•ก์œผ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์ธ๋ฐ ์ „๋…„๋„ ์†์ต๊ณ„์‚ฐ์„œ๋ฅผ ์ด์šฉํ•ด์„œ ์›๊ฐ€์œจ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ „๋…„๋„ ๋งค์ถœ์›๊ฐ€๊ฐ€ 700์–ต์ด๊ณ  ๋งค์ถœ์•ก์ด 1,000์–ต์ด๋ผ๋ฉด ์›๊ฐ€์œจ์€ 70%์ด๋‹ค. ์ด ์›๊ฐ€์œจ์„ ์ „๋…„๋„ ํŒ๋งคํ•œ ์ œํ’ˆ ๊ฐ€๊ฒฉ์— ๊ณฑํ•ด์ฃผ๋ฉด ์ œํ’ˆ๋ณ„ ์›๊ฐ€๋ฅผ ์‚ฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก  ์ด ๊ณ„์‚ฐ์€ ์ œํ’ˆ๋ณ„๋กœ ์›๊ฐ€์œจ์ด ๊ฐ™๋‹ค๋Š” ์ „์ œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ทœ๋ชจ๊ฐ€ ํฐ ๋Œ€๊ธฐ์—…์˜ ๊ฒฝ์šฐ ๋‹ค์–‘ํ•œ ์ œํ’ˆ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ์ด ๊ฒฝ์šฐ ํŠน๋ณ„ํ•œ ์ •์ฑ…์ด ์—†๋‹ค๋ฉด ์›๊ฐ€์œจ์€ ์ƒ์ดํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ์— ์ด ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. [1] ๊ด€๋ฆฌํšŒ๊ณ„๋Š” ์‚ฐ์—…์ด๋‚˜ ๊ธฐ์—…์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค [2] ํŒ๋งค ์ด์ต์€ ์ด ์›๊ฐ€์˜ 10% ๊ฐ€์ • 9.2 ์†์ต๋ถ„๊ธฐ์  ๋ถ„์„ ์†์ต๋ถ„๊ธฐ์ (Break Even Point: BEP)์€ ๊ธฐ์—… ํšŒ๊ณ„์—์„œ ์ˆ˜์ต๊ณผ ๋น„์šฉ์ด ์ผ์น˜ํ•˜๋Š” ์ ์œผ๋กœ ์ด์ต๋„ ์†์‹ค๋„ ์ƒ๊ธฐ์ง€ ์•Š๋Š” ๋งค์ถœ์•ก ๋˜๋Š” ๋งค์ถœ๋Ÿ‰์„ ์˜๋ฏธํ•œ๋‹ค. ์ด๋Š” ๊ฒฝ์˜ ๊ด€์ ์—์„œ ๋งค์šฐ ๋งŽ์€ ํ†ต์ฐฐ๋ ฅ์„ ์ œ๊ณตํ•˜๋Š”๋ฐ ์ง์ ‘์›๊ฐ€ ๊ณ„์‚ฐ๋ฒ•์„ ์ƒ๊ธฐํ•˜๋ฉด์„œ ์†์ต๋ถ„๊ธฐ์ ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. Figure III-41์€ ์†์ต๋ถ„๊ธฐ์ ์˜ ๊ฐœ๋…์„ ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋‹ค. Figure III-41. ์†์ต๋ถ„๊ธฐ์ ์˜ ๊ฐœ๋… ๋งค์ถœ์•ก - ๋ณ€๋™๋น„ = ๊ณตํ—Œ์ด์ต โ€ฆ (1) ๊ณตํ—Œ์ด์ต - ๊ณ ์ •๋น„ = ์˜์—…์ด์ต โ€ฆ (2) ๋งค์ถœ์•ก = ๊ณตํ—Œ์ด์ต + ๋ณ€๋™๋น„ = (๊ณ ์ •๋น„ + ์˜์—…์ด์ต) + ๋ณ€๋™๋น„ โ€ฆ (3) ๋งค์ถœ์•ก - (๋ณ€๋™๋น„+๊ณ ์ •๋น„) = ์˜์—…์ด์ต โ€ฆ (4) ๋˜ํ•œ, ๊ณตํ—Œ์ด์ต์€ ๋งค์ถœ์•ก์—์„œ ๋ณ€๋™๋น„๋ฅผ ๋บ€ ๊ธˆ์•ก์œผ๋กœ ๊ณ ์ •๋น„ ํšŒ์ˆ˜์™€ ์ด์ต ํš๋“์— ๊ณตํ—Œํ•˜๋Š” ์ด์ต์„ ๋งํ•˜๋Š”๋ฐ ๋ณ€๋™ ๋น„์œจ์ด ๋‚ฎ์œผ๋ฉด ๊ณตํ—Œ์ด์ต๋ฅ ์ด ๋†’๊ณ , ๋ฐ˜๋Œ€๋กœ ๋ณ€๋™ ๋น„์œจ์ด ๋†’์œผ๋ฉด ๊ณตํ—Œ์ด์ต๋ฅ ์ด ๋‚ฎ๋‹ค. Figure III-42. ๊ณตํ—Œ์ด์ต๊ณผ ๋ณ€๋™ ๋น„์œจ ์†์ต๋ถ„๊ธฐ์ ๊ณผ ๊ณตํ—Œ์ด์ต ๋ถ„์„์€ ์‚ฌ์—… ์ถ”์ง„ ๋ฐ ๊ฒฝ์˜ ํ™œ๋™์— ๋งŽ์€ ํ†ต์ฐฐ๋ ฅ์„ ์ค€๋‹ค. ๋งˆ์ผ€ํŒ… ๊ด€์ ์—์„œ๋Š” ์ œํ’ˆ ์›๊ฐ€ ์‚ฐ์ •์— ๋Œ€ํ•œ ๊ณ ๋ฏผ, ์ƒ์‚ฐ์˜ ์•„์›ƒ์†Œ์‹ฑ, ๋ชฉํ‘œ์ด์ต ํš๋“์„ ์œ„ํ•œ ๋งค์ถœ ๋ชฉํ‘œ ์‚ฐ์ • ๋“ฑ์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Figure III-42๋Š” ๊ณ ์ •๋น„์— ๋Œ€ํ•œ ์–ธ๊ธ‰์ด ์—†์ง€๋งŒ ๊ณ ์ •๋น„์™€ ๋ณ€๋™๋น„๋ฅผ ์ค„์ด๋Š” ๋…ธ๋ ฅ์„ ํ•จ๊ป˜ ํ•ด์•ผ ํ•œ๋‹ค. ๊ณ ์ •๋น„ ์ด์•ก์„ ๋‚ฎ์ถ”๊ธฐ ์œ„ํ•ด ๊ณ ์ •๋น„ ์ฆ๊ฐ€๋ฅผ ์–ต์ œํ•˜๊ฑฐ๋‚˜ ์™ธ์ฃผ๋ถ„์„ ์ค„์ผ ์ˆ˜๋„ ์žˆ๋‹ค. Table III-24๋Š” ์†์ต๋ถ„๊ธฐ์  ๋ถ„์„์˜ ์žฅ๋‹จ์ ์„ ์š”์•ฝํ•œ ๊ฒƒ์ด๋‹ค. Table III-24. ์†์ต๋ถ„๊ธฐ์  ๋ถ„์„์˜ ์žฅ๋‹จ์  09. ์ˆ˜์ต์„ฑ ๋ถ„์„(2/2) 9.3 ์‚ฌ์—… ์ˆ˜์ต์„ฑ ๋ถ„์„(Business Unit Profitability) ์‚ฌ์—… ์ˆ˜์ต์„ฑ ๋ถ„์„์€ ๊ธฐ์—…์˜ ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ์ค‘ ํŠน์ • ์‚ฌ์—…์ด๋‚˜ ์„ธ๊ทธ๋จผํŠธ(segments)์˜ ์ˆ˜์ต์„ฑ์„ ๋ถ„์„ํ•˜์—ฌ ๊ธฐ์—…์˜ ์‚ฌ์—…๋ณ„ ๋˜๋Š” ๊ฒฝ์Ÿ์‚ฌ์˜ ์‚ฌ์—…๊ณผ ๋น„๊ตํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค. ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์€ ์˜์—…์ด์ต๋ฅ (ROS. Return On Sales)๊ณผ ์ž๋ณธ์ด์ต๋ฅ (ROCE.Return On Capital Employed)์ด๋‹ค. ํŠน์ • ์‚ฌ์—…์˜ ์˜์—…์ด์ต๋ฅ ์€ ํ•ด๋‹น ์‚ฌ์—… ๋ณธ์—ฐ์˜ ํ™œ๋™์„ ํ†ตํ•ด ๋ฒŒ์–ด๋“ค์ธ ๋งค์ถœ์•ก์— ๋Œ€ํ•ด ์ˆœ์›๊ฐ€๋ฅผ ์ œํ•œ ์ด์ต์˜ ๋น„์œจ์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์œผ๋กœ ROS๊ฐ€ ๋†“์„์ˆ˜๋ก ์‚ฌ์—…์˜ ์ˆ˜์ต์„ฑ์ด ์šฐ์ˆ˜ํ•œ ๊ฒƒ์ด๋‹ค. ์ž๋ณธ ์ˆ˜์ต๋ฅ (ROCE)[4]๋Š” ํ•ด๋‹น ์‚ฌ์—…์— ํˆฌ์ž…๋œ ์ด ์ž๋ณธ์— ๋Œ€ํ•ด ์–ผ๋งˆ๋‚˜ ์ˆ˜์ต์„ ์–ป์—ˆ๋Š๋ƒ๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ด ์ž๋ณธ์— ๋Œ€ํ•œ ์ด์ž ๋ฐ ์„ธ์ „์ด์ต์˜ ๋น„์œจ์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. EBIT: Earnings Before Interests and Taxes ์ด์ž ๋ฐ ์„ธ์ „์ด์ต. ์ด ์ž๋ณธ: shareholders funds +debt Figure III-43. ์‚ฌ์—… ์ˆ˜์ต์„ฑ ๋ถ„์„ ์‚ฌ๋ก€ ๋ณด๊ณ ์„œ์˜ ํ‘œํ˜„์€ ๋ณดํ†ต Figure III-47๊ณผ ๊ฐ™์ด ๊ธฐ์—…์˜ ์‚ฌ์—… ๋‚ด์šฉ์„ ๊ฐ ์‚ฌ์—… ๋‹จ์œ„ ๋˜๋Š” ์„ธ๊ทธ๋จผํŠธ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ (๋ณดํ†ต ๊ณ„๋‹จ์‹ ์ฐจํŠธ๋กœ ํ‘œํ˜„) ๊ฐ ์‚ฌ์—… ๋‹จ์œ„ ๋˜๋Š” ์„ธ๊ทธ๋จผํŠธ์˜ ROS์™€ ROCE๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ์ฐจํŠธ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. ์ œ์กฐ ์„ค๋น„๊ฐ€ ํฌ๊ฒŒ ํ•„์š”ํ•˜์ง€ ์•Š์€ ์„œ๋น„์Šค ์‚ฐ์—…์˜ ๊ฒฝ์šฐ, ์ž๋ณธ ์ˆ˜์ต๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ธฐ๋ณด๋‹ค๋Š” ๋งค์ถœ(Sales)๊ณผ ์˜์—…์ด์ต๋ฅ (ROS)์„ ๊ฐ™์ด ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•œ๋‹ค. TableIII-25๋Š” ์‚ฌ์—… ์ˆ˜์ต์„ฑ ๋ถ„์„์˜ ์žฅ๋‹จ์ ์„ ์š”์•ฝํ•œ ๊ฒƒ์ด๋‹ค. Table III-25. ์‚ฌ์—… ์ˆ˜์ต์„ฑ ๋ถ„์„์˜ ์žฅ๋‹จ์  9.4 ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ(Economies of Scale) ์ผ๋ฐ˜์ ์œผ๋กœ ๊ธฐ์—…์ด ์žฌํ™”์™€ ์„œ๋น„์Šค์˜ ์ƒ์‚ฐ์„ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด ๊ทธ์— ํ•„์š”ํ•œ ํ‰๊ท  ๋น„์šฉ[1]๋„ ๋น„๋ก€ํ•˜์—ฌ ์ฆ๊ฐ€ํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ฒ ๋กœ ๊ตฌ์ถ• ๋“ฑ ๋Œ€๊ทœ๋ชจ ์ธํ”„๋ผ ๊ตฌ์ถ•์ด ํ•„์š”ํ•œ ์ฒ ๋„ ์‚ฌ์—…์ด๋‚˜ ํ†ต์‹ ์ด๋‚˜ ์ž๋™์ฐจ ์‚ฌ์—…๊ฐ™์ด ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค ์ƒ์‚ฐ์„ ์œ„ํ•œ ๋Œ€๊ทœ๋ชจ ์„ค๋น„ ๊ตฌ์ถ•์ด ํ•„์š”ํ•œ ์‚ฐ์—…์˜ ๊ฒฝ์šฐ๋Š” ์ƒ์‚ฐ๋Ÿ‰์„ ๋Š˜๋ฆด์ˆ˜๋ก ํ‰๊ท  ๋น„์šฉ์ด ํ•˜๋ฝํ•˜๋Š” ํ˜„์ƒ์ด ๋‚˜ํƒ€๋‚œ๋‹ค. ์ด๋Ÿฐ ํšจ๊ณผ๋ฅผ '๊ทœ๋ชจ์˜ ๊ฒฝ์ œ(Economies of Scale) ํšจ๊ณผ'๋ผ๊ณ  ํ•œ๋‹ค. Figure III-44๋Š” ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ ๊ฐœ๋…์„ ๊ทธ๋ž˜ํ”„๋กœ ๋ณด์—ฌ์ค€๋‹ค. Figure III-44. ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ ๊ฐœ๋… ์‹ค์ œ๋กœ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ์˜ ๊ฒฝ์šฐ์ด๋‹ค. ์—ฐ๊ตฌ๊ฐœ๋ฐœ, ์ƒ์‚ฐ์„ค๋น„ ๊ตฌ์ถ• ๋“ฑ ์ดˆ๊ธฐ ๋น„์šฉ ๋Œ€๋น„ ์ด์ต์˜ ๊ทน๋Œ€ํ™” ๋Œ€๋Ÿ‰ ์ƒ์‚ฐ์„ ์œ„ํ•œ ์›์ž์žฌ ๋Œ€๋Ÿ‰ ๊ตฌ์ž…์œผ๋กœ ์žฌ๋ฃŒ๋น„ ์ ˆ๊ฐ ๋ฐ ๋ฌผ๋ฅ˜๋น„ ๊ฐ์ถ• ๋ถ„์—…์— ๋”ฐ๋ฅธ ์ƒ์‚ฐ ์š”์†Œ์˜ ์ „๋ฌธํ™” ์ด๋ฅผ ํ†ตํ•ด ์กฐ๊ธฐ์— ์‹œ์žฅ์„ ์žฅ์•…ํ•˜๋Š” ์ž์—ฐ๋…์  ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜๊ธฐ๋„ ํ•œ๋‹ค[2]. ๊ทธ๋Ÿฌ๋‚˜ ์ผ์ • ์ˆ˜์ค€ ์ด์ƒ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๋ฉด ์กฐ์ง ์šด์˜๋น„๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ณ  ๊ธฐ์—… ๊ตฌ์กฐ๊ฐ€ ๊ฒฝ์ง๋˜๋Š” ๋“ฑ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ ํšจ๊ณผ๊ฐ€ ์ƒ์‡„๋œ๋‹ค. ์ด๋ฅผ โ€˜X-๋น„ํšจ์œจ์„ฑโ€™์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. 20์„ธ๊ธฐ ์‚ฐ์—… ์‚ฌํšŒ์—์„œ๋Š” ๊ธฐ์—… ํ™œ๋™์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ๋˜ ๊ฒƒ์€ ์ž๋ณธ, ์›์ž์žฌ, ๋…ธ๋™๋ ฅ์œผ๋กœ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ์ƒ์‚ฐ์„ฑ(Productivity)๊ณผ ํšจ์œจ(Efficiency) ์ด์—ˆ๋‹ค. ์ฆ‰, ์ตœ๋Œ€ ์ƒ์‚ฐ๊ณผ ์ตœ๋Œ€ ์†Œ๋น„๊ฐ€ ๊ฒฝ์ œ ๊ด€์ ์—์„œ๋Š” ์ง€์ƒ ๋ชฉํ‘œ์˜€๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์–ด๋Š ์ˆœ๊ฐ„ ๊ณต๊ธ‰์ด ์ˆ˜์š”๋ฅผ ์ดˆ๊ณผํ•ด๋ฒ„๋ ธ๊ณ  ์ตœ๋Œ€ ์ƒ์‚ฐ์€ ๊ณผ์ž‰ ์ƒ์‚ฐ์ด ๋˜์–ด ๋‚ญ๋น„๊ฐ€ ๋˜๊ณ , ์ตœ๋Œ€ ์†Œ๋น„๋Š” ๊ฐ€๊ณ„ ๋ถ€์ฑ„์˜ ์ฆ๊ฐ€๋กœ ์ด์–ด์กŒ๋‹ค. ์ด๋ฅผ ํƒ€๊ฐœํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ƒ์‚ฐ๊ณผ ์†Œ๋น„์˜ ์ ์ • ๊ทœ๋ชจ ์œ ์ง€, ์ฆ‰ โ€˜์ตœ์  ์ƒ์‚ฐ๊ณผ ์ตœ์  ์†Œ๋น„(๋˜๋Š” ์ตœ์†Œ ์ƒ์‚ฐ๊ณผ ์ตœ์†Œ ์†Œ๋น„)โ€™์ด ์ค‘์š”ํ•ด์กŒ๊ณ , ์ด๋ฏธ ์ธํ„ฐ๋„ท์„ ํ†ตํ•ด ์—ฐ๊ฒฐ๋œ ๊ฑฐ๋Œ€ํ•œ ๊ธ€๋กœ๋ฒŒ ์‹œ์žฅ์— ํšจ๊ณผ์ ์œผ๋กœ ๋Œ€์‘ํ•˜๊ธฐ ์œ„ํ•ด ๋ฌผ๋Ÿ‰์„ ๋™์‹œ์— ๊ณต๊ธ‰ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–ค ๋Œ€๊ธฐ์—…๋„ ๊ฐ๋‹นํ•˜๊ธฐ ์‰ฝ์ง€ ์•Š์€ ์ผ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ˜ธ ๊ฐ„์— ์—ฐ๊ณ„๋œ ์„ธ๊ณ„ ๊ฒฝ์ œ๋Š” ์ด๋ฅผ ํ†ต์ œํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณดํ™”์— ํฐ ๊ด€์‹ฌ์„ ๊ฐ€์งˆ ์ˆ˜๋ฐ–์— ์—†๊ฒŒ ๋˜์—ˆ๋‹ค. Figure III-45 ์‚ฌ๋ก€๋ฅผ ๋ณด๋ฉด ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ ํšจ๊ณผ๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ 1์„ 2๋ฐฐ๋กœ ํ•˜์—ฌ๋„ ๋น„์šฉ์€ ํฌ๊ฒŒ ์ค„์–ด๋“ค์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, '์˜๋ฏธ ์žˆ๋Š” ๊ทœ๋ชจ๋ฅผ ํ™•๋ณดํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์  ์ƒ์‚ฐ ๋ถ€๋ถ„์ด ์กด์žฌํ•œ๋‹ค.'๋Š” ๊ฒƒ์ด๋‹ค. Figure III-45. ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ ๋ถ„์„ ์‚ฌ๋ก€ ๋˜ํ•œ, ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ์™€ ๊ฐ™์ด ์ƒ๊ฐํ•ด ๋ณผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐœ๋…๋“ค์ด ์žˆ๋‹ค. ๊ทœ๋ชจ์˜ ๋น„๊ฒฝ์ œ(Diseconomies of Scale). ์–ด๋–ค ์‚ฐ์—…์€ ์†Œ๊ทœ๋ชจ๋กœ ์ƒ์‚ฐํ•˜๋Š” ๊ฒƒ์ด ๋” ์œ ๋ฆฌํ•  ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ ์žฅ์ธ๋“ค์ด ์ƒ์‚ฐํ•˜๋Š” ์ˆ˜๊ณต์—…ํ’ˆ๋“ค์ด ๊ฑฐ์˜ ๊ทธ๋ ‡๋‹ค. ๋ช…ํ’ˆ์„ ๋Œ€๋Ÿ‰์œผ๋กœ ์ƒ์‚ฐํ•˜๊ฒŒ ๋˜๋ฉด ๋น„์šฉ์ด ๋” ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. (์ด ๊ฒฝ์šฐ, ๋งค์Šคํ‹ฐ์ง€(masstige) ์ƒํ’ˆ๋“ค๊ณผ๋Š” ๊ตฌ๋ณ„ํ•ด์•ผ ํ•œ๋‹ค) ๋ฒ”์œ„์˜ ๊ฒฝ์ œ(Economies of Scope). ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ์™€ ๋น„์Šทํ•  ๊ฒƒ ๊ฐ™์ง€๋งŒ ์ „ํ˜€ ๋‹ค๋ฅธ ์–˜๊ธฐ๋กœ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ๋Š” ํ•˜๋‚˜์˜ ์žฌํ™”๋ฅผ ์ƒ์‚ฐํ•  ๋•Œ ์ด์•ผ๊ธฐ์ด๊ณ  ๋ฒ”์œ„์˜ ๊ฒฝ์ œ๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ์žฌํ™”๋ฅผ ๊ฐ๊ฐ ๋‹ค๋ฅธ ์ƒ์‚ฐ์ž๊ฐ€ ์ƒ์‚ฐํ•  ๋•Œ๋ณด๋‹ค ํ•˜๋‚˜์˜ ์ƒ์‚ฐ์ž๊ฐ€ ์ƒ์‚ฐํ•  ๋•Œ ๋น„์šฉ์ด ๊ฐ์†Œํ•œ๋‹ค๋Š” ๊ฐœ๋…์œผ๋กœ ์ˆ˜์ง ๊ณ„์—ดํ™”(Vertical Integration)๋ฅผ ํƒํ•˜๋Š” ๋™๊ธฐ๊ฐ€ ๋œ๋‹ค. ๊ฒฝํ—˜ ๊ณก์„ (Experience Curve)๊ณผ๋„ ๊ตฌ๋ถ„๋˜๋Š”๋ฐ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ๋Š” ์ ˆ๋Œ€ ์ƒ์‚ฐ๋Ÿ‰์ด๋ฉฐ ๊ฒฝํ—˜ ๊ณก์„ ์€ ๋ˆ„์  ์ƒ์‚ฐ๋Ÿ‰์ด๋‹ค. Table III-26์€ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ ๋ถ„์„์˜ ์žฅ๋‹จ์ ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. Table III-26์€ ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ ๋ถ„์„์˜ ์žฅ๋‹จ์  9.5 ๊ฒฝํ—˜ ๊ณก์„  (Experience Curve) ๊ฒฝํ—˜ ๊ณก์„  ํšจ๊ณผ๋Š” 1960๋…„์— BCG ์ปจ์„คํ„ดํŠธ์˜€๋˜ Bruce Hendersen์— ์˜ํ•ด ์ฒ˜์Œ ์‚ฌ์šฉ๋˜์—ˆ๊ณ  ํ•œ๋‹ค. ๋‹ค๋ฅธ ์šฉ์–ด๋กœ โ€˜ํ•™์Šต ๊ณก์„ (Learning Curve)โ€™๋ผ๊ณ ๋„ ์•Œ๋ ค์ ธ ์žˆ๋Š”๋ฐ Hednersen์€ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค์˜ ๋ˆ„์ ๋Ÿ‰๊ณผ ๊ธฐ์—…์˜ ๋น„์šฉ ์‚ฌ์ด์— ์ผ๊ด€๋œ ๊ด€๊ณ„๊ฐ€ ์žˆ์Œ์„ ๋ฐœ๊ฒฌํ•˜๊ณ  ์ด๋ฅผ ๋ถ„์„ํ•ด ๋ณธ ๊ฒฐ๊ณผ, ์‹œ๊ฐ„์ด ์ง€๋‚จ์— ๋”ฐ๋ผ ๊ธฐ์—…์—์„œ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ์ƒ์‚ฐํ•  ๋•Œ ๋” ๋น ๋ฅด๊ณ  ์ข‹์•„์ง„๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•˜์˜€๋‹ค. ์ฆ‰, ๋ˆ„์  ๊ฒฝํ—˜๋Ÿ‰์ด ๋งŽ์€ ๊ธฐ์—…์€ ์ œํ’ˆ ์ƒ์‚ฐ์— ์†Œ์š”๋˜๋Š” ๋น„์šฉ๋„ ๊ฐ์†Œํ•˜๊ณ  ๊ทธ๋งŒํผ ์ˆ˜์ต์„ฑ๋„ ์ฆ๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ตœ๊ทผ ๋“ค์–ด์„œ๋Š” ์ •๋Ÿ‰์ ์ธ ์ธก์ •์˜ ์ค‘์š”๋„๋ณด๋‹ค๋Š” ์‚ฐ์—…์— ๋”ฐ๋ผ ๊ฒฝํ—˜ ๊ณก์„ ์ด ๊ฒฝ์Ÿ ์šฐ์œ„(Competitive Advantage)๋กœ ์ „ํ™˜๋˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์•„์„œ ์‹ ๊ทœ ์‚ฌ์—…์—์„œ ์ด๊ฒƒ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€? ์–ผ๋งˆ๋งŒํผ ๋นจ๋ฆฌ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€?๋ฅผ ์•Œ์•„๋ณด๋Š” ๋„๊ตฌ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. Figure III-46. ๊ฒฝํ—˜ ๊ณก์„  ๋ถ„์„์˜ ์‚ฌ๋ก€ Figure III-46์€ ๊ฒฝํ—˜ ๊ณก์„ ์˜ ์‚ฌ๋ก€๋ฅผ ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์ด ๊ณก์„ ์˜ ๊ธฐ์šธ๊ธฐ 20%๋Š” ์‹ค์ œ ๋ˆ„์  ์ƒ์‚ฐ๋Ÿ‰์ด 2๊ฐœ ์ฆ๊ฐ€ํ•˜๋ฉด ๋‹จ์œ„ ์ƒ์‚ฐ ๋น„์šฉ์€ 20% ๊ฐ์†Œํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ์™ธ์‚ฝ๋ฒ•(extrapolation)[3]์„ ํ†ตํ•ด ํ–ฅํ›„ ์ถ”์ •์น˜๋„ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๊ณ  ์ด๊ฒƒ์ด ์‚ฐ์—… ๋‚ด ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค ๋ˆ„์ ๋Ÿ‰์œผ๋กœ ๋Œ€์ฒด๋  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ฒฝ์Ÿ๋ ฅ์„ ๊ฐ€์ง€๊ธฐ ์œ„ํ•ด ์–ด๋Š ์ˆ˜์ค€๊นŒ์ง€ ์ƒ์‚ฐ๋Ÿ‰์„ ํ™•๋Œ€ํ•ด์•ผ ํ•˜๋Š”์ง€ ๋ชฉํ‘œ์น˜๋„ ๊ฐ€๋Š ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋Œ€๋Ÿ‰์ƒ์‚ฐ, ๋Œ€๋Ÿ‰ ๊ตฌ๋งค, ๋Œ€๋Ÿ‰์†Œ๋น„์‹œ๋Œ€์—๋Š” ์ „๋žต์  ๋„๊ตฌ๋กœ ์ถฉ๋ถ„ํžˆ ํ™œ์šฉํ•˜๊ธฐ ์œ ์šฉํ•˜์˜€์œผ๋‚˜ ์ตœ๊ทผ๊ณผ ๊ฐ™์€ ๋ณดํ˜ธ๋ฌด์—ญ์ฃผ์˜ ์‹œ๋Œ€๋‚˜ ์Šค๋งˆํŠธ ๊ธฐ์ˆ  ๋ฐœ๋‹ฌ์— ๋”ฐ๋ฅธ ๋Œ€๋Ÿ‰ ์ตœ์ ํ™”(Mass Customization) ์ƒ์‚ฐ ์‹œ๋Œ€์—๋Š” ํฐ ํ†ต์ฐฐ๋ ฅ์„ ์ œ๊ณตํ•˜๊ธฐ๋Š” ์–ด๋ ต๋‹ค. ๊ฐœ๋…์ ์œผ๋กœ ๊ณ ๊ฐ๊ณผ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ํ•˜๋Š” ์šฉ๋„๋กœ ์œ ์šฉํ•˜๋‹ค. Table III-27์€ ๊ฒฝํ—˜ ๊ณก์„  ๋ถ„์„์˜ ์žฅ๋‹จ์ ์„ ์š”์•ฝํ•œ ๊ฒƒ์ด๋‹ค. Table III-27. ๊ฒฝํ—˜ ๊ณก์„  ๋ถ„์„์˜ ์žฅ๋‹จ์  9.6 ๊ฐ€๊ฒฉํƒ„๋ ฅ์„ฑ(Price Elasticity) ๋ถ„์„ ์ˆ˜์š”์˜ ๊ฐ€๊ฒฉํƒ„๋ ฅ์„ฑ์€ ๊ฐ€๊ฒฉ์˜ ๋ณ€ํ™”์— ๋”ฐ๋ฅธ ์ˆ˜์š”์˜ ๋ฏผ๊ฐ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. ์–ด๋–ค ์ œํ’ˆ์ด๋“  ๊ฐ€๊ฒฉ์ด ์˜ค๋ฅด๋ฉด ์ˆ˜์š”๊ฐ€ ์ค„๊ณ , ๊ฐ€๊ฒฉ์ด ๋‚ด๋ฆฌ๋ฉด ์ˆ˜์š”๊ฐ€ ์ฆ๊ฐ€ํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ฆ‰, ๊ฐ€๊ฒฉ์ด ์˜ค๋ฅด๊ฑฐ๋‚˜ ๋‚ด๋ฆฌ๋Š” ์ •๋„์— ๋”ฐ๋ผ ์ˆ˜์š”๊ฐ€ ์–ด๋Š ์ •๋„ ๋ฏผ๊ฐํ•˜๊ฒŒ ๋ฐ˜์‘ํ•˜๋Š” ๊ฒƒ์ธ์ง€๋ฅผ ์ •๋Ÿ‰์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ์ˆ˜์‹๊ณผ ๊ฐ™์ด ์ˆ˜์š”๋Ÿ‰์˜ ๋ณ€๋™ ๋น„์œจ์„ ๊ฐ€๊ฒฉ์˜ ๋ณ€๋™ ๋น„์œจ๋กœ ๋‚˜๋ˆˆ ๊ฒƒ์ด๋‹ค. ์ˆ˜์š”์˜ ๊ฐ€๊ฒฉํƒ„๋ ฅ์„ฑ e๊ฐ€ 1๋ณด๋‹ค ํฌ๋ฉด ํƒ„๋ ฅ์ (elastic) ์ˆ˜์š”, 1๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋น„ํƒ„๋ ฅ์ (inelastic) ์ˆ˜์š”๋ผ๊ณ  ํ•œ๋‹ค. 2016๋…„ ์žˆ์—ˆ๋˜ ๋‹ด๋ฑƒ๊ฐ’ ์ธ์ƒ ์‚ฌ๋ก€๋ฅผ ์˜ˆ๋ฅผ ๋“ค๋ฉด ์ˆ˜์š”์˜ ๊ฐ€๊ฒฉํƒ„๋ ฅ์„ฑ์ด 1๋ณด๋‹ค ๋งŽ์ด ์ปธ๋‹ค๊ณ  ํ•œ๋‹ค. ์ด๋Š” ๋‹ด๋ฑƒ๊ฐ’์ด ์˜ฌ๋ž์ง€๋งŒ ๊ทธ์— ๋”ฐ๋ž€ ํŒ๋งค๋Ÿ‰ ๋ณ€ํ™”(๊ฐ์†Œ์ผ ๊ฒƒ์ด๋‹ค)๋Š” ๋”์šฑ ์ปธ๋‹ค๋Š” ๋ง๋กœ ๊ฒฐ๊ตญ ๋‹ด๋ฐฐ ํšŒ์‚ฌ๋“ค์€ ์ˆ˜์ต์ด ๋งŽ์ด ์ค„์—ˆ์„ ๊ฒƒ์œผ๋กœ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. Figure III-47. ๊ฐ€๊ฒฉํƒ„๋ ฅ์„ฑ ๋ถ„์„ ์‚ฌ๋ก€ ๋˜ํ•œ, ๊ฐ€๊ฒฉ ํƒ„๋ ฅ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์˜ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค. ๋Œ€์ฒด์žฌ์˜ ์œ ๋ฌด. ๊ฐ€๊ฒฉ์ด ์˜ค๋ฅผ ๋•Œ ๊ทธ๊ฒƒ์„ ๋Œ€์ฒดํ•  ๋Œ€์ฒดํ’ˆ์„ ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ๊ทธ๋ฆฌ ์˜ฎ๊ฒจ๊ฐˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํƒ„๋ ฅ์„ฑ์€ ์ปค์ง„๋‹ค. ๋Œ€์ฒด์žฌ์˜ ๊ฐ€๊ฒฉ. ํ•„์ˆ˜ํ’ˆ์ด๋ƒ ์‚ฌ์น˜ํ’ˆ์ด๋ƒ? ์ฆ‰, ํ•„์ˆ˜ํ’ˆ์ด๋ฉด ๋น„์‹ธ๋„ ์‚ด ์ˆ˜๋ฐ–์— ์—†์ง€๋งŒ ์‚ฌ์น˜ํ’ˆ์ด๋ฉด ๊ฐ€๊ฒฉ ๋ณ€๋™์ด ํด ๊ฒฝ์šฐ ๊ตฌ๋งคํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•„์ˆ˜ํ’ˆ์€ ๋น„ํƒ„๋ ฅ์  ์ฆ‰, ํƒ„๋ ฅ์„ฑ์ด ์ž‘๊ณ  ์‚ฌ์น˜ํ’ˆ์€ ํƒ„๋ ฅ์„ฑ์ด ํฌ๋‹ค. ๋Œ€์ฒด์žฌ์˜ ์ค‘๋…. ์†Œ๋น„์žฌ๊ฐ€ ์ค‘๋…์— ๊ฐ€๊นŒ์šด ํšจ๊ณผ๋ฅผ ๋‚ด๋ฉด ํƒ„๋ ฅ์„ฑ์ด ์ž‘๋‹ค. ์ˆ ์ด๋‚˜ ๋‹ด๋ฐฐ๋ฅผ ๋Š๊ธฐ ์–ด๋ ค์šด ๊ฒƒ์„ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋‹ค. B2B ์‚ฌ์—…์—๋Š” ์ด๋ฅผ โ€˜Lock-in Effect(๊ต์ฐฉ ํšจ๊ณผ)โ€™๋ผ๊ณ ๋„ ์„ค๋ช…ํ•œ๋‹ค. ์ฆ‰, ํ•œ ๋ฒˆ ํŒ๋งคํ•ด์„œ ์ข‹์€ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ  ํ•ด๋‹น ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค์— ๋Œ€ํ•œ ์˜์กด๋„๊ฐ€ ์ปค์ง€๋ฉด ๊ต์ฒด ๋น„์šฉ(switching cost)๋„ ์ปค์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋กœ ๊ต์ฒดํ•˜๊ธฐ ์–ด๋ ต๊ฒŒ ๋œ๋‹ค. ์ฆ‰, ๋น„ํƒ„๋ ฅ์ ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ B2B ์‚ฌ์—…์€ ํ•œ ๋ฒˆ ๊ฑฐ๋ž˜๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ๊ณ ๊ฐ์˜ ์‹ ๋ขฐ๋ฅผ ์–ป์œผ๋ฉด ์ง€์†๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค. Table III-28์€ ๊ฐ€๊ฒฉํƒ„๋ ฅ์„ฑ ๋ถ„์„์˜ ์žฅ๋‹จ์ ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. Table III-28. ๊ฐ€๊ฒฉํƒ„๋ ฅ์„ฑ ๋ถ„์„์˜ ์žฅ๋‹จ์  ์ง€๊ธˆ๊นŒ์ง€ ๋น„์šฉ๊ตฌ์กฐ ๋ถ„์„์„ ์‹œ์ž‘์œผ๋กœ ์†์ต๋ถ„๊ธฐ์  ๋ถ„์„, ์‚ฌ์—… ์ˆ˜์ต์„ฑ ๋ถ„์„, ๊ทœ๋ชจ์˜ ๊ฒฝ์ œ, ๊ฒฝํ—˜ ๊ณก์„ , ๊ฐ€๊ฒฉํƒ„๋ ฅ์„ฑ๊นŒ์ง€ ์‚ดํŽด๋ณด์•˜๋‹ค. 3C ๋ถ„์„์„ ๊ธฐ์ค€์œผ๋กœ ๋ณด๋ฉด ๊ณ ๊ฐ/์‹œ์žฅ, ๊ฒฝ์Ÿ์„ ์ดํ•ดํ•˜๊ณ  ์ž์‚ฌ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜์ต์„ฑ ๋ถ„์„[5]์„ ์‚ดํŽด๋ณด์•˜๋‹ค. ์ „๋žต์  ๋Œ€์•ˆ์˜ ํŒ๋‹จ ๋˜๋Š” ์„ ํƒ์„ ์œ„ํ•ด ์ถ”์ƒํ™” ์ˆ˜์ค€์ด ์ข€ ๋†’์€ ์—ญ๋Ÿ‰์— ๋Œ€ํ•ด ๋‹ค์Œ ์žฅ์—์„œ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. [1] ์ด์ƒ์‚ฐ๋น„์šฉ / ์ด์ƒ์‚ฐ๋Ÿ‰ [2] ๋Œ€๊ธฐ์—…์ด ์ค‘์†Œ๊ธฐ์—…๋ณด๋‹ค ๋” ๋งŽ์€ ์ด์ต์„ ๋‚ผ ์ˆ˜ ์žˆ์Œ์„ ์„ค๋ช…ํ•˜๋Š” ๊ธฐ๋ฐ˜์ด ๋˜๊ธฐ๋„ ํ•จ [3] ๋ณด์™ธ๋ฒ•์ด๋ผ๊ณ ๋„ ํ•จ. ๋ณด๊ฐ„๋ฒ•(๋‚ด์‚ฝ๋ฒ•)์ด ์ฃผ์–ด์ง„ ๊ตฌ๊ฐ„ ๋‚ด์—์„œ ๊ฐ’์˜ ์ถ”์ •์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ผ๋ฉด ์™ธ์‚ฌ ๋ฒ• ๋˜๋Š” ๋ณด์™ธ๋ฒ•์€ ๊ตฌ๊ฐ„ ๋ฐ–์˜ ๊ฐ’์„ ์ถ”์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•. ๋ฐ์ดํ„ฐ์˜ ๊ฒฝํ–ฅ์„ ๋ณด๊ณ  ๊ณผ๊ฑฐ๋‚˜ ๋ฏธ๋ž˜์˜ ๊ฐ’์„ ์ถ”์ •ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•จ [4] ์ด์™€ ๋น„์Šทํ•œ ๊ฐœ๋…์œผ๋กœ ํˆฌํ•˜์ž๋ณธ์ˆ˜์ต๋ฅ (ROIC. Return On Invested Capital)์ด ์žˆ๋‹ค. ์ด๋Š” ์‚ฌ์—…์— ์ง‘์ค‘ํ•ด์„œ ๋ณด๋Š” ๊ฒƒ์œผ๋กœ ์˜์—…์šฉ ์ˆœ์ž์‚ฐ์— ๋Œ€ํ•œ ์„ธ ํ›„ ์˜์—…์ด์ต๋ฅ ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ROIC = ์˜์—…์ด์ต / ์˜์—…์šฉ ์ˆœ์ž์‚ฐ= ์˜์—…์ด์ต / (์ž์‚ฐ์ด๊ณ„โ€“ ๋น„์˜์—…์ž์‚ฐ โ€“ ์˜์—…๋ถ€์ฑ„) = ์˜์—…์ด์ต / (๋น„ ์˜์—…๋ถ€์ฑ„ + ์ฐจ์ž…๊ธˆ + ์ž๋ณธ์ด๊ณ„โ€“ ๋น„์˜์—…์ž์‚ฐ) [5] ์ˆœํ˜„๊ฐ€๋ถ„์„(NPV) ๊ฐ™์€ ๊ฒƒ๋„ ์ด๋ฒˆ ์žฅ ์ˆ˜์ต์„ฑ ๋ถ„์„์—์„œ ๋‹ค๋ค„์งˆ ์ˆ˜๋„ ์žˆ๊ฒ ์œผ๋‚˜ ์ €์ž๋Š” Part III์˜ ๋งˆ์ง€๋ง‰ ์žฅ์ธ ๋Œ€์•ˆ์˜ ์„ ํƒ์—์„œ ๊ฐ™์ด ๋‹ค๋ฃฐ ๊ณ„ํš์ด๋‹ค. 10. ์—ญ๋Ÿ‰ ๋ถ„์„ ์ง€๊ธˆ๊นŒ์ง€ ์•ž ์žฅ์—์„œ ์ฃผ๋กœ ์„ค๋ช…ํ–ˆ๋˜ ๊ด€์ ์€ ๊ตญ๋‚ด์™ธ ๊ฒฝ์ œ ๋ฐ ์‚ฐ์—… ๋™ํ–ฅ, ๊ณ ๊ฐ์˜ ์š”๊ตฌ ๋“ฑ ๊ธฐ์—… ์™ธ๋ถ€์˜ ํ™˜๊ฒฝ๊ณผ ๊ทธ ๋ณ€ํ™”์— ๋Œ€ํ•ด ๋ถ„์„ํ•˜๋Š” ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•๋“ค์— ๋Œ€ํ•ด ์ฃผ๋กœ ์†Œ๊ฐœํ•˜์˜€๋‹ค. ์ด ์žฅ์—์„œ๋Š” ๊ธฐ์—… ๋‚ด๋ถ€์˜ ์—ญ๋Ÿ‰์— ๋Œ€ํ•œ ๊ณ ์ฐฐ๊ณผ ๊ทธ ๊ฒฝ์Ÿ๋ ฅ์— ๋Œ€ํ•ด ๋ถ„์„ํ•˜๋Š” ๋ฒ•์ด ์ค‘์‹ฌ์ด ๋  ๊ฒƒ์ด๋‹ค. ๊ธฐ์—…์˜ ์—ญ๋Ÿ‰์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๋Š” ๊ฒƒ์€ ๊ธฐ์—…์˜ ๊ฒฝ์˜ ์ „๋žต ์ˆ˜๋ฆฝ์„ ์œ„ํ•ด์„œ๋Š” ํ•„์ˆ˜์ ์ด๋‹ค. ๊ณ ๊ฐ์„ ๊ฐ๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ์šฐ๋ฆฌ์˜ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค์—๋Š” ์–ด๋–ค ๊ณ ๊ฐ ๊ฐ€์น˜๊ฐ€ ๋‹ด๊ฒจ์•ผ ํ•˜๋Š”์ง€? ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ค ์—ญ๋Ÿ‰์ด ํ•„์š”ํ•œ ๊ฒƒ์ธ์ง€? ์ด๋Š” ๊ฒฝ์Ÿ์‚ฌ์™€ ๋น„๊ตํ•ด์„œ ์–ผ๋งˆ๋‚˜ ๊ฐ•ํ•œ์ง€ ๋“ฑ์— ๋Œ€ํ•œ ๋ถ„์„์ด ์—ญ๋Ÿ‰ ๋ถ„์„์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ตญ์ œ๋…ธ๋™๊ธฐ๊ตฌ๋Š” ์—ญ๋Ÿ‰์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•˜์˜€๋‹ค. Competencies covers knowledge, skills, and know-how applied and mastered in a specific context ---------- ILO:R195, HRD Recommendation, 2004 ์ง€์‹๊ณผ ์Šคํ‚ฌ, ๊ฒฝํ—˜์„ ํฌํ•จํ•œ๋‹ค๋Š” ํ‘œํ˜„์€ ๋งค์šฐ ์ ์ ˆํ•จ๊ณผ ๋™์‹œ์— ๋ถ„์„์„ ๋งค์šฐ ์–ด๋ ต๊ฒŒ ํ•˜๋Š” ์š”์ธ์ด ๋˜๊ธฐ๋„ ํ•œ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์—ญ๋Ÿ‰ ๋ถ„์„์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด์ž. Figure III-48. ์—ญ๋Ÿ‰ ๋ถ„์„ ๋กœ๋“œ๋งต 10.1 ์ž์›๊ณผ ๋Šฅ๋ ฅ์˜ ๊ด€๊ณ„ ๊ฒฝ์˜์ „๋žต ์ปจ์„คํŒ…์—์„œ ๊ธฐ์—… ์ „๋žต ๋˜๋Š” ๊ฒฝ์˜ ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  ์ „๋žต ์ˆ˜๋ฆฝ์˜ ๊ด€์ ์„ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ค‘์š”ํ•œ๋ฐ, ์ž์› ์ค€ ๊ฑฐ๋ก (RBV)[1] ๊ด€์ ์—์„œ ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ผ์„ ์ง„ํ–‰ํ•œ๋‹ค. 1. ๊ธฐ์—…์ด ๋ณด์œ ํ•œ ์ž์›(Resources)๊ณผ ๋Šฅ๋ ฅ(Capabilities)์„ ๋ถ„์„ํ•œ๋‹ค 2. ์ด๋ฅผ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•  ์ „๋žต์„ ์ˆ˜๋ฆฝํ•œ๋‹ค 3. ์ˆ˜๋ฆฝ๋œ ์ „๋žต์— ํ•„์š”ํ•œ ์ž์› ๋ฐ ๋Šฅ๋ ฅ๊ณผ ๊ธฐ์—…์ด ํ˜„์žฌ ๋ณด์œ ํ•œ ์ž์› ๋ฐ ๋Šฅ๋ ฅ์˜ ์ฐจ์ด(Gap)๋ฅผ ์ฑ„์šธ ์‹คํ–‰๊ณผ์ œ ๋“ค์„ ๋„์ถœํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ž์›๊ณผ ์—ญ๋Ÿ‰์€ ์–ด๋–ป๊ฒŒ ์ •์˜๋˜๋Š” ๊ฒƒ์ผ๊นŒ? ์ž์›์€ ํฌ๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ 3๊ฐ€์ง€๋กœ ๋‚˜๋‰œ๋‹ค. ์œ ํ˜• ์ž์›(Tangible Resources). ๊ธฐ์—…์˜ ์œ ํ˜• ์ž์›์€ ๊ทธ ํŒŒ์•…๊ณผ ํ‰๊ฐ€๊ฐ€ ๊ฐ€์žฅ ์œ ์šฉํ•œ ์ž์›์œผ๋กœ ํ˜„๊ธˆ๊ณผ ๊ฐ™์€ ์žฌ๋ฌด์  ์ž์›๊ณผ ๊ฑด๋ฌผ, ๊ธฐ๊ณ„์™€ ๊ฐ™์€ ๋ฌผ๋ฆฌ์  ์ž์›์œผ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค ๋ฌดํ˜• ์ž์›(Intangible Resources). ๊ธฐ์—…์˜ ๋ฌดํ˜• ์ž์›์€ ์‰ฝ๊ฒŒ ํ‰๊ฐ€ํ•  ์ˆ˜ ์—†์œผ๋‚˜ ๊ฒฐ์ฝ” ๋ฌด์‹œํ•  ์ˆ˜ ์—†๋Š” ์ž์›์œผ๋กœ ๋Œ€ํ‘œ์ ์ธ ๊ฒƒ์œผ๋กœ ๊ธฐ์ˆ ๊ณผ ํ‰ํŒ(Reputation)์ด ์žˆ๋‹ค. ์ธ์  ์ž์›(Human Resources). ์ธ์  ์ž์›์€ ์‚ฌ๋žŒ์ธ๋ฐ ์‚ฌ๋žŒ์„ ์ž์›์œผ๋กœ ๋ณธ๋‹ค๋Š” ์‹œ๊ฐ์— ์ด์˜๋ฅผ ์ œ๊ธฐํ•˜๋Š” ์‚ฌ๋žŒ๋“ค๋„ ์žˆ์ง€๋งŒ ํ˜„๋Œ€ ๊ฒฝ์˜์—์„œ๋Š” ๋งค์šฐ ์ค‘์š”ํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ณ  ์žˆ๋‹ค. ์ง€์‹์˜ ์›์ฒœ์ด ๋˜๊ธฐ๋„ ํ•˜๊ณ  ๊ธฐ๊ณ„๋ฅผ ๋Šฅ๊ฐ€ํ•˜๋Š” ์„ฑ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ๋„ ํ•œ๋‹ค Table III-29๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ์—…์˜ ์ž์›๊ณผ ์ธก์ •์ง€ํ‘œ๋ฅผ ์†Œ๊ฐœํ•˜์˜€๋‹ค. Table III-29. ๊ธฐ์—… ์ž์›์˜ ์ข…๋ฅ˜ ๋ฐ ์ธก์ • ๊ทธ๋ ‡๋‹ค๋ฉด ๊ธฐ์—…์˜ ๋Šฅ๋ ฅ(Capabilities)์€ ๋ฌด์—‡์ผ๊นŒ? ์ด๋Ÿฐ ์ •์˜๋‚˜ ์ฃผ์ œ๋Š” ์‚ฌ๋žŒ๋งˆ๋‹ค ์ƒ๊ฐ์ด ๋‹ค๋ฅด๊ณ  ์ด์— ๋Œ€ํ•œ ๋…ผ์˜ ์ž์ฒด๋ฅผ ๋งค์šฐ ํ˜„ํ•™์ (่ก’ๅญธ็š„)์œผ๋กœ ๋Š๋‚„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ €์ž ์ƒ๊ฐ์—๋„ ์ด๋Ÿฐ ๋…ผ์˜๊ฐ€ ๊ทธ๋‹ค์ง€ ์‹ค์งˆ์ ์ด์ง€ ์•Š์•„ ํ•œํŽธ์œผ๋กœ๋Š” ์ผ์— ๋„์›€์ด ๋ ๊นŒ ์ƒ๊ฐ๋„ ๋“ค์ง€๋งŒ ๊ธฐ์—…์˜ ๋Šฅ๋ ฅ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ด ๋ณด๊ณ ์ž ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ณต์žฅ ์„ค๋น„์™€ ๊ณต์žฅ ์ง์›์€ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๊ฐ๊ฐ ๊ธฐ์—…์˜ ์œ ํ˜• ์ž์›์ด๊ณ  ์ธ์  ์ž์›์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๊ฒƒ๋“ค์ด ์ž˜ ์œตํ™”๋˜์–ด ๋งŒ๋“ค์–ด ๋‚ด๋Š” ์ƒ์‚ฐ ๋Šฅ๋ ฅ์€ ๊ธฐ์—…์ด ๋ณด์œ ํ•œ ์—ญ๋Ÿ‰(Competencies)[2]์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ๊ธฐ์—…์˜ ์—ญ๋Ÿ‰์ด ์ œ๋Œ€๋กœ ๋ฐœํœ˜๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ž์›๊ณผ ๋Šฅ๋ ฅ์˜ ์กฐํ•ฉ์ด ๋งค์šฐ ์ค‘์š”ํ•œ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‹ค์ œ ํšŒ์‚ฌ ์ƒํ™œ์„ ํ•˜๋ฉด์„œ ๊ธฐ์—…์˜ ์ž์›๊ณผ ๋Šฅ๋ ฅ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๋‹ค์Œ 2๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๋งค์šฐ ๋งŽ์ด ์ ‘ํ•  ์ˆ˜ ์žˆ๋‹ค. 1. ํ•˜๋‚˜ํ•˜๋‚˜์˜ ์ž์›์œผ๋กœ์„œ๋Š” ํ›Œ๋ฅญํ•˜์ง€๋งŒ ์กฐํ•ฉ์œผ๋กœ์„œ๋Š” ํ˜•ํŽธ์—†๋Š” ๊ฒฝ์šฐ 2. ์ž์› ํ•˜๋‚˜ํ•˜๋‚˜๋Š” ๋ณ„ ๋ณผ์ผ ์—†์ง€๋งŒ ์กฐํ•ฉ์œผ๋กœ์„œ ํฐ ๋Šฅ๋ ฅ์„ ๋ฐœํœ˜ํ•˜๋Š” ๊ฒฝ์šฐ ์ž์›๊ณผ ๋Šฅ๋ ฅ์˜ ์กฐํ•ฉ์„ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๊ณ  ๊ฒฝ์Ÿ์šฐ์œ„๋‚˜ ์ง€์† ๊ฐ€๋Šฅํ•œ ์—ญ๋Ÿ‰์œผ๋กœ ์œ ์ง€ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ณ ๋ฏผ์€ ๊ธฐ์—…์˜ ํ•ต์‹ฌ ์—ญ๋Ÿ‰(Core Competence) ๊ฐœ๋ฐœ๋กœ ์ด์–ด์ง€๊ฒŒ ๋œ๋‹ค. ์ฆ‰, RBV ๊ด€์ ์—์„œ ๊ฒฝ์˜ ์ „๋žต์€ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์„ ์–ด๋–ป๊ฒŒ ๊ฐœ๋ฐœํ•˜๊ณ  ๋ฐœ์ „์‹œํ‚ค๋Š๋ƒ๊ฐ€ ๊ฐ€์žฅ ํฐ ๊ด€๊ฑด์ด ๋œ๋‹ค.[3] ๊ด‘์˜์˜ ๊ฐœ๋…์œผ๋กœ ๊ธฐ์—…์˜ ์—ญ๋Ÿ‰(Competence)์€ ๊ธฐ์—…์ด ๋ณด์œ ํ•œ ๋Šฅ๋ ฅ(Capabilities)๊ณผ ์‚ฐ์—…์—์„œ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ๋Š” ๊ตฌ์กฐ์  ์ž…์ง€(Structural Position)๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค.[4] Table III-30์€ ๊ธฐ์—…์ด ๋ณด์œ ํ•œ ๋Šฅ๋ ฅ๊ณผ ์‚ฐ์—…์—์„œ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ๋Š” ๊ตฌ์กฐ์  ์ž…์ง€์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋‹ค. Table III-30. ๊ธฐ์—… ๋Šฅ๋ ฅ๊ณผ ๊ตฌ์กฐ์  ์ž…์ง€ Table III-30์—์„œ ํ‘œํ˜„๋œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์œ  ์—ญ๋Ÿ‰๊ณผ ๊ตฌ์กฐ์  ์ž…์ง€๊ฐ€ ์ƒํ˜ธ๋ณด์™„์ ์œผ๋กœ ์ž‘์šฉํ•˜์—ฌ ๊ฒฝ์Ÿ๋ ฅ์˜ ์ˆ˜์ค€์ด๋‚˜ ๊ณ ๊ฐ ๊ฐ€์น˜์˜ ๊ธฐ์—ฌ๋„๊ฐ€ ๋†’์€ ์—ญ๋Ÿ‰์ด ํ•ต์‹ฌ ์—ญ๋Ÿ‰์œผ๋กœ ์ •์˜๋œ๋‹ค. 10.2 ํ•ต์‹ฌ ์—ญ๋Ÿ‰์˜ ๊ฐœ๋… ๊ธฐ์—…์˜ ์—ญ๋Ÿ‰(Competency)์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ์—…์˜ ์ž์›(Resources)๊ณผ ๋Šฅ๋ ฅ(Capabilities)์— ๋Œ€ํ•ด ์•ž์„œ ์‚ดํŽด๋ณด์•˜๋‹ค. ํ”„๋ผํ• ๋ผ๋“œ(C.K.Prahalad.1941 ~ 2010)์™€ ํ•˜๋ฉœ(GaryHamel. 1954 ~ ํ˜„์žฌ)์— ์˜ํ•ด ์†Œ๊ฐœ๋œ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์˜ ๊ฐœ๋…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํ•ต์‹ฌ ์—ญ๋Ÿ‰(CoreCompetence)์ด๋ž€, ๊ฒฝ์Ÿ ๊ธฐ์—…์— ๋Œ€ํ•ด ์ ˆ๋Œ€์ ์ธ ๊ฒฝ์Ÿ ์šฐ์œ„(Competitive Advantage) ์ฐฝ์ถœ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ๊ธฐ์—…์˜ ๋…ํŠนํ•œ ์ž์›(Resources)๊ณผ ๋Šฅ๋ ฅ(Capabilities)์˜ ์กฐํ•ฉ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์žฅ์—์„œ ๊ตฌ์ž… ๊ฐ€๋Šฅํ•˜๊ฑฐ๋‚˜ ์žฌ์ƒ์‚ฐ, ๋ณต์ œ, ๋Œ€์ฒด๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•œ ์œ ๋ฌดํ˜•์˜ ์ž์‚ฐ์ด๋‹ค. ์œ„์˜ ์ •์˜์—์„œ๋„ ์–ธ๊ธ‰๋˜์—ˆ์ง€๋งŒ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์€ ์‚ฌ์—…์  ๊ด€์ ์—์„œ ๋‹ค์Œ 4๊ฐ€์ง€ ์†์„ฑ์„ ์ถฉ์กฑ์‹œ์ผœ์•ผ ํ•œ๋‹ค. 1. ๋…์ฐฝ์„ฑ(Uniqueness). ํ•ต์‹ฌ ์—ญ๋Ÿ‰์€ ๋‹ค์–‘ํ•œ ์ง€์‹ ๋ฐ ๊ฒฝํ—˜, ๊ธฐ์ˆ ์˜ ๊ฒฐํ•ฉ์œผ๋กœ ํ•œ ๊ธฐ์—…๋งŒ์ด ๊ฐ€์ง€๋Š” ๋…ํŠนํ•จ ์œผ๋กœ ๊ฒฝ์Ÿ์‚ฌ ๋Œ€๋น„ ์ฐจ๋ณ„์  ์šฐ์œ„๋ฅผ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค[5]. 2. ํฌ์†Œ์„ฑ(Rareness or Inimitability).ํ•ต์‹ฌ ์—ญ๋Ÿ‰์€ ๊ฒฝ์Ÿ์‚ฌ๊ฐ€ ๋ชจ๋ฐฉํ•˜๊ฑฐ๋‚˜ ํ‰๋‚ด ๋‚ผ ์ˆ˜ ์—†์–ด์•ผ ํ•œ๋‹ค 3. ๊ณ ๊ฐ ์ˆ˜ํ˜œ(Customer Benefit).ํ•ต์‹ฌ ์—ญ๋Ÿ‰์€ ๊ธฐ์—…์˜ ๊ณ ๊ฐ์ด๋‚˜ ์†Œ๋น„์ž๋“ค์—๊ฒŒ ํ˜œํƒ์„ ์ค„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. 4. ๊ฐ€์น˜ ์ฐฝ์ถœ(Value Creation). ํ•ต์‹ฌ ์—ญ๋Ÿ‰์€ ์ƒˆ๋กœ์šด ์‚ฌ์—… ๊ธฐํšŒ๋ฅผ ์—ด์–ด์ค˜ ์‹ ์ œํ’ˆ ์‹œ์žฅ์œผ๋กœ ์ง„์ž…ํ•˜๊ณ  ํƒ€ ์‚ฐ์—… ๋ฐ ์‚ฌ์—…์—์„œ์˜ ์ ์šฉ ๊ฐ€๋Šฅ์„ฑ์„ ์—ด์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ํ•ต์‹ฌ ์—ญ๋Ÿ‰์˜ ์ •์˜๋‚˜ 4๊ฐ€์ง€ ์†์„ฑ ๋“ฑ ๊ฒฝ์˜ํ•™์ž๋“ค์ด ๊ฐœ๋…์ ์œผ๋กœ ์ž˜ ์„ค๋ช…ํ•˜์˜€์œผ๋‚˜ ์—ฌ์ „ํžˆ ์ถ”์ƒ์ ์ด์–ด์„œ ๋„์ž…๋œ ์„ค๋ช…์ด Figure III-49์™€ ๊ฐ™์€ ๊ฒฝ์Ÿ๋ ฅ ๋‚˜๋ฌด(Competency Tree)์ด๋‹ค. Figure III-49. ๊ฒฝ์Ÿ๋ ฅ ๋‚˜๋ฌด ์ฆ‰, ๊ธฐ์—…์˜ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์ด ๋‚˜๋ฌด์˜ ๋ฟŒ๋ฆฌ์— ํ•ด๋‹นํ•˜๊ณ  ํ•ต์‹ฌ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋Š” ๋‚˜๋ฌด์˜ ํฐ ์ค„๊ธฐ๋‚˜ ํฐ ๊ฐ€์ง€, ์‚ฌ์—… ์˜์—ญ์€ ์ž‘์€ ๊ฐ€์ง€, ๋‚˜๋ญ‡์žŽ์ด๋‚˜ ์—ด๋งค๋“ค์€ ๊ณ ๊ฐ์ด๋‚˜ ์†Œ๋น„์ž์—๊ฒŒ ์ „๋‹ฌ๋˜๋Š” ์ตœ์ข… ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.[6] ํ•œ๋•Œ, ์‚ผ์„ฑ๊ทธ๋ฃน์€ ํ•ต์‹ฌ ์—ญ๋Ÿ‰ ์ด๋ก ์„ ๊ฒฝ์˜ ์ „๋ฐ˜์— ๋…น์—ฌ ๋ฐ˜์˜ํ•˜์˜€๋Š”๋ฐ ๊ทธ๋•Œ ์‚ฌ์šฉํ•œ โ€˜์‹ ์ˆ˜์ข… ์‚ฌ์—…โ€™์ด๋ผ๋Š” ์šฉ์–ด๊ฐ€ ์ •๋ถ€์™€ ์‚ฐ์—…๊ณ„์— ๋„๋ฆฌ ์•Œ๋ ค์ง€๊ธฐ๋„ ํ•˜์˜€๋‹ค. 10.3 ํ•ต์‹ฌ ์—ญ๋Ÿ‰์˜ ์ธก์ • ๋ฐ ๊ด€๋ฆฌ ์ปจ์„คํŒ…์—์„œ ๊ธฐ์—…์˜ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์„ ํŒŒ์•…ํ•˜๊ณ  ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ผ๋ฐ˜์ ์œผ๋กœ TableIII-31๊ณผ ๊ฐ™์ด 5๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ์„œ ์ง„ํ–‰๋œ๋‹ค. Table III-31. ํ•ต์‹ฌ ์—ญ๋Ÿ‰์˜ ๋ถ„์„ ๋‹จ๊ณ„ ๋ฐ ์ ์šฉ ๊ธฐ๋ฒ• ๊ฐ ๋‹จ๊ณ„๋ณ„๋กœ ์กฐ๊ธˆ ๋” ์ƒ์„ธํžˆ ์•Œ์•„๋ณด๋ฉด ์ฒซ ๋ฒˆ์งธ, ์‹œ์žฅ์„ ์ดํ•ดํ•˜๊ณ  ๊ธฐ์—…์ด ๋ณด์œ ํ•œ ๊ฐ•์ ์„ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์ฐจ์›์ (Multi-dimensional) ์‹œ๊ฐ์—์„œ ๊ธฐ์—…์˜ ๊ฒฝ์Ÿ๋ ฅ์„ ํŒŒ์•…ํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ๊นŠ๊ฒŒ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค. ๊ธฐ์—…์˜ ๋ณด์œ  ์—ญ๋Ÿ‰, ์—…๋ฌด ํ”„๋กœ์„ธ์Šค(Value Chain), ์กฐ์ง ๊ตฌ์กฐ์˜ ์—ญํ• ๊ณผ ์ฑ…์ž„์„ ์ธํ„ฐ๋ทฐ ๋“ฑ์„ ํ†ตํ•ด์„œ ์ดํ•ดํ•˜๊ณ  ์™ธ๋ถ€ ์‹œ๊ฐ์˜ ๊ด€์ ์—์„œ ์‹œ์žฅ ๋‚ด ์ž…์ง€(Outside-In Perspective)๋ฅผ ํŒŒ์•…ํ•˜๊ณ , ๋‚ด๋ถ€ ์‹œ๊ฐ์˜ ๊ด€์ (Inside-Out Perspective)์—์„œ ํ˜„์žฌ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ๊ฐ•์ ์„ ํŒŒ์•…ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ๋ณด์œ  ์—ญ๋Ÿ‰์„ ์ •์˜ํ•˜๋Š” ์ผ์€ ๊ฐ•์ ์˜ ๊ทผ๊ฑฐ์™€ ๊ฒฝ์Ÿ ๋Œ€๋น„ ์ˆ˜์ค€์„ ํŒŒ์•…ํ•˜๊ณ  ์‹œ์žฅ ๋‚ด ์„ฑ๊ณต์„ ์œ„ํ•ด ๋˜๋Š” ์ „๋žต์  ํ•„์š”์„ฑ์— ์˜ํ•ด ์š”๊ตฌ๋˜๋Š” ์—ญ๋Ÿ‰์„ ์ •์˜ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋Š” ์—ญ๋Ÿ‰๊ณผ ๊ตฌ์กฐ์  ๊ฐ•์ ์œผ๋กœ ๊ตฌ๋ถ„๋˜๋Š”๋ฐ ํŠนํžˆ, ์—ญ๋Ÿ‰์€ ๊ฒฝ์Ÿ์šฐ์œ„ ๋ฐ ๊ณ ๊ฐ ๊ฐ€์น˜์™€ ๋งคํ•‘๋˜์–ด ๊ฐ ์—ญ๋Ÿ‰์ด ์‹œ์žฅ๊ณผ ๊ฐ ์‚ฌ์—… ์„ธ๊ทธ๋จผํŠธ์—์„œ ์ค‘์š”ํ•จ์ด ์ดํ•ด๋˜์–ด์•ผ ํ•œ๋‹ค. ์„ธ ๋ฒˆ์งธ, ํ•ต์‹ฌ ์—ญ๋Ÿ‰ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์€ Figure III-50๊ณผ ๊ฐ™์€ ๊ฒฝ์Ÿ์šฐ์œ„-๊ณ ๊ฐ ๊ฐ€์น˜ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ๋งŽ์ด ํ™œ์šฉํ•œ๋‹ค. Figure III-50. ๊ฒฝ์Ÿ์šฐ์œ„-๊ณ ๊ฐ ๊ฐ€์น˜ ๋งคํŠธ๋ฆญ์Šค ํŒŒ์•…๋œ ๊ฐ ์—ญ๋Ÿ‰ ๊ทธ๋ฃน์€ ์‹œ์žฅ ์„ธ๊ทธ๋จผํŠธ ๋ณ„๋กœ ์—ญ๋Ÿ‰์˜ ์ค‘์š”์„ฑ์„ ๋น„๊ตํ•˜๊ณ  ๊ณตํ†ต์  ํ•ต์‹ฌ ์—ญ๋Ÿ‰์„ ๋„์ถœํ•˜์—ฌ ์—ญ๋Ÿ‰ ๊ทธ๋ฃน๋ณ„ ์ถ”์ง„ ๋ฐฉ์•ˆ ๋ฐ ์ œ๋ฐ˜ ์‚ฌํ•ญ์„ ์ ๊ฒ€ํ•œ๋‹ค. ๋„ค ๋ฒˆ์งธ, ์—ญ๋Ÿ‰ ์ด์šฉ ๋ฐ ๊ฐ•ํ™” ๋ฐฉ์•ˆ ๋„์ถœ์„ ์œ„ํ•ด ํฌ๋กœ์Šค ์„ธ๊ทธ๋จผํŠธ(Cross-segment) ๋ณ„๋กœ ๊ณตํ†ต ํ•„์š” ์—ญ๋Ÿ‰์„ ํŒŒ์•…ํ•˜๊ณ  ํƒ€ ์‹œ์žฅ์˜ ํ™•๋Œ€ ์ ์šฉ ์—ฌ๋ถ€ ๋ฐ ์‹ ๊ทœ ์‚ฌ์—… ์ง„์ถœ์—์˜ ํ™œ์šฉ ๊ฐ€๋Šฅ์„ฑ์„ ์ ๊ฒ€ํ•œ๋‹ค. ์ด๊ฒƒ์ด ์šฉ์ดํ•œ ์—ญ๋Ÿ‰์ด ๊ฒฝ์Ÿ ์šฐ์œ„๋ฅผ ๊ฐ€์งˆ ํ™•๋ฅ ์ด ๋†’๋‹ค. ๋งˆ์ง€๋ง‰ ๋‹ค์„ฏ ๋ฒˆ์งธ๋กœ ์—ญ๋Ÿ‰์˜ ๊ฐœ๋ฐœ ๋ฐ ํ™•๋ณด๋ฅผ ์œ„ํ•œ ๊ณ„ํš์„ ์„ธ์šฐ๊ฒŒ ๋˜๋Š”๋ฐ ๋ณ€ํ™”๊ด€๋ฆฌ ๊ด€์ ์—์„œ ์ถ”์ง„ ์ฃผ์ฒด๋ฅผ ๋ช…ํ™•ํžˆ ํ•˜๊ณ  ๊ณ„ํš์œผ๋กœ์„œ ๊ด€๋ฆฌ๋˜๊ธฐ ์œ„ํ•ด ๊ฐ์ข… ๋งˆ์ผ์Šคํ†ค(milestones)์„ ์ƒ์„ธํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์„ ๊ฐœ๋ฐœํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ต์‹ฌ ์—ญ๋Ÿ‰์„ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ ๋‹จ๊ณ„๋ณ„๋กœ ์ง„ํ–‰๋˜์–ด ์ œ๋Œ€๋กœ ๋˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ์ ๊ฒ€ํ•˜๋ฉด ๋œ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์†Œ๊ฐœํ•œ ๊ฒƒ์€ ์ €์ž๊ฐ€ ๊ณผ๊ฑฐ์— ์ „๋žต๊ธฐํš์ด๋‚˜ ์ปจ์„คํŒ… ์—…๋ฌด๋ฅผ ํ•˜๋ฉด์„œ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์„ ๋‹ค๋ฃฐ ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐฉ๋ฒ•๋“ค์„ ์†Œ๊ฐœํ•œ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋˜ ์—ญ๋Ÿ‰์„ ์ธก์ •ํ•˜๊ณ  ์ •๋Ÿ‰ํ™”ํ•˜์—ฌ ๊ด€๋ฆฌํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๋งŽ์€ ์–ด๋ ค์›€์ด ๋”ฐ๋ฅธ๋‹ค. ํ•˜๋ฉœ์€ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์ด ์ถ•์ ๋œ ์ง€์‹(Knowledge Stock)์ด ์•„๋‹ˆ๋ผ ํ๋ฅด๋Š” ์ง€์‹(Knowledge Flow)์ด๋ผ๊ณ  ํ•˜์˜€๋‹ค. ์ด๋Š” ๋™์ ์ธ ๋‹จ๋ฉด์˜ ์ค‘์š”์„ฑ์„ ๊ฐ•์กฐํ•œ ๋ง์ด๋ผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์–ด์จŒ๋“  ๊ธฐ์—…์ด ๋‚ด๋ถ€์ ์œผ๋กœ ๋ณด์œ ํ•œ ์ž์›์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ์ƒ๊ฐ์—์„œ ์ถœ๋ฐœํ•œ ํ•ต์‹ฌ ์—ญ๋Ÿ‰ ์ด๋ก ์€ ๊ฒฝ์˜์ „๋žต์—์„œ ํ•œ๋•Œ ์ค‘์š”ํ•˜๊ฒŒ ๋‹ค๋ฃจ์–ด์กŒ๋˜ ๊ฒƒ์ด ์‚ฌ์‹ค์ด๊ณ  ์ด๋ฅผ ๊ฐ•ํ™”ํ•˜๊ณ  ๋ฐœ์ „์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๊ต์œก์˜ ์ค‘์š”์„ฑ์ด ์ œ๊ณ ๋˜์–ด ๊ธฐ์—… ๋‚ด ํ•™์Šต์กฐ์ง์˜ ๋ถ์ด ์ผ์—ˆ๊ณ  ๋‚˜์•„๊ฐ€ ์ง€์‹๊ฒฝ์˜์˜ ํ† ๋Œ€๊ฐ€ ๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ญ๋Ÿ‰์˜ ์ •๋Ÿ‰์  ์ธก์ •์— ๋Œ€ํ•œ ์ด์Šˆ๋„ ๊ทธ๋Œ€๋กœ ๋ฌผ๋ ค๋ฐ›์•„ ์ง€์‹๊ฒฝ์˜์˜ ์„ฑ๊ณผ ์ธก์ •๋„ ๋„์ „์ด ๋งŽ์•˜๋‹ค. ๋˜ํ•œ, 1990๋…„๋Œ€๋ฅผ ๋„˜์–ด 2000๋…„๋Œ€๊นŒ์ง€๋งŒ ํ•˜์—ฌ๋„ ๊ธฐ์—… ๋‚ด๋ถ€์˜ ์—ญ๋Ÿ‰์„ ๊ทน๋Œ€ํ™”ํ•˜์ž๋Š” ์ „๋žต ๊ธฐ์กฐ๋กœ ๋‚ด๋ถ€ ์„ฑ์žฅ(Organic Growth) ์ „๋žต์ด ์ฃผ๋ฅผ ์ด๋ฃจ์—ˆ์œผ๋‚˜ ์˜ค๋Š˜๋‚ ๊ณผ ๊ฐ™์ด ๊ธ‰๋ณ€ํ•˜๋Š” ์‚ฌ์—… ํ™˜๊ฒฝ์—์„œ๋Š” ๋‹จ์ผ ๊ธฐ์—…์ด ๋ชจ๋“  ํ˜์‹ ์„ ๊ฐ๋‹นํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜์—ฌ ํ•ต์‹ฌ ์—ญ๋Ÿ‰ ์ด๋ก ์€ ํ‡ด์ƒ‰๋˜์–ด ๊ฐ€๋Š” ๋Š๋‚Œ์ด๋‹ค. Table III-32๋Š” ํ•ต์‹ฌ ์—ญ๋Ÿ‰ ๋ถ„์„์˜ ์žฅ๋‹จ์ ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. Table III-32. ํ•ต์‹ฌ ์—ญ๋Ÿ‰ ๋ถ„์„์˜ ์žฅ๋‹จ์  Break #16. ์ง€์† ๊ฒฝ์Ÿ์šฐ์œ„(SCA: Sustainable Competitive Advantage) ๋ฐ์ด๋น„๋“œ ์—์ด์ปค(David Aaker. 1938 ~ ํ˜„์žฌ)๋Š” ํ•ต์‹ฌ ์—ญ๋Ÿ‰๊ณผ ๋น„์Šทํ•œ ๊ฐœ๋…์œผ๋กœ '์ง€์† ๊ฒฝ์Ÿ์šฐ์œ„'๋ผ๋Š” ๊ฐœ๋…์„ ์ฃผ์ฐฝํ•˜์˜€๋‹ค. ์ด๊ฒƒ์€ โ€˜๊ฒฝ์Ÿ์šฐ์œ„ ๋Šฅ๋ ฅโ€™๊ณผ โ€˜๊ฒฝ์Ÿ์šฐ์œ„ ์ž์‚ฐโ€™์œผ๋กœ ๊ตฌ๋ถ„๋˜๋Š”๋ฐ ๊ฒฝ์Ÿ์šฐ์œ„ ๋Šฅ๋ ฅ์€ ํ•ต์‹ฌ ์—ญ๋Ÿ‰๊ณผ ์œ ์‚ฌํ•œ ๊ฐœ๋…์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๊ณ  ๊ฑฐ๊ธฐ์— ๊ฒฝ์Ÿ์šฐ์œ„ ์ž์‚ฐ์„ ๋”ํ•œ ๊ฒƒ์ด๋‹ค. ๊ธฐ์กด์˜ ๊ฒฝ์Ÿ ์šฐ์œ„ ๊ฐœ๋…์ด ๊ธ€๋กœ๋ฒŒ ๊ฒฝ์Ÿ ์‹œ๋Œ€์˜ ๋„๋ž˜๋กœ ์ง€์† ๊ฐœ๋ฐœ(Sustainable Development)๊ณผ ๊ฐ™์€ ์šฉ์–ด ๋“ฑ์ด ๋“ฑ์žฅํ•˜๋ฉด์„œ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๊ธฐ ์‹œ์ž‘ํ•˜์˜€๋‹ค. ๊ฒฝ์Ÿ ์šฐ์œ„(Competitive Advantage)๋ž€ ๊ฒฝ์Ÿ์‚ฌ๋ณด๋‹ค ๋†’์€ ์„ฑ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ, ์ƒ์กดํ•˜๊ณ  ์„ฑ์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ, ๋ฐ”๋ผ๋Š” ์„ฑ๊ณผ๋ฅผ ์„ฑ์ทจํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์„ ์˜๋ฏธํ•œ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์Ÿ ์šฐ์œ„๋Š” ํฌ๊ฒŒ ๋‹ค์Œ 2๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ํ™˜๊ฒฝ์  ์šฐ์œ„(Environmental Advantage) ์กฐ์ง ์ง€์‹(Organizational Intelligence) ํ™˜๊ฒฝ์  ์šฐ์œ„๋ž€, ์ง€์—ญ์  ์ด์  ๋“ฑ์œผ๋กœ ํ™•๋ณดํ•œ ๊ฐ€๊ฒฉ๊ฒฝ์Ÿ๋ ฅ์ด๋‚˜ ์•„์›ƒ์†Œ์‹ฑ ๋“ฑ์œผ๋กœ ํ™•๋ณดํ•œ ์šฐ์œ„๋ฅผ ์˜๋ฏธํ•˜๊ณ  ์กฐ์ง ์ง€์‹์ด๋ž€ ์ง€์‹๊ฒฝ์˜๊ณผ ๊ฐ™์€ ๊ธฐ์—… ๋‚ด๋ถ€ ํ”„๋กœ์„ธ์Šค์˜ ๊ฒฝ์Ÿ๋ ฅ์œผ๋กœ ์ธํ•œ ์„ฑ๊ณต์  ์˜์‚ฌ๊ฒฐ์ • ์ง€์› ์—ญ๋Ÿ‰์„ ์˜๋ฏธํ•œ๋‹ค. ์ด์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ง€์† ๊ฒฝ์Ÿ ์šฐ์œ„๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด Figure III-51๊ณผ ๊ฐ™๋‹ค. Figure III-51. ์ง€์† ๊ฒฝ์Ÿ์šฐ์œ„์˜ ๊ฐœ๋… Figure III-51์— ๋”ฐ๋ฅด๋ฉด ๊ธฐ์—…์˜ ์ž์›๊ณผ ๋Šฅ๋ ฅ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฒฝ์Ÿ์šฐ์œ„ A๋Š” ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด์„œ ๊ฒฝ์Ÿ์šฐ์œ„๋กœ์„œ์˜ ์ง€์œ„๋ฅผ ์žƒ๊ฒŒ ๋œ๋‹ค. ํ•œ๋ฒˆ ๋‚ฎ์•„์ง„ ๊ฒฝ์Ÿ์šฐ์œ„๋Š” ๋‹ค์‹œ ํšŒ๋ณต๋˜๊ธฐ๊นŒ์ง€ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. (B) ๊ทธ๋Ÿฌ๋‚˜ ์ง€์† ๊ฒฝ์Ÿ์šฐ์œ„(C)๋Š” ๋‚ฎ์•„์งˆ ๋•Œ ๊ทธ๊ฒƒ์„ ์ง€์†์‹œํ‚ด์œผ๋กœ์จ ๊ฒฝ์Ÿ๋ ฅ์„ ์ง€์†์ ์œผ๋กœ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์ œํ’ˆ ๋ฆฌ๋”์‹ญ(Product Leadership), ์šด์˜ ํšจ์œจ์„ฑ(Operational Excellency), ๊ณ ๊ฐ ์นœ๋ฐ€๋„(Customer Intimacy) ๋“ฑ์ด ๊ทธ๊ฒƒ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค. ์ง€์† ๊ฒฝ์Ÿ์šฐ์œ„์˜ ์†์„ฑ์€ ํ•ต์‹ฌ ์—ญ๋Ÿ‰๊ณผ ์œ ์‚ฌํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด์•ผ๊ธฐํ•˜๋Š” ์‚ฌ๋žŒ๋“ค๋„ ์žˆ๋‹ค. ๋ชจ๋ฐฉ์ด ์–ด๋ ต๋‹ค(difficult to mimic) ๋…ํŠนํ•˜๋‹ค(Unique) ์ง€์†๋œ๋‹ค(Sustainable) ๊ฒฝ์Ÿ์‚ฌ์— ๋น„ํ•ด ์••๋„์ ์ด๋‹ค(superior to compete) ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์— ์ ์‘ ๊ฐ€๋Šฅํ•˜๋‹ค(applicable to multiple situation) ๊ทธ๋Ÿฌ๋‚˜ ๊ธฐ์—…์˜ ์ง€์† ๊ฒฝ์Ÿ์šฐ์œ„๋ฅผ ์ฐฝ์ถœํ•˜๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์š”์†Œ๋Š” โ€˜๊ฐ€์น˜๋ฅผ ์ฐฝ์ถœํ•˜๋Š”์ง€โ€™์™€ โ€˜๋ชจ๋ฐฉ ๋ถˆ๊ฐ€๋Šฅโ€™ํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ ๊ฐ€์น˜๋ฅผ ์ฐฝ์ถœํ•˜๋Š” ๊ฒƒ์€ ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ํ‚ค์šฐ๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ–‰์œ„๋ฅผ ๋งํ•˜๋ฉฐ, โ€˜๋Šฅ๋ ฅ์„ ๋ชจ ๋ฐฉ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹คโ€™๋Š” ๊ฒƒ์€ ์œ ๋ฌดํ˜•์˜ ์ž์›์— ๋น„ํ•ด ๋Šฅ๋ ฅ์ด ๋ชจ ๋ฐฉ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋Š๋ผ๋Š” ์ด์œ ๋Š” ๊ธฐ์—…์˜ ๋Šฅ๋ ฅ์ด๋ผ๋Š” ๊ฒƒ์ด ๊ธฐ์—… ์™ธ๋ถ€ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ์ž˜ ๋ณด์ด์ง€ ์•Š์œผ๋ฉฐ, ๋ณด์ธ๋‹ค ํ•˜๋”๋ผ๊ณ  ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ๋ณด์ด๋Š” ๊ฒƒ์ด์ง€ ์›์ธ๊ณผ ๊ณผ์ •์€ ๊ฑฐ์˜ ์•Œ ์ˆ˜ ์—†๊ณ , ๊ฐœ๊ฐœ์ธ์— ์†ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์กฐ์ง์˜ ๊ธฐ๋Šฅ๊ณผ ํ”„๋กœ์„ธ์Šค์— ์ข…ํ•ฉ๋˜์–ด ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ์ˆ˜๋งŽ์€ ์š”์†Œ๋“ค์˜ ์ข…ํ•ฉ์— ์˜ํ•ด ๋ฐœํ˜„๋˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๋Ÿฌํ•œ ์ ์€ ์ง€์† ๊ฒฝ์Ÿ์šฐ์œ„๋ฅผ ๋ฒค์น˜๋งˆํ‚นํ•˜๋Š” ์ˆ˜๋งŽ์€ ๊ธฐ์—…๋“ค์ด ์ œ๋Œ€๋กœ ์„ฑ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ•˜์ง€ ๋ชปํ•˜๋Š” ์ด์œ ์ด๊ธฐ๋„ ํ•˜๋‹ค. [1] Resource-based View. ๊ธฐ์—…์˜ ๋‚ด๋ถ€์˜ ์ž์›๊ณผ ์—ญ๋Ÿ‰์„ ๋ถ„์„ํ•˜๊ณ  ๋ณด์œ ํ•œ ์ž์›๊ณผ ์—ญ๋Ÿ‰์„ ๊ทน๋Œ€ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๋Š” ์ „๋žต ๊ฐœ๋ฐœ ๊ด€์ ์˜ ๊ทผ๊ฑฐ๊ฐ€ ๋˜๋Š” ์ด๋ก  [2] ๊ฒฝ์Ÿ์šฐ์œ„(Competitive Advantage) ๊ด€์ ์—์„œ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ๋„ ์žˆ์œผ๋‚˜ ํšŒ์‚ฌ ์ƒํ™œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์ฆ์ ์ธ ๋น„์ฆˆ๋‹ˆ์Šค ๊ต์–‘์„œ๋ฅผ ์ง€ํ–ฅํ•˜๋Š” ๋ณธ์„œ์˜ ํŠน์„ฑ์ƒ ๊นŠ์ด ์„œ์ˆ ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค [3] ์‚ฐ์—…์กฐ์ง๋ก (IO. Industrial Organization)์— ๊ทผ๊ฑฐํ•œ ๋งˆ์ดํด ํฌํ„ฐ์˜ ๊ฒฝ์Ÿ์ „๋žต ์ดํ›„ ์ž์› ์ค€ ๊ฑฐ๋ก (Resource-based view) ๊ธฐ๋ฐ˜์˜ ํ•ต์‹ฌ ์—ญ๋Ÿ‰ ์ด๋ก ์€ 2000๋…„๋Œ€ ์ดˆ๋ฐ˜๊นŒ์ง€ ์ค‘์š”ํ•œ ๊ฒฝ์˜์ „๋žต ์ด๋ก ์ด ๋˜์—ˆ๋‹ค. ์‚ฌ์‹ค ํ•ต์‹ฌ ์—ญ๋Ÿ‰์€ ๊ฐœ๋…์ ์œผ๋กœ๋Š” ๊ณต๊ฐํ•  ์ˆ˜ ์žˆ์–ด๋„ ๊ทธ ์ธก์ •๊ณผ ๊ด€๋ฆฌ์— ์žˆ์–ด์„œ๋Š” ๋งŽ์€ ๋…ผ๋ž€์„ ๋‚ณ์„ ์ˆ˜ ์žˆ๊ณ  ์ด๊ฒฌ๋“ค์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์— ๊ทผ๊ฐ„ํ•œ ์ง€์‹๊ฒฝ์˜๋„ ๊ฐ™์€ ๋ฒ”์ฃผ์˜ ๋„์ „์ด ์žˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ •๋Ÿ‰์  ์„ฑ๊ณผ๋ฅผ ๋…ผ์˜ํ•˜๊ธฐ๋Š” ์–ด๋ ต์ง€๋งŒ ๊ฒฝ์˜์ „๋žต ์ปจ์„คํŒ…์—์„œ๋Š” ์ค‘์š”ํ•œ ์œ„์น˜๋ฅผ ์ฐจ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. [4] ๋‹ค๋ถ„ํžˆ ์ด๋ก ์  ๊ตฌ๋ถ„์ธ๋ฐ ๊ณ„์†ํ•ด์„œ ์„ค๋ช…ํ•˜๊ฒ ์ง€๋งŒ ํ•ต์‹ฌ ์—ญ๋Ÿ‰ ์ด๋ก ์˜ ๊ฐ€์žฅ ํฐ ๋‹จ์ ์€ ์ถ”์ƒ์ ์ด๋ฉฐ ์‹ค์ œ๋กœ ๊ตฌ์ฒดํ™”ํ•˜๊ฑฐ๋‚˜ ๊ทธ ์„ฑ๊ณผ๋ฅผ ์ธก์ •ํ•˜๊ธฐ ์–ด๋ ต๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. [5] ํ•ต์‹ฌ ์—ญ๋Ÿ‰์€ ์ง€์‹์ด๋‚˜ ๊ต์œก๊ณผ ๊นŠ์€ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ทธ ์ดํ›„ ๊ฒฝ์˜ ํŠธ๋ Œ๋“œ๋Š” ์ง€์‹๊ฒฝ์˜(Knowledge Management)์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ฒฝ์˜์„ ๋‹จ์ˆœํžˆ ๊ด€๋ฆฌ์˜ ๊ด€์ ์—์„œ ๋ณด๋Š” ๊ฒƒ์€ ์ข€ ๊ทธ๋ ‡์ง€๋งŒ ํ”ผํ„ฐ ๋“œ๋Ÿฌ์ปค๊ฐ€ ์ฃผ์ฐฝํ–ˆ๋“ฏ์ด ์ธก์ •ํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ด€๋ฆฌํ•  ์ˆ˜ ์—†๊ณ , ๊ด€๋ฆฌํ•  ์ˆ˜ ์—†์œผ๋ฉด ๊ธฐ์—… ์„ฑ๊ณผ์—์„œ๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค. ๋”๊ตฐ๋‹ค๋‚˜ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์€ ํ•ต์‹ฌ ์„ฑ๊ณต์š”์†Œ(KSF.Key Success Factors)์™€ ๊ด€๊ณ„๋„ ๊นŠ๋‹ค. ์‚ฌ๋žŒ๋“ค์ด ๊ด€์‹ฌ์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋ฉด์„œ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์ด๋‚˜ ์ง€์‹๊ฒฝ์˜์€ ๊ทธ ์„ฑ๊ณผ์˜ ์ธก์ • ๋ฐ ๊ด€๋ฆฌ๋ผ๋Š” ๋ถ€๋ถ„์—์„œ ๋งŽ์€ ๋„์ „์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค. [6] ์ด ๊ฐœ๋…์€ ๋˜ ๋‹ค๋ฅธ ๊ด€์ ์—์„œ ํ•ด์„๋˜์–ด ๋งฅํ‚จ์ง€ ์ปจ์„คํŒ…์—์„œ๋Š” โ€˜Three Horizonโ€™์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ธฐ๋„ ํ–ˆ๋‹ค. โ€˜Three Horizonโ€™์€ ์‚ฌ์—…์˜ ์ข…๋ฅ˜๋ฅผ ๊ธฐ๋ฐ˜ ์‚ฌ์—…, ์„ฑ์žฅ์‚ฌ์—…, ๋ฏธ๋ž˜ ์‚ฌ์—…์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ๊ธฐ๋ฐ˜ ์‚ฌ์—…์˜ ์ˆ˜์ต์ด ์„ฑ์žฅ ์‚ฌ์—…๊ณผ ๋ฏธ๋ž˜ ์‚ฌ์—…์„ ์ง€์›ํ•˜๊ณ  ์„ฑ์žฅ ์‚ฌ์—…์€ ์ƒˆ๋กœ์šด ์ž๊ธˆ์›์œผ๋กœ ํ‚ค์šฐ๋ฉฐ ๋ฏธ๋ž˜ ์‚ฌ์—…์€ ์‹ ์‚ฌ์—…์„ ๋‹ค์–‘ํ•œ ์˜ต์…˜์œผ๋กœ ๋ฐ”๋ผ๋ณธ๋‹ค๋Š” ๊ด€์ ์ด๋‹ค. 11. ์‹œ์‚ฌ์  ๋ฐ ๋Œ€์•ˆ์˜ ๋„์ถœ ํ”ํžˆ ์ „๋žต์  ์˜ต์…˜(Strategic Options)์„ ๋„์ถœํ•˜๋Š” ๊ฒƒ์€ ๊ณผํ•™(Science)์ด๋ผ๊ธฐ๋ณด๋‹ค๋Š” ์˜ˆ์ˆ (Art)์ด๋ผ๊ณ  ๋งํ•œ๋‹ค. ๊ทธ๋งŒํผ ๋ถ„์„์˜ ๊ฒฐ๊ณผ์™€ ์‹œ์‚ฌ์ ์„ ๊ฐ€์ง€๊ณ ๋„ ์ „๋žต์  ์˜ต์…˜์„ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š๋‹ค๋Š” ๋ง์ด๋ฉฐ ์‚ฐ์—…์ด๋‚˜ ์—…(ๆฅญ)์— ๋Œ€ํ•œ ํ†ต์ฐฐ๋ ฅ(Insight)์ด ์š”๊ตฌ๋จ์„ ์˜๋ฏธํ•œ๋‹ค. ๋˜ํ•œ, ์˜ต์…˜์ด๋‚˜ ๋Œ€์•ˆ, ์ „๋žต์  ์„ ํƒ์€ ๊ณ ์œ ํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ ๊ณ ๊ฐ์˜ ์‚ฌ์ •์— ๋งž๋Š” ์„ ํƒ์ด ๋˜์–ด์•ผ ํ•˜๋ฉฐ, ๊ทธ๋ž˜์•ผ ์ตœ๋Œ€์˜ ์„ฑ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ๋‹จ์ˆœํžˆ ๊ณผ์ œ(Initiatives)๋“ค์„ ๋„์ถœํ•˜๊ณ  ํ™•์ •ํ•˜๋Š” ๊ฒƒ์— ๋๋‚˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์‹œ๋‚˜๋ฆฌ์˜ค ํ”Œ๋ž˜๋‹(Scenario Planning)์ด๋‚˜ ๋‹ค์–‘ํ•œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜(Simulation)์„ ํ†ตํ•ด ๊ณผ์ œ๊ฐ€ ํ˜„์‹ค์—์„œ ์–ด๋–ป๊ฒŒ ์ง„ํ–‰๋  ๊ฒƒ์ด๋ผ๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ํ™•์‹ ๋„ ํ•„์š”ํ•˜๋‹ค. ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ด ๋‹ค์–‘ํ•œ ๊ด€์ ์—์„œ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์„ ํƒ์ง€๋“ค์„ ๋„์ถœํ•˜๊ฒŒ ๋˜๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ์ „๋žต์  ์„ ํƒ์„ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ Table III-33๊ณผ ๊ฐ™์ด ์ „๋žต์  ํฌ์ง€์…˜์— ๋”ฐ๋ผ ๋ฏธ๋ž˜ ํฌ์ง€์…”๋‹์„ ์œ„ํ•œ ์„ ํƒ์ง€๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. Table III-33. ์ „๋žต์  ์„ ํƒ์˜ ์‚ฌ๋ก€ (O: ์œ ๋ง, X: ์œ ๋งํ•˜์ง€ ์•Š์Œ) ๋˜ํ•œ, ํ˜„์žฌ ๋ณด์œ ํ•œ ์—ญ๋Ÿ‰์ด๋‚˜ ์„ค๋ฃจ์…˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒˆ๋กœ์šด ์‚ฌ์—… ๊ธฐํšŒ์— ๋Œ€ํ•œ ์„ ํƒ์„ ๊ฒฐ์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.[1] ๋งคํŠธ๋ฆญ์Šค๋ฅผ ํ˜„์žฌ ์—ญ๋Ÿ‰์˜ ๋ณด์œ  ์ˆ˜์ค€๊ณผ ๊ธฐ์—…์˜ ํ•ต์‹ฌ ์‚ฌ์—…์ด๋ƒ ์•„๋‹ˆ๋ƒ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉด Figure III-51๊ณผ ๊ฐ™์€ ์—ญ๋Ÿ‰-์‚ฌ์—… ๋งคํŠธ๋ฆญ์Šค๋ฅผ ๊ตฌ์„ฑํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Figure III-51. ์—ญ๋Ÿ‰/์„ค๋ฃจ์…˜-์‚ฌ์—… ๋งคํŠธ๋ฆญ์Šค Part III์˜ ๋งˆ์ง€๋ง‰ ์ œ11์žฅ์—์„œ๋Š” ์•ž์„œ ์†Œ๊ฐœํ•˜์˜€๋˜ ๋‹ค์–‘ํ•œ ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์„ ํ†ตํ•ด ๋„์ถœ๋œ ์‹œ์‚ฌ์ ๋“ค์„ ์ข…ํ•ฉํ•˜์—ฌ ๋Œ€์•ˆ(Options or Alternatives)์„ ๋„์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์ž. Figure III-52. ์‹œ์‚ฌ์  ๋ฐ ๋Œ€์•ˆ ๋„์ถœ ๋กœ๋“œ๋งต 11.1 ์‚ฌ์—… ๋งค๋ ฅ๋„ ํ‰๊ฐ€ ์‚ฌ์—… ๋งค๋ ฅ๋„ ํ‰๊ฐ€๋Š” ๋‹ค์ฐจ์› ํ‰๊ฐ€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ณด๋ฉด ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ˆ˜ํ–‰ํ–ˆ๋˜ ๋ชจ๋“  ๋ถ„์„ ๊ธฐ๋ฒ•๋“ค์— ๋Œ€ํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ์ •ํ•˜๊ณ  Table III-34์™€ ๊ฐ™์ด ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Table III-34. ํŠน์ • ์˜ต์…˜์— ๋Œ€ํ•œ ๋งค๋ ฅ๋„ ํ‰๊ฐ€ ์‚ฌ๋ก€ Table III-34๋ฅผ ๋ณด๋ฉด ๋†’์€ ๊ฐ€์ค‘์น˜๋ฅผ ๋‘๊ณ  ์žˆ๋Š” ์‹œ์žฅ ๊ทœ๋ชจ์™€ ๊ณ ๊ฐ ์ˆ˜์š” ๋ถ€๋ถ„์˜ ํ‰๊ฐ€์—์„œ ๋†’์€ ํ‰๊ฐ€๋ฅผ ๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ์‚ฌ์—…์€ ๋งค๋ ฅ์ ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ์‚ฌ์—…์€ ๊ฒฝ์Ÿ ๊ฐ•๋„๊ฐ€ ๊ฐ•ํ•˜๊ณ  ๊ธฐ์ˆ ์  ์š”๊ตฌ ์‚ฌํ•ญ๋„ ๋†’์€ ์‚ฌ์—…์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์—… ๋งค๋ ฅ๋„ ํ‰๊ฐ€์˜ ๊ธฐ์ค€์€ Table III-34์— ๋‚˜ํƒ€๋‚œ ๊ฒƒ ์ด์™ธ์—๋„ ๋งค์šฐ ๋‹ค์–‘ํ•œ ํ•ญ๋ชฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ๊ณ  ๊ทธ ๊ฐ€์ค‘์น˜ ์—ญ์‹œ ๊ณ ๊ฐ์˜ ์‚ฌ์—… ์ƒํ™ฉ์ด๋‚˜ ์‹œ์žฅ์ด๋‚˜ ๊ณ ๊ฐ์— ๋Œ€ํ•œ ์ธ์‹์˜ ์ฐจ์ด์— ๋”ฐ๋ผ ๋ณ€ํ™”๋  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ์ด ๊ฒฝ์šฐ์—์„œ ์ ์šฉ๋œ ๊ฒƒ์ด ์ € ๊ฒฝ์šฐ์—๋„ ๋ฐ˜๋“œ์‹œ ๊ฐ™์œผ๋ฆฌ๋ผ๋Š” ๋ฒ•์ด ์—†์Œ์„ ์œ ๋…ํ•ด์•ผ ํ•œ๋‹ค. ์•„์šธ๋Ÿฌ ํ•ด๋‹น ๊ณ ๊ฐ๋งŒ์˜ ๊ด€์ ์„ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ„์„ ํ•ญ๋ชฉ์ด๋‚˜ ์ฒ™๋„๋ฅผ ๊ฐœ๋ฐœํ•ด ๋‚ด๋Š” ๊ฒƒ์€ ์ปจ์„คํ„ดํŠธ์˜ ์œ ๋Šฅํ•จ์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๊ธฐ๋„ ํ•˜๋‹ค. Table III-35๋Š” ์‚ฌ์—… ๋งค๋ ฅ๋„ ํ‰๊ฐ€์˜ ์žฅ๋‹จ์ ์„ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด๋‹ค. Table III-35. ์‚ฌ์—… ๋งค๋ ฅ๋„ ํ‰๊ฐ€์˜ ์žฅ๋‹จ์  11.2 ์ˆœํ˜„์žฌ๊ฐ€์น˜ ํ‰๊ฐ€ ์ˆœํ˜„์žฌ๊ฐ€์น˜(NPV: Net Present Value)๋Š” ๋ฏธ๋ž˜์˜ ํŠน์ • ์‹œ์ ์— ๋ฐœ์ƒ๋  ๋น„์šฉ์ด๋‚˜ ์ˆ˜์ต์„ ํ˜„์žฌ ์‹œ์ ์—์„œ์˜ ๊ฐ€์น˜๋กœ ํŒŒ์•…ํ•˜๋Š” ์ผ๋กœ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ „์ œ๋Š” ํ˜„์žฌ์˜ 10,000์›๊ณผ 10๋…„ ๋’ค์˜ 10,000์›์˜ ๊ฐ€์น˜๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค. ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์ด๋‚˜ ์ธํ”Œ๋ ˆ์ด์…˜ ๋“ฑ ๋‹ค์–‘ํ•œ ์ด์œ ๋กœ ํ™”ํ ๊ฐ€์น˜๋Š” ๋‹ฌ๋ผ์ง€๋Š”๋ฐ NPV์—์„œ๋Š” ํ• ์ธ์œจ์— ์˜ํ•ด ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค๊ณ  ํ•˜๋ฉฐ ๋ณดํ†ต ํ• ์ธ์œจ์€ ์‹œ์žฅ์ด์ž์œจ ์ •๋„๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 3.0% ์ด์ž์œจ๋กœ ์€ํ–‰์— 1๋…„ ๋™์•ˆ 1์–ต ์›์„ ์˜ˆ์น˜์‹œํ‚จ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด ์ผ ๋…„ ๋’ค์— ๋‚ด๊ฐ€ ์ฐพ์„ ๊ธˆ์•ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์›๊ธˆ + ์ด์ž= ์›๊ธˆ 1์–ต ์›+ (1์–ต ์› X 3.0%) = 1์–ต ์› X (1 + 3.0%) โ€ฆ (1) ์ด๋ ‡๊ฒŒ ์–ป์–ด์ง„ ์›๊ธˆ๊ณผ ์ด์ž์˜ ํ•ฉ์ธ (1)์„ ๋‹ค์‹œ 1๋…„๊ฐ„ ์ด์ž์œจ 3%๋กœ ๋งก๊ฒจ๋‘”๋‹ค๋ฉด ์›๊ธˆ + ์ด์ž= {1์–ต ์› X (1+3.0%)} + [{1์–ต ์› X (1+3.0%)}X3.0%] = {1์–ต ์› X (1+3.0%)}(1+3.0%) = 1์–ต ์› X(1+3.0%) 2 ์ด๋ฅผ ์ข€ ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด ํ™”ํ์˜ ๋ฏธ๋ž˜๊ฐ€์น˜(FV)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. (PV๋Š” ํ˜„์žฌ๊ฐ€์น˜, n์€ ๊ธฐ๊ฐ„) Table III-36์€ ์ˆœํ˜„์žฌ๊ฐ€์น˜ ํ‰๊ฐ€์˜ ์žฅ๋‹จ์ ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค.[2] Table III-36. ์ˆœํ˜„์žฌ๊ฐ€์น˜ ํ‰๊ฐ€์˜ ์žฅ๋‹จ์  ์ด์ƒ์œผ๋กœ Part III. ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์•˜๋‹ค. ๋„๋ฆฌ ์•Œ๋ ค์ง„ ๋Œ€ํ‘œ์ ์ธ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•๋“ค์„ ์†Œ๊ฐœํ•˜์˜€๊ณ  ํ•ด๋‹น ์‚ฐ์—…์ด๋‚˜ ์—…์ข…์— ๋”ฐ๋ฅธ ๋‹ค์–‘ํ•œ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•๋“ค์ด ๋งŽ์ด ์žˆ๋‹ค. ํŠนํžˆ, ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ ์ฒด๊ณ„๋ฅผ ๋„˜์–ด์„œ์„œ ํ†ต๊ณ„๋ฅผ ํ™œ์šฉํ•œ ๊ณผํ•™์  ๊ธฐ๋ฒ•์˜ ์˜์—ญ์œผ๋กœ ๋“ค์–ด๊ฐ€๋ฉด ๋‹ค์–‘ํ•œ ํ†ต๊ณ„๊ธฐ๋ฒ• ๋ฐ ์ฒ˜๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ˆ˜์น˜ํ•ด์„ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์ด ์žˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ ๋‹ค๋ฅธ ์ €์„œ๋ฅผ ์ฐธ๊ณ ํ•  ํ•„์š”๋„ ์žˆ๊ฒ ๋‹ค. ํ•œํŽธ, Part II์—์„œ ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  ์ฒด๊ณ„์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์•˜๊ณ , Part III์—์„œ ๊ทธ๊ฒƒ๋“ค์„ ๊ธฐ๋ฐ˜์— ๋‘” ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•๋“ค์„ ์‚ดํŽด๋ณด์•˜๋‹ค. Part IV์—์„œ๋Š” ์‹ค์งˆ์ ์œผ๋กœ ์ด๋Ÿฐ ๋‹ค์–‘ํ•œ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•๋“ค์ด ํ”„๋กœ์„ธ์Šค์™€ ๋งŒ๋‚ฌ์„ ๋•Œ ๋˜ ํ…œํ”Œ๋ฆฟํ™” ๋˜์—ˆ์„ ๋•Œ ํ”ํžˆ ๋ฐฉ๋ฒ•๋ก ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์†Œํ”„ํŠธ์›จ์–ด์˜ ์˜คํ”ˆ ์†Œ์Šค์ฒ˜๋Ÿผ ๋ฐฉ๋ฒ•๋ก ๋„ ๊ณต๊ฐœ๋œ ๊ฒƒ์ด ์žˆ์œผ๋ฉด ์ข‹์„ ํ…๋ฐ ์ด๊ฒƒ์ด์•ผ๋ง๋กœ ์ปจ์„คํŒ… ํšŒ์‚ฌ๋“ค์˜ ์ž์‚ฐ์ด๋ผ์„œ ๊ณต๊ฐœํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ €์ž๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” ๋ช‡๋ช‡ ๋ฐฉ๋ฒ•๋ก ๋„ ๋ผ์ด์„ ์Šค ์ด์Šˆ๋กœ ๊ทธ๊ฒƒ์„ ์ƒ์„ธํžˆ ์„ค๋ช…ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํฐ ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ ์ด๋Ÿฐ ๊ฒƒ์ด ์žˆ๋‹ค ์ •๋„๋Š” ์•Œ๋ฉด ์ปจ์„คํŒ… ์‚ฌ์—…์„ ์ดํ•ดํ•˜๋Š”๋ฐ ๋งŽ์€ ๋„์›€์ด ๋  ๊ฒƒ์ด๋ผ ์ƒ๊ฐ๋œ๋‹ค. Part IV์—์„œ๋Š” ๊ทธ๊ฒƒ๋“ค์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. [1] RBV ๊ด€์ ์˜ ๋Œ€์‘์ด๋‹ค. ์˜ค๋Š˜๋‚ ์€ ๋‚ด๋ถ€ ์—ญ๋Ÿ‰์— ๊ธฐ๋ฐ˜ํ•œ ์„ฑ์žฅ(Organic Growth) ๋งŒ์œผ๋กœ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ๊ด€์ ์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค๋ฅธ ์˜๊ฒฌ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. [2] ์ˆ˜์ต์„ฑ ๋ถ„์„ ๋ฐ ์ˆœํ˜„์žฌ๊ฐ€์น˜ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์„ ํ™œ์šฉํ•œ ํˆฌ์ž ์„ฑ๊ณผ ํ‰๊ฐ€์— ๊ด€ํ•œ ๊ฒƒ์€ IV. ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ์˜ ์ œ4์žฅ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์—์„œ ์ƒ์„ธํžˆ ๋‹ค๋ฃฌ๋‹ค. 12. ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ๋ฐฉ๋ฒ•๋ก ์ด๋ž€? ์ปจ์„คํŒ…๋„ ํ”„๋กœ์ ํŠธ์ด๋ฏ€๋กœ ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ์˜ ๊ฐ€์žฅ ๊ทผ๊ฐ„์ด ๋˜๋Š” ๊ฒƒ์€ ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ(Project Management) ๋ฐฉ๋ฒ•๋ก ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ”„๋กœ์ ํŠธ์— ๊ด€ํ•ด์„œ๋Š” PMI[1]์™€ ๊ฐ™์€ ์ „๋ฌธ๊ต์œก๊ธฐ๊ด€์ด<NAME>๊ณ  ์žˆ์–ด ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์ฐธ๊ณ ํ•˜๊ณ  ์žˆ๋‹ค. ์ด ์žฅ์—์„œ๋Š” PMI์˜ ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์ผ๋ถ€ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•œ๋‹ค. ์šฐ์„ , ํ”„๋กœ์ ํŠธ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ƒ๊ฐํ•ด ๋ณด์•„์•ผ ํ•œ๋‹ค. ๋‹ค์–‘ํ•œ ์‚ฐ์—…์€ ๋ฌผ๋ก  ์ดˆ๋“ฑํ•™๊ต์—์„œ๋„ ์‚ฌ์šฉํ•  ์ •๋„๋กœ ๋งŽ์ด ๋“ฃ๋˜ ๋‹จ์–ด์ธ๋ฐ ์ด๋ฅผ ์ •์˜ํ•ด ๋ณด๋ผ ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•˜์ง€ ๋ชปํ•œ๋‹ค. ๋งค์šฐ ๋‹ค์–‘ํ•œ ๊ด€์ ์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ ๊ทธ์ค‘ ๋น ์ง€์ง€ ๋ง์•„์•ผ ํ•  ์„ธ ๊ฐ€์ง€ ์†์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ผ์‹œ์ (Temporary) ๊ณ ์œ ํ•จ(Unique) ๊ฒฐ๊ณผ๋ฌผ(Deliverables) ํ”„๋กœ์ ํŠธ๋Š” ์ผ์‹œ์ ์ด๋‹ค. ์ฆ‰, ์‹œ์ž‘๊ณผ ๋์ด ์žˆ์–ด์„œ ํ•ด๋‹น ๊ธฐ๊ฐ„ ์•ˆ์— ํ”„๋กœ์ ํŠธ๋ฅผ ๋๋‚ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ, ํ”„๋กœ์ ํŠธ๋Š”<NAME>๋‹ค. ์–ด๋–ค ํ”„๋กœ์ ํŠธ๋„ ๋™์ผํ•œ ๊ฒƒ์€ ์—†๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋“  ํ”„๋กœ์ ํŠธ๋Š” ๊ฒฐ๊ณผ๋ฌผ์ด ์žˆ๋‹ค. ๋„๋กœ๊ฐ€ ์ƒ๊ธฐ๊ณ  ๋ฐœ์ „์†Œ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋„ ๋ฌธ์„œ ๊ธฐ๋ฐ˜์˜ ๋‹ค์–‘ํ•œ ์‚ฐ์ถœ๋ฌผ์ด ๋‚จ๋Š”๋‹ค. ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ๋ฐฉ๋ฒ•๋ก ์ด๋ผ๋Š” ๊ฒƒ์€ ์ด๋Ÿฐ ์†์„ฑ๋“ค์„ ๊ฐ€์ง„ ํ”„๋กœ์ ํŠธ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์šด์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€์ด๋“œ(guide)์ด ์ž ํ…œํ”Œ๋ฆฟ(template), ์‚ฌ๋ก€(Best practice)๋ฅผ ํฌํ•จํ•œ ๊ฒƒ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. Figure IV-3. ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ๋ฐฉ๋ฒ•๋ก ์˜ ๊ฐœ๋… ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ๋ฐฉ๋ฒ•๋ก ์€ Figure IV-3๊ณผ ๊ฐ™์ด 5๊ฐœ์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ณ  ์žˆ๋‹ค. ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐํ™” (Initiating)๋Š” ํ”„๋กœ์ ํŠธ ์ถ”์ง„์„ ์œ„ํ•œ ๊ฐ์ข… ์ค€๋น„ ๋‹จ๊ณ„๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ”„๋กœ์ ํŠธ ๊ณ„ํš(Planning) ๋‹จ๊ณ„๋Š” ์ผ์ • ๋ฐ ๋น„์šฉ ๊ณ„ํš, ์ถ”์ง„ ์กฐ์ง์„ ํ™•์ •ํ•˜๋Š” ๋‹จ๊ณ„์ด๊ณ  ์‹คํ–‰(Executing) ๋‹จ๊ณ„๋Š” ์ •ํ•ด์ง„ ์ ˆ์ฐจ์— ๋”ฐ๋ผ ์ผ์„ ์ง„ํ–‰ํ•˜๋Š” ๋‹จ๊ณ„, ์ข…๋ฃŒ(closing)์™€ ๋ชจ๋‹ˆํ„ฐ๋ง ๋‹จ๊ณ„๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. 12.1 ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ์˜ ๊ฐœ๋… ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ๋Š” Figure IV-3์— ์†Œ๊ฐœ๋œ ํ”„๋กœ์ ํŠธ ์—…๋ฌด ๊ทธ๋ฃน์„ ์ข€ ๋” ์ƒ์„ธํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ํ˜‘ํšŒ(PMI)์—์„œ๋Š” ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ์—…๋ฌด๋ฅผ Table IV-1๊ณผ ๊ฐ™์ด 9๊ฐ€์ง€ ๋ฒ”์ฃผ[1]๋กœ ๋‚˜๋ˆ„์–ด ์†Œ๊ฐœํ•˜๊ณ  ์žˆ๋‹ค. Table IV-1. ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ์˜ 9๊ฐ€์ง€ ๊ด€์  ๋ฏธ์…˜(mission)์€ ๋ช…ํ™•ํ•˜์ง€๋งŒ ์œ ํ•œํ•œ ์ž์›์„ ๊ฐ€์ง€๊ณ  ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋Š” ๊ทธ ์ •์˜์ฒ˜๋Ÿผ ๊ฐ ์˜์—ญ๋ณ„ ๊ด€๋ฆฌ๊ฐ€ ์ฒ ์ €ํ•˜๊ฒŒ ์ง„ํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค. ๋ฒ”์œ„ ๊ด€๋ฆฌ์—์„œ๋Š” ์ „์ฒด ์ผ์˜ ๋ฒ”์œ„๋ฅผ ์ •์˜ํ•˜๋Š” ์ผ์„ ํ•œ๋‹ค. WBS(Work Break down Structures)๋ผ ๋ถˆ๋ฆฌ๋Š” ์ž‘์—… ๋ถ„๋ฅ˜์ฒด๊ณ„๋ฅผ ๋งŒ๋“ค์–ด ๊ทธ ์ˆœ์„œ๋Œ€๋กœ ์ผ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. WBS๋Š” ์ผ์˜ ์ˆ˜ํ–‰ ์ฃผ์ฒด์™€ ๊ธฐ๊ฐ„์ด ํ‘œ๊ธฐ๋œ๋‹ค. ๊ฐ„ํŠธ(Gantt) ์ฐจํŠธ[3]<NAME>์„ ์ด์šฉํ•ด์„œ ๋ˆ„๊ฐ€, ์–ธ์ œ ๋ฌด์—‡์„ ์‹œ์ž‘ํ•˜๊ณ  ๋๋‚ด๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ์–ด๋Š ์‹œ์ ์— ๋ฌด์—‡์„ ์ ๊ฒ€ํ•˜๋Š”์ง€ ๋“ฑ ๋งˆ์ผ์Šคํ†ค(Milestone) ๊ฐ™์€ ๊ฒƒ์„ ํ‘œ๊ธฐํ•˜๊ณ  ๊ด€๋ฆฌํ•œ๋‹ค. Figure IV-4. ๊ฐ„ํŠธ์ฐจํŠธ ์‚ฌ๋ก€ ๋ฒ”์œ„์™€ ์ผ์ • ๊ด€๋ฆฌ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ PERT/CPM ๊ธฐ๋ฒ•[4]์— ๊ทผ๊ฐ„์„ ๋‘๊ณ  ์žˆ๋‹ค. ์ž์›๊ณผ ์‹œ๊ฐ„์ด ๋ฐฐ์ •๋˜๋ฉด ๊ทธ์— ๋”ฐ๋ฅธ ๋น„์šฉ์„ ๊ด€๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ์— ์ฃผ์–ด์ง„ ์˜ˆ์‚ฐ์€ ์–ผ๋งˆ์ธ๋ฐ ์ง‘ํ–‰๋˜๋Š” ๋น„์šฉ์€ ์–ผ๋งˆ์ด์–ด์„œ ๊ฒฐ๊ณผ์ ์œผ๋กœ ํ”„๋กœ์ ํŠธ ์†์ต์€ ํ‘์ž์ด๋‹ค ๋˜๋Š” ์ ์ž์ด๋‹ค. ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๋น„์šฉ ๋ฐœ์ƒ์„ ๊ณ ๋ คํ•˜์—ฌ ์šฐ๋ฐœ๋น„์šฉ(Contingency Cost)๋ฅผ ์žก๊ธฐ๋„ ํ•˜๋ฉฐ ๊ฐ ๊ณต์ •๋งˆ๋‹ค ์ œ๋Œ€๋กœ ๋น„์šฉ์ด ์ง‘ํ–‰๋˜๊ณ  ๋งค์ž…, ๋งค์ถœ์ด ๋ฐœ์ƒํ•˜๋Š”์ง€ ๋ชฉํ‘œ ๋Œ€๋น„ ์ฐจ์ด(Variance)๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ํ•œํŽธ, ํ”„๋กœ์ ํŠธ๋ฅผ ์ถ”์ง„ํ•  ํŒ€์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์ธ์›์„ ์ˆ˜๋ฐฐํ•˜๊ณ  ์—ญํ• ๊ณผ ์ฑ…์ž„์„ ๋ถ„๋‹ดํ•˜๋Š” ์ผ๋„ ๋งค์šฐ ์ค‘์š”ํ•˜๋ฉฐ ํ”„๋กœ์ ํŠธ์— ํ•„์š”ํ•œ ์žฅ๋น„๋ฅผ ๊ตฌ๋งคํ•˜๊ณ  ๊ณ„์•ฝํ•˜๋Š” ์ผ๋„ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ์ด ๋ชจ๋“  ์ผ๋“ค์ด ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ๊ณผ์ •์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ทธ์™€ ๊ด€๋ จํ•˜์—ฌ ์œ„ํ—˜ ์š”์†Œ๋ฅผ ์‹๋ณ„ํ•˜๊ณ  ์ด๋ฅผ ์ •์„ฑ์ /์ •๋Ÿ‰์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ํ•„์ˆ˜์ด๋‹ค. ์•„์šธ๋Ÿฌ ์ด๋Ÿฐ ๊ฒƒ๋“ค์€ ๋‹ค์–‘ํ•œ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜<NAME>์„ ๋นŒ๋ ค ๊ณต์œ ๋˜๊ณ  ๊ด€๋ฆฌ๋˜์–ด์•ผ ํ•œ๋‹ค. ํ•œ๋งˆ๋””๋กœ ํ”„๋กœ์ ํŠธ๋Š” ์ข…ํ•ฉ์˜ˆ์ˆ ์„ ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์•„์„œ PM์€ ์ •๋ง ์Šˆํผ๋งจ์ด ๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ๋ถ€๋‹ด๋„ ๋งŽ์ด ๊ฐ€์ง„๋‹ค. Break #17. ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณด๋Š” ์‹œ๊ฐ ์ •๋ณด์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ•˜๋Š” ํ”„๋กœ์ ํŠธ์™€ ๊ด€๋ จํ•ด์„œ๋Š” Figure IV-5์™€ ๊ฐ™์€ ์œ ๋ช…ํ•œ ์ด์•ผ๊ธฐ๊ฐ€ ์žˆ๋‹ค. ๋‚ด์šฉ์€ ๊ณ ๊ฐ์€ ๋‚˜๋ฌด์—์„œ ๊ทธ๋„ค๊ฐ€ ํƒ€๊ณ  ์‹ถ๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ๊ฒƒ์— ๋Œ€ํ•ด ํ”„๋กœ์ ํŠธ ๋งค๋‹ˆ์ €(PM)๊ฐ€ ์ดํ•ดํ•œ ๊ฒƒ, ์—…๋ฌด ๋ถ„์„๊ฐ€๊ฐ€ ์ดํ•ดํ•œ ๊ฒƒ, ๊ฐœ๋ฐœ์ž๊ฐ€ ์ดํ•ดํ•œ ๊ฒƒ์ด ๊ฐ๊ฐ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ฒฐ๊ตญ ๊ณ ๊ฐ์ด ๋‚˜๋ฌด์—์„œ ๊ทธ๋„ค๋ฅผ ํƒ€๊ณ  ์‹ถ๋‹ค๊ณ  ํ•  ๋•Œ๋Š” ๊ทธ๋ƒฅ ํƒ€์ด์–ด ํ•˜๋‚˜ ๋งค๋‹ฌ์•„๋„ ๋  ๊ฒƒ์„ Figure IV-5์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ชจ์–‘์˜ ๊ฒฐ๊ณผ๋ฌผ์ด ๋‚˜์˜ค๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด๋Ÿฐ ํ˜„์ƒ์€ ์‹ค์ œ๋กœ ์ •๋ณด์‹œ์Šคํ…œ ๊ฐœ๋ฐœ ํ˜„์žฅ์—์„œ ์‹ฌ์‹ฌ์น˜ ์•Š๊ฒŒ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ๋‹ค. ๊ณ ๊ฐ์˜ ๋งŒ์กฑ๋„๋ฅผ ๋†’์ด๊ณ  ์‹ถ๋‹ค๋ฉด ๊ณ ๊ฐ์ด ์ •ํ™•ํ•˜๊ฒŒ ๋ฌด์—‡์„ ์›ํ•˜๋Š”์ง€๋ฅผ ํŒŒ์•…ํ•ด์•ผ ํ•˜๊ณ  ๋˜ ๊ทธ๊ฒƒ์„ ์ •ํ™•ํ•˜๊ฒŒ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค. Figure IV-5. ์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ๊ณผ ์š”๊ตฌ์‚ฌํ•ญ์˜ ๊ดด๋ฆฌ 1.2 PMO์˜ ์—ญํ• ๊ณผ ์ฑ…์ž„ ํ”„๋กœ์ ํŠธ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๊ฑฐ๋‚˜ ๋‹ค์ˆ˜์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ๊ด€๋ฆฌํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์ƒ๊ธธ ๋•Œ์—๋Š” ๊ณ ๊ฐ์€ PMO ์„œ๋น„์Šค๋ฅผ ์š”์ฒญํ•˜๊ธฐ๋„ ํ•œ๋‹ค. PMO๋Š” โ€˜Project Management Officeโ€™์˜ ์•ฝ์ž๋กœ ํ”„๋กœ์ ํŠธ ํŒ€๊ณผ๋Š” ๋ณ„๋„๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ์ด๋Œ๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•˜๊ณ  ์ด๋ฅผ ์ง€์›ํ•˜๋Š” ์‚ฌ์—… ๊ด€๋ฆฌ ์กฐ์ง์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ตญ๋‚ด ๊ธˆ์œต ๋Œ€ํ˜• IT ์‚ฌ์—…์˜ ๊ฒฝ์šฐ, ์ปจ์„คํ„ดํŠธ๋“ค์ด PMO๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ์ด๋ฅผ ๋งŽ์ด ์ง€์›ํ•˜๋ฉฐ ๋™๋‚จ์•„ ๋“ฑ ๊ฐœ๋ฐœ๋„์ƒ๊ตญ์˜ ๋Œ€ํ˜• SOC ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ๋„ ์ „๋ฌธ๊ฐ€์™€ ์ปจ์„คํ„ดํŠธ๋“ค์ด PMO๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ/ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ๊ฐ์ข… ๋ณด๊ณ  ์™€ ์‚ฐ์ถœ๋ฌผ, ์ง„์ฒ™ ์ƒํ™ฉ ๋“ฑ ์ฑ™๊ฒจ์•ผ ํ•  ๊ฒƒ๋“ค์ด ๋งŽ์•„์„œ ๊ณ ๊ฐ์ด ์ด๋ฅผ ๊ฐ๋‹นํ•˜๊ธฐ ์–ด๋ ค์šธ ๋•Œ ๊ทธ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ์ „๋ฌธ๊ฐ€๋“ค์„ ๊ณ ์šฉํ–ˆ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. PMO์˜ ๊ด€์ ์€ ํ”„๋กœ์ ํŠธ์™€ ๊ฑฐ์˜ ์œ ์‚ฌํ•˜๋‚˜ ๊ทธ ์ผ์˜ ๊นŠ์ด์™€ ๋ฒ”์œ„๊ฐ€ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. Table IV-2๋Š” ํ”„๋กœ์ ํŠธ ํŒ€๊ณผ PMO๊ฐ€ ํ”„๋กœ์ ํŠธ ๊ฐ ์˜์—ญ์—์„œ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋Š” ์ผ์„ ๋น„๊ตํ•œ ๊ฒƒ์ด๋‹ค. Table IV-2. PMO์™€ ํ”„๋กœ์ ํŠธ ํŒ€์˜ ์—…๋ฌด ๋น„๊ต PMO๊ฐ€ ์™œ ํ•„์š”ํ• ๊นŒ? ๊ธˆ์œต๊ถŒ ์ฐจ์„ธ๋Œ€ IT ํ”„๋กœ์ ํŠธ ๋“ฑ์„ ๋„˜์–ด์„œ ๋Œ€ํ˜• ๊ฑด์„ค ํ”„๋กœ์ ํŠธ ๋“ฑ์—์„œ๋„ PMO์˜ ํ•„์š”์„ฑ์€ ์ตœ๊ทผ ๋”์šฑ ๋ถ€๊ฐ๋˜๊ณ  ์žˆ๋‹ค. ์ตœ๊ทผ ๋งŽ์ด ํšŒ์ž๋˜๊ณ  ์žˆ๋Š” ์‚ฌ๋ก€๋กœ ์†กํŒŒ ๊ฐ€๋“ ํŒŒ์ด๋ธŒ ๊ฑด์„ค์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ๋“ค์ด ์ง€์ ๋˜๊ณ  ์žˆ์ง€๋งŒ ์ฃผ์š” ์—”ํ„ฐํ‹ฐ(Entity)๊ฐ„์— ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์•„ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ๊ฐ€ ๋งค์šฐ ํฌ๋‹ค๊ณ  ํ•œ๋‹ค. ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์ž˜ ์•Œ๊ณ  ์žˆ๋“ฏ์ด ์†กํŒŒ ๊ฐ€๋“ ํŒŒ์ด๋ธŒ๋Š” ์ฒญ๊ณ„์ฒœ ๋ณต์› ๋‹น์‹œ ์ฃผ๋ณ€ ์ƒ๊ฐ€์˜ ์ด์ฃผ ๋Œ€์ฑ…์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฑด๋ฌผ์ด๋‹ค. ๊ฑด๋ฌผ ์ž์ฒด๋Š” ๋งŽ์€ TV ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ๋งค์šฐ ์›…์žฅํ•˜๋ฉฐ ํ›Œ๋ฅญํ•œ ์™ธ๊ด€์„ ์ž๋ž‘ํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์ด ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณธ๋ž˜์˜ ๋ชฉ์ ์ธ โ€˜์ฒญ๊ณ„์ฒœ ์ƒ๊ฐ€์˜ ์ƒ์ธ๋“ค์˜ ์ด์ฃผโ€™๋ผ๋Š” ๊ด€์ ์—์„œ ๋ณด๋ฉด 30%์— ๋ฏธ์น˜์ง€ ๋ชปํ•˜๋Š” ์ด์ฃผ์œจ์ด ๋ณด์—ฌ์ฃผ๋“ฏ์ด ์†กํŒŒ ๊ฐ€๋“ ํŒŒ์ด๋ธŒ ํ”„๋กœ์ ํŠธ๋Š” ์‹คํŒจํ–ˆ๋‹ค๋Š” ๊ฒƒ์— ๋™์˜ํ•œ๋‹ค. ๊ทธ ์›์ธ์„ ์‚ดํŽด๋ณด๋ฉด ๋ฐœ์ฃผ์ฒ˜์™€ ์ž…์ฃผ๋ฏผ, ์‹œ๊ณต์‚ฌ ์‚ฌ์ด์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์— ๋ฌธ์ œ๊ฐ€ ๋งŽ์•˜๋˜ ๊ฒƒ์œผ๋กœ ํŒŒ์•…๋œ๋‹ค. ์›๋ž˜ ํ”„๋กœ์ ํŠธ์˜ ๋ฐœ์ฃผ์ฒ˜๋Š” ์„œ์šธ์‹œ์˜€์ง€๋งŒ ์ด ๋ณตํ•ฉ๋‹จ์ง€ ๊ฑด์„ค์„ ์œ„ํ•œ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋‚ธ ์‚ฌ๋žŒ๋“ค์€ ์ฒญ๊ณ„์ฒœ ์ƒ๊ฐ€ ์ด์ฃผ๋ฏผ ํ˜‘์˜์ฒด์˜€๋‹ค. ์ฆ‰ ์‹ค์งˆ์ ์ธ ๊ณ ๊ฐ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด์ฃผ๋ฏผ ํ˜‘์˜์ฒด๋Š” ๋ฉ‹์ง„ ๊ฑด๋ฌผ์ด ๋˜๊ธฐ๋ฅผ ๋ฐ”๋ผ๋Š” ๋งˆ์Œ์— ์ด๋Ÿฐ์ €๋Ÿฐ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋‚ด์—ˆ๊ณ  ์‹œ๊ณต์‚ฌ๋Š” ์ด๋ฅผ ์ถฉ์‹คํžˆ ์ˆ˜ํ–‰ํ–ˆ์ง€๋งŒ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ž…์ฃผ ๋ถ€๋‹ด๊ธˆ์ด ๋†’์•„์ ธ ์ž…์ฃผํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜์–ด๋ฒ„๋ฆฐ ๊ฒƒ์ด๋‹ค. Figure IV-6. ์†กํŒŒ ๊ฐ€๋“ ํŒŒ์ด๋ธŒ ๊ตฌ์ƒ๋„ ์„ค๊ณ„์—์„œ๋„ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ ๋ณต๊ฐœ ์ „ ์ฒญ๊ณ„์ฒœ ์ฃผ๋ณ€ ์ƒ๊ฐ€์˜ ๋Œ€๋ถ€๋ถ„์€ โ€˜๋งˆ์ง€๊ผฌ๋ฐ”(็”บๅทฅๅ ด(ใพใกใ“ใ†ใฐ))[5]โ€™๋ผ๊ณ  ๋ถˆ๋ฆฌ๋˜ ์†Œ๊ทœ๋ชจ ๊ธฐ๊ณ„๊ฐ€๊ณต ์—…์ฒด๋“ค์ด ๋งŽ์•˜๋‹ค. ๊ทธ๋ฆผ ๊ทธ๋ ค๋‹ค ์ฃผ๋ฉด ๋ญ๋“  ๋š๋”ฑ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋„๊นจ๋น„๋ฐฉ๋ง์ด ๊ฐ™์€ ๊ธฐ์—…๋“ค์ด์—ˆ์ง€๋งŒ ์ด๋“ค์€ ์˜์„ธํ–ˆ๊ณ  ๋ณตํ•ฉ์œ ํ†ต๋‹จ์ง€๋ฅผ ๋ชฉํ‘œ๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฑด๋ฌผ ์•ˆ์— ๊ธฐ๋ฆ„๋•Œ ๋ฌป์€ ์ž์‹ ๋“ค์˜ ์žฅ๋น„๋ฅผ ๋†“๊ธฐ์—๋Š” ์ ๋‹นํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜์ค‘์—์•ผ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ธฐ๊ณ„ ๊ณต๋‹จ์„ ๊ฐ€๋ณด๋ฉด ๊ธธ์— ๊ฒ€์€ ๋ ์™€ ๋ฐ˜์ง์ด๋Š” ๊ฐ€๋ฃจ๋“ค์ด ์žˆ๋Š”๋ฐ ์ด๊ฒƒ์€ ๋Œ€๋ถ€๋ถ„์ด ๊ธฐ๊ณ„์˜ ์œคํ™œ์œ ๊ฐ€ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ํ”์ ์ด๋ฉฐ ๊ธฐ๊ณ„ ๊ฐ€๊ณต ๊ณผ์ •์—์„œ ๋‚˜์˜ค๋Š” ๊ธˆ์† ๊ฐ€๋ฃจ๋“ค์ธ๋ฐ ๋ณตํ•ฉ์œ ํ†ต๋‹จ์ง€์—๋Š” ๊ทธ๊ฒƒ๋“ค์„ ์ˆ˜์šฉํ•˜๊ณ  ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋‹ค. ์ ์–ด๋„ ๊ทธ๊ฒƒ๋“ค์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ„๋„์˜ ๊ณต๊ฐ„์ด ํ•„์š”ํ–ˆ๋˜ ๊ฒƒ์ด๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ด๋ ‡๊ฒŒ ๋˜์—ˆ์ง€๋งŒ ๊ณ„์•ฝ ์ƒ ๋ณด๋ฉด ์‹œ๊ณต์‚ฌ๋‚˜ ๊ฐ๋ฆฌ์—…์ฒด๋Š” ์ฑ…์ž„์งˆ ๊ฒƒ์ด ์—†์—ˆ๋‹ค. ๊ณ ๊ฐ์ด ํ•ด๋‹ฌ๋ผ๋Š” ๋Œ€๋กœ ํ•ด์ฃผ์—ˆ๊ณ  ๊ฐ๋ฆฌ ์—ญ์‹œ ํŠน๋ณ„ํžˆ ์ง€์ ํ•  ์‚ฌํ•ญ์ด ์—†์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. PMO๋Š” ๋ฐ”๋กœ ์ด๋Ÿฐ ๊ฒฝ์šฐ์— ๊ด€์—ฌํ•˜๊ฒŒ ๋œ๋‹ค. ์ „๋ฌธ์  ์ง€์‹๊ณผ ๊ฒฝํ—˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ PMO ๊ตฌ์„ฑ์›๋“ค์ด ๊ณ ๊ฐ์ด ์ง„์ •์œผ๋กœ ์›ํ•˜๋Š” ๊ฒƒ์ด ๋ฌด์—‡์ธ์ง€, ์‹œ๊ณต์‚ฌ๋Š” ๊ณ ๊ฐ์˜ ์š”๊ตฌ๋ฅผ ์ œ๋Œ€๋กœ ์ถฉ์กฑ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š”์ง€, ๊ฒฐ๊ณผ๋ฌผ์€ ํ”„๋กœ์ ํŠธ ๋ณธ๋ž˜ ๋ชฉ์ ์— ๋ถ€ํ•ฉํ•˜๋Š”์ง€ ๋“ฑ์„ ํŒ๋‹จํ•˜๊ณ  ์•Œ๋ ค์ฃผ๋Š” ์ผ์„ ๋งก๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ์ œ๋Œ€๋กœ ๊ฐ–์ถฐ์ง„ PMO ๊ตฌ์„ฑ์›๋“ค์„ ๋ณด๋ฉด ํ•ด๋‹น ์—…์— ๋Œ€ํ•œ ์ตœ๊ณ  ์ „๋ฌธ๊ฐ€๋“ค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ณ  ํ•ด์™ธ ๋Œ€ํ˜• ํ”„๋กœ์ ํŠธ์—์„œ PMO๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฝํ—˜์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋ฉด ๊ธ€๋กœ๋ฒŒ ์ธ์žฌ๋กœ ๊ฑฐ๋“ญ๋‚˜๋ฉด์„œ ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์— ๋Œ€ํ•œ ๋†’์€ ์ˆ˜์š”์™€ ๋Œ€๊ฐ€๋ฅผ ์ง€๋ถˆ ๋ฐ›๊ฒŒ ๋œ๋‹ค. 1.3 ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ํ•œํŽธ, ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋Š” ๊ฑด์„ค์ด๋‚˜ ๊ธฐ๊ณ„์‚ฐ์—…์˜ ํ”„๋กœ์ ํŠธ์™€ ๋‹ฌ๋ฆฌ ์—…๋ฌด์˜ ๋Œ€๋ถ€๋ถ„์ด ๋ฌธ์„œ ์ž‘์—…์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฒ ์ €ํ•˜๊ฒŒ ๋ฌธ์„œ ์‚ฐ์ถœ๋ฌผ์„ ์–ด๋–ป๊ฒŒ ์ƒ์„ฑํ•˜๊ณ  ์–ด๋–ป๊ฒŒ ๊ณ ๊ฐ์—๊ฒŒ ๊ฒ€์ˆ˜ ๋ฐ›์„ ๊ฒƒ์ธ๊ฐ€ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค. ํ•œ๊ตญ์˜ ๊ฒฝ์šฐ, ๊ธ€๋กœ๋ฒŒ Top 3 ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…์ด ์ฒ˜์Œ ์ง„์ถœํ•˜๋˜ ์‹œ์ ˆ๋งŒ ํ•ด๋„ ๊ฒฝ์˜์ง„ ๋ณด๊ณ  ๋ช‡ ๋ฒˆ ํ•˜๊ณ  A4 ๋ช‡ ์žฅ์˜ ๋ณด๊ณ ์„œ๋ฅผ ์ตœ์ข… ์‚ฐ์ถœ๋ฌผ๋กœ ๋‚ด๊ณ  ์ฒ ์ˆ˜ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์•˜๋‹ค.[6] ๊ทธ๋Ÿฌ๋‚˜ ์š”์ฆ˜์€ ๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ๋Š” ์ปจ์„คํŒ… ์‚ฌ์—…์„ ์ˆ˜์ฃผํ•˜๊ณ  ์ดํ–‰ํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ๋Š” ์‚ฌ์—… ๋Œ€๊ฐ€ ๋Œ€๋น„ ๊ณผ๋„ํ•˜๊ฒŒ ์ƒ๊ฐ๋˜๋Š” ์š”๊ตฌ๋ฅผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๊ณ , ๊ณ ๊ฐ์‚ฌ์—๋„ ํ’๋ถ€ํ•œ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ ๊ฒฝํ—˜์„ ๊ฐ€์ง„ ์ „์ง ์ปจ์„คํ„ดํŠธ๋“ค์ด ๋งŽ์ด ํฌ์ง„ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋“ค์˜ ๋ˆˆ๋†’์ด์— ๋งž๋Š” ์‚ฐ์ถœ๋ฌผ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๋„ ์‰ฝ์ง€ ์•Š๋‹ค. ์ €์ž๊ฐ€ ๊ฒฝํ—˜ํ•œ ๋ฐ”์— ์˜ํ•˜๋ฉด ์‚ฐ์ถœ๋ฌผ์˜ ์งˆ์ ์ธ ๋ถ€๋ถ„๋ณด๋‹ค๋„ ์‚ฐ์ถœ๋ฌผ์˜ ์–‘์ ์ธ ์ธก๋ฉด์—์„œ ๊ฐ€์žฅ ๊นŒ๋‹ค๋กœ์šด ๊ฒƒ์€ ๊ณต๊ณต IT ์ปจ์„คํŒ…์ด๋‹ค. ๋ฒ•๋ฅ ์— ์˜ํ•ด ๊ฐ ๋‹จ๊ณ„[7] ๋ณ„๋กœ ์–ด๋–ค ๋ฌธ์„œ๊ฐ€ ๋งŒ๋“ค์–ด์ ธ ๋‚˜์™€์•ผ ํ•˜๋Š”์ง€ ์ •ํ•ด์ ธ ์žˆ๊ณ , ๊ฐ€์ด๋“œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งค์šฐ ์ •ํ˜•ํ™”๋˜์–ด ์žˆ๋‹ค. ISP์™€ ๊ฐ™์€ ์ •๋ณดํ™” ์ „๋žต ๊ณ„ํš์„ ์œ„ํ•œ ์ปจ์„คํŒ… ๊ธฐ์—… ๊ณ ์œ ์˜ ๋ฐฉ๋ฒ•๋ก ์„ ๊ธฐ๋ณธ์œผ๋กœ ํ•˜๋˜ ์ „์ฒด์ ์ธ ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ๋Š” ์ •๋ณด์‹œ์Šคํ…œ ๊ตฌ์ถ• ํ”„๋กœ์ ํŠธ๋ฅผ ํ…Œ์ผ๋Ÿฌ๋ง ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค. Table IV-3. ์€ ์ •๋ณด์ „๋žต ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ์˜ ๊ฐ ๋‹จ๊ณ„๋ณ„ ์ฃผ์š” ๋‚ด์šฉ๊ณผ ์ฃผ์š” ์‚ฐ์ถœ๋ฌผ์„ ์ •๋ฆฌํ•ด ๋ณธ ๊ฒƒ์ด๋‹ค. ์†Œ์œ„ ๋งํ•˜๋Š”, Value Pack List์ธ๋ฐ ์ง€์‹๊ณผ ๊ฒฝํ—˜์ด ํ’๋ถ€ํ•œ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ TableIV-3๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ํด๋”๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ๋ฌธ์„œ ํ…œํ”Œ๋ฆฟ, ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋ก€ ๋“ฑ์„ ๋ฌถ์–ด์„œ ์ปจ์„คํ„ดํŠธ๋“ค ์‚ฌ์ด์—์„œ ๊ณต์œ ํ•œ๋‹ค. Value Pack์ด ์ž˜ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉด ๊ณ ๊ฐ์˜ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์•ฝ๊ฐ„๋งŒ ์ˆ˜์ •ํ•˜์—ฌ ์‚ฐ์ถœ๋ฌผ๋“ค์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ปจ์„คํŒ… ์‚ฌ์—…์ด ์ง€์‹ ์‚ฌ์—…(Knowledge Business)์ด๋ผ๋Š” ๊ฒƒ์˜ ๋ฐ˜์ฆ์ด๊ณ , ์‚ฐ์—… ์œ ํ˜• ๋˜๋Š” ์‚ฌ์—… ์œ ํ˜• ๋“ฑ ๋‹ค์–‘ํ•œ ๊ตฌ๋ถ„์— ๋”ฐ๋ฅธ ์œ ์˜๋ฏธํ•œ Value Pack์ด ๋งŽ์„์ˆ˜๋ก ์ปจ์„คํ„ดํŠธ๋“ค์ด ์ผ์˜ ๋ณธ์งˆ์— ๊ณ ๋ฏผํ•  ์ˆ˜ ์žˆ๋Š” ์‹œ๊ฐ„์ด ๋” ๋งŽ์•„์ง€๋ฉฐ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ณด๋‹ค ํšจ์œจ์ ์œผ๋กœ ์ผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์ปจ์„คํŒ… ์‚ฌ์—…์—์„œ Value Pack์€ ํ•„์ˆ˜์ ์ด๋‹ค. ์–‘์งˆ์˜ Value Pack์„ ๊ฐ–์ถœ์ˆ˜๋ก ๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ์ˆ˜์ค€ ๋†’์€ ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์‹ค๋ฌธ์ œ์˜ ๋ณธ์งˆ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์— ๋งŽ์€ ์‹œ๊ฐ„์„ ๋ณด๋‚ด์•ผ ํ•˜๋Š”๋ฐ ๊ทธ ์งง์€ ๊ธฐ๊ฐ„์— ํ”„๋ ˆ์ž„์›Œํฌ ๊ณ ๋ฏผํ•˜๊ณ , ๋ฌธ์„œ ์–‘์‹ ๊ณ ๋ฏผํ•˜๊ณ  ํ•œ๋‹ค๋ฉด ๊ณ ๊ฐ์˜ ๊ธฐ๋Œ€์— ๋ถ€์‘ํ•  ์ˆ˜ ์—†์„ ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ, ์‹œ๋‹ˆ์–ด ์ปจ์„คํ„ดํŠธ ์ด์ƒ์ด ๋˜๋ฉด ๋ณดํ†ต 2~3๊ฐœ์˜ ํ”„๋กœ์ ํŠธ์— ์ง๊ฐ„์ ‘์œผ๋กœ ๊ด€์—ฌํ•˜๊ฒŒ ๋˜๊ณ  PM์„ ํ•˜๊ณ  ์žˆ์ง€๋งŒ ๋‹ค์Œ ์‚ฌ์—… ์ˆ˜์ฃผ๋ฅผ ์œ„ํ•œ ๋งˆ์ผ€ํŒ…์ด๋‚˜ ์˜์—…, ์ œ์•ˆ๋„ ํ•ด์•ผ ํ•˜๊ธฐ์— ์ž˜ ๊ฐ–์ถ”์–ด์ง„ Value Pack ๋งŒํผ ์†Œ์ค‘ํ•œ ๊ฒƒ์ด ์—†๋‹ค. ๊ทธ๋ ‡์ง€ ๋ชปํ•˜๋ฉด ๋‚ฎ์—๋Š” ํ”„๋กœ์ ํŠธ ์ˆ˜ํ–‰, ๋ฐค์—๋Š” ์˜์—…์ด๋‚˜ ์ œ์•ˆ ์ž‘์—…์ด ์ผ์ƒ์ด ๋œ๋‹ค Table IV-3. IT ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ ๋‹จ๊ณ„ ๋ฐ ์ฃผ์š” ๋‚ด์—ญ ์‚ฌ๋ก€[8] Table IV-3์„ ๋ณด๊ณ ๋Š” โ€˜์Œ.. ์ด๊ฑด ์ปจ์„คํŒ…์ด ์•„๋‹ˆ๋ผ ๊ตฌ์ถ• ์‚ฌ์—… ์•„๋ƒ?โ€™๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š” ์‚ฌ๋žŒ๋“ค๋„ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ด ๊ฒฝ์šฐ ์ปจ์„คํŒ… ๊ฒฐ๊ณผ๋ฌผ์ด ์ •๋ณด์‹œ์Šคํ…œ ๊ตฌ์ถ• ํ”„๋กœ์ ํŠธ์™€ ์ด์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— IT ์ปจ์„คํŒ… ์‚ฐ์ถœ๋ฌผ์€ ๋‹น์—ฐํžˆ ๊ทธ๊ฒƒ๊ณผ ์—ฐ๊ด€๋œ ๊ฒƒ๋„ ๋งŽ๊ณ , ์ปจ์„คํ„ดํŠธ๋“ค๋งŒ ์ผํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ”„๋กœ์ ํŠธ ํŒ€์— ์•„ํ‚คํ…์ฒ˜, ๊ฐœ๋ฐœ์ž๋“ค๋„ ๊ฐ™์ด ํ•ฉ๋ฅ˜ํ•˜์—ฌ ์ผํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ๋˜ํ•œ, ๊ณต๊ณต ํ”„๋กœ์ ํŠธ๋Š” ๊ฐ๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— RFP์— ์žˆ๋Š” ํ•ญ๋ชฉ๋“ค์ด ์ œ๋Œ€๋กœ ์ค€์ˆ˜๋˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ์ œ3์ž ์‹œ๊ฐ์˜ ๊ด€์ ์—์„œ ์ ๊ฒ€ํ•˜๋Š” ์ผ๋„ ๋ฐœ์ƒํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌธ์„œ์˜ ์ข…๋ฅ˜๋Š” ๋งŽ์ง€๋งŒ ๋งค์šฐ ์ •ํ˜•ํ™”๋˜์–ด ์žˆ๊ณ  ํ•œ๋ฒˆ ์ œ๋Œ€๋กœ ๊ฒฝํ—˜ํ•ด ๋ณด๋ฉด ํฌ๊ฒŒ ์–ด๋ ต์ง€๋Š” ์•Š๋‹ค. ์ €์ž์˜ ๊ฒฝํ—˜์„ ๋Œ์ด์ผœ๋ณด๋ฉด ๊ณต๊ณต IT ์ปจ์„คํŒ…์„ ์ˆ˜ํ–‰ํ•ด ๋ณธ ๊ฒฝํ—˜์ด ์žˆ๋Š” ์ปจ์„คํ„ดํŠธ๋“ค์€ ๋ฏผ๊ฐ„ ๊ธฐ์—…์˜ ์ปจ์„คํŒ…๋„ ์‰ฝ๊ฒŒ ์ ์‘ํ•ด๋‚˜๊ฐ”๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ทธ ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ, ์ƒ๋Œ€์ ์œผ๋กœ ๋ณต์žกํ•˜๊ณ  ์ƒ์„ธํ•œ ์ ˆ์ฐจ, ๋‹ค์–‘ํ•œ ์‚ฐ์ถœ๋ฌผ์˜ ์ž‘์„ฑ ๋ฐ ํ…Œ์ผ๋Ÿฌ๋ง ๊ฒฝํ—˜์ด ๋ถ€์กฑํ•˜์—ฌ ์ž˜ ๋Œ€์‘ํ•˜์ง€ ๋ชปํ•˜์˜€๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์‚ฐ์ถœ๋ฌผ์˜ ์žฌ์‚ฌ์šฉ ๋ฐ ์ž์‚ฐํ™”๋Š” ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ํฐ ๊ฒฝ์Ÿ๋ ฅ์ด ๋˜๊ณ  ์žˆ๋‹ค. [1] Project Management Institute. http://www.pmi.org [2] PMI์—์„œ๋Š” 4๋…„๋งˆ๋‹ค PMBOK๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋ฉด์„œ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์žˆ๋‹ค. ๊ด€๋ฆฌ ๋ฒ”์ฃผ์˜ ๊ตฌ๋ถ„๊ณผ ๋‚ด์šฉ๋„ ๊ณ„์† ์—…๋ฐ์ดํŠธ๋˜๊ณ  ์žˆ๋Š”๋ฐ Table IV-1์€ PMBOK 3ํŒ์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜์˜€๋‹ค. [3] Henry Gantt(1861 ~ 1919)์— ์˜ํ•ด ์ฐฝ์•ˆ๋œ ๊ณต์ •๊ณ„ํš ์–‘์‹. ์ž‘์—…๊ณ„ํš๊ณผ ์‹ค์ œ ์ž‘์—…๋Ÿ‰์„ ๋ง‰๋Œ€ ๋ชจ์–‘์˜ ์ž‘์—…์‹œ๊ฐ„์œผ๋กœ ํ‘œ๊ธฐํ•˜๊ณ  ๊ด€๋ฆฌํ•จ [4] PERT: Program Evaluation and Review Technique. 1958๋…„ ๋ฏธ๊ตญ ํ•ด๊ตฐ์ด ํด๋ผ๋ฆฌ์Šค ๋ฏธ์‚ฌ์ผ ๊ฐœ๋ฐœ ํ”„๋กœ์ ํŠธ์˜ ์ผ์ • ๊ณ„ํš ๋ฐ ํ†ต์ œ๋ฅผ ์œ„ํ•ด ๊ฐœ๋ฐœํ•จ. CPM: Critical Path Method. 1957๋…„ ๋ฏธ๊ตญ RemintonRand ์‚ฌ์—์„œ ๊ฐœ๋ฐœ. PERT๋Š” ๊ฐ ํ™œ๋™ ์‹œ๊ฐ„์„ 3๊ฐ€์ง€๋กœ ์ถ”์ •ํ•˜์—ฌ ํ‰๊ท  ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜๋Š” ์ผ์ข…์˜ ํ™•๋ฅ ๋ชจํ˜•์ธ๋ฐ ๋น„ํ•ด CPM์€ ๊ฐ ํ™œ๋™ ์‹œ๊ฐ„์„ ํ™•์ •์ ์œผ๋กœ ์ถ”์ •ํ•จ. ์ฆ‰, PERT๋Š” ํ”„๋กœ์ ํŠธ์˜ ์‹œ๊ฐ„์  ์ธก๋ฉด๋งŒ ๊ณ ๋ คํ•˜๋Š”๋ฐ ๋น„ํ•ด, CPM์€ ์‹œ๊ฐ„๊ณผ ๋น„์šฉ์„ ๋ชจ๋‘ ๊ณ ๋ คํ•จ [5] ์ฃผ๋กœ ๋ฐœ์ฃผ์ž์˜ ์š”๊ตฌ์— ๋งž์ถœ ๋Šฅ๋ ฅ์„ ๊ฐ–์ถ”์ง€ ๋ชปํ•œ ์˜์„ธ ์‚ฌ์—…์žฅ์„ ๋น„ํ•˜ํ•˜๋Š” ์˜๋ฏธ๋กœ ์‚ฌ์šฉ [6] ์ด ๋ถ€๋ถ„์€ ๋…ผ๋ž€์˜ ์—ฌ์ง€๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ์‚ฐ์ถœ๋ฌผ์€ ๊ทธ๋ ‡์ง€๋งŒ ์‚ฌ์‹ค CEO๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ฒฝ์˜์ง„๋“ค์€ ๋˜ ๋‹ค๋ฅธ ๋น„์ฆˆ๋‹ˆ์Šค ๋„คํŠธ์›Œํฌ๋ฅผ ๋งŒ๋“œ๋Š” ์„ฑ๊ณผ๋„ ์–ป๊ฒŒ ๋˜๋ฏ€๋กœ ์‹ค๋ฌด์ž๊ฐ€ ์ ‘ํ•˜๋Š” ๋ณด๊ณ ์„œ์™€ ๊ฒฝ์˜์ง„์ด ์–ป๊ฒŒ ๋˜๋Š” ๊ฐ€์น˜(Value)๋Š” ์ข€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก , ์ง€์‹๊ณผ ๊ฒฝํ—˜์ด ํ’๋ถ€ํ•œ ์ปจ์„คํ„ดํŠธ๋“ค์ด ๋“ค์–ด์™€์„œ ์ผ์„ ํ•œ๋‹ค๋Š” ์ „์ œ์—์„œ ๋ง์ด๋‹ค. [7] ์—ฌ๊ธฐ์„œ ๋‹จ๊ณ„๋Š” ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ์˜ ๋‹จ๊ณ„๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ์€ ์ปจ์„คํŒ… ๊ธฐ์—…๋งˆ๋‹ค ๋‹ค๋ฅด๋ฉฐ, ๊ณ ๊ฐ๊ณผ ํ•ฉ์˜ํ•˜์—ฌ ํ…Œ์ผ๋Ÿฌ๋ง ํ•œ ํ›„ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. [8] ๋น„์šฉ ๊ด€๋ฆฌ์˜ ๊ฒฝ์šฐ, ์š”์ฆ˜์€ ์ •๋ณด์‹œ์Šคํ…œ์ด ์ž˜ ๊ตฌ์ถ•๋˜์–ด ์žˆ์–ด ๋น„์šฉ๋„ ์‚ฌํ›„์ •์‚ฐ๋ณด๋‹ค ๋ฒ•์ธ์นด๋“œ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๊ณ , ์‚ฌ์—… ๋Œ€๊ฐ€ ์ฒญ๊ตฌ๋„ ์ „์ž์„ธ๊ธˆ๊ณ„์‚ฐ์„œ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ์ผ๋ฐ˜ํ™”๋˜์–ด ์žˆ๋‹ค. 120 PART VI. ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก  ๋ˆ„๊ตฐ๊ฐ€ ์ปจ์„คํŒ… ์—…๋ฌด์™€ ๊ธฐ์—…์˜ ๋‹ค๋ฅธ ์—…๋ฌด ์‚ฌ์ด์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์ด ๋ฌด์—‡์ธ๊ฐ€๋ฅผ ๋ฌผ์–ด๋ณธ๋‹ค๋ฉด ์ €์ž๋Š” ๋ฐฉ๋ฒ•๋ก ์˜ ์กด์žฌ ์œ ๋ฌด๋ผ๊ณ  ๋งํ•˜๊ณ  ์‹ถ๋‹ค. ๋ฐฉ๋ฒ•๋ก ์€ ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ๋ถ•์–ด๋นต ํ‹€๊ณผ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. ๋ถ•์–ด๋นต์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๋ชจ์–‘๋Œ€๋กœ ํ‹€์„ ๊ตฌ์ƒํ•˜๊ณ  ๋‚œ ํ›„, ๋ถ•์–ด๋นต์ด ๋จน๊ณ  ์‹ถ์„ ๋•Œ์— ๊ทธ ํ‹€์—๋‹ค ๋ฐ˜์ฃฝ๊ณผ ๋‹จํŒฅ ๊ฐ™์€ ๋ง›์žˆ๋Š” ์žฌ๋ฃŒ๋ฅผ ๋„ฃ์œผ๋ฉด ๋ถ•์–ด๋นต์ด ๋งŒ๋“ค์–ด์ง€๊ฒŒ ๋œ๋‹ค. ์ด ๋ถ•์–ด๋นต ํ‹€์„ ๋น„์ฆˆ๋‹ˆ์Šค์˜ ์„ธ๊ณ„๋กœ ๊ฐ€์ ธ์˜ค๋ฉด ๊ทธ๊ฒŒ ๋ฐ”๋กœ ๋ฐฉ๋ฒ•๋ก ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก , ๋ฐฉ๋ฒ•๋ก ์€ ํ…Œ์ผ๋Ÿฌ๋ง(Tailoring)์ด๋ผ๊ณ  ํ•˜๋Š” ๊ณ ์œ ์˜ ์—…๋ฌด๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ ๊ฐœ๋…์€ ์ƒํ†ตํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐฉ๋ฒ•๋ก ์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ์š”์†Œ๋“ค์„ ๋ฐ˜๋“œ์‹œ ์ถฉ์กฑํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ๊ณ„์  ์ ˆ์ฐจ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ• ํ…œํ”Œ๋ฆฟ๊ณผ ์‚ฌ๋ก€ ์ฒซ ๋ฒˆ์งธ, ๋‹จ๊ณ„์™€ ์ ˆ์ฐจ๋Š” ํ”„๋กœ์„ธ์Šค(process)๋ผ๊ณ ๋„ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์–ด๋–ค ๋‹จ๊ณ„(Phase or Stage)์™€ ์ ˆ์ฐจ(Step)์— ๋”ฐ๋ผ ์ง„ํ–‰ํ•˜๋ฉฐ ๊ฐ ๋‹จ๊ณ„๋‚˜ ์ ˆ์ฐจ์—์„œ ํ•ด์•ผ ํ•  ์ผ(Tasks or Activities)์„ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. ๋‘ ๋ฒˆ์งธ, ๋ฐฉ๋ฒ•๋ก ์€ ๊ฐ ๋‹จ๊ณ„์™€ ์ ˆ์ฐจ๊ฐ€ ์ œ์‹œํ•˜๋Š” ์ผ์„ ํ•˜๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ๊ฐ€ Part II, Part III์—์„œ ์‚ดํŽด๋ณธ ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  ์ฒด๊ณ„์— ๊ธฐ๋ฐ˜ํ•œ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•(Tools and Techniques)์„ ์ œ์‹œํ•˜๊ณ  ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์„ธ ๋ฒˆ์งธ๋กœ ๋ฐฉ๋ฒ•๋ก ์€ ๊ทธ๋Ÿฐ ๋„๊ตฌ์™€ ์ ˆ์ฐจ๋ฅผ ์ ์šฉํ•œ ๋ฌธ์„œ ํ…œํ”Œ๋ฆฟ๊ณผ ์‚ฌ๋ก€(Templates and References)๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ์ด์œ ๋กœ ์†Œ์œ„ ๊ฒ€์ฆ๋˜์—ˆ๋‹ค๊ณ  ํ•˜๋Š” ์šฐ์ˆ˜ํ•œ ๋ฐฉ๋ฒ•๋ก ์€ ๋งŽ์€ ์ ์šฉ ์‚ฌ๋ก€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ๊ทธ ๋‹จ๊ณ„์™€ ์ ˆ์ฐจ๊ฐ€ ๋ณต์žกํ•˜์ง€ ์•Š๊ณ  ๋ˆ„๊ตฌ๋‚˜ ์‰ฝ๊ฒŒ ๋ฐฐ์šฐ๊ณ  ์ตํž ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ํŠน์ง•์ด๋‹ค. ๋˜ํ•œ, ๋ฐฉ๋ฒ•๋ก ์€ ์—…๋ฌด ์ˆ˜ํ–‰์„ ์œ„ํ•œ ์ผ์ข…์˜ ๊ฐ€์ด๋“œ(Guide)๋กœ์„œ ์ƒ์„ธํ•œ ํ•ด์„ค(Description)์„ ํ†ตํ•ด ๋ฐฉ๋ฒ•๋ก ๋Œ€๋กœ๋งŒ ํ•˜๋ฉด ๋ˆ„๊ตฌ๋“ ์ง€ 100์  ๋งŒ์ ์— 80์  ์ด์ƒ์˜ ์„ฑ๊ณผ๋Š” ๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ์—ฌ๊ธฐ์„œ 20์ ์€ ๋ชจ๋“  ํ”„๋กœ์ ํŠธ๊ฐ€ ๊ณ ์œ (Uniqueness) ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์กฐ์ •ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ, ๋ฐฉ๋ฒ•๋ก  ๊ณ ์œ ์˜ ์‚ฌ์ƒ๊ณผ ํŠน์ƒ‰์„<NAME> ์ฑ„ ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์— ๋งž๊ฒŒ ๊ฐ ๋‹จ๊ณ„๋‚˜ ์ ˆ์ฐจ, ์ผ์„ ์ค„์ด๊ฑฐ๋‚˜ ํ†ตํ•ฉํ•˜๋Š” ์ผ ์ฆ‰, ํ…Œ์ผ๋Ÿฌ๋ง(Tailoring) ํ•ด์•ผ ํ•œ๋‹ค. ๋ถ•์–ด๋นต์˜ ๋น„์œ ๋กœ ๋Œ์•„๊ฐ€ ๋ณด๋ฉด ๋ฐ˜์ฃฝ์ด ๋น„์œจ์ด ๋‹ฌ๋ผ์ง„๋‹ค๋“ ์ง€ ๋‹จํŒฅ ๋Œ€์‹  ๋‹ค๋ฅธ ์†Œ๋ฅผ ๋„ฃ๋Š”๋‹ค๋“ ์ง€ ํ•˜๋Š” ๊ฒƒ์ด ๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜๋ด์•ผ ๋ถ•์–ด๋นต์ด๋“ฏ ๊ทธ๋Ÿฐ ์•ฝ๊ฐ„์˜ ๋ณ€ํ™”์™€ ์กฐ์ •์„ ๊ฐ€๋ฏธํ•œ ์ปจ์„คํŒ… ์‚ฐ์ถœ๋ฌผ์ด ๋งŒ๋“ค์–ด์ง€๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 2000๋…„ ๋Œ€์— ์ ‘์–ด๋“ค์–ด ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์€ ๊ธฐ์—…์˜ ์ง€์†์„ฑ์žฅ(Sustainable Growth)์ด๋‹ค. ๊ฑฐ์˜ ๋ชจ๋“  ํ™”๋‘๊ฐ€ ์ด '์ง€์†์„ฑ์žฅ'์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ์—์„œ ์ถœ๋ฐœํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ๊ธฐ์—…์ด ์„ฑ์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Figure IV-1๊ณผ ๊ฐ™์€ 3๊ฐ€์ง€ ์ถ•์ด ์ค‘์š”ํ•˜๋‹ค. ์ด 3๊ฐ€์ง€ ์ถ•์€ ์–ด๋–ค ๊ฒƒ์ด ๋จผ์ €๋ผ๊ณ  ํ•  ๊ฒƒ๋„ ์—†์ด ์„œ๋กœ ๋งž๋ฌผ๋ ค ๋Œ์•„๊ฐ€๋ฉฐ ์„ ์ˆœํ™˜(Virtuous cycle)์„ ๋งŒ๋“ค๊ฑฐ๋‚˜ ์•…์ˆœํ™˜(Vicious cycle)์„ ๋งŒ๋“ค๊ฒŒ ๋œ๋‹ค. Figure IV-1. ๊ธฐ์—… ์„ฑ์žฅ์˜ 3๊ฐ€์ง€ ์ถ• ๋”ฐ๋ผ์„œ ๊ธฐ์—…์˜ ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๊ธฐ ์ „์— 3๊ฐ€์ง€ ์ถ• ์ค‘์—์„œ ์–ด๋–ค ๋ถ€๋ถ„๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด ํ•œ๋ฒˆ ๊ณ ๋ฏผํ•ด ๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด Figure IV-2์™€ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. Figure IV-2. ์ „๋žต ์ˆ˜๋ฆฝ์˜ ๊ด€์  ๊ฒฝ์˜์ฒด์งˆ๋„ ํ˜•ํŽธ์—†๊ณ  ๊ฒฝ์˜์ „๋žต๋„ ์—†์œผ๋ฉฐ ๊ฒฝ์˜ ๊ธฐ๋Šฅ๋„ ์ข‹์ง€ ๋ชปํ•  ๊ฒฝ์šฐ๋Š” ๊ฐ€์žฅ ๋จผ์ € ์˜์‹์„ ๊ฐœ์„ ํ•˜๊ณ  ๊ธฐ์—…์˜ ์ฒด์งˆ์„ ๊ฐœ์„ ํ•˜๋Š” ์ผ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. HR์ด๋‚˜ ์กฐ์ง๋ฌธํ™” ์ปจ์„คํŒ…์ด ๋จผ์ € ์‹œ์ž‘๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค. ์–ด๋Š ์ •๋„ ๊ฒฝ์˜ ๊ธฐ๋Šฅ์€ ๋Œ์•„๊ฐ€๋Š”๋ฐ ๊ฒฝ์˜์ฒด์งˆ์ด ๋ฌธ์ œ๊ฐ€ ๋งŽ์•„์„œ ๊ฒฝ์˜์ „๋žต์˜ ์ˆ˜๋ฆฝ๋„ ์‹คํ–‰๋„ ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” ๊ฒฝ์˜ ๊ธฐ๋Šฅ์„ ๊ฐœํ˜ํ•˜๊ณ  ๊ธฐ์—… ๋‚ด ํšจ์œจ ํ–ฅ์ƒ์— ์ง‘์ค‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ฒฝ์˜์ „๋žต๋„ ์žˆ๊ณ  ๊ฒฝ์˜ ๊ธฐ๋Šฅ๋„ ์ž˜ ๋Œ์•„๊ฐ€๊ณ , ๊ฒฝ์˜์ฒด์งˆ๋„ ๋‚˜์˜์ง€ ์•Š์€๋ฐ ๊ธฐ์—…์˜ ์„ฑ์žฅ์ด ๋‹ต๋ณด์ƒํƒœ๋ผ๋ฉด ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ํ˜์‹  ๋˜๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ์žฌ์ฐฝ์กฐ(Business Reinvention)์ด ํ•„์š”ํ•œ ์ƒํ™ฉ์ด๋‹ค. ์ด๋Ÿฐ ๊ธฐ๋ณธ ์ง„๋‹จ์„ ๋‘๊ณ  Part IV์—์„œ๋Š” ํ˜„์žฌ ๊ฐ€์žฅ ๋„๋ฆฌ ์•Œ๋ ค์ง„ ๋ช‡ ๊ฐ€์ง€ ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ๋“ค์„ ๋‹ค๋ฃจ๊ณ ์ž ํ•œ๋‹ค. ์ปจ์„คํŒ…๋„ ์ˆ˜์ฃผ ์‚ฌ์—…์ด๊ธฐ ์ œ์•ˆ๋„ ์“ฐ๊ณ  ์ž…์ฐฐ๋„ ํ•œ๋‹ค. ๊ทธ๋Ÿฐ ์ปจ์„คํŒ… ์‚ฌ์—… ๊ฐœ๋ฐœ์€ Part V์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ณ  Part IV์—์„œ๋Š” ์ˆ˜์ฃผ ์ดํ›„, ์ปจ์„คํŒ…์„ ์ œ๋Œ€๋กœ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ฐฉ๋ฒ•๋ก ์„ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. ํ”„๋กœ์ ํŠธ ๋ฐฉ๋ฒ•๋ก ์€ ์ด ๋ชจ๋“  ๋ฐฉ๋ฒ•๋ก ์˜ ๊ธฐ๋ณธ์ด๋‹ค. ๊ทธ ์™ธ ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ, ํ”„๋กœ์„ธ์Šค ํ˜์‹ , ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ, ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„, ์ •๋ณดํ™” ์ „๋žต ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณผ ๊ฒƒ์ด๋‹ค. ์ด ์ €์„œ๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ๊ฒƒ์„ ์ƒ์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•  ์ˆ˜๋„ ์—†๊ณ  ๋Œ€๋ถ€๋ถ„์˜ ๋ฐฉ๋ฒ•๋ก ์€ ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ํฐ ์ž์‚ฐ[1]์ด๋ฏ€๋กœ ์ด๋ฅผ ์™ธ๋ถ€์— ๊ณต๊ฐœํ•˜๋Š” ๊ฒƒ๋„ ๋งค์šฐ ์ œํ•œ์ ์ผ ์ˆ˜๋ฐ–์— ์—†๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์•„๋งˆ ๋‹ค๋ฅธ ๊ณณ์—์„œ ์‰ฝ๊ฒŒ ์ ‘ํ•ด๋ณด์ง€ ๋ชปํ–ˆ์„ ์ด์•ผ๊ธฐ๋“ค์ด ๋งŽ์„ ๊ฒƒ์ž„์„ ํ™•์‹ ํ•œ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ์˜ ์„ธ๊ณ„๋กœ ๋“ค์–ด๊ฐ€ ๋ณด์ž. [1] ๋Œ€๋ถ€๋ถ„์˜ ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ๋“ค์€ ๋ฏผ๊ฐ„ ๊ธฐ์—… ๊ณ ์œ ์˜ ์ง€์‹๊ณผ ๊ฒฝํ—˜์ด ์ข…ํ•ฉ๋œ ๊ฒƒ์œผ๋กœ ๋ฌด๋‹จ์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ๋ผ์ด์„ ์Šค ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ํ•ด๋‹น ๊ธฐ์—…์˜ ํ—ˆ๋ฝ ์—†์ด ์ง€๋ฉด์„ ํ†ตํ•ด ์ƒ์„ธํžˆ<NAME>๋Š” ๊ฒƒ์€ ๋ฌธ์ œ๊ฐ€ ๋˜๋ฏ€๋กœ ์‹ค์ œ ๊ทธ๊ฒƒ์„ ๊ฒฝํ—˜ํ•ด ๋ณด๊ณ ์ž ํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๊ธฐ์—…์— ์ปจ์„คํŒ… ์˜๋ขฐ๋ฅผ ํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋‹ค. 13. ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•๋ก (1/3) ๊ฟˆ์„ ์ด๋ฃจ๋Š” ๋ฐฉ์‹์—๋Š” ํฌ๊ฒŒ 2๊ฐ€์ง€ ๊ด€์ ์˜ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์ผ๋ณธ์˜ ์ „๊ตญ์‹œ๋Œ€๋ฅผ ๋Œ€ํ‘œํ–ˆ๋˜ ๋‘ ์ธ๋ฌผ ์˜ค๋‹ค ๋…ธ๋ถ€๋‚˜๊ฐ€(็น”็”ฐไฟก้•ท. 1534 ~ 1582)์™€ ๋„์š”ํ† ๋ฏธ ํžˆ๋ฐ์š”์‹œ(่ฑ่‡ฃ็ง€ๅ‰. 1536 ~ 1598)๋Š” ๊ทธ 2๊ฐ€์ง€ ์ ‘๊ทผ๋ฒ•์„ ๋ณด์—ฌ์ฃผ์—ˆ๋Š”๋ฐ, ์ปค๋‹ค๋ž€ ๋ชฉํ‘œ๋ฅผ ๋จผ์ € ์„ธ์šฐ๊ณ  ๊ทธ๊ฒƒ์„ ์ด๋ฃจ๊ธฐ ์œ„ํ•ด ์ตœ์„ ์„ ๋‹คํ•˜๋Š” ๊ฒƒ์ด ์˜ค๋‹ค ๋…ธ๋ถ€๋‚˜๊ฐ€์˜ ๊ฟˆ์— ๋Œ€ํ•œ ์ ‘๊ทผ ๋ฐฉ์‹์ด์—ˆ๊ณ , ํ˜„์žฌ์˜ ํ™˜๊ฒฝ์— ๋งž๋Š” ์‹คํ˜„ ๊ฐ€๋Šฅํ•œ ๋ชฉํ‘œ๋ฅผ ์ •ํ•œ ๋’ค ํ•œ๊ฑธ์Œ ํ•œ๊ฑธ์Œ ๊พธ์ค€ํžˆ ์ „์ง„ํ•˜๋Š” ๋ฐฉ์‹์ด ๋„์š”ํ† ๋ฏธ ํžˆ๋ฐ์š”์‹œ์˜ ์ ‘๊ทผ ๋ฐฉ์‹์ด์—ˆ๋‹ค. ๋‘˜ ๋‹ค ํ˜ผ๋ž€์Šค๋Ÿฌ์šด ์ผ๋ณธ์˜ ์ „๊ตญ์‹œ๋Œ€์˜ ์ผ๋ณธ์„ ํ†ต์ผํ–ˆ๋˜[1] ์‚ฌ๋žŒ๋“ค์ด์ง€๋งŒ ๊ทธ ๋ชฉํ‘œ๋ฅผ ์ด๋ฃฉํ•œ ๋ฐฉ๋ฒ•์€ ๊ทน๋ช…ํ•œ ๋Œ€์กฐ๋ฅผ ์ด๋ฃฌ๋‹ค. ๊ฒฝ์˜ ์ „๋žต์˜ ๊ด€์ ์—์„œ ์ด ๊ฟˆ์€ ๋น„์ „(Vision)๊ณผ๋„ ํ†ตํ•œ๋‹ค. ๋ธŒ๋žœ๋“œ ์ „๋žต์— ์ง€๋Œ€ํ•œ ๊ณตํ—Œ์„ ํ•œ ๋ฐ์ด๋น„๋“œ ์—์ด์ปค(David A. Aaker. 1938 ~ ํ˜„์žฌ)๋Š” ๊ธฐ์—…์ด ์ „๋žต์„ ์šด์˜ํ•ด ๋‚˜๊ฐ€๋Š” ๋ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ 2๊ฐ€์ง€ ๊ด€์ (์‚ฌ์ƒ)์ด ์กด์žฌํ•œ๋‹ค๊ณ  ํ•˜์˜€๋‹ค. ๋น„์ „ ์ฃผ์˜ ๊ธฐํšŒ์ฃผ์˜ ์ฒซ ๋ฒˆ์งธ '๋น„์ „ ์ฃผ์˜'๋Š” ๊ธฐ์—…์ด ์žฅ๊ธฐ์ ์œผ๋กœ ์„ฑ์ทจํ•˜๊ณ  ์‹ถ์€ ๋ชจ์Šต ์ฆ‰, ๋น„์ „(Vision)์„ ์„ค์ •ํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ์‹คํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ธฐ์—…์˜ ๋ชจ๋“  ์ž์›๊ณผ ์—ญ๋Ÿ‰์„ ์ง‘์ค‘์‹œํ‚ค๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ๋น„์ „ ์ฃผ์˜๊ฐ€ ์ง€ํ–ฅํ•˜๋Š” ๊ฒƒ์€ ํฐ ๋ณ€ํ™”์ด๋ฏ€๋กœ ์ง๋ฉดํ•œ ํ™˜๊ฒฝ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ž‘์€ ์ €ํ•ญ๋“ค์— ๋Œ€ํ•ด์„œ๋Š” ํฌ๊ฒŒ ๊ณผ๋ฏผ๋ฐ˜์‘ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ์ž์‹ ์˜ ๊ธธ์„ ๊ตณ๊ฑดํ•œ ์‹ ๋…์œผ๋กœ ๋‚˜์•„๊ฐ€์ž๋Š” ๊ฒƒ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ โ€˜๊ธฐํšŒ์ฃผ์˜โ€™๋Š” ํ˜„์‹ค์— ์ถฉ์‹คํ•˜์ž๋Š” ๊ฒƒ์ด๋‹ค. ์˜ค๋Š˜์ด ์—†์ด๋Š” ๋‚ด์ผ๋„ ์—†์œผ๋ฉฐ ํ˜„์žฌ ์‹œ์žฅ๊ณผ ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ์— ๋Œ€์‘ํ•˜์ง€ ๋ชปํ•˜๋ฉด ๋ถˆํ™•์‹คํ•œ ๋ฏธ๋ž˜๋Š” ๋”์šฑ ์ƒ์กดํ•˜๊ธฐ ์–ด๋ ต๋‹ค๋Š” ์ „์ œ๊ฐ€ ๊น”๋ ค ์žˆ๋‹ค. ์ด๋Ÿฐ ์ด์ƒ๋“ค์— ๋ฐ˜ํ•ด ๊ธฐํšŒ์ฃผ์˜๋ฅผ ์ถ”์ข…ํ•˜๋Š” ๊ธฐ์—…๋“ค์€ ์ค‘์žฅ๊ธฐ์  ๋ฐฉํ–ฅ์„ ์ƒ์‹คํ•˜๊ณ  ํ‘œ๋ฅ˜ํ•˜๊ธฐ ์‰ฝ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์œผ๋ฉฐ ๋น„์ „ ์ฃผ์˜๋ฅผ ์ถ”์ข…ํ•˜๋Š” ๊ธฐ์—…๋“ค์€ ์ „๋žต์  ์™„๊ณ ํ•จ์— ๋น ์ ธ ๋ณ€ํ™”์— ๋ฏผ๊ฐํ•˜๊ฒŒ ๋Œ€์‘ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. ๊ธฐ์ˆ  ๋ณ€ํ™”๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์–ด๋ ต๊ณ  ์–ด๋–ค ์‹œ๋Œ€๋ณด๋‹ค๋„ ๋ถˆํ™•์‹ค์„ฑ์ด ๋งŽ์€ ์š”์ฆ˜์€ ๊ทน๋‹จ์ ์ธ ๋น„์ „ ์ฃผ์˜๋‚˜ ๊ธฐํšŒ์ฃผ์˜๋ฅผ ๊ตฌ์‚ฌํ•˜๋Š” ๊ธฐ์—…๋“ค์€ ์—†์œผ๋‚˜ ์–ด๋Š ์ชฝ์— ๋‹ค์†Œ ๋” ๋น„์ค‘์„ ๋‘˜ ๊ฒƒ์ธ์ง€๋Š” ๊ธฐ์—…์ด ์ฒ˜ํ•œ ํ™˜๊ฒฝ์˜ ๋ณ€ํ™” ํŠนํžˆ, ๊ทธ ์†๋„์™€ ๋ฒ”์œ„์™€ ๊ด€๋ จ์ง€์–ด ๊นŠ์ด ๊ณ ๋ฏผํ•ด์•ผ ํ•œ๋‹ค. ์ „๋žต ์ˆ˜๋ฆฝ์˜ ๊ด€์ ์— ๋น„์ „ ์ฃผ์˜์™€ ๊ธฐํšŒ์ฃผ์˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์ „๋žต ์ˆ˜๋ฆฝ์˜ ์ ‘๊ทผ ๋ฐฉ๋ฒ•๋„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํฌ๊ฒŒ 2๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.[2] ๋…ผ๋ฆฌ์  ์ ‘๊ทผ ์˜์ง€์  ์ ‘๊ทผ ๋…ผ๋ฆฌ์  ์ ‘๊ทผ์€ Part III์—์„œ ์‚ดํŽด๋ณธ ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์„ ์ถฉ์‹คํžˆ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ ์‹œ์‚ฌ์ ์„ ์ „๋žต ์ˆ˜๋ฆฝ์— ํ™œ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ๋Œ€ํ‘œ์ ์ธ ๊ฒƒ์ด ์ž์‚ฌ์˜ ๊ฐ•์ ๊ณผ ์•ฝ์  ๋ถ„์„, ํ•ต์‹ฌ ์—ญ๋Ÿ‰ ๋ถ„์„ ๋“ฑ์„ ์ด์šฉํ•˜์—ฌ ๋” ์ž˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๊ณผ ํ•˜์ง€ ๋ง์•„์•ผ ํ•  ๊ฒƒ๋“ค์„ ์ „๋žต์˜ ์„ ํƒ์ง€๋กœ ๋„์ถœํ•˜๊ฒŒ ๋œ๋‹ค. ์˜์ง€์  ์ ‘๊ทผ์€ ํ˜„์žฌ ๋ณด์œ ํ•œ ๊ธฐ์—…์˜ ์—ญ๋Ÿ‰๋ณด๋‹ค ๊ธฐ์—…์ด ๋„๋‹ฌํ•˜๊ณ ์ž ํ•˜๋Š” ๋ชฉ์ ์ง€ ๋˜๋Š” ๊ฟˆ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ๋‹ค. ์ฆ‰, ๋น„์ „์„ ์ˆ˜๋ฆฝํ•˜๊ณ  ์ด๋ฅผ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•๋ก ์„ ์ „๊ฐœํ•ด ๋‚˜์•„๊ฐ€๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์˜ค๋Š˜๋‚ ์˜ ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•์€ ๋…ผ๋ฆฌ์  ์ ‘๊ทผ๊ณผ ์˜์ง€์  ์ ‘๊ทผ์ด ํ˜ผํ•ฉ๋˜์–ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋…ผ๋ฆฌ์  ์ ‘๊ทผ์€ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์ด๋‚˜ ๊ฒฝ์Ÿ์šฐ์œ„๊ฐ€ ๋ช…ํ™•ํ•œ ๊ธฐ์—…์— ์œ ๋ฆฌํ•˜๋‹ค. ๊ทธ๋Ÿฐ ๊ฒƒ์ด ์—†๋Š” ๊ธฐ์—…์ด ์ „๋žต์„ ์„ธ์šธ ๋•Œ๋Š” ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š๋‹ค. ๊ทธ๋Ÿด ๊ฒฝ์šฐ, ์˜์ง€์  ์ ‘๊ทผ์„ ํ†ตํ•ด ๋‚˜์•„๊ฐ€์•ผ ํ•  ๋ฐฉํ–ฅ์„ ์ค‘์žฅ๊ธฐ์ ์œผ๋กœ ์ œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ทจํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ „ ์„ธ๊ณ„์˜ ๊ธฐ์—…๋“ค์„ ๋†“๊ณ  ๋ณด๋ฉด ๋ˆ„๊ตฌ๋‚˜ ์ธ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ํ•ต์‹ฌ ์—ญ๋Ÿ‰์„ ๋ณด์œ ํ•œ ๊ธฐ์—…๋“ค์€ ์ •๋ง ์†์— ๊ผฝ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋“ค ๊ธฐ์—…๋„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ทธ๋ ‡์ง€ ์•Š์•˜๊ณ  ์ฐฝ์—…์ฃผ๋‚˜ ์ตœ๊ณ ๊ฒฝ์˜์ž์˜ ์˜์ง€๊ฐ€ ๋ฌธ์„ ์—ด์—ˆ๋‹ค ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ค๋Š˜๋‚  ๊ฒฝ์˜ ์ „๋žต ์ˆ˜๋ฆฝ์˜ ์ ‘๊ทผ์€ ์˜์ง€์  ์ ‘๊ทผ์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ๋งŽ์œผ๋ฉฐ ๋‹ค๋งŒ, ๊ทธ ๋น„์ „์˜ ์‹คํ˜„ ๊ฐ€๋Šฅ์„ฑ ์ธก๋ฉด์—์„œ ํ˜„์‹ค์—์„œ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๊ณผ์ œ๋“ค์„ ์–ด๋–ป๊ฒŒ ์ „๋žต์  ์˜์ง€(Strategic Intents)์™€ ์ด์–ด๋‚˜๊ฐˆ ๊ฒƒ์ธ๊ฐ€๊ฐ€ ๊ด€๊ฑด์ด ๋œ๋‹ค. ์ œ13์žฅ์—์„œ๋Š” ์ด์™€ ๊ด€๋ จํ•˜์—ฌ ๊ธฐ์—…์˜ ์ „๋žต ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด์ž. 13.1 ์ „๋žต ์ˆ˜๋ฆฝ์˜ ์ ‘๊ทผ ๋ฐฉ๋ฒ• ๊ฒฝ์˜์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๊ฑฐ๋‚˜ ์žฌ์ •๋ฆฝํ•˜๊ธฐ ์œ„ํ•ด ์—…๋ฌด(work)์˜ ๊ด€์ ์—์„œ ๋ณด๋ฉด<NAME>์  ์ธก๋ฉด๊ณผ ๋™ํƒœ์  ์ธก๋ฉด์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ,<NAME>์ (Static) ์ธก๋ฉด์—์„œ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์€ ์ฝ˜ํ…์ธ ๊ฐ€ ๊ฐ•์กฐ๋˜๋Š” ๊ด€์ ์œผ๋กœ ๊ธฐ์—…์˜ ๋น„์ „๋ถ€ํ„ฐ ์ „์‚ฌ ๊ฒฝ์˜์ „๋žต, ์‚ฌ์—…์ „๋žต, ๊ธฐ๋Šฅ ์ „๋žต์ด ์ผ๊ด€๋˜๊ฒŒ ํ•˜๋‚˜์˜ ํ๋ฆ„์„ ์ด๋ฃจ๋„๋ก ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. Figure IV-7. ๊ฒฝ์˜์ „๋žต์˜ ๊ตฌ์„ฑ -<NAME>์  ์ธก๋ฉด ์ด๋ฅผ ๋„์‹ํ™”ํ•ด๋ณด๋ฉด Figure IV-7๊ณผ ๊ฐ™์€ ์ง‘์˜ ํ˜•ํƒœ๋กœ ๋งŽ์ด ๊ทธ๋ ค๋‚ด๋Š”๋ฐ, ์ „์‚ฌ ๊ฒฝ์˜ํ˜์‹ ์„ ์œ„ํ•ด ๋น„์ „๊ณผ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์žฌ์ •๋ฆฝํ•˜๊ณ  ๊ธฐ์—… ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๋ฉฐ ๋„์ถœ๋œ ์ „๋žต ๊ณผ์ œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์‹คํ–‰์ „๋žต์„ ์ˆ˜๋ฆฝํ•œ ํ›„, ์ด๋ฅผ ๋ณ€ํ™” ๊ด€๋ฆฌํ•˜์—ฌ ๊ฒฝ์˜ ํ˜์‹ ์˜ ์„ฑ๊ณผ๋ฅผ ๋‹ฌ์„ฑํ•˜๋Š” ๊ตฌ์กฐ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค. ์ง‘์„ ๊ตฌ์„ฑํ•˜๊ณ  ์žˆ๋Š” ๊ฐ ์˜์—ญ๋ณ„๋กœ ์ ๊ฒ€ํ•ด์•ผ ํ•  ์งˆ๋ฌธ๋“ค์ด ์žˆ์œผ๋ฉฐ ๊ทธ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์„ ์ฐพ์•„๊ฐ€๋ฉด์„œ ์ „๋žต ์ˆ˜๋ฆฝ์ด ์ง„ํ–‰๋œ๋‹ค. ๊ฐ ๋‹จ๊ณ„๋ณ„ ์งˆ๋ฌธ๊ณผ ๋‚ด์šฉ๋“ค์„ ์ •๋ฆฌํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (1) ๋น„์ „ ์žฌ์ •๋ฆฝ: โ€˜๊ธฐ์—…์˜ ์ƒˆ๋กœ์šด ๋„์•ฝ์„ ์œ„ํ•œ ๋น„์ „(Vision)์€ ๋ฌด์—‡์ธ๊ฐ€?โ€™ - ๊ธฐ์—…์˜ ๋ฏธ์…˜(Mission)์ด๋‚˜ ๋น„์ „(Vision), ๊ธฐ์—… ์ด๋…์„ ์žฌ์ •๋ฆฝํ•ด์•ผ ํ•œ๋‹ค. (2) ํฌํŠธํด๋ฆฌ์˜ค ์žฌ์ •๋ฆฝ: โ€˜์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค๋Š” ํ•ฉ๋ฆฌ์ ์ธ๊ฐ€?โ€™ - ํ˜„์žฌ ํฌํŠธํด๋ฆฌ์˜ค์˜ ์œ„์ƒ ํŒŒ์•… - ๊ฐ€์น˜ ์ฐฝ์ถœ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ๊ฒฝ์˜ํ˜์‹  ๋ฐฉ์•ˆ ์ˆ˜๋ฆฝ - ํฌํŠธํด๋ฆฌ์˜ค ๋น„์ „ ์ดˆ์•ˆ ๋งˆ๋ จ - ํฌํŠธํด๋ฆฌ์˜ค ๋น„์ „ ๋ฐ ์‹คํ–‰๊ณ„ํš ํ™•์ • (3) ๊ธฐ์—… ์ „๋žต ์ˆ˜๋ฆฝ: ๊ฒฝ์˜ ๋ชฉํ‘œ ๋ฐ ์ด์ต ๋‹ฌ์„ฑ์„ ์œ„ํ•œ ์ „๋žต์€ ๋ฌด์—‡์ธ๊ฐ€? - ์ค‘์žฅ๊ธฐ ๋ฐœ์ „ ๊ณ„ํš ๋ฐ Migration Path - ์„ฑ๊ณต์  ๋น„์ „ ๋‹ฌ์„ฑ์„ ์œ„ํ•œ ์ „๋žต๊ณผ์ œ ๋„์ถœ - ์‹ ๊ทœ ์‚ฌ์—…๊ธฐํšŒ ํƒ์ƒ‰ ๋ฐ ์ถ”์ง„์•ˆ ์ˆ˜๋ฆฝ - ์ „๋žต ๊ณผ์ œ์˜ ์šฐ์„ ์ˆœ์œ„ํ™” ๋ฐ Quick Win ๊ณผ์ œ ๋„์ถœ (4) ์ „๋žต๊ณผ์ œ์˜ ์„ฑ๊ณต์  ์‹คํ–‰์ „๋žต: ๊ธฐ์—… ๋ชฉํ‘œ ๋‹ฌ์„ฑ์„ ์œ„ํ•œ ์ˆ˜ํ–‰ ๋ฐฉ์•ˆ์€ ๋ฌด์—‡์ธ๊ฐ€? - ์ „๋žต ๋‹ฌ์„ฑ์„ ์œ„ํ•œ ์ตœ์ ์˜ ์กฐ์ง๊ตฌ์กฐ ์„ค๊ณ„ - ๊ฑฐ๋ž˜์„ ๋ณ„ ์ˆ˜์ต์„ฑ ๊ด€๋ฆฌ์ฒด๊ณ„ ์ •๋ฆฝ - ์ „๋žต์  ํˆฌ์ž ๊ฐœ์„  ๋ฐ ์กฐ์ • - ํ˜์‹ ์  ๋น„์šฉ ์ ˆ๊ฐ - ์‚ฌ์—… ๊ฐ„ ์‹œ๋„ˆ์ง€(synergy) ๊ฐœ์„  - ๋น„์šฉ ์ ˆ๊ฐ์˜ ๊ธฐํšŒ ์š”์†Œ ๋„์ถœ - ์‚ฌ์—… ๊ธฐํšŒ๋ณ„ ์ „๋žต์  ๋ชฉํ‘œ ์ œ์‹œ ๋ฐ ๊ด€๋ฆฌ์ฒด๊ณ„ ์ˆ˜๋ฆฝ - Value Chain ์ƒ์˜ ํ”„๋กœ์„ธ์Šค ๊ฐœ์„  ๋ฐฉ์•ˆ ์ œ์‹œ (5) ๋ณ€ํ™”๊ด€๋ฆฌ: ์ „๋žต๊ณผ์ œ์˜ ์„ฑ๊ณต์  ๋‹ฌ์„ฑ์„ ์œ„ํ•œ ๋ณ€ํ™”๊ด€๋ฆฌ ๋ฐฉ์•ˆ์€ ๋ฌด์—‡์ธ๊ฐ€? - ์žฌ๋ฌด ๋ฐ ์„ฑ๊ณผ ๊ณ„ํš ์ž‘์„ฑ ์ง€์› - ์ „๋žต๊ณผ์ œ ๋‹ฌ์„ฑ์„ ์œ„ํ•œ ์ง€์›์‚ฌํ•ญ ๋„์ถœ - Communication ๊ณ„ํš ์ˆ˜๋ฆฝ ๋‘ ๋ฒˆ์งธ, ๋™ํƒœ์ (Dynamic) ์ธก๋ฉด์—์„œ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์œผ๋กœ ํ”„๋กœ์„ธ์Šค(process)๊ฐ€ ๊ฐ•์กฐ๋˜๋Š” ๊ด€์ ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ๊ธฐ์—…์ด ์™ธ๋ถ€ ํ™˜๊ฒฝ๊ณผ ๋‚ด๋ถ€ ์—ญ๋Ÿ‰ ๋ถ„์„์„ ํ†ตํ•ด ๊ธฐ์—…์˜ ๊ฐ•์ ๊ณผ ํ–ฅํ›„์˜ ๋ณ€ํ™”๋ฅผ ํŒŒ์•…ํ•˜๊ณ  ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ตœ์ ์˜ ์˜์‚ฌ๊ฒฐ์ •์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณด๋ฉด Figure IV-8๊ณผ ๊ฐ™์€ ํ๋ฆ„์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. Figure IV-8. ๊ฒฝ์˜์ „๋žต์˜ ๊ตฌ์„ฑ - ๋™ํƒœ์  ์ธก๋ฉด ์™ธ๋ถ€ ํ™˜๊ฒฝ ๋ถ„์„ ๋ฐ ๋‚ด๋ถ€ ์—ญ๋Ÿ‰ ๋ถ„์„์„ ์œ„ํ•ด ์‚ฐ์—… ๋ฐ ์‚ฌ์—… ํ™˜๊ฒฝ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฐ•์ ๊ณผ ์•ฝ์ ์„ ๊ตฌ์ฒด์ ์œผ๋กœ ํŒŒ์•…ํ•œ๋‹ค. ์ด๋ฅผ ๋ฏธ๋ž˜ ํŠธ๋ Œ๋“œ์™€ ๋งž์ถ”์–ด ์‚ฌ์—… ๋ชจ๋ธ์ด๋‚˜ ์กฐ์ง์˜ ๋ณ€ํ™” ๋ฐฉํ–ฅ์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ „๋žต์  ์˜ต์…˜์„ ๋„์ถœํ•˜๊ณ  ์ด๋ฅผ ์ •๋Ÿ‰ํ™”ํ•˜์—ฌ ์‹คํ–‰ ํƒ€๋‹น์„ฑ์„ ๋ถ„์„ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ํ‰๊ฐ€ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ „๋žต ์ปจ์„คํŒ…์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด ๋™ํƒœ์  ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๊ฒฝ์˜ ์ „๋žต์˜ ๋‹ค์–‘ํ•œ ๊ณผ์ œ(Initiatives)๋ฅผ ๋„์ถœํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋ฉฐ ์ „๋žต๊ฒฝ์˜ ๊ด€์ ์˜ ์„ฑ๊ณผ ํ‰๊ฐ€๋ฅผ ์ปจ์„คํŒ…ํ•  ๊ฒฝ์šฐ,<NAME>์  ๊ด€์ ์—์„œ KPI๋ฅผ ์ฒด๊ณ„์ ์œผ๋กœ ์ˆ˜๋ฆฝํ•˜๊ฒŒ ๋œ๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ๋Š” ์ด๊ฒƒ๋“ค์— ๋Œ€ํ•ด ์ข€ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด์ž. Break #18. ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ๊ณผ ์ „๋žต์  ์˜์‚ฌ๊ฒฐ์ • ์ „๋žต ์ˆ˜๋ฆฝ ํ”„๋ ˆ์ž„์›Œํฌ์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ์ฐจ๊ณ  ๋„˜์ณ์„œ ๊ด€๋ จ๋œ ์ฑ… ํ•œ ๊ถŒ ๋ณธ ์‚ฌ๋žŒ๋“ค์€ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„ ์ž์‹ ๋งŒ์˜ ์ „๋žต ์ˆ˜๋ฆฝ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์ •๋„๋กœ ํŠน๋ณ„ํ•œ ๊ฒƒ๋„ ์•„๋‹ˆ๋ฉฐ ์–ด๋ ค์šด ๊ฒƒ๋„ ์•„๋‹ˆ๋‹ค. ๋ฌธ์ œ๋Š” ์‹ค์ œ๋กœ ๊ทธ๋Ÿฐ ๊ฒฝํ—˜์„ ์–ผ๋งˆ๋‚˜ ํ•ด ๋ณด์•˜์œผ๋ƒ ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ ๊ธ€๋กœ๋ฒŒ Top 3 ์ปจ์„คํŒ… ํŽŒ์˜ ์ปจ์„คํ„ดํŠธ๋“ค๋„ ๊ทธ ๋…ธ๋ จํ•จ์— ์žˆ์–ด์„œ๋Š” ๊ฒฝํ—˜์˜ ์œ ๋ฌด, ์ •๋„์— ๋”ฐ๋ผ ํฐ ์ฐจ์ด๋ฅผ ๋ณด์ธ๋‹ค. ๋ฐฉ๋ฒ•๋ก  ํ…Œ์ผ๋Ÿฌ๋ง์„ ์ œ๋Œ€๋กœ ๋ชปํ•ด์„œ ๋’ค์ฃฝ๋ฐ•์ฃฝ์ด ๋˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๊ณ  ๊ณ ๊ฐ์œผ๋กœ๋ถ€ํ„ฐ ์ฑŒ๋ฆฐ์ง€๋ฅผ ๋ฐ›๊ณ  ๋– ๋‚˜๋Š” ๊ฒฝ์šฐ๋„ ํ—ˆ๋‹คํ•˜๋‹ค. ์ €์ž์˜ ์ƒ๊ฐ์— ๊ทธ๊ฒƒ์€ ์ปจ์„คํ„ดํŠธ ๊ฐœ์ธ์˜ ์—ญ๋Ÿ‰์ด ๋ถ€์กฑํ•˜๋‹ค๊ธฐ๋ณด๋‹ค๋Š” '๊ฒฝํ—˜์˜ ๋ถ€์กฑ(Lack of Experience)'์—์„œ ์˜ค๋Š” ๋ฏธํกํ•œ ๋ณ€ํ™”๊ด€๋ฆฌ ๋Šฅ๋ ฅ ๋•Œ๋ฌธ์ด๋ผ ์ƒ๊ฐ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ์‹ ์ž… ์ปจ์„คํ„ดํŠธ๋“ค์€ ์„ ์ž„/์‹œ๋‹ˆ์–ด ์ปจ์„คํ„ดํŠธ๋“ค๊ณผ ๋ฐ˜๋“œ์‹œ ํ•จ๊ป˜ ํ”„๋กœ์ ํŠธ์— ํˆฌ์ž…๋˜๋Š” ๊ฒƒ์ด ํšŒ์‚ฌ์—๋„ ๊ฐœ์ธ์—๊ฒŒ๋„ ๋ฐ”๋žŒ์งํ•˜๋‹ค. ๊ฐ์„คํ•˜๊ณ  ์ด ์žฅ์—์„œ ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ<NAME>์ , ๋™ํƒœ์  ๊ด€์ ์—์„œ ์˜ˆ๋ฅผ ๋“ค์—ˆ์ง€๋งŒ ๋˜ Figure IV-9์™€ ๊ฐ™์ด ์ƒ๊ฐํ•˜๋Š” ์‚ฌ๋žŒ๋“ค๋„ ๋งŽ๋‹ค. ์ด ํ”„๋ ˆ์ž„์›Œํฌ(Framework)๊ฐ€ ๋งž๋‹ค ํ‹€๋ฆฌ๋‹ค๊ฐ€ ์•„๋‹ˆ๋ผ ์ด๋Ÿฐ ์ƒ๊ฐ์ด๊ณ  ์ €๋Ÿฐ ์ƒ๊ฐ์ด๋ฏ€๋กœ ๊ฐ€์ ธ๋‹ค๊ฐ€ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์ ์šฉํ•˜๋ฉด ๋œ๋‹ค. ์ฆ‰, ํ…Œ์ผ๋Ÿฌ๋ง ํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด๊ณ  ํ•ด๋‹น ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์ข‹๋‹ค๊ณ  ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์—ฌ๋Ÿฌ ์‚ฌ๋žŒ์ด ์•Œ์•„๋ณด๊ธฐ ์‰ฝ๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ณ  ๋˜<NAME>๊ธฐ ์šฉ์ดํ•˜๋ฉฐ ์ „๋žต ์‹คํ–‰์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ์„ค๊ณ„๋œ ๊ทธ๋Ÿฐ ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์ข‹๋‹ค๊ณ  ํ‰๊ฐ€๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. Figure IV-9. ๋น„์ „ ๋ฐ ์ „๋žต ์ˆ˜๋ฆฝ ํ”„๋กœ์„ธ์Šค ๊ทธ๋ž˜์„œ ๊ณผ๊ฑฐ์—๋Š” ์ปจ์„คํŒ… ํŽŒ์˜ ๋ฐฉ๋ฒ•๋ก ๋“ค์„ ์ฒ ์ €ํžˆ ๋น„๊ณต๊ฐœ, ๋น„๋ฐ€ ์‹œ ํ•˜์˜€์œผ๋‚˜ ์›น์œผ๋กœ ๊ฑฐ์˜ ๋ชจ๋“  ์ •๋ณด๊ฐ€ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ณต์œ ๋˜๋Š” ์š”์ฆ˜์€ ๊ทธ๋Ÿฐ ๋น„๋ฐ€์ด ์˜ค๋ž˜๊ฐ€์ง€๋„ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์ „์— ๋น„ํ•ด ๋งŽ์ด ๊ณต๊ฐœํ•˜๊ณ  ์žˆ๋‹ค. ์ปจ์„คํŒ… ํŽŒ ์ž…์žฅ์—์„œ๋Š” ์ด๋Š” ์‚ฌ์‹ค์ƒ์˜ ํ‘œ์ค€(De Facto Standard)์ด ๋ˆ„๋ฆฌ๋Š” ํšจ๊ณผ๋ฅผ ํ•œ๋ฒˆ ๋ˆ„๋ ค๋ณด๊ฒ ๋‹ค๋Š” ํฌ์„๋„ ์žˆ๋Š” ์…ˆ์ด๋‹ค. Figure IV-9์— ๋Œ€ํ•ด ์ข€ ๋” ์ด์•ผ๊ธฐํ•ด ๋ณด๋ฉด ๊ฒฐ๊ตญ ๊ฒฝ์˜์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๋Š” ๋ชจ๋“  ๋‹จ๊ณ„๋Š” ์ „๋žต์  ์˜์‚ฌ๊ฒฐ์ •(decision making)์ด๋ฉฐ ๊ฐ ๋‹จ๊ณ„๋ณ„ ์ „๋žต์ด ์œ ์˜๋ฏธํ•˜๊ฒŒ ์ž‘๋™๋œ๋‹ค๋Š” ๋ง์€ ์˜์‚ฌ๊ฒฐ์ • ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ž˜ ์ž‘๋™ํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ๋˜, Figure IV-10๊ณผ ๊ฐ™์€ ํ”„๋ ˆ์ž„์›Œํฌ๋„ ์žˆ๋‹ค. Figure IV-10. ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ ํ”„๋ ˆ์ž„์›Œํฌ Figure IV-10์€ ์ „์‚ฌ ์ฐจ์›์˜ Top-Down ์ค‘์žฅ๊ธฐ ๊ฒฝ์˜์ „๋žต๊ณผ ์‚ฌ์—…๋ถ€(SBU) ์ตœ์ ํ™”๋ฅผ ์ถ”๊ตฌํ•˜๋Š” Bottom-Up ์ •๋ณด๋ฅผ ์—ฐ๊ณ„ํ•˜์—ฌ ์ค‘์žฅ๊ธฐ ๊ฒฝ์˜์ „๋žต๊ณผ ์‚ฌ์—…๋ถ€ ์ „๋žต์ด ์ƒํ˜ธ ์ผ๊ด€์„ฑ(Alignment)์„ ์œ ์ง€ํ•˜๊ณ  ๋‘˜ ๊ฐ„์˜ ์ƒํ˜ธ๋ณด์™„์„ฑ์„ ๊ณ ๋ คํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. Figure IV-10๊ณผ ๊ฐ™์€ ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์†Œ๊ฐœ๋˜๋ฉด ๋ฐ˜๋“œ์‹œ Figure IV-11๊ณผ ๊ฐ™์€(๋˜๋Š” ์ด์™€ ์œ ์‚ฌํ•œ) ์‚ฌ์—… ์ „๋žต(business strategy) ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ๋™๋ฐ˜๋œ๋‹ค. Figure IV-11. ์‚ฌ์—…์ „๋žต ์ˆ˜๋ฆฝ ํ”„๋ ˆ์ž„์›Œํฌ Figure IV-11์€ ์ฃผ์š” ์ œํ’ˆ ๋ฐ ์„œ๋น„์Šค, ์ฃผ์š” ๊ณ ๊ฐ, ๊ฒฝ์Ÿ์ž, ๊ณต๊ธ‰์ž, ์ œ๊ณต ๊ฒฝ๋กœ ๋“ฑ์„ ๊ณ ๋ คํ•œ ์‚ฌ์—… ๋‹จ์œ„ ์ „๋žต ์ˆ˜๋ฆฝ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋„์‹ํ™”ํ•œ ๊ฒƒ์ด๋‹ค. ๊ฒฐ๊ตญ ์ „๋žต ์˜ต์…˜ ๋˜๋Š” ์ „๋žต ๋Œ€์•ˆ์„ ๊ฐœ๋ฐœํ•˜์—ฌ ์‹คํ–‰ํ•˜๊ณ  ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋˜์–ด ์žˆ๋‹ค. ์‚ฌ์—…์ „๋žต ์ˆ˜๋ฆฝ์— ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ข… ๋ถ„์„ ๊ธฐ๋ฒ•์€ Part III์—์„œ ์†Œ๊ฐœํ•œ ๊ฒƒ๋“ค์„ ํ™œ์šฉํ•˜๋ฉด ๋œ๋‹ค. [1] ์‚ฌ์‹ค ์˜ค๋‹ค ๋…ธ๋ถ€๋‚˜๊ฐ€๊ฐ€ 80% ์ด์ƒ ์ „๊ตญ์‹œ๋Œ€์˜ ์ผ๋ณธ์„ ํ†ต์ผํ•˜๊ณ  ์ฃฝ๊ฒŒ ๋˜์ž, ์˜ค๋‹ค ๋…ธ๋ถ€๋‚˜๊ฐ€์˜ ํ•˜์ธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์žฅ๊ตฐ๊นŒ์ง€ ์˜ค๋ฅธ ๋„์š”ํ† ๋ฏธ ํžˆ๋ฐ์š”์‹œ๊ฐ€ ์™„์ „ํ•œ ํ†ต์ผ์„ ์ด๋ฃจ๊ฒŒ ๋œ๋‹ค. [2] ์‹ค์ œ๋กœ ๋…ผ๋ฆฌ์  ์ ‘๊ทผ๊ณผ ์˜์ง€์  ์ ‘๊ทผ์€ ๋‹ค๋ฅด์ง€๋งŒ ๋งŽ์€ ์ปจ์„คํŒ… ํŽŒ์˜ ์ „๋žต ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•๋ก ์€ ์ด๊ฒƒ๋“ค์ด ์„œ๋กœ ์„ž์—ฌ ์žˆ๋‹ค๋Š” ๋Š๋‚Œ์ด ๋งŽ์ด ๋“ ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ž˜๋ชป ์ ์šฉํ•˜๋ฉด ์ฃฝ๋„ ๋ฐฅ๋„ ์•ˆ๋˜๋Š” ์ด์ƒํ•œ ๊ฒฐ๋ก ์ด ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. 13. ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•๋ก (2/3) 13.2 ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ ํ”„๋ ˆ์ž„์›Œํฌ 13.1์žฅ์—์„œ ์‚ดํŽด๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ๊ธฐ์—…์˜ ๋น„์ „ ์ˆ˜๋ฆฝ ๊ด€์ ์ด๋‚˜ ๊ธฐ์—…์˜ ๊ฒฝ์˜ ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๋‹ค์–‘ํ•˜๋‹ค. 2000๋…„๋Œ€ ๋“ค์–ด ๊ฒฝ์˜์ „๋žต์€ ๋‘ ๊ฐ€์ง€ ํฐ ํ™”๋‘์—์„œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํŒŒ์ƒ์ ์ธ ๋…ผ์˜, ์ด๋ก , ๋ฐฉ๋ฒ•๋ก , ์‚ฌ๋ก€๋“ค์ด ์ •๋ฆฌ๋˜๊ณ  ์žˆ๋Š”๋ฐ ๊ทธ ํ•˜๋‚˜๋Š” ์ž์› ์ค€ ๊ฑฐ๋ก [1]์— ๊ธฐ๋ฐ˜ํ•œ โ€˜Resource-based Strategyโ€™์ด๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” โ€˜Opportunity-based Strategyโ€™์ด๋‹ค. Table IV-4๋Š” ๋‘ ๊ฐ€์ง€ ๊ด€์ ์˜ ์ „๋žต์„ ๋น„๊ตํ•œ ๊ฒƒ์ธ๋ฐ ๊ฐ๊ฐ์˜ ์ „๋žต ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•์„ ์ถ”์ข…ํ•˜๋Š” ๊ธฐ์—…๋“ค๋„ ์—ญ๋Ÿ‰ ์ฃผ์˜๋ฅผ ์ถ”์ข…ํ•˜๋Š” ๊ธฐ์—…๋“ค๊ณผ ๊ธฐํšŒ์ฃผ์˜๋ฅผ ์ถ”์ข…ํ•˜๋Š” ๊ธฐ์—…๋“ค๋กœ ๊ตฌ๋ถ„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Table IV-4. ๊ฒฝ์˜์ „๋žต ํŠธ๋ Œ๋“œ์˜ ๋น„๊ต ์ฒซ ๋ฒˆ์งธ, ์—ญ๋Ÿ‰ ์ฃผ์˜๋ฅผ ์ถ”์ข…ํ•˜๋Š” ๊ธฐ์—…๋“ค์€ ๊ธฐ์—…์˜ ๊ฐ•์ ๊ณผ ์•ฝ์ ์„ ์ž˜ ํŒŒ์•…ํ•˜์—ฌ ๋ณ€ํ™”ํ•˜๋Š” ํ™˜๊ฒฝ์ด๋‚˜ ์‹œ์žฅ์— ์–ด๋–ป๊ฒŒ ์ ์‘ํ•  ๊ฒƒ์ธ์ง€, ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ์—ญ๋Ÿ‰์ด๋‚˜ ์ž์›์„ ๊ทน๋Œ€ํ™”ํ•  ๊ฒƒ์ธ์ง€ ๊ณ ๋ฏผํ•œ๋‹ค. Figure IV-12๋Š” ์—ญ๋Ÿ‰ ์ฃผ์˜ ๊ด€์ ์—์„œ ๊ฒฝ์˜์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ฒƒ์ด๋‹ค. Figure IV-12. ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ์˜ ์ ˆ์ฐจ - ์—ญ๋Ÿ‰ ์ฃผ์˜ ๊ด€์  ํฌ๊ฒŒ 'Foresight Step', 'Insight Step', 'Strategy Formulation Step'์˜ 3๋‹จ๊ณ„๋กœ ๋‚˜๋ˆ„์–ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. Foresight Step์€ ์™ธ๋ถ€ํ™˜๊ฒฝ ๋ถ„์„์„ ํ†ตํ•ด ๋ฏธ๋ž˜์— ๋Œ€ํ•œ ์„ ๊ฒฌ์ง€๋ช…(ๅ…ˆ่ฆ‹ไน‹ๆ˜Ž. Foresight)๋ฅผ ์ด๋Œ์–ด๋‚ด๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ฉ”๊ฐ€ํŠธ๋ Œ๋“œ(Megatrend) ๋ถ„์„์ด๋‚˜ ์‚ฐ์—… ๋ถ„์„, ์‹œ์žฅ ๋ถ„์„, ๊ฒฝ์Ÿ ๋ถ„์„ ๋“ฑ์„ ์ฃผ์š” ๊ธฐ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Insight Step์€ ๊ธฐ์—…์˜ ๊ฐ•์ ๊ณผ ์•ฝ์ , ๊ฒฝ์Ÿ ํ˜„ํ™ฉ, Value Chain ๋“ฑ์„ ๋ถ„์„ํ•˜์—ฌ ํ†ต์ฐฐ๋ ฅ(Insight)์„ ๊ฐ€์ง€๊ณ  ๊ฒฝ์Ÿ ์šฐ์œ„๋ฅผ ๋ช…ํ™•ํ™”ํ•˜๋Š” ๋‹จ๊ณ„์ด๋ฉฐ, Strategy Formulation Step์€ ๋‘ ๊ฐ€์ง€ ๋ถ„์„์˜ ์‹œ์‚ฌ์ ์„ ์ข…ํ•ฉํ•˜์—ฌ ๋น„์ „(Vision)์„ ์ •๋ฆฝํ•˜๊ณ  ๋น„์ „ ๋‹ฌ์„ฑ์„ ์œ„ํ•œ ์ „๋žต๊ณผ ์ „๋žต ๊ณผ์ œ๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š” ๋‹จ๊ณ„์ด๋‹ค. ํŠนํžˆ, ์ค‘์žฅ๊ธฐ ๊ฒฝ์˜ ๋ชฉํ‘œ๋ฅผ ์ •๋Ÿ‰ํ™”ํ•˜๋Š” ๊ณผ์ •์—์„œ BHAG[2]์„ ๋ช…์‹œํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ๊ธฐํšŒ์ฃผ์˜๋ฅผ ์ถ”์ข…ํ•˜๋Š” ๊ธฐ์—…๋“ค์€ ์‹œ์žฅ์˜ ๊ธฐํšŒ, ์ข€ ๋” ๊ตฌ์ฒดํ™”ํ•˜๋ฉด ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๋ฅผ ๊ฐ„ํŒŒํ•ด์•ผ ํ•œ๋‹ค. ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๋ฅผ ์ถฉ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๋‚ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์€ ๊ฒƒ(์ž์›)๊นŒ์ง€ ์—ฐ๊ฒฐํ•˜๊ณ  ์œตํ•ฉํ•˜์—ฌ ๊ณ ๊ฐ์—๊ฒŒ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋Œ€๋ถ€๋ถ„์˜ ์„ค๋ฃจ์…˜ ๊ธฐ์—…๋“ค์ด ์ด๋Ÿฐ ์ „๋žต์„ ์ทจํ•œ๋‹ค[3]. ๊ธฐํšŒ์ฃผ์˜์— ๊ธฐ๋ฐ˜ํ•œ ์„ค๋ฃจ์…˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ๋ง์„ ์ƒ๊ฐํ•ด ๋ณธ๋‹ค๋ฉด Figure IV-13๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. Figure IV-13. ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ์˜ ์ ˆ์ฐจ - ์„ค๋ฃจ์…˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ์ „๋žต ์ˆ˜๋ฆฝ ์„ค๋ฃจ์…˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ๋ง์€ ํฌ๊ฒŒ 6๋‹จ๊ณ„๋กœ ๋‚˜๋‰˜๋Š”๋ฐ ๊ทธ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (1) ์ด์Šˆ ๋ฐ ๋ฌธ์ œ ์ธ์‹ - ์ง„๋‹จ์ด๋‚˜ ์ปจ์„คํŒ…์„ ํ†ตํ•ด ๊ณ ๊ฐ์„ฑ๊ณผ ์ฐฝ์ถœ์˜ ์ด์Šˆ๋‚˜ ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•จ (2) ์„ค๋ฃจ์…˜ ๊ฐœ๋… ์ •๋ฆฝ - ๊ณ ๊ฐ์˜ ์ด์Šˆ๋‚˜ ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์„ ํƒ์ƒ‰ํ•จ (3) ์ œํ’ˆ/์„œ๋น„์Šค ๊ตฌ์„ฑ - ๊ฐœ๋…ํ™”๋œ ์„ค๋ฃจ์…˜์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ œํ’ˆ(์ƒํ’ˆ) ๋ฐ ์„œ๋น„์Šค ํƒ์ƒ‰. ํ•„์š”์‹œ ์—ฐ๊ตฌ๊ฐœ๋ฐœ(R&D)์„ ํ•˜๊ธฐ๋„ ํ•˜๋‚˜ C&D๋‚˜ A&D[4]๊ฐ€ ๋ณด๋‹ค ๋น„์šฉ ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ๋‹ค. (4) ํ”„๋กœ์„ธ์Šค ๋ฐ ์ธํ”„๋ผ ํ™•๋ณด -<NAME> ๊ฐ€๋Šฅํ•œ ๋‚ด/์™ธ๋ถ€ ์ž์›์„ ๊ฒฐํ•ฉํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค์™€ ์ธํ”„๋ผ ํ™•๋ฆฝ (5) ์˜คํผ๋ง(์ œํ’ˆ/์„œ๋น„์Šค) ๊ณต๊ธ‰ - ๊ฐœ๋ณ„ ๊ณ ๊ฐ ๋งž์ถคํ˜• ์ œํ’ˆ ๋ฐ ์„œ๋น„์Šค ๊ณต๊ธ‰ (6) ๊ณ ๊ฐ์˜ ์„ฑ๊ณผ ํ™•์ธ - ๊ณ ๊ฐ์—๊ฒŒ ์„ค๋ฃจ์…˜ ์˜คํผ๋ง์„ ์ œ๊ณตํ•˜๊ณ  ๊ณ ๊ฐ์˜ ์„ฑ๊ณผ๋ฅผ ํ™•์ธ ์ด์ฒ˜๋Ÿผ ์„ค๋ฃจ์…˜ ๋น„์ฆˆ๋‹ˆ์Šค๋ฅผ ์ถ”๊ตฌํ•˜๋Š” ๊ธฐ์—…์˜ ๊ฒฝ์šฐ, ์‹œ์žฅ์˜ ๊ธฐํšŒ๋‚˜ ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๋ฅผ ๋ฐœ๊ตดํ•˜๊ธฐ ์œ„ํ•ด ์ปจ์„คํŒ… ๊ธฐ๋Šฅ์ด ์ค‘์š”ํ•˜๊ฒŒ ๋ถ€๊ฐ๋˜๋ฉฐ ์ด๋ฅผ ํ•ต์‹ฌ ์—ญ๋Ÿ‰์œผ๋กœ ์ถ”๊ตฌํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฒฝํ–ฅ์ด ๋งŽ๋‹ค. ๊ทธ๋ž˜์„œ ์„ค๋ฃจ์…˜ ์‚ฌ์—… ๊ณ ๋ฏผํ•œ๋‹ค. (1/2) ์„ค๋ฃจ์…˜์ด ๋ญ๋ผ๊ณ ? ์•„ ... ๋‚œ ์—ฌํƒœ๊นŒ์ง€ ์ž˜๋ชป ์•Œ๊ณ  ์žˆ์—ˆ๋„ค. | B2B ์˜์—…์€ ์˜์—… ๋ฐฉ์‹(Sales Motion)์— ๋”ฐ๋ผ Volume Sales์™€ Value Sales๋กœ ๊ตฌ๋ถ„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ Volume Sales๋Š” ๋Œ€๋Ÿ‰(Bulk)์˜ ์ œํ’ˆ์„ ์œ ํ†ต์ฑ„๋„์„ ํ†ตํ•ด ๊ธฐ์—…๊ณ ๊ฐ์—๊ฒŒ ํŒ๋งคํ•˜๋Š” ๊ฒƒ์œผ๋กœ, B2C ์„ธ์ผ์ฆˆ์ฒ˜๋Ÿผ ์ฑ„๋„ ๋ฐ ํŒŒํŠธ๋„ˆ ๊ด€๋ฆฌ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. ๊ทธ๋Ÿฐ๋ฐ Value Sales๋Š” B2C ์„ธ์ผ์ฆˆ ๋˜๋Š” Volume Sales์™€๋Š” ์ƒ๋‹นํžˆ brunch.co.kr/@flyingcity/8 13.3 ๊ฒฝ์˜์ „๋žต ์šด์˜ ํ”„๋ ˆ์ž„์›Œํฌ- BSC ๊ฒฝ์˜์ „๋žต ์šด์˜ ํ”„๋ ˆ์ž„์›Œํฌ ์ด๋ž€ ๊ฒฐ๊ตญ ์ „๋žต๊ณผ์ œ๋“ค์ด ์–ด๋–ป๊ฒŒ ์‹คํ–‰๋˜๋Š”์ง€ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ณ  ๊ทธ ์„ฑ๊ณผ๋ฅผ ์ฒด๊ณ„์ ์œผ๋กœ ๋‹ฌ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด์™€ ๊ด€๋ จํ•˜์—ฌ ๊ฐ€์žฅ ๋„๋ฆฌ ์•Œ๋ ค์ง„ ์ปจ์„คํŒ… ๊ธฐ๋ฒ•์€ ๊ท ํ˜•์„ฑ๊ณผ ์ง€ํ‘œ(Balanced Scorecard. BSC)๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. BSC๋Š” ์ „๋žต ๋ชฉํ‘œ์— ๊ทผ๊ฑฐํ•˜์—ฌ ์žฌ๋ฌด, ๊ณ ๊ฐ, ํ”„๋กœ์„ธ์Šค, ์„ฑ์žฅ๊ณผ ํ•™์Šต ๊ด€์ ์˜ ํ•ต์‹ฌ ์„ฑ๊ณต์š”์†Œ๋ฅผ ์ธก์ • ๊ฐ€๋Šฅํ•œ KPI๋กœ ๊ตฌ์ฒดํ™”ํ•˜์—ฌ ์กฐ์ง ์ „์ฒด์˜ ์ „๋žต ๋ชฉํ‘œ์— ๋Œ€ํ•œ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•˜๊ณ  ์ „์‚ฌ ์—ญ๋Ÿ‰์„ ์ „๋žต ๋ชฉํ‘œ์— ์ง‘์ค‘์‹œํ‚ด์œผ๋กœ์จ ์‚ฌ์—… ์ „๋žต์„ ์‹คํ–‰์œผ๋กœ ์ „ํ™˜์‹œ์ผœ์ฃผ๋Š” ๊ฒฝ์˜์ „๋žต ์šด์˜ ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค. Figure IV-14๋Š” BSC ์ฒด๊ณ„ ํ•˜์—์„œ ๋น„์ „๋ถ€ํ„ฐ KPI๊นŒ์ง€ ๋ชฉํ‘œ์— ํ•ฉ์น˜๋˜๊ณ  ์žˆ์Œ์„ ๋ณด์—ฌ์ค€๋‹ค. Figure IV-14. BSC์˜ ๊ฐœ๋… Figure IV-15๋Š” BSC์˜ ๊ฐ ์ˆ˜์ค€๋ณ„ ๊ณ ๋ ค ์‚ฌํ•ญ๋“ค๊ณผ ์‚ฌ๋ก€๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋‹ค. '์žฌ๋ฌด', '๊ณ ๊ฐ', '๋‚ด๋ถ€ ํ”„๋กœ์„ธ์Šค', 'ํ•™์Šต๊ณผ ์„ฑ์žฅ'์ด๋ผ๋Š” BSC 4๊ฐ€์ง€ ๊ด€์ ์—์„œ ํ•ต์‹ฌ ์„ฑ๊ณต์š”์†Œ๋ฅผ ๋„์ถœํ•˜๊ณ  ํ•ต์‹ฌ์„ฑ๊ณผ์ง€ํ‘œ์™€ ์‹ค์ œ ๋ชฉํ‘œ๋ฅผ ์—ฐ๊ณ„ํ•˜์—ฌ ์ข…ํ•ฉ์ ์ธ ๊ฒฝ์˜ ์ฒด๊ณ„๋ฅผ ์ˆ˜๋ฆฝํ•œ๋‹ค. Figure IV-15์˜ ํ•ต์‹ฌ ์„ฑ๊ณต์š”์†Œ ์•„๋ž˜ ๊ทธ๋ฆผ์€ ์ „๋žต๋งต(Strategy Map)์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ฒƒ์ธ๋ฐ BSC์˜ 4๊ฐ€์ง€ ๊ด€์ ์—์„œ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋Š” ์ „๋žต ์˜์—ญ์„ ๋„์‹ํ™”ํ•˜์—ฌ ์„œ๋กœ ์—ฐ๊ณ„์„ฑ์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. ๋‹น์—ฐํžˆ KISS ์›์น™[5]์ด ์ค‘์š”ํ•˜๋‹ค. ๋งต์ด ๋„ˆ๋ฌด ๋งŽ๊ณ  ๋ณต์žกํ•˜๋ฉฐ ๊ทธ ์—ฐ๊ณ„๊ฐ€ ์‹คํƒ€๋ž˜ ์—ฎ์ธ ๊ฒƒ ๊ฐ™๋‹ค๋ฉด ์ƒ๊ฐ์„ ์ข€ ๋” ํ•ด๋ณด์•„์•ผ ํ•œ๋‹ค. Figure IV-15. BSC์˜ ์ฃผ์š” ๋‚ด์šฉ ๋˜ํ•œ, BSC ์ปจ์„คํŒ…์€ Figure IV-16๊ณผ ๊ฐ™์ด ํฌ๊ฒŒ ์„ฑ๊ณผ์ธก์ • ๋ชจ๋ธ๊ณผ ์„ค๋ฃจ์…˜ ๊ตฌ์ถ•์˜ ๋‘ ๋‹จ๊ณ„๋ฅผ ๊ฑธ์ณ ์ง„ํ–‰๋œ๋‹ค. 1๋‹จ๊ณ„ ์ปจ์„คํŒ…์€ ๊ธฐ์—…์˜ ์„ฑ๊ณผ ์ฐฝ์ถœ ์š”์†Œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ๊ทธ ์„ฑ๊ณผ๋ฅผ ์ •๋Ÿ‰์ ์œผ๋กœ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•ด ํ•ต์‹ฌ์„ฑ๊ณผ์ง€ํ‘œ(KPI: Key Performance Indicators)๋ฅผ ๋„์ถœํ•˜๋Š” ๊ฒƒ์„ ์ปจ์„คํŒ…์˜ ์ฃผ์š” ๋‚ด์šฉ์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉฐ, 2๋‹จ๊ณ„๋Š” ๊ฐ ํ˜„์žฅ์—์„œ KPI์˜ ์„ฑ๊ณผ ์ž…๋ ฅ์ด ์‰ฝ๋„๋ก ๋˜ ์„ฑ๊ณผ๋ฅผ ์ทจํ•ฉํ•˜๊ณ  ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๋Š” ์ผ์ด ์‰ฝ๋„๋ก ํ•˜๋Š” ์ •๋ณด์‹œ์Šคํ…œ์„ ์„ค๊ณ„ํ•˜๊ณ  ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด ๋œ๋‹ค. 2๋‹จ๊ณ„์—์„œ BSC ์ปจ์„คํ„ดํŠธ๋Š” KPI ์„ค๊ณ„๋ฅผ ์‹œ์Šคํ…œ ์ˆ˜์ค€๊นŒ์ง€ ์ƒ์„ธํ•˜๊ฒŒ ํ•˜๊ฑฐ๋‚˜ ํ”„๋กœ์ ํŠธ PMO๋กœ ํ™œ๋™ํ•˜๊ฒŒ ๋œ๋‹ค. Figure IV-16. BSC ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•๋ก  ์‚ฌ๋ก€ ์ €์ž๊ฐ€ BSC๋ฅผ ์ฒ˜์Œ ์ ‘ํ•œ ๊ฒƒ์€ 2004๋…„์ด์—ˆ๋Š”๋ฐ BSC๋Š” ๊ทธ์ฆˆ์Œ ์ „๋žต๊ฒฝ์˜์˜ ์„ฑ๊ณผ๊ด€๋ฆฌ ๋„๊ตฌ๋กœ ํ™”๋‘๊ฐ€ ๋˜๊ณ  ์žˆ์—ˆ๋‹ค. ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ํฌ๊ฒŒ 3๊ฐ€์ง€ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ์ทจํ–ˆ๋Š”๋ฐ ๊ธ€๋กœ๋ฒŒ Top 3 ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…๊ณผ ๊ธ€๋กœ๋ฒŒ Big 4 ํšŒ๊ณ„ ํŽŒ์˜ ์ปจ์„คํŒ… ํŒŒํŠธ, ๊ทธ๋ฆฌ๊ณ  SI ๊ธฐ์—…์ด๋‚˜ IT ์„ค๋ฃจ์…˜ ๊ธฐ์—…๋“ค์˜ ์ปจ์„คํŒ… ํŒŒํŠธ๊ฐ€ ์‚ฌ์—…์„<NAME>์˜€๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ณด๋ฉด SI ๊ธฐ์—…์ด๋‚˜ IT ์„ค๋ฃจ์…˜ ๊ธฐ์—…์˜ ์ปจ์„คํŒ… ํŒŒํŠธ๊ฐ€ ์ข‹์€ ์„ฑ๊ณผ๋ฅผ ์–ป์—ˆ๋‹ค. ๊ธ€๋กœ๋ฒŒ Top 3์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ BSC์˜ ์ „๋žต์  ์˜๋ฏธ๋ฅผ ์ œ์‹œํ•˜๊ณ  ๊ธฐ์—… ํ˜์‹ ์˜ ๋ฐฉํ–ฅ์„<NAME>๋Š” ๋ถ€๋ถ„์— ์ง‘์ค‘ํ•˜์˜€๊ณ  ์ด๋กœ ์ธํ•ด ๋ถ„๋ช…ํžˆ ์„ ๋„์ ์ธ ์ž…์ง€๋ฅผ ๊ฐ–๊ฒŒ ๋˜์—ˆ์œผ๋‚˜ ๊ทธ ์ด์ƒ๋„ ๊ทธ ์ดํ•˜๋„ ์•„๋‹ˆ์—ˆ๋‹ค. ๊ธ€๋กœ๋ฒŒ Big 4 ํšŒ๊ณ„ ํŽŒ์˜ ์ปจ์„คํŒ… ํŒŒํŠธ๋“ค์€ ์ „๋žต ํŽŒ๋ณด๋‹ค 1, 2 ๋ ˆ๋ฒจ ๋” ๋‚ฎ๊ฒŒ ์‹ค์งˆ์ ์œผ๋กœ ์ปจ์„คํŒ…์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๊ธฐ์กด์˜ ํšŒ๊ณ„ ์‹œ์Šคํ…œ๊ณผ ์ƒˆ๋กœ์šด ๋ฐฉ์‹์˜ ์„ฑ๊ณผ๋ฅผ ์—ฐ๋™์‹œํ‚จ๋‹ค๋Š” ์ธก๋ฉด์—์„œ ๊ฐ€์žฅ ์ž˜ ์ง„ํ–‰ํ•  ์ˆ˜๋„ ์žˆ์—ˆ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹จ์ˆœํžˆ ์ปจ์„คํŒ…๋งŒ์„ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ BSC ์‹œ์Šคํ…œ ์ฐจ์›์œผ๋กœ ์‹œ์žฅ์ด ์ปค์ง€๋ฉด์„œ ์‹ค์งˆ์ ์ธ ์‹œ์Šคํ…œ์˜ ๊ตฌ์ถ•๊นŒ์ง€ ์ฑ…์ž„์ ธ์•ผ ํ•˜๋Š” IT ์„ค๋ฃจ์…˜ ๊ธฐ์—…๋“ค์ด ๊ฐ€์žฅ ์œ ๋ฆฌํ•œ ๊ณ ์ง€๋ฅผ ์ฐจ์ง€ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. IT ์„ค๋ฃจ์…˜ ๊ธฐ์—…๋“ค์€ ๋ฐ์ดํ„ฐ์›จ์–ดํ•˜์šฐ์Šค(DW) ์‹œ์Šคํ…œ์„ ์ ๊ทน ์ด์šฉํ•˜๋ ค๋Š” ์ „๋žต์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ํฌ๊ฒŒ 2๊ฐ€์ง€ ๊ด€์ ์ด ๊ฐ•์กฐ๋˜์—ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ๋ฐ ์ˆ˜์ง‘ ๋ฐ์ดํ„ฐ ๋ชจ๋‹ˆํ„ฐ๋ง (Dashboard) ๋ฐ์ดํ„ฐ ๋ชจ๋‹ˆํ„ฐ๋ง๋ถ€ํ„ฐ ์ด์•ผ๊ธฐํ•˜์ž๋ฉด BI(Business Intelligence)๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ์˜์—ญ์„ ํ†ตํ•ด ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋‹ค์–‘ํ•œ ๊ทธ๋ž˜ํ”„์™€ ํ‘œ, ์‹ค์‹œ๊ฐ„ ํ‘œ์ถœ ๋“ฑ์„ ๊ฐ•์กฐํ•˜์˜€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์ด๋‚˜ ์ˆ˜์ง‘์€ ๋ฌธ์ œ์˜€๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ BSC๋Š” ์ง€๊ธˆ๋„ ๊ทธ๋ ‡์ง€๋งŒ 2๊ฐ€์ง€ ํฐ ๋ฌธ์ œ์— ๋ด‰์ฐฉํ•ด ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ๋น„์ „๋ถ€ํ„ฐ ์ „๋žต, ์‹คํ–‰๊ณผ์ œ๊นŒ์ง€ ์—ฐ๊ณ„ํ•œ ๊ธฐ์—…์˜ ์„ฑ๊ณผ๋ฅผ ์žฌ๋ฌด ์ธก๋ฉด์— ์น˜์šฐ์น˜์ง€ ์•Š๊ณ  ๊ท ํ˜• ์žˆ๊ฒŒ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋Š๋ƒ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์—์„œ ์ •์„ฑ์ ์ธ ๋ถ€๋ถ„์˜ ์„ฑ๊ณผ๋ฅผ ์—ฌ์ „ํžˆ ์ธ์ •๋ฐ›์ง€ ๋ชปํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋Š” ์ •๋Ÿ‰ํ™”์˜ ์ด์Šˆ์™€ ๋งž๋ฌผ๋ฆฐ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ๋‹ค์–‘ํ•œ ์„ฑ๊ณผ ๋ฐ์ดํ„ฐ์˜ ์ ์‹œ ์ž…๋ ฅ ๋ฐ ์ˆ˜์ง‘์˜ ๋ฌธ์ œ์ด๋‹ค. ์ด ๋ถ€๋ถ„์€ ์ตœ๊ทผ ์ œ4์ฐจ ์‚ฐ์—… ํ˜๋ช…์˜ ๋…ผ์˜๊ฐ€ ํ™œ๋ฐœํ•ด์ง€๋ฉด์„œ ๋กœ๋ด‡์ด๋‚˜ ์ž๋™ํ™” ์˜์—ญ์ด ํ™•์žฅ๋˜๋ฉด ๋ ์ˆ˜๋ก ๋นจ๋ฆฌ ํ•ด๊ฒฐ๋  ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. BSC๊ฐ€ ์•Œ๋ ค์ง„ ์ง€ ๋ช‡ ํ•ด ์žˆ์œผ๋ฉด 30๋…„์ด ๋œ๋‹ค. ์•„๋งˆ ์ƒˆ๋กญ๊ฒŒ ์กฐ๋ช… ๋ฐ›์„ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. [1] Resource-based View: RBV [2] Big Hairy Audacious Goal ์˜ˆ. 2020๋…„ ์˜์—…์ด์ต 5์กฐ ์› [3] ๊ธฐํšŒ์ฃผ์˜๋ฅผ ์ทจํ•œ๋‹ค๊ณ  ํ•ด์„œ ๊ธฐ์—…์˜ ๊ฐ•์ ๊ณผ ์•ฝ์ ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ „๋žต์˜ ํฐ ๊ทธ๋ฆผ์ด ์ž์›์˜ ๋ณด์œ  ์—ฌ๋ถ€์— ์ œํ•œ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. [4] Connect & Development ; Acquisition & Development [5] Keep It Simple, Stupid ๊ฐ„๋‹จํ•œ ๊ฒƒ, ๊ฐ„๋žตํ•œ ๊ฒƒ์ด ์ข‹๋‹ค. 13. ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•๋ก (3/3) ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” 'ํฌํŠธํด๋ฆฌ์˜ค(Portfolio)'์˜ ์˜๋ฏธ๋Š” '์„œ๋ฅ˜ ๊ฐ€๋ฐฉ' ๋˜๋Š” '์ž๋ฃŒ ์ˆ˜์ง‘์ฒ '์ด๋ผ๋Š” ์˜๋ฏธ์ธ๋ฐ ๊ด‘๊ณ ์‚ฐ์—…์—์„œ๋Š” ๋‹ค์–‘ํ•œ ์ž‘ํ’ˆ์ง‘์„ ๋œปํ•˜๋ฉฐ ๊ฑด์ถ•, ํˆฌ์ž๋ฅผ ํฌํ•จํ•ด์„œ ํฌํŠธํด๋ฆฌ์˜ค๋Š” ์˜๋ฏธ ์žˆ๋Š” ๋ฌด์–ธ๊ฐ€๋ฅผ ์ข…ํ•ฉํ•˜์—ฌ ๋‚˜ํƒ€๋ƒ„์„ ๋œปํ•œ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…์—์„œ ํฌํŠธํด๋ฆฌ์˜ค๋Š” ๊ธˆ์œต ํˆฌ์ž์˜ ํฌํŠธํด๋ฆฌ์˜ค ์˜๋ฏธ์™€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šธ ๊ฒƒ ๊ฐ™๋‹ค. ์‚ฌ์—…์„ ์˜๋ฏธ ์žˆ๋Š” ๊ธฐ์ค€์œผ๋กœ ์ •์˜ํ•˜๊ณ  ๊ตฌ๋ถ„ํ•˜์—ฌ ์ง€์†์ ์œผ๋กœ ๋ณ€ํ™”๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ํฌํŠธํด๋ฆฌ์˜ค ๊ด€๋ฆฌ(Portfolio Management)์ด๋‹ค. ์‚ฌ์—… ์ž์ฒด๊ฐ€ ๋Œ€์ƒ์ด ๋  ์ˆ˜๋„ ์žˆ๊ณ , ์ œํ’ˆ์ด๋‚˜ ๋ธŒ๋žœ๋“œ๋„ ๊ทธ ๋Œ€์ƒ์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ ๋ฐฉ๋ฒ•๋ก ์˜ ๋งˆ์ง€๋ง‰ ์ˆœ์„œ์—์„œ๋Š” ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ๊ด€๋ฆฌ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์ž. 13.4 ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ๋ถ„์„ ์ธํŠธ๋กœ์—์„œ ๊ฐ„๋‹จํžˆ ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ ํฌํŠธํด๋ฆฌ์˜ค(Portfolio)๋ผ๋Š” ๊ฒƒ์€ ์ „๋žต์  ์‚ฌ์—… ๋ชฉํ‘œ๋ฅผ ๋‹ฌ์„ฑํ•˜๋Š”๋ฐ ์œ ๋ฆฌํ•˜๋„๋ก ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด ์ž‘์—…์˜ ํšจ์œจ์  ๊ด€๋ฆฌ๋ฅผ ์ด‰์ง„ํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ์ ํŠธ(Projects), ํ”„๋กœ๊ทธ๋žจ(Programs), ๊ธฐํƒ€ ์ž‘์—…์˜ ์ง‘ํ•ฉ์ฒด๋ผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํฌํŠธํด๋ฆฌ์˜ค ๋‚ด์˜ ์‚ฌ์—…๋“ค์€ ์„œ๋กœ ์ข…์† ๊ด€๊ณ„์— ์žˆ๊ฑฐ๋‚˜ ์ง์ ‘ ์—ฐ๊ด€๋  ํ•„์š”๋Š” ์—†์œผ๋ฉฐ, ํฌํŠธํด๋ฆฌ์˜ค ๊ด€๋ฆฌ(Portfolio Management)๋ผ๋Š” ๊ฒƒ์€ ํŠน์ •ํ•œ ์ „๋žต ์‚ฌ์—… ๋ชฉํ‘œ๋ฅผ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์ „๋žต์  ์ค‘์š”๋„๋‚˜ ์žฌ๋ฌด์  ์ค‘์š”๋„์— ๋”ฐ๋ผ ์‚ฌ์—…๋ณ„ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์‹๋ณ„ํ•˜๊ณ  ๊ฒฐ์ •ํ•˜์—ฌ ์ด๋ฅผ ๊ด€๋ฆฌ ๋ฐ ํ†ต์ œํ•˜๋Š” ๊ธฐ๋ฒ•์„ ์˜๋ฏธํ•œ๋‹ค. ๊ฒฝ์˜ ์ง„๋‹จ์—๋„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์‚ฌ์—… ๋‹จ์œ„๋ณ„ ์ „๋žต์  ์ ํ•ฉ์„ฑ๊ณผ ์žฌ๋ฌด์  ์ ํ•ฉ์„ฑ์„ ๋ถ„์„ํ•˜๊ณ  ์ข…ํ•ฉํ•˜์—ฌ ํ‰๊ฐ€ํ•œ๋‹ค. ์ด๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณด๋ฉด Figure IV-17๊ณผ ๊ฐ™๋‹ค. Figure IV-17. ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ๋ถ„์„์˜ ๊ฐœ๋… ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค๋Š” ์ „๋žต์  ์ ํ•ฉ๋„์™€ ์žฌ๋ฌด์  ์ ํ•ฉ๋„์˜ ๊ฑด์ „์„ฑ์„ ๊ธฐ์ค€์œผ๋กœ 4๋ถ„ ๋ฉด์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ํˆฌ์ž๋ฅผ ์ฆ๋Œ€ํ•˜๊ณ  ์‚ฌ์—… ์„ฑ์žฅ์„ ๊ฐ€์†ํ™” ์‹œ์ผœ์•ผ ํ•  ์˜์—ญ๊ณผ ์‚ฌ์—…์„ ์ถ•์†Œํ•ด์•ผ ํ•  ์˜์—ญ, ์ˆ˜์ต์„ฑ์„ ๊ฐœ์„ ํ•ด์•ผ ํ•  ์˜์—ญ, ์ž ์žฌ ๊ธฐํšŒ๋ฅผ ๋ฐœ๊ตดํ•ด์•ผ ํ•  ์˜์—ญ ๋“ฑ์œผ๋กœ ๊ตฌ๋ถ„๋  ์ˆ˜ ์žˆ๋‹ค. ์ „๋žต์  ์ ํ•ฉ๋„๋„ ๊ฒฝ์Ÿ ์ž…์ง€์™€ ์‹œ์žฅ ๋งค๋ ฅ๋„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ „๋žต ์ ํ•ฉ์„ฑ์„ ๊ตฌ๋ถ„ํ•ด ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์žฌ๋ฌด์  ์ ํ•ฉ๋„๋„ ๋งค์ถœ ๊ทœ๋ชจ๋‚˜ ์ˆ˜์ต์„ฑ์„ ๊ธฐ์ค€์œผ๋กœ ์ž์›(resources)์„ ์ง€์†ํ•ด์„œ ํˆฌ์ž…ํ•ด์•ผ ํ•  ๊ณณ๊ณผ ์ฒ ์ˆ˜ํ•ด์•ผ ํ•  ๊ณณ์œผ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค.[1] ์ „๋žต ์ ํ•ฉ๋„ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ์กฐ๊ธˆ ์ƒ์„ธํ™” ์‹œ์ผœ๋ณด๋ฉด Figure IV-18๊ณผ ๊ฐ™๋‹ค. Figure IV-18. ์ „๋žต ์ ํ•ฉ์„ฑ ๋งคํŠธ๋ฆญ์Šค ์‹œ์žฅ์˜ ์„ฑ์žฅ์„ฑ๊ณผ ๊ฒฝ์Ÿ ๊ฐ•๋„[2]๋ฅผ ํ†ตํ•ด ์‚ฌ์—…์˜ ์ž ์žฌ์„ฑ(Business Potential)์„ ํŒŒ์•…ํ•˜๊ณ , ์‹œ์žฅ์ ์œ ์œจ ๋ถ„์„ ๋“ฑ์„ ํ†ตํ•ด ๊ฒฝ์Ÿ ์ž…์ง€๋ฅผ ํŒŒ์•…ํ•˜์—ฌ ์‹ค์งˆ์ ์œผ๋กœ ๋‹จ์œ„ ์‚ฌ์—…์ด ์–ด๋–ป๊ฒŒ ํฌ์ง€์…”๋‹ ๋˜๋Š”์ง€ ์•Œ์•„๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ์กฐ๊ธˆ ๋” ๊ตฌ์ฒด์ ์ธ ํ•ญ๋ชฉ๋“ค์„ ์‚ดํŽด๋ณด๋ฉด Figure IV-19์™€ ๊ฐ™๋‹ค. Figure IV-19. ์ „๋žต ์ ํ•ฉ์„ฑ ํ‰๊ฐ€ Logic Tree ์‚ฌ๋ก€ ์‹œ์žฅ ๋งค๋ ฅ๋„๋ฅผ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ๊ทœ๋ชจ, ์‚ฐ์—… ์ˆ˜์ต์„ฑ, ๊ตฌ์กฐ์  ๋งค๋ ฅ๋„ ๋“ฑ์„ ์ ๊ฒ€ํ•ด ๋ณด๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๊ณ  ์ž…์ง€์™€ ์—ญ๋Ÿ‰์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ์‚ฌ์—…์  ์ž…์ง€, ๊ฒฝ์Ÿ์‚ฌ์˜ ์—ญ๋Ÿ‰ ๋“ฑ์„ ํ‰๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. ์žฌ๋ฌด์  ์ ํ•ฉ๋„๋Š” ์‚ฌ์—…์˜ ์‹คํ˜„(Realization) ๋ฐ ์žฌ๋ฌด์  ์„ฑ๊ณผ(Performance) ์ธก๋ฉด์„ ํ‰๊ฐ€ํ•˜๋Š” ๊ด€์ ์œผ๋กœ ์‚ฌ์—… ๋‹จ์œ„๋ณ„ ์‹œ์žฅ ๋งค๋ ฅ๋„๋Š” ํˆฌ์ž ๋งค์ถœ ๊ทœ๋ชจ์™€ ์ˆ˜์ต์„ฑ์— ๋”ฐ๋ผ ์‚ฌ์—… ๋‹จ์œ„๋ณ„ ์žฌ๋ฌด์  ์ ํ•ฉ์„ฑ์„ ๋ถ„์„ํ•œ๋‹ค. ์žฌ๋ฌด ์ ํ•ฉ๋„ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ํ‘œํ˜„ํ•ด ๋ณด๋ฉด Figure IV-20๊ณผ ๊ฐ™๋‹ค. ์žฌ๋ฌด ์ ํ•ฉ์„ฑ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ๊ตฌ์„ฑํ•  ๋•Œ์—๋Š” ์žฌ๋ฌด์  ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ๊ณ„๋Ÿ‰ํ™”ํ•˜๋Š” ๋ถ„์„์ด ๋ณ‘ํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด CFROI[3] ๋“ฑ ์ˆ˜์ต์„ฑ ๋ถ„์„, ์ž๊ธˆ ์†Œ์š” ๊ณ„ํš, ํ˜„๊ธˆ ์œ ๋™์„ฑ ํ™•๋ณด/๊ณ„ํš/์‹คํ˜„ ๊ฐ€๋Šฅ์„ฑ ๋“ฑ์„ ์˜ต์…˜์œผ๋กœ ๋‘๊ณ  ๋ณ€๊ฒฝ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•œ ํ›„, ๋งคํŠธ๋ฆญ์Šค์— ๋ฐ˜์˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ €์ž๊ฐ€ CFROI์˜ ๊ฐœ๋…์„ ์ฒ˜์Œ ์ ‘ํ•œ ๊ฒƒ์ด 2004๋…„์ด์—ˆ๋‹ค. CFROI๋Š” ํ˜„๊ธˆํ๋ฆ„ ํˆฌ์ž์ˆ˜์ต๋ฅ , 'Cash Flow Return On Investment'์˜ ์•ฝ์ž๋กœ ๋‹น์‹œ ๊ฒฝ์˜ ์„ฑ๊ณผ๋ฅผ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•ด ๋„๋ฆฌ ์‚ฌ์šฉํ•˜๋˜ EVA(๊ฒฝ์ œ์  ๋ถ€๊ฐ€๊ฐ€์น˜. Economic Value Added)๋ณด๋‹ค ์ง„์ผ๋ณดํ•œ ์ข‹์€ ์ง€ํ‘œ๋ผ๊ณ  ์•Œ๋ ค์กŒ์—ˆ๋‹ค. ๊ทธ ๊ฐœ๋…์€ ๊ธฐ์—…๋“ค์ด ํˆฌ์ž ๋Œ€๋น„ ์–ผ๋งˆ๋‚˜ ํ˜„๊ธˆ ์ฐฝ์ถœ์„ ์ž˜ํ•˜๊ณ  ์žˆ๋Š”๊ฐ€๋ฅผ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ธ๋ฐ CFROI๊ฐ€ ๋†’์œผ๋ฉด ๊ธฐ์—…์ด ๋ถ€(ๅฏŒ)๋ฅผ ์ฐฝ์ถœํ•˜์—ฌ ์ฃผ์ฃผ๋“ค์—๊ฒŒ ๊ฐ€์น˜๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ํ•ด์™ธ์—์„œ๋Š” ์ฃผ์‹ํˆฌ์ž ํŠนํžˆ, ๊ฐ€์น˜ ํˆฌ์ž๋ฅผ ์œ„ํ•œ ๋ฐธ๋ฅ˜์—์ด์…˜(Valuation)์—์„œ ๋งŽ์ด ํ™œ์šฉํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ตญ๋‚ด์—์„œ๋Š” ์—ฌ์ „ํžˆ ์˜์—…์ด์ต/์˜์—…์ด์ต๋ฅ ์ด ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐ๋˜๊ณ  ์žˆ์œผ๋ฉฐ ์ฃผ์ฃผ๋ฐฐ๋‹น์„ ์œ„ํ•ด์„œ๋Š” ์˜์—…์ด์ต์—์„œ ๋ชจ๋“  ์„ธ๊ธˆ ๋ถ€๋ถ„์„ ์ œํ•œ ๋‹น๊ธฐ์ˆœ์ด์ต์ด ๊ฐ€์žฅ ์ค‘์š”ํ•˜๊ฒŒ ํ‰๊ฐ€๋˜๊ณ  ์žˆ๋‹ค. Figure IV-20. ์žฌ๋ฌด ์ ํ•ฉ์„ฑ ๋งคํŠธ๋ฆญ์Šค ์ง€๊ธˆ๊นŒ์ง€ ์‚ดํŽด๋ณธ ์ „๋žต ์ ํ•ฉ๋„ ๋งคํŠธ๋ฆญ์Šค์™€ ์žฌ๋ฌด ์ ํ•ฉ๋„ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋‹จ์œ„ ์‚ฌ์—…๋ณ„๋กœ ํ‰๊ฐ€(scoring)๋ฅผ ํ•ด์„œ ๊ทธ๊ฒƒ์„ ํ•˜๋‚˜์˜ ๋งคํŠธ๋ฆญ์Šค์— ํ‘œํ˜„ํ•˜๋ฉด ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. Figure IV-21์€ ๊ทธ ๊ณผ์ •์„ ๋„์‹ํ™”ํ•ด ๋ณธ ๊ฒƒ์ด๋‹ค. 5์  ์ฒ™๋„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ‰๊ฐ€ํ•ด๋„ ์ข‹๊ณ , ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ ๊ฒƒ์„ ์ฐจ๋ก€๋กœ ๋ฐ˜์˜ํ•˜๊ณ  ์ข…ํ•ฉํ•˜์—ฌ ํ•ฉ์‚ฐํ•˜๋ฉด ๋œ๋‹ค. Figure IV-21. ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ํ‰๊ฐ€์˜ ๊ฐœ๋… ํ‰๊ฐ€๋œ ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์€ Figure IV-22์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. Figure IV-22. ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ๊ด€๋ฆฌ์˜ ์ ˆ์ฐจ ์ „๋žต ์ ํ•ฉ์„ฑ์„ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜์—ฌ ๋ถ„์„ํ•˜๊ณ  ํ‰๊ฐ€ํ•œ ํ›„, ์ •ํ•ด์ง„ ๊ธฐ์ค€์— ๋”ฐ๋ผ ์ ์ˆ˜๋ฅผ ๋งค๊ฒจ์„œ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. ์ „๋žต์  ์˜์‚ฌ๊ฒฐ์ •์— ๋”ฐ๋ผ ์‚ฌ์—… ๋‹จ์œ„ ๋˜๋Š” ์„ธ๊ทธ๋จผํŠธ๋ฅผ ์กฐ์ •ํ•˜๊ณ  [4], ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ ์ž์›์„ ๋ฐฐ๋ถ„ํ•œ๋‹ค.[5] ๋˜ํ•œ, TableIV-5๋Š” ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ๊ด€๋ฆฌ์˜ ์žฅ๋‹จ์ ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ธ๋ฐ<NAME>์  ๊ด€์ ์˜ ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ์— ๊ธฐ์ดˆํ•˜๊ณ  ์žˆ์–ด Part III์—์„œ ์†Œ๊ฐœํ•œ BCG ๋งคํŠธ๋ฆญ์Šค ๋ถ„์„์˜ ํŠน์ง•์„ ๋งŽ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ฆ‰, ์‚ฌ์—…์˜ ๋™์ ์ธ ์ธก๋ฉด์„ ์„ค๋ช…ํ•˜๊ธฐ๋Š” ์–ด๋ ต๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋Ÿฐ ์œ ์˜ ์ฐจํŠธ๋ฅผ ์˜๋ฏธ ์žˆ๊ฒŒ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ์ดํ„ฐ์™€ ์—ฐ๊ณ„ํ•˜์—ฌ ์‹ค์‹œ๊ฐ„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์•ผ ํ•œ๋‹ค. Table IV-5. ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ๋ถ„์„์˜ ์žฅ๋‹จ์  Break #19. Three Horizons of Growth Figure IV-23. Three Horizons of Growth Figure IV-22์˜ 5๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ํฌํŠธํด๋ฆฌ์˜ค ๊ด€๋ฆฌ์ธ๋ฐ ๊ฒฐ๊ตญ ํฌํŠธํด๋ฆฌ์˜ค ๋ถ„์„์„ ํ†ตํ•ด ๊ทธ๊ฒƒ์„ ์žฌ๊ตฌ์„ฑํ•˜์˜€๋‹ค๋ฉด ์ž์› ํˆฌ์ž…๊ณผ ๋”๋ถˆ์–ด ์ง€์†์ ์œผ๋กœ ๊ทธ๊ฒƒ์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ์ด์™€ ๊ด€๋ จํ•˜์—ฌ ๋งฅํ‚จ์ง€ ์ปจ์„คํ„ดํŠธ์˜€๋˜ Mehrdad Baghai๋Š” 1999๋…„ ๊ทธ์˜ ์ €์„œ 'The Alchemy of Growth'์—์„œ Figure IV-23๊ณผ ๊ฐ™์€ ๊ฐœ๋…์„ ์†Œ๊ฐœํ•˜์˜€๋‹ค. ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ์ •๋น„ํ•˜๋ฉด์„œ ์‚ฌ์—… ๋‹จ์œ„์˜ ๊ตฌ๋ถ„์„ 3๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ๊ทธ์— ๋งž๊ฒŒ ์ „๋žต์  ์˜์‚ฌ๊ฒฐ์ •์„ ๋‚ด๋ ค์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ ๊ทธ ์„ธ ๊ฐ€์ง€๋Š” ์ฃผ๋ ฅ์‚ฌ์—…, ์Šน๋ถ€ ์‚ฌ์—…, ๋ฏธ๋ž˜์‚ฌ์—…์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค. ์ด๊ฒƒ์„ ์„ฑ์žฅ ํŒŒ์ดํ”„๋ผ์ธ(Growth Pipeline)์ด๋ผ๊ณ  ํ‘œํ˜„ํ–ˆ๋Š”๋ฐ ๊ทธ ๋ชฉ์ ์€ ์ฃผ๋ ฅ ์‚ฌ์—…์ด ์‡ ํ‡ดํ•  ๊ฒฝ์šฐ, ์ƒˆ๋กœ์šด ์„ฑ์žฅ์—”์ง„์ด ์ด๋ฅผ ์ ๊ธฐ์— ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ๊ท ํ˜•์„ ๋‹ฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ, ์ฃผ๋ ฅ ์‚ฌ์—…(Protecting the Core)์€ ๊ณ ๊ฐ๋“ค์ด ํ•ด๋‹น ๊ธฐ์—…์„ ์ƒ๊ฐํ•  ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ๋– ์˜ฌ๋ฆฌ๋Š” ์‚ฌ์—… ์ฆ‰, ๊ทธ ๊ธฐ์—…์˜ ํ•ต์‹ฌ ์‚ฌ์—…์„ ์˜๋ฏธํ•˜๋ฉฐ ์ˆ˜์ต์ด๋‚˜ ํ˜„๊ธˆํ๋ฆ„์˜ ์ƒ๋‹น๋ถ€๋ฌธ์„<NAME>๋‹ค. ๊ธฐ์—…์˜ ๋‹จ๊ธฐ ์„ฑ๊ณผ์— ์ง€๋Œ€ํ•œ ์˜ํ–ฅ์„ ๋ผ์น˜๋ฉฐ ์ž๊ธˆ์ด๋‚˜ ๊ธฐ์ˆ  ์ธก๋ฉด์—์„œ ์„ฑ์žฅ์˜ ์›๋™๋ ฅ์„ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์—…์ด๋‹ค. ์ด ์˜์—ญ์˜ ์‚ฌ์—…๋“ค์€ ๊ฒฝ์Ÿ๋ ฅ๊ณผ ์ž ์žฌ๋ ฅ์„ ๊ทน๋Œ€ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ƒํ’ˆ๊ณผ ์„œ๋น„์Šค๋ฅผ ๋‹ค์–‘ํ™”ํ•˜๊ณ  ์ƒˆ๋กœ์šด ๋งˆ์ผ€ํŒ… ์ „๋žต์„ ๊ฐœ๋ฐœํ•˜๊ณ  ํ•„์š”ํ•˜๋ฉด ์‚ฌ์—… ์ฒ ์ˆ˜ ๋ฐ ๋งค๊ฐ์„ ํ†ตํ•œ ๊ตฌ์กฐ์กฐ์ •๋„ ํ•„์š”ํ•˜๋ฉฐ ์ƒ์‚ฐ์„ฑ ํ–ฅ์ƒ์ด๋‚˜ ๋น„์šฉ ์ ˆ๊ฐ ๊ฐ™์€ ํ™”๋‘๋“ค์ด ๋™๋ฐ˜๋˜๋Š” ์˜์—ญ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ, ์Šน๋ถ€ ์‚ฌ์—…(Changing the Game)์€ ๊ฐœ๋…์ด ์ž๋ฆฌ๋ฅผ ์žก์•„ ๊ธ‰์†๋„๋กœ ์„ฑ์žฅํ•˜๋Š” ์‚ฌ์—…์œผ๋กœ ๋Œ€๊ทœ๋ชจ ํˆฌ์ž๋งŒ ๋’ท๋ฐ›์นจ๋˜๋ฉด ๊ธฐ์—… ์ „์ฒด๋ฅผ ๋ณ€๋ชจ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์œ ๋ง ์‚ฌ์—… ๊ตฐ์ด๋‹ค. ๊ธฐ์กด ํ•ต์‹ฌ์‚ฌ์—…๊ณผ ๊ด€๋ จ์ด ๊นŠ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๋น„๊ต์  ๋‹จ๊ธฐ๊ฐ„ ๋‚ด ๊ธฐ์—…์˜ ์ฃผ๋ ฅ์‚ฌ์—…์— ๋Œ€ํ•œ ๋ณด์™„์ ์ธ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋ฉฐ, ๊ถ๊ทน์ ์œผ๋กœ ์ฃผ๋ ฅ ์‚ฌ์—…์„ ๋Œ€์ฒดํ•ด์•ผ ํ•œ๋‹ค. ๋งค์ถœ๊ณผ ์‹œ์žฅ์ ์œ ์œจ์„ ๋™์‹œ์— ๋†’์ด๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋ฉฐ ์ง€์†์ ์ธ ํ™•์žฅ์„ ์œ„ํ•ด ์—ฐ์†๋œ ํˆฌ์ž๊ฐ€ ๋™๋ฐ˜๋˜์–ด์•ผ ํ•œ๋‹ค. ์„ธ ๋ฒˆ์งธ, ๋ฏธ๋ž˜ ์‚ฌ์—…(Creating the New)์€ ์žฅ๊ธฐ์ ์œผ๋กœ ์ถ”์ง„ํ•  ์‚ฌ์—…์˜ ์”จ์•—(seed)์œผ๋กœ ๋†’์€ ์ž ์žฌ๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ํ˜„์žฌ ํ™œ๋™์ด๋‚˜ ์ž๊ธˆ์ด ์†Œ๊ทœ๋ชจ ํˆฌ์ž๋˜๊ณ  ์žˆ๋Š” ์‚ฌ์—… ๋˜๋Š” ํ”„๋กœ์ ํŠธ๊ฐ€ ํ•ด๋‹น๋  ์ˆ˜ ์žˆ๋‹ค. ์žฅ๊ธฐ์ ์ธ ๊ด€์ ์—์„œ ์‹œ์žฅ์˜ ๋‹ค์–‘ํ•œ ์š”๊ตฌ์— ๋ถ€์‘ํ•˜์—ฌ ๋ฏธ๋ž˜์˜ ์„ฑ์žฅ์„ ์ฑ…์ž„์งˆ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์—… ๋˜๋Š” ์˜ต์…˜์ด ์ด์— ํ•ด๋‹น๋œ๋‹ค. ์ž์›์„ ๋งŽ์ด ํˆฌ์žํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ์˜ต์…˜์„ ๋งŽ์ด ๋ฐœ๊ตดํ•˜๋Š” ๊ฒƒ์ด ์ง€์ƒ๊ณผ์ œ๊ฐ€ ๋œ๋‹ค. ์ง€์†์„ฑ์žฅ(Sustainable Growth)์˜ ๊ด€์ ์—์„œ ๋ณด๋ฉด ๋‹น์—ฐํžˆ ์ด์ฒ˜๋Ÿผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ง€๊ธˆ ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ๋งŽ์€ ๊ธฐ์—…๋“ค์ด ์ด ๋ฌธ์ œ๋กœ ์‹ ์Œํ•˜๊ณ  ์žˆ๋‹ค. ์ด ๊ธ€์„ ์ฝ๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ๊ธฐ์—…์€ ์ด์™€ ๊ฐ™์€ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ ์šฉํ–ˆ์„ ๋•Œ ์ฃผ๋ ฅ์‚ฌ์—…, ์Šน๋ถ€ ์‚ฌ์—…, ๋ฏธ๋ž˜์‚ฌ์—…์ด๋ผ ๋ถ€๋ฅผ ๋งŒํ•œ ๊ฒƒ๋“ค์ด ์žˆ๋Š”๊ฐ€? [1] ์—ฌ๊ธฐ์„œ ์†Œ๊ฐœํ•œ ๊ธฐ์ค€์€ ์ €์ž์˜ ๊ฒฝํ—˜์— ๋”ฐ๋ฅธ ๊ฒƒ์ด๊ณ  ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ๋ถ„์„์˜ ๊ธฐ์ค€์€ ๊ธฐ์—…์˜ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์–ผ๋งˆ๋“ ์ง€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋‹ค. [2] ์ด๋Š” ์‚ฐ์—… ์ˆ˜์ต์„ฑ์œผ๋กœ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ๋Š”๋ฐ ์‚ฐ์—… ์ˆ˜์ต์„ฑ์ด ๋†’์œผ๋ฉด ๊ฒฝ์Ÿ ๊ฐ•๋„๊ฐ€ ๋‚ฎ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. [3] Cash Flow Return On Investment [4] ์‚ฌ์—… ๋‹จ์œ„์˜ ์กฐ์ • ๋ฐ ๋ฐฐ์น˜์™€ ๊ด€๋ จํ•ด์„œ๋Š” ๊ณ ์ „์ ์ธ ๋ถ„์„๋ฒ•์ธ BCG ๋งคํŠธ๋ฆญ์Šค์™€ Ansoff ๋งคํŠธ๋ฆญ์Šค๊ฐ€ ๋งŽ์ด ํ™œ์šฉ๋œ๋‹ค. ์•„๋ž˜ ๊ธ€ ์ฐธ์กฐ ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„ (2/4) BCG/GE ๋งคํŠธ๋ฆญ์Šค ๋ถ„์„, ๊ฒฝ์Ÿ ๋ถ„์„ | ๊ฒฝ์Ÿ ๋ฐ ์‚ฐ์—… ๋ถ„์„์˜ ์ฒซ ๋ฒˆ์งธ ์ˆœ์„œ๋กœ ๋ฉ”๊ฐ€ํŠธ๋ Œ๋“œ ๋ถ„์„์„ ์œ„ํ•œ PEST ๋ถ„์„๊ณผ ์‹œ์žฅ์˜ ํฌ๊ธฐ๋ฅผ ์‚ฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•, ์„ฑ์žฅ๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ• ๋“ฑ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. ์˜ค๋Š˜์€ ๊ฒฝ์Ÿ ๋ถ„์„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. 1.3 ๊ฒฝ์Ÿ ๋ถ„์„ ๊ฒฝ์Ÿ ๋ถ„์„์˜ ๊ธฐ๋ณธ์€ '๊ฒฝ์Ÿ์‚ฌ ํ”„๋กœํŒŒ์ผ๋ง(Competitors Profiling)'๊ณผ '๊ฒฝ์Ÿ์‚ฌ ํฌ์ง€์…”๋‹(Competitors Positioning)' brunch.co.kr/@flyingcity/54 [5] 'Three horizon of Growth'๋ฅผ ๋งŽ์ด ๋”ฐ๋ฅธ๋‹ค. 14. ํ”„๋กœ์„ธ์Šค ํ˜์‹  ๋ฐฉ๋ฒ•๋ก (1/2) ๊ฒฝ์˜์ „๋žต ์ˆ˜๋ฆฝ๊ณผ ๋”๋ถˆ์–ด ํ”„๋กœ์„ธ์Šค ํ˜์‹ (Process Innovation) ๋ถ€๋ถ„์€ ๊ฐ€์žฅ ์ธ๊ธฐ ์ข‹์€ ์ปจ์„คํŒ… ์˜์—ญ์ด๋‹ค. ์šฐ์„ , ํ”„๋กœ์„ธ์Šค ํ˜์‹ ์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•˜๊ธฐ ์ „์— ํ”„๋กœ์„ธ์Šค๋ž€ ๋ฌด์—‡์ธ์ง€, ํ˜์‹ ์ด๋ž€ ๋ฌด์—‡์ธ์ง€ ์ƒ๊ฐํ•ด ๋ณด๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ๋‹ค. ์ฒซ ๋ฒˆ์งธ, ํ”„๋กœ์„ธ์Šค์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•ด ๋ณด์ž. ํ”„๋กœ์„ธ์Šค์— ๋Œ€ํ•œ ์‚ฌ์ „์  ์ •์˜๋ฅผ ๋น„๋กฏํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์˜๊ฒฌ๋“ค์ด ์žˆ์ง€๋งŒ ์ €์ž ๊ฐœ์ธ์ ์œผ๋กœ ๋ˆˆ์— ๊ฐ€์žฅ ์™ ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์€ ์•„๋ฌด๋ž˜๋„ Table IV-6์ฒ˜๋Ÿผ 6-์‹œ๊ทธ๋งˆ[1] ๊ด€์ ์—์„œ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด๋ผ ์ƒ๊ฐ๋œ๋‹ค. ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ๋„ ์“ฐ์ด๋Š” SIPOC๋Š” ๊ฐ๊ฐ ๊ณต๊ธ‰์ž, ํˆฌ์ž…, ํ”„๋กœ์„ธ์Šค, ์‚ฐ์ถœ, ๊ณ ๊ฐ์˜ ์˜์–ด ๋‹จ์–ด ์•ž ๊ธ€์ž๋ฅผ ๋”ฐ์„œ ๋งŒ๋“  ๊ฐœ๋…์œผ๋กœ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ทœ๋ช…ํ•˜๊ธฐ์— ์œ ์šฉํ•œ ๊ฐœ๋…์ด๋‹ค. Table IV-6. 6-์‹œ๊ทธ๋งˆ์˜ SIPOC ํ”„๋กœ์„ธ์Šค๋Š” ์‚ฐ์—…์ด๋‚˜ ์—…์ข…์— ๋”ฐ๋ผ ๊ทธ ์ •์˜๋ฅผ ๋‹ฌ๋ฆฌํ•  ์ˆ˜ ์žˆ๊ณ  ๊ทธ ๊ตฌ๋ถ„๋„ ๋งค์šฐ ๋‹ค์–‘ํ•˜๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์ผœ์ง€๋Š” ์ •์˜๋Š” ์‹œ์ž‘๊ณผ ๋์ด ๋ช…ํ™•ํ•˜๊ณ  ๋…๋ฆฝ์ ์ด๋ฉฐ ์—…๋ฌด ํ”„๋กœ์„ธ์Šค์˜ ์˜๋ฏธ ์žˆ๋Š” ์‚ฐ์ถœ๋ฌผ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค. ๊ฒฝ์˜์ „๋žต ์ปจ์„คํŒ…์—์„œ ๊ฐ€์น˜ ์‚ฌ์Šฌ(Value Chain) ๋ถ„์„ ๋˜๋Š” ๊ณต๊ธ‰ ์‚ฌ์Šฌ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜์ง€๋งŒ ์‹ค์ œ ์—…๋ฌด์—์„œ๋Š” ๊ทธ๋ณด๋‹ค๋Š” ์—…๋ฌด-๊ธฐ๋Šฅ ๋ชจ๋ธ (Function-Process Model)์„ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. Figure IV-24๋Š” FP ๋ชจ๋ธ์„ ์˜ˆ์‹œ์ ์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด๋‹ค. Figure IV-24. ์—…๋ฌด๊ธฐ๋Šฅ ๋ชจ๋ธ ์‚ฌ๋ก€ FP ๋ชจ๋ธ์€ ๊ธฐ๋Šฅ(Function), ํ”„๋กœ์„ธ์Šค(Mega Process or Process), ํ•˜์œ„ ํ”„๋กœ์„ธ์Šค(Sub Process), ๊ธฐ๋ณธ ํ”„๋กœ์„ธ์Šค ๋˜๋Š” ๊ธฐ๋ณธ ํ™œ๋™(Elementary Process or Activity)๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ๊ธฐ๋Šฅ(Function)์€ ๊ธฐ์—…์˜ ๋ชฉ์  ๋‹ฌ์„ฑ์„ ์œ„ํ•ด ๊ด€๋ จ๋œ ์—…๋ฌด๋ฅผ ์œ ๊ธฐ์ ์œผ๋กœ ๋ฌถ์€ ๊ฒƒ์˜ ์ง‘ํ•ฉ์œผ๋กœ ์‹คํ–‰๋ ฅ์€ ์—†์œผ๋‚˜ Value Chain์˜ ๊ธฐ๋ณธ ๋‹จ์œ„๊ฐ€ ๋œ๋‹ค. ์˜ˆ: ์ƒ์‚ฐ๊ด€๋ฆฌ, ์—ฐ๊ตฌ๊ฐœ๋ฐœ, ์˜์—… ๋“ฑ ํ”„๋กœ์„ธ์Šค(Process)๋Š” ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์œ„๋กœ ์ •์˜๋œ ์—…๋ฌด ํ™œ๋™์„ ์˜๋ฏธํ•œ๋‹ค. ์ผ์˜ ์‹œ์ž‘๊ณผ ๋์ด ์žˆ์œผ๋ฉฐ ์‹ค์งˆ์ ์œผ๋กœ ์ผ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋ณต์ˆ˜๊ฐœ์˜ ํ•˜์œ„ ํ”„๋กœ์„ธ์Šค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ ์—ฌ๋Ÿฌ ๋ถ€์„œ๋ฅผ ๊ฑฐ์ณ์„œ ์ผ์–ด๋‚œ๋‹ค. ์˜ˆ: ๊ธฐ์ค€์ •๋ณด ๊ด€๋ฆฌ, ์กฐ์ง ๊ด€๋ฆฌ, ํ’ˆ์งˆ ๊ฒ€์‚ฌ ๋“ฑ ํ•˜์œ„ ํ”„๋กœ์„ธ์Šค(Sub Process)๋Š” ํ”„๋กœ์„ธ์Šค์˜ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ณต์ˆ˜๊ฐœ์˜ ๊ธฐ๋ณธ ํ”„๋กœ์„ธ์Šค ๋˜๋Š” ๊ธฐ๋ณธ ํ™œ๋™์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ๊ธฐ๋ณธ ํ”„๋กœ์„ธ์Šค ๋˜๋Š” ๊ธฐ๋ณธ ํ™œ๋™(Elementary process or Activities)์€ ์—…๋ฌด(business)์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š” ์ตœ์†Œ ๋‹จ์œ„ ํ™œ๋™์œผ๋กœ ํ•œ ์‚ฌ๋žŒ์ด ํ•œ ์žฅ์†Œ์—์„œ ์‹œ์ž‘๊ณผ ๋์„ ํ†ตํ•ด ์ผ์„ ์™„์ˆ˜ํ•œ๋‹ค๋Š” ๊ฐœ๋…์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ FP ๋ชจ๋ธ๋ง์˜ ๊ทผ๊ฐ„์ด์ž ๊ฐœ์„  ๋ชฉํ‘œ์˜ ๊ธฐ๋ณธ ๋‹จ์œ„๊ฐ€ ๋œ๋‹ค. FP ๋ชจ๋ธ์€ ์‚ฐ์—…์— ๋”ฐ๋ผ ์•ฝ๊ฐ„์”ฉ ์ฐจ์ด๊ฐ€ ๋‚˜์ง€๋งŒ ๊ณตํ†ต์ ์ธ ๋ถ€๋ถ„๋„ ๋งŽ๋‹ค. ํ”„๋กœ์„ธ์Šค ํ˜์‹ (PI)๋Š” FP ๋ชจ๋ธ ๋‚ด ๊ฐ€์น˜๊ฐ€ ์ „๋‹ฌ๋˜๋Š” ํ•ต์‹ฌ์ ์ธ ๊ณผ์ •(Critical Path)์˜ ํ˜์‹ ์„ ๋ชฉํ‘œ๋กœ ํ•˜๊ฒŒ ๋œ๋‹ค. ๋˜ํ•œ, ํ”„๋กœ์„ธ์Šค๋Š” ๊ตฌ๋ถ„์„ ์šฉ์ดํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๊ณ„์ธต(Hierarchy) ๊ตฌ์กฐ์— ๋”ฐ๋ผ ๊ตฌ์กฐํ™”ํ•˜๋ฉฐ, ๊ตฌ์กฐํ™” ์ดํ›„ ์ด๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜์ค€๋ณ„ ID (Identification)๋ฅผ ๋ถ€์—ฌํ•˜๋Š”๋ฐ ์ด๋Š” ํ–ฅํ›„ ๋ถ„์„์˜ ์ค‘์š”ํ•œ ๊ธฐ์ค€์ด ๋œ๋‹ค. ๊ตฌ์กฐํ™” ์ž‘์—…์ด ๋๋‚˜๋ฉด ํ”„๋กœ์„ธ์Šค ์ •์˜์„œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ํ”„๋กœ์„ธ์Šค ์ •์˜์„œ๋Š” Figure IV-25์™€ ๊ฐ™์ด ํ”„๋กœ์„ธ์Šค ๊ตฌ์„ฑ๋„, ํ”„๋กœ์„ธ์Šค ํ๋ฆ„๋„, ํ”„๋กœ์„ธ์Šค ์ƒ์„ธ ์„ค๋ช…์˜ 3๊ฐ€์ง€๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. Figure IV-25. ํ”„๋กœ์„ธ์Šค ์ •์˜์„œ์˜ ๊ตฌ์„ฑ - ๊ตฌ์„ฑ๋„, ํ๋ฆ„๋„, ์ƒ์„ธ ์„ค๋ช… ํ”„๋กœ์„ธ์Šค ๊ตฌ์„ฑ๋„๋Š” ์ „์ฒด ํ”„๋กœ์„ธ์Šค๋ฅผ ํ•œ๋ˆˆ์— ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€๋Šฅํ•œ ์ƒํ˜ธ ์—ฐ๊ฒฐ์„ ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค ํ”„๋กœ์„ธ์Šค ํ๋ฆ„๋„๋Š” ์ „์ฒด ํ”„๋กœ์„ธ์Šค๋ฅผ ํ•œ๋ˆˆ์— ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ•˜๋˜ ํ•˜์œ„ ํ”„๋กœ์„ธ์Šค ๊ฐ„์˜ ๊ด€๊ณ„, ์ˆ˜์ž‘์—… ๋ฐ ์‹œ์Šคํ…œ์„ ์ด์šฉํ•œ ์ž๋™ํ™” ์ผ ์ฒ˜๋ฆฌ, ์Šน์ธ ๋ฐ ๊ฒ€ํ†  ์—ฌ๋ถ€์— ๋”ฐ๋ฅธ ๋ถ„๊ธฐ, Trigger Point ๋“ฑ์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ํ‘œ๊ธฐํ•œ๋‹ค ํ”„๋กœ์„ธ์Šค๋ณ„ ์ƒ์„ธ ์„ค๋ช…์€ ๊ฐ ๊ธฐ๋ณธ ํ™œ๋™(Activity)์— ๋Œ€ํ•œ ์ƒ์„ธ ์ˆ˜ํ–‰ ๋ฐฉ๋ฒ•์ด๋‚˜ ์ง€์นจ, ์›์น™, ์ž…์ถœ๋ ฅ ๊ด€๊ณ„, ์ˆ˜ํ–‰ ์ฃผ์ฒด ๋“ฑ์„ ์ƒ์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•œ๋‹ค. ๋˜ํ•œ, ํ”„๋กœ์„ธ์Šค ๋ณ€ํ™” ๋‚ด์—ญ๊ณผ ์ด๋ฅผ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํ˜„์—…์˜ ์‹คํ–‰๊ณผ์ œ, ์ค€์ˆ˜ ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋Š” ์ธก์ •์ง€ํ‘œ ๋“ฑ์„ ๊ฐ™์ด ํ‘œ๊ธฐํ•˜์—ฌ ์ข…ํ•ฉ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. Figure IV-26. ํ”„๋กœ์„ธ์Šค ์ •์˜์„œ ์˜ˆ์‹œ(1) PI ์ปจ์„คํŒ…์„ ์œ„ํ•ด์„œ FP ๋ชจ๋ธ์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ผ๋ฐ˜์ ์ด๋ฉฐ ํ•„์ˆ˜์ ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์•„๋งˆ FP ๋ชจ๋ธ ์ž‘์—…์„ ํ•ด ๋ณธ ๋Œ€๋ถ€๋ถ„์˜ ์ปจ์„คํ„ดํŠธ๋“ค์€ FP ๋ชจ๋ธ์˜ ํšจ์šฉ์„ฑ์— ๋Œ€ํ•ด ์˜๋ฌธ์„ ์ œ๊ธฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์‚ฌ์‹ค FP ๋ชจ๋ธ์„ ์ ์šฉํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค ์ •์˜์„œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ผ์€ ์ƒ๋‹นํžˆ ๋งŽ์€ ๋…ธ๋ ฅ๊ณผ ์‹œ๊ฐ„์ด ํ•„์š”ํ•˜๋‹ค. PI๋ฅผ ์ œ๋Œ€๋กœ ํ•˜๊ธฐ ์œ„ํ•ด ์ƒ์„ธํ•˜๊ฒŒ ์—…๋ฌด ๋ชจ๋ธ์„ ๋ถ„์„ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ํฐ ์˜๋ฏธ ๋ถ€์—ฌ๋ฅผ ํ•˜๊ธฐ๋Š” ์–ด๋ ต๋‹ค. ์ฆ‰, ๋ฌด์–ธ๊ฐ€ ์žˆ์–ด ๋ณด์ด๊ธฐ๋Š” ํ•˜์ง€๋งŒ ๊ณ ๊ฐ๊ณผ ๋‹จ์ˆœํ•œ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์ด ๋ชฉ์ ์ด๋ผ๋ฉด ๋‹ค๋ฅธ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์„ ๊ณ ๋ฏผํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ๋‹ค. Figure IV-27. ํ”„๋กœ์„ธ์Šค ์ •์˜์„œ ์˜ˆ์‹œ(2) ํ”„๋กœ์„ธ์Šค์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๊ฐ€ ๊ธธ์—ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ, ํ˜์‹ (Innovation)์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์ž. ํ˜์‹ ์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ •์˜(definition)๊ฐ€ ์žˆ๋Š”๋ฐ ๊ทธ๊ฒƒ๋“ค ์ค‘ ๋ช‡ ๊ฐ€์ง€๋ฅผ ์†Œ๊ฐœํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. โ€œthe process of making improvements by introducing something newโ€ ---- Wikipedia โ€œthe act of introducing something new or something newly introducedโ€ ----- The American Heritage Dictionary โ€œthe introduction of something new; new idea, method, or device ---- Merriam Webster Online โ€œchange that creates new dimension of performanceโ€ ---- Peter Drucker ์œ„ ์ •์˜๋“ค๋กœ๋ถ€ํ„ฐ ๋„์ถœ๋œ ๊ณตํ†ต๋œ ๊ฐœ๋…์ด โ€˜changeโ€™ ๋˜๋Š” โ€˜introductionโ€™, ๊ทธ๋ฆฌ๊ณ  โ€˜newโ€™์ด๋‹ค. ์ฆ‰, ํ˜์‹ ์˜ ๋ณธ์งˆ์€ โ€˜์ƒˆ๋กœ์šด ๊ฒƒ์˜ ๋„์ž… ๋˜๋Š” ๊ทธ๊ฒƒ์œผ๋กœ ์ธํ•œ ๋ณ€ํ™”โ€™๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ํ˜์‹ ์€ Table IV-7๊ณผ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ์˜์—ญ์—์„œ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. Table IV-7. ๊ฒฝ์˜ํ˜์‹ ์˜ ๋‹ค์–‘ํ•œ ์ ์šฉ ๊ทธ๋Ÿฐ๋ฐ ๊ธฐ์—…๋“ค์€ ์™œ ํ˜์‹ (Innovation)์„ ์™œ ํ•˜๊ณ ์ž ํ• ๊นŒ? ์Š˜ํŽ˜ํ„ฐ(Joseph Schumpeter. 1883 ~ 1950)๋Š” ์ƒˆ๋กœ์šด ์žฌํ™”์˜ ๋„์ž…์„ ์œ„ํ•ด, ์ƒˆ๋กœ์šด ์ƒ์‚ฐ ๋ฐฉ์‹์˜ ์ ์šฉ์„ ์œ„ํ•ด, ์ƒˆ๋กœ์šด ์‹œ์žฅ์˜ ๊ฐœ์ฒ™ ๋“ฑ์„ ์œ„ํ•ด ํ˜์‹ ์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ํ•˜์˜€๋‹ค. ๊ถ๊ทน์ ์œผ๋กœ ์ด๋Š” ๋” ๋งŽ์€ ๊ฒฝ์ œ์  ๋ถ€(ๅฏŒ)์˜ ์ฐฝ์ถœ๊ณผ ์ง๊ฒฐ๋œ๋‹ค. ์ฆ‰, ๊ธฐ์—…์˜ ๊ฐ€์น˜ ์ฐฝ์ถœ๊ณผ ์ง๊ฒฐ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‚˜์•„๊ฐ€์„œ ์˜ค๋Š˜๋‚  ํ˜์‹ ์€ ๋‹จ์ˆœํžˆ ๊ฐ€์น˜ ์ฐฝ์ถœ๋งŒ์„ ์œ„ํ•จ์ด ์•„๋‹Œ ์ƒ์กด์„ ์œ„ํ•œ ํ˜์‹ ์˜ ์ฐจ์›๊นŒ์ง€ ์˜ฌ๋ผ๊ฐ€๊ธฐ๋„ ํ•œ๋‹ค. ๋ณ€ํ•˜์ง€ ๋ชปํ•˜๋ฉด ์‚ฌ๋ผ์ง€๋Š” ๊ฒƒ์ด ์ •๊ธ€์˜ ๋ฒ•์น™์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ฐ•ํ•œ ์ž๊ฐ€ ์‚ด์•„๋‚จ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์‚ด์•„๋‚จ๋Š” ๊ฒƒ์ด ๊ฐ•ํ•œ ์ž๋ผ๋Š” ๋ง์€ ํ˜์‹ ์˜ ๋™๊ธฐ๊ฐ€ ๋˜๊ธฐ์— ์ถฉ๋ถ„ํ•˜๋‹ค. [1] ๊ธฐ์—…์—์„œ ์ œํ’ˆ์˜ ์ „๋žต์  ์˜๋ฏธ์™€ ํ’ˆ์งˆ ์ˆ˜์ค€์„ ๊ทน๋Œ€ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋“ค์„ ์ •์˜ํ•˜๊ณ  ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์ˆ˜์ค€์„ ๊ณ„๋Ÿ‰ํ™”ํ•˜์—ฌ ํ‰๊ฐ€ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ 6-์‹œ๊ทธ๋งˆ๋ž€, ๋ณ€๋™์„ ํ‘œํ˜„ํ•˜๋Š” ํ‘œ์ค€ํŽธ์ฐจ๋กœ ์ œํ’ˆ 100๋งŒ ๊ฐœ๋‹น 0.002๊ฐœ ์ดํ•˜์˜ ๊ฒฐํ•จ ์ฆ‰, ๊ฑฐ์˜ ๋ฌด๊ฒฐ์  ์ˆ˜์ค€์˜ ํ’ˆ์งˆ ์ˆ˜์ค€์„ ์ง€์นญํ•˜๋Š” ์šฉ์–ด์ž„ 14. ํ”„๋กœ์„ธ์Šค ํ˜์‹  ๋ฐฉ๋ฒ•๋กœ(2/2) 14.1 ๋‹ค์–‘ํ•œ ๊ฒฝ์˜ํ˜์‹ ์˜ ๋ฐฉ๋ฒ• ๊ธ‰๋ณ€ํ•˜๋Š” ๊ฒฝ์ œ ๋ฐ ์‹œ์žฅ ํ™˜๊ฒฝ์˜ ๋ณ€ํ™”๋Š” ๊ฒฝ์˜ ํŒจ๋Ÿฌ๋‹ค์ž„์˜ ๋ณ€ํ™”๋ฅผ ์œ ๋ฐœํ•˜์˜€๋‹ค. ํŠนํžˆ, ๊ฒฝ์˜ ํŒจ๋Ÿฌ๋‹ค์ž„์˜ ๋ณ€ํ™”์— ๋”ฐ๋ผ ๊ธฐ์—…์˜ ๊ฒฝ์˜ํ˜์‹  ๊ธฐ๋ฒ•๋“ค๋„ ์ง€์†์ ์œผ๋กœ ๋ณ€ํ™” ๋ฐœ์ „ํ•˜์˜€๋Š”๋ฐ Figure IV-28์€ ๊ทธ ๋ณ€์ฒœ์„ ๋ณด์—ฌ์ค€๋‹ค. Figure IV-28. ๊ฒฝ์˜ ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ณ€ํ™”์— ๋”ฐ๋ฅธ ๊ฒฝ์˜ํ˜์‹  ํ™œ๋™์˜ ๋ณ€์ฒœ 1990๋…„๋Œ€ ์ดํ›„ ํŠนํžˆ ๊ฒฝ์˜ํ˜์‹  ๊ธฐ๋ฒ•๋“ค์ด ๋‹ค์–‘ํ™”๋˜๊ณ  ์žˆ๋Š”๋ฐ ์ œ์กฐ ์‚ฐ์—…์— ์†ํ•œ ๊ธฐ์—…๋“ค์„ ์ค‘์‹ฌ์œผ๋กœ ๊ธฐ์—… ๋‚˜๋ฆ„์˜ ๊ฒฝ์˜ํ˜์‹  ๊ธฐ๋ฒ•๋“ค์˜ ๊ตฌ์ถ•ํ•˜๊ณ  ๋งŒ์กฑํ•  ๋งŒํ•œ ์„ฑ๊ณผ๋ฅผ ์–ป์–ด ์ด๋ฅผ ์‚ฐ์—… ์ „๋ฐ˜์œผ๋กœ ์ „ํŒŒํ•˜์˜€๋‹ค. ์ผ๋ณธ ์ค‘์‹ฌ์˜ TPS[1], ๋ฏธ๊ตญ ์ค‘์‹ฌ์˜ 6 ์‹œ๊ทธ๋งˆ, TOC[2], BPR, PI ๋“ฑ์ด ๊ทธ๊ฒƒ์ธ๋ฐ Table IV-8์€ ๋„๋ฆฌ ์•Œ๋ ค์ง„ ๊ธฐ๋ฒ•๋“ค์˜ ํŠน์ง•์„ ๋ณด์—ฌ์ค€๋‹ค. Table IV-8. ์ฃผ์š” ๊ฒฝ์˜ํ˜์‹  ๊ธฐ๋ฒ•๋“ค์˜ ํŠน์ง•[3] ํŠนํžˆ, IT์˜ ๊ธ‰์†ํ•œ ๋ฐœ๋‹ฌ๊ณผ ํ•จ๊ป˜ ๋”์šฑ ๋ถ€๊ฐ๋˜๊ณ  ์žˆ๋Š” PI์— ๋Œ€ํ•ด ์ข€ ๋” ์•Œ์•„๋ณด์ž. PI ์ด๋ž€, ์‚ฌ์—…์„ ๋ณด๋‹ค ํšจ์œจ์ ์ด๊ณ  ์ˆ˜์ต์„ฑ ๋†’๊ฒŒ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ณ ๊ฐ์—๊ฒŒ ๋” ๋‚˜์€ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์ •๋ณด๊ธฐ์ˆ (IT)์„ ์ด์šฉํ•ด์„œ ํ”„๋กœ์„ธ์Šค๋ฅผ ์žฌ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Table IV-9๋Š” ํ”„๋กœ์„ธ์Šค ํ˜์‹ ๊ณผ ํ”„๋กœ์„ธ์Šค ๊ฐœ์„ ์„ ๋น„๊ตํ•ด ๋ณธ ๊ฒƒ์ด๋‹ค. Table IV-9. ํ”„๋กœ์„ธ์Šค ํ˜์‹ ๊ณผ ํ”„๋กœ์„ธ์Šค ๊ฐœ์„ ์˜ ๋น„๊ต ํ”„๋กœ์„ธ์Šค ํ˜์‹ ์ด ๊ฐ€์žฅ ํฐ ์„ฑ๊ณผ๋ฅผ ์–ป๊ธฐ ์‰ฌ์šด ๊ธฐ์—…์€ ์‚ฐ์—…์ด ์„ฑ์žฅ๊ธฐ๋‚˜ ์„ฑ์ˆ™๊ธฐ์— ์†ํ•ด ์žˆ๋Š” ๊ธฐ์—…๋“ค์ด๋‹ค. ์‚ฐ์—… ์ดˆ๊ธฐ๋Š” ์‚ฐ์—… ๊ณ ์œ ์˜ ํ”„๋กœ์„ธ์Šค๋ผ๋Š” ๊ฒƒ์ด ์ œ๋Œ€๋กœ ์ •๋ฆฝ๋˜์ง€ ์•Š์€ ์ƒํƒœ์ด๋ฉฐ, ์‚ฐ์—… ์„ฑ์žฅ๊ธฐ๋Š” ์›๊ฐ€๋‚˜ ๋น„์šฉ ์ ˆ๊ฐ ๋“ฑ ๊ฒฝ์Ÿ์ด ์‹ฌํ™”๋˜๋Š” ์ƒํ™ฉ์œผ๋กœ ๋‹ค์–‘ํ•œ ํ˜์‹ ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์ด ์š”๊ตฌ๋˜๋Š” ์‹œ๊ธฐ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋˜ํ•œ, ์‚ฐ์—… ์„ฑ์ˆ™๊ธฐ๋Š” ๊ธฐ์กด ๊ฒฝ์Ÿ์˜ ๊ธฐ๋ฐ˜ ์œ„์— ํ˜์‹ ํ•˜์ง€ ์•Š์œผ๋ฉด ์‡ ํ‡ดํ•  ์ˆ˜๋ฐ–์— ์—†๋‹ค๋Š” ์ ˆ๋ฐ•ํ•จ์ด ํŒฝ๋ฐฐํ•˜๊ฒŒ ๋˜์–ด ํ˜์‹ ์— ๋Œ€ํ•œ ๊ฐ•ํ•œ ๊ฐˆ๋ง์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฐ๊ฒฝ์  ์ดํ•ด๋ฅผ ๊ฐ€์ง€๊ณ  PI ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•ด ์ข€ ๋” ์‚ดํŽด๋ณด์ž. ๊ฒฝ์˜ํ˜์‹ ์ด ์„ฑ๊ณต์ ์ธ ์„ฑ๊ณผ๋ฅผ ๊ฑฐ๋‘๊ธฐ ์œ„ํ•ด์„œ๋Š” ํšŒ์‚ฌ์˜ ์ •์ฑ…์ด๋‚˜ ๊ด€๋ฆฌ ๊ตฌ์กฐ, ๊ฐ€์น˜ ์ฒด๊ณ„์˜ ๋ณ€ํ™”๊นŒ์ง€๋„ ํ•„์š”ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ทธ๋ ‡์ง€ ๋ชปํ•˜๊ณ  ํ”„๋กœ์„ธ์Šค์˜ ๊ฐœ์„ ์— ์ง‘์ค‘๋  ๊ฒฝ์šฐ, ๊ทธ ํšจ๊ณผ๊ฐ€ ํฌ์ง€ ์•Š๊ฑฐ๋‚˜ ์ง€์†๋˜๊ธฐ ์–ด๋ ค์šด ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฒฝ์˜ํ˜์‹  ํ”„๋กœ์ ํŠธ๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ์ถ”์ง„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ์ตœ๊ณ  ๊ฒฝ์˜์ž๋ฅผ ๋น„๋กฏํ•œ ๊ฒฝ์˜์ง„์˜ ์ง€์ง€๋ฅผ ์–ป์–ด์•ผ ํ•œ๋‹ค. 14.2 PI ๋ฐฉ๋ฒ•๋ก  PI๋Š” ERP[4] ์‹œ์Šคํ…œ ๊ตฌ์ถ•๊ณผ ๊นŠ์€ ๊ด€๊ณ„๊ฐ€ ์žˆ๊ณ  ๊ทธ์— ๋”ฐ๋ผ ์ถ”์ง„ ๋ฐฉ๋ฒ•๋„ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. SAP์™€ Oracle[5]๊ณผ ๊ฐ™์€ ์„ค๋ฃจ์…˜ ๊ธฐ์—…๋“ค์ด ์ œ๊ณตํ•˜๋Š” ์ƒ์šฉ ํŒจํ‚ค์ง€(Package)๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ธฐ์—…์˜ ์ „(ๅ…จ) ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฒ€ํ† ํ•˜๊ณ  ์ง์ ‘ ์ •๋ณด์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ•˜๋Š” ๋ฐฉ๋ฒ•(In-house ๊ฐœ๋ฐœ)์ด ์žˆ๋‹ค. Table IV-10์€ ๋‘ ๊ฐ€์ง€ ์ ‘๊ทผ๋ฒ•์„ ๋น„๊ตํ•˜๊ณ  ์žˆ๋‹ค. Table IV-10. PI ์ถ”์ง„ ์ ‘๊ทผ๋ฒ• ๋น„๊ต ์šฐ์„ , ํŒจํ‚ค์ง€ ์ค‘์‹ฌ์˜ ์ถ”์ง„ ๋ฐฉ๋ฒ•์€ Top-Down ๊ด€์ ์—์„œ ๊ธฐ์—…์˜ ๋น„์ „๊ณผ ์ „๋žต ๋ชฉํ‘œ๋ฅผ ํ™•์ธํ•œ ํ›„, ํ•ต์‹ฌ ์„ฑ๊ณต์š”์†Œ(CSF)์™€ KPI ๋ชฉํ‘œ๋ฅผ ์„ค์ •ํ•œ ํ›„, ํ”„๋กœ์„ธ์Šค ์ด์Šˆ๋ฅผ ๋„์ถœํ•˜๊ณ  ์ด๋ฅผ ๊ฐœ์„ ํ•˜๋Š” ๋ชจ๋ธ์„ ๊ตฌ์„ฑํ•˜๊ณ  ๊ทธ์— ๋งž๊ฒŒ ํŒจํ‚ค์ง€ ์‹œ์Šคํ…œ์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•(customizing) ํ•œ๋‹ค. ๋ฐ˜๋ฉด, ํ”„๋กœ์„ธ์Šค ์ค‘์‹ฌ์˜ ์ถ”์ง„ ๋ฐฉ๋ฒ•์€ Top-Down ๊ด€์ ์—์„œ ๊ธฐ์—…์˜ ๋น„์ „๊ณผ ์ „๋žต ๋ชฉํ‘œ ํ™•์ธํ•จ๊ณผ ๋™์‹œ์— Bottom-Up ๊ด€์ ์—์„œ ๊ฐ ์—…๋ฌด ์˜์—ญ์˜ KPI ๋ฐ ๋ชฉํ‘œ๋ฅผ ๋„์ถœํ•˜๊ณ  ๊ทธ ์ฐจ์ด(gap)๋ฅผ ๊ทน๋ณตํ•˜๋Š” ๋Œ€์•ˆ์„ ์ฐพ์•„ ํ”„๋กœ์„ธ์Šค์— ๋ฐ˜์˜ํ•˜๊ณ  ์ด๋ฅผ ์ •๋ณด์‹œ์Šคํ…œ์œผ๋กœ ์ง์ ‘ ๊ฐœ๋ฐœํ•œ๋‹ค.[6] 1980๋…„๋Œ€ ERP๊ฐ€ ์†Œ๊ฐœ๋˜๊ณ  PI/ERP ์‚ฌ์—…์ด ๊พธ์ค€ํžˆ ์ด์–ด์กŒ์œผ๋‚˜ ๊ตฌ์ถ• ์ดํ›„ ์šด์˜๊นŒ์ง€ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ํŒจํ‚ค์ง€ ์ ์šฉ์˜ ๊ฒฝ์šฐ, ๊ธ€๋กœ๋ฒŒ ํ‘œ์ค€์„ ๋„์ž…ํ•˜๊ณ  ์ ์šฉ, ์šด์šฉํ•œ๋‹ค๋Š” ์žฅ์ ์€ ์žˆ์œผ๋‚˜ ํŒจํ‚ค์ง€ ๋ผ์ด์„ ์Šค ๋น„์šฉ์ด ๊ณผ๋‹คํ•˜๋‹ค๋Š” ์ง€์ ์ด ์žˆ๊ณ , ํ”„๋กœ์„ธ์Šค ์ค‘์‹ฌ์˜ ์ง์ ‘ ๊ฐœ๋ฐœ์€ ๊ธฐ์—…์˜ ์ „ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ตœ์ ํ™”๋œ ์‹œ์Šคํ…œ์„ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ๊ธ€๋กœ๋ฒŒ ํ‘œ์ค€๊ณผ ๋ฉ€๊ณ  ์šด์˜ ๋ฐ ์œ ์ง€ ๋ณด์ˆ˜๊ฐ€ ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ตœ๊ทผ์—๋Š” ๋งŽ์€ ๊ธฐ์—…๋“ค์ด ๊ธ€๋กœ๋ฒŒ ํ‘œ์ค€ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ด๋ฏธ ๋„์ž…ํ•˜๊ณ  ์žˆ์–ด ๊ทธ์— ์ค€ํ•œ ๊ฐ’์‹ผ IT ๊ตฌ์ถ• ๋ฐ ์šด์˜์„ ๊ณ ๋ คํ•˜๊ณ  ์žˆ๊ณ , ํด๋ผ์šฐ๋“œ ๊ธฐ๋ฐ˜์˜ SaaS ์„œ๋น„์Šค๊ฐ€ ๋– ์˜ค๋ฅด๊ณ  ์žˆ์–ด ERP๋„ ์„œ๋น„์Šค ํ˜•ํƒœ๋กœ ๊ตฌ๋งคํ•˜๊ธฐ๋„ ํ•œ๋‹ค. 14.3 PI์™€ 6-์‹œ๊ทธ๋งˆ์˜ ๋น„๊ต PI์™€ ๋”๋ถˆ์–ด ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํ”„๋กœ์„ธ์Šค ํ˜์‹  ๋ฐฉ๋ฒ•์œผ๋กœ 6-์‹œ๊ทธ๋งˆ ๊ธฐ๋ฒ•์ด ์žˆ๋‹ค. 6์‹œ๊ทธ๋งˆ๋Š” Figure IV-29์—์„œ ๋ณด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ทธ ๋ชฉ์ ๊ณผ ๊ด€์ ์ด PI์™€๋Š” ์ข€ ๋‹ค๋ฅด๋‹ค. PI๊ฐ€ ์ผํšŒ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ธฐ์—…์˜ ๊ตฌ์กฐ์  ๊ฐœ์„  ๊ธฐ๋ฐ˜ ์กฐ์„ฑ์„ ๋ชฉ์ ํ•˜๋Š” ๊ฒƒ์ธ ๋ฐ˜๋ฉด, 6 ์‹œ๊ทธ๋งˆ๋Š” ํŠน์ • ํ”„๋กœ์„ธ์Šค์˜ ํ’ˆ์งˆ์„ ์ค‘์‹ฌ์œผ๋กœ ์ง€์†์ ์ธ ๊ฐœ์„ ์„ ์ถ”๊ตฌํ•œ๋‹ค. Figure IV-29. PI์™€ 6-์‹œ๊ทธ๋งˆ์˜ ๊ด€์  ๋น„๊ต Table IV-11์€ PI์™€ 6-์‹œ๊ทธ๋งˆ๋ฅผ ๋น„๊ตํ•œ ๊ฒƒ์ด๋‹ค. Table IV-11. PI์™€ 6-์‹œ๊ทธ๋งˆ์˜ ๋น„๊ต 6-์‹œ๊ทธ๋งˆ ๊ธฐ๋ฒ•์€ ๋ชจํ† ๋กค๋ผ์— ์˜ํ•ด ์‹œ์ž‘๋˜์–ด GE์— ์˜ํ•ด ๋„๋ฆฌ ์•Œ๋ ค์ง€๊ฒŒ ๋˜์—ˆ๊ณ  ์ œ์กฐ ์‚ฐ์—…์—์„œ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ํ”„๋กœ์„ธ์Šค ํ˜์‹ ์— ๋Œ€ํ•œ ํšจ์šฉ์„ฑ์„ ์ธ์ •๋ฐ›์•„ ์ „ ์‚ฐ์—…์œผ๋กœ ํ™•์žฅ๋˜์—ˆ๋‹ค.[7] ์ดํ›„ PI์™€ ๊ฐ™์ด ์—ฐ๊ณ„๋˜์–ด ์ค‘์žฅ๊ธฐ ๊ณผ์ œ๋Š” PI๋กœ, ๋‹จ๊ธฐ๊ณผ์ œ๋Š” 6-์‹œ๊ทธ๋งˆ๋กœ ๋‹ค์–‘ํ•œ ํ˜์‹ ๊ณผ์ œ๋“ค์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ๋„ ํ•˜์˜€๋‹ค. ์ตœ๊ทผ์—๋Š” PI์™€ 6-์‹œ๊ทธ๋งˆ์˜ ๋ถ์ด ๋งŽ์ด ์ˆ˜๊ทธ๋Ÿฌ์ง„ ํŽธ์ด๋‹ค. ํ˜์‹ ์˜ ํ”ผ๋กœ๋„๋ฅผ ํ˜ธ์†Œํ•˜๋Š” ์ด์œ ๋„ ์žˆ๊ณ  ๊ณผ๊ฑฐ์™€ ๋‹ฌ๋ฆฌ ๊ฐ ๊ธฐ์—…์˜ ์‚ฌ์ •์— ๋งž๋Š” ๋‹ค์–‘ํ•œ ํ˜์‹  ๊ธฐ๋ฒ•๋“ค์„ ๊ธฐ์—…๋“ค์ด ์ฐพ์•˜๊ธฐ ๋•Œ๋ฌธ์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ์ €์ž ๊ฐœ์ธ ์ƒ๊ฐ์—๋Š” ์กฐ๋งŒ๊ฐ„ PI ์˜์—ญ์ด ๋˜ ํ•œ ๋ฒˆ ๋œฐ ๊ฒƒ ๊ฐ™๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ œ4์ฐจ ์‚ฐ์—…ํ˜๋ช… ๋•Œ๋ฌธ์ด๋‹ค. ์ •ํ™•ํžˆ ๋งํ•˜๋ฉด ์˜จ ์‚ฌ๋ฐฉ ์ฒœ์ง€์— ๊น”๋ฆด IoT[8] ๊ธฐ๋ฐ˜ ๋•Œ๋ฌธ์ผ ๊ฒƒ์ด๋‹ค. ๋กœ๋ด‡์ด๋‚˜ ์ธ๊ณต์ง€๋Šฅ์˜ ๋„์ž…์œผ๋กœ ์ „ํ†ต์ ์ธ ๊ธฐ์—…์˜ ์ œ์กฐ๋‚˜ ์šด์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์™„์ „ํžˆ ๋ฐ”๋€Œ๊ณ  ์žˆ๋‹ค. ํฌ๊ณ  ์ž‘์€ ํ”„๋กœ์„ธ์Šค ๊ฐœ์„ /ํ˜์‹ ์˜ ๊ฑด์ด ๋งค์šฐ ๋งŽ์ด ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ปจ์„คํŒ… ๊ธฐ์—… ์ž…์žฅ์—์„œ ๊ทธ ๊ฑด๊ฑด์„ ์ œ๋Œ€๋กœ ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ์„์ง€ ์˜๋ฌธ์ด๋‹ค. ๊ทธ๊ฒƒ์€ ์ดํ•ด๋„์˜ ์ฐจ์ด๋„ ์žˆ์„ ๊ฒƒ์ด๊ณ  ์‚ฌ์—…๋น„์˜ ์ด์Šˆ๋„ ์žˆ์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฐ ๋ฐฐ๊ฒฝ์„ ์ฝ๊ณ  ์žˆ๊ธฐ์— ๊ธฐ์—…๋“ค์€ ๋” ์ด์ƒ ์ปจ์„คํ„ดํŠธ๋“ค์˜ ์†์„ ๋นŒ๋ฆฌ๊ธฐ๋ณด๋‹ค๋Š” ์ž์ฒด์ ์œผ๋กœ ๊ทธ๊ฒƒ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์„ ํ‚ค์šฐ๊ณ ์ž ํ•˜๋Š”์ง€ ๋ชจ๋ฅธ๋‹ค. ์–ด์จŒ๋“  ๊ฒฝ์˜ํ˜์‹ ์€ ๊ทธ ๋ฐฉ๋ฒ•์ด ๋ฌด์—‡์ด๋“  ๊ธฐ์—…์ด๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ•˜๋Š” ์ด์ƒ ์•ž์œผ๋กœ๋„ ์ง€์†์ ์ผ ์ˆ˜๋ฐ–์— ์—†๋Š” ์ปจ์„คํŒ… ํ…Œ๋งˆ์ด๋‹ค. ํ•œ๋งˆ๋””๋กœ ๋ˆ ๋˜๋Š” ์˜์—ญ์ด๋‹ค. [1] Toyota Production System [2] Theory of Constraints [3] Dave Nave, โ€œHow to compare lean, 6 sigma, and TOCโ€,Quality Progress, March 2002 [4] Enterprise Resource Planning ์ „์‚ฌ์  ์ž์›๊ด€๋ฆฌ [5] www.sap.com; www.oracle.com [6] ์ œ์กฐ ์‚ฐ์—…์ด ์•„๋‹ˆ๋ผ ๊ธˆ์œต์‚ฐ์—… ๋“ฑ ๊ธฐํƒ€ ํƒ€ ์‚ฐ์—…์˜ PI๋Š” ๊ฑฐ์˜ ํ›„์ž(In-house ๊ฐœ๋ฐœ)๋ฅผ ๋งŽ์ด ์ฑ„ํƒํ•˜์˜€์œผ๋‚˜ ์ƒ์šฉ ํŒจํ‚ค์ง€๋ฅผ ์ œ๊ณตํ•˜๋Š” ์„ค๋ฃจ์…˜ ๊ธฐ์—…๋“ค๋„ ์ œ์กฐ ์‚ฐ์—…๋ฟ ์•„๋‹ˆ๋ผ ํƒ€ ์‚ฐ์—…์˜ ์‚ฌ๋ก€๋ฅผ ๋งŽ์ด ํ™•๋ณดํ•˜๊ณ  ์žˆ๋Š” ์ค‘์ด๋‹ค. [7] ํ•œ๋•Œ ์ œ์กฐ์—… ๊ธฐ๋ฐ˜์˜ ํ˜์‹  ๊ธฐ๋ฒ•์ด ์„œ๋น„์Šค ์‚ฐ์—…์— ์ ํ•ฉํ• ๊นŒ ํ•˜๋Š” ๊ฒƒ์ด ํ™”๋‘์ธ ์ ์ด ์žˆ์—ˆ๋‹ค. ์ €์ž ์‚ฌ๊ฒฌ์œผ๋กœ๋Š” ์–ต์ง€๋กœ ์ ์šฉํ•  ํ•„์š”๋Š” ์—†์„ ๊ฒƒ ๊ฐ™๋‹ค. [8] Internet of Thing. ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท 15. ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•๋ก (1/2) ์ €์ž ์‚ฌ๊ฒฌ์ด์ง€๋งŒ ๊ฒฝ์˜์ „๋žต, ๊ฒฝ์˜ํ˜์‹ ๊ณผ ๋”๋ถˆ์–ด ๋น„์ฆˆ๋‹ˆ์Šค ์ปจ์„คํŒ…์˜ ์ตœ๊ณ  ์ธ๊ธฐ ํ…Œ๋งˆ๋Š” ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ(New Business Development: NBD)์ด๋‹ค. ํ”ํžˆ โ€˜NBDโ€™๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ ์ปจ์„คํŒ…์€ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„(Feasibility Study: F/S)๊ณผ ๊ณง์ž˜ ๋น„๊ต๋˜๋Š”๋ฐ ๊ทธ ๊ณผ์ •์ด ์ƒ๋‹นํžˆ ๋น„์Šทํ•˜๋ฉด์„œ๋„ NBD๊ฐ€ ์•„์ดํ…œ์„ ์ฐพ๋Š” ๊ฒƒ์— ์ง‘์ค‘๋œ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด F/S๋Š” ๋ฐœ๊ตด๋œ ์‹ ์‚ฌ์—… ์•„์ดํ…œ์ด ์‹ค์ œ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์„œ๋กœ ๊ทธ ๊ด€์ ์ด๋‚˜ ์ผ์˜ ์„ฑ๊ฒฉ์ด ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค. ๋˜ํ•œ, ์‹ ์ œํ’ˆ ๊ฐœ๋ฐœ์€ ํ”ํžˆ R&D ์ „๋žต๊ณผ ๊ฐ™์ด ์—ฐ๊ณ„ํ•˜์—ฌ ๊ณ ๋ฏผ๋˜๋Š” ๊ฒƒ์œผ๋กœ ํ˜‘์˜์˜ ์‚ฌ์—… ๊ฐœ๋ฐœ ๋˜๋Š” ์‚ฌ์—… ๊ฐœ๋ฐœ์˜ ํ•œ ๋ฒ”์ฃผ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋กœ ์ƒ๊ฐํ•ด ๋ณธ๋‹ค๋ฉด 1์ฐจ์ ์œผ๋กœ ์ฒด๊ณ„์ ์ธ ์ ‘๊ทผ์„ ํ†ตํ•ด ๊ณ ๊ฐ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์‹ ์‚ฌ์—…์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ์ผ์ด ์žˆ๊ณ , 2์ฐจ์ ์œผ๋กœ๋Š” ๊ทธ๋ ‡๊ฒŒ ๊ฐœ๋ฐœ๋œ ์‹ ์‚ฌ์—…์„ ์ปจ์„คํ„ดํŠธ ๋ณธ์ธ์ด<NAME>์—ฌ ์ง์ ‘ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ „์ž๋Š” ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ ์ปจ์„คํŒ…์ด ๋  ๊ฒƒ์ด์š” ํ›„์ž๋Š” ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ์ด ๋  ๊ฒƒ์ธ๋ฐ ๋ฌด์—‡์„ ํ•˜๋˜ ๋ณธ ์žฅ์˜ ๋‚ด์šฉ์€ ์œ ์ตํ•  ๊ฒƒ์ด๋‹ค. ์‹ ์‚ฌ์—…์„ ๋‹ค๋ฃจ๋ฉด์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ(Business Model)์— ๋Œ€ํ•œ ์ดํ•ด์ด๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์ด๋ผ๋Š” ์šฉ์–ด๋Š” 1990๋…„๋Œ€ ์ดˆ๋ฐ˜ e-๋น„์ฆˆ๋‹ˆ์Šค์˜ ์‚ฐ์‹ค์ด์—ˆ๋˜ ๋ฏธ๊ตญ ์‹ค๋ฆฌ์ฝ˜๋ฐธ๋ฆฌ์˜ ์ˆ˜๋งŽ์€ ๋ฒค์ฒ˜๊ธฐ์—…๋“ค(Venture)์ด ๋งŽ์ด ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋„๋ฆฌ ์•Œ๋ ค์กŒ๋Š”๋ฐ, ์ธํ„ฐ๋„ท์„ ๋งค๊ฐœ๋กœ ํ•˜๋Š” ๋‹ค์–‘ํ•œ ์•„์ด๋””์–ด๋ฅผ ๊ฐ€์ง€๊ณ  ์นœ๊ตฌ์™€ ๋™๋ฃŒ, ์„ ํ›„๋ฐฐ๋“ค์ด ๋‘˜, ์…‹ ๋ชจ์—ฌ ๋งŒ๋“  ๋ฒค์ฒ˜๊ธฐ์—…๋“ค์€ ๊ธฐ์กด ๊ธฐ์—…๋“ค์— ๋น„ํ•ด ํšŒ์‚ฌ๋ฅผ ์„ค๋ฆฝํ•˜๊ฑฐ๋‚˜ ์šด์˜ํ•  ๋•Œ ์†Œ์š”๋˜๋Š” ์ž๊ธˆ์ด ์ƒ๋Œ€์ ์œผ๋กœ ๋ถ€์กฑํ•˜์˜€๋‹ค. ๋”ฐ๋ผ์„œ ๋ฒค์ฒ˜๊ธฐ์—…๋“ค์€ ๊ทธ๋“ค์˜ ์‚ฌ์—…๋ฐฉ์‹์„ ์ฃผ์š” ์ž๊ธˆ ์ฃผ์ธ ์€ํ–‰์ด๋‚˜ ๋ฒค์ฒ˜ ์บํ”ผํ„ธ(Venture Capital)๋“ค์—๊ฒŒ ์ถฉ๋ถ„ํ•˜๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ์„ค๋ช…ํ•  ํ•„์š”๊ฐ€ ์žˆ์—ˆ๋‹ค. ์ด๋•Œ ๋„์ž…๋œ ๊ฐœ๋…์ด ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์ด๋‹ค. 1980๋…„ ๋Œ€๊นŒ์ง€๋งŒ ํ•˜์—ฌ๋„ ๊ธฐ์—…์˜ ๊ฒฝ์˜ํ˜„ํ™ฉ์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๋„๋ฆฌ ์“ฐ์ด๋˜ โ€˜์ „๋žต(Strategy)โ€™์ด๋ผ๋Š” ์šฉ์–ด๋Š” ์‚ฌ์—… ํ™˜๊ฒฝ๊ณผ ๊ฒฝ์Ÿ ๋ฐฉ์‹์„ ๋ถ„์„ํ•˜๋ฉด์„œ ์‚ฌ์—…์˜ ๋น„์ „ (Vision)์„ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์‹คํ–‰๊ณผ์ œ์™€ ๊ทธ ์ดํ–‰๋ฐฉ์‹์„ ํ‘œํ˜„ํ•˜์˜€๋‹ค๋ฉด, โ€˜๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ'์ด๋ผ๋Š” ์šฉ์–ด๋Š” ๊ณ ๊ฐ๊ณผ์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ•์กฐํ•˜๊ณ  ์–ด๋–ป๊ฒŒ ์ƒ๊ฑฐ๋ž˜๊ฐ€ ์‹คํ˜„๋˜๋ฉฐ ๊ทธ ๊ณผ์ •์—์„œ ์–ด๋–ป๊ฒŒ ์ˆ˜์ต์ด ์ฐฝ์ถœ๋˜๋Š”์ง€ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์— ๋ณด๋‹ค ์ง‘์ค‘ํ•˜์˜€๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์€ ํŠน๋ณ„ํ•œ ์›์น™ ์—†์ด ๊ทธ๋ฆผ์ด๋‚˜ ๊ธ€ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ์„ค๋ช…๋˜์—ˆ๊ณ  ์ด๋ฅผ ํ†ตํ•ด ์ž๋ณธ๊ฐ€๋“ค๊ณผ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ดํ›„ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์€ 2017๋…„ ํ˜„์žฌ ๊ธฐ์—… ํ™œ๋™์„ ์„ค๋ช…ํ•˜๋Š” ํ›Œ๋ฅญํ•œ ๋ฐฉ๋ฒ• ์ค‘์˜ ํ•˜๋‚˜๊ฐ€ ๋˜์–ด ์žˆ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์˜ ํ˜•ํƒœ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” B2B๋‚˜ B2C๋ผ๋Š” ์šฉ์–ด๋Š” ๊ฐ๊ฐ โ€˜Business-To-Businessโ€™, โ€˜Business-To-Consumerโ€™์˜ ์•ฝ์ž๋กœ์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์„ ์ „์ž์ƒ๊ฑฐ๋ž˜ ๋Œ€์ƒ์˜ ๊ด€์ ์—์„œ ์ด๋ถ„ํ™”ํ•˜์—ฌ B2B ์ „์ž์ƒ๊ฑฐ๋ž˜(B2B e-Commerce)๋Š” ๊ธฐ์—… ๊ณ ๊ฐ์„ ๋Œ€์ƒ์œผ๋กœ, B2C ์ „์ž์ƒ๊ฑฐ๋ž˜(B2C e-Commerce)๋Š” ์ผ๋ฐ˜ ์†Œ๋น„์ž๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ œํ’ˆ์„ ํŒ๋งคํ•˜๋Š” ์ƒํ™ฉ์„ ์ง€์นญํ•˜๋˜ ์šฉ์–ด์˜€๋Š”๋ฐ, ํ˜„์žฌ๋Š” ์ „์ž์ƒ๊ฑฐ๋ž˜๋ฟ ์•„๋‹ˆ๋ผ ๋ชจ๋“  ์‚ฐ์—… ๋ถ„์•ผ์—์„œ B2B๋Š” โ€˜๊ธฐ์—… ๊ฐ„ ๊ฑฐ๋ž˜โ€™, B2C๋Š” โ€˜๊ธฐ์—…-๊ฐœ์ธ ๊ฐ„ ๊ฑฐ๋ž˜โ€™๋ผ๋Š” ๊ฐœ๋…์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ™•์žฅ๋˜์–ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค. B2B๋Š” ๊ทธ ์˜๋ฏธ๊ฐ€ ์ข€ ๋” ํ™•์žฅ๋˜์–ด ๋Œ€์ƒ ๊ณ ๊ฐ์ด ๊ตญ๊ฐ€๋‚˜ ์ •๋ถ€๊ธฐ๊ด€, ๊ณต๊ธฐ์—…์ด ๋  ๊ฒฝ์šฐ, B2G(Business-To-Government), ๊ณ ๊ฐ์˜ ๊ณ ๊ฐ๊นŒ์ง€ ์ƒ๊ฐํ•  ๊ฒฝ์šฐ B2B2B, B2B2C, B2B2G ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ํŒŒ์ƒ์  ๊ฐœ๋…๋“ค๊นŒ์ง€ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋˜์—ˆ๋‹ค. Figure IV-30์€ ์žฌํ™”์˜ ํ๋ฆ„์œผ๋กœ ์‚ดํŽด๋ณธ B2B ๊ธฐ์—…๊ณผ B2C ๊ธฐ์—…์˜ ๊ด€๊ณ„์ด๋‹ค. Figure IV-30. ์žฌํ™”์˜ ํ๋ฆ„๊ณผ B2B, B2C์˜ ๊ฐœ๋… 15.1 ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์˜ ๊ฐœ๋… ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ˆ˜๋งŽ์€ ์ •์˜๊ฐ€ ์žˆ๊ณ  ์‚ฌ์—…๊ฐ€(Entrepreneur)๋“ค์˜ ๊ฒฝํ—˜์  ์ด์•ผ๊ธฐ๋“ค์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๊ฐœ๋…์„ ์ˆ˜ํ•™ ๋ฌธ์ œ์™€ ๊ฐ™์ด ๋”ฑ๋”ฑ ๋–จ์–ด์ง€๋Š” ๋‹ต์ฒ˜๋Ÿผ ์ด์•ผ๊ธฐํ•˜๋Š” ๊ฒƒ์ด ์˜ณ์ง€ ์•Š์„ ์ˆ˜ ์žˆ์ง€๋งŒ ์ œ15์žฅ์—์„œ๋Š” ์ €์ž๊ฐ€ ๋ณด์•„์™”๋˜ ๋‹ค์–‘ํ•œ ๊ฐœ๋… ์ค‘ ๊ฐ€์žฅ ๋งŽ์ด ๊ณต๊ฐํ•˜๋Š” ๊ฐœ๋…์„ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•œ๋‹ค. ๋ฏธ๊ตญ์˜ ๊ฒฝ์˜ํ•™์ž ํ”ผํ„ฐ ๋“œ๋Ÿฌ์ปค(Peter Drucker. 1909 ~ 2005)์™€ ์ „(ๅ‰) ๋ฒ ์ธ ์•ค์ปดํผ๋‹ˆ(Bain & Co.) ํŒŒํŠธ๋„ˆ์˜€๋˜ ์กฐ์•ˆ ๋งˆ๊ทธ ๋ ˆํƒ€(Joan Magretta)๊ฐ€ ์ฃผ์ฐฝํ•œ ๊ฒƒ์ด๋‹ค. ์ €์ž๋Š” ์ด๋“ค์˜ ์˜๊ฒฌ๋“ค์„ ์ข…ํ•ฉํ•ด์„œ Figure IV-31๊ณผ ๊ฐ™์ด ๋„์‹ํ™”ํ•ด๋ณด์•˜๋‹ค. Figure IV-31. ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์— ๋Œ€ํ•œ ๊ณ ์ฐฐ ์กฐ์•ˆ ๋งˆ๊ทธ ๋ ˆํƒ€๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‘ ๊ฐ€์ง€ ํ…Œ์ŠคํŠธ ์ฆ‰, โ€˜Narrative Testโ€™์™€ โ€˜Number Testโ€™๋ฅผ ํ†ต๊ณผํ•ด์•ผ ํ•œ๋‹ค๊ณ  ํ•˜์˜€๊ณ [1], ํ”ผํ„ฐ ๋“œ๋Ÿฌ์ปค๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์˜ ๋ณธ์งˆ์„ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ณ ๊ฐ๊ณผ ์ œํ’ˆ, ์ œ๊ณต ๋ฐฉ์‹์— ๋Œ€ํ•œ ์ž˜ ์•Œ์•„์•ผ ํ•œ๋‹ค๊ณ  ํ•˜์˜€๋‹ค. ์ฒซ ๋ฒˆ์งธ, โ€˜Narrative Testโ€™์ด๋ผ ํ•จ์€ ์Šคํ† ๋ฆฌํ…”๋ง(Story-telling)๊ณผ ํ†ตํ•˜๋Š” ํ‘œํ˜„์œผ๋กœ ํˆฌ์ž์ž๋“ค์—๊ฒŒ ๋‚ด ์‚ฌ์—…์˜ ์ด์•ผ๊ธฐ๋ฅผ ์žฌ๋ฏธ์žˆ๊ณ  ์ƒ์ƒํ•˜๊ฒŒ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ โ€˜์žฌ๋ฏธ์žˆ๋‹คโ€™๋Š” ๋ง์€ โ€˜์šฐ์Šค๊ฐฏ์†Œ๋ฆฌ๋ฅผ ํ•˜๋ผโ€™๋ผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ โ€˜๋ณด๋‹ค ์‰ฝ๊ณ  ํ˜„์‹ค๊ฐ ์žˆ๊ฒŒ ์ „๋‹ฌํ•˜์—ฌ ํˆฌ์ž์ž๋“ค๋กœ ํ•˜์—ฌ๊ธˆ ๋‚ด์šฉ์— ๋ชฐ์ž…๋˜๊ฒŒ ํ•ด์•ผ ํ•œ๋‹คโ€™๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.[2] ํ”ผํ„ฐ ๋“œ๋Ÿฌ์ปค์˜ ์˜๊ฒฌ[3]์„ ๊ณ๋“ค์ด๋ฉด ์–ด๋–ค ๊ณ ๊ฐ(WHO)์—๊ฒŒ ๊ณ ๊ฐ ๊ฐ€์น˜(Customer Value)์™€ ์ œํ’ˆ/์„œ๋น„์Šค(WHAT)๋ฅผ ์–ด๋–ป๊ฒŒ(HOW) ์ œ๊ณตํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ์•„์ฃผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ด์•ผ๊ธฐํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ๊ธฐ์—…๊ฐ€๊ฐ€ ๊ตฌ์ƒํ•˜๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์—์„œ ๊ณ ๊ฐ์€ ๋ˆ„๊ตฌ์ด๋ฉฐ ๊ธฐ์—…์˜ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋Š” ๋ฌด์—‡์ด๋ฉฐ, ์–ด๋–ค ์ฒด๊ณ„(์ƒ์‚ฐ๋ฐฉ์‹, ๋ฌผ๋ฅ˜๋ฐฉ์‹, ํŒ๋งค ๋ฐฉ์‹ ๋“ฑ)๋ฅผ ํ†ตํ•ด ์˜คํผ๋ง(Offering)์„ ๊ณ ๊ฐ์—๊ฒŒ ์ œ๊ณตํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ๊ฐ„๋‹จ ๋ช…๋ฃŒํ•˜๊ฒŒ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, โ€˜Number Testโ€™๋Š” ์žฌ๋ฌด์„ฑ๊ณผ์— ๋Œ€ํ•œ ๊ฒƒ ์ฆ‰, ๋ˆ์˜ ํ๋ฆ„์„ ํ†ตํ•ด ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ํ”Œ๋Ÿฌ์Šค์ธ๊ฐ€ ๋งˆ์ด๋„ˆ์Šค์ธ๊ฐ€์— ๋Œ€ํ•œ ๊ฒƒ์ด๋‹ค. Narrative Test๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์–ธ๊ธ‰๋˜๋Š” ๋‹ค์–‘ํ•œ ํ™œ๋™์— ํˆฌ์ž…๋œ ๋ˆ์€ ์–ผ๋งˆ์ด๋ฉฐ ๊ฒฐ๋ก ์ ์œผ๋กœ ์ด ๋น„์ฆˆ๋‹ˆ์Šค๋ฅผ ํ†ตํ•ด ๋ˆ์€ ์–ผ๋งˆ๋ฅผ ๋ฒŒ ์ˆ˜ ์žˆ๋Š”์ง€ ์ฆ‰, ํ˜„๊ธˆ ํ๋ฆ„(Cash flow)์„ ๋ช…๋ฃŒํ•˜๊ฒŒ ๋ณด์—ฌ์คŒ์œผ๋กœ์จ ํˆฌ์ž์ž๊ฐ€ ์ด ์‚ฌ์—…์— ๋Œ€ํ•œ ํˆฌ์ž๋ฅผ ํ™•์‹ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์•ผ ํ•œ๋‹ค. ๋ฌผ๋ก  ์ถ”์ฒญ ์žฌ๋ฌด์ œํ‘œ(Pro Forma)์˜<NAME>์„ ๋„๊ฒŒ ๋  ๊ฒƒ์ด๋ฏ€๋กœ ์‹ค์ œ ์ผ์ด ์ง„ํ–‰๋˜๋ฉด์„œ ๋ณ€๊ฒฝ๋  ์†Œ์ง€๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋‚˜ ํˆฌ์ž์ž๋Š” Narrative Test๋ฅผ ํ†ตํ•ด ์ถฉ๋ถ„ํžˆ ์‚ฌ์—…์— ๋Œ€ํ•ด ๋งค๋ ฅ์„ ๋Š๊ผˆ๊ณ  Number Test๋ฅผ ํ†ตํ•ด์„œ ๋ˆ์ด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค๋ฉด ๋‹น์—ฐํžˆ ํˆฌ์žํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ํŠนํžˆ, Number Test๋Š” ์ตœ์ข…์ ์ธ ๊ฐ€๊ฒฉ์ด ๋งŒ๋“ค์–ด์ ธ์„œ ์ˆ˜์ต์— ๋Œ€ํ•ด ๋…ผ์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜์ค€์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. ์œ„์˜ ๋‘ ๊ฐ€์ง€ ๊ด€์  ํ…Œ์ŠคํŠธ๋Š” ์ˆ˜๋งŽ์€ ์ „๋žต๊ธฐํš๊ฐ€, ์ปจ์„คํ„ดํŠธ์™€ ๊ธฐ์—…๊ฐ€๋“ค์— ์˜ํ•ด ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๊ณ  ๋˜ ์ •๋ฆฌ๋˜์—ˆ๋Š”๋ฐ, 2004๋…„ ์˜ค์Šคํ„ฐ์™ˆ๋”(1974 ~ present)์— ์˜ํ•ด โ€˜Narrative Testโ€™๋ฅผ ํ›Œ๋ฅญํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ์ธ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ์บ”๋ฒ„์Šค(Business Model Canvas. BMC)๊ฐ€ ์†Œ๊ฐœ๋˜๋ฉด์„œ ๋˜ ํ•œ ๋ฒˆ ์ „๊ธฐ๋ฅผ ๋งž์ดํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. Figure IV-32๋Š” BMC๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋Š”๋ฐ ์‚ฌ์šฉ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค. Figure IV-32. ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ์บ”๋ฒ„์Šค ํ…œํ”Œ๋ฆฟ Figure IV-32์™€ ๊ฐ™์€ ๊ทธ๋ฆผ์„ A4 ์šฉ์ง€ ๋˜๋Š” ๋” ํฐ ์ข…์ด์— ์ธ์‡„ํ•˜์—ฌ ๋ฒฝ์— ๋ถ™์ด๊ณ  ์‚ฌ์—…๋ชจ๋ธ์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ด์•ผ๊ธฐ๋“ค์„ ๋ธŒ๋ ˆ์ธ์Šคํ† ๋ฐ(Brainstorming)๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๊ฐ ์„น์…˜์— ๋ถ™์—ฌ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. 9๊ฐœ ์š”์†Œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์—ฌ '9-Block'์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š”๋ฐ ๊ทธ ํ•ญ๋ชฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. BMC๋ฅผ ์ฝ๋Š” ๋ฐฉ๋ฒ•์€ ์ฒซ ๋ฒˆ์งธ ๊ฐ€์น˜ ์ œ์•ˆ(Value Proposition)์„ ์ •์˜ํ•˜๋Š” ์ผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ๊ฐ€์น˜ ์ œ์•ˆ(Value Proposition)์€ ๊ถ๊ทน์ ์œผ๋กœ ๊ธฐ์—…๊ณผ ๊ณ ๊ฐ์—๊ฒŒ ์–ด๋–ค ๊ฐ€์น˜๋ฅผ ์ œ๊ณตํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค์˜ ๊ธฐ๋Šฅ์  ๊ฐ€์น˜ ์ด์™ธ์— ์‚ฌํšŒ์  ๊ฐ€์น˜๊นŒ์ง€ ํฌ๊ฒŒ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ฐ€์น˜ ์ œ์•ˆ ์ •์˜๊ฐ€ ๋๋‚˜๋ฉด BMC์—์„œ ๊ฐ€์น˜ ์ œ์•ˆ์˜ ์ขŒ์ธก์€ ๊ฐ€์น˜ ์ƒ์‚ฐ(Value Production)์„ ํ•˜๋Š” ์˜์—ญ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๊ธฐ์—…์˜ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๊ฐ€ ์ƒ์‚ฐ๋˜๋Š”์ง€๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ํ•ต์‹ฌ ์ž์›(Key Resources)์€ ๊ธฐ์—…์˜ ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ฌผ์  ์ž์›, ์ธ์ ์ž์› ๋“ฑ์„ ์ •์˜ํ•ด ๋ณธ๋‹ค. ํ•ต์‹ฌ ํŒŒํŠธ๋„ˆ(Key Partnerships)๋Š” ๊ธฐ์—…์˜ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์™ธ๋ถ€ ์ดํ•ด๊ด€๊ณ„์ž๋“ค์„ ์ ์–ด๋ณธ๋‹ค. ํ•ต์‹ฌ ํ™œ๋™(Key Activities)์€ ํ•ต์‹ฌ ์ž์›๊ณผ ํ•ต์‹ฌ ํŒŒํŠธ๋„ˆ๋ฅผ ํ†ตํ•ด ๊ฐ€์น˜ ์ œ์•ˆ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ํ™œ๋™์ด๋‹ค. ํŠน๋ณ„ํ•œ ์ œ์ž‘์ด๋‚˜ ์กฐ๋‹ฌ์ด ๋  ์ˆ˜๋„ ์žˆ๊ณ , ๊ณ ๊ฐ์„ ์ดํ•ดํ•˜๋Š” ๋ฐฉ์‹์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค. ๋น„์šฉ(Cost Structure)์€ ๊ฐ€์น˜ ์ƒ์‚ฐ์„ ์œ„ํ•ด ์†Œ์š”๋˜๋Š” ์žฌ๋ฌด์  ๋‚ด์—ญ์„ ๊ธฐ์ˆ ํ•ด ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ๋กœ์ง ํŠธ๋ฆฌ(Logic Tree)๋ฅผ ์ƒ๊ฐํ•ด์„œ ์ฃผ์š” ๋น„์šฉ ํ•ญ๋ชฉ(Cost Drivers)๋“ค์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค. ๊ฐ€์น˜ ์ƒ์‚ฐ ๋ถ€๋ถ„์ด ์ •์˜๋˜๋ฉด ์šฐ์ธก์˜ ๊ฐ€์น˜ ์ „๋‹ฌ(Value Delivery)์„ ์ƒ๊ฐํ•ด ๋ณธ๋‹ค. ๊ณ ๊ฐ(Customer Segments) ๋ถ€๋ถ„์€ ์–ด๋–ค ๊ณ ๊ฐ์„ ๋Œ€์ƒ์œผ๋กœ ํ•  ๊ฒƒ์ธ์ง€ ์ •๋ฆฌํ•ด ๋ณธ๋‹ค. ๊ทธ ๊ณ ๊ฐ์€ ๋Œ€๋ถ€๋ถ„ ๋ชฉํ‘œ ๊ณ ๊ฐ(Target Customer)์ด ๋œ๋‹ค. ์ฑ„๋„(Channels)์€ ์–ด๋–ค ๋ฐฉ์‹์„ ํ†ตํ•ด ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค๋ฅผ ๊ณ ๊ฐ์—๊ฒŒ ์ „๋‹ฌํ•  ๊ฒƒ์ธ์ง€ ๊ณ ๋ฏผํ•ด ๋ณธ๋‹ค. ๊ณ ๊ฐ ๊ด€๊ณ„(Customer Relationships)๋Š” ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค๋ฅผ ๊ณต๊ธ‰ํ•œ ๊ณ ๊ฐ๊ณผ ์–ด๋–ป๊ฒŒ ์ง€์†์ ์œผ๋กœ ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•๋“ค์„ ๊ณ ๋ฏผํ•ด ๋ณธ๋‹ค. ์ˆ˜์ต(Revenue Streams)์€ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ฐ€์น˜ ์ƒ์‚ฐ๋œ ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค๊ฐ€ ๊ณ ๊ฐ์—๊ฒŒ ๊ฐ€์น˜ ์ „๋‹ฌ๋œ ํ›„ ๊ณ ๊ฐ์ด ๊ธฐ์—…์—๊ฒŒ ๋Œ€๊ฐ€๋ฅผ ์ง€๋ถˆํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๋‚ด์šฉ์— ๋Œ€ํ•œ ๊ฒƒ์ด๋‹ค. BMC๋Š” ์ผํšŒ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค๋Š” ์‚ฌ์—…๋ชจ๋ธ์„ ์ง€์†์ ์œผ๋กœ ์ •๊ตํ™”ํ•˜๋ฉด์„œ 9 ๋ธ”๋ก์˜ ๋‚ด์šฉ์„ ๋ฐœ์ „์‹œํ‚ค๊ณ  ํ™•์ •ํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. [1] Joan Magretta, โ€˜Why Business Models Matter?โ€™, HBR, 2002 [2] ๋Œ€ํ™”์˜ ๊ธฐ์ˆ ์ด๋‚˜ ํ™”๋ฒ•๋„ ํšจ๊ณผ์ ์ธ ์ „๋‹ฌ์— ๋ถ„๋ช…ํžˆ ์˜ํ–ฅ์„ ๋ผ์น  ์ˆ˜ ์žˆ์œผ๋‚˜ ์ด ์žฅ์—์„œ ๊ทธ ํšจ๊ณผ๋Š” ๋ฐฐ์ œํ•œ๋‹ค [3] Peter Druckerโ€™s age-old questions, HBR, 1999 15. ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•๋ก (2/2) 15.2 ์‹ ์‚ฌ์—… ๋‹ˆ์ฆˆ(Needs)์˜ ๋ช…ํ™•ํ™” ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์˜ ๊ฐœ๋…์„ ์ดํ•ดํ–ˆ๋‹ค๋ฉด ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•ด ๋ณด์ž. ์ฒซ ๋ฒˆ์งธ ์ˆœ์„œ๋Š” ์‹ ์‚ฌ์—…์˜ ๋‹ˆ์ฆˆ(Needs)๋ฅผ ๋ช…ํ™•ํžˆ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, ์‹ ์‚ฌ์—…์˜ ๋ฐฉํ–ฅ์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ํ”„๋กœ์ ํŠธ์˜ ๋ชฉํ‘œ๋ฅผ ์„ค์ •ํ•˜๊ณ , ์–ด๋–ค ์ธ์›์œผ๋กœ ํŒ€์„ ๊ตฌ์„ฑํ•  ๊ฒƒ์ธ์ง€ ๋“ฑ์„ ๊ฒฐ์ •ํ•˜๋Š” ์ผ์ด๋‹ค. Figure IV-33์€ ์‹ ์‚ฌ์—… ๋‹ˆ์ฆˆ๋ฅผ ๋ช…ํ™•ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋กœ์ง ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•ด ๋ณธ ๊ฒƒ์ธ๋ฐ ์ด๋ฅผ ํ†ตํ•ด ์‚ฌ์—… ๋‹ˆ์ฆˆ๋ฅผ ๋ช…ํ™•ํžˆ ํ•˜๊ณ  ์‚ฌ์—… ๋ฐœ๊ตด์˜ ๋ฐฉํ–ฅ์„ ๊ฒฝ์˜์ง„๊ณผ ์ปจ์„ผ์„œ์Šค(concensus) ํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. Figure IV-33. ์‹ ์‚ฌ์—… ๋‹ˆ์ฆˆ์˜ ๋ช…ํ™•ํ™” ๋˜ํ•œ, ์‹ ์‚ฌ์—… ๋‹ˆ์ฆˆ๊ฐ€ ํ™•์ธ๋˜๋ฉด ์–ด๋–ค ๊ด€์ ์—์„œ ์‚ฌ์—… ์•„์ด๋””์–ด๋ฅผ ์„ ํƒํ•˜๊ณ  ์ง€์ง€ํ•  ๊ฒƒ์ธ์ง€ ๊ธฐ์ค€์ด๋‚˜ ๊ฐ€์ด๋“œ๋ผ์ธ์„ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด ๊ทœ์น™์— ์˜ํ•ด ์‹ ์‚ฌ์—… ์•„์ด๋””์–ด๋‚˜ ์•„์ดํ…œ๋“ค์€ ์ตœ์ข…์ ์œผ๋กœ ์„ ํƒ๋œ๋‹ค. Figure IV-34๋Š” ์‹ ์‚ฌ์—… ๊ฐ€์ด๋“œ๋ผ์ธ(guideline)์„ ์˜ˆ์‹œ์ ์œผ๋กœ ๊ตฌ์„ฑํ•ด ๋ณธ ๊ฒƒ์œผ๋กœ ๊ฐ€์ด๋“œ๋ผ์ธ ์„ค์ •์˜ ๊ด€์ ์„ ์‚ฌ์—…์  ๊ด€์ ์ด๋‚˜ ์žฌ๋ฌด์  ๊ด€์ ์—์„œ ์ •ํ•˜๊ณ  ๊ฒฝ์˜์ง„ ๋ฐ ๊ด€๋ จ์ž ์ธํ„ฐ๋ทฐ์™€ ์‹ค๋ฌด์ง„ ์›Œํฌ์ˆ์„ ํ†ตํ•ด ์ด๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ  ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ์˜ Ground Rule์„ ํ™•์ •ํ•œ๋‹ค. Figure IV-34. ์‹ ์‚ฌ์—… ๊ฐ€์ด๋“œ๋ผ์ธ์˜ ์ž‘์„ฑ ์‚ฌ๋ก€ ์‚ฌ์—…์˜ ์‹คํ˜„ ๊ฐ€๋Šฅ์„ฑ์˜ ์ธก๋ฉด์—์„œ ์ด๋Ÿฐ ์Šคํฌ๋ฆฌ๋‹(screening) ๊ธฐ์ค€์„ ์‚ฌ์ „์— ๊ฐ€์ ธ๊ฐ„๋‹ค๋Š” ๊ฒƒ์€ ๋ฐ”๋žŒ์งํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ํ•œํŽธ์œผ๋กœ๋Š” ๋งค์šฐ ์ฐฝ์˜๋กญ๊ณ  ์ž์œ ๋กœ์™€์•ผ ํ•  ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ์„ ๊ฐ€๋กœ๋ง‰๋Š” ๋ฒฝ์ด ๋˜๊ธฐ๋„ ํ•œ๋‹ค. ์ ์ ˆํ•œ ์‹œํ–‰์ฐฉ์˜ค(Trade-Off)๊ฐ€ ํ•„์š”ํ•˜์ง€๋งŒ ์‹œ๊ฐ„๊ณผ ์ž์›์ด ๋ถ€์กฑํ•œ ํ˜„์žฅ์—์„œ ์ด๋ฅผ ๋ฐธ๋Ÿฐ์Šค ํ•˜๊ธฐ๋Š” ์‰ฌ์šด ์ผ์ด ์•„๋‹ˆ๋ฉฐ, ์‹ค์ œ๋กœ ์‹ ์‚ฌ์—… ๊ฐœ๋ฐœ์„ ํ•˜๋ฉด์„œ ์ด๊ฒƒ ์•ˆ๋˜๊ณ  ์ €๋ž˜์„œ ์•ˆ๋˜๊ณ  ํ•˜๋Š” ์ด์œ ๋กœ ์ข‹์€ ์•„์ด๋””์–ด๊ฐ€ ๋ฒฝ์— ๋ถ€๋”ชํžˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งค์šฐ ๋งŽ๋‹ค. ๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ๊ทน๋ณตํ•˜๊ณ  ํ˜์‹ ํ•˜๋Š” ๊ธฐ์—…๋“ค์€ ๊ธฐํšŒ๋ฅผ ์žก๊ฒŒ ๋œ๋‹ค. ์ฆ‰, ์•„์ด๋””์–ด๊ฐ€ ์—†๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์ƒ๊ฐํ•˜๋Š” ์•„์ด๋””์–ด์ด์ง€๋งŒ ๊ทธ๊ฑธ '์‹คํ˜„์‹œํ‚ค๋Š” ์‹คํ–‰๋ ฅ'๊ณผ 'ํ•œ๊ณ„๋ฅผ ๊ทน๋ณตํ•˜๋Š” ํ˜์‹ '์ด ์‹ค์งˆ์ ์œผ๋กœ ์‹ ์‚ฌ์—…์„ ์„ฑ๊ณต์‹œํ‚จ๋‹ค. 15.3 ์‹ ์‚ฌ์—… ์•„์ด๋””์–ด์˜ ์„ ์ • - BMO ํ‰๊ฐ€ ์‹ ์‚ฌ์—… ๋‹ˆ์ฆˆ๋ฅผ ๋ช…ํ™•ํžˆ ์ •์˜ํ•˜๋ฉด ๋ณธ๊ฒฉ์ ์œผ๋กœ ์‚ฌ์—… ๊ฐœ๋ฐœ์— ๋“ค์–ด๊ฐ€์•ผ ํ•  ์ฐจ๋ก€์ด๋‹ค. ์‚ฌ์—… ์•„์ด๋””์–ด๋Š” Table IV-12์™€ ๊ฐ™์ด ์ฒ˜์Œ ๊ฐœ๋…ํ™”๋˜๊ณ  ์‹œ๊ฐ„์ด ์ง€๋‚จ์— ๋”ฐ๋ผ ์„ฑ์žฅํ•˜์—ฌ ์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค์— ๋ฐ˜์˜๋˜๊ณ  ๊ฒฐ๊ตญ ์ƒ๋ช…์„ ๋‹คํ•˜๊ฒŒ ๋œ๋‹ค. Table IV-12. ๊ธฐ์—…์˜ ์‹ ์‚ฌ์—… ๋ฐœ๊ตด ๊ณผ์ • ์‚ฌ์—… ์•„์ด๋””์–ด๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ ์ด ์žฅ์—์„œ๋Š” BMO ๊ธฐ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•œ๋‹ค. BMO๋Š” ๊ฐœ๋ฐœ์ž์ธ Bruce Merrifield์™€ Ohe์˜ ์ด๋ฆ„์„ ๋”ด ํ‰๊ฐ€ ๊ธฐ๋ฒ•์œผ๋กœ ์›๋ž˜๋Š” ๊ธฐ์ˆ ์˜ ์‚ฌ์—…ํ™”๋ฅผ ์œ„ํ•ด ์œ ๋ง ๊ธฐ์ˆ ์„ ํŒ๋ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ๋Š”๋ฐ ์ผ๋ณธ์˜ ๅคงๆฑŸ ๅปบ(Takeru Ohe) ๊ต์ˆ˜๊ฐ€ ์•„์ด๋””์–ด๋ฅผ ๋”ํ•ด ์‚ฌ์—… ๊ธฐํšŒ์˜ ์Šคํฌ๋ฆฌ๋‹ ๊ธฐ๋ฒ•์œผ๋กœ ๋ฐœ์ „ํ•˜์˜€๋‹ค. ๊ฐœ๋…์€ ๊ฐ„๋‹จํ•˜๋‹ค. BMO ํ‰๊ฐ€๋Š” ์‚ฌ์—… ์•„์ดํ…œ์„ 3๋‹จ๊ณ„๋กœ ํ‰๊ฐ€ํ•˜๋Š”๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1) ์‚ฌ์—…์ด ์–ผ๋งˆ๋‚˜ ๋งค๋ ฅ์ ์ธ๊ฐ€? 2) ์‚ฌ์—…์ด ์ž์‚ฌ์— ์–ผ๋งˆ๋‚˜ ์ ํ•ฉํ•œ๊ฐ€? 3) ์–ด๋Š ์ •๋„ ์„ฑ๊ณต ํ™•๋ฅ ์„ ๊ฐ€์ง€๋Š”๊ฐ€? ์ฒซ ๋ฒˆ์งธ, ํ‰๊ฐ€๋ฅผ ์œ„ํ•ด ๋งค๋ ฅ๋„ ๋ถ„์„์„ ํ•˜๋Š”๋ฐ BMO ํ‰๊ฐ€๋Š” ์‹œ์žฅ์˜ ๊ทœ๋ชจ(๋งค์ถœ ๋˜๋Š” ์ด์ต ๊ฐ€๋Šฅ์„ฑ), ์„ฑ์žฅ์„ฑ, ๊ฒฝ์Ÿ๋ ฅ, ๋ฆฌ์Šคํฌ ๋ถ„์‚ฐ๋„, ์—…๊ณ„ ์žฌ๊ตฌ์ถ• ๊ฐ€๋Šฅ์„ฑ, ์‚ฌํšŒ์  ์šฐ์œ„์„ฑ ๋“ฑ 6๊ฐ€์ง€ ๋งค๋ ฅ๋„ ์ง€ํ‘œ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ๊ฐ 10์ ์”ฉ 60์ ์œผ๋กœ ํ‰๊ฐ€ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ์ ํ•ฉ์„ฑ ๋ถ„์„์„ ์œ„ํ•ด ์ž๊ธˆ๋ ฅ, ๋งˆ์ผ€ํŒ…๋ ฅ, ์ œ์กฐ๋ ฅ, ๊ธฐ์ˆ ๋ ฅ, ์›์ž์žฌ ํ™•๋ณด ๋Šฅ๋ ฅ, ๊ฒฝ์˜ ์ง€์› ๋“ฑ ์ ํ•ฉ์„ฑ ์ง€ํ‘œ๋ฅผ ๊ฐ๊ฐ 10์ ์”ฉ 60์ ์œผ๋กœ ํ‰๊ฐ€ํ•œ๋‹ค. Table IV-13์€ BMO ๋งค๋ ฅ๋„ ํ‰๊ฐ€ ๊ธฐ์ค€์˜ ์‚ฌ๋ก€[1]์ด๋‹ค. Table IV-13. BMO ํ‰๊ฐ€ ๋งค๋ ฅ๋„ ์ง€ํ‘œ์˜ ์‚ฌ๋ก€ ๊ฐ ํ•ญ๋ชฉ๋‹น ๋ฐฐ์ ์„ ๊ณ ๋ คํ•˜์—ฌ 60์  ๋งŒ์  ๊ธฐ์ค€์œผ๋กœ ์ ์ˆ˜๋ฅผ ํ™˜์‚ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, Table IV-14๋Š” BMO ํ‰๊ฐ€์˜ ์ ํ•ฉ๋„ ํ‰๊ฐ€ ๊ธฐ์ค€์˜ ์‚ฌ๋ก€์ด๋‹ค. Table IV-14. BMO ํ‰๊ฐ€์˜ ์ ํ•ฉ๋„ ํ‰๊ฐ€ ์ง€ํ‘œ ์‚ฌ๋ก€ Table IV-13๊ณผ TableIV-14๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‚ฌ์—… ์•„์ด๋””์–ด์˜ ๋งค๋ ฅ ๋„์™€ ์ ํ•ฉ๋„๋ฅผ ๊ณ„์‚ฐํ•œ ๋‹ค์Œ, Figure IV-35์™€ ๊ฐ™์€ BMO ํ‰๊ฐ€ ๋งต์„ ์ž‘์„ฑํ•œ๋‹ค. Figure IV-35. BMO ํ‰๊ฐ€ ๊ฒฐ๊ณผ์™€ ์‚ฌ์—… ์ฐธ์—ฌ ์—ฌ๋ถ€ ๋งค๋ ฅ ๋„์™€ ์ ํ•ฉ๋„์˜ ์กฐํ•ฉ์— ์˜ํ•ด ์ ์ ˆํ•œ ๊ฐ’์ด ์‚ฐ์ถœ๋˜๋Š” ๊ฒฝ์šฐ์— ํ•œ ํ•ด ์‹ ์‚ฌ์—… ์ฐธ์—ฌ๋ฅผ ๊ฒฐ์ •ํ•˜๋ฉด ๋œ๋‹ค. ์ฐธ์—ฌ ๋ฐฉ๋ฒ•์€ ๋…์ž์ ์œผ๋กœ ์‚ฌ์—…์„ ์ „๊ฐœํ•˜๋˜์ง€ ์ œํœด(Strategic Alliance), ํ•ฉ๋ณ‘(Merge), ๋งค์ˆ˜ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ๋‹ค. BMO ํ‰๊ฐ€๋Š” ์ •์„ฑ์ ์ธ ํ‰๊ฐ€๋„ ๋งŽ์ด ๋“ค์–ด๊ฐ€์ง€๋งŒ ๋Œ€๋žต์ ์œผ๋กœ ์‹ ์‚ฌ์—…์˜ ์ „์ฒด ๋ชจ์Šต์„ ํŒŒ์•…ํ•˜๊ธฐ์— ์œ ์šฉํ•˜๋ฉฐ, ์žฌ๋ฌด๋ถ„์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ์•„๋ฌด๋ฆฌ ์ •ํ™•ํ•˜๊ฒŒ ํ•˜์—ฌ๋„ ๋ฏธ๋ž˜๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋‹จ ์‹œ์ž‘ํ• ์ง€ ํ•˜์ง€ ์•Š์„์ง€ ๋น ๋ฅด๊ฒŒ ์ƒ๊ฐํ•ด ๋ณด๋Š” ์ข‹์€ ๋„๊ตฌ์ด๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ๋Š” ๋งŒ์•ฝ์— ์‹คํ–‰ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์–ด๋Š ์ •๋„ ์‹คํ˜„ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š”์ง€ ์กฐ๊ธˆ ๋” ๊นŠ๊ฒŒ ์‚ดํŽด๋ณด๋Š” ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. [1] BMO ๋งค๋ ฅ๋„ ํ‰๊ฐ€์˜ ๊ด€๊ฑด์€ ์ •ํ™•ํ•œ ๊ฐ’์ด ์•„๋‹ˆ๋ผ ๊ธฐ์ค€์˜ ์–ด๋Š ๊ตฌ๊ฐ„์— ์†ํ•ด ์žˆ๋Š” ๋ƒ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. ๊ฐ™์ด ์ฝ์–ด๋ณด๋ฉด ์ข‹์€ ์ฑ…! 'The Business Model Ontology โ€“ A Proposition In A Design Science Approachโ€™, Alexander Osterwalder, 2004 16. ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„ ์‹ ์‚ฌ์—…์„ ๊ฐœ๋ฐœํ•˜๊ณ  ๋ก ์นญ(launching) ํ•˜๊ธฐ๊นŒ์ง€ ์‚ฌ์‹ค์€ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„(Feasibility Study: F/S)์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ด๋Ÿฐ์ €๋Ÿฐ '๋Œ ๋‘๋“œ๋ฆฌ๊ธฐ'๋ฅผ ํ•˜๋Š” ๊ธฐ์—…๋“ค์ด ๋งŽ๋‹ค. ์‚ฌ์—…์ด๋‚˜ ํˆฌ์ž์˜ ๊ทœ๋ชจ๊ฐ€ ํด์ˆ˜๋ก ๊ทธ๋Ÿฐ ๊ฒฝํ–ฅ์€ ๋‹ค๋ถ„ํ•œ๋ฐ ๋„๋กœ๋‚˜ ์ฒ ๋กœ, ๋Œ์ด๋‚˜ ๋‹ค๋ฆฌ ๊ฑด์„ค ๋“ฑ๊ณผ ๊ฐ™์€ ๋Œ€๊ทœ๋ชจ ํ† ๋ชฉ๊ณต์‚ฌ๋‚˜ ๊ฑด์„ค๊ณต์‚ฌ์˜ ๊ฒฝ์šฐ ์—”์ง€๋‹ˆ์–ด๋ง ์—…์ฒด[1]๊ฐ€ F/S๋ฅผ ๋งŽ์ด ํ•˜๊ฒŒ ๋œ๋‹ค. ๋‚ด์šฉ์€ ์„ค๊ณ„๋ฅผ ํฌํ•จํ•  ์ˆ˜๋„ ์žˆ๊ณ  ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ๋Š” Pre F/S(Preliminary F/S)๋ผ๊ณ  F/S ์ด์ „์— ๊ธฐ์ดˆ ์กฐ์‚ฌ ํ”„๋กœ์ ํŠธ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ํ•ด์™ธ ๊ฑด์„ค ์‚ฌ์—… ๋˜๋Š” ํ•ด์™ธ ๊ฑด์„ค ์‚ฌ์—…๊ณผ ๋™๋ฐ˜๋œ ํ•ด์™ธ IT ์‚ฌ์—…์ด ๊ทธ๋Ÿฐ ๊ฒฝํ–ฅ์ด ๋‹ค๋ถ„ํ•œ๋ฐ, Pre F/S ์ดํ›„์—๋„ ์‚ฌ์—…์ด ์ถ”์ง„๋˜๊ธฐ๊นŒ์ง€๋Š” F/S๋ฅผ ํ•˜๊ณ  ๋˜ ์ œ์•ˆ๊นŒ์ง€ 2~3๋…„ ๊ฑธ๋ฆฌ๋Š” ๋“ฑ ์ง„์ฒ™์ด ๋งค์šฐ ๋Š๋ฆฌ๋‹ค. ์–ด์จŒ๋“  ์ด F/S์˜ ๊ฐœ๋…[2]์€ ์ „ ์‚ฐ์—…์„ ํฌ๊ด„ํ•˜์—ฌ ์ ์ •์„ฑ ํ‰๊ฐ€๋ผ๊ณ  ํ•˜๋Š” ํ‹€๋กœ ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค. ๋•Œ๋•Œ๋กœ ๊ธฐ์ˆ ์ ์ธ(Technological) ๋ถ€๋ถ„์˜ ํ‰๊ฐ€๊ฐ€ ์žˆ๋Š”๋ฐ ๊ทธ ๋ถ€๋ถ„์ด ํ•ด๋‹น ์‚ฐ์—… ๋˜๋Š” ๋„๋ฉ”์ธ(Domain)์˜ ์˜ํ–ฅ์ด ๊ฐ€์žฅ ํฌ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ณ , ์žฌ๋ฌด์  ํ‰๊ฐ€ ์˜์—ญ์œผ๋กœ ๊ฐ€๋ฉด ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒฐ๊ตญ, ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์€ ๋ฐœ๊ตด๋œ ์‚ฌ์—… ๋˜๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๋ฏธ๋ž˜์— ์˜ˆ์ƒ๋˜๋Š” ๊ฒฝ์ œ ์ƒํ™ฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์‹œ์žฅ, ๊ธฐ์ˆ , ๊ฒฝ์ œ์„ฑ, ๊ณต์ต์„ฑ(๋˜๋Š” ์ •์ฑ…์„ฑ) ๋“ฑ์„ ๋ถ„์„ํ•˜๊ณ  ํ•ด๋‹น ์‚ฌ์—… ๋˜๋Š” ํ”„๋กœ์ ํŠธ์˜ ์„ฑ๊ณต ๊ฐ€๋Šฅ์„ฑ์„ ํ‰๊ฐ€ํ•˜๋Š” ์ด์ฒด์ ์ธ ๊ณผ์ •์ด๋ผ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„ํ‚คํ”ผ๋””์•„[3]์—์„œ๋Š” ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•˜๊ณ  ์žˆ๋‹ค. Feasibility Studies aim to objectively and rationally uncoverthe strengths and weakness of the existing business or proposed venture, opportunities, and threats as presented by environment, the resources requiredto carry throught and ultimately the prospects for success --- Wikipedia โ€˜ํ˜„์กดํ•˜๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ๋˜๋Š” ์ œ์•ˆํ•˜๋Š” ์‹ ์‚ฌ์—…์˜ ๊ฐ•์ ๊ณผ ์•ฝ์ , ์ œํ•œ๋œ ์œ ํ•œํ•œ ์ž์›๊ณผ ํ™˜๊ฒฝ, ์„ฑ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ๊ถ๊ทน์ ์ธ ๊ธฐํšŒ์™€ ์œ„๊ธฐ ๋“ฑ์— ๋Œ€ํ•œ ๊ณ ์ฐฐ์ด ํ•„์š”ํ•˜๋‹คโ€™๊ณ  ์–ธ๊ธ‰ํ•˜๊ณ  ์žˆ๋‹ค. ์ด์ฏค ๋˜๋ฉด ์‚ฌ์—… ์ „๋žต๊ณผ ์‚ฌ์—… ๊ณ„ํš, ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์€ ๋„๋Œ€์ฒด ์–ด๋–ป๊ฒŒ ์„œ๋กœ ๋‹ค๋ฅธ์ง€ ์˜์•„ํ•ดํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ฐ ์šฉ์–ด๋งˆ๋‹ค ๋™์ผํ•œ โ€˜์‚ฌ์—…โ€™์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด ๋” ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ๋Š”๋ฐ ์šฐ์„  โ€˜์‚ฌ์—… ์ „๋žต(business strategy)โ€™์ด๋ผ๋Š” ์šฉ์–ด๊ฐ€ ๋‚ดํฌํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ ์ค‘์— ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์˜๋ฏธ๋Š” ์‚ฌ์—…์˜ ๋น„์ „(vision)๊ณผ ๊ฐ€์น˜(value)์ด๋‹ค. โ€˜์‚ฌ์—… ๊ณ„ํš(business planning)โ€™์€ ์‚ฌ์—… ์ „๋žต์— ์‹ค์งˆ์ ์ธ ์ž์› ๋ฐฐ๋ถ„(Resources Allocation)์„ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, ์‚ฌ์—… ๊ณ„ํš์ด๋ž€ ์ˆ˜๋ฆฝ๋œ ์‚ฌ์—… ์ „๋žต์˜ ์‹คํ–‰์„ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ™œ๋™๋“ค์„ ๋ˆ„๊ฐ€, ์–ธ์ œ, ์–ด๋–ป๊ฒŒ ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ์ฒœ๋ช…ํ•œ ๊ฒƒ์ด๋‹ค. โ€˜์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„(feasibility study)โ€™์€ ์‚ฌ์—… ๊ณ„ํš ์ˆ˜์ค€์˜ ์ƒ์„ธํ•œ ์ž์› ๋ฐฐ๋ถ„์€ ํ•˜์ง€ ์•Š์ง€๋งŒ ํ•ด๋‹น ์‚ฌ์—…์„ ์ถ”์ง„ํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜๋ฆฝ๋œ ์ „๋žต์„ ์ˆ˜ํ–‰ํ•  ๊ฒฝ์šฐ, ํ•„์š”ํ•œ ์ด๋น„์šฉ๊ณผ ์ด์ˆ˜์ต์„ ์‚ฐ์ •ํ•˜์—ฌ ํ•ด๋‹น ์‚ฌ์—…์ด ๋ˆ์„ ๋ฒŒ ์ˆ˜ ์žˆ๋Š”๊ฐ€ ๋˜๋Š” ์‚ฌํšŒ์  ํŽธ์ต(Social Benefits)์ด ๋ฐœ์ƒํ•˜๋Š”๊ฐ€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ์ผ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์€ ํ•ด๋‹น ์‚ฌ์—…์„ ๋Œ€์ƒ์œผ๋กœ ์‚ฌ์—… ์ „๋žต์ด๋‚˜ ์‚ฌ์—… ๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•˜๋Š” ํ™œ๋™๋“ค์ด ์ƒ๋‹น ๋ถ€๋ถ„ ํฌํ•จ๋œ๋‹ค. ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„ ๋ณด๊ณ ์„œ๋Š” ๋‹ค์Œ ํ•ญ๋ชฉ๋“ค์— ๋Œ€ํ•ด ์„œ์ˆ ํ•œ๋‹ค.[4] ์‚ฌ์—… ๊ฐœ์š”(Business Introduction) ์ œํ’ˆ ๋ฐ ์„œ๋น„์Šค ๊ฐœ์š”(Product or Service) ๊ตฌํ˜„ ๊ธฐ์ˆ (Technology) ์‹œ์žฅ ํ™˜๊ฒฝ(Market Environment) ๊ฒฝ์Ÿ ํ™˜๊ฒฝ(Competition) ์‚ฐ์—… ๊ตฌ์กฐ ๋ฐ ํ˜„ํ™ฉ(Industry) ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ(Business Model) ๋งˆ์ผ€ํŒ… ๋ฐ ์˜์—… ์ „๋žต(Market and Sales Strategy) ์ƒ์‚ฐ์šด์˜ ์š”๊ตฌ์‚ฌํ•ญ(Production Operations Requirements) ๊ทœ์ œ ๋ฐ ํ™˜๊ฒฝ ์ด์Šˆ(Regulations and Environmental Issues) ์ฃผ์š” ์œ„ํ—˜ ์š”์†Œ๋“ค(Critical Risk Factors) ์žฌ๋ฌด ์˜ˆ์ธก(Financial Prediction) ๊ฒฐ๋ก (Conclusion) ๋˜ํ•œ, ๋ชจ๋“  ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„ ๊ฒฐ๊ณผ ๋ฌผ๋“ค์€ ์œ„์˜ ํ•ญ๋ชฉ๋“ค์„ ๊ฑฐ์˜ ํฌํ•จํ•˜์ง€๋งŒ ๊ฒ€ํ† ํ•˜๊ณ ์ž ํ•˜๋Š” ์‚ฌ์—…์ด ๋ฏผ๊ฐ„์‚ฌ์—…์ธ์ง€ ๊ณต๊ณต์‚ฌ์—…์ธ์ง€์— ๋”ฐ๋ผ ๊ทธ ์ˆ˜ํ–‰ ๊ทผ๊ฑฐ๋‚˜ ๊ฐ•์กฐํ•˜๋Š” ๋ถ€๋ถ„์ด ์ข€ ๋‹ค๋ฅด๋‹ค. ์ œ16์žฅ์—์„œ๋Š” ์ด๋Ÿฐ ์ ์„ ์ƒ๊ฐํ•˜๋ฉด์„œ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์— ๋Œ€ํ•ด์„œ ์ƒ์„ธํžˆ ์•Œ์•„๋ณด์ž. 16.1 ๋ฏผ๊ฐ„ ์˜์—ญ์˜ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„ ๋ฏผ๊ฐ„ ์˜์—ญ(Private Sector)์˜ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์€ Figure IV-36๊ณผ ๊ฐ™์€ ํ๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์ง„ํ–‰๋œ๋‹ค. Figure IV-36. ๋ฏผ๊ฐ„์˜์—ญ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์˜ ๊ธฐ๋ณธ ๊ฐœ๋… ์‚ฌ์—…์˜ ๋น„์ „๊ณผ ๊ฐ€์น˜์— ๋Œ€ํ•ด CEO๋ฅผ ๋น„๋กฏํ•œ ์‚ฌ์—… ์ดํ•ด๊ด€๊ณ„์ž๋“ค์ด ๊ณต๊ฐํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ณ , ์‹œ์žฅ ๋ถ„์„์„ ํ†ตํ•ด ์ˆ˜์š”๋ฅผ ์˜ˆ์ธกํ•˜๋ฉฐ ๊ธฐ์ˆ ํƒ€๋‹น์„ฑ ๋ถ„์„์„ ์œ„ํ•ด ํˆฌ์ž๋น„๋ฅผ ์˜ˆ์ธกํ•œ๋‹ค. ์ˆ˜์ต๊ณผ ๋น„์šฉ ๋ถ„์„์„ ํ†ตํ•ด ํ˜„๊ธˆ์˜ ์œ ์ถœ์ž…์„ ์‚ฐ์ •ํ•œ ํ›„, ๊ฒฝ์ œ์„ฑ ํ‰๊ฐ€๋ฅผ ํ†ตํ•ด ์ˆ˜์ต์„ฑ์ด ์žˆ๋‹ค๊ณ  ํŒ๋‹จ๋˜๋ฉด, ์ž๊ธˆ์กฐ๋‹ฌ ๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•˜๊ณ  ์‚ฌ์—…์„ ์ถ”์ง„ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ผ์ด ์ง„ํ–‰๋œ๋‹ค. ๋ฏผ๊ฐ„ ๊ธฐ์—…์˜ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์ด๋‹ค ๋ณด๋‹ˆ ์ด ๊ณผ์ •์—์„œ ๋ฒ•/์ œ๋„ ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ ๊ฒ€ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค์ด ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ํ•œ๊ตญ์˜ ๊ฒฝ์šฐ, ์„ธ๋ฒ•์ด๋‚˜ ๊ณต์ •๊ฑฐ๋ž˜๋ฒ•, IFRS์˜ ์˜ํ–ฅ ๋“ฑ์„ ์‚ดํŽด์•ผ ํ•˜๋Š”๋ฐ ์„ธ๋ฒ•์€ ํ•ด๋‹น ์‚ฌ์—…์ด ๋ฒ•์ธ์„ธ ๊ณต์ œ๋‚˜ ๊ฐ๋ฉด ํ˜œํƒ์ด ์žˆ๋Š”์ง€ ๊ฒ€ํ† ํ•ด์•ผ ํ•œ๋‹ค. ์ •๋ถ€์˜ ์ฃผ์š” ์ •์ฑ…์— ์˜ํ•ด ์ถ”์ง„๋˜๋Š” ์‚ฌ์—… ์•„์ดํ…œ์˜ ๊ฒฝ์šฐ, ๊ทธ๋Ÿฐ ํ˜œํƒ์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ๊ณต์ •๊ฑฐ๋ž˜๋ฒ•์˜ ๊ฒฝ์šฐ, ๋Œ€๊ธฐ์—… ๊ทธ๋ฃน์ด ์‹ ์‚ฌ์—…์„ ์ถ”์ง„ํ•  ๋•Œ ๋ฏผ๊ฐํ•˜๊ฒŒ ๊ฒ€ํ† ๋˜๋Š” ๋ถ€๋ถ„์ธ๋ฐ ์ฃผ์‹ ์ทจ๋“์„ ์ œํ•œํ•œ๋‹ค๋“ ์ง€, ์ง„์ž… ์ œํ•œ ์‚ฐ์—…์ด ์กด์žฌํ•œ๋‹ค๋“ ์ง€, ๊ณ„์—ด ํŽธ์ž…์‹ ๊ณ ์—์„œ ๋…์  ์—ฌ๋ถ€ ๋“ฑ์œผ๋กœ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, IFRS์™€ ๊ฐ™์€ ํšŒ๊ณ„ ์ฒ˜๋ฆฌ์˜ ๋ฌธ์ œ์—์„œ๋„ ์ˆ˜์ต์˜ ์ธ์‹ ๊ธฐ์ค€์ด๋‚˜ ๋ถ€์ฑ„์„ฑ ์ถฉ๋‹น๊ธˆ์˜ ์ธ์‹ ๋“ฑ์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ธฐ์—… ๊ฐ„์˜ ์‹ค์  ํ†ตํ•ฉ ์‹œ ์•…์˜ํ–ฅ์ด ์ƒ๊ธฐ์ง€ ์•Š๋„๋ก ๋ฉด๋ฐ€ํžˆ ๊ฒ€ํ† ํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ ์™ธ์—๋„ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์€ ํˆฌ์ž์— ๋Œ€ํ•œ ํ•ฉ๋ฆฌ์  ํŒ๋‹จ์„<NAME>๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๊ธˆ์œต์‹œ์žฅ์˜ ํ˜„ํ™ฉ์ด ๋Œ€๋‹จํžˆ ์ค‘์š”ํ•œ๋ฐ ํ™˜์œจ์ด๋‚˜ ๊ธˆ๋ฆฌ ๋ณ€๋™์„ ์ž˜ ์‚ดํŽด์•ผ ํ•œ๋‹ค. ํŠนํžˆ, ํ•ด์™ธํˆฌ์ž ์‚ฌ์—…์˜ ๊ฒฝ์šฐ, ํ•ด๋‹น ๊ตญ์˜ ๋ฒ•์ธ์„ธ๋ฒ•, ๋ถ€๊ฐ€์„ธ ๋ฒ•, ํ•ด์™ธ๋ฒ•์ธ์˜ ์†ก๊ธˆ์ œํ•œ ๋“ฑ๊ณผ ๊ด€๋ จํ•ด์„œ ๋Œ€์‘์ฑ…์„ ๋งˆ๋ จํ•ด์•ผ๋งŒ ํ˜„๊ธˆ์ด ์ •์ƒ์ ์œผ๋กœ ์œ ํ†ต๋  ์ˆ˜ ์žˆ๋‹ค. Figure IV-36๊ณผ ๊ฐ™์€ ํ๋ฆ„์˜ ์—…๋ฌด ์ง„ํ–‰์„ ํ†ตํ•ด ๊ฒฝ์ œ์„ฑ ํ‰๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ˆ˜์ต์„ฑ์ด ์žˆ๋‹ค๊ณ  ํŒ๋‹จ๋  ๊ฒฝ์šฐ, ์ž๊ธˆ์กฐ๋‹ฌ ๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•˜๊ฒŒ ๋œ๋‹ค. Figure IV-37. ์ž๊ธˆ์กฐ๋‹ฌ ๊ณ„ํš์˜ ๊ฐœ๋… ์‚ฌ์—…์„ ์ถ”์ง„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Figure IV-37๊ณผ ๊ฐ™์ด ์ธ๊ฑด๋น„, ์žฌ๋ฃŒ๋น„, ๋งˆ์ผ€ํŒ…, ๊ธฐํƒ€ ๋น„์šฉ ๋“ฑ ํˆฌ์ž๊ธˆ์ด ํ•„์š”ํ•˜๋‹ค. ์ด๋ฅผ ๊ธฐ์—… ๋‚ด ์ž๊ธฐ์ž๋ณธ๊ณผ ํƒ€์ธ์ž๋ณธ์„ ์œตํ†ตํ•˜์—ฌ ์‚ฌ์—… ์ž๊ธˆ์„ ๋งˆ๋ จํ•˜๋Š” ๊ฒƒ์ด ์ž๊ธˆ์กฐ๋‹ฌ ๊ณ„ํš์ธ๋ฐ ์–ด๋–ค ์‚ฌ์—…๊ฐ€๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๊ฒ ์ง€๋งŒ ์ž๊ธฐ์ž๋ณธ์€ ์ž‘๊ฒŒ, ํƒ€์ธ์ž๋ณธ์€ ์ €๋ ดํ•˜๊ฒŒ ๋นŒ๋ ค์™€์„œ ์‚ฌ์—…์„ ํ•˜๊ณ  ์‹ถ์–ด ํ•œ๋‹ค. ๊ณต๊ณต์‚ฌ์—…์˜ ๊ฒฝ์šฐ, ์ด๋Ÿฐ ๊ณ ๋ฏผ์€ ์ƒ๋Œ€์ ์œผ๋กœ ์ข€ ๋œํ•˜๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ณต๊ณต์‚ฌ์—…์€ ๊ณต๊ณต์˜ ํŽธ์ต(Benefit)์ด ๋ชฉ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์งˆ์ ์ธ ํŽธ์ต์ด ๋ฐœ์ƒ๋  ์ˆ˜ ์žˆ๋Š” ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ๊ณต๊ธ‰ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๊ณ  ํ•œํŽธ์œผ๋กœ๋Š” ๋ช…๋ถ„์ด ๋Œ€๋‹จํžˆ ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๋ช…๋ถ„์„ ์–ด๋–ป๊ฒŒ ์ž˜ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š๋ƒ๊ฐ€ ๋˜ํ•œ ์ปจ์„คํŒ… ํฌ์ธํŠธ๊ฐ€ ๋œ๋‹ค. 16.2 ๊ณต๊ณต ์˜์—ญ์˜ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„ ์ •๋ถ€๊ฐ€ ์ฃผ๊ด€ํ•˜๋Š” ๊ณต๊ณต ์˜์—ญ(Public Sector) ์‚ฌ์—…์€ ์˜ˆ์‚ฐ์˜ ํšจ์œจ์  ์ง‘ํ–‰์„ ์œ„ํ•ด ์˜ˆ๋น„ํƒ€๋‹น์„ฑ ์กฐ์‚ฌ์˜ ๋Œ€์ƒ๊ณผ ๋ฒ”์œ„๋ฅผ ํ™•๋Œ€ํ•˜๊ณ  ์žˆ๋‹ค. ๊ณต๊ณต์‚ฌ์—…์€ ๋ˆ์„ ๋ฒŒ๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ตญ๊ฐ€๊ฐ€ ๊ตญ๋ฏผ์„ ๋Œ€์ƒ์œผ๋กœ ๊ณต๊ณต์„ฑ์„ ๊ฐ€์ง„ ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ๊ตญ๋ฏผ๋“ค์˜ ํŽธ์ต์„ ์ฆ์ง„์‹œํ‚ค๋Š” ๊ฒƒ์„ ๊ฐ€์žฅ ํฐ ๋ชฉ์ ์œผ๋กœ ์ƒ๊ฐํ•œ๋‹ค. ์„ธ๊ธˆ์œผ๋กœ ๊ฑท์–ด์ง„ ์˜ˆ์‚ฐ์„ ์ง‘ํ–‰ํ•˜๋Š” ๊ณต๊ณต์‚ฌ์—…์ด๋‹ค ๋ณด๋‹ˆ ๊ตญ๊ฐ€ ์žฌ์ •(budget)์„ ํˆฌ์ž…ํ•˜๋”๋ผ๋„ ๊ทœ๋ชจ๊ฐ€ ํฐ ์‚ฌ์—…์˜ ๊ฒฝ์šฐ, ์‚ฌ์—… ์ง„ํ–‰์„ ์œ„ํ•ด์„œ๋Š” ๊ธฐํš์žฌ์ •๋ถ€๊ฐ€ ์ œ์‹œํ•˜๋Š” ์˜ˆ๋น„ํƒ€๋‹น์„ฑ ์กฐ์‚ฌ(์ดํ•˜ ์˜ˆํƒ€)๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. Table IV-15. ๋ฏผ๊ฐ„์‚ฌ์—…๊ณผ ๊ณต๊ณต์‚ฌ์—…์˜ ์‚ฌ์—…ํƒ€๋‹น์„ฑ ์กฐ์‚ฌ ๋น„๊ต ๊ตญ๋‚ด ๊ณต๊ณต์‚ฌ์—…์˜ ์˜ˆ๋น„ํƒ€๋‹น์„ฑ ์กฐ์‚ฌ๋Š” Figure IV-38๊ณผ ๊ฐ™์ด ํฌ๊ฒŒ ๋‹ค์Œ 5๊ฐ€์ง€ ๋ถ„์„์„ ์ง„ํ–‰ํ•œ๋‹ค. ์‚ฌ์—… ๊ธฐ์ดˆ์ž๋ฃŒ ๋ถ„์„ ๊ฒฝ์ œ์  ๋ถ„์„ ์ •์ฑ…์  ๋ถ„์„ ์ง€์—ญ ๊ท ํ˜• ๋ฐœ์ „ ๋ถ„์„ ๋‹ค๊ธฐ์ค€ ๋ถ„์„(AHP) Figure IV-38. ์˜ˆ๋น„ํƒ€๋‹น์„ฑ ์กฐ์‚ฌ ์ˆ˜ํ–‰์ฒด๊ณ„ ์ด ์ค‘ ๊ฒฝ์ œ์„ฑ ๋ถ„์„์€ ๋ฏผ๊ฐ„ ๊ธฐ์—…์—์„œ ์ถ”์ง„ํ•˜๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์˜ ์ˆ˜์ต์„ฑ ๋ถ„์„์— ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์†์ต๋ถ„๊ธฐ์ (Break Even Point: BEP)์ด๋‚˜ ๋ชฉํ‘œ ์ด์ž์œจ, ์ˆ˜์ต ๋“ฑ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋น„์šฉ-ํŽธ์ต ๋ถ„์„(Cost-Benefit Analysis)์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.[5] ์ด๋น„์šฉ๊ณผ ์ด ํŽธ์ต์˜ ๋น„์œจ์„ ์‚ฐ์ •ํ•˜๋Š” B/C ๋น„์œจ์„ ๊ตฌํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, B/C ๋น„์œจ์ด 1๋ณด๋‹ค ํด ๋•Œ ๊ฒฝ์ œ์  ํƒ€๋‹น์„ฑ์„ ํ™•๋ณดํ•œ๋‹ค๊ณ  ๊ฐ„์ฃผํ•œ๋‹ค. ๋˜ํ•œ, ๋ชจ๋“  ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์ข…ํ•ฉํ•˜์—ฌ ๋‹ค๊ธฐ์ค€ ๋ถ„์„ ๋˜๋Š” ๊ณ„์ธตํ™” ๋ถ„์„ ๋ฒ•(Analytic Hierarchy Process: AHP)์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ AHP๊ฐ€ 0.5 ์ด์ƒ์ด๋ฉด ์‚ฌ์—… ์‹œํ–‰์ด ๋ฐ”๋žŒ์งํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. Break #19. ์ „๋ฌธ๊ฐ€์˜ ํ†ต์ฐฐ๋ ฅ? AHP? AHP๋Š” '์Œ๋Œ€ ๋น„๊ต(Pairwise Comparison)'๋ผ๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด์„œ ๋น„๊ต ๋Œ€์ƒ ๊ฐ„์˜ ๊ฐ€์ค‘์น˜(์ค‘์š”๋„)๋ฅผ ๋„์ถœํ•œ๋‹ค. ๋น„๊ต ๋Œ€์ƒ์ด ๋‹ค์ˆ˜ ๊ฐœ์ผ ๊ฒฝ์šฐ ์ด๋“ค์„ ๋™์‹œ์— ๊ณ ๋ คํ•˜์—ฌ ๊ทธ๋“ค ์‚ฌ์ด์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ๋ž€ ์‚ฌ์‹ค์ƒ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋น„๊ต ๋Œ€์ƒ(๋˜๋Š” ๋น„๊ต ์š”์†Œ)๋ฅผ 1:1๋กœ ๋งŒ๋“ค์–ด์ฃผ๋ฉด ์‰ฝ๊ฒŒ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค. AHP์˜ ๊ฐ€์ค‘์น˜ ๋„์ถœ์€ ๋น„๊ต ์š”์†Œ๋“ค์„ ์ƒ์œ„ ์š”์†Œ(๋˜๋Š” ํŒ๋‹จ ๊ธฐ์ค€)์— ๋Œ€ํ•ด 1:1 ๋น„๊ต๋ฅผ ํ†ตํ•ด ๋น„์œจ์ฒ™๋„๋ฅผ ๋„์ถœํ•˜๊ณ  ๊ทธ ๋น„์œจ์ฒ™๋„๋ฅผ ๊ธฐ์ดˆ๋กœ ๊ณ ์œ ์น˜(Eignevalue)๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์š”์†Œ๋ณ„ ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฐ์ •ํ•ด ๋‚ด๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด ๊ณผ์ •์—์„œ ์ผ๊ด€์„ฑ<NAME>๋ฅผ ์‚ฐ์ถœํ•˜์—ฌ ์˜์‚ฌ๊ฒฐ์ •์ž์˜ ํŒ๋‹จ์ด ๋…ผ๋ฆฌ์ ์œผ๋กœ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๊ณ  ์žˆ๋Š” ์ •๋„๋ฅผ ๋ณด์—ฌ์คŒ์œผ๋กœ์จ ํŒ๋‹จ์˜ ์˜ค๋ฅ˜๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ๊ฒฌ์ด์ง€๋งŒ ๋›ฐ์–ด๋‚œ ํ†ต์ฐฐ๋ ฅ์„ ๊ฐ€์ง„ ์ „๋ฌธ๊ฐ€์˜ ํŒ๋‹จ ๊ฒฐ๊ณผ๋ฅผ ๋’ค์—Ž๋Š” ๊ฒฐ๋ก ์„ ์ด๋Œ์ง€๋Š” ๋ชปํ•˜์˜€๋‹ค. ๋‹ค๋งŒ, ๊ทธ๊ฒƒ์˜ ๋…ผ๋ฆฌ์  ๋˜๋Š” ์‚ฐ์ˆ ์  ๊ทผ๊ฑฐ๋ฅผ ์ œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ๋“ค์–ด AHP๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด์ž. ์•ž์„œ ์•Œ์•„๋ณธ BSC์˜ 4๊ฐ€์ง€ ๊ด€์  ์ฆ‰, ์žฌ๋ฌด์  ๊ด€์ (A), ๊ณ ๊ฐ ๊ด€์ (B), ๋‚ด๋ถ€ ํ”„๋กœ์„ธ์Šค ๊ด€์ (C), ํ•™์Šต๊ณผ ์„ฑ์žฅ ๊ด€์ (D) ๊ฐ„์˜ ์ค‘์š”๋„๋ฅผ ์‚ฐ์ถœํ•ด ๋ณด๋Š” ์‚ฌ๋ก€์ด๋‹ค. ๊ฐ ๊ด€์ ์—์„œ ๋Œ€ํ•ด 40%, 30%, 25%, 5%์™€ ๊ฐ™์€ ์ž„์˜์˜ ์ ˆ๋Œ€์น˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ 'A๊ฐ€ B๋ณด๋‹ค ์•ฝ๊ฐ„ ์ค‘์š”ํ•˜๋‹ค'๋ผ๋“ ๊ฐ€ 'B๊ฐ€ C๋ณด๋‹ค ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค'์™€ ๊ฐ™์€ ์ผ์ƒ์ ์ธ ํ‘œํ˜„์„ ํ†ตํ•ด ๊ฐ ๊ด€์  ๊ฐ„์˜ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด์ž. 4๊ฐœ์˜ ๊ด€์  A, B, C, D์— ๋Œ€ํ•ด ๋น„๊ต ํ–‰๋ ฌ์˜ ์š”์†Ÿ๊ฐ’์œผ๋กœ 1-9๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค.[6] Figure IV-39์™€ ๊ฐ™์€ ๋น„๊ต ํ–‰๋ ฌ(Comparison Matrix)๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค. ์ฆ‰, A(์žฌ๋ฌด์  ๊ด€์ )์€ Figure IV-39. ๋น„๊ต ํ–‰๋ ฌ B(๊ณ ๊ฐ ๊ด€์ )๋ณด๋‹ค ์•ฝ๊ฐ„ ์ค‘์š”ํ•˜๋‹ค๋Š” ํ‘œํ˜„์ด๋‹ค.(3๋ฐฐ ์ค‘์š”ํ•˜๋‹ค๊ฐ€ ์•„๋‹ˆ๋‹ค). ๋Œ€๊ฐ์„  ์•„๋ž˜์˜ ๋ž€์—๋Š” ๋Œ€๊ฐ์„  ์œ„์ชฝ ํ•ด๋‹น ๋ž€์˜ ์—ญ์ˆ˜๋ฅผ ์ž๋™์ ์œผ๋กœ ๋ถ€์—ฌํ•œ๋‹ค. ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ์ž‘์„ฑ๋œ ๋น„๊ต ํ–‰๋ ฌ์˜ ์ฃผ ๊ณ ์œ ๋ฒกํ„ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์œ„์˜ 1:1 ๋น„๊ต ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ฉํ•œ๋‹ค. ์šฐ์„  Figure IV-39์˜ ์Œ๋Œ€ ๋น„๊ต ๋งคํŠธ๋ฆญ์Šค์˜ ๊ฐ ์—ด(column)์— ๋Œ€ํ•œ ํ•ฉ๊ณ„๋ฅผ Figure IV-40๊ณผ ๊ฐ™์ด ๊ตฌํ•œ๋‹ค. Figure IV-40. ๋น„๊ต ํ–‰๋ ฌ ๊ณ„์‚ฐ(1) ๊ทธ๋‹ค์Œ ๊ฐ ์—ด์˜ ํ•ฉ๊ณ„ ๊ฐ’์œผ๋กœ ๊ฐ ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋‚˜๋ˆ„์–ด ํ‘œ์ค€ํ™”(normalization) ํ•˜๊ณ , ํ‘œ์ค€ํ™”๋œ ๋งคํŠธ๋ฆญ์Šค์—์„œ ๊ฐ ํ–‰์˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•œ ๋‹ค์Œ, ์ด๋ฅผ ์š”์†Œ์˜ ๊ฐœ์ˆ˜์ธ 4๋กœ ๋‚˜๋ˆ„๋ฉด ๊ฐ ์š”์†Œ์˜ ํ‰๊ท  ๊ฐ€์ค‘์น˜, ์ฆ‰ ์šฐ์„ ์ˆœ์œ„/์ค‘์š”๋„ ๋ฒกํ„ฐ๊ฐ€ ๊ตฌํ•ด์ง„๋‹ค.(Figure IV-41) Figure IV-41. ๋น„๊ต ํ–‰๋ ฌ (2) ์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ์ตœ์ข…์ ์œผ๋กœ 4๊ฐœ ๊ด€์  ๊ฐ๊ฐ์˜ ์ค‘์š”๋„๊ฐ€ ๋„์ถœ๋˜์—ˆ๋‹ค. ์ฆ‰, ์žฌ๋ฌด์  ๊ด€์ (A), ๊ณ ๊ฐ ๊ด€์ (B), ๋‚ด๋ถ€ ํ”„๋กœ์„ธ์Šค ๊ด€์ (C), ํ•™์Šต๊ณผ ์„ฑ์žฅ ๊ด€์ (D)์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ๊ฐ 0.512, 0.238, 0.078, 0.172๋ผ๋Š” ์ด์•ผ๊ธฐ ํžˆ๋‹ค. ์ด๊ฒƒ์˜ ํ•ด์„์€ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•œ ์žฌ๋ฌด์  ๊ด€์ (A)์€ ๋‹ค๋ฅธ 3๊ฐœ๋ฅผ ํ•ฉํ•œ ๊ฒƒ๋ณด๋‹ค ํฐ 51.2%์˜ ์ค‘์š”๋„๋ฅผ ๊ฐ€์ง„๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ด ํšŒ์‚ฌ์—์„œ BSC์˜ ๊ด€๋ฆฌ์—์„œ ์–ด๋Š ๋ถ€๋ถ„์˜ KPI ๊ด€๋ฆฌ์— ๋” ์ง‘์ค‘ํ•ด์•ผ ํ•  ๊ฒƒ์ธ์ง€ ํšจ์œจ์ ์ธ ์˜์‚ฌ๊ฒฐ์ •์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ์‹ค์ œ AHP๋Š” ๊ธฐ๋ฒ• ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์œผ๋ฉฐ ์—‘์…€์ด๋‚˜ ์ „๋ฌธ ๋ถ„์„ ๋„๊ตฌ๋ฅผ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๊ทธ๋ฆฌ๊ณ  ๋ฌด์—‡๋ณด๋‹ค๋„ ์ œ๋Œ€๋กœ ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค. ์–ด๋–ค ๋ถ„์„์ด๋“  'Garbage in, Garbage out' ์›์น™์€ ๋ถˆ๋ณ€์˜ ์ง„๋ฆฌ์ด๋‹ค. [1] ๊ฑด์„ค์—…์—์„œ๋Š” ์—”์ง€๋‹ˆ์–ด๋ง ์—…์ฒด๊ฐ€ ์ปจ์„คํŒ… ์—…์ฒด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. [2] ๊ฑด์„ค ์‚ฌ์—… ์†Œ์œ„, ๋„๋กœ, ์ฒ ๋„, ๊ณตํ•ญ, ํ•ญ๋งŒ ๋“ฑ ๊ตญ๊ฐ€๊ธฐ๋ฐ˜ ์‹œ์„ค์„ ๊ตฌ์ถ•ํ•˜๋Š” SOC(Social Overhead Capital. ์‚ฌํšŒ๊ฐ„์ ‘์ž๋ณธ) ์‚ฌ์—…์—์„œ F/S๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜ํ™”๋˜์–ด ์žˆ์œผ๋ฉฐ SOC ์‚ฌ์—…์€ ํฌ๊ฒŒ ๋‹ค์Œ 3๋‹จ๊ณ„๋กœ ์ง„ํ–‰๋œ๋‹ค. (1) ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„(Feasibility Study: F/S) (2) ์—”์ง€๋‹ˆ์–ด๋ง/๊ตฌ๋งค/๊ฑด์„ค(Engineering/Procurement/Construction: EPC) (3) ์šด์˜ ๊ด€๋ฆฌ(Operation and Management: O&M) [3] http://www.wikipedia.org [4] ์ด ์ˆœ์„œ๋Š” ๋ณด๊ณ ์„œ ๋ชฉ์ฐจ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„ ๋ณด๊ณ ์„œ๋Š” ์‚ฌ์—…์˜ ์ข…๋ฅ˜๋‚˜ ๊ณ ๊ฐ์˜ ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž๊ฒŒ ์ด ํ•ญ๋ชฉ๋“ค์„ ์ ์ ˆํžˆ ์ทจํ•ฉ, ์ •๋ฆฌํ•˜์—ฌ ๋ชฉ์ฐจ๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. [5] ๋น„์šฉ-ํŽธ์ต ๋ถ„์„์ด ์ ์ ˆํ•˜์ง€ ์•Š์€ ์ˆœ์ˆ˜ R&D ์‚ฌ์—…์€ ๋น„์šฉ-ํšจ๊ณผ(Cost-Effectiveness) ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. [6] AHP์—์„œ 9์  ์ฒ™๋„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์‹ฌ๋ฆฌํ•™์˜ '์ž๊ทน-๋ฐ˜์‘ ์ด๋ก '์—์„œ ๋„์ถœ๋œ ์—ฐ๊ตฌ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ A์™€ B์˜ ์„ ํ˜ธ๋„๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ 1, ์•ฝ๊ฐ„ ์ข‹์€ ๊ฒฝ์šฐ 3, ๊ฝค ์ข‹์€ ๊ฒฝ์šฐ 5, ๋งค์šฐ ์ข‹์€ ๊ฒฝ์šฐ 7, ์›”๋“ฑํžˆ ์ข‹์€ ๊ฒฝ์šฐ 9๋กœ ํ‰๊ฐ€ํ•œ๋‹ค. 6.3 ํˆฌ์ž/์‚ฌ์—… ๊ฐ€์น˜ ํ‰๊ฐ€ ๋ฒ• ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์˜ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” ๊ฒฝ์ œ์„ฑ ๋ถ„์„์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๋ถ€๋ถ„์ด๋‹ค. ํ˜น์ž๋Š” ์ˆ˜์ต์„ฑ ๋ถ„์„์ด๋ผ๊ณ ๋„ ๋งํ•˜๋Š”๋ฐ ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ์ˆ˜์ต์„ฑ ๋ถ„์„์€ ์•„๋‹ˆ๋‹ค. ์˜์—…์ด์ต์ด๋‚˜ ์˜์—…์ด์ต๋ฅ , ๋‹น๊ธฐ์ˆœ์ด์ต ๋“ฑ ๊ธฐ์—…์ด ์–ผ๋งˆ๋‚˜ ๋ˆ์„ ๋ฒŒ ์ˆ˜ ์žˆ๋Š”๊ฐ€๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํˆฌ์ž ๊ธฐ๊ฐ„์ด๋‚˜ ํˆฌ์ž๋œ ํ˜„๊ธˆํ๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์‚ฌ์—…์ด ํƒ€๋‹นํ•œ ๊ฒƒ์ธ๊ฐ€๋ฅผ ์‚ดํŽด๋ณด๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ต์„ฑ ๋ถ„์„์ด๋ผ๋Š” ๋ง๋ณด๋‹ค๋Š” ๊ฒฝ์ œ์„ฑ ๋ถ„์„์ด๋ผ๋Š” ๋ง์ด ์˜ฌ๋ฐ”๋ฅธ ์šฉ์–ด์ด๋‹ค. ๋„๋ฆฌ ํ™œ์šฉ๋˜๊ณ  ์žˆ๋Š” 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž. [#1. ํšŒ์ˆ˜๊ธฐ๊ฐ„๋ฒ•(PP: Payback Period Method)] ํšŒ์ˆ˜๊ธฐ๊ฐ„๋ฒ•์€ ํˆฌ์ž ์•ˆ์— ์†Œ์š”๋˜๋Š” ์›๊ธˆ์„ ํšŒ์ˆ˜ํ•˜๋Š” ๊ธฐ๊ฐ„์„ ๊ตฌํ•˜์—ฌ ํˆฌ์ž์˜์‚ฌ ๊ฒฐ์ •์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋งค์šฐ ๊ฐ„๋‹จํ•œ๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐํ•œ๋‹ค. ํšŒ์ˆ˜๊ธฐ๊ฐ„๋ฒ•์˜ ์žฅ์ ์€ ๊ณ„์‚ฐ์ด ๊ฐ„๋‹จํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šฐ๋ฉฐ, ์ž๊ธˆ ์‚ฌ์ •์˜ ์•ˆ์ • ์ธก๋ฉด์—์„œ ์œ ๋™์„ฑ์„ ์„ ํ˜ธํ•˜๊ณ , ๊ธฐ์ค€ ์‹œ์  ์ดํ›„์˜ ํ˜„๊ธˆ ํ๋ฆ„์˜ ๋ถˆํ™•์‹ค์„ฑ์„ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ์ ์€ ํ™”ํ์˜ ์‹œ๊ฐ„๊ฐ€์น˜๋ฅผ ๋ฌด์‹œํ•˜์—ฌ ์ž„์˜์˜ ๊ธฐ์ค€์‹œ์ ์ด ํ•„์š”ํ•˜๊ณ , ๊ธฐ์ค€์‹œ์  ์ดํ›„์˜ ํ˜„๊ธˆํ๋ฆ„์€ ๋ฌด์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์— R&D์™€ ๊ฐ™์€ ์žฅ๊ธฐ/์‹ ๊ทœ ์‚ฌ์—…์˜ ํ‰๊ฐ€๋„๊ตฌ๋กœ์„œ๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค. ์˜์‚ฌ๊ฒฐ์ • ๊ธฐ์ค€์€ ๋…๋ฆฝ์ ์ธ ํˆฌ์ž ์•ˆ์˜ ๊ฒฝ์šฐ, ๋ชฉํ‘œ ํšŒ์ˆ˜๊ธฐ๊ฐ„๋ณด๋‹ค ์งง์€ ํˆฌ์ž ์•ˆ์„ ์„ ํƒํ•˜๋ฉฐ ์ƒํ˜ธ ๋ฐฐํƒ€์ ์ธ ํˆฌ์ž ์•ˆ์˜ ๊ฒฝ์šฐ, ๋ชฉํ‘œ ํšŒ์ˆ˜๊ธฐ๊ฐ„๋ณด๋‹ค ์งง์€ ํˆฌ์ž ์•ˆ ์ค‘ ๊ฐ€์žฅ ์งง์€ ํˆฌ์ž ์•ˆ์„ ์„ ํƒํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๊ฐ๊ฐ 10,000์›์„ ํˆฌ์žํ•œ ํˆฌ์ž ์•ˆ A, B์˜ ํšŒ์ˆ˜๊ธฐ๊ฐ„์„ ๊ณ„์‚ฐํ•ด ๋ณด๋ฉด (1) ํˆฌ์ž ์•ˆ A - 1์ฐจ๋…„ + 2์ฐจ๋…„ + 1,000 = 10,000 - 2๋…„ + 1,000/3,000 = 2.33๋…„ (2) ํˆฌ์ž ์•ˆ B - 1์ฐจ๋…„ + 2์ฐจ๋…„ + 3์ฐจ๋…„ +4,000 = 10,000 - 4๋…„ ๋”ฐ๋ผ์„œ ํšŒ์ˆ˜๊ธฐ๊ฐ„์ด 2.33๋…„์œผ๋กœ 4๋…„๋ณด๋‹ค ์งง์€ ํˆฌ์ž ์•ˆ A๋ฅผ ์ฑ„ํƒํ•œ๋‹ค. [#2. ํšŒ๊ณ„์  ์ด์ต๋ฅ ๋ฒ•(ARR: Accounting Rate of Return Method)] ํšŒ๊ณ„์  ์ด์ต๋ฅ ๋ฒ•์€ ํˆฌ์ž์•ก์— ๋Œ€ํ•œ ์—ฐํ‰๊ท  ํšŒ๊ณ„์  ์ˆœ์ด์ต๋ฅ ์— ์˜๊ฑฐํ•˜์—ฌ ํˆฌ์ž ์˜์‚ฌ ๊ฒฐ์ •์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐํ•œ๋‹ค. ํšŒ๊ณ„์  ์ด์ต๋ฅ ๋ฒ•์˜ ์žฅ์ ์€ ๊ณ„์‚ฐ์ด ๊ฐ„๋‹จํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šฐ๋ฉฐ, ํšŒ๊ณ„์ž๋ฃŒ๋ฅผ ๋ฐ”๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ์–ด ํŽธ๋ฆฌํ•˜๋‹ค. ๋‹จ์ ์€ ํ™”ํ์˜ ์‹œ๊ฐ„๊ฐ€์น˜๋ฅผ ๋ฌด์‹œํ•˜์—ฌ ์‹ค์ œ ์ˆ˜์ต๋ฅ ์ด ์•„๋‹ˆ๋ฉฐ ๋ชฉํ‘œ์ด์ต๋ฅ  ๊ฒฐ์ •์ด ์ž์˜์ ์ด๋‹ค. ๋˜ํ•œ, ํšŒ๊ณ„๋ณ€๊ฒฝ์— ๋”ฐ๋ฅธ ์ฐจ์ด๋กœ ๊ฐ๊ด€์„ฑ์ด ์ €ํ•˜๋  ์ˆ˜ ์žˆ๋‹ค. ์˜์‚ฌ๊ฒฐ์ • ๊ธฐ์ค€์€ ๋…๋ฆฝ์ ์ธ ํˆฌ์ž ์•ˆ์˜ ๊ฒฝ์šฐ, ์ด์ต๋ฅ ์ด ๋†’์€ ํˆฌ์ž ์•ˆ์„ ์„ ํƒํ•˜๋ฉฐ ์ƒํ˜ธ ๋ฐฐํƒ€์ ์ธ ํˆฌ์ž ์•ˆ์˜ ๊ฒฝ์šฐ, ๋ชฉํ‘œ์ด์ต๋ฅ ๋ณด๋‹ค ๋†’์€ ํˆฌ์ž ์•ˆ ์ค‘ ๊ฐ€์žฅ ์ด์ต๋ฅ ์ด ๋†’์€ ํˆฌ์ž ์•ˆ์„ ์„ ํƒํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํšŒ๊ณ„์  ์ด์ต๋ฅ ๋ฒ•์— ๋”ฐ๋ฅด๋ฉด (1) ์—ฐํ‰๊ท  ์žฅ๋ถ€๊ฐ€์•ก = 9,000+ 6,000 + 3,000 + 0 = 18,000; 18,000 / 4 = 4,500 (2) ์—ฐํ‰๊ท  ์ˆœ์ด์ต = (3,000 + 2,000+1,000) / 3 = 2,000 (1์ฐจ๋…„๋„ ์ด์ต) 12,000 โ€“ 6,000 โ€“ 3,000 = 3,000 (2์ฐจ๋…„๋„ ์ด์ต) 10,000 โ€“ 5,000 โ€“ 3,000 = 2,000 (3์ฐจ๋…„๋„ ์ด์ต) 8,000 โ€“ 4,000 โ€“ 3,000 = 1,000 ํšŒ๊ณ„์  ์ด์ต๋ฅ  = ์—ฐํ‰๊ท  ์ˆœ์ด์ต / ํ‰๊ท  ํˆฌ์ž์•ก = 2,000 / 4,500 = 44% [#3. ๋‚ด๋ถ€ ์ˆ˜์ต๋ฅ ๋ฒ•(IRR: Internal Rate of Return Method)] ๋‚ด๋ถ€์ˆ˜์ต๋ฅ ๋ฒ•์€ ํˆฌ์ž ์•ˆ์˜ ๋‚ด๋ถ€์ˆ˜์ต๋ฅ ์„ ๊ตฌํ•˜์—ฌ ์ด๋ฅผ ์š”๊ตฌ์ˆ˜์ต๋ฅ (์ž๋ณธ๋น„์šฉ)๊ณผ ๋น„๊ตํ•จ์œผ๋กœ์จ ํˆฌ์ž์˜์‚ฌ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐํ•œ๋‹ค. ์‚ฐ์‹์ด ์ข€ ์–ด๋ ต๊ฒŒ ๋ณด์ด๋Š”๋ฐ ๋ฏธ๋ž˜ ํ˜„๊ธˆ์œ ์ถœ์˜ ํ˜„๊ฐ€์™€ ๋ฏธ๋ž˜ ํ˜„๊ธˆ์œ ์ž…์˜ ํ˜„๊ฐ€๊ฐ€ ๊ฐ™์•„์ง€๋Š” ์ด์ž์œจ์„ ๊ตฌํ•˜๋ฉด ๊ทธ๊ฒŒ IRR์ด๋‹ค. ์žฅ์ ์€ ์ดํ•ดํ•˜๊ณ  ์˜์‚ฌ์†Œํ†ตํ•˜๊ธฐ ์‰ฌ์šฐ๋ฉฐ ์ˆœ ํ˜„์žฌ๊ฐ€์น˜ ๋ฒ•(NPV)๊ณผ ๋ฐ€์ ‘ํ•˜๊ฒŒ ๊ด€๋ จ๋˜์–ด ์žˆ๊ณ  ์ข…์ข… ๋™์ผํ•œ ์˜์‚ฌ๊ฒฐ์ •์ด ๋‚ด๋ ค์ง„๋‹ค. ๋‹จ์ ์€ ์ž๋ณธ๋น„์šฉ์„ ๊ตฌํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ์˜์‚ฌ๊ฒฐ์ • ๊ธฐ์ค€์€ ๋…๋ฆฝ์ ์ธ ํˆฌ์ž ์•ˆ์˜ ๊ฒฝ์šฐ, IRR >์š”๊ตฌ ์ˆ˜์ต๋ฅ (์ž๋ณธ๋น„์šฉ, k)์ธ ๊ฒฝ์šฐ, ์ƒํ˜ธ ๋ฐฐํƒ€์ ์ธ ํˆฌ์ž ์•ˆ์˜ ๊ฒฝ์šฐ, IRR > k์ด๋ฉด์„œ IRR์ด ๊ฐ€์žฅ ํฐ ํˆฌ์ž ์•ˆ์„ ์„ ํƒํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํˆฌ์ž ์•ˆ A, B์˜ ๋‚ด๋ถ€์ˆ˜์ต๋ฅ ์„ ๊ณ„์‚ฐํ•ด ๋ณด๋ฉด (1) ํˆฌ์ž ์•ˆ A 24,000 = 10,320 / (1+r) + 10,320 / (1+r) 2 + โ€ฆ + 9,120 / (1+r) 5 r = 29.66% (2) ํˆฌ์ž ์•ˆ B 32,000 = 12,160 / (1+ r) + 12,160 / (1+r) 2 + โ€ฆ + 12,160 / (1+r) 5 r = 26.07% ๋”ฐ๋ผ์„œ ์˜์‚ฌ๊ฒฐ์ •์€ ๋‚ด๋ถ€์ˆ˜์ต๋ฅ ์ด ๋†’์€ ํˆฌ์ž ์•ˆ A๋ฅผ ์ฑ„ํƒํ•œ๋‹ค. IRR์˜ ๊ฒฝ์šฐ, NPV์™€ ๋”๋ถˆ์–ด ๋น„๊ต์  ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ฏ€๋กœ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ(spreadsheet) ์†Œํ”„ํŠธ์›จ์–ด์—์„œ ๋ณดํ†ต ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค. MS EXCEL์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋ฉด IRR ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‚ด๋ถ€์ˆ˜์ต๋ฅ  r์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ดˆ๊ธฐ ํˆฌ์ž ๊ฐ’(์Œ์ˆ˜)๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ํˆฌ์ž ๊ฐ’๊นŒ์ง€ ๋‚˜์—ดํ•˜๊ณ  IRR ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์„ ํƒํ•˜๋ฉด ๋‚ด๋ถ€์ˆ˜์ต๋ฅ ์„ ๊ณ„์‚ฐํ•ด ์ค€๋‹ค. [#4. ์ˆœ ํ˜„์žฌ๊ฐ€์น˜ ๋ฒ•(NPV: Net Present Value Method)] ์ˆœ ํ˜„์žฌ๊ฐ€์น˜ ๋ฒ•์€ ํˆฌ์ž๋กœ ์ธํ•œ ๋ฏธ๋ž˜์˜ ํ˜„๊ธˆ์œ ์ž…์„ ์ ์ ˆํ•œ ํ• ์ธ์œจ๋กœ ํ• ์ธํ•œ ๊ธˆ์•ก์—์„œ ๋ฏธ๋ž˜์˜ ํ˜„๊ธˆ ์œ ์ถœ์„ ํ• ์ธํ•œ ๊ธˆ์•ก์„ ์ฐจ๊ฐํ•˜์—ฌ ๊ตฌํ•œ NPV๋ฅผ ๊ทผ๊ฑฐ๋กœ ํˆฌ์ž๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐํ•œ๋‹ค. NPV๋Š” 'ํ˜„๊ธˆ ์œ ์ž…์˜ ํ˜„๊ฐ€ - ํ˜„๊ธˆ ์œ ์ถœ์˜ ํ˜„๊ฐ€'๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ 0๋ณด๋‹ค ํฌ๋ฉด ์ˆ˜์ต์„ฑ์ด ์žˆ๋‹ค๊ณ  ํŒ๋‹จํ•œ๋‹ค. ์žฅ์ ์€ ํ™”ํ์˜ ์‹œ๊ฐ„๊ฐ€์น˜๋ฅผ ๊ณ ๋ คํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ์˜ค์ง ํ˜„๊ธˆํ๋ฆ„ ๊ธฐ๋Œ€์น˜์™€ ์ž๋ณธ๋น„์šฉ๋งŒ์ด ๊ณ ๋ ค๋  ๋ฟ ํšŒ๊ณ„์  ์ˆ˜์น˜์™€๋Š” ๋ฌด๊ด€ํ•˜๋‹ค. ๋‹จ์ ์€ ํ• ์ธ์œจ์˜ ์ถ”์ •์ด ์–ด๋ ค์šฐ๋ฉฐ ๋ฏธ๋ž˜ ์ถ”์ •์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ€ ์ž์˜์ ์ผ ์ˆ˜ ์žˆ๋‹ค. ์˜์‚ฌ๊ฒฐ์ • ๊ธฐ์ค€์€ ํ†ก๋ฆฝ์ ์ธ ํˆฌ์ž ์•ˆ์˜ ๊ฒฝ์šฐ, NPV > 0, ์ƒํ˜ธ๋ฐฐํƒ์ ์ธ ํˆฌ์ž ์•ˆ์˜ ๊ฒฝ์šฐ NPV > 0์ด๋ฉด์„œ NPV๊ฐ€ ๊ฐ€์žฅ ํฐ ํˆฌ์ž ์•ˆ์„ ์ฑ„ํƒํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. (์ž๋ณธ๋น„์šฉ์€ 20%๋ฅผ ๊ฐ€์ •ํ•œ๋‹ค) ํˆฌ์ž ์•ˆ A, B์˜ NPV๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๋ฉด (1) ํˆฌ์ž ์•ˆ A 10,320 / (1 + 0.2) + 10,320 / (1 + 0.2) 2 + โ€ฆ + 10,320 / (1 + 0.2) 5โ€“ 24,000 = 5,108 (2) ํˆฌ์ž ์•ˆ B 12,160 / (1 + 0.2) + 12,160 / (1 + 0.2) 2 + โ€ฆ + 12,160 / (1 + 0.2) 5โ€“ 32,000 = 4,366 ๋”ฐ๋ผ์„œ ์˜์‚ฌ๊ฒฐ์ •์€ NPV> 0์ด๋ฉด์„œ ๊ฐ’์ด ๋” ํฐ ํˆฌ์ž ์•ˆ A๋ฅผ ์ฑ„ํƒํ•œ๋‹ค. ์•ž์„œ ์‚ดํŽด๋ณธ IRR์ฒ˜๋Ÿผ NPV๋„ MS EXCEL์„ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ดˆ๊ธฐ ํˆฌ์ž ์•ˆ๋ถ€ ํ„ฐ ๋งˆ์ง€๋ง‰ ํˆฌ์ž ๊ฐ’๊นŒ์ง€ ๋‚˜์—ดํ•œ ํ›„ NPV ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•œ๋‹ค. NPV ํ•จ์ˆ˜๋Š” NPV(v1, v2:v3)+v4์—์„œ v1์€ ์ž๋ณธ๋น„์šฉ ๋˜๋Š” ์—ฐ๊ฐ„ํ• ์ธ์œจ์ด๊ณ  v2๋ถ€ํ„ฐ v3๋Š” ํˆฌ์ž๋œ ๊ฐ’๋“ค, v4๋Š” ์ดˆ๊ธฐ ํˆฌ์ž ๊ฐ’์ด๋‹ค. ์ฃผ์˜ํ•  ์ ์€ v4๋Š” ์‹ค์ œ ๋งˆ์ด๋„ˆ์Šค ๊ฐ’์ด์ง€๋งŒ ์—‘์…€ ์‚ฐ์‹์—์„œ๋Š” ๊ฐ’๋งŒ ๋”ํ•ด์ค€๋‹ค. ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์—์„œ ๊ฐ€์žฅ ์†์‰ฝ๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์†Œํ”„ํŠธ์›จ์–ด๋Š” MS EXCEL๊ณผ ๊ฐ™์€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ์ธ๋ฐ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ๋„ ์ž˜ ๋ชจ๋ธ๋ง ํ•˜์—ฌ ๊ฐ’์„ ์ž…๋ ฅํ•˜๊ณ  ๋ณด๊ธฐ ์ข‹๊ฒŒ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ๋งŒ๋“ค์–ด ๋†“์œผ๋ฉด ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค. ์‚ฌ์‹ค ์ข‹์€ ์ปจ์„คํŒ… ํŽŒ์€ ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ์ด๋ฏธ ๋„๊ตฌํ™”ํ•˜์—ฌ ๊ทธ๋“ค์˜ ๊ท€์ค‘ํ•œ ์ง€์  ์ž์‚ฐ(Knowledge Asset)์œผ๋กœ์„œ ํ™œ์šฉํ•˜๊ณ  ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์†Œ๊ฐœํ•œ ๋ฐฉ๋ฒ• ์ด์™ธ์—๋„ ๋งค์šฐ ๋‹ค์–‘ํ•˜๊ณ  ์ „๋ฌธ์ ์ธ ๊ฐ€์น˜ ํ‰๊ฐ€ ๋ฒ•๋“ค์ด ์กด์žฌํ•˜์ง€๋งŒ ์‚ฌ์‹ค ํฌ๊ฒŒ ์“ธ ์ผ์ด ์—†๋‹ค. ์™ธ๊ตญ๊ณ„ ๊ธฐ์—…์˜ ์ธ์ˆ˜ํ•ฉ๋ณ‘(M&A)์„ ๋‹ค๋ฃฌ๋‹ค๋ฉด DCF(ํ˜„๊ธˆํ๋ฆ„ ํ• ์ธ๋ฒ•. Discount Cash Flow)๋Š” ์•Œ์•„๋‘๋ฉด ์ข‹๋‹ค. ๊ฐœ๋…์€ NPV ๊ณ„์‚ฐ๊ณผ ๊ฐ™์ด ์ˆœํ˜„๊ธˆ ๊ฐ€์น˜, ์ˆœํ˜„๊ธˆํ๋ฆ„ ํŠนํžˆ, ์ž‰์—ฌํ˜„๊ธˆํ๋ฆ„(FCF. Free Cash Flow)๋ฅผ ๊ฐ€์ง€๊ณ  ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์œ ์‚ฌํ•˜๋‹ค ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ •ํ™•๋„๋กœ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๋‹จ์ •์ ์ธ ์‚ฌ์‹ค๋ณด๋‹ค๋Š” ํ™•๋ฅ ๋กœ ํ‘œํ˜„ํ•ด์•ผ ํ•  ์ผ์ด ์ƒ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์— ๋ชฌํ…Œ์นด๋ฅผ๋กœ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๊ธฐ๋ฒ• ๊ฐ™์€ ๊ฒƒ์„<NAME>ํ•ด์„œ ํ™•๋ฅ ๋ถ„ํฌ์˜ ๊ฐœ๋…์„<NAME>ํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ ๊ฒฐ๊ณผ์ ์œผ๋กœ ํฐ ์ฐจ์ด๊ฐ€ ๋‚˜์ง€๋Š” ์•Š๋Š”๋‹ค. ๋ณด๋‹ค ์ „๋ฌธ์ ์ธ ์„ค๋ช…์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์ด๋Š” ์ •๋„. ์‚ฌ์—…ํƒ€๋‹น์„ฑ ๋ถ„์„์—์„œ ์œ ์˜ํ•  ๊ฒƒ์€ ๊ฐ€์ •์— ๊ฐ€์ •์„ ๋‘๊ณ  ๋ง ๊ทธ๋Œ€๋กœ ํƒ€๋‹น์„ฑ์„ ๊ฒ€ํ† ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ˆซ์ž์— ๋งค๋ชฐ๋˜๋ฉด ์•ˆ ๋œ๋‹ค. ์ „์ฒด์ ์ธ ๋งฅ๋ฝ์—์„œ ์ด ์‚ฌ์—…์ด ์ถ”์ง„ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ธ๊ฐ€๋ฅผ ๊ฒ€ํ† ํ•ด์•ผ ํ•˜๊ณ  ์‚ฌ์—… ๊ด€์ (business perspective)์—์„œ, ์žฌ๋ฌด ๊ด€์ (financial perspective)์—์„œ, ๋ฒ•/์ œ๋„ ๊ด€์ (legal & regulation perspective)์—์„œ ์ด ์‚ฌ์—…์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€๋ฅผ ์ ์ • ํŒ๋‹จํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ธฐ์—… ๋‚ด๋ถ€์˜ ์ •์„œ๋‚˜ ์—ญ๋Ÿ‰ ๋“ฑ๋„ ์‚ฌ์‹ค ์‚ฌ์—…ํƒ€๋‹น์„ฑ์—์„œ ์ค‘์š”ํ•œ ํฌ์ธํŠธ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. ๊ฒฝ์˜์ง„ ์ž…์žฅ์—์„œ๋Š” ๋ˆ์ด ๋˜๋ƒ ์•ˆ๋˜๋ƒ๊ฐ€ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํฌ์ธํŠธ๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ ์žฌ๋ฌด์  ํƒ€๋‹น์„ฑ์„ ํ‰๊ฐ€ํ•˜๋Š” ๊ธฐ๋ฒ•๋“ค์ด 100% ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์ด ์•„๋‹ˆ๊ณ  ์ž„์˜์˜ ๊ฐ€์ •๋„ ๋งŽ์ด ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ์ข…ํ•ฉ์ ์ธ ๊ด€์ ์—์„œ ๊ฒ€ํ† ํ•˜๊ณ  ์˜ต์…˜์ด๋‚˜ ๋Œ€์•ˆ์„ ์ œ์‹œํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. 17. ์ •๋ณด์ „๋žต ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก  ์ •๋ณดํ†ต์‹ ๊ธฐ์ˆ ์˜ ๋ฐœ๋‹ฌ์€ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์˜ ๊ธ‰๊ฒฉํ•œ ๋ณ€ํ™”์™€ ๋‹ค์–‘ํ•œ ์—…์ข… ๊ฐ„์˜ ์ปจ๋ฒ„์ „์Šค(convergence)๋ฅผ ์œ ๋ฐœํ•˜์˜€๋Š”๋ฐ ์ปจ์„คํŒ…๋„ ์˜ˆ์™ธ๋Š” ์•„๋‹ˆ์—ˆ๋‹ค. Figure IV-42๋Š” ๊ธฐ์—… ๋น„์ „๊ณผ ์ •๋ณดํ™” ์ „๋žต์˜ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๊ธฐ์—…์˜ ์ตœ์ข… ๋ชฉํ‘œ์ธ ๋น„์ „(vision) ๋‹ฌ์„ฑ์„ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ์‚ฌ์—… ๋ชฉํ‘œ๊ฐ€ ์žˆ๊ณ , ๊ทธ๊ฒƒ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ํ•ต์‹ฌ ์„ฑ๊ณต์š”์†Œ(CSFs)๋“ค์ด ์žˆ๋‹ค. ํ•ต์‹ฌ ์„ฑ๊ณต์š”์†Œ๋“ค์€ ๊ธฐ์—…์˜ ๊ธฐ๋Šฅ๊ณผ ํ”„๋กœ์„ธ์Šค์˜ ์˜ํ–ฅ์„ ๋ฐ›๊ณ  ์ด๊ฒƒ์ด ๊ธฐ์—…์˜ ์ •๋ณด์‹œ์Šคํ…œ(์‘์šฉ๊ณผ ์ธํ”„๋ผ)์—์„œ ๊ธฐ์ธํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ, ๊ฒฐ๋ก ์ ์œผ๋กœ ์ •๋ณด์‹œ์Šคํ…œ์€ ๊ธฐ์—…์˜ ๋น„์ „๊ณผ ์ƒํ˜ธ ์—ฐ๊ด€๋˜๊ฒŒ ์„ค๊ณ„๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค. ์ฆ‰, ๊ธฐ์—…์˜ ๋น„์ „ ๋‹ฌ์„ฑ ๋˜๋Š” ๋ชฉํ‘œ ๋‹ฌ์„ฑ๊ณผ ๋ฌด๊ด€ํ•œ IT ํˆฌ์ž๋Š” ์ง„ํ–‰๋˜์–ด์„œ๋Š” ์•ˆ๋˜๋‹ค๋Š” ์ด์•ผ๊ธฐ์ด๋‹ค. ์ด๋Ÿฐ ์ด์œ ๋กœ ์ „ํ†ต์ ์ธ ๊ฒฝ์˜ํ˜์‹  ์ปจ์„คํŒ…์ธ BPR[1]๊ณผ ์ •๋ณด์ „๋žต๊ณ„ํš(ISP[2])์€ ํ•˜๋‚˜์˜ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋กœ ํ•ฉ์ณ์ง€๋Š” ์ถ”์„ธ์ด๋‹ค. ์ œ17์žฅ์—์„œ๋Š” BPR/ISP์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž. Figure IV-42. ๊ธฐ์—… ๋น„์ „๊ณผ ์ •๋ณดํ™” ์ „๋žต์˜ ๊ด€๊ณ„ 17.1 BPR์˜ ์ดํ•ด BPR์˜ ์ฐฝ์‹œ์ž์ธ ๋งˆ์ดํด ํ•ด๋จธ(Michael M.Hammer. 1948 ~ 2008)๋Š” BPR์— ๋Œ€ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งํ•˜์˜€๋‹ค. BPR ์ด๋ž€ ๋น„์šฉ, ํ’ˆ์งˆ, ์„œ๋น„์Šค, ์†๋„ ๋“ฑ ์ฃผ์š” ์„ฑ๊ณผ์ธก์ •์ง€ํ‘œ ์ธก๋ฉด์—์„œ ํš๊ธฐ์ ์ธ ๊ฐœ์„ ์„ ์ด๋ฃจ๊ธฐ ์œ„ํ•ด ์—…๋ฌด์ฒ˜๋ฆฌ ์ ˆ์ฐจ๋ฅผ ๊ทผ๋ณธ์ ์œผ๋กœ ์žฌ๊ณ ์ฐฐํ•˜๊ณ  ํ•ฉ๋ฆฌ์ ์œผ๋กœ ์žฌ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋˜, ๋ณ€ํ™”๊ด€๋ฆฌ์™€ ๋ฆฌ๋”์‹ญ ์ „๋ฌธ๊ฐ€์ธ ์ฝ”ํ„ฐ(J.P.Kotter. 1947 ~ ํ˜„์žฌ)๋Š” BPR์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. BPR ์ด๋ž€ ๊ณ„์† ์ƒˆ๋กญ๊ฒŒ ๋ณ€ํ™”ํ•˜๋Š” ์‹œ์žฅ ํ™˜๊ฒฝ์— ํšจ๊ณผ์ ์œผ๋กœ ๋Œ€์‘ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์‚ฌ์—… ์ˆ˜ํ–‰ ๋ฐฉ๋ฒ•์„ ๊ทผ๋ณธ์ ์œผ๋กœ ๋ณ€ํ™”์‹œํ‚ค๋Š” ํ™œ๋™์ด๋‹ค. ๋งˆ์ดํด ํ•ด๋จธ๋Š” BPR์˜ ํ•ต์‹ฌ์— ๋Œ€ํ•ด ๊ทผ๋ณธ์ ์œผ๋กœ ์žฌ๊ณ ์ฐฐ, ํ•ฉ๋ฆฌ์  ์žฌ์„ค๊ณ„๋ฅผ ์ฃผ์ฐฝํ•˜์˜€๊ณ  ์ฝ”ํ„ฐ์˜ ๊ฒฝ์šฐ, ์‚ฌ์—… ์ˆ˜ํ–‰ ๋ฐฉ๋ฒ•์˜ ๊ทผ๋ณธ์  ๋ณ€ํ™”๋ฅผ ๊ฐ•์กฐํ•˜์˜€๋‹ค. ์ด๋Ÿฐ BPR์„ Figure IV-41๊ณผ ๊ฐ™์ด ํฌ๊ฒŒ 3๊ฐ€์ง€ ์œ ํ˜•์œผ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ ๊ธฐ๋Šฅ๋ณ„ ๊ฐœ์„ , ๋‘ ๋ฒˆ์งธ ์—…๋ฌด์žฌ์„ค๊ณ„, ์„ธ ๋ฒˆ์งธ ์—…๋ฌด ์žฌ๊ณ ์ฐฐ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ธฐ๋Šฅ ๊ฐœ์„ ์€ ๊ธฐ์กด์˜ ์—…๋ฌด๊ธฐ๋Šฅ์„ ๊ฐœ์„ ํ•˜์—ฌ ๋” ํ–ฅ์ƒ๋˜๊ณ (Better), ๋” ๋น ๋ฅด๊ณ (Faster), ๋” ์ง€๋Šฅ์ ์œผ๋กœ(Smarter) ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๋ถ€๋ถ„์€ IT์˜ ์˜ํ–ฅ์„ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ›๋Š” ๋ถ€๋ถ„์ด๋‹ค. ์—…๋ฌด์žฌ์„ค๊ณ„๋Š” ๊ธฐ์กด ๋ถ€์„œ์˜ ๊ฒฝ๊ณ„๋ฅผ ์ดˆ์›”ํ•ด์„œ ๊ฐ€์น˜ ์‚ฌ์Šฌ(Value Chain)์„ ์žฌ์„ค๊ณ„ํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์กฐ์ง ์žฌ์„ค๊ณ„์™€ ๋งž๋ฌผ๋ ค ์ง„ํ–‰๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๋˜ํ•œ, ์—…๋ฌด ์žฌ๊ณ ์ฐฐ์€ ๋ฌด(็„ก)์—์„œ ์œ (ๆœ‰)๋ฅผ ์ฐฝ์กฐํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์–ด๋–ป๊ฒŒ ์ „๊ฐœ๋˜๋˜ BPR์€ ๋ณ€ํ™”๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์˜ ์ €ํ•ญ์„ ๊ทน๋ณตํ•˜๊ณ  ๋ณ€ํ™”๊ด€๋ฆฌ๋ฅผ ์ž˜ ํ•ด์•ผ ํ•œ๋‹ค. Figure IV-43. BPR์˜ ์œ ํ˜• ๋ฐ ๋ฒ”์œ„ 17.2 BPR๊ณผ ISP์˜ ๊ด€๊ณ„ ์›๋ž˜ ISP๋Š” ์ œ์ž„์Šค ๋งˆํ‹ด(James Martin. 1933 ~ 2013)์˜ ์ •๋ณด๊ณตํ•™(Information Engineering) ์ด๋ก  ์ค‘ ๊ณ„ํš ์ˆ˜๋ฆฝ ๋ถ€๋ถ„์„ ์นญํ•˜๋˜ ์šฉ์–ด์˜€๋‹ค. ISP๋ž€, ์กฐ์ง์ด๋‚˜ ๊ธฐ๊ด€์˜ ๋น„์ „์„ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ป๊ฒŒ ํšจ๊ณผ์ ์œผ๋กœ ์ •๋ณด๊ธฐ์ˆ (IT)์„ ์ ์šฉํ•˜๊ณ  ์—ฐ๊ณ„ํ•  ๊ฒƒ์ธ๊ฐ€์— ๋Œ€ํ•œ ์ „๋žต๊ณผ ํ•ด๊ฒฐ์ฑ…์„ ์ฐพ๊ณ  ์‹คํ–‰๊ณ„ํš์„ ์ˆ˜๋ฆฝํ•ด๋‚˜๊ฐ€๋Š” ์ผ๋ จ์˜ ๊ณผ์ •์ด๋ผ ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ธฐ์—…์ด๋‚˜ ์ •๋ถ€๊ธฐ๊ด€๋“ค์˜ ๋น„์ „๊ณผ ๋ชฉํ‘œ๊ฐ€ ์ •๋ณด์‹œ์Šคํ…œ์œผ๋กœ ๋ฐ˜์˜๋˜๋„๋ก ํ•˜๊ณ  ์ •๋ณด์‹œ์Šคํ…œ์€ ํ•ต์‹ฌ ๊ธฐ๋Šฅ๊ณผ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ง€์›ํ•˜๋„๋ก ์žฅ๊ธฐ์ ์ธ ๊ณ„ํš์ด ํ•„์š”ํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฐ ๋ชฉ์ ์— ๋”ฐ๋ผ ์ž‘์—… ๋ฐฉ๋ฒ•์ด๋‚˜ ์ ˆ์ฐจ, ์‚ฐ์ถœ๋ฌผ, ๊ธฐ๋ฒ• ๋“ฑ์„ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์ •๋ฆฌํ•ด๋†“์€ ์ฒด๊ณ„๊ฐ€ ํ•„์š”ํ•˜๊ฒŒ ๋˜๋ฉฐ ์ •๋ณด์ „๋žต๊ณ„ํš ๋ฐฉ๋ฒ•๋ก ์€ ์ด๋Ÿฌํ•œ ํ•„์š”๋ฅผ ์ถฉ์กฑํ•˜๊ฒŒ ๋œ๋‹ค. ์•ž์„œ ์–˜๊ธฐํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ชจ๋“  ISP ๋ฐฉ๋ฒ•๋ก ์€ ์ œ์ž„์Šค ๋งˆํ‹ด์˜ ์ •๋ณด๊ณตํ•™์— ๊ทธ ์›๋ฅ˜๋ฅผ ๋‘๊ณ  ์žˆ์œผ๋‚˜ ์ตœ๊ทผ ์ปจ์„คํŒ… ํŽŒ๋“ค์˜ ISP ๋ฐฉ๋ฒ•๋ก ์€ Figure IV-44์˜ ์ ์„  ์ƒ์ž์ฒ˜๋Ÿผ BPR ์ž‘์—…์„ ๋‚ดํฌํ•˜๊ณ  ์žˆ๋‹ค. Figure IV-44. ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์˜ ISP ๋ฐฉ๋ฒ•๋ก  BPR์€ 1980๋…„๋Œ€ ๊ฒฝ์˜ํ˜์‹ ์˜ ๋ฐ”๋žŒ๊ณผ ํ•จ๊ป˜ ํƒœ๋™ํ•˜์—ฌ ๋งŽ์€ ์ปจ์„คํŒ…์—…์ฒด๋“ค์ด ์ˆ˜ํ–‰ํ•ด์™”์ง€๋งŒ ์œ ํ–‰์ฒ˜๋Ÿผ ์ง€๋‚˜๊ฐ€๋ฒ„๋ ธ๊ณ  BPR ๋ฐฉ๋ฒ•๋ก ์€ ์ œ์กฐ/์ƒ์‚ฐ๋ถ€๋ถ„์˜ PI ์˜์—ญ๊ณผ ICT ์‚ฐ์—…์˜ ISP ์˜์—ญ์œผ๋กœ ์Šค๋ฉฐ๋“ค์–ด๋ฒ„๋ ธ๋‹ค. ๋ฌผ๋ก , BPR์˜ ์œ ํ˜• II, III๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๋ณ„๋„๋กœ ์‚ฌ์—…์ด ๋‚˜์˜ค๊ธฐ๋„ ํ•˜์ง€๋งŒ ์ด ์—ญ์‹œ ๊ตฌ์กฐ์กฐ์ • ์ปจ์„คํŒ…์ด ์ปค๋ฒ„ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ํ•œํŽธ, ISP๋Š” Figure IV-45์™€ ๊ฐ™์ด BPR์„ ์ˆ˜์šฉํ•จ์œผ๋กœ์จ ์ž‘์—…์˜ ๋…ผ๋ฆฌ์„ฑ์ด๋‚˜ ํšจ๊ณผ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ง€๊ธˆ์ด์•ผ ๋ณ„๊ฑฐ ์•„๋‹Œ ๋“ฏ ์ด์•ผ๊ธฐํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ 90๋…„๋Œ€ ์ดˆ๋ฐ˜ ๊ธ‰์†ํ•œ IT์˜ ๋ฐœ๋‹ฌ๊ณผ ๋”๋ถˆ์–ด ๊ธฐ์กด ์ปจ์„คํŒ…์˜ ํฐ ์‚ฌ์—… ์˜์—ญ์ด์—ˆ๋˜ BPR์ด ISP๋กœ ํก์ˆ˜๋˜์–ด ์ž‘์—…์ด ์ง„ํ–‰๋  ๋•Œ๋Š” ํฐ ์ถฉ๊ฒฉ์ด์—ˆ๋‹ค. ์ผ๊ฑฐ๋ฆฌ๊ฐ€ ํ•˜๋‚˜ ์—†์–ด์ง€๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ํšจ๊ณผ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์‚ฌ์‹ค BPR์€ ๊ฒฝ์˜ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์ด ๊ตฌ์กฐ์กฐ์ •์ด๋‚˜ ๊ฒฝ์˜ํ˜์‹  ๋ฐฉ๋ฒ•๋ก ์œผ๋กœ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜์˜€๊ณ , ISP๋Š” ํ”„๋กœ์„ธ์Šค ์ปจ์„คํŒ…์˜ ๋ฒ”์ฃผ์—์„œ IT ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์ด ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋˜ ๋ฐฉ๋ฒ•๋ก ์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. Part I์—์„œ ์ด๋ฏธ ์–ธ๊ธ‰ํ•˜์˜€์ง€๋งŒ ์ปจ์„คํŒ… ์‚ฌ์—… ์˜์—ญ์˜ ๊ฒฝ๊ณ„๊ฐ€ ๋ฌด๋„ˆ์ง€๋ฉด์„œ ๋ฐœ์ƒํ•œ ์ผ ์ค‘ ํ•˜๋‚˜๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. Figure IV-45. BPR๊ณผ ISP์˜ ์—ฐ๊ณ„ Break #20. ์ปจ์„คํŒ… ์„œ๋น„์Šค์˜ ๋ถ„ํ™” Figure IV-46. ์ปจ์„คํŒ… ์„œ๋น„์Šค ๊ตฌ๋ถ„ ์ปจ์„คํŒ… ์„œ๋น„์Šค์— ๋Œ€ํ•œ ๊ตฌ๋ถ„์€ Part I์—์„œ ์ด๋ฏธ ๋‹ค๋ฃจ์—ˆ๋Š”๋ฐ Figure IV-46๊ณผ ๊ฐ™์ด ์ „๋žต(Strategy), ์ „์ˆ (Tactics), ์—…๋ฌด(Process), ์ž์›(Resources), ๋ณ€ํ™”(Change)๋กœ ๋‚˜๋ˆ„์–ด ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด์ค‘ ๊ฐ•์กฐ๋˜๋Š” ์‚ฌ์•ˆ์— ๋Œ€ํ•ด ์ปจ์„คํŒ… ์†์„ฑ์ด ๊ฒฐ์ •๋˜๊ณ  ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ๊ทธ๊ฒƒ์„ ์˜คํผ๋ง(Offering)์œผ๋กœ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋ฅผ 6๊ฐ€์ง€ ์œ ํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ ์ด์ค‘ ์˜…์€ ํšŒ์ƒ‰ ๋ถ€๋ถ„์ด ISP์—์„œ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์ด๋‹ค. Strategic Business Planning : ๊ณ ๊ฐ์ด ์ „๋ฐ˜์ ์ธ ์‚ฌ์—…์ „๋žต ์ˆ˜๋ฆฝ์„ ์š”๊ตฌํ•จ. (์˜ˆ. ๋””์ง€ํ„ธ ๋น„์ฆˆ๋‹ˆ์Šค ์ „๋žต) Business Information Planning : ๊ณ ๊ฐ์ด ์ „๋žต๊ณผ ๋ชฉํ‘œ๋Š” ์ •์˜ํ•˜์˜€์œผ๋‚˜ ๋ชฉํ‘œ ๋‹ฌ์„ฑ์„ ์œ„ํ•œ ๊ณผ์ œ๋“ค์ด ์šฐ์„ ์ˆœ์œ„๋‚˜ Value๋ฅผ ์–ด๋–ป๊ฒŒ ์ •๋ฆฌํ•ด์•ผ ํ•˜๋Š”์ง€ ๋ชจ๋ฆ„ Business Process Reengineering : ๊ณ ๊ฐ์ด ์กฐ์ง ๋‚ด ๊ธ‰๊ฒฉํ•œ ๋ณ€ํ™”๋ฅผ ์‹คํ–‰์— ์˜ฎ๊ธธ ํ•„์š”๊ฐ€ ์žˆ์œผ๋‚˜ ์™ธ๋ถ€์—์„œ ๋น„์ฆˆ๋‹ˆ์Šค ํ”„๋กœ์„ธ์Šค๋ฅผ ์žฌ์„ค๊ณ„ํ•˜๊ณ  ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์›€์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ Business Process Improvement : ๊ณ ๊ฐ์€ ์–ด๋Š ํ”„๋กœ์„ธ์Šค๊ฐ€ ์žฌ์„ค๊ณ„๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ๋ช…ํ™•ํžˆ ์•Œ๊ณ  ์žˆ์ง€๋งŒ, ์™ธ๋ถ€ ์ „๋ฌธ๊ฐ€๊ฐ€ ์žฌ์„ค๊ณ„์™€ ์ดํ–‰์— ๋„์›€์„ ์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ Combined, Company-wide, Integration Planning and Process Reengineering : ๊ณ ๊ฐ์ด ๋น„์ฆˆ๋‹ˆ์Šค ์ƒ์˜ ๊ธ‰๊ฒฉํ•œ ๋ณ€ํ™”๋ฅผ ํ•„์š”๋กœ ํ•˜๋‚˜ ์–ด๋””์— ์ดˆ์ ์„ ๋งž์ถ”์–ด์•ผ ํ•˜๊ณ , ์–ด๋–ป๊ฒŒ ๊ฐ€์žฅ ํฐ ํšจ๊ณผ๋ฅผ ์–ป์„ ์˜์—ญ์„ ํŒŒ์•…ํ• ์ง€ ์ž์‹ ์ด ์—†์œผ๋ฉฐ, ์™ธ๋ถ€ ์ „๋ฌธ๊ฐ€๊ฐ€ ํ•„์š”ํ•œ ๋ณ€ํ™” ํ”„๋กœ๊ทธ๋žจ์„ ์„ค๊ณ„ํ•˜๊ณ  ์‹คํ–‰ํ•ด ์ฃผ๊ธฐ๋ฅผ ์›ํ•˜๋Š” ๊ฒฝ์šฐ Information Technology Planning : ๊ณ ๊ฐ์ด ์ •๋ณด๊ธฐ์ˆ ์ด๋‚˜ ๋‹ค๋ฅธ ์ž์›๋“ค์ด ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š”์ง€ ์™ธ๋ถ€ ์ „๋ฌธ๊ฐ€์—๊ฒŒ ์ง„๋‹จ๋ฐ›๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ ์ข…์ข… ์ •๋ณด์ „๋žต๊ณผ ๊ณ„ํš์„ ํ•จ๊ป˜ ํ•„์š”๋กœ ํ•จ 17.3 BPR/ISP ํ…Œ์ผ๋Ÿฌ๋ง ๋ชจ๋“  ๋ฐฉ๋ฒ•๋ก ์ด ๊ทธ๋ ‡์ง€๋งŒ ํ”„๋กœ์ ํŠธ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋ฐฉ๋ฒ•๋ก ์„ ์žฌ๋‹จํ•˜๋Š” ํ…Œ์ผ๋Ÿฌ๋ง(Tailoring)์€ ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ์˜ ์œ ์—ฐํ•จ๊ณผ ์ปจ์„คํŒ… PM์˜ ์œ ๋Šฅํ•จ์„ ๋‚˜ํƒ€๋‚ด์ค€๋‹ค. 17.3์—์„œ ์†Œ๊ฐœํ•˜๋Š” BPR/ISP ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๊ณผ๊ฑฐ ์ €์ž๊ฐ€ ๊ฒฝํ—˜ํ–ˆ๋˜ ๊ฒƒ์„ ์ˆ˜์ •ํ•˜์—ฌ ๋‚˜ํƒ€๋‚ด ๋ณธ ๊ฒƒ์ด๋‹ค.[3] ์ฒซ ๋ฒˆ์งธ, Figure IV-47์€ ์ •๋ณด์ „๋žต๊ณ„ํš์˜ ํ๋ฆ„์— ์ค‘์ ์„ ๋‘๊ณ  ์•„์ด์ฝ˜๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์ „์ฒด์ ์ธ ํ”„๋กœ์ ํŠธ ํ๋ฆ„์„ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด๋‹ค. ์ด์Šˆ ๋ถ„์„ 150๊ฑด, ๊ฐœ์„  ๊ธฐํšŒ ๋„์ถœ 42๊ฑด, 14๋Œ€ ๊ฐœ์„ ๊ณผ์ œ ์ •์˜ ๋“ฑ ๋„์ถœ๋œ ์‹œ์‚ฌ์ ๊ณผ ์„ฑ๊ณผ๋“ค์„ ์ •๋Ÿ‰ํ™”ํ•จ์œผ๋กœ์จ ์„ฑ๊ณผ๋ฅผ ๊ฐ€์‹œ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•˜์˜€๋‹ค. Figure IV-47. A ๊ธฐ์—… ์ •๋ณดํ™” ์ „๋žต๊ณ„ํš ํ”„๋ ˆ์ž„์›Œํฌ ๋‘ ๋ฒˆ์งธ, Figure IV-48์€ B ๊ธฐ์—…์—์„œ ์ˆ˜ํ–‰ํ–ˆ๋˜ BPR/ISP ํ”„๋ ˆ์ž„์›Œํฌ์ธ๋ฐ ํŠน์ดํ•œ ์ ์€ ๋ฏธ๋ž˜ ์ •๋ณดํ™” ๋ชจ๋ธ์˜ ์„ค๊ณ„ ๋ถ€๋ถ„์—์„œ EA ๊ด€์ ์„ ๋„์ž…ํ–ˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ISP์—์„œ ๋ฏธ๋ž˜ ๋ชจ๋ธ ์„ค๊ณ„ ๊ด€์ ์€ ํ•˜๋“œ์›จ์–ด, ๋„คํŠธ์›Œํฌ, ์†Œํ”„ํŠธ์›จ์–ด ๋˜๋Š” ์ธํ”„๋ผ ์•„ํ‚คํ…์ฒ˜, ์†Œํ”„ํŠธ์›จ์–ด ์•„ํ‚คํ…์ฒ˜ ๋“ฑ์˜ ๊ตฌ๋ถ„์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜์˜€์œผ๋‚˜ 2000๋…„๋Œ€ ์ค‘๋ฐ˜ ์ดํ›„ 2010๋…„ ์ดˆ๋ฐ˜๊นŒ์ง€๋Š” ๊ทธ ๋ถ€๋ถ„์„ ์—…๋ฌด(Business Architecture: BA), ์‘์šฉ ์†Œํ”„ํŠธ์›จ์–ด ์•„ํ‚คํ…์ฒ˜(Application Architecture: AA), ๋ฐ์ดํ„ฐ ์•„ํ‚คํ…์ฒ˜(Data Architecture: DA), ๊ธฐ์ˆ  ์•„ํ‚คํ…์ฒ˜(Technical Architecture: TA)์˜ 4๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ฐ ์ˆ˜์ค€๋ณ„๋กœ ์ƒ์„ธํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๊ณ ์ž ํ•˜์˜€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ˜„์‹ค์ ์œผ๋กœ ๊ทธ ์‚ฌ์ƒ๋Œ€๋กœ ์‹œ์Šคํ…œ์„ ๊ตฌํ˜„ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ์ผ์ด ๋…ธ๋ ฅ(Efforts) ๋Œ€๋น„ ํฐ ์„ฑ๊ณผ๋ฅผ ๊ฑฐ๋‘์ง€ ๋ชปํ•˜์˜€๊ณ  ๋ฒˆ๊ฑฐ๋กœ์›Œ ์ง€๊ธˆ์€ ๊ฑฐ์˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. Figure IV-48์€ ๊ธฐ์ˆ  ์•„ํ‚คํ…์ฒ˜ ์ค‘ ๋ณด์•ˆ ๋ถ€๋ถ„์ด ์ค‘์š”ํ•˜๊ฒŒ ๋ถ€๊ฐ๋˜์–ด ๊ทธ ๋ถ€๋ถ„์„ ๊ตฌ๋ถ„ํ•œ ๊ฒƒ์ด๋‹ค. ์ „๋ฐ˜์ ์ธ ํ๋ฆ„์€ ํ™˜๊ฒฝ๋ถ„์„, ์—…๋ฌด ๋ฐ ๊ธฐ์ˆ  ํ˜„ํ™ฉ ๋ถ„์„, ๊ฐœ์„ ๋ฐฉํ–ฅ ๋„์ถœ, ์—…๋ฌด ๋ฐ ์‹œ์Šคํ…œ ๋น„์ „ ์„ค์ •, ๊ฐ ์˜์—ญ๋ณ„ ์ƒ์„ธ ๊ณผ์ œ ๊ฐœ๋ฐœ, ๊ณผ์ œ ์‹คํ–‰๊ณ„ํš ์ˆ˜๋ฆฝ์œผ๋กœ BPR/ISP์˜ ์ฒด๊ณ„๋ฅผ ์ค€์ˆ˜ํ•˜๊ณ  ์žˆ๋‹ค. ๋˜, ํ•œ ๊ฐ€์ง€ ํŠน์ดํ•œ ์ ์€ ๋ณ€ํ™”๊ด€๋ฆฌ ๋ฐฉ์•ˆ์— ๋Œ€ํ•œ ์˜์—ญ์ด ์žˆ๋Š”๋ฐ ๋ณ€ํ™”๊ด€๋ฆฌ ์ปจ์„คํŒ…์˜ ์ฃผ์ œ๊ฐ€ ์žˆ์„ ์ •๋„๋กœ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ์ „์ฒด ๊ณผ์ œ ์ˆ˜ํ–‰์˜ ์œ ์—ฐ์„ฑ์„ ์œ„ํ•ด ์‹ค์ œ ์‹คํ–‰ํ•˜๋ฉด์„œ ๋ˆ„๊ฐ€ ์–ด๋–ป๊ฒŒ ์กฐ์œจํ•˜๊ฒ ๋‹ค๋Š” ๋‚ด์šฉ์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. Figure IV-48. B ๊ธฐ์—… BPR/ISP ํ”„๋ ˆ์ž„์›Œํฌ ์„ธ ๋ฒˆ์งธ, Figure IV-49์€ C ์ •๋ถ€๊ธฐ๊ด€์—์„œ ์ˆ˜ํ–‰ํ–ˆ๋˜ BPR/ISP ํ”„๋ ˆ์ž„์›Œํฌ์ธ๋ฐ ํŠน์ดํ•œ ์ ์€ IT ๊ฑฐ๋ฒ„๋„Œ์Šค(IT Governance)์™€ ์˜ˆ๋น„ํƒ€๋‹น์„ฑ ์กฐ์‚ฌ๊ฐ€ ๊ฐ™์ด ๋ณ‘ํ–‰๋˜์—ˆ๋‹ค. ๊ณ ๊ฐ์€ ์ฐจ์„ธ๋Œ€ ์‹œ์Šคํ…œ์„ ๊ธฐํšํ•˜๋ฉด์„œ IT ์ž์›๊ณผ ์ •๋ณด, ์กฐ์ง์„ ๊ธฐ๊ด€์˜ ๋ชฉํ‘œ์™€ ์—ฐ๊ณ„ํ•˜์—ฌ ๋ณด๋‹ค ๋ช…ํ™•ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋„์ž…์„ ๊ณ ๋ คํ•˜์˜€๊ณ  BPR/ISP์—์„œ ๊ทธ๊ฒƒ์„ ๊ฐ™์ด ๊ณ ๋ คํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ๋˜ํ•œ, ์ „๊ตญ ๋‹จ์œ„์˜ ์‹œ์Šคํ…œ์„ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋Œ€๊ทœ๋ชจ ํˆฌ์ž๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ ์„ ๊ณ ๋ คํ•˜์—ฌ ์‹œ์Šคํ…œ ๊ตฌ์ถ•์˜ ROI๊ฐ€ ์˜๋ฏธ ์žˆ๋Š”์ง€ ๊ฐ™์ด ์ ๊ฒ€ํ•ด ๋ณด๊ธฐ๋กœ ํ•˜์˜€์œผ๋ฉฐ ๊ทธ ๊ณผ์ •์œผ๋กœ ๊ตญ๊ฐ€ ์žฌ์ • ๊ธˆ์•ก์ด ๋งŽ์•„์„œ ์˜ˆ๋น„ํƒ€๋‹น์„ฑ ์กฐ์‚ฌ๋„ ์ˆ˜ํ–‰ํ•˜์˜€๋‹ค. Figure IV-49์„ ๋ณด๋ฉด ๊ทธ๋Ÿฐ ์ธก๋ฉด์—์„œ IT ๊ฑฐ๋ฒ„๋„Œ์Šค๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด IT ํ”„๋กœ์„ธ์Šค ๋ฐ ์ •๋ณดํ™” ์กฐ์ง์— ๋Œ€ํ•œ ํ˜„ํ™ฉ ๋ถ„์„์„ ์‹ฌ๋„ ์žˆ๊ฒŒ ์ˆ˜ํ–‰ํ•˜์˜€์œผ๋ฉฐ BPR/ISP์—์„œ ๋„์ถœ๋˜๋Š” ๊ฒฐ๊ณผ๋ณด๋‹ค ์ƒ์„ธํ•œ ์ˆ˜์ค€์œผ๋กœ ๋ฏธ๋ž˜ ๋ชจ๋ธ์— ๋ฐ˜์˜ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ๋˜ํ•œ, ์ •๋ณด์‹œ์Šคํ…œ ๊ตฌ์ถ•์„ ์œ„ํ•œ ์˜ˆ์‚ฐ ์ˆ˜๋ฆฝ๊ณผ ๋”๋ถˆ์ด ROI ๋ถ„์„์ด ๋ณ‘ํ–‰ ์ˆ˜ํ–‰๋˜์—ˆ๋‹ค. Figure IV-49. C ์ •๋ถ€๊ธฐ๊ด€ BPR/ISP ํ”„๋ ˆ์ž„์›Œํฌ ์„ธ ๊ฐœ์˜ BPR/ISP ํ”„๋ ˆ์ž„์›Œํฌ ๋ชจ๋‘๊ฐ€ ํ•˜๋‚˜์˜ ๋ฐฉ๋ฒ•๋ก ์„ ํ…Œ์ผ๋Ÿฌ๋ง ํ•œ ๊ฒƒ์ด๋‹ค. ๋ฐฉ๋ฒ•๋ก ์„ ์ฒ˜์Œ ์ ‘ํ•˜๊ฑฐ๋‚˜ ์ž˜ ๋ชจ๋ฅด๋Š” ์‚ฌ๋žŒ๋“ค์€ ์œ ์—ฐํ•œ ๋ฐฉ๋ฒ•๋ก ์„ ๋ณด๊ณ  ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•ด์„œ ์ด๊ฒŒ ๊ฐ€์ด๋“œ๊ฐ€ ๋  ๊ฒƒ์ธ๊ฐ€ ์˜์‹ฌ์„ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋˜์ง€๋งŒ ์‹ค์ œ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๋ฉด ๊ทธ ๊ฐ•๋ ฅํ•œ ํšจ๊ณผ๋ฅผ ์ฒดํ—˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ €์ž๋„ ์ €๋ช…ํ•œ ์™ธ๊ตญ๊ณ„ ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ๋ฐฉ๋ฒ•๋ก ์„ ํฌํ•จํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ณ  ๋ฒค์น˜๋งˆํ‚นํ•ด๋ณด์•˜๋Š”๋ฐ ์ €์ž๊ฐ€ ์ปจ์„คํŒ… ํ•  ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ ๊ฒƒ์ด ๊ฐ€์žฅ ์œ ์—ฐํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹์•˜๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ Part II์—์„œ ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ ์ฒด๊ณ„์™€ ๋ฌธ์ œ ํ•ด๊ฒฐ๊ธฐ๋ฒ•, ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ์Šคํ‚ฌ ๋“ฑ ์ปจ์„คํŒ… ์Šคํ‚ฌ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์•˜๊ณ , Part III์—์„œ ์ปจ์„คํŒ… ์ดํ–‰์— ํ•„์š”ํ•œ ๋‹ค์–‘ํ•œ ์ปจ์„คํŒ… ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์œผ๋ฉฐ, Part IV์—์„œ๋Š” ์ปจ์„คํŒ… ์Šคํ‚ฌ ๋ฐ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•์ด ์•„์šฐ๋Ÿฌ์ง„ ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. ์ด๊ฒƒ๋“ค์„ ๊ธฐ๋ฐ˜์œผ๋กœ Part V์—์„œ๋Š” ์ปจ์„คํŒ… ์‚ฌ์—…์€ ์–ด๋–ป๊ฒŒ ๊ฐœ๋ฐœํ•˜๋Š”์ง€ ๊ทธ๋ฆฌ๊ณ  ์ปจ์„คํŒ… ์ดํ–‰์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. [1] Business Process Reengineering. ๊ธฐ์—…์˜ ์—…๋ฌด ํ”„๋กœ์„ธ์Šค๋ฅผ ์ž๋™ํ™” ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ์„ /ํ˜์‹ ํ•˜๋Š” ๊ฒƒ [2] Information Strategy Planning. ์ •๋ณด์ „๋žต๊ณ„ํš [3] ์ปจ์„คํŒ… ์‚ฐ์ถœ๋ฌผ์€ ๋Œ€๋ถ€๋ถ„ ๋Œ€์™ธ๋น„์ด๋ฏ€๋กœ ์™ธ๋ถ€ ๊ณต๊ฐœ ๋•Œ์—์„œ๋Š” ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์ด ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค. ๋ณธ์„œ์—์„œ ์†Œ๊ฐœ๋˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์Šฌ๋ผ์ด๋“œ๋„ ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์„ ์ถฉ๋ถ„ํžˆ ๊ณ ๋ คํ•˜์—ฌ ์ƒ๋žตํ•˜๊ณ  ์ˆ˜์ •ํ•œ ๊ฒƒ์ด๋‹ค 170 PART V. ์ปจ์„คํŒ… ์‚ฌ์—… ๊ฐœ๋ฐœ ๋ฐ ์ดํ–‰ Part II์—์„œ ์ปจ์„คํŒ… ๊ธ€์“ฐ๊ธฐ์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์‚ฌํ•ญ๋“ค์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๊ณ , Part III์—์„œ ๋‹ค์–‘ํ•œ ์ปจ์„คํŒ… ๊ธฐ๋ฒ•๋“ค์— ๋Œ€ํ•ด์„œ๋„ ์•Œ์•„๋ณด์•˜๋‹ค. Part IV์—์„œ๋Š” ๊ฐ€์žฅ ๋งŽ์ด ํ™œ์šฉ๋˜๋Š” ์ปจ์„คํŒ… ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•ด์„œ๋„ ์•Œ์•„๋ณด์•˜๋‹ค. ์ด ๋ชจ๋“  ๊ฒƒ์ด ์‚ฌ์‹ค Part V์—์„œ ๋‹ค๋ฃฐ ์ปจ์„คํŒ… ์‚ฌ์—…๊ฐœ๋ฐœ ๋ฐ ์ดํ–‰์„ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. ์ปจ์„คํŒ…๋„ ๋น„์ฆˆ๋‹ˆ์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜์—…๋„ ํ•„์š”ํ•˜๊ณ  ๋‹ค๋ฅธ ์‚ฌ์—…์ฒ˜๋Ÿผ ์‚ฌ์—… ๋ฐœ๊ตด ๊ณผ์ •์—์„œ ๊ณ ๋ฏผํ•ด์•ผ ํ•  ๊ฒƒ๋“ค์ด ๋งŽ๋‹ค. ์ปจ์„คํŒ… ์‚ฌ์—…์€ B2B ์‚ฌ์—…์— ์†ํ•˜๋Š”๋ฐ B2B ์‚ฌ์—…์˜ ์˜์—… ๋ฐฉ์‹[1] ์ค‘ ๋ฐธ๋ฅ˜ ์„ธ์ผ์ฆˆ(Value Sales)๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. ๋ฐธ๋ฅ˜ ์„ธ์ผ์ฆˆ๋Š” ๊ณ ๊ฐ์˜ ์ด์Šˆ๋‚˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์ด ์ค‘์š”ํ•˜๋ฉฐ ๊ทธ๊ฒƒ๋“ค์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์—…์ž๋Š” ๊ณ ๊ฐ์—๊ฒŒ ์–ด๋–ค ๊ฐ€์น˜(Value)๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ธ์ง€๊ฐ€ ์˜์—… ์„ฑ๊ณต์˜ ๊ด€๊ฑด์ด ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ๊ณ ๊ฐ์˜ ์ด์Šˆ๋‚˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ๊ณ ์œ ์˜ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ๋˜๋Š” ๋ฐฉ๋ฒ•๋ก ์ด ํ•„์š”ํ•˜๋ฉฐ ์ด๊ฒƒ์„ ์„ค๋ฃจ์…˜(Solution)๊ณผ ์—ฐ๊ณ„ํ•˜์—ฌ ์ œ๊ณตํ•œ๋‹ค. ์ตœ๊ทผ์—๋Š” IT์˜ ๋ฐœ๋‹ฌ๋กœ ์ธํ•ด ๊ฑฐ์˜ ๋ชจ๋“  B2B ๊ธฐ์—…๋“ค์€ ๊ทธ๋“ค์˜ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ์„ค๋ฃจ์…˜ ์‚ฌ์—…์œผ๋กœ ๋ณ€ํ™”์‹œํ‚ค๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ B2B ์˜์—…์ด๋ผ ํ•จ์€ ๊ฑฐ์˜ ์„ค๋ฃจ์…˜ ์˜์—…์ด๋ฉฐ, ์ปจ์„คํŒ… ์˜์—…๋„ ๋‹น์—ฐํžˆ ์ด๋Ÿฐ ๋ฒ”์ฃผ ๋˜๋Š” ๊ด€์ ์—์„œ ๊ณ ๋ คํ•ด์•ผ ํ•  ๊ฒƒ๋“ค์ด ์ฆ๊ฐ€ํ•˜๊ณ  ์žˆ๋‹ค. Part V์—์„œ๋Š” B2B ์‚ฌ์—…์œผ๋กœ์„œ ์ปจ์„คํŒ… ์‚ฌ์—…๊ฐœ๋ฐœ๊ณผ ๊ทธ ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. [1] B2B ์˜์—…์€ ์˜์—… ๋ฐฉ์‹(Sales Motion)์— ๋”ฐ๋ผ Volume Sales์™€ Value Sales๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. Volume Sales๋Š” ์ฑ„๋„์„ ํ™œ์šฉํ•ด์„œ ๋Œ€๋‹จ์œ„์˜ ์›์ž์žฌ๋‚˜ ๋ถ€ํ’ˆ์„ ์œ ํ†ตํ•˜๋Š” ํ˜•ํƒœ๊ฐ€ ๋งŽ์œผ๋ฉฐ, Value Sales๋Š” ์„ค๋ฃจ์…˜ ์‚ฌ์—…์˜ ํ˜•ํƒœ๋ฅผ ๋ ๊ณ  ์žˆ๋‹ค. 18. ์ปจ์„คํŒ… ์‚ฌ์—…๊ฐœ๋ฐœ ๊ฐœ์š” B2B ์‚ฌ์—…์€ ๋ฆฌ๋“œ[1]์˜ ์ƒ์„ฑ๊ณผ ๊ด€๋ฆฌ๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘๋œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. โ€˜๋ฆฌ๋“œ(lead)โ€™๋ผ๋Š” ๋‹จ์–ด๋Š” ํ•œ๊ธ€๋กœ ๊ทธ ๋‰˜์•™์Šค๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์ข€ ์–ด๋ ค์šด๋ฐ โ€˜๊ธฐ์—…์˜ ์ œํ’ˆ์„ ๊ตฌ๋งคํ•˜๊ณ ์ž ํ•˜๋Š” ๋‹ˆ์ฆˆ, ์˜๋„, ์˜ˆ์‚ฐ์„ ๊ฐ€์ง„ ๊ฐœ์ธ์ด๋‚˜ ์กฐ์ง์˜ ์‹ ์ƒ ์ •๋ณดโ€™ ์ •๋„๋กœ ๋ฒˆ์—ญ๋  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ๊ฑฐ์น ๊ฒŒ ์ด์•ผ๊ธฐํ•˜๋ฉด ๋ฆฌ๋“œ๋Š” โ€˜์—ฐ๋ฝ์ฒ˜(Contact)โ€™์ด๋‹ค. ์ปจ์„คํŒ…๋„ B2B ์‚ฌ์—…์ด๋ฏ€๋กœ ๋งˆ์ผ€ํŒ… ๋ฆฌ๋“œ(MQLs) [2]๋‚˜ ์„ธ์ผ์ฆˆ ๋ฆฌ๋“œ(SQLs) [3]๋ฅผ ๊ด€๋ฆฌํ•˜์—ฌ ๊ถ๊ทน์ ์œผ๋กœ ํŒŒ์ดํ”„๋ผ์ธ ์ฆ‰, ์‚ฌ์—…๊ธฐํšŒ๊ด€๋ฆฌ(Tier Management: TM)๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ๋Œ€๋‹จํžˆ ์ค‘์š”ํ•˜๋‹ค. ์ปจ์„คํŒ… ์‚ฌ์—…์€ ๋‹ค๋ฅธ B2B ์‚ฌ์—…์— ๋น„ํ•ด ๊ทœ๋ชจ๊ฐ€ ํฌ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํฐ ๊ฒƒ ํ•œ ๊ฑด(Big Deal)๋ณด๋‹ค๋Š” ์ž‘์€ ๋‹ค์–‘ํ•œ ์‚ฌ์—… ๊ธฐํšŒ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์‚ฌ์—…์˜ ์ง€์†์„ฑ์ด๋‚˜ ์—ฐ์†์„ฑ ๊ด€์ ์—์„œ ์ข‹๋‹ค. Figure V-1์€ ํ”ํžˆ ๋งํ•˜๋Š” โ€˜์„ธ์ผ์ฆˆ ๊น”๋•Œ๊ธฐโ€™์ธ๋ฐ ๋‹ค์–‘ํ•œ ์‚ฌ์—… ๋ฆฌ๋“œ๋“ค์ด ์ •๋ณด๊ฐ€ ๋ณด์™„๋˜๋ฉด์„œ ์˜์—… ๊ธฐํšŒ๋กœ ์ „ํ™˜๋˜๊ณ  ์ˆ˜์ฃผ๋˜๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ๋‹ค. ๋ณดํ†ต ํ•˜๋‚˜์˜ ์‚ฌ์—…์„ ์ˆ˜์ฃผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 4~5๋ฐฐ์ˆ˜ ๊ธฐํšŒ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ด ์žฅ์—์„œ๋Š” ์ปจ์„คํŒ… ์‚ฌ์—…๊ฐœ๋ฐœ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž. Figure V-1. ์„ธ์ผ์ฆˆ ๊น”๋•Œ๊ธฐ(Sales Funnel) 18.1 ์ปจ์„คํŒ… ์‚ฌ์—… ๋ฆฌ๋“œ ๊ด€๋ฆฌ ์ปจ์„คํŒ… ๊ธฐ์—…์ด ์‹œ์žฅ์ด๋‚˜ ๊ณ ๊ฐ๋“ค์—๊ฒŒ ์•Œ๋ ค์ง€๊ฒŒ ๋˜๋Š” ๊ฒƒ์€ ๊ทธ๋“ค์˜ ์ƒ๊ฐ(Thoughts) ์ด ์‹œ์žฅ์„ ์„ ๋„ํ•˜๊ณ , ๊ทธ๊ฒƒ์ด ํด๋ผ์ด์–ธํŠธ์˜ ์‚ฌ์—…์— ๋„์›€์ด ๋œ๋‹ค๊ณ  ๋Š๋‚„ ๋•Œ์ด๋‹ค. ์ฆ‰, ์ปจ์„คํŒ… ๊ธฐ์—… ์ž…์žฅ์—์„œ๋Š” ๊ทธ๋“ค์˜ ์ƒ๊ฐ์„ ์‹œ์žฅ๊ณผ ๊ณ ๊ฐ์—๊ฒŒ ํšจ๊ณผ์ ์œผ๋กœ ์•Œ๋ฆฌ๋Š” ์ตœ๊ณ ์˜ ๋งˆ์ผ€ํŒ… ๋ฐฉ์•ˆ์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค. Table V-1์€ ์˜จ๋ผ์ธ ๋˜๋Š” ์˜คํ”„๋ผ์ธ ์ฑ„๋„์„ ์ด์šฉํ•˜์—ฌ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์ด ํŽผ์น˜๋Š” ๋งˆ์ผ€ํŒ… ํ™œ๋™, ๋‚˜์•„๊ฐ€์„œ๋Š” ๋ธŒ๋žœ๋”ฉ ํ™œ๋™์„ ์ •๋ฆฌํ•ด ๋ณธ ๊ฒƒ์ด๋‹ค. Table V-1. ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ๋ธŒ๋žœ๋“œ ์ œ๊ณ ๋ฅผ ์œ„ํ•œ ๋งˆ์ผ€ํŒ… ํ™œ๋™ ์ด๋Š” ์„ค๋ฃจ์…˜ ์‚ฌ์—…์„ ์ง€ํ–ฅํ•˜๋Š” ๋‹ค๋ฅธ B2B ๊ธฐ์—…๋“ค๊ณผ ๋ณ„๋ฐ˜ ๋‹ค๋ฅผ ๊ฒƒ์ด ์—†๋‹ค. ์„ค๋ฃจ์…˜ ๊ธฐ์—…๋“ค์€ ๊ทธ๋“ค์˜ ์ œํ’ˆ์„ ์‚ฌ์ „์— ์ผ๋ถ€ ์‚ฌ์šฉํ•ด ๋ณธ๋‹ค๋“ ์ง€ ํ•˜๋Š” ํ–‰์œ„๊ฐ€ ์ถ”๊ฐ€๋  ๋ฟ์ด๋‹ค. ๋ธŒ๋žœ๋“œ ์ธ์ง€๋„(Brand Recognition) ์ธก๋ฉด์—์„œ ํšจ๊ณผ์ ์ธ ๋งˆ์ผ€ํŒ… ๋ฐฉ์•ˆ์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค. ์ปจ์„คํŒ… ์‚ฌ์—…์˜ ์ฃผ๊ฐ€๊ฐ€ ํ•œ์ฐฝ ์ข‹๋˜ ์‹œ์ ˆ์—๋Š” ์ด๋Ÿฐ ๋ธŒ๋žœ๋”ฉ ํ™œ๋™์ด ์—†์ด๋„ ๊ณ ๊ฐ์ด ์ง์ ‘ ์—ฐ๋ฝํ•ด์˜จ ์ ๋„ ๋งŽ์•„์„œ, ๋‹ค๋ฅธ B2B ์‚ฌ์—…์˜ ์˜์—…๋Œ€ํ‘œ๋“ค์ด ๊ณ ๊ฐ๋“ค์„ ์ฐพ์•„๊ฐ€๋Š” ์˜์—…์„ ํ•˜๋˜ ๊ฒƒ๊ณผ ๋น„๊ตํ•˜์—ฌ ์ปจ์„คํŒ… ์˜์—…์€ ์ƒ์œ„ ์ˆ˜์ค€์˜ ๊ณ ๊ธ‰ ์˜์—…์ด๋ผ ํ‰ํ•˜๊ธฐ๋„ ํ•˜์˜€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฒฝ์ œ ๋ถˆํ™ฉ์ธ ์š”์ฆ˜์€ ์ด๋ฏธ ๊ตฌ์กฐํ™”๋œ ์ปจ์„คํŒ… ์‹œ์žฅ์— ์ƒˆ๋กญ๊ฒŒ ์ง„์ž…ํ•˜๋Š” ๊ฒƒ ์ž์ฒด๊ฐ€ ์‰ฝ์ง€ ์•Š๊ณ , ์ค‘์†Œ ๊ทœ๋ชจ์˜ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์ด ์ดํ•ฉ์ง‘์‚ฐํ•˜์—ฌ ์‹ ๊ทœ ๊ธฐ์—…์„ ๋งŒ๋“ค์–ด๋„ ์‹ค์ (references)์„ ๊ผผ๊ผผํžˆ ์ฑ™๊ฒจ ๋ณด๋Š” ๊ณ ๊ฐ๋“ค๋„ ๋งŽ์•„์ ธ์„œ ์ด๋ฆ„์„ ์•Œ๋ฆฌ๋Š” ์ผ์ด ๊ฒฐ์ฝ” ์‰ฝ์ง€ ์•Š๋‹ค. ์–ด์จŒ๋“  ๋งˆ์ผ€ํŒ… ๋ฆฌ๋“œ(MQLs)๋ฅผ ํ™•๋ณดํ•˜๊ณ , ์„ธ์ผ์ฆˆ ๋ฆฌ๋“œ(SQLs)๋กœ ์ „ํ™˜ํ•˜์—ฌ ์ œ์•ˆ ๋ฐ ์ž…์ฐฐ์„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์€ ๋‹ค๋ฅธ B2B ์‚ฌ์—…์˜ ์˜์—…ํ™œ๋™๊ณผ ๋™์ผํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ปจ์„คํ„ดํŠธ๋„ ์˜์—…ํ•ด์•ผ ํ• ์ง€ ๋ชฐ๋ž์Šต๋‹ˆ๋‹ค. ๊ณผ๊ฑฐ์— ์ž…์‚ฌํ•œ์ง€ ์–ผ๋งˆ ๋˜์ง€ ์•Š์€ ์„ ์ž„ ์ปจ์„คํ„ดํŠธ๊ฐ€ ๋‚ด๊ฒŒ ํ–ˆ๋˜ ์ด์•ผ๊ธฐ์ด๋‹ค. ์‹œ๋‹ˆ์–ด ์ปจ์„คํ„ดํŠธ๋“ค(Senior Consultants)์ด๋‚˜ ํŒŒํŠธ๋„ˆ๋“ค(Partners)์€ ํ•˜๋Š” ์ผ์˜ 70% ์ด์ƒ์ด ์†Œ์œ„ ๋งํ•˜๋Š” โ€˜์˜์—…โ€™์ด๋‹ค. ์ €์ž๊ฐ€ ๊ฒฝํ—˜ํ•œ ์ปจ์„คํŒ… ์‚ฌ์—…์€ ๋…๋ฆฝ ์ปจ์„คํŒ… ๊ธฐ์—…์ด ์•„๋‹ˆ๋ผ IT ์„œ๋น„์Šค ๊ธฐ์—…์˜ ์ปจ์„คํŒ… ๋ถ€๋ฌธ์— ์†ํ•ด ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋Œ€์ ์œผ๋กœ ๋…๋ฆฝ ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ์ปจ์„คํ„ดํŠธ๋“ค์ด ๋Š๋ผ๋Š” ์‚ฌ์—… ์ˆ˜์ฃผ์— ๋Œ€ํ•œ ์••๋ฐ•๊ฐ์€ ์ƒ๋Œ€์ ์œผ๋กœ ๋œํ–ˆ์ง€๋งŒ[4] ์ปจ์„คํ„ดํŠธ๋ฅผ ์•Œ๋ฆฌ๊ณ  ์ปจ์„คํŒ… ์˜คํผ๋ง์„ ์†Œ๊ฐœํ•˜๋Š” ์ผ์€ ๋™์ผํ•˜์˜€๋‹ค. โ€˜ํ›Œ๋ฅญํ•œ ์˜์—…๋Œ€ํ‘œ(Sales Rep.)๋Š” ํŠน๋ณ„ํ•œ ๋„๊ตฌ๊ฐ€ ํ•„์š” ์—†๋‹คโ€™๋Š” ๋ง์€ ๊ฑฐ์ง“์ด๋‹ค. ํ›Œ๋ฅญํ•œ ์˜์—…๋Œ€ํ‘œ๊ฐ€ ๋˜๋ฉด์„œ ์ด๋ฏธ ์ž์‹ ๋„ ๋ชจ๋ฅด๋Š” ์‚ฌ์ด์— ์ˆ˜๋งŽ์€ ๋„๊ตฌ(Tools)์˜ ๋„์›€์„ ๋ฐ›๊ณ  ๊ทธ ์ž์‹ ์ด ๊ทธ๊ฒƒ์„ ํ™œ์šฉํ•˜๊ณ  ์žˆ๋‹ค. ์•„๋ž˜ ๋‚ด์šฉ์€ B2B ๋งˆ์ผ€ํŒ…์ด๋‚˜ B2B ์˜์—…์„ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ๋„๊ตฌ๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. โ€˜์—์ด ์ด๊ฒŒ ๋ฌด์Šจ ๋„๊ตฌ์•ผ?โ€™๋ผ๊ณ  ํ„ํ•˜ํ•ด๋ฒ„๋ฆด์ง€๋„ ๋ชจ๋ฅด์ง€๋งŒ ์ปจ์„คํ„ดํŠธ๋Š” ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋งˆ์ผ€ํŒ…๊ณผ ์˜์—…์„ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ๋ณธ์ธ์ด ๊ทธ๊ฒƒ์„ ๋ชจ๋‘ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— โ€˜๋ชจ๋‘ ๋‚ด๊ฐ€ ์ฑ™๊ธด๋‹คโ€™๋Š” ์ƒ๊ฐ์œผ๋กœ ์‚ดํŽด๋ณด๋ฉด ๋  ๊ฒƒ์ด๋‹ค. [B2B ๋งˆ์ผ€ํŒ… ๋„๊ตฌ] (1) ์›น ํŽ˜์ด์ง€ ์ธํ„ฐ๋„ท ์‹œ๋Œ€์— ์›นํŽ˜์ด์ง€๋Š” ํ•„์ˆ˜์ด๋‹ค. ๊ธฐ์—… ํ™ˆํŽ˜์ด์ง€๋Š” ์ปจ์„คํŒ… ๊ธฐ์—…์„ ์†Œ๊ฐœํ•˜๋Š” ์ •๋ณด๋ฅผ ๋น„๋กฏํ•˜์—ฌ ์‹ค์ , ์‚ฐ์—… ์ •๋ณด๋“ค์„ ๊ฐ™์ด ๊ธฐ์žฌํ•จ์œผ๋กœ์จ ๋ฆฌ๋“œ๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” ์ข‹์€ ๋„๊ตฌ๊ฐ€ ๋œ๋‹ค. ์ฝ˜ํ…์ธ ๊ฐ€ ๋งŽ๋‹ค๋ฉด ํŠน์ • ์ฃผ์ œ๋ฅผ ์ƒ์„ธํžˆ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋งˆ์ดํฌ๋กœ์‚ฌ์ดํŠธ(mircosite)๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋‹ค. (2) ํ™”์ดํŠธ ํŽ˜์ดํผ(White papers) ๋ฐœ๊ฐ„ ์ปจ์„คํ„ดํŠธ๋“ค์ด ์‹ค์ˆ˜ํ•˜๋Š” ๊ฒƒ ์ค‘ ํ•˜๋‚˜๊ฐ€ โ€˜๊ณ ๊ฐ์ด ์ž๊ธฐ๋ณด๋‹ค ํ•˜์ˆ˜โ€™๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ๋กœ ๋งŽ์€ ๊ณ ๊ฐ๋“ค์„ ๋งŒ๋‚˜๋ณด๋ฉด ๋Œ€๋ถ€๋ถ„์€ ํ•ด๋‹น ์‚ฐ์—…์ด๋‚˜ ์—…๋ฌด์˜ ์ „๋ฌธ๊ฐ€๋“ค์ด๋‹ค. ๋‹ค๋งŒ ๊ทธ๋“ค์€ ๊ทธ๋“ค ๋จธ๋ฆฌ ์†์ด๋‚˜ ํ˜„์žฅ์— ํŽผ์ณ์ง„ ์ง€์‹๊ณผ ๊ฒฝํ—˜๋“ค์„ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์—์„œ ๋งŽ์€ ์–ด๋ ค์›€์„ ๋Š๋ผ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ๋“ค์ด ์†ํ•œ ์‚ฐ์—… ๋™ํ–ฅ ์†Œ์‹์ด๋‚˜ ํŠธ๋ Œ๋“œ์— ๋Œ€ํ•œ ์†Œ๊ฐœ์™€ ํ•จ๊ป˜ ๊ทธ๋“ค์ด ๋ณด์œ ํ•œ ์ง€์‹๊ณผ ๊ฒฝํ—˜์„ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์ด๋“œ๋ฅผ ํ™”์ดํŠธ ํŽ˜์ดํผ (white paper)๋ฅผ ํ†ตํ•ด ์ œ๊ณตํ•ด ์ค€๋‹ค๋ฉด ์‚ฌ์—… ์ˆ˜์ฃผ์˜ 80% ์ด์ƒ์„ ํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ œ๋ชฉ์„ ๋‹จ ํ™”์ดํŠธ ํŽ˜์ดํผ๋“ค์€ ๊ณ ๊ฐ์˜ ๋ˆˆ๊ธธ์„ ๋Œ๊ฒŒ ๋˜์–ด ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜~๋ฅผ ํ•˜๋Š” ๋ฐฉ๋ฒ•โ€™, โ€˜์™œ ~๋ฅผ ํ•˜๋Š”๊ฐ€?โ€™, โ€˜XX์˜ ์‹ค์ˆ˜๋ฅผ ํ”ผํ•˜๋Š” 3๊ฐ€์ง€ ์š”์ธโ€™ ๋“ฑ ์ „๋žต์ ์ธ ์ œ๋ชฉ์„ ๊ฐ€์ง„ ํ™”์ดํŠธ ํŽ˜์ดํผ๋ฅผ ๋ฐœ๊ฐ„ํ•ด ๋ณด์ž. ๋ฌผ๋ก , ์–‘์งˆ์˜ ์ฝ˜ํ…์ธ ๋Š” ๊ธฐ๋ณธ์ด๋‹ค. (3) ๋ธ”๋กœ๊ทธ(Blog) ์šด์˜ ๋ธ”๋กœ๊ทธ๋Š” ํŠน์ • ์ฃผ์ œ์— ๋Œ€ํ•œ ์˜๊ฒฌ์„ ์›น(web) ์ƒ์—์„œ ๊ฐœ์ง„ํ•จ์œผ๋กœ์จ ๊ณ ๊ฐ๊ณผ ์Œ๋ฐฉํ–ฅ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์„ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ๋„๊ตฌ์ด๋‹ค. ๋•Œ๋•Œ๋กœ ๋ธ”๋กœ๊ทธ๋ฅผ ํ†ตํ•ด ๋งˆ์ผ€ํŒ… ๋ฆฌ๋“œ(MQLs)๋ฅผ ๋ฐœ๊ตดํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ๋ฐ”๋กœ ์˜์—… ๋‹จ๊ณ„ ๋กœ ์ „ํ™˜๋  ์ˆ˜๋„ ์žˆ๋‹ค. ๋˜, ๋ธ”๋กœ๊ทธ์™€ ์—ฐ๊ณ„๋œ ๋งํฌ(Link)๋ฅผ ์ž˜ ํ™œ์šฉํ•˜์—ฌ ๊ณ ๊ฐ์ด ์›ํ•˜๋Š” ๋‹ค๋ฅธ ์ •๋ณด๋ฅผ ์†Œ๊ฐœํ•˜๋Š” ์›น ํŽ˜์ด์ง€๋‚˜ ์ „์ž์ƒ๊ฑฐ๋ž˜ ํ”Œ๋žซํผ์— ์ข€ ๋” ๊ด€์‹ฌ๊ณผ ๊ธฐ๋Œ€๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌด์—‡๋ณด๋‹ค๋„ ๋ธ”๋กœ๊ทธ ์ž์ฒด์˜ ์ฝ˜ํ…์ธ ๊ฐ€ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค. ๋‹ค๋ฅธ ์†Œ์…œ๋ฏธ๋””์–ด(social media) ํ”Œ๋žซํผ์œผ๋กœ ์ „๋ฌธ๊ฐ€ ๊ทธ๋ฃน์„ ์œ„ํ•œ ๋งํฌ๋“œ ์ธ[5]์ด๋‚˜ ํŽ˜์ด์Šค๋ถ[6]๋„ ๋งค์šฐ ์œ ๋ช…ํ•œ๋ฐ ์งง์€ ๊ธ€์„ ๋นจ๋ฆฌ ๋ฐฐํฌํ•˜๋Š” ์šฉ๋„์™€ ๋น„๊ต์  ์žฅ๋ฌธ์˜ ์ฝ˜ํ…์ธ ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ™œ์šฉ ์šฉ๋„๋ฅผ ๋‹ค๋ฅด๊ฒŒ ๊ฐ€์ ธ๊ฐ€๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. (4) ์ธํฌ๊ทธ๋ž˜ํ”ฝ์Šค(Infographics) ๋‹ค์–‘ํ•œ ์ •๋Ÿ‰์  ์ •๋ณด๋ฅผ ๊ทธ๋ž˜ํ”ฝ ์ฒ˜๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. ์ž˜ ๋งŒ๋“ค์–ด์ง„ ์ธํฌ๊ทธ๋ž˜ํ”ฝ์Šค๋Š” ๊ทธ ํ™œ์šฉ๋„๊ฐ€ ๋งค์šฐ ๋†’์œผ๋ฉฐ ์‰ฝ๊ฒŒ ์ธ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งค์šฐ ์ง๊ด€์ ์ด๋ฏ€๋กœ ์ œํ’ˆ์˜ ํŠน์„ฑ์ด๋‚˜ ๊ธฐ์—…์˜ ํŠน์ง•์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ ์šฉํ•˜๋ฉฐ ์›นํŽ˜์ด์ง€๋‚˜ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ž๋ฃŒ์—์„œ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ปจ์„คํŒ… ์˜์—ญ์—์„œ๋Š” ์‚ฐ์—… ๋™ํ–ฅ ์†Œ๊ฐœ์— ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. (5) ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ž๋ฃŒ ๊ธฐ์—…์˜ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ์„ค๋ช…ํ•˜๋Š” ์ž˜ ๋งŒ๋“ค์–ด์ง„ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ž๋ฃŒ๋Š” ๋งˆ์ผ€ํŒ… ํ™œ๋™์— ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋‹ค. ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ธŒ๋กœ์Šˆ์–ด(Brochure)๋‚˜ ๋ฆฌํ”Œ๋ฆฟ(Leaflet) ๊ฐ™์€ ๊ฒƒ๋„ ๊ฐ™์€ ์šฉ๋„๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. (6) ๋ช…ํ•จ ์˜์—…๋Œ€ํ‘œ๋งŒ ๋ช…ํ•จ์ด ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ช…ํ•จ์ด ๊ทธ๋ ‡์ง€๋งŒ ๊ฐ€๋Šฅํ•˜๋ฉด ์ฐจ๋ณ„ํ™”๋˜๊ฒŒ ๋””์ž์ธํ•˜์—ฌ ๋‹ค์‹œ ํ•œ๋ฒˆ ์†์ด ๊ฐ€๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€๋ฐ ๊ธฐ์—… CI[7]์˜ ์˜ํ–ฅ๋„ ์žˆ๊ณ  ๋ช…ํ•จ ๋””์ž์ธ ๊ทธ ์ž์ฒด์˜ ํšจ๊ณผ๋Š” ์ปจ์„คํŒ… ์‚ฐ์—…์—์„œ๋Š” ์ข€ ๋œํ•œ ๊ฒƒ ๊ฐ™๋‹ค. ๋‹ค๋งŒ, ์ปจ์„คํ„ดํŠธ์˜ ๋ช…ํ•จ์€ ์ถฉ๋ถ„ํžˆ ์ „๋žต์ ์œผ๋กœ ํ™œ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ฌด๋ž˜๋„ ์ปจ์„คํ„ดํŠธ๋ผ๊ณ  ํ•˜๋ฉด ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ณด๊ฒŒ ๋œ๋‹ค. ์œ„์˜ ๊ฒƒ๋“ค์ด ๋งˆ์ผ€ํŒ… ๊ด€์ ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ๋“ค์ด๋ผ๋ฉด ์ด์ œ๋ถ€ํ„ฐ ์†Œ๊ฐœํ•  ๊ฒƒ๋“ค์€ ์˜์—… ์ฐจ์›์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์ด๋‹ค. [B2B ์˜์—… ๋„๊ตฌ] (1) ๊ธฐ์—… ์†Œ๊ฐœ ๋ฉ”์ผ ์ฒ˜์Œ ๊ณ ๊ฐ๊ณผ ์ ‘์ด‰ํ•˜๊ฒŒ ๋  ๋•Œ ์•ฝ๊ฐ„ ์ƒํˆฌ์ ์ธ ๋ฌธ๊ตฌ์™€ ํ•จ๊ป˜ ๋ณด๋‚ด์ง€๋Š” ๊ธฐ์—… ์†Œ๊ฐœ ๋ฉ”์ผ์€ ๊ณ ๊ฐ๋“ค๋กœ ํ•˜์—ฌ๊ธˆ ์ค€๋น„๋œ ์ž์„ธ ๋˜๋Š” ์ „๋ฌธ๊ฐ€์  ๋ชจ์Šต์„ ๊ธฐ๋Œ€ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ๋ฌผ๋ก , ์ง์ ‘ ๋งŒ๋‚˜์„œ ๊ทธ ๊ธฐ๋Œ€๊ฐ€ ๊นจ์ง€๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ๋„ ์žˆ๊ฒ ์ง€๋งŒ ๋งŒ๋‚˜๊ธฐ ๋ฉฐ์น  ์ „๋ถ€ํ„ฐ ๋งŒ๋‚จ์˜ ๋ชฉ์ ์ด๋‚˜ ๊ธฐ๋Œ€, ์ œ๊ณตํ•˜๋Š” ๊ฐ€์น˜์— ๋Œ€ํ•ด ์กฐ๊ธˆ์”ฉ ์ „ํ•ด์ฃผ๋Š” ๊ฒƒ์€ ์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค. (2) ๋‰ด์Šค๋ ˆํ„ฐ ์‹ ๊ทœ ๊ณ ๊ฐ์ด๋“  ๊ธฐ์กด ๊ณ ๊ฐ์ด๋“  ์ง€์†์ ์ธ ๋งŒ๋‚จ๊ณผ ์ ‘์ด‰์ด ์ค‘์š”ํ•˜๋‹ค. ๋‰ด์Šค๋ ˆํ„ฐ๋Š” ์ž์นซ ์ŠคํŒธ ๋ฉ”์ผ์ฒ˜๋Ÿผ ์ฒ˜๋ฆฌ๋  ์ˆ˜๋„ ์žˆ์œผ๋‚˜ ๊ธฐ์—…์˜ ์ƒํ™ฉ์„ ์•Œ๋ฆฌ๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์œผ๋กœ ๊พธ์ค€ํžˆ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ณ ๊ฐ์˜ ์ด๋ฉ”์ผ์„ ํ†ตํ•ด ๋‰ด์Šค ๋ ˆํ„ฐ๋ฅผ ๋ณด๋‚ธ๋‹ค๋Š” ๊ฒƒ์€ ์ ์–ด๋„ ๋งˆ์ผ€ํŒ… ๋ฆฌ๋“œ๋Š” ํ™•๋ณดํ•œ ์ƒํ™ฉ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ํ†ตํ•ด ์—ฐ๋ฝํ•ด์˜ค๋Š” ๊ณ ๊ฐ์€ ์„ธ์ผ์ฆˆ ๋ฆฌ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ๋ณด์•„๋„ ๋ฌด๋ฐฉํ•˜๋‹ค. (3) ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋งˆ์ผ€ํŒ… ๋„๊ตฌ๋กœ์„œ ๋‹จ์ˆœํžˆ ๊ณต์œ ๋˜๋Š” ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ž๋ฃŒ๊ฐ€ ์‹ค์ œ ์ปจ์„คํ„ดํŠธ(์ด๋•Œ ์ปจ์„คํ„ดํŠธ๋Š” ์˜์—…๋Œ€ํ‘œ์™€ ๊ฐ™๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค)๋ฅผ ํ†ตํ•ด ์‹ค์ œ ๋ฐœํ‘œ๋˜๋ฉด์„œ ๊ทธ ๋น›์„ ๋ฐœํ•œ๋‹ค. โ€˜๋ณด๋Š” ๊ฒƒ์ด ๋ฏฟ๋Š” ๊ฒƒ์ด๋‹ค(Seeing is believing)โ€™๋ผ๋Š” ๋ง์ฒ˜๋Ÿผ ์‹ค์ œ ๋‚ด์šฉ์„ ๋“ค์–ด๋ณด๋ฉด์„œ ๋” ๋งŽ์ด ์ดํ•ด๋˜๋Š” ๊ฒƒ์€ ๋‹น์—ฐํ•œ ์ผ์ด๋‹ค. ์˜์—…๋Œ€ํ‘œ๋ผ๋ฉด ๋ณธ์ธ์ด ๊ธฐ์—…๊ณผ ๋ณธ์ธ์ด ํŒ๋งคํ•˜๋Š” ์ œํ’ˆ์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ํ™•์‹คํ•˜๊ฒŒ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ปจ์„คํ„ดํŠธ์˜ ๊ฒฝ์šฐ, ์‹œ๋‹ˆ์–ด ์ปจ์„คํ„ดํŠธ ์ด์ƒ์ด ๋˜๋ฉด ๋ณธ์ธ์ด ๋งˆ์ผ€ํ„ฐ์™€ ์˜์—…๋Œ€ํ‘œ์˜ ์—ญํ• ์„ ๋™์‹œ์— ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์—ญ๋Ÿ‰์€ ๋ฌธ์„œํ™” ์—ญ๋Ÿ‰๊ณผ ๋”๋ถˆ์–ด ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. (4) ์ œํ’ˆ ์‹œ์—ฐ(Demonstration) ์š”์ฆ˜์€ ์ œํ’ˆ ์‹œ์—ฐ๊ณผ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์ด ๋ณ‘ํ–‰ํ•˜์—ฌ ์ง„ํ–‰๋˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. ๋ง์ด ๋ฌด์Šจ ํ•„์š”๊ฐ€ ์žˆ๋Š”๊ฐ€? ์‹ค์ œ๋กœ ๋ณด์—ฌ ์ค€๋‹ค. ๋˜๋Š” ์‹œ๋ฎฌ๋ ˆ์ด์…˜(Simulation) ํ•˜์—ฌ ๊ณ ๊ฐ์ด ์ƒ์ƒํ•œ ๊ฒƒ์„ ์‹คํ˜„ํ•ด ์ค€๋‹ค. ์ œํ’ˆ ์‹œ์—ฐ์„ ํ†ตํ•œ ์˜์—… ๋ฐฉ๋ฒ•์€ ์ €์ž์˜ ๊ฒฝํ—˜์— ์˜ํ•˜๋ฉด ๊ฐ€์žฅ ํšจ๊ณผ๊ฐ€ ํฌ๋‹ค. IT ์„ค๋ฃจ์…˜ ์ปจ์„คํ„ดํŠธ๋“ค์˜ ๊ฒฝ์šฐ, ์ œํ’ˆ ์‹œ์—ฐ์€ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ์„ค๋ฃจ์…˜์„ ํŒ”๋ฉด์„œ ๊ธฐ๋Šฅ ์„ค๋ช…๋ถ€ํ„ฐ ์ ์šฉ ์‚ฌ๋ก€ ๋“ฑ ์ „๋ฐ˜์ ์ธ ์‚ฌ์—…์ „๋žต์„ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (5) ๊ตฌ๋งค์ž ์ •๋ณด โ€˜Persona Guideโ€™๋ผ๊ณ  ๋ถˆ๋ฆฌ๊ธฐ๋„ ํ•˜๋Š”๋ฐ ๊ตฌ๋งค ์˜์‚ฌ๊ฒฐ์ •์—์„œ ํ•ต์‹ฌ ๊ตฌ๋งค์ž์˜ ์„ฑํ–ฅ์„ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ์ˆ˜์ง‘๋˜์–ด์•ผ ํ•˜๋Š” ์ •๋ณด๋“ค์ด๋‹ค. ์–ด๋–ค ๊ด€์ ์—์„œ ์ œํ’ˆ์„ ๊ฐ•์กฐํ•˜๋ฉฐ, ์–ด๋–ค ๋ถ€๋ถ„์„ ๋ฉด๋ฐ€ํžˆ ๋ณด๋ ค ํ•˜๋Š”๊ฐ€ ๋“ฑ ๊ตฌ๋งค ํฌ์ธํŠธ๊ฐ€ ํ‘œ์ถœ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๋˜ํ•œ, Powerbase๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋ผ๋„ ์ด๋Š” ๋ฐ˜๋“œ์‹œ ํ™•๋ณดํ•ด์•ผ ํ•˜๋ฉฐ ๊ถ๊ทน์ ์œผ๋กœ Power map ์ž‘์„ฑ์ด ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ์ปจ์„คํ„ดํŠธ๋“ค์€ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ, ์ตœ๊ณ ๊ฒฝ์˜์ž๋ฅผ ํฌํ•จํ•˜์—ฌ ์ž„์›๋“ค๊ณผ ๋งŽ์€ ์‹œ๊ฐ„์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š”๋ฐ ๊ทธ๋“ค๊ณผ ์ด์•ผ๊ธฐํ•˜๋ฉด์„œ ๊ธฐ์—…์˜ ๊ตฌ๋งค๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ฐœ์ƒํ•˜๋Š”์ง€ ์–ด๋–ค ๋ถ€๋ถ„์ด ์•„์‰ฌ์šด์ง€, ์–ด๋–ค ๊ฒƒ๋“ค์„ ๋„์™€์ค„ ์ˆ˜ ์žˆ๋Š”์ง€ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. (6) ๊ฒฝ์Ÿ์‚ฌ ์ •๋ณด ๋งŽ์ด ์•Œ๋ฉด ์•Œ์ˆ˜๋ก ์ข‹๋‹ค. ์˜์—… ํ™œ๋™์— ๋ฐฉํ•ด๊ฐ€ ๋˜๋Š” ๊ฒฝ์Ÿ์‚ฌ๋ฅผ ํ•ฉ๋ฒ•์ ์ธ ๋ฒ”์œ„ ๋‚ด์—์„œ ์–ด๋–ป๊ฒŒ ์ œ๊ฑฐํ•  ๊ฒƒ์ธ๊ฐ€ ํ•˜๋Š” ๋ฌธ์ œ๋Š” ๊ธฐ์—…๋“ค์˜ ๊ฐ„์˜ ๊ฒฝ์Ÿ์—์„œ ์˜์—…์ด ๊ณ ๋ฏผํ•ด์•ผ ํ•  ์ตœ๊ณ ์˜<NAME> ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ๊ฒฝ์Ÿ์‚ฌ ํ”„๋กœํŒŒ์ผ๋ง์„ ํ†ตํ•ด ์ฒด๊ณ„์ ์œผ๋กœ ํŒŒ์•…ํ•ด์•ผ ํ•œ๋‹ค. (7) ๊ณ ๊ฐ ํ‰ํŒ B2B ์‚ฌ์—…์—์„œ ๊ณ ๊ฐ์€ ๊ธฐ์—…์ด๋ฏ€๋กœ ์ƒํ˜ธ ์œˆ์œˆ(Win-Win) ํ•˜๋Š” ํŒŒํŠธ๋„ˆ์‹ญ์„ ๊ตฌ์ถ•ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ณ ๊ฐ์ด ๊ฐ€์žฅ ์ด์ƒ์ ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๋Œ€์šฐ๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•œ ๊ธฐ์ˆ ์ด๋‚˜ ์—ญ๋Ÿ‰์˜ ์ถ•์ ๋„ ์ค‘์š”ํ•˜๊ฒ ์ง€๋งŒ, ์ปจ์„คํŒ… ๊ธฐ์—…์ด ์ œ๊ณต ํ•˜๋Š” ๊ฐ€์น˜๋ฅผ ์ธ์ •ํ•˜๋ฉด์„œ ๊ทธ๋Ÿฐ ๋Œ€์šฐ๋ฅผ ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ๊ณ ๊ฐ์ธ์ง€๋„ ์ค‘์š”ํ•˜๋‹ค. ์„ฑ๊ณต ์Šคํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ณ ๊ฐ ํ‰ํŒ๋„ ์ ๊ฒ€ํ•ด์•ผ ํ•˜๋ฉฐ ์„ฑ๊ณต์ ์ธ ์ œํ’ˆ ๊ณต๊ธ‰๊ณผ ๊ด€๊ณ„ ํ˜•์„ฑ์„ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ณ ๊ฐ ํ‰ํŒ์€ ์ตœ๊ณ ์˜ ์˜์—… ๋„๊ตฌ๊ฐ€ ๋œ๋‹ค. ๊ธฐ์—… ๊ณ ๊ฐ์ด ๋‚˜์„œ์„œ ์šฐ๋ฆฌ ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค๋ฅผ ์Šค์Šค๋กœ ์•Œ๋ ค์ฃผ๋‹ˆ ์ด๋ณด๋‹ค ์ข‹์€ ์ผ์ด ์—†์ง€ ์•Š์€๊ฐ€? (8) ์งˆ์˜์‘๋‹ต ์‚ฌ์ „ ์˜์–ด๋กœ๋Š” โ€˜Conversation Guideโ€™๋ผ๊ณ  ํ•œ๋‹ค. ์‚ฐ์—…์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅด์ง€๋งŒ ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๋ฅผ ํŒŒ์•…ํ•  ๋•Œ ๋ฌธ์˜ํ•˜๋Š” ์งˆ๋ฌธ๋“ค์€ ๋งค์šฐ ์ •ํ˜•์ ์ด๋‹ค. ์˜์—… ํ™œ๋™์ด ์ฒด๊ณ„์ ์œผ๋กœ ์ง€์‹DBํ™”๋˜์–ด ์žˆ๋‹ค๋ฉด ์œ ํ˜•๋ณ„๋กœ ์ด๋Ÿฐ ์งˆ๋ฌธ๋“ค๊ณผ ์‘๋‹ต, ๋Œ€์‘ ์ƒํ™ฉ ๋“ฑ์ด ์ •๋ฆฌ๋˜์–ด ์žˆ๊ณ  ๊ต์œก ๊ต์žฌ๋กœ๋„ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋…ธ๋ จํ•œ ์‹œ๋‹ˆ์–ด ์ปจ์„คํ„ดํŠธ๋ผ๋ฉด ์•„๋งˆ ์ง์ ‘ ์ž‘์„ฑํ•ด์„œ ํ›„๋ฐฐ๋“ค์—๊ฒŒ ๋ฐฐํฌํ•  ๊ฒƒ์ด๋‹ค (9) ์ „๋ฌธ๊ฐ€ ๋„คํŠธ์›Œํฌ ์˜ค๋Š˜๋‚  B2B ์˜์—…๋Œ€ํ‘œ๋Š” ์‚ฐ์—… ํ˜„์žฅ์˜ ์Šˆํผ๋งจ์ด ๋˜๊ธฐ๋ฅผ ์š”๊ตฌ๋ฐ›๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ˜„์‹ค์ ์œผ๋กœ ๋ชจ๋“  ๊ฒƒ์„ ๋‹ค ๋Œ€์‘ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๊ธฐ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ ์ ์žฌ์ ์†Œ์— ๊ทธ ์ผ์„ ๋Œ€์‹ ํ•ด ์ค„ ๋˜๋Š” ์ง€์›ํ•ด ์ค„ ์‚ฌ๋žŒ๋“ค์„ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ํŠนํžˆ, ์ „๋ฌธ ๋ถ„์•ผ์˜ ์˜๊ฒฌ์„ ์ฒญ์ทจํ•ด์„œ ๊ณ ๊ฐ์—๊ฒŒ ์ „ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ณ ๊ฐ์—๊ฒŒ ํ›จ์”ฌ ์‹ ๋ขฐ๋ฅผ ์ค„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ปจ์„คํ„ดํŠธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค. ์‚ฌ๋‚ด์˜ ์ง€์‹ ์ „๋ฌธ๊ฐ€(Knowledge Expert) ๊ทธ๋ฃน์„ ํ™œ์šฉํ•˜์—ฌ ๋นจ๋ฆฌ ์ด์Šˆ๋‚˜ ๋ฌธ์ œ์— ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. (10) ROI ๊ณ„์‚ฐ๊ธฐ ์—‘์…€์„ ์‚ฌ์šฉํ•˜๋˜ ์ •๋ณด์‹œ์Šคํ…œ์„ ํ™œ์šฉํ•˜๋˜ ์‚ฌ์—…์— ๋“ค์–ด๊ฐ€๋Š” ์›๊ฐ€์™€ ์ด์ต์„ ํ•ญ์ƒ ์ƒ๊ฐํ•ด์•ผ ํ•˜๋Š” ์ž…์žฅ์—์„œ ROI๋Š” ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ์ปจ์„คํŒ…์˜ ๋Œ€๋ถ€๋ถ„ ์›๊ฐ€๋Š” ์ธ๊ฑด๋น„์ด๊ฒ ์ง€๋งŒ ์ตœ๊ทผ ์„ค๋ฃจ์…˜์„ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋˜๋Š” ๋ผ์ด์„ ์‹ฑ์„ ๊ณ ๋ฏผํ•˜๋Š” ๊ธฐ์—…์˜ ๊ฒฝ์šฐ, ์›๊ฐ€ ์š”์†Œ์™€ ๊ทธ์— ๋งž๋Š” ๊ฐ€๊ฒฉ ์ „๋žต์„ ์ˆ˜๋ฆฝํ•˜์—ฌ ์ตœ๊ณ ์˜ ROI๋ฅผ ๋‹ฌ์„ฑํ•˜๋Š” ๋น„์ฆˆ๋‹ˆ์Šค๋ผ๋Š” ์ธก๋ฉด์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค Break #20. ์˜์—…๊ธฐํšŒ๊ด€๋ฆฌ(Tier Management: TM) TM์€ B2B ๊ธฐ์—…์—์„œ ์„ธ์ผ์ฆˆ ๋ฆฌ๋“œ(SQL)๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ธ์ (Sales Resources), ๋ฌผ์ (Budget), ์‹œ๊ฐ„(Time) ์ž์›์„ ์ ์ ˆํ•˜๊ฒŒ ๋ฐฐ๋ถ„ํ•˜์—ฌ ์ตœ์ƒ์˜ ์˜์—… ์„ฑ๊ณผ๋ฅผ ์–ป๊ณ ์ž ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ณด๋‹ค ์ค‘์š”ํ•œ ๊ฒƒ์€ TM์„ ํ†ตํ•ด์„œ โ€˜์ถ”๊ตฌํ•˜๋Š” ๋น„์ฆˆ๋‹ˆ์Šค์˜ ์„ฑ๊ฒฉ์„ ์ •ํ™•ํ•˜๊ฒŒ ๊ทœ์ •ํ•˜๊ณ  ๋‚˜์•„๊ฐ€๋Š” ๊ฒƒโ€™์ด๋‹ค. ํƒ€ B2B ์‚ฌ์—…๊ณผ ๋‹ฌ๋ฆฌ ์ปจ์„คํŒ… ์‚ฌ์—…์€ ์‚ฌ์—… ๊ทœ๋ชจ๊ฐ€ ํฌ์ง€ ์•Š์•„ TM ๊ฐ™์€ ๊ฒƒ์„ ํ•˜์ง€ ์•Š๊ณ  ์ฃผ๋จน๊ตฌ๊ตฌ์‹์œผ๋กœ ์˜์—…๊ธฐํšŒ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํšŒ์‚ฌ๋“ค๋„ ๋งŽ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์—…์€ ์ƒ๋ช…์ฒด์™€ ๊ฐ™์•„์„œ ์ง€์†์ ์œผ๋กœ ๋ณ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์—… ์ˆ˜์ฃผ ์ด์ „๊นŒ์ง€ ์ง€์†์ ์œผ๋กœ ๊ด€๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด TM์€ ๋น„์ฆˆ๋‹ˆ์Šค ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ธ์ง€ํ•˜๊ณ  ์ ์ ˆํ•œ ๋น„์šฉ๊ณผ ์ž์›์„ ์ ์‹œ์— ํˆฌ์ž…ํ•˜์—ฌ ์ตœ์ƒ์˜ ์‚ฌ์—… ์„ฑ๊ณผ๋ฅผ ์–ป๋Š” ์•ˆ๋ชฉ์„ ํ‚ค์šฐ๋Š” ๋„๊ตฌ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ TM์€ ์ถ”์ƒ์  ๊ฐœ๋…์ด๋ฏ€๋กœ ์‹ค์ œ ์—…๋ฌด ํ˜„์žฅ์—์„œ ์ด๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด์„œ๋Š” ์ž˜ ์•Œ๋ ค์ ธ ์žˆ์ง€ ์•Š์œผ๋ฉฐ ๋…ผ๋ฆฌ์™€ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ข‹์•„ํ•˜๋Š” ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค๋งˆ๋‹ค ๊ณ ์œ ์˜ ์˜์—…๊ธฐํšŒ ๊ด€๋ฆฌ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ํŠน๋ณ„ํ•œ ์•„์ด๋””์–ด๊ฐ€ ์—†๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด๋ณด์ž. (1) ์‚ฌ์—…๊ธฐํšŒ๋“ค์„ Tier 4์—์„œ Tier 1๊นŒ์ง€ 4๊ฐ€์ง€๋กœ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์ˆœ์œ„(Ranking)์„ ๋งค๊ธด๋‹ค. ํ˜„์žฌ Tier 4 ์˜์—ญ์— ์žˆ๋Š” ์‚ฌ์—… ๊ธฐํšŒ๋“ค์€ ์˜์—… ์ •๋ณด๊ฐ€ ๋ณด์™„๋˜๋ฉด ๋ฏธ๋ž˜์— Tier 1 ์˜์—ญ์œผ๋กœ ์ด๋™ํ•ด ๊ฐˆ ๊ฒƒ์ด๋‹ค. (2) ๊ฐ Tier์— ๋ฐฐ์น˜๋œ ์‚ฌ์—… ๊ธฐํšŒ๋“ค์„ ์˜์—… ๋‹จ๊ณ„์™€ ๋งคํ•‘ ์‹œํ‚จ๋‹ค. ์˜์—… ๋‹จ๊ณ„๋Š” ๊ฐ ์‚ฐ์—… ๋˜๋Š” ๊ฐ ๊ธฐ์—…๋งˆ๋‹ค ๊ณ ์œ ์˜ ๊ธฐ์ค€์ด ์žˆ๊ฒ ์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ์ดˆ๊ธฐ ์ ‘์ด‰, ์‚ฌ์—…๊ธฐํšŒ ๊ฐœ๋ฐœ, ์‚ฌ์—…๊ธฐํšŒ ํ‰๊ฐ€, ์ œ์•ˆ ๋ฐ ๊ณ„์•ฝ, ์ข…๋ฃŒ ๋ฐ ๊ฒฐ๊ณผ ๋ฆฌ๋ทฐ์˜ 5๋‹จ๊ณ„๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. (3) ์ •์น˜์ , ์‚ฌํšŒ์ , ๋ฌธํ™”์  ์š”์†Œ ๋“ฑ ์‹œ์žฅ ์ƒํ™ฉ์„ ์ •ํ™•ํžˆ ํŒŒ์•…ํ•˜์—ฌ ์ •๋ณด๋ฅผ ์ถฉ์‹คํžˆ ๋ณด์™„ํ•˜๊ณ  ์‚ฌ์—… ํ”„๋กœํŒŒ์ผ๊ณผ ์ˆœ์œ„๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค. (4) ์‹œ์žฅ ํ™˜๊ฒฝ์˜ ํƒ€์ž„๋ผ์ธ์„ ๋‹จ๊ธฐ(2~4๊ฐœ์›”), ์ค‘๊ธฐ(5~12๊ฐœ์›”), ์žฅ๊ธฐ(12~24๊ฐœ์›”)๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ƒ๊ฐํ•ด ๋ณธ๋‹ค. (5) ์ฃผ์–ด์ง„ ์ž์›-์ธ์ /๋ฌผ์ /์‹œ๊ฐ„์„ ์ ์ ˆํ•˜๊ฒŒ ๋ฐฐ๋ถ„ํ•œ๋‹ค. (1) ~ (5)๋ฅผ ์ข…ํ•ฉํ•˜์—ฌ ์˜์—… ๋‹จ๊ณ„์™€ ์—ฐ๊ณ„์‹œํ‚ค๋ฉด Table V-1๊ณผ ๊ฐ™๋‹ค. ์ตœ์ดˆ์˜ ๊ณ ๊ฐ ์ ‘์ด‰๋ถ€ํ„ฐ ์ตœ์ข… ์‚ฌ์—… ์ˆ˜์ฃผ๊นŒ์ง€ ๋ณดํ†ต Tier 4 ~ Tier 1๋กœ ๋‚˜๋ˆ„์–ด ๊ด€๋ฆฌํ•˜์ง€๋งŒ, Tier 0๋„ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. Tier 0์˜ ๊ฒฝ์šฐ, ์„ฑ๊ณต์ ์œผ๋กœ ์‚ฌ์—…์„ ์ˆ˜์ฃผํ•˜์—ฌ ๊ณ„์•ฝ์œผ๋กœ ์ด์–ด์ง€๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์ง€๋งŒ, ์‹ค ์ฃผํ•˜์˜€์„ ๊ฒฝ์šฐ โ€˜์‹ค์ฃผ์˜ ์›์ธ์ด ๋ฌด์—‡์ธ์ง€?โ€™, โ€˜์•ž์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ธ์ง€?โ€™ ๋“ฑ ์˜์—… ํ™œ๋™์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰๊นŒ์ง€ ์ƒํ™ฉ์„ ๊ด€๊ณ„์ž๋“ค๊ณผ ํ•จ๊ป˜ ์ƒํ˜ธ ๊ฒ€ํ† ํ•ด ๋ณด๋Š” ์ผ ์ฆ‰, โ€˜ํฌ์ŠคํŠธ๋ชจํ…œ(Postmortem)โ€™์„ ์ง„ํ–‰ํ•˜๊ณ  ์ „ ์ž…์ฐฐ ๊ณผ์ • ๋ฐ ๊ฒฐ๊ณผ์˜ ๊ตํ›ˆ(lesson learned)์„ ์ •๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. Table V-2. TM์˜ ๋‹จ๊ณ„ ๋ฐ ์ฃผ์š” ๋‚ด์šฉ [1] ์ผ๋ฐ˜์ ์œผ๋กœ Warm, Hot, Cold์˜ 3๊ฐ€์ง€ ๋ฆฌ๋“œ๋กœ ๊ตฌ๋ถ„ํ•˜๋ฉฐ, ๋ณดํ†ต ์ด๋ฉ”์ผ ์ฃผ์†Œ๋‚˜ URL ์ •๋„์˜ ๋งˆ์ผ€ํŒ… ๋ฆฌ๋“œ๊ฐ€ ๋‹ด๋‹น์ž ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์—…๋ฐ์ดํŠธ๋˜๋ฉด ์„ธ์ผ์ฆˆ ๋ฆฌ๋“œ๋กœ ์ „ํ™˜๋œ๋‹ค. [2] Marketing Qualified Leads [3] Sales Qualified Leads [4] ๋Œ€์ฒด์ ์œผ๋กœ IT ์„œ๋น„์Šค ๊ธฐ์—…์—๋Š” ํ•ด๋‹น ์‚ฌ์—… ๋„๋ฉ”์ธ์„ ์ฑ…์ž„์ง€๊ณ  ์žˆ๋Š” ์˜์—…์ด ์žˆ์–ด์„œ ์ปจ์„คํŒ…๊นŒ์ง€ ๊ฐ™์ด ์˜์—…ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ณ , ์ปจ์„คํ„ดํŠธ๋“ค์€ ์˜์—… ์ง€์›์˜ ์—ญํ• ์„ ํ•œ๋‹ค. ๋ฌผ๋ก , ๊ฐ•ํ•œ ์˜ค๋„ˆ์‹ญ(ownership)์„ ๊ฐ€์ง„ ์‹œ๋‹ˆ์–ด ์ปจ์„คํ„ดํŠธ๋“ค์€ ์ง์ ‘ ์˜์—…ํ•˜๊ณ  ์‚ฌ์—…๊ฐœ๋ฐœ์„ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. [5] www.linkedin.com [6] www.facebook.com [7] Corporate Identity 18.2 ์ž…์ฐฐ๊ณผ ๊ณ„์•ฝ ์„ธ์ผ์ฆˆ ๋ฆฌ๋“œ๋ฅผ ํ™•๋ณดํ•˜๋ฉด ๋ณธ๊ฒฉ์ ์œผ๋กœ ์ œ์•ˆ๊ณผ ์ž…์ฐฐ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. B2B ์‚ฌ์—…์€ ์ผ๋ฐ˜ ๋Œ€์ค‘์ด ์†Œ๋น„์ž์ธ B2C ์‚ฌ์—…๊ณผ ๋‹ฌ๋ฆฌ ๊ธฐ์—…์ด๋‚˜ ์ •๋ถ€๊ฐ€ ์„œ๋น„์Šค๋ฅผ ๊ตฌ๋งคํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„ ๊ฒฝ์Ÿ ์ž…์ฐฐ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ B2B ์‚ฌ์—…์—์„œ๋Š” ๊ณ ๊ฐ์˜ ๊ตฌ๋งค ํ”„๋กœ์„ธ์Šค๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ๊ธ€๋กœ๋ฒŒ ๋ฆฌ์„œ์น˜ ํšŒ์‚ฌ CEB[1]๊ฐ€ ์ง์› 5,000๋ช… ์ด์ƒ์˜ B2B ๊ธฐ์—…์˜ ๊ตฌ๋งค๋‹ด๋‹น์ž๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ์— ์˜ํ•˜๋ฉด ํ•œ ๊ฑด์˜ ๊ตฌ๋งค๊ณ„์•ฝ ์ฒด๊ฒฐ๊นŒ์ง€ ํ‰๊ท  5.4๋ช…์˜ ๊ณต์‹์ ์ธ ์Šน์ธ์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ํ•œ๋‹ค. โ€˜ํ†ตํ•ฉ๊ตฌ๋งค์„ผํ„ฐโ€™๊ฐ™์€ ์กฐ์ง์ด ์žˆ๋Š” ๊ธฐ์—…๋“ค์€ ๋‚ฉํ’ˆ๋ฐ›๋Š” ์ œํ’ˆ๋“ค์— ๋Œ€ํ•ด ๊ณ ์œ ์˜ ๊ฒ€ํ†  ์ ˆ์ฐจ๊ฐ€ ์žˆ์œผ๋ฉฐ ๋•Œ๋•Œ๋กœ ๋‹จ์ˆœํžˆ ๊ฒฌ์ ์„ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ž…์ฐฐ์„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๋‹ค. Figure V-2. B2C ์‚ฌ์—…๊ณผ B2B ์‚ฌ์—…์˜ ๊ตฌ๋งค ํ”„๋กœ์„ธ์Šค Figure V-2๋Š” B2B/B2C ๊ตฌ๋งค ํ”„๋กœ์„ธ์Šค๋ฅผ ๋น„๊ตํ•ด ๋ณธ ๊ฒƒ์ธ๋ฐ B2C ๊ตฌ๋งค๋Š” ๋‹ค์–‘ํ•œ ๋งˆ์ผ€ํŒ… ์š”์ธ๊ณผ ํ™˜๊ฒฝ์  ์š”์ธ์— ์˜ํ–ฅ๋ฐ›์œผ๋ฉฐ ์ „์ ์œผ๋กœ ๊ฐœ์ธ์˜ ํŒ๋‹จ์— ์˜ํ•ด ์ด๋ฃจ์–ด์ง€๋Š” ๋ฐ˜๋ฉด, B2B ๊ตฌ๋งค์˜ ๊ฒฝ์šฐ ์ž์‚ฌ ์ œํ’ˆ์„ ๊ณ ๊ฐ์—๊ฒŒ ๋…ธ์ถœํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๊ทธ ์ ˆ์ฐจ๋„ ๋ณต์žกํ•˜์ง€๋งŒ ๊ณ ๊ฐ ๊ด€๊ณ„๋ฅผ ์žฅ๊ธฐ์ ์ด๋ฉฐ ์ง€์†์ ์ธ ๊ด€๊ณ„๋กœ ๋งŒ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋ฉฐ ๊ตฌ๋งค ํ›„ ์„ฑ๊ณผํ‰๊ฐ€ ์‹œ์ ์—์„œ ๊ณ ๊ฐ์˜ ๋ชฉ์†Œ๋ฆฌ(Voice of Customer:VoC)๋ฅผ ์ฒญ์ทจํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ์ด๋Š” VoC๋ฅผ ํ†ตํ•ด์„œ ์ œํ’ˆ์˜ ๊ฐœ์„  ๋ฐ ์ฐจ๋ณ„ํ™” ์š”์ธ๋“ค์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๊ณ , ๊ณ ๊ฐ์˜ Pain points ๋˜๋Š” Unmet Needs๋ฅผ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ๋ถ€๋ถ„์€ ์ œํ’ˆ์ด๋‚˜ ์„œ๋น„์Šค์˜ ์žฌ๊ตฌ๋งค์™€ ์ง๊ฒฐ๋œ๋‹ค. ์ปจ์„คํŒ… ์‚ฌ์—…๋„ ์ปจ์„คํŒ… ๊ฒฐ๊ณผ๊ฐ€ ์–ด๋–ค๊ฐ€์— ๋”ฐ๋ผ ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ํ‰ํŒ(reputation)์ด ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ณ ๊ฐ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ถฉ์กฑ์‹œํ‚จ ํ›„, ๊ณ ๊ฐ์˜ Pain points๋ฅผ ํ„ฐ์น˜ํ•˜๊ฑฐ๋‚˜ Unmet Needs๋ฅผ ํ•ด์†Œ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๊ทธ ๊ณ ๊ฐ์€ ๊ฑฐ์˜ ํ•ต์‹ฌ ๊ณ ๊ฐ์œผ๋กœ ์ „ํ™˜๋œ๋‹ค. ํƒ€ B2B ๊ธฐ์—…๊ณผ ๋‹ฌ๋ฆฌ ์ปจ์„คํŒ… ๊ธฐ์—…์€ ์‚ฌ๋žŒ์ด ์ค‘์š”ํ•œ ์ž์‚ฐ(assets)์ด๋ฏ€๋กœ ๊ทธ๋“ค์˜ ์—ญ๋Ÿ‰์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฒฐ๊ตญ ์„ฑ๊ณต์ ์ธ ๊ฒฐ๊ณผ์™€ ์‹ค์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋ธŒ๋žœ๋“œ(Brand), ๊ทธ๋ฆฌ๊ณ  ์ข‹์€ ์ธ์žฌ๋ฐ–์—๋Š” ์—†๋‹ค. ์ด ๋ถ€๋ถ„์—์„œ ๊ตญ๋‚ด IT ์ปจ์„คํŒ… ์‹œ์žฅ์„ ์‚ดํŽด๋ณด๋ฉด IT ์„œ๋น„์Šค ๊ธฐ์—…์˜ ํ•œ ๋ถ€์„œ์ด๊ฑฐ๋‚˜ IT ์„œ๋น„์Šค ๊ธฐ์—…์„ ๋ชจํšŒ์‚ฌ๋กœ ๋‘” ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ๊ฒฝ์šฐ, ์ด๋Ÿฐ ๋ถ€๋ถ„์—์„œ ์ƒ๋Œ€์ ์œผ๋กœ ๋…ธ๋ ฅ(efforts)์„ ๋œ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด IT ์„œ๋น„์Šค ์กฐ์ง์˜ ์˜์—…๋Œ€ํ‘œ๊ฐ€ ์ปจ์„คํŒ… ์‚ฌ์—…์˜ ์˜์—…๋„ ๋งก์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.[2] ๊ทธ๋Ÿฐ ์กฐ์ง์— ์†ํ•œ ์ปจ์„คํ„ดํŠธ๋“ค์€ ์‚ฌ์—…์˜ ์ˆ˜์ฃผ๋ณด๋‹ค๋Š” ์ˆ˜์ฃผ๋œ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋งŒ ์ž˜ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์™ธ๊ตญ๊ณ„ ์ปจ์„คํŒ… ๊ธฐ์—…์ด๋‚˜ ์ด๋Ÿฐ ์˜์—… ํ™œ๋™์ด ํ•„์š”ํ•œ ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ๊ฒฝ์šฐ, ์ง€์†์ ์ธ ์ˆ˜์ฃผ์ž”๊ณ  ํ™•๋Œ€๋Š” ์ค‘์š”ํ•œ ์„ฑ๊ณผ ์ง€ํ‘œ ์ค‘์˜ ํ•˜๋‚˜์˜€๊ธฐ์— ๋‚ฎ์—๋Š” ๊ธฐ์กด์— ์ˆ˜์ฃผํ•œ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ๋ฐค์—๋Š” ํƒ€ ์‚ฌ์—…์˜ ์ˆ˜์ฃผ๋ฅผ ์œ„ํ•ด ์ œ์•ˆ์„œ๋ฅผ ์“ฐ๋Š” ์ผ๋„ ๋น„์ผ๋น„์žฌํ•˜์˜€๋‹ค.[3] ๊ทธ๋ž˜์„œ ๋งŽ์€ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ๊ทธ๋Ÿฐ ์ œ์•ˆ์— ๋“œ๋Š” ๋…ธ๋ ฅ์„ ๊ฐ์†Œ์‹œํ‚ค๊ณ ์ž C-๋ ˆ๋ฒจ ์˜์—…[4]์„ ์„ ํ˜ธํ•˜์˜€๊ณ , ๋งˆ์ผ€ํŒ… ํ™œ๋™์„ ํ†ตํ•ด ๊ธฐ์—…์˜ ์ฃผ์š” ๊ฒฝ์˜์ž๋“ค์—๊ฒŒ ์ ‘๊ทผํ•˜๊ณ ์ž ๋…ธ๋ ฅํ•˜์˜€๋‹ค. ์‚ฌ์—…์ด ๋ฐœ์ฃผ๋˜๋ฉด ์ œ์•ˆ์š”์ฒญ์„œ(Rfp)์— ๋”ฐ๋ผ ์ œ์•ˆ์„œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ œ์ถœํ•˜๊ณ  ์ž…์ฐฐ ๊ฒฐ๊ณผ ์šฐ์„ ํ˜‘์ƒ ๋Œ€์ƒ์ž(Preferred Bidder)๋กœ ์„ ์ •๋˜๋ฉด ์‚ฌ์—…์˜ ๋ฒ”์œ„ ๋ฐ ๊ฐ€๊ฒฉ ๋“ฑ์— ๋Œ€ํ•ด ํ˜‘์ƒํ•˜๊ฒŒ ๋œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ˜‘์ƒ ํ•ญ๋ชฉ์ธ ์‚ฌ์—…์˜ ๋ฒ”์œ„๋Š” ์ œ์•ˆ์š”์ฒญ์„œ์˜ ๋‚ด์šฉ์„ ๊ธฐ๋ณธ์œผ๋กœ ํ•˜์ง€๋งŒ ๊ณ„์•ฝ ์‹œ์ ์— ๋‹ค์‹œ ํ•œ๋ฒˆ ํ™•์ธํ•˜๊ฒŒ ๋œ๋‹ค. ๊ธฐ๊ฐ„ ๋‚ด ์ œ์•ˆ ๋‚ด์šฉ๋Œ€๋กœ ์ปจ์„คํŒ…์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋“ฑ์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ณ ๊ฐ๊ณผ ํ™•์•ฝํ•˜๊ณ  ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋ฒ”์œ„๋ฅผ ์กฐ์ •ํ•˜์˜€๋‹ค. ๋‘ ๋ฒˆ์งธ ํ˜‘์ƒ ํ•ญ๋ชฉ์ธ ๊ฐ€๊ฒฉ์€ ๋ฒ”์œ„์— ์˜์กด์ ์ด๋ฉฐ ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ๋…ธํ•˜์šฐ๊ฐ€ ๋‹ด๊ธฐ๊ฒŒ ๋œ๋‹ค. ์ปจ์„คํŒ… ๋น„์šฉ์˜ ๋Œ€๋ถ€๋ถ„์€ ์ปจ์„คํ„ดํŠธ๋“ค์˜ ๋ชธ๊ฐ’ ์ฆ‰, ์ธ๊ฑด๋น„์ด๊ธฐ ๋•Œ๋ฌธ์— ์ปจ์„คํŒ… ๊ธฐ์—… ์ž…์žฅ์—์„œ๋Š” ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ์ง€์ผœ๋‚ด๋Š๋ƒ๊ฐ€ ๊ทธ๋“ค ์ˆ˜์ต๊ณผ ์ง๊ฒฐ๋˜์—ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ B2B ์‚ฌ์—…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ 3๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๊ฐ€๊ฒฉ์ด ์กด์žฌํ•œ๋‹ค. ์›๊ฐ€์— ์˜ํ•œ ๊ฐ€๊ฒฉ ๊ฒฝ์Ÿ์ž…์ฐฐ์— ์˜ํ•œ ๊ฐ€๊ฒฉ ๊ณต์‹œ๊ฐ€์— ์˜ํ•œ ๊ฐ€๊ฒฉ ์›๊ฐ€์— ์˜ํ•œ ๊ฐ€๊ฒฉ ๊ฒฐ์ •์€ ์›์ž์žฌ, ์ธ๊ฑด๋น„, ๊ฐ„์ ‘๋น„ ๋“ฑ ๋น„์šฉ ์š”์†Œ๋ฅผ ์ค‘์‹œํ•˜๋Š” ์ œ์กฐ๊ธฐ์—…๋“ค์˜ ๊ฐ€๊ฒฉ ๊ฒฐ์ • ๋ฐฉ๋ฒ•์œผ๋กœ ํŒ๋งค์ž๋Š” ์ƒ์‚ฐ๊ณผ ํŒ๋งค๋น„์šฉ์— ํ•ฉ๋‹นํ•œ ์ด์ต์„ ์–ป๊ณ  ํˆฌ์ž์— ๋Œ€ํ•œ ์œ„ํ—˜์„ ์ตœ์†Œํ™”ํ•˜๋Š” ์ฐจ์›์—์„œ ๊ฐ€๊ฒฉ์„ ๊ฒฐ์ •ํ•˜๊ณ , ๊ตฌ๋งค์ž๋Š” ํŒ๋งค์ž๊ฐ€ ์ƒ์‚ฐ ํ•™์Šตํšจ๊ณผ์— ์˜ํ•ด ๋น„์šฉ ์ ˆ๊ฐ์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์ด๋ผ๋Š” ์ถ”์ •ํ•˜์— ์‚ฐ์ •๋˜๋Š” ์ด์ต์„ ๋ฐ˜์˜ํ•œ ๊ฐ€๊ฒฉ์„ ๊ธฐ๋Œ€ํ•œ๋‹ค. ๊ฒฝ์Ÿ์ž…์ฐฐ์— ์˜ํ•œ ๊ฐ€๊ฒฉ ๊ฒฐ์ •์€ ๊ฐ€๊ฒฉ์ด ์‹œ์žฅ์— ์˜ํ•ด ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ตฌ๋งค์ž์™€ ํŒ๋งค์ž๊ฐ€ ์„œ๋กœ ํ˜‘์ƒํ•˜์—ฌ ๊ฒฐ์ •ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์šฐ์„ ํ˜‘์ƒ ๋Œ€์ƒ์ž๋กœ ์„ ์ •๋œ ๊ธฐ์—…๊ณผ ๊ฐ€๊ฒฉ์„ ์ตœ์ข… ํ˜‘์ƒํ•˜๊ณ  ๊ณ„์•ฝ์„ ๋งบ๋Š”๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ณต์‹œ๊ฐ€์— ์˜ํ•œ ๊ฒฐ์ •์€ B2G ์‚ฌ์—…์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ ์‹œ์žฅ๊ฐ€๊ฒฉ์„ ์ฐธ๊ณ ํ•˜์—ฌ ๊ตฌ๋งค๊ฐ€๊ฒฉ์„ ๊ฒฐ์ •ํ•œ ๋‹ค์Œ ์ •๋ถ€์กฐ๋‹ฌ๊ธฐ๊ด€์„ ํ†ตํ•ด ๊ณต์‹œํ•œ๋‹ค. ์ž…์ฐฐ์„ ํ†ตํ•ด ๊ณต์‹œ๊ฐ€์— ๊ฐ€์žฅ ๋งŽ์ด ๊ทผ์ ‘ํ•œ ๊ธฐ์—…์„ ์„ ์ •ํ•œ ํ›„ ์ œํ’ˆ์„ ๊ตฌ๋งคํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ตฌ๋ถ„์€ ์œ„์™€ ๊ฐ™์ด ํ•˜์˜€์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ๋ณด๋‹ค ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์ด ์ƒ๊ธฐ๊ธฐ๋„ ํ•œ๋‹ค. Figure V-3. ๊ฐ€๊ฒฉ ์ „๋žต ๋งคํŠธ๋ฆญ์Šค Figure V-3์˜ ๊ฐ€๊ฒฉ ์ „๋žต ๋งคํŠธ๋ฆญ์Šค๋Š” ์‚ฌ์—…๊ฐœ๋ฐœ ๊ด€์ ์—์„œ ๊ณ ์‹ฌํ•ด ๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋Š”๋ฐ ๋†’์€ ๋ธŒ๋žœ๋”ฉ ํŒŒ์›Œ์™€ ์‹œ์žฅ ์ง€๋ฐฐ์ ์ธ ์„œ๋น„์Šค๋ฅผ ๋ณด์œ ํ•˜๊ณ  ์žˆ์„ ๋•Œ โ€˜ํ”„๋ฆฌ๋ฏธ์—„ ๊ฐ€๊ฒฉ(Premium Price)โ€™์„ ๊ตฌ์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜, ์ด๋ ‡๊ฒŒ ํ™•๋ณด๋œ ์ด์ต์„ ํ™œ์šฉํ•ด ๊ฒฝ์Ÿ์‚ฌ๊ฐ€ ์‹œ์žฅ์— ์ง„์ž…ํ•  ๋•Œ ์นจํˆฌ๊ฐ€๊ฒฉ (Penetration Price)์„ ์ทจํ•จ์œผ๋กœ์จ ๊ฒฝ์Ÿ ์ปจ์„คํŒ… ๊ธฐ์—…์ด ์‹œ์žฅ์— ์•ˆ์ฐฉํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ํ•˜๋Š” ์ง„์ž… ์žฅ๋ฒฝ(Entry Barrier)์„ ๊ตฌ์ถ•ํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฏผ๊ฐ„๊ธฐ์—…์„ ๋Œ€์ƒ์œผ๋กœ ํ•˜๋Š” ์ปจ์„คํŒ… ์‚ฌ์—…์—์„œ๋Š” ์˜๋„์ ์œผ๋กœ ํ”„๋ฆฌ๋ฏธ์—„ ๊ฐ€๊ฒฉ์ด๋‚˜ ์Šคํ‚ค๋ฐ ๊ฐ€๊ฒฉ(Skimming Price)์„ ๊ตฌ์‚ฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์ปจ์„คํŒ… ์„œ๋น„์Šค์˜ ๊ฐ€์น˜๋ฅผ ์ธ์ •๋ฐ›๊ณ  ๊ฐ•๋ ฅํ•œ ๊ณ ๊ฐ ๋ฆฌ๋”์‹ญ์ด ํ™•๋ณด๋œ ๊ฒฝ์šฐ๋Š” ๋‹น์—ฐํžˆ ํ”„๋ฆฌ๋ฏธ์—„ ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. ๋˜, ์‹œ์žฅ ์ง„์ž…์ž์˜ ๊ฒฝ์šฐ๋„ ์Šคํ‚ค๋ฐ ๊ฐ€๊ฒฉ์„ ๊ณ ๋ คํ•˜์ง€ ์นจํˆฌ ๊ฐ€๊ฒฉ์ด๋‚˜ ์ด์ฝ”๋…ธ๋ฏธ ๊ฐ€๊ฒฉ์„ ๊ณ ๋ คํ•˜์ง€๋Š” ์•Š๋Š”๋ฐ, ์ด๋Š” ๊ฒฝ์ œ ๋ถˆํ™ฉ์—๋„ ์‚ฐ์—… ๊ฐ€์น˜ ์ž์ฒด๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์„œ๋น„์Šค ๊ฐ€์น˜๋ฅผ ์ง€์ผœ๋‚ด๋ ค๋Š” ์˜๋„๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ธฐ์—…์˜ ์ƒ์กด์„ ๊ณ ๋ คํ–ˆ์„ ๋•Œ ๋ชจ๋“  ์ปจ์„คํŒ… ๊ธฐ์—…์ด ์ด๋Ÿฐ ์ •์ฑ…์„ ์ง€์†์ ์œผ๋กœ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์„์ง€ ์ง€์ผœ๋ณผ ์ผ์ด๋‹ค. ๋˜ํ•œ, ๊ตญ๋‚ด ๊ณต๊ณต IT ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ๋Š” ์ปจ์„คํŒ… ์‚ฌ์—… ๋Œ€๊ฐ€๋ฅผ ๊ณต์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์—… ๋ฒ”์œ„์™€ ์—…๋ฌด ๋‚œ์ด๋„๋ฅผ ํ™•์ •ํ•˜๋ฉด ์‚ฌ์—…๋น„์šฉ์ด ์ž๋™์ ์œผ๋กœ ํ™•์ •๋˜๊ฒŒ ๋œ๋‹ค. ๋˜ํ•œ, IT ์„ค๋ฃจ์…˜ ๊ธฐ์—…์˜ ๊ฒฝ์šฐ, ์ปจ์„คํŒ… ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ๋ฐ›์ง€ ์•Š๋Š” ๊ธฐ์—…๋“ค๋„ ์žˆ๋Š”๋ฐ ์„ค๋ฃจ์…˜ ๊ฐ€๊ฒฉ(solution pricing) ์ •์ฑ…์„ ๊ตฌ์‚ฌํ•˜๋ฉด์„œ ์ปจ์„คํŒ… ๋น„์šฉ์„ ์„ค๋ฃจ์…˜ ๊ฐ€๊ฒฉ ๋‚ด ๋น„์šฉ ์š”์†Œ๋กœ ํฌํ•จ์‹œํ‚จ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๋Š” ์—ฐ๊ตฌ๊ฐœ๋ฐœ๋น„์˜ ํšŒ์ˆ˜๋ฅผ ๊ฐ์•ˆํ•ด์„œ ๋งค์šฐ ์„ฑ๊ณต์ ์ธ ์‹œ์žฅ ์ง„์ถœ ์„ฑ๊ณผ๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ๊ทธ ๋ถ€๋ถ„์„ ํˆฌ์ž๋กœ ๋ณผ ๋•Œ ๊ฐ€๋Šฅํ•œ ๊ฐ€๊ฒฉ ์ „๋žต์ด๋‹ค. ์‚ฌ์—… ๋ฒ”์œ„์™€ ๊ฐ€๊ฒฉ์„ ๊ณ ๊ฐ๊ณผ ์ตœ์ข… ํ•ฉ์˜ํ•˜๋ฉด ๊ณ„์•ฝ์„ ๋งบ๊ฒŒ ๋œ๋‹ค. ๋‹ค์–‘ํ•œ ๊ณ„์•ฝ ์กฐ๊ฑด์ด ์žˆ์„ ์ˆ˜ ์žˆ์ง€๋งŒ ์ปจ์„คํŒ… ์‚ฌ์—… ๊ด€์ ์—์„œ๋Š” ๋น„๋ฐ€์œ ์ง€ ๊ณ„์•ฝ(NDA)[6]์ด๋‚˜ ๋ณด์•ˆ์„œ์•ฝ์„œ๊ฐ€ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค. ์ปจ์„คํŒ…์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๊ธฐ์—…์˜ ๊ธฐ๋ฐ€ ์‚ฌํ•ญ์„ ๋งŽ์ด ๋‹ค๋ฃจ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฐ ๋‚ด์šฉ๋“ค์ด ๊ธฐ์—… ์™ธ๋ถ€๋กœ ์œ ์ถœ๋˜์–ด ๊ธฐ์—…์— ์†ํ•ด๋ฅผ ๋ผ์น˜์ง€ ์•Š๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. 1.3 ์ปจ์„คํŒ… ์‚ฌ์—…๋น„์˜ ์‚ฐ์ • ์ปจ์„คํŒ… ์‚ฌ์—…๋น„๋ฅผ ์‚ฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ง€๊ธˆ๊นŒ์ง€ ์—…๊ณ„ ํ‘œ์ค€์ด๋ผ๊ณ  ํ• ๋งŒํ•œ ๊ฒƒ์€ ์—†๋‹ค. ํ•œ๋•Œ ๊ฒฝ์˜ ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ, ๋ถ€๋ฅด๋Š” ๊ฒƒ์ด ๊ฐ’์ด์—ˆ๊ณ  ๊ณต๊ณต IT ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ, ์ •๋ถ€์—์„œ ์ปจ์„คํŒ… ์‚ฌ์—… ๋Œ€๊ฐ€ ๋ฐฉ์‹์„ ์ œ๊ณตํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ์ ์šฉํ•˜๋Š”๋ฐ ์ด๋Š” ๋ฏผ๊ฐ„๊ธฐ์—… ์‚ฌ์—… ๋Œ€๊ฐ€ ๋Œ€๋น„ ๋งค์šฐ ๋ฐ•ํ•˜๋‹ค. ๊ทธ๋ž˜๋„ ์ผ๋ฐ˜์ ์ธ ๊ด€์ ์—์„œ ๊ณ„์•ฝ ๋ฐฉ์‹์„ ์ƒ๊ฐํ•ด ๋ณด์ž๋ฉด ์ฒซ ๋ฒˆ์งธ ๋ฝ‘๋Š” ๊ฒƒ์€ T&M[6] ๋ฐฉ์‹ ๊ณ„์•ฝ์ด๋‹ค. ๋‹จ์–ด ๋œป ๊ทธ๋Œ€๋กœ ์‹œ๊ฐ„๋‹น ํˆฌ์ž… ๋น„์šฉ์„ ์ฒญ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. T&M ๊ณ„์•ฝ์€ ๊ณ ์ • ๊ณ„์•ฝ ์š”์†Œ์™€ ์›๊ฐ€์ •์‚ฐ ๊ณ„์•ฝ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ํ”„๋กœ์ ํŠธ ๊ธฐ๊ฐ„์ด ๊ธธ์–ด์ง€๋ฉด ์ฃผ(week)๋‚˜ ์›”(month)๋กœ ์‚ฐ์ •ํ•˜๊ณ  ์‚ฌ๋ฌด์‹ค ๋น„์šฉ ๋“ฑ ๊ฐ„์ ‘๋น„์™€ ์ด์ต ๋“ฑ์„ ํ•ฉํ•˜์—ฌ ์ปจ์„คํŒ… ๋Œ€๊ฐ€๋ฅผ ์ฒญ๊ตฌํ•œ๋‹ค. ๋˜ ๋‹ค๋ฅธ ๋Œ€ํ‘œ์ ์ธ ๊ณ„์•ฝ ๋ฐฉ์‹์€ ๊ณ ์ •๊ณ„์•ฝ(FP[7]) ๋ฐฉ์‹์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด์•ก ๊ณ„์•ฝ(Lump Sum)์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š”๋ฐ ๋ชจ๋“  ์ž‘์—…๋“ค์— ๋Œ€ํ•ด ๊ณ„์•ฝ ๊ธˆ์•ก์„ ํ™•์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” ์„œ๋น„์Šค ๊ณต๊ธ‰์ž๋Š” ๊ณ„์•ฝ์„œ ๋‚ด ์ž‘์—…์ง€์‹œ์„œ(Contract SOW[8])๊ฐ€ ๊ด€์‹ฌ์‚ฌ์˜€๋‹ค. ์„œ๋น„์Šค ๊ณต๊ธ‰์ž๊ฐ€ โ€˜๋ฌด์Šจ ์ผ์„ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋Š”๊ฐ€?โ€™ํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ๊ณ ๊ฐ๊ณผ ๋ช…ํ™•ํ•œ ํ•ฉ์˜๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค.[9] ๊ตญ๋‚ด ๊ณต๊ณต IT ์ปจ์„คํŒ…์˜ ์‚ฌ์—… ๋Œ€๊ฐ€ ์‚ฐ์ • ๋ฐฉ์‹์€ ์ด๋ฅผ ์‘์šฉํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ณผ์—…์— ๋Œ€ํ•œ ์ปจ์„คํŒ…<NAME> ์‚ฐ์ •๊ณผ ์—…๋ฌด ๋‚œ์ด๋„ ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ์กฐ์ •ํ•ด๋„ ์ด ๋Œ€๊ฐ€์˜ ์ƒํ•œ ๊ฐ’์„ ์ดˆ๊ณผํ•  ์ˆ˜ ์—†๊ฒŒ ๋งŒ๋“ค์–ด ๋†“์•˜๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์›๊ฐ€์ •์‚ฐ(CR[9])๋ฐฉ์‹ ๊ณ„์•ฝ๋„ ๋งŽ์ด ์“ฐ์ธ๋‹ค. ์ด ๊ฒฝ์šฐ ์—…๋ฌด ๋ฒ”์œ„๋ฅผ ์ž˜ ์•Œ๊ธฐ ์–ด๋ ค์šธ ๊ฒฝ์šฐ์— ๋งŽ์ด ์ฒด๊ฒฐํ•˜๋Š” ๊ณ„์•ฝ ๋ฐฉ์‹์ธ๋ฐ ์–ด๋–ป๊ฒŒ ๋ณด๋ฉด ๊ตฌ๋งค์ž์˜ ๋ฆฌ์Šคํฌ๊ฐ€ ๊ฐ€์žฅ ํฐ ๊ณ„์•ฝ ๋ฐฉ์‹์ด๋‹ค. ์ผ์ด ์–ธ์ œ ์–ด๋–ป๊ฒŒ ๋๋‚ ์ง€ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•ด์™ธ ์‚ฌ์—… ์ถ”์ง„ ์‹œ ํ˜„์ง€ ์—…๋ฌด ์ง€์›์„ ์œ„ํ•ด ์ปจ์„คํ„ดํŠธ๋ฅผ ๊ณ ์šฉํ•˜๊ธฐ๋„ ํ•˜๋Š”๋ฐ ์ด๋•Œ ์ฒด๊ฒฐํ•˜๋Š” ๊ณ„์•ฝ์€ ๋Œ€๋ถ€๋ถ„ CR ๊ณ„์•ฝ์ด๋‹ค. CR ๊ณ„์•ฝ์˜ ๊ฒฝ์šฐ ์‚ฌํ›„ ์ •์‚ฐ ์‹œ, ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํ•ด๋‹น๊ตญ์˜ ๋…ธ๋™๋ฒ•์ด๋‚˜ ๊ตญ์ œ ๊ด€ํ–‰ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ๋ฌธ์ œ๊ฐ€ ๋˜๋Š”๋ฐ ๊ณ„์•ฝ ํ•ด์ง€ 3๊ฐœ์›” ์ „์˜ ์‚ฌ์ „ ํ†ต๋ณด๋ผ๋“ ๊ฐ€ ์—ฐ๊ธˆ(pension) ๊ฐ™์€ ๊ฒƒ์„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค๋˜๊ฐ€ ํ•˜๋Š” ๋ฌธ์ œ๋“ค๋„ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ํ•ด๋‹น ์ง€์‹์ด ์—†๋‹ค๋ฉด ํ˜„์ง€์˜ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ๋กœํŽŒ(law firm)์ด๋‚˜ ๋ฒ•๋ฅ ์‚ฌ๋ฌด์†Œ์˜ ๋„์›€์„ ๋ฐ›๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ตญ๋‚ด IT ์ปจ์„คํŒ… ๋ถ€๋ถ„์„ ์ข€ ๋” ์‚ดํŽด๋ณด๋ฉด ๊ณต๊ณต๋ถ„์•ผ ๊ฐ€์ด๋“œ๊ฐ€ ๋งค๋…„ ๊ฐฑ์‹ ๋˜๊ณ  ์žˆ๋‹ค. ๋ฌผ๊ฐ€์ง€์ˆ˜ ๋“ฑ์„ ์ง€์†์ ์œผ๋กœ ๋ฐ˜์˜ํ•˜์ง€๋งŒ Top 3 ์ „๋žต ์ปจ์„คํŒ…์‚ฌ์—๊ฒŒ ์ง€๋ถˆํ•˜๋Š” ๋น„์šฉ์—๋Š” ํ•ญ์ƒ ๋ฏธ์น˜์ง€ ๋ชปํ•œ๋‹ค. 2016๋…„ 12์›” ๊ตญ๋‚ด ๊ณต๊ณต IT ์„œ๋น„์Šค ์‚ฌ์—…์„ ์œ„ํ•ด ๋ฐœํ‘œ๋œ ์†Œํ”„ํŠธ์›จ์–ด ์‚ฌ์—… ๋Œ€๊ฐ€ ์‚ฐ์ • ๊ฐ€์ด๋“œ์— ๋”ฐ๋ฅด๋ฉด ์‚ฌ์—… ๋Œ€๊ฐ€ ์‚ฐ์ •์˜ ํ•ต์‹ฌ์€ ์—ฌ์ „ํžˆ ์ปจ์„คํŒ…<NAME>์™€ ํˆฌ์ž… ๊ณต์ˆ˜(MM: Man/Months) ์‚ฐ์ •์— ์˜์กดํ•œ๋‹ค.(Table V-3 ์ฐธ๊ณ ) ์ปจ์„คํŒ…<NAME>๋Š” ์—…๋ฌด๋ณ„ ๊ฐ€์ค‘์น˜์™€ ์—…๋ฌด๋ณ„ ๋‚œ์ด๋„์˜ ๊ณฑ์œผ๋กœ ๊ณ„์‚ฐ๋˜๋Š”๋ฐ ์žฌ์ • ์‚ฌ์—…์˜ ํŠน์ˆ˜์„ฑ์„ ๊ฐ์•ˆํ•˜์—ฌ ์‚ฌ์—… ๋Œ€๊ฐ€์˜ ์ƒํ•œ์ด ์ •ํ•ด์ ธ ์žˆ๋‹ค. Table V-3. ์†Œํ”„ํŠธ์›จ์–ด ์‚ฌ์—… ๋Œ€๊ฐ€ ์‚ฐ์ • - ์ปจ์„คํŒ… ์‚ฌ๋ก€ Break #21. ์ปจ์„คํ„ดํŠธ๋กœ์„œ ๋‚˜์˜ ๋ชธ๊ฐ’์€ ์–ผ๋งˆ์ธ๊ฐ€? ๊ฒฝ์ œํ™˜๊ฒฝ๊ณผ ์‚ฌํšŒ๊ฐ€ ๋ณ€ํ•˜๋ฉด์„œ ํ‰์ƒ์ง์žฅ์˜ ๊ฐœ๋…์€ ์‚ฌ๋ผ์ง€๊ณ  ์ž๊ธฐ๊ฐ€ ๋ฐฐ์šฐ๊ณ  ์ตํžŒ ์ „๋ฌธ์ ์ธ ๊ฒฝํ—˜์„ ์ž๋ฌธํ•˜๋ฉด์„œ ๋ˆ์„ ๋ฒ„๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋งŽ์•„์ง€๊ณ  ์žˆ๋‹ค. ์ปจ์„คํ„ดํŠธ(Consultant) ๋˜๋Š” ์ž๋ฌธ(Advisor) ๋“ค์ด๋‹ค. ์ •๋ง ํŠน์ˆ˜ํ•œ ์˜์—ญ, ์ž์‹ ๋ฐ–์— ๋ชจ๋ฅด๋Š” ์˜์—ญ์˜ ์ปจ์„คํŒ… ๊ฐ€๊ฒฉ์€ ๋ถ€๋ฅด๋Š” ๊ฒƒ์ด ๊ฐ’์ด๊ฒ ์ง€๋งŒ, ์„ธ์ƒ์— ๊ทธ๋Ÿฐ ์˜์—ญ์€ ๋“œ๋ฌผ๊ณ  ๋”ฐ๋ผ์„œ ์˜ค๋Š˜๋‚  ๋Œ€๋ถ€๋ถ„์˜ ์ปจ์„คํŒ… ๊ฐ€๊ฒฉ์€ ๊ทธ ๋‚ด์—ญ์„ ๊ณต๊ฐœํ•˜๋ผ๊ณ  ํ•œ๋‹ค. ์‹œ๋‹ˆ์–ด ์ปจ์„คํ„ดํŠธ๋“ค์€ ์‚ฌ์—… ๋Œ€๊ฐ€๋ฅผ ์‚ฐ์ •ํ•˜๋ฉด์„œ ์ด๋Ÿฐ์ €๋Ÿฐ ๊ณ„์‚ฐ์„ ํ•˜๊ฒŒ ๋˜๋‹ˆ ์ปจ์„คํ„ดํŠธ ๋Œ€๊ฐ€๊ฐ€ ์–ด๋–ป๊ฒŒ ์‚ฐ์ •๋˜๋Š”์ง€ ๋Œ€๊ฐ• ์•Œ ์ˆ˜ ์žˆ๋‹ค.[11] ๊ทธ๋Ÿฌ๋‚˜ ์‹ ์ž… ์ปจ์„คํ„ดํŠธ๋“ค์€ PM ์—ญํ• ์„ ํ•˜์ง€ ์•Š์œผ๋‹ˆ ์ƒ๋Œ€์ ์œผ๋กœ ๊ทธ๋Ÿฐ ๊ฒฝํ—˜์ด ์—†์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ๊ธ€์„ ์ฝ๋Š” ๋…์ž๊ฐ€ ์‹ ์ž… ์ปจ์„คํ„ดํŠธ๋ผ๋ฉด ํ˜น์‹œ ์ด๋Ÿฐ ์ƒ๊ฐ ํ•ด ๋ณธ ์  ์—†๋Š”๊ฐ€? ๋‚˜์˜ ๋Œ€๊ฐ€๋กœ ์ผ์ฒœ๋งŒ ์› ์ด์ƒ ๋งค์›” ์ฒญ๊ตฌํ•ด์„œ ๊ณ ๊ฐ์ด ์ง€๋ถˆํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์•„๋Š”๋ฐ ์™œ ๋‚ด ์›”๊ธ‰์€ ์™œ ์ด๊ฒƒ๋ฐ–์— ์•ˆ๋˜๋Š”๊ฐ€? ๊ทธ๋Ÿฐ ์˜๋ฌธ์ด ๋“ค๊ฑฐ๋‚˜ ๊ทธ๋Ÿฐ ์ƒ๊ฐ์ด ๋“ค์—ˆ๋˜ ์ ์ด ์žˆ๋‹ค๋ฉด ์ด์ œ๋ถ€ํ„ฐ ์ž˜ ์ฝ์–ด๋ณด๊ธฐ ๋ฐ”๋ž€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ž์œผ๋กœ ๋ณธ์ธ์ด ํ”„๋ฆฌ๋žœ์„œ(Freelancer) ์ปจ์„คํ„ดํŠธ๋กœ ๋…๋ฆฝํ•œ๋‹ค๋ฉด ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์˜ ์ผ๋ถ€๋ฅผ ๋” ์ฑ™๊ธธ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์†Œ๊ฐœํ•  ์ปจ์„คํŒ… ๋Œ€๊ฐ€ ๋ชจ๋ธ์€ ์‹œ๊ฐ„๋ณ„ ์š”๊ธˆ์„ ์‚ฐ์ •ํ•˜๋Š” ๊ฒƒ์„ ๊ธฐ๋ณธ์œผ๋กœ ํ•˜๋ฉฐ ๋ชจ๋“  ์ œ์„ธ๊ณต๊ณผ๊ธˆ์€ ๊ฐœ์ธ์ด ๋ถ€๋‹ดํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ•œ๋‹ค. ์ฆ‰, ์˜๋ฃŒ๋ณดํ—˜์„ ํฌํ•จํ•œ ๊ฐ์ข… ์„ธ๊ธˆ ๊ด€๋ จ ์‚ฌํ•ญ์€ ๋ณธ์ธ์ด ์ง์ ‘ ์ฑ™๊ฒจ์•ผ ํ•˜๋ฉฐ, ๊ธฐ์—… ์†Œ์†์œผ๋กœ์„œ ๋ฐ›์•˜๋˜ ๋‚˜๋ฆ„์˜ ๋ณต์ง€ ํ˜œํƒ๋„ ๋ชจ๋‘ ์‚ฌ๋ผ์ง„๋‹ค๋Š” ๊ฒƒ์„ ๋ช…์‹ฌํ•˜์ž. [12] ์ฒซ ๋ฒˆ์งธ, ์‹œ๊ฐ„๋‹น ์š”์œจ์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์–ด๋–ค ์ปจ์„คํ„ดํŠธ๊ฐ€ ํšŒ์‚ฌ ์†Œ์†์ด์—ˆ์„ ๋•Œ ์—ฐ๋ด‰ 6,000๋งŒ ์›์— ์ธ์„ผํ‹ฐ๋ธŒ ๋“ฑ ๊ฐ์ข… ์ˆ˜๋‹น 1,500๋งŒ ์›์„ ๋ฐ›๋˜ ์‚ฌ๋žŒ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜์ž. ์œ ๊ธ‰ํœด๊ฐ€๋Š” 2์ฃผ(14์ผ), ์ ์‹ฌ์‹œ๊ฐ„ ์ œ์™ธ ํ•˜๋ฃจ 8์‹œ๊ฐ„, ์ฃผ 5์ผ ๊ทผ๋ฌดํ•œ๋‹ค๋ฉด ๊ทธ์˜ ์‹œ๊ฐ„๋‹น ๊ธ‰๋ฃŒ๋Š” (A)์ด๋‹ค. (์—ฐ๋ด‰+๊ธฐํƒ€ ์ˆ˜๋‹น)/(50์ฃผ*40์‹œ๊ฐ„) = 3.75๋งŒ ์›/์‹œ๊ฐ„ .... (A) ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ A์˜ 3๋ฐฐ๋ฅผ ์ทจํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, 3.75๋งŒ ์›/์‹œ๊ฐ„ *3 = 11.25๋งŒ ์›/์‹œ๊ฐ„์„ ์†Œ์ˆ˜ ์ฒซ์งธ ์ž๋ฆฌ์—์„œ ์˜ฌ๋ฆผ ํ•œ 12๋งŒ์›๋ฅผ ์‹œ๊ฐ„๋‹น ์š”์œจ๋กœ ์ •ํ•œ๋‹ค. ์ฆ‰, ์ด ์‚ฌ๋žŒ์€ 12๋งŒ ์›/์‹œ๊ฐ„์˜ ์ž„๊ธˆ ์š”์œจ์„ ๊ฐ€์ง„๋‹ค. (A) ๊ฐ’์˜ 3๋ฐฐ๋ฅผ ํ•˜๋Š” ์ด์œ ๋Š” ๋งค์šฐ ์ค‘์š”ํ•œ๋ฐ, ์‹ค์ œ๋กœ 1/3์€ ๋ณธ์ธ์ด ๊ฐ€์ ธ๊ฐ€๋Š” ์‹ค์ œ ์ž„๊ธˆ์ด๊ณ  ๋˜ ๋‹ค๋ฅธ 1/3์€ ๊ฐ์ข… ๋ณดํ—˜์ฒ˜๋ฆฌ, ์„ธ๊ธˆ ๋“ฑ ๋น„์šฉ์œผ๋กœ, ๋งˆ์ง€๋ง‰ 1/3์€ ํ–‰์ •์ด๋‚˜ ์‚ฌ๋ฌด๋น„ํ’ˆ ๊ด€๋ จ ๋น„์šฉ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์‹ค์ œ๋กœ ์‚ฐ์ •๋œ ๊ธˆ์•ก์˜ 2/3๋Š” ๊ณผ๊ฑฐ ์ปจ์„คํŒ… ํšŒ์‚ฌ์— ์†Œ์†๋˜์–ด ์žˆ์—ˆ์„ ๋•Œ ๊ฐ„์ ‘๋น„๋‚˜ ์˜ค๋ฒ„ํ—ค๋“œ ๋น„์šฉ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋˜ ๊ฒƒ๋“ค์ด๋ฉฐ ํ”„๋ฆฌ๋žœ์„œ๊ฐ€ ๋˜์—ˆ๋‹ค๊ณ  ํ•ด์„œ ์ด ๋ถ€๋ถ„์„ ์•ˆ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋ฏ€๋กœ ๊ฐœ์ธ์ ์œผ๋กœ ๊ฐ๋‹นํ•ด์•ผ ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ์ปจ์„คํŒ… ์ผ์ผ ๋น„์šฉ์˜ ๊ณ„์‚ฐ ์•ž์„œ ๊ณ„์‚ฐํ•œ ์‹œ๊ฐ„๋‹น ์š”์œจ์„ ํ™œ์šฉํ•˜์—ฌ ์ผ๋‹น์„ ๊ณ„์‚ฐํ•œ๋‹ค. ํ•˜๋ฃจ 8์‹œ๊ฐ„ ๊ทผ๋ฌด ์‹œ 12๋งŒ ์› * 8 = 96๋งŒ ์›์„ ์ฒญ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์„ธ ๋ฒˆ์งธ, ํ”„๋กœ์ ํŠธ ์ปจ์„คํŒ… ๋น„์šฉ์˜ ์ฒญ๊ตฌ ์œ„์˜ ์‚ฌ๋žŒ์ด ์‹ค์ œ ํ”„๋กœ์ ํŠธ์— ํˆฌ์ž…๋˜๋ฉด ํˆฌ์ž…์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์ฒญ๊ตฌ์„œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•˜๋ฃจ 8์‹œ๊ฐ„ ๊ทผ๋ฌด ๊ธฐ์ค€ 5์ผ ๋™์•ˆ ํˆฌ์ž…๋œ๋‹ค๋ฉด 12๋งŒ ์›/์‹œ๊ฐ„ * 8์‹œ๊ฐ„ * 5์ผ = 480๋งŒ ์›(VAT ํฌํ•จ)์„ ์ฒญ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์‹œ๊ฐ„๋‹น ์š”์œจ ๊ณ„์‚ฐ์€ ์ผ๋ฐ˜์ ์œผ๋กœ 3๋ฐฐ์˜ ๋ฒ•์น™์ด ์ธ์ •๋ฐ›๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋งŒ์•ฝ ๊ณ ์šฉ ์ธก์—์„œ ๊นŽ์•„๋‹ฌ๋ผ๊ณ  ํ•˜๋ฉด ๊ทธ ๋ถ€๋ถ„์„ ์กฐ์ •ํ•ด์•ผ ํ•˜๋Š”๋ฐ ์„ธ๊ธˆ๋„ ํฌํ•จํ•ด์„œ ๋‹ค์–‘ํ•œ ๋น„์šฉ์„ ๊ฐ๋‹นํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ž˜ ๊ณ ๋ฏผํ•ด์„œ ํ˜‘์ƒํ•ด์•ผ ํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ ์ปจ์„คํŒ… ๋น„์šฉ๊ณผ ๊ด€๋ จํ•˜์—ฌ ์˜๋ฏธ๊ถŒ์—์„œ๋Š” ๋ฌผ๋ฆฌํ•™์ž Niels Bohr์™€ ๊ด€๋ จ๋œ ์žฌ๋ฏธ๋‚œ ์กฐํฌ๊ฐ€ ์ „ํ•ด์˜ค๋Š”๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํšŒ์‚ฌ์˜ ๊ธฐ๊ณ„๊ฐ€ ๊ณ ์žฅ ๋‚ฌ๋Š”๋ฐ Niels Bohr์˜ ์˜› ํ•™๊ต ์นœ๊ตฌ์ธ ํšŒ์‚ฌ ์†Œ์œ ์ฃผ๋Š” ๊ทธ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Niels Nohr๋ฅผ ํšŒ์‚ฌ๋กœ ์ดˆ๋น™ํ•œ๋‹ค. Niels Bohr๋Š” ๊ธฐ๊ณ„๋ฅผ ์ž ์‹œ ์‚ดํŽด๋ณด๋”๋‹ˆ ๊ธฐ๊ณ„ ์˜†์— 'X' ํ‘œ์‹œํ•˜๊ณ  ๋ง์น˜๋กœ ๊ทธ ๋ถ€๋ถ„์„ ์„ธ๊ฒŒ ๋‘๋“œ๋ฆฌ๋ผ๊ณ  ๋งํ•˜๊ณ  ๋Œ์•„๊ฐ„๋‹ค. ๋‚˜์ค‘์— ์ฒญ๊ตฌ์„œ๋ฅผ ๋ฐ›์•„๋ณธ ์นœ๊ตฌ๋Š” ๊นœ์ง ๋†€๋ผ Niels Bohr์—๊ฒŒ ์ „ํ™”๋ฅผ ๊ฑธ์—ˆ๋‹ค. "Niels, ์ž๋„จ 5๋ถ„ ์ •๋„๋ฐ–์— ์žˆ์ง€ ์•Š์•˜๋Š”๋ฐ $10,000 ์ฒญ๊ตฌ์„œ๋Š” ๋ฌด์—‡์ธ๊ฐ€? ์ž์„ธํ•œ ๋‚ด์—ญ์„ ๊ฐ™์ด ์•Œ๋ ค์ฃผ๊ฒŒ!" Niels Bohr๋Š” ์•Œ์•˜๋‹ค๊ณ  ํ•˜๊ณ  ์ƒ์„ธ ๋‚ด์—ญ์ด ๋‹ด๊ธด ์ฒญ๊ตฌ์„œ๋ฅผ ๋‹ค์‹œ ๋ณด๋ƒˆ๋‹ค. ์นœ๊ตฌ๋Š” ์ƒˆ๋กœ์šด ์ฒญ๊ตฌ์„œ๋ฅผ ์—ด์–ด๋ณด์•˜๋”๋‹ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค. INVOICE Drawing X on the side of your machine $ 1 Knowing where to put the X $ 9,999 โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” Total $10,000 ์ฒญ๊ตฌํ•œ ๊ทธ๋Œ€๋กœ ๋ˆ์„ ๋ฐ›์•˜๋Š”์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ปจ์„คํ„ดํŠธ๋Š” ์ž์‹ ์ด ํ•˜๋Š” ์ผ์˜ ๊ฐ€์น˜๋ฅผ ์•Œ์•„์•ผ ์ž์‹ ์˜ ๋Œ€๊ฐ€๋ฅผ ์ œ๋Œ€๋กœ ์ •์˜ํ•˜๊ณ  ์ฒญ๊ตฌํ•  ์ˆ˜ ์žˆ๊ณ  ๊ทธ๊ฒƒ์€ ์„œ๊ตฌ ์‚ฌํšŒ์—์„œ ์ธ์ •๋ฐ›๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋„ค ๋ฒˆ์งธ, ์„ฑ๊ณผ๊ธ‰ ๊ธฐ๋ฐ˜ ๋Œ€๊ฐ€ ์–ด๋–ค ๊ณ ๊ฐ์€ ์‹œ๊ฐ„๋‹น ์š”์œจ์„ ์ธ์ •ํ•˜์ง€ ์•Š๊ณ  ์ปค๋ฏธ์…˜(commission)์„ ํฌํ•จํ•œ ์„ฑ๊ณผ๊ธ‰์„ ์ œ์•ˆํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๊ฒฝ์šฐ ์œ„ํ—˜ ์š”์†Œ๋„ ์žˆ๋Š”๋ฐ ๊ธฐ์—…์˜ ์„ฑ๊ณผ์ธก์ • ์ด์Šˆ๊ฐ€ ๊ณ ์Šค๋ž€ํžˆ ๋”ฐ๋ผ์˜จ๋‹ค. ์ฆ‰, ์ง€ํ‘œ์˜ ์ •์˜ ๋ฐ ์ธก์ •, ์„ฑ๊ณผํ‰๊ฐ€ ๊ธฐ์ค€ ์‹œ์  ๋“ฑ์„ ์–ด๋–ป๊ฒŒ ์ •์˜ํ•˜๋Š๋ƒ๊ฐ€ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ์„ฑ๊ณผ๊ธ‰์˜ ๊ธ์ •์ ์ธ ๋ถ€๋ถ„๋งŒ ๋ฏฟ๊ณ  ๊ธฐ๋ณธ ๋Œ€๊ฐ€ ์ฒญ๊ตฌ ์—†์ด ๊ณ ๊ฐ์—๊ฒŒ ๋Œ๋ ค๋‹ค๋‹ ์ˆ˜๋„ ์žˆ๊ณ  ๋ณ€ํ™”๊ด€๋ฆฌ๋ฅผ ์ž˜ ํ•ด์•ผ ํ•  ์ƒํ™ฉ์ด๋‹ค. ๋‹ค์„ฏ ๋ฒˆ์งธ, ์‹ค์  ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•œ ์ „๋žต์  ์ฒญ๊ตฌ ์ด ๋ถ€๋ถ„์€ ์‹œ๊ฐ„๋‹น ์š”์œจ์„ ์ข€ ๋” ์ •๋ฐ€ํ•˜๊ฒŒ ๊ณ„์‚ฐํ•˜์—ฌ ๊ธฐ๋ณธ๊ธ‰ ํ˜•ํƒœ๋กœ ์ฒญ๊ตฌํ•˜๊ณ  ๋‚˜๋จธ์ง€ ๋น„์šฉ์€ ์‚ฌํ›„ ์ •์‚ฐ(Reimbursement)์˜<NAME>์„ ๊ฐ–๋Š” ์ฒญ๊ตฌ ๋ฐฉ์‹์œผ๋กœ ์ €์ž๋„ ์ผํ•˜๋ฉด์„œ ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ์ฒญ๊ตฌํ•˜๋Š” ์ปจ์„คํ„ดํŠธ๋“ค์„ ๊ณ ์šฉํ•ด ๋ณด์•˜๊ณ  ํ”„๋ฆฌ๋žœ์„œ ์ปจ์„คํ„ดํŠธ๋“ค์ด ๊ฐ€์žฅ ์„ ํ˜ธํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ๋‚ด์šฉ์„ ์•Œ๊ธฐ ์œ„ํ•ด ์ฒซ ๋ฒˆ์งธ ์–ธ๊ธ‰ํ–ˆ๋˜ ๋‚ด์šฉ๋“ค์„ ์กฐ๊ธˆ ๋” ์ •๊ตํ™”ํ•ด ๋ณด์ž. (์ด ๊ฒฝ์šฐ, 3๋ฐฐ์ˆ˜ ์›์น™์€ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค) [Step #1. ์‹œ๊ฐ„๋‹น ์š”์œจ ์ •๊ตํ™”] 1๋…„ 52์ฃผ๋ฅผ ํœด์ผ, ํœด๊ฐ€, ๋ณ‘๊ฐ€ ๋“ฑ์˜ ํ•ฉํ•ด์„œ 6์ฃผ ์ •๋„๋ฅผ ์ œ์™ธํ•˜๊ณ  46์ฃผ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•œ๋‹ค. ์ฃผ 5์ผ 8์‹œ๊ฐ„ ๊ทผ๋ฌด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋ฉด ์ผ ๋…„์€ 46์ฃผ * 5์ผ * 8์‹œ๊ฐ„ = 1,840์‹œ๊ฐ„์„ ์ผํ•  ์ˆ˜ ์žˆ๋‹ค..... (B) [Step #2. ์ฒญ๊ตฌ ๊ฐ€๋Šฅ ์‹œ๊ฐ„์˜ ์‚ฐ์ •] 1,840์‹œ๊ฐ„์„ ์‚ฐ์ถœํ•˜์˜€์ง€๋งŒ ์‹ค์ œ ๊ทธ ์‹œ๊ฐ„์„ ๋ชจ๋‘ ์ผํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋˜ํ•œ, ์–ด๋–ค ์‹œ๊ฐ„์€ ์—…๋ฌด์™€ ๊ด€๊ณ„์—†๋Š” ์ผ์— ์‹œ๊ฐ„์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ด€๋ฆฌ, ์„œ๋ฅ˜ ์ค€๋น„ ๋“ฑ ๋ถ€์™ธ ์žก์ผ์— (B) ์‹œ๊ฐ„์˜ 20%, ๋งˆ์ผ€ํŒ…/๋„คํŠธ์›Œํ‚น, ์ผ์„ ๊ตฌํ•˜๋Š”๋ฐ (B) ์‹œ๊ฐ„์˜ 20%, ์ผ๊ณผ ๊ด€๋ จ ์—†๋Š” ์ผ์— (B) ์‹œ๊ฐ„์˜ 10%๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์‹ค์ œ ์—…๋ฌด์™€ ๊ด€๋ จ๋œ ์‹œ๊ฐ„์€ 1,840 * 50% = 920์‹œ๊ฐ„์ด๋‹ค. ..... (C) [Step #3. ์ตœ๊ณ ์˜ ํšŒ์ˆ˜์œจ ๊ณ ๋ ค] ์„ ์˜๋ฅผ ๋ณด์—ฌ ์ฒญ๊ตฌ ๊ฐ€๋Šฅ ์‹œ๊ฐ„์„ 920์‹œ๊ฐ„์œผ๋กœ ํ–ˆ์ง€๋งŒ ๋ชจ๋“  ๊ณ ๊ฐ์ด ์ด๋ฅผ ์ธ์ •ํ•˜์ง€๋Š” ์•Š์„ ๊ฒƒ์ด๋ฏ€๋กœ ๋ช…ํ™•ํ•œ ์ˆ˜์ˆ˜๋ฃŒ ์ฑ…์ •์„ ์œ„ํ•ด ์ด ๋ถ€๋ถ„ 5% ์ •๋„๋ฅผ ๊ฐ์•ˆํ•˜๋ฉด 920 * 95% = 874์‹œ๊ฐ„. .... (D). ์‹ค์งˆ์ ์œผ๋กœ ์ผ ๋…„ ์ค‘ ์—…๋ฌด์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ฒญ๊ตฌ ๊ฐ€๋Šฅ ์‹œ๊ฐ„์€ (D)๊ฐ€ ๋œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•ž์„œ ์–˜๊ธฐํ•œ ์—ฐ๋ด‰ 6,000๋งŒ ์›, ์ˆ˜๋‹น 1,500๋งŒ ์›์„ ๋ฐ›๋Š” ์‚ฌ๋žŒ์„ ์ƒ๊ฐํ•ด ๋ณด๋ฉด (6,000 + 1,500)/874์‹œ๊ฐ„ = 8.58๋งŒ ์›/์‹œ๊ฐ„ .... (E) [Step #4. ๊ฐ„์ ‘๋น„ ๊ณ ๋ ค] Step #3์—์„œ ์‚ฐ์ •๋œ ๊ธˆ์•ก์ด ๋ชจ๋‘ ์ˆœ์ด์ต์ด ๋˜๋Š” ์ปจ์„คํŒ… ์‚ฌ์—…์ด๋ผ๋ฉด ๊ด€๊ณ„์—†์ง€๋งŒ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ, ๋‹ค์–‘ํ•œ ๊ฐ„์ ‘๋น„ ํ•ญ๋ชฉ์„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ํ•ญ๋ชฉ๋“ค์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. (์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜์˜€๋‹ค) - ์‚ฌ๋ฌด์‹ค ์ž„๋Œ€๋ฃŒ ๋˜๋Š” ๋ชจ๊ธฐ์ง€ ์ด์ž - ์‚ฌ๋ฌด์‹ค ์ง‘๊ธฐ - ์ธํ„ฐ๋„ท ๋“ฑ ํ†ต์‹  ๋น„์šฉ - ๋…ธํŠธ๋ถ ๋˜๋Š” ๋ฐ์Šคํฌํ†ฑ ์ปดํ“จํ„ฐ - ํ”„๋ฆฐํ„ฐ - ๋ฐฐ์†ก ๋ฐ ์šฐ์†ก๋ฃŒ - ํ”„๋ฆฐํ„ฐ ํ† ๋„ˆ ๋ฐ ์ž‰ํฌ - ์ข…์ด, ๋ฌธ๋ฐฉ๊ตฌ ๋“ฑ ์‚ฌ๋ฌด์šฉํ’ˆ - ๋ช…ํ•จ - ํšŒ๊ณ„ ๋ฐ ๋ฒ•๋ฅ ์„œ๋น„์Šค - ์‚ฌ๋ฌด์‹ค ๊ฐ€๊ตฌ - ์ฑ…์ƒ, ์˜์ž, ์„ ๋ฐ˜, ์บ๋น„๋‹›, ์กฐ๋ช… ๋“ฑ - ์‚ฌ์—… ๋ฉดํ—ˆ ๋ฐ ํ—ˆ๊ฐ€ - ๋ณดํ—˜ - ๊ฑด๊ฐ•, ์ƒ๋ช…, ์žฅ์• , ์ฑ…์ž„ ๋“ฑ - ์ž๋™์ฐจ - ๋ณดํ—˜, ์ •๋น„, ๊ฐ€์Šค, ์ž„๋Œ€ - ๊ด‘๊ณ  ๋ฐ ๋งˆ์ผ€ํŒ… - ๊ตฌ๋… - ์ „๋ฌธ ํ˜‘ํšŒ - ์ „๋ฌธ์ ์ธ ๋ชฉ์ ์„ ์œ„ํ•œ ์‹์‚ฌ ๋ฐ ์ ‘๋Œ€ - ํ‰์ƒ ๊ต์œก - ์ „๋ฌธ ํšŒ์˜, ์ฝ˜ํผ๋Ÿฐ์Šค ๋ฐ ์ „์‹œํšŒ - ์ฒญ์†Œ์šฉํ’ˆ ๋ฐ ์ฒญ์†Œ ์„œ๋น„์Šค ๊ทธ ์™ธ ๋” ๋งŽ์€ ํ•ญ๋ชฉ๋“ค์ด ์˜ค๋ฒ„ํ—ค๋“œ ๋น„์šฉ์œผ๋กœ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ช‡๋ช‡ ์„œ๋น„์Šค๋Š” ๊ฐœ์ธ์ด ์ฒ˜๋ฆฌํ•œ๋‹ค ํ•ด๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋น„์šฉ์ด ์†Œ์š”๋˜๋ฏ€๋กœ ์ด๋ฅผ ์ž˜ ๊ณ„์‚ฐํ•ด์•ผ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์˜ˆ๋ฅผ ๋“ค์–ด 2,000๋งŒ ์›์ด ์‚ฌ์šฉ๋œ๋‹ค๊ณ  ํ•ด๋ณด์ž. (ํšŒ์‚ฌ์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด์ง€๋งŒ ์˜ค๋ฒ„ํ—ค๋“œ ๋น„์šฉ์„ ์ ˆ์•ฝํ•˜๋ฉด ์ด์ต ์ฐฝ์ถœ์— ์ ์ง€ ์•Š์€ ๋„์›€์ด ๋œ๋‹ค). ์ด ๋น„์šฉ์„ ์ฒญ๊ตฌ ๊ฐ€๋Šฅ ์‹œ๊ฐ„์œผ๋กœ ๋‚˜๋ˆ„๋ฉด 2,000๋งŒ ์› / 874์‹œ๊ฐ„ = 2.28๋งŒ ์›/์‹œ๊ฐ„ ... (F) ํ”„๋ฆฌ๋žœ์„œ ์ปจ์„คํ„ดํŠธ๊ฐ€ ์ฒญ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜์ˆ˜๋ฃŒ๋Š” (E) + (F) = 8.58๋งŒ ์›/์‹œ๊ฐ„ + 2.28๋งŒ ์›/์‹œ๊ฐ„ = 10.86๋งŒ ์›/์‹œ๊ฐ„ ... (G)๊ฐ€ ์‚ฐ์ถœ๋œ๋‹ค. ํ”„๋ฆฌ๋žœ์„œ ์ปจ์„คํ„ดํŠธ๋Š” ๋˜ ์ผ์„ ์–ธ์ œ ์–ป๊ฒŒ ๋ ์ง€ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฐ ์œ„ํ—˜์š”์†Œ ๋ถ€๋‹ด์„ ์—†์• ๊ธฐ ์œ„ํ•ด ์ˆ˜์ˆ˜๋ฃŒ์— ์ด์ต์„ 10% ~ 33% ์ •๋„ ๋ถ€๊ณผํ•œ๋‹ค. ์ฒซ ๊ณ„์•ฝ์ด ๋  ๊ฒƒ์ด๋‹ˆ 10%๋งŒ ์ƒ๊ฐํ•ด ๋ณธ๋‹ค๋ฉด 10.86๋งŒ ์›/์‹œ๊ฐ„ * 1.1 = 11.94๋งŒ ์›/์‹œ๊ฐ„ ... ์†Œ์ˆ˜์  ์˜ฌ๋ฆผ ํ•˜์—ฌ 12๋งŒ ์›/์‹œ๊ฐ„ ... (H)๊ฐ€ ์‹œ๊ฐ„๋‹น ์š”์œจ์ด ๋œ๋‹ค. ์•ž์„œ ์†Œ๊ฐœํ•œ 3๋ฐฐ์˜ ๋ฒ•์น™์—์„œ ๋Œ€๋žต ์‚ฐ์ •ํ•œ 12๋งŒ ์›๊ณผ ๊ฐ™์•„์กŒ๋‹ค. ์ผ๋ถ€๋Ÿฌ ๊ฐ™๊ฒŒ ๋งŒ๋“  ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ทธ๋ ‡๊ฒŒ ๋˜์—ˆ๋‹ค. ์‹ค์ œ ์ฒญ๊ตฌ๋‚ด์—ญ์€ ์ด๋ ‡์ง€๋งŒ ๋ณธ์ธ์ด ์˜ค๋ฒ„ํ—ค๋“œ ๋น„์šฉ ๋ถ€๋ถ„์„ ์ ˆ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ˆœ์ต์€ ๋†’์•„์ง€๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ์œ„์˜ ๊ธ€์„ ์ˆœ์„œ๋Œ€๋กœ ์ฃฝ ๋”ฐ๋ผ์™”๋‹ค๋ฉด ์ด๊ฑด ๋งˆ์น˜ ๊ธฐ์—…์˜ ์›๊ฐ€๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฑฐ์˜ ํก์‚ฌํ•˜์ง€ ์•Š์€๊ฐ€? ๊ทธ๋ ‡๋‹ค. ๊ฐ™์€ ์ ˆ์ฐจ๋ฅผ ๋ฐŸ์•˜๋‹ค. ์ปจ์„คํŒ… ์ˆ˜์ˆ˜๋ฃŒ๊ฐ€ ์›๊ฐ€์ด๊ณ  ๊ฑฐ๊ธฐ์— ์ด์ต์„ ๋ถ™์ธ ๊ฒƒ์ด๋‹ค. ํ•œ ๊ฐ€์ง€ ๋” ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์ด๋Š” ์„œ๋น„์Šค ์š”๊ธˆ์ด์ง€๋งŒ ๊ทธ ๊ทผ๋ณธ ์‚ฌ์ƒ์ด Head Count ๋ฐฉ์‹๊ณผ ๊ฐ™๋‹ค. ์ฆ‰, ์—ฌ๋Ÿฌ๋ถ„ ์ž์‹ ์ด 100% ์ผ์— ๋ชฐ๋‘ํ•ด์•ผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ˆ์ด๋‹ค. ๊ธฐ์—…์€ ์–ด๋–ค๊ฐ€? ๊ทธ๋Ÿฐ ๋ถ€๋ถ„ ํ˜์‹ ์„ ์œ„ํ•ด ์„ค๋ฃจ์…˜ ๋น„์ฆˆ๋‹ˆ์Šค๋กœ ์ „ํ™˜ํ•˜๋ฉด์„œ ์ œํ’ˆ์˜ ๋‹จ๊ฐ€๋ฅผ ํ•œ ๋ฒˆ์— ๋‹ค ๋ฐ›๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  ์„œ๋น„์Šค๋กœ ์ฒญ๊ตฌํ•œ๋‹ค. ์ฆ‰, ์„ค๋ฃจ์…˜ ๋น„์ฆˆ๋‹ˆ์Šค์˜ ๊ฐ€๊ฒฉ ๋ชจ๋ธ์„ ์ž˜ ๋ฒค์น˜๋งˆํ‚นํ•˜๋ฉด ์ปจ์„คํŒ… ์ˆ˜์ˆ˜๋ฃŒ๋„ ๋‹ค๋ฅด๊ฒŒ ์ฒญ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ง์ด๋‹ค. ๋ฉ€ํ‹ฐ ํ”„๋กœ์ ํŠธ๋ฅผ ๋›ธ ์ˆ˜ ์žˆ์„๊นŒ? ๋Œ€๊ฐ€๋Š” ๋” ๋น„์‹ธ๊ฒŒ ์ฒญ๊ตฌํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ˆ™์ œ๋ผ ์ƒ๊ฐํ•˜์ž. ์—ฌ์„ฏ ๋ฒˆ์งธ, ์‚ฌํ›„ ์ •์‚ฐ ์ฒ˜๋ฆฌ ์ปจ์„คํŒ… ๊ณ„์•ฝ์ด ์ฒด๊ฒฐ๋œ ํ›„์—๋„ ์ผํ•˜๋‹ค ๋ณด๋ฉด ๋‚˜์˜ ์ฒญ๊ตฌ๋‚ด์—ญ๊ณผ ๊ด€๊ณ„์—†๋Š” ๊ทธ๋Ÿฌ๋‚˜ ์ผํ•˜๋ฉด์„œ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๋น„์šฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ €์ž์˜ ๊ฒฝ์šฐ, ํ•ด์™ธ ์ฝ˜ํผ๋Ÿฐ์Šค์— ๊ธ‰ํ•˜๊ฒŒ ๊ฐ€์•ผ ํ–ˆ๋Š”๋ฐ ๋ฏธ์ฒ˜ ๋“ฑ๋ก์„ ๋ชปํ•ด์„œ ์ปจ์„คํ„ดํŠธ๊ฐ€ ๋Œ€์‹  ๋“ฑ๋กํ•ด ์ฃผ๊ณ  ๋น„์šฉ์„ ์ง€๋ถˆํ•˜์˜€๋‹ค. ๋ฌผ๋ก , ๊ทธ๋Š” ๋‚˜์ค‘์— ์ด๊ฒƒ๋„ ์˜์ˆ˜์ฆ๊ณผ ๊ฐ™์ด ์ฒญ๊ตฌํ•˜์˜€๊ณ  ๊ทธ๊ฒƒ์„ ์ •์‚ฐํ•ด ์ฃผ์—ˆ๋‹ค.(Reimbursement) ์ฆ‰, ์ปจ์„คํŒ… ๊ณ„์•ฝ ์‹œ ์ œ์‹œ๋œ ์ฒญ๊ตฌ ๋‚ด์—ญ์ด ์•„๋‹ˆ๋”๋ผ๋„ ์–ผ๋งˆ๋“ ์ง€ ์ƒํ˜ธ ๊ฐ„์˜ ๋น„์šฉ ์ •์‚ฐ์„ ํ•ด์ค„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ผ๊ณฑ ๋ฒˆ์งธ, '๋‚˜์˜ ๋ชธ๊ฐ’์€ ์–ผ๋งˆ์ธ๊ฐ€?'๋ผ๋Š” ๋‹ค์†Œ ๋„๋ฐœ์ ์ธ ์งˆ๋ฌธ์— ๋Œ€ํ•ด ์ด์ œ ๋‹ต์„ ํ•ด๋ณด๋ฉด ์ข…๊ต์ , ์ฒ ํ•™์  ์˜๋ฏธ๋ฅผ ๋ฐฐ์ œํ•˜๊ณ  ์ฒ ์ €ํ•˜๊ฒŒ ์‹œ์žฅ ์›๋ฆฌ๋กœ ์ž์‹ ์ด ํ˜„์žฌ ์–ผ๋งˆ์งœ๋ฆฌ์ธ์ง€ ๊ณ„์‚ฐํ•ด ๋ณผ ํ•„์š”๋Š” ์žˆ๋‹ค. ๊ธฐ์—…์— ์žˆ์—ˆ์„ ๋•Œ ์—ฐ๋ด‰์ด ๋†’์•˜๋‹ค๋ฉด ๋‹น์—ฐํžˆ ์ปจ์„คํ„ดํŠธ๋กœ ์ผํ•  ๋•Œ๋„ ๊ทธ ์ˆ˜์ค€ ๋˜๋Š” ์ด์ƒ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ ํ”„๋ฆฌ๋žœ์„œ๊ฐ€ ์•„๋‹ˆ๋ผ ํ˜„์žฌ ๊ธฐ์—…์— ๋ชธ๋‹ด๊ณ  ์žˆ๋Š” ์‚ฌ๋žŒ์ด๋ผ๋ฉด ํ˜„์žฌ์— ์ถฉ์‹คํ•จ์ด ํ–ฅํ›„ ๋ชธ๊ฐ’์—๋„ ์˜ํ–ฅ์„ ์ค€๋‹ค๋Š” ์‚ฌ์‹ค์„ ์žŠ์ง€ ๋ง์ž. ํ”„๋ฆฌ๋žœ์„œ ์ปจ์„คํ„ดํŠธ๋ผ๋ฉด ์˜ค๋ฒ„ํ—ค๋“œ ๋น„์šฉ์€ ๊ฐ€๋Šฅํ•˜๋ฉด ์ค„์ด๊ณ , ํ˜„์žฌ ์ผ์˜ ์„ฑ๊ณผ๋ฅผ ๊ณ ๊ฐ์—๊ฒŒ ์ธ์ •๋ฐ›์•„ ์žฌ๊ณ„์•ฝ ์‹œ์—๋Š” ์ด์ต๋ฅ ์„ 10% ์ด์ƒ ์ฒญ๊ตฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•˜์ž! [13] [1] www.cebglobal.com [2] ์ปจ์„คํŒ…์„ ์ž˜ ๋ชจ๋ฅด๋Š” B2B ์˜์—…๋Œ€ํ‘œ๊ฐ€ ์ปจ์„คํŒ… ์‚ฌ์—…์„ ์ฃผ๋„ํ•  ์ˆ˜๋Š” ์—†์—ˆ์œผ๋ฏ€๋กœ ์ปจ์„คํ„ดํŠธ์™€ ๊ฐ™์ด ์‚ฌ์—…๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•˜์˜€๊ณ  ์ด๋Š” ๋น„์šฉ ํšจ์œจ์  ๊ด€์ ์—์„œ ๊ฒฝ์Ÿ๋ ฅ ์ด์Šˆ๊ฐ€ ์ œ๊ธฐ๋˜์—ˆ๋‹ค. [3] ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•œ ํ‰๊ฐ€๋Š” ์ €์ž๊ฐ€ ์ปจ์„คํ„ดํŠธ๋กœ ์ผํ•˜๋˜ ๋•Œ๋‚˜ ์ง€๊ธˆ์ด๋‚˜ ํž˜๋“ค์ง€๋งŒ ์‚ฌ์—…์„ ๋ฐœ๊ตดํ•˜๋Š” ์—ญ๋Ÿ‰์„ ํ‚ค์šธ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ชฝ๊ณผ ๊ณผ๋„ํ•œ ๋…ธ๋™๋Ÿ‰์œผ๋กœ ์ปจ์„คํ„ดํŠธ์˜ ์ƒ๋ช…(๋ฌผ๋ฆฌ์ ์ธ ์ƒ๋ช…)์ด ๋” ์งง์•„์ง„๋‹ค๋Š” ์ชฝ์œผ๋กœ ์–‘๋ถ„๋˜๊ณ  ์žˆ๋‹ค [4] ์‚ฌ๊ณ  ๋ฆฌ๋”์‹ญ(Thought Leadership) ํ™œ๋™์„ ํฌํ•จํ•œ ๋งˆ์ผ€ํŒ… ํ™œ๋™์ด ๊ฒฝ์˜์ง„๋“ค์—๊ฒŒ ์–ดํ•„๋œ๋‹ค๋ฉด ์ž…์ฐฐ ์—†์ด ์‚ฌ์—…์„ ์ˆ˜์ฃผํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์•˜๋‹ค. ์ด๋Š” ์‹œ์žฅ ๋ฆฌ๋”์‹ญ(Market Leadership) ํ™•๋ณด์™€ ๋ณ„๊ฐœ๋กœ ์‚ฌ์—… ์ธก๋ฉด์˜ ์‹ค์†์„ ๊ฑฐ๋‘˜ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ Top 3 ์ „๋žต ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค ๋“ฑ ์ด ์˜ค๋ž˜์ „๋ถ€ํ„ฐ ์‹œ๋„ํ•ด์™”๋˜ ๋ฐฉ๋ฒ•์ด๋‹ค. [5] Non-Disclosure Agreement [6] Time and Materials Contract [7] Fixed Price [8] Statements of Work ์ž‘์—…์ง€์‹œ์„œ [9] ์‚ฌ์‹ค ์ด ๋ถ€๋ถ„์€ ์ด์Šˆ๊ฐ€ ์ฒจ์˜ˆํ•˜๋‹ค. ์ž˜ ์ง€์ผœ์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ์—์„œ ๋ฒ”์œ„ ์ดˆ๊ณผ๋Š” ํ•ญ์ƒ ์žˆ๋Š” ์ผ์ด๋ฉฐ ๊ณ ๊ฐ ๊ด€๊ณ„๋ฅผ ์œ„ํ•ด ๋Œ€๋ถ€๋ถ„์˜ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ๊ทธ๊ฒƒ์„ ์ˆ˜์šฉํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌด์—‡์ด Win-win ๋ฐฉ์•ˆ์ธ์ง€๋Š” ๊ณ ๊ฐ๊ณผ ์ปจ์„คํŒ… ๊ธฐ์—…์ด ๊ฐ™์ด ๊นŠ๊ฒŒ ๊ณ ๋ฏผํ•ด์•ผ ํ•œ๋‹ค. [10] Cost Reimbursable [11] ํšŒ์‚ฌ๋งˆ๋‹ค ๊ด€๋ฆฌํšŒ๊ณ„ ๊ธฐ์ค€์ด ๋‹ค๋ฅด๋ฏ€๋กœ ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅด๋‹ค. [12] 'http://consultantjournal.com/blog/setting-consulting-fee-rates'์˜ ๋‚ด์šฉ๊ณผ ๊ฐœ์ธ ๊ฒฝํ—˜์„ ํ† ๋Œ€๋กœ ์žฌ์ •๋ฆฌํ•˜์˜€๋‹ค. [13] ์‚ฌ์‹ค ์ปจ์„คํŒ… ๊ณ„์•ฝ ์‹œ ์ฒญ๊ตฌ๋‚ด์—ญ์„ ์ƒ์„ธํ•˜๊ฒŒ ์ด์ต๋ฅ ๊นŒ์ง€ ์ ์–ด๋‚ด๋ผ๊ณ  ํ•˜๋Š” ๊ณ ๊ฐ์€ ์—†๋‹ค. ์ž์„ธํ•œ ๋‚ด์—ญ์„ ๋ฌป์ง€ ์•Š๋Š” ๊ณ ๊ฐ๋„ ๋งŽ๋‹ค. ๊ทธ๊ฑด ์ผ์ข…์˜ ๋ถˆ๋ฌธ์œจ์ด๊ณ  ์‹œ๊ฐ„๋‹น ์š”์œจ์ด ๋น„์‹ธ๋ฉด ๊ณ„์•ฝํ•˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ์ปจ์„คํ„ดํŠธ๋ฅผ ์ฐพ์„ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ปจ์„คํŒ…์˜ ์†์„ฑ ์ƒ, ๊ทธ ์‚ฌ๋žŒ ์•„๋‹ˆ๋ฉด ์•ˆ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„์„œ ๊ณ„์•ฝํ•˜๋ ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ปจ์„คํ„ดํŠธ ๊ฐœ์ธ ์ž…์žฅ์—์„œ๋Š” ์–ด๋–ป๊ฒŒ ๋‚ด์—ญ์„ ๋งŒ๋“ค์–ด ์ฒญ๊ตฌํ•  ๊ฒƒ์ธ์ง€ ์ •๋„๋Š” ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•˜๊ธฐ์— ํ•˜๋Š” ์–˜๊ธฐ์ด๋‹ค. 19. ์„ฑ๊ณตํ•˜๋Š” ์ปจ์„คํŒ… ์‚ฌ์—… ์ œ์•ˆ 19.2 ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ๊ตฌ์„ฑ ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์€ ์ œ์•ˆ์„œ๋ฅผ ์‹ฌ์‚ฌํ•ด์•ผ ํ•˜๋Š” ํ‰๊ฐ€ ์œ„์›๋“ค ์ž…์žฅ์—์„œ๋Š” ๋ฐฉ๋Œ€ํ•œ ์ œ์•ˆ์„œ๋ฅผ ์ฝ๊ธฐ ์ „์— ์ฃผ์š” ์‚ฌํ•ญ์„ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์•Œ๊ธฐ ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด ์ค€๋‹ค๋Š” ์ธก๋ฉด์—์„œ ๋งค์šฐ ์œ ์šฉํ•œ ์ž๋ฃŒ๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋…ผ๋ฆฌ์ ์ด๊ณ  ๊ตฌ์กฐ์ ์ด๋ฉฐ ๋˜ ๊ฐ๋™๊นŒ์ง€ ์ฃผ์–ด์•ผ ํ•˜๋Š” ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ์›์กฐ๋ฅผ ์ฐพ์•„๋ณด๋ฉด ๊ทธ๋ฆฌ์Šค์˜ ์ฒ ํ•™์ž ์•„๋ฆฌ์Šคํ† ํ…”๋ ˆ์Šค(Aristotle. BC 384 ~ BC 322)๊นŒ์ง€ ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ๋…ผ๋ฆฌ์  ๊ตฌ์กฐ์™€ ์Šคํ† ๋ฆฌํ…”๋ง์„ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๋…ผ๋ฆฌํ•™ (Logic)๊ณผ ์‹œํ•™(Poetica)์„ ๋งŒ๋“  ์•„๋ฆฌ์Šคํ† ํ…”๋ ˆ์Šค์•ผ๋ง๋กœ ์ตœ๊ณ ์˜ ํ”„๋ ˆ์  ํ„ฐ(presenter)์˜€์„ ๋ฒ•ํ•˜๋‹ค. ๋Œ€์ค‘์—ฐ์„ค๊ณผ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์€ ์ฒญ์ค‘์„ ๋Œ€์ƒ์œผ๋กœ ํ•œ๋‹ค๋Š” ์ ์€ ๊ฐ™์ง€๋งŒ ๊ทธ<NAME>์ด๋‚˜ ๋ฐฉ๋ฒ•์€ ์ข€ ๋‹ค๋ฅธ๋ฐ, ํŒŒ์›Œํฌ์ธํŠธ์™€ ๊ฐ™์€ ๋ฐœํ‘œ ๋„๊ตฌ๋ฅผ ํ™œ์šฉํ•œ๋‹ค๊ณ  ์ „์ œํ•˜๋ฉด 21์„ธ๊ธฐ์— ๋“ค์–ด์„œ ์ตœ๊ณ ์˜ ํ”„๋ ˆ์  ํ„ฐ๋Š” Apple์˜ ์ „(ๅ‰) CEO์˜€๋˜ ๊ณ (ๆ•…) ์Šคํ‹ฐ๋ธŒ ์žก์Šค(Steve Jobs. 1955 ~ 2011)๋ฅผ ๋งŽ์ด ๋–  ์˜ฌ๋ฆฐ๋‹ค. ์• ํ”Œ ๊ฐœ๋ฐœ์ž ์ฝ˜ํผ๋Ÿฐ์Šค์—์„œ ์•„์ดํฐ์„ ๋ฉ‹์ง€๊ฒŒ ์†Œ๊ฐœํ•˜๋˜ ๋ชจ์Šต ๋“ฑ ๊ตฐ๋”๋”๊ธฐ ์—†๊ณ  ๊ฐ„๊ฒฐํ•˜์ง€๋งŒ ์ธ์ƒ์ ์ธ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ๋ณด์—ฌ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ , ์š”์ฆ˜์€ ๋งŽ์ด ์—†์–ด์กŒ์ง€๋งŒ ๋ธŒ๋žœ๋“œ ํ˜ธํ…”์˜ ์†Œ๊ฐœ<NAME>์ƒ๋„ ์ข‹์€ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์‚ฌ๋ก€์ด๋‹ค. ์œ ๋ช…ํ•œ ์—ฐ์˜ˆ์ธ์ด๋‚˜ ํ˜ธํ…” ๋งค๋‹ˆ์ €๊ฐ€ ์ง์ ‘ ํ˜ธํ…”์„ ์†Œ๊ฐœํ•˜๋Š” ์ด ๋น„๋””์˜ค ํด๋ฆฝ์€ ํ˜ธํ…”๋ฐฉ์— ๋“ค์–ด๊ฐ€๋ฉด ์ž๋™์œผ๋กœ TV๊ฐ€ ์ผœ์ง€๋ฉด์„œ ๋ถ€๋“œ๋Ÿฌ์šด ์Œ์•…๊ณผ ํ•จ๊ป˜ ํ˜ธํ…”์˜ ์ด๊ณณ์ €๊ณณ์„ ์„ค๋ช…ํ•ด ์ฃผ์—ˆ๋˜ ๊ธฐ์–ต์ด ์žˆ๋‹ค. ์–ด๋–ค ๋ณด์กฐ์ž๋ฃŒ๋„ ์—†์ด ์œกํ•˜์›์น™์— ์ž…๊ฐํ•ด ๋งค์šฐ ๊ฐ„๋‹จ๋ช…๋ฃŒํ•˜๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•˜์˜€์ง€๋งŒ ์•„์ฃผ ์ธ์ƒ์ ์ด์—ˆ๋˜ ์ข‹์€ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์ด์—ˆ๋˜ ๊ฒƒ์œผ๋กœ ๊ธฐ์–ต๋œ๋‹ค. ์‚ฌ์‹ค ์–ด๋–ค ๋ฐฉ์‹์„ ์„ ํƒํ•˜๋˜, ์–ด๋–ค ์ด์•ผ๊ธฐ๋ฅผ ํ•˜๋˜, ์–ด๋–ค ๋งค์ฒด๋‚˜ ์–ด๋–ค ๋ฏธ๋””์–ด๋ฅผ ์„ ํƒํ•˜๋˜ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ž˜ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ์•„๋ž˜ ๋‘ ๊ฐ€์ง€๊ฐ€ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค. ๋ฏธ๋ฆฌ ๋งŽ์ด ์—ฐ์Šตํ•  ๊ฒƒ ์‹œ์ž‘ ํ›„ 2๋ถ„ ์•ˆ์— ๊ด€๊ฐ์˜ ์‹œ์„ ์„ ํ™•๋ณดํ•  ๊ฒƒ ์ด๊ฒƒ๋งŒ ์ž˜๋˜๋ฉด ํ”„๋ ˆ์  ํ…Œ์ด์…˜์€ ์‚ฌ์‹ค ์„ฑ๊ณต์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์€ ๋ฌธ์„œ์˜ ๊ตฌ์„ฑ ๋ฐ ์ž‘์„ฑ๋ถ€ํ„ฐ ์ด๋Ÿฐ์ €๋Ÿฐ ์ฑ™๊ฒจ์•ผ ํ•  ๊ฒƒ๋“ค์ด ์žˆ๋‹ค. ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜(Proposal Presentation)์ด๋ž€, ์ œ์•ˆ ๋‚ด์šฉ์„ ๋…ผ๋ฆฌ์ ์ด๊ณ  ์„ค๋“๋ ฅ ์žˆ๊ฒŒ ๊ตฌ์„ฑํ•˜๊ณ  ํ•œ์ •๋œ ์‹œ๊ฐ„ ๋‚ด์— ํ‰๊ฐ€ ์œ„์›๋“ค์—๊ฒŒ ์ œ์•ˆ ๋‚ด์šฉ์„ ์ •ํ™•ํ•˜๊ฒŒ ์ „๋‹ฌํ•˜์—ฌ ๊ฐ๋™์„ ์คŒ์œผ๋กœ์จ ํ”„๋ ˆ์  ํ„ฐ(Presenter)๊ฐ€ ์˜๋„ํ•œ ๋Œ€๋กœ ํ‰๊ฐ€ ์œ„์›๋“ค์ด ํŒ๋‹จํ•˜๊ณ  ์˜์‚ฌ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ๊ธฐ๋ฒ• ์ œ์•ˆ ๋‚ด์šฉ, ๋…ผ๋ฆฌ, ์„ค๋“๋ ฅ, ํ•œ์ •๋œ ์‹œ๊ฐ„, ์ •ํ™•, ์ „๋‹ฌ, ๊ฐ๋™, ํŒ๋‹จ, ์˜์‚ฌ๊ฒฐ์ •, ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ๊ธฐ๋ฒ• ๋“ฑ์ด ์ฃผ์š” ํ‚ค์›Œ๋“œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์—์„œ ๊ณ ๋ คํ•ด์•ผ ํ•  ๊ฒƒ๋“ค์„ ์ƒ๊ฐํ•ด ๋ณด๋ฉด Figure V-7๊ณผ ๊ฐ™์ด ์ฒญ์ค‘ ๋ถ„์„, ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๊ตฌ์กฐ, ๋ฐœํ‘œ ๋งค์ฒด์˜ ์„ ์ •, ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ฌธ์„œ, ๊ฐœ์ธ์  ์Šคํƒ€์ผ ๋“ฑ์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•ด ๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ์ด ์ค‘ ์ฒญ์ค‘ ๋ถ„์„์€ ๊ฐ€์žฅ ์ค‘์š”ํ•œ๋ฐ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ๋“ฃ๋Š” ์‚ฌ๋žŒ ์ฆ‰, ์ฒญ์ค‘์ด ๋ˆ„๊ตฌ์ธ์ง€ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฒญ์ค‘์˜ ๊ทœ๋ชจ๋‚˜ ์„ฑ๋ณ„, ์ง€์‹ ๋ฐ ํƒœ๋„, ํŠนํžˆ ๋‹ˆ์ฆˆ์— ๋Œ€ํ•ด ๋ช…ํ™•ํ•˜๊ฒŒ ํŒŒ์•…ํ•˜๊ณ  ์žˆ์–ด์•ผ ์ •ํ™•ํ•œ ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. Figure V-7. ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๊ณ ๋ ค ์‚ฌํ•ญ ์ €์ž๊ฐ€ ๊ฒฝํ—˜ํ•œ ์ปจ์„คํ„ดํŠธ๋“ค์€ ๋Œ€๋ถ€๋ถ„ ํ›Œ๋ฅญํ•œ ์–ธ๋ณ€์„ ๊ฐ–์ถ”๊ณ  ์žˆ๋‹ค. ๊ทธ๊ฒŒ ํƒ€๊ณ ๋‚ฌ๋˜ ์—ฐ์Šต์„ ํ†ตํ•ด์„œ ์ตํ˜”๋˜ ์•„์ฃผ ํ›Œ๋ฅญํ•œ ์‚ฌ๋žŒ๋“ค์ด ๋งŽ์•˜๋‹ค. ํ”„๋ ˆ์  ํ…Œ์ด์…˜์€ ํ•˜๋‚˜์˜ ์‡ผ(Show)์ด๊ณ  ํ”„๋ ˆ์  ํ„ฐ๋Š” ๊ทธ ์‡ผ์˜ ์ตœ๊ณ  ๋ฐฐ์šฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ฌธ์„œ๋Š” ์ข‹์€ ๊ฐ๋ณธ์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ฐœํ‘œ ์ž๋ฃŒ์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์–‘ํ•œ ๊ด€์ ์—์„œ ์ด์•ผ๊ธฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ฐ€์žฅ ์‰ฌ์šด ์ ‘๊ทผ์€ ๋„์ž…, ์ „๊ฐœ, ๊ฒฐ๋ง ๋˜๋Š” ์„œ๋ก , ๋ณธ๋ก , ๊ฒฐ๋ก ์œผ๋กœ ํ’€์–ด์ง€๋Š” 3๋‹จ๊ณ„ ์ ‘๊ทผ๋ฒ•์ด๋‹ค. Table V-7์€ ๋…ผ๋ฆฌ์  ๊ด€์ ์—์„œ 3๋‹จ๊ณ„๋กœ ๋‚ด์šฉ์„ ๊ตฌ์„ฑํ•  ๋•Œ ์ฃผ์š” ๊ณ ๋ ค ์‚ฌํ•ญ๊ณผ ์˜ˆ์‹œ์ด๋‹ค. Table V-7. ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ 3๋‹จ ๊ตฌ์„ฑ Table V-7๊ณผ ๊ฐ™์ด ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ฐœํ‘œ ์ž๋ฃŒ์˜ ๊ตฌ์กฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋ฉด ์ฒญ์ค‘์˜ ์ž…์žฅ์—์„œ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€๋ฅผ ์ถฉ๋ถ„ํžˆ ๊ฒ€ํ† ํ•ด ๋ณด์•„์•ผ ํ•œ๋‹ค. 1. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ž๋ฃŒ์˜ ์ฝ˜ํ…์ธ ๊ฐ€ ์ดํ•ด๋˜๋Š”๊ฐ€? 2. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ž๋ฃŒ์˜ ํ๋ฆ„์ด ์žˆ๋Š”๊ฐ€? 3. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ž๋ฃŒ์—์„œ ๊ฐ•์กฐํ•˜๋Š” ๊ฒƒ์ด ๋ณด์ด๋Š”๊ฐ€? ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์€ ์ œ์•ˆ์„œ์˜ ์ •์ˆ˜๋ฅผ ์ง‘์•ฝํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ œ์•ˆ ์ „๋žต ๋ฐ ๋‚ด์šฉ์ด ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์— ๊ทธ๋Œ€๋กœ ๋…น์•„๋“ค์–ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ณต๊ณต IT ์ปจ์„คํŒ…์€ Table V-8๊ณผ ๊ฐ™์€ ๋ชฉ์ฐจ๋กœ ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ, ์ œ์•ˆ ๊ฐœ์š” ํ˜น์€ ์‚ฌ์—…์˜ ์ดํ•ด๋ผ๊ณ  ๋ช…๋ช…๋˜๋Š” ๋ถ€๋ถ„์€ ์ œ์•ˆํ•˜๋Š” ์‚ฌ์—…์— ๋Œ€ํ•ด ์‚ฌ์—…์ž๊ฐ€ ์ž˜ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์„œ์ˆ ํ•˜๋Š” ์žฅ์œผ๋กœ ์‚ฌ์—…์ด ์–ด๋–ค ๋ฐฐ๊ฒฝ์—์„œ ๋ฐœ์ฃผ๋˜์—ˆ๋Š”์ง€ ์–ด๋–ค ๋ชฉ์ ์ด๋‚˜ ๋‹ˆ์ฆˆ๋กœ ๊ณ ๊ฐ์ด ์ด๊ฒƒ์„ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ์ž„ํŒฉํŠธ(impact) ์žˆ๊ฒŒ ์š”์•ฝํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ๋ณดํ†ต ์ œ์•ˆ์š”์ฒญ์„œ๋ฅผ ๋งŽ์ด ์ฐธ๊ณ ํ•˜์ง€๋งŒ ์ปจ์„คํ„ดํŠธ์˜ ํ†ต์ฐฐ๋ ฅ๊ณผ ์ •๋ณด๋ ฅ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฒฝ์Ÿ์‚ฌ๊ฐ€ ํŒŒ์•…ํ•˜์ง€ ๋ชปํ–ˆ๋˜ ์‚ฌ์‹ค์ด๋‚˜ ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๋ฅผ ํ‘œํ˜„ํ•ด ์ค„ ๊ฒฝ์šฐ ํ‰๊ฐ€ ์œ„์›๋“ค์€ ๊นŠ์€ ์ธ์ƒ์„ ๋ฐ›๋Š”๋‹ค. ์œ ์‚ฌ ์‚ฌ์—…์— ๋Œ€ํ•œ ๊ฒฝํ—˜์ด๋‚˜ ์‹ค์ , ์ธ์ •๋ฐ›๋Š” ๋Œ€ํ‘œ์ ์ธ ์„ฑ๊ณต์ ์ธ ์‚ฌ๋ก€(Best Practice)๊ฐ€ ์žˆ๋‹ค๋ฉด ์ถฉ๋ถ„ํžˆ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋‘ ๋ฒˆ์งธ, ์‚ฌ์—… ์ˆ˜ํ–‰์ „๋žต์€ ์ œ์•ˆ์ „๋žต์—์„œ ๋„์ถœ๋œ ์ „๋žต๋“ค์„ ์ „๋žต ํ•˜๋‚˜ ๋‹น 1~2์žฅ์˜ ์Šฌ๋ผ์ด๋“œ์— ๋„์‹ํ™”ํ•˜์—ฌ ๋‹ด์•„๋‚ด๋Š” ๊ฒƒ์œผ๋กœ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ํ•ต์‹ฌ ๋‚ด์šฉ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์š”์ฆ˜์€ ๋œธํ•˜์ง€๋งŒ ํ•œ๋•Œ๋Š” ์˜๋ฏธ ์žˆ๋Š” ์˜์–ด ๋‹จ์–ด๋กœ ์ œ์•ˆ์ „๋žต์„ ์‘์ถ•ํ•˜์—ฌ ๋งŽ์ด ํ‘œํ˜„ํ•˜์˜€๋‹ค. (์˜ˆ๋ฅผ ๋“ค๋ฉด SMART[1] ์ „๋žต) ์ œ์•ˆ ์ „๋žต์„ ํ•˜๋‚˜์˜ ์˜๋ฏธ ์žˆ๋Š” ๋‹จ์–ด๋กœ ํ‘œํ˜„ํ•˜๊ณ  ๋ธŒ๋žœ๋“œํ™”ํ•˜์—ฌ ๊ณ ๊ฐ์˜ ๊นŠ์€ ์ธ์ง€๋ฅผ ์œ ๋„ํ•  ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ๊ธฐ๋ฒ•์ด์ง€๋งŒ ์–ต์ง€๋กœ ์ž‘๋ช…ํ•˜๋ฉด ๋งค์šฐ ์–ด์ƒ‰ํ•ด์ง€๊ธฐ๋„ ํ•œ๋‹ค. Table V-8. ๊ณต๊ณต IT ์ปจ์„คํŒ… ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ชฉ์ฐจ ์‚ฌ๋ก€ ์‚ฌ์—…์ „๋žต์€ ๋ณดํ†ต 4~5๊ฐœ ์ •๋„๊ฐ€ ์ ์ •ํ•˜๋‹ค. ๊ณต๊ณต IT ์‚ฌ์—…์„ ์˜ˆ๋ฅผ ๋“ค๋ฉด ์‚ฌ์—…์ „๋žต 1 ~ 3์€ ํ•ด๋‹น ์ œ์•ˆ์˜ ์„ฑ๊ฒฉ์— ๋”ฐ๋ผ ์ƒ์ดํ•˜๊ณ  ์‚ฌ์—…์ „๋žต 4, 5๋Š” ํ›Œ๋ฅญํ•œ ์‚ฌ๋žŒ์ด ์ฐธ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค, ๋ฐฉ๋ฒ•๋ก ์ด๋‚˜ ์ฒด๊ณ„์ ์ธ ์ง€์›์ด ๋’ท๋ฐ›์นจํ•ฉ๋‹ˆ๋‹ค ๋“ฑ์œผ๋กœ ์ •ํ˜•ํ™”๋˜์–ด ์žˆ๋‹ค. ๊ฐ ์‚ฌ์—…์ „๋žต ๋‹น ์ „๋žต ์‹คํ–‰๊ณผ์ œ๋“ค์ด ์„œ์ˆ ๋˜๋Š”๋ฐ ๋ณดํ†ต 3๊ฐœ ์ •๋„๊ฐ€ ์ ๋‹นํ•˜๋‹ค. ํ•ด๋‹น ์ „๋žต์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๊ตฌ์ฒด์ ์ธ ํ™œ๋™ ๊ณ„ํš์„ ํ‘œํ˜„ํ•œ๋‹ค. ์‹œ์Šคํ…œ ๊ตฌ์ถ•์„ ์ž˜ ํ•˜๊ธฐ ์œ„ํ•ด ํŠน๋ณ„ํ•œ ๋„๊ตฌ๋ฅผ ๋„์ž…ํ•œ๋‹ค๋˜ ์ง€ ์„ ์ง„ ์‚ฌ๋ก€๋ฅผ ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด ๋ฒค์น˜๋งˆํ‚น์„ ์ถ”์ง„ํ•˜๊ฒ ๋‹ค๋“ ์ง€ ๋“ฑ๋“ฑ ๊ฒฝ์Ÿ์‚ฌ์™€ ์ฐจ๋ณ„ํ™”๋œ ์ „๋žต ๊ฐœ๋ฐœ๊ณผ ์‹คํ–‰๊ณ„ํš ์ˆ˜๋ฆฝ์ด ์ œ์•ˆ ์„ฑ๊ณต ์—ฌ๋ถ€, ์‚ฌ์—… ์ˆ˜์ฃผ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. ๋˜ํ•œ, ๊ทธ ๊ณผ์ œ ์ˆ˜ํ–‰์˜ ๊ธฐ๋Œ€ํšจ๊ณผ๋ฅผ ํ‘œํ˜„ํ•จ์œผ๋กœ์จ ์ „์ฒด์ ์œผ๋กœ ์ด ์ผ์ด ์ง„ํ–‰๋˜์—ˆ์„ ๋•Œ ์–ด๋–ค ๋ถ€๋ถ„์ด ์ข‹์•„์ง€๋Š”์ง€ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ๋“ฃ๋Š” ๋‚ด๋‚ด ๊ด€์‹ฌ์ด ์ด์–ด์ง€๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค[2]. ์‚ฌ์—… ๊ด€๋ฆฌ์™€ ์ง€์›๋ฐฉ์•ˆ๋„ ๋งค์šฐ ์ •ํ˜•์ ์ธ๋ฐ PM์„ ๋น„๋กฏํ•˜์—ฌ ์–ด๋–ป๊ฒŒ ์ธ๋ ฅ์ด ๊ตฌ์„ฑ๋˜๋Š”์ง€, ์–ด๋–ค ์ผ์ •์œผ๋กœ ์ถ”์ง„๋˜๋ฉฐ ๋‹จ๊ณ„๋ณ„ ๋ณด๊ณ ๋Š” ์–ธ์ œ ๋ช‡ ๋ฒˆ ํ•˜๋Š”์ง€ ๋“ฑ์ด ํ‘œํ˜„๋œ๋‹ค. ํ’ˆ์งˆ๋ณด์ฆ ๋ฐฉ์•ˆ์€ ์‚ฐ์—… ํ‘œ์ค€์„ ์ค€์ˆ˜ํ•œ๋‹ค๋˜ ์ง€ ์ฃผ์š” ์ธ์ฆ (Certification) ๊ฐ™์€ ๊ฒƒ์ด ์žˆ๋‹ค๋ฉด ๊ทธ ๋ถ€๋ถ„์„ ์ž˜ ํ‘œํ˜„ํ•˜์—ฌ ๊ณ ๊ฐ์˜ ์‹ ๋ขฐ๋ฅผ ๋” ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ํ”„๋กœ์ ํŠธ ๊ด€๋ฆฌ ๋ฐฉ์•ˆ์€ ์ฃผ๋จน๊ตฌ๊ตฌ์‹์œผ๋กœ ์ผ์„ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ฒด๊ณ„์ ์ธ ๋ฐฉ๋ฒ•๋ก ๊ณผ ์ž๋™ํ™” ๋„๊ตฌ๋ฅผ ํ™œ์šฉํ•˜๊ณ  ์žˆ์Œ์„ ๊ฐ•์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์ƒ์ƒ ํ˜‘๋ ฅ ๋ฐฉ์•ˆ์€ ์‚ฌ์—…์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์„œ ์ปจ์†Œ์‹œ์—„์œผ๋กœ ์ผ์„ ์ถ”์ง„ํ•  ๋•Œ ์ƒํ˜ธ ๊ฐ„์— ์–ด๋–ป๊ฒŒ ํ˜‘๋ ฅํ•˜์—ฌ ์„ฑ๊ณต์ ์œผ๋กœ ์ผ์„ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ๋ฐฉ์•ˆ์„ ์„œ์ˆ ํ•˜๊ฒŒ ๋œ๋‹ค. ํ•œํŽธ, ๋ฐœ์ฃผ์ฒ˜๊ฐ€ ์ „ํ˜•์ ์ธ ๋ชฉ์ฐจ๋ฅผ ์ œ์‹œํ•˜๋Š” ๊ณต๊ณต์‚ฌ์—…(B2G)์ด ์•„๋‹ˆ๋ผ ๋ฏผ๊ฐ„๊ธฐ์—…์ด ๋ฐœ์ฃผํ•˜๋Š” ์‚ฌ์—…(B2B)์˜ ๊ฒฝ์šฐ๋Š” ์ข€ ๋” ์ฐฝ์˜์ ์ธ ๋ชฉ์ฐจ๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์•ˆ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒ ์ง€๋งŒ ๊ฐ€์žฅ ๋ณดํŽธ์ ์œผ๋กœ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์•ˆ์€ ๋ฌธ์ œ ์ œ๊ธฐ์™€ ํ•ด๋‹ต์„ ์ œ์‹œํ•˜๋ฉด์„œ ๊ณ ๊ฐ๊ณผ ๊ณต๊ฐ์„ ๊ฐ€์ ธ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ฆ‰, ๊ณ ๊ฐ์ด ๊ธฐ์—…์˜ ์ œํ’ˆ์ด๋‚˜ ์„ค๋ฃจ์…˜์„ ์„ ํƒํ•˜๋„๋ก ํ•  ๋•Œ โ€˜์™œ?โ€™๋ผ๋Š” ํ™”๋‘๋ฅผ ๋˜์ง€๊ณ  ๊ทธ โ€˜๋‹ตโ€™์„ ์ œ์‹œํ•˜์—ฌ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ œ์•ˆ์‚ฌ์˜ ์ œํ’ˆ์ด๋‚˜ ์„ค๋ฃจ์…˜์„ ์„ ํƒํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๊ฒฝ์šฐ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ํ•  ๋•Œ ์ข€ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•ด์•ผ ํ•  ๊ฒƒ์€ ๊ทธ ์งˆ๋ฌธ์— ์ œ์‹œํ•˜๋Š” โ€˜๋‹ตโ€™์„ ์ง์ ‘์ ์œผ๋กœ โ€˜์šฐ๋ฆฌ ์ œํ’ˆ์ด๋‚˜ ์„ค๋ฃจ์…˜์ž…๋‹ˆ๋‹คโ€™๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์€ ํ•˜์ˆ˜(ไธ‹ๆ•ธ)๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ณด๋‹ค ์ „๋ฌธ๊ฐ€์ ์ธ ์ž…์ง€๋ฅผ ๊ฐ€์ ธ๊ฐ€๊ณ ์ž ํ•œ๋‹ค๋ฉด ๊ทธ โ€˜๋‹ตโ€™์€ ๋งค์šฐ ๊ฐ๊ด€์ ์ธ ๊ด€์ ์—์„œ ์ œ์‹œ๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์–‘ํ•œ ๋น„๊ต โ€“ ๊ฒฝ์Ÿ์‚ฌ ์ œํ’ˆ๊ณผ์˜ ๋น„๊ต๋„ ์ข‹๋‹ค. โ€“๋ฅผ ํ†ตํ•ด ๊ณ ๊ฐ์ด ์šฐ๋ฆฌ ์ œํ’ˆ์ด๋‚˜ ์„ค๋ฃจ์…˜์„ ์„ ํƒํ•  ์ˆ˜๋ฐ–์— ์—†๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์žฅ๊ธฐ์ ์ธ ๊ณ ๊ฐ ๊ด€๊ณ„๋ฅผ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ€๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋‹ค. Table V-9๋Š” ๋ฏผ๊ฐ„์‚ฌ์—…์˜ ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ชฉ์ฐจ๋ฅผ ์ƒ๊ฐํ•ด ๋ณธ ๊ฒƒ์ด๋‹ค. Table V-9. ๋ฏผ๊ฐ„๊ธฐ์—… ์ปจ์„คํŒ…์˜ ์ œ์•ˆ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ชฉ์ฐจ ์‚ฌ๋ก€ ์ฒซ ๋ฒˆ์งธ๋Š” ๋ฌธ์ œ์˜ ์ธ์‹์ด๋‹ค. ์ œ์•ˆ์„ ํ•˜๋ฉด์„œ ๊ณ ๊ฐ์ด ๋‹น๋ฉดํ•œ ๋ฌธ์ œ๋ฅผ ์ž˜ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ถฉ๋ถ„ํžˆ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ƒฅ โ€˜์—ด์‹ฌํžˆ ์ž˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹คโ€™๋Š” ์˜ค๋Š˜๋‚  ์‚ฌ์—… ํ™˜๊ฒฝ์—์„œ ๋” ์ด์ƒ ํ†ตํ•˜์ง€ ์•Š์œผ๋ฉฐ ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๋‚˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ์ถฉ๋ถ„ํ•œ ์ดํ•ด๋ฅผ ๋ณด์—ฌ์คŒ์œผ๋กœ์จ ์‚ฌ์—…์„ ๋ฏฟ๊ณ  ๋งก๊ธธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ณ ๊ฐ์˜ ์‹ ๋ขฐ๋ฅผ ํ™•๋ณดํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๊ณ ๊ฐ์— ๋Œ€ํ•œ ๊นŠ์€ ์ดํ•ด๊ฐ€ ์ค‘์š”ํ•˜๋ฉฐ ์ปจ์„คํ„ดํŠธ์˜ ํ†ต์ฐฐ๋ ฅ์ด ๊ฐ•์กฐ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ๊ทธ ๋ฌธ์ œ/๋‹ˆ์ฆˆ์— ๋Œ€์‘ํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์Ÿ์ž๋“ค์˜ ๋™ํƒœ๋ฅผ ๋น„๊ต ๋ถ„์„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์€ ๊ธฐ์—…์˜ ์ฐจ๋ณ„ํ™” ํฌ์ธํŠธ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์ด๋‹ค. ์ค‘์š”ํ•œ ๊ฒƒ์€ ๊ฒฝ์Ÿ์‚ฌ๊ฐ€ ์ œ๋Œ€๋กœ ๋ชปํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ถ€๊ฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ทธ ๋ถ€๋ถ„์„ โ€˜์šฐ๋ฆฌ ์ œํ’ˆ์ด๋‚˜ ์„ค๋ฃจ์…˜์€ ์ด๋ ‡๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹คโ€™๋ฅผ ๊ฐ•์กฐํ•ด์•ผ ํ•œ๋‹ค.[3] ์„ธ ๋ฒˆ์งธ๋Š” ๋ฌธ์ œ ๋ถ„์„, ๋น„๊ต ๋ถ„์„ ๋“ฑ์—์„œ ๋„์ถœ๋œ ๋งŽ์€ ๊ณผ์ œ ์ค‘ ์„ ํƒ๊ณผ ์ง‘์ค‘์„ ํ†ตํ•˜์—ฌ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ณผ์ œ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๊ณ ๊ฐ๊ณผ ๊ณต๊ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๋ถ€๋ถ„์—์„œ ํ‰๊ฐ€ ์œ„์›๋“ค๊ณผ ์‹ฑํฌ(Sync) ๋˜๋ฉด ๊ฑฐ์˜ ์‚ฌ์—…์„ ์ˆ˜์ฃผํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋„ค ๋ฒˆ์งธ ๋Œ€์•ˆ ์ œ์‹œ๋Š” ์‹ค์งˆ์ ์œผ๋กœ ์ œ์•ˆ์‚ฌ๊ฐ€ ๊ณ ๊ฐ ๊ธฐ์—…์—๊ฒŒ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์น˜๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ถ€๋ถ„์ธ๋ฐ ๊ฒฝ์Ÿ์‚ฌ์™€๋Š” ์ฐจ๋ณ„๋œ ์ œํ’ˆ์ด๋‚˜ ์„ค๋ฃจ์…˜์œผ๋กœ ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๋ฅผ ์ถฉ์กฑ์‹œ์ผœ์ฃผ๊ฑฐ๋‚˜ ๊ณ ๊ฐ์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์—ฌ์ฃผ๋Š” ๋ถ€๋ถ„์ด๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ทธ๊ฒƒ์„ ์–ด๋–ค ์‹คํ–‰๊ณ„ํš์œผ๋กœ ์ถ”์ง„ํ•  ๊ฒƒ์ธ์ง€ ๋“ฑ์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ๋งˆ๋ฌด๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ฐœํ‘œ ์ž๋ฃŒ์˜ ๊ตฌ์„ฑ์— ์ ˆ๋Œ€์ ์ธ ๊ธฐ์ค€์€ ์—†์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑํ•˜๋ฉด ๋ณด๊ธฐ ์ข‹๋‹ค. (ํŒŒ์›Œํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ๋‹ค๊ณ  ๊ฐ€์ •) (1) ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ฒซ ์žฅ์€ ๋Œ€๋ถ€๋ถ„ ํ‘œ์ง€๊ฐ€ ์ฐจ์ง€ํ•˜๋Š”๋ฐ ์ œ๋ชฉ(47pt), ๋ถ€์ œ๋ชฉ(36pt), ์ด๋ฆ„(24pt)๊ฐ€ ๋ฌด๋‚œํ•จ (2) ์ด์–ด์ง€๋Š” ๋ณธ๋ฌธ์˜ ๊ตฌ์„ฑ์€ ์†Œ์ œ๋ชฉ(28pt), ๋ณธ๋ฌธ(18pt)๋กœ ํ•˜๊ณ  ์„œ์ฒด๋Š” ํ•œ ๊ฐ€์ง€๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ง‘์€ ๊ณ ๋”•์ฒด์˜ ๊ตต๊ธฐ ์กฐ์ • (L, M, B) (3) ๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ๊ตฌ์„ฑํ•  ๋•Œ ๋‚ด์šฉ์ด ์ˆ˜๋ ด๋˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ฆ‰, ๋‹ค์Œ ์ ˆ์ฐจ๋กœ ๋ฌธ์„œ๋ฅผ ๊ตฌ์„ฑํ•  ๋•Œ ๋งˆ์ง€๋ง‰ ๊ฒฐ๋ก ์ด ์ˆ˜๋ ด๋˜๋Š” ํ˜•ํƒœ์ด๋‹ค. (1) Take Attention (2) Transition (3) Main Stream (4) Conclusion 19.3 ์ฐจ๋ณ„ํ™”๋œ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์— ์žˆ์–ด ํ”„๋ ˆ์  ํ„ฐ๊ฐ€ ๊ฐœ๋ฐฉ์ ์ธ ์„ฑ๊ฒฉ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ์—ฌ๋Ÿฌ ์‚ฌ๋žŒ ์•ž์— ๋‚˜์„œ๊ธฐ ์ข‹์•„ํ•˜๋Š” ์‚ฌ๋žŒ์ด ์ƒ๋Œ€์ ์œผ๋กœ ์œ ๋ฆฌํ•œ ๊ฒƒ์€ ์‚ฌ์‹ค์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ณ ๊ฐ์˜ ๊ฐ๋™๊นŒ์ง€ ๊ฐ€์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์ด ๋ณด์•˜๋Š”๋ฐ ์ปจ์„คํ„ดํŠธ ์ž์‹ ์˜ ์ง€์‹๊ณผ ๊ฒฝํ—˜์ด ์•„๋‹Œ ๊ฒƒ์„ ๋ฐœํ‘œํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์•„๋ฌด๋ฆฌ ์—ฐ์Šต์„ ํ–ˆ์„์ง€์–ธ์ • ๊ณ ๊ฐ ๊ฐ๋™์„ ๊ฐ€์ ธ์˜จ ๊ฒฝ์šฐ๋Š” ๋“œ๋ฌผ์—ˆ๋‹ค. ๋ฌธ์„œ์— ์ ์šฉํ•  ๋‹จ์–ด๋‚˜ ์–ดํœ˜, ๋ง๋กœ ํ‘œํ˜„ํ•ด์•ผ ํ•  ๊ฒƒ๋“ค์€ ์ฒญ์ค‘์˜ ๊ธฐ๋Œ€์— ๋ถ€ํ•ฉํ•ด์•ผ ํ•˜๋ฉฐ ํ•ต์‹ฌ์ ์ธ ๋ฌธ์žฅ์„ ์ค‘์‹ฌ์œผ๋กœ ์ž˜ ํ‘œํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ํŠนํžˆ ์ง€์‹œ์ ์ธ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฃผ์˜๋ฅผ ์ง‘์ค‘์‹œํ‚ค๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด Table V-10๊ณผ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. Table V-10. ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ํ‘œํ˜„ ์‚ฌ๋ก€ ๋˜ํ•œ, ์ ๊ทน์ ์œผ๋กœ ์ž์‹  ์žˆ๊ฒŒ, ๋ฌธ๋ฒ•์— ๋งž๋Š” ๊ตฌ์กฐ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. Table V-11์€ ๊ทธ๋Ÿฐ ์‚ฌ๋ก€๋“ค์ด๋‹ค. Table V-11. ์ ๊ทน์ ์ธ ๋งํ•˜๊ธฐ์™€ ์‚ฌ๋ก€ ๊ทธ ์™ธ ๋ชฉ์†Œ๋ฆฌ ํ†ค, ์ž์„ธ, ์‹œ๊ฐ ๋“ฑ ํ”„๋ ˆ์  ํ„ฐ๊ฐ€ ์ „๋ฌธ๊ฐ€๋กœ์„œ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์œผ๋กœ ๋น„์น  ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ๋ฐœํ‘œ ํƒœ๋„๋‚˜ ์ž์„ธ ๋“ฑ์€ ํ‰์†Œ ๊ฐœ์ธ์ ์ธ ์Šต๊ด€๊ณผ ๊ด€๊ณ„๊ฐ€ ๊นŠ์€๋ฐ ์‰ฝ๊ฒŒ ๋ฐ”๋€Œ์ง€ ์•Š์œผ๋ฏ€๋กœ ์‹œ๊ฐ„์„ ๋‘๊ณ  ๋ฆฌํ—ˆ์„ค(Rehearsal)์„ ํ†ตํ•ด ๊ฐ€๋Šฅํ•˜๋ฉด ๋งŽ์ด ์—ฐ์Šตํ•ด์•ผ ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์ด ๋๋‚˜๋ฉด ์งˆ์˜์‘๋‹ต์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ์งˆ์˜์‘๋‹ต์„ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‚ฌ์ „์— ์ „๋žต์ ์œผ๋กœ ์งˆ์˜๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ํ•ญ๋ชฉ๋“ค์„ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์•ˆ์—์„œ ์ถฉ๋ถ„ํžˆ ๋…น์—ฌ์„œ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ์งˆ์˜์‘๋‹ต ๋Œ€์‘ ํŒ€(Red Team)์„ ๋ณ„๋„๋กœ ๊ตฌ์„ฑํ•˜๊ธฐ๋„ ํ•˜๊ณ  ์‚ฌ์ „ ์งˆ๋ฌธ์„ ๋งŒ๋“ค๊ณ  ์ œ์•ˆํŒ€ ๋˜๋Š” ๋‚ด/์™ธ๋ถ€ ์œ ๊ด€ ์กฐ์ง์„ ๋ณ„๋„๋กœ<NAME>ํ•ด์„œ ํ˜„์žฅ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ํ•ด๋ณด๊ธฐ๋„ ํ•œ๋‹ค. ์–ด์ฐŒ ๋˜์—ˆ๋˜ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์— ์ž„ํ•˜๋Š” ๊ฐ€์žฅ ์ข‹์€ ์ž์„ธ๋Š” โ€˜์—ฐ์Šตโ€™๊ณผ ๋˜ โ€˜์—ฐ์Šตโ€™๋ฟ์ด๋‹ค. ์ปจ์„คํŒ…์— ๋Œ€ํ•ด ๋‹ค์‹œ ์ƒ๊ฐํ•ด ๋ณด๋Š” ํ•ญํ•ด๊ฐ€ ๊ฑฐ์˜ ๋๋‚˜๊ฐ„๋‹ค. ์ง€์‹๊ฒฝ์˜(Knowledge Management)๋Š” ํ•ต์‹ฌ ์—ญ๋Ÿ‰ ์ด๋ก ๊ณผ ๋”๋ถˆ์–ด 1990๋…„๋Œ€ ํ›„๋ฐ˜๋ถ€ํ„ฐ 2000๋…„๋Œ€ ์ดˆ๋ฐ˜๊นŒ์ง€ ์ „ ์„ธ๊ณ„์ ์œผ๋กœ ํ™”๋‘๊ฐ€ ๋˜์—ˆ๋˜ ์˜ค๋ž˜๋œ ์ฃผ์ œ์ด์ง€๋งŒ ๋Œ€ํ‘œ์ ์ธ ์ง€์‹์„œ๋น„์Šค ์‚ฐ์—…์ธ '์ปจ์„คํŒ… ์‚ฌ์—…'๊ณผ๋Š” ๋–ผ์–ด๋†€ ์ˆ˜ ์—†๋Š” ๊ด€๊ณ„์— ์žˆ๋Š” ์ค‘์š”ํ•œ ์˜์—ญ์ด๋‹ค. ๋ณธ ์ €์„œ์˜ ๋งˆ์ง€๋ง‰ ์žฅ์ธ ์ œ20์žฅ์€ ์ปจ์„คํŒ… ์ดํ–‰๊ณผ ์ง€์‹๊ฒฝ์˜์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž. [1] ํ”ผํ„ฐ ๋“œ๋Ÿฌ์ปค์˜ SMART๋Š” ์‚ฌ์—…๋ชฉํ‘œ(MBO) ์ž‘์„ฑ ์‹œ ๊ณ ๋ คํ•ด์•ผ ํ•  Specific, Measurable, Action-oriented, Realistic, Timely์˜ ์•ฝ์ž๋ฅผ ์˜๋ฏธํ•จ [2] ๋ฌธ์„œ ์ž‘์„ฑ์˜ ๊ธฐ์ˆ ์  ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๋ถ€ํ‘œ๋‚˜ ๋ ˆ์ „๋“œ(legend)๋ฅผ ์ด์šฉํ•˜์—ฌ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์˜ ํ๋ฆ„์„ ์ง€์†์ ์œผ๋กœ ์ด์–ด๊ฐ€๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. [3] ๊ฐ„ํ˜น ๊ด‘๊ณ  ๋“ฑ์—์„œ ๋ณด์ด๋Š” ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ „๋žต์€ ์ˆ˜๋งŽ์€ ์—…์ข…์—์„œ ๋ถˆ๋ฌธ์œจ์ฒ˜๋Ÿผ ๋˜์–ด ์žˆ๋‹ค. ์ผ์ข…์˜ ์ƒ๋„๋ฅผ ๊ณ ๋ คํ•œ ๊ฒƒ์ธ๋ฐ ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ „๋žต๊ณผ ์ฐจ๋ณ„ํ™” ์ „๋žต์„ ์ž˜ ๊ตฌ๋ถ„ํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. ๊ฐ™์ด ์ฝ์–ด๋ณด๋ฉด ์ข‹์€ ์ฑ… kx โ–ถ Say It With Charts: The Executiveโ€™s Guide to Visual Communication, Gene Zelazny, McGraw-Hill, 2001 Say It With Presentation: How to Design and Deliver Successful Business Presentations, Gene Zelazny, McGraw-Hill, 2006 20. ์ปจ์„คํŒ… ์ดํ–‰๊ณผ ์ง€์‹๊ฒฝ์˜ ์ปจ์„คํŒ… ์ดํ–‰์˜ ํ•ต์‹ฌ์€ ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ฐ ๋ณด๊ณ ์„œ ์ž‘์„ฑ์ด์ง€๋งŒ ๊ทธ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ๊ฒƒ์€ ์ œ์•ˆ์„ ์œ„ํ•ด ๋ณธ ์‚ฌ์•ˆ์— ๋Œ€ํ•ด ๊ฐ€์กŒ๋˜ ์ „๋žต์  ์‚ฌ๊ณ ๋ฅผ ์ง€์†์ ์œผ๋กœ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Table V-12๋Š” ์ „๋žต์  ์‚ฌ๊ณ ์˜ ๋‹จ๊ณ„์  ํ๋ฆ„์„ ์„ค๋ช…ํ•œ ๊ฒƒ์ธ๋ฐ ์ปจ์„คํŒ… ์ œ์•ˆ์ด๋˜ ์ปจ์„คํŒ… ์ดํ–‰์ด๋˜ ์‚ฌ๊ณ ์˜ ๊ธฐ๋ณธ์ ์ธ ํ๋ฆ„์œผ๋กœ์„œ ์ˆ™์ง€ํ•ด์•ผ ํ•  ์‚ฌํ•ญ๋“ค์ด๋‹ค. ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ์ง€์‹๊ณผ ๊ฒฝํ—˜, ๋„๊ตฌ๋ฅผ ์ถฉ๋ถ„ํžˆ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ง€์‹ ์ž์‚ฐ์˜ ํšจ์œจ์  ๊ตฌ์ถ•๊ณผ ํ™œ์šฉ, ๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฒƒ์„ ์ง€์›ํ•˜๋Š” ์ธํ”„๋ผ์˜ ๊ตฌ์ถ•์ด ๋ฌด์—‡๋ณด๋‹ค๋„ ์ค‘์š”ํ•˜๋‹ค. ์ œ20์žฅ์—์„œ๋Š” ๊ทธ๊ฒƒ๋“ค์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž. Table V-12. ์ „๋žต์  ์‚ฌ๊ณ ์˜ ๋‹จ๊ณ„ 20.1 ์ปจ์„คํŒ… ์‚ฐ์ถœ๋ฌผ ์ผ(work)์˜ ๊ด€์ ์—์„œ ๋ณด๋ฉด ์ปจ์„คํŒ…์€ ํ”„๋กœ์ ํŠธ์˜ ํ•œ ์ข…๋ฅ˜๋กœ์„œ ์‹œ์ž‘๊ณผ ๋์ด ์žˆ๊ณ  ๊ฐ๊ฐ์˜ ์ปจ์„คํŒ…๋งˆ๋‹ค ๊ณ ์œ ํ•œ ํŠน์ƒ‰์ด ์žˆ์œผ๋ฉฐ ๊ทธ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋Š” ๋ฐ˜๋“œ์‹œ ์‚ฐ์ถœ๋ฌผ(deliverables)๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์ปจ์„คํŒ… ์‚ฐ์ถœ๋ฌผ์€ ๋ณด๊ณ ์„œ(Report)์ธ๋ฐ, ์ปจ์„คํŒ…์˜ ์ข…๋ฅ˜์™€ ์„ฑ๊ฒฉ์— ๋”ฐ๋ผ์„œ ๊ทธ ์„ธ๋ถ€์ ์ธ ๋‚ด์—ญ์€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์œผ๋‚˜ ๋งŽ์€ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋ณด๊ณ ์„œ๋Š” ๋‹ค์Œ์˜ ์„ธ ๊ฐ€์ง€์ด๋‹ค. ์ฐฉ์ˆ˜ ๋ณด๊ณ ์„œ(Inception Report) 'Kick-Off Meeting ๋ณด๊ณ ์„œ'๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋Š” ์ฐฉ์ˆ˜ ๋ณด๊ณ ์„œ๋Š” ์ปจ์„คํŒ… ์ œ์•ˆ์„œ์—์„œ ์ œ์‹œํ–ˆ๋˜ ์ปจ์„คํŒ… ์ดํ–‰ ๋ฐฉ์•ˆ๋“ค์„ ์„ ๋ฐœ๋œ ํˆฌ์ž… ์ธ์›๋“ค๊ณผ ํ•จ๊ป˜ ์ œ์‹œํ•œ ๊ธฐ๊ฐ„ ์•ˆ์— ์ˆ˜ํ–‰ํ•˜๊ฒ ์Œ์„ ๊ณ ๊ฐ์—๊ฒŒ ์ฒœ๋ช…ํ•˜๋Š” ๋ณด๊ณ ์„œ์ด๋‹ค. ๋ฏผ๊ฐ„ ๊ธฐ์—…์ด ๋ฐœ์ฃผํ•œ ๊ฒฝ์˜์ „๋žต ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ ์ตœ๊ณ ๊ฒฝ์˜์ž(CEO)๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๋ณด๊ณ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ณ , IT ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ IT ๋‹ด๋‹น ์ž„์›(CIO)์—๊ฒŒ ์ฃผ๋กœ ๋ณด๊ณ ํ•œ๋‹ค. ๊ณต์‹์ ์œผ๋กœ ๊ณ ๊ฐ์—๊ฒŒ ํ”„๋กœ์ ํŠธ๊ฐ€ ์‹œ์ž‘๋˜์—ˆ์Œ์„ ์•Œ๋ฆฌ๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์˜ ์˜์˜, ์ˆ˜ํ–‰ ๋ฐฉ์•ˆ, ๊ธฐ๋Œ€ํšจ๊ณผ, ํŒ€์› ์†Œ๊ฐœ, ํ–ฅํ›„ ๊ณ„ํš ๋“ฑ์— ๋Œ€ํ•ด ์ž˜ ์ •๋ฆฌํ•ด์„œ ๋ณด๊ณ ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋ฉฐ, ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋ฅผ ์‹ค์งˆ์ ์œผ๋กœ ๋„์™€์ค„ ๊ณ ๊ฐ๊ธฐ์—… ๋‚ด ๊ฐ ๋ถ€์„œ์žฅ๋“ค ๋ฐ ์‹ค๋ฌด์ง„๋“ค๊ณผ ๊ณต์‹์ ์œผ๋กœ ์ฒ˜์Œ ๋Œ€๋ฉดํ•˜๋Š” ์ˆœ๊ฐ„ ์ผ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋งค์šฐ ์ „๋žต์ ์œผ๋กœ ๋Œ€์‘ํ•ด์•ผ ํ•œ๋‹ค. ์ค‘๊ฐ„ ๋ณด๊ณ ์„œ(Interim Report) ์ค‘๊ฐ„๋ณด๊ณ ์„œ๋Š” ํ”„๋กœ์ ํŠธ ์ฐฉ์ˆ˜ ์ดํ›„์— ์ˆ˜ํ–‰ํ•œ ํ˜„ํ™ฉ ๋ถ„์„์˜ ๊ฒฐ๊ณผ์™€ ๊ฐœ์„  ๊ธฐํšŒ ๋ถ„์•ผ ์„ ์ •์— ๋Œ€ํ•œ ๋™์˜๋ฅผ ์–ป๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค. ๊ฐ€์„ค์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด์Šˆ๋‚˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ํ•ด๋ฒ•์ด๋‚˜ ์„ค๋ฃจ์…˜์— ๋Œ€ํ•œ ํฐ ๊ทธ๋ฆผ(Big Picture)์„ ์ œ์‹œํ•˜๊ณ  ํ–ฅํ›„ ์ผ์ •์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ ๊ธฐ๊ฐ„์ด ๊ธธ ๊ฒฝ์šฐ, ๊ณ ๊ฐ์˜ ์š”์ฒญ์— ์˜ํ•ด ์›”๋ณ„/๋ถ„๊ธฐ๋ณ„ ๋ณด๊ณ ๋ฅผ ์š”๊ตฌ๋ฐ›๊ธฐ๋„ ํ•˜๋ฉฐ, ํ˜„ํ™ฉ ๋ถ„์„์„ ์œ„ํ•œ ์›Œํฌ์ˆ(workshop)์ด๋‚˜ ์ธํ„ฐ๋ทฐ(interview)๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ๋ณดํ†ต ์ค‘๊ฐ„๋ณด๊ณ ๋ฅผ ์ค€๋น„ํ•œ๋‹ค. ์ข…๋ฃŒ ๋ณด๊ณ ์„œ(Final Report) ์ข…๋ฃŒ ๋ณด๊ณ ์„œ๋Š” ์ค‘๊ฐ„๋ณด๊ณ ์„œ์—์„œ ์ œ์‹œ๋œ ๊ฐœ์„  ๊ธฐํšŒ ๋ถ„์•ผ๋ฅผ ํ˜„์žฅ์—์„œ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐœ๋ฐœํ•œ ํ”„๋กœ๊ทธ๋žจ๊ณผ ์‹คํ–‰๊ณผ์ œ(Key Initiatives ๋˜๋Š” Quick win ๊ณผ์ œ), ๊ทธ ๊ณผ์ œ๋ฅผ ์ดํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ํ›„์† ์—ฐ๊ณ„ ํ”„๋กœ์ ํŠธ๋‚˜ ํ›„์† ์กฐ์น˜์‚ฌํ•ญ ๋“ฑ์˜ ์ดํ–‰ ๊ณ„ํš์— ๋Œ€ํ•ด ์ตœ๊ณ ๊ฒฝ์˜์ž์™€ ๊ฒฝ์˜์ง„๋“ค์˜ ๋™์˜์™€ ์ถ”์ง„ ๊ถŒํ•œ์„ ์–ป๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค. ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๊ฐ€ ์ž˜ ์ง„ํ–‰๋˜๋ฉด ๋ณดํ†ต ์ข…๋ฃŒ ๋ณด๊ณ  ์ „์— ํ›„์† ํ”„๋กœ์ ํŠธ์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๊ฐ€ ๊ณ ๊ฐ๊ณผ ํ•จ๊ป˜ ์˜ค๊ณ  ๊ฐ€๋ฉฐ ์žฌ๊ณ„์•ฝ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ์ปจ์„คํŒ… ๋ณด๊ณ ์„œ๋Š” โ€˜๋ˆ„๊ฐ€ ์ž‘์„ฑํ–ˆ๋Š๋ƒ?โ€™๋ณด๋‹ค โ€˜์‹คํ–‰์— ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋Š๋ƒ?โ€™๊ฐ€ ๋” ์ค‘์š”ํ•˜๋ฉฐ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์„ค๋ฃจ์…˜์„ ์ œ์‹œํ•  ์ˆ˜ ์žˆ์„ ๋•Œ ๊ทธ ๊ฐ€์น˜๊ฐ€ ๋”์šฑ ๋น›๋‚œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋Š” ํ›Œ๋ฅญํ•œ ์ปจ์„คํŒ… PM๊ณผ ์ปจ์„คํŒ… ํŒ€๋งŒ์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์†Œ์œ„ ๋งํ•˜๋Š” ๋ฐฑ์˜คํ”ผ์Šค(Back Office) ํŒ€์ด ๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ์ง€์›์กฐ์ง์ด ํ•„์š”ํ•˜๋‹ค. 20.2 CoE์˜ ์šด์˜ ์˜ค๋Š˜๋‚ ์€ ์ธํ„ฐ๋„ท ๋“ฑ ๋‹ค์–‘ํ•œ ํ†ต์‹ ๋ง์œผ๋กœ ์—ฐ๊ฒฐ๋œ ๊ธ€๋กœ๋ฒŒ ์—ฐ๊ฒฐ์‹œ๋Œ€์ด๋‹ค ๋ณด๋‹ˆ ์‚ฌ์—…์ด๋‚˜ ๊ธฐ์ˆ ๋„ ์„ฑ๊ณต ์‚ฌ๋ก€๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒƒ์ด ์‹ค์‹œ๊ฐ„(realtime)์œผ๋กœ ์ „ ์„ธ๊ณ„์— ๊ณต์œ ๋˜๊ณ  ์žˆ๋‹ค. ๊ธฐ์—… ์กฐ์ง์˜ ํ˜•ํƒœ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ธ๋ฐ GE๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘๋œ ์‚ฌ์—…๋ถ€ ์กฐ์ง(SBU), IBM์ด๋‚˜ ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ๊ฐ€ ์ฑ„ํƒํ•˜๊ณ  ์žˆ๋Š” ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ์—… ์กฐ์ง์˜ ํ˜•ํƒœ๋Š” ๊ทธ ๊ธฐ์—…์˜ ๊ฒฝ์Ÿ๋ ฅ ํ™•๋ณด์— ๋งค์šฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•œ๋‹ค. ๊ทธ์ค‘์— โ€˜Center of Excellenceโ€™๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ์กฐ์ง ํ˜•ํƒœ๊ฐ€ ์žˆ๋‹ค. ์‚ฐ์—… ๋ฐ ์—…์ข…์— ๋”ฐ๋ผ ๊ทธ ์˜๋ฏธ๋ฅผ ์กฐ๊ธˆ์”ฉ ๋‹ฌ๋ฆฌํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ ์ปจ์„คํŒ… ์‚ฐ์—…์—์„œ CoE๋Š” โ€˜์ปจ์„คํ„ดํŠธ ์œก์„ฑโ€™๊ณผ โ€˜์ปจ์„คํŒ… ์‚ฌ์—…์˜ ๋ฐฑ์˜คํ”ผ์Šค ์—ญํ• โ€™์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๊ทธ ๋‚ด์šฉ์„ ์‚ดํŽด๋ณด๋ฉด ์ฒซ ๋ฒˆ์งธ, ์ปจ์„คํ„ดํŠธ ์œก์„ฑ ๋ฐ ๊ด€๋ฆฌ์ด๋‹ค. ์ด์ œ ๋ง‰ ์ž…์‚ฌํ•œ ์‹ ์ž… ์ปจ์„คํ„ดํŠธ๋“ค์—๊ฒŒ ์ปจ์„คํŒ… ์Šคํ‚ฌ์ด๋‚˜ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•, ๋ฐฉ๋ฒ•๋ก  ๋“ฑ์„ ๊ฐ€๋ฅด์น˜๋ฉด์„œ ์ปจ์„คํ„ดํŠธ๋กœ์„œ์˜ ๊ธฐ๋ณธ ์†Œ์–‘์„ ์Œ“๊ฒŒ ๋„์™€์ค€๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋Š” ๊ฐ ์‚ฐ์—… ๋„๋ฉ”์ธ์— ์†ํ•œ ์‹œ๋‹ˆ์–ด ์ปจ์„คํ„ดํŠธ๋“ค์ด ํ”„๋กœ์ ํŠธ๋ฅผ ๋ฐœ๊ตดํ•˜๋ฉด ๋Œ€๋ถ€๋ถ„ ๋ณธ์ธ์ด PM์„ ํ•˜๊ณ  CoE๋ฅผ ํ†ตํ•ด ํˆฌ์ž…๋  ์ธ์›์„ ์ˆ˜๋ฐฐํ•˜๋Š” ์ ˆ์ฐจ๋ฅผ ๊ฑฐ์ณ ํ”„๋กœ์ ํŠธ ํŒ€์„ ๊ตฌ์„ฑํ•œ๋‹ค.[1] ๋”ฐ๋ผ์„œ ํ›Œ๋ฅญํ•œ ์—ญ๋Ÿ‰์„ ๋ณด์œ ํ•œ ์ข‹์€ ์ธ์žฌ๋“ค์„ ์œก์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์€ CoE์˜ ์ค‘์š”ํ•œ ์—ญํ• ์ด์ž ๋ชฉํ‘œ๊ฐ€ ๋œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ๋ฐฑ์˜คํ”ผ์Šค(Back Office)๋กœ์„œ ์ปจ์„คํŒ… ์ง€์‹๊ณผ ์ˆ˜ํ–‰ ์‚ฌ๋ก€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ผ์ด๋‹ค. CoE์™€ ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ๊ฐœ๋…์ด์ง€๋งŒ โ€˜Competency Center(CC)โ€™๋‚˜ โ€˜Resource Center(RC)โ€™๋ผ๊ณ  ํ•˜๋Š” ์กฐ์ง ํ˜•ํƒœ๊ฐ€ ์ธ๋ ฅ์„ ์œก์„ฑํ•˜๊ณ  ํ”„๋กœ์ ํŠธ์— ํˆฌ์ž…ํ•˜๋ฉฐ ๊ทธ๋“ค์˜ ๊ฐ€๋™๋ฅ ์„ ๊ด€๋ฆฌํ•˜๋Š” ์ผ์— ์ง‘์ค‘ํ•œ๋‹ค๋ฉด, CoE๋Š” ํ•œ ๋ฐœ ๋‚˜์•„๊ฐ€์„œ ๋ฐฉ๋ฒ•๋ก ์ด๋‚˜ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ• ๋“ฑ ์ปจ์„คํŒ…์˜ ํ•ต์‹ฌ ์ง€์‹(Core Knowledge)์„ ๊ฐœ๋ฐœ ๋ฐ ๊ด€๋ฆฌํ•˜๋ฉฐ ๊ฐ ํ”„๋กœ์ ํŠธ ํ˜„์žฅ์— ๋ฐฐํฌ, ์ง€์›ํ•˜๋Š” ์ผ๋„ ๋งก๋Š”๋‹ค. ๋˜ํ•œ, ์ž์›๊ณผ ์—ญ๋Ÿ‰์ด ํ—ˆ๋ฝํ•œ๋‹ค๋ฉด ์‚ฐ์—… ๋ฆฌ์„œ์น˜๋ฅผ ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋ณด๊ณ ์„œ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๊ธฐ๋„ ํ•˜๊ณ , ๋‹ค์–‘ํ•œ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•ด์„œ ๊ณ ๊ฐ์—๊ฒŒ ํŠธ๋ Œ๋“œ์™€ ํ†ต์ฐฐ๋ ฅ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ์ž๋ฃŒ๋“ค์„ ์ƒ์„ฑํ•œ๋‹ค. ์›นํŽ˜์ด์ง€๋‚˜ ์†Œ์…œ๋ฏธ๋””์–ด๋ฅผ ์šด์˜ํ•˜๋ฉฐ ๋งˆ์ผ€ํŒ…์„ ํ•˜๊ธฐ๋„ ํ•˜๊ณ  ์‚ฐ์—…์ด๋‚˜ ๊ธฐ์ˆ  ์ฝ˜ํผ๋Ÿฐ์Šค๋ฅผ ๊ฐœ์ตœํ•˜์—ฌ ๊ณ ๊ฐ๋“ค์„ ์ดˆ๋น™ํ•˜๊ณ  ๊ทธ๋“ค์˜ ์‚ฌ๊ณ  ๋ฆฌ๋”์‹ญ(Thought Leadership)์„ ์†Œ๊ฐœํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ–๊ธฐ๋„ ํ•œ๋‹ค. ๋งฅํ‚จ์ง€๋‚˜ ๋ฒ ์ธ, ์—‘์„ผ์ธ„์–ด ๋“ฑ ์—ญ์‚ฌ๊ฐ€ ์˜ค๋ž˜๋œ ์™ธ๊ตญ๊ณ„ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ์ด ๋ฐฑ์˜คํ”ผ์Šค ๊ธฐ๋Šฅ์ด ๋งค์šฐ ์šฐ์ˆ˜ํ•˜๋‹ค. ๋‹ค์–‘ํ•œ ์ง€์  ์ž์‚ฐ์„ ๊ฐœ๋ฐœํ•˜๊ณ  ์ปจ์„คํ„ดํŠธ๋“ค์—๊ฒŒ<NAME>์—ฌ ์ปจ์„คํ„ดํŠธ๋“ค์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ๋„์™€์ฃผ๊ณ  ๋˜ ๊ทธ ์ˆ˜ํ–‰ ์‚ฌ๋ก€๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ ๋ฐฉ๋ฒ•๋ก ์ด๋‚˜ ๋„๊ตฌ, ๊ธฐ๋ฒ•์ด ์ •์ œ๋  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค. ์ด ์šฐ์ˆ˜ ์„ฑ๊ณต ์‚ฌ๋ก€(Best Practice)๋‚˜ ๊ณต์œ ๋˜๋Š” ๋‹ค์–‘ํ•œ ๊ฒฝํ—˜๋“ค์„ ํ™œ์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๊ณ  ์žˆ๋‹ค. Break #22. ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง์€ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…๋“ค์˜ ๋Œ€ํ‘œ์ ์ธ ์กฐ์ง ํ˜•ํƒœ๋กœ ๊ธ€๋กœ๋ฒŒ ์‚ฌ์—…์„ ์ถ”์ง„ํ•˜๋Š” ๊ฐ€์žฅ ์ด์ƒ์ ์ธ ํ˜•ํƒœ์˜ ์กฐ์ง์ด๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค. ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง์€ Figure V-8์ฒ˜๋Ÿผ ๊ธฐ์—…์˜ ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ์ƒ์‚ฐํ•˜๋Š” ์กฐ์ง์„ ์ˆ˜ํ‰์ (Horizontal)์œผ๋กœ ๋†“๊ณ , ์‚ฐ์—… ๋˜๋Š” ์‚ฌ์—…์˜์—ญ์— ๊ธฐ๋ฐ˜ํ•˜๋Š” ์‚ฌ์—… ์กฐ์ง์„ ์ˆ˜์ง์ (Vertical)์œผ๋กœ ๋ฐฐ์น˜ํ•œ๋‹ค. ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง์—์„œ ์ˆ˜์ง์  ๋ฐฐ์น˜ ์กฐ์ง์ด ๊ณ ๊ฐ๊ณผ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ํ•˜๋ฉฐ ์‚ฌ์—…์„ ๋‹ด๋‹นํ•œ๋‹ค. ์ฆ‰, ์ˆ˜ํ‰ ์กฐ์ง์€ ์ œํ’ˆ ์„œ๋น„์Šค, ์„ค๋ฃจ์…˜ ๊ฐœ๋ฐœ์— ์ฃผ๋ ฅํ•˜๊ณ  ์ˆ˜์ง ์กฐ์ง์€ ๊ณ ๊ฐ์ฑ„๋„ ์—ญํ• ์„ ํ•˜๋ฉด์„œ ์ˆ˜ํ‰์กฐ์ง์ด ๋งŒ๋“ค์–ด๋‚ด๋Š” ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค, ์„ค๋ฃจ์…˜์„ ๊ณ ๊ฐ์—๊ฒŒ ํŒ๋งคํ•˜๋Š” ์—ญํ• ์„ ๋งก๋Š”๋‹ค. ์‚ฌ์—…์˜ ๋ณต์žก์„ฑ์— ๋”ฐ๋ผ 3~5์ถ• ๋˜๋Š” ๊ทธ ์ด์ƒ์„ ์ด๋ฃจ๋Š” ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง ํ˜•ํƒœ๋„ ์žˆ๋‹ค. ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๋Š” ๊ฐ ์กฐ์ง ๊ฐ„์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์˜ ํšจ์œจ์„ฑ์— ๋‹ฌ๋ ค ์žˆ๋‹ค. ์กฐ์ง์ด ์„ฑ์ˆ™ํ•˜์ง€ ๋ชปํ•˜์—ฌ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์ด ์–ด๋ ค์šฐ๋ฉด ๊ด€๋ จ ๋น„์šฉ์€ ๋งค์šฐ ๋†’์•„์ง„๋‹ค. Figure V-8. ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง์˜ ๊ฐœ๋…๋„ ์ˆ˜์ง ์กฐ์ง์ด ๋งก๋Š” ๊ณ ๊ฐ๊ธฐ์—… ๋˜๋Š” ์‚ฌ์—…์˜์—ญ, ๊ทธ๊ฒƒ์„ B2B ์‚ฌ์—…์—์„œ๋Š” ์–ด์นด์šดํŠธ 2๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. B2B ๊ธฐ์—…์˜ ์„ฑ๊ณผ๋Š” ์ฒ ์ €ํžˆ ํŒŒ๋ ˆํ†  ๋ฒ•์น™์„ ๋”ฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์–ด์นด์šดํŠธ ๊ด€๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ˆ˜ํ–‰ํ•ด ๋‚ด๋Š๋ƒ์— ๋”ฐ๋ผ ์‚ฌ์—…์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๊ฐ€ ๋‹ฌ๋ ค์žˆ๋‹ค๊ณ  ํ•ด๋„ ๊ณผ์–ธ์ด ์•„๋‹ˆ๋‹ค ๋”ฐ๋ผ์„œ ๊ฑฐ๋ž˜ํ•˜๋Š” ๊ณ ๊ฐ ์ค‘ ํ•ต์‹ฌ ๊ณ ๊ฐ๊ตฐ์„ ํŒŒ์•…ํ•˜๊ณ  ์„ ๋ณ„๋œ ๊ณ ๊ฐ๋“ค์€ ์ง‘์ค‘ ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ๋™์ผํ•œ ์‹œ์žฅ๊ณผ ์‚ฐ์—…์—์„œ๋„ ๊ณ ๊ฐ๋ณ„๋กœ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋Š” ํฌ์ธํŠธ๊ฐ€ ๋‹ค๋ฅด๋‹ค. ์–ด๋–ค ๊ณ ๊ฐ์€ ์ตœ์ €๊ฐ€๋ฅผ ์›ํ•˜๋ฉฐ, ์–ด๋–ค ๊ณ ๊ฐ์€ ์ „๋‹ด ์„œ๋น„์Šค, ์–ด๋–ค ๊ณ ๊ฐ์€ ๊ฒ€์ฆ๋œ ๊ธฐ์ˆ  ๋“ฑ ๋‹ค์–‘ํ•œ ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๊ฐ€ ์กด์žฌํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์–‘ํ•œ ๋‹ˆ์ฆˆ๋ฅผ ๋น„์šฉ ํšจ์œจ์ ์œผ๋กœ ๋Œ€์‘ํ•˜๊ณ  ์ตœ๊ณ ์˜ ๊ฐ€์น˜๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ๋งŽ์€ ๊ธฐ์—…๋“ค์ด ํ•ต์‹ฌ ์–ด์นด์šดํŠธ ๊ด€๋ฆฌ(Key Account Management: KAM)์— ์ฃผ๋ ฅํ•˜๊ณ  ์žˆ๋‹ค. 20.3 ์ง€์‹๊ฒฝ์˜ ์ปจ์„คํŒ…์€ ์ง€์‹ ๊ฒฝ์ œ(Knowledge Economy) ์‹œ๋Œ€์˜ ๋Œ€ํ‘œ์ ์ธ ์ง€์‹ ์‚ฐ์—…(Knowledge Business)์ด๋‹ค. 1990๋…„๋Œ€ ํ›„๋ฐ˜๋ถ€ํ„ฐ 2000๋…„๋Œ€ ์ดˆ๋ฐ˜๊นŒ์ง€ ์ด๋Ÿฐ ์ธ์‹๊ณผ ํŠธ๋ Œ๋“œ์— ๋”ฐ๋ผ ์ง€์‹ ๊ฒฝ์˜(Knowledge Management) ์—ดํ’์ด ๋ถˆ๊ธฐ๋„ ํ•˜์˜€๋‹ค. ์ง€์‹์„ ์ •์˜ํ•˜๊ณ  ์ง€์‹์„ ๊ด€๋ฆฌํ•œ๋‹ค๋Š” ์ทจ์ง€๋กœ ์ง€์‹๊ด€๋ฆฌ์‹œ์Šคํ…œ(KMS)์„ ๊ตฌ์ถ•ํ•˜๊ธฐ๋„ ํ•˜๊ณ  ๋‚ด๋ถ€ ํ”„๋กœ์„ธ์Šค๋ฅผ ํ˜์‹ ํ•˜๋Š” ๋“ฑ ๊ธฐ์—…๋งˆ๋‹ค ๋‹ค์–‘ํ•œ ์‹œ๋„๊ฐ€ ์žˆ์—ˆ๊ณ  ๊ทธ ๊ณผ์ •์—์„œ ๋‹ค์–‘ํ•œ ์„ฑ๊ณต๊ณผ ํ•œ๊ณ„๊ฐ€ ๋‚˜ํƒ€๋‚ฌ๋‹ค. 20๋…„์ด ์ง€๋‚œ ์ง€๊ธˆ์€ ๊ณผ๊ฑฐ์™€ ๊ฐ™์ด ์ง€์‹๊ฒฝ์˜์ด ๊ทธ๋ ‡๊ฒŒ ํ•˜์ด๋ผ์ดํŠธ ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์ง€๋งŒ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ๊ทธ๋“ค์˜ ํ•ต์‹ฌ ์ž์‚ฐ(Core Asset)์ด ์ง€์‹๊ฒฝ์˜์— ๊ธฐ๋ฐ˜ํ•œ ์ง€์‹ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(Knowledge-Base)๋ผ๊ณ  ๋งํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ์ฃผ์ €ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋งŒํผ ์ง€์‹์˜ ๊ณต์œ ์™€ ํ™•๋Œ€ ์ƒ์‚ฐ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ปจ์„คํŒ… ๊ธฐ์—…์—์„œ ์ง€์‹์„<NAME>๋Š” ์ˆ˜๋‹จ์€ ํฌ๊ฒŒ ์ง€์‹๊ฒฝ์˜ ์‹œ์Šคํ…œ(Knowledge Management System: KMS)์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ์ง€์‹ ์ปค๋ฎค๋‹ˆํ‹ฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๋‘ ๊ฐ€์ง€์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ, KMS๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์€ ํฌ๊ฒŒ 4๊ฐœ์˜ Level ๋˜๋Š” ์„ฑ์ˆ™๋„๋กœ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ ์‹œ๋„์ด์ž ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฒƒ์€ ํ”„๋กœ์ ํŠธ ์‚ฐ์ถœ๋ฌผ์„ ์ˆ˜์ง‘ํ•˜๊ณ  ๋ถ„๋ฅ˜ํ•˜๋ฉฐ ๊ฒ€์ƒ‰ ์‹œ์Šคํ…œ์„ ํ™œ์šฉํ•˜์—ฌ ๋งŽ์€ ์ปจ์„คํ„ดํŠธ๋“ค์ด ๊ทธ๊ฒƒ์„ ์žฌ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฉ๋ฒ•๋ก ํ™”ํ•˜๊ฑฐ๋‚˜ ํ•ต์‹ฌ ์ง€์‹์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ์‹์ด๋‹ค. ์ผ์ข…์˜ ์ง€์‹ ์ €์žฅ์†Œ(Knowledge Repository) ํ˜•ํƒœ์ธ๋ฐ ์ด๋Ÿฐ ์ˆ˜์ค€์„ KMS Level #1์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์กฐ๊ธˆ ๋” ์„ฑ์ˆ™ํ•˜์—ฌ KMS Level #2๊ฐ€ ๋˜๋ฉด ์ด๋Ÿฐ์ €๋Ÿฐ ์ €์žฅ์†Œ๊ฐ€ ๋งค์šฐ ๋งŽ์•„์ง€๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๊ฒƒ์„ ๋ชจ๋‘ ๋ฌถ์–ด์„œ ํฌํƒˆ(Portal)๋กœ ์”Œ์šฐ๊ณ  ์šฐ์ˆ˜ํ•œ ๊ฒ€์ƒ‰ ์—”์ง„์„ ํ†ตํ•ด ๋‹ค์–‘ํ•˜๊ณ  ํšจ๊ณผ์ ์ธ ๊ฒ€์ƒ‰์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค. KMS Level #3๊ฐ€ ๋˜๋ฉด KMS๊ฐ€ ๋ณ„๋„์˜ ์ •๋ณด์‹œ์Šคํ…œ์ด ์•„๋‹ˆ๋ผ ์ผ๋ฐ˜์ ์ธ ์—…๋ฌด ์‹œ์Šคํ…œ์œผ๋กœ ๋…น์•„์ ธ ๋ฒ„๋ฆฐ๋‹ค. ๋ณ„๋„์˜ KMS๊ฐ€ ์—†์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ง€์‹๊ฒฝ์˜์„ ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ์‚ฌ์‹ค์€ ๊ทธ๊ฒƒ์„ ์—…๋ฌด ํ๋ฆ„์— ๋…น์ธ๋‹ค. ์ฆ‰, ์—…๋ฌด ํ๋ฆ„(Workflow)์„ ๊ตฌํ˜„ํ•˜๊ณ  ๋‹จ๊ณ„์ ์œผ๋กœ ์—…๋ฌด๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ํ‘œ์ค€์ด๋‚˜ ์‚ฌ๋ก€, ํ…œํ”Œ๋ฆฟ์ฒ˜๋Ÿผ ํ™œ์šฉํ•˜์—ฌ ์ผ์˜ ์ƒ์‚ฐ์„ฑ์„ ๊ทน๋Œ€ํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ KMS Level #4๊ฐ€ ๋˜๋ฉด ๊ทธ๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ์—…๋ฌด ์‹œ์Šคํ…œ์„ ์„œ๋น„์Šค ๋‹จ์œ„๋กœ ์–ธ์ œ ์–ด๋””์„œ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํด๋ผ์šฐ๋“œ๋‚˜ ๋ชจ๋ฐ”์ผ ํ™˜๊ฒฝ์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค. ์ด๊ฒƒ์ด ์ •๋ณด์‹œ์Šคํ…œ ๊ด€์ ์—์„œ ๋ณธ KMS์˜ ๋ฐœ๋‹ฌ ๋ชจ์Šต์ด๋ผ๊ณ  ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๊ฐ€ ์†Œ์œ„<NAME>์ง€(Explicit Knowledge)์˜ ์ƒ์„ฑ๊ณผ ํ™œ์šฉ์— ๋Œ€ํ•œ ์ ‘๊ทผ ๋ฐฉ๋ฒ•๊ณผ ๋ฐœ๋‹ฌ ๊ณผ์ •์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ๋‘ ๋ฒˆ์งธ๋Š” ์•”๋ฌต์ง€(Implicit Knowledge) ์ฆ‰, ์‚ฌ๋žŒ ๋จธ๋ฆฟ์†์˜ ์ง€์‹์ด๋‚˜ ๊ฒฝํ—˜ ๋“ฑ์„<NAME>๋Š” ๊ฒƒ์ด๋‹ค. ์˜ํ™” ๋งคํŠธ๋ฆญ์Šค์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ๋’คํ†ต์ˆ˜์— ๊ฝ‚๊ณ  ์ง€์‹์„ ๋‚ด๋ ค๋ฐ›๋Š” ์ผ์€ ์ง€๊ธˆ ํ˜„์žฌ์—๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•Œ๋ ค์ง„ ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์ปค๋ฎค๋‹ˆํ‹ฐ[3]๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฉด๋Œ€ ๋ฉด(face-to-face) ์ ‘์ด‰์„ ํ™œ์„ฑํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์—…๋ฌด ์‹œ๊ฐ„์˜ ์ผ์ • ๋ถ€๋ถ„(์˜ˆ๋ฅผ ๋“ค๋ฉด 1/5 ์ •๋„)๋ฅผ ์ตœ๊ทผ ํ™”๋‘๊ฐ€ ๋˜๊ณ  ์žˆ๋Š” ์‚ฐ์—…์ด๋‚˜ ๊ธฐ์ˆ ์— ๋Œ€ํ•œ ์—ฐ๊ตฌ์™€ ์Šคํ„ฐ๋””, ์ƒˆ๋กœ์šด ๋ฐฉ์‹์˜ ์‹œ๋„๋‚˜ ๋ณธ์ธ์ด ํ•˜๊ณ  ์‹ถ์€ ์ผ์„ ์ž์œ ๋กญ๊ฒŒ ์ง„ํ–‰ํ•˜๊ณ  ๊ฐ™์€ ๋™๊ธฐ๋‚˜ ๊ด€์‹ฌ์„ ๊ฐ€์ง„ ์‚ฌ๋žŒ๋“ค๋ผ๋ฆฌ ๋ชจ์—ฌ ์ปค๋ฎค๋‹ˆํ‹ฐ ์•ˆ์—์„œ ํ™œ๋ฐœํ•˜๊ฒŒ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์œผ๋กœ ์ธ์  ๋„คํŠธ์›Œํฌ๋ฅผ ํ™œ์„ฑํ™”ํ•˜๊ณ  ๊ถ๊ทน์ ์œผ๋กœ ์ „๋ฌธ๊ฐ€ ๋„คํŠธ์›Œํฌ(Knowledge Experts)๋กœ ๋ฐœ์ „ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. ์•ž์„œ ์†Œ๊ฐœํ•œ CoE ์กฐ์ง์ด ์ด๋Ÿฐ ๋ถ€๋ถ„์„ ๋ฆฌ๋“œํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ปจ์„คํŒ… ์‚ฌ์—…๊ฐœ๋ฐœ๊ณผ ์ดํ–‰๋„ ๊ฒฐ๊ตญ ์ง€์‹๊ฒฝ์˜์ด ๋ณด์ด์ง€ ์•Š๊ฒŒ ๋™์ž‘ํ•˜๋ฉด์„œ ์ปจ์„คํŒ… ํŽŒ์˜ ์ž์‚ฐ์œผ๋กœ ์—ญ๋Ÿ‰์œผ๋กœ ์Œ“์—ฌ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ์„ ๋•Œ ๋ณด๋‹ค ๋†’์€ ์ƒ์‚ฐ์„ฑ์œผ๋กœ ํ›Œ๋ฅญํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ํ”„๋ฆฌ๋žœ์„œ ์ปจ์„คํ„ดํŠธ๋“ค์„ ๋งŒ๋‚˜์„œ ์ด์•ผ๊ธฐ๋ฅผ ๋“ฃ๋‹ค ๋ณด๋ฉด ๊ฐ€์žฅ ์•„์‰ฌ์›Œํ•˜๋Š” ๊ฒƒ์ด KMS์ด๋‹ค. ๊ทธ๋“ค์ด ๊ธฐ์—…์— ์†ํ•ด ์žˆ์—ˆ์„ ๋•Œ๋Š” ๊ทธ ๋ฐฉ๋Œ€ํ•œ ์ž๋ฃŒ์™€ ๋‹ค์–‘ํ•œ ๋„คํŠธ์›Œํฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์—ฌ๋ ค์›€์„ ํ•ด๊ฒฐํ•˜๋ฉด์„œ ์•„์‰ฌ์šด ์ค„ ๋ชฐ๋ž๋Š”๋ฐ ๋…๋ฆฝํ•˜์—ฌ ์ผ์„ ํ•˜๋‹ค ๋ณด๋‹ˆ ๊ทธ ๋ถ€๋ถ„์˜ ํ˜œํƒ์ด ์•„์‰ฝ๊ณ  ๊ทธ๋ฆฝ๋”๋ผ ํ•˜๋Š” ์ด์•ผ๊ธฐ๋Š” ํ”„๋ฆฌ๋žœ์„œ ์ปจ์„คํ„ดํŠธ ๋ˆ„๊ตฌ๋ฅผ ๋งŒ๋‚˜๋˜ ๊ณตํ†ต๋œ ์ด์•ผ๊ธฐ์ด๋‹ค. 21.epilogue ์ปจ์„คํŒ… ์ดํ–‰์˜ ํ•ต์‹ฌ์€ ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ฐ ๋ณด๊ณ ์„œ ์ž‘์„ฑ์ด์ง€๋งŒ ๊ทธ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ๊ฒƒ์€ ์ œ์•ˆ์„ ์œ„ํ•ด ๋ณธ ์‚ฌ์•ˆ์— ๋Œ€ํ•ด ๊ฐ€์กŒ๋˜ ์ „๋žต์  ์‚ฌ๊ณ ๋ฅผ ์ง€์†์ ์œผ๋กœ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Table V-12๋Š” ์ „๋žต์  ์‚ฌ๊ณ ์˜ ๋‹จ๊ณ„์  ํ๋ฆ„์„ ์„ค๋ช…ํ•œ ๊ฒƒ์ธ๋ฐ ์ปจ์„คํŒ… ์ œ์•ˆ์ด๋˜ ์ปจ์„คํŒ… ์ดํ–‰์ด๋˜ ์‚ฌ๊ณ ์˜ ๊ธฐ๋ณธ์ ์ธ ํ๋ฆ„์œผ๋กœ์„œ ์ˆ™์ง€ํ•ด์•ผ ํ•  ์‚ฌํ•ญ๋“ค์ด๋‹ค. ์ปจ์„คํŒ… ๊ธฐ์—…์˜ ์ง€์‹๊ณผ ๊ฒฝํ—˜, ๋„๊ตฌ๋ฅผ ์ถฉ๋ถ„ํžˆ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ง€์‹ ์ž์‚ฐ์˜ ํšจ์œจ์  ๊ตฌ์ถ•๊ณผ ํ™œ์šฉ, ๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฒƒ์„ ์ง€์›ํ•˜๋Š” ์ธํ”„๋ผ์˜ ๊ตฌ์ถ•์ด ๋ฌด์—‡๋ณด๋‹ค๋„ ์ค‘์š”ํ•˜๋‹ค. ์ œ20์žฅ์—์„œ๋Š” ๊ทธ๊ฒƒ๋“ค์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž. Table V-12. ์ „๋žต์  ์‚ฌ๊ณ ์˜ ๋‹จ๊ณ„ 20.1 ์ปจ์„คํŒ… ์‚ฐ์ถœ๋ฌผ ์ผ(work)์˜ ๊ด€์ ์—์„œ ๋ณด๋ฉด ์ปจ์„คํŒ…์€ ํ”„๋กœ์ ํŠธ์˜ ํ•œ ์ข…๋ฅ˜๋กœ์„œ ์‹œ์ž‘๊ณผ ๋์ด ์žˆ๊ณ  ๊ฐ๊ฐ์˜ ์ปจ์„คํŒ…๋งˆ๋‹ค ๊ณ ์œ ํ•œ ํŠน์ƒ‰์ด ์žˆ์œผ๋ฉฐ ๊ทธ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋Š” ๋ฐ˜๋“œ์‹œ ์‚ฐ์ถœ๋ฌผ(deliverables)๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์ปจ์„คํŒ… ์‚ฐ์ถœ๋ฌผ์€ ๋ณด๊ณ ์„œ(Report)์ธ๋ฐ, ์ปจ์„คํŒ…์˜ ์ข…๋ฅ˜์™€ ์„ฑ๊ฒฉ์— ๋”ฐ๋ผ์„œ ๊ทธ ์„ธ๋ถ€์ ์ธ ๋‚ด์—ญ์€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์œผ๋‚˜ ๋งŽ์€ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋ณด๊ณ ์„œ๋Š” ๋‹ค์Œ์˜ ์„ธ ๊ฐ€์ง€์ด๋‹ค. ์ฐฉ์ˆ˜ ๋ณด๊ณ ์„œ(Inception Report) 'Kick-Off Meeting ๋ณด๊ณ ์„œ'๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋Š” ์ฐฉ์ˆ˜ ๋ณด๊ณ ์„œ๋Š” ์ปจ์„คํŒ… ์ œ์•ˆ์„œ์—์„œ ์ œ์‹œํ–ˆ๋˜ ์ปจ์„คํŒ… ์ดํ–‰ ๋ฐฉ์•ˆ๋“ค์„ ์„ ๋ฐœ๋œ ํˆฌ์ž… ์ธ์›๋“ค๊ณผ ํ•จ๊ป˜ ์ œ์‹œํ•œ ๊ธฐ๊ฐ„ ์•ˆ์— ์ˆ˜ํ–‰ํ•˜๊ฒ ์Œ์„ ๊ณ ๊ฐ์—๊ฒŒ ์ฒœ๋ช…ํ•˜๋Š” ๋ณด๊ณ ์„œ์ด๋‹ค. ๋ฏผ๊ฐ„ ๊ธฐ์—…์ด ๋ฐœ์ฃผํ•œ ๊ฒฝ์˜์ „๋žต ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ ์ตœ๊ณ ๊ฒฝ์˜์ž(CEO)๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๋ณด๊ณ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ณ , IT ์ปจ์„คํŒ…์˜ ๊ฒฝ์šฐ IT ๋‹ด๋‹น ์ž„์›(CIO)์—๊ฒŒ ์ฃผ๋กœ ๋ณด๊ณ ํ•œ๋‹ค. ๊ณต์‹์ ์œผ๋กœ ๊ณ ๊ฐ์—๊ฒŒ ํ”„๋กœ์ ํŠธ๊ฐ€ ์‹œ์ž‘๋˜์—ˆ์Œ์„ ์•Œ๋ฆฌ๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์˜ ์˜์˜, ์ˆ˜ํ–‰ ๋ฐฉ์•ˆ, ๊ธฐ๋Œ€ํšจ๊ณผ, ํŒ€์› ์†Œ๊ฐœ, ํ–ฅํ›„ ๊ณ„ํš ๋“ฑ์— ๋Œ€ํ•ด ์ž˜ ์ •๋ฆฌํ•ด์„œ ๋ณด๊ณ ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋ฉฐ, ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋ฅผ ์‹ค์งˆ์ ์œผ๋กœ ๋„์™€์ค„ ๊ณ ๊ฐ๊ธฐ์—… ๋‚ด ๊ฐ ๋ถ€์„œ์žฅ๋“ค ๋ฐ ์‹ค๋ฌด์ง„๋“ค๊ณผ ๊ณต์‹์ ์œผ๋กœ ์ฒ˜์Œ ๋Œ€๋ฉดํ•˜๋Š” ์ˆœ๊ฐ„ ์ผ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋งค์šฐ ์ „๋žต์ ์œผ๋กœ ๋Œ€์‘ํ•ด์•ผ ํ•œ๋‹ค. ์ค‘๊ฐ„ ๋ณด๊ณ ์„œ(Interim Report) ์ค‘๊ฐ„๋ณด๊ณ ์„œ๋Š” ํ”„๋กœ์ ํŠธ ์ฐฉ์ˆ˜ ์ดํ›„์— ์ˆ˜ํ–‰ํ•œ ํ˜„ํ™ฉ ๋ถ„์„์˜ ๊ฒฐ๊ณผ์™€ ๊ฐœ์„  ๊ธฐํšŒ ๋ถ„์•ผ ์„ ์ •์— ๋Œ€ํ•œ ๋™์˜๋ฅผ ์–ป๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค. ๊ฐ€์„ค์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด์Šˆ๋‚˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ํ•ด๋ฒ•์ด๋‚˜ ์„ค๋ฃจ์…˜์— ๋Œ€ํ•œ ํฐ ๊ทธ๋ฆผ(Big Picture)์„ ์ œ์‹œํ•˜๊ณ  ํ–ฅํ›„ ์ผ์ •์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ ๊ธฐ๊ฐ„์ด ๊ธธ ๊ฒฝ์šฐ, ๊ณ ๊ฐ์˜ ์š”์ฒญ์— ์˜ํ•ด ์›”๋ณ„/๋ถ„๊ธฐ๋ณ„ ๋ณด๊ณ ๋ฅผ ์š”๊ตฌ๋ฐ›๊ธฐ๋„ ํ•˜๋ฉฐ, ํ˜„ํ™ฉ ๋ถ„์„์„ ์œ„ํ•œ ์›Œํฌ์ˆ(workshop)์ด๋‚˜ ์ธํ„ฐ๋ทฐ(interview)๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ๋ณดํ†ต ์ค‘๊ฐ„๋ณด๊ณ ๋ฅผ ์ค€๋น„ํ•œ๋‹ค. ์ข…๋ฃŒ ๋ณด๊ณ ์„œ(Final Report) ์ข…๋ฃŒ ๋ณด๊ณ ์„œ๋Š” ์ค‘๊ฐ„๋ณด๊ณ ์„œ์—์„œ ์ œ์‹œ๋œ ๊ฐœ์„  ๊ธฐํšŒ ๋ถ„์•ผ๋ฅผ ํ˜„์žฅ์—์„œ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐœ๋ฐœํ•œ ํ”„๋กœ๊ทธ๋žจ๊ณผ ์‹คํ–‰๊ณผ์ œ(Key Initiatives ๋˜๋Š” Quick win ๊ณผ์ œ), ๊ทธ ๊ณผ์ œ๋ฅผ ์ดํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ํ›„์† ์—ฐ๊ณ„ ํ”„๋กœ์ ํŠธ๋‚˜ ํ›„์† ์กฐ์น˜์‚ฌํ•ญ ๋“ฑ์˜ ์ดํ–‰ ๊ณ„ํš์— ๋Œ€ํ•ด ์ตœ๊ณ ๊ฒฝ์˜์ž์™€ ๊ฒฝ์˜์ง„๋“ค์˜ ๋™์˜์™€ ์ถ”์ง„ ๊ถŒํ•œ์„ ์–ป๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค. ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๊ฐ€ ์ž˜ ์ง„ํ–‰๋˜๋ฉด ๋ณดํ†ต ์ข…๋ฃŒ ๋ณด๊ณ  ์ „์— ํ›„์† ํ”„๋กœ์ ํŠธ์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๊ฐ€ ๊ณ ๊ฐ๊ณผ ํ•จ๊ป˜ ์˜ค๊ณ  ๊ฐ€๋ฉฐ ์žฌ๊ณ„์•ฝ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ์ปจ์„คํŒ… ๋ณด๊ณ ์„œ๋Š” โ€˜๋ˆ„๊ฐ€ ์ž‘์„ฑํ–ˆ๋Š๋ƒ?โ€™๋ณด๋‹ค โ€˜์‹คํ–‰์— ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋Š๋ƒ?โ€™๊ฐ€ ๋” ์ค‘์š”ํ•˜๋ฉฐ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์„ค๋ฃจ์…˜์„ ์ œ์‹œํ•  ์ˆ˜ ์žˆ์„ ๋•Œ ๊ทธ ๊ฐ€์น˜๊ฐ€ ๋”์šฑ ๋น›๋‚œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋Š” ํ›Œ๋ฅญํ•œ ์ปจ์„คํŒ… PM๊ณผ ์ปจ์„คํŒ… ํŒ€๋งŒ์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์†Œ์œ„ ๋งํ•˜๋Š” ๋ฐฑ์˜คํ”ผ์Šค(Back Office) ํŒ€์ด ๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ์ง€์›์กฐ์ง์ด ํ•„์š”ํ•˜๋‹ค. 20.2 CoE์˜ ์šด์˜ ์˜ค๋Š˜๋‚ ์€ ์ธํ„ฐ๋„ท ๋“ฑ ๋‹ค์–‘ํ•œ ํ†ต์‹ ๋ง์œผ๋กœ ์—ฐ๊ฒฐ๋œ ๊ธ€๋กœ๋ฒŒ ์—ฐ๊ฒฐ์‹œ๋Œ€์ด๋‹ค ๋ณด๋‹ˆ ์‚ฌ์—…์ด๋‚˜ ๊ธฐ์ˆ ๋„ ์„ฑ๊ณต ์‚ฌ๋ก€๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒƒ์ด ์‹ค์‹œ๊ฐ„(realtime)์œผ๋กœ ์ „ ์„ธ๊ณ„์— ๊ณต์œ ๋˜๊ณ  ์žˆ๋‹ค. ๊ธฐ์—… ์กฐ์ง์˜ ํ˜•ํƒœ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ธ๋ฐ GE๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘๋œ ์‚ฌ์—…๋ถ€ ์กฐ์ง(SBU), IBM์ด๋‚˜ ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ๊ฐ€ ์ฑ„ํƒํ•˜๊ณ  ์žˆ๋Š” ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ์—… ์กฐ์ง์˜ ํ˜•ํƒœ๋Š” ๊ทธ ๊ธฐ์—…์˜ ๊ฒฝ์Ÿ๋ ฅ ํ™•๋ณด์— ๋งค์šฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•œ๋‹ค. ๊ทธ์ค‘์— โ€˜Center of Excellenceโ€™๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ์กฐ์ง ํ˜•ํƒœ๊ฐ€ ์žˆ๋‹ค. ์‚ฐ์—… ๋ฐ ์—…์ข…์— ๋”ฐ๋ผ ๊ทธ ์˜๋ฏธ๋ฅผ ์กฐ๊ธˆ์”ฉ ๋‹ฌ๋ฆฌํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ ์ปจ์„คํŒ… ์‚ฐ์—…์—์„œ CoE๋Š” โ€˜์ปจ์„คํ„ดํŠธ ์œก์„ฑโ€™๊ณผ โ€˜์ปจ์„คํŒ… ์‚ฌ์—…์˜ ๋ฐฑ์˜คํ”ผ์Šค ์—ญํ• โ€™์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๊ทธ ๋‚ด์šฉ์„ ์‚ดํŽด๋ณด๋ฉด ์ฒซ ๋ฒˆ์งธ, ์ปจ์„คํ„ดํŠธ ์œก์„ฑ ๋ฐ ๊ด€๋ฆฌ์ด๋‹ค. ์ด์ œ ๋ง‰ ์ž…์‚ฌํ•œ ์‹ ์ž… ์ปจ์„คํ„ดํŠธ๋“ค์—๊ฒŒ ์ปจ์„คํŒ… ์Šคํ‚ฌ์ด๋‚˜ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ•, ๋ฐฉ๋ฒ•๋ก  ๋“ฑ์„ ๊ฐ€๋ฅด์น˜๋ฉด์„œ ์ปจ์„คํ„ดํŠธ๋กœ์„œ์˜ ๊ธฐ๋ณธ ์†Œ์–‘์„ ์Œ“๊ฒŒ ๋„์™€์ค€๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ปจ์„คํŒ… ํ”„๋กœ์ ํŠธ๋Š” ๊ฐ ์‚ฐ์—… ๋„๋ฉ”์ธ์— ์†ํ•œ ์‹œ๋‹ˆ์–ด ์ปจ์„คํ„ดํŠธ๋“ค์ด ํ”„๋กœ์ ํŠธ๋ฅผ ๋ฐœ๊ตดํ•˜๋ฉด ๋Œ€๋ถ€๋ถ„ ๋ณธ์ธ์ด PM์„ ํ•˜๊ณ  CoE๋ฅผ ํ†ตํ•ด ํˆฌ์ž…๋  ์ธ์›์„ ์ˆ˜๋ฐฐํ•˜๋Š” ์ ˆ์ฐจ๋ฅผ ๊ฑฐ์ณ ํ”„๋กœ์ ํŠธ ํŒ€์„ ๊ตฌ์„ฑํ•œ๋‹ค.[1] ๋”ฐ๋ผ์„œ ํ›Œ๋ฅญํ•œ ์—ญ๋Ÿ‰์„ ๋ณด์œ ํ•œ ์ข‹์€ ์ธ์žฌ๋“ค์„ ์œก์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์€ CoE์˜ ์ค‘์š”ํ•œ ์—ญํ• ์ด์ž ๋ชฉํ‘œ๊ฐ€ ๋œ๋‹ค. ๋‘ ๋ฒˆ์งธ, ๋ฐฑ์˜คํ”ผ์Šค(Back Office)๋กœ์„œ ์ปจ์„คํŒ… ์ง€์‹๊ณผ ์ˆ˜ํ–‰ ์‚ฌ๋ก€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ผ์ด๋‹ค. CoE์™€ ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ๊ฐœ๋…์ด์ง€๋งŒ โ€˜Competency Center(CC)โ€™๋‚˜ โ€˜Resource Center(RC)โ€™๋ผ๊ณ  ํ•˜๋Š” ์กฐ์ง ํ˜•ํƒœ๊ฐ€ ์ธ๋ ฅ์„ ์œก์„ฑํ•˜๊ณ  ํ”„๋กœ์ ํŠธ์— ํˆฌ์ž…ํ•˜๋ฉฐ ๊ทธ๋“ค์˜ ๊ฐ€๋™๋ฅ ์„ ๊ด€๋ฆฌํ•˜๋Š” ์ผ์— ์ง‘์ค‘ํ•œ๋‹ค๋ฉด, CoE๋Š” ํ•œ ๋ฐœ ๋‚˜์•„๊ฐ€์„œ ๋ฐฉ๋ฒ•๋ก ์ด๋‚˜ ๋„๊ตฌ์™€ ๊ธฐ๋ฒ• ๋“ฑ ์ปจ์„คํŒ…์˜ ํ•ต์‹ฌ ์ง€์‹(Core Knowledge)์„ ๊ฐœ๋ฐœ ๋ฐ ๊ด€๋ฆฌํ•˜๋ฉฐ ๊ฐ ํ”„๋กœ์ ํŠธ ํ˜„์žฅ์— ๋ฐฐํฌ, ์ง€์›ํ•˜๋Š” ์ผ๋„ ๋งก๋Š”๋‹ค. ๋˜ํ•œ, ์ž์›๊ณผ ์—ญ๋Ÿ‰์ด ํ—ˆ๋ฝํ•œ๋‹ค๋ฉด ์‚ฐ์—… ๋ฆฌ์„œ์น˜๋ฅผ ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋ณด๊ณ ์„œ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๊ธฐ๋„ ํ•˜๊ณ , ๋‹ค์–‘ํ•œ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•ด์„œ ๊ณ ๊ฐ์—๊ฒŒ ํŠธ๋ Œ๋“œ์™€ ํ†ต์ฐฐ๋ ฅ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ์ž๋ฃŒ๋“ค์„ ์ƒ์„ฑํ•œ๋‹ค. ์›นํŽ˜์ด์ง€๋‚˜ ์†Œ์…œ๋ฏธ๋””์–ด๋ฅผ ์šด์˜ํ•˜๋ฉฐ ๋งˆ์ผ€ํŒ…์„ ํ•˜๊ธฐ๋„ ํ•˜๊ณ  ์‚ฐ์—…์ด๋‚˜ ๊ธฐ์ˆ  ์ฝ˜ํผ๋Ÿฐ์Šค๋ฅผ ๊ฐœ์ตœํ•˜์—ฌ ๊ณ ๊ฐ๋“ค์„ ์ดˆ๋น™ํ•˜๊ณ  ๊ทธ๋“ค์˜ ์‚ฌ๊ณ  ๋ฆฌ๋”์‹ญ(Thought Leadership)์„ ์†Œ๊ฐœํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ–๊ธฐ๋„ ํ•œ๋‹ค. ๋งฅํ‚จ์ง€๋‚˜ ๋ฒ ์ธ, ์—‘์„ผ์ธ„์–ด ๋“ฑ ์—ญ์‚ฌ๊ฐ€ ์˜ค๋ž˜๋œ ์™ธ๊ตญ๊ณ„ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ์ด ๋ฐฑ์˜คํ”ผ์Šค ๊ธฐ๋Šฅ์ด ๋งค์šฐ ์šฐ์ˆ˜ํ•˜๋‹ค. ๋‹ค์–‘ํ•œ ์ง€์  ์ž์‚ฐ์„ ๊ฐœ๋ฐœํ•˜๊ณ  ์ปจ์„คํ„ดํŠธ๋“ค์—๊ฒŒ<NAME>์—ฌ ์ปจ์„คํ„ดํŠธ๋“ค์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ๋„์™€์ฃผ๊ณ  ๋˜ ๊ทธ ์ˆ˜ํ–‰ ์‚ฌ๋ก€๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ ๋ฐฉ๋ฒ•๋ก ์ด๋‚˜ ๋„๊ตฌ, ๊ธฐ๋ฒ•์ด ์ •์ œ๋  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค. ์ด ์šฐ์ˆ˜ ์„ฑ๊ณต ์‚ฌ๋ก€(Best Practice)๋‚˜ ๊ณต์œ ๋˜๋Š” ๋‹ค์–‘ํ•œ ๊ฒฝํ—˜๋“ค์„ ํ™œ์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๊ณ  ์žˆ๋‹ค. Break #22. ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง์€ ๊ธ€๋กœ๋ฒŒ ๊ธฐ์—…๋“ค์˜ ๋Œ€ํ‘œ์ ์ธ ์กฐ์ง ํ˜•ํƒœ๋กœ ๊ธ€๋กœ๋ฒŒ ์‚ฌ์—…์„ ์ถ”์ง„ํ•˜๋Š” ๊ฐ€์žฅ ์ด์ƒ์ ์ธ ํ˜•ํƒœ์˜ ์กฐ์ง์ด๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค. ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง์€ Figure V-8์ฒ˜๋Ÿผ ๊ธฐ์—…์˜ ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ์ƒ์‚ฐํ•˜๋Š” ์กฐ์ง์„ ์ˆ˜ํ‰์ (Horizontal)์œผ๋กœ ๋†“๊ณ , ์‚ฐ์—… ๋˜๋Š” ์‚ฌ์—…์˜์—ญ์— ๊ธฐ๋ฐ˜ํ•˜๋Š” ์‚ฌ์—… ์กฐ์ง์„ ์ˆ˜์ง์ (Vertical)์œผ๋กœ ๋ฐฐ์น˜ํ•œ๋‹ค. ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง์—์„œ ์ˆ˜์ง์  ๋ฐฐ์น˜ ์กฐ์ง์ด ๊ณ ๊ฐ๊ณผ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ํ•˜๋ฉฐ ์‚ฌ์—…์„ ๋‹ด๋‹นํ•œ๋‹ค. ์ฆ‰, ์ˆ˜ํ‰ ์กฐ์ง์€ ์ œํ’ˆ ์„œ๋น„์Šค, ์„ค๋ฃจ์…˜ ๊ฐœ๋ฐœ์— ์ฃผ๋ ฅํ•˜๊ณ  ์ˆ˜์ง ์กฐ์ง์€ ๊ณ ๊ฐ์ฑ„๋„ ์—ญํ• ์„ ํ•˜๋ฉด์„œ ์ˆ˜ํ‰์กฐ์ง์ด ๋งŒ๋“ค์–ด๋‚ด๋Š” ์ œํ’ˆ๊ณผ ์„œ๋น„์Šค, ์„ค๋ฃจ์…˜์„ ๊ณ ๊ฐ์—๊ฒŒ ํŒ๋งคํ•˜๋Š” ์—ญํ• ์„ ๋งก๋Š”๋‹ค. ์‚ฌ์—…์˜ ๋ณต์žก์„ฑ์— ๋”ฐ๋ผ 3~5์ถ• ๋˜๋Š” ๊ทธ ์ด์ƒ์„ ์ด๋ฃจ๋Š” ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง ํ˜•ํƒœ๋„ ์žˆ๋‹ค. ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๋Š” ๊ฐ ์กฐ์ง ๊ฐ„์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์˜ ํšจ์œจ์„ฑ์— ๋‹ฌ๋ ค ์žˆ๋‹ค. ์กฐ์ง์ด ์„ฑ์ˆ™ํ•˜์ง€ ๋ชปํ•˜์—ฌ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์ด ์–ด๋ ค์šฐ๋ฉด ๊ด€๋ จ ๋น„์šฉ์€ ๋งค์šฐ ๋†’์•„์ง„๋‹ค. Figure V-8. ๋งคํŠธ๋ฆญ์Šค ์กฐ์ง์˜ ๊ฐœ๋…๋„ ์ˆ˜์ง ์กฐ์ง์ด ๋งก๋Š” ๊ณ ๊ฐ๊ธฐ์—… ๋˜๋Š” ์‚ฌ์—…์˜์—ญ, ๊ทธ๊ฒƒ์„ B2B ์‚ฌ์—…์—์„œ๋Š” ์–ด์นด์šดํŠธ 2๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. B2B ๊ธฐ์—…์˜ ์„ฑ๊ณผ๋Š” ์ฒ ์ €ํžˆ ํŒŒ๋ ˆํ†  ๋ฒ•์น™์„ ๋”ฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์–ด์นด์šดํŠธ ๊ด€๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ˆ˜ํ–‰ํ•ด ๋‚ด๋Š๋ƒ์— ๋”ฐ๋ผ ์‚ฌ์—…์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๊ฐ€ ๋‹ฌ๋ ค์žˆ๋‹ค๊ณ  ํ•ด๋„ ๊ณผ์–ธ์ด ์•„๋‹ˆ๋‹ค ๋”ฐ๋ผ์„œ ๊ฑฐ๋ž˜ํ•˜๋Š” ๊ณ ๊ฐ ์ค‘ ํ•ต์‹ฌ ๊ณ ๊ฐ๊ตฐ์„ ํŒŒ์•…ํ•˜๊ณ  ์„ ๋ณ„๋œ ๊ณ ๊ฐ๋“ค์€ ์ง‘์ค‘ ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ๋™์ผํ•œ ์‹œ์žฅ๊ณผ ์‚ฐ์—…์—์„œ๋„ ๊ณ ๊ฐ๋ณ„๋กœ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋Š” ํฌ์ธํŠธ๊ฐ€ ๋‹ค๋ฅด๋‹ค. ์–ด๋–ค ๊ณ ๊ฐ์€ ์ตœ์ €๊ฐ€๋ฅผ ์›ํ•˜๋ฉฐ, ์–ด๋–ค ๊ณ ๊ฐ์€ ์ „๋‹ด ์„œ๋น„์Šค, ์–ด๋–ค ๊ณ ๊ฐ์€ ๊ฒ€์ฆ๋œ ๊ธฐ์ˆ  ๋“ฑ ๋‹ค์–‘ํ•œ ๊ณ ๊ฐ์˜ ๋‹ˆ์ฆˆ๊ฐ€ ์กด์žฌํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์–‘ํ•œ ๋‹ˆ์ฆˆ๋ฅผ ๋น„์šฉ ํšจ์œจ์ ์œผ๋กœ ๋Œ€์‘ํ•˜๊ณ  ์ตœ๊ณ ์˜ ๊ฐ€์น˜๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ๋งŽ์€ ๊ธฐ์—…๋“ค์ด ํ•ต์‹ฌ ์–ด์นด์šดํŠธ ๊ด€๋ฆฌ(Key Account Management: KAM)์— ์ฃผ๋ ฅํ•˜๊ณ  ์žˆ๋‹ค. 20.3 ์ง€์‹๊ฒฝ์˜ ์ปจ์„คํŒ…์€ ์ง€์‹ ๊ฒฝ์ œ(Knowledge Economy) ์‹œ๋Œ€์˜ ๋Œ€ํ‘œ์ ์ธ ์ง€์‹ ์‚ฐ์—…(Knowledge Business)์ด๋‹ค. 1990๋…„๋Œ€ ํ›„๋ฐ˜๋ถ€ํ„ฐ 2000๋…„๋Œ€ ์ดˆ๋ฐ˜๊นŒ์ง€ ์ด๋Ÿฐ ์ธ์‹๊ณผ ํŠธ๋ Œ๋“œ์— ๋”ฐ๋ผ ์ง€์‹ ๊ฒฝ์˜(Knowledge Management) ์—ดํ’์ด ๋ถˆ๊ธฐ๋„ ํ•˜์˜€๋‹ค. ์ง€์‹์„ ์ •์˜ํ•˜๊ณ  ์ง€์‹์„ ๊ด€๋ฆฌํ•œ๋‹ค๋Š” ์ทจ์ง€๋กœ ์ง€์‹๊ด€๋ฆฌ์‹œ์Šคํ…œ(KMS)์„ ๊ตฌ์ถ•ํ•˜๊ธฐ๋„ ํ•˜๊ณ  ๋‚ด๋ถ€ ํ”„๋กœ์„ธ์Šค๋ฅผ ํ˜์‹ ํ•˜๋Š” ๋“ฑ ๊ธฐ์—…๋งˆ๋‹ค ๋‹ค์–‘ํ•œ ์‹œ๋„๊ฐ€ ์žˆ์—ˆ๊ณ  ๊ทธ ๊ณผ์ •์—์„œ ๋‹ค์–‘ํ•œ ์„ฑ๊ณต๊ณผ ํ•œ๊ณ„๊ฐ€ ๋‚˜ํƒ€๋‚ฌ๋‹ค. 20๋…„์ด ์ง€๋‚œ ์ง€๊ธˆ์€ ๊ณผ๊ฑฐ์™€ ๊ฐ™์ด ์ง€์‹๊ฒฝ์˜์ด ๊ทธ๋ ‡๊ฒŒ ํ•˜์ด๋ผ์ดํŠธ ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์ง€๋งŒ ์ปจ์„คํŒ… ๊ธฐ์—…๋“ค์€ ๊ทธ๋“ค์˜ ํ•ต์‹ฌ ์ž์‚ฐ(Core Asset)์ด ์ง€์‹๊ฒฝ์˜์— ๊ธฐ๋ฐ˜ํ•œ ์ง€์‹ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(Knowledge-Base)๋ผ๊ณ  ๋งํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ์ฃผ์ €ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋งŒํผ ์ง€์‹์˜ ๊ณต์œ ์™€ ํ™•๋Œ€ ์ƒ์‚ฐ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ปจ์„คํŒ… ๊ธฐ์—…์—์„œ ์ง€์‹์„<NAME>๋Š” ์ˆ˜๋‹จ์€ ํฌ๊ฒŒ ์ง€์‹๊ฒฝ์˜ ์‹œ์Šคํ…œ(Knowledge Management System: KMS)์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ์ง€์‹ ์ปค๋ฎค๋‹ˆํ‹ฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๋‘ ๊ฐ€์ง€์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ, KMS๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์€ ํฌ๊ฒŒ 4๊ฐœ์˜ Level ๋˜๋Š” ์„ฑ์ˆ™๋„๋กœ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ ์‹œ๋„์ด์ž ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฒƒ์€ ํ”„๋กœ์ ํŠธ ์‚ฐ์ถœ๋ฌผ์„ ์ˆ˜์ง‘ํ•˜๊ณ  ๋ถ„๋ฅ˜ํ•˜๋ฉฐ ๊ฒ€์ƒ‰ ์‹œ์Šคํ…œ์„ ํ™œ์šฉํ•˜์—ฌ ๋งŽ์€ ์ปจ์„คํ„ดํŠธ๋“ค์ด ๊ทธ๊ฒƒ์„ ์žฌ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฉ๋ฒ•๋ก ํ™”ํ•˜๊ฑฐ๋‚˜ ํ•ต์‹ฌ ์ง€์‹์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ์‹์ด๋‹ค. ์ผ์ข…์˜ ์ง€์‹ ์ €์žฅ์†Œ(Knowledge Repository) ํ˜•ํƒœ์ธ๋ฐ ์ด๋Ÿฐ ์ˆ˜์ค€์„ KMS Level #1์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์กฐ๊ธˆ ๋” ์„ฑ์ˆ™ํ•˜์—ฌ KMS Level #2๊ฐ€ ๋˜๋ฉด ์ด๋Ÿฐ์ €๋Ÿฐ ์ €์žฅ์†Œ๊ฐ€ ๋งค์šฐ ๋งŽ์•„์ง€๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๊ฒƒ์„ ๋ชจ๋‘ ๋ฌถ์–ด์„œ ํฌํƒˆ(Portal)๋กœ ์”Œ์šฐ๊ณ  ์šฐ์ˆ˜ํ•œ ๊ฒ€์ƒ‰ ์—”์ง„์„ ํ†ตํ•ด ๋‹ค์–‘ํ•˜๊ณ  ํšจ๊ณผ์ ์ธ ๊ฒ€์ƒ‰์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค. KMS Level #3๊ฐ€ ๋˜๋ฉด KMS๊ฐ€ ๋ณ„๋„์˜ ์ •๋ณด์‹œ์Šคํ…œ์ด ์•„๋‹ˆ๋ผ ์ผ๋ฐ˜์ ์ธ ์—…๋ฌด ์‹œ์Šคํ…œ์œผ๋กœ ๋…น์•„์ ธ ๋ฒ„๋ฆฐ๋‹ค. ๋ณ„๋„์˜ KMS๊ฐ€ ์—†์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ง€์‹๊ฒฝ์˜์„ ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ์‚ฌ์‹ค์€ ๊ทธ๊ฒƒ์„ ์—…๋ฌด ํ๋ฆ„์— ๋…น์ธ๋‹ค. ์ฆ‰, ์—…๋ฌด ํ๋ฆ„(Workflow)์„ ๊ตฌํ˜„ํ•˜๊ณ  ๋‹จ๊ณ„์ ์œผ๋กœ ์—…๋ฌด๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ํ‘œ์ค€์ด๋‚˜ ์‚ฌ๋ก€, ํ…œํ”Œ๋ฆฟ์ฒ˜๋Ÿผ ํ™œ์šฉํ•˜์—ฌ ์ผ์˜ ์ƒ์‚ฐ์„ฑ์„ ๊ทน๋Œ€ํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ KMS Level #4๊ฐ€ ๋˜๋ฉด ๊ทธ๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ์—…๋ฌด ์‹œ์Šคํ…œ์„ ์„œ๋น„์Šค ๋‹จ์œ„๋กœ ์–ธ์ œ ์–ด๋””์„œ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํด๋ผ์šฐ๋“œ๋‚˜ ๋ชจ๋ฐ”์ผ ํ™˜๊ฒฝ์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค. ์ด๊ฒƒ์ด ์ •๋ณด์‹œ์Šคํ…œ ๊ด€์ ์—์„œ ๋ณธ KMS์˜ ๋ฐœ๋‹ฌ ๋ชจ์Šต์ด๋ผ๊ณ  ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๊ฐ€ ์†Œ์œ„<NAME>์ง€(Explicit Knowledge)์˜ ์ƒ์„ฑ๊ณผ ํ™œ์šฉ์— ๋Œ€ํ•œ ์ ‘๊ทผ ๋ฐฉ๋ฒ•๊ณผ ๋ฐœ๋‹ฌ ๊ณผ์ •์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ๋‘ ๋ฒˆ์งธ๋Š” ์•”๋ฌต์ง€(Implicit Knowledge) ์ฆ‰, ์‚ฌ๋žŒ ๋จธ๋ฆฟ์†์˜ ์ง€์‹์ด๋‚˜ ๊ฒฝํ—˜ ๋“ฑ์„<NAME>๋Š” ๊ฒƒ์ด๋‹ค. ์˜ํ™” ๋งคํŠธ๋ฆญ์Šค์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ๋’คํ†ต์ˆ˜์— ๊ฝ‚๊ณ  ์ง€์‹์„ ๋‚ด๋ ค๋ฐ›๋Š” ์ผ์€ ์ง€๊ธˆ ํ˜„์žฌ์—๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•Œ๋ ค์ง„ ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์ปค๋ฎค๋‹ˆํ‹ฐ[3]๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฉด๋Œ€ ๋ฉด(face-to-face) ์ ‘์ด‰์„ ํ™œ์„ฑํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์—…๋ฌด ์‹œ๊ฐ„์˜ ์ผ์ • ๋ถ€๋ถ„(์˜ˆ๋ฅผ ๋“ค๋ฉด 1/5 ์ •๋„)๋ฅผ ์ตœ๊ทผ ํ™”๋‘๊ฐ€ ๋˜๊ณ  ์žˆ๋Š” ์‚ฐ์—…์ด๋‚˜ ๊ธฐ์ˆ ์— ๋Œ€ํ•œ ์—ฐ๊ตฌ์™€ ์Šคํ„ฐ๋””, ์ƒˆ๋กœ์šด ๋ฐฉ์‹์˜ ์‹œ๋„๋‚˜ ๋ณธ์ธ์ด ํ•˜๊ณ  ์‹ถ์€ ์ผ์„ ์ž์œ ๋กญ๊ฒŒ ์ง„ํ–‰ํ•˜๊ณ  ๊ฐ™์€ ๋™๊ธฐ๋‚˜ ๊ด€์‹ฌ์„ ๊ฐ€์ง„ ์‚ฌ๋žŒ๋“ค๋ผ๋ฆฌ ๋ชจ์—ฌ ์ปค๋ฎค๋‹ˆํ‹ฐ ์•ˆ์—์„œ ํ™œ๋ฐœํ•˜๊ฒŒ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์œผ๋กœ ์ธ์  ๋„คํŠธ์›Œํฌ๋ฅผ ํ™œ์„ฑํ™”ํ•˜๊ณ  ๊ถ๊ทน์ ์œผ๋กœ ์ „๋ฌธ๊ฐ€ ๋„คํŠธ์›Œํฌ(Knowledge Experts)๋กœ ๋ฐœ์ „ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. ์•ž์„œ ์†Œ๊ฐœํ•œ CoE ์กฐ์ง์ด ์ด๋Ÿฐ ๋ถ€๋ถ„์„ ๋ฆฌ๋“œํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ปจ์„คํŒ… ์‚ฌ์—…๊ฐœ๋ฐœ๊ณผ ์ดํ–‰๋„ ๊ฒฐ๊ตญ ์ง€์‹๊ฒฝ์˜์ด ๋ณด์ด์ง€ ์•Š๊ฒŒ ๋™์ž‘ํ•˜๋ฉด์„œ ์ปจ์„คํŒ… ํŽŒ์˜ ์ž์‚ฐ์œผ๋กœ ์—ญ๋Ÿ‰์œผ๋กœ ์Œ“์—ฌ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ์„ ๋•Œ ๋ณด๋‹ค ๋†’์€ ์ƒ์‚ฐ์„ฑ์œผ๋กœ ํ›Œ๋ฅญํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ํ”„๋ฆฌ๋žœ์„œ ์ปจ์„คํ„ดํŠธ๋“ค์„ ๋งŒ๋‚˜์„œ ์ด์•ผ๊ธฐ๋ฅผ ๋“ฃ๋‹ค ๋ณด๋ฉด ๊ฐ€์žฅ ์•„์‰ฌ์›Œํ•˜๋Š” ๊ฒƒ์ด KMS์ด๋‹ค. ๊ทธ๋“ค์ด ๊ธฐ์—…์— ์†ํ•ด ์žˆ์—ˆ์„ ๋•Œ๋Š” ๊ทธ ๋ฐฉ๋Œ€ํ•œ ์ž๋ฃŒ์™€ ๋‹ค์–‘ํ•œ ๋„คํŠธ์›Œํฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์—ฌ๋ ค์›€์„ ํ•ด๊ฒฐํ•˜๋ฉด์„œ ์•„์‰ฌ์šด ์ค„ ๋ชฐ๋ž๋Š”๋ฐ ๋…๋ฆฝํ•˜์—ฌ ์ผ์„ ํ•˜๋‹ค ๋ณด๋‹ˆ ๊ทธ ๋ถ€๋ถ„์˜ ํ˜œํƒ์ด ์•„์‰ฝ๊ณ  ๊ทธ๋ฆฝ๋”๋ผ ํ•˜๋Š” ์ด์•ผ๊ธฐ๋Š” ํ”„๋ฆฌ๋žœ์„œ ์ปจ์„คํ„ดํŠธ ๋ˆ„๊ตฌ๋ฅผ ๋งŒ๋‚˜๋˜ ๊ณตํ†ต๋œ ์ด์•ผ๊ธฐ์ด๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: [ ๋ฌธ๊ณผ์ƒ๋„ ํ•  ์ˆ˜ ์žˆ๋Š” ] ๋”ฐ๋ผํ•˜๋ฉด ๋‹ค ๋˜๋Š” ํŒŒ์ด์ฌ ์—…๋ฌด์ž๋™ํ™” ### ๋ณธ๋ฌธ: "๋”ฐ๋ผ ํ•˜๋ฉด ๋‹ค ๋˜๋Š” ํŒŒ์ด์ฌ ์—…๋ฌด ์ž๋™ํ™”"๋Š” ์‹ค๋ฌด ๊ฒฝํ—˜์„ ํ† ๋Œ€๋กœ, ํŒŒ์ด์ฌ์˜ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜์—ฌ ์ผ์ƒ์˜ ์—…๋ฌด๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ž๋™ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋“ค์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ €์ž๋Š” ๋ณธ์ธ์˜ ์‹ค๋ฌด ๊ฒฝํ—˜์„ ๋ฐ”ํƒ•์œผ๋กœ, ํŒŒ์ด์ฌ์„ ํ™œ์šฉํ•˜์—ฌ ์—…๋ฌด์˜ ๋ณต์žก์„ฑ์„ ์ค„์ด๊ณ , ์ƒ์‚ฐ์„ฑ์„ ๋†’์ด๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•๋“ค์„ ์ƒ์„ธํ•˜๊ฒŒ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ฑ…์—๋Š” ์—‘์…€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ถ€ํ„ฐ ์›น ์Šคํฌ๋ ˆ์ดํ•‘๊นŒ์ง€, ๋‹ค์–‘ํ•œ ์‹ค์Šต ์˜ˆ์ œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด ์‹ค์ œ ์—…๋ฌด์— ์ฆ‰์‹œ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ, ์ด ์ฑ…์€ ํŒŒ์ด์ฌ์„ ์ฒ˜์Œ ์ ‘ํ•˜๋Š” ์ดˆ๋ณด์ž๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•˜๊ณ  ์žˆ์–ด, ์–ด๋ ค์šด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์šฉ์–ด๋‚˜ ๊ฐœ๋… ๋Œ€์‹ , ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ์„ค๋ช…๊ณผ ํ•จ๊ป˜ ์‹ค์ œ ์—…๋ฌด์— ๋ฐ”๋กœ ๋„์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์šฉ์ ์ธ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„๋Œ€์˜ ์ง์žฅ ์ƒํ™œ์—์„œ ํšจ์œจ์„ฑ์€ ์„ ํƒ์ด ์•„๋‹Œ ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ์ฑ…์„ ํ†ตํ•ด, ํŒŒ์ด์ฌ์˜ ๊ฐ•๋ ฅํ•œ ์—…๋ฌด ์ž๋™ํ™” ๋Šฅ๋ ฅ์„ ์ฒดํ—˜ํ•˜๊ณ , ์ผ์ƒ ์—…๋ฌด์˜ ํšจ์œจ์„ฑ์„ ํ•œ ๋‹จ๊ณ„ ๋Œ์–ด์˜ฌ๋ฆด ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 01. ํŒŒ์ด์ฌ ์‹œ์ž‘ํ•˜๊ธฐ ํŒŒ์ด์ฌ ์‹œ์ž‘ํ•˜๊ธฐ 1. ํŒŒ์ด์ฌ์„ ํ™œ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ  ์ฒ˜์Œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์œผ๋กœ ์—…๋ฌด ์ž๋™ํ™”๋ฅผ ํ•˜๊ณ ์ž ๋งˆ์Œ์„ ๋จน๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ๋Œ€ํ•ด์„œ ์ฐพ์•„๋ณด๋‹ค ๋ณด๋ฉด Java, C++, JavaScript, R ๋“ฑ ์ˆ˜๋งŽ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋“ค์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋Š” ๋ชจ๋‘ ํŠน์ • ๋ถ„์•ผ๋‚˜ ์ƒํ™ฉ์—์„œ ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ๊ณผ ํŽธ๋ฆฌํ•จ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ '์—…๋ฌด ์ž๋™ํ™”'๋ผ๋Š” ๋„“์€ ๋ฒ”์ฃผ์—์„œ ์ดˆ๋ณด์ž๊ฐ€ ์ ‘๊ทผํ•˜๊ธฐ์— ๊ฐ€์žฅ ์ €๋ณ€์ด ๋„“๊ณ , ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฌ์šฐ๋ฉฐ, ์‹ค์งˆ์ ์ธ ๊ฒฐ๊ณผ๋ฌผ์„ ๋น ๋ฅด๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด๋Š” ๋ฐ”๋กœ 'ํŒŒ์ด์ฌ'์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์€ ์ง๊ด€์ ์ด๊ณ  ์ฝ๊ธฐ ์‰ฌ์šด ๋ฌธ๋ฒ•์œผ๋กœ ์ดˆ๋ณด์ž๋„ ๋ฐฐ์šฐ๊ธฐ ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ ๋ฌธ๋ฒ•์€ ์ผ๋ฐ˜์ ์ธ ์˜์–ด ๋ฌธ์žฅ๊ณผ ์œ ์‚ฌํ•˜์—ฌ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šฐ๋ฉฐ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ๋›ฐ์–ด๋‚ฉ๋‹ˆ๋‹ค. ๊ทธ๋กœ ์ธํ•ด ๊นŠ์ด ์žˆ๋Š” ์ˆ˜์ค€๊นŒ์ง€ ๊ณต๋ถ€ํ•˜์ง€ ์•Š์•„๋„ ๋น ๋ฅด๊ฒŒ ์—…๋ฌด ์ž๋™ํ™”์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ ๋ถ„์„๋ถ€ํ„ฐ ์›น ๊ฐœ๋ฐœ, ์—‘์…€ ์ž‘์—…, ์ด๋ฉ”์ผ ๊ด€๋ฆฌ, ๋” ๋‚˜์•„๊ฐ€์„œ๋Š” ์ธ๊ณต ์ง€๋Šฅ, ๋จธ์‹  ๋Ÿฌ๋‹๊นŒ์ง€ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ํŒŒ์ด์ฌ์€ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋‚˜ ํˆด๊ณผ์˜ ์—ฐ๋™์ด ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ํ†ตํ•ด ๊ธฐ์กด ์‹œ์Šคํ…œ๊ณผ์˜ ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋ฉด์„œ ์—…๋ฌด ์ž๋™ํ™”๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ปค๋ฎค๋‹ˆํ‹ฐ๊ฐ€ ํ™œ๋ฐœํ•˜๋‹ค๋Š” ๊ฒƒ๋„ ํฐ ์žฅ์ ์ž…๋‹ˆ๋‹ค. ์ดˆ๋ณด์ž๊ฐ€ ์ฒ˜์Œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋‹ค ๋ณด๋ฉด ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์˜ค๋ฅ˜์— ๋‹นํ™ฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ์ „ ์„ธ๊ณ„์ ์œผ๋กœ ์ˆ˜๋งŽ์€ ๊ฐœ๋ฐœ์ž๋“ค์˜ ๋‹ค์–‘ํ•œ ์ž๋ฃŒ์™€ ํŠœํ† ๋ฆฌ์–ผ์„ ์ฐพ์•„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ๊ณ  ๋‹ค์–‘ํ•œ ์ปค๋ฎค๋‹ˆํ‹ฐ๋ฅผ ํ†ตํ•ด ๋ฌธ์ œ์— ๋Œ€ํ•œ ๋„์›€์„ ๋ฐ›์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋งŽ์€ ์žฅ์ ์„ ๊ฐ€์ง„ ํŒŒ์ด์ฌ์ด๊ธฐ์— ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์•Œ์ง€ ๋ชปํ•˜๋Š” ์ดˆ๋ณด์ž๋“ค๋„ ํŒŒ์ด์ฌ์„ ํ™œ์šฉํ•œ๋‹ค๋ฉด ์ถฉ๋ถ„ํžˆ ์ž์‹ ์˜ ์—…๋ฌด๋ฅผ ์ž๋™ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ฑ…์„ ํ†ตํ•ด ๋งค์ผ์˜ ๋ฐ˜๋ณต์ ์ธ ์—…๋ฌด๋“ค์„ ํŒŒ์ด์ฌ์—๊ฒŒ ๋งก๊ฒจ๋‘๊ณ  ์ปคํ”ผ๋ฅผ ํ•œ์ž”ํ•  ์ˆ˜ ์žˆ๋Š” ์—ฌ์œ ๊ฐ€ ์ƒ๊ธฐ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. 2. ์•„๋‚˜์ฝ˜๋‹ค ์„ค์น˜์™€ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ ์‚ฌ์šฉํ•˜๊ธฐ 1. ์•„๋‚˜์ฝ˜๋‹ค(Anaconda) ์„ค์น˜ ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ํŒŒ์ด์ฌ์„ ์„ค์น˜ํ•ด์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋กœ ํŒŒ์ด์ฌ๋งŒ ์„ค์น˜ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” '์•„๋‚˜์ฝ˜๋‹ค'๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์—…๋ฌด ์ž๋™ํ™”๋ฅผ ํ•˜๋‹ค ๋ณด๋ฉด ๋‹ค์–‘ํ•œ '๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ'๋‚˜ 'ํŒจํ‚ค์ง€'๋ฅผ ํ™œ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ๋‹ค์‹œ ํ•™์Šตํ•˜๊ฒ ์ง€๋งŒ, ์ผ๋‹จ์€ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•˜๋Š” ํˆด์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŒจํ‚ค์ง€๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ฐ๊ฐ์„ ์ง์ ‘ ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š”๋ฐ, '์•„๋‚˜์ฝ˜๋‹ค'๋ฅผ ์„ค์น˜ํ•˜๋ฉด ํŒŒ์ด์ฌ๋„ ์„ค์น˜๋˜๋ฉด์„œ ์šฐ๋ฆฌ๊ฐ€ ๋’ค์—์„œ ์‚ฌ์šฉํ•  Pandas๋‚˜ Numpy ๋“ฑ ์—ฌ๋Ÿฌ ํŒจํ‚ค์ง€๋„ ํ•จ๊ป˜ ์„ค์น˜๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ conda ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํŒจํ‚ค์ง€๋„ ์‰ฝ๊ฒŒ ์„ค์น˜ํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์œˆ๋„ ํ™˜๊ฒฝ์„ ๊ธฐ์ค€์œผ๋กœ ๋‘๊ณ  ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://www.anaconda.com/distribution/ ์œ„ ์‚ฌ์ดํŠธ ๋งํฌ๋กœ ์ด๋™ํ•˜์—ฌ ์‚ฌ์ดํŠธ ํ•˜๋‹จ์œผ๋กœ ์ด๋™ํ•˜๋ฉด (์ €์ž๊ฐ€ ์ด ์ฑ…์„ ์ž‘์„ฑํ•  ๋‹น์‹œ ๊ธฐ์ค€) ์ขŒ์ธก์— ํŒŒ์ด์ฌ 3.7 ๋ฒ„์ „๊ณผ ์šฐ์ธก์— ํŒŒ์ด์ฌ 2.7 ๋ฒ„์ „์˜ ์•„๋‚˜์ฝ˜๋‹ค ์„ค์น˜ ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํŒŒ์ด์ฌ 3.7 ๋ฒ„์ „ 64 ๋น„ํŠธ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜ ํŒŒ์ผ์„ ์‹คํ–‰ํ•œ ํ›„์— ๋‹ค๋ฅธ ์œˆ๋„ ํ”„๋กœ๊ทธ๋žจ์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Next >๋ฅผ ๋ˆ„๋ฅด๋ฉด์„œ ์„ค์น˜๋ฅผ ์™„๋ฃŒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ๋‹ค ์„ค์น˜ํ–ˆ๋‹ค๋ฉด ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ๋ฅผ ์˜คํ”ˆํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ๋ฅผ ์—ด์—ˆ๋‹ค๋ฉด ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ์— ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์•„๋‚˜์ฝ˜๋‹ค ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์ „๋ถ€ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. > conda update -n base conda > conda update --all 2. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ ์‚ฌ์šฉํ•˜๊ธฐ +๊ตฌ๊ธ€ Co-lab ์ด์šฉํ•˜๊ธฐ ๊ตฌ๊ธ€ Co-lab์€ ์˜จ๋ผ์ธ์—์„œ ํŒŒ์ด์ฌ ์ฝ”๋”ฉ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  ํ™˜๊ฒฝ์œผ๋กœ, Co-lab์„ ์ด์šฉํ•˜๋ฉด ์ปดํ“จํ„ฐ์— ํŒŒ์ด์ฌ์„ ์„ค์น˜ํ•  ํ•„์š” ์—†๊ณ , ์ธํ„ฐ๋„ท๋งŒ ๋˜๋ฉด ์–ด๋””์„œ๋“  ์ ‘์†์ด ๊ฐ€๋Šฅํ•˜๊ณ , ๋‚ด ์ปดํ“จํ„ฐ๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ์ฝ”๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌด๋ฃŒ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์—ฐ์† ์—ฐ๊ฒฐ ์‹œ๊ฐ„์€ ์ตœ๋Œ€ 90๋ถ„, ํ•˜๋ฃจ ์ด์šฉ ์‹œ๊ฐ„์€ 12์‹œ๊ฐ„์ด๋ผ๋Š” ์ œํ•œ์ด ์žˆ์–ด์„œ ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ์ž‘์—…์€ ์ ํ•ฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Co-lab ์ ‘์†ํ•˜๊ธฐ https://colab.research.google.com/notebooks/welcome.ipynb ์œ„์˜ ๋งํฌ๋ฅผ ํ†ตํ•ด ์ ‘์†ํ•œ ํ›„, ๊ตฌ๊ธ€ ์•„์ด๋””๋กœ ๋กœ๊ทธ์ธ -> ํŒŒ์ผ - ์ƒˆ ๋…ธํŠธ๋ฅผ ํด๋ฆญํ•˜์—ฌ Co-lab ๊ตฌ๋™ ์‹œ์ž‘ ์ด์™ธ์—๋„ ํฌํ„ธ์‚ฌ์ดํŠธ์—์„œ ๊ตฌ๊ธ€ ์ฝ”๋žฉ์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ ‘์†ํ•˜๊ฑฐ๋‚˜, ๊ตฌ๊ธ€ ๋“œ๋ผ์ด๋ธŒ์—์„œ ๋งŒ๋“ค๊ธฐ - ๋” ๋ณด๊ธฐ - Google Colaboratory๋ฅผ ํด๋ฆญํ•ด์„œ ์ ‘์†ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•œ Co-lab ํŒŒ์ผ์€ ์ดํ›„ ๊ตฌ๊ธ€ ๋“œ๋ผ์ด๋ธŒ(๋‚ด ๋“œ๋ผ์ด๋ธŒ - Colab Notebooks)์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Co-lab ํŒŒ์ผ๋ช… ๋ณ€๊ฒฝํ•˜๊ธฐ ์‹คํ–‰๋œ Co-lab ํŒŒ์ผ์˜ ์™ผ์ชฝ ์ƒ๋‹จ์— ํŒŒ์ผ๋ช…์„ ํด๋ฆญํ•˜๋ฉด ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. (๋ณดํ†ต ์ฒ˜์Œ ์ƒ์„ฑ๋˜๋Š” ํŒŒ์ผ์€ Untitled_.ipynb ์ด๋ฆ„์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.) ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š”, ๊ตฌ๊ธ€ ๋“œ๋ผ์ด๋ธŒ์— ๋“ค์–ด๊ฐ€์„œ ๋ฐ”๊พธ๊ณ ์ž ํ•˜๋Š” ํŒŒ์ผ์„ ํด๋ฆญ ํ›„, ์ƒ๋‹จ ๋ฉ”๋‰ด๋ฐ”์˜ ์˜ค๋ฅธ์ชฝ์— ์œ„์น˜ํ•œ ์„ธ๋กœ ์  ์„ธ ๊ฐœ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ๋ฅผ ํด๋ฆญํ•˜์—ฌ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ € ์„ค์น˜ํ•˜๊ธฐ ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ € ์„ค์น˜ํ•˜๊ธฐ ์›น์Šคํฌ๋ž˜ํ•‘์„ ์œ„ํ•ด ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ธŒ๋ผ์šฐ์ €๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋ณธ ์ €์„œ์—์„œ๋Š” ํฌ๋กฌ์œผ๋กœ ์ง„ํ–‰ํ•  ์˜ˆ์ •์ด๊ธฐ ๋•Œ๋ฌธ์— ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์„ค์น˜ํ•ด ์ค๋‹ˆ๋‹ค. 1) ์ง์ ‘ ์•„๋ž˜์˜ url๋กœ ์ ‘์† https://www.google.com/chrome/ 2) ํฌํ„ธ์‚ฌ์ดํŠธ์—์„œ 'ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ €' or 'ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ € ์„ค์น˜'๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๊ตฌ๊ธ€ ํฌ๋กฌ ์‚ฌ์ดํŠธ๋กœ ์ ‘์† ์—ด๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํฌ๋กฌ ํ™ˆํŽ˜์ด์ง€์—์„œ 'Chrome ๋‹ค์šด๋กœ๋“œ'๋ฒ„ํŠผ ํด๋ฆญํ•˜์—ฌ ์„ค์น˜ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œํ•œ ์„ค์น˜ ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜์—ฌ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 02. ํŒŒ์ด์ฌ ๊ธฐ์ดˆ ์ด ์ฑ•ํ„ฐ์—์„œ๋Š” ๋จผ์ € ํŒŒ์ด์ฌ๊ณผ ์นœํ•ด์ง€๊ธฐ ์œ„ํ•ด ๊ธฐ์ดˆ ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์™€ ์ž๋ฃŒํ˜•, ์—ฐ์‚ฐ์ž, ์กฐ๊ฑด๋ฌธ ๋“ฑ ํŒŒ์ด์ฌ์„ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ์žˆ์–ด ํ•„์ˆ˜์ ์ธ ๊ฐœ๋…๋“ค์„ ๋‹ค๋ฃฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์ฑ•ํ„ฐ๋Š” ์ด๋ก  ์„ค๋ช…๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‹ค์šฉ์ ์ธ ์˜ˆ์ œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์ˆœํžˆ ์ด๋ก ์„ ์ฝ๊ณ  ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ๋ณด๋‹ค ์‹ค์ œ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉฐ ๊ฐœ๋…์„ ์ฒดํ™”ํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ๋” ํšจ๊ณผ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์ œ ์ฝ”๋“œ๋“ค์„ ๊ทธ๋ƒฅ ๋ˆˆ์œผ๋กœ ๋ณด๊ธฐ๋ณด๋‹ค๋Š” ์ง์ ‘ ๋˜‘๊ฐ™์ด ๋”ฐ๋ผ ํ•˜๋ฉฐ ์‹คํ–‰ํ•ด ๋ณด๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์— ๋Œ€ํ•œ ํ•™์Šต์„ ๋งˆ์นœ ํ›„ ์ง„์ •์œผ๋กœ ํŒŒ์ด์ฌ์œผ๋กœ ์—…๋ฌด ์ž๋™ํ™”๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹จ์ˆœํžˆ ์ฑ…์„ ๋”ฐ๋ผ ํ•˜๊ณ  ๋๋‚˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ฑ…์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ์ž์˜ ์—…๋ฌด ํ™˜๊ฒฝ์— ๋งž์ถฐ ์ ์ ˆํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ณ€ํ˜•ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ฌธ๋ฒ•๊ณผ ๋„๊ตฌ๋“ค์— ๋Œ€ํ•œ ์ถฉ๋ถ„ํ•œ ์ดํ•ด๊ฐ€ ๋ฐ”ํƒ•์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ•ํ„ฐ๋ฅผ ํ†ตํ•ด ๊ธฐ์ดˆ์˜ ํ‹€์„ ํƒ„ํƒ„ํžˆ ์„ธ์›Œ์„œ ๋’ค์— ์ด์–ด์งˆ ๋ณต์žกํ•œ ์ฝ”๋“œ์˜ ์„ธ๊ณ„๋กœ ๋„˜์–ด๊ฐˆ ์ค€๋น„๋ฅผ ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. 02-00. ๊ธฐ๋ณธ ์ž‘์„ฑ๋ฒ• ์ฃผ์„ ์ฃผ์„์€ ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์—์„œ ์‚ฌ๋žŒ์—๊ฒŒ ์˜๋ฏธ ์žˆ๋Š” ์„ค๋ช…์ด๋‚˜ ๋ฉ”๋ชจ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฃผ์„์€ ์‹ค์ œ๋กœ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์œผ๋ฉฐ, ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ๋ฐœ์ž์—๊ฒŒ ๋„์›€์„ ์ฃผ๊ธฐ ์œ„ํ•ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ ์ฃผ์„์€ # ๊ธฐํ˜ธ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. # ๊ธฐํ˜ธ ๋’ค์˜ ๋ชจ๋“  ๋‚ด์šฉ์€ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— ์˜ํ•ด ๋ฌด์‹œ๋˜๊ณ  ์ฝ”๋“œ ์‹คํ–‰์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ์— ์ฃผ์„์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: # ์ด๊ฒƒ์€ ์ฃผ์„์ž…๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. # ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋‘ ์ •์ˆ˜๋ฅผ ๋”ํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. x = 5 # ๋ณ€์ˆ˜ x์— ์ •์ˆซ๊ฐ’ 5๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. y = 10 # ๋ณ€์ˆ˜ y์— ์ •์ˆซ๊ฐ’ 10์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. result = x + y # x์™€ y๋ฅผ ๋”ํ•œ ๊ฐ’์„ result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. print("๊ฒฐ๊ณผ:", result) # ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ฃผ์„์€ # ๊ธฐํ˜ธ๋กœ ์‹œ์ž‘ํ•˜๋ฉฐ, ์ฃผ์„ ๋‚ด์šฉ์€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‚˜ ๋ฉ”๋ชจ์ž…๋‹ˆ๋‹ค. ์ฃผ์„์€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ฃผ์„์ด ์งง์„ ๊ฒฝ์šฐ # ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋’ค์— ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๋ฉด ๋˜์ง€๋งŒ, ์—ฌ๋Ÿฌ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ์ฃผ์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๋ชจ๋“  ํ–‰์— # ๊ธฐํ˜ธ๋ฅผ ์ ์ง€ ์•Š๊ณ  ๋‹ค์Œ์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์—ฌ๋Ÿฌ ์ค„์„ ํ•œ ๋ฒˆ์— ์ฃผ์„ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์‹œ์ž‘ ์ง€์ ์— ''' (์ž‘์€๋”ฐ์˜ดํ‘œ 3๊ฐœ) ๋˜๋Š” """ (ํฐ๋”ฐ์˜ดํ‘œ 3๊ฐœ)๋ฅผ ์‚ฌ์šฉํ•œ ํ›„, ์ฃผ์„์ด ๋๋‚œ ๋ถ€๋ถ„์— ๋‹ค์‹œ ''' ๋˜๋Š” """๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์„์„ ์‹œ์ž‘ํ•  ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ ๊ธฐํ˜ธ์™€ ๋™์ผํ•œ ๊ธฐํ˜ธ๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„ ๋‚ด์—์„œ์˜ ์ฝ”๋“œ๋Š” ์ฃผ์„์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ๋กœ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฃผ์„์ด ๋๋‚œ ์ดํ›„์— ์›ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ''' ์ด ๋ถ€๋ถ„์€ ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„์ž…๋‹ˆ๋‹ค. print("์ฃผ์„ ๋‚ด์˜ ์ฝ”๋“œ") # ์ด ์ฝ”๋“œ๋Š” ์ฃผ์„ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ''' print("์ฃผ์„ ์ดํ›„์˜ ์ฝ”๋“œ") # ์ฃผ์„์ด ๋๋‚œ ์ดํ›„์˜ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„์€ '''๋กœ ์‹œ์ž‘ํ•˜์—ฌ '''๋กœ ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์„ ๋‚ด์˜ ์ฝ”๋“œ๋Š” ์ฃผ์„ ์ฒ˜๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‹คํ–‰๋˜์ง€ ์•Š๊ณ , ์ฃผ์„์ด ๋๋‚œ ์ดํ›„์˜ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์ฃผ์„์€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์„ค๋ช… ์™ธ์—๋„ ์ฝ”๋“œ๋ฅผ ์ž„์‹œ๋กœ ๋น„ํ™œ์„ฑํ™”ํ•˜๊ฑฐ๋‚˜, ์ฝ”๋“œ์˜ ์ผ๋ถ€๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฃผ์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋“ฑ์˜ ์šฉ๋„๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž๋“ค์€ ์ฃผ์„์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๋ฌธ์„œํ™”ํ•˜๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ๊ณผ์˜ ํ˜‘์—… ์‹œ์— ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ํ–‰์— ์—ฌ๋Ÿฌ ๋ฌธ์žฅ ์ž‘์„ฑํ•˜๊ธฐ ํŒŒ์ด์ฌ์—์„œ๋Š” ๋ณดํ†ต ํ•œ ํ–‰์— ํ•œ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ์“ฐ์ง€๋งŒ ํ•„์š”์— ๋”ฐ๋ผ ํ•œ ํ–‰์— ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ํ–‰์— ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•  ๋•Œ๋Š” ๋ฌธ์žฅ ์‚ฌ์ด์— ์„ธ๋ฏธ์ฝœ๋ก (;)์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ์„ธ๋ฏธ์ฝœ๋ก ์€ ๋ฌธ์žฅ์˜ ๋์„ ๋‚˜ํƒ€๋‚ด๋Š” ์—ญํ• ์„ ํ•˜๋ฉฐ, ํ•œ ํ–‰ ์•ˆ์—์„œ ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. x = 1; y = 2; z = x + y ์œ„์˜ ์ฝ”๋“œ๋Š” ํ•œ ํ–‰์— ์„ธ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์€ ๋ณ€์ˆ˜ x์— ์ •์ˆ˜ 1์„ ํ• ๋‹นํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์€ ๋ณ€์ˆ˜ y์— ์ •์ˆ˜ 2๋ฅผ ํ• ๋‹นํ•˜๋ฉฐ, ์„ธ ๋ฒˆ์งธ ๋ฌธ์žฅ์€ ๋ณ€์ˆ˜ z์— x + y์˜ ๊ฐ’์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ํ•œ ํ–‰์— ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ฐ€๋…์„ฑ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•œ ํ–‰์— ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•  ๋•Œ๋Š” ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๊ณ ๋ คํ•˜์—ฌ ์ ์ ˆํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํ•œ ๋ฌธ์žฅ์„ ์—ฌ๋Ÿฌ ํ–‰์— ์ž‘์„ฑํ•˜๊ธฐ ์œ„์˜ ๋‚ด์šฉ๊ณผ๋Š” ๋ฐ˜๋Œ€๋กœ ํ•œ ๋ฌธ์žฅ์„ ์—ฌ๋Ÿฌ ํ–‰์— ๋‚˜๋ˆ ์„œ๋„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ธด ๋ฌธ์žฅ์„ ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ๋ฌธ์žฅ์„ ์—ฌ๋Ÿฌ ํ–‰์— ์ž‘์„ฑํ•  ๋•Œ๋Š” ๋ฌธ์žฅ์„ ๋‚˜๋ˆ„๋Š” ์ง€์ ์—์„œ ์ค„ ๋ฐ”๊ฟˆ์„ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์€ ๋ฌธ์žฅ์ด ์ค„ ๋ฐ”๊ฟˆ ๋  ๋•Œ๋งˆ๋‹ค ๋ฌธ์žฅ์ด ๊ณ„์†๋œ๋‹ค๋Š” ๊ฒƒ์„ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ์ค„ ๋ฐ”๊ฟˆ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ๋ฌธ์žฅ์„ ๋‚˜๋ˆ„๋Š” ์ง€์ ์—์„œ ์ค„ ๋์— ๋ฐฑ์Šฌ๋ž˜์‹œ(\)๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฐฑ์Šฌ๋ž˜์‹œ๋Š” ์ค„ ๋ฐ”๊ฟˆ์„ ์˜๋ฏธํ•˜๋Š” ํŠน์ˆ˜ ๋ฌธ์ž๋กœ, ํŒŒ์ด์ฌ์—๊ฒŒ ๋ฌธ์žฅ์ด ๊ณ„์†๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•œ ๋ฌธ์žฅ์„ ์—ฌ๋Ÿฌ ํ–‰์— ๋‚˜๋ˆ ์„œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ๋ณ€์ˆ˜ = ๊ฐ’ 1 + \ ๊ฐ’ 2 + \ ๊ฐ’ 3 ์œ„์˜ ์˜ˆ์‹œ์—์„œ๋Š” ํ•œ ๋ฌธ์žฅ์„ ์„ธ ์ค„์— ๊ฑธ์ณ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ค„ ๋ฐ”๊ฟˆ ํ•  ๋•Œ๋งˆ๋‹ค ๋ฐฑ์Šฌ๋ž˜์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์žฅ์ด ๊ณ„์†๋œ๋‹ค๊ณ  ์•Œ๋ ค์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ณ€์ˆ˜์—๋Š” ๊ฐ’ 1 + ๊ฐ’ 2 + ๊ฐ’ 3์˜ ๊ฒฐ๊ณผ๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๊ด„ํ˜ธ((), [], {})๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์žฅ์„ ๋ฌถ์–ด์ค„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์€ ๊ด„ํ˜ธ ์•ˆ์— ์žˆ๋Š” ๋ฌธ์žฅ๋“ค์„ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์ธ์‹ํ•˜๋ฏ€๋กœ, ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์นœ ๋ฌธ์žฅ์„ ๊ด„ํ˜ธ๋กœ ๋ฌถ์œผ๋ฉด ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ๋ฌธ์žฅ์„ ์—ฌ๋Ÿฌ ํ–‰์— ๋‚˜๋ˆ ์„œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ๋ณ€์ˆ˜ = (๊ฐ’ 1 + ๊ฐ’ 2 + ๊ฐ’ 3) ์œ„์˜ ์˜ˆ์‹œ์—์„œ๋Š” ๊ด„ํ˜ธ๋กœ ๋ฌถ์—ฌ ์žˆ๋Š” ์„ธ ๊ฐœ์˜ ์ค„์ด ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ณ€์ˆ˜์—๋Š” ๊ฐ’ 1 + ๊ฐ’ 2 + ๊ฐ’ 3์˜ ๊ฒฐ๊ณผ๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. ๋“ค์—ฌ์“ฐ๊ธฐ ํŒŒ์ด์ฌ์—์„œ ๋“ค์—ฌ ์“ฐ๊ธฐ(indentation)๋Š” ์ฝ”๋“œ ๋ธ”๋ก์˜ ์‹œ์ž‘๊ณผ ๋์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ผ์ •ํ•œ ๊ณต๋ฐฑ(์ŠคํŽ˜์ด์Šค ๋˜๋Š” ํƒญ)์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„๋˜๋Š”๋ฐ, ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์ŠคํŽ˜์ด์Šค 4๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ ๋ธ”๋ก์€ ์ผ๋ จ์˜ ๋ฌธ์žฅ๋“ค์„ ํ•จ๊ป˜ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ตฌ์กฐ๋กœ, ์กฐ๊ฑด๋ฌธ(if ๋ฌธ, ๋ฐ˜๋ณต๋ฌธ ๋“ฑ)์ด๋‚˜ ํ•จ์ˆ˜ ์ •์˜ ๋“ฑ์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋“ค์—ฌ ์“ฐ๊ธฐ๋Š” ์ฝ”๋“œ ๋ธ”๋ก์˜ ์‹œ์ž‘์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋ฉฐ, ๋™์ผํ•œ ๋ธ”๋ก์— ์†ํ•˜๋Š” ์ฝ”๋“œ๋“ค์€ ๋™์ผํ•œ ์ˆ˜์ค€์œผ๋กœ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋“ค์—ฌ์“ฐ๊ธฐ ์ˆ˜์ค€์ด ๊ฐ™์€ ์ฝ”๋“œ๋“ค์€ ๋™์ผํ•œ ์ฝ”๋“œ ๋ธ”๋ก์— ์†ํ•˜๋ฉฐ, ๋“ค์—ฌ์“ฐ๊ธฐ ์ˆ˜์ค€์ด ๋‹ค๋ฅธ ์ฝ”๋“œ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์ฝ”๋“œ ๋ธ”๋ก์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, if ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ฝ”๋“œ ๋ธ”๋ก์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋“ค์—ฌ ์“ฐ๊ธฐ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. if ์กฐ๊ฑด: # ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ ๋ธ”๋ก ๋ฌธ์žฅ 1 ๋ฌธ์žฅ 2 ๋ฌธ์žฅ 3 # if ๋ฌธ ๋ธ”๋ก ์ข…๋ฃŒ ๋‹ค์Œ ์ฝ”๋“œ... ์œ„์˜ ์ฝ”๋“œ์—์„œ if ๋ฌธ์˜ ์กฐ๊ฑด์ด ์ฐธ์ด๋ฉด ๋“ค์—ฌ ์“ฐ๊ธฐ ๋œ ๋ธ”๋ก ์•ˆ์˜ ๋ฌธ์žฅ๋“ค์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋“ค์—ฌ์“ฐ๊ธฐ ์ˆ˜์ค€์ด ์ค„์–ด๋“œ๋Š” ๋ถ€๋ถ„์„ ๋ณด๋ฉด if ๋ฌธ์˜ ๋ธ”๋ก์ด ๋๋‚˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ์ž˜๋ชป ์‚ฌ์šฉํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํŒŒ์ด์ฌ์—์„œ๋Š” ์ผ๊ด€๋œ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ์œ ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 02-01. ๋ณ€์ˆ˜์™€ ์ž๋ฃŒํ˜• โ…  ๋ณ€์ˆ˜ ๋ณ€์ˆ˜๋Š” ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” "์ƒ์ž"๋‚˜ "๊ทธ๋ฆ‡"์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด "์ƒ์ž"๋Š” ํŠน์ •ํ•œ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ทธ ์•ˆ์—๋Š” ๊ฐ’์„ ๋‹ด์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์šฐ๋ฆฌ๊ฐ€ ์ˆซ์ž 10์„ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•ด "number_box"๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. number_box = 10 ์œ„์˜ ์ฝ”๋“œ๋Š” number_box๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ์•ˆ์— 10์ด๋ผ๋Š” ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์šฐ๋ฆฌ๋Š” number_box๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๊ฐ’์„ ๋‚˜์ค‘์— ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜๋Š” ์ด๋ฆ„์„ ํ†ตํ•ด ๊ฐ’์„ ์‹๋ณ„ํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฆ„์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ณ€์ˆ˜ ์ด๋ฆ„์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์†Œ๋ฌธ์ž์™€ ์–ธ๋” ์Šค์ฝ”์–ด _๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. age = 25 name = "์œ ์žฌ์„" is_student = True ์œ„์˜ ์ฝ”๋“œ๋Š” age, name, is_student๋ผ๋Š” ์„ธ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. age ๋ณ€์ˆ˜์—๋Š” ์ •์ˆซ๊ฐ’ 25๊ฐ€, name ๋ณ€์ˆ˜์—๋Š” ๋ฌธ์ž์—ด "์œ ์žฌ์„"์ด, is_student ๋ณ€์ˆ˜์—๋Š” ์ฐธ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถ€์šธ ๊ฐ’ True๊ฐ€ ํ• ๋‹น๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋‚˜์ค‘์— ํ•„์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„์˜ ๋ณ€์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print("์ด๋ฆ„:", name) print("๋‚˜์ด:", age) print("ํ•™์ƒ ์—ฌ๋ถ€:", is_student) ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. # ๊ฒฐ๊ด๊ฐ’ ์ด๋ฆ„:<NAME> ๋‚˜์ด: 25 ํ•™์ƒ ์—ฌ๋ถ€: True ํŒŒ์ด์ฌ์—๋Š” ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์ด ์žˆ๊ณ  ์ˆ˜ํ•™์—์„œ์ฒ˜๋Ÿผ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์— '02-04. ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค'์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ฒ ์ง€๋งŒ ๊ทธ์ „์— ๋จผ์ € ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ๊ฐ„๋žตํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ ์‚ฌ์šฉํ•œ print()๋ผ๋Š” ํ•จ์ˆ˜๋Š” ์ž๋ฃŒ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. print()์˜ ๊ด„ํ˜ธ ์•ˆ์— ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์€ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๋ฉด ๊ฐ’์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋ฉฐ ์‰ผํ‘œ(,)๋ฅผ ์‚ฌ์šฉํ•ด ์—ฌ๋Ÿฌ ๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ print("์ด๋ฆ„:", name)๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ "์ด๋ฆ„:"์ฒ˜๋Ÿผ ํฐ๋”ฐ์˜ดํ‘œ(")๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋”ฐ์˜ดํ‘œ ์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋’ค์— name์€ ๋”ฐ์˜ดํ‘œ ์—†์ด ์ž…๋ ฅ๋˜์—ˆ๋Š”๋ฐ ๊ทธ๋Ÿฌ๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์€ ๋ณ€์ˆ˜๋ผ๊ณ  ์ธ์‹๋˜์–ด ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ print("์ด๋ฆ„:", name)์œผ๋กœ ์ถœ๋ ฅ๋œ ๊ฒฐ๊ด๊ฐ’์ด ์ด๋ฆ„:<NAME>์ด ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋ณ€์ˆ˜๋กœ ๋Œ์•„์™€์„œ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜๋Š” ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ์ด์ „ ๊ฐ’์€ ๋Œ€์ฒด๋ฉ๋‹ˆ๋‹ค. age = 30 print("๋‚˜์ด:", age) # ๊ฒฐ๊ด๊ฐ’ ๋‚˜์ด: 30 ์œ„์˜ ์ฝ”๋“œ์—์„œ age ๋ณ€์ˆ˜์— 30์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐ’์„ ํ• ๋‹นํ•˜์˜€๊ณ , ๊ทธ ํ›„์— age ๋ณ€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ ๋ฐ์ดํ„ฐ์˜ ์œ ํ˜•(์ž๋ฃŒํ˜•)์— ๋”ฐ๋ผ ๋ณ€์ˆ˜์˜ ์œ ํ˜•์ด ์ •ํ•ด์ง‘๋‹ˆ๋‹ค. age ๋ณ€์ˆ˜์—๋Š” 30์ด๋ผ๋Š” ์ˆซ์ž๋ฅผ ํ• ๋‹นํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜ age๋Š” ์ˆซ์žํ˜• ๋ณ€์ˆ˜, name์ด๋ผ๋Š” ๋ณ€์ˆ˜์—๋Š”<NAME>์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋„ฃ์—ˆ์œผ๋ฏ€๋กœ ๋ณ€์ˆ˜ name์€ ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜• ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ณ€์ˆ˜์— ๊ธฐ์กด์— ํ• ๋‹นํ•œ ์œ ํ˜•์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜•์„ ๋„ฃ์œผ๋ฉด ๊ทธ ์ˆœ๊ฐ„ ๋ณ€์ˆ˜์˜ ์œ ํ˜•๋„ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜๋ช…์€ ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๋ฐ‘์ค„ ์ด์™ธ์˜ ๊ธฐํ˜ธ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒŒ์ด์ฌ์—์„œ ํŠน์ • ๋ชฉ์ ์„ ์œ„ํ•ด ์˜ˆ์•ฝ๋˜์–ด ์žˆ๋Š” ๋‹จ์–ด๋“ค(์˜ˆ์•ฝ์–ด)๋“ค์€ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. and del from None True as elif global nonlocal try assert else if not while break except import or with class False in pass yield continue finally is raise def for lambda return ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋„ ๋‚˜์ด๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ๋•Œ๋Š” ๋ณ€์ˆ˜๋ช…์„ 'age'๋กœ, ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ํ• ๋‹นํ•  ๋•Œ๋Š” ๋ณ€์ˆ˜๋ช…์„ 'name'์œผ๋กœ ๋งŒ๋“  ๊ฒƒ์ฒ˜๋Ÿผ, ๋ณ€์ˆ˜๋ช…์„ ์ง€์„ ๋•Œ๋Š” ์‹ค์ œ ํ•ด๋‹น ๋ณ€์ˆ˜์— ํ• ๋‹น๋˜์–ด ์žˆ๋Š” ์ž๋ฃŒ๊ฐ€ ๋ฌด์—‡์ธ์ง€๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์‰ฌ์šด ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๋ณ€์ˆ˜๋ฅผ ์‰ฝ๊ฒŒ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž๋ฃŒํ˜• ํŒŒ์ด์ฌ์—์„œ ์ž๋ฃŒํ˜•์€ ๊ฐ’์˜ ์ข…๋ฅ˜ ๋˜๋Š” ์œ ํ˜•์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ž๋ฃŒํ˜•์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ณ  ์กฐ์ž‘ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๊ทœ์น™๊ณผ ์—ฐ์‚ฐ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ž๋ฃŒํ˜•์„ ์ œ๊ณตํ•˜๋ฉฐ, ๊ฐ๊ฐ์˜ ์ž๋ฃŒํ˜•์€ ํŠน์ •ํ•œ ์œ ํ˜•์˜ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ํŒŒ์ด์ฌ ์ž๋ฃŒํ˜•์„ ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ์ˆซ์ž ํŒŒ์ด์ฌ์˜ ์ˆซ์ž ์ž๋ฃŒํ˜•์—๋Š” ์ •์ˆ˜ํ˜•(int)๊ณผ ์‹ค์ˆ˜ํ˜•(float)์ด ์žˆ์Šต๋‹ˆ๋‹ค. 1) ์ •์ˆ˜ (int) ์ •์ˆ˜ ์ž๋ฃŒํ˜•์€ ์ˆซ์ž์— ์†Œ์ˆ˜์ ์ด ์—†๋Š” ์–‘์˜ ์ •์ˆ˜(์–‘์ˆ˜)์™€ ์Œ์˜ ์ •์ˆ˜(์Œ์ˆ˜) ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 10, -5, 0๊ณผ ๊ฐ™์€ ์ •์ˆ˜๋Š” ์ •์ˆ˜ ์ž๋ฃŒํ˜•์œผ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ 10์„ ๋ณ€์ˆ˜ x์— ํ• ๋‹นํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. x = 10 # x ๋ณ€์ˆ˜์— ์ •์ˆซ๊ฐ’ 10์„ ํ• ๋‹น ๊ทธ๋Ÿฌ๋ฉด ์œ„์™€ ๊ฐ™์ด ๊ฐ’์ด ํ• ๋‹น๋œ x ๋ณ€์ˆ˜์˜ ์ž๋ฃŒํ˜•์„ ํ•œ ๋ฒˆ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. type(x) ์•„๋ž˜์™€ ๊ฐ™์ด type() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž๋ฃŒํ˜•์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๊ฒฐ๊ด๊ฐ’ int ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํ•ด๋‹น ๋ณ€์ˆ˜์˜ ์ž๋ฃŒํ˜•์ด int(์ •์ˆ˜ํ˜•)๋ผ๊ณ  ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. 2) ์‹ค์ˆ˜ (float) ์‹ค์ˆ˜ ์ž๋ฃŒํ˜•์€ ์†Œ์ˆ˜์ ์„ ํฌํ•จํ•˜๋Š” ์ˆซ์ž ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 3.14, -0.5, 2.0๊ณผ ๊ฐ™์€ ์‹ค์ˆ˜๋Š” ์‹ค์ˆ˜ ์ž๋ฃŒํ˜•์œผ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. y = 3.14 # y ๋ณ€์ˆ˜์— ์‹ค์ˆซ๊ฐ’ 3.14๋ฅผ ํ• ๋‹น type(y) # ๊ฒฐ๊ด๊ฐ’ float ์—ฐ์‚ฐ์ž ์ˆซ์ž ์ž๋ฃŒํ˜•๋ผ๋ฆฌ๋Š” ์—ฐ์‚ฐ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž ์ž๋ฃŒํ˜•์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ์—ฐ์‚ฐ์ž๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ์˜๋ฏธ ์˜ˆ์‹œ ๊ฒฐ๊ด๊ฐ’ a + b a์™€ b ๋”ํ•˜๊ธฐ 5 + 3 8 a - b a์—์„œ b ๋นผ๊ธฐ 5 - 3 2 a * b a์™€ b ๊ณฑํ•˜๊ธฐ 5 * 3 15 a / b a๋ฅผ b๋กœ ๋‚˜๋ˆ„๊ธฐ 5 / 3 1.6666666666666667 a // b a๋ฅผ b๋กœ ๋‚˜๋ˆด์„ ๋•Œ์˜ ๋ชซ 5 // 3 1 a % b a๋ฅผ b๋กœ ๋‚˜๋ˆด์„ ๋•Œ์˜ ๋‚˜๋จธ์ง€ 5 % 3 2 a ** b a์˜ b ๊ฑฐ๋“ญ์ œ๊ณฑ 5 ** 3 125 ์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์•Œ๊ณ  ์žˆ๋Š” ์—ฐ์‚ฐ์ž์™€ ์œ ์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆซ์ž ์ž๋ฃŒํ˜•์˜ ์—ฐ์‚ฐ์ž๋ฅผ ์ดํ•ดํ•˜๊ธฐ๋Š” ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ธฐ์–ตํ•  ์ ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ •์ˆ˜๋ผ๋ฆฌ์˜ ์—ฐ์‚ฐ์€ ๊ฒฐ๊ด๊ฐ’๋„ ์ •์ˆ˜๋กœ ๋‚˜์˜ค๊ณ  ์‹ค์ˆ˜๋ผ๋ฆฌ์˜ ์—ฐ์‚ฐ์€ ๊ฒฐ๊ด๊ฐ’๋„ ์‹ค์ˆ˜๋กœ ๋‚˜์˜ค์ง€๋งŒ, ๋‚˜๋ˆ—์…ˆ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ด๊ฐ’์€ ํ•ญ์ƒ ์‹ค์ˆ˜(float)๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜๋ผ๋ฆฌ ๋‚˜๋ˆ„๊ธฐ๋„ ์‹ค์ˆ˜ ์—ฐ์‚ฐ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์—, 4 / 2๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ 2๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ 2.0์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ •์ˆ˜์™€ ์‹ค์ˆ˜๋ฅผ ํ˜ผํ•ฉํ•˜์—ฌ ์—ฐ์‚ฐํ•  ๊ฒฝ์šฐ์—๋Š” ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ์‹ค์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์—ฐ์‚ฐ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ์—๋Š” ์—ฐ์‚ฐ์ž์˜ ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ์—ฐ์‚ฐ์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์šฐ์„ ์ˆœ์œ„ ์—ญ์‹œ ์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” ์ˆซ์ž ์—ฐ์‚ฐ ๊ทœ์น™๊ณผ ์œ ์‚ฌํ•˜์—ฌ 1) ๊ด„ํ˜ธ() ์•ˆ์„ ๋จผ์ € ๊ณ„์‚ฐํ•˜๊ณ , ๊ทธ๋‹ค์Œ 2)<NAME> ๊ณ„์‚ฐ, 3) ๊ณฑ์…ˆ ๋‚˜๋ˆ—์…ˆ ๊ณ„์‚ฐ, ๋งˆ์ง€๋ง‰์œผ๋กœ 4) ๋ง์…ˆ๊ณผ ๋บ„์…ˆ ๊ณ„์‚ฐ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ™์„ ๋•Œ๋Š” ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์—ฐ์‚ฐ์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. 2. ๋ฌธ์ž์—ด (str) ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•์€ ๋ง ๊ทธ๋Œ€๋กœ ๋ฌธ์ž๊ฐ€ ๋‚˜์—ด๋œ ๋ฌธ์ž์˜ ์ง‘ํ•ฉ์œผ๋กœ, ํŒŒ์ด์ฌ์—์„œ๋Š” ์ž‘์€๋”ฐ์˜ดํ‘œ(')๋‚˜ ํฐ๋”ฐ์˜ดํ‘œ(")๋กœ ๋‘˜๋Ÿฌ์‹ผ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "Hello, World!"๋‚˜ 'Python'๊ณผ ๊ฐ™์€ ๋ฌธ์ž์—ด์€ ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•์œผ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. name = "์œ ์žฌ์„" # name ๋ณ€์ˆ˜์— ๋ฌธ์ž์—ด "์œ ์žฌ์„"์„ ํ• ๋‹น type(name) # ๊ฒฐ๊ด๊ฐ’ str ๋ฌธ์ž์—ด "์œ ์žฌ์„"์„ name ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ ํ›„ type() ํ•จ์ˆ˜๋กœ ํ™•์ธํ•ด ๋ณด๋ฉด str(๋ฌธ์ž์—ด)์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฌธ์ž์—ด ์•ˆ์— ๋”ฐ์˜ดํ‘œ๋ฅผ ์จ์•ผ ํ•  ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๋”ฐ์˜ดํ‘œ์˜ ์ข…๋ฅ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. '"์•ˆ๋…•"์ด๋ผ๊ณ  ์†Œ๋…€๊ฐ€ ๋งํ–ˆ๋‹ค.' "What's it?" ์œ„์™€ ๊ฐ™์ด ๋ฌธ์ž์—ด์— ํฐ๋”ฐ์˜ดํ‘œ(")๋ฅผ ์จ์•ผ ํ•  ๊ฒฝ์šฐ์—๋Š” ์ž‘์€๋”ฐ์˜ดํ‘œ(')๋กœ ๋ฌธ์ž์—ด์„ ํ‘œ์‹œํ•˜๊ณ , ๋ฐ˜๋Œ€๋กœ ๋ฌธ์ž์—ด์— ์ž‘์€๋”ฐ์˜ดํ‘œ๋ฅผ ์“ธ ๊ฒฝ์šฐ์—๋Š” ํฐ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•์„ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํฐ๋”ฐ์˜ดํ‘œ์™€ ์ž‘์€๋”ฐ์˜ดํ‘œ ๋ชจ๋‘ ๋ฌธ์ž์—ด์— ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด, ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ์‚ผ์ค‘ ํฐ๋”ฐ์˜ดํ‘œ(""")๋‚˜ ์‚ผ์ค‘ ์ž‘์€๋”ฐ์˜ดํ‘œ(''')๋กœ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค. ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋Š” ํŒŒ์ด์ฌ์—์„œ ํŠน๋ณ„ํ•œ ์˜๋ฏธ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ฑฐ๋‚˜ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ํ•˜๊ธฐ๋กœ ์•ฝ์†๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž๋‚˜ ๊ธฐํ˜ธ๋ฅผ ๋‹ค๋ฅธ ์˜๋ฏธ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํŠน์ˆ˜ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํฐ๋”ฐ์˜ดํ‘œ๋‚˜ ์ž‘์€๋”ฐ์˜ดํ‘œ๋ฅผ ์›๋ž˜ ์•ฝ์†๋˜์–ด ์žˆ๋˜ ๊ธฐ๋Šฅ(๋ฌธ์ž์—ด ํ‘œ์‹œ ๊ธฐ๋Šฅ) ๋Œ€์‹  ๋ฌธ์ž์—ด๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์—์„œ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋Š” ํฐ๋”ฐ์˜ดํ‘œ๋‚˜ ์ž‘์€๋”ฐ์˜ดํ‘œ ์•ž์— (๋ฐฑ์Šฌ๋ž˜์‹œ)๋ฅผ ๋ถ™์ด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, \' ๋˜๋Š” \"๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ํฐ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌธ์ž์—ด ํ‘œ์‹œ๋ฅผ ํ•˜๋ฉด์„œ ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ์•ˆ์—๋„ ํฐ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. "\"์•ˆ๋…•\"์ด๋ผ๊ณ  ์†Œ๋…€๊ฐ€ ๋งํ–ˆ๋‹ค." ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์‚ผ์ค‘ ํฐ๋”ฐ์˜ดํ‘œ(""")๋‚˜ ์‚ผ์ค‘ ์ž‘์€๋”ฐ์˜ดํ‘œ(''')๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ํ‘œ์‹œ๋ฅผ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. string = ''' "์•ˆ๋…•"์ด๋ผ๊ณ  ์†Œ๋…€๊ฐ€ ๋งํ–ˆ๋‹ค. '๋ฌด์Šจ ๋œป์ด์ง€?' ์†Œ๋…„์€ ๋งˆ์Œ์†์œผ๋กœ ์ƒ๊ฐํ–ˆ๋‹ค. ''' print(string) # ๊ฒฐ๊ด๊ฐ’ "์•ˆ๋…•"์ด๋ผ๊ณ  ์†Œ๋…€๊ฐ€ ๋งํ–ˆ๋‹ค. '๋ฌด์Šจ ๋œป์ด์ง€?' ์†Œ๋…„์€ ๋งˆ์Œ์†์œผ๋กœ ์ƒ๊ฐํ–ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์‚ผ์ค‘ ๋”ฐ์˜ดํ‘œ(''' ๋˜๋Š” """)๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์ค„ ๋ฐ”๊ฟˆ ๋“ฑ ๊ทธ ์•ˆ์— ํฌํ•จ๋œ ๋ฌธ์ž์—ด์˜ ํ˜•ํƒœ๊นŒ์ง€ ๊ทธ๋Œ€๋กœ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์—ฌ๋Ÿฌ ํ–‰์˜ ๋ฌธ์ž์—ด์„ ๊ทธ๋Œ€๋กœ ์ž…๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ์—๋„ ์‚ผ์ค‘ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ print()๋กœ ์ถœ๋ ฅ๋œ ๊ฒฐ๊ด๊ฐ’์˜ ์ฒซ ์ค„๊ณผ ๋งˆ์ง€๋ง‰ ์ค„์— ๋นˆ ์ค„์ด ๋ฐ˜ํ™˜๋œ ์ด์œ ๋Š” ๋ฌธ์ž์—ด ์•ˆ์— ์ค„ ๋ฐ”๊ฟˆ์œผ๋กœ ๊ณต๋ฐฑ ๋ผ์ธ์ด ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์€ ๊ณต๋ฐฑ๊นŒ์ง€ ํฌํ•จํ•˜๋ฉฐ ๋ฌธ์ž์—ด์˜ ๊ฐ€์žฅ ์•ž์ด๋‚˜ ๋’ค์— ๊ณต๋ฐฑ์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋„ ๊ทธ ๊ณต๋ฐฑ๊นŒ์ง€ ๋ฌธ์ž์—ด๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์—์„œ ๋‹ค๋ฃฐ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ž๋กœ ๋ฌธ์ž์—ด๋ผ๋ฆฌ ์—ฐ์‚ฐ์„ ํ•  ๋•Œ์—๋„ ๊ณต๋ฐฑ๊นŒ์ง€ ํฌํ•จํ•˜์—ฌ ์—ฐ์‚ฐ๋ฉ๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•๋„ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ฃผ์š” ์—ฐ์‚ฐ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ์˜๋ฏธ ์˜ˆ์‹œ ๊ฒฐ๊ด๊ฐ’ a + b ๋ฌธ์ž์—ด a์™€ ๋ฌธ์ž์—ด b๋ฅผ ์—ฐ๊ฒฐํ•˜๊ธฐ 'Hello' + 'World' 'HelloWorld' a * b ๋ฌธ์ž์—ด a๋ฅผ ์ˆซ์ž b ๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ธฐ 'Hello' * 3 'HelloHelloHello' a in b ๋ฌธ์ž์—ด a๊ฐ€ ๋ฌธ์ž์—ด b์— ํฌํ•จ ์—ฌ๋ถ€ ํ™•์ธ (True/False ๊ฐ’ ๋ฐ˜ํ™˜) 'ello' in 'Hello' 'True' a not in b ๋ฌธ์ž์—ด a๊ฐ€ ๋ฌธ์ž์—ด b์— ๋ฏธํฌํ•จ ์—ฌ๋ถ€ ํ™•์ธ (True/False ๊ฐ’ ๋ฐ˜ํ™˜) 'aaa' in 'Hello' 'True' a ==, !=, <, <=, >, >= b ๋ฌธ์ž์—ด a์™€ ๋ฌธ์ž์—ด b๋ฅผ ์‚ฌ์ „ ์ˆœ์„œ๋กœ ๋น„๊ต (True/False ๊ฐ’ ๋ฐ˜ํ™˜) 'apple' < 'banana' 'True' ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃฌ ์—ฐ์‚ฐ์ž ์™ธ์—๋„ ํŒŒ์ด์ฌ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๊ด€๋ จ ์—…๋ฌด๋ฅผ ํŒŒ์ด์ฌ์œผ๋กœ ์ž๋™ํ™”ํ•  ๋•Œ ๋ฌธ์ž์—ด์„ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋Š” ๋’ค์˜ '02-09. ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ' ์ฑ•ํ„ฐ์—์„œ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3. ๋ถ€์šธ (bool): ๋ถ€์šธ ์ž๋ฃŒํ˜•์€ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์ฐธ(True) ๋˜๋Š” ๊ฑฐ์ง“(False)์„ ํ‘œํ˜„ํ•˜๋Š” ์ž๋ฃŒํ˜•์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ ์กฐ๊ฑด ๋ฌธ๊ณผ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. True์™€ False๋Š” ๋”ฐ์˜ดํ‘œ ์—†์ด ์‚ฌ์šฉํ•˜๋ฉฐ, ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๋ฏ€๋กœ ์†Œ๋ฌธ์ž true๋‚˜ false๋กœ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. is_valid = True type(is_valid) # ๊ฒฐ๊ด๊ฐ’ bool ์œ„์™€ ๊ฐ™์ด ๋ถ€์šธ ๊ฐ’ True๋ฅผ ํ• ๋‹นํ•œ ๋ณ€์ˆ˜ is_valid์˜ ์ž๋ฃŒํ˜•์„ type() ํ•จ์ˆ˜๋กœ ํ™•์ธํ•ด ๋ณด๋ฉด bool(๋ถ€์šธ ์ž๋ฃŒํ˜•)์ด๋ผ๊ณ  ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ๋ถ€์šธ ์ž๋ฃŒํ˜•๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ์—ฐ์‚ฐ์ž๋Š” ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์™€ ๋น„๊ต ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์—๋Š” ๋…ผ๋ฆฌ๊ณฑ(and), ๋…ผ๋ฆฌํ•ฉ(or), ๋…ผ๋ฆฌ ๋ถ€์ •(not)์ด ์žˆ์œผ๋ฉฐ, ๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ๋“ฑํ˜ธ์™€ ๋ถ€๋“ฑํ˜ธ ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค. ๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ๋น„๊ต๋ฅผ ํ•˜์—ฌ ๊ฒฐ๊ด๊ฐ’์ด ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ญ์ƒ ๊ฒฐ๊ด๊ฐ’์ด True ๋˜๋Š” False๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋ถ€์šธ ์ž๋ฃŒํ˜•์„ ๋‹ค๋ฃฐ ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ฃผ์š” ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์™€ ๋น„๊ต ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ์˜๋ฏธ ์˜ˆ์‹œ ๊ฒฐ๊ด๊ฐ’ a and b a์™€ b ๋‘ ๊ฐ’์ด ๋ชจ๋‘ ์ฐธ์ผ ๊ฒฝ์šฐ์—๋งŒ ์ฐธ์„ ๋ฐ˜ํ™˜ True and False False a or b a์™€ b ๋‘ ๊ฐ’ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฐธ์ด๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜ True or False True not a a๊ฐ€ ์ฐธ์ด๋ฉด ๊ฑฐ์ง“์„ ๋ฐ˜ํ™˜, ๊ฑฐ์ง“์ด๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜ not True False a ==, !=, <, <=, >, >= b a์™€ b๋ฅผ ๋น„๊ต (True/False ๊ฐ’ ๋ฐ˜ํ™˜) True == False False ์—ฐ์‚ฐ ๊ทœ์น™์— ๋”ฐ๋ผ ๊ด„ํ˜ธ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ๊ด„ํ˜ธ๋ถ€ํ„ฐ, ๊ทธ๋‹ค์Œ์—๋Š” ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ ์ˆœ์„œ๋กœ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” not > and > or ์ˆœ์„œ๋กœ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์™€ ๋น„๊ต ์—ฐ์‚ฐ์ž๊ฐ€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ๊ฒฝ์šฐ์—๋Š” ๋น„๊ต ์—ฐ์‚ฐ์ž๊ฐ€ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ณด๋‹ค ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์ˆ˜ํ–‰ํ•œ ํ›„์— ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. x = 5 y = 10 z = 20 result = (x < y or x > z) and (y == 10) print(result) # ๊ฒฐ๊ด๊ฐ’ True ์œ„์˜ ์˜ˆ์‹œ๋ฅผ ์—ฐ์‚ฐ ์ˆœ์„œ๋Œ€๋กœ ์‚ดํŽด๋ณด๋ฉด ๋จผ์ € ๊ด„ํ˜ธ ์•ˆ์— ์žˆ๋Š” ์—ฐ์‚ฐ์„ ๋จผ์ € ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ด„ํ˜ธ ์•ˆ์—๋Š” ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์™€ ๋น„๊ต ์—ฐ์‚ฐ์ž๊ฐ€ ํ•จ๊ป˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ๋น„๊ต ์—ฐ์‚ฐ์ด ๋จผ์ € ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ด„ํ˜ธ ์•ˆ์˜ 1์ฐจ ์—ฐ์‚ฐ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. โ‘  x < y : 5 < 10์„ ์—ฐ์‚ฐํ•˜์—ฌ ๊ฒฐ๊ด๊ฐ’ True๋ฅผ ๋ฐ˜ํ™˜ โ‘ก x > z : 5 > 20์„ ์—ฐ์‚ฐํ•˜์—ฌ ๊ฒฐ๊ด๊ฐ’ False๋ฅผ ๋ฐ˜ํ™˜ โ‘ข y == 10 : 10 == 10์„ ์—ฐ์‚ฐํ•˜์—ฌ ๊ฒฐ๊ด๊ฐ’ True๋ฅผ ๋ฐ˜ํ™˜ ๊ทธ๋‹ค์Œ, ์ฒซ ๋ฒˆ์งธ ๊ด„ํ˜ธ ์•ˆ์—์„œ โ‘ ์˜ ๊ฒฐ๊ด๊ฐ’๊ณผ โ‘ก์˜ ๊ฒฐ๊ด๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋…ผ๋ฆฌํ•ฉ(or) ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. โ‘ฃ (x < y or x > z)์—์„œ or ์—ฐ์‚ฐ: (True or False)๋ฅผ ์—ฐ์‚ฐํ•˜์—ฌ ๊ฒฐ๊ด๊ฐ’ True๋ฅผ ๋ฐ˜ํ™˜ ๋งˆ์ง€๋ง‰์œผ๋กœ โ‘ฃ์˜ ๊ฒฐ๊ด๊ฐ’๊ณผ โ‘ข์˜ ๊ฒฐ๊ด๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋…ผ๋ฆฌํ•ฉ(and) ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. โ‘ค (x < y or x > z) and (y == 10)์—์„œ and ์—ฐ์‚ฐ: (True) and (True)๋ฅผ ์—ฐ์‚ฐํ•˜์—ฌ ๊ฒฐ๊ด๊ฐ’ True๋ฅผ ๋ฐ˜ํ™˜ ๋”ฐ๋ผ์„œ ์œ„์˜ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•œ result ๋ณ€์ˆ˜์˜ ์ตœ์ข… ๊ฐ’์€ True๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 02-02. ์ž๋ฃŒํ˜• โ…ก 4. ๋ฆฌ์ŠคํŠธ (list) ์ง€๊ธˆ๊นŒ์ง€ ์•Œ์•„๋ณธ ์ˆซ์ž, ๋ฌธ์ž์—ด, ๋ถ€์šธ ์ž๋ฃŒํ˜•์ด ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๊ฐ€์ง€๋Š” ์ž๋ฃŒํ˜•์ด์—ˆ๋‹ค๋ฉด, ์•ž์œผ๋กœ ์•Œ์•„๋ณผ ์ž๋ฃŒํ˜•๋“ค์€ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋ฌถ์–ด์„œ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ์ผ์ข…์˜ ์ง‘ํ•ฉ ํ˜•ํƒœ์˜ ์ž๋ฃŒํ˜•์ž…๋‹ˆ๋‹ค. ๋จผ์ € ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋„ ์—ฌ๋Ÿฌ ๊ฐ’์„ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ €์žฅํ•  ๋•Œ๋Š” ๋Œ€๊ด„ํ˜ธ []๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด ์ž๋ฃŒ๊ฐ€ ๋ฆฌ์ŠคํŠธ๋ผ๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ•˜๋ฉฐ, ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ๋Š” ์‰ผํ‘œ(,)๋กœ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์˜ค๋ Œ์ง€"] print(fruits) # ๊ฒฐ๊ด๊ฐ’ ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์˜ค๋ Œ์ง€'] ์œ„์˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์€ print๋กœ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ ๊ฒฐ๊ด๊ฐ’์— [] ๋Œ€๊ด„ํ˜ธ๊ฐ€ ํฌํ•จ๋˜์–ด ์ด ์ž๋ฃŒ๊ฐ€ ๋ฆฌ์ŠคํŠธ๋ผ๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. type() ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด list๋ผ๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋Š” ์ˆซ์ž, ๋ฌธ์ž์—ด, ๋ถ€์šธ ๋“ฑ ๊ทธ ์–ด๋–ค ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋“  ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ ์ž์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋กœ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๊ณ  ๋นˆ ๋ฆฌ์ŠคํŠธ []๋„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. food = [ ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์˜ค๋ Œ์ง€'], ['์šฐ์œ ', '์น˜์ฆˆ', '์š”๊ตฌ๋ฅดํŠธ' ], [ ] ] ์ด๋ ‡๊ฒŒ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์š”์†Œ๋กœ ๋„ฃ์–ด ์—ฌ๋Ÿฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ค‘์ฒฉํ•œ ๊ฒฝ์šฐ, ์ด๋ฅผ ์ค‘์ฒฉํ•œ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ, ์‚ผ์ค‘ ๋ฆฌ์ŠคํŠธ๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๊ฐ€ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” len() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. len() ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ ์™ธ์—๋„ ๋’ค์—์„œ ๋ฐฐ์šธ ํŠœํ”Œ, ์„ธํŠธ, ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ ์š”์†Œ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ž๋ฃŒ์˜ ๊ธธ์ด(์š”์†Œ์˜ ๊ฐœ์ˆ˜)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. len() ๊ด„ํ˜ธ ์•ˆ์— ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ์ž๋ฃŒ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ์œ„์—์„œ ์ƒ์„ฑํ•œ food ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. len(food) # ๊ฒฐ๊ด๊ฐ’ ๋ฆฌ์ŠคํŠธ food์˜ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋กœ 3์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. food ๋ฆฌ์ŠคํŠธ์— ํฌํ•จ๋œ ๊ฐ๊ฐ์˜ ๋ฆฌ์ŠคํŠธ๋“ค์„ ํ•˜๋‚˜์˜ ์š”์†Œ๋กœ ์ธ์‹ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•๋„ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์˜ ๋ฐฉ์‹์€ ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ฑฐ๋‚˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ์˜๋ฏธ ์˜ˆ์‹œ ๊ฒฐ๊ด๊ฐ’ a + b ๋ฆฌ์ŠคํŠธ a์™€ ๋ฆฌ์ŠคํŠธ b๋ฅผ ์—ฐ๊ฒฐํ•˜๊ธฐ [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] + [ '์˜ค๋ Œ์ง€' ] ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์˜ค๋ Œ์ง€'] a * b ๋ฆฌ์ŠคํŠธ a๋ฅผ ์ˆซ์ž b ๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ธฐ [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] * 2 ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜'] ์œ„์˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ์—์„œ๋„ ์•Œ ์ˆ˜ ์žˆ๋“ฏ ๋ฆฌ์ŠคํŠธ๋Š” ์š”์†Œ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ๊ฐ’์„ ๋ฆฌ์ŠคํŠธ์— ์—ฌ๋Ÿฌ ๋ฒˆ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์‹ฑ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ๋Š” ์ˆœ์„œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ ๊ทธ ์ˆœ์„œ๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค(์ˆœ์„œ ๋ฒˆํ˜ธ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋Š” 0์ด๋ฉฐ, ์š”์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ด n ๊ฐœ์ธ ๋ฆฌ์ŠคํŠธ์—์„œ ๋งˆ์ง€๋ง‰ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋Š” n-1์ด ๋ฉ๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค(i)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ list[i]๋กœ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์˜ค๋ Œ์ง€"] fruits[0] # ๊ฒฐ๊ด๊ฐ’ '์‚ฌ๊ณผ' ์œ„์˜ ์˜ˆ์‹œ์—์„œ fruits[0]์œผ๋กœ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์ธ '์‚ฌ๊ณผ'๋ฅผ ๋ฐ˜ํ™˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด fruits[2]๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์Œ์ˆ˜(-)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๊ฐ€ ์Œ์ˆ˜์ธ ์Œ์ˆ˜ ์ธ๋ฑ์‹ฑ์€ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ˆœ์„œ๊ฐ€ ๋ฐ˜๋Œ€๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ -0์€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์Œ์ˆ˜ ์ธ๋ฑ์Šค๋Š” -1, ๊ทธ ๋ฐ”๋กœ ์•ž ์š”์†Œ๋Š” -2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. fruits[-2]๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. fruits[-2] # ๊ฒฐ๊ด๊ฐ’ '๋ฐ”๋‚˜๋‚˜' ๋’ค์—์„œ ๋‘ ๋ฒˆ์งธ ์š”์†Œ์ธ '๋ฐ”๋‚˜๋‚˜'๊ฐ€ ๋ฐ˜ํ™˜๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ(์ด์ค‘ ๋ฆฌ์ŠคํŠธ)์˜ ์š”์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋ ค๋ฉด ์ธ๋ฑ์Šค๋ฅผ ์ด์ค‘์œผ๋กœ ์ค‘์ฒฉํ•˜์—ฌ ์›ํ•˜๋Š” ์š”์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์‹œ์—์„œ '์š”๊ตฌ๋ฅดํŠธ'๋ฅผ ์ถ”์ถœํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. food = [ ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์˜ค๋ Œ์ง€'], ['์šฐ์œ ', '์น˜์ฆˆ', '์š”๊ตฌ๋ฅดํŠธ' ], [ ] ] food[1][-1] # ๊ฒฐ๊ด๊ฐ’ '์š”๊ตฌ๋ฅดํŠธ' ๋ฐ”๊นฅ์ชฝ ๋ฆฌ์ŠคํŠธ(food)์˜ ๋‘ ๋ฒˆ์งธ ์š”์†Œ[1]์— ๋จผ์ € ์ ‘๊ทผํ•œ ๋‹ค์Œ, ์•ˆ์ชฝ ๋ฆฌ์ŠคํŠธ(['์šฐ์œ ', '์น˜์ฆˆ', '์š”๊ตฌ๋ฅดํŠธ' ])์—์„œ [-1]๋กœ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์š”์†Œ '์š”๊ตฌ๋ฅดํŠธ'์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ค‘์ฒฉ๋œ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ์—๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์š”์†Œ ๋ณ€๊ฒฝํ•˜๊ธฐ ๋งŒ์•ฝ ๋ฆฌ์ŠคํŠธ์— ํŠน์ • ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, ์ด๋ ‡๊ฒŒ ์ธ๋ฑ์Šค๋กœ ์š”์†Œ์— ์ ‘๊ทผํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ '๊ฐ์ž'๋กœ ๋ณ€๊ฒฝํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. food[1] = '๊ฐ์ž' print(food) # ๊ฒฐ๊ด๊ฐ’ [['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์˜ค๋ Œ์ง€'], '๊ฐ์ž', []] food[1]์œผ๋กœ ๋ฆฌ์ŠคํŠธ์˜ ๋‘ ๋ฒˆ์งธ ์š”์†Œ์— ์ ‘๊ทผํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ '๊ฐ์ž'๋ผ๋Š” ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์š”์†Œ ์‚ญ์ œํ•˜๊ธฐ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•  ๋•Œ๋„ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜์—ฌ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. del food[1] print(food) # ๊ฒฐ๊ด๊ฐ’ [['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์˜ค๋ Œ์ง€'], []] ์œ„์—์„œ ๋ณ€๊ฒฝํ–ˆ๋˜ ๋‘ ๋ฒˆ์งธ ์š”์†Œ '๊ฐ์ž'๊ฐ€ ์‚ญ์ œ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•  ๊ฒฝ์šฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋‚จ์•„์žˆ๋Š” ์š”์†Œ๋“ค์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ์ธ๋ฑ์Šค๋ฅผ ๋‹ค์‹œ ์„ค์ •ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์˜ ์˜ˆ์‹œ์—์„œ ๋นˆ ๋ฆฌ์ŠคํŠธ๋Š” ์›๋ž˜ ์„ธ ๋ฒˆ์งธ ์š”์†Œ์˜€์ง€๋งŒ "๊ฐ์ž"๊ฐ€ ์‚ญ์ œ๋œ ํ›„์—๋Š” ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋กœ ์žฌ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์Šฌ๋ผ์ด์‹ฑ ์•ž์—์„œ ํ•™์Šตํ•œ ์ธ๋ฑ์‹ฑ์œผ๋กœ๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ํ•˜๋‚˜์˜ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์š”์†Œ ํ•˜๋‚˜๊ฐ€ ์•„๋‹Œ ํŠน์ • ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋ ค๋ฉด ์ธ๋ฑ์‹ฑ์ด ์•„๋‹Œ ์Šฌ๋ผ์ด์‹ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์Šฌ๋ผ์ด์‹ฑ์€ ๋ฆฌ์ŠคํŠธ์˜ ์ผ๋ถ€๋ถ„์„ ์ž˜๋ผ๋‚ด์–ด ๊ฐ€์ ธ์˜ค๋Š” ๊ธฐ๋Šฅ์œผ๋กœ, ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์ข…๋ฃŒ ์ธ๋ฑ์Šค๊นŒ์ง€์˜ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๊ณ  ์™€์„œ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” list[start:end]์˜<NAME>์œผ๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜์—ฌ ์Šฌ๋ผ์ด์‹ฑํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์œ ์˜ํ•  ์ ์€ start์—๋Š” ๊ฐ€์ ธ์˜ฌ ๋ฒ”์œ„์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ฅผ ์ž…๋ ฅํ•˜์ง€๋งŒ, end์—๋Š” ๊ฐ€์ ธ์˜ฌ ๋ฒ”์œ„์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹Œ ๊ทธ๋‹ค์Œ ์ธ๋ฑ์Šค๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. end๋Š” ๊ฐ€์ ธ์˜ฌ ๋ฒ”์œ„์— ํฌํ•จ๋˜์ง€ ์•Š๊ณ  ๊ทธ ๋ฐ”๋กœ ์•ž ์ธ๋ฑ์Šค๊นŒ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์›ํ•˜๋Š” ๋ฒ”์œ„์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์˜ ๋ฐ”๋กœ ๋‹ค์Œ ์ธ๋ฑ์Šค๋ฅผ end์— ์ž…๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. ๋งŒ์•ฝ start๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ ๋ฒ”์œ„๊ฐ€ ์‹œ์ž‘๋˜๊ณ , ๋ฐ˜๋Œ€๋กœ end๋ฅผ ์„ค์ •ํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๊นŒ์ง€๋กœ ๋๋‚˜๋Š” ๋ฒ”์œ„๊ฐ€ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ start์™€ end ๋‘˜ ๋‹ค ์ƒ๋žตํ•  ๊ฒฝ์šฐ์—๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฒ”์œ„๊ฐ€ ์„ค์ •๋˜์–ด ๊ฐ€์ง€๊ณ  ์˜ค๊ณ ์ž ํ•˜๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๊ฐ–๋Š” ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ˆซ์ž๋ฅผ ์š”์†Œ๋กœ ๊ฐ–๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ 2๋ถ€ํ„ฐ 6๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์Šฌ๋ผ์ด์‹ฑ ํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. number = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] slice1 = number[2:7] print(slice1) # ๊ฒฐ๊ด๊ฐ’ [2, 3, 4, 5, 6] ์Šฌ๋ผ์ด์‹ฑ์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์Œ์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ number[2:7] ๋Œ€์‹  number[2:-3]์œผ๋กœ ์Šฌ๋ผ์ด์‹ฑํ•ด๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์Šฌ๋ผ์ด์‹ฑ์„ ํ•  ๋•Œ ๊ฐ„๊ฒฉ์„ ์ง€์ •ํ•ด์„œ ์š”์†Œ๋ฅผ ๊ฑด๋„ˆ๋›ฐ๋ฉฐ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. list[start:end:step]์˜ ํ˜•ํƒœ๋กœ ๊ฐ€์žฅ ๋’ค์— ๊ฑด๋„ˆ๋›ธ ๊ฐ„๊ฒฉ์„ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์˜ˆ์‹œ์—์„œ ๊ฐ„๊ฒฉ์„ 2๋กœ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. number = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] slice2 = number[2:7:2] print(slice2) # ๊ฒฐ๊ด๊ฐ’ [2, 4, 6] number[2:7:2]๋กœ ์„ธ ๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ ์ผ๊ณฑ ๋ฒˆ์งธ ์š”์†Œ๊นŒ์ง€ ๊ฐ€์ง€๊ณ  ์˜ค๋˜ 2๊ฐœ์”ฉ ๊ฑด๋„ˆ๋›ฐ๋ฉฐ ๊ฐ€์ง€๊ณ  ์˜ต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ๋ฉ”์„œ๋“œ ํŒŒ์ด์ฌ์—์„œ๋Š” ๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ ๋“ฑ ๊ฐ ์ž๋ฃŒํ˜•๋งˆ๋‹ค ์ž์‹ ๋งŒ์˜ ๊ณ ์œ ํ•œ ๊ธฐ๋Šฅ, ์ž‘์—…๋“ค์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ๋“ค์„ ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ๋’ค์— ์˜จ์ (.)์„ ๋ถ™์ด๊ณ  ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์„ ์ ์–ด์„œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฆฌ์ŠคํŠธ์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด list.method() ํ˜•ํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) append() ๋ฆฌ์ŠคํŠธ์˜ ๋์— ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. append(item)์˜ ํ˜•ํƒœ๋กœ ๊ด„ํ˜ธ ์•ˆ์— ๋ฆฌ์ŠคํŠธ์— ๋„ฃ๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] fruits.append('์˜ค๋ Œ์ง€') print(fruits) # ๊ฒฐ๊ด๊ฐ’ ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์˜ค๋ Œ์ง€'] ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋„ ์ธ๋ฑ์Šค[i]๋กœ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์ง€๋งŒ, ๊ธฐ๋ณธ์ ์œผ๋กœ ์ธ๋ฑ์‹ฑ์€ ์กด์žฌํ•˜๋Š” ์ธ๋ฑ์Šค์— ๋Œ€ํ•ด์„œ๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์—†๋Š” ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•˜๋ผ๊ณ  ํ•  ๊ฒฝ์šฐ์—๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฆฌ์ŠคํŠธ์˜ ์ด ๊ธธ์ด๊ฐ€ 3์ธ ์ธ๋ฑ์Šค์—์„œ ๋ฆฌ์ŠคํŠธ[3]์„ ์‹คํ–‰ํ•˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  append()๋‚˜ ๋’ค์—์„œ ๋ฐฐ์šธ insert(), extend()์™€ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 2) insert() insert() ๋ฉ”์„œ๋“œ๋Š” ์ง€์ •ํ•œ ์ธ๋ฑ์Šค ์œ„์น˜์— ์š”์†Œ์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋์ด ์•„๋‹Œ ํŠน์ •ํ•œ ์ง€์ ์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. insert(index, item)์˜<NAME>์œผ๋กœ ๊ด„ํ˜ธ ์•ˆ์— ์ง€์ •ํ•˜๊ณ ์ž ํ•˜๋Š” ์œ„์น˜(์ธ๋ฑ์Šค)์™€ ๋„ฃ๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] fruits.insert(1, 'ํŒŒ์ธ์• ํ”Œ') print(fruits) # ๊ฒฐ๊ด๊ฐ’ ['์‚ฌ๊ณผ', 'ํŒŒ์ธ์• ํ”Œ', '๋ฐ”๋‚˜๋‚˜'] 3) extend() extend() ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋์— ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. extend() ๋ฉ”์„œ๋“œ๋กœ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•์€ ๋ฆฌ์ŠคํŠธ๋‚˜ ๋’ค์—์„œ ๋ฐฐ์šธ ํŠœํ”Œ๊ณผ ๊ฐ™์ด ์š”์†Œ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์„œ ํ•˜๋‚˜์”ฉ ์ ‘๊ทผํ•˜์—ฌ ๋ฐ˜๋ณต ์ž‘์—…์ด ๊ฐ€๋Šฅํ•œ(iterable) ํ˜•ํƒœ์˜ ์ž๋ฃŒํ˜•์ž…๋‹ˆ๋‹ค. extend() ๋ฉ”์„œ๋“œ๋Š” ์ด๋Ÿฌํ•œ iterable ํ•œ ํ˜•ํƒœ์˜ ์ž๋ฃŒํ˜•์„ ์ „๋‹ฌ๋ฐ›์•„ ์ž๋ฃŒ์˜ ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๋Š”<NAME>์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] fruits.extend(['๋”ธ๊ธฐ', '๋ง๊ณ ']) print(fruits) # ๊ฒฐ๊ด๊ฐ’ ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '๋”ธ๊ธฐ', '๋ง๊ณ '] ์‹คํ–‰๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด extend()๋กœ ์ถ”๊ฐ€ํ•ด ์ค€ ๋ฆฌ์ŠคํŠธ ['๋”ธ๊ธฐ', '๋ง๊ณ ']์˜ ์š”์†Œ๋“ค์ด ๊ฐ๊ฐ fruits์˜ ์›์†Œ๋“ค๋กœ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™์ผํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์œ„์—์„œ ํ•™์Šตํ–ˆ๋˜ append() ๋ฉ”์„œ๋“œ๋กœ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] fruits.append(['๋”ธ๊ธฐ', '๋ง๊ณ ']) print(fruits) # ๊ฒฐ๊ด๊ฐ’ ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', ['๋”ธ๊ธฐ', '๋ง๊ณ ']] ๊ฒฐ๊ด๊ฐ’์ด ๋‹ค๋ฅธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. append()๋Š” ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋กœ ๋ณด๊ณ  ์ „์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด extend()๋Š” ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ์— ์—ฌ๋Ÿฌ ์š”์†Œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ๋“ค์„ ๊ฐ๊ฐ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์š”์†Œ๊ฐ€ ํ•˜๋‚˜๋ฟ์ธ ์ž๋ฃŒ๋ฅผ extend() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•  ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์š”์†Œ ํ•˜๋‚˜๋งŒ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๊ฐ€ ์ƒ๊ฐํ•˜๊ธฐ์—๋Š” ์š”์†Œ๊ฐ€ ํ•˜๋‚˜๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š” ์ž๋ฃŒ๋„ extend()๋Š” ๊ทธ ์ž‘๋™ ๋ฐฉ์‹ ๋•Œ๋ฌธ์— ์š”์†Œ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ์ธ์‹ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] fruits.extend('3') fruits.extend('100') fruits.extend('ํŒŒ์ธ์• ํ”Œ') print(fruits) # ๊ฒฐ๊ด๊ฐ’ ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '3', '1', '0', '0', 'ํŒŒ', '์ธ', '์• ', 'ํ”Œ'] extend() ๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ์ž๋ฃŒ๋งŒ ์ „๋‹ฌ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์ˆซ์ž ์ž๋ฃŒํ˜•์€ ์ž…๋ ฅํ•  ์ˆ˜ ์—†์–ด์„œ ์˜ˆ์‹œ์—์„œ๋Š” ์ž‘์€๋”ฐ์˜ดํ‘œ(')๋กœ ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„ํ•˜์—ฌ ์ „๋‹ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ ๋ณด๋ฉด extend() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•œ ๊ฐ’ '3', '100', 'ํŒŒ์ธ์• ํ”Œ'์€ ๋ชจ๋‘ ๋ฌธ์ž์—ด ํ•˜๋‚˜๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ extend() ๋ฉ”์„œ๋“œ์˜ ์ž…์žฅ์—์„œ๋Š” ์ „๋‹ฌ๋ฐ›์€ ์ž๋ฃŒ๋“ค์ด ๋ชจ๋‘ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ์ž๋ฃŒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ญ๋ชฉ์„ ๊ฐ๊ฐ ๋‚˜๋ˆ„์–ด์„œ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋กœ ์ž…๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. '3'์€ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š” ํ•ญ๋ชฉ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— '3' ๊ทธ๋Œ€๋กœ ํ•˜๋‚˜์˜ ์š”์†Œ๋กœ ์ถ”๊ฐ€๋˜์—ˆ์ง€๋งŒ, '100'์€ '1'๊ณผ '0'๊ณผ '0'์œผ๋กœ, 'ํŒŒ์ธ์• ํ”Œ'์€ 'ํŒŒ','์ธ','์• ','ํ”Œ'๋กœ ํ•ญ๋ชฉ๋“ค์„ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž ํ•˜๋‚˜์”ฉ ๋‚˜๋‰˜์–ด์„œ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” extend()๊ฐ€ ์•„๋‹ˆ๋ผ append()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ์›ํ•˜๋Š” ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ๋“ค์„ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” ์ตœ์ข…์ ์œผ๋กœ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ๊ณ ์ž ํ•˜๋Š” ํ˜•ํƒœ์— ๋”ฐ๋ผ append()๋‚˜ extend()๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์‚ฌ์šฉํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 4) remove() remove() ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ์ฒซ ๋ฒˆ์งธ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ง€์ •ํ•œ ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. remove(item)์˜ ํ˜•ํƒœ๋กœ ์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์„ ๊ด„ํ˜ธ() ์•ˆ์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] fruits.remove('์‚ฌ๊ณผ') print(fruits) # ๊ฒฐ๊ด๊ฐ’ ['๋ฐ”๋‚˜๋‚˜'] remove()๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ์ฒซ ๋ฒˆ์งธ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๋™์ผํ•œ ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค๋ฉด remove()๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 5) pop() pop() ๋ฉ”์„œ๋“œ๋Š” ์ง€์ •ํ•œ ์ธ๋ฑ์Šค์˜ ํ•ญ๋ชฉ์„ ๋ฆฌ์ŠคํŠธ์—์„œ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ ํ•ญ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์ด ์ œ๊ฑฐ๋˜๊ณ  ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. pop(index)์˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, pop()๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] pop1 = fruits.pop() print(pop1) print(fruits) # ๊ฒฐ๊ด๊ฐ’ ๋ฐ”๋‚˜๋‚˜ ['์‚ฌ๊ณผ'] 6) index() index() ๋ฉ”์„œ๋“œ๋Š” ์ง€์ •ํ•œ ํ•ญ๋ชฉ๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. index(item)์˜ ํ˜•ํƒœ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] idx = fruits.index('์‚ฌ๊ณผ') print(idx) # ๊ฒฐ๊ด๊ฐ’ 7) count() count() ๋ฉ”์„œ๋“œ๋Š” count(item)์˜<NAME>์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ๋ฆฌ์ŠคํŠธ์—์„œ ์ง€์ •ํ•œ ํ•ญ๋ชฉ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์‚ฌ๊ณผ', '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ] cnt = fruits.count('์‚ฌ๊ณผ') print(cnt) # ๊ฒฐ๊ด๊ฐ’ 8) sort() sort() ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ๋“ค์„ ์ •๋ ฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ด„ํ˜ธ ์•ˆ์— ์•„๋ฌด ์˜ต์…˜๋„ ์„ค์ •ํ•˜์ง€ ์•Š๊ณ  sort()๋กœ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ์—๋Š” ๋ฉ”์„œ๋“œ์˜ ๊ธฐ๋ณธ ์ •๋ ฌ ์„ค์ •๋Œ€๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋“ค์ด ์ˆซ์ž์ธ ๊ฒฝ์šฐ์—๋Š” ์ž‘์€ ์ˆซ์ž๋ถ€ํ„ฐ ํฐ ์ˆซ์ž ์ˆœ์„œ๋กœ ์ •๋ ฌ๋˜๊ณ , ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋“ค์ด ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ์—๋Š” ์•ŒํŒŒ๋ฒณ์ˆœ, ๊ฐ€๋‚˜๋‹ค์ˆœ ๋“ฑ ์‚ฌ์ „ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์ˆซ์ž์™€ ๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ ๋“ฑ ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ์š”์†Œ๋“ค์ด ํ˜ผํ•ฉ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์— sort()๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ˆœ์„œ๋ฅผ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', 'ํŒŒ์ธ์• ํ”Œ', '๋”ธ๊ธฐ' ] fruits.sort() print(fruits) # ๊ฒฐ๊ด๊ฐ’ ['๋”ธ๊ธฐ', '๋ฐ”๋‚˜๋‚˜', '์‚ฌ๊ณผ', 'ํŒŒ์ธ์• ํ”Œ'] ์ •๋ ฌํ•  ๋•Œ sort()์— ์ •๋ ฌ ๊ธฐ์ค€(key)๊ณผ ์—ญ์ˆœ ์ •๋ ฌ(reverse) ์˜ต์…˜์„ ๋ณ„๋„๋กœ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ ์ •๋ ฌ ๊ธฐ์ค€์„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋˜ ์—ญ์ˆœ ์ •๋ ฌ์„ True๋กœ ์„ค์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', 'ํŒŒ์ธ์• ํ”Œ', '๋”ธ๊ธฐ' ] fruits.sort(key=len, reverse=True) print(fruits) # ๊ฒฐ๊ด๊ฐ’ ['ํŒŒ์ธ์• ํ”Œ', '๋ฐ”๋‚˜๋‚˜', '์‚ฌ๊ณผ', '๋”ธ๊ธฐ'] ๋ฌธ์ž์—ด์˜ ๊ธธ์ด ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜์˜€์œผ๋ฉฐ, ์—ญ์ˆœ ์ •๋ ฌ์„ ์„ค์ •ํ•˜์—ฌ์„œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ธด ๊ฒƒ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๊ฐ€๋‚˜๋‹ค์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์ง€๋งŒ ์œ„์˜ ์˜ˆ์‹œ์—์„œ๋Š” ์—ญ์ˆœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋™์ผํ•œ ๊ธธ์ด์ธ ์š”์†Œ๋“ค('๋”ธ๊ธฐ', '์‚ฌ๊ณผ')์ด ๊ฐ€๋‚˜๋‹ค์˜ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 9) reverse() reverse() ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ๋’ค์ง‘๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ๋ณ„๋„์˜ ์ธ์ž๋ฅผ ๋ฐ›์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— reverse() ํ˜•ํƒœ๋กœ๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', 'ํŒŒ์ธ์• ํ”Œ', '๋”ธ๊ธฐ' ] fruits.reverse() print(fruits) # ๊ฒฐ๊ด๊ฐ’ ['๋”ธ๊ธฐ', 'ํŒŒ์ธ์• ํ”Œ', '๋ฐ”๋‚˜๋‚˜', '์‚ฌ๊ณผ'] 10) clear() clear() ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ์š”์†Œ๋“ค์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. fruits = [ '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', 'ํŒŒ์ธ์• ํ”Œ', '๋”ธ๊ธฐ' ] fruits.clear() print(fruits) # ๊ฒฐ๊ด๊ฐ’ [] 5. ํŠœํ”Œ (tuple) ํŠœํ”Œ๋„ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ž๋ฃŒํ˜•์œผ๋กœ ๋™์ž‘์ด๋‚˜ ํ™œ์šฉ๋ฒ•์ด ๋ฆฌ์ŠคํŠธ์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํŠœํ”Œ์ด ๋ฆฌ์ŠคํŠธ์™€ ๋‹ค๋ฅธ ์ ์€ ๋‚ด๋ถ€์˜ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ์ฒ˜์Œ ์ƒ์„ฑ ํ›„์— ์š”์†Œ๋ฅผ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€, ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ํŠœํ”Œ์€ ํ•œ ๋ฒˆ ์ƒ์„ฑํ•œ ํ›„์—๋Š” ๊ทธ ๋‚ด๋ถ€์˜ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” ๋Œ€๊ด„ํ˜ธ([])๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด, ํŠœํ”Œ์„ ์ƒ์„ฑํ•  ๋•Œ๋Š” ์†Œ๊ด„ํ˜ธ(())๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋˜๋Š” ์†Œ๊ด„ํ˜ธ ์—†์ด ์‰ผํ‘œ๋กœ ์š”์†Œ๋ฅผ ๋‚˜์—ดํ•จ์œผ๋กœ์จ ํŠœํ”Œ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. tuple1 = (1, 2, 3, 4) print(tuple1) tuple2 = 3, 4, 5, 6 print(tuple2) # ๊ฒฐ๊ด๊ฐ’ (1, 2, 3, 4) (3, 4, 5, 6) ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด ์†Œ๊ด„ํ˜ธ๋กœ ์ƒ์„ฑํ•œ tuple1์ด๋‚˜ ์†Œ๊ด„ํ˜ธ ์—†์ด ๋‚˜์—ดํ•˜์—ฌ ์ƒ์„ฑํ•œ tupe2 ๋ชจ๋‘ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ ๋™์ผํ•˜๊ฒŒ ํŠœํ”Œํ˜•์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. type()์œผ๋กœ ์ž๋ฃŒํ˜•์„ ํ™•์ธํ•ด๋„ ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ tuple๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ํŠœํ”Œ ์—ญ์‹œ ๋ฆฌ์ŠคํŠธ์™€ ๋™์ผํ•˜๊ฒŒ ์–ด๋–ค ์ž๋ฃŒํ˜•์ด๋“  ์š”์†Œ๋กœ ํฌํ•จํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์—ฌ๋Ÿฌ ์ž๋ฃŒํ˜•์„ ํ˜ผํ•ฉํ•˜์—ฌ ์š”์†Œ๋กœ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ํŠœํ”Œ ์ƒ์„ฑ ์‹œ ์œ ์˜ํ•  ์ ์€, ์š”์†Œ๋ฅผ ํ•˜๋‚˜๋งŒ ๊ฐ–๋Š” ํŠœํ”Œ์„ ์ƒ์„ฑํ•  ๋•Œ๋Š” ํ•ด๋‹น ์š”์†Œ ๋’ค์— ๋‹ค์Œ ์š”์†Œ๊ฐ€ ์—†๋”๋ผ๋„ ๋ฐ˜๋“œ์‹œ ์‰ผํ‘œ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์‰ผํ‘œ๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋ƒฅ ๋ณ€์ˆ˜์— ์ž๋ฃŒ๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. tup1 = (1) print(tup1) tup2 = (1, ) print(tup2) # ๊ฒฐ๊ด๊ฐ’ (1, ) ์œ„์˜ ์˜ˆ์‹œ์—์„œ ๋ณด๋ฉด tup1์€ ์†Œ๊ด„ํ˜ธ๋Š” ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ ๋’ค์— ์‰ผํ‘œ๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ํŠœํ”Œ๋กœ ๊ฐ„์ฃผ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ print()๋กœ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ๋„ ํŠœํ”Œ์ด๋ผ๋Š” ์†Œ๊ด„ํ˜ธ ์—†์ด ์ˆซ์ž 1๋งŒ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์— ๋ฐ˜ํ•ด tup2๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ ๋’ค์— ์‰ผํ‘œ๋ฅผ ์ž…๋ ฅํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— tup2๋Š” ํŠœํ”Œ์ด๋ผ๊ณ  ๊ฐ„์ฃผ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. type()์œผ๋กœ ์ž๋ฃŒํ˜•์„ ํ™•์ธํ•ด ๋ด๋„ type(tup1)์„ ์‹คํ–‰ํ•˜๋ฉด int๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€๋งŒ type(tup2)๋ฅผ ์‹คํ–‰ํ•˜๋ฉด tuple์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ํŠœํ”Œ๋„ ๋ฆฌ์ŠคํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ len() ํ•จ์ˆ˜๋กœ ํŠœํ”Œ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ํŠœํ”Œ์—์„œ๋„ ๋ฆฌ์ŠคํŠธ์™€ ๋™์ผํ•˜๊ฒŒ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ํŠœํ”Œ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ์˜๋ฏธ ์˜ˆ์‹œ ๊ฒฐ๊ด๊ฐ’ a + b ํŠœํ”Œ a์™€ ํŠœํ”Œ b๋ฅผ ์—ฐ๊ฒฐํ•˜๊ธฐ ( '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ) + ( '์˜ค๋ Œ์ง€' , ) ('์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์˜ค๋ Œ์ง€') a * b ํŠœํ”Œ a๋ฅผ ์ˆซ์ž b ๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ธฐ ( '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' ) * 2 ('์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜') ํŠœํ”Œ ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ ํŠœํ”Œ๋„ ๋ฆฌ์ŠคํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์š”์†Œ์— ์ˆœ์„œ๊ฐ€ ๋ถ€์—ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค๋กœ ๊ฐ๊ฐ์˜ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ณ  ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ ํŠน์ • ๋ถ€๋ถ„์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ํŠœํ”Œ ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ์˜ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. tuple1 = (1, 2, 3, 4) print(tuple1[2]) # ํŠœํ”Œ ์ธ๋ฑ์‹ฑ print(tuple1[:2]) # ํŠœํ”Œ ์Šฌ๋ผ์ด์‹ฑ # ๊ฒฐ๊ด๊ฐ’ (1, 2) ์œ„์˜ ์ฝ”๋“œ์—์„œ tuple1[2]๋กœ ํŠœํ”Œ์˜ ์„ธ ๋ฒˆ์งธ ์š”์†Œ์— ์ ‘๊ทผํ•˜์˜€์œผ๋ฉฐ, tuple1[:2]๋กœ ํŠœํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ ๋‘ ๋ฒˆ์งธ ์š”์†Œ๊นŒ์ง€ ์Šฌ๋ผ์ด์‹ฑ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ํŠœํ”Œ์—์„œ๋„ ๋ฆฌ์ŠคํŠธ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํŠœํ”Œ์€ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ์™€ ๋‹ค๋ฅด๊ฒŒ ์ธ๋ฑ์‹ฑ์„ ํ†ตํ•ด ์š”์†Œ๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ tuple1[2] = 8 ๊ณผ๊ฐ™์ด ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•˜๋ฉด TypeError ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํŠœํ”Œ ๋ฉ”์„œ๋“œ ํŠœํ”Œ์€ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ํŠน์„ฑ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ์— ๋น„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ์˜ ์ข…๋ฅ˜๊ฐ€ ์ œํ•œ์ ์ž…๋‹ˆ๋‹ค. append()๋‚˜ insert(), extend()์ฒ˜๋Ÿผ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋‚˜ remove(), pop(), clear()์™€ ๊ฐ™์ด ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ, ๊ทธ๋ฆฌ๊ณ  ์š”์†Œ์˜ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” sort()๋‚˜ reverse() ๋ฉ”์„œ๋“œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 1) index() ์•ž์„œ ์‚ดํŽด๋ณธ ๋ฐ”์™€ ๊ฐ™์ด ํŠœํ”Œ๋„ ์š”์†Œ์— ์ˆœ์„œ๊ฐ€ ๋ถ€์—ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์š”์†Œ๋งˆ๋‹ค ์ธ๋ฑ์Šค๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ index() ๋ฉ”์„œ๋“œ๋กœ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์™€ ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋“ค ์ค‘ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๊ฐ€ ํŠœํ”Œ์ด ์—†์œผ๋ฉด ์˜ค๋ฅ˜(ValueError)๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. t = (1, 2, 3, 2, 4, 5) print(t.index(2)) # ๊ฒฐ๊ด๊ฐ’ 2) count() ํŠœํ”Œ์—์„œ๋„ ๋™์ผํ•˜๊ฒŒ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๊ฐ€ ํŠœํ”Œ ๋‚ด์— ๋ช‡ ๋ฒˆ ๋‚˜ํƒ€๋‚˜๋Š”์ง€ ๊ทธ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. t = (1, 2, 3, 2, 4, 5) print(t.count(2)) # ๊ฒฐ๊ด๊ฐ’ 6. ์„ธํŠธ (set) ์„ธํŠธ(set) ์ž๋ฃŒํ˜•์€ ์ˆ˜ํ•™์˜ ์ง‘ํ•ฉ๊ณผ ๊ฐ™์€ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์†Œ๋กœ ๊ฐ–์ง€๋งŒ, ์„ธํŠธ๋Š” ์š”์†Œ์˜ ์ˆœ์„œ๊ฐ€ ์—†๊ณ  ๊ฐ™์€ ์š”์†Œ๋ผ๋ฆฌ๋Š” ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ด ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋˜ํ•œ ์„ธํŠธ๋Š” ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ํ•œ๋ฒˆ ์ƒ์„ฑ๋œ ์„ธํŠธ์˜ ์š”์†Œ๋Š” ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์„ธํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ์ค‘๊ด„ํ˜ธ({})๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“ญ๋‹ˆ๋‹ค. fruits = {"์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "๋”ธ๊ธฐ", "์‚ฌ๊ณผ"} print(fruits) # ๊ฒฐ๊ด๊ฐ’ {'๋”ธ๊ธฐ', '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜'} ์œ„์—์„œ print() ํ•จ์ˆ˜๋กœ ์ถœ๋ ฅ๋œ ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด ์•ž์—์„œ ํ•™์Šตํ•œ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์„ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ์™€๋Š” ๋‹ค๋ฅธ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์„ธํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ž…๋ ฅํ•œ ์ˆœ์„œ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์„ธํŠธ๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์ž…๋ ฅํ•  ๋•Œ๋Š” "์‚ฌ๊ณผ"๋ฅผ 2๋ฒˆ ์ค‘๋ณตํ•˜์—ฌ ์ž…๋ ฅํ–ˆ์ง€๋งŒ, ์„ธํŠธ๋ฅผ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ๋Š” "์‚ฌ๊ณผ"๊ฐ€ ํ•˜๋‚˜๋งŒ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์„ธํŠธ๋Š” ์ค‘๋ณต ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฉฐ ์š”์†Œ์— ์ˆœ์„œ๋ฅผ ๋ถ€์—ฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์„ธํŠธ์˜ ์š”์†Œ์—๋Š” ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์„ธํŠธ์—์„œ๋Š” ์ธ๋ฑ์‹ฑ์ด๋‚˜ ์Šฌ๋ผ์ด์‹ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์„ธํŠธ๋„ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ์ž๋ฃŒํ˜•์„ ํ˜ผํ•ฉํ•˜์—ฌ ์š”์†Œ๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ์ž๋ฃŒํ˜•๋งŒ ์„ธํŠธ์˜ ์š”์†Œ๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•์ธ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ, ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์„ธํŠธ์˜ ์š”์†Œ๋กœ ํฌํ•จ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ์•ž์„œ ์„ธํŠธ๋Š” ์ˆ˜ํ•™์˜ ์ง‘ํ•ฉ๊ณผ ๊ฐ™์€ ๊ฐœ๋…์ด๋ผ๊ณ  ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„ธํŠธ์˜ ์—ฐ์‚ฐ๋„ ์ง‘ํ•ฉ์˜ ์—ฐ์‚ฐ์ธ ํ•ฉ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ ๋“ฑ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ์˜๋ฏธ ์˜ˆ์‹œ ๊ฒฐ๊ด๊ฐ’ A | B ์„ธํŠธ A์™€ ์„ธํŠธ B์˜ ํ•ฉ์ง‘ํ•ฉ(A โˆช B) { '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' } | { '์˜ค๋ Œ์ง€'} {'์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '์˜ค๋ Œ์ง€'} A & B ์„ธํŠธ A์™€ ์„ธํŠธ B์˜ ๊ต์ง‘ํ•ฉ(A โˆฉ B) { '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' } & {'๋ฐ”๋‚˜๋‚˜'} {'๋ฐ”๋‚˜๋‚˜'} A - B ์„ธํŠธ A์™€ ์„ธํŠธ B์˜ ์ฐจ์ง‘ํ•ฉ(A-B) { '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' } - {'๋ฐ”๋‚˜๋‚˜'} {'์‚ฌ๊ณผ'} A ^ B ์„ธํŠธ A์™€ ์„ธํŠธ B์˜ ๋Œ€์นญ ์ฐจ์ง‘ํ•ฉ(A โ–ณ B) { '์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜' } ^ {'๋ฐ”๋‚˜๋‚˜', '๋”ธ๊ธฐ'} {'๋”ธ๊ธฐ', '์‚ฌ๊ณผ'} ์„ธํŠธ ๋ฉ”์„œ๋“œ ์„ธํŠธ๋Š” ์ด๋ฏธ ์ƒ์„ฑ๋œ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ์š”์†Œ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์„ธํŠธ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ์˜ ์ข…๋ฅ˜์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ํ‘œ์˜ ์˜ˆ์‹œ ์ฝ”๋“œ๋Š” ์„ธํŠธ s1 = {1, 2, 3}๊ณผ s2 = {2, 3, 4}๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ์ด๋ฉฐ ๊ฒฐ๊ด๊ฐ’์€ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜์˜๋˜์–ด ๋ณ€๊ฒฝ๋œ ์„ธํŠธ์ž…๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ ์˜๋ฏธ ์˜ˆ์‹œ ๊ฒฐ๊ด๊ฐ’ add(element) ์„ธํŠธ์— ์š”์†Œ ์ถ”๊ฐ€ s1.add(4) s1 = {1, 2, 3, 4} remove(element) ์š”์†Œ๋ฅผ ์„ธํŠธ์—์„œ ์‚ญ์ œ(์—†์œผ๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ) s1.remove(2) s1 = {1, 3} discard(element) ์š”์†Œ๋ฅผ ์„ธํŠธ์—์„œ ์‚ญ์ œ(์—†์–ด๋„ ์—๋Ÿฌ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ) s1.discard(2) s1 = {1, 3} pop() ์ž„์˜์˜ ์š”์†Œ๋ฅผ ์„ธํŠธ์—์„œ ์‚ญ์ œํ•˜๊ณ  ๊ทธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜(๋นˆ ์„ธํŠธ์ธ ๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฐœ์ƒ) s1.pop() ์˜ˆ์ธก ๋ถˆ๊ฐ€ clear() ์„ธํŠธ์˜ ๋ชจ๋“  ์š”์†Œ ์‚ญ์ œ s1.clear() s1 = {} union(set) ์—ฌ๋Ÿฌ ์„ธํŠธ๋“ค์˜ ํ•ฉ์ง‘ํ•ฉ s1.union(s2) {1, 2, 3, 4} intersection(set) ์—ฌ๋Ÿฌ ์„ธํŠธ๋“ค์˜ ๊ต์ง‘ํ•ฉ s1.intersection(s2) {2, 3} difference(set) ์ฒซ ๋ฒˆ์งธ ์„ธํŠธ์™€ ๋‹ค๋ฅธ ์„ธํŠธ๋“ค์˜ ์ฐจ์ง‘ํ•ฉ s1.difference(s2) {1} 7. ๋”•์…”๋„ˆ๋ฆฌ (dictionary) ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ‚ค(key)์™€ ๊ฐ’(value)์˜ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒํ˜•์ž…๋‹ˆ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ค‘๊ด„ํ˜ธ({})๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„ํ•˜๊ณ , ๊ฐ ํ•ญ๋ชฉ์€ 'ํ‚ค : ๊ฐ’'์˜ ํ˜•ํƒœ๋กœ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์˜ˆ์ž…๋‹ˆ๋‹ค. person = { "name": "ํ•œ์†”", "age": 29, "city": "์„œ์šธ" } type(person) # ๊ฒฐ๊ด๊ฐ’ dict type()์˜ ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅ๋œ ๊ฐ’ dict๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. dict()๋ฅผ ์ด์šฉํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. dict()์˜ ๊ด„ํ˜ธ ์•ˆ์— 'ํ‚ค(key) = ๊ฐ’(value)'์˜ ํ˜•ํƒœ๋กœ ํ‚ค์™€ ๊ฐ’์˜ ์Œ์„ ์ž…๋ ฅํ•˜๊ณ  ๋™์ผํ•˜๊ฒŒ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” dict()๋ฅผ ์ด์šฉํ•˜์—ฌ ์œ„์™€ ๋™์ผํ•˜๊ฒŒ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. person = dict(name = "ํ•œ์†”", age = 29, city = "์„œ์šธ" ) print(person) # ๊ฒฐ๊ด๊ฐ’ {'name': 'ํ•œ์†”', 'age': 29, 'city': '์„œ์šธ'} ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ‚ค์™€ ๊ฐ’์ด ์Œ์„ ์ด๋ฃจ๊ธฐ ๋•Œ๋ฌธ์— ํ‚ค๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ํ‚ค์— ์—ฐ๊ฒฐ๋œ ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ค‘๋ณต๋œ ํ‚ค๋Š” ํ—ˆ์šฉ๋˜์ง€ ์•Š์œผ๋ฉฐ ๊ฐ™์€ ํ‚ค๋ฅผ ๋‘ ๋ฒˆ ์‚ฌ์šฉํ•˜๋ฉด ๋งˆ์ง€๋ง‰ ๊ฐ’์ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ‚ค๋Š” ๋ฌธ์ž์—ด, ์ˆซ์ž, ํŠœํ”Œ๊ณผ ๊ฐ™์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ์ž๋ฃŒํ˜•์œผ๋กœ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ’์œผ๋กœ๋Š” ์–ด๋– ํ•œ ์ข…๋ฅ˜์˜ ์ž๋ฃŒํ˜•๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ ๋งŒ๋“  ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ํ‚ค๋ฅผ ํ†ตํ•ด ํ‚ค์— ๋Œ€์‘ํ•˜๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์™€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(person["age"]) # ๊ฒฐ๊ด๊ฐ’ 29 ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์—์„œ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋”•์…”๋„ˆ๋ฆฌ์—์„œ๋Š” ์ธ๋ฑ์Šค ๋Œ€์‹  ํ‚ค๋กœ ๊ฐ’์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์˜ ์ธ๋ฑ์‹ฑ์—์„œ ํ•ด๋‹น ์ธ๋ฑ์Šค๊ฐ€ ์—†์„ ๋•Œ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ๋”•์…”๋„ˆ๋ฆฌ์—์„œ๋„ ํ•ด๋‹น ํ‚ค๊ฐ€ ์—†์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ‚ค๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’์— ์ ‘๊ทผํ•œ ๋‹ค์Œ ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ํ‚ค์™€ ๊ฐ’์˜ ์Œ์„ ์ถ”๊ฐ€/์‚ญ์ œํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. dictionary[key] = new_value์™€ ๊ฐ™์ด ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉฐ, ๋”•์…”๋„ˆ๋ฆฌ์— ํ•ด๋‹น ํ‚ค๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ์—๋Š” ๊ธฐ์กด ๊ฐ’์„ ์ƒˆ๋กœ์šด ๊ฐ’(new_value)์œผ๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋”•์…”๋„ˆ๋ฆฌ์— ํ•ด๋‹น ํ‚ค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ์ž…๋ ฅํ•œ ํ‚ค์™€ ๊ฐ’์˜ ์Œ์„ ์ƒˆ๋กœ ๋”•์…”๋„ˆ๋ฆฌ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. person = { "name": "ํ•œ์†”", "age": 29, "city": "์„œ์šธ" } person["age"] = 30 person["job"] = "์˜์‚ฌ" print(person) # ๊ฒฐ๊ด๊ฐ’ {'name': 'ํ•œ์†”', 'age': 30, 'city': '์„œ์šธ', 'job': '์˜์‚ฌ'} ์œ„์˜ ์˜ˆ์‹œ์—์„œ ๋”•์…”๋„ˆ๋ฆฌ์— "age"๋ผ๋Š” ํ‚ค๊ฐ€ ์ด๋ฏธ ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— "age"์— ์—ฐ๊ฒฐ๋œ ๊ฐ’ 29๊ฐ€ 30์œผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋”•์…”๋„ˆ๋ฆฌ์— "job"์ด๋ผ๋Š” ํ‚ค๋Š” ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— "job : ์˜์‚ฌ"๋ผ๋Š” ์ƒˆ๋กœ์šด ํ‚ค์™€ ๊ฐ’์˜ ์Œ์ด ๋”•์…”๋„ˆ๋ฆฌ์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ํ‚ค์™€ ๊ฐ’์˜ ์Œ์„ ์‚ญ์ œํ•  ๋•Œ๋Š” del์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. del person["job"] ์‚ญ์ œ๋ฅผ ํ•  ๋•Œ๋„ ํ‚ค๋ฅผ ์‚ญ์ œํ•˜๋ฉด ์—ฐ๊ฒฐ๋œ ๊ฐ’๊นŒ์ง€ ๋ชจ๋‘ ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋งŒ์•ฝ ๋”•์…”๋„ˆ๋ฆฌ์— ํ‚ค๊ฐ€ ์—†์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ์— ํŠน์ •ํ•œ ํ‚ค๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. '์ฐพ๊ณ ์ž ํ•˜๋Š” ํ‚ค in ๋”•์…”๋„ˆ๋ฆฌ'๋ฅผ ์ด์šฉํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฒฐ๊ด๊ฐ’์€ ๋ถ€์šธ ์ž๋ฃŒํ˜•(True/False)๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. person = { "name": "ํ•œ์†”", "age": 29, "city": "์„œ์šธ" } print("job" in person) # ๊ฒฐ๊ด๊ฐ’ False ๋”•์…”๋„ˆ๋ฆฌ ๋ฉ”์„œ๋“œ ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ฃผ์š” ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์‹œ๋Š” 'person = { "name": "ํ•œ์†”", "age": 29, "city": "์„œ์šธ" }'์„ ๊ฐ€์ง€๊ณ  ๊ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ ์˜๋ฏธ ์˜ˆ์‹œ ๊ฒฐ๊ด๊ฐ’ keys() ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ํ‚ค๋ฅผ ๋ฐ˜ํ™˜ person.keys() dict_keys(['name', 'age', 'city']) values() ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ๊ฐ’์„ ๋ฐ˜ํ™˜ person.values() dict_values(['ํ•œ์†”', 29, '์„œ์šธ']) items() ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ํ‚ค์™€ ๊ฐ’์˜ ์Œ์„ ๋ฐ˜ํ™˜ person.items() dict_items([('name', 'ํ•œ์†”'), ('age', 29), ('city', '์„œ์šธ')]) get(key) ์ง€์ •๋œ ํ‚ค์— ๋Œ€์‘ํ•˜๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜(์—†์œผ๋ฉด None์„ ๋ฐ˜ํ™˜) person.get("city") '์„œ์šธ' pop() ์ง€์ •๋œ ํ‚ค์™€ ๊ทธ์— ๋Œ€์‘ํ•˜๋Š” ๊ฐ’์„ ์‚ญ์ œํ•˜๊ณ  ๊ทธ ๊ฐ’์„ ๋ฐ˜ํ™˜ person.pop("name") 'ํ•œ์†”' clear() ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ํ•ญ๋ชฉ ์‚ญ์ œ person.clear() person = {} ์ง€๊ธˆ๊นŒ์ง€ ํŒŒ์ด์ฌ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋‹ค์–‘ํ•œ ์ž๋ฃŒํ˜•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ž๋ฃŒํ˜•๋“ค์€ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž๋ฃŒํ˜•์€ ํŠน์ •ํ•œ ์—ฐ์‚ฐ๋“ค์„ ์ง€์›ํ•˜๋ฉฐ, ๊ทธ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์กฐ์ž‘๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ์–ธ๊ธ‰ํ•œ ๋ฐ”์™€ ๊ฐ™์ด ์ž๋ฃŒํ˜•์€ ๋ณ€์ˆ˜์— ๊ฐ’์ด ํ• ๋‹น๋  ๋•Œ ์ž๋™์œผ๋กœ ์ถ”๋ก ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ์„ ์–ธ์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์€ ๋™์  ํƒ€์ดํ•‘(dynamic typing) ์–ธ์–ด๋กœ, ๋ณ€์ˆ˜์˜ ์ž๋ฃŒํ˜•์€ ํ• ๋‹น๋œ ๊ฐ’์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. 02-03. ์กฐ๊ฑด ๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํŒŒ์ด์ฌ์˜ ์ œ์–ด๋ฌธ์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œ์–ด๋ฌธ์€ ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„์„ ์ œ์–ดํ•˜๋Š” ๊ตฌ๋ฌธ์œผ๋กœ, ์กฐ๊ฑด๋ฌธ์œผ๋กœ ํŠน์ • ์กฐ๊ฑด์„ ์„ค์ •ํ•˜์—ฌ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๊ฒŒ ํ•˜๊ฑฐ๋‚˜ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํŠน์ • ์ž‘์—…์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ฑด ๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ์— ๋Œ€ํ•ด์„œ ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์กฐ๊ฑด๋ฌธ ์กฐ๊ฑด๋ฌธ์€ ๋ง ๊ทธ๋Œ€๋กœ ํŠน์ •ํ•œ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜์—ฌ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์–ด๋–ค ๋™์ž‘์„ ํ•˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ตฌ๋ฌธ์œผ๋กœ, if ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด๋ฌธ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. if ๋ฌธ์€ ๋‹จ๋…์œผ๋กœ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ else, elif ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 1) if ๋ฌธ ๋จผ์ € if ๋ฌธ์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. if ๋ฌธ์€ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์ด ์ฐธ(True)์ธ์ง€ ๊ฑฐ์ง“(False)์ธ์ง€ ๊ฒ€์‚ฌํ•˜๊ณ  ๊ทธ์— ๋งž๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. if ๋ฌธ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. if ์กฐ๊ฑด : ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ์— ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก if ๋’ค์— ์กฐ๊ฑด์„ ์ž…๋ ฅํ•œ ๋‹ค์Œ ์ฝœ๋ก (:)์„ ์ž…๋ ฅํ•˜๊ณ  ์ค„ ๋ฐ”๊ฟˆ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ์ค„์— ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ์— ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€, ๋ฐ˜๋“œ์‹œ ๊ณต๋ฐฑ ๋„ค ์นธ ๋˜๋Š” ํ•œ ํƒญ(Tab)์„ ๋„์›Œ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•œ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ๋Š” ์ฝœ๋ก (:) ์ž…๋ ฅ ํ›„ ์—”ํ„ฐํ‚ค๋กœ ์ค„๋ฐ”๊ฟˆ์„ ์‹คํ–‰ํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋“ค์—ฌ ์“ฐ๊ธฐ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋“ค์—ฌ ์“ฐ๊ธฐ์— ๋Œ€ํ•ด์„œ๋Š” ์ดํ›„ '02-09. ๊ธฐํƒ€ ๋ฌธ๋ฒ•'์—์„œ ์ž์„ธํžˆ ๋‹ค๋ค„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. age = 4 if age < 7: print("์–ด๋ฆฐ์ด ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.") # ๊ฒฐ๊ด๊ฐ’ ์–ด๋ฆฐ์ด ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ if ๋ฌธ์„ ์‚ดํŽด๋ณด๋ฉด, age๊ฐ€ 20 ๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ(True)์— print()๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ age๊ฐ€ 20 ์ด์ƒ์ธ ๊ฒฝ์šฐ(False)์—๋Š” print()๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์กฐ๊ฑด๋ฌธ์—์„œ๋Š” ์ฃผ์–ด์ง„ ์กฐ๊ฑด์˜ ์ฐธ๊ณผ ๊ฑฐ์ง“์„ ํŒ๋ณ„ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋น„๊ต ์—ฐ์‚ฐ์ด๋‚˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ ๋“ฑ์„ ์กฐํ•ฉํ•˜์—ฌ ์ฐธ/๊ฑฐ์ง“ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 2) elif์™€ else ์œ„์—์„œ ์•Œ์•„๋ณธ if ๋ฌธ์„ ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์กฐ๊ฑด์— ๋งž๋Š” ๊ฒฝ์šฐ์— ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ๋งŒ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์กฐ๊ฑด์ด ์ฐธ(True)์ผ ๋•Œ๋Š” A๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€๋งŒ, ๊ฑฐ์ง“(False)์ธ ๊ฒฝ์šฐ์—๋Š” B๋ฅผ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์„ค์ •ํ•˜๋ ค๋ฉด if ๋ฌธ์— else๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. else๋ฅผ ํฌํ•จํ•œ if ๋ฌธ์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. if ์กฐ๊ฑด : ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ์— ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก else: ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๊ฒฝ์šฐ์— ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๊ฒฝ์šฐ์—๋Š” ๋ฌด์กฐ๊ฑด else๋กœ ๋„˜์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— else ๋’ค์—๋Š” ์กฐ๊ฑด์„ ์ ์ง€ ์•Š๊ณ  ์ฝœ๋ก (:)์„ ๋ฐ”๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋™์ผํ•˜๊ฒŒ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•œ ๋‹ค์Œ ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๋•Œ ์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์„œ ๋” ๋‚˜์•„๊ฐ€ ์กฐ๊ฑด์„ ์—ฌ๋Ÿฌ ๊ฐœ ์„ค์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” elif๋กœ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์กฐ๊ฑด 1์ด ์ฐธ์ผ ๊ฒฝ์šฐ์— ์‹คํ–‰ํ•  ์ฝ”๋“œ, ์กฐ๊ฑด 1์€ ๊ฑฐ์ง“์ด์ง€๋งŒ ์กฐ๊ฑด 2๋Š” ์ฐธ์ผ ๊ฒฝ์šฐ์— ์‹คํ–‰ํ•  ์ฝ”๋“œ, ๊ทธ๋ฆฌ๊ณ  ์กฐ๊ฑด 1๊ณผ ์กฐ๊ฑด 2 ๋ชจ๋‘ ๊ฑฐ์ง“์ผ ๋•Œ ์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ๊ฐ๊ฐ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. if ์กฐ๊ฑด 1: ์กฐ๊ฑด 1์ด ์ฐธ์ผ ๊ฒฝ์šฐ์— ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก elif ์กฐ๊ฑด 2: ์กฐ๊ฑด 1์€ ๊ฑฐ์ง“์ด์ง€๋งŒ ์กฐ๊ฑด 2๊ฐ€ ์ฐธ์ผ ๊ฒฝ์šฐ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก elif ์กฐ๊ฑด 3: ์กฐ๊ฑด 1๊ณผ ์กฐ๊ฑด 2๋Š” ๊ฑฐ์ง“์ด์ง€๋งŒ ์กฐ๊ฑด 3์ด ์ฐธ์ผ ๊ฒฝ์šฐ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ฃฉ else: ์กฐ๊ฑด 1๊ณผ ์กฐ๊ฑด 2, ์กฐ๊ฑด 3 ๋ชจ๋‘ ๊ฑฐ์ง“์ผ ๊ฒฝ์šฐ์— ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก elif ๋’ค์—๋Š” ๋˜ ๋‹ค๋ฅธ ์กฐ๊ฑด์„ ์ž…๋ ฅํ•˜๊ณ , ์ด์ „์˜ ์กฐ๊ฑด์€ ๊ฑฐ์ง“์ด์ง€๋งŒ ํ•ด๋‹น ์กฐ๊ฑด์€ ์ฐธ์ผ ๋•Œ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. ์œ„์˜ ๊ตฌ์กฐ์—์„œ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋“ฏ ์กฐ๊ฑด์„ ์—ฌ๋Ÿฌ ๊ฐœ ์„ค์ •ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด elif๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” elif์™€ else๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. age = 18 if age < 7: print("์–ด๋ฆฐ์ด ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.") elif 7 <= age < 20: print("์ฒญ์†Œ๋…„ ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.") else: print("์„ฑ์ธ ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.") # ๊ฒฐ๊ด๊ฐ’ ์ฒญ์†Œ๋…„ ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. 3) ์ค‘์ฒฉ ์กฐ๊ฑด๋ฌธ ์กฐ๊ฑด์˜ ์ฐธ/๊ฑฐ์ง“ ํŒ๋ณ„ ํ›„์— ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋˜ ๋‹ค๋ฅธ ์กฐ๊ฑด๋ฌธ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ค‘์ฒฉ ์กฐ๊ฑด๋ฌธ์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ค‘์ฒฉ ์กฐ๊ฑด๋ฌธ์€ ์ฝ”๋“œ ๋ธ”๋ก์— ์กฐ๊ฑด๋ฌธ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. age = 1 if age < 7: if age < 3: print("3์„ธ ๋ฏธ๋งŒ์€ ๋ฌด๋ฃŒ์ž…๋‹ˆ๋‹ค.") else: print("์–ด๋ฆฐ์ด ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.") elif 7 <= age < 20: print("์ฒญ์†Œ๋…„ ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.") else: print("์„ฑ์ธ ๊ฐ€๊ฒฉ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.") # ๊ฒฐ๊ด๊ฐ’ 3์„ธ ๋ฏธ๋งŒ์€ ๋ฌด๋ฃŒ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ age๊ฐ€ 7๋ฏธ๋งŒ์ธ ์ฒซ ๋ฒˆ์งธ ์กฐ๊ฑด์—์„œ ์ฐธ์ธ ๊ฒฝ์šฐ ๊ทธ ์•ˆ์˜ ๋˜ ๋‹ค๋ฅธ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. ํ•ด๋‹น ์กฐ๊ฑด๋ฌธ ์•ˆ์— ๋˜๋‹ค์‹œ if์™€ else๊ฐ€ ์žˆ์–ด์„œ age๊ฐ€ 3 ๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ์™€ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ๋กœ ์กฐ๊ฑด์ด ๋‹ค์‹œ ๋‚˜๋ˆ„์–ด์ง‘๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์กฐ๊ฑด๋ฌธ์„ ์ค‘์ฒฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ณต๋ฌธ ํŠน์ • ์ž‘์—…์„ ๋ฐ˜๋ณต์‹œํ‚ค๋Š” ๋ฐ˜๋ณต๋ฌธ์—๋Š” for ๋ฌธ๊ณผ while ๋ฌธ์ด ์žˆ์Šต๋‹ˆ๋‹ค. for ๋ฌธ์€ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ•ด๋‹น ๋ฒ”์œ„๋งŒํผ ์ฝ”๋“œ๋ฅผ ๋ฐ˜๋ณตํ•˜๊ณ , while ๋ฌธ์€ ํŠน์ • ์กฐ๊ฑด์„ ์ง€์ •ํ•˜์—ฌ ์กฐ๊ฑด์ด ๋งŒ์กฑ๋˜๋Š” ๋™์•ˆ ๊ณ„์†ํ•ด์„œ ์ฝ”๋“œ๋ฅผ ๋ฐ˜๋ณต ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. for ๋ฌธ๊ณผ while ๋ฌธ์„ ๊ฐ๊ฐ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. for ๋ฌธ for ๋ฌธ์€ ์•ž์„œ ์„ค๋ช…ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด ์ง€์ •๋œ ๋ฒ”์œ„๋งŒํผ ๋ฐ˜๋ณต์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ•˜๊ณ  ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•  ์ž‘์—…์„ ์ „๋‹ฌํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. for ๋ฌธ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. for ๋ณ€์ˆ˜ in ๋ฐ˜๋ณตํ•  ๋ฒ”์œ„: ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก for ๋’ค์— '๋ณ€์ˆ˜ in ๋ฐ˜๋ณตํ•  ๋ฒ”์œ„'๋ฅผ ์ ์€ ๋‹ค์Œ if ๋ฌธ์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฝœ๋ก (:)์„ ์จ์ฃผ๊ณ  ์ค„์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก์˜ ์•ž์— ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ณต๋ฐฑ ๋„ค ์นธ ๋˜๋Š” ํƒญ(Tab) ํ•œ ์นธ์„ ์ž…๋ ฅํ•ด ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. for ๋’ค์— ๋‚˜์˜ค๋Š” ๋ณ€์ˆ˜๋Š” ๋ฐ˜๋ณตํ•  ๋ฒ”์œ„ ์•ˆ์˜ ๊ฐ ํ•ญ๋ชฉ์„ ํ•˜๋‚˜์”ฉ ๊ฐ€๋ฆฌํ‚ค๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฐ˜๋ณตํ•  ๋ฒ”์œ„์—์„œ ํ•˜๋‚˜์”ฉ ํ•ญ๋ชฉ์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ  ๊ทธ ๋ณ€์ˆ˜๋กœ ๋‹ค์Œ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ฝ”๋“œ ์ˆ˜ํ–‰์„ ๋งˆ์น˜๋ฉด ๋„๋Œ์ดํ‘œ์ฒ˜๋Ÿผ ๋‹ค์‹œ ์œ—์ค„๋กœ ๋Œ์•„๊ฐ€์„œ ๋ฒ”์œ„์—์„œ ๋‹ค์Œ ํ•ญ๋ชฉ์„ ๊ฐ€์ง€๊ณ  ์˜ค๊ณ  ๋ณ€์ˆ˜์— ์ €์žฅํ•ด ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์ž‘์—…์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐ˜๋ณต ์ž‘์—…์€ ์ง€์ •๋œ ๋ฒ”์œ„๊ฐ€ ๋๋‚  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ณตํ•  ๋ฒ”์œ„๋Š” ์–ด๋””์„œ๋ถ€ํ„ฐ ์–ด๋””๊นŒ์ง€๋ฅผ ๋ฐ˜๋ณตํ• ์ง€๋ฅผ ์ •ํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ range() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆซ์ž ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ, ๋ฌธ์ž์—ด, ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด(iterable)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๋ฉฐ ๋ฐ˜๋ณต ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ๋ช…๋ นํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. for i in range(5): print(i) # ๊ฒฐ๊ด๊ฐ’ 1 3 range() ํ•จ์ˆ˜๋Š” ์—ฐ์†๋œ ์ˆซ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ด์ฌ ๋‚ด์žฅ ํ•จ์ˆ˜๋กœ for ๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์ผ์ •ํ•œ ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. range(์‹œ์ž‘ ๊ฐ’, ์ข…๋ฃŒ ๊ฐ’, ๊ฐ„๊ฒฉ)์œผ๋กœ ์ˆซ์ž ์ƒ์„ฑ ์˜ต์…˜์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹œ์ž‘ ๊ฐ’๊ณผ ๊ฐ„๊ฒฉ์€ ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ข…๋ฃŒ ๊ฐ’์€ ์ƒ๋žตํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ์ˆซ์ž๋Š” ์ข…๋ฃŒ ๊ฐ’์ด ์•„๋‹Œ ์ข…๋ฃŒ ๊ฐ’๋ณด๋‹ค 1 ์ž‘์€ ์ˆซ์ž์ž…๋‹ˆ๋‹ค. range ํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ข…๋ฃŒ ๊ฐ’์„ ์ƒ๋žตํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— range() ํ•จ์ˆ˜ ์•ˆ์— ์ˆซ์ž๊ฐ€ ํ•˜๋‚˜๋งŒ ์ž…๋ ฅ๋  ๊ฒฝ์šฐ ํŒŒ์ด์ฌ์€ ์ž๋™์ ์œผ๋กœ ์ข…๋ฃŒ ๊ฐ’์ด ์ž…๋ ฅ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž๋ฅผ 2๊ฐœ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ์—๋Š” ์‹œ์ž‘ ๊ฐ’๊ณผ ์ข…๋ฃŒ ๊ฐ’์œผ๋กœ ์ธ์‹๋˜๋ฉฐ ๊ฐ„๊ฒฉ์€ ์ž๋™์ ์œผ๋กœ 1์”ฉ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ range(5)๋Š” ์‹œ์ž‘ ๊ฐ’์ด ์ƒ๋žต๋˜์–ด ๊ฐ€์žฅ ์ฒ˜์Œ ์ˆซ์ž์ธ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ ๋งˆ์ง€๋ง‰ ์ˆซ์ž๋Š” 5๋ณด๋‹ค 1 ์ž‘์€ ์ˆ˜์ธ 4๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋กœ ์ธํ•ด ์œ„์˜ for ๋ฌธ์—์„œ ๋ฐ˜๋ณต ๋ฒ”์œ„๋Š” 0๋ถ€ํ„ฐ 4๊นŒ์ง€ ์ˆซ์ž๊ฐ€ ๋˜๋ฉฐ ๊ฐ ์ˆซ์ž ํ•ญ๋ชฉ์ด ํ•˜๋‚˜์”ฉ ๋ณ€์ˆ˜ i์— ์ €์žฅ๋˜๋ฉฐ ๊ฐ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  print() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฒ”์œ„ ์•ˆ์—์„œ ์ฝ”๋“œ๋ฅผ ๋ฐ˜๋ณต ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋กœ 0๋ถ€ํ„ฐ 4๊นŒ์ง€ ์ˆซ์ž๊ฐ€ ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด๋ฒˆ์—๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  for ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. fruits = ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '๋”ธ๊ธฐ'] for fruit in fruits: print(fruit) # ๊ฒฐ๊ด๊ฐ’ ์‚ฌ๊ณผ ๋ฐ”๋‚˜๋‚˜ ๋”ธ๊ธฐ ์ด๋ฒˆ์—๋Š” ๋ฐ˜๋ณตํ•  ๋ฒ”์œ„๋ฅผ ๋ฆฌ์ŠคํŠธ fruits๋กœ ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ์— ํ•˜๋‚˜์”ฉ ์ ‘๊ทผํ•˜์—ฌ ๊ฐ€์ง€๊ณ  ์˜จ ๋‹ค์Œ ๋ณ€์ˆ˜ fruit์— ์ €์žฅํ•˜๊ณ  ํ•ด๋‹น ๋ณ€์ˆ˜๋กœ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ž‘์—…์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ๋ฐ˜๋ณต ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ์—๋Š” ์ž‘์—…์ด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ์ฝ”๋“œ์—์„œ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋‹ค ๋ณด๋ฉด ์—ฌ๋Ÿฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋™์‹œ์— ๋ฐ˜๋ณต ์ž‘์—…์„ ํ•ด์•ผ ํ•˜๊ฑฐ๋‚˜ ๋˜๋Š” ๋ฆฌ์ŠคํŠธ์˜์—์„œ ๋ฐ˜๋ณต ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ๊ฐ ์š”์†Œ์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค๊ฐ€ ํ•จ๊ป˜ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. enumerate enumerate์€ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด(๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ๋ฌธ์ž์—ด ๋“ฑ)๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ ์š”์†Œ์™€ ํ•ด๋‹น ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ํŠœํ”Œ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ ์›ํ•˜๋Š” ๋‹ค๋ฅธ ์‹œ์ž‘ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ฐ’์„ ๋ณ„๋„๋กœ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ for ๋ฌธ ๋’ค์— ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ€์ ธ์˜จ ์š”์†Œ์˜ ๊ฐ’์„ ์ €์žฅํ•  ๋ณ€์ˆ˜์™€ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜, ์ด 2๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ณ  enumerate() ์•ˆ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. for ์ธ๋ฑ์Šค ๋ณ€์ˆ˜, ๊ฐ’ ๋ณ€์ˆ˜ in enumerate(๋ฆฌ์ŠคํŠธ): ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก ๊ทธ๋Ÿฌ๋ฉด ์‹ค์ œ ์ฝ”๋“œ์—์„œ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ print() ํ•จ์ˆ˜๋กœ ์ธ๋ฑ์Šค์™€ ๊ฐ’์„ ์ถœ๋ ฅํ•จ์œผ๋กœ์จ ์ž‘๋™ ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. fruits = ['์‚ฌ๊ณผ', '๋ฐ”๋‚˜๋‚˜', '๋”ธ๊ธฐ'] for idx, fruit in enumerate(fruits): print(idx, fruit) # ๊ฒฐ๊ด๊ฐ’ 0 ์‚ฌ๊ณผ 1 ๋ฐ”๋‚˜๋‚˜ 2 ๋”ธ๊ธฐ fruits ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ญ๋ชฉ์„ ๊ฐ€์ง€๊ณ  ์˜ค๋Š”๋ฐ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค๋Š” ๋ณ€์ˆ˜ idx์—, ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ๊ฐ’์„ ๋ณ€์ˆ˜ fruit์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ๋ผ์ธ์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ ํ›„ ๋‹ค์‹œ ๋ฆฌ์ŠคํŠธ์—์„œ ๋‹ค์Œ ํ•ญ๋ชฉ์„ ๊ฐ€์ ธ์™€ ์ž‘์—…์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ฅผ ๋ณ„๋„๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ๋œ ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด ํ•ญ๋ชฉ์ด ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ๋งˆ๋‹ค idx ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ ์ธ๋ฑ์Šค๋„ ๊ฐ™์ด ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. zip ์ด๋ฒˆ์—๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ for ๋ฌธ์œผ๋กœ ํ•จ๊ป˜ ์ž‘์—…ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž‘์—…ํ•  ๋•Œ๋Š” zip() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. zip() ํ•จ์ˆ˜๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด(iterable)๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›์•„์„œ ๊ฐ™์€ ์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ๋“ค์„ ๋ฌถ์–ด ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. for ๋ณ€์ˆ˜ 1, ๋ณ€์ˆ˜ 2 zip(๋ฆฌ์ŠคํŠธ 1, ๋ฆฌ์ŠคํŠธ 2): ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก ๊ฐ ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ญ๋ชฉ์„ ํ•˜๋‚˜์”ฉ ๊ฐ€์ ธ์™€์„œ ๋ณ€์ˆ˜ 1๊ณผ ๋ณ€์ˆ˜ 2์— ๊ฐ๊ฐ ์ €์žฅํ•œ ๋‹ค์Œ ๋‘ ๋ณ€์ˆ˜๋กœ ๋’ท ์ค„์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. zip() ํ•จ์ˆ˜๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ํŠœํ”Œ์˜ ์ˆ˜๋Š” ๊ฐ€์žฅ ์งง์€ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์˜ ๊ธธ์ด์— ๋”ฐ๋ผ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋งŒ์•ฝ ๋ฆฌ์ŠคํŠธ A์˜ ์š”์†Œ๊ฐ€ ๋ฆฌ์ŠคํŠธ B์˜ ์š”์†Œ๋ณด๋‹ค ์ ์„ ๊ฒฝ์šฐ ๋ฐ˜๋ณต ์ž‘์—…์€ ๋ฆฌ์ŠคํŠธ A์˜ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋กœ ๊ธธ์ด๊ฐ€ ์ œํ•œ๋˜๋ฉฐ, ๋ฆฌ์ŠคํŠธ B์— ๋‚จ๋Š” ์š”์†Œ๋Š” ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "๋ฉœ๋ก "] colors = ["๋นจ๊ฐ•", "๋…ธ๋ž‘"] for fruit, color in zip(fruits, colors): print(fruit, color) # ๊ฒฐ๊ด๊ฐ’ ์‚ฌ๊ณผ ๋นจ๊ฐ• ๋ฐ”๋‚˜๋‚˜ ๋…ธ๋ž‘ ๋ฆฌ์ŠคํŠธ fruits์™€ colors์—์„œ ๊ฐ๊ฐ ๋™์ผํ•œ ์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ(์‚ฌ๊ณผ - ๋นจ๊ฐ•, ๋ฐ”๋‚˜๋‚˜ - ๋…ธ๋ž‘)๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ fruits์—๋Š” ์„ธ ๋ฒˆ์งธ ์š”์†Œ๋กœ "๋”ธ๊ธฐ"๊ฐ€ ์žˆ์ง€๋งŒ, ๋ฆฌ์ŠคํŠธ colors์—๋Š” ์„ธ ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— for ๋ฌธ์—์„œ๋Š” ๋‘ ๋ฒˆ์งธ ์š”์†Œ๊นŒ์ง€ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. zip() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜๋ณต๋ฌธ์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ ๋™์ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. for ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ in range(len(๋ฆฌ์ŠคํŠธ 1)): ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก ์œ„์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ์—์„œ ์‚ดํŽด๋ณด๋ฉด ํ•œ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋งŒ ์‚ฌ์šฉ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜์•„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ํ’€์–ด์„œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € len() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ 1์˜ ๊ธธ์ด๋ฅผ ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. range() ํ•จ์ˆ˜์—๋Š” ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•˜์—ฌ range()์˜ ์ธ์ž๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ, range() ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ 1์˜ ๊ธธ์ด๋ฅผ ๋ฒ”์œ„๋กœ ํ•˜์—ฌ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅ๋˜๋Š” ์ˆซ์ž๋Š” ๋ฆฌ์ŠคํŠธ 1์˜ ์ธ๋ฑ์Šค์™€ ๋™์ผํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฆฌ์ŠคํŠธ 1๊ณผ ๋ฆฌ์ŠคํŠธ 2๋Š” ๊ธธ์ด๊ฐ€ ๋™์ผํ•œ ๋ฆฌ์ŠคํŠธ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋กœ ํ•˜๋‚˜์”ฉ ์ ‘๊ทผํ•˜๋ฉด ๊ฐ™์€ ์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ๋“ค์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค. fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "๋ฉœ๋ก "] colors = ["๋นจ๊ฐ•", "๋…ธ๋ž‘", "์ดˆ๋ก"] for i in range(len(fruits)): print(fruits[i], colors[i]) # ๊ฒฐ๊ด๊ฐ’ ์‚ฌ๊ณผ ๋นจ๊ฐ• ๋ฐ”๋‚˜๋‚˜ ๋…ธ๋ž‘ ๋ฉœ๋ก  ์ดˆ๋ก len(fruits)๋กœ fruits์˜ ๊ธธ์ด(3)๋ฅผ ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ range(3)์ด ๋˜์–ด 0๋ถ€ํ„ฐ 2๊นŒ์ง€ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ์ฐจ๋ก€๋กœ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ํ•˜๋‚˜์”ฉ ๋ฐ˜ํ™˜๋˜์–ด ๋ณ€์ˆ˜ i์— ์ €์žฅ๋˜๊ณ  i๋ฅผ ๊ฐ€์ง€๊ณ  ๋‹ค์Œ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. fruits[i]์™€ colors[i]๋กœ ๊ฐ๊ฐ์˜ ๋ฆฌ์ŠคํŠธ์—์„œ ์ธ๋ฑ์Šค๊ฐ€ ํ˜„์žฌ ์ˆซ์ž i์ธ ์š”์†Œ์— ์ ‘๊ทผํ•˜์—ฌ print()๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด์ค‘ for ๋ฌธ ๋‘ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜๋ณต๋ฌธ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ for ๋ฌธ์„ ์ค‘์ฒฉํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์ค‘ for ๋ฌธ์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ์™ธ๋ถ€ for ๋ฌธ์—์„œ ๊ฐ ํ•ญ๋ชฉ์„ ๋ฐ˜๋ณตํ•  ๋•Œ๋งˆ๋‹ค ๋‚ด๋ถ€์˜ for ๋ฌธ ์ „์ฒด๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ด์ค‘ for ๋ฌธ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. for ๋ณ€์ˆ˜ 1 in ๋ฐ˜๋ณตํ•  ๋ฒ”์œ„ 1: for ๋ณ€์ˆ˜ 2 in ๋ฐ˜๋ณตํ•  ๋ฒ”์œ„ 2: ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก ์ด์™€ ๊ฐ™์€ ์ด์ค‘ for ๋ฌธ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์กฐํ•ฉ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ค‘์ฒฉ๋œ ๋ฐ์ดํ„ฐ๋‚˜ ์กฐํ•ฉ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์ด์ค‘ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ๊ตฌ๋‹จ์„ ์ถœ๋ ฅํ•ด ๋ณด๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. for i in range(2, 10): for j in range(1, 10): print(f"{i} x {j} = {i*j}") print("----------") # ๊ฒฐ๊ด๊ฐ’ 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 ...(์ค‘๋žต)... 9 x 8 = 72 9 x 9 = 81 ---------- ์ถœ๋ ฅ๊ฐ’์˜ ๊ธธ์ด ๋•Œ๋ฌธ์— ์œ„์—์„œ๋Š” ๊ฒฐ๊ด๊ฐ’์˜ ์ผ๋ถ€๋ฅผ ์ƒ๋žตํ•˜์˜€์ง€๋งŒ, ์‹ค์ œ ์ฝ˜์†”์—์„œ ์‹คํ–‰ํ•ด ๋ณด๋ฉด 2๋‹จ๋ถ€ํ„ฐ 9๋‹จ๊นŒ์ง€ ๋ชจ๋“  ๊ตฌ๊ตฌ๋‹จ์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด, ๋จผ์ € ์™ธ๋ถ€ for ๋ฌธ์—์„œ range(2, 10)์œผ๋กœ 2๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ˆซ์ž๋ฅผ ์ฐจ๋ก€๋กœ ํ•˜๋‚˜์”ฉ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋ณ€์ˆ˜ i์— ๋„ฃ์–ด ๋‹ค์Œ ๋ผ์ธ์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ผ์ธ์ด ๋‹ค์‹œ ๋‚ด๋ถ€ for ๋ฌธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๋ถ€ for ๋ฌธ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€ for ๋ฌธ์—์„œ๋Š” range(1, 10)์œผ๋กœ 1๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ˆซ์ž๊ฐ€ ์ฐจ๋ก€๋กœ ๋ฐ˜ํ™˜๋˜์–ด j์— ์ €์žฅ๋œ ํ›„ ๋‹ค์Œ ๋ผ์ธ์˜ ์ฝ”๋“œ print()๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. print()์˜ ๊ด„ํ˜ธ์— f"{i} x {j} = {i*j}"๋กœ ์•ŒํŒŒ๋ฒณ f์™€ ์ค‘๊ด„ํ˜ธ({})๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ฌธ์ž์—ด ํฌ๋ฉ”์ดํŒ…(foramting)์œผ๋กœ ๋ฌธ์ž์—ด ์•ž์— f๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํฌ๋ฉ”์ดํŒ…์ด๋ผ๋Š” ํ‘œ์‹œ๋ฅผ ํ•ด์ฃผ๊ณ , ํฐ๋”ฐ์˜ดํ‘œ(") ์•ˆ์— ์ถœ๋ ฅํ•  ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์ฃผ๋Š”<NAME>์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ํฐ๋”ฐ์˜ดํ‘œ(") ์•ˆ์˜ ๋ฌธ์ž์—ด ์ค‘์—์„œ {}์•ˆ์— ์žˆ๋Š” ๋ฌธ์ž์—ด์€ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ {} ์•ˆ์˜ ๋ฌธ์ž์—ด์„ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ํ•˜๋Š” ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ ๊ฐ’์ด ํ•ด๋‹น ์œ„์น˜์— ๋Œ€์‘ํ•˜์—ฌ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ํฌ๋ฉ”์ดํŒ…์— ๋Œ€ํ•ด์„œ๋Š” '02-09. ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ'์—์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” print(f"{i} x {j} = {ij}")์—์„œ {i}, {j}, {ij}์— ํ˜„์žฌ ์ˆซ์ž(i, j)๊ฐ€ ์ „๋‹ฌ๋˜์–ด ์ถœ๋ ฅ๋œ๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ฒ˜์Œ ์‹คํ–‰๋˜๋Š” ์ž‘์—…์€ i๊ฐ€ 2์ผ ๋•Œ j๊ฐ€ 1๋กœ '2 x 1 = 2'๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. i๊ฐ€ 2์ผ ๋•Œ j๊ฐ€ 1๋ถ€ํ„ฐ 9๊นŒ์ง€ ๋ฐ˜๋ณต๋˜๊ณ  ๊ทธ๋‹ค์Œ i๊ฐ€ 3์ด ๋˜๋ฉด ๋‹ค์‹œ ๋˜ j๊ฐ€ 1๋ถ€ํ„ฐ 9๊นŒ์ง€ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ด์ค‘ for ๋ฌธ์ด ์ž‘๋™ํ•˜๋‹ค๊ฐ€ i๊ฐ€ 9์ผ ๋•Œ j๋„ 9์ธ ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ํ›„ ์ „์ฒด ๋ฐ˜๋ณต๋ฌธ์ด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. ์ปดํ”„๋ฆฌ ํ—จ ์…˜(Comprehension) ์ปดํ”„๋ฆฌ ํ—จ ์…˜(List Comprehension)์€ ํŒŒ์ด์ฌ์—์„œ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ, ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ์ฃผ๋กœ ๋‹ค๋ฅธ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ(ํŠœํ”Œ, ๋”•์…”๋„ˆ๋ฆฌ)๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— for ๋ฌธ์œผ๋กœ ์š”์†Œ๋“ค์„ ์ˆœํšŒํ•˜๋ฉฐ if ๋ฌธ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์„ ์„œ์ •ํ•˜ ๊ธฐ๋„ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ์ค‘ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ํ•™์Šตํ•ด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. [ํ‘œํ˜„์‹ for ๋ณ€์ˆ˜ in ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด(๋ฒ”์œ„) if ์กฐ๊ฑด] ๋จผ์ € for ๋ณ€์ˆ˜ in ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด(๋ฒ”์œ„)๊นŒ์ง€๋Š” for ๋ฌธ๊ณผ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ณตํ•  ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๊ณ  ํ•ด๋‹น ๋ฒ”์œ„์—์„œ ํ•˜๋‚˜์”ฉ ์š”์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์™€์„œ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ ๊ฐ€์ง€๊ณ  ์˜จ ๋ณ€์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์–ด๋–ป๊ฒŒ ์ €์žฅํ• ์ง€ ํ‘œํ˜„์‹์— ๋”ฐ๋ผ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ ๋‹ค์Œ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๊ณผ์ •์„ ์ง€์ •ํ•œ ๋ฒ”์œ„๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋’ค์— if๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์กฐ๊ฑด์ด ์ฐธ(True)์ผ ๊ฒฝ์šฐ์—๋งŒ ํ‘œํ˜„์‹์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํŠน์ •ํ•œ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜์ง€ ์•Š์„ ๋•Œ๋Š” if ๋ฌธ์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ๋ณธ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. numbers = [x for x in range(10)] print(numbers) # ๊ฒฐ๊ด๊ฐ’ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range(10)์œผ๋กœ 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ˆซ์ž ๋ฒ”์œ„ ์•ˆ์—์„œ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๊ฐ€์ง€๊ณ  ์™€์„œ ๋ณ€์ˆ˜ x์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ‘œํ˜„์‹์— ๋ณ„๋‹ค๋ฅธ ์—ฐ์‚ฐ ์—†์ด x ๊ทธ๋Œ€๋กœ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜๋ผ๊ณ  ๋ช…๋ นํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ๋ณ€์ˆ˜๊ฐ€ ๊ทธ๋Œ€๋กœ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์œ„์™€ ๋™์ผํ•œ ์ฝ”๋“œ์—์„œ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜์—ฌ 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘ ์ง์ˆ˜๋งŒ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. even_numbers = [x for x in range(10) if x % 2 == 0] print(even_numbers) # ๊ฒฐ๊ด๊ฐ’ [0, 2, 4, 6, 8] ์—ฌ๊ธฐ์„œ๋Š” if ๋’ค์— ์กฐ๊ฑด์œผ๋กœ x % 2 == 0์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ x๋ฅผ 2๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ 0์ธ ๊ฒฝ์šฐ, ์ฆ‰, 2์˜ ๋ฐฐ์ˆ˜์ผ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ€์ง€๊ณ  ์˜ค๋„๋ก ํ•˜์—ฌ ์ง์ˆ˜๋งŒ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ณ€์ˆ˜๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ํ‘œํ˜„์‹ ๋ถ€๋ถ„์—์„œ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์ด๋‚˜ ์ฒ˜๋ฆฌ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํ‘œํ˜„์‹์— ์—ฐ์‚ฐ์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. squared_numbers = [x**2 for x in range(10)] print(squared_numbers) # ๊ฒฐ๊ด๊ฐ’ [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] ์—ฌ๊ธฐ์„œ๋Š” ๋ณ€์ˆ˜ x์— ์ €์žฅ๋œ ์ˆซ์ž๋ฅผ ์ œ๊ณฑํ•˜์—ฌ ๊ทธ ๊ฐ’์„ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ์‚ฐ์ˆ  ์—ฐ์‚ฐ ์™ธ์—๋„ ํ•จ์ˆ˜ ๋“ฑ์„ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋„๋ก ๋ช…๋ นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  for ๋ฌธ๊ณผ if ๋ฌธ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ค‘์ฒฉํ•˜์—ฌ ๋” ๋ณต์žกํ•œ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๋„ˆ๋ฌด ๋ณต์žกํ•œ ๋กœ์ง์„ ํฌํ•จํ•˜๊ฒŒ ๋˜๋ฉด ์˜คํžˆ๋ ค ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. while ๋ฌธ ์ด๋ฒˆ์—๋Š” ๋ฐ˜๋ณต๋ฌธ ์ค‘ while ๋ฌธ์„ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. while ๋ฌธ์€ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ˜๋ณต์„ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ฑด๊ณผ ๋ฐ˜๋ณตํ•  ์ž‘์—…์„ ์ž…๋ ฅํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. while ๋ฌธ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. while ์กฐ๊ฑด: ๋ฐ˜๋ณต์„ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ๋ธ”๋ก while ๋’ค์— ์ฐธ/๊ฑฐ์ง“ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์„ ์ž…๋ ฅํ•˜๊ณ  ์ฝœ๋ก (:)์„ ์ž…๋ ฅํ•œ ๋‹ค์Œ ์ค„๋ฐ”๊ฟˆ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ์กฐ๊ฑด๋ฌธ์ด๋‚˜ ๋ฐ˜๋ณต ๋ฌธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ณต๋ฐฑ ๋„ค ์นธ ๋˜๋Š” ํƒญ(Tab) ํ•œ ์นธ์„ ๋„์–ด ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•œ ๋‹ค์Œ ๋ฐ˜๋ณต์„ ์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” while ๋ฌธ์„ ์‚ฌ์šฉํ•œ ๋ฐ˜๋ณต ๋ฌธ์˜ ์˜ˆ์ž…๋‹ˆ๋‹ค. count = 0 while count < 5: print(count) count = count + 1 # ๊ฒฐ๊ด๊ฐ’ 1 3 count = 0์œผ๋กœ ๊ฐ’์ด 0์ธ count ๋ณ€์ˆ˜๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  while ๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐ€๋Š”๋ฐ ์กฐ๊ฑด์ด count < 5๋กœ count ๋ณ€์ˆ˜์˜ ๊ฐ’์ด 5 ๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ์—๋งŒ ๋‹ค์Œ ์ค„์˜ ์ฝ”๋“œ ๋ธ”๋ก์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” count์˜ ๊ฐ’์ด 0์ด๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ฑด์ด ์ฐธ(True)์ด ๋˜์–ด count๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์นด์šดํŠธ์˜ ๊ฐ’์— 1์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ ์‹คํ–‰์ด ๋๋‚œ ํ›„ ๋‹ค์‹œ while ๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค. count์— 1์ด ์ถ”๊ฐ€๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ count = 1์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ count ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋‹ค์‹œ ์กฐ๊ฑด์— ๋งž๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋„ ์กฐ๊ฑด์ด ์ฐธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ณ„์†ํ•ด์„œ ๋ฐ˜๋ณต๋ฌธ์ด ์‹คํ–‰๋˜๋‹ค๊ฐ€ count์˜ ๊ฐ’์ด 5๊ฐ€ ๋˜๋ฉด ์กฐ๊ฑด์ด ๊ฑฐ์ง“(False)์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— while ๋ฌธ์„ ๋น ์ ธ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. while ๋ฌธ๋„ ๋™์ž‘์„ ๋ฐ˜๋ณตํ•˜๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— for ๋ฌธ์œผ๋กœ ๋งŒ๋“  ๋ฐ˜๋ณต๋ฌธ์„ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ while ๋ฌธ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋ฐ˜๋Œ€๋กœ while ๋ฌธ์ด ์‚ฌ์šฉ๋œ ๋ฐ˜๋ณต๋ฌธ์„ ์ˆ˜์ •ํ•˜์—ฌ for ๋ฌธ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ for ๋ฌธ์€ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•˜๊ณ  while ๋ฌธ์€ ์กฐ๊ฑด์„ ์ง€์ •ํ•ด์•ผ ํ•˜๋Š” ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ํŠน์ • ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ์‰ฌ์šธ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต while ๋ฌธ์€ ์กฐ๊ฑด์˜ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ๋ฐ˜๋ณต ํšŸ์ˆ˜๊ฐ€ ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋˜๋Š” ๊ฒฝ์šฐ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜๊ฐ€ ๋ฏธ๋ฆฌ ์•Œ๋ ค์ ธ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” for ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌดํ•œ ๋ฐ˜๋ณต while ๋ฌธ์—์„œ ๋งŒ์•ฝ ์กฐ๊ฑด์ด ํ•ญ์ƒ ์ฐธ์ผ ๊ฒฝ์šฐ์—๋Š” ๋ฌดํ•œ ๋ฐ˜๋ณต์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ๋’ค์—์„œ ๋ฐฐ์šธ 'break'๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์ผ ๊ฒฝ์šฐ ์ข…๋ฃŒ๋˜๊ฒŒ ํ•˜๊ฑฐ๋‚˜ ํ”„๋กœ๊ทธ๋žจ์ด ์ˆ˜๋™์œผ๋กœ ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ํ•ด๋‹น ์ž‘์—…์„ ๊ณ„์† ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜๋™์œผ๋กœ ์ข…๋ฃŒํ•˜๋ ค๋ฉด ํŒŒ์ด์ฌ ์ฝ˜์†”์—์„œ ํ‚ค๋ณด๋“œ Ctrl ๊ณผ C๋ฅผ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ๋Š” ์ƒ๋‹จ์˜ Kernel ๋ฉ”๋‰ด - Interrupt๋ฅผ ํด๋ฆญํ•˜๊ฑฐ๋‚˜ ํˆด ๋ฐ”์—์„œ ์ •์ง€ ๋ฒ„ํŠผ(โ– )์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋ฌดํ•œ ๋ฐ˜๋ณต์„ ์‹คํ–‰ํ•˜๋Š” while ๋ฌธ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. while True: print("๋ฌดํ•œ ๋ฐ˜๋ณต") while ๋ฌธ์€ ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ์— ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š”๋ฐ ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ์กฐ๊ฑด ์ž์ฒด๊ฐ€ ์ฐธ(True)์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด ์กฐ๊ฑด์€ ์ ˆ๋Œ€ ๊ฑฐ์ง“(False)์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฑฐ์ง“์ด์–ด์•ผ ๋ฐ˜๋ณต๋ฌธ์ด ์ข…๋ฃŒ๋˜๋Š”๋ฐ ํ•ญ์ƒ ์ฐธ์ธ ์กฐ๊ฑด์ด๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ while ๋ฌธ์€ ๋ฌดํ•œ ๋ฐ˜๋ณต์— ๋น ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด "๋ฌดํ•œ ๋ฐ˜๋ณต"์ด๋ผ๋Š” ๋ฌธ์ž์—ด์ด ๊ณ„์†ํ•ด์„œ ํ™”๋ฉด์— ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ ์ž์ฒด์— break๋กœ ๋ฐ˜๋ณต์„ ์ข…๋ฃŒํ•˜๋Š” ํŠน์ • ์กฐ๊ฑด์„ ์„ค์ •ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋œ ๊ฒฝ์šฐ์—๋Š” ์ˆ˜๋™์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์ค‘์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. break์™€ continue ๋ฐ˜๋ณต๋ฌธ์—์„œ ํ๋ฆ„์„ ๋ฐ”๊ฟ”์ฃผ๋Š” break์™€ continue์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ while ๋ฌธ์˜ ๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ๋ฉˆ์ถ”๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ break๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๊ณ  ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค. break๋Š” ๋ฐ˜๋ณต๋ฌธ์—์„œ ๋ฐ˜๋ณต ์ž‘์—…์„ ์ข…๋ฃŒ์‹œ์ผœ์„œ ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜์˜ค๊ฒŒ ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๊ณ  continue๋Š” ๋ฐ˜๋ณต๋ฌธ์—์„œ ํ˜„์žฌ ์ง„ํ–‰ ์ค‘์ธ ํšŒ์ฐจ๋ฅผ ๋ฉˆ์ถ”๊ณ  ๋‹ค์Œ ํšŒ์ฐจ์˜ ๋ฐ˜๋ณต์œผ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํŠน์ • ์กฐ๊ฑด์—์„œ ๋ฐ˜๋ณต์„ ๋ฉˆ์ถ”๊ฒŒ ํ•˜๋ ค๋ฉด break๋ฅผ, ํŠน์ • ์กฐ๊ฑด์—์„œ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  ๋‹ค์Œ ๋ฐ˜๋ณต์œผ๋กœ ๋„˜์–ด๊ฐ€๋ ค๋ฉด continue๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. for i in range(10): if i == 3: continue if i == 7: break print(i) # ๊ฒฐ๊ด๊ฐ’ 1 4 6 ๋จผ์ € ๋ฐ˜๋ณต๋ฌธ ์ž์ฒด๋Š” 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๊ฐ€์ง€๊ณ  ์™€์„œ i์— ๋Œ€์ž…ํ•œ ๋‹ค์Œ, ์•„๋ž˜์— ๋“ค์—ฌ์“ฐ๊ธฐ ๋˜์–ด ์žˆ๋Š” ์ฝ”๋“œ ๋ธ”๋ก์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ ๋ธ”๋ก์— ์ง„์ž…ํ•˜๋ฉด ์‹ค์ œ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ ์•ž์— if ๋ฌธ์œผ๋กœ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜์˜€๋Š”๋ฐ, ์ฒซ ๋ฒˆ์งธ if ๋ฌธ์—์„œ๋Š” if i == 3์ด๋ฉด continue๋ผ๊ณ  ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” i๊ฐ€ 3์ผ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ if ๋ฌธ๊ณผ print() ํ•จ์ˆ˜๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š๊ณ  ๋‹ค์Œ ํšŒ์ฐจ๋กœ ๋„˜์–ด๊ฐ€๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. continue๋Š” if ๋ฌธ ๋‹ค์Œ ๋ผ์ธ์— ํ•œ ๋ฒˆ ๋” ๋“ค์—ฌ์“ฐ๊ธฐ ๋˜์–ด ์žˆ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ํ•œ ๋ฒˆ ๋” ๋“ค์—ฌ ์“ฐ๊ธฐ๋Š” ๋ฐ”๋กœ ์œ„์˜ if ๋ฌธ์—์„œ ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ์—๋งŒ ์ˆ˜ํ–‰๋˜๋Š” ์ฝ”๋“œ๋ฅผ ๋œปํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ i๊ฐ€ 3์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” continue๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๊ทธ๋‹ค์Œ if ๋ฌธ(if i == 7)์„ ๋งŒ๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋งŒ์•ฝ i๊ฐ€ 7์ผ ๊ฒฝ์šฐ์—๋Š” break, ์ฆ‰, ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ i๊ฐ€ 3๋„ ์•„๋‹ˆ๊ณ  7๋„ ์•„๋‹ ๊ฒฝ์šฐ์—๋Š” ๋งˆ์ง€๋ง‰ ๋ผ์ธ์˜ print() ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ continue์™€ break๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋‹ค์–‘ํ•œ ์กฐ๊ฑด์—์„œ ๋ฐ˜๋ณต ๋ฌธ์˜ ํ๋ฆ„์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 02-04. ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค ํ•จ์ˆ˜ ํŒŒ์ด์ฌ์—๋Š” ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ ๋ธ”๋ก์ธ ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ์ž‘์—…์„ ํ•  ๋•Œ๋งˆ๋‹ค ์ฝ”๋“œ๋ฅผ ์ผ์ผ์ด ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์—…์„ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ์–ด ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๊ณ  ๊ตฌ์กฐ๋ฅผ ๊ฐœ์„ ํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค๋‹ˆ๋‹ค. ํ•จ์ˆ˜์—๋Š” ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋‚ด์žฅํ•จ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ง์ ‘ ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 1) ํ•จ์ˆ˜ ์ •์˜์™€ ํ˜ธ์ถœ ๋จผ์ € ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ ๋ธ”๋ก์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” def ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๊ธฐ๋ณธ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. def ํ•จ์ˆ˜ ์ด๋ฆ„(๋งค๊ฐœ๋ณ€์ˆ˜ 1, ๋งค๊ฐœ๋ณ€์ˆ˜ 2, ...): ์ฝ”๋“œ ๋ธ”๋ก return ๋ฐ˜ํ™˜๊ฐ’ def ํ‚ค์›Œ๋“œ ๋’ค์— ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•  ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ์ฃผ๋กœ ํ•ด๋‹น ํ•จ์ˆ˜์˜ ๊ธฐ๋Šฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์ด๋ฆ„์œผ๋กœ๋Š” ์˜์–ด ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์™€ ๋ฐ‘์ค„ ๊ธฐํ˜ธ(_)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ณต๋ฐฑ์€ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ •์˜ํ•  ๊ฒฝ์šฐ ํ•จ์ˆ˜ ์ด๋ฆ„์„ ์ค‘๋ณตํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ ์ •์˜๋œ ํ•จ์ˆ˜์™€ ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ๋‹ค์‹œ ์ •์˜ํ•˜๋ฉด ๊ธฐ์กด์˜ ํ•จ์ˆ˜๋ฅผ ๋ฎ์–ด์”๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์ด๋ฆ„ ๋’ค์—๋Š” () ๊ด„ํ˜ธ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ „๋‹ฌ๋  ๊ฐ’์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ์—์„œ ์ œ๊ณตํ•˜๋Š” ์‹ค์ œ ๊ฐ’์„ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ(parameter)๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋ฉฐ, ํ•จ์ˆ˜์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” ์‹ค์ œ ๊ฐ’์€ ์ธ์ž(argument) ๋˜๋Š” ์ธ์ˆ˜๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ฝ”๋“œ์— ๋”ฐ๋ผ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” ํ•„์š”ํ•œ ๋งŒํผ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ ๋’ค์—๋Š” ์ฝœ๋ก (:)์„ ์ž…๋ ฅํ•˜์—ฌ ์ค„๋ฐ”๊ฟˆ์„ ํ•˜๊ณ , ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ์‹คํ–‰๋  ์ฝ”๋“œ๋“ค์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ฝ”๋“œ ์‹คํ–‰ ํ›„์— ์–ด๋–ค ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•  ๋•Œ๋Š” ๋งˆ์ง€๋ง‰ ์ค„์— 'return ๋ฐ˜ํ™˜๊ฐ’'์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ ๋ธ”๋ก๊ณผ return ๋ฐ˜ํ™˜๊ฐ’์„ ์ž…๋ ฅํ•  ๋•Œ๋Š” ๋ชจ๋‘ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์ •์˜ํ•œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” 'ํ•จ์ˆ˜๋ช…()'์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ช…(์ธ์ž 1, ์ธ์ž 2, ์ธ์ž 3, ...) ๋งŒ์•ฝ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ธ์ž๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด ํ•จ์ˆ˜๋ช…()์˜ ๊ด„ํ˜ธ ์•ˆ์— ์ธ์ž๋ฅผ ๋„ฃ์–ด์„œ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ, ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ „๋‹ฌํ•˜๋Š” ์ธ์ž์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜์™€ ๋™์ผํ•ด์•ผ ํ•˜๋ฉฐ, ์ˆœ์„œ๋„ ์ผ์น˜์‹œ์ผœ์„œ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์—๋Š” ๊ด„ํ˜ธ ์•ˆ์— ์•„๋ฌด๊ฒƒ๋„ ์ž…๋ ฅํ•˜์ง€ ์•Š๊ณ  'ํ•จ์ˆ˜๋ช…()'์˜ ํ˜•ํƒœ๋กœ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ , ์ •์˜ํ•œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. def add(a, b): result = a + b return result sum_result = add(10, 20) # ํ•จ์ˆ˜ ํ˜ธ์ถœ print(sum_result) # ๊ฒฐ๊ด๊ฐ’ 30 ์œ„์˜ ์˜ˆ์‹œ์—์„œ add๋ผ๋Š” ์ด๋ฆ„์˜ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ a์™€ b๋ฅผ ๋ฐ›์•„์„œ ๋‘˜์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. add ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ธ์ž๋กœ 10๊ณผ 20์„ ์ „๋‹ฌํ•˜์˜€์œผ๋ฉฐ, 10๊ณผ 20์€ ๊ฐ๊ฐ ๋งค๊ฐœ๋ณ€์ˆ˜ a์™€ b์— ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. a = 10, b = 20์œผ๋กœ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ฝ”๋“œ result = a + b๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ result ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฐ˜ํ™˜๋œ ๊ฐ’์„ sum_result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ๋Š” ํ•จ์ˆ˜๋Š” ๋ฐ˜ํ™˜ ๊ฐ’์„ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์—ฌ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด return ๋’ค์— ๋ฐ˜ํ™˜๊ฐ’์„ ์‰ผํ‘œ๋กœ ๋‚˜์—ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’๋“ค์€ ํŠœํ”Œ๋กœ ๋ฌถ์—ฌ์„œ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. def calculate(x, y): sum_val = x + y diff_val = x - y return sum_val, diff_val result = calculate(10, 5) print(result) # ๊ฒฐ๊ด๊ฐ’ (15, 5) ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” calculate ํ•จ์ˆ˜๋Š” ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ sum_val๊ณผ diff_val 2๊ฐœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ •์˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐ˜ํ™˜๋œ ๊ฒฐ๊ด๊ฐ’์ด ํ• ๋‹น๋œ result๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋‘ ๋ฐ˜ํ™˜๊ฐ’ 15์™€ 5๊ฐ€ ํŠœํ”Œ๋กœ ๋ฌถ์—ฌ์„œ ๋ฐ˜ํ™˜๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฐ˜ํ™˜๋œ ๊ฒฐ๊ด๊ฐ’์„ ํ•˜๋‚˜์˜ ํŠœํ”Œ์ด ์•„๋‹ˆ๋ผ ๊ฐ๊ฐ ๋”ฐ๋กœ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ํŠœํ”Œ์„ "์–ธ ํŒจํ‚น" ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์–ธ ํŒจํ‚น(unpacking)์ด๋ž€ ํŠœํ”Œ, ๋ฆฌ์ŠคํŠธ ๋“ฑ ๋ณตํ•ฉ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์˜ ์š”์†Œ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜์— ํ•œ ๋ฒˆ์— ํ• ๋‹นํ•˜๋Š” ๊ธฐ์ˆ ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ ๋ฐ˜ํ™˜๊ฐ’์„ sum_val๊ณผ diff_val์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ๊ฐ๊ฐ ํ• ๋‹นํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. sum_val, diff_val = calculate(10, 5) print(sum_val) print(diff_val) # ๊ฒฐ๊ด๊ฐ’ 15 ์ด๋ ‡๊ฒŒ ์–ธ ํŒจํ‚น์„ ํ†ตํ•ด ๊ฐ’๋“ค์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ถ”์ถœ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ํ•จ์ˆ˜์—์„œ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๋ฐ˜ํ™˜๋œ ๊ฐ’์„ ์–ธ ํŒจํ‚น์œผ๋กœ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋Š” ๋ฐฉ์‹์€ ํŒŒ์ด์ฌ์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ธฐ๋ณธ๊ฐ’(default value)์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์„ค์ •๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ "๊ธฐ๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜(default parameter)"๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ธฐ๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋ฉด ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ํ•ด๋‹น ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ๊ธฐ๋ณธ๊ฐ’์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์— ์ธ์ž๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. def ํ•จ์ˆ˜ ์ด๋ฆ„(๋งค๊ฐœ๋ณ€์ˆ˜ 1=๊ธฐ๋ณธ๊ฐ’ 1, ๋งค๊ฐœ๋ณ€์ˆ˜ 2=๊ธฐ๋ณธ๊ฐ’ 2, ...): ์ฝ”๋“œ ๋ธ”๋ก return ๋ฐ˜ํ™˜๊ฐ’ ๊ธฐ๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. def example(x, y=5): print(x, y) example(x=10) # ๊ฒฐ๊ด๊ฐ’ 10 20 ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ ํ•จ์ˆ˜ example์€ x์™€ y ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉฐ, ๊ทธ์ค‘ y๋Š” ๊ธฐ๋ณธ๊ฐ’์ด 5๋กœ ์ง€์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ example(x=10)์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜ y์— ํ• ๋‹นํ•  ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์•„๋„ y๋Š” ๊ธฐ๋ณธ๊ฐ’ 5๋กœ ์ž…๋ ฅ๋˜์–ด ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ค‘ ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๊ธฐ๋ณธ๊ฐ’์ด ์—†๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ˜ผํ•ฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€ ๊ธฐ๋ณธ๊ฐ’์ด ์—†๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ณด๋‹ค ํ•ญ์ƒ ์•ž์— ์œ„์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ธฐ๋ณธ๊ฐ’์ด ์—†๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๋ฐ˜๋“œ์‹œ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ, ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์— ์ „๋‹ฌ๋ฐ›์€ ์ธ์ž์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ์ ์œผ๋ฉด ์•ž์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ž…๋ ฅ๋˜๋ฉฐ ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜์—๋Š” ๊ธฐ๋ณธ๊ฐ’์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜์— ์ธ์ž๋ฅผ ์ž…๋ ฅํ•  ๋•Œ ๊ทธ๋ƒฅ ๊ฐ’๋งŒ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋งค ๊ฐœ๋ณ€ ์ˆ˜๋ช…์„ ์ง€์ •ํ•˜์—ฌ ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ์‹œ์—์„œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. def example(x, y=5, z=7): print(x, y, z) example(10, z=6) #๊ฒฐ๊ด๊ฐ’ 10 5 6 ์œ„์˜ ์ฝ”๋“œ์—์„œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ „๋‹ฌํ•œ ์ฒซ ๋ฒˆ์งธ ์ธ์ž 10์€ ๋งค๊ฐœ๋ณ€์ˆ˜ x์— ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  y๋Š” ๊ธฐ๋ณธ๊ฐ’์ด ํ• ๋‹น๋˜๊ณ , z์—๋Š” ์ „๋‹ฌํ•œ 6์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์œ„์˜ ์˜ˆ์‹œ์—์„œ example(10, 6)์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜ z๊ฐ€ ์•„๋‹Œ y์— 6์ด ํ• ๋‹น๋˜๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜ z์—๋Š” ๊ธฐ๋ณธ๊ฐ’์ธ 7์ด ํ• ๋‹น๋˜์—ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ธ์ž๋ฅผ ์ˆœ์„œ์™€ ์ƒ๊ด€์—†์ด ์ž…๋ ฅํ•  ๋•Œ๋‚˜ ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๋Š” ํŠน์ • ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋‹ค์Œ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ธ์ž๋ฅผ ์ „๋‹ฌํ•  ๋•Œ์—๋Š” ๋งค ๊ฐœ๋ณ€ ์ˆ˜๋ช…์„ ์ง€์ •ํ•˜์—ฌ ์ธ์ž๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„์˜ ์˜ˆ์‹œ ์ฝ”๋“œ(example(10, z=6))์—์„œ์ฒ˜๋Ÿผ ์ธ์ž๋กœ ๊ฐ’๋งŒ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹(10)๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ์‹(z=6)์„ ํ˜ผํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๊ฐ’๋งŒ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ์‹๋ณด๋‹ค ์•ž์— ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ง€์—ญ๋ณ€์ˆ˜์™€ ์ „์—ญ๋ณ€์ˆ˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•ด๋‹น ๋ณ€์ˆ˜๋กœ ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ƒ์„ฑ๋œ ๋ณ€์ˆ˜๋ฅผ ์ง€์—ญ๋ณ€์ˆ˜(local variable)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์™€ ๋ฐ˜๋Œ€๋˜๋Š” ๊ฐœ๋…์œผ๋กœ ํ•จ์ˆ˜ ๋ฐ–์—์„œ ์ƒ์„ฑ๋œ ๋ณ€์ˆ˜๋Š” ์ „์—ญ๋ณ€์ˆ˜(global variable)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ •์˜๋œ ๋ณ€์ˆ˜(์ง€์—ญ ๋ณ€์ˆ˜)๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋™์•ˆ ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ•จ์ˆ˜ ์™ธ๋ถ€์—์„œ๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋™์•ˆ๋งŒ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์ง€์—ญ๋ณ€์ˆ˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ์ „์—ญ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ์•ˆ์ด๋‚˜ ๋ฐ– ์–ด๋””์„œ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋ฉ”๋ชจ๋ฆฌ์— ๋‚จ์•„์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์ง€์—ญ๋ณ€์ˆ˜์™€ ์ „์—ญ๋ณ€์ˆ˜์˜ ๊ฐœ๋…์„ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. x = 10 # ์ „์—ญ๋ณ€์ˆ˜ def my_function(): y = 5 # ์ง€์—ญ๋ณ€์ˆ˜ print("์ง€์—ญ๋ณ€์ˆ˜ y =", y) print("์ „์—ญ๋ณ€์ˆ˜ x =", x) my_function() print(y) # ๊ฒฐ๊ด๊ฐ’ ์ง€์—ญ๋ณ€์ˆ˜ y = 5 ์ „์—ญ๋ณ€์ˆ˜ x = 10 --------------------------------------------------- NameError: name 'y' is not defined ์œ„์˜ ์ฝ”๋“œ์—์„œ x = 10์€ def๋กœ ์ •์˜ํ•œ my_function() ํ•จ์ˆ˜์˜ ๋ฐ–์—์„œ ์ƒ์„ฑํ•œ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ณ€์ˆ˜ x๋Š” ์ „์—ญ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด์™€ ๋‹ฌ๋ฆฌ y = 5๋Š” my_function() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ๊ทธ ์•ˆ์—์„œ ์ƒ์„ฑ๋œ ๋ณ€์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜ y๋Š” ์ง€์—ญ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. my_function() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ๋Š” print๋กœ ์ง€์—ญ๋ณ€์ˆ˜ y์™€ ์ „์—ญ๋ณ€์ˆ˜ x ๋ชจ๋‘ ์ถœ๋ ฅํ•˜์—ฌ ์ง€์—ญ๋ณ€์ˆ˜ y = 5์™€ ์ „์—ญ๋ณ€์ˆ˜ x = 10์„ ๋ฐ˜ํ™˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋‹ค์Œ ๋ผ์ธ์—์„œ ์‹คํ–‰ํ•œ ์ฝ”๋“œ print(y)๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  ํ•จ์ˆ˜์˜ ๋ฐ–์—์„œ ์ง€์—ญ๋ณ€์ˆ˜ y์— ์ ‘๊ทผํ•˜๋ ค๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์œ„์—์„œ ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋œ ํ›„ ์ด๋ฏธ ์ง€์—ญ๋ณ€์ˆ˜ y = 5๋Š” ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ง€์›Œ์กŒ๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ์ด์ฌ์— ๋ณ€์ˆ˜ y๋Š” ์กด์žฌํ•˜์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋กœ ์ธํ•ด print()๋กœ y ๋ณ€์ˆ˜ ์ถœ๋ ฅ์„ ์‹œ๋„ํ•˜๋ฉด ์˜ค๋ฅ˜(Name Error)๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ „์—ญ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ์•ˆ์—์„œ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋งŒ์•ฝ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ „์—ญ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด global ํ‚ค์›Œ๋“œ(global ์ „์—ญ ๋ณ€์ˆ˜๋ช…)๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ global์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ „์—ญ ๋ณ€์ˆ˜๋ช…์— ๋‹ค๋ฅธ ๊ฐ’์„ ํ• ๋‹นํ•˜๋ฉด, ํŒŒ์ด์ฌ์€ ๊ธฐ์กด์˜ ์ „์—ญ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ ๊ฒƒ์ด ์•„๋‹Œ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ƒˆ๋กœ์šด ์ง€์—ญ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์‹œ์—์„œ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. x = 10 y = 5 def update_x(): global x x = 20 y = 20 print("ํ•จ์ˆ˜ ์•ˆ์—์„œ ํ˜ธ์ถœํ•œ x์™€ y ๊ฐ’:", x, y) update_x() print("ํ•จ์ˆ˜ ๋ฐ–์—์„œ ํ˜ธ์ถœํ•œ x์™€ y ๊ฐ’:", x, y) # ๊ฒฐ๊ด๊ฐ’ ํ•จ์ˆ˜ ์•ˆ์—์„œ ํ˜ธ์ถœํ•œ x์™€ y ๊ฐ’: 20 20 ํ•จ์ˆ˜ ๋ฐ–์—์„œ ํ˜ธ์ถœํ•œ x์™€ y ๊ฐ’: 20 5 ์œ„์˜ ์ฝ”๋“œ์—์„œ x์™€ y ๋ณ€์ˆ˜ ๋ชจ๋‘ ํ•จ์ˆ˜ update_x()์˜ ๋ฐ–์—์„œ ์ƒ์„ฑ๋œ ์ „์—ญ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. update_x() ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋‘ ์ „์—ญ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด x์™€ y ๋ณ€์ˆ˜์— ๋ชจ๋‘ 20์„ ํ• ๋‹นํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋ณ€์ˆ˜ x์— ๋Œ€ํ•ด์„œ๋Š” ํ•จ์ˆ˜ ์•ˆ์—์„œ global ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ global x๋กœ ์ „์—ญ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด ์ฃผ์—ˆ์ง€๋งŒ ๋ณ€์ˆ˜ y์— ๋Œ€ํ•ด์„œ๋Š” global๋กœ ์„ ์–ธํ•ด ์ฃผ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋กœ ์ธํ•ด ์ „์—ญ๋ณ€์ˆ˜ x์˜ ๊ฐ’์€ 20์œผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์ง€๋งŒ ์ „์—ญ๋ณ€์ˆ˜ y์˜ ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  y = 20์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ํ•จ์ˆ˜ ์•ˆ์— ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•จ์ˆ˜ ์•ˆ์—์„œ y ๊ฐ’์„ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ๋Š” ์ง€์—ญ๋ณ€์ˆ˜ y์˜ ๊ฐ’์ธ 20์ด ์ถœ๋ ฅ๋˜์—ˆ๊ณ , ํ•จ์ˆ˜ ๋ฐ–์—์„œ y ๊ฐ’์„ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ๋Š” ๊ธฐ์กด ์ „์—ญ๋ณ€์ˆ˜ y์— ํ• ๋‹น๋œ ๊ฐ’ 5๊ฐ€ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ „์—ญ๋ณ€์ˆ˜์˜ ์‚ฌ์šฉ์€ ์ฝ”๋“œ์˜ ๋ณต์žก์„ฑ์„ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ, ์˜ˆ๊ธฐ์น˜ ์•Š์€ ์˜ค๋ฅ˜์˜ ์›์ธ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๋ณ€์ˆ˜์ผ ๊ฒฝ์šฐ์—๋Š” ์ „์—ญ๋ณ€์ˆ˜ ๋Œ€์‹  ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜์—ฌ ์ง€์—ญ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. 2) ๋‚ด์žฅํ•จ์ˆ˜ ํŒŒ์ด์ฌ์—๋Š” ์ด๋ฏธ ํŠน์ • ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์ •์˜๋œ ํ•จ์ˆ˜๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋‚ด์žฅํ•จ์ˆ˜(Built-in function)๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ด์™€ ๊ฐ™์€ ๋‚ด์žฅํ•จ์ˆ˜๋Š” ๋”ฐ๋กœ ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ฌ ํ•„์š” ์—†์ด ํŒŒ์ด์ฌ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—๋Š” ์ˆ˜๋งŽ์€ ๋‚ด์žฅํ•จ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉฐ, ์ด๋Ÿฌํ•œ ๋‚ด์žฅํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž‘์—…์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค. ์ด๋ฏธ ์šฐ๋ฆฌ๊ฐ€ ์•ž์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ print()๋‚˜ len(), type()๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๋“ค์ด ๋ฐ”๋กœ ํŒŒ์ด์ฌ ๋‚ด์žฅํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋‚ด์žฅํ•จ์ˆ˜๋“ค์„ ์ข€ ๋” ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‚ด์žฅํ•จ์ˆ˜ ๊ธฐ๋Šฅ ์˜ˆ์‹œ int() ์‹ค์ˆ˜๋‚˜ ๋ฌธ์ž์—ด(์ •์ˆ˜<NAME>)์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ int("123") float() ์ •์ˆ˜๋‚˜ ๋ฌธ์ž์—ด(์ •์ˆ˜/์‹ค์ˆ˜<NAME>)์„ ์‹ค์ˆ˜๋กœ ๋ณ€ํ™˜ float(3) str() ์ฃผ์–ด์ง„ ๊ฐ’(์ •์ˆ˜, ์‹ค์ˆ˜, ๋ฆฌ์ŠคํŠธ ๋“ฑ)์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ str(123) list() ๋ฌธ์ž์—ด, ํŠœํ”Œ, ์„ธํŠธ ๋“ฑ์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ list({1, 2, 3}) tuple() ๋ฆฌ์ŠคํŠธ, ๋ฌธ์ž์—ด, ์„ธํŠธ ๋“ฑ์„ ํŠœํ”Œ๋กœ ๋ณ€ํ™˜ tuple([1, 2, 3]) set() ๋ฆฌ์ŠคํŠธ, ๋ฌธ์ž์—ด, ํŠœํ”Œ ๋“ฑ์„ ์„ธํŠธ๋กœ ๋ณ€ํ™˜ set([1, 2, 3]) dict() ํ‚ค์™€ ๊ฐ’์ด ์Œ์œผ๋กœ ๋‚˜์—ด๋œ ํ•ญ๋ชฉ๋“ค์„ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ณ€ํ™˜ dict([('a', 1), ('b', 2)]) max() ์ฃผ์–ด์ง„ ์ž…๋ ฅ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ max(3, 1, 7, 4, 6) min() ์ฃผ์–ด์ง„ ์ž…๋ ฅ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ min(3, 1, 7, 4, 6) sum() ์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ชฉ๋ก์˜ ํ•ฉ๊ณ„๋ฅผ ๋ฐ˜ํ™˜ sum([1, 2, 3, 4, 5]) input() ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํ‚ค๋ณด๋“œ ์ž…๋ ฅ์„ ๋ฐ›์•„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ input() ์œ„์˜ ๋‚ด์žฅํ•จ์ˆ˜ ์ค‘ ๋ช‡ ๊ฐ€์ง€๋ฅผ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. list() list() ํ•จ์ˆ˜์— ํŠœํ”Œ์ด๋‚˜ ์„ธํŠธ๋ฅผ ๋„ฃ์–ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋ฌธ์ž์—ด์„ ์ธ์ž๋กœ ๋„ฃ์œผ๋ฉด ๋ฌธ์ž์—ด์— ํฌํ•จ๋œ ๋ฌธ์ž๋“ค์ด ๊ฐ๊ฐ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. lst = list("hello") print(lst) # ๊ฒฐ๊ด๊ฐ’ ['h', 'e', 'l', 'l', 'o'] ๋ฌธ์ž์—ด "hello"์˜ ๊ฐ ๋ฌธ์ž๋“ค์„ ์š”์†Œ๋กœ ํ•˜๋Š” lst ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. set() ์ด๋ฒˆ์—๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ธํŠธ๋กœ ๋ณ€ํ™˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์„ธํŠธ๋Š” ์ค‘๋ณต ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ  ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ, ๋ฌธ์ž์—ด ๋“ฑ์„ ์„ธํŠธ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ์œผ๋ฉด ๊ทธ์ค‘ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์š”์†Œ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์š”์†Œ์˜ ์ˆœ์„œ๋„ ์œ ์ง€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. set([4, 1, 2, 2, 3, 1]) # ๊ฒฐ๊ด๊ฐ’ {1, 2, 3, 4} ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ์—์„œ ์ค‘๋ณต๋œ ๊ฐ’ 1๊ณผ 2๋Š” ์„ธํŠธ๋กœ ๋ณ€ํ™˜๋  ๋•Œ ํ•˜๋‚˜์”ฉ๋งŒ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ณ€ํ™˜๋œ ์„ธํŠธ๋ฅผ ๋ณด๋ฉด ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ์™€ ์ˆœ์„œ๋„ ๋‹ค๋ฅธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. sum() ์ด๋ฒˆ์—๋Š” sum() ํ•จ์ˆ˜๋กœ ์—ฌ๋Ÿฌ ์š”์†Œ๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. numbers = [1, 2, 3, 4, 5] total_num = sum(numbers, 10) print(total_num) # ๊ฒฐ๊ด๊ฐ’ 25 sum()์€ ์ˆซ์ž ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ์„ฑ๋œ ์ž๋ฃŒ๋“ค์„ ์ธ์ž๋กœ ๋ฐ›์•„ ๋ชจ๋“  ์š”์†Œ์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ์ „๋‹ฌ๋ฐ›๋Š” ์ธ์ž๋Š” ์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ชฉ๋ก์ด๋‚˜ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž๋“ค์„ ์ง์ ‘ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋ฉด ํ•จ์ˆ˜๋Š” ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฃผ๋กœ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ๊ณผ ๊ฐ™์€ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์œ„์˜ ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ๋Š” sum() ํ•จ์ˆ˜์— ์ง์ ‘ 10์ด๋ผ๋Š” ์ˆซ์ž๋ฅผ ์ „๋‹ฌํ–ˆ๋Š”๋ฐ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. sum()์€ ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ(iterable) ๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์•ผ ํ•˜๋ฉฐ ์ฃผ๋กœ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์„ ๋„ฃ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ์„ ํƒ์ ์œผ๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด ๋‘ ๋ฒˆ์งธ ์ธ์ž๋ฅผ start๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ด ์ธ์ž๋Š” ๊ฒฐ๊ณผ ํ•ฉ๊ณ„์˜ ์‹œ์ž‘ ๊ฐ’์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ๋‘ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ 0์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์œ„์˜ ์ฝ”๋“œ์—์„œ sum(numbers, 10)์€ ๋ฆฌ์ŠคํŠธ numbers์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋”ํ•˜๋Š”๋ฐ ๊ทธ ์‹œ์ž‘ ๊ฐ’์„ 10์œผ๋กœ ์„ค์ •ํ•˜์—ฌ numbers์˜ ํ•ฉ์— 10์„ ๋”ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ sum()์˜ ์ธ์ž๋กœ ์ˆซ์ž๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์•ˆ ๋œ๋‹ค๋Š” ๊ฒƒ์€ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋งŒ์„ ์ „๋‹ฌํ•ด์•ผ ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ˆซ์ž๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์•ˆ ๋œ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฉฐ, start์˜ ๊ฐ’์œผ๋กœ๋Š” ๋‹จ์ผ ์ˆซ์ž๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. input() ๋งˆ์ง€๋ง‰์œผ๋กœ ์‚ดํŽด๋ณผ ๋‚ด์žฅํ•จ์ˆ˜๋Š” input()์ž…๋‹ˆ๋‹ค. input()์€ ํŒŒ์ด์ฌ์—์„œ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ฝ˜์†” ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์€ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋™์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์–ด, ๋Œ€ํ™”ํ˜• ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ๋Š”๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. user_input = input() ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์‚ฌ์šฉ์ž๋Š” ํ‚ค๋ณด๋“œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๊ณ , ์ž…๋ ฅ์ด ๋๋‚œ ํ›„ ํ‚ค๋ณด๋“œ์˜ Enter ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ user_input ๋ณ€์ˆ˜์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. word = input() print(f"์ž…๋ ฅํ•œ ๋ฌธ์ž๋Š” {word}์ž…๋‹ˆ๋‹ค.") ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ๋„ค๋ชจ ์ƒ์ž๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ์•„๋ž˜ ๋„ค๋ชจ ์ƒ์ž๋ฅผ ํด๋ฆญํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ ํ›„์— ํ‚ค๋ณด๋“œ์˜ Enter ํ‚ค๋ฅผ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค. ์ƒ์ž์— "๊ฐ€๋ฐฉ"์ด๋ผ๋Š” ๊ธ€์ž๋ฅผ ์ž…๋ ฅํ•œ ๋‹ค์Œ Enter ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. # ๊ฒฐ๊ด๊ฐ’ ๊ฐ€๋ฐฉ ์ž…๋ ฅํ•œ ๋ฌธ์ž๋Š” ๊ฐ€๋ฐฉ์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ๊ฐ’ "๊ฐ€๋ฐฉ"์ด ์ถœ๋ ฅ๋˜๊ณ , ๊ทธ๋‹ค์Œ์— print() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜์–ด "์ž…๋ ฅํ•œ ๋ฌธ์ž๋Š” ๊ฐ€๋ฐฉ์ž…๋‹ˆ๋‹ค."๋ผ๋Š” ๋ฌธ์ž์—ด์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ input()์˜ ๊ฒฐ๊ณผ๋กœ ์ž…๋ ฅ๊ฐ’ "๊ฐ€๋ฐฉ"์ด ์ถœ๋ ฅ๋œ ๊ฒƒ์€ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์˜ ํŠน์„ฑ ๋•Œ๋ฌธ์ด๋ฉฐ, ๋‹ค๋ฅธ ํŒŒ์ด์ฌ ํ™˜๊ฒฝ์—์„œ input() ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ž…๋ ฅ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ๋งŒ ํ•  ๋ฟ ์ž๋™์œผ๋กœ ์ถœ๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ word ๋ณ€์ˆ˜์— ์ €์žฅ๋˜๊ณ , ์ด word ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ๋‹ค์Œ ๋ผ์ธ์˜ print() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์–ด๋–ค ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. input() ํ•จ์ˆ˜๋Š” ์„ ํƒ์ ์œผ๋กœ ๋ฌธ์ž์—ด ์ธ์ž๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ „๋‹ฌํ•œ ๋ฌธ์ž์—ด์€ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ž…๋ ฅ์„ ์•ˆ๋‚ดํ•˜๋Š” ํ”„๋กฌํ”„ํŠธ๋กœ ํ™”๋ฉด์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋ฌธ์ž์—ด ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ input() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. word = input("์›ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: ") print(f"์ž…๋ ฅํ•œ ๋ฌธ์ž๋Š” {word}์ž…๋‹ˆ๋‹ค.") ์œ„์˜ ์ฝ”๋“œ์—์„œ "์›ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: "๋ผ๋Š” ํ”„๋กฌํ”„ํŠธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํ™”๋ฉด์— ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ์ด ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๊ณ  ๋„ค๋ชจ ์ƒ์ž์— ์›ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋„ค๋ชจ ์ƒ์ž์—๋„ ๋™์ผํ•˜๊ฒŒ "๊ฐ€๋ฐฉ"์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ๊ฒฐ๊ด๊ฐ’ ์›ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: ๊ฐ€๋ฐฉ ์ž…๋ ฅํ•œ ๋ฌธ์ž๋Š” ๊ฐ€๋ฐฉ์ž…๋‹ˆ๋‹ค. input()์˜ ๊ฒฐ๊ณผ๋กœ ๋ฌธ์ž์—ด ์ธ์ž์™€ ์ž…๋ ฅ๊ฐ’์ด ๋ชจ๋‘ ์ถœ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. input() ํ•จ์ˆ˜ ์‚ฌ์šฉ ์‹œ ์œ ์˜ํ•  ์ ์€, input() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ›์€ ๊ฐ’์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž์—ด ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. ์ˆซ์ž๋‚˜ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜•์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ํ›„์— ๋‹ค๋ฅธ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€ํ™˜์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ์‚ฐ์„ ํ•˜๊ธฐ ์œ„ํ•ด int()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. age = input("ํ˜„์žฌ ๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: ") age = int(age) print(f"๋‚ด๋…„ ๋‹น์‹ ์˜ ๋‚˜์ด๋Š” {age + 1}์„ธ์ž…๋‹ˆ๋‹ค.") # ๊ฒฐ๊ด๊ฐ’ (์ž…๋ ฅ๊ฐ’์ด 25์ผ ๋•Œ) ํ˜„์žฌ ๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: 25 ๋‚ด๋…„ ๋‹น์‹ ์˜ ๋‚˜์ด๋Š” 26์„ธ์ž…๋‹ˆ๋‹ค. ํด๋ž˜์Šค(Class) ์•ž์—์„œ ํ•จ์ˆ˜๋Š” ํŠน์ • ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋“ค์„ ๋ฌถ์€ ๊ฒƒ์œผ๋กœ ํ•ด๋‹น ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ํ•จ์ˆ˜๋งŒ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜์—์„œ ๋” ๋‚˜์•„๊ฐ„ ๊ฐœ๋…์œผ๋กœ ํŒŒ์ด์ฌ์—๋Š” ํด๋ž˜์Šค๋ผ๋Š” ๊ฐœ๋…์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋Š” ์ผ์ข…์˜ ์„ค๊ณ„๋„๋‚˜ ํ‹€๊ณผ ๊ฐ™์€ ๊ฒƒ์œผ๋กœ, ์ด ์„ค๊ณ„๋„๋ฅผ ํ† ๋Œ€๋กœ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” "๊ฐ์ฒด"๋ฅผ ๋งŒ๋“ค์–ด๋ƒ…๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋œ ๊ฐ ๊ฐ์ฒด๋ฅผ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ผ๊ณ  ํ•˜๋ฉฐ, ์ด๋Ÿฌํ•œ ์ธ์Šคํ„ด์Šค๋Š” ๊ณ ์œ ํ•œ ์†์„ฑ๊ฐ’์„ ๊ฐ–๊ณ  ํด๋ž˜์Šค์—์„œ ์ •์˜๋œ ๋ฉ”์„œ๋“œ๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ํ™œ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Object-Oriented Programming, OOP)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ํด๋ž˜์Šค๋ฅผ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ๊ฐ™์€ ๋™์ž‘์„ ์ฝ”๋“œ๋กœ ํ’€์–ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ฝ”๋“œ์˜ ๋ฐ˜๋ณต์„ ์ค„์ผ ์ˆ˜ ์žˆ๊ณ , ๊ทœ๋ชจ๊ฐ€ ํฌ๊ณ  ๋ณต์žกํ•œ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ดํ•ด๋„๋ฅผ ๋†’์ด๊ณ  ์œ ์ง€ ๋ณด์ˆ˜๋ฅผ ์šฉ์ดํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. 1) ํด๋ž˜์Šค ์„ ์–ธํ•˜๊ธฐ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•œ๋‹ค๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. class ํด๋ž˜์Šค๋ช…(): # ํด๋ž˜์Šค ๋ณ€์ˆ˜ (์„ ํƒ์ ) ๋ณ€์ˆ˜๋ช… = ๊ฐ’ # ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ (์ƒ์„ฑ์ž, ์„ ํƒ์ ) def __init__(self, ๋งค๊ฐœ๋ณ€์ˆ˜ 1, ๋งค๊ฐœ๋ณ€์ˆ˜ 2, ...): ์ฝ”๋“œ ๋ธ”๋ก # ๋ฉ”์„œ๋“œ (์„ ํƒ์ ) def ๋ฉ”์„œ๋“œ๋ช…(self, ๋งค๊ฐœ๋ณ€์ˆ˜ 1, ๋งค๊ฐœ๋ณ€์ˆ˜ 2, ...): ์ฝ”๋“œ ๋ธ”๋ก ... ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ๋•Œ๋Š” class ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. class ํ‚ค์›Œ๋“œ ๋’ค์— ํด๋ž˜์Šค๋ช…์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋ช…์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํŒŒ์Šค์นผ ์ผ€์ด์Šค(PascalCase)๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์Šค์นผ ์ผ€์ด์Šค๋Š” ์—ฌ๋Ÿฌ ๋‹จ์–ด๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ๋•Œ ๊ฐ ๋‹จ์–ด์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•˜๋Š” ๋ฐฉ์‹์ด๋ฉฐ ๋„์–ด์“ฐ๊ธฐ ์—†์ด ๋‹จ์–ด๋ฅผ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, my class๋ฅผ ํŒŒ์Šค์นผ ์ผ€์ด์Šค๋กœ ์ž‘์„ฑํ•˜๋ฉด MyClass๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋ช… ๋’ค์—๋Š” ์†Œ๊ด„ํ˜ธ()์™€ ์ฝœ๋ก (:)์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ž…๋ ฅํ•˜๊ณ  ์ค„๋ฐ”๊ฟˆ์„ ํ•ด์ค๋‹ˆ๋‹ค. ์ด๋•Œ ์†Œ๊ด„ํ˜ธ ์•ˆ์—๋Š” ํŠน์ • ํด๋ž˜์Šค๋‚˜ ์—ฌ๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ๋ช…์‹œํ•˜์—ฌ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ด„ํ˜ธ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค ์ƒ์†์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ๋‹ค์‹œ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋ช… ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ํ•ด๋‹น ํด๋ž˜์Šค์— ํฌํ•จ๋˜๋Š” ์ฝ”๋“œ ๋ธ”๋ก์„ ์ž‘์„ฑํ•˜๋Š”๋ฐ, ์ฝ”๋“œ ๋ธ”๋ก์€ ๋ชจ๋‘ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•˜์—ฌ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ ๋ธ”๋ก์—๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๊ฑฐ๋‚˜ ์†์„ฑ์„ ์ž…๋ ฅํ•˜๋ฉฐ, ์ด๋•Œ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ์ˆœ์„œ๋‚˜ ์†์„ฑ์˜ ์ˆœ์„œ๋Š” ์ฝ”๋“œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ(method)๋ž€ ํด๋ž˜์Šค์—์„œ ์ •์˜ํ•œ ํ•จ์ˆ˜๋กœ, ํ•ด๋‹น ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค(๊ฐ์ฒด)์™€ ์—ฐ๊ด€๋ฉ๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ทธ ๊ฐ์ฒด ๋˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ฐธ์กฐํ•˜๋Š” self๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. self๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ๊ฐ์ฒด ์ž์‹ ์„ ์ฐธ์กฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. self ๋’ค์—๋Š” ํ•„์š”์— ๋”ฐ๋ผ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค์— ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•˜๊ณ ์ž ํ•  ๋•Œ ๋ฉ”์„œ๋“œ๋ช…์„ ์ง€์ •ํ•ด์•ผ ํ•˜์ง€๋งŒ, ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ๋กœ ์ด๋ฏธ ์ด๋ฆ„์ด ์ •ํ•ด์ง„ ๋ฉ”์„œ๋“œ __init__()์ด ์žˆ์Šต๋‹ˆ๋‹ค. __init__() ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ๋กœ, ์ƒ์„ฑ์ž๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์˜ ์ดˆ๊ธฐํ™”์™€ ๊ฐ™์ด ์ธ์Šคํ„ด์Šค์˜ ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค์˜ ์˜๋„์™€ ๊ตฌ์กฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด ์ผ๋ฐ˜์ ์œผ๋กœ ํด๋ž˜์Šค์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜์—๋Š” ํด๋ž˜์Šค ๋ณ€์ˆ˜(class variable)์™€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(instance variable)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์ •์˜(ํด๋ž˜์Šค ๋ณ€์ˆ˜๋ช… = ๊ฐ’) ๋˜์ง€๋งŒ ๋ฉ”์„œ๋“œ์˜ ๋ฐ”๊นฅ์—์„œ ์„ ์–ธ๋˜๋Š” ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํด๋ž˜์Šค์— ์†ํ•˜๋Š” ๋ณ€์ˆ˜๋กœ ํ•ด๋‹น ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋“ค์ด ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ(ํด๋ž˜์Šค๋ช…. ํด๋ž˜์Šค ๋ณ€์ˆ˜๋ช…) ํ•  ์ˆ˜๋„ ์žˆ๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ(์ธ์Šคํ„ด์Šค๋ช….ํด๋ž˜์Šค ๋ณ€์ˆ˜๋ช…) ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ๋กœ ํ•ด๋‹น ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๊ฐ์ฒด๋“ค์— ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’์„ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค ์•ˆ์—์„œ๋„ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜๋˜๋Š” ๋ณ€์ˆ˜๋กœ, 'self'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜(self. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ช… = ๊ฐ’) ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ณ€์ˆ˜๋Š” ํŠน์ • ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์— ์†ํ•˜๋ฉฐ, ๊ฐ ์ธ์Šคํ„ด์Šค๋Š” ์ž์‹ ๋งŒ์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆซ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ฃผ๋กœ ๊ฐ ๊ฐ์ฒด์˜ ๊ณ ์œ ํ•œ ์ƒํƒœ๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด์„œ๋งŒ ์ ‘๊ทผ(์ธ์Šคํ„ด์Šค๋ช….์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ช…) ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. class Car: # ํด๋ž˜์Šค ๋ณ€์ˆ˜ total_cars = 0 # ์ „์ฒด ์ƒ์„ฑ๋œ ์ž๋™์ฐจ์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ํด๋ž˜์Šค ๋ณ€์ˆ˜ def __init__(self, brand, model): # ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜ # ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ self.brand = brand # ์ž๋™์ฐจ์˜ ๋ธŒ๋žœ๋“œ ์ดˆ๊ธฐํ™” self.model = model # ์ž๋™์ฐจ์˜ ๋ชจ๋ธ๋ช… ์ดˆ๊ธฐํ™” # ์ƒˆ๋กœ์šด ์ž๋™์ฐจ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ์ „์ฒด_์ž๋™์ฐจ_์ˆ˜๋ฅผ 1์”ฉ ์ฆ๊ฐ€ Car.total_cars = Car.total_cars + 1 # ๋ฉ”์„œ๋“œ def display_info(self): """์ž๋™์ฐจ์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์„œ๋“œ""" return f"์ด ์ž๋™์ฐจ๋Š” {self.brand} {self.model}์ž…๋‹ˆ๋‹ค." ์œ„์˜ ์˜ˆ์‹œ๋Š” Car๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๋จผ์ € total_cars = 0์œผ๋กœ ํด๋ž˜์Šค ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค ๋ณ€์ˆ˜๋กœ ์ „์ฒด ์ƒ์„ฑ๋œ ์ž๋™์ฐจ์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. def __init__์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ, ์„ธ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ brand์™€ model์„ ์ „๋‹ฌ๋ฐ›์•„ self.brand = brand์™€ self.model = model๋กœ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Car.total_cars = Car.total_cars + 1๋กœ ์ƒˆ๋กœ์šด ์ž๋™์ฐจ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ์ „์ฒด ์ž๋™์ฐจ ์ˆ˜๋ฅผ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์—ด๊ณผ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜๋Š” display_info() ๋ฉ”์„œ๋“œ๋„ ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 2) ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ํ™œ์šฉํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์„ ์–ธํ•œ ํด๋ž˜์Šค์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ์ฒด๋ช… = ํด๋ž˜์Šค๋ช…(์ธ์ž 1, ์ธ์ž 2, ...) ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” init ๋ฉ”์„œ๋“œ์— ์„ค์ •๋œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜์™€ ์ˆœ์„œ๊ฐ€ ์ผ์น˜ํ•˜๋Š” ์ธ์ž๋“ค์„ ๊ด„ํ˜ธ ์•ˆ์— ํ•จ๊ป˜ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค์—์„œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ ์ž…๋ ฅํ–ˆ๋˜ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜ self๋Š” ์ž…๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ init ๋ฉ”์„œ๋“œ์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๊ฑฐ๋‚˜ ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ธฐ๋ณธ๊ฐ’์ด ์„ค์ •๋˜์–ด ์žˆ๋‹ค๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šคํ™”๋ผ๊ณ ๋„ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„์—๋Š” ์ธ์Šคํ„ด์Šค์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•ด ์†์„ฑ์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•ž์—์„œ ํ•™์Šตํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด ์ธ์Šคํ„ด์Šค๋ช….์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ช…์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฉ”์„œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค๋ช….๋ฉ”์„œ๋“œ๋ช…(์ธ์ž1, ์ธ์ž 2, ...)๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ธ์ž๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•  ๋•Œ ์„ค์ •ํ–ˆ๋˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜์™€ ์ˆœ์„œ์— ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ์ธ์ž๋Š” ์ƒ๋žตํ•˜๊ณ  ๋นˆ ์†Œ๊ด„ํ˜ธ()๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ self๋Š” ์ž…๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. # ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑ car1 = Car("ํ˜„๋Œ€", "์†Œ๋‚˜ํƒ€") car2 = Car("๊ธฐ์•„", "K5") # ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ print(car1.display_info()) print(car2.display_info()) # ํด๋ž˜์Šค ๋ณ€์ˆ˜์— ์ ‘๊ทผ print(Car.total_cars) # ๊ฒฐ๊ด๊ฐ’ ์ด ์ž๋™์ฐจ๋Š” ํ˜„๋Œ€ ์†Œ๋‚˜ํƒ€์ž…๋‹ˆ๋‹ค. ์ด ์ž๋™์ฐจ๋Š” ๊ธฐ์•„ K5์ž…๋‹ˆ๋‹ค. ์•ž์—์„œ ์„ ์–ธํ–ˆ๋˜ ํด๋ž˜์Šค Car์˜ ์ธ์Šคํ„ด์Šค car1๊ณผ car2๋ฅผ ์ƒ์„ฑํ•˜๊ณ  display_info() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ํฌํ•จํ•œ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์—๋Š” print(Car.total_cars)๋กœ ํด๋ž˜์Šค ๋ณ€์ˆ˜๋„ ์ถœ๋ ฅํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. 3) ํด๋ž˜์Šค ์ƒ์† ํด๋ž˜์Šค ์ƒ์†์€ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ์˜ ํ•ต์‹ฌ ๊ฐœ๋… ์ค‘ ํ•˜๋‚˜๋กœ, ์–ด๋–ค ํด๋ž˜์Šค(๋ถ€๋ชจ ํด๋ž˜์Šค ๋˜๋Š” ์Šˆํผ ํด๋ž˜์Šค)์˜ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค๋ฅธ ํด๋ž˜์Šค(์ž์‹ ํด๋ž˜์Šค ๋˜๋Š” ์„œ๋ธŒ ํด๋ž˜์Šค)๊ฐ€ ๋ฌผ๋ ค๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์ž‘๋™ ์›๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ƒ์†์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ์†์„ ๋ฐ›์„ ๋•Œ๋Š” class ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ๋•Œ ํด๋ž˜์Šค๋ช… ๋’ค ์†Œ๊ด„ํ˜ธ() ์•ˆ์— ์ƒ์†๋ฐ›์„ ๋ถ€๋ชจ ํด๋ž˜์Šค๋ช…์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. class ๋ถ€๋ชจ ํด๋ž˜์Šค: ... class ์ž์‹ ํด๋ž˜์Šค(๋ถ€๋ชจ ํด๋ž˜์Šค): ... ์ž์‹ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์ง€๋งŒ, ํ•„์š”์— ๋”ฐ๋ผ ์ผ๋ถ€๋ฅผ ์žฌ์ •์˜(overriding) ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค์˜ ์˜ˆ์‹œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ ์‚ฌ์šฉํ•  Animal ํด๋ž˜์Šค class Animal: def __init__(self, name): self.name = name def make_sound(self): print("๋™๋ฌผ์˜ ์†Œ๋ฆฌ") # Animal ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” Dog ํด๋ž˜์Šค class Dog(Animal): def make_sound(self): # Animal ํด๋ž˜์Šค์˜ make_sound ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ print(f"{self.name}๊ฐ€ ๋ฉ๋ฉ!") # Animal ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” Cat ํด๋ž˜์Šค class Cat(Animal): pass # ๊ณ ์–‘์ด ํด๋ž˜์Šค๋Š” Animal ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์ƒ์† # ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ํ™œ์šฉ dog1 = Dog("๋งฅ์Šค") dog1.make_sound() cat1 = Cat("ํ”ผ์น˜") cat1.make_sound() # ๊ฒฐ๊ด๊ฐ’ ๋งฅ์Šค๊ฐ€ ๋ฉ๋ฉ! ๋™๋ฌผ์˜ ์†Œ๋ฆฌ ๋จผ์ € ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ ์‚ฌ์šฉํ•  Animal ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. Animal ํด๋ž˜์Šค์— ํฌํ•จ๋œ ์š”์†Œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด init ๋ฉ”์„œ๋“œ์™€ make_sound ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. init ๋ฉ”์„œ๋“œ๋กœ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ name์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , make_sound ๋ฉ”์„œ๋“œ๋Š” "๋™๋ฌผ์˜ ์†Œ๋ฆฌ"๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด Animal ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” Dog ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. Class Dog(Animal):์™€ ๊ฐ™์ด ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ๋•Œ ํด๋ž˜์Šค๋ช… ๋’ค ๊ด„ํ˜ธ์— ๋ถ€๋ชจ ํด๋ž˜์Šค๋ช…(Animal)์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด Animal ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ƒ์†๋ฐ›์€ ๋ฉ”์„œ๋“œ ์ค‘ make_sound ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜์—ฌ print()๋กœ ์ถœ๋ ฅํ•  ๋ฌธ์ž์—ด์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ Animal ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ๋˜ ๋‹ค๋ฅธ ํด๋ž˜์Šค Cat์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. Cat ํด๋ž˜์Šค๋Š” Animal ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์ƒ์†ํ•˜๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ ๋ธ”๋ก์— pass๋งŒ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ƒ์†๋ฐ›์€ ๋‘ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Dog ํด๋ž˜์Šค์˜ ๊ฐ์ฒด dog1์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ธ์ž๋กœ "๋งฅ์Šค"๋ฅผ ์ž…๋ ฅํ•˜์—ฌ init ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌ๋˜์—ˆ์œผ๋ฉฐ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ name์— "๋งฅ์Šค"๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. Cat ํด๋ž˜์Šค์˜ ๊ฐ์ฒด cat1๋„ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•œ dog1๊ณผ cat1์œผ๋กœ ๊ฐ๊ฐ make_sound ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, ์ถœ๋ ฅ๋œ ๊ฐ’์ด ๊ฐ๊ฐ ๋‹ค๋ฅธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. object ํด๋ž˜์Šค object ํด๋ž˜์Šค๋Š” ํŒŒ์ด์ฌ์—์„œ ๋ชจ๋“  ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ํŒŒ์ด์ฌ์˜ ๋ชจ๋“  ํด๋ž˜์Šค๋Š” ๋ช…์‹œ์ ์œผ๋กœ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์ง€ ์•Š๋Š” ํ•œ, ์•”์‹œ์ ์œผ๋กœ object ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด object ํด๋ž˜์Šค๋Š” ํŒŒ์ด์ฌ์˜ ๋ชจ๋“  ๊ฐ์ฒด์˜ ๊ธฐ๋ณธ์ ์ธ ๋ฉ”์„œ๋“œ๋“ค(์˜ˆ: init, str, repr ๋“ฑ)์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฌ์šฉ์ž ์ •์˜ ํด๋ž˜์Šค์—์„œ ์ด๋Ÿฌํ•œ ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜์ง€ ์•Š๋Š” ํ•œ object ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ๊ตฌํ˜„์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. class Person: # ์•”์‹œ์ ์œผ๋กœ object ํด๋ž˜์Šค๋ฅผ ์ƒ์† def __init__(self, name): self.name = name ์‚ฌ์‹ค ์œ„์˜ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. class Person(object): # ๋ช…์‹œ์ ์œผ๋กœ object ํด๋ž˜์Šค๋ฅผ ์ƒ์† def __init__(self, name): self.name = name ๊ทธ๋ ‡์ง€๋งŒ ์ง€๊ธˆ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ด์ฌ(3.x)์€ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•  ๋•Œ ๋ช…์‹œ์ ์œผ๋กœ object๋ฅผ ์ƒ์†๋ฐ›์„ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋Œ€๋ถ€๋ถ„์˜ ์ฝ”๋“œ์—์„œ object ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•œ๋‹ค๋Š” (object)๋ฅผ ์ƒ๋žตํ•˜๊ณ  class Person:๊ณผ ๊ฐ™์€<NAME>์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค๋Š” ํ›จ์”ฌ ๋” ๊นŠ๊ณ  ๋ณต์žกํ•œ ์ฃผ์ œ์ด๋ฉฐ, ๋งค์šฐ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๊ณผ ์‚ฌ์šฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ง€๊ธˆ๊นŒ์ง€ ํ•™์Šตํ•œ ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ์ ์ธ ์ดํ•ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์—…๋ฌด ์ž๋™ํ™”๋ฅผ ํ•˜๊ธฐ์—๋Š” ์ถฉ๋ถ„ํ•  ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค์— ๋Œ€ํ•ด ๋” ๊นŠ์ด ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์˜ ํŒŒ์ด์ฌ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. *ํŒŒ์ด์ฌ ๊ณต์‹ ๋ฌธ์„œ(ํ•จ์ˆ˜): https://docs.python.org/3/tutorial/controlflow.html#defining-functions *ํŒŒ์ด์ฌ ๊ณต์‹ ๋ฌธ์„œ(ํด๋ž˜์Šค): https://docs.python.org/3/tutorial/classes.html 02-05. ๋ชจ๋“ˆ, ํŒจํ‚ค์ง€, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋ชจ๋“ˆ๊ณผ ํŒจํ‚ค์ง€, ๊ทธ๋ฆฌ๊ณ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“ˆ(module) ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋“ฑ์˜ ์ฝ”๋“œ๋ฅผ ๋ชจ์•„์„œ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๋ฉด ์ง€๊ธˆ ์‹คํ–‰ํ•˜๊ณ  ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ ์™ธ์—๋„ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋„ ๋ถˆ๋Ÿฌ์™€์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์–ด๋–ค ํŠน์ •ํ•œ ์ž‘์—…์„ ํ•˜๊ธฐ ์œ„ํ•ด ๊ด€๋ จ๋œ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋“ฑ์„ ๋ชจ์•„์„œ ๋งŒ๋“ค์–ด์ง„ ํ•˜๋‚˜์˜. py ํŒŒ์ผ์„ "๋ชจ๋“ˆ"์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“ˆ์€ ํ•จ์ˆ˜, ๋ณ€์ˆ˜, ํด๋ž˜์Šค ๋“ฑ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋ฅผ ๋‹ค๋ฅธ ํŒŒ์ด์ฌ ํŒŒ์ผ์—์„œ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์šฐ๋ฆฌ๊ฐ€ ์ˆซ์ž ๊ณ„์‚ฐ๊ณผ ๊ด€๋ จ๋œ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ(๋”ํ•˜๊ธฐ, ๋นผ๊ธฐ, ๊ณฑํ•˜๊ธฐ, ๋‚˜๋ˆ„๊ธฐ)์„ ์ž์ฃผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ๋“ค์„ ๋ชจ์•„์„œ calculator.py๋ผ๋Š” ํŒŒ์ผ์— ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด calculator.py ํŒŒ์ผ์€ ๊ณ„์‚ฐ๊ธฐ(calculator) ๋ชจ๋“ˆ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“ˆ ์ƒ์„ฑํ•˜๊ธฐ ๋ชจ๋“ˆ์€ ๋ชจ๋“ˆ๋ช….py์˜<NAME>์œผ๋กœ ํŒŒ์ผ์„ ์ €์žฅํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“ˆ๋ช…์€ ์†Œ๋ฌธ์ž๋กœ ์ž‘์„ฑํ•˜๋ฉฐ ์—ฌ๋Ÿฌ ๋‹จ์–ด๋ฅผ ์—ฐ๊ฒฐํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ‘์ค„(_)์„ ์‚ฌ์šฉํ•ด ๋‹จ์–ด๋ฅผ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ํ›„์— ํ…์ŠคํŠธ ์—๋””ํ„ฐ๋‚˜ IDE์—์„œ ํ•ด๋‹น ํŒŒ์ผ์„. py ํ™•์žฅ์ž๋กœ ์ €์žฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ๋Š” ์ƒ๋‹จ์˜ 'File'๋ฉ”๋‰ด์—์„œ 'Download as' > 'Python (.py)' ์˜ต์…˜์„ ์„ ํƒํ•˜๋ฉด ํ˜„์žฌ ๋…ธํŠธ๋ถ์˜ ๋ชจ๋“  ์ฝ”๋“œ ์…€์˜ ๋‚ด์šฉ์„. py ํ™•์žฅ์ž๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # calculator.py def add(a, b): return a + b def subtract(a, b): return a - b # ... ๊ธฐํƒ€ ๋‹ค๋ฅธ ๊ณ„์‚ฐ ํ•จ์ˆ˜๋“ค ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” calculator ๋ชจ๋“ˆ์— ์—ฌ๋Ÿฌ ๊ณ„์‚ฐ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ชจ๋“ˆ์„ ๋งŒ๋“ค๋ฉด ์ดํ›„ ๋‹ค๋ฅธ ํŒŒ์ด์ฌ ์ฝ”๋“œ์—์„œ๋„ ๊ณ„์‚ฐ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ด calculator ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“ˆ์„ ๋งŒ๋“ค ๋•Œ ์œ ์˜ํ•  ์ ์€ math์™€ ๊ฐ™์ด ํŒŒ์ด์ฌ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ชจ๋“ˆ๋ช…์œผ๋กœ ๋ชจ๋“ˆ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ํ”ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•œ ๋ชจ๋“ˆ์€ import ๋ชจ๋“ˆ๋ช…์œผ๋กœ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ์ƒ์„ฑํ•œ calculator ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์™€์„œ add ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import calculator result = calculator.add(4, 55) print(result) # ๊ฒฐ๊ด๊ฐ’ 59 import calculator๋กœ ํ•ด๋‹น ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ , ์ด๋ ‡๊ฒŒ ์ž„ํฌํŠธ ํ•œ ๋ชจ๋“ˆ์— ํฌํ•จ๋œ ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค, ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ชจ๋“ˆ. ํ•จ์ˆ˜(), ๋ชจ๋“ˆ. ํด๋ž˜์Šค(), ๋ชจ๋“ˆ. ๋ณ€์ˆ˜์™€ ๊ฐ™์€<NAME>์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. import ๋ชจ๋“ˆ๋ช… from ๋ชจ๋“ˆ๋ช… import ๋ณ€์ˆ˜๋ช…/ํ•จ์ˆ˜๋ช…/ํด๋ž˜์Šค๋ช… ํ•˜๋‚˜๋Š” ์•ž์„œ ์‚ดํŽด๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ import ๋ชจ๋“ˆ๋ช…์œผ๋กœ ๋ชจ๋“ˆ ์ „์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์ด๊ณ , ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ from ๋ชจ๋“ˆ๋ช… import ๋ณ€์ˆ˜๋ช…(ํ•จ์ˆ˜๋ช…/ํด๋ž˜์Šค๋ช…)์œผ๋กœ ์ง€์ •๋œ ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋งŒ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์—๋Š” ๋ช‡ ๊ฐ€์ง€ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ๋ฅผ ๊ฐ€์ง€๊ณ  ์„ค๋ช…ํ•˜๋ฉด import calculator๋ฅผ ์‹คํ–‰ํ•˜๋ฉด calculator ๋ชจ๋“ˆ ์ „์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“ˆ์— ํฌํ•จ๋œ ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋ชจ๋‘๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  ๋ชจ๋“ˆ์— ์ •์˜๋œ ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ calculator.add(4, 55)์ฒ˜๋Ÿผ ๋ชจ๋“ˆ ์ด๋ฆ„์„ ์ ‘๋‘์‚ฌ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๋‘ ๋ฒˆ์งธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์—ฌ from calculator import add, subtract์™€ ๊ฐ™์ด ๋ถˆ๋Ÿฌ์˜ค๋ฉด ์ง€์ •๋œ ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋งŒ ํ˜„์žฌ ์ด๋ฆ„ ๊ณต๊ฐ„(namespace)์— ์ง์ ‘ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ถˆ๋Ÿฌ์˜จ ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ชจ๋“ˆ ์ด๋ฆ„์„ ์ ‘๋‘์‚ฌ๋กœ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์–ด add(4, 55)์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฝ”๋“œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•„์š”ํ•œ ํ•ญ๋ชฉ๋งŒ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ์™€ ์„ฑ๋Šฅ ์ธก๋ฉด์—์„œ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜ ํ˜ธ์ถœ ์‹œ ๋ชจ๋“ˆ์˜ ์ด๋ฆ„์„ ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ชจ๋“ˆ๊ณผ ์ด๋ฆ„์ด ์ถฉ๋Œํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์ค„์–ด๋“ค์ง€๋งŒ, ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ ์ด๋ฆ„ ๊ณต๊ฐ„์— ๋™์ผํ•œ ์ด๋ฆ„์˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‘ ๋ฒˆ์งธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ง์ ‘ ์ง€์ •ํ•œ ํ•ญ๋ชฉ ์™ธ์—๋Š” ํ•ด๋‹น ๋ชจ๋“ˆ์— ํฌํ•จ๋œ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ด๋ฆ„ ๊ณต๊ฐ„(namespace)๋Š” ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค, ๋ชจ๋“ˆ ๋“ฑ์˜ ์‹๋ณ„์ž(identifier)๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ณต๊ฐ„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ด๋ฆ„๊ณผ ๊ฐ์ฒด๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์ €์žฅ๋˜๋Š” ๊ณต๊ฐ„์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ •์˜๋œ ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜๊ฐ€ ์ €์žฅ๋˜๋Š” ์ „์—ญ ์ด๋ฆ„ ๊ณต๊ฐ„(Global Namespace), ํ•จ์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ์ •์˜๋œ ๋ณ€์ˆ˜๊ฐ€ ์ €์žฅ๋˜๋Š” ์ง€์—ญ ์ด๋ฆ„ ๊ณต๊ฐ„(Local Namespace), ํŒŒ์ด์ฌ์— ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํ•จ๋œ ๋‚ด์žฅ ํ•จ์ˆ˜์™€ ์˜ˆ์™ธ๋“ค์ด ์ €์žฅ๋˜๋Š” ๋‚ด์žฅ ์ด๋ฆ„ ๊ณต๊ฐ„(Built-in Namespace) ๋“ฑ ํŒŒ์ด์ฌ์—๋Š” ์—ฌ๋Ÿฌ ์ด๋ฆ„ ๊ณต๊ฐ„์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“ˆ์„ import ํ•˜๋ฉด ๋ชจ๋“ˆ ์ด๋ฆ„ ๊ณต๊ฐ„์ด ์ƒ๊ธฐ๋Š”๋ฐ, ๋ชจ๋“ˆ์€ ๊ฐ ๋ชจ๋“ˆ๋ณ„๋กœ ๊ฐ์ž์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜ ์ด๋ฆ„์˜ ์ถฉ๋Œ์„ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from ๋ชจ๋“ˆ๋ช… import * from ๋ชจ๋“ˆ๋ช… import ๋ณ€์ˆ˜๋ช…(ํ•จ์ˆ˜๋ช…/ํด๋ž˜์Šค๋ช…) ๋Œ€์‹ ์— from ๋ชจ๋“ˆ๋ช… import *๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ชจ๋“ˆ์— ์ •์˜๋œ ๋ชจ๋“  ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋“ฑ์„ ์ง์ ‘ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ์‹๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“ˆ ๋‚ด์˜ ๋ชจ๋“  ํ•ญ๋ชฉ์„ ๋ชจ๋“ˆ๋ช… ์—†์ด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋˜ ์ด๋ฆ„ ์ถฉ๋Œ๊ณผ ๊ฐ™์€ ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ณ , ๋ถˆํ•„์š”ํ•œ ํ•ญ๋ชฉ๊นŒ์ง€ ๋ชจ๋‘ import๋˜์–ด ๋น„ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. import ๋ชจ๋“ˆ๋ช… as ๋ณ„๋ช… from ๋ชจ๋“ˆ๋ช… import ๋ณ€์ˆ˜๋ช…/ํ•จ์ˆ˜๋ช…/ํด๋ž˜์Šค๋ช… as ๋ณ„๋ช… ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” as ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆ๋Ÿฌ์˜จ ๋ชจ๋“ˆ์— ๋‹ค๋ฅธ ์ด๋ฆ„(๋ณ„๋ช…)์„ ๋ถ™์—ฌ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. import ๋ชจ๋“ˆ๋ช… as ๋ณ„๋ช…์œผ๋กœ as ๋’ค์— ๋ณ„๋ช…์„ ๋„ฃ์–ด์ฃผ๋ฉด ํ•ด๋‹น ๋ชจ๋“ˆ์„ ๋ณ„๋ช…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from ๋ชจ๋“ˆ๋ช… import ๋ณ€์ˆ˜๋ช…/ํ•จ์ˆ˜๋ช…/ํด๋ž˜์Šค๋ช… as ๋ณ„๋ช…๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ง€์ •ํ•œ ํ•ญ๋ชฉ์„ ๋ณ„๋ช…์œผ๋กœ ๊ฐ€์ง€๊ณ  ์˜ต๋‹ˆ๋‹ค. calculator ๋ชจ๋“ˆ์˜ add ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ, import calculator as cal๋กœ ๊ฐ€์ ธ์˜ค๋ฉด cal.add(4, 55)๋กœ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. from calculator import add as cal_add๋กœ ๊ฐ€์ ธ์˜ค๋ฉด cal_add(4, 55)๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์€ ์ž„ํฌํŠธ ํ•˜๋Š” ๋ชจ๋“ˆ์˜ ์ด๋ฆ„์ด ๋„ˆ๋ฌด ๊ธธ๊ฑฐ๋‚˜ ๋ณต์žกํ•  ๋•Œ, ๋˜๋Š” ๋‹ค๋ฅธ ๋ชจ๋“ˆ๊ณผ ์ด๋ฆ„์ด ์ถฉ๋Œํ•  ์œ„ํ—˜์ด ์žˆ๊ฑฐ๋‚˜ ๋ณด๋‹ค ์ง๊ด€์ ์ธ ์ด๋ฆ„์œผ๋กœ ํ•ญ๋ชฉ์„ ์ฐธ์กฐํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ ๋ชจ๋“ˆ ์‚ฌ์šฉ ์‹œ ์œ ์˜์‚ฌํ•ญ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ๋Š” ํ•œ ๋ฒˆ import๋œ ๋ชจ๋“ˆ์„ ์ˆ˜์ •ํ•œ ํ›„์— ๋‹ค์‹œ import ํ•ด๋„ ์ˆ˜์ •๋œ ๋‚ด์šฉ์ด ๋ฐ˜์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์ด ๋ชจ๋“ˆ์„ ๋ฉ”๋ชจ๋ฆฌ์— ํ•œ ๋ฒˆ ๋กœ๋“œํ•œ ํ›„์—๋Š” ๊ฐ™์€ ์„ธ์…˜์—์„œ๋Š” ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ์ƒˆ๋กœ์šด ๋ฒ„์ „์„ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋“ˆ์˜ ์ˆ˜์ •๋œ ๋‚ด์šฉ์„ ๋ฐ˜์˜ํ•˜๋ ค๋ฉด ์ƒ๋‹จ ๋ฉ”๋‰ด์—์„œ 'Kernel > Restart'๋ฅผ ์„ ํƒํ•˜์—ฌ ์ปค๋„์„ ์žฌ์‹œ์ž‘ํ•˜๊ณ  ๋ชจ๋“ˆ์„ ๋‹ค์‹œ import ํ•˜๊ฑฐ๋‚˜ importlib ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ reload ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“ˆ์„ ๋‹ค์‹œ ๋กœ๋“œํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. importlib์˜ reload ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. from importlib import reload reload(๋ชจ๋“ˆ๋ช…) reload๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ „์ฒด ์ปค๋„์„ ์žฌ์‹œ์ž‘ํ•˜์ง€ ์•Š๊ณ ๋„ ์ˆ˜์ •๋œ ๋‚ด์šฉ์„ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ ์ค‘์— ๋ชจ๋“ˆ์„ ์ž์ฃผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” reload๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด์žฅ ๋ชจ๋“ˆ ํŒŒ์ด์ฌ์—๋Š” ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ผ๋ถ€๋กœ ํŒŒ์ด์ฌ ์„ค์น˜์™€ ํ•จ๊ป˜ ์ œ๊ณต๋˜๋Š” ๋ชจ๋“ˆ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋“ˆ์„ ๋‚ด์žฅ ๋ชจ๋“ˆ์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ์ด๋Ÿฌํ•œ ๋ชจ๋“ˆ์€ ๋ณ„๋„์˜ ์„ค์น˜ ์—†์ด import ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด์žฅ ๋ชจ๋“ˆ์€ ๋‹ค์–‘ํ•œ ํ”„๋กœ๊ทธ๋žจ์ด ์ž‘์—…์„ ์œ„ํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด datetime ๋ชจ๋“ˆ์€ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ , os ๋ชจ๋“ˆ์€ ์šด์˜์ฒด์ œ์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ํŒŒ์ด์ฌ ์—…๋ฌด ์ž๋™ํ™”๋ฅผ ์œ„ํ•ด ํ•™์Šตํ•  ๋‚ด์šฉ๋“ค์—๋„ ์—ฌ๋Ÿฌ ๋‚ด์žฅ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ดํ›„ ๊ฐ ๋‚ด์žฅ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ๊ฐ์˜ ๋ชจ๋“ˆ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€(Package) ํŒจํ‚ค์ง€๋Š” ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์„ ๋ฌถ์–ด๋†“์€ ๊ตฌ์กฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ ํŒจํ‚ค์ง€๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ(ํด๋”)์™€ ๊ทธ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‚ด์˜ ๋ชจ๋“ˆ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๋ชจ๋“ˆ๋“ค์„ ๊ณ„์ธต์ (๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ)์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋ฉฐ, ์ด๋Š” ํฐ ํ”„๋กœ์ ํŠธ๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ตฌ์กฐํ™”์— ํฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํŒŒ์ด์ฌ ์—…๋ฌด ์ž๋™ํ™”๋ฅผ ์œ„ํ•ด ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค์ง€๋Š” ์•Š์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋žตํ•˜๊ฒŒ ํŒจํ‚ค์ง€์˜ ๊ฐœ๋…์„ ์ดํ•ดํ•˜๊ณ  ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ๋ชจ๋“ˆ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋””๋ ‰ํ„ฐ๋ฆฌ๋Š” ํŒจํ‚ค์ง€๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ธฐ๋ณธ ๋‹จ์œ„๋กœ, ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—๋Š” ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ๊ณผ ์„œ๋ธŒ ๋””๋ ‰ํ„ฐ๋ฆฌ(์„œ๋ธŒ ํŒจํ‚ค์ง€)๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ํŒจํ‚ค์ง€๋กœ ์ธ์‹๋˜๊ฒŒ ํ•˜๋Š” ํŒŒ์ผ๋กœ init.py์ด๋ผ๋Š” ํŠน๋ณ„ํ•œ ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ 3.3 ์ด์ „์—๋Š” ์ด ํŒŒ์ผ์ด ์žˆ์–ด์•ผ๋งŒ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ํŒจํ‚ค์ง€๋กœ ์ธ์‹๋˜์—ˆ์ง€๋งŒ 3.3 ์ดํ›„๋กœ๋Š” init.py ํŒŒ์ผ์ด ์—†์–ด๋„ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ํŒจํ‚ค์ง€๋กœ ์ธ์‹๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ํŒŒ์ผ์— ํŒจํ‚ค์ง€ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์ด์ „ ๋ฒ„์ „๊ณผ์˜ ํ˜ธํ™˜์„ฑ์„ ์œ„ํ•ด์„œ๋„ ๋ณดํ†ต ์ด ํŒŒ์ผ์„ ํฌํ•จ์‹œํ‚ค๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€ ๊ตฌ์กฐ์˜ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. my_package/ โ”œโ”€โ”€ __init__.py โ”œโ”€โ”€ module1.py โ””โ”€โ”€ sub_package/ โ”œโ”€โ”€ __init__.py โ””โ”€โ”€ module2.py ์œ„์˜ my_package ํŒจํ‚ค์ง€๋Š” init.py์™€ module1.py๊ฐ€ ํŒจํ‚ค์ง€์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉฐ ๊ทธ ํ•˜์œ„์— sub_package ํด๋”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด sub_package ํŒจํ‚ค์ง€์—๋Š” ๋˜๋‹ค์‹œ init.py์™€ module2.py๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋‚˜ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ import ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒจํ‚ค์ง€๋‚˜ ํŒจํ‚ค์ง€ ๋‚ด์˜ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ์œ„์˜ my_package ํŒจํ‚ค์ง€์—์„œ 'module1.py'์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. def hello(): return "Hello from module1!" ๋จผ์ € ํŒจํ‚ค์ง€ ์ „์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ธฐ๋ณธ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. import ํŒจํ‚ค์ง€ ๋ช…[.ํ•˜์œ„ ํด๋”๋ช…] ์œ„์˜ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ ์˜ˆ์‹œ์—์„œ ์‚ฌ์šฉํ•œ my_package ํŒจํ‚ค์ง€๋กœ ์„ค๋ช…ํ•˜์ž๋ฉด, import my_package๋กœ ํŒจํ‚ค์ง€ ์ „์ฒด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ถˆ๋Ÿฌ์˜ค๋ฉด my_pakcage ๋‚ด์˜ ๋ชจ๋“ˆ์€ ๋ณ„๋„๋กœ ๊ฐ€์ ธ์˜ค์ง€ ์•Š๋Š” ํ•œ ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋“ˆ ๋‚ด์˜ ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค, ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ๋„ ํŒจํ‚ค์ง€๋ช…[.ํ•˜์œ„ํด๋”๋ช…].๋ชจ๋“ˆ๋ช….๋ณ€์ˆ˜๋ช…/ํ•จ์ˆ˜๋ช…/ํด๋ž˜์Šค๋ช…์˜<NAME>์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. modul1์˜ hello ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด my_package.module1.hello()์˜ ํ˜•ํƒœ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ํŒจํ‚ค์ง€ ์ „์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ๋Œ€์‹  ํŒจํ‚ค์ง€์˜ ํŠน์ • ๋ชจ๋“ˆ๋งŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. import ํŒจํ‚ค์ง€๋ช…[.ํ•˜์œ„ํด๋”๋ช…].๋ชจ๋“ˆ๋ช… ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์„ ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด import my_package.module1์œผ๋กœ module1์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ด ๋ฐฉ์‹๋„ ๋™์ผํ•˜๊ฒŒ ๋ชจ๋“ˆ์— ์žˆ๋Š” ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค, ๋ณ€์ˆ˜ ๋“ฑ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ „์ฒด ๊ฒฝ๋กœ๋ฅผ ๋ช…์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋™์ผํ•˜๊ฒŒ my_package.module1.hello()๋กœ hello ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ์ „์ฒด ๊ฒฝ๋กœ๋ฅผ ๋ช…์‹œํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์„ ์ค„์ด๋ ค๋ฉด 'from ... import ...'์˜ ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. from ํŒจํ‚ค์ง€ ๋ช…[.ํ•˜์œ„ ํด๋”๋ช…] import ๋ชจ๋“ˆ๋ช… ์ด ๋ฐฉ์‹์œผ๋กœ module1์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค๋ฉด from my_package import module1์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์œ„ ํด๋” sub_package์— ์žˆ๋Š” module2๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒฝ์šฐ์—๋Š” from my_package.sub_package import module2๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด from ... import ...<NAME>์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“ˆ์„ ์ง์ ‘์ ์œผ๋กœ ํ˜„์žฌ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋กœ ๋ถˆ๋Ÿฌ์™€์„œ module1์˜ ํ•จ์ˆ˜, ํด๋ž˜์Šค, ๋ณ€์ˆ˜ ๋“ฑ์„ ์ง์ ‘ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋“ˆ์— ํฌํ•จ๋œ ํ•ญ๋ชฉ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ชจ๋“ˆ๋ช….๋ณ€์ˆ˜๋ช…/ํ•จ์ˆ˜๋ช…/ํด๋ž˜์Šค๋ช…์œผ๋กœ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ hello ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด module1.hello()๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋ฐฉ์‹๋ณด๋‹ค๋„ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜, ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง์ ‘ ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜, ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•˜์—ฌ import ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. from ํŒจํ‚ค์ง€๋ช…[.ํ•˜์œ„ํด๋”๋ช…].๋ชจ๋“ˆ๋ช… import ๋ณ€์ˆ˜๋ช…/ํ•จ์ˆ˜๋ช…/ํด๋ž˜์Šค๋ช… ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ hello ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค๋ฉด from my_package.module1 import hello๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํŠน์ • ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜, ํด๋ž˜์Šค๋ฅผ import ํ•  ๊ฒฝ์šฐ ๋ชจ๋“ˆ๋ช…์„ ์‚ฌ์šฉํ•  ํ•„์š” ์—†์ด hello()์™€ ๊ฐ™์ด ๋ฐ”๋กœ ํ•ด๋‹น ํ•ญ๋ชฉ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ๋ชจ๋“ˆ์˜ ๋ชจ๋“  ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์˜ค๋˜ ๋ชจ๋“ˆ๋ช… ์—†์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. from ํŒจํ‚ค์ง€๋ช…[.ํ•˜์œ„ํด๋”๋ช…].๋ชจ๋“ˆ๋ช… import * ์ด์ฒ˜๋Ÿผ from ... import ...<NAME>๋„ ๋‹ค์–‘ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ from ... import ... ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ™์€ ์ด๋ฆ„์˜ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์ด๋‚˜ ํ•ญ๋ชฉ๋“ค(๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค)์ด ์ด๋ฏธ ์ž„ํฌํŠธ ๋˜์–ด ์žˆ๋‹ค๋ฉด ์ด๋ฆ„ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์‚ดํŽด๋ณธ ๋ชจ๋“  import ๋ฐฉ์‹์— as ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. import ํŒจํ‚ค์ง€ ๋ช…[.ํ•˜์œ„ ํด๋”๋ช…] as ๋ณ„๋ช… import ํŒจํ‚ค์ง€ ๋ช…[.ํ•˜์œ„ ํด๋”๋ช…].๋ชจ๋“ˆ๋ช… as ๋ณ„๋ช… from ํŒจํ‚ค์ง€ ๋ช…[.ํ•˜์œ„ ํด๋”๋ช…] import ๋ชจ๋“ˆ๋ช… as ๋ณ„๋ช… from ํŒจํ‚ค์ง€๋ช…[.ํ•˜์œ„ํด๋”๋ช…].๋ชจ๋“ˆ๋ช… import ๋ณ€์ˆ˜๋ช…/ํ•จ์ˆ˜๋ช…/ํด๋ž˜์Šค๋ช… as ๋ณ„๋ช… ํŒจํ‚ค์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด import ๋ฌธ์„ ์‚ฌ์šฉํ•  ๋•Œ ๋ช‡ ๊ฐ€์ง€ ์ฃผ์˜ํ•  ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ํŒŒ์ด์ฌ์˜ sys.path์— ํŒจํ‚ค์ง€๊ฐ€ ์œ„์น˜ํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•  ๋•Œ, ํ•ด๋‹น ๋ชจ๋“ˆ์ด ์–ด๋””์— ์œ„์น˜ํ•ด ์žˆ๋Š”์ง€ ์ฐพ๊ธฐ ์œ„ํ•ด sys.path๋ผ๋Š” ๋ณ€์ˆ˜์— ๋“ฑ๋ก๋œ ๊ฒฝ๋กœ๋“ค์„ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. sys๋Š” ํŒŒ์ด์ฌ์˜ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ•˜๋‚˜๋กœ, ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์™€ ๊ด€๋ จ๋œ ์—ฐ์‚ฐ๋“ค์„ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค. sys.path๋Š” sys ๋ชจ๋“ˆ ๋‚ด์˜ ๋ณ€์ˆ˜๋กœ, ํŒŒ์ด์ฌ์ด ๋ชจ๋“ˆ์„ ์–ด๋””์—์„œ ์ฐพ์•„์•ผ ํ•˜๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ๊ฒฝ๋กœ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ํ˜„์žฌ ์ž‘์—… ์ค‘์ธ ๋””๋ ‰ํ„ฐ๋ฆฌ(์ฆ‰, ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ)๋Š” sys.path์— ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” ํŒŒ์ด์ฌ ํŒŒ์ผ์€ ๋ฐ”๋กœ ์ž„ํฌํŠธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ์œ„์น˜์— ์žˆ๋Š” ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•˜๋ ค๋ฉด, ๊ทธ ์œ„์น˜๊ฐ€ sys.path์— ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. sys.path์— ๊ฒฝ๋กœ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” append๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ๋กœ ๋ฆฌ์ŠคํŠธ์—๋ฅผ ๊ฒฝ๋กœ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. import sys sys.path.append('/path/to/directory') ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด /path/to/directory๊ฐ€ sys.path์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•ด๋‹น ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” ํŒŒ์ด์ฌ ๋ชจ๋“ˆ์ด๋‚˜ ํŒจํ‚ค์ง€๋ฅผ ์ž„ํฌํŠธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŒจํ‚ค์ง€ ๋‚ด๋ถ€์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์„ ์ฐธ์กฐํ•  ๋•Œ๋Š” '์ƒ๋Œ€ ๊ฒฝ๋กœ'๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ž„ํฌํŠธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, moduleA์™€ moduleB ๋‘ ๊ฐœ์˜ ๋ชจ๋“ˆ์ด ๋™์ผํ•œ ํŒจํ‚ค์ง€ ์•ˆ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ moduleA์—์„œ moduleB์˜ ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, from .moduleB import ํ•จ์ˆ˜๋ช…์ฒ˜๋Ÿผ ์ (.)์„ ์‚ฌ์šฉํ•œ ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž„ํฌํŠธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ (.)์€ "ํ˜„์žฌ ํŒจํ‚ค์ง€"๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ˜„์žฌ ํŒจํ‚ค์ง€์˜ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜จ๋‹ค๋ฉด from . import ๋ชจ๋“ˆ๋ช…์ด ๋‚˜ from .๋ชจ๋“ˆ๋ช… import ํ•จ์ˆ˜๋ช…/๋ณ€์ˆ˜๋ช…/ํด๋ž˜์Šค๋ช…์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , ํ•˜์œ„ ํŒจํ‚ค์ง€์˜ ๋ชจ๋“ˆ์ด๋ผ๋ฉด from. ํ•˜์œ„ ํŒจํ‚ค์ง€ import ๋ชจ๋“ˆ๋ช… ๋˜๋Š” from .ํ•˜์œ„ํŒจํ‚ค์ง€.๋ชจ๋“ˆ๋ช… import ํ•จ์ˆ˜๋ช…/๋ณ€์ˆ˜๋ช…/ํด๋ž˜์Šค ๋ช…์˜<NAME>์œผ๋กœ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์„ ์ƒ๋Œ€ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์„ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๊ณ , ์ฝ”๋“œ๋ฅผ ๋”์šฑ ๊ตฌ์กฐํ™”ํ•˜์—ฌ ํ”„๋กœ์ ํŠธ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์ ธ๋„ ์œ ์ง€ ๋ณด์ˆ˜์™€ ํ™•์žฅ์ด ์‰ฝ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ด€๋ จ ๋ชจ๋“ˆ๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ๋ฌถ์–ด์„œ ์žฌ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๊ณผ<NAME>๊ธฐ ์‰ฝ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—๋Š” ์ „ ์„ธ๊ณ„์˜ ๋งŽ์€ ๊ฐœ๋ฐœ์ž๋“ค์ด ๋งŒ๋“  ๋‹ค์–‘ํ•œ ํŒจํ‚ค์ง€๋“ค์ด ์žˆ์œผ๋ฉฐ, ๊ณต๊ฐœ๋œ ํŒจํ‚ค์ง€๋“ค๋„ ๋งŽ์•„์„œ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด pandas๋Š” ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ฐ ์กฐ์ž‘์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํ…Œ์ด๋ธ”<NAME>์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„(DataFrame) ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๋ฉฐ, beautifulsoup4๋Š” ์›น ์Šคํฌ๋ž˜ํ•‘์„ ์œ„ํ•œ ๋„๊ตฌ๋กœ HTML๊ณผ XML์„ ํŒŒ์‹ฑ ํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์–ธ๊ธ‰ํ•œ pandas์™€ beautifulsoup์€ ์šฐ๋ฆฌ๋„ ์—…๋ฌด ์ž๋™ํ™”๋ฅผ ์œ„ํ•ด ํ•™์Šตํ•  ํŒจํ‚ค์ง€๋“ค๋กœ ๋’ค์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณต๊ฐœ ํŒจํ‚ค์ง€๋Š” ์œˆ๋„ ๋ช…๋ น์ฐฝ์—์„œ pip install ํŒจํ‚ค ์ง€๋ช…์œผ๋กœ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ! pip install ํŒจํ‚ค ์ง€๋ช…์œผ๋กœ ์„ค์น˜ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€ ์ €์žฅ์†Œ(PyPI: https://pypi.org/)์—์„œ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ํŒจํ‚ค์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(Library) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ด€๋ จ ๋ชจ๋“ˆ์ด๋‚˜ ํŒจํ‚ค์ง€๋“ค์˜ ์ง‘ํ•ฉ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ์ž‘์—…์„ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์‚ฌ์ „์— ์ •์˜๋œ ์ฝ”๋“œ์˜ ์ง‘ํ•ฉ์ฒด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ ํ•™์Šตํ•œ "ํŒจํ‚ค์ง€"์™€ "๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ"๋ผ๋Š” ์šฉ์–ด๋Š” ์ข…์ข… ํ˜ผ์šฉ๋˜์–ด ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์ด๋‚˜ ์„œ๋ธŒ ํŒจํ‚ค์ง€๋“ค์„ ํฌํ•จํ•˜๋ฉฐ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๊ตฌ์กฐํ™”ํ•œ ๊ฒƒ์„ 'ํŒจํ‚ค์ง€'๋ผ๊ณ  ํ•˜๊ณ , ๊ทธ๋Ÿฐ ํŒจํ‚ค์ง€๋ฅผ ํฌํ•จํ•œ ๋” ํฐ ์˜๋ฏธ์˜ ์ฝ”๋“œ์˜ ์ง‘ํ•ฉ์ฒด๋ฅผ '๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ'๋ผ๊ณ  ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ๋‘ ์šฉ์–ด๊ฐ€ ๊ตฌ๋ถ„ ์—†์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, requests๋Š” "HTTP ์š”์ฒญ์„ ์œ„ํ•œ ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ"๋ผ๊ณ ๋„ ์„ค๋ช…๋˜์ง€๋งŒ ๋™์‹œ์— ํŒจํ‚ค์ง€ ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์–ด "ํŒจํ‚ค์ง€"๋ผ๊ณ ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹๋„ ํŒจํ‚ค์ง€์—์„œ ํ•™์Šตํ•œ ๋‚ด์šฉ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•  ๋•Œ๋„ ๋™์ผํ•˜๊ฒŒ pip ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•˜๋ฉฐ, ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋„ import๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ from ... import ...<NAME>์œผ๋กœ ํŠน์ • ๋ชจ๋“ˆ์ด๋‚˜ ํ•จ์ˆ˜ ๋“ฑ์„ ์ง€์ •ํ•˜์—ฌ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ๋ฐฉ์‹์— ๋”ฐ๋ผ ํฌํ•จ๋œ ์š”์†Œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ด ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๊ธฐ ์ „์—๋Š” ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ณต์‹ ๋ฌธ์„œ๋‚˜ ํ™ˆํŽ˜์ด์ง€๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ํ˜ธํ™˜์„ฑ, ํ•„์š”ํ•œ ์˜์กด์„ฑ ๋“ฑ์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํŠน์ • ํŒŒ์ด์ฌ ๋ฒ„์ „์— ์ตœ์ ํ™”๋˜์–ด ์ž‘์„ฑ๋˜์—ˆ์„ ์ˆ˜๋„ ์žˆ๊ณ , ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ํฌํ•จ๋œ ์–ด๋– ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์ด ํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ณต์‹ ๋ฌธ์„œ๋‚˜ ํ™ˆํŽ˜์ด์ง€๋ฅผ ํ†ตํ•ด ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ถฉ๋ถ„ํžˆ ํ™•์ธํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ฒ„์ „์— ๋”ฐ๋ผ ๊ธฐ๋Šฅ์ด๋‚˜ ์‚ฌ์šฉ๋ฒ•์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํŠน์ • ๋ฒ„์ „์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ pip install ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ช…==๋ฒ„์ „๋ฒˆํ˜ธ<NAME>์œผ๋กœ ์„ค์น˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋ชจ๋“ˆ๊ณผ ํŒจํ‚ค์ง€, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•ด ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ์š”์•ฝํ•˜๋ฉด, ๋ชจ๋“ˆ์€ ํ•˜๋‚˜์˜ ํŒŒ์ด์ฌ ํŒŒ์ผ, ํŒจํ‚ค์ง€๋Š” ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ๋“ค์„ ๊ตฌ์กฐ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•, ๊ทธ๋ฆฌ๊ณ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํŠน์ • ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์˜ ์ง‘ํ•ฉ(๋ชจ๋“ˆ ๋˜๋Š” ํŒจํ‚ค์ง€)์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 02-06. ๊ฒฝ๋กœ ์„ค์ • ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ๊ฒฝ๋กœ์™€ ๊ด€๋ จ๋œ ํŒŒ์ด์ฌ์˜ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ๋“ค์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—…๋ฌด ์ž๋™ํ™”๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํŠน์ • ๊ฒฝ๋กœ์— ์žˆ๋Š” ํŒŒ์ผ์„ ํŒŒ์ด์ฌ์œผ๋กœ ์ฝ์–ด์˜ค๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ๊ฒฝ๋กœ์— ์ €์žฅํ•˜๋Š” ๋“ฑ ํŒŒ์ด์ฌ์— ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ์‹๋“ค์ด ์žˆ์–ด์„œ ์ƒํ™ฉ๊ณผ ์ž‘์—… ์Šคํƒ€์ผ์— ๋”ฐ๋ผ ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ ˆ๋Œ€ ๊ฒฝ๋กœ์™€ ์ƒ๋Œ€ ๊ฒฝ๋กœ ๊ฒฝ๋กœ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ธฐ ์ „์— ํŒŒ์ด์ฌ์—์„œ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์„ ๋จผ์ € ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ '02-05. ๋ชจ๋“ˆ, ํŒจํ‚ค์ง€, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ' ์ฑ•ํ„ฐ์—์„œ ์ž ๊น ์–ธ๊ธ‰ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ, os ๋ชจ๋“ˆ์€ ์šด์˜์ฒด์ œ์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ os ๋ชจ๋“ˆ์„ ํ†ตํ•ด ํŒŒ์ผ ๋ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฒฝ๋กœ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  os.path ํ•˜์œ„ ๋ชจ๋“ˆ์„ ํ†ตํ•ด ๊ฒฝ๋กœ ๊ด€๋ จ ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. os ๋ชจ๋“ˆ๊ณผ os.path๋กœ ๊ฒฝ๋กœ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ํ•จ์ˆ˜๋“ค์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. os ๋ชจ๋“ˆ์€ ํŒŒ์ด์ฌ์˜ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ชจ๋“ˆ๋กœ import os๋กœ ๋ถˆ๋Ÿฌ์™€์„œ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์˜๋ฏธ ์˜ˆ์‹œ getcwd() ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ os.getcwd() mkdir() ์ง€์ •๋œ ๊ฒฝ๋กœ์— ์ƒˆ๋กœ์šด ๋””๋ ‰ํ„ฐ๋ฆฌ(ํด๋”)๋ฅผ ์ƒ์„ฑ os.mkdir(path) exists() ์ง€์ •๋œ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ (True/False ๊ฐ’ ๋ฐ˜ํ™˜) os.path.exists(path) isdir() ์ง€์ •๋œ ๊ฒฝ๋กœ๊ฐ€ ๋””๋ ‰ํ„ฐ๋ฆฌ์ธ์ง€ ํ™•์ธ (True/False ๊ฐ’ ๋ฐ˜ํ™˜) os.path.isdir(path) isfile() ์ง€์ •๋œ ๊ฒฝ๋กœ๊ฐ€ ํŒŒ์ผ์ธ์ง€ ํ™•์ธ(True/False ๊ฐ’ ๋ฐ˜ํ™˜) os.path.isfile(path) abspath() ์ง€์ •๋œ ๊ฒฝ๋กœ์˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ๋ฐ˜ํ™˜ os.path.abspath(path) join() ์šด์˜ ์ฒด์ œ์— ๋งž๊ฒŒ ๊ฒฝ๋กœ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์ƒˆ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑ os.path.join(path1, path2) split() ๊ฒฝ๋กœ๋ฅผ ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌ(ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜) os.path.split() ์œ„์˜ ํ•จ์ˆ˜๋“ค ์ค‘ join()๊ณผ split()์„ ์กฐ๊ธˆ ๋” ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € os.path.join() ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฒฝ๋กœ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๊ฒฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์šด์˜ ์ฒด์ œ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ๋ถ„ ๋ฌธ์ž(์˜ˆ: ์œˆ๋„์—์„œ๋Š” ์—ญ์Šฌ๋ž˜์‹œ(), ๋Œ€๋ถ€๋ถ„์˜ UNIX ๊ธฐ๋ฐ˜ ์‹œ์Šคํ…œ์—์„œ๋Š” ์Šฌ๋ž˜์‹œ(/))๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฝ๋กœ๋ฅผ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ง์ ‘ ๊ฒฝ๋กœ ๊ตฌ๋ถ„ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋‹ค๋ฅธ ์šด์˜ ์ฒด์ œ์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ os.path.join()์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ์—ฌ๋Ÿฌ ํ”Œ๋žซํผ์—์„œ ์˜ˆ์ƒ๋Œ€๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. import os path1 = "mydir" path2 = "myfile.txt" # ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ๋ถ„ ๋ฌธ์ž(\)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฝ๋กœ ๊ฒฐํ•ฉ # ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์—ญ์Šฌ๋ž˜์‹œ๋ฅผ ๋‘ ๋ฒˆ ์‚ฌ์šฉ(\\) full_path = path1 + "\\" + path2 print(full_path) import os path1 = "mydir" path2 = "myfile.txt" # os.path.join()์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฝ๋กœ ๊ฒฐํ•ฉ full_path = os.path.join(path1, path2) print(full_path) # ๊ฒฐ๊ด๊ฐ’ mydir\myfile.txt ์œ„์˜ ๋‘ ์ฝ”๋“œ ๋ชจ๋‘ "mydir"๊ณผ "myfile.txt"๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ ์ฝ”๋“œ์ธ ๋ฌธ์ž์—ด์„ ์ง์ ‘ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ ์ฝ”๋“œ์ธ os.path.join()์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹ ๋ชจ๋‘ ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๊ฒŒ "mydir\myfile.txt" ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋ฌธ์ž์—ด ์ง์ ‘ ๊ฒฐํ•ฉ๊ณผ๋Š” ๋‹ฌ๋ฆฌ os.path.join()์„ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ค ์šด์˜์ฒด์ œ์—์„œ๋“  ์˜ค๋ฅ˜ ์—†์ด ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ os.path.split()์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. os.path.split() ํ•จ์ˆ˜๋Š” ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ํŒŒ์ผ๋ช…์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ์˜ ๋งˆ์ง€๋ง‰ ๊ตฌ์„ฑ ์š”์†Œ์™€ ๊ทธ ์ด์ „์˜ ๋ถ€๋ถ„์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ๋‚˜๋ˆ„์–ด ํŠœํ”Œ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜๋˜๋Š” ํŠœํ”Œ์„ ์–ธ ํŒจํ‚นํ•˜์—ฌ ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜์— ๊ฐ๊ฐ ์ €์žฅํ•˜์—ฌ ์ถ”๊ฐ€ ์ž‘์—…์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. import os path = "C:/Users/User Name/Documents/example.txt" directory, filename = os.path.split(path) print("๋””๋ ‰ํ„ฐ๋ฆฌ:", directory) print("ํŒŒ์ผ๋ช…:", filename) # ๊ฒฐ๊ด๊ฐ’ ๋””๋ ‰ํ„ฐ๋ฆฌ: C:/Users/User Name/Documents ํŒŒ์ผ๋ช…: example.txt ์ด ํ•จ์ˆ˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋ถ„์„ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ์ฃผ์–ด์ง„ ํŒŒ์ผ ๊ฒฝ๋กœ์—์„œ ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ํŒŒ์ผ๋ช…์„ ๋ถ„๋ฆฌํ•  ๋•Œ ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ํ•™์Šตํ•  '03-01. ์œˆ๋„ ํด๋” ๋ฐ ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ'์—์„œ ํŠน์ • ๊ฒฝ๋กœ์— ์žˆ๋Š” ํด๋”๋‚˜ ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ๋•Œ os ๋ชจ๋“ˆ์ด๋‚˜ os.path ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ os ๋ชจ๋“ˆ์€ ๋’ค์— ๋‹ค์‹œ ํ•œ๋ฒˆ ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2) pathlib ๋ชจ๋“ˆ pathlib์€ ํŒŒ์ด์ฌ 3.4๋ถ€ํ„ฐ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ํฌํ•จ๋œ ๋ชจ๋“ˆ๋กœ, ํŒŒ์ผ ์‹œ์Šคํ…œ ๊ฒฝ๋กœ๋ฅผ ๊ฐ์ฒด ์ง€ํ–ฅ์ ์œผ๋กœ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. os ๋ฐ os.path์™€๋Š” ๋‹ค๋ฅด๊ฒŒ, pathlib์€ ๊ฒฝ๋กœ๋ฅผ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฐ์ฒด๋กœ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ, ๊ฒฝ๋กœ์™€ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ๋” ์ง๊ด€์ ์ด๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pathlib ๋ชจ๋“ˆ์˜ ์ฃผ์š” ํด๋ž˜์Šค๋กœ Path ํด๋ž˜์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Path ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฝ๋กœ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์•„๋ž˜์™€ ๊ฐ™์ด pathlib์˜ Path ํด๋ž˜์Šค๋ฅผ import ํ•œ ํ›„ Path ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. from pathlib import Path p = Path('C:/Users/User Name/Documents/example.txt') Path ํด๋ž˜์Šค์˜ ๊ฐ์ฒด(p)๋ฅผ ์ƒ์„ฑํ•  ๋•Œ Path()์˜ ๊ด„ํ˜ธ ์•ˆ์— ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ๋กœ๋Š” ์ ˆ๋Œ€ ๊ฒฝ๋กœ์™€ ์ƒ๋Œ€ ๊ฒฝ๋กœ ๋ชจ๋‘ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ Path ํด๋ž˜์Šค์˜ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” Path ํด๋ž˜์Šค์˜ ์†์„ฑ์ž…๋‹ˆ๋‹ค. ์˜ˆ์‹œ ๋ถ€๋ถ„์—์„œ๋Š” ์œ„์—์„œ ์ƒ์„ฑํ•œ ๊ฐ์ฒด(p)๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์†์„ฑ ์˜๋ฏธ ์˜ˆ์‹œ parts ์ง€์ •๋œ ๊ฒฝ๋กœ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์š”์†Œ๋“ค์„ ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜ p.parts drive ์ง€์ •๋œ ๊ฒฝ๋กœ์˜ ๋“œ๋ผ์ด๋ธŒ ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜ p.drive root ์ง€์ •๋œ ๊ฒฝ๋กœ์˜ ๋ฃจํŠธ ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜ p.root anchor ์ง€์ •๋œ ๊ฒฝ๋กœ์˜ ๋“œ๋ผ์ด๋ธŒ์™€ ๋ฃจํŠธ ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜ p.anchor name ์ง€์ •๋œ ๊ฒฝ๋กœ์˜ ํŒŒ์ผ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ p.name suffix ์ง€์ •๋œ ๊ฒฝ๋กœ์˜ ํ™•์žฅ์ž๋ฅผ ๋ฐ˜ํ™˜ p.suffix stem ์ง€์ •๋œ ๊ฒฝ๋กœ์˜ ํŒŒ์ผ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜(ํ™•์žฅ์ž ์ œ์™ธ) p.stem ๋‹ค์Œ์€ Path ํด๋ž˜์Šค์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ์—ญ์‹œ ๋™์ผํ•˜๊ฒŒ ์œ„์—์„œ ์ƒ์„ฑํ•œ ๊ฐ์ฒด(p)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ ์˜๋ฏธ ์˜ˆ์‹œ cwd() ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ p.cwd() home() ํ™ˆ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ p.home() exists() ์ง€์ •๋œ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ (True/False ๊ฐ’ ๋ฐ˜ํ™˜) p.exists() is_dir() ์ง€์ •๋œ ๊ฒฝ๋กœ๊ฐ€ ๋””๋ ‰ํ„ฐ๋ฆฌ์ธ์ง€ ํ™•์ธ (True/False ๊ฐ’ ๋ฐ˜ํ™˜) p.is_dir() is_file() ์ง€์ •๋œ ๊ฒฝ๋กœ๊ฐ€ ํŒŒ์ผ์ธ์ง€ ํ™•์ธ(True/False ๊ฐ’ ๋ฐ˜ํ™˜) p.is_file() resolve() ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ p.resolve() joinpath() ์šด์˜ ์ฒด์ œ์— ๋งž๊ฒŒ ๊ฒฝ๋กœ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์ƒˆ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑ p.joinpath(path1, path2) rmdir() ๋นˆ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์‚ญ์ œ p.rmdir() mkdir() ์ง€์ •๋œ ๊ฒฝ๋กœ์— ์ƒˆ๋กœ์šด ๋””๋ ‰ํ„ฐ๋ฆฌ(ํด๋”)๋ฅผ ์ƒ์„ฑ p.mkdir() unlink() ํŒŒ์ผ ๋˜๋Š” ์‹ฌ๋ฒŒ๋ฆญ ๋งํฌ๋ฅผ ์‚ญ์ œ p.unlink() touch() ์ง€์ •๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋นˆ ํŒŒ์ผ์„ ์ƒ์„ฑ p.touch() ๋ช‡ ๊ฐ€์ง€ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ™์ด ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € joinpath ๋ฉ”์„œ๋“œ๋Š” ์—ฌ๋Ÿฌ ๊ฒฝ๋กœ ๊ตฌ์„ฑ์š”์†Œ๋“ค์„ ๊ฒฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์–‘ํ•œ ์šด์˜ ์ฒด์ œ์—์„œ ์ž‘๋™ํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— OS ๋ณ„๋กœ ๊ฒฝ๋กœ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฌธ์ž(/ ๋˜๋Š” )์— ๋Œ€ํ•ด ๊ฑฑ์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด os ๋ชจ๋“ˆ๊ณผ pathlib ๋ชจ๋“ˆ์€ ๋ชจ๋‘ ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ์™€ ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๊ฑฐ๋‚˜ ์œ ์‚ฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ด๋Œ์–ด๋‚ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ pathlib์€ ๊ฐ์ฒด์ง€ํ–ฅ์ ์ด๊ณ  ์ง๊ด€์ ์ธ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, os์˜ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•ด ๋” ํ’๋ถ€ํ•œ ๋ฉ”์„œ๋“œ์™€ ์†์„ฑ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์–ด ๋” ๋งŽ์€ ์ž‘์—…๋“ค์„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ) ์œˆ๋„ ๋ช…๋ น์–ด ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ๋„ ์œˆ๋„ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œˆ๋„ ๋ช…๋ น์–ด๋ž€ ์œˆ๋„ ์šด์˜ ์ฒด์ œ์—์„œ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ(Command Prompt)๋ฅผ ํ†ตํ•ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์œˆ๋„ ๋ช…๋ น์–ด ์ค‘์—๋„ ๋””๋ ‰ํ„ฐ๋ฆฌ๋‚˜ ํŒŒ์ผ, ๊ฒฝ๋กœ์™€ ๊ด€๋ จ๋œ ๊ด€๋ จ๋œ ๋ช…๋ น์–ด๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฝ๋กœ ๊ด€๋ จ ์ฃผ์š” ์œˆ๋„ ๋ช…๋ น์–ด์— ๋Œ€ํ•ด์„œ๋„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์˜ˆ์‹œ ๋ถ€๋ถ„์€ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ ์‹คํ–‰ํ•  ๋•Œ์˜ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์žˆ๊ณ , ์–ด๋–ค ๋ช…๋ น์–ด๋Š” ์—†๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ๋Š๋‚Œํ‘œ(!)๋ฅผ ๋ช…๋ น์–ด ์•ž์— ๋ถ™์—ฌ์„œ ์‹คํ–‰ํ•˜์ง€๋งŒ ์ผ๋ถ€ ๋ช…๋ น์–ด๋Š” ๋ฐ”๋กœ ๋ช…๋ น์–ด๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋ช…๋ น์–ด ์ค‘ cd ๋ช…๋ น์–ด์˜ ๊ฒฝ์šฐ, ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณ€๊ฒฝ์ด ์ž„์‹œ์ ์ด๋ฉฐ ์ง€์†๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, cd ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋ณ€๊ฒฝํ•œ ํ›„, ๊ทธ ์…€์—์„œ์˜ ์ดํ›„์˜ ๋ช…๋ น์–ด๋“ค์€ ๊ทธ ๋ณ€๊ฒฝ๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ cd๋กœ ๋ณ€๊ฒฝํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€์ ์ธ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด! cd new_directory && dir์™€ ๊ฐ™์ด ๋ชจ๋“  ๋ช…๋ น์–ด๋ฅผ ๋™์ผํ•œ ์…€์—์„œ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ์‚ดํŽด๋ณธ ๋ช…๋ น์–ด๋“ค์€ ์šด์˜ ์ฒด์ œ์— ์ข…์†์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์œˆ๋„๊ฐ€ ์•„๋‹Œ ๋ฆฌ๋ˆ…์Šค๋‚˜ macOS์—์„œ๋Š” ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์œˆ๋„ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๋‚˜ ์˜ˆ์™ธ๋ฅผ ํŒŒ์ด์ฌ์—์„œ ์ง์ ‘ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ณด์•ˆ ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŒŒ์ด์ฌ์—์„œ ์œˆ๋„ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ„ฐ๋ฆฌ, ๊ฒฝ๋กœ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์•ž์—์„œ ๋ฐฐ์šด os ๋ชจ๋“ˆ์ด๋‚˜ pathlib์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค. 02-07. ์ •๊ทœ์‹ ์ •๊ทœ ํ‘œํ˜„์‹(์ •๊ทœ์‹, Regular Expression)์ด๋ž€ ๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ ํ˜•ํƒœ์— ํŠน์ •ํ•œ ๊ทœ์น™์ด ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํŠน์ •ํ•œ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•˜๊ฑฐ๋‚˜ ์ถ”์ถœ, ์น˜ํ™˜ํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์œ„ํ•ด ๋ฌธ์ž์—ด์˜ ํŒจํ„ด์„ ์ •์˜ํ•˜๋Š” ๋„๊ตฌ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ •๊ทœ์‹์€ ํŒŒ์ด์ฌ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ํ”„๋กœ๊ทธ๋žจ ์–ธ์–ด๋‚˜ ํ…์ŠคํŠธ ํŽธ์ง‘๊ธฐ์—์„œ๋„ ๋„๋ฆฌ ํ†ต์šฉ๋˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์ •๊ทœ์‹์€ ๊ทธ ๋‚ด์šฉ์ด ๋งค์šฐ ๊ด‘๋ฒ”์œ„ํ•˜๊ณ  ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ง€๊ธˆ ์—ฌ๊ธฐ์„œ ์ •๊ทœ์‹์— ๋Œ€ํ•ด ๊นŠ์ด ํ•™์Šตํ•˜๊ธฐ๋ณด๋‹ค๋Š” ํŒŒ์ด์ฌ ์—…๋ฌด ์ž๋™ํ™”์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ์ •๊ทœ์‹๋“ค์„ ์œ„์ฃผ๋กœ ๊ฐ„๋žตํžˆ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ๋ฌธ์ž ์ •๊ทœ์‹์—์„œ๋Š” ๋ฌธ์ž์—ด์˜ ํŒจํ„ด์„ ๋งŒ๋“ค ๋•Œ ํŠน๋ณ„ํ•œ ์˜๋ฏธ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ž๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๋ฌธ์ž๋“ค์„ ๋ฉ”ํƒ€ ๋ฌธ์ž๋ผ๊ณ  ํ•˜๋ฉฐ, ์—ฌ๋Ÿฌ ๋ฉ”ํƒ€ ๋ฌธ์ž๋“ค์„ ์กฐํ•ฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ๋ฌธ์ž์—ด์˜ ํŒจํ„ด์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”ํƒ€ ๋ฌธ์ž๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ๋ฌธ์ž ์˜๋ฏธ . ํ•˜๋‚˜์˜ ๋ฌธ์ž ์ž๋ฆฟ์ˆ˜๋ฅผ ์˜๋ฏธ (๊ณต๋ฐฑ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž ๊ฐ€๋Šฅ) ^ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ํŒจํ„ด $ ๋ฌธ์ž์—ด์˜ ๋ ํŒจํ„ด * ๋ฐ”๋กœ ์•ž์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ 0๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต + ๋ฐ”๋กœ ์•ž์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ 1๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต ? ๋ฐ”๋กœ ์•ž์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ์žˆ์„ ์ˆ˜๋„ ์—†์„ ์ˆ˜๋„ ์žˆ์Œ {n} ๋ฐ”๋กœ ์•ž์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ n ๋ฒˆ ๋ฐ˜๋ณต {n, m} ๋ฐ”๋กœ ์•ž์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ์ตœ์†Œ n ๋ฒˆ ์ด์ƒ ์ตœ๋Œ€ m ๋ฒˆ ์ดํ•˜๋กœ ๋ฐ˜๋ณต () ๊ด„ํ˜ธ ์•ˆ์˜ ๋ฌธ์ž๋“ค์„ ๊ทธ๋ฃน์œผ๋กœ ์ฒ˜๋ฆฌ [] ๊ด„ํ˜ธ ์•ˆ์˜ ๋ฌธ์ž๋“ค ์ค‘ ํ•˜๋‚˜์™€ ์ผ์น˜ [^] ^๋’ค์— ์žˆ๋Š” ๊ด„ํ˜ธ ์•ˆ์˜ ๋ฌธ์ž๋“ค ์ œ์™ธ [-] -์•ž๊ณผ ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž๋“ค ์‚ฌ์ด์˜ ๋ฌธ์ž | |์•ž์˜ ๋ฌธ์ž ๋˜๋Š”(OR) |๋’ค์˜ ๋ฌธ์ž ๋ฉ”ํƒ€ ๋ฌธ์ž ์ค‘์—๋Š” ๋ฐฑ์Šฌ๋ž˜์‹œ()์™€ ์˜์–ด ์•ŒํŒŒ๋ฒณ์˜ ์กฐํ•ฉ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํŠน์ˆ˜ ๋ฌธ์ž๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ๋ฌธ์ž ์˜๋ฏธ \d ์ˆซ์ž ํ•œ์ž๋ฆฌ๋ฅผ ์˜๋ฏธ. [0-9]์™€ ๋™์ผ \D ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž ํ•œ์ž๋ฆฌ \w ๋ฌธ์ž ํ•œ์ž๋ฆฌ. ์•ŒํŒŒ๋ฒณ+์ˆซ์ž+์–ธ๋” ์Šค์ฝ”์–ด( _ ) ์ค‘ ํ•˜๋‚˜์˜ ๋ฌธ์ž. [a-zA-Z0-9_]์™€ ๋™์ผ \W ์•ŒํŒŒ๋ฒณ+์ˆซ์ž+์–ธ๋” ์Šค์ฝ”์–ด( _ )๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž ํ•œ์ž๋ฆฌ \s ๊ณต๋ฐฑ์ด๋‚˜ ํƒญ ํ•œ์ž๋ฆฌ \S ๊ณต๋ฐฑ์ด๋‚˜ ํƒญ์ด ์•„๋‹Œ ๋ฌธ์ž ํ•œ์ž๋ฆฌ ์—ฌ๊ธฐ์„œ ์‚ดํŽด๋ณธ ๋ฉ”ํƒ€ ๋ฌธ์ž๋Š” ์ผ๋ถ€์ด๋ฉฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”ํƒ€ ๋ฌธ์ž๋Š” ํ›จ์”ฌ ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”ํƒ€ ๋ฌธ์ž์— ๋Œ€ํ•ด ๋” ์ž์„ธํžˆ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ํŒŒ์ด์ฌ ๊ณต์‹ ๋ฌธ์„œ์—์„œ 're'๋ชจ๋“ˆ์— ๊ด€ํ•œ ๋ถ€๋ถ„์„ ์ฐธ๊ณ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Python ๊ณต์‹ ๋ฌธ์„œ (re ๋ชจ๋“ˆ) : https://docs.python.org/ko/3/library/re.html 're'๋ชจ๋“ˆ์„ ํ™œ์šฉํ•œ ์ •๊ทœ์‹ ์ž‘์—… ์ด์ œ ์œ„์—์„œ ์•Œ์•„๋ณธ ๋ฉ”ํƒ€ ๋ฌธ์ž๋ฅผ ํ™œ์šฉํ•œ ๊ฐ„๋‹จํ•œ ์ •๊ทœ์‹ ์ž‘์—…๋“ค์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € import๋กœ ํŒŒ์ด์ฌ์˜ ๋‚ด์žฅ ๋ชจ๋“ˆ์ธ 're'๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. import re ๋‹ค์Œ์€ 're'๋ชจ๋“ˆ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ช‡ ๊ฐ€์ง€ ํ•จ์ˆ˜๋“ค์ž…๋‹ˆ๋‹ค. re.match() 're'๋ชจ๋“ˆ์˜ match ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์ด ์ •๊ทœ์‹ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํŒจํ„ด๊ณผ ๋ฌธ์ž์—ด ์‹œ์ž‘ ๋ถ€๋ถ„์ด ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด 'None'์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. import re # ํŒจํ„ด ์ •์˜: ํ•˜๋‚˜ ์ด์ƒ์˜ ์ˆซ์ž์— ์ผ์น˜ pattern = r"\d+" # ๊ฒ€์‚ฌํ•  ๋ฌธ์ž์—ด string = "123abc" # ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘๋ถ€ํ„ฐ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ฐพ๊ธฐ result = re.match(pattern, string) # ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์œผ๋ฉด ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅ if result: print("์ผ์น˜: ", result.group()) else: print("๋ถˆ์ผ์น˜") # ๊ฒฐ๊ด๊ฐ’ ์ผ์น˜: 123 ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ •์˜๋œ ์ •๊ทœ์‹ ํŒจํ„ด์€ "\d+"๋กœ ํ•˜๋‚˜ ์ด์ƒ์˜ ์ˆซ์ž์— ์ผ์น˜ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. re.match()๋กœ ๋ฌธ์ž์—ด "123abc"์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์ด ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜์˜€์œผ๋ฉฐ, ์‹œ์ž‘ ๋ถ€๋ถ„์˜ "123"์ด ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค. re.search() re.search()๋Š” ๋ฌธ์ž์—ด์—์„œ ์ •๊ทœ์‹ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์„ ์ฐพ์Šต๋‹ˆ๋‹ค. ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ฐพ์œผ๋ฉด match ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ฐพ์ง€ ๋ชปํ•˜๋ฉด None์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. import re # ํŒจํ„ด ์ •์˜: ํ•˜๋‚˜ ์ด์ƒ์˜ ์ˆซ์ž์— ์ผ์น˜ pattern = r"\d+" # ๊ฒ€์‚ฌํ•  ๋ฌธ์ž์—ด string = "abc123def456" # ๋ฌธ์ž์—ด์—์„œ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์„ ์ฐพ๊ธฐ result = re.search(pattern, string) # ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์œผ๋ฉด ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅ if result: print("์ผ์น˜: ", result.group()) else: print("๋ถˆ์ผ์น˜") # ๊ฒฐ๊ด๊ฐ’ ์ผ์น˜: 123 re.search()๋กœ ๋ฌธ์ž์—ด์—์„œ ์ฃผ์–ด์ง„ ์ •๊ทœ์‹ ํŒจํ„ด("\d+")๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š”์ง€ ์ฐพ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด "abc123def456"์—๋Š” ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— if ๋ฌธ์—์„œ ์ผ์น˜ํ•  ๋•Œ์— ํ•ด๋‹นํ•˜๋Š” ๊ฒฐ๊ด๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. re.findall() re.findall()์€ ๋ฌธ์ž์—ด์—์„œ ์ •๊ทœ์‹ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ถ€๋ถ„์„ ์ฐพ์•„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ์—†์œผ๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. import re # ํŒจํ„ด ์ •์˜: ํ•˜๋‚˜ ์ด์ƒ์˜ ์ˆซ์ž์— ์ผ์น˜ pattern = r"\d+" # ๊ฒ€์‚ฌํ•  ๋ฌธ์ž์—ด string = "abc123def456" # ๋ฌธ์ž์—ด์—์„œ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ถ€๋ถ„์„ ์ฐพ์•„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ result = re.findall(pattern, string) print("์ผ์น˜: ", result) # ๊ฒฐ๊ด๊ฐ’ ์ผ์น˜: ['123', '456'] re.findall()์€ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ถ€๋ถ„์„ ์ฐพ๊ธฐ ๋•Œ๋ฌธ์— ์•ž์„œ re.search()๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ์™€ ์ถœ๋ ฅ๋œ ๋‚ด์šฉ์ด ๋‹ค๋ฅธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ํ•˜๋‚˜๋ผ๋ฉด re.search()๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ์™€ ๊ฐ’์€ ๋™์ผํ•˜๊ฒ ์ง€๋งŒ re.findall()์€ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜ํ™˜๋œ ํ˜•ํƒœ๋Š” ๋‹ค๋ฅผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. re.sub() ๋ฌธ์ž์—ด์—์„œ ์ •๊ทœ์‹ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์ฒด ๋ฌธ์ž์—ด์€ repl ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ง€์ •๋ฉ๋‹ˆ๋‹ค. import re # ํŒจํ„ด ์ •์˜: ํ•˜๋‚˜ ์ด์ƒ์˜ ์ˆซ์ž์— ์ผ์น˜ pattern = r"\d+" # ๊ฒ€์‚ฌํ•  ๋ฌธ์ž์—ด string = "abc123def456" # ๋Œ€์ฒดํ•  ๋ฌธ์ž์—ด replacement = "000" # ๋ฌธ์ž์—ด์—์„œ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒด result = re.sub(pattern, replacement, string) print("๊ฒฐ๊ณผ: ", result) # ๊ฒฐ๊ด๊ฐ’ ๊ฒฐ๊ณผ: abc000def000 re.sub()์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ •๊ทœ์‹ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ถ€๋ถ„์„ ์ฐพ์•„์„œ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ถ€๋ถ„์„ ๋Œ€์ฒดํ•˜์ง€ ์•Š๊ณ  ํŠน์ • ์ผ์น˜ ํ•ญ๋ชฉ๊นŒ์ง€๋งŒ ๋Œ€์ฒด ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” re.sub()์„ ์‚ฌ์šฉํ•  ๋•Œ count๋ฅผ ์„ค์ •ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด count=1๋กœ ์„ค์ •ํ•œ๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ์ผ์น˜ ํ•ญ๋ชฉ๊นŒ์ง€๋งŒ ๋Œ€์ฒด ์ž‘์—…์„ ์‹คํ–‰ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋ณ„๋„์˜ count ๊ฐ’์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด count=0์ด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์–ด ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ถ€๋ถ„์„ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. re.compile ์ •๊ทœ์‹ ํŒจํ„ด์„ ์ปดํŒŒ์ผ(์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜) ํ•˜์—ฌ ์ •๊ทœ์‹ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ปดํŒŒ์ผ๋œ ๊ฐ์ฒด๋Š” ์—ฌ๋Ÿฌ ๋ฉ”์„œ๋“œ(match(), search(), findall(), sub() ๋“ฑ)๋ฅผ ์ œ๊ณตํ•˜์—ฌ ์ •๊ทœ์‹ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. import re # ํŒจํ„ด ์ •์˜: ํ•˜๋‚˜ ์ด์ƒ์˜ ์ˆซ์ž์— ์ผ์น˜ pattern = re.compile(r"\d+") # ๊ฒ€์‚ฌํ•  ๋ฌธ์ž์—ด string = "abc123def456" # ์ปดํŒŒ์ผ๋œ ์ •๊ทœ์‹ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ search ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ result = pattern.search(string) # ๊ฒฐ๊ณผ๊ฐ€ ์žˆ์œผ๋ฉด ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅ if result: print("์ผ์น˜: ", result.group()) else: print("๋ถˆ์ผ์น˜") # ๊ฒฐ๊ด๊ฐ’ ์ผ์น˜: 123 ์ง€๊ธˆ๊นŒ์ง€ ์ด์ „ ์˜ˆ์ œ๋“ค์—์„œ๋Š” ํŒจํ„ด์„ ์ปดํŒŒ์ผํ•˜์ง€ ์•Š๊ณ , pattern = r"\d+"์™€ ๊ฐ™์ด ๋‹จ์ˆœํžˆ ๋ฌธ์ž์—ด์„ pattern ๋ณ€์ˆ˜์— ์ €์žฅํ•˜์˜€๋Š”๋ฐ ์ด๋Š” ๋‹จ์ˆœํžˆ ์ •๊ทœ์‹์˜ ํŒจํ„ด์„ ์ •์˜ํ•œ ๊ฒƒ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๋‹ฌ๋ฆฌ pattern = re.compile(r"\d+")์ฒ˜๋Ÿผ re.compile ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹จ์ˆœํžˆ ํŒจํ„ด์„ ์ •์˜ํ•œ ๊ฒƒ์— ๊ทธ์น˜์ง€ ์•Š๊ณ  ํ•ด๋‹น ์ •๊ทœ์‹ ํŒจํ„ด์„ ๋ฏธ๋ฆฌ ์ปดํŒŒ์ผ(์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜) ํ•˜์—ฌ pattern ๋ณ€์ˆ˜์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ์™€ ๊ฐ™์ด ์ปดํŒŒ์ผํ•˜์ง€ ์•Š์€ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ re ๋ชจ๋“ˆ์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, ํŒŒ์ด์ฌ์€ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ๋‚ด๋ถ€์ ์œผ๋กœ ๋งค๋ฒˆ ํŒจํ„ด์„ ์ปดํŒŒ์ผํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฏธ๋ฆฌ ์ปดํŒŒ์ผํ•œ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด, ์—ฌ๋Ÿฌ ๋ฒˆ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ •๊ทœ์‹ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ด๋„ ์ •๊ทœ์‹ ํŒจํ„ด์„ ์ฒ˜์Œ์— ํ•œ ๋ฒˆ๋งŒ ์ปดํŒŒ์ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ํŒŒ์ด์ฌ ์ •๊ทœ ํ‘œํ˜„์‹์— ๋Œ€ํ•ด ๊ฐ„๋žตํžˆ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์„ ์•Œ๋ฉด ์ดํ›„ ๋ฐฐ์šฐ๊ฒŒ ๋  ๋ฌธ์„œ๋ฅผ ๋‹ค๋ฃจ๋Š” ์ฝ”๋“œ์™€ ๊ฒฐํ•ฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณต์žกํ•œ ๋ฌธ์ž์—ด์—์„œ ํ•„์š”ํ•œ ์ •๋ณด๋งŒ ์ถ”์ถœํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์ œํ•˜๋Š” ์ž‘์—… ๋“ฑ ๋‹ค์–‘ํ•œ ์˜์—ญ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋งํ–ˆ๋“ฏ ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃฌ ์ •๊ทœ์‹๊ณผ 're'๋ชจ๋“ˆ์˜ ๋‚ด์šฉ์€ ์ผ๋ถ€์ด๋ฉฐ, ๋” ์ž์„ธํžˆ ์ •๊ทœ์‹์— ๋Œ€ํ•ด ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ํŒŒ์ด์ฌ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. 02-08. ๊ธฐํƒ€ ๋ฌธ๋ฒ• ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํŒŒ์ด์ฌ์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์„œ ์‹คํ–‰ํ•˜๋‹ค ๋ณด๋ฉด ๋‹ค์–‘ํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ๊ณผ ๊ฐ™์ด ์‹œ์Šคํ…œ์˜ ๋ฌธ์ œ๋กœ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜(Error)๋„ ์žˆ์ง€๋งŒ, ํ”„๋กœ๊ทธ๋žจ์ด ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋‹ค๊ฐ€ ๋œป๋ฐ–์˜ ๊ฒฝ์šฐ๋ฅผ ๋งˆ์ฃผ์ณ์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋„ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํŒŒ์ด์ฌ์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•˜๋Š”๋ฐ ๋ถ„๋ชจ์— ์ˆซ์ž 0์ด ์ „๋‹ฌ๋œ๋‹ค๋ฉด ๋‚˜๋ˆ„๊ธฐ๋ฅผ ํ•  ์ˆ˜ ์—†์–ด์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘์— ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ์ƒํ™ฉ์„ ๋งˆ์ฃผ์ณค์„ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋ฅผ ์˜ˆ์™ธ(Exception)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ค‘๋‹จ๋˜๋Š”๋ฐ, ์˜ˆ์™ธ ์ƒํ™ฉ์ผ ๋•Œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€๋ฅผ ๋ฏธ๋ฆฌ ์„ค์ •ํ•ด๋‘๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ๊ฐ‘์ž๊ธฐ ์ข…๋ฃŒ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ๋Š” ๋ณดํ†ต try ~ except๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. try: ์›๋ž˜ ์‹คํ–‰ํ•˜๋ ค๋Š” ์ฝ”๋“œ ๋ธ”๋ก except: ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฝ”๋“œ ๋ธ”๋ก ์˜์–ด ๋‹จ์–ด try์˜ ๋œป์ธ '์‹œ๋„ํ•˜๋‹ค'๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด๋ฐ, ์›๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์‹œ๋„ํ•ด ๋ณด๊ณ  ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด(except) ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” try์™€ except ๋’ค์—๋Š” ๋ฐ”๋กœ ์ฝœ๋ก (:)์„ ๋ถ™์ธ ๋‹ค์Œ ์ค„๋ฐ”๊ฟˆ์„ ํ•˜๊ณ , ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•œ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ try ~ except๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. numbers = [1, 2, 3] try: print(numbers[3]) except: print("ํ•ด๋‹น ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ์š”์†Œ๊ฐ€ ๋ฆฌ์ŠคํŠธ์— ์—†์Šต๋‹ˆ๋‹ค.") # ๊ฒฐ๊ด๊ฐ’ ํ•ด๋‹น ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ์š”์†Œ๊ฐ€ ๋ฆฌ์ŠคํŠธ์— ์—†์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ๋Š” ์ด์š”์†Œ๊ฐ€ 3๊ฐœ์ธ numbers ๋ฆฌ์ŠคํŠธ์—์„œ ๋„ค ๋ฒˆ์งธ ์š”์†Œ(numbers[3])๋ฅผ ๊ฐ€์ ธ์˜ค๋„๋ก ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. try ~ except๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋งŒ์•ฝ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ "ํ•ด๋‹น ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ์š”์†Œ๊ฐ€ ๋ฆฌ์ŠคํŠธ์— ์—†์Šต๋‹ˆ๋‹ค."๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๋Š” IndexError์ธ๋ฐ, ๋งŒ์•ฝ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜์˜ ์ข…๋ฅ˜๋ฅผ ์•Œ๊ณ  ํ•ด๋‹น ์˜ค๋ฅ˜์— ๋Œ€ํ•ด์„œ๋งŒ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด except ๋ฐ”๋กœ ๋’ค์— ์˜ค๋ฅ˜์˜ ์ข…๋ฅ˜๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์˜ค๋ฅ˜์˜ ์ข…๋ฅ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. try: result = 10 / 0 print(result) except ZeroDivisionError: print("0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.") # ๊ฒฐ๊ด๊ฐ’ 0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๋Š” ์ˆซ์ž๋ฅผ 0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์–ด์„œ ์ƒ๊ธด ์˜ค๋ฅ˜(ZeroDivisionError)์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ except ZeroDivisionError:๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์˜ค๋ฅ˜์— ๋Œ€ํ•ด์„œ๋งŒ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์˜ ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋“  ์•ˆ ํ•˜๋“  ์ƒ๊ด€์—†์ด ํ•ญ์ƒ ํŠน์ •ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก finally๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. finally๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. try: ์›๋ž˜ ์‹คํ–‰ํ•˜๋ ค๋Š” ์ฝ”๋“œ ๋ธ”๋ก except: ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฝ”๋“œ ๋ธ”๋ก finally: ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•ญ์ƒ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ ๋ธ”๋ก ์ด ๊ฒฝ์šฐ์—๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ์—†๋‹ค๋ฉด try ๋’ค์˜ ์ฝ”๋“œ ๋ธ”๋ก์ด ์‹คํ–‰๋˜๊ณ , ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ์—๋Š” except ๋’ค์˜ ์ฝ”๋“œ ๋ธ”๋ก์ด ์‹คํ–‰๋˜์ง€๋งŒ ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ ๋งˆ์ง€๋ง‰์—๋Š” finally ๋’ค์˜ ์ฝ”๋“œ ๋ธ”๋ก์ด ํ•ญ์ƒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด except๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  try ~ finally๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. try: ์›๋ž˜ ์‹คํ–‰ํ•˜๋ ค๋Š” ์ฝ”๋“œ ๋ธ”๋ก finally: ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•ญ์ƒ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ ๋ธ”๋ก ๋‹ค์Œ์€ finally๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. my_list = [1, 2, 3, 4, 5] try: print(my_list[10]) except IndexError: print("ํ•ด๋‹น ์ธ๋ฑ์Šค๋Š” ๋ฆฌ์ŠคํŠธ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ์Šต๋‹ˆ๋‹ค.") finally: print("์ด ์ฝ”๋“œ๋Š” ํ•ญ์ƒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.") # ๊ฒฐ๊ด๊ฐ’ ํ•ด๋‹น ์ธ๋ฑ์Šค๋Š” ๋ฆฌ์ŠคํŠธ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋Š” ํ•ญ์ƒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž ํŒŒ์ด์ฌ์—์„œ๋Š” ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ์ถ•์•ฝํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋“ค์€ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๊ฑฐ๋‚˜ ์—ฐ์‚ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ง์…ˆ ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž (+=): ์ด ์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋”ํ•œ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ๋‹ค์‹œ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, x += 5๋Š” x = x + 5์™€ ๋™์ผํ•œ ํšจ๊ณผ๋ฅผ ๊ฐ€์ง€๋ฉฐ, x์— ํ˜„์žฌ ๊ฐ’์— 5๋ฅผ ๋”ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ x์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. x = 10 x += 5 # x = x + 5์™€ ๋™์ผ print(x) # ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 15 ๋บ„์…ˆ ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž (-=): ์ด ์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜์—์„œ ๊ฐ’์„ ๋บ€ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ๋‹ค์‹œ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, x -= 3์€ x = x - 3๊ณผ ๋™์ผํ•œ ํšจ๊ณผ๋ฅผ ๊ฐ€์ง€๋ฉฐ, x์—์„œ ํ˜„์žฌ ๊ฐ’์— 3์„ ๋บ€ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ x์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. x = 10 x -= 3 # x = x - 3๊ณผ ๋™์ผ print(x) # ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 7 ๊ณฑ์…ˆ ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž (*=): ์ด ์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊ณฑํ•œ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ๋‹ค์‹œ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, x *= 2๋Š” x = x * 2์™€ ๋™์ผํ•œ ํšจ๊ณผ๋ฅผ ๊ฐ€์ง€๋ฉฐ, x์˜ ํ˜„์žฌ ๊ฐ’์— 2๋ฅผ ๊ณฑํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ x์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. x = 5 x *= 2 # x = x * 2์™€ ๋™์ผ print(x) # ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 10 ๋‚˜๋ˆ—์…ˆ ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž (/=): ์ด ์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋‚˜๋ˆˆ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ๋‹ค์‹œ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, x /= 3์€ x = x / 3๊ณผ ๋™์ผํ•œ ํšจ๊ณผ๋ฅผ ๊ฐ€์ง€๋ฉฐ, x์˜ ํ˜„์žฌ ๊ฐ’์— 3์œผ๋กœ ๋‚˜๋ˆˆ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ x์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. x = 15 x /= 3 # x = x / 3๊ณผ ๋™์ผ print(x) # ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 5.0 (๋‚˜๋ˆ—์…ˆ ๊ฒฐ๊ณผ๋Š” ์‹ค์ˆ˜๋กœ ํ‘œํ˜„๋จ) ๋‚˜๋จธ์ง€ ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž (%=): ์ด ์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜๋ฅผ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๋ณ€์ˆ˜์— ๋‹ค์‹œ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, x %= 4๋Š” x = x % 4์™€ ๋™์ผํ•œ ํšจ๊ณผ๋ฅผ ๊ฐ€์ง€๋ฉฐ, x์˜ ํ˜„์žฌ ๊ฐ’์— 4๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๋‹ค์‹œ x์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. x = 10 x %= 4 # x = x % 4์™€ ๋™์ผ print(x) # ์ถœ๋ ฅ ๊ฒฐ๊ณผ: 2 ์ด๋Ÿฌํ•œ ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋“ค์€ ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๊ณ  ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๊ฑฐ๋‚˜ ์—ฐ์‚ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ• ๋‹นํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐ˜๋ณต๋ฌธ ๋“ฑ์—์„œ ํŠนํžˆ ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 02-09. ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋‹ค์–‘ํ•œ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—…๋ฌด ์ž๋™ํ™”๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‹ค์ œ ์—…๋ฌด์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€์•ผ ํ•˜๊ณ , ์ฝ์–ด์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ ๊ฐ€๊ณต์„ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ์ฝ์–ด์˜จ ๋ฐ์ดํ„ฐ์— ๋ถˆํ•„์š”ํ•œ ๋ฌธ์ž์—ด์ด ์žˆ์œผ๋ฉด ์ œ๊ฑฐํ•ด ์ฃผ๊ฑฐ๋‚˜, ๋ฌธ์ž์—ด๋“ค์„ ์„œ๋กœ ์—ฐ๊ฒฐํ•˜๊ฑฐ๋‚˜ ๋ถ„๋ฆฌํ•˜๋Š” ๋“ฑ์˜ ๊ฐ€๊ณต์„ ๋จผ์ € ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—๋Š” ์ด์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๋‹ค์–‘ํ•œ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ์™€ ์—ฐ์‚ฐ์ž๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฃผ์š” ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ ์•ž์—์„œ ํ•™์Šตํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฌธ์ž์—ด๋„ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. a + b : ๋ฌธ์ž์—ด a์™€ ๋ฌธ์ž์—ด b๋ฅผ ์—ฐ๊ฒฐํ•˜๊ธฐ a = "Hello" + " " + "World" print(a) # ๊ฒฐ๊ด๊ฐ’ Hello World a * b : ๋ฌธ์ž์—ด a๋ฅผ ์ˆซ์ž b ๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ธฐ a = "Hello " * 3 print(a) # ๊ฒฐ๊ด๊ฐ’ Hello Hello Hello ๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ ๋ฐ ์Šฌ๋ผ์ด์‹ฑ ๋ฌธ์ž์—ด๋„ ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ์€ ๋ฌธ์ž์—ด์— ํฌํ•จ๋œ ๊ฐ ๋ฌธ์ž์— ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•ด ํŠน์ • ์œ„์น˜์— ์žˆ๋Š” ๋ฌธ์ž์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. s = "Python" first_char = s[0] last_char = s[-1] print(first_char) print(last_char) # ๊ฒฐ๊ด๊ฐ’ n ๋ฌธ์ž์—ด "Python"์—์„œ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค(s[0])์™€ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค(s[-1])์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ์—์„œ ๋ณด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ์—์„œ๋„ ์Œ์ˆ˜ ์ธ๋ฑ์Šค ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜์—ฌ ๋ฌธ์ž์—ด์˜ ๋์—์„œ๋ถ€ํ„ฐ ๋ฌธ์ž์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์— ์ธ๋ฑ์Šค๊ฐ€ ๋ถ€์—ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ๋„ ์—ญ์‹œ ๋™์ผํ•˜๊ฒŒ ๋ฌธ์ž์—ด[start:end] ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ, end์˜ ์ธ๋ฑ์Šค๋Š” ํฌํ•จ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. s = "Python" sub_string = s[1:4] print(sub_string) # ๊ฒฐ๊ด๊ฐ’ yth ๋ฆฌ์ŠคํŠธ ์Šฌ๋ผ์ด์‹ฑ์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‹œ์ž‘์ด๋‚˜ ๋์˜ ์ธ๋ฑ์Šค๋ฅผ ์ƒ๋žตํ•˜์—ฌ ๊ฐ๊ฐ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋งˆ์ง€๋ง‰๊นŒ์ง€ ์Šฌ๋ผ์ด ์‹ฑํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๊ฐ„๊ฒฉ์„ ๊ฑด๋„ˆ๋›ฐ๋ฉฐ ๋ฌธ์ž์—ด์„ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ 1) ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜: lower(), upper() ํŒŒ์ด์ฌ์€ ์•ŒํŒŒ๋ฒณ์˜ ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•  ๋•Œ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†์ด ๋น„๊ตํ•˜๋ ค๋ฉด ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž ๋˜๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. lower() ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ๋ฐ˜๋Œ€๋กœ upper() ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋“  ์†Œ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. s = "Python" print(s.lower()) print(s.upper()) # ๊ฒฐ๊ด๊ฐ’ python PYTHON 2) ๋ฌธ์ž์—ด ๋ถ„๋ฆฌ ๋ฐ ๊ฒฐํ•ฉ: split(), join() ๋ฌธ์ž์—ด์„ ํŠน์ • ๋ฌธ์ž๋‚˜ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜๋กœ ๊ฒฐํ•ฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ฐ๊ฐ split()๊ณผ join() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š split() ๋ฉ”์„œ๋“œ์˜ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. str.split(sep=None, maxsplit=-1) ์ด ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๊ตฌ๋ถ„์ž('sep')๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด(str)์„ ์—ฌ๋Ÿฌ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ด„ํ˜ธ ์•ˆ์˜ ๋ถ€๋ถ„์€ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. sep๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๊ณต๋ฐฑ ๋ฌธ์ž(์ŠคํŽ˜์ด์Šค, ํƒญ, ์ค„๋ฐ”๊ฟˆ ๋“ฑ)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. maxsplit์€ ๋ถ„๋ฆฌํ•  ์ตœ๋Œ€ ํšŸ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ๊ธฐ๋ณธ๊ฐ’์€ -1๋กœ ์ œํ•œ ์—†์ด ๋ถ„๋ฆฌํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” join() ๋ฉ”์„œ๋“œ์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. str.join(iterable) ์ด ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด('iterable', ๋ณดํ†ต ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ)์˜ ๋ฌธ์ž์—ด ์›์†Œ๋“ค์„ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐํ•ฉ ์‹œ ๊ตฌ๋ถ„์ž ๋ฌธ์ž์—ด(str)์ด ๊ฐ ์›์†Œ ์‚ฌ์ด์— ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ split()๊ณผ join() ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ฒฐํ•ฉ์‹œํ‚ค๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. s = "Hello, World" words = s.split(", ") new_string = " ".join(words) print(words) print(new_string) # ๊ฒฐ๊ด๊ฐ’ ['Hello', 'World'] 'Hello World' ์œ„์˜ ์ฝ”๋“œ์—์„œ s.split(", ")์€ ๋ฌธ์ž์—ด("Hello, World")์„ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด split ๋ฉ”์„œ๋“œ์— ์‰ผํ‘œ(,)๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์—ด์ด ๋ถ„๋ฆฌ๋˜์–ด ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ๋œ words ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด " ".join(words)๋กœ ๊ณต๋ฐฑ์„ ๊ตฌ๋ถ„์ž๋กœ ํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์žฌ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ๋œ ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด ์ฒซ ๋ฒˆ์งธ ๋ณ€์ˆ˜ words์— ์ €์žฅ๋œ ๊ฐ’(๋ถ„๋ฆฌ๋œ ๊ฐ ๋ฌธ์ž์—ด์„ ์š”์†Œ๋กœ ํ•˜๋Š” ๋ฆฌ์ŠคํŠธ)๊ณผ ๋‘ ๋ฒˆ์งธ ๋ณ€์ˆ˜ new_string(๊ณต๋ฐฑ์„ ๊ตฌ๋ถ„์ž๋กœ ์žฌ๊ฒฐํ•ฉ๋œ ๋ฌธ์ž์—ด)์ด ๊ฐ๊ฐ ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 3) ๋ถˆํ•„์š”ํ•œ ๋ฌธ์ž์—ด ์ œ๊ฑฐ: strip() strip() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘๊ณผ ๋์—์„œ ์ง€์ •๋œ ๋ฌธ์ž๋“ค์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ ๋ฌธ์ž์—ด์˜ ์•ž๋’ค์— ์žˆ๋Š” ๊ณต๋ฐฑ(์ŠคํŽ˜์ด์Šค, ํƒญ, ์ค„๋ฐ”๊ฟˆ ๋“ฑ)์„ ์ œ๊ฑฐํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. strip()์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. str.strip(chars=None) chars ์ธ์ž์— ํŠน์ • ๋ฌธ์ž๋‚˜ ๋ฌธ์ž์—ด์„ ์ง€์ •ํ•˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž๋“ค๋งŒ ๋ฌธ์ž์—ด(str)์˜ ์•ž๊ณผ ๋’ค์—์„œ ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. chars ์ธ์ž๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ณต๋ฐฑ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๋ฉฐ, ์ด์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๊ด„ํ˜ธ ์•ˆ์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฌธ์ž์—ด(str)์˜ ์•ž๊ณผ ๋’ค์— ์ง€์ •ํ•œ ๋ฌธ์ž์—ด์ด ์—†์œผ๋ฉด str์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. chars์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ง€์ •ํ•  ๊ฒฝ์šฐ์— ์ˆœ์„œ๋Š” ์ƒ๊ด€์ด ์—†์Šต๋‹ˆ๋‹ค. s = " Hello " s.strip() # ๊ฒฐ๊ด๊ฐ’ 'Hello' ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” s.strip()์œผ๋กœ ์ธ์ž์— ์•„๋ฌด๊ฒƒ๋„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„ ๋ฌธ์ž์—ด " Hello "์˜ ์•ž๋’ค ๊ณต๋ฐฑ ๋ฌธ์ž๊ฐ€ ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. strip() ๋ฉ”์„œ๋“œ์™€ ๋น„์Šทํ•œ ๋‘ ๊ฐ€์ง€ ๊ด€๋ จ ๋ฉ”์„œ๋“œ๋กœ lstrip()๊ณผ rstrip()์ด ์žˆ์Šต๋‹ˆ๋‹ค. lstrip()์€ ๋ฌธ์ž์—ด์˜ ์™ผ์ชฝ(์‹œ์ž‘) ๋ถ€๋ถ„์—์„œ๋งŒ ์ง€์ •๋œ ๋ฌธ์ž๋“ค์„ ์ œ๊ฑฐํ•˜๋ฉฐ, rstrip()์€ ๋ฌธ์ž์—ด์˜ ์˜ค๋ฅธ์ชฝ(๋) ๋ถ€๋ถ„์—์„œ๋งŒ ์ง€์ •๋œ ๋ฌธ์ž์—ด์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์‚ฌ์šฉํ•  ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ์—์„œ ์‹œ์ž‘ ๋ถ€๋ถ„์ด๋‚˜ ๋๋ถ€๋ถ„ ์ค‘ ํ•œ ์ชฝ์—๋งŒ ์ œ๊ฑฐํ•  ๋ฌธ์ž์—ด์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” lstrip()์ด๋‚˜ rstrip()์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 4) ๋ฌธ์ž์—ด ์ฐพ๊ธฐ: find(), count(), startswith(), endswith() ํŠน์ • ๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์ฐพ๋Š” ์—ฌ๋Ÿฌ ๋ฉ”์„œ๋“œ๋“ค์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. find() ๋จผ์ € find() ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. str.find(substr, start, end) find() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(str)์—์„œ ์ฐพ์œผ๋ ค๋Š” ๊ฒ€์ƒ‰ ๋ฌธ์ž์—ด(substr)์ด ์‹œ์ž‘ํ•˜๋Š” ์ฒซ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์˜ ์œ„์น˜๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ, ์ฐพ์œผ๋ ค๋Š” ๋ฌธ์ž์—ด(substr)์ด ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(str)์— ์—†์„ ๊ฒฝ์šฐ -1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰ ๋ฌธ์ž์—ด(substr)์ด ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(str)์— ์—ฌ๋Ÿฌ ๋ฒˆ ๋‚˜์˜ฌ ๊ฒฝ์šฐ์—๋„ ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์ฒ˜์Œ ๋‚˜์˜จ ์œ„์น˜์˜ ์ธ๋ฑ์Šค๋งŒ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ์—์„œ ๊ด„ํ˜ธ ์•ˆ์˜ substr์€ ์ฐพ์„ ๋ฌธ์ž์—ด์ด๋ฉฐ, start๋Š” ๊ฒ€์ƒ‰์„ ์‹œ์ž‘ํ•  ์ธ๋ฑ์Šค, end๋Š” ๊ฒ€์ƒ‰์„ ์ข…๋ฃŒํ•  ์ธ๋ฑ์Šค๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ start์™€ end๋ฅผ ์„ค์ •ํ•˜๋ฉด ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(str)์—์„œ๋„ ํŠน์ • ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜์—ฌ ๊ฒ€์ƒ‰ ๋ฌธ์ž์—ด(substr)์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. start์™€ end ์˜ต์…˜์€ ์ž…๋ ฅํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, start๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ•˜๊ณ  end๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฌธ์ž์—ด ๋๊นŒ์ง€ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ find()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. s = "Hello, World" s.find("World") # ๊ฒฐ๊ด๊ฐ’ ๋ฌธ์ž์—ด "Hello, World"์—์„œ ๊ฒ€์ƒ‰ ๋ฌธ์ž("World")๋ฅผ ์ฐพ๊ณ  ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์‹œ์ž‘ํ•˜๋Š” ์ฒซ ์ธ๋ฑ์Šค, ์ฆ‰, W๊ฐ€ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค์ธ 7์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. count() find()๊ฐ€ ๊ฒ€์ƒ‰ ๋ฌธ์ž์—ด์ด ์‹œ์ž‘ํ•˜๋Š” ์ฒซ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด, count()๋Š” ๊ฒ€์ƒ‰ ๋ฌธ์ž์—ด์ด ๋ช‡ ๊ฐœ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. count()์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. str.count(substr, start, end) substr์€ ์ฐพ์œผ๋ ค๋Š” ๊ฒ€์ƒ‰ ๋ฌธ์ž์—ด์ด๋ฉฐ, find() ๋ฉ”์„œ๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ start์™€ end๋กœ ๊ฒ€์ƒ‰์„ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ์ข…๋ฃŒํ•  ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜๊ฐ’์€ ๋ฌธ์ž์—ด(str)์— ๊ฒ€์ƒ‰ ๋ฌธ์ž์—ด(substr)์ด ํฌํ•จ๋œ ํšŸ์ˆ˜์ด๋ฉฐ ํฌํ•จ๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์—๋Š” 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ count()๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. s = "Hello, World. Welcome to the World of Python." count_world = s.count("World") print(count_world) # ๊ฒฐ๊ด๊ฐ’ startswith()์™€ endswith() ์ด๋ฒˆ์— ์‚ดํŽด๋ณผ startswith()์™€ endswith()๋Š” ๊ฐ๊ฐ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ๊ฒ€์ƒ‰ ๋ฌธ์ž์—ด๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋๋‚˜๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. str.startswith(prefix, start, end) str.endswith(suffix, start, end) startswith()๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(str)์ด ํ™•์ธํ•˜๋ ค๋Š” ์‹œ์ž‘ ๋ฌธ์ž์—ด(prefix)๋กœ ์‹œ์ž‘ํ•˜๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. endswith()๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(str)์ด ํ™•์ธํ•˜๋ ค๋Š” ์ข…๋ฃŒ ๋ฌธ์ž์—ด(suffix)๋กœ ๋๋‚˜๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. find()๋‚˜ count()์ฒ˜๋Ÿผ start์™€ end ์˜ต์…˜์„ ์ง€์ •ํ•˜์—ฌ ํ™•์ธํ•˜๋ ค๋Š” ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ startswith() ๋ฉ”์„œ๋“œ์™€ endswith() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. s = "Hello, World" is_starting = s.startswith("Hell") is_ending = s.endswith("orld") print(is_starting) print(is_ending) # ๊ฒฐ๊ด๊ฐ’ True True ๋ฌธ์ž์—ด ๊ต์ฒด: replace() replace() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ์ง€์ •ํ•œ ๋ฌธ์ž์—ด์„ ์ฐพ์•„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. str.replace(old, new, count) replace() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(str)์—์„œ ์ง€์ •๋œ ๋ฌธ์ž์—ด(old)์„ ์ฐพ์•„์„œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด(new)๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. count๋Š” ๋Œ€์ฒด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ํšŸ์ˆ˜๋กœ, ์ง€์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“  ์ง€์ •๋œ ๋ฌธ์ž์—ด(old)์„ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด(new)๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. s = "apple apple apple" result = s.replace("apple", "orange", 2) print(result) # ๊ฒฐ๊ด๊ฐ’ orange orange apple ์œ„์˜ ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ๋Š” ๋Œ€์ฒดํ•  ํšŸ์ˆ˜๋ฅผ 2๋กœ ์ง€์ •ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด ๋ฌธ์ž์—ด ์‹œ์ž‘ ๋ถ€๋ถ„๋ถ€ํ„ฐ "apple"๊ณผ ์ผ์น˜ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์ž์—ด์ด "orange"๋กœ ๋Œ€์ฒด๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ํฌ๋งทํŒ… ๋ฌธ์ž์—ด ํฌ๋งทํŒ…์€ ๋ฌธ์ž์—ด ๋‚ด์— ํŠน์ • ๊ฐ’์„ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜, ๋ฌธ์ž์—ด์„ ํŠน์ •<NAME>์— ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ ๋ฌธ์ž์—ด ํฌ๋งทํŒ…์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•๋“ค์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) % ํฌ๋งทํŒ… % ํฌ๋งทํŒ…์€ ๋ฌธ์ž์—ด ๋‚ด์—์„œ ํฌ๋งท ์ง€์ •์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์‚ฝ์ž…ํ•  ์œ„์น˜์™€<NAME>์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. "ํฌ๋งท ์ง€์ •์ž๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์ž์—ด" % (์‚ฝ์ž…ํ•  ๊ฐ’) ํฐ๋”ฐ์˜ดํ‘œ(") ์‚ฌ์ด์— ํฌ๋งท ์ง€์ •์ž๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๊ณ  ๋’ค์— % ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•œ ๋’ค ์†Œ๊ด„ํ˜ธ()์— ํฌ๋งท ์ง€์ •์ž์˜ ์œ„์น˜์— ๋Œ€์ฒดํ•˜์—ฌ ์‚ฝ์ž…ํ•  ๊ฐ’์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ํฌ๋งท ์ง€์ •์ž๋Š” ๋ฌธ์ž์—ด ์•ˆ์—์„œ ๋ณ€์ˆ˜๋‚˜ ๊ฐ’์˜ ์ •ํ™•ํ•œ ํ‘œ์‹œ ์œ„์น˜์™€ ํ‘œ์‹œ<NAME>์„ ์ง€์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ํฌ๋งท ์ง€์ •์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํฌ๋งท ์ง€์ •์ž ์˜๋ฏธ %s ๋ฌธ์ž์—ด %d ์ •์ˆ˜ %f ๋ถ€๋™์†Œ์ˆ˜์  ์ˆซ์ž %x 16์ง„์ˆ˜ %o 8์ง„์ˆ˜ %e<NAME> ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ํ‘œํ˜„๋œ ๋ถ€๋™์†Œ์ˆ˜์  ์ˆซ์ž ์„ค๋ช… ๋ณด๋‹ค ์‹ค์ œ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค. ํฌ๋งท ์ง€์ •์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. name = "Alice" "Hello, %s" % name # ๊ฒฐ๊ด๊ฐ’ 'Hello, Alice' "Hello, %s"๋ผ๋Š” ๋ฌธ์ž์—ด ์•ˆ์— ํฌ๋งท ์ง€์ •์ž %s๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํฌ๋งท ์ง€์ •์ž๋Š” ๋ฌธ์ž์—ด ํฌ๋งท ์ง€์ •์ž๋กœ ํ•ด๋‹น ์œ„์น˜์— ๋ฌธ์ž์—ด์˜<NAME>์œผ๋กœ ๊ฐ’์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. % name์œผ๋กœ ๋ณ€์ˆ˜ name์˜ ๊ฐ’์„ ํฌ๋งท ์ง€์ •์ž ์œ„์น˜์— ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ name ๋ณ€์ˆ˜์˜ ๊ฐ’์ธ Alice๊ฐ€ ํ•ด๋‹น ์œ„์น˜์— ์‚ฝ์ž…๋˜์–ด ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’ 'Hello, Alice'๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. print("I have %d apples and % 0.2f oranges." % (5, 4.5678)) # ๊ฒฐ๊ด๊ฐ’ I have 5 apples and 4.57 oranges. ์ด๋ฒˆ์—๋Š” ์ •์ˆ˜๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” %d์™€ ๋ถ€๋™์†Œ์ˆ˜์  ์ˆซ์ž๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋Š” %f๊ฐ€ ๋ฌธ์ž์—ด ์•ˆ์— ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฝ์ž…ํ•  ๊ฐ’์€ ๊ฐ๊ฐ 5์™€ 4.5678๋กœ ์‚ฝ์ž…ํ•  ์ˆœ์„œ์— ๋งž๊ฒŒ ์†Œ๊ด„ํ˜ธ() ์•ˆ์— ์‰ผํ‘œ(,)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚˜์—ด๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ %d์˜ ์œ„์น˜์—๋Š” ์ •์ˆซ๊ฐ’ 5๊ฐ€, %f์˜ ์œ„์น˜์—๋Š” ๋ถ€๋™์†Œ์ˆ˜์  ์ˆซ์ž 4.5678์ด ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ %f ์‚ฌ์ด์— 0.2๋ผ๋Š” ์ˆซ์ž๊ฐ€ ๋“ค์–ด์žˆ์–ด ๋ฌธ์ž์—ด ์•ˆ์— % 0.2f๊ฐ€ ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์†Œ์ˆ˜์  ๋‘ ๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ํ‘œ์‹œํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ %f๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ 4.5678์ด ๊ทธ๋Œ€๋กœ ์‚ฝ์ž…๋˜์ง€ ์•Š๊ณ  ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ํ‘œ์‹œ๋˜์–ด 4.57์ด ์‚ฝ์ž…๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) str.format() ๋ฉ”์„œ๋“œ str.format() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด ๋‚ด์˜ ์ค‘๊ด„ํ˜ธ {}๋ฅผ ํฌ๋งท ์ง€์ •์ž๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. "์ค‘๊ด„ํ˜ธ {}๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์ž์—ด".format(์‚ฝ์ž…ํ•  ๊ฐ’) ๋ฌธ์ž์—ด ์•ˆ์— ์ค‘๊ด„ํ˜ธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ณ  ์ค‘๊ด„ํ˜ธ๊ฐ€ ์žˆ๋Š” ์œ„์น˜์— ์‚ฝ์ž…ํ•  ๊ฐ’์„ format() ๋ฉ”์„œ๋“œ์˜ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ์•ˆ์— ์—ฌ๋Ÿฌ ์ค‘๊ด„ํ˜ธ๊ฐ€ ์žˆ์–ด ์—ฌ๋Ÿฌ ๊ฐ’์„ ์‚ฝ์ž…ํ•  ๊ฒฝ์šฐ์—๋Š” ์‚ฝ์ž…ํ•  ์ˆœ์„œ๋Œ€๋กœ ์ธ์ˆ˜๋“ค์„ ์‰ผํ‘œ๋กœ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. print("I have {} apples and {} oranges.".format(5, 4)) # ๊ฒฐ๊ด๊ฐ’ I have 5 apples and 4 oranges. ๋˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ค‘๊ด„ํ˜ธ ์•ˆ์— ์ธ๋ฑ์Šค๋ฅผ ๋„ฃ์–ด์„œ ์ธ์ˆ˜๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ํ• ๋‹นํ•  ์ˆ˜๋„ ์žˆ๊ณ , ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๊ฐ๊ฐ ์ค‘๊ด„ํ˜ธ ์•ˆ์— ์ธ๋ฑ์Šค์™€ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. print("My name is {0} and I am {1} years old.".format("Alice", 30)) print("My name is {name} and I am {age} years old.".format(name="Alice", age=30)) # ๊ฒฐ๊ด๊ฐ’ My name is Alice and I am 30 years old. My name is Alice and I am 30 years old. str.format() ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ๋‚˜ ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. fruits = ["apple", "banana", "cherry"] print("I like {} and {}.".format(fruits[0], fruits[2])) # ๊ฒฐ๊ด๊ฐ’ I like apple and cherry. ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋ฅผ ์ธ๋ฑ์‹ฑ์œผ๋กœ ์ ‘๊ทผํ•˜์—ฌ format() ๋ฉ”์„œ๋“œ์˜ ์ธ์ˆ˜๋กœ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋ฆฌ์ŠคํŠธ์˜ ํŠน์ • ์š”์†Œ๋“ค์„ format() ๋ฉ”์„œ๋“œ์˜ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ format() ๋ฉ”์„œ๋“œ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. person = {'name': 'Alice', 'age': 30} print("My name is {person[name]} and I am {person[age]} years old.".format(person=person)) # ๊ฒฐ๊ด๊ฐ’ My name is Alice and I am 30 years old. {person[name]}๊ณผ ๊ฐ™์ด ๋”•์…”๋„ˆ๋ฆฌ person์˜ ํ‚ค name๊ณผ age๋ฅผ ์ค‘๊ด„ํ˜ธ ์•ˆ์— ๋„ฃ์–ด์„œ ๊ฐ ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์™€ ์ค‘๊ด„ํ˜ธ์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด format()์˜ ์ธ์ˆ˜๋กœ ๋„ฃ์„ ๋”•์…”๋„ˆ๋ฆฌ๋ช… ์•ž์— ๊ธฐํ˜ธ ** ์„ ๋ถ™์—ฌ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์–ธ ํŒจํ‚นํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์–ธ ํŒจํ‚นํ•˜๋ฉด ๋ฌธ์ž์—ด ๋ถ€๋ถ„์˜ ์ค‘๊ด„ํ˜ธ ์•ˆ์— {name}๊ณผ ๊ฐ™์ด ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๋งŒ ๋„ฃ์–ด์„œ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print("My name is {name} and I am {age} years old.".format(**person)) str.format() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด ํฌ๋งทํŒ…์— ์œ ์—ฐํ•˜๊ฒŒ ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŽ์€ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•˜๋ฉฐ, ํŠนํžˆ ๋ณต์žกํ•œ ํฌ๋งทํŒ…์ด๋‚˜ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋‹ค๋ฃฐ ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. 3) f-strings ํŒŒ์ด์ฌ 3.6์—์„œ ๋„์ž…๋œ f-strings๋Š” ๋ฌธ์ž์—ด ์•ž์— f ๋˜๋Š” F ์ ‘๋‘์–ด๋ฅผ ๋ถ™์ด๋ฉด, {} ๋‚ด์— ๋ณ€์ˆ˜๋‚˜ ํ‘œํ˜„์‹์„ ์ง์ ‘ ์ž‘์„ฑํ•˜์—ฌ ํฌ๋งทํŒ… ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. f"{๋ณ€์ˆ˜๋‚˜ ํ‘œํ˜„์‹}์ด ํฌํ•จ๋œ ๋ฌธ์ž์—ด" f-strings์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•์€ ์œ„์™€ ๊ฐ™์ด ๋ฌธ์ž์—ด ์•ž์— f๋‚˜ F๋ฅผ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž์—ด ์•ˆ์—๋Š” ์ค‘๊ด„ํ˜ธ{}๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉฐ ์ค‘๊ด„ํ˜ธ ์•ˆ์—๋Š” ๋ณ€์ˆ˜๋‚˜ ํ‘œํ˜„์‹์ด ์ž…๋ ฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. name = "์ง€ํ˜•" age = 28 print(f"์ œ ์ด๋ฆ„์€ {name}์ด๊ณ  ๋‚˜์ด๋Š” {age}์„ธ์ž…๋‹ˆ๋‹ค.") print(f"5 ๊ณฑํ•˜๊ธฐ 4๋Š” {5 * 4}.") # ๊ฒฐ๊ด๊ฐ’ ์ œ ์ด๋ฆ„์€ ์ง€ํ˜•์ด๊ณ  ๋‚˜์ด๋Š” 28์„ธ์ž…๋‹ˆ๋‹ค. 5 ๊ณฑํ•˜๊ธฐ 4๋Š” 20. ์ฒซ ๋ฒˆ์งธ print() ๋ถ€๋ถ„์„ ๋ณด๋ฉด, ๋จผ์ € ๋ฌธ์ž์—ด ์•ž์— ์ ‘๋‘์–ด๋กœ f๋ฅผ ์‚ฌ์šฉํ•˜์˜€๊ณ , ๋ฌธ์ž์—ด ๋‚ด์˜ ์ค‘๊ด„ํ˜ธ์— ๋ณ€์ˆ˜๋ช… name๊ณผ age๊ฐ€ ๊ฐ๊ฐ ์ž…๋ ฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด {name}๊ณผ {age} ๋ถ€๋ถ„์— ๊ฐ ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๊ฐ’๋“ค์ด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. print(f"5 ๊ณฑํ•˜๊ธฐ 4๋Š” {5 * 4}.")์—์„œ๋Š” ์ค‘๊ด„ํ˜ธ {} ์•ˆ์— ํ‘œํ˜„์‹ 5 * 4๊ฐ€ ์ž…๋ ฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ํ‘œํ˜„์‹์ด ์ž…๋ ฅ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋ฉด ์ค‘๊ด„ํ˜ธ ์•ˆ์˜ ํ‘œํ˜„์‹์ด ๊ณ„์‚ฐ๋œ ํ›„ ๊ฒฐ๊ด๊ฐ’์ด ํ•ด๋‹น ์ค‘๊ด„ํ˜ธ์— ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. ์ค‘๊ด„ํ˜ธ ์•ˆ์— ์ฝœ๋ก (:)์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์œ„์น˜์—์„œ์˜ ์ถœ๋ ฅ<NAME>์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ์ •๋ ฌ, ๋„ˆ๋น„ ์ง€์ •, ์ˆซ์ž<NAME> ๋“ฑ๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ<NAME> ์ง€์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ๋ช‡ ๊ฐ€์ง€<NAME>์„ ์ง€์ •ํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ํ•จ๊ป˜ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ratio = 0.756 print(f"Conversion rate: {ratio:.2%}") # ๊ฒฐ๊ด๊ฐ’ Conversion rate: 75.60% ์œ„์˜ ์˜ˆ์‹œ ์ฝ”๋“œ๋Š” ์ˆซ์ž<NAME>์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ค‘๊ด„ํ˜ธ ๋ถ€๋ถ„ {ratio:.2%}๋งŒ ์‚ดํŽด๋ณด๋ฉด ๋ณ€์ˆ˜ ratio ๋’ค์— ์ฝœ๋ก (:)์„ ์‚ฌ์šฉํ•œ ๋‹ค์Œ. 2%๋ฅผ<NAME>์œผ๋กœ ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ '.2'๋Š” ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ํ‘œํ˜„ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฉฐ, %๋Š” ์ˆซ์ž ๋’ค์— ๊ธฐํ˜ธ %๋ฅผ ๋ถ™์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋กœ ์ธํ•ด 75.60%๊ฐ€ ํ•ด๋‹น ์œ„์น˜์— ํ• ๋‹น๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ •๋ ฌ ์˜ต์…˜๊ณผ ์ถœ๋ ฅ ๋„ˆ๋น„๋ฅผ ์ง€์ •ํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. name = "Alice" print(f"{name:<10}") print(f"{name:>10}") print(f"{name:^10}") # ๊ฒฐ๊ด๊ฐ’ Alice Alice Alice ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ค‘๊ด„ํ˜ธ {name:<10}์˜ '<'์€ ๋ณ€์ˆ˜ name์„ ํ• ๋‹นํ•˜๋˜ ๋ฌธ์ž์—ด์„ ์™ผ์ชฝ ์ •๋ ฌํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ˆซ์ž '10'์€ ์ „์ฒด ์ถœ๋ ฅ ๋„ˆ๋น„๋ฅผ 10์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ๊ฐ’์ธ Alice๊ฐ€ ํ•ด๋‹น ์œ„์น˜์— ํ• ๋‹น๋˜๋ฉด์„œ ์™ผ์ชฝ ์ •๋ ฌ ํ›„ ๋’ค์— 5๊ฐœ์˜ ๊ณต๋ฐฑ์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. {name:>10}์™€ {name:^10}๋„ ๊ฐ™์€ ์›๋ฆฌ๋กœ ์ž‘๋™ํ•˜๋ฉฐ '>'๋Š” ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ, '^'๋Š” ๊ฐ€์šด๋ฐ ์ •๋ ฌ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋‘ ์ถœ๋ ฅ ๋„ˆ๋น„๋ฅผ 10์œผ๋กœ ์„ค์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— {name:>10}์—์„œ๋Š” Alice๊ฐ€ ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ๋˜๊ณ  ๊ทธ ์•ž์— 5๊ฐœ์˜ ๊ณต๋ฐฑ์ด ์ถ”๊ฐ€๋˜๋ฉฐ, {name:^10}๋Š” ๊ฐ€์šด๋ฐ ์ •๋ ฌ์ด๊ธฐ ๋•Œ๋ฌธ์— Alice์˜ ์•ž๊ณผ ๋’ค์— ๊ฐ๊ฐ 2๊ฐœ์™€ 3๊ฐœ์˜ ๊ณต๋ฐฑ์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  f-strings ์—ญ์‹œ format() ๋ฉ”์„œ๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฆฌ์ŠคํŠธ๋‚˜ ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. fruits = ["apple", "banana", "cherry"] print(f"I like {fruits[0]} and {fruits[2]}.") # ๊ฒฐ๊ด๊ฐ’ I like apple and cherry. ๋‹ค์Œ์€ f-strings์— ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. person = {'name': 'Alice', 'age': 30} print(f"My name is {person['name']} and I am {person['age']} years old.") # ๊ฒฐ๊ด๊ฐ’ My name is Alice and I am 30 years old. f-strings๋Š” ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด ํฌ๋งทํŒ…์„ ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋„๊ตฌ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ์‹๋“ค์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ฐฉ์‹์—๋Š” ๊ธฐ๋Šฅ์ ์ธ ํŠน์„ฑ์ด๋‚˜ ์žฅ๋‹จ์ ๋“ค์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒํ™ฉ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๋ฐฉ์‹์„ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ํ•™์Šตํ•  ์—ฌ๋Ÿฌ ์ฝ”๋“œ์—์„œ๋„ ํ•„์š”์— ๋”ฐ๋ผ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋ฅผ ํ•จ๊ป˜ ์ง„ํ–‰ํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. 03. ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ ์‹ค๋ฌด์—์„œ ์ง„ํ–‰๋˜๋Š” ๋‹ค์–‘ํ•œ ์—…๋ฌด๋“ค์€ ํŠน์„ฑ์— ๋”ฐ๋ผ ๊ด‘๋ฒ”์œ„ํ•œ ๋ฐ์ดํ„ฐ ๋ฐ ํŒŒ์ผ<NAME>์„ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์—…๋ฌด๊ฐ€ ์—‘์…€๋กœ๋งŒ ์ง„ํ–‰๋˜์ง€ ์•Š์œผ๋ฉฐ, ๋•Œ๋กœ๋Š” CSV ํŒŒ์ผ์ด๋‚˜ ๋‹จ์ˆœํ•œ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•  ๋•Œ๋„ ์žˆ์œผ๋ฉฐ, ํŒŒ์ผ์ด๋‚˜ ํด๋”๋ฅผ ์ด๋™, ๋ณต์‚ฌ, ์‚ญ์ œํ•˜๋Š” ์ž‘์—…๋„ ์ž์ฃผ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํŒŒ์ด์ฌ์„ ํ™œ์šฉํ•˜์—ฌ ์œˆ๋„์—์„œ ํŒŒ์ผ๊ณผ ํด๋”๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šธ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ์กฐ์ž‘ํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ, ๋‹จ์ˆœ ํ…์ŠคํŠธ ํŒŒ์ผ๊ณผ CSV ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ฐ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์€ ์ด๋Ÿฌํ•œ ์ž‘์—…๋“ค์„ ๊ฐ„์†Œํ™”ํ•˜๊ณ  ์ž๋™ํ™”ํ•˜์—ฌ, ๋ณต์žกํ•œ ์—…๋ฌด ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‹จ์ˆœํ™”ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค. ์—‘์…€์ด๋‚˜ ๋‹ค๋ฅธ ์˜คํ”ผ์Šค ํ”„๋กœ๊ทธ๋žจ์€ ๋’ค์—์„œ ์ž์„ธํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์˜ˆ์ •์ด๋ฉฐ, ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํŒŒ์ด์ฌ์˜ ๊ธฐ๋ณธ์ ์ธ ํŒŒ์ผ ํ•ธ๋“ค๋ง ๊ธฐ๋Šฅ์— ์ค‘์ ์„ ๋‘๊ณ  ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 03-01. ์œˆ๋„ ํด๋” ๋ฐ ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ ํŒŒ์ด์ฌ์œผ๋กœ ์œˆ๋„ ํŠน์ • ๊ฒฝ๋กœ์— ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ด๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๊ฑฐ๋‚˜ ํŒŒ์ผ์˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์œˆ๋„์˜ ํด๋”๋‚˜ ํŒŒ์ผ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ฃผ๋กœ os ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ shutil ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. os ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์šด์˜์ฒด์ œ์™€ ๊ด€๋ จ๋œ ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, ์šด์˜์ฒด์ œ์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ๋ช… ๋ณ€๊ฒฝ์ด๋‚˜ ํด๋” ์ƒ์„ฑ๊ณผ ๊ฐ™์ด ํŒŒ์ผ๊ณผ ํด๋”๋ฅผ ๋‹ค๋ฃจ๋Š” ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค๋„ ์ผ๋ถ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. shutil ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํŒŒ์ผ์ด๋‚˜ ํด๋”์— ํŠนํ™”๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๊ฑฐ๋‚˜ ์ด๋™ํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํŠน์„ฑ๊ณผ ๊ธฐ๋Šฅ์ด ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ชจ๋‘ ์ด์šฉํ•˜๋ฉด ํŒŒ์ด์ฌ์—์„œ ํด๋”์™€ ํŒŒ์ผ ์ž‘์—…์„ ๋ณด๋‹ค ํŽธํ•˜๊ฒŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํŒŒ์ด์ฌ์— ๊ธฐ๋ณธ์œผ๋กœ ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„๋กœ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ(ํด๋”) ์กด์žฌ ์œ ๋ฌด ํ™•์ธ ์–ด๋–ค ํŠน์ • ๊ฒฝ๋กœ์— ํŒŒ์ผ์ด๋‚˜ ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๋ ค๊ณ  ํ•  ๋•Œ, ์ด๋ฏธ ๊ทธ ๊ฒฝ๋กœ์— ๋™์ผํ•œ ์ด๋ฆ„์˜ ํŒŒ์ผ์ด๋‚˜ ํด๋”๊ฐ€ ์žˆ์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŒŒ์ผ์ด๋‚˜ ํด๋”๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋Š” os.path.exists()์ž…๋‹ˆ๋‹ค. import os if not os.path.exists('C:\\users\\Book'): # C ๋“œ๋ผ์ด๋ธŒ users์— 'Book' ํด๋”๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ print("ํด๋” ์—†์Œ") else: print("ํด๋” ์žˆ์Œ") ํŒŒ์ผ์ด ์žˆ๋Š”์ง€๋„ ๋™์ผํ•˜๊ฒŒ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํŒŒ์ผ์˜ ๊ฒฝ์šฐ ๋’ค์— ํ™•์žฅ์ž๋ฅผ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํŒŒ์ด์ฌ์ด ์‹คํ–‰๋œ ์œ„์น˜๊ฐ€ ์•„๋‹Œ ํŠน์ • ๊ฒฝ๋กœ์˜ ํด๋”๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์„ ๊ฐ€์ •ํ•˜์—ฌ ์ „์ฒด ๊ฒฝ๋กœ๋ฅผ ์ ์–ด์ฃผ์—ˆ์ง€๋งŒ, ์‹คํ–‰ ๊ฒฝ๋กœ์™€ ๋™์ผํ•œ ๊ฒฝ์šฐ ์ „์ฒด ๊ฒฝ๋กœ๋ฅผ ์ ์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ํŠน์ • ํด๋” ๋‚ด ๋ชฉ๋ก ํ™•์ธ ์œ„์—์„œ์™€ ๊ฐ™์ด ํŠน์ •ํ•œ ํด๋”๋ช…์ด๋‚˜ ํŒŒ์ผ๋ช…์„ ์ฃผ๊ณ  ์กด์žฌ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ํ•ด๋‹น ๊ฒฝ๋กœ์— ์žˆ๋Š” ์ „์ฒด ํŒŒ์ผ๊ณผ ํด๋”๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” os.listdir()๋กœ ์ „์ฒด ๋ชฉ๋ก์„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. import os lists = os.listdir(C:\\users\\Book) # C ๋“œ๋ผ์ด๋ธŒ users ๋‚ด Book ํด๋”์— ์žˆ๋Š” ํŒŒ์ผ๊ณผ ํด๋” ๋ชฉ๋ก ๋ฐ˜ํ™˜ print(lists) # ๊ฒฐ๊ด๊ฐ’ [ '2023', '2023-10-21.xlsx', '2023-10-30.xlsx', 'Untitled.ipynb', '์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx' ] ์ถœ๋ ฅ๋œ ๋ฆฌ์ŠคํŠธ์—์„œ ๋’ค์— ํ™•์žฅ์ž๊ฐ€ ์—†๋Š” ์š”์†Œ๋Š” ํด๋”๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ ํŒŒ์ผ๋“ค์€ ํŒŒ์ผ๋ช… ๋’ค์— ํ™•์žฅ์ž๋ฅผ ํฌํ•จํ•˜์—ฌ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํด๋”์™€ ํŒŒ์ผ์„ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ํŒŒ์ผ๋ช…/ํด๋”๋ช…(์ˆซ์ž - ์˜์–ด - ํ•œ๊ธ€)์„ ๊ธฐ์ค€์œผ๋กœ ์ˆœ์„œ๊ฐ€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ํด๋” ์ƒ์„ฑ ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” os.makedirs()์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํด๋”๊ฐ€ ํ•ด๋‹น ๊ฒฝ๋กœ์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import os if not os.path.exists('C:\\users\\Book'): # C ๋“œ๋ผ์ด๋ธŒ users์— 'Book' ํด๋”๊ฐ€ ์—†์œผ๋ฉด ์•„๋ž˜ ๋ช…๋ น์–ด ์ˆ˜ํ–‰ os.makedirs('C:\\users\\Book') # ๊ฒฝ๋กœ์— 'Book'ํด๋” ์ƒ์„ฑ ํด๋” ์ด๋ฆ„ ๋ณ€๊ฒฝ ์ƒ์„ฑ๋œ ํด๋”์˜ ์ด๋ฆ„์˜ ๋ณ€๊ฒฝํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํด๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ํด๋”๊ฐ€ ์žˆ์œผ๋ฉด ๋ณ€๊ฒฝํ•˜๋„๋ก ์กฐ๊ฑด์„ ์„ค์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. import os if os.path.exists('C:\\users\\Book'): # C ๋“œ๋ผ์ด๋ธŒ users์— 'Book' ํด๋”๊ฐ€ ์žˆ์œผ๋ฉด ์•„๋ž˜ ๋ช…๋ น์–ด ์ˆ˜ํ–‰ os.rename('C:\\users\\Book', 'C:\\users\\2023') # ํด๋”๋ช…์„ 'Book'์—์„œ '2023'์œผ๋กœ ๋ณ€๊ฒฝ os.rename()์€ ์œ„์™€ ๊ฐ™์ด ํด๋”๋ช…์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ฝ”๋“œ์ด์ง€๋งŒ, ๋งŒ์•ฝ ํด๋”์˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•  ๋•Œ ๊ฒฝ๋กœ๊นŒ์ง€๋„ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•  ํ•„์š” ์—†์ด os.rename()๋งŒ์œผ๋กœ๋„ ํด๋”์˜ ์ด๋™๋„ ํ•จ๊ป˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ณ€๊ฒฝํ•  ์ด๋ฆ„์„ ์ง€์ •ํ•  ๋•Œ ์ด๋™์‹œํ‚ฌ ๊ฒฝ๋กœ๊นŒ์ง€๋„ ํ•จ๊ป˜ ์ „๋‹ฌํ•ด ์ฃผ๋ฉด ์ด๋ฆ„์ด ๋ณ€๊ฒฝ๋œ ์ฑ„๋กœ ํด๋”์˜ ์œ„์น˜๊ฐ€ ์ด๋™๋ฉ๋‹ˆ๋‹ค. ํด๋” ๋ณต์‚ฌ ์ƒ์„ฑํ•œ ํด๋”๋ฅผ ๋‹ค๋ฅธ ๊ฒฝ๋กœ๋กœ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. shutil.copytree()๋กœ ํด๋”๋ฅผ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด ๋ฉ”์„œ๋“œ๋Š” ํด๋”์—๋งŒ ์ ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณต์‚ฌํ•  ์›๋ณธ์ด ํŒŒ์ผ์ด๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•ด๋‹น ํด๋”๋ฅผ ๋ณต์‚ฌํ•  ๊ฒฝ๋กœ์— ์ด๋ฏธ ํด๋”๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์—ฌ๊ธฐ์„œ๋Š” ๋Œ€์ƒ ๊ฒฝ๋กœ์— ๋ณต์‚ฌํ•  ํด๋”๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋งŒ ํด๋” ๋ณต์‚ฌ๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ์กฐ๊ฑด์„ ๋„ฃ์–ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. import os import shutil path_from = 'C:\\users\\2023\\sample' # ๋ณต์‚ฌํ•  ์›๋ณธ ํด๋” ๊ฒฝ๋กœ path_to = 'C:\\users\\2023\\10์›”\\sample' # ๋ณต์‚ฌ ์™„๋ฃŒ ํ›„์˜ ์ตœ์ข… ํด๋” ๊ฒฝ๋กœ if not os.path.exists(path_to): shutil.copytree(path_from, path_to) shutil.copytree()๋ฅผ ์ง๊ด€์ ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก, ์ด๋ฒˆ์—๋Š” ๋ณต์‚ฌํ•  ์›๋ณธ ํด๋”์˜ ๊ฒฝ๋กœ์™€ ๋ณต์‚ฌ๋ฅผ ์™„๋ฃŒํ•œ ๋‹ค์Œ์˜ ์ตœ์ข… ํด๋” ๊ฒฝ๋กœ๋ฅผ ๋ฏธ๋ฆฌ ๋ณ€์ˆ˜ path_from๊ณผ path_to์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. shutil.copytree()์—๋Š” ๋ณต์‚ฌํ•  ์›๋ณธ ํด๋” ๊ฒฝ๋กœ์™€ ๋Œ€์ƒ ํด๋” ๊ฒฝ๋กœ, ์ด๋ ‡๊ฒŒ ๋‘ ๊ฐ€์ง€ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ๋Š” ์›๋ณธ ํด๋”๋ฅผ ๋‹ค๋ฅธ ๊ฒฝ๋กœ์— ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋™์ผํ•œ ํด๋”๋ช…์œผ๋กœ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์›๋ณธ ํด๋” ๊ฒฝ๋กœ์™€ ๋™์ผํ•œ ๊ฒฝ๋กœ์— ๋˜‘๊ฐ™์€ ํด๋”๋ฅผ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ณต์‚ฌํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ํด๋”๋ช…๋งŒ ๋ณ€๊ฒฝํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํด๋” ์‚ญ์ œ import shutil path = 'C:\\users\\2023\\10์›”\\sample' # ์‚ญ์ œํ•  ํด๋” ๊ฒฝ๋กœ if os.path.exists(path): shutil.rmtree(path) shutil.rmtree()๋Š” ์ž…๋ ฅ๋ฐ›์€ ๊ฒฝ๋กœ ํด๋”๋ฅผ ์‚ญ์ œํ•˜๋Š” ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์—, ํด๋”๊ฐ€ ์•„๋‹Œ ํŒŒ์ผ์„ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์ฃผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž…๋ ฅ๋ฐ›์€ ํด๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ํด๋”๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์„ ์ฃผ๋กœ ์•Œ์•„๋ณด์•˜๋‹ค๋ฉด ์ด๋ฒˆ์—๋Š” ํŒŒ์ผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•๋“ค์„ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์œผ๋กœ ํ…์ŠคํŠธ ํŒŒ์ผ์ด๋‚˜ csv ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋’ค์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃฐ ์˜ˆ์ •์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ๋Š” ์ƒ์„ฑ๋œ ํŒŒ์ผ์˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๊ณ  ๋ณต์‚ฌ, ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ ๋ณต์‚ฌ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•  ๋•Œ๋Š” shutil.copyfile()์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ์›๋ณธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ž…๋ ฅ๋œ ๋Œ€์ƒ ํŒŒ์ผ๋ช…์œผ๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. import shutil shutil.copyfile('example.txt', 'example_copied.txt') ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” 'example.txt' ํŒŒ์ผ์„ ๋™์ผํ•œ ๊ฒฝ๋กœ์— 'example_copied.txt'๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น ๊ฒฝ๋กœ์— ์ž…๋ ฅ๋œ ๋Œ€์ƒ ํŒŒ์ผ๋ช…๊ณผ ๋™์ผํ•œ ํŒŒ์ผ์ด ์ด๋ฏธ ์กด์žฌํ•  ๊ฒฝ์šฐ, ๊ธฐ์กด์— ์žˆ๋˜ ๋‚ด์šฉ์€ ์‚ฌ๋ผ์ง€๊ณ  ์›๋ณธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์ด ๋ฎ์–ด์“ฐ์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ ์ด๋™ ํŒŒ์ผ์„ ์ด๋™ํ•  ๋•Œ๋Š” shutil.move()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. import shutil file_from = 'example_copied.txt' file_to = './2023\\example_copied.txt' shutil.move(file_from, file_to) shutil.move()์— ์ด๋™์‹œํ‚ฌ ์›๋ณธ ํŒŒ์ผ ๊ฒฝ๋กœ์™€ ์ด๋™ ์™„๋ฃŒ ํ›„์˜ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์ค๋‹ˆ๋‹ค. ์ด๋™ ์™„๋ฃŒ ํ›„์˜ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์ค„ ๋•Œ ํŒŒ์ผ๋ช…์„ ๊ธฐ์กด๊ณผ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•˜๋ฉด, ํŒŒ์ผ ์ด๋™ ์‹œ ํŒŒ์ผ๋ช…๋„ ํ•จ๊ป˜ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ์‚ญ์ œ ํŒŒ์ผ์„ ์‚ญ์ œํ•  ๋•Œ๋Š” os ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ remove()๋‚˜ unlink() ๋‘ ๊ฐ€์ง€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. remove()์™€ unlink() ๋‘ ํ•จ์ˆ˜ ๋ชจ๋‘ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋ฐ›์€ ๊ฒฝ๋กœ์˜ ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๋ฉฐ ์ฝ”๋“œ์˜<NAME>๋„ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‘ ํ•จ์ˆ˜ ์ค‘ ์ž์œ ๋กญ๊ฒŒ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” os.remove()๋กœ ํŒŒ์ผ์„ ์‚ญ์ œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import os if os.path.exists('example.txt'): os.remove('example.txt') ์œ„์˜ os.remove('example.txt') ๋Œ€์‹  os.unlink('example.txt')๋ฅผ ๋„ฃ์–ด์ค„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ํ•จ์ˆ˜ ๋ชจ๋‘ ํŒŒ์ผ์„ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์ž…๋ ฅ๊ฐ’์ด ํด๋”์ธ ๊ฒฝ์šฐ์—๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ, ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋ฐ›์€ ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ํŒŒ์ด์ฌ์—์„œ ํด๋”์™€ ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•๋“ค์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ์‚ดํŽด๋ณธ ๋‚ด์šฉ ์™ธ์—๋„ os์™€ shutil ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด์„œ๋Š” ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ๋ฅผ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. os ๋ชจ๋“ˆ ๋ฌธ์„œ: https://docs.python.org/3/library/os.html shutil ๋ชจ๋“ˆ ๋ฌธ์„œ: https://docs.python.org/3/library/shutil.html 03-02. ํ…์ŠคํŠธ ํŒŒ์ผ ์ฝ๊ณ  ์“ฐ๊ธฐ ํ…์ŠคํŠธ ํŒŒ์ผ ์ฝ๊ธฐ ํŒŒ์ด์ฌ์—์„œ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ฝ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์€ open() ํ•จ์ˆ˜์™€ read() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. # 'example.txt' ํŒŒ์ผ ์—ด๊ธฐ ( ์ฝ๊ธฐ ๋ชจ๋“œ 'r', ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹ ' utf-8') file = open('example.txt', 'r', encoding = 'utf-8') # ํŒŒ์ผ ์ฝ๊ธฐ content = file.read() # ํŒŒ์ผ ๋‹ซ๊ธฐ file.close() # ์ฝ์–ด์˜จ ํŒŒ์ผ ๋‚ด์šฉ ์ถœ๋ ฅ print(content) ํŒŒ์ด์ฌ ๋‚ด์žฅ ํ•จ์ˆ˜ open()์œผ๋กœ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์—ด์–ด์„œ read()๋กœ ํŒŒ์ผ์„ ์ฝ์€ ํ›„ close()๋กœ ๋‹ซ์•„์ค๋‹ˆ๋‹ค. open()์œผ๋กœ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ํŒŒ์ผ์˜ ์œ„์น˜๊ฐ€ ํŒŒ์ด์ฌ์˜ ์‹คํ–‰ ์œ„์น˜์™€ ๋™์ผํ•  ๊ฒฝ์šฐ ํŒŒ์ผ๋ช…๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋˜์ง€๋งŒ, ํŒŒ์ผ์ด ๋‹ค๋ฅธ ๊ฒฝ๋กœ์— ์œ„์น˜ํ•ด์žˆ์„ ๊ฒฝ์šฐ ์ „์ฒด ๊ฒฝ๋กœ๋ฅผ ํฌํ•จํ•˜์—ฌ ํŒŒ์ผ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์—ด ๋•Œ, ํŒŒ์ผ์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ํŒŒ์ผ์˜ ์†์„ฑ ๋ชจ๋“œ๋ฅผ ์˜ต์…˜์„ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ๋Š” ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๊ธฐ ์œ„ํ•ด 'r' ์ฝ๊ธฐ ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ ์—ด๊ธฐ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“œ(mode)์˜ ์ข…๋ฅ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ข…๋ฅ˜ ๋ชจ๋“œ ์„ค๋ช… ์ž‘์—… 'r' ์ฝ๊ธฐ ๋ชจ๋“œ (๊ธฐ๋ณธ). ๋ชจ๋“œ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ ์ฝ๊ธฐ ๋ชจ๋“œ๋กœ ์‹คํ–‰. ํŒŒ์ผ์ด ์—†์œผ๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์ž‘์—… 'w' ์“ฐ๊ธฐ ๋ชจ๋“œ. ๊ฐ™์€ ์ด๋ฆ„์˜ ํŒŒ์ผ์ด ์žˆ์œผ๋ฉด ๊ธฐ์กด ํŒŒ์ผ์„ ๋ฎ์–ด์“ฐ๋ฉฐ, ์—†์„ ๊ฒฝ์šฐ ์ƒˆ ํŒŒ์ผ์„ ์ƒ์„ฑ ์ž‘์—… 'a' ์ถ”๊ฐ€ ๋ชจ๋“œ. ๊ธฐ์กด์— ์ €์žฅ๋œ ํŒŒ์ผ์˜ ๋์— ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ์ถ”๊ฐ€. ๊ธฐ์กด ํŒŒ์ผ์ด ์—†์œผ๋ฉด ์ƒˆ ํŒŒ์ผ์„ ์ƒ์„ฑ ์ž‘์—… 'x' ์“ฐ๊ธฐ ๋ชจ๋“œ. ๊ฐ™์€ ์ด๋ฆ„์˜ ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋งŒ ์ƒˆ ํŒŒ์ผ์„ ์ƒ์„ฑ. ์ด๋ฏธ ์žˆ์œผ๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์ž‘์—… '+' ์ฝ๊ธฐ์™€ ์“ฐ๊ธฐ ํ˜ผํ•ฉ ๋ชจ๋“œ (์˜ˆ: 'r+', 'w+', 'a+') ํ˜•์‹ 'b' ์ด์ง„ ๋ชจ๋“œ. ํŒŒ์ผ์„ ์ด์ง„(binary)<NAME>์œผ๋กœ ์‹คํ–‰ ํ˜•์‹ 't' ํ…์ŠคํŠธ ๋ชจ๋“œ (๊ธฐ๋ณธ). ํŒŒ์ผ์„ ํ…์ŠคํŠธ(text)<NAME>์œผ๋กœ ์‹คํ–‰. ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ ํ…์ŠคํŠธ ๋ชจ๋“œ๋กœ ์‹คํ–‰ ์ž‘์—… ๋ฐฉ์‹์ธ 'r', 'w', 'x', 'a'์™€ ํŒŒ์ผ<NAME>์ธ 'b', 't'๋Š” 'rb', 'wt'์™€ ๊ฐ™์ด ํ˜ผํ•ฉํ•˜์—ฌ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค ์ง€์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๊ธฐ๋ณธ์ ์œผ๋กœ 'rt'์ธ ํ…์ŠคํŠธ ํŒŒ์ผ ์ฝ๊ธฐ ๋ชจ๋“œ๊ฐ€ ์„ ํƒ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ์†์„ฑ ์™ธ์— ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹๋„ ์˜ต์…˜์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ณ„๋„๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์œผ๋กœ ์ž‘์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. *์ธ์ฝ”๋”ฉ์ด๋ž€ ํ…์ŠคํŠธ๋‚˜ ์ •๋ณด๋ฅผ ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•˜๋Š” ์–ธ์–ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ปดํ“จํ„ฐ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฝ๊ณ  ์“ธ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ทœ์น™์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”ฉ์„ ํ•ด์ฃผ๋Š” ๋ฐฉ์‹๋„ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋Š”๋ฐ, ์ž‘์—…ํ•  ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ ํŠน์ •ํ•œ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์œผ๋กœ ์‹คํ–‰ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธ€์ž๊ฐ€ ๊นจ์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ธ€์˜ ๊ฒฝ์šฐ ์ฃผ๋กœ utf-8์ด๋‚˜ euc-kr, cp949 ๋“ฑ์˜ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ํ•œ๊ธ€ ์œˆ๋„ ํ™˜๊ฒฝ์—์„œ cp949๋กœ ์ธ์ฝ”๋”ฉํ•œ ํ…์ŠคํŠธ ํŒŒ์ผ์„ open()์œผ๋กœ ์—ด ๋•Œ๋Š” ์ธ์ฝ”๋”ฉ ์˜ต์…˜์„ ์ƒ๋žตํ•ด๋„ ํŒŒ์ผ์„ ๋ฌธ์ œ์—†์ด ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with ๋ฌธ์œผ๋กœ ํŒŒ์ผ ์ฝ๊ธฐ ์œ„์—์„œ ํ•™์Šตํ•œ ์ฝ”๋“œ์—์„œ๋Š” ์ž‘์—…์ด ๋๋‚ฌ์„ ๋•Œ ๋ณ„๋„๋กœ close()๋กœ ํŒŒ์ผ์„ ๋‹ซ์•„์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ with ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์ผ์„ ๋‹ซ๋Š” ๋ช…๋ น์„ ํ•˜์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ ํŒŒ์ผ์„ ๋‹ซ์•„ ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” with ๋ฌธ์œผ๋กœ ๋™์ผํ•˜๊ฒŒ ํŒŒ์ผ์„ ์—ฌ๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. # 'example.txt' ํŒŒ์ผ ์—ด๊ธฐ ( ์ฝ๊ธฐ ๋ชจ๋“œ 'r', ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹ ' utf-8') with open('example.txt', 'r', encoding = 'utf-8') as file: content = file.read() # ํŒŒ์ผ ์ฝ๊ธฐ print(content) # ์ฝ์–ด์˜จ ํŒŒ์ผ ๋‚ด์šฉ ์ถœ๋ ฅ ์œ„์˜ ์ฝ”๋“œ์—์„œ ๋ณด๋“ฏ ๋ณ„๋„๋กœ close() ์ž‘์—…์„ ํ•˜์ง€ ์•Š์•„๋„ ํŒŒ์ผ์„ ์ž๋™์ ์œผ๋กœ ๋‹ซ์•„์ค๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ํŒŒ์ผ ์“ฐ๊ธฐ ๋‹ค์Œ์œผ๋กœ ์ด๋ฒˆ์—๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # 'example.txt' ํŒŒ์ผ ์—ด๊ธฐ ( ์“ฐ๊ธฐ ๋ชจ๋“œ 'w', ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹ ' utf-8') file = open('example.txt', 'w', encoding = 'utf-8') # ๋ฌธ์ž์—ด ์“ฐ๊ธฐ file.write('Hello, \n world!') # ํŒŒ์ผ ๋‹ซ๊ธฐ file.close() ํŒŒ์ผ์„ ์“ฐ๋Š” ์ฝ”๋“œ๋„ ์ฝ์„ ๋•Œ์˜ ์ฝ”๋“œ์™€ ๋งค์šฐ ์œ ์‚ฌํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์—ด ๋•Œ ๋ชจ๋“œ๋งŒ 'w'๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์œ ์˜ํ•ด์•ผ ํ•  ์ ์€, write() ํ•จ์ˆ˜๋Š” ์ž๋™ ์ค„ ๋ฐ”๊ฟˆ์ด ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ค„ ๋ฐ”๊ฟˆ์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ์—๋Š” ์ง์ ‘ ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค. *๊ฐœํ–‰๋ฌธ์ž๋Š” ํ…์ŠคํŠธ์—์„œ ์ค„์„ ๋ฐ”๊ฟ” ์ƒˆ๋กœ์šด ์ค„์„ ์‹œ์ž‘ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ํŠน์ˆ˜๋ฌธ์ž์ž…๋‹ˆ๋‹ค. ๊ฐœํ–‰๋ฌธ์ž์—๋Š” ์บ๋ฆฌ์ง€ ๋ฆฌํ„ด(Carriage Return, ์ค„์—ฌ์„œ CR)๊ณผ ๋ผ์ธํ”ผ๋“œ(Line Feed, ์ค„์—ฌ์„œ LF)๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์บ๋ฆฌ์ง€ ๋ฆฌํ„ด(CR)์€ ๋ฐฑ์Šฌ๋ž˜์‹œ(\)์™€ ์˜์–ด r์ด ๊ฒฐํ•ฉ๋œ '\r'์ด๋ฉฐ, ๋ผ์ธํ”ผ๋“œ(LF)๋Š” ๋ฐฑ์Šฌ๋ž˜์‹œ(\)์™€ ์˜์–ด n์ด ๊ฒฐํ•ฉ๋œ '\n'์˜ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. CR์€ ๋งจ ์•ž์œผ๋กœ ์ด๋™ํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋ฉฐ LF๋Š” ๋‹ค์Œ ์ค„๋กœ ์ด๋™ํ•˜๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์˜๋ฏธ๋งŒ ์ƒ๊ฐํ•˜๋ฉด LF๋งŒ ์žˆ์œผ๋ฉด ๋  ๊ฒƒ ๊ฐ™์€๋ฐ CR์ด ์žˆ๋Š” ์ด์œ ๋Š” ์˜›๋‚  ํƒ€์ž๊ธฐ๋กœ ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ์‹์—์„œ ์œ ๋ž˜ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์šด์˜์ฒด์ œ์— ๋”ฐ๋ผ ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฅธ๋ฐ, ์œˆ๋„์—์„œ๋Š” CR๊ณผ LF๊ฐ€ ๊ฒฐํ•ฉ๋œ '\r\n'์œผ๋กœ, ๋ฆฌ๋ˆ…์Šค๋‚˜ macOS์—์„œ๋Š” '\n'๋งŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค„๋ฐ”๊ฟˆ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” '\n'๋งŒ ์‚ฌ์šฉํ•ด๋„ ์ค„๋ฐ”๊ฟˆ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃฐ ์˜ˆ์ œ์—์„œ๋Š” '\n'์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์“ฐ๊ธฐ ๋ชจ๋“œ๋„ ์ฝ๊ธฐ ๋ชจ๋“œ์™€ ๋™์ผํ•˜๊ฒŒ with ๋ฌธ์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ with ๋ฌธ์œผ๋กœ ๋™์ผํ•˜๊ฒŒ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์“ฐ๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. # 'example.txt' ํŒŒ์ผ ์—ด๊ธฐ ( ์“ฐ๊ธฐ ๋ชจ๋“œ 'w', ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹ ' utf-8') with open('example.txt', 'w') as file: file.write('Hello, \n world!') # ๋ฌธ์ž์—ด ์“ฐ๊ธฐ ํŒŒ์ผ์ด ์ž˜ ์ƒ์„ฑ๋๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์œˆ๋„ type ๋ช…๋ น์œผ๋กœ ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. !type C:\user\example.txt # ์ถœ๋ ฅ๊ฐ’ Hello, world! ํ…์ŠคํŠธ ํŒŒ์ผ์„ ํ•œ ์ค„์”ฉ ์ฝ๊ณ  ์“ฐ๊ธฐ ์•ž์—์„œ๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ฝ๊ฑฐ๋‚˜ ์“ธ ๋•Œ ์ „์ฒด ํŒŒ์ผ์„ ํ•œ ๋ฒˆ์— ์ž‘์—…ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํ•œ ์ค„์”ฉ ์ฝ๊ฑฐ๋‚˜ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•œ ์ค„์”ฉ ์ฝ๊ธฐ 'readline()' ๋˜๋Š” 'readlines()'๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์ผ์„ ํ•œ ์ค„์”ฉ ์ฝ์–ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. readline()๊ณผ readlines()๋Š” ์ž‘์—… ๋ฐฉ์‹์ด ์กฐ๊ธˆ ๋‹ค๋ฅธ๋ฐ, readline()์€ ํŒŒ์ผ์˜ ํ˜„์žฌ ์œ„์น˜์—์„œ ํ•œ ์ค„์”ฉ๋งŒ ์ฝ์–ด์„œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , readlines()๋Š” ๋ชจ๋“  ๋ผ์ธ์„ ์ฝ์–ด์„œ ๊ฐ ๋ผ์ธ์„ ์š”์†Œ๋กœ ๊ฐ–๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. readline() ๋จผ์ € readline()๋ถ€ํ„ฐ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. readline()์€ ํŒŒ์ผ์˜ ํ˜„์žฌ ์œ„์น˜์—์„œ ํ•˜๋‚˜์˜ ๋ผ์ธ๋งŒ ์ฝ์–ด ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ฒ˜์Œ ํŒŒ์ผ์„ ์—ฐ ํ›„ readline()์„ ์‹คํ–‰ํ•˜๋ฉด ํŒŒ์ผ์˜ ์ฒซ ์ค„์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ์œ„์น˜๋ฅผ ๋‹ค์Œ ๋ผ์ธ์˜ ์‹œ์ž‘ ์ง€์ ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ readline()์„ ์‹คํ–‰ํ•˜๋ฉด, ์ด๋ฒˆ์—๋Š” ๋‘ ๋ฒˆ์งธ ๋ผ์ธ์ด ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•œ ์ค„์”ฉ ์ฝ์–ด์„œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ํŒŒ์ผ์˜ ๋์— ๋„๋‹ฌํ•˜๋ฉด ๋นˆ ๋ฌธ์ž์—ด('')์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ์ฝ์–ด์˜ฌ ๋•Œ๋Š” ๊ทธ ๋ผ์ธ์˜ ๊ฐœํ–‰๋ฌธ์ž(\n)๋„ ํฌํ•จํ•˜์—ฌ ๋ฐ˜ํ™˜๋˜๋Š”๋ฐ, ํŒŒ์ผ์˜ ๋งˆ์ง€๋ง‰ ์ค„์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ์šด์˜์ฒด์ œ์— ๋”ฐ๋ผ ๊ฐœํ–‰๋ฌธ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•œ ์ค„์„ ๋ฐ˜ํ™˜ํ–ˆ์„ ๋•Œ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด ์—†์ด ๊ฐœํ–‰ ๋ฌธ์ž๋งŒ ๋ฐ˜ํ™˜๋œ๋‹ค๋ฉด ๊ทธ ์ค„์€ ๊ณต๋ฐฑ์ธ ์ค„์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ์ฝ”๋“œ๋กœ๋Š” ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # 'example.txt' ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋ชจ๋“œ 'r'๋กœ ์—ด๊ธฐ with open('example.txt', 'r') as file: line1 = file.readline() # ํŒŒ์ผ์˜ ์ฒซ ์ค„ ์ฝ๊ธฐ line2 = file.readline() # ํŒŒ์ผ์˜ ๊ทธ๋‹ค์Œ ์ค„ ์ฝ๊ธฐ print(line1, line2) # ์ถœ๋ ฅ๊ฐ’ Hello, world! ํ•œ ์ค„์”ฉ ์ฝ์–ด์˜ค๋Š” readline()์œผ๋กœ ํŒŒ์ผ ์ „์ฒด๋ฅผ ์ฝ์œผ๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ชจ๋“  ๋ผ์ธ์— ๋Œ€ํ•ด ๊ฐ๊ฐ ์ฝ”๋“œ๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  while ๋ฌธ์œผ๋กœ ์ „์ฒด ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # 'example.txt' ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋ชจ๋“œ 'r'๋กœ ์—ด๊ธฐ with open('example.txt', 'r') as file: line = file.readline() # ํŒŒ์ผ์˜ ์ฒซ ์ค„ ์ฝ๊ธฐ while line: # ํŒŒ์ผ ๋์— ๋„๋‹ฌํ•˜๊ธฐ ์ „๊นŒ์ง€ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ print(line.strip()) # ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ฝ์–ด์˜จ ์ค„์„ ์ถœ๋ ฅ line = file.readline() # ๋‹ค์Œ ์ค„์„ ์ฝ๊ธฐ ํŒŒ์ผ์„ ํ•œ ์ค„์”ฉ ์ฝ์–ด์˜ฌ ๋•Œ while ๋ฌธ์œผ๋กœ ์กฐ๊ฑด์„ ๋„ฃ์–ด readline()์„ ๋ฐ˜๋ณตํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๋” ์ด์ƒ ์ฝ์–ด์˜ฌ ๋ผ์ธ์ด ์—†๋Š” ํŒŒ์ผ์˜ ๋์— ๋„๋‹ฌํ•˜๊ธฐ ์ „๊นŒ์ง€ readline()๋กœ ๊ทธ ๋‹ค์Œ์ค„์„ ์ฝ์–ด์˜ค๋Š” ์ž‘์—…์„ ๋ฐ˜๋ณตํ•จ์œผ๋กœ์จ ์ „์ฒด ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ์–ด์˜จ ์ค„์„ ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉ๋œ. strip()์€ ๋ถˆํ•„์š”ํ•œ ๋ฌธ์ž์—ด์„ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ๊ฐ ๋ผ์ธ์— ๊ฐœํ–‰๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”๋ฐ ์ด๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋’ค์— ๋นˆ ์ค„์ด ํ•จ๊ป˜ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ถˆํ•„์š”ํ•œ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด strip()์„ ์‚ฌ์šฉํ•˜์˜€์œผ๋ฉฐ, ์ด ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. readlines() ์ด๋ฒˆ์—๋Š” ๊ทธ๋Ÿฌ๋ฉด readlines()๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. readlines()๋Š” ๋ชจ๋“  ์ค„์„ ์ฝ์€ ๋‹ค์Œ, ์ฝ์€ ๋ชจ๋“  ๋ผ์ธ์„ ์š”์†Œ๋กœ ํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ์ „์ฒด๋ฅผ ์ฝ์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— readline()๊ณผ ๋‹ฌ๋ฆฌ readlines()๋Š” ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ด ๋ฉ”์„œ๋“œ๋Š” ์ฝ์–ด์˜จ ์ „์ฒด ๋‚ด์šฉ์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํฐ ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ๋•Œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with open('example.txt', 'r') as file: lines = file.readlines() # ํŒŒ์ผ์˜ ์ „์ฒด์˜ ๋‚ด์šฉ ์ฝ๊ธฐ print(lines) # ๋ฆฌ์ŠคํŠธํ˜•์œผ๋กœ ์ €์žฅ๋œ ์ „์ฒด ๋‚ด์šฉ ์ถœ๋ ฅ # ๊ฒฐ๊ด๊ฐ’ ['Hello, \n', ' world!'] readlines()๋กœ ํ•œ ๋ฒˆ์— ํŒŒ์ผ ์ „์ฒด๋ฅผ ์ฝ์–ด์˜จ ๋‹ค์Œ ์ถœ๋ ฅํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ๋œ ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด ๊ฐ ๋ผ์ธ์ด ๊ฐ๊ฐ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋กœ ํ• ๋‹น๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋ผ์ธ์—๋Š” readline()์„ ์‚ฌ์šฉํ•  ๋•Œ์™€ ๋™์ผํ•˜๊ฒŒ ๊ฐœํ–‰๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ๋นผ์ฃผ๋ ค๋ฉด, ์—ฌ๊ธฐ์„œ๋Š” for ๋ฌธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. with open('example.txt', 'r') as file: lines = file.readlines() # ํŒŒ์ผ์˜ ์ „์ฒด์˜ ๋‚ด์šฉ ์ฝ๊ธฐ for line in lines: # ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๋ฉฐ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ print(line.strip()) # ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ฝ์–ด์˜จ ์ค„์„ ์ถœ๋ ฅ # ๊ฒฐ๊ด๊ฐ’ Hello, world! ๋จผ์ € readlines()๋กœ ํŒŒ์ผ์˜ ์ „์ฒด ๋‚ด์šฉ์„ ์ฝ์–ด์™€ ๋ฆฌ์ŠคํŠธํ˜•์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ €์žฅ๋œ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ์— ํ•˜๋‚˜์”ฉ ์ ‘๊ทผํ•˜์—ฌ. strip()์œผ๋กœ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฆฌ์ŠคํŠธ ์ „์ฒด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐ ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅ๋œ ๊ฒฐ๊ด๊ฐ’๋„ ์•ž์„  ์ฝ”๋“œ์™€ ๋‹ค๋ฅด๊ฒŒ ๋ฆฌ์ŠคํŠธํ˜•์ด ์•„๋‹ˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•œ ์ค„์”ฉ ์“ฐ๊ธฐ ์ด๋ฒˆ์—๋Š” ๊ทธ๋Ÿฌ๋ฉด ํ•œ ์ค„์”ฉ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•œ ์ค„์”ฉ ์“ธ ๋•Œ๋„ write()์™€ writelines() ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. write() # ๊ฐ ์ค„๋งˆ๋‹ค ์ž…๋ ฅํ•  ๋‚ด์šฉ์„ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ lines = ['Hello, world!', 'Hello, again!'] with open('example.txt', 'w') as file: for line in lines: # ์ €์žฅ๋œ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ์— ์ ‘๊ทผํ•˜์—ฌ ์•„๋ž˜ ์ž‘์—…์„ ์‹คํ–‰ file.write(f"{line}\n") # ๋‚ด์šฉ์„ ํŒŒ์ผ์— ์“ฐ๊ณ  ์ค„ ๋ฐ”๊ฟˆ ์ž‘์„ฑ๋œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์•ž์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์œˆ๋„ type ๋ช…๋ น์œผ๋กœ ํŒŒ์ผ ๋‚ด์šฉ์„ ํ‘œ์‹œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. !type C:\user\example.txt # ์ถœ๋ ฅ๊ฐ’ Hello, world! Hello, again! ํ•œ ์ค„์”ฉ ์“ฐ๊ธฐ ์œ„ํ•ด์„œ ๋จผ์ € ๊ฐ ๋ผ์ธ์— ๋“ค์–ด๊ฐˆ ๋‚ด์šฉ์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ for ๋ฌธ์œผ๋กœ ๋ฐ˜๋ณตํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ํŒŒ์ผ์— ์“ฐ๊ณ  ์ค„ ๋ฐ”๊ฟˆ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•œ ์ค„์”ฉ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด ํฌ๋ฉ”์ดํŒ…์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. writelines() writelines()๋Š” readlines()์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ์ค„์„ ํ•œ ๋ฒˆ์— ์ž‘์—…ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ๋ฌธ์ž์—ด์— ๊ฐœํ–‰๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # ๊ฐ ์ค„๋งˆ๋‹ค ์ž…๋ ฅํ•  ๋‚ด์šฉ์„ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ lines = ['Hello, world!\n', 'Hello, again!'] with open('example.txt', 'w') as file: file.writelines(lines) # ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ๋œ lines ์ „์ฒด๋ฅผ ํŒŒ์ผ์— ์“ฐ๊ธฐ # ์œˆ๋„ type ๋ช…๋ น์–ด๋กœ ํŒŒ์ผ์„ ์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ Hello, world! Hello, again! writelines()๋Š” for ๋ฌธ์ด ๋‚˜ ๋‹ค๋ฅธ ์กฐ๊ฑด๋ฌธ์ด ๋ณ„๋„๋กœ ํ•„์š”ํ•˜์ง€ ์•Š์•„ ๋” ํŽธํ•˜๊ฒŒ ํŒŒ์ผ ์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์•ž์„œ ์„ค๋ช…ํ•œ ๋ฐ”์™€ ๊ฐ™์ด ๊ฐœํ–‰๋ฌธ์ž๊ฐ€ ์ด๋ฏธ ๊ฐ ๋ฌธ์ž์—ด์˜ ๋’ค์— ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ๋’ค์— ๋ถ™์—ฌ์ฃผ๋Š” ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ๋จผ์ € ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 'r+'๋ชจ๋“œ์™€ seek ํ•จ์ˆ˜ r+๋ชจ๋“œ๋กœ ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋ฉด ์ฝ๊ธฐ์™€ ์“ฐ๊ธฐ๊ฐ€ ๋ชจ๋‘ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“œ๋ผ๋Š” ๊ฒƒ์„ ์•ž์—์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. r+๋ชจ๋“œ๋กœ ํŒŒ์ผ์„ ์—ด ๋•Œ๋Š” ํ˜„์žฌ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ๋กœ seek() ํ•จ์ˆ˜๋ฅผ ํ•จ๊ป˜ ์“ฐ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ์™€ ์“ธ ๋•Œ ๋ชจ๋‘ ํŒŒ์ผ ํฌ์ธํ„ฐ๊ฐ€ ๋ณ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž์นซ ์ƒ๊ฐ๊ณผ ๋‹ค๋ฅธ ์œ„์น˜์—์„œ ์ž‘์—…์ด ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, r+๋ชจ๋“œ๋กœ ํŒŒ์ผ์„ ์—ด๋ฉด, ํŒŒ์ผ ํฌ์ธํ„ฐ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํŒŒ์ผ์˜ ์‹œ์ž‘ ์œ„์น˜์— ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค. ์ด ์ƒํƒœ ๊ทธ๋Œ€๋กœ write()๋กœ ์“ฐ๊ธฐ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋ฉด, ๊ธฐ์กด ํŒŒ์ผ์˜ ๋‚ด์šฉ์ด ๋ฎ์–ด์“ฐ์ผ ์œ„ํ—˜์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์œ„ํ—˜์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด seek()์œผ๋กœ ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•œ ๋‹ค์Œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋ฉด ์•ˆ์ „ํ•˜๊ฒŒ ์ฝ๊ธฐ์™€ ์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. seek() ํ•จ์ˆ˜๋Š” ํŒŒ์ผ ๋‚ด ํŠน์ • ์œ„์น˜๋กœ ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค. r+๋ชจ๋“œ์—์„œ seek() ํ•จ์ˆ˜๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ์˜ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. with open('example.txt', 'r+') as f: content = f.read() # ํŒŒ์ผ์˜ ๋ชจ๋“  ๋‚ด์šฉ์„ ์ฝ๊ธฐ ( ์ฝ๊ธฐ โ‘  ) print('Before:', content) f.seek(0) # ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ํŒŒ์ผ ์‹œ์ž‘ ๋ถ€๋ถ„์œผ๋กœ ์ด๋™ f.write('We can change it. \n') # ํŒŒ์ผ ๋‚ด์šฉ ๋ฎ์–ด์“ฐ๊ธฐ ( ์“ฐ๊ธฐ ) content = f.read() # ํ˜„์žฌ ์œ„์น˜์—์„œ ํŒŒ์ผ ์ฝ๊ธฐ ( ์ฝ๊ธฐ โ‘ก ) print('After:', content) f.seek(0) # ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ํŒŒ์ผ ์‹œ์ž‘ ๋ถ€๋ถ„์œผ๋กœ ์ด๋™ content = f.read() # ํ˜„์žฌ ์œ„์น˜์—์„œ ๋‹ค์‹œ ํŒŒ์ผ ์ฝ๊ธฐ ( ์ฝ๊ธฐ โ‘ข ) print('Final:', content) # ๊ฒฐ๊ด๊ฐ’ Before: Hello, world! Hello, again! After: o, again! Final: We can change it. o, again! ์ฝ”๋“œ์—์„œ ์ฝ๊ธฐ๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ช…ํ™•ํ•œ ์„ค๋ช…์„ ์œ„ํ•ด '์ฝ๊ธฐ โ‘  / โ‘ก / โ‘ข'์œผ๋กœ ๊ตฌ๋ถ„ ์ง“๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ํŒŒ์ผ์„ r+ ๋ชจ๋“œ๋กœ ์—ด์–ด์„œ ์ „์ฒด ๋‚ด์šฉ์„ ์ฝ์–ด์˜ค๋Š” ์ฝ๊ธฐ โ‘ ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ด๊ฐ’์—์„œ "Before: " ๋’ท๋ถ€๋ถ„์— ์ถœ๋ ฅ๋œ ๋‚ด์šฉ์„ ๋ณด๋ฉด ๊ธฐ์กด ํŒŒ์ผ ์ „์ฒด ๋‚ด์šฉ์ธ "Hello, world!(์ค„๋ฐ”๊ฟˆ) Hello, again!"์ด ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ ํŒŒ์ผ์— "We can change it."์ด๋ผ๋Š” ๋‚ด์šฉ์„ ์“ฐ๋Š”๋ฐ ํŒŒ์ผ์˜ ์•ž๋ถ€๋ถ„๋ถ€ํ„ฐ ๋ฎ์–ด์“ฐ๊ธฐ ์œ„ํ•ด ๋‹ค์‹œ ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ์‹œ์ž‘ ๋ถ€๋ถ„์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํŒŒ์ผ ๋์— ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, ์ฝ๊ธฐ โ‘  ์ด ๋๋‚œ ๋‹ค์Œ ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜์ง€ ์•Š์€ ์ฑ„๋กœ ์“ฐ๊ธฐ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ์“ฐ๊ธฐ๊ฐ€ ๋๋‚œ ๋‹ค์Œ ์ฝ๊ธฐ โ‘ก๋ฅผ ์‹คํ–‰ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋ฎ์–ด์“ด ๋‚ด์šฉ์„ ํฌํ•จํ•œ ์ „์ฒด ๋‚ด์šฉ์ด ๋‹ค์‹œ ์ถœ๋ ฅ๋  ๊ฒƒ ๊ฐ™์ง€๋งŒ, "After: " ๋’ค์— ์ถœ๋ ฅ๋œ ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด ๋ฎ์–ด์“ฐ๊ณ  ๋‚œ ๋’ท๋ถ€๋ถ„์ธ "o, again!"๋งŒ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์“ฐ๊ธฐ๊ฐ€ ๋๋‚œ ๋’ค ํŒŒ์ผ ํฌ์ธํ„ฐ์˜ ํ˜„์žฌ ์œ„์น˜์—์„œ read()๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. seek(0)์œผ๋กœ ๋‹ค์‹œ ํŒŒ์ผ ํฌ์ธํ„ฐ๋ฅผ ์‹œ์ž‘ ์œ„์น˜๋กœ ์ด๋™ํ•œ ํ›„ ์ฝ๊ธฐ โ‘ข์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. "Final: " ๋’ค์˜ ์ตœ์ข… ์ถœ๋ ฅ๊ฐ’์ด "We can change it.(์ค„๋ฐ”๊ฟˆ) o, again!"์œผ๋กœ, ์“ฐ๊ธฐ ์ž‘์—… ํ›„์˜ ์ „์ฒด ๋‚ด์šฉ์ด ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด r+๋ชจ๋“œ๋กœ ์ž‘์—…ํ•  ๋•Œ๋Š” seek() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ž‘์—…ํ•  ์œ„์น˜๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ์‹œ์ž‘ ์œ„์น˜๋กœ ์ด๋™์„ ์œ„ํ•ด seek(0)์„ ํ˜ธ์ถœํ–ˆ์Šต๋‹ˆ๋‹ค. '0'์€ ์ ˆ๋Œ“๊ฐ’์œผ๋กœ ์–ธ์ œ๋‚˜ ํŒŒ์ผ์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. seek()์€ ์ธ์ž ๊ฐ’์œผ๋กœ ์ˆซ์ž๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š”๋ฐ, ๋งŒ์ผ ์‹œ์ž‘์ด ์•„๋‹Œ ์ค‘๊ฐ„ ์ง€์ ์œผ๋กœ ์ด๋™ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ๊ทธ ์œ„์น˜๋กœ ๊ฐ€๊ธฐ ์œ„ํ•ด ์–ผ๋งˆํผ ์ด๋™ํ•ด์•ผ ํ•˜๋Š”์ง€ ์ˆซ์ž๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ์˜ ์ˆซ์ž๋Š” ์‚ฌ๋žŒ์ด ์„ธ๋Š” ๊ธ€์ž ์ˆ˜์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์ปดํ“จํ„ฐ๊ฐ€ ์ฝ๋Š” ๋‹จ์œ„์ธ ๋ฐ”์ดํŠธ(byte)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ˜„์žฌ ํฌ์ธํ„ฐ์˜ ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ช‡ ๋ฐ”์ดํŠธ ๋–จ์–ด์ง„ ๊ณณ์œผ๋กœ ์ด๋™ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์ปดํ“จํ„ฐ์—๊ฒŒ ์•Œ๋ ค์ค˜์•ผ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜์–ด๋‚˜ ๊ณต๋ฐฑ์€ ์ž๋ฆฟ์ˆ˜ ํ•˜๋‚˜๋‹น 1๋ฐ”์ดํŠธ๋กœ ๋น„๊ต์  ๊ณ„์‚ฐํ•˜๊ธฐ ์šฉ์ดํ•˜์ง€๋งŒ, ํ•œ๊ธ€์€ ๊ธ€์ž๋‹น 3๋ฐ”์ดํŠธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ณ„์‚ฐ์ด ์กฐ๊ธˆ ๋” ๋ณต์žกํ•ด์ง‘๋‹ˆ๋‹ค. ์œ„์น˜๋ฅผ ์ž˜๋ชป ์ด๋™ํ•œ ์ฑ„๋กœ ์“ฐ๊ธฐ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋ฉด ๋‚ด์šฉ์ด ๋ฎ์–ด์“ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๊ฐ„์ง€์ ์œผ๋กœ ์ด๋™ํ•  ๋•Œ๋Š” ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 03-03. csv ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ csv ํŒŒ์ผ์€ Comma-separated Values์˜ ์•ฝ์ž๋กœ ์ฆ‰, ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„๋œ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. ์—‘์…€๊ณผ ๊ฐ™์ด ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ํŒŒ์ผ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉด์„œ๋„,<NAME>์ด ๊ฐ„๋‹จํ•˜๊ณ  ํŒŒ์ผ์˜ ํฌ๊ธฐ๋„ ์ž‘์•„ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ค„์•ผ ํ•˜๋Š” ๊ณต๊ณต๊ธฐ๊ด€์ด๋‚˜ ๊ธฐ์—…์—์„œ๋„ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ผ<NAME>์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„, ๋‚˜์ด, ์ง์—… ๋ฐ•์€์˜, 30, ์—”์ง€๋‹ˆ์–ด ๊น€์„ธ ๋น›, 25, ๋””์ž์ด๋„ˆ ์•ˆํฌ์ˆ˜,35,์˜์‚ฌ ์ •ํ˜„์šฑ, 40, ์„ ์ƒ๋‹˜ ๊ฐ•์ฐฌ์˜,22,ํ•™์ƒ ์œ„์˜ csv ํŒŒ์ผ ์˜ˆ์‹œ์—์„œ ์ฝค๋งˆ(,)๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ์ค„๋กœ ์น˜ํ™˜ํ•˜์—ฌ ์ƒ๊ฐํ•˜๋ฉด ํ‘œ์™€ ๋™์ผํ•œ ํ˜•ํƒœ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ csv ํŒŒ์ผ์€ ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ์ž‘์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์—‘์…€ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋„ csv ํŒŒ์ผ์„ ์—ด๊ณ  ์ถ”๊ฐ€์ ์ธ ๋ฐ์ดํ„ฐ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋‹จ์ˆœ ํ…์ŠคํŠธ ํŒŒ์ผ์ด๊ธฐ ๋•Œ๋ฌธ์— ์—‘์…€ ํ”„๋กœ๊ทธ๋žจ ์™ธ์—๋„ ๋ชจ๋“  ๋‹ค๋ฅธ ํ…์ŠคํŠธ ํŽธ์ง‘๊ธฐ์—์„œ๋„ ํŒŒ์ผ์„ ์—ด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ openpyxl์ด๋‚˜ pandas๋กœ ์—‘์…€ ํŒŒ์ผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์€ ๋’ค์—์„œ ์ž์„ธํžˆ ๋ฐฐ์šธ ์˜ˆ์ •์ด๊ธฐ ๋•Œ๋ฌธ์—, ์—ฌ๊ธฐ์„œ๋Š” csv ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ๊ธฐ๋ณธ ๋ฐฉ๋ฒ•๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. csv ํŒŒ์ผ ์“ฐ๊ธฐ import csv # csv ํŒŒ์ผ์„ ์“ฐ๊ธฐ ๋ชจ๋“œ 'w'๋กœ ์—ด๊ธฐ(์ธ์ฝ”๋”ฉ 'cp949', ๋ผ์ธ ์ข…๋ฃŒ ๋ฌธ์ž ์„ค์ • 'newline = '') with open('example.csv', 'w', encoding='cp949', newline='') as file: csv_writer = csv.writer(file) # ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ csv.writer ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ csv_writer.writerow(['์ด๋ฆ„', '๋‚˜์ด', '์ง์—…']) # ํ—ค๋” ์ž‘์„ฑ csv_writer.writerow(['๋ฐ•์€์˜', 30, '์—”์ง€๋‹ˆ์–ด']) # ๋ฐ์ดํ„ฐ ํ–‰ ์ถ”๊ฐ€ csv_writer.writerow(['๊น€์„ธ ๋น›', 25, '๋””์ž์ด๋„ˆ']) csv_writer.writerow(['์•ˆํฌ์ˆ˜', 35, '์˜์‚ฌ']) csv_writer.writerow(['์ •ํ˜„์šฑ', 40, '์„ ์ƒ๋‹˜']) csv_writer.writerow(['๊ฐ•์ฐฌ์˜', 22, 'ํ•™์ƒ']) # ์œˆ๋„ type ๋ช…๋ น์–ด๋กœ ํŒŒ์ผ์„ ์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ ์ด๋ฆ„, ๋‚˜์ด, ์ง์—… ๋ฐ•์€์˜, 30, ์—”์ง€๋‹ˆ์–ด ๊น€์„ธ ๋น›, 25, ๋””์ž์ด๋„ˆ ์•ˆํฌ์ˆ˜,35,์˜์‚ฌ ์ •ํ˜„์šฑ, 40, ์„ ์ƒ๋‹˜ ๊ฐ•์ฐฌ์˜,22,ํ•™์ƒ csv ํŒŒ์ผ์„ ์—ด ๋•Œ๋„ ์•ž์„œ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์—ด ๋•Œ์™€ ๊ฐ™์ด open()์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ with ๋ฌธ์œผ๋กœ ํŒŒ์ผ์„ ์—ด์–ด ๋ณ„๋„์˜ ๋‹ซ๊ธฐ close() ๋ช…๋ น ์—†์ด ํŒŒ์ผ ์ž‘์—…์„ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ถˆ๋Ÿฌ์˜จ ํŒŒ์ผ์„ csv.writer ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•œ ๋‹ค์Œ writerrow()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ–‰๋ณ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์˜ ํ˜•ํƒœ๋กœ ์ž…๋ ฅํ•˜๋ฉฐ, ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์˜ ๊ฐ ์š”์†Œ๊ฐ€ csv ํŒŒ์ผ์˜ ํ•˜๋‚˜์˜ ์…€๋กœ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์—ด ๋•Œ newline=''์ด๋ผ๋Š” ๊ฒƒ์ด ์ถ”๊ฐ€๋œ ๊ฒƒ์ด ๋ณด์ž…๋‹ˆ๋‹ค. newline=''์€ ์ค„ ๋ฐ”๊ฟˆ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •ํ•ด์ฃผ๋Š” ์˜ต์…˜์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•ž์„œ 3-2. ํ…์ŠคํŠธ ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ์—์„œ ์šด์˜์ฒด์ œ์— ๋”ฐ๋ผ ๊ฐœํ–‰๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅด๋ฉฐ ์œˆ๋„์—์„œ๋Š” '\r\n'์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์˜ ์ข…๋ฅ˜๋‚˜ ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋งˆ๋‹ค ์‚ฌ์šฉํ•˜๋Š” ๊ฐœํ–‰๋ฌธ์ž๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํŒŒ์ด์ฌ์—์„œ csv ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ ์“ฐ๋Š” csv.writer()๋Š” ์ค„ ๋ฐ”๊ฟˆ์œผ๋กœ '\r\n'์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฐ์ดํ„ฐ์˜ ๊ฐ ํ–‰ ๋’ค์— '\r\n'์ด ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œˆ๋„์—์„œ๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ๋•Œ ํŒŒ์ผ์— ์žˆ๋Š” '\n'์„ ์œˆ๋„์˜ ์ค„๋ฐ”๊ฟˆ ์Šคํƒ€์ผ์ธ '\r\n'์œผ๋กœ ์ž๋™์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด csv.writer()๋กœ ์ž‘์„ฑํ•œ csv ํŒŒ์ผ์˜ ๊ฐ ํ–‰ ๋’ค์˜ '\r\n'๋Š” '\r\r\n'์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ์œˆ๋„์˜ ํ…์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ(๋ฉ”๋ชจ์žฅ)์—์„œ๋Š” ํŒŒ์ผ์ด ์ •์ƒ์ ์œผ๋กœ ๋ณด์ด์ง€๋งŒ, ์—‘์…€๊ณผ ๊ฐ™์€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ ํ”„๋กœ๊ทธ๋žจ์—์„œ ํŒŒ์ผ์„ ์—ด๋ฉด '\r\r\n'์„ ๋‘ ๊ฐœ์˜ ๋‹ค๋ฅธ ํ–‰์œผ๋กœ ํ•ด์„ํ•ด์„œ ๋นˆ ํ–‰์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ๋ฅผ newline ์˜ต์…˜์„ ๋„ฃ์ง€ ์•Š๊ณ  ์‹คํ–‰ํ•˜์—ฌ csv ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ๋’ค ์œˆ๋„ ๋ฉ”๋ชจ์žฅ๊ณผ ์—‘์…€ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๊ฐ๊ฐ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œˆ๋„ ๋ฉ”๋ชจ์žฅ์œผ๋กœ ํŒŒ์ผ ์—ด๊ธฐ ์—‘์…€ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ํŒŒ์ผ ์—ด๊ธฐ ๋™์ผํ•œ ํŒŒ์ผ์ด์ง€๋งŒ ์—‘์…€ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์—ด ๋•Œ๋Š” ๊ฐ ํ–‰ ์‚ฌ์ด์— ๋นˆ ํ–‰์ด ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ถˆํ•„์š”ํ•œ ๊ณต๋ฐฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด csv ํŒŒ์ผ์„ ์—ด ๋•Œ newline=''์„ ์˜ต์…˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜๋ฉด ํŒŒ์ด์ฌ์ด ์šด์˜์ฒด์ œ์— ๋งž๋Š” ์ ์ ˆํ•œ ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž๋ฅผ ์•Œ์•„์„œ ์ž๋™์œผ๋กœ ์‚ฌ์šฉํ•ด ์ค๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŒŒ์ด์ฌ์œผ๋กœ csv ํŒŒ์ผ์— ์ ‘๊ทผํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด newline='' ์˜ต์…˜์„ ๊ธฐ๋ณธ์œผ๋กœ ํ•จ๊ป˜ ๋„ฃ์–ด์ฃผ๋Š” ํŽธ์ด ์ข‹์Šต๋‹ˆ๋‹ค. csv ํŒŒ์ผ ์ฝ๊ธฐ ์ด๋ฒˆ์—๋Š” csv ํŒŒ์ผ์„ ์ฝ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. csv ํŒŒ์ผ์„ ์“ธ ๋•Œ์™€ ๋™์ผํ•˜๊ฒŒ open()์œผ๋กœ ํŒŒ์ผ์„ ์—ด๊ณ  csv.reader()๋กœ ํŒŒ์ผ์„ ์ฝ์–ด์ค๋‹ˆ๋‹ค. import csv # csv ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋ชจ๋“œ 'r'๋กœ ์—ด๊ธฐ(์ธ์ฝ”๋”ฉ 'cp949') with open('example.csv', 'r', encoding='cp949') as file: csv_reader = csv.reader(file) # ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ csv.reader ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ for row in csv_reader: # ํŒŒ์ผ ํ–‰๋ณ„๋กœ ์ฝ๊ธฐ print(row) # ๊ฒฐ๊ด๊ฐ’ ['์ด๋ฆ„', '๋‚˜์ด', '์ง์—…'] ['๋ฐ•์€์˜', '30', '์—”์ง€๋‹ˆ์–ด'] ['๊น€์„ธ ๋น›', '25', '๋””์ž์ด๋„ˆ'] ['์•ˆํฌ์ˆ˜', '35', '์˜์‚ฌ'] ['์ •ํ˜„์šฑ', '40', '์„ ์ƒ๋‹˜'] ['๊ฐ•์ฐฌ์˜', '22', 'ํ•™์ƒ'] csv.reader๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ–‰ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ธฐ ์œ„ํ•ด for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํ–‰๋งˆ๋‹ค ์ ‘๊ทผํ•˜์—ฌ ์ฝ๊ธฐ ์ž‘์—…์„ ์‹คํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ—ค๋” ๊ฑด๋„ˆ๋›ฐ๊ธฐ csv ํŒŒ์ผ์˜ ์ฒซ ํ–‰์— ํ—ค๋”๊ฐ€ ์žˆ์„ ๋•Œ, ์ฒซ ํ–‰์„ ์ œ์™ธํ•˜๊ณ  ํŒŒ์ผ์„ ์ฝ๋Š” ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ csv ํŒŒ์ผ ์ฝ๊ธฐ์—์„œ csv.reader๋Š” ํ–‰ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค๋Š” ๊ฒƒ์„ ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ํ–‰์—์„œ ๋‹ค์Œ ํ–‰์œผ๋กœ ์œ„์น˜๋ฅผ ์ด๋™ํ•˜๋Š” ํŠน์„ฑ์„ ํ™œ์šฉํ•˜์—ฌ ์ฒซ ํ–‰์„ ์ œ์™ธํ•œ ์ฝ๊ธฐ๋ฅผ ์‰ฝ๊ฒŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ next() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ—ค๋”๋ฅผ ์ œ์™ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import csv with open('example.csv', 'r', encoding='cp949') as file: csv_reader = csv.reader(file) next(csv_reader) # ์ฒซ ํ–‰ ๊ฑด๋„ˆ๋›ฐ๊ธฐ for row in csv_reader: # ๋‘ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ํ–‰๋ณ„๋กœ ์ฝ๊ธฐ print(row) # ๊ฒฐ๊ด๊ฐ’ ['๋ฐ•์€์˜', '30', '์—”์ง€๋‹ˆ์–ด'] ['๊น€์„ธ ๋น›', '25', '๋””์ž์ด๋„ˆ'] ['์•ˆํฌ์ˆ˜', '35', '์˜์‚ฌ'] ['์ •ํ˜„์šฑ', '40', '์„ ์ƒ๋‹˜'] ['๊ฐ•์ฐฌ์˜', '22', 'ํ•™์ƒ'] next() ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด์˜ ํ˜„์žฌ ์œ„์น˜์—์„œ ๋‹ค์Œ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์œ„์น˜๋ฅผ ํ•œ ์นธ ์•ž์œผ๋กœ ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค. csv.reader์—์„œ๋„ next() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด ์›ํ•˜๋Š” ํ–‰์„ ๊ฑด๋„ˆ๋›ฐ๊ฑฐ๋‚˜ ํŠน์ • ์œ„์น˜๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์ฝ๊ธฐ ์•ž์—์„œ๋Š” csv ํŒŒ์ผ์„ ํ–‰๋ณ„๋กœ ์ฝ์–ด์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜๋‹ค๋ฉด, ์ด๋ฒˆ์—๋Š” ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์ฝ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. csv.DictReader๋Š” csv ํŒŒ์ผ์„ ์ฝ์–ด์„œ ๊ฐ ํ–‰์„ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๋Š” csv ํŒŒ์ผ์˜ ํ—ค๋”(์ฒซ ๋ฒˆ์งธ ํ–‰)์—์„œ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. with open('example.csv', 'r', encoding='cp949') as file: # csv.DictReader ๊ฐ์ฒด ์ƒ์„ฑ: ํŒŒ์ผ์˜ ์ฒซ ๋ฒˆ์งธ ํ–‰์„ ํ—ค๋”๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋‚˜๋จธ์ง€ ํ–‰์„ ์‚ฌ์ „ ํ˜•ํƒœ๋กœ ์ฝ๊ธฐ csv_dict_reader = csv.DictReader(file) # csv_dict_reader ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ํ–‰์„ ์ถœ๋ ฅ for row in csv_dict_reader: # ํ—ค๋”์ธ '์ด๋ฆ„', '๋‚˜์ด', '์ง์—…'์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํ–‰์˜ ํ•ด๋‹น ์—ด ๊ฐ’์„ ์ถœ๋ ฅ print(row['์ด๋ฆ„'], row['๋‚˜์ด'], row['์ง์—…']) # ๊ฒฐ๊ด๊ฐ’ ๋ฐ•์€์˜ 30 ์—”์ง€๋‹ˆ์–ด ๊น€์„ธ ๋น› 25 ๋””์ž์ด๋„ˆ ์•ˆํฌ์ˆ˜ 35 ์˜์‚ฌ ์ •ํ˜„์šฑ 40 ์„ ์ƒ๋‹˜ ๊ฐ•์ฐฌ์˜ 22 ํ•™์ƒ csv.DictReader๋Š” ์ธ์ž๋กœ ๋ฐ›์€ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์‚ฌ์ „ ํ˜•ํƒœ๋กœ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด(csv_dict_reader)๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. csv.DictReader์˜ ๊ฐ์ฒด๋„ ํ•œ ํ–‰์”ฉ ์ ‘๊ทผํ•˜๋ฉฐ ๋ฐ˜๋ณต ์ž‘์—…์ด ๊ฐ€๋Šฅํ•œ, ์ฆ‰, ๋ฐ˜๋ณต ๊ฐ€๋Šฅ(iterable) ํ•œ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ๋„ for ๋ฌธ์œผ๋กœ ๊ฐ ํ–‰์„ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ํ–‰์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํ–‰์˜ ์—ด๊ฐ’์ด ๊ฐ๊ฐ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์ €์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ ์…€๋ณ„๋กœ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๊ฐ’์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์œ„์—์„œ์ฒ˜๋Ÿผ ํ–‰ ์ „์ฒด๋ฅผ ์ถœ๋ ฅํ•  ๊ฒฝ์šฐ, ๊ฒฐ๊ด๊ฐ’์€ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. with open('example.csv', 'r', encoding='cp949') as file: csv_dict_reader = csv.DictReader(file) for row in csv_dict_reader: print(row) # ๊ฐ ํ–‰ ์ „์ฒด๋ฅผ ์ถœ๋ ฅ # ๊ฒฐ๊ด๊ฐ’ {'์ด๋ฆ„': '๋ฐ•์€์˜', '๋‚˜์ด': '30', '์ง์—…': '์—”์ง€๋‹ˆ์–ด'} {'์ด๋ฆ„': '๊น€์„ธ ๋น›', '๋‚˜์ด': '25', '์ง์—…': '๋””์ž์ด๋„ˆ'} {'์ด๋ฆ„': '์•ˆํฌ์ˆ˜', '๋‚˜์ด': '35', '์ง์—…': '์˜์‚ฌ'} {'์ด๋ฆ„': '์ •ํ˜„์šฑ', '๋‚˜์ด': '40', '์ง์—…': '์„ ์ƒ๋‹˜'} {'์ด๋ฆ„': '๊ฐ•์ฐฌ์˜', '๋‚˜์ด': '22', '์ง์—…': 'ํ•™์ƒ'} ๊ฒฐ๊ด๊ฐ’์ด ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ DictReader๋กœ ํŒŒ์ผ์„ ์ฝ์œผ๋ฉด ์—ด ์ด๋ฆ„(ํ—ค๋”)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํŒŒ์ผ ์ž‘์—…์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ด ์ˆœ์„œ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด๋„ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ์—ด์„ ์ถ”์ถœํ•˜์—ฌ ์„ ํƒ์ ์œผ๋กœ ์ž‘์—…ํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๊ฐ€ ์•„๋‹Œ ์—ด ์ด๋ฆ„์ด ์‚ฌ์šฉ๋˜์–ด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๋„ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๊ตฌ๋ถ„์ž๋กœ ์ฝ๊ธฐ/์“ฐ๊ธฐ ๋ฐ์ดํ„ฐ๊ฐ€ ์ฝค๋งˆ(,)๋กœ ๊ตฌ๋ถ„๋œ csv ํŒŒ์ผ ์™ธ์—๋„ tab์œผ๋กœ ๊ตฌ๋ถ„๋œ tsv ํŒŒ์ผ(Tab-Separated Values)๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝค๋งˆ๊ฐ€ ํƒญ์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค๋Š” ์  ์™ธ์—๋Š” ํŒŒ์ผ์˜<NAME>์ด ์œ ์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— tsv ํŒŒ์ผ๋„ ํŒŒ์ด์ฌ์—์„œ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # 'example.tsv' ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋ชจ๋“œ('r')์™€ cp949 ์ธ์ฝ”๋”ฉ์œผ๋กœ ์—ด๊ธฐ with open('example.tsv', 'r', encoding='cp949') as file: # csv.reader ๊ฐ์ฒด ์ƒ์„ฑ, ๊ตฌ๋ถ„์ž๋ฅผ ํƒญ('\t')์œผ๋กœ ์ง€์ • csv_reader = csv.reader(file, delimiter='\t') # csv_reader ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ํ–‰์„ ์ถœ๋ ฅ for row in csv_reader: print(row) #๊ฒฐ๊ด๊ฐ’ ['๋ฐ•์€์˜', '30', '์—”์ง€๋‹ˆ์–ด'] ['๊น€์„ธ ๋น›', '25', '๋””์ž์ด๋„ˆ'] ['์•ˆํฌ์ˆ˜', '35', '์˜์‚ฌ'] ['์ •ํ˜„์šฑ', '40', '์„ ์ƒ๋‹˜'] ['๊ฐ•์ฐฌ์˜', '22', 'ํ•™์ƒ'] ์ฝ”๋“œ๋Š” csv ํŒŒ์ผ๊ณผ ๋™์ผํ•˜์ง€๋งŒ ๊ตฌ๋ถ„์ž(delimiter)๋ฅผ ํƒญ('\t')์œผ๋กœ ์ง€์ •ํ•˜๋ฉด tsv ํŒŒ์ผ ์ž‘์—…์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ํŒŒ์ผ์„ ์ฝ๋Š” csv.reader๋ฅผ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, ํŒŒ์ผ์„ ์“ธ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” csv.writer()์—๋„ ๊ตฌ๋ถ„์ž(delimiter)๋ฅผ ์ง€์ •ํ•˜์—ฌ ์ฝค๋งˆ(,)๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ตฌ๋ถ„์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŒŒ์ผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ ๋ฌธ์ž๋กœ ์ฝ๊ธฐ/์“ฐ๊ธฐ csv ํŒŒ์ผ์—์„œ ๋งŒ์•ฝ ์ฝค๋งˆ๊ฐ€ ๊ตฌ๋ถ„์ž๊ฐ€ ์•„๋‹Œ ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์œผ๋กœ ์“ฐ์˜€๋‹ค๋ฉด, ํ•ด๋‹น ์ฝค๋งˆ๊ฐ€ ๊ตฌ๋ถ„์ž๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๋Š” ํ‘œ์‹œ๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. csv.reader๋‚˜ csv.writer๋Š” ๊ทธ ํ‘œ์‹œ๋กœ ํฐ๋”ฐ์˜ดํ‘œ(")๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํฐ๋”ฐ์˜ดํ‘œ ์‚ฌ์ด์— ์žˆ๋Š” ์ฝค๋งˆ๋Š” ๋ฌธ์ž๋กœ ๊ฐ„์ฃผํ•˜์—ฌ ์ฒ˜๋ฆฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ž‘์„ฑ๋œ csv ํŒŒ์ผ์ด ๊ทธ ํ‘œ์‹œ๋ฅผ ํฐ๋”ฐ์˜ดํ‘œ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์ผ๋‹ค๋ฉด ๊ทธ ์‚ฌ์‹ค์„ csv.reader๋‚˜ csv.writer์—๊ฒŒ ์ „๋‹ฌํ•ด ์ค˜์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์˜ต์…˜์ด 'quotechar'์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ์˜ˆ์‹œ์—์„œ ํ™•์ธํ•ด ๋ณด๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ 'example2.csv' ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # example2.csv' ํŒŒ์ผ ์ด๋ฆ„, ๋‚˜์ด, ์ง์—… ๋ฐ•์€์˜, 30, ์—”์ง€๋‹ˆ์–ด ๊น€์„ธ ๋น›, 25, '๋””์ž์ด๋„ˆ, ์ธํ”Œ๋ฃจ์–ธ์„œ' ์•ˆํฌ์ˆ˜,35,'์˜์‚ฌ, ํ”„๋กœ๊ทธ๋ž˜๋จธ' ์ •ํ˜„์šฑ, 40, ์„ ์ƒ๋‹˜ ๊ฐ•์ฐฌ์˜,22,ํ•™์ƒ ์ง์—…์ด 2๊ฐœ์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด์„œ ๊ทธ ๋‚ด์šฉ์ด ์ฝค๋งˆ(,)๋กœ ๋‚˜์—ด๋˜์–ด ์žˆ์œผ๋ฉฐ, 3๋ฒˆ์งธ์™€ 4๋ฒˆ์งธ ํ–‰์—์„œ ์ด๋ ‡๊ฒŒ ์ฝค๋งˆ๊ฐ€ ํฌํ•จ๋œ ์ง์—… ์—ด์˜ ๊ฐ’ ์ „์ฒด๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๊ฐ„์ฃผํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ ๊ทธ ๋‚ด์šฉ์ด ์ž‘์€๋”ฐ์˜ดํ‘œ(')๋กœ ๋ฌถ์—ฌ์žˆ์Šต๋‹ˆ๋‹ค. quotechar์„ ๋ณ„๋„๋กœ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  csv ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. with open('example2.csv', 'r', encoding='cp949') as file: # csv.reader ๊ฐ์ฒด ์ƒ์„ฑ csv_reader = csv.reader(file) for row in csv_reader: print(row) # ๊ฒฐ๊ด๊ฐ’ ['์ด๋ฆ„', '๋‚˜์ด', '์ง์—…'] ['๋ฐ•์€์˜', '30', '์—”์ง€๋‹ˆ์–ด'] ['๊น€์„ธ ๋น›', '25', "'๋””์ž์ด๋„ˆ", " ์ธํ”Œ๋ฃจ์–ธ์„œ'"] ['์•ˆํฌ์ˆ˜', '35', "'์˜์‚ฌ", " ํ”„๋กœ๊ทธ๋ž˜๋จธ'"] ['์ •ํ˜„์šฑ', '40', '์„ ์ƒ๋‹˜'] ['๊ฐ•์ฐฌ์˜', '22', 'ํ•™์ƒ'] ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด ์„ธ ๋ฒˆ์งธ์™€ ๋„ค ๋ฒˆ์งธ ํ–‰์— ์žˆ๋˜ 2๊ฐœ์˜ ์ง์—…์ด ๊ฐ๊ฐ์˜ ์…€๋กœ ๋‚˜๋‰˜์–ด ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฝค๋งˆ(,)๊ฐ€ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ตฌ๋ถ„์ž๋กœ ๊ฐ„์ฃผ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์ž‘์€๋”ฐ์˜ดํ‘œ(')๊ฐ€ ๋ฌธ์ž์—ด๋กœ ์ธ์‹๋˜์–ด ์ง์—… ๊ฐ’์˜ ์•ž๋’ค์— ํฌํ•จ๋˜์–ด ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด๋ฒˆ์—๋Š” ๋™์ผํ•œ ์ฝ”๋“œ์—์„œ quotechar ์˜ต์…˜์œผ๋กœ ์ž‘์€๋”ฐ์˜ดํ‘œ๋ฅผ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. with open('example2.csv', 'r', encoding='cp949') as file: # csv.reader ๊ฐ์ฒด ์ƒ์„ฑ( ๋”ฐ์˜ดํ‘œ ๋ฌธ์ž๋ฅผ ์ž‘์€๋”ฐ์˜ดํ‘œ(')๋กœ ์„ค์ •) csv_reader = csv.reader(file, quotechar="'") for row in csv_reader: print(row) # ๊ฒฐ๊ด๊ฐ’ ['์ด๋ฆ„', '๋‚˜์ด', '์ง์—…'] ['๋ฐ•์€์˜', '30', '์—”์ง€๋‹ˆ์–ด'] ['๊น€์„ธ ๋น›', '25', '๋””์ž์ด๋„ˆ, ์ธํ”Œ๋ฃจ์–ธ์„œ'] ['์•ˆํฌ์ˆ˜', '35', '์˜์‚ฌ, ํ”„๋กœ๊ทธ๋ž˜๋จธ'] ['์ •ํ˜„์šฑ', '40', '์„ ์ƒ๋‹˜'] ['๊ฐ•์ฐฌ์˜', '22', 'ํ•™์ƒ'] ์—ฌ๊ธฐ์„œ๋Š” ์„ธ ๋ฒˆ์งธ, ๋„ค ๋ฒˆ์งธ ํ–‰์˜ ์ง์—… ๊ฐ’์ด ์ž‘์€ ๋‹ค๋ฅธ ํ–‰๊ณผ ๋™์ผํ•˜๊ฒŒ ํ•˜๋‚˜์˜ ์…€๋กœ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ ๋ฌธ์ž์—ด๋กœ ์ธ์‹๋˜์—ˆ๋˜ ์ž‘์€๋”ฐ์˜ดํ‘œ๋„ ์‚ฌ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ๊ธฐ๋ณธ ํฐ๋”ฐ์˜ดํ‘œ(")๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌธ์ž์—ด ํ‘œ์‹œ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒฝ์šฐ quotechar ์˜ต์…˜์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” csv.reader๋กœ ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ csv.writer๋กœ ํŒŒ์ผ์„ ์“ธ ๋•Œ๋„ ๋™์ผํ•œ ์˜ต์…˜์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ํŒŒ์ด์ฌ์˜ csv ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ csv ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋น„๊ต์  ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ ์ž‘์—…์ธ ๊ฒฝ์šฐ์—๋Š” csv ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด csv ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด๋ณด๋‹ค ๋” ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” ๋’ค์—์„œ ๋ฐฐ์šธ pandas์™€ ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋” ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 04. ์—‘์…€(Excel) ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ(openpyxl) ์‹ค์ œ ์—…๋ฌด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ๋‹ค๋ฃจ๋Š” ํŒŒ์ผ<NAME> ์ค‘ ํ•˜๋‚˜์ธ ์—‘์…€ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด์ฌ์œผ๋กœ ์ž‘์—…ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—‘์…€์€ ์ˆ˜๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ฆฌํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์—ฐ์‚ฐ ์ž‘์—…, ์ฐจํŠธ ๋ฐ ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ ๋“ฑ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ํŠนํ™”๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ ์ž‘์—…์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์—‘์…€์—๋„ ์•„์‰ฌ์šด ์ ๋“ค์ด ์žˆ๋Š”๋ฐ โ‘ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ฑฐ๋‚˜ ๋ณต์žกํ•œ ์ž‘์—…์„ ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ๋Š๋ ค์ง€๋ฉฐ, โ‘ก์—‘์…€ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ์—‘์…€ ํ”„๋กœ๊ทธ๋žจ์„ ์—ด์–ด์•ผ ํ•˜๋ฉฐ, โ‘ข์—ฌ๋Ÿฌ ๊ฐœ์˜ ์—‘์…€ ํŒŒ์ผ์„ ๋™์‹œ์— ์ž‘์—…ํ•˜๊ธฐ ์–ด๋ ต๋‹ค๋Š” ์  ๋“ฑ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์œผ๋กœ ์—‘์…€ ์ž‘์—…์„ ํ•  ๊ฒฝ์šฐ ์ด๋Ÿฌํ•œ ๋‹จ์ ๋“ค์ด ๋ณด์™„๋˜์–ด ์ž‘์—…์ด ๋” ๋น ๋ฅด๊ณ  ํŽธ๋ฆฌํ•ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—๋Š” Pandas, OpenPyXL, xlrd ๋“ฑ ์—‘์…€์„ ๋‹ค๋ฃจ๋Š” ๋‹ค์–‘ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์œผ๋ฉฐ, ์ด๋Ÿฌํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ ๋ฐ ๋ถ„์„ ์ž‘์—…์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜๋ฐฑ ๊ฐœ์˜ ์—‘์…€ ํŒŒ์ผ์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๊ฑฐ๋‚˜, ํŠน์ • ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœํ•˜๋Š” ๋“ฑ์˜ ๋ฐ˜๋ณต ์ž‘์—…์€ ์ฝ”๋“œ ๋ช‡ ์ค„๋กœ ์ž๋™ํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋˜ํ•œ ๋ฉ”๋ชจ๋ฆฌ์˜ ํ•œ๊ณ„ ๋‚ด์—์„œ๋Š” ๊ฑฐ์˜ ์ œํ•œ ์—†์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž‘์—…ํ•  ๋•Œ ์—‘์…€์—์„œ๋ณด๋‹ค ๋” ๋น ๋ฅธ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ ์—‘์…€์„ ์ง€์›ํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ์—ฌ๊ธฐ์„œ๋Š” ์—‘์…€ ํŒŒ์ผ์˜ ์ฝ๊ธฐ/์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ณ  ์—ฌ๋Ÿฌ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ๋„ ์ œ๊ณต๋˜์–ด ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” OpenPyXL ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ถ€๋ถ„์—์„œ Pandas๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์ถ”๊ฐ€๋กœ ์‚ดํŽด๋ณผ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. OpenPyXL์€ ์—‘์…€ 2010 ์ดํ›„ ๋ฒ„์ „์˜ ํŒŒ์ผ๋ถ€ํ„ฐ ์ง€์›ํ•˜๋ฉฐ, 2010๋ณด๋‹ค ์ด์ „ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ XlsxWriter๋‚˜ xlrd/xlwt ๋“ฑ ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 04-01. ์—‘์…€ ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์—‘์…€ ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ์…€ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ ์ž…๋ ฅํ•˜๊ธฐ ๋จผ์ € OpenPyXL ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install openpyxl ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด ์ด์ œ ๋‹ค์Œ์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์—‘์…€ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # openpyxl ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Workbook์„ import from openpyxl import Workbook # Workbook ๊ฐ์ฒด ์ƒ์„ฑ wb= Workbook() # ํ˜„์žฌ ํ™œ์„ฑํ™”๋œ ์›Œํฌ์‹œํŠธ ์„ ํƒ ํ›„ ws ๋ณ€์ˆ˜์— ํ• ๋‹น ws = wb.active #์‹œํŠธ ์ œ๋ชฉ์„ '์ˆ˜๊ฐ•์ƒ_์ •๋ณด'๋กœ ๋ณ€๊ฒฝ ws.title = "์ˆ˜๊ฐ•์ƒ_์ •๋ณด" #์‹œํŠธ์˜ A1 ์…€์— '์ด์ฒ ์ˆ˜'๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ ws['A1'] = '์ด์ฒ ์ˆ˜' # ์›Œํฌ๋ถ์„ '์ˆ˜๊ฐ•์ƒ_๋ฆฌ์ŠคํŠธ. xlsx' ์—‘์…€ ํŒŒ์ผ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค wb.save("์ˆ˜๊ฐ•์ƒ_๋ฆฌ์ŠคํŠธ. xlsx") #์›Œํฌ๋ถ ๋‹ซ๊ธฐ wb.close() ์œ„์˜ ์ฝ”๋“œ๋Š” ์ƒˆ๋กœ์šด ์—‘์…€ ์›Œํฌ๋ถ์„ ์ƒ์„ฑํ•˜์—ฌ '์ˆ˜๊ฐ•์ƒ_์ •๋ณด'๋ผ๋Š” ์›Œํฌ์‹œํŠธ์˜ A1์— '์ด์ฒ ์ˆ˜'์ด๋ผ๊ณ  ์ž…๋ ฅํ•œ ํ›„, "์ˆ˜๊ฐ•์ƒ_๋ฆฌ์ŠคํŠธ. xlsx"๋ผ๋Š” ์—‘์…€ ํŒŒ์ผ๋กœ ์ €์žฅํ•œ๋‹ค๋Š” ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด, ํŒŒ์ด์ฌ์„ ์‹คํ–‰ํ•œ ์œ„์น˜์—์„œ "์ˆ˜๊ฐ•์ƒ_๋ฆฌ์ŠคํŠธ"๋ผ๋Š” ์ด๋ฆ„์˜ ์—‘์…€ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ–‰ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ ์ž…๋ ฅํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์‹œํŠธ์— ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ  ์ƒˆ๋กœ์šด ์‹œํŠธ๋„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import Workbook wb = Workbook() ws = wb.active ws.title = "์ˆ˜๊ฐ•์ƒ_์ •๋ณด" # ์‹œํŠธ์— ์ถ”๊ฐ€ํ•  ์นผ๋Ÿผ์˜ ๋ชฉ๋ก์„ ๋ฆฌ์ŠคํŠธํ˜•์œผ๋กœ 'column'์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ์ง€์ • column = ['๋ฒˆํ˜ธ', '์ด๋ฆ„', '๊ณผ๋ชฉ'] # column ๋ฆฌ์ŠคํŠธ ๋ชฉ๋ก์„ ์‹œํŠธ ์ฒซ ํ–‰์— ์ž…๋ ฅ ws.append(column) # ์‹œํŠธ์— ์ถ”๊ฐ€ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธํ˜•์œผ๋กœ 'row'๋ผ๋Š” ๋ณ€์ˆ˜์— ์ง€์ • row = [1, '์ด์ฒ ์ˆ˜', '์ˆ˜ํ•™'] # append๋กœ row์˜ ๋ชฉ๋ก์„ column ์•„๋ž˜ ํ–‰์— ์ž…๋ ฅ ws.append(row) # '์ค‘๊ฐ„ํ‰๊ฐ€', '๊ธฐ๋ง ํ‰๊ฐ€' ๋‘ ๊ฐœ์˜ ์‹œํŠธ ์ถ”๊ฐ€ wb.create_sheet('์ค‘๊ฐ„ํ‰๊ฐ€') wb.create_sheet('๊ธฐ๋ง ํ‰๊ฐ€') wb.save("์ˆ˜๊ฐ•์ƒ_๋ฆฌ์ŠคํŠธ. xlsx") wb.close() ์ด๋ฒˆ์—๋Š” ์‹œํŠธ์— ์นผ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๊ณ , ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ(1, '์ด์ฒ ์ˆ˜', '์ˆ˜ํ•™')๋ฅผ ํ•œ ๋ฒˆ์— ์‹œํŠธ์— ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํ•˜๋‚˜๋ฅผ ์—‘์…€ ํŠน์ • ์…€์— ์ž…๋ ฅํ•  ๋•Œ๋Š” ์…€์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•ด์„œ ws['A1'] = '์ด์ฒ ์ˆ˜' ์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ–ˆ์œผ๋‚˜, ํ•œ ํ–‰์— ์ž…๋ ฅํ•  ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธํ˜•์œผ๋กœ ๋งŒ๋“ค์–ด์ค€ ํ›„, append ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. append๋Š” ์‹œํŠธ์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ํ–‰ ๋‹ค์Œ๋ถ€ํ„ฐ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋งŒ์•ฝ ์—ฌ๋Ÿฌ ํ–‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ์ž…๋ ฅํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, ๊ฐ ํ–‰์— ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธํ˜•์œผ๋กœ ๋งŒ๋“ค๊ณ  ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ๋“ค์„ ํฌํ•จํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“  ํ›„ ๋ฐ˜๋ณตํ•ด์„œ append๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œํŠธ์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. # ๊ฐ ํ–‰์˜ ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด row์— ์ €์žฅ row = [[1, '์ด์ฒ ์ˆ˜', '์ˆ˜ํ•™'], [3, '๊น€๋ฏธ์†Œ', '์˜์–ด'], [2, '์ตœํ•™์ค€', '์ปดํ“จํ„ฐ']] # row ๋ฆฌ์ŠคํŠธ๋ฅผ ์ฒซ ๋ฒˆ์งธ ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ row = sorted(row, key = itemgetter(0)) # row ๋ฆฌ์ŠคํŠธ๋ฅผ for ๋ฌธ์œผ๋กœ ๋ฐ˜๋ณตํ•˜์—ฌ ์‹œํŠธ์— ์ž…๋ ฅ for data in row: ws.append(data) ๋ฆฌ์ŠคํŠธ ๊ทธ๋Œ€๋กœ ์—‘์…€ ํ–‰์— ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ •๋ ฌ์„ ๋”ฐ๋กœ ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†์ง€๋งŒ, ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๊ฐ€ ์„ž์—ฌ์žˆ๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด ํŠน์ • ํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ์ •๋ ฌํ•ด ์ค๋‹ˆ๋‹ค. sorted๋Š” ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ์ž๋ฃŒํ˜•(๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ)์„ ์ •๋ ฌํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ด ์ฃผ๋Š” ํŒŒ์ด์ฌ ๋‚ด์žฅํ•จ ์ˆ˜๋กœ ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์ •๋ ฌํ•  ๋•Œ key ๊ฐ’์— ์–ด๋–ค ํŠน์ •ํ•œ ํ‚ค๋‚˜ ํ•จ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ๋ฉด ์ •๋ ฌ ๋ฐฉ์‹์„ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ key ๊ฐ’์œผ๋กœ itemgetter() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ, ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ํŠน์ • ์œ„์น˜๋‚˜ ํ‚ค์˜ ํ•ญ๋ชฉ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. row = sorted(row, key = itemgetter(0)) ์—ฌ๊ธฐ์„œ itemgetter(0)์€ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ธฐ์ค€์„ ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ์ง€์ •ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด itemgetter(1, 0) ์ด๋ ‡๊ฒŒ ๊ธฐ์ค€ ๊ฐ’์„ ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ์ค€ ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ž…๋ ฅ๋œ ์ˆœ์„œ๋Œ€๋กœ ๊ธฐ์ค€ ๊ฐ’์ด ๋˜์–ด ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, itemgetter(1, 0)์ด๋ฉด ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋จผ์ € ์ •๋ ฌํ•œ ํ›„์— ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ๋กœ ์ƒ์„ฑ๋œ ํŒŒ์ผ์„ ์—ด์–ด๋ณด๋ฉด ์ฒซ ํ–‰์—๋Š” ์นผ๋Ÿผ์ด ์ž…๋ ฅ๋˜์–ด ์žˆ๊ณ , ๊ทธ ๋‹ค์Œํ–‰๋ถ€ํ„ฐ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ‚ค์ธ ์ˆซ์ž(1, 2, 3)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ 3๊ฐœ์˜ ๋ฐ์ดํ„ฐํ–‰์ด ์ž…๋ ฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—‘์…€ ์—ด ์ด๋ฆ„ - ์ธ๋ฑ์Šค ๋ณ€ํ™˜ ํŒŒ์ด์ฌ OpenPyXL๋กœ ๋ฐ์ดํ„ฐ ์ž‘์—…์„ ํ•  ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ์—ด ์ด๋ฆ„์„ ์—‘์…€<NAME>๊ณผ ๋™์ผํ•˜๊ฒŒ ์˜์–ด ์•ŒํŒŒ๋ฒณ 'A', 'B', 'C'๋กœ ์ž…๋ ฅํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํŠน์ • ์—ด ๊ฐ„์˜ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ฑฐ๋‚˜ ํŠน์ • ์—ด์— ๊ธฐ๋ฐ˜ํ•ด์„œ ๋‹ค๋ฅธ ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๋Š” ๋“ฑ ํŠน์ˆ˜ํ•œ ์ƒํ™ฉ์—์„œ๋Š” ์—ด ์ด๋ฆ„ ๋Œ€์‹  ์—ด์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค ๊ฐ’์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. OpenPyXL ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ์—‘์…€ ์—ด ์ด๋ฆ„๊ณผ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ ๊ฐ„์˜ ๋ณ€ํ™˜์„ ์ง€์›ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์–ด ์‰ฝ๊ฒŒ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋กœ๋ถ€ํ„ฐ ์—‘์…€ ์—ด ์ด๋ฆ„ ์–ป๊ธฐ: openpyxl.utils.cell.get_column_letter(์ธ๋ฑ์Šค ๋ฒˆํ˜ธ) ์—‘์…€ ์—ด ์ด๋ฆ„์œผ๋กœ๋ถ€ํ„ฐ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ ์–ป๊ธฐ: openpyxl.utils.cell.column_index_from_string(์—ด ์ด๋ฆ„) # OpenPyXL์˜ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜์—์„œ get_column_letter์™€ column_dindex_from_string ํ•จ์ˆ˜ import from openpyxl.utils.cell import get_column_letter, column_index_from_string # ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ 16์— ํ•ด๋‹นํ•˜๋Š” ์—‘์…€ ์—ด ์ด๋ฆ„ ์–ป๊ธฐ index = 16 print(get_column_letter(index)) # ์—‘์…€ ์—ด ์ด๋ฆ„ 'AB'์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ ์–ป๊ธฐ print(column_index_from_string('AB')) ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ 16์— ํ•ด๋‹นํ•˜๋Š” ์—ด ์ด๋ฆ„์€ 'P'์ด๊ณ , ์—ด ์ด๋ฆ„ 'AB'์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋Š” 28์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ์—‘์…€ ์ž‘์—… ์ค‘ ์—ด ์ด๋ฆ„๊ณผ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ ๋ณ€ํ™˜์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ํ•จ์ˆ˜๋กœ ๊ฐ’์„ ์ฐพ์•„์ค๋‹ˆ๋‹ค. ์—ด ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ ์ž…๋ ฅํ•˜๊ธฐ ์—‘์…€์— ์—ด ๋‹จ์œ„๋กœ ํ•œ ๋ฒˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ด ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ–‰ ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” append์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์—ด ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•  ๋•Œ๋Š” ์ฃผ๋กœ ์…€์— ํ•˜๋‚˜์”ฉ ์ ‘๊ทผํ•ด์„œ ์ž…๋ ฅํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์—ด ๋‹จ์œ„๋กœ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 1) 'cell' ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ from openpyxl import Workbook wb = Workbook() ws = wb.active ws.title = "์ˆ˜๊ฐ•์ƒ_์ •๋ณด" # ์—ด๋กœ ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด 'data'์— ์ €์žฅ data = [ '์ด์ฒ ์ˆ˜', '๊น€๋ฏธ์†Œ', '์ตœํ•™์ค€' ] # for ๋ฌธ์œผ๋กœ 'A'์—ด์˜ ๊ฐ ์…€์— ์ˆœ์„œ๋Œ€๋กœ ์ ‘๊ทผํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ for i, value in enumerate(data): ws.cell(row=i+1, column=1, value=value) wb.save("์ˆ˜๊ฐ•์ƒ_๋ฆฌ์ŠคํŠธ. xlsx") 'cell' ๋ฉ”์„œ๋“œ๋Š” ๊ฐ ์…€์˜ ์œ„์น˜๋ฅผ ํ–‰(row)๊ณผ ์—ด(column)๋กœ ์ง€์ •ํ•˜์—ฌ ๊ฐ’์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ ํ–‰(row)์€ ํ•˜๋‚˜์”ฉ ์ˆซ์ž๊ฐ€ ๋Š˜์–ด๋‚˜์„œ ๋‹ค์Œ ํ–‰์œผ๋กœ ์ด๋™ํ•˜๊ณ , ์—ด(column)์€ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ 1, ์ฆ‰, 'A์—ด'์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 'A'์—ด์˜ ๊ฐ ํ–‰์„ ์ˆœ์„œ๋Œ€๋กœ ์ ‘๊ทผํ•˜๋ฉด์„œ 'data' ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. 2) ์—ด ์ด๋ฆ„ ํ™œ์šฉ from openpyxl import Workbook from openpyxl.utils import get_column_letter wb = Workbook() ws = wb.active ws.title = "์ˆ˜๊ฐ•์ƒ_์ •๋ณด" # ์—ด๋กœ ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด 'data'์— ์ €์žฅ data = [ '์ด์ฒ ์ˆ˜', '๊น€๋ฏธ์†Œ', '์ตœํ•™์ค€' ] # ์ž…๋ ฅํ•  ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์—ด์ด๋ฆ„์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ 'column_name'์— ์ €์žฅ column_name = get_column_letter(1) # ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์—ด์˜ ๊ฐ ์…€์˜ ์œ„์น˜๋ฅผ for ๋ฌธ์œผ๋กœ ์ƒ์„ฑํ•œ ํ›„ 'data'์˜ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ์ž…๋ ฅ for i, value in enumerate(data): ws[f"{column_name}{i+1}"] = value wb.save("์ˆ˜๊ฐ•์ƒ_๋ฆฌ์ŠคํŠธ. xlsx") ์ด ๋ฐฉ์‹์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์—ด์˜ ๋ชจ๋“  ์…€์— ๊ฐ๊ฐ ์ ‘๊ทผํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์—ด์˜ ๊ฐ ์…€์— ํ•ด๋‹นํ•˜๋Š” ์œ„์น˜๊ฐ’์„ for ๋ฌธ์œผ๋กœ ์ž๋™ ์ƒ์„ฑํ•˜๋Š”๋ฐ, ws[f"{column_name}{i+1}"]์—์„œ ์—ด์˜ ๊ฐ’์€ 'column_name'์ธ 'A'์—ด๋กœ ๊ณ ์ •ํ•˜๊ณ , i๊ฐ€ ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ํ–‰์˜ ๊ฐ’์ด ํ•˜๋‚˜์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ A1, A2, ... ์ˆœ์„œ๋Œ€๋กœ ์…€์˜ ์œ„์น˜๊ฐ’์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ถœ๋ ฅ๋œ ์…€์˜ ์œ„์น˜์— ์ฐจ๋ก€๋Œ€๋กœ ์ ‘๊ทผํ•˜๋ฉด์„œ 'data'์˜ ์›์†Œ๋“ค์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. 3) iter_cols() ํ•จ์ˆ˜ ์‚ฌ์šฉ from openpyxl import Workbook wb = Workbook() ws = wb.active ws.title = "์ˆ˜๊ฐ•์ƒ_์ •๋ณด" # ์—ด๋กœ ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด 'data'์— ์ €์žฅ data = [ '์ด์ฒ ์ˆ˜', '๊น€๋ฏธ์†Œ', '์ตœํ•™์ค€' ] # A ์—ด์˜ ์…€์„ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” iterator๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ์ตœ๋Œ€ํ–‰์„ 'data'์˜ ์›์†Œ ๊ฐœ์ˆ˜๋กœ ์ง€์ • col_cells = ws.iter_cols(min_col=1, max_col=1, max_row=len(data)) # ์ด์ค‘ for ๋ฌธ์œผ๋กœ B ์—ด์˜ ๊ฐ ์…€์„ ์ˆœํ™˜ํ•˜๋ฉฐ 'data' ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅ for col in col_cells: for i, cell in enumerate(col): cell.value = data[i] wb.save("์ˆ˜๊ฐ•์ƒ_๋ฆฌ์ŠคํŠธ. xlsx") 'iter_cols()'๋Š” ์—ด ๋‹จ์œ„๋กœ ์…€์„ ์ˆœํšŒํ•˜๋Š” ํ•จ์ˆ˜๋กœ, ์—ฌ๊ธฐ์„œ๋Š” ํ•˜๋‚˜์˜ ์—ด์—๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ์œ„ํ•ด 'min_col(์‹œ์ž‘ ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ)'์™€ 'max_col(์ข…๋ฃŒ ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ)'์„ ์—ด์˜ ์ธ๋ฑ์Šค ๊ฐ’์ธ 1์„ ๋„ฃ์–ด์ฃผ์–ด 'A'์—ด๋งŒ ์ˆœํšŒํ•˜๋„๋ก ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ํ–‰์˜ ๋ฒ”์œ„๋„ ์ง€์ •ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด 'max_row'๋Š” ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋กœ ์ œํ•œํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฒ”์œ„๊ฐ€ ์ง€์ •๋œ 'iter_cols()' ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์…€์˜ ์œ„์น˜๋“ค์ด ๋ฐ˜ํ™˜๋˜์–ด 'col_cells'์— ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ฒซ ๋ฒˆ์งธ for ๋ฌธ์œผ๋กœ 'col_cells'์— ์ €์žฅ๋œ ์›์†Œ๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ๊ฐ ์…€์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ for ๋ฌธ์œผ๋กœ ๊ฐ๊ฐ์˜ ์…€์— 'data'์˜ ์›์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. 4) ํ–‰๊ณผ ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋กœ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ from openpyxl import Workbook wb = Workbook() ws = wb.active ws.title = "์ˆ˜๊ฐ•์ƒ_์ •๋ณด" # A์—ด๊ณผ B ์—ด์— ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด 'data'์— ์ €์žฅ data = [ [ '์ด์ฒ ์ˆ˜', '๊น€๋ฏธ์†Œ', '์ตœํ•™์ค€' ], [ '์ˆ˜ํ•™', '์˜์–ด', '์ปดํ“จํ„ฐ' ] ] # ์ด์ค‘ for ๋ฌธ์œผ๋กœ ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์™€ ํ–‰์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋กœ ์…€์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ for col_idx, col_data in enumerate(data, start=1): for row_idx, row_data in enumerate(col_data): ws.cell(row=row_idx+1, column=col_idx, value=row_data) wb.save("์ˆ˜๊ฐ•์ƒ_๋ฆฌ์ŠคํŠธ. xlsx") ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” A์—ด๊ณผ B ์—ด์— ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ๊ฐ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค๊ณ , ๋‘ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์—ด ์ˆœ์„œ๋Œ€๋กœ ์›์†Œ๋กœ ๋„ฃ์–ด ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ ์ด์ค‘ for ๋ฌธ์œผ๋กœ ํ–‰๊ณผ ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๋ฐ, 'start=1'๋กœ ์ง€์ •ํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์—ด๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ฒซ ๋ฒˆ์งธ ์—ด์ด ์•„๋‹Œ ํŠน์ • ์—ด๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ์‹œ์ž‘ํ•˜๋ ค๋ฉด ํ•ด๋‹น ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์‹œ์ž‘์—ด๋กœ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์—ด๊ณผ ์—ด ์‚ฌ์ด์— ๊ณต๋ฐฑ์„ ๋„ฃ๊ณ ์ž ํ•œ๋‹ค๋ฉด, ์ด์ค‘ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ ๋ฆฌ์ŠคํŠธ ์‚ฌ์ด์— ์›ํ•˜๋Š” ๊ณต๋ฐฑ ์—ด์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋นˆ ๋ฆฌ์ŠคํŠธ '[]' ๋„ฃ์–ด์ฃผ๋ฉด ๊ทธ๋งŒํผ ์—ด์„ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๊ทธ๋‹ค์Œ ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. 04-02. ์—‘์…€ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ ์ƒ์„ฑ๋œ ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์ƒˆ๋กญ๊ฒŒ ์—‘์…€ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด์— ๋งŒ๋“ค์–ด์ง„ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์™€์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•˜๋Š” ์ž‘์—…์„ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ด๋ฏธ ์ƒ์„ฑ๋˜์–ด ์žˆ๋Š” ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜จ ํ›„ ํŠน์ • ์›Œํฌ์‹œํŠธ๋ฅผ ์„ ํƒํ•˜์—ฌ ์ž‘์—…ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ์ด ์—‘์…€ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ์—ด๋ ค ์žˆ๋Š” ๊ฒฝ์šฐ์—๋„ ํŒŒ์ด์ฌ์—์„œ ์ ‘๊ทผํ•  ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋‚  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ์–ด์˜ค๋ ค๋Š” ํŒŒ์ผ์ด ์—ด๋ ค์žˆ๋‹ค๋ฉด ๋ชจ๋‘ ์ข…๋ฃŒํ•œ ํ›„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—‘์…€ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ openpyxl์€ ํŒŒ์ผ์— ์ €์žฅ๋œ ๋ชจ๋“  ์š”์†Œ๋“ค(items)์„ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ์ด๋ฏธ ์ง€๋‚˜ ์ฐจํŠธ ๋“ฑ์˜ ๋‚ด์šฉ์ด ์‚ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์—‘์…€ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์™€ ์ž‘์—…์„ ํ•œ ํ›„ ์ €์žฅํ•  ๋•Œ๋Š” ๊ธฐ์กด ํŒŒ์ผ๋ช…์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•˜์—ฌ ๊ธฐ์กด ์—‘์…€ ํŒŒ์ผ์˜ ์ด๋ฏธ ์ง€๋‚˜ ์ฐจํŠธ๊ฐ€ ์ง€์›Œ์ง€์ง€ ์•Š๋„๋ก ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # openpyxl ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ load_workbook์„ import from openpyxl import load_workbook # '์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx' ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # ์ฒซ ๋ฒˆ์งธ ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb.active ์ด๋ฏธ ์ƒ์„ฑ๋˜์–ด ์žˆ๋Š” ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ, ํ•ด๋‹น ํŒŒ์ผ์ด ํŒŒ์ด์ฌ์„ ์‹คํ–‰ํ•œ ์œ„์น˜(์‹คํ–‰ ๊ฒฝ๋กœ)์— ์ €์žฅ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‹ค๋ฅธ ์œ„์น˜์— ์žˆ๋Š” ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ ์ž ํ•œ๋‹ค๋ฉด, load_workbook(filename)์˜ ํŒŒ์ผ๋ช…์„ ์ ์„ ๋•Œ ํŒŒ์ผ์˜ ์œ„์น˜๊นŒ์ง€ ํฌํ•จํ•ด์•ผ ๋กœ๋”ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, C ๋“œ๋ผ์ด๋ธŒ > ์‚ฌ์šฉ์ž > 2023์ด๋ผ๋Š” ํด๋”์— ์ €์žฅ๋œ '์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx'ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฝ๋กœ๋ฅผ ํ•จ๊ป˜ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. wb = load_workbook(filename='C:\Users\2023\์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') ์ง์ ‘ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š๊ณ  os.path๋ฅผ ํ™œ์šฉํ•œ ๊ฒฝ๋กœ ์ž…๋ ฅ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ์‹œํŠธ ์ง€์ •ํ•˜์—ฌ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ํŒŒ์ผ์˜ ํ™œ์„ฑํ™”๋œ ์‹œํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋„๋ก wb.active๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›Œํฌ์‹œํŠธ๋ฅผ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์‹œํŠธ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์‹œํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ์ž‘์—…์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋Œ€๊ด„ํ˜ธ('[ ]')๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ 'get_sheet_by_name()' ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 1) ๋Œ€๊ด„ํ˜ธ '[ ]'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # '10์›”' ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb['10์›”'] # 'A1'์…€์˜ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ ws['A1'].value 2) 'get_sheet_by_name()' ๋ฉ”์„œ๋“œ๋กœ ํŠน์ • ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # '10์›”' ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb.get_sheet_by_name('10์›”') # 'A1'์…€์˜ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ ws['A1'].value ์œ„์˜ ๋‘ ์ฝ”๋“œ ๋ชจ๋‘ ๋™์ผํ•˜๊ฒŒ '10์›”'์ด๋ผ๋Š” ์‹œํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ์‹ ๋ฒ„์ „์ด์ง€๋งŒ, ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์ธ 'get_sheet_by_name()'์€ ๊ตฌ๋ฒ„์ „ ๋ฐฉ์‹์ด๋ผ ์‹คํ–‰ ์‹œ ์ฝ”๋“œ๋Š” ๋ฌธ์ œ์—†์ด ์ž‘๋™ํ•˜๋‚˜ DeprecationWarning ๊ฒฝ๊ณ ๊ฐ€ ๋œฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์‹œํŠธ๋ช…์„ ์ •ํ™•ํžˆ ์•Œ์ง€ ๋ชปํ•˜๊ฑฐ๋‚˜ ์ „์ฒด ์‹œํŠธ๋ช… ํ™•์ธ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ, ํŒŒ์ผ์— ํฌํ•จ๋œ ์ „์ฒด ์‹œํŠธ์˜ ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # ์‹œํŠธ ์ด๋ฆ„๋“ค์„ ๊ฐ€์ ธ์˜ค๊ธฐ sheet_names = wb.sheetnames # ์‹œํŠธ ์ด๋ฆ„๋“ค ์ถœ๋ ฅ print(sheet_names) # ์ถœ๋ ฅ๊ฐ’ ['10์›”', '11์›”', '12์›”', '2023๋…„_์ „์ฒด'] ์‹œํŠธ ์ „์ฒด ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์‹œํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ํ›„ ws['A1'].value ์ฝ”๋“œ๋กœ ํ•ด๋‹น ์‹œํŠธ์˜ A1 ์…€์˜ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค. ์…€ ํ•˜๋‚˜์˜ ๊ฐ’์ด ์•„๋‹Œ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ–‰๋งˆ๋‹ค ์ˆœํšŒํ•˜๋ฉด์„œ ํ•œ ํ–‰์˜ ๋ชจ๋“  ์…€ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋„๋ก ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # '10์›”' ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb['10์›”'] # ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ฝ๊ธฐ for row in ws.rows: row_values = [cell.value for cell in row] print(row_values) # ์ถœ๋ ฅ๊ฐ’ ['10์›”', None, None, None, None, None] ['๊ตฌ๋งค์ผ์ž', '๊ตฌ๋งค ์ œํ’ˆ', '์ˆ˜๋Ÿ‰', '๊ณ ๊ฐ๋ช…', '์†Œ์†', '๊ธฐ์กด ๊ณ ๊ฐ ์—ฌ๋ถ€', '๋‹จ๊ฐ€', '์ด์•ก'] [datetime.datetime(2023, 10, 3, 0, 0), 'A ์ œํ’ˆ', 2, '๊น€์ฒ ์›', '์ตœ๊ฐ• ํšŒ์‚ฌ', '๊ธฐ์กด', 20000, '=C3*G3'] [datetime.datetime(2023, 10, 16, 0, 0), 'B ์ œํ’ˆ', 1, '์ด ๋‚จ์—ฐ', '๋‹ค ์•Œ ์•„์—ฐ ๊ตฌ์†Œ', '์‹ ๊ทœ', 18000, '=C4*G4'] [datetime.datetime(2023, 10, 22, 0, 0), 'B ์ œํ’ˆ', 1, '์ตœ์—ฐํ™”', '์ผ์ผ ์ปดํผ๋‹ˆ', '์‹ ๊ทœ', 18000, '=C5*G5'] [datetime.datetime(2023, 10, 29, 0, 0), 'C ์ œํ’ˆ', 4, 'ํ™ฉ์ˆ˜์ง€', '์†Œ์† ์—†์Œ', '์‹ ๊ทœ', 36000, '=C6*G6'] [datetime.datetime(2023, 10, 29, 0, 0), 'D ์ œํ’ˆ', 2, '์œ ์ง„ํƒœ', '์†Œ์† ์—†์Œ', '๊ธฐ์กด', 68000, '=C7*G7'] ํŠน์ • ํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„์˜ ์ฝ”๋“œ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, ์ฒซ ๋ฒˆ์งธ ํ–‰์—์„œ๋Š” A1 ์…€์— '10์›”'์ด๋ผ๋Š” ๊ฐ’๋งŒ ์žˆ๊ณ  ๋‘ ๋ฒˆ์งธ ํ–‰์€ ํ…Œ์ด๋ธ”์˜ ์—ด์ด๋ฆ„์ด ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‹œ์ž‘๋˜๋Š” ํ–‰์€ ์„ธ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ž‘์—…์„ ์œ„ํ•ด ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ œ์™ธํ•œ ์‹ค์ œ ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. rows()๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # '10์›”' ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb['10์›”'] # ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ํ–‰์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ new_rows = list(ws.rows)[2:] # ๋‚˜๋จธ์ง€ ํ–‰์„ ์ˆœํšŒํ•˜๋ฉฐ ์ฝ๊ธฐ for row in new_rows: row_values = [cell.value for cell in row] print(row_values) # ์ถœ๋ ฅ๊ฐ’ [datetime.datetime(2023, 10, 3, 0, 0), 'A ์ œํ’ˆ', 2, '๊น€์ฒ ์›', '์ตœ๊ฐ• ํšŒ์‚ฌ', '๊ธฐ์กด', 20000, '=C3*G3'] [datetime.datetime(2023, 10, 16, 0, 0), 'B ์ œํ’ˆ', 1, '์ด ๋‚จ์—ฐ', '๋‹ค ์•Œ ์•„์—ฐ ๊ตฌ์†Œ', '์‹ ๊ทœ', 18000, '=C4*G4'] [datetime.datetime(2023, 10, 22, 0, 0), 'B ์ œํ’ˆ', 1, '์ตœ์—ฐํ™”', '์ผ์ผ ์ปดํผ๋‹ˆ', '์‹ ๊ทœ', 18000, '=C5*G5'] [datetime.datetime(2023, 10, 29, 0, 0), 'C ์ œํ’ˆ', 4, 'ํ™ฉ์ˆ˜์ง€', '์†Œ์† ์—†์Œ', '์‹ ๊ทœ', 36000, '=C6*G6'] [datetime.datetime(2023, 10, 29, 0, 0), 'D ์ œํ’ˆ', 2, '์œ ์ง„ํƒœ', '์†Œ์† ์—†์Œ', '๊ธฐ์กด', 68000, '=C7*G7'] ์ฒซ ๋ฒˆ์งธ ํ–‰์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ํ–‰ ์ „์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ 'new_rows'์— ์ €์žฅํ•œ ํ›„, 'new_rows'์˜ ์ „์ฒด ํ–‰์„ ์ˆœํšŒํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋„๋ก ์‹คํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. iter_rows๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ iter_rows ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•ด๋„ ์œ„์˜ ๋‚ด์šฉ์„ ๋™์ผํ•˜๊ฒŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. iter_rows๋Š” ์—‘์…€ ์›Œํฌ์‹œํŠธ์˜ ํŠน์ • ํ–‰ ๋ฒ”์œ„๋ฅผ ๋ฐ˜๋ณตํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์‹œํŠธ์˜ ํŠน์ • ๋ถ€๋ถ„๋งŒ ์„ ํƒ์ ์œผ๋กœ ์ฝ์–ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ–‰๊ณผ ์—ด์˜ ํŠน์ • ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜์—ฌ ๊ทธ ๋ถ€๋ถ„๋งŒ ๋ฐ˜๋ณตํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # '10์›”' ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb['10์›”'] # 3๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ํ–‰๊นŒ์ง€๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ’์„ ์ฝ๊ธฐ (min_row =3) for row in ws.iter_rows(min_row=3, values_only=True): print(row) # ๊ฒฐ๊ด๊ฐ’ (datetime.datetime(2023, 10, 3, 0, 0), 'A ์ œํ’ˆ', 2, '๊น€์ฒ ์›', '์ตœ๊ฐ• ํšŒ์‚ฌ', '๊ธฐ์กด', 20000, '=C3*G3') (datetime.datetime(2023, 10, 16, 0, 0), 'B ์ œํ’ˆ', 1, '์ด ๋‚จ์—ฐ', '๋‹ค ์•Œ ์•„์—ฐ ๊ตฌ์†Œ', '์‹ ๊ทœ', 18000, '=C4*G4') (datetime.datetime(2023, 10, 22, 0, 0), 'B ์ œํ’ˆ', 1, '์ตœ์—ฐํ™”', '์ผ์ผ ์ปดํผ๋‹ˆ', '์‹ ๊ทœ', 18000, '=C5*G5') (datetime.datetime(2023, 10, 29, 0, 0), 'C ์ œํ’ˆ', 4, 'ํ™ฉ์ˆ˜์ง€', '์†Œ์† ์—†์Œ', '์‹ ๊ทœ', 36000, '=C6*G6') (datetime.datetime(2023, 10, 29, 0, 0), 'D ์ œํ’ˆ', 2, '์œ ์ง„ํƒœ', '์†Œ์† ์—†์Œ', '๊ธฐ์กด', 68000, '=C7*G7') iter_rows๋กœ ์‹œํŠธ์˜ ๊ฐ ํ–‰์„ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•œ ๋‹ค์Œ for ๋ฌธ์œผ๋กœ ํ–‰์„ ์ˆœํšŒํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ํŒŒ๋ผ ๋ฏธํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ์‹œํŠธ์˜ ํŠน์ • ๋ถ€๋ถ„๋งŒ ๊ฐ€์ ธ์˜ค๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ์„ธ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ํ–‰๊นŒ์ง€๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด 'min_row=3'์œผ๋กœ ์‹œ์ž‘ํ•  ํ–‰๋งŒ ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฃผ์š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ„๋žตํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. min_row : ๋ฐ˜๋ณต์„ ์‹œ์ž‘ํ•  ํ–‰์˜ ์ธ๋ฑ์Šค(์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ฒซ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ์‹œ์ž‘) max_row : ๋ฐ˜๋ณต์„ ๋งˆ์น  ํ–‰์˜ ์ธ๋ฑ์Šค(์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋งˆ์ง€๋ง‰ ํ–‰๊นŒ์ง€ ๋ฐ˜๋ณต) min_col : ๋ฐ˜๋ณต์„ ์‹œ์ž‘ํ•  ์—ด์˜ ์ธ๋ฑ์Šค(์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ฒซ ๋ฒˆ์งธ ์—ด๋ถ€ํ„ฐ ์‹œ์ž‘) max_col : ๋ฐ˜๋ณต์„ ๋งˆ์น  ์—ด์˜ ์ธ๋ฑ์Šค(์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋งˆ์ง€๋ง‰ ์—ด๊นŒ์ง€ ๋ฐ˜๋ณต) values_only : Ture์ผ ๊ฒฝ์šฐ ์…€ ๊ฐ’์„ ๋ฐ˜ํ™˜(์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์…€ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜) ์œ„์˜ ์—ฌ๋Ÿฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ์›ํ•˜๋Š” ๋ฒ”์œ„์˜ ๋ฐ์ดํ„ฐ๋งŒ ์„ ํƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜์‹์„ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„์˜ ์ฝ”๋“œ ์ถœ๋ ฅ๊ฐ’์—์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์—ด์„ ๋ณด๋ฉด, ํŠน์ •ํ•œ ๊ฐ’์ด ์•„๋‹Œ '= C3 * G3'๊ณผ ๊ฐ™์ด ์ˆ˜์‹์ด ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด ์—‘์…€ ํŒŒ์ผ์— ์ˆ˜์‹์ด ์ž‘์„ฑ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ, ํŒŒ์ด์ฌ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ๋ณ„๋„์˜ ์„ค์ •์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ๊ฐ€ ์•„๋‹Œ ์ˆ˜์‹ ์ž์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ต๋‹ˆ๋‹ค. ์ˆ˜์‹ ์ž์ฒด๊ฐ€ ์•„๋‹Œ ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ด๊ฐ’์ด ํ•„์š”ํ•˜๋‹ค๋ฉด load_workbook()์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ 'data_only = True'๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์œ„์˜ ๋™์ผํ•œ ์ฝ”๋“œ์— 'data_only = True'๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ (์ˆ˜์‹์ด ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ด๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋„๋ก ์ธ์ˆ˜ ์„ค์ •) wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx', data_only = True) # '10์›”' ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb['10์›”'] # ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ํ–‰์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ new_rows = list(ws.rows)[2:] # ๋‚˜๋จธ์ง€ ํ–‰์„ ์ˆœํšŒํ•˜๋ฉฐ ์ฝ๊ธฐ for row in new_rows: row_values = [cell.value for cell in row] print(row_values) # ์ถœ๋ ฅ๊ฐ’ [datetime.datetime(2023, 10, 3, 0, 0), 'A ์ œํ’ˆ', 2, '๊น€์ฒ ์›', '์ตœ๊ฐ• ํšŒ์‚ฌ', '๊ธฐ์กด', 20000, 40000] [datetime.datetime(2023, 10, 16, 0, 0), 'B ์ œํ’ˆ', 1, '์ด ๋‚จ์—ฐ', '๋‹ค ์•Œ ์•„์—ฐ ๊ตฌ์†Œ', '์‹ ๊ทœ', 18000, 18000] [datetime.datetime(2023, 10, 22, 0, 0), 'B ์ œํ’ˆ', 1, '์ตœ์—ฐํ™”', '์ผ์ผ ์ปดํผ๋‹ˆ', '์‹ ๊ทœ', 18000, 18000] [datetime.datetime(2023, 10, 29, 0, 0), 'C ์ œํ’ˆ', 4, 'ํ™ฉ์ˆ˜์ง€', '์†Œ์† ์—†์Œ', '์‹ ๊ทœ', 36000, 144000] [datetime.datetime(2023, 10, 29, 0, 0), 'D ์ œํ’ˆ', 2, '์œ ์ง„ํƒœ', '์†Œ์† ์—†์Œ', '๊ธฐ์กด', 68000, 136000] ์ด๋ฒˆ์—๋Š” ๋งˆ์ง€๋ง‰ ์—ด์ด ์ˆ˜์‹์œผ๋กœ ์ถœ๋ ฅ๋˜์ง€ ์•Š๊ณ , ์ˆ˜์‹ ๊ณ„์‚ฐ์„ ์™„๋ฃŒํ•œ ๊ฐ’์ด ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค๋ฌด์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์—‘์…€ ํŒŒ์ผ์—๋Š” ์ˆ˜์‹์ด ํฌํ•จ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿด ๋•Œ๋Š” ์ด์™€ ๊ฐ™์ด 'data_only =True'๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ด ๊ฒฝ์šฐ์—๋„ ์ž‘์—… ์™„๋ฃŒ ํ›„ ๊ธฐ์กด ์—‘์…€ ํŒŒ์ผ๊ณผ ๋™์ผํ•œ ํŒŒ์ผ๋ช…์œผ๋กœ ๋ฎ์–ด์“ฐ๋ฉด ๊ธฐ์กดํŒŒ์ผ์— ์žˆ๋˜ ์ˆ˜์‹์ด ์ง€์›Œ์ง€๋ฏ€๋กœ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•˜์—ฌ ์›๋ณธ์„ ๋ณ„๋„๋กœ ๋ณด์กดํ•˜๋Š” ํŽธ์ด ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ์— ์ข‹์Šต๋‹ˆ๋‹ค. ์—ด์„ ์ง€์ •ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์ด๋ฒˆ์—๋Š” ํŠน์ • ์—ด์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. rows()๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # '10์›”' ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb['10์›”'] # ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ํ–‰์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ new_rows = list(ws.rows)[2:] # ๋‚˜๋จธ์ง€ ํ–‰์„ ์ˆœํšŒํ•˜๋ฉฐ B ์—ด๊ณผ C ์—ด์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ฝ๊ธฐ for row in new_rows: print(row[1].value, row[2].value) # ์ถœ๋ ฅ๊ฐ’ ๊ตฌ๋งค ์ œํ’ˆ ์ˆ˜๋Ÿ‰ A ์ œํ’ˆ 2 B ์ œํ’ˆ 1 B ์ œํ’ˆ 1 C ์ œํ’ˆ 4 D ์ œํ’ˆ 2 ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ถœ๋ ฅํ•  ์—ด์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋ฐฉ์‹์„ ํ™œ์šฉํ•˜์—ฌ ํŠน์ • ํ–‰๊ณผ ํŠน์ • ์—ด์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ž์œ ์ž์žฌ๋กœ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. iter_cols๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ '3-4-1. ์—‘์…€ ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ'์—์„œ ์—ด ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ iter_cols๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ํŠน์ • ํ–‰์„ ์ง€์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ iter_rows์ฒ˜๋Ÿผ iter_cols๋„ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ™œ์šฉํ•ด ํŠน์ • ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์œ„์˜ ์ฝ”๋“œ๋ฅผ iter_cols๋ฅผ ํ™œ์šฉํ•œ ์ฝ”๋“œ๋กœ ์ˆ˜์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # '10์›”' ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb['10์›”'] # B ์—ด๊ณผ C ์—ด ๋ฐ์ดํ„ฐ๋งŒ ์ˆœํšŒํ•˜๊ธฐ (์ธ๋ฑ์Šค๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ B ์—ด์€ 2, C ์—ด์€ 3์ž…๋‹ˆ๋‹ค) for col in ws.iter_cols(min_col=2, max_col=3, min_row=3): for cell in col: print(cell.value) # ๊ฒฐ๊ด๊ฐ’ A ์ œํ’ˆ B ์ œํ’ˆ B ์ œํ’ˆ C ์ œํ’ˆ D ์ œํ’ˆ 1 4 'min_col=2'๋กœ ๋‘ ๋ฒˆ์งธ ์—ด(B)๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์˜€๊ณ , 'max_col=3'์œผ๋กœ ์„ธ ๋ฒˆ์งธ ์—ด(C)๊นŒ์ง€ ์—ด ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๊ณ , 'min_row=3'์œผ๋กœ ์„ธ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ํ–‰๊นŒ์ง€๋กœ ํ–‰์˜ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. iter_rows๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์™€ ๋น„๊ตํ•˜๋ฉด ์‚ฌ์šฉํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ด๋ฆ„์ด ๋™์ผํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, iter_cols์™€ iter_rows๋Š” ๋™์ผํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ข…๋ฅ˜๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ฐจ์ด์ ์ด ์žˆ๋‹ค๋ฉด iter_rows๋Š” ๊ฐ ํ–‰์˜ ์…€๋“ค์„ ์ˆœํšŒํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ–‰ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ , iter_cols๋Š” ๊ฐ ์—ด์˜ ์…€์„ ์ˆœํšŒํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ด ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. iter_cols๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ์ถœ๋ ฅ๋œ ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด B ์—ด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ์ถœ๋ ฅ๋œ ๋‹ค์Œ์— C ์—ด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ด์–ด์„œ ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด iter_cols๋Š” ์—ด ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ์กฐ๊ฑด์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์—‘์…€์˜ ํ•„ํ„ฐ์ฒ˜๋Ÿผ openpyxl์„ ์ด์šฉํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋ฐ์ดํ„ฐ์—์„œ ์ˆ˜๋Ÿ‰์ด 2๊ฐœ ์ด์ƒ์ธ ๋ฐ์ดํ„ฐ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # '10์›”' ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb['10์›”'] # ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ํ–‰์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ new_rows = list(ws.rows)[2:] # ๋‚˜๋จธ์ง€ ํ–‰์„ ์ˆœํšŒํ•˜๋ฉฐ ์ฝ๊ธฐ for row in new_rows: if row[2].value >= 2: print(row[1].value, row[2].value) # ์ถœ๋ ฅ๊ฐ’ A ์ œํ’ˆ 2 C ์ œํ’ˆ 4 D ์ œํ’ˆ 2 ์ด์™€ ๊ฐ™์ด if ๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ํŠน์ • ์กฐ๊ฑด์„ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ, ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ˆซ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์กฐ๊ฑด์„ ๊ฑฐ๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ์—ด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ˆซ์žํ˜• ๋ฐ์ดํ„ฐ์ด์–ด์•ผ ํ•˜๋ฉฐ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” int()๋‚˜ float() ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์žํ˜•์œผ๋กœ ๋จผ์ € ๋ณ€ํ™˜ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•”ํ˜ธํ™”๋œ ์—‘์…€ ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ openpyxl ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์—‘์…€ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ์•”ํ˜ธํ™”๋œ ํŒŒ์ผ์€ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋Ÿด ๊ฒฝ์šฐ ์ž‘์—… ์‹คํ–‰ ์ „์— ๋ฏธ๋ฆฌ ์—‘์…€ ํŒŒ์ผ์˜ ์•”ํ˜ธ๋ฅผ ํ•ด์ œํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ msoffcrypto-tool์„ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. msoffcrypto-tool์„ ์‚ฌ์šฉํ•˜๋ฉด ์•”ํ˜ธํ™”๋œ ์—‘์…€๊ณผ ์›Œ๋“œ ํŒŒ์ผ์„ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € pip ๋ช…๋ น์–ด๋กœ msoffcrypto-tool์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install msoffcrypto-tool openpyxl ์•„๋ž˜๋Š” ์„ค์น˜๋œ msoffcrypto-tool์„ ์‚ฌ์šฉํ•ด openpyxl๋กœ ์•”ํ˜ธํ™”๋œ ์—‘์…€ ํŒŒ์ผ("์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ_์ž ๊น€. xlsx", ์•”ํ˜ธ: 1234)์„ ์ฝ์–ด ์™€์„œ ํ–‰ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. import msoffcrypto from openpyxl import load_workbook # ์•”ํ˜ธํ™”๋œ ์—‘์…€ ํŒŒ์ผ ์ง€์ • file = msoffcrypto.OfficeFile(open("์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ_์ž ๊น€. xlsx", "rb")) # ํŒŒ์ผ ์•”ํ˜ธ ํ•ด์ œ file.load_key(password="1234") # ์•”ํ˜ธ ํ•ด์ œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ ํŒŒ์ผ์— ์ €์žฅ with open("์›”๋ณ„๊ตฌ๋งค๊ณ ๊ฐ๋ฆฌ์ŠคํŠธ_์ž ๊น€ํ•ด์ œ.xlsx", "wb") as f: file.decrypt(f) # ์•”ํ˜ธ ํ•ด์ œ๋œ ํŒŒ์ผ ์ฝ๊ธฐ wb = load_workbook("์›”๋ณ„๊ตฌ๋งค๊ณ ๊ฐ๋ฆฌ์ŠคํŠธ_์ž ๊น€ํ•ด์ œ.xlsx") ws = wb.active # ์›Œํฌ์‹œํŠธ์˜ ๋ชจ๋“  ํ–‰์„ ์ˆœํšŒํ•˜์—ฌ ์ถœ๋ ฅ for row in ws.iter_rows(values_only=True): print(row) # ์•”ํ˜ธ ํ•ด์ œ๋œ ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ import os os.remove("์›”๋ณ„๊ตฌ๋งค๊ณ ๊ฐ๋ฆฌ์ŠคํŠธ_์ž ๊น€ํ•ด์ œ.xlsx") # ๊ฒฐ๊ด๊ฐ’ ('12์›”', None, None, None, None, None, None, None) ('๊ตฌ๋งค์ผ์ž', '๊ตฌ๋งค ์ œํ’ˆ', '์ˆ˜๋Ÿ‰', '๊ณ ๊ฐ๋ช…', '์†Œ์†', '๊ธฐ์กด ๊ณ ๊ฐ ์—ฌ๋ถ€', '๋‹จ๊ฐ€', '๋งค์ถœ์•ก') (datetime.datetime(2023, 12, 13, 0, 0), 'C ์ œํ’ˆ', 2, '๊ฐ•์ง€์›', 'ํ•™์ƒ', '๊ธฐ์กด', 36000, '=C3*G3') (datetime.datetime(2023, 12, 17, 0, 0), 'C ์ œํ’ˆ', 1, '์ตœ๋ฏผ์€', '์•„์ž์ฝ”ํผ๋ ˆ์ด์…˜', '์‹ ๊ทœ', 36000, '=C4*G4') (datetime.datetime(2023, 12, 22, 0, 0), 'B ์ œํ’ˆ', 1, '๋ฐ•์„œ์€', '์ƒ์ƒ ๋Œ€ํ•™๊ต', '์‹ ๊ทœ', 18000, '=C5*G5') (datetime.datetime(2023, 12, 27, 0, 0), 'A ์ œํ’ˆ', 4, '์ž„์ง€์€', '์ฃผ์‹ํšŒ์‚ฌ ์ง€์€', '์‹ ๊ทœ', 20000, '=C6*G6') msoffcrypto๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ ์•”ํ˜ธ ํ•ด์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import msoffcrypto from openpyxl import load_workbook # ์•”ํ˜ธํ™”๋œ ์—‘์…€ ํŒŒ์ผ ์ง€์ • file = msoffcrypto.OfficeFile(open("์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ_์ž ๊น€. xlsx", "rb")) ๋จผ์ € msoffcrypto๋ฅผ import ํ•œ ํ›„์—, open()๊ณผ msoffcrypto.OfficeFile()๋กœ ์•”ํ˜ธํ™”๋œ ์—‘์…€ ํŒŒ์ผ์„ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค. # ํŒŒ์ผ ์•”ํ˜ธ ํ•ด์ œ file.load_key(password="1234") # ์•”ํ˜ธ ํ•ด์ œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ ํŒŒ์ผ์— ์ €์žฅ with open("์›”๋ณ„๊ตฌ๋งค๊ณ ๊ฐ๋ฆฌ์ŠคํŠธ_์ž ๊น€ํ•ด์ œ.xlsx", "wb") as f: file.decrypt(f) ๊ทธ๋‹ค์Œ, load_key() ๋ฉ”์„œ๋“œ์— ํŒจ์Šค์›Œ๋“œ "1234"๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ „๋‹ฌ๋ฐ›์€ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด decrypt()๋กœ ํŒŒ์ผ์˜ ์•”ํ˜ธ๋ฅผ ํ•ด์ œํ•˜๊ณ , ์ด๋ ‡๊ฒŒ ์•”ํ˜ธ๊ฐ€ ํ•ด์ œ๋œ ๋ฐ์ดํ„ฐ๋ฅผ "์›”๋ณ„๊ตฌ๋งค๊ณ ๊ฐ๋ฆฌ์ŠคํŠธ_์ž ๊น€ํ•ด์ œ.xlsx"๋ผ๋Š” ์ƒˆ ํŒŒ์ผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. # ์•”ํ˜ธ ํ•ด์ œ๋œ ํŒŒ์ผ ์ฝ๊ธฐ wb = load_workbook("์›”๋ณ„๊ตฌ๋งค๊ณ ๊ฐ๋ฆฌ์ŠคํŠธ_์ž ๊น€ํ•ด์ œ.xlsx") ws = wb.active # ์›Œํฌ์‹œํŠธ์˜ ๋ชจ๋“  ํ–‰์„ ์ˆœํšŒํ•˜์—ฌ ์ถœ๋ ฅ for row in ws.iter_rows(values_only=True): print(row) # ์•”ํ˜ธ ํ•ด์ œ๋œ ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ import os os.remove("์›”๋ณ„๊ตฌ๋งค๊ณ ๊ฐ๋ฆฌ์ŠคํŠธ_์ž ๊น€ํ•ด์ œ.xlsx") ์ด๋ ‡๊ฒŒ ์•”ํ˜ธ ํ•ด์ œ ํ›„ ์ƒˆ๋กœ ์ €์žฅ๋œ ํŒŒ์ผ๋กœ ๋ฐ์ดํ„ฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ž‘์—… ์™„๋ฃŒ ํ›„ ์ฝ”๋“œ๋ฅผ ์ข…๋ฃŒํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” os.remove๋ฅผ ์‚ฌ์šฉํ•ด ์•”ํ˜ธ๋ฅผ ํ•ด์ œํ•œ ํŒŒ์ผ์„ ๋‹ค์‹œ ์‚ญ์ œํ•˜๋„๋ก ๋ช…๋ นํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•”ํ˜ธํ™”๋œ ํŒŒ์ผ์˜ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ์ด์ฒ˜๋Ÿผ msoffcrypto-tool ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ msoffcrypto-tool์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฒˆ๊ฑฐ๋กญ๋‹ค๋ฉด ์—‘์…€ ํŒŒ์ผ์˜ ์•”ํ˜ธ๋ฅผ ์ˆ˜๋™์œผ๋กœ ๋จผ์ € ํ•ด์ œํ•œ ํ›„์— ํŒŒ์ด์ฌ์œผ๋กœ ๋ฐ์ดํ„ฐ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 04-03. ์—‘์…€ ์„œ์‹ ์„ค์ •ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ํŒŒ์ด์ฌ์œผ๋กœ ์—‘์…€์„ ๋‹ค๋ฃฐ ๋•Œ ์„œ์‹์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. openpyxl๋„ ๊ธ€๊ผด๊ณผ ์…€ ์„œ์‹, ํ‘œ์‹œ<NAME> ๋“ฑ ๊ธฐ๋ณธ์ ์ธ ์—‘์…€ ์„œ์‹์„ ์„ค์ •ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์—‘์…€ ์„œ์‹ ์ค‘ ์ฃผ์š”ํ•œ ์„œ์‹ ์„ค์ • ๋ช‡ ๊ฐ€์ง€๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธ€๊ผด๊ณผ ์…€ ์Šคํƒ€์ผ ์„ค์ • ๊ธ€๊ผด ์„ค์ • 'Font' ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€๊ผด์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from openpyxl import Workbook from openpyxl.styles import Font wb = Workbook() ws = wb.active cell = ws['A1'] cell.value = "Hello World" # ํฐํŠธ ์Šคํƒ€์ผ ์„ค์ •: ๋นจ๊ฐ„์ƒ‰, ์ดํƒค๋ฆญ ์ฒด, ๋ณผ๋“œ์ฒด, 20ํฌ์ธํŠธ ์‚ฌ์ด์ฆˆ๋กœ ์„ค์ • cell.font = Font(color='FF0000', italic=True, bold=True, size=20) wb.save('์—‘์…€ ์„œ์‹. xlsx') # ์ €์žฅ๋œ ์—‘์…€ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('์—‘์…€ ์„œ์‹. xlsx') openpyxl.styles์˜ Font ํด๋ž˜์Šค๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ํ›„ ์‹œํŠธ A1 ์…€์— ๊ธ€๊ผด ์„œ์‹์„ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. color๋กœ ๊ธ€์ž์ƒ‰์„ ์ง€์ •ํ•˜๊ณ , italic์œผ๋กœ ๊ธฐ์šธ์ž„์„, bold๋กœ ๊ธ€์ž์˜ ๊ตต๊ธฐ๋ฅผ, size๋กœ ๊ธ€์ž ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ธ€๊ผด ์˜ต์…˜๋“ค ์ค‘ ํ•„์š”ํ•œ ๊ฒƒ๋“ค๋งŒ ์„ ํƒ์ ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์…€ ์„œ์‹ ์„ค์ • ์ด๋ฒˆ์—๋Š” ์…€ ์„œ์‹์„ ์„ค์ •ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import Workbook from openpyxl.styles import Font wb = Workbook() ws = wb.active cell = ws['A1'] cell.value = "Hello World" # ํฐํŠธ ์Šคํƒ€์ผ ์„ค์ •: ๋นจ๊ฐ„์ƒ‰, ์ดํƒค๋ฆญ ์ฒด, ๋ณผ๋“œ์ฒด, 20ํฌ์ธํŠธ ์‚ฌ์ด์ฆˆ๋กœ ์„ค์ • cell.font = Font(color='FF0000', italic=True, bold=True, size=20) # ์…€ ๋„ˆ๋น„ ์„ค์ • (A ์—ด์˜ ๋„ˆ๋น„๋ฅผ 50์œผ๋กœ ์„ค์ •) ws.column_dimensions['A'].width = 50 # ์…€ ๋†’์ด ์„ค์ • (1 ํ–‰์˜ ๋†’์ด๋ฅผ 50์œผ๋กœ ์„ค์ •) ws.row_dimensions[1].height = 50 # ์…€ ๋ฐฐ๊ฒฝ๊ฐ’ ์„ค์ • (๋…ธ๋ž€์ƒ‰, ์ƒ‰์ƒ ์ฝ”๋“œ 'FFFF00') from openpyxl.styles import PatternFill yellow_fill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid') cell.fill = yellow_fill # ์…€ ํ…Œ๋‘๋ฆฌ ์„ค์ •: ๋ชจ๋“  ์‚ฌ์ด๋“œ์— ์–‡์€ ํ…Œ๋‘๋ฆฌ ์Šคํƒ€์ผ ์ ์šฉ from openpyxl.styles import Border, Side thin_border = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin')) cell.border = thin_border # ์…€ ์ •๋ ฌ ์„ค์ •: ์…€ ๋‚ด์šฉ์„ ์ˆ˜ํ‰ ๋ฐ ์ˆ˜์ง ์ค‘์•™ ์ •๋ ฌ from openpyxl.styles import Alignment cell.alignment = Alignment(horizontal='center', vertical='center') wb.save('์—‘์…€ ์„œ์‹. xlsx') # ์ €์žฅ๋œ ์—‘์…€ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('์—‘์…€ ์„œ์‹. xlsx') ์…€ ์„œ์‹ ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์…€ ๋„ˆ๋น„ ์„ค์ • (A ์—ด์˜ ๋„ˆ๋น„๋ฅผ 50์œผ๋กœ ์„ค์ •) ws.column_dimensions['A'].width = 50 # ์…€ ๋†’์ด ์„ค์ • (1 ํ–‰์˜ ๋†’์ด๋ฅผ 50์œผ๋กœ ์„ค์ •) ws.row_dimensions[1].height = 50 ์…€์˜ ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ์„ค์ •ํ•˜๋ ค๋ฉด ์—ด๊ณผ ํ–‰์˜ width์™€ height ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์†์„ฑ์€ column_dimensions์™€ row_dimensions๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ด ์ด๋ฆ„๊ณผ ํ–‰ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ๊ฐ ์ „๋‹ฌํ•ด ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๊ณ  width์™€ height์— ์„ค์ •๊ฐ’์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฝ”๋“œ์ƒ ๋„ˆ๋น„์™€ ๋†’์ด์˜ ์„ค์ •๊ฐ’์„ ๋‘˜ ๋‹ค 50์œผ๋กœ ๋„ฃ์—ˆ๋Š”๋ฐ ์‹ค์ œ ์ƒ์„ฑ๋œ ํŒŒ์ผ์„ ์—ด์–ด๋ณด๋ฉด ๋„ˆ๋น„์™€ ๋†’์ด๊ฐ€ ๊ฐ™์•„ ๋ณด์ด์ง€ ์•Š์•„์„œ ์˜์•„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์—‘์…€์˜ ๊ธฐ๋ณธ ๋„ˆ๋น„์™€ ๋†’์ด ๋‹จ์œ„๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ–‰ ๋†’์ด ๋‹จ์œ„๋Š” ํฌ์ธํŠธ(pt)๋กœ 1ํฌ์ธํŠธ๋Š” ์šฐ๋ฆฌ์—๊ฒŒ ์ต์ˆ™ํ•œ cm ๊ธฐ์ค€์œผ๋กœ๋Š” ์•ฝ 0.035cm์ž…๋‹ˆ๋‹ค. ์ด์™€ ๋‹ฌ๋ฆฌ ์—ด ๋„ˆ๋น„๋Š” ๋ฌธ์ž ๋„ˆ๋น„ ๋‹จ์œ„๋กœ, ๋„ˆ๋น„ 1์€ ํ‘œ์ค€ ๊ธ€๊ผด ์„œ์‹์˜ ํ•œ ๊ธ€์ž๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ์—‘์…€์˜ ๋†’์ด์™€ ๋„ˆ๋น„ ๋‹จ์œ„๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ ์„ ๊ฐ์•ˆํ•˜๊ณ  ์„ค์ •ํ•˜๊ณ ์ž ํ•˜๋Š” ํฌ๊ธฐ ๊ฐ’์„ ์ž…๋ ฅํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # ์…€ ๋ฐฐ๊ฒฝ๊ฐ’ ์„ค์ • (๋…ธ๋ž€์ƒ‰, ์ƒ‰์ƒ ์ฝ”๋“œ 'FFFF00') from openpyxl.styles import PatternFill yellow_fill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid') cell.fill = yellow_fill ๋‹ค์Œ์€ ์…€ ๋ฐฐ๊ฒฝ์ƒ‰ ์„ค์ •์ž…๋‹ˆ๋‹ค. ์…€์˜ ๋ฐฐ๊ฒฝ์ƒ‰์€ openpyxl.styles ๋ชจ๋“ˆ์˜ PatternFill ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. PatternFill์„ ์‚ฌ์šฉํ•  ๋•Œ fill_type์œผ๋กœ ๋ฐฐ๊ฒฝ์ƒ‰์˜ ํŒจํ„ด ์œ ํ˜•์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋‹จ์ƒ‰์œผ๋กœ ๋„ฃ๊ธฐ ์œ„ํ•ด fill_type์„ 'solid'๋กœ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  start_color์™€ end_color๋กœ ์ƒ‰์ƒ์˜ ๊ฐ’์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ปฌ๋Ÿฌ์˜ ์†์„ฑ์ด ๋‘ ๊ฐ€์ง€์ธ ์ด์œ ๋Š” ๊ทธ๋Ÿฌ๋ฐ์ด์…˜ ํšจ๊ณผ๋ฅผ ์ ์šฉํ•  ๋•Œ ์‹œ์ž‘ ๋ถ€๋ถ„๊ณผ ์ข…๋ฃŒ ๋ถ€๋ถ„์—์„œ ์‚ฌ์šฉํ•  ์ƒ‰์ƒ์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๊ทธ๋Ÿฌ๋ฐ์ด์…˜์ด ์•„๋‹Œ ๋‹จ์ƒ‰์ธ ๊ฒฝ์šฐ์—๋„ ๋‘ ๊ฐ’์„ ๋ชจ๋‘ ์ž…๋ ฅํ•ด ์ค˜์•ผ ํ•˜๋ฉฐ, ๊ทธ๋Ÿด ๊ฒฝ์šฐ ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์‹œ์ž‘ ์ƒ‰์ƒ๊ณผ ์ข…๋ฃŒ ์ƒ‰์ƒ์„ ๋ชจ๋‘ ๋™์ผํ•œ ๊ฐ’์œผ๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ƒ‰์ƒ์˜ ๊ฐ’์€ HTML ์ƒ‰์ƒ ์ฝ”๋“œ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. # ์…€ ํ…Œ๋‘๋ฆฌ ์„ค์ •: ๋ชจ๋“  ์‚ฌ์ด๋“œ์— ์–‡์€ ํ…Œ๋‘๋ฆฌ ์Šคํƒ€์ผ ์ ์šฉ from openpyxl.styles import Border, Side thin_border = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin')) cell.border = thin_border ์…€์˜ ํ…Œ๋‘๋ฆฌ๋Š” openpyxl.styles์˜ Border์™€ Side ํด๋ž˜์Šค๋กœ ์„œ์‹์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Border๋กœ ์…€์— ํ…Œ๋‘๋ฆฌ๋ฅผ ์„ค์ •ํ•  ๋ฐฉํ–ฅ์„ ์ง€์ •ํ•˜๊ณ , Side๋กœ ํ…Œ๋‘๋ฆฌ์˜ ์Šคํƒ€์ผ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ƒํ•˜์ขŒ์šฐ์— ๋ชจ๋‘ ํ…Œ๋‘๋ฆฌ ์Šคํƒ€์ผ์„ ์–‡๊ฒŒ ๋งŒ๋“ค๋„๋ก ๋ชจ๋‘ ๋™์ผํ•˜๊ฒŒ Side(style='thin')์„ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ 'left', 'right', 'top', 'bottom'์˜ ์Šคํƒ€์ผ์„ ๊ฐ๊ฐ ๋‹ค๋ฅด๊ฒŒ ์ง€์ •ํ•˜๋ฉด ์ ์„ , ๋‘๊บผ์šด ํ…Œ๋‘๋ฆฌ ๋“ฑ ์…€์˜ ์ƒํ•˜์ขŒ์šฐ ํ…Œ๋‘๋ฆฌ์˜ ์Šคํƒ€์ผ์„ ๊ฐ๊ฐ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ์…€ ์ •๋ ฌ ์„ค์ •: ์…€ ๋‚ด์šฉ์„ ์ˆ˜ํ‰ ๋ฐ ์ˆ˜์ง ์ค‘์•™ ์ •๋ ฌ from openpyxl.styles import Alignment cell.alignment = Alignment(horizontal='center', vertical='center') ์…€์— ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์€ openpyxl.styles์˜ Alignment ํด๋ž˜์Šค๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. Alignment์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ horizontal๋กœ ์ˆ˜ํ‰ ์ •๋ ฌ ๋ฐฉ์‹์„, vertical๋กœ ์ˆ˜์ง ์ •๋ ฌ ๋ฐฉ์‹์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ˆ˜์ง๊ณผ ์ˆ˜ํ‰ ๋ชจ๋‘ 'center'์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ํ…์ŠคํŠธ๊ฐ€ ์…€์˜ ์ˆ˜์ง ์ค‘์•™์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. ํ‘œ์‹œ<NAME> ์„ค์ • ์•ž์—์„œ ์…€๊ณผ ๊ธ€์ž์˜ ์Šคํƒ€์ผ์„ ์„ค์ •ํ–ˆ๋‹ค๋ฉด ์ด๋ฒˆ์—๋Š” ์…€์— ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ์˜<NAME>์„ ์„ค์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜์™€ ์ฒœ ๋‹จ์œ„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ ์„œ์‹ ์„ค์ • ๋‹ค์–‘ํ•œ ์ˆซ์ž ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ์‹ค๋ฌด์—์„œ๋Š” ์ฃผ๋กœ ๋ฐ์ดํ„ฐ์˜ ์„ฑ๊ฒฉ์— ๋”ฐ๋ผ ์ˆซ์ž์˜<NAME>์„ ๋‹ค๋ฅด๊ฒŒ ํ‘œํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. openpyxl์˜ number_format์„ ์ด์šฉํ•ด์„œ ์ฒœ ๋‹จ์œ„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ, ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜ ๋“ฑ ์ˆซ์ž๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ์‹์„ ์ง€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ws['A1'] = 1234567.890123 # ์ฒœ ๋‹จ์œ„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ์™€ ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜ ์ง€์ • ws['A1'].number_format = '#,##0.00' ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜๋Š” ๋งˆ์นจํ‘œ(.)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. number_format ์†์„ฑ์— ์„œ์‹ ์ฝ”๋“œ๋ฅผ ๋„ฃ์„ ๋•Œ ๋งˆ์นจํ‘œ ๋’ค์— ์ž๋ฆฟ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” 0 ๋˜๋Š” #์„ ๋ถ™์—ฌ์„œ ์†Œ์ˆ˜์  ์•„๋ž˜ ๋ช‡ ๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€ ํ‘œ์‹œํ• ์ง€ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž ์„œ์‹์—์„œ ์ž๋ฆฟ์ˆ˜๋ฅผ 0์œผ๋กœ ์‚ฌ์šฉํ•  ๋•Œ์™€ #์œผ๋กœ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ํ‘œ์‹œ๋˜๋Š” ๊ฒฐ๊ด๊ฐ’์— ์กฐ๊ธˆ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 0์„ ์‚ฌ์šฉํ•˜๋ฉด ์œ ํšจํ•˜์ง€ ์•Š์€ 0์„ ํฌํ•จํ•˜๊ณ , #์€ ์œ ํšจํ•˜์ง€ ์•Š์€ 0์„ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์…€์˜ ๊ฐ’์ด 0.0์ผ ๊ฒฝ์šฐ, 0์€ ๋‘˜ ๋‹ค ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์„œ์‹์„ #.#์œผ๋กœ ์ง€์ •ํ•˜๋ฉด ์œ ํšจํ•˜์ง€ ์•Š์€ 0์„ ์ œ์™ธํ•˜๊ณ  '.'๋งŒ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์‹œํ•˜๋ ค๋ฉด ์„œ์‹์„ 0.0์œผ๋กœ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž ์„œ์‹์— ์ฒœ ๋‹จ์œ„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋„ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ž๋ฆฟ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” #๊ณผ 0์„ ์‚ฌ์šฉํ•ด ์ฒœ ๋‹จ์œ„๋งˆ๋‹ค ์‰ผํ‘œ(,)๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, #,###์ด๋‚˜ #,##0๊ณผ ๊ฐ™์ด ์„œ์‹์„ ์ž…๋ ฅํ•˜๋Š”๋ฐ ๋ฐ์ดํ„ฐ๊ฐ€ 0์ผ ๋•Œ ๊ณต๋ฐฑ์œผ๋กœ ํ‘œ์‹œํ•˜๋ ค๋ฉด #,###์„, 0์œผ๋กœ ๊ทธ๋Œ€๋กœ ํ‘œ์‹œํ•˜๋ ค๋ฉด #,##0์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ†ตํ™” ๊ธฐํ˜ธ์™€ ํผ์„ผํŠธ ์„œ์‹ ์„ค์ • ์ฒœ ๋‹จ์œ„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋ฅผ ํ†ตํ™” ๊ธฐํ˜ธ์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์—ฌ ๊ธˆ์•ก์„ ๋‚˜ํƒ€๋‚ด๋„๋ก ์„œ์‹์„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ws['D1'] = 1234567.89 # ํ†ตํ™” ๊ธฐํ˜ธ์™€ ์ฒœ ๋‹จ์œ„ ๊ตฌ๋ถ„ ๋‹จ์œ„ ํ‘œ์‹œ ws['D1'].number_format = 'โ‚ฉ#,##0' ์œ„์—์„œ ์‚ฌ์šฉํ•œ ์ˆซ์ž<NAME> ์•ž์— ํ†ตํ™” ๊ธฐํ˜ธ๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ๊ธˆ์•ก์„ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›ํ™”๋Š” ์†Œ์ˆ˜์ ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ, ๋‹ฌ๋Ÿฌ์™€ ๊ฐ™์ด ์†Œ์ˆ˜์ ์„ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š” ํ†ตํ™”์ผ ๊ฒฝ์šฐ์—๋Š” ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜๋„ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ws['B1'] = 0.12345 # ํผ์„ผํŠธ<NAME>์œผ๋กœ ํ‘œ์‹œ ws['B1'].number_format = '0.00%' ์ด์™€ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ํผ์„ผํŠธ<NAME>๋„ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‘œ์‹œํ•  ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜๋ฅผ ์ง€์ •ํ•œ ํ›„ ๊ทธ ๋’ค์— ํผ์„ผํŠธ(%) ๊ธฐํ˜ธ๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ๋ฐฑ๋ถ„์œจ๋กœ ๊ฐ’์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ์„œ์‹ ์„ค์ • ์ด๋ฒˆ์—๋Š” ๋‚ ์งœ์™€ ์‹œ๊ฐ„<NAME>์„ ํ‘œ์‹œํ•˜๋Š” ์„œ์‹์„ ์„ค์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from datetime import datetime ws['C1'] = datetime(2023, 9, 7) # ๋‚ ์งœ ์„œ์‹ ์„ค์ • ws['C1'].number_format = 'MM-DD-YYYY' ํŒŒ์ด์ฌ์—์„œ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ์ฒ˜๋ฆฌํ•  ๋•Œ๋Š” datetime ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” datetime ๋ชจ๋“ˆ์˜ datetime ํด๋ž˜์Šค๋ฅผ ๋ถˆ๋Ÿฌ์™€ 23๋…„ 9์›” 7์ผ์ด๋ผ๋Š” ํŠน์ • ๋‚ ์งœ๋ฅผ ๋‚˜ํƒ€๋ƒˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ '์—ฐ๋„-์›”-์ผ'<NAME>์œผ๋กœ ๋‚ ์งœ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์„œ์‹์„ 'YYYY-MM-DD'๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‚ ์งœ ์™ธ์— ์‹œ๊ฐ„๋„ ํ•จ๊ป˜ ํ‘œ์‹œํ•˜๋ ค๋ฉด 'hh(์‹œ๊ฐ„)'์™€ 'mm(๋ถ„)'์œผ๋กœ ์„œ์‹์„ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 04-04. ์‹ค์ „! ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœํ•˜์—ฌ ์ƒˆ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ธฐ ์‹ค๋ฌด์—์„œ๋Š” ํ•œ ํŒŒ์ผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ๋‹ค๋ฅธ ์—‘์…€ ํŒŒ์ผ์— ๋ถ™์—ฌ ๋„ฃ๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ํŒŒ์ผ์„ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋“ค์ด ๋งŽ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ ๋ฐœ์ƒํ•˜๋Š” ์—…๋ฌด๋ผ๋ฉด ๋ณต์‚ฌ์™€ ๋ถ™์—ฌ๋„ฃ๊ธฐ๋ฅผ ์ˆ˜์ž‘์—…์œผ๋กœ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋น ๋ฅด๊ฒ ์ง€๋งŒ, ๋งค์›” / ๋งค์ฃผ / ๋งค์ผ ๋ฐœ์ƒํ•˜๋Š” ์—…๋ฌด๋ผ๋ฉด ์ด๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ ํ•ฉ์น˜๊ธฐ ์ž‘์—…๋„ ์ž๋™ํ™”ํ•˜๋Š” ๊ฒƒ์ด ์—…๋ฌด ์‹œ๊ฐ„๊ณผ ๋ฆฌ์†Œ์Šค๋ฅผ ๋‹จ์ถ•์‹œ์ผœ์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•ž์—์„œ ํ•™์Šตํ•œ ๋‚ด์šฉ์„ ์‘์šฉํ•˜์—ฌ ์—‘์…€ ๋ฌธ์„œ ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์˜ฎ๊ธฐ๋Š” ์ž‘์—…์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋ฌธ์„œ์—์„œ ๋ฐ์ดํ„ฐ ๋ณต์‚ฌํ•˜์—ฌ ์ƒˆ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ธฐ ์•ž์„œ ์‚ฌ์šฉํ–ˆ๋˜ '์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx'์˜ '10์›”', '11์›”', '12์›”' ์‹œํŠธ์—์„œ ์‹ ๊ทœ ๊ณ ๊ฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋งŒ ๋ณ„๋„๋กœ ์ถ”์ถœํ•œ ํ›„ ์ œํ’ˆ๋ณ„๋กœ ์‹œํŠธ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„ , '10์›”' ํ•˜๋‚˜์˜ ์‹œํŠธ์—์„œ ์‹ ๊ทœ ๊ณ ๊ฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋จผ์ € ์ถ”์ถœํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx', data_only=True) # '10์›”' ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ws = wb['10์›”'] # ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ํ–‰์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ new_rows = list(ws.rows)[2:] # F ์—ด(์ธ๋ฑ์Šค 5)์ด '์‹ ๊ทœ'์ธ ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœ for row in new_rows: if row[5].value == '์‹ ๊ทœ': row_values = [cell.value for cell in row] print(row_values) new_wb.save(filename='์ œํ’ˆ๋ณ„ ์‹ ๊ทœ ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # ์ถœ๋ ฅ๊ฐ’ [datetime.datetime(2023, 10, 16, 0, 0), 'B ์ œํ’ˆ', 1, '์ด ๋‚จ์—ฐ', '๋‹ค ์•Œ ์•„์—ฐ ๊ตฌ์†Œ', '์‹ ๊ทœ', 18000, 18000] [datetime.datetime(2023, 10, 22, 0, 0), 'B ์ œํ’ˆ', 1, '์ตœ์—ฐํ™”', '์ผ์ผ ์ปดํผ๋‹ˆ', '์‹ ๊ทœ', 18000, 18000] [datetime.datetime(2023, 10, 29, 0, 0), 'C ์ œํ’ˆ', 4, 'ํ™ฉ์ˆ˜์ง€', '์†Œ์† ์—†์Œ', '์‹ ๊ทœ', 36000, 144000] ์‹ ๊ทœ/๊ธฐ์กด ๊ณ ๊ฐ ์—ฌ๋ถ€๋Š” '์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx'์˜ '10์›”' ์‹œํŠธ F ์—ด์— ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋Š”๋ฐ, F ์—ด์€ ์ธ๋ฑ์Šค๋กœ๋Š” 5์ด๋ฏ€๋กœ if row[5].value == '์‹ ๊ทœ': ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์‹ ๊ทœ ๊ณ ๊ฐ๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ๋ชจ๋ฅด๋Š” ๊ฒฝ์šฐ์—๋Š” ์•ž์„œ ํ•™์Šตํ–ˆ๋˜ ' ์—ด์ด๋ฆ„ - ์ธ๋ฑ์Šค ๋ณ€ํ™˜ ' ํ•จ์ˆ˜(column_index_from_string(์—ด ์ด๋ฆ„))๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด๋ฒˆ์—๋Š”<NAME>์ด ๋™์ผํ•œ '11์›”', '12์›”' ์‹œํŠธ๊นŒ์ง€ ๋ชจ๋‘ ๋™์ผํ•œ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import load_workbook # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx', data_only=True) # ์ฒ˜๋ฆฌํ•  ์›”๋ณ„ ์‹œํŠธ๋ฅผ ๋ชฉ๋ก์„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅ months = ['10์›”', '11์›”', '12์›”'] # ๊ฐ ์›”๋ณ„๋กœ ๋ฐ˜๋ณต for month in months: ws = wb[month] # ํ•ด๋‹น ์›”์˜ ์‹œํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ new_rows = list(ws.rows)[2:] # ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ํ–‰์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ # F ์—ด(์ธ๋ฑ์Šค 5)์ด '์‹ ๊ทœ'์ธ ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœ for row in new_rows: if row[5].value == '์‹ ๊ทœ': row_values = [cell.value for cell in row] print(row_values) #์ถœ๋ ฅ๊ฐ’ [datetime.datetime(2023, 10, 16, 0, 0), 'B ์ œํ’ˆ', 1, '์ด ๋‚จ์—ฐ', '๋‹ค ์•Œ ์•„์—ฐ ๊ตฌ์†Œ', '์‹ ๊ทœ', 18000, 18000] [datetime.datetime(2023, 10, 22, 0, 0), 'B ์ œํ’ˆ', 1, '์ตœ์—ฐํ™”', '์ผ์ผ ์ปดํผ๋‹ˆ', '์‹ ๊ทœ', 18000, 18000] [datetime.datetime(2023, 10, 29, 0, 0), 'C ์ œํ’ˆ', 4, 'ํ™ฉ์ˆ˜์ง€', '์†Œ์† ์—†์Œ', '์‹ ๊ทœ', 36000, 144000] [datetime.datetime(2023, 11, 16, 0, 0), 'C ์ œํ’ˆ', 1, '์œค์„œ์›', 'ใˆœ์„œ์›', '์‹ ๊ทœ', 36000, 36000] [datetime.datetime(2023, 11, 28, 0, 0), 'A ์ œํ’ˆ', 1, '์œค์„œ์šฐ', '์ฃผ์‹ํšŒ์‚ฌ ๋ผ์ด์–ธ', '์‹ ๊ทœ', 20000, 20000] [datetime.datetime(2023, 11, 30, 0, 0), 'A ์ œํ’ˆ', 2, '์ด๋„์ค€', '์•„๋ฆ„ ๋Œ€ํ•™๊ต', '์‹ ๊ทœ', 20000, 40000] [datetime.datetime(2023, 12, 17, 0, 0), 'C ์ œํ’ˆ', 1, '์ตœ๋ฏผ์€', '์•„์ž์ฝ”ํผ๋ ˆ์ด์…˜', '์‹ ๊ทœ', 36000, 36000] [datetime.datetime(2023, 12, 22, 0, 0), 'B ์ œํ’ˆ', 1, '๋ฐ•์„œ์€', '์ƒ์ƒ ๋Œ€ํ•™๊ต', '์‹ ๊ทœ', 18000, 18000] [datetime.datetime(2023, 12, 27, 0, 0), 'A ์ œํ’ˆ', 4, '์ž„์ง€์€', '์ฃผ์‹ํšŒ์‚ฌ ์ง€์€', '์‹ ๊ทœ', 20000, 80000] ๋ชจ๋“  ์‹œํŠธ์˜<NAME>์ด ๋™์ผํ•˜๋ฏ€๋กœ ์‹คํ–‰ํ•  ์‹œํŠธ์˜ ๋ชฉ๋ก์„ ๋ฆฌ์ŠคํŠธํ˜•์œผ๋กœ ์ €์žฅํ•œ ๋‹ค์Œ for ๋ฌธ์œผ๋กœ ํ•ด๋‹น ์‹œํŠธ๋“ค์„ ๋ชจ๋‘ ๋Œ๋ฉฐ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค. ๋งŒ์•ฝ<NAME>์ด ๋‹ค๋ฅด๋‹ค๋ฉด ์ฝ”๋“œ๋ฅผ ๊ฐ๊ฐ ๋‹ค๋ฅด๊ฒŒ ์จ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š”<NAME>์„ ๋จผ์ € ๋™์ผํ•˜๊ฒŒ ์ˆ˜์ •ํ•˜๋Š” ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋ ‡๊ฒŒ ์ถ”์ถœํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œํ’ˆ๋ณ„๋กœ ๋‚˜๋ˆ ์„œ ๊ฐ๊ฐ์˜ ์‹œํŠธ๋กœ ์ €์žฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œํ’ˆ๋ณ„๋กœ ์–ด๋–ค ์‹œํŠธ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š”์ง€ ์ง๊ด€์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ์‹œํŠธ ์ด๋ฆ„์€ ๊ฐ๊ฐ์˜ ์ œํ’ˆ๋ช…์œผ๋กœ ์ €์žฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import load_workbook from openpyxl import Workbook # ์›๋ณธ ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename='์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx', data_only=True) # ์ƒˆ๋กœ์šด ์—‘์…€ ํŒŒ์ผ ์ƒ์„ฑ new_wb = Workbook() new_ws = new_wb.active # ์ฒ˜๋ฆฌํ•  ์›”๋ณ„ ์‹œํŠธ๋ฅผ ๋ชฉ๋ก์„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅ months = ['10์›”', '11์›”', '12์›”'] # ๊ฐ ์›”๋ณ„๋กœ ๋ฐ˜๋ณต for month in months: ws = wb[month] index_row = [cell.value for cell in list(ws.rows)[1]] # ์›๋ณธ ํŒŒ์ผ์˜ ๋‘ ๋ฒˆ์งธ ํ–‰(์ธ๋ฑ์Šค) new_rows = list(ws.rows)[2:] # ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ํ–‰์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ # ๋ฐ์ดํ„ฐ ๋ถ„๋ฅ˜ ๋ฐ ์ €์žฅ for row in new_rows: if row[5].value == '์‹ ๊ทœ': # F ์—ด(์ธ๋ฑ์Šค 5)์ด '์‹ ๊ทœ'์ธ ๊ฒฝ์šฐ product = row[1].value # B ์—ด(์ธ๋ฑ์Šค 1)์˜ ์ œํ’ˆ ์ด๋ฆ„ # ํ•ด๋‹น ์ œํ’ˆ ์‹œํŠธ๊ฐ€ ์—†์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑ if product not in new_wb.sheetnames: new_wb.create_sheet(title=product) product_ws = new_wb[product] product_ws.append(index_row) # ์ธ๋ฑ์Šค ํ–‰์„ ์ƒˆ ์‹œํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ–‰์— ์ถ”๊ฐ€ # ํ•ด๋‹น ์‹œํŠธ๋ฅผ ์„ ํƒ product_ws = new_wb[product] # ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ product_ws.append([cell.value for cell in row]) # ์ž„์‹œ๋กœ ๋งŒ๋“  ์‹œํŠธ ์‚ญ์ œ del new_wb['Sheet'] # ์ƒˆ๋กœ์šด ์—‘์…€ ํŒŒ์ผ ์ €์žฅ new_wb.save(filename='์ œํ’ˆ๋ณ„ ์‹ ๊ทœ ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') # ์ €์žฅ๋œ ์—‘์…€ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('์ œํ’ˆ๋ณ„ ์‹ ๊ทœ ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') ์œ„์˜ ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ๋‹ค์‹œ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. new_wb = Workbook() new_ws = new_wb.active ๋จผ์ € ์ถ”์ถœํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์ƒˆ๋กœ์šด ์—‘์…€ ์›Œํฌ๋ถ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ธฐ์–ตํ•ด์•ผ ํ•  ์ ์€ ์›Œํฌ์‹œํŠธ์˜ ์ด๋ฆ„์„ ๋ณ„๋„๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ฒ˜์Œ ์ƒ์„ฑ๋œ ์›Œํฌ์‹œํŠธ์˜ ์‹œํŠธ๋ช…์€ 'Sheet'์ž…๋‹ˆ๋‹ค. # ์ฒ˜๋ฆฌํ•  ์›”๋ณ„ ์‹œํŠธ๋ฅผ ๋ชฉ๋ก์„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅ months = ['10์›”', '11์›”', '12์›”'] # ๊ฐ ์›”๋ณ„๋กœ ๋ฐ˜๋ณต for month in months: ws = wb[month] index_row = [cell.value for cell in list(ws.rows)[1]] # ์›๋ณธ ํŒŒ์ผ์˜ ๋‘ ๋ฒˆ์งธ ํ–‰(์ธ๋ฑ์Šค) new_rows = list(ws.rows)[2:] # ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ํ–‰์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ ์œ„์—์„œ ํ•ด๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ์‹œํŠธ ๋ชฉ๋ก์„ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅํ•œ ํ›„ for ๋ฌธ์œผ๋กœ ๋ชจ๋“  ์‹œํŠธ๋ฅผ ๋Œ๋ฉฐ ๋™์ผํ•œ ์ž‘์—…์„ ์‹คํ–‰ํ•ด ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์œ„์˜ ์ฝ”๋“œ์™€ ๋‹ค๋ฅด๊ฒŒ ์ถ”๊ฐ€๋œ ๋ถ€๋ถ„์€ index_row = [cell.value for cell in list(ws.rows)[1]]์ž…๋‹ˆ๋‹ค. ์›๋ณธ ๋ฐ์ดํ„ฐ '์›”๋ณ„ ๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx'์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•  ๋•Œ ์ธ๋ฑ์Šค ๋ถ€๋ถ„์„ ์ œ์™ธํ•˜๊ณ  ์ •๋ณด๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์„ธ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๊ฒŒ ํ•œ ๊ฒƒ์„ ๊ธฐ์–ตํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Œ€๋กœ ์ƒˆ๋กœ์šด ํŒŒ์ผ์— ์ €์žฅํ•˜๋ฉด ๊ทธ ํŒŒ์ผ์—๋Š” ์ธ๋ฑ์Šค ์ •๋ณด๊ฐ€ ์—†์–ด ๊ทธ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ๊ฐ ์–ด๋–ค ์ •๋ณด๋ฅผ ์˜๋ฏธํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ธ๋ฑ์Šค๋ฅผ ๋‹ค์‹œ ๋„ฃ์–ด์ฃผ๊ธฐ ์œ„ํ•ด ์›๋ณธ ํŒŒ์ผ์˜ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ๊ฐ€์ ธ์™€์„œ 'index_row'๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•ด ์ค๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๊ทธ๋ƒฅ ๋‘ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ค๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๊ทธ๋Ÿด ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ์— ์‹ ๊ทœ ๊ณ ๊ฐ์ด๋ผ๋Š” ์กฐ๊ฑด์„ ๊ฑธ์–ด ์ถ”์ถœํ•  ๋•Œ ๋‹ค์‹œ ์ธ๋ฑ์Šค ํ–‰์ด ์ œ์™ธ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. # ๋ฐ์ดํ„ฐ ๋ถ„๋ฅ˜ ๋ฐ ์ €์žฅ for row in new_rows: if row[5].value == '์‹ ๊ทœ': # F ์—ด(์ธ๋ฑ์Šค 5)์ด '์‹ ๊ทœ'์ธ ๊ฒฝ์šฐ ์ด์ œ ๋ชจ๋“  ์‹œํŠธ์—์„œ ํ–‰๋งˆ๋‹ค ๋Œ๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ, if ๋ฌธ์œผ๋กœ ๋จผ์ € F ์—ด์„ ๊ธฐ์ค€์œผ๋กœ '์‹ ๊ทœ'์ธ ๊ณ ๊ฐ์— ํ•ด๋‹นํ•  ๊ฒฝ์šฐ ์•„๋ž˜์˜ ์ž‘์—…์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. product = row[1].value # B ์—ด(์ธ๋ฑ์Šค 1)์˜ ์ œํ’ˆ ์ด๋ฆ„ # ํ•ด๋‹น ์ œํ’ˆ ์‹œํŠธ๊ฐ€ ์—†์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑ if product not in new_wb.sheetnames: new_wb.create_sheet(title=product) '์‹ ๊ทœ'์ธ ๊ณ ๊ฐ์— ํ•ด๋‹นํ•˜๋Š” ํ–‰์—์„œ B ์—ด(์ธ๋ฑ์Šค 1)์— ์žˆ๋Š” ์ œํ’ˆ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์™€์„œ 'product'๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ์ œํ’ˆ ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ์‹œํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด์ฃผ๊ธฐ ์œ„ํ•ด, ํŒŒ์ผ์˜ ์ „์ฒด ์‹œํŠธ ๋ชฉ๋ก์—์„œ ์ œํ’ˆ๋ช…๊ณผ ๋™์ผํ•œ ์‹œํŠธ๋ช…์ด ์—†๋Š” ๊ฒฝ์šฐ ์‹œํŠธ๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•˜๋„๋ก ๋ช…๋ นํ•ฉ๋‹ˆ๋‹ค. product_ws = new_wb[product] product_ws.append(index_row) # ์ธ๋ฑ์Šค ํ–‰์„ ์ƒˆ ์‹œํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ–‰์— ์ถ”๊ฐ€ ๊ทธ๋ฆฌ๊ณ  ์ œํ’ˆ๋ช…๊ณผ ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ ์ƒ์„ฑ๋œ ์‹œํŠธ์˜ ์ฒซ ํ–‰์— ์ธ๋ฑ์Šคํ–‰์„ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค. # ํ•ด๋‹น ์‹œํŠธ๋ฅผ ์„ ํƒ product_ws = new_wb[product] # ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ product_ws.append([cell.value for cell in row]) ์‹œํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” if ๋ฌธ์ด ์ข…๋ฃŒ๋œ ํ›„, ๋‹ค์‹œ ์ œํ’ˆ๋ช…๊ณผ ๋™์ผํ•œ ์ด๋ฆ„์˜ ์‹œํŠธ๋ฅผ ์„ ํƒํ•˜์—ฌ ๊ทธ ํ–‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‹œํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•ด๋†“์€ ๋ชจ๋“  ์‹œํŠธ์˜ ๋ชจ๋“  ํ–‰์„ ๋Œ๋ฉด์„œ ์œ„์˜ ์ž‘์—…์„ ์‹คํ–‰ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ ์‹œํŠธ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. # ์ž„์‹œ๋กœ ๋งŒ๋“  ์‹œํŠธ ์‚ญ์ œ del new_wb['Sheet'] # ์ƒˆ๋กœ์šด ์—‘์…€ ํŒŒ์ผ ์ €์žฅ new_wb.save(filename='์ œํ’ˆ๋ณ„ ์‹ ๊ทœ ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx') ๊ทธ๋Œ€๋กœ ํŒŒ์ผ์— ์ €์žฅํ•ด๋„ ๋ฐ์ดํ„ฐ๋Š” ์ด๋ฏธ ๋‹ค ์˜ฎ๊ฒจ์ ธ์žˆ์ง€๋งŒ, ์ฒ˜์Œ ์—‘์…€ ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ๋•Œ ์ƒ์„ฑ๋œ ๊ธฐ๋ณธ ํ™œ์„ฑ ์‹œํŠธ "Sheet"๋„ ์—‘์…€ ํŒŒ์ผ์— ๋นˆ ์‹œํŠธ๋กœ ๋‚จ์•„์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์กฐ๊ธˆ ๋” ๊น”๋”ํ•œ ์—…๋ฌด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด "Sheet"๋ฅผ ์‹œํŠธ๋ฅผ ์‚ญ์ œํ•œ ํ›„ ํŒŒ์ผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. 04-05. ์‹ค์ „! ์ผ์ž๋ณ„ ํŒฉ์Šค ๋‚ด์—ญ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ํ•ฉ์น˜๊ธฐ ๋™์ผํ•œ<NAME>์˜ ์—‘์…€ ํŒŒ์ผ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ํ•ฉ์น˜๊ธฐ ์ด๋ฒˆ์—๋Š” ๋™์ผํ•œ<NAME>์˜ ์—‘์…€ ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, A ํšŒ์‚ฌ์— ์ „์ž ํŒฉ์Šค๊ฐ€ ์ˆ˜์‹ ๋  ๋•Œ๋งˆ๋‹ค ์š”์•ฝ๋œ ์ •๋ณด๊ฐ€ ๊ฐ๊ฐ ์—‘์…€ ํŒŒ์ผ๋กœ ํ•˜๋‚˜์”ฉ ์ƒ์„ฑ๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ๋ช…์ด ์ˆ˜์‹ ๋œ ์ผ์ž์™€ ์‹œ๊ฐ„์œผ๋กœ ์ €์žฅ๋œ๋‹ค๊ณ  ํ•ด๋„ ํŠน์ • ์‹œ๊ฐ„๋Œ€์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒฉ์Šค๊ฐ€ ํ•œ ๋ฒˆ์— ์˜ฌ ๊ฒฝ์šฐ, ํŠน์ •ํ•œ ์–ด๋–ค ํŒฉ์Šค์˜ ๋ฐœ์‹ ๋ฒˆํ˜ธ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ทธ ์‹œ๊ฐ„๋Œ€์˜ ๋ชจ๋“  ํŒŒ์ผ์„ ํ•˜๋‚˜์”ฉ ์—ด์–ด๋ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์„ ๊ฐ์ˆ˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŒŒ์ผ์„ ์ผ๋‹จ ์œ„๋‚˜ ์ฃผ ๋‹จ์œ„๋กœ, ํ˜น์€ ์›”๋‹จ์œ„๋กœ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ๋ฌถ์–ด์„œ ์ €์žฅํ•œ๋‹ค๋ฉด, ๊ตณ์ด ํ•˜๋‚˜์”ฉ ์—ด์–ด๋ณด์ง€ ์•Š์•„๋„ ํ•˜๋‚˜์˜ ํŒŒ์ผ์—์„œ ์›ํ•˜๋Š” ๋‚ด์šฉ์„ ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ 10์›”์— ์ƒ์„ฑ๋œ 30๊ฐœ์˜ ํŒฉ์Šค ์š”์•ฝ ํŒŒ์ผ์„ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ๋ชจ๋‘ ํ•ฉ์ณ์„œ ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ํŒŒ์ผ์€ "YYYY-MM-DD hhmm.xlsx"์˜<NAME>์œผ๋กœ ํŒฉ์Šค ์ˆ˜์‹  ์ผ์‹œ๊ฐ€ ํŒŒ์ผ๋ช…์œผ๋กœ ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŒŒ์ผ์ด ์ €์žฅ๋œ ์œ„์น˜์—๋Š” ํŒฉ์Šค ํŒŒ์ผ๋งŒ์ด ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์—‘์…€ ํŒŒ์ผ๋“ค๋„ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ '3-11 ์œˆ๋„ ํด๋” ๋ฐ ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ'์—์„œ ๋ฐฐ์šด listdir ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•ด ์ „์ฒด ํŒŒ์ผ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์™€์„œ ํŒฉ์Šค ํŒŒ์ผ๋ช… ๊ทœ์น™์— ํ•ด๋‹นํ•˜๋Š” ํŒŒ์ผ๋งŒ ์ถ”์ถœํ•œ๋‹ค๋ฉด ํŒŒ์ผ์„ ๊ณจ๋ผ๋‚ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ์ฝ”๋“œ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import Workbook, load_workbook import os import re # ์ƒˆ๋กœ์šด ์—‘์…€ ํŒŒ์ผ ์ƒ์„ฑ new_wb = Workbook() new_ws = new_wb.active new_ws.title = "์ˆ˜์‹  ๋‚ด์—ญ" # ํ—ค๋” ํ–‰ ์ถ”๊ฐ€ new_ws.append(['์ˆ˜์‹  ์‹œ๊ฐ„', '๋ฐœ์‹ ๋ฒˆํ˜ธ', 'ํŽ˜์ด์ง€ ์ˆ˜', '์šฉ๋Ÿ‰']) # ํŒŒ์ผ ๋ช…์— ๋งž๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ํŒจํ„ด pattern = re.compile(r'\d{4}-\d{2}-\d{2} \d{4}\.xlsx') # ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋ชจ๋“  ํŒŒ์ผ์„ ์ˆœํšŒ for filename in os.listdir('.'): if pattern.match(filename): # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename) ws = wb.active # ์ฒซ ๋ฒˆ์งธ ํ–‰ (ํ—ค๋”)์€ ์ œ์™ธํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ์šด ์—‘์…€ ํŒŒ์ผ์— ์ถ”๊ฐ€ for row in ws.iter_rows(min_row=2, values_only=True): new_ws.append(row) # ์ƒˆ๋กœ์šด ์—‘์…€ ํŒŒ์ผ ์ €์žฅ new_wb.save('10์›” ํŒฉ์Šค ์ˆ˜์‹  ๋‚ด์—ญ. xlsx') # ์ €์žฅ๋œ ์—‘์…€ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('10์›” ํŒฉ์Šค ์ˆ˜์‹  ๋‚ด์—ญ. xlsx') ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋‹ˆ ๊ฐ ํŒŒ์ผ์— ์ €์žฅ๋˜์–ด ์žˆ๋˜ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์™€์„œ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import Workbook, load_workbook import os import re ์—‘์…€ ๋ฐ์ดํ„ฐ ์ž‘์—…์„ ์œ„ํ•ด openpyxl์„ import ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฒˆ์—๋Š” ํด๋”์— ์žˆ๋Š” ํŒŒ์ผ๋ช…์„ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ์ž‘์—…๊ณผ ์ •๊ทœ์‹์„ ํ™œ์šฉํ•ด ํŒŒ์ผ๋ช…์˜<NAME>์„ ๊ตฌ๋ถ„ํ•ด์„œ ์ถ”์ถœํ•˜๋Š” ์ž‘์—…๋„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด os ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ re ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ํ•จ๊ป˜ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. # ์ƒˆ๋กœ์šด ์—‘์…€ ํŒŒ์ผ ์ƒ์„ฑ new_wb = Workbook() new_ws = new_wb.active new_ws.title = "์ˆ˜์‹  ๋‚ด์—ญ" # ํ—ค๋” ํ–‰ ์ถ”๊ฐ€ new_ws.append(['์ˆ˜์‹  ์‹œ๊ฐ„', '๋ฐœ์‹ ๋ฒˆํ˜ธ', 'ํŽ˜์ด์ง€ ์ˆ˜', '์šฉ๋Ÿ‰']) ์ƒˆ๋กœ ์ €์žฅํ•  ์—‘์…€ ํŒŒ์ผ(new_wb)์„ ์ƒ์„ฑํ•˜์—ฌ ์‹œํŠธ(new_ws) ์ œ๋ชฉ์„ "์ˆ˜์‹  ๋‚ด์—ญ"์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ๋กœ ์ €์žฅ๋  ํŒŒ์ผ์—์„œ๋„ ๊ฐ ์—ด๋ณ„๋กœ ์–ด๋–ค ๋ฐ์ดํ„ฐ์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ์‹œํŠธ ์ฒซ ์ค„์— append()๋กœ ํ—ค๋” ํ–‰์„ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค. # ํŒŒ์ผ ๋ช…์— ๋งž๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ํŒจํ„ด pattern = re.compile(r'\d{4}-\d{2}-\d{2} \d{4}\.xlsx') ์•ž์—์„œ ํ•™์Šตํ–ˆ๋˜ ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ™œ์šฉํ•˜์—ฌ ์ถ”์ถœํ•  ํŒŒ์ผ๋ช… ํŒจํ„ด์„ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ž…๋ ฅํ•œ ํŒจํ„ด์€ '์ˆซ์ž 4์ž๋ฆฌ(YYYY)-์ˆซ์ž 2์ž๋ฆฌ(MM)-์ˆซ์ž 2์ž๋ฆฌ(DD) '๊ณต๋ฐฑ ํ•œ ์นธ' ์ˆซ์ž 4์ž๋ฆฌ(hhmm)'๋กœ ์ž‘์„ฑ๋œ ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. # ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋ชจ๋“  ํŒŒ์ผ์„ ์ˆœํšŒ for filename in os.listdir('.'): if pattern.match(filename): # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook(filename) ws = wb.active os.listdir()๋กœ ํ˜„์žฌ ํŒŒ์ด์ฌ ์‹คํ–‰ ์œ„์น˜('.')์— ์žˆ๋Š” ๋ชจ๋“  ํŒŒ์ผ์„ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ํŒŒ์ผ๋ช…์„ ๊ฐ€์ง€๊ณ  ์˜จ ๋‹ค์Œ ์œ„์—์„œ ์ง€์ •ํ•œ ์ •๊ทœ์‹ ํŒจํ„ด๊ณผ ํŒŒ์ผ๋ช…์˜<NAME>์ด ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•  ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์—‘์…€ ํŒŒ์ผ์˜ ํ™œ์„ฑํ™”๋œ ์‹œํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. # ์ฒซ ๋ฒˆ์งธ ํ–‰ (ํ—ค๋”)์€ ์ œ์™ธํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ์šด ์—‘์…€ ํŒŒ์ผ์— ์ถ”๊ฐ€ for row in ws.iter_rows(min_row=2, values_only=True): new_ws.append(row) # ์ƒˆ๋กœ์šด ์—‘์…€ ํŒŒ์ผ ์ €์žฅ new_wb.save('10์›” ํŒฉ์Šค ์ˆ˜์‹  ๋‚ด์—ญ. xlsx') ๊ฐ€์ง€๊ณ  ์˜จ ์‹œํŠธ์˜ ๋‘ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ํ–‰๊นŒ์ง€ ์ˆœํšŒํ•˜๋ฉด์„œ ํ–‰ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€์„œ append()๋กœ new_ws ์‹œํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ํŒŒ์ผ์— ๋Œ€ํ•œ ์ž‘์—…์ด ๋๋‚˜๋ฉด '10์›” ํŒฉ์Šค ์ˆ˜์‹  ๋‚ด์—ญ. xlsx'์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์•ž์„œ ๋ฐฐ์šด ์ •๊ทœ์‹์„ ํ™œ์šฉํ•ด ํŒŒ์ผ๋ช…์ด ํŠน์ • ๋ฌธ์ž์—ด<NAME>์— ํ•ด๋‹นํ•˜๋Š” ํŒŒ์ผ๋“ค๋งŒ ์ถ”์ถœํ•˜์—ฌ ํ•˜๋‚˜์˜ ์—‘์…€ ํŒŒ์ผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 05. ์›Œ๋“œ(Word) ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ ํšŒ์‚ฌ์˜ ๊ณต์‹ ๋ฌธ์„œ๋‚˜ ๋ณด๊ณ ์„œ, ๊ณ„์•ฝ์„œ ๋“ฑ๊ณผ ๊ฐ™์ด ๋น„์ฆˆ๋‹ˆ์Šค์—์„œ ํŠน์ •ํ•œ ์–‘์‹์œผ๋กœ ์ž‘์„ฑ๋˜๋Š” ์—ฌ๋Ÿฌ ๋ฌธ์„œ๋“ค์€ ์›Œ๋“œ(Word) ํŒŒ์ผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์›Œ๋“œ๋Š” ์‚ฌ์šฉ์ž ์นœํ™”์ ์ธ ์ธํ„ฐํŽ˜์ด์Šค์™€ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๋“ค ๋•๋ถ„์— ๋น„์ฆˆ๋‹ˆ์Šค์—์„œ ์—†์–ด์„œ๋Š” ์•ˆ ๋  ํ•„์ˆ˜ ๋„๊ตฌ๋กœ ์ž๋ฆฌ ์žก์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ํŽธ๋ฆฌํ•จ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ , ๋ฐ˜๋ณต์ ์ด๊ณ  ๋‹จ์ˆœํ•œ ์ž‘์—…์ด ๋งŽ์ด ์š”๊ตฌ๋˜๋ฉด ์‹œ๊ฐ„ ์†Œ๋ชจ๊ฐ€ ๋งŽ์•„์ง€๊ณ  ์ž‘์—… ์ค‘ ์‹ค์ˆ˜ํ•  ๊ฐ€๋Šฅ์„ฑ๋„ ๋†’์•„์ง‘๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์„ ํ™œ์šฉํ•˜์—ฌ ์›Œ๋“œ ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜๋ฉด ์ด๋Ÿฌํ•œ ๋‹จ์ ์„ ๋ณด์™„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„๋ฅผ ๋†’์ด๊ณ  ์ž‘์—… ์‹œ๊ฐ„๋„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ณต์ ์ธ ์›Œ๋“œ ์—…๋ฌด๋Š” ์ž๋™ํ™”ํ•˜์—ฌ ์—…๋ฌด ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐ„์†Œํ™”ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์›Œ๋“œ ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” python-docx์ž…๋‹ˆ๋‹ค. python-docx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋ฌธ์„œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ , ์ˆ˜์ •ํ•˜๊ณ , ํŠน์ • ์–‘์‹์„ ์ ์šฉํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” python-docx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ณ , ์›Œ๋“œ ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ฐ๋Š” ๋ฐฉ๋ฒ•, ํ…์ŠคํŠธ ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๋“ฑ์„ ๋ฐฐ์šธ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ๋” ๋‚˜์•„๊ฐ€, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜์—ฌ ๋ณต์žกํ•œ ์›Œ๋“œ ๋ฌธ์„œ๋„ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 05-01. ์›Œ๋“œ ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์›Œ๋“œ ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ์— ์‚ฌ์šฉํ•  python-docx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋จผ์ € ์„ค์น˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. pip install python-docx ์›Œ๋“œ ๋ฌธ์„œ ์ƒ์„ฑํ•˜๊ธฐ from docx import Document # ์ƒˆ๋กœ์šด ์›Œ๋“œ ๋ฌธ์„œ ์ƒ์„ฑ doc = Document() # ์›Œ๋“œ ๋ฌธ์„œ ์ฝ์–ด์˜ค๊ธฐ doc = Document('ํšŒ์˜๋ก. docx') # ํŒŒ์ผ ์ €์žฅ doc.save('example.docx') ์„ค์น˜ํ•œ docx ๋ชจ๋“ˆ์„ import ํ•œ ๋‹ค์Œ, Document ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ƒˆ๋กœ์šด ์›Œ๋“œ ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด ํŒŒ์ผ์„ ์ฝ์–ด์˜ฌ ๋•Œ๋„ ๋™์ผํ•˜๊ฒŒ Document ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๋ถˆ๋Ÿฌ์˜ฌ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด, save ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ์ƒˆ๋กœ์šด ์ด๋ฆ„์„ ์ง€์ •ํ•˜์—ฌ ์ƒˆ ํŒŒ์ผ๋กœ ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๊ธฐ์กด ํŒŒ์ผ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฎ์–ด์“ธ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ ์ œ๋ชฉ ์ถ”๊ฐ€ํ•˜๊ธฐ from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH # ์›Œ๋“œ ๋ฌธ์„œ ์ƒ์„ฑ doc = Document() # ์ œ๋ชฉ ์ถ”๊ฐ€ title = doc.add_heading('์ œ๋ชฉ์„ ์ด๊ณณ์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค', level=0) title.alignment = WD_ALIGN_PARAGRAPH.CENTER doc.save('example.docx') # ์ €์žฅ๋œ ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('example.docx') ์ƒ์„ฑํ•œ ์›Œ๋“œ ๋ฌธ์„œ์— ์ œ๋ชฉ์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œ๋ชฉ์„ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” add_heading ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ž‘์„ฑํ•  ์ œ๋ชฉ์˜ ๋‚ด์šฉ๊ณผ ์ œ๋ชฉ์˜ ๋ ˆ๋ฒจ(level)์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ๋ฒจ(level) ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์ „๋‹ฌ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด ์ˆซ์ž์— ๋”ฐ๋ผ ์ œ๋ชฉ์˜ ๊ธ€์ž ํฌ๊ธฐ์™€ ํ˜•ํƒœ, ์ฆ‰ ์ œ๋ชฉ์˜ ์Šคํƒ€์ผ์ด ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ ˆ๋ฒจ 0์œผ๋กœ ์ œ๋ชฉ ์Šคํƒ€์ผ์„ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ์›Œ๋“œ์— ์ถ”๊ฐ€ํ•œ ์ œ๋ชฉ์€ ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌ๋œ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์šด๋ฐ ์ •๋ ฌ(title.alignment = WD_ALIGN_PARAGRAPH.CENTER)์„ ์ ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” Level0๋ถ€ํ„ฐ Level10๊นŒ์ง€ ๋ ˆ๋ฒจ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ๊ฐ ์ ์šฉํ•œ ์ œ๋ชฉ ์Šคํƒ€์ผ์ด๋ฉฐ ๋‹จ์ˆœํžˆ ๊ธ€์ž๊ฐ€ ์ž‘์•„์ง€๊ธฐ๋งŒ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒ‰๊น”์ด๋‚˜ ๋ฐ‘์ค„, ๊ธฐ์šธ์ž„ ๋“ฑ ์Šคํƒ€์ผ์ด ๊ฐ๊ฐ ๋‹ค๋ฅด๊ฒŒ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ ๋‹ค์Œ์€ ๋‹จ๋ฝ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH doc = Document() title = doc.add_heading('์ œ๋ชฉ์„ ์ด๊ณณ์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค', level=0) title.alignment = WD_ALIGN_PARAGRAPH.CENTER # ๋‹จ๋ฝ ์ถ”๊ฐ€ p = doc.add_paragraph('์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค') doc.save('example.docx') # ์ €์žฅ๋œ ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('example.docx') ๋จผ์ € add_paragraph ๋ฉ”์„œ๋“œ๋กœ ๋‹จ๋ฝ์„ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ ๋‹จ๋ฝ์— ์ž…๋ ฅํ•  ๋‚ด์šฉ๋„ ํ•จ๊ป˜ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋‹จ๋ฝ์— ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” add_run ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์— p.add_run('์ž…๋ ฅํ•  ๋‚ด์šฉ')์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋ฉด '์ž…๋ ฅํ•  ๋‚ด์šฉ'์ด p ๋‹จ๋ฝ์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๋‹จ๋ฝ ๋ผ์›Œ ๋„ฃ๊ธฐ ๋‹จ๋ฝ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ๋ฌธ์„œ์˜ ์ œ์ผ ์•„๋ž˜์— ์œ„์น˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํŠน์ • ๋‹จ๋ฝ ์•ž์— ๋ผ์›Œ ๋„ฃ์–ด์•ผ ํ•  ๊ฒฝ์šฐ์—๋Š” insert_paragraph_before ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ insert_paragraph_before ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH doc = Document() title = doc.add_heading('์ œ๋ชฉ์„ ์ด๊ณณ์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค', level=0) title.alignment = WD_ALIGN_PARAGRAPH.CENTER p = doc.add_paragraph('์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค') # ๋‘ ๋ฒˆ์งธ, ์„ธ ๋ฒˆ์งธ ๋‹จ๋ฝ์„ ์ถ”๊ฐ€ p2 = doc.add_paragraph('์ด๊ฒƒ์€ ๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค.') p3 = doc.add_paragraph('์ด๊ฒƒ์€ ์„ธ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค.') # ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ ์•ž์— ์ƒˆ ๋‹จ๋ฝ ๋ผ์›Œ ๋„ฃ๊ธฐ p.insert_paragraph_before('์ด๊ฒƒ์€ ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ ์•ž์— ์ƒˆ๋กœ ๋ผ์›Œ ๋„ฃ์€ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค.') doc.save('example.docx') # ์ €์žฅ๋œ ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('example.docx') ์ถ”๊ฐ€๋œ ๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ(p2)๊ณผ ์„ธ ๋ฒˆ์งธ ๋‹จ๋ฝ(p3)์„ ๋ณด๋ฉด ๊ธฐ์กด์— ์ž‘์„ฑ๋˜์–ด ์žˆ๋˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ ์•„๋ž˜์— ๋‹จ๋ฝ๋“ค์ด ์ถ”๊ฐ€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, add_paragraph๋กœ ๋‹จ๋ฝ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฌธ์„œ์˜ ์ œ์ผ ๋์— ์ด์–ด์„œ ๋‹จ๋ฝ์„ ์ถ”๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๋‹ฌ๋ฆฌ insert_paragraph_before ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์„œ์˜ ๋๋ถ€๋ถ„์ด ์•„๋‹Œ ๊ธฐ์กด ๋‹จ๋ฝ์˜ ์•ž๋ถ€๋ถ„์— ์ƒˆ๋กœ์šด ๋‹จ๋ฝ์„ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ œ์ผ ์ฒซ ๋‹จ๋ฝ ์•ž์— ์ƒˆ๋กœ์šด ๋‹จ๋ฝ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์— ์ž‘์„ฑ๋œ ๋ฌธ์„œ์— ๋‹จ๋ฝ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ๋ฝ ์ˆ˜์ •ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์ด๋ฏธ ์ž‘์„ฑ๋˜์–ด ์žˆ๋Š” ๋‹จ๋ฝ์˜ ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH doc = Document() title = doc.add_heading('์ œ๋ชฉ์„ ์ด๊ณณ์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค', level=0) title.alignment = WD_ALIGN_PARAGRAPH.CENTER p = doc.add_paragraph('์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค') p2 = doc.add_paragraph('์ด๊ฒƒ์€ ๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค.') p3 = doc.add_paragraph('์ด๊ฒƒ์€ ์„ธ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค.') p.insert_paragraph_before('์ด๊ฒƒ์€ ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ ์•ž์— ์ƒˆ๋กœ ๋ผ์›Œ ๋„ฃ์€ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค.') # ๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ ํ…์ŠคํŠธ ๋ณ€๊ฒฝ p2.clear() p2.add_run('๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ์˜ ํ…์ŠคํŠธ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.') doc.save('example.docx') # ์ €์žฅ๋œ ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('example.docx') ๊ธฐ์กด์— ์ž‘์„ฑ๋˜์–ด ์žˆ๋˜ ๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ(p2)์˜ ํ…์ŠคํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € p2 ๋‹จ๋ฝ์˜ ๋‚ด์šฉ์„ clear ๋ฉ”์„œ๋“œ๋กœ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„, add_run์œผ๋กœ ๋ณ€๊ฒฝํ•  ํ…์ŠคํŠธ๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ํ‘œ์™€ ๋ฆฌ์ŠคํŠธ, ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ํ•˜๊ธฐ ํ‘œ ์ถ”๊ฐ€ํ•˜๊ธฐ add_table ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›Œ๋“œ ๋ฌธ์„œ์— ํ‘œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ–‰๊ณผ ์—ด์„ ์ง€์ •ํ•˜๊ณ , ์…€์— ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from docx import Document doc = Document() p = doc.add_paragraph('์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค') # 3x3 ํฌ๊ธฐ์˜ ํ‘œ ์ถ”๊ฐ€ table = doc.add_table(rows=3, cols=3) table.style = 'Table Grid' # ํ‘œ์˜ ๊ฐ ์…€์— ๊ฒฉ์ž ์„  ์ถ”๊ฐ€ # ์ฒซ ๋ฒˆ์งธ ํ–‰์˜ ์…€๋“ค์„ hdr_cells ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ๊ฐ ์…€์— ํ—ค๋” ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ hdr_cells = table.rows[0].cells hdr_cells[0].text = 'ํ—ค๋” 1' hdr_cells[1].text = 'ํ—ค๋” 2' hdr_cells[2].text = 'ํ—ค๋” 3' # for ๋ฌธ์œผ๋กœ ๊ฐ ํ–‰์˜ ์…€์— ์ ‘๊ทผํ•˜์—ฌ ๋‘ ๋ฒˆ์งธ์™€ ์„ธ ๋ฒˆ์งธ ํ–‰์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ for i in range(1, 3): row_cells = table.rows[i].cells row_cells[0].text = f'ํ–‰ {i}, ์—ด 1' row_cells[1].text = f'ํ–‰ {i}, ์—ด 2' row_cells[2].text = f'ํ–‰ {i}, ์—ด 3' doc.save('example.docx') # ์ €์žฅ๋œ ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('example.docx') ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ ํ‘œ๋ฅผ ์ถ”๊ฐ€ํ•œ ๋ถ€๋ถ„์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # 3x3 ํฌ๊ธฐ์˜ ํ‘œ ์ถ”๊ฐ€ table = doc.add_table(rows=3, cols=3) table.style = 'Table Grid' # ํ‘œ์˜ ๊ฐ ์…€์— ๊ฒฉ์ž ์„  ์ถ”๊ฐ€ ๋จผ์ € add_table ๋ฉ”์„œ๋“œ๋กœ ํ‘œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ‘œ์˜ ํฌ๊ธฐ๋Š” rows์™€ cols์— ํ–‰๊ณผ ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ๊ฐ ์ž…๋ ฅํ•˜์—ฌ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 3x3 ํฌ๊ธฐ์˜ ํ‘œ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด rows์™€ cols์— ๋™์ผํ•˜๊ฒŒ 3์„ ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ‘œ์˜ ์ „์ฒด ์…€์— ๊ฒฉ์ž์„ ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด table.style = 'Table Grid'๋กœ ํ‘œ ์Šคํƒ€์ผ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฉ์ž ์„  ์™ธ์—๋„ python-docx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๋ฏธ๋ฆฌ ์ •์˜๋œ ์—ฌ๋Ÿฌ ํ‘œ ์Šคํƒ€์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™ธ์˜ ํ‘œ ์Šคํƒ€์ผ์— ๋Œ€ํ•ด ๋” ์•Œ์•„๋ณด๊ธฐ ์›ํ•œ๋‹ค๋ฉด python-docx ๊ณต์‹ ๋ฌธ์„œ์˜ 'Table' ์„น์…˜์„ ์ฐธ๊ณ ํ•˜๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. python-docx ๊ณต์‹ ๋ฌธ์„œ(Table ์„น์…˜) : https://python-docx.readthedocs.io/en/latest/api/table.html # ์ฒซ ๋ฒˆ์งธ ํ–‰์˜ ์…€๋“ค์„ hdr_cells ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ๊ฐ ์…€์— ํ—ค๋” ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ hdr_cells = table.rows[0].cells hdr_cells[0].text = 'ํ—ค๋” 1' hdr_cells[1].text = 'ํ—ค๋” 2' hdr_cells[2].text = 'ํ—ค๋” 3' ํ‘œ์— ํ—ค๋”(์—ด ์ด๋ฆ„)์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด ์ฒซ ๋ฒˆ์งธ ํ–‰์˜ ์…€๋“ค์„ hdr_cells ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์…€์— ์ฐจ๋ก€๋Œ€๋กœ ์ ‘๊ทผํ•˜์—ฌ ํ—ค๋” ๊ฐ’์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. # for ๋ฌธ์œผ๋กœ ๊ฐ ํ–‰์˜ ์…€์— ์ ‘๊ทผํ•˜์—ฌ ๋‘ ๋ฒˆ์งธ์™€ ์„ธ ๋ฒˆ์งธ ํ–‰์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ for i in range(1, 3): row_cells = table.rows[i].cells row_cells[0].text = f'ํ–‰ {i}, ์—ด 1' row_cells[1].text = f'ํ–‰ {i}, ์—ด 2' row_cells[2].text = f'ํ–‰ {i}, ์—ด 3' ํ—ค๋”๋ฅผ ์ œ์™ธํ•œ ํ‘œ์˜ ๋‚˜๋จธ์ง€ ์…€์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ ์…€์— ํ•ด๋‹นํ•˜๋Š” ํ–‰ ๋ฒˆํ˜ธ์™€ ์—ด ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด for ๋ฌธ๊ณผ ํฌ๋ฉ”์ดํŒ…์„ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์›Œ๋“œ ๋ฌธ์„œ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from docx import Document doc = Document() p = doc.add_paragraph('์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค') # ๋ฆฌ์ŠคํŠธ ์ถ”๊ฐ€ doc.add_paragraph( '์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ', style='List Number' ) doc.add_paragraph( '๋‘ ๋ฒˆ์งธ ํ•ญ๋ชฉ', style='List Number' ) doc.save('example.docx') # ์ €์žฅ๋œ ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('example.docx') ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ง„ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด add_paragraph์œผ๋กœ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ style์„ 'List Number'๋กœ ์ง€์ •ํ•˜์—ฌ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ง„ ๋ฆฌ์ŠคํŠธ ์Šคํƒ€์ผ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. from docx import Document from docx.shared import Inches doc = Document() p = doc.add_paragraph('์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค\n') # ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ p.add_run().add_picture('C:\Users\๊ทธ๋ฆผ ํด๋”\๊ทธ๋ฆผ 1.png', width=Inches(1.5)) doc.save('example.docx') # ์ €์žฅ๋œ ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('example.docx') add_picture ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•  ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ „๋‹ฌํ•ด ์ฃผ๊ณ , ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” Inches(1.5)๋กœ ์ด๋ฏธ์ง€์˜ ๋„ˆ๋น„๋ฅผ 1.5 ์ธ์น˜๋กœ ์„ค์ •ํ•˜์˜€์œผ๋ฉฐ, ๋†’์ด ๊ฐ’์€ ๋„ฃ์ง€ ์•Š์Œ์œผ๋กœ์จ ๋†’์ด๋Š” ๋น„์œจ์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ์„ค์ •ํ•˜๋„๋ก ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋„ˆ๋น„์™€ ๋†’์ด ๋‘˜ ๋‹ค ์ธ์ˆ˜๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์›๋ณธ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋กœ ์ด๋ฏธ์ง€๊ฐ€ ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๊ฒฝ์šฐ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋ฌธ์„œ ๋‚ด์—์„œ ์ด๋ฏธ์ง€๊ฐ€ ๋„ˆ๋ฌด ํฌ๊ฑฐ๋‚˜ ๋„ˆ๋ฌด ์ž‘๊ฒŒ ํ‘œ์‹œ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ด ์ ์„ ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” add_run๊ณผ add_picture ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, add_picture๋งŒ ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ๋„ ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. add_run๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ๋•Œ์™€ add_picture๋งŒ ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•  ๋•Œ์˜ ์ฐจ์ด์ ์€ ์ด๋ฏธ์ง€ ์‚ฝ์ž… ์œ„์น˜๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์œ„์˜ ์ฝ”๋“œ์—์„œ add_picture๋งŒ ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์˜ ๋’ค์— ์ด๋ฏธ์ง€๊ฐ€ ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค. add_run๊ณผ add_picture๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋‹จ๋ฝ ์•ˆ์— ํ…์ŠคํŠธ์ฒ˜๋Ÿผ ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ๋กœ ์ƒ์„ฑ๋œ ํŒŒ์ผ์„ ์–ผํ• ๋ณด๋ฉด ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ ๋’ค์— ์ƒ์„ฑ๋œ ๊ฒƒ ๊ฐ™์ง€๋งŒ, ๋‹จ๋ฝ์— ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ ๊ฐœํ–‰๋ฌธ์ž('\n')๋ฅผ ๋„ฃ์–ด์ฃผ์–ด ๋‹จ๋ฝ ์•ˆ์—์„œ ์ค„๋ฐ”๊ฟˆ์„ ์‹คํ–‰ํ•˜๊ณ  ์ด๋ฏธ์ง€๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด, ํ…์ŠคํŠธ ์˜†์— ์ด๋ฏธ์ง€๊ฐ€ ์‚ฝ์ž…๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 05-02. ์›Œ๋“œ ํŒŒ์ผ ์„œ์‹ ์„ค์ •ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์•ž์—์„œ ํ•™์Šตํ•œ ์›Œ๋“œ ํŒŒ์ผ์˜ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์ฝ”๋“œ์— ์„œ์‹์„ ์„ค์ •ํ•˜๋Š” ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์„œ์‹ ์„ค์ •ํ•˜๊ธฐ ์›Œ๋“œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ํ›„ ๋‹จ๋ฝ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ํ…์ŠคํŠธ์— ์„œ์‹๊นŒ์ง€ ์ง€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from docx import Document from docx.shared import Pt, RGBColor #๊ธ€์ž ํฌ๊ธฐ์™€ ๊ธ€์ž์ƒ‰ ์„œ์‹ ์ง€์ •์„ ์œ„ํ•ด docx.shared๋ฅผ import from docx.oxml.ns import qn # ํ•œ๊ธ€ ๊ธ€๊ผด ์„ค์ •์„ ์œ„ํ•ด docx.oxml.ns๋ฅผ import doc = Document() para = doc.add_paragraph() run = para.add_run('ํ…์ŠคํŠธ์— ์„œ์‹์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.') # ๊ธ€์ž ์„œ์‹ ์ง€์ •(๊ตต๊ฒŒ, ๊ธฐ์šธ์ž„, ๋ฐ‘์ค„) run.bold = True run.italic = True run.underline = True # ๊ธ€๊ผด ์„ค์ • run.font.name = '๋ง‘์€ ๊ณ ๋”•' run._r.rPr.rFonts.set(qn('w:eastAsia'), '๋ง‘์€ ๊ณ ๋”•') #๊ธ€์ž ํฌ๊ธฐ ์กฐ์ • run.font.size = Pt(12) # ๊ธ€์ž ์ƒ‰์ƒ ์„ค์ • run.font.color.rgb = RGBColor(0x42, 0x24, 0xE9) doc.save('example.docx') # ์ €์žฅ๋œ ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('example.docx') add_paragraph๋กœ ์ƒ์„ฑํ•œ ๋‹จ๋ฝ์— add_run์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ run ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด run ๊ฐ์ฒด์— ๊ธ€์ž ์„œ์‹์„ ์ถ”๊ฐ€ํ•˜๋Š”๋ฐ ๋จผ์ € ๊ตต๊ฒŒ, ๊ธฐ์šธ์ž„, ๊ทธ๋ฆฌ๊ณ  ๋ฐ‘์ค„์„ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ตต๊ฒŒ, ๊ธฐ์šธ์ž„, ๋ฐ‘์ค„์€ ๊ฐ๊ฐ bold์™€ italic, underline ์†์„ฑ์„ True๋กœ ์„ค์ •ํ•˜์—ฌ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธ€์ž์ฒด๋ฅผ ์ง€์ •ํ•  ๋•Œ๋Š” font.name์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ์‚ฌ์šฉํ•  ๊ธ€๊ผด์˜ ์ด๋ฆ„์„ ์ „๋‹ฌํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์œ ์˜ํ•  ์ ์€ ์˜์–ด ๊ธ€๊ผด์ธ ๊ฒฝ์šฐ์—๋Š” font.name๋งŒ์œผ๋กœ๋„ ๋ฐ”๋กœ ๊ธ€์ž์ฒด๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•œ๊ธ€ ๊ธ€๊ผด์ธ ๊ฒฝ์šฐ์—๋Š” qn('w:eastAsia')๋ฅผ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด qn ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด 'from docx.oxml.ns import qn'๋กœ docx.oxml.ns๋ฅผ ์ถ”๊ฐ€๋กœ import ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ์— ํŠน์ • ๊ธ€๊ผด์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‹œ์Šคํ…œ์— ํ•ด๋‹น ๊ธ€๊ผด์ด ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธ€์ž ํฌ๊ธฐ ์ง€์ •์„ ์œ„ํ•ด์„œ from docx.shared import Pt๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ์˜ ํฌ๊ธฐ๋Š” font.size ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€๊ฒฝํ•˜๋ฉฐ, ๊ธ€์ž ํฌ๊ธฐ์˜ ๋‹จ์œ„๋Š” ์›Œ๋“œ์˜ ๊ธฐ๋ณธ ๋‹จ์œ„์ธ 'ํฌ์ธํŠธ' ๋‹จ์œ„๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธ€์ž์ƒ‰์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด 'from docx.shared import RGBColor'๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ธ€์ž ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” Pt ํด๋ž˜์Šค๋„ ๊ฐ™์ด ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— from docx.shared import Pt, RGBColor๋กœ ํ•œ ๋ฒˆ์— import ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ธ€์ž์ƒ‰์€ font.color.rgb ์†์„ฑ์„ ํ†ตํ•ด ์„ค์ •ํ•˜๋ฉฐ, ์ด๋•Œ RGB(Red, Green, Blue) ๊ฐ’์œผ๋กœ ์ƒ‰์ƒ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. RGB ๊ฐ’์„ ์ง€์ •ํ•  ๋•Œ๋Š” 16์ง„์ˆ˜(์˜ˆ: 0x42) ๋˜๋Š” 10์ง„์ˆ˜(์˜ˆ: 66)๋กœ ๊ฐ ์ƒ‰์ƒ์˜ ๊ฐ•๋„๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 16์ง„์ˆ˜๋กœ RGB ๊ฐ’์„ ์ „๋‹ฌํ•˜์—ฌ RGBColor(0x42, 0x24, 0xE9) ์ง„ํ•œ ํŒŒ๋ž€์ƒ‰์„ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹จ๋ฝ ์„œ์‹ ์„ค์ •ํ•˜๊ธฐ from docx import Document from docx.shared import Pt from docx.enum.text import WD_ALIGN_PARAGRAPH doc = Document() para1 = doc.add_paragraph('์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค.\n ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์˜ ๋‘ ๋ฒˆ์งธ ์ค„์ž…๋‹ˆ๋‹ค.') para2 = doc.add_paragraph('๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค.\n ๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ์˜ ๋‘ ๋ฒˆ์งธ ์ค„์ž…๋‹ˆ๋‹ค.') #๋‹จ๋ฝ์˜ ํ…์ŠคํŠธ๋ฅผ ์ค‘์•™์œผ๋กœ ์ •๋ ฌ para2.alignment = WD_ALIGN_PARAGRAPH.CENTER # ๋‹จ๋ฝ 1์˜ ์ค„ ๊ฐ„๊ฒฉ์„ 12ํฌ์ธํŠธ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. para1.paragraph_format.line_spacing = Pt(12) # ์ค„ ๊ฐ„๊ฒฉ ์„ค์ • # ๋‹จ๋ฝ 2์˜ ์ค„ ๊ฐ„๊ฒฉ์„ 20ํฌ์ธํŠธ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. para2.paragraph_format.line_spacing = Pt(20) # ์ค„ ๊ฐ„๊ฒฉ ์„ค์ • # '๋ณผ๋“œ์ฒด'๋ผ๋Š” ํ…์ŠคํŠธ๋ฅผ ๋‹จ๋ฝ์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ด ํ…์ŠคํŠธ๋ฅผ ๋ณผ๋“œ์ฒด๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. para2.add_run('๋ณผ๋“œ์ฒด').bold = True # ' ๋ฐ ์ดํƒค๋ฆญ ์ฒด'๋ผ๋Š” ํ…์ŠคํŠธ๋ฅผ ๋‹จ๋ฝ์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ด ํ…์ŠคํŠธ๋ฅผ ์ดํƒค๋ฆญ ์ฒด๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. para2.add_run(' ๋ฐ ์ดํƒค๋ฆญ ์ฒด').italic = True doc.save('example.docx') # ์ €์žฅ๋œ ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('example.docx') ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ๋‹จ๋ฝ์˜ ์„œ์‹์„ ์„ค์ •ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋‹จ๋ฝ์˜ ํ…์ŠคํŠธ ์ •๋ ฌ์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด 'from docx.enum.text import WD_ALIGN_PARAGRAPH'๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋‹จ๋ฝ 2์˜ ํ…์ŠคํŠธ๋ฅผ ๊ฐ€์šด๋ฐ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด alignment ์†์„ฑ์„ ์ค‘์•™(WD_ALIGN_PARAGRAPH.CENTER)์œผ๋กœ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์™ธ์— ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” WD_ALIGN_PARAGRAPH.LEFT๋กœ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” WD_ALIGN_PARAGRAPH.RIGHT๋กœ ์„ค์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋‹จ๋ฝ 1๊ณผ ๊ฐ™์ด ์ •๋ ฌ์„ ๋”ฐ๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์™ผ์ชฝ ์ •๋ ฌ๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ paragraph_format.line_spacing์œผ๋กœ ๋‹จ๋ฝ ๋‚ด์˜ ํ…์ŠคํŠธ ๋ผ์ธ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ์กฐ์ ˆํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๊ฒฉ์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ๋‹จ๋ฝ 1์˜ ์ค„ ๊ฐ„๊ฒฉ์„ 12ํฌ์ธํŠธ๋กœ, ๋‹จ๋ฝ 2๋Š” 20ํฌ์ธํŠธ๋กœ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด์„ ๋ณด๋ฉด ๋‹จ๋ฝ 1๊ณผ ๋‹จ๋ฝ 2์—์„œ์˜ ๋ผ์ธ ๊ฐ„๊ฒฉ์ด ๋‹ค๋ฅธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹จ๋ฝ ์ „์ฒด์˜ ํ…์ŠคํŠธ ์„œ์‹์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ์ผ๋ถ€ ํ…์ŠคํŠธ๋งŒ ์„œ์‹์„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 'para2.add_run('๋ณผ๋“œ์ฒด').bold = True'์œผ๋กœ ๋‹จ๋ฝ 2์— '๋ณผ๋“œ์ฒด'๋ผ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ bold ์†์„ฑ์„ True๋กœ ์„ค์ •ํ•˜์—ฌ, ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ์—๋งŒ ๊ธ€์ž ์„œ์‹์„ ๊ตต๊ฒŒ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€ ์ž…๋ ฅํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ๋ฌธ์„œ์— ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. header์™€ footer ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€์— ๊ฐ๊ฐ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from docx import Document doc = Document() para1 = doc.add_paragraph('์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค.') # ๋จธ๋ฆฌ๊ธ€ ์ถ”๊ฐ€ # ๋ฌธ์„œ์˜ ์ฒซ ๋ฒˆ์งธ ์„น์…˜์„ ์ง€์ •ํ•œ ํ›„ header(๋จธ๋ฆฌ๊ธ€)๋ฅผ ๊ฐ€์ ธ์™€ 'header' ๋ณ€์ˆ˜์— ํ• ๋‹น section = doc.sections[0] header = section.header # ๋จธ๋ฆฌ๊ธ€์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์„ ๊ฐ€์ ธ์™€ 'p' ๋ณ€์ˆ˜์— ํ• ๋‹น ํ›„ ํ…์ŠคํŠธ ์„ค์ • p = header.paragraphs[0] p.text = "๋จธ๋ฆฌ๊ธ€์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค." # ๊ผฌ๋ฆฌ ๊ธ€ ์ถ”๊ฐ€ # footer(๊ผฌ๋ฆฌ ๊ธ€)๋ฅผ ๊ฐ€์ ธ์™€ 'footer' ๋ณ€์ˆ˜์— ํ• ๋‹น footer = section.footer # ๊ผฌ๋ฆฌ์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์„ ๊ฐ€์ ธ์™€ 'p2' ๋ณ€์ˆ˜์— ํ• ๋‹น ํ›„ ํ…์ŠคํŠธ ์„ค์ • p2 = footer.paragraphs[0] p2.text = "๊ผฌ๋ฆฌ ๊ธ€์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค." doc.save('example.docx') # ์ €์žฅ๋œ ์›Œ๋“œ ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด('example.docx') (๊ฐ€์šด๋ฐ ๋นˆ ์˜์—ญ ์ƒ๋žต) ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € 'doc.sections[0]๋ฅผ ํ†ตํ•ด ๋ฌธ์„œ์˜ ์ฒซ ๋ฒˆ์งธ ์„น์…˜์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ์›Œ๋“œ ๋ฌธ์„œ๋Š” ์—ฌ๋Ÿฌ ์„น์…˜์œผ๋กœ ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ๊ณ , ๊ฐ ์„น์…˜์€ ๋ณ„๋„์˜ ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„น์…˜์€ ๋ฌธ์„œ์˜ ํ•˜์œ„ ๋‹จ์œ„๋กœ ํŽ˜์ด์ง€์™€๋Š” ๋‹ค๋ฅธ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์„น์…˜ ๋ณ„๋กœ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์„œ์‹์ด๋‚˜ ๋ ˆ์ด์•„์›ƒ, ๋จธ๋ฆฌ๊ธ€/๊ผฌ๋ฆฌ ๊ธ€ ๋“ฑ์„ ๋…๋ฆฝ์ ์œผ๋กœ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋ณดํ†ต ๋Œ€๋ถ€๋ถ„์˜ ์›Œ๋“œ ๋ฌธ์„œ๋Š” ํ•˜๋‚˜์˜ ์„น์…˜์„ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” sections[0]์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ์„น์…˜๋งŒ ์„ ํƒํ•˜์—ฌ ๋ฌธ์„œ ์ „์ฒด์˜ ๋จธ๋ฆฌ๊ธ€/๊ผฌ๋ฆฌ ๊ธ€์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์„น์…˜์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ ์„น์…˜ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ๊ฐ ์„ ํƒํ•˜์—ฌ ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋จธ๋ฆฌ๊ธ€์„ ์„ค์ •ํ•˜๋ ค๋ฉด, ์ฒซ ๋ฒˆ์งธ ์„น์…˜์˜ ๋จธ๋ฆฌ๊ธ€ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์™€์„œ section.header๋กœ ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด์˜ paragraphs[0] ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์— ์ ‘๊ทผํ•œ ๋‹ค์Œ, text ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ธฐ์กด์— ์ž…๋ ฅ๋˜์–ด ์žˆ๋˜ ํ…์ŠคํŠธ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, text ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด์— ์žˆ๋˜ ํ…์ŠคํŠธ๋Š” ์‚ญ์ œ๋˜๊ณ  ์ƒˆ๋กœ์šด ํ…์ŠคํŠธ๊ฐ€ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๊ผฌ๋ฆฌ ๊ธ€์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋จธ๋ฆฌ๊ธ€์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋™์ผํ•˜๋ฉฐ ๋จธ๋ฆฌ๊ธ€ ๊ฐ์ฒด(header) ๋Œ€์‹  ๊ผฌ๋ฆฌ ๊ธ€ ๊ฐ์ฒด(footer)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์„œ์˜ ๊ฐ ํŽ˜์ด์ง€์— ๋ฐ˜๋ณต๋˜๋Š” ํ…์ŠคํŠธ๋‚˜ ์ด๋ฏธ์ง€ ๋“ฑ์„ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํŠนํžˆ ํšŒ์‚ฌ ๋กœ๊ณ , ๋ฌธ์„œ ์ œ๋ชฉ, ์ž‘์„ฑ์ž ์ •๋ณด, ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ ๋“ฑ์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. 05-03. ์›Œ๋“œ ํŒŒ์ผ ์ฝ๊ธฐ ์ด๋ฒˆ์—๋Š” ํŒŒ์ด์ฌ์œผ๋กœ ์›Œ๋“œ ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ๋ฝ ์ฝ๊ธฐ from docx import Document # ์›Œ๋“œ ํŒŒ์ผ ์—ด๊ธฐ doc = Document('example.docx') # ๋‹จ๋ฝ ์ฝ๊ธฐ for para in doc.paragraphs: print(para.text) python-docx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ Document ํด๋ž˜์Šค์— ๋ถˆ๋Ÿฌ์˜ฌ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ํŒŒ์ผ์„ ์—ด์–ด์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  doc.paragraphs๋กœ ๋ชจ๋“  ๋‹จ๋ฝ์— ์ ‘๊ทผํ•˜์—ฌ for ๋ฌธ์œผ๋กœ ๊ฐ ๋‹จ๋ฝ์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•ด์˜ต๋‹ˆ๋‹ค. ๋‹จ๋ฝ๊ณผ ํ…์ŠคํŠธ์˜ ์„œ์‹ ํ™•์ธ ์›Œ๋“œ ๋ฌธ์„œ์˜ ๋‹จ๋ฝ์˜ ์ •๋ ฌ์ด๋‚˜ ๋‹จ๋ฝ ๋‚ด ํ…์ŠคํŠธ์— ์„œ์‹์ด ์„ค์ •๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์„ค์ •๋œ ์„œ์‹์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH # ๋ฌธ์„œ๋ฅผ ์—ด์–ด์„œ ๊ฐ์ฒด๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. doc = Document('example.docx') # ๊ฐ ๋‹จ๋ฝ์˜ ์„œ์‹์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. for index, paragraph in enumerate(doc.paragraphs): print(f"๋‹จ๋ฝ ๋ฒˆํ˜ธ {index}:") print(f" ์ •๋ ฌ: {paragraph.alignment}") # ๊ฐ ๋‹จ๋ฝ ๋‚ด์—์„œ ๊ฐœ๋ณ„ ํ…์ŠคํŠธ ๋Ÿฐ์˜ ์„œ์‹์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. for run in paragraph.runs: print(f" ํ…์ŠคํŠธ ๋‚ด์šฉ: {run.text}") print(f" ๊ตต๊ฒŒ: {run.bold}") print(f" ๊ธฐ์šธ์ž„: {run.italic}") print(f" ๋ฐ‘์ค„: {run.underline}") print(f" ๊ธ€๊ผด: {run.font.name}") print(f" ๊ธ€์ž ํฌ๊ธฐ: {run.font.size}") print(f" ๊ธ€์ž์ƒ‰: {run.font.color.rgb}") print('-' * 20) # ๋‹จ๋ฝ ๊ฐ„ ๊ตฌ๋ถ„์„  # ๊ฒฐ๊ด๊ฐ’ ๋‹จ๋ฝ ๋ฒˆํ˜ธ 0: ์ •๋ ฌ: None ํ…์ŠคํŠธ ๋‚ด์šฉ: ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์˜ ํ…์ŠคํŠธ์— ์„œ์‹์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ตต๊ฒŒ: True ๊ธฐ์šธ์ž„: True ๋ฐ‘์ค„: True ๊ธ€๊ผด: ๋ง‘์€ ๊ณ ๋”• ๊ธ€์ž ํฌ๊ธฐ: 152400 ๊ธ€์ž์ƒ‰: 4224E9 -------------------- ๋‹จ๋ฝ ๋ฒˆํ˜ธ 1: ์ •๋ ฌ: CENTER (1) ํ…์ŠคํŠธ ๋‚ด์šฉ: ๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ์˜ ๋‘ ๋ฒˆ์งธ ์ค„์ž…๋‹ˆ๋‹ค. ๊ตต๊ฒŒ: None ๊ธฐ์šธ์ž„: None ๋ฐ‘์ค„: None ๊ธ€๊ผด: None ๊ธ€์ž ํฌ๊ธฐ: None ๊ธ€์ž์ƒ‰: None -------------------- ๋ชจ๋“  ๋‹จ๋ฝ๊ณผ ๊ฐ ๋‹จ๋ฝ ๋‚ด ๋ชจ๋“  ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ์„œ์‹์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ ๋‹จ๋ฝ๊ณผ ํ…์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋„๋ก for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์„œ์‹์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์„ค์ •๋œ ์„œ์‹์„ ์šฐ๋ฆฌ๋„ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ๊ฐ ์„œ์‹์„ ์ถœ๋ ฅํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์„œ์‹์„ ํ™•์ธํ•  ๋•Œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์„œ์‹์„ ์„ค์ •ํ–ˆ๋˜ ์ฝ”๋“œ๋กœ ํ™•์ธํ•œ๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ๊ฐ ๋‹จ๋ฝ์˜ ์„œ์‹์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. for index, paragraph in enumerate(doc.paragraphs): print(f"๋‹จ๋ฝ ๋ฒˆํ˜ธ {index}:") print(f" ์ •๋ ฌ: {paragraph.alignment}") ๋จผ์ € ๊ฐ ๋‹จ๋ฝ์— ์ ‘๊ทผํ•˜์—ฌ index๋กœ ๋ช‡ ๋ฒˆ์งธ ๋‹จ๋ฝ์ธ์ง€ ๋‹จ๋ฝ์˜ ๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•˜๊ณ , paragraph.alignment๋กœ ๋‹จ๋ฝ์˜ ์ •๋ ฌ ์„œ์‹์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ •๋ ฌ์„ ์„ค์ •ํ•  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 'from docx.enum.text import WD_ALIGN_PARAGRAPH'๋ฅผ ๋จผ์ € ์‹คํ–‰ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์˜ paragraph.alignment์˜ ์ถœ๋ ฅ๊ฐ’์„ ํ™•์ธํ•˜๋ฉด 'None'์ด๋ผ๊ณ  ์ถœ๋ ฅ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์„œ์‹์„ ๋ณ„๋„๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์—๋Š” 'None'์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋‹จ๋ฝ์—์„œ๋Š” ์ •๋ ฌ ์„œ์‹์œผ๋กœ 'CENTER (1)'์ด ์ถœ๋ ฅ๋˜์—ˆ์œผ๋ฉฐ, ์ด๋Š” ์ค‘์•™ ์ •๋ ฌ์ด ์„ค์ •๋˜์–ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ์ผ ๊ฒฝ์šฐ์—๋Š” 'RIGHT (2)'๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. # ๊ฐ ๋‹จ๋ฝ ๋‚ด์—์„œ ๊ฐœ๋ณ„ ํ…์ŠคํŠธ ๋Ÿฐ์˜ ์„œ์‹์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. for run in paragraph.runs: print(f" ํ…์ŠคํŠธ ๋‚ด์šฉ: {run.text}") print(f" ๊ตต๊ฒŒ: {run.bold}") print(f" ๊ธฐ์šธ์ž„: {run.italic}") print(f" ๋ฐ‘์ค„: {run.underline}") print(f" ๊ธ€๊ผด: {run.font.name}") print(f" ๊ธ€์ž ํฌ๊ธฐ: {run.font.size.pt if run.font.size else None}") print(f" ๊ธ€์ž์ƒ‰: {run.font.color.rgb}") print('-' * 20) # ๋‹จ๋ฝ ๊ฐ„ ๊ตฌ๋ถ„์„  python-docx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ์˜ ํ…์ŠคํŠธ ์„œ์‹์„ ๋ถ„์„ํ•  ๋•Œ, ๊ฐ ๋Ÿฐ(run)์˜ ํŠน์ • ์„œ์‹ ์†์„ฑ๋“ค์„ ๊ฐœ๋ณ„์ ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ '๋Ÿฐ'์ด๋ž€, ํ•˜๋‚˜์˜ ๋‹จ๋ฝ ๋‚ด์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์„œ์‹์„ ๊ฐ€์ง„ ํ…์ŠคํŠธ ๊ตฌ๊ฐ„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋‹จ๋ฝ ๋‚ด์˜ ๋Ÿฐ๋“ค์„ ์ˆœํšŒํ•จ์œผ๋กœ์จ ๋Ÿฐ์— ์ ์šฉ๋œ ์„œ์‹์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ธ€์ž ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ์—๋งŒ if ๋ฌธ์ด ๋“ค์–ด๊ฐ€ ์žˆ๋Š”๋ฐ ๊ธ€์ž ํฌ๊ธฐ๋ฅผ ์›๋ž˜ ์›Œ๋“œ ๊ธฐ๋ณธ ๊ธ€์ž ๋‹จ์œ„์ธ ํฌ์ธํŠธ๋กœ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด if ๋ฌธ์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. python-docx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๊ธ€์ž ํฌ๊ธฐ๋Š” 'Pt' ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜๋˜๋ฉฐ, ์ด 'Pt' ๊ฐ์ฒด์˜ 'pt' ์†์„ฑ์„ ํ†ตํ•ด ์šฐ๋ฆฌ๋Š” ๊ธ€์ž ํฌ๊ธฐ๋ฅผ 'ํฌ์ธํŠธ' ๋‹จ์œ„๋กœ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, run.font.size.pt ์ฝ”๋“œ๋Š” ๊ธ€์ž ํฌ๊ธฐ๋ฅผ ํฌ์ธํŠธ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, pt ์†์„ฑ์€ ๊ธ€์ž ํฌ๊ธฐ๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์ง€ ์•Š์•„ run.font.size ๊ฐ’์ด 'None'์ธ ๊ฒฝ์šฐ์—๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ๊ธ€์ž ํฌ๊ธฐ๊ฐ€ 'None'์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ 'pt' ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋„๋ก if ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์„ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ‘œ ์ฝ๊ธฐ ์›Œ๋“œ ๋ฌธ์„œ์—์„œ ํ‘œ๋ฅผ ์ฐพ์•„ ๊ฐ ์…€์˜ ๋‚ด์šฉ์„ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. doc.tables๋ฅผ ํ†ตํ•ด ๋ฌธ์„œ ๋‚ด์˜ ๋ชจ๋“  ํ‘œ์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. from docx import Document doc = Document('example5.docx') # ํ‘œ ์ฝ๊ธฐ for table in doc.tables: for row in table.rows: for cell in row.cells: print(cell.text) # ๊ฒฐ๊ด๊ฐ’ ํ—ค๋” 1 ํ—ค๋” 2 ํ—ค๋” 3 ํ–‰ 1 & ์—ด 1 ํ–‰ 1 & ์—ด 2 ํ–‰ 1 & ์—ด 3 ํ–‰ 2 & ์—ด 1 ํ–‰ 2 & ์—ด 2 ํ–‰ 2 & ์—ด 3 ์ถœ๋ ฅ๋œ ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด 'example.docx' ์›Œ๋“œ ๋ฌธ์„œ์— 3x3 ํฌ๊ธฐ์˜ ํ‘œ๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ์ฒซ ํ–‰์€ 'ํ—ค๋” 1/2/3'์ด, ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ์™€ ์„ธ ๋ฒˆ์งธ ํ–‰์—๋Š” ๊ฐ ์…€์— ํ•ด๋‹นํ•˜๋Š” ํ–‰๊ณผ ์—ด์ด ๊ฐ’์œผ๋กœ ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” table.rows๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œ์˜ ๊ฐ ํ–‰์— ์ ‘๊ทผํ•˜๊ณ , row.cells๋ฅผ ํ†ตํ•ด ๊ฐ ์…€์˜ ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. doc.tables๋Š” ๋ฌธ์„œ ๋‚ด ๋ชจ๋“  ํ‘œ์— ์ ‘๊ทผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ ๋ฌธ์„œ์— ํ‘œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ํฌํ•จ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ ๋ชจ๋“  ํ‘œ์˜ ์…€ ๊ฐ’์ด ์ฐจ๋ก€๋Œ€๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€ ์ฝ๊ธฐ ๋ฌธ์„œ์— ์„ค์ •๋˜์–ด ์žˆ๋Š” ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€์„ ์ฝ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€์„ ์„ค์ •ํ•  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„น์…˜ ๋ณ„๋กœ ์„ค์ •๋˜์–ด ์žˆ๋Š” ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€์„ ์ฝ์–ด์˜ค๊ธฐ ์œ„ํ•ด doc.sections๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ์˜ ๊ฐ ์„น์…˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. from docx import Document doc = Document('example.docx') # ๋จธ๋ฆฌ๊ธ€ ์ฝ๊ธฐ for section in doc.sections: header = section.header for paragraph in header.paragraphs: print(paragraph.text) # ๊ผฌ๋ฆฌ ๊ธ€ ์ฝ๊ธฐ for section in doc.sections: footer = section.footer for paragraph in footer.paragraphs: print(paragraph.text) section.header์™€ section.footer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์„น์…˜์˜ ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€์— ์ ‘๊ทผํ•˜์˜€์œผ๋ฉฐ, ๋จธ๋ฆฌ๊ธ€๊ณผ ๊ผฌ๋ฆฌ ๊ธ€์˜ ๋‹จ๋ฝ๋ณ„๋กœ ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์ถ”์ถœํ•˜๊ธฐ ์›Œ๋“œ ๋ฌธ์„œ์— ์ด๋ฏธ์ง€๊ฐ€ ์‚ฝ์ž…๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ, python-docx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ง์ ‘์ ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— docx2txt ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. docx2txt ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด pip๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋จผ์ € ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install docx2txt ์„ค์น˜ํ•œ docx2txt ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ถ”์ถœํ•˜๋Š” ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. import docx2txt # docx2txt๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ์ถ”์ถœ docx2txt.process('example.docx', './์ถ”์ถœํ•œ ์ด๋ฏธ์ง€ ํด๋”/') # ์ด๋ฏธ์ง€๊ฐ€ ์ €์žฅ๋œ ํด๋”('์ถ”์ถœํ•œ ์ด๋ฏธ์ง€ ํด๋”') ํ™”๋ฉด docx2txt.process์— ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ถ”์ถœํ•  ์›Œ๋“œ ํŒŒ์ผ์„ ์ „๋‹ฌํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ถ”์ถœํ•œ ํ›„ ์ €์žฅํ•  ํด๋”์˜ ๊ฒฝ๋กœ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํŒŒ์ด์ฌ์ด ์‹คํ–‰๋˜๋Š” ํ˜„์žฌ ๊ฒฝ๋กœ์— ๋ฐ”๋กœ ์ €์žฅํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ '.' ๊ฐ’์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. docx2txt๋Š” ๊ฒฝ๋กœ๋งŒ์„ ์ „๋‹ฌํ•˜๋ฉฐ, ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•  ๋•Œ ํŒŒ์ผ๋ช…์„ ๋ณ„๋„๋กœ ์ง€์ •ํ•˜๋Š” ์˜ต์…˜์€ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 'image1', 'image2'์™€ ๊ฐ™์ด image+์ˆซ์ž<NAME>์œผ๋กœ ํŒŒ์ผ ๋‚ด ์ด๋ฏธ์ง€์˜ ์ˆœ์„œ๋Œ€๋กœ ํŒŒ์ผ๋ช…์ด ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ ํ›„ ํด๋” ๊ฒฝ๋กœ๋ฅผ ์บก์ฒ˜ํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๋ฉด, 'thumbnail.jpeg'์™€ 'image1.png'๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ 'thumbnail.jpeg'๋กœ ์ถ”์ถœ๋œ ์ด๋ฏธ์ง€๋Š” ์›Œ๋“œ์˜ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ ์ด๋ฏธ์ง€๋กœ ์‚ฌ์šฉ๋˜๋Š” ์„ฌ๋„ค์ผ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. ๋ฌธ์„œ ํƒ์ƒ‰๊ธฐ๋‚˜ ํŒŒ์ผ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ณด์ด๋Š” ์ž‘์€ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ ์ด๋ฏธ์ง€๋„ ์ด ๋™์ผํ•œ ์„ฌ๋„ค์ผ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌธ์„œ์— ์ด๋Ÿฌํ•œ ์„ฌ๋„ค์ผ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ์ด ์ด๋ฏธ์ง€๋„ 'thumbnail.jpeg'๋กœ ํ•จ๊ป˜ ์ถ”์ถœ๋ฉ๋‹ˆ๋‹ค. docx2txt๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ…์ŠคํŠธ์™€ ์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋‘ ์ถ”์ถœํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ ์‹คํ–‰ ์‹œ ํ…์ŠคํŠธ๋Š” ์ฝ˜์†”์—๋งŒ ๋ฐ˜ํ™˜๋˜๊ณ  ์‹ค์ œ ๋””์Šคํฌ์—๋Š” ์ €์žฅ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€๋งŒ ํŒŒ์ผ๋กœ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ž‘์„ฑ๋œ ์›Œ๋“œ ๋ฌธ์„œ๋ฅผ ํŒŒ์ด์ฌ์œผ๋กœ ์ฝ์–ด์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์œผ๋กœ ์ƒ์„ฑํ•œ ์›Œ๋“œ ํŒŒ์ผ์ด ์•„๋‹Œ ์ˆ˜๋™์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ  ์„œ์‹์„ ์„ค์ •ํ•œ ์›Œ๋“œ ๋ฌธ์„œ๋„ ์œ„์—์„œ ์†Œ๊ฐœํ•œ ๋ฐฉ๋ฒ•๋“ค๋กœ ๋Œ€๋ถ€๋ถ„ ์ฝ์–ด์™€์„œ ์ž‘์—…์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ•œ๊ณ„ ๋•Œ๋ฌธ์— ์ผ๋ถ€ ๋ณต์žกํ•œ ์„œ์‹์ด๋‚˜ ๋งคํฌ๋กœ ๋“ฑ์€ python-docx๋กœ ์™„๋ฒฝํ•˜๊ฒŒ ๋‹ค๋ฃจ์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŒŒ์ด์ฌ์—์„œ ์›Œ๋“œ ํŒŒ์ผ์„ ์ฝ์–ด์™€์„œ ํ•ธ๋“ค๋งํ•  ๋•Œ ํŒŒ์ผ์ด ์ œ๋Œ€๋กœ ๋ถˆ๋Ÿฌ์™€์ง€๋Š”์ง€๋Š” ์ถ”๊ฐ€๋กœ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. 05-04. ์‹ค์ „! ๊ณ ๊ฐ ๊ตฌ๋งค๋ฆฌ์ŠคํŠธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ณ ๊ฐ๋ณ„ ์†ก์žฅ ํŒŒ์ผ ์ž๋™ ์ƒ์„ฑํ•˜๊ธฐ ์•ž์—์„œ ํ•™์Šตํ•œ ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์›Œ๋“œ ํ…œํ”Œ๋ฆฟ์„ ๊ฐ€์ง€๊ณ  ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์›Œ๋“œ ํ…œํ”Œ๋ฆฟ์— ๋ฐ์ดํ„ฐ ๋„ฃ๊ธฐ ํšŒ์‚ฌ์—์„œ ์ œํ’ˆ์„ ๊ตฌ๋งคํ•œ ๊ณ ๊ฐ์—๊ฒŒ ๋ฐœ์†กํ•˜๋Š” ์†ก์žฅ ํ…œํ”Œ๋ฆฟ์— ๊ณ ๊ฐ์˜ ๋ฐ์ดํ„ฐ์™€ ๊ตฌ๋งคํ•œ ์ œํ’ˆ, ๋‹จ๊ฐ€, ํ•ฉ๊ณ„ ๊ธˆ์•ก ๋“ฑ์˜ ์ •๋ณด๋ฅผ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ์†ก์žฅ ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. '์†ก์žฅ. docx' ๋จผ์ € ํŒŒ์ผ์˜ ์–ด๋–ค ์œ„์น˜์— ์–ด๋–ค ์ •๋ณด๋ฅผ ๋„ฃ์–ด์•ผ ํ•˜๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด ํŒŒ์ด์ฌ์œผ๋กœ ์†ก์žฅ ํ…œํ”Œ๋ฆฟ์„ ์ฝ์–ด์™€์„œ ๋‹จ๋ฝ๊ณผ ํ…์ŠคํŠธ์˜ ์ธ๋ฑ์Šค, ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š” ํ…์ŠคํŠธ์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from docx import Document # ์›Œ๋“œ ํŒŒ์ผ ์—ด๊ธฐ doc = Document('์†ก์žฅ. docx') # ๋‹จ๋ฝ ์ฝ๊ธฐ for index, paragraph in enumerate(doc.paragraphs): print(f"๋‹จ๋ฝ ๋ฒˆํ˜ธ {index}:") # ๋‹จ๋ฝ ์ธ๋ฑ์Šค ์ถœ๋ ฅ # ๊ฐ ๋‹จ๋ฝ ๋‚ด์—์„œ ๊ฐœ๋ณ„ ํ…์ŠคํŠธ ๋Ÿฐ์˜ ์„œ์‹์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. for index, run in enumerate(paragraph.runs): print(f" ํ…์ŠคํŠธ ๋ฒˆํ˜ธ: {index}") print(f" ํ…์ŠคํŠธ ๋‚ด์šฉ: {run.text}") # ํ‘œ ํ™•์ธํ•˜๊ธฐ # ๋ฌธ์„œ์— ์žˆ๋Š” ๋ชจ๋“  ํ‘œ๋ฅผ ์ˆœํšŒ for table_index, table in enumerate(doc.tables): print(f"ํ‘œ {table_index}:") #ํ‘œ์˜ ์ธ๋ฑ์Šค ์ถœ๋ ฅ # ํ‘œ์˜ ๊ฐ ํ–‰์„ ์ˆœํšŒ for row_index, row in enumerate(table.rows): print(f" ํ–‰ {row_index}:") # ํ–‰ ๋ฒˆํ˜ธ ์ถœ๋ ฅ # ํ–‰์˜ ๊ฐ ์…€์„ ์ˆœํšŒ for cell_index, cell in enumerate(row.cells): print(f" ์…€ {cell_index}: {cell.text}") # ์…€์˜ ์ธ๋ฑ์Šค ์ถœ๋ ฅ print('-' * 20) # ๋‹จ๋ฝ ๊ฐ„ ๊ตฌ๋ถ„์„  # ๊ฒฐ๊ด๊ฐ’ (์ถœ๋ ฅ๊ฐ’์ด ๋„ˆ๋ฌด ๊ธธ์–ด ์ค‘๊ฐ„ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.) ๋‹จ๋ฝ ๋ฒˆํ˜ธ 0: ํ…์ŠคํŠธ ๋ฒˆํ˜ธ: 0 ํ…์ŠคํŠธ ๋‚ด์šฉ: ์†ก์žฅ ๋‹จ๋ฝ ๋ฒˆํ˜ธ 1: ํ…์ŠคํŠธ ๋ฒˆํ˜ธ: 0 ํ…์ŠคํŠธ ๋‚ด์šฉ: ํšŒ์‚ฌ๋ช… ํ…์ŠคํŠธ ๋ฒˆํ˜ธ: 1 ํ…์ŠคํŠธ ๋‚ด์šฉ: : ํ…์ŠคํŠธ ๋ฒˆํ˜ธ: 2 ํ…์ŠคํŠธ ๋‚ด์šฉ: ๋ฃฐ๋ฃจ ํ…์ŠคํŠธ ๋ฒˆํ˜ธ: 3 ํ…์ŠคํŠธ ๋‚ด์šฉ: ํ…์ŠคํŠธ ๋ฒˆํ˜ธ: 4 ํ…์ŠคํŠธ ๋‚ด์šฉ: ์†”๋ฃจ์…˜์ฆˆ ํ…์ŠคํŠธ ๋ฒˆํ˜ธ: 5 ํ…์ŠคํŠธ ๋‚ด์šฉ: (์ค‘๋žต) ํ‘œ 0: ํ–‰ 0: ์…€ 0: ์ œํ’ˆ ์…€ 1: ์ˆ˜๋Ÿ‰ ์…€ 2: ๋‹จ๊ฐ€ ์…€ 3: ํ•ฉ๊ณ„ ํ–‰ 1: ์…€ 0: ์…€ 1: ์…€ 2: ์…€ 3: (์ค‘๋žต) -------------------- python-docx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋ฌธ์„œ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ, ๋‹จ๋ฝ์ด๋‚˜ ๊ฐ ๋‹จ๋ฝ์˜ ํ…์ŠคํŠธ ๋Ÿฐ์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์œ„์น˜ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค๋ฅผ ์ถ”์ถœํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ ๋‚ด ๋ชจ๋“  ๋‹จ๋ฝ๊ณผ ํ…์ŠคํŠธ ๋Ÿฐ, ํ‘œ์— ๋ชจ๋‘ ์ ‘๊ทผํ•˜์—ฌ ์ธ๋ฑ์Šค์™€ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜์˜€์œผ๋ฉฐ, ์œ„์˜ ๊ฒฐ๊ด๊ฐ’์—๋Š” ์ƒ๋žตํ•˜์˜€์ง€๋งŒ, ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋ฐ˜ํ™˜๋œ ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š” ๊ณ ๊ฐ ์ •๋ณด ๋ถ€๋ถ„์€ ์„ธ ๋ฒˆ์งธ ๋‹จ๋ฝ(์ธ๋ฑ์Šค: 2)์— ํ•ด๋‹นํ•˜๋ฉฐ ๊ณ ๊ฐ๋ช…, ๊ณ ๊ฐ ์ฃผ์†Œ, ๊ณ ๊ฐ ์—ฐ๋ฝ์ฒ˜์˜ ํ…์ŠคํŠธ ๋Ÿฐ์€ ๊ฐ๊ฐ 2, 5, 8์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‘œ์˜ ๊ฒฝ์šฐ์—๋Š” ํ–‰ ๋ฒˆํ˜ธ๋‚˜ ์…€ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ต์  ์ง๊ด€์ ์œผ๋กœ ์•Œ๊ธฐ ์‰ฝ์ง€๋งŒ ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ๋„ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด๋ ‡๊ฒŒ ํŒŒ์•…ํ•œ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ํ…œํ”Œ๋ฆฟ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from docx import Document # ์›Œ๋“œ ๋ฌธ์„œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ doc = Document('์†ก์žฅ. docx') # ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š” ๋‹จ๋ฝ ์ง€์ • target_paragraph = doc.paragraphs[2] # ๊ฐ ๋ผ๋ฒจ ๋’ค์— ํ…์ŠคํŠธ ์ถ”๊ฐ€ target_paragraph.runs[2].add_text('์ดํ˜ธ์—ฐ') target_paragraph.runs[5].add_text('์„œ์šธ์‹œ ๋งˆํฌ๊ตฌ ์„œ๊ต๋™') target_paragraph.runs[8].add_text('070-333-4444') # ๊ตฌ๋งค ๋‚ด์—ญ์„ ์ž…๋ ฅํ•  ํ‘œ ์ง€์ • table = doc.tables[0] # ํ‘œ์— ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ 'data' ๋ณ€์ˆ˜์— ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ data = [ ["์ œํ’ˆ 1", "2", "12,000","24,000"] ] # ํ—ค๋”๋ฅผ ์ œ์™ธํ•œ ํ‘œ์˜ ๋‘ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๊ฐ ์…€์— data๋ฅผ ์ž…๋ ฅ for i, row_data in enumerate(data): for j, cell_data in enumerate(row_data): table.cell(i+1, j).text = cell_data # ํ‘œ์˜ ๋งˆ์ง€๋ง‰ ํ–‰์˜ ๋งˆ์ง€๋ง‰ ์…€์— '์ดํ•ฉ๊ณ„' ๊ฐ’์„ ์ž…๋ ฅ table.rows[-1].cells[-1].text = '240,000' # ๋ฌธ์„œ ์ €์žฅ doc.save('์ดํ˜ธ์—ฐ_์†ก์žฅ. docx') '์ดํ˜ธ์—ฐ_์†ก์žฅ. docx' ์‚ฌ์šฉํ•  ์›Œ๋“œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์„ ์ฝ์–ด์˜จ ๋‹ค์Œ, ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•  ๋ถ€๋ถ„์ธ ์„ธ ๋ฒˆ์งธ ๋‹จ๋ฝ(doc.paragraphs[2])์„ ์ฐพ์•„์„œ target_paragraph ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ํ…์ŠคํŠธ ๋Ÿฐ์˜ ์ธ๋ฑ์Šค(target_paragraph.runs[2])๋ฅผ ์ „๋‹ฌํ•˜์—ฌ add_text๋กœ ๋‚ด์šฉ('์ดํ˜ธ์—ฐ')์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ณ ๊ฐ ์ฃผ์†Œ์™€ ๊ณ ๊ฐ ์—ฐ๋ฝ์ฒ˜๋„ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ํ‘œ์—๋„ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ์œ„ํ•ด doc.tables[0]์œผ๋กœ ๋ฌธ์„œ์˜ ์ฒซ ๋ฒˆ์งธ ํ‘œ๋ฅผ ์ฐพ์•„ table ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ํ‘œ์˜ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ–‰๋ณ„๋กœ ์ž…๋ ฅํ•˜๋ฉฐ, ๊ฐ ์…€์˜ ์œ„์น˜๊ฐ’(ํ–‰๊ณผ ์—ด์˜ ์ธ๋ฑ์Šค)์ด ๊ทœ์น™์ ์ด๊ธฐ ๋•Œ๋ฌธ์— for ๋ฌธ์œผ๋กœ ์…€์˜ ์œ„์น˜๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋ฉฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ์œ„ํ•ด ๋จผ์ € ํ–‰๋ณ„๋กœ ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ด์ค‘ for ๋ฌธ์œผ๋กœ ๋ชจ๋“  ํ–‰์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ํ–‰๋ณ„๋กœ ๋ชจ๋“  ์…€์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ค‘ ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์–ด ์…€๋งˆ๋‹ค ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์†ก์žฅ ํ…œํ”Œ๋ฆฟ์˜ ํ‘œ์—์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ํ–‰์—๋Š” ๊ตฌ๋งคํ•œ ์ „์ฒด ์ œํ’ˆ์— ๋Œ€ํ•œ ์ดํ•ฉ๊ณ„ ๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜๋„๋ก ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ํ–‰์˜ ๋งˆ์ง€๋ง‰ ์…€์ด๋ผ 'table.rows[-1].cells[-1]'๋กœ ์œ„์น˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ text๋กœ '240,000'์˜ ๊ฐ’์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ํ…œํ”Œ๋ฆฟ์„ ํ™œ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ํŒŒ์ผ ํ•œ ๋ฒˆ์— ์ž‘์„ฑํ•˜๊ธฐ ์œ„์™€ ๊ฐ™์ด ํŒŒ์ด์ฌ์œผ๋กœ ํ…œํ”Œ๋ฆฟ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์ œํ’ˆ์„ ๊ตฌ๋งคํ•œ ์—ฌ๋Ÿฌ ๊ณ ๊ฐ์—๊ฒŒ ์ „๋‹ฌํ•  ์†ก์žฅ๋“ค์„ ํ•œ ๋ฒˆ์— ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. '๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx' ์œ„์™€ ๊ฐ™์ด ๊ตฌ๋งคํ•œ ๊ณ ๊ฐ์˜ ์ •๋ณด์™€ ๊ตฌ๋งค ๋‚ด์—ญ์ด ์—‘์…€ ํŒŒ์ผ๋กœ ์ •๋ฆฌ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์œผ๋กœ ์—‘์…€ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ๊ณ ๊ฐ์—๊ฒŒ ๋ณด๋‚ผ ๊ฐ๊ฐ์˜ ์†ก์žฅ ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from openpyxl import load_workbook from docx import Document from docx.enum.text import WD_PARAGRAPH_ALIGNMENT # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook('๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx', data_only = True) ws = wb.active # ์—‘์…€ ํŒŒ์ผ์˜ ๊ฐ ํ–‰์— ๋Œ€ํ•ด ์›Œ๋“œ ํŒŒ์ผ ์ƒ์„ฑ for row_idx, row in enumerate(ws.iter_rows(values_only=True)): # ํ—ค๋” ํ–‰์€ ๊ฑด๋„ˆ๋›ฐ๊ธฐ if row_idx == 0: continue # ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ํ–‰์„ ๊ฑด๋„ˆ๋›ฐ๊ธฐ if not any(row): continue # ์›Œ๋“œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ doc = Document('์†ก์žฅ. docx') # ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š” ๋‹จ๋ฝ ์ง€์ • target_paragraph = doc.paragraphs[2] # ๊ฐ ๋ผ๋ฒจ ๋’ค์— ํ…์ŠคํŠธ ์ถ”๊ฐ€ target_paragraph.runs[2].add_text(str(row[0])) # ๊ณ ๊ฐ๋ช… target_paragraph.runs[5].add_text(str(row[1])) # ๊ณ ๊ฐ ์ฃผ์†Œ target_paragraph.runs[8].add_text(str(row[2])) # ๊ณ ๊ฐ ์—ฐ๋ฝ์ฒ˜ # ๊ตฌ๋งค ๋‚ด์—ญ์„ ์ž…๋ ฅํ•  ํ‘œ ์ง€์ • table = doc.tables[0] # ํ‘œ์— ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ ์„ค์ • data = [ [str(row[3]), int(row[4]), f"{int(row[5]) if row[5] is not None else 0:,}", f"{int(row[6]) if row[6] is not None else 0:,}"] ] # ํ—ค๋”๋ฅผ ์ œ์™ธํ•œ ํ‘œ์˜ ๋‘ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๊ฐ ์…€์— data๋ฅผ ์ž…๋ ฅ for i, row_data in enumerate(data): for j, cell_data in enumerate(row_data): cell = table.cell(i+1, j) cell.text = cell_data for paragraph in cell.paragraphs: paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # ํ‘œ์˜ ๋งˆ์ง€๋ง‰ ํ–‰์˜ ๋งˆ์ง€๋ง‰ ์…€์— '์ดํ•ฉ๊ณ„' ๊ฐ’์„ ์ž…๋ ฅ table.rows[-1].cells[-1].text = f"{int(row[6]) if row[6] is not None else 0:,}" for paragraph in table.rows[-1].cells[-1].paragraphs: paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # ๋ฌธ์„œ ์ €์žฅ (๊ณ ๊ฐ๋ช…์„ ํŒŒ์ผ๋ช…์— ์‚ฌ์šฉ) doc.save(f'./์†ก์žฅ ํด๋”/{row[0]}_์†ก์žฅ. docx') # ์†ก์žฅ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ํด๋”('์†ก์žฅ ํด๋”') ํ™”๋ฉด '์ด์ƒํ›ˆ_์†ก์žฅ. docx'ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋œ ๋ชจ์Šต ์ด์ „ ์ฑ•ํ„ฐ์—์„œ ํ•™์Šตํ•œ openpyxl๋กœ ์—‘์…€ ํŒŒ์ผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋ณธ ์ฑ•ํ„ฐ์˜ python-docx๋กœ ์›Œ๋“œ ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ๋ฅผ ์ข…ํ•ฉํ•˜์—ฌ ์—‘์…€ ํŒŒ์ผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์›Œ๋“œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ถ€๋ถ„๋งˆ๋‹ค ๋‚˜๋ˆ ์„œ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์—‘์…€ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook('๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx', data_only = True) ws = wb.active # ์—‘์…€ ํŒŒ์ผ์˜ ๊ฐ ํ–‰์— ๋Œ€ํ•ด ์›Œ๋“œ ํŒŒ์ผ ์ƒ์„ฑ for row_idx, row in enumerate(ws.iter_rows(values_only=True)): # ํ—ค๋” ํ–‰์€ ๊ฑด๋„ˆ๋›ฐ๊ธฐ if row_idx == 0: continue # ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ํ–‰์„ ๊ฑด๋„ˆ๋›ฐ๊ธฐ if not any(row): continue ๋จผ์ € load_workbook์œผ๋กœ '๊ตฌ๋งค ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ. xlsx' ํŒŒ์ผ์„ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ์—‘์…€ ํŒŒ์ผ์— ์ˆ˜์‹์ด ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— 'data_only = True'๋ฅผ ๋„ฃ์–ด ์ˆ˜์‹์˜ ๊ฒฐ๊ด๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์˜ต๋‹ˆ๋‹ค. ์—‘์…€ ํŒŒ์ผ์˜ ๊ฐ ํ–‰์— ๋Œ€ํ•ด ์›Œ๋“œ ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ for ๋ฌธ์œผ๋กœ ์—‘์…€ ๋ฐ์ดํ„ฐ๋ฅผ ํ–‰๋งˆ๋‹ค ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ํ–‰์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ๋‹ค์Œ ์›Œ๋“œ ์ž‘์—…์„ ์‹คํ–‰ํ•˜์—ฌ ์›Œ๋“œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ธฐ๊นŒ์ง€๊ฐ€ ์ž‘์—…์˜ ํ•œ ์‚ฌ์ดํด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด ์ฝ”๋“œ๋Š” for ๋ฌธ์•ˆ์œผ๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ์—‘์…€ ๋ฐ์ดํ„ฐ์˜ ์ฒซ ํ–‰์€ ํ—ค๋”(์—ด์ด๋ฆ„) ํ–‰์ด๊ธฐ ๋•Œ๋ฌธ์— ํ–‰ ์ธ๋ฑ์Šค๊ฐ€ 0์ธ ๊ฒฝ์šฐ๋Š” continue๋กœ ๊ฑด๋„ˆ๋›ฐ๋„๋ก if ๋ฌธ์œผ๋กœ ์กฐ๊ฑด์„ ๊ฑธ์–ด์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ–‰์— ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋„ ํ•ด๋‹น ํ–‰์„ ๊ฑด๋„ˆ๋›ฐ๋„๋ก any() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ if ๋ฌธ์„ ํ•œ ๋ฒˆ ๋” ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. any() ํ•จ์ˆ˜๋Š” ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด(์—ฌ๊ธฐ์„œ๋Š” ํ–‰)์— ์žˆ๋Š” ์–ด๋–ค ๊ฐ’์ด๋ผ๋„ ์ฐธ์ด๋ฉด 'True'๋ฅผ, ๋ชจ๋“  ๊ฐ’์ด ๊ฑฐ์ง“์„ ๊ฒฝ์šฐ 'False'๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ํ˜„์žฌ ํ–‰์˜ ๋ชจ๋“  ์…€ ์ด ๋น„์–ด์žˆ๋‹ค๋ฉด ํ•ด๋‹น ํ–‰์„ ๊ฑด๋„ˆ๋›ฐ๋ผ๋Š” ์˜๋ฏธ๋กœ if not any(row):๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋นˆ ํ–‰์„ ๊ฑด๋„ˆ๋›ฐ๋ผ๋Š” ์กฐ๊ฑด์„ ๋„ฃ์ง€ ์•Š์œผ๋ฉด 'None'์„ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ํ•œ 'None_์†ก์žฅ. docx' ํŒŒ์ผ์ด ์ƒ์„ฑ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. # ์›Œ๋“œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ doc = Document('์†ก์žฅ. docx') # ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š” ๋‹จ๋ฝ ์ง€์ • target_paragraph = doc.paragraphs[2] # ๊ฐ ๋ผ๋ฒจ ๋’ค์— ํ…์ŠคํŠธ ์ถ”๊ฐ€ target_paragraph.runs[2].add_text(str(row[0])) # ๊ณ ๊ฐ๋ช… target_paragraph.runs[5].add_text(str(row[1])) # ๊ณ ๊ฐ ์ฃผ์†Œ target_paragraph.runs[8].add_text(str(row[2])) # ๊ณ ๊ฐ ์—ฐ๋ฝ์ฒ˜ ์—‘์…€ ํŒŒ์ผ์„ ์—ด์–ด์„œ ํ–‰๋ณ„๋กœ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜์˜€์œผ๋‹ˆ ์ด์ œ ๋‹ค์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์›Œ๋“œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ ํ•˜๋‚˜์˜ ์†ก์žฅ ํŒŒ์ผ์„ ๋งŒ๋“ค ๋•Œ์™€ ๋™์ผํ•˜๊ฒŒ ๋‹จ๋ฝ์„ ์ง€์ •ํ•˜๊ณ  ํ…์ŠคํŠธ ๋Ÿฐ์— ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ์‹์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๋ฐ˜๋ณต ์ž‘์—…์œผ๋กœ ๊ฐ ํ–‰์˜ ์ฒซ ๋ฒˆ์งธ ์—ด(๊ณ ๊ฐ๋ช…), ๋‘ ๋ฒˆ์งธ ์—ด(๊ณ ๊ฐ ์ฃผ์†Œ), ๊ทธ๋ฆฌ๊ณ  ์„ธ ๋ฒˆ์งธ ์—ด(๊ณ ๊ฐ ์—ฐ๋ฝ์ฒ˜)์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. row๋กœ ์…€ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์˜ฌ ๋•Œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌธ์ž์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— str๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. # ๊ตฌ๋งค ๋‚ด์—ญ์„ ์ž…๋ ฅํ•  ํ‘œ ์ง€์ • table = doc.tables[0] # ํ‘œ์— ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ ์„ค์ • data = [ [str(row[3]), int(row[4]), f"{int(row[5]) if row[5] is not None else 0:,}", f"{int(row[6]) if row[6] is not None else 0:,}"] ] ๋‹ค์Œ์€ ์›Œ๋“œ ํ…œํ”Œ๋ฆฟ์˜ ํ‘œ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ ๋‚ด ํ‘œ๊ฐ€ ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ ๋ฒˆ์งธ ํ‘œ(doc.tables[0])๋ฅผ ์ฐพ์•„์˜ต๋‹ˆ๋‹ค. ํ‘œ์— ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋Š” ๋ฌธ์ž์—ด์ด๊ณ  ๋‘ ๋ฒˆ์งธ ์›์†Œ๋Š” ์ˆ˜๋Ÿ‰์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์œผ๋กœ ํ•œ์ž๋ฆฌ ์ˆซ์ž ๋ฐ์ดํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ์ž‘์—…์ด ํ•„์š”ํ•˜์ง€ ์•Š์•„ ๊ฐ๊ฐ 'str(row[3])', 'int(row[4])'๋กœ ๋น„๊ต์  ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ, ๋„ค ๋ฒˆ์งธ ์›์†Œ๋Š” ๊ธˆ์•ก์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์žํ˜• ๋ฐ์ดํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ์ผ์„ ๋” ์™„์„ฑ๋„ ์žˆ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ˆซ์ž ๋ฐ์ดํ„ฐ์— ์ฒœ ๋‹จ์œ„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ(,)๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ฒœ ๋‹จ์œ„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋ฅผ ๋„ฃ๊ธฐ ์œ„ํ•ด์„œ๋Š” '02. ํŒŒ์ด์ฌ ๊ธฐ์ดˆ'์˜ '02-09. ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ'์—์„œ ํ•™์Šตํ•œ 'format'ํ•จ์ˆ˜๋‚˜ 'f-string'์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž๋ฅผ ์ฒœ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” f"{number:,}"๋กœ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹จ, ๋ณ€ํ™˜ํ•  ๋•Œ if ๋ฌธ์œผ๋กœ ์กฐ๊ฑด์„ ๋„ฃ์–ด์ฃผ์—ˆ๋Š”๋ฐ int() ํ•จ์ˆ˜์— 'None'๊ฐ’์ด ์ „๋‹ฌ๋  ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 'None' ๊ฐ’์ธ์ง€ ํ™•์ธํ•œ ํ›„ 'None'์ผ ๊ฒฝ์šฐ์—๋Š” 0์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์…€์˜ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋„๋ก ์กฐ๊ฑด์„ ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. # ํ—ค๋”๋ฅผ ์ œ์™ธํ•œ ํ‘œ์˜ ๋‘ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๊ฐ ์…€์— data๋ฅผ ์ž…๋ ฅ for i, row_data in enumerate(data): for j, cell_data in enumerate(row_data): cell = table.cell(i+1, j) cell.text = cell_data for paragraph in cell.paragraphs: paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # ํ‘œ์˜ ๋งˆ์ง€๋ง‰ ํ–‰์˜ ๋งˆ์ง€๋ง‰ ์…€์— '์ดํ•ฉ๊ณ„' ๊ฐ’์„ ์ž…๋ ฅ table.rows[-1].cells[-1].text = f"{int(row[6]) if row[6] is not None else 0:,}" for paragraph in table.rows[-1].cells[-1].paragraphs: paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER ์œ„์—์„œ ์„ค์ •ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์ œ ํ‘œ์˜ ๊ฐ ์…€์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ์ด์ค‘ for ๋ฌธ์œผ๋กœ ํ‘œ์˜ ์ „์ฒด ํ–‰์„ ์ˆœํšŒํ•˜๋ฉด์„œ ํ˜„์žฌ ํ–‰์˜ ๊ฐ๊ฐ์˜ ์—ด์— ์ฐจ๋ก€๋Œ€๋กœ ์ ‘๊ทผํ•˜์—ฌ ๋ชจ๋“  ์…€์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ๋‹จ, ํ‘œ์˜ ํ—ค๋”๋ฅผ ์ œ์™ธํ•˜๊ธฐ ์œ„ํ•ด ์ž…๋ ฅํ•  ์…€์˜ ์œ„์น˜(table.cell)๋ฅผ ํ•œ ํ–‰ ์•„๋ž˜(i+1)๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ‘œ์— ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์šด๋ฐ ์ •๋ ฌ์‹œํ‚ค๊ธฐ ์œ„ํ•ด paragraph.alignment๋กœ ์ •๋ ฌ ์„œ์‹์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ‘œ์˜ ๋งˆ์ง€๋ง‰ ํ–‰์˜ ๋งˆ์ง€๋ง‰ ์…€์— ์ž…๋ ฅํ•˜๋Š” '์ดํ•ฉ๊ณ„' ๋ฐ์ดํ„ฐ๋„ ๋™์ผํ•˜๊ฒŒ ์ฒœ ๋‹จ์œ„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ์™€ ๊ฐ€์šด๋ฐ ์ •๋ ฌ ์„œ์‹์„ ์ ์šฉํ•ด ์ค๋‹ˆ๋‹ค. # ๋ฌธ์„œ ์ €์žฅ (๊ณ ๊ฐ๋ช…์„ ํŒŒ์ผ๋ช…์— ์‚ฌ์šฉ) doc.save(f'./์†ก์žฅ ํด๋”/{row[0]}_์†ก์žฅ. docx') ๋ฌธ์„œ๋ฅผ ์ €์žฅํ•  ๋•Œ๋„ ํŒŒ์ผ๋ช…์„ '๊ณ ๊ฐ๋ช…_์†ก์žฅ. docx'<NAME>์œผ๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด row[0]์œผ๋กœ ์—‘์…€ ๋ฐ์ดํ„ฐ์˜ ์ฒซ ์—ด์— ์žˆ๋Š” ๊ณ ๊ฐ๋ช…์„ ๊ฐ€์ง€๊ณ  ์™€์„œ f-string์œผ๋กœ ํŒŒ์ผ๋ช…์— ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ํŒŒ์ด์ฌ์œผ๋กœ ์›Œ๋“œ ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ํ•™์Šตํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ํ•™์Šตํ•œ ์˜ˆ์ œ์ฒ˜๋Ÿผ ํŒŒ์ด์ฌ์œผ๋กœ ์›Œ๋“œ์˜ ํ…œํ”Œ๋ฆฟ์„ ํ™œ์šฉํ•ด ์ž‘์—…์„ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋” ๋‚˜์•„๊ฐ€ ์—‘์…€ ํŒŒ์ผ๊ณผ๋„ ์—ฐ๋™์‹œ์ผœ์„œ ๋” ๋ณต์žกํ•œ ์ž‘์—…์„ ์ž๋™ํ™”์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 06. ํŒŒ์›Œํฌ์ธํŠธ(Powerpoint) ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ ๋น„์ฆˆ๋‹ˆ์Šค์—์„œ ๋ฐœํ‘œ๋Š” ๋นผ๋†“์„ ์ˆ˜ ์—†๋Š” ์ฃผ๋œ ์—…๋ฌด ์ค‘ ํ•˜๋‚˜๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ ์— ๋Œ€ํ•œ ๋ฐœํ‘œ, ํ–ฅํ›„ ๊ณ„ํš๊ณผ ๋น„์ „์— ๋Œ€ํ•œ ๋ฐœํ‘œ, ์‹œ์žฅ ๋™ํ–ฅ ๋ถ„์„์ด๋‚˜ ๊ฐœ๋ฐœ ์‚ฌํ•ญ์— ๋Œ€ํ•œ ๋ฐœํ‘œ ๋“ฑ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ๋ชจ๋“  ์—…๋ฌด ๋ถ„์•ผ์—์„œ ๋ฐœํ‘œ๊ฐ€ ์ด๋ฃจ์–ด์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•ด๋„ ๊ณผ์–ธ์ด ์•„๋‹ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ๋ฐœํ‘œ๋ฅผ ์œ„ํ•œ ์ž๋ฃŒ๋กœ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋„๊ตฌ๊ฐ€ ๋ฐ”๋กœ ํŒŒ์›Œํฌ์ธํŠธ์ž…๋‹ˆ๋‹ค. ํŒŒ์›Œํฌ์ธํŠธ๋Š” ์‹œ๊ฐ์ ์œผ๋กœ ํ’๋ถ€ํ•˜๊ณ , ๊ตฌ์กฐํ™”๋œ ์ •๋ณด ์ „๋‹ฌ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๋›ฐ์–ด๋‚œ ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ˆ˜๋™์œผ๋กœ ์ผ์ผ์ด ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆฌ๊ณ  ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์ด ์ˆ˜๋ฐ˜๋˜๊ณค ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์„ ์ด์šฉํ•˜๋ฉด, ์ด๋Ÿฌํ•œ ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์„ ์ตœ์†Œํ™”ํ•˜๋ฉฐ, ๋™์‹œ์— ์ •๊ตํ•˜๊ณ  ํ‘œ์ค€ํ™”๋œ ํŒŒ์›Œํฌ์ธํŠธ ๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ๋น ๋ฅด๊ฒŒ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ์—‘์…€ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์†Œ์Šค์—์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€ ํŒŒ์›Œํฌ์ธํŠธ์— ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํšŒ์‚ฌ๋‚˜ ์กฐ์ง์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•ด ์ž๋™์œผ๋กœ ๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์‹œ๊ฐ„ ์ ˆ์•ฝ์€ ๋ฌผ๋ก ์ด๊ณ  ์ผ๊ด€๋œ ํ€„๋ฆฌํ‹ฐ์˜ ์ž๋ฃŒ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์œผ๋กœ ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ๊ฐ€ ์ฃผ๋กœ ์‚ฌ์šฉํ•  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” python-pptx์ž…๋‹ˆ๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ์„ ์ƒ์„ฑ, ์ˆ˜์ •, ๊ทธ๋ฆฌ๊ณ  ์ฝ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค. ์ด ์ฑ•ํ„ฐ์—์„œ python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ์ด๋ฏธ์ง€, ์ฐจํŠธ, ํ…์ŠคํŠธ ๋ฐ ํ…Œ์ด๋ธ”์„ ํฌํ•จํ•˜๋Š” ๋‹ค์–‘ํ•œ ์š”์†Œ๋ฅผ ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 06-01. ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ํ…์ŠคํŠธ ์ถ”๊ฐ€ python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ์ ์ธ ํŒŒ์›Œํฌ์ธํŠธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € pip๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. # python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ pip install python-pptx ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ๊ณผ ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ ์ด์ œ ์ƒˆ๋กœ์šด ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŒŒ์ผ์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ธฐ๋ณธ ์Šฌ๋ผ์ด๋“œ๋„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from pptx import Presentation # Presentation ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ์ดˆ๊ธฐํ™” prs = Presentation() # ์ฒซ ๋ฒˆ์งธ ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ ์„ ํƒ slide_layout = prs.slide_layouts[0] # ์„ ํƒํ•œ ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ์Šฌ๋ผ์ด๋“œ ์ถ”๊ฐ€ slide = prs.slides.add_slide(slide_layout) # ํŒŒ์ผ ์ €์žฅ prs.save('presentation.pptx') ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ ์ƒ์„ฑ์„ ์œ„ํ•ด python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Presentation ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. Presentation ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ƒˆ๋กœ์šด ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  slide_layouts ๋ฆฌ์ŠคํŠธ์—์„œ ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ œ๋ชฉ ์Šฌ๋ผ์ด๋“œ์ธ ์ฒซ ๋ฒˆ์งธ ๋ ˆ์ด์•„์›ƒ(slide_layouts[0])์„ ์„ ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํŒŒ์›Œํฌ์ธํŠธ์—์„œ ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์€ ์Šฌ๋ผ์ด๋“œ์˜ ๊ธฐ๋ณธ ๋””์ž์ธ๊ณผ ํฌ๋งท์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ œ๋ชฉ, ๋ณธ๋ฌธ, ๊ทธ๋ฆผ, ์ฐจํŠธ, ํ…Œ์ด๋ธ” ๋“ฑ ๋‹ค์–‘ํ•œ ๊ฐœ์ฒดํ‹€(placeholder)์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ ˆ์ด์•„์›ƒ์— ๋”ฐ๋ผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฐœ์ฒดํ‹€์˜ ์ข…๋ฅ˜์™€ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. slide_layouts ๋ฆฌ์ŠคํŠธ๋Š” python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ ˆ์ด์•„์›ƒ ๋ฆฌ์ŠคํŠธ๋กœ, ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ • ๋ ˆ์ด์•„์›ƒ์„ ์„ ํƒํ•˜์—ฌ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์— ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” add_slide ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์„ ํƒํ•œ ๋ ˆ์ด์•„์›ƒ์„ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ํ˜„์žฌ์˜ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ํŒŒ์ผ๋กœ ์ €์žฅํ•  ๋•Œ๋Š” save ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ํŒŒ์ผ์„ ์ €์žฅํ•  ๊ฒฝ๋กœ๋ฅผ ํฌํ•จํ•˜์—ฌ ์ €์žฅํ•  ํŒŒ์ผ๋ช…์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ์ด์•„์›ƒ ๋ฆฌ์ŠคํŠธ ํ™•์ธํ•˜๊ธฐ ์•ž์„œ ๋งํ•œ ๊ฒƒ์ฒ˜๋Ÿผ slide_layouts ๋ฆฌ์ŠคํŠธ์—๋Š” ํ”„๋ ˆ์  ํ…Œ์ด์…˜์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์—ฌ๋Ÿฌ ๋ ˆ์ด์•„์›ƒ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ• ์ง€ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด ํ•ด๋‹น ๋ ˆ์ด์•„์›ƒ์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ฒ ์ง€๋งŒ, ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ ˆ์ด์•„์›ƒ๋“ค์ด ๋ฌด์—‡์ธ์ง€ ๋ชจ๋ฅด๊ฑฐ๋‚˜ ๋ ˆ์ด์•„์›ƒ์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ๋ชจ๋ฅด๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ ˆ์ด์•„์›ƒ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ ˆ์ด์•„์›ƒ๋“ค์˜ ์ธ๋ฑ์Šค์™€ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from pptx import Presentation prs = Presentation() # ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์˜ ์ธ๋ฑ์Šค์™€ ์ด๋ฆ„ ์ถœ๋ ฅ for i, layout in enumerate(prs.slide_layouts): print(f"Layout {i}: {layout.name}") # ๊ฒฐ๊ด๊ฐ’ Layout 0: Title Slide Layout 1: Title and Content Layout 2: Section Header Layout 3: Two Content Layout 4: Comparison Layout 5: Title Only Layout 6: Blank Layout 7: Content with Caption Layout 8: Picture with Caption Layout 9: Title and Vertical Text Layout 10: Vertical Title and Text ํ…์ŠคํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์ƒ์„ฑํ•œ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์Šฌ๋ผ์ด๋“œ์— ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ์—์„œ๋Š” ์ œ๋ชฉ๊ณผ ๋ถ€์ œ๋ชฉ์ด ์žˆ๋Š” ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ œ๋ชฉ๊ณผ ๋ถ€์ œ๋ชฉ์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. from pptx import Presentation prs = Presentation() slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout) # ์Šฌ๋ผ์ด๋“œ์— ์ œ๋ชฉ๊ณผ ๋ถ€์ œ๋ชฉ ์ถ”๊ฐ€ title = slide.shapes.title subtitle = slide.placeholders[1] # ์ œ๋ชฉ๊ณผ ๋ถ€์ œ๋ชฉ ํ…์ŠคํŠธ ๋ฐ•์Šค์— ๊ฐ๊ฐ ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ title.text = "Hello, World!" subtitle.text = "ํŒŒ์ด์ฌ ํŒŒ์›Œํฌ์ธํŠธ ์ž๋™ํ™”" prs.save('presentation.pptx') 'presentation.pptx'ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด shapes.title๊ณผ placeholders[1] ์†์„ฑ์œผ๋กœ ์ œ๋ชฉ๊ณผ ๋ถ€์ œ๋ชฉ ํ…์ŠคํŠธ ๋ฐ•์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ฐ ํ…์ŠคํŠธ ๋ฐ•์Šค์— ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ…์ŠคํŠธ ๋ฐ•์Šค ๊ฐœ์ฒดํ‹€(placeholder)์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” placeholder์˜ ์ธ๋ฑ์Šค๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ์ฒดํ‹€(placeholder) ํ™•์ธํ•˜๊ธฐ ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์— ์ •์˜๋œ ๋‹ค์–‘ํ•œ ์š”์†Œ๋“ค์€ ๊ฐœ์ฒดํ‹€(placeholder)์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ placeholder๋Š” ์Šฌ๋ผ์ด๋“œ์— ํŠน์ • ์œ„์น˜์™€ ์—ญํ• (์˜ˆ: ์ œ๋ชฉ, ๋ถ€์ œ๋ชฉ, ๋ณธ๋ฌธ, ๊ทธ๋ฆผ, ์ฐจํŠธ, ํ…Œ์ด๋ธ” ๋“ฑ)์ด ํ• ๋‹น๋œ ํ…์ŠคํŠธ ๋ฐ•์Šค ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. placeholder์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ฃผ๋กœ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ์ด ๋ฒˆํ˜ธ๋Š” ๋ ˆ์ด์•„์›ƒ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ๊ฐ ๋ ˆ์ด์•„์›ƒ์— ํฌํ•จ๋œ placeholder์˜ ์ข…๋ฅ˜์™€ ์ธ๋ฑ์Šค๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด slide.placeholders ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ placeholder์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  placeholder์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋Š” ๋ ˆ์ด์•„์›ƒ ๋””์ž์ธ์— ๋”ฐ๋ผ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ํŠน์ • ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์˜ placeholder ์ธ๋ฑ์Šค์™€ ๊ทธ๋“ค์˜ ์—ญํ• ์„ ์•Œ์•„๋‚ด๋ ค๋ฉด, ์Šฌ๋ผ์ด๋“œ์˜ ๋ชจ๋“  placeholder๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ placeholder์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์™€ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from pptx import Presentation prs = Presentation() slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout) # ์„ ํƒ๋œ ์Šฌ๋ผ์ด๋“œ์˜ ๋ชจ๋“  placeholder์˜ ์ธ๋ฑ์Šค์™€ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ for i, placeholder in enumerate(slide.placeholders): print(f"Placeholder {i}: {placeholder.name}") # ๊ฒฐ๊ด๊ฐ’ Placeholder 0: Title 1 Placeholder 1: Subtitle 2 ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ฐ placeholder์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์™€ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜์—ฌ, ์–ด๋–ค ์ธ๋ฑ์Šค๊ฐ€ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” placeholder์— ํ…์ŠคํŠธ ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์˜ฌ๋ฐ”๋ฅธ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•  ๋ถ€๋ถ„์€ ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ œ๋ชฉ์„ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” placeholders[0]๊ฐ€ ์•„๋‹Œ shapes.title๋กœ ํ…์ŠคํŠธ ๋ฐ•์Šค๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•œ ๋ ˆ์ด์•„์›ƒ(slide_layouts[0]), ์ œ๋ชฉ ์Šฌ๋ผ์ด๋“œ)์—๋Š” ์ œ๋ชฉ ํ…์ŠคํŠธ ๋ฐ•์Šค์™€ ๋ถ€์ œ๋ชฉ ํ…์ŠคํŠธ ๋ฐ•์Šค๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ณ  ๊ฐ๊ฐ์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋Š” placeholders[0]๊ณผ placeholders[1]์ž…๋‹ˆ๋‹ค. ์ œ๋ชฉ ํ…์ŠคํŠธ ๋ฐ•์Šค๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ shapes.title์ด ์•„๋‹Œ placeholders[0]์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ๋™์ผํ•˜๊ฒŒ ์ œ๋ชฉ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ shapes.title์„ ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์— ๋”ฐ๋ผ์„œ slide.placeholders[0]์ด ํ•ญ์ƒ ์ œ๋ชฉ์„ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. slide.shapes.title ์†์„ฑ์€ ์Šฌ๋ผ์ด๋“œ์˜ ์ œ๋ชฉ placeholder์— ๋ช…์‹œ์ ์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋ฉฐ, ์ด๋Š” ์ฝ”๋“œ๋ฅผ ๋” ๋ช…ํ™•ํ•˜๊ณ  ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์€ ์Šฌ๋ผ์ด๋“œ์— ์ œ๋ชฉ์ด ํ•ญ์ƒ ์žˆ์„ ๊ฒƒ์ด๋ผ๋Š” ์•”์‹œ์  ๊ฐ€์ •์„ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ์ œ๋ชฉ ํ…์ŠคํŠธ ๋ฐ•์Šค๊ฐ€ ๋ ˆ์ด์•„์›ƒ์—์„œ ์ฒซ ๋ฒˆ์งธ placeholder๋กœ ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋„ ์ œ๋ชฉ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, slide.shapes.title๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์ด ์–ด๋–ป๊ฒŒ ์„ค๊ณ„๋˜์—ˆ๋Š”์ง€์— ์ƒ๊ด€์—†์ด ํ•ญ์ƒ ์ œ๋ชฉ placeholder์— ์•ˆ์ „ํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, slide.placeholders[0]์„ ์‚ฌ์šฉํ•˜๋ฉด, ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์— ๋”ฐ๋ผ ์˜ˆ๊ธฐ์น˜ ์•Š์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์ œ๋ชฉ์— ์ ‘๊ทผํ•  ๋•Œ๋Š” slide.shapes.title๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ •ํ™•ํ•˜๊ฒŒ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ฐœ์ฒดํ‹€(placeholder) ์ถ”๊ฐ€ํ•˜๊ธฐ ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฐœ์ฒดํ‹€ ์™ธ์— ์ถ”๊ฐ€๋กœ ๊ฐœ์ฒดํ‹€์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ, ๊ธฐ์กด placeholder๋ฅผ ๋ณต์ œํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด shape๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ[1]์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด์•„์›ƒ[1]์˜ ์ฝ˜์†”์—์„œ placeholder ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๋ฉด 'Placeholder 0: Title 1'๊ณผ 'Placeholder 1: Content Placeholder 2'๋งŒ ์ถœ๋ ฅ๋˜์–ด ์ด 2๊ฐœ์˜ ๊ฐœ์ฒดํ‹€์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ธฐ์กด 2๊ฐœ์˜ ๊ฐœ์ฒดํ‹€ ์™ธ์— ๋˜ ํ•˜๋‚˜์˜ ๊ฐœ์ฒดํ‹€์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from pptx import Presentation from pptx.util import Inches prs = Presentation() slide_layout = prs.slide_layouts[1] slide = prs.slides.add_slide(slide_layout) title = slide.shapes.title subtitle = slide.placeholders[1] title.text = "์ œ๋ชฉ" subtitle.text = "๋ถ€์ œ๋ชฉ" # ์ƒˆ๋กœ์šด ํ…์ŠคํŠธ ๋ฐ•์Šค๋ฅผ ์Šฌ๋ผ์ด๋“œ์— ์ถ”๊ฐ€ left = Inches(1) # ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ๊ฐ€๋กœ ์œ„์น˜(์Šฌ๋ผ์ด๋“œ ์™ผ์ชฝ ๋์œผ๋กœ๋ถ€ํ„ฐ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ) top = Inches(2) # ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ์„ธ๋กœ ์œ„์น˜(์Šฌ๋ผ์ด๋“œ ์œ„์ชฝ ๋์œผ๋กœ๋ถ€ํ„ฐ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ) width = Inches(5) # ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ๊ฐ€๋กœ ๊ธธ์ด height = Inches(1.5) #ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ์„ธ๋กœ ๊ธธ์ด textbox = slide.shapes.add_textbox(left, top, width, height) # ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ํ…์ŠคํŠธ ๋ฐ•์Šค์— ํ…์ŠคํŠธ ์ถ”๊ฐ€ frame = textbox.text_frame p = frame.add_paragraph() p.text = "์ƒˆ๋กœ์šด ํ…์ŠคํŠธ ๋ฐ•์Šค์— ์ถ”๊ฐ€๋œ ํ…์ŠคํŠธ" # ๊ฒฐ๊ณผ๋ฅผ ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ๋กœ ์ €์žฅ prs.save('powerpoint_ex.pptx') 'powerpoint_ex.pptx'ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด ๊ธฐ์กด placeholder์™€ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ placeholder๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๊ธฐ์กด placeholder์— ๊ฐ๊ฐ '์ œ๋ชฉ'๊ณผ '๋ถ€์ œ๋ชฉ'์ด๋ผ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ƒˆ๋กœ์šด ํ…์ŠคํŠธ ๋ฐ•์Šค๋ฅผ ์Šฌ๋ผ์ด๋“œ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. placeholder๋ฅผ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•  ๋•Œ ๊ทธ ์œ„์น˜๋‚˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ์ธ์ž๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. left : ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ๊ฐ€๋กœ ์œ„์น˜(์Šฌ๋ผ์ด๋“œ ์™ผ์ชฝ ๋์œผ๋กœ๋ถ€ํ„ฐ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ) top : ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ์„ธ๋กœ ์œ„์น˜(์Šฌ๋ผ์ด๋“œ ์œ„์ชฝ ๋์œผ๋กœ๋ถ€ํ„ฐ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ) width : ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ๊ฐ€๋กœ ๊ธธ์ด height :ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ์„ธ๋กœ ๊ธธ์ด ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ๋‹จ์œ„๋กœ ์ธ์น˜(Inches)๋ฅผ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, ์ธ์น˜ ์™ธ์—๋„ ์„ผํ‹ฐ๋ฏธํ„ฐ(Cm), ํฌ์ธํŠธ(Pt), ํ”ฝ์…€(Px) ๋“ฑ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๋‹จ์œ„๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์„ ํƒํ•˜์—ฌ ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•œ ์ˆ˜์น˜๋ฅผ ๊ฐ€์ง€๊ณ  add_textbox๋กœ ํ…์ŠคํŠธ ๋ฐ•์Šค๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. add_textbox ๋ฉ”์„œ๋“œ๋Š” ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ํฌ๊ธฐ๋‚˜ ์œ„์น˜๊ฐ’์„ ํ•„์ˆ˜๋กœ ์š”๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜๋ผ๋„ ๊ฐ’์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ ์˜ํ•  ์ ์€ ์œ„์˜ ์ฝ”๋“œ(add_textbox(left, top, width, height))์—์„œ์ฒ˜๋Ÿผ ํฌ๊ธฐ์™€ ์œ„์น˜๊ฐ’์„ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ ๋‹ค์Œ์— ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜๋ฅผ add_text์— ์ „๋‹ฌํ•  ๊ฒฝ์šฐ, 'left', 'top', 'width', 'height' ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ์œ„์น˜์™€ ํฌ๊ธฐ๊ฐ€ ์˜๋„์™€ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ์ธ์ž์˜ ์ˆœ์„œ์— ๋”ฐ๋ผ ํ•จ์ˆ˜์— ์œ„์น˜์™€ ํฌ๊ธฐ ๊ฐ’์ด ์ „๋‹ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ˆœ์„œ์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ํ‚ค์›Œ๋“œ ์ธ์ž(์ด๋ฆ„์„ ์ง€์ •ํ•˜์—ฌ ์ธ์ž๋ฅผ ์ „๋‹ฌ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. # ํ‚ค์›Œ๋“œ ์ธ์ž๋ฅผ ์‚ฌ์šฉ textbox = slide.shapes.add_textbox(top=top, left=left, width=width, height=height) ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ถ”๊ฐ€ํ•  ํ…์ŠคํŠธ ๋ฐ•์Šค์˜ ํฌ๊ธฐ์™€ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๊ณ , ๋ ˆ์ด์•„์›ƒ์— ์ƒˆ๋กœ์šด ํ…์ŠคํŠธ ๋ฐ•์Šค๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์›Œํฌ์ธํŠธ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from pptx import Presentation prs = Presentation() slide_layout = prs.slide_layouts[1] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "ํŒŒ์ด์ฌ์˜ ์žฅ์ " tf = slide.placeholders[1].text_frame # ํ…์ŠคํŠธ ๋ฐ•์Šค์— ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ ์ถ”๊ฐ€ tf.text = "์‰ฌ์šด ์‚ฌ์šฉ๋ฒ•" # ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ํ•˜์œ„ ํ•ญ๋ชฉ ์ถ”๊ฐ€ p = tf.add_paragraph() p.text = "์ง๊ด€์ ์ธ ๋ฌธ๋ฒ•" p.level = 1 # ๋ฆฌ์ŠคํŠธ์˜ ๋‘ ๋ฒˆ์งธ ํ•ญ๋ชฉ ์ถ”๊ฐ€ p = tf.add_paragraph() p.text = "๋†’์€ ์ƒ์‚ฐ์„ฑ" p.level = 0 # ๋‘ ๋ฒˆ์งธ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ํ•˜์œ„ ํ•ญ๋ชฉ ์ถ”๊ฐ€ p = tf.add_paragraph() p.text = "๋น ๋ฅธ ๊ฐœ๋ฐœ ์†๋„" p.level = 1 # ๋ฆฌ์ŠคํŠธ์˜ ์„ธ ๋ฒˆ์งธ ํ•ญ๋ชฉ ์ถ”๊ฐ€ p = tf.add_paragraph() p.text = "๋‹ค์–‘ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํ”„๋ ˆ์ž„์›Œํฌ" p.level = 0 # ์„ธ ๋ฒˆ์งธ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ํ•˜์œ„ ํ•ญ๋ชฉ ์ถ”๊ฐ€ p = tf.add_paragraph() p.text = "๋จธ์‹  ๋Ÿฌ๋‹, ์›น ๊ฐœ๋ฐœ ๋“ฑ์— ์œ ์šฉ" p.level = 1 # ํŒŒ์ผ ์ €์žฅ prs.save('presentation_with_list.pptx') 'presentation_with_list.pptx' ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด ํŒŒ์ด์ฌ์˜ ์žฅ์ ์— ๋Œ€ํ•ด ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ํŒŒ์›Œํฌ์ธํŠธ ์Šฌ๋ผ์ด๋“œ์— ์ž‘์„ฑํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์ถ”๊ฐ€ํ•œ ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์— ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ์œ„ํ•ด placeholders[1]์— ์ ‘๊ทผํ•˜์—ฌ text_frame ๊ฐ์ฒด๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ํ…์ŠคํŠธ ๋ฐ•์Šค์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ์ค„์— ๋ฐ”๋กœ ์ž…๋ ฅํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— 'text_frame.text'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” tf.text = "์‰ฌ์šด ์‚ฌ์šฉ๋ฒ•"๋กœ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๊ทธ๋‹ค์Œ์— ์ด์–ด์ง€๋Š” ์ฝ”๋“œ๋“ค์„ ๋ด๋„ ์•Œ ์ˆ˜ ์žˆ๋“ฏ, ์ฒซ ์ค„ ์ดํ›„์— ์ถ”๊ฐ€๋˜๋Š” ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ๋“ค์€ text_frame.text์ด ์•„๋‹Œ add_paragraph() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ๋„ add_paragraph()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๊ฐ€ํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, ๊ทธ๋ ‡๊ฒŒ ํ•  ๊ฒฝ์šฐ์—๋Š” ์ฒซ ์ค„์ด ๋น„์–ด์žˆ๋Š” ์ฑ„๋กœ ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์— ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•œ ๋‹ค์Œ, ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ํ•˜์œ„ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์€ ์ƒ์œ„ ํ•ญ๋ชฉ๊ณผ ํ•˜์œ„ ํ•ญ๋ชฉ ๋ชจ๋‘ ๋™์ผํ•˜๊ฒŒ add_paragraph()์„ ์‚ฌ์šฉํ•˜๋ฉฐ, level ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜์—ฌ ํ•ด๋‹น ๋‚ด์šฉ์ด ํ•˜์œ„ ํ•ญ๋ชฉ์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. level ์†์„ฑ์˜ ๊ฐ’์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ ๊ฐ’์ด ํด์ˆ˜๋ก ๋” ๊นŠ์€ ํ•˜์œ„ ๋ ˆ๋ฒจ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. 0์€ ์ตœ์ƒ์œ„ ํ•ญ๋ชฉ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ๋ฝ์— ํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ 0์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ level์ด 0์ผ ๊ฒฝ์šฐ์—๋Š” level ์†์„ฑ ์ง€์ •์„ ์ƒ๋žตํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ("์‰ฌ์šด ์‚ฌ์šฉ๋ฒ•" - level 0)์— ๋Œ€ํ•œ ํ•˜์œ„ ํ•ญ๋ชฉ์ด๊ธฐ ๋•Œ๋ฌธ์— "์ง๊ด€์ ์ธ ๋ฌธ๋ฒ•"์ด๋ผ๋Š” ํ…์ŠคํŠธ์˜ level์„ 1๋กœ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ "์ง๊ด€์ ์ธ ๋ฌธ๋ฒ•"์— ๋Œ€ํ•œ ํ•˜์œ„ ํ•ญ๋ชฉ์ด ์žˆ๋‹ค๋ฉด level 2๋กœ ์ง€์ •ํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋‘ ๋ฒˆ์งธ์™€ ์„ธ ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ๋“ค๊ณผ ๊ฐ๊ฐ์˜ ํ•˜์œ„ ํ•ญ๋ชฉ๋“ค๋„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ตœ์ข… ์ƒ์„ฑ๋œ ํŒŒ์ผ์„ ๋ณด๋ฉด, ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ํ•ญ๋ชฉ์— ๊ธ€๋จธ๋ฆฌ ๊ธฐํ˜ธ(bullet point, ๋ถˆ๋ฆฟ ํฌ์ธํŠธ)๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ƒ์œ„ ํ•ญ๋ชฉ๊ณผ ํ•˜์œ„ ํ•ญ๋ชฉ์˜ ๊ธ€๋จธ๋ฆฌ ๊ธฐํ˜ธ๋„ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธฐํ˜ธ๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ๋ณ„๋„๋กœ ๊ธฐํ˜ธ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ ๊ธ€๋จธ๋ฆฌ ๊ธฐํ˜ธ๊ฐ€ ์„ค์ •๋œ ์ด์œ ๋Š” ๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ์— ๊ธ€๋จธ๋ฆฌ ๊ธฐํ˜ธ ์„ค์ •์ด ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๊ธ€๋จธ๋ฆฌ ๊ธฐํ˜ธ๋Š” ๋ ˆ์ด์•„์›ƒ์— ์ •์˜๋˜์–ด ์žˆ๋Š” ์„ค์ • ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋˜๋ฉฐ, python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ๋Š” ๊ธ€๋จธ๋ฆฌ ๊ธฐํ˜ธ์˜ ์Šคํƒ€์ผ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์„œ์‹ ์„ค์ •ํ•˜๊ธฐ ํ…์ŠคํŠธ ๋ฐ•์Šค์— ์„œ์‹์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from pptx import Presentation from pptx.util import Inches, Pt from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT from pptx.dml.color import RGBColor prs = Presentation() slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "์„œ์‹ ์„ค์ •ํ•˜๊ธฐ" # ์Šฌ๋ผ์ด๋“œ์— ํ…์ŠคํŠธ ๋ฐ•์Šค ์ถ”๊ฐ€ left = top = Inches(1) width = height = Inches(5) txBox = slide.shapes.add_textbox(left, top, width, height) tf = txBox.text_frame # ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ ์ถ”๊ฐ€ ๋ฐ ๊ธ€๊ผด ์Šคํƒ€์ผ ์„ค์ • p = tf.add_paragraph() p.text = "์„œ์‹์„ ์„ค์ •ํ•œ ํ…์ŠคํŠธ" p.font.name = '๋‚˜๋ˆ” ๋ฐ”๋ฅธ ๊ณ ๋”•' # ๊ธ€๊ผด ์„ค์ • p.font.bold = True p.font.size = Pt(30) p.font.color.rgb = RGBColor(0x42, 0x24, 0xE9) # RGBColor ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€๊ผด ์ƒ‰์ƒ ์„ค์ • p.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER # ํ…์ŠคํŠธ ์ •๋ ฌ ์„ค์ • prs.save('presentation_kor.pptx') 'presentation_kor.pptx' ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด' ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์„œ์‹์„ ์„ค์ •ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„์€ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ์‹์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๊ธ€๊ผด์€ font.name ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ๊ธ€๊ผด์˜ ์ข…๋ฅ˜๋ฅผ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ๊ธ€๊ผด๊ณผ ์˜์–ด ๊ธ€๊ผด ๋ชจ๋‘ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์„ค์ •ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ธ€๊ผด์ด ์‹œ์Šคํ…œ์— ์„ค์น˜๊ฐ€ ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ตต๊ฒŒ (bold), ๊ธฐ์šธ์ž„ (italic), ๋ฐ‘์ค„ (underline) ๋“ฑ์˜ ๊ธ€์ž ์„œ์‹์€ font ๊ฐ์ฒด์˜ ์†์„ฑ์„ True๋กœ ์„ค์ •ํ•˜์—ฌ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธ€์ž์˜ ํฌ๊ธฐ๋Š” font.size ์†์„ฑ์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์›Œํฌ์ธํŠธ์˜ ๊ธฐ๋ณธ ๊ธ€์ž ํฌ๊ธฐ ๋‹จ์œ„์™€ ๋™์ผํ•˜๊ฒŒ ํฌ์ธํŠธ ๋‹จ์œ„๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด from pptx.util import Pt๋ฅผ ์‹คํ–‰ํ•œ ํ›„, Pt()๋กœ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. font.color.rgb ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€์ž ์ƒ‰์ƒ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. RGBColor ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ RGB ์ฝ”๋“œ (์˜ˆ: ํŒŒ๋ž€์ƒ‰ ์ฝ”๋“œ)๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค ์‚ฌ์šฉ์„ ์œ„ํ•ด from pptx.dml.color import RGBColor๋ฅผ ๋จผ์ € ์‹คํ–‰ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. PP_PARAGRAPH_ALIGNMENT ์—ด๊ฑฐํ˜•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ๋ฝ ์ •๋ ฌ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ€์šด๋ฐ ์ •๋ ฌ (CENTER)์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, LEFT๋กœ ์™ผ์ชฝ ์ •๋ ฌ, RIGHT๋กœ ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๋ ฌ ์„œ์‹์„ ์œ„ํ•ด์„œ๋Š” from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT๋ฅผ ์‹คํ–‰ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ํ…์ŠคํŠธ ์„œ์‹์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์„œ์‹์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ์˜ ์„œ์‹์ด ์ž๋™ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ํ‘œ ์ถ”๊ฐ€ํ•˜๊ธฐ add_table ๋ฉ”์„œ๋“œ๋กœ ์Šฌ๋ผ์ด๋“œ์— ํ‘œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ 3ํ–‰ 2์—ด์˜ ํ‘œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ ์…€์— ํ–‰๊ณผ ์—ด์˜ ๊ฐ’์„ ํ…์ŠคํŠธ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. from pptx import Presentation from pptx.util import Inches prs = Presentation() slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "ํ‘œ ์ถ”๊ฐ€ํ•˜๊ธฐ" # ํ‘œ ์ถ”๊ฐ€ ์œ„์น˜ ๋ฐ ํฌ๊ธฐ ์„ค์ • left = Inches(2) top = Inches(2) width = Inches(6) height = Inches(4.5) # ํ‘œ ์ถ”๊ฐ€ (3ํ–‰ 2์—ด) rows, cols = 3, 2 table = slide.shapes.add_table(rows, cols, left, top, width, height).table # ํ‘œ์— ํ…์ŠคํŠธ ์ถ”๊ฐ€ table.cell(0, 0).text = "์—ด์ด๋ฆ„ 1" table.cell(0, 1).text = "์—ด์ด๋ฆ„ 2" table.cell(1, 0).text = "1 ํ–‰, 1 ์—ด" table.cell(1, 1).text = "1 ํ–‰, 2 ์—ด" table.cell(2, 0).text = "2 ํ–‰, 1 ์—ด" table.cell(2, 1).text = "2 ํ–‰, 2 ์—ด" prs.save('presentation_with_table.pptx') 'presentation_with_table.pptx' ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด ์Šฌ๋ผ์ด๋“œ์— add_table ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•  ํ‘œ์˜ ํ–‰๊ณผ ์—ด์˜ ์ˆ˜, ๊ทธ๋ฆฌ๊ณ  ํ‘œ์˜ ํฌ๊ธฐ์™€ ์œ„์น˜๋ฅผ ์„ค์ •ํ•œ ํ›„, add_table ๋ฉ” ์„œ๋“œ์— ํ–‰์˜ ์ˆ˜, ์—ด์˜ ์ˆ˜, ์œ„์น˜, ํฌ๊ธฐ ์ˆœ์œผ๋กœ ์ธ์ž๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. table.cell(row, col).text ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์…€์— ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์—‘์…€์ด๋‚˜ ์›Œ๋“œ ํŒŒ์ผ์— ํ‘œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ํ•™์Šตํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ํ‘œ์— ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ 2์ฐจ์›์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ƒ์„ฑํ•œ ๋‹ค์Œ ์ด์ค‘ for ๋ฌธ์œผ๋กœ ๊ฐ ์…€์— ์ ‘๊ทผํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ‘œ์˜ ์„œ์‹ ์„ค์ • python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํ‘œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ํ‘œ์˜ ์†์„ฑ์„ ๋ณ„๋„๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์œ„์˜ ์˜ˆ์ œ์™€ ๊ฐ™์ด ๊ธฐ๋ณธ ์Šคํƒ€์ผ์ด ์ ์šฉ๋˜์–ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํ–‰ ๋†’์ด๋‚˜ ์—ด ๋„ˆ๋น„, ํ…Œ๋‘๋ฆฌ ์Šคํƒ€์ผ์ด๋‚˜ ์…€ ๋ฐฐ๊ฒฝ์ƒ‰ ๋“ฑ ํ‘œ์˜ ๋‹ค์–‘ํ•œ ์†์„ฑ์„ ์ง์ ‘ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‘œ์˜ ์†์„ฑ์„ ์„ค์ •ํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ํ–‰ ๋†’์ด ์„ค์ • for row in table.rows: row.height = Inches(1) # ์—ด ๋„ˆ๋น„ ์„ค์ • for col in table.columns: col.width = Inches(3) row.height์™€ col.width๋กœ ๊ฐ๊ฐ ํ–‰ ๋†’์ด์™€ ์—ด ๋„ˆ๋น„๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํ–‰์˜ ๋†’์ด์™€ ๋ชจ๋“  ์—ด์˜ ๋„ˆ๋น„๋ฅผ ๋™์ผํ•˜๊ฒŒ ์„ค์ •ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด for ๋ฌธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ ํ–‰์ด๋‚˜ ๊ฐ ์—ด์— ๋Œ€ํ•ด ์„œ๋กœ ๋‹ค๋ฅธ ๋†’์ด/๋„ˆ๋น„๋ฅผ ์„ค์ •ํ•˜๋ ค๋ฉด ๊ฐœ๋ณ„์ ์œผ๋กœ ํ–‰๊ณผ ์—ด์— ์ ‘๊ทผํ•˜์—ฌ ์ˆ˜์น˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์—ด์ด 2๊ฐœ์ด๋ฉฐ ์ฒซ ๋ฒˆ์งธ ์—ด์˜ ๋„ˆ๋น„๋Š” 2์ธ์น˜๋กœ, ๋‘ ๋ฒˆ์งธ ์—ด์˜ ๋„ˆ๋น„๋Š” 4์ธ์น˜๋กœ ์„ค์ •ํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. table.columns[0].width = Inches(2) table.columns[1].width = Inches(4) ์ด๋ฒˆ์—๋Š” ์…€์˜ ์„œ์‹์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ํŠน์ • ์…€์— ๋ฐฐ๊ฒฝ์ƒ‰๊ณผ ๊ธ€์ž ์„œ์‹์„ ์„ค์ •ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. # ํŠน์ • ์…€์˜ ๋ฐฐ๊ฒฝ์ƒ‰ ์„ค์ • table.cell(0, 0).fill.solid() table.cell(0, 0).fill.fore_color.rgb = RGBColor(91, 155, 213) table.cell(0, 0)๋กœ ์„œ์‹์„ ์„ค์ •ํ•  ์…€์„ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ๊ฒฝ์ƒ‰์„ ์„ค์ •ํ•  ๋•Œ๋Š” fill ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๊ณ  solid() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์ƒ‰ ๋ฐฐ๊ฒฝ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. cell.fill.fore_color_rgb ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ RGB<NAME>์œผ๋กœ ์ƒ‰์ƒ์„ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. RGB<NAME>์œผ๋กœ ๋ฐฐ๊ฒฝ์ƒ‰์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‚ฌ์ „์— from pptx.dml.color import RGBColor๊ฐ€ ์‹คํ–‰๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŠน์ • ์…€์˜ ๊ธ€์ž์ƒ‰์ด๋‚˜ ๊ธ€์ž ํฌ๊ธฐ๋„ ์„œ์‹์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ํŠน์ • ์…€์˜ ๊ธ€์ž ์„œ์‹ ์„ค์ • table.cell(0, 0).text_frame.paragraphs[0].runs[0].font.size = Pt(20) table.cell(0, 0).text_frame.paragraphs[0].runs[0].font.color.rgb = RGBColor(255, 255, 0) table.cell(0, 0)๋กœ ๋™์ผํ•˜๊ฒŒ ์„œ์‹์„ ์„ค์ •ํ•  ์…€์„ ์ฐธ์กฐํ•œ ๋‹ค์Œ, font.size๋กœ ๊ธ€์ž์˜ ํฌ๊ธฐ๋ฅผ, font.color.rgb๋กœ ๊ธ€์ž์˜ ์ปฌ๋Ÿฌ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ ๋จผ์ € ํ…์ŠคํŠธ๋ฅผ ์…€์— ํ• ๋‹นํ•˜๊ณ  ๋‚˜์„œ ํ•ด๋‹น ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ๊ธ€๊ผด์„ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ธ€๊ผด์„ ๋จผ์ € ์„ค์ •ํ•ด ๋†“์€ ๋‹ค์Œ ํ…์ŠคํŠธ๋ฅผ ํ• ๋‹นํ•˜๋ฉด, ํŒŒ์ด์ฌ์—์„œ๋Š” ํ…์ŠคํŠธ๋ฅผ ํ• ๋‹นํ•  ๋•Œ ๊ธฐ๋ณธ ๊ธ€์ž ์„œ์‹์œผ๋กœ ์…€์— ๋ฎ์–ด์“ฐ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด ์„œ์‹์ด ์‚ฌ๋ผ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ…์ŠคํŠธ๋ฅผ ํ• ๋‹นํ•œ ํ›„์— ๊ธ€๊ผด์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šตํ•œ ์„œ์‹ ์„ค์ • ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ‘œ์˜ ํ–‰ ๋†’์ด์™€ ์—ด ๋„ˆ๋น„๋ฅผ ์„ค์ •ํ•˜๊ณ , 0, 0์…€์— ๋ฐฐ๊ฒฝ์ƒ‰๊ณผ ๊ธ€์ž ์„œ์‹์„ ์ถ”๊ฐ€ํ•œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT from pptx.dml.color import RGBColor from pptx import Presentation from pptx.util import Inches prs = Presentation() slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "ํ‘œ ์ถ”๊ฐ€ํ•˜๊ธฐ" # ํ‘œ ์ถ”๊ฐ€ ์œ„์น˜ ๋ฐ ํฌ๊ธฐ ์„ค์ • left = Inches(2) top = Inches(2) width = Inches(6) height = Inches(4.5) # ํ‘œ ์ถ”๊ฐ€ (3ํ–‰ 2์—ด) rows, cols = 3, 2 table = slide.shapes.add_table(rows, cols, left, top, width, height).table # ํ–‰ ๋†’์ด ์„ค์ • for row in table.rows: row.height = Inches(1) # ์—ด ๋„ˆ๋น„ ์„ค์ • for col in table.columns: col.width = Inches(3) # ํ‘œ์— ํ…์ŠคํŠธ ์ถ”๊ฐ€ table.cell(0, 0).text = "์—ด์ด๋ฆ„ 1" table.cell(0, 1).text = "์—ด์ด๋ฆ„ 2" table.cell(1, 0).text = "1 ํ–‰, 1 ์—ด" table.cell(1, 1).text = "1 ํ–‰, 2 ์—ด" table.cell(2, 0).text = "2 ํ–‰, 1 ์—ด" table.cell(2, 1).text = "2 ํ–‰, 2 ์—ด" # ํŠน์ • ์…€์˜ ๊ธ€๊ผด ์„ค์ • table.cell(0, 0).text_frame.paragraphs[0].runs[0].font.size = Pt(40) table.cell(0, 0).text_frame.paragraphs[0].runs[0].font.color.rgb = RGBColor(255, 255, 0) # ํŠน์ • ์…€์˜ ๋ฐฐ๊ฒฝ์ƒ‰ ์„ค์ • table.cell(0, 0).fill.solid() table.cell(0, 0).fill.fore_color.rgb = RGBColor(91, 155, 213) prs.save('presentation_with_table2.pptx') 'presentation_with_table2.pptx' ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด 06-02. ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ ๊ทธ๋ž˜ํ”ฝ ์š”์†Œ ์ถ”๊ฐ€ ํŒŒ์ด์ฌ์—์„œ ํŒŒ์›Œํฌ์ธํŠธ์— ๋„ํ˜•, ์ด๋ฏธ์ง€, ์ฐจํŠธ ๋“ฑ ๊ทธ๋ž˜ํ”ฝ ์š”์†Œ๋“ค์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ๊ทธ๋ž˜ํ”ฝ ์š”์†Œ๋“ค ์ค‘ ๋Œ€ํ‘œ์ ์ธ ๋ช‡ ๊ฐ€์ง€ ์š”์†Œ๋“ค์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋„ํ˜• ์ถ”๊ฐ€ํ•˜๊ธฐ ํŒŒ์›Œํฌ์ธํŠธ ์Šฌ๋ผ์ด๋“œ์— ๋„ํ˜•์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋Š” ์Šฌ๋ผ์ด๋“œ์— ์ƒ‰์ด ์žˆ๋Š” ๋‘ฅ๊ทผ ์ง์‚ฌ๊ฐํ˜•์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from pptx import Presentation from pptx.util import Inches from pptx.dml.color import RGBColor from pptx.enum.shapes import MSO_SHAPE prs = Presentation() slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "๋„ํ˜• ์ถ”๊ฐ€ํ•˜๊ธฐ" # ๋‘ฅ๊ทผ ์ง์‚ฌ๊ฐํ˜•์„ ์ถ”๊ฐ€ left = Inches(2) top = Inches(2) width = Inches(6) height = Inches(4.5) shape = slide.shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, left, top, width, height) # ๋„ํ˜•์˜ ์†์„ฑ ์„ค์ • shape.fill.solid() # ๋‹จ์ƒ‰ ์ฑ„์šฐ๊ธฐ shape.fill.fore_color.rgb = RGBColor(91, 155, 213) # ์ฑ„์šฐ๊ธฐ ์ƒ‰์ƒ ์„ค์ •(ํŒŒ๋ž€์ƒ‰) shape.line.color.rgb = RGBColor(0, 0, 0) # ํ…Œ๋‘๋ฆฌ ์ƒ‰์ƒ ์„ค์ •(๊ฒ€์€์ƒ‰) # ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ ํ›„ ์„œ์‹ ์„ค์ • shape.text = "๋‘ฅ๊ทผ ์ง์‚ฌ๊ฐํ˜•" shape.text_frame.paragraphs[0].font.bold = True shape.text_frame.paragraphs[0].font.size = Inches(0.5) prs.save('presentation_with_graphics.pptx') 'presentation_with_graphics.pptx' ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด ํŒŒ์›Œํฌ์ธํŠธ์— ๋„ํ˜•์„ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” from pptx.enum.shapes import MSO_SHAPE๋ฅผ ์‚ฌ์šฉํ•ด MSO_SHPAE๋ผ๋Š” ์—ด๊ฑฐํ˜•(enum)์„ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. MSO๋Š” Microsoft Office์˜ ์•ฝ์ž์ด๋ฉฐ, SHAPE๋Š” ๊ทธ๋ž˜ํ”ฝ์˜ ํ˜•ํƒœ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ฆ‰, ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ ์˜คํ”ผ์Šค ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ข… ๊ทธ๋ž˜ํ”ฝ ์š”์†Œ๋“ค(์ง์‚ฌ๊ฐํ˜•, ์›, ํ™”์‚ดํ‘œ ๋“ฑ)์„ ์ •์˜ํ•˜๊ณ  ์žˆ์–ด์„œ ์ด๋ฅผ ํ†ตํ•ด ํŠน์ • ํ˜•ํƒœ์˜ ๊ทธ๋ž˜ํ”ฝ ์š”์†Œ๋“ค์„ ์‰ฝ๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” MSO_SHAPE.ROUNDED_RECTANGLE๋กœ ๋‘ฅ๊ทผ ๋ชจ์„œ๋ฆฌ์˜ ์ง์‚ฌ๊ฐํ˜•์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. slide.shapes.add_shape ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ MSO_SHAPE ์—ด๊ฑฐํ˜•์„ ์ „๋‹ฌํ•˜์—ฌ ๋„ํ˜•์˜ ์ข…๋ฅ˜๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  left, top, width, height ์ธ์ž๋ฅผ ์ฐจ๋ก€ ์ „๋‹ฌํ•˜์—ฌ ๋„ํ˜•์˜ ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•œ ๋„ํ˜•์— ์ถ”๊ฐ€์ ์ธ ์†์„ฑ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๋„ํ˜•์˜ ์†์„ฑ ์„ค์ • shape.fill.solid() # ๋‹จ์ƒ‰ ์ฑ„์šฐ๊ธฐ shape.fill.fore_color.rgb = RGBColor(91, 155, 213) # ์ฑ„์šฐ๊ธฐ ์ƒ‰์ƒ ์„ค์ •(ํŒŒ๋ž€์ƒ‰) shape.line.color.rgb = RGBColor(0, 0, 0) # ํ…Œ๋‘๋ฆฌ ์ƒ‰์ƒ ์„ค์ •(๊ฒ€์€์ƒ‰) fill ๋ฉ”์„œ๋“œ๋กœ ๋„ํ˜•์˜ ์ƒ‰์„ ์ฑ„์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ‰์„ ์ฑ„์šธ ๋•Œ๋Š” ๋‹จ์ƒ‰ ์ฑ„์šฐ๊ธฐ ์™ธ์—๋„ ๊ทธ๋Ÿฌ๋ฐ์ด์…˜, ์งˆ๊ฐ(๋ฌด๋Šฌ), ๊ทธ๋ฆผ ๋ฐฐ๊ฒฝ ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ฑ„์šฐ๊ธฐ ์Šคํƒ€์ผ ์ค‘์— ์›ํ•˜๋Š” ์Šคํƒ€์ผ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋‹จ์ƒ‰์œผ๋กœ ๋„ํ˜•์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด fill.solid()๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋„ํ˜•์— ์ƒ‰์„ ์ฑ„์šธ ๋•Œ ์ฑ„์šฐ๊ธฐ ์ƒ‰์ƒ์€ RGB ์ƒ‰์ƒ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ƒ‰ ์ฑ„์šฐ๊ธฐ์—์„œ๋Š” ์ฃผ๋œ ์ƒ‰์ƒ ํ•˜๋‚˜๋งŒ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— fill.fore_color.rgb ๋งŒ์œผ๋กœ ์ƒ‰์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์งˆ๊ฐ ํšจ๊ณผ ๋“ฑ ๋‘ ๋ฒˆ์งธ ์ƒ‰์ƒ์„ ํ•„์š”๋กœ ํ•˜๋Š” ์ฑ„์šฐ๊ธฐ ํšจ๊ณผ๋ฅผ ์ ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” back_color๋กœ ๋‹ค๋ฅธ ์ƒ‰์ƒ์„ ์ถ”๊ฐ€๋กœ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” shape.fill.fore_color.rgb = RGBColor(91, 155, 213)๋กœ ๋„ํ˜•์— ๋‹จ์ƒ‰์œผ๋กœ ํŒŒ๋ž€์ƒ‰์„ ์ฑ„์›Œ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. line ๋ฉ”์„œ๋“œ๋กœ ํ…Œ๋‘๋ฆฌ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ๋‘๋ฆฌ์˜ ์ปฌ๋Ÿฌ๋„ ๋™์ผํ•˜๊ฒŒ RGB ์ฝ”๋“œ๋กœ ๊ฐ’์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ๊ฒ€์€์ƒ‰ ํ…Œ๋‘๋ฆฌ๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด shape.line.color.rgb = RGBColor(0, 0, 0)๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ์„ค์ •ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ shadow ๋ฉ”์„œ๋“œ๋กœ ๋„ํ˜•์— ๊ทธ๋ฆผ์ž ํšจ๊ณผ๋ฅผ ์ ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์ž์˜ ํˆฌ๋ช…๋„๋‚˜ ์ƒ‰์ƒ, ๋ฐ˜๊ฒฝ ๋“ฑ ๊ทธ๋ฆผ์ž์˜ ์†์„ฑ๋„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๊ทธ๋ฆผ์ž ํšจ๊ณผ ์™ธ์— ๋ถ€๋“œ๋Ÿฌ์šด ๊ฐ€์žฅ์ž๋ฆฌ ํšจ๊ณผ, ๋„ค์˜จ ํšจ๊ณผ ๋“ฑ ๋‹ค๋ฅธ ๊ธฐํƒ€ ๊ณ ๊ธ‰ ํšจ๊ณผ๋ฅผ ์ ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ์€ ์•„์ง python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ๋Š” ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. # ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ ํ›„ ์„œ์‹ ์„ค์ • shape.text = "๋‘ฅ๊ทผ ์ง์‚ฌ๊ฐํ˜•" shape.text_frame.paragraphs[0].font.bold = True shape.text_frame.paragraphs[0].font.size = Inches(0.5) shape.text ์†์„ฑ์œผ๋กœ ๋„ํ˜•์— ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋„ํ˜•์— ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. shape.text ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•˜๋ฉด, ์ด์ „์— ๋„ํ˜•์— ์„ค์ •๋œ ๋ชจ๋“  ํ…์ŠคํŠธ๊ฐ€ ์‚ญ์ œ๋˜๊ณ  ์ƒˆ ํ…์ŠคํŠธ๊ฐ€ ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค. ๋„ํ˜•์— ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ์— ์ถ”๊ฐ€์ ์ธ ์„œ์‹์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์˜ ์ฝ”๋“œ์—์„œ์ฒ˜๋Ÿผ shape.text_frame ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. text_frame์„ ์‚ฌ์šฉํ•˜๋ฉด ๋„ํ˜•์— ์—ฌ๋Ÿฌ ๋‹จ๋ฝ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ฐ๊ฐ์˜ ๋‹จ๋ฝ์— ๋‹ค๋ฅธ ์„œ์‹์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ๋‹จ๋ฝ์— ์ถ”๊ฐ€๋œ ํ…์ŠคํŠธ์˜ ์„œ์‹์„ ๊ตต๊ฒŒ ๋ณ€๊ฒฝํ•˜๊ณ , ๊ธ€์ž ํฌ๊ธฐ๋„ ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด python-pptx๋กœ ์Šฌ๋ผ์ด๋“œ์— ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ๋„ํ˜•์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋„ํ˜•์˜ ์„œ์‹์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋„ํ˜• ์•ˆ์— ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ํ…์ŠคํŠธ์˜ ์„œ์‹์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ํ•˜๊ธฐ ํŒŒ์›Œํฌ์ธํŠธ์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋„ python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. from pptx import Presentation from pptx.util import Inches prs = Presentation() slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) # ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ๋ฅผ ์ง€์ • (์—ฌ๊ธฐ์„œ๋Š” ๋™์ผํ•œ ํด๋”์— ์žˆ๋Š” 'image1.png' ํŒŒ์ผ์„ ์‚ฌ์šฉ) img_path = 'image1.png' # ์ด๋ฏธ์ง€์˜ ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ์ง€์ • left = top = Inches(1) width = height = Inches(5) # ์ด๋ฏธ์ง€๋ฅผ ์Šฌ๋ผ์ด๋“œ์— ์ถ”๊ฐ€ slide.shapes.add_picture(img_path, left, top, width, height) prs.save('presentation_with_image.pptx') 'presentation_with_graphics.pptx' ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด ์Šฌ๋ผ์ด๋“œ์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋Š” ์‹คํ–‰ ๊ฒฝ๋กœ์™€ ๋™์ผํ•œ ๊ฒฝ๋กœ์˜ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ๋ช…๋งŒ ์ „๋‹ฌํ–ˆ์ง€๋งŒ, ๋‹ค๋ฅธ ๊ฒฝ๋กœ์˜ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์˜ฌ ๊ฒฝ์šฐ์—๋Š” ์ „์ฒด ๊ฒฝ๋กœ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹๋„ ๋„ํ˜•์„ ์ถ”๊ฐ€ํ•  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‚ฝ์ž…ํ•  ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์™€ ์œ„์น˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์œ„์น˜์˜ ๊ฐ’์œผ๋กœ ๋ชจ๋‘ 1์ธ์น˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์™ผ์ชฝ ์ƒ๋‹จ์œผ๋กœ๋ถ€ํ„ฐ 1์ธ์น˜ x1์ธ์น˜ ๋–จ์–ด์ง„ ์œ„์น˜๋กœ ์ง€์ •ํ•˜์˜€์œผ๋ฉฐ, ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋Š” ๊ฐ€๋กœ์™€ ์„ธ๋กœ ๋™์ผํ•˜๊ฒŒ 5์ธ์น˜ x5์ธ์น˜์˜ ํฌ๊ธฐ๋กœ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•œ ์ด๋ฏธ์ง€์˜ ๊ฒฝ๋กœ, ์œ„์น˜, ํฌ๊ธฐ์˜ ๊ฐ’์„ add_picture ๋ฉ”์„œ๋“œ์— ์ธ์ž๋กœ ์ „๋‹ฌํ•˜์—ฌ ์Šฌ๋ผ์ด๋“œ์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋„ํ˜•์ด๋‚˜ ์ด๋ฏธ์ง€๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ ํฌ๊ธฐ๋‚˜ ์œ„์น˜๊ฐ€ ๊ฒน์น˜๊ฒŒ ๋  ๊ฒฝ์šฐ, ๋จผ์ € ์‚ฝ์ž…๋œ ๊ฐ์ฒด๊ฐ€ ๋’ค์— ์œ„์น˜ํ•˜๊ฒŒ ๋˜๊ณ , ๋‚˜์ค‘์— ์‚ฝ์ž…๋œ ๊ฐ์ฒด๊ฐ€ ์•ž์— ์œ„์น˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ฝ”๋“œ์—์„œ ๋‚˜์ค‘์— ์ถ”๊ฐ€๋œ ๊ฐ์ฒด๊ฐ€ ์Šฌ๋ผ์ด๋“œ์—์„œ ์ƒ๋‹จ์— ๋ณด์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ด๋ฏธ ์‚ฝ์ž…๋œ ๋„ํ˜•์ด๋‚˜ ์ด๋ฏธ์ง€์˜ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ธฐ๋Šฅ์€ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ์ฒด์˜ ์ˆœ์„œ๋ฅผ ์กฐ์ ˆํ•˜๋ ค๋ฉด ์‚ฝ์ž… ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์กฐ์ ˆํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฐจํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฐจํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ง‰๋Œ€ ์ฐจํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from pptx import Presentation from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE from pptx.util import Inches prs = Presentation() slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "์ฐจํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ" # ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค chart_data = CategoryChartData() chart_data.categories = ['A ์ œํ’ˆ', 'B ์ œํ’ˆ', 'C ์ œํ’ˆ'] chart_data.add_series('์ž”๋ฅ˜์˜ค์—ผ๋„', (9.2, 11.4, 16.7)) # ์ฐจํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5) # ์Šฌ๋ผ์ด๋“œ์— ์ฐจํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค chart = slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data ).chart # ์ฐจํŠธ ์ œ๋ชฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค chart.has_title = True chart.chart_title.text_frame.text = "์ œํ’ˆ๋ณ„ ์„ธ์ •๋ ฅ ์ธก์ •" # X์™€ Y ์ถ•์˜ ์ œ๋ชฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค chart.category_axis.axis_title.text_frame.text = "์ œํ’ˆ" chart.value_axis.axis_title.text_frame.text = "์ œํ’ˆ ์‚ฌ์šฉ ํ›„ ์ž”๋ฅ˜ ์˜ค์—ผ๋„" prs.save('presentation_with_chart.pptx') 'presentation_with_chart.pptx' ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด ์ฐจํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” ์ฐจํŠธ์˜ ์ข…๋ฅ˜, ์ถ• ์ œ๋ชฉ, ์ˆ˜์น˜ ๋ฐ์ดํ„ฐ, ํฌ๊ธฐ ๋ฐ ์œ„์น˜ ๋“ฑ ๋‹ค์–‘ํ•œ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ๋ณต์žกํ•˜๊ฒŒ ๋Š๊ปด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์ž์„ธํ•˜๊ฒŒ ๋‚˜๋ˆ„์–ด ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from pptx import Presentation from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE from pptx.util import Inches ์ฐจํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ๊ธฐ๋ณธ Presentation ํด๋ž˜์Šค์™€ ์ฐจํŠธ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•œ Inches ํ•จ์ˆ˜ ์™ธ์—๋„ ์ฐจํŠธ์˜ ์ข…๋ฅ˜๋ฅผ ์ •์˜ํ•˜๋Š” XL_CHART_TYPE ์—ด๊ฑฐํ˜•, ๊ทธ๋ฆฌ๊ณ  ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ CategoryChartData ํด๋ž˜์Šค๊ฐ€ ์ถ”๊ฐ€๋กœ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์˜ ๋ชจ๋“ˆ๊ณผ ํด๋ž˜์Šค๋ฅผ ๋จผ์ € ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. prs = Presentation() slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "์ฐจํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ" # ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค chart_data = CategoryChartData() chart_data.categories = ['A ์ œํ’ˆ', 'B ์ œํ’ˆ', 'C ์ œํ’ˆ'] chart_data.add_series('์ž”๋ฅ˜์˜ค์—ผ๋„', (9.2, 11.4, 16.7)) ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ์„ ์ •์˜ํ•˜๊ณ  ์ƒˆ ์Šฌ๋ผ์ด๋“œ์— ์ œ๋ชฉ์„ ์ถ”๊ฐ€ํ•œ ๋‹ค์Œ, CategoryChartData()๋กœ ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. categories๋กœ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์„ค์ •ํ•˜๊ณ  add_series๋กœ ๊ฐ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ๋ฐ์ดํ„ฐ์˜ ์‹œ๋ฆฌ์ฆˆ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ฐจํŠธ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ์–ด "์‹œ๋ฆฌ์ฆˆ"๋ผ๋Š” ๊ทธ๋ฃน์œผ๋กœ ๋งŒ๋“ค๋ฉฐ, ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ๋œ ํ•˜๋‚˜์˜ ์‹œ๋ฆฌ์ฆˆ๋Š” ์ฐจํŠธ์—์„œ ํŠน์ • ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ํ‘œํ˜„ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ ์„ธํŠธ๋Š” ์ฐจํŠธ์—์„œ ํ•œ ์ค„(๋ผ์ธ ์ฐจํŠธ์˜ ๊ฒฝ์šฐ)์ด๋‚˜ ํ•œ ๊ทธ๋ฃน์˜ ๋ง‰๋Œ€(๋ง‰๋Œ€ ์ฐจํŠธ์˜ ๊ฒฝ์šฐ) ๋“ฑ์œผ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ์‹œ๋ฆฌ์ฆˆ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ ๊ฐ ์‹œ๋ฆฌ์ฆˆ๋Š” ์ฐจํŠธ์— ๋ณ„๊ฐœ์˜ ๋ผ์ธ์ด๋‚˜ ๋ง‰๋Œ€ ๊ทธ๋ฃน์œผ๋กœ ํ‘œ์‹œ๋˜์–ด ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ํ•จ๊ป˜ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” "์ž”๋ฅ˜์˜ค์—ผ๋„"๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์‹œ๋ฆฌ์ฆˆ๋ฅผ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. # ์ฐจํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5) ์ฐจํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์œ„์น˜๋Š” x, y ๋ณ€์ˆ˜์— ํฌ๊ธฐ๋Š” cx, cy ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค. # ์Šฌ๋ผ์ด๋“œ์— ์ฐจํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค chart = slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data ).chart add_chart ๋ฉ” ์„œ๋“œ๋กœ ์Šฌ๋ผ์ด๋“œ์— ์ฐจํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. add_chart ๋ฉ” ์„œ๋“œ์— ์ฐจํŠธ์˜ ์œ ํ˜•, ์œ„์น˜, ํฌ๊ธฐ, ๊ทธ๋ฆฌ๊ณ  ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ฐจํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฐจํŠธ์˜ ์ข…๋ฅ˜๋ฅผ COLUMN_CLUSTERED(์ˆ˜์ง ๋ง‰๋Œ€ ์ฐจํŠธ)๋กœ ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. # ์ฐจํŠธ ์ œ๋ชฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค chart.has_title = True chart.chart_title.text_frame.text = "์ œํ’ˆ๋ณ„ ์„ธ์ •๋ ฅ ์ธก์ •" # X์™€ Y ์ถ•์˜ ์ œ๋ชฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค chart.category_axis.axis_title.text_frame.text = "์ œํ’ˆ" chart.value_axis.axis_title.text_frame.text = "์ œํ’ˆ ์‚ฌ์šฉ ํ›„ ์ž”๋ฅ˜ ์˜ค์—ผ๋„" prs.save('presentation_with_chart.pptx') ์ฐจํŠธ์˜ ์ œ๋ชฉ๊ณผ X์ถ•, Y ์ถ•์˜ ์ œ๋ชฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฐจํŠธ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” has_title ์†์„ฑ์„ True๋กœ ์„ค์ •ํ•˜์—ฌ ์ œ๋ชฉ์„ ํ™œ์„ฑํ™”ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. chart_title.text_frame.text๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ œ๋ชฉ ํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. X์ถ•๊ณผ Y ์ถ•์˜ ์ถ•์ œ๋ชฉ์€ chart.category_axis.axis_title.text_frame.text์™€ chart.value_axis.axis_title.text_frame.text๋กœ ๊ฐ๊ฐ ์„ค์ • ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ์—์„œ ์ถ”๊ฐ€ํ•œ ์ˆ˜์ง ๋ง‰๋Œ€ ์ฐจํŠธ ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์ฐจํŠธ ์œ ํ˜•์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pptx.enum.chart.XL_CHART_TYPE ์—ด๊ฑฐํ˜•์„ ์กฐํšŒํ•˜๋ฉด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ฐจํŠธ์˜ ์œ ํ˜•์„ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ฐจํŠธ ์œ ํ˜•์˜ ์ „์ฒด ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from pptx.enum.chart import XL_CHART_TYPE # ๊ฐ€๋Šฅํ•œ ์ฐจํŠธ ์œ ํ˜•์„ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค chart_types = [attr for attr in dir(XL_CHART_TYPE) if not callable(getattr(XL_CHART_TYPE, attr)) and not attr.startswith("__")] for chart_type in chart_types: print(chart_type) # ๊ฒฐ๊ด๊ฐ’ AREA AREA_STACKED AREA_STACKED_100 BAR_CLUSTERED BAR_OF_PIE BAR_STACKED BAR_STACKED_100 BUBBLE BUBBLE_THREE_D_EFFECT COLUMN_CLUSTERED COLUMN_STACKED (์ค‘๋žต) 06-03. ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ ์ฝ๊ธฐ ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ ์—ด๊ธฐ python-pptx๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ์„ ์—ด์–ด ๋ด…์‹œ๋‹ค. from pptx import Presentation # ๊ธฐ์กด์˜ ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ ์—ด๊ธฐ prs = Presentation('presentation_with_list.pptx') ํŒŒ์ผ์„ ์—ฌ๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ Presentation()์— ์—ด๊ณ ์ž ํ•˜๋Š” ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์Šฌ๋ผ์ด๋“œ ์ œ๋ชฉ๊ณผ ๋ณธ๋ฌธ ํ…์ŠคํŠธ ์ถ”์ถœ ๊ทธ๋Ÿฌ๋ฉด ์ด๋ฒˆ์—๋Š” ์œ„์—์„œ ์—ด์—ˆ๋˜ ํŒŒ์ผ์„ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ ์Šฌ๋ผ์ด๋“œ์—์„œ ์ œ๋ชฉ๊ณผ ๋ณธ๋ฌธ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๋ ค๋ฉด ์Šฌ๋ผ์ด๋“œ์˜ ๊ฐ ์š”์†Œ๋ฅผ ์ˆœํ™˜ํ•˜๋ฉฐ ํ…์ŠคํŠธ๋ฅผ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. from pptx import Presentation prs = Presentation('presentation_with_list.pptx') # ๊ฐ ์Šฌ๋ผ์ด๋“œ์˜ ์ œ๋ชฉ๊ณผ ๋ณธ๋ฌธ ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅ for slide_number, slide in enumerate(prs.slides): #์ „์ฒด ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ˆœํšŒ print(f"Slide {slide_number+1}") # ์Šฌ๋ผ์ด๋“œ์˜ ์ œ๋ชฉ ์ถœ๋ ฅ (์ œ๋ชฉ์ด ์žˆ๋Š” ๊ฒฝ์šฐ) if slide.shapes.title: print(f"Title: {slide.shapes.title.text}") # ์Šฌ๋ผ์ด๋“œ์˜ ๋ณธ๋ฌธ ํ…์ŠคํŠธ ์ถœ๋ ฅ #๊ฐ ์Šฌ๋ผ์ด๋“œ์— ํฌํ•จ๋œ ๋ชจ๋“  ๊ฐœ์ฒด(๋„ํ˜•, ํ…์ŠคํŠธ ์ƒ์ž, ํ‘œ ๋“ฑ)๋ฅผ ์ˆœํšŒ for shape in slide.shapes: # ํ˜„์žฌ ๊ฐœ์ฒด์˜ ํ˜•ํƒœ๊ฐ€ ํ…์ŠคํŠธ ์ƒ์ž์ธ์ง€ ํ™•์ธ, ํ…์ŠคํŠธ ์ƒ์ž์ผ ๊ฒฝ์šฐ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ if hasattr(shape, "text_frame") and shape.text_frame: for paragraph in shape.text_frame.paragraphs: # ํ…์ŠคํŠธ ์ƒ์ž์˜ ๋ชจ๋“  ๋‹จ๋ฝ์„ ์ˆœํšŒ for run in paragraph.runs: # ํ˜„์žฌ ๋‹จ๋ฝ์˜ ํ…์ŠคํŠธ ๋Ÿฐ์— ์ ‘๊ทผ print(run.text) #ํ˜„์žฌ ํ…์ŠคํŠธ ๋Ÿฐ์„ ์ถœ๋ ฅ # ๊ฒฐ๊ด๊ฐ’ Slide 1 Title: ํŒŒ์ด์ฌ์˜ ์žฅ์  1 ํŒŒ์ด์ฌ์˜ ์žฅ์  1 ์‰ฌ์šด ์‚ฌ์šฉ๋ฒ• ์ง๊ด€์ ์ธ ๋ฌธ๋ฒ• Slide 2 Title: ํŒŒ์ด์ฌ์˜ ์žฅ์  2 ํŒŒ์ด์ฌ์˜ ์žฅ์  2 ๋†’์€ ์ƒ์‚ฐ์„ฑ ๋น ๋ฅธ ๊ฐœ๋ฐœ ์†๋„ Slide 3 Title: ํŒŒ์ด์ฌ์˜ ์žฅ์  3 ํŒŒ์ด์ฌ์˜ ์žฅ์  3 ๋‹ค์–‘ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํ”„๋ ˆ์ž„์›Œํฌ ๋จธ์‹  ๋Ÿฌ๋‹, ์›น ๊ฐœ๋ฐœ ๋“ฑ์— ์œ ์šฉ ์ „์ฒด ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ, ๊ฐ ์Šฌ๋ผ์ด๋“œ์— ํฌํ•จ๋œ ๊ฐœ์ฒด๋ฅผ ๋ชจ๋‘ ์ˆœํšŒํ•˜์—ฌ ํ…์ŠคํŠธ ์ถ”์ถœํ•˜๋„๋ก ์ด์ค‘ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์Šฌ๋ผ์ด๋“œ์— ์ œ๋ชฉ์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์žˆ๋‹ค๋ฉด ์ œ๋ชฉ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์—๋Š” ์ œ๋ชฉ์„ ํฌํ•จํ•œ ๋ชจ๋“  ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. if ๋ฌธ์— hasattr๋กœ ์Šฌ๋ผ์ด๋“œ์— ํฌํ•จ๋œ ๊ฐœ์ฒด์˜ ํ˜•ํƒœ๊ฐ€ ํ…์ŠคํŠธ์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๋„๋ก ์กฐ๊ฑด์„ ์„ค์ •(if hasattr(shape, "text") and shape.text_frame) ํ•˜๊ณ , ํ…์ŠคํŠธ์ธ ๊ฒฝ์šฐ์—๋งŒ ํ…์ŠคํŠธ์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ๋ช‡ ๋ฒˆ์งธ ์Šฌ๋ผ์ด๋“œ์—์„œ ์ถœ๋ ฅํ•œ ๊ฒƒ์ธ์ง€ ์ง๊ด€์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ์Šฌ๋ผ์ด๋“œ ๋ฒˆํ˜ธ๋ฅผ ํ•จ๊ป˜ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (print(f"Slide {slide_number+1}: {shape.text}")) ์ถ”์ถœ๋œ ๊ฒฐ๊ด๊ฐ’์„ ๋ณด๋ฉด ํ•ด๋‹น ํŒŒ์ผ์€ 3๊ฐœ์˜ ์Šฌ๋ผ์ด๋“œ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๊ฐ ์Šฌ๋ผ์ด๋“œ์—๋Š” "ํŒŒ์ด์ฌ์˜ ์žฅ์  1/2/3"์ด๋ผ๋Š” ์ œ๋ชฉ๊ณผ ๋ณธ๋ฌธ์˜ ๋‚ด์šฉ์ด ์ž‘์„ฑ๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”์ถœํ•œ ํ…์ŠคํŠธ๋ฅผ ํŒŒ์ผ์— ์ €์žฅ ์œ„์—์„œ ์ถ”์ถœํ•œ ํ…์ŠคํŠธ๋ฅผ ํŒŒ์ผ์— ์ €์žฅํ•˜๋ ค๋ฉด, ํ…์ŠคํŠธ๋ฅผ ๋ฌธ์ž์—ด ๋ณ€์ˆ˜์— ์ถ”๊ฐ€ํ•œ ํ›„ ํŒŒ์ผ์— ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from pptx import Presentation prs = Presentation('presentation_with_list.pptx') extracted_text = "" # ๋นˆ ๋ฌธ์ž์—ด ๋ณ€์ˆ˜ 'extracted_text'๋ฅผ ์ƒ์„ฑ for slide_number, slide in enumerate(prs.slides): for shape in slide.shapes: # ํ…์ŠคํŠธ์ผ ๊ฒฝ์šฐ, extracted_text์— ์Šฌ๋ผ์ด๋“œ ๋ฒˆํ˜ธ์™€ ํ…์ŠคํŠธ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ if hasattr(shape, "text"): extracted_text += f"Slide {slide_number+1}: {shape.text}\n" with open('extracted_text.txt', 'w') as file: file.write(extracted_text) ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ์˜ ํ…์ŠคํŠธ๊ฐ€ 'extracted_text.txt'๋ผ๋Š” ํŒŒ์ผ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋ฅผ ํŒŒ์ผ๋กœ ์ถ”์ถœํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํŒŒ์ผ์— ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‘œ ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ํ‘œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ‘œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๋ ค๋ฉด ๊ฐ ์…€์˜ ๋‚ด์šฉ์„ ์ˆœํšŒํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋Š” ํŒŒ์›Œํฌ์ธํŠธ ์Šฌ๋ผ์ด๋“œ์—์„œ ํ‘œ๋ฅผ ์ฐพ๊ณ , ๊ทธ ์•ˆ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. from pptx import Presentation prs = Presentation("presentation_with_table.pptx") # ์ „์ฒด ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ํ‘œ๋ฅผ ์ฐพ์•„ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœ for slide_number, slide in enumerate(prs.slides): #๊ฐ ์Šฌ๋ผ์ด๋“œ์— ํฌํ•จ๋œ ๋ชจ๋“  ๊ฐœ์ฒด(๋„ํ˜•, ํ…์ŠคํŠธ ์ƒ์ž, ํ‘œ ๋“ฑ)๋ฅผ ์ˆœํšŒ for shape in slide.shapes: if hasattr(shape, "table"): # ํ˜„์žฌ ๊ฐœ์ฒด์˜ ํ˜•ํƒœ๊ฐ€ ํ‘œ์ธ์ง€ ํ™•์ธ table = shape.table # ํ˜„์žฌ ํ‘œ๋ฅผ table ๋ณ€์ˆ˜์— ํ• ๋‹น for row in table.rows: # ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์ˆœํšŒ for cell in row.cells: # ํ˜„์žฌ ํ–‰์˜ ๋ชจ๋“  ์…€์„ ์ˆœํšŒ print(cell.text) #ํ˜„์žฌ ์…€์˜ ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅ # ๊ฒฐ๊ด๊ฐ’ ์—ด์ด๋ฆ„ 1 ์—ด์ด๋ฆ„ 2 1 ํ–‰, 1 ์—ด 1 ํ–‰, 2 ์—ด 2 ํ–‰, 1 ์—ด 2 ํ–‰, 2 ์—ด ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ‘œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋„ ๊ฐ ์Šฌ๋ผ์ด๋“œ์— ํฌํ•จ๋œ ๋ชจ๋“  ๊ฐœ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐœ์ฒด๊ฐ€ ํ‘œ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ํ‘œ์ธ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ํ‘œ์˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์™€ ๋ชจ๋“  ํ–‰์„ ์ˆœํšŒํ•˜๋ฉฐ, ๊ฐ ํ–‰์—์„œ๋Š” ๋ชจ๋“  ์…€์„ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์…€์„ ์ˆœํšŒํ•˜๋ฉฐ ์…€์— ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ถ”์ถœํ•˜๊ธฐ python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์ง์ ‘์ ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ํŒŒ์›Œํฌ์ธํŠธ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด๋ฏธ์ง€์˜ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ถ”์ถœํ•˜๋Š” ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. from pptx import Presentation prs = Presentation("presentation_with_image.pptx") # ์ „์ฒด ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ˆœํšŒ for slide_number, slide in enumerate(prs.slides): # ๊ฐ ์Šฌ๋ผ์ด๋“œ์—์„œ ๋ชจ๋“  ๊ฐœ์ฒด(shape)๋ฅผ ์ˆœํšŒ for shape in slide.shapes: # ํ˜„์žฌ ๊ฐœ์ฒด๊ฐ€ ์ด๋ฏธ์ง€์˜ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์ด๋ฏธ์ง€์ผ ๊ฒฝ์šฐ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ if hasattr(shape, "image"): # ์ด๋ฏธ์ง€์˜ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ์™€ ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ ธ์™€ ๋ณ€์ˆ˜์— ํ• ๋‹น image_stream = shape.image.blob image_format = shape.image.ext # ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์ƒ์„ฑ with open(f"slide_{slide_number}_image.{image_format}", "wb") as img_file: img_file.write(image_stream) ์ด๋ฏธ์ง€๋ฅผ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € ์ „์ฒด ์Šฌ๋ผ์ด๋“œ์™€ ๋ชจ๋“  ๊ฐœ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ hasattr ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ๊ฐœ์ฒด๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ, image.blob๊ณผ image.ext๋กœ ๊ฐ๊ฐ ์ด๋ฏธ์ง€์˜ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ์™€ ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์™€์„œ ํŒŒ์ผ๋กœ ์ €์žฅํ•  ๋•Œ ์›๋ž˜์˜ ํ™•์žฅ์ž๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํ™•์žฅ์ž๋กœ ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๊ทธ๋Ÿด ๊ฒฝ์šฐ ์ผ๋ถ€<NAME>์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€๊ฐ€ ๊นจ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ๋Š” ์›๋ž˜์˜ ํ™•์žฅ์ž๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ํ™•์žฅ์ž๋„ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ๊ฐ€์ง€๊ณ  ์˜จ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ €์žฅํ•  ๋•Œ ํŒŒ์ผ์˜ ํ™•์žฅ์ž๋Š” ์›๋ž˜ ํ™•์žฅ์ž๋กœ ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ, ์‹คํ–‰ ๊ฒฝ๋กœ์— 'slide_0_image.png'ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฐจํŠธ ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๊ธฐ python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์Šฌ๋ผ์ด๋“œ์— ํฌํ•จ๋œ ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋Š” ์ฐจํŠธ๋ฅผ ์ฐพ๊ณ , ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. from pptx import Presentation prs = Presentation("presentation_with_chart.pptx") # ์ „์ฒด ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ˆœํšŒ for slide_number, slide in enumerate(prs.slides): # ๊ฐ ์Šฌ๋ผ์ด๋“œ ๋‚ด์˜ ๋ชจ๋“  ๊ฐœ์ฒด๋ฅผ ์ˆœํšŒ for shape in slide.shapes: # if ๋ฌธ๊ณผ hasattr ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ๊ฐœ์ฒด๊ฐ€ ์ฐจํŠธ์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์ฐจํŠธ์ผ ๊ฒฝ์šฐ chart์— ํ• ๋‹น if hasattr(shape, "chart"): chart = shape.chart # ์ฐจํŠธ ๋‚ด์˜ ๋ชจ๋“  ์‹œ๋ฆฌ์ฆˆ๋ฅผ ์ˆœํšŒ for series in chart.series: print(f"Series title: {series.name}") # ์‹œ๋ฆฌ์ฆˆ ์ด๋ฆ„์„ ์ถœ๋ ฅ # ์ฒซ ๋ฒˆ์งธ ํ”Œ๋กฏ(plot)์˜ ์นดํ…Œ๊ณ ๋ฆฌ์™€ ๊ฐ’(์ฆ‰, x ๊ฐ’๊ณผ y ๊ฐ’)์˜ ์Œ์„ ์ˆœํšŒ for x_val, y_val in zip(chart.plots[0].categories, series.values): print(f"Data point: x={x_val}, y={y_val}") # ๊ฒฐ๊ด๊ฐ’ Series title: ์ž”๋ฅ˜์˜ค์—ผ๋„ Data point: x=A ์ œํ’ˆ, y=9.2 Data point: x=B ์ œํ’ˆ, y=11.4 Data point: x=C ์ œํ’ˆ, y=16.7 ์œ„ ์ฝ”๋“œ ์˜ˆ์ œ๋ฅผ ์ฐจํŠธ๋ฅผ ์ฝ๋Š” ๋ถ€๋ถ„์„ ์ค‘์ ์œผ๋กœ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. for slide_number, slide in enumerate(prs.slides): for shape in slide.shapes: # if ๋ฌธ๊ณผ hasattr ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ๊ฐœ์ฒด๊ฐ€ ์ฐจํŠธ์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์ฐจํŠธ์ผ ๊ฒฝ์šฐ chart์— ํ• ๋‹น if hasattr(shape, "chart"): chart = shape.chart ๊ฐ ์Šฌ๋ผ์ด๋“œ๋ฅผ ๋Œ๋ฉฐ ๋ชจ๋“  ๊ฐœ์ฒด ์ค‘ ์ฐจํŠธ ๊ฐ์ฒด๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ๊ฐœ์ฒด๊ฐ€ ์ฐจํŠธ์ผ ๊ฒฝ์šฐ ๋ณ€์ˆ˜ chart์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. # ์ฐจํŠธ ๋‚ด์˜ ๋ชจ๋“  ์‹œ๋ฆฌ์ฆˆ๋ฅผ ์ˆœํšŒ for series in chart.series: print(f"Series title: {series.name}") # ์‹œ๋ฆฌ์ฆˆ ์ด๋ฆ„์„ ์ถœ๋ ฅ ์ฐจํŠธ์˜ ๋ชจ๋“  ์‹œ๋ฆฌ์ฆˆ๋ฅผ ๋Œ๋ฉฐ ์‹œ๋ฆฌ์ฆˆ์˜ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋Š” ์‹œ๋ฆฌ์ฆˆ๊ฐ€ ํ•˜๋‚˜์˜€์ง€๋งŒ, ํ•œ ์ฐจํŠธ์— ์‹œ๋ฆฌ์ฆˆ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ๋„ ์žˆ์œผ๋ฏ€๋กœ ์‹œ๋ฆฌ์ฆˆ๋ฅผ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค. # ์ฒซ ๋ฒˆ์งธ ํ”Œ๋กฏ(plot)์˜ ์นดํ…Œ๊ณ ๋ฆฌ์™€ ๊ฐ’(์ฆ‰, x ๊ฐ’๊ณผ y ๊ฐ’)์˜ ์Œ์„ ์ˆœํšŒ for x_val, y_val in zip(chart.plots[0].categories, series.values): print(f"Data point: x={x_val}, y={y_val}") ์ฒซ ๋ฒˆ์งธ ํ”Œ๋กฏ์—์„œ ์นดํ…Œ๊ณ ๋ฆฌ์™€ ๊ฐ’(์ฆ‰, x ๊ฐ’๊ณผ y ๊ฐ’)์˜ ์Œ์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ทธ ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ”Œ๋กฏ์ด๋ž€ ์ฐจํŠธ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ๊ฐ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ์˜์—ญ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ ํŒŒ์ผ์—์„œ๋Š” ์ˆ˜์ง ๋ง‰๋Œ€ ์ฐจํŠธ๋งŒ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ”Œ๋กฏ์ด ํ•˜๋‚˜์ง€๋งŒ, ํ•˜๋‚˜์˜ ์ฐจํŠธ์— ๋ผ์ธ ํ”Œ๋กฏ๊ณผ ๋ฐ” ํ”Œ๋กฏ์„ ๋™์‹œ์— ํ‘œํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ์ฒ˜๋Ÿผ ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ํ”Œ๋กฏ์ด ํ•˜๋‚˜์˜ ์ฐจํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”Œ๋กฏ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ๊ฐ ํ”Œ๋กฏ์„ ์ˆœํšŒํ•˜์—ฌ ๊ฐ๊ฐ์˜ ํ”Œ๋กฏ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. chart.plots[0].categories์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ํ”Œ๋กฏ์˜ ์นดํ…Œ๊ณ ๋ฆฌ, ์ฆ‰ x ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ณ , series.values๋กœ ๋ฐ์ดํ„ฐ ๊ฐ’, ์ฆ‰, y ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์˜ต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ฐ€์ ธ์˜จ x ๊ฐ’๊ณผ y ๊ฐ’์„ zip ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ์Œ์œผ๋กœ ๋ฌถ์–ด ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค. ์ถ”์ถœ๋œ ๊ฒฐ๊ด๊ฐ’์„ ํ†ตํ•ด ์˜ˆ์ œ ํŒŒ์ผ์—๋Š” ํ•˜๋‚˜์˜ ์ฐจํŠธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹œ๋ฆฌ์ฆˆ์˜ ์ด๋ฆ„์œผ๋กœ ๋ดค์„ ๋•Œ ์ œํ’ˆ๋ณ„ ์ž”๋ฅ˜์˜ค์—ผ๋„๋ฅผ ์ฐจํŠธ์—์„œ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, A ์ œํ’ˆ์˜ ์ž”๋ฅ˜ ์˜ค์—ผ๋„ ์ˆ˜์น˜๋Š” 9.2, B ์ œํ’ˆ์€ 11.4, C ์ œํ’ˆ์€ 16.7์ด๋ผ๋Š” ๊ฒƒ์„ ๊ฐ„๋žตํ•˜๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํŒŒ์›Œํฌ์ธํŠธ์— ํฌํ•จ๋œ ์ฐจํŠธ๋ฅผ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 06-04. ์‹ค์ „! ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ž๋ฃŒ ์ž๋™ ์ƒ์„ฑํ•˜๊ธฐ ์—‘์…€ ๋ฐ์ดํ„ฐ๋กœ ํŒŒ์›Œํฌ์ธํŠธ ๋งŒ๋“ค๊ธฐ ์•ž์—์„œ ํ•™์Šตํ•œ ์ฝ”๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋‹ค๋ฅธ ๋ฌธ์„œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๋™ํ•˜์—ฌ ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ์„ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” '์ œํ’ˆ๋ณ„ ํŒ๋งค๋‚ด์—ญ. xlsx'์ด๋ผ๋Š” ์—‘์…€ ํŒŒ์ผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์™€์„œ ํŒŒ์›Œํฌ์ธํŠธ๋กœ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ์ž๋ฃŒ๋ฅผ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ๋ฅผ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. '์ œํ’ˆ๋ณ„ ํŒ๋งค๋‚ด์—ญ. xlsx' ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด ์—‘์…€ ํŒŒ์ผ์—๋Š” ๊ฐ๊ฐ A/B/C ์ œํ’ˆ ์‹œํŠธ๊ฐ€ ์žˆ๊ณ  ๊ฐ ์‹œํŠธ์—๋Š” 1์›”๋ถ€ํ„ฐ 6์›”๊นŒ์ง€ ํŒ๋งค๋Ÿ‰๊ณผ ํŒ๋งค ๊ธˆ์•ก์ด ํ‘œ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ œํ’ˆ๋ณ„ ํŒ๋งค ๋‚ด์—ญ์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ํŒŒ์›Œํฌ์ธํŠธ์— ์ œํ’ˆ๋ณ„๋กœ ์Šฌ๋ผ์ด๋“œ๋ฅผ ๋งŒ๋“ค๊ณ , ์›”๋ณ„ ํŒ๋งค๋‚ด์—ญ์ด ์ •๋ฆฌ๋œ ํ‘œ์™€ ์›”๋ณ„ ํŒ๋งค๋Ÿ‰์„ ํ•œ๋ˆˆ์— ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š” ๋ง‰๋Œ€ ์ฐจํŠธ๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ „์ฒด ์ œํ’ˆ์— ๋Œ€ํ•œ ํŒ๋งค ์ถ”์ด๋„ ๋ผ์ธ ํ”Œ๋กฏ์œผ๋กœ ํ‘œํ˜„ํ•˜์—ฌ ํ•˜๋‚˜์˜ ์Šฌ๋ผ์ด๋“œ์— ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์˜ˆ์ œ ์ฝ”๋“œ์™€ ์‹ค์ œ ์ฝ”๋“œ๋กœ ์ƒ์„ฑํ•œ ํŒŒ์ผ์˜ ์‹คํ–‰ ํ™”๋ฉด์ž…๋‹ˆ๋‹ค. from openpyxl import load_workbook from pptx import Presentation from pptx.util import Inches from pptx.enum.chart import XL_CHART_TYPE from pptx.chart.data import CategoryChartData # ์—‘์…€ ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook('์ œํ’ˆ๋ณ„ ํŒ๋งค๋‚ด์—ญ. xlsx') sheet_names = wb.sheetnames # ํŒŒ์›Œํฌ์ธํŠธ ๊ฐ์ฒด ์ƒ์„ฑ prs = Presentation() # ํ‘œ์ง€ ์Šฌ๋ผ์ด๋“œ ์ถ”๊ฐ€ slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "์ƒ๋ฐ˜๊ธฐ ์ œํ’ˆ๋ณ„ ํŒ๋งค ํ˜„ํ™ฉ ๋ถ„์„" # ์ œํ’ˆ๋ณ„ ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋”•์…”๋„ˆ๋ฆฌ sales_data = {} # ๊ฐ ์ œํ’ˆ๋ณ„ ์Šฌ๋ผ์ด๋“œ ์ƒ์„ฑ for sheet_name in sheet_names: sheet = wb[sheet_name] # ์ œํ’ˆ ์ „์ฒด ํŒ๋งค ๋ฐ์ดํ„ฐ ์ €์žฅ (ํŒ๋งค์›”, ํŒ๋งค๋Ÿ‰, ํŒ๋งค๊ธˆ์•ก) data = [list(map(str, row)) for row in sheet.iter_rows(values_only=True)] # ์ œํ’ˆ์˜ ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ €์žฅ sales_data[sheet_name] = [list(map(int, row[1:2])) for row in data[1:]] # ์Šฌ๋ผ์ด๋“œ ์ถ”๊ฐ€ slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = f"{sheet_name}" # ํ‘œ ์ถ”๊ฐ€ rows, cols = len(data), len(data[0]) table = slide.shapes.add_table(rows+1, cols, Inches(0.3), Inches(2), Inches(4.3), Inches(3.8)).table # "์›”๋ณ„ ํŒ๋งค ํ˜„ํ™ฉ" ์ถ”๊ฐ€ table.cell(0, 0).text = "์›”๋ณ„ ํŒ๋งค ํ˜„ํ™ฉ" table.cell(0, 0).merge(table.cell(0, cols - 1)) # ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์— ์ถ”๊ฐ€ for row_idx, row_val in enumerate(data): for col_idx, val in enumerate(row_val): table.cell(row_idx+1, col_idx).text = str(val) # ์ฐจํŠธ ๋ฐ์ดํ„ฐ ์„ค์ • chart_data = CategoryChartData() chart_data.categories = [row[0] for row in data[1:]] chart_data.add_series('์ƒ๋ฐ˜๊ธฐ ํŒ๋งค๋Ÿ‰ ๋ณ€ํ™”', (row[1] for row in data[1:])) # ์ฐจํŠธ ์ถ”๊ฐ€ x, y, cx, cy = Inches(5), Inches(1.8), Inches(4.5), Inches(4.2) chart = slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data).chart # ์ฐจํŠธ ์„ค์ • chart.has_legend = False chart.plots[0].has_data_labels = True # ์ „์ฒด ํŒ๋งค ์ถ”์ด ์Šฌ๋ผ์ด๋“œ ์ƒ์„ฑ slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "์ œํ’ˆ๋ณ„ ํŒ๋งค ์ถ”์ด" # ์ „์ฒด ํŒ๋งค ์ถ”์ด ์ฐจํŠธ ๋ฐ์ดํ„ฐ ์„ค์ • chart_data = CategoryChartData() chart_data.categories = [row[0] for row in data[1:]] for sheet_name, sales in sales_data.items(): chart_data.add_series(sheet_name, (sale[0] for sale in sales)) # ์ „์ฒด ํŒ๋งค ์ถ”์ด ์ฐจํŠธ ์ถ”๊ฐ€ x, y, cx, cy = Inches(1), Inches(2), Inches(8), Inches(4.5) chart = slide.shapes.add_chart(XL_CHART_TYPE.LINE, x, y, cx, cy, chart_data).chart # ์ฐจํŠธ ์„ค์ • chart.has_legend = True chart.legend.include_in_layout = False chart.category_axis.has_major_gridlines = False # ๋งˆ์ง€๋ง‰ ์Šฌ๋ผ์ด๋“œ ์ถ”๊ฐ€ slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค." # ๊ฒฐ๊ณผ ์ €์žฅ prs.save('์ƒ๋ฐ˜๊ธฐ_์ œํ’ˆ๋ณ„_ํŒ๋งค ํ˜„ํ™ฉ_๋ถ„์„. pptx') '์ƒ๋ฐ˜๊ธฐ_์ œํ’ˆ๋ณ„_ํŒ๋งค ํ˜„ํ™ฉ_๋ถ„์„. pptx' ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด ์—‘์…€๊ณผ ํŒŒ์›Œํฌ์ธํŠธ ๋‘ ๊ฐ€์ง€ ์‘์šฉํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•˜๊ณ , ํŒŒ์›Œํฌ์ธํŠธ์— ํ‘œ, ์ฐจํŠธ ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๊ฐ€ ๋น„๊ต์  ๊ธธ๊ณ  ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์‹คํ–‰ ์ฝ”๋“œ๋ฅผ ์ž์„ธํžˆ ๋‚˜๋ˆ ์„œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ํ•„์š”ํ•œ ๋ชจ๋“ˆ๊ณผ ํด๋ž˜์Šค ๋ถˆ๋Ÿฌ์˜ค๊ธฐ from openpyxl import load_workbook from pptx import Presentation from pptx.util import Inches from pptx.enum.chart import XL_CHART_TYPE from pptx.chart.data import CategoryChartData ์—‘์…€ ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ openpyxl ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํŒŒ์›Œํฌ์ธํŠธ ํŒŒ์ผ ์ž‘์—…์„ ์œ„ํ•œ python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ํŒŒ์›Œํฌ์ธํŠธ์—์„œ ๊ฐ์ฒด์˜ ํฌ๊ธฐ๋ฅผ ์ธ์น˜๋กœ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜ Inches์™€ ์ฐจํŠธ์˜ ์œ ํ˜•(XL_CHART_TYPE)์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์—ด๊ฑฐํ˜•, ์ฐจํŠธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” CategoryChartData ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€๋กœ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. 2) ์—‘์…€ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ & ํŒŒ์›Œํฌ์ธํŠธ ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ # ์—‘์…€ ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ wb = load_workbook('์ œํ’ˆ๋ณ„ ํŒ๋งค๋‚ด์—ญ. xlsx') sheet_names = wb.sheetnames # ํŒŒ์›Œํฌ์ธํŠธ ๊ฐ์ฒด ์ƒ์„ฑ prs = Presentation() # ํ‘œ์ง€ ์Šฌ๋ผ์ด๋“œ ์ถ”๊ฐ€ slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "์ƒ๋ฐ˜๊ธฐ ์ œํ’ˆ๋ณ„ ํŒ๋งค ํ˜„ํ™ฉ ๋ถ„์„" ๋จผ์ € ์—‘์…€ ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด load_workbook์œผ๋กœ ์—‘์…€ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” ์—‘์…€ ํŒŒ์ผ์— ์—ฌ๋Ÿฌ ์‹œํŠธ๊ฐ€ ์กด์žฌํ•˜๊ณ  ์‹œํŠธ๋ณ„๋กœ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๊ธฐ ๋•Œ๋ฌธ์— wb.sheetnames๋กœ ์ „์ฒด ์‹œํŠธ๋ช…์„ ๊ฐ€์ง€๊ณ  ์™€์„œ sheet_names ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ ํŒŒ์›Œํฌ์ธํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด Presentation()์œผ๋กœ ํŒŒ์›Œํฌ์ธํŠธ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ‘œ์ง€ ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋ณธ ์˜ˆ์ œ์—์„œ๋Š” ํ‘œ์ง€์— ์ œ๋ชฉ ํ…์ŠคํŠธ๋งŒ ๋„ฃ๊ธฐ ์œ„ํ•ด ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋ ˆ์ด์•„์›ƒ(prs.slide_layouts[0])์œผ๋กœ ์ง€์ •ํ•˜์—ฌ prs.slides.add_slide(slide_layout)๋กœ ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ ์ œ๋ชฉ ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด slide.shapes.title.text๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ "์ƒ๋ฐ˜๊ธฐ ์ œํ’ˆ๋ณ„ ํŒ๋งค ํ˜„ํ™ฉ ๋ถ„์„"์ด๋ผ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. 3) ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ - 1 (๋นˆ ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ๊ณผ ์—‘์…€ ์ „์ฒด ์‹œํŠธ ์ˆœํšŒํ•˜๊ธฐ) # ์ œํ’ˆ๋ณ„ ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋”•์…”๋„ˆ๋ฆฌ sales_data = {} # ์—‘์…€ ์ „์ฒด ์‹œํŠธ๋ฅผ ์ˆœํšŒ for sheet_name in sheet_names: sheet = wb[sheet_name] ๊ฐ€์ €์˜จ ์—‘์…€ ํŒŒ์ผ์—์„œ ์ œํ’ˆ๋ณ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ {}๋ฅผ ์ƒ์„ฑํ•˜์—ฌ sales_data ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์—‘์…€ ํŒŒ์ผ์—๋Š” ์ œํ’ˆ๋ณ„๋กœ ์‹œํŠธ๊ฐ€ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์œผ๋ฉฐ ํŒŒ์›Œํฌ์ธํŠธ๋„ ์ œํ’ˆ๋ณ„๋กœ ๊ฐ๊ฐ์˜ ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•˜๋‚˜์˜ ์‹œํŠธ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ๊นŒ์ง€๋ฅผ ํ•˜๋‚˜์˜ ๊ณผ์ •์œผ๋กœ ๋ณด๋ฉด ์ด ์„ธ ๋ฒˆ์˜ ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„ธ ๋ฒˆ์˜ ๊ณผ์ •์€ ์‹œํŠธ ๋ฐ์ดํ„ฐ๋งŒ ๋‹ฌ๋ผ์งˆ ๋ฟ ๊ณผ์ • ์ž์ฒด๋Š” ๋ชจ๋‘ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— for ๋ฌธ์œผ๋กœ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. for sheet_name in sheet_names:๋กœ ์ „์ฒด ์‹œํŠธ๋ช… ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ ์‹œํŠธ ์ด๋ฆ„์„ ์ˆœํšŒํ•˜์—ฌ sheet_name ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. sheet = wb[sheet_name]๋กœ ํ˜„์žฌ ์ˆœํšŒ ์ค‘์ธ ์‹œํŠธ๋ช…์„ ์‚ฌ์šฉํ•˜์—ฌ ์›Œํฌ๋ถ('wb')์—์„œ ํ•ด๋‹น ์‹œํŠธ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. 4) ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ - 2 (์—‘์…€ ์‹œํŠธ๋ณ„ ์ „์ฒด ๋ฐ์ดํ„ฐ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•˜๊ธฐ) # ์ œํ’ˆ ์ „์ฒด ํŒ๋งค ๋ฐ์ดํ„ฐ ์ €์žฅ (ํŒ๋งค์›”, ํŒ๋งค๋Ÿ‰, ํŒ๋งค๊ธˆ์•ก) data = [list(map(str, row)) for row in sheet.iter_rows(values_only=True)] ์ด ๋ผ์ธ์—์„œ๋Š” iter_rows๋กœ ํ˜„์žฌ ์‹œํŠธ์˜ ๋ชจ๋“  ํ–‰์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ํ–‰์˜ ๊ฐ ์…€ ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. map(str, row) ์ฝ”๋“œ๋Š” row์˜ ๊ฐ ์š”์†Œ์— str ํ•จ์ˆ˜(๋ฌธ์ž์—ด ๋ณ€ํ™˜ ํ•จ์ˆ˜)๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ list() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ map ๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณ€ํ™˜๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ์‹ธ๋Š” ๋” ํฐ ๋ฆฌ์ŠคํŠธ(์ด์ค‘ ๋ฆฌ์ŠคํŠธ)๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด์ œ์‹œ๋ฒ•(list comprehension)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹œํŠธ์—์„œ ํ–‰์„ ์ˆœํšŒํ•  ๋•Œ ์ˆ˜์‹์ด ์žˆ์„ ๊ฒฝ์šฐ ์ˆ˜์‹์ด ๊ณ„์‚ฐ๋œ ํ›„์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์˜ค๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด values_only=True๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ฐ€์ ธ์˜จ ํ–‰๋ณ„ ๋ฐ์ดํ„ฐ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋Š” data ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์˜€์œผ๋ฉฐ, ๋’ค์— ๋งŒ๋“ค ์ œํ’ˆ๋ณ„ ์Šฌ๋ผ์ด๋“œ์—์„œ ํ‘œ์™€ ๋ง‰๋Œ€ ์ฐจํŠธ์˜ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. A ์ œํ’ˆ ์‹œํŠธ๋ฅผ ์ˆœํšŒ ํ›„ print(data) ์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ [['ํŒ๋งค์›”', 'ํŒ๋งค๋Ÿ‰', 'ํŒ๋งค๊ธˆ์•ก'], ['1์›”', '70', '140000'], ['2์›”', '65', '130000'], ['3์›”', '80', '160000'], ['4์›”', '75', '150000'], ['5์›”', '82', '164000'], ['6์›”', '73', '146000']] ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด data ๋ณ€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์œ„์—์„œ ์ถœ๋ ฅ๋œ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋Š” 'A ์ œํ’ˆ'์‹œํŠธ๋ฅผ ์ˆœํšŒํ•˜์—ฌ ๊ฐ€์ง€๊ณ  ์˜จ data ๋ณ€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์˜€์„ ๋•Œ ์ฝ˜์†”์— ๋ฐ˜ํ™˜๋œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. 5) ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ - 3 (์—‘์…€ ์‹œํŠธ๋ณ„ ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ๋งŒ ๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅํ•˜๊ธฐ) # ์ œํ’ˆ์˜ ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ €์žฅ sales_data[sheet_name] = [list(map(int, row[1:2])) for row in data[1:]] ์ด ๋ผ์ธ์—์„œ๋Š” ๊ฐ ํ–‰์˜ ๋‘ ๋ฒˆ์งธ ์—ด(์ธ๋ฑ์Šค 1)์˜ ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ œํ’ˆ๋ช…์„ ํ‚ค๋กœ ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋„ ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด์ œ์‹œ๋ฒ•(list comprehension)์ด ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. data[1:]๋กœ ๋ฐ”๋กœ ์œ„ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ data ๋ฆฌ์ŠคํŠธ์—์„œ ํ—ค๋” ํ–‰์ธ ์ฒซ ๋ฒˆ์งธ ํ–‰(์ธ๋ฑ์Šค 0)์„ ์ œ์™ธํ•œ ๋ชจ๋“  ํ–‰์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ฐ€์ ธ์˜จ ๋ชจ๋“  ํ–‰์„ for ๋ฌธ์œผ๋กœ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ row์— ๋Œ€ํ•ด ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ( for row in data[1:]) row[1:2]๋กœ ๊ฐ row์—์„œ ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋งŒ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์•ž์— ๋ฌธ๋ฒ• ๋ถ€๋ถ„์—์„œ ํ•™์Šตํ•œ ๊ฒƒ์ฒ˜๋Ÿผ, ํŒŒ์ด์ฌ์—์„œ list[a:b]๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ a ์ธ๋ฑ์Šค๋ถ€ํ„ฐ b-1 ์ธ๋ฑ์Šค๊นŒ์ง€์˜ ๋ถ€๋ถ„์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” 1๋ถ€ํ„ฐ 2๋ฏธ๋งŒ์ด๋ฏ€๋กœ ์‹ค์ œ๋กœ๋Š” 1๋ฒˆ ์ธ๋ฑ์Šค๋งŒ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฒฐ๊ณผ๋Š” ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜๋˜๋ฏ€๋กœ ์›์†Œ๊ฐ€ ํ•˜๋‚˜์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. map ํ•จ์ˆ˜๋กœ row[1:2]๋กœ ์–ป์€ ํ•˜๋‚˜์˜ ์›์†Œ๋ฅผ ๊ฐ–๋Š” ๋ฆฌ์ŠคํŠธ์— int ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ํ•ด๋‹น ์›์†Œ(๋ฌธ์ž์—ด)๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๋ฐ˜ํ™˜ํ•œ ๋งต ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜(list()) ํ•˜์—ฌ ์›์†Œ ํ•˜๋‚˜๋ฅผ ๊ฐ–๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณผ์ •์„ data ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ํ–‰์„ ์ œ์™ธํ•œ ๋ชจ๋“  ํ–‰์„ ์ˆœํšŒํ•˜๋ฉฐ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์€ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ฒฐ๊ณผ์ ์œผ๋กœ 1์—ด์— ์žˆ๋˜ ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ๊ฐ€ ์ •์ˆ˜ ํ˜•ํƒœ์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜๋˜์–ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ๋œ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋ฅผ sales_data ๋”•์…”๋„ˆ๋ฆฌ์— ํ˜„์žฌ ์ฒ˜๋ฆฌ ์ค‘์ธ ์‹œํŠธ๋ช…, ์ฆ‰, ์ œํ’ˆ๋ช…์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ ์‹œํŠธ์˜ ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ ๊ฐ’์ด ์ •์ˆ˜์ธ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ sales_data ๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅํ•˜๋Š” ๊ณผ์ •์ด ์™„๋ฃŒ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ sales_data ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๊ฐ ์ œํ’ˆ๋ช…์ด ํ‚ค์ด๊ณ  ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ’์œผ๋กœ ํ•˜๋Š” ํ•ญ๋ชฉ๋“ค๋กœ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค. ์ „์ฒด ์‹œํŠธ ์ˆœํšŒ ํ›„ ์ตœ์ข… ์ƒ์„ฑ๋œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ print(sales_data)๋กœ ์ถœ๋ ฅํ•œ ๊ฒฐ๊ด๊ฐ’ {'A ์ œํ’ˆ': [[70], [65], [80], [75], [82], [73]], 'B ์ œํ’ˆ': [[40], [48], [45], [42], [34], [37]], 'C ์ œํ’ˆ': [[33], [50], [44], [53], [62], [48]]} for ๋ฌธ์œผ๋กœ ์ „์ฒด ์‹œํŠธ๋ฅผ ๋ชจ๋‘ ์ˆœํšŒํ•œ ํ›„ ์ตœ์ข… ์ƒ์„ฑ๋œ sales_data ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ print()๋กœ ์ถœ๋ ฅํ•œ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์ด ๋ฐ˜ํ™˜๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜๋Š” ์ด์œ ๋Š” ๋’ค์— 5๋ฒˆ์งธ ์Šฌ๋ผ์ด๋“œ์—์„œ ์ด ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ œํ’ˆ๋ณ„ ํŒ๋งค๋Ÿ‰ ๋ณ€ํ™” ์ถ”์ด๋ฅผ ๋ผ์ธ ์ฐจํŠธ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. 6) ์ œํ’ˆ๋ณ„ ์Šฌ๋ผ์ด๋“œ ์ถ”๊ฐ€ํ•˜๊ณ  ํ‘œ ์ƒ์„ฑํ•˜๊ธฐ # ์Šฌ๋ผ์ด๋“œ ์ถ”๊ฐ€ slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = f"{sheet_name}" # ํ‘œ ์ถ”๊ฐ€ rows, cols = len(data), len(data[0]) table = slide.shapes.add_table(rows+1, cols, Inches(0.3), Inches(2), Inches(4.3), Inches(3.8)).table ์ œํ’ˆ๋ณ„ ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์Šฌ๋ผ์ด๋“œ ๋ ˆ์ด์•„์›ƒ ๋ฆฌ์ŠคํŠธ ์ค‘ 6๋ฒˆ์งธ ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ์„ ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์‹œํŠธ๋ช…(sheet_name)์„ ๊ฐ€์ ธ์™€์„œ ์Šฌ๋ผ์ด๋“œ์˜ ์ œ๋ชฉ์œผ๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ์„ ํƒ๋œ ์ œํ’ˆ(์‹œํŠธ๋ช…)์— ํ•ด๋‹นํ•˜๋Š” ํŒ๋งค ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ๋กœ ์ž…๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ํ‘œ๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ํŒ๋งค ๋ฐ์ดํ„ฐ๋ฅผ ํ–‰๋ณ„๋กœ ๊ฐ€์ง€๊ณ  ์™€์„œ data์— ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ํ–‰์˜ ์ˆ˜๋Š” data์˜ ๊ธธ์ด(len)๋กœ ์ง€์ •ํ•˜๊ณ , ์—ด์€ data์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ์˜ ๊ธธ์ด๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. data์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋Š” ์›๋ณธ ์‹œํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ–‰, ์ฆ‰ ํ—ค๋” ํ–‰์ด ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ—ค๋” ํ–‰์˜ ๊ธธ์ด๋กœ ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ์ง€์ •ํ•œ ํ–‰๊ณผ ์—ด์˜ ๊ฐœ์ˆ˜์™€ ํ‘œ์˜ ํฌ๊ธฐ ๋ฐ ์œ„์น˜์˜ ๊ฐ’์„ ์ „๋‹ฌํ•˜์—ฌ add_table๋กœ ํ‘œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ํ–‰์˜ ๊ฐœ์ˆ˜๊ฐ€ rows+1๋กœ ํ–‰์ด ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€๋˜์—ˆ๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” ๋ฐ”๋กœ ๋‹ค์Œ ์ฝ”๋“œ ๋ผ์ธ์— ๋‚˜์˜ค๋“ฏ ํ‘œ์˜ ์ฒซ ํ–‰์— ํ‘œ์˜ ์ œ๋ชฉ์„ ๋„ฃ์–ด์ฃผ๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. 7) ์ƒ์„ฑ๋œ ํ‘œ์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•˜๊ธฐ # "์›”๋ณ„ ํŒ๋งค ํ˜„ํ™ฉ" ์ถ”๊ฐ€ table.cell(0, 0).text = "์›”๋ณ„ ํŒ๋งค ํ˜„ํ™ฉ" table.cell(0, 0).merge(table.cell(0, cols - 1)) # ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์— ์ถ”๊ฐ€ for row_idx, row_val in enumerate(data): for col_idx, val in enumerate(row_val): table.cell(row_idx+1, col_idx).text = str(val) ์Šฌ๋ผ์ด๋“œ์— ์ƒ์„ฑ๋œ ํ‘œ์˜ ์ฒซ ๋ฒˆ์งธ ์…€(0, 0)์— "์›”๋ณ„ ํŒ๋งค ํ˜„ํ™ฉ"์ด๋ผ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ–‰์˜ ๋ชจ๋“  ์…€์„ ๋ณ‘ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์…€์„ ๋ณ‘ํ•ฉํ•  ๋•Œ๋Š” table.cell()๋กœ ๋ณ‘ํ•ฉ์„ ์‹œ์ž‘ํ•  ์…€๊ณผ ๋๋‚ผ ์…€์„ ๊ฐ๊ฐ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฒซ ํ–‰์˜ ์ฒซ ์—ด์— ์œ„์น˜ํ•œ ์…€(table.cell(0, 0))๋ถ€ํ„ฐ ์ฒซ ํ–‰์˜ ๋งˆ์ง€๋ง‰ ์—ด(table.cell(0, cols - 1))๊นŒ์ง€ ๋ณ‘ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ˆซ์ž๋กœ ์ง์ ‘ ์ž…๋ ฅํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์œ„์™€ ๊ฐ™์ด 'cols - 1'์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ . merge(...) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ด ๋ฉ”์„œ๋“œ๋Š” ๋ณ‘ํ•ฉ์„ ์‹œ์ž‘ํ•  ์…€ ๊ฐ์ฒด์—์„œ ํ˜ธ์ถœ๋˜๊ณ  ๊ทธ ์ธ์ž๋กœ ๋ณ‘ํ•ฉ๋  ๋งˆ์ง€๋ง‰ ์…€ ๊ฐ์ฒด๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์‹œ์ž‘ ์…€๊ณผ ๋งˆ์ง€๋ง‰ ์…€ ์‚ฌ์ด์— ์žˆ๋Š” ๋ชจ๋“  ์…€์„ ๋ณ‘ํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ํฐ ์…€์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๊ฐ€ ์ž…๋ ฅ๋œ ํ‘œ์˜ ์ฒซ ํ–‰์„ ์ œ์™ธํ•˜๊ณ , ํ‘œ์˜ ๋‘ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ํ–‰๊นŒ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด์ค‘ for ๋ฌธ์œผ๋กœ ํ–‰๊ณผ ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ data์˜ ์›์†Œ๋ฅผ ํ‘œ์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ๋„ฃ์„ ๋•Œ๋Š” str๋กœ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. 8) ์ฐจํŠธ ๋ฐ์ดํ„ฐ ์„ค์ •ํ•˜๊ธฐ # ์ฐจํŠธ ๋ฐ์ดํ„ฐ ์„ค์ • chart_data = CategoryChartData() chart_data.categories = [row[0] for row in data[1:]] chart_data.add_series('์ƒ๋ฐ˜๊ธฐ ํŒ๋งค๋Ÿ‰ ๋ณ€ํ™”', (row[1] for row in data[1:])) ์›”๋ณ„ ํŒ๋งค๋Ÿ‰ ์ฐจํŠธ ์ƒ์„ฑ์„ ์œ„ํ•ด ์ฐจํŠธ์— ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฐจํŠธ ๋ฐ์ดํ„ฐ ์ •์˜๋ฅผ ์œ„ํ•ด CategoryChartData() ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ chart_data๋ผ๋Š” ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ๋Š” chart_data.categories๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฐจํŠธ์—๋Š” ๊ฐ’ ๋ฐ์ดํ„ฐ๋งŒ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— data ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ–‰(ํ—ค๋”ํ–‰)์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ํ–‰์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ํ–‰์˜ ์ฒซ ๋ฒˆ์งธ ์—ด์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฒซ ๋ฒˆ์งธ ์—ด์€ ํŒ๋งค์›”์„ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ, ์›”๋ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจํŠธ์— ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด x ์ถ•์˜ ๊ฐ’์œผ๋กœ ๊ฐ ํŒ๋งค์›”์„ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์œผ๋กœ๋Š” add_series ๋ฉ”์„œ๋“œ๋กœ ์‹œ๋ฆฌ์ฆˆ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. add_series๋Š” ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ์‹œ๋ฆฌ์ฆˆ์˜ ์ด๋ฆ„์ด๋ฉฐ ๋‘ ๋ฒˆ์งธ์˜ ์ธ์ž๋Š” ๋ฐ์ดํ„ฐ ์‹œ๋ฆฌ์ฆˆ์˜ ๊ฐ’๋“ค(๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๋“ฑ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด)์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ(generator expression)์„ ์‚ฌ์šฉํ•ด data ๋ฆฌ์ŠคํŠธ์˜ ๋‘ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ ๋ชจ๋“  ํ–‰์„ ์ˆœํšŒํ•˜๋ฉฐ ๋‘ ๋ฒˆ์งธ ์—ด์˜ ๊ฐ’(=ํŒ๋งค๋Ÿ‰)์„ ๊ฐ€์ ธ์™€ ๋ฐ์ดํ„ฐ ์‹œ๋ฆฌ์ฆˆ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ๋ฆฌ์ŠคํŠธ, ์„ธํŠธ, ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜๋กœ, ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ ์กฐ๊ฑด์ œ์‹œ๋ฒ•(list comprehension)๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ๋Œ€๊ด„ํ˜ธ [] ๋Œ€์‹  ๊ด„ํ˜ธ ()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ์‹œ๋ฆฌ์ฆˆ ๊ฐ’๋“ค์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. 9) ๋ง‰๋Œ€ ์ฐจํŠธ ์ถ”๊ฐ€ํ•˜๊ณ  ์ฐจํŠธ ์„ค์ •ํ•˜๊ธฐ # ์ฐจํŠธ ์ถ”๊ฐ€ x, y, cx, cy = Inches(5), Inches(1.8), Inches(4.5), Inches(4.2) chart = slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data).chart # ์ฐจํŠธ ์„ค์ • chart.has_legend = False chart.plots[0].has_data_labels = True ์ด์ œ ์Šฌ๋ผ์ด๋“œ์— ์ฐจํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € x์™€ y๋กœ ์ฐจํŠธ์˜ ์œ„์น˜๋ฅผ, cx์™€ cy๋กœ ์ฐจํŠธ์˜ ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ์ธ์น˜ ๋‹จ์œ„๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ add_chart ๋ฉ” ์„œ๋“œ์— ์ฐจํŠธ์˜ ์œ ํ˜•, ์œ„์น˜, ํฌ๊ธฐ, ๊ทธ๋ฆฌ๊ณ  ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ฐจํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋Š” XL_CHART_TYPE.COLUMN_CLUSTERED๋กœ ์ˆ˜์ง ๋ง‰๋Œ€ ์ฐจํŠธ๋ฅผ ์„ ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์–ด์„œ ์ฐจํŠธ์˜ ์œ„์น˜์™€ ํฌ๊ธฐ์˜ ๊ฐ’์„ ์„ค์ •ํ•œ x, y, cx, cy ๋ณ€์ˆ˜๋ฅผ ์ฐจ๋ก€๋กœ ์ „๋‹ฌํ•˜๊ณ , ๋งˆ์ง€๋ง‰์œผ๋กœ ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” chart_data ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋์—. chart ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฐจํŠธ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. chart.has_legend๋ฅผ False๋กœ ์„ค์ •ํ•˜์—ฌ ์ฐจํŠธ์˜ ๋ฒ”๋ก€(legend)๋ฅผ ์ˆจ๊น๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  chart.plots[0].has_data_labels๋ฅผ True๋กœ ์„ค์ •ํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ํ”Œ๋กฏ์˜ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์„ ํ™œ์„ฑํ™”ํ•˜์—ฌ ์ฐจํŠธ ์ƒ์— ๊ฐ ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ์˜ ๊ฐ’์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. 11) ์Šฌ๋ผ์ด๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ & ๋ผ์ธ ์ฐจํŠธ ์ถ”๊ฐ€๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์„ค์ •ํ•˜๊ธฐ # ์ „์ฒด ํŒ๋งค ์ถ”์ด ์Šฌ๋ผ์ด๋“œ ์ƒ์„ฑ slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "์ œํ’ˆ๋ณ„ ํŒ๋งค ์ถ”์ด" # ์ „์ฒด ํŒ๋งค ์ถ”์ด ์ฐจํŠธ ๋ฐ์ดํ„ฐ ์„ค์ • chart_data = CategoryChartData() chart_data.categories = [row[0] for row in data[1:]] for sheet_name, sales in sales_data.items(): chart_data.add_series(sheet_name, (sale[0] for sale in sales)) ์ด๋ฒˆ์—๋Š” ์ „์ฒด ์ œํ’ˆ๋“ค์˜ ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ํŒ๋งค ์ถ”์ด๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ผ์ธ ์ฐจํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ฐจํŠธ๋ฅผ ์‚ฝ์ž…ํ•  ์Šฌ๋ผ์ด๋“œ๋ฅผ ๋จผ์ € ์ถ”๊ฐ€ํ•˜๊ณ  "์ œํ’ˆ๋ณ„ ํŒ๋งค ์ถ”์ด"๋ผ๋Š” ํƒ€์ดํ‹€์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด CategoryChartData() ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋™์ผํ•˜๊ฒŒ ํŒ๋งค์›”์„ ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด data ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์—ด์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ—ค๋”ํ–‰์€ ์ œ์™ธํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ผ์ธ์—์„œ๋Š” sales_data.items()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค์™€ ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ ์‹œํŠธ๋ช…(=์ œํ’ˆ๋ช…)๊ณผ ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ(sales) ์Œ์„ ๋ฐ˜๋ณต๋ฌธ์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. add_series ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ ์ œํ’ˆ์˜ ํŒ๋งค๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจํŠธ์— ์‹œ๋ฆฌ์ฆˆ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์‹œ๋ฆฌ์ฆˆ์˜ ์ด๋ฆ„์€ ์‹œํŠธ๋ช…(=์ œํ’ˆ๋ช…)์ด๊ณ  ๊ฐ’์€ ๊ฐ ์›”์˜ ํŒ๋งค๋Ÿ‰(sales[0])์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. 12) ๋ผ์ธ ์ฐจํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ & ์ฐจํŠธ ์„ค์ •ํ•˜๊ธฐ # ์ „์ฒด ํŒ๋งค ์ถ”์ด ์ฐจํŠธ ์ถ”๊ฐ€ x, y, cx, cy = Inches(1), Inches(2), Inches(8), Inches(4.5) chart = slide.shapes.add_chart(XL_CHART_TYPE.LINE, x, y, cx, cy, chart_data).chart # ์ฐจํŠธ ์„ค์ • chart.has_legend = True chart.legend.include_in_layout = False chart.category_axis.has_major_gridlines = False ์Šฌ๋ผ์ด๋“œ์— ์‚ฝ์ž…ํ•  ์ฐจํŠธ์˜ ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•˜๊ณ , add_chart๋กœ ์ฐจํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” XL_CHART_TYPE.LINE์œผ๋กœ ๋ผ์ธ ์ฐจํŠธ๋ฅผ ์„ ํƒํ•˜์˜€๊ณ , ์œ„์˜ ๋ง‰๋Œ€ ์ฐจํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฐจํŠธ์˜ ์œ ํ˜•, ์œ„์น˜, ํฌ๊ธฐ, ๊ทธ๋ฆฌ๊ณ  ์ฐจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ add_chart์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋์—๋Š”. chart๋กœ ์ฐจํŠธ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋ผ์ธ ์ฐจํŠธ์— ํ‘œ์‹œ๋œ ๋ผ์ธ์ด ์ด 3๊ฐœ์ด๋ฏ€๋กœ ๊ฐ ๋ผ์ธ์ด ๋ฌด์—‡์„ ๋‚˜ํƒ€๋‚ด๋Š”์ง€ ํ•œ๋ˆˆ์— ์•Œ ์ˆ˜ ์žˆ๋„๋ก chart.has_legend = True๋กœ ๋ฒ”๋ก€(legend)๋ฅผ ํ‘œ์‹œํ•˜๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. chart.legend.include_in_layout = False๋Š” ๋ฒ”๋ก€๊ฐ€ ์ฐจํŠธ ๋ ˆ์ด์•„์›ƒ์˜ ์ผ๋ถ€๋กœ ํฌํ•จ๋˜์ง€ ์•Š๋„๋ก ์„ค์ •ํ•˜๋Š” ์ฝ”๋“œ๋กœ, ์ฐจํŠธ์™€ ๋ฒ”๋ก€๊ฐ€ ์„œ๋กœ ๊ฒน์น˜์ง€ ์•Š๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์ฐจํŠธ ์„ค์ •์€ chart.category_axis.has_major_gridlines = False๋กœ x์ถ•(์นดํ…Œ๊ณ ๋ฆฌ ์ถ•)์˜ ์ฃผ์š” ๊ทธ๋ฆฌ๋“œ ๋ผ์ธ์„ ํ‘œ์‹œํ•˜์ง€ ์•Š๋„๋ก ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. 13) ๋งˆ์ง€๋ง‰ ์Šฌ๋ผ์ด๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ & ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ธฐ # ๋งˆ์ง€๋ง‰ ์Šฌ๋ผ์ด๋“œ ์ถ”๊ฐ€ slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout) slide.shapes.title.text = "๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค." # ๊ฒฐ๊ณผ ์ €์žฅ prs.save('์ƒ๋ฐ˜๊ธฐ_์ œํ’ˆ๋ณ„_ํŒ๋งค ํ˜„ํ™ฉ_๋ถ„์„. pptx') ๋งˆ์ง€๋ง‰ ์Šฌ๋ผ์ด๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•œ ํ›„, ์ตœ์ข… ํŒŒ์ผ์„ '์ƒ๋ฐ˜๊ธฐ_์ œํ’ˆ๋ณ„_ํŒ๋งค ํ˜„ํ™ฉ_๋ถ„์„. pptx'๋กœ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. 06-05. ์‘์šฉ! ํŒŒ์›Œํฌ์ธํŠธ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์œผ๋กœ<NAME>์ƒ ์ž๋™ ์ƒ์„ฑํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์•ž์—์„œ ๋ฐฐ์šด ๊ธฐ๋ณธ ํŒŒ์›Œํฌ์ธํŠธ ์‚ฌ์šฉ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ๋งŒ๋“  ํŒŒ์›Œํฌ์ธํŠธ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ ํŒŒ์ด์ฌ์—์„œ ์ž๋™์œผ๋กœ<NAME>์ƒ์œผ๋กœ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์›Œํฌ์ธํŠธ๋กœ<NAME>์ƒ ๋งŒ๋“ค๊ธฐ(win32) ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ํŒŒ์›Œํฌ์ธํŠธ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ python-pptx ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํŒŒ์›Œํฌ์ธํŠธ๋ฅผ<NAME>์ƒ์œผ๋กœ ์ง์ ‘ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  pywin32๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ํŒŒ์›Œํฌ์ธํŠธ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„<NAME>์ƒ์œผ๋กœ ๋‚ด๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pywin32๋Š” Windows ์šด์˜ ์ฒด์ œ์—์„œ ๋™์ž‘ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ํŒŒ์ด์ฌ ์–ธ์–ด๋กœ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋„๊ตฌ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ์ด ํŒจํ‚ค์ง€๋Š” Windows์˜ ํŠน์ • ๊ธฐ๋Šฅ๋“ค์„ ํŒŒ์ด์ฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Windows ๊ด€๋ฆฌ ๋„๊ตฌ์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ๊ทธ๋ž˜ํ”ฝ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค(GUI)๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋ฉฐ, ์šฐ๋ฆฌ๊ฐ€ ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  COM ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ๋Šฅ๋„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. COM ๊ฐ์ฒด๋Š” ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ชจ๋ธ๋กœ, ์ด๋ฅผ ํ†ตํ•ด MS Office ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๋“ค(Word, Excel, Powerpoint ๋“ฑ)์„ ํŒŒ์ด์ฌ์œผ๋กœ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” pywin32๋ฅผ ์ด์šฉํ•œ ํŒŒ์›Œํฌ์ธํŠธ์˜ ์˜์ƒ ์ž๋™ํ™” ๋ถ€๋ถ„์— ์ดˆ์ ์„ ๋‘๊ณ  ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € pip ๋ช…๋ น์œผ๋กœ pywin32ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install pywin32 ๊ทธ๋Ÿฌ๋ฉด ์„ค์น˜ํ•œ pywin32๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ์›Œํฌ์ธํŠธ๋ฅผ<NAME>์ƒ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ๋Š” ์•„๋ž˜์˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด 'makevideo.pptx'๋ผ๋Š” ํŒŒ์›Œํฌ์ธํŠธ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„<NAME>์ƒ์œผ๋กœ ๋ณ€ํ™˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณธ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์€ ์ด 12๊ฐœ์˜ ์Šฌ๋ผ์ด๋“œ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € win32com.client๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. win32com.client๋Š” pywin32 ํŒจํ‚ค์ง€์˜ ์ผ๋ถ€๋กœ, ์•ž์„œ ์„ค๋ช…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์œˆ๋„์˜ COM ๊ฐ์ฒด๋ฅผ ํŒŒ์ด์ฌ์—์„œ ์ƒ์„ฑํ•˜๊ณ  ์•ก์„ธ์Šคํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค. win32com.client๋กœ ํŒŒ์ผ์„<NAME>์ƒ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. import win32com.client # ํŒŒ์›Œํฌ์ธํŠธ ์—ด๊ธฐ powerpoint = win32com.client.Dispatch("PowerPoint.Application") # ํŒŒ์›Œํฌ์ธํŠธ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ presentation_path = r"C:\\users\2023\makevideo.pptx" presentation = powerpoint.Presentations.Open(presentation_path, WithWindow=False) # ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„<NAME>์ƒ์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ powerpoint.Presentations(1).CreateVideo( FileName="C:\\users\2023\makevideo.mp4", UseTimingsAndNarrations=True, DefaultSlideDuration=2, Quality=85, VertResolution=1080, FramesPerSecond=30, ) # ํŒŒ์›Œํฌ์ธํŠธ ๋‹ซ๊ธฐ powerpoint.Quit() ๋จผ์ € win32com.client.Dispatch ํ•จ์ˆ˜๋กœ ํŒŒ์›Œํฌ์ธํŠธ ์‘์šฉํ”„๋กœ๊ทธ๋žจ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ<NAME>์ƒ ๋ณ€ํ™˜์— ์‚ฌ์šฉํ•  ํŒŒ์›Œํฌ์ธํŠธ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ powerpoint.Presentations.Open(presentation_path, WithWindow=False)์œผ๋กœ ์—ด์–ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ WithWindow ์˜ต์…˜์€ ํŒŒ์›Œํฌ์ธํŠธ ์ฐฝ์„ ํ™œ์„ฑํ™”ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค. True๋กœ ํ•  ๊ฒฝ์šฐ์—๋Š” ์ฐฝ์ด ๋„์›Œ์ ธ์„œ ์šฐ๋ฆฌ๋„ ์ง์ ‘ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, False๋กœ ํ•  ๊ฒฝ์šฐ์—๋Š” ์ฐฝ์ด ๋น„ํ™œ์„ฑํ™”๋œ ์ƒํƒœ์—์„œ ์ž‘์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. powerpoint.Presentations(1)๋กœ ์—ด๋ ค์žˆ๋Š” ์ฒซ ๋ฒˆ์งธ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  CreativeVideo ๋ฉ” ์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„<NAME>์ƒ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  'FileName'์œผ๋กœ ์ „๋‹ฌํ•œ ์ง€์ • ๊ฒฝ๋กœ์™€ ํŒŒ์ผ๋ช…์œผ๋กœ ํŒŒ์ผ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์—ฌ๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ<NAME>์ƒ ๋ณ€ํ™˜ ์˜ต์…˜์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•œ ๋ช‡ ๊ฐ€์ง€ ์˜ต์…˜๋“ค์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. UseTimingsAndNarrations=True๋Š” ์Šฌ๋ผ์ด๋“œ ์‡ผ์—์„œ ์„ค์ •ํ•œ ํƒ€์ด๋ฐ๊ณผ ๋‚ด๋ ˆ์ด์…˜์„<NAME>์ƒ์— ํฌํ•จํ•˜์—ฌ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ๊ฐ ์Šฌ๋ผ์ด๋“œ์— ์„ค์ •๋œ ์ „ํ™˜ ์‹œ๊ฐ„๊ณผ ์˜ค๋””์˜ค ์„ค๋ช… ๋“ฑ์ด ๊ทธ๋Œ€๋กœ<NAME>์ƒ์— ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ์ด ์˜ต์…˜์„ False๋กœ ์„ค์ •ํ•˜๋ฉด, ์Šฌ๋ผ์ด๋“œ ์‡ผ ํƒ€์ด๋ฐ๊ณผ ๋‚ด๋ ˆ์ด์…˜์€<NAME>์ƒ์— ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฉฐ ๊ธฐ๋ณธ ์ „ํ™˜ ์‹œ๊ฐ„์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. DefaultSlideDuration=2 ์Šฌ๋ผ์ด๋“œ๊ฐ€ ํ‘œ์‹œ๋˜๋Š” ๊ธฐ๋ณธ ์‹œ๊ฐ„์„ ์ง€์ •ํ•˜๋Š” ์˜ต์…˜์ด๋ฉฐ, ๊ฐ’์€ ์ดˆ ๋‹จ์œ„๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋Š” 2๋กœ ์ง€์ •ํ•˜์—ฌ 2์ดˆ๋งˆ๋‹ค ๋‹ค์Œ ์Šฌ๋ผ์ด๋“œ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 3.25์ดˆ์™€ ๊ฐ™์ด ์†Œ์ˆ˜์ ์œผ๋กœ๋„ ์„ค์ • ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Quality=85<NAME>์ƒ์˜ ์ „๋ฐ˜์ ์ธ ํ’ˆ์งˆ์„ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ฐ’์€ 0๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ๋ฒ”์œ„์—์„œ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋†’์€ ๊ฐ’์€ ๋†’์€ ํ’ˆ์งˆ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. VertResolution=1080์€<NAME>์ƒ์˜ ์ˆ˜์ง ํ•ด์ƒ๋„๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•ด์ƒ๋„๋ฅผ ์กฐ์ ˆํ•˜์—ฌ<NAME>์ƒ์˜ ํ™”์งˆ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฐ’์ด ๋†’์•„์งˆ์ˆ˜๋ก ํ•ด์ƒ๋„๊ฐ€ ๋†’์•„์ง‘๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ 720p, 1080p ๋“ฑ์˜ ๊ฐ’์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. FramesPerSecond=30์€<NAME>์ƒ์˜ ํ”„๋ ˆ์ž„ ์†๋„๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋ ˆ์ž„ ์†๋„๋Š” ์ดˆ๋‹น ํ”„๋ ˆ์ž„ ์ˆ˜(FPS)๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ 24, 30, 60 ๋“ฑ์˜ ๊ฐ’์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋†’์€ FPS ๊ฐ’์€ ๋” ๋งค๋„๋Ÿฌ์šด<NAME>์ƒ์„ ์ƒ์„ฑํ•˜์ง€๋งŒ ํŒŒ์ผ ํฌ๊ธฐ๊ฐ€ ์ปค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™์˜์ƒ ์ƒ์„ฑ์ด ๋‹ค ๋๋‚œ ํ›„์—๋Š” powerpoint.Quit()์œผ๋กœ ํŒŒ์›Œํฌ์ธํŠธ๋ฅผ ๋‹ซ์•„์ค๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ํŒŒ์›Œํฌ์ธํŠธ ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„ mp4<NAME>์ƒ์œผ๋กœ ์ž๋™ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 'makevideo.mp4' ํŒŒ์ผ ์‹คํ–‰ ํ™”๋ฉด ์บก์ฒ˜ ๋™์˜์ƒ ์—ฌ๋Ÿฌ ๊ฐœ ํ•ฉ์น˜๊ธฐ ์œ„์™€ ๊ฐ™์ด ํ”„๋ ˆ์  ํ…Œ์ด์…˜์„<NAME>์ƒ์œผ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„์— ๋‹ค๋ฅธ<NAME>์ƒ์„ ์ด์–ด์„œ ๋ถ™์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” 'concatvideo.mp4'์™€ ์œ„์—์„œ ๋งŒ๋“  'makevideo.mp4'๋ฅผ ์ด์–ด๋ถ™์—ฌ์„œ ํ•˜๋‚˜์˜<NAME>์ƒ ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. pip install moviepy ๋™์˜์ƒ์„ ํ•ฉ์น  ๋•Œ์—๋Š” ํŒŒ์ด์ฌ์œผ๋กœ<NAME>์ƒ์„ ํŽธ์ง‘ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ moviepy๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ์„ ์œ„ํ•ด ๋จผ์ € ์œ„์™€ ๊ฐ™์ด pip๋กœ ์„ค์น˜ํ•œ ํ›„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. from moviepy.editor import * #<NAME>์ƒ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ video1 = VideoFileClip("C:\\users\2023\concatvideo.mp4").without_audio() video2 = VideoFileClip("C:\\users\2023\makevideo.mp4") #<NAME>์ƒ ํ•ฉ์น˜๊ธฐ final_video = concatenate_videoclips([video1, video2], method="compose") # ํ•ฉ์นœ<NAME>์ƒ ์ €์žฅํ•˜๊ธฐ final_video.write_videofile("C:\\users\2023\combined_video.mp4", codec='libx264') ๋™์˜์ƒ์„ ํ•ฉ์น˜๊ธฐ ์œ„ํ•ด VideoFileClip ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ๊ฒฝ๋กœ์—์„œ ๋‘ ๊ฐœ์˜ ๋น„๋””์˜ค ํด๋ฆฝ์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.<NAME>์ƒ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ์ฒซ ๋ฒˆ์งธ<NAME>์ƒ ๋’ค์— ๋ถ™์€ without_audio() ๋ฉ”์„œ๋“œ๋Š” ๋น„๋””์˜ค ํด๋ฆฝ์˜ ์†Œ๋ฆฌ๋ฅผ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. ๋ชจ๋“  ์˜์ƒ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์ตœ์ข…์ ์œผ๋กœ ํ•ฉ์ณ์ง„<NAME>์ƒ์—๋Š” ์†Œ๋ฆฌ๊ฐ€ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ถˆ๋Ÿฌ์˜จ 2๊ฐœ์˜<NAME>์ƒ์„ concatenate_videoclips([video1, video2], method="compose")๋กœ ํ•ฉ์ณ์ค๋‹ˆ๋‹ค. concatenate_videoclips ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ๋น„๋””์˜ค ํด๋ฆฝ์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›์•„์„œ ํ•˜๋‚˜์˜ ๋น„๋””์˜ค๋กœ ํ•ฉ์ณ์ฃผ๋Š” ํ•จ์ˆ˜๋กœ, ์—ฌ๊ธฐ์„œ๋Š” video1๊ณผ video2๋ฅผ ํ•ฉ์น˜๊ธฐ ์œ„ํ•ด ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. method="compose"๋Š” concatenate_videoclips ํ•จ์ˆ˜์—์„œ<NAME>์ƒ ํด๋ฆฝ์„ ํ•ฉ์น  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ์‹์„ ์ง€์ •ํ•˜๋Š” ์ธ์ž์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ chain์œผ๋กœ ๋ชจ๋“  ํด๋ฆฝ์ด ๋™์ผํ•œ ํฌ๊ธฐ(๋™์ผํ•œ ํ•ด์ƒ๋„)์ผ ๋•Œ๋งŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. compose๋Š” ํด๋ฆฝ๋“ค์˜ ํ•ด์ƒ๋„๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ์—๋Š” ์˜์ƒ์„ ํ•ฉ์น  ๋•Œ ๊ทธ์ค‘ ๊ฐ€์žฅ ํฐ ํด๋ฆฝ์˜ ํ•ด์ƒ๋„์— ๋งž์ถฅ๋‹ˆ๋‹ค. ํ•ด์ƒ๋„๊ฐ€ ๋” ์ž‘์€<NAME>์ƒ์€ ํ•ด์ƒ๋„๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ๊ฒ€์€์ƒ‰ ๋ฐฐ๊ฒฝ ์œ„ ์ค‘์•™์— ์˜์ƒ์ด ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์˜์ƒ์˜ ํ•ด์ƒ๋„๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด method ์ธ์ž๋ฅผ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋‹ค๋ฅผ ๊ฒฝ์šฐ์—๋Š” compose๋กœ ์ง€์ •ํ•˜์—ฌ์•ผ<NAME>์ƒ์ด ์ •์ƒ์ ์œผ๋กœ ํ•ฉ์ณ์ง‘๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ฉ์นœ<NAME>์ƒ์€ write_videofile ํ•จ์ˆ˜๋กœ ์ง€์ •๋œ ๊ฒฝ๋กœ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. codec ํŒŒ๋ผ๋ฏธํ„ฐ๋Š”<NAME>์ƒ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์ง€์ •ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” 'libx264'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ H.264 ๋ฐฉ์‹์œผ๋กœ ์ธ์ฝ”๋”ฉํ•˜๋„๋ก ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฒฝ๋กœ์—์„œ ๋ณด๋ฉด 'concatvideo.mp4'์™€ 'makevideo.mp4'๊ฐ€ ์—ฐ์†์ ์œผ๋กœ ํ•ฉ์ณ์ง„ 'combined_video.mp4' ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™์˜์ƒ์— ๋ฐฐ๊ฒฝ์Œ์•… ์ถ”๊ฐ€ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š”<NAME>์ƒ์— ๋ฐฐ๊ฒฝ์Œ์•…์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ „ ์˜ˆ์ œ์—์„œ ๋งŒ๋“  ์˜์ƒ ํŒŒ์ผ "combined_video.mp4"์— "Societys Dream - Mini Vandals.mp3"๋ฅผ ๋ฐฐ๊ฒฝ์Œ์•…์œผ๋กœ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. "combined_video.mp4"๋Š” ์ด ์žฌ์ƒ ๊ธธ์ด๊ฐ€ 48์ดˆ์ด๋ฉฐ, ๋ฐฐ๊ฒฝ์Œ์•… mp3 ํŒŒ์ผ์€ ์ด ๊ธธ์ด๊ฐ€ 2๋ถ„ 14์ดˆ์ž…๋‹ˆ๋‹ค. ์˜์ƒ์— ๋ฐฐ๊ฒฝ์Œ์•…์„ ๋„ฃ์„ ๋•Œ ์Œ์›๊ณผ ์˜์ƒ์˜ ๊ธธ์ด๊ฐ€ ๋งŽ์ง€ ์•Š๋Š”๋‹ค๋ฉด<NAME>์ƒ์ด ๋๋‚˜๋„ ์Œ์•…์ด ๊ณ„์† ์žฌ์ƒ๋˜๊ฑฐ๋‚˜<NAME>์ƒ ์žฌ์ƒ ๋„์ค‘ ์Œ์•…์ด ๊ฐ‘์ž๊ธฐ ๋ฉˆ์ถ”๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํŒŒ์ด์ฌ์œผ๋กœ<NAME>์ƒ์˜ ๊ธธ์ด์— ๋งž๊ฒŒ ์Œ์›์„ ์ž๋ฅด๊ฑฐ๋‚˜ ์Œ์›์„ ๋ฐ˜๋ณต์‹œํ‚ค๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from moviepy.editor import * #<NAME>์ƒ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ video = VideoFileClip("C:\\users\2023\combined_video.mp4") # ๋ฐฐ๊ฒฝ ์Œ์•… ๋ถˆ๋Ÿฌ์˜ค๊ธฐ background_music = AudioFileClip('C:\\users\2023\Societys Dream - Mini Vandals.mp3') #<NAME>์ƒ์˜ ๊ธธ์ด์— ๋งž๊ฒŒ ๋ฐฐ๊ฒฝ์Œ์•…์„ ์ž๋ฅด๊ฑฐ๋‚˜ ๋ฐ˜๋ณต if background_music.duration > video.duration: background_music = background_music.subclip(0, video.duration) else: background_music = background_music.fx(vfx.audio_loop, duration=video.duration) #<NAME>์ƒ์— ๋ฐฐ๊ฒฝ ์Œ์•… ์ถ”๊ฐ€ final_video = video.set_audio(background_music) # ๊ฒฐ๊ณผ<NAME>์ƒ ์ €์žฅ final_video.write_videofile('C:\\users\2023\output_video.mp4', codec='libx264') ๋จผ์ € VideoFileClip ํ•จ์ˆ˜์™€ AudioFileClip ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ๊ฒฝ๋กœ์—์„œ<NAME>์ƒ๊ณผ ์Œ์›์„ ๊ฐ๊ฐ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ ๋ฐฐ๊ฒฝ์Œ์•…์˜ ๊ธธ์ด๋ฅผ<NAME>์ƒ์˜ ๊ธธ์ด์— ๋งž๊ฒŒ ์กฐ์ •ํ•˜๋Š” ์ž‘์—…์„ ํ•ฉ๋‹ˆ๋‹ค. if ๋ฌธ์—<NAME>์ƒ์ด๋‚˜ ์Œ์›์˜ ๊ธธ์ด๋ฅผ ์ดˆ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” duration ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๋ถ€๋“ฑํ˜ธ('>', '<')๋กœ ์Œ์›์ด<NAME>์ƒ์˜ ๊ธธ์ด๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ์™€ ๊ทธ ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋ฅผ ๊ฐ๊ฐ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. ์Œ์›์˜ ๊ธธ์ด๊ฐ€<NAME>์ƒ์˜ ๊ธธ์ด๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ์—๋Š” subclip ๋ฉ” ์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ<NAME>์ƒ์˜ ๊ธธ์ด๋งŒํผ ์Œ์›์„ ์ž˜๋ผ์ค๋‹ˆ๋‹ค. subclip ๋ฉ” ์„œ๋“œ๋Š” ์ถ”์ถœํ•˜๋ ค๋Š” ํด๋ฆฝ์˜ ์‹œ์ž‘ ์‹œ์ ๊ณผ ์ข…๋ฃŒ ์‹œ์ ์„ ์ธ์ž๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์—, background_music = background_music.subclip(0, video.duration) ์—ฌ๊ธฐ์„œ 0์€ ์Œ์›์˜ ๊ฐ€์žฅ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ<NAME>์ƒ์˜ ๊ธธ์ด(video.duration)๊นŒ์ง€ ์ž๋ฅด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. subclip(1, 49)์ฒ˜๋Ÿผ ์ง์ ‘ ์›ํ•˜๋Š” ์‹œ์ ์„ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋กœ ์Œ์›์ด<NAME>์ƒ์˜ ๊ธธ์ด๋ณด๋‹ค ์งง์€ ๊ฒฝ์šฐ์—๋Š” fx ๋ฉ” ์„œ๋“œ์™€ vfx.audio_loop ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ๊ฒฝ์Œ์•…์„<NAME>์ƒ์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต์‹œํ‚ต๋‹ˆ๋‹ค. fx๋Š” moviepy ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์—ฌ๋Ÿฌ ํšจ๊ณผ๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๋กœ, vfx๋Š” video effects๋ฅผ ์˜๋ฏธํ•˜๊ณ  audio_loop๋Š” ์˜ค๋””์˜ค ํด๋ฆฝ์„ ์ฃผ์–ด์ง„ ์‹œ๊ฐ„ ๋™์•ˆ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ํšจ๊ณผ์ž…๋‹ˆ๋‹ค. background_music = background_music.fx(vfx.audio_loop, duration=video.duration) ์—ฌ๊ธฐ์„œ๋Š” ๋ฐ˜๋ณตํ•˜๋Š” ์‹œ๊ฐ„์„<NAME>์ƒ์˜ ๊ธธ์ด๋กœ ์„ค์ •ํ•˜์˜€์œผ๋ฉฐ, duration=48๊ณผ ๊ฐ™์ด ์›ํ•˜๋Š” ์ง€์† ์‹œ๊ฐ„์„ ์ดˆ ๋‹จ์œ„๋กœ ์ง์ ‘ ์ž…๋ ฅํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ์„ค์ • ์™„๋ฃŒํ•œ ์Œ์›์„ set_audio ๋ฉ” ์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด<NAME>์ƒ์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž‘์—…์ด ๋๋‚œ ์˜์ƒ์„ write_videofile ํ•จ์ˆ˜๋กœ ์ง€์ •๋œ ๊ฒฝ๋กœ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์œ„์˜ ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ณ„๋„์˜ ๋ฒˆ๊ฑฐ๋กœ์šด ์Œ์› ํŽธ์ง‘ ์ž‘์—… ์—†์ด ํŒŒ์ด์ฌ์œผ๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ์Œ์›์˜ ๊ธธ์ด๋ฅผ ํŽธ์ง‘ํ•˜์—ฌ<NAME>์ƒ์— ์Œ์›์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 07. ๋ฐ์ดํ„ฐ ๋ถ„์„ ์ด ์ฑ•ํ„ฐ์—์„œ๋Š” ํŒ๋‹ค์Šค(Pandas)๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ๋ถ„์„์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถ„์„์€ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ์œ ์šฉํ•œ ์ •๋ณด์™€ ์˜๋ฏธ๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ณผ์ •์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์–ด๋–ค ์ƒํ’ˆ์˜ ํŒ๋งค ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•œ๋‹ค๋ฉด ๊ทธ ์ƒํ’ˆ์˜ ํŒ๋งค๋Ÿ‰์ด ์–ด๋Š ์‹œ๊ธฐ์— ๋†’์•„์ง€๋Š”์ง€, ์–ด๋–ค ์š”์ธ๋“ค์ด ํŒ๋งค๋Ÿ‰์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”์ง€ ๋“ฑ์˜ ์ธ์‚ฌ์ดํŠธ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์–ป์€ ์ •๋ณด๋Š” ์žฌ๊ณ  ๊ด€๋ฆฌ, ๋งˆ์ผ€ํŒ… ์ „๋žต ๋“ฑ์˜ ์—…๋ฌด์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒ๋‹ค์Šค๋Š” ํŒŒ์ด์ฌ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ํŒ๋‹ค์Šค๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ<NAME>์„ ์ง€์›ํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ ์ •์ œ๋‚˜ ๋ณ€ํ™˜, ์ง‘๊ณ„, ๊ทธ๋ฆฌ๊ณ  ํ†ต๊ณ„ ๋ถ„์„ ๋“ฑ ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ์ „ ๊ณผ์ •์— ํ•„์š”ํ•œ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‹ค๋ฅธ ์‹œ๊ฐํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€์˜ ํ˜ธํ™˜์„ฑ์ด ์ข‹์•„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜๋ฉฐ ์‹œ๊ฐํ™”๋„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋งŽ์€ ์žฅ์ ์œผ๋กœ ์ธํ•ด ํŒ๋‹ค์Šค๋Š” ๋ฐ์ดํ„ฐ ๋ถ„์„์— ์žˆ์–ด ํ•„์ˆ˜์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์—ฌ๊ฒจ์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํŒ๋‹ค์Šค์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•๋ถ€ํ„ฐ ์‹ค๋ฌด์— ํ™œ์šฉํ•˜๋Š” ์˜ˆ๊นŒ์ง€ ํ•จ๊ป˜ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ์—…๋ฌด์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํฌ๊ณ  ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ณต๊ณต๋ฐ์ดํ„ฐ์˜ ์ž๋ฃŒ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ํ†ต๊ณ„ํ•™ -SPSS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ- ### ๋ณธ๋ฌธ: SPSS ์‚ฌ์šฉ๋ฐฉ๋ฒ• ์†Œ๊ฐœ 01. ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ, ์ˆ˜์ •, ๋ถ„์„, ์ €์žฅ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ํ…์ŠคํŠธ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๊ฐ€์ค‘์น˜ ์„ค์ • 1. ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ณ  ๋ณ€๊ฒฝํ•˜๊ธฐ SPSS ๋ฐ์ดํ„ฐ ํŒŒ์ผ์€ ํ™•์žฅ์ž๊ฐ€. SAV ํŒŒ์ผ์ด๋‹ค. ์ด ํŒŒ์ผ์€ ์—ฌ๊ธฐ์—์„œ ๋‹ค์šด๋กœ๋“œํ•œ๋‹ค. ์ด ํŒŒ์ผ์„ ํด๋ฆญํ•˜๋ฉด SPSS๊ฐ€ ์‹คํ–‰๋˜๊ณ  ํŒŒ์ผ์ด ์—ด๋ฆฐ๋‹ค. ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ(Data View) ์‹œํŠธ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ž๋ฃŒ๋ฅผ ์ž…๋ ฅ, ์ˆ˜์ • ๋ฐ ์‚ญ์ œ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ ๋ณ€์ˆ˜ ๋ณด๊ธฐ(Variable View) ์‹œํŠธ์—์„œ ๋ณ€์ˆ˜์— ๋Œ€ํ•˜์—ฌ ์ด๋ฆ„์€ ๋ณ€์ˆ˜๋ช…์„ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณ€์ˆ˜๋ช…์€ ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋Š” ๋ฐ˜๋“œ์‹œ ๋ฌธ์ž์ด์–ด์•ผ ํ•˜๊ณ , ํŠน์ˆ˜๋ฌธ์ž๋Š” ์‚ฝ์ž…ํ•  ์ˆ˜ ์—†๊ณ (_ , $ ์ œ์™ธ), ์ˆซ์ž๋Š” ์ฒซ ๊ธ€์ž๋Š” ์ œ์™ธํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ ํ˜•์€ ๋ณ€์ˆ˜์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ˆซ์ž, ๋ฌธ์ž, ๋‚ ์งœ ๋“ฑ์ด ์žˆ๋‹ค. ๋„ˆ๋น„๋Š” ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ์˜ ๊ฐ ๋ณ€์ˆ˜์— ๊ฐ’์ด ๋“ค์–ด๊ฐˆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ•œ๊ธ€์€ ํ•œ ๋ฌธ์ž๋ฅผ 2๋กœ ์˜์–ด๋Š” ํ•œ ๋ฌธ์ž๋ฅผ 1๋กœ ์—ฌ๊ธด๋‹ค. ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฌ๋Š” ์› ์ž๋ฃŒ์— ๋Œ€ํ•˜์—ฌ ๋ณด์ด๋Š” ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜๋กœ ์œ ํšจํ•˜์ง€ ์•Š์€ ์ˆซ์ž๋Š” 0์œผ๋กœ ํ‘œ์‹œํ•œ๋‹ค. ์„ค๋ช…์€ ๋ณ€์ˆ˜์— ๋Œ€ํ•˜์—ฌ ์„ค๋ช…์„ ๊ธธ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์ด๋ฆ„์— ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ค๋ฅด๊ฒŒ ์ œ์•ฝ ์—†์ด ์–ด๋– ํ•œ ๋ฌธ์ž๋„ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ํ†ต๊ณ„๋ถ„์„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์— ์„ค๋ช…์ด ์žˆ์œผ๋ฉด ์ด๋ฆ„๊ฐ’์€ ์ถœ๋ ฅ๋˜์ง€ ์•Š๊ณ  ์„ค๋ช…์ด ์ถœ๋ ฅ๋œ๋‹ค. ๊ฐ’ ์ด์‚ฐํ˜•, ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ์ธ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ์— ์ˆซ์ž๋กœ ์ž…๋ ฅํ•˜๊ณ , ์—ฌ๊ธฐ์„œ ์ˆซ์ž์— ๋Œ€์‘ํ•˜๋Š” ๊ฐ’์— ๋Œ€ํ•˜์—ฌ ์„ค๋ช…์„ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ž๊ฐ€ ์› ์ž๋ฃŒ์— ์ €์žฅ๋œ ๊ฒฝ์šฐ ์ผ๋ถ€ ๋ถ„์„์—์„œ ์ œ์•ฝ์‚ฌํ•ญ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„์„์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. ๊ฒฐ์ธก๊ฐ’ ์„ค์ •์€ ํ•œ ๊ฐ’, ๋˜๋Š” ๋ฒ”์œ„๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ด์€ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ์—์„œ ๋ณด์ด๋Š” ์—ด์˜ ๋„ˆ๋น„์ด๋‹ค. ๋งž์ถค์€ ์ •์—ด์˜ ๋ฐฉ๋ฒ•์„ ์„ค์ •ํ•œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž๋Š” ์™ผ์ชฝ, ์ˆซ์ž๋Š” ์˜ค๋ฅธ์ชฝ์— ์ •์—ด ๋œ๋‹ค. ์ธก๋„์€ ์ž๋ฃŒ์˜ ํ˜•ํƒœ๊ฐ€ ์ฒ™๋„ํ˜•, ๋ช…๋ชฉํ˜•, ์ˆœ์œ„ํ˜• ์ค‘ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆ˜ ๊ณ„์‚ฐ ๋ณ€์ˆ˜ ๊ณ„์‚ฐ์€ ์ด๋ฏธ ์žˆ๋Š” ๋ณ€์ˆ˜๋“ค๊ณผ ์—ฐ์‚ฐํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค. ์•„๋ž˜์˜ ํ™”๋ฉด์€ "๋ณ€ํ™˜(Transform) ->๋ณ€์ˆ˜ ๊ณ„์‚ฐ(Compute)" ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜์˜€๋‹ค. ๋Œ€์ƒ ๋ณ€์ˆ˜(Targer Variable)์— BMI, ์ˆซ์ž ํ‘œํ˜„์‹(Numeric Expression)์— bmi(body mass index, ์ฒด์งˆ๋Ÿ‰์ง€์ˆ˜) = g 2 ๋ฅผ ๋ณ€์ˆ˜์™€ ์ œ๊ณฑ ์Šน ๋“ฑ์„ ์ด์šฉํ•˜์—ฌ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ž…๋ ฅํ•œ๋‹ค. ํ‚ค๋ณด๋“œ๋กœ ์ง์ ‘ ์ž…๋ ฅํ•˜๋ฉด ์˜คํƒ€๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋งˆ์šฐ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒํ•œ๋‹ค. ๋ณ€์ˆ˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ ํ™•์ธ ์ƒ์„ฑ๋œ ๋ณ€์ˆ˜๋ฅผ ํ…Œ์ดํ„ฐ ๋ณด๊ธฐ(Data Variable) ์‹œํŠธ์—์„œ ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋”ฉ ๋ณ€๊ฒฝ ์ฝ”๋”ฉ ๋ณ€๊ฒฝ์€ ๊ธฐ์กด์— ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋ช‡ ๊ฐœ์˜ ๋ฒ”์ฃผ๋กœ ๋‚˜๋ˆ„์–ด ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค. ์ฝ”๋”ฉ ๋ณ€๊ฒฝ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๊ฐ™์€ ๋ณ€์ˆ˜๋กœ ์ฝ”๋”ฉ ๋ณ€๊ฒฝ(Into Same Variables) : ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ณ€๊ฒฝํ•˜๋Š” ๋ณ€์ˆ˜๋กœ ๋ณ€ํ•จ. ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ๋ณ€์ˆซ๊ฐ’์€ ์‚ฌ๋ผ์ง ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋กœ ์ฝ”๋”ฉ ๋ณ€๊ฒฝ(Into Different Variables) : ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ๋ณ€์ˆซ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ณ€์ˆซ๊ฐ’ ์ƒ์„ฑ ๋‹ค์Œ์€ ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋กœ ์ฝ”๋”ฉ ๋ณ€๊ฒฝํ•˜๋Š” ๊ณผ์ •์ด๋‹ค. "๋ณ€ํ™˜(Transform)->๋‹ค๋ฅธ ๋ณ€์ˆ˜๋กœ ์ฝ”๋”ฉ ๋ณ€๊ฒฝ(Into Different Variables)" ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. bmi ๋ณ€์ˆ˜๋ฅผ ์„ ํƒํ•˜๊ณ  ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ณ€๊ฒฝํ•  ๋ณ€์ˆ˜๊ฐ€ ์ถ”๊ฐ€๋˜๊ณ  ์ถœ๋ ฅ ๋ณ€์ˆ˜(Output Variable)์— ๋ฐ”๋€” ์ด๋ฆ„์„ ์ž…๋ ฅํ•œ ํ›„ "๋ฐ”๊พธ๊ธฐ(Change)" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. "๊ธฐ์กด ๊ฐ’ ๋ฐ ์ƒˆ๋กœ์šด ๊ฐ’(Old and New Values)" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ๋ณ€์ˆ˜(bmi)์— ์—ฌ๋Ÿฌ ์กฐ๊ฑด์„ ์ž…๋ ฅํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์กฐ๊ฑด์„ ์ฃผ๋Š” ๊ณผ์ •์€ ๋‹ค์Œ ํ™”๋ฉด์— ์žˆ๋‹ค. ๊ธฐ์กด ๊ฐ’ ๋ฐ ์ƒˆ๋กœ์šด ๊ฐ’ BMI ๋ณ€์ˆ˜๋ฅผ ๋ฒ”์œ„์— ๋”ฐ๋ผ 6๊ฐœ ๋“ฑ๊ธ‰์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ณผ์ •์ด๋‹ค. ์ด ํ™”๋ฉด์—์„œ bmi ๋ณ€์ˆ˜๋Š” ์ตœ์†Ÿ๊ฐ’์—์„œ 18.5๋Š” 1 18.5์—์„œ 23 ์€ 2 23์—์„œ 25๋Š” 3 25์—์„œ 30 ์€ 4 30์—์„œ 35๋Š” 5 35์—์„œ ์ตœ๋Œ“๊ฐ’๊นŒ์ง€๋Š” 6 ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋“ฑ๊ธ‰์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ณผ์ •์€ "๊ธฐ์กด ๊ฐ’(Old Value)->๋ฒ”์œ„(Range)"๋ฅผ ์„ ํƒ "๋ฒ”์œ„(Range)->์ตœ์ “๊ฐ’์—์„œ ๋‹ค์Œ ๊ฐ’๊นŒ์ง€ ๋ฒ”์œ„(Lowest through)" ์ž…๋ ฅ์ฐฝ์— "18.5"๋ฅผ ์ž…๋ ฅ "์ƒˆ๋กœ์šด ๊ฐ’(New Value)->๊ธฐ์ค€๊ฐ’(Value)"์„ ์„ ํƒํ•˜๊ณ  1์„ ์ž…๋ ฅ "์ถ”๊ฐ€(Add)" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ์ด๊ณ  ๊ฐ ๋ฒ”์ฃผ์— ๋Œ€ํ•˜์—ฌ ๋™์ผํ•œ ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด ์—ฐ์†ํ˜• ์ž๋ฃŒ๋ฅผ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋”ฉ ๋ณ€๊ฒฝ ๊ฒฐ๊ณผ ํ™•์ธ ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ๋ณ€์ˆ˜์—์„œ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋กœ ์ž๋ฃŒ๋ฅผ ์ƒ์„ฑํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. ๋ณ€์ˆซ๊ฐ’ ์„ค๋ช… - ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ์—์„œ ์ด๋ฆ„์ด ๋“ฑ๊ธ‰์ธ ๋ ˆ์ฝ”๋“œ(ํ–‰)์—์„œ ๊ฐ’ ํ•„๋“œ(์—ด)์ด ๊ต์ฐจํ•˜๋Š” ์—†์Œ์˜ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ์ƒ์„ฑ๋œ ๋“ฑ๊ธ‰ ๋ณ€์ˆ˜๋Š” 1์—์„œ 6์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์ €์žฅ๋˜์—ˆ์œผ๋ฉฐ ์ด ๊ฐ’๋“ค์€ ๊ฐ๊ฐ 1 ์€ ์ €์ฒด์ค‘ 2๋Š” ์ •์ƒ 3 ์€ ๊ณผ์ฒด์ค‘ 4๋Š” 1 ๋‹จ๊ณ„ ๋น„๋งŒ 5๋Š” 2 ๋‹จ๊ณ„ ๋น„๋งŒ 6 ์€ 3 ๋‹จ๊ณ„ ๋น„๋งŒ ์œผ๋กœ ํ†ต๊ณ„๋ถ„์„ ๊ฒฐ๊ณผ์—์„œ ์ถœ๋ ฅ๋˜๋„๋ก ์„ค์ •ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™๋‹ค. SPSS ๋ช…๋ น ์ฝ”๋“œ * Encoding: UTF-8. COMPUTE bmi=Weight / (Height / 100) ** 2. EXECUTE. RECODE bmi (Lowest thru 18.5=1) (18.5 thru 23=2) (23 thru 25=3) (25 thru 30=4) (30 thru 35=5) (35 thru Highest=6) INTO ๋“ฑ๊ธ‰. EXECUTE. VALUE LABELS ๋“ฑ๊ธ‰ 1 "์ €์ฒด์ค‘" 2 "์ •์ƒ" 3 "๊ณผ์ฒด์ค‘" 4 " 1๋‹จ๊ณ„ ๋น„๋งŒ" 5 " 2๋‹จ๊ณ„ ๋น„๋งŒ" 6 " 3๋‹จ๊ณ„ ๋น„๋งŒ". 2. ํ…์ŠคํŠธ ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ SPSS ๋ฐ์ดํ„ฐ ํŒŒ์ผ(ํ™•์žฅ์ž. SAV)์ด ์•„๋‹Œ ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ณผ์ •์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด์ž. ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด "ํŒŒ์ผ(File)->์—ด๊ธฐ(Open)->๋ฐ์ดํ„ฐ(Data)" ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ถˆ๋Ÿฌ์˜ฌ ํŒŒ์ผ์ด ์žˆ๋Š” ํด๋”๋กœ ์ด๋™ํ•œ๋‹ค. "ํŒŒ์ผ<NAME>"์„ "๋ชจ๋“  ํŒŒ์ผ"๋กœ ๋ฐ”๊พธ์–ด ๋ชจ๋“  ํŒŒ์ผ ์ค‘์—์„œ ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณด์ด๋„๋ก ํ•œ๋‹ค. ํ•ด๋‹น ํŒŒ์ผ์„ ์„ ํƒํ•œ ํ›„ "์—ด๊ธฐ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ ๋งˆ๋ฒ•์‚ฌ "ํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ ๋งˆ๋ฒ•์‚ฌ 6๋‹จ๊ณ„ ์ค‘ 1๋‹จ๊ณ„(Text Import Wizard)" ํ™”๋ฉด์ด ๋ณด์ธ๋‹ค. ์„ค์ •๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ๋ง๊ณ  "๋‹ค์Œ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. "ํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ ๋งˆ๋ฒ•์‚ฌ 2๋‹จ๊ณ„(Text Import Wizard)"์—์„œ๋Š” SPSS ์…€์— ๋“ค์–ด๊ฐˆ ์ž๋ฃŒ๋ฅผ ๊ณต๋ฐฑ์ด๋‚˜ ํƒญ ๋“ฑ ๊ตฌ๋ถ„์ž๋กœ ํ•  ๊ฒƒ์ธ๊ฐ€ ํ•œ ์…€์— ๋“ค์–ด๊ฐˆ ์ž๋ฃŒ์˜ ํฌ๊ธฐ๋ฅผ "๊ตฌ๋ถ„์ž์— ์˜ํ•œ ๋ฐฐ์—ด(Delimited)" "๊ณ ์ • ๋„ˆ๋น„๋กœ ๋ฐฐ์—ด(Fixed Width)" ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๊ณ , ๋ถˆ๋Ÿฌ๋“ค์ผ ํŒŒ์ผ์˜ ์ฒซ ํ–‰์— ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ์žˆ๋Š”์ง€ ์„ ํƒํ•œ๋‹ค. ์„ค์ •๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ๋ง๊ณ  "๋‹ค์Œ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ๊ตฌ๋ถ„์ž์— ์˜ํ•œ ๋ฐฐ์—ด(Delimited)์„ ์„ ํƒํ•œ ๊ฒฝ์šฐ "ํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ ๋งˆ๋ฒ•์‚ฌ 3๋‹จ๊ณ„(Text Import Wizard)"์—์„œ๋Š” ๋ช‡ ์ค„๋ถ€ํ„ฐ ๋ช‡ ์ค„๊นŒ์ง€ ์ž๋ฃŒ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ธ์ง€ ์„ค์ •ํ•˜๋Š” ๋‹จ๊ณ„์ด๋‹ค. ์„ค์ •๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  "๋‹ค์Œ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. "ํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ ๋งˆ๋ฒ•์‚ฌ 4๋‹จ๊ณ„(Text Import Wizard)"์—์„œ๋Š” ๊ตฌ๋ถ„์ž๋ฅผ ์„ค์ •ํ•˜๋Š” ๋‹จ๊ณ„์ด๋‹ค. ๊ฐ ๋ณ€์ˆ˜๋ฅผ ๊ตฌ๋ถ„ํ•  ๊ตฌ๋ถ„์ž๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์ง€์ •ํ•ด๋„ ๋œ๋‹ค. ์„ค์ •๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ๋ง๊ณ  "๋‹ค์Œ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. "ํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ ๋งˆ๋ฒ•์‚ฌ 5๋‹จ๊ณ„(Text Import Wizard)"์—์„œ๋Š” "๋ณ€์ˆ˜ ์ด๋ฆ„(Variable Name)"๊ณผ "๋ฐ์ดํ„ฐ<NAME>(Data Format)"์„ ์ง€์ •ํ•˜๋Š” ๋‹จ๊ณ„์ด๋‹ค. ๊ทธ ์ด์ „ ๋‹จ๊ณ„์—์„œ ์ด๋ฏธ ์ง€์ •๋˜์–ด์•ผ๋งŒ ์ด ๋‹จ๊ณ„์—์„œ ์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์„ค์ •๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  "๋‹ค์Œ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. "ํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ ๋งˆ๋ฒ•์‚ฌ 6๋‹จ๊ณ„(Text Import Wizard)"์—์„œ๋Š” ๋ถˆ๋Ÿฌ์˜จ ํŒŒ์ผ์„ ์ €์žฅํ•  ๊ฒƒ์ธ์ง€ ์„ค์ •ํ•˜๊ณ , SPSS ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฝ์ž…ํ•  ๊ฒƒ์ธ์ง€ ์„ค์ •ํ•˜๋Š” ๋‹จ๊ณ„์ด๋‹ค. ์„ค์ •๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  "๋งˆ์นจ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ๋งˆ์น˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚œ๋‹ค. SPSS ํŒŒ์ผ<NAME>์œผ๋กœ ์ €์žฅ ๋ถˆ๋Ÿฌ์˜จ ํŒŒ์ผ์„ SPSS ๋ฐ์ดํ„ฐ<NAME>(ํ™•์žฅ์ž. SAV)๋กœ ์ €์žฅํ•œ๋‹ค. ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ์—์„œ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๊ฐ’์— ๋ ˆ์ด๋ธ” ์ถ”๊ฐ€ "๋ณ€์ˆ˜ ๋ณด๊ธฐ(Variable View)" ์‹œํŠธ์—์„œ ์ด๋ฆ„์— ๋ณ€์ˆ˜๋ช…์„ "ํ‚ค", "๋ชธ๋ฌด๊ฒŒ", "์ถœ์ƒ์—ฐ๋„", "์ข…๊ต", "์„ฑ๋ณ„", "๊ฒฐํ˜ผ ์—ฌ๋ถ€"๋กœ ์ž…๋ ฅํ•œ๋‹ค. ๊ฐ’์— ์ข…๊ต๋Š” 1 ์€ ๋ถˆ๊ต, 2๋Š” ๊ธฐ๋…๊ต, 3 ์€ ๊ฐ€ํ†จ๋ฆญ, 4๋Š” ์—†์Œ ์„ฑ๋ณ„์— 1 ์€์—ฌ, 2๋Š” ๋‚จ ๊ฒฐํ˜ผ ์—ฌ๋ถ€์— 1 ์€ ๊ธฐํ˜ผ, 2๋Š” ๋ฏธํ˜ผ ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. ๋ณ€์ˆซ๊ฐ’ ์„ค๋ช… - ๋ณ€์ˆ˜์— ๋ ˆ์ด๋ธ” ์ž…๋ ฅ ๋ณ€์ˆซ๊ฐ’์„ ์„ค๋ช…ํ•˜๋Š” ํ™”๋ฉด์ด๋‹ค. "๋ณ€์ˆ˜ ๋ณด๊ธฐ(Variable View)" ์‹œํŠธ์—์„œ "๊ฐ’(Value)"์„ ์„ ํƒํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚œ๋‹ค. ์—ฌ๊ธฐ์— ์ ๋‹นํ•œ ๊ฐ’์„ "์ถ”๊ฐ€(Add)" ํ•˜๊ณ  ์ž…๋ ฅ์ด ์™„๋ฃŒ๋˜๋ฉด "ํ™•์ธ(OK)" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. SPSS ๋ฐ์ดํ„ฐ<NAME>์œผ๋กœ ์ €์žฅ ๋ถˆ๋Ÿฌ์˜จ ์ž๋ฃŒ์˜ ์„ค์ •์ด ์™„๋ฃŒ๋˜๋ฉด ์ €์žฅํ•˜์—ฌ ์ดํ›„์— ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํŠน์ •ํ•œ ํด๋”๋ฅผ ์„ ํƒํ•œ ํ›„ SPSS ํŒŒ์ผ๋กœ ์ €์žฅํ•œ๋‹ค. SPSS ๋ช…๋ น ์ฝ”๋“œ * Encoding: UTF-8. RENAME VARIABLES (V1=ํ‚ค) (V2=๋ชธ๋ฌด๊ฒŒ) (V3=์ถœ์ƒ์—ฐ๋„) (V4=์ข…๊ต) (V5=์„ฑ๋ณ„) (V6=๊ฒฐํ˜ผ ์—ฌ๋ถ€). VALUE LABELS ์ข…๊ต 1 "๋ถˆ๊ต" 2 "๊ธฐ๋…๊ต" 3 "๊ฐ€ํ†จ๋ฆญ" 4 "์—†์Œ". VALUE LABELS ์„ฑ๋ณ„ 1 "์—ฌ" 2 "๋‚จ". VALUE LABELS ๊ฒฐํ˜ผ ์—ฌ๋ถ€ 1 "๊ธฐํ˜ผ" 2 "๋ฏธํ˜ผ". 3. ๋ฐ์ดํ„ฐ ์ž…๋ ฅ, ์ˆ˜์ •, ๋ถ„์„, ์ €์žฅ SPSS ์‹คํ–‰ ํ™”๋ฉด SPSS๋ฅผ ์ฒ˜์Œ์œผ๋กœ ์‹คํ–‰ํ•œ ํ™”๋ฉด์ด๋‹ค. ์‹œํŠธ๋Š” ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ์™€ ๋ณ€์ˆ˜ ๋ณด๊ธฐ๊ฐ€ ์žˆ๋‹ค. ํ™”๋ฉด์€ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ์ด๋‹ค. ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ ์ดˆ๊ธฐ ํ™”๋ฉด์ด๋‹ค. ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ ์„ ํƒ์€ ํ”„๋กœ๊ทธ๋žจ ์•„๋ž˜์—์„œ ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ๋ฅผ ๋งˆ์šฐ์Šค๋กœ ํด๋ฆญํ•œ๋‹ค. ์ž๋ฃŒ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ์— ์ž๋ฃŒ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ์ž๋ฃŒ์ž…๋ ฅ์€ ๋ฐ˜๋“œ์‹œ ์—ด๋กœ ์ž…๋ ฅํ•œ๋‹ค. ํ–‰์œผ๋กœ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹˜์— ์ฃผ์˜ํ•œ๋‹ค. ์•„๋ž˜ ํ™”๋ฉด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ผ์–ด๋‚˜๋Š” ํ˜„์ƒ ๋ณ€์ˆ˜์— ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ๋ณ€์ˆ˜๋ช…์ด ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค. ์†Œ์ˆ˜์  ์ดํ•˜ 2์ž๋ฆฌ๊ฐ€ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ง„๋‹ค. ๋ฌธ์ž ์ž…๋ ฅ์€ ํ•œ๊ธ€ 1๊ฐœ ๋ฌธ์ž์™€ ์˜์–ด 2๊ฐœ ๋ฌธ์ž๋งŒ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฌธ์ž๋Š” "๊ฐ•์›๋„, ๊ฐ•์›๋„, ๊ฒฝ๊ธฐ๋„, ์„œ์šธ"์„ ์ž…๋ ฅํ–ˆ์ง€๋งŒ ํ•œ ๋ฌธ์ž๋งŒ ์ž…๋ ฅ๋˜์—ˆ๋‹ค. ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ์—์„œ ์†์„ฑ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ ํ™”๋ฉด ๊ฐ’์„ ์ž…๋ ฅํ•œ ๊ฒฐ๊ณผ ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ์— ๋ณ€๊ฒฝ๋œ ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ด๋ฆ„์ด VAR0001๋กœ ์‹œ์ž‘ํ•œ๋‹ค ์œ ํ˜•์ด ์ˆซ์ž์™€ ๋ฌธ์ž๋กœ ์„ค์ •๋œ๋‹ค. ๋„ˆ๋น„๋Š” ์ž๋ฃŒ ์ž…๋ ฅ ๋ฒ”์œ„ ์„ค์ •์ด๋‹ค. ์—ด์€ ํ™”๋ฉด์—์„œ ๋ณด์ด๋Š” ์—ด์˜ ๋„ˆ๋น„(width)์ด๋‹ค. ๋ณ€์ˆ˜ ๋ณด๊ธฐ์—์„œ ์†์„ฑ ๊ฐ’ ์„ค์ • ์ด๋ฆ„์€ ์ข…๊ต, ์‹ ์žฅ, ์ง€์—ญ์œผ๋กœ ๋ณ€๊ฒฝ ๋ฌธ์ž ๋ณ€์ˆ˜ ๋„ˆ๋น„๋ฅผ 8๋กœ ๋ณ€๊ฒฝ ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฟ์ˆ˜๋ฅผ 2์—์„œ 0์œผ๋กœ ๋ณ€๊ฒฝ ๋ฌธ์ž ๋ณ€์ˆ˜ ์—ด ๋„ˆ๋น„๋ฅผ 8๋กœ ๋ณ€๊ฒฝ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ์—์„œ ์†์„ฑ ๊ฐ’ ๋ณ€๊ฒฝ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ์—์„œ ํ™•์ธ ๋ฐ์ดํ„ฐ ์ €์žฅ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ์ €์žฅ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ SPSS<NAME>์œผ๋กœ ์ €์žฅํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ์ €์žฅํ•  ๋•Œ ํ™•์žฅ์ž๋Š”. SAV์ด๋‹ค. ๊ธฐ์ˆ ํ†ต๊ณ„๋Ÿ‰ ๊ตฌํ•˜๊ธฐ ๊ฐ„๋‹จํ•œ ๋ถ„์„์„ ํ•˜๊ณ  ๊ทธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜์ž. ๊ธฐ์ˆ  ํ†ต๊ณ„ ๋ฉ”๋‰ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๋ณ€์ˆ˜ ์„ ํƒ ์‹ ์žฅ์„ ์„ ํƒํ•˜๊ณ  ํ™”์‚ดํ‘œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ณ€์ˆ˜์— ์‹ ์žฅ ๋ณ€์ˆ˜๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๊ธฐ์ˆ  ํ†ต๊ณ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์ด๋‹ค ์ถœ๋ ฅ๋ฌผ์€ ๋ช…๋ น์–ด์™€ ๋ถ„์„ ๊ฒฐ๊ณผ์ด๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ €์žฅ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ฐฝ์—์„œ ํŒŒ์ผ->์ €์žฅ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ €์žฅ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์ €์žฅํ•˜๊ณ  ์ดํ›„ ๋ณ„๋„๋กœ ์‹คํ–‰ํ•˜๋ฉด ๊ทธ ๋‚ด์šฉ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ถœ๋ ฅ ํŒŒ์ผ ํ™•์žฅ์ž๋Š”. SPV์ด๋‹ค. ๋ช…๋ น๋ฌธ ๋ฐ์ดํ„ฐ ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ์ถœ๋ ฅ ํ”„๋กœ๊ทธ๋žจ์— ์ƒ๊ด€์—†์ด ์ƒˆ ํŒŒ์ผ -> ๋ช…๋ น๋ฌธ์„ ์‹คํ–‰ํ•œ๋‹ค. ๋ช…๋ น๋ฌธ ๋ช…๋ น๋ฌธ ์‹คํ–‰ ํ™”๋ฉด์ด๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ช…๋ น๋ฌธ์„ ๋ณต์‚ฌํ•œ๋‹ค. ๋ช…๋ น๋ฌธ์€ ๋กœ๊ทธ๋ฅผ ๋ˆŒ๋Ÿฌ๋„ ์„ ํƒ๋œ๋‹ค. ๋ช…๋ น๋ฌธ ๋ช…๋ น๋ฌธ ์ฐฝ์— ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•œ๋‹ค. ๋ช…๋ น๋ฌธ ์‹คํ–‰ ๋ช…๋ น๋ฌธ ์‹คํ–‰์€ ๋ช…๋ น์–ด๋ฅผ ๋งˆ์šฐ์Šค๋กœ ์„ ํƒํ•˜๊ณ , ์„ ํƒ์˜์—ญ ์‹คํ–‰ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ช…๋ น๋ฌธ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์ถœ๋ ฅ์ฐฝ์—์„œ ํ™•์ธํ•œ๋‹ค. ๋ช…๋ น๋ฌธ ๋ช…๋ น๋ฌธ ์ €์žฅ์€ ์ƒˆ ํŒŒ์ผ -> ์ €์žฅ์„ ์‹คํ–‰ํ•œ๋‹ค. ๋ช…๋ น๋ฌธ ๋ช…๋ น๋ฌธ ํ™•์žฅ์ž๋Š”. SPS์ด๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋Œ€๋ถ€๋ถ„ ํ‘œ์™€ ๊ทธ๋ฆผ์ด๋ฉฐ ์ด๊ฒƒ๋“ค์€ ๋ณต์‚ฌํ•˜์—ฌ ๋‹ค๋ฅธ ์‘์šฉํ”„๋ฅด๊ณ ๋žจ์— ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์—‘์…€์— ๋ถ™์—ฌ๋„ฃ๊ธฐ SPSS์—์„œ ์ถœ๋ ฅํ•œ ํ‘œ๋ฅผ ์—‘์…€์— ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜์˜€๋‹ค. ์•„๋ž˜ํ•œ๊ธ€์— ๋ถ™์—ฌ๋„ฃ๊ธฐ SPSS์—์„œ ์ถœ๋ ฅํ•œ ํ‘œ๋ฅผ ์•„๋ž˜ํ•œ๊ธ€์— ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜์˜€๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์—‘์…€ํŒŒ์ผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ ๋‚ด๋ณด๋‚ผ ๊ฐœ์ฒด๋ฅผ Ctrl ํ‚ค๋ฅผ ๋ˆ„๋ฅด๊ณ  ์„ ํƒํ•œ๋‹ค. ์„ ํƒ๋œ ๊ฐœ์ฒด๋Š” ๋…ธ๋ž€์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ๋‹ค. ๋‚ด๋ณผ๋‚ผ ๊ฐœ์ฒด๋Š” ์„ ํƒ, ์œ ํ˜•์€ Excel 2007 ์ด์ƒ, ์ฐพ์•„๋ณด๊ธฐ๋ฅผ ๋ˆ„๋ฅธ๋‹ค. ์ €์žฅํ•  ํด๋”์™€ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•œ๋‹ค. ์ €์žฅํ•  ํด๋”์™€ ํŒŒ์ผ๋ช…์„ ํ™•์ธํ•œ๋‹ค. ์ €์žฅํ•œ ์—‘์…€ ํŒŒ์ผ์„ ์—ด์–ด ๋‚ด์šฉ์„ ํ™•์ธํ•œ๋‹ค. ์—ฐ์Šต๋ฌธ์ œ ๋‹ค์Œ ํ‘œ๋Š” Titanic ์ž๋ฃŒ ์ผ๋ถ€์ด๋‹ค. pclass survived sex age sibsp parch fare 1 1 female 29 0 0 211.3375 1 0 female 2 1 2 151.5500 1 0 male 30 1 2 151.5500 1 0 female 25 1 2 151.5500 1 1 male 48 0 0 26.5500 2 0 male 44 0 0 13.0000 2 1 female 6 0 1 33.0000 2 0 male 28 0 1 33.0000 2 1 male 62 0 0 10.5000 2 0 male 30 0 0 10.5000 2 1 female 7 0 2 26.2500 3 0 female 6 4 2 31.2750 3 0 female 2 4 2 31.2750 3 1 female 17 4 2 7.9250 3 0 female 38 4 2 7.7750 3 0 female 9 4 2 31.2750 3 0 female 11 4 2 31.2750 3 0 male 39 1 5 31.2750 3 1 male 27 0 0 7.7958 3 0 male 26 0 0 7.7750 3 0 female 39 1 5 31.2750 Titanic 1 SPSS์— ํ‘œ์— ์žˆ๋Š” ์ž๋ฃŒ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. sex ๋ณ€์ˆ˜๋Š” ์ˆซ์ž๋กœ ์ž…๋ ฅํ•˜๊ณ  ๊ฐ’์—์„œ ๋ ˆ์ด๋ธ”์„ ๋„ฃ๋Š”๋‹ค. ์•„๋ž˜ ๊ฐ’์„ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์ž…๋ ฅํ•˜๊ณ  pclass, survied ๋ณ€์ˆ˜๋Š” ๊ฐ’์— ๋ ˆ์ด๋ธ”์„ ๋„ฃ๋Š”๋‹ค. pclass: ๊ฐ์‹ค ๋“ฑ๊ธ‰(1=Upper, 2=Middle, 3=Lower) survived ์ƒ์กด ์—ฌ๋ถ€(0=์ฃฝ์Œ, 1=์ƒ์กด) sex: ์„ฑ๋ณ„ age:๋‚˜์ด sibsp:๊ฐ™์ด ํƒ„ ํ˜•์ œ์ž๋งค์™€ ๋ฐฐ์šฐ์ž ์ˆซ์ž parch: ๊ฐ™์ด ํƒ„ ๋ถ€๋ชจ์™€ ์ž์‹ ์ˆซ์ž fare:์š”๊ธˆ 2 ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋Š” titanic.sav๋กœ ์ €์žฅํ•œ๋‹ค. 3 pclass, survived, sex ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๋นˆ๋„ ๋ถ„์„์„ ํ•œ๋‹ค. 4 pclass, survived, sex ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์›๊ทธ๋ฆผ์„ ๊ทธ๋ฆฐ๋‹ค. 5 age, sibsp, parch, fare ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๊ธฐ์ˆ  ํ†ต๊ณ„๋ฅผ ๊ตฌํ•œ๋‹ค. 6 ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” titanic.spv๋กœ ์ €์žฅํ•œ๋‹ค. 7 ์‹คํ–‰ํ•œ ๋ช…๋ น๋ฌธ์€ titanic.sps๋กœ ์ €์žฅํ•œ๋‹ค. 8 titanic.sps ํŒŒ์ผ์— ์žˆ๋Š” ๋ช…๋ น๋ฌธ์„ ์‹คํ–‰ํ•œ๋‹ค. 9 ๋นˆ๋„ ๋ถ„์„, ์›๊ทธ๋ฆผ, ๊ธฐ์ˆ  ํ†ต๊ณ„๋ฅผ titanic.xlsx ์—‘์…€ํŒŒ์ผ๋กœ ๋‚ด๋ณด๋‚ธ๋‹ค. ๋‹จ ๋กœ๊ทธ ์ œ์™ธ 10 ๋งŒ๋“  4๊ฐœ ํŒŒ์ผ์„ titanic.zip๋กœ ์••์ถœํ•˜์—ฌ ๊ณผ์ œ๋กœ ์ œ์ถœํ•œ๋‹ค. 4. ๊ฐ€์ค‘์น˜ ์„ค์ • ๋ณ€์ˆ˜์— ๊ฐ€์ค‘์น˜ ์„ค์ • ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ์ธ ๊ฒฝ์šฐ ์ž๋ฃŒ๊ฐ’์ด ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๊ทธ ์˜ˆ๋กœ ์–ด๋–ค ์—ฌ๋ก  ์กฐ์‚ฌ์—์„œ ์ฐฌ์„ฑ๊ณผ ๋ฐ˜๋Œ€๋ฅผ ๋ฌป๋Š” ๊ฒฝ์šฐ, ๊ทธ ๊ฐ’์€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณต๋˜์–ด ์กฐ์‚ฌ๋  ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์š”์•ฝ๋œ ๊ฐ’์œผ๋กœ ์ฆ‰, ์ฐฌ์„ฑ์ด ๋ช‡ ๋ช…์ด๊ณ  ๋ฐ˜๋Œ€๊ฐ€ ๋ช‡ ๋ช…์œผ๋กœ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์ค‘์น˜ ์„ค์ •์ด๋‹ค. ์ž๋ฃŒ ์ž…๋ ฅ ๊ฐ ์…€์— ๊ฐ’์„ ํ™”๋ฉด๊ณผ ๊ฐ™์ด ์ž…๋ ฅํ•œ๋‹ค. ์ด๋ฆ„ ๋ณ€๊ฒฝ๊ณผ ๊ฐ’ ์ถ”๊ฐ€ ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ์—์„œ ์ด๋ฆ„๊ฐ’์„ ์„ฑ๋ณ„, ์ธ์›์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ์„ฑ๋ณ„ ํ–‰๊ณผ ๊ฐ’ ์—ด์ด ๊ต์ฐจํ•˜๋Š” ์…€์—์„œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ๊ฐ’ ๋ ˆ์ด๋ธ” ๊ธฐ์ค€๊ฐ’์— ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ์—์„œ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์ž…๋ ฅํ•˜๊ณ , ๋ ˆ์ด๋ธ”์— ๊ฒฐ๊ณผ ์ถœ๋ ฅ์—์„œ ๋ณผ ๊ฐ’์„ ์ž…๋ ฅํ•œ๋‹ค. ์ฆ‰ 1์€ ๋‚จ์ž, 2๋Š” ์—ฌ์ž๋กœ ๋ณด์ธ๋‹ค. ๊ฐ’ ๋ ˆ์ด๋ธ” ๊ฐ’ ์ž…๋ ฅ์„ ๋งˆ์ณค์œผ๋ฉด ํ™•์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฟ์ˆ˜์™€ ๊ฐ’ ๋ ˆ์ด๋ธ” ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฟ์ˆ˜๋Š” 0, ๊ฐ’์€ ๋ณ€๊ฒฝํ•œ ๊ฐ’์ด ๋ณด์ธ๋‹ค. ์ˆ˜์ •์„ ์›ํ•˜๋ฉด ๊ฐ’์˜ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ๊ฐ’ ๋ ˆ์ด๋ธ” ๋ณ€๊ฒฝ ํ™”๋ฉด ์„ฑ๋ณ„์—์„œ 1์€ ๋‚จ์ž, 2๋Š” ์—ฌ์ž๋กœ ๋ณด์ธ๋‹ค. ์ด ํ™”๋ฉด์ด ๋ณด์ด์ง€ ์•Š์œผ๋ฉด ํ‘œ์ค€ ๋„๊ตฌ์—์„œ ๊ฐ’ ๋ ˆ์ด๋ธ” ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ๋นˆ๋„ ๋ถ„์„ ๋นˆ๋„ ๋ถ„์„์„ ์‹ค์‹œํ•œ๋‹ค. ๋นˆ๋„ ๋ถ„์„์— ๋ณ€์ˆ˜ ์ถ”๊ฐ€ ๋นˆ๋„ ๋ถ„์„ํ•  ์„ฑ๋ณ„ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ๋นˆ๋„ ์ ์šฉ ์•ˆ๋จ ๊ฐ€์ค‘์น˜๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š์•„ ๋นˆ๋„๊ฐ€ ๊ฐ๊ฐ 1๋กœ ์ถœ๋ ฅ๋œ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ 100์ด๋‹ค. ๊ฐ€์ค‘ ์ผ€์ด์Šค ๋ฉ”๋‰ด ๊ฐ€์ค‘ ์ผ€์ด์Šค ๋ฉ”๋‰ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๊ฐ€์ค‘์น˜ ์ ์šฉํ•  ๋ณ€์ˆ˜ ์„ค์ • ๊ฐ€์ค‘์น˜ ์ ์šฉ ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค. ๋นˆ๋„ ๋ถ„์„ ๋นˆ๋„ ๋ถ„์„ ๋ฉ”๋‰ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๊ฐ€์ค‘์น˜ ์ ์šฉํ•œ ๋นˆ๋„ ๋ถ„์„ ๊ฐ€์ค‘์น˜๊ฐ€ ์ ์šฉ๋ผ ๋ณ€์ˆ˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 5. ๋ฐ์ดํ„ฐ ์„ ํƒ ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ์ผ๋ถ€๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„์„ํ•  ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋•Œ ๋ฐ์ดํ„ฐ ์„ ํƒ ๋ฉ”๋‰ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•˜๋‹ค. ๋ฐ์ดํ„ฐ ์„ ํƒ์€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ -> ์ผ€์ด์Šค ์„ ํƒ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ์‹ค์Šต์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•œ๋‹ค. ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ผ€์ด์Šค์—์„œ ์กฐ๊ฑด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ์‹ ์ž…๋ ฅ ๋ฐ•์Šค์— ๋ชธ๋ฌด๊ฒŒ๊ฐ€ 40 ๋ฏธ๋งŒ์ด๊ฑฐ๋‚˜ 68 ์ดˆ๊ณผ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ์ž…๋ ฅํ•  ๋•Œ ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๋ ค๋ฉด ๋งˆ์šฐ์Šค๋ฅผ ์ด์šฉํ•œ๋‹ค. ์„ ํƒ๋œ ์ผ€์ด์Šค๋Š” filter_$ ๋ณ€์ˆ˜์— Selected ๊ฐ’๊ณผ ํ•จ๊ป˜ ํ‘œ์‹œ๋˜๊ณ  ์„ ํƒ๋˜์ง€ ์•Š์€ ์ผ€์ด์Šค๋Š” filter_$ ๋ณ€์ˆ˜์— Not Selected ๊ฐ’์ด ํ‘œ์‹œ๋˜๊ณ , ๋ฒˆํ˜ธ์— ์‚ฌ์„ ์ด ํ‘œ์‹œ๋œ๋‹ค. 6. ์‹ค์Šต์ž๋ฃŒ ๋‹ค์Œ์€ ์„ฑ๋ณ„์— ๋”ฐ๋ฅธ ๊ต๋‚ด ํก์—ฐ์žฅ์†Œ์— ๋Œ€ํ•œ ์˜๊ฒฌ์ด๋‹ค. ์„ฑ๋ณ„์— ๋”ฐ๋ผ ํก์—ฐ์žฅ์†Œ์— ๋Œ€ํ•œ ์˜๊ฒฌ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๋ ค๊ณ  ์กฐ์‚ฌํ•˜์˜€๋‹ค. SPSS์— ์ž๋ฃŒ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๊ต์ฐจ๋ถ„์„ํ•˜๊ณ  ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์ ๋Š”๋‹ค. ๋ถ„์„์€ ๋ถ„์„์ž๊ฐ€ ์ ํ•ฉํ•˜๊ฒŒ ํ–‰ ๋˜๋Š” ์—ด์— ๋Œ€ํ•œ ๋ฐฑ๋ถ„์œจ์„ ์ถœ๋ ฅํ•˜๊ณ  ์ž๊ธฐ ์˜๊ฒฌ์„ ์ ๋Š”๋‹ค. ์„ฑ๋ณ„ ํก์—ฐ์žฅ์†Œ ์„ค์น˜ ํ•ฉ๊ณ„ ๋„ˆ๋ฌด ๋งŽ๋‹ค ์ ๋‹นํ•˜๋‹ค ๋„ˆ๋ฌด ์ ๋‹ค ๋‚จ์ž 378 247 21 646 ์—ฌ์ž 388 186 30 604 ํ•ฉ๊ณ„ 766 433 51 1250 ๊ต๋‚ด ํก์—ฐ์žฅ์†Œ ์˜๊ฒฌ ์—ฌ๊ธฐ๋ฅผ ๋ˆŒ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•œ๋‹ค. ์ฒซ ํ–‰์€ ๋ณ€์ˆ˜๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ์„ฑ๋ณ„ 1์€ '๋‚จ์ž', 2๋Š” '์—ฌ์ž'์ด๊ณ  ์˜๊ฒฌ 1์€ '๋„ˆ๋ฌด ๋งŽ๋‹ค', 2๋Š” '์ ๋‹นํ•˜๋‹ค', 3์€ '๋„ˆ๋ฌด ์ ๋‹ค'๋ฅผ ์ˆซ์ž๋กœ ์ž…๋ ฅํ•˜์˜€๋‹ค. ๊ฐ ๋ณ€์ˆซ๊ฐ’์— ๋ ˆ์ด๋ธ”์„ ํ•œ๋‹ค. ์ด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•˜์—ฌ ๊ต์ฐจ๋ถ„์„์œผ๋กœ ๋ถ„์„ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ ๋Š”๋‹ค. ์„ฑ์ ์— ๋Œ€ํ•œ ์ž๋ฃŒ๊ฐ€ ์—ฌ๊ธฐ์— ์žˆ๋‹ค. ์„ฑ์ ์€ ์ถœ์„ 10%(10์ ), ๊ณผ์ œ 20%(20์ ), ์ค‘๊ฐ„ 30%(100์ ), ๊ธฐ๋ง 40%(100์ )์ด๋‹ค. ๊ด„ํ˜ธ ์•ˆ ๊ฐ’์€ ํ•ด๋‹น ์‹œํ—˜์—์„œ ๋“์ ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๊ณ  ์ ์ˆ˜์ด๋‹ค. ์ด์ ์€ 100์ ์œผ๋กœ ํ™˜์‚ฐํ•˜๊ณ  ๋“ฑ๊ธ‰์„ A+, A, B+, B, C+, C0, D+, D, F๋กœ ๋‚˜๋ˆˆ๋‹ค. 02. ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ ๊ทธ๋ž˜ํ”„(Graph) ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ํžˆ์Šคํ† ๊ทธ๋žจ ์›๊ทธ๋ž˜ํ”„ ์ค„๊ธฐ-์žŽ ๊ทธ๋ฆผ ์‚ฐ์ ๋„ ์ƒ์ž ๊ทธ๋ฆผ 1. ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„(barplot) ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ํŠน์„ฑ ๊ฐ ๋ฒ”์ฃผ์—์„œ ๋„์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๋ง‰๋Œ€๋กœ ๊ทธ๋ฆฐ ๊ทธ๋ž˜ํ”„๋กœ ๋ง‰๋Œ€์˜ ๊ธธ์ด๋Š” ๋„์ˆ˜๋‚˜ ์ƒ๋Œ€๋„์ˆ˜์˜ ์–‘์ด ํ‘œํ˜„ ๊ฐ ๋ฒ”์ฃผ ๊ฐ„ ๋„์ˆ˜ ๋น„๊ต์— ์šฉ์ด ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ, ์ด์‚ฐํ˜• ์ž๋ฃŒ, ์—ฐ์†ํ˜• ์ž๋ฃŒ ๋ชจ๋‘ ๊ฐ€๋Šฅ ์—ฐ์†ํ˜• ์ž๋ฃŒ๋ณด๋‹ค ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ, ์ด์‚ฐํ˜• ์ž๋ฃŒ ํ‘œํ˜„์— ์ ํ•ฉ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ์ž‘์„ฑ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„๋ฅผ ์ž‘์„ฑํ•˜๋Š” ํ™”๋ฉด์ด๋‹ค. ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ์ž‘์„ฑ์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฉ”๋‰ด ๊ทธ๋ž˜ํ”„(G)->๋ ˆ๊ฑฐ์‹œ ๋Œ€ํ™” ์ƒ์ž->๋ง‰๋Œ€ ๋„ํ‘œ(B)(Graphs->Bar)๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ง‰๋Œ€๋„ํ‘œ์„ ํƒ ์‚ฌ์šฉ์ž๊ฐ€ ๊ทธ๋ฆด ๋ง‰๋Œ€ ๋„ํ‘œ๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋‹จ์ˆœ : ํ•œ ๋ณ€์ˆ˜์— ๋Œ€ํ•˜์—ฌ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ์ˆ˜ํ‰ ๋ˆ„์  : ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„๋ฅผ ์ž‘์„ฑํ•  ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋‹ค์‹œ ์ˆ˜ํ‰์œผ๋กœ ๊ตฌ๋ถ„ํ•  ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์—ฌ ๊ทธ๋ž˜ํ”„๋ฅผ ์ž‘์„ฑ ์ˆ˜์ง ๋ˆ„์  : ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„๋ฅผ ์ž‘์„ฑํ•  ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋‹ค์‹œ ์ˆ˜์ง์œผ๋กœ ๊ตฌ๋ถ„ํ•  ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์—ฌ ๊ทธ๋ž˜ํ”„๋ฅผ ์ž‘์„ฑ ์—ฌ๊ธฐ์—์„œ๋Š” "์ˆ˜ํ‰ ๋ˆ„์ " ํ˜•ํƒœ์˜ ๋ง‰๋Œ€ ๋„ํ‘œ๋ฅผ ๊ทธ๋ฆฐ ๊ฒฝ์šฐ๋ฅผ ๋ณด์—ฌ์ค€ ๊ฒƒ์ด๋‹ค. "๋„ํ‘œ์— ํ‘œ์‹œํ•  ๋ฐ์ดํ„ฐ"๋Š” "์ผ€์ด์Šค ์ง‘๋‹จ๋“ค์˜ ์š” ์•ฝ ๊ฐ’" : ๊ฐ ๋ฒ”์ฃผ๋ณ„๋กœ ๋นˆ๋„์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋ง‰๋Œ€๋„ํ‘œ๋ฅผ ์ž‘์„ฑ. "๊ฐœ๋ณ„ ๋ณ€์ˆ˜์˜ ์š” ์•ฝ ๊ฐ’" : ๋ณ€์ˆ˜๋ฅผ ์ž๋ฃŒ๊ฐ’์œผ๋กœ ํ•˜์—ฌ ๋ง‰๋Œ€ ๋„ํ‘œ๋ฅผ ์ž‘์„ฑ. "๊ฐ ์ผ€์ด์Šค์˜ ๊ฐ’" : ์ด๋ฏธ ๊ณ„์‚ฐ๋œ ์ž๋ฃŒ๊ฐ’์„ ๋ง‰๋Œ€ ๋„ํ‘œ๋กœ ์ž‘์„ฑ. ์—์„œ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆ˜ ์„ค์ • "๋ฒ”์ฃผ์ถ•"์— "๊ฒฐํ˜ผ ์—ฌ๋ถ€" ๋ณ€์ˆ˜๋ฅผ ์„ ํƒํ•˜๊ณ , "์ˆ˜ํ‰ ๋ˆ„์  ๊ธฐ์ค€ ๋ณ€์ˆ˜"์— "์ข…๊ต" ๋ณ€์ˆ˜๋ฅผ ์„ ํƒํ•œ๋‹ค. ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ์™„์„ฑ๋œ ๋ง‰๋Œ€๋„ํ‘œ๋ฅผ SPSS ๋ทฐ์–ด์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ์ž‘์„ฑ ๊ฒฐ๊ณผ ๊ธฐํ˜ผ์ž๊ฐ€ ๋ฏธํ˜ผ์ž์— ๋น„ํ•ด ์›”๋“ฑํ•˜๊ฒŒ ๋งŽ๋‹ค. titanic ์ž๋ฃŒ๋กœ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ์ž‘์„ฑํ•˜๋Š” python code # ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ import pandas as pd import matplotlib.pyplot as plt # ํƒ€์ดํƒ€๋‹‰ ๋ฐ์ดํ„ฐ ์…‹ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ titanic = pd.read_csv('https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv') #ํƒ€์ดํƒ€๋‹‰ ์ž๋ฃŒ์— ๋Œ€ํ•œ ์ •๋ณด titanic.info() # ์„ฑ๋ณ„์— ๋”ฐ๋ฅธ ์ƒ์กด์ž ์ˆ˜ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ survived_by_sex = titanic.groupby('Sex')['Survived'].sum() ax = survived_by_sex.plot(kind='bar', rot=0, color='green') ax.set_xlabel('Sex') ax.set_ylabel('Number of Survivors') plt.show() # ์ขŒ์„ ๋“ฑ๊ธ‰์— ๋”ฐ๋ฅธ ์ƒ์กด์ž ์ˆ˜ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ survived_by_class = titanic.groupby('Pclass')['Survived'].sum() ax = survived_by_class.plot(kind='bar', rot=0, color='blue') ax.set_xlabel('Passenger Class') ax.set_ylabel('Number of Survivors') plt.show() # ํƒ‘์Šน ํ•ญ๊ตฌ์— ๋”ฐ๋ฅธ ์ƒ์กด์ž ์ˆ˜ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ survived_by_embarked = titanic.groupby('Embarked')['Survived'].sum() ax = survived_by_embarked.plot(kind='bar', rot=0, color='red') ax.set_xlabel('Port of Embarkation') ax.set_ylabel('Number of Survivors') plt.show() # ์—ฐ๋ น๋Œ€๋ณ„ ์ƒ์กด์ž ์ˆ˜ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ titanic['AgeGroup'] = pd.cut(titanic['Age'], bins=[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]) survived_by_agegroup = titanic.groupby('AgeGroup')['Survived'].sum() ax = survived_by_agegroup.plot(kind='bar', rot=0, color='purple') ax.set_xlabel('Age Group') ax.set_ylabel('Number of Survivors') plt.show() 2. ํžˆ์Šคํ† ๊ทธ๋žจ(histogram) ํžˆ์Šคํ† ๊ทธ๋žจ ํŠน์„ฑ ์—ฐ์†ํ˜• ์ž๋ฃŒ์—์„œ ๊ณ„๊ธ‰(้šŽ็ดš; class)์— ๋Œ€ํ•˜์—ฌ ๋„์ˆ˜๋‚˜ ์ƒ๋Œ€๋„์ˆ˜๋ฅผ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„์™€ ์œ ์‚ฌ(ไผผ) ํ•œ ๋ชจ์–‘์„ ๊ทธ๋ฆฌ๋Š” ๊ทธ๋ฆผ ๊ณ„๊ธ‰(class) : ์—ฐ์†ํ˜• ์ž๋ฃŒ์—์„œ ๊ด€์ธก ๊ฐ’์„ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ตฌ๊ฐ„(ๅ€้–“)์œผ๋กœ ๋‚˜๋ˆˆ ๊ฒƒ ๊ณ„๊ธ‰ ๋†’์ด๋Š” ์ƒ๋Œ€๋„์ˆ˜ ๊ณ„๊ธ‰๊ตฌ๊ฐ„์˜ ํญๅน… ๋Œ€ ์ˆ˜ ๊ธ‰ ๊ฐ„ ํญ ( ) ๋˜๋Š” ๋„์ˆ˜ ๊ณ„๊ธ‰๊ตฌ๊ฐ„์˜ ํญ ์ˆ˜ ๊ธ‰ ๊ฐ„ ํญ ์ด๋‹ค. ๊ณ„๊ธ‰ ๋†’์ด๊ฐ€ ์ƒ๋Œ€๋„์ˆ˜ ๊ณ„๊ธ‰๊ตฌ๊ฐ„์˜ ํญๅน… ๋Œ€ ์ˆ˜ ๊ธ‰ ๊ฐ„ ํญ ( ) ์ด๋ฉด ๋ชจ๋“  ๋ฉด์ ์€ 1์ด๋‹ค. ๊ฐ ๊ณ„๊ธ‰ ๊ตฌ๊ฐ„์€ ๊ธธ์ด๊ฐ€ ๊ฐ™์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ์—ฐ์†ํ˜• ์ž๋ฃŒ์—์„œ ์šฉ์ด(ๅฎน) ํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค. ๊ฐ€์žฅ ์ ํ•ฉํ•œ ํžˆ์Šคํ† ๊ทธ๋žจ์€ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ”์ฃผ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ ์—ฌ๋Ÿฌ ํžˆ์Šคํ† ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์—ฌ ์ž๋ฃŒ์˜ ์œค๊ณฝ(ๅป“)์„ ํŒŒ์•…ํ•˜๋Š”๋ฐ ์šฉ์ดํ•œ ํžˆ์Šคํ† ๊ทธ๋žจ์„ ์„ ํƒํ•œ๋‹ค. ๊ณ„๊ธ‰ ์ˆ˜์™€ ๊ตฌ๊ฐ„์ด ๊ฐ™๋”๋ผ๋„ ์‹œ์ž‘์ ์ด ๋‹ค๋ฅด๋ฉด ๊ทธ๋ž˜ํ”„์˜ ์ „์ฒด์ ์ธ ํ˜•ํƒœ๊ฐ€ ๋ณ€ํ•˜๋Š” ๋‹จ์ (็Ÿญ้ปž)์ด ์žˆ๋‹ค. ํžˆ์Šคํ† ๊ทธ๋žจ ์ž‘์„ฑ ๋‹ค์Œ์€ ํžˆ์Šคํ† ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด์ž. ํžˆ์Šคํ† ๊ทธ๋žจ ์ž‘์„ฑ์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋จผ์ € ๊ทธ๋ฆผ์— ๋ณด์ด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ๊ทธ๋ž˜ํ”„(Graphs)->๋ ˆ๊ฑฐ์‹œ ๋Œ€ํ™” ์ƒ์ž -> ํžˆ์Šคํ† ๊ทธ๋žจ(Histogram) ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆ˜ ์„ค์ • ํžˆ์Šคํ† ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ๋ณ€์ˆ˜๋ฅผ ์„ ํƒํ•œ๋‹ค. ์„ ํƒํ•œ ๋ณ€์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ์ถ”๊ฐ€ํ•˜๊ณ  ํ™•์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์„ ํƒํ•œ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ํžˆ์Šคํ† ๊ทธ๋žจ์ด ๊ทธ๋ ค์ง„๋‹ค. ์ •๊ทœ๊ณก์„  ์ถœ๋ ฅ์„ ์„ ํƒํ•˜๋ฉด ์ •๊ทœ๋ถ„ํฌ ๊ณก์„ ์ด ํžˆ์Šคํ† ๊ทธ๋žจ๊ณผ ๊ฐ™์ด ๊ทธ๋ ค์ง„๋‹ค. ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ํŽธ์ง‘ ๊ธฐ๋ณธ ์„ค์ •๊ฐ’์œผ๋กœ ์ž‘์„ฑ๋œ ํžˆ์Šคํ† ๊ทธ๋žจ์ด๋‹ค. ์ด ๊ทธ๋ž˜ํ”„๋ฅผ ์ˆ˜์ •ํ•˜๋ ค๋ฉด ์•„๋ž˜์˜ ๊ทธ๋ฆผ์— ๋ณด์ด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ๊ทธ๋ฆผ ์˜์—ญ์— ๋งˆ์šฐ์Šค๋ฅผ ์œ„์น˜์‹œํ‚จ ํ›„ ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  ๋‚ด์šฉ ํŽธ์ง‘->๋ณ„๋„์˜ ์ฐฝ์—์„œ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ๋งˆ์šฐ์Šค ์™ผ์ชฝ ๋ฒ„ํŠผ์„ ๋”๋ธ” ํด๋ฆญํ•œ๋‹ค. ํŠน์„ฑ์ฐฝ์—์„œ ๊ทธ๋ž˜ํ”„ ํŽธ์ง‘ ๊ทธ๋ฆผ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋„ํ‘œ ํŽธ์ง‘๊ธฐ๊ฐ€ ์—ด๋ฆฐ๋‹ค. ์ด ์ฐฝ์—์„œ ๊ทธ๋ž˜ํ”„์— ๋งˆ์šฐ์Šค๋ฅผ ์œ„์น˜ํ•œ ํ›„ ๋งˆ์šฐ์Šค ์™ผ์ชฝ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ํ•ญ๋ชฉ๋“ค์ด ๋‚˜์˜จ๋‹ค. ํŠน์„ฑ ์ฐฝ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜๋ฉด ๋งˆ์šฐ์Šค๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ์—์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์ด ๋‚˜ํƒ€๋‚œ๋‹ค. ๊ทธ๋ž˜ํ”„์—์„œ ํŠน๋ณ„ํžˆ ๋ณ€๊ฒฝ์„ ์›ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ํŠน์„ฑ ์ฐฝ์ด ์—ด๋ ค์žˆ๋Š” ์ƒํƒœ์—์„œ ๊ทธ ๋ถ€๋ถ„์„ ํด๋ฆญํ•œ๋‹ค. ํžˆ์ŠคํŠธ๊ทธ๋žจ ๊ณ„๊ธ‰ ํŽธ์ง‘ ์˜ˆ๋ฅผ ๋“ค์–ด ํžˆ์Šคํ† ๊ทธ๋žจ์˜ ๊ณ„๊ธ‰์˜ ๊ฐœ์ˆ˜๋‚˜ ๋„ˆ๋น„์˜ ๊ธธ์ด๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ํŠน์„ฑ ์ฐฝ์ด ์—ด๋ฆฐ ์ƒํƒœ์—์„œ ๊ณ„๊ธ‰(๋ง‰๋Œ€)๋ฅผ ํด๋ฆญํ•œ๋‹ค. ํžˆ์Šคํ† ๊ทธ๋žจ ์˜ต์…˜์„ ์„ ํƒํ•œ๋‹ค. X ์ถ•์˜ ์‚ฌ์šฉ์ž ์ •์˜์—์„œ ๊ตฌ๊ฐ„ ์ˆ˜๋‚˜ ๊ตฌ๊ฐ„ ๋„ˆ๋น„์— ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ๊ฐ’์„ ์ž…๋ ฅํ•œ๋‹ค. ํžˆ์Šคํ† ๊ทธ๋žจ ๊ทธ๋ž˜ํ”„ ํŽธ์ง‘ ์™„์„ฑ ๊ณ„๊ธ‰์˜ ์ˆ˜๋ฅผ 7๊ฐœ๋กœ ์ ์šฉํ•œ ํ›„ ๋„ํ‘œ ํŽธ์ง‘๊ธฐ์ด๋‹ค ๋„ํ‘œ ํŽธ์ง‘๊ธฐ ์ข…๋ฃŒ ๋„ํ‘œ ํŽธ์ง‘๊ธฐ์—์„œ ์„ค์ •์„ ์™„๋ฃŒํ•œ ํ›„ ์ฐฝ์„ ๋‹ซ์œผ๋ฉด ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ฐฝ์— ๋ณ€๊ฒฝ๋œ ๋‚ด์šฉ์ด ์ €์žฅ๋œ๋‹ค. 3. ์›๊ทธ๋ž˜ํ”„(pie chart) ์›๊ทธ๋ž˜ํ”„ ํŠน์„ฑ ์›์„ ์ž‘์„ฑ(ไฝœๆˆ) ํ•˜๊ณ  ๊ด€์ธก ๊ฐ’์— ๋Œ€ํ•œ ์ƒ๋Œ€๋„์ˆ˜๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ๋น„์œจ(ๆฏ”)์„ ํ˜ธ(ๅผง, arc)๋กœ ์ž‘์„ฑํ•˜์—ฌ ์›์— ๊ฐ๊ฐ์˜ ํ˜ธ๋ฅผ ์—ฐ๊ฒฐ(็ต) ํ•œ ๊ทธ๋ฆผ ๊ฐ ๋ฒ”์ฃผ ๋˜๋Š” ๋ช‡ ๊ฐœ์˜ ๋ฒ”์ฃผ๊ฐ€ ์ „์ฒด(ๅ…จ้ซ”)์—์„œ ์ฐจ์ง€ํ•˜๋Š” ๋น„์œจ์„ ํŒŒ์•…(ๆŠŠๆก) ํ•˜๊ธฐ์— ์šฉ์ด(ๅฎน) ์›๊ทธ๋ž˜ํ”„ ์ž‘์„ฑ ๋‹ค์Œ์€ ์›๊ทธ๋ž˜ํ”„๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. ์›๊ทธ๋ž˜ํ”„ ์ž‘์„ฑ์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์›๊ทธ๋ž˜ํ”„ ์‹คํ–‰์€ ๊ทธ๋ž˜ํ”„(Graphs)->๋ ˆ๊ฑฐ์‹œ ๋Œ€ํ™” ์ƒ์ž -> ์›ํ˜• ์ฐจํŠธ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ์›ํ˜• ์ฐจํŠธ ์ฐฝ์ด ๋‚˜ํƒ€๋‚˜๊ณ  ์ฐจํŠธ์— ํ‘œ์‹œํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•œ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์ธ ์ผ€์ด์Šค ์ง‘๋‹จ๋“ค์˜ ์š” ์•ฝ ๊ฐ’์„ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆ˜ ์„ ํƒ ์›๊ทธ๋ž˜ํ”„ ์ž‘์„ฑ์— ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜๋ฅผ ์„ ํƒํ•œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์›๊ทธ๋ž˜ํ”„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์ด๋‹ค. ์ด ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ํŽธ์ง‘์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์› ์•ˆ์—์„œ ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‚˜ํƒ€๋‚˜๋Š” ๋ฉ”๋‰ด์—์„œ ๋‚ด์šฉ ํŽธ์ง‘ -> ๋ณ„๋„์˜ ์ฐฝ์—์„œ๋ฅผ ํด๋ฆญํ•œ๋‹ค. ๋„ํ‘œ ํŽธ์ง‘๊ธฐ ์›๊ทธ๋ž˜ํ”„๋ฅผ ํŽธ์ง‘ํ•  ์ฐฝ์ด๋‹ค. ์›๊ทธ๋ž˜ํ”„์—์„œ ๊ฐ ๋ฒ”์ฃผ์— ๊ฐ’์„ ํ‘œ์‹œํ•ด ๋ณด์ž. ์›๊ทธ๋ž˜ํ”„์— ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ” ํ‘œ์‹œ๋ฅผ ํด๋ฆญํ•œ๋‹ค. ๋„ํ‘œ ํŽธ์ง‘๊ธฐ ํŠน์„ฑ์ฐฝ ๋ฐ์ดํ„ฐ ๊ฐ’ ๋ ˆ์ด๋ธ” ๊ธฐ๋ณธ๊ฐ’์€ ๋นˆ๋„์ด๊ณ  ๋” ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์„ฑ๋ณ„๊ณผ ํผ์„ผํŠธ์ด๋‹ค. ํ‘œ์‹œ ์•ˆ ํ•จ์— ์žˆ๋Š” ํ•ญ๋ชฉ์„ ํ‘œ์‹œ์— ์ถ”๊ฐ€ํ•˜๋ฉด ์›๊ทธ๋ž˜ํ”„์— ๋ ˆ์ด๋ธ”์ด ์ถ”๊ฐ€๋œ๋‹ค. ๋„ํ‘œ ํŽธ์ง‘๊ธฐ๋ฅผ ์ฐฝ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ข…๋ฃŒํ•œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋„ํ‘œ ํŽธ์ง‘๊ธฐ์—์„œ ํŽธ์ง‘์„ ์™„๋ฃŒํ•˜๋ฉด ์ ์šฉ๋œ ๋‚ด์šฉ์ด ์ถœ๋ ฅ ๊ฒฐ๊ณผ์— ๋‚˜ํƒ€๋‚œ๋‹ค. 4. ์ค„๊ธฐ-์žŽ ๊ทธ๋ฆผ(stem and leaf plot) ์ค„๊ธฐ-์žŽ ๊ทธ๋ฆผ ํŠน์„ฑ ๊ด€์ธก ๊ฐ’์˜ ์ž๋ฆฟ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ค„๊ธฐ์™€ ์žŽ์œผ๋กœ ๋‚˜๋ˆˆ ๊ทธ๋ฆผ ์žŽ ๋‹จ์œ„๋ฅผ ์ค„๊ธฐ๋กœ ํ•˜๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์žŽ์— ํ•ด๋‹นํ•˜๋Š” ์ž๋ฃŒ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„์˜ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์ž๋ฃŒ์˜ ์†์‹ค์ด ์—†์Œ ์ ์€ ์–‘์˜ ์ž๋ฃŒ์— ์ ํ•ฉ ์ค„๊ธฐ-์žŽ ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ ๋‹ค์Œ์€ ์ค„๊ธฐ - ์žŽ ๊ทธ๋ฆผ์„ ์ž‘์„ฑํ•ด ๋ณด์ž. ์›๊ทธ๋ž˜ํ”„ ์ž‘์„ฑ์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณ€์ˆ˜ ์„ค์ • ์ข…์†๋ณ€์ˆ˜์™€ ์š”์ธ ๋ณ€์ˆ˜ ์„ค์ •ํ•˜๊ณ  ํ‘œ์‹œ์—์„œ๋Š” ํ†ต๊ณ„๋Ÿ‰๊ณผ ๋„ํ‘œ๋ฅผ ๋ชจ๋‘ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ ํƒ์ƒ‰: ๋„ํ‘œ ๊ธฐ์ˆ  ํ†ต๊ณ„์—์„œ ์ค„๊ธฐ์™€ ์žŽ ๊ทธ๋ฆผ์„ ์„ ํƒํ•œ๋‹ค. ์ถœ๋ ฅ๊ณผ ๋ถ„์„ ํ‚ค๋Š” ์„ฑ๋ณ„์ด ์—ฌ์ž์ธ ๊ฒฝ์šฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. ๋‚จ์ž๋Š” ์•„๋ž˜์— ์žˆ๋‹ค. ์ค„๊ธฐ ๋„ˆ๋น„๋Š” 10์œผ๋กœ ์ค„๊ธฐ์— 10์„ ๊ณฑํ•œ ๊ฒƒ์ด ๋‹จ์œ„๊ฐ€ ๋œ๋‹ค. 15๋Š” 150์ด๋‹ค. ์žŽ์€ 1๊ฐœ ์ˆซ์ž๊ฐ€ 1๊ฐœ ์ž๋ฃŒ์ด๋‹ค. 00์€ ํ‚ค์˜ ์ผ์˜ ๋‹จ์œ„ ๊ฐ’์ด ๊ฐ๊ฐ 00์ด๋‹ค. 5. ์‚ฐ์ ๋„(scatter plot) ์‚ฐ์ ๋„๋Š” ๋‘ ๋ณ€์ˆ˜์˜ ์ˆœ์„œ์Œ์— ๋Œ€ํ•œ ์„ ์  ( 1 Y) ( 2 2 ) ( n Y) ์„ ํ‰๋ฉด์— ์ž‘์„ฑํ•œ ๊ทธ๋ž˜ํ”„์ด๋‹ค. SPSS ์ž‘์„ฑ ๋‹ค์Œ์€ ์‚ฐ์ ๋„๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. ์›๊ทธ๋ž˜ํ”„ ์ž‘์„ฑ์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋จผ์ € ๊ทธ๋ฆผ์— ๋ณด์ด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ๊ทธ๋ž˜ํ”„(Graphs)->๋ ˆ๊ฑฐ์‹œ ๋Œ€ํ™” ์ƒ์ž -> ์‚ฐ์ ๋„/์  ๋„ํ‘œ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ์‚ฐ์ ๋„ ์ข…๋ฅ˜ ์ •์˜ ์ž‘์„ฑํ•  ์‚ฐ์ ๋„๋ฅผ ์„ ํƒํ•œ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์ธ ๋‹จ์ˆœ ์‚ฐ์ ๋„๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆ˜ ์„ค์ • ๋‹จ์ˆœ ์‚ฐ์ ๋„์— ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋Š” ํ‰๋ฉด์ขŒํ‘œ์ด๋ฏ€๋กœ ์ถ•๊ณผ ์ถœ 2๊ฐœ์ด๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ™”๋ฉด์ด๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํŽธ์ง‘์€ ๋‚ด์šฉ ํŽธ์ง‘ -> ๋ณ„๋„์˜ ์ฐฝ์—์„œ๋ฅผ ํด๋ฆญํ•œ๋‹ค. ๋„ํ‘œ ํŽธ์ง‘๊ธฐ ๋„ํ‘œ ํŽธ์ง‘๊ธฐ์—์„œ ํšŒ๊ท€์„ ์„ ์ถ”๊ฐ€ํ•˜๋Š” ํŽธ์ง‘์„ ํ•ด ๋ณด์ž ๋„ํ‘œ ํŽธ์ง‘๊ธฐ ์ฐฝ์—์„œ ํŽธ์ง‘์€ ๊ทธ๋ฆผ์—์„œ ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ์„ ํด๋ฆญํ•˜์—ฌ ๋‚˜์˜ค๋Š” ๋ฉ”๋‰ด์—์„œ ์ถ”๊ฐ€ ์ „์ฒด ํšŒ๊ท€์„  ์ ํ•ฉ์„ ๋ˆ„๋ฅด๊ฑฐ๋‚˜ ํ‘œ์ค€ ๋ฉ”๋‰ด์—์„œ ํ•ด๋‹น ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋„ํ‘œ ํŽธ์ง‘๊ธฐ ํŠน์„ฑ์ฐฝ ํšŒ๊ท€์„  ์ ํ•ฉ ํƒญ์—์„œ ์„ ํ˜•์€ ๊ธฐ๋ณธ๊ฐ’์ด๋‹ค. ํŽธ์ง‘ ํ›„ ๊ทธ๋ž˜ํ”„ ๋„ํ‘œ ํŽธ์ง‘์„ ๋งˆ์น˜๋ฉด ์ฐฝ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ฐฝ์—์„œ ํ™•์ธํ•œ๋‹ค. 6. ์ƒ์ž ๊ทธ๋ฆผ(box plot) ์ƒ์žโ€“์ˆ˜์—ผ ๊ทธ๋ฆผ(box โ€“ whisker plot)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฆ„ ์ตœ์†Œ(ๆœ€ๅฐ) ๊ฐ’, ์ œ1 ์‚ฌ๋ถ„์œ„์ˆ˜(Q1), ์ œ2 ์‚ฌ๋ถ„์œ„์ˆ˜(Q2), ์ œ3์‚ฌ๋ถ„์œ„ ์ˆ˜(Q3), ์ตœ๋Œ“๊ฐ’ ๋“ฑ 5 ๊ฐœ์˜ ํ†ต๊ณ„๋Ÿ‰์„ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ ๊ทธ๋ฆผ ์ž๋ฃŒ์˜ ๋ฒ”์œ„(็ฏ„ๅœ), ์‚ฌ๋ถ„์œ„์ˆ˜(ๅ››ๅˆ†ไฝๆ•ธ)์˜ ์œ„์น˜ ํŒŒ์•…์— ํŽธ๋ฆฌ ์ด์ƒ์ (ๅธธ้ปž, outlier) ํŒ๋ณ„(ๅˆคๅˆฅ)์— ์‚ฌ์šฉ ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜์—๋„ ์ƒ์ž ๊ทธ๋ฆผ ์ž‘์„ฑ(ไฝœๆˆ) ๊ฐ€๋Šฅ Q2 : ์ค‘์•™(ไธญๅคฎ) ๊ฐ’ IQR(Interquartile Range) : ์‚ฌ๋ถ„์œ„์ˆ˜ ๋ฒ”์œ„(Q3โ€“Q1) SPSS ์ž‘์„ฑ ๋‹ค์Œ์€ ์ƒ์ž ๊ทธ๋ฆผ์„ ์ž‘์„ฑํ•ด ๋ณด์ž. ์ƒ์ž ๊ทธ๋ฆผ ์ž‘์„ฑ์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋จผ์ € ๊ทธ๋ฆผ์— ๋ณด์ด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ๊ทธ๋ž˜ํ”„(Graphs)->๋ ˆ๊ฑฐ์‹œ ๋Œ€ํ™” ์ƒ์ž -> ์ƒ์ž๋„ ํ‘œ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ์ƒ์ž๋„ ํ‘œ ์ •์˜ ์–ด๋–ค ์ƒ์ง€ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆด ๊ฒƒ์ธ์ง€ ์ •์˜ํ•œ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์ธ ๋‹จ์ˆœ์„ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆ˜ ์„ค์ • ์ƒ์ž ๊ทธ๋ฆผ์— ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ ์„ค์ •๊ณผ ์š”์ธ์œผ๋กœ ๋‚˜๋ˆŒ ๋ณŒ์ฃผ๋ฅผ ์„ ํƒํ•œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์—ฌ, ๋‚จ์— ๋Œ€ํ•œ ํ‚ค์˜ ์ƒ์ž ๊ทธ๋ฆผ์ด๋‹ค. ๋‚จ์ž๋Š” Q1์—์„œ Q2 ์‚ฌ์ด์— ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋ชฐ๋ ค์žˆ๋‹ค. ๋‚จ์ž์—์„œ ์ด์ƒ์ ์ด ์žˆ์œผ๋ฉฐ ๊ทธ ๊ฐ’์€ ๋ฐ์ดํ†  ๋ณด๊ธฐ ์‹œํŠธ์—์„œ 85๋ฒˆ์งธ ์ž๋ฃŒ์ด๋‹ค. ์—ฌ์ž๋Š” ์ด์ƒ์ ๋„ ์—†๊ณ  ์ž๋ฃŒ๊ฐ€ ๊ณ ๋ฅด๊ฒŒ ๋ถ„ํฌํ•˜๊ณ  ์žˆ๋‹ค. 03. ํ†ต๊ณ„๋Ÿ‰ ๊ตฌํ•˜๊ธฐ ๊ธฐ์ˆ  ํ†ต๊ณ„๋Ÿ‰(descriptive statistics) ๋นˆ๋„ ๋ถ„์„ ๊ธฐ์ˆ  ํ†ต๊ณ„ ๋ฐ์ดํ„ฐ ํƒ์ƒ‰ 1. ๋นˆ๋„ ๋ถ„์„ ๋„์ˆ˜๋ถ„ํฌํ‘œ ํŠน์„ฑ ๊ด€์ธก(่ง€ๆธฌ) ๊ฐ’์„ ๋ช‡ ๊ฐœ์˜ ๋ฒ”์ฃผ๋กœ ๋‚˜๋ˆˆ ๋‹ค์Œ ๊ทธ ๋ฒ”์ฃผ์— ์†ํ•˜๋Š” ๊ด€์ธก ๊ฐ’์˜ ๊ฐœ์ˆ˜(๋„์ˆ˜, frequency)์™€ ๊ทธ ๋„์ˆ˜๋ฅผ ์ „์ฒด ๊ด€์ธก ๊ฐ’ ๊ฐœ์ˆ˜๋กœ ๋‚˜๋ˆˆ ๊ฐ’(์ƒ๋Œ€(็›ธๅฐ) ๋„์ˆ˜, relative frequency)์— ๋Œ€ํ•œ ํ‘œ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ, ์ด์‚ฐํ˜•(ๆ•ฃๅž‹) ์ž๋ฃŒ, ์—ฐ์†ํ˜•(็บŒๅž‹) ์ž๋ฃŒ ๋ชจ๋‘ ๊ฐ€๋Šฅ ๋นˆ๋„ ๋ถ„์„ ๋นˆ๋„ ๋ถ„์„์€ ๋„์ˆ˜๋ถ„ํฌํ‘œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ์— ๋Œ€ํ•œ ๋„์ˆ˜(frequency)์™€ ์ƒ๋Œ€๋„์ˆ˜(relative frequency)๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด์ž. ๊ทธ๋ฆผ์— ๋ณด์ด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด "๋ถ„์„->๊ธฐ์ˆ ํ†ต๊ณ„๋Ÿ‰->๋นˆ๋„๋ถ„์„" ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ถ„์„์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณ€์ˆ˜ ์„ค์ • ๋” ๋งŽ์€ ํ†ต๊ณ„๋Ÿ‰์€ "ํ†ต๊ณ„๋Ÿ‰" ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒฐ๊ณผ ๋นˆ๋„ ๋ถ„์„ ๊ฒฐ๊ณผ์ด๋‹ค. 2. ๊ธฐ์ˆ  ํ†ต๊ณ„ ํ†ต๊ณ„๋Ÿ‰ ํŽธ์ฐจ = ๊ด€์ธก ๊ฐ’ - ํ‰๊ท  = i X ํ‰๊ท  ๋ชจ๋“  ๊ด€์ธก ๊ฐ’์˜ ํ•ฉ ๊ด€์ธก ๊ฐ’์˜ ๊ฐœ์ˆ˜ ( โ€• ) ๋ชจ๋“  ๊ด€์ธก ๊ฐ’์˜ ํ•ฉ ๊ด€์ธก ๊ฐ’์˜ ๊ฐœ์ˆ˜ x + + n = i 1 X n ํ•ฉ๊ณ„ = X ํŽธ์ฐจ์˜ ํ•ฉ = 0 = ( i X) โˆ‘ i n X ํ‘œ์ค€ํŽธ์ฐจ ํŽธ์ฐจ์˜ ์ œ๊ณฑํ•ฉ = ํŽธ์ฐจ์˜ ์ œ๊ณฑํ•ฉ โˆ’ = ( i X) n 1 ๋ถ„์‚ฐ ํŽธ์ฐจ์˜ ์ œ๊ณฑํ•ฉ 2 ํŽธ์ฐจ์˜ ์ œ๊ณฑํ•ฉ โˆ’ = ( i X) n 1 ๋ฒ”์œ„ : ์ตœ๋Œ“๊ฐ’ - ์ตœ์†Ÿ๊ฐ’ ์ตœ์†Ÿ๊ฐ’ : ์ž๋ฃŒ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ ์ตœ๋Œ“๊ฐ’ : ์ž๋ฃŒ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’ ํ‰๊ท ์˜ ํ‘œ์ค€์˜ค์ฐจ = n ์™œ๋„(ๆญชๅบฆ, skewness) 3 โˆ‘ = n ( i X) / S ฮณ >์ธ ๊ฒฝ์šฐ 3 0 ์ธ ๊ฒฝ์šฐ 3 0 ์ธ ๊ฒฝ์šฐ 3 0 ์ธ ๊ฒฝ์šฐ ์ฒจ๋„(ๅฐ–ๅบฆ, kurtosis) 4 โˆ‘ = n ( i X) / S โˆ’ ฮณ = ์ด๋ฉด ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์ด๊ณ , 4 0 ์ด๋ฉด ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋ณด๋‹ค ๋” ๋พฐ์กฑํ•˜๋ฉฐ, 4 0 ์ด๋ฉด ๋œ ๋พฐ์กฑํ•˜๋‹ค. SPSS ๊ณผ์ • ํ†ต๊ณ„ํ•™์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํ‰๊ท , ์ค‘์•™๊ฐ’, ๋ถ„์‚ฐ, ํ‘œ์ค€ํŽธ์ฐจ ๋“ฑ์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด์ž. ๊ทธ๋ฆผ์— ๋ณด์ด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด "๋ถ„์„->๊ธฐ์ˆ ํ†ต๊ณ„๋Ÿ‰->๊ธฐ์ˆ ํ†ต๊ณ„" ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ถ„์„์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณ€์ˆ˜ ์„ค์ •๊ณผ ์—ฌ๋Ÿฌ ํ†ต๊ณ„๋Ÿ‰ ์„ ํƒ ๋ณ€์ˆ˜์— ๋‚˜์ด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋‹ค๋ฅธ ํ†ต๊ณ„๋Ÿ‰์„ ๊ตฌํ•˜๋ ค๋ฉด ์˜ต์…˜ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ์˜ต์…˜ ์ฐฝ์—์„œ ๊ตฌํ•  ํ†ต๊ณ„๋Ÿ‰์„ ์„ ํƒํ•˜๊ณ  ๊ณ„์† ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ํ›„ ๊ธฐ์ˆ  ํ†ต๊ณ„ ์ฐฝ์—์„œ ํ™•์ธ์„ ๋ˆ„๋ฅด๋ฉด ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒฐ๊ณผ ๋ฐ ๋ถ„์„ ๋ถ„์„ ๊ฒฐ๊ณผ๋กœ ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’, ๋ฒ”์œ„, ํ‰๊ท , ํ‘œ์ค€ํŽธ์ฐจ ๋“ฑ ์—ฌ๋Ÿฌ ํ†ต ๊ณ„๋Ÿ‰์ด ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. 3. ๋ฐ์ดํ„ฐ ํƒ์ƒ‰ ๋ฐ์ดํ„ฐ ํƒ์ƒ‰ ๋ฐ์ดํ„ฐ ํƒ์ƒ‰์€ ํ†ต๊ณ„ํ•™์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํ‰๊ท , ์ค‘์•™๊ฐ’, ๋ถ„์‚ฐ, ํ‘œ์ค€ํŽธ์ฐจ ๋“ฑ๊ณผ ๊ฐ™์€ ํ†ต๊ณ„๋Ÿ‰๊ณผ ๊ทธ๋ž˜ํ”„๋ฅผ ์š”์ธ๋ณ„๋กœ ๊ตฌํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ํƒ์ƒ‰ ์‹คํ–‰์€ "๋ถ„์„->๊ธฐ์ˆ ํ†ต๊ณ„๋Ÿ‰->๋ฐ์ดํ„ฐ ํƒ์ƒ‰" ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ถ„์„์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์— ์žˆ๋‹ค. ๋ณ€์ˆ˜ ์„ ํƒ ์ข…์†๋ณ€์ˆ˜์— ๋‚˜์ด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์š”์ธ์— ๊ฒฐํ˜ผ ์—ฌ๋ถ€๋ฅผ ์ถ”๊ฐ€ํ•œ ํ›„ ํ†ต๊ณ„๋Ÿ‰ ์ฐฝ๊ณผ ๋„ํ‘œ ์ฐฝ์— ์ถœ๋ ฅ์— ์›ํ•˜๋Š” ๊ฒƒ๋“ค์„ ์„ ํƒํ•˜๊ณ  ์‹คํ–‰ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค. ์ถœ๋ ฅ ๋ฐ ๋ถ„์„ ๋ฐ์ดํ„ฐ ํƒ์ƒ‰ ๊ฒฐ๊ณผ๋กœ ํ†ต๊ณ„๋Ÿ‰๊ณผ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. 04. ์ƒ๊ด€๋ถ„์„ ์ƒ๊ด€๋ถ„์„ ์ƒ๊ด€๋ถ„์„์€ ๋‘ ๋ณ€์ˆ˜์˜ ์ƒ๊ด€๊ด€๊ณ„๋ฅผ ์•Œ์•„๋ณด๋Š” ๋ถ„์„๋ฐฉ๋ฒ•์ด๋‹ค. ๋จผ์ € ๋‘ ๋ณ€์ˆ˜์˜ ์ƒ๊ด€์„ฑ์€ ์‚ฐ์ ๋„(scatter plot)๋กœ ํŒŒ์•…ํ•˜๊ณ  ์ƒ๊ด€๊ณ„์ˆ˜๋กœ ๊ตฌ์ฒดํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ๊ด€๊ณ„์ˆ˜ ํŽธ ์ƒ๊ด€๊ณ„์ˆ˜ 1. ์ƒ๊ด€๊ณ„์ˆ˜ ์ƒ๊ด€๋ถ„์„ ์‚ฐ์ ๋„๋Š” ๋‘ ๊ฐœ์˜ ์—ฐ์†ํ˜• ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๊ด€๊ณ„๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ๋Œ€๋žต์ ์œผ๋กœ ํŒŒ์•…ํ•˜๋Š” ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๊ธฐ์— ๊ตฌ์ฒด์ ์ธ ์ •๋ณด๋ผ๊ณ  ํ•˜๊ธฐ์—๋Š” ๋ถ€์กฑํ•˜๋‹ค. ์ƒ๊ด€๊ณ„์ˆ˜๋Š” ๋‘ ๊ฐœ์˜ ์—ฐ์†ํ˜• ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ˆ˜์น˜๋กœ ๊ตฌ์ฒด์ ์ธ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ๋‹จ์ˆœ ํšŒ๊ท€, ์‚ฐ์ ๋„, ์ƒ๊ด€๊ณ„์ˆ˜๋Š” ํ•จ๊ป˜ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์‚ฐ์ ๋„์™€ ๋‹จ์ˆœ ํšŒ๊ท€ ๋ฐ Pearson ํ‘œ๋ณธ์ƒ๊ด€๊ณ„์ˆ˜ ๋ชจ์˜์‹คํ—˜์€ ์—ฌ๊ธฐ๋ฅผ ๋ˆ„๋ฅด๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ๊ด€๊ณ„์ˆ˜ ๊ณ„์‚ฐ ๊ฐœ์˜ ๊ด€์ธก ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋‘ ๋ณ€์ˆ˜์˜ ์ง์€ ( 1 Y) ( 2 Y) โ‹ฏ ( n Y) ๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ ์ด ๊ด€๊ณ„๋Š” ์‚ฐ์ ๋„์™€ ์ƒ๊ด€๊ณ„์ˆ˜๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ๊ด€๊ณ„์ˆ˜ ์ข…๋ฅ˜๋Š” Pearson ํ‘œ๋ณธ์ƒ๊ด€๊ณ„์ˆ˜ Pearson ํ‘œ๋ณธ์ƒ๊ด€๊ณ„์ˆ˜๋Š” x = i 1 ( i x) ( i y) i 1 ( i x) โˆ‘ = n ( i y)๋กœ ๊ตฌํ•œ๋‹ค. ยฏ y๋Š” ํ‘œ๋ณธํ‰๊ท ์ด๋‹ค. ํ‘œ๋ณธ์ƒ๊ด€๊ณ„์ˆ˜ ์„ฑ์งˆ ๋‘ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ƒ๊ด€๊ณ„์ˆ˜๋Š” ํ•ญ์ƒ 1 1 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ ์–‘์ˆ˜(้™ฝๆ•ธ)์ด๋ฉด ์–‘์˜ ์ƒ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ๋‹ค๊ณ  ํ‘œํ˜„ ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ 1์— ๊ฐ€๊นŒ์šฐ๋ฉด ๊ฐ•ํ•œ ์–‘์˜ ์ƒ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ์Œ ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ ์Œ์ˆ˜(้™ฐๆ•ธ)์ด๋ฉด ์Œ์˜ ์ƒ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ๋‹ค๊ณ  ํ‘œํ˜„ ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ -1์— ๊ฐ€๊นŒ์šฐ๋ฉด ๊ฐ•ํ•œ ์Œ์˜ ์ƒ๊ด€๊ด€๊ณ„ ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ 0์— ๊ฐ€๊นŒ์šฐ๋ฉด ๋‘ ๋ณ€์ˆ˜ ๊ฐ„ ์„ ํ˜•์„ฑ(็ทšๅฝขๆ€ง, linear)์ด ์—†๋‹ค๊ณ  ํ‘œํ˜„ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋…๋ฆฝ์ด๋ฉด ์ƒ๊ด€๊ณ„์ˆ˜๋Š” 0์ด๋‚˜ ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ 0์ด๋”๋ผ๋„ ๋‘ ๋ณ€์ˆ˜๋Š” ๋…๋ฆฝ์ด ์•„๋‹ ์ˆ˜ ์žˆ์Œ Kendal ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜ Kendal ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜๋Š” = P 2 ( โˆ’ ) 1 4 n ( โˆ’ ) 1 ๋กœ ๊ตฌํ•œ๋‹ค.๋Š” ๋‘ ๋ณ€์ˆ˜์˜ ์ˆœ์œ„๊ฐ€ ์ผ์น˜ํ•˜๋Š” ์ž๋ฃŒ์˜ ์ดํ•ฉ์ด๋‹ค. ์€ ์ž๋ฃŒ์ˆ˜ Kendal ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜๋Š” ๋‘ ๋ณ€์ˆ˜์—์„œ ํ•œ ๋ณ€์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๋ณ€์ˆ˜๊ฐ€ ์ˆœ์œ„์˜ ์ผ์น˜์„ฑ์ด ์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ†ต๊ณ„๋Ÿ‰์ด๋‹ค. ๋‹ค์Œ ์ž๋ฃŒ๋Š” ํ‚ค์™€ ๋ชธ๋ฌด๊ฒŒ ์ˆœ์œ„๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์˜€๋‹ค. ์ด ์ž๋ฃŒ์— ๋Œ€ํ•œ Kendal ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด์ž. ์‚ฌ๋žŒ A B C D E F G H ํ‚ค ์ˆœ์œ„ 1 2 3 4 5 6 7 8 ๋ชธ๋ฌด๊ฒŒ ์ˆœ์œ„ 3 4 1 2 5 7 8 6 5 4 5 4 3 1 0 0 A ์‚ฌ๋žŒ์ธ ๊ฒฝ์šฐ ๋ชธ๋ฌด๊ฒŒ ์ˆœ์œ„๊ฐ€ 3์ด๊ณ  ์ด ์ˆœ์œ„ ๊ฐ’๋ณด๋‹ค ํฐ ์ˆœ์œ„ ๊ฐ’์ด 5๊ฐœ(4,5,6,7,8) B ์‚ฌ๋žŒ์ธ ๊ฒฝ์šฐ ๋ชธ๋ฌด๊ฒŒ ์ˆœ์œ„๊ฐ€ 4์ด๊ณ  ์ดํ›„ ์ด ์ˆœ์œ„ ๊ฐ’๋ณด๋‹ค ํฐ ์ˆœ์œ„ ๊ฐ’์ด 4๊ฐœ(5,6,7,8) C ์‚ฌ๋žŒ์ธ ๊ฒฝ์šฐ ๋ชธ๋ฌด๊ฒŒ ์ˆœ์œ„๊ฐ€ 1์ด๊ณ  ์ดํ›„ ์ด ์ˆœ์œ„ ๊ฐ’๋ณด๋‹ค ํฐ ์ˆœ์œ„ ๊ฐ’์ด 5๊ฐœ(2,5,6,7,8) ์ˆœ์œ„๊ฐ€ ์ผ์น˜ํ•˜๋Š” ์ž๋ฃŒ์˜ ์ˆ˜๋Š” = + + + + + + + = 22 ์ด๋‹ค. ๋”ฐ๋ผ ์ˆ˜ Kendal ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜๋Š” = ร— 22 ร— โˆ’ = 0.57 Spearman ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜ Spearman ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜๋Š” โˆ’ โˆ‘ i n ( 2 1 ) ๋กœ ๊ตฌํ•œ๋‹ค. i ๋Š” ๋‘ ๋ณ€์ˆ˜์˜ ์ˆœ์œ„ ์ฐจ์ด๋‹ค. ๋‹ค์Œ ์ž๋ฃŒ๋กœ Spearman ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ๊ตฌํ•ด๋ณด์ž. x 86 97 99 100 100 103 106 110 113 113 y 0 20 28 50 28 28 7 17 7 12 x ์ˆœ์œ„ 1 2 3 4.5 4.5 6 7 8 9.5 9.5 y ์ˆœ์œ„ 1 6 8 10 8 8 2.5 5 2.5 4 ์ˆœ์œ„ ์ฐจ 0 4 5 5.5 3.5 2 4.5 3 7 5.5 ์ˆœ์œ„ ์ฐจ 0 16 25 30.25 12.25 4 20.25 9 49 30.25 ์‹์— ๋Œ€์ž…ํ•˜์—ฌ Spearman ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด โˆ’ ร— 196 10 ( 100 1 ) โˆ’ 0.18788 ์ด๋‹ค. ์ƒ๊ด€๋ถ„์„ ์‹œ์ž‘ ์•„๋ฒ„์ง€ ํ‚ค์™€ ์•„๋“ค ํ‚ค๊ฐ€ ์„œ๋กœ ์ƒ๊ด€์žˆ๋Š”์ง€ ์ƒ๊ด€๊ณ„์ˆ˜(Correlation Coefficient)๋ฅผ ๊ตฌํ•ด๋ณด์ž. ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ๋ฅผ ๋ˆ„๋ฅด๋ฉด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ๊ด€๊ณ„์ˆ˜๋Š” "๋ถ„์„->์ƒ๊ด€๋ถ„์„->์ด๋ณ€๋Ÿ‰ ์ƒ๊ด€๊ณ„์ˆ˜" ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆ˜์™€ ์ƒ๊ด€๊ณ„์ˆ˜ ์„ ํƒ ๋‘ ๋ณ€์ˆ˜ "father", "son"์„ "๋ณ€์ˆ˜"์— ์‚ฝ์ž…ํ•˜๊ณ  ์„ธ ๊ฐœ์˜ ์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ์„ ํƒํ•œ ํ›„ ๋‹ค์Œ "ํ™•์ธ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ์ƒ๊ด€๊ณ„์ˆ˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ Pearson ํ‘œ๋ณธ์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ๊ตฌํ•œ ํ™”๋ฉด์ด๋‹ค. ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ 0.501์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์ƒ๋ณด๋‹ค ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ ์ž‘๋‹ค. ๋น„๋ชจ์ˆ˜ ์ƒ๊ด€๊ด€๊ณ„ ๋น„๋ชจ์ˆ˜ ์ƒ๊ด€๊ณ„์ˆ˜๋Š” Spearman ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜์™€ Kendal ์ˆœ์œ„์ƒ๊ด€๊ณ„์ˆ˜์— ๋Œ€ํ•˜์—ฌ ๋ณ„๋„๋กœ ๊ณ„์‚ฐํ•œ๋‹ค. 2. ํŽธ ์ƒ๊ด€๊ณ„์ˆ˜ ํŽธ ์ƒ๊ด€๊ณ„์ˆ˜(partial correlation coefficient) ํŽธ์ƒ๊ด€๊ณ„์ˆ˜๋Š” ๋ถ€๋ถ„ ์ƒ๊ด€๊ณ„์ˆ˜๋กœ๋„ ๋ถ€๋ฅธ๋‹ค. ์ข…์†๋ณ€์ˆ˜ ์™€ ์„ค๋ช…๋ณ€์ˆ˜ 1 x, , p ์ธ ์„ ํ˜•๋ชจํ˜•์ด ์žˆ๋‹ค. ์„ ํ˜•๋ชจํ˜•์—์„œ ํŽธ์ƒ๊ด€๊ณ„์ˆ˜๋Š” ๋‹ค๋ฅธ ๋ณ€์ˆ˜๊ฐ€ ์ฃผ๋Š” ์š”์ธ โˆ’ ๊ฐœ๋ฅผ ์ œ์–ดํ•˜๊ณ  ๋‘ ๋ณ€์ˆ˜์˜ ์ˆœ์ˆ˜ํ•œ ์ƒ๊ด€๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋ณ€์ˆ˜ ์™€ 1 ์˜ ํŽธ์ƒ๊ด€๊ณ„์ˆ˜๋Š” ์„ค๋ช…๋ณ€์ˆ˜ 2 โ‹ฏ x๋ฅผ ์ œ์–ด ๋ณ€์ˆ˜๋กœ ํ•˜๊ณ  ๋‚˜๋จธ์ง€ ๋‘ ๋ณ€์ˆ˜์˜ ์ˆœ์ˆ˜ํ•œ ์ƒ๊ด€๊ณ„์ˆ˜์ด๋‹ค. ์ œ์–ด ๋ณ€์ˆ˜๋Š” ํšŒ๊ท€ ๋ชจํ˜•์—์„œ ํšŒ๊ท€ ์ œ๊ณฑํ•ฉ์— ํฌํ•จ๋œ ์„ค๋ช…๋ ฅ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ œ์–ด ๋ณ€์ˆ˜๊ฐ€ ํšŒ๊ท€ ๋ชจํ˜•์— ํฌํ•จ๋œ ์ƒํƒœ์—์„œ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ์„œ๋กœ ์ƒ๊ด€์„ฑ์ด ์žˆ๋Š”์ง€ ํŒ๋‹จํ•œ๋‹ค. ํšŒ๊ท€๋ถ„์„์—์„œ 2 โ‹ฏ x ๊ฐ€ ํšŒ๊ท€ ๋ชจํ˜•์— ํฌํ•จ๋œ ์ƒํƒœ์—์„œ ์„ค๋ช…๋ณ€์ˆ˜ 1 ์ด ๋ฐ˜์‘ ๋ณ€์ˆ˜๋ฅผ ์„ค๋ช…ํ•˜๋Š”์ง€ ํŒ๋‹จํ•œ๋‹ค. ๋ถ„์„ ๊ฒฐ๊ณผ ์œ ์˜ํ•˜์ง€ ์•Š์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋ฉด ๋ฐ˜์‘ ๋ณ€์ˆ˜๋Š” ์„ค๋ช…๋ณ€์ˆ˜๋ฅผ ์ž˜ ์„ค๋ช…ํ•˜์ง€ ๋ชปํ•จ์„ ์˜๋ฏธํ•œ๋‹ค. ์„ ํ˜•๋ชจํ˜•์—์„œ ๋‘ ๋ณ€์ˆ˜ ์™€ 1 ํŽธ ์ƒ๊ด€๊ณ„์ˆ˜ Y x | 2 x, , p ๊ณ„์‚ฐ์€ Y x | 2 x, , p t 2 d ( S) ๋กœ ํ•œ๋‹ค. ๋Š” 0 ฮฒ =์—์„œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด๊ณ , f ( S) ๋Š” ์˜ค์ฐจ ์ œ๊ณฑ ํ•ฉ์˜ ์ž์œ ๋„ โˆ’ โˆ’์ด๋‹ค. ๋˜ํ•œ 2 F ์ด๋ฏ€๋กœ Y x | 2 x, , p F + f ( S) ์ด๋‹ค. ์˜ˆ์ œ ์ž๋ฃŒ ๋‹ค์Œ์€ ํŽธ์ƒ๊ด€๊ณ„์ˆ˜์— ์‚ฌ์šฉํ•  ์ž๋ฃŒ์ด๋‹ค. ์ด ์ž๋ฃŒ๋Š” [1]์—์„œ ์ฐธ๊ณ ํ•˜์˜€๋‹ค. ์ด ์ž๋ฃŒ๋Š” ๊ธฐ์–ต ๋ฒ”์œ„(memory span)๋Š” ๋‚˜์ด(age)์™€ ๋ง ์†๋„(speech rate)์— ์–ด๋Š ์ •๋„ ์˜ํ–ฅ์„ ๋ฐ›๋Š”์ง€ ์•Œ์•„๋ณด์ž. (Memory span) 14 23 30 50 39 67 1 (age) 4 7 10 10 2 (Speech rate) 2 4 6 ์ฐธ๊ณ ๋ฌธํ—Œ [1]. Abdi, H., Dowling, W.J., Valentin, D., Edelman, B., & Posamentier M. (2002). Experimental Design and research methods. Unpublished manuscript. Richardson: The University of Texas at Dallas, Program in Cognition. ํŽธ ์ƒ๊ด€๋ถ„์„ ์‹œ์ž‘ ๊ธฐ์–ต ๋ฒ”์œ„(memory span), ๋‚˜์ด(age), ๋ง ์†๋„(speech rate)๋Š” ์–ด๋–ค ๊ด€๋ จ์ด ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด์ž. ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ๋ฅผ ๋ˆ„๋ฅด๋ฉด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ๊ด€๊ณ„์ˆ˜๋Š” "๋ถ„์„->์ƒ๊ด€๋ถ„์„->ํŽธ์ƒ ๊ด€๊ณ„ ์ˆ˜" ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆ˜์™€ ์ œ์–ด ๋ณ€์ˆ˜ ์„ ํƒ ์ œ์–ด ๋ณ€์ˆ˜๋Š” Speech rate์ด๊ณ  ์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ๊ตฌํ•  ๋ณ€์ˆ˜๋Š” age, Memory span์ด๋‹ค. ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ํ†ต์ œ๋ณ€์ˆ˜๊ฐ€ speech rate์ธ ๊ฒฝ์šฐ ๋‘ ๋ณ€์ˆ˜์˜ ํŽธ ์ƒ๊ด€๊ณ„์ˆ˜์ด๋‹ค. ์œ ์˜ ํ™•๋ฅ ์ด 0.415๋กœ ์ƒ๊ด€๊ณ„์ˆ˜๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค. ๋ณ€์ˆ˜์™€ ์ œ์–ด ๋ณ€์ˆ˜ ์„ ํƒ ์ œ์–ด ๋ณ€์ˆ˜๋Š” age์ด๊ณ  ์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ๊ตฌํ•  ๋ณ€์ˆ˜๋Š” Speech rate, Memory span์ด๋‹ค. ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ํ†ต์ œ๋ณ€์ˆ˜๊ฐ€ age์ธ ๊ฒฝ์šฐ ๋‘ ๋ณ€์ˆ˜์˜ ํŽธ ์ƒ๊ด€๊ณ„์ˆ˜์ด๋‹ค. ์œ ์˜ ํ™•๋ฅ ์ด 0.047๋กœ ์ƒ๊ด€๊ณ„์ˆ˜๋Š” ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค. ์ƒ๊ด€๋ถ„์„ ์„ธ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ๊ตฌํ•ด๋ณด์ž. ์ถœ๋ ฅ ๋ฐ ๋ถ„์„ ์„ธ ๋ณ€์ˆ˜์—์„œ ๋‘ ๋ณ€์ˆ˜๋ฅผ ์„ ํƒํ•˜์—ฌ ๊ตฌํ•œ ์„ธ ๊ฐœ์˜ ์ƒ๊ด€๊ณ„์ˆ˜๋Š” ๋ชจ๋‘ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค. ํšŒ๊ท€๋ถ„์„ ํšŒ๊ท€๋ถ„์„๊ณผ ํŽธ ์ƒ๊ด€๋ถ„์„์˜ ๊ด€๊ณ„๊ฐ€ ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€ ์•Œ์•„๋ณด์ž. ํšŒ๊ท€๋ถ„์„์€ "๋ถ„์„->ํšŒ๊ท€๋ถ„์„->์„ ํ˜•" ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆ˜ ์„ค์ • ๋ฐ ๋ชจํ˜• ์„ ํƒ ์ข…์†๋ณ€์ˆ˜์— Memory span, ๋…๋ฆฝ๋ณ€์ˆ˜์— age, Speech rate, ๋ฐฉ๋ฒ•์— ์ž…๋ ฅ์„ ์„ค์ •ํ•œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ฐ ๋ถ„์„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ age, Speech rate ๋ณ€์ˆ˜์˜ ์œ ์˜ ํ™•๋ฅ ์€ 0.415, 0.047๋กœ ํŽธ ์ƒ๊ด€๊ณ„์ˆ˜์˜ ๊ฒฐ๊ด๊ฐ’๊ณผ ๋™์ผํ•˜๋‹ค. ์ฆ‰ ํšŒ๊ท€๋ถ„์„ ๋ชจํ˜•์—์„œ ๋ฐฉ๋ฒ•์— ์ž…๋ ฅ์„ ์„ ํƒํ•˜๋ฉด ํŽธ ์ƒ๊ด€๊ณ„์ˆ˜ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™๋‹ค. ๋‹จ์ˆœ ํšŒ๊ท€๋ถ„์„ ๋…๋ฆฝ๋ณ€์ˆ˜ 1๊ฐœ์™€ ์ข…์†๋ณ€์ˆ˜ 1๊ฐœ์— ๋Œ€ํ•œ ํšŒ๊ท€๋ถ„์„์„ ์‹คํ–‰ํ•˜์ž. ์ข…์†๋ณ€์ˆ˜์— Memory span, ๋…๋ฆฝ๋ณ€์ˆ˜์— age, ๋ฐฉ๋ฒ•์— ์ž…๋ ฅ์„ ์„ค์ •ํ•œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ฐ ๋ถ„์„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ Memory span๊ณผ age ๋ณ€์ˆ˜์˜ ์œ ์˜ ํ™•๋ฅ ์€ 0.054๋กœ ์ƒ๊ด€๊ณ„์ˆ˜์˜ ๊ฒฐ๊ด๊ฐ’๊ณผ ๋™์ผํ•˜๋‹ค. ๋‹จ์ˆœ ํšŒ๊ท€๋ถ„์„ ๋…๋ฆฝ๋ณ€์ˆ˜ 1๊ฐœ์™€ ์ข…์†๋ณ€์ˆ˜ 1๊ฐœ์— ๋Œ€ํ•œ ํšŒ๊ท€๋ถ„์„์„ ์‹คํ–‰ํ•˜์ž. ์ข…์†๋ณ€์ˆ˜์— Memory span, ๋…๋ฆฝ๋ณ€์ˆ˜์— Speech rate, ๋ฐฉ๋ฒ•์— ์ž…๋ ฅ์„ ์„ค์ •ํ•œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ฐ ๋ถ„์„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ Memory span๊ณผ Speech rate ๋ณ€์ˆ˜์˜ ์œ ์˜ ํ™•๋ฅ ์€ 0.004๋กœ ์ƒ๊ด€๊ณ„์ˆ˜์˜ ๊ฒฐ๊ด๊ฐ’๊ณผ ๋™์ผํ•˜๋‹ค. 05. ํ‰๊ท  ๋น„๊ต ํ‰๊ท  ๋น„๊ต์— ๋Œ€ํ•œ ๊ฒ€์ • ๋‹จ์ผ ํ‘œ๋ณธ ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ๋Œ€์‘ ํ‘œ๋ณธ ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ 1. ๋‹จ์ผ ํ‘œ๋ณธ ๋‹จ์ผ ํ‘œ๋ณธ ํ‰๊ท  ๋น„๊ต ์ด ๋ถ„์„๋ฐฉ๋ฒ•์€ ํ•œ ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ํŠน์ •ํ•œ ๊ฐ’์ด๋ผ๊ณ  ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณธ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ˆ˜์—… ๋“ฃ๋Š” ํ•™์ƒ ์ค‘ ๋‚จํ•™์ƒ ํ‚ค์˜ ํ‰๊ท ์ด 175cm ์ธ์ง€ ๊ถ๊ธˆํ•˜๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ๋‚จํ•™์ƒ ํ‰๊ท ์ด 175cm๋ณด๋‹ค ๊ฐ™์€์ง€, ํฐ์ง€, ์ž‘์€์ง€๋ฅผ ๊ณผํ•™์ ์œผ๋กœ ๊ฒฐ์ •ํ•œ๋‹ค. ๋‹จ์ผ ํ‘œ๋ณธ ํ‰๊ท  ๋น„๊ต ์ด๋ก ์  ๋‚ด์šฉ ์ด ๊ฒ€์ •์€ ํ•œ ์ง‘๋‹จ์˜ ํ‰๊ท ๊ฐ’์ด ํŠน์ •ํ•œ ๊ฐ’์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. โ‘  ๊ท€๋ฌด๊ฐ€์„ค 0 ฮผ ฮผ (์˜๋ฏธ : ์–ด๋Š ์ง‘๋‹จ์˜ ํ‰๊ท ์€ 0 ์ด๋‹ค.)์ด๋ฉฐ, ์˜ˆ์ „์— ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ ์•Œ๋ ค์ง„ ํ‰๊ท ์ด๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ๋Š” ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์—ฐ๊ตฌ์ž๊ฐ€ ์ •ํ•œ๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ฮผ ฮผ (์˜๋ฏธ : ์–ด๋Š ์ง‘๋‹จ์˜ ํ‰๊ท ์€ 0 ๋ณด๋‹ค ํฌ๋‹ค.) ๋‹จ ์ธก ๊ฒ€์ • ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ฮผ ฮผ (์˜๋ฏธ : ์–ด๋Š ์ง‘๋‹จ์˜ ํ‰๊ท ์€ 0 ๋ณด๋‹ค ์ž‘๋‹ค.) ๋‹จ ์ธก ๊ฒ€์ • ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ฮผ ฮผ (์˜๋ฏธ : ์–ด๋Š ์ง‘๋‹จ์˜ ํ‰๊ท ์€ 0 ์ด ์•„๋‹ˆ๋‹ค. ์ฆ‰ 0 ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ 0 ๋ณด๋‹ค ์ž‘๋‹ค) ์–‘์ธก๊ฒ€์ • โ‘ก SPSS๋กœ ํ†ต๊ณ„์  ๋ชจํ˜•์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”๋‹ค. โ‘ข ์—ฐ๊ตฌ์ž๋Š” SPSS ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ ์œ ์˜ ํ™•๋ฅ ์„ ํ™•์ธํ•˜์—ฌ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์˜ ๊ธฐ๊ฐ, ์ฑ„ํƒ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. SPSS ๋ถ„์„ ๊ณผ์ • ์–ด๋–ค ์ง‘๋‹จ์—์„œ ๋‚จ์ž ํ‚ค์˜ ํ‰๊ท ์ด 175๋ณด๋‹ค ์ž‘๋‹ค๊ณ  ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์ •ํ•ด ๋ณด์ž. ๋ถ„์„์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ๋ฅผ ๋ˆ„๋ฅด๋ฉด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠน์ •ํ•œ ๊ฐ’ ์„ ํƒ ๋จผ์ € ์ž๋ฃŒ๋Š” ๋‚จ, ์—ฌ ๋ชจ๋‘ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ๋‚จ์ž ์ž๋ฃŒ๋งŒ ๋ณ„๋„๋กœ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์„ ํƒํ•œ๋‹ค. ํŠน์ • ๊ฐ’๋งŒ ์„ ํƒํ•˜๋ ค๋ฉด ๋ฐ์ดํ„ฐ->์ผ€์ด์Šค ์„ ํƒ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ์กฐ๊ฑด์„ ๋งŒ์ž‘ํ•˜๋Š” ์ผ€์ด์Šค ์„ค์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ผ€์ด์Šค์—์„œ ์กฐ๊ฑด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  ์„ฑ๋ณ„=2๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋‚จ์ž ์ž๋ฃŒ๋งŒ ์„ ํƒ๋œ๋‹ค. ์„ ํƒํ•œ ์ผ€์ด์Šค ํ™•์ธ ๋‹ค์Œ ํ™”๋ฉด์€ ์—ฌ์ž ์ž๋ฃŒ๋Š” ์„ ํƒ๋˜์ง€ ์•Š์•˜๊ณ  ๋‚จ์ž ์ž๋ฃŒ๋งŒ ์„ ํƒ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ์ผ ํ‘œ๋ณธ ๊ฒ€์ • ์‹คํ–‰ ์ผํ‘œ๋ณธ ํ‰๊ท  ๋น„๊ต๋Š” ๋ถ„์„->ํ‰๊ท  ๋น„๊ต->์ผํ‘œ๋ณธ T ๊ฒ€์ • ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๊ฒ€์ • ๋ณ€์ˆ˜์™€ ๊ฒ€์ • ๊ฐ’ ์„ค์ • ๊ฒ€์ • ๋ณ€์ˆ˜์— ํ‚ค ๋ณ€์ˆ˜๋ฅผ ํด๋ฆญํ•˜์—ฌ ์ž…๋ ฅํ•˜๊ณ , ๊ฒ€์ • ๊ฐ’์— ๊ท€๋ฌด๊ฐ€์„ค 175๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ์™€ ๋ถ„์„ ๋ถ„์„ ๊ฒฐ๊ณผ ์ผํ‘œ๋ณธ ํ‰๊ท ์€ 171.37์ด๊ณ  ๊ท€๋ฌด๊ฐ€์„ค 0 175 ๊ฐ€ ์ฐธ์ธ์ง€ ๊ฒ€์ •ํ•œ ๊ฒฐ๊ณผ ์œ ์˜ ํ™•๋ฅ ์ด 0.006์œผ๋กœ ๋งค์šฐ ์œ ์˜ํ•œ ๊ฐ’์ด ๋‚˜์™”๊ธฐ์— ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ‰๊ท  ์ฐจ์ด(171.37-175)์— ๋Œ€ํ•œ 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ( 6.10 โˆ’ 1.17 ) ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ๊ฐ ๊ฐ’์— 175๋ฅผ ๋”ํ•œ ( 168.9 173.83 ) ์ด๋‹ค. 2. ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ํ‰๊ท  ๋น„๊ต ์ด ๋ถ„์„๋ฐฉ๋ฒ•์€ ์„œ๋กœ ๋…๋ฆฝ์ธ ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๊ฐ™์€์ง€ ์•Œ์•„๋ณธ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์„ค๋ณ„์— ๋”ฐ๋ผ ํ‰๊ท  ํ‚ค๊ฐ€ ๊ฐ™์€์ง€, ๋‹ค๋ฅธ์ง€ ๊ถ๊ธˆํ•˜๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์„ ๋น„๊ตํ•œ๋‹ค. ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ํ‰๊ท  ๋น„๊ต ์ด๋ก ์  ๋‚ด์šฉ ์ด ๊ฒ€์ • ๋ฒ•์€ ๋…๋ฆฝ์ธ ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๊ฐ™๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ถ„์„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋“ฑ ๋ถ„์‚ฐ์„ฑ ๊ฒ€์ • ๋จผ์ € ๋‘ ์ง‘๋‹จ์ด ๋ถ„์‚ฐ์ด ๊ฐ™์€์ง€ ๋‹ค๋ฅธ์ง€์— ๋”ฐ๋ผ ํ†ต๊ณ„๋Ÿ‰์„ ๊ณ„์‚ฐํ•˜๋Š” ์‹์ด ๋‹ค๋ฅด๋‹ค. ๋”ฐ๋ผ์„œ ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์ด ๊ฐ™์€์ง€ ๊ฒ€์ •ํ•œ๋‹ค. ์ด๊ฒƒ์„ ๋“ฑ ๋ถ„์‚ฐ์„ฑ ๊ฒ€์ •์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ๋ถ€๊ต ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. โ‘  ๊ฐ€์„ค ์„ค์ •(Levene ๊ฒ€์ •) ๊ท€๋ฌด๊ฐ€์„ค 0 ฯƒ 2 ฯƒ 2 (์˜๋ฏธ : ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์€ ๊ฐ™๋‹ค.) ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ฯƒ 2 ฯƒ 2 (์˜๋ฏธ : ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์€ ๊ฐ™์ง€ ์•Š๋‹ค.) โ‘ก SPSS์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ Levene ๋“ฑ ๋ถ„์‚ฐ ๊ฒ€์ •์˜ ์œ ์˜ ํ™•๋ฅ ์„ ํ™•์ธํ•œ๋‹ค. โ‘ข ๋“ฑ ๋ถ„์‚ฐ์„ฑ์— ๋Œ€ํ•œ ๊ฒ€์ • ๊ฒฐ๊ณผ ํ•ด์„ ๋ฐ ํ‰๊ท  ๋น„๊ต ๋ฐฉ๋ฒ• ์„ ํƒ ์œ ์˜ ํ™•๋ฅ ์ด 0.05๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์€ ์„œ๋กœ ๋‹ค๋ฅด๋‹ค. ์œ ์˜ ํ™•๋ฅ ์ด 0.05๋ณด๋‹ค ํฌ๋ฉด ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์€ ์„œ๋กœ ๊ฐ™๋‹ค. ๋…๋ฆฝ์ธ ๋‘ ์ง‘๋‹จ ํ‰๊ท  ๋น„๊ต ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท  ๋น„๊ต ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. โ‘  ๊ท€๋ฌด๊ฐ€์„ค 0 ฮผ โˆ’ 2 0 (์˜๋ฏธ : ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท  ์ฐจ์ด๋Š” 0์ด๋‹ค.) ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ์€ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์—ฐ๊ตฌ์ž๊ฐ€ ์ •ํ•œ๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ฮผ โˆ’ 2 0 (์˜๋ฏธ : ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท  ์ฐจ์ด๋Š” 0๋ณด๋‹ค ํฌ๋‹ค.) ๋‹จ ์ธก ๊ฒ€์ • ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ฮผ โˆ’ 2 0 (์˜๋ฏธ : ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท  ์ฐจ์ด๋Š” 0๋ณด๋‹ค ์ž‘๋‹ค.) ๋‹จ ์ธก ๊ฒ€์ • ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ฮผ โˆ’ 2 0 (์˜๋ฏธ : ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท  ์ฐจ์ด๋Š” 0์ด ์•„๋‹ˆ๋‹ค. ์ฆ‰ ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท  ์ฐจ์ด๋Š” 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ 0๋ณด๋‹ค ์ž‘๋‹ค) ์–‘์ธก๊ฒ€์ • โ‘ก SPSS๋กœ ํ†ต๊ณ„์  ๋ชจํ˜•์— ๋Œ€ํ•˜์—ฌ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”๋‹ค. โ‘ข ์—ฐ๊ตฌ์ž๋Š” SPSS ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ ์œ ์˜ ํ™•๋ฅ ์„ ํ™•์ธํ•˜์—ฌ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์˜ ๊ธฐ๊ฐ, ์ฑ„ํƒ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ•ด์„ํ•œ๋‹ค. SPSS ๋ถ„์„๋ฐฉ๋ฒ• ๋…๋ฆฝ ํ‘œ๋ณธ ๊ฒ€์ • ์‹คํ–‰ ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ํ‰๊ท  ๋น„๊ต๋Š” ๋ถ„์„->ํ‰๊ท  ๋น„๊ต->๋…๋ฆฝ ํ‘œ๋ณธ T ๊ฒ€์ • ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ถ„์„์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ๋ฅผ ๋ˆ„๋ฅด๋ฉด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒ€์ • ๋ณ€์ˆ˜์™€ ์ง‘๋‹จ ๋ณ€์ˆ˜ ์„ค์ • ๊ฒ€์ • ๋ณ€์ˆ˜์— ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ์ง‘๋‹จ ๋ณ€์ˆ˜์— ์„ฑ๋ณ„์„ ์ž…๋ ฅํ•˜๊ณ  ์ง‘๋‹จ ์ •์˜๋ฅผ ๋ˆŒ๋Ÿฌ ์„ฑ๋ณ„์— ์ž…๋ ฅ๋œ ์ˆซ์ž ๊ฐ’ 1, 2๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ๋ถ„์„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ถ„์„์—์„œ ์ง‘๋‹จ ํ†ต๊ณ„๋Ÿ‰์—์„œ ์—ฌ์ž ํ‰๊ท  ํ‚ค๋Š” 160.98์ด๊ณ  ๋‚จ์ž ํ‰๊ท  ํ‚ค๋Š” 171.37๋กœ ๊ณ„์‚ฐ๋˜์—ˆ์œผ๋ฉฐ ํ‰๊ท  ์ฐจ์ด๊ฐ€ ํ‘œ์ค€ํŽธ์ฐจ๋ณด๋‹ค ํฌ๋ฏ€๋กœ ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ์ง์ž‘ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค. ๋ถ„์„ ๊ณผ์ •์€ ๋‘ ์ง‘๋‹จ ๋ถ„์‚ฐ์ด ๊ฐ™์€์ง€์— ๋”ฐ๋ผ ๊ณ„์‚ฐ์ด ๋‹ค๋ฅด๋ฏ€๋กœ ๋‘ ์ง‘๋‹จ ๋“ฑ ๋ถ„์‚ฐ์„ฑ์„ ๋จผ์ € ๊ฒ€์ •ํ•œ๋‹ค. ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์ด ๊ฐ™์€์ง€์— ๋Œ€ํ•œ ๊ท€๋ฌด๊ฐ€์„ค 0 ฯƒ 2 2 =์— ๋Œ€ํ•œ ๊ฒ€์ • ๊ฒฐ๊ณผ ์œ ์˜ ํ™•๋ฅ ์ด 0.394๋กœ ์œ ์˜ํ•˜์ง€ ์•Š๋Š” ๊ฐ’์ด๋ฏ€๋กœ ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์€ ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋“ฑ ๋ถ„์‚ฐ์„ฑ ๊ฒ€์ • ๊ฒฐ๊ณผ ์œ ์˜ํ•˜๋ฉด ๋“ฑ ๋ถ„์‚ฐ์„ฑ์„ ๊ฐ€์ •ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋“ฑ ๋ถ„์‚ฐ์„ ๊ฐ€์ •ํ•˜์ง€ ์•Š์Œ์— ์žˆ๋Š” ํ†ต๊ณ„๋Ÿ‰์œผ๋กœ ํ‰๊ท  ์ฐจ์ด์— ๋Œ€ํ•˜์—ฌ ๊ฐ€์„ค๊ฒ€์ •ํ•œ๋‹ค. ๋“ฑ ๋ถ„์‚ฐ์ธ ๊ฒฝ์šฐ ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๊ฐ™์€์ง€ ๊ท€๋ฌด๊ฐ€์„ค 0 ฮผ = 2 ์— ๊ฒ€์ • ๊ฒฐ๊ณผ ์œ ์˜ ํ™•๋ฅ ์ด 0.000์œผ๋กœ ๋งค์šฐ ์œ ์˜ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๊ณ„์‚ฐ๋˜์—ˆ๊ธฐ์— ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์€ ํ†ต๊ณ„์ ์œผ๋กœ ๊ฐ™์ง€ ์•Š๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. 3. ๋Œ€์‘ ํ‘œ๋ณธ ๋Œ€์‘ ํ‘œ๋ณธ ํ‰๊ท  ๋น„๊ต ๋‘ ์ง‘๋‹จ์˜ ์ž๋ฃŒ๊ฐ€ ์Œ์œผ๋กœ ๋œ ๊ฒฝ์šฐ, ๋‘ ์ง‘๋‹จ ์ฐจ์ด์˜ ํ‰๊ท ์„๋ผ๊ณ  ํ•  ๋•Œ ์ด ๊ฐ’์— ๋Œ€ํ•œ ๊ฒ€์ • ๋ฐฉ๋ฒ•์ด๋‹ค. ์Œ์œผ๋กœ ๋œ ์ž๋ฃŒ๋Š” ํ•œ ๊ฐœ์ฒด์—์„œ ๋‘ ๋ฒˆ ์ž๋ฃŒ๋ฅผ ๊ด€์ธกํ•˜๊ฑฐ๋‚˜ ๋™์ผํ•œ ์ข…๋ฅ˜์˜ ๊ธฐ๊ณ„ ๋‘ ๋Œ€์—์„œ ์ž๋ฃŒ๋ฅผ ๊ด€์ธกํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค. ๋™์ผํ•œ ์ข…๋ฅ˜ ๊ธฐ๊ณ„์ธ ๊ฒฝ์šฐ, ์ฐจ๋กœ ์˜ˆ๋กœ ํ•˜๋ฉด ์†Œ๋‚˜ํƒ€ 1๋Œ€์™€ ๋‹ค๋ฅธ ์†Œ๋‚˜ํƒ€ 1๋Œ€๋ฅผ ๊ฐ–๊ณ  ์ธก์ •ํ•œ๋‹ค. ๊ฐœ์ฒด๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ๋‹ค๋ฅด์ง€๋งŒ ๋ชจ๋“  ์กฐ๊ฑด์€ ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ๋งŒ์ผ ๋‘ ์ง‘๋‹จ์˜ ์ฐจ์ด๊ฐ€ ์—†๋‹ค๋ฉด ๋Š” 0์ด ๋œ๋‹ค. ๋Œ€์‘ ํ‘œ๋ณธ ํ‰๊ท  ๋น„๊ต ์ด๋ก ์  ๋‚ด์šฉ โ‘  ๊ท€๋ฌด๊ฐ€์„ค 0 ฮด ฮด (์˜๋ฏธ : ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋‘ ์ง‘๋‹จ ์ฐจ์ด์˜ ํ‰๊ท ์€ 0 ์ด๋‹ค.) ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ์€ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์—ฐ๊ตฌ์ž๊ฐ€ ์ •ํ•œ๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ฮด ฮด (์˜๋ฏธ : ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋‘ ์ง‘๋‹จ ์ฐจ์ด์˜ ํ‰๊ท ์€ 0๋ณด๋‹ค ํฌ๋‹ค.) ๋‹จ ์ธก ๊ฒ€์ • ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ฮด ฮด (์˜๋ฏธ : ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋‘ ์ง‘๋‹จ ์ฐจ์ด์˜ ํ‰๊ท ์€ 0๋ณด๋‹ค ์ž‘๋‹ค.) ๋‹จ ์ธก ๊ฒ€์ • ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ฮด ฮด (์˜๋ฏธ : ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋‘ ์ง‘๋‹จ ์ฐจ์ด์˜ ํ‰๊ท ์€ 0์ด ์•„๋‹ˆ๋‹ค. ์–‘์ธก๊ฒ€์ • ์ฆ‰ ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋‘ ์ง‘๋‹จ ์ฐจ์ด์˜ ํ‰๊ท ์€ 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ 0๋ณด๋‹ค ์ž‘๋‹ค) โ‘ก SPSS๋กœ ํ†ต๊ณ„์  ๋ชจ๋ธ์— ๋Œ€ํ•˜์—ฌ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”๋‹ค. โ‘ข ์—ฐ๊ตฌ์ž๋Š” SPSS ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ ์œ ์˜ ํ™•๋ฅ ์„ ํ™•์ธํ•˜์—ฌ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์˜ ๊ธฐ๊ฐ, ์ฑ„ํƒ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ•ด์„ํ•œ๋‹ค. SPSS ๋ถ„์„ ๊ณผ์ • ๋Œ€์‘ ํ‘œ๋ณธ ํ‰๊ท  ๋น„๊ต๋Š” ํ•œ ๊ฐœ์ฒด์—์„œ ๋‘ ๋ฒˆ ๋ฐ˜๋ณต ์ธก์ •ํ•œ ์ž๋ฃŒ์ด๋‹ค. ๋‹ค์Œ ๋ถ„์„ ์ž๋ฃŒ๋Š” ๋ง์„ ๋”๋“ฌ๋Š” ํ™˜์ž์— ๋Œ€ํ•˜์—ฌ 10๋ช…์„ ๋Œ€์ƒ์œผ๋กœ ์•ฝ์„ ํˆฌ์—ฌํ•˜๊ณ  ๋ง ๋”๋“ฌ๋Š” ํšŸ์ˆ˜๋ฅผ ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. ์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ๋ฅผ ๋ˆ„๋ฅด๋ฉด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋Œ€์‘ ํ‘œ๋ณธ ์‹คํ–‰ ๋Œ€์‘ ํ‘œ๋ณธ ํ‰๊ท  ๋น„๊ต๋Š” ๋ถ„์„ -> ํ‰๊ท  ๋น„๊ต -> ๋Œ€์šฉ ํ‘œ๋ณธ T ๊ฒ€์ • ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ถ„์„ ๊ฒฐ๊ณผ ์œ ์˜ ํ™•๋ฅ ์ด 0.020์œผ๋กœ ์œ ์˜ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๊ณ„์‚ฐ๋˜์—ˆ์œผ๋ฏ€๋กœ ๋‘ ๋ณ€์ˆ˜ ์ฐจ์ด์˜ ํ‰๊ท ์€ ํ†ต๊ณ„์ ์œผ๋กœ 0์ด ์•„๋‹ˆ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. ๋Œ€์‘ ํ‘œ๋ณธ ๋ณ€์ˆ˜ ์„ค์ • ๋Œ€์‘ ๋ณ€์ˆ˜์— ์น˜๋ฃŒ์ „ ์น˜๋ฃŒ ํ›„ ๋ณ€์ˆ˜๋ฅผ ํ•œ ์ค„์— ๋ชจ๋‘ ๋„ฃ๋Š”๋‹ค. ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ๋ถ„์„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ถ„์„์—์„œ ๋Œ€์‘ ํ‘œ๋ณธ ํ†ต๊ณ„๋Ÿ‰์—์„œ ์น˜๋ฃŒ์ „ ํ‰๊ท ์€ 3.1, ์น˜๋ฃŒ ํ›„ ํ‰๊ท ์€ 1.7๋กœ ์ƒ๋‹นํžˆ ๋ง๋”๋“ฌ ํšŸ์ˆ˜๊ฐ€ ์ค„์–ด๋“  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋œ๋‹ค. ๋‘ ๋ณ€์ˆ˜ ์ฐจ์ด์˜ ํ‰๊ท  ๊ฒ€์ •์€ ๊ท€๋ฌด๊ฐ€์„ค 0 ฮผ = 0 ์ผ ๋•Œ ๊ฒ€์ •์ด๋‹ค. ์—ฌ๊ธฐ์„œ 0 ๋Š” ๋‘ ๋ณ€์ˆ˜ ์ฐจ์ด๋Š” | โˆ’ | ์ด๋‹ค. ๋‘ ๋ณ€์ˆ˜์˜ ์ฐจ์ด๊ฐ€ ์—†๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด 0 0 ์ด๋‹ค. ๊ฒ€์ • ๊ฒฐ๊ณผ ์œ ์˜ ํ™•๋ฅ ์ด 0.02๋กœ ์œ ์˜ํ•˜์˜€์œผ๋ฏ€๋กœ ๋‘ ๋ณ€์ˆ˜ ์ฐจ์ด์˜ ํ‰๊ท  D ์€ 0์ด ์•„๋‹ˆ๋ผ๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ๋งํ•  ์ˆ˜ ์žˆ๋‹ค. ๋Œ€์‘ ํ‘œ๋ณธ์„ ์ผํ‘œ๋ณธ ๋ถ„์„์œผ๋กœ ์‹คํ–‰ ๋ณ€์ˆ˜ ๊ณ„์‚ฐ ๋‘ ๋ณ€์ˆ˜ ์ฐจ์ด๋Š” ๋ณ€ํ™˜ -> ๋ณ€์ˆ˜ ๊ณ„์‚ฐ ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•˜์—ฌ ๊ตฌํ•œ๋‹ค. ๋ณ€์ˆ˜ ๊ณ„์‚ฐ ๊ณผ์ • ๋ชฉํ‘œ ๋ณ€์ˆ˜์— ๋ณ€์ˆ˜๋ช… ์ฐจ์ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์น˜๋ฃŒ์ „ - ์น˜๋ฃŒ ํ›„ ์ˆ˜์‹์„ ๋งˆ์šฐ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅํ•œ๋‹ค. ์ง์ ‘ ์ž…๋ ฅํ•ด๋„ ๋˜์ง€๋งŒ ์˜ค๋ฅ˜๋ฅผ ์ค„์ด๋ ค๋ฉด ๋งˆ์šฐ์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋‹จ์ผ ๋ณ€์ˆ˜๋กœ ๋ณ€์ˆ˜ ๊ณ„์‚ฐ ํ™•์ธ SPSS ๋ฐ์ดํ„ฐ ์ฐฝ์—์„œ ๋ณ€์ˆ˜ ๊ณ„์‚ฐ ๋ฉ”๋‰ด๋กœ ๊ณ„์‚ฐ๋œ ์ฐจ์ด ๋ณ€์ˆ˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผํ‘œ๋ณธ ๊ฒ€์ • ์‹คํ–‰ ๋‹จ์ผ ํ‘œ๋ณธ ๊ฒ€์ •์€ ์ผํ‘œ๋ณธ T ๊ฒ€์ • ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•œ๋‹ค. ์ผํ‘œ๋ณธ ๊ฒ€์ •์—์„œ ๊ฒ€์ • ๋ณ€์ˆ˜์™€ ๊ฒ€์ • ๊ฐ’ ์„ค์ • ๊ฒ€์ • ๋ณ€์ˆ˜์— ์ฐจ์ด ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ฒ€์ • ๊ฐ’์— 0์„ ์ž…๋ ฅํ•œ๋‹ค. ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ํ™•์ธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋‹จ์ผ ํ‘œ๋ณธ ๊ฒ€์ • ๊ฒฐ๊ณผ์™€ ๋Œ€์‘ ํ‘œ๋ณธ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•˜๋‹ค. ๋Œ€์‘ ํ‘œ๋ณธ ๊ฒฐ๊ณผ๋Š” ์œ„์— ์žˆ์œผ๋ฉฐ ๊ทธ๋ฆผ์„ ํด๋ฆญํ•ด๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 4. ๋…๋ฆฝ์ธ k ํ‘œ๋ณธ - ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ๊ด€์ธก ๊ฐ’ ๋ถ„ํ•ด ๊ด€์ธก ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ด€์ธก ๊ฐ’ ์ „์ฒด ํ‰๊ท  ์ฒ˜๋ฆฌ ํŽธ์ฐจ ์ž”์ฐจ ์ธก = ( ์ฒด ๊ท  ) ( ๋ฆฌ ํŽธ ) ( ์ฐจ ) i = ยฏ. ( ยฏ. Y. ) ( i โˆ’ ยฏ. ) ์ดˆ๋“ฑํ•™๊ต ํ•™์ƒ๋“ค์€ ๋ฐ˜๋ณ„๋กœ ์ฑ… ์ฝ๋Š” ๊ถŒ์ˆ˜์˜ ํ‰๊ท ์ด ๋‹ค๋ฅธ์ง€ ์•Œ์•„๋ณด๋ ค๊ณ  1๋…„๊ฐ„ ์ฑ… ์ฝ๋Š” ํšŸ์ˆ˜๋ฅผ ์กฐ์‚ฌํ•˜์˜€๋‹ค. ๋‹ค์Œ์€ ์ดˆ๋“ฑํ•™๊ต 4๊ฐœ ํ•™๊ธ‰์„ ๋Œ€์ƒ์œผ๋กœ 1๋…„๊ฐ„ ์ฑ…์„ ์ฝ์€ ํšŸ์ˆ˜์ด๋‹ค. ๋ฐ˜๋ณ„๋กœ ์ฑ… ์ฝ๋Š” ๊ถŒ์ˆ˜์˜ ํ‰๊ท  ์ฐจ์ด๊ฐ€ ์žˆ์œผ๋ ค๋ฉด ์ง‘๋‹จ ๊ฐ„ ํŽธ์ฐจ(์ฒ˜๋ฆฌ ํŽธ์ฐจ)๊ฐ€ ์ง‘๋‹จ ๋‚ด ํŽธ์ฐจ(์ž”์ฐจ)๋ณด๋‹ค ์ปค์•ผ ํ•œ๋‹ค. ํ•™๊ธ‰ ๊ฐ ํ•™์ƒ์˜ ์ฑ… ์ฝ๋Š” ํšŸ์ˆ˜ ์ „์ฒด ํ‰๊ท  ๋ฐ˜ ๋ฐ˜ ๊ธ‰ ํ•™์˜ ์ฑ… ์ฝ ํšŸ ์ „ ํ‰ Y j ยฏ. ( ๋ฐ˜ ๋ฐ˜ ๋ฐ˜ ๋ฐ˜ ) ( 10 15 12 15 14 18 21 15 17 16 14 15 17 15 18 12 15 17 15 16 15 ) ( 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 ) ํ•™๊ธ‰๋ณ„ ํ‰๊ท  ์ฐจ์ด ํ•™๊ธ‰ ์•ˆ์—์„œ ํ‰๊ท  ์ฐจ์ด ๊ธ‰ ํ‰ ์ฐจ ํ•™ ์•ˆ ์„œ ํ‰ ์ฐจ Y i โˆ’ ยฏ. i โˆ’ ยฏ. ( 3 3 3 3 3 2 2 1 1 1 0 0 0 ) ( 2 โˆ’ 0 โˆ’ 1 โˆ’ 1 โˆ’ โˆ’ 1 1 โˆ’ 0 0 0 ) ์ œ๊ณฑํ•ฉ ๋ถ„ํ•ด ๊ด€์ธก ๊ฐ’ ์ด ํ‰๊ท  ์ฒ˜๋ฆฌ ํŽธ์ฐจ ์ž”์ฐจ ์ด์ œ๊ณฑํ•ฉ์ฒ˜๋ฆฌ์ œ๊ณฑํ•ฉ์ž”์ฐจ์ œ๊ณฑํ•ฉ ( ์ธก โˆ’ ํ‰ ) ( ๋ฆฌ ํŽธ ) ( ์ฐจ ) i 1 โˆ‘ = n ( i โˆ’ โ€•. ) = i 1 โˆ‘ = n [ ( โ€•. Y. ) ( i โˆ‘ = k j 1 i [ ( โ€•. Y. ) + ( โ€•. Y. ) ( i โˆ’ โ€•. ) ( โˆ‘ = k j 1 i ( โ€•. Y. ) + i 1 โˆ‘ = n 2 ( โ€•. Y. ) ( i โˆ’ โ€•. ) i โˆ‘ = k j 1 i ( โ€•. Y. ) + i 1 2 ( โ€•. Y. ) j 1 i ( i โˆ’ โ€•. ) 0 i โˆ‘ = k i ( โ€•. Y. ) + i 1 โˆ‘ = n ( i โˆ’ โ€•. ) ์ œ ํ•ฉ ์ฒ˜ ์ œ ํ•ฉ ์ž” ์ œ ํ•ฉ ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์ž๋ฃŒ๊ตฌ์กฐ ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„(one-way ANalysis Of VAriance)์— ๋Œ€ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ฒ˜๋ฆฌ ์ง‘๋‹จ ์ฒ˜๋ฆฌ ์ฒ˜๋ฆฌ โ‹ฏ ์ฒ˜๋ฆฌ ์ž๋ฃŒ 11 21 Y 1 12 22 Y 2 โ‹ฎ โ‹ฎ 1 1 2 2 Y n ํ‰๊ท  ยฏ 1. ยฏ 2. Y g ์ „์ฒด ํ‰๊ท  ยฏ. ํ•ฉ๊ณ„ j 1 1 1 โˆ‘ = n Y j โˆ‘ = n Y j ์ผ์› ๋ฐฐ์น˜ ์ž๋ฃŒ๊ตฌ์กฐ ์ „์ฒด ํ‰๊ท ์€ ยฏ. โˆ‘ = g j 1 i i โˆ‘ = g i ์ด๊ณ  ๊ฐ ์ฒ˜๋ฆฌ ์ง‘๋‹จ ํ‰๊ท ์€ ยฏ. โˆ‘ = n Y j i ์ด๋‹ค. ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ๋ชจํ˜• ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ๋ชจํ˜•์€ (1) i = + i ฯต j ฯต j N ( , 2 ) ์ด๋‹ค. ์‹ (1)์—์„œ๋Š” ์ƒ์ˆ˜ํ•ญ,๋Š” ์š”์ธ ๋ณ€์ˆ˜, ์€ ์˜ค์ฐจ์ด๋‹ค. ์‹ (1)์„ ์ž์„ธํžˆ ๋‚˜ํƒ€๋‚ด๋ฉด (2) i = + ( ยฏ. Y. ) ( i โˆ’ ยฏ. ) ์ด๋‹ค. ์‹ (2)์—์„œ ์–‘๋ณ€์„ ์ œ๊ณฑํ•˜๊ณ  ํ•ฉ์„ ๊ตฌํ•˜๋ฉด ์ด ์ œ๊ณฑํ•ฉ ์ƒ์ˆ˜ (3) i 1 โˆ‘ = n Y j โž ์ œ ํ•ฉ โˆ‘ = g j 1 i ยฏ. โž ์ˆ˜ โˆ‘ = g j 1 i ( ยฏ. Y. ) โž S r + i 1 โˆ‘ = n ( i โˆ’ ยฏ. ) โž S ์‹ (3)์—์„œ ์ „์ฒด ํ‰๊ท ์ธ ์ƒ์ˆ˜ํ•ญ ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ๋ณด๋‚ด๊ณ  ์–‘๋ณ€์˜ ์ œ๊ณฑํ•ฉ(sum of squares)์„ ๊ตฌํ•˜๋ฉด (4) i 1 โˆ‘ = n ( i โˆ’ ยฏ. ) โž S = i 1 โˆ‘ = n ( ยฏ. Y. ) โž S r + i 1 โˆ‘ = n ( i โˆ’ ยฏ. ) โž S์ด๋ฉฐ ๊ฐ ์ œ๊ณฑํ•ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. S (sum of squares total)๋Š” ์ „์ฒด ์ œ๊ณฑํ•ฉ S r (sum of squares treatment)๋Š” ์ฒ˜๋ฆฌ ์ œ๊ณฑํ•ฉ S (sum of squares error)๋Š” ์˜ค์ฐจ ์ œ๊ณฑํ•ฉ ๋‹ค์Œ์€ ์ฒ˜๋ฆฌ ์ง‘๋‹จ์— ๋Œ€ํ•œ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค. ์€ ์ „์ฒด ์ž๋ฃŒ์ˆ˜,๋Š” ์ฒ˜๋ฆฌ ์ง‘๋‹จ ์ˆ˜์ด๋‹ค. ์š”์ธ ์ œ๊ณฑํ•ฉ ์ž์œ ๋„ ํ‰๊ท  ์ œ๊ณฑํ•ฉ F ๊ฐ’ r a e e t S r g 1 S r = S / ( โˆ’ ) S r / S E r r S N g S = S / ( โˆ’ ) o a S T โˆ’ ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ๋ชจํ˜•์˜ ๊ฐ€์ • ํ†ต๊ณ„ ๋ชจํ˜• i์˜ ๊ฐ€์ •์€ ์„œ๋กœ ๋…๋ฆฝ ์ •๊ทœ๋ถ„ํฌ ๋ถ„์‚ฐ์ด ๋ชจ๋‘ ๋™์ผ ์ด๋‹ค. ์ง‘๋‹จ๋ณ„ ํ‰๊ท  ๋น„๊ต ๊ฐ€์„ค์€ "๋ชจ๋“  ์ง‘๋‹จ์˜ ํ‰๊ท ์€ ๋ชจ๋‘ ๊ฐ™๋‹ค"์ด๋ผ๋ฉฐ 0 ฮฑ = 2 โ‹ฏ ฮฑ์ด๋‹ค. ์‚ฌํ›„ ๊ฒ€์ • ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ์ง‘๋‹จ๋ณ„ ํ‰๊ท  ๋น„๊ต์—์„œ ํ‰๊ท ์ด ๊ฐ™์ง€ ์•Š์€ ์ง‘๋‹จ์ด ์žˆ๋‹ค๋ฉด ์–ด๋–ค ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๋‹ค๋ฅธ์ง€ ๊ฒ€์ •ํ•œ๋‹ค. ์‚ฌํ›„ ๊ฒ€์ •๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์œผ๋ฉฐ SPSS์—์„œ ๋ถ„์„๋ฐฉ๋ฒ•์€ ์•ฝ 20์—ฌ ๊ฐœ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ๋ถ„์„๋ฐฉ๋ฒ•์€ ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์„ ๋น„๊ตํ•˜๋ฉฐ ๊ฐœ ์ง‘๋‹จ์ธ ๊ฒฝ์šฐ ํ‰๊ท ์„ ๋น„๊ตํ•˜๋Š” ๊ฐ€์ง“์ˆ˜๋Š” C = ( โˆ’ )์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ง‘๋‹จ ์ˆ˜๊ฐ€ 5๊ฐœ์ด๋ฉด C = ร— 2 10 ์ด๋‹ค. ๊ฐ€์„ค์€ 0 | i ฮฑ | 0 โ‰ ์ด๋‹ค. ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์‹ค์Šต์ž๋ฃŒ ๋ถ„์„์— ์‚ฌ์šฉํ•  ์ž๋ฃŒ์ด๋‹ค. ์ด ์ž๋ฃŒ๋Š” ์ด์› ๋ฐฐ์น˜ ๋ถ„์„์—์„œ ํ•œ ์š”์ธ์„ ๋” ๋‚˜๋ˆ„์–ด ์‚ฌ์šฉํ•œ๋‹ค. ์š”์ธ 1 41 43 50 51 43 53 54 46 45 55 56 60 58 62 62 2 56 47 45 46 49 58 54 49 61 52 62 59 55 68 63 3 43 56 48 46 47 59 46 58 54 55 69 63 56 62 67 Maxwell & Delaney 2004 p339 ์š”์ธ 1 41 43 50 56 47 45 46 49 43 56 48 46 47 2 51 43 53 54 46 58 54 49 61 52 62 59 46 58 54 3 45 55 56 60 58 62 62 59 55 68 63 55 69 63 56 62 67 Maxwell & Delaney 2004 p339 SPSS ๋ถ„์„ ๊ณผ์ • SPSS ์‹ค์Šต์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. 2์—ด, 3์—ด์— ์žˆ๋Š” grpA, grpB ๋ณ€์ˆ˜๋Š” ๋ถ„์‚ฐ๋ถ„์„์—์„œ ์‚ฌ์šฉํ•  ๋…๋ฆฝ๋ณ€์ˆ˜์ด๊ณ  4์—ด ~ 11์—ด์€ ํšŒ๊ท€๋ถ„์„์—์„œ ์‚ฌ์šฉํ•  ๋…๋ฆฝ๋ณ€์ˆ˜๋กœ ๋”๋ฏธ ๋ณ€์ˆ˜์ด๋‹ค. ์ผ์› ๋ฐฐ์น˜ ์‹คํ–‰ ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„์€ ๋ถ„์„ -> ํ‰๊ท  ๋น„๊ต -> ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ์š”์ธ ๋ณ€์ˆ˜์™€ ์ข…์†๋ณ€์ˆ˜ ์„ค์ • ์ข…์†๋ณ€์ˆ˜์— ๋ณ€์ˆซ๊ฐ’์„ ๋„ฃ๊ณ , ์š”์ธ์— ๋ณ€์ˆ˜ grpA๋ฅผ ๋ˆ„๋ฅด๊ณ  ์˜ต์…˜ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ์˜ต์…˜ ์„ค์ • ์˜ต์…˜์—์„œ ๊ธฐ์ˆ  ํ†ต๊ณ„, ๋ถ„์‚ฐ ๋™์งˆ์„ฑ ๊ฒ€์ •, Welch๋ฅผ ์„ ํƒํ•˜๊ณ  ๊ณ„์† ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ๊ธฐ์ˆ  ํ†ต๊ณ„ : ๊ฐ ์š”์ธ์˜ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ํ‰๊ท , ๋ถ„์‚ฐ ๋“ฑ ๊ธฐ์ดˆํ†ต๊ณ„๋Ÿ‰ ํ™•์ธ ๋ถ„์‚ฐ ๋™์งˆ์„ฑ ๊ฒ€์ • : ์š”์ธ์˜ ๊ฐ ์ง‘๋‹จ๋ณ„ ๋ถ„์‚ฐ์ด ๋™์ผํ•œ์ง€ ๊ฒ€์ • Welch : ์ง‘๋‹จ๋ณ„ ๋ถ„์‚ฐ์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ, ํ‰๊ท ์ด ํ‰๊ท ์ด ๋ชจ๋‘ ๊ฐ™์€์ง€ ๊ฒ€์ • ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ๋ถ„์„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ†ต๊ณ„์  ํ•ด์„์€ ์š”์ธ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์ด ๊ฐ™์€์ง€ ๋“ฑ ๋ถ„์‚ฐ์„ฑ์„ ๊ฒ€์ •ํ•œ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ฯƒ 2 ฯƒ 2 ฯƒ 2 ์ด๋‹ค. ์š”์ธ์— ๋Œ€ํ•œ ์ง‘๋‹จ์˜ ํ‰๊ท  ๋น„๊ต๋Š” ๊ท€๋ฌด๊ฐ€์„ค 0 ฮผ = 2 ฮผ์ธ ๊ฒฝ์šฐ ๊ฐ€์„ค๊ฒ€์ •์—์„œ ๋“ฑ ๋ถ„์‚ฐ์„ฑ์„ ๊ฐ€์ •ํ•˜๋ฉด ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ(ANOVA) ๋“ฑ ๋ถ„์‚ฐ์„ฑ์„ ๊ฐ€์ •ํ•  ์ˆ˜ ์—†์œผ๋ฉด Brown-Forsythe ๊ฒ€์ • ๋˜๋Š” Welch ๊ฒ€์ •์œผ๋กœ ๊ฒ€์ •ํ•œ๋‹ค. ๊ธฐ์ˆ  ํ†ต๊ณ„์—์„œ ๋ชจ๋“  ์ง‘๋‹จ์˜ ํ‰๊ท  ์ฐจ์ด๊ฐ€ ํ‘œ์ค€ํŽธ์ฐจ ์ฐจ์ด๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์ง‘๋‹จ๋ณ„ ํ‰๊ท  ์ฐจ์ด๋Š” ์—†์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ถ„์‚ฐ์˜ ๋™์งˆ์„ค ๊ฒ€์ • ๊ฒฐ๊ณผ ๋ชจ๋“  ํ•ญ๋ชฉ์—์„œ ์œ ์˜ ํ™•๋ฅ ์ด 0.05๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์˜ํ•˜์ง€ ์•Š์•„ ๊ท€๋ฌด๊ฐ€์„ค 0 ฯƒ 2 ฯƒ 2 ฯƒ 2 ์„ ๊ธฐ๊ฐํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๊ฐ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์€ ๊ฐ™๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ๊ฐ ์ง‘๋‹จ๋ณ„ ํ‰๊ท  ๋น„๊ต๋Š” ๊ฐ ์ง‘๋‹จ๋ณ„ ๋ถ„์‚ฐ์ด ๋ชจ๋‘ ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ๊ธฐ์— ANOVA๋กœ ๊ท€๋ฌด๊ฐ€์„ค 0 ฮผ = 2 ฮผ ๊ฒ€์ •ํ•œ ๊ฒฐ๊ณผ, ์œ ์˜ ํ™•๋ฅ ์ด 0.401๋กœ ์œ ์˜ํ•˜์ง€ ์•Š์•„ ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๊ฐ ์ง‘๋‹จ ํ‰๊ท ์€ ๋ชจ๋‘ ๊ฐ™๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์ผ ๊ฐ ์ง‘๋‹จ ํ‰๊ท ์œผ ๋ชจ๋‘ ๊ฐ™๋‹ค๊ณ  ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, ํ‰๊ท ์ด ๋‹ค๋ฅธ ์ง‘๋‹จ์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌํ›„ ๊ฒ€์ •์œผ๋กœ ์–ด๋–ค ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๊ฐ™์ง€ ์•Š์€์ง€ ๊ฒ€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” ์—ฌ๊ธฐ์„œ ๋ถ„์„์„ ์ข…๋ฃŒํ•œ๋‹ค. B ์š”์ธ์— ๋Œ€ํ•œ ๋ถ„์„ ๋‹ค๋ฅธ ๋ณ€์ˆ˜ grpB์— ๋Œ€ํ•œ ๋ถ„์‚ฐ๋ถ„์„์„ ํ•œ๋‹ค. ์ข…์†๋ณ€์ˆ˜์— ๊ฐ’, ์š”์ธ์— grpB๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. B ์š”์ธ์— ๋Œ€ํ•œ ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ๋ถ„์„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ†ต๊ณ„์  ํ•ด์„์€ ์ง‘๋‹จ๋ณ„ ํ‰๊ท  ์ฐจ์ด๊ฐ€ ํ‘œ์ค€ํŽธ์ฐจ๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์ง‘๋‹จ๋ณ„ ํ‰๊ท  ์ฐจ์ด๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ์˜์‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ถ„์‚ฐ์˜ ๋™์งˆ์„ฑ ๊ฒ€์ • ๊ฒฐ๊ณผ ์œ ์˜์ˆ˜์ค€์ด ๋ชจ๋“  ํ•ญ๋ชฉ์—์„œ 0.05๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ท€๋ฌด๊ฐ€์„ค 0 ฯƒ 2 ฯƒ 2 ฯƒ 2 ์„ ๊ธฐ๊ฐํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๊ฐ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์€ ๋ชจ๋‘ ๊ฐ™๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ ์ง‘๋‹จ์— ๋Œ€ํ•œ ๋“ฑ ๋ถ„์‚ฐ์„ ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์ง‘๋‹จ ํ‰๊ท ์ด ๋ชจ๋‘ ๊ฐ™์€์ง€ ๊ท€๋ฌด๊ฐ€์„ค 0 ฮผ = 2 ฮผ์— ๊ฒ€์ •์„ ANOVA๋กœ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ ์œ ์˜ ํ™•๋ฅ ์ด 0.000์œผ๋กœ ๋งค์šฐ ์œ ์˜ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๊ณ„์‚ฐ๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋ชจ๋“  ์ง‘๋‹จ์˜ ํ‰๊ท ์€ ๊ฐ™๋‹ค์— ๋Œ€ํ•œ ๊ฐ€์„ค์€ ๊ธฐ๊ฐํ•˜๊ณ  ์ ์–ด๋„ ํ•œ ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๊ฐ™์ง€ ์•Š๋‹ค๋Š” ๊ท€๋ฌด๊ฐ€์„ค์˜ ์—ญ์„ ์ฑ„ํƒํ•œ๋‹ค. ๊ฐ ์ง‘๋‹จ๋ณ„ ํ‰๊ท  ๋น„๊ต๋Š” 2 ๊ฐœ ์ง‘๋‹จ์”ฉํ•˜๊ณ  ๋น„๊ตํ•˜๋Š” ๊ฐœ์ˆ˜๋Š” ์ง‘๋‹จ ์ˆ˜๊ฐ€ ์ผ ๋•Œ ( 2 ) ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์ง‘๋‹จ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ์ด๋ฏ€๋กœ ์ด ๋น„๊ต ํšŸ์ˆ˜๋Š” ( 2 ) 3 ์ด๋‹ค. ๋‹ค์ค‘๋น„๊ต, ์‚ฌํ›„ ๊ฒ€์ • ๋ชจ๋“  ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๊ฐ™๋‹ค๋Š” ๊ท€๋ฌด๊ฐ€์„ค 0 ฮผ = 2 ฮผ ์ด ๊ธฐ๊ฐ๋˜์—ˆ์œผ๋ฏ€๋กœ ์–ด๋–ค ์ง‘๋‹จ ํ‰๊ท ์ด ๋‹ค๋ฅธ์ง€ ์‚ฌํ›„ ๊ฒ€์ •์„ ์‹ค์‹œํ•œ๋‹ค. ์‚ฌํ›„ ๊ฒ€์ •์€ ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์ฐฝ์—์„œ ์‚ฌํ›„๋ถ„์„ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค. ๋“ฑ ๋ถ„์‚ฐ์„ฑ ๊ฒ€์ • ๊ฒฐ๊ณผ ๋ชจ๋“  ์ง‘๋‹จ ๋ถ„์‚ฐ์ด ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋“ฑ ๋ถ„์‚ฐ์„ ๊ฐ€์ •ํ•จ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ„์„๋ฐฉ๋ฒ• 14๊ฐœ ์ค‘ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” Scheffe, Tukey, Duncan ๋ถ„์„๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ๋‘ ์ง‘๋‹จ ํ‰๊ท ์ด ๊ฐ™์€์ง€ ๊ฐ€์„ค ๊ฒ€์ •์€ ์œ ์˜์ˆ˜์ค€ 5%์—์„œ ์‹ค์‹œํ•˜์˜€๋‹ค. ๋‹ค์ค‘๋น„๊ต ๊ฒฐ๊ณผ ๋ถ„์„ ๋ฐฉ๋ฒ• ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ถ„์„์€ ๋‹ค์ค‘๋น„๊ต์—์„œ ์œ ์˜์ˆ˜์ค€ 0.05์—์„œ ์˜๋ฏธ ์žˆ๊ฒŒ ์ง‘๋‹จ ์ฐจ์ด๋ฅผ ๋ณด์ด๋Š” ์Œ๋“ค์€ ๋ณ„ํ‘œ(*)๋กœ ํ‘œ์‹œํ•˜์—ฌ ๊ฐ•์กฐํ•˜์˜€๋‹ค. ์ฆ‰ ์œ ์˜ ํ™•๋ฅ ์ด 0.05๋ณด๋‹ค ์ž‘์€ ๊ฒƒ๋“ค์€ ์˜๋ฏธ๊ฐ€ ์žˆ๊ณ  ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ทœ์€ ๊ฐ™๋‹ค๋Š” ๊ท€๋ฌด๊ฐ€์„ค 0 | i ฮผ | 0 i j ์— ๋Œ€ํ•œ ๊ฒ€์ • ๊ฒฐ๊ณผ ํ•ด์„์ด๋‹ค. ์ง‘๋‹จ B1๊ณผ ์ง‘๋‹จ B2 ํ‰๊ท  ์ฐจ์ด๋Š” -5.872๋กœ ์œ ์˜ ํ™•๋ฅ ์ด 0.019๋กœ ์œ ์˜ํ•˜๋‹ค. ์ง‘๋‹จ B1๊ณผ ์ง‘๋‹จ B3 ํ‰๊ท  ์ฐจ์ด๋Š” -12.244๋กœ ์œ ์˜ ํ™•๋ฅ ์ด 0.00๋กœ ๋งค์šฐ ์œ ์˜ํ•˜๋‹ค. ์ง‘๋‹จ B2๊ณผ ์ง‘๋‹จ B3 ํ‰๊ท  ์ฐจ์ด๋Š” -6.373๋กœ ์œ ์˜ ํ™•๋ฅ ์ด 0.06๋กœ ์œ ์˜ํ•˜๋‹ค. ํ‰๊ท  ์ฐจ์ด ๋น„๊ต ๋ฐฉ๋ฒ•์€ 6 ๊ฐ€์ง€์ด๋‚˜ ๋ถ€ํ˜ธ๋งŒ ๋‹ค๋ฅธ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ 2๋ฒˆ ์ œ์‹œํ–ˆ๊ธฐ์— 3๊ฐ€์ง€์ด๋‹ค. ์ฆ‰ 1 B ๋‚˜ 2 B์ด๋‚˜ ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๋‹ค. ๊ฐ ์ง‘๋‹จ์— ๋Œ€ํ•˜์—ฌ ํ‰๊ท ์ด ๊ฐ™์€ ์ง‘๋‹จ์„ ๋ฌถ๋Š” ๋™์งˆ์  ๋ถ€๋ถ„์ง‘ํ•ฉ์—์„œ B1, B2, B3 ์ง‘๋‹จ์€ ์„œ๋กœ ํ‰๊ท ์ด ๋‹ค๋ฅธ ์ง‘๋‹จ์ด๊ธฐ์— ๊ฐ๊ฐ ๋‹ค๋ฅธ 1, 2, 3 ์ง‘๋‹จ์— ํ‘œ์‹œ๋˜์—ˆ๋‹ค. 5. ๋…๋ฆฝ์ธ k, l ํ‘œ๋ณธ - ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„(two-way ANalysis Of VAriance) ์€ ์ผ๋ฐ˜ ์„ ํ˜•๋ชจํ˜• ๋ฉ”๋‰ด์—์„œ ๋ถ„์„ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„๊ณผ ๋ถ„์„ ๋ฐฉ๋ฒ• ์ฐจ์ด๊ฐ€ ๋งŽ๊ธฐ์— ์ž๋ฃŒ๋ถ„์„ ์ „์— ์ด๋ก ์„ ์†Œ๊ฐœํ•œ๋‹ค. ๋จผ์ € ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„์˜ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. A 2 j b ํ‰๊ท  ํ•ฉ๊ณ„ Y 111 Y 112 โ‹ฏ Y 11 Y 121 Y 122 โ‹ฏ Y 12 Y j, 1 2 โ‹ฏ Y j โ‹ฏ 1 1 Y b, , 1 ยฏ 1. โˆ‘ = b k 1 Y j 2 211 Y 212 โ‹ฏ Y 21 Y 221 Y 222 โ‹ฏ Y 22 Y j, 2 2 โ‹ฏ Y j โ‹ฏ 2 1 Y b, , 2 ยฏ 2. โˆ‘ = b k 1 Y j โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ Y 11 Y 12 โ‹ฏ Y 1 Y 21 Y 22 โ‹ฏ Y 2 โ‹ฏ i 1 Y j, , i n Y b, i 2 โ‹ฏ Y b ยฏ. โˆ‘ = b k 1 Y j โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ Y 11 Y 12 โ‹ฏ Y 1 Y 21 Y 22 โ‹ฏ Y 2 Y j, a 2 โ‹ฏ Y j โ‹ฏ a 1 Y b, , a ยฏ. โˆ‘ = b k 1 Y j ํ‰๊ท  ยฏ .1 Y .2 โ‹ฏ ยฏ j โ‹ฏ ยฏ b Y. . ํ•ฉ๊ณ„ i 1 โˆ‘ = n i k i 1 โˆ‘ = n i k โˆ‘ = a k 1 Y j โ‹ฏ i 1 โˆ‘ = n i k i 1 โˆ‘ = b k 1 Y j ์ด์› ๋ฐฐ์น˜ ์ž๋ฃŒ๊ตฌ์กฐ ์ƒํ˜ธ์ž‘์šฉ์ด ์žˆ๋Š” ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ๋ชจํ˜•์€ (1) i k ฮผ ฮฑ + j ฮณ j ฯต j , ฯต j โˆผ ( , 2 ) ์ด๋‹ค. ์‹ (1)์—์„œ๋Š” ์ƒ์ˆ˜ํ•ญ, ,๋Š” ์š”์ธ ๋ณ€์ˆ˜,๋Š” ๊ตํ˜ธ์ž‘์šฉ, ์€ ์˜ค์ฐจ์ด๋‹ค. ์‹ (1)์„ ์ž์„ธํžˆ ๋‚˜ํƒ€๋‚ด๋ฉด (2) i k ฮผ ( ยฏ. โˆ’ ยฏ. ) ( ยฏ j โˆ’ ยฏ. ) ( ยฏ j โˆ’ ยฏ. โˆ’ ยฏ j + ยฏ. ) ( i k Y i . ) ์ด๋‹ค. ์‹ (2)์—์„œ ์–‘๋ณ€์„ ์ œ๊ณฑํ•˜๊ณ  ํ•ฉ์„ ๊ตฌํ•˜๋ฉด ์ด ์ œ๊ณฑํ•ฉ ์ƒ์ˆ˜ (3) i j k i k โž ์ œ ํ•ฉ โˆ‘, , Y. . โž ์ˆ˜ โˆ‘, , ( ยฏ. โˆ’ ยฏ. ) โž S + i j k ( ยฏ j โˆ’ ยฏ. ) โž S + i j k ( ยฏ j โˆ’ ยฏ. โˆ’ ยฏ j + ยฏ. ) โž S B โˆ‘, , ( i k Y i . ) โž S์ด๋ฉฐ ์ „์ฒด ์ œ๊ณฑํ•ฉ์ด๋‹ค. SPSS ์ถœ๋ ฅ์—์„œ๋Š” ํ•ฉ๊ณ„์ด๋‹ค. ์‹(2)์—์„œ ์ „์ฒด ํ‰๊ท ์ธ ์ƒ์ˆ˜ํ•ญ ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ๋ณด๋‚ด๊ณ  ์–‘๋ณ€์˜ ์ œ๊ณฑํ•ฉ(sum of squares)์„ ๊ตฌํ•˜๋ฉด (4) i j k ( i k Y. . ) โž S = i j k ( ยฏ. โˆ’ ยฏ. ) โž S + i j k ( ยฏ j โˆ’ ยฏ. ) โž S + i j k ( ยฏ j โˆ’ ยฏ. โˆ’ ยฏ j + ยฏ. ) โž S B โˆ‘, , ( i k Y i . ) โž S์ด๋ฉฐ ๊ฐ ์ œ๊ณฑํ•ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. S (sum of squares total)๋Š” ์ˆ˜์ •๋œ ์ „์ฒด ์ œ๊ณฑํ•ฉ S (sum of squares factor A)๋Š” A ์š”์ธ ์ œ๊ณฑํ•ฉ S (sum of squares factor B)๋Š” B ์š”์ธ ์ œ๊ณฑํ•ฉ S B (sum of squares interaction of A B)๋Š” ์š”์ธ A์™€ B์˜ ์ƒํ˜ธ์ž‘์šฉ ์ œ๊ณฑํ•ฉ S (sum of squares error)๋Š” ์˜ค์ฐจ ์ œ๊ณฑํ•ฉ ๋‹ค์Œ์€ ๋‘ ์š”์ธ A, B์— ๋Œ€ํ•œ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค. ์š”์ธ ์ œ๊ณฑํ•ฉ ์ž์œ ๋„ ํ‰๊ท  ์ œ๊ณฑํ•ฉ F ๊ฐ’ S A โˆ’ M A S A ( โˆ’ ) S / S B S b 1 S = S / ( โˆ’ ) S / S A B S B ( โˆ’ ) ( โˆ’ ) S B S A / ( ( โˆ’ ) ( โˆ’ ) ) S B M E r o S E b ( โˆ’ ) S = S / ( b ( โˆ’ ) ) o a S T b โˆ’ ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ ์ƒํ˜ธ์ž‘์šฉ์ด ์—†๋Š” ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์ƒํ˜ธ์ž‘์šฉ์ด ์œ ์˜ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์—์„œ ์ƒํ˜ธ์ž‘์šฉ ์ œ๊ณฑํ•ฉ์„ ์˜ค์ฐจ ์ œ๊ณฑํ•ฉ์— ํ•ฉํ•œ๋‹ค. ์ด๊ฒƒ์„ ์ƒํ˜ธ์ž‘์šฉ์˜ ํ’€๋ง(pooling)์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ƒํ˜ธ์ž‘์šฉ์ด ์—†์„ ๋•Œ ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ๋ชจํ˜•์€ (5) i k ฮผ ฮฑ + j ฯต j , ฯต j โˆผ ( , 2 ) ์ด๋‹ค. ์‹ (5)์—์„œ๋Š” ์ƒ์ˆ˜ํ•ญ, ,๋Š” ์š”์ธ ๋ณ€์ˆ˜, ์€ ์˜ค์ฐจ์ด๋‹ค. ์‹ (5)๋ฅผ ์ž์„ธํžˆ ๋‚˜ํƒ€๋‚ด๋ฉด (6) i k ฮผ ( ยฏ. โˆ’ ยฏ. ) ( ยฏ j โˆ’ ยฏ. ) ( ยฏ j โˆ’ ยฏ. โˆ’ ยฏ j + ยฏ. ) ์ด๋‹ค. ์‹ (6)์˜ ์–‘๋ณ€์„ ์ œ๊ณฑํ•˜๊ณ  ํ•ฉ์„ ๊ตฌํ•˜๋ฉด ์ด ์ œ๊ณฑํ•ฉ ์ƒ์ˆ˜ (7) i j k i k โž ์ œ ํ•ฉ โˆ‘, , Y. . โž ์ˆ˜ โˆ‘, , ( ยฏ. โˆ’ ยฏ. ) โž S + i j k ( ยฏ j โˆ’ ยฏ. ) โž S + i j k ( ยฏ j โˆ’ ยฏ. โˆ’ ยฏ j + ยฏ. ) โž S์ด๋ฉฐ ์ „์ฒด ์ œ๊ณฑํ•ฉ์ด๋‹ค. ์ด ๊ฐ’์€ SPSS ์ถœ๋ ฅ์—์„œ ํ•ฉ๊ณ„์ด๋‹ค. ์‹ (6)์—์„œ ์ „์ฒด ํ‰๊ท ์ธ ์ƒ์ˆ˜ํ•ญ ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ๋ณด๋‚ด๊ณ  ์–‘๋ณ€์˜ ์ œ๊ณฑํ•ฉ(sum of squares)์„ ๊ตฌํ•˜๋ฉด (8) i j k ( i k Y. . ) โž S = i j k ( ยฏ. โˆ’ ยฏ. ) โž S + i j k ( ยฏ j โˆ’ ยฏ. ) โž S + i j k ( ยฏ j โˆ’ ยฏ. โˆ’ ยฏ j + ยฏ. ) โž S์ด๋ฉฐ ๊ฐ ์ œ๊ณฑํ•ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. S (sum of squares total)๋Š” ์ˆ˜์ •๋œ ์ „์ฒด ์ œ๊ณฑํ•ฉ S (sum of squares factor A)๋Š” A ์š”์ธ ์ œ๊ณฑํ•ฉ S (sum of squares factor B)๋Š” B ์š”์ธ ์ œ๊ณฑํ•ฉ S (sum of squares error)๋Š” ์˜ค์ฐจ ์ œ๊ณฑํ•ฉ ๋‹ค์Œ์€ ๋‘ ์š”์ธ A, B์— ๋Œ€ํ•œ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค. ์š”์ธ ์ œ๊ณฑํ•ฉ ์ž์œ ๋„ ํ‰๊ท  ์ œ๊ณฑํ•ฉ F ๊ฐ’ S A โˆ’ M A S A ( โˆ’ ) S / S B S b 1 S = S / ( โˆ’ ) S / S E r r S a n a b 1 S = S / ( b โˆ’ โˆ’ + ) o a S T b โˆ’ ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ(์ƒํ˜ธ์ž‘์šฉ ์ œ์™ธ) Type I, II, III sum of squares ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ œ๊ณฑํ•ฉ์„ ์‚ฌ์šฉํ•˜๋ฉฐ ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„์— ์‚ฌ์šฉํ•˜๋Š” ์ œ๊ณฑํ•ฉ๊ณผ ๋น„๊ตํ•ด ๋ณด์ž. ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„์€ ์ผ๋ฐ˜ ์„ ํ˜•๋ชจํ˜•์—์„œ Type I, II, III, III Sum of Squares 4 ๊ฐœ ์ œ๊ณฑํ•ฉ์œผ๋กœ ๋ถ„์„ํ•œ๋‹ค. ์ด ์ œ๊ณฑํ•ฉ์€ ์œ„๊ณ„์  ํšŒ๊ท€๋ถ„์„์„ ๋ฐ”๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„๊ณ„์  ํšŒ๊ท€๋ถ„์„์€ ์ผ๋ถ€ ๋ณ€์ˆ˜๋ฅผ ํ†ต์ œํ•˜๊ณ  ์–ด๋–ค ํ•œ ๋ณ€์ˆ˜๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ ์„ค๋ช…๋ ฅ์ด ์˜๋ฏธ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ๋ถ„์„๋ฐฉ๋ฒ•์ด๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ ๊ฐœ ํšŒ๊ท€์‹์„ ๊ตฌํ•˜์—ฌ ์œ„๊ณ„์  ํšŒ๊ท€๋ถ„์„์— ์ ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. Type I Type II Type III ANOVA S ( | ) S ( | , ) S ( | , , ) S ( | ) S ( | ) S ( | , ) S ( | , , ) S ( | ) ร— S ( | , , ) S ( | , , ) S ( | , , ) S ( , , | ) S ( | ) S ( | ) ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ œ๊ณฑํ•ฉ ์‚ฌ์šฉํ•œ ์ œ๊ณฑํ•ฉ์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‹ค. S ( , , , ) S ( ) S ( | ) S ( | , ) S ( | , , ) S ( , , B ) โˆ‘, , Y j 2 : ์ „์ฒด ๋ชจํ˜•์—์„œ ์ด ์ œ๊ณฑํ•ฉ์ด๋‹ค. SPSS ์ถœ๋ ฅ์—์„œ ์ „์ฒด์ด๋‹ค. S ( , , | ) S T โˆ‘, , ( i k Y. . ) : ์ˆ˜์ • ์ œ๊ณฑํ•ฉ์œผ๋กœ ์ „์ฒด ๋ชจํ˜• ์ œ๊ณฑํ•ฉ์—์„œ ์ ˆํŽธ์„ ๋บ€ ๊ฐ’์ด๋‹ค. SPSS ์ถœ๋ ฅ์—์„œ ์ˆ˜์ •๋œ ํ•ฉ๊ณ„์ด๋‹ค. S ( | ) S ( ) S A โˆ‘, , ( ยฏ. โˆ’ ยฏ. ) : ๊ฐ€ ์žˆ๋Š” ๋ชจํ˜•์—์„œ ๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ ์ œ๊ณฑ ํ•ฉ์˜ ์ฆ๊ฐ€๋Ÿ‰ S ( | ) S ( ) S B โˆ‘, , ( ยฏ j โˆ’ ยฏ. ) : ๊ฐ€ ์žˆ๋Š” ๋ชจํ˜•์—์„œ ๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ ์ œ๊ณฑ ํ•ฉ์˜ ์ฆ๊ฐ€๋Ÿ‰ S ( | , ) S ( | ) S ( , ) S ( ) ฮผ ฮฑ ๊ฐ€ ์žˆ๋Š” ๋ชจํ˜•์—์„œ ๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ ์ œ๊ณฑ ํ•ฉ์˜ ์ฆ๊ฐ€๋Ÿ‰ S ( | , , ) S ( B A B ) S ( , , B ) S ( , ) ฮผ ฮฑ ฮฒ ๊ฐ€ ์žˆ๋Š” ๋ชจํ˜•์—์„œ ๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ ์ œ๊ณฑ ํ•ฉ์˜ ์ฆ๊ฐ€๋Ÿ‰ Type I SS Type I ์ œ๊ณฑํ•ฉ์€ ์ˆœ์ฐจ ์ œ๊ณฑํ•ฉ(sequential sum of squares)์œผ๋กœ๋„ ๋ถ€๋ฅด๊ณ  ์†์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ถ•์†Œ๋ชจํ˜•์— ์ฃผ์ธ์ž A, B์™€ ๊ตํ˜ธ์ž‘์šฉ AB๊ฐ€ ์ฐจ๋ก€๋กœ ์ถ”๊ฐ€๋  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ฆ๊ฐ€๋Ÿ‰์œผ๋กœ ์ˆœ์ฐจ ์ œ๊ณฑํ•ฉ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ธ์ž๊ฐ€ ๋ชจํ˜•์— ์ถ”๊ฐ€๋˜๋Š” ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜๋ฉฐ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๊ฐ ์ธ์ž์˜ ์ œ๊ณฑํ•ฉ์ด ๋ณ€ํ•œ๋‹ค. ๊ฐ ์ œ๊ณฑํ•ฉ์€ ๋ชจ๋‘ ํ•ฉํ•˜๋ฉด ์ „์ฒด ์ œ๊ณฑํ•ฉ S ( , , , ) Type II SS Type II ์ œ๊ณฑํ•ฉ์€ ๋ชจ๋“  ์ฃผ์ธ์ž๊ฐ€ ํฌํ•จ๋œ ๋ชจํ˜•์—์„œ ํ•œ ๊ฐœ์˜ ์ธ์ž๊ฐ€ ์ œ๊ฑฐ๋  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ œ๊ณฑ ํ•ฉ์˜ ๊ฐ์†Œ๋Ÿ‰์ด๋‹ค. ์ธ์ž๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ์ˆœ์„œ์— ์ œ๊ณฑํ•ฉ์ด ์˜ํ–ฅ๋ฐ›์ง€ ์•Š๋Š”๋‹ค. ๊ฐ ์ œ๊ณฑํ•ฉ์€ ๋ชจ๋‘ ํ•ฉํ•˜๋ฉด ์ „์ฒด ์ œ๊ณฑํ•ฉ S ( , , , ) ์™€ ๊ฐ™์ง€ ์•Š๋‹ค. Type III SS Type III ์ œ๊ณฑํ•ฉ์€ ๋ชจ๋“  ์ฃผ์ธ์ž์™€ ๊ตํ˜ธ์ž‘์šฉ์ด ํฌํ•จ๋œ ์™„์ „ ๋ชจํ˜•์—์„œ ํ•œ ๊ฐœ์˜ ์ธ์ž๊ฐ€ ์ œ๊ฑฐ๋  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ œ๊ณฑ ํ•ฉ์˜ ๊ฐ์†Œ๋Ÿ‰์ด๋‹ค. ์ธ์ž๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ์ˆœ์„œ์— ์ œ๊ณฑํ•ฉ์ด ์˜ํ–ฅ๋ฐ›์ง€ ์•Š๋Š”๋‹ค. ๊ฐ ์ œ๊ณฑํ•ฉ์€ ๋ชจ๋‘ ํ•ฉํ•˜๋ฉด ์ „์ฒด ์ œ๊ณฑํ•ฉ S ( , , , ) ์™€ ๊ฐ™์ง€ ์•Š๋‹ค. ๋ถ€๋ถ„ ์ œ๊ณฑํ•ฉ(partial sum of squares)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์‹ค์Šต ์ž๋ฃŒ ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„์— ์‚ฌ์šฉํ•  ์ž๋ฃŒ๋Š” ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„์—์„œ ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋ฅผ ํ•œ ์š”์ธ์œผ๋กœ ๋” ์„ธ๋ถ„ํ•œ ๊ฒƒ์ด๋‹ค. ์š”์ธ 1 2 3 ์š”์ธ 1 41 43 50 51 43 53 54 46 45 55 56 60 58 62 62 2 56 47 45 46 49 58 54 49 61 52 62 59 55 68 63 3 43 56 48 46 47 59 46 58 54 55 69 63 56 62 67 Maxwell & Delaney 2004 p339 SPSS ๋ถ„์„ ๊ณผ์ • ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„ ์‹ค์Šต์ž๋ฃŒ๋Š” ์—ฌ๊ธฐ์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. SPSS ๋ฐ์ดํ„ฐ ์‹œํŠธ์—์„œ 2์—ด, 3์—ด์— ์žˆ๋Š” grpA, grpB ๋ณ€์ˆ˜๋Š” ๋ถ„์‚ฐ๋ถ„์„์—์„œ ์‚ฌ์šฉํ•  ๋…๋ฆฝ๋ณ€์ˆ˜์ด๊ณ  4์—ด ~ 11์—ด์€ ํšŒ๊ท€๋ถ„์„์—์„œ ์‚ฌ์šฉํ•  ๋…๋ฆฝ๋ณ€์ˆ˜๋กœ ๋”๋ฏธ ๋ณ€์ˆ˜์ด๋‹ค. ์ผ๋ฐ˜ ์„ ํ˜•๋ชจํ˜• ๋ถ„์„ ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„์€ ๋ถ„์„ -> ์ผ๋ฐ˜ ์„ ํ˜•๋ชจํ˜• -> ์ผ๋ณ€๋Ÿ‰ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. 2๊ฐœ ์š”์ธ๊ณผ ์ข…์†๋ณ€์ˆ˜ ์„ค์ • ์ข…์†๋ณ€์ˆ˜์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ณ ์ • ์š”์ธ์— grpA, grpB ์ˆœ์„œ๋กœ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. Type I SS (๋ณ€์ˆ˜ ์ถ”๊ฐ€ ์ˆœ์„œ๊ฐ€ grpA, grpB์ธ ๊ฒฝ์šฐ) ๋ชจํ˜• ์„ค์ •์—์„œ ์™„์ „ ์š”์ธ ๋ชจํ˜•์€ ๋ชจ๋“  ์š”์ธ๊ณผ ๊ตํ˜ธ์ž‘์šฉ์ด ์ถ”๊ฐ€๋œ๋‹ค. ํ•ญ ์„ค์ •์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ถ”๊ฐ€ํ•  ์š”์ธ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ์ž ์ •์˜ ํ•ญ ์„ค์ •์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์š”์ธ ์ค‘์—์„œ ํŠน์ • ์š”์ธ์„ ๋ชจํ˜•์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ œ๊ณฑํ•ฉ์€ ์ œ I ์œ ํ˜•์„ ์„ ํƒํ•œ๋‹ค. ๋ชจํ˜•์„ ์ ˆํŽธ์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ๋ถ„์„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ†ต๊ณ„๋Ÿ‰์— ๋Œ€ํ•œ ๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋‹ค์Œ ์‚ฌ์šฉํ•œ ์ˆซ์ž ๊ธฐํ˜ธ โ‘ , โ‘ก, โ‘ข, โ‘ฃ, โ‘ค, โ‘ฅ, โ‘ฆ, โ‘ง์€ SPSS ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ†ต๊ณ„๋Ÿ‰์— ํ‘œ์‹œํ•œ ๊ฒƒ์ด๋‹ค. โ‘  ์ „์ œ S ( , , , B ) = โ‘ก + โ‘ข โ‘ก ์ˆ˜์ •๋œ ํ•ฉ๊ณ„ S ( , , B ฮฑ ) = โ‘ฃ + โ‘ค + โ‘ฅ + โ‘ฆ = โ‘ฆ + โ‘ง โ‘ข ์ ˆํŽธ ์ œ๊ณฑํ•ฉ S ( ) S ( | ) S ( ) 101.111 , ํ‘œ 1์—์„œ S ( ) 101.111 S ( | , ) S ( , ) S ( ) 1253.189 , ํ‘œ 3 ํšŒ๊ท€ ์ œ๊ณฑํ•ฉ S ( , ) ์—์„œ ํ‘œ 1 S ( ) ์„ ๋บ€ S ( , ) S R ( ) 1354.300 101.111 1253.189 S ( B ฮฑ A B ) 14.184 , ํ‘œ 6 S ( , , B ) ์—์„œ ํ‘œ 3 S ( , ) ๋ฅผ ๋บ€ S ( , , B ) S R ( , ) 1368.487 1354.300 14.187 ์ด๋‹ค. โ‘ฆ ์˜ค์ฐจ ์ œ๊ณฑํ•ฉ; SSE = โ‘ก - โ‘ง โ‘ง ์ˆ˜์ •๋œ ๋ชจํ˜•; S ( , , B ฮฑ ) S E = โ‘ฃ + โ‘ค + โ‘ฅ = โ‘ก - โ‘ฆ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ถ„์„์€ ๊ท€๋ฌด๊ฐ€์„ค 0 ฮฑ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.178๋กœ ์œ ์˜ํ•˜์ง€ ์•Š๋‹ค. grpA ๋ณ€์ˆ˜์—์„œ ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๋Š” ์—†๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ฮฒ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.000์œผ๋กœ ๋งค์šฐ ์œ ์˜ํ•˜๋‹ค. grpB ๋ณ€์ˆ˜์—์„œ ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๋Š” ์ ์–ด๋„ ํ•œ ์Œ ์ด์ƒ ์กด์žฌํ•œ๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ฮณ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.972๋กœ ์œ ์˜ํ•˜์ง€ ์•Š๋‹ค. ๋‘ ๋ณ€์ˆ˜ grpA, grpB์˜ ๊ตํ˜ธ์ž‘์šฉ์€ ์—†๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ Type I SS ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋กœ ๋ณ€์ˆ˜ ์ถ”๊ฐ€ ์ˆœ์„œ๊ฐ€ grpA, grpB์ด๋‹ค. Type I SS (๋ณ€์ˆ˜ ์ถ”๊ฐ€ ์ˆœ์„œ๊ฐ€ grpB, grpA์ธ ๊ฒฝ์šฐ) ์ข…์†๋ณ€์ˆ˜์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ณ ์ • ์š”์ธ์— grpB, grpA ์ˆœ์„œ๋กœ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ Type I SS ์„ค์ •์—์„œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€ ์ˆœ์„œ๊ฐ€ grpB, grpA์ด๋‹ค. ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ๋ถ„์„ ๋ชจํ˜• ์„ค์ •์—์„œ ์™„์ „ ์š”์ธ ๋ชจํ˜•์€ ๋ชจ๋“  ์š”์ธ๊ณผ ๊ตํ˜ธ์ž‘์šฉ์ด ์ถ”๊ฐ€๋œ๋‹ค. ํ•ญ ์„ค์ •์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ถ”๊ฐ€ํ•  ์š”์ธ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ์ž ์ •์˜ ํ•ญ ์„ค์ •์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์š”์ธ ์ค‘์—์„œ ํŠน์ • ์š”์ธ์„ ๋ชจํ˜•์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ œ๊ณฑํ•ฉ์€ ์ œ I ์œ ํ˜•์„ ์„ ํƒํ•œ๋‹ค. ๋ชจํ˜•์„ ์ ˆํŽธ์— ์ถ”๊ฐ€ํ•œ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ Type I SS ์„ค์ •์—์„œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€ ์ˆœ์„œ๊ฐ€ grpB, grpA์ด๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ†ต๊ณ„๋Ÿ‰์— ๋Œ€ํ•œ ๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋‹ค์Œ ์‚ฌ์šฉํ•œ ์ˆซ์ž ๊ธฐํ˜ธ โ‘ , โ‘ก, โ‘ข, โ‘ฃ, โ‘ค, โ‘ฅ, โ‘ฆ, โ‘ง์€ SPSS ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ†ต๊ณ„๋Ÿ‰์— ํ‘œ์‹œํ•œ ๊ฒƒ์ด๋‹ค. โ‘  ์ „์ œ S ( , , , B ) = โ‘ก + โ‘ข โ‘ก ์ˆ˜์ •๋œ ํ•ฉ๊ณ„ S ( , , B ฮฑ ) = โ‘ฃ + โ‘ค + โ‘ฅ + โ‘ฆ = โ‘ฆ + โ‘ง โ‘ข ์ ˆํŽธ ์ œ๊ณฑํ•ฉ S ( ) S ( | ) 1115.818 ํ‘œ 2์—์„œ S ( ) 1115.818 S ( | , ) S ( , ) S ( ) 238.483 ์€ ํ‘œ 3์˜ ํšŒ๊ท€ ์ œ๊ณฑํ•ฉ S ( , ) ์—์„œ ํ‘œ 2์˜ S ( ) ๋ฅผ ๋บ€ S ( , ) S R ( ) 1354.300 1115.818 238.483 ์ด๋‹ค. S ( B ฮฑ A B ) S ( B A B ) 14.187 ์€ ํ‘œ 6 S ( , , B ) ์—์„œ ํ‘œ 3 S ( , ) ๋ฅผ ๋บ€ S ( , , B ) S R ( , ) 1368.487 1354.300 14.187 ์ด๋‹ค. โ‘ฆ ์˜ค์ฐจ ์ œ๊ณฑํ•ฉ; SSE = โ‘ก - โ‘ง โ‘ง ์ˆ˜์ •๋œ ๋ชจํ˜•; S ( , , B ฮฑ ) S E = โ‘ฃ + โ‘ค + โ‘ฅ = โ‘ก - โ‘ฆ Type I SS๋Š” ๋ณ€์ˆ˜ ์ถ”๊ฐ€ ์ˆœ์„œ์— ๋”ฐ๋ผ ๊ฐ’์ด ๋‹ค๋ฅธ ๊ฒƒ์„ ๋‘ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ  ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ถ„์„์€ ๊ท€๋ฌด๊ฐ€์„ค 0 ฮฒ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.000์œผ๋กœ ๋งค์šฐ ์œ ์˜ํ•˜๋‹ค. grpB ๋ณ€์ˆ˜์—์„œ ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๋Š” ์ ์–ด๋„ ํ•œ ์Œ ์ด์ƒ ์กด์žฌํ•œ๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ฮฑ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.022๋กœ ์œ ์˜ํ•˜๋‹ค. grpA ๋ณ€์ˆ˜์—์„œ ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๋Š” ์ ์–ด๋„ ํ•œ ์Œ ์ด์ƒ ์กด์žฌ๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ฮณ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.972๋กœ ์œ ์˜ํ•˜์ง€ ์•Š๋‹ค. ๋‘ ๋ณ€์ˆ˜ grpA, grpB์˜ ๊ตํ˜ธ์ž‘์šฉ์€ ์—†๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ Type I SS ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€ ์ˆœ์„œ๊ฐ€ grpB, grpA์ด๋‹ค. Type II SS ๋ชจํ˜• ์„ค์ •์—์„œ ์™„์ „ ์š”์ธ ๋ชจํ˜•์€ ๋ชจ๋“  ์š”์ธ๊ณผ ๊ตํ˜ธ์ž‘์šฉ์ด ์ถ”๊ฐ€๋œ๋‹ค. ํ•ญ ์„ค์ •์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ถ”๊ฐ€ํ•  ์š”์ธ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ์ž ์ •์˜ ํ•ญ ์„ค์ •์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์š”์ธ ์ค‘์—์„œ ํŠน์ • ์š”์ธ์„ ๋ชจํ˜•์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ œ๊ณฑํ•ฉ์€ ์ œ II ์œ ํ˜•์„ ์„ ํƒํ•œ๋‹ค. ๋ชจํ˜•์„ ์ ˆํŽธ์— ์ถ”๊ฐ€ํ•œ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ Type II SS ์„ค์ •์—์„œ ์ƒํ˜ธ์ž‘์šฉ์„ ์ˆ˜๋™์œผ๋กœ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์ด๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ†ต๊ณ„๋Ÿ‰์— ๋Œ€ํ•œ ๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋‹ค์Œ ์‚ฌ์šฉํ•œ ์ˆซ์ž ๊ธฐํ˜ธ โ‘ , โ‘ก, โ‘ข, โ‘ฃ, โ‘ค, โ‘ฅ, โ‘ฆ, โ‘ง์€ SPSS ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ†ต๊ณ„๋Ÿ‰์— ํ‘œ์‹œํ•œ ๊ฒƒ์ด๋‹ค. โ‘  ์ „์ œ S ( , , , B ) = โ‘ก + โ‘ข โ‘ก ์ˆ˜์ •๋œ ํ•ฉ๊ณ„ S ( , , B ฮฑ ) = โ‘ฆ + โ‘ง โ‘ข ์ ˆํŽธ ์ œ๊ณฑํ•ฉ S ( ) S ( | , ) S ( , ) S ( ) 238.483 ์€ ํ‘œ 3 ํšŒ๊ท€ ์ œ๊ณฑํ•ฉ S ( , ) ์—์„œ ํ‘œ 2 S ( ) ๋ฅผ ๋บ€ S ( , ) S R ( ) 1354.300 1115.818 238.483 S ( | , ) S ( , ) S ( ) 1253.189 ๋Š” ํ‘œ 3 ํšŒ๊ท€ ์ œ๊ณฑํ•ฉ S ( , ) ์—์„œ ํ‘œ 1 S ( ) ๋ฅผ ๋บ€ S ( , ) S R ( ) 1354.300 101.111 1253.189 S ( B ฮฑ A B ) S ( B A B ) 14.187 ์€ ํ‘œ 6 S ( , , B ) ์—์„œ ํ‘œ 3 S ( , ) ๋ฅผ ๋บ€ S ( , , B ) S R ( , ) 1368.487 1354.300 14.187 ์ด๋‹ค. โ‘ฆ ์˜ค์ฐจ ์ œ๊ณฑํ•ฉ; SSE = โ‘ก - โ‘ง โ‘ง ์ˆ˜์ •๋œ ๋ชจํ˜•; S ( , , B ฮฑ ) S E = โ‘ก - โ‘ฆ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ถ„์„์€ ๊ท€๋ฌด๊ฐ€์„ค 0 ฮฑ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.022๋กœ ์œ ์˜ํ•˜๋‹ค. grpA ๋ณ€์ˆ˜์—์„œ ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๋Š” ์ ์–ด๋„ ํ•œ ์Œ ์ด์ƒ ์กด์žฌํ•œ๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ฮฒ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.000์œผ๋กœ ๋งค์šฐ ์œ ์˜ํ•˜๋‹ค. grpB ๋ณ€์ˆ˜์—์„œ ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๋Š” ์ ์–ด๋„ ํ•œ ์Œ ์ด์ƒ ์กด์žฌํ•œ๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ฮณ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.972๋กœ ์œ ์˜ํ•˜์ง€ ์•Š๋‹ค. ๋‘ ๋ณ€์ˆ˜ grpA, grpB์˜ ๊ตํ˜ธ์ž‘์šฉ์€ ์—†๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ Type II SS ์ถœ๋ ฅ ๊ฒฐ๊ณผ์ด๋‹ค. Type III SS ๋ชจํ˜• ์„ค์ •์—์„œ ์™„์ „ ์š”์ธ ๋ชจํ˜•์€ ๋ชจ๋“  ์š”์ธ๊ณผ ๊ตํ˜ธ์ž‘์šฉ์ด ์ถ”๊ฐ€๋œ๋‹ค. ํ•ญ ์„ค์ •์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ถ”๊ฐ€ํ•  ์š”์ธ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ์ž ์ •์˜ ํ•ญ ์„ค์ •์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์š”์ธ ์ค‘์—์„œ ํŠน์ • ์š”์ธ์„ ๋ชจํ˜•์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ œ๊ณฑํ•ฉ์€ ์ œ III ์œ ํ˜•์„ ์„ ํƒํ•œ๋‹ค. ๋ชจํ˜•์„ ์ ˆํŽธ์— ์ถ”๊ฐ€ํ•œ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ Type III SS ์„ค์ •์—์„œ ์ƒํ˜ธ์ž‘์šฉ์„ ์ˆ˜๋™์œผ๋กœ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ ํ†ต๊ณ„๋Ÿ‰์— ๋Œ€ํ•œ ๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. โ‘  ์ „์ œ S ( , , , B ) = โ‘ก + โ‘ข โ‘ก ์ˆ˜์ •๋œ ํ•ฉ๊ณ„ S ( , , B ฮฑ ) = โ‘ฆ + โ‘ง โ‘ข ์ ˆํŽธ ์ œ๊ณฑํ•ฉ S ( ) S ( | , , B ) S ( | , B ) 204.762 ๋Š” ํ‘œ 6 S ( , , B ) ์—์„œ ํ‘œ 5 S ( , B ) ๋ฅผ ๋บ€ S ( , , B ) S R ( , B ) 1368.487 1163.728 204.762 ์ด๋‹ค. S ( | , , B ) S ( | , B ) 1181.105 ๋Š” ํ‘œ 6 S ( , , B ) ์—์„œ ํ‘œ 4 S ( , B ) ๋ฅผ ๋บ€ S ( , , B ) S R ( , B ) 1368.487 187.382 1181.105 ์ด๋‹ค. S ( B ฮฑ A B ) S ( B A B ) 14.187 ์€ ํ‘œ 6 S ( , , B ) ์—์„œ ํ‘œ 3 S ( , ) ๋ฅผ ๋บ€ S ( , , B ) S R ( , ) 1368.487 1354.300 14.187 ์ด๋‹ค. โ‘ฆ ์˜ค์ฐจ ์ œ๊ณฑํ•ฉ; SSE = โ‘ก - โ‘ง โ‘ง ์ˆ˜์ •๋œ ๋ชจํ˜•; S ( , , B ฮฑ ) S E = โ‘ก - โ‘ฆ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ถ„์„์€ ๊ท€๋ฌด๊ฐ€์„ค 0 ฮฑ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.036์œผ๋กœ ์œ ์˜ํ•˜๋‹ค. grpA ๋ณ€์ˆ˜์—์„œ ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๋Š” ์ ์–ด๋„ ํ•œ ์Œ ์ด์ƒ ์กด์žฌํ•œ๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ฮฒ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.000์œผ๋กœ ๋งค์šฐ ์œ ์˜ํ•˜๋‹ค. grpB ๋ณ€์ˆ˜์—์„œ ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๋Š” ์ ์–ด๋„ ํ•œ ์Œ ์ด์ƒ ์กด์žฌํ•œ๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ฮณ 0 ์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.972๋กœ ์œ ์˜ํ•˜์ง€ ์•Š๋‹ค. ๋‘ ๋ณ€์ˆ˜ grpA, grpB์˜ ๊ตํ˜ธ์ž‘์šฉ์€ ์—†๋‹ค๊ณ  ํ†ต๊ณ„์ ์œผ๋กœ ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ Type III SS ์ถœ๋ ฅ ๊ฒฐ๊ณผ์ด๋‹ค. ํšŒ๊ท€์‹์— ๋Œ€ํ•œ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ ๋‹ค์Œ์€ Type I SS, Type II SS, Type III SS๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ํšŒ๊ท€์‹์— ๋Œ€ํ•œ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ๋ฅผ ๊ตฌํ•˜์˜€๋‹ค. ํšŒ๊ท€์‹์— ๋Œ€ํ•œ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ๋Š” ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ์—ด๊ณ  SPSS ๋ช…๋ น์–ด ํŒŒ์ผ์—์„œ REGRESSION PROCEDURE ๋ถ€๋ถ„์„ ์„ ํƒํ•˜์—ฌ ์‹คํ–‰ํ•˜๋ฉด ์ถœ๋ ฅ์„ ๋ฐ”๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ์ผ๋ฐ˜ ์„ ํ˜•๋ชจํ˜•์„ ๊ตฌํ•˜๋Š” ๋ช…๋ น์–ด๋„ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ํšŒ๊ท€ ์ถ”์ •์‹ ^ ฮฒ 0 ฮฒ 1 1 ฮฒ 2 2 ์˜ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค(ํ‘œ 1). ํšŒ๊ท€ ์ถ”์ •์‹ ^ ฮฒ 0 ฮฒ 1 1 ฮฒ 2 2 ์˜ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค(ํ‘œ 2). ํšŒ๊ท€ ์ถ”์ •์‹ ^ ฮฒ 0 ฮฒ 1 1 ฮฒ 2 2 ฮฒ 3 1 ฮฒ 4 2 ์˜ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค(ํ‘œ 3). ํšŒ๊ท€ ์ถ”์ •์‹ ^ ฮฒ 0 ฮฒ 1 1 ฮฒ 2 2 ฮฒ 3 1 1 ฮฒ 4 1 2 ฮฒ 5 2 1 ฮฒ 6 2 2 ์˜ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค(ํ‘œ 4). ํšŒ๊ท€ ์ถ”์ •์‹ ^ ฮฒ 0 ฮฒ 1 1 ฮฒ 2 2 ฮฒ 3 1 1 ฮฒ 4 1 2 ฮฒ 5 2 1 ฮฒ 6 2 2 ์˜ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค(ํ‘œ 5). ํšŒ๊ท€ ์ถ”์ •์‹ ^ ฮฒ 0 ฮฒ 1 1 ฮฒ 2 2 ฮฒ 3 1 ฮฒ 4 2 ฮฒ 5 1 1 ฮฒ 6 1 2 ฮฒ 7 2 1 ฮฒ 8 2 2 ์˜ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค(ํ‘œ 6). 06. ๋น„์œจ ๋น„๊ต ๋น„์œจ ๋น„๊ต ๊ฒ€์ • ๋‹จ์ผ ํ‘œ๋ณธ ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ 1. ๋‹จ์ผ ํ‘œ๋ณธ ๋‹จ์ผ ํ‘œ๋ณธ ๋น„์œจ ์ถ”๋ก  ์‚ฌ๋ก€ 1 : ๋Œ€ํ†ต๋ น ์„ ๊ฑฐ์—์„œ A ํ›„๋ณด์ž ์ง€์ง€์œจ์ด ๊ถ๊ธˆํ•˜๊ณ , ๊ทธ ๊ฐ’์— ๋Œ€ํ•œ ์˜ค์ฐจ ๋ฒ”์œ„๊ฐ€ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค. ์‚ฌ๋ก€ 2 : ์–ด๋–ค ํŠน์ •ํ•œ ์•”์˜ ๊ฒฝ์šฐ์— ์ˆ˜์ˆ ์„ ์‹œํ–‰ํ•œ ํ›„ ์™„์น˜๋˜๋Š” ๋น„์œจ์ด 30%๋กœ ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ์ด ์•”์— ๊ฑธ๋ฆฐ 60๋ช…์˜ ํ™˜์ž๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ˆ˜์ˆ ๋ฟ ์•„๋‹ˆ๋ผ ์ˆ˜์ˆ  ์ „ํ›„์— ์ผ์ • ๊ธฐ๊ฐ„ ๋ฐฉ์‚ฌ์„  ์น˜๋ฃŒ๋ฅผ ๋ณ‘ํ–‰ํ•˜์˜€๋”๋‹ˆ 60๋ช… ์ค‘ 27๋ช…์ด ์™„์น˜๋˜์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค. ์ด ์ž๋ฃŒ๋กœ๋ถ€ํ„ฐ ์ˆ˜์ˆ ๋งŒ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ฐฉ์‚ฌ์„ ์น˜๋ฃŒ๋ฅผ ๋ณ‘ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์•”์˜ ์™„์น˜์œจ(p)์„ ๋†’์ด๋Š”๋ฐ ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„ํ•˜์ž. ์™„์น˜ ์œ ๋ฌด ํ™˜์ž ์ˆ˜ ์™„์น˜ 27 ์น˜๋ฃŒ ์ค‘ 33 ํ•ฉ๊ณ„ 60 ๋‹จ์ผ ํ‘œ๋ณธ ๋น„์œจ ์ถ”๋ก  ์ด๋ก ์  ๋‚ด์šฉ ๋‹จ์ผ ํ‘œ๋ณธ ๋น„์œจ์— ๋Œ€ํ•œ 100 ( โˆ’ ) ์‹ ๋ขฐ๊ตฌ๊ฐ„ ๋˜๋Š” ( ^ z / p ( โˆ’ ^ ) , p + ฮฑ 2 ^ ( โˆ’ ^ ) ) ๋˜๋Š” ^ z / p ( โˆ’ ^ ) ์—ฌ๊ธฐ์„œ ^ X ์€ ํ‘œ๋ณธ ๋น„์œจ,๋Š” ์„ฑ๊ณต ํšŸ์ˆ˜, ฮฑ 2 ๋Š” ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ๋ถ„์œ„์ˆ˜, ์€ ์ž๋ฃŒ์˜ ๊ฐœ์ˆ˜ ๋Œ€ํ†ต๋ น ์„ ๊ฑฐ์—์„œ A ํ›„๋ณด ๋“ํ‘œ์œจ์€ ^ X๋กœ ์˜ˆ์ธกํ•˜๊ณ , ์˜ค์ฐจ ๋ฒ”์œ„๋Š” ฮฑ 2 ^ ( โˆ’ ^ )์ด๋‹ค. ๋‹จ์ผ ํ‘œ๋ณธ ๋น„์œจ์— ๋Œ€ํ•œ ๊ฒ€์ • โ‘  ๊ท€๋ฌด๊ฐ€์„ค 0 p p (์˜๋ฏธ : ์–ด๋Š ์ง‘๋‹จ์˜ ์„ฑ๊ณต ๋น„์œจ์€ 0 ์ด๋‹ค.)์ด๋ฉฐ, ์˜ˆ์ „์— ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ ์•Œ๋ ค์ง„ ํ‰๊ท ์ด๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค์€ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์—ฐ๊ตฌ์ž๊ฐ€ ์ •ํ•œ๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค 1 p p (์˜๋ฏธ : ์–ด๋Š ์ง‘๋‹จ์˜ ์„ฑ๊ณต ๋น„์œจ์€ 0 ๋ณด๋‹ค ํฌ๋‹ค.) ๋‹จ ์ธก ๊ฒ€์ • ๋Œ€๋ฆฝ๊ฐ€์„ค 1 p p (์˜๋ฏธ : ์–ด๋Š ์ง‘๋‹จ์˜ ์„ฑ๊ณต ๋น„์œจ์€ 0 ๋ณด๋‹ค ์ž‘๋‹ค.) ๋‹จ ์ธก ๊ฒ€์ • ๋Œ€๋ฆฝ๊ฐ€์„ค 1 p p (์˜๋ฏธ : ์–ด๋Š ์ง‘๋‹จ์˜ ์„ฑ๊ณต ๋น„์œจ์€ 0 ์ด ์•„๋‹ˆ๋‹ค. ์ฆ‰ 0 ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ 0 ๋ณด๋‹ค ์ž‘๋‹ค) ์–‘์ธก๊ฒ€์ • โ‘ก ์—ฐ๊ตฌ์ž๋Š” ์œ ์˜์ˆ˜์ค€์— ๋Œ€ํ•œ ๊ธฐ๊ฐ์—ญ์„ ๊ตฌํ•˜๊ณ  ์กฐ์‚ฌํ•œ ์ž๋ฃŒ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ = ^ p p ( โˆ’ 0 ) ๊ณผ ์œ ์˜ ํ™•๋ฅ (p-value)์„ ๊ตฌํ•œ๋‹ค. โ‘ข ์—ฐ๊ตฌ์ž๋Š” ์œ ์˜ ํ™•๋ฅ ๊ณผ ์œ ์˜์ˆ˜์ค€์˜ ๋Œ€์†Œ๋‚˜ ๊ธฐ๊ฐ์—ญ์— ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ํฌํ•จ๋˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์˜ ๊ธฐ๊ฐ, ์ฑ„ํƒ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ•ด์„ํ•œ๋‹ค. SPSS ๋ถ„์„ ๊ณผ์ • ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ํ‘œ์— ์žˆ๋Š” ์ž๋ฃŒ๋Š” ์›์ž๋ฃŒ๋ฅผ ์š”์•ฝํ•œ ๊ฒƒ์œผ๋กœ ์ง์ ‘ SPSS์— ์ž…๋ ฅํ•˜์ž. ์ž๋ฃŒ ์ž…๋ ฅ์€ ํŒŒ์ผ -> ์ƒˆ ํŒŒ์ผ -> ๋ฐ์ดํ„ฐ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆซ๊ฐ’์— ๋ณ€์ˆ˜ ์ด๋ฆ„, ์†Œ์ˆ˜์ , ๊ฐ’ ์„ค์ • ์ƒˆ ํŒŒ์ผ์„ ๋งŒ๋“ค๋ฉด ๋‘ ๊ฐœ ์‹œํŠธ๊ฐ€ ๋ณด์ธ๋‹ค. ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ๋Š” ์ž๋ฃŒ๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ๋Š” ์ž…๋ ฅํ•œ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•œ๋‹ค. ์ž๋ฃŒ์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋Š” ์—ด์— ์ž…๋ ฅํ•œ๋‹ค. ์ฒซ ์—ด์€ ์ˆซ์ž๋กœ ์ž…๋ ฅํ•˜์˜€์ง€๋งŒ ์ดํ›„ ๋ ˆ์ด๋ธ”์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฌธ์ž๋กœ ์ถœ๋ ฅ๋˜๊ฒŒ ์„ค์ •ํ•  ๊ฒƒ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ์—ด์€ ํ™˜์ž ์ˆ˜๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ๋ณ€์ˆ˜์— ์ ์šฉ ๊ฒฐ๊ณผ ํ™•์ธ ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ์—์„œ ๋ณ€์ˆ˜์— ์„ค์ •๋œ ๊ธฐ๋ณธ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค. ์ด๋ฆ„์€ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ํ™˜์ž ์œ ๋ฌด์™€ ์•” ํ™˜์ž ์ˆ˜๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ํ˜„์žฌ ๋ณ€์ˆ˜๋“ค์€ ์†Œ์ˆ˜์ ์ด ํ•„์š” ์—†์œผ๋ฏ€๋กœ ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฟ์ˆ˜๋Š” 0์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ๊ฐ’ ์†์„ฑ์—์„œ ํ™˜์ž ์œ ๋ฌด๋Š” 1์„ ์™„์น˜, 2๋ฅผ ์น˜๋ฃŒ ์ค‘์œผ๋กœ ๋ ˆ์ด๋ธ”์„ ๋ถ™์—ฌ ์ˆซ์ž์— ํ•ด๋‹น ๊ฐ’์„ ํ‘œํ˜„ํ•œ๋‹ค. ๊ฐ€์ค‘ ์ผ€์ด์Šค ์„ค์ • ๋ณ€์ˆ˜์— ์„ค์ •ํ•œ ๊ฐ’์ด ์ž˜ ์ ์šฉ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋งŒ์ผ ๊ฐ’์— ๋ ˆ์ด๋ธ” ํ•œ ๊ฐ’์ด ๋ณด์ด์ง€ ์•Š์œผ๋ฉด ๋ณด๊ธฐ - > ๊ฐ’ ๋ ˆ์ด๋ธ” ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋ณด์ธ๋‹ค. ํ˜„์žฌ ์ž…๋ ฅ๋œ ๊ฐ’์€ ์›์ž๋ฃŒ๋ฅผ ์š”์•ฝํ•œ ์ž๋ฃŒ๋กœ ๋นˆ๋„๋ฅผ ๋ชจ๋“  ๋ณ€์ˆ˜์— ์ ์šฉํ•˜๋ ค๋ฉด ๊ฐ€์ค‘์น˜ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ€์ค‘์น˜๋Š” ๋ฐ์ดํ„ฐ -> ๊ฐ€์ค‘ ์ผ€์ด์Šค ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๊ฐ€์ค‘ ์ผ€์ด์Šค ์ฐฝ์—์„œ ๊ฐ€์ค‘ ์ผ€์ด์Šค ์ง€์ •์— ๋นˆ๋„ ๋ณ€์ˆ˜๋Š” ์•” ํ™˜์ž ์ˆ˜๋กœ ์„ค์ •ํ•œ๋‹ค. ์นด์ด์ œ๊ณฑ ๊ฒ€์ • ์‹คํ–‰ ์ด์ œ ์ž๋ฃŒ ์„ค์ •์ด ์™„๋ฃŒ๋˜์—ˆ๊ณ , ๋‹จ์ผ ํ‘œ๋ณธ ๋น„์œจ ๊ฒ€์ •์€ ๋ถ„์„ -> ๋น„๋ชจ์ˆ˜ ๊ฒ€์ • -> ๋ ˆ๊ฑฐ์‹œ ๋Œ€ํ™”์ƒ์ž -> ์นด์ด์ œ๊ณฑ ๊ฒ€์ • ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ ๋น„์œจ ์„ค์ • ์นด์ด์ œ๊ณฑ ๊ฒ€์ • ์ฐฝ์—์„œ ๊ฒ€์ • ๋ณ€์ˆ˜์— ํ™˜์ž ์œ ๋ฌด ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ธฐ๋Œ“๊ฐ’์— ๊ท€๋ฌด๊ฐ€์„ค 0 p 0.3 ์„ ์„ค์ •ํ•œ๋‹ค. ๊ธฐ๋Œ“๊ฐ’์€ ๊ฐ ๋ฒ”์ฃผ ๊ฐ’์— ๋Œ€ํ•œ ๋น„์œจ์„ ์ž…๋ ฅํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 3,7๋กœ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ 30,70์œผ๋กœ ์ž…๋ ฅํ•ด๋„ ๋œ๋‹ค. ๊ธฐ๋Œ“๊ฐ’์— ์ž…๋ ฅํ•˜๋Š” ๊ฐ’์˜ ์ˆœ์„œ๋Š” ๊ฒ€์ • ๋ณ€์ˆ˜์—์„œ ๋ฒ”์ฃผ ๊ฐ’์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์ด๋‹ค. ์ถœ๋ ฅ ๋ฐ ๊ฒฐ๊ณผ ๋ถ„์„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ ๋นˆ๋„๋Š” ๊ฐ ๊ฐ’์„ ์ž˜ ์ž…๋ ฅํ•˜์˜€๋Š”์ง€, ๋˜ ๊ฐ€์ค‘์น˜๋Š” ์ž˜ ์ ์šฉํ•˜์˜€๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋‹จ์ผ ํ‘œ๋ณธ ๋น„์œจ ๊ฒ€์ •์€ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋กœ ๊ฒ€์ •ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ์นด์ด ์ œ๊ณฑ ํ†ต๊ณ„๋Ÿ‰์ด๋‹ค. ๋‹ค์Œ์€ ์นด์ด์ œ๊ณฑ๋ถ„ํฌ 2 ( ) ์™€ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์˜ ๊ด€๊ณ„์ด๋‹ค. 2 ฯ‡ ( ) ฮฑ ( ) Z / ์ด์™€ ๊ฐ™์€ ๊ด€๊ณ„๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹จ์ผ ํ‘œ๋ณธ ๋น„์œจ Z - ๊ฒ€์ •์— ์นด์ด์ œ๊ณฑ๋ถ„ํฌ๋ฅผ ํ™œ์šฉํ•œ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 p 0.3 ์ผ ๋•Œ ๋Œ€๋ฆฝ๊ฐ€์„ค 1 p 0 (์–‘์ธก๊ฒ€์ •)์— ๋Œ€ํ•œ ์œ ์˜ ํ™•๋ฅ ์€ 0.011 ์ด๊ณ  ๋Œ€๋ฆฝ๊ฐ€์„ค 1 p 0.3 (๋‹จ ์ธก ๊ฒ€์ •)์— ๋Œ€ํ•œ ์œ ์˜ ํ™•๋ฅ ์€ 0.011 = 0.0055 ์ด๋‹ค. ๊ทธ ์ด์œ ๋Š” ์นด์ด์ œ๊ณฑ๋ถ„ํฌ ํ™•๋ฅ  ๊ฐ’ [ > p ] p ๋Š” ์ •๊ทœ๋ถ„ํฌ ํ™•๋ฅ  ๊ฐ’์ด [ > p 2 ] P [ < Z / ] p ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. SPSS ์ถœ๋ ฅ์—์„œ ์นด์ด ์ œ๊ณฑ ์œ ์˜ ํ™•๋ฅ ์€ [ 2 6.429 ] 0.011 ์„ ๊ณ„์‚ฐํ•˜์˜€๊ณ  ์ •๊ทœ๋ถ„ํฌ์—์„œ ์œ ์˜ ํ™•๋ฅ ์€ [ > 6.429 2.535547 ] P [ > 6.429 โˆ’ 2.535547 ] 0.011 ๋ถ„์„ ๊ฒฐ๊ณผ ์–‘์ธก๊ฒ€์ •์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.011๋กœ ์œ ์˜ํ•˜๋ฏ€๋กœ ๊ท€๋ฌด๊ฐ€์„ค 0 p 0.3 ๋ฅผ ๊ธฐ๊ฐํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ž๋ฃŒ์—์„œ ์น˜๋ฃŒ ๋น„์œจ 27 60 0.45 ๋Š” ์ด์ „ ์™„์น˜์œจ 0.3๊ณผ๋Š” ํ†ต๊ณ„์ ์œผ๋กœ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. 2. ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ๋น„์œจ ์ถ”๋ก  ์–ด๋–ค ํ™”ํ•™์ ์ธ ์ฒ˜๋ฆฌ๊ฐ€ ์”จ์˜ ๋ฐœ์•„ ๋น„์œจ์„ ๋†’์ด๋Š”๋ฐ ํšจ๊ณผ๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•˜์—ฌ ํ™”ํ•™์  ์ฒ˜๋ฆฌ๊ฐ€ ๋œ 100 ๊ฐœ์˜ ์”จ์™€, ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ๋ณดํ†ต์˜ ์”จ 150 ๊ฐœ๋ฅผ ํŒŒ์ข…ํ•˜์—ฌ ๋ฐœ์•„๋˜๋Š” ๊ฒƒ์„ ๊ด€์ฐฐํ•˜์˜€๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ํ™”ํ•™์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋œ ์”จ๋Š” 88 ๊ฐœ, ๋ณดํ†ต ์”จ๋Š” 126 ๊ฐœ๊ฐ€ ๋ฐœ์•„๋˜์—ˆ๋‹ค. ํ™”ํ•™์ ์ธ ์”จ๊ฐ€ ๋ฐœ์•„ ๋น„์œจ์„ ๋†’์ธ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์ •ํ•˜์ž. ๋ฐœ ์•„์œ ๋ฌด ํ•ฉ๊ณ„ ๋ฐœ์•„ ๋ถˆ๋Ÿ‰ ์”จ์•— ์ข…๋ฅ˜ ํ™”ํ•™์ฒ˜๋ฆฌ 88 12 100 ๋ณดํ†ต 126 24 150 ํ•ฉ๊ณ„ 214 36 250 ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ์ถ”๋ก  ์ด๋ก ์  ๋‚ด์šฉ ๋…๋ฆฝ์ธ ๋‘ ์ง‘๋‹จ์˜ ๋น„์œจ ์ฐจ์ด 1 p์— ๋Œ€ํ•œ 100 ( โˆ’ ) ์‹ ๋ขฐ๊ตฌ๊ฐ„(๋‘ ํ‘œ๋ณธ์ด ๋ชจ๋‘ ํด ๋•Œ; 1 1 10 n ( โˆ’ 1 ) 10 n p โ‰ฅ 10 n ( โˆ’ 2 ) 10 ) ( ( ^ โˆ’ ^ ) z / p 1 ( โˆ’ ^ ) 1 p 2 ( โˆ’ ^ ) 2 ( ^ โˆ’ ^ ) z / p ๋˜๋Š” ๋˜๋Š” ( ^ โˆ’ ^ ) z / p 1 ( โˆ’ ^ ) 1 p 2 ( โˆ’ ^ ) 2 ์—ฌ๊ธฐ์„œ ^ = n, ^ = n๋Š” ํ‘œ๋ณธ ๋น„์œจ,๋Š” ์ดํ•ญ๋ถ„ํฌ์˜ ์ด ์‹œํ–‰ ํšŸ์ˆ˜ 1 ์—์„œ ์„ฑ๊ณต์ธ ํšŸ์ˆ˜,๋Š” ์ดํ•ญ๋ถ„ํฌ์˜ ์ด ์‹œํ–‰ ํšŸ์ˆ˜ 2 ์—์„œ ์„ฑ๊ณต์ธ ํšŸ์ˆ˜, ฮฑ 2 ๋Š” ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ๋ถ„์œ„์ˆ˜, 1 n๋Š” ์ž๋ฃŒ์˜ ๊ฐœ์ˆ˜ ๋…๋ฆฝ์ธ ๋‘ ์ง‘๋‹จ์˜ ๋น„์œจ ์ฐจ์ด 1 p์— ๋Œ€ํ•œ ๊ฒ€์ •(๋‘ ํ‘œ๋ณธ์ด ๋ชจ๋‘ ํด ๋•Œ; 1 1 10 n ( โˆ’ 1 ) 10 n p โ‰ฅ 10 n ( โˆ’ 2 ) 10 ) โ‘  ๊ท€๋ฌด๊ฐ€์„ค 0 p โˆ’ 2 0 (์˜๋ฏธ : ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์€ ๊ฐ™๋‹ค) ๋Œ€๋ฆฝ๊ฐ€์„ค์€ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์—ฐ๊ตฌ์ž๊ฐ€ ์ •ํ•œ๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค 1 p โˆ’ 2 0 ๋‹จ ์ธก ๊ฒ€์ • ๋Œ€๋ฆฝ๊ฐ€์„ค 1 p โˆ’ 2 0 ๋‹จ ์ธก ๊ฒ€์ • ๋Œ€๋ฆฝ๊ฐ€์„ค 1 p โˆ’ 2 0 ์–‘์ธก๊ฒ€์ • โ‘ก ์—ฐ๊ตฌ์ž๋Š” ์œ ์˜์ˆ˜์ค€์— ๋Œ€ํ•œ ๊ธฐ๊ฐ์—ญ์„ ๊ตฌํ•˜๊ณ  ์กฐ์‚ฌํ•œ ์ž๋ฃŒ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ = ( 1 โˆ’ 2 ) ^ ( โˆ’ ^ ) n + n ๊ณผ ์œ ์˜ ํ™•๋ฅ (p-value)์„ ๊ตฌํ•œ๋‹ค. ๋‘ ์ง‘๋‹จ์˜ ํ•ฉ๋™ ๋น„์œจ ์ถ”์ •๋Ÿ‰์€ ^ X Y 1 n์ด๋‹ค. โ‘ข ์—ฐ๊ตฌ์ž๋Š” ์œ ์˜ ํ™•๋ฅ ๊ณผ ์œ ์˜์ˆ˜์ค€์˜ ๋Œ€์†Œ๋‚˜ ๊ธฐ๊ฐ์—ญ์— ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ํฌํ•จ๋˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์˜ ๊ธฐ๊ฐ, ์ฑ„ํƒ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ•ด์„ํ•œ๋‹ค. SPSS ๋ถ„์„ ๊ณผ์ • ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ์— ์ž๋ฃŒ ์ž…๋ ฅ ์ƒˆ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ํ‘œ์— ์žˆ๋Š” ์ž๋ฃŒ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ์ž๋ฃŒ๋Š” ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž…๋ ฅํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ณ€์ˆ˜๋Š” ์ฒซ ๋ฒˆ์งธ ์—ด์— ์ž…๋ ฅํ•˜๋ฉฐ ํ‘œ์—์„œ ํ–‰ ๋ฒˆํ˜ธ์— ํ•ด๋‹น๋œ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ณ€์ˆ˜๋Š” ๋‘ ๋ฒˆ์งธ ์—ด์— ์ž…๋ ฅํ•˜๋ฉฐ ํ‘œ์—์„œ ์—ด ๋ฒˆํ˜ธ์— ํ•ด๋‹น๋œ๋‹ค. ์„ธ ๋ฒˆ์งธ ๋ณ€์ˆ˜๋Š” ์„ธ ๋ฒˆ์งธ ์—ด์— ์ž…๋ ฅํ•˜๋ฉฐ ํ‘œ์—์„œ ๊ฐ ์…€์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์ด๋‹ค. 1ํ–‰ 1์—ด์€ 88, 1ํ–‰ 2์—ด์€ 12, 2ํ–‰ 1์—ด์€ 126, 2ํ–‰ 2์—ด์€ 24์ด๋‹ค. ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ์— ๋ณ€์ˆซ๊ฐ’ ์„ค์ • ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ์— ์ž…๋ ฅ๋œ ๋ณ€์ˆ˜์˜ ํŠน์„ฑ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค. ์ด๋ฆ„์— ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•œ๋‹ค. ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฌ๋Š” ์ด ์ž๋ฃŒ์— ํ•ด๋‹น๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ 0์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. ๊ฐ’์€ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ์— ๋Œ€ํ•˜์—ฌ ๋ ˆ์ด๋ธ”์„ ์„ค์ •ํ•œ๋‹ค. ์”จ์•— ์ข…๋ฅ˜ ๋ณ€์ˆ˜๋Š” ๊ธฐ์ค€๊ฐ’ 1์€ ๋ ˆ์ด๋ธ” ํ™”ํ•™์ฒ˜๋ฆฌ, ๊ธฐ์ค€๊ฐ’ 2๋Š” ๋ณดํ†ต ๋ฐœ ์•„์œ ๋ฌด ๋ณ€์ˆ˜๋Š” ๊ธฐ์ค€๊ฐ’ 1์€ ๋ ˆ์ด๋ธ” ๋ฐœ์•„, ๊ธฐ์ค€๊ฐ’ 2๋Š” ๋ถˆ๋Ÿ‰์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. ์„ค์ •ํ•œ ๋ณ€์ˆซ๊ฐ’ ํ™•์ธ ๋ณ€์ˆ˜์— ์„ค์ •ํ•œ ๊ฐ’์ด ์ž˜ ์ ์šฉ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋งŒ์ผ ๊ฐ’์— ๋ ˆ์ด๋ธ” ํ•œ ๊ฐ’์ด ๋ณด์ด์ง€ ์•Š์œผ๋ฉด ๋ณด๊ธฐ - > ๊ฐ’ ๋ ˆ์ด๋ธ” ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋ณด์ธ๋‹ค. ๊ฐ€์ค‘ ์ผ€์ด์Šค ์„ค์ • ํ˜„์žฌ ์ž…๋ ฅ๋œ ๊ฐ’์€ ์›์ž๋ฃŒ๋ฅผ ์š”์•ฝํ•œ ์ž๋ฃŒ๋กœ ๋นˆ๋„๋ฅผ ๋ชจ๋“  ๋ณ€์ˆ˜์— ์ ์šฉํ•˜๋ ค๋ฉด ๊ฐ€์ค‘์น˜ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ€์ค‘์น˜๋Š” ๋ฐ์ดํ„ฐ -> ๊ฐ€์ค‘ ์ผ€์ด์Šค ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๊ฐ€์ค‘ ์ผ€์ด์Šค ์ฐฝ์—์„œ ๊ฐ€์ค‘ ์ผ€์ด์Šค ์ง€์ •์— ๋นˆ๋„ ๋ณ€์ˆ˜๋Š” ๊ฐœ์ˆ˜๋กœ ์„ค์ •ํ•œ๋‹ค. ๊ต์ฐจ๋ถ„์„ ์‹คํ–‰ ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ๋น„์œจ ๊ฒ€์ •์€ ๋ถ„์„ -> ๊ธฐ์ˆ ํ†ต๊ณ„๋Ÿ‰ -> ๊ต์ฐจ๋ถ„์„ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๊ต์ฐจ๋ถ„์„ ์ฐฝ์—์„œ ํ–‰์— ์”จ์•— ์ข…๋ฅ˜, ์—ด์— ๋ฐœ ์•„์œ ๋ฌด ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ†ต๊ณ„๋Ÿ‰ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. ํ†ต๊ณ„๋Ÿ‰ ์ฐฝ์—์„œ ์นด์ด ์ œ๊ณฑ์„ ์„ ํƒํ•œ๋‹ค. ๊ฒฐ๊ณผ ๋ฐ ๋ถ„์„ ๋ถ„์„ ๊ฒฐ๊ณผ ๋‘ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๊ต์ฐจ๋ถ„์„ ํ‘œ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. ๊ต์ฐจ๋ถ„์„ ํ‘œ์— ๊ฐ’์ด ์ž˜ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€, ๊ฐ€์ค‘์น˜๋Š” ์ž˜ ์„ค์ •๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ๋น„์œจ ๊ฒ€์ •์€ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋กœ ๊ฒ€์ •ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ์นด์ด ์ œ๊ณฑ ํ†ต๊ณ„๋Ÿ‰์ด๋‹ค. ๋‹ค์Œ์€ ์นด์ด์ œ๊ณฑ๋ถ„ํฌ 2 ( ) ์™€ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์˜ ๊ด€๊ณ„์ด๋‹ค. 2 ฯ‡ ( ) ฮฑ ( ) Z / ์ด์™€ ๊ฐ™์€ ๊ด€๊ณ„๋ฅผ ์ด์šฉํ•˜์—ฌ ๋…๋ฆฝ์ธ ๋‘ ํ‘œ๋ณธ ๋น„์œจ - ๊ฒ€์ •์— ์นด์ด์ œ๊ณฑ๋ถ„ํฌ๋ฅผ ํ™œ์šฉํ•œ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 p โˆ’ 2 0 ์ผ ๋•Œ ๋Œ€๋ฆฝ๊ฐ€์„ค 1 p โˆ’ 2 0 (์–‘์ธก๊ฒ€์ •)์— ๋Œ€ํ•œ ์œ ์˜ ํ™•๋ฅ ์€ 0.378 ์ด๊ณ  ๋Œ€๋ฆฝ๊ฐ€์„ค 1 p โˆ’ 2 0 (๋‹จ ์ธก ๊ฒ€์ •)์— ๋Œ€ํ•œ ์œ ์˜ ํ™•๋ฅ ์€ 0.378 = 0.189 ์ด๋‹ค. ๊ทธ ์ด์œ ๋Š” ์นด์ด์ œ๊ณฑ๋ถ„ํฌ ํ™•๋ฅ  ๊ฐ’ [ > p ] p ๋Š” ์ •๊ทœ๋ถ„ํฌ ํ™•๋ฅ  ๊ฐ’์ด [ > p 2 ] P [ < Z / ] p ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. SPSS ์ถœ๋ ฅ์—์„œ ์นด์ด ์ œ๊ณฑ ์œ ์˜ ํ™•๋ฅ ์€ [ 2 0.779 ] 0.378 ์„ ๊ณ„์‚ฐํ•˜์˜€๊ณ  ์ •๊ทœ๋ถ„ํฌ์—์„œ ์œ ์˜ ํ™•๋ฅ ์€ [ > 0.779 0.8826098 ] P [ < 0.779 โˆ’ 0.8826098 ] 0.378 ๋ถ„์„ ๊ฒฐ๊ณผ ์–‘์ธก๊ฒ€์ •์— ๋Œ€ํ•˜์—ฌ ์œ ์˜ ํ™•๋ฅ ์ด 0.378๋กœ ์œ ์˜ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ท€๋ฌด๊ฐ€์„ค 0 p โˆ’ 2 0 ๋ฅผ ๊ธฐ๊ฐ ๋ชปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ž๋ฃŒ์—์„œ ๋ฐœ์•„ ๋น„์œจ ^ = 88 100 0.88 p 2 126 150 0.84 ๋Š” ํ†ต๊ณ„์ ์œผ๋กœ ๋น„์œจ ์ฐจ์ด๊ฐ€ ์—†๋‹ค. 07. ํšŒ๊ท€๋ถ„์„ ํšŒ๊ท€๋ถ„์„ ์†Œ๊ฐœ ํšŒ๊ท€(regression)๋Š” ๊ฐˆํ†ค ๊ฒฝ(Sir Francis Galton, 1822-1911)์ด 1885๋…„ ๋ฐœํ‘œํ•œ "Regression toward mediocrity in hereditary stature''์— ์ฒ˜์Œ์œผ๋กœ ์‚ฌ์šฉ ์•„๋ฒ„์ง€ ํ‚ค์˜ ์œ ์ „์  ์„ฑ์งˆ๊ณผ ์ž์‹๋“ค ํ‚ค์˜ ์ •๊ทœ๊ณก์„ ์˜ ๋ฌธ์ œ ๋…๋ฆฝ๋ณ€์ˆ˜(independent variable) : ์ข…์†๋ณ€์ˆ˜์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๋ณ€์ˆ˜ ์ข…์†๋ณ€์ˆ˜(dependent variable) : ๋…๋ฆฝ๋ณ€์ˆ˜์˜ ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๋ณ€์ˆ˜ ์˜ํ–ฅ์„ ์ฃผ๋Š” ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜์™€ ์˜ํ–ฅ์„ ๋ฐ›๋Š” ํ•œ ๋ณ€์ˆ˜์™€์˜ ๊ด€๊ณ„์— ๋Œ€ํ•œ ๋ชจ๋ธ ์„ค์ •๊ณผ ๊ทธ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ถ”๋ก  ๋‹จ์ˆœ ์„ ํ˜•ํšŒ๊ท€ ๋ชจํ˜•(simple linear regression model) : ๋…๋ฆฝ๋ณ€์ˆ˜์™€ ์ข…์†๋ณ€์ˆ˜๊ฐ€ ๊ฐ๊ฐ ํ•œ ๊ฐœ์ธ ๊ฒฝ์šฐ์˜ ๋‹จ์ˆœํšŒ๊ท€๋ชจํ˜• ๋‹ค์ค‘์„ ํ˜•ํšŒ๊ท€๋ชจํ˜•(multiple linear regression model) : ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋…๋ฆฝ๋ณ€์ˆ˜์™€ ํ•œ ๊ฐœ์˜ ์ข…์†๋ณ€์ˆ˜์˜ ์„ ํ˜•ํšŒ๊ท€ ๋ชจํ˜• ์˜ค๋Š˜๋‚  ์‚ฌํšŒ์˜ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ํ™œ์šฉ ๋ฒ”์œ„๊ฐ€ ๋„“์€ ๋ถ„์„๋ฐฉ๋ฒ•์ž„ 1. ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€ ๋ชจํ˜• ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€ ๋ชจํ˜• ๋…๋ฆฝ๋ณ€์ˆ˜์™€ ์ข…์†๋ณ€์ˆ˜๊ฐ€ ๊ฐ๊ฐ ํ•œ ๊ฐœ์”ฉ ์ด๋ฃจ์–ด์ง„ ๋‹จ์ˆœ ์„ ํ˜•ํšŒ๊ท€ ๋ชจํ˜• ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€ ๋ชจํ˜• : i ฮฒ + 1 i ฮต, i 1 2. . , ฮฒ, 1 ์€ ์ง์„ ์‹์„ ๊ฒฐ์ •ํ•˜๋Š” ๋ฏธ์ง€์˜ ํšŒ๊ท€ ๋ชจ์ˆ˜ ์˜ค์ฐจ i ๋“ค์€ ์„œ๋กœ ๋…๋ฆฝ์ด๊ณ  ํ‰๊ท ์ด 0์ด๊ณ  ๋ถ„์‚ฐ์ด 2 ์ธ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฆ„ i i ๋ฒˆ์งธ ์‹œํ–‰์—์„œ ๋…๋ฆฝ๋ณ€์ˆ˜ i ๋กœ ๊ณ ์ •์‹œ์ผฐ์„ ๋•Œ ์ข…์†๋ณ€์ˆ˜์˜ ๊ฐ’์ž„. i N ( 0 ฮฒ x, 2 ) 0 ฮฒ ์ถ”์ • ์ตœ์†Œ์ œ๊ณฑ๋ฒ•์œผ๋กœ ๋ชจ์ˆ˜ ์ถ”์ • ์ตœ์†Œ์ œ๊ณฑ๋ฒ•(least squares method) : ํŽธ์ฐจ์˜ ์ œ๊ณฑํ•ฉ์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ชจ์ˆ˜๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ• ๋‹จ์ˆœํšŒ๊ท€ ์ตœ์†Œ์ œ๊ณฑ๋ฒ•์— ์˜ํ•œ ๋‹จ์ˆœ ํšŒ๊ท€ ์ถ”์ •์‹์€ ^ ฮฒ 0 ฮฒ 1 i ์ž”์ฐจ i y โˆ’ ^ = i ( ^ + ^ x) ์ž”์ฐจ์˜ ์ œ๊ณฑํ•ฉ = i 1 e 2 โˆ‘ = n [ i ( ^ + ^ x) ] ์„ ํŽธ ๋ฏธ๋ถ„ํ•˜๋ฉด q ฮฒ = i 1 ( 2 ) [ i ( ^ + ^ x) ] q ฮฒ = i 1 ( 2 ) i [ i ( ^ + ^ x) ] ์œ„ ๋‘ ์‹์„ ์ •๊ทœ๋ฐฉ์ •์‹์ด๋ผ ํ•˜๋ฉฐ, 0 ฮฒ์— ๋Œ€ํ•œ ํ•ด๋ฅผ ๊ตฌํ•œ๋‹ค. i 1 y = ฮฒ + 1 i 1 x โˆ‘ = n i i ฮฒ โˆ‘ = n i ฮฒ โˆ‘ = n i ๋‘ ์‹์—์„œ 0 ฮฒ์˜ ํ•ด๋ฅผ ๊ตฌํ•˜๋ฉด 1 n ( i 1 x y) ( i 1 x) ( i 1 y) ( i 1 x 2 ) ( i 1 x ) , ฮฒ = i 1 y โˆ’ 1 i 1 x n ์‚ฌ์šฉํ•  ๊ธฐํ˜ธ๋“ค์„ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค x = i 1 ( i x) = i 1 x 2 1 ( i 1 x) S y โˆ‘ = n ( i y) = i 1 y 2 1 ( i 1 y) S y โˆ‘ = n ( i x) ( i y) โˆ‘ = n i i 1 ( i 1 x) ( i 1 y) ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœํ•œ ์ž„์˜ํ‘œ๋ณธ ( i y) i 1 2. . n ์˜ ์ถ”์ • ํšŒ๊ท€๊ณ„์ˆ˜๋Š” ^ = x S x ฮฒ 0 y โˆ’ ^ x ํšŒ๊ท€์‹ ๋ถ„ํ•ด ๋‹จ์ˆœ ํšŒ๊ท€์—์„œ ๋‘ ๋ณ€์ˆ˜ ๊ด€๊ณ„๋Š” i y i e = ^ + ^ x + i ฮฒ 0 ฮฒ 1 i ์ด๊ณ , ์–‘๋ณ€์— ยฏ ๋ฅผ ๋นผ๋ฉด i y = ^ โˆ’ ยฏ e = ^ + ^ x โˆ’ ยฏ y โˆ’ ^ โˆ’ ^ x์ด๋‹ค. ์ด ์‹์—์„œ ํšŒ๊ท€ ์ œ๊ณฑํ•ฉ์€ S = i 1 ( ^ โˆ’ โ€• ) = ^ S y ์ด๊ณ  ์ž”์ฐจ ์ œ๊ณฑํ•ฉ์€ S = e 2 โˆ‘ ( i ฮฒ 0 ฮฒ 1 i ) = y โˆ’ x 2 x์ด๋‹ค. ์ œ๊ณฑํ•ฉ ๋ถ„ํ•ด๋Š” ์ด ์ œ๊ณฑํ•ฉ ํšŒ๊ท€ ์ œ๊ณฑํ•ฉ ์ž”์ฐจ ์ œ๊ณฑํ•ฉ ( i y) = ( ^ โˆ’ โ€• ) + ( i y i ) S T ( ์ด ์ œ๊ณฑํ•ฉ ) S R ( ํšŒ๊ท€ ์ œ๊ณฑํ•ฉ ) S E ( ์ž”์ฐจ ์ œ๊ณฑํ•ฉ ) ์ด๋‹ค. ๋‹ค์Œ ํ‘œ๋Š” ๋‹จ์ˆœ ์„ ํ˜•ํšŒ๊ท€์— ๋Œ€ํ•œ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค. ์š”์ธ ์ œ๊ณฑํ•ฉ ์ž์œ ๋„ ํ‰๊ท  ์ œ๊ณฑํ•ฉ F ํšŒ๊ท€ S = i 1 ( ^ โˆ’ โ€• ) = ^ S y M R S R M R S ์ž”์ฐจ S = i 1 ( i y i ) = y โˆ’ ^ S y โˆ’ M E S E โˆ’ ํ•ฉ๊ณ„ S = i 1 ( i y) = y n 1 ๋‹จ์ˆœ ์„ ํ˜•ํšŒ๊ท€ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ ๋‹จ์ˆœ ์„ ํ˜•ํšŒ๊ท€ ์˜ˆ์ œ ๋‹ค์Œ์€ ์–ด๋–ค ์•ฝ์˜ ๋ณต์šฉ๋Ÿ‰๊ณผ ํšจ๊ณผ๊ฐ€ ์ง€์†๋˜๋Š” ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•˜์˜€๋‹ค. ์ž๋ฃŒ์ถœ์ฒ˜ : ๊ฐœ์ •ํŒ ํ†ต๊ณ„ํ•™ -์—‘์…€์„ ์ด์šฉํ•œ ๋ถ„์„- ๊น€์ง„๊ฒฝ ๋“ฑ(2008) ๋ณต์šฉ๋Ÿ‰( ) 3 3 4 5 6 6 7 8 8 9 ์‹œ๊ฐ„( ) 9 5 12 9 14 16 22 18 24 22 ๋ณต์šฉ๋Ÿ‰๊ณผ ์‹œ๊ฐ„ ๋‹ค์Œ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์—์„œ ์œ ์˜ ํ™•๋ฅ ์ด 0.001๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ๊ท€๋ฌด๊ฐ€์„ค 0 ฮฒ = ์„ ๊ธฐ๊ฐํ•œ๋‹ค. ๋”ฐ๋ผ์„œ 1 ๊ฐ’์€ ์œ ์˜ํ•˜๋‹ค. ์š”์ธ ์ œ๊ณฑํ•ฉ ์ž์œ ๋„ ํ‰๊ท  ์ œ๊ณฑํ•ฉ F ์œ ์˜ ํ™•๋ฅ  ํšŒ๊ท€ S = i 1 ( ^ โˆ’ โ€• ) = ^ S y 307.247 M R S R = 307.247 S M E 38.615 0.001 ์ž”์ฐจ S = i 1 ( i y i ) = y โˆ’ ^ S y 63.653 โˆ’ = M E S E โˆ’ = 7.957 ํ•ฉ๊ณ„ S = i 1 ( i y) = y = 370.900 โˆ’ = ๋‹จ์ˆœ ์„ ํ˜•ํšŒ๊ท€ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ SPSS๋ฅผ ์ด์šฉํ•˜๊ธฐ ๊ฐ ์ž๋ฃŒ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ํšŒ๊ท€๋ถ„์„ ์‹คํ–‰์€ ๋ถ„์„ ํšŒ๊ท€๋ถ„์„ ์„ ํ˜•ํšŒ๊ท€ ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•œ๋‹ค. ์ž๋ฃŒ๋Š” ๋…๋ฆฝ๋ณ€์ˆ˜์™€ ์ข…์†๋ณ€์ˆ˜๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅํ•œ๋‹ค. ๋ถ„์„ ๊ฒฐ๊ณผ 2 , ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ, ๊ฐ ํšŒ๊ท€๊ณ„์ˆ˜์— ๋Œ€ํ•œ ํ†ต๊ณ„๋Ÿ‰์ด ์ถœ๋ ฅ๋œ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ๋Š” ์œ„์˜ ํ‘œ์—์„œ ๊ณ„์‚ฐํ•œ ๊ฐ’๊ณผ ๊ฐ™์Œ์„ ํ™•์ธํ•œ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์—์„œ ๋…๋ฆฝ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ํšŒ๊ท€๊ณ„์ˆ˜๊ฐ€ ์œ ์˜ํ•˜๊ณ  ์ƒ์ˆ˜ํ•ญ์— ๋Œ€ํ•œ ํšŒ๊ท€๊ณ„์ˆ˜๊ฐ€ ์œ ์˜ํ•œ์ง€ ๊ฒ€์ •ํ•œ๋‹ค. ์œ ์˜ ํ™•๋ฅ ์€ ์ƒ์ˆ˜( 0 )๊ฐ€ 0.707 ์œผ๋กœ ์œ ์˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์ด ์˜๋ฏธ๊ฐ€ ์—†๊ณ , ์ฆ‰,์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ณ , ๋ณต์š•๋Ÿ‰( 1 )์ด 0.000 ์œผ๋กœ ์œ ์˜ํ•˜๋ฏ€๋กœ ๊ณ„์ˆ˜๊ฐ€ ํ†ต๊ณ„์ ์œผ๋กœ ์˜๋ฏธ ์žˆ๋‹ค. ๋…๋ฆฝ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ํ†ต๊ณ„๋Ÿ‰์€ ANOVA ๋ถ„์„ ๊ฒฐ๊ณผ์™€ ๊ณ„์ˆ˜ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•œ ๊ฐ’์ด๋‹ค. ๊ทธ ์ด์œ ๋Š” t ๋ถ„ํฌ์™€ F ๋ถ„ํฌ์˜ ๊ด€๊ณ„๊ฐ€ ฮฑ 2 ( ) F, , , 2 p ( โˆ’ ) โˆ’ F, ์ด๊ธฐ ๋•Œ๋ฌธ์— ANOVA ๋ถ„์„ = 38.615 ์™€ ๊ณ„์ˆ˜์—์„œ = 6.214 t = 38.625 ๊ฐ’์ด ๊ฐ™๋‹ค. ์ง์„  ์œ„ ์ ์ด ํ‰๊ท ์ธ ํ™•๋ฅ ๋ถ„ํฌ | =์˜ ์ •๊ทœ๋ถ„ํฌ ํšŒ๊ท€์‹ ๋ถ„ํ•ด ๊ธฐ๋Œ“๊ฐ’, ์‹ ๋ขฐ๋Œ€, ์˜ˆ์ธก๋Œ€ ๊ธฐ๋Œ“๊ฐ’ : ^ ฮฒ 0 ฮฒ 1 ์‹ ๋ขฐ๋Œ€(confidence band) : ^ t / ( โˆ’ ) 1 + ( โˆ’ ยฏ ) S x where = ^ ์˜ˆ์ธก๋Œ€(prediction band) : ^ t / ( โˆ’ ) 1 1 + ( โˆ’ ยฏ ) S x 2. ๋‹ค์ค‘์„ ํ˜• ํšŒ๊ท€๋ชจํ˜• ๋‹ค์ค‘ ์„ ํ˜•ํšŒ๊ท€(multiple linear regression)๋Š” ๋…๋ฆฝ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ์ด๋‹ค. ์ผ๋ฐ˜์ ์ธ ๋‹ค์ค‘์„ ํ˜• ํšŒ๊ท€๋ชจํ˜•์€ ์ข…์†๋ณ€์ˆ˜ ์™€ โˆ’ ๊ฐœ ๋…๋ฆฝ๋ณ€์ˆ˜ 1 X, , p 1 ๊ฐ€ ์žˆ์œผ๋ฉฐ ๊ด€๊ณ„์‹์€ [ ] ฮฒ + 1 1 ฮฒ X + + p 1 p 1 ์ด๋‹ค. ์ด ์‹์„ ๊ฐ ์ž๋ฃŒ๋งˆ๋‹ค ํ‘œํ˜„ํ•˜๋ฉด i ฮฒ + 1 i + 2 i + + p 1 i p 1 ฯต, i 1 2 โ‹ฏ n ์ด๋‹ค. i ๋Š” ์˜ค์ฐจ๋กœ ์ด์— ๋Œ€ํ•œ ๊ฐ€์ •์€ ํ‰๊ท ์ด, ๋ถ„์‚ฐ์ด 2 ์ด๊ณ  ์„œ๋กœ ๋…๋ฆฝ์ด๋ผ ๊ฐ€์ •ํ•œ๋‹ค. ํšŒ๊ท€์‹์„ ํ–‰๋ ฌ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด n 1 X ร— ฮฒ ร— + n 1 ์ด๋‹ค. ๋‹ค์ค‘ํšŒ๊ท€์‹์—์„œ ๊ฐ ํ•ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค. = ( 1 2 Y) X ( 1 x โ€ฒ x โ€ฒ ) ( X 11 12 X, โˆ’ 1 21 22 X, โˆ’ โ‹ฎ โ‹ฎ โ‹ฎ X 1 n โ‹ฏ n p 1 ) ฮฒ ( 0 1 ฮฒ โˆ’ ) ฯต ( 1 2 ฯต) ์—ฌ๊ธฐ์„œ๋Š” ์ข…์†๋ณ€์ˆ˜์ด๊ณ ,๋Š” ๋””์ž์ธ ํ–‰๋ ฌ(design matrix)๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ ์ฒซ ์—ด์€ ์ƒ์ˆ˜ํ•ญ์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋…๋ฆฝ๋ณ€์ˆ˜์ด๋‹ค. ๋‹ค์ค‘ ์„ ํ˜•ํšŒ๊ท€ ๋ถ„ํ•ด ๋‹ค์ค‘ ์„ ํ˜•ํšŒ๊ท€์—์„œ ์ œ๊ณฑํ•ฉ ๋ถ„ํ•ด๋Š” ์ด ์ œ๊ณฑํ•ฉ ํšŒ๊ท€ ์ œ๊ณฑํ•ฉ ์ž”์ฐจ ์ œ๊ณฑํ•ฉ i 1 ( i Y) = i 1 ( ^ โˆ’ โ€• ) + i 1 ( i Y i ) S T ( ์ œ ํ•ฉ ) S R ( ๊ท€ ๊ณฑ ) S E ( ์ฐจ ๊ณฑ ) ์ œ๊ณฑํ•ฉ ๋ถ„ํ•ด๋ฅผ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ์ด์ฐจ<NAME>์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ด ์ œ๊ณฑํ•ฉ SST๋Š” S = i 1 ( i Y) = i 1 Y 2 n โ€• = i 1 Y 2 1 ( i 1 Y)์œผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. i 1 Y 2 y y โˆ‘ = n i 1 y ์ด๋ฏ€๋กœ S = โ€ฒ โˆ’ n โ€ฒ 11 y y ( โˆ’ n) = โ€ฒ Ty ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•œ ๋ฒกํ„ฐ๋‚˜ ํ–‰๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. n 1 ( 1 1 ) I ร— = ( 0 0 1 0 โ‹ฎ โ‹ฎ 0 1 ) J ร— = 11 = ( 1 1 1 1 โ‹ฎ โ‹ฎ 1 1 ) ํšŒ๊ท€ ์ œ๊ณฑํ•ฉ SSR์€ S = i 1 ( i โˆ’ โ€• ) = i 1 Y ^ โˆ’ Y 2 โˆ‘ = n i 2 1 ( i 1 Y)์œผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. i 1 Y ^ = ( Xb ) ( Xb ) b ( โ€ฒ ) 1 โ€ฒ, i 1 Y = โ€ฒ์ด๋ฏ€๋กœ S = ( Xb ) ( Xb ) 1 y 11 y y X ( โ€ฒ ) 1 โ€ฒ ( โ€ฒ ) 1 โ€ฒ โˆ’ n โ€ฒ 11 y y X ( โ€ฒ ) 1 โ€ฒ โˆ’ n โ€ฒ 11 y y X ( โ€ฒ ) 1 โ€ฒ โˆ’ n โ€ฒ 11 y y ( ( โ€ฒ ) 1 โ€ฒ 1 J ) = โ€ฒ Ry ์ž”์ฐจ ์ œ๊ณฑํ•ฉ SSE์€ S = i 1 ( i Y ^ ) = ( โˆ’ ^ ) ( โˆ’ ^ ) ( โˆ’ Xb ) ( โˆ’ Xb ) y y 2 โ€ฒ Xb b X Xb y y 2 โ€ฒ ( โ€ฒ ) 1 โ€ฒ + โ€ฒ ( โ€ฒ ) 1 โ€ฒ ( โ€ฒ ) 1 = โ€ฒ โˆ’ โ€ฒ ( โ€ฒ ) 1 โ€ฒ = โ€ฒ ( โˆ’ ( โ€ฒ ) 1 โ€ฒ ) = โ€ฒ Ey ์œผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋‹ค์Œ์€ ๋‹ค์ค‘ํšŒ๊ท€๋ถ„์„ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์ด๋‹ค. ์š”์ธ ์ œ๊ณฑํ•ฉ ์ž์œ ๋„ ํ‰๊ท  ์ œ๊ณฑํ•ฉ F ํšŒ๊ท€ S = i 1 ( ^ โˆ’ โ€• ) = โ€ฒ Ry โˆ’ M R S R โˆ’ M R S ์ž”์ฐจ S = i 1 ( i Y i ) = โ€ฒ Ey โˆ’ M E S E โˆ’ ํ•ฉ๊ณ„ S = i 1 ( i Y) = โ€ฒ Ty โˆ’ ๋‹ค์ค‘ ์„ ํ˜•ํšŒ๊ท€ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ ๋‹ค์ค‘์„ ํ˜• ํšŒ๊ท€๋ชจํ˜•์—์„œ ๋ณ€์ˆ˜ ์„ ํƒ ๋ฐฉ๋ฒ• SPSS์—์„œ ๋ชจํ˜•์— ๋ณ€์ˆ˜๋ฅผ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐ๋Š” ๋ถ€๋ถ„ F - ๊ฒ€์ •(partial F-test)์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ตœ์ดˆ ๋ณ€์ˆ˜ ์„ ํƒ์€ = S i S i i 1 โ‹ฏ p 1 ํ†ต๊ณ„๋Ÿ‰ ์ค‘ ๊ฐ€์žฅ ์œ ์˜ํ•œ ๋ณ€์ˆ˜์ด๋‹ค. ์ดํ›„ ๋‘ ๋ฒˆ์งธ ๋ณ€์ˆ˜ ์„ ํƒ์€ = S ( i X) S ( i X) i 1 โ‹ฏ p 1 i j ์—์„œ ํ†ต๊ณ„๋Ÿ‰ ์ค‘ ๊ฐ€์žฅ ์œ ์˜ํ•œ ๋ณ€์ˆ˜์ด๋‹ค. ์„ธ ๋ฒˆ์งธ ๋ณ€์ˆ˜ ์„ ํƒ์€ = S ( i X, k ) S ( i X, k ) i 1 โ‹ฏ p 1 i j k ์—์„œ ํ†ต๊ณ„๋Ÿ‰ ์ค‘ ๊ฐ€์žฅ ์œ ์˜ํ•œ ๋ณ€์ˆ˜์ด๋‹ค. ๋ณ€์ˆ˜ ์ œ๊ฑฐ๋Š” = S ( i X, 2 โ‹ฏ X ( ) โ‹ฏ X โˆ’ ) S ( 1 X, , p 1 ) i 1 โ‹ฏ p 1 X ( ) X ๋ณ€์ˆ˜ ์ œ์™ธ์ผ ๋•Œ ํ†ต๊ณ„๋Ÿ‰์—์„œ ๊ฐ€์žฅ ์œ ์˜ํ•˜์ง€ ์•Š์€ ๋ณ€์ˆ˜์ด๋‹ค. SPSS์—์„œ ๋ณ€์ˆ˜ ์„ ํƒํ•  ๋•Œ๋Š” ์œ ์˜์ˆ˜์ค€ 0.05, ์ œ๊ฑฐํ•  ๋•Œ๋Š” ์œ ์˜์ˆ˜์ค€ 0.1์ด ๊ธฐ๋ณธ๊ฐ’์ด๋‹ค. ๋‹น๋‡จ๋ณ‘ ์ž๋ฃŒ ๋‹ค์Œ์€ ๋‹ค์ค‘ ์„ ํ˜•ํšŒ๊ท€๋ถ„์„์— ์‚ฌ์šฉํ•  ์ž๋ฃŒ์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‹ค. **Data Set Characteristics:** :Number of Instances: 442 :Number of Attributes: First 10 columns are numeric predictive values :Target: Column 11 is a quantitative measure of disease progression one year after baseline :Attribute Information: - age age in years - sex - bmi body mass index - bp average blood pressure - s1 tc, T-Cells (a type of white blood cells) - s2 ldl, low-density lipoproteins - s3 hdl, high-density lipoproteins - s4 tch, thyroid stimulating hormone - s5 ltg, lamotrigine - s6 glu, blood sugar level ๋‹น๋‡จ๋ณ‘ ์ž๋ฃŒ๊ฐ€ ์žˆ๋Š” ์œ„์น˜ https://www4.stat.ncsu.edu/~boos/var.select/diabetes.tab.txt SPSS๋กœ ๋‹น๋‡จ๋ณ‘ ์ž๋ฃŒ์— ๋Œ€ํ•œ ๋‹ค์ค‘ํšŒ๊ท€๋ถ„์„ ๋‹น๋‡จ๋ณ‘ ์ž๋ฃŒ๋Š” ์œ„ ์‚ฌ์ดํŠธ์—์„œ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  SPSS ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ์—์„œ ์—ฐ๋‹ค. ํšŒ๊ท€๋ถ„์„ ์‹คํ–‰์€ ๋ถ„์„ ํšŒ๊ท€๋ถ„์„ ์„ ํ˜•ํšŒ๊ท€ ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•œ๋‹ค. ์ข…์†๋ณ€์ˆ˜์— ๋ณ€์ˆ˜ Y, ๋…๋ฆฝ๋ณ€์ˆ˜์— ๋‚˜๋จธ์ง€ ๋ณ€์ˆ˜๋ฅผ ๋ชจ๋‘ ์ž…๋ ฅํ•˜๊ณ , ๋ชจํ˜• ์„ ํƒ ๋ฐฉ๋ฒ•์€ ๋‹จ๊ณ„ ์„ ํƒ์„ ์„ ํƒํ•œ๋‹ค. ์„ ํ˜•ํšŒ๊ท€ ์ฐฝ์—์„œ ํ†ต๊ณ„๋Ÿ‰์„ ํด๋ฆญํ•˜๊ณ  ํ†ต๊ณ„๋Ÿ‰ ์ฐฝ์—์„œ ์ถœ๋ ฅํ•  ํ†ต๊ณ„๋Ÿ‰ ์„ ํƒ ์„ ํ˜•ํšŒ๊ท€ ์ฐฝ์—์„œ ์˜ต์…˜์„ ํด๋ฆญํ•˜๊ณ  ์˜ต์…˜ ์ฐฝ์—์„œ ๋ชจํ˜•์— ์ถ”๊ฐ€ํ•  ๋ณ€์ˆ˜ ์„ ํƒ ๊ธฐ์ค€ ์„ค์ • ๋ถ„์„ ๊ฒฐ๊ณผ ๋ชจํ˜• ์š”์•ฝ์—์„œ 2 ๊ณผ ์ˆ˜์ •๋œ 2 ๊ฐ’์ด ๋ณ€์ˆ˜๊ฐ€ ์ถ”๊ฐ€๋ ์ˆ˜๋ก ์ฆ๊ฐ€ํ•˜๋ฏ€๋กœ ๋ชจํ˜•์˜ ์„ค๋ช…๋ ฅ์ด ์ข‹๋‹ค. Durbin-Watson ํ†ต๊ณ„๋Ÿ‰์ด 2 ๊ทผ์ฒ˜์— ์žˆ์œผ๋ฉด ์˜ค์ฐจ๊ฐ€ ์„œ๋กœ ๋…๋ฆฝ์ธ๋ฐ, ํ†ต๊ณ„๋Ÿ‰์ด 2.043์ด๋ฏ€๋กœ ์˜ค์ฐจ์˜ ๋…๋ฆฝ์„ฑ์ด ๋ณด์žฅ๋œ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์—์„œ ์ตœ์ข… ๋ชจํ˜•์€ ๋ณ€์ˆ˜ 6๊ฐœ๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์„ ๋•Œ ์ตœ๊ณ  ์ ํ•ฉ ๋ชจํ˜•์œผ๋กœ ํŒ๋‹จํ•˜์˜€๋‹ค. VIF ๊ฐ’์ด 10 ์ด์ƒ์ธ ๊ฐ’์€ ์—†์–ด ๊ณต์„ ์„ฑ์ด ์—†๋‹ค๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์ง€๋งŒ S1 ๋ณ€์ˆ˜๊ฐ€ 8.8๋กœ ์•ฝ๊ฐ„ ๋†’๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๊ณต์„ ์„ฑ์„ ์•ฝ๊ฐ„ ์˜์‹ฌํ•ด ๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค. 3. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜• ์ƒ๋Œ€์œ„ํ—˜๋„์™€ ์Šน์‚ฐ๋น„ ํ‘œ 1์€ ์•„์Šคํ”ผ๋ฆฐ ๋ณต์šฉ ์—ฌ๋ถ€์™€ ์‹ฌ๊ทผ๊ฒฝ์ƒ‰ ๋ฐœ๋ณ‘ ์œ ๋ฌด ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด์—ˆ๋‹ค. ์ด ์กฐ์‚ฌ๋Š” 22,071๋ช… ๋‚จ์„ฑ์„ ์ž„์˜๋กœ ๋‘ ์ง‘๋‹จ์œผ๋กœ ๋‚˜๋ˆ„๊ณ  ์•„์Šคํ”ผ๋ฆฐ๊ณผ ์œ„์•ฝ์„ ๋ณต์šฉํ•œ ํ›„ 5๋…„ ๋™์•ˆ ์‹ฌ๊ทผ๊ฒฝ์ƒ‰ ๋ฐœ๋ณ‘ ์œ ๋ฌด๋ฅผ ๊ธฐ๋กํ•˜์˜€๋‹ค. ๋‘ ์ง‘๋‹จ์„ ํ™•๋ฅ ํ™”๋กœ ๋‚˜๋ˆ„๊ณ  ํ˜„์žฌ๋ถ€ํ„ฐ ๋ฏธ๋ž˜์˜ ์ผ์ • ๊ธฐ๊ฐ„๊นŒ์ง€ ์กฐ์‚ฌํ•˜๋Š” ์—ฐ๊ตฌ ๋ฐฉ๋ฒ•์„ ํ›„ํ–ฅ์  ์—ฐ๊ตฌ(prospective study)์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ง‘๋‹จ ์‹ฌ๊ทผ๊ฒฝ์ƒ‰ ํ•ฉ ๋ฐœ๋ณ‘ํ•จ ๋ฐœ๋ณ‘ ์•ˆ ํ•จ placebo 189 10,845 11,034 aspirin 104 10,933 11,037 ํ‘œ 1. ์•„์Šคํ”ผ๋ฆฐ๊ณผ ์‹ฌ๊ทผ๊ฒฝ์ƒ‰ ํ‘œ 1 ์ถœ์ฒ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. source: Priliminary Report: Findongs from the Aspirin Component of the Ongoing Physician' Health Study. N. Engl. J. Med., 318:262-264(1988). ํ‘œ 2๋Š” ๋ถ๋ถ€ ์ดํƒˆ๋ฆฌ์•„์˜ 30๊ฐœ ๊ด€์ƒ ๋™๋งฅ ์น˜๋ฃŒ์‹ค์—์„œ ์‹ฌ๊ทผ๊ฒฝ์ƒ‰์ด ์žˆ๋Š” ํ™˜์ž 262 ๋ช…๊ณผ ๋‹ค๋ฅธ ๊ธ‰์„ฑ ์งˆํ™˜์ด ์žˆ๋Š” ํ™˜์ž 519๋ช…์ด๋‹ค. ์ด ์กฐ์‚ฌ๋Š” 1983๋…„์—์„œ 1988๋…„๊นŒ์ง€ ๊ณผ๊ฑฐ ์ž๋ฃŒ๋ฅผ ๊ด€์ฐฐํ•˜์˜€๋‹ค. ์ด์™€ ๊ฐ™์ด ํ˜„์‹œ์ ์—์„œ ๊ณผ๊ฑฐ ์ž๋ฃŒ๋ฅผ ์กฐ์‚ฌํ•˜๋Š” ๊ฒƒ์„ ํ›„ํ–ฅ์  ์—ฐ๊ตฌ(retrospective study) ๋˜๋Š” ํŠน์ •ํ•œ ์งˆ๋ณ‘์ด ์žˆ๋Š” ํ™˜์ž๋ฅผ ๊ด€์ฐฐ์ž๊ฐ€ ๊ฒฐ์ •ํ•˜๊ธฐ์— ์‚ฌ๋ก€-๋Œ€์กฐ ์—ฐ๊ตฌ(case-control study)๋ผ๊ณ  ํ•œ๋‹ค. ์‹ฌ์žฅ์งˆํ™˜ ํก์—ฐ ๊ฒฝํ—˜ ์‹ฌ๊ทผ๊ฒฝ์ƒ‰ ๋Œ€์กฐ๊ตฐ ํ•ฉ ์žˆ์Œ 172 173 345 ์—†์Œ 90 346 436 ํ•ฉ 262 519 781 ํ‘œ 2. ํก์—ฐ๊ณผ ์‹ฌ๊ทผ๊ฒฝ์ƒ‰ ํ‘œ 2 ์ถœ์ฒ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. source: A Gramenzi et al., J. Epidemiol. and Commun. Health, 43: 214-217 (1989). ๋ณ€์ˆ˜์˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ๊ฐ ๋‘ ๊ฐœ ๋ฒ”์ฃผ๋กœ ๋‚˜๋ˆˆ ๊ฒฝ์šฐ ๋ถ„ํ• ํ‘œ ์ž๋ฃŒ์—์„œ ์—ฐ๊ด€์„ฑ์— ๋Œ€ํ•œ ์ธก๋„๋Š” ์ƒ๋Œ€์œ„ํ—˜๋ฅ (relative risk)๊ณผ ์Šน์‚ฐ๋น„(odds ratio)๊ฐ€ ์žˆ๋‹ค. ์ƒ๋Œ€์œ„ํ—˜๋„๋Š” ์‹คํ—˜ ๊ตฐ ์œ„ํ—˜๋ฅ  ๋Œ€์กฐ๊ตฐ ์œ„ํ—˜๋ฅ  RR ์‹ค ๊ตฐ ์œ„ ๋ฅ  ์กฐ ์œ„ ๋ฅ  ์ด๋‹ค. ํ‘œ 1์—์„œ ์•„์Šคํ”ผ๋ฆฐ์— ๋Œ€ํ•œ ์ƒ๋Œ€์œ„ํ—˜๋„๋Š” RR 189 11 034 104 11 037 1.82 ์ด๋‹ค. ์œ„์•ฝ์„ ๋ณต์šฉํ•œ ์‚ฌ๋žŒ์— ๋Œ€ํ•œ ์ƒ๋Œ€ ์œ„ํ—˜๋„๋Š” 1.82 ์ด๋‹ค. ์ฆ‰ ์ƒ๋Œ€์œ„ํ—˜๋„๋Š” ์‹ฌ๊ทผ๊ฒฝ์ƒ‰ ๋ฐœ๋ณ‘ ๋น„์œจ์€ ์•„์Šคํ”ผ๋ฆฐ์„ ๋ณต์šฉํ•˜์ง€ ์•Š๋Š” ์‚ฌ๋žŒ์ด ๋ณต์šฉํ•˜๋Š” ์‚ฌ๋žŒ๋ณด๋‹ค 82 ๋” ๋†’๋‹ค. ์ƒ๋Œ€์œ„ํ—˜๋ฅ ์€ ์ฝ”ํ˜ธํŠธ ์—ฐ๊ตฌ(cohort study)๋‚˜ ์‹คํ—˜ ๊ตฐ๊ณผ ๋Œ€์กฐ๊ตฐ์ด ์‚ฌ์ „์— ์ •์˜ํ•˜๊ณ  ๊ด€์ฐฐํ•˜๋Š” ๊ฒฝ์šฐ ์‹คํ—˜ ๊ฒฐ๊ณผ์—์„œ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ๋Œ€์œ„ํ—˜๋ฅ ์€ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ด๊ฐ€ ์‰ฝ์ง€๋งŒ, ์‚ฌ๋ก€-๋Œ€์กฐ ์—ฐ๊ตฌ(case-control study)์™€ ๊ฐ™์ด ๋Œ€์กฐ๊ตฐ๊ณผ ์‹คํ—˜ ๊ตฐ ์ˆ˜๊ฐ€ ์‹คํ—˜๊ฐ€์— ์˜ํ•ด ์ •ํ•ด์ง€๋ฉด ์ž„์˜์„ฑ์ด ์—†๊ฒŒ ๋˜์–ด ํ™•๋ฅ ์„ ๋…ผํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์ƒ๋Œ€์œ„ํ—˜๋ฅ ์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋Ÿฐ ๊ฒฝ์šฐ๋Š” ์Šน์‚ฐ๋น„๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์–ด๋–ค ํ™•๋ฅ ์— ๋Œ€ํ•œ ์Šน์‚ฐ(odds)์€ odds p โˆ’์ด๋‹ค. = 0.8 ์ด๋ฉด ์Šน์‚ฐ์€ odds 0.8 โˆ’ 0.8 4 ์ด๋‹ค. ์œ„ ์‹์—์„œ ํ™•๋ฅ ์€ ์Šน์‚ฐ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด = odds + odds ์ด๋‹ค. ์Šน์‚ฐ์ด ์ด๋ฉด ํ™•๋ฅ  ๋Š” = 1 4 0.8 ์ด๋‹ค. ์Šน์‚ฐ๋น„(odds ratio)๋Š” ์‹คํ—˜ ๊ตฐ ์Šน์‚ฐ ์™€ ๋Œ€์กฐ๊ตฐ ์Šน์‚ฐ ๋น„์œจ์ด๋‹ค. ร— ๋ถ„ํ• ํ‘œ์—์„œ ์ฒซ ํ–‰์˜ ์„ฑ๊ณต ํ™•๋ฅ ์„ 1 ์ด๋ฉด ์ฒซ ํ–‰ ์Šน์‚ฐ์€ odds = 1 โˆ’ 1 ์ด๊ณ , ๋‘ ๋ฒˆ์งธ ํ–‰์˜ ์„ฑ๊ณต ํ™•๋ฅ ์„ 2 ์ด๋ฉด ๋‘ ๋ฒˆ์งธ ํ–‰ ์Šน์‚ฐ์€ odds = 2 โˆ’ 2 ์ด๋‹ค. 1 2 3 0. ห™ ์ด๊ณ  2 0.8 ์ด๋ฉด ๊ฐ ์Šน์‚ฐ์€ odds = 0. ห™ 0. ห™ 2 odds = 0.8 0.2 4 ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์Šน์‚ฐ๋น„๋Š” = odds odds = 4 0.5 ์ด๋‹ค. ์Šน์‚ฐ๋น„์™€ ์ƒ๋Œ€์œ„ํ—˜๋„์˜ ๊ด€๊ณ„๋Š” ์Šน์‚ฐ๋น„ ์ƒ๋Œ€์œ„ํ—˜๋„ ์‚ฐ = 1 ( โˆ’ 1 ) 2 ( โˆ’ 2 ) ์ƒ ์œ„ ๋„ ( โˆ’ 2 โˆ’ 1 ) ์ด๋‹ค. 1 p ๋ชจ๋‘ ์ž‘์œผ๋ฉด ( โˆ’ 2 โˆ’ 1 ) ์ด 1์— ๊ฐ€๊น๊ฒŒ ๋˜๋ฏ€๋กœ ์Šน์‚ฐ๋น„์™€ ์ƒ๋Œ€์œ„ํ—˜๋„๋Š” ๋น„์Šทํ•œ ๊ฐ’์„ ๊ฐ™๋‹ค. ํ‘œ 1์—์„œ ํ”Œ๋ž˜ ์‹œ๋ณด ์‹ฌ๊ทผ๊ฒฝ์ƒ‰ ๋ฐœ๋ณ‘๋ฅ ๊ณผ ์•„์Šคํ”ผ๋ฆฐ ์‹ฌ๊ทผ๊ฒฝ์ƒ‰ ๋ฐœ๋ณ‘๋ฅ ์€ ๋ชจ๋‘ 0์— ๊ฐ€๊น๋‹ค. ์ด ๊ฒฝ์šฐ ์Šน์‚ฐ๋น„์™€ ์ƒ๋Œ€์œ„ํ—˜๋„๋Š” ๋น„์Šทํ•œ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ํ‘œ 1์—์„œ ๊ณ„์‚ฐํ•˜๋ฉด ์Šน์‚ฐ๋น„๋Š” 1.83 ์ด๊ณ  ์ƒ๋Œ€์œ„ํ—˜๋„๋Š” 1.82 ์ด๋‹ค. ์‹คํ—˜์ž๊ฐ€ ํ‘œ๋ณธ์„ ๋ฏธ๋ฆฌ ์ •ํ•ด์„œ ํ™•๋ฅ ์„ ๊ตฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, ์ฆ‰ ์‚ฌ๋ก€-์กฐ์‚ฌ์—ฐ๊ตฌ๋Š” ์ƒ๋Œ€์œ„ํ—˜๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค. ๋‘ ๋ณ€์ˆ˜, ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์—์„œ ํ•œ ๋ฒ”์ฃผ์˜ ๊ฐ’์— ๋Œ€ํ•˜์—ฌ ๋‹ค๋ฅธ ๋ฒ”์ฃผ์˜ ๊ฐ’์ด ์ž‘์„ ๋•Œ, ์ฆ‰ ๋ณ€์ˆ˜์— ๋Œ€ํ•˜์—ฌ ํ™•๋ฅ  ( = ) ์ด ์ž‘์„ ๋•Œ ์Šน์‚ฐ๋น„์™€ ์ƒ๋Œ€์œ„ํ—˜๋„๋Š” ๋น„์Šทํ•œ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ€ ์ฃผ์–ด์ง„ ๊ฒฝ์šฐ์˜ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์„ ์ถ”์ •ํ•˜๋ฉด ( = | ) ๊ฐ€ ์ž‘์„ ๊ฐ’์„ ๊ฐ€์งˆ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ๊ฒฝ์šฐ ํ‘œ๋ณธ ์Šน์‚ฐ๋น„๋Š” ์ƒ๋Œ€์œ„ํ—˜๋„์™€ ๋น„์Šทํ•œ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. ํ‘œ 2์—์„œ ํก์—ฐ ์œ ๋ฌด์— ๋”ฐ๋ฅธ ์Šน์‚ฐ๋น„๋Š” 172 346 90 173 3.82 ์ด๋‹ค. ํก์—ฐ ๊ฒฝํ—˜์„ ํก์—ฐ๋น„ ํก์—ฐ = { ( ์—ฐ ) 1 ( ํก ) } , ์‹ฌ์žฅ์งˆํ™˜์„ ๊ธฐํƒ€ ์งˆํ™˜ ์‹ฌ๊ทผ๊ฒฝ์ƒ‰ = { ( ํƒ€ ํ™˜ ) 1 ( ๊ทผ ์ƒ‰ ) } ๋ผ๊ณ  ํ•˜๋ฉด, ( = | = ) P ( = | = ) ๋ชจ๋‘ ์ž‘์€ ๊ฐ’์ด๋ฏ€๋กœ ์ƒ๋Œ€์œ„ํ—˜๋„๋ฅผ ๋Œ€๋žต 3.82 ๋กœ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•ด์„ํ•˜๋ฉด ํก์—ฐ ๊ฒฝํ—˜์ด ์žˆ๋Š” ์‚ฌ๋žŒ์ด ํก์—ฐ ๊ฒฝํ—˜์ด ์—†๋Š” ์‚ฌ๋žŒ๋ณด๋‹ค ์‹ฌ๊ทผ๊ฒฝ์ƒ‰์— ๊ฑธ๋ฆด ํ™•๋ฅ ์ด ์•ฝ ๋ฐฐ ๋†’๋‹ค๊ณ  ์ถ”์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์ง„ํ˜• ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜• ๋ฐ˜์‘ ๋ณ€์ˆ˜ โˆˆ { , } ์ธ ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰์ธ ๊ฒฝ์šฐ, ์„ค๋ช…๋ณ€์ˆ˜์™€ ํ•จ๊ป˜ ํ‘œํ˜„ํ•˜๋ฉด | = โˆผ ( , ( ) ) ๋ถ„ํฌ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ( ) ์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋Š” logit ( ( ) ) log ( ( ) โˆ’ ( ) ) ( ; ) ฮฒ + 1 1 โ‹ฏ ฮฒ x ฮท ( ; ) logit ( ( ) ) ( ) e ( ; ) + ฮท ( ; ) ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. ๋กœ์ง“(logit) ํ•จ์ˆ˜์—์„œ ๊ณ„์‚ฐํ•œ ๋ถ„ํฌํ•จ์ˆ˜ ( ) ๋ฅผ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•(logistic regression model)์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•์— ๋Œ€ํ•œ ์šฐ๋„ ํ•จ์ˆ˜(likelihood function)๋Š” ๋ฐ˜์‘ ๋ณ€์ˆ˜ ๊ฐ€ ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰์ด๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„๋‹จ ๋‹ค. ( ) โˆ = n ( i ) i ( โˆ’ ( i ) ) โˆ’ i ๋กœ๊ทธ-์šฐ๋„ ํ•จ์ˆ˜๋Š” ( ) โˆ‘ = n [ i log p ( i ) ( โˆ’ i ) log ( โˆ’ ( i ) ) ] โˆ‘ = n [ i log p ( i ) โˆ’ ( i ) log ( โˆ’ ( i ) ) ] โˆ‘ = n [ i ( i ) log ( + ฮท ( i ) ) ] ์ด๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์€ ๋กœ๊ทธ-์šฐ๋„ํ•จ์ˆ˜์—์„œ ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•์—์„œ ํšŒ๊ท€๊ณ„์ˆ˜๋Š” ๋กœ๊ทธ-์šฐ๋„ ํ•จ์ˆ˜๊ฐ€ ์ตœ๋Œ€๊ฐ€ ๋  ๋•Œ ๊ฐ’์ด๋‹ค. ํ•จ์ˆ˜๊ฐ€ ์œ„๋กœ ๋ณผ๋ก์ธ ๊ฒฝ์šฐ ์ตœ๋Œ“๊ฐ’์€ 1์ฐจ ๋ฏธ๋ถ„ํ•œ ํ•จ์ˆ˜๊ฐ€ 0์ผ ๋•Œ ํ•ด์ด๋‹ค. ํšŒ๊ท€ ๊ณ„์ˆ˜๋Š” ํ…Œ์ผ๋Ÿฌ๊ธ‰์ˆ˜ ์ „๊ฐœ(Taylor series expansion)๋กœ ๊ตฌํ•œ๋‹ค. ํ…Œ์ผ๋Ÿฌ๊ธ‰์ˆ˜์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์„ ์–ป๋Š”๋‹ค. = ( ^ ) f ( 0 ) f ( 0 ) ( ^ x) ^ x = f ( 0 ) โ€ฒ ( 0 ) ^ x โˆ’ ( 0 ) โ€ฒ ( 0 ) ํ•ด๋Š” ์œ„์˜ ์‹์—์„œ ๋ฐ˜๋ณตํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๊ณ  ์ด์ „ ๊ฐ’๊ณผ ์ƒˆ ๊ฐ’์˜ ์ฐจ์ด = x โˆ’ 0 ๊ฐ€ < 10 3 ์ผ ๋•Œ ์ƒˆ ๊ฐ’ ^ ์ด๋‹ค. ์ด๋ ‡๊ฒŒ ๋น„์„ ํ˜• ๋ฐฉ์ •์‹์˜ ํ•ด๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์„ Newton-Raphson ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‘ ํ•จ์ˆ˜ 1 ( ) sin ( ) f ( ) x ํ•ด๋ฅผ Newton_Raphson ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ•˜์ž. ๋‘ ํ•จ์ˆ˜์˜ ํ•ด๋Š” ( ) sin ( ) x ํ•ด์™€ ๊ฐ™๋‹ค. ( ) ์˜ ๋„ํ•จ์ˆ˜ โ€ฒ ( ) cos ( ) 2์ด๋‹ค. ํ•ด๋Š” ๋‘ ํ•จ์ˆ˜๊ฐ€ ๊ต์ฐจํ•˜๋Š” ๊ณณ์ด๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ ๋‘ ํ•จ์ˆ˜๊ฐ€ ๊ต์ฐจํ•˜๋Š” ๋‘ ์ ์„ ํ‘œ์‹œํ•˜์˜€๋‹ค. ํ•œ ์ ์€ ์›์ ์ด๋ฏ€๋กœ ํ•ด๋Š” ๊ณผ 0.876 ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ํ•ด๋Š” ์—‘์…€์—์„œ ๋ชฉํ‘ฏ๊ฐ’ ์ฐพ๊ธฐ๋กœ ๊ณ„์‚ฐํ•˜์˜€๋‹ค. ํ•จ์ˆ˜์˜ ํ•ด๋Š” ์•„๋ž˜ ์‹์„ ๋ฐ˜๋ณตํ•˜์—ฌ ๊ตฌํ•œ๋‹ค. ^ x โˆ’ sin ( 0 ) x 2 cos ( 0 ) 2 0 0 2 ๋ฅผ ์‹œ์ž‘ ๊ฐ’์œผ๋กœ ๋ฐ˜๋ณตํ•˜์—ฌ ๊ฐ’์„ ๊ตฌํ•˜๋ฉด ์ด์ „ ๊ฐ’๊ณผ ์ƒˆ ๊ฐ’์˜ ์ฐจ์ด = x new x old ๊ฐ€ < 10 3 x = 0.877 ๊ฐ€ ํ•จ์ˆ˜ ( ) ์˜ ํ•ด๋‹ค. 1 x โˆ’ sin ( 0 ) x 2 cos ( 0 ) 2 0 1.300 2.000 sin ( 2.000 ) 2.000 cos ( 2.000 ) 2 2 x โˆ’ sin ( 1 ) x 2 cos ( 1 ) 2 1 0.989 1.300 sin ( 1.300 ) 1.300 cos ( 1.300 ) 2 3 x โˆ’ sin ( 2 ) x 2 cos ( 2 ) 2 2 0.889 0.989 sin ( 0.989 ) 0.989 cos ( 0.989 ) 2 4 x โˆ’ sin ( 3 ) x 2 cos ( 3 ) 2 3 0.877 0.889 sin ( 0.889 ) 0.889 cos ( 0.889 ) 2 5 x โˆ’ sin ( 4 ) x 2 cos ( 4 ) 2 4 0.877 0.877 sin ( 0.877 ) 0.877 cos ( 0.877 ) 2 ๋‹ค์Œ ๊ทธ๋ฆผ์€ ํ•จ์ˆ˜ ( ) sin ( ) x์˜ ํ•ด๋ฅผ ์ดˆ๊นƒ๊ฐ’ 0 2 ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ Newton-Raphson ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ•˜๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค€๋‹ค. ํšŒ๊ท€ ๋ชจํ˜•์—์„œ 1์ฐจ ๋ฏธ๋ถ„์ธ ์Šค์ฝ”์–ด(score) ํ•จ์ˆ˜ ( ^ ) ์™€ 2์ฐจ ๋ฏธ๋ถ„์ธ ํ—ค์‹œ ์•ˆ(Hessian) ํ•จ์ˆ˜ ( ^ ) ๋กœ ํšŒ๊ท€๊ณ„์ˆ˜ ์ถ”์ •์น˜ ^ ์„ ๊ตฌํ•˜๋ฉด = ( ^ ) S ( 0 ) H ( 0 ) ( ^ ฮฒ) ( 0 ) ( ^ ฮฒ) โˆ’ ( 0 ) ^ ฮฒ โˆ’ ( 0 ) 1 ( 0 ) ์ด๋‹ค. ^ ๊ณ„์‚ฐ์€ ๋กœ๊ทธ-์šฐ๋„ ํ•จ์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ^ ฮฒ โˆ’ ( 0 ) 1 ( 0 ) ๋ฅผ ๊ตฌํ•œ๋‹ค. ๋กœ๊ทธ-์šฐ๋„ ํ•จ์ˆ˜์— ^ ฮฒ ๊ฐ’์„ ์ ์šฉํ•œ ( ^ ) l ( 0 ) ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ( ^ ) l ( 0 ) ์ด๊ณ  l ( ^ ) l ( 0 ) < 10 6 ์ด๋ฉด ^ ๋ฅผ ํ•ด๋กœ ์ •ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 0 ฮฒ์œผ๋กœ ๊ฐ’์„ ๋ฐ”๊พธ๊ณ  2 ๋ฒˆ์œผ๋กœ ๊ฐ€์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.์— ๋Œ€ํ•œ ๋กœ๊ทธ-์šฐ๋„ ํ•จ์ˆ˜์˜ 1์ฐจ ๋ฏธ๋ถ„์ธ ์Šค์ฝ”์–ด(score) ํ•จ์ˆ˜๋Š” ( ) = โˆ‚ j ( ) โˆ‘ = n [ i i โˆ’ ฮท ( ) + ฮท ( ) i ] โˆ‘ = n [ i p ( i ) ] i ์ด๊ณ ์— ๋Œ€ํ•œ ๋กœ๊ทธ-์šฐ๋„ ํ•จ์ˆ˜ 2์ฐจ ๋ฏธ๋ถ„์ธ ํ—ค์‹œ ์•ˆ(Hessian) ํ•จ์ˆ˜๋Š” ( ) k โˆ‚ โˆ‚ j ฮฒ l ( ) โˆ’ i 1 [ ( i ) ( โˆ’ ( i ) ) i x k ] โˆ’ i 1 V x j i์ด๋‹ค. ์œ„ ์‹์—์„œ i ๋Š” ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰ ๋ถ„์‚ฐ์œผ๋กœ ๋‹ค์Œ ์‹์—์„œ ๊ตฌํ•œ๋‹ค. โˆ‚ k ( i ) โˆ‚ ฮฒ ( ฮท ( i ) + ฮท ( i ) ) e ( i ) ( + ฮท ( i ) ) i โˆ’ ฮท ( i ) ฮท ( i ) ( + ฮท ( i ) ) = i 1 p ( i ) ( โˆ’ ( i ) ) variance i = i 1 V x k ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜• ์„ฑ๋Šฅ ํ‰๊ฐ€ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„์—์„œ ๋ชจํ˜•์˜ ์„ฑ๋Šฅ ํ‰๊ฐ€๋Š” ํ˜ผ๋™ํ–‰๋ ฌ(confusion matrix)์„ ์ž‘์„ฑํ•˜์—ฌ ํ™œ์šฉํ•œ๋‹ค. ์‹ค์ œ ์ƒํƒœ ์‹ค์ œ ์–‘์„ฑ ์‹ค์ œ ์Œ์„ฑ ์œ ๋ณ‘๋ฅ (prevalence) TP FN + ์ •๋ฐ€๋„(accuracy) TP TN + ์˜ˆ ์ƒ ํƒœ ์–‘์„ฑ ์ฐธ์ธ ์–‘์„ฑ(TP) ๊ฑฐ์ง“ ์–‘์„ฑ(FP) type I error ์–‘์„ฑ ์˜ˆ์ธก๋„(PPV) TP TP FP ๊ฑฐ์ง“์–‘์„ฑ์˜ˆ์ธก๋„(FDR) FP TP FP ์Œ์„ฑ ๊ฑฐ์ง“ ์Œ์„ฑ(FN) type II error ์ฐธ์ธ ์Œ์„ฑ(TN) ๊ฑฐ์ง“ ์Œ์„ฑ ์˜ˆ์ธก๋„(FOR) FN FN TN ์Œ์„ฑ ์˜ˆ์ธก๋„(NPV) TN FN TN ๋ฏผ๊ฐ๋„(TPR) TP TP FN ๊ฑฐ์ง“ ์Œ์„ฑ๋ฅ (FPR) FP FP TN ์–‘์„ฑ ๊ฐ€๋Šฅ์„ฑ ์Šน์‚ฐ(LR+) TPR FPR ์ง„๋‹จ ์Šน์‚ฐ๋น„ LR LR F1 ์ ์ˆ˜ 2 PPV TPR PPV TPR ๊ฑฐ์ง“์–‘์„ฑ๋ฅ (FNR) FN TP FN ํŠน์ด๋„(TNR) TN FP TN ์Œ์„ฑ ๊ฐ€๋Šฅ์„ฑ ์Šน์‚ฐ(LR) FNR TNR ํ˜ผ๋™ํ–‰๋ ฌ(confusion matrix) ํ˜ผ๋™ํ–‰๋ ฌ์— ์‚ฌ์šฉํ•œ ์šฉ์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. condition positive(P) : ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์—์„œ ์‹ค์ œ๋กœ ์–‘์„ฑ condition negative(N) : ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์—์„œ ์‹ค์ œ๋กœ ์Œ์„ฑ ture positive(TP) : ์‹ค์ œ ์–‘์„ฑ์ด๊ณ  ์˜ˆ์ธก๋„ ์–‘์„ฑ ture negative(TN) : ์‹ค์ œ ์Œ์„ฑ์ด๊ณ  ์˜ˆ์ธก๋„ ์Œ์„ฑ false positive(FP) : ์‹ค์ œ ์Œ์„ฑ์ด๊ณ  ์˜ˆ์ธก์€ ์–‘์„ฑ false negative(FN) : ์‹ค์ œ ์–‘์„ฑ์ด๊ณ  ์˜ˆ์ธก์€ ์Œ์„ฑ sensitivity, recall, hit rate or true positive rate(TPR) : ๋ฏผ๊ฐ๋„, ์žฌํ˜„์„ฑ, ์ฐธ์ธ ์–‘์„ฑ ๋น„์œจ TPR TP = TP TP FN 1 FNR specificity, selectivity or true negative rate (TNR) : ํŠน์ด๋„, ์ฐธ์ธ ์Œ์„ฑ ๋น„์œจ TNR TN = TN TN FP 1 FNR precision or positive predictive value (PPV) : ์–‘์„ฑ ์˜ˆ์ธก๋„ PPV TP TP FP 1 FDR negative predictive value (NPV) : ์Œ์„ฑ ์˜ˆ์ธก๋„ NPV FN FN TN 1 FOR \item miss rate or false negative rate (FNR) : ๊ฑฐ์ง“ ์Œ์„ฑ ๋น„์œจ FNR FN = FN TP FN 1 TPR fall-out or false positive rate (FPR) : ๊ฑฐ์ง“ ์–‘์„ฑ ๋น„์œจ FPR FP = FP FP TN 1 TNR false discovery rate (FDR) FDR FP FP TP 1 PPV false omission rate (FOR) FOR FN FN TN 1 NPV Threat score (TS) or Critical Success Index (CSI) TS TP TP FN FP accuracy (ACC) : ์ •ํ™•๋„ ACC TP TN + = TP TN TP FN FP TN F1 score : F1 ์ ์ˆ˜ 1 2 PPV TPR PPV TPR 2 TP TP FP FN Matthews correlation coefficient (MCC) MCC TP TN FP FN ( TP FP ) ( TP FN ) ( TN FP ) ( TN FN ) Informedness or Bookmaker Informedness (BM) BM TPR TNR 1 Markedness (MK) MK PPV NPV 1 ํ˜ผ๋™ํ–‰๋ ฌ ์˜ˆ ์‹ค์ œ ์ƒํƒœ(๋‚ด์‹œ๊ฒฝ์œผ๋กœ ๋Œ€์žฅ์•” ํŒ์ •) ์‹ค์ œ ์–‘์„ฑ(P) 30 ์‹ค์ œ ์Œ์„ฑ(N) , 000 ์œ ๋ณ‘๋ฅ (prevalence) 1.5 = TP FN + = 20 10 30 2 000 ์ •๋ฐ€๋„(accuracy) 90.6 = TP TN + = 20 1 820 30 2 000 ๋ณ€ ํ˜ˆ ์‚ฌ ์–‘์„ฑ ํŒ์ • ์ฐธ์ธ ์–‘์„ฑ(TP) 20 ๊ฑฐ์ง“ ์–‘์„ฑ(FP) 180 ์–‘์„ฑ ์˜ˆ์ธก๋„(PPV) 10.0 = TP TP FP 20 20 180 ๊ฑฐ์ง“์–‘์„ฑ์˜ˆ์ธก๋„(FDR) 90.0 = FP TP FP 180 20 180 ์Œ์„ฑ ํŒ์ • ๊ฑฐ์ง“ ์Œ์„ฑ(FN) 10 ์ฐธ์ธ ์Œ์„ฑ(TN) , 820 ๊ฑฐ์ง“ ์Œ์„ฑ ์˜ˆ์ธก๋„(FOR) 0.5 = FN FN TN 10 10 1 820 ์Œ์„ฑ ์˜ˆ์ธก๋„(NPV) 99.5 = TN FN TN 1 820 10 1 820 ๋ฏผ๊ฐ๋„(TPR) 66.7 = TP TP FN 20 20 10 ๊ฑฐ์ง“ ์Œ์„ฑ๋ฅ (FPR) 9.0 = FP FP TN 180 180 1 820 ์–‘์„ฑ ๊ฐ€๋Šฅ์„ฑ ์Šน์‚ฐ(LR+) TPR FPR 0.667 0.090 7.41 ์ง„๋‹จ ์Šน์‚ฐ๋น„ LR LR = 7.41 0.37 20.2 F1 ์ ์ˆ˜ 2 PPV TPR PPV TPR 2 0.100 0.667 0.100 0.667 0.17 ๊ฑฐ์ง“์–‘์„ฑ๋ฅ (FNR) 33.3 = FN TP FN 10 10 20 ํŠน์ด๋„(TNR) 91.0 = TN FP TN 1 820 180 1 820 ์Œ์„ฑ ๊ฐ€๋Šฅ์„ฑ ์Šน์‚ฐ(LR) FNR TNR 0.333 0.910 0.37 ํ˜ผ๋™ํ–‰๋ ฌ(confusion matrix) ์˜ˆ Python ์‹ค์Šต Python ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์€ ์บ๊ธ€ ์‚ฌ์ดํŠธ์—์„œ ๊ตฌํ–ˆ๊ณ , titanic ํด๋”์— ์ €์žฅํ•œ๋‹ค. ์บ๊ธ€ ์œ„์น˜๋Š” ์—ฌ๊ธฐ๋ฅผ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น ์‚ฌ์ดํŠธ๋กœ ์ด๋™ํ•œ๋‹ค. import warnings import numpy as np import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.linear_model import LogisticRegression from sklearn.metrics import roc_auc_score, accuracy_score, precision_score, recall_score, confusion_matrix, f1_score, confusion_matrix, precision_recall_curve from sklearn.model_selection import train_test_split warnings.filterwarnings(action='ignore') pd.set_option('display.width',500) X_train = pd.read_csv('titanic/train.csv') X_test = pd.read_csv('titanic/test.csv') X_train['Age'].fillna(X_train['Age'].mean(),inplace=True) X_train['Cabin'].fillna('N',inplace=True) X_train['Embarked'].fillna('N',inplace=True) X_train['Cabin'] = X_train['Cabin'].str[0] X_train.drop(['PassengerId','Name','Ticket'],axis=1, inplace=True) print(X_train['Survived'].value_counts()) def data_preprocessing(enc, df): for i in df.columns: if df[i].dtype == 'object': df[i] = enc.fit_transform(df[i]) return df enc = LabelEncoder() X = data_preprocessing(enc, X_train) Y = X['Survived'] X = X.drop('Survived',axis=1) X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.4, random_state=11) X=X_train T=X_test model = LogisticRegression() model.fit(X_train, y_train) print(X_train.columns) coef = model.coef_ print('ํšŒ๊ท€๊ณ„์ˆ˜ \n',coef) pred = model.predict_proba(T)[:,1] pred2 = model.predict(T) roc_auc = roc_auc_score(y_test, pred) print('roc : {0:.4f}'.format(roc_auc)) acc = accuracy_score(y_test, pred2) print('์ •ํ™•๋„ : {0:.4f}'.format(acc)) precision = precision_score(y_test, pred2) print('์ •๋ฐ€๋„ : {0:.4f}'.format(precision)) recall = recall_score(y_test, pred2) print('์žฌํ˜„์œจ : {0:.4f}'.format(recall)) f1 = f1_score(y_test, pred2) print('f1 : {0:.4f}'.format(f1)) confusion = confusion_matrix(y_test, pred2) print('ํ˜ผ๋™ํ–‰๋ ฌ ๋ฐฑ๋ถ„์œจ \n',confusion/confusion.sum()) print('ํ˜ผ๋™ํ–‰๋ ฌ \n',confusion) ๊ฒฐ๊ณผ๋Š” ์ฃฝ์€ ์‚ฌ๋žŒ์ด 549๋ช…, ์ƒ์กด ์‚ฌ๋žŒ์ด 342๋ช…์ด๋‹ค. ๊ทธ๋‹ค์Œ ์ถœ๋ ฅ์€ ๋…๋ฆฝ๋ณ€์ˆ˜๋“ค์ด๋‹ค. ํšŒ๊ท€๊ณ„์ˆ˜๋Š” Pclass, Sex, ์ˆœ์ด๋‹ค. ํ˜ผ๋™ํ–‰๋ ฌ์€ ์ „์ฒด ์ž๋ฃŒ ์ค‘์—์„œ 20% ์ž๋ฃŒ๋ฅผ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•˜์˜€๋‹ค. 0 549 1 342 Name: Survived, dtype: int64 Index(['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Cabin', 'Embarked'], dtype='object') ํšŒ๊ท€๊ณ„์ˆ˜ [[-9.26702177e-01 -2.88072643e+00 -3.79510684e-02 -2.98038720e-01 -9.14543335e-02 6.20201004e-04 -1.05251360e-01 -5.37820987e-02]] roc : 0.8487 ์ •ํ™•๋„ : 0.7983 ์ •๋ฐ€๋„ : 0.7479 ์žฌํ˜„์œจ : 0.6794 f1 : 0.7120 ํ˜ผ๋™ํ–‰๋ ฌ ๋ฐฑ๋ถ„์œจ [[0.54901961 0.08403361] [0.11764706 0.24929972]] ํ˜ผ๋™ํ–‰๋ ฌ [[196 30] [ 42 89]] SPSS ์‹ค์Šต ์˜ˆ์ œ ํŒŒ์ผ์€ ์บ๊ธ€์—์„œ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ๋ถ„์„์— ํ•„์š” ์—†๋Š” ๋ณ€์ˆ˜๋Š” ์‚ญ์ œํ•˜๊ณ  ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋Š” ์ˆ˜์ •ํ•˜์˜€๋‹ค. ์ˆ˜์ •ํ•œ ํŒŒ์ผ์€ ์—ฌ๊ธฐ์—์„œ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ๋ˆ„๋ฅด๊ณ  titanic_train์„ ํด๋ฆญํ•˜์—ฌ ๋‹ค์šด๋กœ๋“œํ•œ๋‹ค. Age ๋ณ€์ˆ˜์—์„œ ๊ฒฐ์ธก์น˜๋Š” ํ‰๊ท ์œผ๋กœ ๋ณ€๊ฒฝํ•˜์˜€๋‹ค. Cabin ๋ณ€์ˆ˜์—์„œ ๊ฒฐ์ธก์น˜๋Š” 'N' ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๊ฐ’์—์„œ ํ•œ ๋ฌธ์ž๋งŒ ๊ฐ€์ ธ์™”๋‹ค. Embarked ๋ณ€์ˆ˜์—์„œ ๊ฒฐ์ธก์น˜๋Š” 'N' ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•˜์˜€๋‹ค. PassengerId, Name, Ticket ๋ณ€์ˆ˜๋Š” ํ•„์š” ์—†๋‹ค๊ณ  ํŒ๋‹จํ•˜์—ฌ ์ œ๊ฑฐํ•˜์˜€๋‹ค. โ–ผ ํƒ€์ดํƒ€๋‹‰ ์ž๋ฃŒ๋ฅผ SPSS๋กœ ์ฝ์€ ํ™”๋ฉด์ด๋‹ค. โ–ผ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜• ์‹คํ–‰์€ ๋ถ„์„ ํšŒ๊ท€๋ถ„์„ ์ด๋ถ„ํ˜• ๋กœ์ง€์Šคํ‹ฑ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. โ–ผ ์ข…์†๋ณ€์ˆ˜์— Survived, ๋ธ”๋ก์— ๋‚˜๋จธ์ง€ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฒ”์ฃผํ˜• ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค. โ–ผ ๋ฒ”์ฃผํ˜• ๊ณต๋ณ€๋Ÿ‰์— ํ™”๋ฉด์— ๋ณด์ด๋Š” ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. โ–ผ ๋ธ”๋ก ํ™”๋ฉด์— ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋กœ ์ฒ˜๋ฆฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•œ๋‹ค. ์ €์žฅ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค. โ–ผ ์„ ํƒํ•œ ํ™•๋ฅ , ์†Œ์† ์ง‘๋‹จ์ด SPSS ์‹œํŠธ์— ์ถ”๊ฐ€๋œ๋‹ค. ํ™•๋ฅ ์€ 0์—์„œ 1์‚ฌ์ด ๊ฐ’์ด๋‹ค. ์†Œ์† ์ง‘๋‹จ์€ ํ™•๋ฅ  ๊ฐ’์ด 0.5๋ณด๋‹ค ํฌ๋ฉด 1, ์ž‘์œผ๋ฉด 0์œผ๋กœ ํŒ๋‹จํ•œ๋‹ค. โ–ผ ์ •๋ถ„๋ฅ˜ ๊ฐ’์„ ๋ณด๋ ค๋ฉด ๋ถ„๋ฅ˜ ๋„ํ‘œ๋ฅผ ์„ ํƒํ•œ๋‹ค. โ–ผ ๋ณ€์ˆ˜ ์„ ํƒ ๋ฐฉ๋ฒ•์€ ์•ž์œผ๋กœ(forward method)์ด๋‹ค. โ–ผ SPSS ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ์— ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์ด ๋ณด์ธ๋‹ค. PRE_1 ๋ณ€์ˆซ๊ฐ’์€ ํ™•๋ฅ ์ด๊ณ , PGR_1 ๋ณ€์ˆซ๊ฐ’์€ ์†Œ์† ์ง‘๋‹จ์œผ๋กœ ๋ถ„๋ฅ˜ํ•œ ๊ฐ’์ด๋‹ค. ์•„๋ž˜ ํ™”๋ฉด์— ์‹ค์ œ ์‚ฌ๋ง์ž๋ฅผ ์ƒ์กด์ž๋กœ ์ž˜๋ชป ์˜ˆ์ธกํ•œ ๊ฐ’์ด ์žˆ๋‹ค. โ–ผ ์ตœ์ข… ๋‹จ๊ณ„์—์„œ ๋ถ„๋ฅ˜ ํ‘œ์— ์ •ํ™•๋„๋Š” 82.2%์ด๋‹ค. ์œ„์˜ python์—์„œ ๊ฐ’ 79.83% ๋ณด๋‹ค ํฐ ๊ฐ’์ด๋‹ค. SPSS๋Š” ๋ชจํ˜•์„ ์„ธ์šด ์ž๋ฃŒ์™€ ๊ฒ€์ฆํ•œ ์ž๋ฃŒ๊ฐ€ ๋™์ผํ•˜์ง€๋งŒ python์€ ๋ชจํ˜•์„ ์„ธ์šด ์ž๋ฃŒ์™€ ๊ฒ€์ฆํ•œ ์ž๋ฃŒ๊ฐ€ ๋‹ค๋ฅด๋‹ค. ๋”ฐ๋ผ์„œ SPSS ๊ฒฐ๊ณผ๋Š” ๊ณผ์ ํ•ฉ์ด ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. โ–ผ ์ตœ์ข… ๋‹จ๊ณ„์—์„œ ์„ ํƒํ•œ ๋ณ€์ˆ˜๋“ค์ด๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•์—์„œ ์„ ํ˜•๊ฒฐํ•ฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ( | ) โˆ’ 41.461 1.611 I Pclass 1 0.993 I Pclass 2 + 19.808 ๋”ฐ๋ผ์„œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ์ถ”์ •์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. i e ( i ฮฒ ) ฮท ( i ฮฒ ) 1 ์ด ์‹์— ๊ฐ’์„ ์ž…๋ ฅํ•œ ๊ฒฐ๊ณผ๊ฐ€ ํ™•๋ฅ ์ด๊ณ  0.5 ๋ณด๋‹ค ํฌ๋ฉด 1, ์ž‘์œผ๋ฉด 0์œผ๋กœ ํŒ๋ณ„ํ•œ๋‹ค. โ–ผ ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์ด ์žˆ๋Š” ์œ„์น˜๋ฅผ ๊ทธ๋ž˜ํ”„์— ํ‘œํ˜„ํ•˜์˜€๋‹ค. SPSS ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ํ˜ผ๋™ํ–‰๋ ฌ ์‹ค์ œ ์ƒํƒœ ์‹ค์ œ ์ƒ์กด(P) 342 ์‹ค์ œ ์‚ฌ๋ง(N) 549 ์ƒ์กด์œจ 38.4 = TP FN + = 257 85 342 549 ์ •๋ฐ€๋„(accuracy) 82.2 = TP TN + = 257 475 342 549 ์กด ๋ง ์ • ์ƒ์กด ํŒ์ • ์ฐธ์ธ ์ƒ์กด(TP) 257 ๊ฑฐ์ง“ ์ƒ์กด(FP) 74 ์ƒ์กด ์˜ˆ์ธก๋„(PPV) 77.6 = TP TP FP 257 257 74 ๊ฑฐ์ง“ ์ƒ์กด ์˜ˆ์ธก๋„(FDR) 22.4 = FP TP FP 74 257 74 ์‚ฌ๋ง ํŒ์ • ๊ฑฐ์ง“ ์‚ฌ๋ง(FN) 85 ์ฐธ์ธ ์‚ฌ๋ง(TN) 475 ๊ฑฐ์ง“ ์‚ฌ๋ง ์˜ˆ์ธก๋„(FOR) 15.2 = FN FN TN 85 85 475 ์‚ฌ๋ง ์˜ˆ์ธก๋„(NPV) 84.8 = TN FN TN 475 85 475 ๋ฏผ๊ฐ๋„(TPR) 75.1 = TP TP FN 257 257 85 ๊ฑฐ์ง“ ์‚ฌ๋ง๋ฅ (FPR) 13.5 = FP FP TN 74 74 475 ์ƒ์กด ๊ฐ€๋Šฅ์„ฑ ์Šน์‚ฐ(LR+) TPR FPR 0.751 0.135 5.58 ์ง„๋‹จ ์Šน์‚ฐ๋น„ LR LR = 5.58 0.29 19.41 F1 ์ ์ˆ˜ 2 PPV TPR PPV TPR 2 0.751 0.776 0.751 0.776 0.76 ๊ฑฐ์ง“ ์ƒ์กด์œจ(FNR) 24.9 = FN TP FN 85 257 85 ํŠน์ด๋„(TNR) 86.5 = TN FP TN 475 74 475 ์‚ฌ๋ง ๊ฐ€๋Šฅ์„ฑ ์Šน์‚ฐ(LR) FNR TNR 0.249 0.865 0.29 ํƒ€์ดํƒ€๋‹‰ ์ž๋ฃŒ ํ˜ผ๋™ํ–‰๋ ฌ(confusion matrix) ๋‹ค์ง„ ํ˜• ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜• ๊ฐœ์˜ ์ˆ˜์ค€์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ช…๋ชฉํ˜• ๋ฐ˜์‘ ๋ณ€์ˆ˜ ์™€ ์ด๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๊ฐœ์˜ ์„ค๋ช…๋ณ€์ˆ˜ 1 x, , M ์„ ๊ฐ–๋Š” ๋‹คํ•ญ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•์„ ๊ณ ๋ คํ•˜์ž. ๋ฐ˜์‘ ๋ณ€์ˆ˜ ๊ฐ€ ํƒํ•˜๋Š” ๊ฐ’์˜ ์ง‘ํ•ฉ์€ = , ,๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์„ค๋ช…๋ณ€์ˆ˜ = ( 1 โ€ฆ x) ๊ฐ€ ํƒํ•˜๋Š” ๊ฐ’์˜ ์ง‘ํ•ฉ์€ M ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์ธ๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ๋กœ ํ•œ๋‹ค. ์ด๋•Œ ์„ค๋ช…๋ณ€์ˆ˜์˜ ํ™•๋ฅ ๋ณ€์ˆ˜๋ฅผ ๋ผ๊ณ  ํ•˜๋ฉด ๋ฐ˜์‘ ๋ณ€์ˆ˜์™€ ์„ค๋ช…๋ณ€์ˆ˜๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜ ์Œ ( , ) ๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. โˆˆ์ด๊ณ  โˆˆ์— ๋Œ€ํ•˜์—ฌ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ  ( = | = ) ๊ฐ€ ์–‘์ˆ˜ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ (1) ( | ) log P ( = | = ) ( = | = ) x X k K ๋ผ ํ•˜๋ฉด ( | ) 0 ์ด๋‹ค. ์‹ (1)์—<NAME>๋ฅผ ์ทจํ•˜๋ฉด ( = | = ) ( = | = ) e ( | ) ์ด๋ฏ€๋กœ, ์–‘๋ณ€์„ ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ ํ•ฉํ•˜๋ฉด k ( = | = ) 1 ( โˆซ pdf ( ) x 1 ) ์ด๋ฏ€๋กœ ( = | = ) 1 e ( | ) ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ฃผ์–ด์ง„ ์—์„œ ์˜ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์€ (2) ( = | = ; ) e ( | ) ฮธ ( | ) โ‹ฏ e ( | ) x X k K ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‹ (2)๋ฅผ ๋‹คํ•ญ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜• (polychotomous regression model, multinomial logistic regression model)์ด๋ผ ํ•˜๊ณ , ํŠน๋ณ„ํžˆ = ์ธ ๊ฒฝ์šฐ๋ฅผ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜• (logistic regression model)์ด๋ผ ๋ถ€๋ฅธ๋‹ค. ( | = ) (3) ( | = ; ) ฮฒ 0 ฮฒ 1 1 โ‹ฏ ฮฒ M M 1 k K 1 ๋กœ ์„ ํ˜• ๊ฐ€๋ฒ•๋ชจํ˜•์ด ๋œ๋‹ค. = ( 10 11 ฮฒ M 20 21 ฮฒ M K 1 0 K 1 1 ฮฒ โˆ’ , ) ๋ฒกํ„ฐ์˜ ์›์†Œ ์ˆ˜๋Š” ( โˆ’ ) ( + ) ์ด๋‹ค. ๋Š” ๋ฒ”์ฃผ ๊ฐœ์ˆ˜์ด๊ณ , ์€ ๋…๋ฆฝ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜์ด๋‹ค. ์‹ (2)์—์„œ (4) ( = | = ; ) e ( | ; ) ฮธ ( | ; ) โ‹ฏ e ( | ; ) e ( ; ) e ( | ; ) c ( ; ) pdf of polynomial logistic regression ๋‹คํ•ญ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•์— ๋Œ€ํ•œ ์šฐ๋„ ํ•จ์ˆ˜(likelihood function)๋Š” ( ) โˆ = n ฮธ ( | ; ) c ( ; ) ์ด๊ณ , ๋กœ๊ทธ ์šฐ๋„ ํ•จ์ˆ˜(log-likelihood function)๋Š” ( ) i 1 [ ( | ; ) c ( ; ) ] ์ด๋‹ค. ์ด ํ•จ์ˆ˜์—์„œ ๋ชจ์ˆ˜ ์„ Newton-Raphson ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ•œ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์ด์ง„ํ˜• ๋กœ์ง€์Šคํ‹ฑ์—์„œ ๊ณ„์‚ฐํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋กœ๊ทธ-์šฐ๋„ ํ•จ์ˆ˜์—์„œ ํ•ญ๋ชฉ(term)์— ๋Œ€ํ•œ ํŽธ๋ฏธ๋ถ„์€ โˆ‚ k j ฮธ ( | i ฮฒ ) x 1 ฮด 1 where k k { if 1 k if 1 k ์ด๋‹ค. ์‹ (4)์—์„œ ๋ถ„๋ชจ๋Š” ( ; ) log ( ฮธ ( | ; ) โ‹ฏ e ( | ; ) ) ์ด๊ณ , ์ด ํ•ญ๋ชฉ์„ ํŽธ ๋ฏธ๋ถ„ํ•˜๋ฉด โˆ‚ k j c ( i ฮฒ ) e ( 1 X ; ) k ฮธ ( | i ฮฒ ) โˆ‚ k j ฮธ ( 1 X ; ) โ‰ค 1 K 1์ด๋‹ค. ์œ„์—์„œ ํŽธ ๋ฏธ๋ถ„ํ•œ ๋‘ ๊ฐœ ์‹์„ ์ด์šฉํ•˜์—ฌ 1์ฐจ ํŽธ๋ฏธ๋ถ„์ธ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋Š” โˆ‚ k j [ ( | i ฮฒ ) c ( i ฮฒ ) ] [ โˆ‚ k j ฮธ ( 1 X ; ) ] k k [ ฮธ ( 1 X ; ) c ( i ฮฒ ) ] โˆ‚ k j ฮธ ( 1 X [ k k e ( 1 X ; ) c ( i ฮฒ ) ] j์ด๊ณ  2์ฐจ ํŽธ๋ฏธ๋ถ„์ธ ํ—ค์‹œ ์•ˆ ํ•จ์ˆ˜๋Š” 2 ฮฒ 1 1 ฮฒ 2 2 [ ( | i ฮฒ ) [ ( | i ฮฒ ) ] = โˆ’ ( i ฮฒ ) ] where = โˆ‚ โˆ‚ k j โˆ‚ k = โˆ‚ ฮฒ 2 2 [ ฮธ ( 1 X ; ) k ฮธ ( | i ฮฒ ) โˆ’ โˆ‚ k j x 1 [ ฮธ ( 1 x 1 [ ฮธ ( 1 X ; ) c ( i ฮฒ ) ] โˆ‚ k j = j [ ฮธ ( 1 X ; ) c ( i ฮฒ ) ] [ x 2 k k + j e ( 2 X ; ) c ( i ฮฒ ) ] x 1 j [ ฮธ ( 1 X ; ) c ( i ฮฒ ) ] [ ๋˜ํ•œ ์ถ”์ •ํ•˜๋ ค๋Š” ๋ชจํ˜• ( | = ) ์— ์—ฐ์†ํ˜• ๋ณ€์ˆ˜์˜ ํ…์„œ๊ณฑ๊ณผ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜์˜ ์งํ•ฉ์„ ๊ณ ๋ คํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ( | = ; ) ฮฒ 0 ฮฒ 1 1 โ‹ฏ ฮฒ M M ฮฒ 12 1 2 ๋‚˜ํƒ€๋‚ธ๋‹ค. 4. ๋กœ๊ทธ ์„ ํ˜•๋ชจํ˜• 2์ฐจ์› ๋ถ„ํ• ํ‘œ 2์ฐจ์› ๋ถ„ํ• ํ‘œ์—์„œ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜์˜ ํ‘œํ˜„์ด๋‹ค. ํ–‰์˜ ๋ฒ”์ฃผ ๊ฐœ์ˆ˜๋Š” ๊ฐœ๊ณ  ์—ด์˜ ๋ฒ”์ฃผ ๊ฐœ์ˆ˜๋Š” ๊ฐœ๋‹ค. X 1 Y โ‹ฏ J ํ•ฉ๊ณ„ 1 11 n j n J 1 โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ i i โ‹ฏ i โ‹ฏ i n + โ‹ฎ โ‹ฎ โ‹ฎ X n 1 n j n J I ํ•ฉ๊ณ„ + โ‹ฏ + โ‹ฏ + n ๋นˆ๋„์ˆ˜์— ๋Œ€ํ•œ ๋ถ„ํ• ํ‘œ ๋‹ค์Œ์€ ๊ฐ ์…€์˜ ํ™•๋ฅ ์— ๋Œ€ํ•œ 2์ฐจ์› ๋ณ€์ˆ˜ ๋ถ„ํ• ํ‘œ์ด๋‹ค. X 1 Y โ‹ฏ J ํ•ฉ๊ณ„ 1 11 ฯ€ j ฯ€ J 1 โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ i i โ‹ฏ i โ‹ฏ i ฯ€ + โ‹ฎ โ‹ฎ โ‹ฎ X ฯ€ 1 ฯ€ j ฯ€ J I ํ•ฉ๊ณ„ + โ‹ฏ + โ‹ฏ + 1 ํ™•๋ฅ ์— ๋Œ€ํ•œ ๋ถ„ํ• ํ‘œ ๋กœ๊ทธ ์„ ํ˜•๋ชจํ˜• - 2์ฐจ์› ๋ถ„ํ• ํ‘œ 2์ฐจ์› ๋ถ„ํ• ํ‘œ์—์„œ ๋…๋ฆฝ์„ฑ์— ๋Œ€ํ•œ ์กฐ๊ฑด์€ i = ฯ€ + + for all i j ์ด๋‹ค. ์ด ์‹์—์„œ ์–‘๋ณ€์— ๋กœ๊ทธ๋ฅผ ์ทจํ•˜๋ฉด log ฮผ j log n log ฯ€ + log ฯ€ j for all i j ์‹์€ ๋กœ๊ทธ ์„ ํ˜•๊ฒฐํ•ฉ์‹์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค. ร— ๋ถ„ํ• ํ‘œ๋ฅผ ๋กœ๊ทธ ์„ ํ˜•๊ฒฐํ•ฉ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. log ฮผ j ฮผ ฮป X ฮป Y i j 1 2 ์ด ํ‘œ์—์„œ ์œ—์ฒจ์ž๋Š”<NAME>์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ์ด ์•„๋‹ˆ๋ผ ํ•ด๋‹น ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜์ด๋‹ค. ์œ„ ์‹์—์„œ ๊ฐ ํ•ญ์€ i = log ฯ€ + โˆ‘ = 2 k 2 j = log ฯ€ j โˆ‘ = 2 + 2 = log n โˆ‘ = 2 k 2 โˆ‘ = 2 + 2 ์ด๋‹ค. ์ด ๋ชจํ˜•์ด ร— ๋ถ„ํ• ํ‘œ์˜ ๋…๋ฆฝ ๋กœ๊ทธ ์„ ํ˜•๋ชจํ˜•(independent log-linear model)์ด๋‹ค. i , ฮป Y ๋Š” ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜, Y ์˜ ํšจ๊ณผ์ด๋‹ค. ๋ชจ์ˆ˜๋Š” ๋ชจํ˜• ์‹๋ณ„์„ ์œ„ํ•ด ๊ฐ ์ˆ˜์ค€์—์„œ ํ•ฉ์ด 0์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ฮป X 0 โˆ‘ j =์ด๋‹ค. ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜๊ฐ€ ์„œ๋กœ ๋…๋ฆฝ์ด ์•„๋‹ˆ๋ผ๋ฉด ร— ๋ถ„ํ• ํ‘œ๋ฅผ ๋กœ๊ทธ ์„ ํ˜•๊ฒฐํ•ฉ ๋ชจํ˜•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. log ฮผ j ฮผ ฮป X ฮป Y ฮป j Y i j 1 2 ๊ฐ ํ•ญ์€ i = log ฯ€ + โˆ‘ = 2 k 2 j = log ฯ€ j โˆ‘ = 2 + 2 i X = log ฯ€ j โˆ‘ = 2 l 1 ฯ€ l ฮผ log n โˆ‘ = 2 k 2 โˆ‘ = 2 + 2 โˆ‘ = 2 l 1 ฯ€ l์ด๋‹ค. ์ด ๋ชจํ˜•์„ ร— ๋ถ„ํ• ํ‘œ์˜ ํฌํ™” ๋กœ๊ทธ ์„ ํ˜•๋ชจํ˜•(saturated log-linear model)์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋ชจํ˜•์—์„œ ๋ชจ๋“  ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ๋ชจ์ˆ˜๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•œ ๋ชจํ˜•์„ ํฌํ™” ๋ชจํ˜•์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋งŒ์ผ ํฌํ™” ๋ชจํ˜•์—์„œ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋…๋ฆฝ์ด๋ฉด ๋…๋ฆฝ ๋กœ๊ทธ ์„ ํ˜•๋ชจํ˜•์ด ๋œ๋‹ค. ๋‘ ๋ณ€์ˆ˜์˜ ๋…๋ฆฝ์„ฑ ๊ฒ€์ • ๋‘ ๋ณ€์ˆ˜์˜ ๋…๋ฆฝ์„ฑ ๊ฒ€์ •์— ๋Œ€ํ•œ ๊ฐ€์„ค์€ 0 ฮป j Y 0 ์ด๋‹ค. ๋‘ ๋ณ€์ˆ˜์˜ ๋…๋ฆฝ์„ฑ ๊ฒ€์ •์€ Pearson 2 ๊ฒ€์ •๊ณผ ์šฐ๋„๋น„ ๊ฒ€์ •(likelihood ratio test, ๅฐคๅบฆๆฏ” ๆชขๅฎš)์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค. Pearson 2 ๊ฒ€์ •์€ ๊ฐ ์…€ ๊ฐ’์˜ ํ‰๊ท ์ด ์ด์ƒ์ผ ๋•Œ ๊ทผ์‚ฌํ•˜๋Š” ์ œ์•ฝ์กฐ๊ฑด์ด ์žˆ์ง€๋งŒ ์šฐ๋„๋น„ ๊ฒ€์ •์€ ์กฐ๊ฑด์ด ์—†๋‹ค. ์šฐ๋„๋น„๋Š” ๊ท€๋ฌด๊ฐ€์„ค ์กฐ๊ฑด์—์„œ ์ตœ๋Œ€์šฐ๋„ํ•จ์ˆ˜( 0 )๋ฅผ ๋ชจ์ˆ˜์˜ ์ œ์•ฝ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ตœ๋Œ€์šฐ๋„ํ•จ์ˆ˜( )๋กœ ๋‚˜๋ˆˆ ๊ฐ’ ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค. ์ฆ‰ ์šฐ๋„๋น„๋Š” max 0 max = i 1 โˆ = l ( ^ j i) i์ด๋‹ค. ์‹์—์„œ ^ j n + + /์ด๋‹ค. ์šฐ๋„๋น„ ๊ฒ€์ •์— ์‚ฌ์šฉํ•˜๋Š” ํ†ต๊ณ„๋Ÿ‰์€ 2 โˆ’ log ฮป 2 i 1 โˆ‘ = l i log ( i ฮผ i) ์ด๋‹ค. 2 ํ†ต๊ณ„๋Ÿ‰์€ ๊ทผ์‚ฌ์ ์œผ๋กœ ์ž์œ ๋„๊ฐ€ 1์ธ 2 ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ์ฐธ๊ณ ๋กœ Pearson 2 ๊ฒ€์ •์— ์‚ฌ์šฉํ•˜๋Š” ํ†ต๊ณ„๋Ÿ‰์€ 2 โˆ‘ = k j 1 ( i โˆ’ ^ j ) ฮผ i์ด๋‹ค. ๊ฐ€๋ณ€ ์ˆ˜ ๋งŒ๋“ค๊ธฐ ํšŒ๊ท€๋ถ„์„์—์„œ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜๋ฅผ ์„ค๋ช…๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์›๋ž˜ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ง€์—ญ์„ ์กฐ์‚ฌํ•˜๊ณ  ๋ณ€์ˆซ๊ฐ’์— ๊ฐ•์›๋„(1), ๊ฒฝ๊ธฐ๋„(2), ์„œ์šธ์‹œ(3), ์ธ์ฒœ์‹œ(4)๋กœ ์ž…๋ ฅํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ์ด ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ถ„์„์— ์‚ฌ์šฉํ•˜๋ฉด 4๋Š” 2์˜ 2๋ฐฐ์ด๋ฏ€๋กœ ๊ทธ๋งŒํผ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ’์ด ๊ตฌ๋ถ„์ ์ธ ๋ช…๋ชฉ์ด๋ผ๋ฉด ๊ฐ€์ค‘์น˜๊ฐ€ ๋™์ผํ•˜๊ฒŒ ๋ณ€ํ™˜ํ•ด์•ผ ๋œ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์›๋ž˜์˜ ๋ณ€์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ€๋ณ€ ์ˆ˜ (dummy variable)๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ƒ์„ฑ๋œ ๊ฐ€๋ณ€ ์ˆ˜๋ฅผ ์„ค๋ช…๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๋ฒ”์ฃผ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐœ์ผ ๋•Œ โˆ’ ๊ฐœ ๊ฐ€๋ณ€ ์ˆ˜ ๋งŒ๋“ค๊ธฐ 1 ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜ ๊ฐ€ ๊ฐ–๋Š” ๊ฐ’์ด, , , ์ค‘ ํ•˜๋‚˜๋กœ์จ ๋ฒ”์ฃผ ์ˆ˜๊ฐ€ ๊ฐœ์ธ ๊ฒฝ์šฐ ๊ฐ€๋ณ€ ์ˆ˜๋Š” ( โˆ’ ) ๊ฐœ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋ฉฐ ์ด๋•Œ ๊ฐ ๊ฐ€๋ณ€ ์ˆ˜๋Š” c I [ i c ] ๋กœ 1 ๋˜๋Š” 0 ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค ( = , , C 1 ). ๋ฒ”์ฃผ๊ฐ€ 4๊ฐœ์ธ ๊ฒฝ์šฐ ๊ฐ€๋ณ€ ์ˆ˜ ๋ณ€ํ™˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๊ทธ ์™ธ ๊ทธ ์™ธ ๊ทธ ์™ธ 1 1 I [ = ] { c 1 ๊ทธ ์™ธ 2 2 I [ = ] { c 2 ๊ทธ ์™ธ 3 3 I [ = ] { c 3 ๊ทธ ์™ธ ์›์ž๋ฃŒ์˜ ๋ฒ”์ฃผ๊ฐ€ 4 ๊ฐœ์ธ ๊ฒฝ์šฐ ๊ฐ€๋ณ€ ์ˆ˜ ๋ณ€ํ™˜ํ•˜๋Š” ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. D D D ( 1 2 3 4 ) ( 0 1 0 1 0 0 0 0 1 0 0 0 ) ๋ฒ”์ฃผ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐœ์ผ ๋•Œ โˆ’ ๊ฐœ ๊ฐ€๋ณ€ ์ˆ˜ ๋งŒ๋“ค๊ธฐ 2 ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜์— ๋ฒ”์ฃผ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐœ์ธ ๊ฒฝ์šฐ, ์ž์œ ๋„๋Š” โˆ’์ด๋‹ค. ๊ฐ€๋ณ€ ์ˆ˜ c ๋Š” ๋ฒ”์ฃผ ๊ฐ’์ด ์ด๋ฉด, ๋ฒ”์ฃผ ๊ฐ’์ด ์ด๋ฉด 1 , ๋‚˜๋จธ์ง€๋Š”์ด๋‹ค. ๊ทธ ์™ธ c I [ i c ] { c 1 1 = 0 ์™ธ ๋ฒ”์ฃผ๊ฐ€ 4๊ฐœ์ธ ๊ฒฝ์šฐ ๊ฐ€๋ณ€ ์ˆ˜ ๋ณ€ํ™˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๊ทธ ์™ธ ๊ทธ ์™ธ ๊ทธ ์™ธ 1 1 I [ = ] { c 1 1 = 0 ์™ธ 2 2 I [ = ] { c 2 1 = 0 ์™ธ 3 3 I [ = ] { c 3 1 = 0 ์™ธ ์›์ž๋ฃŒ์˜ ๋ฒ”์ฃผ๊ฐ€ 4 ๊ฐœ์ธ ๊ฒฝ์šฐ ๊ฐ€๋ณ€ ์ˆ˜ ๋ณ€ํ™˜ํ•˜๋Š” ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. D D D ( 1 2 3 4 ) ( 0 1 0 1 0 0 0 0 1 1 1 1 1 1 1 ) ์งํ•ฉ์œผ๋กœ ๊ฐ€๋ณ€ ์ˆ˜ ๋งŒ๋“ค๊ธฐ ์ด ๋ฐฉ๋ฒ•์€ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋ฅผ ๋ชจํ˜•์— ์ถ”๊ฐ€ํ•  ๋•Œ ์•ž์˜ ์ผ๋ฐ˜์ ์ธ ๊ฐ€๋ณ€ ์ˆ˜ ๋ฐฉ๋ฒ• ๋Œ€์‹ ์— ์งํ•ฉ(direct sum)์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋ฒ”์ฃผ์— ๋Œ€ํ•œ ๊ฐ€๋ณ€ ์ˆ˜์™€ ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฒ”์ฃผ๊ฐ€ ํ•ฉํ•ด์ง„ ๊ฐ€๋ณ€ ์ˆ˜๋ฅผ ๋ชจํ˜•์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์งํ•ฉ์„ ์‚ฌ์šฉํ•œ ๊ฐ€๋ณ€ ์ˆ˜์˜ ์ƒ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋ฒ”์ฃผ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ด๋ฉด ๊ฐ€๋ณ€ ์ˆ˜๋Š” C 2 ๊ฐœ๋ฅผ ๋งŒ๋“ ๋‹ค. ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋ฅผ ๊ฐ€๋ณ€ ์ˆ˜๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•˜์—ฌ 1์—์„œ C 2 ๊นŒ์ง€ ์ž์—ฐ์ˆ˜๋ฅผ ๋ชจ๋‘ ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ์ด์ง„์ˆ˜์˜ ์ตœ๋Œ€ ์ž๋ฆฟ์ˆ˜๋Š” C 1 ์ด๋ฉฐ ์ด์ง„์ˆ˜์˜ ์ˆซ์ž ๊ฐœ์ˆ˜๋Š” ์ด์ง„์ˆ˜ 0 ์ž๋ฆฌ์—์„œ ์ด์ง„์ˆ˜ C 1 ์ž๋ฆฌ๊นŒ์ง€ ๊ฐœ๋‹ค. ์‹ญ์ง„์ˆ˜๋ฅผ ๋ณ€ํ™˜ํ•œ ์ด์ง„์ˆ˜๊ฐ€ C 1 C 1 โ‹ฏ b โˆ’ 2 โˆ’ + + 0 0 ์ผ ๋•Œ c 1 ์€ ๊ฐ€๋ณ€ ์ˆ˜์—์„œ ๋ฒ”์ฃผ์— ๋Œ€ํ•˜์—ฌ 1 ๋˜๋Š” 0์„ ๊ฒฐ์ •ํ•œ๋‹ค ( โ‰ค โ‰ค โˆ’ ). ๋ฒˆ์งธ ๊ฐ€๋ณ€ ์ˆ˜ A ๋Š” ์‹ญ์ง„์ˆ˜๋ฅผ ์ด์ง„์ˆ˜ i 0 โˆ’ I [ i 1 ] i ๋กœ ํ‘œํ˜„ํ•  ๋•Œ ์‹ญ์ง„์ˆ˜์ธ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋Š” ๊ฐ€๋ณ€ ์ˆ˜ ๊ทธ ์™ธ A { I [ i 1 ] i 0 โ‹ฏ C 1 ๊ทธ ์™ธ ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ์ฆ‰ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋Š” ๊ฐ€๋ณ€ ์ˆ˜์˜ ์ด์ง„์ˆ˜ c 1 ๋กœ ๊ฒฐ์ •๋˜๋ฉฐ i 1 i ( , โˆ’ ) ์ธ ๋ฒ”์ฃผ๋Š” ๊ฐ€๋ณ€ ์ˆ˜ ๋ณ€ํ™˜ ๊ฐ’์ด ๋ชจ๋‘ 1์ด๊ณ , ๊ทธ ์ด์™ธ๋Š” 0์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฒ”์ฃผ๊ฐ€ ๊ฐœ์ธ ๊ฒฝ์šฐ Category dummy 1 dummy 3 dummy 2 โˆ’ 1 ( ) I [ = ] { c 1 c 1 11 ( ) I [ = , ] { c 1 2 o.w 11 โž 0 ( ) I [ โ‰  ] { c 1 c 1 c C ์—์„œ ๊ฐ€๋ณ€ ์ˆ˜ 1 b = ์ธ ๊ฒฝ์šฐ๋กœ, ๋ฒ”์ฃผ๊ฐ€ = ์ธ ๊ฒฝ์šฐ๋งŒ ๊ฐ€๋ณ€ ์ˆ˜ ๊ฐ’์ด 1์ด๊ณ  ๊ทธ ์ด์™ธ์˜ ๊ฒฝ์šฐ๋Š” 0์ด๋ฉฐ, ๊ฐ€๋ณ€ ์ˆ˜ 3 b = , 1 1 ์ธ ๋ฒ”์ฃผ = ๊ณผ ๋ฒ”์ฃผ = ์ผ ๋•Œ ๊ฐ€๋ณ€ ์ˆ˜ ๊ฐ’์ด 1์ด๊ณ  ๊ทธ ์ด์™ธ์˜ ๊ฒฝ์šฐ๋Š” 0์ด๋ฉฐ ๋งจ ๋์— ๋งŒ๋“œ๋Š” C 2 ๋ฒˆ์งธ ๊ฐ€๋ณ€ ์ˆ˜ 2 โˆ’๋Š” ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•  ๊ฒฝ์šฐ 11 โž 0 ( ) ์ด๋ฏ€๋กœ = ์ธ ๊ฒฝ์šฐ๋งŒ ๊ฐ€๋ณ€ ์ˆ˜ ๊ฐ’์ด 0์ด๊ณ  ๋‚˜๋จธ์ง€ ๋ฒ”์ฃผ ๊ฐ’ โ‰  ์€ ๊ฐ€๋ณ€ ์ˆ˜ ๊ฐ’์ด 1์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฒ”์ฃผ์˜ ๊ฐœ์ˆ˜๊ฐ€ = ์ผ ๋•Œ ๊ฐ€๋ณ€ ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด ๊ฐ€๋ณ€ ์ˆ˜์˜ ๊ฐœ์ˆ˜๋Š” 3 2 6 ๊ฐœ๋‹ค. ๋ฒ”์ฃผ =์— ๋Œ€ํ•œ ๊ฐ€๋ณ€ ์ˆ˜์˜ ์ด์ง„์ˆ˜ ๋ฒ”์œ„๋Š” ( 001 ( ) ) 2 โˆ’ = ( 110 ( ) ) ์ด๋‹ค. ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ€๋ณ€ ์ˆ˜ ์กฐ๊ฑด์— ๋งž๊ฒŒ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค ๊ทธ ์™ธ ๊ทธ ์™ธ ๊ทธ ์™ธ ๊ทธ ์™ธ ๊ทธ ์™ธ ๊ทธ ์™ธ 1 1 ( 001 ( ) ) I [ = ] { c 1 ๊ทธ ์™ธ 2 2 ( 010 ( ) ) I [ = ] { c 2 ๊ทธ ์™ธ 3 3 ( 011 ( ) ) I [ = , ] { c 1 2 ๊ทธ ์™ธ 4 4 ( 100 ( ) ) I [ = ] { c 3 ๊ทธ ์™ธ 5 5 ( 101 ( ) ) I [ = , ] { c 1 3 ๊ทธ ์™ธ 6 6 ( 110 ( ) ) I [ = , ] { c 2 3 ๊ทธ ์™ธ ๋ฒ”์ฃผ๊ฐ€ 3 ๊ฐœ์ธ ๊ฒฝ์šฐ ์งํ•ฉ(direct sum)์„ ๊ณ ๋ คํ•œ ๊ฐ€๋ณ€ ์ˆ˜ ๋ณ€ํ™˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์›์ž๋ฃŒ ์ž D D D D D D ( 1 2 3 3 ) ( 0 0 0 0 0 0 1 0 1 1 0 1 1 0 1 0 1 1 0 1 1 0 1 1 ) ๋กœ๊ทธ ์„ ํ˜•๋ชจํ˜•์˜ ๋””์ž์ธ ํ–‰๋ ฌ ๋‹ค์Œ์€ ํ™•๋ฅ ์— ๋Œ€ํ•œ ร— ๋ถ„ํ• ํ‘œ์ด๋‹ค. X 1 2 ํ•ฉ๊ณ„ 1 11 12 1 X ฯ€ 21 22 2 ํ•ฉ๊ณ„ + ฯ€ 2 ํ™•๋ฅ ์— ๋Œ€ํ•œ ร— ๋ถ„ํ• ํ‘œ ๋””์ž์ธ ํ–‰๋ ฌ 1 ร— ๋ชจํ˜•์—์„œ ๋…๋ฆฝ ๋กœ๊ทธ ์„ ํ˜•๋ชจํ˜•์— ๋Œ€ํ•œ ๋””์ž์ธ ํ–‰๋ ฌ์ด๋‹ค. ๋…๋ฆฝ๋ณ€์ˆ˜๋Š”, ๊ฐ€ ์žˆ๊ณ , ๋ฐ˜์‘ ๋ณ€์ˆ˜๋Š” ํ™•๋ฅ  ๊ฐ’ 11 ฯ€ 12 ฯ€ 21 ฯ€ 22 ์ด๋‹ค. ๋””์ž์ธ ํ–‰๋ ฌ ๊ตฌ์„ฑ์€ ์ƒ์ˆ˜์™€ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜,์ด๋‹ค. ๊ฐ€๋ณ€ ์ˆ˜๋Š” ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜์— ๋Œ€ํ•˜์—ฌ i 1 2 โ‹ฏ C ์ผ ๋•Œ ๊ทธ ์™ธ c I [ i c ] { x = 0 ์™ธ ์œผ๋กœ ๋งŒ๋“ ๋‹ค. ๊ฐ ๋ณ€์ˆ˜๋งˆ๋‹ค ๋ฒ”์ฃผ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐœ์ด๋ฏ€๋กœ ๊ฐ€๋ณ€ ์ˆ˜ ๊ฐ’์€ ๊ณผ ์ด ๋œ๋‹ค. SPSS์—์„œ ์ด ๊ณผ์ •์œผ๋กœ ๊ฐ€๋ณ€ ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค. ์ƒ์ˆ˜ ๋ชจ์ˆ˜ log ( ) ( log ( 11 ) log ( 12 ) log ( 21 ) log ( 22 ) ) ์ƒ X ( 1 1 0 0 1 0 ) ์ˆ˜ ( ฮป ฮป) ๋””์ž์ธ ํ–‰๋ ฌ 2 ๋ฐ˜์‘ ๋ณ€์ˆ˜๋Š” ๋กœ์ง“ํ•จ์ˆ˜์— ์ ์šฉํ•˜์—ฌ 3๊ฐœ๋กœ ์ค„์ธ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐ˜์‘ ๋ณ€์ˆ˜๋Š” 1 log ( 11 p 22 ) F = log ( 12 p 22 ) F = log ( 21 p 22 ) ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๋””์ž์ธ ํ–‰๋ ฌ ๊ตฌ์„ฑ์€ ์ƒ์ˆ˜์™€ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜, ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ณ€์ˆ˜์˜ ๊ตํ˜ธ์ž‘์šฉ ร—์ด๋‹ค. ๊ฐ€๋ณ€ ์ˆ˜๋Š” ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜์— ๋Œ€ํ•˜์—ฌ i 1 2 โ‹ฏ C ์ผ ๋•Œ ๊ทธ ์™ธ c I [ i c ] { x = โˆ’ x = 0 ์™ธ ์œผ๋กœ ๋งŒ๋“ ๋‹ค. SAS์—์„œ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ€๋ณ€ ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ถ„์„์— ์‚ฌ์šฉํ•œ๋‹ค. ์•„๋ž˜ ๊ฐ€๋ณ€ ์ˆ˜ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ•˜์˜€๋‹ค. ๊ฐ ๋ณ€์ˆ˜๋งˆ๋‹ค ๋ฒ”์ฃผ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐœ์ด๋ฏ€๋กœ ๊ฐ€๋ณ€ ์ˆ˜ ๊ฐ’์€ ๊ณผ 1 ์ด ๋œ๋‹ค. ํฌํ™” ๋ชจํ˜•์ธ ๊ฒฝ์šฐ, ๊ตํ˜ธ์ž‘์šฉ ๊ฐ€๋ณ€ ์ˆ˜๋Š” ๊ฐ ๊ฐ€๋ณ€ ์ˆ˜ ๊ฐ’์„ ๊ณฑํ•œ๋‹ค. ์ƒ์ˆ˜ log ( ) ( log ( 11 ) log ( 12 ) log ( 21 ) log ( 22 ) ) ์ƒ X X Y ( 1 1 1 1 1 โˆ’ 1 1 โˆ’ โˆ’ 1 ) โˆ’ ( 1 1 ) ( 1 1 1 1 1 โˆ’ โˆ’ โˆ’ 1 ) โˆ’ ( 1 1 ) ์—ฌ๊ธฐ์„œ ์™€๋Š” ๋ชจ์ˆ˜ ๋ฒกํ„ฐ์ด๊ณ  ์™€๋Š” ํ™•๋ฅ  ํ•ฉ์ด 1์ด๋ผ๋Š” ์ œํ•œ์— ํ•„์š”ํ•œ ์ •๊ทœํ™” ์ƒ์ˆ˜์ด๋‹ค. ( 1 2 3 ) ( 0 โˆ’ 0 0 1 0 โˆ’ ) ( log ( 11 ) log ( 12 ) log ( 21 ) log ( 22 ) ) ( 0 โˆ’ 0 0 1 0 โˆ’ ) ( 1 1 1 1 1 โˆ’ โˆ’ โˆ’ 1 ) = ( 2 2 โˆ’ 0 โˆ’ ) 2 2 ๋ชจํ˜•์—์„œ ๊ตํ˜ธ์ž‘์šฉ์„ ์ œ์™ธํ•œ ๋…๋ฆฝ ๋กœ๊ทธ ์„ ํ˜•๋ชจํ˜•์—์„œ ๋””์ž์ธ ํ–‰๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ƒ์ˆ˜ ( log ( 11 ) log ( 12 ) log ( 21 ) log ( 22 ) ) ์ƒ X ( 1 1 โˆ’ 1 1 1 1 1 ) โˆ’ ( 1 1 ) ( 1 โˆ’ โˆ’ 1 1 1 ) โˆ’ ( 1 1 ) ์—ฌ๊ธฐ์„œ ์™€๋Š” ๋ชจ์ˆ˜ ๋ฒกํ„ฐ์ด๊ณ  ์™€๋Š” ํ™•๋ฅ  ํ•ฉ์ด 1์ด๋ผ๋Š” ์ œํ•œ์— ํ•„์š”ํ•œ ์ •๊ทœํ™” ์ƒ์ˆ˜์ด๋‹ค. ( 1 2 3 ) ( 0 โˆ’ 0 0 1 0 โˆ’ ) ( log ( 11 ) log ( 12 ) log ( 21 ) log ( 22 ) ) ( 0 โˆ’ 0 0 1 0 โˆ’ ) ( 1 โˆ’ โˆ’ 1 1 1 ) = ( 2 0 2 ) ๋กœ๊ทธ ์„ ํ˜•๋ชจํ˜• - 3์ฐจ์› ๋ถ„ํ• ํ‘œ 2์ฐจ์› ๋ถ„ํ• ํ‘œ๋Š” ํฌํ™” ๋ชจํ˜•์—์„œ ๊ตํ˜ธ์ž‘์šฉ์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. 3์ฐจ์› ๋ถ„ํ• ํ‘œ๋Š” ํฌํ™” ๋ชจํ˜•์ด ์•„๋‹ˆ๋”๋ผ๋„ ๊ตํ˜ธ์ž‘์šฉ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 3์ฐจ์› ๋ถ„ํ• ํ‘œ์—์„œ ๋ณ€์ˆ˜ ์ƒํ˜ธ ๊ฐ„์˜ ๋…๋ฆฝ์„ฑ์€ ์กฐ๊ฑด๋ถ€ ๋…๋ฆฝ(conditional independence) i | = + k ฯ€ + ์ฃผ๋ณ€ ๋…๋ฆฝ(marginal independence) i + ฯ€ + ฯ€ j ์ƒํ˜ธ ๋…๋ฆฝ(mutual independence) i k ฯ€ + ฯ€ j ฯ€ + ๊ฒฐํ•ฉ ๋…๋ฆฝ(joint independence) i k ฯ€ + ฯ€ j ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ๋‹ค์Œ์€ 3์ฐจ์› ๋ถ„ํ• ํ‘œ์—์„œ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจํ˜•์ด๋‹ค. ์„ธ ๋ณ€์ˆ˜ ์„œ๋กœ ๋…๋ฆฝ ์— ๋Œ€ํ•œ์˜ ๊ฒฐํ•ฉ ๋…๋ฆฝ ์— ๋Œ€ํ•œ์˜ ์กฐ๊ฑด๋ถ€ ๋…๋ฆฝ ์กฐ๊ฑด ์—†๋Š” ์„ธ ๋ณ€์ˆ˜ ์„œ๋กœ ๋…๋ฆฝ ํฌํ™” ๋ชจํ˜• log ฮผ j = + i + j + k ์„ธ ๋ณ€ ์„œ log ฮผ j = + i + j + k + i X XY ๋Œ€ log ฮผ j = + i + j + k + i X + j Y XY ๋Œ€ Z log ฮผ j = + i + j + k + i X + j Y + k Z ์กฐ ์—† ์„ธ ๋ณ€ log ฮผ j = + i + j + k + i X + j Y + k Z + i k Y ํฌ 5. ํŽธ์˜ ํšŒ๊ท€ ๋ชจํ˜• 1. Lasso ํšŒ๊ท€๋ถ„์„์ด๋ž€? Lasso ํšŒ๊ท€๋ถ„์„์€ Ridge ํšŒ๊ท€๋ถ„์„๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ณ€์ˆ˜ ์„ ํƒ๊ณผ ๋ชจํ˜• ์ถ•์†Œ๋ฅผ ์œ„ํ•œ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ด๋‹ค. Lasso๋Š” Least Absolute Shrinkage and Selection Operator์˜ ์•ฝ์–ด๋กœ, ๋ณ€์ˆ˜ ์„ ํƒ๊ณผ ๋ชจํ˜• ์ถ•์†Œ๋ฅผ ์œ„ํ•ด L1 ๊ทœ์ œ๋ฅผ ์ ์šฉํ•œ๋‹ค. ์ฆ‰, Lasso ํšŒ๊ท€๋ถ„์„์€ ์ž…๋ ฅ ๋ณ€์ˆ˜ ์ค‘์—์„œ ์œ ์šฉํ•œ ๋ณ€์ˆ˜๋“ค๋งŒ ์„ ํƒํ•˜๊ณ , ๋‚˜๋จธ์ง€๋Š” ๋ฒ„๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. Lasso ํšŒ๊ท€๋ถ„์„์˜ ์ˆ˜์‹ Lasso ํšŒ๊ท€๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆ˜์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. i i i e i 1 ( i ฮฒ โˆ’ j 1 x j j ) subject to j 1 | j โ‰ค ์—ฌ๊ธฐ์„œ i ๋Š” ์ข…์† ๋ณ€์ˆ˜์ด๊ณ , i๋Š” ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ๋ฒˆ์งธ ๊ด€์ธก์น˜์— ๋Œ€ํ•œ ๋ฒˆ์งธ ์š”์†Œ์ด๋‹ค. j j ๋ฒˆ์งธ ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ๊ณ„์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 0 ๋Š” ์ ˆํŽธ์ด๋‹ค.๋Š” L1 ๊ทœ์ œ ๊ณ„์ˆ˜์ด๋‹ค. Lasso ํšŒ๊ท€๋ถ„์„์˜ ํŠน์ง• Lasso ํšŒ๊ท€๋ถ„์„์€ Ridge ํšŒ๊ท€๋ถ„์„๊ณผ ๋น„๊ตํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง„๋‹ค. Lasso ํšŒ๊ท€๋ถ„์„์€ L1 ๊ทœ์ œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ๊ณ„์ˆ˜๊ฐ€ 0์ธ ๋ณ€์ˆ˜๋ฅผ ์„ ํƒํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋Š” ๋ณ€์ˆ˜ ์„ ํƒ์— ์œ ์šฉํ•˜๋‹ค. Lasso ํšŒ๊ท€๋ถ„์„์€ Ridge ํšŒ๊ท€๋ถ„์„๋ณด๋‹ค ๋ชจํ˜• ์ถ•์†Œ ํšจ๊ณผ๊ฐ€ ๋” ๊ฐ•ํ•˜๋‹ค. Lasso ํšŒ๊ท€๋ถ„์„์€ ๋ณ€์ˆ˜ ์„ ํƒ๊ณผ ๋ชจํ˜• ์ถ•์†Œ๋ฅผ ์œ„ํ•ด L1 ๊ทœ์ œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ํ•ด์„์ด ์šฉ์ดํ•˜๋‹ค. Lasso ํšŒ๊ท€๋ถ„์„์˜ ์˜ˆ์‹œ ๋‹ค์Œ์€ Python์—์„œ Lasso ํšŒ๊ท€๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ด๋‹ค. from sklearn.linear_model import Lasso from sklearn.datasets import load_boston from sklearn.preprocessing import StandardScaler boston = load_boston() X = boston.data y = boston.target scaler = StandardScaler() X = scaler.fit_transform(X) lasso = Lasso(alpha=0.1) lasso.fit(X, y) print(lasso.coef_) ์ด ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ๋Š” ๋ณด์Šคํ„ด ์ฃผํƒ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜์—ฌ Lasso ํšŒ๊ท€๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. ๋จผ์ €, ๋ฐ์ดํ„ฐ๋ฅผ StandardScaler๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘œ์ค€ํ™”ํ•œ ํ›„, Lasso ๋ชจ๋ธ์„ ์ •์˜ํ•˜๊ณ  alpha ๊ฐ’์„ ์„ค์ •ํ•˜์—ฌ ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ๊ณ„์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ๋‹ค. 2. Ridge ํšŒ๊ท€๋ถ„์„์ด๋ž€? Ridge ํšŒ๊ท€๋ถ„์„์€ ์ผ๋ฐ˜์ ์ธ ์ตœ์†Œ์ œ๊ณฑ๋ฒ•(OLS)์˜ ๋ฌธ์ œ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐœ๋ฐœ๋œ ํšŒ๊ท€๋ถ„์„ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ด๋‹ค. Ridge ํšŒ๊ท€๋ถ„์„์€ ์ž…๋ ฅ ๋ณ€์ˆ˜๋“ค์˜ ๊ณต์„ ์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ , ๋ชจํ˜•์˜ ์•ˆ์ •์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด L2 ๊ทœ์ œ๋ฅผ ์ ์šฉํ•œ๋‹ค. Ridge ํšŒ๊ท€๋ถ„์„์˜ ์ˆ˜์‹ Ridge ํšŒ๊ท€๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆ˜์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. i i i e i 1 ( i ฮฒ โˆ’ j 1 x j j ) subject to j 1 ฮฒ 2 s ์—ฌ๊ธฐ์„œ i ๋Š” ์ข…์† ๋ณ€์ˆ˜์ด๊ณ , i๋Š” ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ๋ฒˆ์งธ ๊ด€์ธก์น˜์— ๋Œ€ํ•œ ๋ฒˆ์งธ ์š”์†Œ์ด๋‹ค. j j ๋ฒˆ์งธ ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ๊ณ„์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 0 ๋Š” ์ ˆํŽธ์ด๋‹ค.๋Š” L2 ๊ทœ์ œ ๊ณ„์ˆ˜์ด๋‹ค. Ridge ํšŒ๊ท€๋ถ„์„์˜ ํŠน์ง• Ridge ํšŒ๊ท€๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง„๋‹ค. Ridge ํšŒ๊ท€๋ถ„์„์€ L2 ๊ทœ์ œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ๊ณ„์ˆ˜๊ฐ€ 0์ด ๋˜์ง€ ์•Š๊ณ  ์ž‘์•„์ง€๊ฒŒ ๋œ๋‹ค. ์ด๋Š” ๋ณ€์ˆ˜ ์„ ํƒ๋ณด๋‹ค๋Š” ๋ชจํ˜• ์ถ•์†Œ์— ์œ ์šฉํ•˜๋‹ค. Ridge ํšŒ๊ท€๋ถ„์„์€ ์ž…๋ ฅ ๋ณ€์ˆ˜๋“ค์˜ ๊ณต์„ ์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. Ridge ํšŒ๊ท€๋ถ„์„์€ ๋ชจํ˜•์˜ ์•ˆ์ •์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค. Ridge ํšŒ๊ท€๋ถ„์„์˜ ์˜ˆ์‹œ ๋‹ค์Œ์€ Python์—์„œ Ridge ํšŒ๊ท€๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ด๋‹ค. from sklearn.linear_model import Ridge from sklearn.datasets import load_boston from sklearn.preprocessing import StandardScaler boston = load_boston() X = boston.data y = boston.target scaler = StandardScaler() X = scaler.fit_transform(X) ridge = Ridge(alpha=0.1) ridge.fit(X, y) print(ridge.coef_) ์ด ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ๋Š” ๋ณด์Šคํ„ด ์ฃผํƒ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜์—ฌ Ridge ํšŒ๊ท€๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. ๋จผ์ €, ๋ฐ์ดํ„ฐ๋ฅผ StandardScaler๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘œ์ค€ํ™”ํ•œ ํ›„, Ridge ๋ชจ๋ธ์„ ์ •์˜ํ•˜๊ณ  alpha ๊ฐ’์„ ์„ค์ •ํ•˜์—ฌ ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ๊ณ„์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ๋‹ค. 3. Elastic Net ํšŒ๊ท€๋ถ„์„์ด๋ž€? Elastic Net ํšŒ๊ท€๋ถ„์„์€ Ridge ํšŒ๊ท€๋ถ„์„๊ณผ Lasso ํšŒ๊ท€๋ถ„์„์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐœ๋ฐœ๋œ ํšŒ๊ท€๋ถ„์„ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ด๋‹ค. Elastic Net ํšŒ๊ท€๋ถ„์„์€ L1 ๊ทœ์ œ์™€ L2 ๊ทœ์ œ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ์ ์šฉํ•˜๋ฉฐ, ๋ณ€์ˆ˜ ์„ ํƒ๊ณผ ๋ชจํ˜• ์ถ•์†Œ๋ฅผ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์„ ๊ฐ€์ง„๋‹ค. Elastic Net ํšŒ๊ท€๋ถ„์„์˜ ์ˆ˜์‹ Elastic Net ํšŒ๊ท€๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆ˜์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. i i i e i 1 ( i ฮฒ โˆ’ j 1 x j j ) + 1 j 1 | j + 2 j 1 ฮฒ 2 ์—ฌ๊ธฐ์„œ i ๋Š” ์ข…์† ๋ณ€์ˆ˜์ด๊ณ , i๋Š” ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ๋ฒˆ์งธ ๊ด€์ธก์น˜์— ๋Œ€ํ•œ ๋ฒˆ์งธ ์š”์†Œ์ด๋‹ค. j j ๋ฒˆ์งธ ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ๊ณ„์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 0 ๋Š” ์ ˆํŽธ์ด๋‹ค. 1 ฮฑ๋Š” ๊ฐ๊ฐ L1 ๊ทœ์ œ ๊ณ„์ˆ˜์™€ L2 ๊ทœ์ œ ๊ณ„์ˆ˜์ด๋‹ค. Elastic Net ํšŒ๊ท€๋ถ„์„์˜ ํŠน์ง• Elastic Net ํšŒ๊ท€๋ถ„์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง„๋‹ค. Elastic Net ํšŒ๊ท€๋ถ„์„์€ L1 ๊ทœ์ œ์™€ L2 ๊ทœ์ œ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ์ ์šฉํ•˜๋ฏ€๋กœ, Lasso ํšŒ๊ท€๋ถ„์„๊ณผ Ridge ํšŒ๊ท€๋ถ„์„์˜ ์žฅ์ ์„ ๋ชจ๋‘ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. Elastic Net ํšŒ๊ท€๋ถ„์„์€ ๋ณ€์ˆ˜ ์„ ํƒ๊ณผ ๋ชจํ˜• ์ถ•์†Œ๋ฅผ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. Elastic Net ํšŒ๊ท€๋ถ„์„์€ ์ž…๋ ฅ ๋ณ€์ˆ˜๋“ค ๊ฐ„์˜ ๊ณต์„ ์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. Elastic Net ํšŒ๊ท€๋ถ„์„์˜ ์˜ˆ์‹œ ๋‹ค์Œ์€ Python์—์„œ Elastic Net ํšŒ๊ท€๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ์ด๋‹ค. from sklearn.linear_model import ElasticNet from sklearn.datasets import load_boston from sklearn.preprocessing import StandardScaler boston = load_boston() X = boston.data y = boston.target scaler = StandardScaler() X = scaler.fit_transform(X) elasticnet = ElasticNet(alpha=0.1, l1_ratio=0.5) elasticnet.fit(X, y) print(elasticnet.coef_) ์ด ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ๋Š” ๋ณด์Šคํ„ด ์ฃผํƒ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜์—ฌ Elastic Net ํšŒ๊ท€๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. ๋จผ์ €, ๋ฐ์ดํ„ฐ๋ฅผ StandardScaler๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘œ์ค€ํ™”ํ•œ ํ›„, ElasticNet ๋ชจ๋ธ์„ ์ •์˜ํ•˜๊ณ  alpha ๊ฐ’๊ณผ l1_ratio ๊ฐ’์„ ์„ค์ •ํ•˜์—ฌ ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. 08. ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋ถ„์„ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋ฅผ ์ •๋ฆฌํ•œ ๋ถ„ํ• ํ‘œ(contingency table)๋Š” ์ž๋ฃŒ ์ˆ˜์ง‘ ๋ฐฉ๋ฒ• ๋”ฐ๋ผ ํ•ด์„์„ ๋‹ค๋ฅด๊ฒŒ ํ•œ๋‹ค. ์ž๋ฃŒ ์ˆ˜์ง‘ ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ ๋ถ„์„ ๋ฐฉ๋ฒ•์„ ๋…๋ฆฝ์„ฑ ๊ฒ€์ •(independence test)๊ณผ ๋™์งˆ์„ฑ ๊ฒ€์ •(homogeneity test)์œผ๋กœ ๋‚˜๋ˆ„์–ด ์†Œ๊ฐœํ•œ๋‹ค. 1. ๋…๋ฆฝ์„ฑ ๊ฒ€์ • ๋‘ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ ์ˆ˜์ง‘ ๋ฐฉ๋ฒ•์ด ๊ด€์ฐฐ์—ฐ๊ตฌ(observational study)์ธ ๊ฒฝ์šฐ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ์„œ๋กœ ์˜ํ–ฅ์„ ์ฃผ๋Š”์ง€ ์นด์ด ์ œ๊ณฑ ๋…๋ฆฝ์„ฑ ๊ฒ€์ •์œผ๋กœ ๋‘ ์ง‘๋‹จ์˜ ๋…๋ฆฝ์„ฑ์„ ๊ฒ€์ •ํ•œ๋‹ค. ๋…๋ฆฝ์„ฑ ๊ฒ€์ • ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋Š” ๋™์งˆ์„ฑ ๊ฒ€์ • ๊ณ„์‚ฐ ๊ฒฐ๊ณผ์™€ ๋™์ผํ•˜๋‹ค. ๋‹ค์Œ ์ž๋ฃŒ๋Š” A ๋„์‹œ์—์„œ ์ „๋™ ํ‚ฅ๋ณด๋“œ ์‚ฌ์šฉ์ž ์ค‘ ์‚ฌ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ๋ณ‘์›์— ์˜จ ํ™˜์ž๋ฅผ 6๊ฐœ์›” ๋™์•ˆ ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. ๋ณดํ˜ธ๊ตฌ ์ฐฉ์šฉ ์—ฌ๋ถ€์™€ ๋ถ€์ƒ ์ •๋„๋Š” ์„œ๋กœ ์˜ํ–ฅ์„ ์ฃผ๋Š”์ง€ ๊ฒ€์ •ํ•˜๋ผ. ๋ถ€์ƒ ์ •๋„ ๋ณดํ˜ธ๊ตฌ ์ฐฉ์šฉ 2์ฃผ ์ดํ•˜ 4์ฃผ ์ดํ•˜ 5์ฃผ ์ด์ƒ ํ•ฉ๊ณ„ ์ฐฉ์šฉ 10 17 7 34 ๋ฏธ์ฐฉ์šฉ 23 12 5 40 ํ•ฉ๊ณ„ 33 29 12 74 ๋ณดํ˜ธ๊ตฌ ์ฐฉ์šฉ๊ณผ ๋ถ€์ƒ ์ •๋„ SPSS ๋ถ„์„ ๊ณผ์ • ์ž๋ฃŒ์ž…๋ ฅ ํ‘œ์— ์žˆ๋Š” ์ž๋ฃŒ๋Š” ์›์ž๋ฃŒ๋ฅผ ์š”์•ฝํ•œ ๊ฒƒ์œผ๋กœ ์ง์ ‘ SPSS์— ์ž…๋ ฅํ•œ๋‹ค. ์ž๋ฃŒ ์ž…๋ ฅ์€ ํŒŒ์ผ -> ์ƒˆ ํŒŒ์ผ -> ๋ฐ์ดํ„ฐ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋ณ€์ˆซ๊ฐ’์— ๋ณ€์ˆ˜ ์ด๋ฆ„, ์†Œ์ˆ˜์ , ๊ฐ’ ์„ค์ • ์ƒˆ ํŒŒ์ผ์„ ๋งŒ๋“ค๋ฉด ๋‘ ๊ฐœ ์‹œํŠธ๊ฐ€ ๋ณด์ธ๋‹ค. ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ ์‹œํŠธ๋Š” ์ž๋ฃŒ๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ๋Š” ์ž…๋ ฅํ•œ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•œ๋‹ค. ์ž๋ฃŒ์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋Š” ์—ด์— ์ž…๋ ฅํ•œ๋‹ค. ์ฒซ ์—ด๊ณผ ๋‘˜์งธ ์—ด์€ ์ˆซ์ž๋กœ ์ž…๋ ฅํ•˜์˜€์ง€๋งŒ ์ดํ›„ ๋ ˆ์ด๋ธ”์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฌธ์ž๋กœ ์ถœ๋ ฅ๋˜๊ฒŒ ์„ค์ •ํ•œ๋‹ค. ์…‹์งธ ์—ด์€ ํ™˜์ž ์ˆ˜๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ๋ณ€์ˆ˜์— ์ ์šฉ ๊ฒฐ๊ณผ ํ™•์ธ ๋ณ€์ˆ˜ ๋ณด๊ธฐ ์‹œํŠธ์—์„œ ๋ณ€์ˆ˜์— ์„ค์ •๋œ ๊ธฐ๋ณธ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค. ์ด๋ฆ„์€ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ๋ณดํ˜ธ๊ตฌ ์ฐฉ์šฉ, ๋ถ€์ƒ ์ •๋„, ๋นˆ๋„๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ํ˜„์žฌ ๋ณ€์ˆ˜๋“ค์€ ์†Œ์ˆ˜์ ์ด ํ•„์š” ์—†์œผ๋ฏ€๋กœ ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฟ์ˆ˜๋Š” 0์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ๊ฐ’ ์†์„ฑ์—์„œ ๋ณดํ˜ธ๊ตฌ ์ฐฉ์šฉ์€ 1์„ ์ฐฉ์šฉ, 2๋ฅผ ๋ฏธ์ฐฉ์šฉ, ๋ถ€์ƒ ์ •๋„๋Š” 1์„ 2์ฃผ ์ดํ•˜, 2๋ฅผ 4์ฃผ ์ดํ•˜, 3์„ 5์ฃผ ์ด์ƒ์œผ๋กœ ๋ ˆ์ด๋ธ”์„ ๋ถ™์—ฌ ์ˆซ์ž์— ํ•ด๋‹น ๊ฐ’์„ ํ‘œํ˜„ํ•œ๋‹ค. ๋ณ€์ˆ˜์— ์„ค์ •ํ•œ ๊ฐ’์ด ์ž˜ ์ ์šฉ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋งŒ์ผ ๊ฐ’์— ๋ ˆ์ด๋ธ” ํ•œ ๊ฐ’์ด ๋ณด์ด์ง€ ์•Š์œผ๋ฉด ๋ณด๊ธฐ - > ๊ฐ’ ๋ ˆ์ด๋ธ” ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋ณด์ธ๋‹ค. ๊ฐ€์ค‘ ์ผ€์ด์Šค ์„ค์ • ํ˜„์žฌ ์ž…๋ ฅ๋œ ๊ฐ’์€ ์›์ž๋ฃŒ๋ฅผ ์š”์•ฝํ•œ ์ž๋ฃŒ๋กœ ๋นˆ๋„๋ฅผ ๋ชจ๋“  ๋ณ€์ˆ˜์— ์ ์šฉํ•˜๋ ค๋ฉด ๊ฐ€์ค‘์น˜ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ€์ค‘์น˜๋Š” ๋ฐ์ดํ„ฐ -> ๊ฐ€์ค‘ ์ผ€์ด์Šค ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ๊ฐ€์ค‘ ์ผ€์ด์Šค ์ฐฝ์—์„œ ๊ฐ€์ค‘ ์ผ€์ด์Šค ์ง€์ •์— ๋นˆ๋„ ๋ณ€์ˆ˜๋Š” ๋นˆ๋„๋กœ ์„ค์ •ํ•œ๋‹ค. ์นด์ด์ œ๊ณฑ ๊ฒ€์ • ์‹คํ–‰ ์ด์ œ ์ž๋ฃŒ ์„ค์ •์ด ์™„๋ฃŒ๋˜์—ˆ๊ณ , ๋‹จ์ผ ํ‘œ๋ณธ ๋น„์œจ ๊ฒ€์ •์€ ๋ถ„์„ -> ๊ธฐ์ˆ ํ†ต๊ณ„๋Ÿ‰ -> ๊ต์ฐจ๋ถ„์„ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•œ๋‹ค. ํ–‰์— ๋ณดํ˜ธ๊ตฌ ์ฐฉ์šฉ, ์—ด์— ๋ถ€์ƒ ์ •๋„๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ํ†ต๊ณ„๋Ÿ‰ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์นด์ด ์ œ๊ณฑ์„ ์„ ํƒํ•œ๋‹ค. ๋ถ„์„ ๊ฒฐ๊ณผ ๊ฒ€์ • ๊ฒฐ๊ณผ Pearson ์นด์ด ์ œ๊ณฑ์—์„œ ์œ ์˜ ํ™•๋ฅ ์ด 0.053์œผ๋กœ ๊ท€๋ฌด๊ฐ€์„ค 0 "๋‘ ์ง‘๋‹จ์€ ์„œ๋กœ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค."๋ฅผ ๊ธฐ๊ฐํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๋ณดํ˜ธ๊ตฌ ์ฐฉ์šฉ ์œ ๋ฌด์™€ ๋ถ€์ƒ ์ •๋„๋Š” ์„œ๋กœ ์˜ํ–ฅ์ด ์—†๋‹ค. 2. ๋™์งˆ์„ฑ ๊ฒ€์ • ๋‘ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ ์ˆ˜์ง‘ ๋ฐฉ๋ฒ•์ด ์‹คํ—˜์—ฐ๊ตฌ(experimental study)์ธ ๊ฒฝ์šฐ ์ž„์˜๋กœ ๋‚˜๋ˆˆ ๊ฐ ์ง‘๋‹จ์˜ ํ•ญ๋ชฉ๋งˆ๋‹ค ๋น„์œจ์ด ๊ฐ™์€์ง€ ๋™์งˆ์„ฑ ๊ฒ€์ •์„ ํ•œ๋‹ค. ๋™์งˆ์„ฑ(homogeneity) ๊ฒ€์ •์€ ๋…๋ฆฝ์„ฑ ๊ฒ€์ •๊ณผ ๊ณ„์‚ฐ ๊ณผ์ •๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•˜๋‹ค. ์ œ์•ฝํšŒ์‚ฌ์—์„œ ๊ฐœ๋ฐœํ•œ ์‹ ์•ฝ์ด ํšจ๊ณผ๊ฐ€ ์žˆ๋Š”์ง€ ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. ์‹ ์•ฝ๊ณผ ๊ธฐ์กด ์•ฝ๊ณผ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์ •ํ•˜๋ผ. ์•ฝ ํšจ๊ณผ ์•ฝ ์ข…๋ฅ˜ ํšจ๊ณผ ์ข‹์Œ ํšจ๊ณผ ๋ณดํ†ต ํšจ๊ณผ ์—†์Œ ํ•ฉ๊ณ„ ์‹ ์•ฝ 45 25 30 100 ๊ธฐ์กด ์•ฝ 30 30 40 100 ํ•ฉ๊ณ„ 75 55 70 200 ๊ธฐ์กด ์•ฝ๊ณผ ์‹ ์•ฝ์˜ ํšจ๊ณผ 09. ์ ํ•ฉ๋„ ๊ฒ€์ • ์ ํ•ฉ๋„ ๊ฒ€์ •์€ ์ถ”์ถœํ•œ ํ‘œ๋ณธ๋ถ„ํฌ๊ฐ€ ๊ท€๋ฌด๊ฐ€์„ค ๋ถ„ํฌ์™€ ๋™์ผ์„ฑ์„ ๊ฒ€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. 1. ์นด์ด์ œ๊ณฑ ๊ฒ€์ • ์ ํ•ฉ๋„ ๊ฒ€์ •(goodness of fit test)์€ ์ธก์ • ์ž๋ฃŒ๋‚˜ ๊ด€์ธก ์ž๋ฃŒ๊ฐ€ ํŠน์ • ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋Š”์ง€ ๊ฒ€์ •ํ•œ๋‹ค. ๋ฉ˜๋ธ์˜ ์œ ์ „๋ฒ•์ง 9:3:3:1์ด ์˜ฌ๋ฐ”๋ฅธ์ง€ ํŒ๋‹จํ•˜๋ ค๊ณ  ๊ฐ•๋‚ญ์ฝฉ์„ ์žฌ๋ฐฐํ•˜๊ณ  ์ข…๋ฅ˜ ๋ณ„๋กœ ๋‚˜๋ˆˆ ๊ฒฐ๊ณผ์ด๋‹ค. ๋ชจ์–‘๊ณผ ์ƒ‰ ๋‘ฅ๊ธ€๊ณ  ํ™ฉ์ƒ‰ ๋‘ฅ๊ธ€๊ณ  ๋…น์ƒ‰ ๋ˆ„๋ฆ„ ์ง€๊ณ  ํ™ฉ์ƒ‰ ์ฃผ๋ฆ„์ง€๊ณ  ๋…น์ƒ‰ ํ•ฉ๊ณ„ ๊ด€์ธก ๊ฐ’ 107 38 45 10 200 ๊ธฐ๋Œ“๊ฐ’ 16 200 115.5 16 200 37.5 16 200 37.5 16 200 12.5 200 ๋ฉ˜๋ธ ๋ฒ•์น™ ์ผ์ฃผ์ผ ์ „ 2๊ฐœ ์ •๋‹น ์ง€์ง€์œจ์ด 7:3์ด๋ผ๊ณ  ์•Œ๋ ค์กŒ๋Š”๋ฐ, ์ด ๋น„์œจ์ด ์ง€์†๋˜๋Š”์ง€ ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. A B ํ•ฉ๊ณ„ 60 40 100 ์ •๋‹น ์ง€์ง€์œจ ๋น„์œจ์— ๋Œ€ํ•œ ์ผ๋ฐ˜ํ™”๋œ ๊ฒฝ์šฐ๋ฅผ ๋‹คํ•ญ๋ถ„ํฌ๋ผ๊ณ  ํ•˜๋ฉฐ ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ( 1 x, , k ) n x x โ‹ฏ k 1 1 2 2 p x ๋‹ค์Œ์€ ๋‹คํ•ญ๋ถ„ํฌ ์„ฑ์งˆ์ด๋‹ค. ( ) n i a ( ) n i ( โˆ’ i ) o ( i X) โˆ’ p p . ( โ‰  ) ๋‹คํ•ญ๋ถ„ํฌ์—์„œ ํ™•๋ฅ ์€ ๋งŽ์€ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•˜์—ฌ ์ง์ ‘ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ณ  ์นด์ด ์ œ๊ณฑ( 2 ) ๋ถ„ํฌ๋กœ ๊ณ„์‚ฐํ•œ ํ™•๋ฅ  ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค. 2. Kolmogorov-Smirnov ๊ฒ€์ • ์ž„์˜ํ‘œ๋ณธ 1 X, , n ์— ๋Œ€ํ•œ ๊ฒฝํ—˜์  ๋ถ„ํฌํ•จ์ˆ˜ n ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. n ( ) number of ( i x ) = n i 1 1 [ โˆž x ] ( i ) ์—ฌ๊ธฐ์„œ [ โˆž x ] ( i ) ๋Š” ์ง€์‹œํ•จ ์ˆ˜๋กœ i x ์ด๋ฉด 1 ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 0์ด๋‹ค. ๋ˆ„์ ๋ถ„ํฌํ•จ์ˆ˜ ( ) ์— ๋Œ€ํ•œ Kolmogorov-Smirnov ํ†ต๊ณ„๋Ÿ‰์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. n sup | n ( ) F ( ) ๋ถ„ํฌ ์ ํ•ฉ์„ฑ์— ๋Œ€ํ•œ ๊ท€๋ฌด๊ฐ€์„ค 0 X โˆผ i F i 1 2 โ‹ฏ n ์ด ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ D = sup | n ( ) F ( )์ด๋‹ค. ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ ๋ถ„ํฌํ•จ์ˆ˜๋Š” Pr ( D โ‰ค ) 1 2 k 1 ( 1 ) โˆ’ e 2 2 2 2 x k 1 e ( k 1 ) ฯ€ / 3. Shapior-Wilk ๊ฒ€์ • Shapior-Wilk ๊ฒ€์ •์€ ํ™•๋ฅ  ํ‘œ๋ณธ 1 X, , n ์ด ์ •๊ทœ๋ถ„ํฌ ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœํ•œ ์ž๋ฃŒ์ธ์ง€ ๊ฒ€์ •ํ•œ๋‹ค. ์ •๊ทœ๋ถ„ํฌ ์ ํ•ฉ์„ฑ์— ๋Œ€ํ•œ ๊ท€๋ฌด๊ฐ€์„ค 0 X โˆผ i N ( , 2 ) i 1 2 โ‹ฏ n ์ด๋ฉฐ ๊ฐ€์„ค๊ฒ€์ •์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ = ( i 1 a x ( ) ) โˆ‘ = n ( i x)์ด๊ณ  ( ) : ์ˆœ์„œ ํ†ต๊ณ„๋Ÿ‰์œผ๋กœ ํ‘œ๋ณธ์—์„œ ๋ฒˆ์งธ๋กœ ์ž‘์€ ๊ฐ’์ด๋‹ค. ยฏ : ํ‘œ๋ณธํ‰๊ท  i ( 1 โ€ฆ a) m V 1์ด๋ฉฐ๋Š” ๋ฒกํ„ฐ ๋†ˆ(norm)์œผ๋กœ = V 1 | ( T โˆ’ V 1 ) / 10. ์ž๋ฃŒ ์ด ์žฅ์€ ์ž๋ฃŒ ์ข…๋ฅ˜์™€ ์ž๋ฃŒ ์š”์•ฝ์— ๊ด€ํ•˜์—ฌ ์‚ดํŽด๋ณธ๋‹ค. 1. ์ž๋ฃŒ ์ข…๋ฅ˜ ์ž๋ฃŒ ์ข…๋ฅ˜ ์ž๋ฃŒ๋Š” ์–ด๋–ค ํ˜„์ƒ์„ ์„ค๋ช…ํ•˜๋ ค๊ณ  ์ˆ˜์น˜์  ํŠน์„ฑ์ด๋‚˜ ์‚ฌ์‹ค์  ํŠน์„ฑ์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. ์ž๋ฃŒ ํ˜•ํƒœ ์–‘์  ์ž๋ฃŒ(quantitative data) : ์ž๋ฃŒ์˜ ๊ฐ’์ด ํฌ๊ธฐ์— ๊ด€์‹ฌ์ด ์žˆ๋Š” ์ž๋ฃŒ(์—ฐ์†ํ˜• ์ž๋ฃŒ, ์ด์‚ฐํ˜• ์ž๋ฃŒ) ์งˆ์  ์ž๋ฃŒ(qualitative data) : ์ž๋ฃŒ์˜ ๊ฐ’์ด ํฌ๊ธฐ๊ฐ€ ์•„๋‹ˆ๋ผ ๋‚ด์šฉ์— ๊ด€์‹ฌ์ด ์žˆ๋Š” ์ž๋ฃŒ(๋ฒ”์ฃผํ˜• ์ž๋ฃŒ) ์ž๋ฃŒ ์ธก์ • ์ฒ™๋„ ๋ช…๋ชฉ ์ฒ™๋„(nominal scale) : ์–ด๋Š ์ง‘๋‹จ์— ์†ํ•˜๋Š”์ง€ ์‚ฌ์šฉํ•˜๋Š” ์ฒ™๋„ ์ˆœ์œ„ ์ฒ™๋„(ordinal scale) : ์ธก์ • ๋Œ€์ƒ์ด ์„œ์—ด๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ฒ™๋„ ๊ตฌ๊ฐ„ ์ฒ™๋„(interval scale) : ์ธก์ • ๋Œ€์ƒ์˜ ์–‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ฒ™๋„๋กœ ๋น„์œจ ๊ด€๊ณ„๊ฐ€ ์˜๋ฏธ ์—†์Œ. ์ ˆ๋Œ€ 0์ด ์กด์žฌํ•˜์ง€ ์•Š์Œ.(์˜ˆ : ์˜จ๋„, ์‹œ๊ฐ„ ๋“ฑ) ๋น„์œจ ์ฒ™๋„(ratio scale) : ๊ตฌ๊ฐ„ ์ฒ™๋„์˜ ํŠน์„ฑ์„ ํฌํ•จํ•˜๋ฉด์„œ ๋น„์œจ ๊ด€๊ณ„๊ฐ€ ์˜๋ฏธ ์žˆ๋Š” ์ฒ™๋„. ์ ˆ๋Œ€ 0์ด ์กด์žฌํ•จ.(๊ธธ์ด, ๋ฌด๊ฒŒ ๋“ฑ) ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ(categorical data) ์ž๋ฃŒ์— ๋Œ€ํ•œ ์„ฑ๋ณ„์ด๋‚˜ ์ถœ์‹  ์ง€์—ญ๊ณผ ๊ฐ™์ด ์กฐ์‚ฌ๋Œ€์ƒ์— ์งˆ์ ์ธ ๋ถ„๋ฅ˜์˜ ๋ชฉ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง€๊ฑฐ๋‚˜ ์ •์ฑ…์— ๋Œ€ํ•œ ๋งŒ์กฑ๋„๋‚˜ ๋ณธ์ธ์ด ํ–‰๋ณตํ•œ ์ •๋„์™€ ๊ฐ™์ด ์ •ํ™•ํ•˜๊ฒŒ ๋ฐ˜์‘ ๊ฐ’์„ ์ธก์ •ํ•  ์ˆ˜ ์—†์–ด ์•„์ฃผ ๋งŒ์กฑ, ๋งŒ์กฑ, ๋ถˆ๋งŒ์กฑ, ์•„์ฃผ ๋ถˆ๋งŒ์กฑ ๋“ฑ๊ณผ ๊ฐ™์ด ๋ฒ”์ฃผ๋กœ์จ ์ธก์ •ํ•œ ์ž๋ฃŒ. ๋ช…๋ชฉ ์ž๋ฃŒ(nominal data) : ๋‚จ๋…€, ์ƒ‰์ƒ. ์ถœ์‹  ์ง€์—ญ ๋“ฑ. ์ˆœ์„œ์ž๋ฃŒ(ordinal data) : (์ƒ์ธต, ์ค‘์ธต, ํ•˜์ธต), (์†Œ๋…„, ์ค‘๋…„, ์žฅ๋…„, ๋…ธ๋…„), 3๋‹จ(5๋‹จ, 7๋‹จ) ์ฒ™๋„ ๋“ฑ. ์ธก์ •ํ˜• ์ž๋ฃŒ(measurement data, ์ˆ˜์น˜์  ์ž๋ฃŒ) ์ด์‚ฐํ˜• ์ž๋ฃŒ(discrete data) : ์ž๋…€์˜ ์ˆ˜, ๋ฐœ์ƒ ํšŒ์ˆ˜(๊ตํ†ต์‚ฌ๊ณ  ๋ฐœ์ƒ ์ˆ˜)์™€ ๊ฐ™์ด ์ž๋ฃŒ์˜ ๊ฐ’์ด ์…€ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์—ฐ์†ํ˜• ์ž๋ฃŒ(continuous data) : ์‹ ์žฅ, ์ฒด์ค‘, ์ˆ˜๋ช… ๋“ฑ๊ณผ ๊ฐ™์ด ์—ฐ์†์ ์ธ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ 2. ์ˆซ์ž๋กœ ์ž๋ฃŒ ์š”์•ฝ ์ˆซ์ž๋กœ ์ž๋ฃŒ ์š”์•ฝ ๋งŽ์€ ์ž๋ฃŒ๋ฅผ ๋ช‡ ๊ฐœ์˜ ์˜๋ฏธ(ๆ„ๅ‘ณ) ์žˆ๋Š” ์ˆ˜์น˜๋กœ ์š”์•ฝ ์ž๋ฃŒ์˜ ๋ถ„ํฌ์ƒํƒœ(ๅˆ†ๅธƒ็‹€ๆ…‹)๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋Š” ํ†ต๊ณ„๊ธฐ๋ฒ•(็ตฑ่จˆๆŠ€ๆณ•) ์‚ฌ์šฉ ์ค‘์‹ฌ ์œ„์น˜์˜ ์ธก๋„(ไธญๅฟƒๆธฌๅบฆ, measure of center) : ์–ด๋–ค ๊ฐ’์„ ์ค‘์‹ฌ(ไธญๅฟƒ)์œผ๋กœ ๋ถ„ํฌ(ๅˆ†ๅธƒ) ๋˜์–ด ์žˆ๋Š”์ง€ ๋‚˜ํƒ€๋‚ด๋Š” ์ธก๋„๋กœ ํ‰๊ท (ๅนณๅ‡, mean), ์ค‘์•™(ไธญๅคฎ) ๊ฐ’(median), ์ตœ๋นˆ(ๆœ€้ ป) ๊ฐ’(mode) ํผ์ง„ ์ •๋„์˜ ์ธก๋„(measure of dispersion) : ์ž๋ฃŒ๊ฐ€ ๊ฐ ์ค‘์‹ฌ ์œ„์น˜์—์„œ ์–ผ๋งˆ๋‚˜ ํฉ์–ด์ ธ ์žˆ๋Š”์ง€ ๋‚˜ํƒ€๋‚ด๋Š” ์ธก๋„๋กœ ๋ถ„์‚ฐ(ๅˆ†ๆ•ฃ, variance), ํ‘œ์ค€ํŽธ์ฐจ(ๆจ™ๆบ–ๅๅทฎ, standard deviation), ๋ฒ”์œ„(็ฏ„ๅœ, range), ์‚ฌ๋ถ„์œ„์ˆ˜ ๋ฒ”์œ„(ๅ››ๅˆ†ไฝๆ•ธ ็ฏ„ๅœ, interquartile range) ์ค‘์‹ฌ ์œ„์น˜(ไธญๅฟƒไฝ็ฝฎ) ํ‰๊ท (ๅนณๅ‡, mean, average) ๋ชจ๋“  ๊ด€์ธก ๊ฐ’์˜ ํ•ฉ์„ ์ž๋ฃŒ์˜ ๊ฐœ์ˆ˜๋กœ ๋‚˜๋ˆˆ ๊ฐ’์œผ๋กœ ์‚ฐ์ˆ ํ‰๊ท (็ฎ—่ก“ๅนณๅ‡, arithmetic average)์ด๋ผ๊ณ  ๋ถ€๋ฆ„ ๋ชจ ํ‰๊ท (ๆฏๅนณๅ‡, population mean)์€ ๋ชจ์ง‘๋‹จ ์ž๋ฃŒ์— ๋Œ€ํ•œ ํ‰๊ท ์ด๋ฉฐ, ๊ทธ ๊ฐ’์„ (mu)๋กœ ํ‘œ๊ธฐ ํ‘œ๋ณธํ‰๊ท (ๆจ™ๆœฌๅนณๅ‡, sample mean)์€ ํ‘œ๋ณธ์ง‘๋‹จ ์ž๋ฃŒ์— ๋Œ€ํ•œ ํ‰๊ท ์ด๋ฉฐ, ๊ทธ ๊ฐ’์„ โ€• (X bar)๋กœ ํ‘œ๊ธฐ ํ‰๊ท ์€ ๋ชจ๋“  ๊ด€์ธก ๊ฐ’์ด ๋ฐ˜์˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทน๋‹จ์ ์œผ๋กœ ํฌ๊ฑฐ๋‚˜ ์ž‘์€ ๊ฐ’(์ด์ƒ์ , ็•ฐๅธธ้ปž, outlier)์— ์˜ํ–ฅ์„ ๋งŽ์ด ๋ฐ›์Œ ์ ˆ์‚ฌ ํ‰๊ท (ๅˆ‡ๆจๅนณๅ‡, trimmed mean)์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ทน๋‹จ(ๆฅต็ซฏ, extreme) ์ ์ธ ๊ฐ’์˜ ์˜ํ–ฅ์„ ์ค„์ผ ์ˆ˜ ์žˆ์Œ ์ž๋ฃŒ์˜ ์„ฑ๊ฒฉ์— ๋”ฐ๋ผ ๊ธฐํ•˜ํ‰๊ท (ๅนพไฝ•ๅนณๅ‡, geometric mean), ์กฐํ™”ํ‰๊ท (่ชฟๅ’Œๅนณๅ‡, harmonic mean)์ด ์žˆ์Œ ๋ชจํ‰๊ท  ๋ชจ๋“  ์ž๋ฃŒ์˜ ํ•ฉ ์ž๋ฃŒ์˜ ๊ฐœ์ˆ˜ ํ‰ ( ) ๋ชจ๋“  ์ž๋ฃŒ์˜ ํ•ฉ ์ž๋ฃŒ์˜ ๊ฐœ์ˆ˜ x + + N ํ‘œ๋ณธํ‰๊ท  ๋ชจ๋“  ๊ด€์ธก ๊ฐ’์˜ ํ•ฉ ๊ด€์ธก ๊ฐ’์˜ ๊ฐœ์ˆ˜ ๋ณธ ๊ท  ( โ€• ) ๋ชจ๋“  ๊ด€์ธก ๊ฐ’์˜ ํ•ฉ ๊ด€์ธก ๊ฐ’์˜ ๊ฐœ์ˆ˜ x + + n } ๋ณด๊ธฐ : ํ‰๊ท  ๊ตฌํ•˜๊ธฐ ์–ด๋–ค ๊ณผ๋ชฉ์—์„œ 6๋ช…์˜ ํ•™์ƒ์˜ ์ ์ˆ˜๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํ‘œ๋ณธํ‰๊ท ์„ ๊ตฌํ•ด๋ณด์ž. 89 74 91 88 72 84 โ€• 89 74 91 88 72 84 = 83 ์ ˆ์‚ฌ ํ‰๊ท (ๅˆ‡ๆจๅนณๅ‡, trimmed mean) ๋„ˆ๋ฌด ํฐ ๊ฐ’๊ณผ ์ž‘์€ ๊ฐ’์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ž๋ฃŒ์— ๋Œ€ํ•œ ํ‰๊ท ์œผ๋กœ ๋ณดํ†ต ์ ˆ์‚ฌํ•œ ์ž๋ฃŒ์˜ ๋น„์œจ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด % ์ ˆ์‚ฌ ํ‰๊ท ์ด๋ผ๊ณ  ํ‘œํ˜„ํ•จ % ์ ˆ์‚ฌ ํ‰๊ท ์€ ์ž๋ฃŒ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ–ˆ์„ ๋•Œ ์ƒ์œ„(ไธŠไฝ) % ์˜ ์ž๋ฃŒ์™€ ํ•˜์œ„(ไธ‹ไฝ) % ์˜ ์ž๋ฃŒ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ž๋ฃŒ์˜ ํ‰๊ท  ๋ณด๊ธฐ : ์ ˆ์‚ฌ ํ‰๊ท  ํ”ผ๊ฒจ์Šค์ผ€์ดํŒ… ๊ฒฝ๊ธฐ์—์„œ 10๋ช…์˜ ์‹ฌ์‚ฌ์œ„์›(ๅฏฉๆŸปๅง”ๅ“ก)์ด ํ•œ ์„ ์ˆ˜์— ๋Œ€ํ•œ ์ฑ„์ (ๆŽก้ปž) ๊ฒฐ๊ณผ์ด๋‹ค. 10 9 10 9 10 9 10 9 10 2 10% ์ ˆ์‚ฌ ํ‰๊ท ์€ ์–ผ๋งˆ์ธ๊ฐ€? ์ตœ๊ณ ์  (ๆœ€้ซ˜้ปž) 10์ ๊ณผ ์ตœ์ €์ (ๆœ€ไฝŽ้ปž) 2์ ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ž๋ฃŒ 8๊ฐœ์˜ ํ‰๊ท ์ธ 9.5์ ์ด๋‹ค. ์‚ฌ๋ถ„์œ„์ˆ˜ ํ‰๊ท  (ๅ››ๅˆ†ไฝๆ•ธ ๅนณๅ‡, trimean) ์ž๋ฃŒ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ•œ ํ›„ 25 , 50 , 75์˜ ์œ„์น˜์— ์žˆ๋Š” ์ž๋ฃŒ์˜ ๊ฐ’์„ ๊ฐ๊ฐ ์ œ1์‚ฌ๋ถ„์œ„ ์ˆ˜, ์ œ2์‚ฌ๋ถ„์œ„ ์ˆ˜ ๋ฐ ์ œ3์‚ฌ๋ถ„์œ„ ์ˆ˜๋ผ๊ณ  ํ•จ ์ด๋“ค์€ ๊ฐ๊ฐ 1 Q ๋ฐ 3 ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์‚ฌ๋ถ„์œ„์ˆ˜ ํ‰๊ท ์€ ์ค‘์•™๊ฐ’๊ณผ ์‚ฐ์ˆ ํ‰๊ท  ์žฅ์ (้•ท้ปž)์„ ์ทจ(ๅ–) ํ•˜๊ณ ์ž ์ œ์•ˆ๋œ ๊ฒƒ์œผ๋กœ 1 2 2 Q 4 ์ค‘์•™๊ฐ’ (ไธญๅคฎๅ€ค, median) ์ „์ฒด ๊ด€์ธก ๊ฐ’์„ ํฌ๊ธฐ ์ˆœ์„œ(้ †ๅบ)๋กœ ๋ฐฐ์—ด(้…ๅˆ—) ํ•˜์˜€์„ ๊ฒฝ์šฐ ๊ฐ€์šด๋ฐ ์œ„์น˜ํ•˜๋Š” ๊ฐ’ ๊ด€์ธก ๊ฐ’์˜ ํฌ๊ธฐ๋ณด๋‹ค ๊ด€์ธก ๊ฐ’์˜ ์œ„์น˜(ไฝ็ฝฎ)๊ฐ€ ์ค‘์š” ๊ด€์ธก ๊ฐ’์˜ ๋ณ€ํ™”์— ๋ฏผ๊ฐํ•˜์ง€ ์•Š๋‹ค. ์ฆ‰ ๊ทน๋‹จ(ๆฅต็ซฏ, extreme) ์ ์œผ๋กœ ํฐ ๊ฐ’์ด๋‚˜ ์ž‘์€ ๊ฐ’(์ด์ƒ์ , ็•ฐๅธธ้ปž, outlier)์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์Œ ์ค‘์•™๊ฐ’ ๊ตฌํ•˜๊ธฐ ๊ด€์ธก ๊ฐ’์„ ํฌ๊ธฐ ์ˆœ์„œ๋กœ ๋ฐฐ์—ด ์ž๋ฃŒ์˜ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ด๋ฉด + 2 ๋ฒˆ์งธ ์ž๋ฃŒ๊ฐ’์ด ์ค‘์•™๊ฐ’ ์ž๋ฃŒ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ง์ˆ˜์ด๋ฉด 2 ๋ฒˆ์งธ ๊ด€์ธก ๊ฐ’๊ณผ 2 1 ๋ฒˆ์งธ ๊ด€์ธก ๊ฐ’ ์‚ฌ์ด์˜ ์ค‘๊ฐ„๊ฐ’์ด๋‚˜ ํ‰๊ท ์ด ์ค‘์•™๊ฐ’ ๋ณด๊ธฐ : ์ค‘์•™๊ฐ’ ์–ด๋–ค ๊ณผ๋ชฉ์—์„œ 6๋ช…์˜ ํ•™์ƒ์˜ ์ ์ˆ˜๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ค‘์•™๊ฐ’์„ ๊ตฌํ•ด๋ณด์ž. 89 74 91 88 72 84 ํ•™์ƒ๋“ค์˜ ์„ฑ์ ์„ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ดํ•˜๋ฉด 72 74 84 88 89 91์ด๊ณ  ๊ด€์ธก ๊ฐ’์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ง์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— 2 ๋ฒˆ์งธ ๊ด€์ธก ๊ฐ’ 84์™€ 2 1 ๋ฒˆ์งธ ๊ด€์ธก ๊ฐ’ 88์˜ ํ‰๊ท  86์ด ์ค‘์•™๊ฐ’์ž„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž๋ฃŒ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ผ ๋•Œ ์ค‘์•™๊ฐ’์€ ์–ผ๋งˆ์ธ๊ฐ€? 6.4 7.8 8.1 9.2 10.5 ์ค‘์•™๊ฐ’์€ + 2 3 ๋ฒˆ์งธ ๊ฐ’์ธ 8.1์ž„ ์ตœ๋นˆ๊ฐ’ (ๆœ€้ ปๆ•ธ, mode) ๊ด€์ธก ๊ฐ’ ์ค‘์—์„œ ๊ฐ€์žฅ ์ž์ฃผ ๋‚˜์˜ค๋Š” ๊ฐ’ ์—ฐ์†ํ˜• ์ž๋ฃŒ์—์„œ ๋„์ˆ˜๋ถ„ํฌํ‘œ๋กœ ์ž๋ฃŒ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ์ตœ๋Œ€์˜ ๋„์ˆ˜๋ฅผ ๊ฐ–๋Š” ๊ณ„๊ธ‰๊ตฌ๊ฐ„์˜ ์ค‘๊ฐ„๊ฐ’์„ ์ตœ๋นˆ๊ฐ’์œผ๋กœ ํ•จ ์ด์‚ฐํ˜• ์ž๋ฃŒ์˜ ๊ฒฝ์šฐ ์ตœ๋นˆ๊ฐ’์„ ๋Œ€ํ‘ฏ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•จ ๋ณด๊ธฐ : ์ตœ๋นˆ๊ฐ’ ๋‹ค์Œ ์ž๋ฃŒ์—์„œ ์ตœ๋นˆ๊ฐ’์€ ๋ฌด์—‡์ธ๊ฐ€? 2 5 5 3 5 2 ์œ„ ์ž๋ฃŒ์—์„œ 2๋Š” ๋‘ ๋ฒˆ, 3์€ ํ•œ ๋ฒˆ, 5๋Š” ์„ธ ๋ฒˆ์ด๋ฏ€๋กœ 5๊ฐ€ ์ตœ๋นˆ๊ฐ’์ž„ ํผ์ง„ ์ •๋„ ์ค‘์‹ฌ ์œ„์น˜์—์„œ ์–ผ๋งˆ๋‚˜ ํฉ์–ด์ ธ ์žˆ๋Š”์ง€ ๋‚˜ํƒ€๋‚ด๋Š” ์ธก๋„ ๋ถ„์‚ฐ(ๅˆ†ๆ•ฃ, variance), ํ‘œ์ค€ํŽธ์ฐจ(ๆจ™ๆบ–ๅๅทฎ, standard deviation) ํ‰๊ท ์—์„œ ํผ์ง„ ์ฒ™๋„๋กœ ์‚ฌ์šฉ ์ž๋ฃŒ์— ๋Œ€ํ•œ ํŽธ์ฐจ(deviation)์˜ ํ•ฉ์€ ํ•ญ์ƒ 0์ด๋ฏ€๋กœ ํ‰๊ท ์—์„œ ํฉ์–ด์ง์˜ ์ฒ™๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ ( โˆ‘ ( i X) โˆ‘ ( i ฮผ ) 0 ) ๋ชจ๋ถ„์‚ฐ(ๆฏๅˆ†ๆ•ฃ, population variance)์€ ํŽธ์ฐจๅๅทฎ์˜ ์ œ๊ณฑํ•ฉ 2 ํŽธ์ฐจ(ๅๅทฎ)์˜ ์ œ๊ณฑํ•ฉ = ( i ฮผ ) N ๋ชจ ํ‘œ์ค€ํŽธ์ฐจ(population standard deviation)}๋Š” ๋ชจ๋ถ„์‚ฐ์˜ ์–‘(้™ฝ)์˜ ์ œ๊ณฑ๊ทผ = 2 ํ‘œ๋ณธ ๋ถ„์‚ฐ(ๆจ™ๆœฌๅˆ†ๆ•ฃ, sample variance)์€ ํŽธ์ฐจ์˜ ์ œ๊ณฑํ•ฉ 2 ํŽธ์ฐจ์˜ ์ œ๊ณฑํ•ฉ โˆ’ = ( i X) n 1 ํ‘œ๋ณธํ‘œ์ค€ํŽธ์ฐจ๋Š” ํ‘œ๋ณธ ๋ถ„์‚ฐ์˜ ์–‘์˜ ์ œ๊ณฑ๊ทผ = 2 ํ‰๊ท ์ด ๊ฐ™๊ณ  ๋ถ„์‚ฐ์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ ํ‰๊ท ์ด ๋‹ค๋ฅด๊ณ  ๋ถ„์‚ฐ์ด ๊ฐ™์€ ๊ฒฝ์šฐ ๋ณด๊ธฐ : ๋ถ„์‚ฐ, ํ‘œ์ค€ํŽธ์ฐจ ์–ด๋–ค ๊ณผ๋ชฉ์—์„œ 6๋ช…์˜ ํ•™์ƒ์˜ ์ ์ˆ˜๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋ถ„์‚ฐ๊ณผ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ตฌํ•ด๋ณด์ž. 89 74 91 88 72 84 2 ( 89 83 ) + + ( 84 83 ) 6 1 65.6 = 65.6 8.099382693 ๋ฒ”์œ„(range) ๋ฒ”์œ„๋Š” (๊ด€์ธก ๊ฐ’ ์ค‘์—์„œ {์ตœ๋Œ“๊ฐ’}) - (๊ด€์ธก ๊ฐ’ ์ค‘์—์„œ ์ตœ์†Ÿ๊ฐ’) ๋ฒ”์œ„๋Š” ๊ฐ„ํŽธํ•˜๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ณ  ํ•ด์„์ด ์šฉ์ด ์–‘ ๋์ ์—์„œ ๊ฐ’์ด ๊ฒฐ์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๊ฐ„์˜ ๊ด€์ธก ๊ฐ’์„ ์•Œ ์ˆ˜ ์—†์Œ ๊ทน๋‹จ ์ ์œผ๋กœ ํฐ ๊ฐ’์ด๋‚˜ ์ž‘์€ ๊ฐ’(\textgt{์ด์ƒ์ })์— ๋งŽ์€ ์˜ํ–ฅ์„ ๋ฐ›์Œ ๋ณด๊ธฐ : ๋ฒ”์œ„ ์–ด๋–ค ๊ณผ๋ชฉ์—์„œ 6๋ช…์˜ ํ•™์ƒ์˜ ์ ์ˆ˜๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ž๋ฃŒ์˜ ๋ฒ”์œ„๋ฅผ ๊ตฌํ•ด๋ณด์ž. 89 74 91 88 72 84 ๋ฒ”์œ„ = ์ตœ๋Œ“๊ฐ’ - ์ตœ์†Ÿ๊ฐ’ = 91 - 72 = 19 ๋ฐฑ๋ถ„์œ„์ˆ˜(็™พๅˆ†ไฝๆ•ธ, percentile) ๋ฐฑ๋ถ„์œ„์ˆ˜๋Š” ๊ด€์ธก ๊ฐ’์„ ํฌ๊ธฐ ์ˆœ์„œ๋กœ ๋ฐฐ์—ดํ•˜์˜€์„ ๋•Œ ( โ‰ค p ) p and P ( โ‰ฅ p ) 1 p ๋ฅผ ๋งŒ์กฑํ•˜๋Š” p ๋ฐฑ๋ถ„์œ„์ˆ˜๋Š” 100 p ๋ฐฑ๋ถ„์œ„ ์ˆ˜๋กœ ํ‘œํ˜„ ๋ฐฑ๋ถ„์œ„์ˆ˜๋Š” ๊ด€์ธก ๊ฐ’์˜ ๊ทœ๋ชจ์—๋Š” ์ƒ๊ด€์—†๊ณ , ๊ด€์ธก ๊ฐ’์˜ ์ˆœ์„œ์—๋งŒ ์ƒ๊ด€์žˆ์Œ ๋ฐฑ๋ถ„์œ„์ˆ˜ ๊ตฌํ•˜๋Š” ๊ณผ์ • ๊ด€์ธก ๊ฐ’์„ ์ž‘์€ ์ˆœ์„œ๋กœ ๋ฐฐ์—ดํ•จ ๊ด€์ธก ๊ฐ’์˜ ๊ฐœ์ˆ˜(n)์— ๋ฐฑ๋ถ„์œจ(p)๋ฅผ ๊ณฑํ•จ ร— ๊ฐ€ ์ •์ˆ˜(ๆ•ดๆ•ธ)์ด๋ฉด, ร— ๋ฒˆ์งธ๋กœ ์ž‘์€ ๊ฐ’๊ณผ ร— + ๋ฒˆ์งธ๋กœ ์ž‘์€ ๊ด€์ธก ๊ฐ’์˜ ํ‰๊ท ์ด 100 p ๋ฐฑ๋ถ„์œ„ ์ˆ˜์ž„ ร— ๊ฐ€ ์ •์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด, ร—์˜ ์ •์ˆ˜ ๋ถ€๋ถ„์— 1์„ ๋”ํ•œ ๊ฐ’ m์„ ๊ตฌํ•˜๊ณ  m ๋ฒˆ์งธ ์ž‘์€ ๊ด€์ธก ๊ฐ’์„ 100 p ๋ฐฑ๋ถ„์œ„ ์ˆ˜์ž„ ์‚ฌ๋ถ„์œ„์ˆ˜(ๅ››ๅˆ†ไฝๆ•ธ, quartile) ๊ด€์ธก ๊ฐ’์„ ํฌ๊ธฐ ์ˆœ์„œ๋กœ ๋ฐฐ์—ดํ•˜์˜€์„ ๋•Œ ์ „์ฒด๋ฅผ ์‚ฌ๋“ฑ๋ถ„ํ•œ ๊ฐ’ ์ œ1 ์‚ฌ๋ถ„์œ„์ˆ˜ : 1 ์ œ25๋ฐฑ ๋ถ„ ์œ„์ˆ˜ ์ œ2 ์‚ฌ๋ถ„์œ„์ˆ˜ : 2 ์ œ50๋ฐฑ ๋ถ„ ์œ„์ˆ˜ = ์ค‘์•™๊ฐ’ ์ œ3 ์‚ฌ๋ถ„์œ„์ˆ˜ : 3 ์ œ75๋ฐฑ ๋ถ„ ์œ„์ˆ˜ ์‚ฌ๋ถ„์œ„์ˆ˜ ๋ฒ”์œ„(interquartile range, IQR) : ์ œ3 ์‚ฌ๋ถ„์œ„์ˆ˜ - ์ œ1 ์‚ฌ๋ถ„์œ„์ˆ˜ ์‚ฌ๋ถ„์œ„์ˆ˜ ๋ฒ”์œ„๋Š” ์ค‘์•™๊ฐ’์„ ์ค‘์‹ฌ ์ฒ™๋„๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ํผ์ง„ ์ •๋„์˜ ์ฒ™๋„๋กœ ์‚ฌ์šฉ ๋ณด๊ธฐ : ์‚ฌ๋ถ„์œ„์ˆ˜ ์„œ์šธ์˜ ํ•œ ์ „์ฒ ์—ญ์—์„œ ์ธ์ฒœ์˜ ํ•œ ์ „์ฒ ์—ญ๊นŒ์ง€ ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•œ ์ž๋ฃŒ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค(๋‹จ์œ„ : ๋ถ„). ์ด ์ž๋ฃŒ์—์„œ ์ œ50 ๋ฐฑ๋ถ„์œ„ ์ˆ˜์ธ ์ค‘์•™๊ฐ’๊ณผ ์ œ20 ๋ฐฑ๋ถ„์œ„์ˆ˜๋ฅผ ๊ตฌํ•˜์ž. 42 40 38 37 43 39 78 38 45 44 40 38 41 35 31 44 ์ด ์ž๋ฃŒ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์žฌ๋ฐฐ์—ดํ•˜๋ฉด 31 35 37 38 38 38 39 40 40 41 42 43 44 44 45 78 ์ด๊ณ  ๊ด€์ธก ๊ฐ’์˜ ๊ฐœ์ˆ˜๊ฐ€ 16์ด๋ฏ€๋กœ ์ œ50 ๋ฐฑ๋ถ„์œ„์ˆ˜๋Š” 16 0.5 8 ์ด๋ฏ€๋กœ 8 ๋ฒˆ์งธ ์ž‘์€ ๊ฐ’ 40๊ณผ 9 ๋ฒˆ์งธ ์ž‘์€ ๊ฐ’ 40์˜ ํ‰๊ท ์ธ 40์ด๊ณ , ์ œ20 ๋ฐฑ๋ถ„์œ„์ˆ˜๋Š” 16 0.2 3.2 ์ด๋ฏ€๋กœ (3+1) ๋ฒˆ์งธ ์ž‘์€ ๊ฐ’์ธ 38์ด๋‹ค. ๋Œ€์นญ์„ฑ - ์™œ๋„(ๆญชๅบฆ, skewness) ๋ถ„ํฌ(ๅˆ†ๅธƒ)์˜ ๋Œ€์นญ์„ฑ(ๅฐ็จฑๆ€ง)์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ฒ™๋„ 3 E [ ( โˆ’ ฯƒ ) ] ฮผ ฯƒ = [ ( โˆ’ ) ] ( [ ( โˆ’ ) ] ) / = 3 2 / ์ž๋ฃŒ๊ฐ€ ์˜ค๋ฅธ์ชฝ์— ๋งŽ์€ ๊ฒฝ์šฐ(skewed to the left, ์™ผ์ชฝ์œผ๋กœ ๋’คํ‹€๋ฆผ(ๆญช))๋Š” ์™œ๋„ ๊ฐ€ ์Œ์ˆ˜(้™ฐๆ•ธ, negative skew; 3 0 )๋Š” ํ‰๊ท  ์ค‘์•™๊ฐ’ ์ตœ๋นˆ๊ฐ’ ์ˆœ์ด๋‹ค. ์ž๋ฃŒ๊ฐ€ ์™ผ์ชฝ์— ๋งŽ์€ ๊ฒฝ์šฐ(skewed to the right, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋’คํ‹€๋ฆผ(ๆญช))๋Š” ์™œ๋„ ๊ฐ€ ์–‘์ˆ˜(้™ฝๆ•ธ, positive skew; 3 0 )๋Š” ์ตœ๋นˆ๊ฐ’ ์ค‘์•™๊ฐ’ ํ‰๊ท  ์ˆœ์ด๋‹ค. ์ž๋ฃŒ๊ฐ€ ๋Œ€์นญ์ธ ๊ฒฝ์šฐ๋Š” ์™œ๋„ ๊ฐ€ 0( 3 0 ) ๋ณด๊ธฐ : ์™œ๋„ ์„ธ ๋ถ„ํฌ์˜ ํ†ต๊ณ„๋Ÿ‰์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๊ฐ ๋ถ„ํฌ์˜ ๋Œ€์นญ์„ฑ์— ๋Œ€ํ•˜์—ฌ ์„ค๋ช…ํ•˜๋ผ. ํ‰๊ท  ์ค‘์•™๊ฐ’ ์ตœ๋นˆ๊ฐ’ ์™œ๋„ ๋ถ„ํฌ 1 29 26 20 3 0 ๋ถ„ํฌ 2 71 74 80 3 0 ๋ถ„ํฌ 3 50 50 50 3 0 ํ‘œ ์กฑํ•จ - ์ฒจ๋„(ๅฐ–ๅบฆ, kurtosis) ๋ถ„ํฌ(ๅˆ†ๅธƒ)์˜ ๋พฐ์กฑํ•จ(ๅฐ–)์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ฒ™๋„๋กœ ๋ถ„ํฌ๊ฐ€ ๋พฐ์กฑํ•˜๋ฉด ํ‰๊ท ์— ์ž๋ฃŒ๊ฐ€ ๋งŽ์ด ๋ชฐ๋ ค์žˆ๊ณ , ๋พฐ์กฑํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋ฃŒ๊ฐ€ ํ‰๊ท ์—์„œ ๋ฉ€๋ฆฌ ์žˆ์Œ 4 E [ ( โˆ’ ฯƒ ) ] 3 ฮผ ฯƒ โˆ’ = ( โˆ’ ) ( [ ( โˆ’ ) ] ) โˆ’ ์ž๋ฃŒ๊ฐ€ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋ณด๋‹ค ํ‰๊ท ์—์„œ ํฉ์–ด์ ธ ์žˆ์Œ(ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋ณด๋‹ค ๋พฐ์กฑํ•˜์ง€ ์•Š์Œ; 4 0 ) ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋ณด๋‹ค ์ž๋ฃŒ๊ฐ€ ํ‰๊ท ์— ๋งŽ์ด ๋ชฐ๋ ค์žˆ์Œ(ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋ณด๋‹ค ๋พฐ์กฑํ•จ; 4 0 ) ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ์ฒจ ๋„๋Š” 0( 4 0 ) ๋ณด๊ธฐ : ์ฒจ๋„ ์„ธ ๋ถ„ํฌ์˜ ํ†ต๊ณ„๋Ÿ‰์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๊ฐ ๋ถ„ํฌ์˜ ๋พฐ์กฑํ•จ์— ๋Œ€ํ•˜์—ฌ ์„ค๋ช…ํ•˜๋ผ. ํ‰๊ท  ํ‘œ์ค€ํŽธ์ฐจ ์ฒจ๋„ ๋ถ„ํฌ 1 0 0.5 4 0 ๋ถ„ํฌ 2 0 1 4 0 ๋ถ„ํฌ 3 0 2 4 0 ๋ณ€๋™๊ณ„์ˆ˜ ์ค‘์‹ฌ ์œ„์น˜๋‚˜ ๋‹จ์œ„๊ฐ€ ๋‹ค๋ฅธ ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ถ„ํฌ์˜ ์„œ๋กœ ์ƒ๋Œ€์ ์ธ ํผ์ง„ ์ •๋„์˜ ์ธก๋„ ๋ณ€๋™๊ณ„์ˆ˜ = ํ‘œ์ค€ํŽธ์ฐจ ํ‘œ๋ณธํ‰๊ท  ์ค€ ์ฐจ ๋ณธ ๊ท  100 ๋ณด๊ธฐ : ๋ณ€๋™๊ณ„์ˆ˜ ํ•œ ํˆฌ์ž์ž๊ฐ€ A ํšŒ์‚ฌ์˜ ์ฃผ์‹๊ณผ B ํšŒ์‚ฌ์˜ ์ฃผ์‹ ์ค‘ ํ•˜๋‚˜๋ฅผ ๋งค์ž…ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ 6์ผ ๋™์•ˆ ์กฐ์‚ฌํ•œ ๋‘ ํšŒ์‚ฌ์˜ ๋งˆ๊ฐ ๊ฐ€๊ฒฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. A ํšŒ์‚ฌ ์ฃผ์‹๊ณผ B ํšŒ์‚ฌ ์ฃผ์‹ ๊ฐ€๊ฒฉ์˜ ํ‘œ๋ณธํ‰๊ท , ํ‘œ์ค€ํŽธ์ฐจ, ๋ณ€๋™๊ณ„์ˆ˜ ๋“ฑ์„ ๊ตฌํ•˜์—ฌ ํผ์ง„ ์ •๋„๋ฅผ ๋น„๊ตํ•˜์ž. ๋‚ ์งœ A ํšŒ์‚ฌ B ํšŒ์‚ฌ ๋‚ ์งœ A ํšŒ์‚ฌ B ํšŒ์‚ฌ 1 76,300 6,400 4 77,200 6,900 2 77,400 7,000 5 76,900 7,300 3 77,900 7,400 6 78,800 7,600 โ€• = 77 417 โ€• = 7100 A 861 B 429 V = 1.11 V = 6.04 11. ํ™•๋ฅ ๋ถ„ํฌ ํ™•๋ฅ  ํ™•๋ฅ ์˜ ๊ณต๋ฆฌ(็ขบ์˜ ๅ…ฌ็†, probability axioms) ( ) 1 ( ) P ( 1 ) โ‹ฏ P ( n ) ( i A = , i j ) โ‰ค ( ) 1 ์—ฌ๊ธฐ์„œ i A = , i j ๋Š” ์„œ๋กœ ๋ฐฐ๋ฐ˜(disjoint)์ด๋ผ ํ•˜๋ฉฐ ์ด๋‹ค. ํ™•๋ฅ  ๊ณต๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‘ ์‚ฌ๊ฑด,์˜ ํ•ฉ์€ ์ด๋ฉฐ ( โˆช ) P ( ) P ( ) P ( โˆฉ ) ์ด๋‹ค. ( โˆช ) P ( ) P ( c B ) ( ) P ( โˆฉ ) P ( c B ) ( c B ) P ( ) P ( โˆฉ ) P ( โˆช ) P ( ) P ( ) P ( โˆฉ ) ํ™•๋ฅ  ๊ณต๊ฐ„ ์ž„์˜ ์‹คํ—˜(ไปปๆ„ๅฏฆ้ฉ—, random experiment) : ์‹คํ—˜ ๊ฒฐ๊ณผ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ ์ง€ ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๋Š” ์‹คํ—˜ ํ‘œ๋ณธ๊ณต๊ฐ„(ๆจ™ๆœฌ็ฉบ้–“, sample space : ) : ํ•œ ์‹คํ—˜์—์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฐ๊ณผ๋“ค์˜ ๋ชจ์ž„ ์‚ฌ์ƒ, ์‚ฌ๊ฑด(ไบ‹่ฑก, ไบ‹ไปถ, event : ) : ํ‘œ๋ณธ๊ณต๊ฐ„์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ์–ด๋–ค ํŠน์„ฑ์„ ๊ฐ–๋Š” ๊ฒฐ๊ณผ๋“ค์˜ ๋ชจ์ž„. โˆˆ์ด๊ณ  โˆˆ, โˆˆ๋ผ๊ณ  ํ•˜๋ฉด c F B โˆˆ ์™€ โˆฉ โˆˆ, โˆช โˆˆ ์„ ๋งŒ์กฑํ•จ ๊ทผ์› ์‚ฌ์ƒ(ๆ นๆบไบ‹่ฑก, elementary outcomes) : ํ‘œ๋ณธ๊ณต๊ฐ„์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐœ๊ฐœ์˜ ๊ฒฐ๊ณผ( 1 ฯ‰, . ) ํ™•๋ฅ (็ขบ, probability; ( ) ) : ๋™์ผํ•œ ์กฐ๊ฑด ํ•˜์—์„œ ํ•œ ๊ฐ€์ง€ ์‹คํ—˜์„ ๋ฐ˜๋ณตํ•  ๋•Œ ๊ทธ ์‚ฌ์ƒ์ด ์ผ์–ด๋‚˜๋ฆฌ๋ผ๊ณ  ์˜ˆ์ƒ๋˜๋Š” ํšŸ์ˆ˜์˜ ๋น„์œจ( ( ) ) ํ™•๋ฅ ๊ณต๊ฐ„ ์˜ˆ ๋™์ „์„ ํ•œ ๋ฒˆ ๋˜์ง€๋Š” ๊ฒฝ์šฐ ํ‘œ๋ณธ๊ณต๊ฐ„ : = , ์‚ฌ๊ฑด : = , , , , ํ™•๋ฅ  [ ] P [ โˆฉ ] 0 [ ] 0.5 [ ] 0.5 [ , ] P [ โˆช ] 1 ํ‘œ๋ณธ๊ณต๊ฐ„ ์˜ˆ ์–ด๋Š ์ฃผ๋ง ์˜คํ›„ ๊ฒฝ๋ถ€๊ณ ์†๋„๋กœ์ƒ์—์„œ ๋ฒ„์Šค์ „์šฉ์ฐจ๋กœ์ œ๋ฅผ ์œ„๋ฐ˜ํ•˜๋Š” ์ฐจ์˜ ๋Œ€์ˆ˜๋ฅผ ์กฐ์‚ฌํ•œ๋‹ค. ์ด๋•Œ ์กฐ์‚ฌ๋Œ€์ƒ์˜ ์Šน์šฉ์ฐจ๊ฐ€ 200๋Œ€ ์ผ ๋•Œ ํ‘œ๋ณธ๊ณต๊ฐ„๊ณผ 50 ์ดˆ๊ณผ์˜ ์ฐจ๋“ค์ด ์œ„๋ฐ˜ํ•˜๊ฒŒ ๋˜๋Š” ์‚ฌ์ƒ( )์„ ๊ทผ์›์‚ฌ์ƒ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ํ‘œ๋ณธ๊ณต๊ฐ„ : = , , , , 200 ์‚ฌ์ƒ : = 101 โ‹ฏ 200 ์ž„์˜ ์‹คํ—˜(ไปปๆ„ๅฏฆ้ฉ—, random experiment) ๊ฒฐ๊ณผ ํ‘œ๋ณธ๊ณต๊ฐ„(ๆจ™ๆœฌ็ฉบ้–“, sample space)์˜ ๊ทผ์›์‚ฌ์ƒ(ๆ นๅ…ƒไบ‹่ฑก, elementary event)๋“ค ํ‘œํ˜„ ๋™์ „์„ ๋‘ ๋ฒˆ ๋˜์ง„ ๊ฒฝ์šฐ ๊ทผ์›์‚ฌ์ƒ์€ { H H, H T } โ‹… ๋ฐ˜์„ ๋ฌป๋Š” ์—ฌ๋ก ์กฐ์‚ฌ์—์„œ ๊ทผ์›์‚ฌ์ƒ์€ ์ฐฌ์„ฑ ๋ฐ˜๋Œ€ { ์„ฑ ๋ฐ˜ } ์–ด๋–ค ๋„์‹œ์—์„œ ์ผ์ฃผ์ผ ๋™์•ˆ ๋ฐœ์ƒํ•œ ๊ตํ†ต์‚ฌ๊ณ ์˜ ํšŸ์ˆ˜๋Š” { , , , } ์‹œํ–‰ ๊ฒฐ๊ณผ(ๆ–ฝ่กŒ็ตๆžœ)๋ณด๋‹ค ์‹œํ–‰ ๊ฒฐ๊ณผ์˜ ์ˆ˜์น˜์  ํ•จ์ˆ˜์— ๊ด€์‹ฌ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ฃผ์‚ฌ์œ„๋ฅผ ๋‘ ๋ฒˆ ๋˜์ง„ ๊ฒฝ์šฐ ๋ˆˆ์˜ ํ•ฉ์€ { , , , 12 } ๋™์ „์„ ์—ด ๋ฒˆ ๋˜์ง„ ๊ฒฝ์šฐ ์•ž๋ฉด์ด ๋‚˜์˜จ ์ˆ˜๋Š” { , , , 10 } ํ™•๋ฅ ๋ณ€์ˆ˜ ํ™•๋ฅ ๋ณ€์ˆ˜(็ขบ่ฎŠๆ•ธ, random variable)๋Š” ๊ฐ๊ฐ์˜ ๊ทผ์›์‚ฌ์ƒ๋“ค์— ์‹ค์ˆซ๊ฐ’์„ ๋Œ€์‘์‹œํ‚ค๋Š” ํ•จ์ˆ˜๋กœ ์˜๋ฌธ ๋Œ€๋ฌธ์ž, , ๋“ฑ์œผ๋กœ ํ‘œ์‹œ :๊ณต์ •ํ•œ ๋™์ „์„ ์„ธ ๋ฒˆ ๋˜์ง„ ๊ฒฝ์šฐ ์•ž๋ฉด ์ˆ˜๋Š” ๋™์ „์˜ ์•ž๋ฉด,๋Š” ๋™์ „์˜ ๋’ท๋ฉด์„ ๋‚˜ํƒ€๋ƒ„ ์‹คํ—˜ ๊ฒฐ๊ณผ(๊ทผ์›์‚ฌ์ƒ) ๋Œ€์‘๋˜๋Š” ๊ฐ’ HHH 3 HHT 2 HTH 2 HTT 1 THH 2 THT 1 TTH 1 TTT 0 ํ™•๋ฅ ๋ณ€์ˆ˜ ํŠน์„ฑ ํ•˜๋‚˜์˜ ๊ทผ์›์‚ฌ์ƒ์—๋Š” ์˜ค์ง ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋Œ€์‘ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ทผ์›์‚ฌ์ƒ์ด ๊ฐ™์€์˜ ๊ฐ’์— ๋Œ€์‘๋  ์ˆ˜ ์žˆ์Œ ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๊ฐ’์—์„œ ์—ญ์œผ๋กœ ๋Œ€์‘ํ•˜๋Š” ์‚ฌ์ƒ์€ ๋ฐ˜๋“œ์‹œ ์กด์žฌ ์ด์‚ฐํ™•๋ฅ ๋ณ€์ˆ˜ ์ด์‚ฐํ™•๋ฅ ๋ณ€์ˆ˜(ๆ•ฃ็ขบ่ฎŠๆ•ธ, discrete random variable)๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜ ๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ๊ฐ’๋“ค์ด ์œ ํ•œ๊ฐœ(ๆœ‰้™, finite) ์ฃผ์‚ฌ์œ„ ๋ˆˆ์˜ ์ˆ˜ {1, 2, 3, 4, 5, 6} ๊ณต์ •ํ•œ ๋™์ „์„ 3 ๋ฒˆ ๋˜์ง„ ๊ฒฝ์šฐ ์•ž๋ฉด ์ˆ˜ {0, 1, 2, 3 } ๋ฌดํ•œ ๊ฒŒ์ด๋‚˜ ์…€ ์ˆ˜ ์žˆ๋Š”(countably infinite) ๊ฒฝ์šฐ ์ž์—ฐ์ˆ˜ ์ง‘ํ•ฉ {1, 2, } ์ •์ˆ˜ ์ง‘ํ•ฉ { ,-2, -1, 0, 1, 2, } ์—ฐ์†ํ™•๋ฅ ๋ณ€์ˆ˜ ์—ฐ์†ํ™•๋ฅ ๋ณ€์ˆ˜(็บŒ็ขบ่ฎŠๆ•ธ, continuous random variable)๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜ ๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ๊ฐ’๋“ค์ด ๋ฌดํ•œ ๊ฐœ์ด๋ฉฐ ์…€ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ์ค‘ํ™”์š”๋ฆฌ ๋ฐฐ๋‹ฌ์‹œ๊ฐ„ [0, ) ์–ด๋–ค ๊ธฐ๊ณ„์˜ ์ธก์ •์˜ค์ฐจ(ๆธฌๅฎš่ชคๅทฎ) ( โˆž โˆž ) ํ™•๋ฅ ๋ถ„ํฌ ํ™•๋ฅ ๋ถ„ํฌ(็ขบๅˆ†ๅธƒ, probability distribution)๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ–๋Š” ๊ฐ’๋“ค๊ณผ ๊ทธ์— ๋Œ€์‘ํ•˜๋Š” ํ™•๋ฅ  ๊ฐ’์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์œผ๋กœ ํ‘œ๋‚˜ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. ํ™•๋ฅ ๋ถ„ํฌ ์˜ˆ ๊ณต์ •ํ•œ ๋™์ „์„ ์„ธ ๋ฒˆ ๋˜์ง„ ๊ฒฝ์šฐ ์•ž๋ฉด ์ˆ˜์˜ ํ™•๋ฅ ๋ถ„ํฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œ์™€ ์‹์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๊ฐ’ ํ™•๋ฅ ์˜ ๊ฐ’์— ๋Œ€์‘๋˜๋Š” ์‚ฌ์ƒ 0 1/8 { T } 1 3/8 { T, H, T } 2 3/8 { H, T, H } 3 1/8 { H } ( ) { ( x ) ( 2 ) ( โˆ’ 2 ) โˆ’ if = , , , 0 otherwise ์ด์‚ฐํ™•๋ฅ ๋ณ€์ˆ˜์˜ ํ™•๋ฅ ๋ถ„ํฌ ํ™•๋ฅ  ์งˆ๋Ÿ‰ ํ•จ์ˆ˜(็ขบ็Ž‡่ณช้‡ๅ‡ฝๆ•ธ, probability mass function)๋Š” ์ด์‚ฐํ™•๋ฅ ๋ณ€์ˆ˜์—์„œ ํ™•๋ฅ  ( i ) ๊ฐ€ ํŠน์ •ํ•œ ๊ทœ์น™์„ ๊ฐ–๋Š” ๊ฒฝ์šฐ์— ์‹์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๊ฐ’( i ) 1 2 x ํ•ฉ๊ณ„ ํ™•๋ฅ  ( i ) P ( = i ) ( 1 ) ( 2 ) f ( n ) ํ™•๋ฅ  ์งˆ๋Ÿ‰ ํ•จ์ˆ˜์˜ ์กฐ๊ฑด ( i ) 0 i 1 2 โ‹ฏ ( ) โˆ‘ i ฮฉ ( i ) 1 ํ™•๋ฅ ๋ถ„ํฌ์˜ ๊ธฐ๋Œ“๊ฐ’๊ณผ ํ‘œ์ค€ํŽธ์ฐจ ํ™•๋ฅ ๋ณ€์ˆ˜ X์˜ ๊ธฐ๋Œ“๊ฐ’(ํ‰๊ท , ) ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ์ทจํ•˜๋Š” ๊ฐ’ ๊ทธ ๊ฐ’์„ ๊ฐ€์งˆ ํ™•๋ฅ  ( ) โˆ‘ ( ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ์ทจํ•˜๋Š” ๊ฐ’ ) ( ๊ทธ ๊ฐ’์„ ๊ฐ€์งˆ ํ™•๋ฅ  ) โˆ‘ i f ( i ) ํ™•๋ฅ ๋ณ€์ˆ˜ X์˜ ๋ถ„์‚ฐ( 2 )๊ณผ ํ‘œ์ค€ํŽธ์ฐจ( ) ์˜ ๋ถ„์‚ฐ ํŽธ์ฐจ ํ™•๋ฅ ์˜ ๋ถ„์‚ฐ โˆ‘ ( ์ฐจ ) ร— ๋ฅ  a ( ) E ( โˆ’ ) = ( i ฮผ ) f ( i ) D ( ) V r ( ) ๋ถ„์‚ฐ 2 ๊ณ„์‚ฐ์‹ ํ‰๊ท  = x โ‹… ( i ) ๋ชจ๋“  ํ™•๋ฅ  ํ•ฉ์€ f ( i ) 1 ๋ถ„์‚ฐ ๊ณ„์‚ฐ a ( ) โˆ‘ ( i ฮผ ) f ( i ) โˆ‘ ( i โˆ’ x โ‹… + 2 ) ( i ) โˆ‘ i f ( i ) 2 โˆ‘ i f ( i ) ฮผ ฮผ โˆ‘ ( i ) 1 โˆ‘ i f ( i ) ฮผ ํ™•๋ฅ ๋ถ„ํฌ์˜ ๊ธฐ๋Œ“๊ฐ’๊ณผ ํ‘œ์ค€ํŽธ์ฐจ ์˜ˆ ํ™•๋ฅ ๋ณ€์ˆ˜๋Š” ๊ณต์ •ํ•œ ๋™์ „์„ ์„ธ ๋ฒˆ ๋˜์ง„ ๊ฒฝ์šฐ ์•ž๋ฉด ์ˆ˜์ผ ๋•Œ ํ™•๋ฅ ๋ถ„ํฌ : 3 2 2 1 2 1 1 0 ํ•ฉ ( ) 8 8 8 8 8 8 8 8 X ์˜ ๊ธฐ๋Œ“๊ฐ’ : + + + + + + + 8 1.5์˜ ๋ถ„์‚ฐ : ( โˆ’ 1.5 ) + ( โˆ’ 1.5 ) + + ( โˆ’ 1.5 ) 8 0.75์˜ ํ‘œ์ค€ํŽธ์ฐจ : ( โˆ’ 1.5 ) + ( โˆ’ 1.5 ) + + ( โˆ’ 1.5 ) 8 0.87 ํ™•๋ฅ ๋ณ€์ˆ˜๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ํ™•๋ฅ ๋ถ„ํฌ : = i 3 2 1 0 ํ•ฉ ( ) 8 8 8 8 E ( ) โˆ‘ i f ( i ) 3 1 + โ‹… 8 1 3 + โ‹… 8 1.5 a ( ) E ( โˆ’ ) = ( i ฮผ ) f ( i ) ( โˆ’ 1.5 ) โ‹… 8 ( โˆ’ 1.5 ) โ‹… 8 ( โˆ’ 1.5 ) โ‹… 8 ( โˆ’ 1.5 ) โ‹… 8 0.75 a ( ) E ( โˆ’ ) = x 2 ( i ) ฮผ = 2 1 + 2 3 + 2 3 + 2 1 โˆ’ 1.5 = 0.75 D ( ) V R ( ) 0.75 0.87 ํ™•๋ฅ ๋ณ€์ˆ˜ ๋ฌธ์ œ ์–ด๋–ค ์‚ฌ๋žŒ์ด 100,000 ์žฅ์˜ ํ–‰์šด๊ถŒ์„ ๋ฝ‘๋Š” ๊ฒฝ์šฐ ๊ธฐ๋Œ“๊ฐ’์€? ์ƒ๊ธˆ(์ฒœ ์›) ๋งค์ˆ˜ 1,000 1 100 4 10 10 1 100 0 99,885 ํ•ฉ๊ณ„ 100,000 ์ฒœ์› ( ) 1 000 1 100 000 100 4 100 000 10 10 100 000 1 100 100 000 0 100 99 885 0.016 ( ์› ) ํ™•๋ฅ ๋ณ€์ˆ˜ ๋ฌธ์ œ 2 ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๊ธฐ๋Œ“๊ฐ’, ๋ถ„์‚ฐ, ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๋ผ. 0 2 4 f ( ) 0.1 0.2 0.4 0.2 0.1 E ( ) โˆ‘ โ‹… ( ) 0 0.1 โ‹ฏ 4 0.1 2.0 a ( ) โˆ‘ ( i ฮผ ) f ( i ) ( โˆ’ 2.0 ) ร— 0.1 โ‹ฏ ( โˆ’ 2.0 ) ร— 0.1 1.2 โˆ‘ i f ( i ) ฮผ = 2 0.1 โ‹ฏ 4 ร— 0.1 2.0 = 1.2 D ( ) 1.2 1.095 ํ™•๋ฅ ๋ณ€์ˆ˜ ๋ฌธ์ œ 3 ๋‹ค์Œ์€ ๋กœ๋˜์— ๋Œ€ํ•œ ํ‘œ์ด๋‹ค. ๊ฒฝ์šฐ์˜ ์ˆ˜์™€ ํ™•๋ฅ ์€ ๋งค์ฃผ ์ถ”์ฒจํ•˜๋Š” ๋กœ๋˜์— ์ ์šฉ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‚˜๋จธ์ง€ ์—ด์€ ํŠน์ •ํ•œ ์ฃผ์— ์ถ”์ฒจ๋œ ๋กœ๋˜์˜ ์ •๋ณด์ด๋‹ค. ์ด ์ฃผ์— ์ถ”์ฒจ๋œ ๋กœ๋˜์˜ ๋‹น์ฒจ๊ธˆ์— ๋Œ€ํ•œ ๊ธฐ๋Œ“๊ฐ’์€ ์–ผ๋งˆ์ธ๊ฐ€? ๋“ฑ์ˆ˜ ๊ฒฝ์šฐ์˜์ˆ˜ ํ™•๋ฅ  ๋‹น์ฒจ ์ˆ˜ ์ƒ๋Œ€๋„ ์ˆ˜ ๋‹น์ฒจ๊ธˆ 1์ธ๋‹น ๋‹น์ฒจ๊ธˆ ๋ฐฐ๋ถ„์œจ 1๋“ฑ 1 0.00001% 18 0.00002% 30,052,052,640 1,669,558,480 25.20% 2๋“ฑ 6 0.00007% 70 0.00006% 5,008,675,490 71,552,507 4.20% 3๋“ฑ 228 0.00280% 2,824 0.00237% 5,008,677,464 1,773,611 4.20% 4๋“ฑ 11,115 0.13646% 143,314 0.12019% 7,165,700,000 50,000 6.01% 5๋“ฑ 182,780 2.24406% 2,477,370 2.07757% 12,386,850,000 5,000 10.39% ๋‚™์ฒจ 7,950,930 97.61659% 116,620,311 97.79981% - - 50.00% ํ•ฉ 8,145,060 100.00000% 119,243,907 100.00000% 119,243,907,000 500 100.00% ๊ธฐ๋Œ“๊ฐ’์€ ๋นˆ๋„์ˆ˜์™€ ๊ธฐ๋Œ“๊ฐ’์˜ ๊ณฑ์˜ ํ•ฉ์„ ์ด๋นˆ๋„์ˆ˜๋กœ ๋‚˜๋ˆˆ๋‹ค. , 669 558 480 18 71 552 507 70 1 773 611 2 824 50 000 143 314 5 000 2 477 370 0 116 620 311 119 243 907 500 ๋กœ๋˜ ๊ตฌ์ž… ๋น„์šฉ์€ 1์žฅ์— 1,000์›์ด๊ณ  ๊ธฐ๋Œ“๊ฐ’์€ 500์›์ด๋‹ค. 12. ์ด์‚ฐ ํ™•๋ฅ ๋ถ„ํฌ ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰(Bernoulli trial) ์ดํ•ญ๋ถ„ํฌ(binomial distribution) ์Œ์ดํ•ญ๋ถ„ํฌ(negative binomial distribution) ๊ธฐํ•˜ ๋ถ„ํฌ(geometric distribution) ์ดˆ๊ธฐํ•˜๋ถ„ํฌ(hypergeometric distribution) ํฌ์•„์†ก๋ถ„ํฌ(Poisson distribution) 1. ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰(Bernoulli trial) ์–ด๋–ค ์‹œํ–‰์—์„œ ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์„ฑ๊ณต(S)๊ณผ ์‹คํŒจ(F) ๋‘ ๊ฐ€์ง€์ด๋ฉฐ, ๊ฐ ์‹œํ–‰์€ ์„œ๋กœ ๋…๋ฆฝ์ธ ๊ฒฝ์šฐ ๊ฐ ์‹œํ–‰์€ ์„ฑ๊ณต(S), ์‹คํŒจ(F) ๋‘ ๊ฒฐ๊ณผ. ๊ฐ ์‹œํ–‰์—์„œ ์„ฑ๊ณต์ผ ํ™•๋ฅ ์€ ( ) p , ์‹คํŒจํ•  ํ™•๋ฅ ์€ ( ) 1 p ๋กœ ๊ทธ ๊ฐ’์ด ์ผ์ •. ๊ฐ ์‹œํ–‰์€ ์„œ๋กœ ๋…๋ฆฝ์œผ๋กœ ๊ฐ ์‹œํ–‰์˜ ๊ฒฐ๊ณผ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ์‹œํ–‰์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Œ. ( ) { x ( โˆ’ ) โˆ’ if = , 0 otherwise ์ดํ•ญ๋ถ„ํฌ๋ฅผ ์ดํ•ดํ•˜๋ ค๋ฉด ์•Œ์•„์•ผ ๋˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์ˆ˜์‹์„ ์†Œ๊ฐœํ•œ๋‹ค. ! n ( โˆ’ ) โ‹ฏ 2 1 0 = n r n ( โˆ’ ) = ( โˆ’ ) โ‹ฏ ( โˆ’ + ) ( โˆ’ ) ( โˆ’ ) = ( โˆ’ ) โ‹ฏ ( โˆ’ + ) C = ! ( โˆ’ ) ร—! ( r ) n ( โˆ’ ) โ‹ฏ ( โˆ’ + ) ( โˆ’ ) r ร— ( โˆ’ ) = ( โˆ’ ) โ‹ฏ ( โˆ’ + ) ! ( + ) = ( 0 ) 0 n ( 1 ) 1 n 1 โ‹ฏ ( x ) x n x โ‹ฏ ( n ) n 0 ( r ) ( n r ) ( r ) ( โˆ’ r ) ( โˆ’ r 1 ) ( + ) = ( + ) ( + ) โˆ’ = ( + ) โˆ’ + ( + ) โˆ’ โˆ‘ = k ( r ) ( k r ) ( + k ) ( + ) + = ( + ) ( + ) = [ ( 0 ) ( 1 ) + + ( m ) m ] [ ( 0 ) ( 1 ) + + ( n ) n ] ์„œ๋กœ ๋‹ค๋ฅธ 5๊ฐœ ์ˆซ์ž๋ฅผ ๋ฐฐ์—ดํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š”? ! 5 4 3 2 1 120 ๊ตญํšŒ์˜์› ํ›„๋ณด์ž 3 ๋ช…์ด ๋‹ค๋ฅธ 3๊ฐœ ์žฅ์†Œ์—์„œ 5์ผ ๋™์•ˆ ์„ ๊ฑฐ<NAME>๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š”? P = ! ( โˆ’ ) = ร— ร— ร— ร— 2 1 5 4 3 60 ์šฐ๋ฆฌ๋‚˜๋ผ์—์„œ 45๊ฐœ ์ˆซ์ž ์ค‘ 6๊ฐœ๋ฅผ ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ๋งž์ถ”๋ฉด ๋‹น์ฒจ๋˜๋Š” ๋กœ๋˜ ๋ณต๊ถŒ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š”? 45 6 45 44 43 42 41 40 ร— ร— ร— ร— ร— = , 145 060 ( 100 1 ) ์„ ๊ณ„์‚ฐํ•˜๋ฉด? ( 0 ) 100 1 + ( 1 ) 100 1 + ( 2 ) 100 1 + ( 3 ) 100 1 = + ร— 100 3 10 000 1 000 000 1 030 301 ์ดํ•ญ๋ถ„ํฌ(binomial distribution) ์„ฑ๊ณตํ•  ํ™•๋ฅ ์ด ์ธ ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰์„ ๋ฒˆ ๋ฐ˜๋ณตํ•  ๋•Œ, ์„ฑ๊ณต์˜ ํšŸ์ˆ˜๋ฅผ ๋ผ ํ•˜๋ฉด ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ํ™•๋ฅ ๋ถ„ํฌ ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ํ™•๋ฅ ๋ถ„ํฌ๊ฐ€ โˆผ Bin( , )์ผ ๋•Œ, = , , ,์— ๋Œ€ํ•œ ํ™•๋ฅ  ์งˆ๋Ÿ‰ ํ•จ์ˆ˜ ( ) { ( x ) x ( โˆ’ ) โˆ’ if = , , , 0 o.w : ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜ : ๊ฐ ์‹œํ–‰์—์„œ ์„ฑ๊ณต์ผ ํ™•๋ฅ . ( ) : ๋ฒˆ ์‹œํ–‰ ์ค‘ ์„ฑ๊ณต์˜ ํšŒ์ˆ˜์˜ ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ์ดํ•ญ๋ถ„ํฌ๋ผ ํ•˜๊ณ  โˆผ Bin ( , ) ๋กœ ํ‘œํ˜„ ์ดํ•ญ๋ถ„ํฌ์—์„œ ์ดํ•ญ๊ณ„์ˆ˜๋Š” ์‹œํ–‰ ํšŒ์ˆ˜ ์ด ์ปค์ง€๋ฉด ๊ณ„์‚ฐ๋Ÿ‰์ด ๊ธ‰๊ฒฉํžˆ ์ฆ๊ฐ€ํ•œ๋‹ค. ๋˜ํ•œ ์ด ๋งค์šฐ ์ปค์ง€๋ฉด ์†์œผ๋กœ ํ™•๋ฅ  ๊ณ„์‚ฐ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ์ดํ•ญ๋ถ„ํฌ์˜ ํ™•๋ฅ ์€ ์‹œํ–‰ ํšŒ์ˆ˜ ์ด 30 ์ดํ•˜์ด๋ฉด ์ดํ•ญ๋ถ„ํฌ ํ‘œ๋ฅผ ์ด์šฉํ•˜๊ณ  ์‹œํ–‰ ํšŒ์ˆ˜ ์ด 30 ๋ณด๋‹ค ํฌ๋ฉด ์ •๊ทœ๋ถ„ํฌ ๊ทผ์‚ฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ•œ๋‹ค. ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๋ถ„ํฌ๊ฐ€ i ( , ) ์ผ ๋•Œ ๊ธฐ๋Œ“๊ฐ’ ( ) , ๋ถ„์‚ฐ a ( ) , ํ‘œ์ค€ํŽธ์ฐจ d ( ) E ( ) n V r ( ) n ( โˆ’ ) d ( ) n ( โˆ’ ) ์ด๋‹ค. ์ดํ•ญ๋ถ„ํฌ ๋ฌธ์ œ ํ™•๋ฅ ๋ณ€์ˆ˜ ๊ฐ€ ์ดํ•ญ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ณ  = , = 0.35 ์ผ ๋•Œ, ๋‹ค์Œ์˜ ํ™•๋ฅ ์„ ๊ตฌํ•˜๋ฉด? [ โ‰ค ] [ โ‰ฅ ] [ = or X 4 ] ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ํ™•๋ฅ ๋ถ„ํฌ๊ฐ€ โˆผ Bin( , 0.35 )์ผ ๋•Œ, = , , ,์— ๋Œ€ํ•œ ํ™•๋ฅ  ์งˆ๋Ÿ‰ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ( ) { ( x ) 0.35 ( โˆ’ 0.35 ) โˆ’ if = , , , 0 o.w [ โ‰ค ] P [ = ] P [ = ] P [ = ] ( 0 ) 0.35 ร— ( โˆ’ 0.35 ) + ( 1 ) 0.35 ร— ( โˆ’ 0.35 ) + ( 2 ) 0.35 ร— ( โˆ’ 0.35 ) = 0.7648 [ โ‰ฅ ] 1 ( [ = ] P [ = ] ) 0.5716 [ = or X 4 ] P [ = ] P [ = ] ( 2 ) 0.35 ร— 0.65 + ( 4 ) 0.35 ร— 0.65 = 0.3364 0.0488 0.3852 ์Œ์ดํ•ญ๋ถ„ํฌ ์Œ์ดํ•ญ๋ถ„ํฌ(negative binomial distribution)๋Š” ์„ฑ๊ณตํ•  ํ™•๋ฅ ์ด ์ธ ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰์—์„œ ๋ฒˆ์งธ์— ๋ฒˆ์งธ ์„ฑ๊ณต์˜ ํšŸ์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ํ™•๋ฅ ๋ถ„ํฌ์ด๋‹ค. ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ํ™•๋ฅ ๋ถ„ํฌ๊ฐ€ โˆผ NB ( , ) ์ผ ๋•Œ, = , + ,์— ๋Œ€ํ•œ ํ™•๋ฅ  ์งˆ๋Ÿ‰ ํ•จ์ˆ˜ ( ) { ( โˆ’ r 1 ) r ( โˆ’ ) โˆ’ if = , + , 0 o.w : ๊ฐ ์‹œํ–‰์—์„œ ์„ฑ๊ณต์ผ ํ™•๋ฅ  ( ) : ๋ฒˆ ์‹œํ–‰ ํšŒ์ˆ˜, : ๋ฒˆ ์„ฑ๊ณต ํšŒ์ˆ˜์˜ ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ์ดํ•ญ๋ถ„ํฌ๋ผ ํ•˜๊ณ  โˆผ NB ( , ) ๋กœ ํ‘œํ˜„ ์Œ์ดํ•ญ๋ถ„ํฌ์˜ ํ‰๊ท , ๋ถ„์‚ฐ, ํ‘œ์ค€ํŽธ์ฐจ ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๋ถ„ํฌ๊ฐ€ NB ( , ) ์ผ ๋•Œ ํ‰๊ท  ( ) , ๋ถ„์‚ฐ a ( ) , ํ‘œ์ค€ํŽธ์ฐจ d ( ) E ( ) r p a ( ) r โˆ’ p s ( ) r โˆ’ p ์Œ์ดํ•ญ๋ถ„ํฌ ์‹ค์ œ ๋ฌธ์ œ ์–ด๋–ค ์–ด๋ฆฐ์ด๊ฐ€ ์ „์—ผ์„ฑ ์งˆ๋ณ‘์— ๋…ธ์ถœ๋˜์—ˆ์„ ๋•Œ ์งˆ๋ณ‘์— ๊ฐ์—ผ๋  ํ™•๋ฅ ์ด 0.4์ด๋‹ค. 10 ๋ฒˆ์งธ ์•„์ด๊ฐ€ ์งˆ๋ณ‘์— ๋…ธ์ถœ๋˜์—ˆ์„ ๋•Œ 3 ๋ฒˆ์งธ๋กœ ์งˆ๋ณ‘์— ๊ฐ์—ผ๋˜์—ˆ์„ ํ™•๋ฅ ์€ ์–ผ๋งˆ์ธ๊ฐ€? B ( 10 3 0.4 ) ( 2 ) ( 0.4 ) ( โˆ’ 0.4 ) = 0.0645 ๊ธฐํ•˜ ๋ถ„ํฌ ๊ธฐํ•˜ ๋ถ„ํฌ(geometric distribution)๋Š” ์„ฑ๊ณตํ•  ํ™•๋ฅ ์ด ์ธ ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰์„ ๋…๋ฆฝ์ ์œผ๋กœ ์‹œํ–‰ํ•  ๋•Œ, ์ฒซ ๋ฒˆ์งธ ์„ฑ๊ณต์ด ๋ฐœ์ƒํ•  ๋•Œ ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ํ™•๋ฅ ๋ถ„ํฌ์ด๋‹ค. ์Œ์ดํ•ญ๋ถ„ํฌ์—์„œ ์ฒซ ๋ฒˆ์งธ ์„ฑ๊ณต์— ๋Œ€ํ•œ ๋ถ„ํฌ NB( , )๊ฐ€ ๊ธฐํ•˜ ๋ถ„ํฌ์ž„. ( ) { ( โˆ’ ) โˆ’ if = , , 0 o.w ๊ธฐํ•˜ ๋ถ„ํฌ์˜ ํ‰๊ท , ๋ถ„์‚ฐ, ํ‘œ์ค€ํŽธ์ฐจ ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๋ถ„ํฌ๊ฐ€ NB ( , ) ์ผ ๋•Œ ํ‰๊ท  ( ) , ๋ถ„์‚ฐ a ( ) , ํ‘œ์ค€ํŽธ์ฐจ d ( ) E ( ) 1 V r ( ) 1 p 2 d ( ) 1 p 2 ๊ธฐํ•˜ ๋ถ„ํฌ ์‹ค์ œ ๋ฌธ์ œ ์–ด๋–ค ์–ด๋ฆฐ์ด๊ฐ€ ์ „์—ผ์„ฑ ์งˆ๋ณ‘์— ๋…ธ์ถœ๋˜์—ˆ์„ ๋•Œ ์งˆ๋ณ‘์— ๊ฐ์—ผ๋  ํ™•๋ฅ ์ด 0.4์ด๋‹ค. 5 ๋ฒˆ์งธ ์•„์ด๊ฐ€ ์งˆ๋ณ‘์— ๋…ธ์ถœ๋˜์—ˆ์„ ๋•Œ ์ฒซ ๋ฒˆ์งธ๋กœ ์งˆ๋ณ‘์— ๊ฐ์—ผ๋˜์—ˆ์„ ํ™•๋ฅ ์€ ์–ผ๋งˆ์ธ๊ฐ€? ( , 0.4 ) 0.4 ( โˆ’ 0.4 ) = 0.0518 ์ดˆ๊ธฐํ•˜๋ถ„ํฌ ์ดˆ๊ธฐํ•˜๋ถ„ํฌ(hypergeometric distribution)๋Š” ์œ ํ•œ ๋ชจ์ง‘๋‹จ์—์„œ ๋น„๋ณต์› ์ถ”์ถœํ•˜๋Š” ๊ฒฝ์šฐ ์„ฑ๊ณต์ธ ํšŸ์ˆ˜๋ฅผ ์˜ ๋ถ„ํฌ์ด๋‹ค. ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ํ™•๋ฅ  ์งˆ๋Ÿ‰ ํ•จ์ˆ˜๋Š” ( ) { ( x ) ( โˆ’ n x ) ( n ) if = a ( , + โˆ’ ) โ‹ฏ m n ( , ) o.w : ๋ชจ์ง‘๋‹จ์˜ ํฌ๊ธฐ, : ํ‘œ๋ณธ์˜ ํฌ๊ธฐ : ๋ชจ์ง‘๋‹จ ๋‚ด์—์„œ ๋ฒ”์ฃผ์— ์†ํ•˜๋Š” ๊ตฌ์„ฑ ์›์†Œ์˜ ์ˆ˜ : ํ‘œ๋ณธ ๋‚ด์—์„œ ๋ฒ”์ฃผ์— ์†ํ•˜๋Š” ๊ตฌ์„ฑ ์›์†Œ์˜ ์ˆ˜ ์ดˆ๊ธฐํ•˜๋ถ„ํฌ์˜ ํ‰๊ท  ๋ถ„์‚ฐ, ํ‘œ์ค€ํŽธ์ฐจ ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๋ถ„ํฌ๊ฐ€ ์ดˆ๊ธฐํ•˜๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅผ ๋•Œ ๊ธฐ๋Œ“๊ฐ’ ( ) , ๋ถ„์‚ฐ a ( ) , ํ‘œ์ค€ํŽธ์ฐจ d ( ) E ( ) n V r ( ) n ( โˆ’ ) N n โˆ’ s ( ) n ( โˆ’ ) N n โˆ’ ์ดˆ๊ธฐํ•˜๋ถ„ํฌ ์‹ค์ œ ๋ฌธ์ œ ์ƒ์ž์— ๋นจ๊ฐ„ ๊ณต 5๊ฐœ, ํฐ ๊ณต 4๊ฐœ๊ฐ€ ์žˆ๋‹ค. ์ด ์ƒ์ž์—์„œ ์ž„์˜๋กœ 3๊ฐœ์˜ ๊ณต์„ ๋น„๋ณต์› ์ถ”์ถœํ•œ๋‹ค๊ณ  ํ•˜์ž. ํ‘œ๋ณธ ๊ณต๊ฐ„์„ ๊ธฐ์ˆ ํ•ด ๋ณด์ž. ๊ฐ ๊ทผ์›์‚ฌ์ƒ์— ๋Œ€ํ•œ ํ™•๋ฅ ์„ ๊ตฌํ•ด๋ณด์ž ํ™•๋ฅ ๋ณ€์ˆ˜ X๋ฅผ ์ถ”์ถœํ•œ 3๊ฐœ์˜ ๊ณต์—์„œ ํฐ ๊ณต์˜ ์ˆ˜์ผ ๋•Œ ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ์ž‘์„ฑํ•˜๋ผ. ํฌ์•„์†ก๋ถ„ํฌ ์ดํ•ญ๋ถ„ํฌ์—์„œ โ†’, โ†’ 0 ์ผ ๋•Œ ๋ถ„ํฌ์˜ ๊ทนํ•œ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ( ; , ) ( x ) ( n ) ( โˆ’ n ) โˆ’ = ( โˆ’ ) ( โˆ’ ) ( โˆ’ + ) ! ( n ) ( โˆ’ n ) โˆ’ = ( โˆ’ n ) ( โˆ’ n ) ( โˆ’ โˆ’ n ) ! x [ ( โˆ’ n ) n ฮป ] ฮป ( โˆ’ n โ†’ ๋ผ ๋†“์œผ๋ฉด ( โˆ’ n ) ( โˆ’ n ) ( โˆ’ โˆ’ n ) 1 ( โˆ’ n ) x 1 ( โˆ’ n ) n ฮป ์ด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ทนํ•œ ๋ถ„ํฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ( , ) ฮป e ฮป! f r x 0 1 2. . ์ดํ•ญ๋ถ„ํฌ ๋ฌธ์ œ ์‚ฌ๋žŒ์ด ์—ฌ๋ฆ„์— ํƒˆ์ง„ํ•  ํ™•๋ฅ ์ด 0.005๋ผ๊ณ  ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ๋”์šด ์—ฌ๋ฆ„๋‚  ์–ด๋–ค ์žฅ์†Œ์— ์‚ฌ๋žŒ์ด 3000๋ช… ์žˆ๋‹ค๊ณ  ํ•˜์ž. ์ด ์‚ฌ๋žŒ ์ค‘ 18๋ช…์ด ํƒˆ์ง„ํ•  ํ™•๋ฅ ์ด ์–ผ๋งˆ์ธ์ง€ ์ดํ•ญ๋ถ„ํฌ๋กœ ๊ตฌํ•˜๋ฉด ( 3000 18 ) 0.005 18 0.995 2982 ์ด๋‹ค. ์ด ์‹์€ ์ดํ•ญ๋ถ„ํฌ๋กœ ๊ณ„์‚ฐํ•˜๋ฉด ๊ณ„์‚ฐ๋Ÿ‰์ด ์—„์ฒญ๋‚˜๊ฒŒ ๋งŽ๋‹ค. ์–ด๋–ป๊ฒŒ ๊ณ„์‚ฐํ• ์ง€ ์ƒ๊ฐํ•ด ๋ณด์ž. ํฌ์•„์†ก๋ถ„ํฌ ํฌ์•„์†ก๋ถ„ํฌ(poisson distribution)๋Š” ๋งค ์ˆœ๊ฐ„๋งˆ๋‹ค ์‚ฌ๊ฑด ๋ฐœ์ƒ์ด ๊ฐ€๋Šฅํ•˜๋‚˜ ์‚ฌ๊ฑด ๋ฐœ์ƒ ํ™•๋ฅ ์€ ์•„์ฃผ ์ž‘์€ ๊ฒฝ์šฐ์— ์ด์šฉ๋˜๋Š” ํ™•๋ฅ ๋ชจํ˜•์ด๋‹ค. ํ™•๋ฅ ๋ณ€์ˆ˜ ๊ฐ€ ํ‰๊ท ์ด ์ธ ํฌ์•„์†ก ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ํ•  ๋•Œ ํ™•๋ฅ ํ•จ์ˆ˜๋Š” ( ) { โˆ’ m x if = , , 0 o.w ์ด๋‹ค. ํฌ์•„์†ก๋ถ„ํฌ ๊ฐ€์ • ์ฃผ์–ด์ง„ ๊ตฌ๊ฐ„์—์„œ ์‚ฌ๊ฑด์˜ ํ‰๊ท  ๋ฐœ์ƒ ํšŸ์ˆ˜๋Š” ๊ตฌ๊ฐ„์˜ ์‹œ์ž‘์ ์—๋Š” ๊ด€๊ณ„๊ฐ€ ์—†๊ณ  ๊ตฌ๊ฐ„์˜ ๊ธธ์ด์—๋งŒ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค. ํ•œ์ˆœ๊ฐ„์— 2ํšŒ ์ด์ƒ ์‚ฌ๊ฑด์ด ๋ฐœ์ƒํ•  ํ™•๋ฅ ์€ ๊ฑฐ์˜ 0์— ๊ฐ€๊น๋‹ค. ํ•œ ๊ตฌ๊ฐ„์—์„œ ๋ฐœ์ƒํ•œ ์‚ฌ๊ฑด์˜ ํšŸ์ˆ˜๋Š” ๊ฒน์น˜์ง€ ์•Š๋Š” ๋‹ค๋ฅธ ๊ตฌ๊ฐ„์—์„œ ๋ฐœ์ƒํ•œ ์‚ฌ๊ฑด์˜ ์ˆ˜์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค. ํฌ์•„์†ก๋ถ„ํฌ์™€ ์ดํ•ญ๋ถ„ํฌ์˜ ๊ด€๊ณ„ ์ดํ•ญ๋ถ„ํฌ์˜ ํ™•๋ฅ ์€ โ‰ฅ 20, and โ‰ค 0.05 ์ผ ๋•Œ ํฌ์•„ ์†ก ๊ทผ์‚ฌ๊ฐ€ ์ข‹๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ๊ณ  โ‰ฅ 100, and p 10 ์ธ ๊ฒฝ์šฐ ํฌ์•„ ์†ก ๊ทผ์‚ฌ๊ฐ€ ๋งค์ฃผ ์ข‹๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ํฌ์•„์†ก๋ถ„ํฌ ๋ฌธ์ œ ํฌ์•„์†ก๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜ X์— ๋Œ€ํ•˜์—ฌ ๋‹ค์Œ ํ™•๋ฅ ์„ ๊ตฌํ•ด๋ณด์ž. =3์ผ ๋•Œ [ = ] P [ โ‰ค ] =4์ผ ๋•Œ [ โ‰ค โ‰ค ] P [ = 10 ] =0.5์ผ ๋•Œ [ = ] P [ โ‰ฅ ] ์–ด๋Š ๊ฐ€์ •์ง‘์— ํ‰์ผ ํ‰๊ท  5ํ†ต์˜ ์ „ํ™”๊ฐ€ ์˜จ๋‹ค๊ณ  ํ•œ๋‹ค. ์–ด๋Š ํ‰์ผ ์ „ํ™”๊ฐ€ ํ•œ ํ†ต๋„ ์˜ค์ง€ ์•Š์„ ํ™•๋ฅ ์„ ๊ตฌํ•ด๋ณด์ž. ์–ด๋Š ๋ถ€๋™์‚ฐ ์ค‘๊ฐœ์—…์†Œ์—์„œ๋Š” ํ•œ ๋‹ฌ์— ํ‰๊ท  3๊ฑด์˜ ๋งค๋งค๋ฅผ ์„ฑ์‚ฌ์‹œํ‚จ๋‹ค๊ณ  ํ•œ๋‹ค. ๋ถ€๋™์‚ฐ ๋งค๋งค๊ฐ€ ๊ณ„์ ˆ์˜ ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด ํฌ์•„์†ก๋ถ„ํฌ์˜ ์–ด๋Š ์„ฑ์งˆ์„ ์œ„๋ฐ˜ํ•˜๋Š” ๊ฒƒ์ธ๊ฐ€? 13. ์—ฐ์†ํ™•๋ฅ ๋ถ„ํฌ ํ™•๋ฅ ๋ฐ€๋„ํ•จ์ˆ˜(probabillity density distribution) ์ •๊ทœ๋ถ„ํฌ(normal distribution) t-๋ถ„ํฌ(t distribution) ์นด์ด์ œ๊ณฑ๋ถ„ํฌ( 2 distribution) F-๋ถ„ํฌ(F distribution) ์—ฐ์†ํ™•๋ฅ ๋ถ„ํฌ ์„ฑ์งˆ ํ™•๋ฅ ๋ณ€์ˆ˜ ๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ๊ฐ’๋“ค์ด ๋ฌดํ•œ ๊ฐœ์ด๋ฉฐ ์…€ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ํ”ผ์ž ๋ฐฐ๋‹ฌ์‹œ๊ฐ„ [0, ) ์–ด๋–ค ์ธก์ • ๊ธฐ๊ธฐ์˜ ์ธก์ •์˜ค์ฐจ(ๆธฌๅฎš่ชคๅทฎ) ( โˆž โˆž ) ํ™•๋ฅ ๋ฐ€๋„ํ•จ์ˆ˜(็ขบๅฏ†ๅบฆๅ‡ฝๆ•ธ, probability density function) ์—ฐ์†ํ™•๋ฅ ๋ณ€์ˆ˜์—์„œ ํ™•๋ฅ  ( i ) ๊ฐ€ ํŠน์ •ํ•œ ๊ทœ์น™์„ ๊ฐ–๋Š” ๊ฒฝ์šฐ์— ์‹์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ. ํ™•๋ฅ ๋ฐ€๋„ํ•จ์ˆ˜ ์กฐ๊ฑด(ๆขไปถ) ๋ชจ๋“  x ๊ฐ’์— ๋Œ€ํ•˜์—ฌ ( ) 0 ( โ‰ค โ‰ค ) โˆซ b ( ) x ( โˆž X โˆž ) โˆซ โˆž f ( ) x 1 ํ™•๋ฅ ๋ฐ€๋„ํ•จ์ˆ˜ ์„ฑ์งˆ(ๆ€ง่ณช) ์—ฐ์†ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ค ์ฃผ์–ด์ง„ x ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋  ํ™•๋ฅ  [ = ] 0 ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ž„์˜์˜ ๊ฐ’์—์„œ ์‚ฌ์ด์˜ ๊ตฌ๊ฐ„ ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. [ โ‰ค โ‰ค ] P [ < โ‰ค ] P [ โ‰ค < ] P [ < < ] ์ •๊ทœ๋ถ„ํฌ ์ •๊ทœ๋ถ„ํฌ(normal distribution)๋Š” ์—ฐ์†ํ™•๋ฅ ๋ณ€์ˆ˜ X๊ฐ€ ์œ„์น˜ ๋ชจ์ˆ˜(location parameter) ํ‰๊ท  ์™€ ํฌ๊ธฐ ๋ชจ์ˆ˜(scale parameter) ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ฐ€์ง€๋Š” ๋ถ„ํฌ๋กœ ๊ทธ๋ž˜ํ”„์˜ ํ˜•ํƒœ๋Š” ์ข… ๋ชจ์–‘(bell curve)์ด๊ณ  ํ‘œํ˜„์€ โˆผ N ( , 2 ) ์ž„. ์ •๊ทœ๋ถ„ํฌ ์„ฑ์งˆ ํ‰๊ท ์— ๋Œ€ํ•˜์—ฌ ์ขŒ์šฐ๋กœ ๋Œ€์นญ, ์ตœ๋นˆ๊ฐ’=์ค‘์•™๊ฐ’=ํ‰๊ท  ํ‰๊ท  ๋Š” ์ค‘์‹ฌ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , ํ‘œ์ค€ํŽธ์ฐจ๋Š” ํ‰๊ท  ๋กœ๋ถ€ํ„ฐ ํผ์ ธ์žˆ๋Š” ์ •๋„๋ฅผ ๋‚˜ํƒ€๋ƒ„ ์ •๊ทœ๋ถ„ํฌ์—์„œ [ โˆ’ ฯƒ X ฮผ 1 ] ์ •๊ทœ๋ถ„ํฌ์—์„œ [ โˆ’ ฯƒ X ฮผ 2 ] ์ •๊ทœ๋ถ„ํฌ์—์„œ [ โˆ’ ฯƒ X ฮผ 3 ] ์ •๊ทœ๋ถ„ํฌ์—์„œ [ โˆ’ ฯƒ X ฮผ k ] ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ํ™•๋ฅ ๋ณ€์ˆ˜ X๊ฐ€ ( , 2 ) ์ผ ๋•Œ, ํ‰๊ท ์ด์ด๊ณ  ๋ถ„์‚ฐ์ด 2 ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ์ •๊ทœ๋ถ„ํฌ ํ‘œ์ค€ํ™”๋ผ๊ณ  ํ•œ๋‹ค. ๋˜ํ•œ ์ด ํ™•๋ฅ ๋ณ€์ˆ˜๋ฅผ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ํ™•๋ฅ ๋ณ€์ˆ˜๋ผ๊ณ  ํ•˜๋ฉฐ๋กœ ์“ด๋‹ค. ํ‘œ์ค€ํ™”๋œ ํ™•๋ฅ ๋ณ€์ˆ˜๋Š” = โˆ’ ฯƒ ์ด๋ฉฐ ( , 2 ) ์ธ ์ •๊ทœ๋ถ„ํฌ์ด๋‹ค. ์ •๊ทœ๋ถ„ํฌ ํ™•๋ฅ ์€ ์ง์ ‘ ์†์œผ๋กœ ๊ณ„์‚ฐํ•˜๊ธฐ๊ฐ€ ์ˆ˜์›”ํ•˜์ง€ ์•Š๊ธฐ์— ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์— ๋Œ€ํ•œ ํ™•๋ฅ ํ‘œ๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ ์ด๊ฒƒ์„ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ํ‘œ๋ผ๊ณ  ํ•œ๋‹ค. ์ •๊ทœ๋ถ„ํฌ ํ‘œ๋กœ ํ™•๋ฅ  ๊ณ„์‚ฐ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ํ‘œ๋Š” ํ™•๋ฅ ๋ถ„ํฌ ํ‘œ์—์„œ ์ œ๊ณตํ•œ๋‹ค. ์ •๊ทœ๋ถ„ํฌ ํ‘œ์—์„œ ํ™•๋ฅ ์€ ์–ด๋Š ๋ฐฉํ–ฅ๋ถ€ํ„ฐ ๊ณ„์‚ฐํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋งŒ๋“  ์‚ฌ๋žŒ์— ๋”ฐ๋ผ ๋ฐฉํ–ฅ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ํ‘œ์—์„œ ๊ทธ๋ž˜ํ”„์˜ ๋ฉด์ ์€ ์‹์ด๋‚˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๋ฉด ( โˆž z ] ์ด๋‹ค. ์ •๊ทœ๋ถ„ํฌ ํ‘œ์—์„œ๋Š” ์ด์ฐจ์› ํ‰๋ฉด์—์„œ ์ถ• ์ขŒํ‘œ๊ฐ’์ด๋‹ค. ์ฒซ ์—ด์€ ๊ฐ’์ด 3.5 ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ 0.1์”ฉ ์ฆ๊ฐ€ํ•œ๋‹ค. ์ฒซ ํ–‰์€ ๊ฐ’์ด ์†Œ์ˆ˜์  ์ดํ•˜ ๋‘˜์งธ ์ž๋ฆฌ์— ํ•ด๋‹น๋œ๋‹ค. ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฐ’์€ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์—์„œ ๋ฉด์ ์ด๋‹ค. ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์„ ์ œ์™ธํ•œ ๊ฐ’์€ โˆž ์—์„œ ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์˜ ๊ต์ฐจ์ ๊นŒ์ง€ ๋ฉด์ ์ด๋‹ค. ๊ทธ๋ฆผ์—์„œ ์ฒซ ์—ด์˜ 1.9 ์™€ ์ฒซ ํ–‰์˜ 0.06 ์˜ ๊ต์ฐจ์ ์€ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ๊ฐ’ 1.96 ์ด๊ณ , ์ด๋•Œ ( โˆž โˆ’ 1.96 ] ๊นŒ์ง€ ๋ฉด์ ์€ 0.0025 ์ด๋‹ค. ๊ทธ๋ฆผ์—์„œ p ๊ฐ’์€ โˆž ์—์„œ๊นŒ์ง€ ๋ฉด์ ์ด์ด๋‹ค. ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ๊ณ„์‚ฐ ์ •๊ทœ ํ™•๋ฅ ๋ณ€์ˆ˜๋ฅผ ํ‘œ์ค€ ์ •๊ทœ ํ™•๋ฅ ๋ณ€์ˆ˜๋กœ ๋ณ€ํ™˜ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ํ‘œ๋กœ๋ถ€ํ„ฐ ๊ณ„์‚ฐ๋œ ์— ๋Œ€ํ•œ ํ™•๋ฅ ์„ ์ฐพ์Œ ์ •๊ทœ๋ถ„ํฌ๋Š” ๋Œ€์นญ์ด๊ธฐ ๋•Œ๋ฌธ์— [ < z ] P [ > ] 1 P [ โ‰ฅ z ] ๋กœ ๊ณ„์‚ฐํ•จ. ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ๋ฌธ์ œ ํ™•๋ฅ  [ < 0.15 or Z 1.6 ] ์„ ๊ณ„์‚ฐํ•˜๋ผ. P [ > 1.6 ] P [ < 0.15 ] P [ < 1.6 ] P [ < 0.15 ] ( P [ > 1.6 ] P [ < 1.6 ] ) 0.0548 0.4404 0.4952 ํ‘œ์™€ ๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช… ์‹ค์ œ ๋ฌธ์ œ ์–ด๋Š ๋Œ€ํ•™๊ต์—์„œ A ๊ณผ๋ชฉ ์ค‘๊ฐ„๊ณ ์‚ฌ ์„ฑ์ ์€ ๋ถ„ํฌ๊ฐ€ ํ‰๊ท ์ด 63์ด๊ณ  ๋ถ„์‚ฐ์ด 100์ธ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ํ•œ๋‹ค. 50์  ์ดํ•˜์˜ ํ•™์ƒ์€ ๋ช‡ ํผ์„ผํŠธ๋‚˜ ๋˜๊ฒ ๋Š”๊ฐ€? ์ƒ์œ„ 10% ํ•™์ƒ์—๊ฒŒ A๋ฅผ ์ค€๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด ๋ช‡ ์  ์ด์ƒ์ด ๋˜์–ด์•ผ A๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒ ๋Š”๊ฐ€? 1 ๋ฒˆ ๋ฌธ์ œ ํ’€์ด : [ โ‰ค 50 ] P ( โˆ’ 63 10 50 63 10 ) P [ โ‰ค 1.3 ] 0.0968 2 ๋ฒˆ ๋ฌธ์ œ ํ’€์ด : [ โ‰ฅ ] 0.10 0.10 P [ โ‰ฅ 1.28 ] P ( โˆ’ 63 10 1.28 ) P [ โ‰ฅ 75.8 ] t - ๋ถ„ํฌ(t distribution) t ๋ถ„ํฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1908๋…„ ์˜๊ตญ์˜ ๊ณผํ•™์ž ๊ณ ์…‹(W. C. Gosset)์ด Biometrika์— ์†Œ๊ฐœ ์ด ๋…ผ๋ฌธ์€ ํ•„๋ช…์ธ Student๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ถœ๊ฐ„ ๋ชจ์ง‘๋‹จ์˜ ๋ถ„ํฌ๊ฐ€ ์ •๊ทœ๋ถ„ํฌ์ด๊ณ  ํ‘œ๋ณธ์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฒฝ์šฐ ํ‘œ๋ณธํ‰๊ท  โ€• ์˜ ๋ถ„ํฌ ๋ชจ์ˆ˜๋Š” ์ž์œ ๋„(degree of freedom)์ด๋ฉฐ ๋ชจ์–‘์€ ์ข… ํ˜•ํƒœ์ด๊ณ , 0์—์„œ ๋Œ€์นญ์ธ ๋ถ„ํฌ ์ž์œ ๋„๊ฐ€ ์ปค์ง€๋ฉด ์ •๊ทœ๋ถ„ํฌ์— ๊ฐ€๊น๊ฒŒ ๋จ ( ) ฮ“ ( + 2 ) ฮฝ ( 2 ) ( + 2 ) ฮฝ 1 for โˆž t โˆž ์ •๊ทœ๋ถ„ํฌ์™€ t ๋ถ„ํฌ ๋น„๊ต ๊ทธ๋ฆผ์—์„œ ( ) ๋Š” ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ( ) ๋Š” ์ž์œ ๋„๊ฐ€ 5์ธ t ๋ถ„ํฌ ( ) ๋Š” ์ž์œ ๋„๊ฐ€ 2์ธ t ๋ถ„ํฌ ์ด๋‹ค. t ๋ถ„ํฌ๋Š” ์ž์œ ๋„๊ฐ€ ์ปค์ง€๋ฉด ์ •๊ทœ๋ถ„ํฌ์— ๊ฐ€๊น๊ฒŒ ๋˜๋ฉฐ, ์ž์œ ๋„๊ฐ€ ์ด๋ฉด ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์™€ ๋™์ผํ•˜๋‹ค. t ๋ถ„ํฌ ํ‘œ๋กœ ํ™•๋ฅ  ๊ณ„์‚ฐ t ๋ถ„ํฌ ํ‘œ๋Š” ํ™•๋ฅ ๋ถ„ํฌ ํ‘œ์—์„œ ์ œ๊ณตํ•œ๋‹ค. t ๋ถ„ํฌ ํ‘œ์—์„œ ํ™•๋ฅ ์€ ์–ด๋Š ๋ฐฉํ–ฅ๋ถ€ํ„ฐ ๊ณ„์‚ฐํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. t ๋ถ„ํฌ๋ฅผ ๋งŒ๋“  ์‚ฌ๋žŒ์— ๋”ฐ๋ผ ๋ฐฉํ–ฅ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ํ‘œ์—์„œ ๊ทธ๋ž˜ํ”„์˜ ๋ฉด์ ์€ ์‹์ด๋‚˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๋ฉด [ , ) ์ด๋‹ค. t ๋ถ„ํฌ ํ‘œ์—์„œ๋Š” ์ด์ฐจ์› ํ‰๋ฉด์—์„œ ์ถ• ์ขŒํ‘œ๊ฐ’์ด๋‹ค. ์ฒซ ์—ด d.f๋Š” ์ž์œ ๋„(degree of freedom)๋‹ค. ์ฒซ ํ–‰ ๋Š” [ , ) ๊นŒ์ง€ ๋ฉด์ ์ด๋‹ค. ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฐ’์€ t ๋ถ„ํฌ์—์„œ ๊ฐ’์œผ๋กœ ์ขŒํ‘œํ‰๋ฉด์—์„œ ์ถ• ๊ฐ’์ด๋‹ค. ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์„ ์ œ์™ธํ•œ ๊ฐ’์€ ์ž์œ ๋„๊ฐ€ d.f์ด๊ณ  [ , ) ๋ฉด์ ์ด ์ธ ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์˜ ๊ต์ฐจ์ ์˜ ๊ฐ’์ด๋‹ค. ๊ทธ๋ฆผ์—์„œ ์ฒซ ์—ด d.f๊ฐ€ 9์ด๊ณ  ์ฒซ ํ–‰ ๊ฐ€ 0.05 ์ผ ๋•Œ ๊ต์ฐจ์ ์€ t ๋ถ„ํฌ ๊ฐ’ ์ด 1.8331 ์ด๋‹ค. t ๋ถ„ํฌ ํ‘œ์˜ ๋ฌธ์ œ์ ์€ ์ œํ•œ์ ์ธ ๋ฉด์ ๋งŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ธฐ์— ์ž์„ธํ•œ ๊ฒƒ์€ ์ปดํ“จํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•œ๋‹ค. t ๋ถ„ํฌ ๋ฌธ์ œ ์ž์œ ๋„๊ฐ€ 9์ธ t ๋ถ„ํฌ๋ฅผ ๊ฐ–๋Š” ํ†ต๊ณ„๋Ÿ‰์— ๋Œ€ํ•˜์—ฌ ( b t b ) 0.9 ๋ฅผ ๋งŒ์กฑ์‹œํ‚ค๋Š” ๋ฅผ ์ฐพ์•„๋ผ. t ๋ถ„ํฌ๋Š” ์„ ์ค‘์‹ฌ์œผ๋กœ ๋Œ€์นญ์ด๋ฏ€๋กœ ( โˆž โˆ’ )์™€ ( , )์— ๊ฐ๊ฐ % ์˜ ํ™•๋ฅ ์ด ์กด์žฌํ•œ๋‹ค. ์ž์œ ๋„๊ฐ€ 9์ธ t ๋ถ„ํฌ์—์„œ ์ƒ์œ„ % ํ™•๋ฅ ์ธ ์ œ 95 ๋ฐฑ๋ถ„์œ„์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด 1.8331 ์ด๋‹ค. ๋”ฐ๋ผ์„œ t ๋ถ„ํฌ ํ‘œ์—์„œ 0.05 ( ) 1.833 t 0.95 ( ) โˆ’ 1.833 ์ด๋‹ค. 2 ๋ถ„ํฌ 2 ๋ถ„ํฌ ํ‘œ๋Š” ํ™•๋ฅ ๋ถ„ํฌ ํ‘œ์—์„œ ์ œ๊ณตํ•œ๋‹ค. 2 ๋ถ„ํฌ๋Š” 1900๋…„ Karl Pearson์ด ์ œ์•ˆํ•œ ๋ถ„ํฌ ๊ฐ๋งˆ ๋ถ„ํฌ์˜ ํŠน๋ณ„ํ•œ ๋ถ„ํฌ ๊ฐ๋งˆ ๋ถ„ํฌ a m ( , ) 1 ( ) ฮฑ ฮฑ 1 โˆ’ / ฯ‡ ๋ถ„ํฌ a m ( , = / , = ) 1 ( / ) v 2 v โˆ’ e x 2 2 ๋ถ„ํฌ๋Š” 2 ๊ฒ€์ •๊ณผ ๋ถ„์‚ฐ ์ถ”๋ก ์— ์‚ฌ์šฉ ๋ชจ์ˆ˜(parameter)๋Š” ์ž์œ ๋„๊ฐ€ ์žˆ๊ณ , ๋ชจ์–‘(shape)์€ ๋น„๋Œ€์นญํ˜• ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์˜ ์ œ๊ณฑ์€ ์ž์œ ๋„๊ฐ€ 1์ธ ์นด์ด์ œ๊ณฑ๋ถ„ํฌ์ž„. 2 ฯ‡ ( ) 2 ๋ถ„ํฌ๋“ค 2 ๋ถ„ํฌ๊ฐ€ ์ ์šฉ๋˜๋Š” ๋ถ„์„๋ฐฉ๋ฒ• ๋ชจ์ง‘๋‹จ์˜ ํ‘œ์ค€ํŽธ์ฐจ์— ๋Œ€ํ•œ ์ถ”๋ก  ์ ํ•ฉ๋„ ๊ฒ€์ •(goodness--of--fit test) ๋™์งˆ์„ฑ ๊ฒ€์ •(homogeneity test) ๋…๋ฆฝ์„ฑ ๊ฒ€์ •(independence test) ํ‘œ์ค€ํŽธ์ฐจ์— ๋Œ€ํ•œ ์ถ”๋ก  ์ •๊ทœ๋ชจ์ง‘๋‹จ ( , 2 ) ์—์„œ ์ถ”์ถœ๋œ ํ‘œ๋ณธ์„ 1 โ‹ฏ X์ด๋ผ ํ•  ๋•Œ, i 1 ( i X) ฯƒ = ( โˆ’ ) 2 2 ฯ‡ ( โˆ’ ) ์€ ์ž์œ ๋„๊ฐ€ โˆ’ ์ธ 2 ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ํ•˜๊ณ , ์ด๊ฒƒ์„ ๊ธฐํ˜ธ๋กœ๋Š” 2 ( โˆ’ ) ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. 2 ๋ถ„ํฌ ํ‘œ๋กœ ํ™•๋ฅ  ๊ณ„์‚ฐ 2 ๋ถ„ํฌ ํ‘œ์—์„œ ํ™•๋ฅ ์€ ์–ด๋Š ๋ฐฉํ–ฅ๋ถ€ํ„ฐ ๊ณ„์‚ฐํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. 2 ๋ถ„ํฌ๋ฅผ ๋งŒ๋“  ์‚ฌ๋žŒ์— ๋”ฐ๋ผ ๋ฐฉํ–ฅ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ํ‘œ์—์„œ ๊ทธ๋ž˜ํ”„์˜ ๋ฉด์ ์€ ์‹์„ ๋ณด๋ฉด [ 2 โˆ’ ) ์ด๋‹ค. 2 ๋ถ„ํฌ ํ‘œ์—์„œ 2 ๋Š” ์ด์ฐจ์› ํ‰๋ฉด์—์„œ x ์ถ• ์ขŒํ‘œ๊ฐ’์ด๋‹ค. ์ฒซ ์—ด d.f๋Š” ์ž์œ ๋„(degree of freedom)๋‹ค. ์ฒซ ํ–‰ ๋Š” [ 2 โˆ’ ) ๊นŒ์ง€ ๋ฉด์ ์ด๋‹ค. ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฐ’์€ 2 ๋ถ„ํฌ์—์„œ 2 ๊ฐ’์œผ๋กœ ์ขŒํ‘œํ‰๋ฉด์—์„œ ์ถ• ๊ฐ’์ด๋‹ค. ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์„ ์ œ์™ธํ•œ ๊ฐ’์€ ์ž์œ ๋„๊ฐ€ d.f์ด๊ณ  ๋ฉด์ ์ด ์ธ ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์˜ ๊ต์ฐจ์ ์˜ 2 ๊ฐ’์ด๋‹ค. ๊ทธ๋ฆผ์—์„œ ์ฒซ ์—ด d.f๊ฐ€ 5์ด๊ณ  ์ฒซ ํ–‰ ๊ฐ€ 0.95 ์ผ ๋•Œ ๊ต์ฐจ์ ์€ 2 ๋ถ„ํฌ ๊ฐ’ 1.1455 ์ด๋‹ค. 2 ๋ถ„ํฌ ํ‘œ์˜ ๋ฌธ์ œ์ ์€ ์ œํ•œ์ ์ธ ๋ฉด์ ์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’๋งŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ธฐ์— ์ž์„ธํ•œ ๊ฒƒ์€ ์ปดํ“จํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•œ๋‹ค. 2 ๋ถ„ํฌ ๋ฌธ์ œ 2 ๋ถ„ํฌ ํ‘œ์—์„œ ์ž์œ ๋„๊ฐ€ 5์ธ ์ƒ, ํ•˜์œ„ % ์˜ ํ™•๋ฅ ์„ ์ฃผ๋Š” ๊ฐ’์„ ์ฐพ์•„๋ผ. 2 ๋ถ„ํฌ ํ‘œ๋ฅผ ๋ณด๋ฉด 0.95 ( ) 1.15 0.05 ( ) 11.07 F ๋ถ„ํฌ ํ†ต๊ณ„ํ•™์ž ํ”ผ์…”(R. A. Fisher)๊ฐ€ ์ œ์•ˆํ•œ ํ™•๋ฅ ๋ถ„ํฌ ์„ธ ์ง‘๋‹จ์˜ ํ‰๊ท  ๋น„๊ต์— ์‚ฌ์šฉ ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ ๋น„์œจ ์ถ”๋ก ์— ์‚ฌ์šฉ(ํ•œ ์ง‘๋‹จ ๋ถ„์‚ฐ ์ถ”๋ก ์€ 2 ๋ถ„ํฌ) ( ; 1 ฮฝ) ฮ“ ( 1 ฮฝ 2 ) ( 1 ) ( 2 ) ( 1 2 ) 1 x 1 2 [ + ( 1 2 ) ] 1 ฮฝ 2 F - ๋ถ„ํฌ๋Š” ์„œ๋กœ ๋…๋ฆฝ์ธ v 2 ฯ‡ 2 ์ผ ๋•Œ = v 2 v ฯ‡ 2 / 2 ์นด์ด ์ œ๊ณฑ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์–‘์˜ ๊ตฌ๊ฐ„์—์„œ๋งŒ ํ™•๋ฅ  ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค. ๋ถ„ํฌ ๋ชจ์–‘์€ ๋น„๋Œ€์นญํ˜•์ด๋‹ค. ฮฑ v, 2 1 F โˆ’ , 2 v ๊ณผ ๊ฐ™๋‹ค. t ๋ถ„ํฌ์™€ F ๋ถ„ํฌ์˜ ๊ด€๊ณ„ : ฮฑ 2 ( ) F, , , 2 p ( โˆ’ ) โˆ’ F, F ๋ถ„ํฌ๋“ค F ๋ถ„ํฌ ํ‘œ๋กœ ํ™•๋ฅ  ๊ณ„์‚ฐ F ๋ถ„ํฌ ํ‘œ๋Š” ํ™•๋ฅ ๋ถ„ํฌ ํ‘œ์—์„œ F - ๋ถ„ํฌ ํ‘œ( = 0.05 )๋‚˜ F - ๋ถ„ํฌ ํ‘œ( = 0.01 )์„ ์ œ๊ณตํ•œ๋‹ค. F ๋ถ„ํฌ ํ‘œ์—์„œ ํ™•๋ฅ ์€ ์–ด๋Š ๋ฐฉํ–ฅ๋ถ€ํ„ฐ ๊ณ„์‚ฐํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. F ๋ถ„ํฌ๋ฅผ ๋งŒ๋“  ์‚ฌ๋žŒ์— ๋”ฐ๋ผ ๋ฐฉํ–ฅ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ํ‘œ์—์„œ = 0.5 ๋Š” F ๋ถ„ํฌ์—์„œ [ , โˆž ) ์˜ ๋ฉด์ ์ด๋‹ค. F ๋ถ„ํฌ ํ‘œ์—์„œ๋Š” ์ด์ฐจ์› ํ‰๋ฉด์—์„œ ์ถ• ์ขŒํ‘œ๊ฐ’์ด๋‹ค. ์ฒซ ํ–‰ 1 ์€ F ๋ถ„ํฌ์˜ ์ฒซ ๋ฒˆ์งธ ์ž์œ ๋„(degree of freedom)๋‹ค. ์ฒซ ์—ด 2 ๋Š” F ๋ถ„ํฌ์˜ ๋‘ ๋ฒˆ์งธ ์ž์œ ๋„(degree of freedom)๋‹ค. ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฐ’์€ F ๋ถ„ํฌ์—์„œ = 0.05 ์ผ ๋•Œ ๊ฐ’์œผ๋กœ ์ขŒํ‘œํ‰๋ฉด์—์„œ ์ถ• ๊ฐ’์ด๋‹ค. ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์„ ์ œ์™ธํ•œ ๊ฐ’์€ ์ฒซ ๋ฒˆ์งธ ์ž์œ ๋„๊ฐ€ 1 ์ด๊ณ  ๋‘ ๋ฒˆ์งธ ์ž์œ ๋„๊ฐ€ 2 ์ด๋ฉฐ ๋ฉด์  = 0.05 ์ผ ๋•Œ ๊ต์ฐจ์ ์˜ ๊ฐ’์ด๋‹ค. ๊ทธ๋ฆผ์—์„œ ์ฒซ ๋ฒˆ์งธ ์ž์œ ๋„๊ฐ€ 1 10 ์ด๊ณ  ๋‘ ๋ฒˆ์งธ ์ž์œ ๋„๊ฐ€ 2 20 ์ด๋ฉฐ ๋ฉด์  = 0.05 ์ผ ๋•Œ ๊ต์ฐจ์ ์€ F ๋ถ„ํฌ ๊ฐ’ 2.35 ์ด๋‹ค. F ๋ถ„ํฌ ํ‘œ์˜ ๋ฌธ์ œ์ ์€ ์ œํ•œ์ ์ธ ๋ฉด์ ์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’๋งŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ธฐ์— ์ž์„ธํ•œ ๊ฒƒ์€ ์ปดํ“จํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•œ๋‹ค. 14. ํ‘œ๋ณธ๋ถ„ํฌ ํ‘œ๋ณธ๋ถ„ํฌ ๋ชจ์ง‘๋‹จ์—์„œ ๋ฝ‘์€ ํ‘œ๋ณธ์˜ ๋ถ„ํฌ ํ†ต๊ณ„ ์šฉ์–ด ์ถ”๋ก (ๆŽจ่ซ–, inference) : ํ‘œ๋ณธ์—์„œ ๋ชจ์ง‘๋‹จ์˜ ์„ฑ์งˆ์„ ์•Œ์•„๋‚ด๋Š” ๊ฒƒ ๋ชจ์ˆ˜(ๆฏๆ•ธ, parameter) : ์ˆ˜์น˜๋กœ ํ‘œํ˜„๋˜๋Š” ๋ชจ์ง‘๋‹จ์˜ ํŠน์„ฑ(์˜ˆ : , ) ํ†ต๊ณ„๋Ÿ‰(็ตฑ่จˆ้‡, statistic) : ํ‘œ๋ณธ์˜ ๊ด€์ธก ๊ฐ’์—์„œ ๊ณ„์‚ฐ๋œ ์ˆ˜์น˜ ํ•จ์ˆ˜(์˜ˆ : โ€• s ) ํ‘œ๋ณธ๋ถ„ํฌ(ๆจ™ๆœฌๅˆ†ๅธƒ, sampling distribution) : ํ†ต๊ณ„๋Ÿ‰์˜ ํ™•๋ฅ ๋ถ„ํฌ ์ž„์˜ํ‘œ๋ณธ(ไปปๆ„ๆจ™ๆœฌ, random sample) : ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœํ•œ ํ‘œ๋ณธ์œผ๋กœ 1 X, , n ์€ ์„œ๋กœ ๋…๋ฆฝ์ด๊ณ  ๋ชจ๋‘ ๋ชจ์ง‘๋‹จ์˜ ๋ถ„ํฌ์™€ ๋™์ผํ•œ ๋ถ„ํฌ๋ฅผ ๊ฐ€์ง iid(independent and identically distributed) : ์ž„์˜ํ‘œ๋ณธ๊ณผ ๋™์ผํ•œ ์˜๋ฏธ ํ†ต๊ณ„๋Ÿ‰ ์„ฑ์งˆ ๋ชจ์ˆ˜์˜ ์ฐธ๊ฐ’๊ณผ ํ†ต๊ณ„๋Ÿ‰์€ ํ†ต์ƒ์ ์œผ๋กœ ๊ฐ™์ง€ ์•Š์Œ ํ†ต๊ณ„๋Ÿ‰์€ ์ถ”์ถœํ•œ ํ‘œ๋ณธ์—๋งŒ ์˜ํ–ฅ์„ ๋ฐ›์Œ ๋‹ค๋ฅธ ํ‘œ๋ณธ์€ ์ถ”์ถœํ•˜๋ฉด ํ†ต๊ณ„๋Ÿ‰์˜ ๊ฐ’์€ ๋ณ€ํ•จ ํ‘œ๋ณธ๋ถ„ํฌ ์ž‘์„ฑ๋ฐฉ๋ฒ• ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ํ‘œ๋ณธ์˜ ์ข…๋ฅ˜ ๋‚˜์—ด ๊ฐ ํ‘œ๋ณธ์—์„œ ํ†ต๊ณ„๋Ÿ‰์˜ ๊ฐ’์„ ๊ตฌํ•จ ํ†ต๊ณ„๋Ÿ‰์˜ ๊ฐ’์— ๋Œ€์‘ํ•˜๋Š” ํ™•๋ฅ  ๊ณ„์‚ฐ ํ‘œ๋ณธํ‰๊ท ์˜ ๋ถ„ํฌ ํ‘œ๋ณธํ‰๊ท  โ€• ์˜ ๊ธฐ๋Œ“๊ฐ’๊ณผ ๋ถ„์‚ฐ ๋ชจ์ง‘๋‹จ๊ธฐ๋Œ€๊ฐ’ ( โ€• ) E [ 1 โ‹ฏ X n ] 1 [ ( 1 ) ฮผ โ‹ฏ E ( n ) ฮผ n ] ฮผ (๋ชจ์ง‘๋‹จ ๊ธฐ๋Œ“๊ฐ’) ๋ชจ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ ํ‘œ๋ณธ์˜ ํฌ๊ธฐ a ( โ€• ) V r [ 1 โ‹ฏ X n ] 1 2 [ a ( 1 ) ฯƒ + + a ( n ) ฯƒ โž ] ฯƒ n ๋ชจ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ ํ‘œ๋ณธ์˜ ํฌ๊ธฐ ๋ชจ์ง‘๋‹จ์˜ ํ‘œ์ค€ํŽธ์ฐจ ํ‘œ๋ณธ์˜ ํฌ๊ธฐ d ( โ€• ) V r ( โ€• ) ฯƒ = ๋ชจ์ง‘๋‹จ์˜ ํ‘œ์ค€ํŽธ์ฐจ ํ‘œ๋ณธ์˜ ํฌ๊ธฐ ์ •๊ทœ๋ถ„ํฌ์—์„œ ํ‘œ๋ณธํ‰๊ท  โ€• ๋ถ„ํฌ ๋ชจํ‰๊ท ์ด์ด๊ณ  ๋ชจ ํ‘œ์ค€ํŽธ์ฐจ๊ฐ€ ์ธ ์ •๊ทœ๋ชจ์ง‘๋‹จ์—์„œ ๊ฐœ์˜ ํ‘œ๋ณธ์„ ์ž„์˜๋กœ ์ถ”์ถœํ•  ๋•Œ, ํ‘œ๋ณธํ‰๊ท  โ€• ์˜ ๋ถ„ํฌ๋Š” ( , 2 n ) ์ธ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค. ๋ชจ์ง‘๋‹จ์ด ์ •๊ทœ๋ถ„ํฌ์ด๋ฉด ํ‘œ๋ณธํ‰๊ท  โ€• ์˜ ๋ถ„ํฌ๋„ ์ •๊ทœ๋ถ„ํฌ์ด๋‹ค. ํ‘œ๋ณธ๋ถ„ํฌ ์˜ˆ ์ˆซ์ž (1,2,3)์—์„œ ์ค‘๋ณต์„ ํ—ˆ๋ฝํ•˜์—ฌ ์ˆซ์ž 2 ๊ฐœ๋ฅผ ๋ฝ‘๋Š” ๊ฒฝ์šฐ๋Š” 2 9 ๊ฐ€์ง€. f ( ) f ( ) 2 ( ) 1 1/3 1/3 1/3 2 1/3 2/3 4/3 3 1/3 3/3 9/3 ํ•ฉ๊ณ„ 1 6/3 14/3 ๋ชจ์ง‘๋‹จ ๋ถ„ํฌ = 3 2 2 14 โˆ’ 2 2 ฯƒ 2 ( 1 x) (1,1) (1,2) (1,3) (2,1) (2,2) (2,3) (3,1) (3,2) (3,3) โ€• 1 1.5 2 1.5 2 2.5 2 2.5 3 ํ™•๋ฅ  9 9 9 9 9 9 9 9 9 โ€• ๊ฐ’๊ณผ ํ™•๋ฅ  โ€• ๊ฐ€ ๊ฐ€์ง€๋Š” ๊ฐ’( โ€• ) 1 1.5 2 2.5 3 ํ•ฉ๊ณ„ ( โ€• ) 9 9 9 9 9 x f ( โ€• ) 9 9 9 9 9 3 โ€• f ( โ€• ) 9 4.5 12 12.5 9 13 X์˜ ํ™•๋ฅ ๋ถ„ํฌ = 3 2 2 13 โˆ’ 2 1 = 2 ฯƒ 1 = 2 ์ค‘์‹ฌ๊ทนํ•œ์ •๋ฆฌ ๋ชจ์ง‘๋‹จ์˜ ํ‰๊ท ์ด์ด๊ณ  ๋ถ„์‚ฐ์ด 2 ์ผ ๋•Œ, ์ž„์˜ํ‘œ๋ณธ(random sample) ๊ฐœ์˜ ํ‘œ๋ณธํ‰๊ท ์„ โ€• ๋ผ๊ณ  ํ•˜์ž. ์ค‘์‹ฌ๊ทนํ•œ์ •๋ฆฌ(ไธญๅฟƒๆฅต้™ๅฎš็†, central limit theorem)} ํ‘œ๋ณธํ‰๊ท  โ€• ์˜ ๋ถ„ํฌ๊ฐ€ lim โ†’ Pr ( n z ) ฮฆ ( ) lim โ†’ Pr ( โ€• โˆ’ ฯƒ n z ) ฮฆ ( ) โ†’ ์ผ ๋•Œ N(0,1)์ธ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค. ํ‘œ๋ณธ ์ˆ˜๋ฅผ ๋ฌดํ•œ๋Œ€๋กœ ๋ฝ‘๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ์‹ค์ œ๋กœ ์ด ์ถฉ๋ถ„ํžˆ ํด ๋•Œ(๋ณดํ†ต 30 ์ด์ƒ) ํ‘œ๋ณธํ‰๊ท  โ€•๋Š” ( , 2 n ) ์ธ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค. ์ด ์ •๋ฆฌ๋Š” ๋ชจ์ง‘๋‹จ์˜ ๋ถ„ํฌ๋ฅผ ๋ชจ๋ฅด๋”๋ผ๋„ ํ‘œ๋ณธํ‰๊ท ์˜ ๋ถ„ํฌ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ๋ถ„์„์— ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค. ์ค‘์‹ฌ๊ทนํ•œ์ •๋ฆฌ ์˜ˆ ํ‰๊ท ์ด 82์ด๊ณ  ๋ถ„์‚ฐ์ด 144์ธ ๋ชจ์ง‘๋‹จ์— ๋Œ€ํ•˜์—ฌ ํ‘œ๋ณธ์˜ ํฌ๊ธฐ๊ฐ€ 64์ผ ๋•Œ, ํ‘œ๋ณธํ‰๊ท ์ด 81์—์„œ 83 ์‚ฌ์ด์— ์žˆ์„ ํ™•๋ฅ ์€? ์ค‘์‹ฌ๊ทนํ•œ์ •๋ฆฌ์— ์˜ํ•˜์—ฌ ํ‘œ๋ณธํ‰๊ท ์€ โ€• N ( 82 ( 12 ) ) ์ด๋‹ค. [ 81 X โ‰ค 83 ] P [ 81 82 / โ‰ค โ€• 82 / โ‰ค 83 82 / ] P [ 0.67 Z 0.67 ] 0.4950 ์œ„์˜ ๋ฌธ์ œ์—์„œ ํ‘œ๋ณธ์˜ ํฌ๊ธฐ๊ฐ€ 100์ผ ๋•Œ ํ™•๋ฅ ์„ ๊ตฌํ•˜๋ฉด $$ [ 81 X โ‰ค 83 ] P [ 81 82 / โ‰ค โ€• 82 / โ‰ค 83 82 / ] P [ 1 Z 1 ] 0.5953 = 64 ์ผ ๋•Œ ํ™•๋ฅ  = 100 ์ผ ๋•Œ ํ™•๋ฅ  15. ํ†ต๊ณ„์  ์ถ”๋ก  ํ†ต๊ณ„์  ์ถ”๋ก  ํ†ต๊ณ„์  ์ถ”๋ก (็ตฑ่จˆ็š„ ๆŽจ่ซ–, statistical inference)์€ ์ถ”์ถœ๋œ ํ‘œ๋ณธ์œผ๋กœ๋ถ€ํ„ฐ ๋ชจ์ง‘๋‹จ์˜ ์ผ๋ฐ˜์ ์ธ ํŠน์„ฑ์„ ์ถ”๋ก ํ•œ๋‹ค. ์ถ”์ • ์  ์ถ”์ •(้ปžๆชขๅฎš, point estimation) : ๋ฏธ์ง€์ˆ˜์ธ ๋ชจ์ˆ˜์— ๋Œ€ํ•œ ์ถ”์ธก ๊ตฌ๊ฐ„์ถ”์ •(ๅ€้–“ๆŽจๅฎš, interval estimation) : ์ถ”์ธก ์ง€๋ฅผ ์ˆ˜์น˜ํ™”๋œ ์ •ํ™•๋„์™€ ํ•จ๊ป˜ ์ œ์‹œ ๊ฒ€์ • ๋ชจ์ˆ˜์— ๋Œ€ํ•œ ๊ฐ€์„ค์ด ์ ํ•ฉํ•œ์ง€ ์ ํ•ฉํ•˜์ง€ ์•Š์€์ง€ ์ถ”์ถœ๋œ ํ‘œ๋ณธ์œผ๋กœ๋ถ€ํ„ฐ ํŒ๋‹จ ์ถ”๋ก  ์˜ˆ ์–ด๋–ค ์ง‘๋‹จ ๋‚จ์ž ํ‰๊ท  ํ‚ค์— ๋Œ€ํ•œ ์ถ”๋ก  ์  ์ถ”์ •(้ปžๆŽจๅฎš) :๋ฅผ ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ์ถ”์ •ํ•œ๋‹ค. ๊ตฌ๊ฐ„์ถ”์ •(ๅ€้–“ๆŽจๅฎš) :๋ฅผ ํฌํ•จํ•  ๋งŒํ•œ ์ ๋‹นํ•œ ๊ตฌ๊ฐ„์„ ์ •ํ•œ๋‹ค ๊ฐ€์„ค๊ฒ€์ •(ๅ‡่ชชๆชขๅฎš) : ๊ฐ’์ด ๋‹ค๋ฅธ ์ง‘๋‹จ ํ‰๊ท ๊ฐ’ 175cm์™€ ๋‹ค๋ฅธ์ง€ ํŒ๋‹จํ•œ๋‹ค. ์–ด๋–ค ์ง‘๋‹จ ๋‚จ์ž ํ‰๊ท  ํ‚ค์— ๋Œ€ํ•œ ์ถ”๋ก  ๊ฒฐ๊ณผ ์  ์ถ”์ •(้ปžๆŽจๅฎš) : ๋‚จ์ž ํ‰๊ท  ํ‚ค๋Š” 171.37์ผ ๊ฒƒ์ด๋‹ค ๊ตฌ๊ฐ„์ถ”์ •(ๅ€้–“ๆŽจๅฎš) : ๋‚จ์ž ํ‰๊ท  ํ‚ค๋Š” 95% ์‹ ๋ขฐ์ˆ˜์ค€์—์„œ (168.9, 173.83)์— ํฌํ•จ๋  ๊ฒƒ์ด๋‹ค. ๊ฐ€์„ค๊ฒ€์ •(ๅ‡่ชชๆชขๅฎš) : ๊ฐ’์ด ๋‹ค๋ฅธ ์ง‘๋‹จ ํ‰๊ท ๊ฐ’์ธ 175cm์™€ ๋‹ค๋ฅธ์ง€ ํŒ๋‹จ์€ ์œ ์˜์ˆ˜์ค€ 5%์—์„œ ์œ ์˜ ํ™•๋ฅ ์ด 0.006์œผ๋กœ ํ‚ค๊ฐ€ 175cm๋ณด๋‹ค ์ž‘๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ถ„์„ ์ž๋ฃŒ ์ถ”๋ก ์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ๋Š” ํ‰๊ท  ๋น„๊ต ๋‹จ์ผ ํ‘œ๋ณธ์— ์‚ฌ์šฉํ•œ ์ž๋ฃŒ์ด๋‹ค. 1. ์ถ”์ • ๋ชจ์ง‘๋‹จ์—์„œ ๋ฝ‘์€ ํ‘œ๋ณธ์˜ ๋ถ„ํฌ ์  ์ถ”์ •(point estimation) ๋ชจ์ง‘๋‹จ ํ‰๊ท ์ธ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ์ถ”์ • ๋ชจ์ˆ˜ : ๋ชจ์ง‘๋‹จ์˜ ํ‰๊ท  ๋กœ ์ถ”์ • ๋Œ€์ƒ ์ž๋ฃŒ : ํ‰๊ท ์ด, ํ‘œ์ค€ํŽธ์ฐจ๊ฐ€ ์—์„œ ์ž„์˜์ถ”์ถœํ•œ ํ‘œ๋ณธ( 1 โ‹ฏ X) ์ถ”์ •๋Ÿ‰ : ํ‘œ๋ณธํ‰๊ท  โ€• ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋ชจ์ง‘๋‹จ ํ‰๊ท  ๋กœ ์ถ”์ •. ( โ€• ) ฮผ ์ถ”์ •๋Ÿ‰์˜ ํ‘œ์ค€์˜ค์ฐจ : ์ถ”์ •๋Ÿ‰์˜ ์ •ํ™•๋„๋กœ. ( โ€• ) ฯƒ. ๋ชจ์ง‘๋‹จ ํ‰๊ท ์ธ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ์ถ”์ • ์˜ˆ ๋ชจ์ˆ˜ : ๋‚จ์ž ์ „์ฒด ํ‰๊ท  ๋Š” ์‹ค์ œ๋กœ ๋ชจ๋ฆ„ ์ž๋ฃŒ : ๋‚จ์ž 19๋ช…์€ ํ‘œ๋ณธ( 1 โ‹ฏ X 19 )์ด๋‹ค. ์ถ”์ •๋Ÿ‰ : ํ‘œ๋ณธํ‰๊ท  โ€• 171.37 ์ถ”์ •๋Ÿ‰์˜ ํ‘œ์ค€์˜ค์ฐจ : n ฯƒ ๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†์Œ. ์ถ”์ •๋œ ํ‘œ์ค€์˜ค์ฐจ : n 5.112 19 1.173 ๊ตฌ๊ฐ„์ถ”์ •(interval estimation) ๊ตฌ๊ฐ„์ถ”์ •(ๅ€้–“ๆŽจๅฎš, Interval Estimation) : ์ถ”์ •๋Ÿ‰ ๋ถ„ํฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œ๋ณธ์œผ๋กœ๋ถ€ํ„ฐ ๋ชจ์ˆ˜๊ฐ€ ํฌํ•จ๋  ๊ฒƒ์ด๋ผ ์˜ˆ์ƒ๋˜๋Š” ๊ตฌ๊ฐ„์ถ”์ • ์‹ ๋ขฐ๊ตฌ๊ฐ„(ไฟก่ณดๅ€้–“, Confidence Interval) : ๊ตฌ๊ฐ„์ถ”์ •์—์„œ ์ œ์‹œ๋˜๋Š” ๊ทธ ๊ตฌ๊ฐ„ ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ (ํ•˜ํ•œ ๊ฐ’, ์ƒํ•œ ๊ฐ’)์˜ ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑ ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœํ•œ ํ‘œ๋ณธ๋งˆ๋‹ค ๊ณ„์‚ฐ๋˜๋Š” ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ์„œ๋กœ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ ๊ฐ€์žฅ ํ™•์‹คํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„ : ( โˆž โˆž ) ์–ด๋–ค ์ •๋ณด๋„ ์ œ๊ณตํ•˜์ง€ ๋ชปํ•จ ์‹ ๋ขฐ์ˆ˜์ค€(ไฟก่ณดๆฐดๆบ–, Confidence Level)}} : ์‹ ๋ขฐ๊ตฌ๊ฐ„์— ๋ชจ์ˆ˜๋ฅผ ํฌํ•จํ•  ํ™•๋ฅ ๋กœ ๋ณดํ†ต 90%, 95%, 99%๋ฅผ ์‚ฌ์šฉ ์‹ ๋ขฐ์ˆ˜์ค€ ๋˜๋Š” ์‹ ๋ขฐ๋„๋Š” 100 ( โˆ’ ) ๋˜๋Š” โˆ’๋กœ ํ‘œ์‹œ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„ ๊ณ„์‚ฐ ๊ณผ์ • ์ฃผ์–ด์ง„ ์ •๊ทœ๋ถ„ํฌ โˆผ ( , 2 ) ๋ฅผ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ( , 2 ) ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ์‹ ๋ขฐ์ˆ˜์ค€ 100 ( โˆ’ )์— ๋Œ€ํ•œ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์˜ ๋ถ„์œ„์ˆ˜ ฮฑ 2 ๋ฅผ ๊ตฌํ•œ๋‹ค. ์‹ ๋ขฐ์ˆ˜์ค€ 100 ( โˆ’ )์— ๋Œ€ํ•œ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์˜ ๋ฒ”์œ„๋ฅผ ๊ตฌํ•œ๋‹ค. ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์˜ ๋ฒ”์œ„์— ๋Œ€ํ•œ ๋ถ€๋“ฑ์‹์„ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ๋ถ€๋“ฑ์‹์œผ๋กœ ๊ตฌํ•œ๋‹ค.์— ๋Œ€ํ•œ ๋ถ€๋“ฑ์‹์˜ ๋ฒ”์œ„๊ฐ€ ์‹ ๋ขฐ์ˆ˜์ค€ 100 ( โˆ’ )์— ๋Œ€ํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด๋‹ค. ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„ ๊ณ„์‚ฐ ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœํ•œ ํ‘œ๋ณธ์ด ํฐ ๊ฒฝ์šฐ(์•ฝ 30 ์ด์ƒ)๋Š” ์ค‘์‹ฌ๊ทนํ•œ์ •๋ฆฌ์— ์˜ํ•˜์—ฌ ํ‘œ๋ณธํ‰๊ท  โ€• N ( , 2 n ) ์„ ๋”ฐ๋ฅธ๋‹ค. ์ด ๋ถ„ํฌ๋ฅผ ํ‘œ์ค€ํ™”ํ•˜๋ฉด ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ( , ) ์ด ๋œ๋‹ค. ( X โˆ’ ฯƒ n < ฮฑ 2 ) 1 ฮฑ ( z / ฯƒ < โ€• ฮผ z / ฯƒ) 1 ฮฑ ( โ€• z / ฯƒ < < โ€• z / ฯƒ โ€• ) 1 ฮฑ ์œ„ ์‹์—์„œ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ 100 ( โˆ’ ) ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ( โ€• z / ฯƒ, โ€• z / ฯƒ) ์ด๋‹ค. ๋ชจ์ˆ˜์— ๋Œ€ํ•œ 100 ( โˆ’ ) ์‹ ๋ขฐ๊ตฌ๊ฐ„ ์ถ”์ •๋Ÿ‰ ์˜ค์ฐจํ•œ๊ณ„ ์ถ”์ •๋Ÿ‰ ๋ฒˆ์งธ ๋ถ„์œ„์ˆ˜ ๋ชจ์ˆ˜์˜ ํ‘œ์ค€์˜ค์ฐจ ์ถ”์ •๋Ÿ‰ ( ^ ) ์˜ค์ฐจํ•œ๊ณ„(margin of error) ์ถ”์ •๋Ÿ‰ ( ^ ) ฮฑ ๋ฒˆ์งธ ๋ถ„์œ„์ˆ˜ ( 2 th quantile ) ๋ชจ์ˆ˜์˜ ํ‘œ์ค€์˜ค์ฐจ (s.e( ^ ) ) โ€• Z / ร— n ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„ - ๊ทธ๋ž˜ํ”„๋กœ ์‹ ๋ขฐ๊ตฌ๊ฐ„์˜ ๊ธธ์ด ๊ฐœ๋… ์กฐ๊ฑด์ด ๋ชจ๋‘ ๋™์ผํ•œ ์กฐ๊ฑด์—์„œ ์‹ ๋ขฐ๋„ 100 ( โˆ’ ) ๊ฐ€ ์ปค์ง€๊ฑฐ๋‚˜ ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœํ•˜๋Š” ํ‘œ๋ณธ์˜ ๊ฐœ์ˆ˜ ์ด ์ž‘์•„์ง€๊ฑฐ๋‚˜ ๋ถ„์‚ฐ 2 ์ด ์ปค์ง€๋ฉด ์‹ ๋ขฐ๊ตฌ๊ฐ„์˜ ๊ธธ์ด๋Š” ์ปค์ง€๊ฒŒ ๋œ๋‹ค. ๋‚จ์ž ํ‚ค์— ๋Œ€ํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„ ํ•™์ƒ ํ‰๊ท  ํ‚ค์— ๋Œ€ํ•œ 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„ ( โ€• Z / ร— n X + ฮฑ 2 ฯƒ) ( 171.37 1.96 5.112 19 171.37 1.96 5.112 19 ) ( 169.07 173.67 ) ๋ฐฐ์˜ ํ‰๊ท  ์งˆ๋Ÿ‰์— ๋Œ€ํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„ ์–ด๋Š ๋ฐฐ๋†์žฅ์—์„œ ์˜ฌํ•ด ์ˆ˜ํ™•ํ•œ ๋ฐฐ์˜ ํ‰๊ท ๋ฌด๊ฒŒ( )์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ๋ฐฐ 36 ๊ฐœ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๋ฌด๊ฒŒ๋ฅผ ๋‹ฌ์•˜๋”๋‹ˆ โ€• 706 , s 25 ์ด ๋‚˜์™”๋‹ค๊ณ  ํ•œ๋‹ค. ์ด๋•Œ์— ๋Œ€ํ•œ 95%, 99% ์‹ ๋ขฐ๊ตฌ๊ฐ„์„ ๊ตฌํ•˜๊ณ  ๋น„๊ตํ•˜์ž. ๋ฐฐ ํ‰๊ท  ์งˆ๋Ÿ‰์— ๋Œ€ํ•œ 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„ ๋ฐฐ ํ‰๊ท  ์งˆ๋Ÿ‰์— ๋Œ€ํ•œ 99% ์‹ ๋ขฐ๊ตฌ๊ฐ„ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„ ํ’€์ด 95 ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ = 0.05 ์ด๋ฏ€๋กœ ฮฑ 2 1.96 ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ฃผ์–ด์ง„ ์ž๋ฃŒ๋กœ๋ถ€ํ„ฐ 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค ( 706 1.96 25 36 706 1.96 25 36 ) ( 697.83 714.17 ) or 706 8.17 99 ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ = 0.01 ์ด๋ฏ€๋กœ ฮฑ 2 2.575 ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ฃผ์–ด์ง„ ์ž๋ฃŒ๋กœ๋ถ€ํ„ฐ 99\% ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค ( 706 2.575 25 36 706 2.575 25 36 ) ( 695.27 716.73 ) or 706 10.73 ์œ„์˜ ๊ฒฐ๊ณผ๋กœ ์‹ ๋ขฐ์ˆ˜์ค€์ด ์ปค์ง€๋Š” ๊ฒƒ์€ ฮฑ 2 ๊ฐ€ ์ปค์ง€๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ 95 ์‹ ๋ขฐ๊ตฌ๊ฐ„๋ณด๋‹ค 99 ์‹ ๋ขฐ๊ตฌ๊ฐ„์˜ ๊ธธ์ด๊ฐ€ ๋” ๊ธธ๋‹ค. ์‹ ๋ขฐ๊ตฌ๊ฐ„ ์˜๋ฏธ ํ‰๊ท  ๊ฐ€ 0์ด๊ณ , ๋ถ„์‚ฐ 2 ์„ ๋ชจ๋ฅด๋Š” ์ •๊ทœ๋ถ„ํฌ์—์„œ ํ‘œ๋ณธ ์ˆ˜๊ฐ€ = ์ธ ํ‘œ๋ณธ ์ถ”์ถœ ๊ณผ์ •์„ 100ํšŒ ๋ฐ˜๋ณตํ•˜๊ณ , ์ด ๋ชจ๋“  ๊ณผ์ •์„ 10ํšŒ ๋ฐ˜๋ณตํ•œ ๊ฒฐ๊ณผ ๋ชจํ‰๊ท  =์— ๋Œ€ํ•œ 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„ 100 10 ๋ชจ ํ‰๊ท ์„ ํฌํ•จํ•˜๋Š” ์‹ ๋ขฐ๊ตฌ๊ฐ„์˜ ์ƒ๋Œ€๋„์ˆ˜๊ฐ€ 953 1000 0.953 https://hmkang98.github.io/stat/์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ 2. ๊ฒ€์ • ๋ชจ์ง‘๋‹จ ํ‰๊ท ์— ๋Œ€ํ•œ ๊ฒ€์ • ๊ฐ€์„ค๊ฒ€์ •(ๅ‡่ชชๆชขๅฎš)์ด ํ•„์š”ํ•œ ์˜ˆ ๋†’์€ ํ˜ˆ์ค‘ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜๋Š” ์‹ฌํ˜ˆ๊ด€ ์งˆํ™˜์˜ ์›์ธ์œผ๋กœ ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ์–ด๋Š ๋„์‹œ์—์„œ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜๋ฅผ ๋‚ฎ์ถ”๋ฉด ์‹œ๋ฏผ๋“ค์ด ๊ฑด๊ฐ•ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•œ๋‹ค. ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์€ ์ข‹์€ ์‹์‚ฌ์Šต๊ด€๊ณผ ๊ทœ์น™์ ์ธ ์šด๋™ ๋ฐฉ๋ฒ•์„ ์–ธ๋ก ๋งค์ฒด๋‚˜ ์˜๋ฃŒ๊ธฐ๊ด€์—์„œ 1๋…„๊ฐ„ ํ™๋ณดํ•˜์˜€๋‹ค. ์บ ํŽ˜์ธ์ด ์„ฑ์ธ์˜ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์–‘์„ ์ค„์ด๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์—ˆ๋Š”์ง€ ๊ฒ€์ฆํ•˜๋ ค๊ณ  ๊ทธ ๋„์‹œ์˜ ์„ฑ์ธ 50๋ช…์„ ๋Œ€์ƒ์œผ๋กœ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜๋ฅผ ์ธก์ •ํ•˜์˜€๋‹ค. ์บ ํŽ˜์ธ์„ ์‹œ์ž‘ ์ „์— ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ, ์ด ๋„์‹œ ์„ฑ์ธ์˜ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜๋Š” ํ‰๊ท ์ด 200(mg/dL)์ด๊ณ  ํ‘œ์ค€ํŽธ์ฐจ๋Š” 28(mg/dL)์ธ ๋ถ„ํฌ์˜€๋‹ค. ์บ ํŽ˜์ธ ์ง„ํ–‰ํ•œ ํ›„ ์„ฑ์ธ 50๋ช…์„ ๋ฝ‘์•„ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜์˜ ํ‰๊ท ( โ€• )์„ ๊ณ„์‚ฐํ•˜์˜€๋‹ค. ์ด ๊ฒฝ์šฐ ์„ฑ์ธ์˜ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์–‘์ด ์ค„์—ˆ๋Š”์ง€ ํŒ๋‹จํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜๊ฐ€ ๋‚ฎ์•„์กŒ๋Š”์ง€ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋ชจ๋“  ์„ฑ์ธ์„ ์กฐ์‚ฌํ•˜๋Š” ๊ฒƒ์€ ๋น„์šฉ๋„ ๋งŽ์ด ํ•„์š”ํ•˜๊ณ  ์‰ฝ์ง€ ์•Š์Œ ์บ ํŽ˜์ธ ํ›„ ๊ทธ ๋„์‹œ์˜ ์„ฑ์ธ ์ค‘ ๋ช… ํ‘œ๋ณธ์ถ”์ถœ ์บ ํŽ˜์ธ ํ›„ ์„ฑ์ธ์˜ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜ ๋ชจํ‰๊ท  ๊ฐ€ 200mg/dL๋ณด๋‹ค ์ž‘๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ†ต๊ณ„์ ์œผ๋กœ ํŒ๋‹จ โ€• ๊ฐ€ ์–ผ๋งˆ๋‚˜ ์ž‘์•„์•ผ ๊ฐ€ 200mg/dL๋ณด๋‹ค ์ž‘๋‹ค๊ณ  ์ฃผ์žฅํ•  ์ˆ˜ ์žˆ์„๊นŒ? ๊ฐ€์„ค๊ฒ€์ • ๊ณผ์ • 3 ๋‹จ๊ณ„ ๊ฒ€์ •ํ•  ๊ฐ€์„ค์„ ์„ค์ •ํ•œ๋‹ค. ํ‘œ๋ณธ์„ ๋ฝ‘์•„ ๊ฐ€์„ค๊ฒ€์ •์— ํ•„์š”ํ•œ ๊ณ„์‚ฐ์„ ํ•œ๋‹ค. ์„ค์ •ํ•œ ๊ฐ€์„ค์ด ์˜ณ์€์ง€ ์˜ณ์ง€ ์•Š์€์ง€ ํŒ๋‹จํ•œ๋‹ค. ๊ฐ€์„ค๊ฒ€์ •์— ์‚ฌ์šฉํ•˜๋Š” ์šฉ์–ด(็”จ่ชž)์™€ ๊ฐœ๋…(ๆงชๅฟต) ๊ฐ€์„ค๊ฒ€์ •(ๅ‡่ชชๆชขๅฎš, testing statistical hypothesis) ๋ชจ์ˆ˜์— ๋Œ€ํ•œ ๊ฐ€์„ค์ด ์ ํ•ฉํ•œ์ง€ ์ถ”์ถœํ•œ ํ‘œ๋ณธ์œผ๋กœ ํŒ๋‹จ ๊ฐ€์„ค(ๅ‡่ชช, hypothesis) ๊ท€๋ฌด๊ฐ€์„ค(ๆญธ็„กๅ‡่ชช, null hypothesis, 0 ) : ์—†๋‹ค๊ณ  ์ฃผ์žฅํ•˜๋Š” ๊ฐ€์„ค ๋Œ€๋ฆฝ๊ฐ€์„ค(ๅฐ็ซ‹ๅ‡่ชช, alternate hypothesis, 1 ) : ์žˆ๋‹ค๊ณ  ์ฃผ์žฅํ•˜๋Š” ๊ฐ€์„ค ๋‘ ๊ฐ€์„ค์€ ์„œ๋กœ ๊ณตํ†ต์ ์ด ์—†๋‹ค. ์ฆ‰ 0 H = ๊ฐ€์„ค ์„ค์ • ์ ์šฉ ์‚ฌ๋ก€ 0 : ๋„์‹œ์˜ ์„ฑ์ธ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜ ํ‰๊ท  ๋Š” 200mg/dL์ด๋‹ค. ์ฆ‰ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜๋Š” ๊ณผ๊ฑฐ 1๋…„ ์ „๊ณผ ์ฐจ์ด๊ฐ€ ์—†๋‹ค. 1 : ๋„์‹œ์˜ ์„ฑ์ธ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜ ํ‰๊ท  ๋Š” 200mg/dL๋ณด๋‹ค ์ž‘๋‹ค. ์ฆ‰ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜๋Š” ๊ณผ๊ฑฐ 1๋…„ ์ „๊ณผ ์ฐจ์ด๊ฐ€ ์žˆ์œผ๋ฉฐ ์ž‘๋‹ค๊ณ  ์ฃผ์žฅํ•œ๋‹ค. ๊ฐ€์„ค ํŒ์ • ์—ฐ๊ตฌ์ž๋Š” ๊ท€๋ฌด๊ฐ€์„ค 0 ํŒ์ • ๊ธฐ์ค€์„ ์„ค์ •ํ•œ๋‹ค. ๊ธฐ์ค€์€ ์ƒ์œ„ 5%, ํ•˜์œ„ 5%, ์ƒ ํ•˜์œ„ 2.5% ๋“ฑ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. ์„ค์ •ํ•œ ๊ธฐ์ค€์— ํฌํ•จ๋˜๋Š”์ง€ ์•Š๋Š”์ง€ ํŒ์ •ํ•˜๋Š” ๊ฒฝ๊ณ„์ ์„ ์ž„๊ณ—๊ฐ’(threshold)์ด๋ผ๊ณ  ํ•œ๋‹ค. ์—ฐ๊ตฌ์ž๋Š” ํ‘œ๋ณธ์—์„œ ํ†ต๊ณ„๋Ÿ‰์„ ๊ณ„์‚ฐํ•œ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐ/์ฑ„ํƒ ํŒ์ •์€ ์ž„๊ณ„์ ์„ ๊ธฐ์ค€์œผ๋กœ ํŒ๋‹จํ•œ๋‹ค. ๊ฐ€์„ค ํŒ์ • ์ ์šฉ ์‚ฌ๋ก€ ๊ท€๋ฌด๊ฐ€์„ค 0 ๋Š” ๋„์‹œ์˜ ์„ฑ์ธ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜ ํ‰๊ท  ๋Š” 200mg/dL์ด๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ์€ ๋„์‹œ์˜ ์„ฑ์ธ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜ ํ‰๊ท  ๋Š” 200mg/dL๋ณด๋‹ค ์ž‘๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค 1 ์„ ๊ทผ๊ฑฐ๋กœ ๊ท€๋ฌด๊ฐ€์„ค 0 ๊ธฐ๊ฐ ํŒ์ • ๊ธฐ์ค€์€ ํ•˜์œ„ 5%์ด๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ๊ธฐ๊ฐ/์ฑ„ํƒ ์ž„๊ณ—๊ฐ’์€ 193.49mg/dL์ด๋‹ค. ํ‘œ๋ณธํ‰๊ท ์ด 193.49mg/dL ์ดํ•˜์ด๋ฉด ๊ท€๋ฌด๊ฐ€์„ค 0 ๋ฅผ ๊ธฐ๊ฐํ•˜๊ณ  ์ดˆ๊ณผ๋ฉด ์ฑ„ํƒํ•œ๋‹ค. ์ถ”์ถœํ•œ ํ‘œ๋ณธ์—์„œ ๊ณ„์‚ฐํ•œ ํ‘œ๋ตจํ‰๊ท ์ด 195mg/dL์ด๋ผ๋ฉด ๊ท€๋ฌด๊ฐ€์„ค 0 ๋ฅผ ์ฑ„ํƒํ•œ๋‹ค. ์ถ”์ถœํ•œ ํ‘œ๋ณธ์—์„œ ๊ณ„์‚ฐํ•œ ํ‘œ๋ตจํ‰๊ท ์ด 192mg/dL์ด๋ผ๋ฉด ๊ท€๋ฌด๊ฐ€์„ค 0 ๋ฅผ ๊ธฐ๊ฐํ•œ๋‹ค. ์‹ค์ œ ์ƒํƒœ์™€ ๊ฐ€์„ค ํŒ์ • ํŒ์ • ์‹ค์ œ 0 ๊ธฐ๊ฐ(ๆฃ„ๅด) ๋ชปํ•จ 0 ๊ธฐ๊ฐ(ๆฃ„ๅด) 0 ์ฐธ ์˜ณ์€ ๊ฒฐ์ • ์ œ1์ข… ์˜ค๋ฅ˜ ํ™•๋ฅ  0 ๊ฑฐ์ง“ ์ œ2์ข… ์˜ค๋ฅ˜ ํ™•๋ฅ  ์˜ณ์€ ๊ฒฐ์ • ํŒ์ •๊ณผ ์˜ค๋ฅ˜ ์ œ1์ข… ์˜ค๋ฅ˜ ํ™•๋ฅ (type I error probability, ) : ์—†๋Š”๋ฐ ์žˆ๋‹ค๊ณ  ํŒ์ •ํ•˜๋Š” ์˜ค๋ฅ˜ ํ™•๋ฅ . ์‹ค์ œ ์ƒํƒœ๋Š” ๊ท€๋ฌด๊ฐ€์„ค( 0 )์ด ์˜ณ์ง€๋งŒ ์ž˜๋ชป ํŒ์ •ํ•˜์—ฌ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์„ ๊ธฐ๊ฐํ•˜๋Š” ์˜ค๋ฅ˜ ํ™•๋ฅ  ์ œ2์ข… ์˜ค๋ฅ˜ ํ™•๋ฅ (type II error probability, ) : ์žˆ๋Š”๋ฐ ์—†๋‹ค๊ณ  ํŒ์ •ํ•˜๋Š” ์˜ค๋ฅ˜ ํ™•๋ฅ . ์‹ค์ œ ์ƒํƒœ๋Š” ๊ท€๋ฌด๊ฐ€์„ค( 0 )์ด ์˜ณ์ง€ ์•Š์ง€๋งŒ ์ž˜๋ชป ํŒ์ •ํ•˜์—ฌ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์„ ๊ธฐ๊ฐํ•˜์ง€ ์•Š๋Š” ์˜ค๋ฅ˜ ํ™•๋ฅ  ๊ฒ€์ •๋ ฅ(power, โˆ’ ): ์žˆ๋Š”๋ฐ ์žˆ๋‹ค๊ณ  ํŒ์ •ํ•˜๋Š” ํ™•๋ฅ . ์‹ค์ œ ์ƒํƒœ๋Š” ๊ท€๋ฌด๊ฐ€์„ค( 0 )์ด ์˜ณ์ง€ ์•Š์•„ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์„ ๊ธฐ๊ฐํ•˜๋Š” ํ™•๋ฅ  ์‹ค์ œ์ƒํƒœ์™€ ๊ฐ€์„ค ํŒ์ • ์ ์šฉ ์‚ฌ๋ก€ ํŒ์ • ์‹ค์ œ = 200mg/dL < 200mg/dL = 200mg/dL ์˜ณ์€ ๊ฒฐ์ • ์ œ1์ข… ์˜ค๋ฅ˜ ํ™•๋ฅ  < 200mg/dL ์ œ2์ข… ์˜ค๋ฅ˜ ํ™•๋ฅ  ์˜ณ์€ ๊ฒฐ์ • ๊ฐ€์„ค ํŒ์ •๊ณผ ์˜ค๋ฅ˜ ์ ์šฉ ์‚ฌ๋ก€ ์ œ1์ข… ์˜ค๋ฅ˜ ํ™•๋ฅ (type I error probability, ) : ์„ฑ์ธ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜์˜ ํ‰๊ท  ๋Š” 200์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ž˜๋ชป ์˜ˆ์ธกํ•˜์—ฌ 200๋ณด๋‹ค ์ž‘๋‹ค๊ณ  ํŒ์ •ํ•˜๋Š” ์˜ค๋ฅ˜ ์ œ2์ข… ์˜ค๋ฅ˜ ํ™•๋ฅ (type II error probability, ) : ์„ฑ์ธ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜์˜ ํ‰๊ท  ๋Š” 200๋ณด๋‹ค ์ž‘์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ž˜๋ชปํ•˜์—ฌ 200์œผ๋กœ ํŒ์ •ํ•˜๋Š” ์˜ค๋ฅ˜ ๊ฒ€์ •๋ ฅ(power, โˆ’ ): ์‹ค์ œ ์„ฑ์ธ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜๊ฐ€ 200๋ณด๋‹ค ์ž‘์„ ๋•Œ, ์„ฑ์ธ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜๊ฐ€ 200๋ณด๋‹ค ์ž‘๋‹ค๊ณ  ํŒ์ •ํ•˜๋Š” ํ™•๋ฅ  ๊ท€๋ฌด๊ฐ€์„ค, ๋Œ€๋ฆฝ๊ฐ€์„ค, , , ๊ธฐ๊ฐ์—ญ, ๊ฒ€์ •๋ ฅ , ๊ด€๊ณ„๋ฅผ ํฌ๊ฒŒ ํ•˜๋ฉด ๊ฐ€ ์ž‘์•„์ง ๋ฅผ ์ž‘๊ฒŒํ•˜๋ฉด ๊ฐ€ ํฌ๊ฒŒ ๋จ, , power, effect size ๊ด€๊ณ„ ํšจ๊ณผ ํฌ๊ธฐ(effect size) : ๋‘ ๊ฐ€์„ค ํ‰๊ท  ์ฐจ์ด ฮผ โˆ’ 1๋กœ ์ด ๊ฐ’์ด ํฌ๋ฉด ๊ฒ€์ •๋ ฅ(power, โˆ’ )๊ฐ€ ์ปค์ง„๋‹ค. , , power, effect size ๊ด€๊ณ„ ๋‘ ๊ฐ€์„ค ํ‰๊ท  ์ฐจ์ด ฮผ โˆ’ 1 ๊ฐ€ ์ž‘์•„์ ธ ๊ฒ€์ •๋ ฅ(power, โˆ’ )์ด ์ž‘์•„์ง€๊ณ  ์ œ2์ข… ์˜ค๋ฅ˜( )๊ฐ€ ์ปค์ง„๋‹ค. , , power, effect size ๊ด€๊ณ„ ๋‘ ๊ฐ€์„ค ํ‰๊ท  ์ฐจ์ด ฮผ โˆ’ 1 ๊ฐ€ ์ปค์ ธ์„œ ๊ฒ€์ •๋ ฅ(power, โˆ’ )์ด ์ปค์ง€๊ณ  ์ œ2์ข… ์˜ค๋ฅ˜( )๊ฐ€ ์ž‘์•„์ง„๋‹ค. ๊ฒ€์ •์— ์‚ฌ์šฉํ•˜๋Š” ์šฉ์–ด ์œ ์˜์ˆ˜์ค€(significance level, ) : ์—ฐ๊ตฌ์ž๊ฐ€ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์ธ์ง€, ๋“œ๋ฏˆ๊ฒŒ ์ผ์–ด๋‚˜๋Š” ๊ฒฝ์šฐ์ธ์ง€ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•œ ๊ทธ ๊ธฐ์ค€์„ ์„ค์ •ํ•œ๋‹ค. ๋ฌต์‹œ์ ์œผ๋กœ 5%๊ฐ€ ํ‘œ์ค€์ด๋‹ค. ๊ธฐ๊ฐ์—ญ(critical region) : ์œ ์˜์ˆ˜์ค€์ด ์„ค์ •๋˜๋ฉด ๋“œ๋ฏˆ๊ฒŒ ์ผ์–ด๋‚˜๋Š” ๊ฒฝ์šฐ์˜ ์˜์—ญ์ด๋‹ค. ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰(test statistic) : ๊ด€์ธก ๊ฐ’์—์„œ ๊ฒ€์ •์— ์‚ฌ์šฉํ•  ํ†ต๊ณ„๋Ÿ‰์œผ๋กœ ์˜ˆ๋กœ ํ‘œ๋ณธํ‰๊ท  โ€• ๊ฐ€ ์žˆ๋‹ค. ์œ ์˜ ํ™•๋ฅ (p-๊ฐ’, p-value) : ํ‘œ๋ณธํ‰๊ท ์ด โ€• ๊ฐ€ ๊ท€๋ฌด๊ฐ€์„ค 0 ์—์„œ ๋ฐœ์ƒํ•  ๋ˆ„์  ํ™•๋ฅ ์ด๋‹ค. ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ํ†ต๊ณ„์  ๊ฐ€์„ค๊ฒ€์ • ๋‹จ๊ณ„ ๊ท€๋ฌด๊ฐ€์„ค( 0 )๊ณผ ๋Œ€๋ฆฝ๊ฐ€์„ค( 1 )์„ ์„ค์ •ํ•œ๋‹ค. ๋‘ ๊ฐ€์„ค์€ ์„œ๋กœ ์ƒ๋ฐ˜๋œ ๊ฐ€์„ค๋กœ ๋Œ€๋ถ€๋ถ„ ์—ฐ๊ตฌ์ž ๋˜๋Š” ์กฐ์‚ฌ์ž๋Š” ์ฃผ์žฅํ•˜๋Š” ๋Œ€๋ฆฝ๊ฐ€์„ค( 1 )์ด ์˜๋ฏธ ์žˆ๊ฒŒ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์„ ๊ธฐ๊ฐํ•˜๊ธฐ ์›ํ•œ๋‹ค. ์œ ์˜์ˆ˜์ค€์— ๋Œ€ํ•œ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์„ ๊ธฐ๊ฐํ•˜๋Š” ์˜์—ญ์ธ ๊ธฐ๊ฐ์—ญ์„ ์„ค์ •ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ถ”์ถœํ•œ ํ‘œ๋ณธ์—์„œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰๊ณผ ์œ ์˜ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•œ๋‹ค. ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ๊ธฐ๊ฐ์—ญ์— ์†ํ•˜๋ฉด(์œ ์˜ ํ™•๋ฅ ์ด ์œ ์˜์ˆ˜์ค€๋ณด๋‹ค ์ž‘์œผ๋ฉด) ๊ท€๋ฌด๊ฐ€์„ค( 0 )์„ ๊ธฐ๊ฐํ•œ๋‹ค. ๊ฐ€์„ค ์„ค์ • ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ๊ฐ€์„ค๊ฒ€์ •์—์„œ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์€ 0 ฮผ ฮผ ์ผ ๋•Œ ๋Œ€๋ฆฝ๊ฐ€์„ค( 1 )์€ ๊ท€๋ฌด๊ฐ€์„ค๊ณผ ๊ณตํ†ต๋ถ€๋ถ„์ด ์—†๊ฒŒ 1 ฮผ ฮผ โ†’ one sided test 1 ฮผ ฮผ โ†’ one sided test 1 ฮผ ฮผ ์ฆ‰ 1 ฮผ ฮผ ๋˜๋Š” 1 ฮผ โˆ’ 0 two sided test ์œ ์˜์ˆ˜์ค€(signicance level, )์œผ๋กœ ๊ธฐ๊ฐ์—ญ(reject area) ๊ณ„์‚ฐ ์—ฐ๊ตฌ์ž๋Š” ์Šค์Šค๋กœ ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•˜๋Š” ์œ ์˜์ˆ˜์ค€์„ ์„ค์ • ์—ฐ๊ตฌ์ž๊ฐ€ ๊ฒฐ์ •ํ•œ ์œ ์˜์ˆ˜์ค€์— ๋Œ€ํ•œ ๊ธฐ๊ฐ์—ญ์„ ๊ณ„์‚ฐ ๊ธฐ๊ฐ์—ญ์€ ๋Œ€๋ฆฝ๊ฐ€์„ค ์ข…๋ฅ˜์— ๋”ฐ๋ผ ์„ธ ๊ฐ€์ง€๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ 1 ฮผ ฮผ ์ผ ๋•Œ ๊ธฐ๊ฐ์—ญ์€ : โ€• c ์ด๊ฑฐ๋‚˜ : โ€• ฮผ / = > ฮฑ 2 1 ฮผ ฮผ ์ผ ๋•Œ ๊ธฐ๊ฐ์—ญ์€ : โ€• c ์ด๊ฑฐ๋‚˜ : โ€• ฮผ / = < z / H : โ‰  0 ์ผ ๋•Œ ๊ธฐ๊ฐ์—ญ์€ : X | c }}๋กœ : โ€• c or โ€• โˆ’ ์ด๊ฑฐ๋‚˜ : X โˆ’ ฯƒ n = Z > ฮฑ 2 R Z z / or < z / ๊ธฐ๊ฐ์—ญ ๋ถ€๋“ฑํ˜ธ ๋ฐฉํ–ฅ์€ ๋Œ€๋ฆฝ๊ฐ€์„ค ๋ถ€๋“ฑํ˜ธ ๋ฐฉํ–ฅ๊ณผ ๋™์ผํ•˜๋‹ค. ์‹ค์ œ๋กœ ๋ถ„์„์—์„œ ๊ธฐ๊ฐ์—ญ์€ ๊ด€์‹ฌ์˜ ๋Œ€์ƒ์ด ์•„๋‹ˆ๊ณ , ๊ธฐ๊ฐ์—ญ์— ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ โ€• ์˜ ํฌํ•จ ์œ ๋ฌด์ด๋‹ค. ๊ธฐ๊ฐ์—ญ์— ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ โ€• ๊ฐ€ ํฌํ•จ๋˜๋ฉด ๊ท€๋ฌด๊ฐ€์„ค( 0 )์„ ๊ธฐ๊ฐํ•œ๋‹ค. ๊ฐ™์€ ์˜๋ฏธ๋กœ ์œ ์˜์ˆ˜์ค€์ด ์œ ์˜ ํ™•๋ฅ ๋ณด๋‹ค ํฌ๋ฉด ๊ท€๋ฌด๊ฐ€์„ค( 0 )์„ ๊ธฐ๊ฐํ•œ๋‹ค. ์œ ์˜์ˆ˜์ค€์€ ๋ฌต์‹œ์ ์œผ๋กœ 0.05์ด๋ฉฐ, ์œ ์˜ ํ™•๋ฅ ์ด 0.05๋ณด๋‹ค ์ž‘์œผ๋ฉด ์—ฐ๊ตฌ์ž๋“ค์€ "๋ถ„์„ ๊ฒฐ๊ณผ ์œ ์˜ ํ™•๋ฅ (p--value)์ด 0.05๋ณด๋‹ค ์ž‘์œผ๋ฉด ์˜๋ฏธ ์žˆ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ ๊ฒƒ์ด๋‹ค."๋ผ๊ณ  ํ‘œํ˜„ํ•œ๋‹ค. ๋‹จ ์ธก ๊ฒ€์ •( 1 ฮผ ฮผ)์—์„œ ์œ ์˜์ˆ˜์ค€์— ๋Œ€ํ•œ ๊ธฐ๊ฐ์—ญ ๊ธฐ๊ฐ์—ญ์€ ๋Œ€๋ฆฝ๊ฐ€์„ค ๋ฐฉํ–ฅ๊ณผ ๋™์ผํ•˜๋ฉฐ ์˜์—ญ์ด ํ•œ ์ชฝ๋งŒ ์žˆ๊ธฐ์— ๋‹จ ์ธก ๊ฒ€์ •์ด๋‹ค. ๋‹จ ์ธก ๊ฒ€์ •( 1 ฮผ ฮผ)์—์„œ ์œ ์˜์ˆ˜์ค€์— ๋Œ€ํ•œ ๊ธฐ๊ฐ์—ญ ๊ธฐ๊ฐ์—ญ์€ ๋Œ€๋ฆฝ๊ฐ€์„ค ๋ฐฉํ–ฅ๊ณผ ๋™์ผํ•˜๋ฉฐ ์˜์—ญ์ด ํ•œ ์ชฝ๋งŒ ์žˆ๊ธฐ์— ๋‹จ ์ธก ๊ฒ€์ •์ด๋‹ค. ์–‘์ธก๊ฒ€์ •( 1 ฮผ ฮผ)์—์„œ ์œ ์˜์ˆ˜์ค€์— ๋Œ€ํ•œ ๊ธฐ๊ฐ์—ญ ๊ธฐ๊ฐ์—ญ์€ ๋Œ€๋ฆฝ๊ฐ€์„ค ๋ฐฉํ–ฅ๊ณผ ๋™์ผํ•˜๋ฉฐ ์˜์—ญ์ด ์–‘์ชฝ์— ์žˆ์œผ๋ฏ€๋กœ ์–‘์ธก๊ฒ€์ •์ด๋‹ค. ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์œผ๋กœ ์œ ์˜ ํ™•๋ฅ  ๊ณ„์‚ฐ ์œ ์˜ ํ™•๋ฅ ์€ ์„ธ ๊ฐœ์˜ ๋Œ€๋ฆฝ๊ฐ€์„ค์— ๋Œ€ํ•˜์—ฌ ๊ธฐ๊ฐ์—ญ์˜ ๋ถ€๋“ฑํ˜ธ ๋ฐฉํ–ฅ๊ณผ ๋™์ผํ•˜๊ฒŒ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์˜ ๊ด€์ธก ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐ ์–‘์ธก๊ฒ€์ •์ธ ๊ฒฝ์šฐ, ์œ ์˜ ํ™•๋ฅ ์€ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์˜ ๊ด€์ธก ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ์— 2๋ฅผ ๊ณฑํ•ฉ ์œ ์˜ ํ™•๋ฅ ์€ ์†์œผ๋กœ ๊ณ„์‚ฐํ•˜๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š์œผ๋ฏ€๋กœ ์ปดํ“จํ„ฐ๋ฅผ ์ด์šฉ ๋Œ€๋ถ€๋ถ„ ํ†ต๊ณ„ ํ”„๋กœ๊ทธ๋žจ์ด ์–‘์ธก๊ฒ€์ • ๊ฒฐ๊ณผ๋ฅผ ์ œ์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋‹จ ์ธก ๊ฒ€์ •์ธ ๊ฒฝ์šฐ์—๋Š” 2๋กœ ๋‚˜๋ˆˆ ๊ฐ’์ด ์œ ์˜ ํ™•๋ฅ  ๊ฐ’์ž„ 1 ฮผ ฮผ ์ผ ๋•Œ [ โ€• x ] ์ด๊ฑฐ๋‚˜ [ โ€• ฮผ / = > 0 ] 1 ฮผ ฮผ ์ผ ๋•Œ [ โ€• x ] ์ด๊ฑฐ๋‚˜ [ โ€• ฮผ / = < z ] 1 ฮผ ฮผ }} ์ผ ๋•Œ [ โ€• x ] 2 or [ โ€• x ] 2 ์ด๊ฑฐ๋‚˜ [ โ€• ฮผ / = > 0 ] 2 or [ โ€• ฮผ / = < z ] 2 ์œ ์˜์ˆ˜์ค€๊ณผ ๊ธฐ๊ฐ์—ญ ์—ฐ๊ตฌ์ž๊ฐ€ ์œ ์˜์ˆ˜์ค€( )์„ ์„ค์ •ํ•˜๋ฉด ๊ธฐ๊ฐ์—ญ์„ ๊ณ„์‚ฐํ•œ๋‹ค. ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰๊ณผ ์œ ์˜ ํ™•๋ฅ  ์—ฐ๊ตฌ์ž๋Š” ํ‘œ๋ณธ์—์„œ ๊ตฌํ•œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์œผ๋กœ ์œ ์˜ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ๊ธฐ๊ฐ์—ญ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ์œ ์˜์ˆ˜์ค€ ๋Š” ์œ ์˜ ํ™•๋ฅ  2 ๋ณด๋‹ค ์ž‘๋‹ค. ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰๊ณผ ์œ ์˜ ํ™•๋ฅ  ์—ฐ๊ตฌ์ž๋Š” ํ‘œ๋ณธ์—์„œ ๊ตฌํ•œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์œผ๋กœ ์œ ์˜ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ๊ธฐ๊ฐ์—ญ์— ํฌํ•จ๋œ๋‹ค. ์œ ์˜์ˆ˜์ค€ ๋Š” ์œ ์˜ ํ™•๋ฅ  1 ๋ณด๋‹ค ํฌ๋‹ค. ์œ ์˜์ˆ˜์ค€๊ณผ ๊ธฐ๊ฐ์—ญ, ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰๊ณผ ์œ ์˜ ํ™•๋ฅ  ์—ฐ๊ตฌ์ž๋Š” ์œ ์˜์ˆ˜์ค€์„ ์„ค์ •ํ•˜๊ณ  ๊ธฐ๊ฐ์—ญ์„ ๊ตฌํ•œ๋‹ค. ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ p ์€ ๊ธฐ๊ฐ์—ญ์— ํฌํ•จ๋˜๊ณ  p๋Š” ๊ธฐ๊ฐ์—ญ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ์œ ์˜ ํ™•๋ฅ ์ด ์œ ์˜์ˆ˜์ค€ ๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ๊ธฐ๊ฐ์—ญ์— ํฌํ•จ๋˜์–ด ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•œ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ๊ธฐ๊ฐ ์œ ์˜์ˆ˜์ค€( ) ์œ ์˜ ํ™•๋ฅ ( -๊ฐ’)์ด๋ฉด ๊ท€๋ฌด๊ฐ€์„ค 0 ๊ธฐ๊ฐ ํ†ต๊ณ„์  ๊ฐ€์„ค๊ฒ€์ • ์‚ฌ๋ก€ ๊ฐ€์„ค ์„ค์ • 0 : ์„ฑ์ธ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜์˜ ํ‰๊ท  ๋Š” 200mg/dL์ด๋‹ค. 1 : ์„ฑ์ธ ์ฝœ๋ ˆ์Šคํ…Œ๋กค ์ˆ˜์น˜์˜ ํ‰๊ท  ๋Š” 200mg/dL๋ณด๋‹ค ์ž‘๋‹ค. ์œ ์˜ ํ™•๋ฅ , ์œ ์˜์ˆ˜์ค€, ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰, ๊ธฐ๊ฐ์—ญ ์œ ์˜์ˆ˜์ค€ = 0.05 (์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •) ๊ธฐ๊ฐ์—ญ : โ‰ค 1.645 or โ€• 193.49 ( = 0.05 ฯƒ 28 n 50 ) 0.05 P [ โ‰ค 1.645 ] P ( โ€• 200 28 50 โˆ’ 1.645 ) P [ โ€• 200 1.645 28 50 ] P [ โ€• 193.49 ] ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ : โ‘  โ€• 195 P [ โ€• 195 ] P ( โ€• 200 28 50 195 200 28 50 ) P [ โ‰ค 1.2627 ] 0.1034 X = 192 (ํ‘œ๋ณธ์—์„œ ๊ณ„์‚ฐ) P [ โ€• 192 ] P ( โ€• 200 28 50 192 200 28 50 ) P [ โ‰ค 2.0203 ] 0.0217 ์œ ์˜ ํ™•๋ฅ  : โ‘  [ โ€• 195 ] 0.1034 , โ‘ก [ โ€• 192 ] 0.0217 ๊ฒฐ๋ก  โ‘  ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ๊ธฐ๊ฐ์—ญ์— ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์„ ๊ธฐ๊ฐ ๋ชปํ•จ โ‘ก ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ๊ธฐ๊ฐ์—ญ์— ํฌํ•จ๋˜๋ฏ€๋กœ ๊ท€๋ฌด๊ฐ€์„ค( 0 )์„ ๊ธฐ๊ฐ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ๊ฒ€์ •์„ ๊ทธ๋ฆผ์œผ๋กœ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ๊ฒ€์ •์„ ๊ทธ๋ฆผ์œผ๋กœ ๊ท€๋ฌด๊ฐ€์„ค ๊ธฐ๊ฐ/์ฑ„ํƒ ํŒ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฒฐ์ •ํ•œ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค 0 ์— ๋Œ€ํ•˜์—ฌ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ์œ ์˜์ˆ˜์ค€ ๋ฅผ ๊ฐ–๋Š” ๊ธฐ๊ฐ์—ญ์— ํฌํ•จ๋˜๊ฑฐ๋‚˜ ์œ ์˜ ํ™•๋ฅ  p-value๊ฐ€ ์œ ์˜์ˆ˜์ค€ ๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•œ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค ๊ธฐ๊ฐ/์ฑ„ํƒ ํŒ์ • ์ •๋ณด ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ๊ธฐ๊ฐ์—ญ์— ํฌํ•จ ์œ ๋ฌด๋กœ โ‘  ๊ท€๋ฌด๊ฐ€์„ค 0 ๊ธฐ๊ฐ/์ฑ„ํƒ์„ ํŒ์ • ์œ ์˜ ํ™•๋ฅ  p-value์™€ ์œ ์˜์ˆ˜์ค€ ๊ด€๊ณ„์€ โ‘  0 ๊ธฐ๊ฐ/์ฑ„ํƒ ํŒ์ •๊ณผ โ‘ก p-value ํ™•๋ฅ ์ด ๊ธฐ๊ฐ/์ฑ„ํƒ ๊ฐ•๋„ ์ œ๊ณต ์‹ ๋ขฐ๊ตฌ๊ฐ„๊ณผ ์–‘์ธก๊ฒ€์ • ๊ด€๊ณ„ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ๊ฒ€์ • ์š”์•ฝ ํ‘œ๋ณธ์˜ ํฌ๊ธฐ๊ฐ€ ํฐ ๊ฒฝ์šฐ ๋ชจํ‰๊ท ์— ๋Œ€ํ•œ ๊ท€๋ฌด๊ฐ€์„ค( 0 ฮผ ฮผ)์„ ๊ฒ€์ •ํ•˜๊ธฐ ์œ„ํ•œ ํ†ต๊ณ„๋Ÿ‰์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. โ€• ฮผ s n ํ‘œ๋ณธ์ด ์ž‘์•„๋„ ๋ชจ์ง‘๋‹จ์ด ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋ฉด ์œ„์˜ ํ†ต๊ณ„๋Ÿ‰์„ ์‚ฌ์šฉํ•œ๋‹ค. ํ‘œ๋ณธ ํฌ๊ธฐ๊ฐ€ ํฐ ๊ฒฝ์šฐ, ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ ๋ถ„ํฌ๋Š” 0 ๊ฐ€ ๋งž์„ ๋•Œ ( , ) ์„ ๋”ฐ๋ฅธ๋‹ค. ์ผ ๋•Œ ๋‹จ ์ธก ๊ฒ€์ • ์ผ ๋•Œ ๋‹จ ์ธก ๊ฒ€์ • ์ผ ๋•Œ ์–‘์ธก๊ฒ€์ • 1 ฮผ ฮผ ์ผ ๋•Œ : โ‰ค z or > ๋‹จ ์ธก ๊ฒ€์ • 1 ฮผ ฮผ ์ผ ๋•Œ : โ‰ฅ ฮฑ or > ๋‹จ ์ธก ๊ฒ€์ • 1 ฮผ ฮผ ์ผ ๋•Œ : Z โ‰ฅ ฮฑ 2 or > ์–‘์ธก๊ฒ€์ • ํ‘œ๋ณธ ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฒฝ์šฐ, ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ ๋ถ„ํฌ๋Š” 0 ๊ฐ€ ๋งž์„ ๋•Œ ( . = โˆ’ ) ์„ ๋”ฐ๋ฅธ๋‹ค. ์ผ ๋•Œ ๋‹จ ์ธก ๊ฒ€์ • ์ผ ๋•Œ ๋‹จ ์ธก ๊ฒ€์ • ์ผ ๋•Œ ์–‘์ธก๊ฒ€์ • 1 ฮผ ฮผ ์ผ ๋•Œ : โ‰ค t or > ๋‹จ ์ธก ๊ฒ€์ • 1 ฮผ ฮผ ์ผ ๋•Œ : โ‰ฅ ฮฑ or > ๋‹จ ์ธก ๊ฒ€์ • 1 ฮผ ฮผ ์ผ ๋•Œ : t โ‰ฅ ฮฑ 2 or > ์–‘์ธก๊ฒ€์ • ๊ท€๋ฌด๊ฐ€์„ค 0 ์— ๋Œ€ํ•˜์—ฌ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ์œ ์˜์ˆ˜์ค€ ๋ฅผ ๊ฐ–๋Š” ๊ธฐ๊ฐ์—ญ์— ํฌํ•จ๋˜๊ฑฐ๋‚˜ ์œ ์˜ ํ™•๋ฅ  p-value๊ฐ€ ์œ ์˜์ˆ˜์ค€ ๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•œ๋‹ค. ์ตœ์ข… ํ•ต์‹ฌ : p-value ฮฑ ์ด๋ฉด ๊ท€๋ฌด๊ฐ€์„ค 0 ๊ธฐ๊ฐ 16. ํ™•๋ฅ ๋ถ„ํฌ ํ‘œ ํ™•๋ฅ ๋ถ„ํฌ ํ‘œ ์ดํ•ญ๋ถ„ํฌ ํ‘œ ํฌ์•„์†ก๋ถ„ํฌํ‘œ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ํ‘œ t - ๋ถ„ํฌ ํ‘œ 2 ๋ถ„ํฌ ํ‘œ F - ๋ถ„ํฌ ํ‘œ( = 0.05 ) F - ๋ถ„ํฌ ํ‘œ( = 0.1 )<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ํ•œ๋•€ํ•œ๋•€ ๋”ฅ๋Ÿฌ๋‹ ์ปดํ“จํ„ฐ ๋น„์ „ ๋ฐฑ๊ณผ์‚ฌ์ „ ### ๋ณธ๋ฌธ: ๊ฐ€์งœ ์—ฐ๊ตฌ์†Œ ์Šคํ„ฐ๋”” ํ™œ๋™์œผ๋กœ ๋ชจ์ธ ์Šคํ„ฐ๋”” ๋Ÿฌ๋„ˆ๋ถ„๋“ค์ด ๋”ฅ๋Ÿฌ๋‹ ์ปดํ“จํ„ฐ ๋น„์ „์— ์“ฐ์ด๋Š” ๊ธฐ์ดˆ์ ์ธ ๊ฐœ๋… ์™€ ๋ฐฐ๊ฒฝ์ง€์‹์„ ์ •๋ฆฌํ•œ ๋ฐฑ๊ณผ์‚ฌ์ „์ž…๋‹ˆ๋‹ค. ๊ฒฝํ—˜์ด ๋งŽ์ง€ ์•Š์€ ๋ถ„๋“ค์ด ๊ฐ™์ด ์Šคํ„ฐ๋””๋ฅผ ํ•˜๋ฉด์„œ ํ•œ ๋•€ ํ•œ ๋•€ ๋‚ด์šฉ์„ ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค. (์ดˆ๋ณด๊ฐ€ ์ •๋ฆฌํ–ˆ๊ธฐ์— ์ดˆ๋ณด์˜ ์‹œ์„ ์œผ๋กœ ์“ฐ์˜€์Šต๋‹ˆ๋‹ค) ์ •๋ฆฌํ•œ ๋‚ด์šฉ๋“ค์€ ๋ชจ๋‘ ์›น๊ณผ ์ธํ„ฐ๋„ท์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ํ•„์ˆ˜์ ์ด๋ผ๊ณ  ์ƒ๊ฐ๋˜๋Š” ๊ฐœ๋…๋“ค์„ ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž…๋ฌธ์ž๋ถ„๋“ค์˜ ์„ฑ์žฅ์— ํฐ ๋„์›€์ด ๋˜์—ˆ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ์ž‘์„ฑ์ž <NAME>(<EMAIL>) <NAME>(<EMAIL>) <NAME>(<EMAIL>) <NAME>(<EMAIL>) <NAME>(<EMAIL>) ๋Œ€ํ‘œ ๋ฉ”์ผ <EMAIL> ํ˜น์‹œ ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์žˆ์œผ๋ฉด ๋Œ€ํ‘œ ๋ฉ”์ผ๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฒ€ํ†  ํ›„์— ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค 1. ๋”ฅ๋Ÿฌ๋‹ ๊ธฐ์ดˆ ๋”ฅ๋Ÿฌ๋‹ ๊ธฐ์ดˆ์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ๋ถ€ํ„ฐ ๊ธฐ์ดˆ์ ์ธ ๋”ฅ๋Ÿฌ๋‹ ๊ฐœ๋…๋“ค์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค. (1) ๋”ฅ๋Ÿฌ๋‹๊ณผ ์‹ ๊ฒฝ๋ง 1) ํผ์…‰ํŠธ๋ก ๊ณผ ์‹ ๊ฒฝ๋ง ํผ์…‰ํŠธ๋ก ์€ ์‹ ๊ฒฝ๋ง์˜ ๊ธฐ๋ณธ ๊ตฌ์„ฑ์š”์†Œ์ž…๋‹ˆ๋‹ค. ์‹ ๊ฒฝ๋ง(Neural Net)์˜ ๊ธฐ๋ณธ ์ŠคํŠธ๋Ÿญ์ฒ˜๋ฅผ ๋ณด๋ฉด ๋ณดํ†ต ์•„๋ž˜์™€ ๊ฐ™์ด ๊ทธ๋ฆฝ๋‹ˆ๋‹ค. ์ด์ค‘ ๋นจ๊ฐ„์ƒ‰ ๋ฐ•์Šค๋ฅผ ์นœ ๋ถ€๋ถ„, ์‹ ๊ฒฝ๋ง์˜ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ํผ์…‰ํŠธ๋ก ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์˜ ๊ตฌ์„ฑ ์š”์†Œ ํ•˜๋‚˜์˜ ํผ์…‰ํŠธ๋ก ์„ ํ™•๋Œ€ํ•˜์—ฌ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ํผ์…‰ํŠธ๋ก ์€ ์ž…๋ ฅ(inputs), ๊ฐ€์ค‘์น˜(Connection Weight), ๋ฐ”์ด์–ด์Šค(bias), ํ•ฉ ์—ฐ์‚ฐ(Sum), ํ™œ์„ฑ ํ•จ์ˆ˜(Activation Function)๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ๋‹ค์–‘ํ•œ ๊ฒƒ์ด ๋งŽ์œผ๋‚˜ ํผ์…‰ํŠธ๋ก  ์ณ…ํ„ฐ์—์„œ๋Š” ํ™œ์„ฑ ํ•จ์ˆ˜(Activation Function)์„ ๋‹จ์ˆœํ™”ํ•˜์—ฌ ์ž…๋ ฅ์ด 0๋ณด๋‹ค ์ž‘์œผ๋ฉด 0์„ ์ถœ๋ ฅ์œผ๋กœ ์ž…๋ ฅ์ด 0๋ณด๋‹ค ํฌ๋ฉด 1์„ ์ถœ๋ ฅํ•˜๋Š” ๊ณ„๋‹จํ•จ์ˆ˜(Step function)๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ํ•˜๋‚˜์˜ ํผ์…‰ํŠธ๋ก ์—์„œ ์ด๋ฃจ์–ด์ง€๋Š” ์—ฐ์‚ฐ์€ "์ž…๋ ฅ์˜ ๊ฐ€์ค‘ํ•ฉ" โ†’ "ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ ์šฉ" ์ด ๋‘ ๋‹จ๊ณ„๊ฐ€ ์ „๋ถ€์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ํผ์…‰ํŠธ๋ก ์œผ๋กœ ๊ฐ€๋Šฅํ•œ ์—ฐ์‚ฐ๊ณผ ์˜๋ฏธ ํ•˜๋‚˜์˜ ํผ์…‰ํŠธ๋ก ์œผ๋กœ๋Š” Linearly Separable Problem๋งŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์„ ์ž˜ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ€์ค‘์น˜๊ฐ€ ๊ณ ์ •๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์งœ์ธ ํ•œ ๊ฐœ์˜ ํผ์…‰ํŠธ๋ก ์€ ์šฐ์ธก์˜ ํ‘œ์— ๋‚˜์˜จ AND ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ€์ค‘์น˜๊ฐ€ ๊ณ ์ •๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด OR ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์กฐ๊ธˆ๋งŒ ๋” ์ƒ๊ฐํ•ด ๋ณด๋ฉด ํ•œ ๊ฐœ์˜ ํผ์…‰ํŠธ๋ก ์€ NOT ์—ฐ์‚ฐ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ํ•˜๋‚˜์˜ ํผ์…‰ํŠธ๋ก ์€ ์ž…๋ ฅ(X)์˜ ๊ณต๊ฐ„์—์„œ ๊ทธ๋ ค๋ณด๋ฉด ํ•˜๋‚˜์˜ ์ง์„ (ํ•˜์ดํผ ํ”Œ๋ ˆ์ธ)์„ ์˜๋ฏธํ•˜๊ณ  ์ด๊ฒƒ์€ ํ•˜๋‚˜์˜ ํผ์…‰ํŠธ๋ก ์€ AND, OR, NOT ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ Linearly Separable ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. AND, OR, NOT 3๊ฐ€์ง€ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์€ ํ˜„์žฌ ์ปดํ“จํ„ฐ์—์„œ ์กด์žฌํ•˜๋Š” 3๊ฐ€์ง€ ๊ธฐ๋ณธ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๊ณ  ์ด๊ฒƒ์€ ํผ์…‰ํŠธ๋ก  ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์—ฐ๊ฒฐํ•˜๋ฉด ์ด๋ก ์ ์œผ๋กœ๋Š” ๊ธฐ์กด ์ปดํ“จํ„ฐ์—์„œ ์†Œํ™”ํ•˜๋Š” ๋ชจ๋“  ์—ฐ์‚ฐ(ํ•จ์ˆ˜)๋ฅผ ์žฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํผ์…‰ํŠธ๋ก ์„ ์ธต(Layer) ๋‹จ์œ„๋กœ ๋ฐฐ์น˜ํ•˜๊ณ  ๋‚˜์—ดํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง„ ์•„ํ‚คํ…์ฒ˜๋“ค์„ ์‹ ๊ฒฝ๋ง(Neural Network) ํ˜น์€ ๋ชจ๋ธ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ(Network), ๋ชจ๋ธ(Model) ๋“ฑ ๋‹ค์–‘ํ•œ ์šฉ์–ด๋กœ ํ’€์ด๋˜์ง€๋งŒ ํ•ด๋‹น ๋‹จ์–ด๋“ค์„ ๋ณด์‹œ๊ฒŒ ๋˜์‹ ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๊ฐœ ํผ์…‰ํŠธ๋ก ์ด ๋’ค์„ž์—ฌ ์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ์ง€์นญํ•˜๋Š” ๋‹จ์–ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. (์ •ํ™•ํ•œ ์‚ฌ์ „ ์ •์˜๋ณด๋‹ค๋Š” ๋น ๋ฅธ ์ดํ•ด๋ฅผ ์œ„ํ•ด ์ง๊ด€์ ์ธ ํ‘œํ˜„๊ณผ ๊ด€๋…์ ์ธ ํ‘œํ˜„์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค) ์‹ ๊ฒฝ๋ง์œผ๋กœ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ๋“ค ํผ์…‰ํŠธ๋ก  ๋ฉ์–ด๋ฆฌ์— ํ™œ์„ฑํ™” ํ•จ์ˆ˜, ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ ๋ณ€๊ฒฝ์„ ํ†ตํ•ด์„œ ๋ฌธ์ œ๋ฅผ ์ž˜ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ์‹ ๊ฒฝ๋ง์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋Š” Classification(๋ถ„๋ฅ˜) ๋ฌธ์ œ์™€ Regression(ํšŒ๊ท€) ๋ฌธ์ œ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Classification(๋ถ„๋ฅ˜) ์‹ค์ƒํ™œ์—์„œ์˜ ๋งŽ์€ ๋ฌธ์ œ๋Š” ๋ถ„๋ฅ˜(Classification) ๋ฌธ์ œ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋กœ ๋“ค๋ฉด ํ˜ˆ์•กํ˜•์˜ ์ด๋ฏธ ์ง€์„ ํ†ตํ•ด์„œ ํ†ตํ•ด์„œ ํ˜ˆ์•กํ˜•์ด ๋ฌด์—‡์ธ์ง€ ๋ถ„๋ฅ˜ํ•˜๊ฑฐ๋‚˜. ์ž๋™์ฐจ์˜ ์ „์žฅ, ์ „ํญ, ์ „๊ณ ์— ๋”ฐ๋ผ์„œ ์†Œํ˜•, ์ค‘ํ˜•, ๋Œ€ํ˜• ๋ถ„๋ฅ˜ํ•˜๊ฑฐ๋‚˜. ์‚ฌ์ง„ ์ด๋ฏธ์ง€์˜ ๊ฐœ, ๊ณ ์–‘์ด ๊ตฌ๋ถ„๊ณผ ๊ฐ™์ด ์†ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์˜ˆ์ƒ(Prediction) ํ•˜๋Š” ๋ฌธ์ œ์— ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Image Classification, Image Segmentation๋„ Classification์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ Classification์—์„œ๋Š” ๋ณดํ†ต ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ํด๋ž˜์Šค ๊ฐœ์ˆ˜์™€ ๋™์ผํ•˜๊ฒŒ Output ๋ ˆ์ด์–ด์˜ ํผ์…‰ํŠธ๋ก ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด [๊ฐœ, ๊ณ ์–‘์ด, ์ฝ”๋ผ๋ฆฌ] ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ ์ˆ˜๋Š” 3๊ฐœ๊ฐ€ ๋˜๊ณ  ํ˜ˆ์•กํ˜• ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ ์ˆ˜๋Š” [A, B, O, AB] 4๊ฐœ๋กœ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. Regression(ํšŒ๊ท€) ๋ถ„๋ฅ˜๋ฅผ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ๋ฌธ์ œ๋Š” ํšŒ๊ท€(Regression) ๋ฌธ์ œ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž๋ฅผ ๋งž์ถ”๋Š” ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ํŽธํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์–ผ๊ตด ์‚ฌ์ง„์œผ๋กœ ์‚ฌ๋žŒ์˜ ๋‚˜์ด๋ฅผ ์˜ˆ์ธกํ•˜๊ฑฐ๋‚˜, ๊ตฌ๋งคํ•œ ๋ฌผ๊ฑด์œผ๋กœ ๊ณ„์‚ฐ ๊ธˆ์•ก์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ๋“ค์„ ํšŒ๊ท€ ๋ฌธ์ œ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํšŒ๊ท€ ๋ฌธ์ œ๋Š” ์•ž์„œ Classification ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ๋ช‡ ๊ฐœ์˜ ๋…ธ๋“œ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค๊ณ  ๋ง์”€์„ ๋“œ๋ฆด ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ๋ ˆ์ด์–ด์— ํ•œ ๊ฐœ์˜ ํผ์…‰ํŠธ๋ก ์œผ๋กœ ๋‘๊ณ , 0๊ณผ 1์„ ์ถœ๋ ฅํ•˜๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ๋„“์€ ์ˆซ์ž ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์ ์šฉํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ง€๋„ ํ•™์Šต์—์„œ ๋ฐ์ดํ„ฐ(X, Y)๊ฐ€ ์ค€๋น„๊ฐ€ ๋˜์—ˆ๊ณ  ํŠน์ •ํ•œ ๋ชจ๋ธ(model)์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ์ •ํ–ˆ๋‹ค๋ฉด ๋‚จ์€ ๊ฒƒ์€ ๊ฐ€์ค‘์น˜(Weight)๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋‚จ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ๋”ฅ๋Ÿฌ๋‹์—์„œ ๋งํ•˜๋Š” ํ•™์Šต(Training)์ด๋ž€ ํผ์…‰ํŠธ๋ก  ์‚ฌ์ด ๋ชจ๋“  ๊ฐ€์ค‘์น˜(Connection Weight)๋ฅผ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ ๋งž๊ฒŒ ์„ธํŒ…ํ•˜๋Š” ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. ์ง€๋„ ํ•™์Šต(Supervised Learning)์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ ์ˆœ์„œ๋กœ ํ•™์Šต์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. [์ˆœ์„œ 1] : ๋ชจ๋“  ์ปค๋„ฅ์…˜์— ์ž„์˜์— ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. [์ˆœ์„œ 2] : ๊ฐ€์ค‘์น˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ผ๋ฒจ(Y)๊ณผ ์˜ˆ์ธก๊ฐ’(Prediction)์˜ ์—๋Ÿฌ(Error)๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. [์ˆœ์„œ 3] : ์—๋Ÿฌ(Error)๋ฅผ ๊ฐ ๊ฐ€์ค‘์น˜๋กœ ๋ฏธ๋ถ„ํ•˜์—ฌ ๊ธฐ์šธ๊ธฐ(Gradient)๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. [์ˆœ์„œ 4] : ๊ธฐ์šธ๊ธฐ(Gradient)์— ์ž‘์€ ์ƒ์ˆ˜๋ฅผ ๊ณฑํ•˜๊ณ  ์ด ๊ฐ’์„ ๊ธฐ์กด ๊ฐ€์ค‘์น˜์—์„œ ๋นผ์ฃผ์–ด ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. [์ˆœ์„œ 4] โ†’ [์ˆœ์„œ 2]๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค. ์œ„ ๊ณผ์ •์„ ํ•™๋ฌธ์ ์œผ๋กœ ์ •ํ™•ํ•œ ์„ค๋ช…๋ณด๋‹ค๋Š” ์ง๊ด€์ ์œผ๋กœ ์ดํ•ด๊ฐ€ ๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ๊ฐ„๋‹จํ•œ ๊ทธ๋ฆผ๊ณผ ํ‘œํ˜„์œผ๋กœ ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.(์‚ฌ์‹ค ์ˆ˜์‹์œผ๋กœ ์ •ํ™•ํ•˜๊ฒŒ ์ •๋ฆฌํ•  ์ž์‹ ๋„ ์—†์Šต๋‹ˆ๋‹ค ) [์ˆœ์„œ 1] ์•ž์—์„œ ์„ค๋ช…ํ•œ ํผ์…‰ํŠธ๋ก  ์—ฐ์‚ฐ์ด ์ž…๋ ฅ์ธต(Input layer)๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. "์ด์ „ ํผ์…‰ํŠธ๋ก  ์ถœ๋ ฅ์˜ ๊ฐ€์ค‘ํ•ฉ" โ†’ "ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ ์šฉ"์ด ๋„คํŠธ์›Œํฌ๋ฅผ ๋”ฐ๋ผ์„œ ๊ณ„์† ์ด๋ฃจ์–ด์ง€๊ณ , ์œ„์˜ ์ด๋ฏธ์ง€๋ฅผ ์˜ˆ๋กœ ๋“ค๋ฉด ๋งˆ์ง€๋ง‰ ํผ์…‰ํŠธ๋ก ์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ ๊ฒƒ์ด ์˜ˆ์ธก์น˜๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ์ƒํ•˜์‹  ๊ฒƒ๊ณผ ๊ฐ™์ด ๋‹น์—ฐํžˆ [์ˆœ์„œ 1]์—์„œ ๋ฌด์ž‘์œ„๋กœ ๋ฐฐ์ •๋œ ๊ฐ€์ค‘์น˜์ด๊ธฐ์— ์ด ์˜ˆ์ธก๊ฐ’์€ ์ ˆ๋Œ€๋กœ ๋งž์„ ๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. [์ˆœ์„œ 2] ๋‰ด๋Ÿด๋„ท์„ ํ†ตํ•ด ๊ณ„์‚ฐ๋œ ์˜ˆ์ธก์น˜(Prediction)๊ณผ ๋ผ๋ฒจ ๊ฐ’(Y)์˜ ์—๋Ÿฌ(Error)๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์—๋Ÿฌ(Error)์˜ ์ •์˜๊ฐ€ ๊ต‰์žฅํžˆ ๋ชจํ˜ธํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—๋Ÿฌ(Error)๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์„ ํƒํ•œ ํŠน์ • ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ๋”ฅ๋Ÿฌ๋‹์—์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„ ์ค‘์— ํ•˜๋‚˜๋Š” ์ด ์—๋Ÿฌ๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด๊ณ  ์–ด๋–ค ์ •์˜ํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ์ตœ์ข…์ ์œผ๋กœ ์‹ ๊ฒฝ๋ง์— ์—…๋ฐ์ดํŠธ๋˜๋Š” ๊ฐ€์ค‘์น˜์˜ ๊ฐ’์ด ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์™œ๋ƒ๋ฉด ๋Œ€๋ถ€๋ถ„ ๋”ฅ๋Ÿฌ๋‹์˜ ํ•™์Šต ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์—๋Ÿฌ ํ•จ์ˆซ๊ฐ’์„ ์ตœ๋Œ€ํ™” ํ˜น์€ ์ตœ์†Œํ™”ํ•˜๋„๋ก ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ๋Š” Cross Entropy Error๋ฅผ ์ด์šฉํ•˜๊ณ , ํšŒ๊ท€ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ Mean Squared Error๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์†Œ๊ฐœํ•ด ๋“œ๋ฆด ์ปดํ“จํ„ฐ ๋น„์ „์˜ ๊ฐ ๋ถ„์•ผ์—๋Š” ๋‹คํ–‰ํžˆ ์‚ฌ์šฉํ•˜๋Š” ์—๋Ÿฌ ํ•จ์ˆ˜๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•ž์œผ๋กœ ๊ฐ ๋ถ„์•ผ์—์„œ ๋” ์ข‹์€ ์—๋Ÿฌ ํ•จ์ˆ˜๋Š” ๊ฐœ๋ฐœ์ด ๋  ๊ฒƒ์ด๊ณ , ๊ด€์‹ฌ์ด ๊ฐ€๋Š” ๋ถ„์•ผ์— ์ž์ฃผ ์“ฐ์ด๋Š” ์—๋Ÿฌ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฃผ์˜ ๊นŠ๊ฒŒ ๋ด์•ผ ํ•  ํ•„์š”๋Š” ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์ถฉ ์—๋Ÿฌ ํ•จ์ˆ˜๋ผ๊ณ  ์„ค๋ช…๋“œ๋ฆฌ๊ธด ํ–ˆ์ง€๋งŒ ์ผ๋ฐ˜์ ์ธ ์šฉ์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชฉ์ ํ•จ์ˆ˜(Objective Function), ๋กœ์Šค ํŽ‘์…˜(Loss function)์ด ๊ทธ๊ฒƒ์ด๋ฉฐ ํ•ด๋‹น ์šฉ์–ด๊ฐ€ ๋‚˜์˜ค๋ฉด, ๋ผ๋ฒจ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์˜ ์ฐจ์ด๋ฅผ ์˜๋ฏธํ•˜๋Š”๊ตฌ๋‚˜๋ผ๊ณ  ์ดํ•ดํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต ๋กœ์Šค ํŽ‘์…˜์ด๋ผ๊ณ  ํ•˜๋ฉฐ ์ด๊ฒƒ์€ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’(Loss)๊ฐ€ ํด์ˆ˜๋ก ๋ผ๋ฒจ๊ณผ์˜ ์ฐจ์ด๊ฐ€ ํฌ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ฐ˜๋ณต์„ ํ†ตํ•ด ์ด ๋กœ์Šค ๊ฐ’์ด ์ตœ์†Œ๊ฐ€ ๋˜๋„๋ก ๊ฐ€์ค‘์น˜๋ฅผ ์กฐ๊ธˆ์”ฉ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. [์ˆœ์„œ 3] ์—๋Ÿฌ๋Š” ๋ฌด์—‡์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ๋จผ์ € ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๊ธฐ ์œ„ํ•ด ํšŒ๊ท€ ๋ฌธ์ œ์— ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” MSE๋ฅผ ๋กœ์Šค ํŽ‘์…˜์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ X, Y ๊ฐ’์€ ๊ณ ์ •๋œ ์ƒ์ˆ˜์ด๊ณ  ๊ฐ€์ค‘์น˜(w)๋“ค๋งŒ์ด ๊ณ ์ •๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋กœ์Šค ํŽ‘์…˜์€ ๊ฐ€์ค‘์น˜(w)๋“ค์˜ ํ•จ์ˆ˜๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์˜ ๋ชฉ์ ์€ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ(X, Y)์— ๋Œ€ํ•ด์„œ ๊ฐ€์ค‘์น˜(w)๋“ค์„ ์ ์ ˆํžˆ ์„ธํŒ…ํ•ด์„œ ์—๋Ÿฌ๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฏธ๋ถ„ํ•ด์„œ 0์ด ๋˜๋Š” ๊ฐ€์ค‘์น˜(w)์„ ์ฐพ์œผ๋ฉด ์ข‹๊ฒ ์ง€๋งŒ ์•„์‰ฝ๊ฒŒ ์ˆ˜๋ฐฑ๋งŒ ๊ฐœ์˜ ๋ณ€์ˆ˜(๊ฐ€์ค‘์น˜)๋“ค์ด ์—‰์ผœ์žˆ์–ด ๊ทธ๋ ‡๊ฒŒ ๊ตฌํ•˜๋Š” ๊ฒƒ์€ ๋‹จ์ˆœํ•œ ๋‰ด๋Ÿด๋„ท์ด ์•„๋‹ˆ๊ณ ์„œ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค ๋Œ€์‹  ์ตœ์ ํ•ด(Global optimum)๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฐ˜๋ณต์„ ํ†ตํ•ด์„œ ๊ทธ์ค‘์—์„œ ์ œ์ผ ๋กœ์Šค ๊ฐ’์„ ์ž‘๊ฒŒ ๋งŒ๋“œ๋Š”(minimize) ๊ฐ€์ค‘์น˜(Local optimum)๋ฅผ ์ฐพ๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๊ฐ€์ค‘์น˜(w)๋“ค ์„ธํŒ…๋œ ํ˜„์žฌ ์ง€์ ์—์„œ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๋จผ์ € ๊ตฌํ•ด๋ณด๋ฉด ๊ธฐ์šธ๊ธฐ์˜ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ํ–ฅํ•˜๋Š” ๊ฒƒ์ด ์—๋Ÿฌ์˜ ๊ฐ’์„ ๊ฐ์†Œ์‹œํ‚จ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ํ˜„์žฌ ์œ„์น˜ ๊ธฐ์šธ๊ธฐ์˜ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€์ถฉ์น˜๋ฅผ ์ด๋™ํ•˜๋ฉด ๋ฌด์กฐ๊ฑด ์—๋Ÿฌ๋Š” ๊ฐ์†Œํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. [์ˆœ์„œ 4] ์ด์ œ ๊ฐ€์ค‘์น˜ ๋ฐฉํ–ฅ์„ ์•Œ์•˜์œผ๋‹ˆ ์ด๋™ํ•  ํฌ๊ธฐ๋ฅผ ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜์˜ ์ด๋™ํ•  ์–‘์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ํฌ๊ฒŒ ์ด๋™ํ•˜๋ฉด ๋งŽ์ด ์–ด๊ธ‹๋‚  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋ณดํ†ต์€ ์ž‘์€ ๊ฐ’์œผ๋กœ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์„ ํƒํ•œ ์ž‘์€ ๊ฐ’์— ๊ธฐ์šธ๊ธฐ์˜ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์„ ๊ณฑํ•˜๊ณ  ์ด ํฌ๊ธฐ๋งŒํผ ๊ฐ€์ค‘์น˜๋ฅผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. 3) ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ๋‹ค์‹œ ํ•œ๋ฒˆ ํผ์…‰ํŠธ๋ก ์œผ๋กœ ๋Œ์•„๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ์— ํผ์…‰ํŠธ๋ก ์˜ ์ถœ๋ ฅ๊ฐ’์€ "Activation_Function(๋ชจ๋“  ํผ์…‰ํŠธ๋ก  ์ž…๋ ฅ์˜ ๊ฐ€์ค‘ ํ•ฉ)"์ด ๋ฉ๋‹ˆ๋‹ค. ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜์™€ ์„ ํƒ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜๊ฐ€ ์žˆ๊ณ  ์‹ ๊ฒฝ๋ง์„ ๊ฐœ๋ฐœํ•  ๋•Œ ์‚ฌ๋žŒ์ด ์„ ํƒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ์„ ํƒ์€ ์‹ ๊ฒฝ๋ง์˜ ์˜ˆ์ธก ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ๋ฏธ์นฉ๋‹ˆ๋‹ค, ๋…ผ๋ฌธ์„ ๋ณด๋‹ˆ ๋ณดํ†ต์€ ํ•˜๋‚˜์”ฉ ๋‹ค ํ•ด๋ด์„œ ์„ฑ๋Šฅ์ด ์ข‹์€ ๊ฒƒ์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ์ „์ฒด ๋‰ด๋Ÿด๋„ท์˜ ์ถœ๋ ฅ๊ฐ’์„ ๊ฒฐ์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”ํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 1์žฅ์—์„œ ๋ง์”€๋“œ๋ฆฐ ํšŒ๊ท€ ๋ฌธ์ œ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ์‹ ๊ฒฝ๋ง์˜ ๊ฒฝ์šฐ Sigmoid์™€ ๊ฐ™์ด 0~1์‚ฌ์ด์˜ ๋ฒ”์œ„๋งŒ์„ ๊ฐ–๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŠธ๋ ˆ์ด๋‹์„ ์•„๋ฌด๋ฆฌ ์ง„ํ–‰ํ•ด๋„ ์˜ค์ฐจ(Error)๊ฐ€ ๊ฐ์†Œ๊ฐ€ ๋˜์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ด์ค‘ ๋ถ„๋ฅ˜ ๋ฌธ์ œ True(1), False(0)๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ๋ ˆ์ด์–ด์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ Sigmoid๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‘ ๊ฐœ ์ค‘ ์–ด๋Š ํด๋ž˜์Šค์— ๊ฐ€๊นŒ์šด์ง€ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์ฃผํƒ ๊ฐ€๊ฒฉ ์˜ˆ์ธก๊ณผ ๊ฐ™์€ ํšŒ๊ท€ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ํผ์…‰ํŠธ๋ก ์— ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ฐ€์ค‘ ํ•ฉํ•˜์—ฌ ์˜ˆ์ธก๊ฐ’๋ฅผ ๊ณ„์‚ฐํ•˜๊ฑฐ๋‚˜, ReLU์™€ ๊ฐ™์ด ๊ฐ’์ด ๋ฌดํ•œํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์†Œํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ถœ๋ ฅ ๋ฒ”์œ„ ๋‚ด์— ๋ผ๋ฒจ ๊ฐ’(Y)๋“ค์ด ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ์ •์ƒ์ ์ธ ํ•™์Šต์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ์™œ ํ•„์š”ํ• ๊นŒ? ์กฐ๊ธˆ ์ƒ์†Œํ•œ ์šฉ์–ด์ง€๋งŒ, ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ์‹ ๊ฒฝ๋ง์— ๋น„์„ ํ˜•์„ฑ(non-linearity)์„ ๋”ํ•ด์ค๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋”ฅ๋Ÿฌ๋‹์œผ๋กœ ํ•ด๊ฒฐํ•˜๋ ค๋Š” ๋ฌธ์ œ๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๋‹จ์ˆœํ•œ ๋ฌธ์ œ๋“ค์ด ์•„๋‹™๋‹ˆ๋‹ค. ๊ต‰์žฅํžˆ ๋ณต์žกํ•˜๊ณ  ๋‚œ์žกํ•œ ๋ฌธ์ œ๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ ์†์— ์ˆจ๊ฒจ ์ € ์žˆ๋Š” ์ง„์งœ ํšŒ๊ท€ ์„ , ๊ฒฝ๊ณ„๋ฉด์€ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ(X)์˜ ๋‹ค์ฐจ์› ๊ณต๊ฐ„์—์„œ ๊ต‰์žฅํžˆ ํ•ด๊ดดํ•œ ๊ณก์„ (๋น„์„ ํ˜•)์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ž…๋ ฅ ๋ฐ์ดํ„ฐ(X)์— ๊ฐ€์ค‘ํ•ฉ๋งŒ ์ด์šฉํ•œ๋‹ค๋ฉด ๋ณต์žกํ•˜๊ณ  ์œ ์—ฐํ•œ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ์˜ ์„ ํ˜• ๋ณ€ํ˜•(Transformation)๋งŒ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณต์žกํ•œ ์ง„์งœ ํšŒ๊ท€ ์„ , ๊ฒฝ๊ณ„๋ฉด์— ํ•ํŒ…ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ ์‹ฌํ”Œํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๋ฉด, ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์จ์•ผ๋งŒ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋ณต์žกํ•œ ํ˜•์ƒ์˜ ์„ ์ด๋‚˜ ๊ฒฝ๊ณ„๋ฉด์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค 4) ๋กœ์Šค ํŽ‘์…˜ ์•ž์—์„œ ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์—๋Ÿฌ ํ•จ์ˆ˜๋ผ๊ณ  ๋ง์”€๋“œ๋ ธ์ง€๋งŒ, ๋‹ค์–‘ํ•œ ๋…ผ๋ฌธ ๋ฐ ์ฐธ๊ณ  ์ž๋ฃŒ์—๋Š” ์—๋Ÿฌ ํ•จ์ˆ˜๋ผ๋Š” ์šฉ์–ด๋ณด๋‹ค๋Š” ๋กœ์Šค ํŽ‘์…˜(Loss function)์ด ๋” ์ผ๋ฐ˜์ ์ธ ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ๋กœ์Šค ํŽ‘์…˜์€ ๋ผ๋ฒจ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๋กœ์Šค(Loss)๋ผ๊ณ  ๋งํ•˜๊ณ  ์ด๋“ค์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต์€ ๋กœ์Šค(Loss)๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋ฉฐ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ํ•™์Šต์˜ ์ง„ํ–‰์ด ๋กœ์Šค(Loss)๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ง„ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋กœ์Šค ํŽ‘์…˜(Loss function)์„ ์ œ๋Œ€๋กœ ์ •ํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ๋„คํŠธ์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๋กœ์Šค ํŽ‘์…˜์„ ๋ฐ”๊พธ๋ฉด ๊ฐ€์ค‘์น˜๋Š” ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ๊ฐ€ ์ง„ํ–‰๋˜๊ณ  ์ตœ์ข…์ ์œผ๋กœ ๋‰ด๋Ÿด๋„ท์˜ ์„ฑ๋Šฅ์ด ๋‹ฌ๋ผ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋กœ์Šค ํŽ‘์…˜์€ ๋ชฉ์ ์— ๋งž๋„๋ก ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ๊ณ  ์—ฐ๊ตฌ์ž์˜ ์˜๋„์— ๋”ฐ๋ผ ๋ณ€ํ˜•๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„ ์ผ€์ด์Šค๋ณ„๋กœ ์ถ”๊ฐ€ ํ•™์Šต์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” ๋ถ„๋ฅ˜(Classification)๊ณผ ํšŒ๊ท€(Regression)์— ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์ด๋Š” MSE์™€ Cross Entropy๋ฅผ ์ •๋ฆฌํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Mean Squared Error ๊ฐ€์žฅ ์ง๊ด€์ ์ธ ๋กœ์Šค ํŽ‘์…˜์ž…๋‹ˆ๋‹ค. ๋ผ๋ฒจ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’ ์ฐจ์ด์˜ ์ œ๊ณฑ์„ ๋กœ์Šค๋กœ ๊ณ„์‚ฐํ•˜๋Š” ๋กœ์Šค ํŽ‘์…˜์ž…๋‹ˆ๋‹ค. ์˜ˆ์ธก๊ฐ’์ด ๋ผ๋ฒจ ๊ฐ’๋ณด๋‹ค ํฌ๊ฒŒ ์˜ˆ์ธก๋˜๊ฑฐ๋‚˜ ์ž‘๊ฒŒ ์˜ˆ์ธก๋˜๊ฑด ๊ฐ„์— ์ œ๊ณฑ์„ ๊ณฑํ•ด์ฃผ์–ด ๋ฏธ๋ถ„์ด ๊น”๋”ํ•ฉ๋‹ˆ๋‹ค. ํšŒ๊ท€(Regression) ๋ฌธ์ œ์—์„œ ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Cross Entropy Cross Entropy(์•„๋ž˜๋ถ€ํ„ฐ๋Š” CE๋กœ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค)๋Š” ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋กœ์Šค ํŽ‘์…˜์ž…๋‹ˆ๋‹ค. CE ์ด์ „์— ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ ์ค‘์š”ํ•œ ํ•œ ๊ฐ€์ง€๋ฅผ ๋” ์ •๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค(Softmax) ํ•จ์ˆ˜์ธ๋ฐ ์‚ฌ์‹ค ํ•จ์ˆ˜์ด๊ธด ํ•˜์ง€๋งŒ ๊ทธ๋ƒฅ ํผ์…‰ํŠธ๋ก  ์‚ฌ์ด์— ๋ผ์–ด์žˆ๋Š” ํ•˜๋‚˜์˜ ๋ ˆ์ด์–ด๋กœ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ๋” ์ง๊ด€์ ์ž…๋‹ˆ๋‹ค. True, False์™€ ๊ฐ™์ด ๋‘ ๊ฐœ๋ฅผ ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ๋ผ [๊ฐ•์•„์ง€, ๊ณ ์–‘์ด, ํ•˜๋งˆ, ์‚ฌ์Šด, ๊ธฐ๋ฆฐ]๊ณผ ๊ฐ™์€ ๋‹คํ•ญ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ๋Š” ๋งˆ์ง€๋ง‰ ํผ์…‰ํŠธ๋ก ์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ Sigmoid๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋Š” ์˜ˆ์ธกํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ Output ๋ ˆ์ด์–ด ์ด์ „ ๋ ˆ์–ด์–ด ํผ์…‰ํŠธ๋ก ๋“ค์— ๋งˆ์Œ๋Œ€๋กœ(?) ํ™œ์„ฑํ™” ํ•จ์ˆ˜์„ ๋ถ™์ด๊ณ  ์ด ๊ฐ’๋“ค์„ Softmax ๋ ˆ์ด์–ด๋ฅผ ํ†ต๊ณผ์‹œํ‚ค๋ฉด, Softmax ๋ ˆ์ด์–ด ์•ˆ์—์„œ๋Š” ์ด์ „ ๋ ˆ์ด์–ด ์ถœ๋ ฅ๊ฐ’์˜ ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ๊ฐ๊ฐ์˜ ๋น„์œจ์„ ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ์ข… ์ถœ๋ ฅ๊ฐ’์œผ๋กœ ๋‚ด๋ณด๋ƒ…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ํด๋ž˜์Šค๋ฅผ ๋Œ€ํ‘œํ•˜๋Š” ๋ชจ๋“  ๋…ธ๋“œ๋“ค์˜ ์ถœ๋ ฅ๊ฐ’์˜ ํ•ฉ์€ 1์ด๊ณ  ๋ฒ”์œ„๋Š” 0~1์‚ฌ์ด๋กœ ์ œํ•œ๋ฉ๋‹ˆ๋‹ค. ์ด ์ƒํƒœ์—์„œ CE์˜ ๋กœ์Šค๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. log ํ•จ์ˆ˜์—์„œ 0~1์˜ ์‚ฌ์ด๋Š” ๋ชจ๋‘ ์Œ์ˆ˜๋กœ ์ถœ๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•ž์— -(๋งˆ์ด๋„ˆ์Šค)๊ฐ€ ๋ถ™์—ˆ๊ณ  ๋ผ๋ฒจ์€ ํ•ด๋‹นํ•˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ๊ฐ’๋งŒ 1์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ๋กœ์Šค ๊ฐ’์€ ์ •๋‹ต ํด๋ž˜์Šค์˜ ํ™•๋ฅ ์„ ๋‚ฎ๊ฒŒ ๊ณ„์‚ฐํ• ์ˆ˜๋ก ๋กœ์Šค(Loss) ๊ฐ’์ด ํฌ๊ฒŒ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ํŠน์ˆ˜ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ฉด ๋‹ค์Œ ๋ฐ˜๋ณต ์‹œ์—๋Š” ์ •๋‹ต ํด๋ž˜์Šค์˜ ํ™•๋ฅ ์ด ๋†’๊ฒŒ ๊ณ„์‚ฐ๋˜๋„๋ก ๊ฐ€์ค‘์น˜๊ฐ€ ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค. 5) ์˜ตํ‹ฐ๋งˆ์ด์ € ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” Optimizer๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‰ด๋Ÿด๋„ท์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ์ดํ•ด๊ฐ€ ๊ฐ„ํŽธํ•˜์‹ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•์—์„œ ์ƒ๊ธฐ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋‹จ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์›น์—์„œ ๋„ˆ๋ฌด ์ž˜ ํ‘œํ˜„ํ•œ ํ•œ ์žฅ์˜ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ์–ด ๋จผ์ € ๋ณด๊ณ  ์„ค๋ช…์„ ๊ณ„์†ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. (๊ณต๋ถ€ํ•˜๋Š” ์ž…์žฅ์—์„œ ์ •๋ฆฌ๋ฅผ ๋„ˆ๋ฌด ์ž˜ ๋œ ๊ทธ๋ฆผ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค ) ๊ธฐ์กด์— ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•(GD)์„ ์‚ฌ์šฉํ•  ๋•Œ ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ์ ์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์„ค๋ช…๋“œ๋ฆฌ๊ณ  ์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ป๊ฒŒ ๋ฐœ์ „ํ–ˆ๋Š”์ง€ ํฐ ์ค„๊ธฐ๋ฅผ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. GD(Gradient Descent)์˜ ๋ฌธ์ œ์  ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ œ์ ์€ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค ๊ณ„์‚ฐํ•˜๊ณ  ๋ฐœ์ƒํ•œ ๋ชจ๋“  ์—๋Ÿฌ๋ฅผ ํ•ฉ(Sum) ํ•˜์—ฌ ๊ฐ€์ค‘์น˜(Weight)๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ ํ•™์Šต์— ์‚ฌ์šฉํ•˜๊ธฐ์—๋Š” ํฐ ๋ฌธ์ œ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. ๋‰ด๋Ÿด๋„ท์ด ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜์—ฌ ์ˆ˜์‹ญ ๋ฒˆ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ GD์˜ ๊ฒฝ์šฐ ๋„ˆ๋ฌด ๋Š๋ฆฌ๊ณ , ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋ชจ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋กœ 1000๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์…‹์ด ์กด์žฌํ•˜๊ณ , ๋‰ด๋Ÿด๋„ท์˜ ๊ฐ€์ค‘์น˜๊ฐ€ 200๊ฐœ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. GD์˜ ๋ฐฉ๋ฒ•์€ 1000๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•ด ๋ชจ๋“  gradient๋ฅผ ๋”ํ•œ(Sum) ํ•œ ๊ฐ’์— ํ•™์Šต๋ฅ ์„ ๊ณฑํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, 1Epoch์— 1๋ฒˆ์˜ update๊ฐ€ ์ด๋ค„์ง‘๋‹ˆ๋‹ค. (โ€ป ํŠธ๋ ˆ์ด๋‹ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ํ™œ์šฉํ•˜์—ฌ ํ•™์Šต์„ ์ง„ํ–‰ํ•œ ๊ฒƒ์„ 1Epoch์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.) ์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“  ๊ฐ€์ค‘์น˜๋ฅผ "ํ•œ๋ฒˆ" ๋ณ€ํ™”์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ์•ฝ 1000*200๋ฒˆ์˜ gradient ๊ณ„์‚ฐ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. GD๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ์—์„œ Gradient์˜ ๊ณ„์‚ฐ์ด ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ์—…๋ฐ์ดํŠธ๊ฐ€ ์ด๋ค„์ง€์ง€ ์•Š์•„ ๋„ˆ๋ฌด ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ์™€ ์‹œ๊ฐ„์„ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. SGD(Stochastic Gradient Descent) ์ด๋Ÿฌํ•œ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์•ฝ๊ฐ„์˜ ํŠธ๋ฆญ์„ ์ถ”๊ฐ€ํ•œ SGD ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ์ƒ๊ฒจ๋‚ฌ์Šต๋‹ˆ๋‹ค. SGD๋Š” ์ „์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฐฐ์น˜(Batch) ๋‹จ์œ„๋กœ ๊ฐ€์ค‘์น˜ ์—…๋ฐ์ดํŠธํ•˜์ž๋Š” ์•„์ด๋””์–ด์ž…๋‹ˆ๋‹ค. (โ€ป ๋ฐฐ์น˜(Batch)๋ž€ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋™์ผํ•œ ์ˆซ์ž๋กœ ๋‚˜๋ˆˆ ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค) 1000๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์…‹์„ 200๊ฐœ 1๋ฐฐ์น˜๋กœ ๋ฌถ์œผ๋ฉด, 1epoch ๋‹น ์ด 5๋ฒˆ์˜ ์—…๋ฐ์ดํŠธ๊ฐ€ ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค. ์ •๋ง ์šด ์ข‹๊ฒŒ ๊ธฐ์šธ๊ธฐ์˜ ๋ณ€ํ™”๊ฐ€ ์—†๋Š” ์–ผ๋ฆฌ ์Šคํ†ฑ์˜ ์กฐ๊ฑด์ด 3~4๋ฒˆ์งธ์—์„œ ๋ฐฐ์น˜์—์„œ ์ผ์–ด๋‚ฌ๋‹ค๋ฉด ํ•™์Šต์ด ์กฐ๊ธฐ ์ข…๋ฃŒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์†Œ๊ฐœํ•ด ๋“œ๋ฆด ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ๋ฐฐ์น˜ ์•„์ด๋””์–ด๋ฅผ ์ ์šฉ์‹œํ‚จ SGD๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. GD, SGD์˜ ๊ฐœ์„  ๋ช‡ ๊ฐ€์ง€ ์กฐ๊ธˆ ๋ณต์žกํ•œ ๋ช‡ ๊ฐ€์ง€์˜ ๊ฐœ๋…์„ ํ™œ์šฉํ•จ์œผ๋กœ์จ SGD, GD๋ฅผ ๋”์šฑ ๋ฐœ์ „์‹œํ‚จ ์˜ตํ‹ฐ๋งˆ์ด์ €๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํฌ๊ฒŒ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์˜ตํ‹ฐ๋งˆ์ด์ €์˜ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. โ‘  ๊ณผ๊ฑฐ์˜ ์ง„ํ–‰ ๋ฐฉํ–ฅ์„ ์ฐธ๊ณ ํ•˜๋Š” ๊ด€์„ฑ(Momentum)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค (ex) Momentum, NAG โ‘ก ์ด์ œ๊นŒ์ง€ ๊ณ„์‚ฐ๋œ Gradient์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ํ•™์Šต๋ฅ (Adaptive Learing Rate)์„ ๊ฐ€์ง€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค (ex) Adagrad, RMSProp โ‘ข ๊ด€์„ฑ(Momentum)๊ณผ ์Šคํ… ์‚ฌ์ด์ฆˆ ๋ณ€ํ™”(Adaptive Learning Rate) ๋‘ ๊ฐ€์ง€๋ฅผ ๋‹ค ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. (ex) Adam Momentum ๋ชจ๋ฉ˜ํ…€์€ ๋ฌผ๋ฆฌ์˜ ๊ด€์„ฑ ๊ฐœ๋…์„ ์˜ตํ‹ฐ๋งˆ์ด์ €์— ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ฉ˜ํ…€์€ step์—์„œ์˜ gradient ฮท๊ณผ ์ด์ œ๊นŒ์ง€์˜(๊ณผ๊ฑฐ) ์ด๋™ํ•œ gradient์˜ exponential average๋ฅผ ๋”ํ•ด์ค๋‹ˆ๋‹ค. ์‹์„ ๋ณด๋ฉด ๊ต‰์žฅํžˆ ๋ณต์žกํ•ด ๋ณด์ด์ง€๋งŒ ์‚ฌ์‹ค์€ ๊ทธ์ € ๊ณผ๊ฑฐ์˜ gradient์˜ ํŒŒ๋ผ๋ฏธํ„ฐ(ฮณ)๋กœ ์กฐ์ ˆํ•˜์—ฌ ๋”ํ•ด์ค„ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ฉ˜ํ…€ ๊ฐœ๋…์ด ์ ์šฉ๋œ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด SGD๊ฐ€ ๊ฐ€์ง€์ง€ ๋ชปํ–ˆ๋˜ ์žฅ์ ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋กœ, ๊ด€์„ฑ์ด ๋ถ™์–ด Local optimum์„ ๋ฒ—์–ด๋‚  ์ˆ˜๋„(?) ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ, ๊ถค์ ์ด ํฌ๊ฒŒ ๋ณ€๋™ํ•˜์ง€ ์•Š์•„, SGD๋ณด๋‹ค ์•ˆ์ •์ ์œผ๋กœ Gradient๊ฐ€ ํ•˜๊ฐ•ํ•ฉ๋‹ˆ๋‹ค. Nesterov Accelerated Gradient(NAG) Momentum์˜ ๋‹จ์ ์€ ๊ฐ€์ค‘์น˜(Weight)์˜ ์ด๋™๋Ÿ‰์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด Momentum๊ณผ Gradient ๋‘ ๊ฐ€์ง€๋ฅผ ํ•œ ๋ฒˆ์— ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. NAG์—์„œ๋Š” ์—…๋ฐ์ดํŠธ๊ฐ€ ๊ณผ๋„ํ•˜๊ฒŒ ๊ณ„์‚ฐ๋˜๋Š” ๊ฒƒ์„ ์ค„์ด๊ธฐ ์œ„ํ•ด, Momentum๊ณผ Gradient์˜ ์ˆœ์„œ๋ฅผ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ์œ„์น˜์—์„œ t ์‹œ์ ์œผ๋กœ Momentum๋งŒ ๊ณ„์‚ฐํ•˜์—ฌ ์ด๋™์„ ํ•˜๊ณ , ์ด๋™ํ•œ ๊ณณ์„ ๊ธฐ์ค€์œผ๋กœ Gradient๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋‘ ๋ฒˆ์งธ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. NAG๋Š” ํ•œ ๋ฒˆ์— ๋จผ ์ด๋™์ด ์•„๋‹ˆ๋ผ, ์ˆœ์„œ๋ฅผ ๋‚˜๋ˆ„์–ด ์ด๋™ํ•˜๋ฏ€๋กœ Momentum๋ณด๋‹ค ๋ถ€๋“œ๋Ÿฌ์šด ๊ถค์ ์„ ๊ฐ€์ง€๊ฒŒ ๋˜๊ณ  ๋ชจ๋ฉ˜ํ…€๊ณผ ๊ฐ™์ด ๊ณผ๋„ํ•œ ์—…๋ฐ์ดํŠธ๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Adagrad Adagrad ์˜ตํ‹ฐ๋งˆ์ด์ €(Optimizer)๋ถ€ํ„ฐ๋Š” ๊ธฐ์กด์— ์ƒ์ˆ˜์˜€๋˜ ํ•™์Šต๋ฅ (ฮท, ๋Ÿฌ๋‹ ๋ ˆ์ดํŠธ, Learing rate)์„ ์„ ํƒ์ ์œผ๋กœ ์กฐ์ ˆํ•˜๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ € ์ž…๋‹ˆ๋‹ค. ํ˜„์‹œ์ ๊นŒ์ง€์˜ ๊ณ„์‚ฐ๋˜์—ˆ๋˜ ๋ชจ๋“  Gradient์˜ ์ œ๊ณฑํ•ฉ์— ๋ฐ˜๋น„๋ก€ํ•˜๋Š” ํ•™์Šต๋ฅ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ํ•™์Šต์ด ์ง„ํ–‰๋ ์ˆ˜๋ก ์ด๋™๋Ÿ‰์€ ์ค„์–ด๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก , Global Optimum ๊ทผ์ฒ˜์—์„œ์˜ ์ด๋™๋Ÿ‰ ๊ฐ์†Œ๋Š” ์ข‹์€ ๊ฒฝ์šฐ์ด์ง€๋งŒ, Global Optimum ๊ทผ์ฒ˜๊นŒ์ง€ ๋„๋‹ฌํ•˜์ง€ ๋ชปํ–ˆ๋Š”๋ฐ๋„ ์ด๋™๋Ÿ‰์ด ์ค„์–ด๋“œ๋Š” ๊ฒƒ์€ ํฐ ๋‹จ์ ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ RMSprop๋Š” ์ด๋Ÿฐ ์ ์„ ๋ณด์™„ํ–ˆ์Šต๋‹ˆ๋‹ค. RMSProp ์•ž์—์„œ ๋ง์”€๋“œ๋ฆฐ Adagrad Optimizer์˜ ๋‹จ์ ์„ ๊ฐœ์„ ํ•œ ์˜ตํ‹ฐ๋งˆ์ด์ €์ž…๋‹ˆ๋‹ค. ํฌ๊ฒŒ ๋ฐ”๋€Œ์ง€๋Š” ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ณผ๊ฑฐ Gradient์˜ ๊ฐ’๋“ค๊ณผ ํ˜„์žฌ gradient ๊ฐ’๋“ค์˜ ๋น„์œจ์„ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์กฐ์ ˆํ•ฉ๋‹ˆ๋‹ค. Adam RMSProp + Momentum Adaptive Learning rate์™€ Momentum ๊ฐœ๋…์ด ํ•ฉ์ณ์ง„ ์˜ตํ‹ฐ๋งˆ์ด์ €์ž…๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€๊ฐ€ ํ•ฉ์ณ์กŒ๊ธฐ์— ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋‘ ๊ฐœ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค ์—ฌ๋Ÿฌ ์ฝ”๋“œ๋ฅผ ๋ณผ ๋•Œ ํ˜„์ œ ์ œ์ผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ € ๊ฐ™์Šต๋‹ˆ๋‹ค. Reference https://icim.nims.re.kr/post/easyMath/428 https://needjarvis.tistory.com/694 6) ๋”ฅ๋Ÿฌ๋‹ ํ…Œํฌ๋‹‰ ๋”ฅ๋Ÿฌ๋‹์€ ๋‹ค๋ฅธ ML์— ๋น„ํ•ด ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๋ฐœํœ˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์ง€๋งŒ, ํ•™์Šตํ•˜๊ธฐ๊ฐ€ ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ณ…ํ„ฐ์—์„œ๋Š” ๋”ฅ๋Ÿฌ๋‹์˜ ์„ฑ๋Šฅ๊ณผ ํ•™์Šต์„ ์œ„ํ•ด ๊ฐœ๋ฐœ๋œ ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•๋“ค์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ ์‚ฌ๋ผ์ง(Gradient Vanishing) ๋ฌธ์ œ์™€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜(Activation Function) ๋”ฅ๋Ÿฌ๋‹์—์„œ ๋ณต์žกํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ณดํ†ต ์‚ฌ์šฉ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ Layer๋ฅผ ์Œ“์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณต์žกํ•œ Layer๊ฐ€ ์Œ“์˜€์„ ๊ฒฝ์šฐ ๋ฐœ์ƒํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ œ๊ฐ€ ๊ธฐ์šธ๊ธฐ ์‚ฌ๋ผ์ง(Gradient Vanishing)์ž…๋‹ˆ๋‹ค. ์•ž์—์„œ ์„ค๋ช…๋“œ๋ฆฐ ๊ฒƒ๊ณผ ๊ฐ™์ด ๋‰ด๋Ÿด๋„ท ํ•™์Šต์—์„œ๋Š” ์˜ค์ฐจ ์—ญ์ „ํŒŒ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ค์ฐจ ์—ญ์ „ํŒŒ๋Š” ํ•„์—ฐ์ ์œผ๋กœ ๊ณผ๊ฑฐ ์‚ฌ์šฉ๋œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ๋„ํ•จ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ณฑํ•ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋งŒ์•ฝ Sigmoid์™€ ๊ฐ™์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด, ํ•„์—ฐ์ ์œผ๋กœ ๊ธฐ์šธ๊ธฐ ์‚ฌ๋ผ์ง ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ์˜ ํ•จ์ˆ˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๋ฉด ์œ„์™€ ๊ฐ™์ด ์ƒ๊ฒผ๊ณ , ๋„ํ•จ์ˆ˜๋Š” ์šฐ์ธก๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ๋„ํ•จ์ˆ˜ ๊ฐ’์€ 0~1/4 ์‚ฌ์ด ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋„ํ•จ์ˆ˜ ๊ฐ’์ด ๊ฑฐ๋“ญํ•ด์„œ ๊ณฑํ•ด์ง€๊ฒŒ ๋  ๊ฒฝ์šฐ ํ•„์—ฐ์ ์œผ๋กœ 0์œผ๋กœ ๊ฐ’์ด ์ˆ˜๋ ดํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ค์ฐจ์˜ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๊ฐ’(dE/dw)์€ 0์ด ๋˜์–ด ๊ฐ€์ค‘์น˜๋Š” ๋” ์ด์ƒ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํ˜„์ƒ์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ๋„ํ•จ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ณฑํ•ด์ง€๋Š” Input Layer ๊ทผ๋ฐฉ์—์„œ ๋” ํฌ๊ฒŒ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜(Activiation Function)์„ ๋ณ€๊ฒฝํ•ด ์–ด๋Š ์ •๋„ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ReLU ํ•จ์ˆ˜๋Š” ๋„ํ•จ์ˆ˜ ๊ฐ’์ด 0 ๋˜๋Š” 1๋กœ ๊ฒฐ์ •์ด ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ค์ฐจ๋ฅผ ์•ž ๋ ˆ์ด์–ด์— ์ „๋‹ฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ 100% ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋กœ ํ•™์Šต์ด ์ง„ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฑฐ๋“ญ ๊ณฑํ•ด์ง„๋‹ค๊ณ  ํ•ด์„œ ๋‹คํ–‰ํžˆ Sigmoid์™€ ๊ฐ™์ด Gradient Vanishing ๋ฌธ์ œ๊ฐ€ ํ•„์—ฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ํฌ๋ฐ•ํ•œ ํ™•๋ฅ ์ด์ง€๋งŒ ํŠน์ • layer์—์„œ ๋ชจ๋“  ํผ์…‰ํŠธ๋ก ๋“ค์˜ ๋„ํ•จ์ˆ˜ ๊ฐ’์ด 0์ด ๋˜์–ด๋ฒ„๋ ค Gradient Vanishing์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ด ๋ฌธ์ œ๋ฅผ ๊ฐœ์„ ํ•œ Leaky ReLU์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค) ์ด๋Ÿฐ ์ด์œ ๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ Hidden Layer์—์„œ๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ReLU ๊ณ„์—ด์„ ์‚ฌ์šฉํ•˜๊ณ  ๋งˆ์ง€๋ง‰ Output Layer์—์„œ๋งŒ Sigmoid์™€ ๊ฐ™์ด ๋ชฉ์ ์— ๋งž๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋ฒ„ ํ”ผํŒ…(Overfitting)๊ณผ ์ •๊ทœํ™”(Regularization) ๋”ฅ๋Ÿฌ๋‹์—์„œ ๊ฐ€์žฅ ํ”ํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚˜๊ณ  ํ•ด๊ฒฐํ•˜๊ธฐ ์–ด๋ ค์šด ๋ฌธ์ œ๋Š” ์˜ค๋ฒ„ ํ”ผํŒ…(Overfitting)์ž…๋‹ˆ๋‹ค. ์–ธ๋”, ์˜ค๋ฒ„ ํ”ผํŒ…(Overfitting)์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ณ , ์ด ํ˜„์ƒ์„ ๋ง‰๊ธฐ ์œ„ํ•œ ๋Œ€ํ‘œ์ ์ธ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ• Early Stop, Weight decay, Dropout์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ธ๋” ํ”ผํŒ…(Underfitting), ์˜ค๋ฒ„ ํ”ผํŒ…(Overfitting) ์ง€๋„ ํ•™์Šต ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ(Supervised Deep Learning)์˜ ๋ชฉํ‘œ๋Š” ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜ ์„ค๋ช…ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ , ๋งŒ๋“ค์–ด์ง„ ๋ชจ๋ธ์„ ์ด์šฉํ•ด์„œ ํ•™์Šตํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ์—์„œ๋„ ์˜ˆ์ธก๋ ฅ์ด ์šฐ์ˆ˜ํ•œ ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ์šฐ๋ฆฌ๋Š” Gradient descent๋ฅผ ์ด์šฉํ•ด์„œ ์˜ˆ์ธก๊ฐ’(Prediction)๊ณผ ํ˜„์ƒ(Label) ๊ฐ„์˜ ์˜ค์ฐจ(Loss)๋ฅผ ์ตœ๋Œ€ํ•œ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ํ•™์Šต(Training) ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋”ฅ๋Ÿฌ๋‹ ํ•™์Šต์— ์‚ฌ์šฉํ•œ ์‹ ๊ฒฝ๋ง ๊ตฌ์กฐ๊ฐ€ ๋„ˆ๋ฌด ๋‹จ์ˆœ(too simple) ํ•ด์„œ Training ๋ฐ์ดํ„ฐ์™€ Test ๋ฐ์ดํ„ฐ ๋ชจ๋‘๋ฅผ ์„ค๋ช…ํ•˜๊ณ  ์˜ˆ์ธกํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์šฐ๋ฆฌ๋Š” ์–ธ๋” ํ”ผํŒ… ํ˜„์ƒ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด์— ๋”ฅ๋Ÿฌ๋‹ ํ•™์Šต์— ๋ณต์žกํ•œ ๋ชจ๋ธ(too complex)์„ ์‚ฌ์šฉํ•˜๋ฉด Training ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์ •ํ™•ํžˆ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, Test ๋ฐ์ดํ„ฐ์˜ ์˜ˆ์ธก๋ ฅ์€ ๋งค์šฐ ๋–จ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ผ€์ด์Šค๋ฅผ Overfitting์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Underfitting ๋ฌธ์ œ์˜ ์„ค๋ฃจ์…˜์€ (์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๋” ์ž˜ ์„ค๋ช…ํ•˜๋Š”) ์ ๋‹นํ•œ ๋ชจ๋ธ์„ ์„ ํƒ/๊ฐœ๋ฐœํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ํ•™์Šต์„ ๋” ๋งŽ์ด ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฏธ ์ถฉ๋ถ„ํžˆ ๋ณต์žกํ•œ ์‹ ๊ฒฝ๋ง์—์„œ ๋‚˜ํƒ€๋‚˜๋Š” Overfitting ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ์„ ์œ„ํ•ด์„œ๋Š” ๊ธฐ์กด ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ •๊ทœํ™”(Regularization) ๋ฐฉ๋ฒ•์„ ์ถ”๊ฐ€ํ•ด์„œ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์–ผ๋ฆฌ ์Šคํ†ฑ(Early stopping) ์˜ค๋ฒ„ ํ”ผํŒ…์„ ๋ง‰๋Š” ๊ฐ€์žฅ ์†์‰ฌ์šด ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ํŠธ๋ ˆ์ด๋‹ ์„ธํŠธ์— ๊ฐ€์ค‘์น˜๊ฐ€ ์ตœ์ ํ™”๋˜๊ธฐ ์ด์ „์— ํ•™์Šต์„ ์กฐ๊ธฐ์ข…๋ฃŒ ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๊ฐ€์ค‘์น˜ ์—…๋ฐ์ดํŠธ์— ํ…Œ์ŠคํŠธ ์„ธํŠธ๋ฅผ ํฌํ•จ์‹œํ‚ค์ง€ ์•Š๊ณ  ์—๋Ÿฌ ๋ชจ๋‹ˆํ„ฐ๋ง๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ์…‹์˜ ํ•™์Šต์ด ์ง„ํ–‰๋ ์ˆ˜๋ก ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ํšŸ์ˆ˜(ex, 5ํšŒ) ๋™์•ˆ ๋กœ์Šค๊ฐ€ ์ƒ์Šนํ•œ๋‹ค๋ฉด ์˜ค๋ฒ„ํ•์ด ๋‚˜ํƒ€๋‚œ๋‹ค๊ณ  ๊ฐ„์ฃผํ•˜๊ณ  ํ•™์Šต์„ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค. Weight decay ๋กœ์Šค ํŽ‘์…˜์— ์ถ”๊ฐ€์ ์ธ ํŽ˜๋„ํ‹ฐ ํ…€์„ ๋ถ™์—ฌ์„œ ๋กœ์Šค ํŽ‘์…˜์„ ์ˆ˜์ •ํ•˜์—ฌ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต์€ L2 Regularization์„ ์‚ฌ์šฉํ•˜๋‹ˆ L2๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ E(W)์˜ ์‹์€ ์•ž์„œ ์„ค๋ช…๋“œ๋ฆฐ ๊ฒƒ๊ณผ ๊ฐ™์ด Label๊ณผ Prediction ๊ณผ์˜ Error๋ฅผ ์ตœ์†Œํ™”๋˜๋„๋ก ํ•™์Šต์ด ์ง„ํ–‰๋˜๊ณ  ๋’ค์˜ ฮป*(W^2)๋Š” ๊ฐ€์ค‘์น˜์˜ L2 norm์„ ์ตœ์†Œํ™”ํ•˜๋ผ๋Š” ๋ง๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ชจ์ˆœ์ ์ธ ์š”๊ตฌ ์กฐ๊ฑด์ด ๋‘ ๊ฐ€์ง€๋กœ ์„ž์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€์ค‘์น˜์˜ L2 norm์„ ์ตœ์†Œํ™”ํ•˜๋ฉด Error๋Š” ์ƒ์Šนํ•˜๊ณ , Error๋ฅผ ์ตœ์†Œํ™”ํ•˜๋ฉด L2 norm์˜ ๊ฐ’์€ ์ƒ์Šนํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด, ๋‘ ๊ฐ€์ง€ ์š”๊ตฌ์‚ฌํ•ญ ๊ฐ€์ค‘์น˜์˜ L2 norm๊ณผ Error๋ฅผ ํ•ฉ์ณค์„ ๋•Œ ์ตœ์†Œํ™”ํ•˜๋Š” ์ ๋‹นํ•œ(?) ๊ฐ€์ค‘์น˜๋ฅผ ์ฐพ์œผ๋ผ๋Š” ์š”๊ตฌ ์กฐ๊ฑด์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์š”๊ตฌ ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๋ฉด ๋‰ด๋Ÿด๋„ท์€ Training data์— ์ตœ์ ํ™”๋œ ๊ฐ€์ค‘์น˜๊ฐ€ ์•„๋‹ˆ๋ผ ๋ชจ๋“  ํผ์…‰ํŠธ๋ก ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์ผ์ • ์ˆ˜์ค€ ์ด์ƒ์„ ๊ฐ€์ง€๋Š” ๊ท ํ˜•์ ์ธ ๊ฐ€์ค‘์น˜๋กœ ํ•™์Šต์ด ๋ฉ๋‹ˆ๋‹ค. Dropout ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ์˜ ์•™์ƒ๋ธ”๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ Feature๋ฅผ ๋žœ๋ค์œผ๋กœ ์„ ํƒํ•˜๊ณ  ๋งŒ๋“ค์–ด์ง„ Decision Tree๋“ค์˜ ์•™์ƒ๋ธ”๋กœ ๊ฒฐ๊ด๊ฐ’์„ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋‰ด๋Ÿด๋„ท์˜ Dropout๋„ ์ด๊ฒƒ๊ณผ ๋งค์šฐ ํก์‚ฌํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋งˆ๋‹ค ํžˆ๋“  ๋ ˆ์ด์–ด์—์„œ ์ž„์˜์˜ ๋…ธ๋“œ๋“ค์„ ๋žœ๋ค์œผ๋กœ ์„ ํƒํ•ด์„œ ํ•™์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‚จ๊ฒจ์ง„ ํผ์…‰ํŠธ๋ก ๊ณผ ์—ฐ๊ฒฐ๋œ ๊ฐ€์ค‘์น˜๋Š” drop ์•ˆ ํ•  ๋•Œ๋ณด๋‹ค ๋” ๊ฐ•ํ•œ ๊ฐ€์ค‘์น˜ ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ ํ…Œ์ŠคํŠธ(inference) ์‹œ์—๋Š” drop ํ•˜์ง€ ์•Š๊ณ  ๋ชจ๋“  ๊ฐ€์ค‘์น˜์— (1-drop_rate)%๋ฅผ ๊ณฑํ•ด ์ „์ฒด ๊ฐ€์ค‘์น˜๋ฅผ ํ•œ๋ฒˆ ์Šค์ผ€์ผ๋งํ•˜์—ฌ Prediction ํ•ฉ๋‹ˆ๋‹ค. 7) ์„ฑ๋Šฅ ํ‰๊ฐ€(โ˜…์ž‘์„ฑ ์ค‘) ... (2) ๋”ฅ๋Ÿฌ๋‹ ๊ธฐ๋ณธ ๋„คํŠธ์›Œํฌ ๋”ฅ๋Ÿฌ๋‹ ๊ธฐ๋ณธ ๊ตฌ์กฐ์ธ MLP, CNN, RNN ๊ทธ๋ฆฌ๊ณ  ํŠธ๋žœ์Šคํฌ๋จธ์™€ ์˜คํ†  ์ธ์ฝ”๋”์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค. 1) MLP(โ˜…์ž‘์„ฑ ์ค‘) ... 2) CNN CNN ์ด๋ž€? ์ด๋ฏธ์ง€์— ํŠนํ™”๋œ ์‹ ๊ฒฝ๋ง ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ํ•™์Šต ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ FC ๊ตฌ์กฐ์— ๋น„ํ•ด ๋งค์šฐ ์ ์–ด, ๋‹ค๋ฅธ ๋„คํŠธ์›Œํฌ์— ๋น„ํ•ด ํšจ์œจ์ ์ธ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. (์‚ฌ์‹ค ์ด ๋ถ„์•ผ, ์ € ๋ถ„์•ผ์— ๋‹ค ์“ฐ์—ฌ์„œ... ์ด๋ฏธ์ง€์— ํŠนํ™”๋˜์—ˆ๋‹ค๊ณ  ํ•˜๊ธฐ์—๋„ ์–ด๋ ต์Šต๋‹ˆ๋‹ค) ์•ˆ ์“ฐ์ด๋Š” ๊ณณ์ด ์—†๋Š” ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ์ด๋ฏ€๋กœ ๊นŠ์€ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•œ ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ์ปค๋„(kernel) ์ปค๋„(kernel)์€ ํ•„ํ„ฐ(filter), ๋งˆ์Šคํฌ(mask)์™€ ๊ฐ™์€ ๋ง์ž…๋‹ˆ๋‹ค. ์ปค๋„์˜ ๊ธฐ๋Šฅ์€ ์ด๋ฏธ์ง€๋กœ๋ถ€ํ„ฐ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ •๋ณด(ํŠน์ง•)๋งŒ์„ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ปค๋„์€ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •๋œ ๊ฐ„๊ฒฉ(stride)์œผ๋กœ ์›€์ง์ด๋ฉฐ ํ•ฉ์„ฑ๊ณฑ(convolution)์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ณผ๊ฐ€ feature map์ž…๋‹ˆ๋‹ค. ์ปค๋„์€ ์ด๋ฏธ์ง€์˜ ํŠน์ง•์„ ์ฐพ์•„๋‚ด๊ธฐ ์œ„ํ•œ ๊ณต์šฉ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ (2,2), (3,3) ๋“ฑ์˜ ์ •์‚ฌ๊ฐ ํ–‰๋ ฌ๋กœ ์ •์˜๋˜๊ณ  CNN์—์„œ ํ•™์Šต์˜ ๋Œ€์ƒ์€ ํ•„ํ„ฐ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๊ฐ๊ฐ์˜ ์ฑ„๋„์—์„œ ์ง€์ •๋œ ๊ฐ„๊ฒฉ์œผ๋กœ ์ˆœํšŒํ•˜๋ฉฐ ํ•ฉ์„ฑ ๊ณฑ์„ ํ•˜๊ณ  ๋ชจ๋“  ์ฑ„๋„์˜ ํ•ฉ์„ฑ๊ณฑ์˜ ํ•ฉ์ธ feature map์ด ์ถœ๋ ฅ์œผ๋กœ ๋‚˜์˜ต๋‹ˆ๋‹ค. stride๊ฐ€ 1์ธ ํ•„ํ„ฐ ์ ์šฉ ์ž…๋ ฅ ์ฑ„๋„์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ MLP๋Š” ๋ชจ๋“  edge๊ฐ€ ๊ฐ์ž์˜ weight ๊ฐ’์„ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๊ฐ€ ๋งŽ์ด ํ•„์š”ํ•˜์ง€๋งŒ CNN์—์„œ filter๋Š” ๋ชจ๋“  ์ž…๋ ฅ ๋…ธ๋“œ์— ํ•˜๋‚˜์˜ filter๋ฅผ ์ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๊ฐ€ ์—„์ฒญ๋‚˜๊ฒŒ ๊ฐ์†Œํ•˜์—ฌ ์—ฐ์‚ฐ ์†๋„์™€ ํšจ์œจ์„ฑ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ํ’€๋ง(Pooling) CNN์—์„œ pooling layer๋Š” ๋„คํŠธ์›Œํฌ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐœ์ˆ˜๋‚˜ ์—ฐ์‚ฐ๋Ÿ‰์„ ์ค„์ด๊ธฐ ์œ„ํ•ด input์—์„œ spatial ํ•˜๊ฒŒ downsampling์„ ์ง„ํ–‰ํ•ด ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์ด๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ CNN์—์„œ๋Š” Convolution layer ๋‹ค์Œ์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ๋ณธ๋ฌธ์—์„œ ์†Œ๊ฐœํ•˜๋Š” max pooling ์™ธ์—๋„ average pooling, L2-norm pooling ๋“ฑ ๋‹ค์–‘ํ•œ pooling ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. Pooling(downsampling)์ด ํ•„์š”ํ•œ ์ด์œ ๋Š”, featuremap์˜ weight parameter ๊ฐœ์ˆ˜๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. pooling layer๊ฐ€ ์—†๋‹ค๋ฉด, ๋„ˆ๋ฌด ๋งŽ์€ weight parameter๊ฐ€ ์ƒ๊ธฐ๊ณ , ์‹ฌ๊ฐํ•œ overfitting์„ ์œ ๋„ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋งŽ์€ ์—ฐ์‚ฐ์„ ํ•„์š”๋กœ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ Pooling์„ ์‚ฌ์šฉํ•˜๋ฉด ์—ฐ์†์ ์ธ ConvNet ์ธต์ด ์ ์  ์ปค์ง€๋Š” window๋ฅผ ๋ณด๋„๋ก ๋งŒ๋“ค์–ด (receptive field๋ฅผ ๋„“ํž˜) ํ•„ํ„ฐ์˜ ๊ณต๊ฐ„์ ์ธ ๊ณ„์ธต๊ตฌ์กฐ๋ฅผ ํ˜•์„ฑํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Pooling์˜ Hyper parameter : filter size : stride Max pooling Max pooling์€ ์œ ์˜๋ฏธํ•œ ์ •๋ณด๊ฐ€ ๋†’์€ signal (activation function์˜ output)์„ ๊ฐ€์งˆ ๊ฒƒ์ด๋ผ๋Š” ๊ฐ€์ •์ด ๊ธฐ์ €์— ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณ ๋กœ ๋ณด๊ณ ์ž ํ•˜๋Š” ์˜์—ญ ์•ˆ์—์„œ ๊ฐ€์žฅ ๋†’์€ ๊ฐ’ ํ•˜๋‚˜๋ฅผ ๋„˜๊น๋‹ˆ๋‹ค. Back propagation๊ณผ Max pooling Convolution์ด๋‚˜ Average pooling์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ ํ˜•๋ณ€ํ™˜์ด๋ผ ๋ฏธ๋ถ„์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ maxout ์—ฐ์‚ฐ์€ ๋ฏธ๋ถ„์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— chain rule์„ ์ด์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ตœ๋Œ“๊ฐ’์ด ์†ํ•ด์žˆ๋Š” ์š”์†Œ์˜ local gradient๋Š” 1, ๋‚˜๋จธ์ง€์˜ gradient๋Š” 0์œผ๋กœ ๋‘๋Š” drop out์—์„œ์˜ backprop๊ณผ ๋น„์Šทํ•œ ์•„์ด๋””์–ด๋กœ backpropagation์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Average pooling ์šฐ๋ฆฌ๊ฐ€ ๋ณด๊ณ ์ž ํ•˜๋Š” ์œ„์น˜์—์„œ ํ‰๊ท ๊ฐ’์„ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด noise๋„ ์ค„์ด๊ณ  ๊ทธ ์˜์—ญ์„ ์ž˜ ์„ค๋ช…ํ•˜๋Š” ๊ฐ’์ด๋‹ˆ ์ข‹์„ ๊ฒƒ์ด๋‹ค!๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฐ€์ •ํ•˜์—์„œ ํ‰๊ท ๊ฐ’์„ ๋„˜๊ธฐ๋Š” ๋ฐฉ๋ฒ•์„ Average pooling์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Average pooling์€ ๊ณผ๊ฑฐ LeNet์—์„œ ์‚ฌ์šฉ๋œ ์—ญ์‚ฌ๊ฐ€ ์žˆ์œผ๋‚˜, ํ˜„์žฌ๋Š” max pooling์ด ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์ž˜ ์“ฐ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋‹ค๋ฅธ Pooling method์™€ ๋น„๊ตํ•˜๋ฉด ์•Œ๊ธฐ ์‰ฌ์šด๋ฐ, ์•„๋ž˜ ๊ทธ๋ฆผ์€ ํ•˜๋‚˜์˜ ๊ทธ๋ฆผ์— average pooling, max pooling, min pooling์„ ์ง„ํ–‰ํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, Average pooling์€ ์ด๋ฏธ์ง€๋ฅผ ์ „์ฒด์ ์œผ๋กœ smoothing ์‹œํ‚ค๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์–ด sharp feature๋ฅผ ์žก์•„๋‚ด์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Min pooling min pooling์€ ์™œ ์ผ๋ฐ˜์ ์œผ๋กœ ์“ฐ์ด์ง€ ๋ชปํ• ๊นŒ์š”? ์ด๋Š” ๋งŽ์€ Activation function๋“ค์ด 0์ดํ•˜์˜ ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ์ทจ๊ธ‰ํ•˜๋Š”์ง€๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์–ด๋ ค์›€์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ReLU์˜ ๊ฒฝ์šฐ 0 ์ดํ•˜์˜ ๊ฐ’์€ ์ „๋ถ€ 0์œผ๋กœ ์ฒ˜๋ฆฌํ•ด ๋„˜๊ธฐ๊ธฐ ๋•Œ๋ฌธ์— ์ธต์ด ๊นŠ์–ด์งˆ์ˆ˜๋ก min pooling์„ ํ•˜๊ฒŒ ๋˜๋ฉด ์˜๋ฏธ ์žˆ๋Š” ์ •๋ณด๊ฐ€ ๋„˜์–ด๊ฐ€๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Reference cs230 cheat sheet cs231n lecture note Maxpooling vs minpooling vs average pooling 3) RNN(โ˜…์ž‘์„ฑ ์ค‘) RNN ์ด๋ž€? ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network) ์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง(ANN)์˜ ํ•œ ์ข…๋ฅ˜๋กœ, ์œ ๋‹› ๊ฐ„์˜ ์—ฐ๊ฒฐ์ด ์ˆœํ™˜์ ์ธ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง์— ๋”ฐ๋ผ ์ˆœ์ฐจ์ ์ธ ์ •๋ณด๋ฅผ ๋ชจ๋ธ๋ง ํ•  ์ˆ˜ ์žˆ๋Š” ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. CNN๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ์‹ ๊ฒฝ๋ง๋“ค์€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ ๊ฐ™์€ sequential ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ „์ฒด ์‹œํ€€์Šค๋ฅผ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ๋„ฃ์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋„คํŠธ์›Œํฌ๋ฅผ feed forward network๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. RNN์€ ์ด์™€ ๋ฐ˜๋Œ€๋กœ, ๋‚ด๋ถ€์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ์‹œํ€€์Šค ํ˜•ํƒœ์˜ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ๋…์ ์ธ ์ดํ•ด ๋ฐ ๊ตฌ์กฐ ๊ฐœ๋…์ ์œผ๋กœ RNN์„ ๋‚˜ํƒ€๋‚ด๋ณด์ž๋ฉด, ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์ƒํƒœ( )์—์„œ ๋‹ค์Œ ์ƒํƒœ( + )์˜ ์ž์‹ ์—๊ฒŒ๋กœ ๋ณด๋‚ด๋Š” ์ถœ๋ ฅ๊ฐ’์„ ์šฐ๋ฆฌ๋Š” hidden state๋ผ๊ณ  ํ•˜๊ณ , ์—ฌ๊ธฐ์„œ๋Š” t ๋กœ ๋‚˜ํƒ€๋ƒˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ข€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด, ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. activation function์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์€ ์—์„œ hiddenstate๋กœ ๋“ค์–ด์˜ค๋Š” x x ์™€ ์ด์ „ hidden state์—์„œ ๋‹ค์Œ hidden state๋กœ ๋„˜์–ด๊ฐ€๋Š” h h โˆ’ ์„ ๋”ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ RNN์„ timestep์— ๋”ฐ๋ผ ํŽผ์น˜๋Š” ๊ฒƒ์„ unfolding์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๊ฐ€ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ์‘์šฉ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ, one to one์˜ ๊ฒฝ์šฐ ์ผ๋ฐ˜์ ์ธ vanilla neural net๊ณผ ๊ฐ™๊ณ , one to many์˜ ๊ฒฝ์šฐ Image captioning, many to one์—์„œ๋Š” ๊ฐ์ • ๋ถ„์„, many to many๋Š” ๊ธฐ๊ณ„๋ฒˆ์—ญ ๋˜๋Š” video labeling ๋“ฑ ๋ถ„์•ผ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Bidirectional RNN์€ bidirectional ํ•˜๊ฒŒ ์—ฐ๊ฒฐ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. bidirectional RNN์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. bidirectional ํ•˜๋‹ค๋Š” ๊ฒƒ์˜ ์˜๋ฏธ๋Š” ๊ผญ ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ hidden state๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ด์ „ hidden state๋กœ ์ „๋‹ฌ๋˜๊ธฐ๋„ ํ•˜๋Š” ๋“ฑ ์–‘ ๋ฐฉํ–ฅ์œผ๋กœ ์ „๋‹ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ˆœํ™˜ ๋‰ด๋Ÿฐ(Recurrent Neurons) RNN์€ ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ์„ ๋‹ค์‹œ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š” ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋‰ด๋Ÿฐ์„ ๊ฐ ํƒ€์ž„ ์Šคํ…๋งˆ๋‹ค ์ชผ๊ฐœ์–ด ํŽผ์ณ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ์ด์ „ ํƒ€์ž„ ์Šคํ…์˜ ์ถœ๋ ฅ์„ ๋‹ค์Œ ์Šคํ…์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด์ฃผ๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๊ฐ ํƒ€์ž„ ์Šคํ…์˜ ์ˆœํ™˜ ๋‰ด๋Ÿฐ์€ ๋‘ ๊ฐœ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. x ๋Š” ์ž…๋ ฅ์ธ t ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜์ด๊ณ  y ๋Š” ์ด์ „ ๋‹จ๊ณ„์˜ ์ถœ๋ ฅ์ธ t 1 ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜์ž…๋‹ˆ๋‹ค. t ๋ฅผ ์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๊ณ  CNN์ฒ˜๋Ÿผ bias ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํƒ€์ž„ ์Šคํ… t์—์„œ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ์žˆ๋Š” ๊ฐ ์ƒ˜ํ”Œ์— ๋Œ€ํ•œ ์ˆœํ™˜ ์ธต์˜ ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. t ๋Š” ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ์ž„๋ ฅ๊ฐ’์„ ๋‹ด๊ณ  ์žˆ๋Š” ํ–‰๋ ฌ์ด๊ณ , x ๋Š” ํ˜„์žฌ ํƒ€์ž„ ์Šคํ… t์˜ ์ž…๋ ฅ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ํ–‰๋ ฌ, y ๋Š” ์ด์ „ ํƒ€์ž„ ์Šคํ… t-1์˜ ์ถœ๋ ฅ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. t X ์™€ t ๋กœ ๋‚˜ํƒ€๋‚ด์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์—, ํƒ€์ž„ ์Šคํ… t = 0๋ถ€ํ„ฐ ๋ชจ๋“  ์ž…๋ ฅ์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ ์‹œํ€€์Šค i) LSTM LSTM ์ด๋ž€ LSTM์€ RNN์˜ ํ•œ ์ข…๋ฅ˜๋กœ, RNN์˜ ์žฅ๊ธฐ ์˜์กด์„ฑ ๋ฌธ์ œ(long-term dependencies)๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‚˜์˜จ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ง์ „ ๋ฐ์ดํ„ฐ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ข€ ๋” ๊ฑฐ์‹œ์ ์œผ๋กœ ๊ณผ๊ฑฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๋ฏธ๋ž˜ ๋ฐ์ดํ„ฐ๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. LSTM ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ชจ๋“  RNN์€ Neural Network ๋ชจ๋“ˆ์„ ๋ฐ˜๋ณต์‹œํ‚ค๋Š” ์ฒด์ธ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ RNN์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณต๋˜๋Š” ๊ฐ„๋‹จํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. LSTM๋„ ๋˜‘๊ฐ™์ด ์ฒด์ธ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, 4๊ฐœ์˜ Layer๊ฐ€ ํŠน๋ณ„ํ•œ ๋ฐฉ์‹์œผ๋กœ ์„œ๋กœ ์ •๋ณด๋ฅผ ์ฃผ๊ณ ๋ฐ›๋„๋ก ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ LSTM์˜ ํ•ต์‹ฌ ์•„์ด๋””์–ด๋ฅผ ์„ค๋ช…ํ•ด ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ด 6๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ 4๊ฐœ์˜ ๊ฒŒ์ดํŠธ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. Cell State LSTM์˜ ํ•ต์‹ฌ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ๋ชจ๋“ˆ ๊ทธ๋ฆผ ์œ„์—์„œ ์ˆ˜ํ‰์œผ๋กœ ์ด์–ด์ง„ ์œ„ ์„ ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. Cell State๋Š” ์ปจ๋ฒ ์ด์–ด ๋ฒจํŠธ์™€ ๊ฐ™์•„์„œ ์ž‘์€ linear interaction๋งŒ์„ ์ ์šฉ์‹œํ‚ค๋ฉด์„œ ์ „์ฒด ์ฒด์ธ์„ ๊ณ„์† ๊ตฌ๋™ ์‹œํ‚ต๋‹ˆ๋‹ค. ์ •๋ณด๊ฐ€ ์ „ํ˜€ ๋ฐ”๋€Œ์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ๋งŒ ํ๋ฅด๊ฒŒ ํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ State๊ฐ€ ๊ฝค ์˜ค๋ž˜ ๊ฒฝ๊ณผํ•˜๋”๋ผ๋„ Gradient๊ฐ€ ์ž˜ ์ „ํŒŒ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Gate๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ตฌ์กฐ์— ์˜ํ•ด์„œ ์ •๋ณด๊ฐ€ ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ์ œ๊ฑฐ๋˜๋ฉฐ, Gate๋Š” Training์„ ํ†ตํ•ด์„œ ์–ด๋–ค ์ •๋ณด๋ฅผ ์œ ์ง€ํ•˜๊ณ  ๋ฒ„๋ฆด์ง€ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. Forget Gate ์ด Gate๋Š” ๊ณผ๊ฑฐ์˜ ์ •๋ณด๋ฅผ ๋ฒ„๋ฆด์ง€ ๋ง์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฐ์ •์€ Sigmoid layer์— ์˜ํ•ด์„œ ๊ฒฐ์ •์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ๋Š” t 1 x๋ฅผ ๋ฐ›์•„์„œ 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’์„ t 1 ์— ๋ณด๋‚ด์ค๋‹ˆ๋‹ค. ๊ทธ ๊ฐ’์ด 1์ด๋ฉด "๋ชจ๋“  ์ •๋ณด๋ฅผ ๋ณด์กดํ•ด๋ผ"๊ฐ€ ๋˜๊ณ , 0์ด๋ฉด "์ฃ„๋‹ค ๊ฐ–๋‹ค ๋ฒ„๋ ค๋ผ"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. Input Gate ์ด Gate๋Š” ํ˜„์žฌ ์ •๋ณด๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•œ ๊ฒŒ์ดํŠธ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ์˜ Cell state ๊ฐ’์— ์–ผ๋งˆ๋‚˜ ๋”ํ• ์ง€ ๋ง์ง€๋ฅผ ์ •ํ•˜๋Š” ์—ญํ• ์ž…๋‹ˆ๋‹ค. Update ๊ณผ๊ฑฐ Cell State๋ฅผ ์ƒˆ๋กœ์šด State๋กœ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. Forget Gate๋ฅผ ํ†ตํ•ด์„œ ์–ผ๋งˆ๋‚˜ ๋ฒ„๋ฆด์ง€, Input Gate์—์„œ ์–ผ๋งˆ๋‚˜ ๋”ํ• ์ง€๋ฅผ ์ •ํ–ˆ์œผ๋ฏ€๋กœ ์ด Update ๊ณผ์ •์—์„œ ๊ณ„์‚ฐ์„ ํ•ด์„œ Cell State๋กœ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค. Output Gate ์–ด๋–ค ์ถœ๋ ฅ๊ฐ’์„ ์ถœ๋ ฅํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๊ณผ์ •์œผ๋กœ ์ตœ์ข…์ ์œผ๋กœ ์–ป์–ด์ง„ Cell State ๊ฐ’์„ ์–ผ๋งˆ๋‚˜ ๋นผ๋‚ผ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•ด์ค๋‹ˆ๋‹ค. Reference RNN :: LSTM(Long Short Term Memory) ํ†บ์•„๋ณด๊ธฐ LSTM ์ด๋ž€? RNN, LSTM, GRU๋ž€? Long Short-Term Memory (LSTM) ์ดํ•ดํ•˜๊ธฐ ii) GRU(โ˜…์ž‘์„ฑ ์ค‘) ... 4) ํŠธ๋žœ์Šคํฌ๋จธ(โ˜…์ž‘์„ฑ ์ค‘) ... 5) ์˜คํ† ์ธ ์ฝ”๋”(โ˜…์ž‘์„ฑ ์ค‘) ... (3) ๋”ฅ๋Ÿฌ๋‹ ์‘์šฉ ๋‰ด๋Ÿด๋„ท ๊ธฐ๋ณธ ์—ฐ์‚ฐ๊ณผ๋Š” ๊ด€๋ จ์ด ํฌ์ง€ ์•Š์œผ๋‚˜ Task๋ฅผ ๋ถˆ๋ฌธํ•˜๊ณ  ์ž์ฃผ ํ™œ์šฉ๋˜๊ณ  ๋“ฑ์žฅํ•˜๋Š” ๊ฐœ๋…๋“ค์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ๋ถ€์กฑ์„ ๊ทน๋ณตํ•˜๊ณ  Robust ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” Data Augmentation ํ•™์Šต๋œ ๋‰ด๋Ÿด๋„ท์„ ์žฌํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ Pretraining, Transfer Learning, Fine-Tuning ๊ทธ๋ฆฌ๋„ ๋‰ด๋Ÿด๋„ท์˜ ํฐ ๋‹จ์ ์ธ Under/Overfitting์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์„ ๋‹ค๋ค˜์Šต๋‹ˆ๋‹ค. 1) Data Augmentation ๋”ฅ๋Ÿฌ๋‹์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•ด์•ผ ํ•™์Šต์ด ์ž˜๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋”ฅ๋Ÿฌ๋‹์˜ ๊ณ ์งˆ์ ์ธ ๋ฌธ์ œ์ธ overfitting์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ถฉ๋ถ„ํžˆ ๋งŽ์€, ์–‘์งˆ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๋ฅผ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์€ ๋น„์šฉ๊ณผ ์‹œ๊ฐ„์ด ๋งŽ์ด ํ•„์š”ํ•˜๋ฉฐ, ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ํ•˜๊ฑฐ๋‚˜ ๊ฐ€๊ณตํ•˜๋Š” ๊ฒƒ์กฐ์ฐจ ์–ด๋ ค์šด ๊ฒฝ์šฐ๋„ ๋งŽ์Šต๋‹ˆ๋‹ค. 1) ์ˆ˜์ง‘์ด ์–ด๋ ค์šด ๊ฒฝ์šฐ์˜ ์˜ˆ ์ž์œจ์ฃผํ–‰ ์ธ๊ณต์ง€๋Šฅ ๊ฐœ์„ ์„ ์œ„ํ•ด ๋ˆˆ/๋น„/ํ™ฉ์‚ฌ/์‚ฌ๊ณ  ๋“ฑ ํŠน์ˆ˜ ์ƒํ™ฉ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋ณด์•ˆ์ง€์—ญ ๊ฐ์‹œ ์˜์ƒ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‹ ์ฒด ์ฃผ์š” ๋ถ€์œ„ ํ˜น์€ ๊ฐœ์ธ ์‚ฌ์ƒํ™œ์„ ์นจํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ๊ณ  ๊ฒฌ์  ์‚ฐ์ถœ ์ธ๊ณต์ง€๋Šฅ ํ•™์Šต์„ ์œ„ํ•ด ํ•„์š”ํ•œ ํŒŒ์† ์ฐจ๋Ÿ‰ ๋ฐ์ดํ„ฐ ํš๋“์„ ์œ„ํ•ด ์ฐจ๋Ÿ‰์„ ํŒŒ์†ํ•˜๊ธฐ๋Š” ์–ด๋ ค์›€ 2) ๊ฐ€๊ณต์ด ์–ด๋ ค์šด ๊ฒฝ์šฐ์˜ ์˜ˆ ์˜๋ฃŒ, ๋ฒ•๋ฅ  ๋ฐ์ดํ„ฐ์™€ ๊ฐ™์ด ์ „๋ฌธ์ ์ธ ์ง€์‹์ด ์—†์œผ๋ฉด ๊ฐ€๊ณต์ด ์–ด๋ ค์šด ๊ฒฝ์šฐ ํ‰๊ฐ€์ž์˜ ์ฃผ๊ด€์— ๋”ฐ๋ผ ํŒ๋‹จ ์ •๋„๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ (ex, ์ธ๋ฌผ ํ‘œ์ •) ์ž‘์—…์ž๊ฐ€ ๊ฐ€๊ณต ์ค‘ ํ˜์˜ค๊ฐ์„ ๋Š๋‚„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ (ex, ์Œ๋ž€๋ฌผ, ํญ๋ ฅ์  ์žฅ๋ฉด) ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ์กด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด ์ƒˆ๋กญ๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐฝ์กฐํ•˜๋Š” Data Augmentation ๊ธฐ๋ฒ•์ด ๋‹ค์–‘ํ•˜๊ฒŒ ๊ฐœ๋ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ augmentation ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋งŒ ๋‹ค๋ฃจ๊ธฐ๋กœ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1) Basic image manipulation Geometric Transformation Color space transformations Mixing images Random Erasing Kernel Filters 2) Deep learning approach Adversarial training GAN Data augmentation Neural style Transfer 3) Meta Learning Neural augmentation Autoaugmentation Smart augmentation Data augmentation์˜ ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์ง€๋งŒ ์ง€์ผœ์•ผ ํ•  ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Semantically Invariant Transformation, ์ฆ‰ Data์—์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์„ ๋ณด์กดํ•˜๋Š” ์„ ์—์„œ ์ตœ๋Œ€ํ•œ augmentation ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. 1) Basic image manipulation Geometric Transformation์€ ๊ธฐ์กด ์ด๋ฏธ์ง€๋ฅผ Crop, Rotate, Contrast, Invert, Flip ์‹œ์ผœ ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. Color space transformations์€ ๊ธฐ์กด ์ด๋ฏธ์ง€์˜ RGB ๊ฐ’์„ ์กฐ์ •ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. Mixing images์€ ๋‘ image๋ฅผ 0~1 ์‚ฌ์ด์˜ ฮป ๊ฐ’์„ ํ†ตํ•ด Weighted Linear Interpolation ํ•ด์ฃผ๋Š” ๊ธฐ๋ฒ•์œผ๋กœ, Label๋„ ฮป๊ฐ’์— ๋น„๋ก€ํ•˜์—ฌ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. Random Erasing: ์ด๋ฏธ์ง€์˜ ๋žœ๋ค ํ•œ ์˜์—ญ์„ ์ง€์›Œ์„œ ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ƒ…๋‹ˆ๋‹ค. Basic image manipulation ๋ฐฉ๋ฒ•์„ ์กฐํ•ฉ: CutMix๋Š” Mixing images์™€ Random Erasing ๋ฐฉ๋ฒ•์„ ํ•ฉ์นœ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. A image์—์„œ box๋ฅผ ์ณ์„œ<NAME> ๋‹ค์Œ ๊ทธ ๋นˆ ์˜์—ญ์„ B image๋กœ๋ถ€ํ„ฐ patch๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์ง‘์–ด๋„ฃ์Šต๋‹ˆ๋‹ค. Patch์˜ ๋ฉด์ ์— ๋น„๋ก€ํ•˜์—ฌ Label๋„ ์„ž์–ด ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. PuzzleMix๋Š” cutmix๋ฅผ ๊ฐœ๋Ÿ‰ํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋‘ ์ด๋ฏธ์ง€์—์„œ ์ค‘์š”ํ•œ feature๋Š” ๋ณด์กดํ•˜๋ฉด์„œ ์„ž๋„๋ก ํ•ฉ๋‹ˆ๋‹ค ์ž์„ธํ•œ ์„ค๋ช…์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. 2) Deep learning approach Adversarial training Adversarial attack ์ด๋ž€ DNN์ด ์ž˜๋ชป๋œ ๊ฒฐ๊ณผ๋ฅผ ์‚ฐ์ถœํ•˜๋„๋ก ์˜๋„์ ์œผ๋กœ ์กฐ์ž‘์‹œํ‚จ ์ž…๋ ฅ๊ฐ’ (adversarial example)์„ ์ƒ์„ฑํ•˜์—ฌ training model์— ์ œ์‹œํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 57%์˜ ์ •ํ™•๋„๋กœ ์•„๋ž˜ ๊ทธ๋ฆผ์„ panda๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ชจ๋ธ์ด ์žˆ๋‹ค๊ณ  ํ•˜์ž. ์œก์•ˆ์ƒ์œผ๋กœ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์—†๋Š” ์–ด๋–ค ๋…ธ์ด์ฆˆ๋ฅผ ์ฃผ์–ด ์ด๋ฏธ์ง€๋ฅผ ์กฐ์ž‘ํ•œ ๋’ค ํ•ด๋‹น ๋ชจ๋ธ์— ์ œ์‹œํ•œ. ์‚ฌ๋žŒ์˜ ๋ˆˆ์—๋Š” ์—ฌ์ „ํžˆ ํŒ๋‹ค๋กœ ๋ณด์ด์ง€๋งŒ DNN ๋ชจ๋ธ์€ ์ด๋ฅผ ๊ธดํŒ”์›์ˆญ์ด(Gibbon)์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ  ์žˆ๋‹ค. Adversarial attack์€ ๋งค์šฐ ์œ„ํ—˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ๋กœ ๋„๋กœ ๊ตํ†ต ํ‘œ์ง€ํŒ์— ์ž‘์€ ์Šคํ‹ฐ์ปค๋ฅผ ๋ถ™์—ฌ "STOP" ํ‘œ์ง€ํŒ์„ "์‹œ์† 45๋งˆ์ผ ์†๋„ ์ œํ•œ" ์‹ ํ˜ธ๋กœ ์˜ค ๋ถ„๋ฅ˜ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ์ž์œจ์ฃผํ–‰ ์ž๋™์ฐจ๊ฐ€ ์ด๋Ÿฐ Adversarial attack์— ๋…ธ์ถœ๋œ๋‹ค๋ฉด? ์ •๋ง ํฐ ์‚ฌ๊ณ ๋กœ ์ด์–ด์งˆ ์ˆ˜๋„ ์žˆ๋‹ค. Adversarial attack์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์—ฌ๊ธฐ ์ฐธ๊ณ  Adversarial Example์„ ๋งŒ๋“œ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ด๋ฏธ ๊ฐœ๋ฐœ๋˜์–ด ์žˆ๋‹ค. Adversarial training ์ด๋ž€ Adversarial example์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ œ์ž‘ํ•˜์—ฌ ๋ชจ๋ธ์— ์ œ์‹œํ•œ ๋‹ค์Œ ์–ด๋–ค ์ƒํ™ฉ์—์„œ ๋ชจ๋ธ์ด ์˜ค ๋ถ„๋ฅ˜๋ฅผ ์ผ์œผํ‚ค๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ๋ชจ๋ธ์„ ์ˆ˜์ •ํ•˜์—ฌ ์ „์ฒด์ ์ธ ์„ฑ๋Šฅ์„ ๋†’์ด๋Š” ํ•™์Šต ๋ฐฉ๋ฒ•์ด๋‹ค. GAN Data augmentation GAN (Generative Adversarial Networks) ๋ชจ๋ธ์„ ์ด์šฉํ•ด ๊ธฐ์กด ๋ฐ์ดํ„ฐ์™€ ์œ ์‚ฌํ•œ ์ƒ˜ํ”Œ์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ˆ˜๋ฅผ ๋Š˜๋ฆฐ๋‹ค. (์•„๋ž˜ ์‚ฌ์ง„ ์ฐธ๊ณ ) 3) Meta Learning Autoaugmentation ์ˆ˜๋งŽ์€ data augmentation ๋ฐฉ๋ฒ• ์ค‘ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹์— ์ ํ•ฉํ•œ ๊ธฐ๋ฒ•์„ ์ œ์‹œํ•˜๋Š” ๋ชจ๋ธ์„ AutoAugmentation์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๊ธ€์€ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” 16๊ฐ€์ง€ data augmentation ๊ธฐ๋ฒ•๋“ค ์ค‘ ์ตœ์ ์˜ ์กฐํ•ฉ์„ ์ฐพ๊ธฐ ์œ„ํ•ด PPO(Proximal Policy Optimization)๋กœ ํ•™์Šตํ•˜์—ฌ AutoAugmentation์„ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๊ณ„์‚ฐ๋Ÿ‰์ด ๋งค์šฐ ๋งŽ๊ณ , ํƒ์ƒ‰ ๊ณต๊ฐ„์ด ์ปค ์‹œ๊ฐ„์ด ๋งค์šฐ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ๋„ ์ œํ•œ์ ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. AutoAugmentation์˜ ๊ณ„์‚ฐ ์†๋„๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์ œ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์˜ˆ๋กœ Population Based Augmentation, Fast AutoAugment, Faster AutoAugment ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. RandAugmentation ์ด์ „์˜ ๊ธฐ๋ฒ•๋“ค์ด ์ ํ•ฉํ•œ augmentation ๊ธฐ๋ฒ•์„ ์ฐพ๋Š” ๋ชจ๋ธ์ด๋ผ๋ฉด, RandAugmentation์€ ํŠน์ • ๋ชจ๋ธ์„ ์ฐพ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ batch๋งˆ๋‹ค ์ ์šฉํ•  augmentation ๋ฐฉ๋ฒ•์„ ๋žœ๋ค์œผ๋กœ ์ถ”์ถœํ•˜์—ฌ ์ ์šฉํ•˜๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ์„ฑ๋Šฅ์€ ๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ ํฐ ์ฐจ์ด๊ฐ€ ์—†์œผ๋‚˜ ์ฝ”๋“œ๊ฐ€ ๋‹จ์ˆœํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Reference A survey on Image Data Augmentation for Deep Learning Youtube|๋”ฅ๋Ÿฌ๋‹ ๋…ผ๋ฌธ ์ฝ๊ธฐ ๋ชจ์ž„ [DMQA Open Seminar] Data Augmentation 2) Pre-training, Transfer learning & Fine-tuning Transfer learning์˜ ํ•„์š”์„ฑ MNIST์™€ ๊ฐ™์€ ํ‘๋ฐฑ ์ด๋ฏธ์ง€๋ฅผ ์ธ์‹ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ตœ์†Œ 3๊ฐœ์˜ convolution layer์™€ 1๊ฐœ์˜ fully connected layer๊ฐ€ ํ•„์š”ํ•˜๋ฉฐ, ์ด๋ฅผ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ผ๋ฐ˜์ ์ธ 1๊ฐœ์˜ CPU์—์„œ๋Š” 1์‹œ๊ฐ„ ์ •๋„ ์†Œ์š”๋ฉ๋‹ˆ๋‹ค. CIFAR-10 ๋“ฑ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ณ ํ•ด์ƒ๋„ ์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€๋ฅผ ์ธ์‹ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ตœ์†Œ 5๊ฐœ์˜ convolution layer์™€ 2๊ฐœ์˜ fully connected layer๊ฐ€ ํ•„์š”ํ•˜๋ฉฐ, ์ด๋ฅผ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ผ๋ฐ˜์ ์ธ 1๊ฐœ์˜ CPU์—์„œ๋Š” ์ˆ˜ ๋ฐฑ~์ˆ˜์ฒœ ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค. ์š”์ƒˆ ์‚ฌ์šฉํ•˜๋Š” ํฐ ๋ฉ์น˜์˜ CNN์„ ํ•™์Šต ์‹œํ‚ค๋ ค๋ฉด ์—„์ฒญ๋‚˜๊ฒŒ ๋งŽ์€ ์‹œ๊ฐ„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ ํ•™์Šต๋˜์–ด ์žˆ๋Š” CNN ๋ชจ๋ธ, ์ฆ‰ Pre-trained CNN์„ ๊ฐ€์ ธ์™€์„œ ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ์— ๋งž๋„๋ก Fine-tuning (๋ฏธ์„ธ ์กฐ์ •) ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ๋งจ ๋ฐ‘๋ฐ”๋‹ฅ๋ถ€ํ„ฐ ํ•™์Šต ์‹œํ‚ค๋Š” ๊ฒƒ์— ๋น„ํ•ด ์†Œ์š” ์‹œ๊ฐ„์ด ํš๊ธฐ์ ์œผ๋กœ ์ค„์–ด๋“ญ๋‹ˆ๋‹ค! ์ด์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ Transfer learning (์ „์ด ํ•™์Šต)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Transfer learning์€ ์™œ ์ž‘๋™ํ• ๊นŒ? ๋‹ค๋Ÿ‰์˜ ๋™๋ฌผ ์‚ฌ์ง„์„ ์ฃผ๊ณ  ํ•™์Šต์‹œํ‚จ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž๋™์ฐจ๋‚˜ ์˜คํ† ๋ฐ”์ด ๋“ฑ์˜ ํƒˆ๊ฒƒ์„ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ๋Œ€์ƒ์ด ๋งค์šฐ ๋‹ฌ๋ผ ์ผ๊ฒฌ ๋ณด๊ธฐ์—๋Š” ์ž˜ ์ž‘๋™ํ•˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์ง€๋งŒ fine-tuning์„ ๊ฑฐ์น˜๋ฉด ๊ฝค ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์ค‘์š”ํ•œ ์„ฑ๊ฒฉ ์ค‘ ํ•˜๋‚˜๋Š” ๋ชจ๋ธ์˜ ์ดˆ๊ธฐ ์ธต์€ "์ผ๋ฐ˜์ ์ธ(general)" ํŠน์ง•์„ ์ถ”์ถœํ•˜๋„๋ก ํ•˜๋Š” ํ•™์Šต์ด ์ด๋ฃจ์–ด์ง€๋Š” ๋ฐ˜๋ฉด์—, ๋ชจ๋ธ์˜ ๋งˆ์ง€๋ง‰ ์ธต์— ๊ฐ€๊นŒ์›Œ์งˆ์ˆ˜๋ก ํŠน์ • ๋ฐ์ดํ„ฐ ์…‹ ๋˜๋Š” ํŠน์ • ๋ฌธ์ œ์—์„œ๋งŒ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋Š” "๊ตฌ์ฒด์ ์ธ(specific)" ํŠน์ง•์„ ์ถ”์ถœํ•ด ๋‚ด๋„๋ก ํ•˜๋Š” ๊ณ ๋„ํ™”๋œ ํ•™์Šต์ด ์ด๋ฃจ์–ด์ง„๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ pre-trained CNN์˜ ์ดˆ๊ธฐ ์ธต์€ ๋ชจ์„œ๋ฆฌ๋‚˜ ๊ฒฝ๊ณ„, ๊ณก์„  ๋“ฑ์„ ๊ฒ€์ถœํ•˜๋Š” ์ธต์ด๊ณ , ๋งˆ์ง€๋ง‰ ์ธต์€ ๋™๋ฌผ ๊ฐ๊ฐ์˜ ์„ธ์„ธํ•œ ํŠน์„ฑ์„ ์ถ”์ถœํ•˜๋Š” ์ธต์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ดˆ๊ธฐ ์ธต์€ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์…‹์˜ ์ด๋ฏธ์ง€๋“ค์„ ํ•™์Šตํ•  ๋•Œ๋„ ์žฌ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋งˆ์ง€๋ง‰ ์ธต์€ ์ƒˆ๋กœ์šด ๋ฌธ์ œ๋ฅผ ๋งž์ดํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ ํ•™์Šต์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Fine-tuning ์‹œ ๊ณ ๋ คํ•  ์  Fine-tuning์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ์กด์— ํ•™์Šต๋œ layer์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ํ•™์Šต์‹œ์ผœ, ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€, pre-trained CNN์ด ํ•™์Šตํ•œ ๋ฐ์ดํ„ฐ์™€ ์šฐ๋ฆฌ๊ฐ€ ํ•™์Šต ์‹œํ‚ค๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€, ๋ฐ์ดํ„ฐ์˜ ์–‘์€ ์–ผ๋งˆ์ธ์ง€์— ๋”ฐ๋ผ Fine-tuning ์‹œํ‚ฌ ์˜์—ญ ๋ฐ ์ •๋„๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค๋Š” ๊ฒƒ์ด์ฃ . ์ƒˆ๋กœ ํ›ˆ๋ จํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์ง€๋งŒ, original ๋ฐ์ดํ„ฐ์™€ ์œ ์‚ฌํ•  ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ ์–ด, ์ „์ฒด ๋„คํŠธ์›Œํฌ๋ฅผ fine-tuning ํ•˜๋Š” ๊ฒƒ์€ over-fitting์˜ ์œ„ํ—˜์ด ์žˆ๊ธฐ์— ํ•˜์ง€ ์•Š์Œ. ์ƒˆ๋กœ ํ•™์Šตํ•  ๋ฐ์ดํ„ฐ๋Š” original ๋ฐ์ดํ„ฐ์™€ ์œ ์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ข… classfier layer๋งŒ ํ•™์Šตํ•ด๋„ ์ž˜ ์ž‘๋™ํ•จ. ์ƒˆ๋กœ ํ›ˆ๋ จํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์œผ๋ฉฐ, original ๋ฐ์ดํ„ฐ์™€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ ์ด ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ ๊ณ , ๋ฐ์ดํ„ฐ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์—, ๋„คํŠธ์›Œํฌ์˜ ๋งˆ์ง€๋ง‰ classifier layer๋งŒ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์€ ์ข‹์ง€ ์•Š์Œ. ๋”ฐ๋ผ์„œ, ๋„คํŠธ์›Œํฌ ์ดˆ๊ธฐ ๋ถ€๋ถ„์˜ ํŠน์ • layer๋ฅผ ํ•™์Šต์‹œํ‚ค๋Š” ๊ฒŒ ์ข‹์Œ. ์ƒˆ๋กœ ํ›ˆ๋ จํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์œผ๋ฉฐ, original ๋ฐ์ดํ„ฐ์™€ ์œ ์‚ฌํ•  ๊ฒฝ์šฐ ์ƒˆ๋กœ ํ•™์Šตํ•  ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋งŽ๋‹ค๋Š” ๊ฒƒ์€, over-fitting์˜ ์œ„ํ—˜์ด ๋‚ฎ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ ์ „์ฒด layer๋ฅผ fine-tuning์„ ํ•˜๊ฑฐ๋‚˜ ๋งˆ์ง€๋ง‰ ๋ช‡ ๊ฐœ์˜ layer๋งŒ fine-tuning ํ•˜๊ฑฐ๋‚˜ ๋งˆ์ง€๋ง‰ ๋ช‡ ๊ฐœ์˜ layer๋ฅผ ๋‚ ๋ ค๋ฒ„๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Œ. ์ƒˆ๋กœ ํ›ˆ๋ จํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์ง€๋งŒ, original ๋ฐ์ดํ„ฐ์™€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—, ์•„์˜ˆ ์ƒˆ๋กœ์šด ConvNet์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ด ๊ฒฝ์šฐ์—๋„ transfer learning์˜ ํšจ์œจ์ด ๋” ์ข‹์Œ. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ „์ฒด ๋„คํŠธ์›Œํฌ๋ฅผ fine-tuning ํ•ด๋„ ๋จ. Transfer learning ๊ตฌํ˜„ ์˜ˆ์‹œ ์ƒˆ๋กœ ํ›ˆ๋ จํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์ง€๋งŒ, original ๋ฐ์ดํ„ฐ์™€ ์œ ์‚ฌํ•  ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ classifier layer๋งŒ fine-tuning ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์˜ˆ์‹œ๋กœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Pre-trained CNN์€ VGGnet์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. โ‘  tf.constant๋ฅผ ์ด์šฉํ•ด VGG์˜ convolution layer์™€ fully connected layr์˜ weight๊ณผ bias๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜จ๋‹ค. โ‘ก tf.Variable์„ ์ด์šฉํ•ด classifier layer๋Š” ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ์„ธํŒ…ํ•œ๋‹ค. โ‘ข ํ•ด๋‹น weight์™€ bias๋ฅผ ์ด์šฉํ•ด Transfer learning model์„ ๋งŒ๋“ ๋‹ค. โ‘ฃ Loss์™€ optimizer๋ฅผ ์„ค์ •ํ•˜๊ณ  ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด ํ•™์Šต์‹œํ‚จ๋‹ค. ์ „์ฒด ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ  Reference Transfer Learning(์ „ ์ด ํ•™์Šต)์ด๋ž€? 3) Under/Over-fitting ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• Overfitting Overfitting(๊ณผ๋Œ€์  ํ•ฉ)์ด๋ž€ ๋ชจ๋ธ์ด Train set์—์„œ๋Š” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚ด์ง€๋งŒ Validation set์—์„œ๋Š” ๋‚ฎ์€ ์„ฑ๋Šฅ์„ ๋‚ด๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ Train ๋ฐ์ดํ„ฐ์— ๋ชจ๋ธ์ด ๋„ˆ๋ฌด ์ ํ•ฉํ•˜๊ฒŒ ํ•™์Šต๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Train ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ค๊ฒŒ ๋˜๋ฉด ์ •ํ™•๋„๊ฐ€ ๋–จ์–ด์ง€๋Š” ํ˜„์ƒ์ž…๋‹ˆ๋‹ค. Overfitting ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• Overfitting์€ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋–จ์–ดํŠธ๋ฆฌ๋Š” ์ฃผ์š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ์–‘ ๋Š˜๋ฆฌ๊ธฐ ๋ชจ๋ธ์€ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ ์„์ˆ˜๋ก ํ•ด๋‹น ๋ฐ์ดํ„ฐ์˜ ํŠน์ง• ํŒจํ„ด์ด๋‚˜ ๋…ธ์ด์ฆˆ๊นŒ์ง€ ์•”๊ธฐํ•ด๋ฒ„๋ ค์„œ Overfitting์ด ๋  ํ™•๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๋Š˜๋ฆด์ˆ˜๋ก ๋ชจ๋ธ์€ ์ผ๋ฐ˜์ ์ธ ํŒจํ„ด์„ ํ• ์Šตํ•˜์—ฌ Overfitting์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๋Š˜๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” Data Augmentation ์ด๊ณณ์— ์„ค๋ช…์ด ๋˜์–ด์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 2. ๋ชจ๋ธ์˜ ๋ณต์žก๋„ ์ค„์ด๊ธฐ ์ธ๊ณต์ง€๋Šฅ ์‹ ๊ฒฝ๋ง์˜ ๋ณต์žก๋„๋Š” Hidden layer์˜ ์ˆ˜๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜ ๋“ฑ์œผ๋กœ ๊ฒฐ์ •์ด ๋ฉ๋‹ˆ๋‹ค. Overfitting ํ˜„์ƒ์ด ํฌ์ฐฉ๋˜์—ˆ์„ ๋•Œ, ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ ๊ฐ€์ง€ ์กฐ์น˜๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋ณต์žก๋„๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. 3. Dropout ์‚ฌ์šฉํ•˜๊ธฐ Dropout์€ ํ•™์Šต ๊ณผ์ •์—์„œ ์‹ ๊ฒฝ๋ง์˜ ์ผ๋ถ€๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ Dropout์˜ ๋น„์œจ์„ 0.5๋กœ ํ•œ๋‹ค๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ํ•™์Šต ๊ณผ์ •๋งˆ๋‹ค ๋žœ๋ค์œผ๋กœ ์ ˆ๋ฐ˜์˜ ๋‰ด๋Ÿฐ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ ˆ๋ฐ˜๋งŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Dropout์€ ์‹ ๊ฒฝ๋ง ํ•™์Šต ์‹œ์—๋งŒ ์‚ฌ์šฉํ•˜๊ณ , ์˜ˆ์ธก ์‹œ์—๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ํ•™์Šต ์‹œ์— ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ํŠน์ • ๋‰ด๋Ÿฐ ๋˜๋Š” ํŠน์ • ์กฐํ•ฉ์— ๋„ˆ๋ฌด ์˜์กด์ ์ด๊ฒŒ ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ด ์ฃผ๊ณ , ๋งค๋ฒˆ ๋žœ๋ค ์„ ํƒ์œผ๋กœ ๋‰ด๋Ÿฐ๋“ค์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ์‹ ๊ฒฝ๋ง๋“ค์„ ์•™์ƒ๋ธ” ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๋‚ด์–ด ๊ณผ์ ํ•ฉ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. 4. ์ถœ๋ ฅ์ธต ์ง์ „ ์€๋‹‰์ธต์˜ ๋…ธ๋“œ ์ˆ˜ ์ค„์ด๊ธฐ ํ†ต๊ณ„ํ•™์—์„œ Overfitting์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์“ธ๋ฐ์—†๋Š” ๋ณ€์ˆ˜๋ฅผ ์ œ๊ฑฐํ•ด์„œ ์ž…๋ ฅ ๋ณ€์ˆ˜์˜ ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ธ๋ฐ, ํ†ต๊ณ„ํ•™ ๊ด€์ ์—์„œ ์ถœ๋ ฅ์ธต ์ง์ „ ์€๋‹‰์ธต ๋…ธ๋“œ ์ˆ˜๋Š” ์„ค๋ช… ๋ณ€์ˆ˜์˜ ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜๋ฏธ ์žˆ๋Š” ์„ค๋ช… ๋ณ€์ˆ˜๋“ค์„ ๋‚จ๊ธฐ๊ธฐ ์œ„ํ•ด ์ถœ๋ ฅ ์ง์ „ ๋…ธ๋“œ๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 5. Batch Normalization ๋‰ด๋Ÿด ๋„คํŠธ์›Œํฌ์—์„œ ๊ฐ ํ™œ์„ฑํ•จ์ˆ˜์˜ ๋ฏธ๋ถ„ ๊ฐ’์€ ์—ญ์ „ํŒŒ ๊ณผ์ • ์†์—์„œ ๊ณ„์† ๊ณฑํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”ํ•œ ๋ถ€๋ถ„ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์‹œ๊ทธ ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ์ผ์ • ์ˆ˜์ค€ ์ด์ƒ ํ˜น์€ ์ดํ•˜์˜ ๊ฐ’์ด ์ž…๋ ฅ๋˜์—ˆ์„ ๋•Œ ๋ฏธ๋ถ„ ๊ฐ’์ด 0์— ๊ฐ€๊นŒ์›Œ์ง€๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋•Œ Gradient Vanishing ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์—…๋ฐ์ดํŠธ๊ฐ€ ๊ฑฐ์˜ ์ผ์–ด๋‚˜์ง€ ์•Š๊ณ  ์ˆ˜๋ ด ์†๋„๋„ ์•„์ฃผ ๋Š๋ฆฌ๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ ํ™”์— ์‹คํŒจํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Batch Normalization์ด ์ด์šฉ๋ฉ๋‹ˆ๋‹ค. mini batch ๋ณ„๋กœ ๋ถ„์‚ฐ๊ณผ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ตฌํ•ด ๋ถ„ํฌ๋ฅผ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. Under Fitting Underfitting์€ ์ด๋ฏธ ์žˆ๋Š” Train set๋„ ํ•™์Šต์„ ํ•˜์ง€ ๋ชปํ•œ ์ƒํƒœ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์•„์ง ํ•™์Šต์ด ๋œ ๋œ ๋ชจ๋ธ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ํŽธํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋Š” ํ•™์Šต ๋ฐ˜๋ณต ํšŸ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ์ ๊ณ , ๋ฐ์ดํ„ฐ์˜ ํŠน์„ฑ์— ๋น„ํ•ด ๋ชจ๋ธ์ด ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฐ์ดํ„ฐ์–‘์ด ๋„ˆ๋ฌด ์ ์€ ๋ฌธ์ œ๋„ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Reference ๋จธ์‹ ๋Ÿฌ๋‹ - ๊ณผ๋Œ€์  ํ•ฉ(overfitting)๊ณผ ๊ณผ์†Œ ์ ํ•ฉ(underfitting), ์ •๊ทœํ™” ๋ชจ๋ธ ํŠœ๋‹ ํ•˜๋Š” ๋ฐฉ๋ฒ• - ๊ณผ๋Œ€ ์ ํ•ฉ๊ณผ ๊ณผ์†Œ ์ ํ•ฉ ๊ณผ์ ํ•ฉ๊ณผ ๊ณผ์†Œ ์ ํ•ฉ (Overfitting & Underfitting) (4) ์„ค๋ช… ๊ฐ€๋Šฅํ•œ AI Neural Network๋Š” ์„œ๋กœ ๋ณต์žกํ•˜๊ฒŒ ์—ฐ๊ฒฐ๋œ ์ˆ˜๋ฐฑ๋งŒ ๊ฐœ ์ด์ƒ์˜ parameter๊ฐ€ ๋น„์„ ํ˜•์œผ๋กœ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ๊ตฌ์กฐ๋กœ, ์‚ฌ๋žŒ์ด ๊ทธ ๋งŽ์€ parameter๋ฅผ ์ง์ ‘ ๊ณ„์‚ฐํ•˜๊ณ  ์˜๋ฏธ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๊ณ  back propagation ๋•์— ๊ฐ„์‹ ํžˆ parameter update๋งŒ ๊ฐ€๋Šฅํ•œ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๋ณต์žกํ•œ ๊ตฌ์กฐ ๋•์— ์„ฑ๋Šฅ์€ ๊ธฐ์กด ๊ธฐ๊ณ„ํ•™์Šต๋ณด๋‹ค๋„ ์›”๋“ฑํžˆ ๋†’์•„์กŒ์ง€๋งŒ Neural Network๊ฐ€ ์™œ ๊ทธ๋Ÿฐ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ–ˆ๋Š”์ง€๋Š” ์•Œ ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ”ํžˆ Neural Network๋ฅผ Black Box๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ๋ฌธ์— ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด Neural Network๊ฐ€ ์™œ ์ž‘๋™ํ•˜๋Š”์ง€, ๋„๋Œ€์ฒด ์™œ ๊ทธ๋Ÿฐ ๊ฒฐ๋ก ์— ๋„๋‹ฌํ•œ ๊ฒƒ์ธ์ง€ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๋งŽ์€ ๋…ธ๋ ฅ์„ ํ•ด์™”์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋…ธ๋ ฅ๋“ค์— ๋Œ€ํ•ด ์กฐ์‚ฌํ•ด ๋ณด๊ธฐ ์ „์•  ์™œ ์šฐ๋ฆฌ๊ฐ€ Neural Network๋ฅผ ์„ค๋ช…ํ•ด์•ผ ํ•˜๋Š”์ง€ ์‚ฌ๋ก€๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. โ–ท A ๋ณ‘์›์ด ์ตœ๊ทผ ์•”์„ ์ง„๋‹จํ•˜๋Š” AI๋ฅผ ๋„์ž…ํ–ˆ๋‹ค๊ณ  ํ•˜์ž. B ์”จ๋Š” ๋ฐฐํƒˆ์ด ๋‚œ ๊ฒƒ ๊ฐ™์•„์„œ ๋ณ‘์›์— ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ฐ€ AI๋กœ๋ถ€ํ„ฐ ์•” ํŒ์ •์„ ๋ฐ›์•˜๋‹ค. ์–ด๋–ค ์ฆ์ƒ ๋•Œ๋ฌธ์— ์•”์ด๋ผ๊ณ  ์ง„๋‹จํ–ˆ๋Š”์ง€๋Š” ์„ค๋ช…ํ•˜์ง€ ์•Š๊ณ  ์ตœ์‹  AI ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋น…๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์˜ˆ์ธกํ–ˆ๋‹ค๊ณ  ํ•œ๋‹ค. B ์”จ๋Š” ์ •๋ฐ€๊ฒ€์‚ฌ๋ฅผ ๋ฐ›์•„์•ผ ํ• ์ง€ ๊ณ ๋ฏผ์— ๋น ์ง„๋‹ค. โ–ท ์•ฝ 18,000๋ช…์˜ ๋ฒ”์ฃ„์ž๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ํ–ฅํ›„ 2๋…„ ๋‚ด<NAME> ๊ฐ€๋Šฅ์„ฑ์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ๋ฒ”์ฃ„ ์˜ˆ์ธก ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ–ˆ๋Š”๋ฐ ํ‘์ธ์˜<NAME> ๊ฐ€๋Šฅ์„ฑ์„ ๋ฐฑ์ธ๋ณด๋‹ค 45% ๋†’๊ฒŒ ์˜ˆ์ธกํ–ˆ๋‹ค. ํ‘์ธ์˜<NAME> ๊ฐ€๋Šฅ์„ ์™œ ๋” ๋†’๊ฒŒ ์˜ˆ์ธกํ–ˆ๋Š”์ง€๋Š” ์•Œ ์ˆ˜ ์—†์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ ๋™์ผ ๊ธฐ๊ฐ„์˜<NAME>๋ฅ ์„ ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ ๋ฐฑ์ธ์˜<NAME>๋ฅ ์ด ํ‘์ธ๋ณด๋‹ค ๋†’์•˜๋‹ค. ๋˜ํ•œ Neural Network์ด ์„ค๋ช…ํ•จ์œผ๋กœ์จ ํ•ด๋‹น ๋ชจ๋ธ์„ ํ†ต์ œํ•˜๊ณ , ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๋Š” ๋ฐ ๋„์›€์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Reference ์„ค๋ช… ๊ฐ€๋Šฅ ์ธ๊ณต์ง€๋Šฅ์ด๋ž€? (A Survey on XAI) Peeking Inside the Black-Box: A Survey on Explainable Artificial Intelligence (XAI) A survey of methods for explaining black box models 1) Visualizing the layers of DNN ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์€ ์–ด๋–ป๊ฒŒ ํ•ด์„œ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋ฅผ<NAME>๊ฒŒ ๋˜์—ˆ๋Š”์ง€ ํ•ด์„ํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ๋น„ํŒ๋ฐ›๊ณค ํ•ฉ๋‹ˆ๋‹ค. (Black box๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ์ฃ .) ์ด๋ฏธ์ง€๋ฅผ ๋‹ค๋ฃจ๋Š” ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ Layer ์ค‘๊ฐ„์ค‘๊ฐ„ ์„ž์ธ non-linear activation function์œผ๋กœ ์ธํ•ด ๋”๋”์šฑ ํ•ด์„์ด ํž˜๋“  ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•ด ๋ณด๊ณ ์ž ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ์‹œ๊ฐํ™” (visualization) ํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•๋“ค์ด ์ œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์‹œ๊ฐํ™”(visualization) ๋ฐฉ๋ฒ•์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. Feature vector visualization using t-SNE/PCA Activation Visualization Maximally Activating Images / Patches Maximization by Optimization & Deep Dream Occlusion Feature vector visualization using t-SNE/PCA ์ด๋ฏธ์ง€๋ฅผ ๋ถ„๋ฅ˜ ๋„คํŠธ์›Œํฌ์— ๋„ฃ๊ณ  ๋‚˜์˜ค๋Š” ๋งˆ์ง€๋ง‰ Layer(๋ถ„๋ฅ˜๋˜๊ธฐ ์ „ ๋งˆ์ง€๋ง‰ Layer)์˜ ์ถœ๋ ฅ๊ฐ’์€ ์ž…๋ ฅ์— ๋น„ํ•ด ์ถ•์†Œ๋œ ์ฐจ์›(Dimension)๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ์ถ•์†Œ๋œ ๊ฐ’๋“ค์„ Feature vector๋ผ๊ณ  ํ•˜๋ฉฐ ์ด๋“ค์€ ์ด๋ฏธ์ง€์˜ ๊ฐ•๋ ฅํ•œ ํŠน์ง•๋“ค์„ ํ‘œํ˜„ํ•œ ์ƒˆ๋กœ์šด ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋“ค์—์„œ ์ถ”์ถœ๋œ Feature vector๋“ค๋„ Dims๊ฐ€ ๋งŽ์•„ Visualization์„ ๋ฐ”๋กœ ํ•˜๊ธฐ๋Š” ํž˜๋“ค๊ธฐ ๋•Œ๋ฌธ์— PCA/t-SNE ๋“ฑ์„ ์ด์šฉํ•ด 2,3 ์ฐจ์›์œผ๋กœ ์ถ•์†Œํ•˜๊ณ , Reduction๋œ Feature vector๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋“ค์„ ์ขŒํ‘œํ‰๋ฉด์— ์‹œ๊ฐํ™”ํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ™์€ Visualization์„ ํ†ตํ•ด์„œ ์ด์›ƒ ์ด๋ฏธ์ง€๋“ค์„ ํ™•์ธํ•˜์—ฌ Feature vector๊ฐ€ ์ž˜ ์ถ”์ถœ๋˜์—ˆ๋Š”์ง€ ์‹œ๊ฐ์ ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Activation Visualization ์ด๋ฏธ์ง€๋ฅผ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์— ์ œ์‹œํ•˜๋ฉด Intermediate layer๋ฅผ ํ†ต๊ณผํ•˜๋ฉด์„œ ์ƒ์„ฑ๋œ feature map ๊ฐ๊ฐ์„ ๋“ค์—ฌ๋‹ค๋ณด๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๋จธ์‹ ๋Ÿฌ๋‹ ๋ชจ๋ธ์—์„œ feature๋ฅผ โ€˜๋“ค์—ฌ๋‹ค๋ณธ๋‹คโ€™ ํ•จ์€, feature๋กœ ๊ณ„์‚ฐ๋œ ๊ฐ’์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜๊ณ  ํ†ต๊ณ„์  ๋ถ„ํฌ ๋“ฑ์„ ์กฐ์‚ฌํ•˜๋Š” ๊ฒƒ์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ปจ๋ณผ๋ฃจ์…˜ ์‹ ๊ฒฝ๋ง์˜ feature map์€ ๊ทธ ํŠน์œ ์˜ 2์ฐจ์›์  ๊ตฌ์กฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๋งˆ์น˜ ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ ๊ทธ๋ ค๋ณผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ์˜ ๊ฐ feature map์ด โ€˜ํ™œ์„ฑํ™”๋œ(activation)โ€™ ์ •๋„๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” Activation map์€ ์–ด๋–ค ๋‰ด๋Ÿฐ(๋…ธ๋“œ)์—์„œ ์–ด๋–ค ํŠน์ง•์„ ์ถ”์ถœํ–ˆ๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด ์ด์šฉํ•ด ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์‚ฌ์ง„์€ Conv5 Layer์„ ํ†ต๊ณผํ•˜๊ณ  ๋‚˜์˜จ Feature map์ธ๋ฐ, ํŠน์ • ๋…ธ๋“œ์˜ Activation ๊ฐ’์ด ๋งค์šฐ ๋ฐ๊ฒŒ ๋น›๋‚˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋…ธ๋“œ๋ฅผ ํ™•๋Œ€ํ•ด input image์™€ ๋น„๊ตํ•ด ๋ณด๋ฉด "์‚ฌ๋žŒ์˜ ์–ผ๊ตด"์ด๋ผ๋Š” ํŠน์ง•์„ ์žก์•„๋‚ธ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Maximally Activating Images / Patches Activation Visualization ์ดํ›„๋กœ, ๊ฐ feature map์ด ์ปค๋ฒ„ํ•˜๋Š” ์‹œ๊ฐ์  ํŠน์ง•์ด ์ •ํ™•ํžˆ ๋ฌด์—‡์ธ์ง€ ๋”์šฑ ํšจ๊ณผ์ ์œผ๋กœ ์ฐพ๊ณ  ๊ฐ€์‹œํ™”ํ•˜๊ณ ์ž ํ•˜๋Š” ์‹œ๋„๊ฐ€ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋Š˜์–ด๋‚ฌ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘ ํ•˜๋‚˜๋กœ Maximally Activating Images/Patches๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • Intermediate feature map์„ ์ตœ๋Œ€๋กœ ํ™œ์„ฑํ™”(Maximum activation) ํ•˜๋Š” input image๋ฅผ ์ฐพ๋Š” ๊ฒƒ์„ Maximmaly Activating Imge ์ฐพ๋Š”๋‹ค๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต์€ ์ƒ์œ„ ๋ช‡ ๊ฐœ์˜ Maximally Activating Images๋ฅผ ์ผ๋‹จ ๋ฝ‘์•„๋‚ธ ๋’ค ์ด๋“ค ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ Activation Visualization์„ ์ถ”๊ฐ€๋กœ ์ˆ˜ํ–‰ํ•จ์œผ๋กœ์จ, ํ•ด๋‹น feature map์ด Maximally Activating Images ์ƒ์˜ ์–ด๋Š ๋ถ€๋ถ„์— ์ดˆ์ ์„ ๋งž์ถ”๋Š”์ง€ ๋ณด๋‹ค ๋ฉด๋ฐ€ํ•˜๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ  ์ด ์˜์—ญ์„ Maximally Activating Patches๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ด๋ฏธ์ง€๋ฅผ ์˜ˆ๋กœ ๋“ค๋ฉด Conv5 layer๋ฅผ ํ†ตํ•ด์„œ ์ƒ์„ฑ๋œ 128๊ฐœ์˜ ์ฑ„๋„ ์ค‘ 17๋ฒˆ์งธ ์ฑ„๋„์„ ๋ชฉํ‘œ๋กœ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ฑ„๋„์„ ํ†ต๊ณผํ•ด ์ƒ์„ฑ๋œ filter map์„ Maximum activation ์‹œํ‚จ ์˜์—ญ, ์ฆ‰ Maximally Activating Patches๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋ˆˆ, ์ฝ”, ์ด๋‹ˆ์…œ ๋“ฑ input image ์ƒ ํŠน์ • ์˜์—ญ์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Maximization by Optimization & Deep Dream Maximally Activating Images / Patches๋Š” feature map ๋“ฑ์ด ์ปค๋ฒ„ํ•˜๋Š” ์‹œ๊ฐ์  ํŠน์ง•์„ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์ˆ˜์˜ ๋ฐ์ดํ„ฐ ์…‹์„ ํ•„์š”๋กœ ํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ˜„์žฌ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ์ด๋ฏธ์ง€ ์ˆ˜๊ฐ€ ๋งค์šฐ ์ ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”? ๋ณด์œ ํ•œ ๋ฐ์ดํ„ฐ ์…‹์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  feature map ๋“ฑ์ด ์ปค๋ฒ„ํ•˜๋Š” ์‹œ๊ฐ์  ํŠน์ง•์„ ์ข€ ๋” ์ง์ ‘์ ์œผ๋กœ ์กฐ์‚ฌํ•˜๊ณ ์ž, gradient ascent(๊ฒฝ์‚ฌ ์ƒ์Šน ๋ฒ•)์— ๊ธฐ๋ฐ˜ํ•œ optimization์„ ํ†ตํ•ด ํƒ€๊นƒ feature map์„ ์ตœ๋Œ€๋กœ ํ™œ์„ฑํ™”ํ•˜๋Š” input image๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ์ ‘๊ทผ์ด ์‹œ๋„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์„ Maximization by Optimization๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋žœ๋ค ๋…ธ์ด์ฆˆ(random noise) ํ˜•ํƒœ์˜ ์ด๋ฏธ์ง€์—์„œ ์ถœ๋ฐœํ•˜์—ฌ, ํŠน์ •ํ•œ ํ•˜๋‚˜์˜ feature map F์„ ํƒ€๊นƒ์œผ๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. feature map F์˜ ํ˜„์žฌ ์ด๋ฏธ์ง€ X์— ๋Œ€ํ•œ gradient (dF/dX)๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ์ด๋ฅผ ํ˜„์žฌ ์ด๋ฏธ์ง€์— ๋”ํ•ด์ฃผ์–ด, ๊ธฐ์กด๋ณด๋‹ค ํ•ด๋‹น feature map์„ ๋” ๊ฐ•ํ•˜๊ฒŒ ํ™œ์„ฑํ™”์‹œํ‚ค๋Š” ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€ X1์„ ์–ป์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ถฉ๋ถ„ํžˆ ๋ฐ˜๋ณตํ•˜์—ฌ ํ•ด๋‹น feature map์„ โ€˜์ตœ๋Œ€๋กœ ํ™œ์„ฑํ™”์‹œํ‚ค๋Š” ์ด๋ฏธ์ง€โ€™๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์‹œ ๊ทธ๋ฆผ๋“ค์€ ๊ฐ๊ธฐ ๋‹ค๋ฅธ feature map์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ Maximization by Optimization ์‹œํ–‰ํ•œ ๊ฒฐ๊ณผ๋“ค์ž…๋‹ˆ๋‹ค. ๋Œ€์ฒด๋กœ ์•ž์ชฝ layer์— ์œ„์น˜ํ•œ feature map๋“ค์˜ ๊ฒฝ์šฐ ๋‹จ์ˆœํ•˜๊ณ  ๋ฐ˜๋ณต์ ์ธ ํŒจํ„ด(edges, textures)์„ ์ปค๋ฒ„ํ•˜๋Š” ๊ฒฝํ–ฅ์„ ๋ณด์ด๋ฉฐ, ๋’ค์ชฝ layer์— ์œ„์น˜ํ•œ feature map๋“ค์˜ ๊ฒฝ์šฐ ๊ทธ๋ณด๋‹ค๋Š” ์ข€ ๋” ๋ณต์žกํ•œ ๋ฌด๋Šฌ, ๋ชจ์ข…์˜ ์‚ฌ๋ฌผ์˜ ์ผ๋ถ€๋ถ„ ๋˜๋Š” ์ „์ฒด๋ฅผ ์ปค๋ฒ„ํ•˜๋Š” ๊ฒฝํ–ฅ์„ ๋ณด์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Maximization by Optimization์˜ ํƒ€๊นƒ์„ feature map ๋Œ€์‹  layer๋กœ ์„ค์ •ํ•  ๊ฒฝ์šฐ, ์•„๋ž˜์˜ ์˜ˆ์‹œ ๊ทธ๋ฆผ๋“ค๊ณผ ๊ฐ™์ด ์ƒ๋‹นํžˆ ๋“œ๋ผ๋งˆํ‹ฑํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์ด ๋งˆ์น˜ ๊ฟˆ์†์—์„œ๋งŒ ๋“ฑ์žฅํ•  ๊ฒƒ ๊ฐ™์€ ์ƒ์†Œํ•œ ์ธ์ƒ์„ ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์—ฐ๊ตฌ์ž๋“ค์€ ์—ฌ๊ธฐ์— โ€˜DeepDreamโ€˜์ด๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์˜€์Šต๋‹ˆ๋‹ค. ํ•ด์„ ๊ฐ€๋Šฅ์„ฑ์˜ ์ธก๋ฉด์—์„œ ๋ดค์„ ๋• feature map ๊ฐ๊ฐ์— ๋Œ€ํ•œ ๊ด€์ฐฐ ๊ฒฐ๊ณผ์— ๋น„ํ•ด ๋‹ค์†Œ ๋‚œํ•ดํ•œ ๋“ฏํ•œ ํŠน์ง•์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์‚ฌ์ง„์€ Imagenet์˜ ๋ฐ์ดํ„ฐ ์…‹๊ณผ Inception model์„ ๋ฐ”ํƒ•์œผ๋กœ ํ•˜์—ฌ DeepDream ๋ชจ๋ธ์„ ์ƒ์„ฑํ•œ ํ›„ ์ž„์˜์˜ image๋ฅผ ๋„ฃ์–ด ์ƒ์„ฑํ•œ ์‚ฌ์ง„์ž…๋‹ˆ๋‹ค. ์ž์„ธํžˆ ๋ณด๋ฉด ๊ธฐ์กด ์ด๋ฏธ์ง€์— ๊ฐœ๊ฐ€ ์œตํ•ฉ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Occlusion ๋จผ์ € ์›๋ณธ ์ด๋ฏธ์ง€์˜ ์ผ์ • ๋ถ€๋ถ„์„ Occlusion(ํ์ƒ‰) ์‹œํ‚ต๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ํŠน์ • ๋ถ€๋ถ„์„ ๊ฐ€๋ฆฐ๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ ๋’ค์— ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ต๊ณผ์‹œ์ผœ์„œ Predict score๊ฐ€ ํฌ๊ฒŒ ๋–จ์–ด์ง„๋‹ค๋ฉด ์•ž์„œ ์šฐ๋ฆฌ๊ฐ€ ํ์ƒ‰์‹œํ‚จ ๋ถ€๋ถ„์ด ์ค‘์š”ํ•œ ์˜์—ญ์ž„์„ ์ง๊ด€์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (๋ณ€ํ™”์œจ์ด ํด์ˆ˜๋ก ๋”์šฑ๋” ์ค‘์š”ํ•œ ๋ถ€๋ถ„์œผ๋กœ ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค). ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ด๋ฏธ์ง€์˜ ๋ชจ๋“  ์˜์—ญ์„ ๋ฐ˜๋ณต, Score๊ฐ€ ๋ณ€ํ•˜๋Š” ์ •๋„๋ฅผ Heat map์œผ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. (์•„๋ž˜ ์ด๋ฏธ์ง€์—์„œ ๋ถ‰์€ ์˜์—ญ์ด Predict score๊ฐ€ ํฌ๊ฒŒ ๋–จ์–ด์ง„ ์˜์—ญ์ž…๋‹ˆ๋‹ค.) ํ•ด๋‹น ๊ณผ์ •์„ ํ†ตํ•ด์„œ ๋ถ„๋ฅ˜์— ๊ฒฐ์ •์  ์˜์—ญ์„ ๋ฏธ์น˜๋Š” Patch๋ฅผ Original image ์˜์—ญ์—์„œ ์‹œ๊ฐํ™” ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ด€๋ จ ์ •๋ณด ๋ฐ Reference https://towardsdatascience.com/visualization-attention-part-3-84a43958e48b ๋ธ”๋กœ๊ทธ | CS231n 12๊ฐ•. Visualizing and Understanding Stanford University School of Engineering | Lecture 2 | Image Classification Stanford University School of Engineering | Lecture 12 | Visualizing and Understanding INTERPRETABLE MACHINE LEARNING ๊ฐœ์š”: (2) ์ด๋ฏธ์ง€ ์ธ์‹ ๋ฌธ์ œ์—์„œ์˜ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์ฃผ์š” ํ•ด์„ ๋ฐฉ๋ฒ• 2) CAM, Grad CAM CAM (Class Activation Map) ๋ณดํ†ต CNN์˜ ๊ตฌ์กฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด, Input - Conv Layers - FC Layers์œผ๋กœ ์ด๋ฃจ์–ด์กŒ์Šต๋‹ˆ๋‹ค. CNN์˜ ๋งˆ์ง€๋ง‰ ๋ ˆ์ด์–ด๋ฅผ FC-Layer๋กœ Flatten ์‹œํ‚ค๋ฉด ๊ฐ ํ”ฝ์…€๋“ค์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ์žƒ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Classification์˜ ์ •ํ™•๋„๊ฐ€ ์•„๋ฌด๋ฆฌ ๋†’๋”๋ผ๋„, ์šฐ๋ฆฌ๋Š” ๊ทธ CNN ์ด ๋ฌด์—‡์„ ๋ณด๊ณ  ํŠน์ • class๋กœ ๋ถ„๋ฅ˜ํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. 2016๋…„ ๊ณต๊ฐœ๋œ ๋…ผ๋ฌธ Learning Deep Features for Discriminative Localization์—์„œ๋Š” FC Layer ๋Œ€์‹ ์—, GAP (Global Average Pooling) ์„ ์ ์šฉํ•˜๋ฉด ํŠน์ • ํด๋ž˜์Šค ์ด๋ฏธ์ง€์˜ Heat Map ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ์ด Heat Map ์„ ํ†ตํ•ด CNN ์ด ์–ด๋–ป๊ฒŒ ๊ทธ ์ด๋ฏธ์ง€๋ฅผ ํŠน์ • ํด๋ž˜์Šค๋กœ ์˜ˆ์ธกํ–ˆ๋Š”์ง€๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ฃผ์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. GAP (Global Average Pooling) Global Average Pooling (GAP) layer์„ ํ†ตํ•ด ๊ฐ๊ฐ์˜ feature map๋งˆ๋‹ค Global average pooling (๊ฐ feature map์— ํฌํ•จ๋œ ๋ชจ๋“  ์›์†Œ ๊ฐ’์„ ํ‰๊ท ํ•จ)์„ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ GAP layer๋กœ ๋“ค์–ด์˜ค๋Š” feature map์˜ channel ์ˆ˜์™€ ๋™์ผํ•œ ๊ธธ์ด์˜ ๋ฒกํ„ฐ๋ฅผ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. (์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ๋ฒกํ„ฐ์˜ ๊ธธ์ด๊ฐ€ 4๊ฐ€ ๋˜๊ฒ ์ฃ .) GAP ๋’ค์—๋Š” FC layer๊ฐ€ ํ•˜๋‚˜ ๋ถ™์–ด์žˆ๊ณ  GAP์—์„œ ์ถœ๋ ฅํ•œ ๋ฒกํ„ฐ๋ฅผ Input์œผ๋กœ ์ค๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ๋Š” GAP์„ ํ†ตํ•ด ๊ธธ์ด 6์ธ ๋ฒกํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ๊ณ  FC layer๋ฅผ ํ†ต๊ณผ์‹œ์ผœ ํ† ๋ฅด/์•„์ด์–ธ๋งจ/์ŠคํŒŒ์ด๋”๋งจ/์บกํ‹ด ์•„๋ฉ”๋ฆฌ์นด ์ค‘ ํ•˜๋‚˜๋กœ Input image๋ฅผ ๋ถ„๋ฅ˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ FC layer์˜ weight๋Š” ํ•™์Šต์„ ํ†ตํ•ด์„œ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น weight 1~6์„ ๊ฐ feature map์— ๊ณฑํ•œ ๋’ค ๊ฐ pixel ๋ณ„๋กœ ๋”ํ•ด์ฃผ์–ด ์ตœ์ข… heat map์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. CAM์˜ ๊ฒฐ๊ณผ ๊ต‰์žฅํžˆ ๋‹จ์ˆœํ•œ ๊ตฌ์กฐ์—ฌ ์–ด์„œ ์ด๊ฒŒ ์ •๋ง ์ž‘๋™ํ•˜๋‚˜ ์‹ถ์ง€๋งŒ CAM์„ ํ†ตํ•ด ์–ป์–ด๋‚ธ Heat map ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๊ฝค๋‚˜ ๊ทธ๋Ÿด ๋“ฏํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์žˆ๋Š” ์˜์—ญ์ด ๋นจ๊ฐ›๊ฒŒ ํ‘œ์‹œ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์ฃ . cleaning the floor, cooking ๊ฐ™์ด ๋™์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ด๋ฏธ์ง€๋„ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ž˜ ์ง‘์–ด๋‚ด๊ณ  ์žˆ๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ CAM์€ ์™œ ์ž‘๋™์„ ํ•˜๋Š” ๊ฒƒ์ผ๊นŒ์š”? ๋งˆ์ง€๋ง‰ convolution layer๋ฅผ ํ†ต๊ณผํ•œ feature map์€ input image์˜ ์ „์ฒด ๋‚ด์šฉ์„ ํ•จ์ถ•ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰ Feature map์ด ์•„๋‹Œ ์ค‘๊ฐ„์— ์œ„์น˜ํ•œ featue map์—์„œ๋Š” CAM์„ ํ†ตํ•ด Heatmap์„ ์ถ”์ถœํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. CAM์˜ ๋‹จ์  CAM ๊ตฌ์กฐ์˜ ๋‹จ์ ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. CAM์˜ ๋‹จ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด Grad-CAM์ด 2017๋…„ ๋“ฑ์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. - Global average pooling layer๋ฅผ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•˜๊ณ  ๋’ค์—๋Š” FC layer๊ฐ€ ๋ถ™์–ด์žˆ์Œ. - ํ•ด๋‹น FC layer์˜ weight๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ํ•™์Šต์„ ์‹œ์ผœ์•ผ ํ•จ. - ๋งˆ์ง€๋ง‰ convolution layer๋ฅผ ํ†ต๊ณผํ•ด ๋‚˜์˜จ feature map์— ๋Œ€ํ•ด์„œ๋งŒ CAM์„ ํ†ตํ•ด Heat map ์ถ”์ถœ์ด ๊ฐ€๋Šฅํ•จ Grad-CAM CAM์˜ ๊ตฌ์กฐ Grad-CAM์˜ ๊ตฌ์กฐ Grad-CAM์€ CAM๊ณผ ๋ฌด์—‡์ด ๋‹ค๋ฅผ๊นŒ์š”? - ๊ธฐ์กด CNN ๋ชจ๋ธ ๊ตฌ์กฐ์˜ ๋ณ€ํ™” ์—†์Œ. ์ฆ‰, Global average pooling ์—†์ด FC layer๊ฐ€ ๋‘ ๊ฐœ ์กด์žฌ - ๊ธฐ์กด CNN ๋ชจ๋ธ์˜ ์žฌํ•™์Šต์ด ํ•„์š” ์—†์Œ. ๊ฐ Feature map์— ๊ณฑํ•ด์ค„ weight๋ฅผ ํ•™์Šต์ด ์•„๋‹Œ ๋ฏธ๋ถ„(gradient)์„ ํ†ตํ•ด ๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ k=6๊ฐœ์˜ feature map์„ ์ด์šฉํ•ด y=2, ์•„์ด์–ธ๋งจ์œผ๋กœ ๋ถ„๋ฅ˜ํ•œ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. class c์— ๋Œ€ํ•œ ์ ์ˆ˜ y_c (before the softmax)์„ ๊ฐ ์›์†Œ๋กœ ๋ฏธ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. (back propagation ํ•˜๋“ฏ์ด ๋ง์ด์ฃ .) ์ด ๋ฏธ๋ถ„ ๊ฐ’์€ ๊ฐ Feature map์˜ ์›์†Œ๊ฐ€ ํŠน์ • class์— ์ฃผ๋Š” ์˜ํ–ฅ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ feature map์— ํฌํ•จ๋œ ๋ชจ๋“  ์›์†Œ์˜ ๋ฏธ๋ถ„ ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•˜์—ฌ neuron importance weight, a๋ฅผ ๊ตฌํ•˜๋ฉด, ์ด a๋Š” ํ•ด๋‹น feature map์ด ํŠน์ • class์— ์ฃผ๋Š” ์˜ํ–ฅ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. neuron importance weight, a์™€ ๊ฐ k ๊ฐœ์˜ Feature map์„ ๊ณฑํ•˜์—ฌ weight sum of Feature map์„ ๊ตฌํ•จ โ†’ ReLU๋ฅผ ์ทจํ•˜์—ฌ ์ตœ์ข… Grad-CAM์— ์˜ํ•œ Heatmap์ด ์ถœ๋ ฅ ReLU๋ฅผ ์ทจํ•œ ์ด์œ ๋Š” ์˜ค์ง ๊ด€์‹ฌ ์žˆ๋Š” class์— positive ์˜ํ–ฅ์„ ์ฃผ๋Š” feature์—๋งŒ ๊ด€์‹ฌ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฆ‰, y_c๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ์ฆ๊ฐ€๋˜์–ด์•ผ ํ•  intensity๋ฅผ ๊ฐ€์ง€๋Š” pixel์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ReLU๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด, localization์—์„œ ๋” ๋‚˜์œ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค€๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Reference ์›๋…ผ๋ฌธ Learning Deep Features for Discriminative Localization Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization Youtube iAI POSTECH | CAM ๊น€์„ฑ๋ฒ” ์ธ๊ณต์ง€๋Šฅ ๊ณตํ•™ ์—ฐ๊ตฌ์†Œ | Class Activation Map (CAM), GradCAM ๋ธ”๋กœ๊ทธ CNN visualization: CAM and Grad-CAM ์„ค๋ช… CAM ๊ตฌํ˜„ Grad-CAM(Gradient-weighted Class Activation Mapping), ์ฝ”๋“œ ํฌํ•จ 2. Image Classification(์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜) ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๋‚˜ ์–ด๋–ป๊ฒŒ ๋ณด๋ฉด ๊ฐ€์žฅ ์ค‘์š”ํ•œ Task์ž…๋‹ˆ๋‹ค. ๊ฐœ/๊ณ ์–‘์ด๋ฅผ ๊ตฌ๋ถ„ํ•ด ๋‚ด๋Š” ์•„ํ‚คํ…์ฒ˜๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋” ๋‚˜์•„๊ฐ€ ์ด๋ฏธ์ง€์˜ ์ •ํ•ด์ง„ Category๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค. ์ด ์•„ํ‚คํ…์ฒ˜๋Š” ๋‹ค๋ฅธ Task์˜ Backbone์œผ๋กœ ์ด์šฉ๋˜๊ธฐ๋„ ํ•˜๋ฉฐ, ๊ฐ€์žฅ ์‹คํ—˜์ ์ธ ์‹œ๋„๊ฐ€ ๋จผ์ € ์ด๋ฃจ์–ด์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. (1) ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ์•„์ด๋””์–ด ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๋ž€ ๋ง ๊ทธ๋Œ€๋กœ ์ธ๊ณต์ง€๋Šฅ์ด ์ด๋ฏธ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•ด ์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ•์•„์ง€ ์‚ฌ์ง„๊ณผ ๊ณ ์–‘์ด ์‚ฌ์ง„์„ ์‚ฌ๋žŒ์—๊ฒŒ ๋ณด์—ฌ์ฃผ๋ฉด, ์‹œ๊ฐ ์ด๋ฏธ์ง€์™€ ์ƒ์‹์„ ํ†ตํ•ด ์‚ฌ์ง„์„ ๊ตฌ๋ณ„ํ•ด ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ๋Š” ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๋‚ผ๊นŒ์š”? ์šฐ์„  ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ณ ์–‘์ด ์‚ฌ์ง„์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค๊ณ  ํ•  ๋•Œ, ์ปดํ“จํ„ฐ์—๋Š” ์ด๋ฏธ ๊ฐ•์•„์ง€, ๊ณ ์–‘์ด, ํŠธ๋Ÿญ ๋“ฑ ๋ฏธ๋ฆฌ ์ •ํ•ด๋‘” ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์ปดํ“จํ„ฐ๋Š” ์ž…๋ ฅ ์‚ฌ์ง„์ด ์–ด๋–ค ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ• ์ง€ ๊ฒฐ์ •์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์—๊ฒŒ๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ด์ง€๋งŒ ์ปดํ“จํ„ฐ์—๊ฒŒ๋Š” ๋ณต์žกํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ์—๊ฒŒ ๊ณ ์–‘์ด ์ด๋ฏธ์ง€๋Š” ์•„์ฃผ ํฐ ๊ฒฉ์ž ๋ชจ์–‘์˜ ์ˆซ์ž ์ง‘ํ•ฉ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. W * H * C (W : width , H : height, C : channel (RGB ๊ทธ๋ฆผ์˜ ๊ฒฝ์šฐ 3)) ๊ฐœ์˜ ํ”ฝ์…€ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ์ˆซ์ž ์ง‘ํ•ฉ์œผ๋กœ๋Š” ๊ณ ์–‘์ด๋ฅผ ๊ตฌ๋ณ„ํ•ด ๋‚ด๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๋”๋ผ๋„ ๋ช…์•”์˜ ์ฐจ์ด, ๊ด€์ ์˜ ์ฐจ์ด ๋“ฑ์ด ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์‚ฌํ•ญ์„ ๊ทน๋ณตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹œ์  ๋ณ€ํ™”(Viewpoint variation) : ์ด๋ฏธ์ง€๋ฅผ ์–ด๋Š ๋ฐฉํ–ฅ์—์„œ ์ฐ์—ˆ๋Š”์ง€์— ๋”ฐ๋ผ ๊ฐ™์€ ๋Œ€์ƒ๋„ ๋‹ค๋ฅด๊ฒŒ ๋ณด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํฌ๊ธฐ ๋ณ€ํ™”(Scale variation) : ๋Œ€์ƒ์˜ ํฌ๊ธฐ ๋ณ€ํ™”์— ๋”ฐ๋ผ ์ปดํ“จํ„ฐ์—๊ฒŒ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋กœ ์ธ์‹๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€ํ˜•(Deformation) : ๊ฐ•์ฒด๊ฐ€ ์•„๋‹Œ ๋Œ€์ƒ์€ ํ˜•ํƒœ๊ฐ€ ๋ณ€ํ˜•๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ์ƒ‰(Occlusion) : ๋Œ€์ƒ์ด ๋‹ค๋ฅธ ๋ฌผ์ฒด์— ์˜ํ•ด ๊ฐ€๋ ค์ง€๊ฑฐ๋‚˜ ์ผ๋ถ€๋ถ„๋งŒ ๋ณด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด‘์› ์กฐ๊ฑด(Illumination condition) : ์กฐ๋ช…์„ ๋ฐ›๋Š”์ง€ ์œ ๋ฌด์™€ ๊ทธ ์ •๋„์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋ณด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ๊ฒฝ ํด๋Ÿฌํ„ฐ(Background clutter) : ๋Œ€์ƒ์ด ๋ฐฐ๊ฒฝ์— ์„ž์—ฌ๋“ค์–ด๊ฐ€ ๊ตฌ๋ถ„์ด ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค ๊ฐ„ ๊ตฌ๋ณ„(Intra-class variation) : ๋Œ€์ƒ์ด ์†ํ•˜๋Š” ๋ ˆ์ด๋ธ”(ํด๋ž˜์Šค)์˜ ๋ฒ”์œ„๊ฐ€ ๋„ˆ๋ฌด ํฌ๊ด„์ ์ด์–ด์„œ ๊ทธ ๋Œ€์ƒ์„ ํŠน์ • ์ง“์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ฐฉ๋ฒ• 1. ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ ๋ฐฉ๋ฒ•(Data-Driven Approach) ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ ๋ฐฉ์‹์—๋Š” Nearest Neighbor Classifier, K-Nearest Neighbor Classifier ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ์Šต๋‹ˆ๋‹ค. 1-1. Nearest Neighbor Classifier Nearest Neighbor(NN)์€ ์ปจ๋ณผ๋ฃจ์…˜ ์‹ ๊ฒฝ๋ง ๋ฐฉ๋ฒ•๊ณผ๋Š” ์•„๋ฌด ์ƒ๊ด€์ด ์—†๊ณ  ์‹ค์ œ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ๋˜์ง€๋Š” ์•Š์ง€๋งŒ, ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ธก ๋‹จ๊ณ„์—์„œ๋Š” ํˆฌ์ž…๋œ ์ด๋ฏธ์ง€์™€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”์„ ํ†ตํ•ด ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์™€ ์ด๋ฏธ์ง€์˜ ๊ฐ€๊นŒ์šด ์ •๋„(distance)๋Š” ๋‹ค์–‘ํ•œ ์ง€ํ‘œ (metric) ์ด ์žˆ์ง€๋งŒ ๊ทธ์ค‘ ํ•œ ๊ฐ€์ง€์ธ L1 distance๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1 ( 1 I) โˆ‘ | p โˆ’ p | L1 ์™ธ์—๋„ ๋‹ค์–‘ํ•œ Metric์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. L2 distance๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 2 ( 1 I) โˆ‘ ( p โˆ’ p) L1 L2 distance์˜ ์ฐจ์ด L1๊ณผ L2๋Š” p-norm ๊ณ„์—ด์˜ distance measure์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด L1๊ณผ L2์˜ ์ฐจ์ด๋Š” L2 distance๋Š” L1 distance๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์ฐจ์ด๊ฐ€ ํฐ ๊ฒƒ์— ๋” ๊ด€๋Œ€ํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, L1 ์ด ์•„๋‹Œ L2 distance๋ฅผ ์“ด๋‹ค๋Š” ๊ฒƒ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ dimension์—์„œ ์ ๋‹นํ•œ ์ฐจ์ด๋ฅผ ๋ณด์ด๋Š” ๊ฒƒ๋ณด๋‹ค ํ•˜๋‚˜์˜ dimension์—์„œ ํฐ ์ฐจ์ด๋ฅผ ๋ณด์ด๋Š” ๊ฒƒ์— ๋” ํŽ˜๋„ํ‹ฐ๋ฅผ ๋งŽ์ด ์ค€๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. 1-2. K-Nearest Neighbor Classifier NN์€ ๋‹จ์ ์ด ๋งŽ์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๋จผ์ €, NN ์€ ๋‹จ ํ•˜๋‚˜์˜ label๋งŒ prediction์—์„œ ๊ณ ๋ คํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์ •์„ฑ์ด ๋–จ์–ด์ง€๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด k-nearest neighbor (KNN)๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์˜ˆ์ธก ๋‹จ๊ณ„์—์„œ ์ธํ’‹๊ณผ ๊ฐ€๊นŒ์šด ์ˆœ์œผ๋กœ ์ด k ๊ฐœ์˜ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”์„ ๊ตฌํ•œ ํ›„, ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋‚˜์˜ค๋Š” ๋ ˆ์ด๋ธ”๋กœ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ ๊ฐœ๋กœ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋กœ ํ•˜๋Š” ๊ฒƒ์„ ๋จธ์‹ ๋Ÿฌ๋‹์—์„œ๋Š” voting์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. KNN์€ ํ•™์Šต ๋ฐ์ดํ„ฐ ์…‹์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋งŒ์„ ์ฐพ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด k ๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์•„์„œ ํ…Œ์ŠคํŠธ ์ด๋ฏธ์ง€์˜ ๋ผ๋ฒจ์— ๋Œ€ํ•ด ํˆฌํ‘œํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. k = 1 ์ธ ๊ฒฝ์šฐ, ์›๋ž˜์˜ Nearest Neighbor ๋ถ„๋ฅ˜๊ธฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ง๊ด€์ ์œผ๋กœ k ๊ฐ’์ด ์ปค์งˆ์ˆ˜๋ก ๋ถ„๋ฅ˜๊ธฐ๋Š” ์ด์ƒ์ (outlier)์— ๋”<NAME>ํ•˜๊ณ , ๋ถ„๋ฅ˜ ๊ฒฝ๊ณ„๊ฐ€ ๋ถ€๋“œ๋Ÿฌ์›Œ์ง€๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. NN์—๋Š” ์ด์ƒ์น˜ (outlier)๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์„ฌ๊ณผ ๊ฐ™์€ ์ง€์—ญ (decision boundary) ๊ฐ€ ์ƒ๊ธด๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ KNN์˜ ๊ฒฝ์šฐ, ์ด์ƒ์น˜์— ๋” ๋‘”๊ฐํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. NN์˜ ๊ฒฝ์šฐ ์ด์ƒ์น˜์— ๋ฏผ๊ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŠธ๋ ˆ์ด๋‹ ๋ฐ์ดํ„ฐ์— ๊ตญํ•œ๋œ ๊ทœ์น™์„ ๋ฐฐ์šธ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ KNN ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฒ˜์Œ ๋ณด๋Š” ๋ฐ์ดํ„ฐ (unseen data) ๋Œ€ํ•œ ์„ฑ๋Šฅ (generalization)์ด ๋†’์Šต๋‹ˆ๋‹ค. 1-3 Bayesian Classifier ๋ฒ ์ด์ง€์•ˆ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ๋ฒ ์ด์ฆˆ ์ •๋ฆฌ์— ๋Œ€ํ•ด ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฒ ์ด์ง€์•ˆ ๋ถ„๋ฅ˜๊ธฐ๋Š” ์ด ๋ฒ ์ด์ฆˆ ์ •๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ•˜๋Š”์ง€ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. 2. ๊ทœ์น™ ๊ธฐ๋ฐ˜ ๋ฐฉ๋ฒ•(Rule-Driven Approach) ๊ทœ์น™ ๊ธฐ๋ฐ˜ ํ•™์Šต ๋ฐฉ๋ฒ•์€ ์ฃผ์–ด์ง„ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ๊ฒฐ๊ด๊ฐ’์„ ๋„์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ if-then ๋ฐฉ์‹์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ(example)์— ๋Œ€ํ•ด ๊ฐ€์„ค(hypothesis)๋ฅผ ์„ธ์šฐ๊ณ  ๊ฒฐ๊ณผ(concept)๋ฅผ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์€ ๊ท€๋‚ฉ์  ์‚ฌ๊ณ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๊ทœ์น™ ๊ธฐ๋ฐ˜ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” Find-S ์•Œ๊ณ ๋ฆฌ์ฆ˜, Version space/ Candidate ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทœ์น™ ๊ธฐ๋ฐ˜ ํ•™์Šต์ด ํƒ€๋‹นํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ช‡ ๊ฐ€์ง€ ๊ฐ€์ •์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1. ์šฐ๋ฆฌ๊ฐ€ ์•Œ์•„๋ณผ ์„ธ์ƒ์—์„œ๋Š” ๊ด€์ธก ์˜ค์ฐจ(observation errors)๊ฐ€ ์—†๋‹ค. 2. ์ผ๊ด€์„ฑ์ด ์—†๋Š” ๊ด€์ธก ๋˜ํ•œ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. 3. ์–ด๋–ค ํ™•๋ฅ ๋ก ์  ์š”์†Œ(stochastic element)๋„ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. 4. ์šฐ๋ฆฌ๊ฐ€ ๊ด€์ธกํ•˜๋Š” ์ •๋ณด๊ฐ€ ์‹œ์Šคํ…œ์— ์žˆ๋Š” ๋ชจ๋“  ์ •๋ณด์ด๋‹ค. ์ด ๋„ค ๊ฐ€์ง€ ๊ฐ€์ •์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋Š” ๊ณณ์„ ์™„๋ฒฝํ•œ ์„ธ๊ณ„(perfect world)๋ผ๊ณ  ๋งํ•˜๊ณ  ๊ทœ์น™ ๊ธฐ๋ฐ˜ ํ•™์Šต์€ ์™„๋ฒฝํ•œ ์„ธ๊ณ„์—์„œ ํƒ€๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2-1 Find-S ์•Œ๊ณ ๋ฆฌ์ฆ˜ Find-S ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ€์žฅ ๊ตฌ์ฒด์ ์ธ ๊ฐ€์„ค์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์ ์  General ํ•œ ๊ฐ€์„ค์„ ์ฐพ์•„๋‚ด๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ ์€ ์˜ˆ์ œ ์ค‘ positive training example๋งŒ ์„ ๋ณ„ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํŠน์ • ์˜ˆ์ œ๋กœ๋ถ€ํ„ฐ ๋„์ถœ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค๋ฅธ ์˜ˆ์ œ๋“ค์— ๋ฐ˜๋ณตํ•˜์—ฌ ์ ์šฉํ•˜์—ฌ ๋ณ€๊ฒฝ๋œ ๋ถ€๋ถ„๋งŒ don't care condition์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์ตœ์ข…์ ์ธ ๊ฐ€์„ค์„ ๋งŒ๋“ค์–ด ๋ƒ…๋‹ˆ๋‹ค. ๋ฐ‘์˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ํ‘œ๋Š” ํ•˜๋Š˜, ๋‚ ์”จ, ๋ฐ”๋žŒ ๋“ฑ์˜ ์š”์†Œ๋ฅผ ๋ณด๊ณ  ์šด๋™์„ ์ฆ๊ธธ ์ˆ˜ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋Š” ํ‘œ์ž…๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ positive example์€ Enjoy Sport๊ฐ€ Yes ์ผ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์€ ์šด๋™์„ ์ฆ๊ธธ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์˜ ์ผ๋ฐ˜์ ์ธ ๊ฐ€์„ค์„ ์„ธ์šฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋จผ์ € ๋ชจ๋“  ์นผ๋Ÿผ์ธ sky, temp, humid, wind, water, forecast๋ฅผ ๋ณด๋ฉด์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ–‰์„ ๋ณธ๋‹ค๋ฉด Sunny, Warm, Normal, Strong, Warm, Same์ธ ๊ฒฝ์šฐ์— positive ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„ ๋‘ ๋ฒˆ์งธ ํ–‰์„ ๋ณด๋ฉด Humid ๋ถ€๋ถ„๋งŒ ๋‹ค๋ฅด๊ณ  ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ positive ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ Humid๋ฅผ i don't care condition์œผ๋กœ ์ง€์ •ํ•จ์œผ๋กœ์จ ๊ฐ€์„ค์„ ์ข€ ๋” ์ผ๋ฐ˜ํ™”ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 3๋ฒˆ์งธ ํ–‰์€ positive ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— pass ํ•ฉ๋‹ˆ๋‹ค. 4๋ฒˆ์งธ ํ–‰์€ water์™€ forecast ๋ถ€๋ถ„์ด ๊ธฐ์กด๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋˜์–ด ๋‘ ์นผ๋Ÿผ์ด i don't care ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ Sky = Sunny, Temp = Warm, Wind = Strong ํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ฌด์กฐ๊ฑด ์šด๋™์„ ์ฆ๊ธธ ์ˆ˜ ์žˆ๋Š” ๋‚ ์ด๋ผ๋Š” ์ผ๋ฐ˜ํ™”๋œ ๊ฐ€์„ค์„ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Find-S ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ˜„์‹ค ์„ธ๊ณ„์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ์— ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ์„ธ์ƒ์„ ์ €๋ ‡๊ฒŒ ๋‹จ์ˆœํ•˜์ง€ ์•Š์œผ๋ฉฐ ๊ณ ๋ คํ•ด์•ผ ํ•  ๋ถ€๋ถ„์ด ํ›จ์”ฌ ๋” ๋งŽ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ตœ์ข…์ ์œผ๋กœ ํ•˜๋‚˜์˜ ์ผ๋ฐ˜ํ™”๋œ ๊ฐ€์„ค์„ ๋„์ถœํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์„ค์˜ ์ง‘ํ•ฉ์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ํ•˜๋Š˜์ด ๋ง‘์€ ์ƒํ™ฉ(์ „์ œ ์กฐ๊ฑด)์—์„œ ๋”ฐ๋œปํ•˜๊ณ  ๋ฐ”๋žŒ์ด ๊ฐ•ํ•˜๊ฒŒ ๋ถˆ๋•Œ(1), ์ถฅ์ง€๋งŒ ๋ฐ”๋žŒ์ด ์•ฝํ•˜๊ฒŒ ๋ถˆ๋•Œ(2) ์šด๋™์„ ์ฆ๊ธธ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ € 2๊ฐ€์ง€ ์กฐ๊ฑด์—์„œ๋งŒ ์šด๋™์„ ์ฆ๊ธธ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ธ๋ฐ Find-S ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ๋Š” ํ•˜๋Š˜์ด ๋ง‘์„ ๋•Œ ์šด๋™์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ผ๋ฐ˜ํ™”๋œ ๊ฐ€์„ค์„ ๋„์ถœํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 2-2 Candidate Algorithm ํ•˜๋‚˜์˜ ์ผ๋ฐ˜์ ์ธ ๊ฐ€์„ค์„ ๋„์ถœํ•˜๋Š” Find-S ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ๋ฐœ์ „๋œ ํ˜•ํƒœ๋กœ, ์˜ˆ์ œ์— ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๊ฐ€์„ค์˜ ์ง‘ํ•ฉ(version space)๋ฅผ ๋„์ถœํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ตฌ์กฐ์—์„œ G๋Š” ๋งค์šฐ Ganeral ํ•œ ๊ฐ€์„ค์—์„œ ์‹œ์ž‘ํ•˜๊ณ  S๋Š” ๋งค์šฐ Specific ํ•œ ๊ฐ€์„ค์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. G๋Š” negative ์˜ˆ์ œ, S๋Š” positive ์˜ˆ์ œ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์ž ์กฐ๊ฑด์„ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ๋‹ค ๋ณด๋ฉด G์™€ S๊ฐ€ ๊ฒฐ๊ตญ ๋งŒ๋‚˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด ๋ถ€๋ถ„์ด ๋ชจ๋“  ๊ฐ€์„ค์˜ ์ง‘ํ•ฉ์ธ Version Space๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ S๋Š” 3๊ฐ€์ง€ ์นผ๋Ÿผ์„ ๊ฐ–๋Š” ํ•˜๋‚˜์˜ ์š”์†Œ๊ฐ€ ๋‚จ์•˜๊ณ  G๋Š” 1๊ฐ€์ง€ ์นผ๋Ÿผ์„ ๊ฐ–๋Š” ๋‘ ๊ฐœ์˜ ์š”์†Œ๊ฐ€ ๋‚จ์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์šฐ๋ฆฌ๋Š” ์ด 6๊ฐœ์˜ ๊ฐ€์„ค์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Sunny & Strong Sunny & Warm Warm & Strong Sunny Warm Sunny & Warm & Strong ์ฆ‰ 6๊ฐœ์˜ ํ›„๋ณด(Candidate) ๊ฐ€์„ค์„ ๋„์ถœํ•ด ๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Candidate ๋ฐฉ๋ฒ•์€ ์ผ๋ฐ˜ํ™”๋œ ํ•˜๋‚˜์˜ ๊ฐ€์„ค์„ ๋ฝ‘์•„๋‚ด์ง„ ๋ชปํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ ์ƒํ™ฉ์„ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ Find-S๋ณด๋‹ค๋Š” ํƒ€๋‹นํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์–ด๋–ค ๊ฐ€์„ค์ด ์ •ํ™•ํ•œ์ง€ ์•Œ ์ˆ˜ ์—†๊ธฐ์— ์ด ๋˜ํ•œ ํ˜„์‹ค ์„ธ๊ณ„์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ฒฐ๋ก  ๊ฒฐ๋ก ์ ์œผ๋กœ ์šฐ๋ฆฌ์˜ ์„ธ๊ณ„๋Š” ์™„๋ฒฝํ•œ ์„ธ๊ณ„๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทœ์น™ ๊ธฐ๋ฐ˜ ํ•™์Šต๋ณด๋‹ค๋Š” ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ ํ•™์Šต์„ ํ†ตํ•ด ์ธ๊ณต์ง€๋Šฅ์„ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค. ์„ธ์ƒ์€ ์™„๋ฒฝํ•ด์งˆ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์•ž์œผ๋กœ๋„ ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ ํ•™์Šต์ด ๋ฐœ์ „ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค. Reference ๊ทœ์น™ ๊ธฐ๋ฐ˜ ๋ฐฉ๋ฒ• ์ด๋ฏธ์ง€ : https://yngie-c.github.io/machine%20learning/2020/04/05/rule_based/ 1) ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ฐ์ดํ„ฐ ์…‹ ์†Œ๊ฐœ MNIST MNIST(Modified National Institute of Standards and Technology database) ๋ฐ์ดํ„ฐ๋Š” ์†์œผ๋กœ ์จ ์—ฌ์ง„ ์ˆซ์ž 0-9๋กœ, ํ›ˆ๋ จ์„ ์œ„ํ•œ 60,000๊ฐœ์˜ ์ด๋ฏธ์ง€์™€ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ 1,000๊ฐœ์˜ ์ด๋ฏธ์ง€๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. 28x28์˜ ํฌ๊ธฐ๋กœ ์ •๊ทœํ™” ๋ฐ ์ค‘์•™ ์ •๋ ฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์…‹์€ ์ฑ„๋„์ด ํ•˜๋‚˜์ธ ํ‘๋ฐฑ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. 1990๋…„๋Œ€ Yann LeCun์— ์˜ํ•ด ์ œ์‹œ๋˜์—ˆ์œผ๋ฉฐ ๊ณ ๋“ฑํ•™์ƒ๋“ค์˜ ๊ธ€์”จ์ธ NIST SD-1 ๊ณผ ์ธ๊ตฌ์กฐ์‚ฌ๊ตญ ์ง์›๋“ค์˜ ๊ธ€์”จ์ธ NIST SD-3๋กœ๋ถ€ํ„ฐ 3๋งŒ ๊ฐœ์”ฉ ๋ฝ‘์•„ training set์„, 5์ฒœ ๊ฐœ์”ฉ ๋ฝ‘์•„ test set์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. MNIST dataset์€ Yann LeCun's website์—์„œ ๋‹ค์šด๋กœ๋“œ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. MNIST์˜ ๊ตฌ์กฐ ๊ฐ ์ด๋ฏธ์ง€๋Š” 28 * 28 = 784์˜ ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ํ‘๋ฐฑ ์ด๋ฏธ์ง€์ด๊ธฐ์— ํ•œ ๊ฐœ์˜ ์ฑ„๋„์„ ๊ฐ–์Šต๋‹ˆ๋‹ค. 55000๊ฐœ์˜ Training set์˜ ๊ตฌ์กฐ๋ฅผ ๋ณธ๋‹ค๋ฉด, [55000, 784]์˜ tensor๋กœ ๊ตฌ์„ฑ๋จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์˜ label์˜ ๊ตฌ์กฐ๋Š” [55000, 10] (0 ~ 9์˜ ์ˆซ์ž์ด๋ฏ€๋กœ)์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. CIFAR CIFAR-10 CIFAR-10 (Canadian Institute for Advanced Reseacrh -10) ๋ฐ์ดํ„ฐ ์…‹์—๋Š” 50,000๊ฐœ์˜ Training set๊ณผ 10,000๊ฐœ์˜ test set์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ด 60,000๊ฐœ์˜ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด label ๋น„ํ–‰๊ธฐ, ์ž๋™์ฐจ, ์ƒˆ, ๊ณ ์–‘์ด, ์‚ฌ์Šด, ๊ฐœ, ๊ฐœ๊ตฌ๋ฆฌ, ๋ง, ๋ฐฐ, ํŠธ๋Ÿญ์œผ๋กœ ์ด 10๊ฐœ์ด๋ฉฐ, ๊ฐ ์ด๋ฏธ์ง€๋Š” 3channel, 32x32 image์ž…๋‹ˆ๋‹ค. CIFAR-100 CIFAR-100 ๋ฐ์ดํ„ฐ ์…‹์€ ๋™์ผํ•œ ์ˆ˜์˜ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, 100๊ฐœ์˜ label์„ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ํด๋ž˜์Šค ๋‹น 600๊ฐœ์˜ ์ด๋ฏธ์ง€๋งŒ ์žˆ์Šต๋‹ˆ๋‹ค. CIFAR-100 ๋ฐ์ดํ„ฐ ์…‹์€ 20๊ฐœ์˜ Superclass๋กœ ๋ฌถ์–ด์ง€๋ฉฐ, ๋ชจ๋“  ์ด๋ฏธ์ง€๋Š” ๋ณธ์ธ์ด ์†ํ•œ class์ธ "fine"label๊ณผ ๋ณธ์ธ์ด ์†ํ•œ Superclass์ธ "coarse"label 2๊ฐœ์˜ label์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Superclass Classes aquatic mammals beaver, dolphin, otter, seal, whale fish aquarium fish, flatfish, ray, shark, trout flowers orchids, poppies, roses, sunflowers, tulips food containers bottles, bowls, cans, cups, plates fruit and vegetables apples, mushrooms, oranges, pears, sweet peppers household electrical devices clock, computer keyboard, lamp, telephone, television household furniture bed, chair, couch, table, wardrobe insects bee, beetle, butterfly, caterpillar, cockroach large carnivores bear, leopard, lion, tiger, wolf large man-made outdoor things bridge, castle, house, road, skyscraper large natural outdoor scenes cloud, forest, mountain, plain, sea large omnivores and herbivores camel, cattle, chimpanzee, elephant, kangaroo medium-sized mammals fox, porcupine, possum, raccoon, skunk non-insect invertebrates crab, lobster, snail, spider, worm people baby, boy, girl, man, woman reptiles crocodile, dinosaur, lizard, snake, turtle small mammals hamster, mouse, rabbit, shrew, squirrel trees maple, oak, palm, pine, willow vehicles 1 bicycle, bus, motorcycle, pickup truck, train vehicles 2 lawn-mower, rocket, streetcar, tank, tractor Fashion MNIST Fashion MNIST๋Š” MNIST์˜ ๋Œ€์•ˆ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋ฐ์ดํ„ฐ ์…‹์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ MNIST๋Š” ๋„ˆ๋ฌด ์‰ฌ์›Œ์„œ ์–ด๋–ค ๋ฐฉ๋ฒ•๋ก ์„ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ๋Œ€๋ถ€๋ถ„ ๋†’์€ ์ •ํ™•๋„๋ฅผ ๋ณด์ด๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. Fashion MNIST๋Š” MNIST๋ณด๋‹ค๋Š” ์ข€ ๋” ์–ด๋ ค์šด ๋ฌธ์ œ์ด๋‚˜, ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ๋Š” MNIST์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 60,000๊ฐœ์˜ training set๊ณผ, 10,000 ๊ฐœ์˜ test set์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ MNIST๋ฅผ ์™„๋ฒฝํžˆ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Label ์—ญ์‹œ MNIST์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 0-9๊นŒ์ง€ 10๊ฐœ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Label Description 0 T-shirt/top 1 Trouser 2 Pullover 3 Dress 4 Coat 5 Sandal 6 Shirt 7 Sneaker 8 Bag 9 Ankle boot ImageNet ImageNet์€ ๋Œ€ํ‘œ์ ์ธ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ, 20,000๊ฐœ๊ฐ€ ๋„˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ๊ฐ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ์ˆ˜๋ฐฑ ๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Fei-Fei Li์˜ ์•„์ด๋””์–ด๋กœ 2006๋…„๋ถ€ํ„ฐ image net์„ ๋งŒ๋“œ๋Š” ํ”„๋กœ์ ํŠธ๊ฐ€ ์‹œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Image Net์˜ ๋ฐ์ดํ„ฐ๋Š”, Classification์„ ์œ„ํ•ด Amazon Mechanical Turk ์„œ๋น„์Šค๋กœ ์‚ฌ๋žŒ์ด ์ด๋ฆดํžˆ ๋ถ„๋ฅ˜ํ•˜๋„๋ก ํ•ด ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ImageNet์˜ ๋ฐ์ดํ„ฐ ์…‹์€ ILSVRC(ImageNet Large Scale Visual Recognition Challenge)์—์„œ๋„ ์“ฐ์ž…๋‹ˆ๋‹ค. 2013๋…„์— ์ปดํ“จํ„ฐ ๋น„์ „์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๋ชจ๋ธ์ด 1์œ„๋ฅผ<NAME> ํ›„ ๊ทธ ์ดํ›„๋กœ ๊ณ„์† ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์ด ์šฐ์Šน์„ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. paperswithcode์—์„œ ํ˜„์žฌ๊นŒ์ง€์˜ ILSVRC์—์„œ ์šฐ์Šนํ•œ ๋ชจ๋ธ๋“ค์˜ top 1 accuracy, ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜ ๋“ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Reference Yann LeCun's website Tensorflow MNIST tutorial for beginner CIFAR-10 and CIFAR-100 datasets Tensorflow fashion MNIST ํŠœํ† ๋ฆฌ์–ผ Fashion-MNIST (2) ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ ์ดํ•ด๋ฅผ ์œ„ํ•œ ์‚ฌ์ „ ์ง€์‹(โ˜…์ž‘์„ฑ ์ค‘) ... (3) ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ๋“ค Image Classification ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ชจ๋ธ์— ์‚ฌ์šฉ๋œ Layer ์ˆ˜๋กœ ๊ตฌ๋ถ„ํ–ˆ์Šต๋‹ˆ๋‹ค. 1) LeNet, AlexNet, ZFNet(๋ ˆ์ด์–ด 8๊ฐœ ์ดํ•˜) LeNet-5 LeNet์€ CNN์„ ์ฒ˜์Œ ๊ฐœ๋ฐœํ•œ Yann LeCun์˜ ์—ฐ๊ตฌํŒ€์ด 1998๋…„์— ์ œ์‹œํ•œ ๋‹จ์ˆœํ•œ CNN์ž…๋‹ˆ๋‹ค. LeNet์˜ ๋“ฑ์žฅ ๋ฐฐ๊ฒฝ LeNet ์ด์ „์˜ ํŒจํ„ด์ธ์‹์—์„œ ์ด์šฉ๋˜๋Š” ๋ชจ๋ธ์€ Hand-designed feature extractor๋กœ ํŠน์ง•์„ ์ถ”์ถœํ•˜๊ณ  FC multi layer networks๋ฅผ ๋ถ„๋ฅ˜๊ธฐ๋กœ ์ด์šฉํ–ˆ์œผ๋‚˜, ์ด ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. Hand designed feature extractor๋Š” ๊ด€๋ จ ์žˆ๋Š” ์ •๋ณด๋งŒ ์ˆ˜์ง‘ํ•˜๊ณ  ๋ฌด๊ด€ํ•œ ์ •๋ณด๋Š” ์ œ๊ฑฐํ•˜๋Š”๋ฐ, feature extractor์— ์˜ํ•ด ์ถ”์ถœ๋œ ์ •๋ณด๋งŒ ๊ฐ€์ง€๊ณ  classifier์˜ ํ•™์Šต์ด ์ง„ํ–‰๋˜๋ฏ€๋กœ ํ•™์Šต์— ์ œํ•œ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. LeCun์€ feature extractor ๊ทธ ์ž์ฒด์—์„œ ํ•™์Šต์ด ์ด๋ฃจ์–ด์ ธ์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ FC๋กœ ์ „ํ™˜ํ•ด ํ•™์Šตํ•˜๋Š” ๋ฐฉ์‹์€ ๋„ˆ๋ฌด ๋งŽ์€ parameter๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ๊ฐ’์˜ Topology๊ฐ€ ์™„์ „ํžˆ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 2D ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ, ์ด๋Š” ๊ณต๊ฐ„์ ์œผ๋กœ ๋งค์šฐ ํฐ ์ƒ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. FC๋Š” ์ด๋ฏธ์ง€๋ฅผ ์ผ๋ ฌ๋กœ ํŽผ์น˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋Ÿฐ ๊ณต๊ฐ„์ ์ธ ๊ด€๊ณ„๋ฅผ ์™„์ „ํžˆ ๋ฌด์‹œํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. LeCun์€ ์ด ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ์ ์œผ๋กœ CNN์„ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. CNN์€ classifier๋ฟ ์•„๋‹ˆ๋ผ feature๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋‹จ๊ณ„ ์—ญ์‹œ ํ•™์Šต์ด ์ง„ํ–‰๋˜๊ณ , Weight sharing๊ณผ Local connectivity์— ์˜ํ•ด ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋ฏธ์ง€์˜ ๊ณต๊ฐ„์ ์ธ Topology๋ฅผ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. LeNet์˜ ๊ตฌ์กฐ ์œ„ LeNet์˜ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•  ์ ์€ C3 layer์ธ๋ฐ, ๋ชจ๋“  S2 feature map์ด C3์˜ feature map์— ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. C3์˜ feature map๊ณผ ์—ฐ๊ฒฐ๋œ S2์˜ feature map์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ ๋ชจ๋“  feature map์„ ์—ฐ๊ฒฐํ•˜์ง€ ์•Š์•„ Connection์„ ์ค„์ผ ์ˆ˜ ์žˆ๊ณ , ์„œ๋กœ ๋‹ค๋ฅธ ์ž…๋ ฅ๊ฐ’์„ ์ทจํ•˜๋„๋ก ํ•ด์„œ C3์˜ ๊ฐ feature map์ด ์„œ๋กœ ๋‹ค๋ฅธ feature๋ฅผ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด ๊ทธ๋ ‡๊ฒŒ ํ–ˆ๋‹ค๊ณ  ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ๊ณ„๋ณ„๋กœ LeNet์ด ์–ด๋–ป๊ฒŒ ๋‚˜ํƒ€๋‚˜๋Š”์ง€๋ฅผ ์•„๋ž˜์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. LeNet์˜ ํŠน์ง• Filter size : 5x5 stride : 1 Pooling : 2x2 average pooling Activation function: ๋Œ€๋ถ€๋ถ„์˜ unit์ด sigmoid๋ฅผ ์‚ฌ์šฉ. F6์—์„œ๋Š” tanh๋ฅผ ์‚ฌ์šฉ. ์ตœ์ข…์ ์ธ output layer์ธ F7์—์„œ๋Š” RBF (Euclidian Radia basis function unit)์„ ์‚ฌ์šฉ loss function : MSE Reference ์›๋ณธ paper : Gradient-based learning applied to document recognition (Y.Lecunn et al. , 1998) Yann LeCun 's MNIST Demo ๋”ฅ๋Ÿฌ๋‹ ๊ณต๋ถ€๋ฐฉ | LeNet-5 (1998), ํŒŒ์ด ํ† ์น˜๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ AlexNet AlexNet์€ 2012๋…„ Alex Krizhevsky , Ilya Sutskever, Geoffrey Hinton์— ์˜ํ•ด ์ œ์‹œ๋œ CNN architecture๋กœ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ๋Š” LeNet๊ณผ ๋น„์Šทํ•˜๋‚˜, GPU 2๋Œ€๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น ๋ฅธ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ด์ง€๋ฉด์„œ ๋ณ‘๋ ฌ์ ์ธ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. AlexNet์˜ ํŠน์ง• Activation function ์ฒ˜์Œ์œผ๋กœ ReLU ์‚ฌ์šฉ. RELU๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ Tanh, Sigmoid function์— ๋น„ํ•ด 6๋ฐฐ ๋น ๋ฅด๊ฒŒ ์›ํ•˜๋Š” ์ˆ˜์ค€ ์ดํ•˜์˜ error rate์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Over-fitting ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด ๋„์ž…ํ•œ ๋ฐฉ๋ฒ• Data augmentation : ๋ฐ์ดํ„ฐ ์…‹ ์ด๋ฏธ์ง€๋ฅผ ์ขŒ์šฐ ๋ฐ˜์ „์„ ์‹œํ‚ค๊ฑฐ๋‚˜ (flip augmentation), ์ด๋ฏธ์ง€๋ฅผ ์ž˜๋ผ์„œ (Crop augmentation) ๋ฐ์ดํ„ฐ ์ˆ˜๋ฅผ ๋Š˜๋ฆผ. ๋˜ RGB ๊ฐ’์„ ์กฐ์ •ํ•˜์—ฌ (jittering) ๋ฐ์ดํ„ฐ ์ˆ˜๋ฅผ ๋Š˜๋ฆผ. Dropout: rate 0.5 Norm layer ์‚ฌ์šฉ : ์›์‹œ์ ์ธ ํ˜•ํƒœ์˜ batch normalization , ์ง€๊ธˆ์€ ์“ฐ์ด์ง€ ์•Š์Œ Batch size 128 SGD momentum 0.9 learning rate 1e-2 , validation accuracy์— ๋”ฐ๋ผ manual ํ•˜๊ฒŒ ๋‚ฎ์ถค L2 weigh decay 5e-4 7 CNN ensemble : error 18.2 % --> 15.4% Reference ์›๋ณธ paper : ImageNet Classification with Deep Convolutional Neural Network (A, Krizhevsky et al. , 2012) PIEW 9 | AI ์ธ๊ณต์ง€๋Šฅ ๊ธฐ์ดˆ CNN ์•„ํ‚คํ…์ฒ˜ '์•Œ๋ ‰์Šค ๋„ท' ๋…ผ๋ฌธ ๋ฆฌ๋ทฐ ZFNet AlexNet์— ์ดํ—ˆ ILSVRC 2013์—์„œ ์šฐ์Šนํ•œ ๊ตฌ์กฐ์ด๋ฉฐ ์—ญ์‹œ CNN ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. CNN ๋ชจ๋ธ์˜ ๊ณ ์งˆ์ ์ธ ๋ฌธ์ œ๋Š” Black box, ์ฆ‰ ํŠน์ • layer๋Š” ์ด๋ฏธ์ง€์˜ ์–ด๋–ค ๋ถ€๋ถ„์„ ๊ฒ€์ถœํ•˜๋Š”์ง€, ๋ชจ๋ธ์ด ์™œ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ZFNet์€ feature map์„ ์‹œ๊ฐํ™”ํ•˜์—ฌ ๋ธ”๋ž™๋ฐ•์Šค๋ฅผ ๋“ค์—ฌ๋‹ค๋ณด๊ณ , ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ๊ณ ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด n-1 ๋ฒˆ์งธ pooled maps์ด "Convolution > ReLU activation > Max Pooling"์„ ํ†ต๊ณผํ•˜์—ฌ n ๋ฒˆ์งธ Pooled Maps์„ ์ƒ์„ฑํ•˜์˜€๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ €์ž๋“ค์€ n ๋ฒˆ์งธ Pooled Maps์— ํ•ด๋‹น ๊ตฌ์กฐ์˜ ์—ญ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•˜์—ฌ n-1 ๋ฒˆ์งธ pooled maps์„ ๋ณต์› (reconstruction) ํ•ด๋ณด๊ณ ์ž ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 1. Max unpooling: max pooling์„ ํ•  ๋•Œ ์œ„์น˜ ์ •๋ณด๋ฅผ ๊ฐ™์ด ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค. max pooling์˜ ์—ญ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•  ๋•Œ max ๊ฐ’์ด ์œ„์น˜ํ–ˆ๋˜ ์˜์—ญ์— max ๊ฐ’์„ ์ง‘์–ด๋„ฃ๊ณ  ๋‚˜๋จธ์ง€ ์˜์—ญ์€ 0์œผ๋กœ ์ฑ„์›๋‹ˆ๋‹ค. 2. ReLU: ReLU๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์–‘์ˆ˜๋Š” ๊ทธ๋Œ€๋กœ ๊ฐ’์ด ๋ณด์กด๋˜์ง€๋งŒ ์Œ์ˆ˜ ๊ฐ’์€ ๋ชจ๋‘ 0์œผ๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ์Œ์ˆ˜ ๊ฐ’์ด ๋ชจ๋‘ ์†Œ์‹ค๋˜๊ธฐ ๋•Œ๋ฌธ์— ReLU์˜ ์—ญ๊ณผ ์ •์€ (์–‘์ˆ˜ โ†’ ์–‘์ˆ˜, 0 โ†’ 0)์ด ๋ฉ๋‹ˆ๋‹ค. (์ €์ž๋“ค์€ ์Œ์ˆ˜ ๊ฐ’์„ ๋ณต์›ํ•˜์ง€ ๋ชปํ•˜๊ณ  0์œผ๋กœ ์ฒ˜๋ฆฌํ•ด๋„ ๊ทธ ์˜ํ–ฅ์ด ๋ฏธ๋ฏธํ•˜๋‹ค๊ณ  ์ฃผ์žฅํ•ฉ๋‹ˆ๋‹ค.) 3. Transposed Convolution: Convolution์˜ ์—ญ๊ณผ์ •์œผ๋กœ Transposed convolution์„ ์‹œํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Transposed Convolution์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. AlexNet์˜ ๊ฐ layer๋ฅผ ์‹œ๊ฐํ™”ํ•œ ๊ฒฐ๊ณผ Layer 1์ด๋‚˜ 2๋ฅผ ์‹œ๊ฐํ™”ํ•œ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€์˜ ๋ชจ์„œ๋ฆฌ, ๊ฒฝ๊ณ„, ์ƒ‰๊ณผ ๊ฐ™์€ Low level feature๋ฅผ ์žก์•„๋‚ด๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Layer 3์—์„œ๋Š” ์ „๋ฐ˜์ ์ธ ํŒจํ„ด, ์‚ฌ๋ฌผ๊ณผ ๊ฐ์ฒด์˜ ๊ฒฝ๊ณ„๋ฅผ ์žก์•„๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Layer 5์—์„œ๋Š” ์‚ฌ๋ฌผ์ด๋‚˜ ๊ฐœ์ฒด์˜ ์ „๋ถ€๋ฅผ ๋ณด์—ฌ์ฃผ๋ฉฐ ๊ฐ๊ฐ ๋‹ค๋ฅธ ์œ„์น˜๋‚˜ ์ž์„ธ๋ฅผ ์ทจํ•˜๊ณ  ์žˆ๋Š” ๋ชจ์Šต์„ ์žก์•„๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. AlexNet์„ ์ˆ˜์ •ํ•˜์—ฌ ZFNet์œผ๋กœ feature map์„ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์–ด๋–ค ์‹์œผ๋กœ CNN ๊ตฌ์กฐ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ์„ฑ๋Šฅ์ด ์ข‹์•„์งˆ์ง€ ์–ด๋Š ์ •๋„ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ์ฐจ๋ก€ ์ˆ˜์ •์„ ๊ฑฐ์ณ ์ตœ์ข…์ ์œผ๋กœ ๋งŒ๋“ค์–ด๋‚ธ ZFNet์˜ ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ImageNet์œผ๋กœ ํ…Œ์ŠคํŠธํ–ˆ์„ ๋•Œ, Test set Top-5 error๊ฐ€ 16.4%์—์„œ 11.7%๋กœ ๊ฐ์†Œํ–ˆ์Šต๋‹ˆ๋‹ค. Reference ZFNet ์ด๋ž€ DSBA | CNN Localization (ZFNet&Deep Taylor Decomposition) DSBA | Introduction to Image Classification Basic Networks : AlexNet, ZFNet, VGGNet, ResNet Review: ZFNet โ€” Winner of ILSVRC 2013 (Image Classification) Docs ยป Computer vision ยป Convolutional Neural Network ยป ZFNet 2) VGG, GoogLeNet(๋ ˆ์ด์–ด 22๊ฐœ ์ดํ•˜) ์šฐ๋ฆฌ๋Š” ์•ž์„œ 8 layer ์ดํ•˜์˜ ๋น„๊ต์  ์ ์€ ์ˆ˜์˜ layer๋ฅผ ๊ฐ€์ง„ CNN model ๋“ค์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ์‚ดํŽด๋ณผ VGG, GoogleNet์˜ ๋ถ€ ํ„ฐ๋Š” layer๊ฐ€ ๋” ๊นŠ๊ฒŒ ์Œ“์ด๊ธฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋…ผ๋ฌธ ๋ชจ๋‘ ๋…ผ๋ฌธ ์ œ๋ชฉ์—์„œ๋ถ€ํ„ฐ ๋ณธ์ธ๋“ค์ด layer๋ฅผ ๊นŠ๊ฒŒ ์Œ“์•˜์Œ์„ ๊ฐ•์กฐํ•˜๊ณ  ์žˆ์„ ์ •๋„๋กœ, ๋” ๋„คํŠธ์›Œํฌ๋ฅผ ๊นŠ๊ฒŒ ๋งŒ๋“ค์—ˆ์Œ์€ ์ด์ „ ๋ชจ๋ธ๋“ค๊ณผ์˜ ํ•ต์‹ฌ์ ์ธ ์ฐจ์ด์ ์ด์ž, ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚ด๋Š” ์ด์œ ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋” ๊นŠ์€ ๋„คํŠธ์›Œํฌ๊ฐ€ ์ข‹์€ ์ด์œ ๋Š” 2) ๋” ๊นŠ์€ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ๋“ค์—์„œ ๊ฐ„๋žตํžˆ ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋„คํŠธ์›Œํฌ๋ฅผ ๊นŠ๊ฒŒ ์Œ“๋Š”๋‹ค๋Š” ๊ฒƒ์€ ์ƒ๊ฐ๋ณด๋‹ค ๋‹น์‹œ์—๋Š” ์‰ฌ์šด ์ผ์ด ์•„๋‹ˆ์—ˆ์Šต๋‹ˆ๋‹ค. Gradient vasnishing /exploding ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ณ , ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜ overfitting์„ ์•ผ๊ธฐํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์„ค๋ช…ํ•  VGG, GoogLeNet, ResNet์€ ์ „์ˆ ํ•œ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ์ด๋Ÿฐ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•˜๋Š”์ง€ ์ง‘์ค‘ํ•ด์„œ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. VGG-16 / VGG-19 VGGNet์€ ILSVRC' 14์—์„œ<NAME>์Šน์„<NAME> ๋ชจ๋ธ๋กœ, ๋‹น์‹œ 7.3 %์˜ Top 5 error๋ฅผ ๋ณด์˜€์Šต๋‹ˆ๋‹ค. 13๋…„๋„์— ์šฐ์Šนํ–ˆ๋˜ ZFNet ์ด 11.7%์˜ Top 5 error๋ฅผ ๋ณด์˜€๋˜ ๊ฒƒ์„ ๊ฐ์•ˆํ•˜๋ฉด ์ƒ๋‹นํžˆ ๋งŽ์ด ํ–ฅ์ƒ๋œ ๊ฒƒ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. AlexNet, ZFNet์ด ๋ชจ๋‘ 8๊ฐœ์˜ ์ธต์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ฉด ๋„คํŠธ์›Œํฌ๋ฅผ 16-19 ์ธต๊นŒ์ง€ ์Œ“์•„ VGG๋ฅผ ๊ธฐ์ ์œผ๋กœ ๋„คํŠธ์›Œํฌ์˜ ๊ตฌ์กฐ๊ฐ€ ๋งŽ์ด ๊นŠ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. VGGNet์˜ ํŠน์ง• 3x3์˜, ๋ณด๋‹ค ์ž‘์€ ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ( VGG ์ด์ „์—๋Š” 5x5๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ) ์ž‘์€ ํ•„ํ„ฐ๊ฐ€ ์ฃผ๋Š” ํšจ๊ณผ ํ•„ํ„ฐ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์ด๋ฉด์„œ ๋ณด๋‹ค ๊นŠ๊ฒŒ ์Œ“์•˜์„ ๋•Œ ๋” ํšจ์œจ์ ์ธ receptive field๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋” ๋„“์€ ํ•„ํ„ฐ๋ฅผ ์“ฐ๊ณ  ์–‡์€ ์ธต์„ ์Œ“๋Š” ๊ฒƒ์ด๋‚˜, ์ž‘์€ ํ•„ํ„ฐ๋ฅผ ์“ฐ๊ณ  ๊นŠ๊ฒŒ ์Œ“๋Š” ๊ฒƒ์ด๋‚˜ receptive field๊ฐ€ ๊ฐ™๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. Layer๊ฐ€ ๊นŠ์–ด์ง€๋ฉด์„œ ๋‹ค์ˆ˜์˜ activation function์„ ํ†ต๊ณผํ•˜๋ฏ€๋กœ ๋” ๋งŽ์€ non-linearity๋ฅผ ์ค„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ธต ๋‹น ๋” ์ ์€ ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ex) 10ร—10 image์— 7ร—7 filter ์ ์šฉํ•˜์—ฌ 4ร—4 feature map ์ƒ์„ฑ โ†’ parameter ๊ฐœ์ˆ˜: 49๊ฐœ ex) 10ร—10 image์— 3ร—3 filter 3๋ฒˆ ์ ์šฉํ•˜์—ฌ 4ร—4 feature map ์ƒ์„ฑ โ†’ parameter ๊ฐœ์ˆ˜: 9๊ฐœ์”ฉ 3๋ฒˆ ์ด 27๊ฐœ padding์„ ์ด์šฉํ•ด ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ๋ฅผ ์œ ์ง€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. padding์ด ์ฃผ๋Š” ํšจ๊ณผ ์ด์ „์—๋Š” Convolution ์—ฐ์‚ฐ์˜ ํŠน์ง•์ƒ Layer๊ฐ€ ๊นŠ์–ด์ง€๊ฒŒ ๋˜๋ฉด ๊ฐ€์žฅ์ž๋ฆฌ ๋ถ€๋ถ„์ด ์ฃผ๋Š” ์˜ํ–ฅ๋ ฅ์ด ์ ์  ์ค„์–ด๋“ค๊ฒŒ ๋˜๊ณ  ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ VGG๋ถ€ํ„ฐ๋Š” padding์„ ๋„์ž…ํ•˜๋ฉด์„œ Network๊ฐ€ ๊นŠ์–ด์ ธ๋„ ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Framework ๋ณ„ ์‚ฌ์šฉ๋ฒ• VGG๋Š” ๋Œ€๋ถ€๋ถ„์˜ deep learning framework(pytorch, tensorflow ๋“ฑ)์—์„œ pre-trained ๋œ ๋ชจ๋ธ์„ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•„์š”์— ๋”ฐ๋ผ์„œ๋Š”, pre-trained๋œ weight ์—†์ด network architecture๋งŒ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Keras Keras์—์„œ๋Š” keras.applications ๋ชจ๋“ˆ์—์„œ import ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. keras์—์„œ๋Š” VGG 16, 19 ๋ชจ๋‘ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค๋งŒ, keras document์— ์žˆ๋Š” VGG16์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋ณด๋ฉด์„œ ์‚ฌ์šฉ๋ฒ•์„ ์ตํ˜€๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. tf.keras.applications.VGG16( include_top=True, weights="imagenet", input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation="softmax", ) ์‹ค์ œ๋กœ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ด์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. VGG16, 19 ๋ชจ๋‘ kereas.Model object๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค. model.summary() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์ „์ฒด ๊ตฌ์กฐ๋ฅผ ์ถœ๋ ฅํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from keras.applications import vgg16 model = vgg16.VGG16(weights ='imagenet', include_top = True) model.summary() model.summary()์˜ output, VGG16 ๊ตฌ์กฐ (์ฃผ์˜) PyTorch PyTorch์—์„œ๋Š” pytorch hub๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„๋œ architecture๋ฅผ ๋ฐ›์„ ์ˆ˜๋„ ์žˆ๊ณ , torchvision ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณต์‹์ ์œผ๋กœ๋Š” vgg11, vgg16, vgg 19 ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋ชจ๋ธ์— batch normalization์ด ์ถ”๊ฐ€๋œ ๋ฒ„์ „์œผ๋กœ ์ด 6๊ฐ€์ง€๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณต์‹ ์‚ฌ์ดํŠธ์—์„œ ๊ฐ„๋‹จํ•œ classification ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. (https://pytorch.org/hub/pytorch_vision_vgg/) torch hub import torch model = torch.hub.load('pytorch/vision:v0.10.0', 'vgg16', pretrained=True) # pre-trained weight๊ฐ€ ํ•„์š” ์—†์„ ๋•Œ๋Š” False torchvision import torchvision model = torchvision.models.vgg16(pretrained=True) Reference cs231n VGGNet paper : Very Deep Convolutional Networks for Large-Scale Image Recognition PIEW9|VGGNET GoogLeNet (inception - v1) ์˜ํ™” Inception์—์„œ๋Š”, ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ๊ฟˆ์— ์ ‘์†ํ•˜๊ณ , ๊ฟˆ์†์˜ ๊ฟˆ์— ์ ‘์†ํ•˜๊ณ , ๊ฟˆ์†์˜ ๊ฟˆ์†์˜ ๊ฟˆ์— ์ ‘์†ํ•˜๋Š” ์‹์œผ๋กœ ๊ณ„์† ๊นŠ์ด ๋“ค์–ด๊ฐ€ ํ‘œ์ ์˜ ๋ฌด์˜์‹์— ๋„๋‹ฌํ•ด ์–ด๋–ค ์ƒ๊ฐ์„ ์‹ฌ์–ด์ฃผ๊ฑฐ๋‚˜ ํ•˜๋Š” ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. GoogleNet์˜ ํ•ต์‹ฌ์ด ๋˜๋Š” Inception module์˜ ์ด๋ฆ„์ด ์ด ์˜ํ™”์—์„œ ๋น„๋กฏ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. GoogleNet์€ ILSVRC' 14์—์„œ ์šฐ์Šน์„ ์ฐจ์ง€ํ–ˆ์œผ๋ฉฐ, "Going Deeper with Convolutions"๋ผ๋Š” ๋…ผ๋ฌธ์˜ ์ œ๋ชฉ๋‹ต๊ฒŒ ์ „๋…„๋„์— ์šฐ์Šนํ•œ ZFNet๋ณด๋‹ค ๋ฌด๋ ค 14์ธต์„ ๋” ์Œ“์•„, 22์ธต์˜ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” GoogleNet๋ณด๋‹ค ์„ฑ๋Šฅ์ด ๋” ์ข‹์€ ๋„คํŠธ์›Œํฌ๋“ค์ด ๋งŽ์ด ๋“ฑ์žฅํ–ˆ์ง€๋งŒ, Inception module์ด๋ผ๋Š” ์•„์ด๋””์–ด๋Š” ์ตœ๊ทผ๊นŒ์ง€๋„ ๋‹ค๋ฅธ ๋ชจ๋ธ์—์„œ ์‘์šฉ๋˜๊ธฐ๋„ ํ•˜๊ณ  ๋˜ Classification์— ์“ฐ์ด์ง€๋Š” ์•Š๋”๋ผ๋„, ์ข‹์€ Feature selector ๋กœ์„œ ์ปดํ“จํ„ฐ ๋น„์ „ ๋ถ„์•ผ์˜ ๋‹ค๋ฅธ ๋ฌธ์ œ ํ•ด๊ฒฐ์— ์•„์ง๊นŒ์ง€๋„ ํ™œ๋ฐœํžˆ ์ด์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. GoogleNet์˜ ํŠน์ง• 22 layers ํšจ๊ณผ์ ์ธ "Inception" module FC layer ์—†์Œ (Output layer ์—์„œ๋งŒ ํ•œ๋ฒˆ ๋‚˜์˜ด) ์˜ค์ง 500๋งŒ ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์‚ฌ์šฉ ILSVRC'14 ์šฐ์Šน, 6.7 % top 5 error Inception module GoogLeNet์€ Inception module์ด ์—ฌ๋Ÿฌ ์ฐจ๋ก€ ๋ฐ˜๋ณต๋˜๋Š” ํ˜•ํƒœ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ง์ ‘ ํ•˜๋‚˜ํ•˜๋‚˜ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ธฐ ์ „์— ์–ด๋Š ์œ„์น˜์— ์–ด๋–ค ํฌ๊ธฐ์˜ ํ•„ํ„ฐ๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด optimal ์ธ์ง€, ํ˜น์€ ์ด ์ž๋ฆฌ์— pooling์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒŒ optimal ์ผ ์ง€ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ์š”? VGG๋Š” 3x3์„ ์“ฐ์ง€๋งŒ ๊ณผ์—ฐ 3x3์ด optimal์ผ๊นŒ์š”? ์šฐ๋ฆฌ๋Š” ๋ชจ๋ฆ…๋‹ˆ๋‹ค. Inception module์˜ ๊ธฐ๋ณธ์ ์ธ ์•„์ด๋””์–ด๋Š” ์šฐ๋ฆฌ๋Š” ๋ญ๊ฐ€ optimal ์ธ์ง€ ๋ชจ๋ฅด์ง€๋งŒ! ๋‹คํ•ด์„œ ๋„ฃ์–ด๋ณด์ž ๊ทธ๋Ÿผ optimal์„ ๋ฝ‘์•„๋‚ด๋„๋ก ํ•™์Šตํ•˜๊ฒ ์ง€!! ์ž…๋‹ˆ๋‹ค. (ํฅ๋ฏธ๋กญ๊ฒŒ๋„ ๊ฐ™์€ Google์—์„œ Semantic segmentation์„ ์œ„ํ•ด ์ œ์‹œํ•œ DeepLab๊ณผ๋„ ์ฒ ํ•™์ด ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.) ๊ทธ๋ž˜์„œ 1x1 convolution, 3x3 convolution, 5x5 convolution ๊ทธ๋ฆฌ๊ณ  3x3 max pooling 4๊ฐ€์ง€๋ฅผ ๋ณ‘๋ ฌ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  output์„ concat ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด naive inception module์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Š” ์‹ค์ œ๋กœ๋Š” ์ž˜ ์ž‘๋™ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ€์ง€ convolution ๋ฐ pooling์˜ output์„ ํ•˜๋‚˜๋กœ concat ํ•˜๋Š” ๊ฒƒ์€ dimension์„ ์ƒ๋‹นํžˆ ๋Š˜๋ฆด ์ˆ˜๋ฐ–์— ์—†์—ˆ๊ณ  ๋„ˆ๋ฌด๋‚˜๋„ ๋น„ํšจ์œจ์ ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋ฒˆ์—๋Š” Inception module ์•ˆ์— 1x1 convolution ์ธต์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. Convolution ์—ฐ์‚ฐ์€ ์—ฐ์‚ฐ๋Ÿ‰์ด ๋งŽ์œผ๋‹ˆ๊นŒ, Convolution ์—ฐ์‚ฐ ์ „์— dimension์„ ์ค„์—ฌ์ฃผ๊ณ , max pooling์€ Convolution์— ๋น„ํ•ด ๊ฐ„๋‹จํ•˜๋‹ˆ ์—ฐ์‚ฐ ์ดํ›„์— dimension์„ ์ค„์—ฌ ๋ชจ๋“  output์„ concat ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ inception์€ ๊ณ„์‚ฐ์ƒ์˜ ์–ด๋ ค์›€ ์—†์ด layer๋ฅผ ๋Š˜๋ฆด ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ์ ์œผ๋กœ ์ˆ˜ํ–‰๋œ ์—ฐ์‚ฐ๋“ค์€ Inception module์„ ๋‚˜์˜ค๋ฉด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด Concat ๋˜์–ด ๋‹ค์Œ Layer๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. 1x1 Convolution์˜ ์—ญํ•  Inception module ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” 1x1 Convolution์€ Network in Network (Lin et al)์—์„œ ์†Œ๊ฐœ๋œ ๊ฐœ๋…์œผ๋กœ inception์—์„œ๋Š” Bottleneck layer๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 1x1 convolution์€ Dimension์ด ๊นŠ์€ network์— ๋Œ€ํ•ด ๊ฐ ํฌ์ธํŠธ๋งˆ๋‹ค ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ์ •๋ณด๋ฅผ ๋ฝ‘์•„๋‚ด๋Š” ์—ญํ• ์„ ํ•œ๋‹ค๊ณ ๋„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Inception์—์„œ๋Š” Channel ์ˆ˜๋ฅผ ์ค„์—ฌ ๊ณ„์‚ฐ์ƒ์—์„œ์˜ ์ด์ ์„ ๋ณด๋„๋ก ๋„์™€์ฃผ๊ณ , ๋น„์„ ํ˜•์„ฑ์„ ์ฆ๊ฐ€์‹œ์ผœ ๋” ๋ณต์žกํ•œ ํ•จ์ˆ˜๋„ approximation ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. Inception์˜ Auxilary classifier GoogleNet์—๋Š” Auxilary classifier๋ผ๊ณ  ํ•˜๋Š” ์ค‘๊ฐ„์ค‘๊ฐ„์— Output์„ ๋‚ด๋Š” ๊ตฌ์กฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. GoogleNet์—์„œ ์ฒ˜์Œ ๋„์ž…ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ, ์ค‘๊ฐ„์ค‘๊ฐ„์— Output์„ ๋จผ์ € ๋งŒ๋“ค๊ณ  ์ด๋ฅผ Back propagation ์‹œ ๋ฐ˜์˜ํ•ด Layer๊ฐ€ ๊นŠ์–ด์ง์— ๋”ฐ๋ผ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” Gradient vanishing / explosion ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ œ์ผ ์ดˆ์ฐฝ๊ธฐ ๋ชจ๋ธ์ธ GoogleNet (Inception-v1)์˜ ๊ฒฝ์šฐ์—๋Š” Auxilary classifier๋ฅผ 2๊ฐœ ์ผ์œผ๋‚˜, Inception-v2, v3๋ถ€ํ„ฐ๋Š” Auxilary classifier๋ฅผ ํ•˜๋‚˜๋กœ ์ค„์˜€๊ณ , Inception-v4๋ถ€ํ„ฐ๋Š” Auxilary classifier๋ฅผ ์•„์˜ˆ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์™œ ์š”์ฆ˜์€ Auxilary classifier๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„๊นŒ์š”? Auxilary classifier์—๋Š” ํ•œ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๋ฐ˜ Layer์—์„œ output์„ ๋‚ด๊ณ  ์ด๊ฑธ Backpropagation์— ๋ฐ˜์˜ํ•˜๋ฉด ๋‹น์—ฐํ•˜๊ฒŒ๋„ ์ดˆ๋ฐ˜ Layer์—์„œ ์ž˜ Classification์„ ์ž˜ ํ•™์Šต์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์ด ์–ด๋Š ์ •๋„ ๋ฐ˜์˜๋˜์–ด ํ•™์Šต์ด ์ง„ํ–‰๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ์ตœ์ข… Classification layer์—์„œ Optimal ํ•œ feature๊ฐ€ ๋ฝ‘ํžˆ์ง€ ์•Š๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Framework ๋ณ„ ์‚ฌ์šฉ๋ฒ• VGG ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋Œ€๋ถ€๋ถ„์˜ deep learning frame work์—์„œ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Keras Keras์—์„œ๋Š” keras.applications ๋ชจ๋“ˆ์—์„œ import ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Keras document์— ์žˆ๋Š” InceptionV3์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋ณด๋ฉด์„œ ์‚ฌ์šฉ๋ฒ•์„ ์ตํ˜€๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. tf.keras.applications.InceptionV3( include_top=True, # classification (softmax) ๋ถ€๋ถ„์„ ํฌํ•จํ•  ๊ฒƒ์ธ์ง€ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. weights="imagenet", # imageNet์œผ๋กœ pre-trained ๋œ ๋ชจ๋ธ์„ ๋ฐ›์„ ๊ฒƒ์ธ์ง€๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ pre-trained ๋œ ๋ชจ๋ธ์„ ๋ฐ›๊ธธ ์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด None์œผ๋กœ ๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation="softmax", ) ์‹ค์ œ๋กœ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ด์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. InceptionV3๋Š” kereas.Model object๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค. model.summary() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์ „์ฒด ๊ตฌ์กฐ๋ฅผ ์ถœ๋ ฅํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from keras.applications import inception_v3 model = inceotion_v3.InceptionV3(weights='imagenet', include_top=True) model.summary() model.summary()์˜ output, Inception V3์˜ ๊ตฌ์กฐ (์ฃผ์˜) Pytorch VGG์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ torch hub์™€ torchvision ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Inception - V3์™€ GoogLeNet์„ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณต์‹ ์‚ฌ์ดํŠธ์—์„œ ๊ฐ„๋‹จํ•œ classification ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. (https://pytorch.org/hub/pytorch_vision_inception_v3/ torchhub import torch model = torch.hub.load('pytorch/vision:v0.10.0', 'inception_v3', pretrained=True) torchvision import torchvision model = torchvision.models.inception_v3(pretrained=True) Reference Youtube|Idea Factory KAIST ๋…ผ๋ฌธ : Going Deeper with Convolutions (Szegedy et al) cs231n C4W2L05 Network In Network 3) ResNet, ResNet์˜ ํ™•์žฅ(๋ ˆ์ด์–ด 152๊ฐœ ์ดํ•˜) ResNet CNN์„ ์—ฐ๊ตฌํ•˜๋ฉด์„œ ๊ธฐ์กด ๋ชจ๋ธ๋“ค์€ Layer์„ ๊นŠ๊ฒŒ ์Œ“์„์ˆ˜๋ก ์„ฑ๋Šฅ์ด ๋” ์ข‹์•„์งˆ ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ–ˆ์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” 20์ธต ์ด์ƒ๋ถ€ํ„ฐ ์„ฑ๋Šฅ์ด ๋‚ฎ์•„์ง€๋Š” ํ˜„์ƒ์ธ Degradation ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ResNet์€ Residual Learning์ด๋ผ๋Š” ๊ฐœ๋…์„ ํ†ตํ•ด ๋ชจ๋ธ์˜ ์ธต์ด ๊นŠ์–ด์ ธ๋„ ํ•™์Šต์ด ์ž˜ ๋˜๋„๋ก ๊ตฌํ˜„ํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ResNet์€ ๋ฌด๋ ค 152์ธต๊นŒ์ง€ ๋„คํŠธ์›Œํฌ๋ฅผ ์Œ“์œผ๋ฉฐ ILSVRC'15์™€ COCO'15์—์„œ ์šฐ์Šนํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฒ˜์Œ์œผ๋กœ Human error๋ฅผ ๋Šฅ๊ฐ€ํ•˜๋Š” 3.57%์˜ top5 error๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. ResNet์—์„œ ์ œ์‹œํ•˜๋Š” Residual Learning์ด๋ผ๋Š” ๊ฐœ๋…์€ ์ดํ›„ ๋งŽ์€ ๋ชจ๋ธ๋“ค์—์„œ ์‘์šฉ๋˜์—ˆ์œผ๋ฉฐ, ์•„์ง๊นŒ์ง€๋„ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์„ฑ๋Šฅ๋„ ์ข‹๊ณ , ๊ตฌํ˜„๋„ ํŽธํ•˜๊ณ  ๋‹จ์ˆœํ•˜๋‹ค๋Š” ์  ๋•Œ๋ฌธ์— ResNet์„ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งค์šฐ ๊นŠ์€ ๋„คํŠธ์›Œํฌ์˜ ๋ฌธ์ œ์  Inception๊ณผ VGG๊ฐ€ ๋ณด๋‹ค '๊นŠ์€' ๋„คํŠธ์›Œํฌ์ž„์„ ๊ฐ•์กฐํ•˜๋ฉด์„œ ILSVRC์—์„œ ์ข‹์€ ์„ฑ์ ์„ ๊ฑฐ๋‘๊ฒŒ ๋˜์—ˆ์ง€๋งŒ ์‚ฌ๋žŒ๋“ค์€ ๊ณผ์—ฐ ๋„คํŠธ์›Œํฌ๊ฐ€ ๊นŠ์–ด์ง€๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์„๊นŒ๋ผ๋Š” ์˜๋ฌธ์„ ๊ฐ€์ง€๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. "plain" convolutional neural network์—์„œ Layer๋ฅผ ๋ฌด์ž‘์ • ๋Š˜๋ ธ์„ ๋•Œ ์„ฑ๋Šฅ์ด ์˜คํžˆ๋ ค ๋–จ์–ด์กŒ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. Training์—์„œ๋„, Test์—์„œ๋„ ์„ฑ๋Šฅ์ด ์ข‹์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ์ด Overfitting ๋•Œ๋ฌธ์ด ์•„๋‹˜์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ง๊ด€์ ์œผ๋กœ ์šฐ๋ฆฌ๋Š” Gradient vanishing / explosion์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ ์™ธ์—๋„ ์šฐ๋ฆฌ๋Š” Degradation Problem์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”, layer๊ฐ€ ์–ด๋Š ์ •๋„ ์ด์ƒ์œผ๋กœ ๊นŠ์–ด์ง€๋ฉด ์˜คํžˆ๋ ค ์„ฑ๋Šฅ์ด ์•ˆ ์ข‹์•„์ง€๋Š” ํ˜„์ƒ์ด ๋ฒŒ์–ด์ง์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ˜„์ƒ์˜ ์›์ธ์„ ์–ด๋–ค ์‚ฌ๋žŒ๋“ค์€ layer๊ฐ€ ๊นŠ์–ด์กŒ์„ ๋•Œ Optimization์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์•„์„œ๋ผ๊ณ  ์ถ”์ธกํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ์— Optimization ๋ฌธ์ œ๋ผ๋ฉด ์šฐ๋ฆฌ๋Š” 1) ์ƒˆ๋กœ์šด Optimizer๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜, 2) ๊นŠ์–ด์ง€๋”๋ผ๋„ ์‰ฝ๊ฒŒ Optimization์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒˆ๋กœ์šด Architecture๋ฅผ ๋งŒ๋“ค์–ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Optimizer๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๋งค์šฐ ์–ด๋ ค์šด ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์€ ์ƒˆ๋กœ์šด Network๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์ง‘์ค‘ํ–ˆ์Šต๋‹ˆ๋‹ค. Residual block H(x)๋ฅผ ๊ธฐ์กด์˜ ๋„คํŠธ์›Œํฌ๋ผ๊ณ  ํ•  ๋•Œ, H(x)๋ฅผ ๋ณต์žกํ•œ ํ•จ์ˆ˜์— ๊ทผ์‚ฌ ์‹œํ‚ค๋Š” ๊ฒƒ๋ณด๋‹ค F(x) := H(x) - x ์ผ ๋•Œ, H(x) = F(x) + x์ด๊ณ , F(x) + x๋ฅผ ๊ทผ์‚ฌ ์‹œํ‚ค๋Š” ๊ฒƒ์ด ๋” ์‰ฌ์šธ ๊ฒƒ์ด๋ผ๋Š” ์•„์ด๋””์–ด์—์„œ ์ถœ๋ฐœํ•ฉ๋‹ˆ๋‹ค. ์›๋ž˜ Output์—์„œ ์ž๊ธฐ ์ž์‹ ์„ ๋นผ๋Š” ๊ฒƒ์ด F(x)์˜ ์ •์˜์ด๋ฏ€๋กœ, 'Residual learning'์ด๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, x๊ฐ€ F(x)๋ฅผ ํ†ต๊ณผํ•˜๊ณ  ๋‚˜์„œ ๋‹ค์‹œ x๋ฅผ ๋”ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ Skip Connection์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. x : ์ž…๋ ฅ๊ฐ’ F(x) : CNN Layer -> ReLU -> CNN Layer ์„ ํ†ต๊ณผํ•œ ์ถœ๋ ฅ๊ฐ’ H(x) : CNN Layer -> ReLU -> CNN Layer -> ReLU๋ฅผ ํ†ต๊ณผํ•œ ์ถœ๋ ฅ๊ฐ’ ๊ธฐ์กด ์‹ ๊ฒฝ๋ง์€ H(x)๊ฐ€ ์ •๋‹ต ๊ฐ’ y์— ์ •ํ™•ํžˆ ๋งคํ•‘์ด ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰ ์‹ ๊ฒฝ๋ง์€ ํ•™์Šต์„ ํ•˜๋ฉด์„œ H(x) -y์˜ ๊ฐ’์„ ์ตœ์†Œํ™”์‹œํ‚ค๋ฉด์„œ ๊ฒฐ๊ตญ H(x) = y๊ฐ€ ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ H(x)๋Š” Identity๋ฅผ ๋งคํ•‘ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— H(x)-x๋ฅผ ์ตœ์†Œํ™”ํ•˜๋ฉด์„œ H(x) = x ๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด ์‹ ๊ฒฝ๋ง์ด H(x) - x = 0์„ ๋งŒ๋“ค๋ ค ํ–ˆ๋‹ค๋ฉด ResNet์€ H(x) - x = F(x)๋กœ ๋‘์–ด F(x)๋ฅผ ์ตœ์†Œํ™”์‹œํ‚ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ F(x) = 0์ด๋ผ๋Š” ๋ชฉํ‘œ๋ฅผ ๋‘๊ณ  ํ•™์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๋ฉด F(x) = 0์ด๋ผ๋Š” ๋ชฉํ‘ฏ๊ฐ’์ด ์ฃผ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ํ•™์Šต์ด ๋” ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ H(x) = F(x) + x ๊ฐ€ ๋˜๋Š”๋ฐ ์ด๋•Œ ์ž…๋ ฅ๊ฐ’์ธ x๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์“ฐ๋Š” ๊ฒƒ์ด Skip Connection์ž…๋‹ˆ๋‹ค. ์ฆ‰ Skip Connection์€ ์ž…๋ ฅ ๊ฐ’์ด ์ผ์ • ์ธต๋“ค์„ ๊ฑด๋„ˆ๋›ฐ์–ด ์ถœ๋ ฅ์— ๋”ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ์ฒ˜๋Ÿผ ์ผ๋ฐ˜์ ์ธ CNN์—์„œ ๋‚˜ํƒ€๋‚˜๋Š” "main path"์™€ Skip connection์— ์˜ํ•ด ์—ฐ๊ฒฐ๋˜๋Š” "short cut"์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Plain neural network์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ Layer๋ฅผ ์ƒ๋‹นํžˆ ๊นŠ๊ฒŒ ์Œ“์•˜์Œ์—๋„ ์ ๊ฒŒ ์Œ“์•˜์„ ๋•Œ๋ณด๋‹ค ์—๋Ÿฌ๊ฐ€ ๋‚ฎ์œผ๋ฉฐ degradation problem์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์€ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Plain๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ ResNet์€ ์•„๋ž˜์™€ ๊ฐ™์ด Shortcut์œผ๋กœ ์ด์–ด์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Deeper bottleneck architecture 50์ธต ์ด์ƒ์˜ ๊นŠ์€ ๋ชจ๋ธ์—์„œ๋Š” Inception์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์—ฐ์‚ฐ ์ƒ์˜ ์ด์ ์„ ์œ„ํ•ด "bottleneck" layer (1x1 convolution)์„ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ Residual Block์€ ํ•œ ๋ธ”๋ก์— Convolution Layer(3X3) 2๊ฐœ๊ฐ€ ์žˆ๋Š” ๊ตฌ์กฐ์˜€์Šต๋‹ˆ๋‹ค. Bottleneck ๊ตฌ์กฐ๋Š” ์˜ค๋ฅธ์ชฝ ๊ทธ๋ฆผ์˜ ๊ตฌ์กฐ๋กœ ๋ฐ”๊พธ์—ˆ๋Š”๋ฐ ์ธต์ด ํ•˜๋‚˜ ๋” ์ƒ๊ฒผ์ง€๋งŒ Convolution Layer(1X1) 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๊ฐ€ ๊ฐ์†Œํ•˜์—ฌ ์—ฐ์‚ฐ๋Ÿ‰์ด ์ค„์–ด๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ Layer๊ฐ€ ๋งŽ์•„์ง์— ๋”ฐ๋ผ Activation Function์ด ์ฆ๊ฐ€ํ•˜์—ฌ ๋” ๋งŽ์€ non-linearity๊ฐ€ ๋“ค์–ด๊ฐ”์Šต๋‹ˆ๋‹ค. ์ฆ‰ Input์„ ๊ธฐ์กด๋ณด๋‹ค ๋‹ค์–‘ํ•˜๊ฒŒ ๊ฐ€๊ณตํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ ResNet์€ Skip Connection์„ ์ด์šฉํ•œ Shortcut๊ณผ Bottleneck ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋” ๊นŠ๊ฒŒ ์ธต์„ ์Œ“์„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ResNet ์ดˆ์ฐฝ๊ธฐ ๋…ผ๋ฌธ์—์„œ๋Š” 110 Layer์—์„œ ๊ฐ€์žฅ ์ ์€ ์—๋Ÿฌ๊ฐ€ ๋‚˜์™”๊ณ , 1000๊ฐœ ์ด์ƒ์˜ layer๊ฐ€ ์Œ“์˜€์„ ๋•Œ๋Š” ์˜ค๋ฒ„ ํ”ผํŒ…์ด ์ผ์–ด๋‚ฌ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ResNet์ด ์ž˜ ๋˜๋Š” ์ด์œ  ์› ๋…ผ๋ฌธ์—์„œ๋Š” ์ž์‹ ๋“ค์ด ์–ด๋–ค ์ด๋ก ์„ ๋ฐ”ํƒ•์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ–ˆ๋‹ค๊ธฐ๋ณด๋‹ค๋Š” ๊ฒฝํ—˜์ ์œผ๋กœ residual block์„ ์“ฐ๋‹ˆ ๊ฒฐ๊ณผ๊ฐ€ ์ข‹๊ฒŒ ๋‚˜์™”๋‹ค๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ResNet์ด ์™œ ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ์„ค๋ช…ํ•˜๋ ค๋Š” ๋งŽ์€ ๋…ธ๋ ฅ์ด ์žˆ์—ˆ๊ณ , ์•„์ง๊นŒ์ง€๋„ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์—ฐ๊ตฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘์—์„œ ์†Œ๊ฐœํ•  ํ•œ ๊ฐ€์ง€๋Š” ๋ฐ”๋กœ Residual Net์ด ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋ชจ๋ธ์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์€ Optimal depth์—์„œ์˜ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๋‹ค๋Š” ๊ฐ€์„ค์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์‰ฝ๊ฒŒ Optimal depth๋ฅผ ์•Œ ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. 20์ธต์ด Optimal ์ธ์ง€, 30์ธต์ด optimal ์ธ์ง€, 100์ธต์ด optimal ์ธ์ง€ ์•„๋ฌด๋„ ๋ชจ๋ฆ…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, degradation problem์€ ์•ผ์†ํ•˜๊ฒŒ๋„ ์šฐ๋ฆฌ๋Š” ์•Œ ์ˆ˜ ์—†๋Š” optimal depth๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ๋ฐ”๋กœ ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค. ResNet์€ ์—„์ฒญ๋‚˜๊ฒŒ ๊นŠ์€ ๋„คํŠธ์›Œํฌ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ , Optimal depth์—์„œ์˜ ๊ฐ’์„ ๋ฐ”๋กœ Output์œผ๋กœ ๋ณด๋‚ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒŒ ์–ด๋–ป๊ฒŒ ๊ฐ€๋Šฅํ• ๊นŒ์š”? ๋ฐ”๋กœ Skip connection ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ResNet์€ Skip connection์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— Main path์—์„œ Optimal depth ์ดํ›„์˜ Weight์™€ Bias๊ฐ€ ์ „๋ถ€ 0์— ์ˆ˜๋ ดํ•˜๋„๋ก ํ•™์Šต๋œ๋‹ค๋ฉด Optimal depth์—์„œ์˜ Output์ด ๋ฐ”๋กœ Classification์œผ๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰ Optimal depth ์ดํ›„์˜ block์€ ๋ชจ๋‘ ๋นˆ ๊นกํ†ต์ด๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 27์ธต์ด Optimal depth์ธ๋ฐ ResNet 50์—์„œ ํ•™์Šต์„ ํ•œ๋‹ค๋ฉด, 28์ธต๋ถ€ํ„ฐ Classification ์ „๊นŒ์ง€์˜ weight์™€ bias๋ฅผ ์ „๋ถ€ 0์œผ๋กœ ๋งŒ๋“ค์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด 27์ธต์—์„œ์˜ output์ด ๋ฐ”๋กœ Classification์—์„œ ์ด์šฉ๋˜๊ณ , ์ด๋Š” Optimal depth์˜ ๋„คํŠธ์›Œํฌ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ResNet์˜ ํ™•์žฅ Identify Mappings in Deep Residual Networks Wide Residual Networks ResNeXt Deep Networks with Stochastic Depth SeNet DenseNet Reference DeepLearning AI | C4W2L03 Resnets cs231n ์› ๋…ผ๋ฌธ: Deep Residual Learning for Image Recognition (He et al.) Residual block ์‚ฌ์ง„ : https://mole-starseeker.tistory.com/12 4) Vision transformer(โ˜…์ž‘์„ฑ ์ค‘) NLP ๋ถ„์•ผ์—์„œ์˜ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ปดํ“จํ„ฐ ๋น„์ „์— ์ ์šฉํ•œ ๋„คํŠธ์›Œํฌ์ž…๋‹ˆ๋‹ค. CNN๋งŒ ์ด์šฉํ•œ ๋„คํŠธ์›Œํฌ ๋ณด๋‹ค ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ณ…ํ„ฐ1์—์„œ ์ •๋ฆฌํ•œ Transformer์„ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์ „์ œํ•˜์— AN IMAGE IS WORTH 16X16 WORDS : TRANSFORMERS FOR IMAGE RECOGNITION AT SCALE ๋…ผ๋ฌธ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์–ด๋–ป๊ฒŒ Transformer์— ์ ์šฉํ–ˆ๋Š”์ง€ ์ค‘์‹ฌ์œผ๋กœ ๋ฆฌ๋ทฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์š”์•ฝ ๋…ผ๋ฌธ์—์„œ ์ œ์‹œํ•œ ViT๋Š” NLP ๋ถ„์•ผ์—์„œ์˜ Transformer์˜ Encoder ๋ถ€๋ถ„(Self-Attention)์„ ๊ทธ๋Œ€๋กœ ์‘์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์ด ์ฃผ๋Š” ๊ฐ•๋ ฅํ•œ ๋ฉ”์‹œ์ง€๋Š” Vision Task์—์„œ CNN์„ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ์ถฉ๋ถ„ํ•œ ํผํฌ๋จผ์Šค๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ๋ณด์—ฌ์คฌ๋‹ค๋Š” ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๋‹จ์  ์—ญ์‹œ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ถฉ๋ถ„ํ•œ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ Pretrain ํ•˜๊ณ  ํƒ€๊นƒ ๋„๋ฉ”์ธ์— Fine-tuningํ•ด์•ผ ์„ฑ๋Šฅ์„ ์ œ๋Œ€๋กœ ๋ฐœํœ˜ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ ํ‘œํ˜„ํ•œ ํ•œ ์žฅ์˜ ๋Œ€ํ‘œ ์ด๋ฏธ์ง€๊ฐ€ ๋ชจ๋“  ๊ณผ์ •์„ ์ž˜ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ๋Œ€ํ‘œ ์ด๋ฏธ์ง€ ํ•˜๋‚˜๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ณ  ViT ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆœ์„œ๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ€ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ 1. Image Patch ๋งŒ๋“ค๊ธฐ ํŠธ๋žœ์Šคํฌ๋จธ๋Š” NLP ๋ถ„์•ผ์—์„œ ์ถœ๋ฐœํ•œ ๋งŒํผ 1D ์ž„๋ฒ ๋”ฉ๋“ค์„ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ ์ €์ž๋“ค์€ ์ด๋ฏธ์ง€ ํŒจ์น˜๋ฅผ ๋งŒ๋“ค์–ด 1D์ž„๋ฐฐ๋”ฉ์„ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ‘๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ๋Œ€ํ‘œ ์ด๋ฏธ์ง€ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…๋“œ๋ฆฌ๋ฉด [300,300,3]์˜ ์ด๋ฏธ์ง€๋ฅผ [100,100,3] ์ด๋ฏธ์ง€ 9๊ฐœ๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. 2-1. Patch Embedding ๋งŒ๋“ค๊ธฐ ํŒจ์น˜ํ™”๋œ ๊ฐ ์ด๋ฏธ์ง€๋ฅผ 1์ฐจ์›์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค (Linear Projection). ์•ž์„œ ํ•˜๋‚˜์˜ ํŒจ์น˜๊ฐ€ [100,100,3]์ด์—ˆ๋‹ค๋ฉด ๊ฐ ํ”ฝ์…€๋“ค์„ ์ผ๋ ฌ๋กœ ์ด์–ด ๋ถ™์—ฌ์„œ [1,100X100X3]์ธ 1์ฐจ์›์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. (์ด ๋’ค๋ถ€ํ„ฐ๋Š” NLP์˜ Transformer ์ธ์ฝ”๋”ฉ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค, ์ •๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ ๋‹ค์‹œ ๋ฆฌ๋ทฐํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค) 2-2. Class token ๋งŒ๋“ค๊ธฐ BERT์˜ CLS token๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ, ์ €์ž๋“ค์€ ์ž„๋ฒ ๋”ฉ๋œ patch์˜ ๋งจ ์•ž์— ํ•˜๋‚˜์˜ ํ•™์Šต ๊ฐ€๋Šฅํ•œ Class token์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด Class token์€ Transformer์˜ ์—ฌ๋Ÿฌ encoder ์ธต์„ ๊ฑฐ์ณ ์ตœ์ข… output์œผ๋กœ ๋‚˜์™”์„ ๋•Œ, ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ 1์ฐจ์› representation vector๋กœ์„œ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. (BERT์˜ CLS Token๊ณผ ๊ฑฐ์˜ ๋™์ผํ•œ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์ด์— ๋Œ€ํ•ด ๋จผ์ € ๊ณต๋ถ€ํ•˜๋ฉด ์ดํ•ด๊ฐ€ ๋น ๋ฆ…๋‹ˆ๋‹ค. BERT์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๋ ค๋ฉด ๋”ฅ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ | BERT๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. CLS Token์ด ์™œ ์ „์ฒด ๋ฌธ์žฅ์˜ representation vector๋กœ ์ž‘์šฉํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๋ ค๋ฉด ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.) 2-3. Position Embedding 1์ฐจ์›์œผ๋กœ ๋งŒ๋“  ๋ฒกํ„ฐ์— ๊ฐ™์€ ์ฐจ์›์˜ Position Embedding์„ ๋”ํ•ด์ค๋‹ˆ๋‹ค. Position Embedding์€ ์˜ค๋ฆฌ์ง€๋„ Transformer์™€ ๊ฐ™์ด ์ž„๋ฐฐ๋”ฉ์— ์ˆœ์„œ ์ •๋ณด๋ฅผ ๋ถ€์—ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค. (ํ•˜์ง€๋งŒ ์ด ๋ถ€๋ถ„์€ Optional์ด๋ผ๊ณ  ์†Œ๊ฐœํ•œ ์‚ฌ์ดํŠธ๋„ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์•„ ๊ผญ ํ•„์š”ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค) ์œ„ ๊ณผ์ •์„ ๋งˆ์น˜๋ฉด ์ „์ฒด ์ด๋ฏธ์ง€๋Š” [1, 100X100X3] ์ฐจ์›์˜ embedding vector 10๊ฐœ๋กœ ์ •์˜๋˜๋ฉฐ, ์ดํ›„ Transformer์˜ Encoder๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 3-1. Layer Normalization ๋ชจ๋“  ์ด๋ฏธ์ง€ ์ž„๋ฐฐ๋”ฉ์„ ์ฑ„๋„ ๊ธฐ์ค€์œผ๋กœ Layer Normalization์„ ํ•ฉ๋‹ˆ๋‹ค. 3-2. Multi-Head Self Attention (MSA) Patch + Position Embedding์„ ์ด์šฉํ•˜์—ฌ Self-Attention์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์ž„๋ฐฐ๋”ฉ 1๊ฐœ๋‹น 1๊ฐœ์”ฉ์˜ q(์ฟผ๋ฆฌ),k(ํ‚ค),v(๋ฐธ๋ฅ˜)๋ฅผ ํ•™์Šต ๊ฐ€์ค‘์น˜ W_q, W_k, W_v๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ•ฉ๋‹ˆ๋‹ค ๋งŒ์•ฝ num_heads๋ฅผ 12์ด๋ผ๊ณ  ํ•˜๋ฉด ๊ฐ ์ž„๋ฐฐ๋”ฉ๋งˆ๋‹ค [1, 100X100X3/12] ์ฐจ์›์˜ q, k, v๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ•œ q, k, v๋ฅผ ์ด์šฉํ•˜์—ฌ Attention Value๋ฅผ ๊ตฌํ•˜๊ณ  ์ด๊ฒƒ๋“ค์„ ์ฐจ์› ๋ฐฉํ–ฅ์œผ๋กœ concat ํ•˜์—ฌ Multi-Head Attention์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. 12๋ฒˆ self-attention์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด image patch ํ•œ ๊ฐœ์˜ Attention Value Matrix๋Š” [1, 100X100X3/12] ์ฐจ์›์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. num_heads ๊ฐœ์ˆ˜์ธ 12๊ฐœ Attention Value Matrix๋ฅผ ๊ตฌํ•˜์—ฌ ์ฐจ์› ๋ฐฉํ–ฅ์œผ๋กœ Concat ํ•˜๋ฉด ํ•œ ๊ฐœ์˜ image patch ๋‹น [1,100X100X3] ๊ฐœ์˜ ์˜ค๋ฆฌ์ง€๋„ ์ž„๋ฐฐ๋”ฉ์˜ ์ฐจ์›์„ ํšŒ๋ณตํ•ฉ๋‹ˆ๋‹ค. 3-3. Residual Connection (์ž”์ฐจ ์—ฐ๊ฒฐ) 2๋‹จ๊ณ„์—์„œ ๊ตฌํ•œ Input ์ž„๋ฐฐ๋”ฉ๊ณผ 3- 2๋‹จ๊ณ„์—์„œ ๊ตฌํ•œ Multi-Head Attention์„ ๋”ํ•ด ์ž”์ฐจ ์—ฐ๊ฒฐ์„ ํ•ฉ๋‹ˆ๋‹ค. ([10, 1,100X100X3]๋กœ ์ฐจ์›์ด ๋™์ผํ•ฉ๋‹ˆ๋‹ค) 4-1. Layer Normalization 3- 3๋‹จ๊ณ„์—์„œ ์ž”์ฐจ ์—ฐ๊ฒฐํ•œ Matrix๋ฅผ 3-1๋ฒˆ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ฑ„๋„ ๊ธฐ์ค€์œผ๋กœ Normalization์„ ์‹ค์‹œํ•ฉ๋‹ˆ๋‹ค. 4-2. Multi Layer Perceptron (MLP) ๋‘ ๊ฐœ์˜ Linear Layer๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ ์ฒซ ๋ฒˆ์งธ ๋ ˆ์ด์–ด์—์„œ ์ž„๋ฐฐ๋”ฉ ์‚ฌ์ด์ฆˆ๋ฅผ ํ™•์žฅํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ ๋ ˆ์ด์–ด์—์„œ ์›๋ž˜์˜ ์ž„๋ฐฐ๋”ฉ ์‚ฌ์ด์ฆˆ๋กœ ๋ณต์›ํ•ฉ๋‹ˆ๋‹ค. 4-3. Residual Connection (์ž”์ฐจ ์—ฐ๊ฒฐ) 4- 2๋‹จ๊ณ„์—์„œ ๊ตฌํ•œ Matrix์™€ 3- 3๋‹จ๊ณ„์˜ Matrix๋ฅผ ๋”ํ•ด ์ตœ์ข… output feature๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. 2๋ฒˆ input embedding์ด 3 & 4๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ ์ตœ์ข… output feature๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ์ˆ˜์‹์œผ๋กœ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜ (1)~(3)์ฒ˜๋Ÿผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 5. Multi Layer Perceptron (MLP) head ์—ฌ๊ธฐ๋ถ€ํ„ฐ Transformer์˜ output ์ถœ๋ ฅ๋‹จ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ผ๋ฐ˜์ ์ธ CNN์˜ Image classfier์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ํŠน์ดํ•œ ๊ฒƒ์€ Class token๋งŒ์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋งํ–ˆ๋“ฏ Class token์ด Transformer์˜ ์—ฌ๋Ÿฌ encoder ์ธต๊ณผ Layer Normalization์„ ๊ฑฐ์ณ ์ตœ์ข… output, y๊ฐ€ ๋‚˜์™”์„ ๋•Œ, ์ด y๊ฐ€ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ 1์ฐจ์› representation vector๋กœ์„œ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. MLP๋Š” y๋ฅผ Flatten ํ•œ ํ›„ ๋‹จ์ˆœ Linear Layer๋ฅผ ํ†ต๊ณผ์‹œ์ผœ classification์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ViT Training & Fine-Tuning ์ผ๋ฐ˜์ ์œผ๋กœ large-scale dataset์— ๋Œ€ํ•ด ViT๋ฅผ pre-train ํ•˜๊ณ  downstream task์— ๋Œ€ํ•ด fine-tuning์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด pre-trained Multi Layer Perceptron (MLP) head๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ D ร— K feedforward layer๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ D๋Š” 100X100X3, K๋Š” downstream class์˜ ๊ฐœ์ˆ˜์ž…๋‹ˆ๋‹ค. ViT์˜ ์„ฑ๋Šฅ ViT์˜ ๋‹จ์ , ํ•œ๊ณ„ Reference ์›๋…ผ๋ฌธ AN IMAGE IS WORTH 16X16 WORDS : TRANSFORMERS FOR IMAGE RECOGNITION AT SCALE Youtube DMQA Open Seminar | Transformer in Computer Vision ๋ธ”๋กœ๊ทธ ViT(Vision Transformer) ํ•ด์ฒด ์‹ ์„œ ๋…ผ๋ฌธ ์š”์•ฝ Vision ๋ถ„์•ผ์—์„œ ๋“œ๋””์–ด Transformer๊ฐ€ ๋“ฑ์žฅ ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ 6) CoAtNet(Convolution+Transformer) Background 2021๋…„ 6์›”, Google AI ํŒ€์—์„œ EfficientNet-V2์™€ ํ•จ๊ป˜ ๋ฐœํ‘œํ•œ CoatNet์€ ๋ฐœํ‘œ์™€ ๋™์‹œ์— ImageNet top 1 accuracy SOTA๋ฅผ ๊ฐˆ์•„์น˜์šด ๋ชจ๋ธ๋กœ Convolution๊ณผ Attention์„ ํ•ฉ์นœ ๋ฐฉ๋ฒ•์„ ์ œ์‹œํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ์ œ์•ˆ๋œ ๋ฐฐ๊ฒฝ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ViT์˜ ๋ฌธ์ œ์ . ViT๋Š” ํ™•์‹คํžˆ JFT-300(๋Œ€์ถฉ 3์–ต ๊ฐœ ์ •๋„..) ๊ฐ™์€ ์—„์ฒญ๋‚œ ์–‘์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ๋Š” CNN๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์ข‹์ง€๋งŒ, ๊ทธ๋ ‡์ง€ ๋ชปํ•  ๋•Œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ vanilla ResNet๋ณด๋‹ค๋„ ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ViT๊ฐ€ ์ ์€ ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง€๋Š” ์ด์œ ๋Š”, CNN์ด ๊ฐ€์ง€๋Š” ๊ณ ์œ ์˜ inductive bias(์ง€๊ธˆ๊นŒ์ง€ ๋งŒ๋‚˜๋ณด์ง€ ๋ชปํ–ˆ๋˜ ์ด๋ฏธ์ง€๋ฅผ ๋ดค์„ ๋•Œ ์ •ํ™•ํ•œ ํŒ๋‹จ์„ ๋‚ด๋ฆฌ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ถ”๊ฐ€์ ์ธ ๊ฐ€์ •)๋ณด๋‹ค ViT๊ฐ€ ๊ฐ€์ง€๋Š” inductive bias๊ฐ€ ์•ฝํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด CNN์˜ inductive bias๋Š” "Locality"์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ์„ค๊ณ„ ์ž์ฒด์— Vision task๋Š” ์ง€์—ญ์ ์ธ ์ •๋ณด์—์„œ ์ค‘์š”ํ•œ ๊ฒŒ ๋งŽ๋‹ค๋Š” ๊ฐ€์ •์ด ๊น”๋ ค์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋„ ์ด๋Š” Vision task์—์„œ๋Š” ์ ์ ˆํ•œ ๊ฐ€์ •์ž…๋‹ˆ๋””. CNN ํŠน์œ ์˜ generalization์ด ์ž˜ ๋œ๋‹ค๋Š” ํŠน์ง• ์—ญ์‹œ ์ด ๊ฐ€์ •์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๋ชจ๋ธ์ด๊ธฐ์— ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ CNN์€ Global ํ•œ ์ •๋ณด๋ฅผ ์ž˜ ์ด์šฉํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ CNN์€ receptive field๋ฅผ ๋„“ํžˆ๊ธฐ ์œ„ํ•ด ์ธต์„ ๊นŠ๊ฒŒ ์Œ“๋Š” ์‹์œผ๋กœ ์ง„ํ™”ํ•ด ์™”์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด CNN์˜ Locality์™€ ViT์˜ Self-attention, Input-dependent weightning์„ ๋ชจ๋‘ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ์š”? ๋‹น์—ฐํžˆ ์ด๋Ÿฐ ์‹œ๋„๋ฅผ ํ•œ ์‚ฌ๋žŒ๋“ค์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ง€๊ธˆ๊นŒ์ง€์˜ ์—ฐ๊ตฌ๋“ค์€ ์–ด๋–ป๊ฒŒ Convolution์— Self-attention์˜ ํŠน์ง•์„ ์ถ”๊ฐ€ํ• ์ง€, ์–ด๋–ป๊ฒŒ ViT์— Convolution์˜ ํŠน์ง•์„ ์ถ”๊ฐ€ํ• ์ง€๋ฅผ ๊ณ ๋ฏผํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ง„ํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์–ด๋–ป๊ฒŒ ๋„ฃ๋Š๋ƒ ๋งŒํผ์ด๋‚˜ ์ค‘์š”ํ•œ ๊ฒƒ์ด ๊ฐ๊ฐ์˜ ํŠน์ง•์„ ์–ผ๋งˆ๋‚˜ ํฌํ•จ์‹œ์ผœ ์„ž์„์ง€๋„ ์ค‘์š”ํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. CoatNet์€ ์ด๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์ œ์‹œ๋œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. Convolution๊ณผ Attention CNN๊ณผ transformer๋ฅผ ๋ฒ ์ด์Šค๋กœ ํ•œ architecture๋Š” ์ƒ์ˆ ํ–ˆ๋“ฏ ๊ฐ๊ฐ์˜ ์žฅ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Convolution๊ณผ Attention์„ ํ•ฉ์น˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด ๋‘˜์˜ ํŠน์ง•์„ ๋ถ„์„ํ•ด ๋ณผ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ CoatNet์˜ ์ €์ž๋“ค์€ ๋‘˜ ๊ฐ๊ฐ์˜ ์‹œ์Šคํ…œ์ ์ธ ๋ถ„์„์„ ์ง„ํ–‰ํ–ˆ๊ณ  ์š”์•ฝํ•˜์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. Convolution layer๋Š” ๋น ๋ฅด๊ฒŒ ์ˆ˜๋ ดํ•  ์ˆ˜ ์žˆ๊ณ , ํŠน์œ ์˜ inductive bias(Locality, parameter sharing) ๋•ํƒ์— generalization์„ ์ž˜ํ•œ๋‹ค. (translation equivariance) Translation equivariance Equivariance๋ž€, ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์ด ๋ฐ”๋€Œ๋ฉด ์ถœ๋ ฅ ๋˜ํ•œ ๋ฐ”๋€๋‹ค๋Š” ์˜๋ฏธ๋กœ ์ž…๋ ฅ์˜ ์œ„์น˜๊ฐ€ ๋ณ€ํ•˜๋ฉด ์ถœ๋ ฅ๋„ ๋™์ผํ•˜๊ฒŒ ๋ณ€ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. CNN์˜ Classification์€ max pooling์œผ๋กœ ์ธํ•ด translation invariance ํ•˜๊ฒŒ ์ด๋ฃจ์–ด์ง€์ง€๋งŒ, Convolution layer ์ž์ฒด๋Š” translation equivariance ํ•ฉ๋‹ˆ๋‹ค. CNN์˜ inductive bias๋กœ ๊ผฝํžˆ๋Š” Locality์™€ Parameter sharing์— ์ฃผ๋ชฉํ•ด ๋ด…์‹œ๋‹ค. Locality๋Š” ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ ์ผ๋ถ€ ์ง€์—ญ์˜ ํ”ฝ์…€๋งŒ ๊ณ ๋ คํ•˜๋Š” ํŠน์ง•์ด๊ณ , Parameter sharing์€ Conv kernel์˜ weight๊ฐ€ ๋ชจ๋‘ ๊ณต์œ ๋˜๋Š” ํŠน์ง•์ž…๋‹ˆ๋‹ค. Convolution layer๊ฐ€ Translation equivalence๋ผ๋Š” ํŠน์ง•์„ ๊ฐ–๋Š” ๊ฒƒ์€ ์ด ๋‘ ๊ฐ€์ง€ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” ์ฃผ๋กœ MobileNet์˜ MBConv block์— ์ดˆ์ ์„ ๋งž์ท„๋Š”๋ฐ, Attention์˜ FFN์ด ์ฑ„๋„์„ ๋Š˜์˜€๋‹ค๊ฐ€ ์ค„์ด๋Š” Inverted Bottle neck์ด MBConv์™€ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Attention์€ CNN๊ณผ ๋ฐ˜๋Œ€๋กœ Global receptive field๋ฅผ ๊ฐ€์ง€๊ณ , Input-dependent ํ•œ weight๋Š” ์—ฌ๋Ÿฌ spatial position๋“ค ์‚ฌ์ด์˜ ๊ด€๊ณ„์„ฑ์„ ์ž˜ ํฌ์ฐฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, CNN๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ Capacity๊ฐ€ ๋†’๋‹ค. > ๋” ํฐ dataset์— ์ ํ•ฉํ•˜๋‹ค. ์ด๋Ÿฐ ์ ๋“ค์„ ๋†“๊ณ  ๋ณด๋ฉด Computer vision์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ๋Š” Conv layer๋กœ๋ถ€ํ„ฐ Translation Equivariance๋ฅผ, Attention layer๋กœ๋ถ€ํ„ฐ๋Š” Input-adaptive weighting๊ณผ Global receptive field๋ผ๋Š” ์žฅ์ ์„ ๊ฐ€์ ธ์™€์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ ์ด๋ฅผ ๋‹จ์ˆœํžˆ Attention์— Global static convolution kernel์„ ์ถ”๊ฐ€ํ•ด์„œ ์‹œ๋„ํ–ˆ์Šต๋‹ˆ๋‹ค. Softmax ํ›„์— ๋„ฃ์„์ง€, ์ „์— ๋„ฃ์„์ง€ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ Relative Attention์ด๋ผ๊ณ  ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ Block๋“ค์„ ์–ด๋–ป๊ฒŒ ํ•ฉ์น  ๊ฒƒ์ธ๊ฐ€? Attention์˜ ์‹ค์šฉ์„ฑ์„ ํ•ด์น˜๋Š” ๊ฐ€์žฅ ํฐ ์›์ธ์€ ํ† ํฐ์˜ ์ œ๊ณฑ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์—ฐ์‚ฐ๋Ÿ‰์ž…๋‹ˆ๋‹ค. Attention์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ architecture๋“ค์€ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ Attention์˜ ์—ฐ์‚ฐ ๋ฐฉ์‹์„ ๋ฐ”๊ฟ” ์—ฐ์‚ฐ๋Ÿ‰์„ ์ค„์ด๋ ค๋Š” ์‹œ๋„๋ฅผ ํ–ˆ๊ณ  ViT์˜ ๊ฒฝ์šฐ ์ด๋ฏธ์ง€๋ฅผ ํŒจ์น˜๋กœ ์ž˜๋ผ ํ† ํฐ์„ ์ค„์ด๋Š” ์ „๋žต์„ ์ทจํ–ˆ์Šต๋‹ˆ๋‹ค. CoatNet์—์„œ๋Š” multi-stage layout์„ ์ด์šฉํ•ด, ConvNet์ด 5๊ฐœ์˜ stage๋ฅผ ๊ฐ–๋„๋ก ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. S0๋Š” simple 2-layer convolution์ด, S1์—์„œ๋Š” MBConv block๊ณผ Squeeze-excitation์ด ์ ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. S2-S4๊นŒ์ง€๋Š” Convolution์ด Local pattern ์ฒ˜๋ฆฌ์— ์œ ๋ฆฌํ•˜๋‹ค๋Š” ๊ฐ€์ •์— ์ž…๊ฐํ•ด ๊ฐ stage์˜ ์ดˆ๋ฐ˜๋ถ€์—๋Š” Convolution(MBConv)์„ ๋„ฃ๊ณ , ๋’ค์ชฝ์—๋Š” Transformer stage๋ฅผ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค. C๋ฅผ Convolution, T๋ฅผ transformer๋ผ๊ณ  ํ–ˆ์„ ๋•Œ C-C-C-C, C-C-C-T, C-C-T-T, C-T-T-T๋กœ ๊ตฌ์„ฑ๋˜์—ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ๊ฒฐ์ • ์ €์ž๋“ค์€ Convolution์˜ ์žฅ์ ์ด์—ˆ๋˜ generalization๊ณผ, Attention์˜ ์žฅ์ ์ด์—ˆ๋˜ model Capacity์˜ ๊ด€์ ์—์„œ ๋น„๊ต๋ฅผ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์šฉ์–ด์— ๋Œ€ํ•ด ํ•œ๋ฒˆ ๋‹ค์‹œ ์ •์˜๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Generalization : train๊ณผ validation score์˜ ์ตœ์†Œ gap. Capacity : Overfit ์—†์ด ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์…‹์˜ ํฌ๊ธฐ. Generalization Generalization ์„ฑ๋Šฅ์€ ImageNet-1K ์„ฑ๋Šฅ์œผ๋กœ ๋น„๊ตํ–ˆ์Šต๋‹ˆ๋‹ค. ViT์˜ ๊ฒฝ์šฐ Low-level information processing์ด ๋ถ€์กฑํ•ด ์ผ๋ฐ˜ํ™” ์„ฑ๋Šฅ์ด ๋–จ์–ด์กŒ๊ณ , Convolution layer๊ฐ€ ๋งŽ์„์ˆ˜๋ก ์ผ๋ฐ˜ํ™” ์„ฑ๋Šฅ์ด ์ข‹์•„์กŒ์Šต๋‹ˆ๋‹ค. C-C-C-C C-C-C-T C-C-T-T C-T-T-T ViT REL Capacity ์˜ˆ์ƒ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ViT์ด ์ค‘๊ฐ„ ์ •๋„์˜ Capacity๋งŒ ๊ฐ€์กŒ์œผ๋ฉฐ, C-C-T-T / C-T-T-T๊ฐ€ ๋” Capacity๊ฐ€ ๋†’์•˜๋Š”๋ฐ ์ด๋Š” ๋งŽ์€ transformer block์ด vision์—์„œ๋Š” ๋†’์€ Capacity๋ฅผ ์–ป๋Š”๋ฐ ํ•„์ˆ˜์ ์ด์ง€ ์•Š๊ฑฐ๋‚˜, ViT Stem์ด ๋„ˆ๋ฌด ๋งŽ์€ ์ •๋ณด๋ฅผ ์žƒ์–ด๋ฒ„๋ฆผ์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. C-C-T-T C-T-T-T ViT REL C-C-C-T C-C-C-C ์ถ”๊ฐ€์ ์œผ๋กœ C-C-T-T์™€ C-T-T-T ์‚ฌ์ด์—์„œ ์–ด๋Š ๊ฒƒ์ด ๋” ๊ดœ์ฐฎ์€์ง€ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด JFT๋กœ pre training ์‹œํ‚จ ํ›„ fine tuning์„ ํ•ด์„œ ์„ฑ๋Šฅ ๋น„๊ต๋ฅผ ํ•ด๋ณด์•˜๋Š”๋ฐ, C-C-T-T๊ฐ€ ๋” ๊ดœ์ฐฎ์€ ์„ฑ๋Šฅ์„ ๋ณด์˜€๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. C-C-T-T๊ฐ€ ์ตœ์ข…์ ์œผ๋กœ ์„ฑ๋Šฅ์ด ์ œ์ผ ๊ดœ์ฐฎ์•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ตœ์ข… ๊ตฌ์กฐ๋Š” ์ตœ๋Œ€ํ•œ C-C-T-T์— ๊ฐ€๊น๊ฒŒ ๊ฒฐ์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์„ฑ๋Šฅ ํ‰๊ฐ€ ๊ธฐ์กด SOTA ๋“ค๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ ์‚ด์ง ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚ด๋Š”๋ฐ ์„ฑ๊ณตํ–ˆ์œผ๋ฉฐ, 21K pretrained๋ฅผ 1K๋กœ transfer ํ–ˆ์„ ๋•Œ๋Š” ๋” ๊ดœ์ฐฎ์€ ์„ฑ๋Šฅ์„ ๋ณด์˜€์Šต๋‹ˆ๋‹ค. ViT๊ฐ€ ์ฒ˜์Œ ์ œ์‹œ๋˜์—ˆ์„ ๋•Œ ์‚ฌ๋žŒ๋“ค์€ ์ด์ œ Convolution์„ ํ†ตํ•ด ํ•ด๊ฒฐํ•˜๋˜ ๋งŽ์€ ๋ฌธ์ œ๋“ค์ด ViT๋กœ ๋Œ€์ฒด๋˜์ง€ ์•Š๊ฒ ๋ƒ๊ณ  ์ถ”์ธกํ–ˆ์—ˆ๋Š”๋ฐ์š”, ์ด๋ ‡๋“ฏ Convolution๊ณผ Attention์˜ ์žฅ์ ์„ ๊ฒฐํ•ฉํ•œ ์ƒˆ๋กœ์šด ๋ชจ๋ธ์ด ๋˜ ํ•œ ๋ฒˆ SOTA๋ฅผ ๊ฐˆ์•„์น˜์šฐ๋ฉด์„œ ์ƒˆ๋กœ์šด ๊ฐ€๋Šฅ์„ฑ์„ ์—ด์—ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. Reference ๋…ผ๋ฌธ : https://arxiv.org/abs/2106.04803 ๊ตญ๋ฌธ์ •๋ฆฌ๋œ ๋ธ”๋กœ๊ทธ (๋งŽ์€ ๋ถ€๋ถ„์„ ์ฐธ๊ณ ํ•จ.) : https://creamnuts.github.io/short%20review/coatnet/ ์˜๋ฌธ ์ •๋ฆฌ๋œ ๋ธ”๋กœ๊ทธ : https://andlukyane.com/blog/paper-review-coatnet inductive bias์— ๋Œ€ํ•œ ์„ค๋ช… https://velog.io/@euisuk-chung/Inductive-Bias๋ž€ https://visionhong.tistory.com/25 https://robot-vision-develop-story.tistory.com/29. (4) ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•(โ˜…์ž‘์„ฑ ์ค‘) Introduction ๋จธ์‹ ๋Ÿฌ๋‹์—์„œ ๋ชจ๋ธ์˜ ํ‰๊ฐ€๋Š” ๋ฌธ์ œ๊ฐ€ ๋ถ„๋ฅ˜(Classification) ๋ฌธ์ œ์ด๋ƒ, ํšŒ๊ท€(Regression) ๋ฌธ์ œ์ด๋ƒ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋งˆ๋‹ค ํŠน์„ฑ์— ์ฐจ์ด๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๋ฐ˜์˜ํ•ด ๋ชจ๋ธ์„ ์ •ํ™•ํ•˜๊ฒŒ ํ‰๊ฐ€ํ•  ์ง€ํ‘œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Classification์˜ ์„ฑ๋Šฅ ํ‰๊ฐ€ Confusion matrix Precision - Recall F1 score Accuracy AUC , ROC curve Regression์˜ ์„ฑ๋Šฅ ํ‰๊ฐ€ MSE Coefficientg of determination 3. Image Segmentation(์ด๋ฏธ์ง€ ๋ถ„ํ• ) Image Segmentation์€ ์ž…๋ ฅ ์ด๋ฏธ์ง€์—์„œ ํ”ฝ์…€์˜ ๊ฐ ํด๋ž˜์Šค๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” Task์ž…๋‹ˆ๋‹ค. (1) ์ด๋ฏธ์ง€ ๋ถ„ํ•  ์•„์ด๋””์–ด ์ด๋ฏธ์ง€ ๋ถ„ํ•  ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค(Bounding box)๋กœ ๊ฒ€์ถœ๋œ ๋ฌผ์ฒด๋“ค์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด ๊ฒ€์ถœ(Object detection) ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์ด๋ฏธ์ง€ ๋ถ„ํ• (Image segmentation)์€ ํ”ฝ์…€์˜ ๋ถ„๋ฅ˜(Classification) ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ๊ฐ€ ์ž…๋ ฅ ์ด๋ฏธ์ง€ ์•ˆ์˜ ๋ชจ๋“  ํ”ฝ์…€์„ (์ง€์ •๋œ ๊ฐœ์ˆ˜์˜) ํด๋ž˜์Šค๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„ํ• ์˜ ์ข…๋ฅ˜ ์ด๋ฏธ์ง€ ๋ถ„ํ• ์€ Sementic segmentation๊ณผ Instance segmentation์œผ๋กœ ์„ธ๋ถ€์ ์œผ๋กœ ๋‚˜๋ˆ ์ง‘๋‹ˆ๋‹ค. Semantic Segmentation์€ ๊ฐ Pixel์ด ์–ด๋–ค ํด๋ž˜์Šค์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฌธ์ œ์ด๊ณ  Instance Segmentation์€ ๋” ๋‚˜์•„๊ฐ€ ๊ฐ™์€ ์‚ฌ๋ฌผ ์•ˆ์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด(Instacne)๊นŒ์ง€ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฌธ์ œ๋ผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์„ ๋ณด์‹œ๋ฉด Segmentation์˜ ์ฐจ์ด๊ฐ€ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ด๊ฐ€ ๋˜์‹ค ๊ฒ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„ํ•  ๋„คํŠธ์›Œํฌ๋Š” ์•„๋ž˜์˜ ์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด ๊ฐ pixel์ด N ๊ฐœ์˜ ํด๋ž˜์Šค ์ค‘ ์–ด๋–ค ํด๋ž˜์Šค์— ์†ํ•˜๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ Segmentation map์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (Segmentation map์€ ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜์™€ ๋™์ผํ•˜๊ฒŒ N ๊ฐœ์˜ ์ฑ„๋„๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค) ์ตœ์ข…์ ์œผ๋กœ๋Š” ๋‘ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด Segmentation map์— argmax๋ฅผ ํ†ตํ•ด์„œ ์•„๋ž˜ ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ 1์ฑ„๋„ ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅ์œผ๋กœ ๋‚ด๋ณด๋ƒ…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„ํ•  ๋„คํŠธ์›Œํฌ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ : ์ธ์ฝ”๋” & ๋””์ฝ”๋”(Encoder & Decoder) ์ด๋ฏธ์ง€ ๋ถ„ํ• ์— ์‚ฌ์šฉ๋˜๋Š” ๋งŽ์€ ๋„คํŠธ์›Œํฌ๋Š” ์ด๋ฏธ์ง€์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์ด๋Š” Encoder-Decoder๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ ์ด๋ฏธ์ง€ ๋ถ„ํ• ์˜ ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. โ‘  ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ W, H๋ฅผ ์ค„์ด๊ณ  ์ฑ„๋„์ˆ˜๋ฅผ ๋Š˜๋ ค ํ”ผ์ฒ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค โ‘ก W, H๋ฅผ ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ ์‚ฌ์ด์ฆˆ๋กœ ํšŒ๋ณต, ์ฑ„๋„์ˆ˜๋Š” ํด๋ž˜์Šค์˜ ์‚ฌ์ด๋กœ ๋งž์ถฐ Segmentation map์„ ์ƒ์„ฑํ•œ๋‹ค ๋ฌผ๋ก  ์ž…๋ ฅ ์ด๋ฏธ์ง€ W, H๋ฅผ ๋ณด์กดํ•˜๋ฉด์„œ ํ”ผ์ฒ˜๋ฅผ ์ถ”์ถœํ•˜๋ฉด ์ข‹๊ฒ ์œผ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ๋ฌธ์ œ๋กœ ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ W, H๋ฅผ ์œ ์ง€ํ•˜๋ฉฐ ํ”ผ์ฒ˜๋ฅผ ์ถ”์ถœํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค ์œ„์™€ ๊ฐ™์ด ๋„คํŠธ์›Œํฌ๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๋ฉด Instance/Sementic ๋ณ„ ํ”ฝ์…€์ด ๋ถ„ํ• ๋˜๋„๋ก ๋„คํŠธ์›Œํฌ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค Reference sementic segmentation ์ฒซ๊ฑธ์Œ cs231n : lecture 11 detection and segmentation (2) ์ด๋ฏธ์ง€ ๋ถ„ํ•  ๋ชจ๋ธ ์ดํ•ด๋ฅผ ์œ„ํ•œ ์‚ฌ์ „ ์ง€์‹ 1) Down-sampling Downsampling ์ด๋ž€ ์‹ ํ˜ธ์ฒ˜๋ฆฌ์—์„œ ๋งํ•˜๋Š” ์šฉ์–ด๋กœ sample์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ด๋Š” ์ฒ˜๋ฆฌ ๊ณผ์ •์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋”ฅ๋Ÿฌ๋‹์—์„œ๋Š” ์ธ์ฝ”๋”ฉํ•  ๋•Œ data์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ด๋Š” ์ฒ˜๋ฆฌ ๊ณผ์ •์ด๋ผ๊ณ  ๋ณด์‹œ๋ฉด ์ดํ•ดํ•˜์‹œ๋Š”๋ฐ ์–ด๋ ค์›€์ด ์—†์œผ์‹ค ๊ฒ๋‹ˆ๋‹ค. Pooling ์ด๋ฏธ ์ต์ˆ™ํ•œ ๊ฐœ๋…์ด๊ณ  ์•ž์—์„œ๋„ ์„ค๋ช…ํ•œ ๋‚ด์šฉ์ด์ง€๋งŒ, ๊ฐ„๋‹จํ•˜๊ฒŒ Pooling์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์ •๋ฆฌํ•˜๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. ํŠน์ •ํ•œ ๊ทœ์น™(Max, Average)์— ์˜ํ•ด์„œ Kernel ๋‚ด์—์„œ ๊ฐ’์„ ๋งŒ๋“ค์–ด ๋‚ด๊ฑฐ๋‚˜ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค ๋Œ€ํ‘œ์ ์ธ Max_pooling๊ณผ Average_pooling์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•œ๋ฒˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด ํ•ด๋‹น ์˜์—ญ(kernel) ๋‚ด์—์„œ ํ‰๊ท  ํ˜น์€ ์ตœ๋Œ“๊ฐ’์„ ๊ณ„์‚ฐํ•˜์—ฌ ์š”์•ฝ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ƒ…๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ Pooling์˜ ๋ฐฉ๋ฒ•์œผ๋กœ Downsampling์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต์€ Average pooling๋ณด๋‹ค๋Š” Max pooling์ด ๋” ๋งŽ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. (Average pooling์˜ ๊ฒฝ์šฐ ํ•„์—ฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ข€ ์žˆ์Šต๋‹ˆ๋‹ค, ๋…ผ๋ฌธ, ๊ต์žฌ์—์„œ ๋ณ„๋„ ์–ธ๊ธ‰์ด ์—†์œผ๋ฉด Max pooling์œผ๋กœ ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค) Dilated (Atrous) convolution Convolution๋„ Down Sampling์˜ ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•ž์„œ ์„ค๋ช…๋“œ๋ฆฐ Pooling ๊ณ„์—ด๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ํ•™์Šต๊ณผ์ •์„ ๊ฑฐ์น˜๋ฏ€๋กœ ๋ณด๋‹ค ํšจ๊ณผ์ ์œผ๋กœ down sampling ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ receptive field๋ฅผ ํฌ๊ฒŒ ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. receptive field๋ฅผ ํฌ๊ฒŒ ํ•˜๋ฉด trainable parameter๊ฐ€ ๋ฌด์ˆ˜ํžˆ ๋Š˜์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ณ ์•ˆํ•ด๋‚ธ ๋ฐฉ๋ฒ•์ด Dilated convolution์ž…๋‹ˆ๋‹ค. Atrous๋Š” ํ”„๋ž‘์Šค์–ด๋กœ 'A trous'(๊ตฌ๋ฉ์ด ์žˆ๋Š”)์—์„œ ๋‚˜์˜จ ๋ง์ž…๋‹ˆ๋‹ค. " Dilated (Atrous) convolution์˜ filter๋Š” ์ผ๋ฐ˜์ ์ธ convolution filter ์‚ฌ์ด์— ๋นˆ ๊ณต๊ฐ„์„ ๋„ฃ์–ด์„œ ๊ตฌ๋ฉ์ด ๋šซ๋ ค ์žˆ๋Š” ๋“ฏํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. Semantic Segmentation์—์„œ ๋†’์€ ์„ฑ๋Šฅ์„ ๋‚ด๊ธฐ ์œ„ํ•ด์„œ๋Š” CNN์˜ ๋งˆ์ง€๋ง‰ feature map์— ์กด์žฌํ•˜๋Š” ํ•œ ํ”ฝ์…€์ด ์ž…๋ ฅ๊ฐ’์—์„œ ์–ด๋Š ํฌ๊ธฐ์˜ ์˜์—ญ์—์„œ ์ปค๋ฒ„ํ•˜๋Š”์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” receptive field๊ฐ€ ์–ผ๋งˆ๋‚˜ ํฐ ์ง€๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. Atrous convolution์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๊ธฐ์กด convolution๊ณผ ๋™์ผํ•œ ์–‘์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๊ณ„์‚ฐ๋Ÿ‰์„ ์œ ์ง€ํ•˜๋ฉด์„œ๋„, receptive field๋Š” ์ปค์ง‘๋‹ˆ๋‹ค. Atrous convolution์„ ์‹œํ–‰ํ•  ๋•Œ๋Š” ํ™•์žฅ ๋น„์œจ(dilation rate, r)๋ฅผ ์ง€์ •ํ•˜์—ฌ filter ์‚ฌ์ด์— ๋นˆ ๊ณต๊ฐ„์„ ์–ผ๋งˆ๋‚˜ ๋‘˜์ง€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. Depthwise convolution ์ผ๋ฐ˜์ ์ธ convolution ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ํ•˜๋‚˜์˜ filter๋ฅผ ์ž…๋ ฅ ์˜์ƒ์˜ ๋ชจ๋“  ์ฑ„๋„์— ์ ์šฉํ•˜๊ณ  ๊ทธ ์ถœ๋ ฅ๋ฌผ์„ ๋”ํ•˜์—ฌ ํ•˜๋‚˜์˜ feature map์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ํŠน์ • ์ฑ„๋„๋งŒ์˜ Spatial Feature๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช…ํ•ด ๋ณด์ž๋ฉด input image์˜ R, G, B ์ฑ„๋„ ๋ชจ๋‘์— ๊ฐ™์€ filter๋ฅผ ์ ์šฉํ•˜๊ธฐ์— R ์ฑ„๋„ ํŠน์ด์ ์ธ spatial feature๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด์ „์— ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด convolution layer๊ฐ€ ๊นŠ์–ด์งˆ์ˆ˜๋ก ์—ฐ์‚ฐ๋Ÿ‰์ด ์ฆํญ๋œ๋‹ค๋Š” ๋‹จ์ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•˜๊ณ ์ž ์ œ์‹œ๋œ ๊ฒƒ์ด Depthwise convolution์ž…๋‹ˆ๋‹ค. Depthwise convolution์—์„œ ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ channel๋งˆ๋‹ค spatial feature๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๊ฐ Channel ๋ณ„ filter๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์ง•์œผ๋กœ ์ธํ•ด input channel ์ˆ˜์™€ output channel ์ˆ˜๊ฐ€ ๊ฐ™๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ Depth-wise Convolution์€ ํ•œ ๋ฒˆ ํ†ต๊ณผํ•˜๋ฉด, ํ•˜๋‚˜๋กœ ๋ณ‘ํ•ฉ๋˜์ง€ ์•Š๊ณ , (R, G, B)๊ฐ€ ๊ฐ๊ฐ Feature Map์ด ๋ฉ๋‹ˆ๋‹ค. ํŠน์ • ์ฑ„๋„๋งŒ์˜ Spatial Feature๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง„ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค. ์ด์ œ Parameter์™€ ์—ฐ์‚ฐ๋Ÿ‰ ์ธก๋ฉด์—์„œ ๊ธฐ์กด convolution๊ณผ Depthwise convolution๋ฅผ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. W : input/ouput์˜ width H : input/ouput์˜ height C : input์˜ channel K : kernel์˜ ํฌ๊ธฐ M : output์˜ channel No. of Parameters ๊ธฐ์กด convolution์—์„œ ํ•˜๋‚˜์˜ filter๊ฐ€ ๊ฐ€์ง€๋Š” parameter ํฌ๊ธฐ๋Š” K x K x C๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ filter์—์„œ ๋‚˜์˜ค๋Š” output์€ output channel ์ค‘ ํ•˜๋‚˜์— ํ•ด๋‹นํ•˜๋‹ˆ, output์„ M ๊ฐœ์˜ channel๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋ ค๋ฉด ์ด๋Ÿฌํ•œ filter์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ด M ๊ฐœ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด parameter ์ˆ˜๋Š” K x K x C x M ๊ฐœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด Depthwise convolution์„ ์‹œํ–‰ํ•˜๋ฉด convolution filter๊ฐ€ ๊ฐ€์ง€๋Š” parameter ํฌ๊ธฐ๋Š” K x K x C๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ filter์—์„œ ๋‚˜์˜ค๋Š” output์ด output channel ์ „์ฒด์— ํ•ด๋‹นํ•˜๋ฏ€๋กœ ์ด parameter ์ˆ˜๋Š” K x K x C ๊ฐœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. Computational Cost ๊ธฐ์กด convolution์™€ Depthwise convolution ๋ชจ๋‘ output ํฌ๊ธฐ๊ฐ€ H x W์ด๋ฏ€๋กœ, parameter ๊ฐœ์ˆ˜ x H x W ๋ฒˆ ์—ฐ์‚ฐ์„ ํ•ด์•ผ ํ•˜๋‚˜์˜ output ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. Depthwise separable convolution Depthwise separable convolution์€ Depthwise convolution ๋’ค์— 1x 1 convolution์„ ์—ฐ๊ฒฐํ•œ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. Depthwise separable convolution์€ ๊ธฐ์กด Convolution ํ•„ํ„ฐ๊ฐ€ Spatial dimension๊ณผ Channel dimension์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๋˜ ๊ฒƒ์„ ๋”ฐ๋กœ ๋ถ„๋ฆฌ์‹œ์ผœ ๊ฐ๊ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ์ถ•์„ ๋ถ„๋ฆฌ์‹œ์ผœ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋”๋ผ๋„, ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’์€ ๋‘ ์ถ• ๋ชจ๋‘๋ฅผ ์ฒ˜๋ฆฌํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ๊ธฐ์กด convolution์ด ์ˆ˜ํ–‰ํ•˜๋˜ ์—ญํ• ์„ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Depthwise separable convolution์€ ๊ธฐ์กด Convolution์— ๋น„ํ•ด parameter์˜ ์ˆ˜์™€ ์—ฐ์‚ฐ๋Ÿ‰์ด ํ›จ์”ฌ ์ ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ต์‹ฌ์€ ์—ฐ์‚ฐ๋Ÿ‰์ด ๋†’์€ ๊ณณ์—์„œ๋Š” ์ตœ๋Œ€ํ•œ Feature Map์„ ์ ๊ฒŒ ์ƒ์„ฑํ•˜๊ณ , ์—ฐ์‚ฐ๋Ÿ‰์ด ๋‚ฎ์€ ๊ณณ์—์„œ Feature Map์˜ ์ˆซ์ž๋ฅผ ์กฐ์ ˆํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. No. of Parameters ๊ธฐ์กด convolution์—์„œ ํ•˜๋‚˜์˜ filter๊ฐ€ ๊ฐ€์ง€๋Š” ์ด parameter ์ˆ˜๋Š” K x K x C x M ๊ฐœ์ž…๋‹ˆ๋‹ค. Depthwise convolution์„ ์‹œํ–‰ํ•˜๋ฉด ์ด parameter ์ˆ˜๋Š” K x K x C ๊ฐœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. Depthwise seperable convolution์€ Depthwise convolution์— 1x1x C ํฌ๊ธฐ์˜ Convolution์„ M ๊ฐœ ์—ฐ๊ฒฐํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ์ด parameter ์ˆ˜๋Š” K x K x C + 1 x 1x C x M ๊ฐœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. Computational Cost Depthwise seperable convolution ์—ญ์‹œ output ํฌ๊ธฐ๊ฐ€ H x W์ด๋ฏ€๋กœ, parameter ๊ฐœ์ˆ˜ x H x W ๋ฒˆ ์—ฐ์‚ฐ์„ ํ•ด์•ผ ํ•˜๋‚˜์˜ output ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. (Atrous convolution, Depthwise separable convolution์€ sementic segmentation ๋ชจ๋ธ ์ค‘ ์ƒ์œ„ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š” Deep lab์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ฏ€๋กœ Deep lab์„ ๊ณต๋ถ€ํ•˜์‹œ๋‹ค๊ฐ€ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋Œ์•„์™€์„œ ๊ณต๋ถ€ํ•˜๋Š” ๊ฒƒ๋„ ๋„์›€์ด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.) Reference ์™„์ˆ™์˜ ์—๊ตฌ๋จธ๋‹ˆ | Deep Lab Enough is not enough | ๋”ฅ๋Ÿฌ๋‹์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋‹ค์–‘ํ•œ Convolution ๊ธฐ๋ฒ•๋“ค Different types of Convolutions 2) Up-sampling(โ˜…์ž‘์„ฑ ์ค‘) Up sampling์€ Down sampling์˜ ๋ฐ˜๋Œ€๋กœ ๋””์ฝ”๋”ฉ ์‹œ ๋ณต์›ํ•˜๊ธฐ ์œ„ํ•ด์„œ data์˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๋Š” ์ฒ˜๋ฆฌ ๊ณผ์ •์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Upsampling์˜ ๋ฐฉ๋ฒ• ์ค‘ ๋Œ€ํ‘œ์ ์ธ ๊ฒƒ์„ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Unpooling Unpooling์€ Maxpooling์„ ๊ฑฐ๊พธ๋กœ ์žฌํ˜„ํ•˜์—ฌ ์ฃผ๋ณ€ ํ”ฝ์…€๋“ค์„ ๋™์ผํ•œ ๊ฐ’์œผ๋กœ ์ฑ„์šฐ๊ฑฐ๋‚˜ (Nearest Neighbor Unpooling), 0์œผ๋กœ ์ฑ„์›Œ์ฃผ๋Š” ๋ฐฉ์‹(Bed of NailsUnpooling)์ž…๋‹ˆ๋‹ค. Max Unpooling Unpooling ๋ฐฉ์‹์—๋Š” ํ•œ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ 2X2์˜ Matrix๋กœ Max pooling๋œ data๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋ฅผ ์›๋ž˜ ์‚ฌ์ด์ฆˆ์ธ 4X4์˜ Matrix๋กœ Unpooling ํ•˜๊ฒŒ ๋˜๋ฉด ์›๋ž˜ Max pooled๋œ ๊ฐ’์˜ ์œ„์น˜๋ฅผ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด Max Unpooling์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด Max poolingํ•  ๋•Œ์˜ ์„ ํƒ๋œ ๊ฐ’๋“ค์˜ ์œ„์น˜๋ฅผ ๊ธฐ์–ตํ•ด ์›๋ž˜ ์ž๋ฃŒ์˜ ๋™์ผํ•œ ์œ„์น˜์— Max ๊ฐ’์„ ์œ„์น˜์‹œ์ผœ Unpooling ํ•ฉ๋‹ˆ๋‹ค. Bilinear Interpolation ... Deconvolution ๋งŽ์€ ๋ฌธํ—Œ์—์„œ Deconvolution๊ณผ Transposed convolution์„ ํ˜ผ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๋‘ ๊ฐœ๋…์€ ์‚ฌ์‹ค ๋‹ค๋ฆ…๋‹ˆ๋‹ค. Decovnolution์€ convolution์˜ ์—ญ์—ฐ์‚ฐ์œผ๋กœ inversed matrix (์—ญํ–‰๋ ฌ) ์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. Convolution ์—ฐ์‚ฐ์„ ์ˆ˜์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. f * i = o (f๋Š” filter(๋˜๋Š” kernel), *๋Š” convolution ์—ฐ์‚ฐ, i๋Š” input, o๋Š” output) Deconvolution์€ f, i, o ๊ฐ’์„ ๋ชจ๋‘ ๋‹ค ์•Œ๊ณ  ์žˆ๋Š” ์ƒํ™ฉ์—์„œ f ์—ญํ–‰๋ ฌ * o๋ผ๋Š” ์—ฐ์‚ฐ์„ ํ†ตํ•ด i๋ฅผ ์•Œ์•„๋‚ด๋Š” ๊ณผ์ •์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Deconvolution ์—ฐ์‚ฐ์„ ์ˆ˜์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. inverse matrix of f * i = o Transposed Convolution (Backward Strided Convolution) Transposed Convolution์€ ๊ธฐ์กด์— ์•Œ๊ณ  ์žˆ๋˜ f์˜ ์—ญํ–‰๋ ฌ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•™์Šต์„ ํ†ตํ•ด ์ƒˆ๋กœ์šด f๋ฅผ ๊ตฌํ•œ๋‹ค๋Š” ์ ์—์„œ Deconvolution๊ณผ ๊ตฌ๋ณ„๋ฉ๋‹ˆ๋‹ค. Transposed convolution ์—ฐ์‚ฐ์„ ์ˆ˜์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. f' * i = o ์—ฌ๊ธฐ์„œ f'๋Š” ํ•™์Šต์„ ํ†ตํ•ด ๋งŒ๋“ค์–ด๋‚ธ ์ƒˆ๋กœ์šด filter์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ฆ„์— "transposed(์ „์น˜, ํ–‰๊ณผ ์—ด์ด ๋ฐ”๋€œ)"๋ผ๋Š” ๋‹จ์–ด๋Š” ์™œ ๋ถ™์–ด์žˆ์„๊นŒ์š”? ์ด๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์ผ๋ฐ˜์ ์ธ convolution์˜ ๊ณ„์‚ฐ ๊ณผ์ •์ด ์‹ค์ œ๋กœ๋Š” ์–ด๋–ป๊ฒŒ ์ด๋ฃจ์–ด์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 3x3 kernel * 4x4 input = 2x2 output ์„ ๋„์ถœํ•˜๊ธฐ ์œ„ํ•ด ์ผ๋ฐ˜์ ์ธ convolution์˜ matrix ์—ฐ์‚ฐ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. โ“ 3x3 kernel์€ 4x16 sparse matrix๋กœ ๋ณ€ํ™˜ โ“‘ 4x4 input์€ 16x1 vector๋กœ ๋ณ€ํ™˜ โ“’ 4x1 output vector๋ฅผ 2x2 output์œผ๋กœ ๋ณ€ํ™˜ Transposed convolution์€ 3x3 kernel * 2x2 input = 4x4 output์„ ๋„์ถœํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฐ์‚ฐํ•ฉ๋‹ˆ๋‹ค. โ“ 3x3 kernel์€ 16x4 sparse matrix๋กœ ๋ณ€ํ™˜ โ“‘ 2x2 input์€ 4x1 vector๋กœ ๋ณ€ํ™˜ โ“’16x1 output vector๋ฅผ 4x4 output์œผ๋กœ ๋ณ€ํ™˜ ๊ทธ๋ฆผ์„ ์ž์„ธํžˆ ๋ณด์‹œ๋ฉด transposed convolution ํ•  ๋•Œ ๊ฐ€์ค‘์น˜ w์˜ ์œ„์น˜๊ฐ€ transposed ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ๋Š” ๊ฐ€์ค‘์น˜ w์˜ ์œ„์น˜๋งŒ ์ „์น˜๋˜๊ณ  ๊ฐ’์€ ๋ณด์กด๋œ ๊ฒƒ์ฒ˜๋Ÿผ ๊ทธ๋ ค๋†จ์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ์œ„์น˜๋„ ์ „์น˜๋˜๊ณ , ๊ฐ€์ค‘์น˜์˜ ๊ฐ’๋„ ๋ณ€ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” input, output ๊ฐ’์„ ์•Œ๊ณ  ์žˆ์œผ๋ฏ€๋กœ ํ•™์Šต์„ ํ†ตํ•ด ์ตœ์ ์˜ ๊ฐ€์ค‘์น˜ ๊ฐ’์„ ์ฐพ์•„๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Transposed convolution์˜ ๊ตฌํ˜„ keras์—์„œ transposed convolution์„ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด Conv2DTranspose๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ stride ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ํ•˜๋Š”์ง€์— ๋”ฐ๋ผ output์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์•„๋ž˜ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๋ณด๋ฉด ์™œ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”๋Š”์ง€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ๋ž€์ƒ‰ feature map์ด 2x2 input์ด๊ณ , ์ดˆ๋ก์ƒ‰ feature map์ด 4x4 ๋˜๋Š” 5x5 output์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ Convolution๊ณผ Transposed Convolution์—์„œ stride์˜ ์˜๋ฏธ๊ฐ€ ๋ฐ˜๋Œ€์ž…๋‹ˆ๋‹ค. - ์ผ๋ฐ˜์ ์ธ Convolution์—์„œ stride 2 = ํ•œ ๋ฒˆ์— input ์›์†Œ ๋‘ ์นธ์”ฉ ์›€์ง์ธ๋‹ค. - Transposed Convolution์—์„œ stride 2 = 2์นธ ์›€์ง์—ฌ์•ผ ๋‹ค์Œ input ์›์†Œ์— ๋„๋‹ฌํ•œ๋‹ค. transposed convolution์—์„œ stride =1 ์ธ ๊ฒฝ์šฐ ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ž‘๋™ํ•˜์—ฌ ์ตœ์ข… output ํฌ๊ธฐ๋Š” 4x4์ž…๋‹ˆ๋‹ค. transposed convolution์—์„œ stride =2 ์ธ ๊ฒฝ์šฐ ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ž‘๋™ํ•˜์—ฌ ์ตœ์ข… output ํฌ๊ธฐ๋Š” 5x5์ž…๋‹ˆ๋‹ค. Reference CNN์—์„œ Pooling ์ด๋ž€? Pooling์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ , Pooling์˜ ํŠน์ง•, Pooling์˜ ํšจ๊ณผ ์›๋…ผ๋ฌธ | A guide to convolution arithmetic for deep learning JINHYO AI Blog | CS231n์˜ Transposed Convolution์€ Deconvolution์— ๊ฐ€๊นŒ์šด Transposed Convolution์ด๋‹ค Loner์˜ ํ•™์Šต๋…ธํŠธ |Transpose Convolution ๊ฐ„๋‹จ ์ •๋ฆฌ (3) ์ด๋ฏธ์ง€ ๋ถ„ํ•  ๋ชจ๋ธ๋“ค ์ด๋ฏธ์ง€ ๋ถ„ํ• ์˜ ๋Œ€ํ‘œ์ ์ธ ๋…ผ๋ฌธ์˜ ๋ชจ๋ธ๋“ค์„ ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค. 1) FCN FCN์€ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์—์„œ ์šฐ์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ธ CNN ๊ธฐ๋ฐ˜ ๋ชจ๋ธ(AlexNet, VGG16, GoogLeNet)์„ Semantic Segmentation Task๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณ€ํ˜•์‹œํ‚จ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. (FCN ๋„คํŠธ์›Œํฌ๋Š” ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ๋จผ์ € ํŠธ๋ ˆ์ด๋‹ ์‹œํ‚จ ํ›„ ๋ชจ๋ธ์„ ํŠœ๋‹ํ•ด ํ•™์Šตํ•˜๋Š” Transfer Learning์œผ๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค) ์ดํ›„ ๋‚˜์˜จ Semantic Segmentation ๋ฐฉ๋ฒ•์€ ๋Œ€๋ถ€๋ถ„ FCN์˜ ์•„์ด๋””์–ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ํ•ต์‹ฌ ์•„์ด๋””์–ด ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๋Š” ์ด๋ฏธ์ง€ ๋‚ด์˜ ๋ชจ๋“  ํ”ฝ์…€์—์„œ Feature๋ฅผ ์ถ”์ถœ(Extraction) ํ•˜๊ณ , ์ถ”์ถœํ•œ Feature๋“ค์„ ๋ถ„๋ฅ˜๊ธฐ(Classifier)์— ๋„ฃ์–ด ์ž…๋ ฅ ์ด๋ฏธ์ง€(Total)์˜ Class๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ตฌ์กฐ๋กœ ๋งŒ๋“ค์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„ํ• ์€ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์—์„œ ์ข€ ๋” ๋‚˜์•„๊ฐ€์„œ ์ด๋ฏธ์ง€(Total)์˜ Class๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ด๋ฏธ์ง€๋ฅผ ์ด๋ฃจ๋Š” ๋ชจ๋“  ํ”ฝ์…€๋“ค์˜ Class๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ๋กœ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. FCN์€ ๊ธฐ์กด ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์—์„œ ์“ฐ์ธ ๋„คํŠธ์›Œํฌ๋ฅผ ํŠธ๋ ˆ์ธ ๋œ ์ƒํƒœ์—์„œ(Pretrain) Feature Extraction ๋ ˆ์ด์–ด๋Š” ๊ทธ๋Œ€๋กœ ์žฌํ™œ์šฉํ•˜์—ฌ Feature๋ฅผ ์ถ”์ถœํ•˜๊ณ  FC ๋ ˆ์ด์–ด๋ฅผ ๋ฒ„๋ฆฌ๊ณ  1X1 Conv ๊ทธ๋ฆฌ๊ณ  Up-sampling(Transpose Convolution)๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ(Fine-Tuning) ํ”ฝ์…€ ํด๋ž˜์Šค ๋ถ„๋ฅ˜์™€ ์ž…๋ ฅ ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ์‚ฌ์ด์ฆˆ ํšŒ๋ณต์„ ํ•˜๋„๋ก ๋„คํŠธ์›Œํฌ๊ฐ€ ๊ตฌ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ FCN์˜ ๊ตฌ์กฐ๋Š” ํฌ๊ฒŒ 4๋‹จ๊ณ„๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. Convolution Layer๋ฅผ ํ†ตํ•ด Feature ์ถ”์ถœ 1x1 Convolution Layer๋ฅผ ์ด์šฉํ•ด ํ”ผ์ฒ˜ ๋งต์˜ ์ฑ„๋„์ˆ˜๋ฅผ ๋ฐ์ดํ„ฐ ์…‹ ๊ฐ์ฒด์˜ ๊ฐœ์ˆ˜์™€ ๋™์ผํ•˜๊ฒŒ ๋ณ€๊ฒฝ (Class Presence Heat Map ์ถ”์ถœ) Up-sampling: ๋‚ฎ์€ ํ•ด์ƒ๋„์˜ Heat Map์„ Upsampling(=Transposed Convolution) ํ•œ ๋’ค, ์ž…๋ ฅ ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ํฌ๊ธฐ์˜ Map ์ƒ์„ฑ ์ตœ์ข… ํ”ผ์ฒ˜ ๋งต๊ณผ ๋ผ๋ฒจ ํ”ผ์ฒ˜ ๋งต์˜ ์ฐจ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋„คํŠธ์›Œํฌ ํ•™์Šต ํ•˜์ง€๋งŒ ๊ธฐ๋ณธ FCN ๋„คํŠธ์›Œํฌ์—๋Š” ํฐ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. VGG16์—์„œ ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๊ฐ€ 224x224 ์ธ ๊ฒฝ์šฐ 5๊ฐœ์˜ convolution block์„ ํ†ต๊ณผํ•˜๋ฉด Feature map์˜ ํฌ๊ธฐ๋Š” 7x7์ด ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ FCN์—์„œ๋„ ๊ธฐ์กด ์ž…๋ ฅ ์ด๋ฏธ์ง€ (ํฌ๊ธฐ H x W)๊ฐ€ 5๊ฐœ์˜ convolution block์„ ํ†ต๊ณผํ•˜๋ฉด H/32 x W/32 ํฌ๊ธฐ์˜ Feature map์„ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Feature map์˜ ํ•œ ํ”ฝ์…€์€ ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ 32 x 32 pixel๋ฅผ ๋Œ€ํ‘œํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ Feature map์€ ๋‚ฎ์€ ํ•ด์ƒ๋„๋ฅผ ๊ฐ€์ ธ ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ '๋Œ€๋žต์ ์œผ๋กœ๋งŒ' ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋Š” 3๋ฒˆ Up-sampling ๊ณผ์ •์—์„œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ์ด๋ฏธ์ง€ ์œ„์น˜ ์ •๋ณด๋ฅผ '๋Œ€๋žต์ ์œผ๋กœ' ๊ฐ€์ง€๊ณ  ์žˆ๋Š” feature map์„ Up-sampling ํ•˜์—ฌ ์–ป์€ segmentation map์€ ๊ธฐ์กด ์ž…๋ ฅ ์ด๋ฏธ์ง€์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ ๋ญ‰๋šฑ๊ทธ๋ ค์ ธ ์žˆ๊ณ  ๋””ํ…Œ์ผํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. Up-sampling by Transposed convolution ๋ญ‰๊ทธ๋Ÿฌ์ง ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € ๋“œ๋Š” ์ƒ๊ฐ์€ Down-sampling์„ ํ•˜์ง€ ์•Š์•„ feature map์ด ์ž‘์•„์ง€์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Down-sampling์„ ํ†ตํ•ด feature map์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์ด๋Š” ๊ณผ์ •์ด ์—†๋‹ค๋ฉด ์—ฐ์‚ฐ๋Ÿ‰์ด ๊ธ‰๊ฒฉํžˆ ๋Š˜์–ด๋‚˜ ํ•™์Šต์— ํ•„์š”ํ•œ ์‹œ๊ฐ„ ๋ฐ ๋น„์šฉ์ด ๋„ˆ๋ฌด ์ปค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— Down-sampling์€ ํ•„์ˆ˜์ ์ด๊ณ  ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ํšจ๊ณผ์ ์ธ Up-sampling์„ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐœ ๊ณ ์•ˆ๋ฉ๋‹ˆ๋‹ค. (Up-sampling์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”) FCN์—์„œ๋Š” Transposed convolution์„ ์ด์šฉํ•˜์—ฌ Up-sampling์„ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Skip architecture FCN์˜ ๊ฐœ๋ฐœ์ž๋“ค์€ ์ข€ ๋” ๋””ํ…Œ์ผํ•œ segmentation map์„ ์–ป๊ธฐ ์œ„ํ•ด, Skip architecture๋ผ๋Š” ๊ธฐ๋ฒ•์„ ์ œ์•ˆํ–ˆ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ ์„ค๋ช…๊ณผ ๊ฐ™์ด ์ตœ์ข… ํ”ผ์ฒ˜ ๋งต(Feature map)์€ ์ง€์—ญ ์ •๋ณด๋ฅผ '๋Œ€๋žต์ ์œผ๋กœ ์œ ์ง€'ํ•˜๊ณ  ์žˆ์–ด ์ด๋ฏธ์ง€๊ฐ€ ๋ญ‰๊ฐœ์ง€๋Š” ๊ฒƒ์„ ๋ณด์™„ํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ํ”ผ์ฒ˜ ์ถ”์ถœ ๋‹จ๊ณ„์˜ ํ”ผ์ฒ˜ ๋งต๋„ ์—… ์ƒ˜ํ”Œ๋ง์— ํฌํ•จ(Sum) ํ•˜์—ฌ ์œ„์น˜ ์ •๋ณด ์†์‹ค์„ ๋ง‰ ์ž๋ผ๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ ์•„์ด๋””์–ด์ž…๋‹ˆ๋‹ค. FCN ํ™•์žฅ ๋ชจ๋ธ FCN-32s 5๋ฒˆ์˜ convolution block์„ ํ†ต๊ณผํ•ด 1/32๋งŒํผ ์ค„์–ด๋“  5๋ฒˆ Feature map(5๋ฒˆ Feature map ํฌ๊ธฐ๋Š” 7x7) 5๋ฒˆ Feature map์ด convolution layer๋ฅผ ํ†ต๊ณผํ•˜์—ฌ ๊ฐ™์€ ํฌ๊ธฐ์˜ 6๋ฒˆ Feature map์„ ์–ป์Œ 6๋ฒˆ Feature map์„ ํ•œ ๋ฒˆ์— 32๋ฐฐ upsampling (H/32 x W/32 ํฌ๊ธฐ๋ฅผ H x W ํฌ๊ธฐ๋กœ) FCN-16s 4๋ฒˆ์˜ convolution block์„ ํ†ต๊ณผํ•ด 1/16 ๋งŒํผ ์ค„์–ด๋“  4๋ฒˆ Feature map, 4๋ฒˆ Feature map ํฌ๊ธฐ๋Š” 14x14 6๋ฒˆ Feature map์„ 2๋ฐฐ upsampling (H/32 x W/32 ํฌ๊ธฐ๋ฅผ H/16 x W/16 ํฌ๊ธฐ๋กœ) ํ•œ ๊ฒƒ๊ณผ 4๋ฒˆ Feature map์„ Sum ํ•œ๋‹ค.(4-1๋ฒˆ Feature map) ์ƒˆ๋กญ๊ฒŒ ์–ป์€ 4-1๋ฒˆ Feature map ์„ ํ•œ ๋ฒˆ์— 16๋ฐฐ upsampling (H/16 x W/16 ํฌ๊ธฐ๋ฅผ H x W ํฌ๊ธฐ๋กœ) FCN-8s FCN-16s๊ณผ ์œ ์‚ฌํ•œ Step์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. 4-1๋ฒˆ Feature map์„ 2๋ฐฐ upsampling(H/16 x W/16 ํฌ๊ธฐ๋ฅผ H/8 x W/8 ํฌ๊ธฐ๋กœ) ํ•œ ๊ฒƒ๊ณผ 3๋ฒˆ Feature map์„ Sum ํ•œ๋‹ค.(3-1๋ฒˆ Feature map) 3-1๋ฒˆ Feature map์„ ํ•œ ๋ฒˆ์— 8๋ฐฐ upsampling (H/8 x W/8 ํฌ๊ธฐ๋ฅผ H x W ํฌ๊ธฐ๋กœ) ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๋ฉด ์œ„์น˜ ์ •๋ณด๊ฐ€ ๋” ์ž˜ ์ „๋‹ฌ๋˜์–ด FCN-32s โ†’ FCN-16s โ†’ FCN-8s ์ˆœ์„œ๋กœ ๋” ์ •๊ตํ•ด์กŒ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ํŠธ๋ ˆ์ด๋‹ ์ด๋ฏธ์ง€ ๋ถ„ํ•  ํŠธ๋ ˆ์ด๋‹์„ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์…‹์€ {์ธ๋ ฅ ๋ฐ์ดํ„ฐ : ์ด๋ฏธ์ง€, ๋ผ๋ฒจ ๋ฐ์ดํ„ฐ : ํ”ฝ์…€์ด ์†ํ•˜๋Š” ํด๋ž˜์Šค}๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด์„œ ๋‚˜์˜จ ์ถœ๋ ฅ๊ฐ’(Matrix)๋„ ๋ผ๋ฒจ ๋ฐ์ดํ„ฐ์™€ ๊ฐ™์€ ํฌ๋งท์œผ๋กœ ํ”ฝ์…€์˜ ํด๋ž˜์Šค๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ํ”ผ์ฒ˜ ๋งต์ž…๋‹ˆ๋‹ค. ์†์‹ค ํ•จ์ˆ˜(Loss function)์€ ๋ชจ๋“  ํ”ฝ์…€์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ๋ฅผ ๊ตฌํ•˜๊ณ  ์ด๋ฅผ ๋ชจ๋‘ ๋”ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ์†์‹ค(loss)๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. (๋…ผ๋ฌธ์—์„œ๋Š” Multinomial logistic loss๋ผ ํ‘œ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค) ๋„คํŠธ์›Œํฌ ์žฅ๋‹จ์  ๋ฐ ์‹œ์‚ฌ์  Skip architecture ์•„์ด๋””์–ด ์ตœ์ข…์ ์œผ๋กœ ๋‚˜์˜ค๋Š” ํ”ผ์ฒ˜์— local ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” Skip architecture ์•„์ด๋””์–ด๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํฐ ์ฆ๊ฐ€ ์—†์ด ์„ฑ๋Šฅ์„ ๋น„์•ฝ์ ์œผ๋กœ ํ–ฅ์ƒ์‹œ์ผฐ์„ ๋ฟ ์•„๋‹ˆ๋ผ, ํ›„์† ์—ฐ๊ตฌ์—๋„ ํฐ ์˜ํ–ฅ์„ ๋ฏธ์ณค์Šต๋‹ˆ๋‹ค. "์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜" ๋„คํŠธ์›Œํฌ๋ฅผ "์ด๋ฏธ์ง€ ๋ถ„ํ• " ๋„คํŠธ์›Œํฌ์— ์ ์šฉ ์„œ๋กœ ๋‹ค๋ฅธ Task์˜ ๋„คํŠธ์›Œํฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ์ž˜ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๋Š” ๋‹น์‹œ ์—„์ฒญ๋‚œ ์„ฑ๋Šฅ ํ–ฅ์ƒ์ด ์ด๋ค„์ง€๊ณ  ์žˆ๋Š” ์ƒํƒœ์˜€๊ณ , ์ด๋Ÿฌํ•œ ๋ฒ ์ด์Šค ๋ชจ๋ธ์„ ์ˆ˜์ •ํ•˜์—ฌ ์ด๋ฏธ์ง€ ๋ถ„ํ• ์— ์ ์šฉ์‹œ์ผœ ํฐ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋„คํŠธ์›Œํฌ์—์„œ ์ถ”์ถœํ•˜๋Š” ํ”ผ์ฒ˜๋ฅผ ์ด๋ฏธ์ง€ ๋ถ„ํ• ์— ์ˆ˜์ • ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์„ ๋ณด์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. FC ๋ ˆ์ด์–ด๋ฅผ ์‚ญ์ œํ•˜์—ฌ ์ด๋ฏธ์ง€ ๋ถ„ํ• ์„ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค AlexNet, VGG ๋“ฑ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์— ์ž์ฃผ ์“ฐ์ด๋Š” FC ๋ ˆ์ด์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ๋ถ„ํ• ์„ ํ•˜๋Š”๋ฐ ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์ด์œ  ๋ช‡ ๊ฐ€์ง€๋ฅผ ์ •๋ฆฌํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. โ‘  FC layer(Fully connected layer)๋ฅผ ํ†ต๊ณผํ•˜๊ณ  ๋‚˜๋ฉด ์ด๋ฏธ์ง€์˜ ์œ„์น˜ ์ •๋ณด๊ฐ€ ์‚ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. โ‘ก FC layer๋Š” ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ input image๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. โ‘ข FC layer๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์น˜ ์ •๋ณด์˜ ์†Œ์‹ค ๊ณ ์ •๋œ Input image ํฌ๊ธฐ Dense layer์— ๊ฐ€์ค‘์น˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ์•ž ๋ ˆ์ด์–ด์˜ Feature Map์˜ ํฌ๊ธฐ๋„ ๊ณ ์ •๋˜๋ฉฐ, ์—ฐ์‡„์ ์œผ๋กœ ๊ฐ ๋ ˆ์ด์–ด์˜ Feature Map ํฌ๊ธฐ์™€ Input Image ํฌ๊ธฐ ์—ญ์‹œ ๊ณ ์ •๋ฉ๋‹ˆ๋‹ค. FC layer๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ํ•„์š”๋กœ ํ•œ๋‹ค. FC ๋ ˆ์ด์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ”ฝ์…€๋งˆ๋‹ค์˜ ํด๋ž˜์Šค๋ฅผ ์˜ˆ์ธกํ•˜๊ฒŒ ๊ตฌ์„ฑํ•˜๊ฒŒ ํ•˜๊ฑฐ๋‚˜ ํ˜น์€ Class Presence Heat Map์„ ๋งŒ๋“ค๋ ค๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐํ„ฐ์˜ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„์ฃผ ์ž‘์€ 4X4์˜ ํ”ผ์ฒ˜ ๋งต์—์„œ 2X2(=4)์˜ ํžˆํŠธ๋งต์„ ์ถ”์ถœํ•ด๋‚ธ๋‹ค๊ณ  ํ•˜๋ฉด FC ๋ ˆ์ด์–ด๋กœ๋Š” 4X4X4๊ฐœ์˜ Weight๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ํ”ผ์ฒ˜ ๋งต์ด 4X4๋ณด๋‹ค ํ›จ์”ฌ ํฌ๊ณ , ๋˜ ๋’ค์ด์–ด์ง€๋Š” Up-sampling์„ ์ƒ๊ฐํ•˜๋ฉด ์–ด๋งˆ์–ด๋งˆํ•œ ์ปดํ“จํ„ฐ ์„ฑ๋Šฅ์„ ํ•„์š”๋กœ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด, FCN ๋ชจ๋ธ์€ ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ fully connected ๋ ˆ์ด์–ด๋ฅผ 1x1 convolution ๋ ˆ์ด์–ด๋กœ ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค. ๋ ˆํผ๋Ÿฐ์Šค DL | Sementic segmentation FCN ๋…ผ๋ฌธ ๋ฆฌ๋ทฐ โ€” Fully Convolutional Networks for Semantic Segmentation 2-1. Fully Convolutional Networks(FCN) Time Traveler ๋ธ”๋กœ๊ทธ 2) SegNet SegNet์€ 2016๋…„์— ๊ณต๊ฐœ๋œ ๋ชจ๋ธ๋กœ, ๋„๋กœ๋ฅผ ๋‹ฌ๋ฆฌ๋ฉด์„œ ์ดฌ์˜ํ•œ ์˜์ƒ(road scene)์— ๋Œ€ํ•ด pixel-wise semantic segmentation ํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ €์ž(Alex Kendall)์˜ ์œ ํŠœ๋ธŒ ์ฑ„๋„์—์„œ ์‹ค์ œ๋กœ road scene์— ์ ์šฉํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„๋กœ์™€ ๋ณด๋„๋Š” ์ผ๊ฒฌ ๋ณด๊ธฐ์— ๋น„์Šทํ•ด ๋ณด์ด์ง€๋งŒ ๋‘ class ๊ฐ„์˜ ๊ฒฝ๊ณ„๋ฅผ ์ž˜ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. (ํ•ด๋‹น ๋ชจ๋ธ์€ ์ž์œจ์ฃผํ–‰์— ํฐ ์—ญํ• ์„ ํ•œ ๋ชจ๋ธ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋„๋กœ์™€ ๋ณด๋„๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ๋ชปํ•˜๋ฉด ์ •๋ง ํฐ ์‚ฌ๊ณ ๊ฐ€ ๋‚˜๊ฒ ์ฃ ?) ๋ฌธ์ œ๋Š” ๊ธฐ์กด์˜ sementic segmentation ๋ชจ๋ธ๋“ค์˜ ํ•ด์ƒ๋„๊ฐ€ ๋งค์šฐ ๋–จ์–ด์ง„๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. Max pooling, sub-sampling ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋‹ค ๋ณด๋ฉด coarse(์ถ”์ƒ์ ์ธ, ์•Œ๋งน์ด๊ฐ€ ํฐ) feature map์ด ๋งŒ๋“ค์–ด์ง€๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋Ÿฐ feature map์œผ๋กœ๋Š” ํ”ฝ์…€ ๋‹จ์œ„๋กœ ์ •๊ตํ•˜๊ฒŒ segmentation์„ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. FCN์—์„œ๋Š” ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Skip architecture ๋“ฑ ๋‹ค์–‘ํ•œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ–ˆ์—ˆ์œผ๋‚˜, ์—ญ์‹œ ์ •๊ตํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ž์œจ์ฃผํ–‰์„ ์œ„ํ•ด์„œ๋Š” ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋น ๋ฅด๊ฒŒ segmentation์ด ๊ฐ€๋Šฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ •ํ™•๋„๊ฐ€ ๋†’๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๊ณ„์‚ฐ๋Ÿ‰์ด ๋งŽ๊ฑฐ๋‚˜ parameter ์ˆ˜๊ฐ€ ๋งŽ์œผ๋ฉด ๋น ๋ฅด๊ฒŒ segmentation์„ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ memory ๋ฐ inference time ์ธก๋ฉด์—์„œ ํšจ์œจ์ ์œผ๋กœ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ ์ž ๋“ฑ์žฅํ•œ ๋ชจ๋ธ์ด SegNet์ž…๋‹ˆ๋‹ค. SegNet์€ Encoder - Decoder๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๊ณ , ํ•ด๋‹น ๊ตฌ์กฐ๋Š” ์ตœ๊ทผ์˜ ๋ชจ๋ธ์—์„œ๋„ ํ”ํžˆ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ๋งŒํผ sementic segmantation ๋ถ„์•ผ์— ์ „์ฒด์ ์œผ๋กœ ํฐ ์˜ํ–ฅ์„ ๋ผ์นœ ๊ตฌ์กฐ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ 1) Encoder Network ์›๋ณธ ๋…ผ๋ฌธ์—์„œ๋Š” VGG16์˜ ๊ตฌ์กฐ์—์„œ FC layer๋ฅผ ๋บ€ 13๊ฐœ์˜ layer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ์‚ฐ๋Ÿ‰์„ ์ค„์˜€์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ encoder-decoder network์—์„œ encoder๋กœ CNN์„ ์ด์šฉํ•  ๊ฒฝ์šฐ FC layer๋ฅผ ๋บ๋‹ˆ๋‹ค. Classification์„ ํ•  ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฉด FC๊ฐ€ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•˜์ง€๋„ ์•Š๋Š” ๋ฐ๋‹ค๊ฐ€ ๊ณ„์‚ฐ๋Ÿ‰์ด ๋„ˆ๋ฌด ๋งŽ์ด ๋“ค๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . 2) Decoder Network Decoder network๋Š” ๊ฐ encoder network์— ๋Œ€์‘ํ•˜์—ฌ (mirrored) ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. Encoder์˜ pooling layer๋Š” Up-sampling layer๋กœ ๋Œ€์ฒด๋˜์–ด ์žˆ๊ณ  Up-sampling layer ๋’ค์—๋Š” Conv + batch norm + ReLU๊ฐ€ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค. Coarse feature map์ด ์ƒ๊ธฐ๋Š” ์ด์œ ๋Š” pooling ๋ฐ convolution ์—ฐ์‚ฐ ๋•Œ๋ฌธ์— feature map์˜ ์ •๋ณด๊ฐ€ ์†Œ์‹ค๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. Decoder์—์„œ up-sampling์„ ํ•  ๋•Œ Encoder์˜ feature map ์ •๋ณด๋ฅผ decoder๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์†Œ์‹ค๋œ ์ •๋ณด๋ฅผ ๋‹ค์‹œ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ˆ๊นŒ pixel ๋‹จ์œ„๋กœ ์ •๊ตํ•˜๊ฒŒ segmentation์„ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Encoder์˜ feature map ์ •๋ณด๋ฅผ decoder๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฐ€์žฅ ์ •ํ™•ํ•œ ๋ฐฉ๋ฒ•์€ ์ „์ฒด feature map์„ ์ €์žฅํ•ด ๋‘์—ˆ๋‹ค๊ฐ€ Up-sampling ํ•  ๋•Œ ๋ชจ๋‘ Decoder๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ์‹์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ, SegNet์—์„œ๋Š” max-pooling indices (์œ„์น˜ ์ •๋ณด)๋งŒ์„ ์ €์žฅํ•ด๋‘์—ˆ๋‹ค ์ดํ›„ Max Unpooling์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. (Max Unpooling์— ๋Œ€ํ•ด์„œ๋Š” ๋”ฐ๋กœ ์ •๋ฆฌํ•ด๋‘์—ˆ์œผ๋‹ˆ Up-sampling ์ด ๊ธ€์„ ์ฐธ๊ณ ํ•˜์„ธ์š”.) ๊ทธ๋Ÿฌ๋ฉด accuracy๋Š” ์•„์ฃผ ์กฐ๊ธˆ ๊ฐ์†Œํ•˜์ง€๋งŒ memory๋Š” ํฌ๊ฒŒ ์•„๋‚„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Max Unpooling ๋ฐฉ์‹์œผ๋กœ Up-sampling์„ ํ•˜๋ฉด ๊ทธ ์™ธ์—๋„ ์•„๋ž˜์™€ ๊ฐ™์€ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 1. FCN์—์„œ๋Š” Transposed convolution์œผ๋กœ upsampling์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. SegNet์€ Transposed convolution์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•™์Šต ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†์–ด ์ „์ฒด parameter ๊ฐœ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. ์ „์ฒด ๋ชจ๋ธ์€ end-to-end ํ•™์Šต์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 3. ๋‹ค๋ฅธ encoder-decoder<NAME>์— ์‘์šฉ๋  ์ˆ˜ ์žˆ๊ณ , ๋ณ€ํ˜•๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 3) Softmax classifier Decoder์˜ output์€ K-class softmax classifier๋กœ ๋“ค์–ด๊ฐ€ ์ตœ์ข…์ ์œผ๋กœ๋Š” ๊ฐ ํ”ฝ์…€๋งˆ๋‹ค์˜ ๋…๋ฆฝ์ ์ธ ํ™•๋ฅ  ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ์œ„ ๊ฒฐ๊ณผ์—์„œ, ๊ฐ ํ”ฝ์…€ ๋ณ„๋กœ ๊ฐ€์žฅ ํ™•๋ฅ ์ด ๋†’์€ class๋งŒ์„ ์ถœ๋ ฅํ•˜๋ฉด ์ตœ์ข… segmentation์ด ๋ฉ๋‹ˆ๋‹ค. SegNet์˜ ์„ฑ๋Šฅ ์› ๋…ผ๋ฌธ์€ SegNet์˜ ์„ฑ๋Šฅ์„ ๋‹ค๋ฅธ architecture (FCN, DeepLab-LargeFOV, DeconvNet ๋“ฑ)์™€ ๋น„๊ตํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ œ์‹œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. SegNet์˜ ์„ฑ๋Šฅ์„ 2๊ฐ€์ง€ scene segmentation benchmark์—์„œ ํ‰๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 1. Cam Vid dataset - road scene segmentation 2. SUN RGB-D dataset - indoor scene segmentation ์ •๋Ÿ‰์  ํ‰๊ฐ€ ์ฒ™๋„๋กœ ์ด 4๊ฐ€์ง€ metric์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. 1. Global accuracy (G): ๋ฐ์ดํ„ฐ ์…‹ ์ „์ฒด์˜ ํ”ฝ์…€ ์ˆ˜์—์„œ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋ถ„๋ฅ˜๋œ ํ”ฝ์…€์˜ ์ˆ˜ 2. Class average accuracy (C): ๊ฐ ํด๋ž˜์Šค๋งˆ๋‹ค accuracy๋ฅผ ๊ณ„์‚ฐํ•œ ๋’ค, ํ‰๊ท  ๋‚ธ ๊ฒƒ 3. mIoU 4. Boundary F1 Score (BF): 2 * precision * recall / (recall + precision) Cam Vid dataset SegNet, DeconvNet์ด ๊ฐ€์žฅ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค SUN RGB-D dataset SegNet์€ ๋‹ค๋ฅธ ๋ชจ๋ธ๋“ค๊ณผ ๋น„๊ตํ•ด์„œ G, C, mIoU, BF ๋ชจ๋‘์—์„œ ์šฐ์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค Reference https://eremo2002.tistory.com/120 SegNet์˜ ์›๋ฆฌ SegNet: A Deep Convolutional Encoder-Decoder Architecture for Image Segmentation 3) U-Net ์›๋ž˜ UNET์€ ISBI cell tracking challenge 2015 ๋Œ€ํšŒ์—์„œ ๋“ฑ์žฅํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด ๋Œ€ํšŒ์—์„œ ์‚ฌ์šฉ๋œ ๋ฐ์ดํ„ฐ ์…‹์˜ ํŠน์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Dataset: Transmitted light microscopy images (Phase contrast and DIC) Transmitted light microscopy images: ํˆฌ๊ณผ ๊ด‘์„  ํ˜„๋ฏธ๊ฒฝ ๊ฒ€์‚ฌ Phase contrast: ์œ„์ƒ์ฐจ ํ˜„๋ฏธ๊ฒฝ DIC (Differential interference contrast microscope) = ์ฐจ๋“ฑ๊ฐ„์„ญ๋Œ€๋น„ ํ˜„๋ฏธ๊ฒฝ Input size: 512x512 pixel, 30๊ฐœ(๋”ฅ๋Ÿฌ๋‹์— ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ณ ์ž‘ 30๊ฐœ์ž…๋‹ˆ๋‹ค...) ๋„คํŠธ์›Œํฌ ํ•ต์‹ฌ ์•„์ด๋””์–ด U-Net ๋„คํŠธ์›Œํฌ๋Š” ๋งค์šฐ ์•„๋ฆ„๋‹ค์šด(?) ๋ชจ์Šต์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด๋ฆ„๋„ ๋„ˆ๋ฌด ์ž˜ ์ง€์–ด(?) ๋„คํŠธ์›Œํฌ ๋ชจ์–‘์ด ์ •๋ง U์ž…๋‹ˆ๋‹ค. U-Net ๋„คํŠธ์›Œํฌ์˜ ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š” 3๊ฐ€์ง€๋กœ ์š”์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ, ์ธ์ฝ”๋”(Contract path)์˜ ํ”ผ์ฒ˜ ๋งต์„ ๋””์ฝ”๋” ํ”ผ์ฒ˜ ๋งต์— Concat ํ•˜์—ฌ ์œ„์น˜ ์ •๋ณด ์ „๋‹ฌ ๋‘ ๋ฒˆ์งธ, ๋ฐ์ดํ„ฐ ์…‹์˜ ์ „์ฒ˜๋ฆฌ, ๋ณ€ํ˜•(deformation)์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ˆ˜ ์ฆ๊ฐ€ ์„ธ ๋ฒˆ์งธ, ํ…Œ๋‘๋ฆฌ(border line)๋ฅผ ๋” ์ž˜ ๋ถ„ํ• ํ•˜๊ธฐ ์œ„ํ•ด Weight๋ฅผ ์ถ”๊ฐ€ํ•œ ์†์‹ค ํ•จ์ˆ˜(Loss function) ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ UNET์€ 3๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ Semantic Segmentation ๋ชจ๋ธ๋“ค์€ Down-sampling์„ ํ†ตํ•ด ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“ค์—ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ Up-sampling์„ ํ†ตํ•ด ํฌ๊ธฐ๊ฐ€ ๋Š˜์–ด๋‚˜๋Š” ๊ตฌ์กฐ๋ฅผ ์ทจํ•˜๋Š”๋ฐ, U-Net ๋…ผ๋ฌธ์—์„œ๋Š” ์ด๋ฅผ ๊ฐ๊ฐ Contracting Path, Expanding Path๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. Contracting Path: ์ ์ง„์ ์œผ๋กœ ๋„“์€ ๋ฒ”์œ„์˜ ์ด๋ฏธ์ง€ ํ”ฝ์…€์„ ๋ณด๋ฉฐ ์˜๋ฏธ ์ •๋ณด(Context Information)์„ ์ถ”์ถœ Bottle Neck: ์ˆ˜์ถ• ๊ฒฝ๋กœ์—์„œ ํ™•์žฅ ๊ฒฝ๋กœ๋กœ ์ „ํ™˜๋˜๋Š” ์ „ํ™˜ ๊ตฌ๊ฐ„ Expanding Path: ์˜๋ฏธ ์ •๋ณด๋ฅผ ํ”ฝ์…€ ์œ„์น˜์ •๋ณด์™€ ๊ฒฐํ•ฉ(Localization) ํ•˜์—ฌ ๊ฐ ํ”ฝ์…€๋งˆ๋‹ค ์–ด๋–ค ๊ฐ์ฒด์— ์†ํ•˜๋Š”์ง€๋ฅผ ๊ตฌ๋ถ„ Skip Architecture U-Net์—์„œ๋„ FCN๊ณผ ๋น„์Šทํ•˜๊ฒŒ Skip Architecture๋ฅผ ํ™œ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋™์ผํ•œ Level์—์„œ ๋‚˜์˜จ Feature map์„ ๋”ํ•œ๋‹ค๋Š” ์ ์ด FCN์˜ Skip architecture์™€ ๋‹ค๋ฅธ ์ ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๊ทธ๋ฆผ์„ ์ž์„ธํžˆ ๋ณด์‹œ๋ฉด Contracting Path์˜ Feature map์ด Expanding Path์˜ Feature map๋ณด๋‹ค ํฐ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” Contracting Path์—์„œ ์—ฌ๋Ÿฌ ๋ฒˆ์˜ ํŒจ๋”ฉ์ด ์—†๋Š” 3ร—3 Convolution Layer๋ฅผ ์ง€๋‚˜๋ฉด์„œ Feature map์˜ ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“ค๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. Contracting Path์˜ Feature map์˜ ํ…Œ๋‘๋ฆฌ ๋ถ€๋ถ„์„ ์ž๋ฅธ ํ›„ ํฌ๊ธฐ๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งž์ถ”์–ด ๋‘ feature map์„ ํ•ฉ์ณ ์ค๋‹ˆ๋‹ค. ์ž…๋ ฅ ์ด๋ฏธ์ง€(Input image)์— ์ ์šฉํ•œ ๊ธฐ๋ฒ•๋“ค ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ์ €์ž๋“ค์€ input image์— ๋‹ค์–‘ํ•œ ๊ธฐ๋ฒ•๋“ค์„ ์ ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. โ‘  Overlap-tile strategy & Mirroring Extrapolation โ‘ก Data Augmentation ์ด์— ๋Œ€ํ•ด ํ•˜๋‚˜ํ•˜๋‚˜ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์˜๋ฃŒ ๋ฐ์ดํ„ฐ๋Š” ํฌ๊ธฐ๊ฐ€ ์ฒœ์ฐจ๋งŒ๋ณ„์ด๊ณ  ์‚ฌ์ด์ฆˆ๋‚˜ ํ•ด์ƒ๋„๊ฐ€ ํฐ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํฌ๊ธฐ๋ฅผ ์ ๋‹นํžˆ ์ž˜๋ผ์„œ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์— ์ œ์‹œํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์€ sliding window๊ฐ€ ์ „์ฒด ์ด๋ฏธ์ง€ ์œ„๋กœ ์กฐ๊ธˆ์”ฉ ์ด๋™ํ•˜์—ฌ ํ•ด๋‹น ์˜์—ญ์„ ๊ฐ๊ฐ์˜ DNN์— ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฏธ ์‚ฌ์šฉํ•œ ์˜์—ญ์˜ ์ผ๋ถ€๋ฅผ ๋‹ค์Œ ์ˆœ์„œ์—์„œ๋„ ๋‹ค์‹œ ํ›‘์–ด๋ณด๊ธฐ ๋•Œ๋ฌธ์— ๊ณ„์‚ฐ๋Ÿ‰์ด ๋งŽ์•„์ง‘๋‹ˆ๋‹ค. ๋‹น์—ฐํžˆ input image ํฌ๊ธฐ๊ฐ€ ํด์ˆ˜๋ก ๋ชจ๋ธ์˜ ์†๋„๊ฐ€ ํ™•์—ฐํžˆ ์ €ํ•˜๋ฉ๋‹ˆ๋‹ค. UNET์˜ ์ €์ž๋“ค์€ patch ํƒ์ƒ‰ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ Patch ํƒ์ƒ‰์€ ์ด๋ฏธ์ง€๋ฅผ ๊ฒฉ์ž ๋ชจ์–‘์œผ๋กœ ์ž˜๋ž๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์ฒฉ๋˜๋Š” ๋ถ€๋ถ„์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ ํ›‘์–ด๋ณธ ์˜์—ญ์€ ๊น”๋”ํ•˜๊ฒŒ ๋„˜๊ธฐ๊ณ  ๋‹ค์Œ patch๋ถ€ํ„ฐ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ ์†๋„ ๋ฉด์— ์žˆ์–ด ํ›จ์”ฌ ๋‚˜์€ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋‹ค์–‘ํ•œ ํฌ๊ธฐ์˜ input image๋ฅผ ์–ด๋–ป๊ฒŒ UNET ๋ชจ๋ธ์— ๋„ฃ์„ ์ผ์ •ํ•œ ํฌ๊ธฐ์˜ patch๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„๊นŒ์š”? ๋˜ ๋ถ€์กฑํ•œ ๋ฐ์ดํ„ฐ์˜ ์ˆซ์ž๋ฅผ ์–ด๋–ป๊ฒŒ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ์„๊นŒ์š”? ๋…ผ๋ฌธ์˜ ์ €์ž๋“ค์€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด๋กœ ์ด ๋ฌธ์ œ๋ฅผ ๋ฉ‹์ง€๊ฒŒ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ „(?)์— ๋ฐฐ์šด ์„ธํฌ ๋“ฑ์˜ ๋ฐ์ดํ„ฐ์—๋Š” ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋ฐ˜๋ณต์ ์ธ ๊ตฌ์กฐ๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์˜ ์ €์ž๋“ค์€ ์ปดํŒจํ‹ฐ์…˜ ๋Œ€ํšŒ์˜ ์˜ํ•™ ๋ฐ์ดํ„ฐ์— ๋‘ ๊ฐ€์ง€ ๊ฐ€์ •์„ ์„ธ์› ์Šต๋‹ˆ๋‹ค. โ‘  ์ผ๋‹จ ์ž‘๊ฒŒ ์ž๋ฅด๊ณ  ๋ถ€์กฑํ•œ ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€๋Š” ํ…Œ๋‘๋ฆฌ์— ์ขŒ์šฐ๋Œ€์นญ ํŒจ๋”ฉ(Padding)์„ ํ•˜์ž. ํ…Œ๋‘๋ฆฌ๋ฅผ ์ขŒ์šฐ๋Œ€์นญ์„ ํ•˜๋Š” ๊ฒƒ์ด 0์œผ๋กœ ์ฑ„์›Œ ๋„ฃ๋Š” ์ œ๋กœ ํŒจ๋”ฉ๋ณด๋‹ค ์‹ค์ œ ์‚ญ์ œ๋˜์ง€ ์•Š์€ ๋ชจ์Šต์— ๋” ๊ฐ€๊นŒ์šธ ๊ฒƒ์ด๋‹ค. โ‘ก Cell๋“ค์€ ์œ ์‚ฌํ•œ ํ˜•์ƒ์ด๋ฏ€๋กœ ๊ธฐ์ดˆ์ ์ธ ๋ณ€ํ˜•๋“ค(deformation)์„ ์ฃผ์–ด๋„ ์‹ค์ œ๋กœ ์žˆ์„๋ฒ•ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ์•„๋ž˜์—์„œ ๋‘ ๊ฐ€์ง€ ๊ฐ€์ •์„ ์ด์šฉํ•˜์—ฌ ๋…ผ๋ฌธ์—์„œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ์™€ ๋ณ€ํ˜•์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋Š”์ง€ ์ข€ ๋” ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. (์•„๋งˆ, ๋‘ ๊ฐ€์ง€ ๊ฐ€์„ค์ด ์ž˜ ๋“ค์–ด๋งž์•˜๊ธฐ์— ๋‹น์‹œ์— ํฐ ๊ฒฉ์ฐจ๋กœ SOTA์˜ ์œ„์น˜๋ฅผ ์ฐจ์ง€ํ–ˆ๊ฒ ์ฃ ?) โ‘  Overlap-tile strategy & Mirroring Extrapolation ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ UNET์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด 388ร—388 ํฌ๊ธฐ์˜ Segmentation map์„ ์–ป๊ธฐ ์œ„ํ•ด 572ร—572 ํฌ๊ธฐ์˜ Input image๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ํŒŒ๋ž€์ƒ‰ patch๋ฅผ Input image๋กœ ์ œ์‹œํ•˜๋ฉด ๋…ธ๋ž€์ƒ‰ ์˜์—ญ์˜ Segmentation map์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“ค์–ด ๋‚˜์˜ค๋Š” ์ด์œ ๋Š” UNET์—์„œ padding ์—†์ด Convolution ์—ฐ์‚ฐ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜ํ–‰ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ™์€ missing data ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ ๊ฒƒ์ด mirroring extrapolation์ž…๋‹ˆ๋‹ค. ํŒŒ๋ž€์ƒ‰ ๋ฐ•์Šค์˜ ๋นˆ ๊ณต๊ฐ„์„ ๋…ธ๋ž€์ƒ‰ ์˜์—ญ์ด ๊ฑฐ์šธ์— ๋ฐ˜์‚ฌ๋œ ํ˜•ํƒœ๋กœ ์ฑ„์šฐ๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ํ•ด๋‹น ์˜์—ญ์„ zero pixel๋กœ ์ฑ„์šธ ์ˆ˜๋„ ์žˆ์—ˆ๊ฒ ์ง€๋งŒ mirroring extrapolation์„ ํ•˜๋ฉด์„œ data augmentation์˜ ํšจ๊ณผ๋ฅผ ์ค„ ์ˆ˜ ์žˆ์–ด ์ขŒ์šฐ๋Œ€์นญ์„ ํ•ด๋„ ํฐ ์˜ํ–ฅ์ด ์—†๋Š” ์„ธํฌ ๋“ฑ์˜ ์˜ํ•™ ๋ฐ์ดํ„ฐ์—์„œ๋Š” zero padding ๋ณด๋‹ค ๋‚˜์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. โ‘ก Data Augmentation Sementic segmentation์€ pixel ๋ณ„๋กœ class labeling ํ•ด์ฃผ์–ด์•ผ ํ•ด์„œ ํ•™์Šต ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์€ ํŽธ์ž…๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ISBI cell tracking challenge 2015์—์„œ ์ œ๊ณตํ•œ ๋ฐ์ดํ„ฐ๋Š” ๊ณ ์ž‘ 30๊ฐœ์ž…๋‹ˆ๋‹ค. UNET ์ €์ž๋“ค์ด ๋…ผ๋ฌธ์—์„œ ์–ธ๊ธ‰ํ•œ data augmentation ๋ฐฉ์‹์€ ์ด 4๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. โ‘  Shift โ‘ก Rotation โ‘ข Gray value โ‘ฃ Elastic Deformation ๊ทธ์ค‘ Elastic Deformation์€ Pixel์ด ๋žœ๋คํ•˜๊ฒŒ ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ ๋’คํ‹€๋ฆฌ๋„๋ก ๋ณ€ํ˜•ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด elastic deformation์„ ํ•ด๋„ ํ˜„์‹ค ์„ธ๊ณ„์— ์žˆ์„ ๋ฒ•ํ•˜๊ฒŒ ๋ณ€ํ˜•๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์ธ ์ด๋Ÿฌํ•œ ๊ธฐ๋ฒ•๋“ค์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ๊ฐ€์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ํŠธ๋ ˆ์ด๋‹ ๋…ผ๋ฌธ์˜ ์ €์ž๋“ค์€ ์†์‹ค ํ•จ์ˆ˜(Loss func)๋ฅผ ํ”ฝ์…€๋งˆ๋‹ค์˜ ๊ตฌํ•œ ์—๋„ˆ์ง€ ํŽ‘์…˜(Energy function)์˜ ์ดํ•ฉ์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ”ฝ์…€์˜ ์˜ˆ์ธก๊ฐ’(Prediction)์— SoftMax๋ฅผ ๊ตฌํ•˜๊ณ  ์ด ๊ตฌํ•œ ๊ฐ’์— ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ(Cross Entropy)๋ฅผ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŠน์ดํ•˜๊ฒŒ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ์— ํ”ฝ์…€ ๊ณ ์œ ์˜ Weight๋ฅผ ๊ณฑํ•จ์œผ๋กœ์จ ํ”ฝ์…€์˜ Loss ๊ฐ’์„ ๊ณ„์‚ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ”ฝ์…€ ๊ณ ์œ ์˜ Weight๋Š” ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ Weight๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ. ๋…ผ๋ฌธ์˜ ์ €์ž๋“ค์ด ๊ฒฝ๊ณ„์„ (border) ๋ผ์ธ์— ๋” ๊ฐ•ํ•œ ํ•™์Šต์„ ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ๊ฐ€์šฐ์‹œ์•ˆ ๋ถ„ํฌ(Gaussian distribution)์„ ๊ฐ€์ •ํ•˜๊ณ  ๊ฒฝ๊ณ„์„  ํ”ฝ์…€ ๋” ํฐ Loss๋ฅผ ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํ”ฝ์…€๋งˆ๋‹ค ๊ฐ์ž์˜ Weight๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๋งŒ์•ฝ ํ”ฝ์…€์ด ๊ฒฝ๊ณ„์„  ํ”ฝ์…€์ผ ๊ฒฝ์šฐ ๋” ํฐ ์†์‹ค ๊ฐ’(Loss)์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. (ํ•™์Šต์ „์— ๊ฒฝ๊ณ„์„ ๊ณผ ๊ฒฝ๊ณ„์„  ์•„๋‹Œ ํ”ฝ์…€๋“ค์˜ Weight๋ฅผ ๋ชจ๋‘ ๊ตฌํ–ˆ๊ณ  ์ด๊ฒƒ์„ weight map์œผ๋กœ ๊ตฌํ˜„ํ•˜์—ฌ ๊ฐœ๋ฐœ ์‹œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค) ์ด๋Ÿฌํ•œ ๊ฒฝ๊ณ„์„ ์— ํŠนํ™”๋œ ๋กœ์Šค ํŽ‘์…˜์„ ํ†ตํ•ด์„œ ๋”์šฑ Segmentation์— ์ตœ์ ํ™”๋œ ํ•™์Šต์„ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. โ€ป ์ˆ˜์‹ ๋ฐ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ์•Œ๊ณ  ์‹ถ์€ ๋ถ„์€ Time Traveler ๋ธ”๋กœ๊ทธ์— ์ž์„ธํžˆ ๋‚˜์™€ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”. ๋„คํŠธ์›Œํฌ ์žฅ์ /๋‹จ์ /ํ•œ๊ณ„์  U-Net ๋„คํŠธ์›Œํฌ๋Š” FCN์—์„œ ๋‚˜์˜จ ์•„์ด๋””์–ด์ธ Skip architecture๋ฅผ ๋”์šฑ ํ™•์žฅํ•ด์„œ ์‚ฌ์šฉํ•œ ๋ชจ์Šต์„ ๋ณด์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์ดํ›„ ๊ฐœ๋ฐœ๋œ ํ™”์ง€ ๋ถ„๋ฆฌ(Speech Separation) ๋“ฑ์˜ Segment๋ฅผ ํ•˜๋Š” ๋„คํŠธ์›Œํฌ๋Š” U-Net์˜ ํ˜•์ƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ด ๋…ผ๋ฌธ์€ ์ œ์•ˆํ•œ ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ๋„ ์šฐ์ˆ˜ํ•˜์ง€๋งŒ ํฌ์ธํŠธ๋Š” ๋ถ€์กฑํ•œ ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆํ•œ ์ „์ฒ˜๋ฆฌ(Preprocessing)๊ณผ ๋ฐ์ดํ„ฐ ๋ณด์ถฉ(Data augmentation)์— ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ํŠน์ • ๋ฐ์ดํ„ฐ์— ํ•œ์ •๋œ ๊ธฐ๋ฒ•์ด๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ๋ฐ์ดํ„ฐ ๋„๋ฉ”์ธ์— ๋Œ€ํ•œ ๊นŠ์€ ์ดํ•ด๋ฅผ ํ† ๋Œ€๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์ธ์ƒ ๊นŠ์€ ๋…ผ๋ฌธ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋ ˆํผ๋Ÿฐ์Šค [๋…ผ๋ฌธ ๋ฆฌ๋ทฐ] - U-Net : Convolutional Networks for Biomedical Image Segmentation, MICCAI 2015 [Paper Review] U-Net: Convolutional Networks for Biomedical Image Segmentation Youtube|The right way to segment large images by applying a trained U-Net model on smaller patches Time Traveler 4) DeepLab V3+ DeepLab v1~v3+ architecture๋Š” ๊ตฌ๊ธ€์—์„œ ์ œ์‹œํ•œ ๋ชจ๋ธ๋กœ, 2015๋…„๋ถ€ํ„ฐ ํ˜„์žฌ์— ์ด๋ฅด๊ธฐ๊นŒ์ง€ ๊ณ„์†ํ•ด์„œ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•˜๊ณ  ์žˆ๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. DeepLab ์‹œ๋ฆฌ์ฆˆ๋Š” ์—ฌ๋Ÿฌ segmentation model ์ค‘ ์„ฑ๋Šฅ์ด ์ƒ์œ„๊ถŒ์— ํฌ์ง„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. DeepLab ์‹œ๋ฆฌ์ฆˆ์˜ ๋ฐœ์ „์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ด๋ฃจ์–ด์กŒ๋Š”๋ฐ, ์ตœ๊ทผ ๋ชจ๋ธ๋“ค์€ ๋ชจ๋‘ ์•ž์„  ๋ชจ๋ธ๋“ค์„ ๊ณ„์Šนํ•˜๋ฉด์„œ ์กฐ๊ธˆ์”ฉ ์ถ”๊ฐ€๋˜๋Š” ํ•ญ๋ชฉ์ด ์ƒ๊น๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ฐœ๋…๋“ค์„ ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด๋ฉด์„œ ๊ฐ ๋ชจ๋ธ ๋ณ„ ์ฐจ์ด์ ์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. DeepLab V1 : Atrous convolution์„ ์ฒ˜์Œ ์ ์šฉ DeepLab V2 : multi-scale context๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ Atrous spatial pyramid pooling(ASPP) ์ œ์•ˆ DeepLab V3 : ๊ธฐ์กด ResNet ๊ตฌ์กฐ์— Atrous convolution์„ ํ™œ์šฉ DeepLab V3+ : Depthwise separable convolution๊ณผ Atrous convolution์„ ๊ฒฐํ•ฉํ•œ Atrous separable convolution์„ ์ œ์•ˆ. ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ฐœ๋… Atrous convolution classification์ด๋‚˜ object detection์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์‹ ๊ฒฝ๋ง๋“ค์„ semantic segmentation์— ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ์—ฌ๋Ÿฌ ๋ฒˆ์˜ convolution๊ณผ pooling์„ ๊ฑฐ์น˜๋ฉด์„œ ๋””ํ…Œ์ผํ•œ ์ •๋ณด๊ฐ€ ์ค„์–ด๋“ค๊ณ  ํŠน์„ฑ์ด ์ ์  ์ถ”์ƒํ™”๋˜๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. FCN์˜ ๊ฒฝ์šฐ ์ด๋ฅผ skip layer๋ฅผ ํ†ตํ•ด ํ•ด๊ฒฐํ–ˆ์ง€๋งŒ, skip layer๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด receptive field๊ฐ€ ๊ณ ์ •๋˜์–ด ๋‹ค์–‘ํ•œ scale์˜ object์— ๋Œ€์‘ํ•˜๊ธฐ ํž˜๋“ค๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Deep lap V1~ V3์—์„œ๋Š” Atrous convolution์„ ์ด์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. (Atrous convolution์— ๋Œ€ํ•ด์„œ๋Š” ๋”ฐ๋กœ ์ •๋ฆฌํ•˜๊ณ  ์žˆ์œผ๋‹ˆ Down-sampling์„ ์ฝ์–ด๋ณด์„ธ์š”.) Atrous convolution์„ ์ด์šฉํ•˜๋ฉด ๊ธฐ์กด convolution๊ณผ ๋™์ผํ•œ ์–‘์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๊ณ„์‚ฐ๋Ÿ‰์„ ์œ ์ง€ํ•˜๋ฉด์„œ๋„, receptive field๋Š” ์ปค์ง‘๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ๋‹จ์ˆœํžˆ Pooling - Convolution ํ›„ Upsampling ํ•˜๋Š” ๊ฒƒ๊ณผ Dilated Convolution์„ ํ•˜๋Š” ๊ฒƒ์˜ ์ฐจ์ด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ž์˜ ๊ฒฝ์šฐ Pooling์„ ํ•˜๋ฉด์„œ ๊ณต๊ฐ„์  ์ •๋ณด์˜ ์†์‹ค์ด ๋ฐœ์ƒํ•˜๋Š” ๋ฐ ์ด๋ฅผ ๊ทธ๋Œ€๋กœ upsampling ํ•˜๋ฉด์„œ ํ•ด์ƒ๋„๊ฐ€ ๋–จ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ dilated convolution์„ ํ•˜๊ฒŒ ๋˜๋ฉด, Receptive field๋ฅผ ํฌ๊ฒŒ ๊ฐ€์ ธ๊ฐ€๋ฉด์„œ๋„ Pooling ์—†์–ด ์ •๋ณด์˜ ์†์‹ค์„ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ์–ด ํ•ด์ƒ๋„๊ฐ€ ๋ณด๋‹ค ์ข‹์€ output์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Atrous Spatial Pyramid Pooling (ASPP) ์—ฌ๋‹ด์ด์ง€๋งŒ Atrous Spatial Pyramid Pooling (ASPP)๋Š” ResNet์˜ ์„ค๊ณ„์ž์ธ Kaiming He์˜ SPPNet์—์„œ ์†Œ๊ฐœ๋˜๋Š” Spatial pyramid Pooling ๊ธฐ๋ฒ•์— ์˜๊ฐ์„ ๋ฐ›์•„ ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. DeepLab V2์—์„œ ์ œ์•ˆ๋œ ๋ฐฉ๋ฒ•์œผ๋กœ, feature map์œผ๋กœ๋ถ€ํ„ฐ ํ™•์žฅ ๋น„์œจ (dilation rate, r)๊ฐ€ ๋‹ค๋ฅธ Atrous convolution์„ ๋ณ‘๋ ฌ๋กœ ์ ์šฉํ•œ ๋’ค ๋‹ค์‹œ ํ•ฉ์ณ์ฃผ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ํ™•์žฅ ๋น„์œจ (dilation rate, r)์„ 6 ~ 24๊นŒ์ง€ ๋‹ค์–‘ํ•˜๊ฒŒ ๋ณ€ํ™”ํ•˜๋ฉด์„œ ๋‹ค์–‘ํ•œ receptive field๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์ ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ASPP๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋‹ค์–‘ํ•œ ํฌ๊ธฐ์˜ feature map์„ ๋ฝ‘์•„๋‚ด ์„ž์–ด์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Receptive field๊ฐ€ ์ •ํ•ด์ ธ์žˆ์–ด ์ž‘์€ ๋ฌผ์ฒด๊ฐ€ ๋ฌด์‹œ๋˜๊ฑฐ๋‚˜ ์ด์ƒํ•˜๊ฒŒ ์ธ์‹๋˜๋Š” FCN๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ ์ง๊ด€์ ์œผ๋กœ ๊ดœ์ฐฎ์€ ์ ‘๊ทผ๋ฒ•์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Depthwise separable convolution (Depthwise convolution๊ณผ Depthwise separable convolution์— ๋Œ€ํ•ด์„œ๋Š” ๋”ฐ๋กœ ์ •๋ฆฌํ•˜๊ณ  ์žˆ์œผ๋‹ˆ Down-sampling์„ ์ฝ์–ด๋ณด์„ธ์š”.) Depthwise Separable convolution์€ ๊ธฐ์กด Convolution ํ•„ํ„ฐ๊ฐ€ Spatial dimension๊ณผ Channel dimension์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๋˜ ๊ฒƒ์„ ๋”ฐ๋กœ ๋ถ„๋ฆฌ์‹œ์ผœ ๊ฐ๊ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ์ถ•์„ ๋ถ„๋ฆฌ์‹œ์ผœ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋”๋ผ๋„, ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’์€ ๋‘ ์ถ• ๋ชจ๋‘๋ฅผ ์ฒ˜๋ฆฌํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ๊ธฐ์กด convolution์ด ์ˆ˜ํ–‰ํ•˜๋˜ ์—ญํ• ์„ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ธฐ์กด convolution ์—ฐ์‚ฐ๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜์™€ ์—ฐ์‚ฐ๋Ÿ‰์„ ํš๊ธฐ์ ์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Xception model Xception์€ ๊ตฌ๊ธ€์ด 2017๋…„์— ๋ฐœํ‘œํ•œ ๋ชจ๋ธ๋กœ, 2015๋…„์— ILSVRC ๋Œ€ํšŒ์—์„œ 2๋“ฑ์„ ํ•œ Google์˜ Inception-V3 ๋ชจ๋ธ๋ณด๋‹ค ํ›จ์”ฌ ์ข‹์€ ๊ฒฐ๊ณผ๋ฅผ ๋ƒˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ๋‚ด์šฉ์€ ๋ธ”๋กœ๊ทธ ๊ธ€์„ ๊ฐ€์ ธ์˜จ ๊ฒƒ์œผ๋กœ ์ž์„ธํ•œ ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ๋Š” ํ•ด๋‹น ๊ธ€์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. Xception์˜ ์ค‘์  ํฌ์ธํŠธ: Modified Depthwise Separable Convolution Xception์˜ ๋ชฉ์ : ์—ฐ์‚ฐ๋Ÿ‰๊ณผ parameter์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์—ฌ์„œ, ํฐ ์ด๋ฏธ์ง€ ์ธ์‹์„ ๊ณ ์†ํ™” ์‹œํ‚ค์ž! ์žฅ์ : VGG์ฒ˜๋Ÿผ ๋„คํŠธ์›Œํฌ์˜ ๊ตฌ์กฐ๊ฐ€ ๊ฐ„๋‹จํ•ด์„œ (inception ๊ณผ ๋‹ฌ๋ฆฌ..) ํ™œ์šฉ๋„๊ฐ€ ๋†’๋‹ค! ๋…ผ๋ฌธ์—์„œ ์–˜๊ธฐํ•˜๋Š” Xception์˜ ๋ฐ”ํƒ•์ด ๋œ ๊ฐœ๋…๋“ค VGG16์˜ ๊ตฌ์กฐ: Deep ํ•˜๊ฒŒ ์Œ“์•„๊ฐ€๋Š” ๊ตฌ์กฐ๋ฅผ ๋”ฐ์™”์Œ Inception Family: Conv๋ฅผ ํ•  ๋•Œ ๋ช‡ ๊ฐœ์˜ branch๋กœ factorize ํ•ด์„œ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์˜ ์žฅ์ ์„ ์•Œ๋ ค์คฌ์Œ Depthwise Separable Convolution์„ ์ ์šฉํ•จ Xception์˜ ๊ตฌ์กฐ๋Š” Entry, Middle, Exit์˜ 3๊ฐœ ๊ตฌ์กฐ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. DeepLab V3+ Architecture 2018๋…„ 2์›”์— ๊ตฌ๊ธ€์ด ๊ณต๊ฐœํ•œ DeepLab V3+์˜ ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์•ž์„  ๋ชจ๋ธ๋“ค์˜ ๋ฐฉ๋ฒ•์„ ๋ชจ๋‘ ๊ณ„์Šนํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. DeepLab V3+์˜ ํŠน์„ฑ์„ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 2๊ฐ€์ง€์˜ ์ธ์ฝ”๋”๋ฅผ ์ œ์‹œํ•จ. DeepLab V3๋ฅผ ์ธ์ฝ”๋”๋กœ ๋ณ€ํ˜•๋œ Xception๋ฅผ ์ธ์ฝ”๋”๋กœ Atrous Convolution, ASPP์™€ Depth-wise Separable Convolution ์„ ์ ์šฉ Skip architecture๋กœ Encoder - Decoder๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Œ DeepLab V3๋ฅผ Encoder๋กœ ํ•œ ๋ชจ๋ธ DeepLab V3๋Š” ์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ์ด๊ณ  Atrous Spatial Pyramid Pooling (ASPP)๋ฅผ Encoder์— ์‚ฌ์šฉํ•ด feature๋“ค์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. DeepLab V3 encoder๋ฅผ ํ†ต๊ณผํ•ด์„œ ๋‚˜์˜จ feature map์€ ์›๋ณธ ์‚ฌ์ง„์˜ ํ•ด์ƒ๋„๋ณด๋‹ค 16๋ฐฐ๊ฐ€ ์ž‘์Šต๋‹ˆ๋‹ค (Output Stride: 16). 16์œผ๋กœ ํ•œ ์ด์œ ๋Š” ์‹คํ—˜ ๊ฒฐ๊ณผ ์†๋„์™€ ์ •ํ™•๋„์˜ ์ข‹์€ trade-off point์˜€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. DeepLab V3์˜ decoder์—์„œ๋Š” encoder feature map์„ ๋‹จ์ˆœํžˆ 16๋ฐฐ bilinear up-sample ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌ๋ฉด segmentation ํ•ด์ƒ๋„๊ฐ€ ๋„ˆ๋ฌด ๋–จ์–ด์ง€๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ DeepLab V3+์—์„œ๋Š” ์ด์ „์˜ FCN์ด๋‚˜ UNET์—์„œ ์‚ฌ์šฉํ•˜์˜€๋˜ ๊ฒƒ์ฒ˜๋Ÿผ Skip architecture๋ฅผ ๋„์ž…ํ•˜์—ฌ Encoder์™€ Decoder๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ค๋‹ˆ๋‹ค. DeepLab V3๋ฅผ Encoder๋กœ ํ•œ ๋ชจ๋ธ์˜ ๊ตฌ์กฐ์— ๋Œ€ํ•ด ๋‹ค๋ฅธ ๊ทธ๋ฆผ์œผ๋กœ ํ•œ ๋ฒˆ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Encoder์—์„œ ๋‚˜์˜จ ์ตœ์ข… feature map์— ๋Œ€ํ•ด 4๋ฐฐ bilinear upsample์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. Encoder ์ค‘๊ฐ„์—์„œ ๋‚˜์˜จ feature map (Low-Level Features)์„ 1x1 convolution์„ ์ ์šฉํ•˜์—ฌ channel์„ ์ค„์ž…๋‹ˆ๋‹ค. (2๋ฒˆ ๊ณผ์ •์„ ํ†ตํ•ด 1๋ฒˆ๊ณผ 2๋ฒˆ feature map์ด concatenation์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.) 1๋ฒˆ๊ณผ 2๋ฒˆ feature map์„ concatenate ํ•ฉ๋‹ˆ๋‹ค. 3x3 convolution layers๋ฅผ ๊ฑฐ์นœ ํ›„ ๋งˆ์ง€๋ง‰ 1x1 convolution์„ ๊ฑฐ์ณ output์„ ๋ฝ‘์•„๋ƒ…๋‹ˆ๋‹ค. 4๋ฐฐ bilinear upsample์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์›๋ž˜ input size๋กœ ๋ณต์›๋œ ์ตœ์ข… segmentated data๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋ณ€ํ˜•๋œ Xception์„ Encoder๋กœ ํ•œ ๋ชจ๋ธ Xception ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋ณ€ํ˜•ํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ Xception ๊ณผ ๋น„๊ตํ•ด์„œ (1) ๋” ๊นŠ๊ณ  (2) Max Pooling์„ stride๊ฐ€ ์žˆ๋Š” Depthwise Separable Convolution๋กœ ๋ฐ”๊พธ์—ˆ๊ณ  (3) 3x3 Depthwise Convolution ํ›„์— Batch Normalization ๊ณผ ReLU๋ฅผ ๋”ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. DeepLab V3+์˜ loss function & training ImageNet์˜ ๋ฐ์ดํ„ฐ๋กœ pre-train๋œ ResNet-101 ๋˜๋Š” modified aligned Xception์„ backbone network๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. POLY learning rate policy: ์ดˆ๊ธฐ learning rate=0.007๋กœ ์„ค์ •ํ•˜๊ณ , ์•„๋ž˜์™€ ๊ฐ™์€ ์‹์„ ๋”ฐ๋ผ ์ ์  learning rate์„ ์ค„์—ฌ๋‚˜๊ฐ€๋Š” "poly" learning rate policy๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. Crop size: 513x513 crop image๋ฅผ ์‚ฌ์šฉ Batch normalization: batch size = 16์„ ์‚ฌ์šฉ DeepLab V3+์˜ ์„ฑ๋Šฅ 2018/02 PASCAL VOC 2012 ์™€ Cityscapes dataset์—์„œ State-of-art๋ฅผ ๋‹ฌ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. DeepLab Code ํŒŒ์ด ํ† ์น˜๋ฅผ ์ด์šฉํ•ด DeepLab์„ ํ•˜๋‚˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import numpy as np import torch from torch.nn import Conv2d from PIL import Image import matplotlib.pyplot as plt import requests from scipy.ndimage import zoom as resize ์šฐ์„  ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  imput์œผ๋กœ ๋“ค์–ด๊ฐˆ ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. url_Image = "http://raw.githubusercontent.com/jmlipman/jmlipman.github.io/master/images/kumamon.jpeg" im = Image.open(requests.get(url_Image, stream=True).raw).convert("L") plt.imshow(im, cmap="gray") plt.show() im = np.array(im) ์œ„์˜ ๊ตฌ์กฐ์™€ ๊ฐ™์ด ๋‘ ๊ฐ€์ง€์˜ ์ปจ๋ณผ๋ฃจ์…˜์„ ๊ตฌ์„ฑํ•ด ์ค๋‹ˆ๋‹ค. kernel_size = 7 dilation = 1 stride = 1 padding = 3 #Define a 2D convolutions conv_1 = Conv2d(in_channels=1, out_channels=1, kernel_size=kernel_size, dilation=dilation, padding=padding) kernel_size = 7 dilation = 2 stride = 1 padding = 6 conv_2 = Conv2d(in_channels=1, out_channels=1, kernel_size=kernel_size, dilation=dilation, padding=padding) #Use the same weights and bias conv_2.weight.data = conv_1.weight.data conv_2.bias.data = conv_1.bias.data ์ฒซ ๋ฒˆ์งธ ๊ตฌ์กฐ๋Š” Downsampling์„ ํ•ด์ฃผ๊ณ , Image๋ฅผ Conv_1์— ์ ์šฉ์‹œ์ผœ์ค€ ํ›„ Upsampling์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. # 1) Downsampling 100x100 -> 50x50 im1 = resize(im, (0.5,0.5)).reshape(1,1,50,50) # 2) Apply the Convolution to that Image input_image = torch.Tensor(im1) output_image = conv_1(input_image).detach().numpy()[0,0] # 3) Upsample upsampled = resize(output_image, (2,2)) ๋‘ ๋ฒˆ์งธ ๊ตฌ์กฐ๋Š” ์ด๋ฏธ ๋งŒ๋“ค์–ด๋†“์€ Conv_2์— ์ ์šฉ์‹œ์ผœ์ค๋‹ˆ๋‹ค. im2 = im.reshape(1, 1, 100, 100) imput_image = torch.Tensor(im2) output_image = conv_2(input_image).detach().numpy()[0,0] ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. plt.figure(figsize=(10,5)) plt.subplot(121) plt.imshow(output_image) plt.subplot(122) plt.imshow(upsampled) plt.show() Reference DeepLab V3+ ์›๋…ผ๋ฌธ:Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation Youtube DeepLab: Semantic Image Segmentation ๋ธ”๋กœ๊ทธ Enough is not enough | ๋”ฅ๋Ÿฌ๋‹์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋‹ค์–‘ํ•œ Convolution ๊ธฐ๋ฒ•๋“ค Different types of Convolutions Deep Lab ASPP(Atrous Spatial Pyramid Pooling) DeepLab V3+ ์ด์ œ๋Š” ๊ธฐ๋ณธ ๋ชจ๋ธ์ด ๋œ 'Xception' ์ดํ•ดํ•˜๊ธฐ (4) ์ด๋ฏธ์ง€ ๋ถ„ํ•  ๋ชจ๋ธ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•(โ˜…์ž‘์„ฑ ์ค‘) ... 4. Object detection(๊ฐ์ฒด ๊ฒ€์ถœ) ์ปดํ“จํ„ฐ ๋น„์ „์—์„œ ๊ฐ€์žฅ ์‹ ๊ธฐํ•˜๊ณ  ์žฌ๋ฏธ์žˆ๊ณ  ์‹ค์šฉ์ ์ธ Task๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€์—์„œ ๊ฐ ์ธ์Šคํ„ด์Šค๋“ค์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๋ฐ•์Šค ํ…Œ๋‘๋ฆฌ๋กœ ์˜ˆ์ธกํ•˜๊ณ  ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์˜ ํด๋ž˜์Šค๋ฅผ ์˜ˆ์ธกํ•˜๋Š” Task์ž…๋‹ˆ๋‹ค. (1) ๊ฐ์ฒด ๊ฒ€์ถœ ์•„์ด๋””์–ด ๊ฐ์ฒด ๊ฒ€์ถœ(object detection)์€ ํ•œ ์ด๋ฏธ์ง€์—์„œ ๊ฐ์ฒด์™€ ์ด ๊ฐ์ฒด๋ฅผ ๋‘˜๋Ÿฌ์‹ธ๋Š” ๊ฐ€์žฅ ์ž‘์€ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ์ •์˜๋˜๋Š” bounding box๋ฅผ ์ฐพ๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. Image classification๊ณผ ๋น„๊ตํ•ด ๋ณด์ž๋ฉด, Image classification๋Š” ๊ฐ์ฒด๋งˆ๋‹ค label์„ ๋ถ™์ด๋Š”๋ฐ ์ดˆ์ ์„ ๋งž์ถ”์ง€๋งŒ, object detection์—์„œ๋Š” label์„ ๋ถ™์ด๋Š” ๊ฒƒ๋ฟ ์•„๋‹ˆ๋ผ, ์ด๋ฏธ์ง€ ์ƒ์—์„œ์˜ ์ขŒํ‘œ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋น„์Šทํ•œ ๊ฐœ๋…์ธ Image localization๊ณผ Image classification, object detection์„ ๋น„๊ตํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. Image classification : ํ•˜๋‚˜์˜ object์— ๋Œ€ํ•ด ๋ฌด์—‡์ธ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•จ. Image localization : ํ•˜๋‚˜์˜ object์— ๋Œ€ํ•ด ์–ด๋””์— ์œ„์น˜ํ•˜๋Š”์ง€ ํŒŒ์•…ํ•จ. Object detection : ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌผ์ฒด์— ๋Œ€ํ•ด์„œ ์œ„์น˜๋ฅผ ํŒŒ์•…ํ•˜๊ณ , ๊ฐ๊ฐ์ด ๋ฌด์—‡์— ํ•ด๋‹นํ•˜๋Š”์ง€๋ฅผ ํŒŒ์•…ํ•จ. ๊ฐ์ฒด ๊ฒ€์ถœ์˜ ์—ญ์‚ฌ (์˜ฎ๊ฒจ๊ฐˆ ์˜ˆ์ •) ๊ฐ์ฒด ๊ฒ€์ถœ์€ Image descriptor๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด '์ž์ „๊ฑฐ'๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ํƒ์ง€ํ•˜๋ ค๋ฉด, ์ž์ „๊ฑฐ๊ฐ€ ํฌํ•จ๋œ ๋ช‡ ์žฅ์˜ ์‚ฌ์ง„์œผ๋กœ๋ถ€ํ„ฐ ์ž์ „๊ฑฐ์˜ ํŠน์ • ๋ถ€๋ถ„์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” descriptor๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ด ๊ฐ์ฒด๋ฅผ ์ฐพ์„ ๋•Œ ๋ชฉํ‘œํ•˜๋Š” ์ด๋ฏธ์ง€์—์„œ ์ด descriptor๋ฅผ ๋‹ค์‹œ ์ฐพ์œผ๋ ค๊ณ  ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์—์„œ ํŠน์ • ๋ฌผ์ฒด๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ฒ•์€ floating window ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๋Š”, ์ด๋ฏธ์ง€๋ฅผ ์ž‘์€ ์ง์‚ฌ๊ฐํ˜• ํ˜•ํƒœ๋กœ ๋Œ๋ฉด์„œ ๊ฒ€์‚ฌํ•ด ๊ฐ€์žฅ ์ผ์น˜ํ•˜๋Š” descriptor๋ฅผ ๊ฐ€์ง„ ๋ถ€๋ถ„์ด ์ด ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๋Š” ๋ถ€๋ถ„์ด๋ผ๊ณ  ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์ด๋ฏธ์ง€๋ฅผ ํšŒ์ „์‹œํ‚ค๊ฑฐ๋‚˜ ์ƒ‰์ด ๋ฐ”๋€Œ๋”๋ผ๋„ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ณ , ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์ด ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฉฐ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ์ฒด์— ์ž‘๋™ํ•œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ ์ •ํ™•๋„๊ฐ€ ๋†’์ง€ ๋ชปํ•˜๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 1990๋…„๋Œ€ ์ดˆ๋ฐ˜๋ถ€ํ„ฐ ์‹ ๊ฒฝ๋ง์„ ์‚ฌ์šฉํ–ˆ์œผ๋‚˜ ์ •ํ™•๋„ ์ธก๋ฉด์—์„œ ํฐ ์„ฑ๊ณต์„ ๋ณด์ง€๋Š” ๋ชปํ–ˆ๊ณ , 2010๋…„์— ๋“ค์–ด์„œ์•ผ ImageNet์—์„œ image descriptor ๊ธฐ๋ฒ•์„ ์‹ ๊ฒฝ๋ง์ด ๋›ฐ์–ด๋„˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. 1) ๊ฐ์ฒด ๊ฒ€์ถœ ๋ฐ์ดํ„ฐ ์…‹ ์†Œ๊ฐœ Image classification์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, Object detection์— ์“ฐ์ด๋Š” ๋ฐ์ดํ„ฐ ์…‹์€ ์ •๋ง ๋‹ค์–‘ํ•˜๊ฒŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ตœ๊ทผ์—๋Š” Google, Facebook, Microsoft์™€ ๊ฐ™์€ ๊ธฐ์—…๋“ค์ด ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ ์…‹๋“ค์„ ๊ณต๊ฐœํ•˜๊ณ  ์žˆ๊ณ , ๊ณต๊ฐœ๋˜๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์…‹์˜ ๋‚œ์ด๋„๊ฐ€ ์˜ฌ๋ผ๊ฐ€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘ ์ค‘์š”ํ•˜๊ณ  ์ž์ฃผ ์“ฐ์ด๋Š” ๊ฒƒ๋“ค์— ๋Œ€ํ•ด ๋‹ค๋ค„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. PASCAL VOC PASCAL VOC๋Š” 2005๋…„์—์„œ 2012๋…„๊นŒ์ง€ ์ง„ํ–‰๋˜์—ˆ๋˜ PASCAL VOC challenge์—์„œ ์“ฐ์ด๋˜ ๋ฐ์ดํ„ฐ ์…‹์ž…๋‹ˆ๋‹ค. ๊ทธ์ค‘ PASCAL 2007๊ณผ PASCAL 2012 ๋ฐ์ดํ„ฐ ์…‹์ด ๋ฒค์น˜๋งˆํฌ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ์ž์ฃผ ์“ฐ์ž…๋‹ˆ๋‹ค. VOC ๋ฐ์ดํ„ฐ ์…‹ ๊ตฌ์กฐ XML format Annotation: Class Bounding box (x, y, w, h) Pose: ๊ฐ๊ฐ์˜ ์˜ค๋ธŒ์ ํŠธ์˜ ๋ฐฉํ–ฅ์„ฑ ์ •๋ณด Truncated: ๊ฐ์ฒด๊ฐ€ ํ•ด๋‹น ์ด๋ฏธ์ง€์— ์˜จ์ „ํžˆ ํ‘œํ˜„๋˜์ง€ ๋ชปํ•˜๊ณ  ์ž˜๋ ค๋‚˜๊ฐ”๋Š”์ง€ Difficult: ์ธ์‹ํ•˜๊ธฐ ์–ด๋ ค์šด์ง€ Image sets JPEG Images Segmentation class: sementic segmentation์„ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•œ labeled images Segmentation object: instance segmentation์„ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•œ labeled images VOC ๋ฐ์ดํ„ฐ ์…‹ ํฌ๊ธฐ ์ด๋ฏธ์ง€ ๊ฐœ์ˆ˜: object detection: ์ด 9,963๊ฐœ์˜ ์ฃผ์„์ด ๋‹ฌ๋ฆฐ(annotated) ์ด๋ฏธ์ง€๊ฐ€ ํฌํ•จ. ์ด ์ค‘์—์„œ 5,011๊ฐœ๊ฐ€ ํ•™์Šต ๋ฐ์ดํ„ฐ segmentation: 422๊ฐœ์˜ ํ•™์Šต ๋ฐ์ดํ„ฐ ์ด๋ฏธ์ง€๋‹น ํ‰๊ท  object ์ˆ˜: 2.4๊ฐœ ์ด๋ฏธ์ง€๋‹น ํ‰๊ท  class ์ˆ˜: 1.4๊ฐœ class ๊ฐœ์ˆ˜ 20๊ฐœ ImageNet ImageNet Large Scale Visual Recognition Challenge (ILSVRC) ๋Œ€ํšŒ์—์„œ Image classification, Object detection ์„ฑ๋Šฅ ํ‰๊ฐ€ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘ ILSVRC 2012 ๋ฐ์ดํ„ฐ ์…‹์ด ์ž์ฃผ ์“ฐ์ž…๋‹ˆ๋‹ค. ImageNet ๋ฐ์ดํ„ฐ ์…‹ ํฌ๊ธฐ ์ด๋ฏธ์ง€ ๊ฐœ์ˆ˜: 1000k class ๊ฐœ์ˆ˜ 1000๊ฐœ ImageNet ๋ฐ์ดํ„ฐ ์…‹์˜ ๋‹จ์  ์ด๋ฏธ์ง€ ๋‚ด object๊ฐ€ ํฐ ํŽธ์ž„ object๊ฐ€ ์ค‘์•™์— ์œ„์น˜ ์ด๋ฏธ์ง€๋‹น object ์ˆ˜๊ฐ€ ์ ์Œ ์œ„์˜ ๋ฌธ์ œ์  ๋•Œ๋ฌธ์— PASCAL VOC๋‚˜ ImageNet์ด ํ˜„์‹ค์„ธ๊ณ„ ์‚ฌ์ง„์—์„œ๋Š” object๋ฅผ ์ž˜ ํฌ์ฐฉํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋ฌธ์ œ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด 2014๋…„ COCO ๋ฐ์ดํ„ฐ ์…‹์ด ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. COCO COCO ๋ฐ์ดํ„ฐ ์…‹ ๊ตฌ์กฐ JSON format Annotation: Class Bounding box (x, y, w, h) Segmentation JPEG Images COCO ๋ฐ์ดํ„ฐ ์…‹์˜ ์žฅ์  ๋‹ค์–‘ํ•œ ํฌ๊ธฐ์˜ ๋ฌผ์ฒด๊ฐ€ ์กด์žฌ ๋†’์€ ๋น„์œจ๋กœ ์ž‘์€ ๋ฌผ์ฒด๋“ค์ด ์กด์žฌ Object๋“ค์ด ํ˜ผ์žกํ•˜๊ฒŒ ์กด์žฌํ•˜๊ณ , occlusion(ํ์ƒ‰)์ด ๋งŽ์ด ์กด์žฌ ๋œ iconic ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ iconic์˜ ์˜๋ฏธ๋Š” ์ด๋ฏธ์ง€๊ฐ€ ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ์— ๋ช…ํ™•ํ•˜๊ฒŒ ์†ํ•ด์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜ ์ด๋ฏธ์ง€์˜ (a)์˜ ์ด๋ฏธ์ง€๋“ค์€ ๋ˆ„๊ฐ€ ๋ด๋„ ๋ช…ํ™•ํ•˜๊ฒŒ ๊ฐœ, ์†Œ ๋“ฑ์˜ ๋ช…ํ™•ํ•˜๊ฒŒ ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ•œ ๊ฑธ ๋ˆ„๊ตฌ๋‚˜ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (b)์˜ ์ด๋ฏธ์ง€๋“ค์€ (a)๋ณด๋‹ค๋Š” ๋ชจํ˜ธํ•˜์ง€๋งŒ, ํŠน์ • ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ•˜๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ (c)์˜ ๊ฒฝ์šฐ ์–ด๋–ค ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ•˜๋Š”์ง€ ๋ถ„๋ฅ˜ํ•˜๊ธฐ๊ฐ€ ๋ชจํ˜ธํ•ฉ๋‹ˆ๋‹ค. ํ˜„์‹ค ์„ธ๊ณ„ ์‚ฌ์ง„์—์„œ๋Š” (a)๋‚˜ (b)๋ณด๋‹จ (c)์— ์†ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ํ›จ์”ฌ ๋งŽ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. COCO ๋ฐ์ดํ„ฐ ์…‹ ํฌ๊ธฐ ์ด๋ฏธ์ง€ ๊ฐœ์ˆ˜: ํ•™์Šต(training) ๋ฐ์ดํ„ฐ ์…‹: 118,000์žฅ์˜ ์ด๋ฏธ์ง€ ๊ฒ€์ฆ(validation) ๋ฐ์ดํ„ฐ ์…‹: 5,000์žฅ์˜ ์ด๋ฏธ์ง€ ํ…Œ์ŠคํŠธ(test) ๋ฐ์ดํ„ฐ ์…‹: 41,000์žฅ์˜ ์ด๋ฏธ์ง€ ์ด๋ฏธ์ง€๋‹น ํ‰๊ท  object ์ˆ˜: 8๊ฐœ ์ด๋ฏธ์ง€๋‹น ํ‰๊ท  class ์ˆ˜: 3.5๊ฐœ class ๊ฐœ์ˆ˜ 91๊ฐœ ์ˆ˜ํ–‰ํ•˜๋Š” task classification object detection semantic segmentation instance segmentation pose estimation etc Reference Object Detection ์ฃผ์š” ๋ฐ์ดํ„ฐ ์„ธํŠธ ์†Œ๊ฐœ Object Detection Dataset ๋ฆฌ๋ทฐ PASCAL VOC MS COCO (2) ๊ฐ์ฒด ๊ฒ€์ถœ ๋ชจ๋ธ ์ดํ•ด๋ฅผ ์œ„ํ•œ ์‚ฌ์ „ ์ง€์‹ ... 1) General process of object detection ์„œ๋ก  Object detection์€ ์ดˆ์ฐฝ๊ธฐ ์ปดํ“จํ„ฐ ๋น„์ „ ๋ชจ๋ธ์—์„œ๋ถ€ํ„ฐ ์ตœ๊ทผ์˜ learning-based object detector๊นŒ์ง€ ์˜ค๋žœ ์„ธ์›” ์—ฐ๊ตฌ๊ฐ€ ๋˜์–ด์˜จ ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ process๋Š” ํฌ๊ฒŒ ๋ณ€ํ•˜์ง€ ์•Š๊ณ  learning-based๋“ , ๊ณ ์ „์ ์ธ ๋ฐฉ๋ฒ•์ด๋“  ์–ด๋Š ์ •๋„ ์ •ํ•ด์ง„ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ์š”. ์ด ๋ฌธ์„œ์—์„œ๋Š” object detection์˜ general flow์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1. Specify Object Model ๋ณดํ†ต์€ Object detection์ด ๋ชฉ์ ์œผ๋กœ ํ•˜๋Š” ๊ฑด ์ด ์„ธ์ƒ์˜ ๋ชจ๋“  ๋ฌผ์ฒด๋ฅผ ๋‹ค detection ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ •ํ•ด์ฃผ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฌผ์ฒด๋“ค์— ํ•œ์ •๋˜๋Š” ๊ฒฝ์šฐ๋“ค์ด ๋งŽ์ฃ . Object detection์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„์ธ Object model์„ ๊ตฌ์ฒดํ™”ํ•˜๋Š” ๊ฒƒ์€ ์šฐ๋ฆฌ๊ฐ€ ์–ด๋–ค object๋ฅผ detection ํ• ์ง€ ์ •ํ•˜๊ณ  feature๋ฅผ define ํ•˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ์œ ํ–‰ ์ค‘์ธ NN์„ ๋ฒ ์ด์Šค๋กœ ํ•œ ๋ชจ๋ธ๋“ค์€ ์‚ฌ๋žŒ์ด ๋ฝ‘์•„๋‚ผ feature๋ฅผ ์ •ํ•ด์ฃผ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด ๊ณผ์ •์ด ์ƒ๋žต๋ฉ๋‹ˆ๋‹ค. (๊ทธ์ € ์–ด๋–ค object๋ฅผ detection ํ• ์ง€๋งŒ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.) ํ•˜์ง€๋งŒ ์ตœ๊ทผ์—๋Š” ์ด๋Ÿฌํ•œ ๊ณ ์ „์ ์ธ ๋ฐฉ๋ฒ•๋“ค์„ domain knowledge๋กœ ์ด์šฉํ•ด learning-based detection์—์„œ๋„ ์ด์šฉํ•˜๋ ค๋Š” ์›€์ง์ž„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์„œ์—์„œ๋Š” ์ด๋Ÿฐ ๊ณ ์ „์ ์ธ ๋ฐฉ๋ฒ•๋“ค์„ ๋งค์šฐ ๊ฐ€๋ณ๊ฒŒ ํ•œ๋ฒˆ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์—ฌ๊ธฐ์„œ ์ œ์‹œ๋œ ๋ฐฉ๋ฒ•๋“ค ๋ง๊ณ ๋„ ์ •๋ง ๋งŽ์€ ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์„œ์—์„œ๋Š” ์œ ๋ช…ํ•œ ๊ณ ์ „์ ์ธ ๋ฐฉ๋ฒ• ๋ช‡ ๊ฐ€์ง€์—๋งŒ ํฌ์ปค์Šค๋ฅผ ๋งž์ถฐ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ข…๋ฅ˜ 1. Statistical Template in Bounding Box ์ด ๋ฐฉ๋ฒ•์—์„œ๋Š” object๋Š” ์ด๋ฏธ์ง€ ์–ด๋”˜๊ฐ€์— ์กด์žฌํ•˜๊ณ , Feature๋Š” bounding box์˜ coordinate์— ๋Œ€ํ•ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์—์„œ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ข‹์€ feature๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”? ๋ชจ๋ฆ„์ง€๊ธฐ ์ข‹์€ feature๋ผ๋ฉด, 1) ๊ฐ™์€ object๋ผ๋ฉด ๋น„์Šทํ•œ feature๊ฐ€ ๋ฝ‘ํ˜€์•ผ ํ•  ๊ฒƒ์ด๊ณ  2) ๊ฐ™์€ object๋ผ๋ฉด ์ฃผ๋ณ€์˜ ํ™˜๊ฒฝ์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๊ณ  ๊พธ์ค€ํžˆ ๋น„์Šทํ•˜๊ฒŒ ๋ฝ‘ํ˜€์•ผ ํ•˜๊ณ  3) flat์ด๋‚˜ edge ๊ฐ™์€ ์˜์—ญ์ด ์•„๋‹Œ corner ๊ฐ™์€ ์ฃผ๋ณ€๊ณผ ์ฐจ๋ณ„ํ™”๋˜๋Š” ๋ถ€๋ถ„์„ ๋ฝ‘์•„์•ผ ํ•˜๊ณ  4) feature์— ์ƒ์‘ํ•˜๋Š” descriptor๋ฅผ ๋‹ฌ ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ด๋ฏธ์ง€์—์„œ๋Š” edge์—์„œ ๋ฐ๊ธฐ ๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์— ๋”ฐ๋ผ ๊ฐ•๋ ฅํ•œ edge์˜ ๋ฐฉํ–ฅ๋“ค๋งŒ ๋ฝ‘์•„์„œ feature๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ feature ์Šค์Šค๋กœ๋ฅผ descriptor๋กœ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌ๋ฉด ๊ฐ™์€ ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์คฌ์„ ๋•Œ, (illumination ๊ฐ™์€ ๊ฐ•๋ ฅํ•œ ๋ฐฉํ•ด๊ฐ€ ์žˆ์ง€ ์•Š๋‹ค๋ฉด) ํ™˜๊ฒฝ์ด ๋‹ฌ๋ผ์ ธ๋„ ๋น„์Šทํ•œ feature๋ฅผ ๋ฝ‘์•„๋‚ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ SIFT์™€ ๋น„์Šทํ•œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ์— ๊ฐ•๋ ฅํ•œ feature๋ผ๋Š” ๊ฒƒ๋„ ์•Œ ์ˆ˜ ์žˆ๊ณ , ์Šค์Šค๋กœ๊ฐ€ descriptior์ด๊ธฐ์— descriptor๋„ ๋ฐ”๋กœ ์ •์˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 2. Articulated parts ์‚ฌ๋žŒ์˜ ์–ผ๊ตด์ด๋‚˜ ๊ธฐ๊ณ„ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์ •ํ˜•ํ™”๋œ ๊ตฌ์กฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ Object๋Š” ๋ถ€๋ถ„ ์š”์†Œ์š”์†Œ๋“ค์˜ configuration์ด ๋˜๊ฒ ์ง€์š”. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ์ด ๊ตฌ์กฐ๋ฅผ feature๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ตฌ์กฐ๋Š” ํ˜„์žฌ๋„ feature๋กœ ์ด์šฉ๋˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, detection ๊ทธ ์ž์ฒด๋ณด๋‹ค๋Š” ์‚ฌ์‹ค ์—ญ์œผ๋กœ pose estimation์„ ํ•  ๋•Œ๋‚˜, graphics ์ชฝ์—์„œ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์œ„ํ•ด ์•„์ง ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. 3. Hybrid Template & body ์ด ๊ตฌ์กฐ๋Š” 2๊ฐ€์ง€ ๋ชจ๋ธ์„ ํ•ฉ์ณ์„œ ์‚ฌ์šฉํ•˜๋Š”๋ฐ์š”, ์•ž์„œ ์„ค๋ช…ํ–ˆ๋˜ ๋ฐฉ๋ฒ•๊ณผ deformation model์ด๋ผ๋Š” ๋‹ค๋ฅธ ๋ชจ๋ธ์„ ํ•ฉ์ณ์„œ ์ž์ „๊ฑฐ๋ฅผ ํƒ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. 2. Generate Hypothesis ์„œ๋ก  1๋ฒˆ ๋‹จ๊ณ„์—์„œ ์šฐ๋ฆฌ๋Š” ์–ด๋–ค Object๋ฅผ ๋ฝ‘์„์ง€, ๊ทธ๋ฆฌ๊ณ  ๋ฝ‘์„ ojbect์— ๋Œ€ํ•œ dbํ™”๋ฅผ ๋๋‚ธ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. 2๋ฒˆ ๋‹จ๊ณ„์—์„œ๋Š”, ์šฐ๋ฆฌ๋Š” Ojbect์˜ ํ›„๋ณด๊ตฐ์„ ๋ฝ‘๊ณ  Scoring ํ•ด๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Sliding Window Sliding window๋Š” Scale์„ ๊ณ ๋ คํ•˜๋ฉด์„œ ๋ชจ๋“  pixel location์— ๋Œ€ํ•ด์„œ ์ผ์ • ํฌ๊ธฐ์˜ patch ๋งŒํผ ๋Œ์•„๋‹ค๋‹ˆ๋ฉด์„œ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  pixel location์„ ๋„๋Š” ๊ฒƒ์€ ์–ด๋ ต์ง€ ์•Š์•„ ๋ณด์ด์ง€๋งŒ, scale์„ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์€ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ์ง€ ๊ฐ์ด ์ž˜ ์žกํžˆ์ง€ ์•Š์ง€์š”? ์ด๋ฏธ์ง€์˜ ์ „์ฒด ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๊ณ  ์ค„์ด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ ์ž‘์€ ๋ฌผ์ฒด๋“  ํฐ ๋ฌผ์ฒด๋“  detection ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์„ ํ†ตํ•ด ๋‚ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” feature์™€ ๋น„์Šทํ•œ ์‚ฌ์ด์ฆˆ๋กœ candidate๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์ด ์žˆ๋Š”๋ฐ์š”, ์•ž์„œ ์ œ๊ฐ€ sliding window์—์„œ๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์ด๋ฉด์„œ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅด๋”๋ผ๋„ object๋ฅผ detection ํ•œ๋‹ค๊ณ  ๋ง์”€๋“œ๋ ธ์Šต๋‹ˆ๋‹ค๋งŒ, ์ด๋ฏธ์ง€๋ฅผ ๋Š˜๋ฆฌ๊ฒŒ ๋˜๋ฉด overhead๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ฃ . ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋ฆผ ์‚ฌ์ด์ฆˆ๋ฅผ ๋Š˜๋ฆฌ๊ธฐ๋ณด๋‹ค๋Š”, patch์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ค„์ด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์š”์ฆ˜์—๋Š” objectness๋ฅผ ์ถ”์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ์–ด์„œ ์ด ์˜์—ญ๋งŒ ๊ฐ€์ง€๊ณ  candidate๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์„ค๋ช…์€ ๋ฌธ์„œ๋ฅผ ํ†ตํ•ด ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 3. Score Hypothesis Score hypothesis๋Š” ์•Œ๋ ค์ง„ Classifier๋ฅผ ์ด์šฉํ•ด ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. learning-based object detector๋ผ๋ฉด ์•ž์„œ (2)์—์„œ ๋ฐฐ์šด Classifier ๋ชจ๋ธ๋“ค์„ ๊ฐ ํŒจ์น˜๋งˆ๋‹ค ์ด์šฉํ•˜์—ฌ object ๋ณ„ ์ ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ณ , ๊ณ ์ „์ ์ธ ๋ชจ๋ธ๋“ค์€ SVM์„ ๋ณดํ†ต ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. 4. Resolve Score Non-max Suppression Sliding window๋ฅผ ํ†ตํ•ด ๊ฐ patch ๋ณ„๋กœ scoring์„ ํ•˜๊ฒŒ ๋˜๋ฉด, ์‚ฌ์‹ค YOLO ๋“ฑ ์ตœ๊ทผ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ํ•ด๋„ ๋ชจ๋‘ ๊ฐ–๋Š” ํ•œ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์˜ ์ฃผ๋ณ€์— ์žˆ๋Š” patch๋“ค์ด ์ „๋ถ€ ๋†’์€ score๋ฅผ ๊ฐ–๊ฒŒ ๋œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. object ์ฃผ์œ„๋กœ ์ข‹์€ score๋ฅผ ๊ฐ–๋Š” bounding box๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ๊ธฐ๊ฒ ์ฃ . ๊ทธ์ค‘์—์„œ ์ œ์ผ ์ข‹์€ ๊ฑฐ ํ•˜๋‚˜๋ฅผ ๋ฝ‘๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ด NMS์ž…๋‹ˆ๋‹ค. ๋ฐฉ๋ฒ•์€ ์‹ฌํ”Œํ•ฉ๋‹ˆ๋‹ค. ์ด object ์ฃผ๋ณ€์— ์กด์žฌํ•˜๋Š” bounding box ๋“ค์„ score ๊ธฐ์ค€์œผ๋กœ sorting ํ•ด์„œ ์ œ์ผ ์ข‹์€ ๊ฑฐ ํ•˜๋‚˜๋งŒ ๋‚จ๊ธฐ๊ณ  ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹น์—ฐํžˆ optimal ํ•œ solution์ด๋ผ๊ณ  ๋ณด๊ธฐ๋Š” ์–ด๋ ต๊ณ , Local maximum์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๊ณผ์ •์€ ์ถ”ํ›„ ๋ฌธ์„œ์—์„œ ์„œ์ˆ ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ๋ชจ๋ธ์—์„œ์˜ ์ดํ•ด ์ด ๋ชจ๋ธ์€ 2005๋…„์— CVPR์—์„œ ์ œ์‹œ๋œ ๋ณดํ–‰์ž๋ฅผ detection ํ•˜๋Š” Dalal-Triggs detector๋ผ๊ณ  ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. general flow๊ฐ€ ์–ด๋–ป๊ฒŒ ์ ์šฉ๋˜์—ˆ๋Š”์ง€๋งŒ ๊ฐ€๋ณ๊ฒŒ ๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค. (์ด๋ฏธ ์ •๋‹ต์œผ๋กœ ์‚ฌ์šฉํ•  ์‚ฌ๋žŒ์— ๋Œ€ํ•œ HOG feature๋Š” ์กด์žฌํ•˜๋Š” ์ƒํƒœ) 64x128์˜ window ์‚ฌ์ด์ฆˆ๋กœ sliding window๋ฅผ ํ†ตํ•ด candidate๋ฅผ ๋ฝ‘๊ณ  ์ด์— ๋”ฐ๋ฅธ HOG๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. -> step 2 HOG(histogram of gradient)๋Š” ์ผ์ข…์˜ feature์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ (a) ๋ฒˆ ๊ทธ๋ฆผ์ด silehouse contour๋ฅผ ์ž…ํžŒ gradient map์ž…๋‹ˆ๋‹ค. ์ด ์ž์ฒด๋ฅผ feature๋กœ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์ „์ œ gradient map์„ ์ด์šฉํ•ด Histogram์— ์ง‘์–ด๋„ฃ๊ณ  descriptor๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค. (SIFT์™€ ๋น„์Šทํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.) (e)๊ฐ€ descriptor๋ฅผ ๋ถ™์ธ ๋ชจ์Šต์ด๊ณ ์š”. SVM classifier๋ฅผ ํ†ตํ•ด scoring ํ•ฉ๋‹ˆ๋‹ค. -> step 3 NMS๋ฅผ ํ†ตํ•ด overlapping detection์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. -> step4 Reference Object Detection with Discriminatively Trained Part Based Models (Felzenszwalb et al. 2008) : http://cs.brown.edu/people/pfelzens/papers/lsvm-pami.pdf Histograms of Oriented Gradients for Human Detection (Dalal et al. 2005) : https://lear.inrialpes.fr/people/triggs/pubs/Dalal-cvpr05.pdf 2) Localization Sliding window object detection Sliding window๋Š” (1)์—์„œ ๊ฐ„๋žตํ•˜๊ฒŒ ์†Œ๊ฐœ๊ฐ€ ๋˜์—ˆ์—ˆ๋Š”๋ฐ์š”, ๋ชจ๋“  location์— ๋Œ€ํ•ด scale์„ ๊ณ ๋ คํ•˜์—ฌ object candidate๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ๋Š” ์ž„์˜์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ patch๊ฐ€ pixel-wise๋กœ ์ด๋ฏธ์ง€ ์ „์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ candidate๋ฅผ ๋งŒ๋“ค์–ด๋ƒ„๊ณผ ๋™์‹œ์—, ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์ด object์˜ ํฌ๊ธฐ์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๊ณ  ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” object๋ฅผ ์ฐพ๋„๋ก ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋งํ–ˆ๋“ฏ object์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ž‘์„ ๋•Œ๋Š” ์ด๋ฏธ์ง€์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๋Š˜๋ฆฌ๋Š” ๊ฑด candidate๊ฐ€ ๊ธฐํ•˜๊ธ‰์ˆ˜์ ์œผ๋กœ ๋Š˜์–ด๋‚˜๊ฒŒ ๋˜๋ฉด์„œ overhead๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— wiindow size๋ฅผ ์ค„์—ฌ์„œ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. Sliding window์˜ ๊ตฌ์ฒด์ ์ธ ์ž‘๋™๋ฒ• ์˜ˆ๋ฅผ ๋“ค์–ด ์šฐ๋ฆฌ๊ฐ€ ์ฐจ๋ฅผ detection ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ธธ ์›ํ•œ๋‹ค๋ฉด, ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” patch size ์•ˆ์—์„œ ์ฐจ๋ฅผ classification ํ•˜๋Š” CNN ๋ชจ๋ธ์„ ํ•œ๋ฒˆ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. sliding winodw๊ฐ€ ๋งŒ๋“ค์–ด๋‚ด๋Š” candidate๋“ค์€ patch ๊ฐ๊ฐ์ด ์ฐจ์ธ์ง€, ์•„๋‹Œ์ง€ ๋ถ„๋ฅ˜ํ•˜๊ณ  scoring ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Sliding window์˜ ๋ฌธ์ œ์  Sliding window์˜ ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ์ ์€ ์–ด๋งˆ ๋ฌด์‹œํ•œ ๊ณ„์‚ฐ๋Ÿ‰์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ์ปดํ“จํ„ฐ ๋น„์ „ ๋ถ„์•ผ์—์„œ๋Š” Linear classifier๋ฅผ ์ด์šฉํ–ˆ๊ธฐ์— ๊ทธ๋‚˜๋งˆ ํ•ฉ๋ฆฌ์ ์ธ ์‹œ๊ฐ„ ์•ˆ์— ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ–ˆ์ง€๋งŒ ์œ„์—์„œ ๋งํ–ˆ๋“ฏ CNN ๊ฐ™์€ DL ๊ธฐ๋ฐ˜์˜ classifier๋ฅผ ๊ฐ™์ด ํ™œ์šฉํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๋งค์šฐ ๋งŽ์€ ๊ณ„์‚ฐ ๋น„์šฉ์ด ๋“ญ๋‹ˆ๋‹ค. pixel ๋‹จ์œ„๋กœ ์„ฑ๋Šฅ์ด ์™”๋‹ค ๊ฐ”๋‹ค ํ•˜๋Š” object detection์˜ ํŠน์„ฑ์ƒ stride๋ฅผ ๋„“ํžˆ๋Š” ๊ฒƒ์€ ์ข‹์€ ์„ ํƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ pixel-wise๋กœ ์ „์ฒด location์„ ๋‹ค ๋ˆ๋‹ค๋ฉด ๊ณ„์‚ฐ ๋น„์šฉ์ด ๋„ˆ๋ฌด ๋งŽ์ด ๋“ค๊ฒ ์ฃ . Convolutional implementation sliding windows ๊ทธ๋ž˜์„œ, ์ด ๊ณ„์‚ฐ ๋น„์šฉ์ด๋ผ๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ๋žŒ๋“ค์€ "์™œ ์ด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€"์— ๋Œ€ํ•ด ์ƒ๊ฐ์„ ํ•ด๋ณด๋Š”๋ฐ์š”. ์ด๊ฑด Pixel-wise๋กœ Patch๋ฅผ ์ž๋ฅด๋Š” ๊ณผ์ • ์ž์ฒด๊ฐ€ ๋ฌธ์ œ ์•„๋‹ˆ๋ƒ!!!! ์ „์ฒด ์ด๋ฏธ์ง€๋ฅผ ๋ฐ€์–ด ๋„ฃ์„ ์ˆ˜๋Š” ์—†์„๊นŒ? ํ•˜๋Š” ๊ณ ๋ฏผ์œผ๋กœ ์ด์–ด์ง‘๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ CNN Classifier๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ •ํ•ด์ค€ ์ผ์ •ํ•œ patch size ๋งŒํผ์˜ ์ด๋ฏธ์ง€๋งŒ ๋ฐ›๊ฒŒ ๋˜๋Š”๋ฐ์š”. ์ด๋Š” CNN ์•ˆ์— ํฌํ•จ๋œ FC(Fully Connected) layer ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. CNN์€ ์•ž๋‹จ์€ Convolution์œผ๋กœ ์ด๋ฃจ์–ด์ง„ Conv net, ๋’ท๋‹จ์€ FC layer๋กœ ์ด๋ฃจ์–ด์ง„ Dense net์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋Š” Dense net์ด์ฃ . ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐœ์ˆ˜๊ฐ€ fix ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ „์ฒด ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ž…๋ ฅ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ œํ•œ์ด ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Convolution์œผ๋กœ๋งŒ ๊ตฌ์„ฑ์ด ๋˜๋ฉด ๋ชจ๋“  ์‚ฌ์ด์ฆˆ์˜ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์šฐ๋ฆฌ๋Š” ์•Œ๊ณ  ์žˆ์ฃ . ๊ทธ๋Ÿฌ๋ฉด Dense net์„ Convolution ์—ฐ์‚ฐ์œผ๋กœ ๋ชจ๋‘ ๋Œ€์ฒดํ•  ์ˆ˜๋Š” ์—†์„๊นŒ์š”? ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋งˆ์ง€๋ง‰ Conv net์ด 5x5x16์ด์—ˆ๋‹ค๋ฉด, padding ์—†์ด 5x5 convolution์„ ์‹œ์ผœ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด 1x1์˜ output์ด ์ƒ๊ธฐ๊ฒ ์ฃ ? ์ด๋•Œ output์ด 400๊ฐœ๊ฐ€ ๋‚˜์˜ค๋„๋ก filter์˜ ๊ฐœ์ˆ˜๋ฅผ 400๊ฐœ๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ๋’ค๋กœ๋„ 1x1 convolution์„ ํ•ด์„œ ๋˜‘๊ฐ™์ด ์ง„ํ–‰ํ•ด ์ตœ์ข…์ ์œผ๋กœ element๊ฐ€ 4๊ฐœ๋กœ ์ด๋ฃจ์–ด์ง„ ํ•„ํ„ฐ๋ฅผ ๋งŒ๋“ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ์ƒํ™ฉ์—์„œ FC๋Š” Fully Convolutional layer๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์ฃ . ์‹ ๊ธฐํ•œ ๊ฑด, output์ด ์กฐ๊ธˆ ๋ฐ”๋€Œ๋Š”๋ฐ์š”. original patch size์— ๋”ฑ ๋งž๊ฒŒ ๋„ฃ์–ด์ฃผ๋ฉด output์ด 1x1x4๋กœ ๋”ฑ ๋งž๊ฒŒ ๋‚˜์˜ค๋Š”๋ฐ, original patch size๋ณด๋‹ค ํฌ๊ฒŒ ๋„ฃ์–ด์ฃผ๋ฉด ์ข€ ๋‚จ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ๋Š” original patch size๊ฐ€ 14x14์ธ๋ฐ, 16x16์œผ๋กœ ๋„ฃ์–ด์ฃผ๋‹ˆ 2x2๋กœ ๋” ํฐ๊ฐ’์ด ๋‚จ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ [ c b, y b, h ] ๋กœ 4๊ฐœ์”ฉ ๋‹ฌ๋ ค์žˆ๊ฒ ์ฃ . ์—ฌ๊ธฐ์„œ output์—์„œ ์ขŒ์ƒ๋‹จ์˜ ํŒŒ๋ž€์ƒ‰ ์˜์—ญ๋งŒ ๋”ฐ๋ผ๊ฐ€๋ณด๋ฉด์š”, ์ •ํ™•ํžˆ ์›๋ณธ์—์„œ๋„ ์ขŒ์ƒ๋‹จ์— ์œ„์น˜ํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ์š”.(์ด๋Š” ๋‚˜๋จธ์ง€ ์˜์—ญ๋Œ€์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค.) ๋งŒ์•ฝ์— ์ขŒ ์ƒ๋‹จ์— ์žˆ๋Š” output์˜ ๊ฒฐ๊ณผ๊ฐ€ [1, x, x, x,]๋กœ ๋‚˜์™”๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. object๊ฐ€ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด์ฃ . ์ด๋•Œ ์šฐ ํ•˜๋‹จ์˜ output์ด [0, x, x, x]๋กœ ๋‚˜์™”๋‹ค๋ฉด ๋ง์ด์ฃ , ์ด ์˜์—ญ์€ background๋ผ๋Š” ๊ฑธ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋‘ ์˜์—ญ์ด overlapping ๋˜๋Š” ๋ถ€๋ถ„์€ ์šฐ๋ฆฌ๊ฐ€ ์–ด๋–ป๊ฒŒ ํ•ด์„ํ•ด์•ผ ํ• ๊นŒ์š”?(ํŒŒ๋ž€์ƒ‰ ๋ฐ•์Šค๋ฅผ ํ•œ ์นธ ์˜ฎ๊ฒจ๋ณด์„ธ์š”) ์ด๊ฑด ๋ชจ๋ฆ…๋‹ˆ๋‹ค. ๋งˆ์น˜ stride๋ฅผ 2๋กœ ์ฃผ๊ณ  sliding window๋ฅผ ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋งํ–ˆ๋“ฏ stride๋ฅผ ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์€ ์„ ํƒ์€ ์•„๋‹ˆ์ฃ . ๊ทธ๋Ÿฌ๋ฉด ์ด๋Ÿฐ ๋‚จ๋Š” ๊ณต๊ฐ„์ด ์•ˆ ๋‚˜์˜ค๊ฒŒ ์ด˜์ด˜ํ•˜๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? network size๋ฅผ ์กฐ๊ธˆ ์ˆ˜์ •ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ์ƒํ™ฉ์ด๋ผ๋ฉด 3x3์œผ๋กœ output์ด ๋‚˜์˜ค๋„๋ก ์ˆ˜์ •ํ•ด ์ค€๋‹ค๋ฉด ๋น„๋Š” ๊ณต๊ฐ„ ์—†์ด ๋ชจ๋“  ๊ณต๊ฐ„์—์„œ ๊ฐ’์„ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Fully convolutional layer๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•˜๋ฉด Overlap ๋˜๋Š” ์ •๋ณด๋“ค์„ ๋น ๋ฅด๊ฒŒ ๊ฑธ๋Ÿฌ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆผ ์ „์ฒด๊ฐ€ ํ•œ๋ฒˆ ๋“ค์–ด๊ฐ€๋ฉด ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌ๋˜์–ด ๋‚˜์˜ค๊ธฐ์— ์†๋„ ๋ฉด์—์„œ๋Š” ๋งค์šฐ ๋นจ๋ผ์ง€๊ฒ ์ง€์š”. ์„ฑ๋Šฅ ๋ฉด์—์„œ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋งค์šฐ ์ค„์–ด๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์–ด์ฐŒ ๋ณด๋ฉด ์–‘๋‚ ์˜ ๊ฒ€์ž…๋‹ˆ๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋งŽ์œผ๋ฉด ๋ณต์žกํ•œ function๋„ approximation์ด ๊ฐ€๋Šฅํ•˜์ฃ . ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๊ฐ€ ์ค„์–ด๋“ ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ๋Ÿฐ ๋ณต์žกํ•œ ๋ฌธ์ œ๋Š” ํ’€๊ธฐ ์–ด๋ ค์›Œ์ง์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ๋‹ค๋ฉด Fully connected layer๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์„ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ๋งŒ์•ฝ์— ํŒŒ๋ผ๋ฏธํ„ฐ ๋น„์Šทํ•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด Fully convolutional layer๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์กฐ๊ธˆ ๋” ์žฅ์ ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—„์ฒญ๋‚˜๊ฒŒ ์ž‘์€ object๋ฅผ ์ฐพ๋Š” ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•ด ๋ณด์ฃ . ๊ทธ๋Ÿฌ๋ฉด ์ผ๋ฐ˜์ ์ธ sliding window๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ทธ๋ฆผ ์‚ฌ์ด์ฆˆ๋ฅผ ํ‚ค์šฐ๊ฑฐ๋‚˜, patch size๋ฅผ ์ค„์ด๊ฑฐ๋‚˜ ํ•ด์•ผ ํ•˜๋Š”๋ฐ์š”. ์–ด์ฐŒ ๋˜์—ˆ๋“  ๊ทธ๋ฆผ์˜ resolution์„ ๊ฑด๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. (patch size๋ฅผ ์ค„์—ฌ๋„ ๊ฒฐ๊ตญ classifier์— ๋“ค์–ด๊ฐˆ ๋•Œ๋Š” classifier์˜ input size ๋งŒํผ ํ‚ค์›Œ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ) ํ•˜์ง€๋งŒ Fully convolutional layer๋Š” resolution์„ ๊ฑด๋“ค ์ด์œ ๊ฐ€ ์—†์ฃ . ๋˜ํ•œ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋ฉด ์—„์ฒญ๋‚˜๊ฒŒ ํฐ object๊ฐ€ ๋“ค์–ด์™”์„ ๋•Œ ๋Œ€์ฒ˜๊ฐ€ ์•ˆ๋œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Selective Search sliding window ๋ฐฉ๋ฒ•์˜ ๋‹จ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ROI (Range of Interest)๋ฅผ ์ œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ด๋ฏธ์ง€์—์„œ ๊ฐ์ฒด (object)๊ฐ€ ์žˆ์„ ๋ฒ•ํ•œ ์˜์—ญ์„ ์ œ์‹œํ•˜๊ณ  ์ด ์˜์—ญ์— ๋Œ€ํ•ด์„œ๋งŒ classification ํ•˜๋Š” ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค. ROI๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ ๋Œ€ํ‘œ์ ์ธ ๊ฒƒ์€ selective search, RPN ๋“ฑ์ด ์žˆ๋‹ค. Selective Search๋Š” Segmentation ๋ถ„์•ผ์— ๋งŽ์ด ์“ฐ์ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๊ณ  ๊ณผ์ •์„ ๊ฐ„๋žตํžˆ ์„ค๋ช…ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. (1) ์ฃผ๋ณ€ pixel ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. (2) ๋น„์Šทํ•œ ์˜์—ญ์„ ํ•ฉ์ณ์„œ segmented area๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. (3) ๋‹ค์‹œ ํ•ด๋‹น segmented area๋ฅผ ํ•ฉ์ณ์„œ ๋” ํฐ segmented area๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. (4) 2-3 ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ 2000์—ฌ ๊ฐœ์˜ ROI๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. RPN (Region Proposal Network) Selective Search๋ฅผ ์ ์šฉํ•œ R-CNN, Fast R-CNN์˜ ๊ฒฝ์šฐ Region Proposal์— ๊ต‰์žฅํžˆ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด ์ƒˆ๋กญ๊ฒŒ ๋“ฑ์žฅํ•œ ๋ฐฉ๋ฒ•์ด ๋ฐ”๋กœ RPN (Region Proposal Network)์ž…๋‹ˆ๋‹ค. RPN์„ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ์€ Faster R-CNN์ž…๋‹ˆ๋‹ค. RPN์—์„œ ์ฃผ๋ชฉํ•  ๊ฒƒ์€ Anchor box๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐœ๋…์˜ ๋“ฑ์žฅ์ž…๋‹ˆ๋‹ค. Anchor box๋Š” bounding box๊ฐ€ ๋  ํ›„๋ณด๊ตฐ์œผ๋กœ, ๋ฏธ๋ฆฌ ์ •ํ•ด๋‘” ํฌ๊ธฐ์™€ ๋น„์œจ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ถ”ํ›„ ํ•™์Šต์„ ํ†ตํ•ด Bounding box๊ฐ€ ์•„๋‹ ๊ฒƒ ๊ฐ™์€ anchor box๋Š” NMS (Non-maximum suppression)์„ ํ†ตํ•ด ํ›„๋ณด๊ตฐ์—์„œ ํƒˆ๋ฝ์‹œํ‚ค๊ณ , ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” anchor box์— ๋Œ€ํ•ด์„œ๋งŒ Regression ํ•˜์—ฌ ์ตœ์ข… bounding box๋ฅผ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์›๋…ผ๋ฌธ ์ €์ž๋“ค์ด ์‚ฌ์šฉํ•œ ๋ฐฉ์‹์„ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํŠน์ • ๋น„์œจ๊ณผ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” k (= 9) ๊ฐœ์˜ anchor box๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. 2. Input image๋ฅผ pre-trained CNN์— ๋„ฃ์–ด H x W x C ํฌ๊ธฐ์˜ feature map์„ ๋ฝ‘์•„๋ƒ…๋‹ˆ๋‹ค. 3. feature map์— ๋Œ€ํ•ด 3X3 filter with 1 stride and 1 padding convolution์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. convolution ๊ฒฐ๊ณผ H x W x C ํฌ๊ธฐ์˜ intermediate layer๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. 4. intermediate layer๋ฅผ ๋ฐ›์•„์„œ 1 x 1 convolution์„ classification ๊ณผ Bounding box regression์— ๋Œ€ํ•ด ๊ฐ๊ฐ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. classification layer: intermediate layer์˜ ํŠน์ • ์ง€์ ์„ ์ค‘์‹ฌ์œผ๋กœ ํ•˜๋Š” anchor box 9๊ฐœ๋ฅผ ๊ทธ๋ ค๋ณด๊ณ , ์ด anchor box์— object๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค. (๊ฐ anchor box๋ฅผ ๋ชจ๋‘ classification ํ•ด๋ฒ„๋ฆฌ๋ฉด ์—ฐ์‚ฐ๋Ÿ‰์ด ๋„ˆ๋ฌด ๋งŽ์•„์ง‘๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ๋ฅผ ๊ฐ€๋ณ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด binary classification์„ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.) ๊ทธ ๊ฒฐ๊ณผ ์ฑ„๋„ ์ˆ˜๋Š” 2 x k(= 9)๊ฐ€ ๋˜๊ณ  ์ตœ์ข… ์ถœ๋ ฅ์€ H x W x k x 2์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ’๋“ค์„ ์ ์ ˆํžˆ reshape ํ•ด์ค€ ๋‹ค์Œ Softmax๋ฅผ ์ ์šฉํ•˜์—ฌ ํ•ด๋‹น anchor box์— object๊ฐ€ ์žˆ์„ ํ™•๋ฅ  ๊ฐ’์„ ์–ป์Šต๋‹ˆ๋‹ค. Bounding box regression layer: intermediate layer์˜ ํŠน์ • ์ง€์ ์„ ์ค‘์‹ฌ์œผ๋กœ ํ•˜๋Š” anchor box 9๊ฐœ๋ฅผ ๊ทธ๋ ค๋ด…๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์ฑ„๋„ ์ˆ˜๋Š” 4 x k(= 9)๊ฐ€ ๋˜๊ณ  ์ตœ์ข… ์ถœ๋ ฅ์€ H x W x k x 4์ž…๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ์ดํ›„์— ํ•™์Šต์„ ํ†ตํ•ด anchor box์˜ ์œ„์น˜, ํฌ๊ธฐ๋ฅผ ์ •๋ฐ€ํ•˜๊ฒŒ ์กฐ์ •ํ•˜์—ฌ object๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ detection ํ•˜๋Š” bounding box๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. 5. ์ด์ œ ์•ž์„œ ์–ป์€ ๊ฐ’๋“ค๋กœ ์ตœ์ข… Bounding box๋ฅผ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € Non-Maximum-Suppressio์„ ํ†ตํ•ด์„œ object๊ฐ€ ์žˆ์„ ํ™•๋ฅ ์ด ๋†’์€ n ๊ฐœ์˜ Anchor box๋ฅผ ์ถ”๋ ค๋ƒ…๋‹ˆ๋‹ค. ์ดํ›„ ํ•™์Šต์„ ํ†ตํ•ด classification๊ณผ Bounding box regression์„ ๋™์‹œ์— end-to-end๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. (ํ•™์Šต์˜ ์ž์„ธํ•œ ๋‚ด์šฉ์€ Faster R-CNN์—์„œ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค.) Selective Search๊ฐ€ 2000๊ฐœ์˜ RoI๋ฅผ ์ œ์‹œํ•˜๋Š”๋ฐ ๋ฐ˜ํ•ด RPN์€ 800๊ฐœ ์ •๋„์˜ RoI๋ฅผ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ CPU์—์„œ ์ž‘๋™ํ•˜๋Š” Selective Search์™€๋Š” ๋‹ฌ๋ฆฌ GPU์—์„œ ์ž‘๋™ํ•˜๋ฏ€๋กœ ์†๋„๋Š” ๋”๋”์šฑ ๋น ๋ฆ…๋‹ˆ๋‹ค. Unified detection 1-stage detection ์ค‘ ํ•˜๋‚˜์ธ YOLO์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ Object Dection์€ 1) region proposal 2) classification ์ด๋ ‡๊ฒŒ ๋‘ ๋‹จ๊ณ„๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ง„ํ–‰ํ–ˆ๋‹ค๋ฉด Unified detection์€ ํ•œ ๋ฒˆ์— Object Detection์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ๊ทธ๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. input image๋ฅผ S X S ๊ฐœ์˜ grid cell๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ๊ฐ grid cell๋งˆ๋‹ค Bounding box prediction, box confidence score, Conditional class probability๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. Bounding box prediction (x, y, w, h): object๊ฐ€ ์žˆ์„ ๋งŒํ•œ ์˜์—ญ์— B ๊ฐœ์˜ Bounding Box๋ฅผ ๊ทธ๋ฆฌ๊ณ , Bounding box์˜ ์œ„์น˜ ๋ฐ ํฌ๊ธฐ๋ฅผ (x, y, w, h)๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. box confidence score (Pc): ํ•ด๋‹น Bounding box๋งˆ๋‹ค box confidence score (Pc)๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. box confidence score (Pc)= Pr(Object) x IoU box confidence score๋Š” ํ•ด๋‹น grid cell์— object๊ฐ€ ์žˆ์„ ํ™•๋ฅ " Pr(Object)"์™€ ์žˆ๋‹ค๋ฉด Ground truth boudning box์™€ ์˜ˆ์ƒํ•œ bouding box๊ฐ€ ์–ผ๋งˆ๋‚˜ ๊ฒน์ณ์ง€๋Š”์ง€ ํ‰๊ฐ€ํ•˜๋Š” "IoU"๋ฅผ ๊ณฑํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. Conditional class probability (Ci): Ci์€ object๊ฐ€ Bounding box ์•ˆ์— ์žˆ์„ ๋•Œ ํ•ด๋‹น object๊ฐ€ i ๋ฒˆ์งธ class์— ํ•ด๋‹นํ•  ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ด 20๊ฐœ์˜ class๋กœ classification ํ•˜๊ธฐ ๋•Œ๋ฌธ์— C1๋ถ€ํ„ฐ C20๊นŒ์ง€์˜ ๊ฐ’์„ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. (3) ์ตœ์ข…์ ์œผ๋กœ ๊ฐ grid cell์—์„œ ๋ฝ‘์•„๋‚ธ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋Š” B*(4+1) + C ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฝ‘์•„๋‚ธ ๋ฒกํ„ฐ์˜ ํ˜•ํƒœ๋ฅผ ๋ณด๋ฉด YOLO๋Š” ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์ž‘์€ grid cell๋กœ ๋‚˜๋ˆ„๊ณ , ๊ฐ grid cell ๋ณ„๋กœ Bounding box ์˜ˆ์ธก ์™€ classification์„ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์œ„์˜ ์„ค๋ช…์€ ๊ฐ„์†Œํ™”๋œ ์„ค๋ช…์œผ๋กœ ์ž์„ธํ•œ ๋‚ด์šฉ์€ (4) ๊ฐ์ฒด ๊ฒ€์ถœ - YOLO์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค. ) Reference ๋…ผ๋ฌธ: Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks OverFeat:Integrated Recognition, Localization and Detection using Convolutional Networks ๋ธ”๋กœ๊ทธ: ๊ฐˆ์•„๋จน๋Š” Object Detection Faster R-CNN Sliding window๋Š” ๋ฌด์—‡์ผ๊นŒ? ์œ ํŠœ๋ธŒ: KoreaUniv DSBA | You Only Look Once : Unified, Real-Time Object Detection C4W3L03 3) NMS (Non-Maximum Suppression) & Anchor box ์„œ๋ก  ์•ž์„œ 1) General process of object detection์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ, ๋Œ€๋‹ค์ˆ˜์˜ object detection algorithm์€ object๊ฐ€ ์กด์žฌํ•˜๋Š” ์œ„์น˜ ์ฃผ๋ณ€์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ score๊ฐ€ ๋†’์€ bounding box๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ๋ฌธ์ œ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ค‘ ํ•˜๋‚˜์˜ bounding box๋งŒ์„ ์„ ํƒํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋•Œ ์ ์šฉํ•˜๋Š” ๊ธฐ๋ฒ•์ด non-max suppression์ž…๋‹ˆ๋‹ค. ์ฆ‰, Non-Maximum Suppression์€ object detector๊ฐ€ ์˜ˆ์ธกํ•œ bounding box ์ค‘์—์„œ ์ •ํ™•ํ•œ bounding box๋ฅผ ์„ ํƒํ•˜๋„๋ก ํ•˜๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. optimal ํ•œ solution ์ผ ์ˆ˜๋Š” ์—†๊ณ  local maxima๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. NMS์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ๋‚ด์šฉ์ด ๊ถ๊ธˆํ•˜์‹œ๋‹ค๋ฉด ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ณ  YOLO์—์„œ ์‚ฌ์šฉ๋œ NMS์— ๋Œ€ํ•ด ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์ด ๋ธ”๋กœ๊ทธ์— ์•„์ฃผ ์ž์„ธํžˆ ์„ค๋ช…์ด ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ NMS NMS์˜ ๊ณผ์ • ๋ชจ๋“  Bounding box๋Š” ์ž์‹ ์ด ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์–ผ๋งˆ๋‚˜ ์ž˜ ์žก์•„๋‚ด์ง€ ๋‚˜ํƒ€๋‚ด๋Š” confidence score๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋ชจ๋“  bounding box์— ๋Œ€ํ•˜์—ฌ threshold ์ดํ•˜์˜ confidence score๋ฅผ ๊ฐ€์ง€๋Š” Bounding Box๋Š” ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. Confidence score๊ฐ€ ์ผ์ • ์ˆ˜์ค€ ์ดํ•˜์ธ bounding box ๋“ค์— ๋Œ€ํ•ด ์ผ์ฐจ์ ์œผ๋กœ ํ•„ํ„ฐ๋ง์„ ๊ฑฐ์น˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ๋‚จ์€ Bounding Box๋“ค์„ Confidence score ๊ธฐ์ค€ ๋ชจ๋‘ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ๋งจ ์•ž์— ์žˆ๋Š” Bounding box ํ•˜๋‚˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์žก๊ณ , ๋‹ค๋ฅธ bounding box์™€ IoU ๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. IoU๊ฐ€ threshold ์ด์ƒ์ธ Bounding box๋“ค์€ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. Bounding box๋ผ๋ฆฌ IoU๊ฐ€ ๋†’์„์ˆ˜๋ก, ์ฆ‰ ๋งŽ์ด ๊ฒน์ณ์งˆ์ˆ˜๋ก ๊ฐ™์€ ๋ฌผ์ฒด๋ฅผ ๊ฒ€์ถœํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ณผ์ •์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹œํ–‰ํ•˜์—ฌ ๋ชจ๋“  Bounding box๋ฅผ ๋น„๊ตํ•˜๊ณ  ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. Confidense threshold๊ฐ€ ๋†’์„์ˆ˜๋ก, IoU threshold๊ฐ€ ๋‚ฎ์„์ˆ˜๋ก ๋” ๋งŽ์€ bounding box๊ฐ€ ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ Confidence score์˜ threshold๋ฅผ 0.4๋ผ๊ณ  ์ง€์ •ํ•˜๋ฉด Confidence score๊ฐ€ 0.4 ์ดํ•˜์ธ bounding box๋“ค์€ ๋ชจ๋‘ ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์ด ๊ทธ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. Bounding box๋ฅผ Confidence score ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ [0.9 ๋ฐ•์Šค, 0.8 ๋ฐ•์Šค, 0.7 ๋ฐ•์Šค, 0.65 ๋ฐ•์Šค, 0.6 ๋ฐ•์Šค(์™ผ์ชฝ), 0.6 ๋ฐ•์Šค(์˜ค๋ฅธ์ชฝ)] ์ด๋ ‡๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Confidence score 0.9์ธ bounding box๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์žก๊ณ  ๋’ค์˜ ๋ชจ๋“  ๋ฐ•์Šค๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. 0.8 ๋ฐ•์Šค์™€๋Š” ๊ฒน์น˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋‚จ๊ฒจ๋‘ . 0.7 ๋ฐ•์Šค์™€ IoU๊ฐ€ threshold ์ด์ƒ์ด๋ฏ€๋กœ ์ด ๋ฐ•์Šค๋Š” 0.9 ๋ฐ•์Šค์™€ ๊ฐ™์€ ๊ฒƒ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค๊ณ  ๊ฐ„์ฃผํ•˜๊ณ  ์ œ๊ฑฐํ•จ. 0.65 ๋ฐ•์Šค, 0.6 ๋ฐ•์Šค(์™ผ์ชฝ) ๊ณผ๋Š” ๊ฒน์น˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋‚จ๊ฒจ๋‘ . 0.6 ๋ฐ•์Šค(์˜ค๋ฅธ์ชฝ)์™€ IoU๊ฐ€ ๋˜ threshold ์ด์ƒ์ด๋ฏ€๋กœ ์ œ๊ฑฐํ•จ. ์ด์ œ 0.8 ๋ฐ•์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋’ค์˜ ๋ชจ๋“  ๋ฐ•์Šค์™€ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ทธ๋ฆผ์—์„œ ํ•˜์–€ ๋ฐ•์Šค๊ฐ€ NMS๋ฅผ ๋Œ๋ฆฌ๊ณ  ๋‚จ์€ ๋ฐ•์Šค์ด๊ณ , ๋นจ๊ฐ„ ๋ฐ•์Šค๋Š” NMS ๊ณผ์ •์œผ๋กœ ์ œ๊ฑฐ๋œ ๋ฐ•์Šค์ž…๋‹ˆ๋‹ค. YOLO์—์„œ์˜ Non-Max Suppression ์ดํ›„ ๋ฌธ์„œ์—์„œ ์„ค๋ช…๋  YOLO ์—ญ์‹œ non-max suppression์„ ์ด์šฉํ•˜๋Š”๋ฐ์š”, ์ด ๊ฒฝ์šฐ ์ถ”๊ฐ€์ ์œผ๋กœ ๊ณ ๋ คํ•ด์•ผ ํ•  ์ ์ด ์žˆ๊ณ  ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ณผ์ • ๋ชจ๋“  output prediction์€ [ c b, y b, w ] ํ˜•ํƒœ์˜ array ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Cell๋งˆ๋‹ค c 0.6 ์ธ ๋ฐ•์Šค๋ฅผ ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ๋‚จ์•„์žˆ๋Š” ๊ฒƒ ์ค‘์—์„œ c ๊ฐ€ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ณ ๋ฆ…๋‹ˆ๋‹ค. ์„ ํƒํ•œ ๋ฐ•์Šค์™€ o > 0.5 ์ธ ๋‹ค๋ฅธ bounding ๋ฐ•์Šค๋ฅผ ๋ชจ๋‘ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์„ ๋‹ค์‹œ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. (๊ทธ๋‹ค์Œ์œผ๋กœ c ๊ฐ€ ๋†’์€ ๋ฐ•์Šค๋ฅผ ๊ณ ๋ฆ„) ์˜ˆ์‹œ ์˜ˆ๋ฅผ ๋“ค์–ด YOLO๋ฅผ ํ†ตํ•ด ์–ป์–ด๋‚ธ bounding box๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ๋“ค์–ด์กŒ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด c ๊ฐ€ 0.9์ธ bounding box๊ฐ€ ์ œ์ผ ๋จผ์ € ์„ ํƒ์ด ๋˜๊ณ , ์ด์™€ ๊ฒน์น˜๋Š” 0.6, 0.7์€ ์ด bounding box์™€ IoU๊ฐ€ ๋ˆˆ์œผ๋กœ ๋ด๋„ 0.5๋Š” ๋„˜์„ ๊ฒƒ ๊ฐ™์ฃ ? ์ œ๊ฑฐ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ c ๋ฅผ ์„ ํƒํ•˜๋Š” ๋‹จ๊ณ„๋กœ ๋Œ์•„์™€์„œ ๊ทธ๋‹ค์Œ์œผ๋กœ๋Š” ์™ผ์ชฝ์— ์žˆ๋Š” c ๊ฐ€ 0.8์ธ bounding box๊ฐ€ ์„ ํƒ๋˜๊ณ  0.7์€ ์ œ๊ฑฐ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. NMS์˜ ๋ฌธ์ œ์ ๊ณผ Anchor box ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์—๋Š” ๋ฌธ์ œ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ object๋ผ๋ฆฌ ๊ฒน์น  ๋•Œ ๋‹ค๋ฅธ object์— ๋Œ€ํ•œ bounding box๊นŒ์ง€ ๋‚ ์•„๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ์š”. ํ˜„์‹ค์—์„œ๋Š” object๋ผ๋ฆฌ ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งค์šฐ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผํ…Œ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ๊ทธ๋ƒฅ NMS๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด, ์ž๋™์ฐจ์— ๋Œ€ํ•ด์„œ detection์„ ํ•˜๋ฉด ํŠธ๋Ÿญ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” detection์„ ๋ชปํ•˜๊ณ  ๋‚ ์•„๊ฐˆ ์ˆ˜๋„ ์žˆ๊ฒ ์ฃ ? ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ๋ฐฉ๋ฒ•์ด Anchor box์ž…๋‹ˆ๋‹ค. Anchor box๋Š” ํƒ์ง€ํ•˜๋ ค๋Š” ๊ฐ์ฒด์˜ ๋ชจ์–‘์„ ์ •ํ•ด๋†“๊ณ  ๊ฐ์ฒด๊ฐ€ ํƒ์ง€๋˜์—ˆ์„ ๋•Œ ์–ด๋–ค anchor box์™€ ์œ ์‚ฌํ•œ์ง€ ํŒ๋‹จํ•ด์„œ ๋ฒกํ„ฐ ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด Anchor box๊ฐ€ 2๊ฐœ๋ผ๋ฉด vector๋Š” ์›๋ž˜ ๋ฒกํ„ฐ์—์„œ ๋‘ ๊ฐœ๋ฅผ ์ด์–ด๋ถ™์ธ ๊ฒƒ๊ณผ ๊ฐ™์ด ์ƒ๊ธฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ( [ c b, y b, z c, 2 c, c b, y b, z c, 2 c ] ) detection์„ ํ•  ๋•Œ๋Š” detection์„ ํ†ตํ•ด ์˜ˆ์ธกํ•œ object์˜ boundary box๊ฐ€ anchor box1์— ์œ ์‚ฌํ•œ์ง€ 2์— ์œ ์‚ฌํ•œ์ง€ IoU๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. IoU ๊ณ„์‚ฐ ํ›„ ๋†’์€ IoU๋ฅผ ๊ฐ–๋Š” anchor box ์ž๋ฆฌ์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. Reference YOLO, Object Detection Network ํ™๋Ÿฌ๋‹ | NMS(Non-Maximum Suppression) IoU(Intersection of Union), NMS(Non-Maximum Suppression) C4W3L08 (3) ๊ฐ์ฒด ๊ฒ€์ถœ ๋ชจ๋ธ๋“ค(โ˜…์ž‘์„ฑ ์ค‘) ์›์Šค ํ…Œ์ด์ง€์™€ ํˆฌ ์Šคํ…Œ์ด์ง€ ๋ชจ๋ธ์˜ ์ฐจ์ด์  ์„ค๋ช… 1) ํˆฌ ์Šคํ…Œ์ด์ง€ ๋ชจ๋ธ๋“ค ... 1) R-CNN R-CNN R-CNN ์‹œ๋ฆฌ์ฆˆ๋“ค์˜ ์‹œ์ž‘์„ ์—ฐ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. CNN์„ ์‚ฌ์šฉํ•˜์—ฌ object detection task์˜ ์ •ํ™•๋„์™€ ์†๋„๋ฅผ ํš๊ธฐ์ ์œผ๋กœ ํ–ฅ์ƒ์‹œ์ผฐ์œผ๋‚˜ ์ดˆ๊ธฐ ๋ชจ๋ธ์ธ ๋งŒํผ ๋ณต์žกํ•˜๊ณ , ์—„์ฒญ ์˜ค๋ž˜ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ 1. Image Input 2. Region proposal Selective search ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ROI๋ฅผ 2000์—ฌ ๊ฐœ ์ •๋„์˜ Region์„ ๋ฝ‘์•„๋‚ด์–ด bounding box๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. Selective Search ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ Segmentation ๋ถ„์•ผ์— ๋งŽ์ด ์“ฐ์ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ฉฐ, ๊ฐ์ฒด์™€ ์ฃผ๋ณ€ ๊ฐ„์˜ ์ƒ‰๊ฐ(Color), ์งˆ๊ฐ(Texture) ์ฐจ์ด, ๋‹ค๋ฅธ ๋ฌผ์ฒด์— ์• ์›Œ์Œ“์—ฌ์žˆ๋Š”์ง€(Enclosed) ์—ฌ๋ถ€ ๋“ฑ์„ ํŒŒ์•…ํ•ด์„œ ๋‹ค์–‘ํ•œ ์ „๋žต์œผ๋กœ ๋ฌผ์ฒด์˜ ์œ„์น˜๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด Bounding box๋“ค์„ Random ํ•˜๊ฒŒ ๋งŽ์ด ์ƒ์„ฑ์„ ํ•˜๊ณ  ์ด๋“ค์„ ์กฐ๊ธˆ์”ฉ Merge ํ•ด๋‚˜๊ฐ€๋ฉด์„œ ๋ฌผ์ฒด๋ฅผ ์ธ์‹ํ•ด๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ![](https://wikidocs.net/images/page/141994/Selective_Search_Algorithm.png) 3. Warping CNN์— ๋„ฃ๊ธฐ ์ „์— ๊ฐ™์€ ์‚ฌ์ด์ฆˆ(227 x 227)๋กœ warping ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. bounding box ํฌ๊ธฐ์˜ ๋น„์œจ์€ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ์ผ๋ฅ ์ ์œผ๋กœ ๊ฐ™์€ ์‚ฌ์ด์ฆˆ๋กœ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์ด ๊ณผ์ •์—์„œ input image๊ฐ€ ์™œ๊ณก๋˜๊ณ  ์ •๋ณด๊ฐ€ ์†Œ์‹ค๋˜๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. 4. CNN feature extract 3๋ฒˆ์˜ ๊ณผ์ •์„ ํ†ตํ•ด ํฌ๊ธฐ๊ฐ€ ์กฐ์ ˆ๋œ Bounding box๋ฅผ CNN ๋ชจ๋ธ์— ๋„ฃ์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ ์ด๋ฏธ์ง€ ๋„ท ๋ฐ์ดํ„ฐ(ILSVRC2012 classification)๋กœ ๋ฏธ๋ฆฌ ํ•™์Šต๋œ CNN ๋ชจ๋ธ์„ ๊ฐ€์ ธ์˜จ ๋‹ค์Œ Object Detection ์šฉ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ fine tuning ํ•˜๋Š” ๋ฐฉ์‹์„ ์ทจํ–ˆ์Šต๋‹ˆ๋‹ค. 5. Image Classification with SVM (Support Vector Machine) CNN์„ ํ†ตํ•ด ์ถ”์ถœํ•œ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ๊ฐ์˜ ํด๋ž˜์Šค ๋ณ„๋กœ SVM Classifier๋ฅผ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค. (CNN Classifier๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  SVM์„ ์“ฐ๋Š” ์ด์œ ๋Š” ๊ทธ๋ƒฅ CNN Classifier๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด SVM์„ ์ผ์„ ๋•Œ๋ณด๋‹ค mAP ์„ฑ๋Šฅ์ด 4% ์ •๋„ ๋‚ฎ์•„์กŒ๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ํ•จ) SVM Classification์„ ํ•œ ํ›„์˜ 2000์—ฌ ๊ฐœ์˜ bounding box๋“ค์€ ์–ด๋–ค ๋ฌผ์ฒด์ผ ํ™•๋ฅ  ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 6. Non-Maximum Suppression 2000์—ฌ ๊ฐœ์˜ bounding box๋Š” ์–ด๋–ค ๊ฐ์ฒด์ผ ํ™•๋ฅ  ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋˜์—ˆ์ง€๋งŒ ์ด ๋ฐ•์Šค๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋™์ผ ๋ฌผ์ฒด์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ bounding box๊ฐ€ ์„ค์ •๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ์ด๋Ÿฐ ๊ฒฝ์šฐ Non-Maximum Suppression์„ ํ†ตํ•ด ๊ฐ์ฒด ์ „์ฒด๋ฅผ ๋Œ€์ƒํ™”ํ•  ์ˆ˜ ์žˆ๋Š” bounding box ํ•œ ๊ฐœ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. 7. Bounding box regression (BBR) Selective Search๋กœ ์ฐพ์€ Bounding Box ์œ„์น˜๊ฐ€ ๋ถ€์ •ํ™•ํ•˜๊ธฐ์— Bounding Box Regression์„ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. bounding box ์œ„์น˜ ์„ ์ •์„ ๊ต์ •ํ•˜๊ณ  ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•œ ๊ณผ์ •์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. Ground Truth, G ์™€ ์ดˆ๊ธฐ Bounding Box, P์˜ ์œ„์น˜๋Š” ์ค‘์‹ฌ์  ์ขŒํ‘œ (x, y)์™€ box์˜ ํฌ๊ธฐ (width, height)๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. P (x, y, w, h)๊ฐ€ input์œผ๋กœ ๋“ค์–ด์™”์„ ๋•Œ ์ด๋ฅผ ์ด๋™์‹œ์ผœ G๋ฅผ ์ž˜ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ. dx, dy, exp(dw), exp(dh)๋ฅผ ๊ฐ๊ฐ ๊ณฑํ•˜์—ฌ ์˜ˆ์ธก์น˜ G_hat์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. d ํ•จ์ˆ˜๋Š” P๋ฅผ G_hat์œผ๋กœ ์ด๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ์ด๋™๋Ÿ‰์„ ์˜๋ฏธํ•˜๋ฉฐ BBR์—์„œ ํ•™์Šต์‹œํ‚ค๋Š” ๊ฒƒ์€ ์ด d ํ•จ ์ˆ˜์ž…๋‹ˆ๋‹ค. t ํ•จ์ˆ˜๋Š” P๋ฅผ G๋กœ ์ด๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ์ด๋™๋Ÿ‰์„ ์˜๋ฏธํ•˜๋ฉฐ d ํ•จ์ˆ˜์™€ ํ˜•ํƒœ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. loss function์€ "t ํ•จ์ˆ˜์™€ d ํ•จ์ˆ˜์˜ MSE" + "L2 normalization"์„ ์ถ”๊ฐ€ํ•œ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. ๋‹จ์  R-CNN์€ ๋‹น์‹œ์—๋Š” ํš๊ธฐ์ ์ด์—ˆ์ง€๋งŒ ์—ฌ๋Ÿฌ ๋‹จ์ ์ด ์กด์žฌํ–ˆ๋Š”๋ฐ, ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. AlexNet์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด image๋ฅผ ๊ฐ•์ œ๋กœ ๋ณ€ํ˜• ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. warping ํ•˜๋Š” ๊ณผ์ •์—์„œ input ์ด๋ฏธ์ง€๊ฐ€ ์™œ๊ณก๋˜๊ณ  ์ •๋ณด ์†์‹ค์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค Selective Search๋ฅผ ํ†ตํ•ด ๋ฝ‘ํžŒ 2000๊ฐœ์˜ Region proposal ํ›„๋ณด๋ฅผ ๋ชจ๋‘ CNN์— ์ง‘์–ด๋„ฃ๊ธฐ ๋•Œ๋ฌธ์— training / testing ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. Selective Search๋‚˜ SVM์ด GPU์— ์ ํ•ฉํ•œ ๊ตฌ์กฐ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. Computation sharing์ด ์ผ์–ด๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. (CNN, SVM, Bounding Box Regression ์ด ์„ธ ๊ฐ€์ง€์˜ ๋ชจ๋ธ์ด ๊ฒฐํ•ฉ๋œ ํ˜•ํƒœ๋กœ ํ•œ ๋ฒˆ์— ํ•™์Šต์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, end-to-end ํ›ˆ๋ จ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. SVM, Bounding Box Regression์„ ํ•™์Šต์‹œ์ผœ๋„ Back propagation์ด ์•ˆ ๋˜๋ฏ€๋กœ CNN์€ ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ) Reference ๊ฐ์ฒด ๊ฒ€์ถœ(Object Detection) ๋”ฅ๋Ÿฌ๋‹ ๊ธฐ์ˆ : R-CNN, Fast R-CNN, Faster R-CNN ๋ฐœ์ „ ๊ณผ์ • ํ•ต์‹ฌ ์š”์•ฝ PR-012: Faster R-CNN : Towards Real-Time Object Detection with Region Proposal Networks ๊ฐˆ์•„๋จน๋Š” ๋จธ์‹ ๋Ÿฌ๋‹|R-CNN ์‹ค์ „ ํ…์„œ ํ”Œ๋กœ 2๋ฅผ ํ™œ์šฉํ•œ ๋”ฅ๋Ÿฌ๋‹-์ปดํ“จํ„ฐ ๋น„์ „ Deep Learning AI : C4W3L01~10 cs231n - Lecture 11 Detection and Segmentation R-CNN์„ ์•Œ์•„๋ณด์ž 2) Fast R-CNN ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ Fast R-CNN์€ ์ด์ „์— ์„œ์ˆ ํ•œ R-CNN์˜ ๋‹จ์ ์ธ 1) AlexNet์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Image๋ฅผ 224x224 ํฌ๊ธฐ๋กœ ๊ฐ•์ œ๋กœ warping ์‹œ์ผฐ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€ ๋ณ€ํ˜•์œผ๋กœ ์ธํ•œ ์„ฑ๋Šฅ ์†์‹ค์ด ์กด์žฌ, 2) Selective Search๋ฅผ ํ†ตํ•ด ๋ฝ‘ํžŒ 2000๊ฐœ์˜ Image Proposal ํ›„๋ณด๋ฅผ ๋ชจ๋‘ CNN ๋ชจ๋ธ์— ์ง‘์–ด๋„ฃ๊ธฐ ๋•Œ๋ฌธ์—, training, testing ์‹œ๊ฐ„์ด ๋งค์šฐ ์˜ค๋ž˜ ๊ฑธ๋ฆผ, 3) Selective Search๋‚˜ SVM์ด GPU๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ์—” ์ ํ•ฉํ•œ ๊ตฌ์กฐ๊ฐ€ ์•„๋‹˜, 4) ๋’ท๋ถ€๋ถ„์—์„œ ์ˆ˜ํ–‰ํ•œ Computation์„ Share ํ•˜์ง€ ์•Š์Œ์„ ๋ณด์™„ํ•˜๊ณ ์ž ๋‚˜์˜จ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ „์ฒด์ ์ธ ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1-1. CNN Feature extraction Input image๋ฅผ ๋ฏธ๋ฆฌ ํ•™์Šต๋œ CNN์„ ํ†ต๊ณผ์‹œ์ผœ feature map์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ VGG16 ๋ชจ๋ธ์„ ๋ณ€ํ˜•ํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 1-2. Region proposal Selective Search๋ฅผ ํ†ตํ•ด์„œ ์ฐพ์€ ๊ฐ๊ฐ์˜ RoI์— ๋Œ€ํ•˜์—ฌ bounding box๋ฅผ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค. 2. Region projection 1-2 ๊ณผ์ •์—์„œ ์ฐพ์€ bounding box๋ฅผ 1-1 ๊ณผ์ •์—์„œ ์ฐพ์€ feature map ์œ„์— ํˆฌ์˜(projection) ํ•ฉ๋‹ˆ๋‹ค. Selective search๋ฅผ ํ†ตํ•ด ์–ป์€ bounding box๋Š” sub-sampling ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š์€ ๋ฐ˜๋ฉด, ์›๋ณธ ์ด๋ฏธ์ง€์˜ feature map์€ sub-sampling ๊ณผ์ •์„ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ฑฐ์ณ ํฌ๊ธฐ๊ฐ€ ์ž‘์•„์กŒ์Šต๋‹ˆ๋‹ค. ์ž‘์•„์ง„ feature map์—์„œ bounding box์— ํ•ด๋‹นํ•˜๋Š” ์˜์—ญ์„ ์ฐพ์•„์•ผ ํ•˜๋Š”๋ฐ, ์ด๋Š” bounding box์˜ ํฌ๊ธฐ์™€ ์ค‘์‹ฌ ์ขŒํ‘œ๋ฅผ sub sampling ratio์— ๋งž๊ฒŒ ๋ณ€๊ฒฝ์‹œ์ผœ์คŒ์œผ๋กœ์จ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 3. RoI Pooling ์ถ”์ถœํ•œ RoI feature map์„ max-pooling ํ•˜์—ฌ ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ pooling map์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ROI feature map ์œ„์— grid๋ฅผ ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์…€์— ๋Œ€ํ•˜์—ฌ max pooling์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. RoI pooling์„ ์ด์šฉํ•จ์œผ๋กœ์จ ๋žœ๋ค ํ•œ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ROI projection์ด FC layer์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ๊ณ ์ •๋œ ํฌ๊ธฐ๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 4. Image Classification & Bounding Box Prediction 3๋ฒˆ ๊ณผ์ •์„ ํ†ตํ•ด ์–ป์€ feature map์„ FC layer์— ํ†ต๊ณผ์‹œ์ผœ feature vector๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ด feature vector๋ฅผ ์ด์šฉํ•˜์—ฌ classification๊ณผ bounding box prediction์„ ๊ฐ๊ฐ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 5. Loss function & training 4์—์„œ ๋ฐœ์ƒํ•œ ๋ฐœ์ƒํ•œ classification loss & bounding box prediction loss๋ฅผ ์ด์šฉํ•ด back propagation ํ•˜์—ฌ ์ „์ฒด ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. (์ €์ž๋“ค์€ layer conv layer3๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ๊ฐ’์€ ๊ณ ์ •ํ•˜๊ณ , ์ดํ›„ layer์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ํ•™์Šต๋  ์ˆ˜ ์žˆ๋„๋ก ํ•  ๋•Œ ๊ฐ€์žฅ ์„ฑ๋Šฅ์ด ์ข‹์•˜๋‹ค๊ณ  ์ฃผ์žฅํ•ฉ๋‹ˆ๋‹ค) ์ด๋•Œ, classificaiton loss์™€ bounding box regression loss๋ฅผ ์ ์ ˆํžˆ ์—ฎ์–ด์ฃผ์–ด multi-task loss๋ฅผ ๋งŒ๋“ค๋ฉด classification & bounding box regression์„ ๋™์‹œ์— ์ง„ํ–‰ํ•˜๋Š” ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ) - u: positive sample์ธ ๊ฒฝ์šฐ 1, negative sample์ธ ๊ฒฝ์šฐ 0์œผ๋กœ ์„ค์ •๋˜๋Š” index parameter์ž…๋‹ˆ๋‹ค. - smooth L1: ์ €์ž๋“ค์€ ์‹คํ—˜ ๊ณผ์ •์—์„œ ๋ผ๋ฒจ ๊ฐ’๊ณผ ์ง€๋‚˜์น˜๊ฒŒ ์ฐจ์ด๊ฐ€ ๋งŽ์ด ๋‚˜๋Š” outlier ์˜ˆ์ธก ๊ฐ’์— ๋Œ€ํ•ด L2 distance๋กœ ๊ณ„์‚ฐํ•˜์—ฌ ์ ์šฉํ•  ๊ฒฝ์šฐ gradient๊ฐ€ explode ํ•ด๋ฒ„๋ฆฌ๋Š” ํ˜„์ƒ์„ ๊ด€์ฐฐํ–ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ smooth_L1์„ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Non maximum suppression ์˜ˆ์ธกํ•œ bounding box์— ๋Œ€ํ•˜์—ฌ Non maximum suppression์„ ํ†ตํ•ด ์ตœ์ ์˜ bounding box๋งŒ์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ˆซ์ž๋กœ ๋ณด๋Š” ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ ๋…ผ๋ฌธ์—์„œ ์ €์ž๋“ค์ด ์ž‘์„ฑํ•œ ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ๋ฅผ ์ˆซ์ž์™€ ํ•จ๊ป˜ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1-1. CNN Feature extraction Input image๋ฅผ VGG16์˜ layer 13๊นŒ์ง€ ํ†ต๊ณผ์‹œ์ผœ feature map์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ 14x14x512 feature map์ด ์ถ”์ถœ๋ฉ๋‹ˆ๋‹ค. 1-2. Region proposal Selective Search๋ฅผ ํ†ตํ•ด์„œ ์ฐพ์€ ๊ฐ๊ฐ์˜ RoI์— ๋Œ€ํ•˜์—ฌ bounding box๋ฅผ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค. ์•ฝ 2000์—ฌ ๊ฐœ์˜ Bounding box๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. 2. Region projection 1-1๋ฒˆ์—์„œ ๋ฝ‘์•„๋‚ธ 14x14x512 feature map์„ ๊ธฐ์ค€์œผ๋กœ Region projection์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 3. RoI Pooling VGG16์˜ ๋งˆ์ง€๋ง‰ max pooling layer๋ฅผ RoI pooling layer๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ RoI pooling์„ ํ†ตํ•ด ์ถœ๋ ฅ๋˜๋Š” feature map์€ (FC layer์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก) ํฌ๊ธฐ๋ฅผ 7x7x512๋กœ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. - Input : 14x14 sized 512 feature maps/bounding box, total, 2000 bounding box - Output : 7x7x512 feature maps/bounding box, total 2000 bounding box - ๊ฐ bounding box๋งˆ๋‹ค 7x7x512์˜ feature map์„ flatten ํ•œ ํ›„ fc layer์— ์ž…๋ ฅํ•˜์—ฌ 4096 ํฌ๊ธฐ์˜ feature vector๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. 4-1. Image Classification 4096 ํฌ๊ธฐ์˜ feature vector๋ฅผ FC layer์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. FC layer๋Š” K ๊ฐœ์˜ class์™€ ๋ฐฐ๊ฒฝ์„ ํฌํ•จํ•˜์—ฌ (K+1) ๊ฐœ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋„๋ก (K+1) ํฌ๊ธฐ์˜ Feature vector๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. 2000์—ฌ ๊ฐœ์˜ Bounding box๊ฐ€ ํ•œ ๋ฒˆ์”ฉ FC layer๋ฅผ ํ†ต๊ณผํ•˜์—ฌ (K+1) ํฌ๊ธฐ์˜ Feature vector๋ฅผ 1๊ฐœ์”ฉ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 4-2. Bounding Box Prediction 4096 ํฌ๊ธฐ์˜ feature vector๋ฅผ FC layer์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. FC layer๋Š” class ๋ณ„๋กœ bounding box์˜ ์ขŒํ‘œ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋„๋ก (K+1) x4 ํฌ๊ธฐ์˜ Feature vector๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. 2000์—ฌ ๊ฐœ์˜ Bounding box๊ฐ€ ํ•œ ๋ฒˆ์”ฉ FC layer๋ฅผ ํ†ต๊ณผํ•˜์—ฌ (K+1) x4 ํฌ๊ธฐ์˜ Feature vector๋ฅผ 1๊ฐœ์”ฉ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 5. training Multi-task loss๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ region proposal์— ๋Œ€ํ•œ Classifier์™€ Bounding box regressor์˜ loss๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ Backpropagation์„ ํ†ตํ•ด Image Classifier, Bounding box regressor, CNN์„ ํ•œ ๋ฒˆ์— ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค. (๋‹ค๋งŒ VGG16์˜ conv layer3๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ๊ฐ’์€ ๊ณ ์ •ํ•˜๊ณ , ์ดํ›„ layer์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค.) ์žฅ์  Fast R-CNN์—๋Š” 1๊ฐœ์˜ CNN ์—ฐ์‚ฐ๋งŒ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ R-CNN์—์„œ CNN ์—ฐ์‚ฐ์„ 2000์—ฌ ๋ฒˆ ํ•˜๋˜ ๊ฒƒ์— ๋น„ํ•ด์„œ ์—ฐ์‚ฐ๋Ÿ‰์ด ๋งค์šฐ ๊ฐ์†Œํ–ˆ๊ณ  ์†๋„๋„ ๋นจ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. CNN fine tuning, boundnig box regression, classification์„ ๋ชจ๋‘ ํ•˜๋‚˜์˜ ๋„คํŠธ์›Œํฌ์—์„œ ํ•™์Šต์‹œํ‚ค๋Š” end-to-end ๊ธฐ๋ฒ•์„ ์ œ์‹œํ•˜์˜€์Šต๋‹ˆ๋‹ค. Pascal VOC 2007 ๋ฐ์ดํ„ฐ ์…‹์„ ๋Œ€์ƒ์œผ๋กœ mAP 66%๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์  R-CNN๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฅด๋‹ค๊ณ ๋Š” ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ ๋Š๋ฆฝ๋‹ˆ๋‹ค. ๊ณ„์‚ฐ๋Ÿ‰์„ ๋ณด์„ธ์š”. ๋น ๋ฅผ ์ˆ˜๊ฐ€ ์—†๊ฒ ์ฃ  Region proposal ์—๋งŒ 2์ดˆ๊ฐ€ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. Reference ๊ฐ์ฒด ๊ฒ€์ถœ(Object Detection) ๋”ฅ๋Ÿฌ๋‹ ๊ธฐ์ˆ : R-CNN, Fast R-CNN, Faster R-CNN ๋ฐœ์ „ ๊ณผ์ • ํ•ต์‹ฌ ์š”์•ฝ PR-012: Faster R-CNN : Towards Real-Time Object Detection with Region Proposal Networks Herbwood | Fast R-CNN ๋…ผ๋ฌธ ๋ฆฌ๋ทฐ ๊ฐˆ์•„๋จน๋Š” ๋จธ์‹ ๋Ÿฌ๋‹|R-CNN ์‹ค์ „ ํ…์„œ ํ”Œ๋กœ 2๋ฅผ ํ™œ์šฉํ•œ ๋”ฅ๋Ÿฌ๋‹-์ปดํ“จํ„ฐ ๋น„์ „ Deep Learning AI : C4W3L01~10 cs231n - Lecture 11 Detection and Segmentation 3) Faster R-CNN ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ Faster R-CNN์˜ ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š” Region Proposal Network์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด Fast R-CNN์˜ ๊ตฌ์กฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ค๋ฉด์„œ Selective Search๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  RPN์„ ํ†ตํ•ด์„œ RoI๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด RPN์€ Selective Search๊ฐ€ 2000๊ฐœ์˜ RoI๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ๋ฐ˜ํ•ด 800๊ฐœ ์ •๋„์˜ RoI๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด์„œ๋„ ๋” ๋†’์€ ์ •ํ™•๋„๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. ์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ pre-trained๋œ CNN ๋ชจ๋ธ์— ์ž…๋ ฅํ•˜์—ฌ feature map์ด ์ถ”์ถœ๋ฉ๋‹ˆ๋‹ค. feature map์€ RPN์— ์ „๋‹ฌ๋˜์–ด ์ ์ ˆํ•œ region proposals์„ ์‚ฐ์ถœํ•ฉ๋‹ˆ๋‹ค. Region proposals์™€ 1) ๊ณผ์ •์—์„œ ์–ป์€ feature map์„ ํ†ตํ•ด RoI pooling์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ feature map์„ ์–ป์Šต๋‹ˆ๋‹ค. Fast R-CNN ๋ชจ๋ธ์— ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ feature map์„ ์ž…๋ ฅํ•˜์—ฌ Classification๊ณผ Bounding box regression์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ ์‹ฌํ™” 1. feature extraction by pre-trained VGG-16 pre-trained๋œ VGG16 ๋ชจ๋ธ์— 800x800x3 ํฌ๊ธฐ์˜ ์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅํ•˜์—ฌ 50x50x512 ํฌ๊ธฐ์˜ feature map์„ ์–ป์Šต๋‹ˆ๋‹ค. 2. Generate Anchors by Anchor generation layer Region proposals๋ฅผ ์ถ”์ถœํ•˜๊ธฐ์— ์•ž์„œ ์›๋ณธ ์ด๋ฏธ์ง€์— ๋Œ€ํ•˜์—ฌ anchor box๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์›๋ณธ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์— sub-sampling ratio๋ฅผ ๊ณฑํ•œ ๋งŒํผ์˜ grid cell์ด ์ƒ์„ฑ๋˜๋ฉฐ, ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ grid cell๋งˆ๋‹ค 9๊ฐœ์˜ anchor box๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์›๋ณธ ์ด๋ฏธ์ง€์— 50x50(=800x1/16 x 800x1/16) ๊ฐœ์˜ grid cell์ด ์ƒ์„ฑ๋˜๊ณ , ๊ฐ grid cell๋งˆ๋‹ค 9๊ฐœ์˜ anchor box๋ฅผ ์ƒ์„ฑํ•˜๋ฏ€๋กœ ์ด 22500(=50x50x9) ๊ฐœ์˜ anchor box๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. 3. Class scores and Bounding box regressor by RPN RPN์€ VGG16์œผ๋กœ๋ถ€ํ„ฐ feature map์„ ์ž…๋ ฅ๋ฐ›์•„ anchor์— ๋Œ€ํ•œ class score, bounding box regressor๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. **RPN(Region Proposal Network)** RPN์€ ์›๋ณธ ์ด๋ฏธ์ง€์—์„œ region proposals๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋„คํŠธ์›Œํฌ์ž…๋‹ˆ๋‹ค. ์›๋ณธ ์ด๋ฏธ์ง€์—์„œ anchor box๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ์ˆ˜๋งŽ์€ region proposals๊ฐ€ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. RPN์€ region proposals์— ๋Œ€ํ•˜์—ฌ class score๋ฅผ ๋งค๊ธฐ๊ณ , bounding box coefficient๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•ฉ๋‹ˆ๋‹ค. 1) ์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ pre-trained๋œ VGG ๋ชจ๋ธ์— ์ž…๋ ฅํ•˜์—ฌ feature map์„ ์–ป์Šต๋‹ˆ๋‹ค. 2) ์œ„์—์„œ ์–ป์€ feature map์— ๋Œ€ํ•˜์—ฌ 3x3 conv ์—ฐ์‚ฐ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ feature map์˜ ํฌ๊ธฐ๊ฐ€ ์œ ์ง€๋  ์ˆ˜ ์žˆ๋„๋ก padding์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. 3) class score๋ฅผ ๋งค๊ธฐ๊ธฐ ์œ„ํ•ด์„œ feature map์— ๋Œ€ํ•˜์—ฌ 1x1 conv ์—ฐ์‚ฐ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ถœ๋ ฅํ•˜๋Š” feature map์˜ channel ์ˆ˜๊ฐ€ 2x9๊ฐ€ ๋˜๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. RPN์—์„œ๋Š” ํ›„๋ณด ์˜์—ญ์ด ์–ด๋–ค class์— ํ•ด๋‹นํ•˜๋Š”์ง€๊นŒ์ง€ ๊ตฌ์ฒด์ ์ธ ๋ถ„๋ฅ˜๋ฅผ ํ•˜์ง€ ์•Š๊ณ  ๊ฐ์ฒด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ๋งŒ์„ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ anchor box๋ฅผ ๊ฐ grid cell๋งˆ๋‹ค 9๊ฐœ๊ฐ€ ๋˜๋„๋ก ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ channel ์ˆ˜๋Š” 2(object ์—ฌ๋ถ€) x 9(anchor box 9๊ฐœ)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 4) bounding box regressor๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด feature map์— ๋Œ€ํ•˜์—ฌ 1x1 conv ์—ฐ์‚ฐ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ถœ๋ ฅํ•˜๋Š” feature map์˜ channel ์ˆ˜๊ฐ€ 4(bounding box regressor) x9(anchor box 9๊ฐœ)๊ฐ€ ๋˜๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. RPN์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ขŒ์ธก ํ‘œ๋Š” anchor box์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๊ฐ์ฒด ํฌํ•จ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ธ feature map์ด๋ฉฐ, ์šฐ์ธก ํ‘œ๋Š” anchor box์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ bounding box regressor๋ฅผ ๋‚˜ํƒ€๋‚ธ feature map์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด 8x8 grid cell๋งˆ๋‹ค 9๊ฐœ์˜ anchor box๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์ด 576(=8x8x9) ๊ฐœ์˜ region proposals๊ฐ€ ์ถ”์ถœ๋˜๋ฉฐ, feature map์„ ํ†ตํ•ด ๊ฐ๊ฐ์— ๋Œ€ํ•œ ๊ฐ์ฒด ํฌํ•จ ์—ฌ๋ถ€์™€ bounding box regressor๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ›„ class score์— ๋”ฐ๋ผ ์ƒ์œ„ N ๊ฐœ์˜ region proposals๋งŒ์„ ์ถ”์ถœํ•˜๊ณ , Non maximum suppression์„ ์ ์šฉํ•˜์—ฌ ์ตœ์ ์˜ region proposals๋งŒ์„ Fast R-CNN์— ์ „๋‹ฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 4. Region proposal by Proposal layer Proposal layer์—์„œ๋Š” 2) ๋ฒˆ ๊ณผ์ •์—์„œ ์ƒ์„ฑ๋œ anchor boxes์™€ RPN์—์„œ ๋ฐ˜ํ™˜ํ•œ class scores์™€ bounding box regressor๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ region proposals๋ฅผ ์ถ”์ถœํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € Non maximum suppression์„ ์ ์šฉํ•˜์—ฌ ๋ถ€์ ์ ˆํ•œ ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•œ ํ›„, class score ์ƒ์œ„ N ๊ฐœ์˜ anchor box๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ regression coefficients๋ฅผ anchor box์— ์ ์šฉํ•˜์—ฌ anchor box๊ฐ€ ๊ฐ์ฒด์˜ ์œ„์น˜๋ฅผ ๋” ์ž˜ detect ํ•˜๋„๋ก ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. 5. Select anchors for training RPN by Anchor target layer Anchor target layer์˜ ๋ชฉํ‘œ๋Š” RPN์ด ํ•™์Šตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” anchor๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋จผ์ € 2) ๋ฒˆ ๊ณผ์ •์—์„œ ์ƒ์„ฑํ•œ anchor box ์ค‘์—์„œ ์›๋ณธ ์ด๋ฏธ์ง€์˜ ๊ฒฝ๊ณ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋Š” anchor box๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ positive/negative ๋ฐ์ดํ„ฐ๋ฅผ sampling ํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ positive sample์€ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜๋Š” foreground, negative sample์€ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” background๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด anchor box ์ค‘์—์„œ 1) ground truth box์™€ ๊ฐ€์žฅ ํฐ IoU ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ 2) ground truth box์™€์˜ IoU ๊ฐ’์ด 0.7 ์ด์ƒ์ธ ๊ฒฝ์šฐ์— ํ•ด๋‹นํ•˜๋Š” box๋ฅผ positive sample๋กœ ์„ ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ground truth box์™€์˜ IoU ๊ฐ’์ด 0.3 ์ดํ•˜์ธ ๊ฒฝ์šฐ์—๋Š” negative sample๋กœ ์„ ์ •ํ•ฉ๋‹ˆ๋‹ค. IoU ๊ฐ’์ด 0.3~0.7์ธ anchor box๋Š” ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณผ์ •์„ ํ†ตํ•ด RPN์„ ํ•™์Šต์‹œํ‚ค๋Š”๋ฐ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ ์…‹์„ ๊ตฌ์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 6. Select anchors for training Fast R-CNN by Proposal Target layer Proposal target layer์˜ ๋ชฉํ‘œ๋Š” proposal layer์—์„œ ๋‚˜์˜จ region proposals ์ค‘์—์„œ Fast R-CNN ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์œ ์šฉํ•œ sample์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์„ ํƒ๋œ region proposals๋Š” 1) ๋ฒˆ ๊ณผ์ •์„ ํ†ตํ•ด ์ถœ๋ ฅ๋œ feature map์— RoI pooling์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋จผ์ € region proposals์™€ ground truth box์™€์˜ IoU๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ 0.5 ์ด์ƒ์ผ ๊ฒฝ์šฐ positive, 0.1~0.5 ์‚ฌ์ด์ผ ๊ฒฝ์šฐ negative sample๋กœ label ๋ฉ๋‹ˆ๋‹ค. 7. Max pooling by RoI pooling ์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ VGG16 ๋ชจ๋ธ์— ์ž…๋ ฅํ•˜์—ฌ ์–ป์€ feature map๊ณผ 6) ๊ณผ์ •์„ ํ†ตํ•ด ์–ป์€ sample์„ ์‚ฌ์šฉํ•˜์—ฌ RoI pooling์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ feature map์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. RoI pooling์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ Fast R-CNN ๋…ผ๋ฌธ ๋ฆฌ๋ทฐ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. 8. Train Fast R-CNN by Multi-task loss ๋‚˜๋จธ์ง€ ๊ณผ์ •์€ Fast R-CNN ๋ชจ๋ธ์˜ ๋™์ž‘ ์ˆœ์„œ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ๋ฐ›์€ feature map์„ fc layer์— ์ž…๋ ฅํ•˜์—ฌ 4096 ํฌ๊ธฐ์˜ feature vector๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ดํ›„ feature vector๋ฅผ Classifier์™€ Bounding box regressor์— ์ž…๋ ฅํ•˜์—ฌ (class์˜ ์ˆ˜๊ฐ€ K๋ผ๊ณ  ํ•  ๋•Œ) ๊ฐ๊ฐ (K+1), (K+1) x 4 ํฌ๊ธฐ์˜ feature vector๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ๋œ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Multi-task loss๋ฅผ ํ†ตํ•ด Fast R-CNN ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค. Loss Function RPN์€ ์•ž์„œ์„œ Classificaiton๊ณผ Bounding Box Regression์„ ์ˆ˜ํ–‰ํ•˜์˜€๋Š”๋ฐ, Loss Function์€ ์ด ๋‘ ๊ฐ€์ง€ Task์—์„œ ์–ป์€ Loss๋ฅผ ์—ฎ์€ ํ˜•ํƒœ๋ฅผ ์ทจํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ i๋Š” ํ•˜๋‚˜์˜ ์•ต์ปค๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. pi๋Š” classsification์„ ํ†ตํ•ด์„œ ์–ป์€ ํ•ด๋‹น ์—ฅ์ปค๊ฐ€ ์˜ค๋ธŒ์ ํŠธ์ผ ํ™•๋ฅ ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ti๋Š” bounding box regression์„ ํ†ตํ•ด์„œ ์–ป์€ ๋ฐ•์Šค ์กฐ์ • ๊ฐ’ ๋ฒกํ„ฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. pi์™€ ti๋Š” ground truth ๋ผ๋ฒจ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. Classification์€ log loss๋ฅผ ํ†ตํ•ด์„œ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. Regression loss์˜ ๊ฒฝ์šฐ Fast R-CNN์—์„œ ์†Œ๊ฐœ๋˜์—ˆ๋˜ smoothL1 ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Faster R-CNN์˜ ์žฅ์  ๊ทธ๋™์•ˆ Selective Search๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•ด์™”๋˜ Region Proposal ๋‹จ๊ณ„๋ฅผ Neural Network ์•ˆ์œผ๋กœ ๋Œ์–ด์™€์„œ ์ง„์ •ํ•œ ์˜๋ฏธ์˜ end-to-end object detection ๋ชจ๋ธ์„ ์ œ์‹œํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋‹จ๊ณ„๋ฅผ ๋‹ค ํ•ฉ์ณ์„œ 5fps๋ผ๋Š” ๋น ๋ฅธ ์†๋„๋ฅผ ๋‚ด๋ฉฐ Pascal VOC๋ฅผ ๊ธฐ์ค€์œผ๋กœ 78.8%๋ผ๋Š” ์„ฑ๋Šฅ์„ ๋ƒ…๋‹ˆ๋‹ค. Reference Faster R-CNN ๋…ผ๋ฌธ ์ฐธ์‹  ๋Ÿฌ๋‹ (Fresh-Learning) ๊ฐˆ์•„๋จน๋Š” ๋จธ์‹ ๋Ÿฌ๋‹ JinWon Lee ๋‹˜์˜ Faster R-CNN ๋ชจ๋ธ ์„ค๋ช… ์˜์ƒ ์•ฝ์ดˆ์˜ ์ˆฒ์œผ๋กœ ๋†€๋Ÿฌ ์˜ค์„ธ์š” Faster R-CNN ์„ค๋ช… 2) ์› ์Šคํ…Œ์ด์ง€ ๋ชจ๋ธ๋“ค .... 1) YOLO V1(โ˜…์ž‘์„ฑ ์ค‘) One-stage Detector์˜ ์‹œ์ž‘์„ ์—ฐ YOLO V1 ๋„คํŠธ์›Œํฌ์ž…๋‹ˆ๋‹ค. YOLO ๊ณ„์—ด์€ ๊ธ€์„ ์ž‘์„ฑํ•˜๊ณ  ์žˆ๋Š” ํ˜„์žฌ๊นŒ์ง€๋„ ๋Œ€ํ‘œ์ ์ธ One-Stage Detector๋กœ ์ž๋ฆฌ ์žก๊ณ  ์žˆ์œผ๋ฉฐ ๋ฒ„์ „์ด ์—…๋ฐ์ดํŠธ๋ ์ˆ˜๋ก ๋‹ค๋ฅธ ๋„คํŠธ์›Œํฌ์˜ ์žฅ์ ์„ ํก์ˆ˜, ๋ฐœ์ „ํ•˜์—ฌ ์„ฑ๋Šฅ์ด ๋น„์•ฝ์ ์œผ๋กœ ํ–ฅ์ƒ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์ฒด ๊ฒ€์ถœ ๋„คํŠธ์›Œํฌ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ํฐ ๋‘ ๊ฐ€์ง€ ์ง€ํ‘œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. mAP๋กœ ์ธก์ •ํ•˜๋Š” ์ •ํ™•๋„, FPS๋กœ ์ธก์ •ํ•˜๋Š” ์ˆ˜ํ–‰ ์‹œ๊ฐ„(inference time)์ž…๋‹ˆ๋‹ค. YOLO V1์ด ๋‚˜์™”์„ ๋‹น์‹œ์— ์ •ํ™•๋„(mAP)๋Š” Two-stage Detector์ธ Faster R-CNN๋ณด๋‹ค ๋ถ€์กฑํ–ˆ์ง€๋งŒ ์†๋„(FPS)์—์„œ๋Š” ๋งค์šฐ ํฐ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. YOLO ๋„คํŠธ์›Œํฌ์˜ ๊ตฌ์กฐ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์‚ดํŽด๋ณด๋ฉด์„œ ์–ด๋–ค ํฌ์ธํŠธ์—์„œ ์ด๋Ÿฌํ•œ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ–ˆ๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Network ๊ตฌ์กฐ ์ด 24๊ฐœ์˜ convolutional layers๊ณผ 2๊ฐœ์˜ fully connected layers์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต ์ธก๋ฉด์—์„œ ๋„คํŠธ์›Œํฌ๋ฅผ ํฐ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ„๋ฉด "Pretrain Network", "Training Network" 2๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Pretrained Network Pretrained Network๋Š” Pre-trained GoogleLeNet์„ Fine-Tuning ํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (๋ฌผ๋ก  Reduction Layer ๋“ฑ ์•ฝ๊ฐ„์˜ ์ฐจ์ด๋Š” ์žˆ์œผ๋‚˜, YOLO ๋„คํŠธ์›Œํฌ์—์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ ์•„๋‹ˆ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.) Pre-trained GoogleLeNet์€ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์— ์‚ฌ์šฉํ–ˆ๋˜ ๋„คํŠธ์›Œํฌ์—ฌ์„œ input image์— ๋Œ€ํ•œ ๊ณต๊ฐ„ ์ •๋ณด(Spatial Information)๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํŠน์„ฑ ๋•๋ถ„์— Object detection์„ ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Training Network Pretrained GoogleNet์€ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์— ์‚ฌ์šฉ๋˜์—ˆ๋˜ ๋ชจ๋ธ์ด๋ฏ€๋กœ Object detection์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ 4๊ฐœ์˜ Convolutional layer์™€ 2๊ฐœ์˜ Fully connected layer๋ฅผ ๋”ํ•ด์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ถ€๋ถ„์„ ๋”ฐ๋กœ ํ•™์Šตํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. training network๊นŒ์ง€ ๊ฑฐ์น˜๊ณ  ๋‚˜๋ฉด ์ตœ์ข…์ ์œผ๋กœ 7 x 7 x 30 ํฌ๊ธฐ์˜ Prediction tensors๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด ๋„คํŠธ์›Œํฌ์˜ Output ๊ฐ’์„ ๋ณด๋ฉด ์ด์ƒํ•˜๊ฒŒ ์ƒ๊ฐ๋  ๊ฒ๋‹ˆ๋‹ค.(์ œ๊ฐ€ ๊ทธ๋žฌ์Šต๋‹ˆ๋‹ค) ์™œ Pretrain+Training Newtork๋Š” 488X448X3 ํ•ด์ƒ๋„์˜ ์ด๋ฏธ์ง€๊ฐ€ ๋“ค์–ด์™”๋Š”๋ฐ 7X7X30 ๋งคํŠธ๋ฆญ์Šค๋กœ ๊ฐ์ฒด ๊ฒ€์ถœ์ด ๋งˆ๋ฌด๋ฆฌ๊ฐ€ ๋ ๊นŒ์š”? (์‚ฌ์‹ค ๋งˆ๋ฌด๋ฆฌ๋Š” ์•„๋‹ˆ๊ณ  ์ด ๋’ค์— Final detection ํ•˜๋Š” ๊ณผ์ •์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.) ํ•˜์ง€๋งŒ YOLO์—์„œ Training ๋„คํŠธ์›Œํฌ์˜ Output์ด ํŠธ๋ ˆ์ด๋‹๊ณผ ๋งค์šฐ ์ƒ๊ด€์ด ์žˆ๊ณ  YOLO์˜ ์ฒ ํ•™(?)์ด ๊ฐ€์žฅ ์ž˜ ๋…น์•„์žˆ๋Š” ๋ถ€๋ถ„์ด๋ฏ€๋กœ Training Network์˜ Output์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋ฆฌ๋ทฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Unified detection 7 X 7 X 30 ๋…ผ๋ฌธ์—์„œ๋Š” (448x448) ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€๋ฅผ CNN์„ ์ด์šฉํ•˜์—ฌ (7x7) ํฌ๊ธฐ์˜ feature map์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ Pretrain Network์—์„œ CNN์„ ์ด์šฉํ–ˆ์œผ๋ฏ€๋กœ ๊ณต๊ฐ„ ์ •๋ณด(Spatial Information)๋Š” ๋ณด์กด๋˜๊ณ , ์ด๊ฒƒ์€ ๊ทธ๋ฆฌ๋“œ๋ฅผ ๋‚˜๋ˆˆ ๊ฒƒ๊ณผ ๊ฐ™์ด ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (7x7) ๋งคํŠธ๋ฆญ์Šค์˜ (1,1) ๋ฒˆ์งธ์˜ ํ–‰๋ ฌ ๊ฐ’์€ ์ธํ’‹ ์ด๋ฏธ์ง€์˜ (0~64, 0~64) ๋ฒ”์œ„์˜ ํ”ฝ์…€์—์„œ ํ”ผ์ฒ˜๊ฐ€ ์ถ”์ถœ๋œ ๊ฒƒ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Training Network๋Š” ์ด๋ ‡๊ฒŒ ๊ตฌ์—ญ(Grid)๋งˆ๋‹ค ๊ฐ์ฒด ํƒ์ง€๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. 7 X 7 X 30 YOLO ์ด์ „์˜ Two-stage detectors๋Š” ๊ฐ์ฒด์˜ class์™€ bounding box์˜ ์ขŒํ‘œ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋‚ด๋ณด๋ƒˆ์Šต๋‹ˆ๋‹ค. {(๋น„ํ–‰๊ธฐ, 100, 200, 30, 40)} ์ด๊ฒƒ์ฒ˜๋Ÿผ์š”. YOLO๋Š” ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ์— class์™€ bounding box์˜ ์ขŒํ‘œ, ๊ทธ๋ฆฌ๊ณ  ์˜ˆ์ธก์— ๋Œ€ํ•œ confidence score ๊ฐ’์„ ํ•œ ๋ฒˆ์— ๋‚ด๋ณด๋ƒ…๋‹ˆ๋‹ค. ๊ตฌ์—ญ(Grid)๋งˆ๋‹ค 30์ฐจ์›์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” ๊ฐ Grid๋งˆ๋‹ค ์„œ๋กœ ๋‹ค๋ฅธ ์‚ฌ์ด์ฆˆ์˜ 2๊ฐœ์˜ bounding box๋ฅผ ์ด์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ, 1~5์ฐจ์›์˜ ๊ฐ’์€ ํ•ด๋‹น Grid์—์„œ ์ฒซ ๋ฒˆ์งธ bounding box(BB)์—์„œ ํƒ์ง€๊ฐ€ ๋œ ๊ฐ์ฒด์˜ bound box์— ๋Œ€ํ•œ ์ •๋ณด๋กœ ์ฑ„์›Œ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. [๊ฐ์ฒด๊ฐ€ ํƒ์ง€๋œ BB์˜ x์ขŒํ‘œ ์ค‘์‹ฌ, ๊ฐ์ฒด๊ฐ€ ํƒ์ง€๋œ BB์˜ y์ขŒํ‘œ ์ค‘์‹ฌ, BB์˜ ๋„ˆ๋น„, BB์˜ ๋†’์ด, BB ํ™•๋ฅ ]์™€ ๊ฐ™์ด ์ •๋ณด๋ฅผ ์ฑ„์šฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ, 6~10์ฐจ์›์˜ ๊ฐ’์€ ์ฒซ ๋ฒˆ์งธ์™€ ๋™์ผํ•œ ์ˆœ์„œ๋กœ ๋‘ ๋ฒˆ์งธ BB์—์„œ ํƒ์ง€๋œ ๊ฐ์ฒด์˜ ์ •๋ณด๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ, 11~30์ฐจ์›์˜ ๊ฐ’์€ ์ฒซ ๋ฒˆ์งธ BB์—์„œ ํƒ์ง€๋œ ๊ฐ์ฒด์˜ 20๊ฐœ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ํ™•๋ฅ  ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์นฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ ์‚ฌ์šฉ๋œ ๋ฐ์ดํ„ฐ ์…‹์ด 20๊ฐœ์˜ ํด๋ž˜์Šค๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ์˜€์œผ๋ฏ€๋กœ 20๊ฐœ์˜ ์ฐจ์›์„ ๊ฐ–์Šต๋‹ˆ๋‹ค. Detection Procedure ์ด๋ ‡๊ฒŒ ๋‚˜์˜จ Training output matrix๋ฅผ ์ตœ์ข…์ ์œผ๋กœ prediction ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ๋งˆ์ง€๋ง‰ ์ ˆ์ฐจ์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋‚˜์˜จ ๊ฐ๊ฐ์˜ ์ฐจ์› ๊ฐ’์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด BB์˜ confidence score์™€ class์˜ probability๋ฅผ ๊ณฑํ•˜์—ฌ BB๋“ค์˜ Class specific confidence score๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” 7X7๊ฐœ์˜ Grid ๋‹น 2๊ฐœ์˜ BB๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์—ˆ์œผ๋ฏ€๋กœ ์ „์ฒด Class specific confidence score ๊ฐœ์ˆ˜๋Š” 7X7X2 = 98๊ฐœ์˜ bounding box์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ 98๊ฐœ์˜ bb์˜ ์ •๋ณด๋“ค์— ๋Œ€ํ•ด์„œ NMS๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ํƒ์ง€๋œ ๊ฐ์ฒด์˜ prediction ๊ฒฐ๊ณผ๋งŒ์„ ์–ป์–ด๋ƒ…๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ํŠธ๋ ˆ์ด๋‹ ๋„คํŠธ์›Œํฌ ์„ฑ๋Šฅ ๋„คํŠธ์›Œํฌ ์žฅ๋‹จ์  Reference CURG ๋ธ”๋กœ๊ทธ 2) SSD (Single Shot Multibox Detector) Yolo๋Š” ์ •ํ™•๋„ ์ธก๋ฉด์—์„  ๋‹ค์†Œ ์ œ์•ฝ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ž‘์€ ๋ฌผ์ฒด๋“ค์€ ์ž˜ ์žก์•„๋‚ด์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. SSD๋Š” ์ด๋Ÿฌํ•œ ํ•œ๊ณ„์ ์„ ๊ทน๋ณตํ•˜๊ณ ์ž ํ•˜๋Š” ์‹œ๋„์—์„œ ์ถœ๋ฐœํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. SSD๋Š” ๊ธฐ์กด R-CNN, YOLO์˜ ๊ตฌ์กฐ๋ฅผ ๊ต๋ฌ˜ํ•˜๊ฒŒ ์งœ๊น๊ธฐํ•˜์—ฌ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „ ๋ชจ๋ธ๊ณผ ๋‹ค๋ฅธ, SSD ํŠน์ง•์ ์€ Multi Scale Feature Maps for Detection, Default Boxes Generation์ด๋ผ๊ณ  ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐ€์ง€ ์š”์†Œ์— ์ง‘์ค‘ํ•˜์—ฌ ์‚ดํŽด ๋ณด์‹œ๋Š” ๊ฒŒ ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. Network ๊ตฌ์กฐ 0. Default box generation ํŠน์ •ํ•œ ๊ฐ€๋กœ, ์„ธ๋กœ ๋น„๋ฅผ ๊ฐ–๋Š” default box k ๊ฐœ๋ฅผ ๋ฏธ๋ฆฌ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. Default box๋Š” YOLO์˜ Anchor box์™€ ๋น„์Šทํ•œ ๊ฐœ๋…์œผ๋กœ bounding box๊ฐ€ ๋  ํ›„๋ณด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. feature map ์œ„์— default box๋ฅผ ๊ทธ๋ฆฐ ๋’ค Image Classification & Default Boxes Prediction์„ ์ง„ํ–‰ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. 1. Image Input 300x300 ํฌ๊ธฐ์˜ input image๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. YOLO๋Š” 448x448 ํฌ๊ธฐ์˜ input image๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. YOLO๋ณด๋‹ค ์ € ํ•ด์ƒ๋„ ๋ฐ์ดํ„ฐ์—์„œ๋„ ์ž˜ ์ž‘๋™ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. CNN Feature extraction pretrained๋œ VGG-16์˜ Conv5_3์ธต๊นŒ์ง€ ํ†ต๊ณผํ•˜๋ฉฐ Feature๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค 3. Image Classification & Default Boxes Prediction convolution layer 4_3์—์„œ ์ถ”์ถœํ•œ Featuer map์„ ์ด์šฉํ•ด default box์˜ ์œ„์น˜ ์˜ˆ์ธก๊ณผ classification์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ Default box๋Š” YOLO์˜ Anchor box์™€ ๋น„์Šทํ•œ ๊ฐœ๋…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. default box์˜ ์ˆ˜๋ฅผ k, ์˜ˆ์ธกํ•˜๋ ค๋Š” class์˜ ์ˆ˜๋ฅผ c๋ผ๊ณ  ํ•  ๋•Œ, output feature map์˜ channel ์ˆ˜๋Š” k x (C+4)๊ฐ€ ๋˜๋„๋ก ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. (4๋Š” default box์˜ x, y, w, h์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. Yolo์™€ ๋น„์Šทํ•˜์ฃ ?) SSD ๋ชจ๋ธ์€ ์˜ˆ์ธกํ•˜๋ ค๋Š” class์˜ ์ˆ˜๊ฐ€ 20๊ฐœ์ด๊ณ , ๋ฐฐ๊ฒฝ๋„ class์— ํฌํ•จํ•˜๊ธฐ์— c=21์ž…๋‹ˆ๋‹ค. 4. Multi Scale Feature Maps for Detection convolution layer 7, 8_2, 9_2, 10_2, 11_2์—์„œ ์ถ”์ถœํ•œ Featuer map์— ๋Œ€ํ•ด์„œ๋„ default box์˜ ์œ„์น˜ ์˜ˆ์ธก๊ณผ classification์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. convolution layer 4_3, 7, 8_2, 9_2, 10_2, 11_2๋ผ๋Š” ์ด 6๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ scale์˜ feature map์„ ์˜ˆ์ธก์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. YOLO V1๊ณผ ๋„คํŠธ์›Œํฌ ๋””์ž์ธ์„ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SSD๋Š” ๋‹ค์–‘ํ•œ ์‚ฌ์ด์ฆˆ์˜ Feature map 38X38, 19X19, 10X10, 5X5, 1X1์—์„œ ๊ฒ€์ถœ์„ ํ•˜๋Š” ๋ฐ˜๋ฉด์— YOLO V1์—์„œ๋Š” ๋‹จ ํ•˜๋‚˜ ์‚ฌ์ด์ฆˆ์˜ Feature map 7X7์—์„œ๋งŒ ๊ฐ์ฒด ๊ฒ€์ถœ์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 5. NMS(Non-maximum suppression) ์—ฌ๋Ÿฌ feature map์—์„œ ์ƒ์„ฑ๋œ default box์— ๋Œ€ํ•ด NMS๋ฅผ ์‹œํ–‰ํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค Default Boxes Generation SSD์—์„œ๋Š” ํŠน์ •ํ•œ ๊ฐ€๋กœ, ์„ธ๋กœ ๋น„๋ฅผ ๊ฐ–๋Š” default box k ๊ฐœ๋ฅผ ๋ฏธ๋ฆฌ ์„ค์ •ํ•˜๊ณ  ๊ฐ grid cell ๋ณ„๋กœ default box๋ฅผ ๊ทธ๋ ค๋ณธ ๋‹ค์Œ Image Classification & Default Boxes Prediction์„ ์ง„ํ–‰ํ•œ๋‹ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋Š” convolution layer 4_3, 7, 8_2, 9_2, 10_2, 11_2์—์„œ ๋ฝ‘์•„๋‚ธ ์ด 6๊ฐœ์˜ Feature map์ด ์กด์žฌํ•˜๋Š”๋ฐ, ๊ฐ feature map์€ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ grid cell๋„ ํฌ๊ธฐ๋„ ๋‹ฌ๋ผ์ง„๋‹ค ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 6๊ฐœ์˜ featuer map๋งˆ๋‹ค input image ๋Œ€๋น„ default box์˜ ํฌ๊ธฐ ๋น„์œจ Sk์„ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์›๋ณธ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๊ฐ€ 300x300์ด๊ณ  default box width:height๊ฐ€ 1:1์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ ์•ž์ชฝ Layer์˜ s = 0.9๋กœ ์ •ํ•˜์—ฌ default box์˜ ํฌ๊ธฐ๋Š” 30x30 (=300x0.1 x 300 x 0.9)๊ฐ€ ๋˜๊ณ , ๋’ค์ชฝ Layer์˜ s = 0.1๋กœ ์ •ํ•˜์—ฌ default box์˜ ํฌ๊ธฐ๋Š” 30x30 (=300x0.1 x 300 x 0.1)๊ฐ€ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. (์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋œ๋‹ค๋ฉด TimeTraveler | SSD ์ด ๊ธ€์„ ์ฐธ๊ณ ํ•˜์„ธ์š”) Sk์˜ ์ˆ˜์‹์  ํ‘œํ˜„ Feature Map์˜ ๊ฐœ์ˆ˜๋Š” m๊ฐœ ์ด๊ณ , ์ž„์˜์˜ Smin๊ณผ Smax๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. Sk๋Š” Smin๊ณผ Smax ์‚ฌ์ด๋ฅผ m-1๊ฐœ์˜ ๊ตฌ๊ฐ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋“ฑ๊ฐ„๊ฒฉ์˜ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์› ๋…ผ๋ฌธ์—์„œ๋Š” m=6์ด๋ผ๊ณ  ํ–ˆ๊ณ , ์ฒซ ๋ฒˆ์งธ Feature Map์—์„  Input image ํฌ๊ธฐ ๋Œ€๋น„ 0.2 ๋น„์œจ์„ ๊ฐ€์ง„ ์ž‘์€ default box๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด s_min์€ 0.2, 6 ๋ฒˆ์งธ Feature Map์—์„œ๋Š” Input image ํฌ๊ธฐ ๋Œ€๋น„ 0.9 ๋น„์œจ์„ ๊ฐ€์ง„ ํฐ default box๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด s_max = 0.9, ๋กœ ์„ค์ •ํ–ˆ์œผ๋ฉฐ ์ตœ์ข… Sk๋Š” [0.2, 0.34, 0.48, 0.62, 0.76, 0.9]๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. Default box์˜ width/height ๋น„์œจ ์„ค์ • input image ๋Œ€๋น„ default box์˜ ํฌ๊ธฐ ๋น„์œจ Sk์„ ์ •ํ–ˆ๋‹ค๋ฉด ์ด์ œ๋Š” default box ์ž์ฒด์˜ ๊ฐ€๋กœ, ์„ธ๋กœ ๋น„๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ๋‹ค์†Œ ๋ณต์žกํ•˜๊ฒŒ ์ •์˜๊ฐ€ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ a_r = 2๋ผ๋ฉด ๊ฐ€๋กœ : ์„ธ๋กœ ๋น„๋Š” 2:1์ด ๋˜๊ณ , a_r = 3์ด๋ผ๋ฉด ๊ฐ€๋กœ : ์„ธ๋กœ ๋น„๋Š” 3:1์ด ๋ฉ๋‹ˆ๋‹ค. SSD Network ๊ตฌ์กฐ๋ฅผ ๋ณด์‹œ๋ฉด default box ๊ฐœ์ˆ˜๊ฐ€ 4๊ฐœ์ธ layer๋„ ์žˆ๊ณ  6๊ฐœ์ธ layer๋„ ์žˆ์Šต๋‹ˆ๋‹ค. (๊ฐœ์ˆ˜๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•œ ์ด์œ ์— ๋Œ€ํ•ด ์› ๋…ผ๋ฌธ์—์„œ๋Š” ๊ทธ๋ƒฅ ๊ฒฝํ—˜์ ์œผ๋กœ ํ•ด๋ดค๋”๋‹ˆ ๋” ์„ฑ๋Šฅ์ด ์ข‹๋”๋ผ~๋ผ๊ณ  ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.) Default box 4๊ฐœ์ธ ๊ฒฝ์šฐ - a = 1, 2, 1/2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋กœ : ์„ธ๋กœ ๋น„๊ฐ€ 1:1, 2:1, 1:2์ธ default box 3๊ฐœ๋ฅผ ๋งŒ๋“ค๊ณ  - ์ถ”๊ฐ€์ ์œผ๋กœ S'k & a =1 ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฐ€๋กœ : ์„ธ๋กœ ๋น„๊ฐ€ 1:1์ธ default box 1๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Default box 6๊ฐœ์ธ ๊ฒฝ์šฐ - a = 1, 2, 3, 1/2, 1/3๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋กœ : ์„ธ๋กœ ๋น„๊ฐ€ 1:1, 2:1, 3:1, 1:2, 1:3์ธ default box 5๊ฐœ๋ฅผ ๋งŒ๋“ค๊ณ  - ์ถ”๊ฐ€์ ์œผ๋กœ S'k & a =1 ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฐ€๋กœ : ์„ธ๋กœ ๋น„๊ฐ€ 1:1์ธ default box 1๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Multi Scale Feature Maps for Detection Yolo๋Š” Input image๋ฅผ 7x7 ํฌ๊ธฐ์˜ featuer map์— ๋Œ€ํ•ด์„œ Object Detection์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 1๊ฐœ์˜ Bounding box๊ฐ€ 1๊ฐœ์˜ ๊ฐ์ฒด๋งŒ์„ ์žก์•„๋‚ผ ์ˆ˜ ์žˆ๊ณ , 1๊ฐœ์˜ Bounding box ์•ˆ์— ์ž‘์€ ๊ฐ์ฒด๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์žก์•„๋‚ด์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. SSD๋Š” convolution layer๋ฅผ ํ†ต๊ณผํ•˜๋ฉด์„œ ์ƒ์„ฑ๋œ ๋‹ค์ˆ˜์˜ feature map์—์„œ ๋ชจ๋‘ Object Detection์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์•ž์ชฝ layer์—์„œ๋Š” ์ž‘์€ ๊ฐ์ฒด๋ฅผ ๊ฒ€์ถœํ•˜๊ณ , ๋’ค์ชฝ layer์—์„œ๋Š” ํฐ ๊ฐ์ฒด๋ฅผ ๊ฒ€์ถœํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ YOLO์˜ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ Multi Scale Feature Maps for Detection์„ ์‹œํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ layer์—์„œ object detection์„ ์‹œํ–‰ํ•˜๋ฏ€๋กœ ์ •ํ™•๋„๊ฐ€ ์˜ฌ๋ผ๊ฐ‘๋‹ˆ๋‹ค. 8x8 feature map์—์„œ detection์„ ์‹œํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ์™€ 4x4 feature map์—์„œ detection์„ ์‹œํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์˜ˆ์‹œ๋กœ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŠน์ •ํ•œ ๊ฐ€๋กœ, ์„ธ๋กœ ๋น„๋ฅผ ๊ฐ–๋Š” default box k ๊ฐœ๋ฅผ ๋ฏธ๋ฆฌ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. (default box๋Š” R-CNN์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ Anchor box์™€ ๋น„์Šทํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.) CNN์˜ ํŠน์„ฑ์ƒ ์•ž์ชฝ Layer๋Š” grid cell ํฌ๊ธฐ๊ฐ€ ์ž‘๊ณ , ๋’ค์ชฝ layer๋Š” grid cell ํฌ๊ธฐ๊ฐ€ ํฝ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 8x8 feature map์˜ default box๋Š” ์ž‘์€ ๊ฐ์ฒด (์œ„ ๊ทธ๋ฆผ์—์„œ ๊ณ ์–‘์ด)๋ฅผ ๊ฒ€์ถœํ•˜๊ณ , 4x4 feature map์˜ default box๋Š” ํฐ ๊ฐ์ฒด (์œ„ ๊ทธ๋ฆผ์—์„œ ๊ฐ•์•„์ง€)๋ฅผ ๊ฒ€์ถœํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Select candidate default boxes by Hard Negative Mining ์„œ๋กœ ๋‹ค๋ฅธ scale์˜ default box๋ฅผ ๊ฐ Feature map์— ์ ์šฉํ•œ ๊ฒฝ์šฐ ์ด default box์˜ ์ˆ˜๋Š” 8732 (=38x38x4 + 19x19x6 + 10x10x6 + 5x5x6 + 3x3x6 + 1x1x4) ๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋“  default box๋ฅผ ํ•™์Šต์‹œํ‚ค๊ธฐ์—๋Š” ํšจ์œจ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. ์ด ํšจ์œจ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ์ €์ž๋“ค์€ Hard Negative Mining์ด๋ผ๋Š” ๊ธฐ๋ฒ•์„ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. Hard Negative Mining์€ ์‚ฌ์‹ค ์šฐ๋ฆฌ์—๊ฒŒ ๊ต‰์žฅํžˆ ์นœ์ˆ™ํ•œ ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๊ฐ๊ด€์‹ 5์ง€ ์„ ๋‹ค์—์„œ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋ฉด ์–ด๋–ป๊ฒŒ ํ•˜๋‚˜์š”? 1๋ฒˆ์ด ๊ฐ€์žฅ ์ •๋‹ต์ธ ๊ฒƒ ๊ฐ™๊ณ  2,3๋ฒˆ์€ ์•Œ์ญ๋‹ฌ์ญํ•˜๊ณ  4,5๋ฒˆ์€ ํ™•์‹คํžˆ ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ํ•˜๋ฉด 4,5๋ฒˆ์€<NAME>๊ณ  1,2,3๋ฒˆ ์ค‘์— ๊ณ ๋ฏผํ•˜์ฃ . Hard Negative Mining์€ ๋ฐ”๋กœ ๊ทธ ๊ธฐ๋ฒ•์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  default box ์•ˆ์— ๊ฐ์ฒด๊ฐ€ ์žˆ๋‹ค๊ณ  ํŒ๋‹จํ•œ default box๋ฅผ ์ถ”๋ฆฝ๋‹ˆ๋‹ค. (์› ๋…ผ๋ฌธ์—์„œ๋Š” ground truth์™€ IoU 0.5 ์ด์ƒ์ธ box๋ฅผ positive๋กœ, IoU 0.5 ๋ฏธ๋งŒ์ด๋ฉด negative๋กœ label ํ•ฉ๋‹ˆ๋‹ค.) ์œ„์˜ ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด ๋Œ€๋ถ€๋ถ„์˜ default box๋“ค์€ negative๋กœ label ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. (๋ณดํ†ต ์ด๋ฏธ์ง€์—์„œ ๊ฐ์ฒด๋ณด๋‹ค๋Š” ๋ฐฐ๊ฒฝ์— ํ•ด๋‹น๋˜๋Š” ์˜์—ญ์ด ๋งŽ์œผ๋‹ˆ๊นŒ์š”) ์ด๋•Œ ๋ชจ๋“  negative labeled data๋ฅผ ํ•™์Šต์— ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด positive์™€ negative ํ•™์Šต ๋ฐ์ดํ„ฐ์˜ Class imbalance ๋ฌธ์ œ๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Hard Negative Mining์„ ํ†ตํ•ด ๋ชจ๋“  negative ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šต์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ confidence loss๊ฐ€ ๋†’์€ negative ๋ฐ์ดํ„ฐ๋งŒ์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฆ‰, negative๋ผ๊ณ  label ํ•˜๊ธด ํ–ˆ์ง€๋งŒ ์•Œ์ญ๋‹ฌ์ญํ•˜๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋˜ default box๋“ค์„ ์ถ”๋ฆฐ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. negative label default box : positive label default box = 3:1 ๋น„์œจ์„ ์œ ์ง€ํ•˜์—ฌ ํ•™์Šต์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (์ €์ž๋“ค์€ ์‹คํ—˜์ ์œผ๋กœ ์ด ๋น„์œจ์ด ์ œ์ผ optimal ํ•˜๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.) Loss function ์ „์ฒด ์†์‹ค ํ•จ์ˆ˜๋Š” localization loss (loc)์™€ confidence loss (conf)์˜ ํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ( , , , ) 1 ( c n ( , ) ฮฑ l c ( , , ) ) N : Ground truth box์™€ ๋งค์นญ๋œ default box์˜ ๊ฐœ์ˆ˜, N=0์ด๋ผ๋ฉด Loss๊ฐ€ 0์ด ๋ฉ๋‹ˆ๋‹ค. l : ์˜ˆ์ธกํ•œ box g : gt box c : confidence score(๋ฌผ์ฒด์ผ ํ™•๋ฅ ) ฮฑ : weight term, ๋””ํดํŠธ ๊ฐ’์œผ๋กœ ฮฑ = 1์„ ์‚ฌ์šฉ c n ( , ) โˆ’ i P s x j l g ( i ^ ) โˆ‘ โˆˆ e l g ( i ^ ) w e e c p = x ( i) p x ( i) l c ( , , ) โˆ‘ โˆˆ o N m c, y w h i k m o h 1 ( i โˆ’ j ^ ) localization loss (loc)๋Š” l๊ณผ g์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ€์ง€๋Š” Smooth L1 loss function์ž…๋‹ˆ๋‹ค. l: ์˜ˆ์ธกํ•œ box์˜ (x, y, w, h) ์ขŒํ‘œ g: ground truth box์˜ (x, y, w, h) ์ขŒํ‘œ x_k_ij๋Š” i ๋ฒˆ์งธ default box์™€ class๊ฐ€ k์ธ j ๋ฒˆ์งธ ground truth box์™€์˜ ๋งค์นญ ์—ฌ๋ถ€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” indicator parameter๋กœ, ๋งค์นญ๋  ๊ฒฝ์šฐ 1, ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ 0 ์žฅ์  YOLO V1๋ณด๋‹ค๋Š” FPS๋Š” ์†Œํญ ์ƒ์Šนํ–ˆ์Šต๋‹ˆ๋‹ค๋งŒ mAP๋Š” ๋งค์šฐ ํฌ๊ฒŒ ์ƒ์Šนํ–ˆ์Šต๋‹ˆ๋‹ค. end-to-end ํ•™์Šต์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๊ตฌ์ถ•ํ–ˆ์œผ๋ฉฐ ์ € ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€์—์„œ๋„ ๋†’์€ ์ •ํ™•๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. YOLO์™€ ๋‹ฌ๋ฆฌ Fully Convolution Network์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์–ป๋Š” ์žฅ์ ์„ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. Fully Convolution Network๋ฅผ ํ†ต๊ณผํ•˜๋ฉด์„œ ๋””ํ…Œ์ผํ•œ ์ •๋ณด๋“ค์ด ์‚ฌ๋ผ์ง€๋Š” ๋ฌธ์ œ์ ์ด ์žˆ์—ˆ๋Š”๋ฐ ์ด๋ฅผ ํ•ด์†Œํ•˜์˜€์Šต๋‹ˆ๋‹ค. Parameter ๊ฐœ์ˆ˜๊ฐ€ ๊ธ‰๊ฒฉํžˆ ๊ฐ์†Œํ•˜์—ฌ ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ๋งค์šฐ ๋นจ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. ๋‹จ์  YOLO๋ณด๋‹ค ๊ฐœ์„ ๋˜์—ˆ๋‹ค๊ณ ๋Š” ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ ์ž‘์€ ํฌ๊ธฐ์˜ ๋ฌผ์ฒด๋ฅผ ์ž˜ ์ฐพ์•„๋‚ด์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ์ž‘์€ ๋ฌผ์ฒด๋Š” ์•ž์ชฝ layer์—์„œ ์ƒ์„ฑ๋œ Feature map์„ ์ด์šฉํ•˜์—ฌ object detection์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์•ž์ชฝ layer๋ฅผ ์ด์šฉํ•˜์—ฌ object detection์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์€ Depth๊ฐ€ ์ถฉ๋ถ„ํžˆ ๊นŠ์ง€ ์•Š์€ CNN ๋ชจ๋ธ๋กœ Object detection์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ์ €์ž๋“ค์€ Data Augmentation์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๊ธฐ์กด input image๋ฅผ ์ถ•์†Œํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์—ฌ๋ฐฑ์—๋Š” input image์˜ ํ‰๊ท ๊ฐ’์œผ๋กœ ์ฑ„์›Œ ๋„ฃ์€ ์ƒˆ๋กœ์šด data๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐ์ดํ„ฐ๋“ค๋กœ ํ•™์Šต์„ ์‹œํ‚ค๋‹ˆ ์„ฑ๋Šฅ ๊ฐœ์„ ์ด ๋‚˜ํƒ€๋‚ฌ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Reference Youtube SSD: Single Shot MultiBox Detector ๋ธ”๋กœ๊ทธ TimeTraveler | SSD ๊ฐˆ์•„๋จน๋Š” ๋จธ์‹ ๋Ÿฌ๋‹ | SSD herbwoord | SSD ํ‰๋ฒ”ํ•œ ๊ฐœ๋ฐœ์ผ์ง€ | SSD: Single Shot MultiBox Detector 3) DETR (Detection with Transformer) ์ง€๊ธˆ๊นŒ์ง€ object detection ๋ชจ๋ธ์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด์„œ Region proposal, Anchor box, NMS ๋“ฑ๋“ฑ ์ƒˆ๋กœ์šด ๊ฐœ๋…๋“ค์ด ๋งŽ์ด ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฐœ๋…๋“ค์€ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šด ๊ฒƒ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‹ค์ œ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์–ด๋ ต๊ณ  ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง„๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 2020๋…„ Facebook ํŒ€์€ ์ด๋Ÿฐ ๊ฐœ๋…๋“ค์„ ์ ์šฉํ•˜์ง€ ์•Š์•„ ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ๊ฐ€ ๋‹จ์ˆœํ•˜๋ฉด์„œ end-to-end ํ•™์Šต์ด ๊ฐ€๋Šฅํ•œ DETR (End-to-end object detection with transformers)์„ ๊ณต๊ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์˜ abstract๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด๋‹น ๋ชจ๋ธ์„ ์„ค๋ช…ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌด์Šจ ๋ง์ธ์ง€ ์ฐจ๊ทผ์ฐจ๊ทผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. "DETR are a set-based global loss that forces unique predictions via bi-partite matching and a transformer encoder-decoder architecture." ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ DETR์€ CNN Backbone + Transformer + FFN (Feed Forward Network)๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋งค์šฐ ๊ฐ„๋‹จํ•ด์„œ PyTorch๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋„ ๊ต‰์žฅํžˆ ์งง๊ณ  ๊น”๋”ํ•ฉ๋‹ˆ๋‹ค. CNN Backbone input image๋ฅผ CNN Backbone์„ ํ†ต๊ณผ์‹œ์ผœ feature map์„ ๋ฝ‘์•„๋ƒ…๋‹ˆ๋‹ค. ์ด feature map์ด transformer์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. โ“ input image ํฌ๊ธฐ๋Š” H_0 x W_0 โ“‘ CNN์„ ํ†ต๊ณผํ•˜์—ฌ ์ถœ๋ ฅ๋œ feature map์€ C ร— H ร— W (ResNet50์„ ์‚ฌ์šฉํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— C=2048, H = H_0/32, W = W_0/32) โ“’ 1x1 convolution์„ ์ ์šฉํ•˜์—ฌ d ร— H ร— W ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟˆ (C>d) โ““ transformer์— ๋“ค์–ด๊ฐ€๊ธฐ ์œ„ํ•ด์„œ๋Š” 2์ฐจ์›์ด์–ด์•ผ ํ•˜๋ฏ€๋กœ, d ร— H ร— W์˜ 3์ฐจ์›์—์„œ d ร— HW์˜ 2์ฐจ์›์œผ๋กœ ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊ฟˆ. Transformer Encoder (ํŒŒ๋ž€์ƒ‰ ๋ฐ•์Šค) d ร— HW์˜ feature matrix์— Positional encoding ์ •๋ณด๋ฅผ ๋”ํ•œ matrix๋ฅผ multi-head self-attention์— ํ†ต๊ณผ์‹œํ‚ต๋‹ˆ๋‹ค. Transformer์˜ ํŠน์„ฑ์ƒ ์ž…๋ ฅ matrix์™€ ์ถœ๋ ฅ matrix์˜ ํฌ๊ธฐ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. Decoder (๋ถ„ํ™์ƒ‰ ๋ฐ•์Šค) N ๊ฐœ์˜ bounding box์— ๋Œ€ํ•ด N ๊ฐœ์˜ object query๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ดˆ๊ธฐ object query๋Š” 0์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. (๋ณด๋ผ์ƒ‰ ๋ฐ•์Šค) Decoder๋Š” ์•ž์„œ ์„ค๋ช…ํ•œ N ๊ฐœ์˜ object query๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ multi-head self-attention์„ ๊ฑฐ์ณ ๊ฐ€๊ณต๋œ N ๊ฐœ์˜ unit์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (๋…ธ๋ž€์ƒ‰ ๋ฐ•์Šค) ์ด N ๊ฐœ์˜ unit๋“ค์ด Query๋กœ, ๊ทธ๋ฆฌ๊ณ  encoder์˜ ์ถœ๋ ฅ unit๋“ค์ด Key์™€ Value๋กœ ์ž‘๋™ํ•˜์—ฌ encoder-decoder multi-head attention์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. (์ดˆ๋ก์ƒ‰ ๋ฐ•์Šค) ์ตœ์ข…์ ์œผ๋กœ N ๊ฐœ์˜ unit๋“ค์€ ๊ฐ๊ฐ FFN์„ ๊ฑฐ์ณ object class์™€ box ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. FFN (Feed Forward Network) Transformer์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ N ๊ฐœ์˜ unit์€ FFN์„ ํ†ต๊ณผํ•˜์—ฌ class์™€ bounding box์˜ ํฌ๊ธฐ์™€ ์œ„์น˜๋ฅผ ๋™์‹œ์— ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ bi-partite matching์„ ํ†ตํ•ด ๊ฐ bounding box๊ฐ€ ๊ฒน์น˜์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. (์ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ loss function & Training์—์„œ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.) Original Transformer์™€ ๋ฌด์—‡์ด ๋‹ค๋ฅธ๊ฐ€? Original Transformer("Attention is All You Need")์™€ DETR์˜ transformer ๊ตฌ์กฐ๋Š” ๋น„์Šทํ•˜๋ฉด์„œ ์•ฝ๊ฐ„ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. 1. Positional encoding ํ•˜๋Š” ์œ„์น˜๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. CNN Backbone์œผ๋กœ ๋ฝ‘์•„๋‚ธ feature matrix d ร— HW์—๋Š” ์œ„์น˜ ์ •๋ณด๊ฐ€ ์†Œ์‹ค๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ Transformer๋„ ์ด์™€ ๊ฐ™์€ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Positional encoding์„ ๋”ํ•ด์ฃผ์—ˆ์ฃ . DETR๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Positional encoding์„ ๋”ํ•ด์ฃผ๋Š”๋ฐ ์œ„์น˜๊ฐ€ ์‚ด์ง ๋‹ค๋ฆ…๋‹ˆ๋‹ค. 2) Autoregression์ด ์•„๋‹Œ Parallel ๋ฐฉ์‹์œผ๋กœ output์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด Transformer๋Š” ๋‹จ์–ด ํ•œ ๊ฐœ์”ฉ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅ๊ฐ’์„ ๋‚ด๋†“์Šต๋‹ˆ๋‹ค. Autoregression์€ ํ˜„์žฌ output ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ์ด์ „ ๋‹จ๊ณ„๊นŒ์ง€ ์ถœ๋ ฅํ•œ output ๊ฐ’์„ ์ฐธ๊ณ ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด DETR์—์„œ ์‚ฌ์šฉํ•œ Transformer๋Š” Paralle ๋ฐฉ์‹์œผ๋กœ, ์ฆ‰ ๋ชจ๋“  output ๊ฐ’์„ ํ†ต์งธ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. Bipartite Matching DETR์€ ์ถฉ๋ถ„ํžˆ ํฐ ์ˆ˜์˜ Bounding box๋ฅผ N ๊ฐœ ์„ค์ •ํ•˜๊ณ  ์ด์— ๋Œ€ํ•ด์„œ class์™€ bounding box์˜ ํฌ๊ธฐ ๋ฐ ์œ„์น˜๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ Input image ์ƒ์— 2๊ฐœ์˜ object๋งŒ ์กด์žฌํ•œ๋‹ค๋ฉด 2๊ฐœ์˜ bounding box์— ๋Œ€ํ•ด์„œ๋Š” class, bounding box์˜ ํฌ๊ธฐ ๋ฐ ์œ„์น˜๋ฅผ ์˜ˆ์ธกํ•˜๊ณ , ๋‚˜๋จธ์ง€ 2๊ฐœ์— ๋Œ€ํ•ด์„œ๋Š” no object๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด DETR์€ Autoregression์ด ์•„๋‹Œ Parallel ๋ฐฉ์‹์œผ๋กœ output์„ ์ถœ๋ ฅํ•˜๋ฏ€๋กœ N ๊ฐœ์˜ Bouding box๊ฐ€ ๋™์‹œ์— ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰ N ๊ฐœ์˜ bounding box๊ฐ€ ์–ด๋–ค ground-truth object๋ฅผ ๊ฒ€์ถœํ•˜๊ณ  ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ทธ๋ฆผ์—์„œ ๋ถ„ํ™์ƒ‰ boudning box๊ฐ€ 1๋ฒˆ ๊ฐˆ๋งค๊ธฐ์— ๋Œ€ํ•œ bouding box์˜€๋‹ค๋ฉด loss ๊ฐ’์ด ์ž‘๊ฒ ์ง€๋งŒ, ๋งŒ์•ฝ 2๋ฒˆ ๊ฐˆ๋งค๊ธฐ์— ๋Œ€ํ•œ bouding box์˜€๋‹ค๋ฉด loss ๊ฐ’์ด ํฌ๊ฒ ์ฃ ? ๋”ฐ๋ผ์„œ bounding box๊ฐ€ ground truth์˜ ์–ด๋–ค object๋ฅผ ๊ฒ€์ถœํ•˜๊ณ  ์žˆ๋Š”์ง€ 1 ๋Œ€ 1๋กœ ๋งค์นญ์„ ํ•ด์ฃผ๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜๋ฉฐ ์ด๋ฅผ bipartite matching์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. (Bipartite matching์ด ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.) Loss function & Training ฯƒ: Ground truth์˜ object set์˜ ์ˆœ์—ด ฯƒ_ hat: L_match๋ฅผ ์ตœ์†Œ๋กœ ํ•˜๋Š” ์˜ˆ์ธก bounding box set์˜ ์ˆœ์—ด y: Ground truth์˜ object set // y_hat: ์˜ˆ์ธกํ•œ N object set c: class label // p(c): ํ•ด๋‹น class์— ์†ํ•  ํ™•๋ฅ  b: bounding box์˜ ์œ„์น˜์™€ ํฌ๊ธฐ (x, y, w, h) L_match ground truth์˜ bounding box์™€ ์˜ˆ์ธก bounding box๊ฐ€ ์ •๋ณด๊ฐ€ ์ž˜ matching ๋˜์—ˆ์„ ๋•Œ ๋‚ฎ์€ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. 1-1. p(c): ํ•ด๋‹น class๋กœ ์˜ˆ์ธกํ•œ ํ™•๋ฅ . ์•ž์— (-)๊ฐ€ ๋ถ™์–ด์žˆ์œผ๋ฏ€๋กœ ํ•ด๋‹น ํ™•๋ฅ ์ด ๋†’์„์ˆ˜๋ก L_match๊ฐ€ ์ž‘์•„์ง 1-2. L_box: ground truth์˜ bounding box์™€ ์˜ˆ์ธก bounding box ์‚ฌ์ด์˜ loss. ๋‘ box๊ฐ€ ๋น„์Šทํ• ์ˆ˜๋ก L_match๊ฐ€ ์ž‘์•„์ง. bounding box์˜ ํฌ๊ธฐ๊ฐ€ ํด์ˆ˜๋ก L1 loss๊ฐ€ ์ปค์ง€๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด bounding box ๊ฐ„ IOU loss๋ฅผ ๋”ํ•˜์—ฌ ์ด๋ฅผ ๋ณด์ •ํ•ด ์คŒ ฯƒ_ hat L_match๋ฅผ ์ตœ์†Œ๋กœ ํ•˜๋Š” ์˜ˆ์ธก bounding box ์ˆœ์„œ ฯƒ_ hat ์„ ์ฐพ์Šต๋‹ˆ๋‹ค. Hungarian loss ฯƒ_ hat๋ฅผ ์ฐพ์•˜์œผ๋ฉด bipartite matching์ด ์™„๋ฃŒ๋œ ๊ฒƒ์ด๋ฏ€๋กœ ์ด์ œ Loss๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Loss๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด Hungarian loss๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. (์ผ๋ฐ˜์ ์ธ object detection ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” loss์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค๋งŒ Hungarian loss๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.) ์ด Loss๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•™์Šต์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ์žฅ์  Transformer๋ฅผ object detection์— ์ตœ์ดˆ๋กœ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. COCO dataset์— ๋Œ€ํ•ด์„œ Faster R-CNN baseline ๊ธ‰์˜ ์ •ํ™•๋„์™€ ๋Ÿฐํƒ€์ž„ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. End-to-End training์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์•„์ฃผ ๊ฐ„๋‹จ๋ช…๋ฃŒํ•œ ๊ตฌ์กฐ + ๊น”๋”ํ•œ ์ฝ”๋“œ! ๋‹จ์  Transformer์˜ ํŠน์„ฑ์ƒ ํ•™์Šตํ•˜๋Š”๋ฐ ๊ต‰์žฅํžˆ ๋งŽ์€ ์‹œ๊ฐ„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. small object detection ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. Reference ์›๋…ผ๋ฌธ: End-to-end object detection with transformers ์›๋…ผ๋ฌธ์˜ PyTorch ๊ตฌํ˜„ ๋™๋นˆ๋‚˜ | DETR: End-to-End Object Detection with Transformer JinWon Lee | End-to-End Object Detection with Transformers(DETR) ์ •๋ฆฌ๋Š” ์Šต๊ด€ | End-to-end object detection with transformers ๋น ๋ฅด๊ฒŒ ์ดํ•ดํ•˜๊ธฐ(DETR ๋ฆฌ๋ทฐ) KP's blog| End-to-End Object Detection with Transformers (4) ๊ฐ์ฒด ๊ฒ€์ถœ ๋ชจ๋ธ ํ‰๊ฐ€ ๋ฐฉ๋ฒ• ๊ฐœ์š” ๊ฐ์ฒด ๊ฒ€์ถœ ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ 3๊ฐ€์ง€ ์š”์†Œ๊ฐ€ ๊ณ ๋ ค๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ๊ณ„ ์ƒ์ž ์ •๋ฐ€๋„(Bounding box precision) : ์ •ํ™•ํ•œ ๊ฒฝ๊ณ„ ์ƒ์ž (๋„ˆ๋ฌด ํฌ์ง€๋„, ๋„ˆ๋ฌด ์ž‘์ง€๋„ ์•Š์€)๋ฅผ ์ œ๊ณตํ•˜๋Š”๊ฐ€? ์žฌํ˜„์œจ(recall) : ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์ฐพ์•˜๋Š”๊ฐ€? ํด๋ž˜์Šค ์ •๋ฐ€๋„(Class precision) : ๊ฐ์ฒด๋งˆ๋‹ค ์ •ํ™•ํ•œ ํด๋ž˜์Šค๋ฅผ ์ถœ๋ ฅํ–ˆ๋Š”๊ฐ€? ์—ฐ๊ตฌ์ž๋“ค์ด ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด ํƒ์ง€ ๋ชจ๋ธ์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ณตํ†ต์˜ ํ‰๊ฐ€ ์ง€ํ‘œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ๊ฐ์ฒด ๊ฒ€์ถœ์— ์žˆ์–ด์„œ๋Š” ํ›ˆ๋ จ์…‹์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ฐ์ดํ„ฐ ์ž์ฒด๊ฐ€ ์‚ฌ๋žŒ์ด ์ž„์˜๋กœ ๊ฒฝ๊ณ„ ์ƒ์ž๋ฅผ ๊ทธ๋ฆฐ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ฆ‰ human annotation์— ์˜์กดํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ์ด ๋ฐ˜๋“œ์‹œ ์ง„๋ฆฌ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์—†๊ณ  ์ด์— ๋”ฐ๋ผ ์„ฑ๋Šฅ ํ‰๊ฐ€๋ฅผ ํ•  ๋•Œ๋„ ์ด๋Ÿฐ ์ ์„ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. mAP(mean Average Precision) ์˜ˆ๋ฅผ ๋“ค์–ด 2๊ฐœ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋น„๊ตํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์‚ฌ๋žŒ์„ ๊ฒ€์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒ€์ถœ๋ฅ ์€ 99%์ด์ง€๋งŒ, 1์žฅ๋‹น 10๊ฑด ์ •๋„ ์˜ค๊ฒ€์ถœ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์‚ฌ๋žŒ์„ ๊ฒ€์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒ€์ถœ๋ฅ ์€ 50% ์ง€๋งŒ ์˜ค๊ฒ€์ถœ์€ ์ „ํ˜€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ด ์ƒํ™ฉ์—์„œ ์–ด๋Š ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ข‹๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ์„œ ๊ฒ€์ถœ๋ฅ ๊ณผ ์˜ค๊ฒ€์ถœ์ด ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ ๋ชจ๋‘๋ฅผ ๊ณ ๋ คํ•ด์„œ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•ด์•ผ ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ mAP๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ์ •๋ฐ€๋„(precision)์™€ ์žฌํ˜„์œจ(recall, ๊ฒ€์ถœ๋ฅ )์ด ๊ณ„์‚ฐ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ†ต๊ณ„ํ•™์—์„œ ์ •์˜ํ•˜๊ธฐ๋กœ๋Š” ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ์„ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ ๋„ค ๊ฐ€์ง€๊ฐ€ ์ •์˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. TP (true positive) : ์‹ค์ œ ์ฐธ์ธ ๊ฒƒ์„ ์ฐธ์œผ๋กœ ์˜ˆ์ธกํ•œ ์ˆ˜ FP (false positive) : ์‹ค์ œ ๊ฑฐ์ง“์ธ ๊ฒƒ์„ ์ฐธ์œผ๋กœ ์˜ˆ์ธกํ•œ ์ˆ˜ FN (false negative) : ์‹ค์ œ ์ฐธ์ธ ๊ฒƒ์„ ๊ฑฐ์ง“์œผ๋กœ ์˜ˆ์ธกํ•œ ์ˆ˜ TN (true negative) : ์‹ค์ œ ๊ฑฐ์ง“์ธ ๊ฒƒ์„ ๊ฑฐ์ง“์œผ๋กœ ์˜ˆ์ธกํ•œ ์ˆ˜ ์ด๋ฅผ ๊ฐ์ฒด ๊ฒ€์ถœ์˜ ๋ฌธ์ œ์— ๋Œ€์ž…ํ•ด ๋ณธ๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ์ƒํ™ฉ ์˜ˆ์ธก ๊ฒฐ๊ณผ Positive TP, ์˜ณ์€ ๊ฒ€์ถœ FN, ๊ฒ€์ถœ๋˜์–ด์•ผ ํ•  ๊ฒƒ์ด ๊ฒ€์ถœ๋˜์ง€ ์•Š์Œ Negative FP, ํ‹€๋ฆฐ ๊ฒ€์ถœ TN, ๊ฒ€์ถœ๋˜์ง€ ๋ง์•„์•ผ ํ•  ๊ฒƒ์ด ๊ฒ€์ถœ๋˜์ง€ ์•Š์Œ ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. r c s o = P P F = P l D t c i n r c l = P P F = P l G o n T u h ์ •๋ฐ€๋„(precision)์€ ์˜ˆ์ธก๋œ ๊ฒฐ๊ณผ๊ฐ€ ์–ผ๋งˆ๋‚˜ ์ •ํ™•ํ•œ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. ์ฐธ์ด๋ผ๊ณ  ์˜ˆ์ธกํ•œ ๊ฒƒ ์ค‘์—์„œ ์ •๋‹ต์„ ๋งžํžŒ ๊ฒƒ์˜ ๋น„์œจ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์žฌํ˜„์œจ(recall)์€ GT(์‹ค์ œ ์ฐธ์ธ ๊ฒƒ๋“ค) ์ค‘์— ์–ผ๋งˆ๋‚˜ ์ •๋‹ต์„ ๋งžํ˜”๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. ์ฐธ์ด๋ผ๊ณ  ์˜ˆ์ธกํ•ด์•ผ ํ•  ๊ฐ์ฒด๋“ค ์ค‘์— ์ฐธ์ด๋ผ๊ณ  ๋งž๊ฒŒ ์˜ˆ์ธกํ•œ ๊ฒƒ์˜ ๋น„์œจ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ธก์ด ์‹ค์ œ ๋ถ„๋ฅ˜์™€ ์ผ์น˜ํ•˜๋ฉด FP๋‚˜ FN์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— 1์— ์ˆ˜๋ ดํ•˜์ง€๋งŒ, ๋ชจ๋ธ์ด ์•ˆ์ •์ ์ด์ง€ ๋ชปํ•œ ํŠน์ง•์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์˜ˆ์ธก์„ ํ•œ๋‹ค๋ฉด FP๊ฐ€ ๋Š˜์–ด๋‚˜ ์ •๋ฐ€๋„๊ฐ€ ๋–จ์–ด์ง€๊ณ  ๋ชจ๋ธ์ด ๋„ˆ๋ฌด ์—„๊ฒฉํ•œ ๊ธฐ์ค€์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค๋ฉด FN์ด ๋งŽ์•„์ ธ recall์ด ๋‚ฎ์•„์ง‘๋‹ˆ๋‹ค. ์ •๋ฐ€๋„ - ์žฌํ˜„์œจ ๊ณก์„  ์ผ๋ฐ˜์ ์œผ๋กœ ์ •๋ฐ€๋„-์žฌํ˜„์œจ ๊ณก์„ ์€ ์‹ ๋ขฐ๋„ ์ž„๊ณ—๊ฐ’๋งˆ๋‹ค ๋ชจ๋ธ์˜ ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ์„ ์‹œ๊ฐํ™”ํ•ด์„œ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์‹ ๋ขฐ๋„๊ฐ€ ๋‚ฎ์€ ์˜ˆ์ธก์€ ์œ ์ง€ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ์‹ ๋ขฐ๋„ ์ž„๊ณ—๊ฐ’ T ์ดํ•˜์˜ ์˜ˆ์ธก์€ ์ œ๊ฑฐํ•˜๊ณ  ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ T๊ฐ€ 1์— ๊ฐ€๊นŒ์šฐ๋ฉด, ์ •๋ฐ€๋„๋Š” ๋†’์ง€๋งŒ ์žฌํ˜„์œจ์€ ๋‚ฎ์•„์ง‘๋‹ˆ๋‹ค. : ๋†“์น˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋งŽ์•„์ ธ ์žฌํ˜„์œจ์ด ๋‚ฎ์•„์ง€๊ณ , ์‹ ๋ขฐ๋„๊ฐ€ ๋†’์€ ์˜ˆ์ธก๋งŒ ์œ ์ง€ํ•ด FP์˜ ์ˆ˜๋Š” ๋–จ์–ด์ ธ ์ •๋ฐ€๋„๊ฐ€ ๋†’์•„์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ T๊ฐ€ 0์— ๊ฐ€๊นŒ์šฐ๋ฉด, ์ •๋ฐ€๋„๋Š” ๋‚ฎ์ง€๋งŒ ์žฌํ˜„์œจ์€ ๋†’์•„์ง‘๋‹ˆ๋‹ค. : ๋Œ€๋ถ€๋ถ„์˜ ์˜ˆ์ธก์„ ์œ ์ง€ํ•ด์„œ FN์ด ๋‚ฎ์•„์ง€๊ณ , ์žฌํ˜„์œจ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค. ๋Œ€์‹  FP๊ฐ€ ๋งŽ์•„์ ธ ์ •๋ฐ€๋„๊ฐ€ ๋‚ฎ์•„์ง‘๋‹ˆ๋‹ค. AP์™€ mAP precision-recall curve๋กœ ๋งŽ์€ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ˆซ์ž ํ•˜๋‚˜๋กœ ์‹ ๋ขฐ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ด ์ดํ•ดํ•˜๊ธฐ ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. AP(average precision)๋Š” ๊ณก์„ ์˜ ์•„๋ž˜ ์˜์—ญ์œผ๋กœ, ํ•ญ์ƒ 0-1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ–์Šต๋‹ˆ๋‹ค. mAP(mean Average Precision)์€ ๊ฐ ํด๋ž˜์Šค๋งˆ๋‹ค์˜ AP ํ‰๊ท ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ๋†’์„์ˆ˜๋ก ์ข‹์Šต๋‹ˆ๋‹ค. AP(Averager Precision)์€ ์žฌํ˜„์œจ(recall)์„ 0๋ถ€ํ„ฐ 1๊นŒ์ง€ ์ผ์ • ๋‹จ์œ„๋กœ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์žฌํ˜„์œจ๊ณผ ์ •๋ฐ€๋„๋Š” ๋ฐ˜๋น„๋ก€ ๊ด€๊ณ„์ด๊ธฐ ๋•Œ๋ฌธ์— ์žฌํ˜„์œจ์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์ •๋ฐ€๋„๊ฐ€ ๊ฐ์†Œํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฐ ๋‹จ์œ„๋งˆ๋‹ค ์ •๋ฐ€๋„ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜์—ฌ ํ‰๊ท ์„ ๊ตฌํ•œ ๊ฒƒ์ด AP์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๊ฐ๊ฐ์˜ ํด๋ž˜์Šค์˜ AP ๊ฐ’์„ ๊ณ„์‚ฐํ•œ ํ›„ ํ‰๊ท ์„ ๋‚ธ ๊ฒƒ์ด mAP์ž…๋‹ˆ๋‹ค. IoU(Intersection over Union) ์•ž์„œ AP์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๋ฉด์„œ, FP์™€ TP๊ฐ€ ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก์ด ์ผ์น˜ํ•˜๋Š” ๋˜๋Š” ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ์ •์˜๋œ๋‹ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์–ธ์ œ ์ผ์น˜ํ•˜๋Š”์ง€๋Š” ์–ด๋–ป๊ฒŒ ๊ฒฐ์ •ํ• ๊นŒ์š”? ๋‘ ์ง‘ํ•ฉ์ด ์–ผ๋งˆ๋‚˜ ๊ฒน์น˜๋Š”์ง€ ์ธก์ •ํ•˜๋Š” ์ž์นด๋ฅด ์ง€ํ‘œ(Jaccard index)๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ IoU(Intersection over Union)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅด๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. IOU์—๋Š” ๊ธฐ์ค€์ด ๋˜๋Š” Threshold ๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. Threshold ๊ฐ’์„ ํ†ตํ•ด TP์™€ FP๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. TP : IoU >= Threshold FP : IoU < Threshold Threshold ๊ฐ’์ด ์ž‘์•„์งˆ์ˆ˜๋ก TP๊ฐ€ ์ปค์ง€๊ฒŒ ๋˜์–ด Recall ๊ฐ’์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. IoU๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o ( , ) | โˆฉ | A B = A B ( A + B โˆ’ A B) ์ด๋ฅผ ์ด๋ฏธ์ง€๋ฅผ ํ†ตํ•ด ๋‚˜ํƒ€๋‚ด์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๊ฑด, IoU๋Š” ์˜ˆ์ธกํ•œ bounding box์™€ ์‹ค์ œ(์‚ฌ๋žŒ์ด ์ •ํ•ด์ค€) bounding box๊ฐ€ ๊ฒน์น˜๋Š” ์˜์—ญ์ด ๋„“์œผ๋ฉด ๋„“์„์ˆ˜๋ก ๋†’์€ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ธ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์™œ ๊ต์ง‘ํ•ฉ๋งŒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ด๋Ÿฐ ๋ถ„์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ด์„œ ๊ณ„์‚ฐํ• ๊นŒ์š”? ๊ต์ง‘ํ•ฉ์€ ์ ˆ๋Œ€์ ์ธ ์ˆ˜์น˜์ด์ง€ ์ƒ๋Œ€์ ์ด์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‘ ๊ฐœ์˜ ํฐ bounding box๋Š” ๊ฒน์น˜๋Š” ์ •๋„๋Š” ๋น„์Šทํ•ด๋„ ํ›จ์”ฌ ๋งŽ์€ ํ”ฝ์…€์ด ๊ฒน์น˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ•ฉ์ง‘ํ•ฉ์œผ๋กœ ๋‚˜๋ˆ  ๋น„์œจ์„ ์ง€ํ‘œ๋กœ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. IoU ์ž„๊ณ—๊ฐ’ Object detection ๋ฌธ์ œ์—์„œ TP์™€ FP๋ฅผ ๊ณ„์‚ฐํ•  ๋•Œ bounding box๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€์˜ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€๊ฐ€ ๋‹ฌ๋ผ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต์€ IoU ์ž„๊ณ—๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ bounding box๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€, ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”์ง€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์ด๋ผ๋ฉด, 1,4,5๋Š” ์ •ํ•ด์ง„ truth (์ดˆ๋ก์ƒ‰) ๊ณผ ์ ˆ๋ฐ˜ ์ด์ƒ ๊ฒน์น˜๋ฏ€๋กœ IoU ์ž„๊ณ—๊ฐ’์ด 0.5๋ผ๋ฉด ์ฐธ์œผ๋กœ ๊ฐ„์ฃผ๋˜์ง€๋งŒ, 2,3์€ FP๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค. IoU์˜ ์ž„๊ณ—๊ฐ’์ด ๋†’์•„์ง€๋ฉด TP์— ํ•ด๋‹นํ•˜๋Š” ์˜ˆ์ธก์€ ์ค„๊ณ , FP์— ํ•ด๋‹นํ•˜๋Š” ์˜ˆ์ธก์€ ๋Š˜์–ด๋‚  ๊ฒƒ์ž…๋‹ˆ๋‹ค. Reference Object Detection์˜ ์ •์˜์™€ Metric mAP(mean Average Precision) ์‹ค์ „ ํ…์„œ ํ”Œ๋กœ 2๋ฅผ ํ™œ์šฉํ•œ ์ปดํ“จํ„ฐ ๋น„์ „ (๋ฒ ๋ƒ๋ฏผ ํ”Œ๋žœ์น˜, ์—˜๋ฆฌ์—‡ ์•ˆ๋“œ๋ ˆ์Šค) IoU gif 5. Image Captioning(์ด๋ฏธ์ง€ ์บก์…”๋‹) ๋ชจ๋“  Task๊ฐ€ ๊ทธ๋ ‡์ง€๋งŒ ์‹ ๊ธฐํ•œ ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ ์ž˜ ์„ค๋ช…ํ•˜๋Š” ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•ด ๋‚ด๋Š” Task์ž…๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ ๋น„์ „๊ณผ NLP๊ฐ€ ํ•ฉ์ณ์ง„ ์˜์—ญ์œผ๋กœ NLP์— ๋Œ€ํ•œ ์ง€์‹๋„ ํ•„์š”ํ•œ Task์ž…๋‹ˆ๋‹ค. (1) ์ด๋ฏธ์ง€ ์บก์…”๋‹ ์•„์ด๋””์–ด <img ์ด๋ฏธ์ง€๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์ด๋ฏธ์ง€์˜ ๊ฐ ๋ฌผ์ฒด์™€ ์ƒํ™ฉ์„ ํŒ๋‹จํ•˜๊ณ  ์„ค๋ช…ํ•˜๋Š” ๋ฌธ์žฅ์ด ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒƒ์„ text generation์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. image captioning์€ ์ด๋ฏธ์ง€ ๋‚ด์— ์žˆ๋Š” ๊ฐ์ฒด์— ๋Œ€ํ•œ ํŒ๋‹จ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด๋“ค ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ํŒŒ์•…ํ•˜๊ณ  ์ž์—ฐ์–ด์˜ ํ˜•ํƒœ๋กœ ์•Œ๋งž๊ฒŒ ํ‘œํ˜„ํ•˜๋Š” ๋ฌธ์ œ๋„ ๊ฒธํ•˜๊ณ  ์žˆ์–ด ์ปดํ“จํ„ฐ ๋น„์ „๊ณผ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์ข…ํ•ฉ์ ์ธ ์ดํ•ด๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ๋ฌธ์žฅ์œผ๋กœ ๋ฐ”๊พผ ํ›„ NLP ๋ถ„์•ผ์™€ ๊ฒฐํ•ฉํ•˜์—ฌ ๋‹ค์–‘ํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ๋น„๊ต์  ์šฉ๋Ÿ‰์ด ํฐ ๋ฐ์ดํ„ฐ์ธ ์ด๋ฏธ์ง€๋ฅผ ํ…์ŠคํŠธ๋กœ ๋ฐ”๊พธ๊ธฐ ๋•Œ๋ฌธ์— ์ €์žฅ ๊ณต๊ฐ„์ด ํš๊ธฐ์ ์œผ๋กœ ์ค„์–ด๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์บก์…”๋‹ ํ™œ์šฉ ์‚ฌ๋ก€ ์ด๋ฏธ์ง€์— ์บก์…˜์„ ์ถ”๊ฐ€ํ•ด ๊ฒ€์ƒ‰์˜ ํšจ์œจ์„ฑ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์—๋Š” ํ…์ŠคํŠธ๋งŒ์œผ๋กœ ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ–ˆ์ง€๋งŒ ์ด๋ฏธ์ง€๋ฅผ ํ…์ŠคํŠธํ™”ํ•˜์—ฌ ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋ฐ”๊พธ์–ด ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ ํšจ์œจ์ด ๋” ์ข‹์•„์ง‘๋‹ˆ๋‹ค. ์‹œ๊ฐ ์žฅ์• ์ธ์ด๋‚˜ ์ €์‹œ๋ ฅ์ž๋“ค์—๊ฒŒ ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ํ…์ŠคํŠธํ™”ํ•œ ํ›„ TTS(Text To Speak) ๊ธฐ์ˆ ์„ ์ด์šฉํ•˜์—ฌ ์Œ์„ฑ์œผ๋กœ ์ฝ์–ด์ฃผ์–ด ์•ž์˜ ์ƒํ™ฉ์ด๋‚˜ ๊ทธ๋ฆผ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ์ˆ  ์น˜๋ฃŒ ๋ถ„์•ผ์—์„œ ์น˜๋ฃŒ์‚ฌ์˜ ์ฃผ๊ด€์„ ๋ฐฐ์ œํ•˜๊ณ  ์ด๋ฏธ์ง€ ์บก์…”๋‹์„ ์ด์šฉํ•˜์—ฌ ๊ฐ๊ด€์ ์œผ๋กœ ๋ฏธ์ˆ ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ํ•จ์œผ๋กœ์จ ์น˜๋ฃŒ์˜ ์ผ๊ด€์„ฑ์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ฐธ๊ณ  : https://www.earticle.net/Article/A366290) ์ด๋ฏธ์ง€ ๋ถ„ํ•  ๋„คํŠธ์›Œํฌ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ : CNN & RNN ์ด๋ฏธ์ง€ ์บก์…”๋‹์— ๊ธฐ๋ณธ ๋„คํŠธ์›Œํฌ๋Š” ์ด๋ฏธ์ง€ ํŠน์ง•์„ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•œ CNN๊ณผ ๋ฌธ์žฅ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•œ RNN ์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. โ‘  ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ CNN์„ ํ†ต๊ณผ์‹œ์ผœ ํŠน์ง•๋“ค์„ ์ถ”์ถœํ•œ๋‹ค. โ‘ก ์ถ”์ถœํ•œ ํŠน์ง•๋“ค์„ RNN์— ๋Œ€์ž…ํ•˜์—ฌ ๋ฌธ์žฅ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก, ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์…‹๊ณผ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์บก์…˜๋“ค์„ ํ† ๋Œ€๋กœ ํ•™์Šต์‹œํ‚จ๋‹ค. ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ CNN์„ ์ด์šฉํ•ด ํŠน์ง•์„ ์ถ”์ถœํ•˜๊ณ  attention๊ณผ LSTM์„ ์ด์šฉํ•œ RNN ๋ชจ๋ธ์„ ํ†ต๊ณผํ•˜์—ฌ ๊ฐ ํŠน์ง•์— ๋งž๋Š” ๋‹จ์–ด๋“ค์„ ์–ป์–ด๋‚ด๊ณ  ์ด๋ฏธ์ง€๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋ฌธ์žฅ์„ ์™„์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์บก์…”๋‹ ๋‹จ๊ณ„ 1. CNN์„ ์ด์šฉํ•œ ํŠน์ง• ์ถ”์ถœ RGB 3์ฑ„๋„์˜ ์ธํ’‹ ๋ฐ์ดํ„ฐ๋ฅผ ResNet-101์„ ์ด์šฉํ•˜์—ฌ ํŠน์ง•์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. 2. Attention Network๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹จ์–ด ์ƒ์„ฑ Attention Network๋ฅผ ํ†ต๊ณผํ•˜์—ฌ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์— ๊ฐ€์ค‘์น˜๋ฅผ ๋‘์–ด ๋‹จ์–ด๋ฅผ ์ถ”์ถœํ•ด ๋ƒ…๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜๊ฐ€ ๋†’์€ ๋ถ€๋ถ„์„ ์ด์šฉํ•ด ์‚ฌ์ง„์„ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌผ์ฒด, ํ–‰์œ„ ๋“ฑ์„ ์ถ”์ถœํ•ด ๋ƒ…๋‹ˆ๋‹ค. 3. RNN์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์žฅ ์™„์„ฑ ๊ฐ€์ค‘์น˜๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ถ”์ถœํ•ด๋‚ธ ์ด๋ฏธ์ง€์˜ ์ฃผ์š” ๋‹จ์–ด๋“ค์„ RNN์„ ํ†ตํ•ด ์กฐํ•ฉํ•˜์—ฌ ๋ฌธ์žฅ์„ ์™„์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์š” ๋‹จ์–ด๋“ค๋กœ ๋ฌธ์žฅ์„ ์™„์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ๊ฒฝ๊ณผ ๊ฐ™์€ ์ค‘์š”ํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„์€ ๋ˆ„๋ฝ๋  ์ˆ˜ ์žˆ์ง€๋งŒ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์„ค๋ช…ํ•ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๊ตฌ์กฐ ๋งˆ๋ฌด๋ฆฌ ์˜ˆ์‹œ ์‚ฌ์ง„์„ ํ†ตํ•ด ๋‚˜์˜จ ๋ฌธ์žฅ์ธ 'a man holds a football'์„ Google ๊ฒ€์ƒ‰์ฐฝ์— ๊ฒ€์ƒ‰ํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์บก์…”๋‹ ์„ฑ๊ณต ์˜ˆ์‹œ ๋ฌผ์ฒด์™€ ์ƒํ™ฉ์„ ์ •ํ™•ํžˆ ์ธ์ง€ํ•œ ๊ฒฝ์šฐ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ž˜ ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜์„ ์ด์šฉํ•˜์—ฌ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์— ์ง‘์ค‘ํ•˜์—ฌ ์‚ฌ์ง„์˜ ํŠน์ง•์„ ์ •ํ™•ํ•˜๊ฒŒ ํ…์ŠคํŠธ๋กœ ๋‚˜ํƒ€๋‚ด์–ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ํ…Œ๋‹ˆ์Šค ๊ฐ™์€ ๊ฒฝ์šฐ ํ…Œ๋‹ˆ์Šค ๋™์ž‘์— ๋Œ€ํ•œ ํ•™์Šต์ด ์ด๋ฃจ์–ด์กŒ๋‹ค๋ฉด ๋”์šฑ ์ •ํ™•ํ•˜๊ฒŒ ํ–‰์œ„๋ฅผ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ์—ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์บก์…”๋‹ ์‹คํŒจ ์˜ˆ์‹œ ํ„ธ์˜ท์„ ์ž…์€ ์—ฌ์ž ์‚ฌ์ง„์—์„œ ํ„ธ์˜ท์„ ๊ณ ์–‘์ด๋กœ ์ธ์‹์„ ํ•˜์—ฌ ์—ฌ์ž๊ฐ€ ๊ณ ์–‘์ด๋ฅผ ๋“ค๊ณ  ์žˆ๋‹ค๊ณ  ์ž˜๋ชป ํ•ด์„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฌผ๊ตฌ๋‚˜๋ฌด ์„  ์‚ฌ๋žŒ์„ ๊ทธ๋ƒฅ ์„œ์žˆ๋‹ค๊ณ  ์ธ์‹ํ•˜๊ฑฐ๋‚˜, ์žˆ์ง€๋„ ์•Š์€ ์ƒˆ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜๊ฑฐ๋‚˜, ์•ผ๊ตฌ์„ ์ˆ˜๊ฐ€ ๊ณต์„ ๋ฐ›๊ณ  ์žˆ๋Š”๋ฐ ๋˜์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜๋Š” ๋“ฑ ์ž˜๋ชป ํ•ด์„ํ•˜๋Š” ์˜ˆ์‹œ๋“ค์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์‹คํŒจ๋Š” ๋ฌผ์ฒด๋‚˜ ํ–‰์œ„๋ฅผ ์ž˜๋ชป ์ธ์ง€ํ•œ ์ƒํ™ฉ์œผ๋กœ ์–ด๋Š ํ•œ ๊ฐ€์ง€๋ผ๋„ ์ž˜๋ชป ์ถ”์ถœํ•˜๋ฉด ์•„์˜ˆ ๋‹ค๋ฅธ ์„ค๋ช…์ด ๋˜์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋‹นํ•œ ์ •ํ™•์„ฑ์ด ์š”๊ตฌ๋ฉ๋‹ˆ๋‹ค. Reference ์› ๋…ผ๋ฌธ https://k.kakaocdn.net/dn ์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ [https://throwexception.tistory.com/1203] 1) ์ด๋ฏธ์ง€ ์บก์…”๋‹ ๋ฐ์ดํ„ฐ ์…‹ UIUC Pascal sentence only 1000 images to train and test models simple captions and images 25% captions do not contain verbs. 15% contain static verbs like sit, stand, wear, look etc. Flickr 8k, Flickr 30k 82,783 images in Flickr 8k, >30,000 images in Flickr 30k Images were manually selected to focus mainly on people and animals performing actions. 5 captions per image. Captions contain graded human quality scores for 5,822 captions, with scores ranging from 1 (the selected caption is unrelated to the image.) to 4 (the selected caption described the image w/o any errors) 21% contain static verbs like sit, stand, wear, look etc. MS COCO 120k train & validation images (a lot more than Pascal or Flickr) 5 captions per image. instance level segmentations labels with 91 object classes and 2.5M labelled instances Standard benchmark for image caption generation task 255,000 human judgments were collected. The judgments capture the dimensions of overall caption quality (M1-M2), correctness (M3), detailedness (M4) and saliency (M5) Abstract Scenes Dataset 1002 sets of scenes with 10 images in each Reduced variability than real word scenes Descriptions have non-visual attributes Provide segmentation labels. Reference Univ. of Toronto, Kaustav Kundu | Datasets and Metrics for Image Captioning Generation ์„œ์šธ๋Œ€ํ•™๊ต ๋Œ€ํ•™์› ์ปดํ“จํ„ฐ๊ณตํ•™๋ถ€ ์„์‚ฌ๋…ผ๋ฌธ<NAME> | ์˜๋ฏธ์ ์œผ๋กœ ๋ณด๊ฐ•๋œ ๋น„์ง€๋„ ํ•™์Šต ๊ธฐ๋ฐ˜์˜ ์ด๋ฏธ์ง€ ์บก์…”๋‹ (2) ์ด๋ฏธ์ง€ ์บก์…”๋‹ ๋ชจ๋ธ ์ดํ•ด๋ฅผ ์œ„ํ•œ ์‚ฌ์ „ ์ง€์‹ 1) ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding) ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด๋ž€ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ํ‘œํ˜„์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)์˜ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(word embedding)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋ฅผ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๊ณผ์ •์„ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ผ๊ณ  ํ•˜์—ฌ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(embedding vector)๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์œผ๋กœ๋Š” LSA, Word2Vec, FastText, Glove ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ๋„๊ตฌ์ธ Embedding()๋Š” ์•ž์„œ ์–ธ๊ธ‰ํ•œ ๋ฐฉ๋ฒ•๋“ค์„ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์ง€๋งŒ, ๋‹จ์–ด๋ฅผ ๋žœ๋ค ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค์—, ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ฐ€์ค‘์น˜๋ฅผ ํ•™์Šตํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 1. ํฌ์†Œ ํ‘œํ˜„(Sparse Representation) ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ๋ชจ๋“  ๋‹จ์–ด ๊ฐœ์ˆ˜๋กœ ๋ฒกํ„ฐ ์ฐจ์›์„ ์„ค์ •ํ•˜๊ณ , ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ์ธ๋ฑ์Šค ๊ฐ’๋งŒ 1๋กœ ์„ค์ •ํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” 0์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํฌ์†Œ ํ‘œํ˜„์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Ex) ๊ณ ์–‘์ด = [ 0 0 0 0 1 0 0 0 0 0 0 0 ... ์ค‘๋žต ... 0] # ์ด๋•Œ 1 ๋’ค์˜ 0์˜ ์ˆ˜๋Š” 9995๊ฐœ. ์ด๋Ÿฌํ•œ ํฌ์†Œ ๋ฒกํ„ฐ์˜ ๋ฌธ์ œ์ ์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๋ฉด ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ํ•œ์—†์ด ์ปค์ง„๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ๊ฐ–๊ณ  ์žˆ๋Š” ๋‹จ์–ด๊ฐ€ 10,000๊ฐœ์˜€๋‹ค๋ฉด ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 10,000์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ๊ทธ์ค‘์—์„œ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„๋งŒ 1์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” 0์˜ ๊ฐ’์„ ๊ฐ€์ ธ์•ผ๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์ด ํด์ˆ˜๋ก ๊ณ ์ฐจ์›์˜ ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์–ด๊ฐ€ 10,000๊ฐœ ์žˆ๊ณ  ๊ณ ์–‘์ด๋ผ๋Š” ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๋Š” 5์˜€๋‹ค๋ฉด ์› ํ•ซ ๋ฒกํ„ฐ๋Š” ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋˜์–ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฒกํ„ฐ ํ‘œํ˜„์€ ๊ณต๊ฐ„์  ๋‚ญ๋น„๋ฅผ ๋ถˆ๋Ÿฌ์ผ์œผํ‚ต๋‹ˆ๋‹ค. ํฌ์†Œ ํ‘œํ˜„์˜ ์ผ์ข…์ธ DTM๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋„ ํŠน์ • ๋ฌธ์„œ์— ์—ฌ๋Ÿฌ ๋‹จ์–ด๊ฐ€ ๋‹ค์ˆ˜ ๋“ฑ์žฅํ•˜์˜€์œผ๋‚˜, ๋‹ค๋ฅธ ๋งŽ์€ ๋ฌธ์„œ์—์„œ๋Š” ํ•ด๋‹น ํŠน์ • ๋ฌธ์„œ์— ๋“ฑ์žฅํ–ˆ๋˜ ๋‹จ์–ด๋“ค์ด ์ „๋ถ€ ๋“ฑ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์—ญ์‹œ๋‚˜ ํ–‰๋ ฌ์˜ ๋งŽ์€ ๊ฐ’์ด 0์ด ๋˜๋ฉด์„œ ๊ณต๊ฐ„์  ๋‚ญ๋น„๋ฅผ ์ผ์œผํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ๋‹ด์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋‹จ์ ์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 2. ๋ฐ€์ง‘ ํ‘œํ˜„(Dense Representation) ๋ฐ€์ง‘ ํ‘œํ˜„์€ ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋กœ ์ƒ์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ๊ฐ’์œผ๋กœ ๋ชจ๋“  ๋‹จ์–ด์˜ ๋ฒกํ„ฐ ํ‘œํ˜„์˜ ์ฐจ์›์„ ๋งž์ถฅ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ด ๊ณผ์ •์—์„œ ๋” ์ด์ƒ 0๊ณผ 1๋งŒ ๊ฐ€์ง„ ๊ฐ’์ด ์•„๋‹ˆ๋ผ ์‹ค์ˆซ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Ex) ๊ณ ์–‘์ด = [0.2 1.8 1.1 -2.1 1.1 2.8 ... ์ค‘๋žต ...] # ์ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128 ์ด ๊ฒฝ์šฐ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ์กฐ๋ฐ€ํ•ด์กŒ๋‹ค๊ณ  ํ•˜์—ฌ ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Reference ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ 2) Word2Vec Word2Vec ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ๋ฒกํ„ฐํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ๋Œ€ํ‘œ์ ์ธ ๋ฐฉ๋ฒ•์ด Word2Vec์ž…๋‹ˆ๋‹ค. Word2Vec์—๋Š” CBOW(Continuous Bag of Words)์™€ Skip-Gram ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ์Šต๋‹ˆ๋‹ค. CBOW๋Š” ์ฃผ๋ณ€์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ๊ฐ€์ง€๊ณ , ์ค‘๊ฐ„์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ, Skip-Gram์€ ์ค‘๊ฐ„์— ์žˆ๋Š” ๋‹จ์–ด๋กœ ์ฃผ๋ณ€ ๋‹จ์–ด๋“ค์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ž์ฒด๋Š” ๊ฑฐ์˜ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— CBOW๋ฅผ ์ดํ•ดํ•œ๋‹ค๋ฉด Skip-Gram๋„ ์†์‰ฝ๊ฒŒ ์ดํ•ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 1. CBOW(Continuous Bag of Words) CBOW์˜ ๊ธฐ๋ณธ ์•„์ด๋””์–ด๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ํ†ตํ•ด ์ฃผ์–ด์ง„ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋‹จ์–ด๊ฐ€ C ๊ฐœ๋ผ๊ณ  ํ•  ๋•Œ ์•ž๋’ค๋กœ C/2๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ํ†ตํ•ด ์ฃผ์–ด์ง„ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฌธ : "The fat cat sat on the mat" {"The", "fat", "cat", "on", "the", "mat"}์œผ๋กœ๋ถ€ํ„ฐ sat์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์€ CBOW๊ฐ€ ํ•˜๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ๋‹จ์–ด sat์„ ์ค‘์‹ฌ ๋‹จ์–ด(center word)๋ผ๊ณ  ํ•˜๊ณ , ์˜ˆ์ธก์— ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์–ด๋“ค์„ ์ฃผ๋ณ€ ๋‹จ์–ด(context word)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•ž๋’ค๋กœ ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๋ณผ์ง€ ์„ ํƒํ•˜๋Š” ๋ฒ”์œ„๋ฅผ ์œˆ๋„(Window)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ์˜ˆ๋ฌธ์—์„œ ์ค‘์‹ฌ ๋‹จ์–ด๊ฐ€ cat์ด๊ณ  Window๊ฐ€ 2๋ผ๋ฉด "The", "fat", "on", "the" ๋‹จ์–ด๋ฅผ ์ฐธ๊ณ ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ํ•™์Šต์‹œํ‚ฌ ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด๋“ค์„ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ์‹œ์ผœ์ค๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ window = m์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ํ•˜๋‚˜์˜ Center ๋‹จ์–ด์— ๋Œ€ํ•ด ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ๋ฒกํ„ฐ๋ฅผ Input์œผ๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ์ˆ˜์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. CBOW ๋ฐฉ์‹์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” Input layer -> Hidden layer๋กœ ๊ฐ€๋Š” weights์™€ Hidden layer -> Output layer๋กœ ๊ฐ€๋Š” weights์ž…๋‹ˆ๋‹ค. CBOW ์‹ ๊ฒฝ๋ง์„ ํ•™์Šต์‹œ์ผœ ๋‚˜์˜จ ์ตœ์ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋ฐ”๋กœ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๋œ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. CBOW ๋ชจ๋ธ์€ ๋งฅ๋ฝ ๋‹จ์–ด๋กœ๋ถ€ํ„ฐ ํƒ€๊นƒ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ์ด๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ CBOW ๋ชจ๋ธ์˜ ์ž…๋ ฅ์€ ๋งฅ๋ฝ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ์ด๊ณ , ์ถœ๋ ฅ์€ ํƒ€๊นƒ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” CBOW ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์˜ ๊ฐœ๊ด„์ ์ธ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค. Window๊ฐ€ 2์ผ ๋•Œ์˜ ์˜ˆ์ด๋ฉฐ, ์ž…๋ ฅ์ธ ๋งฅ๋ฝ ๋‹จ์–ด๋Š” fat, cat, on, the์ด๋ฉฐ ์ถœ๋ ฅ์ธ ํƒ€๊นƒ ๋‹จ์–ด๋Š” sat์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ์ž…์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ ์›-ํ•ซ ๋ฒกํ„ฐ์ด๋ฉฐ ์ง€๋ฉด ๊ด€๊ณ„์ƒ fat๊ณผ the๋Š” ์ƒ๋žต๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํˆฌ์‚ฌ์ธต(projection layer)์˜ ํฌ๊ธฐ๋Š” M์ธ๋ฐ ์ด๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๊ฒฐ๊ณผ์˜ ์ฐจ์›์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ M์€ 5์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ์ธต(input layer)์—์„œ์˜ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W์˜ ํฌ๊ธฐ๋Š” V x M์ž…๋‹ˆ๋‹ค. V๋Š” ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜์ด๋ฏ€๋กœ ์œ„ ๊ทธ๋ฆผ์—์„œ V = 7์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W์˜ ํฌ๊ธฐ๋Š” 7 x 5์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ์ถœ๋ ฅ์ธต์˜ W' ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” M x V, ์ฆ‰ 5 x 7์ž…๋‹ˆ๋‹ค. W์™€ W'๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํ–‰๋ ฌ์ž„์— ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. CBOW ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์€ ์ž…๋ ฅ ๋ฒกํ„ฐ(๋งฅ๋ฝ ๋‹จ์–ด)๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅ ๋ฒกํ„ฐ(ํƒ€๊นƒ ๋‹จ์–ด)๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ๊ณ„์†ํ•ด์„œ ํ•™์Šตํ•˜๋ฉฐ ์ด ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W์™€ W'์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ์ธต์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ์™€ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ณฑํ•ด์ง€๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ๋งฅ๋ฝ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ x๋ผ ํ‘œ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด x_cat (cat์— ๋Œ€ํ•œ ์›Ÿ-ํ•ซ ๋ฒกํ„ฐ)์€ 3๋ฒˆ์งธ ์ธ๋ฑ์Šค๋งŒ 1์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋‹ค 0์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W๋ฅผ ๊ณฑํ•ด์ฃผ๋ฉด W์˜ 3ํ–‰์— ํ•ด๋‹นํ•˜๋Š” ๋ฒกํ„ฐ๊ฐ€ ๋„์ถœ๋ฉ๋‹ˆ๋‹ค. ๊ทธ์ € ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W์—์„œ '์ž…๋ ฅ ๋ฒกํ„ฐ์—์„œ 1์ด ํฌํ•จ๋œ ์ธ๋ฑ์Šค'์— ํ•ด๋‹นํ•˜๋Š” ํ–‰์„ ์ถ”์ถœํ•˜๋Š” ์ž‘์—…์ผ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ์›-ํ•ซ ๋ฒกํ„ฐ x์—์„œ 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ธ๋ฑ์Šค๋ฅผ i๋ผ ํ•  ๋•Œ, ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W์˜ i๋ฒˆ์งธ ํ–‰์„ ๊ฐ€์ ธ์˜ค๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋’ค์—์„œ๋„ ๋งํ•˜๊ฒ ์ง€๋งŒ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W๊ฐ€ ๊ฒฐ๊ตญ ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ์ฆ‰ ์ข‹์€ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W๋ฅผ ์ž˜ ํ•™์Šตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ์ธ ๋งฅ๋ฝ ๋‹จ์–ด๊ฐ€ ์ด 4๊ฐœ์ด๋ฏ€๋กœ ๊ฐ ์ž…๋ ฅ ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W๋ฅผ ๊ณฑํ•œ ๋ฒกํ„ฐ v๊ฐ€ 4๊ฐœ ๋„์ถœ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ด ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท  ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌํ•ด์ง„ ๋ชจ๋“  v ๋ฒกํ„ฐ (v_fat, v_eat, v_on, v_the)๋ฅผ ๋”ํ•œ ๋’ค 4๋กœ ๋‚˜๋ˆ„์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 4๋Š” ์–ด๋–ป๊ฒŒ ๋‚˜์˜จ ๊ฐ’์ผ๊นŒ์š”? 2 x (window size)์ž…๋‹ˆ๋‹ค. ์•ž์„œ ์œˆ๋„ ์‚ฌ์ด์ฆˆ๋ฅผ 2๋กœ ์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํƒ€๊นƒ ๋‹จ์–ด ์•ž๋’ค๋กœ 2๊ฐœ์˜ ๋งฅ๋ฝ ๋‹จ์–ด๋ฅผ ์ž…๋ ฅ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 2 x 2 = 4์ด๋ฏ€๋กœ 4๋กœ ๋‚˜๋ˆ„์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ํ‰๊ท  ๋ฒกํ„ฐ v๋ฅผ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ท  ๋ฒกํ„ฐ v์˜ ํฌ๊ธฐ๋Š” M์ž…๋‹ˆ๋‹ค. (์ฆ‰ 5์ž…๋‹ˆ๋‹ค.) ์œ„์—์„œ ๊ตฌํ•œ ํ‰๊ท  ๋ฒกํ„ฐ v๋ฅผ ์ถœ๋ ฅ์ธต ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W'์™€ ๊ณฑํ•ฉ๋‹ˆ๋‹ค. ๊ณฑํ•ด์„œ ์–ป์–ด์ง„ z ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋Š” V (์ฆ‰ 7)์ž…๋‹ˆ๋‹ค. ์ด ๋ฒกํ„ฐ z์— ์†Œํ”„ํŠธ๋งฅ์Šค(Softmax)๋ฅผ ์ทจํ•ด์ฃผ๋ฉด ํ™•๋ฅ  ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๋ฒกํ„ฐ ๊ฐ’์ด ๊ตฌํ•ด์ง‘๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค๋ฅผ ์ทจํ•ด์ฃผ๋ฉด ๋ชจ๋“  ์›์†Œ์˜ ํ•ฉ์ด 1์ธ ์ƒํƒœ๋กœ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ด๋ผ ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค๋ฅผ ์ทจํ•ด์ค€ ๋ฒกํ„ฐ์™€ ์‹ค์ œ ํƒ€๊นƒ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ์†์‹ค ํ•จ์ˆ˜(Loss function)๋กœ cross-entropy๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. cross-entropy์— ๋Œ€ํ•ด ์ˆ˜์‹์„ ์ œ์™ธํ•˜๊ณ  ์ง๊ด€์ ์œผ๋กœ ์„ค๋ช…ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ํƒ€๊นƒ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” (0, 0, 0, 1, 0, 0, 0)์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋งˆ์ง€๋ง‰์— ์†Œํ”„ํŠธ๋งฅ์Šค๋ฅผ ์ทจํ•ด์ค€ ๋ฒกํ„ฐ๊ฐ€ (0, 0, 0, 1, 0, 0, 0)์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก cross-entropy ๊ฐ’์€ 0์ด ๋˜๊ณ , (0, 0, 0, 1, 0, 0, 0)๊ณผ ๋‹ฌ๋ผ์งˆ์ˆ˜๋ก 1์— ๊ฐ€๊นŒ์šด ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์†์‹ค ํ•จ์ˆ˜๋กœ cross-entropy๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์€ CBOW ์‹ ๊ฒฝ๋ง์„ ํ†ตํ•ด ์ตœ์ข…์ ์œผ๋กœ ๋„์ถœํ•œ ๋ฒกํ„ฐ๊ฐ€ ํƒ€๊นƒ ๋ฒกํ„ฐ์™€ ์ตœ๋Œ€ํ•œ ๋˜‘๊ฐ™์•„์ง€๊ฒŒ ํ•˜๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•™์Šต์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W์™€ W'๋ฅผ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต์ด ์ž˜ ๋˜๋ฉด W์™€ W'์˜ ๊ฐ’์€ ๊ฑฐ์˜ ๋น„์Šทํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ๊ฒƒ์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ๋กœ ์‚ฌ์šฉํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” W์™€ W'์˜ ํ‰๊ท ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต์„ ์ง„ํ–‰ํ• ์ˆ˜๋ก ๋งฅ๋ฝ์œผ๋กœ๋ถ€ํ„ฐ ํƒ€๊นƒ ๋‹จ์–ด๋ฅผ ์ž˜ ์ถ”์ธกํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W๊ฐ€ ๊ฐฑ์‹ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ํ•ด์„œ ์–ป์€ ๋ถ„์‚ฐ ํ‘œํ˜„ W์—๋Š” '๋‹จ์–ด์˜ ์˜๋ฏธ'๋„ ์ž˜ ๋…น์•„ ์žˆ์Šต๋‹ˆ๋‹ค. 2. Skip-gram CBOW์—์„œ๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ํ†ตํ•ด ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ–ˆ๋‹ค๋ฉด, Skip-gram์€ ์ค‘์‹ฌ ๋‹จ์–ด์—์„œ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์˜ˆ๋ฌธ์— ๋Œ€ํ•ด์„œ ๋™์ผํ•˜๊ฒŒ ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ 2์ผ ๋•Œ, ๋ฐ์ดํ„ฐ ์…‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์‹ ๊ฒฝ๋ง์„ ๋„์‹ํ™”ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋ฏ€๋กœ ํˆฌ์‚ฌ์ธต์—์„œ ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์€ ์—†์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋…ผ๋ฌธ์—์„œ ์„ฑ๋Šฅ ๋น„๊ต๋ฅผ ์ง„ํ–‰ํ–ˆ์„ ๋•Œ, ์ „๋ฐ˜์ ์œผ๋กœ Skip-gram์ด CBOW๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. Reference ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ ๊ท€ํ‰์ด ์„œ์žฌ ๋ธ”๋กœ๊ทธ (3) ์ด๋ฏธ์ง€ ์บก์…”๋‹ ๋ชจ๋ธ๋“ค ... 1) Show and Tell ๋„คํŠธ์›Œํฌ ํ•ต์‹ฌ ์•„์ด๋””์–ด 2015๋…„ ๊ตฌ๊ธ€์—์„œ ๊ณต๊ฐœํ•œ "Show and Tell: A Neural Image Caption Generator"์—์„œ ๊ณต๊ฐœํ•œ NIC ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์•ž์—์„œ ์†Œ๊ฐœ ๋“œ๋ฆฐ ์ด๋ฏธ์ง€ ์บก์…”๋‹ ์•„์ด๋””์–ด(์ด๋ฏธ์ง€ ์บก์…”๋‹์˜ ๋ฌธ์ œ๋ฅผ ๊ธฐ๊ณ„๋ฒˆ์—ญ์œผ๋กœ ์ƒ๊ฐํ•˜๊ฒ ๋‹ค)์˜ ์‹œ์ž‘์ด ๋œ ๋…ผ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์˜ ์ž„๋ฐฐ๋”ฉ์„ ์ถ”์ถœํ•˜๋Š” CNNs ๋„คํŠธ์›Œํฌ๋กœ๋Š” GoogleNet์„ ์ด์šฉํ•˜์˜€๊ณ  ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๋Š” RNNs ๋„คํŠธ์›Œํฌ๋กœ๋Š” Seq2Seq์„ ์ด์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์บก์…”๋‹ ๋„คํŠธ์›Œํฌ๋Š” NLP ๋ถ„์•ผ์™€ ํฐ ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค... ์ €๋Š” ์ปดํ“จํ„ฐ ๋น„์ „๋ณด๋‹ค NLP์— ๋” ๊ฐ€๊น๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค. ์ƒ์„ธ ๊ฐœ๋…๋“ค์„ ์„ค๋ช…๋“œ๋ฆฌ๊ธฐ์—๋Š” ์ง€์‹๋„ ๋ถ€์กฑํ•˜๊ณ  ์ปดํ“จํ„ฐ ๋น„์ „์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์„œ ๋ถ€๋“์ดํ•˜๊ฒŒ NLP๋ฅผ ์กฐ๊ธˆ์€ ์•Œ๊ณ  ์žˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ๋ฌธ์„œ๋ฅผ ๊ธฐ์ˆ ํ•ด ๋‚˜๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. NLP์˜ ์„ค๋ช…์ด ํ•„์š”ํ•˜์‹  ๋ถ„์€ ์œ„ํ‚ค๋…์Šค์˜ ๋”ฅ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ์„ ๊ฐ•์ถ”ํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์€ ๋‹จ์ˆœํ•œ ๋„คํŠธ์›Œํฌ์— ๋Œ€ํ•œ ์„ค๋ช…๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ ์ด๋ฏธ์ง€ ์บก์…”๋‹์—์„œ ์ƒ๊ฐ ๋ด์•ผ ํ•  ์ฃผ์ œ๋ฅผ ๋‹ค๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. "ํ‰๊ฐ€ ๋ฐฉ๋ฒ•(Metric)์— ๋Œ€ํ•œ ํ•œ๊ณ„"์™€ "๊ทธ๋Ÿผ์—๋„ ์ €์ž๋“ค์ด ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์ด์šฉํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ Metric"์ž…๋‹ˆ๋‹ค. ์ ์ ˆํ•œ Metric์ด ์—†์–ด ์ˆซ์ž๋กœ ์ •ํ™•ํ•œ ์„ฑ๋Šฅ์„ ํ‘œํ˜„ํ•˜์ง€๋Š” ๋ชปํ•˜๋‚˜ ์—ฌ๋Ÿฌ ๋ถˆ์™„์ „ํ•œ Metric์œผ๋กœ ์ธก์ •ํ•ด ๋ณด์•˜์„ ๋•Œ ์ด์ „ ๋„คํŠธ์›Œํฌ๋“ค์— ๋น„ํ•ด์„œ ๋งค์šฐ ํฐ ๋ฐœ์ „์„ ๋ณด์—ฌ์ค€ ๋…ผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ ๋„คํŠธ์›Œํฌ๋Š” ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์— ์‚ฌ์šฉ๋œ GoogleNet๊ณผ ๊ธฐ๊ณ„๋ฒˆ์—ญ(Machine Translation)์˜ Seq2Seq ๋ชจ๋ธ์„ ์ด์–ด์„œ ๋ถ™์ธ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค. CNN ์ธ์ฝ”๋”(GoogleNet) ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์— ์‚ฌ์šฉ๋œ GoogleNet์„ ๊ฑฐ์˜ ๊ทธ๋Œ€๋กœ ์ด์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ณผ ์ ํ•ฉ(Overfit)์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ImageNet ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜์—ฌ Pretrain ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ FC layer๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์›Œ๋“œ ์ž„๋ฐฐ๋”ฉ๊ณผ ๊ฐ™์€ ์ฐจ์›(2048โ†’512)์œผ๋กœ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๊ณ , ๊ทธ ์™ธ์˜ ๋„คํŠธ์›Œํฌ ์•ž ๋ถ€๋ถ„ Weight๋“ค์€ Freezing ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ถ”์ถœ(Extraction) ๋œ ํ”ผ์ฒ˜(feature)๋“ค์€ ๊ธฐ์กด Seq2Seq ๋„คํŠธ์›Œํฌ์˜ Context Vector์™€ ๋™์ผํ•œ ๊ฐœ๋…์œผ๋กœ ์ด์šฉ๋ฉ๋‹ˆ๋‹ค. RNN ๋””์ฝ”๋”(Seq2Seq) ์ด ๋ถ€๋ถ„๋„ ๊ธฐ์กด Seq2Seq์˜ ์ธ์ฝ”๋” ๋ถ€๋ถ„์„ ๊ทธ๋Œ€๋กœ ์ด์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.(^^;;) ์ด๋ฏธ์ง€ ์ž„๋ฐฐ๋”ฉ๊ณผ ์ฐจ์›์„ ๋งž์ถ”์–ด Label ์›ํ•ซ๋ฒกํ„ฐ๋ฅผ ์›Œ๋“œ์ž„๋ฐฐ๋”ฉ ์ดํ›„ 512์ฐจ์›์ด ๋˜๋„๋ก ์ •์˜ํ•ด ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ๋Š” ๊ธฐ์กด ๋‹ค๋ฅธ Task์˜ SOTA ๋„คํŠธ์›Œํฌ(์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์˜ GoogleNet, ๊ธฐ๊ณ„๋ฒˆ์—ญ์˜ Seq2Seq)๋ฅผ ์ด์–ด ๋ถ™์˜€์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ๋„คํŠธ์›Œํฌ๋ฅผ ์ž˜ ์•„์‹ ๋‹ค๋ฉด ์–ด๋ ค์›€ ์—†์ด ์ดํ•ด๋˜์‹ค ๊ฒ๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ํŠธ๋ ˆ์ด๋‹ ์†์‹ค ํ•จ์ˆ˜(Loss function) ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•œ ์†์‹ค ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๊ฐ ์Šคํ…์—์„œ ์˜ˆ์ธกํ•œ(Prediction) ์›Œ๋“œ์ž„๋ฐฐ๋”ฉ๊ณผ ์ง ์ง€์–ด์ง„ Label์˜ ์›Œ๋“œ์ž„๋ฐฐ๋”ฉ(S, embedding word) ๊ณผ์˜ ๋ถ„ํฌ(Distribution) ์ฐจ์ด์˜ ํ•ฉ์„ ์ด์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ์—์„œ๋Š” ์œ„ ์‹์—๋‹ค -1์„ ๊ณฑํ•˜์—ฌ ์œ ์‚ฌ๋„๊ฐ€ ๋†’์„์ˆ˜๋ก ์†์‹ค ๊ฐ’(Loss)๊ฐ€ ๊ฐ์†Œํ•˜๋„๋ก ํ•˜์—ฌ ํ›ˆ๋ จํ•ฉ๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ ๋น„์ „์—์„œ๋Š” ์กฐ๊ธˆ ์–ด์ƒ‰ํ•œ ์†์‹คํ•จ ์ˆ˜์ง€๋งŒ, ๊ธฐ๊ณ„๋ฒˆ์—ญ(Machine Translation)์—๋Š” ๋งŽ์ด ์“ฐ๋Š” ์†์‹คํ•จ ์ˆ˜์ž…๋‹ˆ๋‹ค. (์ถ”๊ฐ€์ ์ธ ํ•™์Šต์ด ํ•„์š”ํ•˜์‹  ๋ถ„์€ negative log likelihood ๊ฒ€์ƒ‰์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค) ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์—…๋ฐ์ดํŠธ๋˜๋Š” ๊ฐ€์ค‘์น˜๋“ค์€ ์„ธ ๊ฐ€์ง€ ๊ทธ๋ฃน์ž…๋‹ˆ๋‹ค. "CNN์˜ ์ตœ์ƒ๋‹จ ๋ ˆ์ด์–ด", "์›Œ๋“œ์ž„๋ฐฐ๋”ฉ ๋ฒกํ„ฐ", "LSTM์˜ ํŒŒ๋ผ๋ฏธํ„ฐ" (CNN ์ธ์ฝ”๋”์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์€ Pretrain ๋ฉ๋‹ˆ๋‹ค) ๋ฐ์ดํ„ฐ ์…‹(Dataset) Pascal VOC2008, Flickr8k, Flickr30k, MSCOCO, SBU data๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. SBU๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋‘ 5๊ฐœ์˜ correct sentence ๋ผ๋ฒจ์ด ์žˆ์Šต๋‹ˆ๋‹ค. SBU ๋ฐ์ดํ„ฐ๋Š” Flickr๋ผ๋Š” ์‚ฌ์ดํŠธ์— ์ด๋ฏธ์ง€๋ฅผ ์˜ฌ๋ฆด ๋•Œ ๊ฐ™์ด ์˜ฌ๋ฆฐ ์ด๋ฏธ์ง€ ์„ค๋ช…์œผ๋กœ ํ•™์Šต ์‹œ ์ผ์ข…์˜ noise๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. (์ธํ„ฐ๋„ท์— ์‚ฌ์ง„์„ ์˜ฌ๋ฆฌ๋ฉด์„œ ์•„๋ฌด๋ ‡๊ฒŒ ๋ผ์ ๊ฑฐ๋ฆฐ ์„ค๋ช…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์™œ noise๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ๋Œ€์ถฉ ์ง์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค) ๋‚˜๋จธ์ง€ 4๊ฐœ์˜ data set์œผ๋กœ ํ•™์Šต์„ ํ•˜๊ณ  Pascal ๋ฐ์ดํ„ฐ๋Š” test๋ฅผ ์œ„ํ•ด์„œ๋งŒ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ธํผ๋Ÿฐ์Šค(inference) ๋„คํŠธ์›Œํฌ ํŠธ๋ ˆ์ด๋‹๊ณผ ๊ด€๋ จ๋œ ๋ถ€๋ถ„์€ ์•„๋‹ˆ์ง€๋งŒ, ์‹ค์ œ ๋ชจ๋ธ ํ™œ์šฉ ์‹œ์— ์˜ˆ์ธก(Prediction) ๋ฌธ์žฅ์˜ ํ›„๋ณด(Candidate)๋“ค์„ ๊ตฌํ•˜๋Š” BeamSearch ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. Seq2Seq์—์„œ๋Š” step ๋‹น ๋ฌธ์žฅ์˜ ๊ฐ ๋‹จ์–ด๋“ค์„ ์˜ˆ์ธกํ•˜๋Š”๋ฐ, ๊ฐ step์—์„œ์˜ ์ตœ๊ณ ์˜ ์˜ˆ์ธก์ด ์ข…ํ•ฉ์ ์œผ๋กœ๋Š” ์ตœ๊ณ ๊ฐ€ ์•„๋‹ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฌธ์žฅ 2๊ฐœ๋ฅผ ์˜ˆ๋กœ ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค. โ‘  [๋‚˜๋Š”, ๋ฒ„์Šค๋ฅผ, ํƒ”์Šต๋‹ˆ๋‹ค] โ‘ก [๋‚˜๋Š”, ๋ฒ„์Šค๋Š”, ๊ทธ๊ฒƒ์€] ๋‘ ๋ฌธ์žฅ์—์„œ ๋ถ„๋ช… ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์˜ ๋‹จ์–ด๊ฐ€ ๋” ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ์ž˜ ์„ค๋ช…ํ•˜์˜€์œผ๋‚˜ ๊ฐ step์—์„œ ๊ฐ€์žฅ ์ตœ๊ณ  ํ™•๋ฅ ์˜ ์›Œ๋“œ๋ฅผ ํƒํ•˜๋ฉด ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ๊ณผ ๊ฐ™์ด ์˜ˆ์ธก๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ๋…ผ๋ฌธ์—์„œ๋Š” ๊ฐ step์—์„œ 20๊ฐœ์˜ ํ›„๋ณด๊ตฐ์„ ๋ฝ‘์•„์„œ ์—ฌ๋Ÿฌ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ตœ์ข…์ ์œผ๋กœ๋Š” ๊ฐ€์žฅ ์ข‹์€ ์‹œํ€€์Šค๋ฅผ ์„ ํƒํ•˜๋Š” ๋น” ์„œ์น˜(BeamSearch) ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ํ‰๊ฐ€ ๋ชจ๋ธ ํ‰๊ฐ€์˜ ์–ด๋ ค์›€์— ๋Œ€ํ•ด์„œ ๊ณผ์—ฐ ์‚ฌ์ง„์„ ์ž˜ ์„ค๋ช…ํ•˜๋Š” ๋ฌธ์žฅ์—์„œ Ground Truth๊ฐ€ ์กด์žฌํ• ๊นŒ์š”? ์œ„ ์‚ฌ์ง„์˜ GT(Ground Truth) ์บก์…”๋‹์€ ๋ฌด์—‡์ผ๊นŒ์š”? โ‘  ์‚ฌ๋žŒ 3๋ช…์ด ๊ธธ์„ ๊ฑท๋Š”๋‹ค. โ‘ก ์‚ฌ๋žŒ๋“ค์ด ์ฐจ๋„ ์˜†์„ ๊ฑธ์–ด๊ฐ„๋‹ค. โ‘ข ๋‚จ์ž๊ฐ€ ๋‚˜๋ฌด๋กœ ๋œ ๊ธธ์„ ๊ฑด๋„ˆ๊ฐ„๋‹ค. ์ด๋ฏธ์ง€ ์บก์…”๋‹ Task์—์„œ๋Š” ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์™€๋Š” ์–ด๋–ป๊ฒŒ ๋ณด๋ฉด Ground Truth๋ผ๋Š” ๊ฒƒ์ด ์—†์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ๊นŒ์ง€๋Š” ์‚ฌ๋žŒ์˜ ์ฃผ๊ด€์ ์ธ ํ‰๊ฐ€ ๋ฐฉ๋ฒ• ์™ธ์—๋Š” 100% ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•  Metric์ด ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ์ €์ž๋“ค์€ ๋‹ค์–‘ํ•œ ๊ด€์ ์—์„œ ์—ฌ๋ ค๊ฐ€์ง€ ํ‰๊ฐ€ ์ฒ™๋„(BLUE, METEOR, Cider, recall@k...)๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. Generation Result BLEU, METEOR, CIDER๋ผ๋Š” ์ž๋™, ์ •๋Ÿ‰์  ์ง€ํ‘œ๋กœ ํ‰๊ฐ€ํ•œ ๊ฒฐ๊ณผ ๋†’์€ ์„ฑ๋Šฅ์„ ๋ณด์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ Table 1,2์—์„œ Human์€ 5๊ฐœ์˜ correct sentence label ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด ๋‚˜๋จธ์ง€ 4๊ฐœ์™€ ๋น„๊ตํ•˜์—ฌ ๋„์ถœํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. Generation Divsersity Discussion ๋ชจ๋ธ์ด ์ƒ์„ฑํ•˜๋Š” caption ์ค‘ ํ•™์Šต ๋ฐ์ดํ„ฐ์— ํฌํ•จ๋˜์ง€ ์•Š์€ ์ƒˆ๋กœ์šด caption์ด ๋งŒ๋“ค์–ด์งˆ ํ™•๋ฅ ์€ ์–ผ๋งˆ๋‚˜ ๋ ๊นŒ์š”? ์ €์ž๋“ค์€ ๋ชจ๋ธ์ด ์ƒ์„ฑํ•œ ์ƒˆ๋กœ์šด caption์˜ ์˜ˆ์‹œ๋ฅผ ์•„๋ž˜ Table 3์—์„œ ์ œ์‹œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ input image์— ๋Œ€ํ•ด BLEU score๊ฐ€ ๊ฐ€์žฅ ์ข‹์€ caption์„ ๋ฝ‘์•„๋ณด๋ฉด ์•ฝ 80%๋Š” ๊ธฐ์กด ํ•™์Šต ๋ฐ์ดํ„ฐ์— ํฌํ•จ๋˜์–ด ์žˆ๋˜ ๋ฌธ์žฅ๋“ค์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ BLEU score๊ฐ€ ๋†’์€ ์ˆœ์œผ๋กœ 15๊ฐœ์˜ caption์„ ๋ฝ‘์•„๋ณด๋ฉด ์•ฝ 50% ์ •๋„๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์žฅ์ด์—ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Human Evaluation ์‚ฌ๋žŒ์„ ์ด์šฉํ•ด NIC๋ฅผ ํฌํ•จํ•œ ๋‹ค์–‘ํ•œ ML ๋ชจ๋ธ์—์„œ ์ƒ์„ฑ๋œ Caption์„ ํ‰๊ฐ€ํ•˜๋„๋ก ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ๋“ค์—์„œ ์ƒ์„ฑํ•œ Caption์—์„œ ๋žญํ‚น์„ ๋งค๊ฒจ ํ‰๊ฐ€ํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (R@1์€ NO.1์œผ๋กœ ๊ผฝํžŒ ์บก์…˜์˜ ๊ฐœ์ˆ˜๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค) ๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ ๋Œ€๋น„ํ•ด์„œ๋Š” ์•„์ฃผ ํฐ ์„ฑ๋Šฅ ์ฐจ์ด๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์ด NIC์—์„œ ๋งŒ๋“ค์–ด์ง„ ์บก์…”๋‹์— ์ฃผ๊ด€์ ์ธ ์ ์ˆ˜๋ฅผ ๋งค๊ฒจ ๋ถ„๋ฅ˜ํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์—์„œ ๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. "Described without errors"์— ํ•ด๋‹นํ•˜๋Š” caption๋“ค์€ ๊ต‰์žฅํžˆ ์ •ํ™•ํ•˜์ง€๋งŒ, (1์—ด 2๋ฒˆ์งธ์˜ frisbee๋Š” ์–ด๋–ป๊ฒŒ ์ €๊ฒƒ์„ ์ดํ•ดํ–ˆ์ง€๋ผ๋Š” NIC์˜ ๋Œ€๋‹จํ•จ๋„ ๋ณด์ž…๋‹ˆ๋‹ค) "Unrelated to the image"์— ํ•ด๋‹นํ•˜๋Š” caption์€ ์–ด๋””๋ฅผ ๋ณด๊ณ  ์ด๋Ÿฐ caption์ด ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ์˜์•„ํ•  ์ •๋„์ž…๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์ด ๋งŒ๋“  ์บก์…”๋‹๊ณผ NIC๊ฐ€ ๋งŒ๋“ค์–ด๋‚ธ ์บก์…”๋‹์„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์ฑ„์ ํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Flickr-8k: GT (Ground truth)์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ NIC (Neural Image Caption)์˜ ์„ฑ๋Šฅ์ด ํ˜„์ €ํžˆ ๋–จ์–ด์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” NIC ์„ฑ๋Šฅ์˜ ํ•œ๊ณ„๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ž๋ฃŒ์ด๊ธฐ๋„ ํ•˜์ง€๋งŒ, ์ž๋™&์ •๋Ÿ‰์  ํ‰๊ฐ€ ์ง€ํ‘œ์ธ BLEU์˜ ํ•œ๊ณ„๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. (BLUE-4์—์„œ๋Š” ์‚ฌ๋žŒ์ด ๋งŒ๋“  ์บก์…”๋‹๋ณด๋‹ค NIC์—์„œ ๋งŒ๋“ค์–ด๋‚ธ ์บก์…˜์˜ ์ ์ˆ˜๊ฐ€ ์˜คํžˆ๋ ค ๋” ๋†’์•˜์Šต๋‹ˆ๋‹ค) ๋„คํŠธ์›Œํฌ์˜ ์žฅ๋‹จ์  ๋ฐ ํ•œ๊ณ„์  Show and Tell ๋…ผ๋ฌธ์€ "CNN ๋„คํŠธ์›Œํฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฝ‘์•„๋‚ธ Feature๋ฅผ NLP ๋ถ„์•ผ์˜ Context Vector๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค"๋ผ๋Š” ํฐ ์•„์ด๋””์–ด์˜ ์‹œ์‚ฌ์ ์„ ์ค€ ๋…ผ๋ฌธ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. (๋„ˆ๋ฌด ๊ฐ•๋ ฅ) ๋…ผ๋ฌธ์„ ์“ธ ๋‹น์‹œ('15)์˜ SOTA์ธ GoogleNet๊ณผ Seq2Seq๋ฅผ ์ด์šฉํ–ˆ์œผ๋‹ˆ, ์ตœ๊ทผ์˜ SOTA๋“ค์˜ ๊ฒฐํ•ฉ์œผ๋กœ๋Š” ๋” ์ข‹์€ ์„ฑ๋Šฅ์˜ ๋„คํŠธ์›Œํฌ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋น„์Šทํ•œ ๋งฅ๋ฝ์—์„œ Seq2Seq์˜ ๋‹จ์ ์ธ ํ•œ์ •๋œ ์ˆซ์ž์˜ Context Vector ๋ฌธ์ œ๋„ ์œ ์ „ ๋ฐ›์•˜๊ณ ์š”... (์ด ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Show, Attention and Tell ๋…ผ๋ฌธ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค) ๊ฐœ์ธ์ ์œผ๋กœ๋Š” ๋…ผ๋ฌธ์„ ์ฝ๊ณ ๋„ ํ€˜์Šค์ฒœ์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋‚จ์Šต๋‹ˆ๋‹ค๋งŒ '15๋…„ ๋…ผ๋ฌธ์ด๋ฏ€๋กœ ๋‹ค์Œ ๋…ผ๋ฌธ๋“ค์—์„œ ์ด๋Ÿฌํ•œ ์ ์ด ํ•ด๊ฒฐ๋˜๋ฆฌ๋ผ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๋‹ค. ์บก์…”๋‹์—์„œ ์‚ฌ๋ฌผ(Object) ๊ฐ„์˜ ์—ฐ๊ด€๊ด€๊ณ„ ์ •๋ณด๋Š” ๋””์ฝ”๋”(Seq2Seq)์—์„œ ์ถ”๊ฐ€๊ฐ€ ๋˜๋Š” ๊ฑธ๊นŒ์š”? ์‚ฌ์ „ํ›ˆ๋ จ(Pretrain) ๋œ ์›Œ๋“œ ์ž„๋ฐฐ๋”ฉ(Word Embedding)์€ ์™œ ์„ฑ๋Šฅ์ด ์ข‹์ง€ ๋ชปํ•˜์˜€์„๊นŒ์š”? ์‚ฌ์ „ํ›ˆ๋ จ(Pretrain) ๋œ GoogleNet์ด ์•„๋‹ˆ๋ผ ์•„์˜ˆ ์ฒ˜์Œ๋ถ€ํ„ฐ Train ํ–ˆ์œผ๋ฉด ์ด๋ฏธ์ง€์—์„œ ์—ฐ๊ด€๊ด€๊ณ„์˜ ํŠน์ง•์„ ์žก์•„ ๋” ์ข‹์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”? ๋ ˆํผ๋Ÿฐ์Šค https://uding.tistory.com/20 https://yseon99.tistory.com/110 2) Show, Attend and Tell ์ด์ „ ๋…ผ๋ฌธ์ธ Show and Tell์€ ๋‹จ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. CNN์˜ FC layer๋ฅผ ํ†ต๊ณผํ•œ context vector๋ฅผ ์ด์šฉํ•˜์—ฌ image captioning์„ ์ง„ํ–‰ํ•˜๋ฏ€๋กœ ํ•ญ์ƒ ๊ฐ™์€ context vector๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์บก์…”๋‹์„ ์ƒ์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ FC layer๋ฅผ ํ†ต๊ณผํ•˜๋ฉด์„œ ๊ธฐ์กด ์ด๋ฏธ์ง€์—์„œ ๊ณต๊ฐ„ ์ •๋ณด๋ฅผ ์žƒ์–ด๋ฒ„๋ ค ์ด๋ฏธ์ง€์˜ ์–ด๋Š ๋ถ€๋ถ„์„ ๋ณด๊ณ  ์บก์…”๋‹์„ ์ƒ์„ฑํ–ˆ๋Š”์ง€ ๋„คํŠธ์›Œํฌ ๋ถ„์„์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ ์„ ๋ณด์™„ํ•˜์—ฌ ์ €์ž๋“ค์ด ๋ฐœํ‘œํ•œ Show, Attention and Tell(2016) ๋…ผ๋ฌธ์—์„œ๋Š” ์ด๋ฏธ์ง€์—์„œ ์ถ”์ถœ๋œ ํ”ผ์ฒ˜(Extract Feature)๋“ค์„ ํ•œ ๊ฐœ์˜ ๊ณ ์ •๋œ ๋ฒกํ„ฐ(Context Vector)๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ์–ดํ…์…˜(Attention) ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ด์šฉํ•˜์—ฌ 1๊ฐœ์˜ ์›Œ๋“œ๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์Šคํ…๋งˆ๋‹ค ๋‹ค๋ฅธ ๋ถ„ํฌ์˜ ๋ฒกํ„ฐ๋“ค์„ ์ƒ์„ฑํ•˜์—ฌ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์œผ๋กœ ์ด์šฉํ•˜๋ก ๋„คํŠธ์›Œํฌ๋ฅผ ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ๋” ๋‚˜์•„๊ฐ€ ์–ดํ…์…˜์„ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•(Soft/Hard)๋กœ ์ ์šฉํ•˜์—ฌ ๊ฐ๊ฐ์˜ ์„ฑ๋Šฅ ๋ฐ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ ๋…ผ๋ฌธ ์ „์ฒด(~22 ํŽ˜์ด์ง€)์—์„œ ๋„คํŠธ์›Œํฌ์˜ ์ „์ฒด ๊ตฌ์กฐ๊ฐ€ ๋‚˜ํƒ€๋‚œ Figure๋Š” ๋”ฑ ํ•œ ๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด์ „ Show and Tell ๋…ผ๋ฌธ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋„คํŠธ์›Œํฌ์˜ ๊ตฌ์กฐ๊ฐ€ ํ˜์‹ ์ ์ด๊ณ  ๋…์ฐฝ์ ์ธ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค์–ด๋ƒˆ๋‹ค๊ธฐ๋ณด๋‹ค ๊ธฐ๊ณ„๋ฒˆ์—ญ์—์„œ ์กด์žฌํ–ˆ๋˜ ๋„คํŠธ์›Œํฌ์™€ ๊ธฐ๋ฒ•๋“ค์„ ์ž˜ ์—ฐ๊ฒฐ์‹œ์ผœ์„œ ์ด๋ฏธ์ง€ ์บก์…˜ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.(์ œ ์ƒ๊ฐ..) Figure1์—์„œ 1,2,4๋ฒˆ์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ์€ Show and Tell๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ 3๋ฒˆ ์ˆœ์„œ์ธ RNN with attention์ด ๊ฐ€์žฅ ์ƒ์†Œํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๋ถ€๋ถ„์„ ์ง‘์ค‘ํ•˜์—ฌ ์†Œ๊ฐœํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜(Attention mechanism) ๋…ผ๋ฌธ์—์„œ๋Š” CNNs๋ฅผ ์ด์šฉํ•ด์š” ์ƒ์„ฑ๋œ ํ”ผ์ฒ˜ ๋งต(Feature map)์„ ์–ดํ…์…˜์„ ์ด์šฉํ•˜์—ฌ ๋งค ์Šคํƒœํ”„๋งˆ๋‹ค RNNs์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ณผ๊ฑฐ Show and Tell ๋…ผ๋ฌธ์—์„œ๋„ ํ”ผ์ฒ˜ ๋ฒกํ„ฐ(Feature Vector)๋ฅผ ๋งค ์Šคํ…๋งˆ๋‹ค ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, ๊ณผ ์ ํ•ฉ(Overfit)์ด ๋ฐœ์ƒํ•˜์—ฌ ํ•ด๋‹น ๊ณผ์ •์„ ์‚ญ์ œํ•˜์˜€๋‹ค๊ณ  ์ €์ž๋“ค์„ ๋งํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งค๋ฒˆ ์ฃผ์–ด์ง€๋Š” ์ด๋ฏธ์ง€์˜ ์ •๋ณด๋ฅผ ํ•ญ์ƒ ๋™์ผํ•œ ํ”ผ์ฒ˜ ๋ฒกํ„ฐ(Feature Vector)๊ฐ€ ์•„๋‹ˆ๋ผ ๋งค๋ฒˆ ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜๋ฅผ ๊ณ ๋ คํ•œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ด์šฉํ•œ ํ”ผ์ฒ˜ ๋ฒกํ„ฐ๋ผ๋ฉด ๊ฒฐ๊ณผ๋Š” ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. (๋ฐœํ€„์˜ ์ด๋ฏธ์ง€์ด์ง€๋งŒ) ์œ„์˜ ๊ทธ๋ฆผ์„ ์ด์šฉํ•ด์„œ ์„ค๋ช…ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ”ผ์ฒ˜ ๋งต(3X3X128)์„ LSTM ์…€์— ๋„ฃ์–ด ์ฒซ ๋ฒˆ์งธ ์›Œ๋“œ ์ž„๋ฐฐ๋”ฉ์„ ๊ฐ€์žฅ ์ž˜ ์œ ์ถ”ํ•ด ๋‚ผ ์ˆ˜ ์žˆ๋Š” ์–ดํ…์…˜ ๋ถ„ํฌ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ถ„ํฌ๋Š” ํ”ผ์ฒ˜ ๋งต๊ณผ ๊ฐ™์€ ์ฐจ์›(3X3)์ด๋‚˜ ์ฑ„๋„์ˆ˜๋Š” 1๋กœ ๊ตฌํ•ด์ง‘๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ•œ ์–ดํ…์…˜ ๋ถ„ํฌ 1์„ ํ”ผ์ฒ˜ ๋งต์˜ ๊ฐ ์ฑ„๋„๊ณผ ๊ณฑํ•˜์—ฌ 1X128 ์ฐจ์›์˜ Weighted ํ”ผ์ฒ˜ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฒซ ๋ฒˆ์งธ ์›Œ๋“œ ์ž„๋ฐฐ๋”ฉ(๋ฌธ์žฅ์˜ ์‹œ์ž‘์„ ์•Œ๋ ค์ฃผ๋Š” ์›Œ๋“œ์ž„๋ฐฐ๋”ฉ)๊ณผ ๊ฐ™์ด LSTM ์…€๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. LSTM์€ 2๊ฐœ์˜ Matrix๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์˜ˆ์ธก ์›Œ๋“œ์™€ ๋‹ค์Œ ์›Œ๋“œ์˜ ์–ดํ…์…˜ ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. (์ด๋ ‡๊ฒŒ ๊ฐ ์Šคํ…๋งˆ๋‹ค ๋‹ค์Œ ์›Œ๋“œ๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ์„œ๋กœ ๋‹ค๋ฅธ ์–ดํ…์…˜ ๋ถ„ํฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค) LSTM ์…€์˜ ์ž…์žฅ์—์„œ๋Š” ์ด์ „ ์›Œ๋“œ์™€ ์˜ˆ์ธกํ•  ์›Œ๋“œ์˜ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” Context Vector๋ฅผ ์ž…๋ ฅ๋ฐ›๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” ์œ„์™€ ๊ฐ™์€ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜(Soft attention)์„ ๋” ๋ณด์™„ํ•˜์—ฌ ์ถ”๊ฐ€์ ์œผ๋กœ Hard ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. RNN with Attention (Visual Attention) Attention์— ๋Œ€ํ•œ ๊ฐœ๋…์€ ์—ฌ๊ธฐ์— ๋”ฐ๋กœ ์ •๋ฆฌํ•˜๊ณ  ์žˆ์œผ๋‹ˆ, ํ•ด๋‹น ๋‚ด์šฉ์„ ๋จผ์ € ์ฝ์–ด๋ณด์‹œ๊ธธ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์ €๋Š” Show, Attentoin and Tell ๋…ผ๋ฌธ์— ๋‚˜์˜จ Visual Attention์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. (๋…ผ๋ฌธ์ด ์ƒ๋‹นํžˆ ๋ถˆ์นœ์ ˆํ•˜๊ณ  ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋Š” ์ž๋ฃŒ๊ฐ€ ๋งŽ์ง€ ์•Š์•„ ์ •ํ™•๋„๊ฐ€ ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์–‘ํ•ด ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.) ์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๊ธฐ์กด Show and tell ๋…ผ๋ฌธ์—์„œ LSTM์œผ๋กœ ์ž…๋ ฅ๋˜๋Š” context vector๋Š” CNN์˜ FC layer๋ฅผ ํ†ต๊ณผํ•œ vector์˜€์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ Show, attend and tell ๋…ผ๋ฌธ์—์„œ๋Š” CNN์˜ Feature Map (L x D) ์„ LSTM์— ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” CNN ๋ชจ๋ธ๋กœ VGG 16์„ ์‚ฌ์šฉํ–ˆ๊ณ  14 x 14 x 256 ํฌ๊ธฐ์˜ Feature map์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. 14 x 14๋Š” flatten ์‹œ์ผœ์„œ 196 x 256 ํฌ๊ธฐ์˜ feature map๋ฅผ LSTM์— ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ Feature Map์„ ์ด์šฉํ•˜์—ฌ ์–ด๋–ป๊ฒŒ Attention Value๋ฅผ ๋งŒ๋“ค์–ด ๋‚ด๋Š”์ง€ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. L x D ํฌ๊ธฐ์˜ Feature map์„ 0๋ฒˆ์งธ Hidden cell h0์— ๋„ฃ์–ด ์ฒซ ๋ฒˆ์งธ attention value a1 (L x 1) ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ attention value์™€ Feature Map์„ ๊ณฑํ•˜์—ฌ ํ•˜๋‚˜์˜ Weighted feature z1๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ•ด์ง„ Weighted feature z1๊ณผ First word(y1)๋ฅผ 1๋ฒˆ์งธ Hidden cell h1์˜ Input์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 1๋ฒˆ์งธ Hidden cell h1์˜ Output์œผ๋กœ๋Š” 2๊ฐ€์ง€๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” 0๋ฒˆ์งธ Hidden cell h0์˜ Output๊ณผ ๊ฐ™์€ Attention value a2๊ฐ€ ๋‚˜์˜ค๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” Prediction Word์˜ ์›ํ•ซ ๋ฒกํ„ฐ distribution์ธ d1์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ์žฌ๊ท€์ ์œผ๋กœ ๋ฐ˜๋ณตํ•˜์—ฌ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์บก์…”๋‹์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ํ•ด๋‹น ๋…ผ๋ฌธ์—์„œ๋Š” attention value a๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์„ ๋‘ ๊ฐ€์ง€ ์ œ์‹œํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ๊ฐ๊ฐ์„ soft attention, hard attention์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. Soft attention์˜ ์ˆ˜์‹์  ์ดํ•ด Soft attention์€ ์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์“ฐ๋Š” attention ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋ฏธ๋ถ„๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ์ด๋ฏ€๋กœ Back propagation์„ ํ†ตํ•ด ํ•™์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, Hard Attention์€ ๋ฏธ๋ถ„ ๋ถˆ๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ๋กœ ํ•™์Šต์„ ์œ„ํ•ด ๊ฐ•ํ™” ํ•™์Šต์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค Hard attention์€ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต๊ณ  ๋งŽ์ด ์“ฐ์ด๋Š” ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๊ธฐ์— ์„ค๋ช…์„ ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. * i, f, c, o, h = input, forget, memory, output, hidden state of LSTM E = embedding matrix y = output word * z = context vector, capturing the visual information associated with a particular input location* LSTM cell์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด ์ด๋ ‡๊ฒŒ ๋ณต์žกํ•˜๊ฒŒ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ LSTM ๊ตฌ์กฐ์™€ ๋น„์Šทํ•˜๊ฒŒ input, forget, output gate๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ ๊ฐ gate๋กœ ๋“ค์–ด๊ฐ€๋Š” ์š”์†Œ๋“ค์ด ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. h_t-1: t-1 ๋ฒˆ์งธ cell์—์„œ ์ถœ๋ ฅ๋œ hidden state Ey_t-1: t-1 ๋ฒˆ์งธ cell์—์„œ ์ถœ๋ ฅ๋œ output word y_t-1์˜ Embedding matrix Z_t: t ๋ฒˆ์งธ context vector z ๋ฅผ input์œผ๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ context vector z๋Š” "capturing the visual information associated with a particular input location", ์ฆ‰ feature map์˜ ํŠน์ • ์˜์—ญ์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” vector์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์˜ ํ•ต์‹ฌ์€ context vector z๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. a๋Š” feature map L x D์—์„œ ๋ฝ‘์•„๋‚ธ Vector์ž…๋‹ˆ๋‹ค. a์˜ ํฌ๊ธฐ๋Š” D์ด๊ณ  ์ด L ๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. t ๋ฒˆ์งธ LSTM cell์— ๋“ค์–ด๊ฐˆ ์•ŒํŒŒ๋Š” L ๊ฐœ์˜ a์™€ h_t-1 ์‚ฌ์ด์—์„œ ๊ตฌํ•œ attention value์ด๋ฉฐ ๊ฐ ์•ŒํŒŒ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•˜๋ฉด 1์ด ๋ฉ๋‹ˆ๋‹ค. f_att = attention value๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜์ด๊ณ , Soft attention์€ ์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ ์“ฐ๋Š” attention ๊ฐœ๋…์ด๋‹ˆ f_att๊ฐ€ ์–ด๋–ค ์‹์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ํ•จ์ˆ˜์ธ์ง€ ์•„์‹ค ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ•œ a์™€ ์•ŒํŒŒ๋ฅผ ๊ณฑํ•˜์—ฌ context vector z๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ฒฐ๊ณผ ๋ถ„์„ Soft, Hard Attention ๋„คํŠธ์›Œํฌ์—์„œ ์บก์…”๋‹์„ ํ•  ๋•Œ์˜ ์–ดํ…์…˜ ๋ถ€๋ถ„์„ ์‹œ๊ฐํ™” ํ•œ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. Soft๋Š” ์–ดํ…์…˜ ํ•œ ๋ถ€๋ถ„์ด ๋ถ„ํฌ์˜ ํ˜•ํƒœ๋ฅผ ๋ณด์ด๊ณ , Hard๋Š” ์ฐธ๊ณ ํ•œ ๋ถ€๋ถ„์ด 1๊ณผ 0์œผ๋กœ ํ™•์‹คํ•˜๊ฒŒ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋„ค์š”. ํ•˜์ง€๋งŒ ๊ฐ ์ด๋ฏธ์ง€์—์„œ ์–ดํ…์…˜ ์˜์—ญ์ด ๋™์ผํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์€ ์กฐ๊ธˆ ๋” ์ƒ๊ฐํ•ด์•ผ ํ•  ๋ถ€๋ถ„์œผ๋กœ ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค. Attention with Caption ๋„คํŠธ์›Œํฌ์—์„œ ์บก์…˜์„ ์ƒ์„ฑํ•  ๋•Œ ์ด๋ฏธ์ง€์—์„œ ์–ดํ…์…˜ ํ•œ ๋ถ€๋ถ„์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ด์šฉํ•˜๋‹ˆ NIC์™€ ๋‹ค๋ฅด๊ฒŒ ํ•ด์„ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์ด ํฐ ์žฅ์ ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” ์ž˜๋ชป ์ƒ์„ฑํ•œ ์บก์…”๋‹์˜ ์‚ฌ๋ก€๋“ค๋„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ์„ฑ๋Šฅ ๋„คํŠธ์›Œํฌ์˜ ์„ฑ๋Šฅ์€ NIC๋ณด๋‹ค ๋ชจ๋‘ ๋‹ค ๋†’์€ ๊ฒƒ์œผ๋กœ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Soft์™€ Hard์—์„œ๋Š” Hard Attention์„ ์ ์šฉํ•œ ๋ชจ๋ธ์ด ์„ฑ๋Šฅ์ด ๋” ์ข‹๊ฒŒ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์บก์…”๋‹์€ NLP์™€ ๊ฒฐํ•ฉ๋œ ๋‚ด์šฉ์ด๋ผ ์ปดํ“จํ„ฐ ๋น„์ „ ๋„คํŠธ์›Œํฌ๋งŒ ๊ฐ„์‹ ํžˆ ์ดํ•ดํ•œ ์ž…์žฅ์—์„œ๋Š” ๋งค์šฐ ์–ด๋ ต๊ณ  ์ƒ์†Œํ•œ ๋‚ด์šฉ์ด ๋งŽ์•˜์Šต๋‹ˆ๋‹คใ…œ ์ถ”๊ฐ€์ ์œผ๋กœ ํ•™์Šตํ•˜๋Š” ๋Œ€๋กœ ์œ„ ๋…ผ๋ฌธ์˜ ๋‚ด์šฉ์„ ๋ณด์ถฉํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ ˆํผ๋Ÿฐ์Šค ์›๋…ผ๋ฌธ | Show, Attend and Tell: Neural Image Caption Generation with Visual Attention ์œคํ˜•๋นˆ ํ”„๋กœ์ ํŠธ ๊ฐœ๋ฐœ | attention models, image captioning, machine translation Information and Intelligence | Attention (5): Show, Attend and Tell https://sunshower76.github.io/deeplearning/2020/08/24/CS231n-Lecture10/ (4) ๋ชจ๋ธ ํ‰๊ฐ€ Image captioning ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ๋žŒ์ด ์ง์ ‘ ํ‰๊ฐ€ํ•˜๋Š” Human evaluation์€ ์ •ํ™•ํ•˜์ง€๋งŒ ๋น„์šฉ๊ณผ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ๋งŽ์ด ๋“ ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์‚ฌ๋žŒ์ด ์ง์ ‘ ํ‰๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ‰๊ฐ€ํ•˜๋ ค๋Š” ์–ธ์–ด์— ๋Œ€ํ•œ ์ œํ•œ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์–‘ํ•œ ์ž๋™ ํ‰๊ฐ€ ์ง€ํ‘œ๋“ค์ด ๊ฐœ๋ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘ ์—ฌ๋Ÿฌ ๋…ผ๋ฌธ์— ๋ฐ˜๋ณต์ ์œผ๋กœ ๋“ฑ์žฅํ•˜๋Š” ๋Œ€ํ‘œ์  ํ‰๊ฐ€ ์ง€ํ‘œ๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. Perplexity (PPL) PPL์€ ์–ธ์–ด ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ์ง€ํ‘œ๋กœ ํ”ํžˆ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Perplexity๋Š” "๋‹นํ˜น, ํ˜ผ๋ž€, ๊ณคํ˜น"์ด๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์—ฌ๊ธฐ์„œ PPL์€ 'ํ—ท๊ฐˆ๋ฆฌ๋Š” ์ •๋„'๋กœ ์ดํ•ดํ•ฉ์‹œ๋‹ค. ๋‹จ์–ด ๋œป์„ ์ƒ๊ฐํ•ด ๋ณด๋ฉด PPL ์ˆ˜์น˜๊ฐ€ '๋‚ฎ์„์ˆ˜๋ก' ์–ธ์–ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๋Š” ๊ฒƒ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Perplexity๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์‹์œผ๋กœ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ๊ฐ ๋‹จ์–ด w1~ w_n์ด ์กฐํ•ฉ๋˜์–ด ํ•ด๋‹น ๋ฌธ์žฅ์ด ๋งŒ๋“ค์–ด์งˆ ํ™•๋ฅ ์— -1/n์„ ์ทจํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. " I can play the piano." ์ด ๋ฌธ์žฅ์ด ๋งŒ๋“ค์–ด์งˆ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•ž์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ๊ฐ์˜ ๋‹จ์–ด๊ฐ€ ๋‚˜ํƒ€๋‚  ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ณ  ์ด๋ฅผ Chain Rule๋กœ ๋ฌถ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. PPL์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ ํ‰๊ฐ€ PPL์€ "์–ธ์–ด ๋ชจ๋ธ์ด ํŠน์ • ์‹œ์ ์—์„œ ํ‰๊ท ์ ์œผ๋กœ ๋ช‡ ๊ฐœ์˜ ์„ ํƒ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ๊ณ ๋ฏผํ•˜๋Š”์ง€ ํ‰๊ฐ€" ํ•˜๋Š” ์ง€ํ‘œ๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์–ธ์–ด ๋ชจ๋ธ์˜ PPL์ด 10์ด ๋‚˜์™”๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ํ•ด๋‹น ์–ธ์–ด ๋ชจ๋ธ์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋“  ์‹œ์ (time-step)๋งˆ๋‹ค ํ‰๊ท ์ ์œผ๋กœ 10๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ๊ฒƒ์ด ์ •๋‹ต์ธ์ง€ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. P ( ) P ( 1 w, 3. . w) 1 = ( 10 ) 1 = 10 1 10 ๊ฐ™์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋‘ ์–ธ์–ด ๋ชจ๋ธ์˜ PPL ๊ฐ’์„ ๋น„๊ตํ•˜๋ฉด, ๋‘ ์–ธ์–ด ๋ชจ๋ธ ์ค‘ ์–ด๋–ค ๊ฒƒ์ด ์„ฑ๋Šฅ์ด ์ข‹์€์ง€ ํŒ๋‹จ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด A ๋ชจ๋ธ์€ PPL์ด 10, B ๋ชจ๋ธ์€ 20์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ A ๋ชจ๋ธ์€ 10๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ๊ณ ๋ฏผํ•˜๋Š” ๊ฑฐ๊ณ , B ๋ชจ๋ธ์€ 20๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ๊ณ ๋ฏผํ•˜๋Š” ์ค‘์ด๋ฏ€๋กœ ๋‹น์—ฐํžˆ A ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ๋” ์ข‹์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Reference Wikidocs, ๋”ฅ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ | Perplexity JH Programming| Language Model (1) N-Gram, Perplexity BLEU BLEU score์˜ ํ•œ๊ณ„ ๊ฐ™์€ ์˜๋ฏธ์˜ ๋‹ค๋ฅธ ๋‹จ์–ด์—ฌ๋„ ๋‹ค๋ฅธ ๋‹จ์–ด๋ฅผ ์“ฐ๋ฉด ํ‹€๋ ธ๋‹ค๊ณ  ํŒ๋‹จํ•œ๋‹ค. ์ฆ‰, ๋‹จ์–ด ๊ฐ„์˜ ์œ ์‚ฌ์„ฑ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค. BLEU๋Š” ๋‹จ์–ด๋ณ„ ๊ฐ€์ค‘์น˜๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”ํ•œ ๋‹จ์–ด๊ฐ€ ๋Œ€์ฒด๋˜๋“ , ์•ˆ ์ค‘์š”ํ•œ ๋‹จ์–ด๊ฐ€ ๋Œ€์ฒด๋˜๋“  ๋น„์Šทํ•œ ์ ์ˆ˜๊ฐ€ ๋‚˜์˜จ๋‹ค. Reference DeepLearningAI | Bleu Score (Optional) ๋”ฅ๋Ÿฌ๋‹ ๋…ผ๋ฌธ ์ฝ๊ธฐ ๋ชจ์ž„ | BLEU - a Method for Automatic Evaluation of Machine Translation ๋…ผ๋ฌธ ๋ฆฌ๋ทฐ BLEU: a Method for Automatic Evaluation of Machine Translation (2002 ACL) HULK์˜ ๊ฐœ์ธ ๊ณต๋ถ€์šฉ ๋ธ”๋กœ๊ทธ | BLEU์˜ ๋ชจ๋“  ๊ฒƒ METEOR ์–ด๋ ค์›Œ์„œ ํŒจ์Šค...... ํ›„ํ›„........ CIDEr : Consensus-based Image Description Evaluation ํ•œ๊ตญ์–ด๋กœ ๋œ ์ž๋ฃŒ๋Š” ๊ณ ์‚ฌํ•˜๊ณ  ์˜์–ด๋กœ ๋œ ์ž๋ฃŒ๋„ ๋ณ„๋กœ ์—†์–ด ์ง์ ‘ ๋…ผ๋ฌธ์„ ์ฝ๊ณ  ์„ค๋ช…ํ•˜๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค. ์ •ํ™•๋„๊ฐ€ ๋‚ฎ์œผ๋‹ˆ ๊ฐ์•ˆํ•˜๊ณ  ๋ด์ฃผ์„ธ์š”. NEW reference sentence dataset ์ค€๋น„ํ•˜๊ธฐ ๊ธฐ์กด์˜ Image captioning ๋ฐ์ดํ„ฐ ์…‹์€ ์ผ๋ฐ˜์ ์œผ๋กœ Image ํ•˜๋‚˜์— 5๊ฐœ์˜ reference sentece๊ฐ€ ๋ถ™์–ด์žˆ๋Š”๋ฐ, ์ €์ž๋Š” reference sentece 5๊ฐœ๋Š” ๋„ˆ๋ฌด ์ ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค. UIUC Pascal Sentence Dataset์—์„œ 1000์žฅ์˜ image๋ฅผ, Abstract Scenes Dataset์—์„œ 500์žฅ์˜ image๋ฅผ ์ถ”๋ฆฐ ๋’ค ๋ชจ๋“  Image์— ๋Œ€ํ•ด 50๊ฐœ์˜ reference sentence์„ ๋ถ™์—ฌ์„œ PASCAL-50S, ABSTRACT-50S๋ผ๋Š” ๋ฐ์ดํ„ฐ ์…‹์„ ์ค€๋น„ํ–ˆ์Šต๋‹ˆ๋‹ค. (๊ฐ ๋ฐ์ดํ„ฐ ์…‹์˜ Reference sentence๋ฅผ ์ œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ๋ฌด๋ ค 465๋ช…, 683๋ช…์„<NAME>ํ–ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.) CIDEr Metric CIDEr์€ ๊ธฐ๋ณธ์ ์œผ๋กœ TF-IDF์˜ ์•„์ด๋””์–ด๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. (TF-IDF์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.) Wk๋Š” ํŠน์ • ๋ชจ๋ธ์ด ์ƒ์„ฑํ•œ Candidate sentence๋กœ๋ถ€ํ„ฐ ๊ตฌํ•œ n-gram ์›์†Œ๋“ค์ž…๋‹ˆ๋‹ค. ํŠน์ • ๋ชจ๋ธ์ด "An eagle is perched among trees."๋ผ๋Š” Candidate sentence๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. - W1์€ [An, Eagle, is, perched, among, trees] ๊ฐ€ ๋˜๊ฒ ๊ณ  - W2๋Š” [An Eagle, Eagle is, is perched, perched among, among trees] ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์‹์€ ๊ฐ n-gram ์›์†Œ๋“ค์— ๋Œ€ํ•ด TF-IDF ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ์œ„ ์‹์—์„œ h_k(Sij)๋Š” n-gram Wk๊ฐ€ reference sentence Sij์— ๋“ฑ์žฅํ•˜๋Š” ํšŸ์ˆ˜์ž…๋‹ˆ๋‹ค. I๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹์— ์กด์žฌํ•˜๋Š” ์ „์ฒด Image ๊ฐœ์ˆ˜์ž…๋‹ˆ๋‹ค. ํŒŒ๋ž€์ƒ‰ ๋ฐ•์Šค๋Š” n-gram Wk๊ฐ€ reference sentece์— ๋“ฑ์žฅํ•˜๋Š” ๋น„์œจ์„ ๋‚˜ํƒ€๋‚ด๋ฏ€๋กœ TF์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ดˆ๋ก์ƒ‰ ๋ฐ•์Šค๋Š” n-gram Wk๊ฐ€ ์ „์ฒด Image ์ค‘ p ๋ฒˆ์งธ image์ธ Ip์— ๋Œ€ํ•œ 50๊ฐœ์˜ reference sentence์—์„œ ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š”์ง€ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋นจ๊ฐ„์ƒ‰ ๋ฐ•์Šค๋Š” IDF์— ํ•ด๋‹นํ•œ๋‹ค ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Candidate sentence์˜ n-gram ์›์†Œ๋“ค์— ๋Œ€ํ•ด ๊ฐ๊ฐ g_k(Sij) ๊ฐ’์„ ๊ตฌํ•œ ๋’ค ์ด๋ฅผ ๋ชจ์•„ ๋ฒกํ„ฐํ™”ํ•˜์—ฌ g^n(Sij)๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. "An eagle is perched among trees."๋ผ๋Š” Candidate sentence์— ๋Œ€ํ•ด 1-gram ์›์†Œ๋ณ„ g_k(Sij) ๊ฐ’์ด ๊ฐ๊ฐ a, b, c, d, e, f๋ผ๊ณ  ํ•˜๋ฉด g^1(Sij)๋Š” [a, b, c, d, e, f]๊ฐ€ ๋˜๋Š” ์‹์ž…๋‹ˆ๋‹ค. ๋น„์Šทํ•œ ์‹์œผ๋กœ g^n(Ci)๋„ ๊ตฌํ•œ ๋’ค g^n(Ci)์™€ g^n(Sij)์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜์—ฌ CIDEr_n(Ci, Si) ๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ candidate sentece์— ํฌํ•จ๋œ n-gram ์›์†Œ๋“ค์— ๋Œ€ํ•ด candidate sentence ๋‚ด TF-IDF ๋ฒกํ„ฐ์™€ reference sentece ๋‚ด TF-IDF ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•œ ๋’ค ๋‘ ๋ฒกํ„ฐ์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ตœ์ข… CIDEr(Ci, Si) ๊ฐ’์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ•ฉ๋‹ˆ๋‹ค. w_n = 1/N์ž…๋‹ˆ๋‹ค. (1-gram ~ 3-gram์„ ๋น„๊ตํ•˜๊ฒ ๋‹ค๊ณ  ํ•˜๋ฉด N=3์ด๊ฒ ๊ณ  1-gram ~ 4-gram์„ ๋น„๊ตํ•˜๊ฒ ๋‹ค๊ณ  ํ•˜๋ฉด N=4๊ฒ ์ฃ ?) Reference ์›๋…ผ๋ฌธ: Vedantam, Ramakrishna, C. Lawrence Zitnick, and Devi Parikh. "Cider: Consensus-based image description evaluation." Proceedings of the IEEE conference on computer vision and pattern recognition. 2015. SPICE: Semantic Propositional Image Caption Evaluation ํ•œ๊ตญ์–ด๋กœ ๋œ ์ž๋ฃŒ๋Š” ๊ณ ์‚ฌํ•˜๊ณ  ์˜์–ด๋กœ ๋œ ์ž๋ฃŒ๋„ ๋ณ„๋กœ ์—†์–ด ์ง์ ‘ ๋…ผ๋ฌธ์„ ์ฝ๊ณ  ์„ค๋ช…ํ•˜๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค. ์ •ํ™•๋„๊ฐ€ ๋‚ฎ์œผ๋‹ˆ ๊ฐ์•ˆํ•˜๊ณ  ๋ด์ฃผ์„ธ์š”. ๊ธฐ์กด์˜ BLEU, METEOR, CIDEr ๊ฐ™์€ ๋ฐฉ๋ฒ•๋“ค์€ ๋Œ€์ฒด๋กœ candidate sentence์™€ reference sentence์˜ n-gram ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ํ‰๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด "n-gram ๊ฐ„ ์œ ์‚ฌ๋„ ๋น„๊ต"์˜ ํ•œ๊ณ„๋ฅผ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (a) A young girl standing on top of a tennis court. (b) A giraffe standing on top of a green field. (c) A shiny metal pot filled with some diced veggies. (d) The pan on the stove has chopped vegetables in it. (a)์™€ (b)๋Š” ์™„์ „ํžˆ ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฌธ์žฅ์ด์ง€๋งŒ standing on top of a๋ผ๋Š” ๊ณตํ†ต ๊ตฌ์ ˆ์„ ๊ฐ€์ง€๊ธฐ์— n-gram ์œ ์‚ฌ๋„๋Š” ๋งค์šฐ ๋†’์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด (c)์™€ (d)๋Š” ๊ฑฐ์˜ ๋น„์Šทํ•œ ์˜๋ฏธ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฌธ์žฅ์ด์ง€๋งŒ ๊ณตํ†ต ๊ตฌ์ ˆ์ด ๊ฑฐ์˜ ์—†์–ด์„œ n-gram ์œ ์‚ฌ๋„๋Š” ํ˜•ํŽธ์—†์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ ์ž ์ €์ž๋“ค์€ SPICE๋ผ๋Š” ์ƒˆ๋กœ์šด ํ‰๊ฐ€ ๋ฐฉ์‹์„ ๋„์ž…ํ•ฉ๋‹ˆ๋‹ค. Semantic scene graphs candidate sentence์™€ reference sentence๋ฅผ ๋ถ„์„ํ•˜์—ฌ Semantic scene graphs๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. Semantic scene graphs๋Š” object, attribute, relations ๊ฐ„์˜ ๊ด€๊ณ„ ๊ทธ๋ž˜ํ”„๋ผ๊ณ  ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ๋ฌธ์žฅ์—์„œ ํ•ต์‹ฌ์ด ๋˜๋Š” ๋ช…์‚ฌ๊ฐ€ object์ด๊ณ , ์ด๋ฅผ ์ˆ˜์‹ํ•˜๋Š” ๋‹จ์–ด๊ฐ€ attriutes์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ object ์‚ฌ์ด์˜ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹จ์–ด๋ฅผ relations๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ ์ œ์‹œํ•œ Fig1, Fig2๋ฅผ ํ†ตํ•ด Object, attribute, relation์˜ ๊ฐœ๋…์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ €์ž๋“ค์ด Standford Scene Graph Parser๋ผ๋Š” ๋ฐฉ์‹์„ ์•ฝ๊ฐ„ ์ˆ˜์ •ํ•˜์—ฌ semantic scene graphs๋ฅผ ๊ทธ๋ฆฌ๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜๋‹ˆ ๋งŒ์•ฝ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ด ๊ฐœ๋…์„ ์ฐพ์•„๋ณด๋Š” ๊ฒƒ๋„ ๋„์›€์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.) SPICE Meteric candidate sentence = c, scene graphs of candidate caption c = G(c) reference sentece = s, scene graphs of reference caption s = G(s) the set of object mentions in c = O(c) the set of relations between objects in c = E(c) the set of attributes associated with objects in c = K(c) ์œ„์—์„œ ์ •์˜ํ•œ O(c), E(c), K(c)์˜ ํ•ฉ์ง‘ํ•ฉ์„ Tuple, T(G(c))๋ผ๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Fig 1.์˜ "A young girl standing on top of a tennis court."๋ผ๋Š” candidate sentence์— ๋Œ€ํ•œ T(G(c))๋ฅผ ๊ตฌํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. T(G(c))์™€ T(G(s))๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•œ ๋’ค, Tuple์˜ ์›์†Œ๊ฐ€ ๋ช‡ ๊ฐœ๋‚˜ ๊ฐ™์€์ง€ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ lemmatized word form์ด ๊ฐ™์œผ๋ฉด ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. (lemmatization์˜ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ) ๋˜ํ•œ wordnet์˜ ๊ฐ™์€ synset์— ํฌํ•จ๋˜๋ฉด ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. (wordnet synset์˜ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ) ์ €์ž๋“ค์€ ๊ธฐ์กด์˜ BLEU, METEOR, CIDEr score์— ๋น„ํ•ด SPICE score๊ฐ€ ์„ฑ๋Šฅ์ด ๋” ์ข‹๋‹ค๊ณ  ๋…ผ๋ฌธ์—์„œ ์ œ์‹œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ SPICE score๋Š” Object, attribute, relation์—๋งŒ ์ง‘์ค‘ํ•˜๋‹ค ๋ณด๋‹ˆ syntax์˜ ์˜ค๋ฅ˜, ์ฆ‰ ์–ธ์–ด์˜ ๋ฌธ๋ฒ•์ , ๊ตฌ์กฐ์  ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ ์ œ๋Œ€๋กœ ์ง‘์–ด๋‚ด์ง€ ๋ชปํ•œ๋‹ค๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Reference ์›๋…ผ๋ฌธ: SPICE: Semantic Propositional Image Caption Evaluation ๋”ฅ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ | ์–ด๊ฐ„ ์ถ”์ถœ(Stemming) and ํ‘œ์ œ์–ด ์ถ”์ถœ(Lemmatization) ์œ„ํ‚ค๋ฐฑ๊ณผ | ์›Œ๋“œ ๋„ท 6. Visual Embedding(๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ) ์ปดํ“จํ„ฐ ๋น„์ „์—์„œ ๊ฐ€์žฅ ์‹ค์šฉ์ ์ธ Task๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๊ธ€, ๋„ค์ด๋ฒ„์˜ ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰์„ ์ƒ๊ฐํ•˜๋ฉด ๊ฐ€์žฅ ์‰ฝ๊ฒŒ ์ดํ•ด๋˜์‹ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ๋‰ด๋Ÿด๋„ท์œผ๋กœ ํ™œ์šฉํ•ด ๋ฒกํ„ฐ ํ‘œํ˜„ํ•˜์—ฌ ์ ์šฉํ•˜๋Š” ์˜์—ญ๋“ค์„ ํ•˜๋‚˜๋กœ ๋ฌถ์—ˆ์Šต๋‹ˆ๋‹ค. (1) ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ ์•„์ด๋””์–ด ์ด์ œ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์ •๋ฆฌํ•œ ๋ชจ๋“ ์— ์„œ๋Š” ์•”์‹œ์ ์œผ๋กœ CNN์„ ์ด์šฉํ•˜์—ฌ ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ์„ ์ถ”์ถœํ•˜๋Š” ๊ณผ์ •์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ณ…ํ„ฐ1์˜ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜(Image Classification)์˜ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋„คํŠธ์›Œํฌ์™€ ๋กœ์Šค ํŽ‘์…˜์œผ๋กœ ๋Œ์•„๊ฐ€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋„คํŠธ์›Œํฌ์—์„œ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ์ด์ „์— ์žˆ๋Š” ๋งˆ์ง€๋ง‰ FC layer์˜ ๋ฒกํ„ฐ๋“ค์€ ์–ด๋–ค ์˜๋ฏธ๊ฐ€ ์žˆ์„๊นŒ์š”? FC layer์˜ ์ถœ๋ ฅ ๋ฐฑํ„ฐ(์ž„๋ฐฐ๋”ฉ ๋ฒกํ„ฐ)๋Š” ๋ฒกํ„ฐ๊ณต๊ฐ„์—์„œ์˜ ํ•œ ํฌ์ธํŠธ(Point)์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด FC ๋ ˆ์ด์–ด์˜ ์ถœ๋ ฅ๊ฐ’์€ ๋กœ์Šค ํŽ‘์…˜์˜ ๋ชฉ์ ์— ๋งž๊ฒŒ ๋ถ„๋ฅ˜(Classification) ํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์ ์˜ ๋ฒกํ„ฐ(Vector space)๋ฅผ ํ•™์Šตํ–ˆ์„ ๊ฒƒ์ด๊ณ . ๋ถ„๋ฅ˜์— ์ตœ์ ํ™”๋œ ์ž„๋ฐฐ๋”ฉ ๋ฒกํ„ฐ(Embedding vector)๋“ค์€ ๊ณต๊ฐ„์ƒ์—์„œ ๊ฐ€๊นŒ์šด ์œ„์น˜์— ๋ชจ์—ฌ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ด๋ฃจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ž„๋ฐฐ๋”ฉ ๋ฒกํ„ฐ ๊ฐœ๋…์€ ์‹ค์šฉ์ ์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜(application)์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ์˜ ํ™œ์šฉ ์ž„๋ฐฐ๋”ฉ ํ™œ์šฉ์€ ์‚ฌ์ „์— ๋‹ค์–‘ํ•œ ์ด๋ฏธ์ง€์—์„œ ์ถ”์ถœํ•œ ์ž„๋ฐฐ๋”ฉ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” DB๊ฐ€ ์žˆ์„ ๋•Œ ํฐ ํšจ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ Face Recognition๊ณผ Image recommendation์ด ์žˆ์Šต๋‹ˆ๋‹ค. Face recognition Face recognition์€ ์—ญํ• ์— ๋”ฐ๋ผ 2๊ฐ€์ง€ ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Face identification์€ ์‚ฌ์ง„์˜ ์ด๋ฏธ์ง€๊ฐ€ ๋ˆ„๊ตฌ์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ๊ธฐ์ˆ ์„ ์˜๋ฏธํ•˜๊ณ  Face verification์€ ์ดฌ์˜๋œ ์‚ฌ์ง„์˜ ์ธ๋ฌผ์ด ํƒ€๊นƒ ์ธ๋ฌผ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ธฐ์ˆ ๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค. Image recommendation system ์ž…๋ ฅ ์ด๋ฏธ์ง€(Query Image)๋ฅผ ๋„ฃ์œผ๋ฉด ์œ ์‚ฌํ•œ ์ƒํ’ˆ๋“ค์„ ์ถ”์ฒœํ•ด ์ฃผ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์‹œ๊ฐ์  ํŠน์ง•๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ ์ƒํ’ˆ ๊ฒ€์ƒ‰์—์„œ๋Š” ๋‹ค์–‘ํ•œ ์œ ์‚ฌ๋„ ๊ธฐ์ค€(Color, Size, Shape ๋“ฑ) ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๋“ค์€ ๋ผˆ๋Œ€๋Š” ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€(Query Image)์—์„œ ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ์„ ์ถ”์ถœํ•˜๊ณ  DB์— ์ €์žฅ๋œ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋“ค์˜ ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ๋“ค๊ณผ ๊ณต๊ฐ„์—์„œ ์œ ์‚ฌ๋„(Similarity)๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฑฐ์น˜๋„๋ก ์‹œ์Šคํ…œ์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๋“ค์˜ ํ•ต์‹ฌ์€ ์–ด๋–ค ๊ธฐ์ค€ ์œ ์‚ฌ์„ฑ ๊ธฐ์ค€์— ๋งž์ถฐ ๋ผ๋ฒจ์„ ๋ถ™์ด๊ณ  ํ•™์Šต์„ ์ง„ํ–‰ํ–ˆ๋Š”์ง€๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ์ด ์ฃผ์ œ๋Š” ๋น„์ง€๋„ ํ•™์Šต(Unsupervised learning)๊ณผ๋„ ๋งŽ์€ ์—ฐ๊ด€์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ณ…ํ„ฐ์—์„œ๋Š” ๋ผ๋ฒจ์ด ๋ช…ํ™•ํ•œ Face Recognition ์˜ˆ์‹œ๋กœ ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋กœ์Šค ํŽ‘์…˜(Loss function) ์•ž์— ๋ง์”€๋“œ๋ฆฐ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜(Image Classification) CNN ๋„คํŠธ์›Œํฌ + Cross Entropy Loss๋กœ ํ•™์Šต์ด ์™„๋ฃŒ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉํ•œ ๋„คํŠธ์›Œํฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์–ด๋–ค ์ด๋ฏธ์ง€๋ฅผ ์ธ์ฝ”๋”ฉํ•˜์˜€์„ ๋•Œ ๋ฏผํฌ ์‚ฌ์ง„ ์ž„๋ฐฐ๋”ฉ๊ณผ ์œ ์‚ฌํ•˜๋‹ค๋ฉด ์ด ์ด๋ฏธ์ง€๋Š” ๋ฏผํฌ์˜ ์ด๋ฏธ์ง€์ผ๊นŒ์š”? ์•ž์„œ ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด์„œ ๋น„์Šทํ•œ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ด๋ฃฌ๋‹ค๊ณ  ์„ค๋ช…๋“œ๋ ธ์ง€๋งŒ, ์‚ฌ์‹ค 100% ์žฅ๋‹ดํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋„คํŠธ์›Œํฌ๋ฅผ Cross Entropy Loss๋กœ ํ•™์Šตํ•˜๊ณ  Face Recognition์— ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทผ๋ณธ์ ์œผ๋กœ Cross entropy ๋กœ์Šค๋Š” ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ ์ถ”์ถœ์— ์ตœ์ ํ™”๋œ ๋กœ์Šค ํŽ‘์…˜์ด ์•„๋‹ˆ๊ธฐ์— Training(๋ถ„๋ฅ˜ ํ›ˆ๋ จ)๊ณผ Inference(๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ ํ™œ์šฉ)์˜ ๊ฐญ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ํด๋ž˜์Šค๊ฐ€ ๋Š˜์–ด๋‚  ๊ฒฝ์šฐ์— ๋งค๋ฒˆ ์žฌํ•™์Šต์„ ํ•ด์•ผ๋งŒ ์„ฑ๋Šฅ์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ์„ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ Contrastive, Triplet๊ณผ ๊ฐ™์ด ์ž„๋ฐฐ๋”ฉ์„ ์ด์šฉํ•˜์—ฌ ๋กœ์Šค ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ช…์‹œ์ ์ธ ๋กœ์Šค ํŽ‘์…˜(+๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ)์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์–ด์ง€๋Š” ๋‚ด์šฉ์—์„œ๋Š” ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ์„ ์ถ”์ถœํ•˜๋Š” ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ, ๋กœ์Šค ํŽ‘์…˜๊ณผ ํŠธ๋ ˆ์ด๋‹ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. (2) ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ์„ ์œ„ํ•œ ์‚ฌ์ „ ์ง€์‹ 1) One-shot learning(โ˜…์ž‘์„ฑ ์ค‘) ๋น„์Šทํ•œ ์ด๋ฏธ์ง€๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ๋ฅผ ๊ธฐ์กด์˜ classification model๋กœ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ์ œ์ ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. 1) ๋ชจ๋“  N ๊ฐœ์˜ class์— ๋Œ€ํ•˜์—ฌ ํ•™์Šต์„ ์ง„ํ–‰ํ•  ๋งŒํผ์˜ ์ถฉ๋ถ„ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๊ณ  2) N ๊ฐœ์˜ class๋ฅผ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์—์„œ ๊ธฐ์กด์— ์—†๋˜ class์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ œ์‹œ๋œ ๊ฒฝ์šฐ ๋ชจ๋ธ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šต์‹œ์ผœ์•ผ ํ•จ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ์•ˆ์œผ๋กœ์จ one-shot learning ๋˜๋Š” few-shot learning ๊ณ„์—ด์˜ ํ•™์Šต ๋ฐฉ๋ฒ•๋ก ์ด ๊ณ ์•ˆ๋˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. One-shot / Few-shot learning ์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€ ์šฐ๋ฆฌ๋Š” ์ง€๊ธˆ๊นŒ์ง€ Fully supervised learning์„ ๊ธฐ๋ฐ˜์œผ๋กœ Computer vision์—์„œ์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค์„ ๋ฐฐ์›Œ์™”์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Š” ์‚ฌ๋žŒ์ด ์ด๋ฏธ์ง€๋ฅผ ์ธ์ง€ํ•˜๊ณ  ํ•™์Šตํ•˜๋Š” ๊ณผ์ •๊ณผ ์ƒ๋‹นํ•œ ์ฐจ์ด๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ „ํ†ต์ ์ธ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์—์„œ๋Š” ๊ฐ€๋ น ์•„๋ž˜์˜ ์˜ˆ์‹œ์—์„œ ์šฐ๋ฆฌ๋Š” ํ—ˆ์Šคํ‚ค ์‚ฌ์ง„์„ ์ฃผ๋ฉด ํ—ˆ์Šคํ‚ค๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ , ์ฝ”๋ผ๋ฆฌ ์‚ฌ์ง„์„ ์ฃผ๋ฉด ์ฝ”๋ผ๋ฆฌ๋กœ ๊ตฌ๋ถ„์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ์—ฌ๊ธฐ ์˜ˆ์‹œ์—๋Š” ์—†๋Š” ๋‹ค๋žŒ์ฅ ์‚ฌ์ง„ ๋‘ ๊ฐœ๋ฅผ ์ฃผ๋ฉด ๋‘˜์ด '๋น„์Šทํ•˜๋‹ค'๋ผ๋Š” ๊ฒฐ๋ก ์„ ๋‚ผ ์ˆ˜ ์žˆ์„๊นŒ์š”? ์•„๋งˆ๋„ ๋ถˆ๊ฐ€๋Šฅํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ๋žŒ์—์„œ๋Š” ๊ฐ€๋Šฅํ•˜์ฃ . ์–ด๋–ป๊ฒŒ ๊ฐ€๋Šฅํ• ๊นŒ์š”? ์šฐ๋ฆฌ๋Š” "๊ตฌ๋ถ„ํ•˜๋Š” ๋ฒ•"์„ ํ•™์Šตํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅด๊ฒŒ ๋งํ•˜๋ฉด "๋ฐฐ์šฐ๋Š” ๋ฒ•์„ ๋ฐฐ์šฐ๋Š”"(learn to learn) ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์‚ด๋ฉด์„œ ์‚ฌ์ž์™€ ํ˜ธ๋ž‘์ด๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ๋ฐฐ์šฐ๊ณ  ํ† ๋ผ์™€ ์ฐจ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ๋ฐฐ์šฐ๊ณ  ์ƒ๋‹นํžˆ ๋งŽ์€ ๋ฌผ์ฒด๋ฅผ ๊ตฌ๋ถ„ ์ง“๋Š” ๋ฒ•์„ ํ•™์Šตํ•ด์™”์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์šฐ๋ฆฌ๋Š” ๋‹ค๋žŒ์ฅ ์‚ฌ์ง„ ๋‘ ์žฅ์„ ๋ณด๊ณ  ๋‘˜์ด ๋น„์Šทํ•˜๋‹ค๋Š” ์‚ฌ์‹ค์„ ์œ ์ถ”ํ•ด๋‚ผ ์ˆ˜ ์žˆ๊ณ , ๋‹ค๋žŒ์ฅ์™€ ์ฐจ๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์„ค๋ น ์ฒ˜์Œ ๋ณด๋Š” ๋ฌผ์ฒด๋ผ๊ณ  ํ• ์ง€๋ผ๋„ ๋ฌธ์ œ์—†์ด ์šฐ๋ฆฌ๋Š” ์ ์€ ์ˆ˜์˜ ์‚ฌ์ง„๋งŒ ๋ณด๊ณ ๋„ ๋ฌผ์ฒด๋ฅผ ๊ตฌ๋ถ„ ์ง€์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Few shot learning์˜ ํ•ต์‹ฌ์ด ์—ฌ๊ธฐ์„œ ์˜ต๋‹ˆ๋‹ค. One-shot / Few-shot learning์˜ ํ•™์Šต๋ฐฉ๋ฒ• Few shot learning์—์„œ์˜ ๋ฐ์ดํ„ฐ๋Š” training์— ์ด์šฉ๋˜๋Š” Support set๊ณผ ํ…Œ์ŠคํŠธ์— ์ด์šฉ๋˜๋Š” Query๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ Few shot learning task๋ฅผ ๋ณดํ†ต N-way K-shot problem์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, label ๋‹น Support set์—์„œ์˜ ์ด๋ฏธ์ง€ ์ˆ˜๋ฅผ K๋ผ๊ณ  ํ•˜๊ณ , N์€ label์˜ ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ๋‹ค๋ฃฐ One-shot learning์€ Few shot learning์˜ ๊ทน๋‹จ์ ์ธ ์˜ˆ์‹œ๋กœ K๊ฐ€ 1, ์ฆ‰ label ๋‹น ์ด๋ฏธ์ง€๋ฅผ ํ•˜๋‚˜์”ฉ๋งŒ ๋ณด์—ฌ์ฃผ๊ณ  ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. 1) Distance ๊ธฐ๋ฐ˜ ํ•™์Šต Distance ๊ธฐ๋ฐ˜ ํ•™์Šต์˜ ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š”, ์•„๋ž˜์™€ ๊ฐ™์€ "distance function"์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ( m 1 i g) degree of difference between images ์—ฌ๊ธฐ์„œ $D(img1, img2) > 2) Graph neural network ๊ธฐ๋ฐ˜ ํ•™์Šต Reference Few shot learning Siamese Neural Networks (์ƒด ๋„คํŠธ์›Œํฌ) ๊ฐœ๋… ์ดํ•ดํ•˜๊ธฐ https://youtu.be/96b_weTZb2w 2) Siamese Network ์ƒด ๋„คํŠธ์›Œํฌ๋Š” Siamese Neural Networks for One-shot Image Recognition๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์ฒ˜์Œ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค๊ฐ€ ๋งŽ์•„ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋„คํŠธ์›Œํฌ๋ฅผ ์ „์ด(Transfer) ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ , ํด๋ž˜์Šค ์‚ฌ์ง„์„ ๋Œ€๋Ÿ‰์œผ๋กœ ๊ตฌํ•  ์ˆ˜ ์—†์„ ๋•Œ ์ถ”๊ฐ€์ ์ธ ํ•™์Šต(Re-training) ์—†์ด ์ƒˆ๋กœ์šด ํด๋ž˜์Šค์— ๋Œ€ํ•ด์„œ๋„ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ๋ชจ๋ธ์ด ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. (one-shot learning์„ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค) ์ƒด ๋„คํŠธ์›Œํฌ๋Š” ๋‘ ์‚ฌ์ง„์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ณ , ๋™์ผํ•œ CNN ๋„คํŠธ์›Œํฌ๋กœ ๋‘ ์ด๋ฏธ์ง€์—์„œ ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ์„ ๊ฐ๊ฐ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ ๋ฒกํ„ฐ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ(Distance)๋ฅผ ๊ตฌํ•˜๊ณ  ๊ฑฐ๋ฆฌ๋ฅผ ์œ ์‚ฌ๋„ ๊ฐ’์œผ๋กœ ์ถœ๋ ฅ(0~1) ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋ ˆ์ด๋‹(Training)์€ ๋‹ค์Œ ์ˆœ์„œ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฐ์ดํ„ฐ(Input 1, Input 2) ์Œ์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ํด๋ž˜์Šค ์Œ์˜ ๊ฒฝ์šฐ ์ถœ๋ ฅ๊ฐ’์„ 1๋กœ, ๋‹ค๋ฅผ ๊ฒฝ์šฐ 0์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ<NAME>๋Š” CNN์„ ํ†ต๊ณผ์‹œ์ผœ ๊ฐ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์ž„๋ฒ ๋”ฉ ๊ฐ’ (Embedding 1, Embedding 2)์„ ์–ป์Šต๋‹ˆ๋‹ค. ๋‘ ์ž„๋ฒ ๋”ฉ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. L1, L2 norm ๋“ฑ์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‘ ์ž…๋ ฅ์ด ๊ฐ™์€ ํด๋ž˜์Šค ๋ฉด ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€๊น๊ฒŒ, ๋‹ค๋ฅธ ํด๋ž˜์Šค ๋ฉด ๊ฑฐ๋ฆฌ๋ฅผ ๋ฉ€๊ฒŒ ํ•˜๋Š” Contrastive loss๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ๊ฐ€ ๋งค์šฐ ํŠน๋ณ„ํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‰ด๋Ÿด๋„ท์˜ ๋ณต์ˆ˜ ์‚ฌ์šฉ, ํŠน๋ณ„ํ•œ ๋กœ์Šค ํŽ‘์…˜ ์‚ฌ์šฉ์ด ๊ธฐ์กด๊ณผ ์ฐจ๋ณ„๋˜๋Š” ๋„คํŠธ์›Œํฌ์ž…๋‹ˆ๋‹ค. Reference ์› ๋…ผ๋ฌธ: Siamese Neural Networks for One-shot Image Recognition ์œ ํŠœ๋ธŒ DeepLearningAI | Siamese network ๊ณ ๋ ค๋Œ€ํ•™๊ต ์‚ฐ์—…๊ฒฝ์˜๊ณตํ•™๋ถ€ DSBA ์—ฐ๊ตฌ์‹ค Siamese neural networks for one-shot image recognition ๋ธ”๋กœ๊ทธ ์ƒด ๋„คํŠธ์›Œํฌ๋ฅผ ์ด์šฉํ•œ ๋ฌธ์ œ ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ Siamese Neural Networks (์ƒด ๋„คํŠธ์›Œํฌ) ๊ฐœ๋… ์ดํ•ดํ•˜๊ธฐ Siamese Neural Networks for One-shot Image Recognition 3) Contrastive Loss ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด "์ฒ ์ˆ˜", "์˜ํฌ", "๋ฏผํฌ"์˜ ์‚ฌ์ง„์„ ๊ฐ๊ฐ 100๊ฐœ์”ฉ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ 2๊ฐœ์”ฉ ๋žœ๋ค์œผ๋กœ ๋ฝ‘์•„ ์ง์„ ์ง€์–ด์„œ ์ด๋ฏธ์ง€ ์„ธํŠธ(Set)๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ™์€ ์‚ฌ๋žŒ์ผ ๊ฒฝ์šฐ ๋ผ๋ฒจ์€ 1, ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ผ ๊ฒฝ์šฐ 0์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. {(์ฒ ์ˆ˜-์ฒ ์ˆ˜, 1), (์˜ํฌ-์ฒ ์ˆ˜, 0), (์ฒ ์ˆ˜-๋ฏผํฌ, 0), (์˜ํฌ-์˜ํฌ, 1), ....} ์ด์ œ ๊ฐ๊ฐ์˜ ์ด๋ฏธ์ง€์—์„œ CNN์„ ์ด์šฉํ•˜์—ฌ ์ž„๋ฐฐ๋”ฉ์„ ์ถ”์ถœํ•˜๊ณ  ์ž„๋ฐฐ๋”ฉ๊ฐ„์˜ ๊ฑฐ๋ฆฌ(Distance)๋ฅผ ์ด์šฉํ•˜์—ฌ ๋กœ์Šค ๊ฐ’์„ ๊ตฌํ•˜์—ฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์‹์˜ ์˜๋ฏธ๋ฅผ ๋ถ„์„ํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ผ๋ฒจ์ด 1์ธ ๊ฒฝ์šฐ์—๋Š” ์ž„๋ฐฐ๋”ฉ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๊ฐ€ Loss ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ํด๋ž˜์Šค์ธ ๊ฒฝ์šฐ๋Š” ๊ฑฐ๋ฆฌ๋ฅผ 0์ด ๋˜๋„๋ก ํ•™์Šต์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ๋ผ๋ฒจ์˜ 0์ธ ๊ฒฝ์šฐ์—๋Š” (1-y)๋Š” 1์ด ๋˜๋ฏ€๋กœ ์—†๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•˜๊ณ  ํ•ด์„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. max() ๊ฐ€ ์ด์šฉ๋˜์—ˆ์œผ๋ฏ€๋ฅด ๊ฒฝ์šฐ๋ฅผ 2๊ฐ€์ง€๋กœ ์ชผ๊ฐœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Case 1) ฮฑ(margin) > Distance์ธ ๊ฒฝ์šฐ : ๋กœ์Šค ๊ฐ’์ด ์กด์žฌํ•˜๊ณ  Distance๊ฐ€ ฮฑ๋งŒํผ์˜ ํฌ๊ธฐ๊ฐ€ ๋˜๋„๋ก CNN์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์—…๋ฐ์ดํŠธ๊ฐ€ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. Case 2) ฮฑ < Distance์ธ ๊ฒฝ์šฐ : max ํ•จ์ˆ˜๋ฅผ ๊ฑฐ์น˜๋ฉด Loss๊ฐ€ 0์ด๋ฏ€๋กœ ๊ฐ€์ค‘์น˜ ์—…๋ฐ์ดํŠธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋ณต์žกํ•˜๊ฒŒ ๋ณด์ด์ง€๋งŒ ์š”์•ฝํ•˜๋ฉด ์„œ๋กœ ๊ฐ™์€ ํด๋ž˜์Šค์ธ ๊ฒฝ์šฐ์—๋Š” ์ž„๋ฐฐ๋”ฉ๊ฐ„ ๊ฑฐ๋ฆฌ๊ฐ€ 0์ด ๋˜๋„๋ก ์„œ๋กœ ๋‹ค๋ฅธ ํด๋ž˜์Šค์ผ ๊ฒฝ์šฐ ๊ฑฐ๋ฆฌ๊ฐ€ ฮฑ(margin) ์ด์ƒ์ด ๋˜๋„๋ก ํ•™์Šตํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. Contrastive Loss ํ•œ๊ณ„์  โ‘ ฮฑ(margin)์€ ์‚ฌ๋žŒ์ด ์ •ํ•ด์ค˜์•ผ ํ•˜๋Š” ๊ฐ’(ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ)์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฑฐ๋ฆฌ์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ์—˜๋ฆฌ๋จผํŠธ๋“ค์ด CNN ํ†ตํ•ด ์ž„๋ฐฐ๋”ฉ ๋˜์—ˆ๊ธฐ์— ์‚ฌ๋žŒ ์ž…์žฅ์—์„œ๋Š” ์ง๊ด€์ ์œผ๋กœ ๊ฑฐ๋ฆฌ ๊ฐ’์„ ์ดํ•ดํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋ฒˆ ์‹œ๋„ ์—†์ด๋Š” ์ตœ์ (Optimum) ฮฑ(margin)์„ ์ •ํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. โ‘ก Distance๊ฐ€ ฮฑ(margin) ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๋ชจ๋“  ๊ฒฝ์šฐ์— ๊ฒฐ๊ตญ์—๋Š” ๋ชจ๋“  ์ž„๋ฐฐ๋”ฉ๊ฐ„ ๊ฑฐ๋ฆฌ๊ฐ€ ฮฑ(margin)์œผ๋กœ ์ˆ˜๋ ดํ•˜์—ฌ ์ •๋ณด๋ฅผ ์žƒ์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. โ‘ก๋ฒˆ ํ•œ๊ณ„์ ์„ ๋ณด์™„ํ•  ์ˆ˜ ์žˆ๋Š” FaceNet(+Triplet Loss)์—์„œ ์ œ์•ˆํ•˜์˜€์Šต๋‹ˆ๋‹ค. 4) FaceNet ์ƒด ๋„คํŠธ์›Œํฌ๊ฐ€ ๋‘ ๊ฐœ ์ž…๋ ฅ ์ด๋ฏธ์ง€ ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๋น„๊ตํ•ด์„œ ๋กœ์Šค๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์—ญ์ „ํŒŒ ํ–ˆ๋‹ค๋ฉด, FaceNet(2015)์—์„œ ์ œ์‹œํ•œ ๊ตฌ์กฐ๋Š” ์„ธ ๊ฐœ์˜ ์ž…๋ ฅ ์ด๋ฏธ์ง€ ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ํŠธ๋ ˆ์ด๋‹ ์‹œ ์ด๋ฏธ์ง€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ค€๋น„๋ฉ๋‹ˆ๋‹ค. Anchor ์ด๋ฏธ์ง€ : ๊ธฐ์ค€์ด ๋˜๋Š” ์ด๋ฏธ์ง€ Positive ์ด๋ฏธ์ง€ : Anchor ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ํด๋ž˜์Šค์˜ ์ด๋ฏธ์ง€ Negative ์ด๋ฏธ์ง€ : Anchor ์ด๋ฏธ์ง€์™€ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ์ด๋ฏธ์ง€ FaceNet ์•„ํ‚คํ…์ฒ˜ ๊ฐ ์ด๋ฏธ์ง€์—์„œ ๋™์ผํ•œ(Weight sharing) CNN์œผ๋กœ ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ์„ ๋ฝ‘์•„๋‚ด๊ณ , ์ž„๋ฐฐ๋”ฉ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ Triplet ๋กœ์Šค ๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. FaceNet์ด ์ƒด ๋„คํŠธ์›Œํฌ์™€ ๊ตฌ์กฐ์  ๋‹ค๋ฅธ ๊ฒƒ์€ ๋„คํŠธ์›Œํฌ ๋งˆ์ง€๋ง‰์ด ์ด๋ฏธ์ง€ ๊ฐ„ ์œ ์‚ฌ๋„๊ฐ€ ์•„๋‹ˆ๋ผ ์ž„๋ฐฐ๋”ฉ์œผ๋กœ ๋๋‚œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. FaceNet์€ ์ž„๋ฐฐ๋”ฉ๋งŒ์„ ์ด์šฉํ•œ ์ง์ ‘์ ์ธ ๋กœ์Šค ํŽ‘์…˜(Triplet Loss)์„ ์ œ์•ˆํ•˜์—ฌ Face Recognition ๋ฐœ์ „์— ๊ธฐ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. FaceNet์˜ ์žฅ์  FaceNet์ด Siamese Network์— ๋น„ํ•ด ๊ฐ€์ง€๋Š” ์žฅ์ ์œผ๋กœ๋Š” ์ˆœ์œ„ ๋งค๊ธฐ๊ธฐ(Ranking)๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. Siamese Networ์€ ์ž…๋ ฅ ์ด๋ฏธ์ง€์™€ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€์˜ ์œ ์‚ฌ๋„๋ฅผ 0~1์˜ ์ ˆ๋Œ€์  ์œ ์‚ฌ๋„(absolute similarity)๋งŒ ์•Œ ์ˆ˜ ์žˆ์œผ๋‚˜ FaceNet์€ ์ž…๋ ฅ ์ด๋ฏธ์ง€(Anchor)์™€ ๋‚˜๋จธ์ง€ ๋‘ ์ด๋ฏธ์ง€์™€ ๊ฑฐ๋ฆฌ๋ฅผ ํ†ตํ•ด์„œ ์ƒ๋Œ€์  ์œ ์‚ฌ๋„(relative similarity)๋ฅผ ์•Œ ์ˆ˜ ์žˆ๊ธฐ์— ์ˆœ์œ„ ๋งค๊ธฐ๊ธฐ(Ranking)๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Reference ์œ ํŠœ๋ธŒ DeepLearningAI | Triplet Loss ๋ธ”๋กœ๊ทธ Pairwise / Triplet Loss Triplet Loss ๊ฐ„๋‹จ ์ •๋ฆฌ 5) Triplet Loss Triple Loss๋Š” ์ž…๋ ฅ ์ด๋ฏธ์ง€ 3๊ฐœ(anchor, positive, negative)์˜ ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ์„ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ [ ]+์˜ ํ‘œ์‹œ๋Š” max ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์†์‹ค์€ ์œ„์™€ ๊ฐ™์ด ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. Contrastive Loss์—์„œ "๊ฐ™์€ ํด๋ž˜์Šค ์‚ฌ์ด ๊ฑฐ๋ฆฌ" = "0", "๋‹ค๋ฅธ ํด๋ž˜์Šค ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ" = "ฮฑ ์ด์ƒ"๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์„ ํƒ๋œ Positive, Negative ํด๋ž˜์Šค ๊ฐ„์˜ ์ƒ๋Œ€์ ์ธ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ํ•™์Šต์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ๊ฐ™์€ Positive ํด๋ž˜์Šค์— ์†ํ•ด ์žˆ๋”๋ผ๋„ ๋งค์šฐ ๋น„์Šทํ•œ ์ด๋ฏธ์ง€ ๊ฐ„์€ ํ•™์Šต ์™„๋ฃŒ ์ดํ›„์—๋„ ๋” ๊ฐ€๊นŒ์šด ๊ฑฐ๋ฆฌ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. (๊ทธ๋ž˜์„œ ์•ž์„œ ๋ง์”€๋“œ๋ฆฐ Ranking๊ณผ ๊ฐ™์€ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค) ํ•™์Šต์ด ์ด์ƒ์ ์œผ๋กœ ์™„๋ฃŒ๋œ ์ดํ›„์—๋Š” ์ถ”์ถœ๋œ ์ž„๋ฐฐ๋”ฉ์„ ๋ฒกํ„ฐ๊ณต๊ฐ„์—์„œ ์‹œ๊ฐํ™”ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ถ„ํฌ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. (3) ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ์„ ์ด์šฉํ•œ ๋ถ„์•ผ 1) Face recognition Similarity learning์ด ํ™œ์šฉ๋˜๋Š” ๋ถ„์•ผ์—๋Š” Face recognition ๋ถ„์•ผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Face recognition ๊ธฐ์ˆ ์€ ์–ผ๊ตด์„ ํฌํ•จํ•˜๋Š” ์ž…๋ ฅ ์ •์ง€ ์˜์ƒ ๋˜๋Š” ๋น„๋””์˜ค์— ๋Œ€ํ•ด ์–ผ๊ตด ์˜์—ญ์˜ ์ž๋™์ ์ธ ๊ฒ€์ถœ ๋ฐ ๋ถ„์„์„ ํ†ตํ•ด ํ•ด๋‹น ์–ผ๊ตด์ด ์–ด๋–ค ์ธ๋ฌผ์ธ์ง€ ํŒ๋ณ„ํ•ด ๋‚ด๋Š” ๊ธฐ์ˆ ๋กœ ํŒจํ„ด์ธ์‹ ๋ฐ ์ปดํ“จํ„ฐ ๋น„์ „ ๋ถ„์•ผ์—์„œ ์˜ค๋žซ๋™์•ˆ ์—ฐ๊ตฌ๋˜์–ด ์˜จ ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. ์–ผ๊ตด์ธ์‹ ๊ธฐ์ˆ ์€ ์‘์šฉ์— ๋”ฐ๋ผ ์–ผ๊ตด ๊ฒ€์ฆ(Face Verification) ๊ทธ๋ฆฌ๊ณ  ์–ผ๊ตด ์‹๋ณ„(Face Identification) ๊ธฐ์ˆ ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ผ๊ตด ๊ฒ€์ฆ(Face Verification): ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๋‘ ๊ฐœ์˜ ์–ผ๊ตด ์˜์ƒ์ด ๋™์ผ ์ธ๋ฌผ์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” 1:1 ๊ฒ€์ฆ ๋ฌธ์ œ ์–ผ๊ตด ์‹๋ณ„(Face Identification): ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ํ•˜๋‚˜์˜ ์–ผ๊ตด ์˜์ƒ์ด ์‚ฌ์ „์— ๋“ฑ๋ก๋œ N ๋ช…์˜ ์ธ๋ฌผ ์ค‘ ์–ด๋–ค ์ธ๋ฌผ์— ํ•ด๋‹นํ•˜๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” 1:N ๊ฒ€์ฆ ๋ฌธ์ œ Image Classification ๋ฐฉ์‹์œผ๋กœ Face recognition์„ ํ•˜๋ ค๋ฉด ์—„์ฒญ๋‚œ ์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ธฐ์กด ํ•™์Šต ๋ฐ์ดํ„ฐ์— ์—†๋Š” ์ƒˆ๋กœ์šด ์‚ฌ๋žŒ์ด ์ถ”๊ฐ€๋  ๊ฒฝ์šฐ ๋„คํŠธ์›Œํฌ๋ฅผ ๋‹ค์‹œ ํ•™์Šตํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํ•œ๊ณ„๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ, Face recognition์—๋Š” Similarity learning์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ต์‹ฌ์€ DB์— ๋ณด์œ ํ•œ Class์˜ ๋Œ€ํ‘œ ์ด๋ฏธ์ง€์™€ ์ž…๋ ฅ๋œ Input ์ด๋ฏธ์ง€ ๊ฐ„์˜ distance๋ฅผ ๊ตฌํ•˜๊ณ  ์ด distance๋ฅผ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ์‚ฌ๋žŒ์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ด์ง€์š”. ์—ฌ๊ธฐ์„œ distance๋Š” Raw ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ๋น„๊ตํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์ง„ ์† ์–ผ๊ตด ์œ„์น˜๊ฐ€ ์ œ๊ฐ๊ธฐ ๋‹ค๋ฅด๊ฑฐ๋‚˜ ๊ทธ ์ดฌ์˜ ๊ฐ๋„๊ฐ€ ๋‹ค๋ฅด๋ฉด ์–ผ๊ตด ์ธ์‹ ์ •ํ™•๋„๊ฐ€ ๋‚ฎ์•„์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฌ์ง„์—์„œ ์–ผ๊ตด ์˜์—ญ์„ ์ฐพ์•„ ๋™์ผํ•œ ํ˜•ํƒœ์˜ ์ •๋ฉด ์–ผ๊ตด์„ ์ถ”์ถœํ•˜๋Š” ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์ด ์„ ํ–‰๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1) face detection, ์–ผ๊ตด ๊ฒ€์ถœ: ์‹œ์Šคํ…œ์— ์ž…๋ ฅ๋œ ์ด๋ฏธ์ง€์—์„œ ์–ผ๊ตด ์˜์—ญ์„ ์ฐพ์Œ (face detection, ์–ผ๊ตด ๊ฒ€์ถœ), 2) face alignment, ์–ผ๊ตด ์ •๋ ฌ: ๋ˆˆ๊ณผ ์ฝ” ๋“ฑ ์–ผ๊ตด์˜ ํŠน์ง•์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ ์„ ์ฐพ์Œ 3) ์ด ํŠน์ง•์ ์„ ์ด์šฉํ•ด ์–ผ๊ตด ์˜์—ญ์„ ๋™์ผํ•œ ํ˜•ํƒœ์™€ ํฌ๊ธฐ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค ์ „์ฒ˜๋ฆฌ๋ฅผ ๊ฑฐ์นœ ์ด๋ฏธ์ง€๋ฅผ CNN ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ต๊ณผ์‹œ์ผœ embedding ํ•˜๊ณ  ์ถœ๋ ฅ๋œ feature vector ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์–ผ๊ตด ์ธ์‹์—์„œ๋Š” ๊ฐ™์€ ์–ผ๊ตด์ด๋ฉด distance๊ฐ€ ์ž‘๊ฒŒ, ๋‹ค๋ฅธ ์–ผ๊ตด์ด๋ฉด distance๋ฅผ ํฌ๊ฒŒ ๋‚˜์˜ค๋„๋ก ๋„คํŠธ์›Œํฌ๋ฅผ ํ•™์Šต์‹œํ‚ค๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋ฉฐ, ๋Œ€๊ฐœ loss function์œผ๋กœ Triplet Loss๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Introduction ์–ผ๊ตด ์ธ์‹ ๋ชจ๋ธ ์ค‘ 2015๋…„์— ๋‚˜์˜จ FaceNet: A Unified Embedding for Face Recognition and Clustering์„ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ผ๊ตด ์ธ์‹ ๋ชจ๋ธ์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฐœ๋…์ธ Triplet Loss๊ฐ€ ์ฒ˜์Œ ๋“ฑ์žฅํ•œ ๋…ผ๋ฌธ์ž…๋‹ˆ๋‹ค. FaceNet์€ ๊ฐ๊ฐ์˜ ์–ผ๊ตด ์ด๋ฏธ์ง€๋ฅผ 128์ฐจ์›์œผ๋กœ ์ž„๋ฒ ๋”ฉํ•œ ํ›„ ์œ ํด๋ฆฌ๋“œ ๊ณต๊ฐ„์—์„œ ์ด๋ฏธ์ง€ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋น„๊ตํ•˜์—ฌ Face recognition / verification / clustering์„ ํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด ๋ชจ๋ธ๋“ค๊ณผ ๋‹ค๋ฅด๊ฒŒ face alignment๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ ๋„ accuracy 99.63% (LFW dataset), 95.12% (YTF dataset)์˜ ๋†’์€ ์„ฑ๋Šฅ์„ ๋ณด์ด๋ฉฐ, end-to-end training์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ด FaceNet์ด ๊ฐ€์ง€๋Š” ์žฅ์ ์ž…๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ CNN network๋ฅผ ์ด์šฉํ•˜์—ฌ input image์— ๋Œ€ํ•ด 128์ฐจ์›์˜ embedding vector๋ฅผ ์ถ”์ถœํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•œ CNN์€ 2015๋…„ ๋‹น์‹œ ๊ณ ์„ฑ๋Šฅ์ด์—ˆ๋˜ ZF net๊ณผ GoogLeNet์ž…๋‹ˆ๋‹ค. ์ด์ „ ๋ชจ๋ธ๋“ค์€ 2D, 3D์˜ face alignment๋ฅผ ์ˆ˜ํ–‰ํ•˜์˜€์ง€๋งŒ, FaceNet์€ ๊ทธ๋Ÿฐ ๊ณผ์ •์„ ์ƒ๋žตํ•˜๊ณ  ์ด๋ฏธ์ง€๋ฅผ ํ†ต์งธ๋กœ CNN์— ์ œ์‹œํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•œ ZF Net ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜ ์‚ฌ์šฉํ•œ GoogLeNet ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜ Loss function - Triplet loss ์ž์„ธํ•œ ๋‚ด์šฉ์€ Triplet Loss์— ๋Œ€ํ•ด ์ •๋ฆฌํ•œ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. ์ €์ž๋“ค์€ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ Hard Positive / Hard Negative / Semi-Hard Negative ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ์ง€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Hard Positive data์™€ Hard Negative data ์Œ์„ ๋ชจ๋ธ์— ์ œ์‹œํ•˜์—ฌ ํ•™์Šต์‹œํ‚ค๋ฉด ๋” ๋น ๋ฅธ convergence๊ฐ€ ๊ฐ€๋Šฅํ–ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Hard Positive data - ๊ฐ™์€ ์‚ฌ๋žŒ์ด์ง€๋งŒ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ์‚ฌ๋žŒ. - anchor data์™€ positive data์˜ embedding vector ๊ฐ„ ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ๋Œ€๋กœ ๋งŒ๋“œ๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. Hard Negative data - ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด์ง€๋งŒ ๊ฐ™์€ ์‚ฌ๋žŒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ์‚ฌ๋žŒ - anchor data์™€ negative data์˜ embedding vector ๊ฐ„ ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ์†Œ๋กœ ๋งŒ๋“œ๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์—์„œ argmax, argmin์„ ๊ณ„์‚ฐํ•˜์—ฌ Hard positive, Hard negative๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด ์—ฐ์‚ฐ๋Ÿ‰์ด ๋„ˆ๋ฌด ๋งŽ์•„์ง‘๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ €์ž๋“ค์€ ํ•œ ์‚ฌ๋žŒ์˜ ์ด๋ฏธ์ง€ ์ค‘์—์„œ Hard positive data๋ฅผ ์•ฝ 40์—ฌ ๊ฐœ๋ฅผ ๊ณจ๋ผ mini-batch๋กœ ์„ ์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋žœ๋คํ•˜๊ฒŒ ๊ตฌํ•œ negative image ์ค‘ ๋ฌด์ž‘์œ„๋กœ ๊ณจ๋ผ mini-batch์— ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ Hard Negative data๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๋Š” ๊ฒฝ์šฐ ๋ชจ๋“  embedding vector f(x)๋ฅผ 0์œผ๋กœ ๋งŒ๋“œ๋Š” ์ฒ˜์ฐธํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Hard Negative data ๋Œ€์‹  Semi-hard negative data๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Semi-hard negative data๋Š” ์œ„์˜ ์‹์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด Euclidean distance๊ฐ€ Positive data๋ณด๋‹ค ์‚ด์ง ํฐ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์ฆ‰ margin ฮฑ (anchor - positive, anchor- negative ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ํ™•์—ฐํžˆ ์ฐจ์ด๊ฐ€ ๋‚˜๋„๋ก ํ•ด์ฃผ๋Š” ์žฅ์น˜)๊ฐ€ ์—†์–ด์„œ positive data์™€ ๊ตฌ๋ถ„์ด ์–ด๋ ค์šด ๋ฐ์ดํ„ฐ์ธ ๊ฒƒ์ด์ฃ . ๋ฐ์ดํ„ฐ ์…‹ LFW (Labeled Faces in the Wild) ํ†ต์ œ๋˜์ง€ ์•Š๋Š” ํ™˜๊ฒฝ์—์„œ์˜ ์•ˆ๋ฉด ์ธ์‹ ๋ฌธ์ œ๋ฅผ ์—ฐ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋””์ž์ธ๋œ ์–ผ๊ตด ์‚ฌ์ง„ ๋ฐ์ดํ„ฐ ์…‹์ž…๋‹ˆ๋‹ค. ์ธ๋ฌผ์˜ ์ด๋ฆ„์ด ๋ผ๋ฒจ๋ง ๋œ 1680์—ฌ ๋ช…์˜ ์‚ฌ๋žŒ์ด ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉฐ 13,233 ๊ฐœ์˜ ์ด๋ฏธ์ง€๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. YTF (YouTube Faces) You Tube์—์„œ ๋‹ค์šด๋กœ๋“œํ•œ ์˜์ƒ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์–ผ๊ตด ์ธ์‹ ์˜์ƒ ๋ฐ์ดํ„ฐ ์…‹์ž…๋‹ˆ๋‹ค.<NAME>์ƒ์—์„œ์˜ ํ†ต์ œ๋˜์ง€ ์•Š์€ ์•ˆ๋ฉด ์ธ์‹์˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•ด ๋””์ž์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 1,595๋ช…์˜ ์‚ฌ๋žŒ์ด ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉฐ, 3,425๊ฐœ์˜<NAME>์ƒ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์„ฑ๋Šฅ FaceNet์˜ ์—ฌ๋Ÿฌ ๊ตฌ์กฐ๋“ค์ด ๊ฐ–๋Š” ์„ฑ๋Šฅ์„ ๋น„๊ตํ•œ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. (NN = ZF Net, NNS = GoogLeNet) ์–ผ๊ตด ํฌ๊ธฐ์— ๋งž๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์ž˜๋ผ์ฃผ๋Š” ๊ณผ์ •์„ ์ถ”๊ฐ€๋กœ ์ˆ˜ํ–‰ํ•˜๋ฉด LFW์—์„œ 98.87%์˜ ์ •ํ™•๋„๋ฅผ ๋ณด์˜€๊ณ , ์ถ”๊ฐ€์ ์ธ alignment๋ฅผ ํ–ˆ์„ ๊ฒฝ์šฐ์—๋Š” 99.63%๊นŒ์ง€ ์ •ํ™•๋„๊ฐ€ ์˜ค๋ฅด๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ์—ˆ์œผ๋ฉฐ, YTF์—์„œ๋Š” 95.12%์˜ ์ •ํ™•๋„๋ฅผ ๋ณด์—ฌ์ฃผ์—ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. False accept๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ์„ ๊ฐ™๋‹ค๊ณ  ๋ถ„๋ฅ˜ํ•œ ๊ฒฝ์šฐ์ด๊ณ , False reject๋Š” ๊ฐ™์€ ์‚ฌ๋žŒ์„ ๋‹ค๋ฅด๋‹ค๊ณ  ๋ถ„๋ฅ˜ํ•œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋ช‡ ๊ฐœ๋Š” ์ธ๊ฐ„์ด ๋ณด๊ธฐ์—๋„ ์•Œ์•„๋ณด๊ธฐ ํž˜๋“ค ์ •๋„ ๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ๋Œ€๋‹จํžˆ ๋†’๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Reference ์›๋…ผ๋ฌธ: FaceNet: A Unified Embedding for Face Recognition and Clustering ๋ธ”๋กœ๊ทธ DeepLeraningAI | What is face recognition ๋”ฅ๋Ÿฌ๋‹ ๊ธฐ๋ฐ˜ ๊ณ ์„ฑ๋Šฅ ์–ผ๊ตด์ธ์‹ ๊ธฐ์ˆ  ๋™ํ–ฅ kakaoenterprise | ์–ผ๊ตด์ธ์‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ ํ–‰ ์—ฐ๊ตฌ๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค ๋‚ด ๋งˆ์Œ๋Œ€๋กœ FaceNet ๋…ผ๋ฌธ ๋ฆฌ๋ทฐ - Triplet Loss๋ž€? Face Recognition 2. FaceNet ๋…ผ๋ฌธ ๋ฆฌ๋ทฐ 2) Visual Recommendation system ์ถ”์ฒœ ์‹œ์Šคํ…œ์ด๋ž€? ์ถ”์ฒœ ์‹œ์Šคํ…œ์€ ์˜ํ™”, ์‡ผํ•‘, ์Œ์•… ๋“ฑ ๋‹ค์–‘ํ•œ ์˜์—ญ์—์„œ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š” ๊ธฐ๋Šฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„๋งˆ ๋งŽ์€ ๋ถ„๋“ค์—๊ฒŒ ์ต์ˆ™ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ถ”์ฒœ ์‹œ์Šคํ…œ์€ ์ด์šฉ์ž๊ฐ€ ์•ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ œ๊ณตํ•˜๋ฉด ์ˆ˜๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•„ํ„ฐ๋งํ•ด ์‚ฌ์šฉ์ž ๊ฐ„์˜ ์œ ์‚ฌ๋„, ์•„์ดํ…œ ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ์•Œ์•„๋‚ด๊ณ , ์ด์šฉ์ž์˜ ์„ ํ˜ธ๋„์™€ ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋น„์Šทํ•œ ์ œํ’ˆ์„ ์ถ”์ฒœํ•ด ์ค๋‹ˆ๋‹ค. ์ถ”์ฒœ ์‹œ์Šคํ…œ ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•ด ์•„์ฃผ ๊ฐ„๋žตํ•˜๊ฒŒ๋งŒ ๋‹ค๋ฃจ์–ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ๋ถ„์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 1. Content-based Filtering (์ฝ˜ํ…์ธ  ๊ธฐ๋ฐ˜ ํ•„ํ„ฐ๋ง) ์ฝ˜ํ…์ธ  ๊ธฐ๋ฐ˜ ํ•„ํ„ฐ๋ง์€ ์œ ์ €๊ฐ€ ๊ด€์‹ฌ ์žˆ๋Š” ์•„์ดํ…œ์˜ ์†์„ฑ์„ ๋ถ„์„ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์•„์ดํ…œ์„ ์ถ”์ฒœํ•ด ์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋’ค์—์„œ ์„ค๋ช…๋“œ๋ฆด Collaborative Filtering(Item-based)์™€ ๋‹ค๋ฅธ ์ ์€ ๋‹ค๋ฅธ ์œ ์ €์˜ ์ •๋ณด๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ์‚ฐ ์˜ท๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ธด ์˜ท์„ ์ถ”์ฒœํ•ด ์ฃผ๊ฑฐ๋‚˜ ๋‰ด์Šค ๊ธฐ์‚ฌ๊ฐ€ ๋น„์Šทํ•˜๊ฑฐ๋‚˜ ๊ด€๋ จ๋œ ๋‹ค๋ฅธ ๋‰ด์Šค๋ฅผ ์ถ”์ฒœํ•ด ์ฃผ๋Š” ๊ฒฝ์šฐ์— ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ฝ˜ํ…์ธ  ๊ธฐ๋ฐ˜ ํ•„ํ„ฐ๋ง์€ ์•„์ดํ…œ๋“ค์˜ feature๋ฅผ ์ž˜ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค feature extraction ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜๋Š”์ง€๊ฐ€ ์ค‘์š”ํ•˜๊ณ , ์ถ”์ถœ๋œ feature๋ฅผ ํ†ตํ•ด ์•„์ดํ…œ์„ ๋น„๊ตํ•  ์œ ์‚ฌ๋„(Similarity)์— ๋Œ€ํ•œ ์„ ํƒ๋„ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. 2. Collaborative Filtering (ํ˜‘์—… ํ•„ํ„ฐ๋ง) ํ˜‘์—… ํ•„ํ„ฐ๋ง์€ ์‚ฌ์šฉ์ž ๊ทธ๋ฃน์ด ํ˜•์„ฑ๋˜์–ด ์žˆ๊ณ , ๊ทธ๋“ค ๊ฐ„์˜ ํ‰๊ฐ€ ์ ์ˆ˜์™€ ์„ ํ˜ธ๋„๋ฅผ ๊ณ ๋ คํ•˜์—ฌ, ์‚ฌ์šฉ์ž์˜ ์˜ˆ์ธก ์ ์ˆ˜์™€ ์„ ํ˜ธ๋„๊ฐ€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ํ•œ๋งˆ๋””๋กœ, ์‚ฌ์šฉ์ž์™€ ๋น„์Šทํ•œ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์•„์„œ ๊ทธ ์‚ฌ์šฉ์ž๋Š” ์–ด๋–ค ํ‰๊ฐ€๋ฅผ ํ–ˆ๋Š”์ง€๋ฅผ ์‚ดํŽด๋ณด๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. 2_1. Memory-based Filtering User-Item Matrix๋กœ๋ถ€ํ„ฐ ๋„์ถœ๋˜๋Š” ์œ ์‚ฌ๋„ ๊ธฐ๋ฐ˜์œผ๋กœ ์•„์ดํ…œ์„ ์ถ”์ฒœ์„ ํ•ฉ๋‹ˆ๋‹ค. 2_1_1. User-based ์œ ์ € ๊ฐ„์˜ ์„ ํ˜ธ๋„(์•„์ดํŒ€์— ๋Œ€ํ•œ ์ ์ˆ˜)๋‚˜ ๊ตฌ๋งค ์ด๋ ฅ์„ ๋น„๊ตํ•˜์—ฌ ์ถ”์ฒœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ User-based CF๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ์œ ์ € A๊ฐ€ [ํฌ๋„, ๋”ธ๊ธฐ, ์ˆ˜๋ฐ•, ๊ทค]๋ฅผ ์ƒ€๋‹ค๊ณ  ํ•˜๋ฉด, ์œ ์ € A์™€ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์‡ผํ•‘ ๋ชฉ๋ก์„ ๊ฐ–๊ณ  ์žˆ๋Š” ์œ ์ € B [๋”ธ๊ธฐ, ์Šˆ๋ฐ•]์—๊ฒŒ [๊ทค, ํฌ๋„]๋ฅผ ์ถ”์ฒœํ•ด ์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 2_1_2. Item-based User-based์™€๋Š” ๋ฐ˜๋Œ€๋กœ ์•„์ดํ…œ ๊ฐ„์˜ ์œ ์ € ๋ชฉ๋ก์„ ๋น„๊ตํ•˜์—ฌ ์ถ”์ฒœํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ์ˆ˜๋ฐ•์„ [์œ ์ € A, ์œ ์ € B, ์œ ์ € C]๊ฐ€ ๊ตฌ๋งค๋ฅผ ํ–ˆ๋‹ค๋ฉด, ์ˆ˜๋ฐ•์„ ์‚ฐ ์œ ์ €์˜ ๋ชฉ๋ก๊ณผ ๊ฐ€์žฅ ๋น„์Šทํ•œ ํฌ๋„ [์œ ์ € A, ์œ ์ € B]์„ ์œ ์ € C์—๊ฒŒ ์ถ”์ฒœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 2_2. Model-based Filtering Model-based CF๋Š” User-Item interaction์„ ๋จธ์‹ ๋Ÿฌ๋‹์ด๋‚˜ ๋”ฅ๋Ÿฌ๋‹๊ณผ ๊ฐ™์€ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ถ„์„(Association rule)๊ณผ ๊ฐ™์ด ์•„์ดํ…œ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ํ•™์Šตํ•  ์ˆ˜๋„ ์žˆ๊ณ , Clustering์„ ํ†ตํ•ด ์œ ์‚ฌํ•œ ์•„์ดํ…œ์ด๋‚˜ ์œ ์ € ๊ฐ„์˜ ๊ทธ๋ฃน์„ ํ˜•์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์™ธ์—๋„ Matrix Factorization, Bayesian Network, Decision Tree ๋“ฑ ๋งŽ์€ ๋ฐฉ๋ฒ•๋ก ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Collaborative Filtering (ํ˜‘์—… ํ•„ํ„ฐ๋ง)์ด ๊ฐ€์ง€๋Š” ์žฅ/๋‹จ์ ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. * ์žฅ์  : ์•„์ดํ…œ์— ๋Œ€ํ•œ ์ฝ˜ํ…์ธ ์˜ ์ •๋ณด ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ * ๋‹จ์  1 Cold Start : โ€˜์ƒˆ๋กœ ์‹œ์ž‘ํ•  ๋•Œ ๊ณค๋ž€ํ•จโ€™์„ ์˜๋ฏธํ•˜๋ฉฐ, ํ‰๊ฐ€๋˜์ง€ ์•Š์€ ์•„์ดํ…œ์— ๋Œ€ํ•ด ์ถ”์ฒœ์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. * ๋‹จ์  2 Data Sparsity : ์ˆ˜๋งŽ์€ ์œ ์ €์™€ ์•„์ดํ…œ ์‚ฌ์ด์— ๊ฒฝํ—˜ํ•˜์ง€ ๋ชปํ•œ ๋ฐ์ดํ„ฐ์˜ ๋Œ€๋ถ€๋ถ„์„ ์ฐจ์ง€ํ•จ (Ex. ์˜จ๋ผ์ธ ์‡ผํ•‘๋ชฐ์—์„œ ๋‚ด๊ฐ€ ๊ตฌ๋งคํ•œ ๋ชฉ๋ก๋ณด๋‹ค ๊ตฌ๋งคํ•˜์ง€ ์•Š์€ ๋ชฉ๋ก์ด ์••๋„์ ์œผ๋กœ ๋งŽ์Œ) * ๋‹จ์  3 Scalability : ์œ ์ €์™€ ์•„์ดํ…œ์˜ ์ˆ˜๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๊ฐ€ ๊ธฐํ•˜๊ธ‰์ˆ˜๋กœ ์ปค์ง 3. Hybrid Filtering ์ฝ˜ํ…์ธ  ๊ธฐ๋ฐ˜ ํ•„ํ„ฐ๋ง๊ณผ ํ˜‘์—… ํ•„ํ„ฐ๋ง์€ ์žฅ๋‹จ์ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๋ฐฉ๋ฒ•์„ ๊ฐ™์ด ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ Hybrid Filtering์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Visual similarity๋ฅผ ์ด์šฉํ•œ ์ถ”์ฒœ ์‹œ์Šคํ…œ ์ถ”์ฒœ ์‹œ์Šคํ…œ ์ค‘ Visual similarity, ์ฆ‰ ์‹œ๊ฐ์  ์œ ์‚ฌ์„ฑ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋น„์Šทํ•œ ์ œํ’ˆ์„ ์ถ”์ฒœํ•ด ์ฃผ๋Š” ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค. ์ถ”์ฒœ๋œ ์ด๋ฏธ์ง€๋Š” ์„ ํƒํ•œ ์ด๋ฏธ์ง€์™€ ์œ ์‚ฌํ•œ ์ƒ‰, ์งˆ๊ฐ ๋ฐ ๋ชจ์–‘๊ณผ ๊ฐ™์€ ์‹œ๊ฐ์  ์†์„ฑ์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ ์ €๊ฐ€ ๊ด€์‹ฌ ์žˆ๋Š” ์•„์ดํ…œ์˜ ์†์„ฑ์„ ๋ถ„์„ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์•„์ดํ…œ์„ ์ถ”์ฒœํ•ด ์ค€๋‹ค๋Š” ์ ์—์„œ Content-based filtering์— ํ•ด๋‹น๋˜๊ฒ ์ฃ ? Visual similarity๋ฅผ ์ด์šฉํ•œ ์ถ”์ฒœ ์‹œ์Šคํ…œ ์ค‘ Deep Learning based Large Scale Visual Recommendation and Search for E-Commerce, 2017๋ผ๋Š” ๋…ผ๋ฌธ์„ ์ฝ๊ณ  ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Introduction ์ „ํ†ต์ ์ธ ์ „์ž ์ƒ๊ฑฐ๋ž˜ ๊ฒ€์ƒ‰ ์—”์ง„์€ ํ…์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜ ๊ฒ€์ƒ‰๋งŒ ์ง€์›ํ•˜๋ฏ€๋กœ ํŒจ์…˜ ์นดํ…Œ๊ณ ๋ฆฌ ๊ฒ€์ƒ‰์— ์„ฑ๋Šฅ์ด ์ œํ•œ์ ์ž…๋‹ˆ๋‹ค. ํŒจ์…˜ ์นดํ…Œ๊ณ ๋ฆฌ ๊ฒ€์ƒ‰์€ ํŠนํžˆ๋‚˜ ์ œํ’ˆ์˜ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ Collaborative filtering (ํ˜‘์—… ํ•„ํ„ฐ๋ง) ๊ธฐ๋ฒ•๋“ค์€ ์‚ฌ์šฉ์ž์˜ ํด๋ฆญ์ด๋‚˜ ๊ตฌ๋งค ์ด๋ ฅ ๋“ฑ์— ์ง‘์ค‘ํ•˜๋ฏ€๋กœ ์ œํ’ˆ์˜ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋Š” ๋ฌด์‹œํ•˜๋Š” ํŠน์„ฑ์ด ์žˆ์œผ๋ฉฐ, cold start (๊ตฌ๋งค ์ด๋ ฅ์ด ์—†๋Š” ์‹ ์ƒํ’ˆ์˜ ๊ฒฝ์šฐ ์ถฉ๋ถ„ํ•œ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ถ”์ฒœํ•˜์ง€ ์•Š๋Š” ํŠน์„ฑ)์™€ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๋…ผ๋ฌธ์—์„œ ๋‹ฌ์„ฑํ•˜๊ณ ์ž ํ•œ Visual Recommendation (catalog ์ด๋ฏธ์ง€์™€ ๋น„์Šทํ•œ ๋‹ค๋ฅธ catalog ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์•„์ฃผ๋Š” ์‹œ์Šคํ…œ)๊ณผ Visual Search (์‚ฌ์šฉ์ž๊ฐ€ ์˜ฌ๋ฆฐ ์ด๋ฏธ์ง€์™€ ๋น„์Šทํ•œ catalog ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์•„์ฃผ๋Š” ์‹œ์Šคํ…œ)์˜ ๋ชฉํ‘œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์—ฐ์˜ˆ์ธ์ด ์ž…์€ ๋“œ๋ ˆ์Šค์ด๊ฑฐ๋‚˜ ์นœ๊ตฌ๊ฐ€ ์†Œ์œ ํ•œ ํ•ธ๋“œ๋ฐฑ ๊ฐ™์€ ์•„์ดํ…œ์˜ ์‚ฌ์ง„์„ ์—…๋กœ๋“œํ•˜์—ฌ ์ œํ’ˆ์„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํ•ด๊ฒฐํ•ด์•ผ ํ•  ๋ฌธ์ œ๋กœ๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. catalog ์ด๋ฏธ์ง€๋ผ๊ณ  ํ•˜๋”๋ผ๋„ ๊ฐ™์€ ์ œํ’ˆ์— ๋Œ€ํ•ด ์ˆ˜์—†์ด ๋งŽ์€ ์‚ฌ์ง„์ด ์กด์žฌํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆผ (a)์—์„œ ๋ณด๋“ฏ ์˜ท๊ฑธ์ด์— ๊ฑธ๋ฆฐ ์‚ฌ์ง„, ๋งˆ๋„คํ‚น์ด ์ž…์€ ์‚ฌ์ง„, ๋ชจ๋ธ์ด ์ž…์€ ์‚ฌ์ง„ ๋“ฑ ๋‹ค์–‘ํ•˜๊ณ  ๋น›์ด๋‚˜ ์ดฌ์˜ ๊ฐ๋„์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ์‚ฌ์ง„์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์ด ์ƒ๊ฐํ•˜๋Š” "์œ ์‚ฌํ•จ"์€ ๊ฝค๋‚˜ ๋ชจํ˜ธํ•˜๊ณ  ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆผ (b)์—์„œ ๋ณด๋“ฏ "๊ธฐ๊ดดํ•œ ๊ทธ๋ฆผ"์ด ๊ทธ๋ ค์ง„ ํ‹ฐ์…”์ธ ๋ผ๋Š” ์ธก๋ฉด์—์„œ ์‚ฌ๋žŒ๋“ค์€ ์ด๋ฅผ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์€ ๋‹จ์ˆœํžˆ ์ƒ‰์ด๋‚˜ ๋ชจ์–‘, ์งˆ๊ฐ๊ณผ ๊ฐ™์€ ๋‚ฎ์€ ์ˆ˜์ค€์˜ feature๊ฐ€ ์•„๋‹Œ ๋” ๋†’์€ ์ˆ˜์ค€์˜ feature๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Visual Search (์‚ฌ์šฉ์ž๊ฐ€ ์˜ฌ๋ฆฐ ์ด๋ฏธ์ง€์™€ ๋น„์Šทํ•œ catalog ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์•„์ฃผ๋Š” ์‹œ์Šคํ…œ)์„ ์‹คํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์˜ฌ๋ฆฐ ์ด๋ฏธ์ง€์— ํฌํ•จ๋œ ๋ถˆํ•„์š”ํ•œ ๋ฐฐ๊ฒฝ, ์•ˆ ์ข‹์€ ์กฐ๋ช…, ์ž˜๋ฆฐ ์ œํ’ˆ ์‚ฌ์ง„ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋…ผ๋ฌธ์—์„œ๋Š” end-to-end๋กœ Visual Recommendation๊ณผ Visual Search๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋ชจ๋ธ VisNet์„ ์„ค๊ณ„ํ•˜์˜€๊ณ , ํ•ด๋‹น ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•ด ํ•„์ˆ˜์ ์ธ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•๋ก ์„ ์ œ์‹œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ VisNet์˜ ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ๋Š” Triplet network๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ํ•˜๋ฉฐ ๋น„๊ต์  ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. Feature extraction์„ ์œ„ํ•ด Deep layer & Shallow layer๋ผ๋Š” ๋‘ ๊ฐœ์˜ ๋ณ‘๋ ฌ์  ๋„คํŠธ์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Deep layer์—๋Š” VGG-16์„ ์ด์šฉํ•˜์—ฌ, Shallow layer์—๋Š” ์–•์€ convolution layer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Feature extraction์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. Deep layer & Shallow layer๋ฅผ ๋™์‹œ์— ์‚ฌ์šฉํ•˜๊ธฐ์— Input image์˜ high level & low level detail์„ ๋ชจ๋‘ ์žก์•„๋‚ผ ์ˆ˜ ์žˆ์—ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. input ๊ฐ’์œผ๋กœ query/positive/negative image 3๊ฐœ์˜ ์ด๋ฏธ์ง€ ์„ธํŠธ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. positive image๋Š” query image์™€ ์œ ์‚ฌํ•œ ์ด๋ฏธ์ง€์ด๊ณ , negative image๋Š” query image์™€ ์œ ์‚ฌํ•˜์ง€ ์•Š์€ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. input image๋ฅผ ๋ชจ๋‘ VisNet์— ํ†ต๊ณผ์‹œ์ผœ Feature vector๋ฅผ ๊ตฌํ•˜๊ณ , ๊ฐ Feature vector ์‚ฌ์ด์˜ Euclidean distance๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. Loss function์œผ๋กœ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ Triplet loss๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์…‹ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ์…‹์€ ๋‘ ๊ฐœ์ž…๋‹ˆ๋‹ค. 1) Flipkart Fashion Dataset: ์ €์ž๋“ค์ด ๋…์ž์ ์œผ๋กœ ์ œ์ž‘ํ•œ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ๋ชจ๋‘ catalog ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. ์ˆ˜๋งŽ์€ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์†์—์„œ ์œ ์‚ฌ๋„๋ฅผ ํ‰๊ฐ€ํ•˜์—ฌ query image / positive image/ negative image ๋ฐ์ดํ„ฐ ์…‹์„ ๋‚˜๋ˆ„๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. BISS (Basic Image Similarity Scorer)๋ผ๋Š” ์ง€ํ‘œ๋ฅผ ๋„์ž…ํ•˜์—ฌ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ์ •๋Ÿ‰์ ์œผ๋กœ ํ‰๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์ด ์‚ฌ์šฉํ•œ BISS๋Š” ์•„๋ž˜ 3๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. 1. AlexNet์„ ํ†ต๊ณผ์‹œ์ผœ ์–ป์€ feature vecotor 2. ColorHist: ์ด๋ฏธ์ง€์˜ ์ƒ‰์ƒ ํžˆ์Šคํ† ๊ทธ๋žจ 3. PatterNet: ์ด๋ฏธ์ง€์˜ ํŒจํ„ด์„ ์ธ์‹ํ•˜๋„๋ก ํ›ˆ๋ จ๋œ AlexNet์„ ํ†ต๊ณผ์‹œ์ผœ ์–ป์€ feature vector ๊ฐ BISS๋“ค์€ query image๋งˆ๋‹ค 1000 nearest neighbor๋ฅผ ์„ ๋ณ„ํ•˜๊ณ , ๊ทธ์ค‘ ์œ ์‚ฌ๋„๊ฐ€ ๋†’์€ ์ƒ์œ„ 200๊ฐœ๋ฅผ positive image๋ผ๊ณ  ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜์œ„ 500-1000๊ฐœ๋Š” in-class negative image (query image์™€ ์ผ๋ถ€ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€)๋ผ๊ณ  ํ•˜์˜€๊ณ  1000 nearest neighbor์— ํฌํ•จ๋˜์ง€ ์•Š์€ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋Š” out-of-class negative (query image์™€ ์™„์ „ํžˆ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€)๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. 2) Exact Street2Shop Dataset: ๊ณต๊ฐœ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ 25๊ฐœ์˜ ๋‹ค๋ฅธ ์˜จ๋ผ์ธ ํŒ๋งค ์‚ฌ์ดํŠธ์—์„œ ์ˆ˜์ง‘ํ•œ 404,683๊ฐœ์˜ ๋ฌผ๊ฑด ์‚ฌ์ง„ (shop photo)๊ณผ 20,357๊ฐœ์˜ ๊ฑฐ๋ฆฌ์—์„œ ์ดฌ์˜ํ•œ ์‚ฌ์ง„(street photo)์„ ํฌํ•จํ•œ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ์ด 39,479 ์ข…๋ฅ˜์˜ ์˜ท์ด shop photo - street photo ์Œ์„ ์ด๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์„ฑ๋Šฅ Visual Recommendation VisNet์„ ์ด์šฉํ•ด Visual Recommendation (catalog ์ด๋ฏธ์ง€์™€ ๋น„์Šทํ•œ ๋‹ค๋ฅธ catalog ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์•„์ฃผ๋Š” ์‹œ์Šคํ…œ)์„ ์‹œํ–‰ํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜ Table 1์— ์ œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์—๊ฒŒ 5000๊ฐœ์˜ query image์™€ ๊ฐ query image์— ๋Œ€ํ•ด VisNet์ด ์ถ”์ฒœํ•œ image ์Œ์„ ์ œ์‹œํ•˜๊ณ  very bad / bad/ good / excellent 4๋‹จ๊ณ„๋กœ ํ‰๊ฐ€ํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 97%์˜ ์‚ฌ๋žŒ์ด VisNet ์ถ”์ฒœ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด excellent๋ผ๊ณ  ํ‰๊ฐ€ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” AlexNet, PatternNet, ColorHist์„ ์ด์šฉํ•˜์—ฌ n-nearest neighbor๋กœ ์ถ”์ฒœํ•œ image๋“ค ๋ณด๋‹ค ํ™•์—ฐํžˆ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. Visual Search VisNet์„ ์ด์šฉํ•ด Visual Search (์‚ฌ์šฉ์ž๊ฐ€ ์˜ฌ๋ฆฐ ์ด๋ฏธ์ง€์™€ ๋น„์Šทํ•œ catalog ์ด๋ฏธ์ง€๋ฅผ ์ฐพ์•„์ฃผ๋Š” ์‹œ์Šคํ…œ)์„ ์‹œํ–‰ํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜ Table 2์— ์ œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ query image์— ๋Œ€ํ•ด VisNet์ด ์ถ”์ฒœํ•œ image๋ฅผ k ๊ฐœ ์ œ์‹œํ•˜๊ณ  ๊ธฐ์กด์˜ Street2Shop Dataset ๋‚ด์—์„œ ์‹ค์ œ๋กœ ์Œ์„ ์ด๋ฃจ๊ณ  ์žˆ๋Š”์ง€ ํ‰๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. AlexNet์„ ์‚ฌ์šฉํ•˜์˜€์„ ๋•Œ๋Š” ์•„์ฃผ ์ฒ˜์ฐธํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๊ณ , VisNet w/o Shallow layers์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ๋Œ€ํญ ์„ฑ๋Šฅ์ด ๊ฐœ์„ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ VisNet with Shallow layers์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ์„ฑ๋Šฅ์ด ์•ฝ 3% ๋” ์ข‹์•„์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Reference ๋ธ”๋กœ๊ทธ Product Recommendation based on Visual Similarity on the web: Machine Learning / Data Science/ Deep Learning ์ถ”์ฒœ ์‹œ์Šคํ…œ(Recommendation System) ๊ฐœ์š” ์ถ”์ฒœ ์‹œ์Šคํ…œ (1) - ๊ฐœ์š” ๋ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ The Future of Visual Recommender Systems: Four Practical State-Of-The-Art Techniques ๊ด€๋ จ ๋…ผ๋ฌธ Deep Learning based Recommender System: A Survey and New Perspectives Deep Learning based Large Scale Visual Recommendation and Search for E-Commerce (4) ๋น„์ฃผ์–ผ ์ž„๋ฐฐ๋”ฉ ๋ชจ๋ธ ํ‰๊ฐ€(โ˜…์ž‘์„ฑ ์ค‘) .... 7. Generative model(์ƒ์„ฑ ๋ชจ๋ธ) ๊ฐ€์žฅ ํ•ซํ•œ ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. ์ •๋ง ์ด๊ฒŒ ์ธ๊ณต์ง€๋Šฅ์œผ๋กœ ํ–ˆ๋‚˜๊ฐ€ ์˜์‹ฌ๋  ์ •๋„๋กœ ๋น ๋ฅด๊ฒŒ ๋ฐœ์ „ํ•˜๊ณ  ์žˆ๋Š” ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. ์ตœ๊ทผ('21)์—๋Š” ์ƒ์„ฑ ๋ชจ๋ธ์„ ์ด์šฉํ•˜์—ฌ ๊ฐ€์ƒ ๋ชจ๋ธ๊นŒ์ง€ ์†์† ๋ณด์ด๋„ค์š”. ์•ž์œผ๋กœ ๋งŽ์€ ์—ฐ๊ตฌ๊ฐ€ ์ด๋ฃจ์–ด์ ธ ํฌ๊ฒŒ ๋ฐœ์ „์ด ๋  ๊ฒƒ์ด๋ผ ๋ณด์ด๋Š” ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. ์ƒ์„ฑ ๋ชจ๋ธ๋กœ ๋งŒ๋“ค์—ˆ์„ ๊ฒƒ์ด๋ผ ์ƒ๊ฐ๋˜๋Š” ๊ฐ€์ƒ ๋ชจ๋ธ '๋กœ์ง€' (1) ์ƒ์„ฑ ๋ชจ๋ธ ์•„์ด๋””์–ด ์ƒ์„ฑ ๋ชจ๋ธ์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ? ์ƒ์„ฑ ๋ชจ๋ธ(generative model)์€ ์ฃผ์–ด์ง„ ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜๊ณ  ํ•™์Šต ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋Š” ์œ ์‚ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ฃผ์–ด์ง„ training data์™€ ๊ฐ™์€ distribution์„ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด sample์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์•ž์„œ ๋‹ค๋ค˜๋˜ ์ปดํ“จํ„ฐ ๋น„์ „์—์„œ์˜ ๋ฌธ์ œ๋“ค์€ ๋Œ€๋ถ€๋ถ„์ด ์ผ๋ฐ˜์ ์ธ classification์— ๊ธฐ๋ฐ˜ํ•œ ๋ฌธ์ œ๋กœ์„œ feature space ์ƒ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๊ณ ์ž ํ•˜๋Š” label๋กœ ๊ตฌ๋ถ„ ์ง€์„ ์ˆ˜ ์žˆ๋Š” classifier๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ generative model์€ classifier๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด ์•„๋‹Œ, training set์˜ distribution์„ ๋ฐฐ์šฐ๋Š”๋ฐ ๋ชฉ์ ์„ ๋‘๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๊ฐ๊ฐ์„ label๋กœ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ , ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ joint distribution์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์„ธ์ƒ์— ์‹ค์ œ๋กœ ์กด์žฌํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ๋งˆ์น˜ ์กด์žฌํ•  ๊ฒƒ ๊ฐ™์€ ํŠนํžˆ ์šฐ๋ฆฌ๊ฐ€ ํ•™์Šต์‹œ์ผœ์ค€ ๋ฐ์ดํ„ฐ๋“ค ์‚ฌ์ด์— ์ •๋ง๋กœ ์กด์žฌํ•  ๊ฒƒ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒƒ์ด generative model์˜ ๋ชฉ์ ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์— ๋‚˜์™€์žˆ๋Š” P_model(x)์™€ P_data๊ฐ€ ์œ ์‚ฌํ•˜๋„๋ก ํ•™์Šต์„ ํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ P_model์€ Generative model์ด ๋งŒ๋“ค์–ด๋‚ธ Generated Sample๋“ค์˜ ๋ถ„ํฌ์ด๊ณ  P_data๋Š” Training data, ์ฆ‰ Real World์˜ ๋ฐ์ดํ„ฐ๋“ค์˜ ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋‘ ๊ฐ€์ง€ ๋Œ€๋ถ„๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. P_model์„ ํ™•์‹คํ•˜๊ฒŒ ์ •์˜ํ•˜๋Š” Explicit Density Estimation(ex. VAE, Approximate Density, etc.)๊ณผ Model์„ ์ •์˜ํ•˜์ง€ ์•Š๊ณ  P_model์—์„œ sample์„ ์ƒ์„ฑํ•˜๋Š” Implicit Density(ex. GAN, Markov Chain, etc.)์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ƒ์„ฑ ๋ชจ๋ธ์€ ์‹ค์ œ ์„ธ๊ณ„์˜ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๋น„์Šทํ•œ Fake Data๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, Time-series data ๋“ฑ์€ ์ƒ์„ฑ ๋ชจ๋ธ์—์„œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์ด๋‚˜ Planning์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜ ์ด๋ฏธ์ง€์—์„œ ๊ฐ•์•„์ง€ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” generative model์„ ๊ตฌ์ถ•ํ•œ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋•Œ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€, model์˜ distribution์ด ์‹ค์ œ ๋ฐ์ดํ„ฐ, ์ฆ‰ d t ์™€ ๊ฐ€์žฅ ๊ฐ€๊น๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋˜์–ด์•ผ ํ• ๊นŒ์š”? ์•„๋ž˜ ์ƒํ™ฉ์ด๋ผ๋ฉด ๋…น์ƒ‰์œผ๋กœ ํ‘œํ˜„๋œ model distribution, ฮธ p a a ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ, ๊ฐ€ ์ตœ์†Œํ™”๋˜๋Š” ๋ฌธ์ œ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. min โˆˆ d ( data p) generative model์„ ์ด์šฉํ•ด ํ•ด๊ฒฐํ•˜๋Š” ๋ฌธ์ œ๋Š” 3๊ฐ€์ง€ ์ •๋„๋กœ ์š”์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Density estimation : ์ฃผ์–ด์ง„ datapoint x์— ๋Œ€ํ•ด์„œ model์— ์˜ํ•ด ํ• ๋‹น๋˜๋Š” ํ™•๋ฅ ($p_{\theta}(x)}์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์„๊นŒ? Sampling : training์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š์ง€๋งŒ, training๊ณผ ๊ฐ€์žฅ ๋น„์Šทํ•œ ๋ถ„ํฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” model distribution์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ์„๊นŒ? Unsupervised representation learning : ํŠน์ • datapoint x์—์„œ ์˜๋ฏธ ์žˆ๋Š” feature representation์„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ข‹์€ generative model์ด๋ผ๋ฉด, ์œ„์˜ ์˜ˆ์‹œ์— ๋น—๋Œ€์–ด ํ‘œํ˜„ํ–ˆ์„ ๋•Œ density estimation์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค๋ฉด ๊ฐœ๋ผ๋Š” ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด์„œ๋Š” ๋†’์€ ฮธ ( ) ๋ฅผ, ๊ทธ ์™ธ์˜ ์ด๋ฏธ์ง€์—์„œ๋Š” ๋‚ฎ์€ ํ™•๋ฅ ์„ ๋ณด์ผ ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ณ  sampling์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค๋ฉด, ํ•™์Šต๋œ ๋ฐ์ดํ„ฐ ์…‹์—๋Š” ์—†์ง€๋งŒ ํ™•์‹คํžˆ ๊ฐ•์•„์ง€์ธ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ณ , representation learning์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค๋ฉด ๊ฐœ ํ’ˆ์ข…๊ณผ ๊ฐ™์€ ๋†’์€ ์ˆ˜์ค€์˜ feature๋ฅผ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ์–ด์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. generative model์ด ํ•ด๊ฒฐํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ๋“ค์˜ ํŠน์„ฑ์ƒ ์ •๋Ÿ‰์ ์ธ evaluation์€ non-trivial ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ์ •๋Ÿ‰์ ์ธ ์ง€ํ‘œ๊ฐ€ ์žˆ๊ธด ํ•˜์ง€๋งŒ, ํŠนํžˆ sampling์ด๋‚˜ representation learning์˜ ๊ฒฝ์šฐ ๋งŒ๋“ค์–ด๋‚ธ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ, ์ฐพ์•„๋‚ธ feature์— ๋Œ€ํ•œ ์งˆ์ ์ธ ํŠน์ง•์„ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๊ณ ๋กœ generative model์˜ ์ •๋Ÿ‰์ ์ธ evaluation ๋ฐฉ๋ฒ•์„ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ํ™œ๋ฐœํ•˜๊ฒŒ ์—ฐ๊ตฌ๊ฐ€ ์ง„ํ–‰๋˜๊ณ  ์žˆ๋Š” ์˜์—ญ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋ชจ๋“  generative model๋“ค์ด ๋ชจ๋“  ์ƒํ™ฉ์—์„œ ํšจ์œจ์ ์ด๊ณ  ์ข‹์€ ์„ฑ๋Šฅ์„ ์•„์ง๊นŒ์ง€ ๋ณด์ด์ง€๋Š” ๋ชปํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์— ์˜ํ•ด์„œ ์œ ํ–‰ํ•˜๊ณ  ์žˆ๋Š” GAN์˜ ๊ฒฝ์šฐ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์ด ๋งค์šฐ ํ•œ์ •์ ์ž…๋‹ˆ๋‹ค. Discrimitve vs Generative ์™œ Generative model์ด ํ•„์š” ํ•œ๊ฐ€? Dataset์˜ ์ฆ๊ฐ€ generative model์€ training set์ด ๋ถ€์กฑํ•œ ์ƒํ™ฉ์—์„œ ๋” training ์„ ๋งŒ๋“ค์–ด๋‚ด๋Š”๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Computer vision์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋งŽ์€ ๊ตฌ์กฐ๋“ค์€ ์ ์  ๋” ๋งŽ์€ ๋ฐ์ดํ„ฐ ์…‹์„ ์š”๊ตฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ๋‚˜ ์ตœ๊ทผ ๋“ค์–ด ์œ ํ–‰์ฒ˜๋Ÿผ ๋ฒˆ์ง€๊ณ  ์žˆ๋Š” vision transformer๋ฅผ ๋ฒ ์ด์Šค๋กœ ํ•œ ๊ตฌ์กฐ๋“ค์€ ๊ธฐ์กด์˜ CNN์„ ๋ฒ ์ด์Šค๋กœ ํ•œ ๋ชจ๋ธ๋ณด๋‹ค ํ›จ์”ฌ ๋” ๋งŽ์€ ์–‘์˜ training์„ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ๋žŒ์ด ์ผ์ผ์ด classification์„ ํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“œ๋Š” ๋ฐ์—๋Š” ๋งŽ์€ ์‹œ๊ฐ„๊ณผ ๋…ธ๋™๋ ฅ์ด ํˆฌ์ž…๋˜๋ฉฐ, ํ˜„์‹ค์ ์ธ ํ•œ๊ณ„ ์—ญ์‹œ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ด๋ฏธ์ง€๋Š” MNIST์—์„œ ๊ทธ๋Ÿด๋“ฏํ•œ training์„ ๋งŒ๋“ค์–ด ๋‚ธ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. Style transfer & artwork Style transfer, ๋˜๋Š” Image-to-image translation์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ์ด ์ž‘์—…์€ ๋‹ค์–‘ํ•œ artwork์— ์‘์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋Š” ์ƒ‰์ด ์—†๋Š” ์Šค์ผ€์น˜์— realistic ํ•œ coloring์„ ํ•ด์ฃผ๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์‹ค์ œ ์‚ฌ์ง„์„ ๋ฒ ์ด์Šค๋กœ ๋งŒํ™” ๊ทธ๋ฆผ์ฒด๊ฐ™์ด ์ด๋ฏธ์ง€๋ฅผ ๋ฐ”๊พธ์–ด์ฃผ๋Š”๋ฐ ์‘์šฉ์ด ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๊ตญ๋‚ด์—์„œ๋Š” ์›นํˆฐ ๊ทธ๋ฆผ์ฒด๋กœ ์‹ค์ œ ์‚ฌ์ง„์„ ๋ฐ”๊พธ์–ด์ฃผ๋Š” ํ•„ํ„ฐ๊ฐ€ ์นด๋ฉ”๋ผ ์•ฑ์—์„œ ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค. Super resolution ์•„๋ž˜ ์˜ˆ์‹œ๋Š” super resolution์— generative model์„ ์ ์šฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ super resolution ๋ชจ๋ธ ์ค‘ ํ•˜๋‚˜์ธ SRGAN์˜ ๊ฒฝ์šฐ ํ”ฝ์…€ ๋‹จ์œ„ resolution๋„ ๋” ๊ณ ํ•ด์ƒ๋„๋กœ transfer ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ์„ฑ ๋ชจ๋ธ์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์˜ˆ์ˆ ์  ์Šคํƒ€์ผ ์ด์ „ ๋ฐฉ๋ฒ• ์˜ˆ์ˆ  ์Šคํƒ€์ผ์„ ์–ด๋–ค ์ด๋ฏธ์ง€ ๋Œ€์ƒ์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด๋ฏธ์ง€์˜ ์˜ˆ์ˆ ์  ์Šคํƒ€์ผ๊ณผ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€์˜ ๋‚ด์šฉ์„ ๊ฒฐํ•ฉํ•ด ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ์‚ฌ์ง„์€ ์ด๋ฏธ์ง€ A๊ฐ€ ์˜ˆ์ˆ  ์Šคํƒ€์ผ๋กœ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋“ค๊ณผ ๊ฒฐํ•ฉํ•œ ๊ฒฐ๊ณผ๋ฌผ์ž…๋‹ˆ๋‹ค. ๋™์˜์ƒ์˜ ๋‹ค์Œ ํ”„๋ ˆ์ž„ ์˜ˆ์ธก ๋ฐฉ๋ฒ• ์ƒ์„ฑ<NAME>์ƒ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด ๋ฏธ๋ž˜์˜ ํ”„๋ ˆ์ž„์„ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœํ„ฐ(Lotter)๊ฐ€ ์ œ์•ˆํ•œ ๋‹ค์Œ ์ด๋ฏธ์ง€์—์„œ ์™ผ์ชฝ์˜ ์ด๋ฏธ์ง€๋Š” ์ด์ „ ํ”„๋ ˆ์ž„ ๋ชจ๋ธ์ด๊ณ , ์˜ค๋ฅธ์ชฝ์€ ์‹ค์ธก๊ฐ’์ž…๋‹ˆ๋‹ค. ์ฆ‰ ์ด์ „ ํ”„๋ ˆ์ž„์˜ ์–ผ๊ตด์„ ๋ณด๊ณ  ๋‹ค๋ฅธ ๊ฐ๋„์—์„œ์˜ ์–ผ๊ตด ๋ชจ์Šต ๋ฐ์ดํ„ฐ๋ฅผ ์˜ˆ์ธกํ•ด๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์Šˆํผ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€ ์Šˆํผ ํ•ด์ƒ๋„๋Š” ์ž‘์€ ์ด๋ฏธ์ง€์—์„œ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ์ „ํ†ต์ ์œผ๋กœ ๋ณด๊ฐ„๋ฒ•์€ ๋” ํฐ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ณด๊ฐ„๋ฒ•์€ ๋งค๋„๋Ÿฌ์šด ํšจ๊ณผ๋ฅผ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๊ณ ์ฃผํŒŒ ์„ธ๋ถ€ ์‚ฌํ•ญ๋“ค์„ ๋†“์น˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์‚ฌ์ง„์€ ์ดˆ๊ณ ํ•ด์ƒ๋„๋ผ๋Š” ํŠน์ • ๋ชฉ์ ์„ ์œ„ํ•ด ๋ ˆ๋”•(Ledig)์ด ์ œ์•ˆํ•œ SRResNet ๋ชจ๋ธ์˜ ์˜ˆ๋กœ ์›๋ณธ ์ด๋ฏธ์ง€๋ณด๋‹ค ๋” ๋†’์€ ํ•ด์ƒ๋„์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์ด๋ฏธ์ง€๋Š” ํŠน์ • ๋ชฉ์ ์„ ๊ฐ€์ง„ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ž‘์—…์„ '์ด๋ฏธ์ง€๋ฅผ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ(an inmage to image translation)'์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋กœ ์ด๋ฏธ์ง€ ์ƒ์„ฑํ•˜๊ธฐ ํ…์ŠคํŠธ ์„ค๋ช…์„ ๊ฐ€์ง€๊ณ  ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋ฆฌ๋“œ(Reed)์˜ ์ž์—ฐ์„ ์„ค๋ช…ํ•œ ํ…์ŠคํŠธ์—์„œ ๊ทธ๋ฆผ์„ ์ƒ์„ฑํ•œ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. ์‚ฌ์ง„์œผ๋กœ๋ถ€ํ„ฐ 3D ๋ชจ๋ธ ์ƒ์„ฑ ์šฐ(Wu)๊ฐ€ ์ œ์•ˆํ•œ ์ƒ์„ฑ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด 2D ์ด๋ฏธ์ง€๋กœ๋ถ€ํ„ฐ 3D ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ”„๋กœ์„ธ์Šค๋Š” ๋กœ๋ด‡ ๊ณตํ•™, ์ฆ๊ฐ• ํ˜„์‹ค, ์• ๋‹ˆ๋ฉ”์ด์…˜ ์‚ฐ์—… ๋“ฑ์—์„œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค Generative model์˜ ์ข…๋ฅ˜ Reference Stanford, cs231n : lecture 13 Generative models lecture note CMU, 10-708 PGM : lecture 17-18 Deep generative models lecture note https://minsuksung-ai.tistory.com/12 https://machinelearningmastery.com/impressive-applications-of-generative-adversarial-networks/ https://developers.google.com/machine-learning/gan/generative cs236 lecture slide : https://deepgenerativemodels.github.io/notes/index.html Gatys_Image_Style_Transfer_CVPR_2016_paper https://arxiv.org/pdf/1511.06380.pdf https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix (2) ์ƒ์„ฑ ๋ชจ๋ธ์„ ์œ„ํ•œ ์‚ฌ์ „ ์ง€์‹ 1) ์ƒ์„ฑ ๋ชจ๋ธ์˜ ์—ญ์‚ฌ(โ˜…์ž‘์„ฑ ์ค‘) ์ดˆ์ฐฝ๊ธฐ ์ƒ์„ฑ ๋ชจ๋ธ ์•„์ด๋””์–ด Boltzman machine Boltzman machine์€ ์ž…๋ ฅ์ธต 1๊ฐœ, ์€๋‹‰์ธต 1๊ฐœ ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  node๋“ค์„ ์ž‡๋Š” edge๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. RBM ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ๊ฐ™์€ layer ์•ˆ์—์„œ๋„ ์—ฐ๊ฒฐ์ด ์žˆ๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋ณผ์ธ ๋งŒ ๋จธ์‹ ์€ ์—๋„ˆ์ง€ ๊ธฐ๋ฐ˜ ๋ชจํ˜•์ธ๋ฐ์š”. ๋ณผ์ธ ๋งŒ ๋จธ์‹ ์˜ ๊ฒฐํ•ฉ ํ™•๋ฅ  ๋ถ„ํฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋„ˆ์ง€ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ( ) ๋Š” ํ•˜๋‚˜์˜ ์—๋„ˆ์ง€ ํ•จ์ˆ˜์ด๊ณ , Z๋Š” x ( ) 1 ์ด ๋˜๊ฒŒ ํ•˜๋Š” (ํ™•๋ฅ ์˜ ํ•ฉ์ด 1์ด ๋˜๊ฒŒ ํ•˜๋Š”) ๋ถ„ํ• ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ( ) e p ( E ( ) ) ์—๋„ˆ์ง€ ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ( ) โˆ’ โŠค x b x RBM (Restricted Boltzman Machine, 1980) RBM์€ ์ž…๋ ฅ์ธต(visible layer) 1๊ฐœ ์€๋‹‰์ธต(hidden layer) 1๊ฐœ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  hidden layer์˜ ๋…ธ๋“œ ๋“ค์€ visible layer์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๊ณ , ๊ทธ ์—ญ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ธ๋ฐ์š”. ๊ฐ™์€ layer ์•ˆ์—์„œ๋Š” ์—ฐ๊ฒฐ์ด ๋˜์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค(์ด๋ฅผ bipartite graph๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค). ์ด๋Ÿฌํ•œ ํŠน์„ฑ ๋•Œ๋ฌธ์— "์ œํ•œ๋œ" ๋ณผ์ธ ๋งŒ ๋จธ์‹ ์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Perceptron์˜ output์€ ๊ฒฐ์ •๋ก ์ ์ธ ๊ฒƒ์— ๋น„ํ•ด RBN์€ ํ™•๋ฅ ์— ๋”ฐ๋ผ์„œ ์ž…๋ ฅ์„ ์€๋‹‰์ธต์— ์ „๋‹ฌํ• ์ง€ ์ „๋‹ฌํ•˜์ง€ ์•Š์„์ง€ ๊ฒฐ์ •ํ•ด ๋ณด๋ƒ…๋‹ˆ๋‹ค. output ์—ญ์‹œ ํ™•๋ฅ  ๊ฐ’์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์„ค๋ช…์€ ์ƒ๋žตํ•˜๊ฒ ์œผ๋‚˜, reference์˜ ๋ธ”๋กœ๊ทธ ๋ฐ Deep Learning(Ian goodfellow)์—์„œ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๊ณ  ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. DBN(Deep belief network, 2006) DBN์€ 2006๋…„์— ์ œ์•ˆ๋˜์–ด ์ตœ์ดˆ๋กœ Deep ํ•œ network ๊ตฌ์กฐ๋ฅผ ํ›ˆ๋ จํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ๋ชจ๋ธ์ด์—ˆ์Šต๋‹ˆ๋‹ค. DBN์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ hidden layer๋ฅผ ๊ฐ€์ง„ ์ƒ์„ฑ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ๋Š” RBM์„ stacking ํ•ด์„œ ๊นŠ๊ฒŒ ์Œ“์•„ ์˜ฌ๋ฆฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ ๋Œ€๋ถ€๋ถ„์ด Deep neural network๋ฅผ ํ•™์Šตํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š”, "gradient descent"์—๋Š” ์ต์ˆ™ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ค์ฐจ๋ฅผ ํ† ๋Œ€๋กœ output layer์—์„œ๋ถ€ํ„ฐ chain rule์„ ์ด์šฉํ•ด input layer๊นŒ์ง€ ๊ต์ •ํ•ด๋‚˜๊ฐ€๋Š” ๋ฐฉ๋ฒ•์ด์ฃ . ์ด ๋ฐฉ๋ฒ•์—๋Š” ์•„์ง๊นŒ์ง€๋„ ์กด์žฌํ•˜๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๋Š”๋ฐ์š”, Gradient vanishing / explosion์ž…๋‹ˆ๋‹ค. ์‹ฌ์ธต ๋ชจ๋ธ์˜ ํ•™์Šต์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ด๊ธฐ๋„ ํ•˜์ฃ . ๋‹น๋Œ€์—๋Š” ์ด ๋ฌธ์ œ ํ•ด๊ฒฐ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋ณด๊ณ  ๋„คํŠธ์›Œํฌ๋ฅผ ๊นŠ๊ฒŒ ์Œ“๊ธฐ๋ณด๋‹ค๋Š” kernel machine ๋“ฑ์„ ์—ฐ๊ตฌํ–ˆ์—ˆ๋Š”๋ฐ์š”, DBN์€ ๊ฑฐ๊พธ๋กœ ์•„๋ž˜์ธต์—์„œ ์œ„๋กœ ์˜ฌ๋ผ๊ฐ€๋ฉด์„œ pre-training์„ ํ•˜๋ฉด์„œ ๋ชจ๋ธ์„ ๊นŠ๊ฒŒ ์Œ“์•„ ์˜ฌ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์„ ์”๋‹ˆ๋‹ค. ์ด๋ฅผ layer-wise pretraining์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์œ„๋กœ ์–ด๋–ป๊ฒŒ ์˜ฌ๋ผ๊ฐˆ๊นŒ์š”? ์—ฌ๊ธฐ์„œ unsupervised learning์˜ ๊ฐœ๋…์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. DBN์—์„œ๋Š” v(vision layer, input)๋งŒ ์•Œ๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ์ด ์ž…๋ ฅ์ด ์–ด๋–ค h (hidden layer, ๋‚˜์•„๊ฐ€์„œ y)์˜ ๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์กŒ์„๊นŒ?๋ฅผ ์ถ”๋ก ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์„ ํ•œ๋ฒˆ ํ’€์–ด์„œ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์˜ˆ๋ฅผ ๋“ค์–ด h์™€ w๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ์ดˆ๊ธฐํ™”ํ–ˆ๋‹ค๊ณ  ํ•ด ๋ด„ ์‹œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋งจ ์ฒ˜์Œ ์ธต์—์„œ ์—‰ํ„ฐ๋ฆฌ h1์™€ w1๊ฐ€ ์žˆ๊ฒ ์ฃ . ์—‰ํ„ฐ๋ฆฌ h๋ผ๋„ ์ด๊ฑธ ํ† ๋Œ€๋กœ x(์ž…๋ ฅ ๋ฐ์ดํ„ฐ)๋ฅผ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ๊ณ  x๋กœ h๋ฅผ ์ถ”๋ก ํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ์š”, ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ํ•„ํ„ฐ w๋ฅผ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค. h1์ด ์ž…๋ ฅ์ด๊ณ  x'์ด ์ถœ๋ ฅ์ผ ๋•Œ x'์ด ์ตœ๋Œ€ํ•œ x์™€ ๊ฐ™์€ ๊ฐ’์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก์š”. ๊ทธ๋ฆฌ๊ณ  w1์„ freeze ์‹œํ‚ค๊ณ  ๊ทธ๋‹ค์Œ ๋‹จ๊ณ„์—์„œ ๋˜‘๊ฐ™์€ ์ž‘์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๊ณผ์ •์„ ์ •๋ฆฌํ•ด ๋ณด์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. pixel ๊ฐ’์„ FC๋กœ ํ’€์–ด์„œ ์ฒซ ๋ฒˆ์งธ ์ธต์„ ํ•™์Šตํ•œ ํ›„(layer-wise pretraining) hidden layer์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•œ๋‹ค. ํ•˜์œ„ hidden layer์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ณด๊ณ  ๋‹ค์Œ layer์— ๋Œ€ํ•ด์„œ ํ•™์Šต์„ ์ง„ํ–‰ (๋ฐ˜๋ณต) ์ดํ›„ wake-sleep algorithm์„ ํ†ตํ•ด ๋ฏธ์„ธ ์กฐ์ • ์•„๋ž˜๋Š” DBN์„ ์ด์šฉํ•ด MNIST ์ด๋ฏธ์ง€ ์ƒ์„ฑํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. Neural Network model Training of DGMs via an EM style framework 1980~ ํ˜„๋Œ€ ํ˜„๋Œ€์˜ ๋ชจ๋ธ๋“ค ์ค‘ ๋ช‡ ๊ฐ€์ง€๋Š” (3)์—์„œ ์ž์„ธํžˆ ๋…ผ์˜๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ์–ด๋– ํ•œ ์•„์ด๋””์–ด์— ๊ธฐ์ดˆํ•ด ๋‚˜์˜จ ๊ฒƒ์ธ์ง€ ์‹œ๋Œ€์ ์ธ ํ๋ฆ„๋งŒ ํŒŒ์•…ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Deep Boltzmann Machines (DBM, 2009) Variational Autoencoder (VAE, 2014) Generative Adversarial Network (GAN, 2014) Reference CMU, 10-708 PGM lecture note : https://sailinglab.github.io/pgm-spring-2019/notes/lecture-17/ RBM : https://idplab-konkuk.tistory.com/14 DBN : https://bi.snu.ac.kr/Courses/ML2016/LectureNote/LectureNote_ch5.pdf Deep Learning (Ian Goodfellow, 2016) 2) Variational Inference & Wake sleep algorithm(โ˜…์ž‘์„ฑ ์ค‘) Inference model๊ณผ Generative model Inference model Posterior probability Variational approximation to the posterior Recognition model์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๊ธฐ๋„ ํ•จ. (Neural network๋กœ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด) Inference network, Recognition network๋กœ ๋ถˆ๋ฆผ. (Probablistic) Encoder Generative model prior + conditional / joint probability Likelihood model (Neural network๋กœ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด) Generative network๋กœ ๋ถˆ๋ฆผ. ๋ณดํ†ต Generator๋กœ์„œ ์ž‘์šฉ (Probablistic) Decoder Variational Inference Variational Inference๋Š” ์‚ฌํ›„ ํ™•๋ฅ  (Posterior probability)๋ฅผ approximation ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. EM algorithm Wake sleep algorithm Reference https://hyeongminlee.github.io/post/bnn003_vi/ https://sailinglab.github.io/pgm-spring-2019/notes/lecture-17/ ์œ ํŠœ๋ธŒ : Variational Inference (AAILab Kaist) 10-708 (CMU, Probablistic Graphical Modeling) video lectue : https://sailinglab.github.io/pgm-spring-2019/lectures/ (3) ์ƒ์„ฑ ๋ชจ๋ธ๋“ค 1) VAE(Variational Auto-Encoder) VAE๋ž€? VAE๋Š” Input image X๋ฅผ ์ž˜ ์„ค๋ช…ํ•˜๋Š” feature๋ฅผ ์ถ”์ถœํ•˜์—ฌ Latent vector z์— ๋‹ด๊ณ , ์ด Latent vector z๋ฅผ ํ†ตํ•ด X์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฐ feature๊ฐ€ ๊ฐ€์šฐ์‹œ์•ˆ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  latent z๋Š” ๊ฐ feature์˜ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•œ๊ตญ์ธ์˜ ์–ผ๊ตด์„ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•ด ๋ˆˆ, ์ฝ”, ์ž… ๋“ฑ์˜ feature๋ฅผ Latent vector z์— ๋‹ด๊ณ , ๊ทธ z๋ฅผ ์ด์šฉํ•ด ๊ทธ๋Ÿด๋“ฏํ•œ ํ•œ๊ตญ์ธ์˜ ์–ผ๊ตด์„ ๊ทธ๋ ค๋‚ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. latent vector z๋Š” ํ•œ๊ตญ์ธ ๋ˆˆ ๋ชจ์–‘์˜ ํ‰๊ท  ๋ฐ ๋ถ„์‚ฐ, ํ•œ๊ตญ์ธ ์ฝ” ๊ธธ์ด์˜ ํ‰๊ท  ๋ฐ ๋ถ„์‚ฐ, ํ•œ๊ตญ์ธ ๋จธ๋ฆฌ์นด๋ฝ ๊ธธ์ด์˜ ํ‰๊ท  ๋ฐ ๋ถ„์‚ฐ ๋“ฑ๋“ฑ์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. โ†’ p โˆ— ( ) ฮธ ( | ( ) ) ์ˆ˜์‹์„ ์•ฝ๊ฐ„ ๊ณ๋“ค์—ฌ ์ด๋ฅผ ํ‘œํ˜„ํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. p(z): latent vector z์˜ ํ™•๋ฅ ๋ฐ€๋„ํ•จ์ˆ˜. ๊ฐ€์šฐ์‹œ์•ˆ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ๊ฐ€์ • p(x|z): ์ฃผ์–ด์ง„ z์—์„œ ํŠน์ • x๊ฐ€ ๋‚˜์˜ฌ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์— ๋Œ€ํ•œ ํ™•๋ฅ ๋ฐ€๋„ํ•จ์ˆ˜ ฮธ: ๋ชจ๋ธ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ VAE์˜ ๊ตฌ์กฐ Input image X๋ฅผ Encoder์— ํ†ต๊ณผ์‹œ์ผœ Latent vector z๋ฅผ ๊ตฌํ•˜๊ณ , Latent vector z๋ฅผ ๋‹ค์‹œ Decoder์— ํ†ต๊ณผ์‹œ์ผœ ๊ธฐ์กด input image X์™€ ๋น„์Šทํ•˜์ง€๋งŒ ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€ X๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. VAE๋Š” input image๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๊ทธ ์ด๋ฏธ์ง€์—์„œ์˜ ๋‹ค์–‘ํ•œ ํŠน์ง•๋“ค์ด ๊ฐ๊ฐ์˜ ํ™•๋ฅ  ๋ณ€์ˆ˜๊ฐ€ ๋˜๋Š” ์–ด๋–ค ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ์ž˜ ์ฐพ์•„๋‚ด๊ณ , ํ™•๋ฅ  ๊ฐ’์ด ๋†’์€ ๋ถ€๋ถ„์„ ์ด์šฉํ•˜๋ฉด ์‹ค์ œ์— ์žˆ์„๋ฒ•ํ•œ ์ด๋ฏธ์ง€๋ฅผ ์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. VAE์˜ ์ˆ˜์‹์  ์ฆ๋ช… ๋ชจ๋ธ์˜ ํŒŒ๋ผ๋ฏธํ„ฐฮธ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์ •๋‹ต์ธ x๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์ด p ฮธ(x) ๋†’์„์ˆ˜๋ก ์ข‹์€ ๋ชจ๋ธ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰ p ฮธ(X)๋ฅผ ์ตœ๋Œ€ํ™”ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ VAE์˜ ํŒŒ๋ผ๋ฏธํ„ฐฮธ๋ฅผ ํ•™์Šต์‹œํ‚ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. VAE๋Š” Intractable Density๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์—ฌ๋Ÿฌ ์ˆ˜์‹์„ ์ „๊ฐœํ•˜์—ฌ Lower Bound ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. VAE ์žฅ๋‹จ์  ์žฅ์  : ํ™•๋ฅ  ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ž ์žฌ ์ฝ”๋“œ๋ฅผ ๋” ์œ ์—ฐํ•˜๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ์  : Density๋ฅผ ์ง์ ‘์ ์œผ๋กœ ๊ตฌํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— Pixel RNN/CNN ๊ณผ๊ฐ™์ด ์ง์ ‘์ ์œผ๋กœ Density๋ฅผ ๊ตฌํ•œ ๋ชจ๋ธ๋ณด๋‹ค๋Š” ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง„๋‹ค. AE vs. VAE VAE(Variational AutoEncoder)๋Š” ๊ธฐ์กด์˜ AutoEncoder์™€ ํƒ„์ƒ ๋ฐฐ๊ฒฝ์ด ๋‹ค๋ฅด์ง€๋งŒ ๊ตฌ์กฐ๊ฐ€ ์ƒ๋‹นํžˆ ๋น„์Šทํ•ด์„œ Variational AE๋ผ๋Š” ์ด๋ฆ„์ด ๋ถ™์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, VAE์™€ AE๋Š” ์—„์—ฐํžˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. AutoEncoder์˜ ๋ชฉ์ ์€ Encoder์— ์žˆ์Šต๋‹ˆ๋‹ค. AE๋Š” Encoder ํ•™์Šต์„ ์œ„ํ•ด Decoder๋ฅผ ๋ถ™์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ VAE์˜ ๋ชฉ์ ์€ Decoder์— ์žˆ์Šต๋‹ˆ๋‹ค. Decoder ํ•™์Šต์„ ์œ„ํ•ด Encoder๋ฅผ ๋ถ™์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. VAE๋Š” ๋‹จ์ˆœํžˆ ์ž…๋ ฅ๊ฐ’์„ ์žฌ๊ตฌ์„ฑํ•˜๋Š” AE์—์„œ ๋ฐœ์ „ํ•œ ๊ตฌ์กฐ๋กœ ์ถ”์ถœ๋œ ์ž ์žฌ ์ฝ”๋“œ์˜ ๊ฐ’์„ ํ•˜๋‚˜์˜ ์ˆซ์ž๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๊ฐ€์šฐ์‹œ์•ˆ ํ™•๋ฅ  ๋ถ„ํฌ์— ๊ธฐ๋ฐ˜ํ•œ ํ™•๋ฅ  ๊ฐ’์œผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. AE : ์ž ์žฌ ์ฝ”๋“œ ๊ฐ’์ด ์–ด๋–ค ํ•˜๋‚˜์˜ ๊ฐ’ VAE : ์ž ์žฌ ์ฝ”๋“œ ๊ฐ’์ด ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ์–ด๋–ค ๊ฐ€์šฐ์‹œ์•ˆ ๋ถ„ํฌ ์•„๋ž˜ ๊ทธ๋ฆผ์€ MNist ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ๊ฐ AE์™€ VAE๋กœ ํŠน์ง•์„ ์ถ”์ถœํ•ด ํ‘œํ˜„ํ•œ ๊ทธ๋ฆผ์ด๋‹ค. ๊ฐ ์ ์˜ ์ƒ‰๊น”์€ MNIST ๋ฐ์ดํ„ฐ์ธ 0~9์˜ ์ˆซ์ž๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. AE๊ฐ€ ๋งŒ๋“ค์–ด๋‚ธ ์ž ์žฌ ๊ณต๊ฐ„์€ ๊ตฐ์ง‘์ด ๋น„๊ต์  ๋„“๊ฒŒ ํผ์ ธ์žˆ๊ณ , ์ค‘์‹ฌ์œผ๋กœ ์ž˜ ๋ญ‰์ณ์žˆ์ง€ ์•Š์ง€๋งŒ, VAE๊ฐ€ ๋งŒ๋“ค์–ด๋‚ธ ์ž ์žฌ ๊ณต๊ฐ„์€ ์ค‘์‹ฌ์œผ๋กœ ๋” ์ž˜ ๋ญ‰์ณ์ ธ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ์žฌ์ƒํ•˜๋Š”๋ฐ AE์— ๋น„ํ•ด์„œ VAE๊ฐ€ ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์ธ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ VAE๋ฅผ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ์˜ ํŠน์ง•์„ ํŒŒ์•…ํ•˜๋Š” ๊ฒŒ ๋” ์œ ๋ฆฌํ•˜๋‹ค. Reference Idea Factor KAIST | AutoEncoder and Variational AutoEncoder - ๋”ฅ๋Ÿฌ๋‹ ํ™€๋กœ์„œ๊ธฐ Auto-Encoding Variational Bayes ๋…ผ๋ฌธ ์ •๋ฆฌ NAVER D2 | ์ดํ™œ์„ - ์˜คํ†  ์ธ์ฝ”๋”์˜ ๋ชจ๋“  ๊ฒƒ ๊น€ํƒœ์œ 's blog | VAE(Auto-Encoding Variational Bayes) ์ง๊ด€์  ์ดํ•ด 2018, DEC 20 VAE (Variational Auto Encoder)๋ฅผ ์‚ฌ์šฉํ•œ ์ƒ์„ฑ ๋ชจ๋ธ๋ง 2) Generative Adversarial Networks (GANs) Background GAN์€ 2014๋…„, Ian Goodfellow์˜ "Generative Adversarial Network"๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์ฒ˜์Œ ์ œ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. CNN์˜ ์ฐฝ์‹œ์ž์ธ Lecun์€ ์ง€๋‚œ 20๋…„ ๋™์•ˆ ๋‚˜์˜จ ์•„์ด๋””์–ด ์ค‘์— ๊ฐ€์žฅ ๋ฉ‹์žˆ๋Š” ์•„์ด๋””์–ด๋ผ๊ณ  ์นญ์†กํ•˜๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค. GAN์€ ๊ตฌ์กฐ ์ž์ฒด์˜ ์ดํ•ด๊ฐ€ ์–ด๋ ต์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ๋งŽ์€ ์—ฐ๊ตฌ์ž๋“ค์— ์˜ํ•ด ์—ฐ๊ตฌ๊ฐ€ ๋˜์–ด์™”์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ด๋ฏธ์ง€๋Š” GAN์ด ์ œ์‹œ๋œ 2014๋…„๋ถ€ํ„ฐ 2020๋…„๊นŒ์ง€ GAN์„ ์ฃผ์ œ๋กœ ํ•œ ๋…ผ๋ฌธ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ฐจํŠธ๋กœ, ์ƒ๋‹นํžˆ ๋น ๋ฅธ ์†๋„๋กœ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ์—ฐ๊ตฌ๊ฐ€ ๋˜์–ด์˜ค๊ณ  ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŽ์€ ๋„๋ฉ”์ธ์—์„œ, ๋งŽ์€ ์—ฐ๊ตฌ์ž๋“ค์ด ๋›ฐ์–ด๋“ค๊ณ  ์žˆ๋Š” ๋งŒํผ ์งง์€ ์‹œ๊ฐ„ ๋™์•ˆ ์ƒ๋‹นํ•œ ๋ฐœ์ „์„ ๋ณด์˜€์Šต๋‹ˆ๋‹ค. 2014๋…„์— ๋‚˜์˜จ vanilla GAN๊ณผ ์ตœ๊ทผ ๋‚˜์˜จ StyleGAN์˜ ์•„์›ƒํ’‹์„ ๋น„๊ตํ•ด ๋ณด๋ฉด 4๋…„์ด๋ผ๋Š” ์งง์€ ๊ธฐ๊ฐ„ ๋™์•ˆ ํฌ๊ฒŒ ๋ฐœ์ „ํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. How GAN works? Intuition ์œ„ ๊ทธ๋ฆผ์€ GAN์„ ์„ค๋ช…ํ•  ๋•Œ ๊ฐ€์žฅ ๋งŽ์ด ๋‚˜์˜ค๋Š” ๊ทธ๋ฆผ์ธ๋ฐ์š”, ์ด๋Š” ์ฒ˜์Œ GAN์„ ์ œ์‹œํ•œ Ian goodfellow ๊ฐ€ GAN์„ ์œ„์กฐ์ง€ํ๋ฒ”๊ณผ ๊ฒฝ์ฐฐ์— ๋น—๋Œ€์–ด ์„ค๋ช…ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์œ„์กฐ์ง€ํ๋ฒ”์€ ์ตœ๋Œ€ํ•œ ์ง„์งœ ๊ฐ™์€ ์ง€ํ๋ฅผ ๋งŒ๋“ค์–ด ๊ฒฝ์ฐฐ์„ ์†์ด๊ณ , ๊ฒฝ์ฐฐ์€ ์œ„์กฐ์ง€ํ๋ฒ”์ด ๋งŒ๋“ค์–ด๋‚ธ ์ง€ํ์™€ ์ง„์งœ ์ง€ํ๋ฅผ ๋Œ€์กฐํ•˜๋ฉด์„œ ๋‘˜์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ์ฐจ์ด์ ์„ ๊ณ„์†ํ•ด์„œ ์ฐพ์•„๋‚ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ์œ„์กฐ์ง€ํ๋ฒ”์€ ์ ์  ๋” ์ •๊ตํ•œ ์ง€ํ๋ฅผ ๋งŒ๋“ค์–ด ๊ฒฝ์ฐฐ์„ ์†์ด๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜๊ณ  ๊ฒฝ์ฐฐ์€ ์™„๋ฒฝํžˆ ํŒ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ๋” ๋…ธ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์„œ๋กœ ๊ฒฝ์Ÿ์ ์ธ ํ•™์Šต์ด ๊ณ„์†๋˜๋‹ค ๋ณด๋ฉด, ์–ด๋Š ์ˆœ๊ฐ„ ๊ฒฝ์ฐฐ์ด ์ง„์งœ ์ง€ํ์™€ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์—†์„ ์ •๋„๋กœ ๋น„์Šทํ•œ ์ง€ํ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. GAN์˜ ๊ธฐ๋ณธ ์ฒ ํ•™์„ ์ดํ•ดํ•ด ๋ณด์ž๋ฉด, ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋Š”, "๋ณต์žกํ•œ ๊ณ ์ฐจ์›์˜ training distribution์—์„œ sampling์„ ํ•˜๊ณ  ์‹ถ๋‹ค." ๋ผ๋ฉด ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด, simple distribution(e.g. random noise)๋ฅผ ์ƒ˜ํ”Œ๋ง ํ•ด์„œ, Training distribution ์„ ๋”ฐ๋ฅด๋Š” Transformation์„ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ•™์Šตํ•˜์ž!๋Š” ์ „๋žต์„ ์ทจํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. likelihood - free model Generator and discriminator GAN์—๋Š” ์œ„์กฐ์ง€ํ๋ฒ”์— ํ•ด๋‹นํ•˜๋Š” Generator(G) ์™€ ๊ฒฝ์ฐฐ์— ํ•ด๋‹นํ•˜๋Š” Discriminator(D) ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. Generator๋Š” real data์˜ distribution์„ ํ•™์Šตํ•ด fake ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ์ผ์„ ํ•ฉ๋‹ˆ๋‹ค. โ†’ ์ตœ์ข…์ ์œผ๋กœ ์ด๋ฅผ Discriminator๊ฐ€ ์ตœ๋Œ€ํ•œ ํ—ท๊ฐˆ๋ฆฌ๊ฒŒ ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. Discriminator๋Š” smaple์ด realdata(training)์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. โ†’ ์ตœ์ข…์ ์œผ๋กœ Fake ์ด๋ฏธ์ง€๋ฅผ ์ตœ๋Œ€ํ•œ ์ž˜ ํŒ๋ณ„ํ•˜๋Š” ๊ฒƒ ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. Adversarial learning ์ง€๊ธˆ๋ถ€ํ„ฐ training data์˜ distribution์ธ d t ์™€ Generator๊ฐ€ ๋งŒ๋“ค์–ด๋‚ธ ์ด๋ฏธ์ง€์˜ ๋ถ„ํฌ์ธ g ( ( ) ) ์‚ฌ์ด๋ฅผ discriminator๊ฐ€ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ํ™•๋ฅ ์„๋ผ๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์‚ฌ์ง„์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ, ์ฒ˜์Œ์—๋Š” discriminator๊ฐ€ fake ์ด๋ฏธ์ง€์™€ ์‹ค์ œ ์ด๋ฏธ์ง€๋ฅผ ๊ตฌ๋ถ„ํ•ด ๋‚ด๊ธฐ๊ฐ€ ๋งค์šฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ์ด ์ƒํ™ฉ์—์„œ๋Š” ๊ฐ€ ๋งค์šฐ ๋‚ฎ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ดํ›„, discriminator๋ฅผ ๊ณ ์ • ํ›„ generaotr๋ฅผ ์ข€ ๋” ์ž˜ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ์—…๋ฐ์ดํŠธํ•œ๋‹ค๋ฉด, ์ ์  discriminator๊ฐ€ ๊ตฌ๋ถ„ํ•˜๊ธฐ ํž˜๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด discriminator๋Š” ๋” ์ž˜ ๊ตฌ๋ถ„ํ•ด๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ๋‹ค์‹œ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์„ ๊ณ„์† ๋ฐ˜๋ณตํ•˜๋‹ค ๋ณด๋ฉด, ๊ฒฐ๊ตญ discriminator๊ฐ€ ์ ์  ๋” ๋งŽ์ด ํ‹€๋ฆฌ๊ฒŒ ๋˜๊ณ  = /์— ์ˆ˜๋ ดํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. discriminator๊ฐ€ ์™„์ „ํžˆ ๊ตฌ๋ถ„ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋งจ ๋งˆ์ง€๋ง‰ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๋ฉด, g p a a ์ฆ‰ ์™„์ „ํžˆ ๋˜‘๊ฐ™์€ ๋ถ„ํฌ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. GAN์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ฅผ ์š”์•ฝํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (initial๋กœ ๊ณ ์ •๋œ) G๋กœ ์ƒ์„ฑ โ†’ D๋กœ classify, ์—…๋ฐ์ดํŠธ โ†’ (D๋ฅผ constant๋กœ ๋งŒ๋“ค๊ณ ) G ์—…๋ฐ์ดํŠธ โ†’ (์—…๋ฐ์ดํŠธ๋œ ์ƒํƒœ๋กœ ๊ณ ์ •๋œ ) G๋กœ ์ƒ์„ฑ โ†’ D๋กœ classify โ†’ (D๋ฅผ constant๋กœ ๋งŒ๋“ค๊ณ ) G ์—…๋ฐ์ดํŠธ ... (๋ฐ˜๋ณต) ... โ†’ ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋‹ค๊ฐ€ D(x) = 1/2, ์ฆ‰ discriminator๊ฐ€ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ๊ฐ€ ๋จ. g p a a Objective function of GAN min max V ( ฮธ D) E โˆผ data [ log D ( ) ] E โˆผ ( ) [ log ( GAN์˜ objective function์ž…๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ ๋ณด๋ฉด ๊ต‰์žฅํžˆ ๋ณต์žกํ•  ๊ฒƒ ๊ฐ™์ง€๋งŒ, ์‚ฌ์‹ค ๋œฏ์–ด์„œ ๋ณด๋ฉด ์ •๋ง ๊ฐ„๋‹จ ๋ช…๋ฃŒํ•œ ๋ฃฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด๊ฑธ GAN์˜ ๊ตฌ์กฐ์— ๋งž๊ฒŒ ํ•˜๋‚˜์”ฉ ๋œฏ์–ด์„œ ์ƒ๊ฐ์„ ํ•ด๋ด…์‹œ๋‹ค. Discriminator์˜ Objective function discriminator๋Š” ๋งŒ๋“ค์–ด๋‚ธ ๋ฐ์ดํ„ฐ x๊ฐ€ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ผ๋ฉด 1์„ ๋ฆฌํ„ดํ•˜๊ณ , fake image๋ผ๋ฉด 0์„ ๋ฆฌํ„ดํ•˜๋Š” classifier๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ( ) ๊ฐ€ disriminator์˜ ๋ฆฌํ„ด ๊ฐ’์ด๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ discriminator์˜ objective function์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ์ด๋ฏธ์ง€๊ฐ€ input ์ผ ๋•Œ ์‹ค์ œ ์ด๋ฏธ์ง€๋ฅผ ์ž˜ ์˜ˆ์ธกํ–ˆ๋‹ค๋ฉด, D(x) = 1์ด๋ฏ€๋กœ, log(D(x)) = 0์ด ๋˜๊ณ  ๋งŒ์•ฝ์— fake ์ด๋ฏธ์ง€๋กœ ์˜ˆ์ธกํ–ˆ๋‹ค๋ฉด, D(x) = 0์ด๋ฏ€๋กœ, log(D(x)) = - \infinity ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ์— fake ์ด๋ฏธ์ง€๊ฐ€ input ์ผ ๋•Œ ์‹ค์ œ ์ด๋ฏธ์ง€๋กœ ์˜ˆ์ธกํ–ˆ๋‹ค๋ฉด D(x) = 1์ด๋ฏ€๋กœ, log(1-D(x) ) = - \infinity ๊ฐ€ ๋˜๊ณ , ์ œ๋Œ€๋กœ ์˜ˆ์ธกํ–ˆ๋‹ค๋ฉด log(1-D(x)) = 0์ด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, Discriminator์˜ objective function์€ ์ตœ๋Œ“๊ฐ’์ด 0, ์ตœ์†Ÿ๊ฐ’์€ - \infinity ์ด๋ฉฐ, ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ ํ•ญ์ƒ ์ œ๋Œ€๋กœ ์˜ˆ์ธกํ•˜๋Š” discriminator๊ฐ€ ๋˜๋Š” ๊ฒƒ, ์ฆ‰ ํ•ญ์ƒ ์ตœ๋Œ“๊ฐ’์ธ 0์˜ ์„ฑ๋Šฅ์„ ๋ณด์ด๊ธฐ๋ฅผ ์›ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ง€๊ธˆ๊นŒ์ง€์™€๋Š” ๋‹ค๋ฅด๊ฒŒ gradient ascending, ์ฆ‰ ์ตœ๋Œ€ ๊ธฐ์šธ๊ธฐ๋ฅผ ํ–ฅํ•ด ํ•™์Šต์ด ๋˜์–ด์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. Generator์˜ objective function ๊ทธ๋ ‡๋‹ค๋ฉด generator์˜ objective function์€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฒจ์•ผ ํ• ๊นŒ์š”? generator๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ, discriminator๊ฐ€ ์‹ค์ œ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ํŒ๋ณ„ํ–ˆ๋Š”์ง€๊ฐ€ ์ค‘์š”ํ•œ ์ •๋ณด์ผ๊นŒ์š”? ์•„๋‹™๋‹ˆ๋‹ค. generator๋Š” ์ž๊ธฐ๊ฐ€ ๋งŒ๋“  fake ์ด๋ฏธ์ง€๊ฐ€ ์–ผ๋งˆ๋‚˜ discriminator๋ฅผ ํ—ท๊ฐˆ๋ฆฌ๊ฒŒ ํ–ˆ๋Š”์ง€ ์ด๊ฒƒ๋งŒ์ด ์ค‘์š”ํ•œ ์ •๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ discriminator objective function์˜ ๋’ท๋ถ€๋ถ„, fake ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋งŒ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. discriminator์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ตœ์†Œ - \infinity , ์ตœ๋Œ€ 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ๋˜์ง€๋งŒ, ์ด๋•Œ๋Š” ๋ฐ˜๋Œ€๋กœ discriminator๋ฅผ ์†์ด๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ด๊ธฐ ๋•Œ๋ฌธ์— gradient descent๋ฅผ ์‹œํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ generator๊ฐ€ ๋งŒ๋“ค์–ด๋‚ธ fake ์ด๋ฏธ์ง€๋ฅผ ์‹ค์ œ ์ด๋ฏธ์ง€๋ผ๊ณ  ๋ถ„๋ฅ˜ํ–ˆ์„ ๋•Œ, ๊ทธ ๋ฐฉํ–ฅ์œผ๋กœ gradient update๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. GAN์˜ Objective function ์ด์ œ ๊ฐ์ด ์˜ค์‹œ๋‚˜์š”? GAN์˜ objective function์€ discriminator์˜ objective function๊ณผ generator์˜ objective function์„ ํ•ฉ์นœ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ตœ์†Œํ™”๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋Š” generator ( ) ์™€ ์ตœ๋Œ€ํ™”๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋Š” discriminator ( )์˜ objective function์œผ๋กœ ๋Œ€์‘ํ•ด ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์™„์ „ํžˆ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. min max V ( ฮธ D) E โˆผ data [ log D ( ) ] E โˆผ ( ) [ log ( Optimizing GAN g p a a ๊ฐ€ ์ •๋ง global optimum ์ธ๊ฐ€? GAN์˜ ๊ธฐ๋ณธ ๊ฐ€์ •์€ g p a a ์ผ ๋•Œ๋ฅผ optimum์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋Š” ๊ฒƒ์ธ๋ฐ์š”, ๊ณผ์—ฐ ์ด๊ฒƒ์ด ํ•ฉ๋‹นํ•œ ๊ฐ€์ •์ผ๊นŒ์š”? ์ฒซ ๋ฒˆ ๋•Œ๋กœ, ์ง€๊ธˆ๋ถ€ํ„ฐ ์–ด๋–ค G์— ๋Œ€ํ•ด์„œ๋“  optimal ํ•œ discriminator๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ์ด๋•Œ (๊ณ ์ •๋œ G์— ๋Œ€ํ•œ) D์˜ ์„ฑ๋Šฅ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. optimal ํ•˜๋‹ค๋ฉด 0.5๋กœ ์ˆ˜๋ ดํ•˜๊ฒ ์ฃ . G = d t ( ) d t ( ) p ( ) ์ด optimal ํ•œ D์— ๋Œ€ํ•œ objective function์€ ์•„๋ž˜์™€ ๊ฐ™์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. max V ( , ) E โˆผ d t [ o D ( ) ] E โˆผ G [ o ( โˆ’ ( ) ] ์œ„์˜ ์‹์—์„œ ๊ธฐ๋Œ“๊ฐ’์„ x์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ด์–ด ํ‘œ์‹œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ( , ) โˆซ p a a ( ) [ o D ( ) ] p ( ) [ o ( โˆ’ ( ) ] ์–ด๋–ค ( , ) R์— ๋Œ€ํ•ด์„œ๋“  โ†’ l g ( ) b o ( โˆ’ ) [ , ] dptj a b ์— maximum์œผ๋กœ ๋„๋‹ฌํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์ด ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฑธ ์œ„์˜ ์‹์— ๋Œ€์ž…ํ•ด์„œ ์ƒ๊ฐํ•ด ๋ณด๋ฉด d t ๋ถ€๋ถ„์ด a, g ๋ถ€๋ถ„์ด b๊ฐ€ ๋˜์–ด, optimal maximum์— ๋„๋‹ฌํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ฆ๋ช…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Jenson-Shannon Divergence (JSD) ์œ„์—์„œ ๊ตฌํ•œ optimal D, G = d t ( ) d t ( ) p ( ) ๋ฅผ objective function์— ์ง์ ‘ ๋„ฃ์–ด๋ด…์‹œ๋‹ค. x p a a [ o ( d t ( ) d t ( ) p ( ) ) ] E โˆผ G [ o ( โˆ’ ( d t ( ) d t ( ) p ( ) ) ] E โˆผ d t [ o ( d t ( ) d t ( ) p ( ) ) ] E โˆผ G [ o ( ( G ( ) d t ( ) p ( ) ) ] E โˆผ d t [ o ( d t ( ) d t ( ) p ( ) ) ] E โˆผ G [ o ( ( G ( ) d t ( ) p ( ) ) ] l g ์ด๋ ‡๊ฒŒ ์ •์˜๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”, ์–‘์˜†์˜ ๊ตฌ์กฐ๊ฐ€ ์ƒ๋‹นํžˆ ๋น„์Šทํ•˜์ฃ . ์ด ๋ถ€๋ถ„์„ KL-divergence๋ผ๊ณ  ํ•ด์„œ K๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์œ„์˜ ์‹์€, ์•„๋ž˜์™€ ๊ฐ™์ด ์ •๋ฆฌ๋ฉ๋‹ˆ๋‹ค. D L [ d t, d t + G ] D L [ G p a a p 2 ] l g ์—ฌ๊ธฐ์„œ K๋กœ ์ •์˜๋œ ๋ถ€๋ถ„์„ ํ•ฉ์ณ์„œ 2 * Jenson- Shannon Divergence๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2 J D [ d t, G ] l g Jenson-shannon divergence๋Š” symmetric KL divergence๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š”๋ฐ์š”. ์‹์„ ํ† ๋Œ€๋กœ ๋‹ค์Œ์˜ 4๊ฐ€์ง€ ์„ฑ์งˆ์„ ์œ ๋„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. * 1) J D ( , ) >= 2) J D ( , ) 0 i f = 3) J D ( , ) >= J D ( , ) * 4) J D ( , ) (Jenson-Shannon Distance)๋Š” triangle inequality๋ฅผ ๋งŒ์กฑํ•ฉ๋‹ˆ๋‹ค. Optimizing GAN Code implementation ์ฝ”๋“œ ๊ตฌํ˜„์„ ๋”ฐ๋กœ ๋ฌธ์„œ๋กœ ๋ถ„๋ฆฌํ•˜์ง€ ์•Š๊ณ , ์—ฌ๊ธฐ์„œ ๊ฐ™์ด ๋ณด์—ฌ์ฃผ๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Œ. (๊ฐ€๋Šฅํ•œ ๊ฒƒ๋งŒ) Limitation 1) Training instability ์ด๋ก ๋งŒ ๋ณด๋ฉด GAN์€ ์ƒ๋‹นํžˆ ์ด์ƒ์ ์ธ ๋ชจ๋ธ์ผ ๊ฒƒ ๊ฐ™์ง€๋งŒ, GAN์€ ์‰ฝ๊ฒŒ training์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ GAN์œผ๋กœ training ํ–ˆ์„ ๋•Œ epoch์— ๋”ฐ๋ฅธ loss๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. GAN์€ ์ฒ˜์Œ์—๋Š” training์ด ์ž˜ ๋˜์ง€๋งŒ, epoch์ด ๊ธธ์–ด์งˆ์ˆ˜๋ก ์—„์ฒญ๋‚˜๊ฒŒ oscilation์ด ์ผ์–ด๋‚˜๊ณ , loss๊ฐ€ ๋–จ์–ด์ง€์ง€ ์•Š์•„ ์•ˆ์ •ํ™”๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์™œ ์ด๋Ÿฐ ํ˜„์ƒ์ด ๋ฐœ์ƒํ• ๊นŒ์š”? ์ด๋Š” GAN์ด ๊ธฐ์ดˆ๋กœ ํ•˜๋Š” ๊ฐ€์ • ์ž์ฒด๊ฐ€ ๋น„ํ˜„์‹ค์ ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. GAN์€ generator์™€ discriminator๊ฐ€ ์„œ๋กœ๊ฐ€ ์„œ๋กœ๋ฅผ ์†์ด๋Š” ๊ณผ์ •์—์„œ generator๊ฐ€ data distribution์— ๊ทผ์‚ฌํ•˜๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉด์„œ ์—…๋ฐ์ดํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ํŠน์„ฑ์ƒ generator๊ฐ€ ์ข‹์•„์ง€๋ฉด discriminator๋„ ์ข‹์•„์ง€๊ณ , discriminator๊ฐ€ ์ข‹์•„์ง€๋ฉด generator๋„ ์ข‹์•„์ง€๊ณ , ์„œ๋กœ๊ฐ€ ์„œ๋กœ์˜ ๋ถ„ํฌ์— ๊ทผ์‚ฌํ•ด๊ฐ€๋ฉด์„œ ๋๋‚˜์ง€ ์•Š๋Š” ์ˆจ๋ฐ”๊ผญ์งˆ์„ ๋ฌดํ•œํžˆ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด discriminator, generator ๋ชจ๋‘ ์„œ๋กœ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ์–ด๊ฐ€๋ฉฐ ์ซ“์•„๋‹ค๋‹ˆ๊ฒŒ ๋˜๊ณ , global optimum์— ์ˆ˜๋ ดํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํ˜„์ƒ์„ oscilation์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2) Mode collapse " ๋‚˜๋Š” ํ•œ ๋†ˆ๋งŒ ํŒฌ๋‹ค " Model collapse๋ฅผ ๊ฐ€์žฅ ์ž˜ ๋‚˜ํƒ€๋‚ด๋Š” ๋ง์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ€์ง€ training์„ ๋ณด์—ฌ์ค˜๋„, ์šฐ์—ฐ์— ์˜ํ•ด ๋จผ์ € ๋งŒ๋“ค์–ด์ง„ ๋ช‡ ๊ฐ€์ง€ ์ด๋ฏธ์ง€์™€ ์ง€๋‚˜์น˜๊ฒŒ ๋น„์Šทํ•˜๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. GAN์„ training ํ•  ๋•Œ ๋ณด์—ฌ์ฃผ๋Š” ๋งŽ์€ ์ˆ˜์˜ training set ์ค‘์—์„œ ์šฐ์—ฐํžˆ ๋”ฑ ํ•˜๋‚˜์˜ training image ์™€ ๋น„์Šทํ•œ image๋ฅผ generation ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด, ๊ทธ ์ด๋ฏธ์ง€์™€ ๋น„์Šทํ•œ ๊ฒฐ๊ณผ๋ฌผ์„ ๋ƒˆ์œผ๋‹ˆ ๊ทธ ์ด๋ฏธ์ง€์™€ ์ตœ๋Œ€ํ•œ ๋น„์Šทํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์ชฝ์œผ๋กœ gradient update๊ฐ€ ์ผ์–ด๋‚˜๊ฒŒ ๋˜๊ณ , ์ƒ๋‹นํžˆ ๋งŽ์€ ์ˆ˜์˜ training set์„ ๋ณด์—ฌ์คฌ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋ช‡ ๊ฐ€์ง€ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด์„œ๋งŒ ๋น„์Šทํ•œ ๊ฒฐ๊ณผ๋ฌผ์„ ์ƒ์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ๋Š” MNIST data set์„ ๊ฐ€์ง€๊ณ  ํ›ˆ๋ จ๋œ ๊ฒฐ๊ณผ๋ฌผ์ธ๋ฐ์š”, ์ฒซ ๋ฒˆ์งธ ์ค„์€ model collapse ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ unrolledGAN์˜ ๊ฒฐ๊ณผ๋ฌผ์ด๊ณ , ๋‘ ๋ฒˆ์งธ ์ค„์€ vanilla GAN์˜ ๊ฒฐ๊ณผ๋ฌผ์ž…๋‹ˆ๋‹ค. ์ƒ์„ฑํ•œ ์ƒ˜ํ”Œ์ด ๋‹ค์–‘ํ•˜๊ฒŒ ๋˜์ง€ ๋ชปํ•˜๊ณ  ํ•œ๋‘ ๊ฐ€์ง€ ์ƒ˜ํ”Œ๊ณผ ๋น„์Šทํ•œ ๊ฒฐ๊ณผ๋ฌผ๋งŒ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Further study goodfellow์— ์˜ํ•ด GAN์ด๋ผ๋Š” architecture๊ฐ€ ์ œ์‹œ๋œ ์ดํ›„, GAN์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ถ„์•ผ๋กœ ์‘์šฉ๋˜๊ธฐ๋„ ํ–ˆ๊ณ  ์•ž์„œ ๋งํ•œ ๋ฌธ์ œ์ ๋“ค์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์‹œ๋„๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ด๋ฏธ์ง€๋ฅผ ํ†ตํ•ด GAN์˜ ํ›„์† ์—ฐ๊ตฌ๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Reference ๋ฐ ์ถ”์ฒœ ์ž๋ฃŒ https://ysbsb.github.io/gan/2020/06/17/GAN-newbie-guide.html [how tot rain GAN?] https://github.com/soumith/ganhacks https://towardsdatascience.com/understanding-and-optimizing-gans-going-back-to-first-principles-e5df8835ae18 cs236 lecture note : [GAN] https://deepgenerativemodels.github.io/notes/GAN/ 3) DCGAN(Deep Convolution GAN) GAN์„ ํ™œ์šฉํ•˜์—ฌ ์…€ ์ˆ˜๋„ ์—†์„ ๋งŒํผ ๋‹ค์–‘ํ•œ GAN ์‘์šฉ ๋…ผ๋ฌธ๋“ค์ด ์žˆ์ง€๋งŒ, ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์‘์šฉ GAN ๋…ผ๋ฌธ๋ถ€ํ„ฐ ๋‹ค๋ค„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2016๋…„ ๋ฐœํ‘œ๋œ ๋…ผ๋ฌธ Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks์—์„œ ์ฒ˜์Œ ๋“ฑ์žฅํ•œ Deep Convolutional Generative Adversarial Nets (DCGAN)์ž…๋‹ˆ๋‹ค. Original GAN์„ ์ด์šฉํ•ด ์ƒ์„ฑํ•œ a) MNIST, b) TFD, c&d) CIFAR-10 ๋ฐ์ดํ„ฐ 2014๋…„ Ian Goodfellow๊ฐ€ ๊ณต๊ฐœํ•œ ๊ธฐ์กด์˜ GAN์€ MNIST ๊ฐ™์€ ๋น„๊ต์  ๋‹จ์ˆœํ•œ ์ด๋ฏธ์ง€์—์„œ๋Š” ์ž˜ ์ž‘๋™ํ–ˆ์ง€๋งŒ CIFAR-10 ๊ฐ™์ด ์กฐ๊ธˆ๋งŒ ์ด๋ฏธ์ง€๊ฐ€ ๋ณต์žกํ•ด์ ธ๋„ ์„ฑ๋Šฅ์ด ๊ทธ๋‹ค์ง€ ์ข‹์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋Œ€๋ถ€๋ถ„์˜ Deep neural network์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ black box model์ด์—ˆ๊ธฐ์— ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๊ธฐ์—๋„ ์‰ฝ์ง€ ์•Š์•˜์ฃ . ๋˜ ์ด๋•Œ ๋‹น์‹œ๋งŒ ํ•˜๋”๋ผ๋„ GAN์ด ์ƒ์„ฑํ•œ ๊ฒฐ๊ณผ๋ฌผ์ด ์–ผ๋งˆ๋‚˜ ์ž˜ ๋งŒ๋“ค์—ˆ๋Š”์ง€ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ์ง€ํ‘œ๊ฐ€ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅํ•œ ๋ชจ๋ธ์ด DCGAN์ž…๋‹ˆ๋‹ค. DCGAN ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ Fully connected layer์™€ Pooling layer๋ฅผ ์ตœ๋Œ€ํ•œ ๋ฐฐ์ œํ•˜๊ณ  Strided Convolution๊ณผ Transposed Convolution์œผ๋กœ ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. Fully connected layer์™€ Max-pooling layer๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๋ฏธ์ง€์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ์žƒ์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Generator์™€ Discriminator์— ๋ฐฐ์น˜ ์ •๊ทœํ™”(Batch Nomalization)์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ์น˜์šฐ์ณ์ ธ ์žˆ์„ ๊ฒฝ์šฐ์˜ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์„ ์กฐ์ •ํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ back propagation์„ ์‹œํ–‰ํ–ˆ์„ ๋•Œ ๊ฐ ๋ ˆ์ด์— ์–ด ์ œ๋Œ€๋กœ ์ „๋‹ฌ๋˜๋„๋ก ํ•ด ํ•™์Šต์ด ์•ˆ์ •์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋Š”๋ฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ layer๋ฅผ ์ œ์™ธํ•˜๊ณ  ์ƒ์„ฑ์ž์˜ ๋ชจ๋“  layer์— ReLU activation๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ layer์—๋Š” Tanh๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. Discriminator์˜ ๋ชจ๋“  ๋ ˆ์ด์–ด์— LeakyReLU๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์ด ๋‹ค์ˆ˜์˜ ์‹คํ—˜์„ ํ†ตํ•ด ์ฐพ์•„๋‚ธ ์ตœ์ ์˜ ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. DCGAN์˜ Generator ๊ตฌ์กฐ DCGAN์˜ ์„ฑ๋Šฅ DCGAN์„ LSUN(Large-scale Scene Understanding) ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜์—ฌ ํ•™์Šต์‹œํ‚ค๊ณ  1 epoch ํ›„์— ์–ป์€ ๊ฒฐ๊ณผ๊ฐ€ Figure 2.์— ์ œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 5 epoch ํ›„์— ์–ป์„ ๊ฒฐ๊ณผ๋Š” Figure 3.์— ์ œ์‹œ๋˜์–ด ์žˆ๋Š”๋ฐ ์„ฑ๋Šฅ์ด ํ›จ์”ฌ ๋” ์ข‹์•„์กŒ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. DCGAN์˜ ํ•™์Šต DCGAN์˜ ํ•™์Šต์ด ์ž˜ ์ด๋ค„์กŒ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ฒ€์ฆ ๋ฐฉ๋ฒ•์„ ๋„์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ž ์žฌ ๊ณต๊ฐ„ (latent space, z)์— ์‹ค์ œ ๋ฐ์ดํ„ฐ์˜ ํŠน์„ฑ์ด ํˆฌ์˜๋๋Š”์ง€ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ๋žŒ ์–ผ๊ตด์„ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋ธ์ด ์ž˜ ํ•™์Šต๋˜๋ฉด ์„ฑ๋ณ„, ๋จธ๋ฆฌ ์ƒ‰๊น”, ์–ผ๊ตด ๋ฐฉํ–ฅ, ์•ˆ๊ฒฝ ์ฐฉ์šฉ ์—ฌ๋ถ€ ๋“ฑ์˜ ์˜๋ฏธ ์žˆ๋Š” ๋‹จ์œ„๋“ค์ด ์ž ์žฌ ๊ณต๊ฐ„์— ๋“œ๋Ÿฌ๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ƒ์„ฑ์ž์˜ ์ž…๋ ฅ์ธ 100์ฐจ์›์งœ๋ฆฌ 'z' ๋ฒกํ„ฐ์˜ ๊ฐ’์„ ๋ฐ”๊พธ๋ฉด ์ƒ์„ฑ์ž์˜ ์ถœ๋ ฅ์ธ ์ด๋ฏธ์ง€์˜ ์†์„ฑ์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์‚ฌ์ง„์€ DCGAN์ด ์ƒ์„ฑํ•œ ์‚ฌ๋žŒ ์–ผ๊ตด ์ด๋ฏธ์ง€์—์„œ ์–ผ๊ตด ๋ฐฉํ–ฅ์— ํ•ด๋‹นํ•˜๋Š” 'z' ๋ฒกํ„ฐ์˜ ๊ฐ’์„ ๋ฐ”๊พธ์–ด์„œ ์–ผ๊ตด์ด ๋ฐ”๋ผ๋ณด๋Š” ๋ฐฉํ–ฅ์„ ๋ฐ”๊พผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์€ ์ƒ์„ฑ์ž๊ฐ€ ์–ผ๊ตด์˜ ์˜๋ฏธ์ ์ธ ์†์„ฑ์„ ํ•™์Šตํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. Reference ์›๋…ผ๋ฌธ Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks ๋ธ”๋กœ๊ทธ DCGAN, cGAN and SAGAN & the CIFAR-10 dataset ๋ผ์˜จํ”ผํ”Œ ๋จธ์‹ ๋Ÿฌ๋‹ ์•„์นด๋ฐ๋ฏธ | DCGAN ์‰ฝ๊ฒŒ ์” GAN 4) SRGAN(Super Resolution GAN) Image Super Resolution (์ดํ•˜ SR)์€ ์ € ํ•ด์ƒ๋„(Low Resolution) ์ด๋ฏธ์ง€๋ฅผ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€(High Resolution) ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜์‹œํ‚ค๋Š” ๋ฌธ์ œ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. SR์ด ์‚ฌ์šฉ๋˜๋Š” ๋ถ„์•ผ๋Š” ๋ฌด๊ถ๋ฌด์ง„ํ•œ๋ฐ, ๊ณผ๊ฑฐ์˜ TV์—์„œ ์‚ฌ์šฉํ•˜๋˜ HD(1280 x 720) ํ•ด์ƒ๋„์˜ ์˜์ƒ์„ ์ตœ์‹  TV์— ๋งž๊ฒŒ UHD(3840 x 2160)์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋”์šฑ ์„ ๋ช…ํ•œ ํ™”์งˆ๋กœ ๊ฐ์ƒ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ฑฐ๋‚˜, ์šฐ์ฃผ์—์„œ ์ดฌ์˜ํ•œ ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ ํ”ผ์‚ฌ์ฒด์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์•„์„œ ๋ถ„๋ณ„์ด ์–ด๋ ค์šด ๊ฒฝ์šฐ์—๋„ SR์ด ์ ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ SIngle Image Super Resolution ๋ฌธ์ œ๋ฅผ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์€ ํฌ๊ฒŒ 3๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. Interpolation-based method Reconstruction-based method (Deep) Learning-based method ์ด๋ฅผ ๋ชจ๋‘ ๋‹ค๋ฃจ๊ธฐ๋Š” ์–ด๋ ค์šฐ๋‹ˆ ์ด ๊ธ€์—์„œ๋Š” Deep Learning-based method, ๊ทธ์ค‘์—์„œ๋„ 2017๋…„ ๊ณต๊ฐœ๋œ ๋…ผ๋ฌธ Photo-Realistic Single Image Super Resolution Using a Generative Adversarial Network์—์„œ ์ฒ˜์Œ ๋“ฑ์žฅํ•œ SR GAN์„ ์ค‘์ ์ ์œผ๋กœ ์†Œ๊ฐœ๋“œ๋ฆด ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. Introduction Super Resolution์˜ (Deep) Learning-based method์˜ ์ดˆ๊ธฐ ๋ชจ๋ธ๋ถ€ํ„ฐ GAN์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์€ ์•„๋‹ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ดˆ๋กœ deep learning์„ super resolution์— ์ ์šฉํ•œ SRCNN ์ดํ›„ ๋” ๋น ๋ฅด๊ณ , ๋” ๊นŠ์€ CNN ๋ชจ๋ธ๋“ค์ด ์ œ์‹œ๋˜์—ˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ํ•ด๊ฒฐ๋˜์ง€ ์•Š์€ ๋ฌธ์ œ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. How do we recover the finer texture details when we super-resolve at large upscaling factors? ๊ธฐ์กด SR ๋ชจ๋ธ ์ค‘ ํ•˜๋‚˜์ธ SRResNet์ด ์ƒ์„ฑํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋งค์šฐ ํ™•๋Œ€ํ•ด ๋ณด๋ฉด, original HR image์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ texture detail์ด ๋–จ์–ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ ์ด ์›์ธ์ด ๊ธฐ์กด SR ๋ชจ๋ธ๋“ค์˜ loss function์— ์žˆ๋‹ค๊ณ  ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด SR ๋ชจ๋ธ๋“ค์˜ ๋ชฉํ‘œ๋Š” ๋ณดํ†ต ๋ณต๊ตฌ๋œ HR ์ด๋ฏธ์ง€์™€ ์›๋ณธ ์ด๋ฏธ์ง€์˜ pixel ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ pixel-wise MSE๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ pixel-wise loss๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด high texture detail์„ ์ œ๋Œ€๋กœ ์žก์•„๋‚ด์ง€ ๋ชปํ•˜๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์ž๋“ค์€ ์ด์ „ ์—ฐ๊ตฌ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ VGG network์˜ high-level feature map์„ ์ด์šฉํ•œ perceptual loss๋ฅผ ์ œ์‹œํ•˜์—ฌ ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ Generator network, G ์ด๋ฏธ์ง€์— ๋‚˜์™€์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋˜‘๊ฐ™์€ layout์„ ์ง€๋‹Œ B ๊ฐœ์˜ residual block์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. Residual block์˜ ๊ตฌ์„ฑ - kernel size: 3 x 3 - kernel ๊ฐœ์ˆ˜: 64 - stride: 1 - Batch normalization layer - Activation function: ParametricReLU ์ผ๋ฐ˜์ ์œผ๋กœ convolution layer๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ทธ image์˜ ์ฐจ์›์€ ์ž‘์•„์ง€๊ฑฐ๋‚˜ ๋™์ผํ•˜๊ฒŒ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค. super resolution์„ ์œ„ํ•ด image์˜ dimension์„ ์ฆ๊ฐ€์‹œ์ผœ์•ผ ํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ์ด์šฉ๋œ ๋ฐฉ์‹์ด sub-pixel convolution์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. (๋…ผ๋ฌธ์—์„œ๋Š” ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋‹ค๊ณ  ํ•˜๊ณ  ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ด๋Š” 2016. 9 CVPR, Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network์—์„œ ์†Œ๊ฐœ๋œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ถ๊ธˆํ•˜๋ฉด ์ฝ์–ด๋ณด์„ธ์š”.) Discriminator Network, D LeakyReLU(ฮฑ=0.2)๋ฅผ ์‚ฌ์šฉํ–ˆ๊ณ , max-pooling์€ ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋ฏ€๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. - 3 ร— 3 kernel์„ ์‚ฌ์šฉํ•˜๋Š” conv layer 8๊ฐœ๋กœ ๊ตฌ์„ฑ - feature map์˜ ์ˆ˜๋Š” VGG network์ฒ˜๋Ÿผ 64๋ถ€ํ„ฐ 512๊นŒ์ง€ ์ปค์ง. ๋งˆ์ง€๋ง‰ feature maps ๋’ค์—๋Š” dense layer ๋‘ ๊ฐœ, ๊ทธ๋ฆฌ๊ณ  classification์„ ์œ„ํ•œ sigmoid๊ฐ€ ๋ถ™์Šต๋‹ˆ๋‹ค. Loss function - Perceptual loss Loss function์œผ๋กœ Perceptual loss๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ content loss์™€ adversarial loss๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ค‘ adversarial loss๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์•Œ๊ณ  ์žˆ๋Š” GAN์˜ loss์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ธˆ ํŠน๋ณ„ํ•œ ๋ถ€๋ถ„์€ Content loss์ž…๋‹ˆ๋‹ค. Adversarial loss G n R โˆ‘ = N log D D ( ฮธ ( L) ) D D ( ฮธ ( L) ) ๋Š” Generator๊ฐ€ ์ƒ์„ฑํ•œ ์ด๋ฏธ์ง€๋ฅผ ์ง„์งœ๋ผ๊ณ  ํŒ๋‹จํ•  ํ™•๋ฅ ๋กœ ์•ž์— - ๊ฐ€ ๋ถ™์–ด์žˆ์œผ๋ฏ€๋กœ ์ด๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด GAN loss๋Š” log (1-x)์˜ ํ˜•ํƒœ๋กœ ๋˜์–ด์žˆ์œผ๋‚˜ ์ด๋Ÿฌ๋ฉด training ์ดˆ๋ฐ˜ ๋ถ€์— ํ•™์Šต์ด ๋Š๋ฆฌ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ -log (x) ํ˜•ํƒœ๋กœ ๋ฐ”๊พธ์–ด์ฃผ๋ฉด ํ•™์Šต ์†๋„๊ฐ€ ํ›จ์”ฌ ๋นจ๋ผ์ง„๋‹ค๊ณ  ํ•˜๋„ค์š”. Content loss V G i j R 1 i j i j x 1 i j y 1 i j ( i j ( ( H) , ) ฯ•, ( ฮธ ( L) ) , ) ฮฆ_ i, j = Feature map obtained by the jth convolution (after activation) before the ith maxpooling layer within the VGG 19 network ์‹œ๊ทธ๋งˆ ์•ˆ์— ๊ฐ’ = Generator๊ฐ€ ์ƒ์„ฑํ•œ ์ด๋ฏธ์ง€์™€ original HR ์ด๋ฏธ์ง€๋กœ๋ถ€ํ„ฐ ์–ป์€ Feature map ์‚ฌ์ด์˜ Euclidean distance Wi, j & Hi, j = the dimensions of the respective feature maps within the VGG network. Content loss๋Š” Perceptual Losses for Real-Time Style Transfer and Super-Resolution๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ ์ฒ˜์Œ ์ œ์‹œ๋œ Perceptual loss์™€ ๊ฑฐ์˜ ๋™์ผํ•˜๋ฉฐ, ์ด์— ๋Œ€ํ•ด ์ž˜ ์„œ์ˆ ํ•˜๊ณ  ์žˆ๋Š” ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ฝ์–ด๋ณด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํžˆ๋งŒ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Generator์„ ์ด์šฉํ•ด ์–ป์–ด๋‚ธ ๊ฐ€์งœ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋ฅผ ์ง„์งœ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€์™€ Pixel by pixel๋กœ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์„ Per-pixel loss๋ผ๊ณ  ํ•˜๊ณ , ๊ฐ ์ด๋ฏธ์ง€๋ฅผ pre-trained CNN ๋ชจ๋ธ์— ํ†ต๊ณผ์‹œ์ผœ ์–ป์–ด๋‚ธ feature map์„ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์„ Perceptual loss๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ์ด๋ฏธ์ง€์ด๋‚˜ ํ•œ pixel์”ฉ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€๋ ค์žˆ๋Š” ๋‘ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ loss๋Š” 0 ์ด์–ด์•ผ ํ•˜๊ฒ ์ง€๋งŒ per-pixel loss๋ฅผ ๊ตฌํ•˜๋ฉด ์ ˆ๋Œ€ 0์ด ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. per-pixel loss์˜ ์ด๋Ÿฌํ•œ ๋‹จ์ ์€ super resolution์˜ ๊ณ ์งˆ์ ์ธ ๋ฌธ์ œ์ธ Ill-posed problem ๋•Œ๋ฌธ์— ๋” ๋ถ€๊ฐ๋ฉ๋‹ˆ๋‹ค. [Ill-posed problem ์˜ˆ์‹œ] Ill-posed problem ์ด๋ž€ ์ € ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋ฅผ ๊ณ ํ•ด์ƒ๋„๋กœ ๋ณต์›์„ ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๊ฐ€๋Šฅํ•œ ๊ณ ํ•ด์ƒ๋„์˜ ์ด๋ฏธ์ง€๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. GAN ๋ชจ๋ธ์„ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ€๋Šฅํ•œ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€ (์•„๋ž˜ ๊ทธ๋ฆผ์ƒ Possible solutions)๋ฅผ ๊ตฌํ•˜์—ฌ๋„ MSE based Per-pixel loss๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด possible solutions ๋“ค์„ ํ‰๊ท  ๋‚ด๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ทจํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, GAN์ด ์ƒ์„ฑํ•œ ๋‹ค์–‘ํ•œ high texture detail๋“ค์ด smoothing ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ดˆ๋ž˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ €์ž๋“ค์€ GAN์ด ์ƒ์„ฑํ•œ HR ์ด๋ฏธ์ง€์™€ Original HR ์ด๋ฏธ์ง€๋ฅผ Pretrained VGG 19์— ํ†ต๊ณผ์‹œ์ผœ ์–ป์€ Feature map ์‚ฌ์ด์˜ Euclidean distance๋ฅผ ๊ตฌํ•˜์—ฌ content loss๋ฅผ ๊ตฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์„ฑ๋Šฅ SR GAN์ด ์ƒ์„ฑํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋งค์šฐ ํ™•๋Œ€ํ•ด ๋ณด๋ฉด, SRResNet์ด ๋งŒ๋“  ์ด๋ฏธ์ง€์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ texture detail์ด ์ข‹์•„์กŒ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ MOS (Mean Opinion score) testing์„ ์ง„ํ–‰ํ•˜์˜€์„ ๋•Œ SRGAN์˜ ์—„์ฒญ๋‚œ ์„ฑ๋Šฅ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. MOS (Mean Opinion score) testing์€ 26๋ช…์˜ ์‚ฌ๋žŒ์— ์„ธ 1์ (bad)๋ถ€ํ„ฐ 5์  (excellent)๊นŒ์ง€ ์ ์ˆ˜๋ฅผ ๋งค๊ธฐ๋„๋ก ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. (๊ธฐ์กด Super Resolution rating์—์„œ ํ”ํžˆ ์‚ฌ์šฉํ•˜๋˜ PSNR์ด๋‚˜ SSIM๊ณผ ๊ฐ™์€ ์ ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ด์œ ๋Š” ํ•ด๋‹น ์ ์ˆ˜๋“ค์ด MSE๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ธฐ๊ณ„์ ์œผ๋กœ ์ ์ˆ˜๋ฅผ ์‚ฐ์ถœํ•  ๋ฟ, ์‹ค์ œ ์‚ฌ๋žŒ์˜ ํ‰๊ฐ€๋ฅผ ์ œ๋Œ€๋กœ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•˜๋Š” ํ•œ๊ณ„๋ฅผ ๋ณด์˜€๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.) Reference ์›๋…ผ๋ฌธ Perceptual Losses for Real-Time Style Transfer and Super-Resolution Photo-Realistic Single Image Super Resolution Using a Generative Adversarial Network Youtube DMQA Open Seminar | Image Super Resolution Photo-Realistic Single Image Super Resolution Using a Generative Adversarial Network ๋ธ”๋กœ๊ทธ Super Resolution ๊ฐœ๋ก  Perceptual loss SR GAN ๋…ผ๋ฌธ ๋ฆฌ๋ทฐ ์ž‘์‹ฌ์‚ผ์ผ | SRGAN ๋ฆฌ๋ทฐ Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network (CVPR 17) 5) Cycle GAN image to image translation ์ด๋ž€ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์…‹์„ ์‚ฌ์šฉํ•˜์—ฌ input ์ด๋ฏธ์ง€์™€ output ์ด๋ฏธ์ง€๋ฅผ mapping ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” ์ƒ์„ฑ ๋ชจ๋ธ์˜ ํ•œ ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. ํ‘๋ฐฑ ์ด๋ฏธ์ง€์— ์ปฌ๋Ÿฌ๋ฅผ ์ž…ํžŒ๋‹ค๋“ ์ง€, ๋‚ฎ ์‚ฌ์ง„์„ ๋ฐค ์‚ฌ์ง„์œผ๋กœ ๋งŒ๋“ ๋‹ค๋“ ์ง€, ํ…Œ๋‘๋ฆฌ๋งŒ ์ฃผ์–ด์ง„ ์‚ฌ์ง„์„ ์‹ค์ œ ๋ฌผ๊ฑด๊ฐ™์ด ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜์ฃ . ์ตœ๊ทผ ์ธ๊ณต์ง€๋Šฅ ์ฑ„์ƒ‰์ด๋‚˜ ์‚ฌ์ง„ ๋ณต๊ตฌ, ๊ทธ๋ฆผ์ฒด ๋ณ€ํ˜• ๋“ฑ์˜ ์•ฑ ๋“ฑ์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. image to image translation์˜ ๋Œ€ํ‘œ์  ๋ชจ๋ธ๋กœ Pix-2-Pix, Cycle GAN, Style GAN ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Pix-2-Pix๊ฐ€ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜์˜€๊ณ  ์ดํ›„ Pix-2-Pix์˜ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Cycle GAN์ด ๋“ฑ์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. (๋‘ ๋…ผ๋ฌธ์€ ๊ฐ™์€ Berkeley AI Research(BAIR) ๋žฉ์‹ค์—์„œ ๋ฐœํ‘œ๋œ ๋…ผ๋ฌธ์ด๊ธฐ๋„ ํ•˜๋ฉฐ Cycle GAN์˜ ์›์ €์ž๋Š” ์‹ฌ์ง€์–ด ํ•œ๊ตญ์ธ!) Style GAN์€ Cycle GAN์„ ๋ฒ ์ด์Šค๋กœ ํ•˜์—ฌ ๊ฐœ๋ฐœ๋˜์—ˆ๊ณ ์š”. ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ Pix-2-Pix์— ๋Œ€ํ•ด ๋จผ์ € ์•Œ์•„๋ณด๊ณ  Cycle GAN์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Related work - Pix-2-Pix ์ผ๋ฐ˜์ ์ธ CNN ๋ชจ๋ธ์—์„œ ์„ค์ •ํ•˜๋“ฏ, Generator๊ฐ€ ๋งŒ๋“ค์–ด๋‚ธ ์ด๋ฏธ์ง€์™€ ์‹ค์ œ ์ด๋ฏธ์ง€ ์‚ฌ์ด์˜ ์œ ํด๋ผ๋””์•ˆ ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋„๋ก Loss function์„ ์„ค์ •ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. x = input image, G(x) = ๋ชจ๋ธ์ด ์ƒ์„ฑํ•œ ์ด๋ฏธ์ง€, y = Ground truth ๋ฌธ์ œ๋Š” ์ด๋Ÿฐ loss function์„ ์„ค์ •ํ•˜๋ฉด ๊ฐ€์šด๋ฐ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋ฟŒ์˜‡๊ฒŒ (blurring) ํ‘œํ˜„๋œ output์ด ๋‚˜์˜จ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” Generator๊ฐ€ input image๋งŒ ๋ณด๋ฉด ๋ฒฝ์ด ๋‚˜๋ฌด๋กœ ๋˜์–ด์žˆ๋Š”์ง€, ํฐ์ƒ‰ ๋ฒฝ๋Œ๋กœ ๋˜์–ด์žˆ๋Š”์ง€ / ์ƒˆ์˜ ๊นƒํ„ธ ์ƒ‰์ด ์–ด๋–ค์ง€ ์ „ํ˜€ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Generator๋Š” ์–ด๋Š ๊ฒƒ์„ ํƒํ•ด๋„ loss๊ฐ€ ๋„ˆ๋ฌด ์ปค์ง€์ง€ ์•Š๋„๋ก ์• ๋งคํ•œ ์ค‘๊ฐ„๊ฐ’์„ ํƒํ•˜๋Š” ๊ฒฝํ–ฅ์„ ๋ณด์ด๊ฒŒ ๋˜์–ด ์• ๋งคํ•œ ๊ฒฐ๊ณผ๋ฌผ์„ ๋„์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ €์ž๋“ค์€ conditional GAN (cGAN)์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. (conditional GAN๊นŒ์ง€ ๋‹ค๋ฃจ๋ฉด ๋„ˆ๋ฌด ๊ธธ์–ด์ง€๋‹ˆ ์ž˜ ์„ค๋ช…๋œ Conditional GAN ๊ด€๋ จ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์„ธ์š”.) GAN์€ ์–ด๋–ค ๋ฐ์ดํ„ฐ ๋ถ„ํฌ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์‹ค์ œ์— ๊ฐ€๊น๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋ธ์ด๊ธฐ ๋•Œ๋ฌธ์— input image์™€ ๋น„์Šทํ•˜๋‚˜ ๋‹ค๋ฅธ, ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. cGAN์€ ground truth (y)๋ฅผ ์ถ”๊ฐ€๋กœ Generator์— ์ œ์‹œํ•˜์—ฌ input image๋ฅผ ground truth (y)์™€ '์—ฐ๊ด€๋œ' ์ด๋ฏธ์ง€๋กœ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ cGAN์˜ ํŠน์„ฑ์€ image to image translation์˜ ๋ชฉํ‘œ, ์ฆ‰ ํŠน์ • ์ด๋ฏธ์ง€๋ฅผ ์ œ์‹œํ•˜๊ณ  ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ ์•„์ฃผ ์ ํ•ฉํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. Pix-2-Pix์˜ loss function ์ตœ์ข… pix2pix์˜ loss function์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ•ญ์€ cGAN์˜ loss function์ž…๋‹ˆ๋‹ค. (x, y, z๊ฐ€ ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์œ ์˜ํ•ด์„œ ๋ด์ฃผ์„ธ์š”. z = random noise vector, y = input image, x= ground truth) ๋‘ ๋ฒˆ์งธ ํ•ญ์€ ๋งŒ๋“ค์–ด๋‚ธ ์ด๋ฏธ์ง€์™€ ์‹ค์ œ ์ด๋ฏธ์ง€ ๊ฐ„์˜ ๊ฒฉ์ฐจ๋ฅผ ์ค„์—ฌ์ฃผ๋Š” loss function์ธ L1 loss function์ž…๋‹ˆ๋‹ค. Pix-2-Pix์˜ ์„ฑ๋Šฅ๊ณผ ํ•œ๊ณ„ ์•„๋ž˜ ์‚ฌ์ง„์„ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋“ฏ์ด cGAN๊ณผ L1 loss๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ๊ฐ€์žฅ ํ›Œ๋ฅญํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋Š” Pix-2-Pix๋ฅผ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•ด paired image ๋ฐ์ด ์…‹์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๋ง ์‚ฌ์ง„์— ์–ผ๋ฃฉ๋ง์ด ๊ฐ€์ง„ ํŠน์ง•์„ ์”Œ์›Œ์„œ ์–ผ๋ฃฉ๋ง๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด ๋ง ์‚ฌ์ง„๊ณผ ๋™์ผํ•œ ํฌ์ฆˆ์™€ ํฌ๊ธฐ์˜ ์–ผ๋ฃฉ๋ง ์‚ฌ์ง„, ์ฆ‰ paired image๋ฅผ ๊ฐ€์ง€๊ณ  ํ•™์Šต์„ ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. Pix-2-Pix ํ•™์Šต์„ ์œ„ํ•ด paired image ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌํ•˜๋Š” ๋น„์šฉ์ด ํฌ๋ฉฐ ์–ด๋–ค ๊ฒฝ์šฐ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ‘๋ฐฑ ์‚ฌ์ง„์„ ์ปฌ๋Ÿฌ ์‚ฌ์ง„์œผ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•œ training dataset์€ ๊ตฌ์„ฑํ•˜๊ธฐ ์‰ฝ์ง€๋งŒ, ํ”ผ์นด์†Œ์˜ ๊ทธ๋ฆผ์„ ์‹ค๋ฌผ ์ด๋ฏธ์ง€๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์…‹์„ ๊ตฌ์„ฑํ•˜๊ธฐ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅํ•œ ๋ชจ๋ธ์ด Cycle GAN์ž…๋‹ˆ๋‹ค. Cycle GAN์˜ ๊ตฌ์กฐ unpaired image ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  image to image translation๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด cycle GAN์˜ ๋ชฉ์ ์ด๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋ชจ๋ธ์˜ loss function์„ GAN์˜ Loss function์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ Mode collapse๋ผ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. Mode collapse๋ฅผ ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•œ ๊ทธ๋ฆผ์€ ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ๋ฐ์ดํ„ฐ ๋ถ„ํฌ๊ฐ€ p(x) (ํŒŒ๋ž€์ƒ‰ ์‹ค์„ )๋ผ๊ณ  ์ฃผ์–ด์กŒ์„ ๋•Œ, ์šฐ๋ฆฌ๋Š” generator๊ฐ€ ์ด ์‹ค์ œ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ํ•™์Šตํ•˜๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ GAN loss๋ฅผ ์ด์šฉํ•ด ํ•™์Šต์„ ํ•˜๋‹ค ๋ณด๋ฉด Generator๊ฐ€ q'(x) (์ดˆ๋ก์ƒ‰ ์ ์„ )๊ณผ ๊ฐ™์ด ํ•˜๋‚˜์˜ mode์—๋งŒ ๊ฐ•ํ•˜๊ฒŒ ๋ชฐ๋ฆฌ๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด input์˜ ํŠน์ง•์„ ๋‹ค ์žŠ์–ด๋ฒ„๋ฆฌ๊ณ  ๋˜‘๊ฐ™์€ ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋ฅผ Mode collapse๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ์กด์˜ Generator G ์™ธ์— Generator F๋ฅผ ์ถ”๊ฐ€ํ•œ ์ˆœํ™˜๊ตฌ์กฐ๊ฐ€ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. input image์—์„œ output image๋กœ ๋งคํ•‘ํ•˜๋Š” ๋™์ž‘ ๊ณผ์ •์„ forward consistency๋ผ๊ณ  ํ•˜๊ณ , ๋ฐ˜๋Œ€์˜ ๊ณผ์ •์„ backward consistency๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Generator G์™€ F๋ฅผ ๊ฑฐ์ณ์„œ ํ•œ ๋ฐ”ํ€ด ๋Œ์•„์˜ค๋ฉด ๋‹ค์‹œ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๋Œ์•„์™€์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆœํ™˜ ์ผ๊ด€์„ฑ(Cycle Consistency)์ด๋ผ๊ณ  ํ–ˆ๊ณ , Input image์™€ Generator G, F ํ•œ ๋ฐ”ํ€ด๋ฅผ ๋Œ์•„ ์ƒ์„ฑ๋œ Output image ๊ฐ„์˜ ์ฐจ์ด๋ฅผ cycle consistency loss๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. Forward cycle consistency - X๋ฅผ Generator G์— ๋„ฃ์–ด G(X), ํ˜น์€ Y_hat๋ฅผ ์ถœ๋ ฅํ•จ - Y์™€ G(X)๋ฅผ Discriminator์— ์ œ์‹œํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฌผ Dy๋ฅผ ์ถœ๋ ฅํ•จ - G(X), ํ˜น์€ Y_hat์„ Generator F์— ๋„ฃ์–ด X_hat์œผ๋กœ ๋ณต๊ตฌํ•จ - ๊ฒฐ๋ก : x โ†’ G(x) โ†’ F(G(x)) โ‰ˆ x Backward cycle consistency - Y๋ฅผ Generator F์— ๋„ฃ์–ด F(Y), ํ˜น์€ X_hat๋ฅผ ์ถœ๋ ฅํ•จ - X์™€ F(Y)๋ฅผ Discriminator์— ์ œ์‹œํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฌผ Dx๋ฅผ ์ถœ๋ ฅํ•จ - F(Y), ํ˜น์€ X_hat์„ Generator G์— ๋„ฃ์–ด Y_hat์œผ๋กœ ๋ณต๊ตฌํ•จ - ๊ฒฐ๋ก : y โ†’ F(y) โ†’ G(F(y)) โ‰ˆ y cycleGAN ๊ตฌ์กฐ์˜ ํ•ต์‹ฌ์€ input์„ ์ฃผ์—ˆ์„ ๋•Œ ์˜๋„ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ”๊พธ๋˜, ์ด๊ฑธ ๋‹ค์‹œ ์›๋ž˜์˜ input์œผ๋กœ ๋˜๋Œ๋ฆด ์ˆ˜ ์žˆ์„ ์ •๋„๋กœ ๋ฐ”๊พธ๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. (๋ณต์žกํ•œ ์ˆ˜์‹ ์—†์ด ๋จธ๋ฆฟ์†์œผ๋กœ ๊ทธ๋ ค๋งŒ ๋ด๋„ ์ด๋Ÿฐ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด mode collapse๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์ฃ ? Cycle GAN์˜ loss function Cycle GAN์˜ loss function์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ ํ•ญ๋ชฉ์€ ํฌ๊ฒŒ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง‘๋‹ˆ๋‹ค. Adverarial Loss CycleGAN ์—ญ์‹œ ์ƒ์„ฑ ๋ชจ๋ธ์ด๊ธฐ์— adversarial loss๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ ์•Œ์•„๋‘ฌ์•ผ ํ•  ์ ์€ ์ผ๋ฐ˜์ ์ธ GAN์ฒ˜๋Ÿผ Cross Entropy loss๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  Least Square loss๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. (Cross Entropy loss๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์ข‹์•˜๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.) Forward & Backward Adverarial Loss๋ฅผ ๋ชจ๋‘ ๋”ํ•ด์ค๋‹ˆ๋‹ค. G N ( , Y X Y ) E p a a ( ) [ log D ( ) ] E p a a ( ) [ log ( โˆ’ Y ( ( ) ) ) ] Cycle Consistency Loss c c ( , ) E p a a ( ) [ | ( ( ) ) x | ] E p a a ( ) [ | ( ( ) ) y | ] ์„ฑ๋Šฅ ๋ณธ ๋…ผ๋ฌธ์—์„œ๋Š” ๋‹ค์–‘ํ•œ ์˜ˆ์‹œ๋ฅผ ๋“ค๋ฉฐ Cycle GAN์˜ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. Cycle GAN์˜ ํ™œ์šฉ Style Transfer Season Transfer Photo generation from paintings ๋ชจ๋„ค์˜ ๊ทธ๋ฆผ์„ ์‚ฌ์ง„์ฒ˜๋Ÿผ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. Photo enhancement ์Šค๋งˆํŠธํฐ์œผ๋กœ ์ฐ์€ ์‚ฌ์ง„์„ DSLR๋กœ ์ฐ์€ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. ํ•œ๊ณ„ ์ด๋ฏธ์ง€์˜ ๋ชจ์–‘์„ ๋ฐ”๊พธ์ง„ ๋ชปํ•ฉ๋‹ˆ๋‹ค. CycleGAN์€ ์ฃผ๋กœ ๋ถ„์œ„๊ธฐ๋‚˜ ์ƒ‰์ƒ์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์œผ๋กœ ์Šคํƒ€์ผ์„ ํ•™์Šตํ•˜์—ฌ ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค ๋ณด๋‹ˆ ํ”ผ์‚ฌ์ฒด์˜ ๋ชจ์–‘ ์ž์ฒด๋Š” ๋ฐ”๊ฟ€ ์ˆ˜๊ฐ€ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. EX) ์‚ฌ๊ณผ๋ฅผ ์˜ค๋ Œ์ง€๋กœ ๋ฐ”๊พธ๋Š” ์ž‘์—… ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ถ„ํฌ๊ฐ€ ๋ถˆ์•ˆ์ •ํ•˜๋ฉด ์ด๋ฏธ์ง€๋ฅผ ์ œ๋Œ€๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. EX) ์‚ฌ๋žŒ์„ ํƒœ์šด ๋ง์„ ์–ผ๋ฃฉ๋ง๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์‚ฌ๋žŒ๊นŒ์ง€ ์–ผ๋ฃฉ๋ง์ด ๋˜๋Š” ๊ฒƒ์€ ๋ฐ์ดํ„ฐ ์…‹์— ์‚ฌ๋žŒ์ด ๋ง์„ ํƒ„ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ Reference ์›๋…ผ๋ฌธ Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks ๋ธ”๋กœ๊ทธ Loner์˜ ํ•™์Šต๋…ธํŠธ | Cycle GAN ๊ฐ„๋‹จ ์„ค๋ช… Conditional GAN Pix-2-Pix Cycle GAN ์ž‘์‹ฌ์‚ผ์ผ | Cycle GAN Youtube ๋™๋นˆ๋‚˜ | CycleGAN - ๋”ฅ๋Ÿฌ๋‹ ๊ธฐ๋ฐ˜์˜ ์ด๋ฏธ์ง€ ๋ณ€ํ™˜ ๊ธฐ๋ฒ• CycleGAN์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ๊ฐ„์˜ ์—ฐ๊ฒฐ ์ฐพ๊ธฐ - ์ €์ž ์ง๊ฐ• 6) Disco GAN Disco GAN ์ด๋ž€? Disco GAN์€ ์•ž์—์„œ ๋‚˜์˜จ Cycle GAN๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ Pix2Pix ๋ชจ๋ธ์˜ ํ•œ๊ณ„์ ์€ Paired Dataset์ด ํ•„์š”ํ•˜๋‹ค๋Š” ์ ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋ง์„ ํ†ตํ•ด ์–ผ๋ฃฉ๋ง์„ ์ƒ์„ฑํ•˜๋ ค๋ฉด ์ƒ์„ฑํ•˜๋ ค๋Š” ์–ผ๋ฃฉ๋ง๊ณผ ๋˜‘๊ฐ™์€ ์ž์„ธ๋กœ ์žˆ๋Š” ๋ง์ด ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค. Paired Dataset์€ ๊ตฌํ•˜๊ธฐ ์–ด๋ ต๊ณ , ์กด์žฌํ•˜์ง€ ์•Š์„ ํ™•๋ฅ ์ด ํฝ๋‹ˆ๋‹ค. Disco GAN์€ Paired Dataset์ด ๊ตฌํ•˜๊ธฐ ํž˜๋“  ๊ฒฝ์šฐ์—๋„ Style Transfer๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ GAN ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. Disco GAN ๊ตฌ์กฐ reconstruction loss๋ฅผ ๊ฐ–๋Š” ๋„คํŠธ์›Œํฌ๋“ค์€ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ํ•™์Šต์ด ์ด๋ฃจ์–ด์กŒ์—ˆ๋Š”๋ฐ Disco GAN์€ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์–‘๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋‘ ๊ฐœ์˜ const loss๋ฅผ ๊ฐ–๋Š” ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. 2๊ฐœ์˜ Discriminator์™€ 4๊ฐœ์˜ Generator๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ํ•™์Šต์„ ์‹œํ‚ฌ ๋•Œ ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋‘ ๊ฐ€์ง€ ๋„คํŠธ์›Œํฌ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ๋‘ ๋„คํŠธ์›Œํฌ์—์„œ ์ฒซ ๋ฒˆ์งธ ์ƒ์„ฑ์ž์˜ ์ถœ๋ ฅ์„ ์„œ๋กœ<NAME>๋ฉด์„œ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ํ•™์Šต์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Loss Function Input Image xA ์ƒ์„ฑ์ž GAB๋ฅผ ๊ฑฐ์ณ xAB๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ถœ๋ ฅ์ด GBA๋ฅผ ๊ฑฐ์น˜๋ฉด xABA๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰ xABA๋Š” xA ๊ฐ€ ๋‘ Generator๋ฅผ ๊ฑฐ์ณ ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. GAB์—๊ฒŒ๋Š” LCONSTA์™€ LGANB๊ฐ€ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. LCONSTA๋Š” ์ž…๋ ฅ์ด 2๊ฐœ์˜ generator๋“ค์„ ๊ฑฐ์นœ ํ›„์— ์–ผ๋งˆ๋‚˜ ์ž˜ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€๋ฅผ ์ธก์ •ํ•œ ๊ฒƒ์ด๊ณ , LGANB๋Š” ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€๊ฐ€ B ๋„๋ฉ”์ธ์—์„œ ์–ผ๋งˆ๋‚˜ ์‚ฌ์‹ค์ ์ธ์ง€๋ฅผ ์ธก์ •ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ์„ ํ•˜๋Š” ๋™์•ˆ ์ƒ์„ฑ์ž GAB๋Š” A๋„ ๋ฉ”์ธ์—์„œ B ๋„๋ฉ”์ธ์œผ๋กœ ๋งคํ•‘๋˜๋Š” ๊ฒƒ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. A๊ฐ€ B๋กœ ๋งคํ•‘๋œ ํ›„ ๋‹ค์‹œ B์—์„œ A๋กœ reconstruct ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‘ generator GAB์™€ GBA์˜ ๊ฒฐ๊ณผ๋กœ ์ƒ์„ฑ๋œ XAB์™€ XBA๋Š” ๊ฐ๊ฐ์˜ Discriminator์ธ LDA์™€ LDB์˜ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. Generator์™€ Dicriminator์˜ loss function์€ ์œ„์˜ ์‹๊ณผ ๊ฐ™์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. ์ฆ‰ ๋„๋ฉ”์ธ A์™€ ๋„๋ฉ”์ธ B์—์„œ ๊ตฌํ•œ ๊ฒƒ์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Loss GAN๊ณผ Loss const ๋ชจ๋‘ ๋‘ ๊ฐœ๊ฐ€ ์กด์žฌํ•˜๊ณ  2๊ฐ€์ง€์˜ ๋„คํŠธ์›Œํฌ๊ฐ€ ๊ฐ๊ฐ์˜ Loss๋ฅผ ๊ณต์œ ํ•จ์œผ๋กœ์จ GAN์—์„œ ์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” mode collapse problem์„ ์–ด๋Š ์ •๋„ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ Generator ๊ตฌ์กฐ Discriminator ๊ตฌ์กฐ Disco GAN ์„ฑ๋Šฅ ์‚ฌ๋žŒ์˜ ์„ฑ๋ณ„์„ ๋ฐ”๊พธ๊ฑฐ๋‚˜, ๋จธ๋ฆฌ ์ƒ‰์„ ๋ฐ”๊พธ๊ณ , ์•ˆ๊ฒฝ์„ ์—†์•ค ๋ชจ์Šต, ๋ช‡ ๋…„ ํ›„์˜ ๋ชจ์Šต ๋“ฑ์„ ์ƒ์„ฑํ•ด ๋‚ด์—ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์˜ˆ์‹œ์—์„œ๋Š” ๊ต‰์žฅํžˆ ๋†’์€ ์„ฑ๋Šฅ์œผ๋กœ ์ƒ์„ฑ๋œ ์‚ฌ์ง„๋“ค์ด ๋ชจ๋‘ ์ด์งˆ๊ฐ ์—†์ด ์ž˜ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Disco GAN์˜ ํ™œ์šฉ ์™ผ์ชฝ์ด Input์ด๊ณ  ์˜ค๋ฅธ์ชฝ์ด Output์ž…๋‹ˆ๋‹ค. 1. ์‹ ๋ฐœ ๊ทธ๋ฆผ๊ณผ ์‹ ๋ฐœ ์‹ ๋ฐœ์„ ํ†ตํ•ด ๋น„์Šทํ•œ ๊ทธ๋ฆผ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ๊ทธ๋ฆผ์„ ํ†ตํ•ด ๋น„์Šทํ•œ ์Šคํƒ€์ผ์˜ ์‹ ๋ฐœ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 2. ๊ฐ€๋ฐฉ ๊ทธ๋ฆผ๊ณผ ๊ฐ€๋ฐฉ ๊ฐ€๋ฐฉ์„ ํ†ตํ•ด ๋น„์Šทํ•œ ๊ทธ๋ฆผ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ๊ทธ๋ฆผ์„ ํ†ตํ•ด ๋น„์Šทํ•œ ์Šคํƒ€์ผ์˜ ๊ฐ€๋ฐฉ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 3. ์‹ ๋ฐœ๊ณผ ๊ฐ€๋ฐฉ ๊ฐ€๋ฐฉ๊ณผ ๋น„์Šทํ•œ ์Šคํƒ€์ผ์˜ ์‹ ๋ฐœ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ์‹ ๋ฐœ๊ณผ ๋น„์Šทํ•œ ์Šคํƒ€์ผ์˜ ๊ฐ€๋ฐฉ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 4. ๋ง ์˜์ƒ ์–ผ๋ฃฉ๋ง๋กœ ๋ง ์˜์ƒ์„ ์–ผ๋ฃฉ๋ง๋กœ ๋ฐ”๊พธ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ณ„ Reference https://github.com/taeoh-kim/Pytorch_DiscoGAN Disco GAN ๋…ผ๋ฌธ 7) Improved Techniques for Training GANs GAN์„ ํ›ˆ๋ จํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒ์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋‚ด์šฉ์€ Improved Techniques for Training GAN์— ๊ธฐ๋ก๋œ ๋‚ด์šฉ์œผ๋กœ ์ €์ž๋“ค์˜ ๊ฒฝํ—˜์ ์ธ ํŒ ์ค‘ 3๊ฐ€์ง€ ๋‚ด์šฉ๋งŒ ์ •๋ฆฌํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. (๋…ผ๋ฌธ์—์„œ๋Š” ์ด 5๊ฐ€์ง€ ํ…Œํฌ๋‹‰์„ ์ œ์•ˆํ–ˆ์Šต๋‹ˆ๋‹ค) Feature Matching G(Generator)์˜ ๋กœ์Šค ํŽ‘์…˜์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ˆ๋‹ค. ๊ธฐ์กด์— G์˜ ๋กœ์Šค ํŽ‘์…˜์€ ์•„๋ž˜์™€ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์—๋„ ๊ธฐ๋กํ–ˆ์ง€๋งŒ, ์ด ๋กœ์Šค ํŽ‘์…˜์€ "G(Generator)๊ฐ€ D(Discriminator)๋ฅผ ์ž˜๋ชป ํŒ๋‹จํ•˜๊ฒŒ ํ•˜๋ผ"๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ €์ž๋“ค์€ G์˜ ๋กœ์Šค ํŽ‘์…˜์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ด์ง ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ–ˆ์Šต๋‹ˆ๋‹ค. "G์—์„œ ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€์˜ ํ”ผ์ฒ˜์™€ ์ง„์งœ(Real) ์ด๋ฏธ์ง€์˜ ์ž„๋ฐฐ๋”ฉ ํ”ผ์ฒ˜์™€ ์œ ์‚ฌํ•˜๊ฒŒ(๊ฑฐ๋ฆฌ๊ฐ€ ์ ๊ฒŒ) ํ•˜๋ผ" ์ €์ž๋“ค์€ D์˜ ๋กœ์Šค ํŽ‘์…˜ ๋ณ€๊ฒฝ์€ ๋ถˆ์•ˆ์ •ํ•œ GAN์˜ ํ•™์Šต์— ํšจ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ์—ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Minibatch discrimination ๋ชจ๋“œ ๋ถ•๊ดด๊ฐ€ ์ผ์–ด๋‚  ๋•Œ, G๊ฐ€ ํ•˜๋‚˜์˜ D๋ฅผ ์ž˜ ์†์ด๋Š” ๊ฟ€ ์ด๋ฏธ์ง€๋งŒ์„ ์ƒ์„ฑํ•ด์„œ ๋ณด๋‚ด์ค๋‹ˆ๋‹ค. G๊ฐ€ ๊ณ„์†ํ•ด์„œ ๊ฐ™์€ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์ •๋ณด๋ฅผ D์—๊ฒŒ ์ถ”๊ฐ€๋กœ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์œ„์˜ Feature Matching๋ฅผ ์ด์šฉํ•œ ๋„คํŠธ์›Œํฌ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ–ˆ์„ ๋•Œ (G๊ฐ€ ์ƒ์„ฑํ•˜๋Š” ์ด๋ฏธ์ง€์˜ ํ”ผ์ฒ˜ ๋ฒกํ„ฐ = f(x)) Minibatch discrimination์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ”ผ์ฒ˜์˜ ์ •๋ณด๋ฟ ์•„๋‹ˆ๋ผ 1๋ฐฐ์น˜ ์ด๋ฏธ์ง€๋“ค์˜ ์œ ์‚ฌ๋„ ์ •๋ณด O(x)๋„ ํ”ผ์ฒ˜ ๋ฒกํ„ฐ์— concat ํ•˜์—ฌ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Historical Average G์™€ D์˜ ๊ฐ ๋กœ์Šค ํŽ‘์…˜์— ์•„๋ž˜์˜ ์ถ”๊ฐ€์ ์ธ ํŽ˜๋„ํ‹ฐ ํ…€์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. GAN ๋„คํŠธ์›Œํฌ๊ฐ€ ์ˆ˜๋ ดํ•˜์ง€ ์•Š์„ ๋•Œ Loss Space์—์„œ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด Loss๊ฐ€ ๋ฑ…๊ธ€๋ฑ…๊ธ€ ๋Œ๋ฉฐ ๋ฐœ์‚ฐํ•˜๋Š” ํ˜„์ƒ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์— ๊ณผ๊ฑฐ Parameter์˜ Average ๊ฐ’์„ ํŽ˜๋„ํ‹ฐ ํ…€์œผ๋กœ ๋กœ์Šค ํŽ‘์…˜์— ์ถ”๊ฐ€ํ•˜๋ฉด ์ˆ˜๋ ดํ•  ๋•Œ ๋‚˜ํƒ€๋‚˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ธ‰๊ฒฉํ•œ ๋ณ€๋™์„ ๋ง‰์•„ ํ•™์Šต์ด ๊ฑฐ๋“ญ๋ ์ˆ˜๋ก ๋Œํ•‘ ํšจ๊ณผ๋ฅผ ์ฃผ์–ด ์ˆ˜๋ ดํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. (์‚ฌ์‹ค ๋ฌด์Šจ ๋ง์ธ์ง€ ๋ชจ๋ฅด.) Reference GAN โ€” Ways to improve GAN performance (4) ์ƒ์„ฑ ๋ชจ๋ธ์˜ ํ‰๊ฐ€ Generative model์˜ ํ‰๊ฐ€๋Š” Ground truth๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ†ต์ƒ์ ์ธ ๋‹ค๋ฅธ supervised learning ๊ณ„์—ด ๋ฌธ์ œ์— ๋น„ํ•˜๋ฉด ์ƒ๋Œ€์ ์œผ๋กœ ์–ด๋ ค์šด ํŽธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ํ‰๊ฐ€๋ฅผ ํ•ด์•ผ ์„ฑ๋Šฅ์„ ๋น„๊ตํ•ด ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์ฃ ? ๋งŽ์€ ์—ฐ๊ตฌ์ž๋“ค์ด ํ‰๊ฐ€ ์ง€ํ‘œ์˜ ๊ฐœ๋ฐœ์— ๋›ฐ์–ด๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์•„๋งˆ ๊ฐ๊ฐ์˜ metric์„ ์ข€ ๋” ์ฐพ์•„๋ณด์‹œ๋ฉด ๋งค์šฐ ์–ด๋ ต๊ฒŒ ์„ค๋ช…์ด ๋˜์–ด์žˆ์„ ํ…๋ฐ์š”. ์ด ๋ฌธ์„œ์—์„œ๋Š” intuition์„ ์žก๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ๊ฐ€๋ณ๊ฒŒ ์„ค๋ช…์„ ํ•ด๋“œ๋ฆฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. "ํ‰๊ฐ€"๋ผ๋Š” ๊ฒƒ์„ ํ•˜๊ธฐ ์ „์—, ์šฐ๋ฆฌ๊ฐ€ ํ‰๊ฐ€๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” "๋ฌด์—‡"์— ์ดˆ์ ์„ ๋งž์ถฐ์•ผ ํ• ์ง€ ํ•œ๋ฒˆ ์ƒ๊ฐํ•ด ๋ณด์ฃ . Generative model์ด ํ•ด๊ฒฐํ•˜๊ณ ์ž ํ•˜๋Š” task๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€ ์ •๋„๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค : 1) Density estimation, 2) Sampling / Generation , 3) Latent representation learning. ๋‹น์—ฐํ•˜๊ฒŒ๋„ ์šฐ๋ฆฌ๊ฐ€ ํ’€๊ณ ์ž ํ•˜๋Š” task์— ๋”ฐ๋ผ ์–ด๋–ค ๊ฒƒ์„ ์ค‘์ ์— ๋‘๊ณ  ํ‰๊ฐ€๋ฅผ ํ•ด์•ผ ํ• ์ง€๋Š” ๋‹ฌ๋ผ์ง€๊ฒŒ ๋˜๊ฒ ์ฃ ? ์ƒ์„ฑ ๋ชจ๋ธ์ด ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒฐ๊ณผ๋ฌผ์ด ๋‹ฌ๋ผ์งˆ ํ…Œ๋‹ˆ ์–ด์ฐŒ ๋ณด๋ฉด ๋‹น์—ฐํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์•ž์„  ์„ค๋ช…์—์„œ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ Sampling / generation์„ ๊ฐ€์ •ํ•˜๊ณ  ์ดํ•ดํ•ด์™”๊ธฐ์— ์ด ๋ฌธ์„œ์—์„œ๋„ ์ด์— ์ดˆ์ ์„ ๋งž์ถฐ ํ•œ๋ฒˆ ์„ค๋ช…์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Generative model์€ Training set์˜ ๋ถ„ํฌ๋ฅผ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ํŠนํžˆ๋‚˜ Sampling / generation ๋ฌธ์ œ๋ผ๋ฉด training data์— ๊ฑฐ์˜ ๊ทผ์‚ฌํ•˜๋Š” ์ž„์˜์˜ ์ƒ˜ํ”Œ์„ ๋ด…์•„๋‚ด๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๊ฒ ์ง€์š”. ์‚ฌ๋žŒ์€ ๋ฝ‘ํžŒ ์ƒ˜ํ”Œ ์ค‘ ์–ด๋Š ์ชฝ์ด ๋” ์ž˜ ๋‚˜์™”๋Š”์ง€๋ฅผ ์‰ฝ๊ฒŒ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€์ถฉ ๋ดค์„ ๋•Œ ์˜ค๋ฅธ์ชฝ์ด ์ข‹์•„ ๋ณด์ด์ฃ ? ์‚ฌ๋žŒ์ด ์ด๋ฅผ ์–ด๋–ค ๊ธฐ์ค€์„ ๋‘๊ณ  ํŒ๋‹จํ• ๊นŒ์š”? ํ•œ๋ฒˆ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ์šฐ๋ฆฌ๋Š” "์ข‹์€ ์ƒ˜ํ”Œ"์ด๋ผ๊ณ  ๋งํ•  ๋•Œ ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€๊ฐ€ ์–ผ๋งˆ๋‚˜ ์ง„์งœ ๊ฐ™์€์ง€๋ฅผ ๋จผ์ € ๊ณ ๋ คํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์™ผ์ชฝ ์ด๋ฏธ์ง€๊ฐ€ ์ข€ ๋” ํ˜„์‹ค์ ์ธ ์ด๋ฏธ์ง€๋“ค์ด ๋งŽ์ด ๋ณด์ด์ง€์š”? ์ด๋Š” ์ด๋ฏธ์ง€์˜ ํ’ˆ์งˆ(quality) ๊ณผ ์ง๊ฒฐ๋˜๋Š” ๋ฌธ์ œ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋งŒ๋“ค์–ด์ง„ ์ด๋ฏธ์ง€๋“ค์˜ ๋‹ค์–‘์„ฑ(diversity) ์—ญ์‹œ ์ค‘์š”ํ•œ ๊ฐ€์น˜์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. GAN์—์„œ ์•„๋ฌด๋ฆฌ realistic ํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋”๋ผ๋„ Model collapse๊ฐ€ ์ผ์–ด๋‚˜ ๊ฐ™์€ ๊ฒƒ๋งŒ ๊ณ„์† ๋ฝ‘์•„๋‚ธ๋‹ค๋ฉด ์ข‹์€ ๋ชจ๋ธ์ด๋ผ๊ณ  ๋งํ•˜๊ธฐ ํž˜๋“ค๊ฒ ์ฃ . ๊ทธ๋ ‡๋‹ค๋ฉด ํ‰๊ฐ€ ์ง€ํ‘œ ์—ญ์‹œ, ์ด ๋‘˜์„ ๊ณ ๋ คํ•ด์„œ ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ข‹์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Evaluation Sample quality (1) - Inception score Inception score๋Š” generative model ์ค‘ GAN์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ด๊ณ , 2๊ฐ€์ง€ ๊ฐ€์ •์— ๊ธฐ์ดˆํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Inception score ๊ณ„์‚ฐ ์‹œ์— ์ค‘์š”ํ•˜๊ฒŒ ๋ณด๋Š” metric์ธ sharpness์™€ diversity๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‘˜์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ธฐ ์ „์—, Entropy์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด ํ•œ๋ฒˆ ์งš์–ด๋ด…์‹œ๋‹ค. Entropy๋Š” ๋‹ค๋ฅธ ๋ถ„์•ผ์—์„œ ํ†ต์šฉ๋˜๋Š” ์˜๋ฏธ์™€ ๊ฐ™์ด "๋ฌด์งˆ์„œ๋„"๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ c(y|x)์— ๋Œ€ํ•œ ๋ฌด์งˆ์„œ๋„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. entropy๊ฐ€ ๋†’๋‹ค๋Š” ๊ฒƒ์€ ๋žœ๋ค ๋ณ€์ˆ˜ x์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก๋˜๋Š” y์˜ ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ผ๋Š” ๊ฒƒ, ์ฆ‰ y๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ํž˜๋“ค๋‹ค๋Š” ๊ฒƒ์ด๊ณ , entropy๊ฐ€ ๋‚ฎ๋‹ค๋Š” ๊ฒƒ์€ ๋žœ๋ค ๋ณ€์ˆ˜ x์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก๋˜๋Š” y์˜ ์ˆ˜๊ฐ€ ์ ๋‹ค๋Š” ๊ฒƒ, ์ฆ‰ x์— ๋Œ€ํ•œ y๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Sharpness (S) MNIST๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ€์ƒ์˜ ์ˆซ์ž ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  "4"์— ํ•ด๋‹นํ•˜๋Š” ์†๊ธ€์”จ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ, ์ˆซ์ž ์ธ์‹๊ธฐ๊ฐ€ ์ด๋ฅผ 4๋ผ๊ณ  ์ธ์‹ํ•œ๋‹ค๋ฉด ์ข‹์€ ํ€„๋ฆฌํ‹ฐ๋กœ ์ž˜ ๋งŒ๋“ค์–ด์กŒ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์ฃ . ์ฆ‰, classifier๊ฐ€ ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€ x๋ฅผ ๊ฐ€์ง€๊ณ  y๋ผ๊ณ  ์ž˜ classification ํ•˜๋ฉด, ์ด๋ฏธ์ง€ x๋Š” ์ž˜ ๋งŒ๋“  ์ด๋ฏธ์ง€์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. sharpness๊ฐ€ ํฌ๋‹ค๋Š” ๊ฒƒ์€, classifier๊ฐ€ ํ™•์‹ ์„ ๊ฐ€์ง€๊ณ  prediction์„ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์ด๊ณ , ์ด๋Š” Clssifier์˜ predictive distribution ( c(y|x) )๊ฐ€ low entropy๋ฅผ ๊ฐ€์ง์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. = x ( x p [ c ( | ) log c ( | ) y ] ) Diversity (D) ์ข‹์€ quality์˜ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๋งŒํผ ์ค‘์š”ํ•œ ๊ฒƒ์ด, ๋‹ค์–‘ํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋„ฃ์–ด์ค€ ์ƒ˜ํ”Œ๋“ค์˜ ๊ฒฐ๊ด๊ฐ’ ๋“ค์˜ entropy(c(y))๋ฅผ ๋ณด๋ฉด ์ด๊ฒŒ 1๋„ ๋‚˜์˜ค๊ณ  2๋„ ๋‚˜์˜ค๊ณ , 9๋„ ๋‚˜์˜ค๊ณ  ๋‹ค์–‘ํ•˜๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Diversity ๊ณต์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. = x ( E p [ c ( | ) log c ( ) y ] ) ์—ฌ๊ธฐ์„œ c(y)๋Š” marginal distribution์„ ์˜๋ฏธํ•˜๋Š”๋ฐ์š”, ์ƒ์„ฑ๋œ ์˜์ƒ์˜ Diversity๊ฐ€ ํฌ๋‹ค๋Š” ๊ฒƒ์€ marginal distribution์ธ c(y)๊ฐ€ ํฐ entropy๋ฅผ ๊ฐ€์ง์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ( ) E p ( | ) Inception score (IS) Inception score๋Š” ์•ž์„œ ์†Œ๊ฐœ๋œ 2๊ฐ€์ง€ ๊ธฐ์ค€, sharpness์™€ diversity๋ฅผ ๊ณฑํ•ด ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค ์ข‹์•„์•ผ ์ข‹์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒ ์ฃ ? ์ƒ๋‹นํžˆ ์‹ฌํ”Œํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ์‚ฌ๋žŒ์ด ํŒ๋‹จ์„ ๋‚ด๋ฆฌ๋Š” ๊ธฐ์ค€๊ณผ ์ƒ์‘ํ•˜๋Š” ๋ฉด์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋„ ์‚ฌ๋žŒ์ด ํŒ๋‹จ์„ ํ–ˆ์„ ๋•Œ์˜ ๊ฒฐ๊ณผ์™€ correlation์ด ๋†’๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ์— Classifier๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์ด๋ฉด, imageNet์œผ๋กœ pre-training ๋œ Inception model์„ ์ด์šฉํ•ด์„œ ์ด๋ฆ„์ด Inception score๊ฐ€ ๋˜์—ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. S D S Evaluation Sample quality (2) - Frechet Inception Distance (FID) ์•„์ด๋””์–ด ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ์ƒ์„ฑ ๋ชจ๋ธ์ด ๋งŒ๋“ค์–ด๋‚ธ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค๋ฅธ ๋„คํŠธ์›Œํฌ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•œ๋ฒˆ ํ™•์ธํ•ด ๋ณด๋ฉด ์–ด๋–จ๊นŒ? ํ•˜๋Š” ์•„์ด๋””์–ด์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค์–ด๋‚ธ ๋„คํŠธ์›Œํฌ๊ฐ€ A๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด ๋„คํŠธ์›Œํฌ๋Š” ๋ฐ”๋‹ค ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์ƒ์„ฑ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด A๊ฐ€ ๋งŒ๋“ค์–ด๋‚ธ ์ด๋ฏธ์ง€๋ฅผ pre-trained๋œ Inception network์— ๋„ฃ์–ด๋ณด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Inception์—์„œ ๋งŒ๋“ค์–ด๋‚ธ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ๋ฝ‘์•„๋‚ธ feature๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๊ณ , ๋˜ ์‹ค์ œ ๋ฐ”๋‹ค ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด์„œ feature๋ฅผ ๋ฝ‘์„ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€์š”? ์ด ๋งŒ๋“ค์–ด๋‚ธ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ feature์™€, ์‹ค์ œ ๋ฐ”๋‹ค ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ feature๊ฐ€ ๋น„์Šทํ•˜๋‹ค๋ฉด ์ข‹์€ ๋ชจ๋ธ์ด๋ผ๊ณ  ๋งํ•˜๊ณ  ์‹ถ๋‹ค! ํ•˜๋Š” ๊ฒƒ์ด FID์˜ ๊ธฐ๋ณธ ์ฒ ํ•™์ž…๋‹ˆ๋‹ค. Computing FID FID์˜ ๊ณ„์‚ฐ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. I = ฮผ โˆ’ G 2 T ( T ฮฃ โˆ’ ( T G ) / ) * T = test set, G = generated image set ์ˆ˜์‹์€ ๋ณต์žกํ•˜์ง€๋งŒ, ํ•˜๋‚˜์”ฉ ๋œฏ์–ด์„œ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํฌ๊ฒŒ๋Š” ํ‰๊ท ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ธ ์•ž๋ถ€๋ถ„, ๊ณต๋ถ„์‚ฐ(covariance)์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ธ ๋’ท๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์ธ ฮผ โˆ’ G 2 ๋ถ€ํ„ฐ ์‚ดํŽด๋ณด์ฃ . ์˜ˆ๋ฅผ ๋“ค์–ด ์šฐ๋ฆฌ์˜ ์ƒ์„ฑ ๋ชจ๋ธ์ด ์ž์—ฐ์ง€ํ˜•์— ๋Œ€ํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“œ๋Š” ๋ชจ๋ธ์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด ๋ชจ๋ธ์€ ๋ฐ”๋‹ค์— ๋Œ€ํ•ด์„œ๋Š” ์ƒ๋‹นํžˆ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š”๋ฐ, ์‚ฐ๋ฆผ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ์ข‹์€ ์„ฑ๋Šฅ์„ ์•ˆ ๋ณด์ผ์ง€๋„ ๋ชจ๋ฆ…๋‹ˆ๋‹ค. ์šฐ์—ฐํžˆ ์šฐ๋ฆฌ๊ฐ€ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋งŒ ๊ฐ€์ง€๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ํ–ˆ๋Š”๋ฐ ๋ฐ”๋‹ค ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋˜๊ฒŒ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์ธ๋‹ค๊ณ  ํŒ๋‹จํ•œ๋‹ค๋ฉด ์ด๊ฑด ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์ผ๊นŒ์š”? ๋‹น์—ฐํžˆ ์•„๋‹ˆ๊ฒ ์ง€์š”. ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋‹ค์–‘ํ•œ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ๊ฒฐ๊ณผ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋‚˜์™€์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋ด์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ๋งŒ๋“ค์–ด๋‚ธ ์ด๋ฏธ์ง€๋“ค์„ pre-trained ๋œ ๋ชจ๋ธ์— ์ง‘์–ด๋„ฃ์–ด ์ „๋ถ€ feature๋ฅผ ๋ฝ‘์•„๋ณด๋Š”๋ฐ, ์ด ๋งŒ๋“ค์–ด์ง„ feature ๋“ค์— ๋Œ€ํ•œ ํ‰๊ท ๊ฐ’์ด G ์ž…๋‹ˆ๋‹ค. ๋˜ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์‹ค์ œ ์ž์—ฐ์ง€ํ˜•์˜ ์ด๋ฏธ์ง€๋„ ๊ฐ™์€ ๊ณผ์ •์„ ์ง„ํ–‰ํ•ด ์ด๋ฅผ T ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ํ‰๊ท ๊ฐ’์„ ๋น„๊ตํ•œ metric์ด FID์˜ ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์ด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ์ง€๊ฐ€ ์‹ค์ œ ์ด๋ฏธ์ง€์™€ ์–ผ๋งˆ๋‚˜ ๋น„์Šทํ•œ์ง€ (quality)๋ฅผ ์•Œ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์„ ํ†ตํ•ด ์šฐ๋ฆฌ๋Š” quality๋Š” ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ์ง€๋งŒ, diversity๋Š” ์•Œ์•„๋‚ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ถ€๋ถ„์ธ r ( T ฮฃ โˆ’ ( T G ) / ) ๋Š” ์ด์— ์ง‘์ค‘ํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. sample ์ด ๋งŒ๋“ค์–ด๋‚ด๋Š” feature๋“ค ๊ฐ„์˜ statistics , ์ด๋ฅผํ…Œ๋ฉด Covariance ๊ฐ™์€ ๋‹ค๋ฅธ ์„ฑ๋ถ„๋“ค๋„ ๋น„์Šทํ•œ ์„ฑ์งˆ์„ ๊ฐ€์กŒ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. FID๋Š” ์ž‘์œผ๋ฉด ์ž‘์„์ˆ˜๋ก, test์™€ generated image ์‚ฌ์ด์˜ ์ฐจ์ด๊ฐ€ ์ž‘๋‹ค๋Š” ์˜๋ฏธ์ด๊ธฐ์— ์ข‹์€ ๋ชจ๋ธ์ž„์„ ๋งํ•ฉ๋‹ˆ๋‹ค. Evaluation Sample quality (3) - Kernel Inception Distance (KID) ์•„์ด๋””์–ด KID๋Š” MMD(Maximum Mean Discrepancy)๋ฅผ feature space์—์„œ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ๋Š” ์‹ค์ œ ์ด๋ฏธ์ง€์™€ ๊ฐ€์งœ ์ด๋ฏธ์ง€, ์‹ค์ œ ์ด๋ฏธ์ง€์™€ ๊ฐ€์งœ ์ด๋ฏธ์ง€์˜ ์„ธํŠธ ๊ฐ„์˜ similarity๋ฅผ ๋ณด๋Š” ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Computing KID KID๋Š” FID์™€ ์ƒ๋‹นํžˆ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ์ด๋ฏธ์ง€ ์…‹ p, ๊ฐ€์งœ ์ด๋ฏธ์ง€ ์…‹์„ q๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. p์—์„œ ๊ทธ๋ฆผ 2์žฅ์„ ๋ฝ‘๊ณ  ๋‘ ์ด๋ฏธ์ง€ ๊ฐ„์˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด ์ผ์„ ๊ณ„์† ๋ฐ˜๋ณตํ•˜์—ฌ ์ด๋ฏธ์ง€๋“ค ๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€์— ๋Œ€ํ•œ ๊ธฐ๋Œ“๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๋˜‘๊ฐ™์€ ์ผ์„ q์— ๋Œ€ํ•ด์„œ๋„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•˜๋‚˜๋Š” p์—์„œ, ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” q์—์„œ ๋ฝ‘์€ ๊ฐ’์œผ๋กœ ๋˜ ํ‰๊ท ์ ์ธ ์ฐจ์ด๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์‹์€ ์ง„์งœ ์ด๋ฏธ์ง€ p๋งŒ ๊ฐ€์ง€๊ณ  ๊ตฌํ•œ ๊ฐ’ + ๊ฐ€์งœ ์ด๋ฏธ์ง€ q๋งŒ ๊ฐ€์ง€๊ณ  ๊ตฌํ•œ ๊ฐ’ - 2 * ์ง„์งœ ์ด๋ฏธ์ง€์™€ ๊ฐ€์งœ ์ด๋ฏธ์ง€ ํ•˜๋‚˜์”ฉ์„ ๊ฐ€์ง€๊ณ  ๊ตฌํ•œ ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. M ( , ) E, โ€ฒ p [ ( , โ€ฒ ) ] E, โ€ฒ q [ ( , โ€ฒ ) ] 2 x p x q [ FID์™€์˜ ๋น„๊ต FID๋Š” biased ๋  ์ˆ˜ ์žˆ์ง€๋งŒ, KID๋Š” unbiased ํ•ฉ๋‹ˆ๋‹ค. FID๋Š” O(n) ์•ˆ์— evaluation์ด ๋  ์ˆ˜ ์žˆ์ง€๋งŒ, KID๋Š” O(n^2) ์ •๋„์˜ time complexity๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. Evaluating Latent Representation Reference ์› ๋…ผ๋ฌธ Inception score : https://arxiv.org/abs/1606.03498 FID : https://arxiv.org/pdf/1706.08500.pdf KID : https://arxiv.org/abs/1801.01401 ๋ธ”๋กœ๊ทธ A simple explanation of the Inception Score (David mack, 2019) : https://medium.com/octavian-ai/a-simple-explanation-of-the-inception-score-372dff6a8c7a ์ฃผ ์ฐธ๊ณ  ์ž๋ฃŒ : CS 6785 lecture 13 slide (Cornell university, Volodymyr Kuleshov) https://canvas.cornell.edu/courses/27332/assignments/syllabus Appendix A. ํ”ผ์ฒ˜ ์‹œ๊ฐํ™”์™€ ๋”ฅ ๋“œ๋ฆผ(โ˜…์ž‘์„ฑ ์ค‘) ... Appendix B. Multimodal NN multi modal ์ด๋ž€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ˜•ํƒœ์˜ ์ •๋ณด๋ฅผ ํ†ตํ•ด ์†Œํ†ตํ•˜๋Š” ํ™˜๊ฒฝ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ modality๋Š” ๋ฐ์ดํ„ฐ์˜ ์ข…๋ฅ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ์„ธ๊ณ„์—์„œ๋Š” ๋งŽ์€ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋“ค์ด ์ง‘ํ•ฉ๋œ ํ˜•ํƒœ๋กœ ์šฐ๋ฆฌ๋Š” ์ •๋ณด๋ฅผ ์ ‘ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ธ์Šคํƒ€๊ทธ๋žจ์„ ๋ณผ ๋•Œ ์šฐ๋ฆฌ๋Š”, ์นœ๊ตฌ๊ฐ€ ์˜ฌ๋ฆฐ ์˜์ƒ์„ ๋ณด๊ณ , ์˜์ƒ์— ์žˆ๋Š” ์†Œ๋ฆฌ๋ฅผ ๋“ฃ๊ณ , ๊ฒŒ์‹œ๋ฌผ์— ํƒœ๊ทธ ๋œ ๋‹จ์–ด๋“ค์„ ๋ณด๊ณ , ์นœ๊ตฌ๊ฐ€ ์˜์ƒ์— ๋Œ€ํ•ด ์“ด ์„ค๋ช…๋„ ๋ด…๋‹ˆ๋‹ค. ์œ ํŠœ๋ธŒ์—์„œ ์˜์ƒ์„ ๋ณผ ๋•Œ๋„, ์˜์ƒ, ์†Œ๋ฆฌ, ์ž๋ง‰, ๋Œ“๊ธ€, ์˜์ƒ์— ๋Œ€ํ•œ ์„ค๋ช… ๋“ฑ ๊ต‰์žฅํžˆ ๋งŽ์€ ์ •๋ณด๋ฅผ ํ•œ ๋ฒˆ์— ๋ฐ›์•„๋“ค์ž…๋‹ˆ๋‹ค. Multimodal learning์ด ํ•„์š”ํ•œ ์ด์œ  ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๋Š”, ํ•œ ๊ฐ€์ง€์˜ ์ •๋ณด๋งŒ์„ ์ด์šฉํ•˜๋Š” ๋ชจ๋ธ์„ ๋ฐฐ์›Œ์™”์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ์‹ค์ œ ์‚ฌ๋žŒ์€ ๊ฐ๊ฐ๊ธฐ๊ด€์„ ํ†ตํ•ด ๋“ค์–ด์˜ค๋Š” ์ •๋ณด๋ฅผ ๋ณตํ•ฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์„ธ์ƒ์„ ์ธ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜ ์ •์ง€ ํ‘œ์‹œ๋ฅผ ๋ณด๋ฉด ์šฐ๋ฆฌ๋Š” ๋นจ๊ฐ„์ƒ‰์ด๋ผ๋Š” ์ƒ‰๊น” ์ •๋ณด, 'STOP'์ด๋ผ๋Š” context ์ •๋ณด, ์†๋ฐ”๋‹ฅ์ด๋ผ๋Š” ์ด๋ฏธ์ง€ ๋“ฑ ์—ฌ๋Ÿฌ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฐ ํ‘๋ฐฑ์œผ๋กœ ๋œ ํ‘œ์ง€ํŒ์„ ๋ด๋„ ์ด๊ฒƒ์ด ์ •์ง€ ํ‘œ์‹œ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ , ์‹ฌ์ง€์–ด๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋ชจ๋ฅด๋Š” ์–ธ์–ด๋กœ ์ ํ˜€์žˆ์–ด๋„, ์œ„ํ—˜ํ•œ sign์ด๋ผ๋Š” ๊ฒƒ์„ ์ง์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ๊ฐ€์ง€์˜ modality๋งŒ ๊ฐ€์ง€๊ณ  ์ •๋ณด๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์—ฌ๋Ÿฌ ๊ฐ€์ง€ modality๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ๋” ์ •ํ™•ํ•œ ํŒŒ์•…์— ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ modality๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ํ•„์ˆ˜์ ์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์ƒํ™ฉ์—์„œ ์ผ๋ณธ์–ด๋ฅผ ๋ชจ๋ฅด๋Š” ์‚ฌ๋žŒ์ด ๋ถ‰์€์ƒ‰ ํ‘œ์‹œ ์—†์ด 'ๆญขใพใ‚Œ'๋ผ๊ณ ๋งŒ ์“ฐ์—ฌ์žˆ๋Š” ํ‘œ์ง€ํŒ์„ ๋ดค๋‹ค๋ฉด ์ •์ง€ ํ‘œ์‹œ์ธ์ง€ ์ „ํ˜€ ๋ชฐ๋ž์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. Multimodal learning์˜ ์ „๋žต multimodal elarning์˜ ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ์ ์€ modality๋งˆ๋‹ค data์˜ representation์ด ๋‹ค๋ฅด๋ฉฐ, data๊ฐ€ ์ƒ๋‹นํžˆ noisy ํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋ณดํ†ต์€ ํ•˜๋‚˜์˜ modality์— ๋Œ€ํ•ด pre-training์„ ์‹œํ‚ค๊ณ  embedding์„ ํ†ตํ•ด ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ multimodal learning์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. Google Open Images Open Images๋Š” 2016๋…„ ์ฒ˜์Œ ๊ณต๊ฐœ๋˜์—ˆ๊ณ  2020๋…„ Version 6๊ฐ€ ๋‚˜์˜จ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹์€ 59.9M ๊ฐœ์˜ ์ด๋ฏธ์ง€, 20000๊ฐœ์˜ Category๋ฅผ ๋ณด์œ ํ•œ ๋ฐ์ดํ„ฐ ์…‹์ด๋ฉฐ, ๊ธฐ์กด ๋ฐ์ดํ„ฐ image localization๋ฟ๋งŒ ์•„๋‹ˆ๋ผ segmentation, image captioning ๋“ฑ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐฉ๋Œ€ํ•œ ์ฃผ์„์ด ๋‹ฌ๋ ค์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ง•์ ์ธ ๊ฒƒ์€ localized narratives๋ผ๋Š” ์ƒˆ๋กœ์šด ํ˜•ํƒœ์˜ multi-modal annotation์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด object detection, segmentation์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ๋ผ๋ฒจ๋ง ๋˜์–ด์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์‚ฌ๋žŒ์˜ ๋ชฉ์†Œ๋ฆฌ๋กœ ๋œ ํ•ด์„ค, ํ…์ŠคํŠธ๋กœ ๋œ ํ•ด์„ค(caption)์ด ๋ง๋ถ™์—ฌ์ ธ ์žˆ์œผ๋ฉฐ ํŠน์ดํ•˜๊ฒŒ ๋งˆ์šฐ์Šค ์ปค์„œ๋„ ํ•˜๋‚˜์˜ annotation์œผ๋กœ ๋“ค์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 500k ์ด์ƒ์˜ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ํ•ด๋‹น ์ฃผ์„์ด ๋‹ฌ๋ ค์žˆ๋Š” ๋ฐฉ๋Œ€ํ•œ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์‚ฌ๋žŒ์€ ๋ฏธ๋ฆฌ ์ž‘์„ฑ๋œ image caption์„ ์ฝ์œผ๋ฉฐ ์ด๋ฏธ์ง€๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๋™์‹œ์— ์ง์ ‘ ์ด๋ฏธ์ง€ ์œ„์— ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ ์›€์ง์ด๋ฉด์„œ ์„ค๋ช…ํ•˜๋„๋ก ํ•˜์—ฌ, ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋ฅผ ์Œ์„ฑ+๋งˆ์šฐ์Šค ์ปค์„œ + image caption์ด ๋™์‹œ์— ์„œ์ˆ ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ ๋ผ๋ฒจ๋ง ๋ฐ์ดํ„ฐ๋กœ ์“ฐ๋Š” ๋ฐ์ดํ„ฐ ์…‹์€ ์•„๋งˆ ์ฒ˜์Œ์ธ ๊ฒƒ ๊ฐ™์€๋ฐ์š”, ๊ตฌ๊ธ€์€ ์ด ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ๋žŒ๋“ค์ด ์–ด๋–ป๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์ธ์‹ํ•˜๊ณ  ์„ค๋ช…ํ•˜๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ์ข‹์€ ์ž๋ฃŒ๋ผ๊ณ  ๋งํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. open images๋Š” ๊ธฐ์กด image caption ๋ฐ์ดํ„ฐ์—์„œ ์•„์‰ฌ์šด ์ ์„ ๋ณด๊ฐ•ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์šฐ์„  ๊ฐ์ฒด ๊ฐ„ ๊ด€๊ณ„๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋ง์„ ๋‹ค์ˆ˜ ํฌํ•จํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ โ€œman riding a skateboardโ€, โ€œman and woman holding handsโ€, and โ€œdog catching a flying diskโ€.์™€ ๊ฐ™์€ ๋ฌธ์žฅ์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ "Person is jumping and smiling." "Person is laying down." ๊ณผ๊ฐ™์ด ์‚ฌ๋žŒ์˜ ํ–‰๋™์„ ์„ค๋ช…ํ•˜๋Š” ๋ง์„ ๋‹ค์ˆ˜ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Reference Google Open Images<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ๊ณตํ•™์ž๋ฅผ ์œ„ํ•œ PySide2 ### ๋ณธ๋ฌธ: ์ด ์ฑ…์€ ๊ณตํ•™์ž๋ฅผ ์œ„ํ•œ Python์˜ ์ž๋งค ์ฑ…์ด๋‹ค. History 2019.5.13 : 1์ฐจ ํŽธ์ง‘ ์™„๋ฃŒ 0. ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ์ด ์ฑ…์€ ์ด ์ฑ…์€ ๊ณตํ•™์ž๋ฅผ ์œ„ํ•œ Python์˜ ์ž๋งค ์ฑ…์ด๋‹ค. Qt์˜ ๊ณต์‹ Python ๋ฐ”์ธ๋”ฉ์ธ PySide2์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•์„ ๊ธฐ์ˆ ํ•˜๊ณ  ์žˆ๋‹ค(QML์€ ์ œ์™ธ) Windows ํ™˜๊ฒฝ์—์„œ Anaconda ๋ฐฐํฌ๋ณธ์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค(Anaconda ์„ค์น˜๋Š” ๊ณตํ•™์ž๋ฅผ ์œ„ํ•œ Python ์ฐธ์กฐ) PySide2 Python์—์„œ GUI๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” Qt์˜ Python ๋ฐ”์ธ๋”ฉ์ธ PyQt๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. PyQt๋Š” Riverbank Computing์ด๋ผ๋Š” ํšŒ์‚ฌ์—์„œ ์ œ์ž‘ํ•œ ๊ฒƒ์œผ๋กœ, GPL์™€ ์ƒ์šฉ ๋ผ์ด์„ ์Šค๋กœ ์ œ๊ณต๋œ๋‹ค. PyQt4๋Š” Qt4๋ฅผ PyQt5๋Š” Qt5๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•œ๋‹ค. LGPL ๋ผ์ด์„ ์Šค๊ฐ€ ์—†๋‹ค๋Š” ์ ์ด ์•„์‰ฌ์šด ์ ์ด๋‹ค. ํ•œํŽธ Qt์˜ ์ œ์ž‘์‚ฌ๋Š” Qt4๋ฅผ ๋Œ€์ƒ์œผ๋กœ PySide๋ผ๋Š” Python ๋ฐ”์ธ๋”ฉ์„ ์ œ๊ณตํ•˜์˜€์ง€๋งŒ, Qt5์— ๋Œ€ํ•ด์„œ๋Š” ์ œ๊ณตํ•˜์ง€ ์•Š๋‹ค๊ฐ€ ์ตœ๊ทผ PySide2๋ผ๋Š” Qt5 ์šฉ Python ๋ฐ”์ธ๋”ฉ์„ ์ œ๊ณตํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. Qt์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ GPL, LGPL, ์ƒ์šฉ ๋ผ์ด์„ ์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. PyQt5์™€ PySide2์˜ API๋Š” ๋งค์šฐ ๋น„์Šทํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ฝ๊ฐ„์˜ ์ˆ˜์ •๋งŒ์œผ๋กœ PyQt5 ์†Œ์Šค๋ฅผ PySide2์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. PyQt5 : Riverbank Computing ์ œ๊ณต, ์ธํ„ฐ๋„ท์—์„œ ์ถฉ๋ถ„ํžˆ ๋งŽ์€ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด ์žฅ์  PySide2 : Qt for Python ํ”„๋กœ์ ํŠธ(Qt ์ œ์ž‘์‚ฌ ๊ณต์‹ Python ๋ฐฐํฌ๋ณธ). LGPL ๋ผ์ด์„ ์Šค๊ฐ€ ์žˆ๋‹ค๋Š” ์ ์ด ํŠน์ง•. ์ฃผ์š” ์ฐธ๊ณ  ์ž๋ฃŒ Qt for Python : PySide2 ์‚ฌ์ดํŠธ Qt Documentation : Qt ์˜จ๋ผ์ธ ํ—ฌํ”„ PyQt : PyQt ์‚ฌ์ดํŠธ PyQt5 Tutorial : ํŒŒ์ด์ฌ์œผ๋กœ ๋งŒ๋“œ๋Š” ๋‚˜๋งŒ์˜ GUI ํ”„๋กœ๊ทธ๋žจ : PyQt5 ๋Œ€์ƒ PyQT5 - Python Tutorial Rapid GUI Programming with Python and Qt : PyQt4๋กœ ์ž‘์„ฑ๋˜์—ˆ์ง€๋งŒ ์„ค๋ช…์ด ์ž˜ ๋˜์–ด ์žˆ๋‹ค. ์˜ˆ์ œ ์†Œ์Šค๋ฅผ PyQt5๋กœ ํฌํŒ… ํ•œ ๊ฒƒ์„ ์ธํ„ฐ๋„ท์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. PySide2 ์„ค์น˜ Anaconda ๋ฐฐํฌ๋ณธ์—๋Š” PyQt5๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. PySide2๋Š” pip๋กœ ์„ค์น˜ํ•œ๋‹ค. > pip install PySide2 PySide2์˜ Qt ๋ฒ„์ „ ํ™•์ธ >>> import PySide2 >>> PySide2.__version__ '5.12.2' >>> import PySide2.QtCore >>> PySide2.QtCore.__version__ '5.12.2' >>> PySide2.QtCore.qVersion() '5.12.2' __version__์€ PySide2์˜ ๋ฒ„์ „์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๊ณ  QtCore::qVersion()์€ QtCore DLL ๋ชจ๋“ˆ ์ˆ˜์ค€์—์„œ์˜ ๋ฒ„์ „์ด๋‹ค. ์ด ๋‘˜์€ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค. PySide2์˜ Qt ๋ฒ„์ „์€ 5.12.2 ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. PyQt5์˜ Qt ๋ฒ„์ „ ํ™•์ธ import PyQt5.QtCore PyQt5.QtCore.qVersion() Out[4]: '5.9.7' PyQt5๋Š” ๋ณ„๋„์˜ version ๋ฉค๋ฒ„๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค. QtCore::qVersion()๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค. PyQt5์˜ Qt ๋ฒ„์ „์€ 5.9.7 ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์„ค์น˜ ๊ฒฝ๋กœ ํŠนํžˆ Anaconda ๋ฐฐํฌ๋ณธ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” PyQt5์™€ ๊ฒฝ๋กœ๊ฐ€ ์ค‘๋ณต๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. PyQt5 : PyQt5 ์„ค์น˜ ๊ฒฝ๋กœ : C:\ProgramData\Anaconda3\Lib\site-packages\PyQt5 PyQt5๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” Qt DLL ๊ฒฝ๋กœ : C:\ProgramData\Anaconda3\Library\bin PyQt5๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” plugin ๊ฒฝ๋กœ : C:\ProgramData\Anaconda3\Library\plugins PySide2 PySide2 ์„ค์น˜ ๊ฒฝ๋กœ: C:\ProgramData\Anaconda3\Lib\site-packages\PySide2 PySide2๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” Qt DLL ๊ฒฝ๋กœ : C:\ProgramData\Anaconda3\Lib\site-packages\PySide2 PySide2๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” plugin ๊ฒฝ๋กœ : C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\plugins PySide2์˜ ์˜ˆ์ œ ํŒŒ์ผ์€ ๋‹ค์Œ ๊ฒฝ๋กœ์— ์žˆ๋‹ค. * C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\examples HelloQt ๋‹ค์Œ์€ PyQt5์™€ PySide2๋ฅผ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ด๋‹ค. (HelloQt ์‹คํ–‰ ์ „๊ฒฝ) HelloQtPyQt5.py (PyQt5 ์˜ˆ์ œ) from PyQt5.QtWidgets import QApplication, QWidget, QLabel import sys if __name__ == '__main__': app = QApplication(sys.argv) window = QWidget() window.resize(289,170) window.setWindowTitle("FIrst Qt Program") label = QLabel('Hello Qt',window) label.move(110,80) window.show() app.exec_() HelloQt.py (PySide2 ์˜ˆ์ œ) if __name__ == '__main__': from PySide2.QtCore import QCoreApplication QCoreApplication.setLibraryPaths(['C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\plugins']) from PySide2.QtWidgets import QApplication, QWidget, QLabel import sys if __name__ == '__main__': app = QApplication(sys.argv) window = QWidget() window.resize(289,170) window.setWindowTitle("FIrst Qt Program") label = QLabel('Hello Qt',window) label.move(110,80) window.show() app.exec_() PySide2๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํŒจํ‚ค์ง€ ๋ช…์นญ PyQt5๊ฐ€ PySide2๋กœ ๋ณ€๊ฒฝ๋˜๊ณ , ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒจ์Šค๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š” ๊ฒƒ ์ด์™ธ์—๋Š” ์™„์ „ํžˆ ๋™์ผํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. PyQt5๋‚˜ PySide2๋ฅผ ๊ตฌ๋™ํ•˜๋ ค๋ฉด ์˜ฌ๋ฐ”๋ฅธ plugins ๊ฒฝ๋กœ๊ฐ€ ์ง€์ •๋˜์–ด์•ผ ํ•œ๋‹ค. PyQt5๋Š” Anaconda์— ์„ค์น˜๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๊ณ ๋ คํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ํ•˜์ง€๋งŒ ์ถ”๊ฐ€๋กœ ์ธ์Šคํ†จ ํ•œ PySide2๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด setLibraryPaths(..)๋กœ ์ฝ”๋“œ์—์„œ ์ ์šฉํ•˜๊ฑฐ๋‚˜, ํ™˜๊ฒฝ ๋ณ€์ˆ˜ QT_PLUGIN_PATH๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ฝ”๋“œ์—์„œ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• if __name__ == "__main__": from PySide2.QtCore import QCoreApplication QCoreApplication.setLibraryPaths(['C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\plugins']) ... ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• set QT_PLUGIN_PATH=C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\plugins PySide2์™€ PyQt5์˜ ์ฐจ์ด์  ๋™์‹œ์— PySide2์™€ PyQt5๊ฐ€ ์„ค์น˜ ์‹œ ์‹คํ–‰ํ™˜๊ฒฝ์— ์ฃผ์˜ PySide2๋Š” LGPL์„ ์ง€์›ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ API๊ฐ€ ๋งค์šฐ ์œ ์‚ฌํ•˜๋‹ค. ๋‹จ์ง€ ์ตœ์ƒ์œ„ ๋ชจ๋“ˆ ๋ช…์ธ PySide2์™€ PyQt5๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ์ž‘๋™ํ•˜์ง€๋งŒ ๋‹ค์Œ์— ์œ ์˜ํ•œ๋‹ค. PySide2๋Š” Qt์˜ QString์„ ์ œ๊ณตํ•˜์ง€ ์•Š๊ณ  ํŒŒ์ด์ฌ ๋ฌธ์ž์—ด(str๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. PySide2๋Š” QVariant๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค. 1. ๊ฐœ๋… ์žก๊ธฐ HelloQt ํ”„๋กœ๊ทธ๋žจ ๋ถ„์„ ์•ž์„œ์—์„œ ์ œ์‹œํ–ˆ๋˜ HelloQt ํ”„๋กœ๊ทธ๋žจ์„ ๋ถ„์„ํ•˜๊ณ  ์ด๋ฅผ ํ†ตํ•ด Qt ํ”„๋กœ๊ทธ๋žจ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ, ์œ„์ ฏ, ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. HelloQt.py from PySide2.QtWidgets import * import sys if __name__ == '__main__': app = QApplication(sys.argv) window = QWidget() window.resize(289,170) window.setWindowTitle("FIrst Qt Program") label = QLabel('Hello Qt',window) label.move(110,80) window.show() app.exec_() from PySide2.QtWidgets import *๋Š” QtWidgets ๋ชจ๋“ˆ์˜ ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ ์ž„ํฌํŠธ ํ•œ๋‹ค. Qt๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ชจ๋“ˆ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ณ , ๊ทธ ํ•˜์œ„์— QWidget, QLabel ๋“ฑ ๋‹ค์–‘ํ•œ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด * ๋Œ€์‹  ๊ฐœ๋ณ„ ํด๋ž˜์Šค๋ฅผ ์ž„ํฌํŠธ ํ•ด๋„ ๋˜๋ฉฐ, ์ด ๊ฒฝ์šฐ IDE์—์„œ code completion์ด ์ž‘๋™ํ•œ๋‹ค. from PySide2.QtWidgets import QApplication, QWidget, QLabel if __name__ == '__main__': ๋ธ”๋ก์€ C++ ์ฝ”๋“œ์˜ main() ํ•จ์ˆ˜์— ๋Œ€์‘ํ•œ๋‹ค. ๋จผ์ € QApplication ๊ฐ์ฒด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋Œ€ํ‘œํ•˜๋Š” ๊ฐ์ฒด์ด๋‹ค. QWidget ๊ฐ์ฒด๋กœ ์ตœ์ƒ์œ„ ์ฐฝ์„ ๋งŒ๋“ค๊ณ  ๊ทธ ์ฐฝ ๋‚ด๋ถ€์— ์ž์‹ ์ฐฝ์ธ QLabel์„ ๋งŒ๋“ ๋‹ค. ์ตœ์ƒ์œ„ ์ฐฝ์— ๋Œ€ํ•œ show() ๋ฉค๋ฒ„๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ QApplication ๊ฐ์ฒด์ด exec_() ๋ฉค๋ฒ„๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฉ”์‹œ์ง€ ๋ฃจํ”„๋ฅผ ๊ฐ€๋™ํ•˜๊ฒŒ ๋œ๋‹ค. QWidget์ธ QLabel ๋“ฑ์€ ์œ„์ ฏ ๊ฐ์ฒด์ด๋‹ค. ์œ„์ ฏ์€ ํ™”๋ฉด์— ๋ณด์ด๋Š” ์ฐฝ์„ ์˜๋ฏธํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ์ž ์ž…๋ ฅ์— ๋ฐ˜์‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“  ์œ„์ ฏ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋Š” QWidget์ด๋‹ค. QLabel์€ QWidget์„ ์ƒ์†๋ฐ›์•„ ๋งŒ๋“  ํ™”๋ฉด์— ๊ธ€์ž๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ์œ„์ ฏ์ด๋‹ค. QWidget์€ ๋ชจ๋“  ์œ„์ ฏ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค์ด๊ธฐ๋„ ํ•œ ๋™์‹œ์— ๊ทธ ์ž์ฒด๋กœ ๋‹ค๋ฅธ ์œ„์ ฏ์„ ์ž์‹ ์ฐฝ์œผ๋กœ ๋‹ด๋Š” ๊ธฐ๋Šฅ(์ปจํ…Œ์ด๋„ˆ ์œ„์ ฏ)์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. Qt์—์„œ ์œ„์ ฏ์€ ๊ฐ์ฒด ๊ฐ„์˜ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. ์ตœ์ƒ์œ„ ์ฐฝ์€ ๋ถ€๋ชจ ์œ„์ ฏ์ด ์—†์ง€๋งŒ, ์ž์‹ ์œ„์ ฏ์€ ๋ถ€๋ชจ ์œ„์ ฏ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์œ„์—์„œ QLabel์€ window ๊ฐ์ฒด๋ฅผ ๋ถ€๋ชจ ์œ„์ ฏ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค(label = QLabel('Hello Qt',window)์—์„œ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋ถ€๋ชจ ์œ„์ ฏ ๊ฐ์ฒด๋ฅผ ์ง€์ •ํ•œ ๊ฒƒ์ด๋‹ค). ๊ฐ์ฒด ๊ฐ„์˜ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๊ฐ€ ์„ค์ •๋˜๋ฉด ๋ถ€๋ชจ ๊ฐ์ฒด๊ฐ€ ์‚ญ์ œ๋  ๋•Œ ์ž์‹ ๊ฐ์ฒด๊นŒ์ง€ ์‚ญ์ œ๋˜๊ฒŒ ๋œ๋‹ค. ์ฐฝ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ณด์ด๊ธฐ ์•ˆ ๋ณด๊ธฐ์ด๊ฐ€ ์„ค์ • ์—ญ์‹œ ๋ณ„๋„์ด ์„ค์ •์ด ์—†๋Š” ๊ฒฝ์šฐ ๋ถ€๋ชจ๊ฐ€ ๋‹ด๋‹นํ•œ๋‹ค. ์ฆ‰ ์œ„ ์˜ˆ์—์„œ window๊ฐ€ ์‚ญ์ œ๋  ๋•Œ label๋„ ๊ฐ™์ด ์‚ญ์ œ๋˜๋ฉฐ, window๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜๊ฑฐ๋‚˜ ์•ˆ ๋ณด๊ฒŒ ๋  ๋•Œ label๋„ ๊ฐ™์ด ๋‚˜ํƒ€๋‚˜๊ณ  ์•ˆ ๋ณด์ด๊ฒŒ ๋œ๋‹ค. 0. ๋“ค์–ด๊ฐ€๊ธฐ ์ „์—์—์„œ ์ œ์‹œํ•œ QT_PLUGIN_PATH ์„ค์ •์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋˜์–ด์•ผ ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.(์ฝ”๋“œ์—์„œ ๋ฐ˜์˜ ๋˜๋Š” QT_PLUGIN_PATH ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •). ์ดํ›„ ์ฝ”๋“œ ๋ชจ๋‘ ๋™์ผ Logon1 ์˜ˆ์ œ Logon1 ์˜ˆ์ œ๋Š” ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์•„์ด๋””์™€ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ ˆ์ด์•„์›ƒ, ๋ฆฌ์†Œ์Šค, ์‹œ๊ทธ๋„-์Šฌ๋กฏ, ์—ด๊ฑฐ ๋ณ€์ˆ˜์˜ ๊ฐœ๋…์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. Logon1 ์˜ˆ์ œ๋Š” Logon.qrc๋ผ๋Š” ๋ฆฌ์†Œ์Šค ํŒŒ์ผ๊ณผ Logon1.py๋ผ๋Š” ์†Œ์Šค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. ๋ฆฌ์†Œ์Šค ํŒŒ์ผ์ธ Logon.qrc๋Š” xml ํŒŒ์ผ ํฌ๋งท์ด๋ฉฐ ์—ฌ๊ธฐ์—์„œ ์‚ฌ์šฉํ•œ ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Logon.qrc <RCC> <qresource prefix="/"> <file>images/ok.png</file> </qresource> </RCC> ๋ฒ„ํ„ด ์ด๋ฏธ์ง€ ok.png๋Š” Logon.qrc์ด ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ํ•˜์œ„์˜ images์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค .qrc ๋ฆฌ์†Œ์Šค ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด pyside2-rcc ์œ ํ‹ธ๋Ÿฌํ‹ฐ๋กœ ํŒŒ์ด์ฌ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•ด ์ค€๋‹ค. > pyside2-rcc -o Logon_rc.py -py3 Logon.qrc ์ด์ œ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ ์›๋ณธ ๋Œ€์‹  ๋ณ€ํ™˜๋œ ํŒŒ์ด์ฌ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ์„ ์ž„ํฌํŠธ ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค. ์•„๋ž˜๋Š” ์†Œ์Šค ํŒŒ์ผ์ด๋‹ค. Logon1.py import sys from PySide2.QtWidgets import (QApplication, QWidget, QLabel, QLineEdit, QGridLayout, QPushButton, QHBoxLayout, QVBoxLayout) from PySide2.QtGui import QIcon from PySide2.QtCore import Qt #from PySide2.QtCore import SIGNAL, SLOT, QObject import Logon_rc if __name__ == '__main__': app = QApplication(sys.argv) logon = QWidget() labelId = QLabel('&Id :') labelId.setAlignment(Qt.AlignRight | Qt.AlignVCenter) labelPW = QLabel('&Password:') lineEditId = QLineEdit() lineEditPW = QLineEdit() lineEditPW.setEchoMode(QLineEdit.Password) labelId.setBuddy(lineEditId) labelPW.setBuddy(lineEditPW) buttonOk = QPushButton("&Ok") buttonOk.setIcon(QIcon(":/ok.png")) layout1 = QGridLayout() layout1.addWidget(labelId, 0,0) layout1.addWidget(lineEditId, 0,1); layout1.addWidget(labelPW, 1,0) layout1.addWidget(lineEditPW, 1,1) layout2 = QHBoxLayout() layout2.addStretch() layout2.addWidget(buttonOk) mainLayout = QVBoxLayout() mainLayout.addLayout(layout1) mainLayout.addLayout(layout2) logon.setLayout(mainLayout) logon.setWindowTitle('Log on') logon.setWindowIcon(QIcon(":/images/ok.png")) # buttonOk.clicked.connect(app.quit) logon.show() app.exec_() ์ฝ”๋“œ์—์„œ import Logon_rc๋Š”. qrc ํŒŒ์ผ์„ ํŒŒ์ด์ฌ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•œ ํŒŒ์ผ์„ ์ž„ํฌํŠธ ํ•œ ๊ฒƒ์ด๋‹ค. QIcon(":/images/ok.png") ๋“ฑ๊ณผ ๊ฐ™์ด ๋ฆฌ์†Œ์Šค๋ฅผ ์˜๋ฏธํ•˜๋Š” : ๊ธฐํ˜ธ์™€ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜์—ฌ ์•„์ด์ฝ˜์˜ ์ด๋ฏธ์ง€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. QWidget์„ ์ปจํ…Œ์ด๋„ˆ๋กœ ์‚ฌ์šฉํ•  ๋•Œ ์ž์‹ ์œ„์ ฏ์˜ ์œ„์น˜๋ฅผ ์ง์ ‘ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์€ ์ƒ๋‹นํ•œ ์–ด๋ ค์šธ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์œ„์ ฏ์˜ ํฌ๊ธฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ํ…์ŠคํŠธ๊ฐ€ ์ž˜๋ ค๋‚˜๊ฐ€๋Š” ๋“ฑ์˜ ๋ฌธ์ œ์ ์ด ๋ฐœ์ƒํ•œ๋‹ค. Qt๋Š” ๋ ˆ์ด์•„์›ƒ(layout) ํด๋ž˜์Šค๋ฅผ ๋„์ž…ํ•ด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ  ์žˆ๋‹ค. ์˜ˆ์ œ์—์„œ๋Š” QGridLayout, QHBoxLayout, QVBoxLayout์ด๋ผ๋Š” ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๋ฅผ ์ ์šฉํ•˜์˜€๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜๋ฉฐ ์œˆ๋„ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ๋„ ์ ์ ˆํ•˜๊ฒŒ ์ž์‹ ์ฐฝ์ด ๋ฐฐ์น˜๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ์—์„œ ๋ช…์‹œ์ ์œผ๋กœ ๊ฐ์ฒด ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ logon.setLayout(mainLayout) ํ˜ธ์ถœ ์‹œ ์ž๋™์œผ๋กœ ์„ค์ •๋˜๊ฒŒ ๋œ๋‹ค. Qt๋Š” ์–ด๋–ค ๊ฐ์ฒด์—์„œ ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  ์ด๋ฅผ ์Šฌ๋กฏ ํ•จ์ˆ˜์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ์‹œ๊ทธ๋„-์Šฌ๋กฏ์ด๋ผ๋Š” ๋…ํŠนํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ œ๊ณตํ•œ๋‹ค. ์ฝ”๋“œ์—์„œ buttonOk.clicked.connect(app.quit)๋Š” Ok ๋ฒ„ํ„ด์— ๋ฐ˜์‘ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๋„๋ก ํ•˜๊ณ  ์žˆ๋‹ค. QPushButton ํด๋ž˜์Šค๋Š” ์ž์‹ ์ด ๋ˆŒ๋ฆฌ๊ฒŒ ๋˜๋ฉด clicked()๋ผ๋Š” ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ์‹œํ‚ค๋ฉฐ, QApplication์€ close()๋ผ๋Š” ์Šฌ๋กฏ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฐ ์ด ํ•จ์ˆ˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์›๋ž˜ C++ Qt์— ๋”ฐ๋ฅด๋Š” ์˜ˆ์ „ ๋ฌธ๋ฒ•์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด QtCore์— ์ •์˜๋œ SIGNAL(), SLOT()์ด๋ผ๋Š” ๋งคํฌ๋กœ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด๊ณ  ์ฝ”๋“œ์™€ ๊ฐ™์€<NAME>์€ PyQt5๋ฅผ ํ‰๋‚ด ๋‚ด์–ด ์ƒˆ๋กญ๊ฒŒ ๋„์ž…๋œ ๋ฐฉ๋ฒ•์ด๋‹ค. from PySide2.QtCore import SIGNAL, SLOT, QObject ... QObject.connect(buttonOk, SIGNAL('clicked()'),app, SLOT('quit()')) ์‹œ๊ทธ๋„-์Šฌ๋กฏ์€ Qt์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ณ  ์ˆ˜์ค€์˜ ๋ช…๋ น ์ฒ˜๋ฆฌ ๋ฐฉ์‹์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋Œ€๋ถ€๋ถ„ Qt ํด๋ž˜์Šค์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค์ธ QObject๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•œํŽธ, ์ € ์ˆ˜์ค€์˜ ๋ช…๋ น ์ฒ˜๋ฆฌ ๋ฐฉ์‹์œผ๋กœ ์œ„์ ฏ์„ ๋Œ€์ƒ์œผ๋กœ ํ•˜๋Š” ๋ฉ”์‹œ์ง€(์ด๋ฒคํŠธ)๊ฐ€ ์žˆ๋‹ค. Qt๋Š” ๋‹ค์–‘ํ•œ ์—ด๊ฑฐ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๊ณตํ†ต์ ์ธ ์—ด๊ฑฐ ๋ณ€์ˆ˜๋Š” QtCore.Qt์— ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉฐ, ๊ฐœ๋ณ„ ํด๋ž˜์Šค์—์„œ๋„ ์ •์˜๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. labelId.setAlignment(Qt.AlignRight | Qt.AlignVCenter)์— ์‚ฌ์šฉ๋œ AlignRight์™€ AlignVCenter๋Š” ์ „์ž์˜ ์˜ˆ์ด๊ณ , lineEditPW.setEchoMode(QLineEdit.Password)์˜ Password๋Š” ํ›„์ž์˜ ์˜ˆ์ด๋‹ค. Logon2 ์˜ˆ์ œ Logon2 ์˜ˆ์ œ๋Š” Logon1 ์˜ˆ์ œ์˜ ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ•œ ์˜ˆ์ œ๋กœ์„œ ์„œ๋ธŒํด๋ž˜์‹ฑ ๊ณผ ์‚ฌ์šฉ์ž ์ •์˜ ์‹œ๊ทธ๋„ ๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. Logon2.py import sys from PySide2.QtWidgets import (QApplication, QWidget, QLabel, QLineEdit, QGridLayout, QPushButton, QHBoxLayout, QVBoxLayout, QMessageBox) from PySide2.QtGui import QIcon from PySide2.QtCore import Qt, Signal import Logon_rc class Logon(QWidget): ok = Signal() def __init__(self, ids, pws, parent=None): QWidget.__init__(self, parent) self.listIds = ids self.listPWs = pws self.labelId = QLabel('&Id :') self.labelId.setAlignment(Qt.AlignRight | Qt.AlignVCenter) self.labelPW = QLabel('&Password:') self.lineEditId = QLineEdit() self.lineEditPW = QLineEdit() self.lineEditPW.setEchoMode(QLineEdit.Password) self.labelId.setBuddy(self.lineEditId) self.labelPW.setBuddy(self.lineEditPW) self.buttonOk = QPushButton("&Ok") self.buttonOk.setIcon(QIcon(":/images/ok.png")) layout1 = QGridLayout() layout1.addWidget(self.labelId, 0,0) layout1.addWidget(self.lineEditId, 0,1); layout1.addWidget(self.labelPW, 1,0) layout1.addWidget(self.lineEditPW, 1,1) layout2 = QHBoxLayout() layout2.addStretch() layout2.addWidget(self.buttonOk) mainLayout = QVBoxLayout() mainLayout.addLayout(layout1) mainLayout.addLayout(layout2) self.setLayout(mainLayout) self.setWindowTitle('Log on') self.setWindowIcon(QIcon(":/images/ok.png")) self.buttonOk.clicked.connect(self.onOk) def onOk(self): if (self.lineEditId.text() not in self.listIds): QMessageBox.critical(self,"Logon error","Unregistered user") self.lineEditId.setFocus() else: idx = self.listIds.index(self.lineEditId.text()) if self.lineEditPW.text() != self.listPWs[idx] : QMessageBox.critical(self,"Logon error","Incroreect password") self.lineEditPW.setFocus() else: self.ok.emit() if __name__ == '__main__': app = QApplication(sys.argv) ids = ['James','John','Jane'] pws = ['123','456','789'] logon = Logon(ids, pws) logon.ok.connect(app.exit) logon.show() app.exec_() ์„œ๋ธŒํด๋ž˜์‹ฑ(subclassing)์€ ํด๋ž˜์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ƒ์†์„ ํ†ตํ•ด ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๋Š” ๊ธฐ๋ฒ•์„ ์˜๋ฏธํ•œ๋‹ค. Qt ์—ญ์‹œ C++ ํด๋ž˜์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋ฏ€๋กœ ์ด๋ฅผ ์ ๊ทน ํ™œ์šฉํ•˜์˜€๋‹ค. Logon1 ์˜ˆ์ œ๋Š” '__main__' ๋ธ”๋ก์—์„œ ์„œ๋ธŒํด๋ž˜์‹ฑ์—†์ด QWidget ํด๋ž˜์Šค๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ–ˆ๋‹ค. ๋ฐ˜๋ฉด์— Logon2๋Š” QWidget์„ ์„œ๋ธŒํด๋ž˜์‹ฑํ•œ Logon ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๊ณ , QLabel, QLineEdit ๋“ฑ์„ ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๋กœ ๋ฐฐ์น˜ํ•˜์˜€๋‹ค. Qt์—์„œ ๊ฐ์ฒด ๊ฐ„์˜ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋ฅผ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋ณดํ†ต์€ ์ƒ์„ฑ์ž์˜ ๋งˆ์ง€๋ง‰ ์ธ์ž๋ฅผ ๋ถ€๋ชจ ๊ฐ์ฒด parent๋กœ ์ง€์ •ํ•˜๋Š” ๊ทœ์•ฝ์„ ๊ด€์Šต์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ Logon ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋Š” def __init__(self, ids, pws, parent=None): ์™€ ๊ฐ™์ด ์ •์˜ํ•˜์˜€๋‹ค. Logon ํด๋ž˜์Šค์— ํฌํ•จ๋˜๋Š” ์—ฌ๋Ÿฌ ์ฐฝ์ด ๋‹ค์‹œ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๋Œ€๋น„ํ•˜์—ฌ ๋ผ์ธ ์—๋””ํŠธ ์œ„์ ฏ๊ณผ ๋ฒ„ํ„ด ์œ„์ ฏ(self.lineEditId, self.lineEditPW, self.buttonOk)์„ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•œ๋‹ค. ๋ฐ˜๋ฉด์— ๋‹ค์‹œ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๋ผ๋ฒจ ์œ„์ ฏ๊ณผ ๋ ˆ์ด์•„์›ƒ ๊ฐ์ฒด๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค. ๋‹ค๋งŒ ๊ฐ์ฒด ๊ฐ„์˜ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋Š” ์œ ์ง€๋˜๋ฉฐ, ๋ถ€๋ชจ๊ฐ€ ์ž์‹ ๊ฐ์ฒด์˜ ์‚ญ์ œ๋ฅผ ์ฑ…์ž„์ง„๋‹ค. Logon ํด๋ž˜์Šค๋Š” ๋ฒ„ํ„ด์ด ๋ˆŒ ๋ฆฌ ๋•Œ ๋ฐ˜์‘ํ•˜๋„๋ก ์ƒˆ๋กœ์šด ์‹œ๊ทธ๋„์ธ ok()๋ฅผ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. ์‹œ๊ทธ๋„์„ ์ •์˜ํ•˜๋Š” ํ•˜๋Š” ๊ฒƒ์€ QtCore์— ์žˆ๋Š” Signal ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋กœ ์ •์˜ํ•œ ํ›„ ํ•„์š”์‹œ emit()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค. ๋‹ค์Œ์€ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ์˜ ํ˜•ํƒœ์ด๋‹ค. ์‹œ๊ทธ๋„์„ ๊ฐ–๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ QObject๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์•„์•ผ ํ•œ๋‹ค. from PySide2.QtCore import Signal # Must inherit QObject for signals class Communicate(QObject): speak = Signal() def __init__(self): super(Communicate, self).__init__() self.speak.connect(self.say_hello) def speaking_method(self): self.speak.emit() def say_hello(self): print("Hello") someone = Communicate() someone.speaking_method.connect(some_slot) # ์‹œ๊ทธ๋„-์Šฌ๋กฏ ์—ฐ๊ฒฐ ๊ฐœ๋… ์žก๊ธฐ Qt๋Š” C++ ํด๋ž˜์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๊ณ , PySide2๋Š” ์ด์— ๋Œ€ํ•œ ํŒŒ์ด์ฌ ๋ฐ”์ธ๋”ฉ์ด๋‹ค. ๋‹ค์Œ์€ Qt ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ฐœ๋…์„ ์žก๊ธฐ ์œ„ํ•ด์„œ ์•Œ์•„์•ผ ํ•  ์ฃผ์š” ํŠน์ง•๋“ค์ด๋‹ค. QObject : Qt ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋กœ (1) ๊ฐ์ฒด ๊ฐ„์˜ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„์™€ (2) ์‹œ๊ทธ๋„-์Šฌ๋กฏ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์กŒ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ Qt ํด๋ž˜์Šค๊ฐ€ QObject๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›๋Š”๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๊ฐ€ ์—†๊ณ , ์‹œ๊ทธ๋„-์Šฌ๋กฏ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. QWidget : ์ฐฝ ํด๋ž˜์Šค์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค. ๊ทธ ์ž์ฒด๋กœ ๋‹ค๋ฅธ ์ž์‹ ์ฐฝ์„ ๋‹ด๋Š” ์ปจํ…Œ์ด๋„ˆ๋กœ ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•œ๋‹ค. ๊ฐ์ฒด์˜ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„ : ์•ž์„œ ์„ค๋ช…ํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค : ์•ž์„œ์—์„œ ์„ค๋ช… ์‹œ๊ทธ๋„-์Šฌ๋กฏ : ์•ž์„œ์—์„œ ์„ค๋ช…. ๊ณ  ์ˆ˜์ค€์˜ ๋ช…๋ น ์ฒ˜๋ฆฌ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์„œ๋ธŒํด๋ž˜์‹ฑ : ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•˜๋Š” ๊ธฐ๋ฒ•์„ ์˜๋ฏธ(Qt๋‚˜ PySide2์˜ ํŠน์ง•์ด ์•„๋‹Œ ๊ฐ์ฒด์ง€ํ–ฅํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํŠน์ง•์œผ๋กœ Qt์—์„œ ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ์‚ฌ์šฉํ•จ) ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ : ์ฐฝ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์‹œ์ง€(์ด๋ฒคํŠธ) ์ฒ˜๋ฆฌ. ์‹œ๊ทธ๋„-์Šฌ๋กœ๊ณผ ๋น„๊ตํ•  ๋•Œ ์ € ์ˆ˜์ค€์˜ ๋ช…๋ น ์ฒ˜๋ฆฌ์ด๋‹ค. ๋ณดํ†ต ์ƒˆ๋กœ์šด ์œ„์ ฏ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ํ•„์š”ํ•˜๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด QPushButton์˜ ๊ฒฝ์šฐ clicked() ์‹œ๊ทธ๋„์—๋งŒ ๊ด€์‹ฌ์ด ์žˆ์„ ๋ฟ ์‹œ๊ทธ๋„์„ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ์ € ์ˆ˜์ค€์˜ ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ๋‚˜ ํ‚ค ์ด๋ฒคํŠธ๋ฅผ ์•Œ ํ•„์š”๊ฐ€ ์—†๋‹ค. ํ•˜์ง€๋งŒ QWidget์„ ์„œ๋ธŒํด๋ž˜์‹ฑํ•˜์—ฌ ์ง์ ‘ ์ปค์Šคํ…€ ์œ„์ ฏ์„ ์ž‘์„ฑํ•  ๋•Œ๋Š” ๋งˆ์šฐ์Šค, ํ‚ค, ํŽ˜์ธํŠธ, ํƒ€์ด๋จธ ์ด๋ฒคํŠธ ๋“ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์ ฏ์— ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ ํŽ˜์ธํŠธ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. ์—ด๊ฑฐ ๋ณ€์ˆ˜ : ์•ž์„œ์—์„œ ์„ค๋ช… ๋ฆฌ์†Œ์Šค ํŒŒ์ผ(.qrc ํŒŒ์ผ) : ์•ž์„œ์—์„œ ์„ค๋ช… Ui ํŒŒ์ผ(.ui ํŒŒ์ผ) : ๊ทธ๋ž˜ํ”ฝ ํ•˜๊ฒŒ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ์ถ•ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ผ. Qt Designer๋กœ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. 2. ์œ„์ ฏ ๊ธฐ์ดˆ ์œ„์ ฏ Qt ํ”„๋กœ๊ทธ๋žจ์€ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ์œ„์ ฏ์„ ๋งŒ๋“ค๊ณ  ์กฐํ•ฉํ•˜๋Š” ์ž‘์—…์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋งŒ๋“  ์œ„์ ฏ์„ ์ปค์Šคํ…€ ์œ„์ ฏ(custom widget)์ด๋ผ ํ•˜๋Š”๋ฐ ์ปค์Šคํ…€ ์œ„์ ฏ์€ ์„œ๋ธŒํด๋ž˜์‹ฑ(subclassing)์„ ํ†ตํ•ด ๋งŒ๋“ค์–ด ๋‚ธ๋‹ค. ๊ทธ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. QWidget์„ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค๋ฅธ ์œ„์ ฏ์„ ๊ทธ ์†์— ๋ฐฐ์น˜ํ•ด์„œ ๋งŒ๋“ค๊ฑฐ๋‚˜(์ด ๊ฒฝ์šฐ๋„ QWidget์—์„œ ์ƒ์†๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ธŒํด๋ž˜์‹ฑ ๋ฒ”์ฃผ์— ๋“ค์–ด๊ฐ) ๋ฒ„ํ„ด, ์ž…๋ ฅ, ์•„์ดํ…œ ์œ„์ ฏ๊ฐ™์ด Qt๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋‚ด์žฅ ์œ„์ ฏ(built-in widget)์„ ์„œ๋ธŒํด๋ž˜์‹ฑํ•˜๊ฑฐ๋‚˜ QWidget์„ ์ง์ ‘ ์„œ๋ธŒํด๋ž˜์‹ฑ ๋“ฑ์˜ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์ด ์ค‘์—์„œ (1)์€ ์ด๋ฏธ Logon2 ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค. ์ปค์Šคํ…€ ์œ„์ ฏ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ์ง์ ‘ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ด๋„ ๋˜์ง€๋งŒ Qt Designer๋กœ ์‹œ๊ฐ์ ์œผ๋กœ ํผ(form)์„ ์ž‘์„ฑํ•˜์—ฌ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์œ„์ ฏ ๋‘˜๋Ÿฌ๋ณด๊ธฐ QWidget์˜ ์ž์‹ ํด๋ž˜์Šค๋กœ Qt๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋‹ค์–‘ํ•œ ์œ„์ ฏ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ฃผ์š” ์œ„์ ฏ์„ ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. ๋ฒ„ํ„ด ๋ฒ„ํ„ด์œผ๋กœ๋Š” QPushButton, QToolButton, QCheckBox, QRadioButton ๋“ฑ์ด ์žˆ๋‹ค. QPushButton์ด ์ผ๋ฐ˜ ๋ฒ„ํ„ด์ด๊ณ , QCheckBox, QRadioButton์€ ๊ฐ๊ฐ ์ฒดํฌ ๋ฒ„ํ„ด, ๋ผ๋””์˜ค ๋ฒ„ํ„ด์ด๋‹ค. QToolButton์€ QPushButton๊ณผ ๋™์ผํ•˜์ง€๋งŒ ์•ก์…˜(QAction)๊ณผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํ„ด์œผ๋กœ ํˆด๋ฐ”(QToolBar)์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์•ก์…˜์€ ๋ฉ”๋‰ด, ํˆด๋ฐ” ๋ฒ„ํ„ด, ํ‚ค๋ณด๋“œ ๋‹จ์ถ•ํ‚ค(shortcuts) ๋“ฑ์„ ํ†ตํ•ฉํ•ด์„œ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ๋„์ž…๋œ ๊ฐœ๋…์ด๋‹ค. (๋ฒ„ํ„ด ์œ„์ ฏ) ์ž…๋ ฅ ์œ„์ ฏ๊ณผ ํ‘œ์‹œ ์œ„์ ฏ ๊ฐ„๋‹จํ•œ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•œ ์œ„์ ฏ์œผ๋กœ๋Š” QLineEdit, QComboBox, QSlider, QSpinBox ๋“ฑ์ด ํ”ํžˆ ์‚ฌ์šฉ๋œ๋‹ค. QLineEdit๋Š” ํ•œ ์ค„์งœ๋ฆฌ ์—๋””ํ„ฐ ์œ„์ ฏ์ด๊ณ , QComboBox๋Š” ๋“œ๋กญ ๋‹ค์šด ์ด ๊ฐ€๋Šฅํ•œ ๋ฆฌ์ŠคํŠธ์™€ ํ•œ ์ค„ ์—๋””ํ„ฐ ์œ„์ ฏ์ด ๊ฒฐํ•ฉ๋œ ์œ„์ ฏ์ด๋‹ค. ์—ฌ๋Ÿฌ ์ค„์„ ํŽธ์ง‘ํ•˜๋Š” ์—๋””ํ„ฐ ์œ„์ ฏ์œผ๋กœ๋Š” QTextEdit์™€ QPlainTextEdit๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. QPlainTextEdit๋Š” ๊ฐ„๋‹จํ•œ ์„œ์‹๋งŒ ์ค„ ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ฉด QTextEdit๋Š” ๋ฆฌ์น˜ ํ…์ŠคํŠธ(rich text) ์„œ์‹์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ํ‘œ์‹œ ์œ„์ ฏ ์ค‘์—๋Š” QLabel์ด ๊ฐ€์žฅ ํ”ํžˆ ์‚ฌ์šฉ๋œ๋‹ค. ๊ณ ๊ธ‰ ํ‘œ์‹œ ์œ„์ ฏ์œผ๋กœ QCalender, QWebView ๋“ฑ์ด ์žˆ๋‹ค. (์ž…๋ ฅ ์œ„์ ฏ๊ณผ ํ‘œ์‹œ ์œ„์ ฏ) ์ปจํ…Œ์ด๋„ˆ ๋‹ค๋ฅธ ์œ„์ ฏ์— ๋Œ€ํ•œ ์ปจํ…Œ์ด๋„ˆ๋กœ ์‚ฌ์šฉ๋˜๋Š” QGroupBox, QTabWidget, QStackedWidget, QDockWidget, QSpliter, QScrollArea ๋“ฑ์ด ์žˆ๋‹ค. QTabWidget์ด๋‚˜ QStackWidget์œผ๋กœ ํ˜•ํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ์„ธ๋ จ๋œ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. (์ปจํ…Œ์ด๋„ˆ ์œ„์ ฏ : ์ถœ์ฒ˜ ParaView) ์•„์ดํ…œ ๋ทฐ ์œ„์ ฏ ์•„์ดํ…œ์„ ํ…Œ์ด๋ธ”, ๋ฆฌ์ŠคํŠธ, ํŠธ๋ฆฌ๋กœ ํ‘œ์‹œํ•˜๋Š” ์œ„์ ฏ์œผ๋กœ QTableWidget, QListWidget, QTreeWidget ๋“ฑ์ด ์žˆ๋‹ค. ๋˜ํ•œ ์ด๋“ค๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜์ง€๋งŒ Qt์˜ ๋ชจ๋ธ-๋ทฐ ๊ตฌ์กฐ๋ฅผ ์ง€์›ํ•˜๋Š” QTableView, QListView, QTreeView ๋“ฑ์ด ์žˆ๋‹ค. (์•„์ดํ…œ ๋ทฐ) 2.1 Qt Designer ์†Œ๊ฐœ Qt Designer ์ปค์Šคํ…€ ์œ„์ ฏ, ๋‹ค์ด์–ผ๋กœ๊ทธ, ๋ฉ”์ธ ์œˆ๋„ ๋“ฑ์„ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ด๊ฒƒ์€ ๋งค์šฐ ์ง€๊ฒจ์šด ์ž‘์—…์ด๋‹ค. Qt๋Š” Qt Designer๋ฅผ ํ†ตํ•ด ์‹œ๊ฐ์ ์„ ์‰ฝ๊ฒŒ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ์˜ ์ ˆ์ฐจ์— ๋”ฐ๋ฅธ๋‹ค. ์‹œ๊ฐ์ ์œผ๋กœ ์œ„์ ฏ์„ ๋””์ž์ธํ•˜์—ฌ ์ด๋ฅผ XML<NAME>์˜. ui ํŒŒ์ผ๋กœ ์ €์žฅํ•œ๋‹ค. ์ด๋ฅผ pyside2-uic ์œ ํ‹ธ๋Ÿฌํ‹ฐ๋กœ .ui ํŒŒ์ผ์„ ํŒŒ์ด์ฌ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉ๋œ๋‹ค. ๋ณ€ํ™˜๋œ ํŒŒ์ด์ฌ ํŒŒ์ผ์— ์ •์˜๋œ Ui_xxx ํ˜•ํƒœ์˜ ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ์— ๋ฐ˜์˜ํ•œ๋‹ค. Qt Designer ์‹คํ–‰๊ณผ ์ธํ„ฐํŽ˜์ด์Šค Qt Designer๋Š” PySide2๋ฅผ ์ธ์Šคํ†จํ•˜๋ฉด ๊ฐ™์ด ์„ค์น˜๋œ๋‹ค. ์‹คํ–‰์€ ์ปค๋งจ๋“œ ํ”„๋กฌํ”„ํŠธ์—์„œ ๋‹ค์Œ์œผ๋กœ ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค. > designer ์‹คํ–‰<NAME> ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋ฉ”๋‰ด์™€ ํˆด๋ฐ”๊ฐ€ ์žˆ์œผ๋ฉฐ, ํ™”๋ฉด ์ค‘์•™์— ํผ์ด ํ‘œ์‹œ๋œ๋‹ค. 5๊ฐœ์˜ ํˆด ํŒจ๋„์ด ์ขŒ์šฐ์— ๋„ํ‚น ์œˆ๋„๋กœ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์ ฏ ์ƒ์ž(Widget Box): ์ ์šฉ ๊ฐ€๋Šฅํ•œ Qt ์œ„์ ฏ์ด ์žˆ๋‹ค. ์œ„์ ฏ์„ ์„ ํƒํ•œ ํ›„ ํผ์œผ๋กœ ๋“œ๋ž˜๊ทธ ๋“œ๋กญํ•˜๋ฉด ํผ์— ์œ„์ ฏ์ด ์ƒ์„ฑ๋œ๋‹ค. ์†์„ฑ ํŽธ์ง‘๊ธฐ(Property Editor): ์„ ํƒ๋œ ์œ„์ ฏ์˜ ์†์„ฑ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ์ฒด ํƒ์ƒ‰๊ธฐ(Object Inspector): ํผ์˜ ์œ„์ ฏ๋“ค์˜ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋ฅผ ๋ณด์—ฌ ์ค€๋‹ค. ๋ฆฌ์†Œ์Šค ํƒ์ƒ‰๊ธฐ(Resource Browser): ๋ฆฌ์†Œ์Šค ํŒŒ์ผ์„ ํŽธ์ง‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋™์ž‘ ํŽธ์ง‘๊ธฐ(Action Editor): ์•ก์…˜(QAction)์„ ํŽธ์ง‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‹œ๊ทธ๋„/์Šฌ๋กฏ ํŽธ์ง‘๊ธฐ(Singal/Slot Editor): ์œ„์ ฏ ๊ฐ„์˜ ์‹œ๊ทธ๋„-์Šฌ๋กฏ์„ ๋ณด์—ฌ ์ฃผ๋ฉฐ ์‹œ๊ทธ๋„-์Šฌ๋กฏ์„ ํŽธ์ง‘ํ•  ์ˆ˜ ์žˆ๋‹ค. Qt Designer๋Š” ๋‹ค์Œ์˜ 4๊ฐ€์ง€ ๋ชจ๋“œ์—์„œ ์‹คํ–‰๋œ๋‹ค. ์œ„์ ฏ ํŽธ์ง‘(Edit widgets): ์ž์‹ ์œ„์ ฏ์„ ์ƒ์„ฑํ•˜๊ณ  ์†์„ฑ์„ ์ดˆ๊ธฐํ™”ํ•˜๋ฉฐ, ๋ ˆ์ด์•„์›ƒ์„ ์„ค์ •ํ•œ๋‹ค. ์‹œ๊ทธ๋„/์Šฌ๋กฏ ํŽธ์ง‘(Edit Signals/Slots): ์ž์‹ ์œ„์ ฏ ๊ฐ„์˜ ์‹œ๊ทธ๋„/์Šฌ๋กฏ์„ ์„ค์ •ํ•œ๋‹ค. ์นœ๊ตฌ ํŽธ์ง‘(Edit Buddies): QLabel์˜ ๋ฒ„๋””๋ฅผ ์ง€์ •ํ•œ๋‹ค. ํƒญ ์ˆœ์„œ ํŽธ์ง‘(Edit Tab Order): ํƒญ ์ˆœ์„œ(ํƒญํ‚ค๋กœ ์ด๋™๋˜๋Š” ํฌ์ปค์Šค์˜ ์ˆœ์„œ)๋ฅผ ์„ค์ •ํ•œ๋‹ค. ๋ณดํ†ต์€ ์œ„์ ฏ ํŽธ์ง‘(Edit widgets) ๋ชจ๋“œ์—์„œ ํผ์„ ์ž‘์„ฑํ•˜๊ณ , ์ž‘์„ฑ์ด ๋๋‚œ ํ›„ ๋‚˜๋จธ์ง€ ๋ชจ๋“œ๋กœ ๋“ค์–ด๊ฐ€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. 4๊ฐ€์ง€ ๋ชจ๋“œ๋Š” ๋ฉ”๋‰ด๋ฐ”์˜ ํŽธ์ง‘(Edit)์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. 2.2 ํผ์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ์œ„์ ฏ Logon3 ์˜ˆ์ œ๋Š” ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•œ Logon2 ์˜ˆ์ œ์˜ ์œ„์ ฏ Logon์„ Qt Designer๋กœ ์ž‘์„ฑํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค. ํผ ๋งŒ๋“ค๊ธฐ(Logon.ui ํŒŒ์ผ) images/ok.png ํŒŒ์ผ์„ ์ค€๋น„ํ•œ๋‹ค. Qt Designer๋ฅผ ์‹คํ–‰ํ•œ๋‹ค(์ปค๋งจ๋“œ ํ”„๋กฌํ”„ํŠธ์—์„œ designer ํƒ€์ดํ•‘) File/New ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•ด ํ‘œ์‹œ๋˜๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ์—์„œ โ€œWidgetโ€์„ ์„ ํƒํ•œ๋‹ค. ์ €์žฅ์„ ๋ˆŒ๋Ÿฌ Logon.ui๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์†์„ฑ ํŽธ์ง‘๊ธฐ์—์„œ Logon ์œ„์ ฏ์— ๋Œ€ํ•œ ์†์„ฑ์„ ์ง€์ •ํ•œ๋‹ค. objectName์„ Logon, Window Title๋กœ๋Š” Log on, windowIcon์œผ๋กœ๋Š” ok.png๋ฅผ ์„ ํƒํ•ด ์ค€๋‹ค. ok.png๋ฅผ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฆฌ์†Œ์Šค๋กœ ์ง€์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๋Š” ๋ฐ ์•„๋ž˜ ๊ทธ๋ฆผ์˜ ์ˆœ์„œ๋กœ Logon.qrc ์ด๋ฆ„์œผ๋กœ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ์„ ์ €์žฅํ•˜๊ณ , ok.png ํŒŒ์ผ์„ ๋ฆฌ์†Œ์Šค๋กœ ์„ ํƒ ์ค€๋‹ค. Widget Box์—์„œ Label, Line Edit, Push Button, Horizontal Spacer ๋“ฑ์„ ์ฐพ์•„ ๋งˆ์šฐ์Šค๋กœ ๋“œ๋ž˜๊ทธํ•ด์„œ ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ํผ์— ๋ฐฐ์น˜ํ•˜๊ณ  ํผ์˜์˜ ํฌ๊ธฐ๋„ ์ ์ ˆํžˆ ์กฐ์ •ํ•ด ์ค€๋‹ค. ๋ผ๋ฒจ๊ณผ ๋ฒ„ํ„ด์„ ํด๋ฆญํ•ด์„œ ํ…์ŠคํŠธ๋ฅผ "&Id:", "&Password:", "&Ok"๋กœ ๋ฐ”๊พธ์–ด ์ค€๋‹ค. ์ด ์ž‘์—…์€ ์†์„ฑ ํŽธ์ง‘๊ธฐ์—์„œ text ์†์„ฑ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํผ ๋‚ด์˜ ์œ„์ ฏ๋“ค์€ ์ž์‹ ์˜ ๊ณ ์œ  ์ด๋ฆ„์„ ๊ฐ€์ง€๋Š”๋ฐ ์ด๋ฅผ objectName์ด๋ผ๊ณ  ํ•œ๋‹ค. ํ•„์š”ํ•  ๊ฒฝ์šฐ ๋””ํดํŠธ๋กœ ์ƒ์„ฑ๋œ objectName ๋Œ€์‹  ์˜๋ฏธ ์žˆ๋Š” ์ด๋ฆ„์œผ๋กœ ๋Œ€์ฒดํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ฐ์ฒด ํƒ์ƒ‰๊ธฐ์—์„œ ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์œ„์ ฏ์˜ objectName์„ ๋ณ€๊ฒฝํ•œ๋‹ค. ์ด ์ž‘์—…์„ Property Editor์—์„œ objectName ์†์„ฑ์—์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ๋‹ค. lineEditPW๋ฅผ ์„ ํƒํ•œ ํ›„ ์†์„ฑ ํŽธ์ง‘๊ธฐ์—์„œ echoMode๋ฅผ Password๋กœ ์„ค์ •ํ•ด ์ฃผ๊ณ , buttonOk๋ฅผ ์„ ํƒํ•˜์—ฌ icon ์†์„ฑ์— ๋ฆฌ์†Œ์Šค ์ค‘ ok.png๋กœ ์„ค์ •ํ•ด ์ค€๋‹ค. ์ด์ œ ๋ ˆ์ด์•„์›ƒ์„ ์„ค์ •ํ•œ๋‹ค. ๋จผ์ € labelId, lineEditId, labelPW, lineEditPW๋ฅผ ์„ ํƒํ•ด ๋ฉ”๋‰ด์—์„œ Form/Layout in a Grid๋ฅผ ์„ ํƒํ•œ๋‹ค(๋˜๋Š” ํˆด๋ฐ”์˜ ํ–‰๋ ฌ ํ˜•ํƒœ์ด ์•„์ด์ฝ˜ ). ์œ„์ ฏ์˜ ๋‹ค์ค‘ ์„ ํƒ์€ Ctrl์„ ๋ˆ„๋ฅธ ์ฑ„๋กœ ๋งˆ์šฐ์Šค๋กœ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค. ์ŠคํŽ˜์ด์„œ์™€ buttonOk๋ฅผ ์„ ํƒํ•ด ๋ฉ”๋‰ด์—์„œ Form/Layout Horizontally๋ฅผ ์„ ํƒํ•œ๋‹ค(๋˜๋Š” ํˆด๋ฐ”์˜ ๊ฐ€๋กœ ์‚ผ์„  ์•„์ด์ฝ˜). ํผ์˜ ๋ฐ”ํƒ•์„ ๋งˆ์šฐ์Šค๋กœ ํด๋ฆญํ•˜๊ฑฐ๋‚˜ Object Inspector์—์„œ Logon์„ ์„ ํƒํ•œ ํ›„ ๋ฉ”๋‰ด์—์„œ Form/Layout Vertically๋ฅผ ์„ ํƒํ•œ๋‹ค(๋˜๋Š” ํˆด๋ฐ”์˜ ์„ธ๋กœ ์‚ผ์„  ์•„์ด์ฝ˜). ์ด์ œ ๋ผ๋ฒจ์˜ ๋ฒ„๋””๋ฅผ ์นœ๊ตฌ ํŽธ์ง‘(Edit Buddies) ๋ชจ๋“œ์—์„œ ์ง€์ •ํ•˜๊ณ , ํƒญ ์ˆœ์„œ๋ฅผ ๊ฒ€ํ† ํ•œ๋‹ค. ๋ฉ”๋‰ด์—์„œ โ€œํŽธ์ง‘/์นœ๊ตฌ ํŽธ์ง‘โ€์„ ์„ ํƒํ•˜๋ฉด ์นœ๊ตฌ ํŽธ์ง‘ ๋ชจ๋“œ๋กœ ์ง„์ž…์ด๋‹ค. labelId๋ฅผ ๋งˆ์šฐ์Šค๋กœ ์„ ํƒํ•˜๊ณ  lineEditId๋กœ ๋“œ๋ž˜๊ทธํ•˜๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๋ฒ„๋””๊ฐ€ ์„ค์ •๋œ๋‹ค. ๋ฒ„๋””๊ฐ€ ์„ค์ •๋˜๊ณ  ๋‚˜๋ฉด โ€œ&Id"๊ฐ€ ์•„๋‹ˆ๋ผ "Id"ํ˜•ํƒœ๋กœ ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค. ๋™์ผํ•œ ์ž‘์—…์„ labelPW์™€ lineEditPW์—๋„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋ฉ”๋‰ด์—์„œ Edit/Edit Tab Order๋ฅผ ์„ ํƒํ•ด Edit Tab Order ๋ชจ๋“œ๋กœ ์ง„์ž…ํ•œ๋‹ค. ๋งˆ์šฐ์Šค๋ฅผ ํด๋ฆญํ•ด ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ˆœ์„œ๊ฐ€ ๋˜๋„๋ก ํ•œ๋‹ค. .ui ํŒŒ์ผ ์ปดํŒŒ์ผ(.ui -> .py) .ui ํŒŒ์ผ์€ pyside2-uic ์œ ํ‹ธ๋Ÿฌํ‹ฐ๋กœ ui_xxx.py ํ˜•ํƒœ์˜ ํŒŒ์ด์ฌ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. .ui ํŒŒ์ผ์—์„œ ์‚ฌ์šฉ๋œ. qrc ํŒŒ์ผ ์—ญ์‹œ pyside2-rcc์„ ์ด์šฉํ•ด ํŒŒ์ด์ฌ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. > pyside2-uic Logon.ui > ui_logon.py > pyside2-rcc -o Logon_rc.py -py3 Logon.qrc ์ƒ์„ฑ๋œ ui_logon.py๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์ด๋‹ค. ... from PySide2 import QtCore, QtGui, QtWidgets class Ui_Logon(object): def setupUi(self, Logon): ... self.labelId = QtWidgets.QLabel(Logon) self.lineEditId = QtWidgets.QLineEdit(Logon) ... QtCore.QMetaObject.connectSlotsByName(Logon) ... import Logon_rc ์ƒ์„ฑ๋œ ui_logon.py๋ฅผ ๋ณด๋ฉด Ui_Logon ํด๋ž˜์Šค๊ฐ€ ์ž๋™ ์ƒ์„ฑ๋จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ import Logon_rc๋กœ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ์„ ํŒŒ์ด์ฌ์œผ๋กœ ๋ณ€ํ™˜ํ•œ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•œ๋‹ค. ํŒŒ์ด์ฌ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์ด์ œ. ui ํŒŒ์ผ์—์„œ ์ƒ์„ฑ๋œ Ui_Logon ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. Logon3.py import sys from PySide2.QtWidgets import (QApplication, QWidget, QLabel, QLineEdit, QGridLayout, QPushButton, QHBoxLayout, QVBoxLayout, QMessageBox) from PySide2.QtGui import QIcon from PySide2.QtCore import Qt, Signal from ui_logon import Ui_Logon class Logon(QWidget): ok = Signal() def __init__(self, ids, pws, parent=None): QWidget.__init__(self, parent) self.listIds = ids self.listPWs = pws self.ui = Ui_Logon() self.ui.setupUi(self) self.ui.buttonOk.clicked.connect(self.onOk) def onOk(self): if (self.ui.lineEditId.text() not in self.listIds): QMessageBox.critical(self,"Logon error","Unregistered user") self.ui.lineEditId.setFocus() else: idx = self.listIds.index(self.ui.lineEditId.text()) if self.ui.lineEditPW.text() != self.listPWs[idx] : QMessageBox.critical(self,"Logon error","Incroreect password") self.ui.lineEditPW.setFocus() else: self.ok.emit() if __name__ == '__main__': app = QApplication(sys.argv) ids = ['James','John','Jane'] pws = ['123','456','789'] logon = Logon(ids, pws) logon.ok.connect(app.exit) logon.show() app.exec_() ์ƒ์„ฑ์ž์—์„œ Ui_Logon ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  setupUi(parent)๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. self.ui = Ui_Logon() self.ui.setupUi(self) Ui_Logon ํด๋ž˜์Šค ๋‚ด์— ์ •์˜๋˜์–ด ์žˆ๋Š” ์ž์‹ ์œ„์ ฏ์€ self.ui.buttonOk ๋“ฑ๊ณผ ๊ฐ™์ด ์ ‘๊ทผํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ•˜๋ฉด ๋œ๋‹ค. 2.3 ๊ธฐ๋ณธ ๋‚ด์žฅ ์œ„์ ฏ - ๋ฒ„ํ„ด Qt์˜ ๋ฒ„ํ„ด ํด๋ž˜์Šค๋กœ๋Š” QPushButton, QCheckBox, QRadioButton, QToolButton ๋“ฑ์ด ์žˆ์œผ๋ฉฐ ์ด๋“ค์€ QAbstractButton์œผ๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›๋Š”๋‹ค. QAbstractButton๋Š” ๋ชจ๋“  ๋ฒ„ํ„ด ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ณตํ†ต์ ์ธ ํ•จ์ˆ˜์™€ ์‹œ๊ทธ๋„์„ ์ •์˜ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์ฃผ์š” ํ•จ์ˆ˜๋กœ๋Š” setText(str), setIcon(icon) ๋“ฑ์ด ์žˆ๊ณ , ์‹œ๊ทธ๋„๋กœ๋Š” clicked(), pressed(), released(), toggled(bool) ๋“ฑ์ด ์žˆ๋‹ค. ๋‹ค์Œ์€ ๊ธฐ๋ณธ ๋ฒ„ํ„ด์ธ QPushButton์„ ์‚ฌ์šฉํ•œ ์˜ˆ์ด๋‹ค. self.button = QPushButton("&Ok",self) self.button.addIcon(QIcon(":/images/apply.png") self.button.clicked.connect(self.okButtonClicked) QCheckBox์—์„œ๋Š” clicked() ์‹œ๊ทธ๋„๋ณด๋‹ค๋Š” toggled(bool) ์‹œ๊ทธ๋„์„ ์—ฐ๊ฒฐํ•ด ์‚ฌ์šฉํ•œ๋‹ค. self.button = QCheckBox("&Case Sensitivity",self) self.button.toggled.connect(onCaseSensity) QRadioButton์€ ์„œ๋กœ ๋ฐฐํƒ€์ ์ธ ์„ ํƒ์ด ๊ฐ€๋Šฅํ•œ ๋ฒ„ํ„ด์„ ์˜๋ฏธํ•œ๋‹ค. QRadioButton์€ ๊ฐ™์€ ๋ถ€๋ชจ ๊ฐ์ฒด๋ฅผ ๊ฐ–๋Š” ๋ผ๋””์˜ค ๋ฒ„ํ„ด๋ผ๋ฆฌ ์„œ๋กœ ๋ฐฐํƒ€์ ์ธ ์„ ํƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ„๋„ ์ž‘์—… ์—†์ด ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ๋ฐฐํƒ€์ ์ธ ์„ ํƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. box = QGroupBox("Sex",self) self.button1 = QRadioButton("Male",box) button2 = QRadioButton("Female",box) self.button1.toggled.connect(self.onMale) ์ด ์ฝ”๋“œ์—์„œ ๋‘ ๋ฒ„ํ„ด์€ QGroupBox๋ฅผ ๊ฐ™์€ ๋ถ€๋ชจ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์„œ๋กœ ๋ฐฐํƒ€์ ์ธ ์„ ํƒ์ด ๋œ๋‹ค. ๋˜ํ•œ button1์— ๋Œ€ํ•ด์„œ๋งŒ toggled(bool) ์‹œ๊ทธ๋„์„ ์—ฐ๊ฒฐํ•ด ์ฃผ์—ˆ๋‹ค. ButtonDemo ์˜ˆ์ œ ์ด ์˜ˆ์ œ๋Š” ๋ฒ„ํ„ด ์œ„์ ฏ์„ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ด๋‹ค. ๋ฒ„ํ„ด์˜ ์‹œ๊ทธ๋„์— ๋ฐ˜์‘ํ•˜๋Š” ์Šฌ๋กฏ์„ ์ •์˜ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ฝ˜์†”์— ์ถœ๋ ฅํ•ด ์ค€๋‹ค. ButtonDemo.py from PySide2.QtWidgets import (QApplication, QWidget, QPushButton, QCheckBox, QRadioButton, QVBoxLayout, QGroupBox) import sys class MyForm(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setWindowTitle('Button Demo') self.button = QPushButton('&Ok',self) self.button.clicked.connect(self.okButtonClicked) self.checkBox = QCheckBox('&Case sensitivity',self) self.checkBox.toggled.connect(self.onCaseSensitivity) box = QGroupBox("Sex",self) self.button1 = QRadioButton("Male",box) self.button2 = QRadioButton("Female",box) self.button1.setChecked(True) groupBoxLayout = QVBoxLayout(box) groupBoxLayout.addWidget(self.button1) groupBoxLayout.addWidget(self.button2) self.button1.toggled.connect(self.onMale) mainlayout = QVBoxLayout() mainlayout.addWidget(self.button) mainlayout.addWidget(self.checkBox) mainlayout.addWidget(box) self.setLayout(mainlayout) def okButtonClicked(self): print('okButtonClicked') def onCaseSensitivity(self, toggle): print('okCaseSensitity',toggle) print(self.checkBox.isChecked()) def onMale(self, toggle): print('onMale',toggle) if __name__ == '__main__': app = QApplication(sys.argv) form = MyForm() form.show() app.exec_() 2.4 ๊ธฐ๋ณธ ๋‚ด์žฅ ์œ„์ ฏ - QLabel QLabel์€ ๋ฌธ์ž์—ด์ด๋‚˜ ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ผ๋ฒจ ์œ„์ ฏ๋กœ ๋ณดํ†ต QLineEdit๋กœ ์กฐํ•ฉํ•˜์—ฌ ์ด๋ฅผ ์„ค๋ช…ํ•˜๋Š” ํ…์ŠคํŠธ ๋ผ๋ฒจ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. textLabel = QLabel(self) textLabel.setText("&Name") self.lineEdit = QLineEdit(self) textLabel.setBuddy(self.lineEdit); ์œ„ ์ฝ”๋“œ์—์„œ ๋ผ๋ฒจ ํ…์ŠคํŠธ์˜ &๊ธฐํ˜ธ๋Š” ๋‹จ์ถ•ํ‚ค์— ๋ฐ˜์‘ํ•˜๋„๋ก ํ•œ๋‹ค. ๋ผ๋ฒจ์— QLineEdit๋‚˜ QComboBox๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋‹จ์ถ•ํ‚ค๊ฐ€ ๋ˆŒ๋ฆด ๋•Œ ๋ฒ„๋””๋กœ ์ง€์ •๋œ ์œ„์ ฏ์ด ํฌ์ปค์Šค๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ๋Š” ๋‹จ์ถ•ํ‚ค 'N'์ด ๋ˆŒ๋ฆฌ๋ฉด self.lineEdit๊ฐ€ ํฌ์ปค์Šค๊ฐ€ ๊ฐ–๋Š”๋‹ค. QLabel์— ๋ฌธ์ž์—ด์„ ์ง€์ •ํ•  ๋•Œ ๊ฐ„๋‹จํ•œ HTML ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. label = QLabel("<h2><i>Hello</i> <font color=red>Qt!</h2>",self) QLabel์€ ๋ฌธ์ž์—ด์„ ํ‘œ์‹œํ•˜๋Š” ๋ผ๋ฒจ ์ด์™ธ์—๋„ ์ด๋ฏธ์ง€ ๋ผ๋ฒจ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ์ˆœํžˆ QLabel.setPixmap() ํ•จ์ˆ˜๋กœ ํ”ฝ์Šค ๋งต์„ ์„ค์ •ํ•ด ์ฃผ๋ฉด ๋ฌธ์ž์—ด ๋Œ€์‹  ์ด๋ฏธ์ง€๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. imageLabel = QLabel(self) imageLabel.setPixmap(QPixmap("./test1.png")) # ์ปดํŒŒ์ผ ํด๋”์— ์žˆ๋Š” ๊ฒฝ์šฐ ๋˜๋Š” imageLabel.setPixmap(QPixmap(":/image/test1.png") # ๋ฆฌ์†Œ์Šค์— ์žˆ๋Š” ๊ฒฝ์šฐ QLabel์˜ ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ ์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์†์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ImageViewer ์—ฌ๊ธฐ์—์„œ๋Š” Qt ์˜จ๋ผ์ธ ์˜ˆ์ œ ์ค‘ ImageViewer๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ˆ˜์ •ํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ์ด๋ฏธ์ง€ ๋ทฐ์–ด๋ฅผ ๋งŒ๋“ค์–ด ๋ณธ๋‹ค. ImageViewer.py from PySide2.QtWidgets import (QApplication, QWidget, QVBoxLayout, QLabel, QFrame, QSizePolicy, QPushButton, QFileDialog, QMessageBox) from PySide2.QtGui import QPixmap, QImage import sys class MainWindow(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setWindowTitle('Image viewer') self.imageLabel=QLabel() self.imageLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken) self.imageLabel.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) self.imageLabel.setScaledContents(True) self.imageLabel.setPixmap(QPixmap()) openButton = QPushButton("Load image") layout = QVBoxLayout() layout.addWidget(self.imageLabel) layout.addWidget(openButton) self.setLayout(layout) openButton.clicked.connect(self.open) self.resize(QApplication.primaryScreen().availableSize()*2/5) def open(self): fileName, _ = QFileDialog.getOpenFileName(self, "Open Image File",".","Images (*.png *.xpm *.jpg)") if fileName != "": self.load(fileName) def load(self, fileName): image = QImage(fileName) if image.isNull(): QMessageBox.information(self, QApplication.applicationName(), "Cannot load "+fileName) self.setWindowTitle("Image viewer") self.setPixmap(QPixmap()) self.imageLabel.setPixmap(QPixmap.fromImage(image)) self.setWindowTitle(fileName) if __name__ == '__main__': app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() app.exec_() QWideget์„ ์„œ๋ธŒํด๋ž˜์‹ฑํ•œ MainWindow์—๋Š” ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•œ QLabel ๊ฐ์ฒด์ธ self.imageLabel, ํŒŒ์ผ ์—ด๊ธฐ ๋ฒ„ํ„ด์ด ๋ฐฐ์น˜๋œ๋‹ค. self.imageLabel์— setPixmap(pixmap) ๋ฉค๋ฒ„๋กœ ํ”ฝ์Šค ๋งต์„ ์ง€์ •ํ•˜๋Š”๋ฐ ํ”ฝ์Šค๋งต(QPximap ๊ฐ์ฒด)์€ ๋ฉ”๋ชจ๋ฆฌ์ƒ์˜ ์ด๋ฏธ์ง€์ด๋‹ค. ๋ฒ„ํ„ด์ด ๋ˆŒ๋ฆฌ๋ฉด ํŒŒ์ผ ์ด๋ฆ„์„ ์•Œ์•„๋‚ธ ๋’ค QImage ๊ฐ์ฒด(ํŒŒ์ผ ์ƒ์˜ ์ด๋ฏธ์ง€)๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋‹ค์‹œ ๋ฉ”๋ชจ๋ฆฌ์ƒ์˜ ์ด๋ฏธ์ง€์ธ ํ”ฝ์Šค ๋งต์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ self.imageLabel์— ์ง€์ •ํ•˜๋ฉด ํ™”๋ฉด์— ํ‘œ์‹œ๋œ๋‹ค. QFileDialog๋Š” ํŒŒ์ผ ์—ด๊ธฐ, ์ €์žฅ์„ ์œ„ํ•œ ํŒŒ์ผ ์ด๋ฆ„์„ ๋ฐ›์•„์˜ค๋Š” ๊ณต์šฉ ๋‹ค์ด์–ผ๋กœ๊ทธ์ด๋‹ค. QFileDialog.getOpenFileName(...)๋Š” ํŽธ์˜ ํ•จ์ˆ˜๋กœ์„œ ์ •์  ํด๋ž˜์Šค ๋ฉค๋ฒ„ ํ•จ์ˆ˜์ด๋‹ค. fileName, selectedFilter = QFileDialog.getOpenFileName(parent, title, dir, filters,...) QMessageBox๋Š” ํ™”๋ฉด์— ๊ฐ„๋‹จํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•˜๋Š” ํด๋ž˜์Šค๋กœ ํ‘œ์‹œ๋˜๋Š” ๋ฉ”์‹œ์ง€์˜ ์œ ํ˜•์— ๋”ฐ๋ผ information(...) , question(...) , warning(...) , critical(...) ๋“ฑ์˜ ์ •์  ํด๋ž˜์Šค ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ํ˜•ํƒœ์˜ ํŽธ์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. information(...)์€ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์šฉ๋„์˜ ๋ฉ”์‹œ์ง€ ๋ฐ•์Šค๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. 2.5 ๊ธฐ๋ณธ ๋‚ด์žฅ ์œ„์ ฏ - QLineEdit์™€ QComboBox QLineEdit QLineEdit๋Š” ํ•œ ์ค„์งœ๋ฆฌ ์—๋””ํ„ฐ ์œ„์ ฏ์œผ๋กœ ๋ณดํ†ต textChanged() ์‹œ๊ทธ๋„์— ์ปค์Šคํ†ฐ ์Šฌ๋กฏ์„ ์—ฐ๊ฒฐํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค. setReadOnly(bool)๋ฅผ ํ†ตํ•ด ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, setEchoMode()๋กœ ํŒจ์Šค์›Œ๋“œ ์ž…๋ ฅํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜๋„ ์žˆ๋‹ค. setPlaceholderText()๋กœ ์–ด๋–ค ํ…์ŠคํŠธ๋ฅผ ํ‘œ์‹œ๋˜๊ฒŒ ํ•˜๊ณ  ์‹ค์ œ ์ž…๋ ฅํ•  ๋•Œ๋Š” ๊ทธ ํ…์ŠคํŠธ๊ฐ€ ์—†์–ด์ง€๋„๋ก(์ฆ‰, ๋ฏธ๋ฆฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ์–ด๋–ค ํžŒํŠธ๋ฅผ ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋„๋ก) ํ•  ์ˆ˜ ์žˆ๋‹ค. self.lineEdit = QLineEdit(self) self.lineEdit.setReadOnly(True) self.passwordEdit = QLineEdit(self) self.passwordEdit.setPlaceholderText("Set your password") self.passwordEdit.setEchMode(QLineEdit.Password); textChanged() ์‹œ๊ทธ๋„ ์ด์™ธ์— ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์‹œ๊ทธ๋„๋กœ editingFinished()์™€ returnPressed()์ด๋‹ค. ๋‘˜ ๋‹ค ๋ฆฌํ„ด/์—”ํ„ฐํ‚ค๋ฅผ ๋ˆ„๋ฅผ ๋•Œ ๋ฐœ์ƒ๋˜๋ฉฐ, editionFinished()๋Š” ํฌ์ปค๋ฅผ ์žƒ์„ ๋•Œ์—๋„ ๋ฐœ์ƒ๋œ๋‹ค. QComboBox QComboBox๋Š” QLineEdit์™€ ๋“œ๋กญ ๋‹ค์šด ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ•ฉ์ณ์ง„ ํ˜•ํƒœ์ด๋‹ค. QCombBox์—์„œ๋Š” currrentIndexChanged(), editTextChanged() ์‹œ๊ทธ๋„์ด ํ”ํžˆ ์‚ฌ์šฉ๋œ๋‹ค. addItem() ๋“ฑ์œผ๋กœ ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ณ , ๋””ํดํŠธ๋กœ ์—๋””ํŒ…์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ ๋ณดํ†ต currentIndexChanged() ์‹œ๊ทธ๋„๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค. ๋‹ค์Œ์€ setEditable(True)๋ฅผ ํ˜ธ์ถœํ•ด ์—๋””ํŒ…์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค์ •ํ•œ ์ฝค๋ณด ๋ฐ•์Šค์ด๋‹ค. self.comboBox = QComboBox(self) self.comboBox.addItem("Apple") self.comboBox.addItem("StrawBerry") self.comboBox.addItem("Water Melon") self.comboBox.setEditable(True) comboBox.currentIndexChanged.connect(self.onSelected) # QComboBox.currentIndexChanged(str) -- self.onSelected(str) currentIndexChanged(int)๋กœ ์ „๋‹ฌ๋˜๋Š” ์ธ๋ฑ์Šค๋‚˜ currentIndex() ํ•จ์ˆ˜๋กœ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ธ๋ฑ์Šค๋Š” ํ˜„์žฌ ์•„์ดํ…œ์˜ ์ธ๋ฑ์Šค์ด๋‹ค. clear() ๋“ฑ์„ ์ˆ˜ํ–‰ํ•œ ํ›„์—๋Š” -1์ด ๋œ๋‹ค. QComboBox์˜ ๋‚ด์šฉ์ด ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ๋„์ค‘ ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ์—๋Š” clear(), addItem()์„ ์ ์ ˆํ•ด ์กฐํ•ฉํ•˜๋ฉด ๋œ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ๋ณดํ†ต ์•„์ดํ…œ์˜ ๋‚ด์šฉ์— ๋งž๋„๋ก QComboBox์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. self.comboBox = QComboBox(self) self.comboBox.setSizeAdjustPolocy(QComboBox.AdjustToContents) self.comboBox.currentIndexChanged.connect(onComboBoxChanged) # QComboBox.currentIndexChanged(int) -- self.onComboBoxChanged(int) self.combBox.addItem("Apple") # ์ดˆ๊ธฐ ์•„์ดํ…œ ์„ค์ • .... // ์•„์ดํ…œ์ด ๋ฐ”๋€” ๋•Œ self.comboBox.clear() # ์ด๋•Œ๋„ currentIndexChanged() ์‹œ๊ทธ๋„ ๋ฐœ์ƒํ•จ. index = -1์ž„์— ์ฃผ์˜ self.comboBox.addItem("Graph") ... ๋ฐธ๋ฆฌ๋ฐ์ดํ„ฐ QLineEdit์™€ QComboBox๋กœ ์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•œ ์œ ํšจ ๋ฌธ์ž์—ด์„ ๋ฐธ๋ฆฌ ๋ฐ์ดํ„ฐ(QValidator)๋กœ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค. Qt๋Š” QIntValidator, QDoubleValidor, QRegExpValidator ๋“ฑ์˜ 3 ์ข…๋ฅ˜์˜ ๋ฐธ๋ฆฌ ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฐธ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•œ ํ›„ QLineEdit๋‚˜ QComboBox์˜ setValidator(validator) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ์ฃผ๋ฉด ๋œ๋‹ค. ๋‹ค์Œ์€ QIntValidator์™€ QDoubleValidator๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์ด๋‹ค. self.lineEdit.setValidator(QIntValidator(self)) # ์ •์ˆ˜ self.lineEdit.setValidator(QIntValidator(100,999, self)) # 100.. 999์‚ฌ์ด์˜ ์ •์ˆ˜ self.lineEdit.setValidator(QDoubleValidator(self)) # ์‹ค์ˆ˜, 1.2, -1.3, 1E-2 ๋“ฑ ๊ฐ€๋Šฅ self.lineEdit.setValidator(QDoubleValidator(-0.1, 100, 2, self)) # -0.1, 100 ์‚ฌ์ด์˜ ์‹ค์ˆ˜, 2๊ฐœ์˜ ์†Œ์ˆ˜ ์ž๋ฆฟ์ˆ˜๋งŒ ํ—ˆ์šฉ. QIntValidator์™€ QDoubleValidator๋Š” ์ƒ์„ฑ์ž ๋˜๋Š” setRange() ํ•จ์ˆ˜๋กœ ์ตœ์†Œ, ์ตœ๋Œ€, ์†Œ์ˆ˜์ (QDoubleValidator์ธ ๊ฒฝ์šฐ์—๋งŒ)์„ ๋™์‹œ์— ์„ค์ •ํ•œ๋‹ค. ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ ๋ณ„๋„๋กœ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์žˆ๋Š” ๋ฐ ์ด๋•Œ๋Š” setBottom(), setTop(), setDecimals()(QDoubleValidator์ธ ๊ฒฝ์šฐ์—๋งŒ)์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 0. ์ด์ƒ์œผ๋กœ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. validator = QDoubleValidator(self) validator.setBottom(0.) self.lineEdit.setValidator(validator) ์ •๊ทœ์‹์„ ์‚ฌ์šฉํ•˜๋Š” QRegExpValidator๋Š” ๋ณต์žกํ•œ ํŒจํ„ด์„ ์ง€์›ํ•˜๋Š” ์ •๊ทœ์‹ ํด๋ž˜์Šค QRegExp๋ฅผ ์ด์šฉํ•œ๋‹ค. regExp = QRegExp("[A-Za-z][1-9][0-9]{0,2}") self.lineEdit.setValidator(QRegExpValidator(regExp, self)) ์ฝ”๋“œ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋Š” ๋Œ€์†Œ๋ฌธ์ž์˜ ์•ŒํŒŒ๋ฒณ์ด๊ณ , ๋‘ ๋ฒˆ์งธ๋Š” 1-9์‚ฌ์ด์˜ ์ˆซ์ž๊ฐ€, ์ดํ›„ 0-9์‚ฌ์ด์˜ ์ˆซ์ž๊ฐ€ 0๊ฐœ๋ถ€ํ„ฐ 2๊ฐœ๊นŒ์ง€ ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก lineEdit์˜ ์ž…๋ ฅ์„ ์ œํ•œํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. []๋กœ ๋‘˜๋Ÿฌ์‹ผ ๋ถ€๋ถ„์ด ํ•˜๋‚˜์˜ ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•˜๊ณ  {}๋Š” ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ๋‹ค์Œ์€ ๊ฐ„๋‹จํ•œ QRegExp์˜ ์˜ˆ์ด๋‹ค. regExp = QRegExp(โ€œ[A-Za-z]*") # ์•ŒํŒŒ๋ฒณ๋งŒ ์ž„์˜์˜ ๊ฐœ์ˆ˜๋งŒํผ ๊ฐ€๋Šฅ regExp = QRegExp("[0-9]*") # ์ˆซ์ž๋งŒ ์ž„์˜์˜ ๊ฐœ์ˆ˜๋งŒํผ ๊ฐ€๋Šฅ regExp = QRegExp("[A-Za-z0-9]*") # ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž๋งŒ ์ž„์˜์˜ ๊ฐœ์ˆ˜๋งŒํผ ๊ฐ€๋Šฅ *๋Š” {}์™€ ๋‹ฌ๋ฆฌ ์ž„์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์„ ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜์—ˆ๋‹ค. QLineEdit๋‚˜ QComboBox์— ๋ฐธ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์€ Qt Designer์—์„œ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. 2.6 ๊ธฐ๋ณธ ๋‚ด์žฅ ์œ„์ ฏ - QSpinBox, QSlider, QProgressBar QSpinBox๋Š” ์Šคํ•€ ๋ฐ•์Šค๋ฅผ, QSlider๋Š” ์ˆ˜ํ‰ ๋˜๋Š” ์ˆ˜์ง ์Šฌ๋ผ์ด๋”๋ฅผ, QProgressBar๋Š” ์ง„ํ–‰์‚ฌํ•ญ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. QSpinBox QSpinBox๋Š” setRange(int, int)๋กœ ๋ฒ”์œ„๋กœ ์ง€์ •ํ•œ๋‹ค. setSuffix(str)์œผ๋กœ ์ ‘๋‘์–ด๋ฅผ ๋ถ™์ผ ์ˆ˜ ์žˆ๊ณ , setSingStep(int)์œผ๋กœ ์Šคํ… ์‚ฌ์ด์ฆˆ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. setValue(int)๋กœ ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, value()๋กœ ๊ฐ’์„ ์–ป์–ด๋‚ธ๋‹ค. setValue(int)๋Š” ์Šฌ๋กฏ ํ•จ์ˆ˜์ด๋ฉฐ, ๋Œ€ํ‘œ์ ์ธ ์‹œ๊ทธ๋„์€ valueChanged(int)์ด๋‹ค. ๋‹ค์Œ์€ QSpinBox๋ฅผ ์ƒ์„ฑํ•œ ์ผ๋ฐ˜์ ์ธ ์ฝ”๋“œ์ด๋‹ค. spin = QSpinBox(self) self.spin.setRange(20,30) self.spin.setSuffix(" km") self.spin.setSingleStep(2) self.spin.setValue(24) self.spin.valueChanged.connect(self.do_something) QSlider QSlider๋Š” ์ƒ์„ฑ์ž์—์„œ ๊ฐ€๋กœ ์„ธ๋กœ ๋ฐฐ์น˜ ํ˜•์ƒ์„ Qt.Orientation ์ƒ์ˆ˜(Qt.Horizontal ๋˜๋Š” Qt.Vertical)์— ๋‹ด์•„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. self.slider = QSlider(Qt.Horizontal, this); self.slider = QSlider(self); // QSlider(Qt.Vertical, self)์™€ ๋™์ผ QSlider์˜ ์ฃผ์š” ํ•จ์ˆ˜๋“ค์€ QSpinBox์™€ ์•„์ฃผ ์œ ์‚ฌํ•œ๋ฐ setRange(min, max), setSingleStep(int), setValue(int), value(), valueChanged(int) ๋“ฑ์ด ๋™์ผํ•œ ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. QProgressBar QProgressBar๋Š” ๋ณดํ†ต ๊ธด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ง„ํ–‰ ์ƒํ™ฉ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ QSlider์™€ ๊ฐœ๋…์ด ๊ฐ™์ง€๋งŒ ์œ„์ ฏ ์Šค์Šค๋กœ ์ž…๋ ฅ์„ ๋ฐ›์ง€ ๋ชปํ•œ๋‹ค. ๋ฒ”์œ„๋Š” setRange(min, max)๋กœ, ์ˆ˜ํ‰ ๋˜๋Š” ์ˆ˜์ง ๋ฐฐ์น˜ ํ˜•์ƒ์€ setOrientation(orient)๋กœ, ์ •๋ ฌ์€ setAlignment(alignment)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. setFormat(format)์„ ์ด์šฉํ•ด ํ‘œ์‹œ๋˜๋Š” ํ…์ŠคํŠธ์˜ ํฌ๋งท์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. setValue(int) ์Šฌ๋กฏ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ํ˜„์žฌ์˜ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค. self.progressBar = QProgressBar(self) self.progressBar.setOrientation(Qt.Vertical) # ๋””ํดํŠธ๊ฐ€ Qt.Horizontal self.progressBar.setAlignment(Qt.AlignCenter) # ์ค‘๊ฐ„์— ํ‘œ์‹œ๋˜๋Š” ํ…ํŠธ๊ฐ€ ์ค‘์•™์— ๋‚˜ํƒ€๋‚จ self.progressBar.setRange(0,100) self.progressBar.setFormat("%v km") # 10 km ํ˜•ํƒœ๋กœ ํ‘œ์‹œ, ๋””ํดํŠธ๋Š” %p self.someSignal.connect(self.progressBar.setValue) setFormat(format)์—์„œ fomat์„ ๋ฌธ์ž์—ด๋กœ ์ง€์ •ํ•  ๋•Œ๋Š” %p, %v, %m๋ฅผ ์ด์šฉํ•˜๋Š”๋ฐ, ๊ฐ๊ฐ ์ฃผ์–ด์ง„ ๋ฒ”์œ„์— ๋Œ€ํ•œ ํผ์„ผํŠธ, ์‹ค์ œ ๊ฐ’, ์Šคํ… ์ˆ˜๋ฅผ ๋Œ€ํ‘œํ•œ๋‹ค. SpinSliderProgressDemo SpinSliderProgressDemo๋Š” ์„ธ ์œ„์ ฏ์„ ์กฐํ•ฉํ•˜์—ฌ ๋งŒ๋“  ๊ฐ„๋‹จํ•œ ์˜ˆ์ด๋‹ค. QSpin๊ณผ QSlider์— ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ๋‚˜๋จธ์ง€ ์œ„์ ฏ๊นŒ์ง€ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋„๋ก ์‹œ๊ทธ๋„/์Šฌ๋กฏ ์—ฐ๊ฒฐ์„ ํ•˜์˜€๋‹ค. ์†Œ์Šค ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. SpinSliderProgressDemo.py import sys from PySide2.QtWidgets import (QApplication, QWidget, QSpinBox, QSlider, QProgressBar, QHBoxLayout) from PySide2.QtCore import Qt if __name__ == '__main__': app = QApplication(sys.argv) form = QWidget() spin = QSpinBox() spin.setRange(0,100) slider = QSlider(Qt.Horizontal) slider.setRange(0,100) progressBar = QProgressBar() progressBar.setAlignment(Qt.AlignCenter) progressBar.setRange(0,100) spin.valueChanged.connect(slider.setValue) # valuChanged(int), setValue(int) slider.valueChanged.connect(spin.setValue) spin.valueChanged.connect(progressBar.setValue) layout = QHBoxLayout() layout.addWidget(spin) layout.addWidget(slider) layout.addWidget(progressBar) form.setLayout(layout) form.setWindowTitle('SpinSliderProgressDemo') form.show() app.exec_() 2.7 ๊ธฐ๋ณธ ๋‚ด์žฅ ์œ„์ ฏ - QGroupBox์™€ QFrame QGroupBox QGroupBox๋Š” ๋‹ค๋ฅธ ์œ„์ ฏ์„ ๋ฌถ์–ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ์ผ์ข…์˜ ์ปจํ…Œ์ด๋„ˆ ์œ„์ ฏ์ด๋‹ค. ๊ทธ๋ฃน ๋ฐ•์Šค ๋‚ด์— ๋“ค์–ด๊ฐ€๋Š” ์œ„์ ฏ์€ ๊ทธ๋ฃน ๋ฐ•์Šค์˜ ์ž์‹ ์œ„์ ฏ์œผ๋กœ ์„ค์ •ํ•˜๊ณ , ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๋กœ ์ž์‹ ์œ„์ ฏ์„ ๋ฐฐ์น˜ํ•ด ์ค€๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ๋ฃน ๋ฐ•์Šค์˜ ๋ณด์ด๊ธฐ ์ƒํƒœ(show ๋˜๋Š” hide), ํ™œ์„ฑํ™” ์ƒํƒœ(enabled/disabled)๊ฐ€ ๊ทธ๋ฃน ๋ฐ•์Šค ๋‚ด์˜ ๋ชจ๋“  ์œ„์ ฏ์— ์ ์šฉ๋œ๋‹ค. self.appleCheckBox = QCheckBox("apple") self.grapeCheckBox = QCheckBox("grape") layout = QVBoxLayout() layout.addWidget(appleCheckBox) layout.addWidget(grapeCheckBox) self.groupBox = QGroupBox("title",self) self.groupBox.setLayout(layout) # appleCheckBox, grapeCheckBox๋Š” ์ž์‹ ์œ„์ ฏ์ด ๋ฉ ํ•œํŽธ ๊ทธ๋ฃน ๋ฐ•์Šค๋Š” setCheckable(True)๋กœ ์ฒดํฌ ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ๊ฒฝ์šฐ setChecked(bool) ์Šฌ๋กฏ๊ณผ toggled(bool) ์‹œ๊ทธ๋„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ์ฒดํฌ๊ฐ€ ๋œ ์ƒํƒœ์ด๋ฉด ๊ทธ๋ฃน ๋ฐ•์Šค๊ฐ€ ํ™œ์„ฑํ™”(enabled) ๋˜์ง€๋งŒ, ์ฒด๊ฐ€๊ฐ€ ํ•ด์ œ๋˜๋ฉด ๊ทธ๋ฃน ๋ฐ•์Šค๊ฐ€ ๋น„ํ™œ์„ฑํ™”(disabled) ๋œ๋‹ค. ์ด๋•Œ ๊ทธ๋ฃน ๋ฐ•์Šค ๋‚ด์˜ ๋ชจ๋“  ์ž์‹ ์œ„์ ฏ๋„ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋กœ๋ถ€ํ„ฐ ํ™œ์„ฑ/๋น„ํ™œ์„ฑํ™”๋œ๋‹ค. QFrame QFrame์€ ํ”„๋ ˆ์ž„์„ ๋ถ€๊ณผ๋œ QWidget์ด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. QMenu, QLabel, QTextEdit ๋“ฑ ํ”„๋ ˆ์ž„์ด ์žˆ๋Š” ์œ„์ ฏ๋“ค์€ QFrame์˜ ์ž์‹ ํด๋ž˜์Šค์ด๋‹ค. ์ง์ ‘ QFrame์„ QWidget์ฒ˜๋Ÿผ ์ปจํ…Œ์ด๋„ˆ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค. setFrameStyle()์ด๋‚˜ setLineWidth() ๋“ฑ์ด ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์˜๋“œ์ด๋‹ค. ImageViewer ์˜ˆ์ œ์—์„œ QLabel ํด๋ž˜์Šค๋ฅผ ๋Œ€์ƒ์œผ๋กœ setFrameStyle()์„ ์‚ฌ์šฉํ•œ ์ ์ด ์žˆ๋‹ค. QLabel์€ QFrame์˜ ์ž์‹ ์œ„์ ฏ์ธ๋ฐ, QFrame์€ QWidget์˜ ์ž์‹ ํด๋ž˜์Šค๋กœ ํ”„๋ ˆ์ž„์ด ์žˆ๋Š” QWidget ์ •๋„๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ๋กœ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ง์ ‘ ์ฝ”๋”ฉ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค๋Š” Qt Designer์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ๋‹ค. 2.8 ๋‚ด์žฅ ์œ„์ ฏ ์„œ๋ธŒํด๋ž˜์‹ฑ ์ปค์Šคํ…€ ์œ„์ ฏ์€ Qt๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋‚ด์žฅ ์œ„์ ฏ(built-in widget)์„ ์„œ๋ธŒํด๋ž˜์‹ฑํ•ด์„œ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋ฒˆ ์ ˆ์—์„œ๋Š” QLineEdit๋ฅผ ์„œ๋ธŒํด๋ž˜์‹ฑํ•˜์—ฌ ํฌ์ปค์Šค๋ฅผ ๊ฐ€์งˆ ๋•Œ ๋ฐฐ๊ฒฝ์ƒ‰์ด ๋ฐ”๋€Œ๋Š” ์œ„์ ฏ์„ ๋งŒ๋“ค์–ด ๋ด…๋‹ˆ๋‹ค. ์˜ˆ์ œ ํ”„๋กœ๊ทธ๋žจ ๋ช…์นญ์€ LineEdit์ด๋‹ค. LineEdit.py from PySide2.QtWidgets import QApplication, QLineEdit, QWidget, QVBoxLayout from PySide2.QtGui import QPalette, QColor import sys class LineEdit(QLineEdit): def __init__(self, parent=None): QLineEdit.__init__(self, parent) self.clearOnFocus = False self.originalPalette = self.palette() self.newPalette = QPalette(self.palette()) # copy constructor self.newPalette.setColor(QPalette.Normal, QPalette.Base, QColor(200,255,125)) def setColorOnFocus(self, color): self.newPalette.setColor(QPalette.Normal, QPalette.Base, color) def setClearOnFocus(self, clear): self.clearOnFocus = clear def focusInEvent(self, e): self.setPalette(self.newPalette) if self.clearOnFocus : self.clear() QLineEdit.focusInEvent(self, e) def focusOutEvent(self, e): self.setPalette(self.originalPalette) QLineEdit.focusOutEvent(self, e) if __name__ == '__main__': app = QApplication(sys.argv) mainWindow = QWidget() lineEditId = LineEdit(mainWindow) lineEditPW = LineEdit(mainWindow) lineEditPW.setEchoMode(QLineEdit.Password) lineEditPW.setClearOnFocus(True) layout = QVBoxLayout() layout.addWidget(lineEditId) layout.addWidget(lineEditPW) mainWindow.setLayout(layout) mainWindow.setWindowTitle("Line Edit") mainWindow.show() app.exec_() LineEdit ํด๋ž˜์Šค๋Š” QLinEdit๋ฅผ ์„œ๋ธŒํด๋ž˜์‹ฑํ•˜์˜€๋‹ค. ๋ผ์ธ ์—๋””ํŠธ ์œ„์ ฏ์€ ํฌ์ปค์Šค๋ฅผ ๊ฐ€์ง€๋ฉด ๋ฐฐ๊ฒฝ ์ƒ‰์ƒ์ด ์—ฐ๋…น์ƒ‰์œผ๋กœ ๋ฐ”๋€Œ๊ณ , ์žƒ์œผ๋ฉด ์›๋ž˜ ์ƒ‰์ƒ์œผ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค. ์œ„์ ฏ์˜ ๋ฐฐ๊ฒฝ ์ƒ‰์ƒ์€ QPalette๋กœ ํ‘œํ˜„๋˜๋Š” ํŒ”๋ ˆํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ์„ฑ์ž์—์„œ ์›๋ž˜์˜ ํŒ”๋ ˆํŠธ๋ฅผ ์ €์žฅํ•˜๊ณ , ํฌ์ปค์Šค๋ฅผ ์–ป๊ณ  ์žƒ์„ ๋•Œ ํŒ”๋ ˆํŠธ๋ฅผ ์Šค์œ„์นญ ํ•ด์ค€๋‹ค. focusInEvent(self, event), focusOutEvent(self, event)๋Š” ํฌ์ปค์Šค๋ฅผ ์–ป๊ณ  ์žƒ์„ ๋•Œ ๋ฐœ์ƒ๋˜๋Š” ์ด๋ฒคํŠธ์— ๋ฐ˜์‘ํ•˜๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์ด๋‹ค. ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด์—์„œ ํŒ”๋ ˆํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ํ›„ ์ž์‹ ์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค์ธ QLineEdit์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์›๋ž˜ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ์ž‘์—…์ด ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. 3. ๋‹ค์ด์–ผ๋กœ๊ทธ ๋‹ค์ด์–ผ๋กœ๊ทธ๋Š” QWidget์˜ ์ž์‹ ํด๋ž˜์Šค์ธ QDialog๋กœ ์ถ”์ƒํ™”๋˜์–ด ์žˆ๋‹ค. ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์€ QDialog๋ฅผ ์„œ๋ธŒํด๋ž˜์‹ฑํ•œ ์ปค์Šคํ…€ ๋‹ค์ด์–ผ๋กœ๊ทธ์— ์ž์‹ ์œ„์ ฏ์„ ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜๋ฉด QWidget์— ์ž์‹ ์œ„์ ฏ์„ ๋ฐฐ์น˜ํ•˜์—ฌ ์ปค์Šคํ…€ ์œ„์ ฏ์„ ๋งŒ๋“  ๊ฒƒ๊ณผ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์—ญ์‹œ ์ฝ”๋“œ๋กœ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๊ณ , Qt Designer๋กœ ํผ(form)์„ ๋งŒ๋“ค์–ด ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. QWidget์„ ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ ๊ฐ€์ง€๋Š” ์ผ๋ฐ˜ ์œ„์ ฏ์€ ๋‹ค๋ฅธ ์œ„์ ฏ์˜ ๊ตฌ์„ฑ์š”์†Œ๋กœ ํฌํ•จ๋  ์ˆ˜ ์žˆ๊ณ , ๊ทธ ์œ„์ ฏ ์œ„์— ํ•ญ์ƒ ๋ถ™์–ด ์žˆ๊ฒŒ e ๋œ๋‹ค. ๋ฐ˜๋ฉด์— QDialog๋ฅผ ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ ํ•˜๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ๋Š” ๋ถ€๋ชจ ์œ„์ ฏ์ด ์žˆ๋”๋ผ๋„ ํ•ญ์ƒ ํŒ์—… ๋˜์–ด ๋‚˜ํƒ€๋‚ด๊ณ , ๋‹ค๋ฅธ ์œ„์ ฏ์˜ ๊ตฌ์„ฑ์š”์†Œ๋กœ ํฌํ•จ๋  ์ˆ˜ ์—†๋‹ค. ์ •๋ฆฌํ•˜๋ฉด QWidget์„ ์ƒ์†ํ•˜์—ฌ ๋งŒ๋“  ์œ„์ ฏ์€ ์ผ์ข…์˜ ๊ธฐ๋ณธ ๋ธ”๋ก(building block)์ด๊ณ , ๋‹ค์ด์–ผ๋กœ๊ทธ๋Š” ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ํ•˜๋‚˜์˜ ๋…๋ฆฝ๋œ ๋‹จ์œ„์ด๋‹ค. ๋‹ค์ด์–ผ๋กœ๊ทธ๋Š” ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋ชจ๋‹ฌ(modal) ๋˜๋Š” ๋ชจ๋œ๋ฆฌ์Šค(modeless) ๋‹ค์ด์–ผ๋กœ๊ทธ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค. ๋ชจ๋‹ฌ ๋‹ค์ด์–ผ๋กœ๊ทธ๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ๊ฐ€ ํ™œ์„ฑํ™”๋œ ํ›„ ๋‹ซ์„ ๋•Œ๊นŒ์ง€ ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ์ด๋‹ค. ๋ชจ๋œ๋ฆฌ์Šค ๋‹ค์ด์–ผ๋กœ๊ทธ๋Š” ํ™œ์„ฑํ™”ํ•œ ์ฑ„๋กœ ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ์ด๋‹ค. ํ…์ŠคํŠธ ํŽธ์ง‘ ํ”„๋กœ๊ทธ๋žจ์— ์žˆ๋Š” ํŒŒ์ผ ์—ด๊ธฐ ๋‹ค์ด์–ผ๋กœ๊ทธ๊ฐ€ ๋ชจ๋‹ฌ ๋‹ค์ด์–ผ๋กœ๊ทธ์˜ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ด๊ณ , ์ฐพ๊ธฐ ๋‹ค์ด์–ผ๋กœ๊ทธ๊ฐ€ ๋ชจ๋‹ฌ๋ฆฌ์Šค ๋‹ค์ด์–ผ๋กœ๊ทธ์˜ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ด๋‹ค. ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ ๋ชจ๋‹ฌ์ด๋ƒ ๋ชจ๋‹ฌ๋ฆฌ์Šค๋ƒ์— ๋”ฐ๋ผ ์ฐจ์ด๊ฐ€ ๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ถ€๋ถ„์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ์—ผ๋‘์— ๋‘๊ณ  ์„ค๊ณ„ํ•ด์•ผ ํ•œ๋‹ค. 3.1 ๋ชจ๋‹ฌ ๋‹ค์ด์–ผ๋กœ๊ทธ ๋ชจ๋‹ฌ(modal) ๋‹ค์ด์–ผ๋กœ๊ทธ๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ๊ฐ€ ํ™œ์„ฑํ™”๋œ ํ›„ ๋‹ซ์„ ๋•Œ๊นŒ์ง€ ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ๋กœ ๋ณดํ†ต Ok, Cancel ๋ฒ„ํ„ด์„ ๊ฐ€์ง„๋‹ค. Ok ๋ฒ„ํ„ด์—์„œ๋Š” QDialog์—์„œ ์ œ๊ณตํ•˜๋Š” accept() ์Šฌ๋กฏ์„ ์—ฐ๊ฒฐํ•˜๊ณ , Cancel ๋ฒ„ํ„ด์—์„œ๋Š” reject() ์Šฌ๋กฏ์„ ์—ฐ๊ฒฐํ•˜์—ฌ ์ œ์ž‘๋œ๋‹ค. ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ QDialog.exec_() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ์ˆ˜ํ–‰์ด ๋๋‚œ ํ›„ ๋‹ค์ด์–ผ๋กœ๊ทธ๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ์ฝ์–ด๋“ค์ด๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. class MyDialog(QDialog): def __init__(self, parent=None): QDialog.__init__(self, parent) # ๋˜๋Š” super().__init__(parent) # okButton, cancelButton ์ƒ์„ฑ ... okButton.clicked.connect(self.accept) # accept() ์Šฌ๋กฏ์— ์—ฐ๊ฒฐ cancelButton.clicked.connect(self.reject) # reject() ์Šฌ๋กฏ์— ์—ฐ๊ฒฐ ... # ์‚ฌ์šฉํ•˜๋Š” ์ชฝ myDialog = MyDialog(self) if myDialog.exec_(): ... # okButton ์ด ๋ˆŒ๋ฆด ๋•Œ์˜ ์ฒ˜๋ฆฌ else: .... # cancelButton์ด ๋ˆŒ๋ฆด ๋•Œ์˜ ์ฒ˜๋ฆฌ ๋‹ค์ด์–ผ๋กœ๊ทธ๋Š” ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ์‹๊ณผ Qt Designer๋ฅผ ์ด์šฉํ•œ ํผ์„ ์ด์šฉํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. GridDialog1 ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ GridDialog๋ผ๋Š” ์ปค์Šคํ…€ ๋ชจ๋‹ฌ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ณ , ๋ชจ๋‹ฌ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ํ˜ธ์ถœํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃจ๊ธฐ๋กœ ํ•œ๋‹ค. ๊ทธ๋ฆผ์—์„œ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜๋ฉด GridDialog๊ฐ€ ๋‚˜ํƒ€๋‚˜๊ณ  Ok๋ฅผ ์„ ํƒํ•˜๋ฉด ํ™”๋ฉด์— ์„ ํƒ๋œ ์˜ต์…˜๋“ค์ด ๋‚˜ํƒ€๋‚œ๋‹ค. ์ด๋ฏธ์ง€ ์ค€๋น„ (images ๋””๋ ‰ํ„ฐ๋ฆฌ์— ok.png, cancel.png) ๋ฆฌ์†Œ์Šค ํŒŒ์ผ GridDialog.rc <RCC> <qresource prefix="/"> <file>images/ok.png</file> </qresource> </RCC> ๋ฆฌ์†Œ์Šค ์ปดํŒŒ์ผ > pyside2-rcc -o GridDialog_rc.py -py3 GridDialog.qrc ๋‹ค์Œ์€ ์ฝ”๋“œ์ด๋‹ค. GridDialog1.py from PySide2.QtWidgets import (QApplication, QMainWindow, QTextEdit, QAction, QMenu, QMenuBar, QDialog, QLabel, QLineEdit, QGroupBox, QCheckBox, QPushButton, QGridLayout, QHBoxLayout, QVBoxLayout) from PySide2.QtGui import QIcon, QDoubleValidator import GridDialog_rc import sys class GridDialog(QDialog): def __init__(self, x, y, useGrid, useSnap, parent): QDialog.__init__(self, parent) xLabel = QLabel("&X:") yLabel = QLabel("&Y:") self.xLineEdit = QLineEdit(str(x)) self.yLineEdit = QLineEdit(str(y)) xLabel.setBuddy(self.xLineEdit) yLabel.setBuddy(self.yLineEdit) validator = QDoubleValidator(self) validator.setBottom(0.) self.xLineEdit.setValidator(validator) self.yLineEdit.setValidator(validator) self.useGridGroupBox = QGroupBox("Use &Grid") self.useGridGroupBox.setCheckable(True) self.useGridGroupBox.setChecked(useGrid) self.useSnapCheckBox = QCheckBox("Use &Snap") self.useSnapCheckBox.setChecked(useSnap) self.useSnapCheckBox.setEnabled(useGrid) okButton = QPushButton("&Ok") okButton.setIcon(QIcon(":/ok.png")) okButton.setDefault(True) cancelButton = QPushButton("&Cancel") cancelButton.setIcon(QIcon(":/cancel.png")) gridLayout = QGridLayout() gridLayout.addWidget(xLabel, 0,0) gridLayout.addWidget(self.xLineEdit, 0,1) gridLayout.addWidget(yLabel, 1,0) gridLayout.addWidget(self.yLineEdit, 1,1) self.useGridGroupBox.setLayout(gridLayout) buttonLayout = QHBoxLayout() buttonLayout.addWidget(okButton) buttonLayout.addWidget(cancelButton) mainLayout = QVBoxLayout() mainLayout.addWidget(self.useGridGroupBox) mainLayout.addWidget(self.useSnapCheckBox) mainLayout.addLayout(buttonLayout) self.setLayout(mainLayout) self.setWindowTitle('Set Grid') # toggled(bool) - setEnabled(bool) self.useGridGroupBox.toggled.connect(self.useSnapCheckBox.setEnabled) okButton.clicked.connect(self.accept) # clicked() - accept() cancelButton.clicked.connect(self.reject) # clicked() - reject() def gridInfo(self): x = float(self.xLineEdit.text()) y = float(self.yLineEdit.text()) useGrid = self.useGridGroupBox.isChecked() useSnap = self.useSnapCheckBox.isChecked() return (x, y, useGrid, useSnap) class MainWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.textEdit = QTextEdit() self.setCentralWidget(self.textEdit) action = QAction('set grid',self) action.triggered.connect(self.setGrid) myMenu = self.menuBar().addMenu("&action") myMenu.addAction(action) self.x = 10 self.y = 10 self.useGrid = True self.useSnap = False def setGrid(self): gridDialog = GridDialog(self.x, self.y, self.useGrid, self.useSnap, self) if gridDialog.exec(): self.x, self.y, self.useGrid, self.useSnap = gridDialog.gridInfo() log = "Ok : useGrid ="+str(self.useGrid) + \ ", useSnap = " + str(self.useSnap) + \ ", x= " + str(self.x) + ", y = " + str(self.y) self.textEdit.append(log) else: self.textEdit.append("Cancel") if __name__ == '__main__': app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.resize(400,300) mainWindow.show() mainWindow.setWindowTitle("Test GridDialog") mainWindow.show() app.exec_() ๋ฉ”์ธ ์œˆ๋„ ํด๋ž˜์Šค(QMainWndow)๋Š” ์ค‘์•™ ์œ„์ ฏ(centralWidget)์„ ๊ฐ€์ง€๊ณ , ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์œ„ํ•ด ๋ฉ”๋‰ด, ํˆด๋ฐ”, ์ƒํƒœ ๋ฐ” ๋“ฑ์„ ๊ฐ–๋Š” ๋ฉ”์ธ ์ฐฝ์„ ์œ„ํ•œ ํด๋ž˜์Šค์ด๋‹ค. QMainWindow.setCentralWidget(widget)์„ ํ†ตํ•ด ์ค‘์•™ ์œ„์ ฏ์„ ๋“ฑ๋กํ•œ๋‹ค. Qt๋Š” ๋ฉ”๋‰ด, ํˆด๋ฐ”, ํ‚ค๋ณด๋“œ ๋‹จ์ถ•ํ‚ค(shortcuts)์„ ํ†ตํ•ฉํ•ด์„œ ์„ ํƒ ๋“ฑ์— ๋Œ€ํ•ด์„œ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋„๋ก ์•ก์…˜(QAction)์„ ๋„์ž…ํ•˜์˜€๋‹ค. ์•ก์…˜์„ ๋Œ€์ƒ์œผ๋กœ ์•ก์…˜์ด ์„ ํƒ๋  ๋•Œ triggered()๋ผ๋Š” ์‹œ๊ทธ๋„์ด ๋ฐœ์ƒํ•˜๋ฏ€๋กœ, ์ž‘์—… ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์Šฌ๋กฏ ํ•จ์ˆ˜๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์•ก์…˜์— ๋ฐ˜์‘ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฉ”๋‰ด๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ ๋ฉ”๋‰ด ์•„์ดํ…œ์— ๋Œ€ํ•œ ์•ก์…˜์„ ์ƒ์„ฑํ•˜๊ณ  ์‹œ๊ทธ๋„-์Šฌ๋กฏ์„ ์—ฐ๊ฒฐํ•œ ํ›„ ์ด๋ฅผ ๋ฉ”๋‰ด์— ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค. ์ž์„ธํ•œ ์‚ฌํ•ญ์€ 4.1 ์•ก์…˜, ๋ฉ”๋‰ด, ์ƒํƒœ ๋ฐ”์— ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค. 3.2 ํผ์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋ชจ๋‹ฌ ๋‹ค์ด์–ผ๋กœ๊ทธ ์•ž์„œ์˜ GridDialog1๊ณผ ๋™์ผํ•œ ์˜ˆ์ œ๋ฅผ Qt Designer๋กœ ํผ์œผ๋กœ ๋งŒ๋“  ํ›„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. GridDialog2 ๋จผ์ € ์•„๋ž˜์™€ ๊ฐ™์ด Qt Deginer์™€ GridDialog.ui๋ฅผ ์ค€๋น„ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฆฌ์†Œ์Šค์™€ ui๋ฅผ ํŒŒ์ด์ฌ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. > pyside2-rcc -o GridDialog_rc.py -py3 GridDialog.qrc > pyside2-uic GridDialog.ui > ui_girddialog.py ์†Œ์Šค ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. GridDialog2.py ... ์•ž์„œ์™€ ๋™์ผ import GridDialog_rc from ui_griddialog import Ui_GridDialog class GridDialog(QDialog): def __init__(self, x, y, useGrid, useSnap, parent): QDialog.__init__(self, parent) self.ui = Ui_GridDialog() self.ui.setupUi(self) self.ui.xLineEdit.setText(str(x)) self.ui.yLineEdit.setText(str(y)) validator = QDoubleValidator(self) validator.setBottom(0.) self.ui.xLineEdit.setValidator(validator) self.ui.yLineEdit.setValidator(validator) self.ui.useGridGroupBox.setChecked(useGrid) self.ui.useSnapCheckBox.setChecked(useSnap) self.ui.useSnapCheckBox.setEnabled(useGrid) def gridInfo(self): x = float(self.ui.xLineEdit.text()) y = float(self.ui.yLineEdit.text()) useGrid = self.ui.useGridGroupBox.isChecked() useSnap = self.ui.useSnapCheckBox.isChecked() return (x, y, useGrid, useSnap) class MainWindow(QMainWindow): ... ์•ž์„œ์™€ ๋™์ผ if __name__ == '__main__': ... ์•ž์„œ์™€ ๋™์ผ 3.3 ๋ชจ๋œ๋ฆฌ์Šค ๋‹ค์ด์–ผ๋กœ๊ทธ ๋ชจ๋‹ฌ๋ฆฌ์Šค(modeless) ๋‹ค์ด์–ผ๋กœ๊ทธ๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ๊ฐ€ ๋– ์žˆ๋Š” ์ƒํƒœ์—์„œ ๋‹ค๋ฅธ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ์ด๋‹ค. Ok, Cancel ๋ฒ„ํ„ด์„ ๊ฐ€์ง€๋Š” ๋ชจ๋‹ฌ ๋‹ค์ด์–ผ๋กœ๊ทธ์™€ ๋‹ฌ๋ฆฌ Close ๋ฒ„ํ„ด์œผ๋กœ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋‹ซ๋„๋ก ์„ค๊ณ„๋˜๋ฉฐ ๋‹ค๋ฅธ ๋ฒ„ํ„ด์ด๋‚˜ ์œ„์ ฏ์— ๋Œ€ํ•œ ๋ฐ˜์‘์œผ๋กœ ์ฆ‰์‹œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์„ค๊ณ„ํ•œ๋‹ค. FindReplaceDialog1 ์—ฌ๊ธฐ์—์„œ FindReplaceDialog1 ์˜ˆ์ œ๋Š” ์ฝ”๋“œ๋กœ๋งŒ ์ž‘์„ฑํ•œ ๊ฒƒ์ด๊ณ , FindReplaceDialog2๋Š” ํผ์œผ๋กœ ์ž‘์„ฑํ•œ ์˜ˆ์ œ์ด๋‹ค. ์ด๋ฏธ์ง€(find.png)์™€ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ(FindReplaceDialog.qrc)์„ ์ค€๋น„ํ•œ๋‹ค. <RCC> <qresource prefix="/"> <file>images/find.png</file> </qresource> </RCC> >pyside2-rcc -o FindReplaceDialog_rc.py -py3 FindReplaceDialog.qrc if __name__ == '__main__': from PySide2.QtCore import QCoreApplication QCoreApplication.setLibraryPaths(['C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\plugins']) from PySide2.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction import sys from PySide2.QtWidgets import (QDialog, QLabel, QComboBox, QPushButton, QGroupBox, QCheckBox, QGridLayout, QVBoxLayout, QHBoxLayout) from PySide2.QtGui import QIcon from PySide2.QtCore import Signal import FindReplaceDialog_rc class FindReplaceDialog(QDialog): find = Signal(str, bool, bool, bool) # find(findText, matchWholeWord, matchCase, upward) replace = Signal(str, str, bool, bool, bool) # replace(findText, replaceText, matchWoleWord, matchCase, upward) replaceAll = Signal(str, str, bool, bool, bool) # replaceAll(findText, replaceText, matchWoleWord, matchCase, upward) def __init__(self, parent=None): QDialog.__init__(self, parent) self.setWindowTitle("Find/Replace..."); self.setWindowIcon(QIcon(":images/find.png")); # widget and layout self.findLabel = QLabel("&Option") self.replaceLabel = QLabel("Re&place with: ") self.findComboBox = QComboBox() self.replaceComboBox = QComboBox() self.optionGroupBox = QGroupBox("&Option") self.wordCheckBox = QCheckBox("Match whole word") self.caseCheckBox = QCheckBox("Match case") self.upwardCheckBox = QCheckBox("Upward") self.findButton = QPushButton("&Find") self.replaceButton = QPushButton("&Replace") self.replaceAllButton = QPushButton("Replace &All") self.closeButton = QPushButton("&Close") formLayout = QGridLayout() formLayout.addWidget(self.findLabel, 0,0) formLayout.addWidget(self.findComboBox, 0,1) formLayout.addWidget(self.replaceLabel, 1,0) formLayout.addWidget(self.replaceComboBox, 1,1) groupBoxLayout = QVBoxLayout() groupBoxLayout.addWidget(self.wordCheckBox) groupBoxLayout.addWidget(self.caseCheckBox) groupBoxLayout.addWidget(self.upwardCheckBox) self.optionGroupBox.setLayout(groupBoxLayout) leftLayout = QVBoxLayout() leftLayout.addLayout(formLayout) leftLayout.addWidget(self.optionGroupBox) rightLayout = QVBoxLayout() rightLayout.addWidget(self.findButton) rightLayout.addWidget(self.replaceButton) rightLayout.addWidget(self.replaceAllButton) rightLayout.addWidget(self.closeButton) rightLayout.addStretch() mainLayout = QHBoxLayout() mainLayout.addLayout(leftLayout) mainLayout.addLayout(rightLayout) self.setLayout(mainLayout) # some stuffs for child widget self.findLabel.setBuddy(self.findComboBox) self.replaceLabel.setBuddy(self.replaceComboBox) self.findComboBox.setMinimumWidth(160) self.findComboBox.setEditable(True) self.replaceComboBox.setEditable(True) self.findButton.setDefault(True) self.findButton.setEnabled(False) self.replaceButton.setEnabled(False) self.replaceAllButton.setEnabled(False) # signal slot self.closeButton.clicked.connect(self.close) self.findComboBox.editTextChanged.connect(self.enableButtons) self.findComboBox.currentIndexChanged.connect(self.enableButtons) self.replaceComboBox.editTextChanged.connect(self.enableButtons) self.replaceComboBox.currentIndexChanged.connect(self.enableButtons) self.findButton.clicked.connect(self.onFind) self.replaceButton.clicked.connect(self.onReplace) self.replaceAllButton.clicked.connect(self.onReplaceAll) def enableButtons(self): findText = self.findComboBox.currentText() if findText != '': self.findButton.setEnabled(True) replaceText = self.replaceComboBox.currentText() if findText != "" and replaceText != "": self.replaceButton.setEnabled(True) self.replaceAllButton.setEnabled(True) def onFind(self): findText = self.findComboBox.currentText() matchWholeWord = self.wordCheckBox.isChecked() matchCase = self.caseCheckBox.isChecked() upward = self.upwardCheckBox.isChecked() index = self.findComboBox.findText(findText) if index != 1: self.findComboBox.removeItem(index) self.findComboBox.insertItem(0, findText) self.findComboBox.setCurrentIndex(0) self.find.emit(findText, matchWholeWord, matchCase, upward) def onReplace(self): findText = self.findComboBox.currentText() replaceText = self.replaceComboBox.currentText() matchWholeWord = self.wordCheckBox.isChecked() matchCase = self.caseCheckBox.isChecked() upward = self.upwardCheckBox.isChecked() index = self.findComboBox.findText(findText) if index != 1: self.findComboBox.removeItem(index) self.findComboBox.insertItem(0, findText) self.findComboBox.setCurrentIndex(0) index = self.replaceComboBox.findText(replaceText) if index != 1: self.replaceComboBox.removeItem(index) self.replaceComboBox.insertItem(0, replaceText) self.replaceComboBox.setCurrentIndex(0) self.replace.emit(findText, replaceText, matchWholeWord, matchCase, upward) def onReplaceAll(self): findText = self.findComboBox.currentText() replaceText = self.replaceComboBox.currentText() matchWholeWord = self.wordCheckBox.isChecked() matchCase = self.caseCheckBox.isChecked() upward = self.upwardCheckBox.isChecked() index = self.findComboBox.findText(findText) if index != 1: self.findComboBox.removeItem(index) self.findComboBox.insertItem(0, findText) self.findComboBox.setCurrentIndex(0) index = self.replaceComboBox.findText(replaceText) if index != 1: self.replaceComboBox.removeItem(index) self.replaceComboBox.insertItem(0, replaceText) self.replaceComboBox.setCurrentIndex(0) self.replaceAll.emit(findText, replaceText, matchWholeWord, matchCase, upward) class MainWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.textEdit = QTextEdit() self.setCentralWidget(self.textEdit) action = QAction("Test",self) action.triggered.connect(self.findReplace) myMenu = self.menuBar().addMenu("&Test") myMenu.addAction(action) self.findReplaceDialog = None def findReplace(self): if self.findReplaceDialog is None: self.findReplaceDialog = FindReplaceDialog() self.findReplaceDialog.find.connect(self.find) self.findReplaceDialog.replace.connect(self.replace) self.findReplaceDialog.replaceAll.connect(self.replaceAll) self.findReplaceDialog.show() self.findReplaceDialog.raise_() # Note raise_() not raise() self.findReplaceDialog.activateWindow() def find(self, findText, matchWholeWord, matchCase, upward): log = "Find operation " + findText + \ "\nMatchWoleWord : " + str(matchWholeWord) + \ "\nMatchCase : " + str(matchCase) + \ "\nUpward : " + str(upward) self.textEdit.append(log) def replace(self, findText, replaceText, matchWholeWord, matchCase, upward): log = "Replace operation " + findText + " " + replaceText + \ "\nMatchWoleWord : " + str(matchWholeWord) + \ "\nMatchCase : " + str(matchCase) + \ "\nUpward : " + str(upward) self.textEdit.append(log) def replaceAll(self, findText, replaceText, matchWholeWord, matchCase, upward): log = "ReplaceAll operation " + findText + " " + replaceText + \ "\nMatchWoleWord : " + str(matchWholeWord) + \ "\nMatchCase : " + str(matchCase) + \ "\nUpward : " + str(upward) self.textEdit.append(log) if __name__ == '__main__': app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() app.exec_() 3.5 ํผ์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋ชจ๋œ๋ฆฌ์Šค ๋‹ค์ด์–ผ๋กœ๊ทธ ์•ž์„œ์˜ GridDialog1๊ณผ ๋™์ผํ•œ ์˜ˆ์ œ๋ฅผ Qt Designer๋กœ ํผ์œผ๋กœ ๋งŒ๋“  ํ›„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. FindReplaceDialog2 ๋จผ์ € ์•„๋ž˜์™€ ๊ฐ™์ด Qt Deginer์™€ GridDialog.ui๋ฅผ ์ค€๋น„ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฆฌ์†Œ์Šค์™€ ui๋ฅผ ํŒŒ์ด์ฌ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. > pyside2-rcc -o FindReplaceDialog_rc.py -py3 FindReplaceDialog.qrc > pyside2-uic FindReplaceDialog.ui > ui_findreplacedialog.py ์†Œ์Šค ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. FindReplaceDialog2.py if __name__ == '__main__': from PySide2.QtCore import QCoreApplication QCoreApplication.setLibraryPaths(['C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\plugins']) from PySide2.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction import sys from PySide2.QtWidgets import QDialog from PySide2.QtGui import QIcon from PySide2.QtCore import Signal from ui_findreplacedialog import Ui_FindReplaceDialog class FindReplaceDialog(QDialog): find = Signal(str, bool, bool, bool) # find(findText, matchWholeWord, matchCase, upward) replace = Signal(str, str, bool, bool, bool) # replace(findText, replaceText, matchWoleWord, matchCase, upward) replaceAll = Signal(str, str, bool, bool, bool) # replaceAll(findText, replaceText, matchWoleWord, matchCase, upward) def __init__(self, parent=None): QDialog.__init__(self, parent) self.setWindowTitle("Find/Replace..."); self.setWindowIcon(QIcon(":images/find.png")); self.ui = Ui_FindReplaceDialog() self.ui.setupUi(self) # some stuffs for child widget self.ui.findButton.setEnabled(False) self.ui.replaceButton.setEnabled(False) self.ui.replaceAllButton.setEnabled(False) # signal slot self.ui.closeButton.clicked.connect(self.close) self.ui.findComboBox.editTextChanged.connect(self.enableButtons) self.ui.findComboBox.currentIndexChanged.connect(self.enableButtons) self.ui.replaceComboBox.editTextChanged.connect(self.enableButtons) self.ui.replaceComboBox.currentIndexChanged.connect(self.enableButtons) self.ui.findButton.clicked.connect(self.onFind) self.ui.replaceButton.clicked.connect(self.onReplace) self.ui.replaceAllButton.clicked.connect(self.onReplaceAll) def enableButtons(self): findText = self.ui.findComboBox.currentText() if findText != '': self.ui.findButton.setEnabled(True) replaceText = self.ui.replaceComboBox.currentText() if findText != "" and replaceText != "": self.ui.replaceButton.setEnabled(True) self.ui.replaceAllButton.setEnabled(True) def onFind(self): findText = self.ui.findComboBox.currentText() matchWholeWord = self.ui.wordCheckBox.isChecked() matchCase = self.ui.caseCheckBox.isChecked() upward = self.ui.upwardCheckBox.isChecked() index = self.ui.findComboBox.findText(findText) if index != 1: self.ui.findComboBox.removeItem(index) self.ui.findComboBox.insertItem(0, findText) self.ui.findComboBox.setCurrentIndex(0) self.find.emit(findText, matchWholeWord, matchCase, upward) def onReplace(self): findText = self.ui.findComboBox.currentText() replaceText = self.ui.replaceComboBox.currentText() matchWholeWord = self.ui.wordCheckBox.isChecked() matchCase = self.ui.caseCheckBox.isChecked() upward = self.ui.upwardCheckBox.isChecked() index = self.ui.findComboBox.findText(findText) if index != 1: self.ui.findComboBox.removeItem(index) self.ui.findComboBox.insertItem(0, findText) self.ui.findComboBox.setCurrentIndex(0) index = self.ui.replaceComboBox.findText(replaceText) if index != 1: self.ui.replaceComboBox.removeItem(index) self.ui.replaceComboBox.insertItem(0, replaceText) self.ui.replaceComboBox.setCurrentIndex(0) self.replace.emit(findText, replaceText, matchWholeWord, matchCase, upward) def onReplaceAll(self): findText = self.ui.findComboBox.currentText() replaceText = self.ui.replaceComboBox.currentText() matchWholeWord = self.ui.wordCheckBox.isChecked() matchCase = self.ui.caseCheckBox.isChecked() upward = self.ui.upwardCheckBox.isChecked() index = self.ui.findComboBox.findText(findText) if index != 1: self.ui.findComboBox.removeItem(index) self.ui.findComboBox.insertItem(0, findText) self.ui.findComboBox.setCurrentIndex(0) index = self.ui.replaceComboBox.findText(replaceText) if index != 1: self.ui.replaceComboBox.removeItem(index) self.ui.replaceComboBox.insertItem(0, replaceText) self.ui.replaceComboBox.setCurrentIndex(0) self.replaceAll.emit(findText, replaceText, matchWholeWord, matchCase, upward) class MainWindow(QMainWindow): ... ์•ž์„œ์™€ ๋™์ผ if __name__ == '__main__': ... ์•ž์„œ์™€ ๋™์ผ 3.6 ํ‘œ์ค€ ๋‹ค์ด์–ผ๋กœ๊ทธ Qt์—์„œ๋Š” ํ‘œ์ค€ ๋‹ค์ด์–ผ๋กœ๊ทธ(standard dialog) ๋˜๋Š” ๋‚ด์žฅ ๋‹ค์ด์–ผ๋กœ๊ทธ(built-in dialog)๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ๋ช‡๋ช‡<NAME>์˜ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์•„๋ž˜ ํ‘œ๋Š” Qt์—์„œ ์ œ๊ณตํ•˜๋Š” ํ‘œ์ค€ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋‚˜์—ดํ•œ ๊ฒƒ์œผ๋กœ ๋ชจ๋‘ QDialog๋ฅผ ์ƒ์†ํ•˜์—ฌ ๊ตฌํ˜„๋˜์—ˆ๋‹ค. ํ‘œ์ค€ ๋‹ค์ด์–ผ๋กœ๊ทธ ์ค‘ QInputDialog, QMessageBox, QErrorMessage, QProgressDialog๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฐ„๋‹จํ•œ ๋ฉ”์‹œ์ง€๋‚˜ ์ •๋ณด๋ฅผ ์•Œ๋ ค์ฃผ๊ณ , ๊ทธ ์‘๋‹ต์„ ๋ฐ›๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ”ผ๋“œ๋ฐฑ ๋‹ค์ด์–ผ๋กœ๊ทธ(feedback dialog)๋ผ๊ณ  ํ•œ๋‹ค ๋ฐ˜๋ฉด์—, QFileDialog, QColorDialog, QFontDialog, QPageSetupDialog, QPrintDialog, QPrintPreviewDialog๋Š” ์‹œ์Šคํ…œ ์ฐจ์›์—์„œ ์ง€์›ํ•˜๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ณต์šฉ ๋‹ค์ด์–ผ๋กœ๊ทธ(common dialog)๋กœ ๋ถ„๋ฅ˜๋œ๋‹ค. QErrorMessage์™€ QProcessDialog๋งŒ ๋ชจ๋œ๋ฆฌ์Šค ๋‹ค์ด์–ผ๋กœ๊ทธ๋กœ ์‚ฌ์šฉ๋˜๊ณ , ๋‚˜๋จธ์ง€ ํ‘œ์ค€ ๋‹ค์ด์–ผ๋กœ๊ทธ๋Š” ๋ชจ๋‹ฌ ๋‹ค์ด์–ผ๋กœ๊ทธ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํŠน์ง•์ด ์žˆ๋‹ค. ํ•œํŽธ, ํ”„๋ฆฐํ„ฐ์™€ ๊ด€๋ จ๋œ QPrintDialog, QPageSetupDialog, QPrintPreviewDialog๋Š” Qt Print Support ๋ชจ๋“ˆ์ด๊ณ , ๋‚˜๋จธ์ง€๋Š” ๋ชจ๋‘ Qt Widgets ๋ชจ๋“ˆ์— ์†ํ•œ๋‹ค. 3.7 ํ‘œ์ค€ ๋‹ค์ด์–ผ๋กœ๊ทธ - ํ”ผ๋“œ๋ฐฑ ๋‹ค์ด์–ผ๋กœ๊ทธ Qt์—์„œ๋Š” ํ‘œ์ค€ ๋‹ค์ด์–ผ๋กœ๊ทธ(standard dialog) ๋˜๋Š” ๋‚ด์žฅ ๋‹ค์ด์–ผ๋กœ๊ทธ(built-in dialog)๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ๋ช‡๋ช‡<NAME>์˜ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์•„๋ž˜ ํ‘œ๋Š” Qt์—์„œ ์ œ๊ณตํ•˜๋Š” ํ‘œ์ค€ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋‚˜์—ดํ•œ ๊ฒƒ์œผ๋กœ ๋ชจ๋‘ QDialog๋ฅผ ์ƒ์†ํ•˜์—ฌ ๊ตฌํ˜„๋˜์—ˆ๋‹ค. FeedbackDlgDemo.py from PySide2.QtWidgets import (QApplication, QMainWindow, QTextEdit, QAction, QErrorMessage, QInputDialog, QLineEdit, QMessageBox,) import sys class MainWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.logWindow = QTextEdit(self) self.logWindow.setReadOnly(True) self.setCentralWidget(self.logWindow) # Testing QInputDialog actionInt = QAction("QInputDialog::getInt()",self) actionDouble = QAction("QInputDialog::getDouble()",self) actionText = QAction("QInputDialog::getText()",self) actionMultiLineText = QAction("QInputDialog::getMultiLineText",self) actionItem = QAction("QInputDialog::getItem()",self) inputDialogMenu = self.menuBar().addMenu("Q&InputDialog") inputDialogMenu.addAction(actionInt) inputDialogMenu.addAction(actionDouble) inputDialogMenu.addAction(actionText) inputDialogMenu.addAction(actionMultiLineText) inputDialogMenu.addAction(actionItem) actionInt.triggered.connect(self.getInt) actionDouble.triggered.connect(self.getDouble) actionText.triggered.connect(self.getText) actionMultiLineText.triggered.connect(self.getMultiLineText) actionItem.triggered.connect(self.getItem) # Testing for QMessageBox actionAbout = QAction("QMessageBox::about()",self) actionInformation = QAction("QMessageBox::information()",self) actionQuestion = QAction("QMessageBox::question()",self) actionWarning = QAction("QMessageBox::warning()",self) actionCritical = QAction("QMessageBox::critcal()",self) messgeWidgetMenu = self.menuBar().addMenu("Q&MessageBox") messgeWidgetMenu.addAction(actionAbout) messgeWidgetMenu.addAction(actionInformation) messgeWidgetMenu.addAction(actionQuestion) messgeWidgetMenu.addAction(actionWarning) messgeWidgetMenu.addAction(actionCritical) actionAbout.triggered.connect(self.aboutMessage) actionInformation.triggered.connect(self.informationMessage) actionQuestion.triggered.connect(self.questionMessage) actionWarning.triggered.connect(self.warningMessage) actionCritical.triggered.connect(self.criticalMessage) # Testing for QErrorMessage self.errorMessageDialog = QErrorMessage(self); self.errorMessageDialog.setWindowTitle("Error"); actionErrorMessage = QAction("QErrorMessage::show()",self); errorWidgetMenu = self.menuBar().addMenu("QErrorMessage"); errorWidgetMenu.addAction(actionErrorMessage); actionErrorMessage.triggered.connect(self.errorMessage); def getInt(self): value, ok = QInputDialog.getInt(self,"Input value", "Percentange:", 25,0,100,1) if ok: self.logWindow.append(">>> " + str(value)) def getDouble(self): value, ok = QInputDialog.getDouble(self,"Input value","Amount:", 37.56, -1000,1000,2) if ok: self.logWindow.append(">>> " + str(value)) def getText(self): text, ok = QInputDialog.getText(self,"Input text","Enter text:", QLineEdit.Normal,"Enter") if ok and text != "": self.logWindow.append(">>> " + text) def getMultiLineText(self): text, ok = QInputDialog.getMultiLineText(self,"Input text", "Adress:","John Doe\nFreedom Stress") if ok and text != "": self.logWindow.append(">>> " + text) def getItem(self): text, ok = QInputDialog.getItem(self,"Select season","Season:", ["Spring","Summer","Fall","Winter"],0, False) if ok and text != "": self.logWindow.append(">>> " + text) def aboutMessage(self): QMessageBox.about(self,"About Section Designer", "<h2>Section Designer 1.1</h2>" "<p>Copyright ยฉ 2014 Qt5Programming Inc." "<p>Section Designer is a small application that " "computes section properties, moment-curvature diagram, and many other."); def informationMessage(self): QMessageBox.information(self,"Assign Workspace", "Failed to assign the directory as workspace") def questionMessage(self): r = QMessageBox.question(self,"Licence agreement", "Do you agree the follwoing license?" "<p> 1. Use the SW as <strong>educational purpose</strong>." "<p> 2. Report the bugs in use", QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) if r == QMessageBox.Yes: self.logWindow.append("Yes") elif r == QMessageBox.No: self.logWindow.append("No") else: self.logWindow.append("Cancel") def warningMessage(self): r = QMessageBox.warning(self,"Warning: Delete Node", "Cannot delete the connected node only." "<p>Delete all connected line? ", QMessageBox.Yes | QMessageBox.No) if r == QMessageBox.Yes: self.logWindow.append("Yes") else: self.logWindow.append("No") def criticalMessage(self): r = QMessageBox.critical(self,"Error: Analysis error", "Some input is not proper", QMessageBox.Abort | QMessageBox.Retry | QMessageBox.Ignore) if r == QMessageBox.Abort: self.logWindow.append("Abort") elif r == QMessageBox.Retry: self.logWindow.append("Retry") else: self.logWindow.append("Ignore") def errorMessage(self): self.errorMessageDialog.showMessage( "<h2>Error 663</h2><p>Cannot find file") if __name__ == '__main__': app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.resize(400,300) mainWindow.setWindowTitle("Feedback dialog") mainWindow.show() mainWindow.show() app.exec_() 3.8 ํ‘œ์ค€ ๋‹ค์ด์–ผ๋กœ๊ทธ - ๊ณต์šฉ ๋‹ค์ด์–ผ๋กœ๊ทธ ๊ณต์šฉ ๋‹ค์ด์–ผ๋กœ๊ทธ๋กœ๋Š” ํŒŒ์ผ, ์ƒ‰์ƒ, ํฐํŠธ ์„ ํƒ์„ ์œ„ํ•œ QFileDialog, QColorDialog, QFontDialog์™€ ํ”„๋ฆฐํ„ฐ ์ง€์›์„ ์œ„ํ•œ QPrintDialog, QPageSetupDialog, QPrintPreviewDialog๊ฐ€ ์žˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” QFileDialog, QColorDialog, QFontDialog๋งŒ ์„ค๋ช…ํ•œ๋‹ค. QFileDialog QFileDialog๋Š” ์ •์  ํŽธ์˜ ํ•จ์ˆ˜ getOpenFileName(), getSaveFileName() ์ด ํŒŒ์ผ ์—ด๊ธฐ ๋ฐ ์ €์žฅ ๊ด€๋ จ ๋ฉ”๋‰ด์— ๋ฐ˜์‘ํ•˜์—ฌ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ์ „ํ˜•์ ์ธ ์ฝ”๋“œ ์กฐ๊ฐ์ด๋‹ค. class MainWindow(QMainWindow): ... def open(self): if self.okToContinue(): fileName, selectedFilter = QFileDialog.getOpenFileName(self,"Open Image File",".", "Image files (*.jpg *.png);; XPM file (*.xpm)") if fileName != "": self.load(fileName) if QFileInfo(fileName).suffix() == "jpg" : ... something with .jpg file elif QFileInfo(fileName).suffix() == "png" : ... something with .png file else: ... something with .xpm file def saveAs(self): fileName, _ = QFileDialog.getSaveFileName(self,"Save Datafile", ".", "Data files (*.dat)") if fileName == "": return False return self.saveFile(fileName) ์œ„ ์ฝ”๋“œ์—์„œ ๋งˆ์ง€๋ง‰ ์ธ์ž์—์„œ ํŒŒ์ผ ํ•„ํ„ฐ(filter)๋ฅผ ์ง€์ •ํ•˜๊ณ  ์žˆ๋‹ค. ํ™•์žฅ์ž๋Š” ์„ค๋ช…๋ฌธ (*.ext) ํ˜•ํƒœ์˜ ํฌ๋งท์ด๋‹ค. *.ext์— ์ŠคํŽ˜์ด์Šค๋กœ ์—ฌ๋Ÿฌ ํ™•์žฅ์ž๋ฅผ ๊ธฐ์ž…ํ•  ์ˆ˜ ์žˆ๊ณ  ์„ค๋ช…๋ฌธ ์ž์ฒด๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ์—๋Š” ;; ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ์ฝ”๋“œ์—์„œ QFileInfo ํด๋ž˜์Šค๋Š” ํŒŒ์ผ๋ช…์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š” ํŽธ์˜ ํด๋ž˜์Šค์ด๋‹ค. ์œ„์—์„œ๋Š” ํ™•์žฅ์ž๋ฅผ ๊ฒ€ํ† ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜์˜€๋‹ค. QFileDialog๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์–ป๋Š” getOpenFileNames(), ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์–ป๋Š” getExistingDirectory(), URL์„ ์–ป๋Š” getOpenFileUrl() ๋“ฑ ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ ์ •์  ํŽธ์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. QFontDialog์™€ QColorDialog QFontDialog๋Š” ํฐํŠธ๋ฅผ ์„ ํƒํ•˜๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ๋กœ QFont ํด๋ž˜์Šค์™€ ์—ฐ๋™ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉฐ, QColorDialog๋Š” QColor ๊ฐ์ฒด์™€ ์—ฐ๋™ํ•˜์—ฌ ์ƒ‰์ƒ์„ ์„ ํƒํ•ด ์ฃผ๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ์ด๋‹ค. class MainWindow(QMainWindow): ... def setFont(self): (ok, font) = QFontDialog.getFont(QFont("Helvetica [Cronyx]", 10), self) if ok: someLabel->setText( "family:{}, pointSize:{}, weight:{}, italic:{}".format( font.family().font.pointSize(),font.weight(),font.italic()) ) # ํฐํŠธ ์ด๋ฆ„, ํฌ์ธํŠธ ํฌ๊ธฐ, ์›จ์ดํŠธ(ํด์ˆ˜๋ก ๋‘๊บผ์›Œ์ง), ์ดํƒค๋ฆญ ์—ฌ๋ถ€ ์ถœ๋ ฅ someLabel.setFont(font) def setColor(self): color = QColorDialog.getColor(Qt.green, self, "Select Color") if color.isValid(): someLabel.append(color.name()) someLabel.setPalette(QPalette(color)) someLabel.setAutoFillBackground(true) 4. ๋ฉ”์ธ ์œˆ๋„ ๋ฉ”์ธ ์œˆ๋„๋Š” ์ตœ์ƒ์œ„์ฐฝ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๋ฉ”๋‰ด, ํˆด๋ฐ”, ์ƒํƒœ ๋ฐ” ๋“ฑ์„ ๊ฐ–๋Š” ์œ„์ ฏ์„ ์˜๋ฏธํ•œ๋‹ค. Qt์—์„œ ๋ฉ”์ธ ์œˆ๋„๋Š” QMainWindow๋ฅผ ์„œ๋ธŒํด๋ž˜์‹ฑํ•ด์„œ ๋งŒ๋“ ๋‹ค. QMainWindow์—์„œ๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ƒํ•˜์ขŒ์šฐ์— ๋ฉ”๋‰ด(QMenuBar), ํˆด๋ฐ”(QToolBar), ์ƒํƒœ ๋ฐ”(QStatusBar) ๋“ฑ์„ ์‰ฝ๊ฒŒ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ๋„ํ‚น ์œˆ๋„(QDockWidget์œผ๋กœ ์„œ๋ธŒํด๋ž˜์‹ฑ) ์˜์—ญ๋„ ์ •ํ•ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์šด๋ฐ ์˜์—ญ์— ๋“ค์–ด๊ฐ€๋Š” ์œ„์ ฏ์„ ์ค‘์•™ ์œ„์ ฏ(Central widget)์ด๋ผ๊ณ  ํ•˜๋Š” ๋ฐ QWidget์—์„œ ์ƒ์†๋ฐ›์€ ์œ„์ ฏ์ด๋ฉด ๋œ๋‹ค. ์—ฌ๊ธฐ์—๋Š” QTextEdit ๊ฐ™์€ ๋‚ด์žฅ Qt ์œ„์ ฏ์„ ๋‘˜ ์ˆ˜๋„ ์žˆ๊ณ , ์ง์ ‘ ์ž‘์„ฑํ•œ ์ปค์Šคํ…€ ์œ„์ ฏ์„ ๋‘˜ ์ˆ˜๋„ ์žˆ๊ณ , QMdiArea, QTabWidget ๊ฐ™์€ ์ปจํ…Œ์ด๋„ˆ ์œ„์ ฏ์„ ๋‘˜ ์ˆ˜๋„ ์žˆ๋‹ค. QMdiArea๋กœ๋Š” ๊ณ ์ „์ ์ธ MDI ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๊ณ , QTabWidget์„ ์‚ฌ์šฉํ•˜๋ฉด ํƒญ ๋ฌธ์„œ ํ˜•ํƒœ์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฉ”์ธ ์œˆ๋„์˜ ์„ผํŠธ๋Ÿด ์œ„์ ฏ์€ QMainWindow.setCentralWidget() ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑ์ž์—์„œ ํ˜ธ์ถœํ•ด ์ฃผ๋ฉด ๋œ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ ์‹ค์ œ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ฉ”์ธ ์œˆ๋„์˜ ๋ชจ์Šต์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฉ”์ธ ์œˆ๋„๋Š” ์ƒ์„ฑ์ž์—์„œ ๋ฉ”๋‰ด, ํˆด๋ฐ”, ์ƒํƒœ ๋ฐ”, ์„ผํŠธ๋Ÿด ์œ„์ ฏ ์„ค์ • ๋“ฑ๋“ฑ ๋‹ค์–‘ํ•œ ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋‹ค์Œ์€ QMainWindow๋ฅผ ์„œ๋ธŒํด๋ž˜์‹ฑํ•œ ์ปค์Šคํ…€ ๋ฉ”์ธ ์œˆ๋„ ํด๋ž˜์Šค์˜ ์ผ๋ฐ˜์ ์ธ ์ƒ์„ฑ์ž ๊ตฌ์กฐ๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. class MainWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Shape') self.setWindowIcon(QIcon(":/images/qt.png")) self.shapeWidget = ShapeWidget() self.setCentralWidget(self.shapeWidget) self.createActions() self.createMenus() self.createContextMenu() self.createToolBar() self.createStatusBar() self.readSettings() self.myModalessDialog = None def createActions(self): .... ์œ„ ์ฝ”๋“œ๋Š” ๋‹จ์œ„ ๋ธ”๋ก์œผ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ํ•จ์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฆฌํŒฉํ† ๋ง(refactoring) ํ˜•ํƒœ๋กœ ์ œ์‹œํ•˜์˜€๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ฉ”์ธ ์œˆ๋„์˜ ์ƒ์„ฑ์ž์— ํŽธ์ง‘ํ•  ๋‚ด์šฉ์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ƒ์„ฑ์ž์—์„œ ์•ก์…˜(QAction)์ด๋ผ๋Š” ๊ฒƒ์„ ์ƒ์„ฑํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. Qt๋Š” ๋ฉ”๋‰ด ์•„์ดํ…œ๊ณผ ํˆด๋ฐ” ํˆด๋ฒ„ํ„ด์— ๋Œ€ํ•œ ๋ช…๋ น์„ ์ผ์›ํ™”ํ•˜๊ธฐ ์œ„ํ•ด QAction ํด๋ž˜์Šค๋ฅผ ๋„์ž…ํ•˜์˜€๋‹ค. ์•ก์…˜ ์ƒ์„ฑ ํ›„ ๋ฉ”๋‰ด, ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด, ํˆด๋ฐ”, ์ƒํƒœ ๋ฐ” ๋“ฑ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. readSettings()๋Š” ์‘์šฉํ”„๋กœ๊ทธ๋žจ์˜ ์ฐฝ์˜ ์œ„์น˜, ํˆด๋ฐ”๋‚˜ ๋„ํ‚น ์œ„์ ฏ์˜ ์ƒํƒœ ๋“ฑ๊ณผ ๊ฐ™์€ ๊ฐ์ข… ์„ค์ •์„ ์ฝ์–ด์˜ค๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•œ๋‹ค. QSettings๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋Š”๋ฐ ์ด ํด๋ž˜์Šค๋Š” ๋‹ค์–‘ํ•œ ํ”Œ๋žซํผ์„ ์ง€์›ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œˆ๋„์šฐ์ฆˆ์˜ ๊ฒฝ์šฐ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋œ๋ฆฌ์Šค๋‹ค์ด์–ผ๋กœ๊ทธ ๊ฐ™์€ ํ•„์š”ํ•œ ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. 4.1 ์•ก์…˜, ๋ฉ”๋‰ด, ์ƒํƒœ ๋ฐ” ์•ก์…˜ ์ผ๋ฐ˜ Qt๋Š” ๋ฉ”๋‰ด, ํˆด๋ฐ”, ์ƒํƒœ ๋ฐ” ๋“ฑ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์•ก์…˜(QAction)์ด๋ผ๋Š” ๊ฐœ๋…์„ ๋„์ž…ํ•˜์˜€๋‹ค. QAction์— ๋ฉ”๋‰ด์— ํ‘œ์‹œ๋˜๋Š” ๋ฌธ์ž์—ด(text), ์•„์ด์ฝ˜(icon), ๋‹จ์ถ•ํ‚ค(shortcut), ํˆดํŒ(tooltip), ์ƒํƒœ ๋ฐ”์— ๋‚˜ํƒ€๋‚˜๋Š” ํŒ ๋ฌธ์ž์—ด(statustip), ์™€์ธ  ๋””์Šค ๋ฌธ์ž์—ด(whatsthis) ๋“ฑ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•ก์…˜์„ ๋ฉ”๋‰ด(QMenu)์— addAction()์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์•ก์…˜์— ์ง€์ •ํ•œ ๋ฌธ์ž์—ด๊ณผ ์•„์ด์ฝ˜์„ ๊ฐ€์ง„ ๋ฉ”๋‰ด ์•„์ดํ…œ์ด ๋œ๋‹ค. ํˆด๋ฐ”(QToolBar)๋Š” ์ฝค๋ณด ๋ฐ•์Šค ๋“ฑ์˜ ์œ„์ ฏ์ด ์˜ฌ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋ณดํ†ต ๋ฉ”๋‰ด ์•„์ดํ…œ๊ณผ 1:1๋กœ ๋Œ€์‘ํ•˜๋Š” ํˆด๋ฒ„ํ„ด์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. ํˆด๋ฐ”์˜ ํˆด๋ฒ„ํ„ด ์—ญ์‹œ addAction()์„ ํ†ตํ•ด QAction์— ์ง€์ •ํ•œ ์•„์ด์ฝ˜์„ ๊ฐ–๋Š” ํˆด๋ฒ„ํ„ด์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ์„ฑ๋œ๋‹ค. ๋˜ํ•œ ์œ„์ ฏ์˜ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด์— ๋Œ€ํ•ด์„œ๋„ ์•ก์…˜์„ ์ด์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์‚ฌ์‹ค QWidget์€ ์ž์‹ ์˜ ๋‚ด๋ถ€์— QAction์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์œ ์ง€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. QWidget.addAction()์€ ์—ฌ๊ธฐ์— ์•ก์…˜์„ ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์œ„์ ฏ๋“ค์€ ์ด ์•ก์…˜์„ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. ์ƒํƒœ ๋ฐ”์™€๋Š” ์•ก์…˜๊ณผ ์—ฐ๊ด€๋œ ๋ฉ”๋‰ด ์•„์ดํ…œ์ด๋‚˜ ํˆด๋ฐ”์— ๋งˆ์šฐ์Šค๋ฅผ ์œ„์น˜ํ•˜๋ฉด ์ง€์ •๋œ ๋ฌธ์ž์—ด์ด ํ‘œ์‹œ๋œ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํ•˜๋‚˜์˜ ํ–‰์œ„๋ฅผ ํ•˜๋Š” ์•ก์…˜์„ ์ƒ์„ฑํ•ด์„œ ๋ฉ”๋‰ด, ํˆด๋ฐ”, ์ƒํƒœ ๋ฐ”๋ฅผ ์ผ์›ํ™”ํ•˜์—ฌ ๊ด€๋ฆฌ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฉ”๋‰ด ์•„์ดํ…œ์ด๋‚˜ ํˆด๋ฐ” ๋ฒ„ํ„ด์„ ์„ ํƒํ•œ ๊ฒฝ์šฐ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์•ก์…˜์—์„œ triggered(), toggled(bool) ๋“ฑ๊ณผ ๊ฐ™์€ ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ์‹œํ‚ค๋ฉฐ, ์ด๋ฅผ ์Šฌ๋กฏ์— ์—ฐ๊ฒฐํ•จ์œผ๋กœ์จ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ๋ฉ”์ธ ์œˆ๋„์—์„œ ์•ก์…˜์œผ๋กœ ๋ฉ”๋‰ด์™€ ํˆด๋ฐ”๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์ ˆ์ฐจ์™€ ์˜ˆ์ด๋‹ค. ์•ก์…˜์„ ๋งŒ๋“ค๊ณ  ์„ค์ •ํ•œ๋‹ค(์•„์ด์ฝ˜, ๋‹จ์ถ•ํ‚ค, ํŒ, slot ํ•จ์ˆ˜ ์—ฐ๊ฒฐ ๋“ฑ) ๋ฉ”๋‰ด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๋งŒ๋“ค์–ด๋‘” ์•ก์…˜์„ ์ถ”๊ฐ€ํ•œ๋‹ค(QMenu.addAction() ํ•จ์ˆ˜) ํˆด๋ฐ”๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๋งŒ๋“ค์–ด๋‘” ์•ก์…˜์„ ์ถ”๊ฐ€ํ•œ๋‹ค(QToolBar.addAction() ํ•จ์ˆ˜) class MainWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Shape') self.setWindowIcon(QIcon(":/images/qt.png")) self.myWidget = MyWidget() self.setCentralWidget(self.myWidget) # ์•ก์…˜์„ ์ƒ์„ฑํ•œ๋‹ค. self.newAction = QAction("&New", self) self.newAction.setIcon(QIcon(":/images/new.png") self.newAction.setShortcut("Ctrl+N") # or newAction.setShortcut(QKeySequence::New) self.newAction.setStatusTip("Create a new file") self.newAction.triggered.connect(newFile) ... self.copyAction = QAction("&Copy", self) ... self.copyAction.triggered.connect(myWidget.copy) # MyWidget::copy() ์Šฌ๋กฏ๊ณผ ์—ฐ๊ฒฐ # ๋ฉ”๋‰ด๋ฅผ ๋งŒ๋“ ๋‹ค fileMenu = self.menuBar().addMenu("&File") fileMenu.addAction(self.newAction) ... sperator = fileMenu.addSeparator() fileMenu.addAction(self.exitAction) # ํˆด๋ฐ”๋ฅผ ๋งŒ๋“ ๋‹ค fileToolBar = self.addToolBar("&File") fileToolBar.addAction(self.copyAction) ... # ์ƒํƒœ ๋ฐ”๋ฅผ ๋งŒ๋“ ๋‹ค .... def newFile(self): ... ... ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด QAction์„ ์ƒ์„ฑํ•  ๋•Œ ํ…์ŠคํŠธ, ์•„์ด์ฝ˜, ๋‹จ์ถ•ํ‚ค, ์ƒํƒœ ๋ฐ”์— ํ‘œ์‹œ๋  ๋‚ด์šฉ ๋“ฑ ๋‹ค์–‘ํ•œ ์†์„ฑ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ์„ฑ๋œ QAction์€ triggered() ์‹œ๊ทธ๋„๊ณผ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•  ์Šฌ๋กฏ์„ ์ ์ ˆํžˆ ์—ฐ๊ฒฐํ•œ๋‹ค. ๋‹จ์ถ•ํ‚ค๋Š” setShortcut()์—์„œ โ€œCtrl+Nโ€ ๋ฌธ์ž์—ด์„ ์ง€์ •ํ•˜์—ฌ Ctrl+N์ด ๋ˆŒ๋ฆด ๋•Œ ์ž‘๋™ํ•˜๋„๋ก ํ•˜์˜€๋‹ค. Qt์—๋Š” ๊ฐ ํ”Œ๋žซํผ๋งˆ๋‹ค ํ‘œ์ค€์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์ถ•ํ‚ค ์กฐํ•ฉ์„ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด File New์— ๋Œ€ํ•ด์„œ๋Š” ๋ฌธ์ž์—ด์ธ โ€œCtrl+Nโ€ ๋Œ€์‹  QKeySequence.New๋ฅผ ์ง€์ •ํ•˜๋ฉด ํ”Œ๋žซํผ์—์„œ ํ‘œ์ค€์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์ถ•ํ‚ค ์กฐํ•ฉ์ด ์ ์šฉ๋œ๋‹ค. ์ž์„ธํ•œ ๋ฆฌ์ŠคํŠธ๋Š” Qt ํ—ฌํ”„์˜ QKeySequence ํด๋ž˜์Šค์˜ ์„ค๋ช…์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋‹ค์Œ์€ ๋Œ€ํ‘œ์ ์ธ ํ‘œ์ค€ ๋‹จ์ถ•ํ‚ค ์กฐํ•ฉ์„ ๋‚˜์—ดํ•œ ๊ฒƒ์ด๋‹ค. ํŒŒ์ผ ๋ฉ”๋‰ด : QKeySequence.New, QKeySequence::Open, QKeySequence::Save, QKeySequence::SaveAs, QKeySequence::Print, QKeySequence::Quit ํŽธ์ง‘ ๋ฉ”๋‰ด : QKeySequence::Copy, QKeySequence::Cut, QKeySequence::Redo, QKeySequence::Undo QAction.setStatusTip(text)๋กœ ๋ฉ”๋‰ด๋‚˜ ํˆด๋ฐ”์˜ ์•„์ดํ…œ์ด ํฌ์ปค์Šค๋ฅผ ๊ฐ€์งˆ ๋•Œ ์ƒํƒœ ๋ฐ”์— text๊ฐ€ ํ‘œ์‹œ๋˜๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ ์•ก์…˜์— ๊ด€๋ จ๋œ ์•„์ดํ…œ์ด ํฌ์ปค์Šค๊ฐ€ ์—†์„ ๋•Œ ์–ด๋–ค ๋‚ด์šฉ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” QLabel์„ ์ถ”๊ฐ€ํ•ด ์ƒํ™ฉ์— ๋งž๋„๋ก ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. ๋น„์Šทํ•œ ํ•จ์ˆ˜๋กœ QAction.setToolTip(text)์ด ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ํˆด๋ฐ”์— ํฌ์ปค์Šค๊ฐ€ ์žˆ์„ ๋•Œ ํˆดํŒ์— ๋‚˜ํƒ€๋‚ผ ํ…์ŠคํŠธ๋ฅผ ์ง€์ •ํ•œ๋‹ค. ๋งŒ์•ฝ ํˆดํŒ ํ…์ŠคํŠธ๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” QAction์˜ ํ…์ŠคํŠธ๊ฐ€ ์ง€์ •๋œ๋‹ค. ๋ณดํ†ต ํˆดํŒ์€ ์งง๊ฒŒ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ๋ฉ”๋‰ด ์•„์ดํ…œ๊ณผ ๋™์ผํ•˜๊ฒŒ ์„ค์ •ํ•˜๋ฏ€๋กœ ์ž˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. ์‚ฌ์šฉ์ž์—๊ฒŒ ํ—ฌํ”„๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋กœ๋Š” QAction.setWhatsThis(text)๊ฐ€ ์žˆ๋‹ค. ์•ก์…˜์˜ ์ƒ์„ฑ์ด ๋๋‚˜๋ฉด ๋‹จ์ˆœํžˆ QMenu.addAction()์ด๋‚˜ QToolBar.addAction()์œผ๋กœ ์•ก์…˜์„ ์ถ”๊ฐ€ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ”๋‰ด์™€ ํˆด๋ฐ”์˜ ๊ตฌ์„ฑ์ด ๋๋‚œ๋‹ค. ์ฝ”๋“œ์—์„œ menuBar()๋Š” MainWindow์˜ ๋ฉ”๋‰ด๋ฐ”(QMenuBar)๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ์ฒ˜์Œ ํ˜ธ์ถœ๋  ๋•Œ ๋ฉ”๋‰ด๋ฐ”๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. addToolBar("File") ์—ญ์‹œ ์ฒ˜์Œ ํ˜ธ์ถœ๋  ๋•Œ File์ด๋ผ๋Š” ์ด๋ฆ„์˜ ํˆด๋ฐ”๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ๋ฆฌํ„ด๋œ๋‹ค. ๋ถ„๋ฆฌ์ž ์—ญ์‹œ ์•ก์…˜์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋ถ„๋ฆฌ ์ž๋Š” ์•ก์…˜์„ ์ƒ์„ฑํ•ด์„œ ๋ฉ”๋‰ด๋‚˜ ํˆด๋ฐ”์— ๋„ฃ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ QMenu.addSeparator()๋‚˜ QToolBar.AddSeperator()๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•ด ์ค€๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์€ ๋ถ„๋ฆฌ์ž๋ฅผ ์ง€์นญํ•˜๋Š” QAction ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ์ฒดํฌ ์•ก์…˜ ์•ก์…˜ ์ค‘์—๋Š” ์ฒดํฌ ๊ฐ€๋Šฅํ•œ ์•ก์…˜์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. QAction.setCheckable(true)์„ ์„ค์ •ํ•ด ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฐ˜ ์•ก์…˜์ด triggered() ์‹œ๊ทธ๋„์„ ์ด์šฉํ•ด ์Šฌ๋กฏ ํ•จ์ˆ˜์— ์—ฐ๊ฒฐํ•œ ๋ฐ˜๋ฉด ์ฒดํฌ ์•ก์…˜์€ toggled(bool)์ด๋‚˜ triggered(bool) ์‹œ๊ทธ๋„์„ ์‚ฌ์šฉํ•œ๋‹ค. checkAction = QAction(self) ... set icon, shortcut, status tips checkAction.setCheckable(True) checAction.setChecked(True) checkAction.toggled.connect(self.someSlot) # someSlot(bool) ์ฒดํฌ ์•ก์…˜์— ์•„์ด์ฝ˜์„ ๋„ฃ๋Š” ๊ฒฝ์šฐ์— On/Off ์ƒํƒœ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์•„์ด์ฝ˜์„ ๋„ฃ์„ ํ•„์š”๊ฐ€ ์žˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด QIcon์— ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ์„ ์ƒํƒœ์— ๋”ฐ๋ผ ์„ค์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค. icon= QIcon(self) icon.addFile(":/images/normal.png", QSize(), QIcon.Normal, QIcon.Off) icon.addFile(":/images/pressed.png", QSize(), QIcon.Normal, QIcon.On) someAction.setIcon(icon) ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹จ์ˆœํ•œ ์ฒดํฌ ์•ก์…˜์€ toggled(bool) ์‹œ๊ทธ๋„์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ triggered(bool) ์‹œ๊ทธ๋„์„ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. toggeled(bool)์€ ์‚ฌ์šฉ์ž๊ฐ€ ๋งˆ์šฐ์Šค ๋“ฑ์œผ๋กœ ์•ก์…˜์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ setCheck(bool) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ณ€๊ฒฝํ•˜๋“  ํ•ญ์ƒ ๋ฐœ์ƒํ•˜๋Š” ์‹œ๊ทธ๋„์ธ ๋ฐ˜๋ฉด, triggered(bool)์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ๋งŒ ๋ฐœ์ƒํ•˜๊ณ  setCheck(bool) ํ˜ธ์ถœ์—๋Š” ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค. ์ด ๋‘˜์€ ๋ฏธ๋ฌ˜ํ•œ ์ฐจ์ด๊ฐ€ ์žˆ๋Š” ๋ฐ ์ž˜ ์„ ํƒํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ setCheck(bool) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ณ  ์•ก์…˜์˜ ์ƒํƒœ๊ฐ€ ๋ณต์žกํ•˜๊ฒŒ ์–ฝํ˜€ ์žˆ๋Š” ๊ฒฝ์šฐ triggered(bool)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. toggled(bool)์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์‹ฌํ•œ ๊ฒฝ์šฐ ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์ง€๊ธฐ๋„ ํ•œ๋‹ค. ์•ก์…˜์˜ ์ƒํƒœ ์•ก์…˜์—๋Š” ํ™œ์„ฑํ™” ์ƒํƒœ์™€ ์ฒดํฌ ์ƒํƒœ๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ์ƒํƒœ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์ค‘ ์ฒดํฌ ์ƒํƒœ๋Š” setCheckable(True)๋กœ ์ฒดํฌ ์•ก์…˜์œผ๋กœ ์„ค์ •ํ–ˆ์„ ๋•Œ๋งŒ ์กด์žฌํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์—๋””ํŒ… ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์–ด๋–ค ์˜์—ญ์„ ์„ ํƒํ–ˆ์„ ๋•Œ๋งŒ ๋ณต์‚ฌ ์•ก์…˜์ด ํ™œ์„ฑํ™”๋˜๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์•ก์…˜์˜ ํ™œ์„ฑํ™” ์ƒํƒœ๋Š” setEnabled(bool) ํ•จ์ˆ˜๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ณ  ๋””ํดํŠธ๋กœ ์•ก์…˜์„ ํ™œ์„ฑํ™” ์ƒํƒœ์— ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์Šฌ๋กฏ ํ•จ์ˆ˜์ด๋ฏ€๋กœ ์ ์ ˆํ•œ ์ƒํ™ฉ์—์„œ ๋ฐ˜์‘ํ•˜๋„๋ก ์‹œ๊ทธ๋„๊ณผ ์—ฐ๊ฒฐํ•˜๋ฉด ๋œ๋‹ค. ์•ก์…˜์˜ ์ˆ˜๊ฐ€ ๋งŽ๊ณ  ๋ณด๋‹ค ๋ณต์žกํ•œ ์ƒํ™ฉ์ด๋ผ๋ฉด ์•ก์…˜์˜ ์ƒํƒœ๋ฅผ ์กฐ์ •ํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜(์˜ˆ๋ฅผ ๋“ค์–ด updateActions(self)๋ผ๋Š” ํ•จ์ˆ˜)๋ฅผ ์ž‘์„ฑํ•œ ๋‹ค์Œ ์ด ํ•จ์ˆ˜์—์„œ ์ผ๊ด„์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ ์Šฌ๋กฏ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด ์–ด๋–ค ์‹œ๊ทธ๋„์— ์—ฐ๊ฒฐํ•˜๊ฑฐ๋‚˜ ์ˆ˜๋™์œผ๋กœ ์ƒํ™ฉ์ด ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœํ•ด ์ฃผ๋ฉด ๋œ๋‹ค. ์ฒดํฌ ์•ก์…˜์˜ ์ฒดํฌ ์ƒํƒœ๋กœ toggled(bool) ์‹œ๊ทธ๋„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋‹ค. Qt๋Š” ์•ก์…˜์„ ๋„์ž…ํ•ด ๋ฉ”๋‰ด, ํˆด๋ฐ”, ์ƒํƒœ ๋ฐ” ๋“ฑ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ผ์›ํ™”ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ MFC(Microsoft Foundation Class Library)์™€ ๋‹ฌ๋ฆฌ ์•ก์…˜์˜ ์ƒํƒœ(ํ™œ์„ฑํ™” ์—ฌ๋ถ€, ์ฒดํฌ ์—ฌ๋ถ€ ๋“ฑ)๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ์ด๋ฒคํŠธ๊ฐ€ ์กด์žฌํ•˜๊ธฐ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ๋•Œ ์ด์ ์— ์œ ์˜ํ•ด์•ผ ํ•œ๋‹ค. ์•ก์…˜ ๊ทธ๋ฃน(QActionGroup) ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๋ช‡ ๊ฐœ์˜ ์ฒดํฌ ์•ก์…˜์„ ๋ฌถ์–ด ๊ทธ์ค‘ ํ•˜๋‚˜๋งŒ ์„ ํƒ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•  ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” QActionGroup์œผ๋กœ ๋ฌถ์–ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. QActionGroup์€ ์ผ๋ฐ˜ ํˆด๋ฒ„ํ„ด์—๋„ ์ ์šฉ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋Œ€๋ถ€๋ถ„ ์ฒดํฌ ๋ฒ„ํ„ด์„ ๋ฌถ๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. redAction = QAction("&Red",self) ... set icon, shortcut, tool tips, status tips redAction.setCheckable(True) redAction.setChecked(True) # initial check greenAction = QAction("&Green",self) ... set icon, shortcut, tool tips, status tips greenAction.setCheckable(True); blueAction = QAction("&Blue",self) ... set icon, shortcut, tool tips, status tips blueAction.setCheckable(True) colorActionGroup = QActionGroup(self) colorActionGroup.addAction(redAction) colorActionGroup.addAction(greenAction) colorActionGroup.addAction(blueAction) redAction.setChecked(True) # actionGroup ์ค‘ ํ•˜๋‚˜์˜ ์•ก์…˜์„ ์ฒดํฌ ์ƒํƒœ๋กœ ์ดˆ๊ธฐํ™” colorActionGroup.triggered.connect(self.setColor) # triggered(QAction*) - self.setColor(QAction*) ์œ„ ์ฝ”๋“œ์—์„œ QActionGroup์— addAction(action)์œผ๋กœ ์ฒดํฌ ๋ฒ„ํ„ด ๊ทธ๋ฃน์— ํฌํ•จ๋œ ์•ก์…˜์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถ”๊ฐ€ํ•œ ํ›„, ์ดˆ๊ธฐ์— ์ฒดํฌ ์ƒํƒœ์— ์žˆ๋Š” ์•ก์„ ์— ๋Œ€ํ•ด QAction.setChecked(True)๋ฅผ ์‹คํ–‰ํ•ด ์ค๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ QActionGroup.triggered(QAction) ์‹œ๊ทธ๋„์„ ์ ๋‹นํ•œ ์Šฌ๋กฏ ํ•จ์ˆ˜์— ์—ฐ๊ฒฐํ•œ๋‹ค. QActionGroup.triggered(QAction) ์‹œ๊ทธ๋„์€ ๊ทธ๋ฃน ๋‚ด์˜ ์ฒดํฌ ์•ก์…˜์ด ์‚ฌ์šฉ์ž์— ์˜ํ•ด ์„ ํƒ๋  ๋•Œ ์„ ํƒ๋œ ์ฒดํฌ ์•ก์…˜์„ ์ธ์ž๋กœ ๋ฐœ์ƒํ•˜๋Š” ์‹œ๊ทธ๋„์ด๋‹ค. ๊ฐœ๋ณ„ ์ฒดํฌ ์•ก์…˜์˜ triggered(bool)๋‚˜ toggled(bool)๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜์ง€๋งŒ ์ฒดํฌ ์•ก์…˜ ๊ทธ๋ฃน์— ๋Œ€ํ•ด์„œ๋Š” ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•˜๋‹ค. ์ƒํƒœ ๋ฐ” ์•ก์…˜์„ ์ƒ์„ฑํ•  ๋•Œ QAction::setStatusTip()์œผ๋กœ ์ง€์ •ํ•œ ๋‚ด์šฉ์€ ์•ก์…˜์— ํฌ์ปค์Šค๊ฐ€ ์žˆ์„ ๋•Œ(์˜ˆ๋ฅผ ๋“ค์–ด ๋ฉ”๋‰ด ์•„์ดํ…œ์ด๋‚˜ ํˆด๋ฐ” ๋ฒ„ํ„ด์— ๋งˆ์šฐ์Šค๊ฐ€ ์˜ฌ๋ ค์งˆ ๋•Œ)๋งŒ ์ƒํƒœ ๋ฐ”์— ํ‘œ์‹œ๋œ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์— ์ƒํƒœ ๋ฐ”์— ์–ด๋–ค ๋‚ด์šฉ์„ ํ‘œํ˜„ํ•˜๋ ค๋ฉด QLabel์„ addWidget() ํ•จ์ˆ˜๋กœ ์ƒํƒœ ๋ฐ”์— ์ถ”๊ฐ€ํ•˜์—ฌ QLabel.setText()๋กœ ์“ฐ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ๋‹ค์Œ์€ ๋‹ค์Œ์ ˆ์—์„œ ๋‹ค๋ฃฐ Shape์ด๋ผ๋Š” ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ƒํƒœ ๋ฐ”์— ๋ผ๋ฒจ์„ ์ถ”๊ฐ€ํ•˜๊ณ , mousePostionChanged()๋ผ๋Š” ์‹œ๊ทธ๋„์— QLabel.setText() ์Šฌ๋กฏ์„ ์—ฐ๊ฒฐํ•œ ๊ฒƒ์ด๋‹ค. self.locationLabel = QLabel(" ( 0, 0) ") self.locationLabel.setAlignment(Qt.AlignHCenter) self.locationLabel.setMinimumSize(locationLabel.sizeHint()) self.shape.mousePositionChanged.connect(self.locationLabel.setText) # locationLabel.setText(str) slot self.status Bar().addWidget(self.locationLabel) ์œ„ ์˜ˆ๋Š” 1๊ฐœ์˜ QLabel ์œ„์ ฏ์„ ์ถ”๊ฐ€ํ–ˆ์ง€๋งŒ ๋‹ค์–‘ํ•œ ์œ„์ ฏ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 4.2 ํˆด๋ฐ”์™€ ๋„ํ‚น ์œ„์ ฏ ํˆด๋ฐ”(QToolBar)์™€ ๋„ํ‚น ์œ„์ ฏ(QDockWidget)๋Š” ๋ฉ”์ธ ์œˆ๋„์— ๋„ํ‚น๋˜๊ฑฐ๋‚˜ ๋…๋ฆฝ๋œ ์œˆ๋„๋กœ ๋–  ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์œˆ๋„์ด๋‹ค. ๋‘˜ ๋‹ค ๋ฉ”์ธ ์œˆ๋„์— ์ƒํ•˜์ขŒ์šฐ 4๊ฐœ์˜ ๋„ํ‚น ์˜์—ญ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ฑฐ์˜ ๋น„์Šทํ•œ ๊ฑฐ๋™์„ ๋ณด์ด์ง€๋งŒ ํˆด๋ฐ”๋Š” addAction(), insertAction()์œผ๋กœ ์‰ฝ๊ฒŒ ํˆด๋ฒ„ํ„ด์„ ๊ฐ–๋Š” ์•ก์…˜์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ addSeparator(), insertSeprator() ๋“ฑ์œผ๋กœ ๊ตฌ๋ถ„์„ ์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ๋„ํ‚น ์œˆ๋„์™€ ๋‹ค๋ฅด๋‹ค. ํˆด๋ฐ” ํˆด๋ฐ”์— ์•ก์…˜์œผ๋กœ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ƒ์„ฑํ•˜๋Š” ํˆด๋ฒ„ํ„ด์œผ๋กœ ๋ถ€์กฑํ•  ๊ฒฝ์šฐ์— QSpinBox, QComboBox ๋“ฑ์„ ๋น„๊ต์  ๊ฐ„๋‹จํ•œ ์œ„์ ฏ์„ addWidget()์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ํˆด๋ฒ„ํ„ด๊ณผ QComboBox๋ฅผ ๋™์‹œ์— ๊ฐ–๋Š” ํˆด๋ฐ”์ด๋‹ค. ... create copyAction, cutAction, ... self.combo = QComboBox(self) self.combo.addItem("Item 1") self.combo.addItem("Item 2") self.combo.currentIndexChanged.connect(self.setSomething) # setSomething(int) self.addToolBarBreak(Qt.TopToolBarArea) # insert toolbar break editToolBar = QToolBar() editToolBar.setObjectName("editToolBar") # assign object name editToolBar.addAction(self.cutAction) editToolBar.addAction(self.copyAction) editToolBar.addWidget(self.combo) self.combo.setMinimumSize(self.combo.sizeHint().width(),editToolBar.iconSize().height()) editToolBar.setAllowedAreas(Qt.TopToolBarArea | Qt.BottomToolBarArea) ์œ„ ์ฝ”๋“œ์—์„œ ๋จผ์ € QComboBox๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ , ํˆด๋ฐ”์— addWidget()์œผ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค. QComboBox์—์„œ ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ์‹œ๊ทธ๋„์€ currentIndexChanged(int)์™€ currentIndexChanged(str)์ด๋‹ค. ์ด ์‹œ๊ทธ๋„๋กœ ์ ์ ˆํ•œ ์ปค์Šคํ…€ ์‹œ๊ทธ๋„์— ์—ฐ๊ฒฐํ•˜์—ฌ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•œ๋‹ค. ํˆด๋ฐ”๊นŒ์ง€ ์ƒ์„ฑํ•œ ํ›„์— setMinimunSize()๋ฅผ ํ˜ธ์ถœํ•œ ๋ถ€๋ถ„์ด ์žˆ๋‹ค. ์ด๊ฒƒ์€ ํˆด๋ฒ„ํ„ด์˜ ํฌ๊ธฐ์™€ ๋†’์ด๋ฅผ ๊ฐ™๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ํ˜ธ์ถœ๋œ ๊ฒƒ์ด๋‹ค. ํˆด๋ฐ”์˜ ์œ„์น˜๋ฅผ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด setAllowedAreas()๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค. ์ด ํ•จ์ˆ˜ ํ˜ธ์ถœ๋กœ ํˆด๋ฐ”๊ฐ€ ๋ฉ”์ธ ์œˆ๋„์˜ ์ƒ, ํ•˜์—๋งŒ ๋ถ™๋„๋ก ํ•œ๋‹ค. QMainWindow.addToolBarBreak()๋Š” ์—ฌ๋Ÿฌ ํˆด๋ฐ”๊ฐ€ ๊ฐ™์€ ์˜์—ญ์— ์žˆ์„ ๋•Œ ๋‹ค์Œ ์—ด์ด ๋‚˜ ํ–‰์— ๋‚˜๋ˆ„๊ธฐ๋ฅผ ํ•˜๋„๋ก ํ•œ๋‹ค. addToolBarBreak()์™€ ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํ•จ์ˆ˜๋กœ ํŠน์ • ์œ„์น˜์— ๊ตฌ๋ถ„์„ ์„ ๋„ฃ์€ insertToolBarBreak(beforeToolBar)๊ฐ€ ์žˆ๋‹ค. setObjectName()์€ ๊ฐ์ฒด์˜ ๋ช…์นญ์„ ์ง€์ •ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋’ค์— ์„ค๋ช…ํ•œ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ ์•ก์…˜์œผ๋กœ ์ถ”๊ฐ€ ๊ฐ€๋Šฅํ•œ ํˆด๋ฒ„ํ„ด ์ด์™ธ์—, QComboBox ๋“ฑ ๋‹ค์–‘ํ•œ ์œ„์ ฏ์œผ๋กœ ๊ตฌ์„ฑํ•œ ํˆด๋ฐ” ์˜ˆ์ด๋‹ค. ํˆด๋ฐ”์˜ ๋„ํ‚น ์˜์—ญ์€ ๋””ํดํŠธ๋กœ ๋ฉ”์ธ ์œˆ๋„์˜ 4๊ฐœ์˜ ๋ชจ์„œ๋ฆฌ ์˜์—ญ์— ๋„ํ‚น๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ์ œ์–ดํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ QToolBar.setAllowedAreas(areas)์ด๋‹ค. ๋˜ํ•œ QMainWIndow.addToolBar(area, toolbar) ํ•จ์ˆ˜๋กœ ๋ฉ”์ธ ์œˆ๋„์˜ ํˆด๋ฐ”๋กœ ๋“ฑ๋กํ•˜๋Š”๋ฐ ์ด๋•Œ ์ดˆ๊ธฐ ํˆด๋ฐ” ์œ„์น˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ๋˜๋Š” ์ธ์ž๋Š” Qt.ToolBarArea ์—ด๊ฑฐ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ๋‹ค์Œ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Qt.ToolBarArea : Qt.LeftToolBarArea, Qt.RightToolBarArea, Qt.TopToolBarArea, Qt.BottomToolBarArea, Qt.AllToolBarAreas, Qt::NoToolBarArea ์˜ˆ๋ฅผ ๋“ค์–ด QMainWindow.addToolBar(toolbar)๋Š” QMainWindow.addToolBar(Qt.TopToolBarArea, toolbar)๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋„ํ‚น ์œ„์ ฏ ๋„ํ‚น ์œ„์ ฏ(QDockWidget)์€ ์ˆ˜ํ‰ ๋˜๋Š” ์ˆ˜์ง์œผ๋กœ๋งŒ ์กด์žฌํ•˜๋Š” ํˆด๋ฐ”(QToolBar)์™€ ๋‹ฌ๋ฆฌ ์ž์œ ๋กœ์šด ํ˜•ํƒœ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ ์œ„์ ฏ์„ ์ž์œ ๋กญ๊ฒŒ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. QDockWidget์€ ๋‹ค๋ฅธ ์œ„์ ฏ์„ ๋ž˜ํ•‘(wrapping) ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ๋ž˜ํ•‘ ํ•  ์œ„์ ฏ์„ QDockWidget.setWidget(contentWidget)์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ž˜ํ•‘(wrapping) ํ•œ๋‹ค. ๋„ํ‚น ์œˆ๋„๋Š” ์ž์‹ ๋งŒ์˜ ํƒ€์ดํ‹€ ๋ฐ”๋ฅผ ๊ฐ€์ง€๋ฉด ๋‹ซ๊ธฐ ๋ฒ„ํ„ด ์—ญ์‹œ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋งŒ์•ฝ ํƒ€์ดํ‹€๋ฐ”๊ฐ€ ์žˆ๋Š” ํˆด๋ฐ”๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด QToolBar๋ฅผ ๋‹จ์ˆœํžˆ QDockWidget์œผ๋กœ ๋ž˜ํ•‘ ํ•˜๋ฉด ๋œ๋‹ค. ๋‹ค์Œ์€ QWidget์„ ์ปจํ…Œ์ด๋„ˆ๋กœ ํ•˜์—ฌ ์—ฌ๋Ÿฌ ์œ„์ ฏ์„ ๋ฐฐ์น˜ํ•œ ํ›„ QDockWidget์— ๋ž˜ํ•‘ ํ•œ ์˜ˆ์ด๋‹ค. dockWidgetContent = QWidget() ... widget์— ๋ฒ„ํ„ด, ์ฝค๋ณด ๋ฐ•์Šค ๋“ฑ์„ ์ถ”๊ฐ€ self.dockingWidget = QDockWidget("Icon panel") # ํƒ€์ดํ‹€ ์„ค์ • self.dockingWidget.setObjectName("IconPanel") self.dockingWidget.setWidget(dockWidgetContent) # ๋ž˜ํ•‘ ํ•  ์œ„์ ฏ ์„ค์ • self.dockingWidget.setAllowedAreas(Qt.LeftDockWidgetArea|Qt.RightDockWidgetArea) self.dockingWidget.setFloating(True) self.addDockWidget(Qt.RightDockWidgetArea, self.dockingWidget) # ์ดˆ๊ธฐ ์œ„์น˜ ๋ฐ ๋„ํ‚น ์œ„์ ฏ์„ ๋ฉ”์ธ ์œˆ๋„์˜ # ๋„ํ‚น ์œˆ๋„๋กœ ์„ค์ • ๋‹ค์Œ์€ ํŠธ๋ฆฌ ์œ„์ ฏ(QTreeWidget)์„ ๋ž˜ํ•‘ ํ•œ ์˜ˆ์ด๋‹ค. self.shapesDockWidget = QDockWidget("Shapes") self.shapesDockWidget.setObjectName("shapesDockWidget"); self.shapesDockWidget.setWidget(treeWidget); self.shapesDockWidget.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea) self.shapeDockWidget.setFloating(True) self.addDockWidget(Qt.RightDockWidgetArea, self.shapesDockWidget); ์œ„ ์ฝ”๋“œ์—์„œ QDockWidget.setAllowedAreas()๋Š” ๋„ํ‚น ์œ„์ ฏ์ด ๋„ํ‚น๋  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ์„ ์ง€์ •ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋˜ํ•œ QMainWindow.addDockWidget(dockWidget)์—์„œ ๋„ํ‚น ์œ„์ ฏ์˜ ์ดˆ๊ธฐ ์œ„์น˜์™€ ๋ถ€๋ชจ ์œ„์ ฏ์œผ๋กœ ๋ฉ”์ธ ์œˆ๋„๋ฅผ ์„ค์ •ํ•œ๋‹ค. QMainWindow::setCorner(corner, area)๋Š” ์ฝ”๋„ˆ ์˜์—ญ์„ ์ขŒ์šฐ ๋„ํ‚น ์œ„์ ฏ์ด ์ฐจ์ง€ํ• ์ง€ ์ƒํ•˜ ๋„ํ‚น ์œ„์ ฏ์ด ์ฐจ์ง€ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. ๋””ํดํŠธ๋Š” ์ƒํ•˜ ๋„ํ‚น ์œ„์ ฏ์ด ์ฝ”๋„ˆ ์˜์—ญ์„ ์šฐ์„ ์ ์œผ๋กœ<NAME>๋‹ค. QDockWidget.setAllowedAreas(area) ; QMainWindow.addDockWidget(area, dockwidget) QMainWindow.setCorner(corner, area) ์œ„์—์„œ area๋Š” Qt.DockWidgetArea, corner๋Š” Qt.Corner ์—ด๊ฑฐ ์ƒ์ˆ˜๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. enum Qt.DockWidgetArea Qt.LeftDockWidgetArea Qt.RightDockWidgetArea Qt.TopDockWidgetArea Qt.BottomDockWidgetArea Qt.AllDockWidgetAreas Qt.NoDockWidgetArea enum Qt.Corner Qt.TopLeftCorner Qt.TopRightCorner Qt.BottomLeftCorner Qt.BottomRightCorner QMainWindow๋Š” ์ž์‹ ์˜ ๋ชจ๋“  ํˆด๋ฐ”, ๋„ํ‚น ์œˆ๋„์— ๋Œ€ํ•œ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ์ž๋™์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค. ์ด์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ๋„ํ‚น ์œˆ๋„์™€ ํˆด๋ฐ”๋ฅผ ๋‹ซ๊ฑฐ๋‚˜ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ ํˆด๋ฐ” ์˜์—ญ ์ค‘ ๋นˆ ๊ณต๊ฐ„์—์„œ ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํ„ด์„ ์„ ํƒํ•ด ํ‘œ์‹œ๋˜๋Š” ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด์˜ ์˜ˆ๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. 4.4 Shape ์˜ˆ์ œ ๋ฉ”์ธ ์œˆ๋„์˜ ์‚ฌ์šฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด Shape์ด๋ผ๋Š” ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณธ๋‹ค. ์ด๋ฏธ์ง€ ์ค€๋น„ ๋ฆฌ์†Œ์Šค ์ค€๋น„(Shape.qrc) <RCC> <qresource prefix="/"> <file>images/qt.png</file> <file>images/circle.png</file> <file>images/exit.png</file> <file>images/rectangle.png</file> <file>images/triangle.png</file> </qresource> </RCC> ๋ฆฌ์†Œ์Šค ์ปดํŒŒ์ผ >pyside2-rcc -o Shape_rc.py -py3 Shape.qrc Shape1.py ## ShapeWidget from PySide2.QtWidgets import QWidget from PySide2.QtGui import QPalette, QPainter from PySide2.QtCore import Signal, Qt, QRect, QPointF class ShapeWidget(QWidget): mousePositionChanged = Signal(str) # mousePositionChanged(pos) NONE, RECTANGLE, TRIANGLE, CIRCLE = 0, 1, 2, 3 # constants def __init__(self, parent=None): QWidget.__init__(self, parent) self.setBackgroundRole(QPalette.Light) self.setAutoFillBackground(True) self.shape = ShapeWidget.NONE self.color = Qt.blue # slots def rectangle(self): self.shape = ShapeWidget.RECTANGLE self.update() def triangle(self): self.shape = ShapeWidget.TRIANGLE self.update() def circle(self): self.shape = ShapeWidget.CIRCLE self.update() def red(self): self.color = Qt.red self.update() def green(self): self.color = Qt.green self.update() def blue(self): self.color = Qt.blue self.update() def setMouseTracking(self, track): QWidget.setMouseTracking(self, track) # event handler def mouseMoveEvent(self, event): pos = "({},{})".format(event.x(),event.y()) self.mousePositionChanged.emit(pos) def paintEvent(self, event): painter = QPainter(self) painter.setPen(self.color) r = QRect(self.width()/4, self.height()/4, self.width()/2, self.height()/2) if self.shape == ShapeWidget.RECTANGLE: painter.drawRect(r) elif self.shape == ShapeWidget.TRIANGLE: points = [ QPointF(r.left()+r.width()/2, r.top()), r.bottomLeft(), r.bottomRight()] painter.drawPolygon(points) elif self.shape == ShapeWidget.CIRCLE: painter.drawEllipse(r) from PySide2.QtWidgets import (QMainWindow, QAction, QActionGroup, QToolBar, QLabel, QMessageBox) from PySide2.QtGui import QIcon from PySide2.QtCore import QSettings import Shape_rc class MainWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Shape') self.setWindowIcon(QIcon(":/images/qt.png")) self.shapeWidget = ShapeWidget() self.setCentralWidget(self.shapeWidget) self.createActions(); self.createMenus(); self.createContextMenu(); self.createToolBar(); self.createStatusBar(); self.readSettings(); def createActions(self): # create actions self.exitAction = QAction("E&xit",self) self.exitAction.setIcon(QIcon(":/images/exit.png")) self.exitAction.setShortcut("Ctrl+Q") self.exitAction.setStatusTip("Exit the application") self.exitAction.triggered.connect(self.close) self.triangleAction = QAction("&Triangle",self) self.triangleAction.setIcon(QIcon(":/images/triangle.png")) self.triangleAction.setShortcut("Ctrl+T"); self.triangleAction.setStatusTip("Draw a triangle") self.triangleAction.triggered.connect(self.shapeWidget.triangle) self.rectangleAction = QAction("&Rectangle",self) self.rectangleAction.setIcon(QIcon(":/images/rectangle.png")); self.rectangleAction.setShortcut("Ctrl+R") self.rectangleAction.setStatusTip("Draw a rectangle"); self.rectangleAction.triggered.connect(self.shapeWidget.rectangle) self.circleAction = QAction("&Circle",self) self.circleAction.setIcon(QIcon(":/images/circle.png")) self.circleAction.setShortcut("Ctrl+C") self.circleAction.setStatusTip("Draw a circle") self.circleAction.triggered.connect(self.shapeWidget.circle) # actions for colors self.redAction = QAction("&Red",self) self.redAction.setStatusTip("Set red color") self.redAction.setCheckable(True) self.redAction.triggered.connect(self.shapeWidget.red) self.greenAction = QAction("&Green",self) self.greenAction.setStatusTip("Set green color") self.greenAction.setCheckable(True); self.greenAction.triggered.connect(self.shapeWidget.green) self.blueAction = QAction("&Blue",self) self.blueAction.setStatusTip("Set blue color") self.blueAction.setCheckable(True) self.blueAction.triggered.connect(self.shapeWidget.blue) self.colorActionGroup = QActionGroup(self) self.colorActionGroup.addAction(self.redAction); self.colorActionGroup.addAction(self.greenAction); self.colorActionGroup.addAction(self.blueAction); self.redAction.setChecked(True); self.colorActionGroup.triggered.connect(self.setColor) # (triggered(QAction*)),this, SLOT(setColor(QAction*))); # mouse tracking self.mouseTrackingAction = QAction("M&ouse tracking",self) self.mouseTrackingAction.setStatusTip("mouse tracking on/off") self.mouseTrackingAction.setCheckable(True) self.mouseTrackingAction.setChecked(self.shapeWidget.hasMouseTracking()) self.mouseTrackingAction.triggered.connect(self.shapeWidget.setMouseTracking) #connect(mouseTrackingAction, SIGNAL(triggered(bool)), shapeWidget, SLOT(setMouseTracking(bool))); # about self.aboutAction = QAction("&About",self) self.aboutAction.setStatusTip("Show the application's About box") self.aboutAction.triggered.connect(self.about) def createMenus(self): fileMenu = self.menuBar().addMenu("&File") fileMenu.addAction(self.exitAction) shapeMenu = self.menuBar().addMenu("&Shape") shapeMenu.addAction(self.triangleAction); shapeMenu.addAction(self.rectangleAction); shapeMenu.addAction(self.circleAction); colorMenu = self.menuBar().addMenu("&Color") colorMenu.addAction(self.redAction) colorMenu.addAction(self.greenAction) colorMenu.addAction(self.blueAction) mouseMenu = self.menuBar().addMenu("&Mouse") mouseMenu.addAction(self.mouseTrackingAction) aboutMenu = self.menuBar().addMenu("&About") aboutMenu.addAction(self.aboutAction) def createContextMenu(self): self.shapeWidget.addAction(self.triangleAction) self.shapeWidget.addAction(self.rectangleAction) self.shapeWidget.addAction(self.circleAction) self.shapeWidget.addAction(self.redAction) self.shapeWidget.addAction(self.greenAction) self.shapeWidget.addAction(self.blueAction) self.shapeWidget.setContextMenuPolicy(Qt.ActionsContextMenu) def createToolBar(self): shapeToolBar = self.addToolBar("&Shape") shapeToolBar.setObjectName("ShapeToolBar") shapeToolBar.addAction(self.triangleAction) shapeToolBar.addAction(self.rectangleAction) shapeToolBar.addAction(self.circleAction) def createStatusBar(self): locationLabel = QLabel(" ( 0, 0) ") locationLabel.setAlignment(Qt.AlignHCenter) locationLabel.setMinimumSize(locationLabel.sizeHint()) self.shapeWidget.mousePositionChanged.connect(locationLabel.setText) # shapeWidget.mousePositionChanged(str) - QLabel.setText(str) self.status Bar().addWidget(locationLabel) def readSettings(self): settings = QSettings("Qt5Programming Inc.", "Shape") self.restoreGeometry(settings.value("geometry")) self.restoreState(settings.value("state")) def writeSettings(self): settings = QSettings("Qt5Programming Inc.", "Shape") self.saveGeometry() settings.setValue("geometry", self.saveGeometry()) settings.setValue("state",self.saveState()) # event def closeEvent(self, event): self.writeSettings() # slot def setColor(self, action): if action == self.redAction: self.shapeWidget.red() elif action == self.greenAction: self.shapeWidget.green() else: self.shapeWidget.blue() def about(self): QMessageBox.about(self, "About Shape", "<h2>Shape 1.0</h2>" "<p>Copyright ยฉ 2014 Q5Programming Inc." "<p>Shape is a small application that " "demonstrates QAction, QMainWindow, QMenuBar, " "QStatusBar, QToolBar, and many other " "Qt classes.") import sys from PySide2.QtWidgets import QApplication if __name__ == '__main__': app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() app.exec_() 4.6 ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋Š” ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค์˜ ๋ˆŒ๋ฆผ ํ•ด์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ˆœ๊ฐ„ ํŒ์—… ๋˜๋Š” ๋ฉ”๋‰ด๋ฅผ ์˜๋ฏธํ•œ๋‹ค. Qt๋Š” ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค ํด๋ฆญ์ด ์ผ์–ด๋‚˜๋ฉด mousePressEvent(), mouseReleaseEvent(), contextMenuEvent()์˜ ์ˆœ์„œ๋กœ ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ฒŒ ๋œ๋‹ค. ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด contextMenuEvent() ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ(override) ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค. def contextMenuEvent(self, event): # QContextMenuEvent ... ์ด์ œ contextMenuEvent() ํ•ธ๋“ค๋Ÿฌ์—์„œ QMenu ๊ฐ์ฒด์— ๋Œ€ํ•ด exec_() ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ํŒ์—…๋ฉ”๋‰ด๊ฐ€ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋œ๋‹ค. ๋‹ค์Œ์€ ์ปค์Šคํ…€ ์œ„์ ฏ์—์„œ contextMenuEvent() ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. class myWidget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) ... ์•ก์…˜ ์ƒ์„ฑ - self.copyAction, self.deleteAction, self.pasteAction, ... def contextMenuEvent(self, event): # QContextMenuEvent menu = QMenu(self) menu.addAction(self.cutAct) menu.addAction(self.copyAct) menu.addAction(self.pasteAct) menu.addSeperator() ... menu.exec_(event.globalPos()) ... ์œ„ ์ฝ”๋“œ๋Š” myWidget ์†Œ์œ ์˜ self.copyAction, self.deleteAction, self.pasteAction์„ ์ƒ์„ฑ์ž์—์„œ ๋งŒ๋“ค์–ด ๋‘” ๋‹ค์Œ contextMenEvent() ํ•ธ๋“ค๋Ÿฌ์—์„œ ๋ฉ”๋‰ด QMenu๋ฅผ ์Šคํƒ์— ์ƒ์„ฑํ•˜๊ณ , exec_()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด ์ž์ฒด๋ฅผ ์œ„์ ฏ ์†Œ์œ ๋กœ ๋งŒ๋“ค์–ด ๋ฏธ๋ฆฌ ์ƒ์„ฑ์ž์—์„œ ๋งŒ๋“ค์–ด ๋‘” ๋‹ค์Œ contextMenuEvent() ํ•ธ๋“ค๋Ÿฌ์—์„œ exec_()๋งŒ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. class myWidget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) ... ์•ก์…˜ ์ƒ์„ฑ - self.copyAction, self.deleteAction, self.pasteAction, ... self.contextMenu = QMenu(self) self.contextMenu = QMenu(self) self.contextMenu.addAction(self.cutAct) self.contextMenu.addAction(self.copyAct) self.contextMenu.addAction(self.pasteAct) self.contextMenu.addSeperator() ... def contextMenuEvent(self, event): # QContextMenuEvent self.contextMenu.exec_(event.globalPos()) ... ์‚ฌ์‹ค ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ์œ„ํ•œ ์ „์šฉ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์ธ contextMenuEvent()๊ฐ€ ์—†๋”๋ผ๋„ mouseReleaseEvent() ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. Qt๊ฐ€ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ์œ„ํ•œ ์ „์šฉ ์ด๋ฒคํŠธ์™€ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ด์œ ๋Š” QWidget์— ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด์™€ ๊ด€๋ จ๋œ ๋ช‡ ๊ฐ€์ง€ ์ •์ฑ…์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด ์‰ฝ๊ฒŒ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ๊ตฌํ˜„ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. ๋‹ค์Œ์€ ๊ทธ ์ •์ฑ…์„ ์ •ํ•˜๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค. QWidget.setContextMenuPolicy(contextMenuPolicy) contextMenuPolicy๋Š” Qt.ContextMenuPolicy๋ผ๋Š” enum์œผ๋กœ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉฐ ๋‹ค์Œ์˜ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. Qt.DefaultContextMenu : contextMenuEvent() ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ˜ธ์ถœ (QWidget์˜ ๋””ํดํŠธ ๊ฐ’). Qt.DefaultContextMenu๊ฐ€ contextMenuEvent() ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ธฐ๋ณธ ๋ฐฉ์‹์ด๋‹ค. ๋‚˜๋จธ์ง€ ์ •์ฑ…์€ ๋งˆ์šฐ์Šค ๋ฒ„ํ„ด์ด ๋ˆŒ๋ฆผ ํ•ด์ œ๋˜์–ด๋„ ์ด ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ปค์Šคํ…€ ์œ„์ ฏ์—์„œ Qt.DefaultContextMenu ์ •์ฑ…์„ ๋ถ€์—ฌํ•˜๊ณ  contextMenuEvent()๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” QContextMenuEvent๋Š” ๋””ํดํŠธ๋กœ accepted ์ƒํƒœ์ด๋‹ค. ์ฆ‰ ์กฐ์ž‘์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ทธ ์œ„์ ฏ์—์„œ ์ฒ˜๋ฆฌ๋œ ํ›„ ๋ถ€๋ชจ ์œ„์ ฏ์œผ๋กœ ๋ฉ”์‹œ์ง€๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋””ํดํŠธ๋กœ QWidget์€ Qt.DefaultContextMenu๋ฅผ ์ •์ฑ…์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฉฐ QWidget.contextMenuEvent() ํ•ธ๋“ค๋Ÿฌ์—์„œ๋Š” ์ด๋ฒคํŠธ๋ฅผ ๊ฐ•์ œ๋กœ ๋ฌด์‹œ(ignore) ํ•œ๋‹ค. ์ด๊ฒƒ์€ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€๋งŒ ์ปค์Šคํ…€ ์œ„์ ฏ์—์„œ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์žฌ์ •์˜ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ถ€๋ชจ ์œ„์ ฏ์œผ๋กœ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด ์ด๋ฒคํŠธ๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. Qt.NoContextMenu : ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด ์ด๋ฒคํŠธ๋ฅผ ์ž์‹ ์ด ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ  ๋ณด๋ชจ ์œ„์ ฏ์œผ๋กœ ์œ„์ž„ (์œ„์ ฏ์— ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๊ฐ€ ์—†์„ ๋•Œ ์ฃผ๋กœ ์„ค์ •). Qt::NoContextMenu๋Š” contextMenuEvent()์—์„œ ์•„๋ฌด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š๊ณ  ๋‹จ์ˆœํžˆ QContextMenuEvent์„ ignored ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด ๋ถ€๋ชจ ์œ„์ ฏ์œผ๋กœ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„์ž„ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. Qt.PreventContextMenu : ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด ์ด๋ฒคํŠธ๋ฅผ ์ž์‹ ์ด ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ณด๋ชจ ์œ„์ ฏ์œผ๋กœ ์œ„์ž„ํ•˜์ง€๋„ ์•Š์Œ. ์ด ์ •์ฑ…์„ ๋ถ€๊ณผํ•œ ์œ„์ ฏ์€ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๊ฐ€ ํŒ์—… ๋˜์ง€ ์•Š์Œ. Qt::PreventContextMenu๋Š” Qt.NoContextMenu์™€ ๊ฐ™์ง€๋งŒ QContextMenuEvent์˜ ์†์„ฑ์„ accept๋กœ ์œ ์ง€ํ•˜์—ฌ ๋ถ€๋ชจ ์œ„์ ฏ์—์„œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ๋ชปํ•˜๋„๋ก ํ•œ๋‹ค. Qt.ActionsContextMenu : ์ด ์ •์ฑ…์„ ์„ค์ •ํ•˜๋ฉด ์œ„์ ฏ์˜ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ์•ก์…˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋กœ ํ‘œ์‹œํ•œ๋‹ค. ์œ„์ ฏ์— ๋”ฐ๋ผ ์ž์‹ ๋งŒ์˜ ์•ก์…˜์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์„œ ๊ทธ ์•ก์…˜์„ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋กœ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด QLineEdit์™€ ๊ฐ™์€ ์—๋””ํŒ… ์œ„์ ฏ๋“ค์€ ๋ณต์‚ฌ, ์‚ญ์ œ ๋“ฑ๊ณผ ๊ฐ™์€ ์ž์‹ ๋งŒ์˜ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ๋””ํดํŠธ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ Qt๋Š” QWidget์ด ์•ก์…˜ ๋ฆฌ์ŠคํŠธ(QAction์˜ ๋ฆฌ์ŠคํŠธ)๋ฅผ ์œ ์ง€ํ† ๋ก ์„ค๊ณ„๋˜์–ด ์žˆ๋‹ค. Qt.CustomContextMenu : customContextMenuRequest(point) ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ ์‹œ. Qt.CustomContextMenu ์ •์ฑ…์€ ๋ถ€๋ชจ ์œ„์ ฏ(๋ณดํ†ต ๋ฉ”์ธ ์œˆ๋„)์—์„œ ์ž์‹ ์œ„์ ฏ์˜ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ์„ธ๋ จ๋˜๊ฒŒ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„ ์˜ˆ์—์„œ rectangleAction, redAction ๋“ฑ์€ ๋ฉ”์ธ ์œˆ๋„๊ฐ€ ์†Œ์œ ํ•˜๊ณ  ์žˆ๋Š” ์•ก์…˜์ด๊ธฐ ๋•Œ๋ฌธ์— shape ์œ„์ ฏ์—์„œ ๊ตฌ์ฒด์ ์ธ ๋ฉ”๋‰ด ํ˜•ํƒœ๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค. ์ด๋•Œ๋Š” Qt.CustomContextMenu ์ •์ฑ…์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. class MainWindow(QMainWindow): def __init__(self, parent=None): self.shape = Shape() self.setCentralWidget(self.shape) self.exitAction = QAction("E&xit", self) ... self.shape.setContextMenuPolicy(Qt.CustomContextMenu) self.shape.customContextMenuRequested.connect(self.showShapeContextMenu) def showShapeContextMenu(self, pos): menu = QMenu(self) ... triangleAction, rectangleAction ๋“ฑ์œผ๋กœ ๋ฉ”๋‰ด๊ตฌ์„ฑ menu.exec_(QCursor.pos()); # do not use pos in args, which is relative to widget .... Qt::CustomContextMenu์˜ ๋˜ ๋‹ค๋ฅธ ์‚ฌ์šฉ ์˜ˆ๋Š” ์ž์‹ ์œ„์ ฏ์ด ์•ก์…˜์„ ์†Œ์œ ํ•˜๊ณ  ์žˆ๊ณ , ์ด ์•ก์…˜์— ๋‹ค๋ฅธ ์•ก์…˜์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ ์ž ํ•  ๋•Œ์ด๋‹ค. class MainWindow(QMainWindow): def __init__(self, parent=None): self.myWidget = MyWidget() # myWidget์€ copy, cut ๋“ฑ์˜ ์•ก์…˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ. self.setCentralWidget(self.myWidget) self.myWidget.setContextMenuPolicy(Qt.CustomContextMenu); self.myWidget.customContextMenuRequested.connect(showShapeContextMenu) ... def showShapeContextMenu(self, pos): menu = QMenu(self) menu.addActions(self.myWidget); ... MainWindow์˜ ์•ก์…˜ ์ถ”๊ฐ€ menu.exec_(QCursor::pos()) ๋งˆ์ง€๋ง‰์— ์„ค๋ช…ํ•œ ๋ถ€๋ถ„์—์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ์‹œ๊ทธ๋„-์Šฌ๋กฏ์˜ ์œ„๋ ฅ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด, ์œ„์ ฏ์„ ์„ค๊ณ„ํ•  ๋•Œ ์ž์‹ ๊ณผ ์ž์‹ ๊ณผ ๊ด€๋ จ๋œ ์•ก์…˜์„ ๊ฐ€์ง€๋„๋ก ์„ค๊ณ„ํ•  ๋•Œ๋Š” ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๊ณผ ๊ด€๋ จํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ์ปค์Šคํ…€ ์œ„์ ฏ์ด Qt.DefaultContextMenu ์ •์ฑ…์„ ๊ฐ–๋„๋ก ํ•˜๊ณ  contextMenuEvent()๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๋งŒ์•ฝ ์ปค์Šคํ…€ ์œ„์ ฏ์„ ์‚ฌ์šฉํ•˜๋Š” ์œ„์ ฏ(์ฃผ๋กœ ๋ฉ”์ธ ์œˆ๋„)์—์„œ ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” Qt.CustomContextMenu ์ •์ฑ…์„ ๋ถ€๊ณผํ•˜๊ณ  customContextMenuRequested()์™€ ์—ฐ๊ฒฐํ•˜๋Š” ์Šฌ๋กฏ์„ ์žฌ์ •์˜ํ•˜๋„๋ก ํ•œ๋‹ค. ํŠน์ˆ˜ํ•œ ๊ฒฝ์šฐ๋กœ ๋ฏธ๋ฆฌ ์ •์˜๋œ custumContextMenuRequested()๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ง์ ‘ ์‹œ๊ทธ๋„์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Shift ํ‚ค์™€ ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค ๋ฒ„ํ„ด์ด ํ•จ๊ป˜ ๋ˆ„๋ฅธ ํ›„ ์ดํ›„ ๋งˆ์šฐ์Šค ์›€์ง์ž„์— ๋”ฐ๋ผ ํ™”๋ฉด์„ ํ™•๋Œ€ํ•˜๋Š” ์œ„์ ฏ์„ ์ž‘์„ฑํ•œ๋‹ค๊ณ  ํ•˜๋ฉด customContextMenuRequested()๋Š” ์•ฝ๊ฐ„ ์ด์ƒํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค ๋ฒ„ํ„ด์ด ํ•ด์ œ๋  ๋•Œ ํ•ญ์ƒ ์ด ์‹œ๊ทธ๋„์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” Qt.PreventContextMenu ์ •์ฑ…์„ ์ง€์ •ํ•˜๊ณ  mouseReleaseEvent()์—์„œ ์กฐ๊ฑด์— ๋งž๋Š” ์ปค์Šคํ…€ ์‹œ๊ทธ๋„(์˜ˆ๋ฅผ ๋“ค์–ด myConstextedMenuRequested()์„ ๋ฐœ์ƒ์‹œํ‚ค๋„๋ก ํ•œ๋‹ค. ์ด ์‹œ๊ทธ๋„์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ customContextMenuRequested()์™€ ๋™์ผํ•˜๋‹ค. class MyZoomWidget(QWidget): contextMenuRequested = Signal() def __init__(self, parent=None): QWidget.__init__(self, parent) self.setContextMenuPolicy(Qt::PreventContextMenu); ... def mouseReleaseEvent(self, event): # QMouseEvent if e->button() == Qt.RightButton: if in zoom mode ... something for zooming else contextMenuRequested.emit() 5. ์œ„์ ฏ ๊ณ ๊ธ‰ ์ด๋ฒˆ ์žฅ์—์„œ๋Š” QWidget์— ๋Œ€ํ•ด ์‹ฌํ™”ํ•˜์—ฌ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 5.1 ์ด๋ฒคํŠธ ์ด๋ฒคํŠธ์™€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ด๋ฒคํŠธ ํ˜น์€ ๋ฉ”์‹œ์ง€๋ผ๋Š” ๊ฒƒ์€ GUI ์šด์˜ ์ฒด๊ณ„์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐœ๋…์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์–ด๋–ค ํ”„๋กœ๊ทธ๋žจ์— ๋Œ€ํ•ด ๋งˆ์šฐ์Šค ์กฐ์ž‘์„ ํ•˜๋Š” ๊ฒฝ์šฐ ๊ด€๋ จ ์ด๋ฒคํŠธ๋ฅผ ๊ทธ ํ”„๋กœ๊ทธ๋žจ์— ๋ฐœ์ƒ์‹œ์ผœ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์€ ์ž์‹ ๋งŒ์˜ ์ด๋ฒคํŠธํ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ๊ด€๋ จ๋œ ์œ„์ ฏ์— ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์š”์ฒญํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, QWidget์€ ๋ฏธ๋ฆฌ ์ •์˜๋œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๊ฐ€์ƒํ•จ ์ˆ˜๋กœ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์žฌ์ •์˜ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋‹ค์Œ์€ QWidget์—์„œ ์ •์˜ํ•˜๊ณ  ์žˆ๋Š” ์ฃผ์š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์˜ ์ธ์ž๋กœ๋Š” QEvent๋ฅผ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ๊ฐ–๋Š” ๊ฐ์ข… ์ด๋ฒคํŠธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. class QWidget: ... def paintEvent(self, event): # QPaintEvent ๊ฐ์ฒด, ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ธฐ๊ฐ€ ํ•„์š”ํ•  ๋•Œ def void resizeEvent(self, event): # QResizeEvent ๊ฐ์ฒด, ์œ„์ ฏ ํฌ๊ธฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ def void enterEvent(self, event): # QEvent ๊ฐ์ฒด, ๋งˆ์šฐ์Šค๊ฐ€ ์œ„์ ฏ ๋‚ด๋กœ ๋“ค์–ด์™”์„ ๋•Œ def void leaveEvent(self, event): # QEvent ๊ฐ์ฒด, ๋งˆ์šฐ์Šค๊ฐ€ ์œ„์ ฏ ๋ฐ–์œผ๋กœ ๋‚˜๊ฐˆ ๋•Œ def void mouseMoveEvent(self, event): # QMouseEvent ๊ฐ์ฒด, ๋งˆ์šฐ์Šค ์ด๋™ ์‹œ def mousePressEvent(self, event): # QMouseEvent ๊ฐ์ฒด, ๋งˆ์šฐ์Šค ๋ฒ„ํ„ด(์™ผ์ชฝ์ด๋“  ์˜ค๋ฅธ์ชฝ์ด๋˜)์ด ๋ˆ„๋ฅผ ๋•Œ def mouseReleaseEvent(self, event): # QMouseEvent ๊ฐ์ฒด, ๋ˆŒ๋Ÿฌ์ง„ ๋งˆ์šฐ์Šค ๋ฒ„ํ„ด์„ ํ•ด์ œํ•  ๋•Œ def mouseDoubleClickEvent(self, event): # QMouseEvent ๊ฐ์ฒด, ๋”๋ธ”ํด๋ฆญ ์‹œ def wheelEvent(self, event): # QWheelEvent ๊ฐ์ฒด, ๋งˆ์šฐ์Šค ํœ  def contextMenuEvent(self, event): # QContextMenuEvent ๊ฐ์ฒด, ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด๊ฐ€ ํ•„์š”ํ•  ๋•Œ(์˜ค๋ฅธ ๋งˆ์šฐ์Šค ๋ฒ„ํ„ด) def focusInEvent(self, event): # QFocusEvent ๊ฐ์ฒด, ํ‚ค๋ณด๋“œ ์ž…๋ ฅ ํฌ์ปค์Šค๋ฅผ ๊ฐ€์งˆ ๋•Œ def focusOutEvent(self, event): # QFocusEvent ๊ฐ์ฒด, ํ‚ค๋ณด๋“œ ์ž…๋ ฅ ํฌ์ปค์Šค๋ฅผ ์žƒ์„ ๋•Œ def keyPressEvent(self, event): # QKeyEvent, ํ‚ค๋ฅผ ๋ˆ„๋ฅผ ๋•Œ def keyReleaseEvent(self, event): # QKeyEvent, ๋ˆ„๋ฅผ ํ‚ค๋ฅผ ํ•ด์ œํ•  ๋•Œ def inputMethodEvent(self, event): # QInputMethodEvent ๊ฐ์ฒด, ์•„์‹œ์•„ ๋ฌธ์ž์— ๋Œ€ํ•œ ํ‚ค ์ž…๋ ฅ def closeEvent(self, event): # QCloseEvent ๊ฐ์ฒด, ์œ„์ ฏ์„ ๋‹ซ์„ ๋•Œ def dragEnterEvent(self, event): # QDragEnterEvent ๊ฐ์ฒด, ์œ„์ ฏ ๋‚ด๋กœ ๋“œ๋ž˜๊ทธ ๋˜์–ด ๋“ค์–ด์˜ฌ ๋•Œ def dragLeaveEvent(self, event): # QDragLeaveEvent ๊ฐ์ฒด, ์œ„์ ฏ ๋ฐ–์œผ๋กœ ๋“œ๋ž˜๊ทธ ๋˜์–ด ๋‚˜๊ฐˆ ๋•Œ def dragMoveEvent(self, event): # QDropEvent ๊ฐ์ฒด, ๋“œ๋ž˜๊ทธํ•˜๋ฉฐ ์›€์ง์ผ ๋•Œ def dropEvent(self, event): # ๋“œ๋กญ๋  ๋•Œ def hideEvent(self, event): # QHideEvent ๊ฐ์ฒด, ์œ„์ ฏ์ด ํ™”๋ฉด์—์„œ ๊ฐ์ถฐ์งˆ ๋•Œ def showEvent(self, event): # QShowEvent ๊ฐ์ฒด, ์œ„์ ฏ์ด ํ™”๋ฉด์—์„œ ๋‚˜ํƒ€๋‚  ๋•Œ ... ์˜ˆ์™ธ์ ์œผ๋กœ ํƒ€์ด๋จธ ์ด๋ฒคํŠธ(QTimerEvent)๋Š” QObject๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๋ฐœ์ƒํ•œ๋‹ค. class QObject: ... def timerEvent(self, event) #QTimerEvent ๊ฐ์ฒด, ... ์ด๋ฒคํŠธ์˜ ์ฒ˜๋ฆฌ ๋ฐฉ์‹ ์ด๋ฒคํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ธ ๊ฐ€์ง€ ๋ฐฉ์‹ ์ค‘ ํ•˜๋‚˜์— ์˜ํ•ด ๋ฐœ์ƒ๋˜๊ณ  ์ฒ˜๋ฆฌ๋œ๋‹ค. ์œˆ๋„ ์‹œ์Šคํ…œ์—์„œ ๋ฐœ์ƒ๋˜์–ด ์ด๋ฒคํŠธ๋ฃจํ”„๋กœ ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒฝ์šฐ(spontaneous event) Qt๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํฌ์ŠคํŠธ(Posted) ๋˜์–ด ์ด๋ฒคํŠธ๋ฃจํ”„๋กœ ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒฝ์šฐ(posted event) Qt๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ณด๋‚ด์ ธ(Sented) ๋Œ€์ƒ ๊ฐ์ฒด๋กœ ๋ฐ”๋กœ ์ „๋‹ฌ๋˜๋Š” ์ด๋ฒคํŠธ(sented event) (1), (2)์— ํ•ด๋‹นํ•˜๋Š” ์ด๋ฒคํŠธ ๋ฃจํ”„๋Š” ๋ฉ”์ธ ํ•จ์ˆ˜(__main__ ๋ถ€๋ถ„)์˜ ๋์— ํ˜ธ์ถœ๋˜๋Š” QApplication.exec_()์—์„œ ๊ฐ€๋™๋˜๋Š”๋ฐ, ์ด ํ•จ์ˆ˜๋Š” ๊ฐœ๋…์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์ด๋‹ค. while(!exit_was_called) { while(! posted_event_queue_is_empty) process_next_posted_event(); while(! spontaneous_event_queue_is_empty) process_next_spontaneous_event(); while(! posted_event_queue_is_empty) process_next_posted_event(); } ๋จผ์ € posted event์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ ๋ฃจํ”„๊ฐ€ ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ ์‹œ์Šคํ…œ์—์„œ ์ง์ ‘ ๋ฐœ์ƒ๋˜๋Š” ์ด๋ฒคํŠธ์ธ spontaneous event์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ ๋ฃจํ”„๋ฅผ ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ posted event์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ค์‹œ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” spontaneous event์˜ ๊ฒฐ๊ณผ posted event๊ฐ€ ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ๋Š” spontaneous event์™€ posted event๊ฐ€ ํ ํ˜•ํƒœ์˜ ์ด๋ฒคํŠธ ๋ฃจํ”„๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌ๋œ๋‹ค. ์ง์ ‘ ํ•ด๋‹น ๊ฐ์ฒด๋กœ ์ด๋ฒคํŠธ๋ฅผ ๋ณด๋‚ด๋Š” sented event๋Š” ๋Œ€์ƒ ๊ฐ์ฒด๊ฐ€ ๊ทธ ์ด๋ฒคํŠธ๊ฐ€ ์ฒ˜๋ฆฌ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€๋งŒ(blocked), ์ด๋ฒคํŠธ ๋ฃจํ”„๋ฅผ ํ†ตํ•˜๊ฒŒ ๋˜๋ฉด ์ด๋ฒคํŠธ๋ฅผ ์••์ถ•ํ•˜๊ณ  ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•  ๋•Œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด QPaintEvent๋‚˜ QResizeEvent์˜ ๊ฒฝ์šฐ ์ด๋ฒคํŠธ ๋ฃจํ”„๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋˜๋ฉด, ์งง์€ ์‹œ๊ฐ„ ๋‚ด์— ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋ฉด ํ•œ ๋ฒˆ๋งŒ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์••์ถ•์ด ๋˜์–ด ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋ฒคํŠธ๊ฐ€ ํ•„์š”ํ•œ ์ด์œ  Qt์˜ ์‹œ๊ทธ๋„/์Šฌ๋กฏ์˜ ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์œผ๋กœ ์ด๋ฒคํŠธ(event)๋ฅผ ์ง์ ‘ ์ฒ˜๋ฆฌํ•  ์ผ์„ ๋ณ„๋กœ ์—†๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค์Œ์˜ ๊ฒฝ์šฐ ํ•„์š”ํ•˜๋‹ค ์ปค์Šคํ…€ ์œ„์ ฏ์„ ์ œ์ž‘ํ•  ๋•Œ๋Š” ์ด๋ฒคํŠธ์˜ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ํŠนํžˆ ํ™”๋ฉด ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํŽ˜์ธํŠธ ์ด๋ฒคํŠธ(QPaintEvent)๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ๋งŒ ํ•œ๋‹ค. ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ์‹œํ‚ฌ ๋•Œ๋„ ์ด๋ฒคํŠธ์˜ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. 5.2 ํŽ˜์ธํŠธ ์ด๋ฒคํŠธ์™€ ์œ„์ ฏ ๊ทธ๋ฆฌ๊ธฐ ์œ„์ ฏ์„ ๋Œ€์ƒ์œผ๋กœ ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ paintEvent(QPaintEvent) ํ•ธ๋“ค๋Ÿฌ ๋‚ด์—์„œ QPainter๋กœ ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค. QPaintEvent๋Š” ํŽ˜์ธํŠธ ์ด๋ฒคํŠธ๋Š” ์œ„์ ฏ์ด ๋’ค์— ์žˆ๋‹ค๊ฐ€ ์•ž์œผ๋กœ ๋‚˜์˜ค๊ฒŒ ๋˜์–ด ์ƒˆ๋กœ ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ์šด์˜ ์ฒด๊ณ„์— ์˜ํ•ด ๋ฐœ์ƒํ•œ๋‹ค. ๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ์—์„œ QWidget.update(), QWidget.repaint()์— ์˜ํ•ด ๊ฐ•์ œ๋กœ ๋ฐœ์ƒ๋˜๊ธฐ๋„ ํ•œ๋‹ค. RenderMinimalWidget ๋‹ค์Œ์€ ์œ„์ ฏ์— ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ ์ตœ์†Œํ•œ์˜ ์ฝ”๋“œ๋ฅผ ์ œ์‹œํ•œ RenderMinimalWidget ์˜ˆ์ œ์ด๋‹ค. RenderMinimalWidget.py from PySide2.QtWidgets import QWidget from PySide2.QtGui import QPalette, QPainter, QPen, QBrush, QFont from PySide2.QtCore import Qt, QRect class RenderArea(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setAutoFillBackground(True) # ๋ฐฐ๊ฒฝ์„ ์น ํ•˜๋„๋ก ์„ค์ • self.setBackgroundRole(QPalette.Base) # ๋ฐฐ๊ฒฝ์ƒ‰ QPalette.Base๋กœ ์„ค์ • def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) painter.setPen(QPen(Qt.black, 4, Qt.DotLine, Qt.RoundCap)) # ํŽœ ์„ค์ ˆ painter.setBrush(QBrush(Qt.green, Qt.SolidPattern)) # ๋ธŒ๋Ÿฌ์‹œ ์„ค์ • painter.setFont(QFont("Arial",30)) rect = QRect(80,80,400,200) painter.drawRoundRect(rect, 50,50) # ๊ฒฝ๊ณ„์„ ์€ ํŽœ, ๋‚ด๋ถ€๋Š” ๋ธŒ๋Ÿฌ์‹œ painter.drawText(rect, Qt.AlignCenter,"Hello, Qt!") # ํŽœ์˜ ์ƒ‰์ƒ, ํฐํŠธ ์‚ฌ์šฉ from PySide2.QtWidgets import QApplication import sys if __name__ == "__main__": app = QApplication(sys.argv) renderArea = RenderArea() renderArea.setWindowTitle("Render Minimal") renderArea.resize(530,360) renderArea.show() app.exec_() 5.3 ์ž…๋ ฅ ์ด๋ฒคํŠธ์™€ QCloseEvent ์ž…๋ ฅ ์ด๋ฒคํŠธ ์ด๋ฒคํŠธ ์ค‘์—๋Š” ์ž…๋ ฅ ์ด๋ฒคํŠธ(input event)๋Š” ๋ถ€๋ชจ ์œ„์ ฏ์œผ๋กœ ์ „ํŒŒ(propagated) ๋˜๋Š” ์ด๋ฒคํŠธ๋“ค์ด๋‹ค. ํ‚ค ์ด๋ฒคํŠธ, ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ, ํœ  ์ด๋ฒคํŠธ, ์ปจํ…์ŠคํŠธ ๋ฉ”๋‰ด ์ด๋ฒคํŠธ ๋“ฑ์ด ์ด๋Ÿฐ ๋ฒ”์ฃผ์— ๋“ค์–ด๊ฐ„๋‹ค. ์ด๋Ÿฐ ์ด๋ฒคํŠธ๋“ค์€ accept(), ignore() ํ•จ์ˆ˜๋กœ ์ด๋ฒคํŠธ ํด๋ž˜์Šค์˜ bool ํƒ€์ž…์˜ accepted ์†์„ฑ์„ ์„ค์ •ํ•˜๊ณ  ํ•ด์ œํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค. ์ด๋ฒคํŠธ๋ฅผ accept ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋Š” ์ด๋ฏธ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ–ˆ์œผ๋‹ˆ ๋” ์ด์ƒ ๋ถ€๋ชจ ์œ„์ ฏ์œผ๋กœ ์ „ํŒŒํ•˜์ง€ ๋ง๋ผ๋Š” ์˜๋ฏธ์ด๊ณ , ignore ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๋ถ€๋ชจ ์œ„์ ฏ์œผ๋กœ ์ด๋ฒคํŠธ๋ฅผ ์ „ํŒŒํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด A๋ผ๋Š” ์œ„์ ฏ์ด B ์œ„์ ฏ์˜ ์ž์‹ ์œ„์ ฏ์ด๋ผ๋ฉด, A ์œ„์ ฏ์—์„œ ์ด๋ฒคํŠธ๋ฅผ ๋ฌด์‹œํ•˜๋ฉด B ์œ„์ ฏ์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ด๋ฒคํŠธ๊ฐ€ ์ „ํŒŒ๋˜๊ฒŒ ๋œ๋‹ค. ๋‹ค์Œ์€ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ธ ํ‚ค ์ด๋ฒคํŠธ์— ๋Œ€ํ•ด ESC ํ‚ค์— ๋ฐ˜์‘ํ•˜๋„๋ก ๊ตฌํ˜„ํ•œ ์˜ˆ์ด๋‹ค. class MyWidget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setFocusPolicy(Qt.StrongFocus); // enable keyPressEvent() ... def keyPressEvent(event): # QKeyEvent : event if event.key() == Qt.Key_Escape: self.doEscape() # event->accept() ... dummy call else: QWidget.keyPressEvent(self, event) # event->ignore() is called by default ... ์—„๋ฐ€ํ•˜๊ฒŒ QKeyEvent๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ์žˆ๋Š” ์œ„์ ฏ์—์„œ ์ฒ˜๋ฆฌ๋  ๊ฒฝ์šฐ accept()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ์•ผ ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” ignore()๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ถ€๋ชจ ์œ„์ ฏ์ด ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ‚ค ์ด๋ฒคํŠธ๋ฅผ ์ „ํŒŒํ•˜๋„๋ก ํ•œ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ QWidget.keyPressEvent(event)๋Š” ๋‹จ์ˆœํžˆ event.ignore()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, QKeyEvent๋Š” accepted ํ”Œ๋ž˜๊ทธ๊ฐ€ ์ดˆ๊ธฐ๋ถ€ํ„ฐ True์ด๋ฏ€๋กœ accept() ํ˜ธ์ถœ์ด ์—†๋”๋ผ๋„ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๋ฆฌํ„ด๋˜๋ฉด accept()๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋งŒ์•ฝ ์œ„ ์ฝ”๋“œ๋ฅผ accept()์™€ ignore()๋ฅผ ์จ์„œ ์ž‘์„ฑํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. class MyWidget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setFocusPolicy(Qt.StrongFocus); // enable keyPressEvent() ... def keyPressEvent(event): # QKeyEvent : event if event.key() == Qt.Key_Escape: self.doEscape() event.. accept() else: event.ignore() ... QWidget ์™ธ์— ์œ„์ ฏ์—์„œ ์ƒ์†๋ฐ›๋Š” ๊ฒฝ์šฐ์—๋Š” ํ‚ค ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์˜ ๋””ํดํŠธ ๊ฑฐ๋™์ด ์–ด๋–ค์ง€๋ฅผ ํŒŒ์•…ํ•ด์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ํ‚ค ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. QCloseEvent QCloseEvent๋Š” accept()์™€ ignore() ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ด ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š”๋‹ค. ๋‹ค์Œ ๋Œ€ํ‘œ์ ์ธ QCloseEvent์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์ด๋‹ค. class MainWindow(QMainWindow): ... def closeEvent(self, event): # QCloseEvent if self.userReallyWantsToQuit(): ... do something before quit event.accept() else: event.ignore() ... QCloseEvent ํ•ธ๋“ค๋Ÿฌ์—์„œ๋Š” accept()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋‹ซ๊ธฐ ์ด๋ฒคํŠธ๋ฅผ ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ผ, ์ฆ‰ ์ฐฝ์„ ๋‹ซ์œผ๋ผ๋Š” ์˜๋ฏธ์ด๊ณ , ignore()๋Š” ๋‹ซ๊ธฐ ์ด๋ฒคํŠธ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ์•„๋ฌด ์ผ์ด ์—†๋‹ค๋Š” ๋“ฏ ๋‹ซ๊ธฐ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์€ ์›์ƒํƒœ๋กœ ๋˜๋Œ๋ฆฐ๋‹ค. 5.4 ์œ„์ ฏ ์†์„ฑ๊ณผ ์ด๋ฒคํŠธ ์–ด๋–ค ์ด๋ฒคํŠธ๋Š” ์œ„์ ฏ์˜ ์†์„ฑ์— ๋”ฐ๋ผ ๋ฐœ์ƒ ์—ฌ๋ถ€๊ฐ€ ๊ฒฐ์ •๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. mouseMoveEvent(QMouseEvent) ํ•ธ๋“ค๋Ÿฌ๋Š” ๋””ํดํŠธ๋กœ ๋งˆ์šฐ์Šค๊ฐ€ ๋ˆŒ๋ฆฐ ์ƒํƒœ๋กœ ์ด๋™ํ•ด์•ผ ๋ฐœ์ƒํ•œ๋‹ค. ๋งˆ์šฐ์Šค ์ด๋™ ์‹œ ๋งˆ์šฐ์Šค ๋ˆŒ๋ฆผ๊ณผ ์ƒ๊ด€์—†์ด ๋งˆ์šฐ์Šค ์ด๋™ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด QWidget.setMouseTracking(True)๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ์ฃผ์–ด์•ผ ํ•œ๋‹ค. keyPressEvent(QKeyEvent), focusInEvent(QFocusEvent), focusOutEvent(QFocuseEvent)๋ฅผ ์œ„์ ฏ์—์„œ ํ•˜์šฉํ•˜๋ ค๋ฉด ์œ„์ ฏ์˜ focusPolicy ์†์„ฑ์„ ํฌ์ปค์Šค๋ฅผ ๋ฐ›๋„๋ก QWidget.setFocusPolicy(Qt.StrongFocus) ๋“ฑ์„ ํ˜ธ์ถœํ•ด์„œ ์„ค์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ ์šฉ ๊ฐ€๋Šฅํ•œ ํฌ์ปค์Šค ์ •์ฑ…์€ Qt.StrongFocus ์ด์™ธ์— Qt.TabFocus, Qt.ClickFocus๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Qt::TabFocus๋Š” ํƒญํ‚ค๋กœ ํฌ์ปค์Šค๋ฅผ ์ค€๋‹ค. Qt::ClickFocus๋Š” ๋งˆ์šฐ์Šค ํด๋ฆญ์œผ๋กœ ํฌ์ปค์Šค๋ฅผ ์ค€๋‹ค Qt::StrongFocus๋Š” ์ด ๋‘˜์˜ ์กฐํ•ฉ์œผ๋กœ ํƒญ ํ‚ค๋‚˜ ๋งˆ์šฐ์Šค ํด๋ฆญ์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๊ทธ ์œ„์ ฏ์œผ๋กœ ํฌ์ปค์Šค๋ฅผ ์ค€๋‹ค. contextMenuEvent(QContextMenuEvent)๋Š” QWdiget.setContextMenuPolicy(Qt.DefaultCo- ntextMenu)๋ฅผ ์„ค์ •ํ•ด์•ผ ๋ฐœ์ƒํ•œ๋‹ค. inputMethodEvent(QInputMethodEvent)๋Š” setAttribute(Qt.WA_InputMethodEnabled)๋ฅผ ์„ค์ •ํ•ด์•ผ ๋ฐœ์ƒํ•œ๋‹ค. dropEvent(QDropEvent)๋Š” setAcceptDrops(True)๋ฅผ ์„ค์ •ํ•ด์•ผ ๋ฐœ์ƒํ•œ๋‹ค. ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋Š” ์œ„์ ฏ์— ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์šฉ๋„๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. ๋‹ค์Œ์€ ์–ด๋–ค ์œ„์ ฏ์˜ ๋งˆ์šฐ์Šค ์ด๋™ํ•  ๋•Œ positionChanged() ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ์‹œํ‚จ ์˜ˆ์ด๋‹ค. class MyWidget(QWidget): mousePositionChanged = Signal(str) # mousePositonChanged(str) def __init__(self, parent=None): QWidget.__init__(self, parent) self.setMouseTracking(True) ... def mouseMoveEvent(self, event): # QMouseEvent event pos = "({},{})".format(event.x(),event.y()) self.mousePositionChanged.emit(pos) ... 5.5 ํƒ€์ด๋จธ ์ด๋ฒคํŠธ QTimerEvent ํƒ€์ด๋จธ ์ด๋ฒคํŠธ(QTimerEvent)๋Š” ์œ„์ ฏ์ด ์•„๋‹Œ QObject๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— GUI ์š”์†Œ๊ฐ€ ์•„๋‹ˆ๋”๋ผ๋„ ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. QObject์˜ startTimer(millisecond)๋กœ ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๋กœ ํƒ€์ด๋จธ ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ํƒ€์ด๋จธ์˜ ์ฒ˜๋ฆฌ๋Š” timerEvent() ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ด์šฉํ•œ๋‹ค. ์‚ฌ์šฉ์ด ๋๋‚˜๋ฉด killTimer(id)๋กœ ํƒ€์ด๋จธ๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค. startTimer(millisecond)๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ• ๋‹น๋œ ํƒ€์ด๋จธ์˜ ๊ณ ์œ ๋ฒˆํ˜ธ(timerId)๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ํƒ€์ด๋จธ ์ž์ฒด๋Š” ์šด์˜ ์ฒด๊ณ„์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ์ž์›์ด๋ฉฐ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํƒ€์ด๋จธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ• ๋‹น๋œ ํƒ€์ด๋จธ์—๋Š” ๊ณ ์œ ๋ฒˆํ˜ธ๊ฐ€ ์žˆ๊ฒŒ ๋œ๋‹ค. ์ด ๋ฒˆํ˜ธ๋ฅผ ์ด์šฉํ•ด killTimer(id)๋กœ ํƒ€์ด๋จธ๋ฅผ ๋ฉˆ์ถ”๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ timerEvent() ํ•ธ๋“ค๋Ÿฌ์—์„œ event.timerId()๋กœ ํƒ€์ด๋จธ ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ ํƒ€์ด๋จธ๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ 1๊ฐœ์˜ ํƒ€์ด๋จธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์ฝ”๋“œ์ด๋‹ค. class MyObject(QObject): ... def OnStartTimer(self): self.timerId = self.startTimer(10) # 10 milliseconds def OnKillTimer(self): self.killTimer(timerId); def timerEvent(self, event): # QTimerEvent if event.timerId() != self.timerId: super().timerEvnet(event); .... something to process the timer event QTimer Qt๋Š” ํƒ€์ด๋จธ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋Œ€์‹  ํŽธ๋ฆฌํ•œ ์‹œ๊ทธ๋„/์Šฌ๋กฏ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก QTimer๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. QTimer๋Š” QObject๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋กœ start(millisecond), stop() ์Šฌ๋กฏ ํ•จ์ˆ˜์™€ timeout() ์‹œ๊ทธ๋„์„ ์ œ๊ณตํ•œ๋‹ค. start(millisecond)๋กœ QTimer ํด๋ž˜์Šค ๋‚ด์˜ ํƒ€์ด๋จธ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. QTimer ํด๋ž˜์Šค๋Š” ์ฃผ์–ด์ง„ ์‹œ๊ฐ„์ด ๋˜๋ฉด timeout() ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ ์‹œ๊ฒŒ ๋˜๋ฏ€๋กœ timeout() ์‹œ๊ทธ๋„์„ ์—ฐ๊ฒฐํ•˜์—ฌ ๋‹ค๋ฅธ ์ž‘์—…ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. stop()์€ ํƒ€์ด๋จธ๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค. QTimer๋Š” 1๊ฐœ์˜ ํƒ€์ด๋จธ๋ฅผ ๋‚ด์žฅํ•˜๊ณ  ๊ทธ ๊ณ ์œ ๋ฒˆํ˜ธ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— QTimer๋ฅผ ํ™œ์šฉํ•  ๋•Œ๋Š” ๊ณ ์œ ๋ฒˆํ˜ธ ๋Œ€์‹  QTimer ๊ฐ์ฒด ์ž์ฒด๋ฅผ ๊ณ ์œ ๋ฒˆํ˜ธ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๋‹ค์Œ์€ AnalogClock ์˜ˆ์ œ์—์„œ QTimer ๊ด€๋ จ ์‚ฌํ•ญ์„ ๋ฐœ์ทŒํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. class AnalogClock(QWidget): updated = Signal(QTime) #updated(QTime currentTime); def __init__(self, parent=None): QWidget.__init__(self, parent) ... timer = QTimer(self) timer.timeout.connect(self.update) timer.start(1000) def paintEvent(self, event): ...๊ทธ๋ฆฌ๊ธฐ ์ฝ”๋“œ self.updated.emit(time) AnalogClock ์œ„์ ฏ์€ ํ™”๋ฉด์— 1์ดˆ์— ํ•œ๋ฒˆ ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ƒ์„ฑ์ž์—์„œ ๋‹จ์ˆœํžˆ ํƒ€์ด๋จธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  timeout() ์‹œ๊ทธ๋„์„ update() ์Šฌ๋กฏ์œผ๋กœ ์—ฐ๊ฒฐํ•˜์˜€๋‹ค. 1์ดˆ ๊ฐ„๊ฒฉ์œผ๋กœ ํƒ€์ด๋จธ๋ฅผ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ 1์ดˆ ๋‹จ์œ„๋กœ timeout() ์‹œ๊ทธ๋„์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋ฉด ์ž๋™์ ์œผ๋กœ update()๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. update() ํ˜ธ์ถœ๋กœ paintEvent() ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ์ž‘๋™ํ•œ๋‹ค. ๋˜ํ•œ AnalogClock:update(QTime)์ด๋ผ๋Š” ์‹œ๊ทธ๋„์„ 1์ดˆ ๋‹จ์œ„๋กœ ๋ฐœ์ƒํ•˜๋„๋ก paintEvent()ํ•ธ๋“ค์–ด ๋งˆ์ง€๋ง‰์— ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. ๋งŒ์•ฝ QTimer๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ์œ„์ ฏ(์œ„์ ฏ๋„ QObject ์ž์‹ ํด๋ž˜์Šค์ด๋ฏ€๋กœ)์— ๋‚ด์žฅ๋œ ํƒ€์ด๋จธ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋น„๊ตํ•˜๋ฉด QTimer์˜ ์žฅ์ ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. class AnalogClock(QWidget): updated = Signal(QTime) #updated(QTime currentTime); def __init__(self, parent=None): QWidget.__init__(self, parent) ... self.timeId = self.startTime(1000) def timeEvent(self, event): # QTimerEvent if event.timerId() != self.timeId : super().timerEvent(event) self.update() def paintEvent(self, event): ...๊ทธ๋ฆฌ๊ธฐ ์ฝ”๋“œ self.updated.emit(time) def closeEvent(event): # QCloseEvent self.killTimer(self.timeId) if self.userReallyWantsToQuit() : event.accept() else: event.ignore() QTimer๋Š” ๋‹จ ํ•œ ๋ฒˆ๋งŒ ๋ฐœ์ƒํ•˜๋Š” ํƒ€์ด๋จธ ์ด๋ฒคํŠธ๋ฅผ ์‹œ๊ทธ๋„/์Šฌ๋กฏ ๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ์ •์  ํ•จ์ˆ˜(static function)์ธ `QTimer.singleShot(miliseconds, slotFunction)์„ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค QTimer.singleShot(200, self.updateCaption) ์œ„ ์ฝ”๋“œ๋Š” 200 ๋ฐ€๋ฆฌ์ดˆ ํ›„ updateCaption() ์Šฌ๋กฏ์„ ์‹คํ–‰ํ•˜๋„๋ก ํ•œ๋‹ค. 5.6 ๋ ˆ์ด์•„์›ƒ ์œ„์ ฏ์˜ ํ˜•์ƒ QWidget์—๋Š” ์ž์‹ ์˜ ํ˜„์žฌ ํ˜•์ƒ๊ณผ ๊ด€๋ จํ•ด์„œ ๋ถ€๋ชจ ์œ„์ ฏ์— ๋Œ€ํ•ด ์ƒ๋Œ€์ ์ธ ์ขŒํ‘œ๊ฐ’์„ ๋‹ด๊ณ  ์žˆ๋Š” QRect ํƒ€์ž…์˜ geometry, frameGeometry ๋ฉค๋ฒ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์œ„์ ฏ์˜ ์œ„์น˜๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ํฌ๊ธฐ๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์ด ๋‘ ๋ณ€์ˆ˜์˜ ๊ฐ’์€ ํ˜„์žฌ ๊ฐ’์œผ๋กœ ์œ ์ง€๋œ๋‹ค. ์œ„์ ฏ์˜ ํด๋ผ์ด์–ธํŠธ ์˜์—ญ์˜ ํฌ๊ธฐ๋Š” width(), height(), size(), rect() ๋“ฑ ์–ด๋Š ๊ฒƒ์„ ์จ๋„ ์ƒ๊ด€์—†๋‹ค. ์ด๋•Œ ๊ธฐ๋ณธ์ ์ธ ์ขŒํ‘œ๊ณ„๋Š” ์œ„์ ฏ์ด ์ขŒ์ธก ์ƒ๋‹จ์„ (0,0)์œผ๋กœ ํ•˜๊ณ  +x ๋ฐฉํ–ฅ์€ ์˜ค๋ฅธ์ชฝ, +y๋Š” ์•„๋ž˜ ๋ฐฉํ–ฅ์ด๋‹ค. ํ”ฝ์…€ ๋‹จ์œ„๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉด ์ด ์ขŒํ‘œ๊ณ„๊ฐ€ ๋ทฐํฌํŠธ ์ขŒํ‘œ๊ณ„์ด๋‹ค. ์ด๋•Œ ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ์˜ ๋์ ์€ (width()-1, height()-1)์ธ ์ ์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ๋ ˆ์ด์•„์›ƒ ์œ„์ ฏ์˜ ๋ ˆ์ด์•„์›ƒ์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋‹ค์Œ์˜ ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ์ง์ ‘ gemoerty ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ• : QWidget.setGeomerty(x, y, w, h), QWidget.resize(w, h) ๋“ฑ์˜ ํ•จ์ˆ˜๋กœ geometry ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์ง์ ‘ ์œ„์ ฏ์˜ ์ดˆ๊ธฐ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ตœ์ƒ์œ„ ์œ„์ ฏ์œผ๋กœ ์‚ฌ์šฉ๋  ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ž์‹ ์œ„์ ฏ์—์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ถ€๋ชจ ์œ„์ ฏ ๋‚ด์˜ ์ƒ๋Œ€ ์œ„์น˜๋ฅผ ์ง์ ‘ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ์œˆ๋„ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๊ธ€๊ผด์— ๋”ฐ๋ผ ์ผ๋ถ€ ํ…์ŠคํŠธ๊ฐ€ ์ž˜๋ ค๋‚˜๊ฐ€๋Š” ๋“ฑ์˜ ๋ฌธ์ œ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. resizeEvent() ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ• : ์œ„์ ฏ์˜ ํฌ๊ธฐ๊ฐ€ ๋ฐ”๋€” ๊ฒฝ์šฐ QResizeEvent๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ด๋•Œ resizeEvent(QResizeEvent) ํ•ธ๋“ค๋Ÿฌ์—์„œ ์ฒ˜๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ ˆ์ด์•„์›ƒ์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์ฝ”๋”ฉ์— ์˜ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ์ƒ๋‹นํ•œ ํ•˜๋“œ์ฝ”๋”ฉ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•˜์ง€ ์•Š์„ ๋•Œ ์ œํ•œ์ ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• : Qt์˜ ๋…ํŠนํ•˜๋ฉด์„œ๋„ ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์ธ ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๋Š” QWidget์˜ sizeHint(), mimimumSizeHint(), sizePolicy๋ผ๋Š” ๋ฉค๋ฒ„๋ฅผ ์ด์šฉํ•ด ๋ ˆ์ด์•„์›ƒ์„ ๊ฒฐ์ •ํ•˜๊ฒŒ ๋œ๋‹ค. ๋ณด๋‹ค ์ž์„ธํ•œ ์ž์„ธํ•œ ์‚ฌํ•ญ์€ ๋’ค์— ๋‹ค์‹œ ์„ค๋ช…ํ•œ๋‹ค. ๋ ˆ์ด์•„์›ƒ์ด ์ž‘๋™๋˜๋Š” ๋ฐฉ์‹์„ ์ดํ•ดํ•  ๋•Œ ์ตœ์ƒ์œ„ ์œ„์ ฏ๊ณผ ์ž์‹ ์œ„์ ฏ ์ค‘ ์–ด๋–ค ์œ„์ ฏ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์— ๋”ฐ๋ผ QResizeEvent๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์กฐ๊ฑด์„ ์ •ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค. โ–ช ์ตœ์ƒ์œ„ ์œ„์ ฏ์˜ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž๊ฐ€ ์œ„์ ฏ์˜ ํฌ๊ธฐ๋ฅผ ์ง์ ‘ ๋ฐ”๊พธ๊ฑฐ๋‚˜, resize(w, h)๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋ฐœ์ƒํ•œ๋‹ค. โ–ช ์ž์‹ ์œ„์ ฏ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๋กœ ๊ด€๋ฆฌ๋  ๋•Œ๋Š” ๋ ˆ์ด์•„์›ƒ์ด ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค, ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๋กœ ๊ด€๋ฆฌ๋˜์ง€ ์•Š์„ ๋•Œ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ง์ ‘ resize(w, h)๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ๋ฐœ์ƒํ•œ๋‹ค. ์ž์‹ ์œ„์ ฏ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ๊ด€๋ฆฌ๋˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์—๋„ ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๊ฐ€ ์ž์‹ ์˜ ์ž์‹ ์œ„์ ฏ์— ๋Œ€ํ•ด resize(w, h)๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๊ณ  ๊ฒฐ๊ตญ QResizeEvent๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๋ ˆ์ด์•„์›ƒ ๋งค๋‹ˆ์ €(๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด)๋กœ ๊ด€๋ฆฌ๋˜๋Š” ์ž์‹ ์œ„์ ฏ์— ๋Œ€ํ•ด ํ”„๋กœ๊ทธ๋žจ ๊ฐ€ ์ง์ ‘ resize(w, h)๋‚˜ setGeometry() ๋“ฑ์„ ํ˜ธ์ถœํ•˜๋ฉด ๋ฌด์‹œ๋œ๋‹ค. ํ•œํŽธ, resizeEvent() ํ•ธ๋“ค๋Ÿฌ ๋‚ด์—์„œ resize()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋ฉด ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์ง€๊ฒŒ ๋˜๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ QWidget::resize() ํ•ธ๋“ค๋Ÿฌ๋Š” mimimumSize, maximumSize๋ผ๋Š” ๋ฉค๋ฒ„๋ฅผ ์ด์šฉํ•ด ์ตœ์†Œ ๋ฐ ์ตœ์†Œ ํฌ๊ธฐ์— ๋Œ€ํ•œ ์ œํ•œ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๊ทธ ์ดํ•˜ ๋˜๋Š” ๊ทธ ์ด์ƒ ํฌ๊ธฐ๊ฐ€ ์กฐ์ ˆ๋˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค. ๋‘ ๋ฉค๋ฒ„๋Š” setMinimumSize(), setMaximumSize() ํ•จ์ˆ˜๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ QWidget์˜ ๋””ํดํŠธ ๊ฐ’์€ (0,0)๊ณผ (1677721,1677721)๋กœ ์„ค์ •๋˜์–ด ์žˆ๋‹ค. ๋ ˆ์ด์•„์›ƒ ๋งค๋‹ˆ์ € ์ปค์Šคํ…€ ์œ„์ ฏ์„ ์ œ์ž‘ํ•  ๋•Œ๋Š” ์ž์‹ ์œ„์ ฏ์œผ๋กœ ์‚ฌ์šฉ๋  ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด์„œ ๋ ˆ์ด์•„์›ƒ ๋งค๋‹ˆ์ €(๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด)๋กœ ์ ์ ˆํžˆ ๋ ˆ์ด์•„์›ƒ์ด ๋˜๋„๋ก ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Qt๊ฐ€ ์ œ๊ณตํ•˜๋Š” QPushButton, QLabel, QLineEdit ๋“ฑ์˜ ๊ธฐ๋ณธ ์œ„์ ฏ๋“ค๋„ ์ž์‹ ์œ„์ ฏ์œผ๋กœ ์‚ฌ์šฉ๋  ๋•Œ ์–ด๋–ป๊ฒŒ ๋ ˆ์ด ์•„์›ƒ๋˜๋Š”์ง€๋ฅผ ์ดํ•ดํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋ ˆ์ด์•„์›ƒ ๋งค๋‹ˆ์ €(๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด)๋กœ ๊ด€๋ฆฌ๋  ๋•Œ ์ž์‹ ์œ„์ ฏ๋“ค์ด ๋ ˆ์ด์•„์›ƒ์„ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ QWidget์˜ ๋ฉค๋ฒ„๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. minimumSize, maximumSize : ์œ„์ ฏ์˜ ํฌ๊ธฐ ๋ณ€ํ™”๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ตœ์†Œ ๋ฐ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. QWidget์˜ ๋””ํดํŠธ ๊ฐ’์€ ๊ฐ๊ฐ (0,0)๊ณผ (16777215,16777215)์ด๋‹ค. setMinimumSize(), setMaximumSize()๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๊ฐ’์€ ๋™์ผํ•˜๊ฒŒ ํ•œ๋‹ค๋ฉด ๋” ์ด์ƒ ํฌ๊ธฐ ๋ณ€ํ™”๊ฐ€ ๋˜๋Š”๋‹ค. ํŽธ์˜ ํ•จ์ˆ˜๋กœ setFixedSize() ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ๊ฐ’์œผ๋กœ ๋™์ผํ•œ minimumSize์™€ maximumSize๋กœ ์„ค์ •ํ•ด ์ค€๋‹ค. sizeHint()์™€ mimimumSizeHint(), sizePolicy : ์ด ๊ฐ’๋“ค์€ ๋ ˆ์ด์•„์›ƒ ๋งค๋‹ˆ์ €๋กœ ๊ด€๋ฆฌ๋  ๋•Œ ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š” ๊ฐ’์ด๋‹ค. sizeHint()์—์„œ ๋ฆฌํ„ด๋˜๋Š” ํฌ๊ธฐ๋Š” ์œ„์ ฏ์ด ๊ฐ€์ง€๋Š” ์ด์ƒ์ ์ธ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. minimumSizeHint()๋Š” ์ด์ƒ์ ์ธ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ฐ ๋ ˆ์ด์•„์›ƒ ๋งค๋‹ˆ์ €๋กœ ๊ด€๋ฆฌ๋  ๋•Œ ๊ทธ ์ดํ•˜๋กœ ํฌ๊ธฐ๊ฐ€ ์ค„์ง€ ์•Š๋„๋ก ํ•œ๋‹ค. ๋งŒ์•ฝ minimumSize๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์œผ๋ฉด(minimumSize๊ฐ€ (0,0)์ด ์•„๋‹ˆ๋ฉด) ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. sizeHint()์™€ minimumSizeHint()๋Š” ๊ฐ€์ƒํ•จ ์ˆ˜๋กœ ํ•ญ์ƒ ํ˜ธ์ถœ๋˜๊ธฐ๋งŒ ํ•˜๋Š” ๊ฐ’์ด๋ฏ€๋กœ ์™ธ๋ถ€์—์„œ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ์ปค์Šคํ…€ ์œ„์ ฏ์„ ๋งŒ๋“ค๊ณ  ๋‹ค์Œ์˜ ๊ฐ€์ƒ ํ•จ์ˆ˜๋ฅผ ์žฌ์ •์˜ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. class CustomWidget(QWidget): ... def sizeHint(self): ... # QSize ๋ฆฌํ„ด def mimimumSizeHint(self): ... # QSize ๋ฆฌํ„ด sizeHint()๋Š” ํฌ๊ธฐ ์ •์ฑ…(sizePolicy)์—์„œ ์–ด๋–ค ์‹์œผ๋กœ ํ™œ์šฉํ•  ๊ฑด์ง€ ๊ฒฐ์ •ํ•œ๋‹ค. ํฌ๊ธฐ ์ •์ฑ…(QSizePolicy ํด๋ž˜์Šค)์œผ๋กœ๋Š” Fixed, Minimum, Maximum, Preference, Expanding์ด ์žˆ๊ณ , QWidget.setSizePolicy()๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. QWidget.setSizePolicy(horizontalSizePolicy, verticalSizePolicy) ์œ„์—์„œ horizontalSizePolicy, verticalSizePolicty๋Š” QSizePolicy.Policy๋ผ๋Š” ์—ด๊ฑฐ ์ƒ์ˆ˜๋กœ ๋‹ค์Œ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. QSizePolicy.Fixed : ์œ„์ ฏ์˜ ํฌ๊ธฐ๋Š” sizeHint()๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ํฌ๊ธฐ๋กœ ๊ณ ์ •๋จ QSizePolicy.Minimum : sizeHint()๊ฐ€ ์ตœ์†Œ์ด๋ฉฐ์„œ ์ตœ์ ์˜ ํฌ๊ธฐ. ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์Œ. QSizePolicy.Maximum : ์œ„์ ฏ์˜ sizeHint()๊ฐ€ ์ตœ๋Œ€์ด๋ฉด ์ตœ์ ์˜ ํฌ๊ธฐ. ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ค„์–ด๋“ค ์ˆ˜ ์žˆ์Œ. ์ตœ์†Œ ํฌ๊ธฐ๋Š” minimumSizeHint(). QSizePolicy.Preferred : ์œ„์ ฏ์˜ sizeHint()๊ฐ€ ์ตœ์ ์˜ ํฌ๊ธฐ. ์ค„๊ฑฐ๋‚˜ ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์Œ. ์ตœ์†Œ ํฌ๊ธฐ๋Š” minimumSizeHint(). QSizePolicy.Expanding : ์œ„์ ฏ์˜ sizeHint()๊ฐ€ ์ตœ์ ์˜ ํฌ๊ธฐ. ์ค„๊ฑฐ๋‚˜ ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์Œ. ๊ณต๊ฐ„์ด ๋” ์žˆ๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ์‚ฌ์šฉํ•จ. ์ตœ์†Œ ํฌ๊ธฐ๋Š” minimumSizeHint(). QSizePolicy.MinimumExpanding : sizeHint()๊ฐ€ ์ตœ์†Œ์ด๋ฉฐ์„œ ์ตœ์ ์˜ ํฌ๊ธฐ. ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์Œ. ๊ณต๊ฐ„์ด ๋” ์žˆ๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ์‚ฌ์šฉํ•จ. ์ตœ์†Œ ํฌ๊ธฐ๋Š” minimumSizeHint(). QSizePolicy.Ignored : sizeHint() ๋ฌด์‹œ. ์œ„์ ฏ์€ ๊ฐ€๋Šฅํ•œ ๊ณต๊ฐ„์„ ๊ฐ€์ง‘. QLineEdit, QComboBox ๋“ฑ๊ณผ ๊ฐ™์ด Qt๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ์œ„์ ฏ๋“ค์€ ๋Œ€๋ถ€๋ถ„ ์ž์‹ ๋งŒ์˜ sizePolicy, sizeHint()์™€ mimimumSizeHint()๋ฅผ ๊ฐ€์ง„๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด QLineEdit๋Š” ์ˆ˜ํ‰ ๋ฐ ์ˆ˜์ง ๋ฐฉํ–ฅ์˜ sizePolicy๊ฐ€ ๊ฐ๊ฐ QSizePolicy.Minimum, QSizePolicy.Fixed์ด๊ณ , ์ˆ˜ํ‰ ๋ฐฉํ–ฅ์œผ๋กœ๋Š” 15-20๊ฐœ์˜ ๋ฌธ์ž์™€ ์ˆ˜์ง ๋ฐฉํ–ฅ์œผ๋กœ 1๊ฐœ ๋ฌธ์ž ๋†’์ด ์ •๋„๋กœ sizeHint()๋ฅผ ์žฌ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ QLineEdit๋ฅผ ๋ ˆ์ด์•„์›ƒ ๋งค๋‹ˆ์ €๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ ์ˆ˜ํ‰๋ฐฉํ–ฅ์œผ๋กœ๋Š” 15-20 ๋ฌธ์ž๋ฅผ ๊ฐ–๋Š” ํฌ๊ธฐ๋กœ ๋ฐฐ์น˜๋˜๋ฉด์„œ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ˆ˜์ง ๋ฐฉํ–ฅ์œผ๋กœ๋Š” ๊ณ ์ •๋œ ํฌ๊ธฐ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค. QLineEdit๋Š” minimumSizeHint()๋ฅผ 1๊ฐœ ๋ฌธ์ž๊ฐ€ ๋“ค์–ด๊ฐˆ ์ •๋„๋กœ ์žฌ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค. QSizePolicy.Minimum ์ผ ๋•Œ ํ•„์š” ์—†๋Š” minimumSizeHint()๋ฅผ ์žฌ์ •์˜ํ•œ ๊ฒƒ์€ QSizePolicy.Preferred, QSizePolicy.Expanding, QSizePolicy.MinimumExpanding ๋“ฑ๊ณผ ๊ฐ™์ด ์ค„์–ด๋“ค ์—ฌ์ง€๊ฐ€ ์žˆ๋Š” sizePolicy๋กœ ๋ฐ”๊ฟ€ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด์„œ์ด๋‹ค. ์ปค์Šคํ…€ ์œ„์ ฏ์„ ์ œ์ž‘ํ•  ๋•Œ ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋„๋ก ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ์ƒ์„ฑ์ž์—์„œ QWidget.setSizePolocy()์„ ํ˜ธ์ถœํ•˜์—ฌ ์œ„์ ฏ์˜ ๋””ํดํŠธ ํฌ๊ธฐ ์ •์ฑ…์„ ์„ค์ •ํ•œ๋‹ค. QWidget.sizeHint()๋ฅผ ์œ„์ ฏ์˜ ์ ์ ˆํ•œ ํฌ๊ธฐ๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ์žฌ์ •์˜ํ•œ๋‹ค. ์ค„์–ด๋“ค ์—ฌ์ง€๊ฐ€ ์žˆ๋Š” sizePolicy๋ฅผ ๋Œ€๋น„ํ•˜์—ฌ QWidget.mimumSizeHint()๋ฅผ ์œ„์ ฏ์˜ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ์žฌ์ •์˜ํ•œ๋‹ค. ์ปค์Šคํ…€ ์œ„์ ฏ์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ์ž‘์„ฑ ์‹œ sizeHint()๊ฐ€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋Š” ์ž‘์—…ํ•˜๊ฑฐ๋‚˜, sizePolicy๋ฅผ ๋ณ€๊ฒฝํ•œ ๊ฒฝ์šฐ QWidget.updateGeometry()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๋†’์ด์™€ ํญ๋ฅผ ์ œ์–ดํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” setHeightForWidth(True)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ height-for-width ํ”Œ๋ž˜๊ทธ๋ฅผ ํ™œ์„ฑํ™”ํ•˜๊ณ (๋ณดํ†ต ์ƒ์„ฑ์ž์—์„œ), QWidget.heightForWidth()๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค. (1), (2)๊ฐ€ ํ•„์ˆ˜์‚ฌํ•ญ์ด๊ณ , (3)์€ ์ค„์–ด๋“ค ์ˆ˜ ์žˆ๋Š” sizePolicy๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•˜์—ฌ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ๋งŒ์•ฝ QSizePolicy.Fixed, QSizePolicy.Minimum, QSizePolicy.Ignored๋งŒ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค๋ฉด minimumSizeHint()๋ฅผ ์žฌ์ •์˜ํ•  ํ•„์š” ์—†๋‹ค. (4)๋Š” ์ปค์Šคํ…€ ์œ„์ ฏ ๋‚ด๋ถ€์˜ ์–ด๋–ค ํ–‰์œ„๋กœ ์ธํ•ด(๋ฉค๋ฒ„ ํ•จ์ˆ˜ ๊ตฌํ˜„ ์‹œ) ๋ ˆ์ด์•„์›ƒ์ด ๋ณ€๊ฒฝ๋  ๋•Œ ํ˜ธ์ถœํ•ด ์ค๋‹ˆ๋‹ค. (5)๋Š” ๊ฑฐ์˜ ์‚ฌ์šฉ๋  ์ผ์ด ์—†๋‹ค. 5.7 ์„œ๋ธŒํด๋ž˜์‹ฑ์‹œ ์ฃผ์˜์‚ฌํ•ญ ์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•œ ์‚ฌํ•ญ์„ ์š”์•ฝํ•˜์—ฌ QWidget์—์„œ ์ง์ ‘ ์„œ๋ธŒํด๋ž˜์‹ฑ์œผ๋กœ ์ปค์Šคํ…€ ์œ„์ ฏ์„ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ํ•„์š”ํ•œ ๊ฐœ๋žต์ ์ธ ํ˜•ํƒœ์˜ ํด๋ž˜์Šค ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. class MyWidget(QWidget): def __init__(self,....,parent=None): QWidget.__init__(self, parent) # ํ•„์š”์‹œ ๊ด€๋ จ ์†์„ฑ ์„ค์ • self.setAttribute(Qt.QA_DeleteOnClose, True); self.setAttribute(Qt.QA_StaticContents, True); self.setAttribute(Qt.QA_Hover, True); self.setMouseTracking(True); self.setFocusPolicy(Qt.StrongFocus) self.setContextMenuPolicy(Qt.DefaultContextMenu) self.setAcceptDrops(True); self.setAttribute(Qt.WA_InputMethodEnabled, True); self.setAttribute(Qt.WA_KeyCompression, True); # ํ•„์š”์‹œ ๋ฐฐ๊ฒฝ์ƒ‰ ์„ค์ • self.setBackgrundRole(QPalette.Dark); self.setAutoFillBackground(True); # ๋””ํดํŠธ sizePolicy ์„ค์ • self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum); ... # ๋ ˆ์ด์•„์›ƒ ๊ด€๋ จ ํ•จ์ˆ˜ ์žฌ์ •์˜ def sizeHint(self): ... def mimimumSizeHint(self): ... # ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์žฌ์ •์˜ def keypressEvent(event): ... ... def paintEvent(event): # ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ธฐ๊ฐ€ ํ•„์š”ํ•  ๋•Œ ... 5.8 AnalogClock AnalogClock ์˜ˆ์ œ๋Š” ์•ž์„œ์—์„œ ๋ฐฐ์šด ์œ„์ ฏ์˜ ํŠน์„ฑ์„ ์ด์šฉํ•œ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ธฐ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์„ค๋ช…์€ 6.4 AnalogClock ์˜ˆ์ œ ๋ถ„์„์„ ์ฐธ์กฐํ•˜๋„๋ก ํ•œ๋‹ค. AnalogClock.py from PySide2.QtWidgets import QWidget, QSizePolicy from PySide2.QtCore import Signal, Qt, QTimer, QTime, QPoint, QRectF, QSize from PySide2.QtGui import QPainter, QColor, QRadialGradient, QPen class AnalogClock(QWidget): updated = Signal(QTime) #updated(QTime currentTime); def __init__(self, parent=None): QWidget.__init__(self, parent) self.backgroundColor = Qt.gray self.timeZoneOffset = 0 self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) timer = QTimer(self) timer.timeout.connect(self.update) timer.start(1000) def sizeHint(self): return QSize(200,200) def setBgColor(self, newColor): self.backgroundColor = newColor def setTimeZone(self, hourOffset): self.timeZoneOffset = min(max(-12, hourOffset),12)*3600 self.update() def paintEvent(self, event): hourHand = [QPoint(7,8), QPoint(-7,8), QPoint(0, -40)] minuteHand = [QPoint(7,8), QPoint(-7,8), QPoint(0, -70)] secondHand = [QPoint(0,8),QPoint(0, -80)] hourColor = QColor(127,0,127) minuteColor = QColor(0,127,127,191) secondColor = QColor(255,0,0,191) side = min(self.width(),self.height()) time = QTime.currentTime() # static function time = time.addSecs(self.timeZoneOffset) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) painter.translate(self.width()/2, self.height()/2) painter.scale(side/200.0, side/200.0) # Draw circle radialGradient = QRadialGradient(0,0,100, -40, -40) # center, radius, focalPoint radialGradient.setColorAt(0.0, Qt.white) radialGradient.setColorAt(1.,self.backgroundColor) painter.setBrush(radialGradient) painter.setPen(QPen(Qt.darkGray, 0)) # darkGray cosmetic pen painter.drawEllipse(QRectF(-97, -97,194,194)) # Draw minute tick painter.setPen(minuteColor) for j in range(60): if (j % 5) != 0: painter.drawLine(92,0,96,0) painter.rotate(6.0) # draw hour hand painter.setPen(Qt.NoPen) painter.setBrush(hourColor) painter.save() painter.rotate(30.0*((time.hour()+time.minute()/60.0))) painter.drawConvexPolygon(hourHand) painter.restore() # draw hour tick painter.setPen(hourColor) for i in range(12): painter.drawLine(88,0,96,0) painter.rotate(30.0) # draw mimute hand painter.setPen(Qt.NoPen) painter.setBrush(minuteColor) painter.save() painter.rotate(6.8*(time.minute()+time.second()/60.0)) painter.drawConvexPolygon(minuteHand) painter.restore() # Draw second hand painter.setPen(secondColor) painter.save() painter.rotate(6.0*time.second()) painter.drawLine(secondHand[0],secondHand[1]) painter.restore() self.updated.emit(time) from PySide2.QtWidgets import QApplication import sys if __name__ == "__main__": app = QApplication(sys.argv) clock = AnalogClock() clock.setWindowTitle("Render Minimal") clock.resize(530,360) clock.show() app.exec_() 6. ๊ทธ๋ž˜ํ”ฝ์Šค Qt์˜ ๊ทธ๋ฆฌ๊ธฐ๋Š” 2์ฐจ์›๊ณผ 3์ฐจ์›์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š”๋ฐ, ๊ฐ๊ฐ QPainter์™€ Open GL API๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์€ QPainter API๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์„ค๋ช…ํ•œ๋‹ค. Qt ํŽ˜์ธํŒ… ์‹œ์Šคํ…œ Qt์˜ ํŽ˜์ธํŒ… ์‹œ์Šคํ…œ์€ QPainter, QPaintEngine, QPaintDevice๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์— ๋‚˜ํƒ€๋‚ธ ๊ฒƒ๊ณผ ๊ฐ™์ด QPainter API๋ฅผ ํ†ตํ•ด ํ™”๋ฉด(์ฆ‰, ์œ„์ ฏ)์ด๋‚˜ ์ด๋ฏธ์ง€ ํŒŒ์ผ, ์ถœ๋ ฅ์žฅ์น˜์— ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. QPainter๋Š” ๊ทธ๋ฆฌ๊ธฐ ์ž‘์—…์„ ๋‹ด๋‹นํ•œ๋‹ค. ์—ฌ๋Ÿฌ API๋ฅผ ํ†ตํ•ด ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. QPaintEngine์€ QPainter์™€ QPaintDevice์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค๋กœ ํ”„๋กœ๊ทธ๋žจ ๊ฐœ๋ฐœ์ž๊ฐ€ ์•Œ์•„๋‘์–ด์•ผ ํ•  ํ•„์š”๋Š” ๊ฑฐ์˜ ์—†๋‹ค. QPaintDevice๋Š” QPainter๋กœ ๊ทธ๋ฆด 2์ฐจ์› ์˜์—ญ์„ ์ถ”์ƒํ™”ํ•œ ๊ฒƒ์ด๋‹ค. ํŽ˜์ธํŠธ ๋””๋ฐ”์ด์Šค ์ค‘ QWidget์€ ์œ„์ ฏ์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ํ™”๋ฉด์„ ์˜๋ฏธํ•œ๋‹ค. ํŽ˜์ธํŠธ ๋””๋ฐ”์ด์Šค ์ค‘ QImage, QPixamp, QBitmap, QPicture, QSvgGenerator๋Š” Qt์—๋Š” ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ 5๊ฐœ์˜ ํด๋ž˜์Šค์ด๋‹ค. QImage๋Š” ์ž…์ถœ๋ ฅ์— ์ตœ์ ํ™”๋˜์–ด ์žˆ์œผ๋ฉฐ ํ”ฝ์…€ ๋‹จ์œ„๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค๋ฃจ๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ˜๋ฉด, QPixmap์€ ํ™”๋ฉด์— ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐ ์ตœ์ ํ™”๋˜์–ด ์žˆ๋‹ค. ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•˜๋ฉด QImage๋Š” ๋ฉ”์ธ ๋ฉ”๋ชจ๋ฆฌ์ƒ์˜ ์ด๋ฏธ์ง€์ด๊ณ  QPixmap์€ ๋น„๋””์˜ค์นด๋“œ์ƒ์— ์กด์žฌํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. QPixmap์€ ๋น ๋ฅธ ๋Œ€์‹  ํ•˜๋“œ์›จ์–ด์— ์˜์กดํ•˜๋ฉฐ, QImage๋Š” ํ”Œ๋žซํผ์— ์˜์กดํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ณดํ†ต QPixmap์€ ๋ช…์‹œ์ ์ธ(explicit) ๋”๋ธ” ๋ฒ„ํผ๋ง(double buffering)์— ์‚ฌ์šฉ๋˜๊ณ , QImage๋Š” ํ”ฝ์…€ ๋‹จ์œ„์˜ ์ž‘์—…์ด ํ•„์š”ํ•œ ๊ณ ํ’ˆ์งˆ์˜ ์ด๋ฏธ์ง€ ์ž‘์—…์„ ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. QBitmap์€ QPixmap์˜ ์ž์‹ ํด๋ž˜์Šค๋กœ ํ‘๋ฐฑ ํ”ฝ์Šค ๋งต์„ ์˜๋ฏธํ•œ๋‹ค. QPicture๋Š” QPainter API ๋ช…๋ น์„ ์ €์žฅํ•˜๊ณ  ๋ฆฌํ”Œ๋ ˆ์ดํ•˜๋Š” ์žฅ์น˜์ด๋‹ค. Qt๋Š” ์œˆ๋„์šฐ์ฆˆ ๋ฉ”ํƒ€ํŒŒ์ผ์ธ WMF๋‚˜ EMF๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์ง€๋งŒ QPicture๋ฅผ ํ†ตํ•ด QPainter์˜ ๋ช…๋ น์„ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ Qt ์ „์šฉ์˜ ๋ฉ”ํƒ€ํŒŒ์ผ(metafile)์„ ์ง€์›ํ•œ๋‹ค. QSvgGenerator๋Š” ํ‘œ์ค€ ๋ฒกํ„ฐ ๊ทธ๋ž˜ํ”ฝ ํŒŒ์ผ์ธ SVG ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ์‚ฌ์šฉ๋œ๋‹ค. QPrinter๋Š” ํ”„๋ฆฐํ„ฐ ๋””๋ฐ”์ด์Šค๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ฐ ๋ณดํ†ต QPrintDialog๋กœ ํ”„๋ฆฐํ„ฐ๋ฅผ ์ฐพ์•„์„œ ์‚ฌ์šฉํ•œ๋‹ค. QPrinter๋ฅผ ์ด์šฉํ•  ๋•Œ PDF ํ”„๋ฆฐํ„ฐ ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์žˆ์œผ๋ฉด PDF๋กœ ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋งŒ์•ฝ ์—†๋‹ค๋ฉด QPdfWriter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ PDF ๋ฌธ์„œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. QWidget๊ณผ QPrinter๋ฅผ ์ œ์™ธํ•˜๋ฉด ๋ชจ๋‘ Qt GUI ๋ชจ๋“ˆ์— ํฌํ•จ๋˜๊ณ , QSvgGenrator, QPrinter, QPdfWriter๋Š” ์“ฐ๊ธฐ ์ „์šฉ์ด๋‹ค. QPainter ์‚ฌ์šฉ๋ฒ• QPainter๋Š” ์ƒ์„ฑํ•  ๋•Œ ์ž์‹ ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋Œ€์ƒ ๋””๋ฐ”์ด์Šค๋ฅผ ์ƒ์„ฑ์ž QPainter(QPainterDevice)์—์„œ ์ง€์ •ํ•œ๋‹ค. ์ดํ›„ ์ž‘์—…์€ ๊ทธ๋ฆฌ๊ธฐ์™€ ๊ด€๋ จ๋œ API๋ฅผ ๋™์ผํ•˜๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ์œ„์ ฏ์— ๊ทธ๋ฆฌ๊ธฐ(=ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ)๋Š” ๋ฐ˜๋“œ์‹œ paintEvent(event) ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ ์ˆ˜ํ–‰๋˜์–ด ํ•œ๋‹ค. # ์œ„์ ฏ์— ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ def paintEvent(self, event): painter = QPainter(self) ... #์ด๋ฏธ์ง€์— ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ imagePainter = QPainter(image) # image๋Š” QImage์˜ ๊ฐ์ฒด ... # ํ”„๋ฆฐํ„ฐ์— ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ printerPainter = QPainter(printer) # printer๋Š” QPrinter ๊ฐ์ฒด ... Qt์—์„œ๋Š” ์ด์™€ ๊ฐ™์ด ํด๋ž˜์Šค ์ƒ์† ๊ฐœ๋…์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ๋””๋ฐ”์ด์Šค์˜ ์ถœ๋ ฅ์„ ๋™์ผํ•œ ์ž‘์—…์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ  ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ™”๋ฉด์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•  ๋•Œ ๋งŽ์ด ์“ฐ๋Š” ๊ธฐ๋ฒ• ์ค‘์— ํ•˜๋‚˜๊ฐ€ QPaintDevice๋ฅผ ์ธ์ž๋กœ ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ ํ›„ ์—ฌ๊ฑด์— ๋”ฐ๋ผ ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๋‹ค. class MyWidget(QWidget): ... def drawScreen(self, device): painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, true) painter.setPen(QPen(Qt.black, 4, Qt::DotLine, Qt.RoundCap)) # ํŽœ ์„ค์ • painter.setBrush(QBrush(Qt.green, Qt.SolidPattern)) # ๋ธŒ๋Ÿฌ์‹œ ์„ค์ • painter.setFont(QFont("Arial",30)) # ํฐํŠธ ์„ค์ • rect = QRect(80,80,400,200) painter.drawRoundedRect(rect, 50,50) # ๊ฒฝ๊ณ„์„ ์€ ํŽœ, ๋‚ด๋ถ€๋Š” ๋ธŒ๋Ÿฌ์‹œ painter.drawText(rect, Qt.AlignCenter,"Hello, Qt!") # ํŽœ์˜ ์ƒ‰์ƒ, ํฐํŠธ ์‚ฌ์šฉ def paintEvent(self, event): self.drawScreen(self) def printSomething(self): printer = QPrinter() dialog = QPrintDialog(printer, self) if dialog.exec_(): self.drawScreen(printer); def exportToImageFile(self): pixmap = QPixmap(self.size()) pixmap.fill(self.palette().color(self.backgroundRole())) self.drawScreen(pixmap) pixmap.save(โ€˜./test.pngโ€™) 6.1 QPainter ์ด๋ฒˆ ์ ˆ์€ ์˜ˆ์ œ๋ฅผ ๋จผ์ € ์†Œ๊ฐœํ•˜๊ณ  ์ˆœ์ฐจ์ ์œผ๋กœ ์„ค๋ช…ํ•œ๋‹ค. RenderLab ์˜ˆ์ œ๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ QPainter์˜ ๊ธฐ๋Šฅ์„ ํ…Œ์ŠคํŠธํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค. ๋จผ์ € ๋ฆฌ์†Œ์Šค ํŒŒ์ผ๊ณผ ๋ฆฌ์†Œ์Šค ์ปดํŒŒ์ผ์ด๋‹ค. RenderLab.qrc <!DOCTYPE RCC><RCC version="1.0"> <qresource> <file>images/brick.png</file> <file>images/qt-logo.png</file> </qresource> </RCC> >pyside2-rcc -o RenderArea_rc.py -py3 RenderArea.qrc ์†Œ์Šค์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ฝ”๋“œ๋Š” drawScene() ํ•จ์ˆ˜์—์„œ ๊ทธ๋ฆฌ๊ธฐ ์ž‘์—…์ด ์ด๋ฃจ์–ด์ง€๊ณ  ์ด ํ•จ์ˆ˜๋ฅผ paintEvent()๋‚˜ print_(), exportToFile() ์‹œ์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํŠน์ง•์ด๋‹ค. drawScene()์˜ ๊ทธ๋ฆฌ๊ธฐ ์ž‘์—…์€ ์„ ํƒํ•œ ๋ฉ”๋‰ด์— ๋ฐ˜์‘ํ•˜์—ฌ ์—ฌ๋Ÿฌ ์ž‘์—…์„ ํ•˜๋„๋ก ๋ถ„๋ฐฐํ•˜๊ณ  ์žˆ๋‹ค. ๊ฐ ์ž‘์—…์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋Š” ์ด์–ด์ง€๋Š” ๋ฌธ์„œ์—์„œ ์„ค๋ช…๊ณผ ํ•จ๊ป˜ ์†Œ๊ฐœํ•œ๋‹ค. RenderLab.py from PySide2.QtWidgets import QWidget, QActionGroup, QAction, QFileDialog from PySide2.QtPrintSupport import QPrintDialog, QPrinter from PySide2.QtGui import (QPainter, QPen, QBrush, QFont, QPalette, QPainterPath, QPixmap, QLinearGradient, QConicalGradient, QRadialGradient, QPdfWriter, QColor, QRegion) from PySide2.QtCore import Qt, QRect, QRectF, QPoint, QPointF, QFileInfo from PySide2.QtSvg import QSvgGenerator import RenderArea_rc class RenderArea(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setAutoFillBackground(True) self.setBackgroundRole(QPalette.Base) # load resource self.pixmapLogo = QPixmap() self.pixmapLogo.load(":/images/qt-logo.png") self.pixmapTexture = QPixmap() self.pixmapTexture.load(":/images/brick.png") # scene actions self.actionGroupScene = QActionGroup(self) texts = ["Draw Rendering API" , "Draw Qt::GlobalColor", "Draw Qt::PenStyle" , "Draw Qt::PenCapStyle and Qt::PenJoinStyle", "Draw Qt::BrushStyle" , "Draw Qt::FillRule", "Draw Path" , "Draw Text" , "Draw Gradient", "Drawing on Clipping Region" , "Draw Transform"] for i in range(len(texts)): action = QAction(texts[i],self) action.setCheckable(True) self.actionGroupScene.addAction(action) self.actionGroupScene.actions()[0].setChecked(True) self.actionGroupScene.triggered.connect(self.update) def sceneActions(self): return self.actionGroupScene def paintEvent(self, event): self.drawScene(self) def print_(self): printer = QPrinter() dialog = QPrintDialog(printer, self) if dialog.exec_(): drawScene(printer) def exportToFile(self): fileName,_ = QFileDialog.getSaveFileName(self,"Export file",".", "JPG file(*.jpg);;PNG file (*.png);;PDF file (*.pdf);;SVG file (*.svg)") if fileName == "": return if QFileInfo(fileName).suffix() == "jpg" or QFileInfo(fileName).suffix() == "png" : pixmap = QPixmap(self.size()) pixmap.fill(self.palette().color(self.backgroundRole())) self.drawScene(pixmap) pixmap.save(fileName) elif QFileInfo(fileName).suffix == "svg" : svgGenerator = QSvgGenerator() svgGenerator.setFile(fileName) self.drawScene(svgGenerator) else: pdfWriter = QPdfWriter(fileName) self.drawScene(pdfWriter) def drawScene(self, device): text = self.actionGroupScene.checkedAction().text() if text == "Draw Rendering API": self.drawRenderingAPI(device) elif text == "Draw Qt::GlobalColor": self.drawGlobalColor(device) elif text == "Draw Qt::PenStyle": self.drawPenStyle(device) elif text == "Draw Qt::PenCapStyle and Qt::PenJoinStyle": self.drawPenCapJoinStyle(device) elif text == "Draw Qt::BrushStyle": self.drawBrushStyle(device) elif text == "Draw Qt::FillRule": self.drawFillRule(device) elif text == "Draw Path": self.drawPath(device) elif text == "Draw Text": self.drawText(device) elif text == "Draw Gradient": self.drawGradient(device) elif text == "Drawing on Clipping Region": self.drawClipping(device) elif text == "Draw Transform": self.drawTransform(device) def drawRenderingAPI(self, device): ... def drawGlobalColor(self, device): ... def drawPenStyle(self, device): ... def drawPenCapJoinStyle(self, device): ... def drawBrushStyle(self, device): ... def drawFillRule(self, device): ... def drawPath(self, device): ... def drawText(self, device): ... def drawGradient(self, device): ... def drawClipping(self, device): ... def drawTransform(self, device): ... from PySide2.QtWidgets import QMainWindow, QAction class MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.renderArea = RenderArea(self) self.setCentralWidget(self.renderArea) actionPrint = QAction("&Print",self) actionPrint.triggered.connect(self.renderArea.print_) actionExport = QAction("&Export",self) actionExport.triggered.connect(self.renderArea.exportToFile) menuPrint = self.menuBar().addMenu("&PaintDevice") menuPrint.addAction(actionPrint) menuPrint.addAction(actionExport) menuScene = self.menuBar().addMenu("&Scene") menuScene.addActions(self.renderArea.sceneActions().actions()) from PySide2.QtWidgets import QApplication import sys if __name__ == '__main__': app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.resize(1100,401) mainWindow.show() app.exec_() 6.1.1 ๊ทธ๋ฆฌ๊ธฐ 1 ์•„๋ž˜ ๊ทธ๋ฆผ์€ RenderArea.drawRenderingAPI()๊ฐ€ ์‹คํ–‰๋œ ์ „๊ฒฝ์œผ๋กœ QPainter์˜ drawXXX() ํ˜•ํƒœ์˜ ๊ทธ๋ฆฌ๊ธฐ ํ•จ์ˆ˜ ์ค‘ drawImage(), drawPicture() ๋“ฑ ์ผ๋ถ€ ํ•จ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ  ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒƒ์ด๋‹ค. RenderLab.drawRenderingAPI(self, device) def drawRenderingAPI(self, device): painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, True) painter.setPen(QPen(Qt.black, 2, Qt.SolidLine, Qt.RoundCap)) painter.setBrush(QBrush(Qt.darkGreen, Qt.SolidPattern)) painter.setFont(QFont("Arial",12)) rect = QRect(10,20,180,120) rectText = QRect(0,140,200,40) rects = [QRect(10,20,100,80),QRect(80,60,100,80)] points = [QPoint(20,120),QPoint(40,20),QPoint(140,60),QPoint(180,120)] path = QPainterPath() path.moveTo(40,120) path.lineTo(40,60) path.cubicTo(180,0,100,100,180,120) startAngle = 90*16 arcLength = 120*16 # point painter.translate(10,10) painter.drawPoint(rect.center()) painter.drawText(rectText, Qt.AlignCenter,"drawPoint()") # points painter.translate(200,0) painter.drawPoints(points) painter.drawText(rectText, Qt.AlignCenter,"drawPoints()") # line painter.translate(200,0) painter.drawLine(rect.topLeft(),rect.bottomRight()) painter.drawText(rectText, Qt.AlignCenter,"drawLine()") # lines painter.translate(200,0) painter.drawLines(points) painter.drawText(rectText, Qt.AlignCenter,"drawLines()") # rectangle painter.translate(200, 0) painter.drawRect(rect) painter.drawText(rectText, Qt.AlignCenter, "drawRect()") # rectangles painter.translate(200, 0) painter.drawRects(rects) painter.drawText(rectText, Qt.AlignCenter, "drawRects()") # rounded rect painter.translate(-1000,200) painter.drawRoundedRect(rect, 25,25, Qt.RelativeSize) painter.drawText(rectText, Qt.AlignCenter,"drawRoundRect()") # ellipse painter.translate(200,0) painter.drawEllipse(rect) painter.drawText(rectText, Qt.AlignCenter,"drawEllipse()") # polyLine painter.translate(200,0) painter.drawPolyline(points) painter.drawText(rectText, Qt.AlignCenter,"drawPolyline()") # polygon painter.translate(200,0) painter.drawPolygon(points) painter.drawText(rectText, Qt.AlignCenter,"drawPolygon()") # convex polygon painter.translate(200,0) painter.drawConvexPolygon(points) painter.drawText(rectText, Qt.AlignCenter,"drawConvexPolygon()") # arc painter.translate(200,0) painter.drawArc(rect, startAngle, arcLength) painter.drawText(rectText, Qt.AlignCenter,"drawArc()") # chord painter.translate(-1000,200) painter.drawChord(rect, startAngle, arcLength) painter.drawText(rectText, Qt.AlignCenter,"drawChord()") # pie painter.translate(200,0) painter.drawPie(rect, startAngle, arcLength) painter.drawText(rectText, Qt.AlignCenter,"drawPie()") # path painter.translate(200,0) painter.drawPath(path) painter.drawText(rectText, Qt.AlignCenter,"drawPath()") # text painter.translate(200,0) painter.drawText(rect, Qt.AlignCenter,"Hello Qt") painter.drawText(rectText, Qt.AlignCenter,"drawText()") # pixmap painter.translate(200,0) painter.drawPixmap(40,40, self.pixmapLogo) painter.drawText(rectText, Qt.AlignCenter,"drawPixmap()") # fillRect linGradient = QLinearGradient(rect.bottomRight(),rect.topLeft()) linGradient.setColorAt(0, Qt.darkGreen) linGradient.setColorAt(0.5, Qt.green) linGradient.setColorAt(1.0, Qt.white) brush = QBrush(linGradient) painter.translate(200,0) painter.fillRect(rect, brush) painter.drawText(rectText, Qt.AlignCenter,"fillRect()") painter.translate(x, y)๋Š” QPainter์˜ ์ขŒํ‘œ๊ณ„๋ฅผ ํ‰ํ–‰์ด๋™ํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” drawXXX() ํ•จ์ˆ˜๋ฅผ ๊ทธ๋ฆด ๋•Œ QPoint, QRect ๋“ฑ์œผ๋กœ ์ง€์ •ํ•œ ์ขŒํ‘œ๊ฐ’์„ ์ƒ๋Œ€ ์ขŒํ‘œ๋กœ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜์˜€๋‹ค. ๋ณด์กฐ ํด๋ž˜์Šค QPainter์˜ drawXXX() ํ•จ์ˆ˜์—์„œ๋Š” QPoint, QLine, QRect, QPolygon ๋“ฑ๊ณผ ๊ฐ™์€ ์ , ์„ , ์‚ฌ๊ฐํ˜•, ํด๋ฆฌ๊ณค ๋“ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋‹ด์€ ๋ณด์กฐ ํด๋ž˜์Šค๊ฐ€ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. QPointF, QLineF, QRectF, QPolygonF ๋“ฑ๊ณผ ๊ฐ™์ด F๋กœ ๋๋‚˜๋Š” ํด๋ž˜์Šค๋Š” ์‹ค์ˆ˜ํ˜• ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค. 2์ฐจ์› ๊ทธ๋ž˜ํ”ฝ์Šค๋ฅผ ๋‹ด๋‹นํ•˜๋Š” QPainter์—์„œ ๊ทธ๋ฆฌ๊ธฐ ๋Œ€์ƒ์ธ ํ™”๋ฉด์€ ์ •์ˆ˜๋กœ ์ขŒํ‘œ๋ฅผ ํ‘œํ˜„ํ•˜์ง€๋งŒ, ์น˜์ˆ˜ ๊ณ„์‚ฐ์ด ๋งŽ์€ ๊ฒฝ์šฐ ๋ผ์šด๋“œ ์˜คํ”„ ์—๋Ÿฌ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด ์‹ค์ˆ˜ํ˜•์˜ ๋ณด์กฐ ํด๋ž˜์Šค๊ฐ€ ๋„์ž…๋˜์—ˆ๋‹ค. ๋˜ํ•œ ๋’ค์— ์„ค๋ช…ํ•  ์•คํ‹ฐ์–ผ๋ผ์ด์‹ฑ์ด ํ™œ์„ฑํ™”๋œ ๊ฒฝ์šฐ ํŠน๋ณ„ํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ธฐ๋„ ํ•œ๋‹ค. ๋ณด์กฐ ํด๋ž˜์Šค ์ค‘ ๊ฐ€์žฅ ์œ ์šฉํ•œ ํด๋ž˜์Šค๋Š” QRect์ž…๋‹ˆ๋‹ค. ์ด๋“ค ํด๋ž˜์Šค๋Š” left(), right(), width(), height(), center(), bottomLeft(), topRight() ๋“ฑ๊ณผ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ๊ด€๋ก€์ ์œผ๋กœ right()์™€ bottom()์ด ๊ฐ๊ฐ left()+width()-1, top()+height()-1 ๋“ฑ 1ํ”ฝ์…€๋งŒํผ ์ž‘์€ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ์ ์ด๋‹ค. ๋งŒ์•ฝ ์ •ํ™•ํ•œ ๊ฐ’์„ ๊ตฌํ•˜๋ ค๋ฉด left() + width(), right() + height()๋ฅผ ์“ฐ์•ผ ํ•œ๋‹ค. QRect๋Š” translate(x, y), translated(x, y), contains(), intersects() ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. rect = QRect(10,10,100,50) # x, y, width, height rect.translate(10,20) # (10,20) ๋งŒํผ ํ‰ํ–‰์ด๋™ if rect.contains(5,20): # (5,10)์ด rect์— ํฌํ•จ๋˜๋Š”์ง€ ๊ฒ€ํ†  ... rect = rectA.intersected(rectB) // rectA์™€ rectB์˜ ๊ต์ฐจ ์˜์—ญ์„ ๋ฆฌํ„ด QPolygon๋Š” OPoint, QPolygonF๋Š” QPointF์˜ ๋ฐฐ์—ด๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์˜์—ญ์ด๋ฏ€๋กœ QRect ๋“ฑ๊ฐ€ ์œ ์‚ฌํ•˜๊ฒŒ subtracted(), tranlsate(), united(), intersected(), containsPoint() ๋“ฑ ์˜์—ญ๊ณผ ๊ด€๋ จ๋œ ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง„๋‹ค. ๊ธฐ๋ณธ ๊ทธ๋ฆฌ๊ธฐ ํ•จ์ˆ˜๋“ค ์ ๊ณผ ์„ ์„ ๊ทธ๋ฆฌ๋Š” drawPoint(), drawPoints(), drawLine(), drawLines() ํ•จ์ˆ˜์ด๊ณ , QPainter์˜ ํŽœ ์†์„ฑ(QPen) ์†์„ฑ์ด ์‚ฌ์šฉ๋œ๋‹ค. ์‚ฌ๊ฐํ˜•์€ drawRect(), drawRects(), ๋ชจ์„œ๋ฆฌ๊ฐ€ ๋‘ฅ๊ทผ ์‚ฌ๊ฐํ˜•์€ drawRoundedRect(), ํƒ€์›์€ drawEllipse()๋กœ ๊ทธ๋ฆฐ๋‹ค. ๊ฒฝ๊ณ„์„ ์„ ๊ทธ๋ฆด ๋•Œ๋Š” QPainter์˜ ํŽœ(QPen) ์†์„ฑ, ๋‚ด๋ถ€๋Š” ๋ธŒ๋Ÿฌ์‹œ(QBrush) ์†์„ฑ์ด ์‚ฌ์šฉ๋œ๋‹ค. ํด๋ฆฌ ๋ผ์ธ(polyline)์€ ๊ฒฝ๊ณ„์„ ๋งŒ ์žˆ๋Š” ๋„ํ˜•์„ ์˜๋ฏธํ•˜๊ณ , ํด๋ฆฌ๊ณค์€ ๋‚ด๋ถ€๊ฐ€ ๊ณพ์ฐฌ ๋„ํ˜•์„ ์˜๋ฏธํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํด๋ฆฌ ๋ผ์ธ์€ ํŽœ(QPen) ์†์„ฑ๋งŒ์„ ํด๋ฆฌ๊ณค์€ ํŽœ(QPen)๊ณผ ๋ธŒ๋Ÿฌ์‹œ(QBrush) ์†์„ฑ์„ ์ด์šฉํ•œ๋‹ค. ํด๋ฆฌ ๋ผ์ธ์€ drawPolyline(), ํด๋ฆฌ๊ณค์€ drawPolygon(), drawConvexPolygon()์„ ์‚ฌ์šฉํ•œ๋‹ค. drawConvexPolygon()์€ drawPolygon()๊ณผ ๊ฐ™์€๋ฐ ๊ทธ๋ฆฌ๊ธฐ ์†๋„๊ฐ€ ์šฐ์ˆ˜ํ•˜์ง€๋งŒ, ํด๋ฆฌ๊ณค ๋‚ด๊ฐ์ด 180๋„ ๋ณด๊ฐ€ ํฐ ๊ฒฝ์šฐ(non-convex์ธ ๊ฒฝ์šฐ) ํ”Œ๋žซํผ์— ๋”ฐ๋ผ ์˜๋„ํ•˜์ง€ ์•Š๊ฒŒ ๊ทธ๋ ค์งˆ ์ˆ˜ ์žˆ๋‹ค. drawPolygon()์˜ Qt::FillRule์€ ํด๋ฆฌ๊ณค์ด๋‚˜ ๋’ค์— ์„ค๋ช…ํ•œ ํŒจ์Šค๋กœ ๊ตฌ์„ฑ๋˜๋Š” ๋„ํ˜•์˜ ๋‚ด๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ •์˜ํ•œ๋‹ค. Qt.OddEvenFill ๋˜๋Š” Qt.WindingFill์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ ์ฐจ์ด๊ฐ€ ์—†์ง€๋งŒ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ณ„ ๋ชจ์–‘์ฒ˜๋Ÿผ ํด๋ฆฌ๊ณค์ฒ˜๋Ÿผ ์„ ๋ถ„๋“ค์ด ๊ต์ฐจํ•˜์—ฌ ๋‚ด๋ถ€์— ๋˜ ๋‹ค๋ฅธ ์˜์—ญ์„ ํ˜•์„ฑํ•˜๋ฉด ๊ทธ ์˜๋ฏธ๊ฐ€ ์ƒ๊ธด๋‹ค. Qt.OddEvenFill์€ ๋‚ด์™ธ๋ถ€๊ฐ€ ๋ฒˆ๊ฐˆ์•„ ๋‚˜์˜ค๋ฉฐ(ํด๋ฆฌ๊ณค ์™ธ๋ถ€์˜ ํ•œ์ ์—์„œ ์ˆ˜ํ‰์œผ๋กœ ์„ ์„ ํด๋ฆฌ๊ณค์„ ๊ตฌ์„ฑํ•˜๋Š” ์„ ๋ถ„์„ ๋งŒ๋‚˜๋ฉด ๋ธŒ๋Ÿฌ์‹œ๋กœ ์น ํ•˜๊ณ , Qt.WindingFill์€ ๋„ํ˜• ์™ธ๊ณฝ์„  ์•ˆ์ชฝ์„ ๋ชจ๋‘ ์น ํ•œ๋‹ค. ์•„ํฌ(arc)๋Š” ํ˜„์„, ์ฝ”๋“œ(chord)๋Š” ํ˜„๊ณผ ํ˜„์˜ ์–‘๋‹จ์„ ์ž‡๋Š” ์„ ๋ถ„์„ ๊ฐ€์ง„ ๋„ํ˜•์„, ํŒŒ์ด(pie)๋Š” ํ˜„๊ณผ ์›์ ์„ ์ž‡๋Š” ๋„ํ˜•์„ ์˜๋ฏธํ•œ๋‹ค. ์•„ํฌ(arc)๋ฅผ ํŽœ(QPen) ์†์„ฑ๋งŒ ์ด์šฉํ•˜๋ฉฐ, ์ฝ”๋“œ(chord) ๋ฐ ํŒŒ์ด(pie)๋Š” ํŽœ๊ณผ ๋ธŒ๋Ÿฌ์‹œ ์†์„ฑ์„ ์ด์šฉํ•œ๋‹ค. drawArc(), drawChord(), drawPie() ๋“ฑ์˜ ํ•จ์ˆ˜๋ฅด ์‚ฌ์šฉํ•œ๋‹ค. startAngle, spanAngle๋Š” 1/16๋„ ๋‹จ์œ„์ด๋‹ค( 360๋„๊ฐ€ ๋˜๋ ค๋ฉด 5760=16 * 360). ๊ฐ์˜ ๊ธฐ์ค€์ ์€ 3์‹œ ๋ฐฉํ–ฅ์ด๋ฉฐ, +๊ฐ’์€ ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ, -๊ฐ’์€ ์‹œ๊ณ„๋ฐฉํ–ฅ์ด๋‹ค. 6.1.2 ๊ทธ๋ฆฌ๊ธฐ 2 ํŒจ์Šค ํŒจ์Šค(QPainterPath)๋ฅผ ์ด์šฉํ•˜๋ฉด ์ง์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ณก์„ ์ด ํฌํ•จ๋œ ๋ณต์žกํ•œ ๋„ํ˜•์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ํŒจ์Šค๋Š” moveTo(), lineTo(), cubicTo(), quadTo()๋ผ๋Š” 4๊ฐœ์˜ ๊ธฐ๋ณธ ํ•จ์ˆ˜๋ฅผ ์—ฐ์†์ ์œผ๋กœ ํ˜ธ์ถœํ•˜์—ฌ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. lineTo(), cubicTo(), quadTo()๋Š” ํ˜„์žฌ ์  ์œ„์น˜(currentPosition()๋กœ ์–ป์„ ์ˆ˜ ์žˆ์Œ)์—์„œ ์ฃผ์–ด์ง„ ๋‹ค๋ฆ„ ์  ์ •๋ณด๋กœ ์ง์„ ์ด๋‚˜ 2์ฐจ, 3์ฐจ ๋ฒ ์ง€์–ด ๊ณก์„ ์„ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ˜ธ์ถœ์ด ๋๋‚˜๋ฉด ํ˜„์žฌ ์  ์œ„์น˜๋Š” ์ง์„  ๋˜๋Š” ๊ณก์„ ์˜ ๋์ ์œผ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค. moveTo()๋Š” ๋‹จ์ˆœํžˆ ํ˜„์žฌ ์  ์œ„์น˜๋ฅผ ์˜ฎ๊ธฐ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. QPainterPath์˜ lineTo() ๋“ฑ๊ณผ ๊ฐ™์€ ๊ทธ๋ฆฌ๊ธฐ ํ•จ์ˆ˜๋Š” ์‹ค์ˆ˜ํ˜•์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. quadTo()๋กœ ๊ทธ๋ฆฌ๋Š” 2์ฐจ ๋ฒ ์ง€์–ด ๊ณก์„ ์€ ์‹œ์ž‘์ , ๋์ , ์ œ์–ด์ ์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š”๋ฐ ์‹œ์ž‘์ ์€ ํŒจ์Šค์˜ ํ˜„์žฌ ์  ์œ„์น˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๊ณ , ๋‚˜๋จธ์ง€ ์ œ์–ด์ ๊ณผ ๋์ ์œผ๋กœ ์ธ์ž๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. cubicTo()๋กœ ๊ทธ๋ฆฌ๋Š” 3์ฐจ ๋ฒ ์ง€์–ด ๊ณก์„ ์€ ์ œ์–ด์ ์ด 1๊ฐœ ๋” ์žˆ๋Š” ํ˜•ํƒœ์ด๋‹ค. ์—ฐ๊ฒฐ๋œ ์„ ์ด๋‚˜ ๊ณก์„ ์„ ์„œ๋ธŒ ํŒจ์Šค(sub path)๋ผ๊ณ  ํ•˜๋Š”๋ฐ ํŒจ์Šค ๋‚ด์—๋Š” ์—ฌ๋Ÿฌ ์„œ๋ธŒ ํŒจ์Šค๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด moveTo()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์—ฐ๊ฒฐ๋œ ์„ ์ด๋‚˜ ๊ณก์„ ์ด ๋Š์–ด์ง€๋ฉด์„œ ์„œ๋ธŒ ํŒจ์Šค๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. closeSubpath()๋Š” ๊ฐ•์ œ๋กœ ์„œ๋ธŒ ํŒจ์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ํ˜„ ์œ„์น˜๋ฅผ (0,0)์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. addEllipse(), addPolygon(), addRect(), addRoundedRect(), addArc() ๋“ฑ์˜ ํ•จ์ˆ˜๋Š” ์ด๋“ค 4๊ฐœ์˜ ๊ธฐ๋ณธ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„๋˜์–ด ์žˆ์œผ๋ฉฐ, QPainter.drawEllipse() ๋“ฑ๊ณผ ๊ฐ™์ด QPainter์˜ ๋Œ€์‘ํ•˜๋Š” ํ•จ์ˆ˜์™€ ์œ ์‚ฌํ•œ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•œํŽธ, addText(point, font, text) ํ•จ์ˆ˜๋กœ ์ฃผ์–ด์ง„ ํฐํŠธ์˜ ๋ฌธ์ž์—ด์˜ ์œค๊ณฝ์„ ์„ ๊ฐ–๋Š” ๋„ํ˜•์œผ๋กœ ํŒจ์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ RenderLab ์˜ˆ์ œ์˜ RenderArea.drawPath()๋ฅผ ์‹คํ–‰ํ•œ ๋ชจ์Šต์ด๋‹ค. ์™ผ์ชฝ ๋„ํ˜•์€ addRect()๋กœ ์‚ฌ๊ฐํ˜•์„ ๊ทธ๋ฆฐ ํ›„, 2๊ฐœ์˜ ๋ฒ ์ง€์–ด ๊ณก์„ ์œผ๋กœ ํŒจ์Šค๋ฅผ ๊ตฌ์„ฑํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‚ด๋ถ€์— ์น ํ•˜๋Š” ๊ฒƒ์€ ๋””ํดํŠธ์ธ Qt::OddFill์ด ์„ค์ •์— ๋”ฐ๋ผ ๊ทธ๋ฆฐ ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ์ด๋ฅผ ๋ฐ”๊พธ๋ ค๋ฉด path.setFillRule(Qt::WindingFill)์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋ฅธ์ชฝ ๋„ํ˜•์€ ๋ฌธ์ž์—ด์˜ ์œค๊ณฝ์„ ์„ ์ด์šฉํ•ด ํŒจ์Šค๋ฅผ ๊ตฌ์„ฑํ•œ ๊ฒƒ์ด๋‹ค. ๋‚ด๋ถ€๋ฅผ ์น ํ•˜๋Š” ๋ธŒ๋Ÿฌ์‹œ๋กœ ๊ทธ๋ž˜๋”” ์–ธํŠธ๋ฅผ ์„ค์ •ํ•˜์˜€๋‹ค. RenderArea.drawPath() def drawPath(self, device): painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, True) painter.translate(50,50) painter.scale(2,2) # basic path path = QPainterPath() path.addRect(20,20,60,60) path.moveTo(0,0) path.cubicTo(99, 0,50,50,99,99) path.cubicTo( 0,99,50,50, 0, 0) path.contains(QPointF(40,40)) painter.setPen(QPen(QColor(79,106,25),1,Qt.SolidLine, Qt.FlatCap, Qt.MiterJoin)) painter.setBrush(QColor(122,163,39)) painter.drawPath(path) # text outline path linearGradient = QLinearGradient(0,0,200,200) linearGradient.setColorAt(0.,Qt.blue) linearGradient.setColorAt(1.,Qt.white) font = QFont("Helvetical",80) baseline = QPointF(100,80) outlinePath = QPainterPath() outlinePath.addText(baseline, font,"Qt") painter.setBrush(linearGradient) painter.setPen(Qt.NoPen) painter.drawPath(outlinePath) QPainterPath์˜ ๊ธฐํƒ€ ํ•จ์ˆ˜๋กœ๋Š” ํŒจ์Šค ๋‚ด์— ํด๋ฆฌํ•‘ ์˜์—ญ์„ ์„ค์ •ํ•˜๋Š” addRegion(), ํŒจ์Šค๋ฅผ ํด๋ฆฌ๊ณค์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” toFillPolygon(), toFillPolygons(), toSubpathPolygons(), ํŒจ์Šค๋ฅผ ์—ฐ๊ฒฝํ•˜๋Š” addPath(), connectPath(), ์ , ์‚ฌ๊ฐํ˜•, ํŒจ์Šค ๋“ฑ์˜ ๋„ํ˜•์ด ํ˜„ ํŒจ์Šค ๋‚ด์— ์žˆ๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” contains() ํ•จ์ˆ˜ ๋“ฑ์ด ์žˆ๋‹ค. ํ…์ŠคํŠธ ํ…์ŠคํŠธ์€ drawText() ํ•จ์ˆ˜๋กœ QPainter์— ์„ค์ •๋œ ํŽœ์˜ ์ƒ‰์ƒ๊ณผ ํฐํŠธ๋กœ ๊ทธ๋ฆฐ๋‹ค. Qt.NoPen์œผ๋กœ ํŽœ ์Šคํƒ€์ผ์„ ์ง€์ •ํ•˜๋ฉด ํ™”๋ฉด์ƒ์— ํ…์ŠคํŠธ๊ฐ€ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š” ์ ์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ, QPainter์˜ ๋ฐฑ๊ทธ๋ผ์šด ๋ธŒ๋Ÿฌ์‹œ๋ฅผ ์„ค์ •ํ•  ๊ฒฝ์šฐ ํ…์ŠคํŠธ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ์˜์—ญ์˜ ๋ฐฐ๊ฒฝ์ƒ‰์„ ์น ํ•  ์ˆ˜ ์žˆ๋‹ค. painter = QPainter(self) ... painter.setPen(Qt.red) # ์ƒ‰์ƒ painter.setBackground(Qt.gray) # ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋ธŒ๋Ÿฌ์‹œ painter.setBackgroundMode(Qt.OpaqueMode) # ๋ฐฑ๊ทธ๋ผ์šด ๋ธŒ๋Ÿฌ์‹œ enable painter.setFont(QFont("Arial",15)) # ํฐํŠธ painter.drawText(10,10, โ€œHello, Qtโ€); drawText() ํ•จ์ˆ˜๋Š” (1) ๋‹จ์ˆœํžˆ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฒ„์ „๊ณผ (2) ์‚ฌ๊ฐํ˜•๊ณผ ์ •๋ ฌ ๋ฐฉ์‹(alignment)๋ฅผ ๋™์‹œ์— ์ง€์ •ํ•˜๋Š” ๋ฒ„์ „ ๋ฅ˜์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. drawText(x, y, text) ๋“ฑ์€ (1)์— ํ•ด๋‹นํ•˜๊ณ , drawText(rect, flags, text, boundingRect) ๋“ฑ์€ (2)์— ํ•ด๋‹นํ•œ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ RenderLab ์˜ˆ์ œ์˜ RenderArea.drawText()๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. (1) ์œ ํ˜•์˜ drawText() ํ•จ์ˆ˜์—์„œ ์ง€์ •๋œ ์œ„์น˜๋Š” ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ขŒ์ธก ํ•˜๋‹จ๋ถ€์— ์žˆ๋‹ค. RenderLab.drawText() def drawText(self, device): painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, True) painter.setFont(QFont("Arial",100)) painter.setPen(Qt.blue) painter.setBackground(Qt.gray); painter.setBackgroundMode(Qt.OpaqueMode) point = QPointF(100,200) rect = QRectF(450,50,400,200) painter.drawText(point,"Qt") painter.setPen(QPen(Qt.red, 10)) painter.drawPoint(point) painter.drawText(rect, Qt.AlignCenter,"Qt") painter.setPen(QPen(Qt.red, 1)) painter.drawRect(rect) ์ด๋ฏธ์ง€ ์ด๋ฏธ์ง€๋Š” drawPixmap() ๋˜๋Š” drawImage()๋กœ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๊ฐ๊ฐ ํ”ฝ์Šค๋งต(QPixmap)๊ณผ QImage๋ฅผ ๊ทธ๋ฆฌ๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ์ด๋“ค ํ•จ์ˆ˜๋Š” ๋‹ค์–‘ํ•œ ์˜ค๋ฒ„๋กœ๋“œ ๋ฒ„์ „์„ ์ œ๊ณตํ•˜๋Š” ๋ฐ ์ ๋งŒ์„ ์ฃผ์–ด์ง€๋ฉด ์ด๋ฏธ์ง€ ์™œ๊ณก ์—†์ด ํ‘œ์‹œํ•˜์ง€๋งŒ ์‚ฌ๊ฐ์˜์—ญ์ด๋‚˜ ์Šค์ผ€์ผ๋ง ๊ด€๋ จ ์ธ์ž๋ฅผ ์ถ”๊ฐ€๋กœ ์ง€์ •ํ•˜๋ฉด ๊ทธ๋ฆผ์— ์™œ๊ณก์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค. fillRect()์™€ fillPath() ์‚ฌ๊ฐ์˜์—ญ์ด ๋‚œ ํŒจ์Šค๋กœ ๊ตฌ์„ฑ๋˜๋Š” ์˜์—ญ์„ ์™ธ๊ณฝ์„  ์—†์ด ๊ทธ๋ฆฌ๋Š” ๊ฒƒ์€ fillRect() ๋˜๋Š” fillPath()๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋“ค์€ drawRect()๋‚˜ drawPath()์—์„œ ํŽœ์„ Qt.NoPen์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ๊ทธ๋ฆฐ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋Œ€์‹  ๋ธŒ๋Ÿฌ์‹œ๋ฅผ QPainter์˜ ์†์„ฑ์œผ๋กœ ์‚ฌ์šฉ๋œ ๋ธŒ๋Ÿฌ์‹œ ๋Œ€์‹  ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ๋ธŒ๋Ÿฌ์‹œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค(fillRect(rect, brush), fillPath(path, brush) ). 6.1.3 ์†์„ฑ - ์•ˆํ‹ฐ์—์ผ๋ผ์ด์‹ฑ ์ด๋ฒˆ ์ ˆ์—์„œ๋Š” QPainter์˜ ๊ธฐ๋ณธ ์†์„ฑ์ธ ํŽœ, ๋ธŒ๋Ÿฌ์‹œ, ํฐํŠธ๋ฅผ ํฌํ•จํ•œ ์—ฌ๋Ÿฌ ์†์„ฑ์„ ์‹ฌํ™”ํ•ด์„œ ์„ค๋ช…ํ•œ๋‹ค. ๋…ผ๋ฆฌ์  ํ‘œํ˜„๊ณผ ํ”ฝ์…€ ํ‘œํ˜„, ์•ˆํ‹ฐ์—์ผ๋ผ์ด์‹ฑ(antialiasing) QPainter์—์„œ๋Š” ํŠน๋ณ„ํ•˜๊ฒŒ ์ขŒํ‘œ๊ณ„๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ขŒ์ธก ์ƒ๋‹จ์ด ์›์  (0,0)์ด๊ณ , ์˜ค๋ฅธ์ชฝ์œผ๋กœ +x, ์•„๋ž˜์ชฝ์œผ๋กœ +y์ธ ์ขŒํ‘œ๊ณ„๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋…ผ๋ฆฌ์ ์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ , ์„ , ๋ฉด ๋“ฑ์ด ํ™”๋ฉด์— ํ‘œํ˜„๋  ๋•Œ๋Š” ํ”ฝ์…€ ๋‹จ์œ„๋กœ ํ‘œํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ๊ทœ์น™์— ์˜ํ•ด ํ”ฝ์…€์„ ์ฑ„์šฐ ๊ฐœ ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋…ผ๋ฆฌ์ ์œผ๋กœ (2,3) ์œ„์น˜์˜ ์ ์€ ํ”ฝ์…€์˜ ์ค‘์‹ฌ์ ์ด (2.5,3.5)์ธ ํ”ฝ์…€์„ ์ฑ„์šฐ๊ฒŒ ๋œ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ QLine(2, 7, 6, 1)์€(2,7)๊ณผ (6,1)์„ ์ž‡๋Š” ์„ ๋ถ„์ด๋‹ค. ์‹ค์ œ ํ”ฝ์…€ ๋‹จ์œ„์˜ ํ™”๋ฉด์— ํ‘œํ˜„๋  ๋•Œ๋Š” ๋‘ ๋ฒˆ์งธ ์—ด๊ณผ ๊ฐ™์ด (2.5,7.5)์™€ (1.5,6.5)๋ฅผ ์ค‘์‹ฌ์ ์œผ๋กœ ๊ฐ–๋Š” ํ”ฝ์…€์„ ์—ฐ๊ฒฐํ•˜๋Š” ์„ ๋ถ„์ด ๋˜๋ฉฐ ์ด๋•Œ ์ง€๊ทธ์žฌ๊ทธ ํ˜•ํƒœ๋กœ ํ‘œํ˜„๋˜๊ฒŒ ๋œ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋†’์€ ํ•ด์ƒ๋„์˜ ์‹ ํ˜ธ๋ฅผ ๋‚ฎ์€ ํ•ด์ƒ๋„์—์„œ ๋‚˜ํƒ€๋‚ผ ๋•Œ, ๋˜๋Š” ๋…ผ๋ฆฌ์ ์ธ ํ‘œํ˜„์„ ์ด์‚ฐ์ ์œผ๋ฃŒ ํ‘œํ˜„ํ•  ๋•Œ ํŒจํ„ด์ด ๊นจ์ง€๋Š” ํ˜„์ƒ์„ ์—์ผ๋ฆฌ์–ด์‹ฑ(aliasing)์ด๋ผ๊ณ  ํ•œ๋‹ค. ์•ˆํ‹ฐ์—์ผ๋ฆฌ์–ด์‹ฑ(antialiasing)์€ ์—์ผ๋ฆฌ์–ด์‹ฑ์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์˜๋ฏธํ•˜๋Š”๋ฐ, ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ์•ˆํ‹ฐ์—์ผ๋ฆฌ์–ด์‹ฑ์„ ์ ์šฉํ•˜๋ฉด ๋„ํ˜•์˜ ๊ฒฝ๊ณ„์„ ์„ ๋ณด๋‹ค ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ์•ˆํ‹ฐ์—์–ผ๋ฆฌ์–ด์‹ฑ์„ ์ ์šฉํ•˜๋ฉด ๊ทธ๋ฆฐ ์‚ฌ์„ ์˜ ๋…ผ๋ฆฌ์ ์ธ ํ‘œํ˜„์œผ๋กœ ๊ตฌ์„ฑ๋œ ์„ ๋ถ„์˜ ์ขŒ์šฐ๋กœ ๋Œ€์นญ์ด ๋˜๋„๋ก ํ”ฝ์…€์ผ ์ฑ„์›Œ์ง์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ์ด์œ ์—์„œ QPainter๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ๋Œ€๋ถ€๋ถ„์ด ์ฝ”๋“œ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™œ์„ฑํ™”๋ฅผ ํ•ด์ค€๋‹ค. painter = QPainter(self) painter.setRenderHint(QPainter::Antialising, True) ... ์—์–ผ๋ฆฌ์–ด์‹ฑ์ธ ๊ฒฝ์šฐ ํŽœ ๋‘๊ป˜๊ฐ€ ์ง์ˆ˜(even number)์ด๋ฉด ๋…ผ๋ฆฌ์ ์œผ๋กœ ์ฃผ์–ด์ง„ ์ขŒํ‘œ๊ฐ’์—์„œ ๋Œ€์นญ์œผ๋กœ ํ‘œํ˜„๋˜์ง€๋งŒ, ํ™€์ˆ˜(odd number)์ด๋ฉด ์˜ค๋ฅธ์ชฝ๊ณผ ์•„๋ž˜์ชฝ์œผ๋กœ 1 ํ”ฝ์…€๋งŒํผ ๋” ๊ทธ๋ฆฌ๊ฒŒ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์•ˆํ‹ฐ์—์–ผ๋ฆฌ์–ด์‹ฑ์—์„œ๋Š” ๊ฐ€๋Šฅํ•œ ๋…ผ๋ฆฌ์ ์ธ ํ‘œํ˜„์— ๋งž๋Š” ์„ ์„ ๊ตฌ์„ฑํ•˜๊ณ , ๋Œ€์นญ์ด ๋˜๋„๋ก ํ”ฝ์…€์„ ์ฑ„์šด๋‹ค. ๋”ฐ๋ผ์„œ QRectF ์™€ ๊ฐ™์€ ์‹ค์ˆ˜ํ˜•์„ ์‚ฌ์šฉํ•  ๋•Œ ๋” ํฐ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ฒŒ ๋œ๋‹ค. ์ฐธ๊ณ ๋กœ QPainter.setRenderHint() ํ•จ์ˆ˜๋Š” QPainter.Antialiasing ์™ธ์—๋„ ํ…์ŠคํŠธ์˜ ์•ˆํ‹ฐ์—์ผ๋ฆฌ์–ด์‹ฑ์„ ์ง€์ •ํ•˜๋Š” QPainter.TextAntialiasing, ๋ถ€๋“œ๋Ÿฌ์šด ํ”ฝ์Šค ๋งต ๋ณ€ํ™˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์“ฐ๋„๋ก ํ•˜๋Š” QPainter.SmoothPixmapTransform ๋“ฑ์„ ์†์„ฑ์„ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ์ด์ค‘ Qt ํŽ˜์ธํŠธ ์‹œ์Šคํ…œ์€ ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ์•ˆํ‹ฐ์—์ผ๋ฆฌ์–ด์‹ฑ์„ ํ•ญ์ƒ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— QPainter.TextAntialiasing์€ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋งŒ์•ฝ QPainter.Antialiasing๊ณผ QPainter.SmoothPixmapTransform์„ ๋™์‹œ์— ์ง€์ •ํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. painter = QPainter(device) # ๋“œ๋กœ์ž‰์— ๋Œ€ํ•œ ์•ˆํ‹ฐ ์—์–ด๋ฆฌ์–ด ์‹ฑ๊ณผ ๋ถ€๋“œ๋Ÿฌ์šด ํ”ฝ์Šค ๋งต ๋ณ€ํ™˜์„ ๋™์‹œ์— ์ง€์ •ํ•  ๋•Œ painter.setRenderHints(QPainter.Antialising|QPainter.SmoothPixmapTransform) ... 6.1.4 ์†์„ฑ - ์ƒ‰์ƒ Qt์—์„œ ์ƒ‰์ƒ์€ QColor ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ƒ‰์ƒ์„ ํ‘œํ˜„ํ•˜๋ฉฐ, QColor๋กœ ํŽœ์ด๋‚˜ ๋ธŒ๋Ÿฌ์‹œ ๋“ฑ ์ƒ‰์ƒ ์ •๋ณด๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. QColor๋Š” RGB(red, green, blue), HSV(hue, saturation, value), CMYK(cyan, magenta, yellow, black) ์ƒ‰์ƒ์„ ์ง€์›ํ•œ๋‹ค. ์ด๋“ค ์ƒ‰์ƒ์„ ๊ตฌ์„ฑํ•˜๋Š” ์„ฑ๋ถ„์— ๋ถˆํˆฌ๋ช…๋„๋ฅผ ์ง€์ •ํ•˜๋Š” A(alpha) ์„ฑ๋ถ„๊นŒ์ง€ ํฌํ•จํ•ด์„œ ํ‘œํ˜„๋˜๋Š” ์ƒ‰์ƒ์„ ์™„์ „ํ•˜๊ฒŒ ์ •์˜ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ฐ ์„ฑ๋ถ„์€ ์ •์ˆ˜๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” 0~255์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ , ์‹ค์ˆ˜๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” 0.~1.์˜ ๊ฐ–๋Š”๋‹ค. QColor์˜ ์ƒ์„ฑ์ž๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ RGB ์ƒ‰์ƒ์— ๊ธฐ์ดˆํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์ƒ์„ฑ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. QColor(r, g, b, a=255) # QColor ( int r, int g, int b, int a = 255 ) in C++, a=255๊ฐ€ ๋ถˆํˆฌ๋ช…์„ ์˜๋ฏธ QColor(name) # QColor ( const QString& name) in C++ QColor(color) # QColor ( Qt::GlobalColor color) in C++ ์ฒซ ๋ฒˆ์งธ ์„ฑ์„ฑ์ž๋Š” a๋ฅผ ํฌํ•จํ•œ ๊ฐ ์„ฑ๋ถ„์„ ์ •์ˆซ๊ฐ’์œผ๋กœ ์ง€์ •ํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ƒ์„ฑ์ž๋Š” #์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํŠน์ˆ˜ ๋ฌธ์ž์—ด์„ ์ด์šฉํ•œ๋‹ค. red = QColor(โ€œ#ff0000โ€) # #rrggbb ํฌ๋งท, rr ๋“ฑ์€ 16์ง„์ˆ˜, QColor red(255,0,0); ์™€ ๋™์ผ red = QColor(โ€œ#ffff0000โ€) # #aarrggbb ํฌ๋งท, aa ๋“ฑ์€ 16์ง„์ˆ˜, QColor red(255,0,0,255)์™€ ๋™์ผ ์„ธ ๋ฒˆ์งธ ์„ฑ์„ฑ์ž๋Š” Qt.red, Qt.blue ๋“ฑ Qt.GlobalColor ์—ด๊ฑฐํ˜•์œผ๋กœ ๋ฏธ๋ฆฌ ์ง€์ •๋˜์–ด ์žˆ๋Š” ์ƒ‰์ƒ์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. darkGreen = QColor(Qt.darkGreen) HSV, CMYK ์ƒ‰์ƒ์€ fromHsv(h, s, v, a=255), fromCmyk(c, m, y, k, a=255) ๋“ฑ์˜ ์ •์  ๋ฉค๋ฒ„ ํ•จ์ˆ˜(static member function) ์œผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค. QColor red = QColor::fromHsv(0,255,255); QColor red = QColor::fromCmyk(0,255,255,0); ๋˜ํ•œ ๊ฐ๊ฐ์— ๋Œ€ํ•œ ์‹ค์ˆ˜ ๋ฒ„์ „์˜ ํ•จ์ˆ˜์ธ fromHsvF(h, s, v, a=1.), fromCmykF(h, s, l, a=1.)์ด ์žˆ์œผ๋ฉฐ, fromRgb(r, g, b, a=255), fromRgbF(r, g, b, a=1.) ๋“ฑ๋„ ์ œ๊ณต ํ•ฉํ•œ. ํ•œํŽธ, ๊ฐ ์ƒ‰์ƒ ์„ฑ๋ถ„์„ ์ œ๊ณตํ•˜๋Š” ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด RGB ์ƒ‰์ƒ์˜ r ์„ฑ๋ถ„์„ ๊ตฌํ•˜๊ธฐ ์šฐํ•ด์„œ๋Š” ์ •์ˆ˜ ๋ฒ„์ „์˜ red(), ์‹ค์ˆ˜ ๋ฒ„์ „์˜ redF() ๋“ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. QColor์˜ ํ•จ์ˆ˜ ์ค‘์—์„œ๋Š” ์ฃผ์–ด์ง€ ์ƒ‰์ƒ์„ ์กฐ๊ธˆ ๋ฐ๊ฒŒ ๋˜๋Š” ์–ด๋‘ก๊ฒŒ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ligher(),darker() ํ•จ์ˆ˜๊ฐ€ ์œ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘˜ ๋ชจ๋‹ค ์ž์‹  ์ƒ‰์ƒ์˜ ๋ณ€๊ฒฝ ์—†์ด ์ƒˆ๋กœ์šด ์ƒ‰์ƒ์„ ๋ฆฌํ„ดํ•œ๋‹ค. red QColor(255,0,0) lightRed = red.lighter() darkRed = red.darker() ์ƒ‰์ƒ๊ณผ ๊ด€๋ จ๋œ ์ฃผ์ œ๋กœ๋Š” ํŒ”๋ ˆํŠธ(QPalette)๊ฐ€ ์žˆ๋‹ค. ํŒ”๋ ˆํŠธ์—์„œ ์ •์˜ํ•œ ๊ฐ์ข… ์ƒ‰์ƒ์„ ๊ฐ€์ ธ๋‹ค ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์œ„์ ฏ์˜ ๋ฐฐ๊ฒฝ์ƒ‰์€ QPainter์˜ ์†์„ฑ์ด ์•„๋‹ˆ๋ฏ€๋กœ QWidget์„ ์ œ์™ธํ•œ QImage, QPixmap ๋“ฑ์˜ ๊ธฐํƒ€ ํŽ˜์ธํŠธ ๋””๋ฐ”์ด์Šค(QPaintDevice๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค)์—๋Š” ์˜ํ–ฅ์ด ์—†๋‹ค๋Š” ์ ์ด๋‹ค. ๋‹ค์Œ์€ RenderLab ์˜ˆ์ œ์˜ RenderArea.drawGlobalColor()๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ์™€ ์†Œ์Šค์ฝ”๋“œ์ด๋‹ค. RenderArea::drawGlobalColors() def drawGlobalColor(self, device): gcolors = [Qt.white, Qt.black, Qt.cyan, Qt.darkCyan, Qt.red, Qt.darkRed, Qt.magenta, Qt.darkMagenta, Qt.green, Qt.darkGreen, Qt.yellow, Qt.darkYellow, Qt.blue, Qt.darkBlue, Qt.gray, Qt.darkGray, Qt.lightGray] gcolorNames = ["Qt.white", "Qt.black", "Qt.cyan", "Qt.darkCyan", "Qt.red", "Qt.darkRed", "Qt.magenta", "Qt.darkMagenta", "Qt.green", "Qt.darkGreen", "Qt.yellow", "Qt.darkYellow", "Qt.blue", "Qt.darkBlue", "Qt.gray", "Qt.darkGray", "Qt.lightGray"] painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, True) painter.setFont(QFont("Arial",15)) rectTitle = QRect(0,0,4*280+30,50) rect = QRect(0,0,280,50) painter.translate(10,10) # draw title painter.setPen(Qt.NoPen) painter.setBrush(Qt.lightGray) painter.drawRect(rectTitle) painter.setPen(QPen(Qt.black, 1, Qt.SolidLine)) painter.drawText(rectTitle, Qt.AlignCenter,"Qt.GlobalColor enum") painter.translate(0,60) for i in range(17): if i and i%4 == 0: painter.translate(-290*4,60) painter.setPen(QPen(Qt.NoPen)) painter.setBrush(QBrush(gcolors[i])) painter.drawRect(rect) if i%2: painter.setPen(Qt.white) else: painter.setPen(Qt.black) if gcolors[i] == Qt.blue: painter.setPen(Qt.white) painter.drawText(rect, Qt.AlignCenter, gcolorNames[i]) painter.translate(290,0) 6.1.5 ์†์„ฑ - ํŽœ ํŽœ(QPen)์€ ์ƒ‰์ƒ, ํญ, ์„ ์ข…๋ฅ˜, ์„ ์˜ ๋ ๋ชจ์–‘(cap style), ์กฐ์ธ ์œ ํ˜•(join style) ๋“ฑ์˜ ์†์„ฑ์„ ๊ฐ€์ง€๋ฉฐ, ์„ ๊ณผ ๋„ํ˜•์˜ ์œค๊ณฝ์„ ์„ ๊ทธ๋ฆฌ๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ํ…์ŠคํŠธ๋ฅผ ๊ทธ๋ฆด ๋•Œ๋Š”(drawText()) ํŽœ์˜ ์ƒ‰์ƒ ์ •๋ณด๋งŒ ์‚ฌ์šฉ๋œ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์ƒ์„ฑ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. QPen(brush, width, style=Qt.SolidLine, cap=Qt.SquareCap, join=Qt.BevelJoin) QPen(color) ์ฒซ ๋ฒˆ์งธ ์ƒ์„ฑ์ž๊ฐ€ ํŽœ์˜ ์†์„ฑ์„ ๋ชจ๋‘ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ์„ฑ์ž์ด๊ณ , ๋‘ ๋ฒˆ ์žฌ๋Š” ์ƒ‰์ƒ๋งŒ ์ง€์ •ํ•˜๋Š” ํ˜•ํƒœ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ƒ์„ฑ์ž์—์„œ ํŽœ์˜ ์†์„ฑ์œผ๋กœ ๋ธŒ๋Ÿฌ์‹œ(QBrush), ํŽœ ํญ(width), ํŽœ ์Šคํƒ€์ผ(Qt.PenStyle), ํŽœ ์บก ์Šคํƒ€์ผ(Qt.PenCapStyle), ํŽœ ์กฐ์ธ ์Šคํƒ€์ผ(Qt.PenJoinStyle)์ด ์žˆ๋Š” ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์†์„ฑ์œผ๋กœ ์ƒ‰์ƒ(QColor)๊ฐ€ ์•„๋‹Œ ๋ธŒ๋Ÿฌ์‹œ(QBrush)๊ฐ€ ์‚ฌ์šฉ๋˜๋Š”๋ฐ, ๊ทธ ์ด์œ ๋Š” ํŽœ์œผ๋กœ ํ•˜์—ฌ๊ธˆ ๋‹จ์ˆœํ•œ ์„ ์ด ์•„๋‹Œ ์–ด๋–ค ํŒจํ„ด์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. QBrush์˜ ๋‹ค์–‘ํ•œ ์ƒ์„ฑ์ž ์ค‘์—์„œ QColor๋‚˜ Qt.GlobalColor ์—ด๊ฑฐ ๋ณ€์ˆ˜(Qt.red ๋“ฑ)๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์กด์žฌํ•œ๋‹ค. QBrush(color, style=Qt.SolidPattern) ๋”ฐ๋ผ์„œ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ์ƒ์„ฑ์ž๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. painter.setPen(QPen(QColor(255,0,0),0, Qt.DotLine) painter.setPen(QPen(Qt.black, 12, Qt.DashDotLine, Qt.RoundCap)) ์ฆ‰, ํŒจํ„ด์„ ๊ฐ–์ง€ ์•Š๋Š” ํŽœ(์ฆ‰, ์†”๋ฆฌ๋“œ ๋ธŒ๋Ÿฌ์‹œ๋ฅผ ๊ฐ–๋Š” ํŽœ)์„ ์œ„์™€ ๊ฐ™์ด ์ƒ‰์ƒ์„ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ง€์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ์„ฑ์ž์—์„œ ์ง€์ •ํ•˜๋Š” ์ •๋ณด๋Š” setBrush(), setColor(), setPenStyle(), setPenCapStyle() ๋“ฑ์„ ํ†ตํ•ด ๊ฐœ๋ณ„์ ์œผ๋กœ ๊ทธ ์†์„ฑ์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ํŽœ์˜ ํฌ๊ธฐ(width)๋Š” ์ƒ์„ฑ์ž ๋˜๋Š” setWidth(width)์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅํ•˜๋‹ค. ํฌ๊ธฐ๊ฐ€ 0์ธ ํŽœ์„ ๋’ค์—์„œ ๋‹ค๋ฃฐ ์ขŒํ‘œ๊ณ„์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ 1๊ฐœ ํ”ฝ์…€ ํฌ๊ธฐ์˜ ํŽœ์„ ์˜๋ฏธํ•œ๋‹ค. ์ด๋ฅผ ์ฝ”์ฆˆ๋ฉ”ํ‹ฑ ํŽœ(cosmetic pen)์ด๋ผ๊ณ  ํ•œ๋‹ค, ํ™”๋ฉด์— ํ‘œ์‹œ๋˜์ง€ ์•Š๋Š” ํŽœ์€ ํŽœ ์Šคํƒ€์ผ๋กœ Qt.NoPen ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค. ์•„๋ž˜์—์„œ ํŽœ์˜ ์Šคํƒ€์ผ(Qt.PenStyle ์—ด๊ฑฐํ˜•)์— ๋Œ€ํ•œ ๊ทธ๋ฆผ๊ณผ ์ฝ”๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. RenderArea::drawPenStyle() def drawPenStyle(self, device): styles = [Qt.NoPen, Qt.SolidLine, Qt.DashLine, Qt.DotLine, Qt.DashDotLine, Qt.DashDotDotLine] styleNames = ["Qt.NoPen", "Qt.SolidLine", "Qt.DashLine", "Qt.DotLine","Qt.DashDotLine","Qt.DashDotDotLine"] painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, True) painter.setFont(QFont("Arial",15)) rectTitle = QRect(0,0,220*5+10*4,50) rect = QRect(0,0,220,50) painter.translate(10,10) # draw title painter.setPen(Qt.NoPen) painter.setBrush(Qt.lightGray) painter.drawRect(rectTitle) painter.setPen(QPen(Qt.black, 1, Qt.SolidLine)) painter.drawText(rectTitle, Qt.AlignCenter,"Qt::PenStyle enum and pen width") painter.translate(0,60) painter.setBrush(Qt.lightGray) for width in range(4): painter.translate(230,0) painter.setPen(Qt.NoPen) painter.setBrush(Qt.lightGray) painter.drawRect(rect) painter.setPen(Qt.black) painter.drawText(rect, Qt.AlignCenter,"Pen Width={}".format(width)) for style in range(5): painter.translate(-230*4,60) painter.setPen(Qt.NoPen) painter.setBrush(Qt.lightGray) painter.drawRect(rect) painter.setPen(Qt.black) painter.drawText(rect, Qt.AlignCenter, styleNames[style]) for width in range(4): painter.translate(230,0) painter.setPen(QPen(Qt.black, width, styles[style])) painter.drawLine(rect.left(),rect.top()+rect.height()/2, rect.right(),rect.top()+rect.height()/2) ๋‘๊ป˜๊ฐ€ ๋‘๊บผ์šด ํŽœ์˜ ๊ฒฝ์šฐ ์„ ์˜ ๋๋‹จ๊ณผ ์„ ๊ณผ ์„ ์ด ๊บพ์ด๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์†์„ฑ์„ ๊ฐ๊ฐ Qt.PenCapStyle๊ณผ Qt.PenJoinStyle๋กœ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ์ฝ”๋“œ์—์„œ ๊ฐ๊ฐ์˜ ์—ด๊ฑฐ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ๋‚˜์—ดํ•˜์˜€๋‹ค. RenderArea::drawPenCapJoinStyle() def drawPenCapJoinStyle(self, device): penCapStyles = [Qt.FlatCap, Qt.SquareCap, Qt.RoundCap] penCapStyleNames = ["Qt.FlatCap","Qt.SquareCap","Qt.RoundCap"] penJoinStyles = [Qt.MiterJoin, Qt.BevelJoin, Qt.RoundJoin] penJoinStyleNames = ["Qt.MiterJoin","Qt.BevelJoin","Qt.RoundJoin"] painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, True) painter.setFont(QFont("Arial",15)) rectTitle = QRect(0,0,410,50) rect = QRect(0,0,200,50) points = [QPoint(0,15),QPoint(45,0),QPoint(30,45)] painter.save() painter.translate(10,10) # title painter.setPen(Qt.NoPen) painter.setBrush(Qt.lightGray) painter.drawRect(rectTitle) painter.setPen(Qt.black) painter.drawText(rectTitle, Qt.AlignCenter,"Qt::PenCapStyle enum") painter.translate(0,60) for i in range(3): print(i) painter.setPen(Qt.NoPen) painter.setBrush(Qt.lightGray) painter.drawRect(rect) painter.setPen(Qt.black) painter.drawText(rect, Qt.AlignCenter, penCapStyleNames[i]) painter.translate(210,0) painter.setPen(QPen(Qt.red, 10, Qt.SolidLine, penCapStyles[i])) painter.setBrush(Qt.NoBrush) painter.drawLine(rect.left()+20, rect.top()+rect.height()/2, rect.right()-20,rect.top()+rect.height()/2) painter.translate(-210,60) # PenJoinStyle painter.restore() painter.translate(500,10) # title painter.setPen(Qt.NoPen) painter.setBrush(Qt.lightGray) painter.drawRect(rectTitle) painter.setPen(Qt.black) painter.drawText(rectTitle, Qt.AlignCenter,"Qt::PenJoinStyle enum") painter.translate(0,60) for i in range(3): painter.setPen(Qt.NoPen) painter.setBrush(Qt.lightGray) painter.drawRect(rect) painter.setPen(Qt.black) painter.drawText(rect, Qt.AlignCenter, penJoinStyleNames[i]) painter.translate(260,0) painter.setPen(QPen(Qt.red, 10, Qt.SolidLine, Qt.SquareCap, penJoinStyles[i])) painter.setBrush(Qt.NoBrush) painter.drawPolyline(points) painter.translate(-260,60) 6.1.6 ์†์„ฑ - ๋ธŒ๋Ÿฌ์‹œ ๋ธŒ๋Ÿฌ์‹œ๋Š” ๋„ํ˜•์˜ ๋‚ด๋ถ€๋ฅผ ์น ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํŒจํ„ด์ด๋‹ค. ํŽœ์˜ ํŒจํ„ด์„ ๊ทธ๋ฆฌ๋Š” ๋ฐ์—๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์ƒ์„ฑ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. QBrush(color, style=Qt.SolidPattern) # ์ผ๋ฐ˜ ๋ธŒ๋Ÿฌ์‹œ QBrush(pixmap) ๋˜๋Š” QBrush(image) # ํ…์Šค์ฒ˜ ๋ธŒ๋Ÿฌ์‹œ QBrush(gradient) # ๊ทธ ๋ ˆ๋”” ์–ธํŠธ ๋ธŒ๋Ÿฌ์‹œ ์ฒซ ๋ฒˆ์งธ๋Š” ์ƒ‰์ƒ๊ณผ ํ•จ๊ป˜ ๋ฏธ๋ฆฌ ์ •์˜๋œ ํŒจํ„ด์œผ๋กœ ๋ธŒ๋Ÿฌ์‹œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. Qt.BrushStyle ์—ด๊ฑฐ ์ƒ์ˆ˜์ธ style๋กœ Qt.SolidPattern, Qt.Dense1Pattern ๋“ฑ์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ํ”ฝ์Šค ๋งต ๋˜๋Š” QImage ๊ฐ์ฒด๋ฅผ ํ…์Šค์ฒ˜๋กœ ํ™œ์šฉํ•˜๋Š” ๋ธŒ๋Ÿฌ์‹œ์ด๋‹ค. ์ด ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‚ด๋ถ€์ ์œผ๋กœ ๋ธŒ๋Ÿฌ์‹œ ์Šคํƒ€์ผ์ด Qt.TextureBrush๊ฐ€ ์„ค์ •๋œ๋‹ค. ์„ธ ๋ฒˆ์งธ ๊ทธ๋ž˜๋”” ์–ธํŠธ ํŒจํ„ด์„ ํ™œ์šฉํ•˜๋Š” ๋ธŒ๋Ÿฌ์‹œ์ด๋‹ค. ์ด ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ ์šฉ๋˜๋Š” ๊ทธ๋ž˜๋”” ์–ธํŠธ์— ๋”ฐ๋ผ Qt.LinearGradientPattern, Qt.ConicalGradientPattern, Qt.RadialGradientPattern ๋“ฑ์ด ๋ธŒ๋Ÿฌ์‹œ ์Šคํƒ€์ผ๋กœ ์ง€์ •๋œ๋‹ค. ๋‹ค์Œ์€ ๋ธŒ๋Ÿฌ์‹œ ์Šคํƒ€์ธ๊ณผ ๊ด€๋ จ๋œ ๊ทธ๋ฆผ๊ณผ ์†Œ์Šค ์ฝ”๋“œ์ด๋‹ค. RendearArea::drawBrushStyle() def drawBrushStyle(self, device): brushStyles = [Qt.NoBrush, Qt.SolidPattern, Qt.Dense1Pattern, Qt.Dense2Pattern, Qt.Dense3Pattern, Qt.Dense4Pattern, Qt.Dense5Pattern, Qt.Dense6Pattern, Qt.Dense7Pattern, Qt.HorPattern, Qt.VerPattern, Qt.CrossPattern, Qt.BDiagPattern, Qt.FDiagPattern, Qt.DiagCrossPattern, Qt.LinearGradientPattern, Qt.ConicalGradientPattern, Qt.RadialGradientPattern, Qt.TexturePattern] brushStyleNames = ["Qt.NoBrush" , "Qt.SolidPattern" , "Qt.Dense1Pattern" , "Qt.Dense2Pattern", "Qt.Dense3Pattern" , "Qt.Dense4Pattern" , "Qt.Dense5Pattern" , "Qt.Dense6Pattern", "Qt.Dense7Pattern" , "Qt.HorPattern" , "Qt.VerPattern" ,"Qt.CrossPattern", "Qt.BDiagPattern" , "Qt.FDiagPattern" , "Qt.DiagCrossPattern", "Qt.LinearGradientPattern" , "Qt.ConicalGradientPattern" , "Qt.RadialGradientPattern", "Qt.TexturePattern"] painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, True) painter.setFont(QFont("Arial",15)) rectTitle = QRect(0,0,250*4+30,50) rect = QRect(0,0,250,50) painter.translate(10,10) # draw title painter.setPen(Qt.NoPen) painter.setBrush(Qt.lightGray) painter.drawRect(rectTitle) painter.setPen(QPen(Qt.black, 1, Qt.SolidLine)) painter.drawText(rectTitle, Qt.AlignCenter,"Qt.BrushStyle enum") painter.translate(-260,60) # draw color painter.setFont(QFont("Arial",12)) for i in range(len(brushStyles)): if i and i%2 == 0: painter.translate(-260*3,60) else: painter.translate(260,0) painter.setPen(Qt.NoPen) painter.setBrush(Qt.lightGray) painter.drawRect(rect) painter.setPen(Qt.black) painter.drawText(rect, Qt.AlignCenter, brushStyleNames[i]) painter.translate(260,0) painter.setPen(Qt.NoPen) if brushStyles[i] == Qt.LinearGradientPattern: linearGradient = QLinearGradient(rect.topLeft(),rect.bottomRight()) linearGradient.setColorAt(0. ,Qt.white) linearGradient.setColorAt(0.2, Qt.green) linearGradient.setColorAt(1.0, Qt.black) painter.setBrush(linearGradient) elif brushStyles[i] == Qt.ConicalGradientPattern: conicalGradient = QConicalGradient(rect.center(),rect.width()) conicalGradient.setColorAt(0. ,Qt.white) conicalGradient.setColorAt(0.2, Qt.green) conicalGradient.setColorAt(1.0, Qt.black) painter.setBrush(conicalGradient) elif brushStyles[i] == Qt.RadialGradientPattern: radialGradient = QRadialGradient(rect.center(),rect.width()) radialGradient.setColorAt(0. ,Qt.white) radialGradient.setColorAt(0.2, Qt.green) radialGradient.setColorAt(1.0, Qt.black) painter.setBrush(radialGradient) elif brushStyles[i] == Qt.TexturePattern: painter.setBrush(self.pixmapTexture) else: painter.setBrush(QBrush(Qt.darkCyan, brushStyles[i])) painter.drawRect(rect) Qt์—์„œ๋Š” ์„ ํ˜•, ์›ํ˜•, ๋ฐฉ์‚ฌ ํ˜•ํƒœ์˜ ๊ทธ๋ž˜๋”” ์–ธํŠธ๋ฅผ QLinearGradient, QRadialGradient, QConicalGradient ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ง€์›ํ•œ๋‹ค. ์ด๋“ค ํด๋ž˜์Šค๋กœ ์ƒ์„ฑํ•œ ๊ทธ๋ž˜๋”” ์–ธํŠธ ๊ฐ์ฒด๋Š” QBrush๋กœ ์ „๋‹ฌํ•˜๋ฉด ๊ทธ๋ž˜๋”” ์–ธํŠธ๋ฅผ ๊ฐ–๋Š” ๋ธŒ๋Ÿฌ์‹œ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ QLinearGradient๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ธŒ๋Ÿฌ์‹œ์— ์„ค์ •ํ•œ ์˜ˆ์ด๋‹ค. gradient = QLinearGradient(startPoint, endPoint) gradient.setColorAt(0.0, Qt.white) # setAt(r, color) r = 0~1 gradient.setColorAt(0.2, Qt.green) gradient.setColorAt(1.0, Qt.blue) painter.setBrush(gradient) QLinearGradient๋Š” ์‹œ์ž‘์ ๊ณผ ๋์ ์„ ์žŠ๋Š” ์„ ๋ถ„์„ ๊ธฐ์ค€์œผ๋กœ ์„ ํ˜•์ ์œผ๋กœ ๊ทธ๋ž˜๋”” ์–ธํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ƒ์„ฑ์ž์—์„œ ์‹œ์ž‘์ ๊ณผ ๋์ ์„ ์ง€์ •ํ•˜๊ณ , ์„ ๋ถ„ ์‚ฌ์ด์˜ ์ƒ๋Œ€ ๊ธธ์ด r๊ณผ ์ƒ‰์ƒ์„ ์ธ์ž๋กœ ํ•˜๋Š” setColor(r, color)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ r์€ 0~1 ์‚ฌ์ด์˜ ๊ฐ’์ด๋‹ค. QRadialGradient์™€ QConicalGradient๋„ ์ž์‹ ์˜ ์ƒ์„ฑ์ž๋กœ ๊ธฐ์ค€์  ๋“ฑ์˜ ์†์„ฑ์„ ์ง€์ •ํ•˜๊ณ , setColor(r, color)๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ ๊ฐ ๊ทธ๋ž˜๋”” ์–ธํŠธ ํด๋ž˜์Šค์ด ์ƒ์„ฑ์ž์ด๋‹ค. * QLinearGradient : ์‹œ์ž‘์ ๊ณผ ๋์  QLinearGradient(startPoint, finalPoint) QLinearGradient(x1, y1, x2, y2) * QRadialGradient : ์ค‘์‹ฌ๊ณผ ๋ฐ˜์ง€์ , ์ดˆ์  QRadialGradient(centerPoint, radius, focalPoint) QRadialGradient(cx, cy, radius, fx, fy) (3) QConicalGradient : ์ค‘์‹ฌ์ ๊ณผ ๊ฐ QConicalGradient(center, angle) QConicalGradient(cx, cy, angle) ์•„๋ž˜๋Š” RenderArea::drawGradient()์˜ ์‹คํ–‰ ์ „๊ฒฝ๊ณผ ์ฝ”๋“œ์ด๋‹ค. ์—ฌ๊ธฐ์—์„œ ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๋ฅผ ๋ธŒ๋Ÿฌ์‹œ๋กœ ์„ค์ •ํ•˜์—ฌ ์›์„ ๊ทธ๋ ธ์œผ๋ฉฐ, ๋ชจ๋‘ r=0, r=0.5, r=1.์— ๋Œ€์‘ํ•˜๋Š” ์„ธ ๊ฐ€์ง€ ์ƒ‰์ƒ์„ ์ง€์ •ํ•˜์˜€๋‹ค. RenderArea::drawGradient() def drawGradient(self, device): painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, True) painter.translate(100,100) painter.scale(2,2) linearGradient = QLinearGradient(QPointF(0,0),QPointF(100,100)) linearGradient.setColorAt(0, Qt.white) linearGradient.setColorAt(0.5, Qt.blue) linearGradient.setColorAt(1.0, Qt.cyan) painter.setBrush(QBrush(linearGradient)) painter.drawEllipse(QRectF(0,0,100,100)) painter.translate(150,0) radialGradient = QRadialGradient(QPointF(50,50),50, QPoint(30,30)) # center, radius, focalPoint radialGradient.setColorAt(0, Qt.white) radialGradient.setColorAt(0.5, Qt.blue) radialGradient.setColorAt(1.0, Qt.cyan) painter.setBrush(QBrush(radialGradient)) painter.drawEllipse(QRectF(0,0,100,100)) painter.translate(150,0) conicalGradient = QConicalGradient(QPointF(50,50),90) conicalGradient.setColorAt(0, Qt.white) conicalGradient.setColorAt(0.5, Qt.blue) conicalGradient.setColorAt(1.0, Qt.cyan) painter.setBrush(QBrush(conicalGradient)) painter.drawEllipse(QRectF(0,0,100,100)) 6.1.7 ์†์„ฑ - ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋ธŒ๋Ÿฌ์‹œ, ํฐํŠธ, ํด๋ฆฌํ•‘, ๋ถˆํˆฌ๋ช…๋„ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋ธŒ๋Ÿฌ์‹œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋ธŒ๋Ÿฌ์‹œ๋Š” ์ ์„ ์ด๋‚˜ ํ…์ŠคํŠธ, ๋น„ํŠธ๋งต์„ ๊ทธ๋ฆด ๋นˆ ๊ณต๊ฐ„์— ์ฑ„์›Œ์ง€๋Š” ๋ธŒ๋Ÿฌ์‹œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์œ„์ ฏ์˜ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋ฅผ ์น ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ๋จผ์ € QPainter์˜ backgroundMode๋ฅผ Qt.Opacity๋กœ ์„ค์ •ํ•˜๊ณ , setBackgorund(brush)๋กœ ๋ธŒ๋Ÿฌ์‹œ๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ๋””ํดํŠธ๋กœ backgroundMode๋Š” Qt.Transparent์ด๋ฏ€๋กœ, ์ด ๊ฒฝ์šฐ์—๋Š” ์„ค์ •๋œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋ธŒ๋Ÿฌ์‹œ๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. ํฐํŠธ ํฐํŠธ๋Š” drawText()๋กœ ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ๋‹ค์Œ์€ ํฐํŠธ ์ด๋ฆ„(font family), ํฌ์ธํŠธ ํฌ๊ธฐ(point), ๊ฐ•์กฐ(weight), ์ดํƒค๋ฆญ ์—ฌ๋ถ€๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€ํ‘œ์ ์ธ ์ƒ์„ฑ์ž์ด๋‹ค. QFont(familyName, pointSize=-1, weight = -1, italic = False) weight๋Š” 0 - 99 ์‚ฌ์ด์˜ Qt.Light, Qt.Normal, Qt.Bold, Qt.DemiBold, Qt.Black ๋“ฑ์˜ ๋ฏธ๋ฆฌ ์ •์˜๋œ ๊ฐ’์ด ์žˆ๋‹ค. ๋‹ค์Œ์€ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ œ์ด๋‹ค. serifFont = QFont("Times", 10, QFont.Bold); sansFont = QFont("Helvetica [Cronyx]", 12); ๋Œ€ํ‘œ์ ์ธ ํฐํŠธ ์ด๋ฆ„์œผ๋กœ "Times", "Arial" ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ผ์น˜ํ•˜๋Š” ํฐํŠธ๋ฅผ ๋ชป ์ฐพ์„ ๊ฒฝ์šฐ์—๋Š” ์ ์ ˆํ•œ ํฐํŠธ๋ฅผ ์„ ํƒํ•˜๊ฒŒ ๋œ๋‹ค. ํฐํŠธ์˜ ํฌ๊ธฐ๋Š” ํฌ์ธํŠธ๋กœ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ํ”ฝ์…€ ๋‹จ์œ„๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒ์„ฑ์ž์—์„œ๋Š” ํฌ์ธํŠธ ํฌ๊ธฐ๋กœ ๋งŒ ์ง€์ • ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, setPixelSize()๋กœ ํ”ฝ์…€ ๋‹จ์œ„๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. drawText() ํ•จ์ˆ˜๋กœ ํ…์ŠคํŠธ๋ฅผ ๊ทธ๋ฆด ๋•Œ๋Š” ์ƒ‰์ƒ์€ ํŽ˜์ธํ„ฐ์— ์„ค์ •๋œ ํŽœ์˜ ์ƒ‰์ƒ์„ ์ด์šฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํŽœ์˜ ์ƒ‰์ƒ ์ •๋ณด๋ฅผ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ์ •๋ณด๋Š” ์‚ฌ์šฉ๋˜์ง€ ์•Š์œผ๋ฉฐ ํŽ˜์ธํ„ฐ์˜ ๋ธŒ๋Ÿฌ์‹œ ์—ญ์‹œ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. ํด๋ฆฌํ•‘ ์˜์—ญ ํด๋ฆฌํ•‘ ์˜์—ญ(clipping region)์€ ๊ทธ๋ฆฌ๊ธฐ๊ฐ€ ์ˆ˜ํ–‰๋˜๋Š” ์˜์—ญ์„ ์˜๋ฏธํ•œ๋‹ค. ํด๋ฆฌํ•‘ ์˜์—ญ ์™ธ์— ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒƒ์€ ๋ฌด์‹œ๋œ๋‹ค. QPainter์— setClipRegion(region)์ด๋‚˜ setClipRect(rect) ๋“ฑ์˜ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ํด๋ฆฌํ•‘ ์˜์—ญ์„ ์„ค์ •ํ•˜๊ณ , ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๊ทธ ์˜์—ญ ๋‚ด์—์„œ๋งŒ ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. def paint def paintEvent(self, event): r1 = QRegion(QRect(100, 100, 200, 80)) # r1: rectangular region r2 = QRegion(QRect(170, 110, 60, 60),QRegion::Ellipse) # r2: elliptic region r3 = r1.subtracted(r2); # r3: r2-r1 (subtraction) painter=QPainter(self) painter.setClipRegion(r3) ... # paint clipped graphics ์œ„ ์ฝ”๋“œ ์กฐ๊ฐ์—์„œ๋Š” ์˜์—ญ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค์ธ QRegion์„ ๋‘ ๊ฐœ ์ƒ์„ฑํ•œ ํ›„, ํ•œ์ชฝ ์˜์—ญ์—์„œ ๋‹ค๋ฅธ ์˜์—ญ์„ ๋บ€ ์˜์—ญ์„ ์ƒ์„ฑํ•˜๊ณ  ํด๋ฆฌํ•‘ ์˜์—ญ์œผ๋กœ ์„ค์ •ํ–ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ RenderLab ์˜ˆ์ œ์˜ RenderArea::darwClipping()์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋กœ์„œ ํด๋ฆฌํ•‘ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๊ทธ๋ฆฌ๊ธฐ ์˜์—ญ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. paintEvent() ํ•ธ๋“ค๋Ÿฌ์˜ ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” QPaintEvent ๊ฐ์ฒด๋Š” region()์— Qt์˜ ํŽ˜์ธํŠธ ์‹œ์Šคํ…œ ์ฐจ์›์—์„œ ๊ด€๋ฆฌ๋˜๋Š” ํด๋ฆฌํ•‘ ์˜์—ญ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค. ์ด ํด๋ฆฌํ•‘ ์˜์—ญ๊ณผ QPainter.setClipRegion()์œผ๋กœ ์„ค์ •ํ•˜๋Š” ํด๋ฆฌํ•‘ ์˜์—ญ์„ ์„œ๋กœ ๋‹ค๋ฅด๋‹ค. RenderArea::drawClipping() def drawClipping(self, device): r1 = QRegion(QRect(100,100,200,80)) # r1: rectangular region r2 = QRegion(QRect(170, 110, 60, 60),QRegion.Ellipse) # r2: elliptic region r3 = r1.subtracted(r2) # r3: r2-r1 (subtraction) painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, True) painter.setFont(QFont("Arial",15)) painter.save() painter.setPen(Qt.red) for i in range(60): painter.drawLine(0,5*i, 400,5*i) painter.setPen(Qt.black) painter.drawText(QRect(100,100,200,80),Qt.AlignCenter,"unclipped region") painter.restore() painter.translate(400,0) painter.setClipRegion(r3) painter.setPen(Qt.red) for i in range(60): painter.drawLine(0,5*i, 400,5*i) painter.setPen(Qt.black) painter.drawText(QRect(100,100,200,80),Qt.AlignCenter,"clipped region") ๋ถˆํˆฌ๋ช…๋„ QPainter์˜ ์†์„ฑ์œผ๋กœ ๋ถˆํˆฌ๋ช…๋„(opacity)๋ฅผ setOpacity(opacity)๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. opacity๋Š” 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. opacity=1์€ ์™„์ „ํžˆ ๋ถˆํˆฌ๋ช…์ด๋ฉฐ ๋””ํดํŠธ ๊ฐ’์ด๋‹ค. opacity=0๋Š” ์™„์ „ํžˆ ํˆฌ๋ช…์ด๋‹ค. 6.1.8 ์†์„ฑ - ์ขŒํ‘œ๋ณ€ํ™˜ QPainter๋Š” ๋””ํดํŠธ๋กœ ์ ์šฉ๋œ ํŽ˜์ธํŠธ ๋””๋ฐ”์ด์Šค์˜ ์ขŒํ‘œ๊ณ„(๋ณดํ†ต ํ”ฝ์…€ ๋‹จ์œ„)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค์–‘ํ•œ ์ขŒํ‘œ๋ณ€ํ™˜(coordinate transformation)์„ ์ง€์›ํ•œ๋‹ค. QPainter์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ขŒํ‘œ๊ณ„๋กœ๋Š” ์žฅ์น˜(device), ์œˆ๋„(window), ์„ธ๊ณ„(world) ์ขŒํ‘œ๊ณ„๊ฐ€ ์žˆ๋‹ค. QPainter๋กœ ๊ทธ๋ฆฐ ๋ฌผ์ฒด๋Š” ์„ธ๊ณ„ ์ขŒํ‘œ๊ณ„๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•œ๋‹ค. ์ดํ›„ ๋ณ€ํ™˜ ํ–‰๋ ฌ(transformation matrix)์„ ํ†ตํ•ด ์œˆ๋„ ์ขŒํ‘œ๊ณ„๋กœ ๋ณ€ํ™˜๋˜๊ณ , ์„ค์ •๋œ ๋ทฐํฌํŠธ(viewport), ์œˆ๋„(window) ์‚ฌ๊ฐํ˜• ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค์‹œ ์žฅ์น˜ ์ขŒํ‘œ๊ณ„๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ๋””ํดํŠธ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ ํ–‰๋ ฌ์€ ๋‹จ์œ„ํ–‰๋ ฌ์ด๊ณ , ์œˆ๋„์™€ ๋ทฐํฌํŠธ ์‚ฌ๊ฐํ˜•์€ ์œ„์ ฏ์˜ ํด๋ผ์ด์–ธํŠธ ์˜์—ญ์ด ๋œ๋‹ค. ์ฆ‰ ๋””ํดํŠธ๋กœ ์ขŒ์ธก ์ƒ๋‹จ์ด ์›์  (0,0)์ด๊ณ , ์˜ค๋ฅธ์ชฝ์œผ๋กœ +x, ์•„๋ž˜์ชฝ์œผ๋กœ +y์ธ ํ”ฝ์…€ ๋‹จ์œ„์ธ ์žฅ์น˜ ์ขŒํ‘œ๊ณ„์— ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋œ๋‹ค. ์œˆ๋„ ์ขŒํ‘œ๊ณ„๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์€ setWindow(x, y, w, h)์™€ setViewport(x, y, w, h)๋กœ ํ™”๋ฉด์ƒ์— ์œˆ๋„ ๋ฐ ๋ทฐํฌํŠธ ์‚ฌ๊ฐํ˜•์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค. ์œˆ๋„ ์‚ฌ๊ฐํ˜•์€ ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋…ผ๋ฆฌ ์ขŒํ‘œ๊ณ„์ธ ์œˆ๋„ ์ขŒํ‘œ๊ณ„๋กœ ์ง€์ •๋˜๋Š” ์‚ฌ๊ฐํ˜•์ด๊ณ , ๋ทฐํฌํŠธ ์‚ฌ๊ฐํ˜•์ด ์œˆ๋„ ์‚ฌ๊ฐํ˜•๊ณผ ๋™์ผํ•œ ์˜์—ญ์„<NAME>๋Š” ๋””๋ฐ”์ด์Šค ์ขŒํ‘œ๊ณ„ ์ƒ์˜ ์‚ฌ๊ฐ์˜์—ญ์„ ์˜๋ฏธํ•œ๋‹ค. ์ด ๋‘ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋‹ค์–‘ํ•œ ์ขŒํ‘œ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ •์ˆ˜ํ˜• ์ธ์ž๋งŒ์„ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์ œํ•œ์ ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ๋ณ€ํ™˜ ํ–‰๋ ฌ์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋ณด๋‹ค ํšจ์œจ์ ์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์—์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๋‹ค. ์ขŒํ‘œ๋ณ€ํ™˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์ถ”์ „๋˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ณ„๋„์˜ ์œˆ๋„ ์ขŒํ‘œ๊ณ„๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š๊ณ (์ฆ‰, ๋””ํดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ), ๋ณ€ํ™˜ ํ–‰๋ ฌ์„ ์ด์šฉํ•ด ์ขŒํ‘œ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. QPainter๋กœ ๊ทธ๋ฆด ๋ฌผ์ฒด๋ฅผ ์ž„์˜์˜ ์ขŒํ‘œ๊ณ„์—์„œ ๊ทธ๋ฆฐ ํ›„ ๋ณ€ํ™˜ ํ–‰๋ ฌ์„ ํ†ตํ•ด ์žฅ์น˜ ์ขŒํ‘œ๊ณ„๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค(์—ฌ๊ธฐ์—์„œ๋Š” ์œˆ๋„ ์ขŒํ‘œ๊ณ„์™€ ์žฅ์น˜ ์ขŒํ‘œ๊ณ„๊ฐ€ ๋™์ผํ•œ ์ขŒํ‘œ๊ณ„๋กœ ๊ฐ€์ •). ๋ณ€ํ™˜ ํ–‰๋ ฌ์€ QTransform ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ QPainter.setTransform(T)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„ค์ •ํ•œ๋‹ค. QPainter ๋‚ด๋ถ€์—๋Š” ํ•œ ๊ฐœ์˜ ๋ณ€ํ™˜ ํ–‰๋ ฌ๋งŒ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์ดˆ๊ธฐ์—๋Š” ๋‹จ์œ„ํ–‰๋ ฌ์ด๋‹ค. setTransform(T)์„ ์—ฐ์†์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋ฉด ๋ณ€ํ™˜์ด ์ค‘์ฒฉ๋˜๊ฒŒ ๋œ๋‹ค. T1 = QTransform(...) T2 = QTransform(...) T3 = QTransform(...) painter.setTransform(T1) painter.setTransform(T2) painter.setTransform(T3; ... T = T3*T2*T1์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š” ๋ณ€ํ™˜์ด ์ ์šฉ ... deviceCoordinate = T*localCoordinate ํ˜•ํƒœ์˜ ๋ณ€ํ™˜ ์ ์šฉ QTransform์€ * ์—ฐ์‚ฐ์ž๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™˜ ํ–‰๋ ฌ์„ ๊ตฌ์„ฑํ•œ ํ›„ ํ•œ๊บผ๋ฒˆ์— ์„ค์ •ํ•˜๋Š” ๊ฒƒ ์—ญ์‹œ ๊ฐ€๋Šฅํ•˜๋‹ค. T1 = QTransform(...) T2 = QTransform(...) T3 = QTransform(...) T = T3*T2*T1 painter.setTransform(T) ... T = T3*T2*T1์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š” ๋ณ€ํ™˜์ด ์ ์šฉ ... deviceCoordinate = T*localCoordinate ํ˜•ํƒœ์˜ ๋ณ€ํ™˜ ์ ์šฉ QPainter์— ์„ค์ •๋œ ๋ณ€ํ™˜ ํ–‰๋ ฌ์€ resetTranform()์„ ํ˜ธ์ถœํ•˜์—ฌ ๋‹จ์œ„ํ–‰๋ ฌ๋กœ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. QTransform ํด๋ž˜์Šค๋Š” 3x3 ํ–‰๋ ฌ์„ ์ €์žฅํ•˜๊ณ , (x, y, w) ํ˜•ํƒœ์˜ 2์ฐจ์› ๋™์ฐจ ์ขŒํ‘œ๊ณ„(homogeneous)์— ๋Œ€ํ•œ ๋ณ€ํ™˜์„ ์ •์˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ QTransform ํด๋ž˜์Šค๋ฅผ ์ง์ ‘ ์กฐ์ž‘ํ•˜๋ ค๋ฉด ๊นŒ๋‹ค๋กญ๋‹ค. ๋Œ€์‹  ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ๋ณ€ํ™˜์ธ ํ‰ํ–‰, ํšŒ์ „, ํฌ๊ธฐ, ์ „๋‹จ ๋ณ€ํ™˜์„ translate(dx, dx), rotate(angle), scale(sx, sy), shear(sh, sv) ๋“ฑ์˜ ํŽธ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•˜๋‹ค. โ–ช ๋ฐฉ๋ฒ• 1 : QPainter์˜ ํŽธ์˜ ํ•จ์ˆ˜ ์‚ฌ์šฉ painter = QPainter(self) # T = I ... indentity matrix transform.translate(50,50) # T = (translation)*T = translation transform.rotate(45.0) # T = (rotation)*T = rotation*translation transform.scale(0.5,1.0) # T = (scale)*T = scale*rotation*translation โ–ช ๋ฐฉ๋ฒ• 2: QTransform์˜ ํŽธ์˜ ํ•จ์ˆ˜ ์‚ฌ์šฉ ํ›„ setTransform() ํ˜ธ์ถœ painter = QPainter(this) # T = I ... indentity matrix transform = QTransform() transform.translate(50,50) # T = (translation)*T = translation transform.rotate(45.0) # T = (rotation)*T = rotation*translation transform.scale(0.5,1.0) # T = (scale)*T = scale*rotation*translation painter.setTransform(transform) ... T = (scale)*(rotation)*(translation)์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š” ๋ณ€ํ™˜์ด ์ ์šฉ ... deviceCoordinate = T*localCoordinate ํ˜•ํƒœ์˜ ๋ณ€ํ™˜ ์ ์šฉ ์•„๋ž˜ ๊ทธ๋ฆผ์€ ๋„ค ๊ฐ€์ง€ ํŽธ์˜ ํ•จ์ˆ˜์—์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ณ€ํ™˜์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆผ์—์„œ ํ–‰๋ ฌ T๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” QTransform์˜ ๋ณ€ํ™˜ ํ–‰๋ ฌ์ด๋‹ค. ํ•œํŽธ, QTransform ํด๋ž˜์Šค๋Š” map(), mapRect(), mapToPolygon() ๋“ฑ๊ณผ ๊ฐ™์ด ์ง์ ‘ ์ขŒํ‘œ๋ณ€ํ™˜์„ ํŽธ์˜ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค. point = transform.map(QPointF(0,0)) rect = transform.mapRect(QRectF(0,0,1,1)) # shear, rotation์ด ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด # bounding rect๋กœ ๋ณ€ํ™˜ poygon = transform.mapToPolygon(QRectF(0,0,1,1)) ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ์ฝ”๋“œ๋Š” RenderLab ์˜ˆ์ œ์˜ RenderArea.drawTransform()์„ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ์™€ ์ฝ”๋“œ์ด๋‹ค. ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ๊ฝƒ์„ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•ด ํ•ด ๋จผ์ € ์ž„์˜๋กœ ์„ค์ •ํ•œ ์ขŒํ‘œ๊ณ„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฝƒ์žŽ ํ•˜๋‚˜๋ฅผ ์ƒ์„ฑํ•ด ๋‘”๋‹ค. ์ดํ›„ ํ‰ํ–‰์ด๋™, ํฌ๊ธฐ ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์ค‘์‹ฌ ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•˜๊ณ , ์ดํ›„ ํšŒ์ „ ๋ณ€ํ™˜์œผ๋กœ 8๊ฐœ์˜ ๊ฝƒ์žŽ์„ ๊ฐ–๋Š” ๊ฝƒ์„ ์™„์„ฑํ•˜์˜€๋‹ค. . RenderArea.drawTransform() def drawTransform(self, device): painter = QPainter(device) painter.setRenderHint(QPainter.Antialiasing, True) floralLeaf = QPainterPath() floralLeaf.cubicTo(-30,100, +30,100,0,0); gradient = QRadialGradient(QPointF(0,50),50, QPointF(0,50)) gradient.setColorAt(0.,Qt.white) gradient.setColorAt(0.5, Qt.red) gradient.setColorAt(1.,Qt.white) painter.setBrush(gradient) painter.setPen(Qt.NoPen) painter.translate(200,200) painter.scale(2,2) for i in range(8): painter.drawPath(floralLeaf) painter.rotate(45) 6.1.9 ์†์„ฑ - ๊ธฐํƒ€ save()์™€ restore() QPainter์˜ save()์™€ restore() ํ˜ธ์ถœ์„ ํ†ตํ•ด ํ˜„ ์ƒํƒœ์˜ ์†์„ฑ์„ ์ €์žฅํ•˜๊ณ  ๋‹ค์‹œ ๋˜๋Œ๋ฆด ์ˆ˜ ์žˆ๋‹ค. painter = QPainter(self) ...์†์„ฑ ์„ค์ • painter.save(); ... ์†์„ฑ ์„ค์ • ๋ฐ ๊ทธ๋ฆฌ๊ธฐ painter.restore() ... Composition ์—ฌ๊ธฐ์—์„œ๋Š” ์†Œ๊ฐœํ•˜์ง€ ์•Š์Œ. 6.2 AnalogClock ์˜ˆ์ œ ๋ถ„์„ ์ด๋ฒˆ ์ ˆ์—์„œ๋Š” ์„ค๋ช…์„ ๋ฏธ๋ค„์™”๋˜ AnalogClock์˜ paintEvent() ํ•ธ๋“ค๋Ÿฌ์˜ ์†Œ์Šค์ฝ”๋“œ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•œ๋‹ค. ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์†Œ๊ฐœํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. class AnalogClock(QWidget): ... def paintEvent(self, event): hourHand = [QPoint(7,8), QPoint(-7,8), QPoint(0, -40)] minuteHand = [QPoint(7,8), QPoint(-7,8), QPoint(0, -70)] secondHand = [QPoint(0,8),QPoint(0, -80)] hourColor = QColor(127,0,127) minuteColor = QColor(0,127,127,191) secondColor = QColor(255,0,0,191) side = min(self.width(),self.height()) time = QTime.currentTime() # static function time = time.addSecs(self.timeZoneOffset) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) painter.translate(self.width()/2, self.height()/2) painter.scale(side/200.0, side/200.0) # Draw circle radialGradient = QRadialGradient(0,0,100, -40, -40) # center, radius, focalPoint radialGradient.setColorAt(0.0, Qt.white) radialGradient.setColorAt(1.,self.backgroundColor) painter.setBrush(radialGradient) painter.setPen(QPen(Qt.darkGray, 0)) # darkGray cosmetic pen painter.drawEllipse(QRectF(-97, -97,194,194)) # Draw minute tick painter.setPen(minuteColor) for j in range(60): if (j % 5) != 0: painter.drawLine(92,0,96,0) painter.rotate(6.0) # draw hour hand painter.setPen(Qt.NoPen) painter.setBrush(hourColor) painter.save() painter.rotate(30.0*((time.hour()+time.minute()/60.0))) painter.drawConvexPolygon(hourHand) painter.restore() # draw hour tick painter.setPen(hourColor) for i in range(12): painter.drawLine(88,0,96,0) painter.rotate(30.0) # draw mimute hand painter.setPen(Qt.NoPen) painter.setBrush(minuteColor) painter.save() painter.rotate(6.8*(time.minute()+time.second()/60.0)) painter.drawConvexPolygon(minuteHand) painter.restore() # Draw second hand painter.setPen(secondColor) painter.save() painter.rotate(6.0*time.second()) painter.drawLine(secondHand[0],secondHand[1]) painter.restore() self.updated.emit(time) ์ž„์˜์˜ ์ขŒํ‘œ๊ณ„๋กœ ์‹œ์นจ, ๋ถ„์นจ, ์ดˆ์นจ์„ ์ •์˜ํ•˜๊ณ , ์‚ฌ์šฉํ•  ์ปฌ๋Ÿฌ๋ฅผ ์ •์˜ํ•œ๋‹ค. ์œ„์ ฏ์˜ ์ตœ์†Œ ๋ณ€์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๊ณ , ํ˜„์žฌ ์‹œ๊ฐ„์„ ์‹œ๊ฐ„๋Œ€(time zone)์„ ๊ณ ๋ คํ•ด ๊ตฌํ•œ๋‹ค. ์œ„์ ฏ์˜ ์ค‘์‹ฌ์œผ๋กœ ์ขŒํ‘œ๊ณ„๋ฅผ ํ‰ํ–‰์ด๋™ํ•˜๊ณ , ์œ„์ ฏ์˜ ํฌ๊ธฐ์— ๋งž์ถ”์–ด ํฌ๊ธฐ ๋ณ€ํ™˜์„ ํ•œ๋‹ค. ์ด๋•Œ ์‹œ๊ณ„์˜ ํฌ๊ธฐ๋Š” 200์„ ๊ฐ€์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•ž์„œ ์‹œ์นจ, ๋ถ„์นจ, ์ดˆ์นจ์˜ ์ขŒํ‘œ ์—ญ์‹œ ์ด ํฌ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์„ค์ •ํ•œ ๊ฒƒ์ด๋‹ค. AnalogClock์— ์›ํ˜•์˜ ๋ฐฐ๊ฒฝ์„ ๊ทธ๋ž˜๋”” ์–ธํŠธ๋กœ ๊ทธ๋ฆฐ๋‹ค. ์› ์™ธ๋ถ€์— ๋ถ„์„ ํ‘œ์‹œํ•˜๋Š” ํ‹ฑ(minute tick)์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค. ์ด๋•Œ 6๋„์”ฉ ํšŒ์ „ ๋ณ€ํ™˜์„ ์‚ฌ์šฉํ•œ๋‹ค. ํ˜„์žฌ ์‹œ๊ฐ„์— ๋งž์ถ”์–ด ํšŒ์ „ ๋ณ€ํ™˜์„ ์ ์šฉํ•˜์—ฌ ์‹œ์นจ์„ ๊ทธ๋ฆฐ๋‹ค. ๊ทธ๋ฆฌ๊ธฐ ์ „ํ›„๋กœ save()์™€ restore()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ณ€ํ™˜์„ ํฌํ•จํ•œ ์†์„ฑ์„ ๋‹ค์‹œ ํ™˜์›ํ•œ๋‹ค. ์› ์™ธ๋ถ€์— ์‹œ์„ ํ‘œ์‹œํ•˜๋Š” ํ‹ฑ(hour tick)์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค. ์ด๋•Œ 30๋„์”ฉ ํšŒ์ „ ๋ณ€ํ™˜์„ ์‚ฌ์šฉํ•œ๋‹ค. ํ˜„์žฌ ์‹œ๊ฐ„์— ๋งž์ถ”์–ด ํšŒ์ „ ๋ณ€ํ™˜์„ ์ ์šฉํ•˜์—ฌ ๋ถ„์นจ๊ณผ ์ดˆ์นจ์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ธฐ ์ „ํ›„๋กœ save()์™€ restore()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ณ€ํ™˜์„ ํฌํ•จํ•œ ์†์„ฑ์„ ๋‹ค์‹œ ํ™˜์›ํ•œ๋‹ค. updated(QTime) ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. 7. ์•„์ดํ…œ ๋ทฐ ์•„์ดํ…œ ๋ทฐ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ๋ชฉ์š”์—ฐํ•˜๊ฒŒ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ๋ฆฌ์ŠคํŠธ, ํ…Œ์ด๋ธ”, ํŠธ๋ฆฌ ๋“ฑ์ด๋‹ค. Qt์—์„œ ์ด๋ฅผ ์œ„ํ•ด ์•„์ดํ…œ ๋ทฐ ํด๋ž˜์Šค(item view class)์ธ QListView, QTableView, QTreeView๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด๋“ค ํด๋ž˜์Šค๋Š” GUI ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ๋Š”๋ฐ ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” MVC(๋ชจ๋ธ-๋ทฐ-์ปจํŠธ๋กค๋Ÿฌ) ๋””์ž์ธํŒจํ„ด๋ฅผ ๋ณ€ํ˜•ํ•œ ๋ชจ๋ธ-๋ทฐ ์•„ํ‚คํ…์ฒ˜๋กœ ์„ค๊ณ„๋˜์–ด ์žˆ์œผ๋ฉฐ, QAbstractItemView๋ฅผ ๋ถ€๋ชจ ํด๋ž˜์Šค๋กœ ํ•˜๊ณ  ์žˆ๋‹ค. ์•„์ดํ…œ ๋ทฐ ํด๋ž˜์Šค์˜ ๊ฐ„ํŽธํ™”๋œ ๋ฒ„์ „์œผ๋กœ QTableWidget, QListWidget, QTreeWidget ๋“ฑ์ด ์žˆ๋Š” ๋ฐ ์ด๋“ค์€ ๋ชจ๋ธ-๋ทฐ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํŽธ์˜ ์œ„์ ฏ์œผ๋กœ ๋ณดํ†ต ์•„์ดํ…œ ๋ทฐ ํŽธ์˜ ํด๋ž˜์Šค(item view convience class)๋กœ ๋ถˆ๋ฆฐ๋‹ค. ์ด๋•Œ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋ธ์ด ์•„๋‹Œ ๊ฐœ๋ณ„ ์•„์ดํ…œ์œผ๋กœ ์ง€์ •๋œ๋‹ค. ์•„์ดํ…œ ๋ทฐ ํด๋ž˜์Šค๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์ƒ์†๊ด€๊ณ„๋ฅผ ์ด๋ฃจ๋ฉฐ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋Š” QAbstractItemView์ด๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ๋ชจ๋ธ-๋ทฐ ์•„ํ‚คํ…์ฒ˜์˜ ์„ค๋ช…์„ ์ตœ์†Œํ™”ํ•˜๊ณ  ์•„์ดํ…œ ๋ทฐ ํŽธ์˜ ํด๋ž˜์Šค(item view convience class) ์œ„์ฃผ๋กœ ์„ค๋ช…ํ•œ๋‹ค. ๋ชจ๋ธ-๋ทฐ ๊ตฌ์กฐ ๋ง›๋ณด๊ธฐ : FileSystem ์˜ˆ์ œ ์ด ์ฑ…์—์„œ QListView, QTableView, QTreeView๋ฅผ ๋‹ค๋ฃจ์ง€ ์•Š์ง€๋งŒ ๋‹ค์Œ์˜ ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ํ†ตํ•ด ์•„์ดํ…œ ๋ทฐ ํด๋ž˜์Šค์˜ ์ž‘๋™ ๋ฐฉ์‹์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์ œ๋Š” ๊ฐ ์•„์ดํ…œ ๋ทฐ์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ(์—ฌ๊ธฐ์„œ๋Š” ํŒŒ์ผ์‹œ์Šคํ…œ์— ๋Œ€ํ•œ ๋ฏธ๋ฆฌ ์ •์˜๋˜์–ด ์žˆ๋Š” ๋ชจ๋ธ์ด๋‹ค)์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์™ธ๊ด€๋งŒ ๊ฐ ์•„์ดํ…œ ๋ทฐ ํด๋ž˜์Šค ํŠน์„ฑ์— ๋”ฐ๋ผ ํ‘œ์‹œ๋˜๊ฒŒ ๋œ๋‹ค. FileSystem.py from PySide2.QtWidgets import (QApplication, QSplitter, QFileSystemModel, QTreeView, QListView, QTableView) from PySide2.QtCore import QDir import sys if __name__ == '__main__': app = QApplication(sys.argv) splitter = QSplitter() model = QFileSystemModel(app) model.setRootPath(QDir.currentPath()) treeView = QTreeView(splitter) treeView.setModel(model) treeView.setRootIndex(model.index(QDir.currentPath())) listView = QListView(splitter) listView.setModel(model) listView.setRootIndex(model.index(QDir.currentPath())) tableView = QTableView(splitter) tableView.setModel(model) tableView.setRootIndex(model.index(QDir.currentPath())) splitter.setWindowTitle("Three views onto the same file system model") splitter.show() app.exec_() 7.1 ๊ณตํ†ต ์†์„ฑ QAbstractItemView์—๋Š” ๋ชจ๋“  ์•„์ดํ…œ ๋ทฐ์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ช‡๋ช‡ ์†์„ฑ์ด ์žˆ๋‹ค. ์—๋””ํŠธ ํŠธ๋ฆฌ๊ฑฐ(edit trigger) ์—๋””ํŠธ ํŠธ๋ฆฌ๊ฑฐ๋Š” ์•„์ดํ…œ ๋ทฐ ์œ„์ ฏ์˜ ์•„์ดํ…œ์„ ์„ ํƒํ•œ ํ•œ ํ›„ ์–ด๋–ค ํ–‰์œ„๋กœ ํŽธ์ง‘๋ชจ๋“œ๋กœ ๋“ค์–ด๊ฐˆ ๊ฑด์ง€๋ฅผ ์˜๋ฏธํ•œ๋‹ค. editTriggers()์™€ setEditTriggers(triggers)๋กœ ๊ฐ’์„ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. triggers๋Š” QAbstractItemView.EditTriger ์—ด๊ฑฐ ์ƒ์ˆ˜์˜ ์กฐํ•ฉ์ด๋ฉฐ ์ด ์—ด๊ฑฐ ์ƒ์ˆ˜๋Š” ๋‹ค์Œ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. QAbstractItemView.NoEditTriggers : ํŽธ์ง‘ ๋ถˆ๊ฐ€ QAbstractItemView.CurrentChanged : ํ˜„์žฌ ์•„์ดํ…œ์ด ๋ฐ”๋€” ๋•Œ QAbstractItemView.DoubleClicked : ์•„์ดํ…œ ๋”๋ธ”ํด๋ฆญ ์‹œ QAbstractItemView.SelectedClicked : ์ด๋ฏธ ์„ ํƒ๋˜์–ด ์žˆ๋Š” ์•„์ดํ…œ์„ ํด๋ฆฌ ํ•  ๋•Œ QAbstractItemView.EditKeyPressed : ์—๋””ํŠธ ํ‚ค๊ฐ€ ๋ˆŒ๋ฆด ๋•Œ QAbstractItemView.AnyKeyPressed : ์„ ํƒ๋œ ์•„์ดํ…œ์— ๋Œ€ํ•ด ์•„๋ฌด ํ‚ค๊ฐ€ ๋ˆŒ๋ฆด ๋•Œ ๋””ํดํŠธ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋ฆฌ์ŠคํŠธ ๋ทฐ์™€ ํŠธ๋ฆฌ๋ทฐ(QListView, QListWidget, QTreeView, QTreeWidget) : DoubleClicked|EditKeyPressed ํ…Œ์ด๋ธ” ๋ทฐ(QTableView, QTableWidget) : DoubleClicked|EditKeyPressed|AnyKeyPressed ์•„์ดํ…œ ๋ทฐ๊ฐ€ ํŽธ์ง‘ ๊ฐ€๋Šฅํ•˜๋ ค๋ฉด NoEditTriggers๋ฅผ ์ œ์™ธํ•œ editTriggers๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๊ณ , ์—ฐ๊ฒฐ๋œ ๋ชจ๋ธ์ด๋‚˜ ์•„์ดํ…œ์ด ํŽธ์ง‘ ๊ฐ€๋Šฅํ•ด์•ผ ํ•œ๋‹ค. ์„ ํƒ ๋ชจ๋“œ ์„ ํƒ ๋ชจ๋“œ๋Š” ๋งˆ์šฐ์Šค๋‚˜ ํ‚ค๋ณด๋“œ ๋“ฑ์œผ๋กœ ์•„์ดํ…œ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ์‹์„ ์ง€์ •ํ•œ๋‹ค. selectionMode()์™€ setSelectionMode(mode)๋กœ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. mode๋Š” QAbstractItemView.SelectionMode ์—ด๊ฑฐ ์ƒ์ˆ˜๋กœ ๋‹ค์Œ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. QAbstrationItemView.NoSelection : ์„ ํƒ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ์„ค์ • QAbstractionItemView.SingleSelection : ํ•˜๋‚˜์˜ ์•„์ดํ…œ๋งŒ์„ ์„ ํƒ ๊ฐ€๋Šฅ QAbstractionItemView.ContiguousSection : ์—ฐ์†๋œ ๋ธ”๋ก์œผ๋กœ ์„ ํƒ ๊ฐ€๋Šฅ QAbstractionItemView.ExtendedSelection : Shift ํ‚ค์˜ ์กฐํ•ฉ ์—ฐ์†๋œ ๋ธ”๋ก์„ ํ™•์žฅ ๊ฐ€๋Šฅํ•˜๊ณ , Ctrl ํ‚ค ์กฐํ•ฉ์œผ๋กœ ๋‹ค์ค‘ ๋ธ”๋ก ์„ ํƒ ๊ฐ€๋Šฅ ๋””ํดํŠธ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋ฆฌ์ŠคํŠธ ๋ทฐ์™€ ํŠธ๋ฆฌ๋ทฐ(QListView, QListWidget, QTreeView, QTreeWidget) : SingleSelection ํ…Œ์ด๋ธ” ๋ทฐ(QTableView, QTableWidget) : ExtendedSelection ์•„์ดํ…œ ๋ทฐ์—์„œ ์•„์ดํ…œ์ด ์„ ํƒ๋˜๋ ค๋ฉด NoSelection์ด ์•„๋‹Œ ๋ชจ๋“œ๋กœ ์ง€์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๊ณ , ์—ฐ๊ฒฐ๋œ ๋ชจ๋ธ์ด๋‚˜ ์•„์ดํ…œ์˜ ์†์„ฑ๊ฐ’์ด ์„ ํƒ ๊ฐ€๋Šฅํ•˜๋„๋ก ์ง€์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. alternatingRowColor QAbstractItemView๋Š” ๋ทฐ์˜ ์™ธ๊ด€๊ณผ ๊ด€๋ จํ•ด ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” setAlternatingRowColor(bool) ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด ํ•จ์ˆ˜๋กœ alternatingRowColor ๋ณ€์ˆ˜๋ฅผ True๋กœ ์„ค์ •ํ•˜๋ฉด ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ํ–‰๊ณผ ํ–‰์ด ์„œ๋กœ ๋‹ค๋ฅธ ์ƒ‰์ด ๋ฒˆ๊ฐˆ์•„ ๋‚˜์˜ค๊ฒŒ ๋˜์–ด ๋ณด๊ธฐ ์ข‹๊ฒŒ ๋œ๋‹ค. QHeaderView ํ…Œ์ด๋ธ” ์œ„์ ฏ(QTableView, QTableWidget)์ด๋‚˜ ํŠธ๋ฆฌ ์œ„์ ฏ(QTreeView, QTreeWidget)์€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ํ—ค๋” ๋ถ€๋ถ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ํ—ค๋” ๋ถ€๋ถ„์„ ํ‘œ์‹œํ•˜๋Š” ์œ„์ ฏ์ด QHeaderView์œผ๋กœ ์ด ์œ„์ ฏ์€ ํ…Œ์ด๋ธ”์ด๋‚˜ ํŠธ๋ฆฌ ์œ„์ ฏ์„ ์ƒ์„ฑํ•  ๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์„ฑ๋˜์–ด ํ‘œ์‹œ๋œ๋‹ค. ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ ์ด QHeaderView๋ฅผ ์ œ์–ดํ•ด์„œ ๋ณด๋‹ค ๋ณด๊ธฐ ์ข‹๊ฒŒ ๊พธ๋ฐ€ ํ•„์š”๊ฐ€ ์žˆ๋‹ค. QHeaderView๋Š” QTableView.horizontalHeader(), QTableView.verticalHeader (), QTreeView.header() ๋“ฑ์œผ๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ํ…Œ์ด๋ธ”์˜ ํ–‰ ๋˜๋Š” ์—ด, ํŠธ๋ฆฌ์˜ ์—ด์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์„ ์„น์…˜(section)์ด๋ผ๊ณ  ํ•˜๋Š” ๋ฐ, ์„น์…˜์˜ ResizeMode๋ฅผ QHeaderView์˜ setSectionResizeMode(mode)์„ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•œ๋‹ค. table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) setSectionResizeMode(index, mode)๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. index๋Š” ๋ช‡ ๋ฒˆ์งธ ์„น์…˜์ธ์ง€๋ฅผ ์ง€์ •ํ•˜๋Š” ์ธ์ž์ด๊ณ , mode๋Š” QHeaderView::ResizeMode ์—ด๊ฑฐ ์ƒ์ˆ˜๋กœ ๋‹ค์Œ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค. QHeaderView.Interactive - ์‚ฌ์šฉ์ž๊ฐ€ ์„น์…˜ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Œ. defaultSectionSize๊ฐ€ ๊ธฐ๋ณธ ํฌ๊ธฐ QHeaderView.Fixed - ๊ณ ์ •๋œ ์„น์…˜ ํฌ๊ธฐ. defaultSectionSize๊ฐ€ ๊ธฐ๋ณธ ํฌ๊ธฐ QHeaderView.Stretch - ๊ฐ€๋Šฅํ•œ ๊ณต๊ฐ„์œผ๋กœ ์ž๋™์œผ๋กœ ์„น์…˜ ํฌ๊ธฐ ์กฐ์ • QHeaderView.ResizeToContents : ๋‚ด์šฉ์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ์„น์…˜ ํฌ๊ธฐ ์กฐ์ • ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” QHeaderView::Interactive๋‚˜ QHeaderView::Stretch์ด๋‹ค. ํŠนํžˆ ํ—ค๋”์˜ ์„น์…˜ ์ˆ˜๊ฐ€ ์ž‘์„ ๊ฒฝ์šฐ์—๋Š” QHeaderView.Stretch๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ ์ˆ˜ํ‰๋ฐฉํ–ฅ QHeaderView๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์•„๋ž˜ ์ฝ”๋“œ๋กœ QHeaderView.Stretch๋ฅผ ์ˆ˜ํ‰๋ฐฉํ–ฅ ํ—ค๋” ๋ทฐ์— ์ ์šฉ ์ „ํ›„์˜ ๋ชจ์Šต์ด๋‹ค. tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) ํ•œํŽธ ๋””ํดํŠธ๋กœ ํ•ญ์ƒ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜๋Š” ํ—ค๋”๋ฅผ QWiget.hide()๋‚˜ QWidget.setVisibility(False)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ™”๋ฉด์ƒ์—์„œ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. tableView.verticalHeader().setVisibility(False); ๋“œ๋ž˜๊ทธ ๋“œ๋กญ ๊ด€๋ จ ๋“œ๋ž˜๊ทธ ๋“œ๋กญ๊ณผ ๊ด€๋ จ๋œ dragDropMode, dragEnabled, defaultDropAction ๋“ฑ์„ ์„ค์ •ํ•˜๋Š” ํ•จ์ˆ˜๊ตฐ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. 7.2 QListWidget์—์„œ ์ œ์‹œํ•˜๋Š” ์˜ˆ์ œ์—์„œ ์„ค๋ช…ํ•œ๋‹ค. 7.2 QListWidget QListWidget์— ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ํ‘œ์‹œํ•˜๋Š” ์œ„์ ฏ์ด๋‹ค. ํ‘œ์‹œ๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” QListWidgetItem์œผ๋กœ ์ถ”์ƒํ™”ํ•œ๋‹ค. ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„์ดํ…œ(QListWidgetItem ๊ฐ์ฒด) ์ƒ์„ฑ ํ›„ addItem(item), insertItem(item)์œผ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค. ํ…์ŠคํŠธ๋งŒ ์ €์žฅํ•˜๋Š” ์•„์ดํ…œ์ธ ๊ฒฝ์šฐ addItem(str), insertItem(i, str) ๋“ฑ์˜ ํŽธ์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. listWidget = QListWidget(self) listWidget.setAlternateColors(True) itemA = QListWidgetItem() itemA.setText("A") listWidget.addItem(itemA) # ์•„์ดํ…œ ์ƒ์„ฑ ํ›„ addItem(listItem) ํ˜ธ์ถœ listWidget.addItem("B") # ํ…์ŠคํŠธ๋งŒ ์ €์žฅํ•˜๋Š” ์•„์ดํ…œ์ธ ๊ฒฝ์šฐ addItem(str) ๊ฐ„ํŽธ ํ•จ์ˆ˜ ... itemC = QListWidgetItem() newC.setText("C") listWidget.insertItem(2, itemC) # ์•„์ดํ…œ ์ƒ์„ฑ ํ›„, 2 ์œ„์น˜์— ์‚ฝ์ž…, insertItem(i, listItem) listWidget.insertItem(3, "D") # 3 ์œ„์น˜์— ์‚ฝ์ž…, insertItem(i, listItem)์˜ ๊ฐ„ํŽธ ๋ฒ„์ „ ์ „์ฒด ์•„์ดํ…œ์˜ ๊ฐœ์ˆ˜๋Š” count()๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ณ , item(row)๋กœ row ์œ„์น˜์˜ ์•„์ดํ…œ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ํ˜„์žฌ ์„ ํƒ๋œ ์•„์ดํ…œ์€ currentItem()์œผ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ณ , setCurrentItem()์œผ๋กœ ํ˜„์žฌ ์•„์ดํ…œ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ํ‚ค๋ณด๋“œ๋‚˜ ๋งˆ์šฐ์Šค๋กœ ํ˜„ ์„ ํƒ ์•„์ดํ…œ์„ ๋ณ€๊ฒฝํ•  ๊ฒฝ์šฐ currentItemChanged() ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. ๋ฆฌ์ŠคํŠธ ์œ„์ ฏ์—์„œ ์•„์ดํ…œ์„ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์€ ์•„์ดํ…œ์„ takeItem(row)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. takeItem(row)์œผ๋กœ ์–ป์€ item์€ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ ์œ„์ ฏ์— ์ถ”๊ฐ€ํ•˜๋ฉด ์•„์ดํ…œ ์ด๋™์ด ์ผ์–ด๋‚˜๊ณ  ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ ์‚ญ์ œ๋œ๋‹ค. . # ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ listWidget.takeItem(row); # ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ item = listWidget.takeItem(row); listWidget2.addItem(item) ์ฃผ์˜ : C++ Qt์—์„œ๋Š” item(i), currentItem()์œผ๋กœ item์„ ์–ป์–ด๋‚ธ ๋’ค delete ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ด๋„ ์•„์ดํ…œ์ด ์‚ญ์ œ๋œ๋‹ค. ํ•˜์ง€๋งŒ PySide2๋Š” ์ด๊ฐ€ ์ž‘๋™ํ•˜์ง€ ์•Š์œผ๋ฉฐ, takeItem(i)์œผ๋กœ ๋ฆฌ์ŠคํŠธ ์œ„์ ฏ์—์„œ ์•„์ดํ…œ์„ ์‚ญ์ œํ•ด์•ผ ํ•œ๋‹ค. QListWidget์€ ๋””ํดํŠธ๋กœ ํŽธ์ง‘์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ์ฝ๊ธฐ ์ „์šฉ์ด๋‹ค. ๊ทธ ์ด์œ ๋Š” ์—๋””ํŠธ ํŠธ๋ฆฌ๊ฑฐ(editTriggers)๋Š” ์„ค์ •๋˜์–ด ์žˆ์ง€๋งŒ ์•„์ดํ…œ์˜ ์†์„ฑ์— Qt::ItemIsEditable์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋˜ํ•œ SingleSlectionMode์˜ ์„ ํƒ ๋ชจ๋“œ๋กœ ์•„์ดํ…œ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์€ QListWidget์™€ QListWidgetItem์˜ ๊ด€๋ จ๋œ ์†์„ฑ์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. QListWidget์˜ ๋””ํดํŠธ ์†์„ฑ editTriggers : QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed selectionMode : QAbstractItemView::SingleSelection QListWidgetItem์˜ ๋””ํดํŠธ ์†์„ฑ( flags() ): Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsDragEnabled ๋‹ค์Œ ์ฝ”๋“œ๋Š” ๋””ํดํŠธ ์—๋””ํŠธ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์•„์ดํ…œ์— ๋Œ€ํ•œ ํŽธ์ง‘ ์†์„ฑ์„ ๋ถ€๊ณผํ•ด ๋ฆฌ์ŠคํŠธ ์œ„์ ฏ์„ ํŽธ์ง‘ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•œ ์˜ˆ์ด๋‹ค. listWidget = QListWidget(self) listWidget.setSelectionMode(QAbstractItemView.ExtendedSelection); listWidget.setEditTriggers(QAbstractItemView.DoubleClicked | QAbstractItemView.AnyKeyPressed) item = QListWidgetItem("AAA") item.setFlags(item.flags() | Qt::ItemIsEditable) listWidget.addItem(item) ActiveSet ์˜ˆ์ œ QListWidge์„ ์ด์šฉํ•œ ์˜ˆ๋กœ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋‹ค์ด์–ผ๋กœ๊ทธ์— ๋‘ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ ์œ„์ ฏ์„ ๋ฐฐ์น˜ํ•˜์—ฌ ํ™œ์„ฑํ™”๋œ ์ง‘ํ•ฉ์„ ๋งŒ๋“œ๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋งŒ๋“ ๋‹ค. ๋‹ค์ด์–ผ๋กœ๊ทธ์—์„œ ํ™”์‚ดํ‘œ ๋ชจ์–‘์˜ ํˆด๋ฒ„ํ„ด์œผ๋กœ ์ขŒโ€ค์šฐ ๋ฆฌ์ŠคํŠธ ์œ„์ ฏ์˜ ์•„์ดํ…œ๋“ค์ด ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋“œ๋ž˜๊ทธ ๋“œ๋กญ ์—ญ์‹œ ์ง€์›ํ•œ๋‹ค. ๋‹ค์Œ์€ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ๊ณผ ๋ฆฌ์†Œ์Šค ์ปดํŒŒ์ผ ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ActiveSet.qrc <RCC> <qresource prefix="/"> <file>images/forward.png</file> <file>images/backward.png</file> </qresource> </RCC> > pyside2-rcc -o ActiveSet_rc.py -py3 ActiveSet.qrc ๋‹ค์Œ์€ ์†Œ์Šค์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ActiveSet.py from PySide2.QtWidgets import (QDialog, QCheckBox, QPushButton, QToolButton, QListWidget, QListWidgetItem, QLabel, QVBoxLayout, QHBoxLayout) from PySide2.QtGui import QIcon from PySide2.QtCore import QSize, Qt import ActiveSet_rc class ActiveSetDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) showSetCB = QCheckBox("Show SelectedSet") applyPB = QPushButton("&Apply") closePB = QPushButton("&Close") toActiveTB = QToolButton() toActiveTB.setIcon(QIcon(":/images/forward.png")) toActiveTB.setIconSize(QSize(32,32)) toActiveTB.setAutoRaise(True) toInactiveTB = QToolButton() toInactiveTB.setIcon(QIcon(":/images/backward.png")) toInactiveTB.setIconSize(QSize(32,32)) toInactiveTB.setAutoRaise(True) self.inactiveSetLW = QListWidget() self.inactiveSetLW.setAlternatingRowColors(True) self.inactiveSetLW.addItem("Set 1") self.inactiveSetLW.addItem("Set 2") self.inactiveSetLW.addItem("Set 3") self.inactiveSetLW.addItem("Set 4") self.activeSetLW = QListWidget() self.activeSetLW.setAlternatingRowColors(True) self.activeSetLW.addItem("Set 10") self.activeSetLW.addItem("Set 11") self.activeSetLW.addItem("Set 12") inactiveLabel = QLabel("Inactive Set") activeLabel = QLabel("Active Set") left = QVBoxLayout() left.addWidget(inactiveLabel) left.addWidget(self.inactiveSetLW) center = QVBoxLayout() center.addStretch() center.addWidget(toActiveTB) center.addWidget(toInactiveTB) center.addStretch() right = QVBoxLayout() right.addWidget(activeLabel) right.addWidget(self.activeSetLW) top = QHBoxLayout() top.addLayout(left) top.addLayout(center) top.addLayout(right) bottom = QHBoxLayout() bottom.addWidget(showSetCB) bottom.addStretch() bottom.addWidget(applyPB) bottom.addWidget(closePB) mainLayout = QVBoxLayout() mainLayout.addLayout(top) mainLayout.addLayout(bottom) self.setLayout(mainLayout) # drag-drop setting self.inactiveSetLW.setDragEnabled(True) self.inactiveSetLW.viewport().setAcceptDrops(True) self.inactiveSetLW.setDropIndicatorShown(True) self.inactiveSetLW.setDefaultDropAction(Qt.MoveAction) self.activeSetLW.setDragEnabled(True) self.activeSetLW.viewport().setAcceptDrops(True) self.activeSetLW.setDropIndicatorShown(True) self.activeSetLW.setDefaultDropAction(Qt.MoveAction) # signal-slot closePB.clicked.connect(self.close) toActiveTB.clicked.connect(self.onToActiveSet) toInactiveTB.clicked.connect(self.onToInactiveSet) def onToActiveSet(self): self.moveCurrentItem(self.inactiveSetLW, self.activeSetLW) def onToInactiveSet(self): self.moveCurrentItem(self.activeSetLW, self.inactiveSetLW) def moveCurrentItem(self, source, target): if source.currentItem() : row = source.currentRow() target.addItem(source.takeItem(row)) from PySide2.QtWidgets import QApplication import sys if __name__ == "__main__": app = QApplication(sys.argv) dialog = ActiveSetDialog() dialog.show() app.exec_() ์†Œ์Šค์ฝ”๋“œ ์ค‘์—์„œ ์„ค๋ช…ํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„์€ ๋“œ๋ž˜๊ทธ ๋“œ๋กญ๊ณผ ๊ด€๋ จ๋ผ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์•„์ดํ…œ ๋ทฐ ํด๋ž˜์Šค๋กœ ๊ตญํ•œํ•  ๊ฒฝ์šฐ QDragEnterEvent, QDragMoveEvent, QDragLeaveEvent, QDropEvent ๋“ฑ์˜ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ ์—†์ด ๊ฐ„ํŽธํ•˜๊ฒŒ ๋“œ๋ž˜๊ทธ ๋“œ๋กญ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ ์•„์ดํ…œ ๋ทฐ ํŽธ์˜ ํด๋ž˜์Šค๋กœ ํ•œ์ •ํ•  ๊ฒฝ์šฐ ์œ„์ ฏ ์†์„ฑ์„ ๋ช‡ ๊ฐœ ์„ค์ •ํ•ด ์ฃผ๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„์ดํ…œ์ด ๋“œ๋ž˜๊ทธ ๋  ์ˆ˜ ์žˆ๋„๋ก ๋ทฐ์˜ dragEnabled๋ฅผ true๋กœ ์„ค์ •ํ•œ๋‹ค. ๋ทฐ ์†์œผ๋กœ ๋“œ๋กญ๋  ์ˆ˜ ์žˆ๋„๋ก ๋ทฐ์˜ viewport()์˜ acceptDrops๋ฅผ true๋กœ ์„ค์ •ํ•œ๋‹ค. ๋“œ๋กญ๋  ์œ„์น˜๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด, ๋ทฐ์˜ showDorpIndicator ์†์„ฑ๋ฅผ ์„ค์ •ํ•ด ์ค€๋‹ค. ํ•„์š”ํ•œ ๊ฒฝ์šฐ dragDropMode์™€ defaultDropAction ์†์„ฑ์„ ์ ์ ˆํžˆ ์„ค์ •ํ•ด ์ค€๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋Š” ๋ณต์‚ฌ๋กœ ๋“œ๋ž˜๊ทธ ๋“œ๋กญ์ด ๋˜๋„๋ก ์„ค์ •ํ•œ ์˜ˆ์ด๋‹ค. listWidget = QListWidget(self) listWidget.setSelectionMode(QAbstractItemView::SingleSelection) listWidget.setDragEnabled(True) listWidget.viewport()->setAcceptDrops(True) listWidget.setDropIndicatorShown(True) ๋งŒ์•ฝ ๋ทฐ๋‚ด์—์„œ๋งŒ ์•„์ดํ…œ์„ ์›€์ง์ผ ๊ฒฝ์šฐ ๋‹ค์Œ์„ ์„ค์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. listWidget.setDragDropMode(QAbstractItemView.InternalMove) 7.3 QTableWidget QTableWidget์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ฏธ๋ฆฌ ํ–‰๊ณผ ์—ด ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๊ณ  ์•„์ดํ…œ์„ ์‚ฝ์ž…ํ•œ๋‹ค. ํ–‰๊ณผ ํ–‰๊ณผ ์—ด์˜ ํฌ๊ธฐ๋Š” ์ƒ์„ฑ์ž์—์„œ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ , setRowCount(row), setColumnCount(column)๋กœ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์•„์ดํ…œ(QTableWidgetItem)์€ setItem(row, column, item)์œผ๋กœ ์„ค์ •ํ•ด ์ค€๋‹ค. ์ด๋•Œ ์ง€์ •๋˜์–ด ์žˆ๋Š” ํ–‰๊ณผ ์—ด ํฌ๊ธฐ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ์œ„์น˜๋ฅผ ์ง€์ •ํ•  ๊ฒฝ์šฐ ์›์น˜ ์•Š๋Š” ๊ณณ์— ์•„์ดํ…œ์ด ์ถ”๊ฐ€๋˜๋ฏ€๋กœ ์ฃผ์˜ํ•œ๋‹ค. ์ด๋“ค ํ•จ์ˆ˜๋“ค์— ๋Œ€์‘ํ•˜๋Š” rowCount(), columnCount(), item(row, column) ๋“ฑ์˜ ๊ฒŸํ„ฐ(getter) ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. # Creation tableWidget = QTableWidget(self) tableWidget.setRowCount(12) tableWidget.setColumnCount(3) # ortableWidget = QTableWidget(12,3, self) itemA = QTableWidgetItem("itemA") tableWidget.setItem(0,1, itemA) itemB = QTableWidgetItem() itemB.setText("itemB") itemB.setIcon(QIcon(":/images/xxx.png")) tableWidget.setItem(3,2, itemB) ํ…Œ์ด๋ธ”์—์„œ m-by-n ํฌ๊ธฐ๋กœ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ–ˆ๋‹ค๊ณ  ํ•ด์„œ ๋ชจ๋“  ์…€์— QTableWidgetItem ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์•„์ดํ…œ ๊ฐ์ฒด๋Š” setItem()์œผ๋กœ ์ง์ ‘ ์ƒ์„ฑํ•œ ์•„์ดํ…œ์„ ์ง€์ •ํ•˜๊ฑฐ๋‚˜, ์“ฐ๊ธฐ ๊ฐ€๋Šฅํ•œ QTableWidget์—์„œ ์•„์ง ์•„์ดํ…œ ๊ฐ์ฒด๊ฐ€ ์—†๋Š” ์—†๋Š” ๋นˆ ์…€(cell)์— ๋Œ€ํ•ด ์—๋””ํŒ… ๋ชจ๋“œ๋กœ ์ง„์ž…ํ•  ๋•Œ QTableWidget์ด ์ง์ ‘ ์ƒ์„ฑํ•ด ์ค€๋‹ค. ๋‹ค์‹œ ๋งํ•˜๋ฉด ํ™”๋ฉด์— ๋ณด์ด๋Š” ๋นˆ ์…€์€ ์•„์ดํ…œ ๊ฐ์ฒด๊ฐ€ ์•„์ง ์ƒ์„ฑ๋˜์ง„ ์•Š๋Š” ์…€๊ณผ ์•„์ดํ…œ ๊ฐ์ฒด๊ฐ€ ์žˆ์ง€๋งŒ ์•„์ง ์•„์ดํ…œ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ๋”ฐ๋ผ์„œ item(row, column)์œผ๋กœ ๋ฆฌํ„ด๋˜๋Š” ์•„์ดํ…œ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด if ๋ฌธ์œผ๋กœ ๊ฒ€ํ† ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค(์•„์ดํ…œ์ด ์—†๋Š” ๊ฒฝ์šฐ None์„ ๋ฆฌํ„ดํ•จ) item = tableWidget.item(row, column) if item: x = float(item.text()) ๋นˆ ์…€์— ๋Œ€ํ•ด ์—๋””ํŒ… ๋ชจ๋“œ๋กœ ์ง„์ž…ํ•ด ํŽธ์ง‘ํ•  ๋•Œ QTableWidget ์ด ์•„์ดํ…œ ๊ฐ์ฒด๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•˜๊ธฐ ์œ„ํ•ด QTableWidgetItem์„ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •์˜์˜ ์•„์ดํ…œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ด ํด๋ž˜์Šค์˜ ํ˜•์„ setItemPrototype()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฏธ๋ฆฌ ๋“ฑ๋กํ•ด์•ผ ํ•˜๊ณ , ์•„์ดํ…œ ํด๋ž˜์Šค๋Š” clone() ๊ฐ€์ƒ ํ•จ์ˆ˜๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์•ผ ํ•œ๋‹ค. ํ–‰๊ณผ ์—ด์˜ ํ—ค๋”์˜ ๋ ˆ์ด๋ธ”์€ ๋””ํดํŠธ๋กœ 1, 2, ... ๋“ฑ๊ณผ ๊ฐ™์ด ์ˆซ์ž๋กœ ์ง€์ •๋ฉ๋‹ˆ๋‹ค. item(), setItem(), currentItem() ๋“ฑ์˜ ํ•จ์ˆ˜์—์„œ ์…€์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐ 0์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„๊ตํ•  ๋•Œ 1์”ฉ ํฐ ์ˆซ์ž์ด๋ฏ€๋กœ ํ˜ผ๋ˆํ•˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ ํ—ค๋” ๋ ˆ์ด๋ธ”์„ ๋””ํดํŠธ ๊ฐ’์œผ๋กœ ํ•œ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์…€์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค. table = QTableWidget(6,3) table.setAlternatingRowColors(true) table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) for i=0 in range(table.rowCount()): for j=0 in range(table->columnCount()): item = QTableWidgetItem() text = "({},{})".format(i).format(j) item.setText(text) item.setTextAlignment(Qt.AlignCenter) table.setItem(i, j, item) ํ—ค๋”์˜ ๋ ˆ์ด๋ธ”์„ ๋ฐ”๊ฟ€ ๋•Œ๋Š” setHorizontalHeaderItem(column, item), setVerticalHeaderItem(row, item)์œผ๋กœ ์„ค์ •ํ•ด ํ•œ๋‹ค. ์ด๋•Œ item์€ QTableWidgetItem ๊ฐ์ฒด์ด๋‹ค. ๋ฌธ์ž์—ด๋งŒ ์กด์žฌํ•  ๊ฒฝ์šฐ์—๋Š” setHorizontalHeaderLabels(stringList), setVerticalHeaderLabels(stringList) ๋“ฑ์˜ ํŽธ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ ์ˆ˜์ง ํ—ค๋”๋ฅผ ๋ณด์ด์ง€ ์•Š๋„๋ก ์„ค์ •ํ•˜๊ณ  ์ˆ˜ํ‰ ํ—ค๋”์— ๋Œ€ํ•ด์„œ๋งŒ โ€œName", "Age", "Sex" ๋“ฑ๊ณผ ๊ฐ™์€ ๋ฌธ์ž์—ด์„ ๊ฐ–๋„๋ก ์„ค์ •ํ•œ ์˜ˆ์ด๋‹ค. table = QTableWidget(6,3) table.setAlternatingRowColors(True) table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) table.setHorizontalHeaderLabels(["Name" ,"Age" , "Sex" ]) table.verticalHeader().setVisible(False) for i in range(leftTable.rowCount()): for j in range(leftTable.columnCount()): item = QTableWidgetItem() item.setTextAlignment(Qt.AlignCenter) table.setItem(i, j, item) ํ…Œ์ด๋ธ”์˜ ์•„์ดํ…œ์„ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์€ takeItem(i, j)๋ฅผ ์ด์šฉํ•œ๋‹ค. clear() ํ•จ์ˆ˜๋Š” ํ–‰๊ณผ ์—ด ํฌ๊ธฐ๋Š” ๊ทธ๋Œ€๋กœ ๋‘” ์ฑ„ ๋ชจ๋“  ํ—ค๋” ์•„์ดํ…œ์„ ํฌํ•จํ•œ ๋ชจ๋“  ์•„์ดํ…œ ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ•ด ํ…Œ์ด๋ธ”์„ ๋น„์šด๋‹ค. ๋น„์Šทํ•œ ํ•จ์ˆ˜๋กœ clearContents()๊ฐ€ ์žˆ๋Š” ๋ฐ ์ด ํ•จ์ˆ˜๋Š” ํ—ค๋” ์•„์ดํ…œ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์•„์ดํ…œ์„ ์‚ญ์ œํ•œ๋‹ค. ๋™์ ์œผ๋กœ ํ–‰์ด๋‚˜ ์—ด์„ ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ insertRow(row), insertColumn(column)์ด๋‹ค. ์ด ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ๋กœ ํ–‰์ด๋‚˜ ์—ด์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ ํ…Œ์ด๋ธ”์˜ ํฌ๊ธฐ๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ํ•จ์ˆ˜ ํ˜ธ์ถœ๋กœ ์•„์ดํ…œ ๊ฐ์ฒด๊ฐ€ ์ถ”๊ฐ€๋˜์ง€๋Š” ์•Š๋Š”๋‹ค. ๋ฐ˜๋Œ€๋กœ removeRow(row), removeColumn(colum)์œผ๋กœ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. clear(), clearContents(), insertRow(row), insertColumn(column), removeRow(row), removeColumn(column) ๋“ฑ์€ ๋ชจ๋‘ ์Šฌ๋กฏ ํ•จ์ˆ˜์ด๋ฏ€๋กœ ์‹œ๊ทธ๋„์— ์—ฐ๊ฒฐํ•ด ํ…Œ์ด๋ธ”์˜ ํ–‰, ์—ด์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜<NAME>๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. QTableWidget์€ ๋””ํดํŠธ๋กœ ํŽธ์ง‘ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋””ํดํŠธ ์„ ํƒ ๋ชจ๋“œ๋Š” ExtendedSelection์ด๋‹ค. ๋‹ค์Œ์€ ํŽธ์ง‘๊ณผ ์„ ํƒ ๋ชจ๋“œ์™€ ๊ด€๋ จ๋œ QTableWidget ๋ฐ QTableWidgetItem์˜ ๋””ํดํŠธ ๊ฐ’์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. * QTableWidget์˜ ๋””ํดํŠธ editTriggers : QAbstractItemView.DoubleClicked|QAbstractItemView.EditKeyPressed|QAbstractItemView.AnyKeyPressed * QTableWidget์˜ ๋””ํดํŠธ selectionMode : QAbstractItemView.ExtendedSelection * QTableWidgetItem์˜ ๋””ํดํŠธ ์†์„ฑ( flags() ): Qt.ItemIsEditable|Qt.ItemIsSelectable|Qt.ItemIsUserCheckable|Qt.ItemIsEnabled|Qt.ItemIsDragEnabled|Qt.ItemIsDropEnabled ๋งŒ์•ฝ ์ฝ๊ธฐ ์ „์šฉ์ด๊ณ  ํ•˜๋‚˜์˜ ์•„์ดํ…œ๋งŒ์„ ์„ ํƒ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers) tableWidget.setSelectionMode(QAbstractItemView.SingleSelection) ํ˜„์žฌ์˜ ์…€๊ณผ ์•„์ดํ…œ์€ currentCell(), currentItem() ๋“ฑ์œผ๋กœ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๊ณ , ๋ฐ˜๋Œ€๋กœ setCurrrentCell(), setCurrentItem()์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋น„์Šทํ•œ ์œ ํ˜•์˜ ํ•จ์ˆ˜์ธ currentRow(), currentColumn()์€ ํ˜„์žฌ ์…€์˜ ํ–‰์ด๋‚˜ ์—ด ๋ฒˆํ˜ธ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. QTableWidget์—๋Š” cellActivated(), itemEntered(), itemChanged(), itemSelectionChanged() ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ์‹œ๊ทธ๋„์ด ์žˆ๋‹ค. ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์‹œ๊ทธ๋„์€ itemChanged(item)์ด๋‹ค. ์‚ฌ์šฉ ์˜ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. tableWidget.itemChanged.connect(self.itemChanged) ์…€์˜ ์„ ํƒ์˜์—ญ์„ selectedRanges()๋ฅผ ํ†ตํ•ด ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” QTableWidgetSelectionRange ๊ฐ์ฒด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š”๋ฐ ์•„์ดํ…œ ๊ฐ์ฒด๊ฐ€ ์กด์žฌ ์—ฌ๋ถ€์— ์ƒ๊ด€์—†์ด ๊ทธ ๋ฒ”์œ„๋ฅผ QTableWidgetSelectionRange์— ๋‹ด์•„ ๋ฆฌํ„ดํ•œ๋‹ค. ๋น„์Šทํ•œ ํ•จ์ˆ˜๋กœ QTableWidgetItem์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” selectedItems()๊ฐ€ ์žˆ๋Š”๋ฐ ์ด ํ•จ์ˆ˜๋Š” ์„ ํƒ ์˜์—ญ ์ค‘ ๋นˆ ์…€์„ ์ œ์™ธํ•œ ์•„์ดํ…œ์„ ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„ ๋ฆฌํ„ดํ•œ๋‹ค. XYTable1 ์˜ˆ์ œ๋Š” XY ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ์ด๋‹ค. ์ด ํ…Œ์ด๋ธ”์€ 2๊ฐœ์˜ ์—ด์„ ๊ฐ€์ง€๋ฉฐ ์ฃผ์–ด์ง„ xy ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. ์…€์„ ์„ ํƒํ•˜์—ฌ ํด๋ฆฝ๋ณด๋“œ๋ฅผ ํ†ตํ•ด ๋ณต์‚ฌ(copy) ํ•˜์—ฌ ์—‘์…€ ๋“ฑ์— ๋ถ™์—ฌ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. from PySide2.QtWidgets import (QDialog, QTableWidget, QTableWidgetItem, QHeaderView, QPushButton, QHBoxLayout, QVBoxLayout, QAbstractItemView, QShortcut) from PySide2.QtGui import QKeySequence from PySide2.QtCore import QPointF, Qt class XYTableDialog(QDialog): def __init__(self, coordinates, parent=None): super().__init__(parent) self.rowCount = len(coordinates) self.table = QTableWidget() self.table.setRowCount(len(coordinates)) self.table.setColumnCount(2) self.table.setAlternatingRowColors(True) self.table.setSelectionMode(QAbstractItemView.ContiguousSelection) self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.table.setFocusPolicy(Qt.StrongFocus) self.table.setHorizontalHeaderLabels(["X", "Y"]) for row in range(len(coordinates)): point = coordinates[row] itemX = QTableWidgetItem(str(point.x())) itemX.setTextAlignment(Qt.AlignRight | Qt.AlignVCenter) self.table.setItem(row, 0, itemX) itemY = QTableWidgetItem(str(point.y())) itemY.setTextAlignment(Qt.AlignRight | Qt.AlignVCenter) self.table.setItem(row, 1, itemY) copyShortcut = QShortcut(QKeySequence.Copy, self) copyShortcut.activated.connect(self.copy) self.closeButton = QPushButton("&Close") bottomLayout = QHBoxLayout() bottomLayout.addStretch() bottomLayout.addWidget(self.closeButton) layout = QVBoxLayout() layout.addWidget(self.table) layout.addLayout(bottomLayout) self.setLayout(layout) self.closeButton.clicked.connect(self.close) def coordinate(self): coord = [] for i in range(self.table.rowCount()): itemX = self.table.item(i, 0) itemY = self.table.item(i, 1) pt = QPointF(float(itemX.text()),float(itemY.text())) coord.append(pt) return coord def copy(self): selectedRangeList = self.table.selectedRanges() if selectedRangeList == [] : return text = "" selectedRange = selectedRangeList[0] for i in range(selectedRange.rowCount()): if i > 0: text += "\n" for j in range(selectedRange.columnCount()): if j > 0: text += "\t" itemA = self.table.item(selectedRange.topRow()+i, selectedRange.leftColumn()+j) if itemA : text += itemA.text() text += '\n' QApplication.clipboard().setText(text) from PySide2.QtWidgets import QApplication import sys if __name__ == '__main__': app = QApplication(sys.argv) coordinates = [QPointF(0.0, 0.9), QPointF(0.2, 11.0), QPointF(0.4, 15.4), QPointF(0.6, 12.9), QPointF(0.8, 8.5), QPointF(1.0, 7.1), QPointF(1.2, 4.0), QPointF(1.4, 13.6), QPointF(1.6, 22.2), QPointF(1.8, 22.2)] dlg = XYTableDialog(coordinates) dlg.show() app.exec_() QShortcut ์€ ๋‹จ์ถ•ํ‚ค์— ๋ฐ˜์‘ํ•˜๋„๋ก ํ•œ๋‹ค. ํด๋ฆฝ๋ณด๋“œ๋Š” \t์™€ ์œผ๋กœ ๊ตฌ๋ถ„๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์—‘์…€ ๋“ฑ์œผ๋กœ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์—‘์…€์—์„œ ๋ณต์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ๋„ ๊ฐ™์€ ํฌ๋งท์œผ๋กœ ํด๋ฆฝ๋ณด๋“œ๋กœ ๋ณต์‚ฌ๋œ๋‹ค. 1 3 2 1 โ†’ 1\t3\n2\t1\n3\t\4\n 3 4 XYTable2 ์•ž์„œ์˜ XYTable1 ์˜ˆ์ œ๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด QTableWidget์„ ์„œ๋ธŒํด๋ž˜์‹ฑํ•œ๋‹ค. ๋‹ค์Œ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. Enter๋‚˜ Return ํ‚ค์— ๋ฐ˜์‘ํ•˜์—ฌ ์ž…๋ ฅ์„ ์™„๋ฃŒํ•˜๊ณ  ์•„๋ž˜ ์…€๋กœ ์ด๋™ํ•˜๋Š” ๊ธฐ๋Šฅ paste ๊ธฐ๋Šฅ(๋” ํฐ ์˜์—ญ์ด ๋ณต์‚ฌ๋˜๋Š” ๊ฒฝ์šฐ ํ–‰์ด ๋Š˜์–ด๋‚จ) from PySide2.QtWidgets import (QDialog, QTableWidget, QTableWidgetItem, QHeaderView, QPushButton, QHBoxLayout, QVBoxLayout, QAbstractItemView, QShortcut, QLineEdit) from PySide2.QtGui import QKeySequence from PySide2.QtCore import QPointF, Qt import numpy as np # incremental row + keypress event class MyTable(QTableWidget): def __init__(self, horizonatlHearderLabels, parent=None): super().__init__(parent) self.setColumnCount(len(horizonatlHearderLabels)) self.setRowCount(1) self.setAlternatingRowColors(True) self.setSelectionMode(QAbstractItemView.ContiguousSelection) self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.setFocusPolicy(Qt.StrongFocus) self.setHorizontalHeaderLabels(horizonatlHearderLabels) for i in range(self.rowCount()): for j in range(self.columnCount()): item = QTableWidgetItem("") item.setTextAlignment(Qt.AlignRight|Qt.AlignVCenter) self.setItem(i, j, item) copyShortcut = QShortcut(QKeySequence.Copy, self) pasteShortcut = QShortcut(QKeySequence.Paste, self) copyShortcut.activated.connect(self.copy) pasteShortcut.activated.connect(self.paste) def setData(self, data): self.setRowCount(0) # erase data row, col = data.shape self.setColumnCount(col) self.setRowCount(row) print(data) for i in range(self.rowCount()): for j in range(self.columnCount()): item = QTableWidgetItem(str(data[i, j])) item.setTextAlignment(Qt.AlignRight|Qt.AlignVCenter) self.setItem(i, j, item) def data(self): r = np.zeros(self.rowCount(),self.columnCount()) for i in range(self.rowCount()): for j in range(self.columnCount()): r[i, j]=float(self.items(i, j).text()) return r def keyPressEvent(self, event): super().keyPressEvent(event) if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter : i = self.currentRow() + 1 j = self.currentColumn() if i == self.rowCount(): self.setRowCount(i+1) for kk in range(self.columnCount()): item = QTableWidgetItem() item.setTextAlignment(Qt.AlignRight|Qt.AlignVCenter) self.setItem(i, kk, item) self.setCurrentCell(i, j) event.accept() def copy(self): selectedRangeList = self.selectedRanges() if selectedRangeList == [] : return text = "" selectedRange = selectedRangeList[0] for i in range(selectedRange.rowCount()): if i > 0: text += "\n" for j in range(selectedRange.columnCount()): if j > 0: text += "\t" itemA = self.item(selectedRange.topRow()+i, selectedRange.leftColumn()+j) if itemA : text += itemA.text() text += '\n' QApplication.clipboard().setText(text) def paste(self): # 1\t2\n2\t3\n text = QApplication.clipboard().text() rows = text.split('\n') numRows = len(rows)-1 numColumns = rows[0].count('\t')+1 if self.currentRow()+numRows > self.rowCount(): prevRowCount = self.rowCount() self.setRowCount(self.currentRow()+numRows) for i in range(prevRowCount, self.currentRow()+numRows): for kk in range(self.columnCount()): item = QTableWidgetItem() item.setTextAlignment(Qt.AlignRight|Qt.AlignVCenter) self.setItem(i, kk, item) for i in range(numRows): columns = rows[i].split('\t') for j in range(numColumns): row = self.currentRow()+i column = self.currentColumn()+j if column < self.columnCount(): self.item(row, column).setText(columns[j]) class XYTableDialog(QDialog): def __init__(self, horizonatlHearderLabels, data, parent=None): super().__init__(parent) self.table = MyTable(horizonatlHearderLabels, parent) self.table.setData(data) self.closeButton = QPushButton("&Close") bottomLayout = QHBoxLayout() bottomLayout.addStretch() bottomLayout.addWidget(self.closeButton) layout = QVBoxLayout() layout.addWidget(self.table) layout.addLayout(bottomLayout) self.setLayout(layout) self.closeButton.clicked.connect(self.close) from PySide2.QtWidgets import QApplication import sys if __name__ == '__main__': app = QApplication(sys.argv) data = np.array([ [0.0,0.9 ], [0.2,11.0], [0.4,15.4], [0.6,12.9], [0.8,8.5 ], [1.0,7.1], [1.2,4.0 ], [1.4,13.6], [1.6,22.2], [1.8,22.2]]) dlg = XYTableDialog(["X","Y"],data) dlg.show() # table = MyTable(["X","Y","Z"]) # table.setData(data) # table.show() app.exec_() 7.4 QTreeWidget ํŠธ๋ฆฌ ์œ„์ ฏ(QTreeWidget)์€ ์ฒซ ๋ฒˆ์งธ ์—ด(column)์— ํŠธ๋ฆฌ ํ˜•ํƒœ์˜ ์•„์ดํ…œ์ด ๋ฐฐ์น˜๋˜๊ณ , ์˜ต์…˜์ธ ๋‘ ๋ฒˆ์งธ ์นผ๋Ÿผ๋ถ€ํ„ฐ๋Š” ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์–ด๋–ค ๊ฐ’์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์ ฏ์ด๋‹ค. ๋””ํดํŠธ ์—ด ๊ฐœ์ˆ˜๋Š” 1๊ฐœ์ด๋‹ค. ์ฆ‰, ํŠธ๋ฆฌ๋งŒ ํ‘œํ˜„๋œ๋‹ˆ๋‹ค. setColumnCount(column)๋กœ ํ–‰ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ํŠธ๋ฆฌ ์œ„์ ฏ์˜ ์•„์ดํ…œ(QTreeWidgetItem)์€ ์•„์ดํ…œ ๊ฐ์ฒด๋ผ๋ฆฌ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋ฅผ ์ด๋ฃจ๋„๋ก ๊ตฌ์„ฑ๋˜๋ฉฐ ์ตœ์ƒ์œ„ ์•„์ดํ…œ์€ QTreeWidget์„ ๋ถ€๋ชจ๋กœ ๊ฐ–๋„๋ก ์„ค์ •ํ•œ๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์—ด ๊ฐœ์ˆ˜๋ฅผ ํ•œ ๊ฐœ๋กœ ํŠธ๋ฆฌ๋งŒ ํ‘œํ˜„ํ•˜๋„๋ก ํ•œ ์˜ˆ์ด๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ํ—ค๋”๊ฐ€ ๋ณด์ด ์•Š๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. treeWidget = QTreeWidget(mainWindow) treeWidget.setAlternatingRowColors(True) treeWidget.header().setVisible(False) itemTop1 = QTreeWidgetItem(treeWidget) itemTop1.setText(0, "QAbstractItemView") itemChild1 = QTreeWidgetItem(itemTop1) itemChild1.setText(0, "QListView") itemChild2 = QTreeWidgetItem(itemTop1) itemChild2.setText(0, "QTreeView") itemTop2 = QTreeWidgetItem(treeWidget) itemTop2.setText(0, "QAbstractItemModel") itemChild3 = QTreeWidgetItem(itemTop2) itemChild3.setText(0, "QListModel") itemChild4 = QTreeWidgetItem(itemChild3) itemChild4.setText(0, "QStringListModel") QTreeWidget์˜ ํ—ค๋”๋Š” ๋””ํดํŠธ๋กœ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜๊ธฐ ๋•Œ๋ฌธ์— treeWidget().header().setVisible(false)๋ฅผ ํ˜ธ์ถœํ•ด ์ฃผ์—ˆ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ ์ตœ์ƒ์œ„ ์•„์ดํ…œ์€ ์ƒ์„ฑํ•  ๋•Œ ํŠธ๋ฆฌ ์œ„์ ฏ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ธ์ž๋กœ ์ฃผ์–ด ์ƒ์„ฑํ–ˆ๋‹ค. ์ดํ›„ ์ž์‹ ์•„์ดํ…œ์€ ๋‹ค๋ฅธ ์•„์ดํ…œ์„ ๋ถ€๋ชจ๋กœ ํ•˜์—ฌ ์ƒ์„ฑํ•œ ๊ฒƒ์ด๋‹ค. ์ตœ์ƒ์œ„ ์•„์ดํ…œ์„ ์ง€์ •ํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ invisibleItem()์˜ ์ž์‹์ด๋‚˜ addTopLevelItem()์ด๋‚˜ insertTopLevelItem()์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. invisibleItem()์€ QTreeWidget ๋‚ด์— ์กด์žฌํ•˜๋Š” ๋ฃจํŠธ ์•„์ดํ…œ์„ ๋ฆฌํ„ดํ•˜๋Š”๋ฐ ์—„๋ฐ€ํ•˜๊ฒŒ ์ตœ์ƒ์œ„ ์•„์ดํ…œ์€ ์ด ๋ฃจํŠธ ์•„์ดํ…œ์˜ ์ž์‹์ด๋‹ค. ๋‹ค์Œ์€ ์ตœ์ƒ์œ„ ์•„์ดํ…œ์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค. # treeWidget์„ ๋ถ€๋ชจ๋กœ ์ƒ์„ฑ item = QTreeWidgetItem(treeWidget) # treeWidget ๋‚ด์˜ invisibleItem()์„ ๋ถ€๋ชจ๋กœ ์ƒ์„ฑ item = QTreeWidgetItem(treeWidget.invisibleItem()) # ๋ถ€๋ชจ ์—†์ด ์ƒ์„ฑ ํ›„ addTopLevelItem() ํ˜ธ์ถœ item = new QTreeWidgetItem() item.insertTopLevelItem(item) # ๋งˆ์ง€๋ง‰ ์ตœ์ƒ์œ„ ์•„์ดํ…œ์œผ๋กœ ์‚ฝ์ž… # ๋ถ€๋ชจ ์—†์ด ์ƒ์„ฑ ํ›„ insertTopLevelItem() ํ˜ธ์ถœ item = QTreeWidgetItem() item.insertTopLevelItem(3, item) # 4๋ฒˆ์งธ ์ตœ์ƒ์œ„ ์•„์ดํ…œ์œผ๋กœ ์‚ฝ์ž… ๋งŒ์•ฝ ์—ด ์ˆ˜๊ฐ€ 2 ์ด์ƒ์ธ ๊ฒฝ์šฐ์ผ ๊ฒฝ์šฐ์—๋Š” ๋ณดํ†ต ํ—ค๋”๋„ ์กด์žฌํ•˜๋„๋ก ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ํ—ค๋”์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ผ๋ฒจ๋กœ๋Š” QTableWidget์ฒ˜๋Ÿผ 1,2, ... ์ด ๋””ํดํŠธ๋กœ ํ‘œ์‹œ๋œ๋‹ค. ์ด๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด setHeaderItem()์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. treeWidget = QTreeWidget(mainWindow) treeWidget.setColumnCount(2) treeWidget.setHeaderLabels([ "Class", "Descriptions"] ) treeWidget.setAlternatingRowColors(True); treeWidget.header().setSectionResizeMode(QHeaderView.Stretch) itemTop1 = QTreeWidgetItem(treeWidget,["QAbstractItemView", "base clase of item view"]) itemChild1 = QTreeWidgetItem(itemTop1, ["QListView","list view"]) itemChild2 = QTreeWidgetItem(itemTop1) itemChild2.setText(0, "QTreeView") itemChild2.setText(1, "tree view") itemTop2 = QTreeWidgetItem(treeWidget) itemTop2.setText(0, "QAbstractItemModel") itemTop2.setText(1, "base class of item model") itemChild3 = QTreeWidgetItem(itemTop2) itemChild3.setText(0, "QListModel") itemChild3.setText(1, "list model") itemChild4 = QTreeWidgetItem(itemChild3) itemChild4.setText(0, "QStringListModel") itemChild4.setText(1, "string list model") ์œ„ ์ฝ”๋“œ์—์„œ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ QTreeWidgetItem์„ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ์„ค์ •ํ–ˆ๋‹ค. ํŠธ๋ฆฌ ์œ„์ ฏ์˜ ์•„์ดํ…œ์€ clear()๋กœ ์ง€์šธ ์ˆ˜ ์žˆ๊ณ , collapseItem(item), expandItem(item)์œผ๋กœ ์ž์‹ ์•„์ดํ…œ์ด ์•ˆ ๋ณด์ด๊ฒŒ ์ ‘๊ฑฐ๋‚˜, ๋ณด์ด๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋“ค ํ•จ์ˆ˜๋“ค์€ ์Šฌ๋กฏ ํ•จ์ˆ˜์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์‹œ๊ทธ๋„ ํ•จ์ˆ˜๋Š” currentItemChanged(currentItem, previsousItem), itemActivated(item, colum) ๋“ฑ์ด ์žˆ๋‹ค. ํŠธ๋ฆฌ ์œ„์ ฏ์€ ๋””ํดํŠธ๋กœ ํŽธ์ง‘์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋„ ํŠธ๋ฆฌ ์œ„์ ฏ์ด ํŽธ์ง‘ ํŠธ๋ฆฌ๊ฑฐ๋Š” ๊ฐ€์ง€์ง€๋งŒ QTreeWidgetItem์ด Qt::ItemIsEditable ์†์„ฑ์„ ๊ฐ–์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ฆฌ์ŠคํŠธ ์œ„์ ฏ์—์„œ ์ œ์‹œํ•œ ๋ฐฉ๋ฒ•๊ณผ ๋™์ผํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํŽธ์ง‘ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. QTreeWidget์˜ ๋””ํดํŠธ editTriggers : QAbstractItemView.DoubleClicked|QAbstractItemView.EditKeyPressed QTreeWidget์˜ ๋””ํดํŠธ selectionMode : QAbstractItemView.SingleSelection QTreeWidgetItem์˜ ๋””ํดํŠธ ์†์„ฑ( flags() ) Qt.ItemIsSelectable|Qt.ItemIsUserCheckable|Qt.ItemIsEnabled|Qt.ItemIsDragEnabled|Qt.ItemIsDropEnabled 8. ๊ธฐํƒ€ ... 8.1 ๋…๋ฆฝ ์‹คํ–‰ํŒŒ์ผ ํŒŒ์ด์ฌ์—์„œ ๋…๋ฆฝ ์‹คํ–‰ํŒŒ์ผ์„ ๋งŒ๋“œ๋Š” ํˆด๋กœ๋Š” PyInstaller, py2exe, cx_Freeze ๋“ฑ์ด ์žˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ PyInstaller๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ๋‹ค. ์„ค์น˜ > pip install pyinstaller ์‚ฌ์šฉ๋ฒ• > pyinstaller program.py # ๊ธฐ๋ณธ ๋ฐฉ๋ฒ• > pyinstaller -w program.py # ์ฝ˜์†” ์ฐฝ ์—†์• ๊ธฐ > pyinstaller -w -F program.py # ์ฝ˜์†” ์ฐฝ ์—†์• ๊ธฐ + 1๊ฐœ ํŒŒ์ผ PySide2๊ฐ€ LGPL ์ž„์„ ๊ฐ์•ˆํ•  ๋•Œ 1๊ฐœ ํŒŒ์ผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋ผ์ด์„ ์Šค ๊ทœ์•ฝ์ƒ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ์— ์œ ์˜ํ•œ๋‹ค. --> ์ด์ œ PySide2 ํ”„๋กœ๊ทธ๋žจ์ด ์ž‘๋™ํ•˜์ง€ ์•Š์Œ next try ์ฝ˜๋‹ค๋กœ ์ธ์Šคํ†จ(๊ด€๋ฆฌ์ž ๊ถŒํ•œ) conda install -c conda-forge pyinstaller 8.2 Matplotlib ์—ฐ๊ณ„ PySide2์—์„œ Matplotlib์„ ์—ฐ๊ณ„ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ๋ฒ• ๊ฐœ๊ด€์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Matplotlib์€ ์ € ์ˆ˜์ค€์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(Qt4, Qt5, Cairo, tk ๋“ฑ๋“ฑ)์„ ๋Œ€์ƒ(backends)์œผ๋กœ ์„ ํƒ์ ์œผ๋กœ ๊ตฌ๋™๋œ๋‹ค. PySide2๋‚˜ PyQt5 ๋“ฑ Qt5๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž„ํฌํŠธ ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค. from matplotlib.backends.backend_qt5agg import FigureCanvas Qt5 backend๋กœ ์„ ํƒ ์‹œ FigureCanvas ํด๋ž˜์Šค๋Š” FigureCanvasQT๋ฅผ ์žฌ์ •์˜ํ•œ ๊ฒƒ์ด๋ฉฐ, QWidget๊ณผ FigureCanvasBase๋กœ๋ถ€ํ„ฐ ๋‹ค์ค‘ ์ƒ์†์„ ๋ฐ›์œผ๋ฉฐ, ์ƒ์„ฑ์ž์˜ ์œ ์ผํ•œ ์ธ์ž๋Š” Matplotlib์ด Figure ๊ฐ์ฒด์ด๋‹ค. class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase): def __init__(self, figure): ... FigureCanvas๊ฐ€ QWidget์—์„œ ์ƒ์†๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ ์œ„์ ฏ์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹ค๋งŒ Matplotlib API๋Š” ๋ช…๋ น์–ด ๋ฐฉ์‹์ด ์•„๋‹Œ ๊ฐ์ฒด์ง€ํ–ฅ ๋ฐฉ์‹์˜ API๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. HelloMatplotlib HelloMatplotlib ์˜ˆ์ œ๋Š” FigureCanvas๋ฅผ QWidget์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ด๋‹ค. HelloMatplotlib.py import sys from PySide2.QtWidgets import QApplication from matplotlib.backends.backend_qt5agg import FigureCanvas from matplotlib.figure import Figure if __name__ == "__main__": import numpy as np app = QApplication(sys.argv) x = np.linspace(0,1,50) y1 = np.cos(4*np.pi*x) y2 = np.cos(4*np.pi*x)*np.exp(-2*x) canvasWidget = FigureCanvas(Figure(figsize=(6,4))) ax = canvasWidget.figure.add_subplot(2,1,1) ax.plot(x, y1,'r-*',lw=1) ax.grid(True) ax.set_ylabel(r'$sin(4 \pi x)$') ax.axis([0,1, -1.5,1.5]) ax = canvasWidget.figure.add_subplot(2,1,2) ax.plot(x, y2,'b--o',lw=1) ax.grid(True) ax.set_xlabel('x') ax.set_ylabel(r'$ e^{-2x} sin(4\pi x) $') ax.axis([0,1, -1.5,1.5]) canvasWidget.figure.tight_layout() canvasWidget.show() app.exec_() FigureCanvas ๊ฐ์ฒด canvasWidget์„ ์ƒ์„ฑํ•œ ํ›„ ์บ”๋ฒ„์Šค์˜ ๋ฉค๋ฒ„๋กœ Figure ํด๋ž˜์Šค ๊ฐ์ฒด์ธ figure๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๊ฐ์ฒด์ง€ํ–ฅ API๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ๋‹ค. Plot from file ๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ๋Š” FigureCanvas๋ฅผ ์ž์‹ ์œ„์ ฏ์œผ๋กœ ์‚ฌ์šฉํ•œ ์˜ˆ์ด๋‹ค. if __name__ == "__main__": from PySide2.QtCore import QCoreApplication QCoreApplication.setLibraryPaths(['C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\plugins']) import sys from PySide2.QtWidgets import QWidget, QApplication, QVBoxLayout from matplotlib.backends.backend_qt5agg import FigureCanvas from matplotlib.figure import Figure from matplotlib import rcParams class MatplotlibWidget(QWidget): def __init__(self): QWidget.__init__(self) self.canvas = FigureCanvas(Figure()) vertLayout = QVBoxLayout() vertLayout.addWidget(self.canvas) self.setLayout(vertLayout) self.axes = self.canvas.figure.add_subplot(111) if __name__ == "__main__": import numpy as np app = QApplication(sys.argv) window = MatplotlibWidget() window.show() x = np.linspace(0,10.,50) window.axes.plot(x, np.sin(x)) window.axes.plot(x, np.sin(x),'*') app.exec_() if __name__ == "__main__": from PySide2.QtCore import QCoreApplication QCoreApplication.setLibraryPaths(['C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\plugins']) import time import numpy as np import sys from PySide2.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout) from PySide2.QtCore import Qt from matplotlib.backends.backend_qt5agg import FigureCanvas from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar from matplotlib.figure import Figure class ApplicationWindow(QMainWindow): def __init__(self): super().__init__() self._main = QWidget() self.setCentralWidget(self._main) layout = QVBoxLayout(self._main) static_canvas = FigureCanvas(Figure(figsize=(5, 3))) layout.addWidget(static_canvas) self.addToolBar(NavigationToolbar(static_canvas, self)) dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3))) layout.addWidget(dynamic_canvas) self.addToolBar(Qt.BottomToolBarArea, NavigationToolbar(dynamic_canvas, self)) self._static_ax = static_canvas.figure.subplots() t = np.linspace(0, 10, 501) self._static_ax.plot(t, np.tan(t), ".") self._dynamic_ax = dynamic_canvas.figure.subplots() self._timer = dynamic_canvas.new_timer(100, [(self._update_canvas, (), {})]) self._timer.start() def _update_canvas(self): self._dynamic_ax.clear() t = np.linspace(0, 10, 101) # Shift the sinusoid as a function of time. self._dynamic_ax.plot(t, np.sin(t + time.time())) self._dynamic_ax.figure.canvas.draw() if __name__ == "__main__": app = QApplication(sys.argv) window = ApplicationWindow() window.show() app.exec_() input('...') 8.3 VTK ์—ฐ๊ณ„ VTK๋Š” 3์ฐจ์› ์‹œ๊ฐํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. C++ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋ฉฐ Python ๋ฐ”์ธ๋”ฉ์ด ์ œ๊ณต๋œ๋‹ค. ์ธ์Šคํ†จ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. > pip install vtk PySide2๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” VTK 8.2 ์ด์ƒ์ด ํ•„์š”ํ•˜๋‚˜ 2-19.5.12 ๊ธฐ์ค€์œผ๋กœ pip ์ธ์Šคํ†จ ๋œ VTK 8.1.2์ด๋‹ค. ๋”ฐ๋ผ์„œ PySide2๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค(PyQt5๋งŒ ์ •์ƒ ์ง€์›) SimpleVTK.py 8. ๋‹จ์–ด ํ•™์Šต๊ธฐ Google TTS๋ฅผ ์ด์šฉํ•œ ๋‹จ์–ด ํ•™์Šต๊ธฐ player = new QMediaPlayer; // ... player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); player->setVolume(50); player->play(); ์ฃผ์˜ํ•  ์ ์€ ๋ฉ”์‹œ์ง€ ๋ฃจํ”„๊ฐ€ ๊ฐ€๋™๋˜๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ if __name__ == '__main__': from PySide2.QtCore import QCoreApplication QCoreApplication.setLibraryPaths(['C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\plugins']) from PySide2.QtMultimedia import QMediaPlayer from PySide2.QtCore import QUrl from PySide2.QtWidgets import QWidget, QPushButton, QApplication import sys if __name__ == '__main__': app = QApplication(sys.argv) player = QMediaPlayer() player.setMedia(QUrl.fromLocalFile(r'D:\Home\Music\ABBA.mp3')) player.setVolume(50) player.play() app.exec_() Qt์˜ TTS ์‚ฌ์šฉ ์ค€๋น„ ์‚ฌํ•ญ : ์„ค์ •/์ง€์—ญ ๋ฐ ์–ธ์–ด์— "ํ•œ๊ตญ์–ด", "English"๊ฐ€ ํ‘œ์‹œ๋˜์–ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•˜๊ณ , ๊ฐ ์–ธ์–ด๋ฅผ ์„ ํƒํ•ด ์˜ต์…˜์œผ๋กœ ๋“ค์–ด๊ฐ€ TTS๊ฐ€ ๋˜๋„๋ก ์„ค์ •ํ•œ๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ ### ๋ณธ๋ฌธ: ๋งŽ์€ ๋ถ„๋“ค์˜ ํ”ผ๋“œ๋ฐฑ์œผ๋กœ ์ˆ˜๋…„๊ฐ„ ๋ณด์™„๋œ ์ž…๋ฌธ์ž๋ฅผ ์œ„ํ•œ ๋”ฅ ๋Ÿฌ๋‹ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ต์žฌ E-book์ž…๋‹ˆ๋‹ค. ์˜คํ”„๋ผ์ธ ์ถœํŒ๋ฌผ ๊ธฐ์ค€์œผ๋กœ ์ฝ”๋“œ ํฌํ•จ ์•ฝ 1,000 ํŽ˜์ด์ง€ ์ด์ƒ์˜ ๋ถ„๋Ÿ‰์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ๋ถ€ํ„ฐ BERT์™€ ๊ฐ™์€ PLM์˜ ๋‹ค์–‘ํ•œ ๋‹ค์šด์ŠคํŠธ๋ฆผ ํƒœ์Šคํฌ๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋‚ด์šฉ์„ ๋ฌด๋ฃŒ๋กœ ๊ณต๊ฐœํ•˜์—ฌ ์ด ์›น ์‚ฌ์ดํŠธ๋ฅผ ํ†ตํ•ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์ž…๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ, BERT ์‹ค์Šต๊ณผ ๊ฐ™์€ ์‹ฌํ™” ๋‚ด์šฉ์€ ์œ ๋ฃŒ E-book ( ๊ตฌ๋งค ์‹œ ์ด ์ฑ…์˜ PDF ํŒŒ์ผ ์ œ๊ณต ) ์—์„œ๋งŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ฑ…์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์™€ ๋”ฅ ๋Ÿฌ๋‹ ์ž…๋ฌธ์ž๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ•˜์ง€๋งŒ, ํŒŒ์ด์ฌ์€ ์ด๋ฏธ ์–ด๋Š ์ •๋„ ์•ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์€ ํ…์„œ ํ”Œ๋กœ์˜ ์ผ€๋ผ์Šค API๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (๋ฌด์Šจ ๋‚ด์šฉ์ธ์ง€ ๋ชฐ๋ผ๋„ ์ด ์ฑ…์œผ๋กœ ์‹œ์ž‘ ๊ฐ€๋Šฅ!) 2022-01-01์ผ ์ž๋กœ ๊นƒํ—ˆ๋ธŒ ์ €์žฅ์†Œ๋ฅผ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ py ํŒŒ์ผ์— Colab ๋งํฌ๊ฐ€ ๊ธฐ์žฌ๋ผ ์žˆ์œผ๋‹ˆ ํ•ด๋‹น ๋งํฌ๋ฅผ ํ†ตํ•ด Colab ์‹ค์Šต๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. github : https://github.com/ukairia777/tensorflow-nlp-tutorial ๋Œ“๊ธ€ ๋˜๋Š” ํ”ผ๋“œ๋ฐฑ(์งˆ๋ฌธ/์ง€์ ) ๋˜๋Š” ์ด๋ฉ”์ผ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋‚ด์šฉ์— ๋Œ€ํ•œ ํŽ˜์ด์ง€๋งˆ๋‹ค ๋Œ“๊ธ€ ๋ฒ„ํŠผ ์˜†์„ ๋ณด๋ฉด ํ”ผ๋“œ๋ฐฑ ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ํšŒ์›๊ฐ€์ž…์ด ๋ฒˆ๊ฑฐ๋กœ์šฐ์‹œ๋‹ค๋ฉด ํ”ผ๋“œ๋ฐฑ ๋ฒ„ํŠผ์œผ๋กœ ์˜๊ฒฌ ์ฃผ์…”๋„ ๋Œ“๊ธ€๋กœ ๋‹ต๋ณ€๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋™์ผ ์ €์ž๊ฐ€ ๋งŒ๋“  PyTorch ํ•™์Šต ์ž๋ฃŒ : https://wikidocs.net/book/2788 ๊ณต๋ถ€๋ฅผ ์œ„ํ•ด ๋ชจ๋“  ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ง์ ‘ ํŒŒ์›Œํฌ์ธํŠธ๋กœ ๊ทธ๋ ธ์œผ๋ฉฐ ์•„๋ž˜ ๋งํฌ์—์„œ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์˜ ์ด๋ฏธ์ง€ ์ž๋ฃŒ ๊ณต์œ  (์˜๋ฆฌ์  ๋ชฉ์  ์ œ์™ธ ์‚ฌ์šฉ ๊ฐ€๋Šฅ) : https://www.slideshare.net/wonjoonyoo/ss-188835227 00. ๊ธ€์“ด์ด ์†Œ๊ฐœ ๋ฐ e-mail ์ €ํฌ๋Š” ์ทจ๋ฏธ๋กœ ๊ธ€์„ ์“ฐ๊ณ  ์žˆ๋Š” ํ˜„์—… ๋”ฅ ๋Ÿฌ๋‹ ์—”์ง€๋‹ˆ์–ด์ž…๋‹ˆ๋‹ค. ๊ทธ ๊ณผ์ •์—์„œ ๋‹ค๋ฅธ ๋ถ„๋“ค๊ณผ ์ € ๋ชจ๋‘์—๊ฒŒ ๋„์›€์ด ๋˜๋ฉด ๋” ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜์–ด์— ๋น„ํ•ด ํ•œ๊ตญ์–ด ์ž๋ฃŒ๊ฐ€ ๋งŽ์ด ๋ถ€์กฑํ•œ ์ด ๋ถ„์•ผ์—์„œ ๋งŽ์€ ์˜๊ฒฌ ๊ณต์œ ๋ฅผ ํ†ตํ•ด ํ•จ๊ป˜ ์„ฑ์žฅํ•˜๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์‹ค์ˆ˜๋กœ ์งˆ๋ฌธ์„ ๋†“์น˜๊ธฐ๋„ ํ•˜์ง€๋งŒ ์ ์ ˆ์น˜ ๋ชปํ•œ ์งˆ๋ฌธ์€ ๋‹ต๋ณ€๋“œ๋ฆฌ์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปจ์„คํŒ…์€ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ์œผ๋‚˜ ์•„์ง ํ•™์ƒ์ด์‹  ๋ถ„๋“ค์˜ ๊ณผ์ œ ๋Œ€ํ–‰์€ ํ•ด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ์žˆ์œผ๋‹ˆ ๋ฌธ์˜ํ•˜์ง€ ๋ง์•„์ฃผ์„ธ์š”. ์ €์ž ๊ณต๋™ E-mail : <EMAIL> - <NAME> (์ „ ์‚ผ์„ฑ AI ์—ฐ๊ตฌ์› / ํ˜„ AI ๊ฒธ์ž„ ๊ต์ˆ˜) - ์—ฐ์„ธ๋Œ€, ์ธ์ฒœ๋Œ€, ์„œ์šธ์˜ˆ๋Œ€, ๊ฒฝํฌ๋Œ€, ์ค‘์•™๋Œ€ ๋“ฑ ๋‹ค์ˆ˜ NLP ๊ฐ•์˜ ์ง„ํ–‰ ๊ฒฝ๋ ฅ ์žˆ์œผ๋ฉฐ ๊ณผ์™ธ, ๊ฐ•์˜ ๋ฌธ์˜๋Š” ์ด๋ฉ”์ผ๋กœ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. - ํ˜„์žฌ ์˜คํ”ˆํ•œ ์ •๊ทœ ๊ฐ•์˜: https://learningspoons.com/course/detail/nlp-pipeline/ - <NAME> (ํ˜„ ๋Œ€๊ธฐ์—… ๋”ฅ ๋Ÿฌ๋‹ ์—ฐ๊ตฌ์›) - ๋‹ค์–‘ํ•œ ๋„๋ฉ”์ธ์—์„œ BERT, Instruct GPT, ํ•œ๊ตญ์–ด LLM์„ ํŠœ๋‹ํ•˜๋ฉฐ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ชจ๋ธ์„ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์˜ ์‹ค์Šต ์ฝ”๋“œ๋“ค์ด ์žˆ๋Š” ์ €์ž์˜ ๊นƒํ—ˆ๋ธŒ ์ €์žฅ์†Œ์ž…๋‹ˆ๋‹ค. py ํŒŒ์ผ์— Colab ๋งํฌ๊ฐ€ ๊ธฐ์žฌ๋ผ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์„ธ์š”. github : https://github.com/ukairia777/tensorflow-nlp-tutorial ์‹ค์ œ ํŒ๋งค๋˜๊ณ  ์žˆ๋Š” ์ฑ…์ž…๋‹ˆ๋‹ค. full Copy ํ•˜์—ฌ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŒ… ๋˜๋Š” ์ถœํŒ๋ฌผ์— ์‚ฌ์šฉํ•˜์‹œ๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์ €์ž‘๋ฌผ์ด๋ฏ€๋กœ ๊ตฌ๋งคํ•˜์‹  E-book ํŒŒ์ผ์„ ํƒ€์ธ์—๊ฒŒ ํ•จ๋ถ€๋กœ ๋ฐฐํฌํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๋ณธ ์ž๋ฃŒ์— ๋Œ€ํ•œ ํ—ˆ๊ฐ€๋˜์ง€ ์•Š์€ ๋ฐฐํฌ๋ฅผ ๊ธˆ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐํƒ€ ๊ต์žฌ ๊ด€๋ จ ๋ฌธ์˜๋Š” ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ๋Œ“๊ธ€ ๋˜๋Š” ์ด๋ฉ”์ผ๋กœ ์—ฐ๋ฝ ์ฃผ์„ธ์š”. 00. ๋น„๊ณต๊ฐœ ์ฝ˜ํ…์ธ  & E-book ๊ตฌ๋งค ์•ˆ๋‚ด ์ด ์ฑ…์˜ ๋Œ€๋ถ€๋ถ„์˜ ๋‚ด์šฉ์€ ์ด๋ฏธ ์›น ์‚ฌ์ดํŠธ์— ๊ณต๊ฐœ๊ฐ€ ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ๋ถ€ ์‹ฌํ™” ๋‚ด์šฉ์€ ์œ ๋ฃŒ ๊ต์žฌ์ธ E-book(๊ตฌ๋งค ์‹œ PDF ์ œ๊ณต)์—๋งŒ ๊ณต๊ฐœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํŠนํžˆ 18์ฑ•ํ„ฐ์ธ BERT์˜ ์‹ค์Šต ๋ถ€๋ถ„(๋ถ„๋ฅ˜, NLI, ๊ฐœ์ฒด๋ช… ์ธ์‹, ๊ธฐ๊ณ„ ๋…ํ•ด/์งˆ์˜์‘๋‹ต) ์€ ์œ ๋ฃŒ E-book์—์„œ๋งŒ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹ค ๊ฒฝ์šฐ ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์—ˆ๋˜ ๋‚ด์šฉ์ด ๋ชจ๋‘ ๊ณต๊ฐœ๋œ '๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ'์˜ ์ตœ์ข… ์™„์„ฑ PDF ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ•ฉ ์•ฝ 1,000 ํŽ˜์ด์ง€๋กœ ์•ฝ 300 ํŽ˜์ด์ง€์”ฉ ์„ธ ๊ถŒ์œผ๋กœ ๋ถ„๊ถŒํ•œ ์„ธ ๊ฐœ์˜ ํŒŒ์ผ์ด ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ๋ชฉ์ฐจ ์ด๋™ ๊ธฐ๋Šฅ๋„ ์ œ๊ณต๋˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ํŽ˜์ด์ง€๊ฐ€ ๋งŽ์€ ๊ฒƒ์€ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค. 1๊ถŒ : 1์ฑ•ํ„ฐ ~ 7์ฑ•ํ„ฐ 2๊ถŒ : 8์ฑ•ํ„ฐ ~ 12์ฑ•ํ„ฐ 3๊ถŒ : 13์ฑ•ํ„ฐ ~ 21์ฑ•ํ„ฐ (Part 2. ์‹ฌํ™” ๊ณผ์ • ์ „์ฒด) ๊ต์žฌ๋ฅผ ๊ตฌ๋งค ํ›„ ์ฑ… ๋‚ด์šฉ์—์„œ ์ดํ•ด๊ฐ€ ์•ˆ ๋˜๋Š” ๋ถ€๋ถ„์ด๋‚˜ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰์ด ์•ˆ ๋˜๋Š” ๋ถ€๋ถ„์€ ์ €์ž ์ด๋ฉ”์ผ๋กœ ์—ฐ๋ฝ ์ฃผ์‹œ๋ฉด ๋‹ต๋ณ€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜จ๋ผ์ธ ๊ต์žฌ์˜ ์žฅ์ ์„ ์‚ด๋ ค ์ฝ”๋“œ ์ตœ์‹ ํ™”๋ฅผ ํ•ญ์‹œ ๋ชฉํ‘œ๋กœ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์„ ์ƒ๋‹˜, ๊ต์ˆ˜๋‹˜๋“ค์ด ์ˆ˜์—… ๊ต์žฌ๋กœ ๊ตฌ๋งคํ•˜์—ฌ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ณ„๋„ ๊ฐ•์˜ ๋…ธํŠธ(PPT ํŒŒ์ผ)๋ฅผ ์ œ๊ณตํ•ด ๋“œ๋ฆฌ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ €์ž ์ด๋ฉ”์ผ๋กœ ๋”ฐ๋กœ ์—ฐ๋ฝ ์ฃผ์„ธ์š”. 000. Part 1. ๊ธฐ๋ณธ ๊ณผ์ • ๋‚œ์ด๋„๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์ฑ…์„ ๋‘ ํŒŒํŠธ๋กœ ๋‚˜๋ˆด์Šต๋‹ˆ๋‹ค. 1์ฑ•ํ„ฐ๋ถ€ํ„ฐ 12์ฑ•ํ„ฐ๋ฅผ ๊ธฐ๋ณธ ๊ณผ์ •์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค. 13์ฑ•ํ„ฐ๋ถ€ํ„ฐ ๋๊นŒ์ง€๋ฅผ ์‹ฌํ™” ๊ณผ์ •์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. 01. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(natural language processing) ์ค€๋น„ํ•˜๊ธฐ ์ž์—ฐ์–ด(natural language)๋ž€ ์šฐ๋ฆฌ๊ฐ€ ์ผ์ƒ์ƒํ™œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ(natural language processing)๋ž€ ์ด๋Ÿฌํ•œ ์ž์—ฐ์–ด์˜ ์˜๋ฏธ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์ปดํ“จํ„ฐ๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์ผ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋Š” ์Œ์„ฑ ์ธ์‹, ๋‚ด์šฉ ์š”์•ฝ, ๋ฒˆ์—ญ, ์‚ฌ์šฉ์ž์˜ ๊ฐ์„ฑ ๋ถ„์„, ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…(์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜, ๋‰ด์Šค ๊ธฐ์‚ฌ ์นดํ…Œ๊ณ ๋ฆฌ ๋ถ„๋ฅ˜), ์งˆ์˜์‘๋‹ต ์‹œ์Šคํ…œ, ์ฑ—๋ด‡๊ณผ ๊ฐ™์€ ๊ณณ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. ์ตœ๊ทผ ๋”ฅ ๋Ÿฌ๋‹์˜ ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์ด ๊ด„๋ชฉํ•  ๋งŒํ•œ ์„ฑ๊ณผ๋ฅผ ์–ป์œผ๋ฉด์„œ, ์ธ๊ณต์ง€๋Šฅ์ด IT ๋ถ„์•ผ์˜ ์ค‘์š” ํ‚ค์›Œ๋“œ๋กœ ๋– ์˜ค๋ฅด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋Š” ๊ธฐ๊ณ„์—๊ฒŒ ์ธ๊ฐ„์˜ ์–ธ์–ด๋ฅผ ์ดํ•ด์‹œํ‚จ๋‹ค๋Š” ์ ์—์„œ ์ธ๊ณต์ง€๋Šฅ์—์„œ ๊ฐ€์žฅ ์˜๋ฏธ ์žˆ๋Š” ์—ฐ๊ตฌ ๋ถ„์•ผ์ด๋ฉด์„œ๋„ ์•„์ง๋„ ์ •๋ณต๋˜์–ด์•ผ ํ•  ์‚ฐ์ด ๋งŽ์€ ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ํ•„์š”ํ•œ ์ „์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•, ๋”ฅ ๋Ÿฌ๋‹ ์ด์ „ ์ฃผ๋ฅ˜๋กœ ์‚ฌ์šฉ๋˜์—ˆ๋˜ ํ†ต๊ณ„ ๊ธฐ๋ฐ˜์˜ ์–ธ์–ด ๋ชจ๋ธ, ๊ทธ๋ฆฌ๊ณ  ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ๋น„์•ฝ์ ์ธ ์„ฑ๋Šฅ์„ ์ด๋ฃจ์–ด๋‚ธ ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ๋Œ€ํ•œ ์ „๋ฐ˜์ ์ธ ์ง€์‹์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ณต๋ถ€๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ ๊ธฐ๋ณธ์ ์ธ ์„ธํŒ… ๋ฐฉ๋ฒ•๊ณผ ์•ž์œผ๋กœ ๊ณต๋ถ€ํ•˜๊ฒŒ ๋  ๋จธ์‹  ๋Ÿฌ๋‹์— ๋Œ€ํ•œ ์ „์ฒด์ ์ธ ์›Œํฌํ”Œ๋กœ์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 01-01 ์•„๋‚˜์ฝ˜๋‹ค(Anaconda)์™€ ์ฝ”๋žฉ(Colab) ๋จธ์‹  ๋Ÿฌ๋‹ ์‹ค์Šต์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งŽ์€ ํŒจํ‚ค์ง€๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ผ์ผ์ด ์„ค์น˜ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋“ค์„ ๋ชจ์•„๋†“์€ ํŒŒ์ด์ฌ ๋ฐฐํฌํŒ '์•„๋‚˜์ฝ˜๋‹ค'๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋Š” Numpy, Pandas, Jupyter Notebook, IPython, scikit-learn, matplotlib, seaborn, nltk ๋“ฑ ์ด ์ฑ…์—์„œ ์‚ฌ์šฉํ•  ๋Œ€๋ถ€๋ถ„์˜ ํŒจํ‚ค์ง€๋ฅผ ์ „๋ถ€ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์œˆ๋„ ํ™˜๊ฒฝ์„ ๊ธฐ์ค€์œผ๋กœ ๋‘๊ณ  ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ธํ„ฐ๋„ท์„ ํ†ตํ•ด ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ด์ฌ ์‹ค์Šต ํ™˜๊ฒฝ์ธ ๊ตฌ๊ธ€์˜ ์ฝ”๋žฉ(Colab)์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 1. ์•„๋‚˜์ฝ˜๋‹ค(Anaconda) ์„ค์น˜ ๋งํฌ : https://www.anaconda.com/distribution/ ์œ„ ์‚ฌ์ดํŠธ ๋งํฌ๋กœ ์ด๋™ํ•˜์—ฌ ์‚ฌ์ดํŠธ ํ•˜๋‹จ์œผ๋กœ ์ด๋™ํ•˜๋ฉด (์ €์ž๊ฐ€ ์ด ์ฑ…์„ ์ž‘์„ฑํ•  ๋‹น์‹œ ๊ธฐ์ค€) ์ขŒ์ธก์— ํŒŒ์ด์ฌ 3.7 ๋ฒ„์ „๊ณผ ์šฐ์ธก์— ํŒŒ์ด์ฌ 2.7 ๋ฒ„์ „์˜ ์•„๋‚˜์ฝ˜๋‹ค ์„ค์น˜ ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํŒŒ์ด์ฌ 3.7 ๋ฒ„์ „ 64 ๋น„ํŠธ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜ ํŒŒ์ผ์„ ์‹คํ–‰ํ•œ ํ›„์— ๋‹ค๋ฅธ ์œˆ๋„ ํ”„๋กœ๊ทธ๋žจ์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Next >๋ฅผ ๋ˆ„๋ฅด๋ฉด์„œ ์„ค์น˜๋ฅผ ์™„๋ฃŒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜๋ฉด ๋จธ์‹  ๋Ÿฌ๋‹์„ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋“ค์€ ์ž๋™์œผ๋กœ ์„ค์น˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ํ…์„œ ํ”Œ๋กœ, ์ผ€๋ผ์Šค, ์  ์‹ฌ, ์ฝ”์—”์—˜ํŒŒ์ด์™€ ๊ฐ™์€ ํŒจํ‚ค์ง€๋“ค์€ ๋ณ„๋„ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•œ๋ฐ ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ๋‹ค ์„ค์น˜ํ–ˆ๋‹ค๋ฉด ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ๋ฅผ ์˜คํ”ˆํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ๋ฅผ ์—ด์—ˆ๋‹ค๋ฉด ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ์— ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์•„๋‚˜์ฝ˜๋‹ค ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์ „๋ถ€ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. > conda update -n base conda > conda update --all ์ด ์ฑ…์ด ์ž‘์„ฑ๋˜์—ˆ์„ ๋‹น์‹œ์—๋Š” ํŒŒ์ด์ฌ 3.7 ๋ฒ„์ „์ด ์ตœ์‹  ๋ฒ„์ „์ด์—ˆ์ง€๋งŒ, ๋…์ž๋ถ„๋“ค์ด ํŒŒ์ด์ฌ์„ ์„ค์น˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•„๋‚˜์ฝ˜๋‹ค ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜์˜€์„ ๋•Œ๋Š” 3.7๋ณด๋‹ค ๋”์šฑ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋˜์—ˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋ฌด์ž‘์ • ํŒŒ์ด์ฌ ์ตœ์‹  ๋ฒ„์ „์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์•„๋ž˜์˜ ๋งํฌ์—์„œ ํŒŒ์ด์ฌ ๋ฒ„์ „๊ณผ ํ˜ธํ™˜๋˜๋Š” ํ…์„œ ํ”Œ๋กœ ๋ฒ„์ „์— ๋Œ€ํ•œ ์•ˆ๋‚ด๊ฐ€ ๋‚˜์™€์žˆ์œผ๋‹ˆ ๋ฐ˜๋“œ์‹œ ์„ค์น˜ ์ „ ํ™•์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://www.tensorflow.org/install/pip? hl=ko ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ํŽ˜์ด์ง€์—์„œ 'Python 3.9 ์ง€์›์—๋Š” Tensorflow 2.5 ์ด์ƒ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.'๋ผ๊ณ  ๊ธฐ์žฌ๋ผ ์žˆ๋‹ค๋ฉด, ํŒŒ์ด์ฌ 3.9๋ฅผ ์„ค์น˜ํ•˜์˜€์„ ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ Tensorflow๋Š” 2.5 ์ด์ƒ์„ ์„ค์น˜ํ•ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. 2. ๊ตฌ๊ธ€์˜ ์ฝ”๋žฉ(Colab) ํ…์„œ ํ”Œ๋กœ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 64๋น„ํŠธ ํ”Œ๋žซํผ๋งŒ์„ ์ง€์›ํ•˜๋ฏ€๋กœ 32๋น„ํŠธ ํ™˜๊ฒฝ์—์„œ๋Š” ๋”ฅ ๋Ÿฌ๋‹ ์‹ค์Šต ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•˜๊ธฐ์—๋Š” ๋งŽ์€ ์• ๋กœ ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ๊ฐœ์ธ์˜ ์ปดํ“จํ„ฐ ์‚ฌ์–‘์ด๋‚˜ ๋‹ค๋ฅธ ์ด์œ ๋กœ ์•„๋‚˜์ฝ˜๋‹ค๋‚˜ ์—ฌ๋Ÿฌ ํŒจํ‚ค์ง€ ์„ค์น˜๊ฐ€ ์–ด๋ ค์šด ๊ฒฝ์šฐ๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ์ธํ„ฐ๋„ท๋งŒ ๋œ๋‹ค๋ฉด ๋ฐ”๋กœ ํŒŒ์ด์ฌ์„ ์‹ค์Šตํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ๊ธ€์˜ ์ฝ”๋žฉ(Colab)์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ๊ธ€์˜ Colab์€ ๋’ค์—์„œ ์„ค๋ช…ํ•˜๊ฒŒ ๋  '์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ'๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ์‹ค์Šต ํ™˜๊ฒฝ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Colab ์ฃผ์†Œ : https://colab.research.google.com/ ๊ตฌ๊ธ€์˜ Colab์— ์ ‘์†ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์œ„์˜ URL์„ ํ†ตํ•ด์„œ ์ ‘์†ํ•˜๊ฑฐ๋‚˜, ๊ตฌ๊ธ€(http://www.google.co.kr/)์—์„œ Colab์ด๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•ด์„œ ์ ‘์†ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1) ํŒŒ์ด์ฌ ์‹ค์Šตํ•˜๊ธฐ Colab ์‚ฌ์šฉ ์‹œ์—๋Š” ๊ตฌ๊ธ€ ๊ณ„์ •์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ๊ตฌ๊ธ€ ์•„์ด๋””๊ฐ€ ์—†์œผ์‹  ๋ถ„๋“ค์€ ๋จผ์ € ํšŒ์›๊ฐ€์ž… ํ›„ ๋กœ๊ทธ์ธ๋ถ€ํ„ฐ ํ•ด์ฃผ์„ธ์š”. ๋กœ๊ทธ์ธ ํ›„ ์ขŒ์ธก ์ƒ๋‹จ์—์„œ ํŒŒ์ผ > ์ƒˆ ๋…ธํŠธ๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ธˆ๋งŒ ๊ธฐ๋‹ค๋ฆฌ๋ฉด ํŒŒ์ด์ฌ์„ ์‹ค์Šตํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์Šต ํ™˜๊ฒฝ ์ฐฝ์ด ๋œจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด Colab์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ถ€๋ถ„์˜ ๋‹จ์œ„๋ฅผ '์…€'์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ ๋ณด์ด๋Š” ์ขŒ์ธก ์ƒ๋‹จ์˜ '+ ์ฝ”๋“œ' ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ƒˆ๋กœ์šด ์…€์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์…€์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  Shift + Enter ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์…€์— 3 + 5๋ผ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , Shift + Enter๋ฅผ ๋ˆ„๋ฅด๋ฉด 8์ด๋ผ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ขŒ์ธก์— [1]์€ ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ๋ช‡ ๋ฒˆ์งธ๋กœ ์‹คํ–‰๋˜์—ˆ๋Š”์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์…€์„ ์ถ”๊ฐ€ํ•ด ๋ณด๋ฉด์„œ ๋‹ค๋ฅธ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋„ ์ถ”๊ฐ€์ ์œผ๋กœ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”. 2) ๋ฌด๋ฃŒ๋กœ GPU ์‚ฌ์šฉํ•˜๊ธฐ ๋”ฅ ๋Ÿฌ๋‹์—์„œ๋Š” CPU๋ณด๋‹ค๋Š” GPU๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Colab์—์„œ ์‹ค์Šตํ•  ๋•Œ์˜ ์žฅ์ ์€ GPU๋ฅผ ๋ฌด๋ฃŒ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. GPU๊ฐ€ ์žฅ์ฐฉ๋œ ์ปดํ“จํ„ฐ๊ฐ€ ์—†๋Š” ๋”ฅ ๋Ÿฌ๋‹ ์ž…๋ฌธ์ž๋“ค์€ ํ–ฅํ›„ ์ด ์ฑ…์˜ ์‹ค์Šต์„ ์ง„ํ–‰ํ•  ๋•Œ Colab์—์„œ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋”ฅ ๋Ÿฌ๋‹์„ ๊ณต๋ถ€ํ•˜๋Š” ๊ฒƒ์„ ๊ฐ•ํ•˜๊ฒŒ ๊ถŒ์žฅ ๋“œ๋ฆฝ๋‹ˆ๋‹ค. GPU๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๋ฉด ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š” ์‹œ๊ฐ„์ด ์ง€๋‚˜์น˜๊ฒŒ ์†Œ์š”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Colab์—์„œ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ƒˆ ๋…ธํŠธ์— ์ง„์ž…ํ–ˆ์„ ๋•Œ ์ƒ๋‹จ์—์„œ ๋Ÿฐํƒ€์ž„ > ๋Ÿฐํƒ€์ž„ ์œ ํ˜• ๋ณ€๊ฒฝ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ๋…ธํŠธ ์„ค์ •์˜ ํ•˜๋“œ์›จ์–ด ๊ฐ€์†๊ธฐ > GPU๋ฅผ ์„ ํƒ ํ›„ ์ €์žฅ์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค. ์ดํ›„ ์‹ค์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 3) ํŒŒ์ผ ์—…๋กœ๋“œ ๊ตฌ๊ธ€์˜ Colab์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋กœ๋“œํ•˜์—ฌ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋กœ ์‹ค์Šต์„ ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, ์ขŒ์ธก ์ƒ๋‹จ์—์„œ ํด๋” ๋ชจ์–‘์˜ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ ์œ„ ๋ฐฉํ–ฅ์˜ ํ™”์‚ดํ‘œ(โ†‘)๊ฐ€ ๊ทธ๋ ค์ง„ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ์ˆซ์ž 1๋ฒˆ ๋ฒ„ํŠผ๊ณผ ์ˆซ์ž 2๋ฒˆ ๋ฒ„ํŠผ์ด ๊ฐ๊ฐ ์ด์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด test.txt ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•œ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์—…๋กœ๋“œ ํ›„์—๋Š” ํŒŒ์ผ ๋ชฉ๋ก์— test.txt ํŒŒ์ผ์ด ๋ณด์ž…๋‹ˆ๋‹ค. 01-02 ํ•„์š” ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ–ˆ๋‹ค๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ Numpy, Pandas, Jupyter notebook, scikit-learn, matplotlib, seaborn, nltk ๋“ฑ์ด ์ด๋ฏธ ์„ค์น˜๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์•„๋‚˜์ฝ˜๋‹ค์— ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์€ tensorflow, keras, gensim๊ณผ ๊ฐ™์€ ํŒจํ‚ค์ง€๋งŒ ๋ณ„๋„๋กœ pip๋ฅผ ํ†ตํ•ด ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ปดํ“จํ„ฐ์— ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ๋‹จ์ˆœํžˆ ํŒŒ์ด์ฌ๋งŒ ์„ค์น˜๋œ ์ƒํƒœ๋ผ๋ฉด ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๋ชจ๋“  ํŒจํ‚ค์ง€๋ฅผ pip๋กœ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์œˆ๋„ ํ™˜๊ฒฝ์„ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 1. ํ…์„œ ํ”Œ๋กœ(Tensorflow) ํ…์„œ ํ”Œ๋กœ๋Š” ๊ตฌ๊ธ€์ด 2015๋…„์— ๊ณต๊ฐœํ•œ ๋จธ์‹  ๋Ÿฌ๋‹ ์˜คํ”ˆ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹๊ณผ ๋”ฅ ๋Ÿฌ๋‹์„ ์ง๊ด€์ ์ด๊ณ  ์†์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋’ค์˜ ๋”ฅ ๋Ÿฌ๋‹ ์‹ค์Šต์„ ์œ„ํ•ด์„œ ํ…์„œ ํ”Œ๋กœ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ(Anaconda Prompt) ๋˜๋Š” ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋ฅผ ํ†ตํ•ด์„œ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ๋ฅผ ์—ด์—ˆ๋‹ค๋ฉด ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ์— ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ํ…์„œ ํ”Œ๋กœ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. > pip install tensorflow ์ด์ œ ipython ์‰˜์„ ์‹คํ–‰ํ•˜์—ฌ ํ…์„œ ํ”Œ๋กœ๊ฐ€ ์ •์ƒ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์˜๋ฏธ์—์„œ ํ…์„œ ํ”Œ๋กœ๋ฅผ ์ž„ํฌํŠธํ•˜๊ณ  ๋ฒ„์ „์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. > ipython ... In [1]: import tensorflow as tf In [2]: tf.__version__ Out[2]: '2.0.0' ํ…์„œ ํ”Œ๋กœ 2.0์ด ์„ค์น˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ์ œ ์ปดํ“จํ„ฐ ํ™”๋ฉด์˜ ์Šคํฌ๋ฆฐ์ˆ์„ ์•„๋ž˜์— ์ฒจ๋ถ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์‰˜์„ ๋‚˜์˜ฌ ๋•Œ๋Š” exit๋ผ๋Š” ์ปค๋งจ๋“œ๋กœ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํŒจํ‚ค์ง€๋“ค๋„ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์„ค์น˜ ๋ฐ ์ •์ƒ์ ์œผ๋กœ ์„ค์น˜๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ๋ฒ„์ „์„ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ €์ž๊ฐ€ ๊ฐ ํŒจํ‚ค์ง€์˜ ๋ฒ„์ „๋“ค์„ ๊ธฐ์žฌํ•˜๋Š” ์ด์œ ๋Š” ์ €์ž๊ฐ€ ํ•ด๋‹น ๋ฒ„์ „์œผ๋กœ ์‹ค์Šตํ–ˆ์œผ๋ฏ€๋กœ ์ฐธ๊ณ ํ•˜๋ผ๋Š” ์˜๋ฏธ์—์„œ ๊ณต๊ฐœํ•˜๋Š” ๊ฒƒ์ด์ง€, ๋…์ž๊ฐ€ ๋” ๋†’์€ ๋ฒ„์ „์ž„์—๋„ ์ €์ž๊ฐ€ ๊ณต๊ฐœํ•œ ๋ฒ„์ „๋“ค๊ณผ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ํ…์„œ ํ”Œ๋กœ๋Š” ์ฃผ๋กœ tf๋ผ๋Š” ๋ช…์นญ์œผ๋กœ ์ž„ํฌํŠธ ํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ก€์ž…๋‹ˆ๋‹ค. 2. ์ผ€๋ผ์Šค(Keras) ์ผ€๋ผ์Šค(Keras)๋Š” ๋”ฅ ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ์ธ ํ…์„œ ํ”Œ๋กœ์— ๋Œ€ํ•œ ์ถ”์ƒํ™”๋œ API๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค๋Š” ๋ฐฑ์—”๋“œ๋กœ ํ…์„œ ํ”Œ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ข€ ๋” ์‰ฝ๊ฒŒ ๋”ฅ ๋Ÿฌ๋‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด, ํ…์„œ ํ”Œ๋กœ ์ฝ”๋“œ๋ฅผ ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. > pip install keras ์ผ€๋ผ์Šค๋ฅผ ์„ค์น˜ ํ›„์— ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ํ…์„œ ํ”Œ๋กœ์—์„œ ์ผ€๋ผ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ๋Š” ์ˆœ์ˆ˜ ์ผ€๋ผ์Šค๋ฅผ keras๋ผ๊ณ  ํ‘œ๊ธฐํ•œ๋‹ค๋ฉด, ํ…์„œ ํ”Œ๋กœ์—์„œ ์ผ€๋ผ์Šค API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” tf.keras๋ผ๊ณ  ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐ€์ง€๋Š” ์‹ค์ œ๋กœ ๋ฌธ๋ฒ•๋„ ๋งŽ์€ ๋ฉด์—์„œ ๊ฐ™์•„์„œ keras ์ฝ”๋“œ๋ฅผ tf.keras๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฑด ์•„์ฃผ ์‰ฝ๊ณ , keras๋ฅผ ํ•™์Šตํ•˜์˜€๋‹ค๋ฉด tf.keras๋„ ๊ธˆ๋ฐฉ ์ต์ˆ™ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค ๊ฐœ๋ฐœ์ž์ธ ํ”„๋ž‘์ˆ˜์•„ ์ˆ„๋ ˆ(Franรงois Chollet)๋Š” ์•ž์œผ๋กœ๋Š” keras๋ณด๋‹ค๋Š” tf.keras๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋„ ์ฃผ๋กœ tf.keras๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. > ipython ... In [1]: import keras In [2]: keras.__version__ Out[2]: '2.3.1' 3. ์  ์‹ฌ(Gensim) ์  ์‹ฌ(Gensim)์€ ๋จธ์‹  ๋Ÿฌ๋‹์„ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํ”ฝ ๋ชจ๋ธ๋ง๊ณผ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋“ฑ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์˜คํ”ˆ ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋„ ์  ์‹ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ Word2Vec ๋“ฑ ๋‹ค์–‘ํ•œ ๋ชจ๋ธ๋“ค์„ ํ•™์Šตํ•ด ๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. > pip install gensim > ipython ... In [1]: import gensim In [2]: gensim.__version__ Out[2]: '3.8.1' 4. ์‚ฌ์ดํ‚ท๋Ÿฐ(Scikit-learn) ์‚ฌ์ดํ‚ท๋Ÿฐ(Scikit-learn)์€ ํŒŒ์ด์ฌ ๋จธ์‹ ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์„ ํ†ตํ•ด ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜, ์„œํฌํŠธ ๋ฒกํ„ฐ ๋จธ์‹  ๋“ฑ ๋‹ค์–‘ํ•œ ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์‚ฌ์ดํ‚ท๋Ÿฐ์—๋Š” ๋จธ์‹ ๋Ÿฌ๋‹์„ ์—ฐ์Šตํ•˜๊ธฐ ์œ„ํ•œ ์•„์ด๋ฆฌ์Šค ๋ฐ์ดํ„ฐ, ๋‹น๋‡จ๋ณ‘ ๋ฐ์ดํ„ฐ ๋“ฑ ์ž์ฒด ๋ฐ์ดํ„ฐ ๋˜ํ•œ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์€ ์œ„ ํŒจํ‚ค์ง€๋“ค๊ณผ ๋‹ฌ๋ฆฌ ์•„๋‚˜์ฝ˜๋‹ค๋กœ ์ž๋™ ์„ค์น˜๋˜์ง€๋งŒ ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ Scikit-learn์„ ๋ณ„๋„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. > pip install scikit-learn > ipython ... In [1]: import sklearn In [2]: sklearn.__version__ Out[2]: '0.21.3' 5. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ(Jupyter Notebook) ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์€ ์›น์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์˜คํ”ˆ์†Œ์Šค ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ž…๋‹ˆ๋‹ค. ์ด ์ฑ…์˜ ๋ชจ๋“  ์ฝ”๋“œ๋“ค์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณธ์ธ์˜ ์ปดํ“จํ„ฐ์— ์„ค์น˜๋œ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ ๋˜๋Š” ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ๊ณผ ์‹ค์Šต ํ™˜๊ฒฝ์ด ์œ ์‚ฌํ•œ ๊ตฌ๊ธ€์˜ ์ฝ”๋žฉ(Colab)์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ๋„ ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜๋ฉด ์ž๋™์œผ๋กœ ์„ค์น˜๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ Jupyter notebook์„ ๋ณ„๋„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. > pip install jupyter ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฉด ํ”„๋กฌํ”„ํŠธ์—์„œ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. > jupyter notebook ํ•ด๋‹น ๋ช…๋ น์–ด๋ฅผ ์น˜๋ฉด ์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ž๋™์œผ๋กœ ์—ด๋ฆฌ๋ฉด์„œ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ์ง์ ‘ ์‹คํ–‰์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์›น ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ด๊ณ  ํ”„๋กฌํ”„ํŠธ์—์„œ ๋‚˜์˜ค๊ณ  ์žˆ๋Š” ์ฃผ์†Œ์ธ "localhost:8888"์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค. 1) ์ƒˆ๋กœ์šด ๋…ธํŠธ ์‹คํ–‰ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ๋Š” ๋…ธํŠธ๋ฅผ ์ƒ์„ฑํ•ด์„œ ํ•ด๋‹น ๋…ธํŠธ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ™”๋ฉด ์šฐ์ธก์˜ New ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ , Python3์„ ๋ˆŒ๋Ÿฌ์„œ ์ƒˆ๋กœ์šด ๋…ธํŠธ๋ฅผ ์‹คํ–‰ํ•ด ๋ด…์‹œ๋‹ค. 2) ์…€์— ์ฝ”๋“œ ์ž‘์„ฑํ•ด ๋ณด๊ธฐ ๋…ธํŠธ๊ฐ€ ์‹คํ–‰๋˜๋ฉด In [ ]์ด๋ผ๋Š” ๋ฌธ์ž๊ฐ€ ์ ํžŒ ํ…์ŠคํŠธ ์ƒ์ž๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ๋Š” ํ•ด๋‹น ํ…์ŠคํŠธ ์ƒ์ž์˜ ๋‹จ์œ„๋ฅผ ์…€(cell)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ํ•ด๋‹น ์…€์— ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  [Cell] โ†’ [Run Cells]๋ฅผ ํด๋ฆญํ•˜๋ฉด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋งˆ์šฐ์Šค๋กœ ์ผ์ผ์ด ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋ฒˆ๊ฑฐ๋กญ๊ฒŒ ๋Š๊ปด์ง„๋‹ค๋ฉด ํ‚ค๋ณด๋“œ์˜ Shift + Enter๋ฅผ ํ†ตํ•ด์„œ ํ˜„์žฌ ์…€ ์‹คํ–‰ ํ›„ ๋‹ค์Œ ์…€๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ตฌ๊ธ€์˜ Colab์—์„œ ์„ค๋ช…ํ–ˆ๋˜ ์‹คํ–‰ ๋ฐฉ์‹๊ณผ ๋™์ผํ•œ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. 01-03 ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ NLTK์™€ KoNLPy ์„ค์น˜ํ•˜๊ธฐ ๋‹ค์Œ ์ฑ•ํ„ฐ์ธ ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ(Text preprocessing) ์ฑ•ํ„ฐ์—์„œ๋Š” ์ „์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์ด๋ก ์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜๊ณ , ๊ทธ ์ด๋ก ์„ ๋ฐ”ํƒ•์œผ๋กœ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์‹ค์Šต์— ํ•„์š”ํ•œ ๊ธฐ๋ณธ์ ์ธ ์ž์—ฐ์–ด ํŒจํ‚ค์ง€๋“ค์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 1. NLTK์™€ NLTK Data ์„ค์น˜ ์—”์—˜ํ‹ฐ์ผ€์ด(NLTK)๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์˜€๋‹ค๋ฉด NLTK๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์น˜๊ฐ€ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ NLTK๋ฅผ ๋ณ„๋„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. > pip install nltk > ipython ... In [1]: import nltk In [2]: nltk.__version__ Out[2]: '3.4.5' NLTK์˜ ๊ธฐ๋Šฅ์„ ์ œ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” NLTK Data๋ผ๋Š” ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ํŒŒ์ด์ฌ ์ฝ”๋“œ ๋‚ด์—์„œ import nltk ์ดํ›„์— nltk.download()๋ผ๋Š” ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. In [3]: nltk.download() ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ ํ›„์— NLTK ์‹ค์Šต์— ํ•„์š”ํ•œ ๊ฐ์ข… ํŒจํ‚ค์ง€์™€ ์ฝ”ํผ์Šค๋ฅผ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ต์นญํ•˜์—ฌ NLTK Data๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, NLTK ์‹ค์Šต์„ ์ˆ˜ํ–‰ํ•˜๋˜ ๋„์ค‘์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ์•„๋ž˜์˜ 2๋ฒˆ๊ณผ 3๋ฒˆ ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. 2. NLTK Data๊ฐ€ ์„ค์น˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ NLTK๋Š” ๊ฐ ์‹ค์Šต๋งˆ๋‹ค ํ•„์š”ํ•œ NLTK Data๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น ์‹ค์Šต์— ํ•„์š”ํ•œ NLTK Data๊ฐ€ ์„ค์น˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ์ฝ”๋“œ ์‹คํ–‰ ์‹œ์— ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ๊ณ ๋ฌธ์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. LookupError: ********************************************************************** Resource treebank not found. Please use the NLTK Downloader to obtain the resource: >>> import nltk >>> nltk.download('treebank') ********************************************************************** ์œ„์˜ ๊ฒฝ์šฐ์—๋Š” NLTK Data ์ค‘์—์„œ 'treebank'๋ผ๋Š” ๋ฆฌ์†Œ์Šค๊ฐ€ ์„ค์น˜๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์œ„์˜ ์•ˆ๋‚ด์ฒ˜๋Ÿผ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ ๋˜๋Š” iPython ์‰˜ ์•ˆ์—์„œ ๋™์ผํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. In [1]: import nltk In [2]: nltk.download('treebank') 3. NLTK Data ์„ค์น˜ ์‹œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ๋งํฌ : https://github.com/nltk/nltk_data ์„ค์น˜ ์‹œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด, ์ˆ˜๋™ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•˜์—ฌ ์ •ํ•ด์ง„ ๊ฒฝ๋กœ์— ์œ„์น˜์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜๋™ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋Š” nltk_data์˜ ๊นƒํ—ˆ๋ธŒ ์ฃผ์†Œ์™€ nltk_data ๊ณต์‹ ์‚ฌ์ดํŠธ ๋‘ ๊ณณ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  nltk_data์˜ ๊นƒํ—ˆ๋ธŒ ์‚ฌ์ดํŠธ์˜ ๋งํฌ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œ„ ๋งํฌ์˜ packages ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ํ•„์š”ํ•œ nltk_data ํŒŒ์ผ๋“ค์„ ๋ชจ๋‘ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ํ† ํฐํ™” ์ž‘์—…์„ ์œ„ํ•ด 'punkt' ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด, nltk_data/packages/tokenizer ๊ฒฝ๋กœ์—์„œ punkt.zip ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค์„ ๋‹ค์šด๋กœ๋“œํ•œ ํ›„, ๊ฐ O/S ๋ณ„ ์ •ํ•ด์ง„ ๊ฒฝ๋กœ์— ์œ„์น˜์‹œํ‚ต๋‹ˆ๋‹ค. ๊ฐ O/S ๋ณ„ ์ •ํ•ด์ง„ ๊ฒฝ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œˆ๋„ : C:/nltk_data ๋˜๋Š” D:/nltk_data UNIX : /usr/local/share/nltk_data/ ๋˜๋Š” /usr/share/nltk_data ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” packages ๋””๋ ‰ํ„ฐ๋ฆฌ ์ „์ฒด๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ๊ฒฝ๋กœ์— ์œ„์น˜์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : http://www.nltk.org/nltk_data/ ์ˆ˜๋™ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ๋กœ nltk_data ๊ณต์‹ ์‚ฌ์ดํŠธ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฐํ™” ์ž‘์—…์ด ํ•„์š”ํ•œ ์ƒํ™ฉ์ด๋ผ๊ณ  ๋‹ค์‹œ ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์œ„ ๋งํฌ๋กœ ์ด๋™ํ•ด CTRL + F๋ฅผ ๋ˆŒ๋Ÿฌ 'tokenizer'๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ , ๊ฒ€์ƒ‰์— ๋‚˜์˜ค๋Š” 106. Punkt Tokenizer Models [ download | source ] ํ•ด๋‹น ์ค„์„ ์ฐพ์€ ๋’ค, download ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด punkt.zip ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ํ›„ ์œ„์น˜์‹œ์ผœ์•ผ ํ•˜๋Š” ์•Œ๋งž์€ ๊ฒฝ๋กœ๋Š” ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๊ฒฝ๋กœ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. 4. KoNLPy ์„ค์น˜ ์ฝ”์—”์—˜ํŒŒ์ด(KoNLPy)๋Š” ํ•œ๊ตญ์–ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ํ”„๋กฌํ”„ํŠธ์—์„œ ์•„๋ž˜ ์ปค๋งจ๋“œ๋กœ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. > pip install konlpy > ipython ... In [1]: import konlpy In [2]: konlpy.__version__ Out[2]: '0.5.1' 5. ์œˆ๋„์—์„œ KoNLPy ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ์œˆ๋„์—์„œ KoNLPy๋ฅผ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ์‹คํ–‰ ์‹œ JDK ๊ด€๋ จ ์˜ค๋ฅ˜๋‚˜ JPype ์˜ค๋ฅ˜์— ๋ถ€๋”ชํžˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” KoNLPy๊ฐ€ JAVA๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ ์˜ค๋ฅ˜ ํ•ด๊ฒฐ์„ ์œ„ํ•ด์„œ๋Š” JDK 1.7 ์ด์ƒ์˜ ๋ฒ„์ „๊ณผ JPype๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1) JDK ์„ค์น˜ ์šฐ์„  JDK๋ฅผ 1.7 ๋ฒ„์ „ ์ด์ƒ์œผ๋กœ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜ ์ฃผ์†Œ : https://www.oracle.com/technetwork/java/javase/downloads/index.html ์„ค์น˜ํ•œ ํ›„์—๋Š” JDK๊ฐ€ ์„ค์น˜๋œ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ €์ž์˜ ๊ฒฝ์šฐ์—๋Š” jdk๊ฐ€ ์•„๋ž˜์˜ ๊ฒฝ๋กœ์— ์„ค์น˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฝ๋กœ : C:\Program Files\Java\jdk-11.0.1 11.0.1๊ณผ ๊ฐ™์ด ๋ฒ„์ „์— ๋Œ€ํ•œ ์ˆซ์ž๋Š” ์–ด๋–ค ๋ฒ„์ „์„ ์„ค์น˜ํ–ˆ๋Š๋ƒ์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) JDK ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์น˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•˜๋‹ค๋ฉด ํ•ด๋‹น ๊ฒฝ๋กœ๋ฅผ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฒฝ๋กœ๋ฅผ ์œˆ๋„ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์œˆ๋„ 10๊ธฐ์ค€) ์ œ์–ดํŒ > ์‹œ์Šคํ…œ ๋ฐ ๋ณด์•ˆ > ์‹œ์Šคํ…œ > ๊ณ ๊ธ‰ ์‹œ์Šคํ…œ ์„ค์ • > ๊ณ ๊ธ‰ > ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ƒˆ๋กœ ๋งŒ๋“ค๊ธฐ(N)...๋ฅผ ๋ˆ„๋ฅด๊ณ  JAVA_HOME์ด๋ผ๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ™˜๊ฒฝ ๋ณ€์ˆ˜์˜ ๊ฐ’์€ ์•ž์„œ ์ฐพ์•˜๋˜ jdk ์„ค์น˜ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค. ์ด์ œ KoNLPy๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ JDK ์„ค์ •์„ ๋งˆ์ณค์Šต๋‹ˆ๋‹ค. 3) JPype ์„ค์น˜ ์ด์ œ JAVA์™€ Python์„ ์—ฐ๊ฒฐํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” JPype๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜ ์ฃผ์†Œ : https://github.com/jpype-project/jpype/releases ํ•ด๋‹น ๋งํฌ์—์„œ Assets๋ผ๊ณ  ๊ธฐ์žฌ๋œ ๊ณณ์—์„œ ์ ์ ˆํ•œ ๋ฒ„์ „์„ ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š”๋ฐ cp27์€ ํŒŒ์ด์ฌ 2.7, cp36์€ ํŒŒ์ด์ฌ 3.6์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ €์ž๊ฐ€ ์ฑ…์„ ์ง‘ํ•„ํ•  ๋‹น์‹œ์—๋Š” ํŒŒ์ด์ฌ 3.6์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ์œผ๋ฏ€๋กœ cp36์ด๋ผ๊ณ  ์ ํžŒ JPype๋ฅผ ์„ค์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ ์‚ฌ์šฉํ•˜๋Š” ์œˆ๋„ O/S๊ฐ€ 32๋น„ํŠธ์ธ์ง€, 64๋น„ํŠธ์ธ์ง€์— ๋”ฐ๋ผ์„œ ์„ค์น˜ JPype๊ฐ€ ๋‹ค๋ฅธ๋ฐ, ์œˆ๋„ 32๋น„ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด win32๋ฅผ, ์œˆ๋„ 64๋น„ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด win_amd64๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํŒŒ์ด์ฌ 3.6, ์œˆ๋„ 64๋น„ํŠธ๋ฅผ ์‚ฌ์šฉ ์ค‘์ด๋ผ๋ฉด JPype1-0.6.3-cp36-cp36m-win_amd64.whl๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กฌํ”„ํŠธ์—์„œ ํ•ด๋‹น ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•˜์—ฌ ์•„๋ž˜ ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. > pip install JPype1-0.6.3-cp36-cp36m-win_amd64.whl ์ด์ œ JPype์˜ ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด, KoNLPy๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  : KoNLPy ์ˆ˜ํ–‰ ์‹œ ์ž๋ฐ” ์˜ค๋ฅ˜๋Š”, ํŒŒ์ด์ฌ bit์™€ ์ž๋ฐ” bit๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋„ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ํ™•์‹คํ•œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ์„ค์น˜๋œ ์ž๋ฐ”๋ฅผ ์ „๋ถ€<NAME>๊ณ  ์ตœ์‹  ๋ฒ„์ „(JRE, JDK)์œผ๋กœ ์ƒˆ๋กœ ๊น”๋ฉด ๋Œ€๋ถ€๋ถ„ ํ•ด๊ฒฐ๋ฉ๋‹ˆ๋‹ค. 01-04 ํŒ๋‹ค์Šค(Pandas) and ๋„˜ํŒŒ์ด(Numpy) and ๋งทํ”Œ๋กญ๋ฆฝ(Matplotlib) ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•œ ํ•„์ˆ˜ ํŒจํ‚ค์ง€ ์‚ผ๋Œ€์žฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ Pandas์™€ Numpy ๊ทธ๋ฆฌ๊ณ  Matplotlib์ž…๋‹ˆ๋‹ค. ์„ธ ๊ฐœ์˜ ํŒจํ‚ค์ง€ ๋ชจ๋‘ ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ–ˆ๋‹ค๋ฉด ์ถ”๊ฐ€ ์„ค์น˜ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„ธ ๊ฐœ์˜ ํŒจํ‚ค์ง€๋ฅผ ๊ฐ„๋‹จํžˆ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. 1. ํŒ๋‹ค์Šค(Pandas) ํŒ๋‹ค์Šค(Pandas)๋Š” ํŒŒ์ด์ฌ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์„ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ๋ถ„์„๊ณผ ๊ฐ™์€ ์ž‘์—…์—์„œ ํ•„์ˆ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š” Pandas ๋งํฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋งํฌ : http://pandas.pydata.org/pandas-docs/stable/ ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ Pandas๋ฅผ ๋ณ„๋„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pip install pandas > ipython ... In [1]: import pandas as pd In [2]: pd.__version__ Out[2]: '0.25.1' Pandas์˜ ๊ฒฝ์šฐ pd๋ผ๋Š” ๋ช…์นญ์œผ๋กœ ์ž„ํฌํŠธ ํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ก€์ž…๋‹ˆ๋‹ค. import pandas as pd Pandas๋Š” ์ด ์„ธ ๊ฐ€์ง€์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‹œ๋ฆฌ์ฆˆ(Series) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„(DataFrame) ํŒจ๋„(Panel) ์ด ์ค‘ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์ด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋ฉฐ ์—ฌ๊ธฐ์„œ๋Š” ์‹œ๋ฆฌ์ฆˆ์™€ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 1) ์‹œ๋ฆฌ์ฆˆ(Series) ์‹œ๋ฆฌ์ฆˆ ํด๋ž˜์Šค๋Š” 1์ฐจ์› ๋ฐฐ์—ด์˜ ๊ฐ’(values)์— ๊ฐ ๊ฐ’์— ๋Œ€์‘๋˜๋Š” ์ธ๋ฑ์Šค(index)๋ฅผ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. sr = pd.Series([17000, 18000, 1000, 5000], index=["ํ”ผ์ž", "์น˜ํ‚จ", "์ฝœ๋ผ", "๋งฅ์ฃผ"]) print('์‹œ๋ฆฌ์ฆˆ ์ถœ๋ ฅ :') print('-'*15) print(sr) ์‹œ๋ฆฌ์ฆˆ ์ถœ๋ ฅ : --------------- ํ”ผ์ž 17000 ์น˜ํ‚จ 18000 ์ฝœ๋ผ 1000 ๋งฅ์ฃผ 5000 dtype: int64 ๊ฐ’(values)๊ณผ ์ธ๋ฑ์Šค(index)๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. print('์‹œ๋ฆฌ์ฆˆ์˜ ๊ฐ’ : {}'.format(sr.values)) print('์‹œ๋ฆฌ์ฆˆ์˜ ์ธ๋ฑ์Šค : {}'.format(sr.index)) ์‹œ๋ฆฌ์ฆˆ์˜ ๊ฐ’ : [17000 18000 1000 5000] ์‹œ๋ฆฌ์ฆˆ์˜ ์ธ๋ฑ์Šค : Index(['ํ”ผ์ž', '์น˜ํ‚จ', '์ฝœ๋ผ', '๋งฅ์ฃผ'], dtype='object') 2) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„(DataFrame) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์€ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. 2์ฐจ์›์ด๋ฏ€๋กœ ํ–‰๋ฐฉํ–ฅ ์ธ๋ฑ์Šค(index)์™€ ์—ด ๋ฐฉํ–ฅ ์ธ๋ฑ์Šค(column)๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ–‰๊ณผ ์—ด์„ ๊ฐ€์ง€๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์‹œ๋ฆฌ์ฆˆ๊ฐ€ ์ธ๋ฑ์Šค(index)์™€ ๊ฐ’(values)์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค๋ฉด, ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์€ ์—ด(columns)๊นŒ์ง€ ์ถ”๊ฐ€๋˜์–ด ์—ด(columns), ์ธ๋ฑ์Šค(index), ๊ฐ’(values)์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด ์„ธ ๊ฐœ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ƒ์„ฑํ•ด ๋ด…์‹œ๋‹ค. values = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] index = ['one', 'two', 'three'] columns = ['A', 'B', 'C'] df = pd.DataFrame(values, index=index, columns=columns) print('๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ถœ๋ ฅ :') print('-'*18) print(df) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ถœ๋ ฅ : ------------------ A B C one 1 2 3 two 4 5 6 three 7 8 9 ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ๋ถ€ํ„ฐ ์ธ๋ฑ์Šค(index), ๊ฐ’(values), ์—ด(columns)์„ ๊ฐ๊ฐ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์ธ๋ฑ์Šค : {}'.format(df.index)) print('๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์—ด์ด๋ฆ„: {}'.format(df.columns)) print('๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ๊ฐ’ :') print('-'*18) print(df.values) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์ธ๋ฑ์Šค : Index(['one', 'two', 'three'], dtype='object') ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์—ด์ด๋ฆ„: Index(['A', 'B', 'C'], dtype='object') ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ๊ฐ’ : ------------------ [[1 2 3] [4 5 6] [7 8 9]] 3) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์ƒ์„ฑ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์€ ๋ฆฌ์ŠคํŠธ(List), ์‹œ๋ฆฌ์ฆˆ(Series), ๋”•์…”๋„ˆ๋ฆฌ(dict), Numpy์˜ ndarrays, ๋˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฆฌ์ŠคํŠธ์™€ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. # ๋ฆฌ์ŠคํŠธ๋กœ ์ƒ์„ฑํ•˜๊ธฐ data = [ ['1000', 'Steve', 90.72], ['1001', 'James', 78.09], ['1002', 'Doyeon', 98.43], ['1003', 'Jane', 64.19], ['1004', 'Pilwoong', 81.30], ['1005', 'Tony', 99.14], ] df = pd.DataFrame(data) print(df) 0 1 2 0 1000 Steve 90.72 1 1001 James 78.09 2 1002 Doyeon 98.43 3 1003 Jane 64.19 4 1004 Pilwoong 81.30 5 1005 Tony 99.14 ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์—ด(columns)์„ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ด์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ณ  ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. df = pd.DataFrame(data, columns=['ํ•™๋ฒˆ', '์ด๋ฆ„', '์ ์ˆ˜']) print(df) ํ•™๋ฒˆ ์ด๋ฆ„ ์ ์ˆ˜ 0 1000 Steve 90.72 1 1001 James 78.09 2 1002 Doyeon 98.43 3 1003 Jane 64.19 4 1004 Pilwoong 81.30 5 1005 Tony 99.14 ํŒŒ์ด์ฌ ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜์ธ ๋”•์…”๋„ˆ๋ฆฌ(dictionary)๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ƒ์„ฑํ•˜๊ธฐ data = { 'ํ•™๋ฒˆ' : ['1000', '1001', '1002', '1003', '1004', '1005'], '์ด๋ฆ„' : [ 'Steve', 'James', 'Doyeon', 'Jane', 'Pilwoong', 'Tony'], '์ ์ˆ˜': [90.72, 78.09, 98.43, 64.19, 81.30, 99.14] } df = pd.DataFrame(data) print(df) ํ•™๋ฒˆ ์ด๋ฆ„ ์ ์ˆ˜ 0 1000 Steve 90.72 1 1001 James 78.09 2 1002 Doyeon 98.43 3 1003 Jane 64.19 4 1004 Pilwoong 81.30 5 1005 Tony 99.14 4) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์กฐํšŒํ•˜๊ธฐ ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋Š” ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์—์„œ ์›ํ•˜๋Š” ๊ตฌ๊ฐ„๋งŒ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์–ด๋กœ์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. df.head(n) - ์•ž ๋ถ€๋ถ„์„ n ๊ฐœ๋งŒ ๋ณด๊ธฐ df.tail(n) - ๋’ท๋ถ€๋ถ„์„ n ๊ฐœ๋งŒ ๋ณด๊ธฐ df['์—ด ์ด๋ฆ„'] - ํ•ด๋‹น๋˜๋Š” ์—ด์„ ํ™•์ธ ์œ„์—์„œ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. # ์•ž ๋ถ€๋ถ„์„ 3๊ฐœ๋งŒ ๋ณด๊ธฐ print(df.head(3)) ํ•™๋ฒˆ ์ด๋ฆ„ ์ ์ˆ˜ 0 1000 Steve 90.72 1 1001 James 78.09 2 1002 Doyeon 98.43 # ๋’ท๋ถ€๋ถ„์„ 3๊ฐœ๋งŒ ๋ณด๊ธฐ print(df.tail(3)) ํ•™๋ฒˆ ์ด๋ฆ„ ์ ์ˆ˜ 3 1003 Jane 64.19 4 1004 Pilwoong 81.30 5 1005 Tony 99.14 # 'ํ•™๋ฒˆ'์— ํ•ด๋‹น๋˜๋Š” ์—ด์„ ๋ณด๊ธฐ print(df['ํ•™๋ฒˆ']) 0 1000 1 1001 2 1002 3 1003 4 1004 5 1005 Name: ํ•™๋ฒˆ, dtype: object 5) ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ Pandas๋Š” CSV, ํ…์ŠคํŠธ, Excel, SQL, HTML, JSON ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ์ฝ๊ณ  ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด csv ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ๋Š” pandas.read_csv()๋ฅผ ํ†ตํ•ด ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ example.csv ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. df = pd.read_csv('example.csv') print(df) student id name score 0 1000 Steve 90.72 1 1001 James 78.09 2 1002 Doyeon 98.43 3 1003 Jane 64.19 4 1004 Pilwoong 81.30 5 1005 Tony 99.14 ์ด ๊ฒฝ์šฐ ์ธ๋ฑ์Šค๊ฐ€ ์ž๋™์œผ๋กœ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(df.index) RangeIndex(start=0, stop=6, step=1) 2. ๋„˜ํŒŒ์ด(Numpy) ๋„˜ํŒŒ์ด(Numpy)๋Š” ์ˆ˜์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. Numpy์˜ ํ•ต์‹ฌ์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๋‹ค์ฐจ์› ํ–‰๋ ฌ ์ž๋ฃŒ๊ตฌ์กฐ์ธ ndarray๋ฅผ ํ†ตํ•ด ๋ฒกํ„ฐ ๋ฐ ํ–‰๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ์„ ํ˜• ๋Œ€์ˆ˜ ๊ณ„์‚ฐ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Numpy๋Š” ํŽธ์˜์„ฑ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์†๋„ ๋ฉด์—์„œ๋„ ์ˆœ์ˆ˜ ํŒŒ์ด์ฌ์— ๋น„ํ•ด ์••๋„์ ์œผ๋กœ ๋น ๋ฅด๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ Numpy๋ฅผ ๋ณ„๋„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pip install numpy > ipython ... In [1]: import numpy as np In [2]: np.__version__ Out[2]: '1.16.5' Numpy์˜ ๊ฒฝ์šฐ np๋ผ๋Š” ๋ช…์นญ์œผ๋กœ ์ž„ํฌํŠธ ํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ก€์ž…๋‹ˆ๋‹ค. import numpy as np 1) np.array() Numpy์˜ ํ•ต์‹ฌ์€ ndarray์ž…๋‹ˆ๋‹ค. np.array()๋Š” ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ndarray๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  1์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # 1์ฐจ์› ๋ฐฐ์—ด vec = np.array([1, 2, 3, 4, 5]) print(vec) [1 2 3 4 5] 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ array() ์•ˆ์— ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋งŒ ๋“ค์–ด๊ฐ€๋ฏ€๋กœ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋„ฃ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # 2์ฐจ์› ๋ฐฐ์—ด mat = np.array([[10, 20, 30], [ 60, 70, 80]]) print(mat) [[10 20 30] [60 70 80]] ๋‘ ๋ฐฐ์—ด์˜ ํƒ€์ž…์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('vec์˜ ํƒ€์ž… :',type(vec)) print('mat์˜ ํƒ€์ž… :',type(mat)) vec์˜ ํƒ€์ž… : <class 'numpy.ndarray'> mat์˜ ํƒ€์ž… : <class 'numpy.ndarray'> ๋™์ผํ•˜๊ฒŒ ํƒ€์ž…์ด numpy.ndarray๋ผ๊ณ  ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Numpy ๋ฐฐ์—ด์—๋Š” ์ถ•์˜ ๊ฐœ์ˆ˜(ndim)์™€ ํฌ๊ธฐ(shape)๋ผ๋Š” ๊ฐœ๋…์ด ์กด์žฌํ•˜๋Š”๋ฐ, ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ์ •ํ™•ํžˆ ์ˆ™์ง€ํ•˜๋Š” ๊ฒƒ์€ ๋”ฅ ๋Ÿฌ๋‹์—์„œ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ถ•์˜ ๊ฐœ์ˆ˜์™€ ํฌ๊ธฐ๊ฐ€ ์–ด๋–ค ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ์—์„œ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ์„ค๋ช…ํ•  ๋•Œ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. print('vec์˜ ์ถ•์˜ ๊ฐœ์ˆ˜ :',vec.ndim) # ์ถ•์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ print('vec์˜ ํฌ๊ธฐ(shape) :',vec.shape) # ํฌ๊ธฐ ์ถœ๋ ฅ vec์˜ ์ถ•์˜ ๊ฐœ์ˆ˜ : 1 vec์˜ ํฌ๊ธฐ(shape) : (5, ) print('mat์˜ ์ถ•์˜ ๊ฐœ์ˆ˜ :',mat.ndim) # ์ถ•์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ print('mat์˜ ํฌ๊ธฐ(shape) :',mat.shape) # ํฌ๊ธฐ ์ถœ๋ ฅ mat์˜ ์ถ•์˜ ๊ฐœ์ˆ˜ : 2 mat์˜ ํฌ๊ธฐ(shape) : (2, 3) 2) ndarray์˜ ์ดˆ๊ธฐํ™” ์œ„์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ndarray๋ฅผ ์ƒ์„ฑํ–ˆ์ง€๋งŒ ndarray๋ฅผ ๋งŒ๋“œ๋Š” ๋‹ค์–‘ํ•œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•˜๋ฏ€๋กœ ํ•„์š”์— ๋”ฐ๋ผ์„œ ๋‹ค์–‘ํ•œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. np.zeros()๋Š” ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์›์†Œ์— 0์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋“  ๊ฐ’์ด 0์ธ 2x3 ๋ฐฐ์—ด ์ƒ์„ฑ. zero_mat = np.zeros((2,3)) print(zero_mat) [[0. 0. 0.] [0. 0. 0.]] np.ones()๋Š” ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์›์†Œ์— 1์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋“  ๊ฐ’์ด 1์ธ 2x3 ๋ฐฐ์—ด ์ƒ์„ฑ. one_mat = np.ones((2,3)) print(one_mat) [[1. 1. 1.] [1. 1. 1.]] np.full()์€ ๋ฐฐ์—ด์— ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ๊ฐ’์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋“  ๊ฐ’์ด ํŠน์ • ์ƒ์ˆ˜์ธ ๋ฐฐ์—ด ์ƒ์„ฑ. ์ด ๊ฒฝ์šฐ 7. same_value_mat = np.full((2,2), 7) print(same_value_mat) [[7 7] [7 7]] np.eye()๋Š” ๋Œ€๊ฐ์„ ์œผ๋กœ๋Š” 1์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” 0์ธ 2์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. # ๋Œ€๊ฐ์„  ๊ฐ’์ด 1์ด๊ณ  ๋‚˜๋จธ์ง€ ๊ฐ’์ด 0์ธ 2์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑ. eye_mat = np.eye(3) print(eye_mat) [[1. 0. 0.] [0. 1. 0.]] [0. 0. 1.]] np.random.random()์€ ์ž„์˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. # ์ž„์˜์˜ ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์ง„ ๋ฐฐ์—ด ์ƒ์„ฑ random_mat = np.random.random((2,2)) # ์ž„์˜์˜ ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์ง„ ๋ฐฐ์—ด ์ƒ์„ฑ print(random_mat) [[0.3111881 0.72996102] [0.65667734 0.40758328]] ์ด ์™ธ์—๋„ Numpy์—๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•˜๋ฏ€๋กœ ํ•„์š”ํ•œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3) np.arange() np.arange(n)์€ 0๋ถ€ํ„ฐ n-1๊นŒ์ง€์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. # 0๋ถ€ํ„ฐ 9๊นŒ์ง€ range_vec = np.arange(10) print(range_vec) [0 1 2 3 4 5 6 7 8 9] np.arange(i, j, k)๋Š” i๋ถ€ํ„ฐ j-1๊นŒ์ง€ k์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. # 1๋ถ€ํ„ฐ 9๊นŒ์ง€ +2์”ฉ ์ ์šฉ๋˜๋Š” ๋ฒ”์œ„ n = 2 range_n_step_vec = np.arange(1, 10, n) print(range_n_step_vec) [1 3 5 7 9] 4) np.reshape() np.reshape()์€ ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋Š” ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋ฐฐ์—ด์˜ ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. 0๋ถ€ํ„ฐ 29๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” arange(30)์„ ์ˆ˜ํ–‰ํ•œ ํ›„, ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 30๊ฐœ์ด๋ฏ€๋กœ 5ํ–‰ 6์—ด์˜ ํ–‰๋ ฌ๋กœ ๋ณ€๊ฒฝํ•ด ๋ด…์‹œ๋‹ค. reshape_mat = np.array(np.arange(30)).reshape((5,6)) print(reshape_mat) [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23] [24 25 26 27 28 29]] 5) Numpy ์Šฌ๋ผ์ด์‹ฑ ndarray๋ฅผ ํ†ตํ•ด ๋งŒ๋“  ๋‹ค์ฐจ์› ๋ฐฐ์—ด์€ ํŒŒ์ด์ฌ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์ธ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ์Šฌ๋ผ์ด์‹ฑ(slicing) ๊ธฐ๋Šฅ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์Šฌ๋ผ์ด์‹ฑ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ํ–‰์ด๋‚˜ ์—ด๋“ค์˜ ์›์†Œ๋“ค์„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. mat = np.array([[1, 2, 3], [4, 5, 6]]) print(mat) [[1 2 3] [4 5 6]] # ์ฒซ ๋ฒˆ์งธ ํ–‰ ์ถœ๋ ฅ slicing_mat = mat[0, :] print(slicing_mat) [1 2 3] # ๋‘ ๋ฒˆ์งธ ์—ด ์ถœ๋ ฅ slicing_mat = mat[:, 1] print(slicing_mat) [2 5] 6) Numpy ์ •์ˆ˜ ์ธ๋ฑ์‹ฑ(integer indexing) ์Šฌ๋ผ์ด์‹ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์—ฐ์†์ ์ด์ง€ ์•Š์€ ์›์†Œ๋กœ ๋ฐฐ์—ด์„ ๋งŒ๋“ค ๊ฒฝ์šฐ์—๋Š” ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ๋Š” ๋งŒ๋“ค ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ 2ํ–‰ 2์—ด์˜ ์›์†Œ์™€ 5ํ–‰ 5์—ด์˜ ์›์†Œ๋ฅผ ๋ฝ‘์•„์„œ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ์ธ๋ฑ์‹ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์‹ฑ์€ ์›ํ•˜๋Š” ์œ„์น˜์˜ ์›์†Œ๋“ค์„ ๋ฝ‘์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. mat = np.array([[1, 2], [4, 5], [7, 8]]) print(mat) [[1 2] [4 5] [7 8]] ํŠน์ • ์œ„์น˜์˜ ์›์†Œ๋งŒ์„ ๊ฐ€์ ธ์™€๋ด…์‹œ๋‹ค. # 1ํ–‰ 0์—ด์˜ ์›์†Œ # => 0๋ถ€ํ„ฐ ์นด์šดํŠธํ•˜๋ฏ€๋กœ ๋‘ ๋ฒˆ์งธ ํ–‰ ์ฒซ ๋ฒˆ์งธ ์—ด์˜ ์›์†Œ. print(mat[1, 0]) ํŠน์ • ์œ„์น˜์˜ ์›์†Œ ๋‘ ๊ฐœ๋ฅผ ๊ฐ€์ ธ์™€ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. # mat[[2ํ–‰, 1ํ–‰],[0์—ด, 1์—ด]] # ๊ฐ ํ–‰๊ณผ ์—ด์˜ ์Œ์„ ๋งค์นญํ•˜๋ฉด 2ํ–‰ 0์—ด, 1ํ–‰ 1์—ด์˜ ๋‘ ๊ฐœ์˜ ์›์†Œ. indexing_mat = mat[[2, 1],[0, 1]] print(indexing_mat) [7 5] 7) Numpy ์—ฐ์‚ฐ Numpy๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด ๊ฐ„ ์—ฐ์‚ฐ์„ ์†์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ง์…ˆ, ๋บ„์…ˆ, ๊ณฑ์…ˆ, ๋‚˜๋ˆ—์…ˆ์„ ์œ„ํ•ด์„œ๋Š” ์—ฐ์‚ฐ์ž +, -, *, /๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋˜๋Š” np.add(), np.subtract(), np.multiply(), np.divide()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. x = np.array([1,2,3]) y = np.array([4,5,6]) # result = np.add(x, y)์™€ ๋™์ผ. result = x + y print(result) [5 7 9] # result = np.subtract(x, y)์™€ ๋™์ผ. result = x - y print(result) [-3 -3 -3] # result = np.multiply(result, x)์™€ ๋™์ผ. result = result * x print(result) [-3 -6 -9] # result = np.divide(result, x)์™€ ๋™์ผ. result = result / x print(result) [-3. -3. -3.] ์œ„์—์„œ *๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•œ ๊ฒƒ์€ ์š”์†Œ๋ณ„ ๊ณฑ์ž…๋‹ˆ๋‹ค. Numpy์—์„œ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ๊ณฑ ๋˜๋Š” ํ–‰๋ ฌ ๊ณฑ์„ ์œ„ํ•ด์„œ๋Š” dot()์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. mat1 = np.array([[1,2],[3,4]]) mat2 = np.array([[5,6],[7,8]]) mat3 = np.dot(mat1, mat2) print(mat3) [[19 22] [43 50]] 3. ๋งทํ”Œ๋กฏ๋ฆฝ(Matplotlib) ๋งทํ”Œ๋กฏ๋ฆฝ(Matplotlib)์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจํŠธ(chart)๋‚˜ ํ”Œ๋กฏ(plot)์œผ๋กœ ์‹œ๊ฐํ™”ํ•˜๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถ„์„์—์„œ Matplotlib์€ ๋ฐ์ดํ„ฐ ๋ถ„์„ ์ด์ „์— ๋ฐ์ดํ„ฐ ์ดํ•ด๋ฅผ ์œ„ํ•œ ์‹œ๊ฐํ™”๋‚˜, ๋ฐ์ดํ„ฐ ๋ถ„์„ ํ›„์— ๊ฒฐ๊ณผ๋ฅผ ์‹œ๊ฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ Matplotlib๋ฅผ ๋ณ„๋„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pip install matplotlib > ipython ... In [1]: import matplotlib as mpl In [2]: mpl.__version__ Out[2]: '2.2.3' Matplotlib์„ ๋‹ค ์„ค์น˜ํ•˜์˜€๋‹ค๋ฉด Matplotlib์˜ ์ฃผ์š” ๋ชจ๋“ˆ์ธ pyplot๋ฅผ ๊ด€๋ก€์ƒ plt๋ผ๋Š” ๋ช…์นญ์œผ๋กœ ์ž„ํฌํŠธ ํ•ด๋ด…์‹œ๋‹ค. import matplotlib.pyplot as plt 1) ๋ผ์ธ ํ”Œ๋กฏ ๊ทธ๋ฆฌ๊ธฐ plot()์€ ๋ผ์ธ ํ”Œ๋กฏ์„ ๊ทธ๋ฆฌ๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. plot()์— x์ถ•๊ณผ y ์ถ•์˜ ๊ฐ’์„ ๊ธฐ์žฌํ•˜๊ณ  ๊ทธ๋ฆผ์„ ํ‘œ์‹œํ•˜๋Š” show()๋ฅผ ํ†ตํ•ด์„œ ์‹œ๊ฐํ™”ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋ž˜ํ”„์—๋Š” title('์ œ๋ชฉ')์„ ์‚ฌ์šฉํ•˜์—ฌ ์ œ๋ชฉ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ๋ž˜ํ”„์— 'test'๋ผ๋Š” ์ œ๋ชฉ์„ ๋„ฃ์–ด๋ด…์‹œ๋‹ค. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ๋Š” show()๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ž๋™์œผ๋กœ ๋ Œ๋”๋ง ๋˜๋ฏ€๋กœ ๊ทธ๋ž˜ํ”„๊ฐ€ ์‹œ๊ฐํ™”๊ฐ€ ๋˜์ง€๋งŒ ๋‹ค๋ฅธ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ๋ฅผ ๊ฐ€์ •ํ•˜์—ฌ show()๋ฅผ ์ฝ”๋“œ์— ์‚ฝ์ž…ํ•˜์˜€์Šต๋‹ˆ๋‹ค. plt.title('test') plt.plot([1,2,3,4],[2,4,8,6]) plt.show() 2) ์ถ• ๋ ˆ์ด๋ธ” ์‚ฝ์ž…ํ•˜๊ธฐ x์ถ•๊ณผ y ์ถ• ๊ฐ๊ฐ์— ์ถ•์ด๋ฆ„์„ ์‚ฝ์ž…ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด xlabel('๋„ฃ๊ณ  ์‹ถ์€ ์ถ•์ด๋ฆ„')๊ณผ ylabel('๋„ฃ๊ณ  ์‹ถ์€ ์ถ•์ด๋ฆ„')์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ž˜ํ”„์— hours์™€ score๋ผ๋Š” ์ถ•์ด๋ฆ„์„ ๊ฐ๊ฐ ์ถ”๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. plt.title('test') plt.plot([1,2,3,4],[2,4,8,6]) plt.xlabel('hours') plt.ylabel('score') plt.show() 3) ๋ผ์ธ ์ถ”๊ฐ€์™€ ๋ฒ”๋ก€ ์‚ฝ์ž…ํ•˜๊ธฐ ๋‹ค์ˆ˜์˜ plot()์„ ํ•˜๋‚˜์˜ ๊ทธ๋ž˜ํ”„์— ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ผ์ธ ํ”Œ๋กฏ์„ ๋™์‹œ์— ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๊ฐ ์„ ์ด ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ๋ฒ”๋ก€(legend)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. plt.title('students') plt.plot([1,2,3,4],[2,4,8,6]) plt.plot([1.5,2.5,3.5,4.5],[3,5,8,10]) # ๋ผ์ธ ์ƒˆ๋กœ ์ถ”๊ฐ€ plt.xlabel('hours') plt.ylabel('score') plt.legend(['A student', 'B student']) # ๋ฒ”๋ก€ ์‚ฝ์ž… plt.show() ์ข€ ๋” ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ์‹ค์Šต์€ ๋”ฅ ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ํ›‘์–ด๋ณด๊ธฐ ์‹ค์Šต์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 01-05 ํŒ๋‹ค์Šค ํ”„๋กœํŒŒ์ผ๋ง(Pandas-Profiling) ์ข‹์€ ์š”๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ๋Š” ์กฐ๋ฆฌ ๋ฐฉ๋ฒ•๋„ ์ค‘์š”ํ•˜์ง€๋งŒ, ๊ทธ๋งŒํผ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๊ฐ–๊ณ  ์žˆ๋Š” ์žฌ๋ฃŒ์˜ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์žฌ๋ฃŒ๊ฐ€ ์ƒํ•˜๊ฑฐ๋‚˜ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ข‹์€ ์š”๋ฆฌ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ข‹์€ ๋จธ์‹  ๋Ÿฌ๋‹ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ์ดํ„ฐ์˜ ์„ฑ๊ฒฉ์„ ํŒŒ์•…ํ•˜๋Š” ๊ณผ์ •์ด ์„ ํ–‰๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ๋ฐ์ดํ„ฐ ๋‚ด ๊ฐ’์˜ ๋ถ„ํฌ, ๋ณ€์ˆ˜ ๊ฐ„์˜ ๊ด€๊ณ„, Null ๊ฐ’๊ณผ ๊ฐ™์€ ๊ฒฐ์ธก๊ฐ’(missing values) ์กด์žฌ ์œ ๋ฌด ๋“ฑ์„ ํŒŒ์•…ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ณผ์ •์„ EDA(Exploratory Data Analysis, ํƒ์ƒ‰์  ๋ฐ์ดํ„ฐ ๋ถ„์„)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฐฉ๋Œ€ํ•œ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„. profile_report()๋ผ๋Š” ๋‹จ ํ•œ ์ค„์˜ ๋ช…๋ น์œผ๋กœ ํƒ์ƒ‰ํ•˜๋Š” ํŒจํ‚ค์ง€์ธ ํŒ๋‹ค์Šค ํ”„๋กœํŒŒ์ผ๋ง(pandas-profiling)์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กฌํ”„ํŠธ์—์„œ ์•„๋ž˜์˜ pip ๋ช…๋ น์„ ํ†ตํ•ด ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install -U pandas-profiling 1. ์‹ค์Šต ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์‹ค์Šต์„ ์œ„ํ•ด ์•„๋ž˜์˜ ๋งํฌ์—์„œ spam.csv๋ž€ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์…‹์€ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ฑ•ํ„ฐ์—์„œ ์žฌ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://www.kaggle.com/uciml/sms-spam-collection-dataset spam.csv๋ฅผ ๋‹ค์šด๋กœ๋“œํ–ˆ๋‹ค๋ฉด ํ•ด๋‹น ํŒŒ์ผ์„ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์— ๊ฐ€์ ธ์™€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import pandas as pd import pandas_profiling data = pd.read_csv('spam.csv ํŒŒ์ผ์˜ ๊ฒฝ๋กœ',encoding='latin1') # ์œˆ๋„ ๋ฐ”ํƒ•ํ™”๋ฉด์—์„œ ์ž‘์—…ํ•œ ์ €์ž์˜ ๊ฒฝ์šฐ์—๋Š” # data = pd.read_csv(r'C:\Users\USER\Desktop\spam.csv',encoding='latin1') ๋‹ค์šด๋กœ๋“œํ•œ spam.csv ํŒŒ์ผ์„ Pandas๋ฅผ ์ด์šฉํ•˜์—ฌ data์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. 5๊ฐœ์˜ ํ–‰๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. data[:5] ์ด ๋ฐ์ดํ„ฐ์—๋Š” ์ด 5๊ฐœ์˜ ์—ด์ด ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ Unnamed๋ผ๋Š” ์ด๋ฆ„์˜ 3๊ฐœ์˜ ์—ด์€ 5๊ฐœ์˜ ํ–‰๋งŒ ์ถœ๋ ฅํ–ˆ์Œ์—๋„ ๋ฒŒ์จ Null ๊ฐ’์ด ๋ณด์ž…๋‹ˆ๋‹ค. v1์—ด์€ ํ•ด๋‹น ๋ฉ”์ผ์ด ์ŠคํŒธ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” ์—ด์ž…๋‹ˆ๋‹ค. ham์€ ์ •์ƒ ๋ฉ”์ผ์„ ์˜๋ฏธํ•˜๊ณ , spam์€ ์ŠคํŒธ ๋ฉ”์ผ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. v2์—ด์€ ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์„ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. pandas-profiling์„ ํ†ตํ•ด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ข€ ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ๋ฆฌํฌํŠธ ์ƒ์„ฑํ•˜๊ธฐ pr=data.profile_report() # ํ”„๋กœํŒŒ์ผ๋ง ๊ฒฐ๊ณผ ๋ฆฌํฌํŠธ๋ฅผ pr์— ์ €์žฅ # data.profile_report() # ๋ฐ”๋กœ ๊ฒฐ๊ณผ ๋ณด๊ธฐ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์—. profile_report()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ํ”„๋กœํŒŒ์ผ๋ง ํ•œ ๋ฆฌํฌํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ €์žฅํ•  ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ ๋ฐ”๋กœ ๋ฆฌํฌํŠธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pr.to_file('./pr_report.html') # pr_report.html ํŒŒ์ผ๋กœ ์ €์žฅ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์œผ๋กœ ๋ฆฌํฌํŠธ๋ฅผ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์œ„์˜ ๋ช…๋ น์„ ํ†ตํ•ด HTML ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 3. ๋ฆฌํฌํŠธ ์‚ดํŽด๋ณด๊ธฐ ์ด์ œ ์ €์žฅํ•  ๋ฆฌํฌํŠธ๋ฅผ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์— ์ถœ๋ ฅํ•˜์—ฌ ๋ฆฌํฌํŠธ ๋‚ด์šฉ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. pr # pr์— ์ €์žฅํ–ˆ๋˜ ๋ฆฌํฌํŠธ ์ถœ๋ ฅ 1) ๊ฐœ์š”(Overview) Overview๋Š” ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด์ ์ธ ๊ฐœ์š”๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ, ๋ณ€์ˆ˜์˜ ์ˆ˜, ๊ฒฐ์ธก๊ฐ’(missing value) ๋น„์œจ, ๋ฐ์ดํ„ฐ์˜ ์ข…๋ฅ˜๋Š” ์–ด๋–ค ๊ฒƒ์ด ์žˆ๋Š”์ง€๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Dataset info๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ์ด 5,572๊ฐœ์˜ ์ƒ˜ํ”Œ(ํ–‰)์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, 5๊ฐœ์˜ ์—ด์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๊ฐ’์„ ์…€์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ด 5,572 ร— 5๊ฐœ์˜ ์…€ ์ด ์กด์žฌํ•˜์ง€๋งŒ ๊ทธ์ค‘ 16,648๊ฐœ(59.8%)๊ฐ€ ๊ฒฐ์ธก๊ฐ’(missing values)์œผ๋กœ ํ™•์ธ๋ฉ๋‹ˆ๋‹ค. Warnings๋ฅผ ๋ณด๋ฉด ์‚ฌ์‹ค ๊ฒฐ์ธก๊ฐ’๋“ค์€ Unnamed๋ผ๋Š” 3๊ฐœ์˜ ์—ด์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 3๊ฐœ์˜ ์—ด ๋ชจ๋‘๋Š” 99% ์ด์ƒ์ด ๊ฒฐ์ธก๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์–ด ๋ฐ์ดํ„ฐ์—์„œ ๋ณ„๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ํ–ˆ๋˜ v2์—ด์€ ์ด 5,169๊ฐœ์˜ ์ค‘๋ณต๋˜์ง€ ์•Š์€ ๊ฐ’(distinct value)์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 5,572๊ฐœ์ธ ๊ฒƒ์„ ๊ฐ์•ˆํ•˜๋ฉด 403๊ฐœ์˜ ๋ฉ”์ผ์€ ์ค‘๋ณต์ด ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. 2) ๋ณ€์ˆ˜(Variables) ๋ณ€์ˆ˜(Variables)๋Š” ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ํŠน์„ฑ ๋ณ€์ˆ˜๋“ค์— ๋Œ€ํ•œ ๊ฒฐ์ธก๊ฐ’, ์ค‘๋ณต์„ ์ œ์™ธํ•œ ์œ ์ผํ•œ ๊ฐ’(unique values)์˜ ๊ฐœ์ˆ˜ ๋“ฑ์˜ ํ†ต๊ณ„์น˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋˜ํ•œ ์ƒ์œ„ 5๊ฐœ์˜ ๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ์šฐ์ธก์— ๋ฐ” ๊ทธ๋ž˜ํ”„๋กœ ์‹œ๊ฐํ™”ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  Unnamed๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ 3๊ฐœ์˜ ์—ด์— ๋Œ€ํ•ด์„œ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๊ฐœ์š”์—์„œ ๋ดค๋“ฏ์ด 3๊ฐœ์˜ ์—ด ๋ชจ๋‘ 99% ์ด์ƒ์˜ ๊ฐ’์ด ๊ฒฐ ์ธก ๊ฐ’์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Unnamed_2 ์—ด์€ ์ด 5,572๊ฐœ์˜ ๊ฐ’ ์ค‘์—์„œ 5,522๊ฐœ๊ฐ€ ๊ฒฐ ์ธก ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์ด 50๊ฐœ์˜ ๊ฒฐ์ธก๊ฐ’์ด ์•„๋‹Œ ๊ฐ’์ด ์กด์žฌํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ์ค‘ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ์œ ์ผํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜๋Š” 44๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋‹ค๋ฅธ 2๊ฐœ์˜ ์—ด์ธ v1๊ณผ v2๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. v2๋Š” ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์ด๊ณ , v1์€ ํ•ด๋‹น ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ(ham)์ธ์ง€, ์ŠคํŒธ ๋ฉ”์ผ(spam)์ธ์ง€ ์œ ๋ฌด๊ฐ€ ๊ธฐ์žฌ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. v1์˜ ๊ฒฝ์šฐ ์œ ์ผํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜(distinct count)๊ฐ€ 2๊ฐœ๋ฟ์œผ๋กœ 5,572๊ฐœ์˜ ๊ฐ’ ์ค‘์—์„œ ์šฐ์ธก์˜ ๋ฐ” ๊ทธ๋ž˜ํ”„๋ฅผ ํ†ตํ•ด 4,825๊ฐœ๊ฐ€ ham์ด๊ณ  747๊ฐœ๊ฐ€ spam์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ์ดํ„ฐ์—์„œ ์ •์ƒ ๋ฉ”์ผ ์ƒ˜ํ”Œ์ด ํ›จ์”ฌ ๋งŽ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. v2์˜ ๊ฒฝ์šฐ 5,572๊ฐœ์˜ ๋ฉ”์ผ ๋ณธ๋ฌธ ์ค‘์—์„œ ์ค‘๋ณต์„ ์ œ์™ธํ•˜๋ฉด 5,169๊ฐœ์˜ ์œ ์ผํ•œ ๋‚ด์šฉ์˜ ๋ฉ”์ผ ๋ณธ๋ฌธ์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘ ์ค‘๋ณต์ด ๊ฐ€์žฅ ๋งŽ์€ ๋ฉ”์ผ์€ Sorry, I'll call later๋ผ๋Š” ๋‚ด์šฉ์˜ ๋ฉ”์ผ๋กœ ์ด 30๊ฐœ์˜ ๋ฉ”์ผ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ v1๊ณผ v2 ๋ชจ๋‘ ๊ฒฐ์ธก๊ฐ’(missing values)์€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋ฐ, ์ด๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ, Unnamed๋ผ๋Š” 3๊ฐœ์˜ ์—ด์„ ์ œ๊ฑฐํ•˜๊ณ  ๋‚˜์„œ๋Š” ๋ณ„๋„์˜ ๊ฒฐ์ธก๊ฐ’ ์ „์ฒ˜๋ฆฌ๋Š” ํ•„์š”๊ฐ€ ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฐ” ๊ทธ๋ž˜ํ”„ ์šฐ์ธก ํ•˜๋‹จ์— ์žˆ๋Š” ์ƒ์„ธ์‚ฌํ•ญ ํ™•์ธํ•˜๊ธฐ(Toggle details)๋ฅผ ๋ˆŒ๋Ÿฌ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3) ์ƒ์„ธ์‚ฌํ•ญ ํ™•์ธํ•˜๊ธฐ(Toggle details) ์šฐ์„  v1์˜ ์ƒ์„ธ์‚ฌํ•ญ ํ™•์ธํ•˜๊ธฐ(Toggle details)๋ฅผ ๋ˆ„๋ฅธ ๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ƒ์„ธ์‚ฌํ•ญ ํ™•์ธํ•˜๊ธฐ์—์„œ๋Š” ์ด 2๊ฐœ์˜ ํƒญ์ด ์กด์žฌํ•˜๋Š”๋ฐ, ์ฒซ ๋ฒˆ์งธ ํƒญ์ธ ๋นˆ๋„ ๊ฐ’(common values)์—์„œ๋Š” ์•ž์„œ ๋ฐ” ๊ทธ๋ž˜ํ”„๋กœ ํ™•์ธํ–ˆ๋˜ ๊ฐ ๊ฐ’์˜ ๋ถ„ํฌ๋ฅผ ์ข€ ๋” ์ƒ์„ธํ•˜๊ฒŒ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. v1์˜ ๊ฒฝ์šฐ, ham์ด ์ด 4,825๊ฐœ๋กœ ์ด๋Š” ์ „์ฒด ๊ฐ’ ์ค‘ 86.6%์— ํ•ด๋‹น๋˜๋ฉฐ, spam์€ 747๊ฐœ๋กœ ์ „์ฒด ๊ฐ’ ์ค‘์—์„œ๋Š” 13.4%์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ํƒญ์ธ ๊ตฌ์„ฑ(composition)์—์„œ๋Š” ์ „์ฒด ๊ฐ’์˜ ์ตœ๋Œ€ ๊ธธ์ด, ์ตœ์†Œ ๊ธธ์ด, ํ‰๊ท  ๊ธธ์ด์™€ ๊ฐ’์˜ ๊ตฌ์„ฑ์— ๋Œ€ํ•ด์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. v1์˜ ๋ชจ๋“  ๊ฐ’๋“ค์€ spam ๋˜๋Š” ham์ด๋ผ๋Š” 1๊ฐœ ๋‹จ์–ด๋งŒ ์กด์žฌํ•˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ์˜ ๊ธธ์ด๋Š” ๋‹จ์–ด ๋‹จ์œ„๋กœ ์ธก์ •ํ•œ ๊ธธ์ด๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฏ€๋กœ spam์˜ ๊ธธ์ด์ธ 4๊ฐ€ ์ตœ๋Œ€ ๊ธธ์ด(max length)๊ฐ€ ๋˜๊ณ , ham์˜ ๊ธธ์ด์ธ 3์ด ์ตœ์†Œ ๊ธธ์ด(min length)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 4,825๊ฐœ์˜ 4์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ ๊ฐ’๊ณผ 747๊ฐœ์˜ ๊ฐ’์˜ 3์˜ ๊ธธ์ด๋ฅผ ํ‰๊ท  ๊ธธ์ด๋Š” 3.134063173์ด ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ v1 ์—ด์˜ ๋ชจ๋“  ๊ฐ’๋“ค์€ ์ˆซ์ž, ๊ณต๋ฐฑ, ํŠน์ˆ˜ ๋ฌธ์ž ๋“ฑ์ด ์—†์ด ์•ŒํŒŒ๋ฒณ๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋˜๋ฏ€๋กœ Contains chars์—์„œ๋งŒ True๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” v2์˜ ์ƒ์„ธ์‚ฌํ•ญ ํ™•์ธํ•˜๊ธฐ๋ฅผ ๋ˆŒ๋Ÿฌ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ค‘๋ณต์ด ์กด์žฌํ•˜๋Š” ์ƒ์œ„ 10๊ฐœ ๋ฉ”์ผ์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์„ฑ(composition)์„ ๋ˆŒ๋Ÿฌ๋ณด๋ฉด ๊ฐ’์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 910, ์ตœ์†Œ ๊ธธ์ด๋Š” 2, ํ‰๊ท  ๊ธธ์ด๋Š” 80.11880833์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฉ”์ผ ๋ณธ๋ฌธ์—์„œ๋Š” ๊ธ€์ž, ์ˆซ์ž, ๊ณต๋ฐฑ, ํŠน์ˆ˜๋ฌธ์ž ๋“ฑ์ด ๋ชจ๋‘ ํฌํ•จ๋ผ ์žˆ์œผ๋ฏ€๋กœ ๋ชจ๋“  Contains์— True๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ๋” ์ด์ƒ ๋ถˆํ•„์š”ํ•œ ์ •๋ณด๋“ค์ด๋ฏ€๋กœ ์ดํ•˜ ์„ค๋ช…์€ ์ƒ๋žตํ•˜๊ฒ ์ง€๋งŒ, pandas-profiling์€ ์œ„์—์„œ ์—ด๊ฑฐํ•œ ๊ธฐ๋Šฅ๋“ค ์™ธ์—๋„ ์ˆ˜์น˜ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ ์ƒ๊ด€๊ณ„์ˆ˜(correlations), ๊ฒฐ์ธก๊ฐ’์— ๋Œ€ํ•œ ํžˆํŠธ๋งต(headmap), ์ˆ˜์ง€๋„(dendrogram) ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. 01-06 ๋จธ์‹  ๋Ÿฌ๋‹ ์›Œํฌํ”Œ๋กœ(Machine Learning Workflow) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์‚ฌ์ด์–ธ์Šค(Data Science) ๋˜๋Š” ๋จธ์‹  ๋Ÿฌ๋‹(Machine Learning) ๊ณผ์ •์—์„œ ๊ฑฐ์น˜๋Š” ์ „๋ฐ˜์ ์ธ ๊ณผ์ •์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ฑ…์˜ ์ œ๋ชฉ์€ ๋”ฅ ๋Ÿฌ๋‹(Deep Learning)์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์ด์ง€๋งŒ, ๋”ฅ ๋Ÿฌ๋‹ ๋˜ํ•œ ๋จธ์‹  ๋Ÿฌ๋‹์˜ ํ•œ ๊ฐˆ๋ž˜๋กœ ๋”ฅ ๋Ÿฌ๋‹ ์›Œํฌํ”Œ๋กœ ๋˜ํ•œ ๋จธ์‹  ๋Ÿฌ๋‹ ์›Œํฌํ”Œ๋กœ์šฐ๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. ๋จธ์‹  ๋Ÿฌ๋‹ ์›Œํฌํ”Œ๋กœ(Machine Learning Workflow) ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ๋จธ์‹  ๋Ÿฌ๋‹์„ ํ•˜๋Š” ๊ณผ์ •์„ ํฌ๊ฒŒ 6๊ฐ€์ง€๋กœ ๋‚˜๋ˆ„๋ฉด, ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1) ์ˆ˜์ง‘(Acquisition) ๋จธ์‹  ๋Ÿฌ๋‹์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ๊ณ„์— ํ•™์Šต์‹œ์ผœ์•ผ ํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ๊ฒฝ์šฐ, ์ž์—ฐ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ง๋ญ‰์น˜ ๋˜๋Š” ์ฝ”ํผ์Šค(corpus)๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ ์ฝ”ํผ์Šค์˜ ์˜๋ฏธ๋ฅผ ํ’€์ดํ•˜๋ฉด, ์กฐ์‚ฌ๋‚˜ ์—ฐ๊ตฌ ๋ชฉ์ ์— ์˜ํ•ด์„œ ํŠน์ • ๋„๋ฉ”์ธ์œผ๋กœ๋ถ€ํ„ฐ ์ˆ˜์ง‘๋œ ํ…์ŠคํŠธ ์ง‘ํ•ฉ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํŒŒ์ผ<NAME>์€ txt ํŒŒ์ผ, csv ํŒŒ์ผ, xml ํŒŒ์ผ ๋“ฑ ๋‹ค์–‘ํ•˜๋ฉฐ ๊ทธ ์ถœ์ฒ˜๋„ ์Œ์„ฑ ๋ฐ์ดํ„ฐ, ์›น ์ˆ˜์ง‘๊ธฐ๋ฅผ ํ†ตํ•ด ์ˆ˜์ง‘๋œ ๋ฐ์ดํ„ฐ, ์˜ํ™” ๋ฆฌ๋ทฐ ๋“ฑ ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. 2) ์ ๊ฒ€ ๋ฐ ํƒ์ƒ‰(Inspection and exploration) ๋ฐ์ดํ„ฐ๊ฐ€ ์ˆ˜์ง‘๋˜์—ˆ๋‹ค๋ฉด, ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์ ๊ฒ€ํ•˜๊ณ  ํƒ์ƒ‰ํ•˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ, ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ, ๋จธ์‹  ๋Ÿฌ๋‹ ์ ์šฉ์„ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ •์ œํ•ด์•ผ ํ•˜๋Š”์ง€ ๋“ฑ์„ ํŒŒ์•…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„๋ฅผ ํƒ์ƒ‰์  ๋ฐ์ดํ„ฐ ๋ถ„์„(Exploratory Data Analysis, EDA) ๋‹จ๊ณ„๋ผ๊ณ ๋„ ํ•˜๋Š”๋ฐ ์ด๋Š” ๋…๋ฆฝ ๋ณ€์ˆ˜, ์ข…์† ๋ณ€์ˆ˜, ๋ณ€์ˆ˜ ์œ ํ˜•, ๋ณ€์ˆ˜์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋“ฑ์„ ์ ๊ฒ€ํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ์˜ ํŠน์ง•๊ณผ ๋‚ด์žฌํ•˜๋Š” ๊ตฌ์กฐ์  ๊ด€๊ณ„๋ฅผ ์•Œ์•„๋‚ด๋Š” ๊ณผ์ •์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ์‹œ๊ฐํ™”์™€ ๊ฐ„๋‹จํ•œ ํ†ต๊ณ„ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 3) ์ „์ฒ˜๋ฆฌ ๋ฐ ์ •์ œ(Preprocessing and Cleaning) ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํŒŒ์•…์ด ๋๋‚ฌ๋‹ค๋ฉด, ๋จธ์‹  ๋Ÿฌ๋‹ ์›Œํฌํ”Œ๋กœ์—์„œ ๊ฐ€์žฅ ๊นŒ๋‹ค๋กœ์šด ์ž‘์—… ์ค‘ ํ•˜๋‚˜์ธ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„๋Š” ๋งŽ์€ ๋‹จ๊ณ„๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๊ฐ€๋ น ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ผ๋ฉด ํ† ํฐํ™”, ์ •์ œ, ์ •๊ทœํ™”, ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ๋“ฑ์˜ ๋‹จ๊ณ„๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ๋น ๋ฅด๊ณ  ์ •ํ™•ํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ํˆด(์ด ์ฑ…์—์„œ๋Š” ํŒŒ์ด์ฌ)์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์ง€์‹์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ •๋ง ๊นŒ๋‹ค๋กœ์šด ์ „์ฒ˜๋ฆฌ์˜ ๊ฒฝ์šฐ์—๋Š” ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ ๋จธ์‹  ๋Ÿฌ๋‹์ด ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 4) ๋ชจ๋ธ๋ง ๋ฐ ํ›ˆ๋ จ(Modeling and Training) ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚ฌ๋‹ค๋ฉด, ๋จธ์‹  ๋Ÿฌ๋‹์— ๋Œ€ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋‹จ๊ณ„์ธ ๋ชจ๋ธ๋ง ๋‹จ๊ณ„์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ ์ ˆํ•œ ๋จธ์‹  ๋Ÿฌ๋‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ ํƒํ•˜์—ฌ ๋ชจ๋ธ๋ง์ด ๋๋‚ฌ๋‹ค๋ฉด, ์ „์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋จธ์‹  ๋Ÿฌ๋‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ๊ธฐ๊ณ„์—๊ฒŒ ํ•™์Šต(training) ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ›ˆ๋ จ์ด๋ผ๊ณ ๋„ ํ•˜๋Š”๋ฐ, ์ด ๋‘ ์šฉ์–ด๋ฅผ ํ˜ผ์šฉํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ•™์Šต์„ ๋งˆ์น˜๊ณ  ๋‚˜์„œ ํ›ˆ๋ จ์ด ์ œ๋Œ€๋กœ ๋˜์—ˆ๋‹ค๋ฉด ๊ทธ ํ›„์— ๊ธฐ๊ณ„๋Š” ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ํƒœ์Šคํฌ(task)์ธ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ, ์Œ์„ฑ ์ธ์‹, ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๋“ฑ์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๊ณ„์—๊ฒŒ ํ•™์Šต์‹œ์ผœ์„œ๋Š” ์•ˆ ๋œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋’ค์˜ ์‹ค์Šต์—์„œ ๋ณด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ ๋ฐ์ดํ„ฐ ์ค‘ ์ผ๋ถ€๋Š” ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ๋‚จ๊ฒจ๋‘๊ณ  ํ›ˆ๋ จ์šฉ ๋ฐ์ดํ„ฐ๋งŒ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ๋งŒ ๊ธฐ๊ณ„๊ฐ€ ํ•™์Šต์„ ํ•˜๊ณ  ๋‚˜์„œ, ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ํ˜„์žฌ ์„ฑ๋Šฅ์ด ์–ผ๋งˆ๋‚˜ ๋˜๋Š”์ง€๋ฅผ ์ธก์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ณผ ์ ํ•ฉ(overfitting) ์ƒํ™ฉ์„ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ตœ์„ ์€ ํ›ˆ๋ จ์šฉ, ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ๋‘ ๊ฐ€์ง€๋งŒ ๋‚˜๋ˆ„๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ํ›ˆ๋ จ์šฉ, ๊ฒ€์ฆ์šฉ, ํ…Œ์ŠคํŠธ์šฉ. ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋ ‡๊ฒŒ ์„ธ ๊ฐ€์ง€๋กœ ๋‚˜๋ˆ„๊ณ  ํ›ˆ๋ จ์šฉ ๋ฐ์ดํ„ฐ๋งŒ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฒ€์ฆ์šฉ๊ณผ ํ…Œ์ŠคํŠธ์šฉ์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? ์ˆ˜๋Šฅ ์‹œํ—˜์— ๋น„์œ ํ•˜์ž๋ฉด ํ›ˆ๋ จ์šฉ์€ ํ•™์Šต์ง€, ๊ฒ€์ฆ์šฉ์€ ๋ชจ์˜๊ณ ์‚ฌ, ํ…Œ์ŠคํŠธ์šฉ์€ ์ˆ˜๋Šฅ ์‹œํ—˜์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์ง€๋ฅผ ํ’€๊ณ  ์ˆ˜๋Šฅ ์‹œํ—˜์„ ๋ณผ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๋ชจ์˜๊ณ ์‚ฌ๋ฅผ ํ’€๋ฉฐ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด ๋ฌด์—‡์ธ์ง€ ๊ฒ€์ฆํ•˜๊ณ  ๋ณด์™„ํ•˜๋Š” ๋‹จ๊ณ„๋ฅผ ํ•˜๋‚˜ ๋” ๋†“๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ฒ ์ง€์š”. ์‚ฌ์‹ค ํ˜„์—…์˜ ๊ฒฝ์šฐ๋ผ๋ฉด ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋Š” ๊ฑฐ์˜ ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค. ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋Š” ํ˜„์žฌ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ. ์ฆ‰, ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ์šฉ ๋ฐ์ดํ„ฐ๋กœ ์–ผ๋งˆ๋‚˜ ์ œ๋Œ€๋กœ ํ•™์Šต์ด ๋˜์—ˆ๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์šฉ์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋ธ์˜ ์ตœ์ข… ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๋Š” ์ผ์— ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์ˆ˜์น˜ํ™”ํ•˜์—ฌ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด ์‹œํ—˜์— ๋น„์œ ํ•˜๋ฉด ์ฑ„์ ํ•˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์‹ค์Šต ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ํ›ˆ๋ จ์šฉ, ๊ฒ€์ฆ์šฉ, ํ…Œ์ŠคํŠธ์šฉ ์„ธ ๊ฐ€์ง€๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋•Œ๋กœ๋Š” ํ›ˆ๋ จ์šฉ, ํ…Œ์ŠคํŠธ์šฉ ๋‘ ๊ฐ€์ง€๋งŒ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์—…์—์„œ ์ตœ์„ ์€ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ ๋˜ํ•œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž„์„ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. 5) ํ‰๊ฐ€(Evaluation) ๋ฏธ๋ฆฌ ์–ธ๊ธ‰ํ•˜์˜€๋Š”๋ฐ, ๊ธฐ๊ณ„๊ฐ€ ๋‹ค ํ•™์Šต์ด ๋˜์—ˆ๋‹ค๋ฉด ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ๋กœ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์€ ๊ธฐ๊ณ„๊ฐ€ ์˜ˆ์ธกํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ์˜ ์‹ค์ œ ์ •๋‹ต๊ณผ ์–ผ๋งˆ๋‚˜ ๊ฐ€๊นŒ์šด์ง€๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค. 6) ๋ฐฐํฌ(Deployment) ํ‰๊ฐ€ ๋‹จ๊ณ„์—์„œ ๊ธฐ๊ณ„๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ํ›ˆ๋ จ์ด ๋œ ๊ฒƒ์œผ๋กœ ํŒ๋‹จ๋œ๋‹ค๋ฉด ์™„์„ฑ๋œ ๋ชจ๋ธ์ด ๋ฐฐํฌ๋˜๋Š” ๋‹จ๊ณ„๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์—ฌ๊ธฐ์„œ ์™„์„ฑ๋œ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ „์ฒด์ ์ธ ํ”ผ๋“œ๋ฐฑ์œผ๋กœ ์ธํ•ด ๋ชจ๋ธ์„ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์˜จ๋‹ค๋ฉด ์ˆ˜์ง‘ ๋‹จ๊ณ„๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 02. ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ(Text preprocessing) ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ๋Š” ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์˜ ์šฉ๋„์— ๋งž๊ฒŒ ํ…์ŠคํŠธ๋ฅผ ์‚ฌ์ „์— ์ฒ˜๋ฆฌํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์š”๋ฆฌ๋ฅผ ํ•  ๋•Œ ์žฌ๋ฃŒ๋ฅผ ์ œ๋Œ€๋กœ ์†์งˆํ•˜์ง€ ์•Š์œผ๋ฉด, ์š”๋ฆฌ๊ฐ€ ์—‰๋ง์ด ๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ…์ŠคํŠธ์— ์ œ๋Œ€๋กœ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด ๋’ค์—์„œ ๋ฐฐ์šธ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ธฐ๋ฒ•๋“ค์ด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํ…์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ์ „์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•๋“ค์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 02-01 ํ† ํฐํ™”(Tokenization) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํฌ๋กค๋ง ๋“ฑ์œผ๋กœ ์–ป์–ด๋‚ธ ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”์— ๋งž๊ฒŒ ์ „์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ์ƒํƒœ๋ผ๋ฉด, ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์šฉ๋„์— ๋งž๊ฒŒ ํ† ํฐํ™”(tokenization) & ์ •์ œ(cleaning) & ์ •๊ทœํ™”(normalization) ํ•˜๋Š” ์ผ์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ทธ์ค‘์—์„œ๋„ ํ† ํฐํ™”์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”ํผ์Šค(corpus)์—์„œ ํ† ํฐ(token)์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š” ์ž‘์—…์„ ํ† ํฐํ™”(tokenization)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐ์˜ ๋‹จ์œ„๊ฐ€ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅด์ง€๋งŒ, ๋ณดํ†ต ์˜๋ฏธ ์žˆ๋Š” ๋‹จ์œ„๋กœ ํ† ํฐ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ† ํฐํ™”์— ๋Œ€ํ•œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ƒํ™ฉ์— ๋Œ€ํ•ด์„œ ์–ธ๊ธ‰ํ•˜์—ฌ ํ† ํฐํ™”์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. ์ด์–ด์„œ NLTK, KoNLPY๋ฅผ ํ†ตํ•ด ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๋ฉฐ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 1. ๋‹จ์–ด ํ† ํฐํ™”(Word Tokenization) ํ† ํฐ์˜ ๊ธฐ์ค€์„ ๋‹จ์–ด(word)๋กœ ํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹จ์–ด ํ† ํฐํ™”(word tokenization)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์—ฌ๊ธฐ์„œ ๋‹จ์–ด(word)๋Š” ๋‹จ์–ด ๋‹จ์œ„ ์™ธ์—๋„ ๋‹จ ์–ด๊ตฌ, ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š” ๋ฌธ์ž์—ด๋กœ๋„ ๊ฐ„์ฃผ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ๊ตฌ๋‘์ (punctuation)๊ณผ ๊ฐ™์€ ๋ฌธ์ž๋Š” ์ œ์™ธํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋‹จ์–ด ํ† ํฐํ™” ์ž‘์—…์„ ํ•ด๋ด…์‹œ๋‹ค. ๊ตฌ๋‘์ ์ด๋ž€ ๋งˆ์นจํ‘œ(.), ์ฝค๋งˆ(,), ๋ฌผ์Œํ‘œ(?), ์„ธ๋ฏธ์ฝœ๋ก (;), ๋Š๋‚Œํ‘œ(!) ๋“ฑ๊ณผ ๊ฐ™์€ ๊ธฐํ˜ธ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ: Time is an illusion. Lunchtime double so! ์ด๋Ÿฌํ•œ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ๊ตฌ๋‘์ ์„ ์ œ์™ธํ•œ ํ† ํฐํ™” ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ : "Time", "is", "an", "illustion", "Lunchtime", "double", "so" ์ด ์˜ˆ์ œ์—์„œ ํ† ํฐํ™” ์ž‘์—…์€ ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋‘์ ์„<NAME> ๋’ค์— ๋„์–ด์“ฐ๊ธฐ(whitespace)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž˜๋ผ๋ƒˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ์˜ˆ์ œ๋Š” ํ† ํฐํ™”์˜ ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ์ค€ ๊ฒƒ์— ๋ถˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ํ† ํฐํ™” ์ž‘์—…์€ ๋‹จ์ˆœํžˆ ๊ตฌ๋‘์ ์ด๋‚˜ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ „๋ถ€ ์ œ๊ฑฐํ•˜๋Š” ์ •์ œ(cleaning) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ํ•ด๊ฒฐ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ตฌ๋‘์ ์ด๋‚˜ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ „๋ถ€ ์ œ๊ฑฐํ•˜๋ฉด ํ† ํฐ์ด ์˜๋ฏธ๋ฅผ ์žƒ์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ์ž๋ฅด๋ฉด ์‚ฌ์‹ค์ƒ ๋‹จ์–ด ํ† ํฐ์ด ๊ตฌ๋ถ„๋˜๋Š” ์˜์–ด์™€ ๋‹ฌ๋ฆฌ, ํ•œ๊ตญ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๋งŒ์œผ๋กœ๋Š” ๋‹จ์–ด ํ† ํฐ์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋’ค์—์„œ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ํ† ํฐํ™” ์ค‘ ์ƒ๊ธฐ๋Š” ์„ ํƒ์˜ ์ˆœ๊ฐ„ ํ† ํฐํ™”๋ฅผ ํ•˜๋‹ค ๋ณด๋ฉด, ์˜ˆ์ƒํ•˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด์„œ ํ† ํฐํ™”์˜ ๊ธฐ์ค€์„ ์ƒ๊ฐํ•ด ๋ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์ด๋Ÿฌํ•œ ์„ ํƒ์€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€์— ๋”ฐ๋ผ์„œ ๊ทธ ์šฉ๋„์— ์˜ํ–ฅ์ด ์—†๋Š” ๊ธฐ์ค€์œผ๋กœ ์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๋ฅผ(')๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๋‹จ์–ด๋Š” ์–ด๋–ป๊ฒŒ ํ† ํฐ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์„ ํƒ์˜ ๋ฌธ์ œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. Don't be fooled by the dark sounding name, Mr. Jone's Orphanage is as cheery as cheery goes for a pastry shop. ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๊ฐ€ ๋“ค์–ด๊ฐ„ ์ƒํ™ฉ์—์„œ Don't์™€ Jone's๋Š” ์–ด๋–ป๊ฒŒ ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ๋‹ค์–‘ํ•œ ์„ ํƒ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Don't Don t Dont Do n't Jone's Jone s Jone Jones ์ด ์ค‘ ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก ํ† ํฐํ™” ๋„๊ตฌ๋ฅผ ์ง์ ‘ ์„ค๊ณ„ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๊ธฐ์กด์— ๊ณต๊ฐœ๋œ ๋„๊ตฌ๋“ค์„ ์‚ฌ์šฉํ•˜์˜€์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์‚ฌ์šฉ์ž์˜ ๋ชฉ์ ๊ณผ ์ผ์น˜ํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. NLTK๋Š” ์˜์–ด ์ฝ”ํผ์Šค๋ฅผ ํ† ํฐํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ์ค‘ word_tokenize์™€ WordPunctTokenizer๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from nltk.tokenize import word_tokenize from nltk.tokenize import WordPunctTokenizer from tensorflow.keras.preprocessing.text import text_to_word_sequence ์šฐ์„  word_tokenize๋ฅผ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. print('๋‹จ์–ด ํ† ํฐํ™” 1 :',word_tokenize("Don't be fooled by the dark sounding name, Mr. Jone's Orphanage is as cheery as cheery goes for a pastry shop.")) ๋‹จ์–ด ํ† ํฐํ™” 1 : ['Do', "n't", 'be', 'fooled', 'by', 'the', 'dark', 'sounding', 'name', ',', 'Mr.', 'Jone', "'s", 'Orphanage', 'is', 'as', 'cheery', 'as', 'cheery', 'goes', 'for', 'a', 'pastry', 'shop', '.'] word_tokenize๋Š” Don't๋ฅผ Do์™€ n't๋กœ ๋ถ„๋ฆฌํ•˜์˜€์œผ๋ฉฐ, ๋ฐ˜๋ฉด Jone's๋Š” Jone๊ณผ 's๋กœ ๋ถ„๋ฆฌํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, wordPunctTokenizer๋Š” ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๊ฐ€ ๋“ค์–ด๊ฐ„ ์ฝ”ํผ์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ๊นŒ์š”? print('๋‹จ์–ด ํ† ํฐํ™” 2 :',WordPunctTokenizer().tokenize("Don't be fooled by the dark sounding name, Mr. Jone's Orphanage is as cheery as cheery goes for a pastry shop.")) ['Don', "'", 't', 'be', 'fooled', 'by', 'the', 'dark', 'sounding', 'name', ',', 'Mr', '.', 'Jone', "'", 's', 'Orphanage', 'is', 'as', 'cheery', 'as', 'cheery', 'goes', 'for', 'a', 'pastry', 'shop', '.'] WordPunctTokenizer๋Š” ๊ตฌ๋‘์ ์„ ๋ณ„๋„๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ํŠน์ง•์„ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์•ž์„œ ํ™•์ธํ–ˆ๋˜ word_tokenize์™€๋Š” ๋‹ฌ๋ฆฌ Don't๋ฅผ Don๊ณผ '์™€ t๋กœ ๋ถ„๋ฆฌํ•˜์˜€์œผ๋ฉฐ, ์ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Jone's๋ฅผ Jone๊ณผ '์™€ s๋กœ ๋ถ„๋ฆฌํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค ๋˜ํ•œ ํ† ํฐํ™” ๋„๊ตฌ๋กœ์„œ text_to_word_sequence๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. print('๋‹จ์–ด ํ† ํฐํ™” 3 :',text_to_word_sequence("Don't be fooled by the dark sounding name, Mr. Jone's Orphanage is as cheery as cheery goes for a pastry shop.")) ๋‹จ์–ด ํ† ํฐํ™” 3 : ["don't", 'be', 'fooled', 'by', 'the', 'dark', 'sounding', 'name', 'mr', "jone's", 'orphanage', 'is', 'as', 'cheery', 'as', 'cheery', 'goes', 'for', 'a', 'pastry', 'shop'] ์ผ€๋ผ์Šค์˜ text_to_word_sequence๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์„ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋ฉด์„œ ๋งˆ์นจํ‘œ๋‚˜ ์ฝค๋งˆ, ๋Š๋‚Œํ‘œ ๋“ฑ์˜ ๊ตฌ๋‘์ ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ don't๋‚˜ jone's์™€ ๊ฐ™์€ ๊ฒฝ์šฐ ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๋Š” ๋ณด์กดํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ํ† ํฐํ™”์—์„œ ๊ณ ๋ คํ•ด์•ผ ํ•  ์‚ฌํ•ญ ํ† ํฐํ™” ์ž‘์—…์„ ๋‹จ์ˆœํ•˜๊ฒŒ ์ฝ”ํผ์Šค์—์„œ ๊ตฌ๋‘์ ์„ ์ œ์™ธํ•˜๊ณ  ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ์ž˜๋ผ๋‚ด๋Š” ์ž‘์—…์ด๋ผ๊ณ  ๊ฐ„์ฃผํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ผ์€ ๋ณด๋‹ค ์„ฌ์„ธํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ•„์š”ํ•œ๋ฐ ๊ทธ ์ด์œ ๋ฅผ ์ •๋ฆฌํ•ด ๋ด…๋‹ˆ๋‹ค. 1) ๊ตฌ๋‘์ ์ด๋‚˜ ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ๋‹จ์ˆœ ์ œ์™ธํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ ๋‹จ์–ด๋“ค์„ ๊ฑธ๋Ÿฌ๋‚ผ ๋•Œ, ๊ตฌ๋‘์ ์ด๋‚˜ ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ๋‹จ์ˆœํžˆ ์ œ์™ธํ•˜๋Š” ๊ฒƒ์€ ์˜ณ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฝ”ํผ์Šค์— ๋Œ€ํ•œ ์ •์ œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋‹ค ๋ณด๋ฉด, ๊ตฌ๋‘์ ์กฐ์ฐจ๋„ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด์ž๋ฉด, ๋งˆ์นจํ‘œ(.)์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๋ฌธ์žฅ์˜ ๊ฒฝ๊ณ„๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ ๋„์›€์ด ๋˜๋ฏ€๋กœ ๋‹จ์–ด๋ฅผ ๋ฝ‘์•„๋‚ผ ๋•Œ, ๋งˆ์นจํ‘œ(.)๋ฅผ ์ œ์™ธํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋กœ ๋‹จ์–ด ์ž์ฒด์— ๊ตฌ๋‘์ ์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋Š”๋ฐ, m.p.h๋‚˜ Ph.D๋‚˜ AT&T ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ ํŠน์ˆ˜ ๋ฌธ์ž์˜ ๋‹ฌ๋Ÿฌ๋‚˜ ์Šฌ๋ž˜์‹œ(/)๋กœ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋ฉด, $45.55์™€ ๊ฐ™์€ ๊ฐ€๊ฒฉ์„ ์˜๋ฏธํ•˜๊ธฐ๋„ ํ•˜๊ณ , 01/02/06์€ ๋‚ ์งœ๋ฅผ ์˜๋ฏธํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ์ด๋Ÿฐ ๊ฒฝ์šฐ 45.55๋ฅผ ํ•˜๋‚˜๋กœ ์ทจ๊ธ‰ํ•˜๊ณ  45์™€ 55๋กœ ๋”ฐ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ  ์‹ถ์ง€๋Š” ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆซ์ž ์‚ฌ์ด์— ์ฝค๋งˆ(,)๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต ์ˆ˜์น˜๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ๋Š” 123,456,789์™€ ๊ฐ™์ด ์„ธ ์ž๋ฆฌ ๋‹จ์œ„๋กœ ์ปด๋งˆ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ์ค„์ž„๋ง๊ณผ ๋‹จ์–ด ๋‚ด์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ. ํ† ํฐํ™” ์ž‘์—…์—์„œ ์ข…์ข… ์˜์–ด๊ถŒ ์–ธ์–ด์˜ ์•„ํฌ์ŠคํŠธ๋กœํ”ผ(')๋Š” ์••์ถ•๋œ ๋‹จ์–ด๋ฅผ ๋‹ค์‹œ ํŽผ์น˜๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด what're๋Š” what are์˜ ์ค„์ž„๋ง์ด๋ฉฐ, we're๋Š” we are์˜ ์ค„์ž„๋ง์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ re๋ฅผ ์ ‘์–ด(clitic)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋‹จ์–ด๊ฐ€ ์ค„์ž„๋ง๋กœ ์“ฐ์ผ ๋•Œ ์ƒ๊ธฐ๋Š” ํ˜•ํƒœ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น I am์„ ์ค„์ธ I'm์ด ์žˆ์„ ๋•Œ, m์„ ์ ‘์–ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. New York์ด๋ผ๋Š” ๋‹จ์–ด๋‚˜ rock 'n' roll์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๋ด…์‹œ๋‹ค. ์ด ๋‹จ์–ด๋“ค์€ ํ•˜๋‚˜์˜ ๋‹จ์–ด์ด์ง€๋งŒ ์ค‘๊ฐ„์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์šฉ๋„์— ๋”ฐ๋ผ์„œ, ํ•˜๋‚˜์˜ ๋‹จ์–ด ์‚ฌ์ด์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋„ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ๋ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํ† ํฐํ™” ์ž‘์—…์€ ์ €๋Ÿฌํ•œ ๋‹จ์–ด๋ฅผ ํ•˜๋‚˜๋กœ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ๋„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. 3) ํ‘œ์ค€ ํ† ํฐํ™” ์˜ˆ์ œ ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ํ‘œ์ค€์œผ๋กœ ์“ฐ์ด๊ณ  ์žˆ๋Š” ํ† ํฐํ™” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ธ Penn Treebank Tokenization์˜ ๊ทœ์น™์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•˜๊ณ , ํ† ํฐํ™”์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทœ์น™ 1. ํ•˜์ดํ”ˆ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋‹จ์–ด๋Š” ํ•˜๋‚˜๋กœ<NAME>๋‹ค. ๊ทœ์น™ 2. doesn't์™€ ๊ฐ™์ด ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๋กœ '์ ‘์–ด'๊ฐ€ ํ•จ๊ป˜ํ•˜๋Š” ๋‹จ์–ด๋Š” ๋ถ„๋ฆฌํ•ด ์ค€๋‹ค. ํ•ด๋‹น ํ‘œ์ค€์— ์•„๋ž˜์˜ ๋ฌธ์žฅ์„ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด๋ด…๋‹ˆ๋‹ค. "Starting a home-based restaurant may be an ideal. it doesn't have a food chain or restaurant of their own." from nltk.tokenize import TreebankWordTokenizer tokenizer = TreebankWordTokenizer() text = "Starting a home-based restaurant may be an ideal. it doesn't have a food chain or restaurant of their own." print('ํŠธ๋ฆฌ ๋ฑ…ํฌ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ € :',tokenizer.tokenize(text)) ํŠธ๋ฆฌ ๋ฑ…ํฌ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ € : ['Starting', 'a', 'home-based', 'restaurant', 'may', 'be', 'an', 'ideal.', 'it', 'does', "n't", 'have', 'a', 'food', 'chain', 'or', 'restaurant', 'of', 'their', 'own', '.'] ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, ๊ฐ๊ฐ ๊ทœ์น™ 1๊ณผ ๊ทœ์น™ 2์— ๋”ฐ๋ผ์„œ home-based๋Š” ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ์ทจ๊ธ‰ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, dosen't์˜ ๊ฒฝ์šฐ does์™€ n't๋Š” ๋ถ„๋ฆฌ๋˜์—ˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. ๋ฌธ์žฅ ํ† ํฐํ™”(Sentence Tokenization) ์ด๋ฒˆ์—๋Š” ํ† ํฐ์˜ ๋‹จ์œ„๊ฐ€ ๋ฌธ์žฅ(sentence)์ผ ๊ฒฝ์šฐ๋ฅผ ๋…ผ์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค ๋‚ด์—์„œ ๋ฌธ์žฅ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ์ž‘์—…์œผ๋กœ ๋•Œ๋กœ๋Š” ๋ฌธ์žฅ ๋ถ„๋ฅ˜(sentence segmentation)๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋ณดํ†ต ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค๊ฐ€ ์ •์ œ๋˜์ง€ ์•Š์€ ์ƒํƒœ๋ผ๋ฉด, ์ฝ”ํผ์Šค๋Š” ๋ฌธ์žฅ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์ง€ ์•Š์•„์„œ ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์šฉ๋„์— ๋งž๊ฒŒ ๋ฌธ์žฅ ํ† ํฐ ํ™”๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ์ฃผ์–ด์ง„ ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ ๋‹จ์œ„๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ง๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•ด ๋ดค์„ ๋•Œ๋Š”? ๋‚˜ ๋งˆ์นจํ‘œ(.)๋‚˜! ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์žฅ์„ ์ž˜๋ผ๋‚ด๋ฉด ๋˜์ง€ ์•Š์„๊นŒ๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ผญ ๊ทธ๋ ‡์ง€๋งŒ์€ ์•Š์Šต๋‹ˆ๋‹ค. !๋‚˜?๋Š” ๋ฌธ์žฅ์˜ ๊ตฌ๋ถ„์„ ์œ„ํ•œ ๊ฝค ๋ช…ํ™•ํ•œ ๊ตฌ๋ถ„์ž(boundary) ์—ญํ• ์„ ํ•˜์ง€๋งŒ ๋งˆ์นจํ‘œ๋Š” ๊ทธ๋ ‡์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋งˆ์นจํ‘œ๋Š” ๋ฌธ์žฅ์˜ ๋์ด ์•„๋‹ˆ๋”๋ผ๋„ ๋“ฑ์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. EX1) IP 192.168.56.31 ์„œ๋ฒ„์— ๋“ค์–ด๊ฐ€์„œ ๋กœ๊ทธ ํŒŒ์ผ ์ €์žฅํ•ด์„œ aaa@gmail.com๋กœ ๊ฒฐ๊ณผ ์ข€ ๋ณด๋‚ด์ค˜. ๊ทธ ํ›„ ์ ์‹ฌ ๋จน์œผ๋Ÿฌ ๊ฐ€์ž. EX2) Since I'm actively looking for Ph.D. students, I get the same question a dozen times every year. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์˜ˆ์ œ์— ๋งˆ์นจํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์žฅ ํ† ํฐํ™”๋ฅผ ์ ์šฉํ•ด ๋ณธ๋‹ค๋ฉด ์–ด๋–จ๊นŒ์š”? ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์—์„œ๋Š” ๋ณด๋‚ด์ค˜.์—์„œ ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ์—์„œ๋Š” year.์—์„œ ์ฒ˜์Œ์œผ๋กœ ๋ฌธ์žฅ์ด ๋๋‚œ ๊ฒƒ์œผ๋กœ ์ธ์‹ํ•˜๋Š” ๊ฒƒ์ด ์ œ๋Œ€๋กœ ๋ฌธ์žฅ์˜ ๋์„ ์˜ˆ์ธกํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์ˆœํžˆ ๋งˆ์นจํ‘œ(.)๋กœ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ ์ง“๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด, ๋ฌธ์žฅ์˜ ๋์ด ๋‚˜์˜ค๊ธฐ ์ „์— ์ด๋ฏธ ๋งˆ์นจํ‘œ๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋“ฑ์žฅํ•˜์—ฌ ์˜ˆ์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ์ฝ”ํผ์Šค๊ฐ€ ์–ด๋–ค ๊ตญ์ ์˜ ์–ธ์–ด์ธ์ง€, ๋˜๋Š” ํ•ด๋‹น ์ฝ”ํผ์Šค ๋‚ด์—์„œ ํŠน์ˆ˜๋ฌธ์ž๋“ค์ด ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š”์ง€์— ๋”ฐ๋ผ์„œ ์ง์ ‘ ๊ทœ์น™๋“ค์„ ์ •์˜ํ•ด ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. 100% ์ •ํ™•๋„๋ฅผ ์–ป๋Š” ์ผ์€ ์‰ฌ์šด ์ผ์ด ์•„๋‹Œ๋ฐ, ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ์— ์˜คํƒ€๋‚˜, ๋ฌธ์žฅ์˜ ๊ตฌ์„ฑ์ด ์—‰๋ง์ด๋ผ๋ฉด ์ •ํ•ด๋†“์€ ๊ทœ์น™์ด ์†Œ์šฉ์ด ์—†์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. NLTK์—์„œ๋Š” ์˜์–ด ๋ฌธ์žฅ์˜ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” sent_tokenize๋ฅผ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. NLTK๋ฅผ ํ†ตํ•ด ๋ฌธ์žฅ ํ† ํฐํ™”๋ฅผ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from nltk.tokenize import sent_tokenize text = "His barber kept his word. But keeping such a huge secret to himself was driving him crazy. Finally, the barber went up a mountain and almost to the edge of a cliff. He dug a hole in the midst of some reeds. He looked about, to make sure no one was near." print('๋ฌธ์žฅ ํ† ํฐํ™” 1 :',sent_tokenize(text)) ๋ฌธ์žฅ ํ† ํฐํ™” 1 : ['His barber kept his word.', 'But keeping such a huge secret to himself was driving him crazy.', 'Finally, the barber went up a mountain and almost to the edge of a cliff.', 'He dug a hole in the midst of some reeds.', 'He looked about, to make sure no one was near.'] ์œ„ ์ฝ”๋“œ๋Š” text์— ์ €์žฅ๋œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์žฅ๋“ค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์„ฑ๊ณต์ ์œผ๋กœ ๋ชจ๋“  ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•ด ๋‚ด์—ˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ฒˆ์—๋Š” ๋ฌธ์žฅ ์ค‘๊ฐ„์— ๋งˆ์นจํ‘œ๊ฐ€ ๋‹ค์ˆ˜ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ๋„ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. text = "I am actively looking for Ph.D. students. and you are a Ph.D student." print('๋ฌธ์žฅ ํ† ํฐํ™” 2 :',sent_tokenize(text)) ๋ฌธ์žฅ ํ† ํฐํ™” 2 : ['I am actively looking for Ph.D. students.', 'and you are a Ph.D student.'] NLTK๋Š” ๋‹จ์ˆœํžˆ ๋งˆ์นจํ‘œ๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ํ•˜์—ฌ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, Ph.D.๋ฅผ ๋ฌธ์žฅ ๋‚ด์˜ ๋‹จ์–ด๋กœ ์ธ์‹ํ•˜์—ฌ ์„ฑ๊ณต์ ์œผ๋กœ ์ธ์‹ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด์— ๋Œ€ํ•œ ๋ฌธ์žฅ ํ† ํฐํ™” ๋„๊ตฌ ๋˜ํ•œ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฐ•์ƒ๊ธธ ๋‹˜์ด ๊ฐœ๋ฐœํ•œ KSS(Korean Sentence Splitter)๋ฅผ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด KSS๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install kss KSS๋ฅผ ํ†ตํ•ด์„œ ๋ฌธ์žฅ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import kss text = '๋”ฅ ๋Ÿฌ๋‹ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๊ฐ€ ์žฌ๋ฏธ์žˆ๊ธฐ๋Š” ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ œ๋Š” ์˜์–ด๋ณด๋‹ค ํ•œ๊ตญ์–ด๋กœ ํ•  ๋•Œ ๋„ˆ๋ฌด ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ•ด๋ณด๋ฉด ์•Œ๊ฑธ์š”?' print('ํ•œ๊ตญ์–ด ๋ฌธ์žฅ ํ† ํฐํ™” :',kss.split_sentences(text)) ํ•œ๊ตญ์–ด ๋ฌธ์žฅ ํ† ํฐํ™” : ['๋”ฅ ๋Ÿฌ๋‹ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๊ฐ€ ์žฌ๋ฏธ์žˆ๊ธฐ๋Š” ํ•ฉ๋‹ˆ๋‹ค.', '๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ œ๋Š” ์˜์–ด๋ณด๋‹ค ํ•œ๊ตญ์–ด๋กœ ํ•  ๋•Œ ๋„ˆ๋ฌด ์–ด๋ ต์Šต๋‹ˆ๋‹ค.', '์ด์ œ ํ•ด๋ณด๋ฉด ์•Œ๊ฑธ์š”?'] ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์ •์ƒ์ ์œผ๋กœ ๋ชจ๋“  ๋ฌธ์žฅ์ด ๋ถ„๋ฆฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 5. ํ•œ๊ตญ์–ด์—์„œ์˜ ํ† ํฐํ™”์˜ ์–ด๋ ค์›€. ์˜์–ด๋Š” New York๊ณผ ๊ฐ™์€ ํ•ฉ์„ฑ์–ด๋‚˜ he's ์™€ ๊ฐ™์ด ์ค„์ž„๋ง์— ๋Œ€ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋งŒ ํ•œ๋‹ค๋ฉด, ๋„์–ด์“ฐ๊ธฐ(whitespace)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋Š” ๋„์–ด์“ฐ๊ธฐ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ด๋„ ๋‹จ์–ด ํ† ํฐ ํ™”๊ฐ€ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ๋‹จ์–ด ๋‹จ์œ„๋กœ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋„์–ด์“ฐ๊ธฐ ํ† ํฐํ™”์™€ ๋‹จ์–ด ํ† ํฐ ํ™”๊ฐ€ ๊ฑฐ์˜ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•œ๊ตญ์–ด๋Š” ์˜์–ด์™€๋Š” ๋‹ฌ๋ฆฌ ๋„์–ด์“ฐ๊ธฐ๋งŒ์œผ๋กœ๋Š” ํ† ํฐํ™”๋ฅผ ํ•˜๊ธฐ์— ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๊ฐ€ ๋˜๋Š” ๋‹จ์œ„๋ฅผ '์–ด์ ˆ'์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ ์–ด์ ˆ ํ† ํฐํ™”๋Š” ํ•œ๊ตญ์–ด NLP์—์„œ ์ง€์–‘๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด์ ˆ ํ† ํฐํ™”์™€ ๋‹จ์–ด ํ† ํฐํ™”๋Š” ๊ฐ™์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ ๊ทผ๋ณธ์ ์ธ ์ด์œ ๋Š” ํ•œ๊ตญ์–ด๊ฐ€ ์˜์–ด์™€๋Š” ๋‹ค๋ฅธ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋Š” ์–ธ์–ด์ธ ๊ต์ฐฉ์–ด๋ผ๋Š” ์ ์—์„œ ๊ธฐ์ธํ•ฉ๋‹ˆ๋‹ค. ๊ต์ฐฉ์–ด๋ž€ ์กฐ์‚ฌ, ์–ด๋ฏธ ๋“ฑ์„ ๋ถ™์—ฌ์„œ ๋ง์„ ๋งŒ๋“œ๋Š” ์–ธ์–ด๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. 1) ๊ต์ฐฉ์–ด์˜ ํŠน์„ฑ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. ์˜์–ด์™€๋Š” ๋‹ฌ๋ฆฌ ํ•œ๊ตญ์–ด์—๋Š” ์กฐ์‚ฌ๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•œ๊ตญ์–ด์— ๊ทธ(he/him)๋ผ๋Š” ์ฃผ์–ด๋‚˜ ๋ชฉ์ ์–ด๊ฐ€ ๋“ค์–ด๊ฐ„ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ, ๊ทธ๋ผ๋Š” ๋‹จ์–ด ํ•˜๋‚˜์—๋„ '๊ทธ๊ฐ€', '๊ทธ์—๊ฒŒ', '๊ทธ๋ฅผ', '๊ทธ์™€', '๊ทธ๋Š”'๊ณผ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ์กฐ์‚ฌ๊ฐ€ '๊ทธ'๋ผ๋Š” ๊ธ€์ž ๋’ค์— ๋„์–ด์“ฐ๊ธฐ ์—†์ด ๋ฐ”๋กœ ๋ถ™๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋‹ค ๋ณด๋ฉด ๊ฐ™์€ ๋‹จ์–ด์ž„์—๋„ ์„œ๋กœ ๋‹ค๋ฅธ ์กฐ์‚ฌ๊ฐ€ ๋ถ™์–ด์„œ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ์ธ์‹์ด ๋˜๋ฉด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๊ฐ€ ํž˜๋“ค๊ณ  ๋ฒˆ๊ฑฐ๋กœ์›Œ์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํ•œ๊ตญ์–ด NLP์—์„œ ์กฐ์‚ฌ๋Š” ๋ถ„๋ฆฌํ•ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๊ฐ€ ์˜์–ด์ฒ˜๋Ÿผ ๋…๋ฆฝ์ ์ธ ๋‹จ์–ด๋ผ๋ฉด ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ํ† ํฐํ™”๋ฅผ ํ•˜๋ฉด ๋˜๊ฒ ์ง€๋งŒ ํ•œ๊ตญ์–ด๋Š” ์–ด์ ˆ์ด ๋…๋ฆฝ์ ์ธ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์กฐ์‚ฌ ๋“ฑ์˜ ๋ฌด์–ธ๊ฐ€๊ฐ€ ๋ถ™์–ด์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„์„œ ์ด๋ฅผ ์ „๋ถ€ ๋ถ„๋ฆฌํ•ด ์ค˜์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ํ† ํฐํ™”์—์„œ๋Š” ํ˜•ํƒœ์†Œ(morpheme) ๋ž€ ๊ฐœ๋…์„ ๋ฐ˜๋“œ์‹œ ์ดํ•ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ˜•ํƒœ์†Œ(morpheme)๋ž€ ๋œป์„ ๊ฐ€์ง„ ๊ฐ€์žฅ ์ž‘์€ ๋ง์˜ ๋‹จ์œ„๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด ํ˜•ํƒœ์†Œ์—๋Š” ๋‘ ๊ฐ€์ง€ ํ˜•ํƒœ์†Œ๊ฐ€ ์žˆ๋Š”๋ฐ ์ž๋ฆฝ ํ˜•ํƒœ์†Œ์™€ ์˜์กด ํ˜•ํƒœ์†Œ์ž…๋‹ˆ๋‹ค. ์ž๋ฆฝ ํ˜•ํƒœ์†Œ : ์ ‘์‚ฌ, ์–ด๋ฏธ, ์กฐ์‚ฌ์™€ ์ƒ๊ด€์—†์ด ์ž๋ฆฝํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ์†Œ. ๊ทธ ์ž์ฒด๋กœ ๋‹จ์–ด๊ฐ€ ๋œ๋‹ค. ์ฒด์–ธ(๋ช…์‚ฌ, ๋Œ€๋ช…์‚ฌ, ์ˆ˜์‚ฌ), ์ˆ˜์‹์–ธ(๊ด€ํ˜•์‚ฌ, ๋ถ€์‚ฌ), ๊ฐํƒ„์‚ฌ ๋“ฑ์ด ์žˆ๋‹ค. ์˜์กด ํ˜•ํƒœ์†Œ : ๋‹ค๋ฅธ ํ˜•ํƒœ์†Œ์™€ ๊ฒฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉ๋˜๋Š” ํ˜•ํƒœ์†Œ. ์ ‘์‚ฌ, ์–ด๋ฏธ, ์กฐ์‚ฌ, ์–ด๊ฐ„์„ ๋งํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๋ฌธ์žฅ : ์—๋””๊ฐ€ ์ฑ…์„ ์ฝ์—ˆ๋‹ค ์ด ๋ฌธ์žฅ์„ ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ['์—๋””๊ฐ€', '์ฑ…์„', '์ฝ์—ˆ๋‹ค'] ํ•˜์ง€๋งŒ ์ด๋ฅผ ํ˜•ํƒœ์†Œ ๋‹จ์œ„๋กœ ๋ถ„ํ•ดํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž๋ฆฝ ํ˜•ํƒœ์†Œ : ์—๋””, ์ฑ… ์˜์กด ํ˜•ํƒœ์†Œ : -๊ฐ€, -์„, ์ฝ-, -์—ˆ, -๋‹ค '์—๋””'๋ผ๋Š” ์‚ฌ๋žŒ ์ด๋ฆ„๊ณผ '์ฑ…'์ด๋ผ๋Š” ๋ช…์‚ฌ๋ฅผ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ํ•œ๊ตญ์–ด์—์„œ ์˜์–ด์—์„œ์˜ ๋‹จ์–ด ํ† ํฐํ™”์™€ ์œ ์‚ฌํ•œ ํ˜•ํƒœ๋ฅผ ์–ป์œผ๋ ค๋ฉด ์–ด์ ˆ ํ† ํฐ ํ™”๊ฐ€ ์•„๋‹ˆ๋ผ ํ˜•ํƒœ์†Œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. 2) ํ•œ๊ตญ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์˜์–ด๋ณด๋‹ค ์ž˜ ์ง€์ผœ์ง€์ง€ ์•Š๋Š”๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ํ•œ๊ตญ์–ด ์ฝ”ํผ์Šค๊ฐ€ ๋‰ด์Šค ๊ธฐ์‚ฌ์™€ ๊ฐ™์ด ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ฒ ์ €ํ•˜๊ฒŒ ์ง€ํ‚ค๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๋Š” ๊ธ€์ด๋ผ๋ฉด ์ข‹๊ฒ ์ง€๋งŒ, ๋งŽ์€ ๊ฒฝ์šฐ์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ํ‹€๋ ธ๊ฑฐ๋‚˜ ์ง€์ผœ์ง€์ง€ ์•Š๋Š” ์ฝ”ํผ์Šค๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด๋Š” ์˜์–ด๊ถŒ ์–ธ์–ด์™€ ๋น„๊ตํ•˜์—ฌ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์–ด๋ ต๊ณ  ์ž˜ ์ง€์ผœ์ง€์ง€ ์•Š๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์—ฌ๋Ÿฌ ๊ฒฌํ•ด๊ฐ€ ์žˆ์œผ๋‚˜, ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฒฌํ•ด๋Š” ํ•œ๊ตญ์–ด์˜ ๊ฒฝ์šฐ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ง€์ผœ์ง€์ง€ ์•Š์•„๋„ ๊ธ€์„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์—†๋˜ ํ•œ๊ตญ์–ด์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋ณดํŽธํ™”๋œ ๊ฒƒ๋„ ๊ทผ๋Œ€(1933๋…„, ํ•œ๊ธ€๋งž์ถค๋ฒ•ํ†ต์ผ์•ˆ)์˜ ์ผ์ž…๋‹ˆ๋‹ค. ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ „ํ˜€ ํ•˜์ง€ ์•Š์€ ํ•œ๊ตญ์–ด์™€ ์˜์–ด ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๋ด…์‹œ๋‹ค. EX1) ์ œ๊ฐ€ ์ด๋ ‡๊ฒŒ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ „ํ˜€ ํ•˜์ง€ ์•Š๊ณ  ๊ธ€์„ ์ผ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๊ธ€์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. EX2) Tobeornottobethatisthequestion ์˜์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด ์†์‰ฝ๊ฒŒ ์•Œ์•„๋ณด๊ธฐ ์–ด๋ ค์šด ๋ฌธ์žฅ๋“ค์ด ์ƒ๊น๋‹ˆ๋‹ค. ์ด๋Š” ํ•œ๊ตญ์–ด(๋ชจ์•„์“ฐ๊ธฐ ๋ฐฉ์‹)์™€ ์˜์–ด(ํ’€์–ด์“ฐ๊ธฐ ๋ฐฉ์‹)๋ผ๋Š” ์–ธ์–ด์  ํŠน์„ฑ์˜ ์ฐจ์ด์— ๊ธฐ์ธํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ๋ชจ์•„์“ฐ๊ธฐ์™€ ํ’€์–ด์“ฐ๊ธฐ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ํ•˜์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๊ฒฐ๋ก ์ ์œผ๋กœ ํ•œ๊ตญ์–ด๋Š” ์ˆ˜๋งŽ์€ ์ฝ”ํผ์Šค์—์„œ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋ฌด์‹œ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๊ฐ€ ์–ด๋ ค์›Œ์กŒ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 6. ํ’ˆ์‚ฌ ํƒœ๊น…(Part-of-speech tagging) ๋‹จ์–ด๋Š” ํ‘œ๊ธฐ๋Š” ๊ฐ™์ง€๋งŒ ํ’ˆ์‚ฌ์— ๋”ฐ๋ผ์„œ ๋‹จ์–ด์˜ ์˜๋ฏธ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์˜์–ด ๋‹จ์–ด 'fly'๋Š” ๋™์‚ฌ๋กœ๋Š” '๋‚ ๋‹ค'๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€๋งŒ, ๋ช…์‚ฌ๋กœ๋Š” 'ํŒŒ๋ฆฌ'๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. '๋ชป'์ด๋ผ๋Š” ๋‹จ์–ด๋Š” ๋ช…์‚ฌ๋กœ์„œ๋Š” ๋ง์น˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ชฉ์žฌ ๋”ฐ์œ„๋ฅผ ๊ณ ์ •ํ•˜๋Š” ๋ฌผ๊ฑด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ถ€์‚ฌ๋กœ์„œ์˜ '๋ชป'์€ '๋จน๋Š”๋‹ค', '๋‹ฌ๋ฆฐ๋‹ค'์™€ ๊ฐ™์€ ๋™์ž‘ ๋™์‚ฌ๋ฅผ ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์˜๋ฏธ๋กœ ์“ฐ์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ์ œ๋Œ€๋กœ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ์–ด๋–ค ํ’ˆ์‚ฌ๋กœ ์“ฐ์˜€๋Š”์ง€ ๋ณด๋Š” ๊ฒƒ์ด ์ฃผ์š” ์ง€ํ‘œ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์— ๋”ฐ๋ผ ๋‹จ์–ด ํ† ํฐํ™” ๊ณผ์ •์—์„œ ๊ฐ ๋‹จ์–ด๊ฐ€ ์–ด๋–ค ํ’ˆ์‚ฌ๋กœ ์“ฐ์˜€๋Š”์ง€๋ฅผ ๊ตฌ๋ถ„ํ•ด๋†“๊ธฐ๋„ ํ•˜๋Š”๋ฐ, ์ด ์ž‘์—…์„ ํ’ˆ์‚ฌ ํƒœ๊น…(part-of-speech tagging)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. NLTK์™€ KoNLPy๋ฅผ ํ†ตํ•ด ํ’ˆ์‚ฌ ํƒœ๊น… ์‹ค์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 7. NLTK์™€ KoNLPy๋ฅผ ์ด์šฉํ•œ ์˜์–ด, ํ•œ๊ตญ์–ด ํ† ํฐํ™” ์‹ค์Šต NLTK์—์„œ๋Š” Penn Treebank POS Tags๋ผ๋Š” ๊ธฐ์ค€์„ ์‚ฌ์šฉํ•˜์—ฌ ํ’ˆ์‚ฌ๋ฅผ ํƒœ๊น… ํ•ฉ๋‹ˆ๋‹ค. from nltk.tokenize import word_tokenize from nltk.tag import pos_tag text = "I am actively looking for Ph.D. students. and you are a Ph.D. student." tokenized_sentence = word_tokenize(text) print('๋‹จ์–ด ํ† ํฐํ™” :',tokenized_sentence) print('ํ’ˆ์‚ฌ ํƒœ๊น… :',pos_tag(tokenized_sentence)) ๋‹จ์–ด ํ† ํฐํ™” : ['I', 'am', 'actively', 'looking', 'for', 'Ph.D.', 'students', '.', 'and', 'you', 'are', 'a', 'Ph.D.', 'student', '.'] ํ’ˆ์‚ฌ ํƒœ๊น… : [('I', 'PRP'), ('am', 'VBP'), ('actively', 'RB'), ('looking', 'VBG'), ('for', 'IN'), ('Ph.D.', 'NNP'), ('students', 'NNS'), ('.', '.'), ('and', 'CC'), ('you', 'PRP'), ('are', 'VBP'), ('a', 'DT'), ('Ph.D.', 'NNP'), ('student', 'NN'), ('.', '.')] ์˜์–ด ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ’ˆ์‚ฌ ํƒœ๊น…์„ ์ˆ˜ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Penn Treebank POG Tags์—์„œ PRP๋Š” ์ธ์นญ ๋Œ€๋ช…์‚ฌ, VBP๋Š” ๋™์‚ฌ, RB๋Š” ๋ถ€์‚ฌ, VBG๋Š” ํ˜„์žฌ ๋ถ€์‚ฌ, IN์€ ์ „์น˜์‚ฌ, NNP๋Š” ๊ณ ์œ  ๋ช…์‚ฌ, NNS๋Š” ๋ณต์ˆ˜ํ˜• ๋ช…์‚ฌ, CC๋Š” ์ ‘์†์‚ฌ, DT๋Š” ๊ด€์‚ฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ๋Š” KoNLPy(์ฝ”์—”์—˜ํŒŒ์ด)๋ผ๋Š” ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”์—”์—˜ํŒŒ์ด๋ฅผ ํ†ตํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋กœ Okt(Open Korea Text), ๋ฉ”์บ…(Mecab), ์ฝ”๋ชจ๋ž€(Komoran), ํ•œ ๋‚˜๋ˆ”(Hannanum), ๊ผฌ๊ผฌ๋งˆ(Kkma)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด NLP์—์„œ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์–ด ํ† ํฐํ™”. ๋” ์ •ํ™•ํžˆ๋Š” ํ˜•ํƒœ์†Œ ํ† ํฐํ™”(morpheme tokenization)๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” Okt์™€ ๊ผฌ๊ผฌ๋งˆ ๋‘ ๊ฐœ์˜ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. from konlpy.tag import Okt from konlpy.tag import Kkma okt = Okt() kkma = Kkma() print('OKT ํ˜•ํƒœ์†Œ ๋ถ„์„ :',okt.morphs("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) print('OKT ํ’ˆ์‚ฌ ํƒœ๊น… :',okt.pos("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) print('OKT ๋ช…์‚ฌ ์ถ”์ถœ :',okt.nouns("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) OKT ํ˜•ํƒœ์†Œ ๋ถ„์„ : ['์—ด์‹ฌํžˆ', '์ฝ”๋”ฉ', 'ํ•œ', '๋‹น์‹ ', ',', '์—ฐํœด', '์—๋Š”', '์—ฌํ–‰', '์„', '๊ฐ€๋ด์š”'] OKT ํ’ˆ์‚ฌ ํƒœ๊น… : [('์—ด์‹ฌํžˆ', 'Adverb'), ('์ฝ”๋”ฉ', 'Noun'), ('ํ•œ', 'Josa'), ('๋‹น์‹ ', 'Noun'), (',', 'Punctuation'), ('์—ฐํœด', 'Noun'), ('์—๋Š”', 'Josa'), ('์—ฌํ–‰', 'Noun'), ('์„', 'Josa'), ('๊ฐ€๋ด์š”', 'Verb')] OKT ๋ช…์‚ฌ ์ถ”์ถœ : ['์ฝ”๋”ฉ', '๋‹น์‹ ', '์—ฐํœด', '์—ฌํ–‰'] ์œ„์˜ ์˜ˆ์ œ๋Š” Okt ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋กœ ํ† ํฐํ™”๋ฅผ ์‹œ๋„ํ•ด ๋ณธ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ฉ”์„œ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 1) morphs : ํ˜•ํƒœ์†Œ ์ถ”์ถœ 2) pos : ํ’ˆ์‚ฌ ํƒœ๊น…(Part-of-speech tagging) 3) nouns : ๋ช…์‚ฌ ์ถ”์ถœ ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์ฝ”์—”์—˜ํŒŒ์ด์˜ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋“ค์€ ๊ณตํ†ต์ ์œผ๋กœ ์ด ๋ฉ”์„œ๋“œ๋“ค์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ ํ˜•ํƒœ์†Œ ์ถ”์ถœ๊ณผ ํ’ˆ์‚ฌ ํƒœ๊น… ๋ฉ”์„œ๋“œ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์กฐ์‚ฌ๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด NLP์—์„œ ์ „์ฒ˜๋ฆฌ์— ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ต‰์žฅํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ผฌ๊ผฌ๋งˆ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ™์€ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. print('๊ผฌ๊ผฌ๋งˆ ํ˜•ํƒœ์†Œ ๋ถ„์„ :',kkma.morphs("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) print('๊ผฌ๊ผฌ๋งˆ ํ’ˆ์‚ฌ ํƒœ๊น… :',kkma.pos("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) print('๊ผฌ๊ผฌ๋งˆ ๋ช…์‚ฌ ์ถ”์ถœ :',kkma.nouns("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) ๊ผฌ๊ผฌ๋งˆ ํ˜•ํƒœ์†Œ ๋ถ„์„ : ['์—ด์‹ฌํžˆ', '์ฝ”๋”ฉ', 'ํ•˜', 'ใ„ด', '๋‹น์‹ ', ',', '์—ฐํœด', '์—', '๋Š”', '์—ฌํ–‰', '์„', '๊ฐ€๋ณด', '์•„์š”'] ๊ผฌ๊ผฌ๋งˆ ํ’ˆ์‚ฌ ํƒœ๊น… : [('์—ด์‹ฌํžˆ', 'MAG'), ('์ฝ”๋”ฉ', 'NNG'), ('ํ•˜', 'XSV'), ('ใ„ด', 'ETD'), ('๋‹น์‹ ', 'NP'), (',', 'SP'), ('์—ฐํœด', 'NNG'), ('์—', 'JKM'), ('๋Š”', 'JX'), ('์—ฌํ–‰', 'NNG'), ('์„', 'JKO'), ('๊ฐ€๋ณด', 'VV'), ('์•„์š”', 'EFN')] ๊ผฌ๊ผฌ๋งˆ ๋ช…์‚ฌ ์ถ”์ถœ : ['์ฝ”๋”ฉ', '๋‹น์‹ ', '์—ฐํœด', '์—ฌํ–‰'] ์•ž์„œ ์‚ฌ์šฉํ•œ Okt ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์™€ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋Š” ์„ฑ๋Šฅ๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์—, ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์˜ ์„ ํƒ์€ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ํ•„์š” ์šฉ๋„์— ์–ด๋–ค ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๊ฐ€ ๊ฐ€์žฅ ์ ์ ˆํ•œ์ง€๋ฅผ ํŒ๋‹จํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์†๋„๋ฅผ ์ค‘์‹œํ•œ๋‹ค๋ฉด ๋ฉ”์บ…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 02-02 ์ •์ œ(Cleaning) and ์ •๊ทœํ™”(Normalization) ์ฝ”ํผ์Šค์—์„œ ์šฉ๋„์— ๋งž๊ฒŒ ํ† ํฐ์„ ๋ถ„๋ฅ˜ํ•˜๋Š” ์ž‘์—…์„ ํ† ํฐํ™”(tokenization)๋ผ๊ณ  ํ•˜๋ฉฐ, ํ† ํฐํ™” ์ž‘์—… ์ „, ํ›„์—๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์šฉ๋„์— ๋งž๊ฒŒ ์ •์ œ(cleaning) ๋ฐ ์ •๊ทœํ™”(normalization) ํ•˜๋Š” ์ผ์ด ํ•ญ์ƒ ํ•จ๊ป˜ํ•ฉ๋‹ˆ๋‹ค. ์ •์ œ ๋ฐ ์ •๊ทœํ™”์˜ ๋ชฉ์ ์€ ๊ฐ๊ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ •์ œ(cleaning) : ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. ์ •๊ทœํ™”(normalization) : ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์„ ํ†ตํ•ฉ์‹œ์ผœ์„œ ๊ฐ™์€ ๋‹จ์–ด๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค. ์ •์ œ ์ž‘์—…์€ ํ† ํฐํ™” ์ž‘์—…์— ๋ฐฉํ•ด๊ฐ€ ๋˜๋Š” ๋ถ€๋ถ„๋“ค์„ ๋ฐฐ์ œ์‹œํ‚ค๊ณ  ํ† ํฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ† ํฐํ™” ์ž‘์—…๋ณด๋‹ค ์•ž์„œ ์ด๋ฃจ์–ด์ง€๊ธฐ๋„ ํ•˜์ง€๋งŒ, ํ† ํฐํ™” ์ž‘์—… ์ดํ›„์—๋„ ์—ฌ์ „ํžˆ ๋‚จ์•„์žˆ๋Š” ๋…ธ์ด์ฆˆ๋“ค์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ์ง€์†์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์™„๋ฒฝํ•œ ์ •์ œ ์ž‘์—…์€ ์–ด๋ ค์šด ํŽธ์ด๋ผ์„œ, ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์ด ์ •๋„๋ฉด ๋๋‹ค.๋ผ๋Š” ์ผ์ข…์˜ ํ•ฉ์˜์ ์„ ์ฐพ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 1. ๊ทœ์น™์— ๊ธฐ๋ฐ˜ํ•œ ํ‘œ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์˜ ํ†ตํ•ฉ ํ•„์š”์— ๋”ฐ๋ผ ์ง์ ‘ ์ฝ”๋”ฉ์„ ํ†ตํ•ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๊ทœํ™” ๊ทœ์น™์˜ ์˜ˆ๋กœ์„œ ๊ฐ™์€ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Œ์—๋„, ํ‘œ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์„ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ์ •๊ทœํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, USA์™€ US๋Š” ๊ฐ™์€ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ์ •๊ทœํ™”ํ•ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. uh-huh์™€ uhhuh๋Š” ํ˜•ํƒœ๋Š” ๋‹ค๋ฅด์ง€๋งŒ ์—ฌ์ „ํžˆ ๊ฐ™์€ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ •๊ทœํ™”๋ฅผ ๊ฑฐ์น˜๊ฒŒ ๋˜๋ฉด, US๋ฅผ ์ฐพ์•„๋„ USA๋„ ํ•จ๊ป˜ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋’ค์—์„œ ํ‘œ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์„ ํ†ตํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ ์–ด๊ฐ„ ์ถ”์ถœ(stemming)๊ณผ ํ‘œ์ œ์–ด ์ถ”์ถœ(lemmatizaiton)์— ๋Œ€ํ•ด์„œ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. 2. ๋Œ€, ์†Œ๋ฌธ์ž ํ†ตํ•ฉ ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ ๋Œ€, ์†Œ๋ฌธ์ž๋ฅผ ํ†ตํ•ฉํ•˜๋Š” ๊ฒƒ์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ์ •๊ทœํ™” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ ๋Œ€๋ฌธ์ž๋Š” ๋ฌธ์žฅ์˜ ๋งจ ์•ž ๋“ฑ๊ณผ ๊ฐ™์€ ํŠน์ • ์ƒํ™ฉ์—์„œ๋งŒ ์“ฐ์ด๊ณ , ๋Œ€๋ถ€๋ถ„์˜ ๊ธ€์€ ์†Œ๋ฌธ์ž๋กœ ์ž‘์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€, ์†Œ๋ฌธ์ž ํ†ตํ•ฉ ์ž‘์—…์€ ๋Œ€๋ถ€๋ถ„ ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์ž‘์—…์œผ๋กœ ์ด๋ฃจ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์ด ์™œ ์œ ์šฉํ•œ์ง€ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, Automobile์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ๋ฌธ์žฅ์˜ ์ฒซ ๋‹จ์–ด์˜€๊ธฐ ๋•Œ๋ฌธ์— A๊ฐ€ ๋Œ€๋ฌธ์ž์˜€๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์— ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์„ ์‚ฌ์šฉํ•˜๋ฉด, automobile์„ ์ฐพ๋Š” ์งˆ์˜(query)์˜ ๊ฒฐ๊ณผ๋กœ์„œ Automobile๋„ ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰ ์—”์ง„์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ํŽ˜๋ผ๋ฆฌ ์ฐจ๋Ÿ‰์— ๊ด€์‹ฌ์ด ์žˆ์–ด์„œ ํŽ˜๋ผ๋ฆฌ๋ฅผ ๊ฒ€์ƒ‰ํ•ด ๋ณธ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์—„๋ฐ€ํžˆ ๋งํ•ด์„œ ์‚ฌ์‹ค ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฒฐ๊ณผ๋Š” a Ferrari car๋ผ๊ณ  ๋ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฒ€์ƒ‰ ์—”์ง„์€ ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์„ ์ ์šฉํ–ˆ์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ferrari๋งŒ ์ณ๋„ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๋ฌด์ž‘์ • ํ†ตํ•ฉํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ๊ตฌ๋ถ„๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น ๋ฏธ๊ตญ์„ ๋œปํ•˜๋Š” ๋‹จ์–ด US์™€ ์šฐ๋ฆฌ๋ฅผ ๋œปํ•˜๋Š” us๋Š” ๊ตฌ๋ถ„๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ ํšŒ์‚ฌ ์ด๋ฆ„(General Motors)๋‚˜, ์‚ฌ๋žŒ ์ด๋ฆ„(Bush) ๋“ฑ์€ ๋Œ€๋ฌธ์ž๋กœ ์œ ์ง€๋˜๋Š” ๊ฒƒ์ด ์˜ณ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํ† ํฐ์„ ์†Œ๋ฌธ์ž๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ฌธ์ œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค๋ฉด, ๋˜ ๋‹ค๋ฅธ ๋Œ€์•ˆ์€ ์ผ๋ถ€๋งŒ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์ด๋Ÿฐ ๊ทœ์น™์€ ์–ด๋–จ๊นŒ์š”? ๋ฌธ์žฅ์˜ ๋งจ ์•ž์—์„œ ๋‚˜์˜ค๋Š” ๋‹จ์–ด์˜ ๋Œ€๋ฌธ์ž๋งŒ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๊ณ , ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์€ ์ „๋ถ€ ๋Œ€๋ฌธ์ž์ธ ์ƒํƒœ๋กœ ๋†”๋‘๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ž‘์—…์€ ๋” ๋งŽ์€ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์„ ์–ธ์ œ ์‚ฌ์šฉํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ์‹œํ€€์Šค ๋ชจ๋ธ๋กœ ๋” ์ •ํ™•ํ•˜๊ฒŒ ์ง„ํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ์˜ฌ๋ฐ”๋ฅธ ๋Œ€๋ฌธ์ž ๋‹จ์–ด๋ฅผ ์–ป๊ณ  ์‹ถ์€ ์ƒํ™ฉ์—์„œ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•˜๋Š” ์ฝ”ํผ์Šค๊ฐ€ ์‚ฌ์šฉ์ž๋“ค์ด ๋‹จ์–ด์˜ ๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž์˜ ์˜ฌ๋ฐ”๋ฅธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ ์ƒ๊ด€์—†์ด ์†Œ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ๋“ค๋กœ๋ถ€ํ„ฐ ๋‚˜์˜จ ๋ฐ์ดํ„ฐ๋ผ๋ฉด ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ• ๋˜ํ•œ ๊ทธ๋‹ค์ง€ ๋„์›€์ด ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ์—๋Š” ์˜ˆ์™ธ ์‚ฌํ•ญ์„ ํฌ๊ฒŒ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ , ๋ชจ๋“  ์ฝ”ํผ์Šค๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์ข…์ข… ๋” ์‹ค์šฉ์ ์ธ ํ•ด๊ฒฐ์ฑ…์ด ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 3. ๋ถˆํ•„์š”ํ•œ ๋‹จ์–ด์˜ ์ œ๊ฑฐ ์ •์ œ ์ž‘์—…์—์„œ ์ œ๊ฑฐํ•ด์•ผ ํ•˜๋Š” ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ(noise data)๋Š” ์ž์—ฐ์–ด๊ฐ€ ์•„๋‹ˆ๋ฉด์„œ ์•„๋ฌด ์˜๋ฏธ๋„ ๊ฐ–์ง€ ์•Š๋Š” ๊ธ€์ž๋“ค(ํŠน์ˆ˜ ๋ฌธ์ž ๋“ฑ)์„ ์˜๋ฏธํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ชฉ์ ์— ๋งž์ง€ ์•Š๋Š” ๋ถˆํ•„์š” ๋‹จ์–ด๋“ค์„ ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋ถˆํ•„์š” ๋‹จ์–ด๋“ค์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ์™€ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ์ ์€ ๋‹จ์–ด, ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋“ค์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ๋Š” ๋ถˆ์šฉ์–ด ์ฑ•ํ„ฐ์—์„œ ๋”์šฑ ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ธฐ๋กœ ํ•˜๊ณ , ์—ฌ๊ธฐ์„œ๋Š” ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ์ ์€ ๋‹จ์–ด์™€ ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ๊ฐ„๋žตํžˆ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. (1) ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ์ ์€ ๋‹จ์–ด ๋•Œ๋กœ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ๋„ˆ๋ฌด ์ ๊ฒŒ ๋“ฑ์žฅํ•ด์„œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ๋„์›€์ด ๋˜์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž…๋ ฅ๋œ ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ์„ค๊ณ„ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด 100,000๊ฐœ์˜ ๋ฉ”์ผ์„ ๊ฐ€์ง€๊ณ  ์ •์ƒ ๋ฉ”์ผ์—์„œ๋Š” ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ์ฃผ๋กœ ๋“ฑ์žฅํ•˜๊ณ , ์ŠคํŒธ ๋ฉ”์ผ์—์„œ๋Š” ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ์ฃผ๋กœ ๋“ฑ์žฅํ•˜๋Š”์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์„ค๊ณ„ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋•Œ 100,000๊ฐœ์˜ ๋ฉ”์ผ ๋ฐ์ดํ„ฐ์—์„œ ์ดํ•ฉ 5๋ฒˆ ๋ฐ–์— ๋“ฑ์žฅํ•˜์ง€ ์•Š์€ ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด ๋‹จ์–ด๋Š” ์ง๊ด€์ ์œผ๋กœ ๋ถ„๋ฅ˜์— ๊ฑฐ์˜ ๋„์›€์ด ๋˜์ง€ ์•Š์„ ๊ฒƒ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (2) ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ๋Š” ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ์–ด๋Š ์ •๋„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํฌ๊ฒŒ ์˜๋ฏธ๊ฐ€ ์—†๋Š” ๋‹จ์–ด๋“ค์„ ์ œ๊ฑฐํ•˜๋Š” ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๋ถˆ์šฉ์–ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” 2์ฐจ ์ด์œ ๋Š” ๊ธธ์ด๋ฅผ ์กฐ๊ฑด์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ์‚ญ์ œํ•˜๋ฉด์„œ ๋‹จ์–ด๊ฐ€ ์•„๋‹Œ ๊ตฌ๋‘์ ๋“ค๊นŒ์ง€๋„ ํ•œ๊บผ๋ฒˆ์— ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•จ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•œ๊ตญ์–ด์—์„œ๋Š” ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋ผ๊ณ  ์‚ญ์ œํ•˜๋Š” ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ํฌ๊ฒŒ ์œ ํšจํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ทธ ์ด์œ ์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์ •์ ์œผ๋กœ ๋งํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ, ์˜์–ด ๋‹จ์–ด์˜ ํ‰๊ท  ๊ธธ์ด๋Š” 6~7 ์ •๋„์ด๋ฉฐ, ํ•œ๊ตญ์–ด ๋‹จ์–ด์˜ ํ‰๊ท  ๊ธธ์ด๋Š” 2~3 ์ •๋„๋กœ ์ถ”์ •๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋‚˜๋ผ์˜ ๋‹จ์–ด ํ‰๊ท  ๊ธธ์ด๊ฐ€ ๋ช‡ ์ธ์ง€์— ๋Œ€ํ•ด์„œ๋Š” ํ™•์‹คํžˆ ๋งํ•˜๊ธฐ ์–ด๋ ต์ง€๋งŒ ๊ทธ๋Ÿผ์—๋„ ํ™•์‹คํ•œ ์‚ฌ์‹ค์€ ์˜์–ด ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ํ•œ๊ตญ์–ด ๋‹จ์–ด์˜ ๊ธธ์ด๋ณด๋‹ค๋Š” ํ‰๊ท ์ ์œผ๋กœ ๊ธธ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์˜์–ด ๋‹จ์–ด์™€ ํ•œ๊ตญ์–ด ๋‹จ์–ด์—์„œ ๊ฐ ํ•œ ๊ธ€์ž๊ฐ€ ๊ฐ€์ง„ ์˜๋ฏธ์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ์ ์—์„œ ๊ธฐ์ธํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ๋‹จ์–ด๋Š” ํ•œ์ž์–ด๊ฐ€ ๋งŽ๊ณ , ํ•œ ๊ธ€์ž๋งŒ์œผ๋กœ๋„ ์ด๋ฏธ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 'ํ•™๊ต'๋ผ๋Š” ํ•œ๊ตญ์–ด ๋‹จ์–ด๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด, ๋ฐฐ์šธ ํ•™(ๅญธ)๊ณผ ํ•™๊ต ๊ต(ๆ ก)๋กœ ๊ธ€์ž ํ•˜๋‚˜, ํ•˜๋‚˜๊ฐ€ ์ด๋ฏธ ํ•จ์ถ•์ ์ธ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์–ด ๋‘ ๊ธ€์ž๋งŒ์œผ๋กœ ํ•™๊ต๋ผ๋Š” ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์˜์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ํ•™๊ต๋ผ๋Š” ๊ธ€์ž๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” s, c, h, o, o, l์ด๋ผ๋Š” ์ด 6๊ฐœ์˜ ๊ธ€์ž๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์˜ˆ๋กœ๋Š” ์ „์„ค ์† ๋™๋ฌผ์ธ ์šฉ()์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•œ๊ตญ์–ด๋กœ๋Š” ํ•œ ๊ธ€์ž ๋ฉด ์ถฉ๋ถ„ํ•˜์ง€๋งŒ, ์˜์–ด์—์„œ๋Š” d, r, a, g, o, n์ด๋ผ๋Š” ์ด 6๊ฐœ์˜ ๊ธ€์ž๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ์˜์–ด๋Š” ๊ธธ์ด๊ฐ€ 2~3 ์ดํ•˜์ธ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ํฌ๊ฒŒ ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ๋ชปํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์ค„์ด๋Š” ํšจ๊ณผ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ–๊ณ  ์žˆ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ๊ธธ์ด๊ฐ€ 1์ธ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋Œ€๋ถ€๋ถ„์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ๋ชปํ•˜๋Š” ๋‹จ์–ด์ธ ๊ด€์‚ฌ 'a'์™€ ์ฃผ์–ด๋กœ ์“ฐ์ด๋Š” 'I'๊ฐ€ ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ธธ์ด๊ฐ€ 2์ธ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค๊ณ  ํ•˜๋ฉด it, at, to, on, in, by ๋“ฑ๊ณผ ๊ฐ™์€ ๋Œ€๋ถ€๋ถ„ ๋ถˆ์šฉ์–ด์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด๋“ค์ด ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ํ•„์š”์— ๋”ฐ๋ผ์„œ๋Š” ๊ธธ์ด๊ฐ€ 3์ธ ๋‹จ์–ด๋„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด ๊ฒฝ์šฐ fox, dog, car ๋“ฑ ๊ธธ์ด๊ฐ€ 3์ธ ๋ช…์‚ฌ๋“ค์ด ์ œ๊ฑฐ๋˜๊ธฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ์—์„œ ํ•ด๋‹น ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋„ ๋˜๋Š”์ง€์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. import re text = "I was wondering if anyone out there could enlighten me on this car." # ๊ธธ์ด๊ฐ€ 1~2์ธ ๋‹จ์–ด๋“ค์„ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ์‚ญ์ œ shortword = re.compile(r'\W*\b\w{1,2}\b') print(shortword.sub('', text)) was wondering anyone out there could enlighten this car. 4. ์ •๊ทœ ํ‘œํ˜„์‹(Regular Expression) ์–ป์–ด๋‚ธ ์ฝ”ํผ์Šค์—์„œ ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ์˜ ํŠน์ง•์„ ์žก์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ†ตํ•ด์„œ ์ด๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, HTML ๋ฌธ์„œ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜จ ์ฝ”ํผ์Šค๋ผ๋ฉด ๋ฌธ์„œ ์—ฌ๊ธฐ์ €๊ธฐ์— HTML ํƒœ๊ทธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‰ด์Šค ๊ธฐ์‚ฌ๋ฅผ ํฌ๋กค๋ง ํ–ˆ๋‹ค๋ฉด, ๊ธฐ์‚ฌ๋งˆ๋‹ค ๊ฒŒ์žฌ ์‹œ๊ฐ„์ด ์ ํ˜€์ ธ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์€ ์ด๋Ÿฌํ•œ ์ฝ”ํผ์Šค ๋‚ด์— ๊ณ„์†ํ•ด์„œ ๋“ฑ์žฅํ•˜๋Š” ๊ธ€์ž๋“ค์„ ๊ทœ์น™์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ํ•œ ๋ฒˆ์— ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ์„œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋„ ์ „์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ •๊ทœ ํ‘œํ˜„์‹์„ ์•ž์œผ๋กœ ์ข…์ข… ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๊ฒ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์—์„œ ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ๋„, ์ •๊ทœ ํ‘œํ˜„์‹์ด ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋’ค์—์„œ ์ข€ ๋” ์ƒ์„ธํ•˜๊ฒŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 02-03 ์–ด๊ฐ„ ์ถ”์ถœ(Stemming) and ํ‘œ์ œ์–ด ์ถ”์ถœ(Lemmatization) ์ •๊ทœํ™” ๊ธฐ๋ฒ• ์ค‘ ์ฝ”ํผ์Šค์— ์žˆ๋Š” ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ฒ•์ธ ํ‘œ์ œ์–ด ์ถ”์ถœ(lemmatization)๊ณผ ์–ด๊ฐ„ ์ถ”์ถœ(stemming)์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ๋‘˜์˜ ๊ฒฐ๊ณผ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ์ง€ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ์ž‘์—…์ด ๊ฐ–๊ณ  ์žˆ๋Š” ์˜๋ฏธ๋Š” ๋ˆˆ์œผ๋กœ ๋ดค์„ ๋•Œ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์ด์ง€๋งŒ, ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ์ผ๋ฐ˜ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋ฉด ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ์ผ๋ฐ˜ํ™”์‹œ์ผœ์„œ ๋ฌธ์„œ ๋‚ด์˜ ๋‹จ์–ด ์ˆ˜๋ฅผ ์ค„์ด๊ฒ ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•๋“ค์€ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ ์ž ํ•˜๋Š” ๋’ค์—์„œ ํ•™์Šตํ•˜๊ฒŒ ๋  BoW(Bag of Words) ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ฌธ์ œ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์ „์ฒ˜๋ฆฌ, ๋” ์ •ํ™•ํžˆ๋Š” ์ •๊ทœํ™”์˜ ์ง€ํ–ฅ์ ์€ ์–ธ์ œ๋‚˜ ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๋ณต์žก์„ฑ์„ ์ค„์ด๋Š” ์ผ์ž…๋‹ˆ๋‹ค. 1. ํ‘œ์ œ์–ด ์ถ”์ถœ(Lemmatization) ํ‘œ์ œ์–ด(Lemma)๋Š” ํ•œ๊ธ€๋กœ๋Š” 'ํ‘œ์ œ์–ด' ๋˜๋Š” '๊ธฐ๋ณธ ์‚ฌ์ „ํ˜• ๋‹จ์–ด' ์ •๋„์˜ ์˜๋ฏธ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ํ‘œ์ œ์–ด ์ถ”์ถœ์€ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ํ‘œ์ œ์–ด๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ํ‘œ์ œ์–ด ์ถ”์ถœ์€ ๋‹จ์–ด๋“ค์ด ๋‹ค๋ฅธ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋”๋ผ๋„, ๊ทธ ๋ฟŒ๋ฆฌ ๋‹จ์–ด๋ฅผ ์ฐพ์•„๊ฐ€์„œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ am, are, is๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์ŠคํŽ ๋ง์ด์ง€๋งŒ ๊ทธ ๋ฟŒ๋ฆฌ ๋‹จ์–ด๋Š” be๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, ์ด ๋‹จ์–ด๋“ค์˜ ํ‘œ์ œ์–ด๋Š” be๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ‘œ์ œ์–ด ์ถ”์ถœ์„ ํ•˜๋Š” ๊ฐ€์žฅ ์„ฌ์„ธํ•œ ๋ฐฉ๋ฒ•์€ ๋‹จ์–ด์˜ ํ˜•ํƒœํ•™์  ํŒŒ์‹ฑ์„ ๋จผ์ € ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ˜•ํƒœ์†Œ๋ž€ '์˜๋ฏธ๋ฅผ ๊ฐ€์ง„ ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„'๋ฅผ ๋œปํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ˜•ํƒœํ•™(morphology)์ด๋ž€ ํ˜•ํƒœ์†Œ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋“ค์„ ๋งŒ๋“ค์–ด๊ฐ€๋Š” ํ•™๋ฌธ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. ํ˜•ํƒœ์†Œ์˜ ์ข…๋ฅ˜๋กœ ์–ด๊ฐ„(stem)๊ณผ ์ ‘์‚ฌ(affix)๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 1) ์–ด๊ฐ„(stem) : ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋‹จ์–ด์˜ ํ•ต์‹ฌ ๋ถ€๋ถ„. 2) ์ ‘์‚ฌ(affix) : ๋‹จ์–ด์— ์ถ”๊ฐ€์ ์ธ ์˜๋ฏธ๋ฅผ ์ฃผ๋Š” ๋ถ€๋ถ„. ํ˜•ํƒœํ•™์  ํŒŒ์‹ฑ์€ ์ด ๋‘ ๊ฐ€์ง€ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ์ž‘์—…์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, cats๋ผ๋Š” ๋‹จ์–ด์— ๋Œ€ํ•ด ํ˜•ํƒœํ•™์  ํŒŒ์‹ฑ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๋ฉด, ํ˜•ํƒœํ•™์  ํŒŒ์‹ฑ์€ ๊ฒฐ๊ณผ๋กœ cat(์–ด๊ฐ„)์™€ -s(์ ‘์‚ฌ)๋ฅผ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ผญ ๋‘ ๊ฐ€์ง€๋กœ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด fox๋Š” ํ˜•ํƒœํ•™์  ํŒŒ์‹ฑ์„ ํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๋” ์ด์ƒ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. fox๋Š” ๋…๋ฆฝ์ ์ธ ํ˜•ํƒœ์†Œ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์™€ ์œ ์‚ฌํ•˜๊ฒŒ cat ๋˜ํ•œ ๋” ์ด์ƒ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. NLTK์—์„œ๋Š” ํ‘œ์ œ์–ด ์ถ”์ถœ์„ ์œ„ํ•œ ๋„๊ตฌ์ธ WordNetLemmatizer๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํ‘œ์ œ์–ด ์ถ”์ถœ์„ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from nltk.stem import WordNetLemmatizer lemmatizer = WordNetLemmatizer() words = ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting'] print('ํ‘œ์ œ์–ด ์ถ”์ถœ ์ „ :',words) print('ํ‘œ์ œ์–ด ์ถ”์ถœ ํ›„ :',[lemmatizer.lemmatize(word) for word in words]) ํ‘œ์ œ์–ด ์ถ”์ถœ ์ „ : ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting'] ํ‘œ์ œ์–ด ์ถ”์ถœ ํ›„ : ['policy', 'doing', 'organization', 'have', 'going', 'love', 'life', 'fly', 'dy', 'watched', 'ha', 'starting'] ํ‘œ์ œ์–ด ์ถ”์ถœ์€ ๋’ค์—์„œ ์–ธ๊ธ‰ํ•  ์–ด๊ฐ„ ์ถ”์ถœ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๋‹จ์–ด์˜ ํ˜•ํƒœ๊ฐ€ ์ ์ ˆํžˆ ๋ณด์กด๋˜๋Š” ์–‘์ƒ์„ ๋ณด์ด๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋Ÿผ์—๋„ ์œ„์˜ ๊ฒฐ๊ณผ์—์„œ๋Š” dy๋‚˜ ha์™€ ๊ฐ™์ด ์˜๋ฏธ๋ฅผ ์•Œ ์ˆ˜ ์—†๋Š” ์ ์ ˆํ•˜์ง€ ๋ชปํ•œ ๋‹จ์–ด๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ‘œ์ œ์–ด ์ถ”์ถœ๊ธฐ(lemmatizer)๊ฐ€ ๋ณธ๋ž˜ ๋‹จ์–ด์˜ ํ’ˆ์‚ฌ ์ •๋ณด๋ฅผ ์•Œ์•„์•ผ๋งŒ ์ •ํ™•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. WordNetLemmatizer๋Š” ์ž…๋ ฅ์œผ๋กœ ๋‹จ์–ด๊ฐ€ ๋™์‚ฌ ํ’ˆ์‚ฌ๋ผ๋Š” ์‚ฌ์‹ค์„ ์•Œ๋ ค์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, dies์™€ watched, has๊ฐ€ ๋ฌธ์žฅ์—์„œ ๋™์‚ฌ๋กœ ์“ฐ์˜€๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ค€๋‹ค๋ฉด ํ‘œ์ œ์–ด ์ถ”์ถœ๊ธฐ๋Š” ํ’ˆ์‚ฌ์˜ ์ •๋ณด๋ฅผ ๋ณด์กดํ•˜๋ฉด์„œ ์ •ํ™•ํ•œ Lemma๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. lemmatizer.lemmatize('dies', 'v') 'die' lemmatizer.lemmatize('watched', 'v') 'watch' lemmatizer.lemmatize('has', 'v') 'have' ํ‘œ์ œ์–ด ์ถ”์ถœ์€ ๋ฌธ๋งฅ์„ ๊ณ ๋ คํ•˜๋ฉฐ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๋Š” ํ•ด๋‹น ๋‹จ์–ด์˜ ํ’ˆ์‚ฌ ์ •๋ณด๋ฅผ ๋ณด์กดํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์–ด๊ฐ„ ์ถ”์ถœ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋Š” ํ’ˆ์‚ฌ ์ •๋ณด๊ฐ€ ๋ณด์กด๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํžˆ๋Š” ์–ด๊ฐ„ ์ถ”์ถœ์„ ํ•œ ๊ฒฐ๊ณผ๋Š” ์‚ฌ์ „์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋‹จ์–ด์ผ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. 2. ์–ด๊ฐ„ ์ถ”์ถœ(Stemming) ์–ด๊ฐ„(Stem)์„ ์ถ”์ถœํ•˜๋Š” ์ž‘์—…์„ ์–ด๊ฐ„ ์ถ”์ถœ(stemming)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์–ด๊ฐ„ ์ถ”์ถœ์€ ํ˜•ํƒœํ•™์  ๋ถ„์„์„ ๋‹จ์ˆœํ™”ํ•œ ๋ฒ„์ „์ด๋ผ๊ณ  ๋ณผ ์ˆ˜๋„ ์žˆ๊ณ , ์ •ํ•ด์ง„ ๊ทœ์น™๋งŒ ๋ณด๊ณ  ๋‹จ์–ด์˜ ์–ด๋ฏธ๋ฅผ ์ž๋ฅด๋Š” ์–ด๋ฆผ์ง์ž‘์˜ ์ž‘์—…์ด๋ผ๊ณ  ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ์„ฌ์„ธํ•œ ์ž‘์—…์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์–ด๊ฐ„ ์ถ”์ถœ ํ›„์— ๋‚˜์˜ค๋Š” ๊ฒฐ๊ณผ ๋‹จ์–ด๋Š” ์‚ฌ์ „์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋‹จ์–ด์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๊ฐ„ ์ถ”์ถœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜์ธ ํฌํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Porter Algorithm)์— ์•„๋ž˜์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ๋Š”๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. This was not the map we found in Billy Bones's chest, but an accurate copy, complete in all things--names and heights and soundings--with the single exception of the red crosses and the written notes. from nltk.stem import PorterStemmer from nltk.tokenize import word_tokenize stemmer = PorterStemmer() sentence = "This was not the map we found in Billy Bones's chest, but an accurate copy, complete in all things--names and heights and soundings--with the single exception of the red crosses and the written notes." tokenized_sentence = word_tokenize(sentence) print('์–ด๊ฐ„ ์ถ”์ถœ ์ „ :', tokenized_sentence) print('์–ด๊ฐ„ ์ถ”์ถœ ํ›„ :',[stemmer.stem(word) for word in tokenized_sentence]) ์–ด๊ฐ„ ์ถ”์ถœ ์ „ : ['This', 'was', 'not', 'the', 'map', 'we', 'found', 'in', 'Billy', 'Bones', "'s", 'chest', ',', 'but', 'an', 'accurate', 'copy', ',', 'complete', 'in', 'all', 'things', '--', 'names', 'and', 'heights', 'and', 'soundings', '--', 'with', 'the', 'single', 'exception', 'of', 'the', 'red', 'crosses', 'and', 'the', 'written', 'notes', '.'] ์–ด๊ฐ„ ์ถ”์ถœ ํ›„ : ['thi', 'wa', 'not', 'the', 'map', 'we', 'found', 'in', 'billi', 'bone', "'s", 'chest', ',', 'but', 'an', 'accur', 'copi', ',', 'complet', 'in', 'all', 'thing', '--', 'name', 'and', 'height', 'and', 'sound', '--', 'with', 'the', 'singl', 'except', 'of', 'the', 'red', 'cross', 'and', 'the', 'written', 'note', '.'] ๊ทœ์น™ ๊ธฐ๋ฐ˜์˜ ์ ‘๊ทผ์„ ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์–ด๊ฐ„ ์ถ”์ถœ ํ›„์˜ ๊ฒฐ๊ณผ์—๋Š” ์‚ฌ์ „์— ์—†๋Š” ๋‹จ์–ด๋“ค๋„ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, ํฌํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์–ด๊ฐ„ ์ถ”์ถœ์€ ์ด๋Ÿฌํ•œ ๊ทœ์น™๋“ค์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ALIZE โ†’ AL ANCE โ†’ ์ œ๊ฑฐ ICAL โ†’ IC ์œ„์˜ ๊ทœ์น™์— ๋”ฐ๋ฅด๋ฉด ์ขŒ์ธก์˜ ๋‹จ์–ด๋Š” ์šฐ์ธก์˜ ๋‹จ์–ด์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. formalize โ†’ formal allowance โ†’ allow electricical โ†’ electric ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. words = ['formalize', 'allowance', 'electricical'] print('์–ด๊ฐ„ ์ถ”์ถœ ์ „ :',words) print('์–ด๊ฐ„ ์ถ”์ถœ ํ›„ :',[stemmer.stem(word) for word in words]) ์–ด๊ฐ„ ์ถ”์ถœ ์ „ : ['formalize', 'allowance', 'electricical'] ์–ด๊ฐ„ ์ถ”์ถœ ํ›„ : ['formal', 'allow', 'electric'] โ€ป Porter ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ƒ์„ธ ๊ทœ์น™์€ ๋งˆํ‹ด ํฌํ„ฐ์˜ ํ™ˆํŽ˜์ด์ง€์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์–ด๊ฐ„ ์ถ”์ถœ ์†๋„๋Š” ํ‘œ์ œ์–ด ์ถ”์ถœ๋ณด๋‹ค ์ผ๋ฐ˜์ ์œผ๋กœ ๋น ๋ฅธ๋ฐ, ํฌํ„ฐ ์–ด๊ฐ„ ์ถ”์ถœ๊ธฐ๋Š” ์ •๋ฐ€ํ•˜๊ฒŒ ์„ค๊ณ„๋˜์–ด ์ •ํ™•๋„๊ฐ€ ๋†’์œผ๋ฏ€๋กœ ์˜์–ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์–ด๊ฐ„ ์ถ”์ถœ์„ ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ๊ฐ€์žฅ ์ค€์ˆ˜ํ•œ ์„ ํƒ์ž…๋‹ˆ๋‹ค. NLTK์—์„œ๋Š” ํฌํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์™ธ์—๋„ ๋žญ์ปค์Šคํ„ฐ ์Šคํƒœ๋จธ(Lancaster Stemmer) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํฌํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋žญ์ปค์Šคํ„ฐ ์Šคํƒœ๋จธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ฐ๊ฐ ์–ด๊ฐ„ ์ถ”์ถœ์„ ์ง„ํ–‰ํ–ˆ์„ ๋•Œ, ์ด ๋‘˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from nltk.stem import PorterStemmer from nltk.stem import LancasterStemmer porter_stemmer = PorterStemmer() lancaster_stemmer = LancasterStemmer() words = ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting'] print('์–ด๊ฐ„ ์ถ”์ถœ ์ „ :', words) print('ํฌํ„ฐ ์Šคํ…Œ๋จธ์˜ ์–ด๊ฐ„ ์ถ”์ถœ ํ›„:',[porter_stemmer.stem(w) for w in words]) print('๋žญ์ปค์Šคํ„ฐ ์Šคํ…Œ๋จธ์˜ ์–ด๊ฐ„ ์ถ”์ถœ ํ›„:',[lancaster_stemmer.stem(w) for w in words]) ์–ด๊ฐ„ ์ถ”์ถœ ์ „ : ['policy', 'doing', 'organization', 'have', 'going', 'love', 'lives', 'fly', 'dies', 'watched', 'has', 'starting'] ํฌํ„ฐ ์Šคํ…Œ๋จธ์˜ ์–ด๊ฐ„ ์ถ”์ถœ ํ›„: ['polici', 'do', 'organ', 'have', 'go', 'love', 'live', 'fli', 'die', 'watch', 'ha', 'start'] ๋žญ์ปค์Šคํ„ฐ ์Šคํ…Œ๋จธ์˜ ์–ด๊ฐ„ ์ถ”์ถœ ํ›„: ['policy', 'doing', 'org', 'hav', 'going', 'lov', 'liv', 'fly', 'die', 'watch', 'has', 'start'] ๋™์ผํ•œ ๋‹จ์–ด๋“ค์˜ ๋‚˜์—ด์— ๋Œ€ํ•ด์„œ ๋‘ ์Šคํƒœ๋จธ๋Š” ์ „ํ˜€ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋‘ ์Šคํƒœ๋จธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์„œ๋กœ ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ ์•Œ๋ ค์ง„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š”, ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์ฝ”ํผ์Šค์— ์Šคํƒœ๋จธ๋ฅผ ์ ์šฉํ•ด ๋ณด๊ณ  ์–ด๋–ค ์Šคํƒœ๋จธ๊ฐ€ ํ•ด๋‹น ์ฝ”ํผ์Šค์— ์ ํ•ฉํ•œ์ง€๋ฅผ ํŒ๋‹จํ•œ ํ›„์— ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ทœ์น™์— ๊ธฐ๋ฐ˜ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ข…์ข… ์ œ๋Œ€๋กœ ๋œ ์ผ๋ฐ˜ํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๊ฐ„ ์ถ”์ถœ์„ ํ•˜๊ณ  ๋‚˜์„œ ์ผ๋ฐ˜ํ™”๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ๋˜๊ฑฐ๋‚˜, ๋˜๋Š” ๋œ ๋˜๊ฑฐ๋‚˜ ํ•˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํฌํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ organization์„ ์–ด๊ฐ„ ์ถ”์ถœํ–ˆ์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ด…์‹œ๋‹ค. organization โ†’ organ organization๊ณผ organ์€ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋‹จ์–ด์ž„์—๋„ organization์— ๋Œ€ํ•ด์„œ ์–ด๊ฐ„ ์ถ”์ถœ์„ ํ–ˆ๋”๋‹ˆ organ์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. organ์— ๋Œ€ํ•ด์„œ ์–ด๊ฐ„ ์ถ”์ถœ์„ ํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๊ฒฐ๊ณผ๋Š” ์—ญ์‹œ organ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋‘ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์–ด๊ฐ„ ์ถ”์ถœ์„ ํ•œ๋‹ค๋ฉด ๋™์ผํ•œ ์–ด๊ฐ„์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์˜๋ฏธ๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ™์€ ๋‹จ์–ด๋ฅผ ์–ป๊ธฐ๋ฅผ ์›ํ•˜๋Š” ์ •๊ทœํ™”์˜ ๋ชฉ์ ์—๋Š” ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋™์ผํ•œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ํ‘œ์ œ์–ด ์ถ”์ถœ๊ณผ ์–ด๊ฐ„ ์ถ”์ถœ์„ ๊ฐ๊ฐ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ, ๊ฒฐ๊ณผ์—์„œ ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Stemming am โ†’ am the going โ†’ the go having โ†’ hav Lemmatization am โ†’ be the going โ†’ the going having โ†’ have 3. ํ•œ๊ตญ์–ด์—์„œ์˜ ์–ด๊ฐ„ ์ถ”์ถœ ํ•œ๊ตญ์–ด์˜ ์–ด๊ฐ„์— ๋Œ€ํ•ด์„œ ๊ฐ„๋žตํžˆ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด๋Š” ์•„๋ž˜์˜ ํ‘œ์™€ ๊ฐ™์ด 5 ์–ธ 9ํ’ˆ์‚ฌ์˜ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์–ธ ํ’ˆ์‚ฌ ์ฒด์–ธ ๋ช…์‚ฌ, ๋Œ€๋ช…์‚ฌ, ์ˆ˜์‚ฌ ์ˆ˜์‹์–ธ ๊ด€ํ˜•์‚ฌ, ๋ถ€์‚ฌ ๊ด€๊ณ„ ์–ธ ์กฐ์‚ฌ ๋…๋ฆฝ์–ธ ๊ฐํƒ„์‚ฌ ์šฉ์–ธ ๋™์‚ฌ, ํ˜•์šฉ์‚ฌ ์ด ์ค‘ ์šฉ์–ธ์— ํ•ด๋‹น๋˜๋Š” '๋™์‚ฌ'์™€ 'ํ˜•์šฉ์‚ฌ'๋Š” ์–ด๊ฐ„(stem)๊ณผ ์–ด๋ฏธ(ending)์˜ ๊ฒฐํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์šฉ์–ธ์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•˜๋Š” ๋ถ€๋ถ„์€ ์ „๋ถ€ ๋™์‚ฌ์™€ ํ˜•์šฉ์‚ฌ๋ฅผ ํฌํ•จํ•˜์—ฌ ์–ธ๊ธ‰ํ•˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. (1) ํ™œ์šฉ(conjugation) ํ™œ์šฉ(conjugation)์€ ํ•œ๊ตญ์–ด์—์„œ๋งŒ ๊ฐ€์ง€๋Š” ํŠน์ง•์ด ์•„๋‹ˆ๋ผ, ์ธ๋„์œ ๋Ÿฝ์–ด(indo-european language)์—์„œ๋„ ์ฃผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด์  ํŠน์ง• ์ค‘ ํ•˜๋‚˜๋ฅผ ๋งํ•˜๋Š” ํ†ต์นญ์ ์ธ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ํ•œ๊ตญ์–ด์— ํ•œ์ •ํ•˜์—ฌ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ํ™œ์šฉ์ด๋ž€ ์šฉ์–ธ์˜ ์–ด๊ฐ„(stem)์ด ์–ด๋ฏธ(ending)๋ฅผ ๊ฐ€์ง€๋Š” ์ผ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์–ด๊ฐ„(stem) : ์šฉ์–ธ(๋™์‚ฌ, ํ˜•์šฉ์‚ฌ)์„ ํ™œ์šฉํ•  ๋•Œ, ์›์น™์ ์œผ๋กœ ๋ชจ์–‘์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„. ํ™œ์šฉ์—์„œ ์–ด๋ฏธ์— ์„ ํ–‰ํ•˜๋Š” ๋ถ€๋ถ„. ๋•Œ๋ก  ์–ด๊ฐ„์˜ ๋ชจ์–‘๋„ ๋ฐ”๋€” ์ˆ˜ ์žˆ์Œ(์˜ˆ: ๊ธ‹๋‹ค, ๊ธ‹๊ณ , ๊ทธ์–ด์„œ, ๊ทธ์–ด๋ผ). ์–ด๋ฏธ(ending): ์šฉ์–ธ์˜ ์–ด๊ฐ„ ๋’ค์— ๋ถ™์–ด์„œ ํ™œ์šฉํ•˜๋ฉด์„œ ๋ณ€ํ•˜๋Š” ๋ถ€๋ถ„์ด๋ฉฐ, ์—ฌ๋Ÿฌ ๋ฌธ๋ฒ•์  ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ ํ™œ์šฉ์€ ์–ด๊ฐ„์ด ์–ด๋ฏธ๋ฅผ ์ทจํ•  ๋•Œ, ์–ด๊ฐ„์˜ ๋ชจ์Šต์ด ์ผ์ •ํ•˜๋‹ค๋ฉด ๊ทœ์น™ ํ™œ์šฉ, ์–ด๊ฐ„์ด๋‚˜ ์–ด๋ฏธ์˜ ๋ชจ์Šต์ด ๋ณ€ํ•˜๋Š” ๋ถˆ๊ทœ์น™ ํ™œ์šฉ์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. (2) ๊ทœ์น™ ํ™œ์šฉ ๊ทœ์น™ ํ™œ์šฉ์€ ์–ด๊ฐ„์ด ์–ด๋ฏธ๋ฅผ ์ทจํ•  ๋•Œ, ์–ด๊ฐ„์˜ ๋ชจ์Šต์ด ์ผ์ •ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ๋Š” ์–ด๊ฐ„๊ณผ ์–ด๋ฏธ๊ฐ€ ํ•ฉ์ณ์งˆ ๋•Œ, ์–ด๊ฐ„์˜ ํ˜•ํƒœ๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์žก/์–ด๊ฐ„ + ๋‹ค/์–ด๋ฏธ ์ด ๊ฒฝ์šฐ์—๋Š” ์–ด๊ฐ„์ด ์–ด๋ฏธ๊ฐ€ ๋ถ™๊ธฐ ์ „์˜ ๋ชจ์Šต๊ณผ ์–ด๋ฏธ๊ฐ€ ๋ถ™์€ ํ›„์˜ ๋ชจ์Šต์ด ๊ฐ™์œผ๋ฏ€๋กœ, ๊ทœ์น™ ๊ธฐ๋ฐ˜์œผ๋กœ ์–ด๋ฏธ๋ฅผ ๋‹จ์ˆœํžˆ ๋ถ„๋ฆฌํ•ด ์ฃผ๋ฉด ์–ด๊ฐ„ ์ถ”์ถœ์ด ๋ฉ๋‹ˆ๋‹ค. (3) ๋ถˆ๊ทœ์น™ ํ™œ์šฉ ๋ถˆ๊ทœ์น™ ํ™œ์šฉ์€ ์–ด๊ฐ„์ด ์–ด๋ฏธ๋ฅผ ์ทจํ•  ๋•Œ ์–ด๊ฐ„์˜ ๋ชจ์Šต์ด ๋ฐ”๋€Œ๊ฑฐ๋‚˜ ์ทจํ•˜๋Š” ์–ด๋ฏธ๊ฐ€ ํŠน์ˆ˜ํ•œ ์–ด๋ฏธ์ผ ๊ฒฝ์šฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜๋“ฃ-, ๋•-, ๊ณฑ-, ์ž‡-, ์˜ค๋ฅด-, ๋…ธ๋ž—-โ€™ ๋“ฑ์ด โ€˜๋“ฃ/๋“ค-, ๋•/๋„์šฐ-, ๊ณฑ/๊ณ ์šฐ-, ์ž‡/์ด-, ์˜ฌ/์˜ฌ-, ๋…ธ๋ž—/๋…ธ๋ผ-โ€™์™€ ๊ฐ™์ด ์–ด๊ฐ„์˜<NAME>์ด ๋‹ฌ๋ผ์ง€๋Š” ์ผ์ด ์žˆ๊ฑฐ๋‚˜ โ€˜์˜ค๋ฅด+ ์•„/์–ดโ†’์˜ฌ๋ผ, ํ•˜+์•„/์–ดโ†’ํ•˜์—ฌ, ์ด๋ฅด+์•„/์–ดโ†’์ด๋ฅด๋Ÿฌ, ํ‘ธ๋ฅด+์•„/์–ดโ†’ํ‘ธ๋ฅด๋Ÿฌโ€™์™€ ๊ฐ™์ด ์ผ๋ฐ˜์ ์ธ ์–ด๋ฏธ๊ฐ€ ์•„๋‹Œ ํŠน์ˆ˜ํ•œ ์–ด๋ฏธ๋ฅผ ์ทจํ•˜๋Š” ๊ฒฝ์šฐ ๋ถˆ๊ทœ์น™ํ™œ์šฉ์„ ํ•˜๋Š” ์˜ˆ์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ์–ด๊ฐ„์ด ์–ด๋ฏธ๊ฐ€ ๋ถ™๋Š” ๊ณผ์ •์—์„œ ์–ด๊ฐ„์˜ ๋ชจ์Šต์ด ๋ฐ”๋€Œ์—ˆ์œผ๋ฏ€๋กœ ๋‹จ์ˆœํ•œ ๋ถ„๋ฆฌ๋งŒ์œผ๋กœ ์–ด๊ฐ„ ์ถ”์ถœ์ด ๋˜์ง€ ์•Š๊ณ  ์ข€ ๋” ๋ณต์žกํ•œ ๊ทœ์น™์„ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋งํฌ๋Š” ๋‹ค์–‘ํ•œ ๋ถˆ๊ทœ์น™ ํ™œ์šฉ์˜ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋งํฌ : https://namu.wiki/w/ํ•œ๊ตญ์–ด/๋ถˆ๊ทœ์น™%20ํ™œ์šฉ 02-04 ๋ถˆ์šฉ์–ด(Stopword) ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์—์„œ ์œ ์˜๋ฏธํ•œ ๋‹จ์–ด ํ† ํฐ๋งŒ์„ ์„ ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํฐ ์˜๋ฏธ๊ฐ€ ์—†๋Š” ๋‹จ์–ด ํ† ํฐ์„ ์ œ๊ฑฐํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํฐ ์˜๋ฏธ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์€ ์ž์ฃผ ๋“ฑ์žฅํ•˜์ง€๋งŒ ๋ถ„์„์„ ํ•˜๋Š” ๊ฒƒ์— ์žˆ์–ด์„œ๋Š” ํฐ ๋„์›€์ด ๋˜์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, I, my, me, over, ์กฐ์‚ฌ, ์ ‘๋ฏธ์‚ฌ ๊ฐ™์€ ๋‹จ์–ด๋“ค์€ ๋ฌธ์žฅ์—์„œ๋Š” ์ž์ฃผ ๋“ฑ์žฅํ•˜์ง€๋งŒ ์‹ค์ œ ์˜๋ฏธ ๋ถ„์„์„ ํ•˜๋Š” ๋ฐ๋Š” ๊ฑฐ์˜ ๊ธฐ์—ฌํ•˜๋Š” ๋ฐ”๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋‹จ์–ด๋“ค์„ ๋ถˆ์šฉ์–ด(stopword)๋ผ๊ณ  ํ•˜๋ฉฐ, NLTK์—์„œ๋Š” ์œ„์™€ ๊ฐ™์€ 100์—ฌ ๊ฐœ ์ด์ƒ์˜ ์˜์–ด ๋‹จ์–ด๋“ค์„ ๋ถˆ์šฉ์–ด๋กœ ํŒจํ‚ค์ง€ ๋‚ด์—์„œ ๋ฏธ๋ฆฌ ์ •์˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋ถˆ์šฉ์–ด๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์˜์–ด ๋ฌธ์žฅ์—์„œ NLTK๊ฐ€ ์ •์˜ํ•œ ์˜์–ด ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์‹ค์Šต์„ ํ•˜๊ณ , ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์—์„œ ์ง์ ‘ ์ •์˜ํ•œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. NLTK ์‹ค์Šต์—์„œ๋Š” 1์ฑ•ํ„ฐ์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด NLTK Data๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ ์‹œ์—๋Š” nltk.download(ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ)๋ผ๋Š” ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from nltk.corpus import stopwords from nltk.tokenize import word_tokenize from konlpy.tag import Okt 1. NLTK์—์„œ ๋ถˆ์šฉ์–ด ํ™•์ธํ•˜๊ธฐ stop_words_list = stopwords.words('english') print('๋ถˆ์šฉ์–ด ๊ฐœ์ˆ˜ :', len(stop_words_list)) print('๋ถˆ์šฉ์–ด 10๊ฐœ ์ถœ๋ ฅ :',stop_words_list[:10]) ๋ถˆ์šฉ์–ด ๊ฐœ์ˆ˜ : 179 ๋ถˆ์šฉ์–ด 10๊ฐœ ์ถœ๋ ฅ : ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're"] stopwords.words("english")๋Š” NLTK๊ฐ€ ์ •์˜ํ•œ ์˜์–ด ๋ถˆ์šฉ์–ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ 100๊ฐœ ์ด์ƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํžˆ 10๊ฐœ๋งŒ ํ™•์ธํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. 'i', 'me', 'my'์™€ ๊ฐ™์€ ๋‹จ์–ด๋“ค์„ NLTK์—์„œ ๋ถˆ์šฉ์–ด๋กœ ์ •์˜ํ•˜๊ณ  ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. NLTK๋ฅผ ํ†ตํ•ด์„œ ๋ถˆ์šฉ์–ด ์ œ๊ฑฐํ•˜๊ธฐ example = "Family is not an important thing. It's everything." stop_words = set(stopwords.words('english')) word_tokens = word_tokenize(example) result = [] for word in word_tokens: if word not in stop_words: result.append(word) print('๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ์ „ :',word_tokens) print('๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ํ›„ :',result) ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ์ „ : ['Family', 'is', 'not', 'an', 'important', 'thing', '.', 'It', "'s", 'everything', '.'] ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ํ›„ : ['Family', 'important', 'thing', '.', 'It', "'s", 'everything', '.'] ์œ„ ์ฝ”๋“œ๋Š” "Family is not an important thing. It's everything."๋ผ๋Š” ์ž„์˜์˜ ๋ฌธ์žฅ์„ ์ •์˜ํ•˜๊ณ , NLTK์˜ word_tokenize๋ฅผ ํ†ตํ•ด์„œ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹จ์–ด ํ† ํฐํ™” ๊ฒฐ๊ณผ๋กœ๋ถ€ํ„ฐ NLTK๊ฐ€ ์ •์˜ํ•˜๊ณ  ์žˆ๋Š” ๋ถˆ์šฉ์–ด๋ฅผ ์ œ์™ธํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ 'is', 'not', 'an'๊ณผ ๊ฐ™์€ ๋‹จ์–ด๋“ค์ด ๋ฌธ์žฅ์—์„œ ์ œ๊ฑฐ๋˜์—ˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ํ•œ๊ตญ์–ด์—์„œ ๋ถˆ์šฉ์–ด ์ œ๊ฑฐํ•˜๊ธฐ ํ•œ๊ตญ์–ด์—์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ๋Š” ํ† ํฐํ™” ํ›„์— ์กฐ์‚ฌ, ์ ‘์†์‚ฌ ๋“ฑ์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋ ค๊ณ  ํ•˜๋‹ค ๋ณด๋ฉด ์กฐ์‚ฌ๋‚˜ ์ ‘์†์‚ฌ์™€ ๊ฐ™์€ ๋‹จ์–ด๋“ค๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ช…์‚ฌ, ํ˜•์šฉ์‚ฌ์™€ ๊ฐ™์€ ๋‹จ์–ด๋“ค ์ค‘์—์„œ ๋ถˆ์šฉ์–ด๋กœ์„œ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ์€ ๋‹จ์–ด๋“ค์ด ์ƒ๊ธฐ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ์—๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ๋ถˆ์šฉ์–ด ์‚ฌ์ „์„ ๋งŒ๋“ค๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ง์ ‘ ๋ถˆ์šฉ์–ด๋ฅผ ์ •์˜ํ•ด ๋ณด๊ณ , ์ฃผ์–ด์ง„ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ๋ถˆ์šฉ์–ด ์‚ฌ์ „์œผ๋กœ๋ถ€ํ„ฐ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋ถˆ์šฉ์–ด๋Š” ์ž„์˜ ์„ ์ •ํ•œ ๊ฒƒ์œผ๋กœ ์‹ค์ œ ์˜๋ฏธ ์žˆ๋Š” ์„ ์ • ๊ธฐ์ค€์ด ์•„๋‹™๋‹ˆ๋‹ค. okt = Okt() example = "๊ณ ๊ธฐ๋ฅผ ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ๊ตฌ์šฐ๋ ค๊ณ  ํ•˜๋ฉด ์•ˆ ๋ผ. ๊ณ ๊ธฐ๋ผ๊ณ  ๋‹ค ๊ฐ™์€ ๊ฒŒ ์•„๋‹ˆ๊ฑฐ๋“ . ์˜ˆ์ปจ๋Œ€ ์‚ผ๊ฒน์‚ด์„ ๊ตฌ์šธ ๋•Œ๋Š” ์ค‘์š”ํ•œ ๊ฒŒ ์žˆ์ง€." stop_words = "๋ฅผ ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ๊ตฌ ์šฐ๋ ค ๊ณ  ์•ˆ ๋ผ ๊ฐ™์€ ๊ฒŒ ๊ตฌ์šธ ๋•Œ๋Š”" stop_words = set(stop_words.split(' ')) word_tokens = okt.morphs(example) result = [word for word in word_tokens if not word in stop_words] print('๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ์ „ :',word_tokens) print('๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ํ›„ :',result) ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ์ „ : ['๊ณ ๊ธฐ', '๋ฅผ', '์•„๋ฌด๋ ‡๊ฒŒ๋‚˜', '๊ตฌ', '์šฐ๋ ค', '๊ณ ', 'ํ•˜๋ฉด', '์•ˆ', '๋ผ', '.', '๊ณ ๊ธฐ', '๋ผ๊ณ ', '๋‹ค', '๊ฐ™์€', '๊ฒŒ', '์•„๋‹ˆ๊ฑฐ๋“ ', '.', '์˜ˆ์ปจ๋Œ€', '์‚ผ๊ฒน์‚ด', '์„', '๊ตฌ์šธ', '๋•Œ', '๋Š”', '์ค‘์š”ํ•œ', '๊ฒŒ', '์žˆ์ง€', '.'] ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ํ›„ : ['๊ณ ๊ธฐ', 'ํ•˜๋ฉด', '.', '๊ณ ๊ธฐ', '๋ผ๊ณ ', '๋‹ค', '์•„๋‹ˆ๊ฑฐ๋“ ', '.', '์˜ˆ์ปจ๋Œ€', '์‚ผ๊ฒน์‚ด', '์„', '์ค‘์š”ํ•œ', '์žˆ์ง€', '.'] ์•„๋ž˜์˜ ๋งํฌ๋Š” ๋ณดํŽธ์ ์œผ๋กœ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ๊ตญ์–ด ๋ถˆ์šฉ์–ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ ์ ˆ๋Œ€์ ์ธ ๊ธฐ์ค€์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋งํฌ : https://www.ranks.nl/stopwords/korean ๋ถˆ์šฉ์–ด๊ฐ€ ๋งŽ์€ ๊ฒฝ์šฐ์—๋Š” ์ฝ”๋“œ ๋‚ด์—์„œ ์ง์ ‘ ์ •์˜ํ•˜์ง€ ์•Š๊ณ  txt ํŒŒ์ผ์ด๋‚˜ csv ํŒŒ์ผ๋กœ ์ •๋ฆฌํ•ด๋†“๊ณ  ์ด๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 02-05 ์ •๊ทœ ํ‘œํ˜„์‹(Regular Expression) ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ์—์„œ ์ •๊ทœ ํ‘œํ˜„์‹์€ ์•„์ฃผ ์œ ์šฉํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํŒŒ์ด์ฌ์—์„œ ์ง€์›ํ•˜๊ณ  ์žˆ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ re์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ NLTK๋ฅผ ํ†ตํ•œ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ด์šฉํ•œ ํ† ํฐํ™”์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. 1. ์ •๊ทœ ํ‘œํ˜„์‹ ๋ฌธ๋ฒ•๊ณผ ๋ชจ๋“ˆ ํ•จ์ˆ˜ ํŒŒ์ด์ฌ์—์„œ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ re์„ ์ง€์›ํ•˜๋ฏ€๋กœ, ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด ํŠน์ • ๊ทœ์น™์ด ์žˆ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ •์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํŠน์ˆ˜ ๋ฌธ์ž์™€ ๋ชจ๋“ˆ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ํ‘œ๋ฅผ ํ†ตํ•ด ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ‘œ๋งŒ์œผ๋กœ๋Š” ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ํ‘œ ์•„๋ž˜์˜ ์‹ค์Šต๊ณผ ํ‘œ๋ฅผ ๋ณ‘ํ–‰ํ•˜์—ฌ ์ดํ•ดํ•˜์‹œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. 1) ์ •๊ทœ ํ‘œํ˜„์‹ ๋ฌธ๋ฒ• ์ •๊ทœ ํ‘œํ˜„์‹์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ๋ฒ• ์ค‘ ํŠน์ˆ˜ ๋ฌธ์ž๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํŠน์ˆ˜ ๋ฌธ์ž ์„ค๋ช… . ํ•œ ๊ฐœ์˜ ์ž„์˜์˜ ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. (์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž์ธ \n๋Š” ์ œ์™ธ) ? ์•ž์˜ ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. (๋ฌธ์ž๊ฐ€ 0๊ฐœ ๋˜๋Š” 1๊ฐœ) * ์•ž์˜ ๋ฌธ์ž๊ฐ€ ๋ฌดํ•œ๊ฐœ๋กœ ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. (๋ฌธ์ž๊ฐ€ 0๊ฐœ ์ด์ƒ) + ์•ž์˜ ๋ฌธ์ž๊ฐ€ ์ตœ์†Œ ํ•œ ๊ฐœ ์ด์ƒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. (๋ฌธ์ž๊ฐ€ 1๊ฐœ ์ด์ƒ) ^ ๋’ค์˜ ๋ฌธ์ž์—ด๋กœ ๋ฌธ์ž์—ด์ด ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค. $ ์•ž์˜ ๋ฌธ์ž์—ด๋กœ ๋ฌธ์ž์—ด์ด ๋๋‚ฉ๋‹ˆ๋‹ค. {์ˆซ์ž} ์ˆซ์ž๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. {์ˆซ์ž 1, ์ˆซ์ž 2} ์ˆซ์ž 1 ์ด์ƒ ์ˆซ์ž 2 ์ดํ•˜๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ?, *, +๋ฅผ ์ด๊ฒƒ์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. {์ˆซ์ž,} ์ˆซ์ž ์ด์ƒ๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. [ ] ๋Œ€๊ด„ํ˜ธ ์•ˆ์˜ ๋ฌธ์ž๋“ค ์ค‘ ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์™€ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. [amk]๋ผ๊ณ  ํ•œ๋‹ค๋ฉด a ๋˜๋Š” m ๋˜๋Š” k ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์กด์žฌํ•˜๋ฉด ๋งค์น˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [a-z]์™€ ๊ฐ™์ด ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. [a-zA-Z]๋Š” ์•ŒํŒŒ๋ฒณ ์ „์ฒด๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ฒ”์œ„์ด๋ฉฐ, ๋ฌธ์ž์—ด์— ์•ŒํŒŒ๋ฒณ์ด ์กด์žฌํ•˜๋ฉด ๋งค์น˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [^๋ฌธ์ž] ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋ฌธ์ž๋ฅผ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. l AlB์™€ ๊ฐ™์ด ์“ฐ์ด๋ฉฐ A ๋˜๋Š” B์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹ ๋ฌธ๋ฒ•์—๋Š” ์—ญ ์Šฌ๋ž˜์‰ฌ(\)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž์ฃผ ์“ฐ์ด๋Š” ๋ฌธ์ž ๊ทœ์น™๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž ๊ทœ์น™ ์„ค๋ช… \\\ ์—ญ ์Šฌ๋ž˜์‰ฌ ๋ฌธ์ž ์ž์ฒด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค \\d ๋ชจ๋“  ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [0-9]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. \\D ์ˆซ์ž๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [^0-9]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. \\s ๊ณต๋ฐฑ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [ \t\n\r\f\v]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. \\S ๊ณต๋ฐฑ์„ ์ œ์™ธํ•œ ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [^ \t\n\r\f\v]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. \\w ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [a-zA-Z0-9]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. \\W ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [^a-zA-Z0-9]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. 2) ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ ํ•จ์ˆ˜ ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ์—์„œ ์ง€์›ํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ชจ๋“ˆ ํ•จ์ˆ˜ ์„ค๋ช… re.compile() ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ปดํŒŒ์ผํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ํŒŒ์ด์ฌ์—๊ฒŒ ์ „ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ฐพ๊ณ ์ž ํ•˜๋Š” ํŒจํ„ด์ด ๋นˆ๋ฒˆํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ฏธ๋ฆฌ ์ปดํŒŒ์ผํ•ด๋†“๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ์†๋„์™€ ํŽธ ์˜์„ฑ๋ฉด์—์„œ ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. re.search() ๋ฌธ์ž์—ด ์ „์ฒด์— ๋Œ€ํ•ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋˜๋Š”์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. re.match() ๋ฌธ์ž์—ด์˜ ์ฒ˜์Œ์ด ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋˜๋Š”์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. re.split() ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. re.findall() ๋ฌธ์ž์—ด์—์„œ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ๋ฌธ์ž์—ด์„ ์ฐพ์•„์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋งค์น˜๋˜๋Š” ๋ฌธ์ž์—ด์ด ์—†๋‹ค๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค. re.finditer() ๋ฌธ์ž์—ด์—์„œ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์ดํ„ฐ ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. re.sub() ๋ฌธ์ž์—ด์—์„œ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ง„ํ–‰๋  ์‹ค์Šต์—์„œ๋Š” re.compile()์— ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ปดํŒŒ์ผํ•˜๊ณ , re.search()๋ฅผ ํ†ตํ•ด์„œ ํ•ด๋‹น ์ •๊ทœ ํ‘œํ˜„์‹์ด ์ž…๋ ฅ ํ…์ŠคํŠธ์™€ ๋งค์น˜๋˜๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋ฉด์„œ ๊ฐ ์ •๊ทœ ํ‘œํ˜„์‹์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. re.search()๋Š” ๋งค์น˜๋œ๋‹ค๋ฉด Match Object๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ๋งค์น˜๋˜์ง€ ์•Š์œผ๋ฉด ์•„๋ฌด๋Ÿฐ ๊ฐ’๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 2. ์ •๊ทœ ํ‘œํ˜„์‹ ์‹ค์Šต ์•ž์„œ ํ‘œ๋กœ ๋ดค๋˜ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import re 1) . ๊ธฐํ˜ธ .์€ ํ•œ ๊ฐœ์˜ ์ž„์˜์˜ ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด a.c๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. a์™€ c ์‚ฌ์ด์—๋Š” ์–ด๋–ค 1๊ฐœ์˜ ๋ฌธ์ž๋ผ๋„ ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. akc, azc, avc, a5c, a! c์™€ ๊ฐ™์€ ํ˜•ํƒœ๋Š” ๋ชจ๋‘ a.c์˜ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋ฉ๋‹ˆ๋‹ค. r = re.compile("a.c") r.search("kkk") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("abc") <_sre.SRE_Match object; span=(0, 3), match='abc'> .์€ ์–ด๋–ค ๋ฌธ์ž๋กœ๋„ ์ธ์‹๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— abc๋ผ๋Š” ๋ฌธ์ž์—ด์€ a.c๋ผ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ํŒจํ„ด์œผ๋กœ ๋งค์น˜๋ฉ๋‹ˆ๋‹ค. 2) ?๊ธฐํ˜ธ ?๋Š”? ์•ž์˜ ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ๊ณ  ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด ab? c๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ ์ด ์ •๊ทœ ํ‘œํ˜„์‹์—์„œ์˜ b๋Š” ์žˆ๋‹ค๊ณ  ์ทจ๊ธ‰ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์—†๋‹ค๊ณ  ์ทจ๊ธ‰ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, abc์™€ ac ๋ชจ๋‘ ๋งค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. r = re.compile("ab?c") r.search("abbc") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("abc") <_sre.SRE_Match object; span=(0, 3), match='abc'> b๊ฐ€ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•˜์—ฌ abc๋ฅผ ๋งค์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค. r.search("ac") <_sre.SRE_Match object; span=(0, 2), match='ac'> b๊ฐ€ ์—†๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•˜์—ฌ ac๋ฅผ ๋งค์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค. 3) *๊ธฐํ˜ธ *์€ ๋ฐ”๋กœ ์•ž์˜ ๋ฌธ์ž๊ฐ€ 0๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์•ž์˜ ๋ฌธ์ž๋Š” ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์ด ab*c๋ผ๋ฉด ac, abc, abbc, abbbc ๋“ฑ๊ณผ ๋งค์น˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ b์˜ ๊ฐœ์ˆ˜๋Š” ๋ฌด์ˆ˜ํžˆ ๋งŽ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. r = re.compile("ab*c") r.search("a") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("ac") <_sre.SRE_Match object; span=(0, 2), match='ac'> r.search("abc") <_sre.SRE_Match object; span=(0, 3), match='abc'> r.search("abbbbc") <_sre.SRE_Match object; span=(0, 6), match='abbbbc'> 4) +๊ธฐํ˜ธ +๋Š” *์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ ์€ ์•ž์˜ ๋ฌธ์ž๊ฐ€ ์ตœ์†Œ 1๊ฐœ ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์ด ab+c๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ac๋Š” ๋งค์น˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ abc, abbc, abbbc ๋“ฑ๊ณผ ๋งค์น˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ b์˜ ๊ฐœ์ˆ˜๋Š” ๋ฌด์ˆ˜ํžˆ ๋งŽ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. r = re.compile("ab+c") r.search("ac") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("abc") <_sre.SRE_Match object; span=(0, 3), match='abc'> r.search("abbbbc") <_sre.SRE_Match object; span=(0, 6), match='abbbbc'> 5) ^๊ธฐํ˜ธ ^๋Š” ์‹œ์ž‘๋˜๋Š” ๋ฌธ์ž์—ด์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์ด ^ab๋ผ๋ฉด ๋ฌธ์ž์—ด ab๋กœ ์‹œ์ž‘๋˜๋Š” ๊ฒฝ์šฐ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. r = re.compile("^ab") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("bbc") r.search("zab") r.search("abz") <re.Match object; span=(0, 2), match='ab'> 6) {์ˆซ์ž} ๊ธฐํ˜ธ ๋ฌธ์ž์— ํ•ด๋‹น ๊ธฐํ˜ธ๋ฅผ ๋ถ™์ด๋ฉด, ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋งŒํผ ๋ฐ˜๋ณตํ•œ ๊ฒƒ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด ab{2}c๋ผ๋ฉด a์™€ c ์‚ฌ์ด์— b๊ฐ€ ์กด์žฌํ•˜๋ฉด์„œ b๊ฐ€ 2๊ฐœ์ธ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. r = re.compile("ab{2}c") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("ac") r.search("abc") r.search("abbbbbc") r.search("abbc") <_sre.SRE_Match object; span=(0, 4), match='abbc'> 7) {์ˆซ์ž 1, ์ˆซ์ž 2} ๊ธฐํ˜ธ ๋ฌธ์ž์— ํ•ด๋‹น ๊ธฐํ˜ธ๋ฅผ ๋ถ™์ด๋ฉด, ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ˆซ์ž 1 ์ด์ƒ ์ˆซ์ž 2 ์ดํ•˜๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด ab{2,8}c๋ผ๋ฉด a์™€ c ์‚ฌ์ด์— b๊ฐ€ ์กด์žฌํ•˜๋ฉด์„œ b๋Š” 2๊ฐœ ์ด์ƒ 8๊ฐœ ์ดํ•˜์ธ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. r = re.compile("ab{2,8}c") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("ac") r.search("abc") r.search("abbbbbbbbbc") r.search("abbc") <_sre.SRE_Match object; span=(0, 4), match='abbc'> r.search("abbbbbbbbc") <_sre.SRE_Match object; span=(0, 10), match='abbbbbbbbc'> 8) {์ˆซ์ž,} ๊ธฐํ˜ธ ๋ฌธ์ž์— ํ•ด๋‹น ๊ธฐํ˜ธ๋ฅผ ๋ถ™์ด๋ฉด ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ˆซ์ž ์ด์ƒ๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด a{2, }bc๋ผ๋ฉด ๋’ค์— bc๊ฐ€ ๋ถ™์œผ๋ฉด์„œ a์˜ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ์ธ ๋ฌธ์ž์—ด๊ณผ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋งŒ์•ฝ {0, }์„ ์“ด๋‹ค๋ฉด *์™€ ๋™์ผํ•œ ์˜๋ฏธ๊ฐ€ ๋˜๋ฉฐ, {1, }์„ ์“ด๋‹ค๋ฉด +์™€ ๋™์ผํ•œ ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. r = re.compile("a{2, }bc") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("bc") r.search("aa") r.search("aabc") <_sre.SRE_Match object; span=(0, 4), match='aabc'> r.search("aaaaaaaabc") <_sre.SRE_Match object; span=(0, 10), match='aaaaaaaabc'> 9) [ ] ๊ธฐํ˜ธ [ ] ์•ˆ์— ๋ฌธ์ž๋“ค์„ ๋„ฃ์œผ๋ฉด ๊ทธ ๋ฌธ์ž๋“ค ์ค‘ ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์™€ ๋งค์น˜๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด [abc] ๋ผ๋ฉด, a ๋˜๋Š” b ๋˜๋Š” c๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๋ฌธ์ž์—ด๊ณผ ๋งค์น˜๋ฉ๋‹ˆ๋‹ค. ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. [a-zA-Z]๋Š” ์•ŒํŒŒ๋ฒณ ์ „๋ถ€๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, [0-9]๋Š” ์ˆซ์ž ์ „๋ถ€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. r = re.compile("[abc]") # [abc]๋Š” [a-c]์™€ ๊ฐ™๋‹ค. r.search("zzz") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("a") <_sre.SRE_Match object; span=(0, 1), match='a'> r.search("aaaaaaa") <_sre.SRE_Match object; span=(0, 1), match='a'> r.search("baac") <_sre.SRE_Match object; span=(0, 1), match='b'> ์ด๋ฒˆ์—๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ ๋ฒ”์œ„ ์ง€์ •ํ•˜์—ฌ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๋งŒ๋“ค์–ด๋ณด๊ณ  ๋ฌธ์ž์—ด๊ณผ ๋งค์น˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. r = re.compile("[a-z]") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("AAA") r.search("111") r.search("aBC") <_sre.SRE_Match object; span=(0, 1), match='a'> 10) [^๋ฌธ์ž] ๊ธฐํ˜ธ [^๋ฌธ์ž]๋Š” ^๊ธฐํ˜ธ ๋’ค์— ๋ถ™์€ ๋ฌธ์ž๋“ค์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ๋งค์น˜ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ [^abc]๋ผ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์ด ์žˆ๋‹ค๋ฉด, a ๋˜๋Š” b ๋˜๋Š” c๊ฐ€ ๋“ค์–ด๊ฐ„ ๋ฌธ์ž์—ด์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. r = re.compile("[^abc]") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("a") r.search("ab") r.search("b") r.search("d") <_sre.SRE_Match object; span=(0, 1), match='d'> r.search("1") <_sre.SRE_Match object; span=(0, 1), match='1'> 3. ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ ํ•จ์ˆ˜ ์˜ˆ์ œ ์•ž์„œ re.compile()๊ณผ re.search()๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (1) re.match() ์™€ re.search()์˜ ์ฐจ์ด search()๊ฐ€ ์ •๊ทœ ํ‘œํ˜„์‹ ์ „์ฒด์— ๋Œ€ํ•ด์„œ ๋ฌธ์ž์—ด์ด ๋งค์น˜ํ•˜๋Š”์ง€๋ฅผ ๋ณธ๋‹ค๋ฉด, match()๋Š” ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜ํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ์ค‘๊ฐ„์— ์ฐพ์„ ํŒจํ„ด์ด ์žˆ๋”๋ผ๋„ match ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์—์„œ ํŒจํ„ด์ด ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ์ฐพ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. r = re.compile("ab.") r.match("kkkabc") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("kkkabc") <_sre.SRE_Match object; span=(3, 6), match='abc'> r.match("abckkk") <_sre.SRE_Match object; span=(0, 3), match='abc'> ์œ„ ๊ฒฝ์šฐ ์ •๊ทœ ํ‘œํ˜„์‹์ด ab. ์ด๊ธฐ ๋•Œ๋ฌธ์—, ab ๋‹ค์Œ์—๋Š” ์–ด๋–ค ํ•œ ๊ธ€์ž๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ํŒจํ„ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. search ๋ชจ๋“ˆ ํ•จ์ˆ˜์— kkkabc๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด ๋งค์น˜๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค๋ฉด abc๋ผ๋Š” ๋ฌธ์ž์—ด์—์„œ ๋งค์น˜๋˜์–ด Match object๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ match ๋ชจ๋“ˆ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ์•ž ๋ถ€๋ถ„์ด ab. ์™€ ๋งค์น˜๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ˜๋Œ€๋กœ abckkk๋กœ ๋งค์น˜๋ฅผ ์‹œ๋„ํ•ด ๋ณด๋ฉด, ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ํŒจํ„ด๊ณผ ๋งค์น˜๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •์ƒ์ ์œผ๋กœ Match object๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. (2) re.split() split() ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๋œ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด๋“ค์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐํ™”์— ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด ๋ถ„๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋กœ์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•ด๋ด…์‹œ๋‹ค. # ๊ณต๋ฐฑ ๊ธฐ์ค€ ๋ถ„๋ฆฌ text = "์‚ฌ๊ณผ ๋”ธ๊ธฐ ์ˆ˜๋ฐ• ๋ฉœ๋ก  ๋ฐ”๋‚˜๋‚˜" re.split(" ", text) ['์‚ฌ๊ณผ', '๋”ธ๊ธฐ', '์ˆ˜๋ฐ•', '๋ฉœ๋ก ', '๋ฐ”๋‚˜๋‚˜'] ์ด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ค„๋ฐ”๊ฟˆ์ด๋‚˜ ๋‹ค๋ฅธ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ธฐ์ค€์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ๋ถ„๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. # ์ค„๋ฐ”๊ฟˆ ๊ธฐ์ค€ ๋ถ„๋ฆฌ text = """์‚ฌ๊ณผ ๋”ธ๊ธฐ ์ˆ˜๋ฐ• ๋ฉœ๋ก  ๋ฐ”๋‚˜๋‚˜""" re.split("\n", text) ['์‚ฌ๊ณผ', '๋”ธ๊ธฐ', '์ˆ˜๋ฐ•', '๋ฉœ๋ก ', '๋ฐ”๋‚˜๋‚˜'] # '+'๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌ text = "์‚ฌ๊ณผ+๋”ธ๊ธฐ+์ˆ˜๋ฐ•+๋ฉœ๋ก +๋ฐ”๋‚˜๋‚˜" re.split("\+", text) ['์‚ฌ๊ณผ', '๋”ธ๊ธฐ', '์ˆ˜๋ฐ•', '๋ฉœ๋ก ', '๋ฐ”๋‚˜๋‚˜'] (3) re.findall() findall() ํ•จ์ˆ˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ๋งค์น˜๋˜๋Š” ๋ฌธ์ž์—ด์ด ์—†๋‹ค๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ž„์˜์˜ ํ…์ŠคํŠธ์— ์ •๊ทœ ํ‘œํ˜„์‹์œผ๋กœ ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ทœ์น™์œผ๋กœ findall()์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ „์ฒด ํ…์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ์ˆซ์ž๋งŒ ์ฐพ์•„๋‚ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. text = """์ด๋ฆ„ : ๊น€์ฒ ์ˆ˜ ์ „ํ™”๋ฒˆํ˜ธ : 010 - 1234 - 1234 ๋‚˜์ด : 30 ์„ฑ๋ณ„ : ๋‚จ""" re.findall("\d+", text) ['010', '1234', '1234', '30'] ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ์ž…๋ ฅ ํ…์ŠคํŠธ์— ์ˆซ์ž๊ฐ€ ์—†๋‹ค๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. re.findall("\d+", "๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.") [] (4) re.sub() sub() ํ•จ์ˆ˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ์•„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ์ •์ œ ์ž‘์—…์— ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ, ์˜์–ด ๋ฌธ์žฅ์— ๊ฐ์ฃผ ๋“ฑ๊ณผ ๊ฐ™์€ ์ด์œ ๋กœ ํŠน์ˆ˜ ๋ฌธ์ž๊ฐ€ ์„ž์—ฌ์žˆ๋Š” ๊ฒฝ์šฐ์— ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•ŒํŒŒ๋ฒณ ์™ธ์˜ ๋ฌธ์ž๋Š” ๊ณต๋ฐฑ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋“ฑ์˜ ์šฉ๋„๋กœ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. text = "Regular expression : A regular expression, regex or regexp[1] (sometimes called a rational expression)[2][3] is, in theoretical computer science and formal language theory, a sequence of characters that define a search pattern." preprocessed_text = re.sub('[^a-zA-Z]', ' ', text) print(preprocessed_text) 'Regular expression A regular expression regex or regexp sometimes called a rational expression is in theoretical computer science and formal language theory a sequence of characters that define a search pattern ' 4. ์ •๊ทœ ํ‘œํ˜„์‹ ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ ์˜ˆ์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž„์˜์˜ ํ…์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ํ…Œ์ด๋ธ”<NAME>์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ…์ŠคํŠธ์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. text = """100 John PROF 101 James STUD 102 Mac STUD""" \s+๋Š” ๊ณต๋ฐฑ์„ ์ฐพ์•„๋‚ด๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์ž…๋‹ˆ๋‹ค. ๋’ค์— ๋ถ™๋Š” +๋Š” ์ตœ์†Œ 1๊ฐœ ์ด์ƒ์˜ ํŒจํ„ด์„ ์ฐพ์•„๋‚ธ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. s๋Š” ๊ณต๋ฐฑ์„ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์†Œ 1๊ฐœ ์ด์ƒ์˜ ๊ณต๋ฐฑ์ธ ํŒจํ„ด์„ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. split์€ ์ฃผ์–ด์ง„ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋ฏ€๋กœ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. re.split('\s+', text) ['100', 'John', 'PROF', '101', 'James', 'STUD', '102', 'Mac', 'STUD'] ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ’์ด ๊ตฌ๋ถ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ˆซ์ž๋งŒ์„ ๋ฝ‘์•„์˜จ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์„œ \d๋Š” ์ˆซ์ž์— ํ•ด๋‹น๋˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์ž…๋‹ˆ๋‹ค. +๋ฅผ ๋ถ™์ด๋ฉด ์ตœ์†Œ 1๊ฐœ ์ด์ƒ์˜ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. findall()์€ ํ•ด๋‹น ํ‘œํ˜„์‹์— ์ผ์น˜ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. re.findall('\d+',text) ['100', '101', '102] ์ด๋ฒˆ์—๋Š” ํ…์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ๋Œ€๋ฌธ์ž์ธ ํ–‰์˜ ๊ฐ’๋งŒ ๊ฐ€์ ธ์™€๋ด…์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ ์ •๊ทœ ํ‘œํ˜„์‹์— ๋Œ€๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋งค์น˜์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ •๊ทœ ํ‘œํ˜„์‹์— ๋Œ€๋ฌธ์ž๋ผ๋Š” ๊ธฐ์ค€๋งŒ์„ ๋„ฃ์„ ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์ž์—ด์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ชจ๋“  ๋Œ€๋ฌธ์ž ๊ฐ๊ฐ์„ ๊ฐ–๊ณ  ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. re.findall('[A-Z]',text) ['J', 'P', 'R', 'O', 'F', 'J', 'S', 'T', 'U', 'D', 'M', 'S', 'T', 'U', 'D'] ๋Œ€๋ฌธ์ž๊ฐ€ ์—ฐ์†์ ์œผ๋กœ ๋„ค ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋Š” ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. re.findall('[A-Z]{4}',text) ['PROF', 'STUD', 'STUD'] ๋Œ€๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด๋“ค์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ด๋ฆ„์˜ ๊ฒฝ์šฐ์—๋Š” ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ์„ž์—ฌ์žˆ๋Š” ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„์— ๋Œ€ํ•œ ํ–‰์˜ ๊ฐ’์„ ๊ฐ–๊ณ  ์˜ค๊ณ  ์‹ถ๋‹ค๋ฉด ์ฒ˜์Œ์— ๋Œ€๋ฌธ์ž๊ฐ€ ๋“ฑ์žฅํ•œ ํ›„์— ์†Œ๋ฌธ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ์— ๋งค์น˜ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. re.findall('[A-Z][a-z]+',text) ['John', 'James', 'Mac'] 5. ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ด์šฉํ•œ ํ† ํฐํ™” NLTK์—์„œ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด์„œ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” RegexpTokenizer๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. RegexpTokenizer()์—์„œ ๊ด„ํ˜ธ ์•ˆ์— ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ๊ทœ์ •ํ•˜๊ธฐ๋ฅผ ์›ํ•˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ๋„ฃ์–ด์„œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. tokenizer1์— ์‚ฌ์šฉํ•œ \w+๋Š” ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž๊ฐ€ 1๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. tokenizer2์—์„œ๋Š” ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ํ† ํฐํ™”ํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค. gaps=true๋Š” ํ•ด๋‹น ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ† ํฐ์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•œ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ gaps=True๋ผ๋Š” ๋ถ€๋ถ„์„ ๊ธฐ์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ํ† ํฐํ™”์˜ ๊ฒฐ๊ณผ๋Š” ๊ณต๋ฐฑ๋“ค๋งŒ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. tokenizer2์˜ ๊ฒฐ๊ณผ๋Š” ์œ„์˜ tokenizer1์˜ ๊ฒฐ๊ณผ์™€๋Š” ๋‹ฌ๋ฆฌ ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๋‚˜ ์˜จ์ ์„ ์ œ์™ธํ•˜์ง€ ์•Š๊ณ  ํ† ํฐ ํ™”๊ฐ€ ์ˆ˜ํ–‰๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from nltk.tokenize import RegexpTokenizer text = "Don't be fooled by the dark sounding name, Mr. Jone's Orphanage is as cheery as cheery goes for a pastry shop" tokenizer1 = RegexpTokenizer("[\w]+") tokenizer2 = RegexpTokenizer("\s+", gaps=True) print(tokenizer1.tokenize(text)) print(tokenizer2.tokenize(text)) ['Don', 't', 'be', 'fooled', 'by', 'the', 'dark', 'sounding', 'name', 'Mr', 'Jone', 's', 'Orphanage', 'is', 'as', 'cheery', 'as', 'cheery', 'goes', 'for', 'a', 'pastry', 'shop'] ["Don't", 'be', 'fooled', 'by', 'the', 'dark', 'sounding', 'name,', 'Mr.', "Jone's", 'Orphanage', 'is', 'as', 'cheery', 'as', 'cheery', 'goes', 'for', 'a', 'pastry', 'shop'] 02-06 ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ(Integer Encoding) ์ปดํ“จํ„ฐ๋Š” ํ…์ŠคํŠธ๋ณด๋‹ค๋Š” ์ˆซ์ž๋ฅผ ๋” ์ž˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ˆซ์ž๋กœ ๋ฐ”๊พธ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ๋ฒ•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋Ÿฌํ•œ ๊ธฐ๋ฒ•๋“ค์„ ๋ณธ๊ฒฉ์ ์œผ๋กœ ์ ์šฉ์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์ฒซ ๋‹จ๊ณ„๋กœ ๊ฐ ๋‹จ์–ด๋ฅผ ๊ณ ์œ ํ•œ ์ •์ˆ˜์— ๋งคํ•‘(mapping) ์‹œํ‚ค๋Š” ์ „์ฒ˜๋ฆฌ ์ž‘์—…์ด ํ•„์š”ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ–๊ณ  ์žˆ๋Š” ํ…์ŠคํŠธ์— ๋‹จ์–ด๊ฐ€ 5,000๊ฐœ๊ฐ€ ์žˆ๋‹ค๋ฉด, 5,000๊ฐœ์˜ ๋‹จ์–ด๋“ค ๊ฐ๊ฐ์— 1๋ฒˆ๋ถ€ํ„ฐ 5,000๋ฒˆ๊นŒ์ง€ ๋‹จ์–ด์™€ ๋งคํ•‘๋˜๋Š” ๊ณ ์œ ํ•œ ์ •์ˆ˜. ๋‹ค๋ฅธ ํ‘œํ˜„์œผ๋กœ๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, book์€ 150๋ฒˆ, dog๋Š” 171๋ฒˆ, love๋Š” 192๋ฒˆ, books๋Š” 212๋ฒˆ๊ณผ ๊ฐ™์ด ์ˆซ์ž๊ฐ€ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ๋žœ๋ค์œผ๋กœ ๋ถ€์—ฌํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ๋ณดํ†ต์€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ๋’ค์— ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. 1. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ(Integer Encoding) ์™œ ์ด๋Ÿฌํ•œ ์ž‘์—…์ด ํ•„์š”ํ•œ ์ง€์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ์‹ค์Šต์ด๋‚˜, ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ฑ•ํ„ฐ ๋“ฑ์—์„œ ์•Œ์•„๋ณด๊ธฐ๋กœ ํ•˜๊ณ  ์—ฌ๊ธฐ์„œ๋Š” ์–ด๋–ค ๊ณผ์ •์œผ๋กœ ๋‹จ์–ด์— ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋งŒ ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด์— ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ ๋‹จ์–ด๋ฅผ ๋นˆ๋„์ˆ˜ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ๋งŒ๋“ค๊ณ , ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ฐจ๋ก€๋กœ ๋‚ฎ์€ ์ˆซ์ž๋ถ€ํ„ฐ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๊ฐ€ ์ ๋‹นํ•˜๊ฒŒ ๋ถ„ํฌ๋˜๋„๋ก ์˜๋„์ ์œผ๋กœ ๋งŒ๋“  ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) dictionary ์‚ฌ์šฉํ•˜๊ธฐ from nltk.tokenize import sent_tokenize from nltk.tokenize import word_tokenize from nltk.corpus import stopwords raw_text = "A barber is a person. a barber is good person. a barber is huge person. he Knew A Secret! The Secret He Kept is huge secret. Huge secret. His barber kept his word. a barber kept his word. His barber kept his secret. But keeping and keeping such a huge secret to himself was driving the barber crazy. the barber went up a huge mountain." ์šฐ์„  ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์ด ํ•จ๊ป˜ ์žˆ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ๋ฌธ์žฅ ํ† ํฐํ™” sentences = sent_tokenize(raw_text) print(sentences) ['A barber is a person.', 'a barber is good person.', 'a barber is huge person.', 'he Knew A Secret!', 'The Secret He Kept is huge secret.', 'Huge secret.', 'His barber kept his word.', 'a barber kept his word.', 'His barber kept his secret.', 'But keeping and keeping such a huge secret to himself was driving the barber crazy.', 'the barber went up a huge mountain.'] ๊ธฐ์กด์˜ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌธ์žฅ ๋‹จ์œ„๋กœ ํ† ํฐํ™”๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ •์ œ ์ž‘์—…๊ณผ ์ •๊ทœํ™” ์ž‘์—…์„ ๋ณ‘ํ–‰ํ•˜๋ฉฐ, ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋‹จ์–ด๋“ค์„ ์†Œ๋ฌธ์žํ™”ํ•˜์—ฌ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ํ†ต์ผ์‹œํ‚ค๊ณ , ๋ถˆ์šฉ์–ด์™€ ๋‹จ์–ด ๊ธธ์ด๊ฐ€ 2์ดํ•˜์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด๋ฅผ ์ผ๋ถ€ ์ œ์™ธํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋ฅผ ์ˆ˜์น˜ํ™”ํ•˜๋Š” ๋‹จ๊ณ„๋ผ๋Š” ๊ฒƒ์€ ๋ณธ๊ฒฉ์ ์œผ๋กœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์— ๋“ค์–ด๊ฐ„๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ, ๋‹จ์–ด๊ฐ€ ํ…์ŠคํŠธ์ผ ๋•Œ๋งŒ ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ํ•œ์˜ ์ „์ฒ˜๋ฆฌ๋ฅผ ๋๋‚ด๋†“์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. vocab = {} preprocessed_sentences = [] stop_words = set(stopwords.words('english')) for sentence in sentences: # ๋‹จ์–ด ํ† ํฐํ™” tokenized_sentence = word_tokenize(sentence) result = [] for word in tokenized_sentence: word = word.lower() # ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์†Œ๋ฌธ์žํ™”ํ•˜์—ฌ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ธ๋‹ค. if word not in stop_words: # ๋‹จ์–ด ํ† ํฐํ™”๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. if len(word) > 2: # ๋‹จ์–ด ๊ธธ์ด๊ฐ€ 2์ดํ•˜์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•˜์—ฌ ์ถ”๊ฐ€๋กœ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. result.append(word) if word not in vocab: vocab[word] = 0 vocab[word] += 1 preprocessed_sentences.append(result) print(preprocessed_sentences) [['barber', 'person'], ['barber', 'good', 'person'], ['barber', 'huge', 'person'], ['knew', 'secret'], ['secret', 'kept', 'huge', 'secret'], ['huge', 'secret'], ['barber', 'kept', 'word'], ['barber', 'kept', 'word'], ['barber', 'kept', 'secret'], ['keeping', 'keeping', 'huge', 'secret', 'driving', 'barber', 'crazy'], ['barber', 'went', 'huge', 'mountain']] ํ˜„์žฌ vocab์—๋Š” ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ธฐ๋ก๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. vocab์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('๋‹จ์–ด ์ง‘ํ•ฉ :',vocab) ๋‹จ์–ด ์ง‘ํ•ฉ : {'barber': 8, 'person': 3, 'good': 1, 'huge': 5, 'knew': 1, 'secret': 6, 'kept': 4, 'word': 2, 'keeping': 2, 'driving': 1, 'crazy': 1, 'went': 1, 'mountain': 1} ํŒŒ์ด์ฌ์˜ ๋”•์…”๋„ˆ๋ฆฌ ๊ตฌ์กฐ๋กœ ๋‹จ์–ด๋ฅผ ํ‚ค(key)๋กœ, ๋‹จ์–ด์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ’(value)์œผ๋กœ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. vocab์— ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋นˆ๋„์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. # 'barber'๋ผ๋Š” ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜ ์ถœ๋ ฅ print(vocab["barber"]) ์ด์ œ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. vocab_sorted = sorted(vocab.items(), key = lambda x:x[1], reverse = True) print(vocab_sorted) [('barber', 8), ('secret', 6), ('huge', 5), ('kept', 4), ('person', 3), ('word', 2), ('keeping', 2), ('good', 1), ('knew', 1), ('driving', 1), ('crazy', 1), ('went', 1), ('mountain', 1)] ๋†’์€ ๋นˆ๋„์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด์ผ์ˆ˜๋ก ๋‚ฎ์€ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜๋Š” 1๋ถ€ํ„ฐ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. word_to_index = {} i = 0 for (word, frequency) in vocab_sorted : if frequency > 1 : # ๋นˆ๋„์ˆ˜๊ฐ€ ์ž‘์€ ๋‹จ์–ด๋Š” ์ œ์™ธ. i = i + 1 word_to_index[word] = i print(word_to_index) {'barber': 1, 'secret': 2, 'huge': 3, 'kept': 4, 'person': 5, 'word': 6, 'keeping': 7} 1์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด๊ฐ€ ๊ฐ€์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ๋‹จ์–ด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋™์‹œ์— ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์•Œ ๊ฒฝ์šฐ์—๋งŒ ํ•  ์ˆ˜ ์žˆ๋Š” ์ „์ฒ˜๋ฆฌ์ธ ๋นˆ๋„์ˆ˜๊ฐ€ ์ ์€ ๋‹จ์–ด๋ฅผ ์ œ์™ธํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋นˆ๋„์ˆ˜๊ฐ€ 1์ธ ๋‹จ์–ด๋“ค์€ ์ „๋ถ€ ์ œ์™ธํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋‹ค ๋ณด๋ฉด, ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋‹จ์–ด๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค๋Š” ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ n ๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์œ„ ๋‹จ์–ด๋“ค์€ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์œผ๋กœ ๋‚ฎ์€ ์ •์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋ผ ์žˆ์œผ๋ฏ€๋กœ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ n ๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ํ•˜๋ฉด vocab์—์„œ ์ •์ˆซ๊ฐ’์ด 1๋ถ€ํ„ฐ n๊นŒ์ง€์ธ ๋‹จ์–ด๋“ค๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ƒ์œ„ 5๊ฐœ ๋‹จ์–ด๋งŒ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. vocab_size = 5 # ์ธ๋ฑ์Šค๊ฐ€ 5 ์ดˆ๊ณผ์ธ ๋‹จ์–ด ์ œ๊ฑฐ words_frequency = [word for word, index in word_to_index.items() if index >= vocab_size + 1] # ํ•ด๋‹น ๋‹จ์–ด์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค ์ •๋ณด๋ฅผ ์‚ญ์ œ for w in words_frequency: del word_to_index[w] print(word_to_index) {'barber': 1, 'secret': 2, 'huge': 3, 'kept': 4, 'person': 5} word_to_index์—๋Š” ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ƒ์œ„ 5๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. word_to_index๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์–ด ํ† ํฐ ํ™”๊ฐ€ ๋œ ์ƒํƒœ๋กœ ์ €์žฅ๋œ sentences์— ์žˆ๋Š” ๊ฐ ๋‹จ์–ด๋ฅผ ์ •์ˆ˜๋กœ ๋ฐ”๊พธ๋Š” ์ž‘์—…์„ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด sentences์—์„œ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์€ ['barber', 'person']์ด์—ˆ๋Š”๋ฐ, ์ด ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ๋Š” [1, 5]๋กœ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์ธ ['barber', 'good', 'person']์—๋Š” ๋” ์ด์ƒ word_to_index์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋‹จ์–ด์ธ 'good'์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์ด ์ƒ๊ธฐ๋Š” ์ƒํ™ฉ์„ Out-Of-Vocabulary(๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด) ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•ฝ์ž๋กœ 'OOV ๋ฌธ์ œ'๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. word_to_index์— 'OOV'๋ž€ ๋‹จ์–ด๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•˜๊ณ , ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๋“ค์€ 'OOV'์˜ ์ธ๋ฑ์Šค๋กœ ์ธ์ฝ”๋”ฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. word_to_index['OOV'] = len(word_to_index) + 1 print(word_to_index) {'barber': 1, 'secret': 2, 'huge': 3, 'kept': 4, 'person': 5, 'OOV': 6} ์ด์ œ word_to_index๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ sentences์˜ ๋ชจ๋“  ๋‹จ์–ด๋“ค์„ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋กœ ์ธ์ฝ”๋”ฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. encoded_sentences = [] for sentence in preprocessed_sentences: encoded_sentence = [] for word in sentence: try: # ๋‹จ์–ด ์ง‘ํ•ฉ์— ์žˆ๋Š” ๋‹จ์–ด๋ผ๋ฉด ํ•ด๋‹น ๋‹จ์–ด์˜ ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ด. encoded_sentence.append(word_to_index[word]) except KeyError: # ๋งŒ์•ฝ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๋ผ๋ฉด 'OOV'์˜ ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ด. encoded_sentence.append(word_to_index['OOV']) encoded_sentences.append(encoded_sentence) print(encoded_sentences) [[1, 5], [1, 6, 5], [1, 3, 5], [6, 2], [2, 4, 3, 2], [3, 2], [1, 4, 6], [1, 4, 6], [1, 4, 2], [6, 6, 3, 2, 6, 1, 6], [1, 6, 3, 6]] ์ง€๊ธˆ๊นŒ์ง€ ํŒŒ์ด์ฌ์˜ dictionary ์ž๋ฃŒํ˜•์œผ๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋ณด๋‹ค๋Š” ์ข€ ๋” ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ Counter, FreqDist, enumerate๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. 2) Counter ์‚ฌ์šฉํ•˜๊ธฐ from collections import Counter print(preprocessed_sentences) [['barber', 'person'], ['barber', 'good', 'person'], ['barber', 'huge', 'person'], ['knew', 'secret'], ['secret', 'kept', 'huge', 'secret'], ['huge', 'secret'], ['barber', 'kept', 'word'], ['barber', 'kept', 'word'], ['barber', 'kept', 'secret'], ['keeping', 'keeping', 'huge', 'secret', 'driving', 'barber', 'crazy'], ['barber', 'went', 'huge', 'mountain']] ํ˜„์žฌ sentences๋Š” ๋‹จ์–ด ํ† ํฐ ํ™”๊ฐ€ ๋œ ๊ฒฐ๊ณผ๊ฐ€ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ sentences์—์„œ ๋ฌธ์žฅ์˜ ๊ฒฝ๊ณ„์ธ [, ]๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋‹จ์–ด๋“ค์„ ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค. # words = np.hstack(preprocessed_sentences)์œผ๋กœ๋„ ์ˆ˜ํ–‰ ๊ฐ€๋Šฅ. all_words_list = sum(preprocessed_sentences, []) print(all_words_list) ['barber', 'person', 'barber', 'good', 'person', 'barber', 'huge', 'person', 'knew', 'secret', 'secret', 'kept', 'huge', 'secret', 'huge', 'secret', 'barber', 'kept', 'word', 'barber', 'kept', 'word', 'barber', 'kept', 'secret', 'keeping', 'keeping', 'huge', 'secret', 'driving', 'barber', 'crazy', 'barber', 'went', 'huge', 'mountain'] ์ด๋ฅผ ํŒŒ์ด์ฌ์˜ Counter()์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ  ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. # ํŒŒ์ด์ฌ์˜ Counter ๋ชจ๋“ˆ์„ ์ด์šฉํ•˜์—ฌ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜ ์นด์šดํŠธ vocab = Counter(all_words_list) print(vocab) Counter({'barber': 8, 'secret': 6, 'huge': 5, 'kept': 4, 'person': 3, 'word': 2, 'keeping': 2, 'good': 1, 'knew': 1, 'driving': 1, 'crazy': 1, 'went': 1, 'mountain': 1}) ๋‹จ์–ด๋ฅผ ํ‚ค(key)๋กœ, ๋‹จ์–ด์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ’(value)์œผ๋กœ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. vocab์— ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋นˆ๋„์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. print(vocab["barber"]) # 'barber'๋ผ๋Š” ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜ ์ถœ๋ ฅ barber๋ž€ ๋‹จ์–ด๊ฐ€ ์ด 8๋ฒˆ ๋“ฑ์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. most_common()๋Š” ์ƒ์œ„ ๋นˆ๋„์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ฃผ์–ด์ง„ ์ˆ˜์˜ ๋‹จ์–ด๋งŒ์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ๋‹จ์–ด๋“ค์„ ์›ํ•˜๋Š” ๊ฐœ์ˆ˜๋งŒํผ๋งŒ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 5๊ฐœ์˜ ๋‹จ์–ด๋งŒ ๋‹จ์–ด ์ง‘ํ•ฉ์œผ๋กœ ์ €์žฅํ•ด ๋ด…์‹œ๋‹ค. vocab_size = 5 vocab = vocab.most_common(vocab_size) # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ƒ์œ„ 5๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์ €์žฅ vocab [('barber', 8), ('secret', 6), ('huge', 5), ('kept', 4), ('person', 3)] ์ด์ œ ๋†’์€ ๋นˆ๋„์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด์ผ์ˆ˜๋ก ๋‚ฎ์€ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. word_to_index = {} i = 0 for (word, frequency) in vocab : i = i + 1 word_to_index[word] = i print(word_to_index) {'barber': 1, 'secret': 2, 'huge': 3, 'kept': 4, 'person': 5} 3) NLTK์˜ FreqDist ์‚ฌ์šฉํ•˜๊ธฐ NLTK์—์„œ๋Š” ๋นˆ๋„์ˆ˜ ๊ณ„์‚ฐ ๋„๊ตฌ์ธ FreqDist()๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ์‚ฌ์šฉํ•œ Counter()๋ž‘ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from nltk import FreqDist import numpy as np # np.hstack์œผ๋กœ ๋ฌธ์žฅ ๊ตฌ๋ถ„์„ ์ œ๊ฑฐ vocab = FreqDist(np.hstack(preprocessed_sentences)) ๋‹จ์–ด๋ฅผ ํ‚ค(key)๋กœ, ๋‹จ์–ด์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ’(value)์œผ๋กœ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. vocab์— ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋นˆ๋„์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. print(vocab["barber"]) # 'barber'๋ผ๋Š” ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜ ์ถœ๋ ฅ barber๋ž€ ๋‹จ์–ด๊ฐ€ ์ด 8๋ฒˆ ๋“ฑ์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. most_common()๋Š” ์ƒ์œ„ ๋นˆ๋„์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ฃผ์–ด์ง„ ์ˆ˜์˜ ๋‹จ์–ด๋งŒ์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ๋‹จ์–ด๋“ค์„ ์›ํ•˜๋Š” ๊ฐœ์ˆ˜๋งŒํผ๋งŒ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 5๊ฐœ์˜ ๋‹จ์–ด๋งŒ ๋‹จ์–ด ์ง‘ํ•ฉ์œผ๋กœ ์ €์žฅํ•ด ๋ด…์‹œ๋‹ค. vocab_size = 5 vocab = vocab.most_common(vocab_size) # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ƒ์œ„ 5๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์ €์žฅ print(vocab) [('barber', 8), ('secret', 6), ('huge', 5), ('kept', 4), ('person', 3)] ์•ž์„œ Counter()๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์™€ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด์ „ ์‹ค์Šต๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋†’์€ ๋นˆ๋„์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด์ผ์ˆ˜๋ก ๋‚ฎ์€ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋ฒˆ์—๋Š” enumerate()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ข€ ๋” ์งง์€ ์ฝ”๋“œ๋กœ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. word_to_index = {word[0] : index + 1 for index, word in enumerate(vocab)} print(word_to_index) {'barber': 1, 'secret': 2, 'huge': 3, 'kept': 4, 'person': 5} ์œ„์™€ ๊ฐ™์ด ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•  ๋•Œ๋Š” enumerate()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. enumerate()์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์†Œ๊ฐœํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4) enumerate ์ดํ•ดํ•˜๊ธฐ enumerate()๋Š” ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ์ž๋ฃŒํ˜•(list, set, tuple, dictionary, string)์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์ธ๋ฑ์Šค๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ํ•จ๊ป˜ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด enumerate()๋ฅผ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. test_input = ['a', 'b', 'c', 'd', 'e'] for index, value in enumerate(test_input): # ์ž…๋ ฅ์˜ ์ˆœ์„œ๋Œ€๋กœ 0๋ถ€ํ„ฐ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•จ. print("value : {}, index: {}".format(value, index)) value : a, index: 0 value : b, index: 1 value : c, index: 2 value : d, index: 3 value : e, index: 4 ์œ„์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ํ† ํฐ์— ๋Œ€ํ•ด์„œ ์ธ๋ฑ์Šค๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฆ๊ฐ€๋˜๋ฉฐ ๋ถ€์—ฌ๋œ ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 2. ์ผ€๋ผ์Šค(Keras)์˜ ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ ์ผ€๋ผ์Šค(Keras)๋Š” ๊ธฐ๋ณธ์ ์ธ ์ „์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋„๊ตฌ๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์œ„ํ•ด์„œ ์ผ€๋ผ์Šค์˜ ์ „์ฒ˜๋ฆฌ ๋„๊ตฌ์ธ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜๋Š”๋ฐ, ์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ ๊ทธ ํŠน์ง•์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from tensorflow.keras.preprocessing.text import Tokenizer preprocessed_sentences = [['barber', 'person'], ['barber', 'good', 'person'], ['barber', 'huge', 'person'], ['knew', 'secret'], ['secret', 'kept', 'huge', 'secret'], ['huge', 'secret'], ['barber', 'kept', 'word'], ['barber', 'kept', 'word'], ['barber', 'kept', 'secret'], ['keeping', 'keeping', 'huge', 'secret', 'driving', 'barber', 'crazy'], ['barber', 'went', 'huge', 'mountain']] ๋‹จ์–ด ํ† ํฐํ™”๊นŒ์ง€ ์ˆ˜ํ–‰๋œ ์•ž์„œ ์‚ฌ์šฉํ•œ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์™€ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. tokenizer = Tokenizer() # fit_on_texts() ์•ˆ์— ์ฝ”ํผ์Šค๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ•˜๋ฉด ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ƒ์„ฑ. tokenizer.fit_on_texts(preprocessed_sentences) fit_on_texts๋Š” ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์œผ๋กœ ๋‚ฎ์€ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜๋Š”๋ฐ, ์ •ํ™•ํžˆ ์•ž์„œ ์„ค๋ช…ํ•œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ž‘์—…์ด ์ด๋ฃจ์–ด์ง„๋‹ค๊ณ  ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์— ์ธ๋ฑ์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ถ€์—ฌ๋˜์—ˆ๋Š”์ง€๋ฅผ ๋ณด๋ ค๋ฉด, word_index๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. print(tokenizer.word_index) {'barber': 1, 'secret': 2, 'huge': 3, 'kept': 4, 'person': 5, 'word': 6, 'keeping': 7, 'good': 8, 'knew': 9, 'driving': 10, 'crazy': 11, 'went': 12, 'mountain': 13} ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ธ๋ฑ์Šค๊ฐ€ ๋ถ€์—ฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด๊ฐ€ ์นด์šดํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜์˜€์„ ๋•Œ ๋ช‡ ๊ฐœ์˜€๋Š”์ง€๋ฅผ ๋ณด๊ณ ์ž ํ•œ๋‹ค๋ฉด word_counts๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. print(tokenizer.word_counts) OrderedDict([('barber', 8), ('person', 3), ('good', 1), ('huge', 5), ('knew', 1), ('secret', 6), ('kept', 4), ('word', 2), ('keeping', 2), ('driving', 1), ('crazy', 1), ('went', 1), ('mountain', 1)]) texts_to_sequences()๋Š” ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ ์ฝ”ํผ์Šค์— ๋Œ€ํ•ด์„œ ๊ฐ ๋‹จ์–ด๋ฅผ ์ด๋ฏธ ์ •ํ•ด์ง„ ์ธ๋ฑ์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. print(tokenizer.texts_to_sequences(preprocessed_sentences)) [[1, 5], [1, 8, 5], [1, 3, 5], [9, 2], [2, 4, 3, 2], [3, 2], [1, 4, 6], [1, 4, 6], [1, 4, 2], [7, 7, 3, 2, 10, 1, 11], [1, 12, 3, 13]] ์•ž์„œ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๋‹จ์–ด n ๊ฐœ๋งŒ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ most_common()์„ ์‚ฌ์šฉํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €์—์„œ๋Š” tokenizer = Tokenizer(num_words=์ˆซ์ž)์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ƒ์œ„ ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 1๋ฒˆ ๋‹จ์–ด๋ถ€ํ„ฐ 5๋ฒˆ ๋‹จ์–ด๊นŒ์ง€๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์žฌ์ •์˜ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. vocab_size = 5 tokenizer = Tokenizer(num_words = vocab_size + 1) # ์ƒ์œ„ 5๊ฐœ ๋‹จ์–ด๋งŒ ์‚ฌ์šฉ tokenizer.fit_on_texts(preprocessed_sentences) num_words์—์„œ +1์„ ๋”ํ•ด์„œ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š” ์ด์œ ๋Š” num_words๋Š” ์ˆซ์ž๋ฅผ 0๋ถ€ํ„ฐ ์นด์šดํŠธํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ 5๋ฅผ ๋„ฃ์œผ๋ฉด 0 ~ 4๋ฒˆ ๋‹จ์–ด ๋ณด์กด์„ ์˜๋ฏธํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ๋’ค์˜ ์‹ค์Šต์—์„œ 1๋ฒˆ ๋‹จ์–ด๋ถ€ํ„ฐ 4๋ฒˆ ๋‹จ์–ด๋งŒ ๋‚จ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— 1 ~ 5๋ฒˆ ๋‹จ์–ด๊นŒ์ง€ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด num_words์— ์ˆซ์ž 5๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ 5+1์ธ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์งˆ์ ์œผ๋กœ ์ˆซ์ž 0์— ์ง€์ •๋œ ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋ฐ๋„ ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์ˆซ์ž 0๊นŒ์ง€ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋กœ ์‚ฐ์ •ํ•˜๋Š” ์ด์œ ๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํŒจ๋”ฉ(padding)์ด๋ผ๋Š” ์ž‘์—… ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์— ๋‹ค๋ฃจ๊ฒŒ ๋˜๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋Š” ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ˆซ์ž 0๋„ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋กœ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค๊ณ ๋งŒ ์ดํ•ดํ•ฉ์‹œ๋‹ค. ๋‹ค์‹œ word_index๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(tokenizer.word_index) {'barber': 1, 'secret': 2, 'huge': 3, 'kept': 4, 'person': 5, 'word': 6, 'keeping': 7, 'good': 8, 'knew': 9, 'driving': 10, 'crazy': 11, 'went': 12, 'mountain': 13} ์ƒ์œ„ 5๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ์„ ์–ธํ•˜์˜€๋Š”๋ฐ ์—ฌ์ „ํžˆ 13๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ๋ชจ๋‘ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. word_counts๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(tokenizer.word_counts) OrderedDict([('barber', 8), ('person', 3), ('good', 1), ('huge', 5), ('knew', 1), ('secret', 6), ('kept', 4), ('word', 2), ('keeping', 2), ('driving', 1), ('crazy', 1), ('went', 1), ('mountain', 1)]) word_counts์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 13๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ๋ชจ๋‘ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์‹ค์ œ ์ ์šฉ์€ texts_to_sequences๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ ์šฉ์ด ๋ฉ๋‹ˆ๋‹ค. print(tokenizer.texts_to_sequences(preprocessed_sentences)) [[1, 5], [1, 5], [1, 3, 5], [2], [2, 4, 3, 2], [3, 2], [1, 4], [1, 4], [1, 4, 2], [3, 2, 1], [1, 3]] ์ฝ”ํผ์Šค์— ๋Œ€ํ•ด์„œ ๊ฐ ๋‹จ์–ด๋ฅผ ์ด๋ฏธ ์ •ํ•ด์ง„ ์ธ๋ฑ์Šค๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ, ์ƒ์œ„ 5๊ฐœ์˜ ๋‹จ์–ด๋งŒ์„ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ์ง€์ •ํ•˜์˜€์œผ๋ฏ€๋กœ 1๋ฒˆ ๋‹จ์–ด๋ถ€ํ„ฐ 5๋ฒˆ ๋‹จ์–ด๊นŒ์ง€๋งŒ ๋ณด์กด๋˜๊ณ  ๋‚˜๋จธ์ง€ ๋‹จ์–ด๋“ค์€ ์ œ๊ฑฐ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฝํ—˜์ƒ ๊ตณ์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ง€๋Š” ์•Š์ง€๋งŒ, ๋งŒ์•ฝ word_index์™€ word_counts์—์„œ๋„ ์ง€์ •๋œ num_words ๋งŒํผ์˜ ๋‹จ์–ด๋งŒ ๋‚จ๊ธฐ๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์˜ ์ฝ”๋“œ๋„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(preprocessed_sentences) vocab_size = 5 words_frequency = [word for word, index in tokenizer.word_index.items() if index >= vocab_size + 1] # ์ธ๋ฑ์Šค๊ฐ€ 5 ์ดˆ๊ณผ์ธ ๋‹จ์–ด ์ œ๊ฑฐ for word in words_frequency: del tokenizer.word_index[word] # ํ•ด๋‹น ๋‹จ์–ด์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค ์ •๋ณด๋ฅผ ์‚ญ์ œ del tokenizer.word_counts[word] # ํ•ด๋‹น ๋‹จ์–ด์— ๋Œ€ํ•œ ์นด์šดํŠธ ์ •๋ณด๋ฅผ ์‚ญ์ œ print(tokenizer.word_index) print(tokenizer.word_counts) print(tokenizer.texts_to_sequences(preprocessed_sentences)) {'barber': 1, 'secret': 2, 'huge': 3, 'kept': 4, 'person': 5} OrderedDict([('barber', 8), ('person', 3), ('huge', 5), ('secret', 6), ('kept', 4)]) [[1, 5], [1, 5], [1, 3, 5], [2], [2, 4, 3, 2], [3, 2], [1, 4], [1, 4], [1, 4, 2], [3, 2, 1], [1, 3]] ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด์ธ OOV์— ๋Œ€ํ•ด์„œ๋Š” ๋‹จ์–ด๋ฅผ ์ •์ˆ˜๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •์—์„œ ์•„์˜ˆ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๋“ค์€ OOV๋กœ ๊ฐ„์ฃผํ•˜์—ฌ ๋ณด์กดํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Tokenizer์˜ ์ธ์ž oov_token์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. # ์ˆซ์ž 0๊ณผ OOV๋ฅผ ๊ณ ๋ คํ•ด์„œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” +2 vocab_size = 5 tokenizer = Tokenizer(num_words = vocab_size + 2, oov_token = 'OOV') tokenizer.fit_on_texts(preprocessed_sentences) ๋งŒ์•ฝ oov_token์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค๋ฉด ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 'OOV'์˜ ์ธ๋ฑ์Šค๋ฅผ 1๋กœ ํ•ฉ๋‹ˆ๋‹ค. print('๋‹จ์–ด OOV์˜ ์ธ๋ฑ์Šค : {}'.format(tokenizer.word_index['OOV'])) ๋‹จ์–ด OOV์˜ ์ธ๋ฑ์Šค : 1 ์ด์ œ ์ฝ”ํผ์Šค์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. print(tokenizer.texts_to_sequences(preprocessed_sentences)) [[2, 6], [2, 1, 6], [2, 4, 6], [1, 3], [3, 5, 4, 3], [4, 3], [2, 5, 1], [2, 5, 1], [2, 5, 3], [1, 1, 4, 3, 1, 2, 1], [2, 1, 4, 1]] ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 5๊ฐœ์˜ ๋‹จ์–ด๋Š” 2 ~ 6๊นŒ์ง€์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์กŒ์œผ๋ฉฐ, ๊ทธ ์™ธ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” 'good'๊ณผ ๊ฐ™์€ ๋‹จ์–ด๋“ค์€ ์ „๋ถ€ 'OOV'์˜ ์ธ๋ฑ์Šค์ธ 1๋กœ ์ธ์ฝ”๋”ฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 02-07 ํŒจ๋”ฉ(Padding) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋‹ค ๋ณด๋ฉด ๊ฐ ๋ฌธ์žฅ(๋˜๋Š” ๋ฌธ์„œ)์€ ์„œ๋กœ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ธฐ๊ณ„๋Š” ๊ธธ์ด๊ฐ€ ์ „๋ถ€ ๋™์ผํ•œ ๋ฌธ์„œ๋“ค์— ๋Œ€ํ•ด์„œ๋Š” ํ•˜๋‚˜์˜ ํ–‰๋ ฌ๋กœ ๋ณด๊ณ , ํ•œ๊บผ๋ฒˆ์— ๋ฌถ์–ด์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ณ‘๋ ฌ ์—ฐ์‚ฐ์„ ์œ„ํ•ด์„œ ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์˜ ๊ธธ์ด๋ฅผ ์ž„์˜๋กœ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ฃผ๋Š” ์ž‘์—…์ด ํ•„์š”ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์Šต์„ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 1. Numpy๋กœ ํŒจ๋”ฉ ํ•˜๊ธฐ import numpy as np from tensorflow.keras.preprocessing.text import Tokenizer ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ฑ•ํ„ฐ์—์„œ ์ˆ˜ํ–‰ํ–ˆ๋˜ ์‹ค์Šต์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜๋ณตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. preprocessed_sentences = [['barber', 'person'], ['barber', 'good', 'person'], ['barber', 'huge', 'person'], ['knew', 'secret'], ['secret', 'kept', 'huge', 'secret'], ['huge', 'secret'], ['barber', 'kept', 'word'], ['barber', 'kept', 'word'], ['barber', 'kept', 'secret'], ['keeping', 'keeping', 'huge', 'secret', 'driving', 'barber', 'crazy'], ['barber', 'went', 'huge', 'mountain']] ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค๊ณ , ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(preprocessed_sentences) encoded = tokenizer.texts_to_sequences(preprocessed_sentences) print(encoded) ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ๊ณ ์œ ํ•œ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค. [[1, 5], [1, 8, 5], [1, 3, 5], [9, 2], [2, 4, 3, 2], [3, 2], [1, 4, 6], [1, 4, 6], [1, 4, 2], [7, 7, 3, 2, 10, 1, 11], [1, 12, 3, 13]] ๋ชจ๋‘ ๋™์ผํ•œ ๊ธธ์ด๋กœ ๋งž์ถฐ์ฃผ๊ธฐ ์œ„ํ•ด์„œ ์ด ์ค‘์—์„œ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋ฌธ์žฅ์˜ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. max_len = max(len(item) for item in encoded) print('์ตœ๋Œ€ ๊ธธ์ด :',max_len) ์ตœ๋Œ€ ๊ธธ์ด : 7 ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋ฌธ์žฅ์˜ ๊ธธ์ด๋Š” 7์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฌธ์žฅ์˜ ๊ธธ์ด๋ฅผ 7๋กœ ๋งž์ถฐ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฐ€์ƒ์˜ ๋‹จ์–ด 'PAD'๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 'PAD'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ์ด ๋‹จ์–ด๋Š” 0๋ฒˆ ๋‹จ์–ด๋ผ๊ณ  ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๊ธธ์ด๊ฐ€ 7๋ณด๋‹ค ์งง์€ ๋ฌธ์žฅ์—๋Š” ์ˆซ์ž 0์„ ์ฑ„์›Œ์„œ ๊ธธ์ด 7๋กœ ๋งž์ถฐ์ค๋‹ˆ๋‹ค. for sentence in encoded: while len(sentence) < max_len: sentence.append(0) padded_np = np.array(encoded) padded_np array([[ 1, 5, 0, 0, 0, 0, 0], [ 1, 8, 5, 0, 0, 0, 0], [ 1, 3, 5, 0, 0, 0, 0], [ 9, 2, 0, 0, 0, 0, 0], [ 2, 4, 3, 2, 0, 0, 0], [ 3, 2, 0, 0, 0, 0, 0], [ 1, 4, 6, 0, 0, 0, 0], [ 1, 4, 6, 0, 0, 0, 0], [ 1, 4, 2, 0, 0, 0, 0], [ 7, 7, 3, 2, 10, 1, 11], [ 1, 12, 3, 13, 0, 0, 0]]) ๊ธธ์ด๊ฐ€ 7๋ณด๋‹ค ์งง์€ ๋ฌธ์žฅ์—๋Š” ์ „๋ถ€ ์ˆซ์ž 0์ด ๋’ค๋กœ ๋ถ™์–ด์„œ ๋ชจ๋“  ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ ์ „๋ถ€ 7์ด ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ์ด๋“ค์„ ํ•˜๋‚˜์˜ ํ–‰๋ ฌ๋กœ ๋ณด๊ณ , ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, 0๋ฒˆ ๋‹จ์–ด๋Š” ์‚ฌ์‹ค ์•„๋ฌด๋Ÿฐ ์˜๋ฏธ๋„ ์—†๋Š” ๋‹จ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž์—ฐ์–ด ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ธฐ๊ณ„๋Š” 0๋ฒˆ ๋‹จ์–ด๋ฅผ ๋ฌด์‹œํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ์— ํŠน์ • ๊ฐ’์„ ์ฑ„์›Œ์„œ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape)๋ฅผ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ์„ ํŒจ๋”ฉ(padding)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž 0์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์ œ๋กœ ํŒจ๋”ฉ(zero padding)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ์ผ€๋ผ์Šค ์ „์ฒ˜๋ฆฌ ๋„๊ตฌ๋กœ ํŒจ๋”ฉ ํ•˜๊ธฐ ์ผ€๋ผ์Šค์—์„œ๋Š” ์œ„์™€ ๊ฐ™์€ ํŒจ๋”ฉ์„ ์œ„ํ•ด pad_sequences()๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. from tensorflow.keras.preprocessing.sequence import pad_sequences encoded ๊ฐ’์ด ์œ„์—์„œ ์ด๋ฏธ ํŒจ๋”ฉ ํ›„์˜ ๊ฒฐ๊ณผ๋กœ ์ €์žฅ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ํŒจ๋”ฉ ์ด์ „์˜ ๊ฐ’์œผ๋กœ ๋‹ค์‹œ ๋˜๋Œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. encoded = tokenizer.texts_to_sequences(preprocessed_sentences) print(encoded) [[1, 5], [1, 8, 5], [1, 3, 5], [9, 2], [2, 4, 3, 2], [3, 2], [1, 4, 6], [1, 4, 6], [1, 4, 2], [7, 7, 3, 2, 10, 1, 11], [1, 12, 3, 13]] ์ผ€๋ผ์Šค์˜ pad_sequences๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒจ๋”ฉ์„ ํ•ด๋ด…์‹œ๋‹ค. padded = pad_sequences(encoded) padded array([[ 0, 0, 0, 0, 0, 1, 5], [ 0, 0, 0, 0, 1, 8, 5], [ 0, 0, 0, 0, 1, 3, 5], [ 0, 0, 0, 0, 0, 9, 2], [ 0, 0, 0, 2, 4, 3, 2], [ 0, 0, 0, 0, 0, 3, 2], [ 0, 0, 0, 0, 1, 4, 6], [ 0, 0, 0, 0, 1, 4, 6], [ 0, 0, 0, 0, 1, 4, 2], [ 7, 7, 3, 2, 10, 1, 11], [ 0, 0, 0, 1, 12, 3, 13]], dtype=int32) Numpy๋กœ ํŒจ๋”ฉ์„ ์ง„ํ–‰ํ•˜์˜€์„ ๋•Œ์™€๋Š” ํŒจ๋”ฉ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ๋ฐ ๊ทธ ์ด์œ ๋Š” pad_sequences๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์„œ์˜ ๋’ค์— 0์„ ์ฑ„์šฐ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์•ž์— 0์œผ๋กœ ์ฑ„์šฐ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋’ค์— 0์„ ์ฑ„์šฐ๊ณ  ์‹ถ๋‹ค๋ฉด ์ธ์ž๋กœ padding='post'๋ฅผ ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. padded = pad_sequences(encoded, padding='post') padded array([[ 1, 5, 0, 0, 0, 0, 0], [ 1, 8, 5, 0, 0, 0, 0], [ 1, 3, 5, 0, 0, 0, 0], [ 9, 2, 0, 0, 0, 0, 0], [ 2, 4, 3, 2, 0, 0, 0], [ 3, 2, 0, 0, 0, 0, 0], [ 1, 4, 6, 0, 0, 0, 0], [ 1, 4, 6, 0, 0, 0, 0], [ 1, 4, 2, 0, 0, 0, 0], [ 7, 7, 3, 2, 10, 1, 11], [ 1, 12, 3, 13, 0, 0, 0]], dtype=int32) Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒจ๋”ฉ์„ ํ–ˆ์„ ๋•Œ์™€ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•œ์ง€ ๋‘ ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. (padded == padded_np).all() True True ๊ฐ’์ด ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•˜๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” ๊ฐ€์žฅ ๊ธด ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ ๋ฌธ์„œ์˜ ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํŒจ๋”ฉ์„ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์˜€์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” ๊ผญ ๊ฐ€์žฅ ๊ธด ๋ฌธ์„œ์˜ ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ฐ€๋ น, ๋ชจ๋“  ๋ฌธ์„œ์˜ ํ‰๊ท  ๊ธธ์ด๊ฐ€ 20์ธ๋ฐ ๋ฌธ์„œ 1๊ฐœ์˜ ๊ธธ์ด๊ฐ€ 5,000์ด๋ผ๊ณ  ํ•ด์„œ ๊ตณ์ด ๋ชจ๋“  ๋ฌธ์„œ์˜ ๊ธธ์ด๋ฅผ 5,000์œผ๋กœ ํŒจ๋”ฉ ํ•  ํ•„์š”๋Š” ์—†์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๊ธธ์ด์— ์ œํ•œ์„ ๋‘๊ณ  ํŒจ๋”ฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. maxlen์˜ ์ธ์ž๋กœ ์ •์ˆ˜๋ฅผ ์ฃผ๋ฉด, ํ•ด๋‹น ์ •์ˆ˜๋กœ ๋ชจ๋“  ๋ฌธ์„œ์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. padded = pad_sequences(encoded, padding='post', maxlen=5) padded array([[ 1, 5, 0, 0, 0], [ 1, 8, 5, 0, 0], [ 1, 3, 5, 0, 0], [ 9, 2, 0, 0, 0], [ 2, 4, 3, 2, 0], [ 3, 2, 0, 0, 0], [ 1, 4, 6, 0, 0], [ 1, 4, 6, 0, 0], [ 1, 4, 2, 0, 0], [ 3, 2, 10, 1, 11], [ 1, 12, 3, 13, 0]], dtype=int32) ๊ธธ์ด๊ฐ€ 5๋ณด๋‹ค ์งง์€ ๋ฌธ์„œ๋“ค์€ 0์œผ๋กœ ํŒจ๋”ฉ ๋˜๊ณ , ๊ธฐ์กด์— 5๋ณด๋‹ค ๊ธธ์—ˆ๋‹ค๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ์†์‹ค๋ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ๋’ค์—์„œ ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์€ ๋ณธ๋ž˜ [ 7, 7, 3, 2, 10, 1, 11]์˜€์œผ๋‚˜ ํ˜„์žฌ๋Š” [ 3, 2, 10, 1, 11]๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋ฐ์ดํ„ฐ๊ฐ€ ์†์‹ค๋  ๊ฒฝ์šฐ์— ์•ž์˜ ๋‹จ์–ด๊ฐ€ ์•„๋‹ˆ๋ผ ๋’ค์˜ ๋‹จ์–ด๊ฐ€ ์‚ญ์ œ๋˜๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด truncating์ด๋ผ๋Š” ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. truncating='post'๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋’ค์˜ ๋‹จ์–ด๊ฐ€ ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค. padded = pad_sequences(encoded, padding='post', truncating='post', maxlen=5) padded array([[ 1, 5, 0, 0, 0], [ 1, 8, 5, 0, 0], [ 1, 3, 5, 0, 0], [ 9, 2, 0, 0, 0], [ 2, 4, 3, 2, 0], [ 3, 2, 0, 0, 0], [ 1, 4, 6, 0, 0], [ 1, 4, 6, 0, 0], [ 1, 4, 2, 0, 0], [ 7, 7, 3, 2, 10], [ 1, 12, 3, 13, 0]], dtype=int32) ์ˆซ์ž 0์œผ๋กœ ํŒจ๋”ฉ ํ•˜๋Š” ๊ฒƒ์€ ๋„๋ฆฌ ํผ์ง„ ๊ด€๋ก€์ด๊ธด ํ•˜์ง€๋งŒ, ๋ฐ˜๋“œ์‹œ ์ง€์ผœ์•ผ ํ•˜๋Š” ๊ทœ์น™์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ˆซ์ž 0์ด ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์ˆซ์ž๋ฅผ ํŒจ๋”ฉ์„ ์œ„ํ•œ ์ˆซ์ž๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ด ๋˜ํ•œ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ์‚ฌ์šฉ๋œ ์ •์ˆ˜๋“ค๊ณผ ๊ฒน์น˜์ง€ ์•Š๋„๋ก, ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์— +1์„ ํ•œ ์ˆซ์ž๋กœ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. last_value = len(tokenizer.word_index) + 1 # ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ณด๋‹ค 1 ํฐ ์ˆซ์ž๋ฅผ ์‚ฌ์šฉ print(last_value) 14 ํ˜„์žฌ ๋‹จ์–ด๊ฐ€ ์ด 13๊ฐœ์ด๊ณ , 1๋ฒˆ๋ถ€ํ„ฐ 13๋ฒˆ๊นŒ์ง€ ์ •์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์œผ๋ฏ€๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์— +1์„ ํ•˜๋ฉด ๋งˆ์ง€๋ง‰ ์ˆซ์ž์ธ 13๋ณด๋‹ค 1์ด ํฐ 14๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. pad_sequences์˜ ์ธ์ž๋กœ value๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 0์ด ์•„๋‹Œ ๋‹ค๋ฅธ ์ˆซ์ž๋กœ ํŒจ๋”ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. padded = pad_sequences(encoded, padding='post', value=last_value) padded array([[ 1, 5, 14, 14, 14, 14, 14], [ 1, 8, 5, 14, 14, 14, 14], [ 1, 3, 5, 14, 14, 14, 14], [ 9, 2, 14, 14, 14, 14, 14], [ 2, 4, 3, 2, 14, 14, 14], [ 3, 2, 14, 14, 14, 14, 14], [ 1, 4, 6, 14, 14, 14, 14], [ 1, 4, 6, 14, 14, 14, 14], [ 1, 4, 2, 14, 14, 14, 14], [ 7, 7, 3, 2, 10, 1, 11], [ 1, 12, 3, 13, 14, 14, 14]], dtype=int32) 02-08 ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-Hot Encoding) ์ปดํ“จํ„ฐ ๋˜๋Š” ๊ธฐ๊ณ„๋Š” ๋ฌธ์ž๋ณด๋‹ค๋Š” ์ˆซ์ž๋ฅผ ๋” ์ž˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ๋Š” ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ฐ”๊พธ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ๋ฒ•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-Hot Encoding)์€ ๊ทธ ๋งŽ์€ ๊ธฐ๋ฒ• ์ค‘์—์„œ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด๋ฉฐ, ๋จธ์‹  ๋Ÿฌ๋‹, ๋”ฅ ๋Ÿฌ๋‹์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ๋ฐฐ์›Œ์•ผ ํ•˜๋Š” ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šฐ๊ธฐ์— ์•ž์„œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์— ๋Œ€ํ•ด์„œ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์€ ์•ž์œผ๋กœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๊ณ„์† ๋‚˜์˜ค๋Š” ๊ฐœ๋…์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ ์ดํ•ดํ•˜๊ณ  ๊ฐ€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์€ ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์˜ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ˜ผ๋™์ด ์—†๋„๋ก ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๋ผ๋Š” ์ •์˜์— ๋Œ€ํ•ด์„œ ์ข€ ๋” ์ฃผ๋ชฉํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ book๊ณผ books์™€ ๊ฐ™์ด ๋‹จ์–ด์˜ ๋ณ€ํ˜• ํ˜•ํƒœ๋„ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์•ž์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ๊ฐ€์ง€๊ณ , ๋ฌธ์ž๋ฅผ ์ˆซ์ž. ๋” ๊ตฌ์ฒด์ ์œผ๋กœ๋Š” ๋ฒกํ„ฐ๋กœ ๋ฐ”๊พธ๋Š” ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํฌํ•จํ•œ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์œ„ํ•ด์„œ ๋จผ์ € ํ•ด์•ผ ํ•  ์ผ์€ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“œ๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ชจ์•„๋†“์œผ๋ฉด ์ด๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‹จ์–ด ์ง‘ํ•ฉ์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ์— ๋‹จ์–ด๊ฐ€ ์ด 5,000๊ฐœ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 5,000์ž…๋‹ˆ๋‹ค. 5,000๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ๋Š” ์ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ๋‹จ์–ด๋“ค๋งˆ๋‹ค 1๋ฒˆ๋ถ€ํ„ฐ 5,000๋ฒˆ๊นŒ์ง€ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, book์€ 150๋ฒˆ, dog๋Š” 171๋ฒˆ, love๋Š” 192๋ฒˆ, books๋Š” 212๋ฒˆ๊ณผ ๊ฐ™์ด ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜์˜€๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ์ˆซ์ž๋กœ ๋ฐ”๋€ ๋‹จ์–ด๋“ค์„ ๋ฒกํ„ฐ๋กœ ๋‹ค๋ฃจ๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ๋ ๊นŒ์š”? 1. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-Hot Encoding)์ด๋ž€? ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์€ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๋ฒกํ„ฐ์˜ ์ฐจ์›์œผ๋กœ ํ•˜๊ณ , ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์€ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์— 1์˜ ๊ฐ’์„ ๋ถ€์—ฌํ•˜๊ณ , ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์—๋Š” 0์„ ๋ถ€์—ฌํ•˜๋Š” ๋‹จ์–ด์˜ ๋ฒกํ„ฐ ํ‘œํ˜„ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋œ ๋ฒกํ„ฐ๋ฅผ ์›-ํ•ซ ๋ฒกํ„ฐ(One-Hot vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ๋‘ ๊ฐ€์ง€ ๊ณผ์ •์œผ๋กœ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ์งธ, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ๋‘˜์งธ, ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์€ ๋‹จ์–ด์˜ ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ์ธ๋ฑ์Šค๋กœ ๊ฐ„์ฃผํ•˜๊ณ  ํ•ด๋‹น ์œ„์น˜์— 1์„ ๋ถ€์—ฌํ•˜๊ณ , ๋‹ค๋ฅธ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์˜ ์œ„์น˜์—๋Š” 0์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์„ ์˜ˆ์ œ๋กœ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์žฅ : ๋‚˜๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ๋ฐฐ์šด๋‹ค Okt ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ํ†ตํ•ด์„œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. from konlpy.tag import Okt okt = Okt() tokens = okt.morphs("๋‚˜๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ๋ฐฐ์šด๋‹ค") print(tokens) ['๋‚˜', '๋Š”', '์ž์—ฐ์–ด', '์ฒ˜๋ฆฌ', '๋ฅผ', '๋ฐฐ์šด๋‹ค'] ๊ฐ ํ† ํฐ์— ๋Œ€ํ•ด์„œ ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ๋ฌธ์žฅ์ด ์งง๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š์ง€๋งŒ, ๋นˆ๋„์ˆ˜ ์ˆœ์œผ๋กœ ๋‹จ์–ด๋ฅผ ์ •๋ ฌํ•˜์—ฌ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. word_to_index = {word : index for index, word in enumerate(tokens)} print('๋‹จ์–ด ์ง‘ํ•ฉ :',word_to_index) ๋‹จ์–ด ์ง‘ํ•ฉ : {'๋‚˜': 0, '๋Š”': 1, '์ž์—ฐ์–ด': 2, '์ฒ˜๋ฆฌ': 3, '๋ฅผ': 4, '๋ฐฐ์šด๋‹ค': 5} ํ† ํฐ์„ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ํ† ํฐ์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. def one_hot_encoding(word, word_to_index): one_hot_vector = [0]*(len(word_to_index)) index = word_to_index[word] one_hot_vector[index] = 1 return one_hot_vector '์ž์—ฐ์–ด'๋ผ๋Š” ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ์–ป์–ด๋ด…์‹œ๋‹ค. one_hot_encoding("์ž์—ฐ์–ด", word_to_index) [0, 0, 1, 0, 0, 0] '์ž์—ฐ์–ด'๋Š” ์ •์ˆ˜ 2์ด๋ฏ€๋กœ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ์ธ๋ฑ์Šค 2์˜ ๊ฐ’์ด 1์ด๋ฉฐ, ๋‚˜๋จธ์ง€ ๊ฐ’์€ 0์ธ ๋ฒกํ„ฐ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. 2. ์ผ€๋ผ์Šค(Keras)๋ฅผ ์ด์šฉํ•œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-Hot Encoding) ์œ„์—์„œ๋Š” ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ํŒŒ์ด์ฌ์œผ๋กœ ์ง์ ‘ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€์ง€๋งŒ, ์ผ€๋ผ์Šค๋Š” ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์œ ์šฉํ•œ ๋„๊ตฌ to_categorical()๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ผ€๋ผ์Šค๋งŒ์œผ๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. text = "๋‚˜๋ž‘ ์ ์‹ฌ ๋จน์œผ๋Ÿฌ ๊ฐˆ๋ž˜ ์ ์‹ฌ ๋ฉ”๋‰ด๋Š” ํ–„๋ฒ„๊ฑฐ ๊ฐˆ๋ž˜ ๊ฐˆ๋ž˜ ํ–„๋ฒ„๊ฑฐ ์ตœ๊ณ ์•ผ" ์œ„์™€ ๊ฐ™์€ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ด์šฉํ•œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.utils import to_categorical text = "๋‚˜๋ž‘ ์ ์‹ฌ ๋จน์œผ๋Ÿฌ ๊ฐˆ๋ž˜ ์ ์‹ฌ ๋ฉ”๋‰ด๋Š” ํ–„๋ฒ„๊ฑฐ ๊ฐˆ๋ž˜ ๊ฐˆ๋ž˜ ํ–„๋ฒ„๊ฑฐ ์ตœ๊ณ ์•ผ" tokenizer = Tokenizer() tokenizer.fit_on_texts([text]) print('๋‹จ์–ด ์ง‘ํ•ฉ :',tokenizer.word_index) ๋‹จ์–ด ์ง‘ํ•ฉ : {'๊ฐˆ๋ž˜': 1, '์ ์‹ฌ': 2, 'ํ–„๋ฒ„๊ฑฐ': 3, '๋‚˜๋ž‘': 4, '๋จน์œผ๋Ÿฌ': 5, '๋ฉ”๋‰ด๋Š”': 6, '์ตœ๊ณ ์•ผ': 7} ์œ„์™€ ๊ฐ™์ด ์ƒ์„ฑ๋œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์— ์žˆ๋Š” ๋‹จ์–ด๋“ค๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ํ…์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๋ฉด, texts_to_sequences()๋ฅผ ํ†ตํ•ด์„œ ์ด๋ฅผ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ๋œ ๋‹จ์–ด ์ง‘ํ•ฉ ๋‚ด์˜ ์ผ๋ถ€ ๋‹จ์–ด๋“ค๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ์„œ๋ธŒ ํ…์ŠคํŠธ์ธ sub_text๋ฅผ ๋งŒ๋“ค์–ด ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. sub_text = "์ ์‹ฌ ๋จน์œผ๋Ÿฌ ๊ฐˆ๋ž˜ ๋ฉ”๋‰ด๋Š” ํ–„๋ฒ„๊ฑฐ ์ตœ๊ณ ์•ผ" encoded = tokenizer.texts_to_sequences([sub_text])[0] print(encoded) [2, 5, 1, 6, 3, 7] ์ง€๊ธˆ๊นŒ์ง€ ์ง„ํ–‰ํ•œ ๊ฒƒ์€ ์ด๋ฏธ ์ด์ „์— ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์‹ค์Šต์„ ํ•˜๋ฉฐ ๋ฐฐ์šด ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ด์ œ ํ•ด๋‹น ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ , ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๋กœ๋ถ€ํ„ฐ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜๋Š” to_categorical()๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. one_hot = to_categorical(encoded) print(one_hot) [[0. 0. 1. 0. 0. 0. 0. 0.] # ์ธ๋ฑ์Šค 2์˜ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 0. 0. 0. 0. 1. 0. 0.] # ์ธ๋ฑ์Šค 5์˜ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 1. 0. 0. 0. 0. 0. 0.] # ์ธ๋ฑ์Šค 1์˜ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 0. 0. 0. 0. 0. 1. 0.] # ์ธ๋ฑ์Šค 6์˜ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 0. 0. 1. 0. 0. 0. 0.] # ์ธ๋ฑ์Šค 3์˜ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 0. 0. 0. 0. 0. 0. 1.]] # ์ธ๋ฑ์Šค 7์˜ ์›-ํ•ซ ๋ฒกํ„ฐ ์œ„์˜ ๊ฒฐ๊ณผ๋Š” "์ ์‹ฌ ๋จน์œผ๋Ÿฌ ๊ฐˆ๋ž˜ ๋ฉ”๋‰ด๋Š” ํ–„๋ฒ„๊ฑฐ ์ตœ๊ณ ์•ผ"๋ผ๋Š” ๋ฌธ์žฅ์ด [2, 5, 1, 6, 3, 7]๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋˜๊ณ  ๋‚˜์„œ, ๊ฐ๊ฐ์˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์ธ๋ฑ์Šค๋กœ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 3. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-Hot Encoding)์˜ ํ•œ๊ณ„ ์ด๋Ÿฌํ•œ ํ‘œํ˜„ ๋ฐฉ์‹์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚ ์ˆ˜๋ก, ๋ฒกํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๊ณต๊ฐ„์ด ๊ณ„์† ๋Š˜์–ด๋‚œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ‘œํ˜„์œผ๋กœ๋Š” ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋Š˜์–ด๋‚œ๋‹ค๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์› ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ๊ณง ๋ฒกํ„ฐ์˜ ์ฐจ์› ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ๋‹จ์–ด๊ฐ€ 1,000๊ฐœ์ธ ์ฝ”ํผ์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์› ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค๋ฉด, ๋ชจ๋“  ๋‹จ์–ด ๊ฐ๊ฐ์€ ๋ชจ๋‘ 1,000๊ฐœ์˜ ์ฐจ์›์„ ๊ฐ€์ง„ ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ชจ๋“  ๋‹จ์–ด ๊ฐ๊ฐ์€ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ 1์„ ๊ฐ€์ง€๊ณ , 999๊ฐœ์˜ ๊ฐ’์€ 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๊ฐ€ ๋˜๋Š”๋ฐ ์ด๋Š” ์ €์žฅ ๊ณต๊ฐ„ ์ธก๋ฉด์—์„œ๋Š” ๋งค์šฐ ๋น„ํšจ์œจ์ ์ธ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ํ‘œํ˜„ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋Š‘๋Œ€, ํ˜ธ๋ž‘์ด, ๊ฐ•์•„์ง€, ๊ณ ์–‘์ด๋ผ๋Š” 4๊ฐœ์˜ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•ด์„œ ๊ฐ๊ฐ, [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]์ด๋ผ๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋ถ€์—ฌ๋ฐ›์•˜๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋•Œ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ๋Š” ๊ฐ•์•„์ง€์™€ ๋Š‘๋Œ€๊ฐ€ ์œ ์‚ฌํ•˜๊ณ , ํ˜ธ๋ž‘์ด์™€ ๊ณ ์–‘์ด๊ฐ€ ์œ ์‚ฌํ•˜๋‹ค๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ข€ ๋” ๊ทน๋‹จ์ ์œผ๋กœ๋Š” ๊ฐ•์•„์ง€, ๊ฐœ, ๋ƒ‰์žฅ๊ณ ๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์„ ๋•Œ ๊ฐ•์•„์ง€๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ๊ฐœ์™€ ๋ƒ‰์žฅ๊ณ ๋ผ๋Š” ๋‹จ์–ด ์ค‘ ์–ด๋–ค ๋‹จ์–ด์™€ ๋” ์œ ์‚ฌํ•œ์ง€๋„ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ์„ฑ์„ ์•Œ ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์€ ๊ฒ€์ƒ‰ ์‹œ์Šคํ…œ ๋“ฑ์—์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ๋  ์†Œ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์—ฌํ–‰์„ ๊ฐ€๋ ค๊ณ  ์›น ๊ฒ€์ƒ‰์ฐฝ์— '์‚ฟํฌ๋กœ ์ˆ™์†Œ'๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ œ๋Œ€๋กœ ๋œ ๊ฒ€์ƒ‰ ์‹œ์Šคํ…œ์ด๋ผ๋ฉด, '์‚ฟํฌ๋กœ ์ˆ™์†Œ'๋ผ๋Š” ๊ฒ€์ƒ‰์–ด์— ๋Œ€ํ•ด์„œ '์‚ฟํฌ๋กœ ๊ฒŒ์ŠคํŠธ ํ•˜์šฐ์Šค', '์‚ฟํฌ๋กœ ๋ฃŒ์นธ', '์‚ฟํฌ๋กœ ํ˜ธํ…”'๊ณผ ๊ฐ™์€ ์œ ์‚ฌ ๋‹จ์–ด์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋„ ํ•จ๊ป˜ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ์„ฑ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†๋‹ค๋ฉด, '๊ฒŒ์ŠคํŠธ ํ•˜์šฐ์Šค'์™€ '๋ฃŒ์นธ'๊ณผ 'ํ˜ธํ…”'์ด๋ผ๋Š” ์—ฐ๊ด€ ๊ฒ€์ƒ‰์–ด๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‹จ์–ด์˜ ์ž ์žฌ ์˜๋ฏธ๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ ๋‹ค์ฐจ์› ๊ณต๊ฐ„์— ๋ฒกํ„ฐํ™”ํ•˜๋Š” ๊ธฐ๋ฒ•์œผ๋กœ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ์งธ๋Š” ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ ๋ฒกํ„ฐํ™” ๋ฐฉ๋ฒ•์ธ LSA(์ž ์žฌ ์˜๋ฏธ ๋ถ„์„), HAL ๋“ฑ์ด ์žˆ์œผ๋ฉฐ, ๋‘˜์งธ๋Š” ์˜ˆ์ธก ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฒกํ„ฐํ™”ํ•˜๋Š” NNLM, RNNLM, Word2Vec, FastText ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์นด์šดํŠธ ๊ธฐ๋ฐ˜๊ณผ ์˜ˆ์ธก ๊ธฐ๋ฐ˜ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ GloVe๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์–ธ๊ธ‰ํ•œ ๋ฐฉ๋ฒ•๋“ค ์ค‘ ๋Œ€๋ถ€๋ถ„์€ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 02-09 ๋ฐ์ดํ„ฐ์˜ ๋ถ„๋ฆฌ(Splitting Data) ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ณ  ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ๋ถ„๋ฆฌํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ์ง€๋„ ํ•™์Šต(Supervised Learning)์„ ๋‹ค๋ฃจ๋Š”๋ฐ, ์ด๋ฒˆ์—๋Š” ์ง€๋„ ํ•™์Šต์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌ ์ž‘์—…์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. import pandas as pd import numpy as np from sklearn.model_selection import train_test_split 1. ์ง€๋„ ํ•™์Šต(Supervised Learning) ์ง€๋„ ํ•™์Šต์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ๋ฌธ์ œ์ง€๋ฅผ ์—ฐ์ƒ์ผ€ ํ•ฉ๋‹ˆ๋‹ค. ์ง€๋„ ํ•™์Šต์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์ •๋‹ต์ด ๋ฌด์—‡์ธ์ง€ ๋งž์ถฐ ํ•˜๋Š” '๋ฌธ์ œ'์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ”์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” '์ •๋‹ต'์ด ์ ํ˜€์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ๋น„์œ ํ•˜๋ฉด, ๊ธฐ๊ณ„๋Š” ์ •๋‹ต์ด ์ ํ˜€์ ธ ์žˆ๋Š” ๋ฌธ์ œ์ง€๋ฅผ ๋ฌธ์ œ์™€ ์ •๋‹ต์„ ํ•จ๊ป˜ ๋ณด๋ฉด์„œ ์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ•˜๊ณ , ํ–ฅํ›„์— ์ •๋‹ต์ด ์—†๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ๋„ ์ •๋‹ต์„ ์ž˜ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ๊ณผ ํ•ด๋‹น ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ธ์ง€, ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€ ์ ํ˜€์žˆ๋Š” ๋ ˆ์ด๋ธ”๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์€<NAME>์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์•ฝ 20,000๊ฐœ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ๋‘ ๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋˜๋Š”๋ฐ, ๋ฐ”๋กœ ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์— ํ•ด๋‹น๋˜๋Š” ์ฒซ ๋ฒˆ์งธ ์—ด๊ณผ ํ•ด๋‹น ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๊ฐ€ ์ ํ˜€์žˆ๋Š” ์ •๋‹ต์— ํ•ด๋‹น๋˜๋Š” ๋‘ ๋ฒˆ์งธ ์—ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์ด ์ด 20,000๊ฐœ์˜ ํ–‰์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ํ…์ŠคํŠธ(๋ฉ”์ผ์˜ ๋‚ด์šฉ) ๋ ˆ์ด๋ธ”(์ŠคํŒธ ์—ฌ๋ถ€) ๋‹น์‹ ์—๊ฒŒ ๋“œ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰ ํ˜œํƒ! ... ์ŠคํŒธ ๋ฉ”์ผ ๋‚ด์ผ ๋ต ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธ ๋ถ€ํƒ... ์ •์ƒ ๋ฉ”์ผ ... ... (๊ด‘๊ณ ) ๋ฉ‹์žˆ์–ด์งˆ ์ˆ˜ ์žˆ๋Š”... ์ŠคํŒธ ๋ฉ”์ผ ๊ธฐ๊ณ„๋ฅผ ์ง€๋„ํ•˜๋Š” ์„ ์ƒ๋‹˜์˜ ์ž…์žฅ์ด ๋˜์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋ฅผ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ด 4๊ฐœ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ์šฐ์„  ๋ฉ”์ผ์˜ ๋‚ด์šฉ์ด ๋‹ด๊ธด ์ฒซ ๋ฒˆ์งธ ์—ด์„ X์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฉ”์ผ์ด ์ŠคํŒธ์ธ์ง€ ์ •์ƒ์ธ์ง€ ์ •๋‹ต์ด ์ ํ˜€์žˆ๋Š” ๋‘ ๋ฒˆ์งธ ์—ด์„ y์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ฌธ์ œ์ง€์— ํ•ด๋‹น๋˜๋Š” 20,000๊ฐœ์˜ X์™€ ์ •๋‹ต์ง€์— ํ•ด๋‹น๋˜๋Š” 20,000๊ฐœ์˜ y๊ฐ€ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ œ ์ด X์™€ y์— ๋Œ€ํ•ด์„œ ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋˜๋‹ค์‹œ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ฌธ์ œ์ง€๋ฅผ ๋‹ค ๊ณต๋ถ€ํ•˜๊ณ  ๋‚˜์„œ ์‹ค๋ ฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‹œํ—˜(test) ์šฉ์œผ๋กœ ์ผ๋ถ€๋กœ ์ผ๋ถ€ ๋ฌธ์ œ์™€ ํ•ด๋‹น ๋ฌธ์ œ์˜ ์ •๋‹ต์ง€๋ฅผ ๋ถ„๋ฆฌํ•ด๋†“๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 2,000๊ฐœ๋ฅผ ๋ถ„๋ฆฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋ถ„๋ฆฌ ์‹œ์—๋Š” ์—ฌ์ „ํžˆ X์™€ y์˜ ๋งคํ•‘ ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค X(๋ฌธ์ œ)์— ๋Œ€ํ•œ ์–ด๋–ค y(์ •๋‹ต)์ธ์ง€ ๋ฐ”๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํ•™์Šต์šฉ์— ํ•ด๋‹น๋˜๋Š” 18,000๊ฐœ์˜ X, y์˜ ์Œ๊ณผ ์‹œํ—˜์šฉ์— ํ•ด๋‹น๋˜๋Š” 2000๊ฐœ์˜ X, y์˜ ์Œ์ด ์ƒ๊น๋‹ˆ๋‹ค ์ด ์ฑ…์—์„œ๋Š” ์ด ์œ ํ˜•์˜ ๋ฐ์ดํ„ฐ๋“ค์—๊ฒŒ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ณ€์ˆ˜๋ช…์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. <ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ> X_train : ๋ฌธ์ œ์ง€ ๋ฐ์ดํ„ฐ y_train : ๋ฌธ์ œ์ง€์— ๋Œ€ํ•œ ์ •๋‹ต ๋ฐ์ดํ„ฐ. <ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ> X_test : ์‹œํ—˜์ง€ ๋ฐ์ดํ„ฐ. y_test : ์‹œํ—˜์ง€์— ๋Œ€ํ•œ ์ •๋‹ต ๋ฐ์ดํ„ฐ. ๊ธฐ๊ณ„๋Š” ์ด์ œ๋ถ€ํ„ฐ X_train๊ณผ y_train์— ๋Œ€ํ•ด์„œ ํ•™์Šต์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ํ•™์Šต ์ƒํƒœ์—์„œ๋Š” ์ •๋‹ต์ง€์ธ y_train์„ ๋ณผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 18,000๊ฐœ์˜ ๋ฌธ์ œ์ง€ X_train๊ณผ y_train์„ ํ•จ๊ป˜ ๋ณด๋ฉด์„œ ์–ด๋–ค ๋ฉ”์ผ ๋‚ด์šฉ์ผ ๋•Œ ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๋ฅผ ์—ด์‹ฌํžˆ ๊ทœ์น™์„ ๋„์ถœํ•ด๋‚˜๊ฐ€๋ฉด์„œ ์ •๋ฆฌํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต์„ ๋‹ค ํ•œ ๊ธฐ๊ณ„์—๊ฒŒ y_test๋Š” ๋ณด์—ฌ์ฃผ์ง€ ์•Š๊ณ , X_test์— ๋Œ€ํ•ด์„œ ์ •๋‹ต์„ ์˜ˆ์ธกํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ธฐ๊ณ„๊ฐ€ ์˜ˆ์ธกํ•œ ๋‹ต๊ณผ ์‹ค์ œ ์ •๋‹ต์ธ y_test๋ฅผ ๋น„๊ตํ•˜๋ฉด์„œ ๊ธฐ๊ณ„๊ฐ€ ์ •๋‹ต์„ ์–ผ๋งˆ๋‚˜ ๋งž์ท„๋Š”์ง€๋ฅผ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ˆ˜์น˜๊ฐ€ ๊ธฐ๊ณ„์˜ ์ •ํ™•๋„(Accuracy)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 2. X์™€ y ๋ถ„๋ฆฌํ•˜๊ธฐ 1) zip ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜๊ธฐ zip() ํ•จ์ˆ˜๋Š” ๋™์ผํ•œ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์‹œํ€€์Šค ์ž๋ฃŒํ˜•์—์„œ ๊ฐ ์ˆœ์„œ์— ๋“ฑ์žฅํ•˜๋Š” ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ ๊ตฌ์„ฑ์—์„œ zip ํ•จ์ˆ˜๋Š” X์™€ y๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š”๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  zip ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. X, y = zip(['a', 1], ['b', 2], ['c', 3]) print('X ๋ฐ์ดํ„ฐ :',X) print('y ๋ฐ์ดํ„ฐ :',y) X ๋ฐ์ดํ„ฐ : ('a', 'b', 'c') y ๋ฐ์ดํ„ฐ : (1, 2, 3) ๊ฐ ๋ฐ์ดํ„ฐ์—์„œ ์ฒซ ๋ฒˆ์งธ๋กœ ๋“ฑ์žฅํ•œ ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์ด๊ณ , ๋‘ ๋ฒˆ์งธ๋กœ ๋“ฑ์žฅํ•œ ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์ธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ ๋˜๋Š” ํ–‰๋ ฌ ๋˜๋Š” ๋’ค์—์„œ ๋ฐฐ์šธ ๊ฐœ๋…์ธ 2D ํ…์„œ. sequences = [['a', 1], ['b', 2], ['c', 3]] X, y = zip(*sequences) print('X ๋ฐ์ดํ„ฐ :',X) print('y ๋ฐ์ดํ„ฐ :',y) X ๋ฐ์ดํ„ฐ : ('a', 'b', 'c') y ๋ฐ์ดํ„ฐ : (1, 2, 3) ๊ฐ ๋ฐ์ดํ„ฐ์—์„œ ์ฒซ ๋ฒˆ์งธ๋กœ ๋“ฑ์žฅํ•œ ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์ด๊ณ , ๋‘ ๋ฒˆ์งธ๋กœ ๋“ฑ์žฅํ•œ ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์ธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐ๊ฐ X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ด์šฉํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜๊ธฐ values = [['๋‹น์‹ ์—๊ฒŒ ๋“œ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰ ํ˜œํƒ!', 1], ['๋‚ด์ผ ๋ต ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธ ๋ถ€ํƒ๋“œ...', 0], ['๋„์—ฐ ์”จ. ์ž˜ ์ง€๋‚ด์‹œ์ฃ ? ์˜ค๋žœ ๋งŒ์ž…...', 0], ['(๊ด‘๊ณ ) AI๋กœ ์ฃผ๊ฐ€๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋‹ค!', 1]] columns = ['๋ฉ”์ผ ๋ณธ๋ฌธ', '์ŠคํŒธ ๋ฉ”์ผ ์œ ๋ฌด'] df = pd.DataFrame(values, columns=columns) df ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์€ ์—ด์˜ ์ด๋ฆ„์œผ๋กœ ๊ฐ ์—ด์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด ์†์‰ฝ๊ฒŒ X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. X = df['๋ฉ”์ผ ๋ณธ๋ฌธ'] y = df['์ŠคํŒธ ๋ฉ”์ผ ์œ ๋ฌด'] X์™€ y ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('X ๋ฐ์ดํ„ฐ :',X.to_list()) print('y ๋ฐ์ดํ„ฐ :',y.to_list()) X ๋ฐ์ดํ„ฐ : ['๋‹น์‹ ์—๊ฒŒ ๋“œ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰ ํ˜œํƒ!', '๋‚ด์ผ ๋ต ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธ ๋ถ€ํƒ๋“œ...', '๋„์—ฐ ์”จ. ์ž˜ ์ง€๋‚ด์‹œ์ฃ ? ์˜ค๋žœ ๋งŒ์ž…...', '(๊ด‘๊ณ ) AI๋กœ ์ฃผ๊ฐ€๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋‹ค!'] y ๋ฐ์ดํ„ฐ : [1, 0, 0, 1] 3) Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ž„์˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์„œ Numpy์˜ ์Šฌ๋ผ์ด์‹ฑ(slicing)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. np_array = np.arange(0,16).reshape((4,4)) print('์ „์ฒด ๋ฐ์ดํ„ฐ :') print(np_array) ์ „์ฒด ๋ฐ์ดํ„ฐ : [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] ๋งˆ์ง€๋ง‰ ์—ด์„ ์ œ์™ธํ•˜๊ณ  X ๋ฐ์ดํ„ฐ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์—ด๋งŒ์„ y ๋ฐ์ดํ„ฐ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. X = np_array[:, :3] y = np_array[:,3] print('X ๋ฐ์ดํ„ฐ :') print(X) print('y ๋ฐ์ดํ„ฐ :',y) X ๋ฐ์ดํ„ฐ : [[ 0 1 2] [ 4 5 6] [ 8 9 10] [12 13 14]] y ๋ฐ์ดํ„ฐ : [ 3 7 11 15] 3. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์ด๋ฏธ X์™€ y๊ฐ€ ๋ถ„๋ฆฌ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์‚ฌ์ดํ‚ท ๋Ÿฐ์„ ์ด์šฉํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜๊ธฐ ์‚ฌ์ดํ‚ท๋Ÿฐ์€ ํ•™์Šต์šฉ ํ…Œ์ŠคํŠธ์™€ ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” train_test_split()๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.2, random_state=1234) ๊ฐ ์ธ์ž๋Š” ๋‹ค์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. train_size์™€ test_size๋Š” ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ๊ธฐ์žฌํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค. X : ๋…๋ฆฝ ๋ณ€์ˆ˜ ๋ฐ์ดํ„ฐ. (๋ฐฐ์—ด์ด๋‚˜ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„) y : ์ข…์† ๋ณ€์ˆ˜ ๋ฐ์ดํ„ฐ. ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ. test_size : ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค. 1๋ณด๋‹ค ์ž‘์€ ์‹ค์ˆ˜๋ฅผ ๊ธฐ์žฌํ•  ๊ฒฝ์šฐ, ๋น„์œจ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. train_size : ํ•™์Šต์šฉ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค. 1๋ณด๋‹ค ์ž‘์€ ์‹ค์ˆ˜๋ฅผ ๊ธฐ์žฌํ•  ๊ฒฝ์šฐ, ๋น„์œจ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. random_state : ๋‚œ์ˆ˜ ์‹œ๋“œ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž„์˜๋กœ X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. # ์ž„์˜๋กœ X์™€ y ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ X, y = np.arange(10).reshape((5, 2)), range(5) print('X ์ „์ฒด ๋ฐ์ดํ„ฐ :') print(X) print('y ์ „์ฒด ๋ฐ์ดํ„ฐ :') print(list(y)) X ์ „์ฒด ๋ฐ์ดํ„ฐ : [[0 1] [2 3] [4 5] [6 7] [8 9]] y ์ „์ฒด ๋ฐ์ดํ„ฐ : [0, 1, 2, 3, 4] ์—ฌ๊ธฐ์„œ๋Š” 7:3์˜ ๋น„์œจ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. train_test_split()์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๋ฅผ ์„ž๊ณ  ๋‚˜์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, random_state์˜ ๊ฐ’์„ ํŠน์ • ์ˆซ์ž๋กœ ๊ธฐ์žฌํ•ด ์ค€ ๋’ค์— ๋‹ค์Œ์—๋„ ๋™์ผํ•œ ์ˆซ์ž๋กœ ๊ธฐ์žฌํ•ด ์ฃผ๋ฉด ํ•ญ์ƒ ๋™์ผํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ์„ž์ธ ์ฑ„ ๋ถ„๋ฆฌ๋˜๋ฏ€๋กœ ์ด์ „๊ณผ ๋‹ค๋ฅธ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์‹ค์Šต์„ ํ†ตํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. random_state ๊ฐ’์„ ์ž„์˜๋กœ 1234๋กœ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. # 7:3์˜ ๋น„์œจ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1234) 70%์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌ๋œ X์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ 30%์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌ๋œ X์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. print('X ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ :') print(X_train) print('X ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(X_test) X ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ : [[2 3] [4 5] [6 7]] X ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [[8 9] [0 1]] 70%์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌ๋œ y์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ 30%์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌ๋œ y์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. print('y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ :') print(y_train) print('y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(y_test) y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ : [1, 2, 3] y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [4, 0] ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋Š ์ค‘๊ฐ„ ๋ถ€๋ถ„์—์„œ ์•ž๊ณผ ๋’ค๋กœ ์ž๋ฅธ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์•ž์— ์žˆ๋˜ ์ƒ˜ํ”Œ์ด ๋’ค๋กœ ๊ฐ€๊ธฐ๋„ ํ•˜๊ณ , ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๊ฐ€ ์ „๋ฐ˜์ ์œผ๋กœ ์„ž์ด๋ฉด์„œ ๋ถ„๋ฆฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. random_state์˜ ์˜๋ฏธ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด๋ฒˆ์—๋Š” random_state์˜ ๊ฐ’์„ ์ž„์˜๋กœ ๋‹ค๋ฅธ ๊ฐ’์ธ 1์„ ์ฃผ๊ณ  ๋‹ค์‹œ ๋ถ„๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  y ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # random_state์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) print('y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ :') print(y_train) print('y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(y_test) y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ : [4, 0, 3] y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [2, 1] random_state ๊ฐ’์ด 1234์ผ ๋•Œ์™€ ์ „ํ˜€ ๋‹ค๋ฅธ y ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ์„ž์˜€๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋‹ค์‹œ random_state์˜ ๊ฐ’์„ 1234๋กœ ์ฃผ๊ณ  ๋‹ค์‹œ y ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # random_state์„ ์ด์ „์˜ ๊ฐ’์ด์—ˆ๋˜ 1234๋กœ ๋ณ€๊ฒฝ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1234) print('y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ :') print(y_train) print('y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(y_test) y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ : [1, 2, 3] y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [4, 0] ์ด์ „๊ณผ ๋™์ผํ•œ y ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. random_state์˜ ๊ฐ’์„ ๊ณ ์ •ํ•ด๋‘๋ฉด ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ํ•ญ์ƒ ๋™์ผํ•œ ์ˆœ์„œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์„ž์œผ๋ฏ€๋กœ, ๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ์— ์žฌํ˜„ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ์ˆ˜๋™์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” ์ˆ˜๋™์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ์ž„์˜๋กœ X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์‹ค์Šต์„ ์œ„ํ•ด ์ž„์˜๋กœ X์™€ y๊ฐ€ ์ด๋ฏธ ๋ถ„๋ฆฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ X, y = np.arange(0,24).reshape((12,2)), range(12) print('X ์ „์ฒด ๋ฐ์ดํ„ฐ :') print(X) print('y ์ „์ฒด ๋ฐ์ดํ„ฐ :') print(list(y)) X ์ „์ฒด ๋ฐ์ดํ„ฐ : [[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9] [10 11] [12 13] [14 15] [16 17] [18 19] [20 21] [22 23]] y ์ „์ฒด ๋ฐ์ดํ„ฐ : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. num_of_train์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, num_of_test๋Š” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. num_of_train = int(len(X) * 0.8) # ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๊ธธ์ด์˜ 80%์— ํ•ด๋‹นํ•˜๋Š” ๊ธธ์ด๊ฐ’์„ ๊ตฌํ•œ๋‹ค. num_of_test = int(len(X) - num_of_train) # ์ „์ฒด ๊ธธ์ด์—์„œ 80%์— ํ•ด๋‹นํ•˜๋Š” ๊ธธ์ด๋ฅผ ๋บ€๋‹ค. print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',num_of_train) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',num_of_test) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : 9 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : 3 ์•„์ง ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆˆ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ด ๋‘ ๊ฐœ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ช‡ ๊ฐœ๋กœ ํ• ์ง€ ์ •ํ•˜๊ธฐ๋งŒ ํ•œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ num_of_test๋ฅผ len(X) * 0.2๋กœ ๊ณ„์‚ฐํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์— ๋ˆ„๋ฝ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 4,518์ด๋ผ๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ 4,518์˜ 80%์˜ ๊ฐ’์€ 3,614.4๋กœ ์†Œ์ˆ˜์ ์„ ๋‚ด๋ฆฌ๋ฉด 3,614๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ 4,518์˜ 20%์˜ ๊ฐ’์€ 903.6์œผ๋กœ ์†Œ์ˆ˜์ ์„ ๋‚ด๋ฆฌ๋ฉด 903์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  3,614 + 903 = 4517์ด๋ฏ€๋กœ ๋ฐ์ดํ„ฐ 1๊ฐœ๊ฐ€ ๋ˆ„๋ฝ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์–ด๋Š ํ•œ ์ชฝ์„ ๋จผ์ € ๊ณ„์‚ฐํ•˜๊ณ  ๊ทธ ๊ฐ’๋งŒํผ ์ œ์™ธํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. X_test = X[num_of_train:] # ์ „์ฒด ๋ฐ์ดํ„ฐ ์ค‘์—์„œ 20%๋งŒํผ ๋’ค์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ y_test = y[num_of_train:] # ์ „์ฒด ๋ฐ์ดํ„ฐ ์ค‘์—์„œ 20%๋งŒํผ ๋’ค์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ X_train = X[:num_of_train] # ์ „์ฒด ๋ฐ์ดํ„ฐ ์ค‘์—์„œ 80%๋งŒํผ ์•ž์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ y_train = y[:num_of_train] # ์ „์ฒด ๋ฐ์ดํ„ฐ ์ค‘์—์„œ 80%๋งŒํผ ์•ž์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆŒ ๋•Œ๋Š” num_of_train์™€ ๊ฐ™์ด ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ์˜ ๋ˆ„๋ฝ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ ๊ตฌํ•œ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋งŒํผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ์ •์ƒ์ ์œผ๋กœ ๋ถ„๋ฆฌ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('X ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(X_test) print('y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(list(y_test)) X ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [[18 19] [20 21] [22 23]] y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [9, 10, 11] ๊ฐ ๊ธธ์ด๊ฐ€ 3์ธ ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. train_test_split()๊ณผ ๋‹ค๋ฅธ ์ ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์„ž์ด์ง€ ์•Š์€ ์ฑ„ ์–ด๋Š ์ง€์ ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์•ž๊ณผ ๋’ค๋กœ ๋ถ„๋ฆฌํ–ˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ˆ˜๋™์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ „์— ์ˆ˜๋™์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์„ž๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋’ค์—์„œ ์ด๋Ÿฌํ•œ ์‹ค์Šต๋“ค์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 02-10 ํ•œ๊ตญ์–ด ์ „์ฒ˜๋ฆฌ ํŒจํ‚ค์ง€(Text Preprocessing Tools for Korean Text) ์œ ์šฉํ•œ ํ•œ๊ตญ์–ด ์ „์ฒ˜๋ฆฌ ํŒจํ‚ค์ง€๋ฅผ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. ์•ž์„œ ์†Œ๊ฐœํ•œ ํ˜•ํƒœ์†Œ์™€ ๋ฌธ์žฅ ํ† ํฌ๋‚˜์ด์ง• ๋„๊ตฌ๋“ค์ธ KoNLPy์™€ KSS(Korean Sentence Splitter)์™€ ํ•จ๊ป˜ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€๋“ค์ž…๋‹ˆ๋‹ค. 1. PyKoSpacing pip install git+https://github.com/haven-jeon/PyKoSpacing.git ์ „ํฌ ์›๋‹˜์ด ๊ฐœ๋ฐœํ•œ PyKoSpacing์€ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜์–ด์žˆ์ง€ ์•Š์€ ๋ฌธ์žฅ์„ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. PyKoSpacing์€ ๋Œ€์šฉ๋Ÿ‰ ์ฝ”ํผ์Šค๋ฅผ ํ•™์Šตํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง„ ๋„์–ด์“ฐ๊ธฐ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ๋กœ ์ค€์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. sent = '๊น€์ฒ ์ˆ˜๋Š” ๊ทน์ค‘ ๋‘ ์ธ๊ฒฉ์˜ ์‚ฌ๋‚˜์ด ์ด๊ด‘์ˆ˜ ์—ญ์„ ๋งก์•˜๋‹ค. ์ฒ ์ˆ˜๋Š” ํ•œ๊ตญ ์œ ์ผ์˜ ํƒœ๊ถŒ๋„ ์ „์Šน์ž๋ฅผ ๊ฐ€๋ฆฌ๋Š” ๊ฒฐ์ „์˜ ๋‚ ์„ ์•ž๋‘๊ณ  10๋…„๊ฐ„ ํ•จ๊ป˜ ํ›ˆ๋ จํ•œ ์‚ฌํ˜•์ธ ์œ ์—ฐ์žฌ(๊น€๊ด‘์ˆ˜ ๋ถ„)๋ฅผ ์ฐพ์œผ๋Ÿฌ ์†์„ธ๋กœ ๋‚ด๋ ค์˜จ ์ธ๋ฌผ์ด๋‹ค.' ์ž„์˜์˜ ๋ฌธ์žฅ์„ ์ž„์˜๋กœ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์—†๋Š” ๋ฌธ์žฅ์œผ๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. new_sent = sent.replace(" ", '') # ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์—†๋Š” ๋ฌธ์žฅ ์ž„์˜๋กœ ๋งŒ๋“ค๊ธฐ print(new_sent) ๊น€์ฒ ์ˆ˜๋Š”๊ทน์ค‘๋‘์ธ๊ฒฉ์˜์‚ฌ๋‚˜์ด์ด๊ด‘์ˆ˜์—ญ์„๋งก์•˜๋‹ค.์ฒ ์ˆ˜๋Š”ํ•œ๊ตญ์œ ์ผ์˜ํƒœ๊ถŒ๋„์ „์Šน์ž๋ฅผ๊ฐ€๋ฆฌ๋Š”๊ฒฐ์ „์˜๋‚ ์„์•ž๋‘๊ณ 10๋…„๊ฐ„ํ•จ๊ป˜ํ›ˆ๋ จํ•œ์‚ฌํ˜•์ธ์œ ์—ฐ์žฌ(๊น€๊ด‘์ˆ˜๋ถ„)๋ฅผ์ฐพ์œผ๋Ÿฌ์†์„ธ๋กœ๋‚ด๋ ค์˜จ์ธ๋ฌผ์ด๋‹ค. ์ด๋ฅผ PyKoSpacing์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์› ๋ฌธ์žฅ๊ณผ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. from pykospacing import Spacing spacing = Spacing() kospacing_sent = spacing(new_sent) print(sent) print(kospacing_sent) ๊น€์ฒ ์ˆ˜๋Š” ๊ทน์ค‘ ๋‘ ์ธ๊ฒฉ์˜ ์‚ฌ๋‚˜์ด ์ด๊ด‘์ˆ˜ ์—ญ์„ ๋งก์•˜๋‹ค. ์ฒ ์ˆ˜๋Š” ํ•œ๊ตญ ์œ ์ผ์˜ ํƒœ๊ถŒ๋„ ์ „์Šน์ž๋ฅผ ๊ฐ€๋ฆฌ๋Š” ๊ฒฐ์ „์˜ ๋‚ ์„ ์•ž๋‘๊ณ  10๋…„๊ฐ„ ํ•จ๊ป˜ ํ›ˆ๋ จํ•œ ์‚ฌํ˜•์ธ ์œ ์—ฐ์žฌ(๊น€๊ด‘์ˆ˜ ๋ถ„)๋ฅผ ์ฐพ์œผ๋Ÿฌ ์†์„ธ๋กœ ๋‚ด๋ ค์˜จ ์ธ๋ฌผ์ด๋‹ค. ๊น€์ฒ ์ˆ˜๋Š” ๊ทน์ค‘ ๋‘ ์ธ๊ฒฉ์˜ ์‚ฌ๋‚˜์ด ์ด๊ด‘์ˆ˜ ์—ญ์„ ๋งก์•˜๋‹ค. ์ฒ ์ˆ˜๋Š” ํ•œ๊ตญ ์œ ์ผ์˜ ํƒœ๊ถŒ๋„ ์ „์Šน์ž๋ฅผ ๊ฐ€๋ฆฌ๋Š” ๊ฒฐ์ „์˜ ๋‚ ์„ ์•ž๋‘๊ณ  10๋…„๊ฐ„ ํ•จ๊ป˜ ํ›ˆ๋ จํ•œ ์‚ฌํ˜•์ธ ์œ ์—ฐ์žฌ(๊น€๊ด‘์ˆ˜ ๋ถ„)๋ฅผ ์ฐพ์œผ๋Ÿฌ ์†์„ธ๋กœ ๋‚ด๋ ค์˜จ ์ธ๋ฌผ์ด๋‹ค. ์ •ํ™•ํ•˜๊ฒŒ ๊ฒฐ๊ณผ๊ฐ€ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. 2. Py-Hanspell pip install git+https://github.com/ssut/py-hanspell.git Py-Hanspell์€ ๋„ค์ด๋ฒ„ ํ•œ๊ธ€ ๋งž์ถค๋ฒ• ๊ฒ€์‚ฌ๊ธฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. from hanspell import spell_checker sent = "๋งž์ถค๋ฒ• ํ‹€๋ฆฌ๋ฉด ์™œ ์•ˆ๋ผ? ์“ฐ๊ณ  ์‹ถ์€ ๋Œ€๋กœ ์“ฐ๋ฉด ๋˜์ง€ " spelled_sent = spell_checker.check(sent) hanspell_sent = spelled_sent.checked print(hanspell_sent) ๋งž์ถค๋ฒ• ํ‹€๋ฆฌ๋ฉด ์™œ ์•ˆ๋ผ? ์“ฐ๊ณ  ์‹ถ์€ ๋Œ€๋กœ ์“ฐ๋ฉด ๋˜์ง€ ์ด ํŒจํ‚ค์ง€๋Š” ๋„์–ด์“ฐ๊ธฐ ๋˜ํ•œ ๋ณด์ •ํ•ฉ๋‹ˆ๋‹ค. PyKoSpacing์— ์‚ฌ์šฉํ•œ ์˜ˆ์ œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. spelled_sent = spell_checker.check(new_sent) hanspell_sent = spelled_sent.checked print(hanspell_sent) print(kospacing_sent) # ์•ž์„œ ์‚ฌ์šฉํ•œ kospacing ํŒจํ‚ค์ง€์—์„œ ์–ป์€ ๊ฒฐ๊ณผ ๊น€์ฒ ์ˆ˜๋Š” ๊ทน ์ค‘ ๋‘ ์ธ๊ฒฉ์˜ ์‚ฌ๋‚˜์ด ์ด๊ด‘์ˆ˜ ์—ญ์„ ๋งก์•˜๋‹ค. ์ฒ ์ˆ˜๋Š” ํ•œ๊ตญ ์œ ์ผ์˜ ํƒœ๊ถŒ๋„ ์ „์Šน์ž๋ฅผ ๊ฐ€๋ฆฌ๋Š” ๊ฒฐ์ „์˜ ๋‚ ์„ ์•ž๋‘๊ณ  10๋…„๊ฐ„ ํ•จ๊ป˜ ํ›ˆ๋ จํ•œ ์‚ฌํ˜•์ธ ์œ ์—ฐ์ œ(๊น€๊ด‘์ˆ˜ ๋ถ„)๋ฅผ ์ฐพ์œผ๋Ÿฌ ์†์„ธ๋กœ ๋‚ด๋ ค์˜จ ์ธ๋ฌผ์ด๋‹ค. ๊น€์ฒ ์ˆ˜๋Š” ๊ทน์ค‘ ๋‘ ์ธ๊ฒฉ์˜ ์‚ฌ๋‚˜์ด ์ด๊ด‘์ˆ˜ ์—ญ์„ ๋งก์•˜๋‹ค. ์ฒ ์ˆ˜๋Š” ํ•œ๊ตญ ์œ ์ผ์˜ ํƒœ๊ถŒ๋„ ์ „์Šน์ž๋ฅผ ๊ฐ€๋ฆฌ๋Š” ๊ฒฐ์ „์˜ ๋‚ ์„ ์•ž๋‘๊ณ  10๋…„๊ฐ„ ํ•จ๊ป˜ ํ›ˆ๋ จํ•œ ์‚ฌํ˜•์ธ ์œ ์—ฐ์žฌ(๊น€๊ด‘์ˆ˜ ๋ถ„)๋ฅผ ์ฐพ์œผ๋Ÿฌ ์†์„ธ๋กœ ๋‚ด๋ ค์˜จ ์ธ๋ฌผ์ด๋‹ค. PyKoSpacing๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๊ฑฐ์˜ ๋น„์Šทํ•˜์ง€๋งŒ ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. 3. SOYNLP๋ฅผ ์ด์šฉํ•œ ๋‹จ์–ด ํ† ํฐํ™” soynlp๋Š” ํ’ˆ์‚ฌ ํƒœ๊น…, ๋‹จ์–ด ํ† ํฐํ™” ๋“ฑ์„ ์ง€์›ํ•˜๋Š” ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €์ž…๋‹ˆ๋‹ค. ๋น„์ง€๋„ ํ•™์Šต์œผ๋กœ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ํ•œ๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ–๊ณ  ์žˆ์œผ๋ฉฐ, ๋ฐ์ดํ„ฐ์— ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋“ค์„ ๋‹จ์–ด๋กœ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค. soynlp ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋‹จ์–ด ์ ์ˆ˜ ํ‘œ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ ์ˆ˜๋Š” ์‘์ง‘ ํ™•๋ฅ (cohesion probability)๊ณผ ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ(branching entropy)๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. pip install soynlp 1. ์‹ ์กฐ์–ด ๋ฌธ์ œ soynlp๋ฅผ ์†Œ๊ฐœํ•˜๊ธฐ ์ „์— ๊ธฐ์กด์˜ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๊ฐ€ ๊ฐ€์ง„ ๋ฌธ์ œ๋Š” ๋ฌด์—‡์ด์—ˆ๋Š”์ง€, SOYNLP๊ฐ€ ์–ด๋–ค ์ ์—์„œ ์œ ์šฉํ•œ์ง€ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. ๊ธฐ์กด์˜ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋Š” ์‹ ์กฐ์–ด๋‚˜ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์— ๋“ฑ๋ก๋˜์ง€ ์•Š์€ ๋‹จ์–ด ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ œ๋Œ€๋กœ ๊ตฌ๋ถ„ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. from konlpy.tag import Okt tokenizer = Okt() print(tokenizer.morphs('์—์ด๋น„์‹์Šค<NAME> 1์›” ์ตœ์• ๋Œ ๊ธฐ๋ถ€ ์š”์ •')) ['์—์ด', '๋น„์‹์Šค', '์ด๋Œ€', 'ํœ˜', '1์›”', '์ตœ์• ', '๋Œ', '๊ธฐ๋ถ€', '์š”์ •'] ์—์ด๋น„ ์‹์Šค๋Š” ์•„์ด๋Œ์˜ ์ด๋ฆ„์ด๊ณ ,<NAME>๋Š” ์—์ด๋น„ ์‹์Šค์˜ ๋ฉค๋ฒ„์ด๋ฉฐ, ์ตœ์• ๋Œ์€ ์ตœ๊ณ ๋กœ ์• ์ • ํ•˜๋Š” ์บ๋ฆญํ„ฐ๋ผ๋Š” ๋œป์ด์ง€๋งŒ ์œ„์˜ ํ˜•ํƒœ์†Œ ๋ถ„์„ ๊ฒฐ๊ณผ์—์„œ๋Š” ์ „๋ถ€ ๋ถ„๋ฆฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ํŠน์ • ๋ฌธ์ž ์‹œํ€€์Šค๊ฐ€ ํ•จ๊ป˜ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋นˆ๋„๊ฐ€ ๋†’๊ณ , ์•ž๋’ค๋กœ ์กฐ์‚ฌ ๋˜๋Š” ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด์„œ ํ•ด๋‹น ๋ฌธ์ž ์‹œํ€€์Šค๋ฅผ ํ˜•ํƒœ์†Œ๋ผ๊ณ  ํŒ๋‹จํ•˜๋Š” ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €๋ผ๋ฉด ์–ด๋–จ๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด ์—์ด๋น„ ์‹์Šค๋ผ๋Š” ๋ฌธ์ž์—ด์ด ์ž์ฃผ ์—ฐ๊ฒฐ๋˜์–ด ๋“ฑ์žฅํ•œ๋‹ค๋ฉด ํ•œ ๋‹จ์–ด๋ผ๊ณ  ํŒ๋‹จํ•˜๊ณ , ๋˜ํ•œ ์—์ด๋น„ ์‹์Šค๋ผ๋Š” ๋‹จ์–ด ์•ž, ๋’ค์— '์ตœ๊ณ ', '๊ฐ€์ˆ˜', '์‹ค๋ ฅ'๊ณผ ๊ฐ™์€ ๋…๋ฆฝ๋œ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์ด ๊ณ„์†ํ•ด์„œ ๋“ฑ์žฅํ•œ๋‹ค๋ฉด ์—์ด๋น„ ์‹์Šค๋ฅผ ํ•œ ๋‹จ์–ด๋กœ ํŒŒ์•…ํ•˜๋Š” ์‹์ด์ง€์š”. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ์•„์ด๋””์–ด๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ soynlp์ž…๋‹ˆ๋‹ค. 2. ํ•™์Šตํ•˜๊ธฐ soynlp๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•™์Šต์— ๊ธฐ๋ฐ˜ํ•œ ํ† ํฌ ๋‚˜์ด์ €์ด๋ฏ€๋กœ ํ•™์Šต์— ํ•„์š”ํ•œ ํ•œ๊ตญ์–ด ๋ฌธ์„œ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. import urllib.request from soynlp import DoublespaceLineCorpus from soynlp.word import WordExtractor urllib.request.urlretrieve("https://raw.githubusercontent.com/lovit/soynlp/master/tutorials/2016-10-20.txt", filename="2016-10-20.txt") ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์ˆ˜์˜ ๋ฌธ์„œ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์ˆ˜์˜ ๋ฌธ์„œ๋กœ ๋ถ„๋ฆฌ corpus = DoublespaceLineCorpus("2016-10-20.txt") len(corpus) 30091 ์ด 3๋งŒ 91๊ฐœ์˜ ๋ฌธ์„œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 3๊ฐœ์˜ ๋ฌธ์„œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ์ค‘๋žตํ•˜์˜€์Šต๋‹ˆ๋‹ค. i = 0 for document in corpus: if len(document) > 0: print(document) i = i+1 if i == 3: break 19 1990 52 1 22 ์˜คํŒจ์‚ฐ ํ„ฐ๋„ ์ด๊ฒฉ์ „ ์šฉ์˜์ž ๊ฒ€๊ฑฐ ์„œ์šธ ์—ฐํ•ฉ๋‰ด์Šค ๊ฒฝ์ฐฐ ๊ด€๊ณ„์ž๋“ค์ด 19์ผ ์˜คํ›„ ์„œ์šธ ๊ฐ•๋ถ๊ตฌ ์˜คํŒจ์‚ฐ ํ„ฐ๋„ ์ธ๊ทผ์—์„œ ์‚ฌ์ œ ์ด๊ธฐ๋ฅผ ๋ฐœ์‚ฌํ•ด ๊ฒฝ์ฐฐ์„ ์‚ดํ•ดํ•œ ์šฉ์˜์ž ์„ฑ๋ชจ ์”จ๋ฅผ ๊ฒ€๊ฑฐํ•˜๊ณ  ์žˆ๋‹ค ... ์ค‘๋žต ... ์ˆฒ์—์„œ ๋ฐœ๊ฒฌ๋๊ณ  ์ผ๋ถ€๋Š” ์„ฑ์”จ๊ฐ€ ์†Œ์ง€ํ•œ ๊ฐ€๋ฐฉ ์•ˆ์— ์žˆ์—ˆ๋‹ค ํ…Œํ—ค๋ž€ ์—ฐํ•ฉ๋‰ด์Šค ๊ฐ•ํ›ˆ์ƒ ํŠนํŒŒ์› ์ด์šฉ ์Šน๊ฐ์ˆ˜ ๊ธฐ์ค€ ์„ธ๊ณ„ ์ตœ๋Œ€ ๊ณตํ•ญ์ธ ์•„๋ž์—๋ฏธ๋ฆฌํŠธ ๋‘๋ฐ”์ด ๊ตญ์ œ๊ณตํ•ญ์€ 19์ผ ํ˜„์ง€์‹œ๊ฐ„ ์ด ๊ณตํ•ญ์„ ์ด๋ฅ™ํ•˜๋Š” ๋ชจ๋“  ํ•ญ๊ณต๊ธฐ์˜ ํƒ‘์Šน๊ฐ์€ ์‚ผ์„ฑ์ „์ž์˜ ๊ฐค๋Ÿญ์‹œ๋…ธํŠธ7์„ ํœด๋Œ€ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค๊ณ  ๋ฐํ˜”๋‹ค ... ์ค‘๋žต ... ์ด๋Ÿฐ ์กฐ์น˜๋Š” ๋‘๋ฐ”์ด ๊ตญ์ œ๊ณตํ•ญ๋ฟ ์•„๋‹ˆ๋ผ ์‹ ๊ณตํ•ญ์ธ ๋‘๋ฐ”์ด ์›”๋“œ์„ผํ„ฐ์—๋„ ์ ์šฉ๋œ๋‹ค ๋ฐฐํ„ฐ๋ฆฌ ํญ๋ฐœ ๋ฌธ์ œ๋กœ ํšŒ์ˆ˜๋œ ๊ฐค๋Ÿญ์‹œ๋…ธํŠธ7 ์—ฐํ•ฉ๋‰ด์Šค ์ž๋ฃŒ ์‚ฌ์ง„ ์ •์ƒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. soynlp๋Š” ํ•™์Šต ๊ธฐ๋ฐ˜์˜ ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €์ด๋ฏ€๋กœ ๊ธฐ์กด์˜ KoNLPy์—์„œ ์ œ๊ณตํ•˜๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋“ค๊ณผ๋Š” ๋‹ฌ๋ฆฌ ํ•™์Šต ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ „์ฒด ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ์‘์ง‘ ํ™•๋ฅ ๊ณผ ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ ๋‹จ์–ด ์ ์ˆ˜ํ‘œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. WordExtractor.extract()๋ฅผ ํ†ตํ•ด์„œ ์ „์ฒด ์ฝ”ํผ์Šค์— ๋Œ€ํ•ด ๋‹จ์–ด ์ ์ˆ˜ํ‘œ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. word_extractor = WordExtractor() word_extractor.train(corpus) word_score_table = word_extractor.extract() training was done. used memory 5.186 Gb all cohesion probabilities was computed. # words = 223348 all branching entropies was computed # words = 361598 all accessor variety was computed # words = 361598 ํ•™์Šต์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 3. SOYNLP์˜ ์‘์ง‘ ํ™•๋ฅ (cohesion probability) ์‘์ง‘ ํ™•๋ฅ ์€ ๋‚ด๋ถ€ ๋ฌธ์ž์—ด(substring)์ด ์–ผ๋งˆ๋‚˜ ์‘์ง‘ํ•˜์—ฌ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์ฒ™๋„์ž…๋‹ˆ๋‹ค. ์‘์ง‘ ํ™•๋ฅ ์€ ๋ฌธ์ž์—ด์„ ๋ฌธ์ž ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ๋‚ด๋ถ€ ๋ฌธ์ž์—ด์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •์—์„œ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ ๊ฐ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๊ทธ๋‹ค์Œ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜์—ฌ ๋ˆ„์  ๊ณฑ์„ ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ’์ด ๋†’์„์ˆ˜๋ก ์ „์ฒด ์ฝ”ํผ์Šค์—์„œ ์ด ๋ฌธ์ž์—ด ์‹œํ€€์Šค๋Š” ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ๋“ฑ์žฅํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์ˆ˜์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›์—'๋ผ๋Š” 7์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ ๋ฌธ์ž ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ ๊ฐ ๋‚ด๋ถ€ ๋ฌธ์ž์—ด์˜ ์Šค์ฝ”์–ด๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹ค์Šต์„ ํ†ตํ•ด ์ง์ ‘ ์‘์ง‘ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. '๋ฐ˜ํฌํ•œ'์˜ ์‘์ง‘ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. word_score_table["๋ฐ˜ํฌํ•œ"].cohesion_forward 0.08838002913645132 ๊ทธ๋ ‡๋‹ค๋ฉด '๋ฐ˜ํฌ ํ•œ๊ฐ•'์˜ ์‘์ง‘ ํ™•๋ฅ ์€ '๋ฐ˜ํฌํ•œ'์˜ ์‘์ง‘ ํ™•๋ฅ ๋ณด๋‹ค ๋†’์„๊นŒ์š”? word_score_table["๋ฐ˜ํฌ ํ•œ๊ฐ•"].cohesion_forward 0.19841268168224552 '๋ฐ˜ํฌ ํ•œ๊ฐ•'์€ '๋ฐ˜ํฌํ•œ'๋ณด๋‹ค ์‘์ง‘ ํ™•๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด '๋ฐ˜ํฌํ•œ๊ฐ•๊ณต'์€ ์–ด๋–จ๊นŒ์š”? word_score_table["๋ฐ˜ํฌํ•œ๊ฐ•๊ณต"].cohesion_forward 0.2972877884078849 ์—ญ์‹œ๋‚˜ '๋ฐ˜ํฌ ํ•œ๊ฐ•'๋ณด๋‹ค ์‘์ง‘ ํ™•๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค. '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›'์€ ์–ด๋–จ๊นŒ์š”? word_score_table["๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›"].cohesion_forward 0.37891487632839754 '๋ฐ˜ํฌํ•œ๊ฐ•๊ณต'๋ณด๋‹ค ์‘์ง‘ ํ™•๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๋‹ค๊ฐ€ ์กฐ์‚ฌ '์—'๋ฅผ ๋ถ™์ธ '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›์—'๋Š” ์–ด๋–จ๊นŒ์š”? word_score_table["๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›์—"].cohesion_forward 0.33492963377557666 ์˜คํžˆ๋ ค '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›'๋ณด๋‹ค ์‘์ง‘๋„๊ฐ€ ๋‚ฎ์•„์ง‘๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๊ฒฐํ•ฉ ๋„๋Š” '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›'์ผ ๋•Œ๊ฐ€ ๊ฐ€์žฅ ๋†’์•˜์Šต๋‹ˆ๋‹ค. ์‘์ง‘๋„๋ฅผ ํ†ตํ•ด ํŒ๋‹จํ•˜๊ธฐ์— ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ํŒ๋‹จํ•˜๊ธฐ์— ๊ฐ€์žฅ ์ ํ•ฉํ•œ ๋ฌธ์ž์—ด์€ '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›'์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. 4. SOYNLP์˜ ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ(branching entropy) Branching Entropy๋Š” ํ™•๋ฅ  ๋ถ„ํฌ์˜ ์—”ํŠธ๋กœํ”ผ ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ์–ผ๋งˆ๋‚˜ ๋‹ค์Œ ๋ฌธ์ž๊ฐ€ ๋“ฑ์žฅํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์ฒ™๋„์ž…๋‹ˆ๋‹ค. ํ€ด์ฆˆ๋ฅผ ํ•˜๋‚˜ ๋‚ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์–ด๋–ค ๋‹จ์–ด๋ฅผ ์ƒ๊ฐ ์ค‘์ธ๋ฐ, ํ•œ ๋ฌธ์ž์”ฉ ๋งํ•ด๋“œ๋ฆด ํ…Œ๋‹ˆ๊นŒ ๋งค๋ฒˆ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ๋งž์ถ”๋Š” ๊ฒƒ์ด ํ€ด์ฆˆ์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋Š” '๋””'์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ๋ฌธ์ž๋ฅผ ๋งž์ถฐ๋ณด์„ธ์š”. ์†”์งํžˆ ๊ฐ€๋Š ์ด ์ž˜ ์•ˆ ๊ฐ€์ง€์š”? '๋””'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ์–ด๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋งŽ์€๋ฐ์š”. ์ •๋‹ต์€ '์Šค'์ž…๋‹ˆ๋‹ค. ์ด์ œ '๋””์Šค'๊นŒ์ง€ ๋‚˜์™”๋„ค์š”. '๋””์Šค '๋‹ค์Œ ๋ฌธ์ž๋Š” ๋ญ˜๊นŒ์š”? '๋””์Šค์นด์šดํŠธ'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋‹ˆ๊นŒ '์นด'์ผ๊นŒ? ์•„๋‹ˆ๋ฉด '๋””์Šค์ฝ”๋“œ'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋‹ˆ๊นŒ '์ฝ”'์ธ๊ฐ€? ์ƒ๊ฐํ•ด ๋ณด๋‹ˆ '๋””์Šค์ฝ”'๊ฐ€ ์ •๋‹ต์ผ ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”. ๊ทธ๋Ÿฌ๋ฉด '์ฝ”'์ธ๊ฐ€? '๋””์Šค์•„๋„ˆ๋“œ'๋ผ๋Š” ๊ฒŒ์ž„์ด ์žˆ์œผ๋‹ˆ๊นŒ '์•„'? ์ด ๋‹จ์–ด๋“ค์„ ์ƒ๊ฐํ•˜์‹  ๋ถ„๋“ค์€ ์ „๋ถ€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค. ์ •๋‹ต์€ 'ํ”Œ'์ด์—ˆ์Šต๋‹ˆ๋‹ค. '๋””์Šคํ”Œ'๊นŒ์ง€ ์™”์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ฌธ์ž ๋งž์ถฐ๋ณด์„ธ์š”. ์ด์ œ ์ข€ ๋ช…๋ฐฑํ•ด์ง‘๋‹ˆ๋‹ค. ์ •๋‹ต์€ '๋ ˆ'์ž…๋‹ˆ๋‹ค. '๋””์Šคํ”Œ๋ ˆ์ด' ๋‹ค์Œ์—๋Š” ์–ด๋–ค ๋ฌธ์ž์ผ๊นŒ์š”? ์ •๋‹ต์€ '์ด'์ž…๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ƒ๊ฐํ•œ ๋‹จ์–ด๋Š” '๋””์Šคํ”Œ๋ ˆ์ด'์˜€์Šต๋‹ˆ๋‹ค. ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ๋ฅผ ์ฃผ์–ด์ง„ ๋ฌธ์ž ์‹œํ€€์Šค์—์„œ ๋‹ค์Œ ๋ฌธ์ž ์˜ˆ์ธก์„ ์œ„ํ•ด ํ—ท๊ฐˆ๋ฆฌ๋Š” ์ •๋„๋กœ ๋น„์œ ํ•ด ๋ด…์‹œ๋‹ค. ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ์˜ ๊ฐ’์€ ํ•˜๋‚˜์˜ ์™„์„ฑ๋œ ๋‹จ์–ด์— ๊ฐ€๊นŒ์›Œ์งˆ์ˆ˜๋ก ๋ฌธ๋งฅ์œผ๋กœ ์ธํ•ด ์ ์  ์ •ํ™•ํžˆ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋ฉด์„œ ์ ์  ์ค„์–ด๋“œ๋Š” ์–‘์ƒ์„ ๋ณด์ž…๋‹ˆ๋‹ค. word_score_table["๋””์Šค"].right_branching_entropy 1.6371694761537934 word_score_table["๋””์Šคํ”Œ"].right_branching_entropy -0.0 '๋””์Šค' ๋‹ค์Œ์—๋Š” ๋‹ค์–‘ํ•œ ๋ฌธ์ž๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ 1.63์ด๋ผ๋Š” ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐ˜๋ฉด, '๋””์Šคํ”Œ'์ด๋ผ๋Š” ๋ฌธ์ž์—ด ๋‹ค์Œ์—๋Š” ๋‹ค์Œ ๋ฌธ์ž๋กœ '๋ ˆ'๊ฐ€ ์˜ค๋Š” ๊ฒƒ์ด ๋„ˆ๋ฌด๋‚˜ ๋ช…๋ฐฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— 0์ด๋ž€ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. word_score_table["๋””์Šคํ”Œ๋ ˆ์ด"].right_branching_entropy -0.0 word_score_table["๋””์Šคํ”Œ๋ ˆ์ด"].right_branching_entropy 3.1400392861792916 ๊ฐ‘์ž๊ธฐ ๊ฐ’์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ฌธ์ž ์‹œํ€€์Šค '๋””์Šคํ”Œ๋ ˆ์ด'๋ผ๋Š” ๋ฌธ์ž ์‹œํ€€์Šค ๋‹ค์Œ์—๋Š” ์กฐ์‚ฌ๋‚˜ ๋‹ค๋ฅธ ๋‹จ์–ด์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ•˜๋‚˜์˜ ๋‹จ์–ด๊ฐ€ ๋๋‚˜๋ฉด ๊ทธ ๊ฒฝ๊ณ„ ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋‹ค์‹œ ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ ๊ฐ’์ด ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฐ’์œผ๋กœ ๋‹จ์–ด๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๊ฒ ์ฃ ? 5. SOYNLP์˜ L tokenizer ํ•œ๊ตญ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ๋‚˜๋ˆˆ ์–ด์ ˆ ํ† ํฐ์€ ์ฃผ๋กœ L ํ† ํฐ + R ํ† ํฐ์˜<NAME>์„ ๊ฐ€์งˆ ๋•Œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ '๊ณต์›์—'๋Š” '๊ณต์› +์—'๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ฒ ์ง€์š”. ๋˜๋Š” '๊ณต๋ถ€ํ•˜๋Š”'์€ '๊ณต๋ถ€ + ํ•˜๋Š”'์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. L ํ† ํฌ ๋‚˜์ด์ €๋Š” L ํ† ํฐ + R ํ† ํฐ์œผ๋กœ ๋‚˜๋ˆ„๋˜, ๋ถ„๋ฆฌ ๊ธฐ์ค€์„ ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ L ํ† ํฐ์„ ์ฐพ์•„๋‚ด๋Š” ์›๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. from soynlp.tokenizer import LTokenizer scores = {word:score.cohesion_forward for word, score in word_score_table.items()} l_tokenizer = LTokenizer(scores=scores) l_tokenizer.tokenize("๊ตญ์ œ์‚ฌํšŒ์™€ ์šฐ๋ฆฌ์˜ ๋…ธ๋ ฅ๋“ค๋กœ ๋ฒ”์ฃ„๋ฅผ ์ฒ™๊ฒฐํ•˜์ž", flatten=False) [('๊ตญ์ œ์‚ฌํšŒ', '์™€'), ('์šฐ๋ฆฌ', '์˜'), ('๋…ธ๋ ฅ', '๋“ค๋กœ'), ('๋ฒ”์ฃ„', '๋ฅผ'), ('์ฒ™๊ฒฐ', 'ํ•˜์ž')] 6. ์ตœ๋Œ€ ์ ์ˆ˜ ํ† ํฌ ๋‚˜์ด์ € ์ตœ๋Œ€ ์ ์ˆ˜ ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๋ฌธ์žฅ์—์„œ ์ ์ˆ˜๊ฐ€ ๋†’์€ ๊ธ€์ž ์‹œํ€€์Šค๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฐพ์•„๋‚ด๋Š” ํ† ํฌ ๋‚˜์ด์ €์ž…๋‹ˆ๋‹ค. ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๋ฌธ์žฅ์„ ๋„ฃ์–ด์„œ ์ ์ˆ˜๋ฅผ ํ†ตํ•ด ํ† ํฐํ™”๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from soynlp.tokenizer import MaxScoreTokenizer maxscore_tokenizer = MaxScoreTokenizer(scores=scores) maxscore_tokenizer.tokenize("๊ตญ์ œ์‚ฌํšŒ์™€ ์šฐ๋ฆฌ์˜ ๋…ธ๋ ฅ๋“ค๋กœ ๋ฒ”์ฃ„๋ฅผ ์ฒ™๊ฒฐํ•˜์ž") ['๊ตญ์ œ์‚ฌํšŒ', '์™€', '์šฐ๋ฆฌ', '์˜', '๋…ธ๋ ฅ', '๋“ค๋กœ', '๋ฒ”์ฃ„', '๋ฅผ', '์ฒ™๊ฒฐ', 'ํ•˜์ž'] 4. SOYNLP๋ฅผ ์ด์šฉํ•œ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž ์ •์ œ SNS๋‚˜ ์ฑ„ํŒ… ๋ฐ์ดํ„ฐ์™€ ๊ฐ™์€ ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ์—๋Š” ใ…‹ใ…‹, ใ…Žใ…Ž ๋“ฑ์˜ ์ด๋ชจํ‹ฐ์ฝ˜์˜ ๊ฒฝ์šฐ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์—ฐ์†๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€๋ฐ ใ…‹ใ…‹, ใ…‹ใ…‹ใ…‹, ใ…‹ใ…‹ใ…‹ใ…‹์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ๋ถˆํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์€ ํ•˜๋‚˜๋กœ ์ •๊ทœํ™”์‹œ์ผœ์ค๋‹ˆ๋‹ค. from soynlp.normalizer import * print(emoticon_normalize('์•œใ…‹ใ…‹ใ…‹ใ…‹์ด์˜ํ™”์กด์žผ์“ฐใ… ใ… ใ… ใ… ใ… ', num_repeats=2)) print(emoticon_normalize('์•œใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹์ด์˜ํ™”์กด์žผ์“ฐใ… ใ… ใ… ใ… ', num_repeats=2)) print(emoticon_normalize('์•œใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹์ด์˜ํ™”์กด์žผ์“ฐใ… ใ… ใ… ใ… ใ… ใ… ', num_repeats=2)) print(emoticon_normalize('์•œใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹์ด์˜ํ™”์กด์žผ์“ฐใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ', num_repeats=2)) ์•„ใ…‹ใ…‹์˜ํ™”์กด์žผ์“ฐใ… ใ…  ์•„ใ…‹ใ…‹์˜ํ™”์กด์žผ์“ฐใ… ใ…  ์•„ใ…‹ใ…‹์˜ํ™”์กด์žผ์“ฐใ… ใ…  ์•„ใ…‹ใ…‹์˜ํ™”์กด์žผ์“ฐใ… ใ…  ์˜๋ฏธ ์—†๊ฒŒ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์€ ๋น„๋‹จ ์ด๋ชจํ‹ฐ์ฝ˜์— ํ•œ์ •๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. print(repeat_normalize('์™€ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•ซ', num_repeats=2)) print(repeat_normalize('์™€ํ•˜ํ•˜ ํ•˜ํ•˜ ํ•˜ํ•˜ ํ•ซ', num_repeats=2)) print(repeat_normalize('์™€ํ•˜ํ•˜ ํ•˜ํ•˜ ํ•ซ', num_repeats=2)) ์™€ํ•˜ํ•˜ ํ•ซ ์™€ํ•˜ํ•˜ ํ•ซ ์™€ํ•˜ํ•˜ ํ•ซ 5. Customized KoNLPy ์˜์–ด๊ถŒ ์–ธ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๋งŒ ํ•ด๋„ ๋‹จ์–ด๋“ค์ด ์ž˜ ๋ถ„๋ฆฌ๋˜์ง€๋งŒ, ํ•œ๊ตญ์–ด๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๊ณ  ์•ž์—์„œ ๋ช‡ ์ฐจ๋ก€ ์–ธ๊ธ‰ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋งŒํผ ์ด๋ฒˆ์—๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ์ด๋Ÿฐ ์ƒํ™ฉ์— ๋ด‰์ฐฉํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ํ˜•ํƒœ์†Œ ๋ถ„์„ ์ž…๋ ฅ : '์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.' ํ˜•ํƒœ์†Œ ๋ถ„์„ ๊ฒฐ๊ณผ : ['์€', '๊ฒฝ์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ์‚ฌ์‹ค ์œ„๋ฌธ์žฅ์—์„œ '์€๊ฒฝ์ด'๋Š” ์‚ฌ๋žŒ ์ด๋ฆ„์ด๋ฏ€๋กœ ์ œ๋Œ€๋กœ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” '์€', '๊ฒฝ์ด'์™€ ๊ฐ™์ด ๊ธ€์ž๊ฐ€ ๋ถ„๋ฆฌ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ '์€๊ฒฝ์ด' ๋˜๋Š” ์ตœ์†Œํ•œ '์€๊ฒฝ'์ด๋ผ๋Š” ๋‹จ์–ด ํ† ํฐ์„ ์–ป์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์— ์‚ฌ์šฉ์ž ์‚ฌ์ „์„ ์ถ”๊ฐ€ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. '์€๊ฒฝ์ด'๋Š” ํ•˜๋‚˜์˜ ๋‹จ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„๋ฆฌํ•˜์ง€ ๋ง๋ผ๊ณ  ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์— ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์‚ฌ์ „์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋งˆ๋‹ค ๋‹ค๋ฅธ๋ฐ, ์ƒ๊ฐ๋ณด๋‹ค ๋ณต์žกํ•œ ๊ฒฝ์šฐ๋“ค์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” Customized Konlpy๋ผ๋Š” ์‚ฌ์šฉ์ž ์‚ฌ์ „ ์ถ”๊ฐ€๊ฐ€ ๋งค์šฐ ์‰ฌ์šด ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. pip install customized_konlpy customized_konlpy์—์„œ ์ œ๊ณตํ•˜๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Twitter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ž์„œ ์†Œ๊ฐœํ–ˆ๋˜ ์˜ˆ๋ฌธ์„ ๋‹จ์–ด ํ† ํฐํ™”ํ•ด๋ด…์‹œ๋‹ค. from ckonlpy.tag import Twitter twitter = Twitter() twitter.morphs('์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.') ['์€', '๊ฒฝ์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ์•ž์„œ ์†Œ๊ฐœํ•œ ์˜ˆ์‹œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ '์€๊ฒฝ์ด'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ '์€', '๊ฒฝ์ด'์™€ ๊ฐ™์ด ๋ถ„๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Twitter์— add_dictionary('๋‹จ์–ด', 'ํ’ˆ์‚ฌ')์™€ ๊ฐ™์€<NAME>์œผ๋กœ ์‚ฌ์ „ ์ถ”๊ฐ€๋ฅผ ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. twitter.add_dictionary('์€๊ฒฝ์ด', 'Noun') ์ œ๋Œ€๋กœ ๋ฐ˜์˜๋˜์—ˆ๋Š”์ง€ ๋™์ผํ•œ ์˜ˆ๋ฌธ์„ ๋‹ค์‹œ ํ˜•ํƒœ์†Œ ๋ถ„์„ํ•ด ๋ด…์‹œ๋‹ค. twitter.morphs('์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.') ['์€๊ฒฝ์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] '์€๊ฒฝ์ด'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ œ๋Œ€๋กœ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ์ธ์‹๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 03. ์–ธ์–ด ๋ชจ๋ธ(Language Model) ์–ธ์–ด ๋ชจ๋ธ(Languagel Model)์ด๋ž€ ๋‹จ์–ด ์‹œํ€€์Šค(๋ฌธ์žฅ)์— ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๋Š” ๋ชจ๋ธ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๋ฌธ์žฅ๋“ค์ด ์žˆ์„ ๋•Œ, ๊ธฐ๊ณ„๊ฐ€ ์ด ๋ฌธ์žฅ์€ ์ ์ ˆํ•ด! ์ด ๋ฌธ์žฅ์€ ๋ง์ด ์•ˆ ๋ผ!๋ผ๊ณ  ์‚ฌ๋žŒ์ฒ˜๋Ÿผ ์ •ํ™•ํžˆ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๊ธฐ๊ณ„์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์„ฑ๋Šฅ์ด ๋›ฐ์–ด๋‚˜๋‹ค ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํ†ต๊ณ„์— ๊ธฐ๋ฐ˜ํ•œ ์ „ํ†ต์ ์ธ ์–ธ์–ด ๋ชจ๋ธ(Statistical Languagel Model, SLM)์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ํ†ต๊ณ„์— ๊ธฐ๋ฐ˜ํ•œ ์–ธ์–ด ๋ชจ๋ธ์€ ์šฐ๋ฆฌ๊ฐ€ ์‹ค์ œ ์‚ฌ์šฉํ•˜๋Š” ์ž์—ฐ์–ด๋ฅผ ๊ทผ์‚ฌํ•˜๊ธฐ์—๋Š” ๋งŽ์€ ํ•œ๊ณ„๊ฐ€ ์žˆ์—ˆ๊ณ , ์š”์ฆ˜ ๋“ค์–ด ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ๊ทธ๋Ÿฌํ•œ ํ•œ๊ณ„๋ฅผ ๋งŽ์ด ํ•ด๊ฒฐํ•ด ์ฃผ๋ฉด์„œ ํ†ต๊ณ„ ๊ธฐ๋ฐ˜ ์–ธ์–ด ๋ชจ๋ธ์€ ๋งŽ์ด ์‚ฌ์šฉ ์šฉ๋„๊ฐ€ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋Ÿผ์—๋„ ํ†ต๊ณ„ ๊ธฐ๋ฐ˜ ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•œ ์ดํ•ด๋Š” ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ „์ฒด์ ์ธ ์‹œ์•ผ๋ฅผ ๊ฐ–๋Š” ์ผ์— ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. 03-01 ์–ธ์–ด ๋ชจ๋ธ(Language Model)์ด๋ž€? ์–ธ์–ด ๋ชจ๋ธ(Language Model, LM)์€ ์–ธ์–ด๋ผ๋Š” ํ˜„์ƒ์„ ๋ชจ๋ธ๋งํ•˜๊ณ  ์ž ๋‹จ์–ด ์‹œํ€€์Šค(๋ฌธ์žฅ)์— ํ™•๋ฅ ์„ ํ• ๋‹น(assign) ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ๋Š” ํ†ต๊ณ„๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•๊ณผ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์—๋Š” ํ†ต๊ณ„๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•๋ณด๋‹ค๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์ด ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ ํ•ซํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ๊ธฐ์ˆ ์ธ GPT๋‚˜ BERT ๋˜ํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฐœ๋…๊ณผ ์–ธ์–ด ๋ชจ๋ธ์˜ ์ „ํ†ต์  ์ ‘๊ทผ ๋ฐฉ์‹์ธ ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. 1. ์–ธ์–ด ๋ชจ๋ธ(Language Model) ์–ธ์–ด ๋ชจ๋ธ์€ ๋‹จ์–ด ์‹œํ€€์Šค์— ํ™•๋ฅ ์„ ํ• ๋‹น(assign) ํ•˜๋Š” ์ผ์„ ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์กฐ๊ธˆ ํ’€์–ด์„œ ์“ฐ๋ฉด, ์–ธ์–ด ๋ชจ๋ธ์€ ๊ฐ€์žฅ ์ž์—ฐ์Šค๋Ÿฌ์šด ๋‹จ์–ด ์‹œํ€€์Šค๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด ์‹œํ€€์Šค์— ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ€์žฅ ๋ณดํŽธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์€ ์–ธ์–ด ๋ชจ๋ธ์ด ์ด์ „ ๋‹จ์–ด๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์œ ํ˜•์˜ ์–ธ์–ด ๋ชจ๋ธ๋กœ๋Š” ์ฃผ์–ด์ง„ ์–‘์ชฝ์˜ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๊ฐ€์šด๋ฐ ๋น„์–ด์žˆ๋Š” ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ฌธ์žฅ์˜ ๊ฐ€์šด๋ฐ์— ์žˆ๋Š” ๋‹จ์–ด๋ฅผ ๋น„์›Œ๋†“๊ณ  ์–‘์ชฝ์˜ ๋ฌธ๋งฅ์„ ํ†ตํ•ด์„œ ๋นˆ์นธ์˜ ๋‹จ์–ด์ธ์ง€ ๋งž์ถ”๋Š” ๊ณ ๋“ฑํ•™๊ต ์ˆ˜ํ—˜ ์‹œํ—˜์˜ ๋นˆ์นธ ์ถ”๋ก  ๋ฌธ์ œ์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์ด ์œ ํ˜•์˜ ์–ธ์–ด ๋ชจ๋ธ์€ BERT ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃจ๊ฒŒ ๋  ์˜ˆ์ •์ด๊ณ , ๊ทธ๋•Œ๊นŒ์ง€๋Š” ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ์‹์—๋งŒ ์ง‘์ค‘ํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์— -ing๋ฅผ ๋ถ™์ธ ์–ธ์–ด ๋ชจ๋ธ๋ง(Language Modeling)์€ ์ฃผ์–ด์ง„ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ์•„์ง ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ž‘์—…์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์–ธ์–ด ๋ชจ๋ธ์ด ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ผ์€ ์–ธ์–ด ๋ชจ๋ธ๋ง์ž…๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋กœ ์œ ๋ช…ํ•œ ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™๊ต์—์„œ๋Š” ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฌธ๋ฒ•(grammar)์ด๋ผ๊ณ  ๋น„์œ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์ด ๋‹จ์–ด๋“ค์˜ ์กฐํ•ฉ์ด ์–ผ๋งˆ๋‚˜ ์ ์ ˆํ•œ์ง€, ๋˜๋Š” ํ•ด๋‹น ๋ฌธ์žฅ์ด ์–ผ๋งˆ๋‚˜ ์ ํ•ฉํ•œ์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์ผ์„ ํ•˜๋Š” ๊ฒƒ์ด ๋งˆ์น˜ ๋ฌธ๋ฒ•์ด ํ•˜๋Š” ์ผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 2. ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ  ํ• ๋‹น ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋‹จ์–ด ์‹œํ€€์Šค์— ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๋Š” ์ผ์ด ์™œ ํ•„์š”ํ• ๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋Œ€๋ฌธ์ž P๋Š” ํ™•๋ฅ ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. a. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ(Machine Translation): P(๋‚˜๋Š” ๋ฒ„์Šค๋ฅผ ํƒ”๋‹ค) > P(๋‚˜๋Š” ๋ฒ„์Šค๋ฅผ ํƒœ์šด๋‹ค) : ์–ธ์–ด ๋ชจ๋ธ์€ ๋‘ ๋ฌธ์žฅ์„ ๋น„๊ตํ•˜์—ฌ ์ขŒ์ธก์˜ ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์ด ๋” ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. b. ์˜คํƒ€ ๊ต์ •(Spell Correction) ์„ ์ƒ๋‹˜์ด ๊ต์‹ค๋กœ ๋ถ€๋ฆฌ๋‚˜์ผ€ P(๋‹ฌ๋ ค๊ฐ”๋‹ค) > P(์ž˜๋ ค๊ฐ”๋‹ค) : ์–ธ์–ด ๋ชจ๋ธ์€ ๋‘ ๋ฌธ์žฅ์„ ๋น„๊ตํ•˜์—ฌ ์ขŒ์ธก์˜ ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์ด ๋” ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. c. ์Œ์„ฑ ์ธ์‹(Speech Recognition) P(๋‚˜๋Š” ๋ฉ”๋กฑ์„ ๋จน๋Š”๋‹ค) < P(๋‚˜๋Š” ๋ฉœ๋ก ์„ ๋จน๋Š”๋‹ค) : ์–ธ์–ด ๋ชจ๋ธ์€ ๋‘ ๋ฌธ์žฅ์„ ๋น„๊ตํ•˜์—ฌ ์šฐ์ธก์˜ ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์ด ๋” ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์€ ์œ„์™€ ๊ฐ™์ด ํ™•๋ฅ ์„ ํ†ตํ•ด ๋ณด๋‹ค ์ ์ ˆํ•œ ๋ฌธ์žฅ์„ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. 3. ์ฃผ์–ด์ง„ ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด ์˜ˆ์ธกํ•˜๊ธฐ ์–ธ์–ด ๋ชจ๋ธ์€ ๋‹จ์–ด ์‹œํ€€์Šค์— ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹จ์–ด ์‹œํ€€์Šค์— ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ€์žฅ ๋ณดํŽธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด์ „ ๋‹จ์–ด๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ๋กœ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. A. ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ  ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ, ๋‹จ์–ด ์‹œํ€€์Šค๋ฅผ ๋Œ€๋ฌธ์ž๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) P ( 1 w, 3 w, 5. . w) B. ๋‹ค์Œ ๋‹จ์–ด ๋“ฑ์žฅ ํ™•๋ฅ  ๋‹ค์Œ ๋‹จ์–ด ๋“ฑ์žฅ ํ™•๋ฅ ์„ ์‹์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. -1๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ๋‚˜์—ด๋œ ์ƒํƒœ์—์„œ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( n w, . , n 1 ) |์˜ ๊ธฐํ˜ธ๋Š” ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ (conditional probability)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์„ฏ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ํ™•๋ฅ ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( 5 w, 2 w, 4 ) ์ „์ฒด ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ ์€ ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ์˜ˆ์ธก๋˜๊ณ  ๋‚˜์„œ์•ผ ์•Œ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) P ( 1 w, 3 w, 5. . n ) โˆ = n ( i w, . , 4. ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฐ„๋‹จํ•œ ์ง๊ด€ ๋น„ํ–‰๊ธฐ๋ฅผ ํƒ€๋ ค๊ณ  ๊ณตํ•ญ์— ๊ฐ”๋Š”๋ฐ ์ง€๊ฐ์„ ํ•˜๋Š” ๋ฐ”๋žŒ์— ๋น„ํ–‰๊ธฐ๋ฅผ [?]๋ผ๋Š” ๋ฌธ์žฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. '๋น„ํ–‰๊ธฐ๋ฅผ' ๋‹ค์Œ์— ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ์˜ค๊ฒŒ ๋ ์ง€ ์‚ฌ๋žŒ์€ ์‰ฝ๊ฒŒ '๋†“์ณค๋‹ค'๋ผ๊ณ  ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ ์ง€์‹์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋‹จ์–ด๋“ค์„ ํ›„๋ณด์— ๋†“๊ณ  ๋†“์ณค๋‹ค๋Š” ๋‹จ์–ด๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์ด ๊ฐ€์žฅ ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ธฐ๊ณ„์—๊ฒŒ ์œ„๋ฌธ์žฅ์„ ์ฃผ๊ณ , '๋น„ํ–‰๊ธฐ๋ฅผ' ๋‹ค์Œ์— ๋‚˜์˜ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ด ๋ณด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ๊ณผ์—ฐ ์–ด๋–ป๊ฒŒ ์ตœ๋Œ€ํ•œ ์ •ํ™•ํžˆ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ๊ธฐ๊ณ„๋„ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์•ž์— ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ๋‚˜์™”๋Š”์ง€ ๊ณ ๋ คํ•˜์—ฌ ํ›„๋ณด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋‹จ์–ด๋“ค์— ๋Œ€ํ•ด์„œ ํ™•๋ฅ ์„ ์˜ˆ์ธกํ•ด ๋ณด๊ณ  ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ๋‹จ์–ด๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์•ž์— ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ๋‚˜์™”๋Š”์ง€ ๊ณ ๋ คํ•˜์—ฌ ํ›„๋ณด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋‹จ์–ด๋“ค์— ๋Œ€ํ•ด์„œ ๋“ฑ์žฅ ํ™•๋ฅ ์„ ์ถ”์ •ํ•˜๊ณ  ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ๋‹จ์–ด๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. 5. ๊ฒ€์ƒ‰ ์—”์ง„์—์„œ์˜ ์–ธ์–ด ๋ชจ๋ธ์˜ ์˜ˆ ๊ฒ€์ƒ‰ ์—”์ง„์ด ์ž…๋ ฅ๋œ ๋‹จ์–ด๋“ค์˜ ๋‚˜์—ด์— ๋Œ€ํ•ด์„œ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 03-02 ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ(Statistical Language Model, SLM) ์–ธ์–ด ๋ชจ๋ธ์˜ ์ „ํ†ต์ ์ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์ธ ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ์ด ํ†ต๊ณ„์ ์ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์œผ๋กœ ์–ด๋–ป๊ฒŒ ์–ธ์–ด๋ฅผ ๋ชจ๋ธ๋ง ํ•˜๋Š”์ง€ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ(Statistical Language Model)์€ ์ค„์—ฌ์„œ SLM์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 1. ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ  ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์€ ๋‘ ํ™•๋ฅ  ( ) P ( ) ์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ด€๊ณ„๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ( | ) P ( , ) P ( ) ( , ) P ( ) ( | ) ๋” ๋งŽ์€ ํ™•๋ฅ ์— ๋Œ€ํ•ด์„œ ์ผ๋ฐ˜ํ™”ํ•ด๋ด…์‹œ๋‹ค. 4๊ฐœ์˜ ํ™•๋ฅ ์ด ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์งˆ ๋•Œ, ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( , , , ) P ( ) ( | ) ( | , ) ( | , , ) ์ด๋ฅผ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์˜ ์—ฐ์‡„ ๋ฒ•์น™(chain rule)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ๋Š” 4๊ฐœ๊ฐ€ ์•„๋‹Œ ๊ฐœ์— ๋Œ€ํ•ด์„œ ์ผ๋ฐ˜ํ™”๋ฅผ ํ•ด๋ด…์‹œ๋‹ค. ( 1 x, 3. x) P ( 1 ) ( 2 x) ( 3 x, 2 ) . P ( n x ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์— ๋Œ€ํ•œ ์ •์˜๋ฅผ ํ†ตํ•ด ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ๋ฌธ์žฅ์— ๋Œ€ํ•œ ํ™•๋ฅ  ๋ฌธ์žฅ 'An adorable little boy is spreading smiles'์˜ ํ™•๋ฅ  ( An adorable little boy is spreading smiles ) ๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ๊ฐ ๋‹จ์–ด๋Š” ๋ฌธ๋งฅ์ด๋ผ๋Š” ๊ด€๊ณ„๋กœ ์ธํ•ด ์ด์ „ ๋‹จ์–ด์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ ๋‚˜์˜จ ๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ๋‹จ์–ด๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์ด ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์„ ๊ตฌํ•˜๊ณ ์ž ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์˜ ์ผ๋ฐ˜ํ™” ์‹์„ ๋ฌธ์žฅ์˜ ํ™•๋ฅ  ๊ด€์ ์—์„œ ๋‹ค์‹œ ์ ์–ด๋ณด๋ฉด ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์€ ๊ฐ ๋‹จ์–ด๋“ค์ด ์ด์ „ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋‹ค์Œ ๋‹จ์–ด๋กœ ๋“ฑ์žฅํ•  ํ™•๋ฅ ์˜ ๊ณฑ์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ( 1 w, 3 w, 5. . n ) โˆ = n ( n w, . , n 1 ) ์œ„์˜ ๋ฌธ์žฅ์— ํ•ด๋‹น ์‹์„ ์ ์šฉํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( An adorable little boy is spreading smiles ) P ( An ) P ( adorable|An ) P ( little|An adorable ) P ( boy|An adorable little ) P ( is|An adorable little boy ) P ( spreading|An adorable little boy is ) P ( smiles|An adorable little boy is spreading ) ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ์˜ˆ์ธก ํ™•๋ฅ ๋“ค์„ ๊ณฑํ•ฉ๋‹ˆ๋‹ค. 3. ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ ์ ‘๊ทผ ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ ๋‹จ์–ด์— ๋Œ€ํ•œ ์˜ˆ์ธก ํ™•๋ฅ ์„ ๋ชจ๋‘ ๊ณฑํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์•Œ์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด SLM์€ ์ด์ „ ๋‹จ์–ด๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด์— ๋Œ€ํ•œ ํ™•๋ฅ ์€ ์–ด๋–ป๊ฒŒ ๊ตฌํ• ๊นŒ์š”? ์ •๋‹ต์€ ์นด์šดํŠธ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. An adorable little boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ, is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์ธ ( is|An adorable little boy ) ๋ฅผ ๊ตฌํ•ด๋ด…์‹œ๋‹ค. (is|An adorable little boy ) count(An adorable little boy is ) count(An adorable little boy ) ๊ทธ ํ™•๋ฅ ์€ ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ธฐ๊ณ„๊ฐ€ ํ•™์Šตํ•œ ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ์—์„œ An adorable little boy๊ฐ€ 100๋ฒˆ ๋“ฑ์žฅํ–ˆ๋Š”๋ฐ ๊ทธ๋‹ค์Œ์— is๊ฐ€ ๋“ฑ์žฅํ•œ ๊ฒฝ์šฐ๋Š” 30๋ฒˆ์ด๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ ( is|An adorable little boy ) ๋Š” 30%์ž…๋‹ˆ๋‹ค. 4. ์นด์šดํŠธ ๊ธฐ๋ฐ˜ ์ ‘๊ทผ์˜ ํ•œ๊ณ„ - ํฌ์†Œ ๋ฌธ์ œ(Sparsity Problem) ์–ธ์–ด ๋ชจ๋ธ์€ ์‹ค์ƒํ™œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด์˜ ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ๊ทผ์‚ฌ ๋ชจ๋ธ๋ง ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ์•„๋ณผ ๋ฐฉ๋ฒ•์€ ์—†๊ฒ ์ง€๋งŒ ํ˜„์‹ค์—์„œ๋„ An adorable little boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์ด๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹ค์ œ ์ž์—ฐ์–ด์˜ ํ™•๋ฅ  ๋ถ„ํฌ, ํ˜„์‹ค์—์„œ์˜ ํ™•๋ฅ  ๋ถ„ํฌ๋ผ๊ณ  ๋ช…์นญ ํ•ฉ์‹œ๋‹ค. ๊ธฐ๊ณ„์—๊ฒŒ ๋งŽ์€ ์ฝ”ํผ์Šค๋ฅผ ํ›ˆ๋ จ์‹œ์ผœ์„œ ์–ธ์–ด ๋ชจ๋ธ์„ ํ†ตํ•ด ํ˜„์‹ค์—์„œ์˜ ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ๊ทผ์‚ฌํ•˜๋Š” ๊ฒƒ์ด ์–ธ์–ด ๋ชจ๋ธ์˜ ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์นด์šดํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ ‘๊ทผํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค(corpus). ์ฆ‰, ๋‹ค์‹œ ๋งํ•ด ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ์ •๋ง ๋ฐฉ๋Œ€ํ•œ ์–‘์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. (is|An adorable little boy ) count(An adorable little boy is ) count(An adorable little boy ) ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์™€ ๊ฐ™์ด (is|An adorable little boy ) ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ์—์„œ ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จํ•œ ์ฝ”ํผ์Šค์— An adorable little boy is๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์—†์—ˆ๋‹ค๋ฉด ์ด ๋‹จ์–ด ์‹œํ€€์Šค์— ๋Œ€ํ•œ ํ™•๋ฅ ์€ 0์ด ๋ฉ๋‹ˆ๋‹ค. ๋˜๋Š” An adorable little boy๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์—†์—ˆ๋‹ค๋ฉด ๋ถ„๋ชจ๊ฐ€ 0์ด ๋˜์–ด ํ™•๋ฅ ์€ ์ •์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ฝ”ํผ์Šค์— ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์—†๋‹ค๊ณ  ํ•ด์„œ ์ด ํ™•๋ฅ ์„ 0 ๋˜๋Š” ์ •์˜๋˜์ง€ ์•Š๋Š” ํ™•๋ฅ ์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์ด ์ •ํ™•ํ•œ ๋ชจ๋ธ๋ง ๋ฐฉ๋ฒ•์ผ๊นŒ์š”? ์•„๋‹™๋‹ˆ๋‹ค. ํ˜„์‹ค์—์„  An adorable little boy is๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์กด์žฌํ•˜๊ณ  ๋˜ ๋ฌธ๋ฒ•์—๋„ ์ ํ•ฉํ•˜๋ฏ€๋กœ ์ •๋‹ต์ผ ๊ฐ€๋Šฅ์„ฑ ๋˜ํ•œ ๋†’์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์ถฉ๋ถ„ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€์ธกํ•˜์ง€ ๋ชปํ•˜์—ฌ ์–ธ์–ด๋ฅผ ์ •ํ™•ํžˆ ๋ชจ๋ธ๋ง ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํฌ์†Œ ๋ฌธ์ œ(sparsity problem)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๋ฌธ์ œ๋ฅผ ์™„ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ”๋กœ ์ด์–ด์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” n-gram ์–ธ์–ด ๋ชจ๋ธ์ด๋‚˜ ์ด ์ฑ…์—์„œ ๋‹ค๋ฃจ์ง€๋Š” ์•Š์ง€๋งŒ ์Šค๋ฌด๋”ฉ์ด๋‚˜ ๋ฐฑ์˜คํ”„์™€ ๊ฐ™์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ผ๋ฐ˜ํ™”(generalization) ๊ธฐ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํฌ์†Œ ๋ฌธ์ œ์— ๋Œ€ํ•œ ๊ทผ๋ณธ์ ์ธ ํ•ด๊ฒฐ์ฑ…์€ ๋˜์ง€ ๋ชปํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด๋Ÿฌํ•œ ํ•œ๊ณ„๋กœ ์ธํ•ด ์–ธ์–ด ๋ชจ๋ธ์˜ ํŠธ๋ Œ๋“œ๋Š” ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ์—์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 03-03 N-gram ์–ธ์–ด ๋ชจ๋ธ(N-gram Language Model) n-gram ์–ธ์–ด ๋ชจ๋ธ์€ ์—ฌ์ „ํžˆ ์นด์šดํŠธ์— ๊ธฐ๋ฐ˜ํ•œ ํ†ต๊ณ„์  ์ ‘๊ทผ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ SLM์˜ ์ผ์ข…์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์•ž์„œ ๋ฐฐ์šด ์–ธ์–ด ๋ชจ๋ธ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ด์ „์— ๋“ฑ์žฅํ•œ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ผ๋ถ€ ๋‹จ์–ด๋งŒ ๊ณ ๋ คํ•˜๋Š” ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋•Œ ์ผ๋ถ€ ๋‹จ์–ด๋ฅผ ๋ช‡ ๊ฐœ ๋ณด๋Š๋ƒ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š”๋ฐ ์ด๊ฒƒ์ด n-gram์—์„œ์˜ n์ด ๊ฐ€์ง€๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. 1. ์ฝ”ํผ์Šค์—์„œ ์นด์šดํŠธํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ์˜ ๊ฐ์†Œ. SLM์˜ ํ•œ๊ณ„๋Š” ํ›ˆ๋ จ ์ฝ”ํผ์Šค์— ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์žฅ์ด๋‚˜ ๋‹จ์–ด๊ฐ€ ์—†์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์žฅ์ด ๊ธธ์–ด์งˆ์ˆ˜๋ก ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ ๊ทธ ๋ฌธ์žฅ์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜๋ฉด ์นด์šดํŠธํ•  ์ˆ˜ ์—†์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐธ๊ณ ํ•˜๋Š” ๋‹จ์–ด๋“ค์„ ์ค„์ด๋ฉด ์นด์šดํŠธ๋ฅผ ํ•  ์ˆ˜ ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( is|An adorable little boy ) P ( is|boy ) ๊ฐ€๋ น, An adorable little boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์„ ๊ทธ๋ƒฅ boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ๋กœ ์ƒ๊ฐํ•ด ๋ณด๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”? ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์— An adorable little boy is๊ฐ€ ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ๋ณด๋‹ค๋Š” boy is๋ผ๋Š” ๋” ์งง์€ ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์กด์žฌํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋” ๋†’์Šต๋‹ˆ๋‹ค. ์กฐ๊ธˆ ์ง€๋‚˜์นœ ์ผ๋ฐ˜ํ™”๋กœ ๋Š๊ปด์ง„๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด little boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ๋„ ๋Œ€์•ˆ์ž…๋‹ˆ๋‹ค. ( is|An adorable little boy ) P ( is|little boy ) ์ฆ‰, ์•ž์—์„œ๋Š” An adorable little boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” An adorable little boy๊ฐ€ ๋‚˜์˜จ ํšŸ์ˆ˜์™€ An adorable little boy is๊ฐ€ ๋‚˜์˜จ ํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธํ•ด์•ผ๋งŒ ํ–ˆ์ง€๋งŒ, ์ด์ œ๋Š” ๋‹จ์–ด์˜ ํ™•๋ฅ ์„ ๊ตฌํ•˜๊ณ ์ž ๊ธฐ์ค€ ๋‹จ์–ด์˜ ์•ž ๋‹จ์–ด๋ฅผ ์ „๋ถ€ ํฌํ•จํ•ด์„œ ์นด์šดํŠธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์•ž ๋‹จ์–ด ์ค‘ ์ž„์˜์˜ ๊ฐœ์ˆ˜๋งŒ ํฌํ•จํ•ด์„œ ์นด์šดํŠธํ•˜์—ฌ ๊ทผ์‚ฌํ•˜์ž๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ ํ•ด๋‹น ๋‹จ์–ด์˜ ์‹œํ€€์Šค๋ฅผ ์นด์šดํŠธํ•  ํ™•๋ฅ ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค. 2. N-gram ์ด๋•Œ ์ž„์˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์ค€์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด n-gram์ž…๋‹ˆ๋‹ค. n-gram์€ n ๊ฐœ์˜ ์—ฐ์†์ ์ธ ๋‹จ์–ด ๋‚˜์—ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ n ๊ฐœ์˜ ๋‹จ์–ด ๋ญ‰์น˜ ๋‹จ์œ„๋กœ ๋Š์–ด์„œ ์ด๋ฅผ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋ฌธ์žฅ An adorable little boy is spreading smiles์ด ์žˆ์„ ๋•Œ, ๊ฐ n์— ๋Œ€ํ•ด์„œ n-gram์„ ์ „๋ถ€ ๊ตฌํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. unigrams : an, adorable, little, boy, is, spreading, smiles bigrams : an adorable, adorable little, little boy, boy is, is spreading, spreading smiles trigrams : an adorable little, adorable little boy, little boy is, boy is spreading, is spreading smiles 4-grams : an adorable little boy, adorable little boy is, little boy is spreading, boy is spreading smiles n-gram์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” n์ด 1์ผ ๋•Œ๋Š” ์œ ๋‹ˆ๊ทธ๋žจ(unigram), 2์ผ ๋•Œ๋Š” ๋ฐ”์ด ๊ทธ๋žจ(bigram), 3์ผ ๋•Œ๋Š” ํŠธ๋ผ์ด ๊ทธ๋žจ(trigram)์ด๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ณ  n์ด 4 ์ด์ƒ์ผ ๋•Œ๋Š” gram ์•ž์— ๊ทธ๋Œ€๋กœ ์ˆซ์ž๋ฅผ ๋ถ™์—ฌ์„œ ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ถœ์ฒ˜์— ๋”ฐ๋ผ์„œ๋Š” ์œ ๋‹ˆ๊ทธ๋žจ, ๋ฐ”์ด ๊ทธ๋žจ, ํŠธ๋ผ์ด ๊ทธ๋žจ ๋˜ํ•œ ๊ฐ๊ฐ 1-gram, 2-gram, 3-gram์ด๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. n-gram์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. n-gram์„ ํ†ตํ•œ ์–ธ์–ด ๋ชจ๋ธ์—์„œ๋Š” ๋‹ค์Œ์— ๋‚˜์˜ฌ ๋‹จ์–ด์˜ ์˜ˆ์ธก์€ ์˜ค์ง n-1๊ฐœ์˜ ๋‹จ์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 'An adorable little boy is spreading' ๋‹ค์Œ์— ๋‚˜์˜ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ํ•  ๋•Œ, n=4๋ผ๊ณ  ํ•œ 4-gram์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ, spreading ๋‹ค์Œ์— ์˜ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์€ n-1์— ํ•ด๋‹น๋˜๋Š” ์•ž์˜ 3๊ฐœ์˜ ๋‹จ์–ด๋งŒ์„ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. ( |boy is spreading ) count(boy is spreading w ) count(boy is spreading) ๋งŒ์•ฝ ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ boy is spreading๊ฐ€ 1,000๋ฒˆ ๋“ฑ์žฅํ–ˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  boy is spreading insults๊ฐ€ 500๋ฒˆ ๋“ฑ์žฅํ–ˆ์œผ๋ฉฐ, boy is spreading smiles๊ฐ€ 200๋ฒˆ ๋“ฑ์žฅํ–ˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด boy is spreading ๋‹ค์Œ์— insults๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ ์€ 50%์ด๋ฉฐ, smiles๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ ์€ 20%์ž…๋‹ˆ๋‹ค. ํ™•๋ฅ ์  ์„ ํƒ์— ๋”ฐ๋ผ ์šฐ๋ฆฌ๋Š” insults๊ฐ€ ๋” ๋งž๋Š”๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ( insults|boy is spreading ) 0.500 ( smiles|boy is spreading ) 0.200 3. N-gram Language Model์˜ ํ•œ๊ณ„ ์•ž์„œ 4-gram์„ ํ†ตํ•œ ์–ธ์–ด ๋ชจ๋ธ์˜ ๋™์ž‘ ๋ฐฉ์‹์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์กฐ๊ธˆ ์˜๋ฌธ์ด ๋‚จ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ณธ 4-gram ์–ธ์–ด ๋ชจ๋ธ์€ ์ฃผ์–ด์ง„ ๋ฌธ์žฅ์—์„œ ์•ž์— ์žˆ๋˜ ๋‹จ์–ด์ธ '์ž‘๊ณ  ์‚ฌ๋ž‘์Šค๋Ÿฌ์šด(an adorable little)'์ด๋ผ๋Š” ์ˆ˜์‹์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋ฐ˜์˜ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ '์ž‘๊ณ  ์‚ฌ๋ž‘์Šค๋Ÿฌ์šด' ์ˆ˜์‹์–ด๊นŒ์ง€ ๋ชจ๋‘ ๊ณ ๋ คํ•˜์—ฌ ์ž‘๊ณ  ์‚ฌ๋ž‘ํ•˜๋Š” ์†Œ๋…„์ด ํ•˜๋Š” ํ–‰๋™์— ๋Œ€ํ•ด ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ์ด์—ˆ๋‹ค๋ฉด ๊ณผ์—ฐ '์ž‘๊ณ  ์‚ฌ๋ž‘์Šค๋Ÿฌ์šด ์†Œ๋…„์ด' '๋ชจ์š•์„ ํผํŠธ๋ ธ๋‹ค'๋ผ๋Š” ๋ถ€์ •์ ์ธ ๋‚ด์šฉ์ด '์›ƒ์Œ ์ง€์—ˆ๋‹ค'๋ผ๋Š” ๊ธ์ •์ ์ธ ๋‚ด์šฉ ๋Œ€์‹  ์„ ํƒ๋˜์—ˆ์„๊นŒ์š”? ๋ฌผ๋ก  ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ฐ€์ •ํ•˜๋Š๋ƒ์˜ ๋‚˜๋ฆ„์ด๊ณ , ์ „ํ˜€ ๋ง์ด ์•ˆ ๋˜๋Š” ๋ฌธ์žฅ์€ ์•„๋‹ˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ ์ง€์ ํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์€ n-gram์€ ์•ž์˜ ๋‹จ์–ด ๋ช‡ ๊ฐœ๋งŒ ๋ณด๋‹ค ๋ณด๋‹ˆ ์˜๋„ํ•˜๊ณ  ์‹ถ์€ ๋Œ€๋กœ ๋ฌธ์žฅ์„ ๋๋งบ์Œํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋ฌธ์žฅ์„ ์ฝ๋‹ค ๋ณด๋ฉด ์•ž ๋ถ€๋ถ„๊ณผ ๋’ท๋ถ€๋ถ„์˜ ๋ฌธ๋งฅ์ด ์ „ํ˜€ ์—ฐ๊ฒฐ ์•ˆ ๋˜๋Š” ๊ฒฝ์šฐ๋„ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๋ก ๋งŒ ๋งํ•˜์ž๋ฉด, ์ „์ฒด ๋ฌธ์žฅ์„ ๊ณ ๋ คํ•œ ์–ธ์–ด ๋ชจ๋ธ๋ณด๋‹ค๋Š” ์ •ํ™•๋„๊ฐ€ ๋–จ์–ด์งˆ ์ˆ˜๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ† ๋Œ€๋กœ n-gram ๋ชจ๋ธ์— ๋Œ€ํ•œ ํ•œ๊ณ„์ ์„ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (1) ํฌ์†Œ ๋ฌธ์ œ(Sparsity Problem) ๋ฌธ์žฅ์— ์กด์žฌํ•˜๋Š” ์•ž์— ๋‚˜์˜จ ๋‹จ์–ด๋ฅผ ๋ชจ๋‘ ๋ณด๋Š” ๊ฒƒ๋ณด๋‹ค ์ผ๋ถ€ ๋‹จ์–ด๋งŒ์„ ๋ณด๋Š” ๊ฒƒ์œผ๋กœ ํ˜„์‹ค์ ์œผ๋กœ ์ฝ”ํผ์Šค์—์„œ ์นด์šดํŠธํ•  ์ˆ˜ ์žˆ๋Š” ํ™•๋ฅ ์„ ๋†’์ผ ์ˆ˜๋Š” ์žˆ์—ˆ์ง€๋งŒ, n-gram ์–ธ์–ด ๋ชจ๋ธ๋„ ์—ฌ์ „ํžˆ n-gram์— ๋Œ€ํ•œ ํฌ์†Œ ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. (2) n์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์€ trade-off ๋ฌธ์ œ. ์•ž์—์„œ ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๋ณผ์ง€ n์„ ์ •ํ•˜๋Š” ๊ฒƒ์€ trade-off๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ž„์˜์˜ ๊ฐœ์ˆ˜์ธ n์„ 1๋ณด๋‹ค๋Š” 2๋กœ ์„ ํƒํ•˜๋Š” ๊ฒƒ์€ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ์–ธ์–ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, spreading๋งŒ ๋ณด๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” is spreading์„ ๋ณด๊ณ  ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด ๋” ์ •ํ™•ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์ ˆํ•œ ๋ฐ์ดํ„ฐ์˜€๋‹ค๋ฉด ์–ธ์–ด ๋ชจ๋ธ์ด ์ ์–ด๋„ spreading ๋‹ค์Œ์— ๋™์‚ฌ๋ฅผ ๊ณ ๋ฅด์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. n์„ ํฌ๊ฒŒ ์„ ํƒํ•˜๋ฉด ์‹ค์ œ ํ›ˆ๋ จ ์ฝ”ํผ์Šค์—์„œ ํ•ด๋‹น n-gram์„ ์นด์šดํŠธํ•  ์ˆ˜ ์žˆ๋Š” ํ™•๋ฅ ์€ ์ ์–ด์ง€๋ฏ€๋กœ ํฌ์†Œ ๋ฌธ์ œ๋Š” ์ ์  ์‹ฌ๊ฐํ•ด์ง‘๋‹ˆ๋‹ค. ๋˜ํ•œ n์ด ์ปค์งˆ์ˆ˜๋ก ๋ชจ๋ธ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ปค์ง„๋‹ค๋Š” ๋ฌธ์ œ์ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฝ”ํผ์Šค์˜ ๋ชจ๋“  n-gram์— ๋Œ€ํ•ด์„œ ์นด์šดํŠธ๋ฅผ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. n์„ ์ž‘๊ฒŒ ์„ ํƒํ•˜๋ฉด ํ›ˆ๋ จ ์ฝ”ํผ์Šค์—์„œ ์นด์šดํŠธ๋Š” ์ž˜ ๋˜๊ฒ ์ง€๋งŒ ๊ทผ์‚ฌ์˜ ์ •ํ™•๋„๋Š” ํ˜„์‹ค์˜ ํ™•๋ฅ ๋ถ„ํฌ์™€ ๋ฉ€์–ด์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ ์ ˆํ•œ n์„ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ•œ trade-off ๋ฌธ์ œ๋กœ ์ธํ•ด ์ •ํ™•๋„๋ฅผ ๋†’์ด๋ ค๋ฉด n์€ ์ตœ๋Œ€ 5๋ฅผ ๋„˜๊ฒŒ ์žก์•„์„œ๋Š” ์•ˆ ๋œ๋‹ค๊ณ  ๊ถŒ์žฅ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. n์ด ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์œ ๋ช…ํ•œ ์˜ˆ์ œ ํ•˜๋‚˜๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™๊ต์˜ ๊ณต์œ  ์ž๋ฃŒ์— ๋”ฐ๋ฅด๋ฉด, ์›”์ŠคํŠธ๋ฆฌํŠธ ์ €๋„์—์„œ 3,800๋งŒ ๊ฐœ์˜ ๋‹จ์–ด ํ† ํฐ์— ๋Œ€ํ•˜์—ฌ n-gram ์–ธ์–ด ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ณ , 1,500๋งŒ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ–ˆ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ฑ๋Šฅ์ด ๋‚˜์™”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒ ์ง€๋งŒ, ํŽ„ ํ”Œ๋ ‰์„œํ‹ฐ(perplexity)๋Š” ์ˆ˜์น˜๊ฐ€ ๋‚ฎ์„์ˆ˜๋ก ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Unigram Bigram Trigram Perplexity 962 170 109 ์œ„์˜ ๊ฒฐ๊ณผ๋Š” n์„ 1์—์„œ 2, 2์—์„œ 3์œผ๋กœ ์˜ฌ๋ฆด ๋•Œ๋งˆ๋‹ค ์„ฑ๋Šฅ์ด ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 4. ์ ์šฉ ๋ถ„์•ผ(Domain)์— ๋งž๋Š” ์ฝ”ํผ์Šค์˜ ์ˆ˜์ง‘ ์–ด๋–ค ๋ถ„์•ผ์ธ์ง€, ์–ด๋–ค ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ธ์ง€์— ๋”ฐ๋ผ์„œ ํŠน์ • ๋‹จ์–ด๋“ค์˜ ํ™•๋ฅ  ๋ถ„ํฌ๋Š” ๋‹น์—ฐํžˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ฐ€๋ น, ๋งˆ์ผ€ํŒ… ๋ถ„์•ผ์—์„œ๋Š” ๋งˆ์ผ€ํŒ… ๋‹จ์–ด๊ฐ€ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋“ฑ์žฅํ•  ๊ฒƒ์ด๊ณ , ์˜๋ฃŒ ๋ถ„์•ผ์—์„œ๋Š” ์˜๋ฃŒ ๊ด€๋ จ ๋‹จ์–ด๊ฐ€ ๋‹น์—ฐํžˆ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์–ธ์–ด ๋ชจ๋ธ์— ์‚ฌ์šฉํ•˜๋Š” ์ฝ”ํผ์Šค๋ฅผ ํ•ด๋‹น ๋„๋ฉ”์ธ์˜ ์ฝ”ํผ์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋‹น์—ฐํžˆ ์–ธ์–ด ๋ชจ๋ธ์ด ์ œ๋Œ€๋กœ ๋œ ์–ธ์–ด ์ƒ์„ฑ์„ ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” ์ด๋ฅผ ์–ธ์–ด ๋ชจ๋ธ์˜ ์•ฝ์ ์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋Š”๋ฐ, ํ›ˆ๋ จ์— ์‚ฌ์šฉ๋œ ๋„๋ฉ”์ธ ์ฝ”ํผ์Šค๊ฐ€ ๋ฌด์—‡์ด๋ƒ์— ๋”ฐ๋ผ์„œ ์„ฑ๋Šฅ์ด ๋น„์•ฝ์ ์œผ๋กœ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 5. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ(Neural Network Based Language Model) ์—ฌ๊ธฐ์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š๊ฒ ์ง€๋งŒ, N-gram Language Model์˜ ํ•œ๊ณ„์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ๋ถ„๋ชจ, ๋ถ„์ž์— ์ˆซ์ž๋ฅผ ๋”ํ•ด์„œ ์นด์šดํŠธํ–ˆ์„ ๋•Œ 0์ด ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๋“ฑ์˜ ์—ฌ๋Ÿฌ ์ผ๋ฐ˜ํ™”(generalization) ๋ฐฉ๋ฒ•๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋Ÿผ์—๋„ ๋ณธ์งˆ์ ์œผ๋กœ n-gram ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ทจ์•ฝ์ ์„ ์™„์ „ํžˆ ํ•ด๊ฒฐํ•˜์ง€๋Š” ๋ชปํ•˜์˜€๊ณ , ์ด๋ฅผ ์œ„ํ•œ ๋Œ€์•ˆ์œผ๋กœ N-gram Language Model๋ณด๋‹ค ๋Œ€์ฒด์ ์œผ๋กœ ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์ด ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 03-04 ํ•œ๊ตญ์–ด์—์„œ์˜ ์–ธ์–ด ๋ชจ๋ธ(Language Model for Korean Sentences) ์˜์–ด๋‚˜ ๊ธฐํƒ€ ์–ธ์–ด์— ๋น„ํ•ด์„œ ํ•œ๊ตญ์–ด๋Š” ์–ธ์–ด ๋ชจ๋ธ๋กœ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ๊ฐ€ ํ›จ์”ฌ ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. 1. ํ•œ๊ตญ์–ด๋Š” ์–ด์ˆœ์ด ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค. ํ•œ๊ตญ์–ด์—์„œ๋Š” ์–ด์ˆœ์ด ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด์ „ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‹ค์Œ ๋‹จ์–ด๊ฐ€ ๋‚˜ํƒ€๋‚  ํ™•๋ฅ ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š”๋ฐ ์–ด์ˆœ์ด ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์€ ๋‹ค์Œ ๋‹จ์–ด๋กœ ์–ด๋–ค ๋‹จ์–ด๋“  ๋“ฑ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋‚˜๋Š” ์šด๋™์„ ํ•ฉ๋‹ˆ๋‹ค ์ฒด์œก๊ด€์—์„œ. 2. ๋‚˜๋Š” ์ฒด์œก๊ด€์—์„œ ์šด๋™์„ ํ•ฉ๋‹ˆ๋‹ค. 3. ์ฒด์œก๊ด€์—์„œ ์šด๋™์„ ํ•ฉ๋‹ˆ๋‹ค. 4. ๋‚˜๋Š” ์šด๋™์„ ์ฒด์œก๊ด€์—์„œ ํ•ฉ๋‹ˆ๋‹ค. 4๊ฐœ์˜ ๋ฌธ์žฅ์€ ์ „๋ถ€ ์˜๋ฏธ๊ฐ€ ํ†ตํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด '๋‚˜๋Š”'์ด๋ผ๋Š” ์ฃผ์–ด๋ฅผ ์ƒ๋žตํ•ด๋„ ๋ง์ด ๋ผ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‹จ์–ด ์ˆœ์„œ๋ฅผ ๋’ค์ฃฝ๋ฐ•์ฃฝ์œผ๋กœ ๋ฐ”๊พธ์–ด๋†”๋„ ํ•œ๊ตญ์–ด๋Š” ์˜๋ฏธ๊ฐ€ ์ „๋‹ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ™•๋ฅ ์— ๊ธฐ๋ฐ˜ํ•œ ์–ธ์–ด ๋ชจ๋ธ์ด ์ œ๋Œ€๋กœ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. 2. ํ•œ๊ตญ์–ด๋Š” ๊ต์ฐฉ์–ด์ด๋‹ค. ํ•œ๊ตญ์–ด๋Š” ๊ต์ฐฉ์–ด์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ•œ๊ตญ์–ด์—์„œ์˜ ์–ธ์–ด ๋ชจ๋ธ ์ž‘๋™์„ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„์ธ ์–ด์ ˆ ๋‹จ์œ„๋กœ ํ† ํฐํ™”๋ฅผ ํ•  ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์žฅ์—์„œ ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•œ ๋‹จ์–ด์˜ ์ˆ˜๊ฐ€ ๊ต‰์žฅํžˆ ๋Š˜์–ด๋‚ฉ๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋กœ ๊ต์ฐฉ์–ด์ธ ํ•œ๊ตญ์–ด์—๋Š” ์กฐ์‚ฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์กฐ์‚ฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•œ๊ตญ์–ด์—๋Š” ์–ด๋–ค ํ–‰๋™์„ ํ•˜๋Š” ๋™์‚ฌ์˜ ์ฃผ์–ด๋‚˜ ๋ชฉ์ ์–ด๋ฅผ ์œ„ํ•ด์„œ ์กฐ์‚ฌ๋ผ๋Š” ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น '๊ทธ๋…€'๋ผ๋Š” ๋‹จ์–ด ํ•˜๋‚˜๋งŒ ํ•ด๋„ ๊ทธ๋…€๊ฐ€, ๊ทธ๋…€๋ฅผ, ๊ทธ๋…€์˜, ๊ทธ๋…€์™€, ๊ทธ๋…€๋กœ, ๊ทธ๋…€๊ป˜์„œ, ๊ทธ๋…€์ฒ˜๋Ÿผ ๋“ฑ๊ณผ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ๊ตญ์–ด์—์„œ๋Š” ํ† ํฐํ™”๋ฅผ ํ†ตํ•ด ์ ‘์‚ฌ๋‚˜ ์กฐ์‚ฌ ๋“ฑ์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ์ค‘์š”ํ•œ ์ž‘์—…์ด ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 3. ํ•œ๊ตญ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ œ๋Œ€๋กœ ์ง€์ผœ์ง€์ง€ ์•Š๋Š”๋‹ค. ํ•œ๊ตญ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ œ๋Œ€๋กœ ํ•˜์ง€ ์•Š์•„๋„ ์˜๋ฏธ๊ฐ€ ์ „๋‹ฌ๋˜๋ฉฐ, ๋„์–ด์“ฐ๊ธฐ ๊ทœ์น™ ๋˜ํ•œ ์ƒ๋Œ€์ ์œผ๋กœ ๊นŒ๋‹ค๋กœ์šด ์–ธ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์— ์žˆ์–ด์„œ ํ•œ๊ตญ์–ด ์ฝ”ํผ์Šค๋Š” ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ œ๋Œ€๋กœ ์ง€์ผœ์ง€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ํ† ํฐ์ด ์ œ๋Œ€๋กœ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š๋Š” ์ฑ„ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉ๋œ๋‹ค๋ฉด ์–ธ์–ด ๋ชจ๋ธ์€ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 03-05 ํŽ„ ํ”Œ๋ ‰์„œํ‹ฐ(Perplexity, PPL) ๋‘ ๊ฐœ์˜ ๋ชจ๋ธ A, B๊ฐ€ ์žˆ์„ ๋•Œ ์ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์€ ์–ด๋–ป๊ฒŒ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ๋‘ ๊ฐœ์˜ ๋ชจ๋ธ์„ ์˜คํƒ€ ๊ต์ •, ๊ธฐ๊ณ„ ๋ฒˆ์—ญ ๋“ฑ์˜ ํ‰๊ฐ€์— ํˆฌ์ž…ํ•ด ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ชจ๋ธ์ด ํ•ด๋‹น ์—…๋ฌด์˜ ์„ฑ๋Šฅ์„ ๋ˆ„๊ฐ€ ๋” ์ž˜ํ–ˆ๋Š”์ง€๋ฅผ ๋น„๊ตํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‘ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋น„๊ตํ•˜๊ณ ์ž, ์ผ์ผ์ด ๋ชจ๋ธ๋“ค์— ๋Œ€ํ•ด์„œ ์‹ค์ œ ์ž‘์—…์„ ์‹œ์ผœ๋ณด๊ณ  ์ •ํ™•๋„๋ฅผ ๋น„๊ตํ•˜๋Š” ์ž‘์—…์€ ๊ณต์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์ด ๋“œ๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋น„๊ตํ•ด์•ผ ํ•˜๋Š” ๋ชจ๋ธ์ด ๋‘ ๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ ๊ทธ ์ด์ƒ์˜ ์ˆ˜๋ผ๋ฉด ์‹œ๊ฐ„์€ ๋น„๊ตํ•ด์•ผ ํ•˜๋Š” ๋ชจ๋ธ์˜ ์ˆ˜๋งŒํผ ๋ฐฐ๋กœ ๋Š˜์–ด๋‚  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ‰๊ฐ€๋ณด๋‹ค๋Š” ์–ด์ฉŒ๋ฉด ์กฐ๊ธˆ์€ ๋ถ€์ •ํ™•ํ•  ์ˆ˜๋Š” ์žˆ์–ด๋„ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋น ๋ฅด๊ฒŒ ์‹์œผ๋กœ ๊ณ„์‚ฐ๋˜๋Š” ๋” ๊ฐ„๋‹จํ•œ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๋ชจ๋ธ ๋‚ด์—์„œ ์ž์‹ ์˜ ์„ฑ๋Šฅ์„ ์ˆ˜์น˜ํ™”ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋†“๋Š” ํŽ„ ํ”Œ๋ ‰์„œํ‹ฐ(perplexity)์ž…๋‹ˆ๋‹ค. 1. ์–ธ์–ด ๋ชจ๋ธ์˜ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•(Evaluation metric) : PPL ํŽ„ ํ”Œ๋ ‰์„œํ‹ฐ(perplexity)๋Š” ์–ธ์–ด ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ํ‰๊ฐ€ ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต ์ค„์—ฌ์„œ PPL์ด๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์™œ perplexity๋ผ๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ–ˆ์„๊นŒ์š”? ์˜์–ด์—์„œ 'perplexed'๋Š” 'ํ—ท๊ฐˆ๋ฆฌ๋Š”'๊ณผ ์œ ์‚ฌํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์—ฌ๊ธฐ์„œ PPL์€ 'ํ—ท๊ฐˆ๋ฆฌ๋Š” ์ •๋„'๋กœ ์ดํ•ดํ•ฉ์‹œ๋‹ค. PPL๋ฅผ ์ฒ˜์Œ ๋ฐฐ์šธ ๋•Œ ๋‹ค์†Œ ๋‚ฏ์„ค๊ฒŒ ๋Š๊ปด์งˆ ์ˆ˜ ์žˆ๋Š” ์ ์ด ์žˆ๋‹ค๋ฉด, PPL์€ ์ˆ˜์น˜๊ฐ€ ๋†’์œผ๋ฉด ์ข‹์€ ์„ฑ๋Šฅ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, '๋‚ฎ์„์ˆ˜๋ก' ์–ธ์–ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. PPL์€ ๋ฌธ์žฅ์˜ ๊ธธ์ด๋กœ ์ •๊ทœํ™”๋œ ๋ฌธ์žฅ ํ™•๋ฅ ์˜ ์—ญ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ ์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ์˜ PPL์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. P ( ) P ( 1 w, 3. . w) 1 = P ( 1 w, 3. . N ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์— ์ฒด์ธ ๋ฃฐ(chain rule)์„ ์ ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. P ( ) 1 ( 1 w, 3. . w) = โˆ = N ( i w, 2 ์—ฌ๊ธฐ์— n-gram์„ ์ ์šฉํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด bigram ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ์—๋Š” ์‹์ด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. P ( ) 1 i 1 P ( i w โˆ’ ) 2. ๋ถ„๊ธฐ ๊ณ„์ˆ˜(Branching factor) PPL์€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ถ„๊ธฐ ๊ณ„์ˆ˜(branching factor)์ž…๋‹ˆ๋‹ค. PPL์€ ์ด ์–ธ์–ด ๋ชจ๋ธ์ด ํŠน์ • ์‹œ์ ์—์„œ ํ‰๊ท ์ ์œผ๋กœ ๋ช‡ ๊ฐœ์˜ ์„ ํƒ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์–ธ์–ด ๋ชจ๋ธ์— ์–ด๋–ค ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์„ ์ฃผ๊ณ  ์ธก์ •ํ–ˆ๋”๋‹ˆ PPL์ด 10์ด ๋‚˜์™”๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ•ด๋‹น ์–ธ์–ด ๋ชจ๋ธ์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋“  ์‹œ์ (time step)๋งˆ๋‹ค ํ‰๊ท  10๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ๊ฒƒ์ด ์ •๋‹ต์ธ์ง€ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋‘ ์–ธ์–ด ๋ชจ๋ธ์˜ PPL์„ ๊ฐ๊ฐ ๊ณ„์‚ฐ ํ›„์— PPL์˜ ๊ฐ’์„ ๋น„๊ตํ•˜๋ฉด, ๋‘ ์–ธ์–ด ๋ชจ๋ธ ์ค‘ PPL์ด ๋” ๋‚ฎ์€ ์–ธ์–ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ๋” ์ข‹๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. P ( ) P ( 1 w, 3. . w) 1 = ( 10 ) 1 = 10 1 10 ๋‹จ, ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์— ์žˆ์–ด์„œ ์ฃผ์˜ํ•  ์ ์€ PPL์˜ ๊ฐ’์ด ๋‚ฎ๋‹ค๋Š” ๊ฒƒ์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์ƒ์—์„œ ๋†’์€ ์ •ํ™•๋„๋ฅผ ๋ณด์ธ๋‹ค๋Š” ๊ฒƒ์ด์ง€, ์‚ฌ๋žŒ์ด ์ง์ ‘ ๋Š๋ผ๊ธฐ์— ์ข‹์€ ์–ธ์–ด ๋ชจ๋ธ์ด๋ผ๋Š” ๊ฒƒ์„ ๋ฐ˜๋“œ์‹œ ์˜๋ฏธํ•˜์ง„ ์•Š๋Š”๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์–ธ์–ด ๋ชจ๋ธ์˜ PPL์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์˜์กดํ•˜๋ฏ€๋กœ ๋‘ ๊ฐœ ์ด์ƒ์˜ ์–ธ์–ด ๋ชจ๋ธ์„ ๋น„๊ตํ•  ๋•Œ๋Š” ์ •๋Ÿ‰์ ์œผ๋กœ ์–‘์ด ๋งŽ๊ณ , ๋˜ํ•œ ๋„๋ฉ”์ธ์— ์•Œ๋งž์€ ๋™์ผํ•œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ์‹ ๋ขฐ๋„๊ฐ€ ๋†’๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 3. ๊ธฐ์กด ์–ธ์–ด ๋ชจ๋ธ Vs. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ. ํŽ˜์ด์Šค๋ถ AI ์—ฐ๊ตฌํŒ€์€ ์•ž์„œ ๋ฐฐ์šด n-gram ์–ธ์–ด ๋ชจ๋ธ๊ณผ ์ดํ›„ ๋ฐฐ์šฐ๊ฒŒ ๋  ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ PPL๋กœ ์„ฑ๋Šฅ ํ…Œ์ŠคํŠธ๋ฅผ ํ•œ ํ‘œ๋ฅผ ๊ณต๊ฐœํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://engineering.fb.com/2016/10/25/ml-applications/building-an-efficient-neural-language-model-over-a-billion-words/ ํ‘œ์—์„œ ๋งจ ์œ„์˜ ์ค„์˜ ์–ธ์–ด ๋ชจ๋ธ์ด n-gram์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์ด๋ฉฐ PPL์ด 67.6์œผ๋กœ ์ธก์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 5-gram์„ ์‚ฌ์šฉํ•˜์˜€์œผ๋ฉฐ, 5-gram ์•ž์— Interpolated Kneser-Ney๋ผ๋Š” ์ด๋ฆ„์ด ๋ถ™์—ˆ๋Š”๋ฐ ์ด ์ฑ…์—์„œ๋Š” ๋ณ„๋„ ์„ค๋ช…์„ ์ƒ๋žตํ•˜๊ฒ ๋‹ค๊ณ  ํ–ˆ๋˜ ์ผ๋ฐ˜ํ™”(generalization) ๋ฐฉ๋ฒ•์ด ์‚ฌ์šฉ๋œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๊ทธ ์•„๋ž˜์˜ ๋ชจ๋ธ๋“ค์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ๋“ค๋กœ ํŽ˜์ด์Šค๋ถ AI ์—ฐ๊ตฌํŒ€์ด ์ž์‹ ๋“ค์˜ ์–ธ์–ด ๋ชจ๋ธ์„ ๋‹ค๋ฅธ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ๊ธฐ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•„์ง RNN๊ณผ LSTM ๋“ฑ์ด ๋ฌด์—‡์ธ์ง€ ๋ฐฐ์šฐ์ง€๋Š” ์•Š์•˜์ง€๋งŒ, ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ๋“ค์€ ๋Œ€๋ถ€๋ถ„ n-gram์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ๋ณด๋‹ค ๋” ์ข‹์€ ์„ฑ๋Šฅ ํ‰๊ฐ€๋ฅผ ๋ฐ›์•˜์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 03-06 ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ (Conditional Probability) ์ด ์ฑ•ํ„ฐ๋Š” ์–ธ์–ด ๋ชจ๋ธ(Language Model)๊ณผ n-gram์˜ ๊ทผ๊ฐ„์ด ๋˜๋Š” ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์˜ ์ดํ•ด๋ฅผ ์œ„ํ•ด ์ž‘์„ฑํ•˜๋Š” ๋ถ€๋ก ๊ฐœ๋…์˜ ์˜ˆ์ œ ์ฑ•ํ„ฐ์ž…๋‹ˆ๋‹ค. A = ํ•™์ƒ์ด ๋‚จํ•™์ƒ์ธ ์‚ฌ๊ฑด B = ํ•™์ƒ์ด ์—ฌํ•™์ƒ์ธ ์‚ฌ๊ฑด C = ํ•™์ƒ์ด ์ค‘ํ•™์ƒ์ธ ์‚ฌ๊ฑด D = ํ•™์ƒ์ด ๊ณ ๋“ฑํ•™์ƒ์ธ ์‚ฌ๊ฑด ์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ์•„๋ž˜์˜ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํ•™์ƒ์„ ๋ฝ‘์•˜์„ ๋•Œ, ๋‚จํ•™์ƒ์ผ ํ™•๋ฅ  ( ) =180/360=0.5 2. ํ•™์ƒ์„ ๋ฝ‘์•˜์„ ๋•Œ, ๊ณ ๋“ฑํ•™์ƒ์ด๋ฉด์„œ ๋‚จํ•™์ƒ์ผ ํ™•๋ฅ  ( โˆฉ ) = 80/360 3. ๊ณ ๋“ฑํ•™์ƒ ์ค‘ ํ•œ ๋ช…์„ ๋ฝ‘์•˜์„ ๋•Œ, ๋‚จํ•™์ƒ์ผ ํ™•๋ฅ  ( | ) = 80/200 = ( โˆฉ ) P ( ) = (80/360)/(200/360) = 80/200 = 0.4 04. ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ ๋‹จ์–ด ํ‘œํ˜„(Count based word Representation) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํ…์ŠคํŠธ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ทธ์ค‘ ์ •๋ณด ๊ฒ€์ƒ‰๊ณผ ํ…์ŠคํŠธ ๋งˆ์ด๋‹ ๋ถ„์•ผ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ ํ…์ŠคํŠธ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ธ DTM(Document Term Matrix)๊ณผ TF-IDF(Term Frequency-Inverse Document Frequency)์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋ฅผ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์น˜ํ™”๋ฅผ ํ•˜๊ณ  ๋‚˜๋ฉด, ํ†ต๊ณ„์ ์ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ๋ฌธ์„œ๋กœ ์ด๋ฃจ์–ด์ง„ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์„ ๋•Œ ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ํŠน์ • ๋ฌธ์„œ ๋‚ด์—์„œ ์–ผ๋งˆ๋‚˜ ์ค‘์š”ํ•œ ๊ฒƒ์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๊ฑฐ๋‚˜, ๋ฌธ์„œ์˜ ํ•ต์‹ฌ์–ด ์ถ”์ถœ, ๊ฒ€์ƒ‰ ์—”์ง„์—์„œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์˜ ์ˆœ์œ„ ๊ฒฐ์ •, ๋ฌธ์„œ๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋“ฑ์˜ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 04-01 ๋‹ค์–‘ํ•œ ๋‹จ์–ด์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ• ์—ฌ๊ธฐ์„œ๋Š” ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ ๋‹จ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ• ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ๋‹จ์–ด์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์—๋Š” ์–ด๋–ค ๊ฒƒ์ด ์žˆ์œผ๋ฉฐ, ์•ž์œผ๋กœ ์ด ์ฑ…์—์„œ๋Š” ์–ด๋–ค ์ˆœ์„œ๋กœ ๋‹จ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•˜๊ฒŒ ๋  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 1. ๋‹จ์–ด์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ• ๋‹จ์–ด์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ ๊ตญ์†Œ ํ‘œํ˜„(Local Representation) ๋ฐฉ๋ฒ•๊ณผ ๋ถ„์‚ฐ ํ‘œํ˜„(Distributed Representation) ๋ฐฉ๋ฒ•์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ๊ตญ์†Œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น ๋‹จ์–ด ๊ทธ ์ž์ฒด๋งŒ ๋ณด๊ณ , ํŠน์ • ๊ฐ’์„ ๋งคํ•‘ํ•˜์—ฌ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ฉฐ, ๋ถ„์‚ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ๊ทธ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ณ ์ž ์ฃผ๋ณ€์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด puppy(๊ฐ•์•„์ง€), cute(๊ท€์—ฌ์šด), lovely(์‚ฌ๋ž‘์Šค๋Ÿฌ์šด)๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์„ ๋•Œ ๊ฐ ๋‹จ์–ด์— 1๋ฒˆ, 2๋ฒˆ, 3๋ฒˆ ๋“ฑ๊ณผ ๊ฐ™์€ ์ˆซ์ž๋ฅผ ๋งคํ•‘(mapping) ํ•˜์—ฌ ๋ถ€์—ฌํ•œ๋‹ค๋ฉด ์ด๋Š” ๊ตญ์†Œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๋ถ„์‚ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์˜ ์˜ˆ๋ฅผ ํ•˜๋‚˜ ๋“ค์–ด๋ณด๋ฉด ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์ฐธ๊ณ ํ•ฉ๋‹ˆ๋‹ค. puppy(๊ฐ•์•„์ง€)๋ผ๋Š” ๋‹จ์–ด ๊ทผ์ฒ˜์—๋Š” ์ฃผ๋กœ cute(๊ท€์—ฌ์šด), lovely(์‚ฌ๋ž‘์Šค๋Ÿฌ์šด)์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ, puppy๋ผ๋Š” ๋‹จ์–ด๋Š” cute, lovely ํ•œ ๋Š๋‚Œ์ด ๋‹ค๋กœ ๋‹จ์–ด๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ด ๋‘ ๋ฐฉ๋ฒ•์˜ ์ฐจ์ด๋Š” ๊ตญ์†Œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ๋‹จ์–ด์˜ ์˜๋ฏธ, ๋‰˜์•™์Šค๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†์ง€๋งŒ, ๋ถ„์‚ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ๋‹จ์–ด์˜ ๋‰˜์•™์Šค๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋น„์Šทํ•œ ์˜๋ฏธ๋กœ ๊ตญ์†Œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•(Local Representation)์„ ์ด์‚ฐ ํ‘œํ˜„(Discrete Representation)์ด๋ผ๊ณ ๋„ ํ•˜๋ฉฐ, ๋ถ„์‚ฐ ํ‘œํ˜„(Distributed Representation)์„ ์—ฐ์† ํ‘œํ˜„(Continuous Represnetation)์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ์˜๊ฒฌ์œผ๋กœ ๊ตฌ๊ธ€์˜ ์—ฐ๊ตฌ์› ํ† ๋งˆ์Šค ๋ฏธ์ฝ” ๋กœ๋ธŒ(Tomas Mikolov)๋Š” 2016๋…„์— ํ•œ ๋ฐœํ‘œ์—์„œ ์ž ์žฌ ์˜๋ฏธ ๋ถ„์„(LSA)์ด๋‚˜ ์ž ์žฌ ๋””๋ฆฌํด๋ ˆ ํ• ๋‹น(LDA)๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•๋“ค์€ ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ์—ฐ์† ํ‘œํ˜„(Continuous Represnetation)์ด์ง€๋งŒ, ์—„๋ฐ€ํžˆ ๋งํ•ด์„œ ๋‹ค๋ฅธ ์ ‘๊ทผ์˜ ๋ฐฉ๋ฒ•๋ก ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์›Œ๋“œํˆฌ๋ฒกํ„ฐ(Word2vec)์™€ ๊ฐ™์€ ๋ถ„์‚ฐ ํ‘œํ˜„(Distributed Representation)์€ ์•„๋‹Œ ๊ฒƒ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์—ฐ์† ํ‘œํ˜„์„ ๋ถ„์‚ฐ ํ‘œํ˜„์„ ํฌ๊ด„ํ•˜๊ณ  ์žˆ๋Š” ๋” ํฐ ๊ฐœ๋…์œผ๋กœ ์„ค๋ช…ํ•˜๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค. 2. ๋‹จ์–ด ํ‘œํ˜„์˜ ์นดํ…Œ๊ณ ๋ฆฌํ™” ์ด ์ฑ…์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด ํ‘œํ˜„์„ ์นดํ…Œ๊ณ ๋ฆฌํ™”ํ•˜์—ฌ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์˜ Bag of Words๋Š” ๊ตญ์†Œ ํ‘œํ˜„์—(Local Representation)์— ์†ํ•˜๋ฉฐ, ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์นด์šดํŠธ(Count) ํ•˜์—ฌ ๋‹จ์–ด๋ฅผ ์ˆ˜์น˜ํ™”ํ•˜๋Š” ๋‹จ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด ์ฑ•ํ„ฐ์—์„œ๋Š” BoW์™€ ๊ทธ์˜ ํ™•์žฅ์ธ DTM(๋˜๋Š” TDM)์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜๊ณ , ์ด๋Ÿฌํ•œ ๋นˆ๋„์ˆ˜ ๊ธฐ๋ฐ˜ ๋‹จ์–ด ํ‘œํ˜„์— ๋‹จ์–ด์˜ ์ค‘์š”๋„์— ๋”ฐ๋ฅธ ๊ฐ€์ค‘์น˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋Š” TF-IDF์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ฑ•ํ„ฐ์—์„œ๋Š” ์—ฐ์† ํ‘œํ˜„(Continuous Representation)์— ์†ํ•˜๋ฉด์„œ, ์˜ˆ์ธก(prediction)์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹จ์–ด์˜ ๋‰˜์•™์Šค๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์›Œ๋“œํˆฌ๋ฒกํ„ฐ(Word2Vec)์™€ ๊ทธ์˜ ํ™•์žฅ์ธ ํŒจ์ŠคํŠธ ํ…์ŠคํŠธ(FastText)๋ฅผ ํ•™์Šตํ•˜๊ณ , ์˜ˆ์ธก๊ณผ ์นด์šดํŠธ๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ๋ชจ๋‘ ์‚ฌ์šฉ๋œ ๊ธ€๋กœ๋ธŒ(GloVe)์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 04-02 Bag of Words(BoW) ๋‹จ์–ด์˜ ๋“ฑ์žฅ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š” ๋นˆ๋„์ˆ˜ ๊ธฐ๋ฐ˜์˜ ๋‹จ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ธ Bag of Words์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. Bag of Words๋ž€? Bag of Words๋ž€ ๋‹จ์–ด๋“ค์˜ ์ˆœ์„œ๋Š” ์ „ํ˜€ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ , ๋‹จ์–ด๋“ค์˜ ์ถœํ˜„ ๋นˆ๋„(frequency)์—๋งŒ ์ง‘์ค‘ํ•˜๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์น˜ํ™” ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. Bag of Words๋ฅผ ์ง์—ญํ•˜๋ฉด ๋‹จ์–ด๋“ค์˜ ๊ฐ€๋ฐฉ์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด๋“ค์ด ๋“ค์–ด์žˆ๋Š” ๊ฐ€๋ฐฉ์„ ์ƒ์ƒํ•ด ๋ด…์‹œ๋‹ค. ๊ฐ–๊ณ  ์žˆ๋Š” ์–ด๋–ค ํ…์ŠคํŠธ ๋ฌธ์„œ์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ๊ฐ€๋ฐฉ์—๋‹ค๊ฐ€ ์ „๋ถ€ ๋„ฃ์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„์—๋Š” ์ด ๊ฐ€๋ฐฉ์„ ํ”๋“ค์–ด ๋‹จ์–ด๋“ค์„ ์„ž์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ํ•ด๋‹น ๋ฌธ์„œ ๋‚ด์—์„œ ํŠน์ • ๋‹จ์–ด๊ฐ€ N ๋ฒˆ ๋“ฑ์žฅํ–ˆ๋‹ค๋ฉด, ์ด ๊ฐ€๋ฐฉ์—๋Š” ๊ทธ ํŠน์ • ๋‹จ์–ด๊ฐ€ N ๊ฐœ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฐ€๋ฐฉ์„ ํ”๋“ค์–ด์„œ ๋‹จ์–ด๋ฅผ ์„ž์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋” ์ด์ƒ ๋‹จ์–ด์˜ ์ˆœ์„œ๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. BoW๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ์ด๋ ‡๊ฒŒ ๋‘ ๊ฐ€์ง€ ๊ณผ์ •์œผ๋กœ ์ƒ๊ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (1) ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. # ๋‹จ์–ด ์ง‘ํ•ฉ ์ƒ์„ฑ. (2) ๊ฐ ์ธ๋ฑ์Šค์˜ ์œ„์น˜์— ๋‹จ์–ด ํ† ํฐ์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ๊ธฐ๋กํ•œ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ BoW์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 1 : ์ •๋ถ€๊ฐ€ ๋ฐœํ‘œํ•˜๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ ์†Œ๋น„์ž๊ฐ€ ๋Š๋ผ๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ ๋‹ค๋ฅด๋‹ค. ๋ฌธ์„œ 1์— ๋Œ€ํ•ด์„œ BoW๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๋œ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocaburary)์„ ๋งŒ๋“ค์–ด ๊ฐ ๋‹จ์–ด์— ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ํ• ๋‹นํ•˜๊ณ , BoW๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. from konlpy.tag import Okt okt = Okt() def build_bag_of_words(document): # ์˜จ์  ์ œ๊ฑฐ ๋ฐ ํ˜•ํƒœ์†Œ ๋ถ„์„ document = document.replace('.', '') tokenized_document = okt.morphs(document) word_to_index = {} bow = [] for word in tokenized_document: if word not in word_to_index.keys(): word_to_index[word] = len(word_to_index) # BoW์— ์ „๋ถ€ ๊ธฐ๋ณธ๊ฐ’ 1์„ ๋„ฃ๋Š”๋‹ค. bow.insert(len(word_to_index) - 1, 1) else: # ์žฌ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค index = word_to_index.get(word) # ์žฌ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋Š” ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค์˜ ์œ„์น˜์— 1์„ ๋”ํ•œ๋‹ค. bow[index] = bow[index] + 1 return word_to_index, bow ํ•ด๋‹น ํ•จ์ˆ˜์— ๋ฌธ์„œ 1์„ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด๋ด…์‹œ๋‹ค. doc1 = "์ •๋ถ€๊ฐ€ ๋ฐœํ‘œํ•˜๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ ์†Œ๋น„์ž๊ฐ€ ๋Š๋ผ๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ ๋‹ค๋ฅด๋‹ค." vocab, bow = build_bag_of_words(doc1) print('vocabulary :', vocab) print('bag of words vector :', bow) vocabulary : {'์ •๋ถ€': 0, '๊ฐ€': 1, '๋ฐœํ‘œ': 2, 'ํ•˜๋Š”': 3, '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ': 4, '๊ณผ': 5, '์†Œ๋น„์ž': 6, '๋Š๋ผ๋Š”': 7, '์€': 8, '๋‹ค๋ฅด๋‹ค': 9} bag of words vector : [1, 2, 1, 1, 2, 1, 1, 1, 1, 1] ๋ฌธ์„œ 1์— ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•œ ๊ฒฐ๊ณผ๋Š” ์ฒซ ๋ฒˆ์งธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ๋ฌธ์„œ 1์˜ BoW๋Š” ๋‘ ๋ฒˆ์งธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, ์ธ๋ฑ์Šค 4์— ํ•ด๋‹นํ•˜๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ ๋‘ ๋ฒˆ ์–ธ๊ธ‰๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค 4์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด 2์ž…๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘๋จ์— ์ฃผ์˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ BoW์—์„œ ๋‹ค์„ฏ ๋ฒˆ์งธ ๊ฐ’์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ํ•œ๊ตญ์–ด์—์„œ ๋ถˆ์šฉ์–ด์— ํ•ด๋‹น๋˜๋Š” ์กฐ์‚ฌ๋“ค ๋˜ํ•œ ์ œ๊ฑฐํ•œ๋‹ค๋ฉด ๋” ์ •์ œ๋œ BoW๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 2. Bag of Words์˜ ๋‹ค๋ฅธ ์˜ˆ์ œ๋“ค ๋ฌธ์„œ 2 : ์†Œ๋น„์ž๋Š” ์ฃผ๋กœ ์†Œ๋น„ํ•˜๋Š” ์ƒํ’ˆ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์„ ๋Š๋‚€๋‹ค. ์œ„์˜ ํ•จ์ˆ˜์— ์ž„์˜์˜ ๋ฌธ์„œ 2๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. doc2 = '์†Œ๋น„์ž๋Š” ์ฃผ๋กœ ์†Œ๋น„ํ•˜๋Š” ์ƒํ’ˆ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์„ ๋Š๋‚€๋‹ค.' vocab, bow = build_bag_of_words(doc2) print('vocabulary :', vocab) print('bag of words vector :', bow) vocabulary : {'์†Œ๋น„์ž': 0, '๋Š”': 1, '์ฃผ๋กœ': 2, '์†Œ๋น„': 3, 'ํ•˜๋Š”': 4, '์ƒํ’ˆ': 5, '์„': 6, '๊ธฐ์ค€': 7, '์œผ๋กœ': 8, '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ': 9, '๋Š๋‚€๋‹ค': 10} bag of words vector : [1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1] ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2๋ฅผ ํ•ฉ์ณ์„œ ๋ฌธ์„œ 3์ด๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ณ , BoW๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 3: ์ •๋ถ€๊ฐ€ ๋ฐœํ‘œํ•˜๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ ์†Œ๋น„์ž๊ฐ€ ๋Š๋ผ๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ ๋‹ค๋ฅด๋‹ค. ์†Œ๋น„์ž๋Š” ์ฃผ๋กœ ์†Œ๋น„ํ•˜๋Š” ์ƒํ’ˆ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์„ ๋Š๋‚€๋‹ค. doc3 = doc1 + ' ' + doc2 vocab, bow = build_bag_of_words(doc3) print('vocabulary :', vocab) print('bag of words vector :', bow) vocabulary : {'์ •๋ถ€': 0, '๊ฐ€': 1, '๋ฐœํ‘œ': 2, 'ํ•˜๋Š”': 3, '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ': 4, '๊ณผ': 5, '์†Œ๋น„์ž': 6, '๋Š๋ผ๋Š”': 7, '์€': 8, '๋‹ค๋ฅด๋‹ค': 9, '๋Š”': 10, '์ฃผ๋กœ': 11, '์†Œ๋น„': 12, '์ƒํ’ˆ': 13, '์„': 14, '๊ธฐ์ค€': 15, '์œผ๋กœ': 16, '๋Š๋‚€๋‹ค': 17} bag of words vector : [1, 2, 1, 2, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1] ๋ฌธ์„œ 3์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์€ ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ๋‹จ์–ด๋“ค์„ ๋ชจ๋‘ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BoW๋Š” ์ข…์ข… ์—ฌ๋Ÿฌ ๋ฌธ์„œ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ํ•ฉ์นœ ๋’ค์—, ํ•ด๋‹น ๋‹จ์–ด ์ง‘ํ•ฉ์— ๋Œ€ํ•œ ๊ฐ ๋ฌธ์„œ์˜ BoW๋ฅผ ๊ตฌํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ๋ฌธ์„œ 3์— ๋Œ€ํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์„œ 1, ๋ฌธ์„œ 2์˜ BoW๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 3 ๋‹จ์–ด ์ง‘ํ•ฉ์— ๋Œ€ํ•œ ๋ฌธ์„œ 1 BoW : [1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0] ๋ฌธ์„œ 3 ๋‹จ์–ด ์ง‘ํ•ฉ์— ๋Œ€ํ•œ ๋ฌธ์„œ 2 BoW : [0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 2, 1, 1, 1] ๋ฌธ์„œ 3 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์ด๋ผ๋Š” ๋‹จ์–ด๋Š” ์ธ๋ฑ์Šค๊ฐ€ 4์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์ด๋ผ๋Š” ๋‹จ์–ด๋Š” ๋ฌธ์„œ 1์—์„œ๋Š” 2ํšŒ ๋“ฑ์žฅํ•˜๋ฉฐ, ๋ฌธ์„œ 2์—์„œ๋Š” 1ํšŒ ๋“ฑ์žฅํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ๋‘ BoW์˜ ์ธ๋ฑ์Šค 4์˜ ๊ฐ’์€ ๊ฐ๊ฐ 2์™€ 1์ด ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BoW๋Š” ๊ฐ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ ์ˆ˜์น˜ํ™”ํ•˜๋Š” ํ…์ŠคํŠธ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด๋ฏ€๋กœ ์ฃผ๋กœ ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋“ฑ์žฅํ–ˆ๋Š”์ง€๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์„œ๊ฐ€ ์–ด๋–ค ์„ฑ๊ฒฉ์˜ ๋ฌธ์„œ์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์ž‘์—…์— ์“ฐ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋‚˜ ์—ฌ๋Ÿฌ ๋ฌธ์„œ ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์— ์ฃผ๋กœ ์“ฐ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น, '๋‹ฌ๋ฆฌ๊ธฐ', '์ฒด๋ ฅ', '๊ทผ๋ ฅ'๊ณผ ๊ฐ™์€ ๋‹จ์–ด๊ฐ€ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋ฉด ํ•ด๋‹น ๋ฌธ์„œ๋ฅผ ์ฒด์œก ๊ด€๋ จ ๋ฌธ์„œ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ฉฐ, '๋ฏธ๋ถ„', '๋ฐฉ์ •์‹', '๋ถ€๋“ฑ์‹'๊ณผ ๊ฐ™์€ ๋‹จ์–ด๊ฐ€ ์ž์ฃผ ๋“ฑ์žฅํ•œ๋‹ค๋ฉด ์ˆ˜ํ•™ ๊ด€๋ จ ๋ฌธ์„œ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. CountVectorizer ํด๋ž˜์Šค๋กœ BoW ๋งŒ๋“ค๊ธฐ ์‚ฌ์ดํ‚ท ๋Ÿฐ์—์„œ๋Š” ๋‹จ์–ด์˜ ๋นˆ๋„๋ฅผ Count ํ•˜์—ฌ Vector๋กœ ๋งŒ๋“œ๋Š” CountVectorizer ํด๋ž˜์Šค๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด ์˜์–ด์— ๋Œ€ํ•ด์„œ๋Š” ์†์‰ฝ๊ฒŒ BoW๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CountVectorizer๋กœ ๊ฐ„๋‹จํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ BoW๋ฅผ ๋งŒ๋“œ๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. from sklearn.feature_extraction.text import CountVectorizer corpus = ['you know I want your love. because I love you.'] vector = CountVectorizer() # ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ๋ก print('bag of words vector :', vector.fit_transform(corpus).toarray()) # ๊ฐ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ถ€์—ฌ๋˜์—ˆ๋Š”์ง€๋ฅผ ์ถœ๋ ฅ print('vocabulary :',vector.vocabulary_) bag of words vector : [[1 1 2 1 2 1]] vocabulary : {'you': 4, 'know': 1, 'want': 3, 'your': 5, 'love': 2, 'because': 0} ์˜ˆ์ œ ๋ฌธ์žฅ์—์„œ you์™€ love๋Š” ๋‘ ๋ฒˆ์”ฉ ์–ธ๊ธ‰๋˜์—ˆ์œผ๋ฏ€๋กœ ๊ฐ๊ฐ ์ธ๋ฑ์Šค 2์™€ ์ธ๋ฑ์Šค 4์—์„œ 2์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, ๊ทธ ์™ธ์˜ ๊ฐ’์—์„œ๋Š” 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์•ŒํŒŒ๋ฒณ I๋Š” BoW๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์—์„œ ์‚ฌ๋ผ์กŒ๋Š”๋ฐ, ์ด๋Š” CountVectorizer๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ธธ์ด๊ฐ€ 2 ์ด์ƒ์ธ ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ๋งŒ ํ† ํฐ์œผ๋กœ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ •์ œ(Cleaning) ์ฑ•ํ„ฐ์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด, ์˜์–ด์—์„œ๋Š” ๊ธธ์ด๊ฐ€ ์งง์€ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ ๋˜ํ•œ ์ „์ฒ˜๋ฆฌ ์ž‘์—…์œผ๋กœ ๊ณ ๋ ค๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ๊ฒƒ์€ CountVectorizer๋Š” ๋‹จ์ง€ ๋„์–ด์“ฐ๊ธฐ๋งŒ์„ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด๋ฅผ ์ž๋ฅด๋Š” ๋‚ฎ์€ ์ˆ˜์ค€์˜ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  BoW๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์˜์–ด์˜ ๊ฒฝ์šฐ ๋„์–ด์“ฐ๊ธฐ๋งŒ์œผ๋กœ ํ† ํฐ ํ™”๊ฐ€ ์ˆ˜ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ ํ•œ๊ตญ์–ด์— CountVectorizer๋ฅผ ์ ์šฉํ•˜๋ฉด, ์กฐ์‚ฌ ๋“ฑ์˜ ์ด์œ ๋กœ ์ œ๋Œ€๋กœ BoW๊ฐ€ ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•ž์„œ BoW๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉํ–ˆ๋˜ '์ •๋ถ€๊ฐ€ ๋ฐœํ‘œํ•˜๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ ์†Œ๋น„์ž๊ฐ€ ๋Š๋ผ๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ ๋‹ค๋ฅด๋‹ค.'๋ผ๋Š” ๋ฌธ์žฅ์„ CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ BoW๋กœ ๋งŒ๋“ค ๊ฒฝ์šฐ, CountVectorizer๋Š” '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ '์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. CountVectorizer๋Š” ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•œ ๋’ค์— '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ'์™€ '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€'์œผ๋กœ ์กฐ์‚ฌ๋ฅผ ํฌํ•จํ•ด์„œ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๋‹จ์–ด๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ'์™€ '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€'์ด ๊ฐ์ž ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์—์„œ 1์ด๋ผ๋Š” ๋นˆ๋„์˜ ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 4. ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•œ BoW ๋งŒ๋“ค๊ธฐ ์•ž์„œ ๋ถˆ์šฉ์–ด๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ณ„๋กœ ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. BoW๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ ๋ฌธ์„œ์—์„œ ๊ฐ ๋‹จ์–ด๊ฐ€ ์–ผ๋งˆ๋‚˜ ์ž์ฃผ ๋“ฑ์žฅํ–ˆ๋Š”์ง€๋ฅผ ๋ณด๊ฒ ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜๋ฅผ ์ˆ˜์น˜ํ™”ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์€ ๊ฒฐ๊ตญ ํ…์ŠคํŠธ ๋‚ด์—์„œ ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ์ค‘์š”ํ•œ์ง€๋ฅผ ๋ณด๊ณ  ์‹ถ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ํ•จ์ถ•ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด BoW๋ฅผ ๋งŒ๋“ค ๋•Œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์ผ์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์ •ํ™•๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ „์ฒ˜๋ฆฌ ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜์–ด์˜ BoW๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” CountVectorizer๋Š” ๋ถˆ์šฉ์–ด๋ฅผ ์ง€์ •ํ•˜๋ฉด, ๋ถˆ์šฉ์–ด๋Š” ์ œ์™ธํ•˜๊ณ  BoW๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. from sklearn.feature_extraction.text import CountVectorizer from nltk.corpus import stopwords (1) ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ •์˜ํ•œ ๋ถˆ์šฉ์–ด ์‚ฌ์šฉ text = ["Family is not an important thing. It's everything."] vect = CountVectorizer(stop_words=["the", "a", "an", "is", "not"]) print('bag of words vector :',vect.fit_transform(text).toarray()) print('vocabulary :',vect.vocabulary_) bag of words vector : [[1 1 1 1 1]] vocabulary : {'family': 1, 'important': 2, 'thing': 4, 'it': 3, 'everything': 0} (2) CountVectorizer์—์„œ ์ œ๊ณตํ•˜๋Š” ์ž์ฒด ๋ถˆ์šฉ์–ด ์‚ฌ์šฉ text = ["Family is not an important thing. It's everything."] vect = CountVectorizer(stop_words="english") print('bag of words vector :',vect.fit_transform(text).toarray()) print('vocabulary :',vect.vocabulary_) bag of words vector : [[1 1 1]] vocabulary : {'family': 0, 'important': 1, 'thing': 2} (3) NLTK์—์„œ ์ง€์›ํ•˜๋Š” ๋ถˆ์šฉ์–ด ์‚ฌ์šฉ text = ["Family is not an important thing. It's everything."] stop_words = stopwords.words("english") vect = CountVectorizer(stop_words=stop_words) print('bag of words vector :',vect.fit_transform(text).toarray()) print('vocabulary :',vect.vocabulary_) bag of words vector : [[1 1 1 1]] vocabulary : {'family': 1, 'important': 2, 'thing': 3, 'everything': 0} 04-03 ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix, DTM) ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์„œ๋“ค์˜ BoW๋“ค์„ ๊ฒฐํ•ฉํ•œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ธ ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix, DTM) ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ดํ•˜ DTM์ด๋ผ๊ณ  ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ํ–‰๊ณผ ์—ด์„ ๋ฐ˜๋Œ€๋กœ ์„ ํƒํ•˜๋ฉด TDM์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์„œ๋“ค์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 1. ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix, DTM)์˜ ํ‘œ๊ธฐ๋ฒ• ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix, DTM)์ด๋ž€ ๋‹ค์ˆ˜์˜ ๋ฌธ์„œ์—์„œ ๋“ฑ์žฅํ•˜๋Š” ๊ฐ ๋‹จ์–ด๋“ค์˜ ๋นˆ๋„๋ฅผ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ๊ฐ ๋ฌธ์„œ์— ๋Œ€ํ•œ BoW๋ฅผ ํ•˜๋‚˜์˜ ํ–‰๋ ฌ๋กœ ๋งŒ๋“  ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, BoW์™€ ๋‹ค๋ฅธ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋ผ BoW ํ‘œํ˜„์„ ๋‹ค์ˆ˜์˜ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ๋ถ€๋ฅด๋Š” ์šฉ์–ด์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ด๋ ‡๊ฒŒ 4๊ฐœ์˜ ๋ฌธ์„œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๋ฌธ์„œ 1 : ๋จน๊ณ  ์‹ถ์€ ์‚ฌ๊ณผ ๋ฌธ์„œ 2 : ๋จน๊ณ  ์‹ถ์€ ๋ฐ”๋‚˜๋‚˜ ๋ฌธ์„œ 3 : ๊ธธ๊ณ  ๋…ธ๋ž€ ๋ฐ”๋‚˜๋‚˜ ๋ฐ”๋‚˜๋‚˜ ๋ฌธ์„œ 4 : ์ €๋Š” ๊ณผ์ผ์ด ์ข‹์•„์š” ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ณผ์ผ์ด ๊ธธ๊ณ  ๋…ธ๋ž€ ๋จน๊ณ  ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์‹ถ์€ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 0 0 0 1 0 1 1 0 0 ๋ฌธ์„œ 2 0 0 0 1 1 0 1 0 0 ๋ฌธ์„œ 3 0 1 1 0 2 0 0 0 0 ๋ฌธ์„œ 4 1 0 0 0 0 0 0 1 1 ๊ฐ ๋ฌธ์„œ์—์„œ ๋“ฑ์žฅํ•œ ๋‹จ์–ด์˜ ๋นˆ๋„๋ฅผ ํ–‰๋ ฌ์˜ ๊ฐ’์œผ๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์€ ๋ฌธ์„œ๋“ค์„ ์„œ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์น˜ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ์˜์˜๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•„์š”์— ๋”ฐ๋ผ์„œ๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋กœ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , ๋ถˆ์šฉ์–ด์— ํ•ด๋‹น๋˜๋Š” ์กฐ์‚ฌ๋“ค ๋˜ํ•œ ์ œ๊ฑฐํ•˜์—ฌ ๋” ์ •์ œ๋œ DTM์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 2. ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix)์˜ ํ•œ๊ณ„ DTM์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ณ  ๊ตฌํ˜„ํ•˜๊ธฐ๋„ ์‰ฝ์ง€๋งŒ, ๋ณธ์งˆ์ ์œผ๋กœ ๊ฐ€์ง€๋Š” ๋ช‡ ๊ฐ€์ง€ ํ•œ๊ณ„๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. 1) ํฌ์†Œ ํ‘œํ˜„(Sparse representation) ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋˜๊ณ  ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ’์ด 0์ด ๋˜๋Š” ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๊ณต๊ฐ„์  ๋‚ญ๋น„์™€ ๊ณ„์‚ฐ ๋ฆฌ์†Œ์Šค๋ฅผ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ๋‹จ์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. DTM๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. DTM์—์„œ์˜ ๊ฐ ํ–‰์„ ๋ฌธ์„œ ๋ฒกํ„ฐ๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ฐ ๋ฌธ์„œ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ „์ฒด ์ฝ”ํผ์Šค๊ฐ€ ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ผ๋ฉด ๋ฌธ์„œ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ ์ˆ˜๋งŒ ์ด์ƒ์˜ ์ฐจ์›์„ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋งŽ์€ ๋ฌธ์„œ ๋ฒกํ„ฐ๊ฐ€ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ’์ด 0์„ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์žฅ ์œ„์—์„œ ์˜ˆ๋กœ ๋“ค์—ˆ๋˜ ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์˜ ๋ชจ๋“  ํ–‰์ด 0์ด ์•„๋‹Œ ๊ฐ’๋ณด๋‹ค 0์˜ ๊ฐ’์ด ๋” ๋งŽ์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋‚˜ DTM๊ณผ ๊ฐ™์€ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ’์ด 0์ธ ํ‘œํ˜„์„ ํฌ์†Œ ๋ฒกํ„ฐ(sparse vector) ๋˜๋Š” ํฌ์†Œ ํ–‰๋ ฌ(sparse matrix)๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, ํฌ์†Œ ๋ฒกํ„ฐ๋Š” ๋งŽ์€ ์–‘์˜ ์ €์žฅ ๊ณต๊ฐ„๊ณผ ๋†’์€ ๊ณ„์‚ฐ ๋ณต์žก๋„๋ฅผ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š” ์ผ์€ BoW ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์—์„œ ์ค‘์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ๋‘์ , ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด, ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ์–ด๊ฐ„์ด๋‚˜ ํ‘œ์ œ์–ด ์ถ”์ถœ์„ ํ†ตํ•ด ๋‹จ์–ด๋ฅผ ์ •๊ทœํ™”ํ•˜์—ฌ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ๋‹จ์ˆœ ๋นˆ๋„ ์ˆ˜ ๊ธฐ๋ฐ˜ ์ ‘๊ทผ ์—ฌ๋Ÿฌ ๋ฌธ์„œ์— ๋“ฑ์žฅํ•˜๋Š” ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ๋นˆ๋„ ํ‘œ๊ธฐ๋ฅผ ํ•˜๋Š” ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์€ ๋•Œ๋กœ๋Š” ํ•œ๊ณ„๋ฅผ ๊ฐ€์ง€๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์˜์–ด์— ๋Œ€ํ•ด์„œ DTM์„ ๋งŒ๋“ค์—ˆ์„ ๋•Œ, ๋ถˆ์šฉ์–ด์ธ the๋Š” ์–ด๋–ค ๋ฌธ์„œ์ด๋“  ์ž์ฃผ ๋“ฑ์žฅํ•  ์ˆ˜๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œ ์‚ฌํ•œ ๋ฌธ์„œ์ธ์ง€ ๋น„๊ตํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์„œ 1, ๋ฌธ์„œ 2, ๋ฌธ์„œ 3์—์„œ ๋™์ผํ•˜๊ฒŒ the๊ฐ€ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’๋‹ค๊ณ  ํ•ด์„œ ์ด ๋ฌธ์„œ๋“ค์ด ์œ ์‚ฌํ•œ ๋ฌธ์„œ๋ผ๊ณ  ํŒ๋‹จํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ฌธ์„œ์—๋Š” ์ค‘์š”ํ•œ ๋‹จ์–ด์™€ ๋ถˆํ•„์š”ํ•œ ๋‹จ์–ด๋“ค์ด ํ˜ผ์žฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ถˆ์šฉ์–ด(stopwords)์™€ ๊ฐ™์€ ๋‹จ์–ด๋“ค์€ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’๋”๋ผ๋„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ์žˆ์–ด ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ๋ชปํ•˜๋Š” ๋‹จ์–ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด DTM์— ๋ถˆ์šฉ์–ด์™€ ์ค‘์š”ํ•œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ๊ฐ€์ค‘์น˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ์š”? ์ด๋Ÿฌํ•œ ์•„์ด๋””์–ด๋ฅผ ์ ์šฉํ•œ TF-IDF๋ฅผ ์ด์–ด์„œ ํ•™์Šตํ•ด ๋ด…์‹œ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DTM์„ ๋งŒ๋“œ๋Š” ์‹ค์Šต ๋˜ํ•œ TF-IDF๋ฅผ ์„ค๋ช…ํ•˜๋ฉด์„œ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 04-04 TF-IDF(Term Frequency-Inverse Document Frequency) ์ด๋ฒˆ์—๋Š” DTM ๋‚ด์— ์žˆ๋Š” ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ์ค‘์š”๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” TF-IDF ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. TF-IDF๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ธฐ์กด์˜ DTM์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ณด๋‹ค ๋งŽ์€ ์ •๋ณด๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๋ฌธ์„œ๋“ค์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. TF-IDF๊ฐ€ DTM๋ณด๋‹ค ํ•ญ์ƒ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์žฅํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ๋งŽ์€ ๊ฒฝ์šฐ์—์„œ DTM๋ณด๋‹ค ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. TF-IDF(๋‹จ์–ด ๋นˆ๋„-์—ญ ๋ฌธ์„œ ๋นˆ๋„, Term Frequency-Inverse Document Frequency) TF-IDF(Term Frequency-Inverse Document Frequency)๋Š” ๋‹จ์–ด์˜ ๋นˆ๋„์™€ ์—ญ ๋ฌธ์„œ ๋นˆ๋„(๋ฌธ์„œ์˜ ๋นˆ๋„์— ํŠน์ • ์‹์„ ์ทจํ•จ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DTM ๋‚ด์˜ ๊ฐ ๋‹จ์–ด๋“ค๋งˆ๋‹ค ์ค‘์š”ํ•œ ์ •๋„๋ฅผ ๊ฐ€์ค‘์น˜๋กœ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์šฐ์„  DTM์„ ๋งŒ๋“  ํ›„, TF-IDF ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. TF-IDF๋Š” ์ฃผ๋กœ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ์ž‘์—…, ๊ฒ€์ƒ‰ ์‹œ์Šคํ…œ์—์„œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์˜ ์ค‘์š”๋„๋ฅผ ์ •ํ•˜๋Š” ์ž‘์—…, ๋ฌธ์„œ ๋‚ด์—์„œ ํŠน์ • ๋‹จ์–ด์˜ ์ค‘์š”๋„๋ฅผ ๊ตฌํ•˜๋Š” ์ž‘์—… ๋“ฑ์— ์“ฐ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. TF-IDF๋Š” TF์™€ IDF๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ์˜๋ฏธํ•˜๋Š” ๋ฐ ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ๋ฅผ d, ๋‹จ์–ด๋ฅผ t, ๋ฌธ์„œ์˜ ์ด๊ฐœ์ˆ˜๋ฅผ n์ด๋ผ๊ณ  ํ‘œํ˜„ํ•  ๋•Œ TF, DF, IDF๋Š” ๊ฐ๊ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (1) tf(d, t) : ํŠน์ • ๋ฌธ์„œ d์—์„œ์˜ ํŠน์ • ๋‹จ์–ด t์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜. ์ƒ์†Œํ•œ ๊ธ€์ž ๋•Œ๋ฌธ์— ์–ด๋ ค์›Œ ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ž˜ ์ƒ๊ฐํ•ด ๋ณด๋ฉด TF๋Š” ์ด๋ฏธ ์•ž์—์„œ ๊ตฌํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. TF๋Š” ์•ž์—์„œ ๋ฐฐ์šด DTM์˜ ์˜ˆ์ œ์—์„œ ๊ฐ ๋‹จ์–ด๋“ค์ด ๊ฐ€์ง„ ๊ฐ’๋“ค์ž…๋‹ˆ๋‹ค. DTM์ด ๊ฐ ๋ฌธ์„œ์—์„œ์˜ ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. (2) df(t) : ํŠน์ • ๋‹จ์–ด t๊ฐ€ ๋“ฑ์žฅํ•œ ๋ฌธ์„œ์˜ ์ˆ˜. ์—ฌ๊ธฐ์„œ ํŠน์ • ๋‹จ์–ด๊ฐ€ ๊ฐ ๋ฌธ์„œ, ๋˜๋Š” ๋ฌธ์„œ๋“ค์—์„œ ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ–ˆ๋Š”์ง€๋Š” ๊ด€์‹ฌ ๊ฐ€์ง€์ง€ ์•Š์œผ๋ฉฐ ์˜ค์ง ํŠน์ • ๋‹จ์–ด t๊ฐ€ ๋“ฑ์žฅํ•œ ๋ฌธ์„œ์˜ ์ˆ˜์—๋งŒ ๊ด€์‹ฌ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด DTM์—์„œ ๋ฐ”๋‚˜๋‚˜๋Š” ๋ฌธ์„œ 2์™€ ๋ฌธ์„œ 3์—์„œ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋ฐ”๋‚˜๋‚˜์˜ df๋Š” 2์ž…๋‹ˆ๋‹ค. ๋ฌธ์„œ 3์—์„œ ๋ฐ”๋‚˜๋‚˜๊ฐ€ ๋‘ ๋ฒˆ ๋“ฑ์žฅํ–ˆ์ง€๋งŒ, ๊ทธ๊ฒƒ์€ ์ค‘์š”ํ•œ ๊ฒŒ ์•„๋‹™๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ๋ฐ”๋‚˜๋‚˜๋ž€ ๋‹จ์–ด๊ฐ€ ๋ฌธ์„œ 2์—์„œ 100๋ฒˆ ๋“ฑ์žฅํ–ˆ๊ณ , ๋ฌธ์„œ 3์—์„œ 200๋ฒˆ ๋“ฑ์žฅํ–ˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๋ฐ”๋‚˜๋‚˜์˜ df๋Š” 2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. (3) idf(t) : df(t)์— ๋ฐ˜๋น„๋ก€ํ•˜๋Š” ์ˆ˜. d ( ) l g ( 1 d ( ) ) IDF๋ผ๋Š” ์ด๋ฆ„์„ ๋ณด๊ณ  DF์˜ ์—ญ์ˆ˜๊ฐ€ ์•„๋‹๊นŒ ์ƒ๊ฐํ–ˆ๋‹ค๋ฉด, IDF๋Š” DF์˜ ์—ญ์ˆ˜๋ฅผ ์ทจํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์ด ๋งž์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ log์™€ ๋ถ„๋ชจ์— 1์„ ๋”ํ•ด์ฃผ๋Š” ์‹์— ์˜์•„ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. log๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์„ ๋•Œ, IDF๋ฅผ DF์˜ ์—ญ์ˆ˜( d ( ) ๋ผ๋Š” ์‹)๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ด ๋ฌธ์„œ์˜ ์ˆ˜ n์ด ์ปค์งˆ์ˆ˜๋ก, IDF์˜ ๊ฐ’์€ ๊ธฐํ•˜๊ธ‰์ˆ˜์ ์œผ๋กœ ์ปค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— log๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์™œ log๊ฐ€ ํ•„์š”ํ•œ์ง€ n=1,000,000์ผ ๋•Œ์˜ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. log์˜ ๋ฐ‘์€ 10์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์˜€์„ ๋•Œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. d ( ) l g ( / f ( ) ) = , 000 000 ๋‹จ์–ด d ( ) d ( , ) word1 1 6 word2 100 4 word3 1,000 3 word4 10,000 2 word5 100,000 1 word6 1,000,000 0 ๊ทธ๋ ‡๋‹ค๋ฉด log๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด idf์˜ ๊ฐ’์ด ์–ด๋–ป๊ฒŒ ์ปค์ง€๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. d ( ) n d ( ) = , 000 000 ๋‹จ์–ด d ( ) d ( ) word1 1 1,000,000 word2 100 10,000 word3 1,000 1,000 word4 10,000 100 word5 100,000 10 word6 1,000,000 1 ๋˜ ๋‹ค๋ฅธ ์ง๊ด€์ ์ธ ์„ค๋ช…์€ ๋ถˆ์šฉ์–ด ๋“ฑ๊ณผ ๊ฐ™์ด ์ž์ฃผ ์“ฐ์ด๋Š” ๋‹จ์–ด๋“ค์€ ๋น„๊ต์  ์ž์ฃผ ์“ฐ์ด์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค๋ณด๋‹ค ์ตœ์†Œ ์ˆ˜์‹ญ ๋ฐฐ ์ž์ฃผ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋น„๊ต์  ์ž์ฃผ ์“ฐ์ด์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์กฐ์ฐจ ํฌ๊ท€ ๋‹จ์–ด๋“ค๊ณผ ๋น„๊ตํ•˜๋ฉด ๋˜ ์ตœ์†Œ ์ˆ˜๋ฐฑ ๋ฐฐ๋Š” ๋” ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. ์ด ๋•Œ๋ฌธ์— log๋ฅผ ์”Œ์›Œ์ฃผ์ง€ ์•Š์œผ๋ฉด, ํฌ๊ท€ ๋‹จ์–ด๋“ค์— ์—„์ฒญ๋‚œ ๊ฐ€์ค‘์น˜๊ฐ€ ๋ถ€์—ฌ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ๊ทธ๋ฅผ ์”Œ์šฐ๋ฉด ์ด๋Ÿฐ ๊ฒฉ์ฐจ๋ฅผ ์ค„์ด๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. log ์•ˆ์˜ ์‹์—์„œ ๋ถ„๋ชจ์— 1์„ ๋”ํ•ด์ฃผ๋Š” ์ด์œ ๋Š” ์ฒซ ๋ฒˆ์งธ ์ด์œ ๋กœ๋Š” ํŠน์ • ๋‹จ์–ด๊ฐ€ ์ „์ฒด ๋ฌธ์„œ์—์„œ ๋“ฑ์žฅํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์— ๋ถ„๋ชจ๊ฐ€ 0์ด ๋˜๋Š” ์ƒํ™ฉ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. TF-IDF๋Š” ๋ชจ๋“  ๋ฌธ์„œ์—์„œ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋Š” ์ค‘์š”๋„๊ฐ€ ๋‚ฎ๋‹ค๊ณ  ํŒ๋‹จํ•˜๋ฉฐ, ํŠน์ • ๋ฌธ์„œ์—์„œ๋งŒ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋Š” ์ค‘์š”๋„๊ฐ€ ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. TF-IDF ๊ฐ’์ด ๋‚ฎ์œผ๋ฉด ์ค‘์š”๋„๊ฐ€ ๋‚ฎ์€ ๊ฒƒ์ด๋ฉฐ, TF-IDF ๊ฐ’์ด ํฌ๋ฉด ์ค‘์š”๋„๊ฐ€ ํฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, the๋‚˜ a์™€ ๊ฐ™์ด ๋ถˆ์šฉ์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“  ๋ฌธ์„œ์— ์ž์ฃผ ๋“ฑ์žฅํ•˜๊ธฐ ๋งˆ๋ จ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ถˆ์šฉ์–ด์˜ TF-IDF์˜ ๊ฐ’์€ ๋‹ค๋ฅธ ๋‹จ์–ด์˜ TF-IDF์— ๋น„ํ•ด์„œ ๋‚ฎ์•„์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ณผ์ผ์ด ๊ธธ๊ณ  ๋…ธ๋ž€ ๋จน๊ณ  ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์‹ถ์€ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 0 0 0 1 0 1 1 0 0 ๋ฌธ์„œ 2 0 0 0 1 1 0 1 0 0 ๋ฌธ์„œ 3 0 1 1 0 2 0 0 0 0 ๋ฌธ์„œ 4 1 0 0 0 0 0 0 1 1 ์•ž์„œ DTM์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๋“ค์—ˆ๋˜ ์œ„์˜ ์˜ˆ์ œ๋ฅผ ๊ฐ€์ง€๊ณ  TF-IDF์— ๋Œ€ํ•ด ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  TF๋Š” ์•ž์„œ ์‚ฌ์šฉํ•œ DTM์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด, ๊ทธ๊ฒƒ์ด ๊ฐ ๋ฌธ์„œ์—์„œ์˜ ๊ฐ ๋‹จ์–ด์˜ TF๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ๊ตฌํ•ด์•ผ ํ•  ๊ฒƒ์€ TF์™€ ๊ณฑํ•ด์•ผ ํ•  ๊ฐ’์ธ IDF์ž…๋‹ˆ๋‹ค. ๋กœ๊ทธ๋Š” ์ž์—ฐ ๋กœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž์—ฐ ๋กœ๊ทธ๋Š” ๋กœ๊ทธ์˜ ๋ฐ‘์„ ์ž์—ฐ ์ƒ์ˆ˜ e(e=2.718281...)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋กœ๊ทธ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. IDF ๊ณ„์‚ฐ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋กœ๊ทธ์˜ ๋ฐ‘์€ TF-IDF๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ž„์˜๋กœ ์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ๋กœ๊ทธ๋Š” ๋งˆ์น˜ ๊ธฐ์กด์˜ ๊ฐ’์— ๊ณฑํ•˜์—ฌ ๊ฐ’์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ ˆํ•˜๋Š” ์ƒ์ˆ˜์˜ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ์ข… ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ํŒจํ‚ค์ง€๋กœ ์ง€์›ํ•˜๋Š” TF-IDF์˜ ๋กœ๊ทธ๋Š” ๋Œ€๋ถ€๋ถ„ ์ž์—ฐ ๋กœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ์ž์—ฐ ๋กœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž์—ฐ ๋กœ๊ทธ๋Š” ๋ณดํ†ต log๋ผ๊ณ  ํ‘œํ˜„ํ•˜์ง€ ์•Š๊ณ , ln์ด๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด IDF(์—ญ ๋ฌธ์„œ ๋นˆ๋„) ๊ณผ์ผ์ด ln(4/(1+1)) = 0.693147 ๊ธธ๊ณ  ln(4/(1+1)) = 0.693147 ๋…ธ๋ž€ ln(4/(1+1)) = 0.693147 ๋จน๊ณ  ln(4/(2+1)) = 0.287682 ๋ฐ”๋‚˜๋‚˜ ln(4/(2+1)) = 0.287682 ์‚ฌ๊ณผ ln(4/(1+1)) = 0.693147 ์‹ถ์€ ln(4/(2+1)) = 0.287682 ์ €๋Š” ln(4/(1+1)) = 0.693147 ์ข‹์•„์š” ln(4/(1+1)) = 0.693147 ๋ฌธ์„œ์˜ ์ด ์ˆ˜๋Š” 4์ด๊ธฐ ๋•Œ๋ฌธ์— ln ์•ˆ์—์„œ ๋ถ„์ž๋Š” ๋Š˜ 4๋กœ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋ถ„๋ชจ์˜ ๊ฒฝ์šฐ์—๋Š” ๊ฐ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ๋ฌธ์„œ์˜ ์ˆ˜(DF)๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด์„œ '๋จน๊ณ '์˜ ๊ฒฝ์šฐ์—๋Š” ์ด 2๊ฐœ์˜ ๋ฌธ์„œ(๋ฌธ์„œ 1, ๋ฌธ์„œ 2)์— ๋“ฑ์žฅํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— 2๋ผ๋Š” ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ IDF์˜ ๊ฐ’์„ ๋น„๊ตํ•ด ๋ณด๋ฉด ๋ฌธ์„œ 1๊ฐœ์—๋งŒ ๋“ฑ์žฅํ•œ ๋‹จ์–ด์™€ ๋ฌธ์„œ 2๊ฐœ์—๋งŒ ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋Š” ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. IDF๋Š” ์—ฌ๋Ÿฌ ๋ฌธ์„œ์—์„œ ๋“ฑ์žฅํ•œ ๋‹จ์–ด์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋‚ฎ์ถ”๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. TF-IDF๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์˜ TF๋Š” DTM์—์„œ์˜ ๊ฐ ๋‹จ์–ด์˜ ๊ฐ’๊ณผ ๊ฐ™์œผ๋ฏ€๋กœ, ์•ž์„œ ์‚ฌ์šฉํ•œ DTM์—์„œ ๋‹จ์–ด ๋ณ„๋กœ ์œ„์˜ IDF ๊ฐ’์„ ๊ณฑํ•ด์ฃผ๋ฉด TF-IDF ๊ฐ’์„ ์–ป์Šต๋‹ˆ๋‹ค. ๊ณผ์ผ์ด ๊ธธ๊ณ  ๋…ธ๋ž€ ๋จน๊ณ  ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์‹ถ์€ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 0 0 0 0.287682 0 0.693147 0.287682 0 0 ๋ฌธ์„œ 2 0 0 0 0.287682 0.287682 0 0.287682 0 0 ๋ฌธ์„œ 3 0 0.693147 0.693147 0 0.575364 0 0 0 0 ๋ฌธ์„œ 4 0.693147 0 0 0 0 0 0 0.693147 0.693147 ์‚ฌ์‹ค ์˜ˆ์ œ ๋ฌธ์„œ๊ฐ€ ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณ„์‚ฐ์€ ๋งค์šฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 3์—์„œ์˜ ๋ฐ”๋‚˜๋‚˜๋งŒ TF ๊ฐ’์ด 2์ด๋ฏ€๋กœ IDF์— 2๋ฅผ ๊ณฑํ•ด์ฃผ๊ณ , ๋‚˜๋จธ์ง„ TF ๊ฐ’์ด 1์ด๋ฏ€๋กœ ๊ทธ๋Œ€๋กœ IDF ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ 2์—์„œ์˜ ๋ฐ”๋‚˜๋‚˜์˜ TF-IDF ๊ฐ€์ค‘์น˜์™€ ๋ฌธ์„œ 3์—์„œ์˜ ๋ฐ”๋‚˜๋‚˜์˜ TF-IDF ๊ฐ€์ค‘์น˜๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜์‹์ ์œผ๋กœ ๋งํ•˜๋ฉด, TF๊ฐ€ ๊ฐ๊ฐ 1๊ณผ 2๋กœ ๋‹ฌ๋ž๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ TF-IDF์—์„œ์˜ ๊ด€์ ์—์„œ ๋ณด์ž๋ฉด TF-IDF๋Š” ํŠน์ • ๋ฌธ์„œ์—์„œ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋Š” ๊ทธ ๋ฌธ์„œ ๋‚ด์—์„œ ์ค‘์š”ํ•œ ๋‹จ์–ด๋กœ ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฌธ์„œ 2์—์„œ๋Š” ๋ฐ”๋‚˜๋‚˜๋ฅผ ํ•œ ๋ฒˆ ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ, ๋ฌธ์„œ 3์—์„œ๋Š” ๋ฐ”๋‚˜๋‚˜๋ฅผ ๋‘ ๋ฒˆ ์–ธ๊ธ‰ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์„œ 3์—์„œ์˜ ๋ฐ”๋‚˜๋‚˜๋ฅผ ๋”์šฑ ์ค‘์š”ํ•œ ๋‹จ์–ด๋ผ๊ณ  ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 2. ํŒŒ์ด์ฌ์œผ๋กœ TF-IDF ์ง์ ‘ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„์˜ ๊ณ„์‚ฐ ๊ณผ์ •์„ ํŒŒ์ด์ฌ์œผ๋กœ ์ง์ ‘ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์˜ ์„ค๋ช…์—์„œ ์‚ฌ์šฉํ•œ 4๊ฐœ์˜ ๋ฌธ์„œ๋ฅผ docs์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. import pandas as pd # ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์‚ฌ์šฉ์„ ์œ„ํ•ด from math import log # IDF ๊ณ„์‚ฐ์„ ์œ„ํ•ด docs = [ '๋จน๊ณ  ์‹ถ์€ ์‚ฌ๊ณผ', '๋จน๊ณ  ์‹ถ์€ ๋ฐ”๋‚˜๋‚˜', '๊ธธ๊ณ  ๋…ธ๋ž€ ๋ฐ”๋‚˜๋‚˜ ๋ฐ”๋‚˜๋‚˜', '์ €๋Š” ๊ณผ์ผ์ด ์ข‹์•„์š”' ] vocab = list(set(w for doc in docs for w in doc.split())) vocab.sort() TF, IDF, ๊ทธ๋ฆฌ๊ณ  TF-IDF ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. # ์ด ๋ฌธ์„œ์˜ ์ˆ˜ N = len(docs) def tf(t, d): return d.count(t) def idf(t): df = 0 for doc in docs: df += t in doc return log(N/(df+1)) def tfidf(t, d): return tf(t, d)* idf(t) TF๋ฅผ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด DTM์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•˜์—ฌ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. result = [] # ๊ฐ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ์•„๋ž˜ ์—ฐ์‚ฐ์„ ๋ฐ˜๋ณต for i in range(N): result.append([]) d = docs[i] for j in range(len(vocab)): t = vocab[j] result[-1].append(tf(t, d)) tf_ = pd.DataFrame(result, columns = vocab) ์ •์ƒ์ ์œผ๋กœ DTM์ด ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ IDF ๊ฐ’์„ ๊ตฌํ•ด๋ด…์‹œ๋‹ค. result = [] for j in range(len(vocab)): t = vocab[j] result.append(idf(t)) idf_ = pd.DataFrame(result, index=vocab, columns=["IDF"]) idf_ ์œ„์—์„œ ์ˆ˜๊ธฐ๋กœ ๊ตฌํ•œ IDF ๊ฐ’๋“ค๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. TF-IDF ํ–‰๋ ฌ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. result = [] for i in range(N): result.append([]) d = docs[i] for j in range(len(vocab)): t = vocab[j] result[-1].append(tfidf(t, d)) tfidf_ = pd.DataFrame(result, columns = vocab) tfidf_ TF-IDF์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์‹์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜๊ณ  ์‹ค์ œ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์‹ค์ œ TF-IDF ๊ตฌํ˜„์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š” ๋งŽ์€ ๋จธ์‹  ๋Ÿฌ๋‹ ํŒจํ‚ค์ง€๋“ค์€ ํŒจํ‚ค์ง€๋งˆ๋‹ค ์‹์ด ์กฐ๊ธˆ์”ฉ ์ƒ์ดํ•˜์ง€๋งŒ, ์œ„์—์„œ ๋ฐฐ์šด ์‹๊ณผ๋Š” ๋‹ค๋ฅธ ์กฐ์ •๋œ ์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์œ„์˜ ๊ธฐ๋ณธ์ ์ธ ์‹์„ ๋ฐ”ํƒ•์œผ๋กœ ํ•œ ๊ตฌํ˜„์—๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜ ์ด 4์ธ๋ฐ, f ( ) ์˜ ๊ฐ’์ด 3์ธ ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ค ์ผ์ด ๋ฒŒ์–ด์งˆ๊นŒ์š”? f ( ) ์— 1์ด ๋”ํ•ด์ง€๋ฉด์„œ log ํ•ญ์˜ ๋ถ„์ž์™€ ๋ถ„๋ชจ์˜ ๊ฐ’์ด ๊ฐ™์•„์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” o์˜ ์ง„์ˆ˜ ๊ฐ’์ด 1์ด ๋˜๋ฉด์„œ d ( , ) ์˜ ๊ฐ’์ด 0์ด ๋จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด d ( , ) l g ( / ( f ( ) 1 ) ) 0 ์ž…๋‹ˆ๋‹ค. IDF์˜ ๊ฐ’์ด 0์ด๋ผ๋ฉด ๋” ์ด์ƒ ๊ฐ€์ค‘์น˜์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์‹ค์Šตํ•  ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ TF-IDF ๊ตฌํ˜„์ฒด ๋˜ํ•œ ์œ„์˜ ์‹์—์„œ ์กฐ์ •๋œ ์‹์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 3. ์‚ฌ์ดํ‚ท๋Ÿฐ์„ ์ด์šฉํ•œ DTM๊ณผ TF-IDF ์‹ค์Šต ์‚ฌ์ดํ‚ท๋Ÿฐ์„ ํ†ตํ•ด DTM๊ณผ TF-IDF๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. BoW๋ฅผ ์„ค๋ช…ํ•˜๋ฉฐ ๋ฐฐ์šด CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด DTM์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from sklearn.feature_extraction.text import CountVectorizer corpus = [ 'you know I want your love', 'I like you', 'what should I do ', ] vector = CountVectorizer() # ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ๋ก print(vector.fit_transform(corpus).toarray()) # ๊ฐ ๋‹จ์–ด์™€ ๋งคํ•‘๋œ ์ธ๋ฑ์Šค ์ถœ๋ ฅ print(vector.vocabulary_) [[0 1 0 1 0 1 0 1 1] [0 0 1 0 0 0 0 1 0] [1 0 0 0 1 0 1 0 0]] {'you': 7, 'know': 1, 'want': 5, 'your': 8, 'love': 3, 'like': 2, 'what': 6, 'should': 4, 'do': 0} DTM์ด ์™„์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. DTM์—์„œ ๊ฐ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ถ€์—ฌ๋˜์—ˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด, ์ธ๋ฑ์Šค๋ฅผ ํ™•์ธํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์—ด์˜ ๊ฒฝ์šฐ์—๋Š” 0์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ do์ž…๋‹ˆ๋‹ค. do๋Š” ์„ธ ๋ฒˆ์งธ ๋ฌธ์„œ์—๋งŒ ๋“ฑ์žฅํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์„ธ ๋ฒˆ์งธ ํ–‰์—์„œ๋งŒ 1์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์—ด์˜ ๊ฒฝ์šฐ์—๋Š” 1์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ know์ž…๋‹ˆ๋‹ค. know๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ์—๋งŒ ๋“ฑ์žฅํ–ˆ์œผ๋ฏ€๋กœ ์ฒซ ๋ฒˆ์งธ ํ–‰์—์„œ๋งŒ 1์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์€ TF-IDF๋ฅผ ์ž๋™ ๊ณ„์‚ฐํ•ด ์ฃผ๋Š” TfidfVectorizer๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ TF-IDF๋Š” ์œ„์—์„œ ๋ฐฐ์› ๋˜ ๋ณดํŽธ์ ์ธ TF-IDF ๊ธฐ๋ณธ ์‹์—์„œ ์กฐ์ •๋œ ์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜์ž๋ฉด, IDF์˜ ๋กœ๊ทธ ํ•ญ์˜ ๋ถ„์ž์— 1์„ ๋”ํ•ด์ฃผ๋ฉฐ, ๋กœ๊ทธํ•ญ์— 1์„ ๋”ํ•ด์ฃผ๊ณ , TF-IDF์— L2 ์ •๊ทœํ™”๋ผ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ’์„ ์กฐ์ •ํ•˜๋Š” ๋“ฑ์˜ ์ฐจ์ด๋กœ TF-IDF๊ฐ€ ๊ฐ€์ง„ ์˜๋„๋Š” ์—ฌ์ „ํžˆ ๊ทธ๋Œ€๋กœ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. from sklearn.feature_extraction.text import TfidfVectorizer corpus = [ 'you know I want your love', 'I like you', 'what should I do ', ] tfidfv = TfidfVectorizer().fit(corpus) print(tfidfv.transform(corpus).toarray()) print(tfidfv.vocabulary_) [[0. 0.46735098 0. 0.46735098 0. 0.46735098 0. 0.35543247 0.46735098] [0. 0. 0.79596054 0. 0. 0. 0. 0.60534851 0. ] [0.57735027 0. 0. 0. 0.57735027 0. 0.57735027 0. 0. ]] {'you': 7, 'know': 1, 'want': 5, 'your': 8, 'love': 3, 'like': 2, 'what': 6, 'should': 4, 'do': 0} BoW, DTM, TF-IDF์— ๋Œ€ํ•ด์„œ ์ „๋ถ€ ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์žฌ๋ฃŒ ์†์งˆํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šด ์…ˆ์ž…๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค๋กœ๋„ DTM๊ณผ TF-IDF ํ–‰๋ ฌ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Š” ๋”ฅ ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ์˜ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‹ค์Šต์—์„œ ๋ณ„๋„๋กœ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ด๋ฅผ ์ด์šฉํ•œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ TF-IDF์˜ ์ˆ˜์‹์„ ์ดํ•ดํ•˜๊ณ  ์‹ถ์€ ๋ถ„๋“ค์„ ์œ„ํ•ด์„œ ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์— ๋Œ“๊ธ€๋กœ ์„ค๋ช…ํ•ด๋†จ์Šต๋‹ˆ๋‹ค. ๊ถ๊ธˆํ•˜์‹  ๋ถ„๋“ค์€ ์ฐธ๊ณ ํ•˜์„ธ์š”. 05. ๋ฒกํ„ฐ์˜ ์œ ์‚ฌ๋„(Vector Similarity) ๋ฌธ์žฅ์ด๋‚˜ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ์ž‘์—…์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์ฃผ์š” ์ฃผ์ œ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์‚ฌ๋žŒ๋“ค์ด ์ธ์‹ํ•˜๋Š” ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋Š” ์ฃผ๋กœ ๋ฌธ์„œ๋“ค ๊ฐ„์— ๋™์ผํ•œ ๋‹จ์–ด ๋˜๋Š” ๋น„์Šทํ•œ ๋‹จ์–ด๊ฐ€ ์–ผ๋งˆ๋‚˜ ๊ณตํ†ต์ ์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜์—ˆ๋Š”์ง€์— ์˜์กดํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ๊ณ„์‚ฐํ•˜๋Š” ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„์˜ ์„ฑ๋Šฅ์€ ๊ฐ ๋ฌธ์„œ์˜ ๋‹จ์–ด๋“ค์„ ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ ์ˆ˜์น˜ํ™”ํ•˜์—ฌ ํ‘œํ˜„ํ–ˆ๋Š”์ง€(DTM, Word2Vec ๋“ฑ), ๋ฌธ์„œ ๊ฐ„์˜ ๋‹จ์–ด๋“ค์˜ ์ฐจ์ด๋ฅผ ์–ด๋–ค ๋ฐฉ๋ฒ•(์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ, ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๋“ฑ)์œผ๋กœ ๊ณ„์‚ฐํ–ˆ๋Š”์ง€์— ๋‹ฌ๋ ค์žˆ์Šต๋‹ˆ๋‹ค. 05-01 ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„(Cosine Similarity) BoW์— ๊ธฐ๋ฐ˜ํ•œ ๋‹จ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ธ DTM, TF-IDF, ๋˜๋Š” ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋  Word2Vec ๋“ฑ๊ณผ ๊ฐ™์ด ๋‹จ์–ด๋ฅผ ์ˆ˜์น˜ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ–ˆ๋‹ค๋ฉด ์ด๋Ÿฌํ•œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 1. ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„(Cosine Similarity) ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋Š” ๋‘ ๋ฒกํ„ฐ ๊ฐ„์˜ ์ฝ”์‚ฌ์ธ ๊ฐ๋„๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ์œ ์‚ฌ๋„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒกํ„ฐ์˜ ๋ฐฉํ–ฅ์ด ์™„์ „ํžˆ ๋™์ผํ•œ ๊ฒฝ์šฐ์—๋Š” 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, 90ยฐ์˜ ๊ฐ์„ ์ด๋ฃจ๋ฉด 0, 180ยฐ๋กœ ๋ฐ˜๋Œ€์˜ ๋ฐฉํ–ฅ์„ ๊ฐ€์ง€๋ฉด -1์˜ ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๊ฒฐ๊ตญ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋Š” -1 ์ด์ƒ 1 ์ดํ•˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ๊ฐ’์ด 1์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ์œ ์‚ฌ๋„๊ฐ€ ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•˜๋ฉด ๋‘ ๋ฒกํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐฉํ–ฅ์ด ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ๊ฐ€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒกํ„ฐ A, B์— ๋Œ€ํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋Š” ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. i i a i y c s ( ) A B | | | B | โˆ‘ = n i B โˆ‘ = n ( i ) ร— i 1 ( ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์ด๋‚˜ TF-IDF ํ–‰๋ ฌ์„ ํ†ตํ•ด์„œ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์ด๋‚˜ TF-IDF ํ–‰๋ ฌ์ด ๊ฐ๊ฐ์˜ ํŠน์ง• ๋ฒกํ„ฐ A, B๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์— ๋Œ€ํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ด๋ด…์‹œ๋‹ค. ๋ฌธ์„œ 1 : ์ €๋Š” ์‚ฌ๊ณผ ์ข‹์•„์š” ๋ฌธ์„œ 2 : ์ €๋Š” ๋ฐ”๋‚˜๋‚˜ ์ข‹์•„์š” ๋ฌธ์„œ 3 : ์ €๋Š” ๋ฐ”๋‚˜๋‚˜ ์ข‹์•„์š” ์ €๋Š” ๋ฐ”๋‚˜๋‚˜ ์ข‹์•„์š” ๋„์–ด์“ฐ๊ธฐ ๊ธฐ์ค€ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ์œ„์˜ ์„ธ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์„ ๋งŒ๋“ค๋ฉด ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 0 1 1 1 ๋ฌธ์„œ 2 1 0 1 1 ๋ฌธ์„œ 3 2 0 2 2 Numpy๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ๊ฐ ๋ฌธ์„œ ๋ฒกํ„ฐ ๊ฐ„์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import numpy as np from numpy import dot from numpy.linalg import norm def cos_sim(A, B): return dot(A, B)/(norm(A)*norm(B)) doc1 = np.array([0,1,1,1]) doc2 = np.array([1,0,1,1]) doc3 = np.array([2,0,2,2]) print('๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ์œ ์‚ฌ๋„ :',cos_sim(doc1, doc2)) print('๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 3์˜ ์œ ์‚ฌ๋„ :',cos_sim(doc1, doc3)) print('๋ฌธ์„œ 2์™€ ๋ฌธ์„œ 3์˜ ์œ ์‚ฌ๋„ :',cos_sim(doc2, doc3)) ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ์œ ์‚ฌ๋„ : 0.67 ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 3์˜ ์œ ์‚ฌ๋„ : 0.67 ๋ฌธ์„œ 2๊ณผ ๋ฌธ์„œ 3์˜ ์œ ์‚ฌ๋„ : 1.00 ๋ˆˆ์—ฌ๊ฒจ๋ณผ ๋งŒํ•œ ์ ์€ ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„์™€ ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 3์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๊ฐ€ ๊ฐ™๋‹ค๋Š” ์ ๊ณผ ๋ฌธ์„œ 2์™€ ๋ฌธ์„œ 3์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๊ฐ€ 1์ด ๋‚˜์˜จ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•ž์„œ 1์€ ๋‘ ๋ฒกํ„ฐ์˜ ๋ฐฉํ–ฅ์ด ์™„์ „ํžˆ ๋™์ผํ•œ ๊ฒฝ์šฐ์— 1์ด ๋‚˜์˜ค๋ฉฐ, ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๊ด€์ ์—์„œ๋Š” ์œ ์‚ฌ๋„์˜ ๊ฐ’์ด ์ตœ๋Œ€์ž„์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 3์€ ๋ฌธ์„œ 2์—์„œ ๋‹จ์ง€ ๋ชจ๋“  ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๊ฐ€ 1์”ฉ ์ฆ๊ฐ€ํ–ˆ์„ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ•œ ๋ฌธ์„œ ๋‚ด์˜ ๋ชจ๋“  ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๊ฐ€ ๋™์ผํ•˜๊ฒŒ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ธฐ์กด์˜ ๋ฌธ์„œ์™€ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„์˜ ๊ฐ’์ด 1์ด๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์‹œ์‚ฌํ•˜๋Š” ์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ A์™€ B๊ฐ€ ๋™์ผํ•œ ์ฃผ์ œ์˜ ๋ฌธ์„œ. ๋ฌธ์„œ C๋Š” ๋‹ค๋ฅธ ์ฃผ์ œ์˜ ๋ฌธ์„œ๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์„œ A์™€ ๋ฌธ์„œ C์˜ ๋ฌธ์„œ์˜ ๊ธธ์ด๋Š” ๊ฑฐ์˜ ์ฐจ์ด๊ฐ€ ๋‚˜์ง€ ์•Š์ง€๋งŒ, ๋ฌธ์„œ B์˜ ๊ฒฝ์šฐ ๋ฌธ์„œ A์˜ ๊ธธ์ด๋ณด๋‹ค ๋‘ ๋ฐฐ์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๋กœ ์œ ์‚ฌ๋„๋ฅผ ์—ฐ์‚ฐํ•˜๋ฉด ๋ฌธ์„œ A๊ฐ€ ๋ฌธ์„œ B๋ณด๋‹ค ๋ฌธ์„œ C์™€ ์œ ์‚ฌ๋„๊ฐ€ ๋” ๋†’๊ฒŒ ๋‚˜์˜ค๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์œ ์‚ฌ๋„ ์—ฐ์‚ฐ์— ๋ฌธ์„œ์˜ ๊ธธ์ด๊ฐ€ ์˜ํ–ฅ์„ ๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ, ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๊ฐ€ ํ•ด๊ฒฐ์ฑ…์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋Š” ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ๋•Œ ๋ฒกํ„ฐ์˜ ๋ฐฉํ–ฅ(ํŒจํ„ด)์— ์ดˆ์ ์„ ๋‘๋ฏ€๋กœ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋Š” ๋ฌธ์„œ์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ์ƒํ™ฉ์—์„œ ๋น„๊ต์  ๊ณต์ •ํ•œ ๋น„๊ต๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค. 2. ์œ ์‚ฌ๋„๋ฅผ ์ด์šฉํ•œ ์ถ”์ฒœ ์‹œ์Šคํ…œ ๊ตฌํ˜„ํ•˜๊ธฐ ์บ๊ธ€์—์„œ ์‚ฌ์šฉ๋˜์—ˆ๋˜ ์˜ํ™” ๋ฐ์ดํ„ฐ ์…‹์„ ๊ฐ€์ง€๊ณ  ์˜ํ™” ์ถ”์ฒœ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. TF-IDF์™€ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋งŒ์œผ๋กœ ์˜ํ™”์˜ ์ค„๊ฑฐ๋ฆฌ์— ๊ธฐ๋ฐ˜ํ•ด์„œ ์˜ํ™”๋ฅผ ์ถ”์ฒœํ•˜๋Š” ์ถ”์ฒœ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://www.kaggle.com/rounakbanik/the-movies-dataset ์›๋ณธ ํŒŒ์ผ์€ ์œ„ ๋งํฌ์—์„œ movies_metadata.csv ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ์ด 24๊ฐœ์˜ ์—ด์„ ๊ฐ€์ง„ 45,466๊ฐœ์˜ ์ƒ˜ํ”Œ๋กœ ๊ตฌ์„ฑ๋œ ์˜ํ™” ์ •๋ณด ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. import pandas as pd from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import cosine_similarity data = pd.read_csv('movies_metadata.csv', low_memory=False) data.head(2) ๋‹ค์šด๋กœ๋“œํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ์ƒ์œ„ 2๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜<NAME>์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ... original_title overview ... title video vote_average vote_count 0 ... Toy Story Led by Woody, Andy's toys live happily in his ... ์ค‘๋žต ... ... Toy Story False 7.7 5415.0 1 ... Jumanji When siblings Judy and Peter discover an encha ... ์ค‘๋žต ... ... Jumanji False 6.9 2413.0 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์ด 24๊ฐœ์˜ ์—ด์„ ๊ฐ–๊ณ  ์žˆ์œผ๋‚˜ ์ฑ…์˜ ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ์ผ๋ถ€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„์— ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ์˜ํ™” ์ œ๋ชฉ์— ํ•ด๋‹นํ•˜๋Š” title ์—ด๊ณผ ์ค„๊ฑฐ๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” overview ์—ด์ž…๋‹ˆ๋‹ค. ์ข‹์•„ํ•˜๋Š” ์˜ํ™”๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, ํ•ด๋‹น ์˜ํ™”์˜ ์ค„๊ฑฐ๋ฆฌ์™€ ์œ ์‚ฌํ•œ ์ค„๊ฑฐ๋ฆฌ์˜ ์˜ํ™”๋ฅผ ์ฐพ์•„์„œ ์ถ”์ฒœํ•˜๋Š” ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. # ์ƒ์œ„ 2๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์„ data์— ์ €์žฅ data = data.head(20000) ๋งŒ์•ฝ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ์ค„์ด๊ณ  ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ค„์—ฌ์„œ ์žฌ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ƒ์œ„ 20,000๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. TF-IDF๋ฅผ ์—ฐ์‚ฐํ•  ๋•Œ ๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ๋“ค์–ด์žˆ์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. TF-IDF์˜ ๋Œ€์ƒ์ด ๋˜๋Š” data์˜ overview ์—ด์— ๊ฒฐ์ธก๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # overview ์—ด์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฒฐ์ธก๊ฐ’์„ ์ „๋ถ€ ์นด์šดํŠธํ•˜์—ฌ ์ถœ๋ ฅ print('overview ์—ด์˜ ๊ฒฐ์ธก๊ฐ’์˜ ์ˆ˜:',data['overview'].isnull().sum()) overview ์—ด์˜ ๊ฒฐ์ธก๊ฐ’์˜ ์ˆ˜: 135 135๊ฐœ์˜ Null ๊ฐ’์ด ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ฒฐ์ธก๊ฐ’์„ ๊ฐ€์ง„ ํ–‰์„ ์ œ๊ฑฐํ•˜๋Š” pandas์˜ dropna()๋‚˜ ๊ฒฐ์ธก๊ฐ’์ด ์žˆ๋˜ ํ–‰์— ํŠน์ • ๊ฐ’์œผ๋กœ ์ฑ„์›Œ ๋„ฃ๋Š” pandas์˜ fillna()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด„ํ˜ธ ์•ˆ์— Null ๋Œ€์‹  ๋„ฃ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๋˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ๋นˆ ๊ฐ’(empty value)์œผ๋กœ ๋Œ€์ฒดํ•˜์˜€์Šต๋‹ˆ๋‹ค. # ๊ฒฐ์ธก๊ฐ’์„ ๋นˆ ๊ฐ’์œผ๋กœ ๋Œ€์ฒด data['overview'] = data['overview'].fillna('') Null ๊ฐ’์„ ๋นˆ ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•˜์˜€์Šต๋‹ˆ๋‹ค. overview ์—ด์— ๋Œ€ํ•ด์„œ TF-IDF ํ–‰๋ ฌ์„ ๊ตฌํ•œ ํ›„ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. tfidf = TfidfVectorizer(stop_words='english') tfidf_matrix = tfidf.fit_transform(data['overview']) print('TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) :',tfidf_matrix.shape) TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) : (20000, 47487) TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” 20,000์˜ ํ–‰์„ ๊ฐ€์ง€๊ณ  47,847์˜ ์—ด์„ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด 20,000๊ฐœ์˜ ์˜ํ™”๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด 47,487๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” 47,847์ฐจ์›์˜ ๋ฌธ์„œ ๋ฒกํ„ฐ๊ฐ€ 20,000๊ฐœ๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ ๋„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. ์ด์ œ 20,000๊ฐœ์˜ ๋ฌธ์„œ ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ์ƒํ˜ธ ๊ฐ„์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix) print('์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ :',cosine_sim.shape) ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ : (20000, 20000) ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋กœ๋Š” 20,000ํ–‰ 20,000์—ด์˜ ํ–‰๋ ฌ์„ ์–ป์Šต๋‹ˆ๋‹ค. ์ด๋Š” 20,000๊ฐœ์˜ ๊ฐ ๋ฌธ์„œ ๋ฒกํ„ฐ(์˜ํ™” ์ค„๊ฑฐ๋ฆฌ ๋ฒกํ„ฐ)์™€ ์ž๊ธฐ ์ž์‹ ์„ ํฌํ•จํ•œ 20,000๊ฐœ์˜ ๋ฌธ์„œ ๋ฒกํ„ฐ ๊ฐ„์˜ ์œ ์‚ฌ๋„๊ฐ€ ๊ธฐ๋ก๋œ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  20,000๊ฐœ ์˜ํ™”์˜ ์ƒํ˜ธ ์œ ์‚ฌ๋„๊ฐ€ ๊ธฐ๋ก๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ธฐ์กด ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ๋ถ€ํ„ฐ ์˜ํ™”์˜ ํƒ€์ดํ‹€์„ key, ์˜ํ™”์˜ ์ธ๋ฑ์Šค๋ฅผ value๋กœ ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ title_to_index๋ฅผ ๋งŒ๋“ค์–ด๋‘ก๋‹ˆ๋‹ค. title_to_index = dict(zip(data['title'], data.index)) # ์˜ํ™” ์ œ๋ชฉ Father of the Bride Part II์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ด idx = title_to_index['Father of the Bride Part II'] print(idx) ์„ ํƒํ•œ ์˜ํ™”์˜ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜๋ฉด ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ overview๊ฐ€ ์œ ์‚ฌํ•œ 10๊ฐœ์˜ ์˜ํ™”๋ฅผ ์ฐพ์•„๋‚ด๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. def get_recommendations(title, cosine_sim=cosine_sim): # ์„ ํƒํ•œ ์˜ํ™”์˜ ํƒ€์ดํ‹€๋กœ๋ถ€ํ„ฐ ํ•ด๋‹น ์˜ํ™”์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ›์•„์˜จ๋‹ค. idx = title_to_index[title] # ํ•ด๋‹น ์˜ํ™”์™€ ๋ชจ๋“  ์˜ํ™”์™€์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. sim_scores = list(enumerate(cosine_sim[idx])) # ์œ ์‚ฌ๋„์— ๋”ฐ๋ผ ์˜ํ™”๋“ค์„ ์ •๋ ฌํ•œ๋‹ค. sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True) # ๊ฐ€์žฅ ์œ ์‚ฌํ•œ 10๊ฐœ์˜ ์˜ํ™”๋ฅผ ๋ฐ›์•„์˜จ๋‹ค. sim_scores = sim_scores[1:11] # ๊ฐ€์žฅ ์œ ์‚ฌํ•œ 10๊ฐœ์˜ ์˜ํ™”์˜ ์ธ๋ฑ์Šค๋ฅผ ์–ป๋Š”๋‹ค. movie_indices = [idx[0] for idx in sim_scores] # ๊ฐ€์žฅ ์œ ์‚ฌํ•œ 10๊ฐœ์˜ ์˜ํ™”์˜ ์ œ๋ชฉ์„ ๋ฆฌํ„ดํ•œ๋‹ค. return data['title'].iloc[movie_indices] ์˜ํ™” ๋‹คํฌ ๋‚˜์ดํŠธ ๋ผ์ด์ฆˆ์™€ overview๊ฐ€ ์œ ์‚ฌํ•œ ์˜ํ™”๋“ค์„ ์ฐพ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. get_recommendations('The Dark Knight Rises') 12481 The Dark Knight 150 Batman Forever 1328 Batman Returns 15511 Batman: Under the Red Hood 585 Batman 9230 Batman Beyond: Return of the Joker 18035 Batman: Year One 19792 Batman: The Dark Knight Returns, Part 1 3095 Batman: Mask of the Phantasm 10122 Batman Begins Name: title, dtype: object ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์˜ํ™”๊ฐ€ ์ถœ๋ ฅ๋˜๋Š”๋ฐ, ์˜ํ™” ๋‹คํฌ ๋‚˜์ดํŠธ๊ฐ€ ์ฒซ ๋ฒˆ์งธ๊ณ , ๊ทธ ์™ธ์—๋„ ์ „๋ถ€ ๋ฐฐํŠธ๋งจ ์˜ํ™”๋ฅผ ์ฐพ์•„๋‚ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 05-02์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์œ ์‚ฌ๋„ ๊ธฐ๋ฒ• ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ์™ธ์—๋„ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋“ค์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ(Euclidean distance) ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ(euclidean distance)๋Š” ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ๋•Œ ์ž์นด๋ฅด ์œ ์‚ฌ๋„๋‚˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋งŒํผ, ์œ ์šฉํ•œ ๋ฐฉ๋ฒ•์€ ์•„๋‹™๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•˜๊ณ , ์‹œ๋„ํ•ด ๋ณด๋Š” ๊ฒƒ ์ž์ฒด๋งŒ์œผ๋กœ ๋‹ค๋ฅธ ๊ฐœ๋…๋“ค์„ ์ดํ•ดํ•  ๋•Œ ๋„์›€์ด ๋˜๋ฏ€๋กœ ์˜๋ฏธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์ฐจ์› ๊ณต๊ฐ„์—์„œ ๋‘ ๊ฐœ์˜ ์  ์™€ ๊ฐ€ ๊ฐ๊ฐ = ( 1 p, 3. . p) q ( 1 q, 3. . q) ์˜ ์ขŒํ‘œ๋ฅผ ๊ฐ€์งˆ ๋•Œ ๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( 1 p) + ( 2 p) + . . + ( n p) = i 1 ( i p) ๋‹ค์ฐจ์› ๊ณต๊ฐ„์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด, ์ฒ˜์Œ ๋ณด๋Š” ์ž…์žฅ์—์„œ๋Š” ์‹์ด ๋„ˆ๋ฌด ๋ณต์žกํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. ์ข€ ๋” ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ 2์ฐจ์› ๊ณต๊ฐ„์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ขŒํ‘œ ํ‰๋ฉด ์ƒ์—์„œ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2์ฐจ์› ์ขŒํ‘œ ํ‰๋ฉด ์ƒ์—์„œ ๋‘ ์  ์™€ ์‚ฌ์ด์˜ ์ง์„ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ฒฝ์šฐ์—๋Š” ์ง๊ฐ ์‚ผ๊ฐํ˜•์œผ๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ์ค‘ํ•™๊ต ์ˆ˜ํ•™ ๊ณผ์ •์ธ ํ”ผํƒ€๊ณ ๋ผ์Šค์˜ ์ •๋ฆฌ๋ฅผ ํ†ตํ•ด ์™€ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, 2์ฐจ์› ์ขŒํ‘œ ํ‰๋ฉด์—์„œ ๋‘ ์  ์‚ฌ์ด์˜ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ ๊ณต์‹์€ ํ”ผํƒ€๊ณ ๋ผ์Šค์˜ ์ •๋ฆฌ๋ฅผ ํ†ตํ•ด ๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ์›์ ์œผ๋กœ ๋Œ์•„๊ฐ€์„œ ์—ฌ๋Ÿฌ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ณ ์ž ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ ๊ณต์‹์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์€, ์•ž์„œ ๋ณธ 2์ฐจ์›์„ ๋‹จ์–ด์˜ ์ด๊ฐœ์ˆ˜๋งŒํผ์˜ ์ฐจ์›์œผ๋กœ ํ™•์žฅํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์€ DTM์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 2 3 0 1 ๋ฌธ์„œ 2 1 2 3 1 ๋ฌธ์„œ 3 2 1 2 2 ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ 4๊ฐœ์ด๋ฏ€๋กœ, ์ด๋Š” 4์ฐจ์› ๊ณต๊ฐ„์— ๋ฌธ์„œ 1, ๋ฌธ์„œ 2, ๋ฌธ์„œ 3์„ ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์„œ Q์— ๋Œ€ํ•ด์„œ ๋ฌธ์„œ 1, ๋ฌธ์„œ 2, ๋ฌธ์„œ 3 ์ค‘ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋‚ด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ Q 1 1 0 1 ์ด๋•Œ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๋ฅผ ํ†ตํ•ด ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด, ๋ฌธ์„œ Q ๋˜ํ•œ ๋‹ค๋ฅธ ๋ฌธ์„œ๋“ค์ฒ˜๋Ÿผ 4์ฐจ์› ๊ณต๊ฐ„์— ๋ฐฐ์น˜์‹œ์ผฐ๋‹ค๋Š” ๊ด€์ ์—์„œ 4์ฐจ์› ๊ณต๊ฐ„์—์„œ์˜ ๊ฐ๊ฐ์˜ ๋ฌธ์„œ๋“ค๊ณผ์˜ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import numpy as np def dist(x, y): return np.sqrt(np.sum((x-y)**2)) doc1 = np.array((2,3,0,1)) doc2 = np.array((1,2,3,1)) doc3 = np.array((2,1,2,2)) docQ = np.array((1,1,0,1)) print('๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ :',dist(doc1, docQ)) print('๋ฌธ์„œ 2๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ :',dist(doc2, docQ)) print('๋ฌธ์„œ 3๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ :',dist(doc3, docQ)) ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ : 2.23606797749979 ๋ฌธ์„œ 2๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ : 3.1622776601683795 ๋ฌธ์„œ 3๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ : 2.449489742783178 ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ์˜ ๊ฐ’์ด ๊ฐ€์žฅ ์ž‘๋‹ค๋Š” ๊ฒƒ์€ ๋ฌธ์„œ ๊ฐ„ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ๊ฐ€๊น๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฌธ์„œ 1์ด ๋ฌธ์„œ Q์™€ ๊ฐ€์žฅ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. ์ž์นด๋ฅด ์œ ์‚ฌ๋„(Jaccard similarity) A์™€ B ๋‘ ๊ฐœ์˜ ์ง‘ํ•ฉ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋•Œ ๊ต์ง‘ํ•ฉ์€ ๋‘ ๊ฐœ์˜ ์ง‘ํ•ฉ์—์„œ ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์›์†Œ๋“ค์˜ ์ง‘ํ•ฉ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ•ฉ์ง‘ํ•ฉ์—์„œ ๊ต์ง‘ํ•ฉ์˜ ๋น„์œจ์„ ๊ตฌํ•œ๋‹ค๋ฉด ๋‘ ์ง‘ํ•ฉ A์™€ B์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ์ž์นด๋ฅด ์œ ์‚ฌ๋„(jaccard similarity)์˜ ์•„์ด๋””์–ด์ž…๋‹ˆ๋‹ค. ์ž์นด๋ฅด ์œ ์‚ฌ๋„๋Š” 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, ๋งŒ์•ฝ ๋‘ ์ง‘ํ•ฉ์ด ๋™์ผํ•˜๋‹ค๋ฉด 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ , ๋‘ ์ง‘ํ•ฉ์˜ ๊ณตํ†ต ์›์†Œ๊ฐ€ ์—†๋‹ค๋ฉด 0์˜ ๊ฐ’์„ ๊ฐ–์Šต๋‹ˆ๋‹ค. ์ž์นด๋ฅด ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ž์นด๋ฅด ์œ ์‚ฌ๋„ ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( , ) | โˆฉ | A B = A B | | | | | โˆฉ | ๋‘ ๊ฐœ์˜ ๋น„๊ตํ•  ๋ฌธ์„œ๋ฅผ ๊ฐ๊ฐ o 1 d c๋ผ๊ณ  ํ–ˆ์„ ๋•Œ o 1 d c์˜ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์ž์นด๋ฅด ์œ ์‚ฌ๋„๋Š” ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( o 1 d c) d c โˆฉ o 2 o 1 d c ๋‘ ๋ฌธ์„œ o 1 d c ์‚ฌ์ด์˜ ์ž์นด๋ฅด ์œ ์‚ฌ๋„ ( o 1 d c) ๋Š” ๋‘ ์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ ํฌ๊ธฐ๋ฅผ ๋‘ ์ง‘ํ•ฉ์˜ ํ•ฉ์ง‘ํ•ฉ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์œผ๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ํ†ตํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. doc1 = "apple banana everyone like likey watch card holder" doc2 = "apple banana coupon passport love you" # ํ† ํฐํ™” tokenized_doc1 = doc1.split() tokenized_doc2 = doc2.split() print('๋ฌธ์„œ 1 :',tokenized_doc1) print('๋ฌธ์„œ 2 :',tokenized_doc2) ๋ฌธ์„œ 1 : ['apple', 'banana', 'everyone', 'like', 'likey', 'watch', 'card', 'holder'] ๋ฌธ์„œ 2 : ['apple', 'banana', 'coupon', 'passport', 'love', 'you'] ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ํ•ฉ์ง‘ํ•ฉ์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. union = set(tokenized_doc1).union(set(tokenized_doc2)) print('๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ํ•ฉ์ง‘ํ•ฉ :',union) ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ํ•ฉ์ง‘ํ•ฉ : {'you', 'passport', 'watch', 'card', 'love', 'everyone', 'apple', 'likey', 'like', 'banana', 'holder', 'coupon'} ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ํ•ฉ์ง‘ํ•ฉ์˜ ๋‹จ์–ด์˜ ์ด๊ฐœ์ˆ˜๋Š” 12๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์—์„œ ๋‘˜ ๋‹ค ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋ฅผ ์ฐพ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. intersection = set(tokenized_doc1).intersection(set(tokenized_doc2)) print('๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ๊ต์ง‘ํ•ฉ :',intersection) ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ๊ต์ง‘ํ•ฉ : {'apple', 'banana'} ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์—์„œ ๋‘˜ ๋‹ค ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋Š” banana์™€ apple ์ด 2๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๊ต์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ•ฉ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆ„๋ฉด ์ž์นด๋ฅด ์œ ์‚ฌ๋„๊ฐ€ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. print('์ž์นด๋ฅด ์œ ์‚ฌ๋„ :',len(intersection)/len(union)) ์ž์นด๋ฅด ์œ ์‚ฌ๋„ : 0.16666666666666666 06. ๋จธ์‹  ๋Ÿฌ๋‹(Machine Learning) ๊ฐœ์š” ๋จธ์‹  ๋Ÿฌ๋‹์€ ์˜์ƒ ์ฒ˜๋ฆฌ, ๋ฒˆ์—ญ๊ธฐ, ์Œ์„ฑ ์ธ์‹, ์ŠคํŒธ ๋ฉ”์ผ ํƒ์ง€ ๋“ฑ ๊ต‰์žฅํžˆ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ์‘์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ๋จธ์‹  ๋Ÿฌ๋‹์˜ ํ•œ ๊ฐˆ๋ž˜์ธ ๋”ฅ ๋Ÿฌ๋‹์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์—”์ง€๋‹ˆ์–ด์—๊ฒŒ ํ•„์ˆ˜ ์—ญ๋Ÿ‰์ด ๋˜์–ด๊ฐ€๊ณ  ์žˆ์Šต์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹์˜ ๊ฐœ๋…๊ณผ ์„ ํ˜• ํšŒ๊ท€, ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€, ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์™€ ๊ฐ™์€ ๊ธฐ๋ณธ์ ์ธ ๋ชจ๋ธ์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ์ดํ•ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋‹ค์Œ ๋”ฅ ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ์—์„œ ๊ธฐ๋ณธ์ ์ธ ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ๋กœ ๊ฐœ๋…์„ ํ™•์žฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 06-01 ๋จธ์‹  ๋Ÿฌ๋‹์ด๋ž€(What is Machine Learning?) ๋”ฅ ๋Ÿฌ๋‹์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฐœ๋…์ธ ๋จธ์‹  ๋Ÿฌ๋‹(Machine Learning)์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ๋จธ์‹  ๋Ÿฌ๋‹(Machine Learning)์ด ์•„๋‹Œ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์˜ ํ•œ๊ณ„ ๋จธ์‹  ๋Ÿฌ๋‹์ด ์•„๋‹Œ ๊ธฐ์กด์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž‘์„ฑ ๋ฐฉ์‹์„ ํ†ตํ•ด์„œ๋Š” ํ•ด๊ฒฐํ•˜๊ธฐ ์–ด๋ ค์šด ๋ฌธ์ œ ์˜ˆ์‹œ๋ฅผ ํ•˜๋‚˜ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ : ์ฃผ์–ด์ง„ ์‚ฌ์ง„์œผ๋กœ๋ถ€ํ„ฐ ๊ณ ์–‘์ด ์‚ฌ์ง„์ธ์ง€ ๊ฐ•์•„์ง€ ์‚ฌ์ง„์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ์ผ. ์œ„ ๋ฌธ์ œ๋Š” ์‹ค์ œ 2017๋…„์— ์žˆ์—ˆ๋˜ DGIST์˜ ๋”ฅ ๋Ÿฌ๋‹<NAME>๋Œ€ํšŒ์˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์‚ฌ์ง„์„ ๋ณด๊ณ  ๊ณ ์–‘์ด ์‚ฌ์ง„์ธ์ง€, ๊ฐ•์•„์ง€ ์‚ฌ์ง„์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ๊ฑด ์‚ฌ๋žŒ์—๊ฒŒ๋Š” ๋„ˆ๋ฌด๋‚˜ ์‰ฌ์šด ์ผ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์ƒ๋‹นํžˆ ๋‚œํ•ดํ•œ ์ˆ˜์ค€์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ๋œ ์ด๋ฏธ์ง€๋กœ๋ถ€ํ„ฐ ๊ฐ•์•„์ง€์™€ ๊ณ ์–‘์ด๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? def prediction(์ด๋ฏธ์ง€ as input): ์–ด๋–ป๊ฒŒ ์ฝ”๋”ฉํ•ด์•ผ ํ•˜์ง€? return ๊ฒฐ๊ณผ ์‚ฌ์ง„์ด๋ž€ ๊ฑด ์‚ฌ์ง„์„ ๋ณด๋Š” ๊ฐ๋„, ์กฐ๋ช…, ํƒ€๊นƒ์˜ ๋ณ€ํ˜•(๊ณ ์–‘์ด์˜ ์ž์„ธ)์— ๋”ฐ๋ผ์„œ ๋„ˆ๋ฌด๋‚˜ ์ฒœ์ฐจ๋งŒ๋ณ„์ด๋ผ ์‚ฌ์ง„์œผ๋กœ๋ถ€ํ„ฐ ๊ณตํ†ต๋œ ๋ช…ํ™•ํ•œ ํŠน์ง•์„ ์žก์•„๋‚ด๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค, ๊ฒฐ๋ก ์„ ๋ฏธ๋ฆฌ ๋ง์”€๋“œ๋ฆฌ๋ฉด ํ•ด๋‹น ํ”„๋กœ๊ทธ๋žจ์€ ์ˆซ์ž๋ฅผ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ช…ํ™•ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์• ์ดˆ์— ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด๋ฏธ์ง€ ์ธ์‹ ๋ถ„์•ผ์—์„œ ๊ทœ์น™์„ ์ •์˜ํ•˜๊ณ  ํŠน์ง•์„ ์žก์•„๋‚ด๊ธฐ ์œ„ํ•œ ๋งŽ์€ ์‹œ๋„๋“ค์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ๋‚ด์˜ ๊ฒฝ๊ณ„์„ ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ฐพ์•„๋‚ด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ํ™”ํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๊ณ , ๋‹ค๋ฅธ ์‚ฌ์ง„ ์ด๋ฏธ์ง€๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ์ „๋ฐ˜์ ์ธ ์ƒํƒœ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋ถ„๋ฅ˜ํ•˜๋ ค๊ณ  ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋Ÿฌํ•œ ์‹œ๋„๋“ค์€ ๊ฒฐ๊ตญ ํŠน์ง•์„ ์žก์•„๋‚ด๋Š” ๊ฒƒ์— ํ•œ๊ณ„๊ฐ€ ์žˆ์„ ์ˆ˜๋ฐ–์— ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์š”์ฆ˜์— ์ด๋ฅด๋Ÿฌ์„œ๋Š” ์‚ฌ์ง„์œผ๋กœ๋ถ€ํ„ฐ ๋Œ€์ƒ์„ ์ฐพ์•„๋‚ด๋Š” ์ผ์€ ์‚ฌ๋žŒ์ด ๊ทœ์น™์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋จธ์‹  ๋Ÿฌ๋‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 2. ๋จธ์‹  ๋Ÿฌ๋‹ ๋ฐฉ์‹ ๋จธ์‹  ๋Ÿฌ๋‹์ด ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์˜ˆ์‹œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์ ‘๊ทผ ๋ฐฉ์‹์ด ๊ธฐ์กด์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹๊ณผ๋Š” ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์œ„ ์ด๋ฏธ์ง€์—์„œ ์œ„์ชฝ์€ ๊ธฐ์กด์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์ ‘๊ทผ ๋ฐฉ์‹, ์•„๋ž˜์ชฝ์€ ๋จธ์‹  ๋Ÿฌ๋‹์˜ ์ ‘๊ทผ ๋ฐฉ์‹์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ๊ธฐ๊ณ„๊ฐ€ ์Šค์Šค๋กœ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๊ทœ์น™์„ฑ์„ ์ฐพ๋Š” ๊ฒƒ์— ์ง‘์ค‘ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๊ทœ์น™์„ฑ์„ ์ฐพ๋Š” ๊ณผ์ •์„ ์šฐ๋ฆฌ๋Š” ํ›ˆ๋ จ(training) ๋˜๋Š” ํ•™์Šต(learning)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋‹จ ๊ทœ์น™์„ฑ์„ ๋ฐœ๊ฒฌํ•˜๊ณ  ๋‚˜๋ฉด, ๊ทธ ํ›„์— ๋“ค์–ด์˜ค๋Š” ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋ฐœ๊ฒฌํ•œ ๊ทœ์น™์„ฑ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋‹ต์„ ์ฐพ์•„๋‚ด๋Š”๋ฐ ์ด๋Š” ๊ธฐ์กด์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์–ด๋ ค์› ๋˜ ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ์ฑ…์ด ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค์—ˆ์ง€๋งŒ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋„ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ๋งŒํผ์ด๋‚˜ ์–ด๋ ค์šด ๋ฌธ์ œ๋“ค์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์—๋Š” ๋จธ์‹  ๋Ÿฌ๋‹์˜ ํ•œ ๊ฐˆ๋ž˜์ธ ๋”ฅ ๋Ÿฌ๋‹์ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๊ต‰์žฅํžˆ ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์ ์œผ๋กœ, ๊ตฌ๊ธ€ ๋ฒˆ์—ญ๊ธฐ์™€ ๊ฐ™์€ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๊ฐ€ ๊ทธ๋Ÿฌํ•œ๋ฐ, ์ด๋Ÿฌํ•œ ๋ฒˆ์—ญ๊ธฐ๋Š” ์‚ฌ๋žŒ์ด ์ง์ ‘ ๊ทœ์น™์„ ์ •์˜ํ•ด์„œ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ณด๋‹ค ๋”ฅ ๋Ÿฌ๋‹์œผ๋กœ ๋ชจ๋ธ์ด ์Šค์Šค๋กœ ๊ทœ์น™์„ ์ฐพ์•„๋‚ด๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 06-02 ๋จธ์‹  ๋Ÿฌ๋‹ ํ›‘์–ด๋ณด๊ธฐ ๋จธ์‹  ๋Ÿฌ๋‹์˜ ํŠน์ง•์„ ์ดํ•ดํ•˜๊ณ , ์ฃผ์š” ์šฉ์–ด์— ๋ฏธ๋ฆฌ ์นœ์ˆ™ํ•ด์ ธ๋ด…์‹œ๋‹ค. 1. ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ํ‰๊ฐ€ ๋จธ์‹  ๋Ÿฌ๋‹์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ–ˆ๋‹ค๋ฉด ๊ธฐ๊ณ„๋ฅผ ํ•™์Šตํ•˜๊ธฐ ์ „ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ์šฉ, ๊ฒ€์ฆ์šฉ, ํ…Œ์ŠคํŠธ์šฉ ์ด๋ ‡๊ฒŒ ์„ธ ๊ฐ€์ง€๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š” ์šฉ๋„์ž…๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” ํ•™์Šตํ•œ ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ์˜ ์šฉ๋„๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๊ฐ€ ์•„๋‹ˆ๋ผ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„์ž…๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํžˆ๋Š” ๋ชจ๋ธ์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ ์ ํ•ฉ(overfitting) ์ด ๋˜๊ณ  ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๊ฑฐ๋‚˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์กฐ์ •์„ ์œ„ํ•œ ์šฉ๋„์ž…๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ผ๋Š” ์šฉ์–ด๋ฅผ ์ •๋ฆฌํ•ด๋‘ก์‹œ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ(์ดˆ ๋งค๊ฐœ๋ณ€์ˆ˜) : ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์‚ฌ๋žŒ์ด ๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๋ณ€์ˆ˜. ๋งค๊ฐœ๋ณ€์ˆ˜ : ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ. ํ•™์Šต์„ ํ•˜๋Š” ๋™์•ˆ ๊ฐ’์ด ๊ณ„์†ํ•ด์„œ ๋ณ€ํ•˜๋Š” ์ˆ˜. ์•„์ง ์ด ์žฅ์—์„œ๋Š” ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ์™€ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐœ๋…์ด ์–ด๋–ค ์˜๋ฏธ์ธ์ง€ ์™€๋‹ฟ์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐœ๋…์€ ์•ž์œผ๋กœ ์ง€์†์ ์œผ๋กœ ์–ธ๊ธ‰ํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋‘ ๋ณ€์ˆ˜์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋ณดํ†ต ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋’ค์˜ ์„ ํ˜• ํšŒ๊ท€์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์—์„œ ํ•™์Šต๋ฅ (learning rate)์ด๋‚˜, ๋”ฅ ๋Ÿฌ๋‹์—์„œ ๋‰ด๋Ÿฐ์˜ ์ˆ˜๋‚˜ ์ธต์˜ ์ˆ˜์™€ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ๋Œ€ํ‘œ์ ์ธ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ๊ณผ ๊ฐ™์€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒฐ์ •ํ•ด ์ฃผ๋Š” ๊ฐ’์ด ์•„๋‹ˆ๋ผ ๋ชจ๋ธ์ด ํ•™์Šตํ•˜๋Š” ๊ณผ์ •์—์„œ ์–ป์–ด์ง€๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ์šฉ ๋ฐ์ดํ„ฐ๋กœ ํ›ˆ๋ จ์„ ๋ชจ๋‘ ์‹œํ‚จ ๋ชจ๋ธ์€ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •ํ™•๋„๋ฅผ ๊ฒ€์ฆํ•˜๋ฉฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํŠœ๋‹(tuning) ํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋†’์€ ์ •ํ™•๋„๋ฅผ ์–ป๋„๋ก ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์„ ๋ฐ”๊ฟ”๋ณด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํŠœ๋‹ํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ชจ๋ธ์€ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„๋ฅผ ๋†’์ด๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ ์ฐจ์ ์œผ๋กœ ์ˆ˜์ •๋ฉ๋‹ˆ๋‹ค. ํŠœ๋‹ ๊ณผ์ •์„ ๋ชจ๋‘ ๋๋‚ด๊ณ  ๋ชจ๋ธ์˜ ์ตœ์ข… ํ‰๊ฐ€๋ฅผ ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, ์ด์ œ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ ์ˆ˜์ •๋ผ ์˜จ ๋ชจ๋ธ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์— ๋Œ€ํ•œ ํ‰๊ฐ€๋Š” ์ด์ œ ๋ชจ๋ธ์ด ํ•œ ๋ฒˆ๋„ ๋ณด์ง€ ๋ชปํ•œ ๋ฐ์ดํ„ฐ์ธ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋ชซ์ž…๋‹ˆ๋‹ค. ์ˆ˜ํ•™ ๋Šฅ๋ ฅ ์‹œํ—˜์„ ์ค€๋น„ํ•˜๋Š” ์ˆ˜ํ—˜์ƒ์œผ๋กœ ๋น„์œ ํ•˜์ž๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์‹ค์ œ ๊ณต๋ถ€๋ฅผ ์œ„ํ•œ ๋ฌธ์ œ์ง€, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ์˜๊ณ ์‚ฌ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” ์‹ค๋ ฅ์„ ์ตœ์ข…์ ์œผ๋กœ ํ‰๊ฐ€ํ•˜๋Š” ์ˆ˜๋Šฅ ์‹œํ—˜์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. ๋ถ„๋ฅ˜(Classification)์™€ ํšŒ๊ท€(Regression) ์ „๋ถ€๋Š” ์•„๋‹ˆ์ง€๋งŒ ๋จธ์‹  ๋Ÿฌ๋‹์˜ ๋งŽ์€ ๋ฌธ์ œ๋Š” ๋ถ„๋ฅ˜ ๋˜๋Š” ํšŒ๊ท€ ๋ฌธ์ œ์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ๊ธฐ๋ฒ• ์ค‘ ์„ ํ˜• ํšŒ๊ท€(Lineare Regression)์™€ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€(Logistic Regression)๋ฅผ ๋‹ค๋ฃจ๋Š”๋ฐ ์„ ํ˜• ํšŒ๊ท€๋Š” ๋Œ€ํ‘œ์ ์ธ ํšŒ๊ท€ ๋ฌธ์ œ์— ์†ํ•˜๊ณ , ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋Š” (์ด๋ฆ„์€ ํšŒ๊ท€์ด์ง€๋งŒ) ๋Œ€ํ‘œ์ ์ธ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์— ์†ํ•ฉ๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜๋Š” ๋˜ํ•œ ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification)๊ณผ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-Class Classification)๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์—„๋ฐ€ํžˆ๋Š” ๋‹ค์ค‘ ๋ ˆ์ด๋ธ” ๋ถ„๋ฅ˜(Multi-lable Classification)๋ผ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ, ์ด ์ฑ…์—์„œ๋Š” ์ด์ง„ ๋ถ„๋ฅ˜์™€ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋งŒ์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 1) ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ(Binary Classification) ์ด์ง„ ๋ถ„๋ฅ˜๋Š” ์ฃผ์–ด์ง„ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜์˜ ๋‹ต์„ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ข…ํ•ฉ์‹œํ—˜ ์„ฑ์ ํ‘œ๋ฅผ ๋ณด๊ณ  ์ตœ์ข…์ ์œผ๋กœ ํ•ฉ๊ฒฉ, ๋ถˆํ•ฉ๊ฒฉ์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ๋ฌธ์ œ, ๋ฉ”์ผ์„ ๋ณด๊ณ  ๋‚˜์„œ ์ •์ƒ ๋ฉ”์ผ, ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋ฌธ์ œ ๋“ฑ์ด ์ด์— ์†ํ•ฉ๋‹ˆ๋‹ค. 2) ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-class Classification) ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋Š” ์ฃผ์–ด์ง„ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์„ธ ๊ฐœ ์ด์ƒ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ๋‹ต์„ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์„œ์  ์ง์›์ด ์ผ์„ ํ•˜๋Š”๋ฐ ๊ณผํ•™, ์˜์–ด, IT, ํ•™์Šต์ง€, ๋งŒํ™”๋ผ๋Š” ๋ ˆ์ด๋ธ”์ด ๋ถ™์–ด์žˆ๋Š” 5๊ฐœ์˜ ์ฑ…์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ƒˆ ์ฑ…์ด ์ž…๊ณ ๋˜๋ฉด, ์ด ์ฑ…์€ ๋‹ค์„ฏ ๊ฐœ์˜ ์ฑ…์žฅ ์ค‘์—์„œ ๋ถ„์•ผ์— ๋งž๋Š” ์ ์ ˆํ•œ ์ฑ…์žฅ์— ์ฑ…์„ ๋„ฃ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” ํ˜„์‹ค์—์„œ์˜ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. 3) ํšŒ๊ท€ ๋ฌธ์ œ(Regression) ํšŒ๊ท€ ๋ฌธ์ œ๋Š” ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ฒ˜๋Ÿผ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์•ผ ํ•œ๋‹ค๊ฑฐ๋‚˜, ์ฑ…์ด ์ž…๊ณ ๋˜์—ˆ์„ ๋•Œ 5๊ฐœ์˜ ์ฑ…์žฅ ์ค‘ ํ•˜๋‚˜์˜ ์ฑ…์žฅ์„ ๊ณจ๋ผ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์ฒ˜๋Ÿผ ์ •๋‹ต์ด ๋ช‡ ๊ฐœ์˜ ์ •ํ•ด์ง„ ์„ ํƒ์ง€ ์ค‘์—์„œ ์ •ํ•ด์ ธ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ ์–ด๋– ํ•œ ์—ฐ์†์ ์ธ ๊ฐ’์˜ ๋ฒ”์œ„ ๋‚ด์—์„œ ์˜ˆ์ธก๊ฐ’์ด ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์—ญ๊ณผ์˜ ๊ฑฐ๋ฆฌ, ์ธ๊ตฌ ๋ฐ€๋„, ๋ฐฉ์˜ ๊ฐœ์ˆ˜ ๋“ฑ์„ ์ž…๋ ฅํ•˜๋ฉด ๋ถ€๋™์‚ฐ ๊ฐ€๊ฒฉ์„ ์˜ˆ์ธกํ•˜๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ์ด ๋ถ€๋™์‚ฐ ๊ฐ€๊ฒฉ์„ 7์–ต 8,456๋งŒ 3,450์›์œผ๋กœ ์˜ˆ์ธกํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์„ ๊ฒƒ์ด๊ณ , 8์–ต 1257๋งŒ 300์›์œผ๋กœ ์˜ˆ์ธกํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ๊ฐ’์˜ ๋ฒ”์œ„ ๋‚ด์—์„œ๋Š” ์–ด๋–ค ์ˆซ์ž๋„ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์™€ ๊ฐ™์ด ๋ถ„๋ฆฌ๋œ(๋น„์—ฐ์†์ ์ธ) ๋‹ต์ด ๊ฒฐ๊ณผ๊ฐ€ ์•„๋‹ˆ๋ผ ์—ฐ์†๋œ ๊ฐ’์„ ๊ฒฐ๊ณผ๋กœ ๊ฐ€์ง€๋Š” ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํšŒ๊ท€ ๋ฌธ์ œ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ํšŒ๊ท€ ๋ฌธ์ œ์˜ ์˜ˆ์‹œ๋กœ ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ(Time Series Data)๋ฅผ ์ด์šฉํ•œ ์ฃผ๊ฐ€ ์˜ˆ์ธก, ์ƒ์‚ฐ๋Ÿ‰ ์˜ˆ์ธก,<NAME> ์˜ˆ์ธก ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 3. ์ง€๋„ ํ•™์Šต๊ณผ ๋น„์ง€๋„ ํ•™์Šต ๋จธ์‹  ๋Ÿฌ๋‹์€ ํฌ๊ฒŒ ์ง€๋„ ํ•™์Šต, ๋น„์ง€๋„ ํ•™์Šต, ๊ฐ•ํ™” ํ•™์Šต์œผ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ๊ฐ•ํ™” ํ•™์Šต์€ ์ด ์ฑ…์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฏ€๋กœ ์„ค๋ช…ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํฐ ๊ฐˆ๋ž˜๋กœ์„œ๋Š” ์ž์ฃผ ์–ธ๊ธ‰๋˜์ง€๋Š” ์•Š์ง€๋งŒ ๋”ฅ ๋Ÿฌ๋‹ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์ค‘์š”ํ•œ ํ•™์Šต ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ธ ์ž๊ธฐ ์ง€๋„ ํ•™์Šต(Self-Supervised Learning, SSL)์— ๋Œ€ํ•ด์„œ๋„ ์–ธ๊ธ‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์ง€๋„ ํ•™์Šต(Supervised Learning) ์ง€๋„ ํ•™์Šต์ด๋ž€ ๋ ˆ์ด๋ธ”(Label)์ด๋ผ๋Š” ์ •๋‹ต๊ณผ ํ•จ๊ป˜ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋Š” ๋Œ€๋ถ€๋ถ„ ์ง€๋„ ํ•™์Šต์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ํ’€๊ฒŒ ๋  ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ๋งŽ์€ ๋ฌธ์ œ๋“ค์€ ๋ ˆ์ด๋ธ”์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์•ž์„œ 2์ฑ•ํ„ฐ์˜ ๋ฐ์ดํ„ฐ์˜ ๋ถ„๋ฆฌ๋ฅผ ์„ค๋ช…ํ•˜๋ฉฐ ์ƒ์„ธํžˆ ์„ค๋ช…ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์ด๋ผ๋Š” ๋ง ์™ธ์—๋„, ์‹ค์ œ ๊ฐ’ ๋“ฑ์œผ๋กœ ๋ถ€๋ฅด๊ธฐ๋„ ํ•˜๋Š”๋ฐ ์ด ์ฑ…์—์„œ๋Š” ์ด ์šฉ์–ด๋“ค์„ ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์ฐจ์ด์ธ ์˜ค์ฐจ๋ฅผ ์ค„์ด๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•™์Šต์„ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์˜ˆ์ธก๊ฐ’์€ ^ ๊ณผ๊ฐ™์ด ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 2) ๋น„์ง€๋„ ํ•™์Šต(Unsupervised Learning) ๋น„์ง€๋„ ํ•™์Šต์€ ๋ฐ์ดํ„ฐ์— ๋ณ„๋„์˜ ๋ ˆ์ด๋ธ”์ด ์—†์ด ํ•™์Šตํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ…์ŠคํŠธ ์ฒ˜๋ฆฌ ๋ถ„์•ผ์˜ ํ† ํ”ฝ ๋ชจ๋ธ๋ง ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ LSA๋‚˜ LDA๋Š” ๋น„์ง€๋„ ํ•™์Šต์— ์†ํ•ฉ๋‹ˆ๋‹ค. LSA์™€ LDA๋Š” ์˜จ๋ผ์ธ ์›น ์‚ฌ์ดํŠธ ์œ„ํ‚ค๋…์Šค์˜ e-book( https://wikidocs.net/30707 )์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3) ์ž๊ธฐ ์ง€๋„ ํ•™์Šต(Self-Supervised Learning, SSL) ๋ ˆ์ด๋ธ”์ด ์—†๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ๋ชจ๋ธ์ด ํ•™์Šต์„ ์œ„ํ•ด์„œ ์Šค์Šค๋กœ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๋ ˆ์ด๋ธ”์„ ๋งŒ๋“ค์–ด์„œ ํ•™์Šตํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ž๊ธฐ ์ง€๋„ ํ•™์Šต์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ๋กœ๋Š” Word2Vec๊ณผ ๊ฐ™์€ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‚˜, BERT์™€ ๊ฐ™์€ ์–ธ์–ด ๋ชจ๋ธ์˜ ํ•™์Šต ๋ฐฉ๋ฒ•์„ ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์ด ์–ด๋–ป๊ฒŒ ๋ ˆ์ด๋ธ”์„ ๋งŒ๋“ค์–ด ํ•™์Šตํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์„ค๋ช…์€ Word2Vec๊ณผ BERT๋ฅผ ์„ค๋ช…ํ•˜๋Š” ํŽ˜์ด์ง€๋ฅผ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. 4. ์ƒ˜ํ”Œ(Sample)๊ณผ ํŠน์„ฑ(Feature) ๋งŽ์€ ๋จธ์‹  ๋Ÿฌ๋‹ ๋ฌธ์ œ๊ฐ€ 1๊ฐœ ์ด์ƒ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์ข…์† ๋ณ€์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ ์ค‘ ํŠนํžˆ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ๋…๋ฆฝ ๋ณ€์ˆ˜, ์ข…์† ๋ณ€์ˆ˜, ๊ฐ€์ค‘์น˜, ํŽธํ–ฅ ๋“ฑ์„ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์—ฐ์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งŽ์ด ๋ณด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ํ–‰๋ ฌ์„ X๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ n ๊ฐœ๊ณ  ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ m์ธ ํ–‰๋ ฌ X๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ๋Š” ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ. ํ–‰๋ ฌ ๊ด€์ ์—์„œ๋Š” ํ•˜๋‚˜์˜ ํ–‰์„ ์ƒ˜ํ”Œ(Sample)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. (๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ ˆ์ฝ”๋“œ๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๋‹จ์œ„์ž…๋‹ˆ๋‹ค.) ๊ทธ๋ฆฌ๊ณ  ์ข…์† ๋ณ€์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ๊ฐ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜๋ฅผ ํŠน์„ฑ(Feature)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ํ–‰๋ ฌ ๊ด€์ ์—์„œ๋Š” ๊ฐ ์—ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 5. ํ˜ผ๋™ ํ–‰๋ ฌ(Confusion Matrix) ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ๋Š” ๋งž์ถ˜ ๋ฌธ์ œ ์ˆ˜๋ฅผ ์ „์ฒด ๋ฌธ์ œ ์ˆ˜๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ ์ •ํ™•๋„(Accuracy)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ •ํ™•๋„๋Š” ๋งž์ถ˜ ๊ฒฐ๊ณผ์™€ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์„ธ๋ถ€์ ์ธ ๋‚ด์šฉ์„ ์•Œ๋ ค์ฃผ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ˜ผ๋™ ํ–‰๋ ฌ(Confusion Matrix)์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ฐธ(True)์™€ ๊ฑฐ์ง“(False) ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ์•„๋ž˜์˜ ํ˜ผ๋™ ํ–‰๋ ฌ์—์„œ ๊ฐ ์—ด์€ ์˜ˆ์ธก๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๊ฐ ํ–‰์€ ์‹ค์ œ ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ์ธก ์ฐธ ์˜ˆ์ธก ๊ฑฐ์ง“ ์‹ค์ œ ์ฐธ TP FN ์‹ค์ œ ๊ฑฐ์ง“ FP TN ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋„ค ๊ฐ€์ง€ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ TP, FP, FN, TN์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. True Positive(TP) : ์‹ค์ œ True์ธ ์ •๋‹ต์„ True๋ผ๊ณ  ์˜ˆ์ธก (์ •๋‹ต) False Positive(FP) : ์‹ค์ œ False์ธ ์ •๋‹ต์„ True๋ผ๊ณ  ์˜ˆ์ธก (์˜ค๋‹ต) False Negative(FN) : ์‹ค์ œ True์ธ ์ •๋‹ต์„ False๋ผ๊ณ  ์˜ˆ์ธก (์˜ค๋‹ต) True Negative(TN) : ์‹ค์ œ False์ธ ์ •๋‹ต์„ False๋ผ๊ณ  ์˜ˆ์ธก (์ •๋‹ต) ์ด ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜๋ฉด ์ •๋ฐ€๋„(Precision)๊ณผ ์žฌํ˜„์œจ(Recall)์ด ๋ฉ๋‹ˆ๋‹ค. 1) ์ •๋ฐ€๋„(Precision) ์ •๋ฐ€๋„๋ž€ ๋ชจ๋ธ์ด True๋ผ๊ณ  ๋ถ„๋ฅ˜ํ•œ ๊ฒƒ ์ค‘์—์„œ ์‹ค์ œ True์ธ ๊ฒƒ์˜ ๋น„์œจ์ž…๋‹ˆ๋‹ค. ์ •๋ฐ€๋„ ์ •๋ฐ€๋„ T T + P 2) ์žฌํ˜„์œจ(Recall) ์žฌํ˜„์œจ์ด๋ž€ ์‹ค์ œ True์ธ ๊ฒƒ ์ค‘์—์„œ ๋ชจ๋ธ์ด True๋ผ๊ณ  ์˜ˆ์ธกํ•œ ๊ฒƒ์˜ ๋น„์œจ์ž…๋‹ˆ๋‹ค. ์žฌํ˜„์œจ ์žฌํ˜„์œจ T T + N Precision์ด๋‚˜ Recall์€ ๋ชจ๋‘ ์‹ค์ œ True์ธ ์ •๋‹ต์„ ๋ชจ๋ธ์ด True๋ผ๊ณ  ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ. ์ฆ‰, TP์— ๊ด€์‹ฌ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ์‹ ๋ชจ๋‘ ๋ถ„์ž๊ฐ€ TP์ž„์— ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. 3) ์ •ํ™•๋„(Accuracy) ์ •ํ™•๋„(Accuracy)๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์‹ค์ƒํ™œ์—์„œ๋„ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ์˜ˆ์ธกํ•œ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์ •๋‹ต์„ ๋งžํžŒ ๊ฒƒ์— ๋Œ€ํ•œ ๋น„์œจ์ž…๋‹ˆ๋‹ค. TP, FP, FN, TN์„ ๊ฐ€์ง€๊ณ  ์ˆ˜์‹์„ ์„ค๋ช…ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ •ํ™•๋„ ์ •ํ™•๋„ T + N P F + P T ๊ทธ๋Ÿฐ๋ฐ Accuracy๋กœ ์„ฑ๋Šฅ์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•˜์ง€ ์•Š์€ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋น„๊ฐ€ ์˜ค๋Š” ๋‚ ์„ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, 200์ผ ๋™์•ˆ ์ด 6์ผ๋งŒ ๋น„๊ฐ€ ์™”๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ๋ชจ๋ธ์€ 200์ผ ๋‚ด๋‚ด ๋‚ ์”จ๊ฐ€ ๋ง‘์•˜๋‹ค๊ณ  ์˜ˆ์ธกํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ 200๋ฒˆ ์ค‘ ์ด 6ํšŒ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค. 194/200=0.97์ด๋ฏ€๋กœ ์ •ํ™•๋„๋Š” 97%์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ •์ž‘ ๋น„๊ฐ€ ์˜จ ๋‚ ์€ ํ•˜๋‚˜๋„ ๋ชป ๋งž์ถ˜ ์…ˆ์ž…๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. ์ŠคํŒธ ๋ฉ”์ผ์„ ๋ถ„๋ฅ˜ํ•˜๋Š” ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”์ผ 100๊ฐœ ์ค‘ ์ŠคํŒธ ๋ฉ”์ผ์€ 5๊ฐœ์˜€์Šต๋‹ˆ๋‹ค. ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ๋Š” ๋ชจ๋‘ ์ •์ƒ ๋ฉ”์ผ์ด๋ผ๊ณ  ํƒ์ง€ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ •ํ™•๋„๋Š” 95%์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ •์ž‘ ์ŠคํŒธ ๋ฉ”์ผ์€ ํ•˜๋‚˜๋„ ๋ชป ์ฐพ์•„๋‚ธ ์…ˆ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์‹ค์งˆ์ ์œผ๋กœ ๋” ์ค‘์š”ํ•œ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๋„ˆ๋ฌด ์ ์€ ๋น„์œจ์„<NAME>๋‹ค๋ฉด ์ •ํ™•๋„๋Š” ์ข‹์€ ์ธก์ • ์ง€ํ‘œ๊ฐ€ ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” F1-Score๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹ ์ฑ•ํ„ฐ์—์„œ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 6. ๊ณผ ์ ํ•ฉ(Overfitting)๊ณผ ๊ณผ์†Œ ์ ํ•ฉ(Underfitting) ํ•™์ƒ์˜ ์ž…์žฅ์ด ๋˜์–ด ํ•˜๋‚˜์˜ ๋ฌธ์ œ์ง€๋ฅผ ๊ณผํ•˜๋„๋ก ๋งŽ์ด ํ’€์–ด์„œ ๋ฌธ์ œ ๋ฒˆํ˜ธ๋งŒ ๋ด๋„ ์ •๋‹ต์„ ๋งžํž ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋„ˆ๋ฌด ์˜ค๋žœ ์‹œ๊ฐ„ ํ•˜๋‚˜์˜ ๋ฌธ์ œ์ง€๋งŒ ๋ฐ˜๋ณตํ•ด์„œ ํ‘ผ ๋‚˜๋จธ์ง€ ๋‹ค๋ฅธ ๋ฌธ์ œ๋ฅผ ํ’€๊ฑฐ๋‚˜ ์‹œํ—˜์„ ๋ดค์„ ๋•Œ ์ ์ˆ˜๊ฐ€ ์•ˆ ์ข‹๋‹ค๋ฉด ๊ทธ๊ฒŒ ์˜๋ฏธ๊ฐ€ ์žˆ์„๊นŒ์š”? ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ๊ณผ ์ ํ•ฉ(Overfitting) ์ด๋ž€ ์œ„์˜ ๋น„์œ ์ฒ˜๋Ÿผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณผํ•˜๊ฒŒ ํ•™์Šตํ•œ ๊ฒฝ์šฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ์ด ํ•™์Šต์— ์‚ฌ์šฉํ•˜๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์‹ค์ œ๋กœ ์•ž์œผ๋กœ ๊ธฐ๊ณ„๊ฐ€ ํ’€์–ด์•ผ ํ•  ํ˜„์‹ค์˜ ์ˆ˜๋งŽ์€ ๋ฌธ์ œ์— ๋น„ํ•˜๋ฉด ๊ทนํžˆ ์ผ๋ถ€์— ๋ถˆ๊ณผํ•œ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋งŒ ๊ณผํ•˜๊ฒŒ ํ•™์Šตํ•˜๋ฉด ์„ฑ๋Šฅ ์ธก์ •์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ์ธ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋‚˜ ์‹ค์ œ ์„œ๋น„์Šค์—์„œ๋Š” ์ •ํ™•๋„๊ฐ€ ์ข‹์ง€ ์•Š์€ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ณผ์ ํ•ฉ ์ƒํ™ฉ์—์„œ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์˜ค์ฐจ๊ฐ€ ๋‚ฎ์ง€๋งŒ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์˜ค์ฐจ๊ฐ€ ์ปค์ง‘๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ž˜ํ”„๋Š” ๊ณผ์ ํ•ฉ ์ƒํ™ฉ์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ›ˆ๋ จ ํšŸ์ˆ˜์— ๋”ฐ๋ฅธ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์˜ค์ฐจ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์˜ค์ฐจ(๋˜๋Š” ์†์‹ค์ด๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค.)์˜ ๋ณ€ํ™”๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ž˜ํ”„๋Š” ๋’ค์˜ RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ฑ•ํ„ฐ์˜ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‹ค์Šต์—์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ›ˆ๋ จ ํšŸ์ˆ˜๋ฅผ 30 ์—ํฌํฌ๋กœ ์ฃผ์–ด์„œ ์˜๋„์ ์œผ๋กœ ๊ณผ์ ํ•ฉ์„ ๋ฐœ์ƒ์‹œํ‚จ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. y ์ถ•์€ ์˜ค์ฐจ(loss), X ์ถ•์˜ ์—ํฌํฌ(epoch)๋Š” ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ›ˆ๋ จ ํšŸ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ์‚ฌ๋žŒ์œผ๋กœ ๋น„์œ ํ•˜๋ฉด ๋™์ผํ•œ ๋ฌธ์ œ์ง€(ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ)๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ํ‘ผ ํšŸ์ˆ˜์ž…๋‹ˆ๋‹ค. ์—ํฌํฌ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ํฌ๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‹ค์Šต์€ ์—ํฌํฌ๊ฐ€ 3~4์—์„œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๊ฐ€ ๊ฐ€์žฅ ๋†’๊ณ , ์—ํฌํฌ๊ฐ€ ๊ทธ ์ด์ƒ์„ ๋„˜์–ด๊ฐ€๋ฉด ๊ณผ์ ํ•ฉ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ž˜ํ”„๋Š” ์—ํฌํฌ๊ฐ€ ์ฆ๊ฐ€ํ• ์ˆ˜๋ก ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ค์ฐจ๊ฐ€ ์ ์ฐจ ์ฆ๊ฐ€ํ•˜๋Š” ์–‘์ƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ณผ์ ํ•ฉ์€ ๋‹ค๋ฅด๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋Š” ๋†’์ง€๋งŒ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” ์ •ํ™•๋„๊ฐ€ ๋‚ฎ์€ ์ƒํ™ฉ์ด๋ผ๊ณ  ๋งํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์˜ค์ฐจ๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ธฐ ์ „์ด๋‚˜, ์ •ํ™•๋„๊ฐ€ ๊ฐ์†Œํ•˜๊ธฐ ์ „์— ํ›ˆ๋ จ์„ ๋ฉˆ์ถ”๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์„ฑ๋Šฅ์ด ์˜ฌ๋ผ๊ฐˆ ์—ฌ์ง€๊ฐ€ ์žˆ์Œ์—๋„ ํ›ˆ๋ จ์„ ๋œ ํ•œ ์ƒํƒœ๋ฅผ ๊ณผ์†Œ ์ ํ•ฉ(Underfitting)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ณผ์†Œ ์ ํ•ฉ์€ ํ›ˆ๋ จ ์ž์ฒด๊ฐ€ ๋ถ€์กฑํ•œ ์ƒํƒœ์ด๋ฏ€๋กœ ํ›ˆ๋ จ ํšŸ์ˆ˜์ธ ์—ํฌํฌ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ์ ์œผ๋ฉด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณผ๋Œ€ ์ ํ•ฉ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๊ณผ์†Œ ์ ํ•ฉ์€ ํ›ˆ๋ จ ์ž์ฒด๋ฅผ ๋„ˆ๋ฌด ์ ๊ฒŒ ํ•œ ์ƒํƒœ์ด๋ฏ€๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ์ •ํ™•๋„๊ฐ€ ๋‚ฎ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋‘ ๊ฐ€์ง€ ํ˜„์ƒ์„ ๊ณผ์ ํ•ฉ๊ณผ ๊ณผ์†Œ ์ ํ•ฉ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ์ด์œ ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ํ•™์Šต ๋˜๋Š” ํ›ˆ๋ จ์ด๋ผ๊ณ  ํ•˜๋Š” ๊ณผ์ •์„ ์ ํ•ฉ(fitting)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ ํ•ฉํ•ด์ ธ๊ฐ€๋Š” ๊ณผ์ •์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ์ผ€๋ผ์Šค์—์„œ๋Š” ๊ธฐ๊ณ„๋ฅผ ํ•™์Šต์‹œํ‚ฌ ๋•Œ fit()์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๋’ค์˜ ์„ ํ˜• ํšŒ๊ท€ ์‹ค์Šต์—์„œ ๋ณด๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฅ ๋Ÿฌ๋‹์„ ํ•  ๋•Œ๋Š” ๊ณผ์ ํ•ฉ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋Š” ๋“œ๋กญ์•„์›ƒ(Dropout), ์กฐ๊ธฐ ์ข…๋ฃŒ(Early Stopping)๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•˜๋Š”๋ฐ ์ด๋Š” ๋’ค์˜ ๋”ฅ ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ์—์„œ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ๊ณผ์ ํ•ฉ๊ณผ ๊ณผ์†Œ ์ ํ•ฉ์„ ์„ค๋ช…ํ•˜๋ฉด์„œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์„ค๋ช…ํ•˜์˜€์ง€๋งŒ, ๋” ์ •ํ™•ํžˆ ์„ค๋ช…ํ•˜๋ฉด ํ˜„์—…์—์„œ๋Š” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‘ ๊ฐ€์ง€ ์šฉ๋„๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์šฉ๋„๋Š” ๊ณผ์ ํ•ฉ ๋ชจ๋‹ˆํ„ฐ๋ง๊ณผ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ํŠœ๋‹์„ ์œ„ํ•œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์™€ ์˜ค์ง ์„ฑ๋Šฅ ํ‰๊ฐ€๋งŒ์„ ์œ„ํ•œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ „์ž์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์•ž์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์„ธ ๊ฐ€์ง€๋กœ ๋‚˜๋ˆ„์–ด์•ผ ํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ–ˆ๋˜ ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์‹œ๋‚˜์š”? ๊ณผ์ ํ•ฉ ๋ฐฉ์ง€๋ฅผ ๊ณ ๋ คํ•œ ์ผ๋ฐ˜์ ์ธ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ํ•™์Šต ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Step 1. ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋‚˜๋ˆˆ๋‹ค. ๊ฐ€๋ น, 6:2:2 ๋น„์œจ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. Step 2. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ์„ ํ•™์Šตํ•œ๋‹ค. (์—ํฌํฌ +1) Step 3. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜์—ฌ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์˜ค์ฐจ(loss)๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. Step 4. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์˜ค์ฐจ๊ฐ€ ์ฆ๊ฐ€ํ•˜์˜€๋‹ค๋ฉด ๊ณผ์ ํ•ฉ ์ง•ํ›„์ด๋ฏ€๋กœ ํ•™์Šต ์ข…๋ฃŒ ํ›„ Step 5๋กœ ์ด๋™, ์•„๋‹ˆ๋ผ๋ฉด Step 2.๋กœ ์žฌ์ด๋™ํ•œ๋‹ค. Step 5. ๋ชจ๋ธ์˜ ํ•™์Šต์ด ์ข…๋ฃŒ๋˜์—ˆ์œผ๋‹ˆ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•œ๋‹ค. 06-03 ์„ ํ˜• ํšŒ๊ท€(Linear Regression) ๋”ฅ ๋Ÿฌ๋‹์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ ํ˜• ํšŒ๊ท€(Linear Regression)์™€ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€(Logsitic Regression)๋ฅผ ์ดํ•ดํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ์“ฐ์ด๋Š” ์šฉ์–ด์ธ ๊ฐ€์„ค(Hypothesis), ์†์‹ค ํ•จ์ˆ˜(Loss Function) ๊ทธ๋ฆฌ๊ณ  ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Gradient Descent)์— ๋Œ€ํ•œ ๊ฐœ๋…๊ณผ ์„ ํ˜• ํšŒ๊ท€์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 1. ์„ ํ˜• ํšŒ๊ท€(Linear Regression) ์‹œํ—˜๊ณต๋ถ€ํ•˜๋Š” ์‹œ๊ฐ„์„ ๋Š˜๋ฆฌ๋ฉด ๋Š˜๋ฆด์ˆ˜๋ก ์„ฑ์ ์ด ์ž˜ ๋‚˜์˜ต๋‹ˆ๋‹ค. ํ•˜๋ฃจ์— ๊ฑท๋Š” ํšŸ์ˆ˜๋ฅผ ๋Š˜๋ฆด์ˆ˜๋ก, ๋ชธ๋ฌด๊ฒŒ๋Š” ์ค„์–ด๋“ญ๋‹ˆ๋‹ค. ์ง‘์˜ ํ‰์ˆ˜๊ฐ€ ํด์ˆ˜๋ก, ์ง‘์˜ ๋งค๋งค ๊ฐ€๊ฒฉ์€ ๋น„์‹ผ ๊ฒฝํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ˆ˜ํ•™์ ์œผ๋กœ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์–ด๋–ค ์š”์ธ์˜ ์ˆ˜์น˜์— ๋”ฐ๋ผ์„œ ํŠน์ • ์š”์ธ์˜ ์ˆ˜์น˜๊ฐ€ ์˜ํ–ฅ์„ ๋ฐ›๊ณ  ์žˆ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ๊ธˆ ๋” ์ˆ˜ํ•™์ ์ธ ํ‘œํ˜„์„ ์จ๋ณด๋ฉด ์–ด๋–ค ๋ณ€์ˆ˜์˜ ๊ฐ’์— ๋”ฐ๋ผ์„œ ํŠน์ • ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ์˜ํ–ฅ์„ ๋ฐ›๊ณ  ์žˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€ํ•˜๊ฒŒ ํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ, ๋ณ€์ˆ˜์— ์˜ํ•ด์„œ ๊ฐ’์ด ์ข…์†์ ์œผ๋กœ ๋ณ€ํ•˜๋Š” ๋ณ€์ˆ˜๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋•Œ ๋ณ€์ˆ˜์˜ ๊ฐ’์€ ๋…๋ฆฝ์ ์œผ๋กœ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์— ๋ฐ˜ํ•ด, ๊ฐ’์€ ๊ณ„์†ํ•ด์„œ์˜ ๊ฐ’์— ์˜ํ•ด์„œ, ์ข…์†์ ์œผ๋กœ ๊ฒฐ์ •๋˜๋ฏ€๋กœ๋ฅผ ๋…๋ฆฝ ๋ณ€์ˆ˜,๋ฅผ ์ข…์† ๋ณ€์ˆ˜๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์„ ํ˜• ํšŒ๊ท€๋Š” ํ•œ ๊ฐœ ์ด์ƒ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜ ์™€์˜ ์„ ํ˜• ๊ด€๊ณ„๋ฅผ ๋ชจ๋ธ๋ง ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋…๋ฆฝ ๋ณ€์ˆ˜ ๊ฐ€ 1๊ฐœ๋ผ๋ฉด ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 1) ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€ ๋ถ„์„(Simple Linear Regression Analysis) = x b ์œ„์˜ ์ˆ˜์‹์€ ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€์˜ ์ˆ˜์‹์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋…๋ฆฝ ๋ณ€์ˆ˜ ์™€ ๊ณฑํ•ด์ง€๋Š” ๊ฐ’ ๋ฅผ ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ๋Š” ๊ฐ€์ค‘์น˜(weight), ๋ณ„๋„๋กœ ๋”ํ•ด์ง€๋Š” ๊ฐ’ ๋ฅผ ํŽธํ–ฅ(bias)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ง์„ ์˜ ๋ฐฉ์ •์‹์—์„œ๋Š” ๊ฐ๊ฐ ์ง์„ ์˜ ๊ธฐ์šธ๊ธฐ์™€ ์ ˆํŽธ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์™€ ๊ฐ€ ์—†์ด ์™€ ๋ž€ ์ˆ˜์‹์€ ๋Š” ์™€ ๊ฐ™๋‹ค๋Š” ํ•˜๋‚˜์˜ ์‹๋ฐ–์— ํ‘œํ˜„ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„ ์ƒ์œผ๋กœ ๋งํ•˜๋ฉด ํ•˜๋‚˜์˜ ์ง์„ ๋ฐ–์— ํ‘œํ˜„ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. = ๋‹ค์‹œ ๋งํ•ด ์™€์˜ ๊ฐ’์— ๋”ฐ๋ผ์„œ ์™€ ๊ฐ€ ํ‘œํ˜„ํ•˜๋Š” ์ง์„ ์€ ๋ฌด๊ถ๋ฌด์ง„ํ•ด์ง‘๋‹ˆ๋‹ค. 2) ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ ๋ถ„์„(Multiple Linear Regression Analysis) = 1 1 w x + . w x + ์ง‘์˜ ๋งค๋งค ๊ฐ€๊ฒฉ์€ ๋‹จ์ˆœํžˆ ์ง‘์˜ ํ‰์ˆ˜๊ฐ€ ํฌ๋‹ค๊ณ  ๊ฒฐ์ •๋˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ์ง‘์˜ ์ธต์˜ ์ˆ˜, ๋ฐฉ์˜ ๊ฐœ์ˆ˜, ์ง€ํ•˜์ฒ ์—ญ๊ณผ์˜ ๊ฑฐ๋ฆฌ์™€๋„ ์˜ํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋‹ค์ˆ˜์˜ ์š”์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์ง‘์˜ ๋งค๋งค ๊ฐ€๊ฒฉ์„ ์˜ˆ์ธกํ•ด ๋ณด๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.๋Š” ์—ฌ์ „ํžˆ 1๊ฐœ์ด์ง€๋งŒ ์ด์ œ๋Š” 1๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ ๋ถ„์„์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ์‹ค์Šต์€ ๋’ค์—์„œ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 2. ๊ฐ€์„ค(Hypothesis) ์„ธ์šฐ๊ธฐ ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ด…์‹œ๋‹ค. ์–ด๋–ค ํ•™์ƒ์˜ ๊ณต๋ถ€ ์‹œ๊ฐ„์— ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ์ˆ˜๋ฅผ ์–ป์—ˆ๋‹ค๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. hours( ) score( ) 2 25 3 50 4 42 5 61 ์ด๋ฅผ ์ขŒํ‘œ ํ‰๋ฉด์— ๊ทธ๋ ค๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•Œ๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์™€์˜ ๊ด€๊ณ„๋ฅผ ์œ ์ถ”ํ•˜๊ณ , ์ด ํ•™์ƒ์ด 6์‹œ๊ฐ„, 7์‹œ๊ฐ„, 8์‹œ๊ฐ„์„ ๊ณต๋ถ€ํ•˜์˜€์„ ๋•Œ์˜ ์„ฑ์ ์„ ์˜ˆ์ธกํ•ด ๋ณด๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.์™€์˜ ๊ด€๊ณ„๋ฅผ ์œ ์ถ”ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ˆ˜ํ•™์ ์œผ๋กœ ์‹์„ ์„ธ์›Œ๋ณด๊ฒŒ ๋˜๋Š”๋ฐ ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ๋Š” ์ด๋Ÿฌํ•œ ์‹์„ ๊ฐ€์„ค(Hypothesis)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ( ) ์—์„œ๋Š” Hypothesis๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ( ) w + ์œ„์˜ ๊ทธ๋ฆผ์€ ์™€์˜ ๊ฐ’์— ๋”ฐ๋ผ์„œ ์ฒœ์ฐจ๋งŒ๋ณ„๋กœ ๊ทธ๋ ค์ง€๋Š” ์ง์„ ์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ค‘ํ•™๊ต ์ˆ˜ํ•™ ๊ณผ์ •์ธ ์ง์„ ์˜ ๋ฐฉ์ •์‹์„ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด, ์œ„์˜ ๊ฐ€์„ค์—์„œ๋Š” ์ง์„ ์˜ ๊ธฐ์šธ๊ธฐ์ด๊ณ ๋Š” ์ ˆํŽธ์œผ๋กœ ์ง์„ ์„ ํ‘œํ˜„ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์„ ํ˜• ํšŒ๊ท€๋Š” ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์™€์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์žฅ ์ž˜ ๋‚˜ํƒ€๋‚ด๋Š” ์ง์„ ์„ ๊ทธ๋ฆฌ๋Š” ์ผ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์–ด๋–ค ์ง์„ ์ธ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์€ ์™€์˜ ๊ฐ’์ด๋ฏ€๋กœ ์„ ํ˜• ํšŒ๊ท€์—์„œ ํ•ด์•ผ ํ•  ์ผ์€ ๊ฒฐ๊ตญ ์ ์ ˆํ•œ ์™€๋ฅผ ์ฐพ์•„๋‚ด๋Š” ์ผ์ด ๋ฉ๋‹ˆ๋‹ค. ์•„์ง์€ ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅด์ง€๋งŒ, ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์ ์ ˆํ•œ ์™€์˜ ๊ฐ’์„ ์ฐพ์€ ๋•ํƒ์— ์™€์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์žฅ ์ž˜ ๋‚˜ํƒ€๋‚ด๋Š” ์ง์„ ์„ ์œ„์˜ ์ขŒํ‘œ ํ‰๋ฉด ์ƒ์—์„œ ๊ทธ๋ ธ๋‹ค๊ณ  ํ•œ ๋ฒˆ ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ง์„ ์„ ๊ฐ€ 6์ผ ๋•Œ, 7์ผ ๋•Œ, 8์ผ ๋•Œ์— ๋Œ€ํ•ด์„œ๋„ ๊ณ„์†ํ•ด์„œ ์ง์„ ์„ ๊ทธ์ € ์ด์–ด ๊ทธ๋ฆฐ๋‹ค๋ฉด ์ด ํ•™์ƒ์ด 6์‹œ๊ฐ„์„ ๊ณต๋ถ€ํ–ˆ์„ ๋•Œ, 7์‹œ๊ฐ„์„ ๊ณต๋ถ€ํ–ˆ์„ ๋•Œ, 8์‹œ๊ฐ„์„ ๊ณต๋ถ€ํ–ˆ์„ ๋•Œ์˜ ์˜ˆ์ƒ ์ ์ˆ˜๋ฅผ ๋งํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ์ € ๊ฐ€ ๊ฐ๊ฐ 6์ผ ๋•Œ, 7์ผ ๋•Œ, 8์ผ ๋•Œ์˜ ๊ฐ’์„ ํ™•์ธํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 3. ๋น„์šฉ ํ•จ์ˆ˜(Cost function) : ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ(MSE) ์•ž์„œ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์—์„œ ์™€์˜ ๊ด€๊ณ„๋ฅผ ์™€๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹์„ ์„ธ์šฐ๋Š” ์ผ์„ ๊ฐ€์„ค์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ œ ํ•ด์•ผ ํ•  ์ผ์€ ๋ฌธ์ œ์— ๋Œ€ํ•œ ๊ทœ์น™์„ ๊ฐ€์žฅ ์ž˜ ํ‘œํ˜„ํ•˜๋Š” ์™€๋ฅผ ์ฐพ๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹์€ ์™€๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ ์‹ค์ œ ๊ฐ’๊ณผ ๊ฐ€์„ค๋กœ๋ถ€ํ„ฐ ์–ป์€ ์˜ˆ์ธก๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์‹์„ ์„ธ์šฐ๊ณ , ์ด ์‹์˜ ๊ฐ’์„ ์ตœ์†Œํ™”ํ•˜๋Š” ์ตœ์ ์˜ ์™€๋ฅผ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. ์ด๋•Œ ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์— ๋Œ€ํ•œ ์˜ค์ฐจ์— ๋Œ€ํ•œ ์‹์„ ๋ชฉ์  ํ•จ์ˆ˜(Objective function) ๋˜๋Š” ๋น„์šฉ ํ•จ์ˆ˜(Cost function) ๋˜๋Š” ์†์‹ค ํ•จ์ˆ˜(Loss function)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜์˜ ๊ฐ’์„ ์ตœ์†Œํ™”ํ•˜๊ฑฐ๋‚˜, ์ตœ๋Œ€ํ™”ํ•˜๊ฑฐ๋‚˜ ํ•˜๋Š” ๋ชฉ์ ์„ ๊ฐ€์ง„ ํ•จ์ˆ˜๋ฅผ ๋ชฉ์  ํ•จ์ˆ˜(Objective function)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ’์„ ์ตœ์†Œํ™”ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์ด๋ฅผ ๋น„์šฉ ํ•จ์ˆ˜(Cost function) ๋˜๋Š” ์†์‹ค ํ•จ์ˆ˜(Loss function)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„ธ ๊ฐ€์ง€๋Š” ์—„๋ฐ€ํžˆ๋Š” ๊ฐ™์€ ์˜๋ฏธ๋Š” ์•„๋‹ˆ์ง€๋งŒ, ์ด ์ฑ…์—์„œ๋Š” ๋ชฉ์  ํ•จ์ˆ˜, ๋น„์šฉ ํ•จ์ˆ˜, ์†์‹ค ํ•จ์ˆ˜๋ž€ ์šฉ์–ด๋ฅผ ๊ฐ™์€ ์˜๋ฏธ๋กœ ํ˜ผ์šฉํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋น„์šฉ ํ•จ์ˆ˜๋Š” ๋‹จ์ˆœํžˆ ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์— ๋Œ€ํ•œ ์˜ค์ฐจ๋ฅผ ํ‘œํ˜„ํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์˜ˆ์ธก๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ์ค„์ด๋Š” ์ผ์— ์ตœ์ ํ™”๋œ ์‹์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ๋ฐฐ์šธ ๋Ÿฌ๋‹, ๋”ฅ ๋Ÿฌ๋‹์—๋Š” ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋“ค์ด ์žˆ๊ณ , ๊ฐ ๋ฌธ์ œ๋“ค์—๋Š” ์ ํ•ฉํ•œ ๋น„์šฉ ํ•จ์ˆ˜๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํšŒ๊ท€ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ์—๋Š” ์ฃผ๋กœ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ(Mean Squared Error, MSE)๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ž˜ํ”„์— ์ž„์˜์˜์˜ ๊ฐ’ 13๊ณผ ์ž„์˜์˜์˜ ๊ฐ’ 1์„ ๊ฐ€์ง„ ์ง์„ ์„ ๊ทธ๋ ธ์Šต๋‹ˆ๋‹ค. ์ž„์˜๋กœ ๊ทธ๋ฆฐ ์ง์„ ์œผ๋กœ ์ •๋‹ต์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด์ œ ์ด ์ง์„ ์œผ๋กœ๋ถ€ํ„ฐ ์„œ์„œํžˆ ์™€์˜ ๊ฐ’์„ ๋ฐ”๊พธ๋ฉด์„œ ์ •๋‹ต์ธ ์ง์„ ์„ ์ฐพ์•„๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์™€์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์žฅ ์ž˜ ๋‚˜ํƒ€๋‚ด๋Š” ์ง์„ ์„ ๊ทธ๋ฆฐ๋‹ค๋Š” ๊ฒƒ์€ ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ๋ชจ๋“  ์ ๋“ค๊ณผ ์œ„์น˜์ ์œผ๋กœ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ง์„ ์„ ๊ทธ๋ฆฐ๋‹ค๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด์ œ ์˜ค์ฐจ(error)๋ฅผ ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ค์ฐจ๋Š” ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ์—์„œ์˜ ์‹ค์ œ ๊ฐ’ ์™€ ์œ„์˜ ์ง์„ ์—์„œ ์˜ˆ์ธกํ•˜๊ณ  ์žˆ๋Š” ( ) ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์œ„์˜ ๊ทธ๋ฆผ์—์„œ โ†•๋Š” ๊ฐ ์ ์—์„œ์˜ ์˜ค์ฐจ์˜ ํฌ๊ธฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์˜ค์ฐจ๋ฅผ ์ค„์—ฌ๊ฐ€๋ฉด์„œ ์™€์˜ ๊ฐ’์„ ์ฐพ์•„๋‚ด๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ „์ฒด ์˜ค์ฐจ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ค์ฐจ์˜ ํฌ๊ธฐ๋ฅผ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์€ ๊ฐ ์˜ค์ฐจ๋ฅผ ๋ชจ๋‘ ๋”ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ = 13 + ์ง์„ ์ด ์˜ˆ์ธกํ•œ ์˜ˆ์ธก๊ฐ’์„ ๊ฐ๊ฐ ์‹ค์ œ ๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ํ‘œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. hours( ) 2 3 4 5 ์‹ค์ œ ๊ฐ’ 25 50 42 61 ์˜ˆ์ธก๊ฐ’ 27 40 53 66 ์˜ค์ฐจ -2 10 -9 -5 ๊ทธ๋Ÿฐ๋ฐ, ์ˆ˜์‹์ ์œผ๋กœ ๋‹จ์ˆœํžˆ '์˜ค์ฐจ = ์‹ค์ œ ๊ฐ’ - ์˜ˆ์ธก๊ฐ’'์ด๋ผ๊ณ  ์ •์˜ํ•œ ํ›„์— ๋ชจ๋“  ์˜ค์ฐจ๋ฅผ ๋”ํ•˜๋ฉด ์Œ์ˆ˜ ์˜ค์ฐจ๋„ ์žˆ๊ณ , ์–‘์ˆ˜ ์˜ค์ฐจ๋„ ์žˆ์œผ๋ฏ€๋กœ ์˜ค์ฐจ์˜ ์ ˆ๋Œ€์ ์ธ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ชจ๋“  ์˜ค์ฐจ๋ฅผ ์ œ๊ณฑํ•˜์—ฌ ๋”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์œ„์˜ ๊ทธ๋ฆผ์—์„œ์˜ ๋ชจ๋“  ์ ๊ณผ ์ง์„  ์‚ฌ์ด์˜ โ†• ๊ฑฐ๋ฆฌ๋ฅผ ์ œ๊ณฑํ•˜๊ณ  ๋ชจ๋‘ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹จ, ์—ฌ๊ธฐ์„œ ์€ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. i 1 [ ( ) H ( ( ) ) ] = ( 2 ) + 10 + ( 9 ) + ( 5 ) = 210 ์ด๋•Œ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์ธ์œผ๋กœ ๋‚˜๋ˆ„๋ฉด, ์˜ค์ฐจ์˜ ์ œ๊ณฑํ•ฉ์— ๋Œ€ํ•œ ํ‰๊ท ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋ฅผ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ(Mean Squered Error, MSE)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. n i 1 [ ( ) H ( ( ) ) ] = 210 4 52.5 = 13 +์˜ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ์˜ ๊ฐ’์€ 52.5์ž…๋‹ˆ๋‹ค. ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ์˜ ๊ฐ’์„ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ๋งŒ๋“œ๋Š” ์™€๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์ด ์ •๋‹ต์ธ ์ง์„ ์„ ์ฐพ์•„๋‚ด๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ์™€์— ์˜ํ•œ ๋น„์šฉ ํ•จ์ˆ˜(Cost function)๋กœ ์žฌ์ •์˜ํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o t ( , ) 1 โˆ‘ = n [ ( ) H ( ( ) ) ] ๋ชจ๋“  ์ ๋“ค๊ณผ์˜ ์˜ค์ฐจ๊ฐ€ ํด์ˆ˜๋ก ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋Š” ์ปค์ง€๋ฉฐ, ์˜ค์ฐจ๊ฐ€ ์ž‘์•„์งˆ์ˆ˜๋ก ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋Š” ์ž‘์•„์ง‘๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด ํ‰๊ท  ์ตœ์†Œ ์˜ค์ฐจ. ์ฆ‰, o t ( , ) ๋ฅผ ์ตœ์†Œ๊ฐ€ ๋˜๊ฒŒ ๋งŒ๋“œ๋Š” ์™€๋ฅผ ๊ตฌํ•˜๋ฉด ๊ฒฐ๊ณผ์ ์œผ๋กœ ์™€์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์žฅ ์ž˜ ๋‚˜ํƒ€๋‚ด๋Š” ์ง์„ ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. , โ†’ i i i e c s ( , ) 4. ์˜ตํ‹ฐ๋งˆ์ด์ €(Optimizer) : ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•(Gradient Descent) ์„ ํ˜• ํšŒ๊ท€๋ฅผ ํฌํ•จํ•œ ์ˆ˜๋งŽ์€ ๋จธ์‹  ๋Ÿฌ๋‹, ๋”ฅ ๋Ÿฌ๋‹์˜ ํ•™์Šต์€ ๊ฒฐ๊ตญ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜์ธ ์™€ ์„ ์ฐพ๊ธฐ ์œ„ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์˜ตํ‹ฐ๋งˆ์ด์ €(Optimizer) ๋˜๋Š” ์ตœ์ ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ํ†ตํ•ด ์ ์ ˆํ•œ ์™€๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ณผ์ •์„ ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ํ›ˆ๋ จ(training) ๋˜๋Š” ํ•™์Šต(learning)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์˜ตํ‹ฐ๋งˆ์ด์ € ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Gradient Descent)์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ cost์™€ ๊ธฐ์šธ๊ธฐ์™€์˜ ๊ด€๊ณ„๋ฅผ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ์šฉ์–ด๋กœ๋Š” ๊ฐ€์ค‘์น˜๋ผ๊ณ  ๋ถˆ๋ฆฌ์ง€๋งŒ, ์ง์„ ์˜ ๋ฐฉ์ •์‹ ๊ด€์ ์—์„œ ๋ณด๋ฉด ์ง์„ ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ์˜๋ฏธํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ž˜ํ”„๋Š” ๊ธฐ์šธ๊ธฐ ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ๋†’๊ฑฐ๋‚˜, ๋‚ฎ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์˜ค์ฐจ๊ฐ€ ์ปค์ง€๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์ฃผํ™ฉ์ƒ‰์„ ์€ ๊ธฐ์šธ๊ธฐ ๊ฐ€ 20์ผ ๋•Œ, ์ดˆ๋ก์ƒ‰์„ ์€ ๊ธฐ์šธ๊ธฐ ๊ฐ€ 1์ผ ๋•Œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ฐ๊ฐ = 20 , =์— ํ•ด๋‹น๋˜๋Š” ์ง์„ ์ž…๋‹ˆ๋‹ค. โ†•๋Š” ๊ฐ ์ ์—์„œ์˜ ์‹ค์ œ ๊ฐ’๊ณผ ๋‘ ์ง์„ ์˜ ์˜ˆ์ธก๊ฐ’๊ณผ์˜ ์˜ค์ฐจ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋Š” ์•ž์„œ ์˜ˆ์ธก์— ์‚ฌ์šฉํ–ˆ๋˜ = 13 + ์ง์„ ๋ณด๋‹ค ํ™•์—ฐํžˆ ํฐ ์˜ค์ฐจ ๊ฐ’๋“ค์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ํฌ๋ฉด ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์˜ ์˜ค์ฐจ๊ฐ€ ์ปค์ง€๊ณ , ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ์ž‘์•„๋„ ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์˜ ์˜ค์ฐจ๊ฐ€ ์ปค์ง‘๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋˜ํ•œ ๋งˆ์ฐฌ๊ฐ€์ง€์ธ๋ฐ ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ํฌ๊ฑฐ๋‚˜ ์ž‘์œผ๋ฉด ์˜ค์ฐจ๊ฐ€ ์ปค์ง‘๋‹ˆ๋‹ค. ์„ค๋ช…์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด ํŽธํ–ฅ ๊ฐ€ ์—†์ด ๋‹จ์ˆœํžˆ ๊ฐ€์ค‘์น˜ ๋งŒ์„ ์‚ฌ์šฉํ•œ = x ๋ผ๋Š” ๊ฐ€์„ค ( ) ๋ฅผ ๊ฐ€์ง€๊ณ , ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋น„์šฉ ํ•จ์ˆ˜์˜ ๊ฐ’ o t ( ) ๋Š” cost๋ผ๊ณ  ์ค„์—ฌ์„œ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ์™€ cost์˜ ๊ด€๊ณ„๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ ๊ฐ€ ๋ฌดํ•œ๋Œ€๋กœ ์ปค์ง€๋ฉด ์ปค์งˆ์ˆ˜๋ก cost์˜ ๊ฐ’ ๋˜ํ•œ ๋ฌดํ•œ๋Œ€๋กœ ์ปค์ง€๊ณ , ๋ฐ˜๋Œ€๋กœ ๊ธฐ์šธ๊ธฐ ๊ฐ€ ๋ฌดํ•œ๋Œ€๋กœ ์ž‘์•„์ ธ๋„ cost์˜ ๊ฐ’์€ ๋ฌดํ•œ๋Œ€๋กœ ์ปค์ง‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ž˜ํ”„์—์„œ cost๊ฐ€ ๊ฐ€์žฅ ์ž‘์„ ๋•Œ๋Š” ๋ณผ๋กํ•œ ๋ถ€๋ถ„์˜ ๋งจ ์•„๋ž˜ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ํ•ด์•ผ ํ•  ์ผ์€ cost๊ฐ€ ๊ฐ€์žฅ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ํ•˜๋Š” ๋ฅผ ์ฐพ๋Š” ์ผ์ด๋ฏ€๋กœ, ๋ณผ๋กํ•œ ๋ถ€๋ถ„์˜ ๋งจ ์•„๋žซ๋ถ€๋ถ„์˜์˜ ๊ฐ’์„ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ์ž„์˜์˜ ๋žœ๋ค ๊ฐ’ ๊ฐ’์„ ์ •ํ•œ ๋’ค์—, ๋งจ ์•„๋ž˜์˜ ๋ณผ๋กํ•œ ๋ถ€๋ถ„์„ ํ–ฅํ•ด ์ ์ฐจ์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๊ฐ’์ด ์ ์ฐจ ์ˆ˜์ •๋˜๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Gradient Descent)์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ณ ๋“ฑํ•™๊ต ์ˆ˜ํ•™ ๊ณผ์ •์ธ ๋ฏธ๋ถ„์„ ์ดํ•ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ๋ฏธ๋ถ„์„ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋ฉด ๊ฐ€์žฅ ์ฒ˜์Œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” ๊ฐœ๋…์ธ ํ•œ ์ ์—์„œ์˜ ์ˆœ๊ฐ„ ๋ณ€ํ™”์œจ ๋˜๋Š” ๋‹ค๋ฅธ ํ‘œํ˜„์œผ๋กœ๋Š” ์ ‘์„ ์—์„œ์˜ ๊ธฐ์šธ๊ธฐ์˜ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์ดˆ๋ก์ƒ‰ ์„ ์€ ๊ฐ€ ์ž„์˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š” ๋„ค ๊ฐ€์ง€์˜ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ, ๊ทธ๋ž˜ํ”„ ์ƒ์œผ๋กœ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ฃผ๋ชฉํ•  ๊ฒƒ์€ ๋งจ ์•„๋ž˜์˜ ๋ณผ๋กํ•œ ๋ถ€๋ถ„์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ ์ฐจ ์ž‘์•„์ง„๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งจ ์•„๋ž˜์˜ ๋ณผ๋กํ•œ ๋ถ€๋ถ„์—์„œ๋Š” ๊ฒฐ๊ตญ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„ ์ƒ์œผ๋กœ๋Š” ์ดˆ๋ก์ƒ‰ ํ™”์‚ดํ‘œ๊ฐ€ ์ˆ˜ํ‰์ด ๋˜๋Š” ์ง€์ ์ž…๋‹ˆ๋‹ค. ์ฆ‰, cost๊ฐ€ ์ตœ์†Œํ™”๊ฐ€ ๋˜๋Š” ์ง€์ ์€ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ด ๋˜๋Š” ์ง€์ ์ด๋ฉฐ, ๋˜ํ•œ ๋ฏธ๋ถ„ ๊ฐ’์ด 0์ด ๋˜๋Š” ์ง€์ ์ž…๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์˜ ์•„์ด๋””์–ด๋Š” ๋น„์šฉ ํ•จ์ˆ˜(Cost function)๋ฅผ ๋ฏธ๋ถ„ํ•˜์—ฌ ํ˜„์žฌ์—์„œ์˜ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ตฌํ•˜๊ณ , ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ๋‚ฎ์€ ๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ  ๋‹ค์‹œ ๋ฏธ๋ถ„ํ•˜๊ณ  ์ด ๊ณผ์ •์„ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ธ ๊ณณ์„ ํ–ฅํ•ด์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์— ์žˆ์Šต๋‹ˆ๋‹ค. ๋น„์šฉ ํ•จ์ˆ˜(Cost function)๋Š” ์•„๋ž˜์™€ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค. o t ( , ) 1 โˆ‘ = n [ ( ) H ( ( ) ) ] ์ด์ œ ๋น„์šฉ(cost)๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. := โˆ’ โˆ‚ w o t ( ) ์œ„์˜ ์‹์€ ํ˜„์žฌ์—์„œ์˜ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ์™€ ์™€ ๊ณฑํ•œ ๊ฐ’์„ ํ˜„์žฌ์—์„œ ๋นผ์„œ ์ƒˆ๋กœ์šด์˜ ๊ฐ’์œผ๋กœ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋Š” ์—ฌ๊ธฐ์„œ ํ•™์Šต๋ฅ (learning rate)์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์šฐ์„  ๋Š” ์ƒ๊ฐํ•˜์ง€ ์•Š๊ณ  ํ˜„์žฌ์—์„œ ํ˜„์žฌ์—์„œ์˜ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๋นผ๋Š” ํ–‰์œ„๊ฐ€ ์–ด๋–ค ์˜๋ฏธ๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์Œ์ˆ˜์ผ ๋•Œ, 0์ผ ๋•Œ, ์–‘์ˆ˜์ผ ๋•Œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์Œ์ˆ˜์ผ ๋•Œ์˜ ์ˆ˜์‹์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์Œ์ˆ˜ ๊ธฐ์šธ๊ธฐ := โˆ’ ( ์Œ์ˆ˜ ๊ธฐ์šธ๊ธฐ ) ๊ธฐ์šธ๊ธฐ๊ฐ€ ์Œ์ˆ˜ ๋ฉด '์Œ์ˆ˜๋ฅผ ๋นผ๋Š” ๊ฒƒ'์€ ๊ณง 'ํ•ด๋‹น ๊ฐ’์„ ์–‘์ˆ˜๋กœ ๋ฐ”๊พธ๊ณ  ๋”ํ•˜๋Š” ๊ฒƒ'๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (๊ฐ€๋ น, ์–ด๋–ค ์ˆ˜์—์„œ -2๋ฅผ ๋บ€๋‹ค๋Š” ๊ฒƒ์€ ํ•ด๋‹น ์ˆซ์ž์— 2๋ฅผ ๋”ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.) ๊ฒฐ๊ตญ ์Œ์ˆ˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๋นผ๋ฉด์˜ ๊ฐ’์ด ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ธ ๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฐ’์ด ์กฐ์ •๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์–‘์ˆ˜๋ผ๋ฉด ์œ„์˜ ์ˆ˜์‹์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–‘์ˆ˜ ๊ธฐ์šธ๊ธฐ := โˆ’ ( ์–‘์ˆ˜ ๊ธฐ์šธ๊ธฐ ) ๊ธฐ์šธ๊ธฐ๊ฐ€ ์–‘์ˆ˜๋ฉด์˜ ๊ฐ’์ด ๊ฐ์†Œํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ธ ๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฐ’์ด ์กฐ์ •๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ, ์•„๋ž˜์˜ ์ˆ˜์‹์€ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์Œ์ˆ˜๊ฑฐ๋‚˜, ์–‘์ˆ˜์ผ ๋•Œ ๋ชจ๋‘ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ธ ๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฐ’์„ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. := โˆ’ โˆ‚ w o t ( ) ๊ทธ๋ ‡๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ํ•™์Šต๋ฅ (learning rate)์ด๋ผ๊ณ  ๋งํ•˜๋Š” ๋Š” ์–ด๋–ค ์˜๋ฏธ๋ฅผ ๊ฐ€์งˆ๊นŒ์š”? ํ•™์Šต๋ฅ  ์€์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ, ์–ผ๋งˆ๋‚˜ ํฌ๊ฒŒ ๋ณ€๊ฒฝํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋ฉฐ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ 0.01์ด ๋  ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•™์Šต๋ฅ ์€ ๋ฅผ ๊ทธ๋ž˜ํ”„์˜ ํ•œ ์ ์œผ๋กœ ๋ณด๊ณ  ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ผ ๋•Œ๊นŒ์ง€ ๊ฒฝ์‚ฌ๋ฅผ ๋”ฐ๋ผ ๋‚ด๋ ค๊ฐ„๋‹ค๋Š” ๊ด€์ ์—์„œ๋Š” ์–ผ๋งˆ๋‚˜ ํฐ ํญ์œผ๋กœ ์ด๋™ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์ง๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•˜๊ธฐ์— ํ•™์Šต๋ฅ ์˜ ๊ฐ’์„ ๋ฌด์ž‘์ • ํฌ๊ฒŒ ํ•˜๋ฉด ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ตœ์†Œ๊ฐ’์ด ๋˜๋Š” ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์ง€๋งŒ ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ํ•™์Šต๋ฅ  ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ๋†’์€ ๊ฐ’์„ ๊ฐ€์งˆ ๋•Œ, ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ด ๋˜๋Š” ๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ o t ์˜ ๊ฐ’์ด ๋ฐœ์‚ฐํ•˜๋Š” ์ƒํ™ฉ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ํ•™์Šต๋ฅ  ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ๋‚ฎ์€ ๊ฐ’์„ ๊ฐ€์ง€๋ฉด ํ•™์Šต ์†๋„๊ฐ€ ๋Š๋ ค์ง€๋ฏ€๋กœ ์ ๋‹นํ•œ ์˜ ๊ฐ’์„ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” ๋Š” ๋ฐฐ์ œ์‹œํ‚ค๊ณ  ์ตœ์ ์˜ ๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์—๋งŒ ์ดˆ์ ์„ ๋งž์ถ”์–ด ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์˜ ์›๋ฆฌ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ๋Š”๋ฐ, ์‹ค์ œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ์™€์— ๋Œ€ํ•ด์„œ ๋™์‹œ์— ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ์ตœ์ ์˜ ์™€์˜ ๊ฐ’์„ ์ฐพ์•„๊ฐ‘๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜์ž๋ฉด ๊ฐ€์„ค, ๋น„์šฉ ํ•จ์ˆ˜, ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํฌ๊ด„์  ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ํ’€๊ณ ์ž ํ•˜๋Š” ๊ฐ ๋ฌธ์ œ์— ๋”ฐ๋ผ ๊ฐ€์„ค, ๋น„์šฉ ํ•จ์ˆ˜, ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ์ „๋ถ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์„ ํ˜• ํšŒ๊ท€์— ๊ฐ€์žฅ ์ ํ•ฉํ•œ ๋น„์šฉ ํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ์•Œ๋ ค์ ธ ์žˆ๋Š”๋ฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ์–ธ๊ธ‰๋œ MSE์™€ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์ด ๊ฐ๊ฐ ์ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 06-04 ์ž๋™ ๋ฏธ๋ถ„๊ณผ ์„ ํ˜• ํšŒ๊ท€ ์‹ค์Šต ์„ ํ˜• ํšŒ๊ท€๋ฅผ ํ…์„œ ํ”Œ๋กœ์™€ ์ผ€๋ผ์Šค๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 1. ์ž๋™ ๋ฏธ๋ถ„ import tensorflow as tf tape_gradient()๋Š” ์ž๋™ ๋ฏธ๋ถ„ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ž„์˜๋กœ w +๋ผ๋Š” ์‹์„ ์„ธ์›Œ๋ณด๊ณ ,์— ๋Œ€ํ•ด ๋ฏธ๋ถ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. w = tf.Variable(2.) def f(w): y = w**2 z = 2*y + 5 return z gradients๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด์— ๋Œ€ํ•ด ๋ฏธ๋ถ„ํ•œ ๊ฐ’์ด ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with tf.GradientTape() as tape: z = f(w) gradients = tape.gradient(z, [w]) print(gradients) [<tf.Tensor: shape=(), dtype=float32, numpy=8.0>] ์ด ์ž๋™ ๋ฏธ๋ถ„ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 2. ์ž๋™ ๋ฏธ๋ถ„์„ ์ด์šฉํ•œ ์„ ํ˜• ํšŒ๊ท€ ๊ตฌํ˜„ ์šฐ์„  ๊ฐ€์ค‘์น˜ ๋ณ€์ˆ˜ w์™€ b๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต๋  ๊ฐ’์ด๋ฏ€๋กœ ์ž„์˜์˜ ๊ฐ’์ธ 4์™€ 1๋กœ ์ดˆ๊ธฐํ™”ํ•˜์˜€์Šต๋‹ˆ๋‹ค. # ํ•™์Šต๋  ๊ฐ€์ค‘์น˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ w = tf.Variable(4.0) b = tf.Variable(1.0) ๊ฐ€์„ค์„ ํ•จ์ˆ˜๋กœ์„œ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. @tf.function def hypothesis(x): return w*x + b ํ˜„์žฌ์˜ ๊ฐ€์„ค์—์„œ w์™€ b๋Š” ๊ฐ๊ฐ 4์™€ 1์ด๋ฏ€๋กœ ์ž„์˜์˜ ์ž…๋ ฅ๊ฐ’์„ ๋„ฃ์—ˆ์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. x_test = [3.5, 5, 5.5, 6] print(hypothesis(x_test).numpy()) [15. 21. 23. 25.] ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ์†์‹ค ํ•จ์ˆ˜๋กœ์„œ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. @tf.function def mse_loss(y_pred, y): # ๋‘ ๊ฐœ์˜ ์ฐจ์ด ๊ฐ’์„ ์ œ๊ณฑ์„ ํ•ด์„œ ํ‰๊ท ์„ ์ทจํ•œ๋‹ค. return tf.reduce_mean(tf.square(y_pred - y)) ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” x์™€ y๊ฐ€ ์•ฝ 10๋ฐฐ์˜ ์ฐจ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. x = [1, 2, 3, 4, 5, 6, 7, 8, 9] # ๊ณต๋ถ€ํ•˜๋Š” ์‹œ๊ฐ„ y = [11, 22, 33, 44, 53, 66, 77, 87, 95] # ๊ฐ ๊ณต๋ถ€ ํ•˜๋Š” ์‹œ๊ฐ„์— ๋งคํ•‘๋˜๋Š” ์„ฑ์  ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•˜๋˜, ํ•™์Šต๋ฅ (learning rate)๋Š” 0.01์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. optimizer = tf.optimizers.SGD(0.01) ์•ฝ 300๋ฒˆ์— ๊ฑธ์ณ์„œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. for i in range(301): with tf.GradientTape() as tape: # ํ˜„์žฌ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ธฐ๋ฐ˜ํ•œ ์ž…๋ ฅ x์— ๋Œ€ํ•œ ์˜ˆ์ธก๊ฐ’์„ y_pred y_pred = hypothesis(x) # ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐ cost = mse_loss(y_pred, y) # ์†์‹ค ํ•จ์ˆ˜์— ๋Œ€ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๋ฏธ๋ถ„ ๊ฐ’ ๊ณ„์‚ฐ gradients = tape.gradient(cost, [w, b]) # ํŒŒ๋ผ๋ฏธํ„ฐ ์—…๋ฐ์ดํŠธ optimizer.apply_gradients(zip(gradients, [w, b])) if i % 10 == 0: print("epoch : {:3} | w์˜ ๊ฐ’ : {:5.4f} | b์˜ ๊ฐ’ : {:5.4} | cost : {:5.6f}".format(i, w.numpy(), b.numpy(), cost)) epoch : 0 | w์˜ ๊ฐ’ : 8.2133 | b์˜ ๊ฐ’ : 1.664 | cost : 1402.555542 ... ์ค‘๋žต ... epoch : 280 | w์˜ ๊ฐ’ : 10.6221 | b์˜ ๊ฐ’ : 1.191 | cost : 1.091434 epoch : 290 | w์˜ ๊ฐ’ : 10.6245 | b์˜ ๊ฐ’ : 1.176 | cost : 1.088940 epoch : 300 | w์˜ ๊ฐ’ : 10.6269 | b์˜ ๊ฐ’ : 1.161 | cost : 1.086645 w์™€ b ๊ฐ’์ด ๊ณ„์† ์—…๋ฐ์ดํŠธ ๋จ์— ๋”ฐ๋ผ์„œ cost๊ฐ€ ์ง€์†์ ์œผ๋กœ ์ค„์–ด๋“œ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต๋œ w์™€ b์˜ ๊ฐ’์— ๋Œ€ํ•ด์„œ ์ž„์˜ ์ž…๋ ฅ์„ ๋„ฃ์—ˆ์„ ๊ฒฝ์šฐ์˜ ์˜ˆ์ธก๊ฐ’์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. x_test = [3.5, 5, 5.5, 6] print(hypothesis(x_test).numpy()) [38.35479 54.295143 59.608593 64.92204 ] ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ•œ ๊ฐ€์ง€๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ํ…์„œ ํ”Œ๋กœ์˜ ๊ฒฝ์šฐ, ์ผ€๋ผ์Šค๋ผ๋Š” ๊ณ  ์ˆ˜์ค€์˜ API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ์„ ์ด๋ณด๋‹ค ์ข€ ๋” ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ์„ ์ผ€๋ผ์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 3. ์ผ€๋ผ์Šค๋กœ ๊ตฌํ˜„ํ•˜๋Š” ์„ ํ˜• ํšŒ๊ท€ ์ผ€๋ผ์Šค์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์˜ ๋”ฅ ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ์—์„œ ๋” ์ž์„ธํžˆ ๋ฐฐ์šฐ๊ฒ ์ง€๋งŒ, ๊ฐ„๋‹จํ•˜๊ฒŒ ์ผ€๋ผ์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์ผ€๋ผ์Šค๋กœ ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๊ธฐ๋ณธ์ ์ธ<NAME>์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Sequential๋กœ model์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ , ๊ทธ๋ฆฌ๊ณ  add๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ ๊ฐ™์€ ํ•„์š”ํ•œ ์ •๋ณด๋“ค์„ ์ถ”๊ฐ€ํ•ด๊ฐ‘๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ๋ด…์‹œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž์ธ 1์€ ์ถœ๋ ฅ์˜ ์ฐจ์›์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ output_dim์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ์ธ์ž์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ input_dim์€ ์ž…๋ ฅ์˜ ์ฐจ์›์„ ์ •์˜ํ•˜๋Š”๋ฐ ์ด๋ฒˆ ์‹ค์Šต๊ณผ ๊ฐ™์ด 1๊ฐœ์˜ ์‹ค์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ํ•˜๋Š” 1๊ฐœ์˜ ์‹ค์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ฐ๊ฐ 1์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.. # ์˜ˆ์‹œ ์ฝ”๋“œ. ์‹คํ–‰ ๋ถˆ๊ฐ€. model = Sequential() model.add(keras.layers.Dense(1, input_dim=1)) ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๊ฐ„๋‹จํ•˜์ง€๋งŒ, ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๊ฒƒ๋“ค์ด ์ง‘๋Œ€์„ฑ๋œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ๊ณต๋ถ€ํ•œ ์‹œ๊ฐ„์„, ๊ฐ ๊ณต๋ถ€ ํ•œ ์‹œ๊ฐ„์— ๋”ฐ๋ฅธ ์„ฑ์ ์„๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. activation์€ ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” linear๋ผ๊ณ  ๊ธฐ์žฌํ•ฉ๋‹ˆ๋‹ค. ์˜ตํ‹ฐ๋งˆ์ด์ €๋กœ ๊ธฐ๋ณธ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, sgd๋ผ๊ณ  ๊ธฐ์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต๋ฅ ์€ 0.01๋กœ ์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์†์‹ค ํ•จ์ˆ˜๋กœ๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ์‚ฌ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ›ˆ๋ จ ํšŸ์ˆ˜๋Š” 300์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras import optimizers x = [1, 2, 3, 4, 5, 6, 7, 8, 9] # ๊ณต๋ถ€ํ•˜๋Š” ์‹œ๊ฐ„ y = [11, 22, 33, 44, 53, 66, 77, 87, 95] # ๊ฐ ๊ณต๋ถ€ ํ•˜๋Š” ์‹œ๊ฐ„์— ๋งคํ•‘๋˜๋Š” ์„ฑ์  model = Sequential() # ์ถœ๋ ฅ y์˜ ์ฐจ์›์€ 1. ์ž…๋ ฅ x์˜ ์ฐจ์›(input_dim)์€ 1 # ์„ ํ˜• ํšŒ๊ท€์ด๋ฏ€๋กœ activation์€ 'linear' model.add(Dense(1, input_dim=1, activation='linear')) # sgd๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์˜๋ฏธ. ํ•™์Šต๋ฅ (learning rate, lr)์€ 0.01. sgd = optimizers.SGD(lr=0.01) # ์†์‹ค ํ•จ์ˆ˜(Loss function)์€ ํ‰๊ท ์ œ๊ณฑ์˜ค์ฐจ mse๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. model.compile(optimizer=sgd, loss='mse', metrics=['mse']) # ์ฃผ์–ด์ง„ x์™€ y ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ์ž‘์—…์„ 300๋ฒˆ ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค. model.fit(x, y, epochs=300) ํ•™์Šต์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ์„ ํƒ๋œ ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ์ง์„ ์„ ๊ทธ๋ž˜ํ”„๋กœ ๊ทธ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. plt.plot(x, model.predict(x), 'b', x, y, 'k.') ์œ„์˜ ๊ทธ๋ž˜ํ”„์—์„œ ๊ฐ ์ ์€ ์šฐ๋ฆฌ๊ฐ€ ์‹ค์ œ ์ฃผ์—ˆ๋˜ ์‹ค์ œ ๊ฐ’์— ํ•ด๋‹น๋˜๋ฉฐ, ์ง์„ ์€ ์‹ค์ œ ๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ์™€์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ง์„ ์ž…๋‹ˆ๋‹ค. ์ด ์ง์„ ์„ ํ†ตํ•ด 9์‹œ๊ฐ„ 30๋ถ„์„ ๊ณต๋ถ€ํ•˜์˜€์„ ๋•Œ์˜ ์‹œํ—˜ ์„ฑ์ ์„ ์˜ˆ์ธกํ•˜๊ฒŒ ํ•ด๋ด…์‹œ๋‹ค. model.predict()์€ ํ•™์Šต์ด ์™„๋ฃŒ๋œ ๋ชจ๋ธ์ด ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์–ด๋–ค ๊ฐ’์„ ์˜ˆ์ธกํ•˜๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. print(model.predict([9.5])) [[98.556465]] 9์‹œ๊ฐ„ 30๋ถ„์„ ๊ณต๋ถ€ํ•˜๋ฉด ์•ฝ 98.5์ ์„ ์–ป๋Š”๋‹ค๊ณ  ์˜ˆ์ธกํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 06-05 ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€(Logistic Regression) ์ผ์ƒ ์† ํ’€๊ณ ์ž ํ•˜๋Š” ๋งŽ์€ ๋ฌธ์ œ ์ค‘์—์„œ๋Š” ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ์ •๋‹ต์„ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‹œํ—˜์„ ๋ดค๋Š”๋ฐ ์ด ์‹œํ—˜ ์ ์ˆ˜๊ฐ€ ํ•ฉ๊ฒฉ์ธ์ง€ ๋ถˆํ•ฉ๊ฒฉ์ธ์ง€๊ฐ€ ๊ถ๊ธˆํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์–ด๋–ค ๋ฉ”์ผ์„ ๋ฐ›์•˜์„ ๋•Œ ์ด๊ฒŒ ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฌธ์ œ๋„ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ๋Œ€ํ‘œ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€(Logistic Regression)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 1. ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification) ์•ž์„œ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ์„ค๋ช…ํ•˜๋ฉฐ ๊ณต๋ถ€ ์‹œ๊ฐ„๊ณผ ์„ฑ์  ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ง์„ ์˜ ๋ฐฉ์ •์‹์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋Š” ๊ฐ€์„ค ํ•˜์—, ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜ ์™€ ํŽธํ–ฅ ๋ฅผ ์ฐพ์•„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์žฅ ์ž˜ ํ‘œํ˜„ํ•˜๋Š” ์ง์„ ์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋ฒˆ์— ๋ฐฐ์šธ ๋‘˜ ์ค‘ ํ•˜๋‚˜์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ์ •๋‹ต์„ ๊ณ ๋ฅด๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋Š” ์ง์„ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•™์ƒ๋“ค์ด ์‹œํ—˜ ์„ฑ์ ์— ๋”ฐ๋ผ์„œ ํ•ฉ๊ฒฉ, ๋ถˆํ•ฉ๊ฒฉ์ด ๊ธฐ์žฌ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ์‹œํ—˜ ์„ฑ์ ์ด๋ผ๋ฉด, ํ•ฉ๋ถˆ ๊ฒฐ๊ณผ๋Š”์ž…๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ํŠน์ • ์ ์ˆ˜๋ฅผ ์–ป์—ˆ์„ ๋•Œ์˜ ํ•ฉ๊ฒฉ, ๋ถˆํ•ฉ๊ฒฉ ์—ฌ๋ถ€๋ฅผ ํŒ์ •ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ ์ž ํ•ฉ์‹œ๋‹ค. score( ) result( ) 45 ๋ถˆํ•ฉ๊ฒฉ 50 ๋ถˆํ•ฉ๊ฒฉ 55 ๋ถˆํ•ฉ๊ฒฉ 60 ํ•ฉ๊ฒฉ 65 ํ•ฉ๊ฒฉ 70 ํ•ฉ๊ฒฉ ์œ„ ๋ฐ์ดํ„ฐ์—์„œ ํ•ฉ๊ฒฉ์„ 1, ๋ถˆํ•ฉ๊ฒฉ์„ 0์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ ๋“ค์„ ํ‘œํ˜„ํ•˜๋Š” ๊ทธ๋ž˜ํ”„๋Š” ์•ŒํŒŒ๋ฒณ์˜ S์ž ํ˜•ํƒœ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์™€์˜ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ง์„ ์„ ํ‘œํ˜„ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ S์ž ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ง์„ ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ณดํ†ต ๋ถ„๋ฅ˜ ์ž‘์—…์ด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ ์‹ค์ œ ๊ฐ’. ์ฆ‰, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ€ 0 ๋˜๋Š” 1์ด๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ๊ฐ’๋งŒ์„ ๊ฐ€์ง€๋ฏ€๋กœ, ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ ์˜ˆ์ธก๊ฐ’์€ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ํ™•๋ฅ ๋กœ ํ•ด์„ํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ๊ฐ€ ํ›จ์”ฌ ์šฉ์ดํ•ด์ง‘๋‹ˆ๋‹ค. ์ตœ์ข… ์˜ˆ์ธก๊ฐ’์ด 0.5๋ณด๋‹ค ์ž‘์œผ๋ฉด 0์œผ๋กœ ์˜ˆ์ธกํ–ˆ๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ณ , 0.5๋ณด๋‹ค ํฌ๋ฉด 1๋กœ ์˜ˆ์ธกํ–ˆ๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ = x b ์˜ ์ง์„ ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ๊ฐ’์ด ์Œ์˜ ๋ฌดํ•œ๋Œ€๋ถ€ํ„ฐ ์–‘์˜ ๋ฌดํ•œ๋Œ€์™€ ๊ฐ™์€ ํฐ ์ˆ˜๋“ค๋„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋Š” ์ง์„ ์ด ๋ถ„๋ฅ˜ ๋ฌธ์ œ์— ์ ํ•ฉํ•˜์ง€ ์•Š์€ ๋‘ ๋ฒˆ์งธ ์ด์œ ์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ด 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉด์„œ S์ž ํ˜•ํƒœ๋กœ ๊ทธ๋ ค์ง€๋Š” ํ•จ์ˆ˜๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜(Sigmoid function)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 2. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜(Sigmoid function) ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” ์ข…์ข… ฯƒ๋กœ ์ถ•์•ฝํ•ด์„œ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ๊ฐ€์„ค์„ ์„ธ์›Œ๋ด…์‹œ๋‹ค. ( ) 1 + โˆ’ ( x b ) s g o d ( x b ) ฯƒ ( x b ) ์—ฌ๊ธฐ์„œ e(e=2.718281...)๋Š” ์ž์—ฐ ์ƒ์ˆ˜๋ผ ๋ถˆ๋ฆฌ๋Š” ์ˆซ์ž์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ตฌํ•ด์•ผ ํ•  ๊ฒƒ์€ ์—ฌ์ „ํžˆ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ๊ฐ€์žฅ ์ ํ•ฉํ•œ ๊ฐ€์ค‘์น˜ (weight)์™€ ํŽธํ–ฅ (bias)์ž…๋‹ˆ๋‹ค. ์ธ๊ณต ์ง€๋Šฅ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ•˜๋Š” ๊ฒƒ์€ ๊ฒฐ๊ตญ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ์ ํ•ฉํ•œ ๊ฐ€์ค‘์น˜ ์™€๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ์‹œ๊ฐํ™”ํ•ด๋ด…์‹œ๋‹ค. import numpy as np import matplotlib.pyplot as plt ์•„๋ž˜์˜ ๊ทธ๋ž˜ํ”„๋Š” ๋Š” 1,๋Š” 0์ž„์„ ๊ฐ€์ •ํ•œ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. def sigmoid(x): return 1/(1+np.exp(-x)) x = np.arange(-5.0, 5.0, 0.1) y = sigmoid(x) plt.plot(x, y, 'g') plt.plot([0,0],[1.0,0.0], ':') # ๊ฐ€์šด๋ฐ ์ ์„  ์ถ”๊ฐ€ plt.title('Sigmoid Function') plt.show() ์œ„์˜ ๊ทธ๋ž˜ํ”„์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” ์ถœ๋ ฅ๊ฐ’์„ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ์กฐ์ •ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์น˜ S์ž์˜ ๋ชจ์–‘์„ ์—ฐ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ๊ฐ€ 0์ผ ๋•Œ ์ถœ๋ ฅ๊ฐ’์€ 0.5์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด 1์— ์ˆ˜๋ ดํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ์™€ ํŽธํ–ฅ ์ด ์ถœ๋ ฅ๊ฐ’์— ์–ด๋–ค ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”์ง€ ์‹œ๊ฐํ™”๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„ ์˜ ๊ฐ’์„ ๋ณ€ํ™”์‹œํ‚ค๊ณ  ์ด์— ๋”ฐ๋ฅธ ๊ทธ๋ž˜ํ”„๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. def sigmoid(x): return 1/(1+np.exp(-x)) x = np.arange(-5.0, 5.0, 0.1) y1 = sigmoid(0.5*x) y2 = sigmoid(x) y3 = sigmoid(2*x) plt.plot(x, y1, 'r', linestyle='--') # w์˜ ๊ฐ’์ด 0.5์ผ ๋•Œ plt.plot(x, y2, 'g') # w์˜ ๊ฐ’์ด 1์ผ ๋•Œ plt.plot(x, y3, 'b', linestyle='--') # w์˜ ๊ฐ’์ด 2์ผ ๋•Œ plt.plot([0,0],[1.0,0.0], ':') # ๊ฐ€์šด๋ฐ ์ ์„  ์ถ”๊ฐ€ plt.title('Sigmoid Function') plt.show() ์œ„ ๊ทธ๋ž˜ํ”„๋Š” ์˜ ๊ฐ’์ด 0.5์ผ ๋•Œ ๋นจ๊ฐ„์ƒ‰ ์„ ,์˜ ๊ฐ’์ด 1์ผ ๋•Œ๋Š” ์ดˆ๋ก์ƒ‰์„ , ์˜ ๊ฐ’์ด 2์ผ ๋•Œ ํŒŒ๋ž€์ƒ‰ ์„ ์ด ๋‚˜์˜ค๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.์˜ ๊ฐ’์— ๋”ฐ๋ผ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์‚ฌ๋„๊ฐ€ ๋ณ€ํ•ฉ๋‹ˆ๋‹ค. ์„ ํ˜• ํšŒ๊ท€์—์„œ ์ง์„ ์„ ํ‘œํ˜„ํ•  ๋•Œ, ๊ฐ€์ค‘์น˜๋Š” ์ง์„ ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ์˜๋ฏธํ–ˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์‚ฌ๋„๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.์˜ ๊ฐ’์ด ์ปค์ง€๋ฉด ๊ฒฝ์‚ฌ๊ฐ€ ์ปค์ง€๊ณ ์˜ ๊ฐ’์ด ์ž‘์•„์ง€๋ฉด ๊ฒฝ์‚ฌ๊ฐ€ ์ž‘์•„์ง‘๋‹ˆ๋‹ค.์˜ ๊ฐ’์— ๋”ฐ๋ผ์„œ ๊ทธ๋ž˜ํ”„๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. def sigmoid(x): return 1/(1+np.exp(-x)) x = np.arange(-5.0, 5.0, 0.1) y1 = sigmoid(x+0.5) y2 = sigmoid(x+1) y3 = sigmoid(x+1.5) plt.plot(x, y1, 'r', linestyle='--') # x + 0.5 plt.plot(x, y2, 'g') # x + 1 plt.plot(x, y3, 'b', linestyle='--') # x + 1.5 plt.plot([0,0],[1.0,0.0], ':') # ๊ฐ€์šด๋ฐ ์ ์„  ์ถ”๊ฐ€ plt.title('Sigmoid Function') plt.show() ์œ„ ๊ทธ๋ž˜ํ”„๋Š” ๊ฐ’์— ๋”ฐ๋ผ์„œ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๊ฐ’์ด ์ปค์ง€๋ฉด 1์— ์ˆ˜๋ ดํ•˜๊ณ , ์ž…๋ ฅ๊ฐ’์ด ์ž‘์•„์ง€๋ฉด 0์— ์ˆ˜๋ ดํ•ฉ๋‹ˆ๋‹ค. 0๋ถ€ํ„ฐ์˜ 1๊นŒ์ง€์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š”๋ฐ ์ถœ๋ ฅ๊ฐ’์ด 0.5 ์ด์ƒ์ด๋ฉด 1(True), 0.5์ดํ•˜๋ฉด 0(False)๋กœ ๋งŒ๋“ค๋ฉด ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ๋น„์šฉ ํ•จ์ˆ˜(Cost function) ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋˜ํ•œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ ์ฐพ์•„๋‚ด์ง€๋งŒ, ๋น„์šฉ ํ•จ์ˆ˜๋กœ๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ๋น„์šฉ ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋Š” ์ข‹์ง€ ์•Š์€ ๋กœ์ปฌ ๋ฏธ๋‹ˆ๋ฉˆ์— ๋น ์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ์ง€๋‚˜์น˜๊ฒŒ ๋†’์•„ ๋ฌธ์ œ ํ•ด๊ฒฐ์ด ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ๋น„์šฉ ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋ฉด, ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•˜์˜€์„ ๋•Œ ์ฐพ๊ณ ์ž ํ•˜๋Š” ์ตœ์†Ÿ๊ฐ’์ด ์•„๋‹Œ ์ž˜๋ชป๋œ ์ตœ์†Ÿ๊ฐ’์— ๋น ์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ๋งค์šฐ ๋†’์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ „์ฒด ํ•จ์ˆ˜์— ๊ฑธ์ณ ์ตœ์†Ÿ๊ฐ’์ธ ๊ธ€๋กœ๋ฒŒ ๋ฏธ๋‹ˆ๋ฉˆ(Global Minimum) ์ด ์•„๋‹Œ ํŠน์ • ๊ตฌ์—ญ์—์„œ์˜ ์ตœ์†Ÿ๊ฐ’์ธ ๋กœ์ปฌ ๋ฏธ๋‹ˆ๋ฉˆ(Local Minimum)์— ๋„๋‹ฌํ–ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋กœ์ปฌ ๋ฏธ๋‹ˆ๋ฉˆ์— ์ง€๋‚˜์น˜๊ฒŒ ์‰ฝ๊ฒŒ ๋น ์ง€๋Š” ๋น„์šฉ ํ•จ์ˆ˜๋Š” cost๊ฐ€ ๊ฐ€๋Šฅํ•œ ํ•œ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ์ฐพ๋Š”๋‹ค๋Š” ๋ชฉ์ ์—๋Š” ์ข‹์ง€ ์•Š์€ ์„ ํƒ์ž…๋‹ˆ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ์˜ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋Š” ๋ฐ”๋กœ ๊ทธ ์ข‹์ง€ ์•Š์€ ์„ ํƒ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ผ๋Š” ๋ฌธ์ œ์—์„œ ๊ฐ€์ค‘์น˜๋ฅผ ์ตœ์†Œ๋กœ ๋งŒ๋“œ๋Š” ์ ์ ˆํ•œ ์ƒˆ๋กœ์šด ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ์•„๋ž˜์˜ ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ๋ชฉ์  ํ•จ์ˆ˜๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค.๋Š” ๋ชฉ์  ํ•จ์ˆ˜(objective function)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ( ) 1 โˆ‘ = n ( ( ( ) ) y ( ) ) ) ์•„์ง ์™„์„ฑ๋œ ์‹์ด ์•„๋‹™๋‹ˆ๋‹ค. ์œ„์˜ ์‹์—์„œ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐœ๊ณ , ์–ด๋–ค ํ•จ์ˆ˜ ๊ฐ€ ์‹ค์ œ ๊ฐ’ i ์™€ ์˜ˆ์ธก๊ฐ’ ( i ) ์˜ ์˜ค์ฐจ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•จ์ˆ˜๋ผ๊ณ  ํ•  ๋•Œ, ์—ฌ๊ธฐ์„œ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์ •์˜ํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ์„œ ๊ฐ€์ค‘์น˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ์ ์ ˆํ•œ ๋ชฉ์  ํ•จ์ˆ˜๊ฐ€ ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ชฉ์  ํ•จ์ˆ˜๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์–ด๋–ค ํ•จ์ˆ˜์˜ ๊ฐ’์˜ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ ์ ˆํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์— ๋Œ€ํ•œ ์˜ค์ฐจ๋ฅผ ์ค„์—ฌ์•ผ ํ•˜๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ ์ด๋Š” ๋น„์šฉ ํ•จ์ˆ˜(cost function)๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹์„ ๋‹ค์‹œ ์“ฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) 1 โˆ‘ = n o t ( ( ( ) ) y ( ) ) ) ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์‹ค์ œ ๊ฐ’์ด 0์ผ ๋•Œ ๊ฐ’์ด 1์— ๊ฐ€๊นŒ์›Œ์ง€๋ฉด ์˜ค์ฐจ๊ฐ€ ์ปค์ง€๋ฉฐ ์‹ค์ œ ๊ฐ’์ด 1์ผ ๋•Œ ๊ฐ’์ด 0์— ๊ฐ€๊นŒ์›Œ์ง€๋ฉด ์˜ค์ฐจ๊ฐ€ ์ปค์ง์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋Š” ๋กœ๊ทธ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ‘œํ˜„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. if = โ†’ cost ( ( ) y ) โˆ’ log ( ( ) ) if = โ†’ cost ( ( ) y ) โˆ’ log ( โˆ’ ( ) )์˜ ์‹ค์ œ ๊ฐ’์ด 1์ผ ๋•Œ l g ( ) ๊ทธ๋ž˜ํ”„๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์˜ ์‹ค์ œ ๊ฐ’์ด 0์ผ ๋•Œ l g ( โˆ’ ( ) ) ๊ทธ๋ž˜ํ”„๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๋‘ ์‹์„ ๊ทธ๋ž˜ํ”„ ์ƒ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’ ๊ฐ€ 1์ผ ๋•Œ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ํŒŒ๋ž€์ƒ‰ ์„ ์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์œผ๋ฉฐ, ์‹ค์ œ ๊ฐ’ ๊ฐ€ 0์ผ ๋•Œ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋นจ๊ฐ„์ƒ‰ ์„ ์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ฐ„๋žตํžˆ ์„ค๋ช…ํ•˜๋ฉด, ์‹ค์ œ ๊ฐ’์ด 1์ผ ๋•Œ, ์˜ˆ์ธก๊ฐ’์ธ ( ) ์˜ ๊ฐ’์ด 1์ด๋ฉด ์˜ค์ฐจ๊ฐ€ 0์ด๋ฏ€๋กœ ๋‹น์—ฐํžˆ cost๋Š” 0์ด ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ์‹ค์ œ ๊ฐ’์ด 1์ผ ๋•Œ, ( ) ๊ฐ€ 0์œผ๋กœ ์ˆ˜๋ ดํ•˜๋ฉด cost๋Š” ๋ฌดํ•œ๋Œ€๋กœ ๋ฐœ์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’์ด 0์ธ ๊ฒฝ์šฐ๋Š” ๊ทธ ๋ฐ˜๋Œ€๋กœ ์ดํ•ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜๋‚˜์˜ ์‹์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. cost ( ( ) y ) โˆ’ [ l g ( ) ( โˆ’ ) o ( โˆ’ ( ) ) ] ์ž์„ธํžˆ ๋ณด๋ฉด ์™€ ( โˆ’ ) ๊ฐ€ ์‹ ์ค‘๊ฐ„์— ๋“ค์–ด๊ฐ”๊ณ , ๋‘ ๊ฐœ์˜ ์‹์„ -๋กœ ๋ฌถ์€ ๊ฒƒ ์™ธ์—๋Š” ๊ธฐ์กด์˜ ๋‘ ์‹์ด ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€ 0์ด๋ฉด l g ( ) ๊ฐ€ ์—†์–ด์ง€๊ณ , ๊ฐ€ 1์ด๋ฉด ( โˆ’ ) o ( โˆ’ ( ) ) ๊ฐ€ ์—†์–ด์ง€๋Š”๋ฐ ์ด๋Š” ๊ฐ๊ฐ ๊ฐ€ 1์ผ ๋•Œ์™€ ๊ฐ€ 0์ผ ๋•Œ์˜ ์•ž์„œ ๋ณธ ์‹๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ๋ชฉ์  ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) โˆ’ n i 1 [ ( ) o H ( ( ) ) ( โˆ’ ( ) ) o ( โˆ’ ( ( ) ) ) ] ์ด๋•Œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ์ฐพ์•„๋‚ธ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ(Cross Entropy) ํ•จ์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋Š” ๋น„์šฉ ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ๊ฐ€์ค‘์น˜๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์˜ ํ‰๊ท ์„ ์ทจํ•œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ๋น„์šฉ ํ•จ์ˆ˜์ด๊ธฐ๋„ ํ•˜๋ฏ€๋กœ ๋’ค์—์„œ ์žฌ ์–ธ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. 06-06 ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ์‹ค์Šต ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์ผ€๋ผ์Šค๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 1. ์ผ€๋ผ์Šค๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋…๋ฆฝ ๋ณ€์ˆ˜ ๋ฐ์ดํ„ฐ๋ฅผ, ์ˆซ์ž 10 ์ด์ƒ์ธ ๊ฒฝ์šฐ์—๋Š” 1, ๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ์—๋Š” 0์„ ๋ถ€์—ฌํ•œ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋ฒˆ ๋ฐ์ดํ„ฐ๋Š” ์•ž์„œ ๋ฐฐ์šด ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€ ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 1๊ฐœ์˜ ์‹ค์ˆ˜๋กœ๋ถ€ํ„ฐ 1๊ฐœ์˜ ์‹ค์ˆ˜์ธ ๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋งคํ•‘ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ Dense์˜ output_dim, input_dim ์ธ์ž ๊ฐ’์œผ๋กœ ๊ฐ๊ฐ 1์„ ๊ธฐ์žฌํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ activation์˜ ์ธ์ž ๊ฐ’์œผ๋กœ๋Š” sigmoid๋ฅผ ๊ธฐ์žฌํ•ด ์ค๋‹ˆ๋‹ค. ์˜ตํ‹ฐ๋งˆ์ด์ €๋กœ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฒฝ์‚ฌ ํ•˜๊ฐ• ๋ฒ•์ธ sgd๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์— ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ binary_crossentropy๋ฅผ ๊ธฐ์žฌํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ํฌํฌ๋Š” 200์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras import optimizers x = np.array([-50, -40, -30, -20, -10, -5, 0, 5, 10, 20, 30, 40, 50]) y = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) # ์ˆซ์ž 10๋ถ€ํ„ฐ 1 model = Sequential() model.add(Dense(1, input_dim=1, activation='sigmoid')) sgd = optimizers.SGD(lr=0.01) model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['binary_accuracy']) model.fit(x, y, epochs=200) ์ด 200ํšŒ์— ๊ฑธ์ณ ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ์™€๋ฅผ ์ฐพ์•„๋‚ด๋Š” ์ž‘์—…์„ ํ•ฉ๋‹ˆ๋‹ค. ์ €์ž์˜ ๊ฒฝ์šฐ ์•ฝ 190ํšŒ๋ถ€ํ„ฐ ์ •ํ™•๋„๊ฐ€ 100%๊ฐ€ ๋‚˜์˜ค๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’๊ณผ ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋„๋ก ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ ์™€์˜ ๊ฐ’์„ ๊ฐ€์ง„ ๋ชจ๋ธ์„ ์ด์šฉํ•˜์—ฌ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. plt.plot(x, model.predict(x), 'b', x, y, 'k.')์˜ ๊ฐ’์ด 5์™€ 10์‚ฌ์ด์˜ ์–ด๋–ค ๊ฐ’์ผ ๋•Œ ๊ฐ’์ด 0.5๊ฐ€ ๋„˜๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ •ํ™•๋„๊ฐ€ 100%๊ฐ€ ๋‚˜์™”์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ ์–ด๋„์˜ ๊ฐ’์ด 5์ผ ๋•Œ๋Š” ๊ฐ’์ด 0.5๋ณด๋‹ค ์ž‘๊ณ ,์˜ ๊ฐ’์ด 10์ผ ๋•Œ๋Š” ๊ฐ’์ด 0.5๋ฅผ ๋„˜์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์˜ ๊ฐ’์ด 5๋ณด๋‹ค ์ž‘์€ ๊ฐ’์ผ ๋•Œ์™€์˜ ๊ฐ’์ด 10๋ณด๋‹ค ํด ๋•Œ์— ๋Œ€ํ•ด์„œ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(model.predict([1, 2, 3, 4, 4.5])) print(model.predict([11, 21, 31, 41, 500])) [[0.21071826] [0.26909265] [0.33673897] [0.41180944] [0.45120454]] [[0.86910886] [0.99398106] [0.99975663] [0.9999902 ] [1. ]]์˜ ๊ฐ’์ด 5๋ณด๋‹ค ์ž‘์„ ๋•Œ๋Š” 0.5๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„,์˜ ๊ฐ’์ด 10๋ณด๋‹ค ํด ๋•Œ๋Š” 0.5๋ณด๋‹ค ํฐ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 06-07 ๋‹ค์ค‘ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์‹ค์Šต ๋…๋ฆฝ ๋ณ€์ˆ˜ ๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๋น„์šฉ ํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ € ๋“ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. import numpy as np from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras import optimizers 1. ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ ๋”ฅ ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด ๋Œ€๋ถ€๋ถ„์˜ ์ž…๋ ฅ๋“ค์€ ๋…๋ฆฝ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์ง์ ‘ ์ฝ”๋”ฉํ•˜๋Š” ๊ด€์ ์—์„œ๋Š” ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 2 ์ด์ƒ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐ ์žˆ์–ด ๋…๋ฆฝ ๋ณ€์ˆ˜๊ฐ€ 3๊ฐœ์ธ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ํ’€์–ด๋ด…์‹œ๋‹ค. ์ค‘๊ฐ„๊ณ ์‚ฌ, ๊ธฐ๋ง๊ณ ์‚ฌ, ๊ทธ๋ฆฌ๊ณ  ์ถ”๊ฐ€ ์ ์ˆ˜๋ฅผ ์–ด๋–ค ๊ณต์‹์„ ํ†ตํ•ด ์ตœ์ข… ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Midterm( 1 ) Final( 2 ) Added point( 3 ) Score($1000)(y) 70 85 11 73 71 89 18 82 50 80 20 72 99 20 10 57 50 10 10 34 20 99 10 58 40 50 20 56 ( ) w x + 2 2 w x + 3๊ฐœ์˜ ํŠน์„ฑ์„ ๊ฐ€์ง„ ๋ฒกํ„ฐ [ 1 x, 3 ] ๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ ์ค‘ ์ƒ์œ„ 5๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•˜๊ณ , ๋‚˜๋จธ์ง€ 2๊ฐœ๋Š” ํ…Œ์ŠคํŠธ์— ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ์˜ ์ฐจ์›์ด 3์œผ๋กœ ๋ฐ”๋€Œ๋ฉด์„œ, input_dim์˜ ์ธ์ž ๊ฐ’์ด 3์œผ๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ž…๋ ฅ ๋ฒกํ„ฐ์˜์˜ ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ๋ผ๊ณ ๋„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ , ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 3์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. # ์ค‘๊ฐ„๊ณ ์‚ฌ, ๊ธฐ๋ง๊ณ ์‚ฌ, ๊ฐ€์‚ฐ์  ์ ์ˆ˜ X = np.array([[70,85,11], [71,89,18], [50,80,20], [99,20,10], [50,10,10]]) y = np.array([73, 82 ,72, 57, 34]) # ์ตœ์ข… ์„ฑ์  model = Sequential() model.add(Dense(1, input_dim=3, activation='linear')) sgd = optimizers.SGD(learning_rate=0.0001) model.compile(optimizer=sgd, loss='mse', metrics=['mse']) model.fit(X, y, epochs=2000) ๋ชจ๋ธ์˜ ํ•™์Šต์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ํ•™์Šต๋œ ๋ชจ๋ธ์— ์ž…๋ ฅ X์— ๋Œ€ํ•œ ์˜ˆ์ธก์„ ํ•ด๋ด…์‹œ๋‹ค. print(model.predict(X)) [[73.15294 ] [81.98001 ] [71.93192 ] [57.161617] [33.669353]] ์‹ค์ œ ๊ฐ’์— ๊ทผ์ ‘ํ•œ ์˜ˆ์ธก์„ ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จํ•  ๋•Œ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ˆ์ธก ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. X_test = np.array([[20,99,10], [40,50,20]]) print(model.predict(X_test)) [[58.08134 ] [55.734634]] 2. ๋‹ค์ค‘ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐ ์žˆ์–ด ๋…๋ฆฝ ๋ณ€์ˆ˜ ๊ฐ€ 2๊ฐœ์ธ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ํ’€์–ด๋ด…์‹œ๋‹ค. ๊ฝƒ๋ฐ›์นจ(Sepal)์˜ ๊ธธ์ด์™€ ๊ฝƒ์žŽ(Petal)์˜ ๊ธธ์ด์™€ ํ•ด๋‹น ๊ฝƒ์ด A ์ธ์ง€ B ์ธ์ง€๊ฐ€ ์ ํ˜€์ ธ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์„ ๋•Œ, ์ƒˆ๋กœ ์กฐ์‚ฌํ•œ ๊ฝƒ๋ฐ›์นจ์˜ ๊ธธ์ด์™€ ๊ฝƒ์žŽ์˜ ๊ธธ์ด๋กœ๋ถ€ํ„ฐ ๋ฌด์Šจ ๊ฝƒ์ธ์ง€ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ ์ž ํ•œ๋‹ค๋ฉด ์ด๋•Œ ๋…๋ฆฝ ๋ณ€์ˆ˜๋Š” 2๊ฐœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. SepalLengthCm( 1 ) PetalLengthCm( 2 ) Species(y) 5.1 3.5 A 4.7 3.2 A 5.2 1.8 B 7 4.1 A 5.1 2.1 B ( ) s g o d ( 1 1 w x + ) 2๊ฐœ์˜ ํŠน์„ฑ์„ ๊ฐ€์ง„ ๋ฒกํ„ฐ [ 1 x ] ๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ๋…๋ฆฝ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ ์ข€ ๊ฐ„๋‹จํ•œ ์ƒˆ๋กœ์šด ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ด๋ฅผ ์ผ€๋ผ์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ 1 x์˜ ํ•ฉ์ด 2 ์ด์ƒ์ด๋ฉด ์ถœ๋ ฅ๊ฐ’ ๊ฐ€ 1์ด ๋˜๊ณ  ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์˜ ํ•ฉ์ด 2๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ์—๋งŒ ์ถœ๋ ฅ๊ฐ’์ด 0์ด ๋˜๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์•ž์„œ ์‹ค์Šตํ•œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ์ฝ”๋“œ์™€ ๊ฑฐ์˜ ๋™์ผํ•œ๋ฐ ๋‹ฌ๋ผ์ง„ ์ ์€ ์ž…๋ ฅ์˜ ์ฐจ์›์ด 2๋กœ ๋ฐ”๋€Œ๋ฉด์„œ input_dim์˜ ๊ฐ’์ด 2๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 2์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. X = np.array([[0, 0], [0, 1], [1, 0], [0, 2], [1, 1], [2, 0]]) y = np.array([0, 0, 0, 1, 1, 1]) model = Sequential() model.add(Dense(1, input_dim=2, activation='sigmoid')) model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['binary_accuracy']) model.fit(X, y, epochs=2000) 2000์—ํฌํฌ ์ •๋„๋กœ ํ•™์Šต์„ ๋ฉˆ์ถ”๊ณ  ๊ฐ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅ๊ฐ’์ด 0.5๋ณด๋‹ค ํฌ๊ณ  ์ž‘์€์ง€๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(model.predict(X)) [[0.23379876] [0.48773268] [0.4808667 ] [0.7481605 ] [0.74294543] [0.7376603 ]] ์ž…๋ ฅ์˜ ํ•ฉ์ด 2 ์ด์ƒ์ธ ๊ฒฝ์šฐ์—๋Š” ์ „๋ถ€ ๊ฐ’์ด 0.5๋ฅผ ๋„˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋‹ค์ด์–ด๊ทธ๋žจ ๋‹ค์ค‘ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•„์ง ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๋ฐฐ์šฐ์ง€ ์•Š์•˜์Œ์—๋„ ์ด๋ ‡๊ฒŒ ๋‹ค์ด์–ด๊ทธ๋žจ์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณด๋Š” ์ด์œ ๋Š” ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์ผ์ข…์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๊ตฌ์กฐ๋กœ ํ•ด์„ํ•ด๋„ ๋ฌด๋ฐฉํ•จ์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. = i m i ( 1 1 w x + 3 3. . w x + ) ฯƒ ( 1 06-08 ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ ์•ž์„œ ๋…๋ฆฝ ๋ณ€์ˆ˜ ๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ์„ ํ˜• ํšŒ๊ท€์™€ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‹ค์Œ ์‹ค์Šต์ธ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ๋Š” ์ข…์† ๋ณ€์ˆ˜์˜ ์ข…๋ฅ˜๋„ 3๊ฐœ ์ด์ƒ์ด ๋˜๋ฉด์„œ ๋”์šฑ ๋ณต์žกํ•ด์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ์‹๋“ค์ด ๊ฒน๊ฒน์ด ๋ˆ„์ ๋˜๋ฉด ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ฐœ๋…์ด ๋ฉ๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค๋Š” ์‚ฌ์šฉํ•˜๊ธฐ๊ฐ€ ํŽธ๋ฆฌํ•ด์„œ ์ด๋Ÿฐ ๊ณ ๋ฏผ์„ ํ•  ์ผ์ด ์ƒ๋Œ€์ ์œผ๋กœ ์ ์ง€๋งŒ, Numpy๋‚˜ ํ…์„œ ํ”Œ๋กœ์˜ ๋กœ์šฐ-๋ ˆ๋ฒจ(low-level)์˜ ๋จธ์‹  ๋Ÿฌ๋‹ ๊ฐœ๋ฐœ์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ ๋ณ€์ˆ˜๋“ค์˜ ์—ฐ์‚ฐ์„ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ์ดํ„ฐ์™€ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋กœ๋ถ€ํ„ฐ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ, ๋” ๋‚˜์•„๊ฐ€ ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ์‚ฐ์ •ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ๊ณผ ํ…์„œ ๋ฒกํ„ฐ๋Š” ํฌ๊ธฐ์™€ ๋ฐฉํ–ฅ์„ ๊ฐ€์ง„ ์–‘์ž…๋‹ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ๋‚˜์—ด๋œ ํ˜•์ƒ์ด๋ฉฐ ํŒŒ์ด์ฌ์—์„œ๋Š” 1์ฐจ์› ๋ฐฐ์—ด ๋˜๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ํ–‰๋ ฌ์€ ํ–‰๊ณผ ์—ด์„ ๊ฐ€์ง€๋Š” 2์ฐจ์› ํ˜•์ƒ์„ ๊ฐ€์ง„ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” 2์ฐจ์› ๋ฐฐ์—ด๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋กœ์ค„์„ ํ–‰(row)๋ผ๊ณ  ํ•˜๋ฉฐ, ์„ธ๋กœ์ค„์„ ์—ด(column)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3์ฐจ์›๋ถ€ํ„ฐ๋Š” ์ฃผ๋กœ ํ…์„œ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ํ…์„œ๋Š” ํŒŒ์ด์ฌ์—์„œ๋Š” 3์ฐจ์› ์ด์ƒ์˜ ๋ฐฐ์—ด๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. 2. ํ…์„œ(Tensor) ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ๋ณต์žกํ•œ ๋ชจ๋ธ ๋‚ด์˜ ์—ฐ์‚ฐ์„ ์ฃผ๋กœ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” ํ–‰๋ ฌ ์—ฐ์‚ฐ์ด๋ž€ ๋‹จ์ˆœํžˆ 2์ฐจ์› ๋ฐฐ์—ด์„ ํ†ตํ•œ ํ–‰๋ ฌ ์—ฐ์‚ฐ๋งŒ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹์˜ ์ž…, ์ถœ๋ ฅ์ด ๋ณต์žกํ•ด์ง€๋ฉด 3์ฐจ์› ํ…์„œ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์ˆ˜๋กœ ์š”๊ตฌ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ ์ค‘ ํ•˜๋‚˜์ธ RNN์—์„œ๋Š” 3์ฐจ์› ํ…์„œ์— ๋Œ€ํ•œ ๊ฐœ๋… ์ดํ•ด ์—†์ด๋Š” ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Numpy๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์„œ๋ฅผ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import numpy as np 1) 0์ฐจ์› ํ…์„œ(์Šค์นผ๋ผ) ์Šค์นผ๋ผ๋Š” ํ•˜๋‚˜์˜ ์‹ค์ˆซ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ 0์ฐจ์› ํ…์„œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฐจ์›์„ ์˜์–ด๋กœ Dimension์ด๋ผ๊ณ  ํ•˜๋ฏ€๋กœ 0D ํ…์„œ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. d = np.array(5) print('ํ…์„œ์˜ ์ฐจ์› :',d.ndim) print('ํ…์„œ์˜ ํฌ๊ธฐ(shape) :',d.shape) ํ…์„œ์˜ ์ฐจ์› : 0 ํ…์„œ์˜ ํฌ๊ธฐ(shape) : () Numpy์˜ ndim์„ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ ๋‚˜์˜ค๋Š” ๊ฐ’์— ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. ndim์„ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ ๋‚˜์˜ค๋Š” ๊ฐ’์„ ์šฐ๋ฆฌ๋Š” ์ถ•(axis)์˜ ๊ฐœ์ˆ˜ ๋˜๋Š” ํ…์„œ์˜ ์ฐจ์›์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ ์ด ๋‘ ์šฉ์–ด๋ฅผ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. 2) 1์ฐจ์› ํ…์„œ(๋ฒกํ„ฐ) ์ˆซ์ž๋ฅผ ๋ฐฐ์—ดํ•œ ๊ฒƒ์„ ๋ฒกํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฒกํ„ฐ๋Š” 1์ฐจ์› ํ…์„œ์ž…๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ๋ฒกํ„ฐ์—์„œ๋„ ์ฐจ์›์ด๋ผ๋Š” ์šฉ์–ด๋ฅผ ์“ฐ๋Š”๋ฐ, ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ ํ…์„œ์˜ ์ฐจ์›์€ ๋‹ค๋ฅธ ๊ฐœ๋…์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ๋Š” 4์ฐจ์› ๋ฒกํ„ฐ์ด์ง€๋งŒ, 1์ฐจ์› ํ…์„œ์ž…๋‹ˆ๋‹ค. 1D ํ…์„œ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. d = np.array([1, 2, 3, 4]) print('ํ…์„œ์˜ ์ฐจ์› :',d.ndim) print('ํ…์„œ์˜ ํฌ๊ธฐ(shape) :',d.shape) ํ…์„œ์˜ ์ฐจ์› : 1 ํ…์„œ์˜ ํฌ๊ธฐ(shape) : (4, ) ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ ํ…์„œ์˜ ์ฐจ์›์˜ ์ •์˜๋กœ ์ธํ•ด ํ˜ผ๋™ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๋ฒกํ„ฐ์—์„œ์˜ ์ฐจ์›์€ ํ•˜๋‚˜์˜ ์ถ•์— ๋†“์ธ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด๊ณ , ํ…์„œ์—์„œ์˜ ์ฐจ์›์€ ์ถ•์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 3) 2์ฐจ์› ํ…์„œ(ํ–‰๋ ฌ) ํ–‰๊ณผ ์—ด์ด ์กด์žฌํ•˜๋Š” ๋ฒกํ„ฐ์˜ ๋ฐฐ์—ด. ์ฆ‰, ํ–‰๋ ฌ(matrix)์„ 2์ฐจ์› ํ…์„œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2D ํ…์„œ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. # 3ํ–‰ 4์—ด์˜ ํ–‰๋ ฌ d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) print('ํ…์„œ์˜ ์ฐจ์› :',d.ndim) print('ํ…์„œ์˜ ํฌ๊ธฐ(shape) :',d.shape) ํ…์„œ์˜ ์ฐจ์› : 2 ํ…์„œ์˜ ํฌ๊ธฐ(shape) : (3, 4) ํ…์„œ์˜ ํฌ๊ธฐ(shape)์— ๋Œ€ํ•ด์„œ๋„ ์ •๋ฆฌํ•ฉ์‹œ๋‹ค. ํ…์„œ์˜ ํฌ๊ธฐ๋ž€, ๊ฐ ์ถ•์„ ๋”ฐ๋ผ์„œ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์ฐจ์›์ด ์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฐ’์ž…๋‹ˆ๋‹ค. ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ”๋กœ ๋จธ๋ฆฟ์†์œผ๋กœ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์œผ๋ฉด ๋ชจ๋ธ ์„ค๊ณ„ ์‹œ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ์–ด๋ ค์šธ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ์ˆœ์ฐจ์ ์œผ๋กœ ํ™•์žฅํ•ด๋‚˜๊ฐ€๋ฉฐ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ๋„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ฒฝ์šฐ 3๊ฐœ์˜ ์ปค๋‹ค๋ž€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”๋ฐ ๊ทธ ๊ฐ๊ฐ์˜ ์ปค๋‹ค๋ž€ ๋ฐ์ดํ„ฐ๋Š” ์ž‘์€ ๋ฐ์ดํ„ฐ 4๊ฐœ๋กœ ์ด๋ฃจ์–ด์กŒ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4) 3์ฐจ์› ํ…์„œ(๋‹ค์ฐจ์› ๋ฐฐ์—ด) ํ–‰๋ ฌ ๋˜๋Š” 2์ฐจ์› ํ…์„œ๋ฅผ ๋‹จ์œ„๋กœ ํ•œ ๋ฒˆ ๋” ๋ฐฐ์—ดํ•˜๋ฉด 3์ฐจ์› ํ…์„œ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 3D ํ…์„œ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ 0์ฐจ์› ~ 2์ฐจ์› ํ…์„œ๋Š” ๊ฐ๊ฐ ์Šค์นผ๋ผ, ๋ฒกํ„ฐ, ํ–‰๋ ฌ์ด๋ผ๊ณ  ํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋ฏ€๋กœ 3์ฐจ์› ์ด์ƒ์˜ ํ…์„œ๋ถ€ํ„ฐ ๋ณธ๊ฒฉ์ ์œผ๋กœ ํ…์„œ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์‚ฌ์ด์–ธ์Šค ๋ถ„์•ผ ํ•œ์ •์œผ๋กœ ์ฃผ๋กœ 3์ฐจ์› ์ด์ƒ์˜ ๋ฐฐ์—ด์„ ํ…์„œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค๊ณ  ์ดํ•ดํ•ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค. 3D ํ…์„œ๋Š” ์ ์–ด๋„ ์—ฌ๊ธฐ์„œ๋Š” 3์ฐจ์› ๋ฐฐ์—ด๋กœ ์ดํ•ดํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด 3์ฐจ์› ํ…์„œ์˜ ๊ตฌ์กฐ๋ฅผ ์ดํ•ดํ•˜์ง€ ์•Š์œผ๋ฉด, ๋ณต์žกํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ž…, ์ถœ๋ ฅ๊ฐ’์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐœ๋… ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์ง€๋งŒ ๋ฐ˜๋“œ์‹œ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. d = np.array([ [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [10, 11, 12, 13, 14]], [[15, 16, 17, 18, 19], [19, 20, 21, 22, 23], [23, 24, 25, 26, 27]] ]) print('ํ…์„œ์˜ ์ฐจ์› :',d.ndim) print('ํ…์„œ์˜ ํฌ๊ธฐ(shape) :',d.shape) ํ…์„œ์˜ ์ฐจ์› : 3 ํ…์„œ์˜ ํฌ๊ธฐ(shape) : (2, 3, 5) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํŠนํžˆ ์ž์ฃผ ๋ณด๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด ์ด 3D ํ…์„œ์ž…๋‹ˆ๋‹ค. 3D ํ…์„œ๋Š” ์‹œํ€€์Šค ๋ฐ์ดํ„ฐ(sequence data)๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‹œํ€€์Šค ๋ฐ์ดํ„ฐ๋Š” ์ฃผ๋กœ ๋‹จ์–ด์˜ ์‹œํ€€์Šค๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ์‹œํ€€์Šค๋Š” ์ฃผ๋กœ ๋ฌธ์žฅ์ด๋‚˜ ๋ฌธ์„œ, ๋‰ด์Šค ๊ธฐ์‚ฌ ๋“ฑ์˜ ํ…์ŠคํŠธ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ 3D ํ…์„œ๋Š” (samples, timesteps, word_dim)์ด ๋ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ์ผ๊ด„๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ๋Š” ๋‹จ์œ„์ธ ๋ฐฐ์น˜์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ๋’ค์—์„œ ๋ฐฐ์šธ ํ…๋ฐ (batch_size, timesteps, word_dim)์ด๋ผ๊ณ ๋„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. samples ๋˜๋Š” batch_size๋Š” ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜, timesteps๋Š” ์‹œํ€€์Šค์˜ ๊ธธ์ด, word_dim์€ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋” ์ƒ์„ธํ•œ ์„ค๋ช…์€ RNN ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์™œ 3D ํ…์„œ์˜ ๊ฐœ๋…์ด ์‚ฌ์šฉ๋˜๋Š”์ง€ ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ 3๊ฐœ์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๋ฌธ์„œ 1 : I like NLP ๋ฌธ์„œ 2 : I like DL ๋ฌธ์„œ 3 : DL is AI ์ด๋ฅผ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ ํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด๋‚˜ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ๋Œ€ํ‘œ์ ์ž…๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์€ ์•„์ง ๋ฐฐ์šฐ์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๊ฐ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด One-hot vector I [1 0 0 0 0 0] like [0 1 0 0 0 0] NLP [0 0 1 0 0 0] DL [0 0 0 1 0 0] is [0 0 0 0 1 0] AI [0 0 0 0 0 1] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋‹จ์–ด๋“ค์„ ๋ชจ๋‘ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ๋ฐ”๊ฟ”์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ž…๋ ฅ์œผ๋กœ ํ•œ๊บผ๋ฒˆ์— ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์ˆ˜ ๋ฌถ์–ด ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋”ฅ ๋Ÿฌ๋‹์—์„œ๋Š” ๋ฐฐ์น˜(Batch)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. [[[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0]], [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0]], [[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]]] ์ด๋Š” (3, 3, 6)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 3D ํ…์„œ์ž…๋‹ˆ๋‹ค. 5) ๊ทธ ์ด์ƒ์˜ ํ…์„œ 3์ฐจ์› ํ…์„œ๋ฅผ ๋ฐฐ์—ด๋กœ ํ•ฉ์น˜๋ฉด 4์ฐจ์› ํ…์„œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 4์ฐจ์› ํ…์„œ๋ฅผ ๋ฐฐ์—ด๋กœ ํ•ฉ์น˜๋ฉด 5์ฐจ์› ํ…์„œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ…์„œ๋Š” ๋‹ค์ฐจ์› ๋ฐฐ์—ด๋กœ์„œ ๊ณ„์†ํ•ด์„œ ํ™•์žฅ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๊ฐ ํ…์„œ๋ฅผ ๋„ํ˜•์œผ๋กœ ์‹œ๊ฐํ™”ํ•œ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 6) ์ผ€๋ผ์Šค์—์„œ์˜ ํ…์„œ ์•ž์„œ Numpy๋กœ ๊ฐ ํ…์„œ์˜ ndim(์ฐจ์›)๊ณผ shape(ํฌ๊ธฐ)๋ฅผ ์ถœ๋ ฅํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” 3์ฐจ์› ํ…์„œ์˜ ํฌ๊ธฐ(shape)๋Š” (2, 3, 5)์˜€์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์—์„œ๋Š” ์‹ ๊ฒฝ๋ง์˜ ์ธต์— ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape)๋ฅผ ์ธ์ž๋กœ ์ค„ ๋•Œ input_shape๋ผ๋Š” ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ์˜ˆ์‹œ๋Š” ๋’ค ์ฑ•ํ„ฐ๋“ค์—์„œ ๋ณด๊ฒ ์ง€๋งŒ input_shape๋Š” ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์ œ์™ธํ•˜๊ณ  ์ฐจ์›์„ ์ง€์ •ํ•˜๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด input_shape(6, 5)๋ผ๋Š” ์ธ์ž ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ด ํ…์„œ์˜ ํฌ๊ธฐ๋Š” (?, 6, 5)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” ์ง€์ •ํ•ด ์ฃผ๊ธฐ ์ „๊นŒ์ง€๋Š” ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—? ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฐฐ์น˜ ํฌ๊ธฐ๊นŒ์ง€ ์ง€์ •ํ•ด ์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด batch_input_shape=(8, 2, 10)์™€ ๊ฐ™์ด ์ธ์ž๋ฅผ ์ฃผ๋ฉด ์ด ํ…์„œ์˜ ํฌ๊ธฐ๋Š” (8, 2, 10)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋„ ์ž…๋ ฅ์˜ ์†์„ฑ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” input_dim, ์‹œํ€€์Šค ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋ฅผ ์˜๋ฏธํ•˜๋Š” input_length ๋“ฑ์˜ ์ธ์ž๋„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, input_shape์˜ ๋‘ ๊ฐœ์˜ ์ธ์ž๋Š” (input_length, input_dim)์ž…๋‹ˆ๋‹ค. 3. ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ์—ฐ์‚ฐ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ๊ธฐ๋ณธ์ ์ธ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import numpy as np 1) ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ ๊ฐ™์€ ํฌ๊ธฐ์˜ ๋‘ ๊ฐœ์˜ ๋ฒกํ„ฐ๋‚˜ ํ–‰๋ ฌ์€ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ฐ™์€ ์œ„์น˜์˜ ์›์†Œ๋ผ๋ฆฌ ์—ฐ์‚ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์—ฐ์‚ฐ์„ ์š”์†Œ๋ณ„(element-wise) ์—ฐ์‚ฐ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด A์™€ B๋ผ๋Š” ๋‘ ๊ฐœ์˜ ๋ฒกํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. = [ 4 ] B [ 2 ] ์ด๋•Œ ๋‘ ๋ฒกํ„ฐ A์™€ B์˜ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. + = [ 4 ] [ 2 ] [ 6 ] โˆ’ = [ 4 ] [ 2 ] [ 2 ] Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A = np.array([8, 4, 5]) B = np.array([1, 2, 3]) print('๋„ ๋ฒกํ„ฐ์˜ ํ•ฉ :',A+B) print('๋„ ๋ฒกํ„ฐ์˜ ์ฐจ :',A-B) ๋‘ ํ–‰๋ ฌ์˜ ํ•ฉ : [9 6 8] ๋‘ ํ–‰๋ ฌ์˜ ์ฐจ : [7 2 2] ํ–‰๋ ฌ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. A์™€ B๋ผ๋Š” ๋‘ ๊ฐœ์˜ ํ–‰๋ ฌ์ด ์žˆ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, ๋‘ ํ–‰๋ ฌ A์™€ B์˜ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. = [ 10 20 30 40 50 60 70 80 ] B [ 6 7 8 2 3 4 ] + = [ 10 20 30 40 50 60 70 80 ] [ 6 7 8 2 3 4 ] [ 15 26 37 48 51 62 73 84 ] โˆ’ = [ 10 20 30 40 50 60 70 80 ] [ 6 7 8 2 3 4 ] [ 14 23 32 49 58 67 76 ] Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A = np.array([[10, 20, 30, 40], [50, 60, 70, 80]]) B = np.array([[5, 6, 7, 8],[1, 2, 3, 4]]) print('๋„ ํ–‰๋ ฌ์˜ ํ•ฉ :') print(A + B) print('๋„ ํ–‰๋ ฌ์˜ ์ฐจ :') print(A - B) ๋‘ ํ–‰๋ ฌ์˜ ํ•ฉ : [[15 26 37 48] [51 62 73 84]] ๋‘ ํ–‰๋ ฌ์˜ ์ฐจ : [[ 5 14 23 32] [49 58 67 76]] 2) ๋ฒกํ„ฐ์˜ ๋‚ด์ ๊ณผ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ ๋ฒกํ„ฐ์˜ ์ ๊ณฑ(dot product) ๋˜๋Š” ๋‚ด์ (inner product)์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค. ๋ฒกํ„ฐ์˜ ๋‚ด์ ์€ ์—ฐ์‚ฐ์„ ์ (dot)์œผ๋กœ ํ‘œํ˜„ํ•˜์—ฌ โ‹… ์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋‚ด์ ์ด ์„ฑ๋ฆฝํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๊ฐ™์•„์•ผ ํ•˜๋ฉฐ, ๋‘ ๋ฒกํ„ฐ ์ค‘ ์•ž์˜ ๋ฒกํ„ฐ๊ฐ€ ํ–‰๋ฒกํ„ฐ(๊ฐ€๋กœ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ)์ด๊ณ  ๋’ค์˜ ๋ฒกํ„ฐ๊ฐ€ ์—ด๋ฒกํ„ฐ(์„ธ๋กœ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ) ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๊ฐ™๊ณ  ๊ณฑ์…ˆ์˜ ๋Œ€์ƒ์ด ๊ฐ๊ฐ ํ–‰๋ฒกํ„ฐ์ด๊ณ  ์—ด๋ฒกํ„ฐ์ผ ๋•Œ ๋‚ด์ ์ด ์ด๋ฃจ์–ด์ง€๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฒกํ„ฐ์˜ ๋‚ด์ ์˜ ๊ฒฐ๊ณผ๋Š” ์Šค์นผ๋ผ๊ฐ€ ๋œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์Šค์นผ๋ผ โ‹… = [ 2 3 ] [ 5 ] 1 4 2 5 3 6 32 (์Šค์นผ๋ผ) Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A = np.array([1, 2, 3]) B = np.array([4, 5, 6]) print('๋„ ๋ฒกํ„ฐ์˜ ๋‚ด์  :',np.dot(A, B)) ๋‘ ๋ฒกํ„ฐ์˜ ๋‚ด์  : 32 ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฒกํ„ฐ์˜ ๋‚ด์ ์„ ์ดํ•ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์€ ์™ผ์ชฝ ํ–‰๋ ฌ์˜ ํ–‰๋ฒกํ„ฐ(๊ฐ€๋กœ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ)์™€ ์˜ค๋ฅธ์ชฝ ํ–‰๋ ฌ์˜ ์—ด๋ฒกํ„ฐ(์„ธ๋กœ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ)์˜ ๋‚ด์ (๋Œ€์‘ํ•˜๋Š” ์›์†Œ๋“ค์˜ ๊ณฑ์˜ ํ•ฉ)์ด ๊ฒฐ๊ณผ ํ–‰๋ ฌ์˜ ์›์†Œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด A์™€ B๋ผ๋Š” ๋‘ ๊ฐœ์˜ ํ–‰๋ ฌ์ด ์žˆ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, ๋‘ ํ–‰๋ ฌ A์™€ B์˜ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. = [ 3 4 ] B [ 7 8 ] B [ 3 4 ] [ 7 8 ] [ ร— + ร— 1 7 3 8 ร— + ร— 2 7 4 8 ] [ 23 31 34 46 ] Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A = np.array([[1, 3],[2, 4]]) B = np.array([[5, 7],[6, 8]]) print('๋„ ํ–‰๋ ฌ์˜ ํ–‰๋ ฌ๊ณฑ :') print(np.matmul(A, B)) ๋‘ ํ–‰๋ ฌ์˜ ํ–‰๋ ฌ๊ณฑ : [[23 31] [34 46]] ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์€ ๋”ฅ ๋Ÿฌ๋‹์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ํ•„์ˆ˜์ ์ธ ๊ฐœ๋…์ด๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ์ˆ™์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ–‰๋ ฌ ๊ณฑ์…ˆ์—์„œ์˜ ์ฃผ์š”ํ•œ ๋‘ ๊ฐ€์ง€ ์กฐ๊ฑด ๋˜ํ•œ ๋ฐ˜๋“œ์‹œ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ๋‘ ํ–‰๋ ฌ์˜ ๊ณฑ A ร— B์ด ์„ฑ๋ฆฝ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ–‰๋ ฌ A์˜ ์—ด์˜ ๊ฐœ์ˆ˜์™€ ํ–‰๋ ฌ B์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๋Š” ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ๋‘ ํ–‰๋ ฌ์˜ ๊ณฑ A ร— B์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ํ–‰๋ ฌ AB์˜ ํฌ๊ธฐ๋Š” A์˜ ํ–‰์˜ ๊ฐœ์ˆ˜์™€ B์˜ ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง„๋‹ค. ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ๊ณฑ ๋˜๋Š” ํ–‰๋ ฌ๊ณผ ๋ฒกํ„ฐ์˜ ๊ณฑ ๋˜ํ•œ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ๊ณผ ๋™์ผํ•œ ์›๋ฆฌ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. 4. ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ๋…๋ฆฝ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ผ ๋•Œ, 1๊ฐœ์˜ ์ข…์† ๋ณ€์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ–‰๋ ฌ์˜ ์—ฐ์‚ฐ์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€๋‚˜ ๋‹ค์ค‘ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๊ฐ€ ์ด๋Ÿฌํ•œ ์—ฐ์‚ฐ์˜ ์˜ˆ์ธ๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ํ†ตํ•ด ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋…๋ฆฝ ๋ณ€์ˆ˜ ๊ฐ€ n ๊ฐœ์ธ ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ ์ˆ˜์‹์ž…๋‹ˆ๋‹ค. = 1 1 w x + 3 3. . w x + ์ด๋Š” ์ž…๋ ฅ ๋ฒกํ„ฐ [ 1. . n ] ์™€ ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ [ 1. . w ] ์˜ ๋‚ด์ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. = [ 1 x x โ‹… โ‹… x ] [ 1 2 3 โ‹… w ] b x w + 2 2 x w + ๋˜๋Š” ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ [ 1. . w ] ์™€ ์ž…๋ ฅ ๋ฒกํ„ฐ [ 1. . n ] ์˜ ๋‚ด์ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. = [ 1 w w โ‹… โ‹… w ] [ 1 2 3 โ‹… x ] b x w + 2 2 x w ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ์—๋Š” ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์œผ๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ง‘์˜ ํฌ๊ธฐ, ๋ฐฉ์˜ ์ˆ˜, ์ธต์˜ ์ˆ˜, ์ง‘์ด ์–ผ๋งˆ๋‚˜ ์˜ค๋ž˜๋˜์—ˆ๋Š”์ง€์™€ ์ง‘์˜ ๊ฐ€๊ฒฉ์ด ๊ธฐ๋ก๋œ ๋ถ€๋™์‚ฐ ๋ฐ์ดํ„ฐ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ง‘์˜ ์ •๋ณด๊ฐ€ ๋“ค์–ด์™”์„ ๋•Œ, ์ง‘์˜ ๊ฐ€๊ฒฉ์„ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•œ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. size( e t)( 1 ) number of bedrooms( 2 ) number of floors( 3 ) age of home( 4 ) price($1000)(y) 1800 2 1 10 207 1200 4 2 20 176 1700 3 2 15 213 1500 5 1 10 234 1100 2 2 10 155 ์œ„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ž…๋ ฅ ํ–‰๋ ฌ ์™€ ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ์˜ ๊ณฑ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. [ 11 x 12 x 13 x 14 21 x 22 x 23 x 24 31 x 32 x 33 x 34 41 x 42 x 43 x 44 51 x 52 x 53 x 54 ] [ 1 2 3 4 ] [ 11 1 x 12 2 x 13 3 x 14 4 21 1 x 22 2 x 23 3 x 24 4 31 1 x 32 2 x 33 3 x 34 4 41 1 x 42 2 x 43 3 x 44 4 51 1 x 52 2 x 53 3 x 54 4 ์—ฌ๊ธฐ์— ํŽธํ–ฅ ๋ฒกํ„ฐ๋ฅผ ๋” ํ•ด์ฃผ๋ฉด ์œ„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ „์ฒด ๊ฐ€์„ค ์ˆ˜์‹ ( ) ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [ 11 1 x 12 2 x 13 3 x 14 4 21 1 x 22 2 x 23 3 x 24 4 31 1 x 32 2 x 33 3 x 34 4 41 1 x 42 2 x 43 3 x 44 4 51 1 x 52 2 x 53 3 x 54 4 ] [ b b ] [ 1 2 3 4 5 ] ( ) X + ์œ„์˜ ์ˆ˜์‹์—์„œ ์ž…๋ ฅ ํ–‰๋ ฌ ๋Š” 5ํ–‰ 4์—ด์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๋ฒกํ„ฐ๋ฅผ ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ๋Š” 5ํ–‰ 1์—ด์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ณฑ์…ˆ์ด ์„ฑ๋ฆฝํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋Š” 4ํ–‰ 1์—ด์„ ๊ฐ€์ ธ์•ผ ํ•จ์„ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ๋ฅผ ์•ž์— ๋‘๊ณ  ์ž…๋ ฅ ํ–‰๋ ฌ์„ ๋’ค์— ๋‘๊ณ  ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ํ•œ๋‹ค๋ฉด ์ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. [ 1 w w w ] [ 11 x 21 x 31 x 41 x 51 12 x 22 x 32 x 42 x 52 13 x 23 x 33 x 43 x 53 14 x 24 x 34 x 44 x 54 ] [ b b b b ] [ 1 ์ˆ˜ํ•™์  ๊ด€๋ก€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ์ฃผ๋กœ ๊ฐ€์ค‘์น˜ ๊ฐ€ ์ž…๋ ฅ์˜ ์•ž์— ์˜ค๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. ( ) W + ์ธ๊ณต ์‹ ๊ฒฝ๋ง๋„ ๋ณธ์งˆ์ ์œผ๋กœ ์œ„์™€ ๊ฐ™์€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. 5. ์ƒ˜ํ”Œ(Sample)๊ณผ ํŠน์„ฑ(Feature) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ž…๋ ฅ ํ–‰๋ ฌ์„๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ์ƒ˜ํ”Œ(Sample)๊ณผ ํŠน์„ฑ(Feature)์˜ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์…€ ์ˆ˜ ์žˆ๋Š” ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•  ๋•Œ, ๊ฐ๊ฐ์„ ์ƒ˜ํ”Œ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ์ข…์† ๋ณ€์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ๊ฐ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜๋ฅผ ํŠน์„ฑ์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 6. ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ ๊ฒฐ์ • ์•ž์„œ ์–ธ๊ธ‰ํ•˜์˜€๋˜ ํ–‰๋ ฌ ๊ณฑ์…ˆ์˜ ๋‘ ๊ฐ€์ง€ ์ฃผ์š”ํ•œ ์กฐ๊ฑด์„ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ๋‘ ํ–‰๋ ฌ์˜ ๊ณฑ J ร— K์ด ์„ฑ๋ฆฝ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ–‰๋ ฌ J์˜ ์—ด์˜ ๊ฐœ์ˆ˜์™€ ํ–‰๋ ฌ K์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๋Š” ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ๋‘ ํ–‰๋ ฌ์˜ ๊ณฑ J ร— K์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ํ–‰๋ ฌ JK์˜ ํฌ๊ธฐ๋Š” J์˜ ํ–‰์˜ ๊ฐœ์ˆ˜์™€ K์˜ ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง„๋‹ค. ์ด๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W์™€ ํŽธํ–ฅ ํ–‰๋ ฌ B์˜ ํฌ๊ธฐ๋ฅผ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋…๋ฆฝ ๋ณ€์ˆ˜ ํ–‰๋ ฌ์„ X, ์ข…์† ๋ณ€์ˆ˜ ํ–‰๋ ฌ์„ Y๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ด๋•Œ ํ–‰๋ ฌ X๋ฅผ ์ž…๋ ฅ ํ–‰๋ ฌ(Input Matrix), Y๋ฅผ ์ถœ๋ ฅ ํ–‰๋ ฌ(Output Matrix)์ด๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด์ œ ์ž…๋ ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์™€ ์ถœ๋ ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋กœ๋ถ€ํ„ฐ W ํ–‰๋ ฌ๊ณผ B ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ์ถ”๋ก ํ•ด ๋ด…์‹œ๋‹ค. ํ–‰๋ ฌ์˜ ๋ง์…ˆ์— ํ•ด๋‹น๋˜๋Š” B ํ–‰๋ ฌ์€ Y ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ B ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” Y ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์ด ์„ฑ๋ฆฝ๋˜๋ ค๋ฉด ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์—์„œ ์•ž์— ์žˆ๋Š” ํ–‰๋ ฌ์˜ ์—ด์˜ ํฌ๊ธฐ์™€ ๋’ค์— ์žˆ๋Š” ํ–‰๋ ฌ์˜ ํ–‰์˜ ํฌ๊ธฐ๋Š” ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ž…๋ ฅ ํ–‰๋ ฌ X๋กœ๋ถ€ํ„ฐ W ํ–‰๋ ฌ์˜ ํ–‰์˜ ํฌ๊ธฐ๊ฐ€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ๋‘ ํ–‰๋ ฌ์˜ ๊ณฑ์˜ ๊ฒฐ๊ณผ๋กœ์„œ ๋‚˜์˜จ ํ–‰๋ ฌ์˜ ์—ด์˜ ํฌ๊ธฐ๋Š” ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์—์„œ ๋’ค์— ์žˆ๋Š” ํ–‰๋ ฌ์˜ ์—ด์˜ ํฌ๊ธฐ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ถœ๋ ฅ ํ–‰๋ ฌ Y๋กœ๋ถ€ํ„ฐ W ํ–‰๋ ฌ์˜ ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ํ–‰๋ ฌ๊ณผ ์ถœ๋ ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ ํŽธํ–ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜์˜€์„ ๋•Œ ํ•ด๋‹น ๋ชจ๋ธ์— ์กด์žฌํ•˜๋Š” ์ด ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์ด ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋Š” ํ•ด๋‹น ๋ชจ๋ธ์— ์กด์žฌํ•˜๋Š” ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ ํŽธํ–ฅ ํ–‰๋ ฌ์˜ ๋ชจ๋“  ์›์†Œ์˜ ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 06-09 ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€(Softmax Regression) ์•ž์„œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ํ†ตํ•ด 2๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ 1๊ฐœ๋ฅผ ๊ณ ๋ฅด๋Š” ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification)๋ฅผ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” 3๊ฐœ ์ด์ƒ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ 1๊ฐœ๋ฅผ ๊ณ ๋ฅด๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์œ„ํ•œ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€(Softmax Regression)์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. 1. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-class Classification) ์•ž์„œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ์‚ฌ์šฉํ•œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•˜์—ฌ ํ•ด๋‹น ๊ฐ’์ด ๋‘˜ ์ค‘ ํ•˜๋‚˜์— ์†ํ•  ํ™•๋ฅ ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 0์ด ์ •์ƒ ๋ฉ”์ผ, 1์ด ์ŠคํŒธ ๋ฉ”์ผ์ด๋ผ๊ณ  ์ •์˜ํ•ด๋†“๋Š”๋‹ค๋ฉด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ 0๊ณผ 1์‚ฌ์ด์˜ ์ถœ๋ ฅ๊ฐ’์„ ์ŠคํŒธ ๋ฉ”์ผ์ผ ํ™•๋ฅ ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ™•๋ฅ  ๊ฐ’์ด 0.5๋ฅผ ๋„˜์œผ๋ฉด 1์— ๊ฐ€๊นŒ์šฐ๋‹ˆ ์ŠคํŒธ ๋ฉ”์ผ๋กœ ํŒ๋‹จํ•˜๋ฉด ๋˜๊ณ , ๊ทธ ๋ฐ˜๋Œ€๋ผ๋ฉด ์ •์ƒ ๋ฉ”์ผ๋กœ ํŒ๋‹จํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜๊ฐ€ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ์˜€๋‹ค๋ฉด, ์„ธ ๊ฐœ ์ด์ƒ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ๋ฅผ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋ถ“๊ฝƒ ํ’ˆ์ข… ์˜ˆ์ธก ๋ฐ์ดํ„ฐ๋Š” ๊ฝƒ๋ฐ›์นจ ๊ธธ์ด, ๊ฝƒ๋ฐ›์นจ ๋„“์ด, ๊ฝƒ์žŽ ๊ธธ์ด, ๊ฝƒ์žŽ ๋„“์ด๋กœ๋ถ€ํ„ฐ setosa, versicolor, virginica๋ผ๋Š” 3๊ฐœ์˜ ํ’ˆ์ข… ์ค‘ ์–ด๋–ค ํ’ˆ์ข…์ธ์ง€๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋กœ ์ „ํ˜•์ ์ธ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. SepalLengthCm( 1 ) SepalWidthCm( 2 ) PetalLengthCm( 3 ) PetalWidthCm( 4 ) Species(y) 5.1 3.5 1.4 0.2 setosa 4.9 3.0 1.4 0.2 setosa 5.8 2.6 4.0 1.2 versicolor 6.7 3.0 5.2 2.3 virginica 5.6 2.8 4.9 2.0 virginica ์—ฌ๊ธฐ์— ์•ž์„œ ๋ฐฐ์šด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณธ๋‹ค๋ฉด ์–ด๋–จ๊นŒ์š”? ์–ด์ฉŒ๋ฉด ์ž…๋ ฅ๋œ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๊ฐ ์ •๋‹ต์ง€์— ๋Œ€ํ•ด์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ทธ๋ ‡๊ฒŒ ํ•œ๋‹ค๋ฉด, setosa๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์€ 0.8, versicolor๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์€ 0.2, virginica๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์€ 0.4 ๋“ฑ๊ณผ ๊ฐ™์€ ์ถœ๋ ฅ์„ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ์ „์ฒด ํ™•๋ฅ ์˜ ํ•ฉ๊ณ„๊ฐ€ 1์ด ๋˜๋„๋ก ํ•˜์—ฌ ์ „์ฒด ์„ ํƒ์ง€์— ๊ฑธ์นœ ํ™•๋ฅ ๋กœ ๋ฐ”๊ฟ€ ์ˆœ ์—†์„๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋ฉด ๋ชจ๋ธ์ด setosa ์ผ ํ™•๋ฅ ์ด 0.7, versicolor ์ผ ํ™•๋ฅ  0.05, virginica ์ผ ํ™•๋ฅ ์ด 0.25๊ณผ ๊ฐ™์ด ์„ธ ๊ฐœ์˜ ํ™•๋ฅ ์˜ ์ดํ•ฉ์ด 1์ธ ์˜ˆ์ธก๊ฐ’์„ ์–ป๋„๋ก ํ•˜์ž๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฒฝ์šฐ ํ™•๋ฅ  ๊ฐ’์ด ๊ฐ€์žฅ ๋†’์€ setosa๋กœ ์˜ˆ์ธกํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. 2. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜(Softmax function) ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ์„ ํƒ์ง€์˜ ์ด๊ฐœ์ˆ˜๋ฅผ k๋ผ๊ณ  ํ•  ๋•Œ, k ์ฐจ์›์˜ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ํ™•๋ฅ ์„ ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ˆ˜์‹์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ณ , ๊ทธ ํ›„์—๋Š” ๊ทธ๋ฆผ์œผ๋กœ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ดํ•ด k ์ฐจ์›์˜ ๋ฒกํ„ฐ์—์„œ i ๋ฒˆ์งธ ์›์†Œ๋ฅผ i , i ๋ฒˆ์งธ ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์„ i ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” i ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. i e i j 1 e j f r i 1 2. . ์œ„์—์„œ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ฐจ๊ทผ์ฐจ๊ทผ ์ ์šฉํ•ด ๋ด…์‹œ๋‹ค. ์œ„์—์„œ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ k=3์ด๋ฏ€๋กœ 3์ฐจ์› ๋ฒกํ„ฐ = [ 1 z z ] ์˜ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ถœ๋ ฅ์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ธก๊ฐ’ o t a ( ) [ z โˆ‘ = 3 z e 2 j 1 e j e 3 j 1 e j ] [ 1 p, 3 ] p, 2 p ๊ฐ๊ฐ์€ 1๋ฒˆ ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , 2๋ฒˆ ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , 3๋ฒˆ ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ ๊ฐ๊ฐ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ์ดํ•ฉ์€ 1์ด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” 3๊ฐœ์˜ ํด๋ž˜์Šค๋Š” virginica, setosa, versicolor์ด๋ฏ€๋กœ ์ด๋Š” ๊ฒฐ๊ตญ ์ฃผ์–ด์ง„ ์ž…๋ ฅ์ด virginica ์ผ ํ™•๋ฅ , setosa ์ผ ํ™•๋ฅ , versicolor ์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” i๊ฐ€ 1์ผ ๋•Œ๋Š” virginica ์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๊ณ , 2์ผ ๋•Œ๋Š” setosa ์ผ ํ™•๋ฅ , 3์ผ ๋•Œ๋Š” versicolor ์ผ ํ™•๋ฅ ์ด๋ผ๊ณ  ์ง€์ •ํ•˜์˜€๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ์ง€์ • ์ˆœ์„œ๋Š” ๋ฌธ์ œ๋ฅผ ํ’€๊ณ ์ž ํ•˜๋Š” ์‚ฌ๋žŒ์˜ ๋ฌด์ž‘์œ„ ์„ ํƒ์ž…๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ์‹์„ ๋ฌธ์ œ์— ๋งž๊ฒŒ ๋‹ค์‹œ ์“ฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. o t a ( ) [ z โˆ‘ = 3 z e 2 j 1 e j e 3 j 1 e j ] [ 1 p, 3 ] ๋‹ค์†Œ ๋ณต์žกํ•ด ๋ณด์ด์ง€๋งŒ ์–ด๋ ค์šด ๊ฐœ๋…์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ k ๊ฐœ์ผ ๋•Œ, k ์ฐจ์›์˜ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋ชจ๋“  ๋ฒกํ„ฐ ์›์†Œ์˜ ๊ฐ’์„ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์—ฌ ๋‹ค์‹œ k ์ฐจ์›์˜ ๋ฒกํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๋‚ด์šฉ์„ ์‹์œผ๋กœ ๊ธฐ์žฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฐฉ๊ธˆ ๋ฐฐ์šด ๊ฐœ๋…์„ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ๋‹ค์‹œ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2) ๊ทธ๋ฆผ์„ ํ†ตํ•œ ์ดํ•ด ์œ„์˜ ๊ทธ๋ฆผ์— ์ ์ฐจ ์‚ด์„ ๋ถ™์—ฌ๊ฐ€๋Š” ์‹์œผ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ 1๊ฐœ์”ฉ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ์ฆ‰, ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—๋Š” ๋‘ ๊ฐ€์ง€ ์˜๋ฌธ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์งˆ๋ฌธ์€ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์˜๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋Š” 4๊ฐœ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ ์ด๋Š” ๋ชจ๋ธ์ด 4์ฐจ์› ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์†Œํ”„ํŠธ๋งฅ์Šค์˜ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฒกํ„ฐ๋Š” ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ์–ด๋–ค ๊ฐ€์ค‘์น˜ ์—ฐ์‚ฐ์„ ํ†ตํ•ด 3์ฐจ์› ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” 3์ฐจ์› ๋ฒกํ„ฐ๋ฅผ ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ๋ฒกํ„ฐ๋ฅผ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ๋กœ ์ฐจ์›์„ ์ถ•์†Œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์› ์ˆ˜๋งŒํผ ๊ฒฐ๊ด๊ฐ’์ด ๋‚˜์˜ค๋„๋ก ๊ฐ€์ค‘์น˜ ๊ณฑ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ํ™”์‚ดํ‘œ๋Š” ์ด (4 ร— 3 = 12) 12๊ฐœ์ด๋ฉฐ ์ „๋ถ€ ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง€๊ณ , ํ•™์Šต ๊ณผ์ •์—์„œ ์ ์ฐจ์ ์œผ๋กœ ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฐ€์ค‘์น˜๋กœ ๊ฐ’์ด ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์งˆ๋ฌธ์€ ์˜ค์ฐจ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์˜๋ฌธ์ž…๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ์€ ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๋กœ ๊ฐ ์›์†Œ๋Š” 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด ๊ฐ๊ฐ์€ ํŠน์ • ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ์›์†Œ์ธ 1 ์€ virginica๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , ๋‘ ๋ฒˆ์งธ ์›์†Œ์ธ 2 ๋Š” setosa๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , ์„ธ ๋ฒˆ์งธ ์›์†Œ์ธ 3 ์€ versicolor๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ๋กœ ๊ณ ๋ คํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด ์˜ˆ์ธก๊ฐ’๊ณผ ๋น„๊ต๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์ œ ๊ฐ’์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ๋Š” ์‹ค์ œ ๊ฐ’์„ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ 1 ๊ฐ€ virginica๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , ๋‘ ๋ฒˆ์งธ ์›์†Œ 2 ๊ฐ€ setosa๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , ์„ธ ๋ฒˆ์งธ ์›์†Œ 3 ๊ฐ€ versicolor๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, ๊ฐ ์‹ค์ œ ๊ฐ’์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์€ 1, 2, 3์ด ๋˜๊ณ  ์ด์— ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์‹ค์ œ ๊ฐ’์„ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ์ˆ˜์น˜ํ™”ํ•œ ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ˜„์žฌ ํ’€๊ณ  ์žˆ๋Š” ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ์˜ ์‹ค์ œ ๊ฐ’์ด setosa๋ผ๋ฉด setosa์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” [0 1 0]์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์˜ค์ฐจ๊ฐ€ 0์ด ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๊ฐ€ [0 1 0]์ด ๋˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ด ๋‘ ๋ฒกํ„ฐ์˜ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋Š” ๋น„์šฉ ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด๋Š” ๋’ค์—์„œ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ๋‹ค์‹œ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ์„ ํ˜• ํšŒ๊ท€๋‚˜ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์˜ค์ฐจ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํžˆ๋Š” ์„ ํ˜• ํšŒ๊ท€๋‚˜ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํŽธํ–ฅ ๋˜ํ•œ ์—…๋ฐ์ดํŠธ์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์ž…๋ ฅ์„ ํŠน์„ฑ(feature)์˜ ์ˆ˜๋งŒํผ์˜ ์ฐจ์›์„ ๊ฐ€์ง„ ์ž…๋ ฅ ๋ฒกํ„ฐ๋ผ๊ณ  ํ•˜๊ณ , ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์„, ํŽธํ–ฅ์„๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ ์˜ˆ์ธก๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์„ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ 4๋Š” ํŠน์„ฑ์˜ ์ˆ˜์ด๋ฉฐ 3์€ ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 3. ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๋ฌด์ž‘์œ„์„ฑ ๊ผญ ์‹ค์ œ ๊ฐ’์„ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•ด์•ผ๋งŒ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„์˜ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๊ฐ€ ๊ฐ ํด๋ž˜์Šค ๊ฐ„์˜ ๊ด€๊ณ„๊ฐ€ ๊ท ๋“ฑํ•˜๋‹ค๋Š” ์ ์—์„œ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ์ด๋Ÿฌํ•œ ์ ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ ˆํ•œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋‹ค์ˆ˜์˜ ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฌธ์ œ์—์„œ๋Š” ์ด์ง„ ๋ถ„๋ฅ˜์ฒ˜๋Ÿผ 2๊ฐœ์˜ ์ˆซ์ž ๋ ˆ์ด๋ธ”์ด ์•„๋‹ˆ๋ผ ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ˆซ์ž ๋ ˆ์ด๋ธ”์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ง๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ ˆ์ด๋ธ”๋ง ๋ฐฉ๋ฒ•์€ ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ํด๋ž˜์Šค ์ „์ฒด์— ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ๋ ˆ์ด๋ธ”์ด {red, green, blue}์™€ ๊ฐ™์ด 3๊ฐœ๋ผ๋ฉด ๊ฐ๊ฐ 0, 1, 2๋กœ ๋ ˆ์ด๋ธ”์„ ํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ํด๋ž˜์Šค๊ฐ€ 4๊ฐœ๊ณ  ์ธ๋ฑ์Šค๋ฅผ ์ˆซ์ž 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ํ•˜๋ฉด {baby, child, adolescent, adult}๋ผ๋ฉด 1, 2, 3, 4๋กœ ๋ ˆ์ด๋ธ”์„ ํ•ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ผ๋ฐ˜์ ์ธ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ ๋ ˆ์ด๋ธ”๋ง ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์œ„์™€ ๊ฐ™์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์•„๋‹ˆ๋ผ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ณด๋‹ค ํด๋ž˜์Šค์˜ ์„ฑ์งˆ์„ ์ž˜ ํ‘œํ˜„ํ•˜์˜€๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋ฅผ ์•Œ์•„๋ด…์‹œ๋‹ค. Banana, Tomato, Apple๋ผ๋Š” 3๊ฐœ์˜ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๋ ˆ์ด๋ธ”์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ๊ฐ 1, 2, 3์„ ๋ถ€์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์†์‹ค ํ•จ์ˆ˜๋กœ ์„ ํ˜• ํšŒ๊ท€ ์‹ค์Šต์—์„œ ๋ฐฐ์šด ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ MSE๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์–ด๋–ค ์˜คํ•ด๋ฅผ ๋ถˆ๋Ÿฌ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์‹์€ ์•ž์„œ ์„ ํ˜• ํšŒ๊ท€์—์„œ ๋ฐฐ์› ๋˜ MSE๋ฅผ ๋‹ค์‹œ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜จ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ^ ๋Š” ์˜ˆ์ธก๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. o s f n t o = n i ( i y ^ ) ์ง๊ด€์ ์ธ ์˜ค์ฐจ ํฌ๊ธฐ ๋น„๊ต๋ฅผ ์œ„ํ•ด ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ์ˆ˜์‹์€ ์ œ์™ธํ•˜๊ณ  ์ œ๊ณฑ ์˜ค์ฐจ๋กœ๋งŒ ํŒ๋‹จํ•ด ๋ด…์‹œ๋‹ค. ์‹ค์ œ ๊ฐ’์ด Tomato ์ผ ๋•Œ ์˜ˆ์ธก๊ฐ’์ด Banana์ด์—ˆ๋‹ค๋ฉด ์ œ๊ณฑ ์˜ค์ฐจ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( โˆ’ ) = ์‹ค์ œ ๊ฐ’์ด Apple ์ผ ๋•Œ ์˜ˆ์ธก๊ฐ’์ด Banana์ด์—ˆ๋‹ค๋ฉด ์ œ๊ณฑ ์˜ค์ฐจ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( โˆ’ ) = ์ฆ‰, Banana๊ณผ Tomato ์‚ฌ์ด์˜ ์˜ค์ฐจ๋ณด๋‹ค Banana๊ณผ Apple์˜ ์˜ค์ฐจ๊ฐ€ ๋” ํฝ๋‹ˆ๋‹ค. ์ด๋Š” ๊ธฐ๊ณ„์—๊ฒŒ Banana๊ฐ€ Apple๋ณด๋‹ค๋Š” Tomato์— ๋” ๊ฐ€๊น๋‹ค๋Š” ์ •๋ณด๋ฅผ ์ฃผ๋Š” ๊ฒƒ๊ณผ ๋‹ค๋ฆ„์—†์Šต๋‹ˆ๋‹ค. ๋” ๋งŽ์€ ํด๋ž˜์Šค์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ–ˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. {Banana :1, Tomato :2, Apple :3, Strawberry :4, ... Water melon :10} ์ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์€ Banana๊ฐ€ Water melon๋ณด๋‹ค๋Š” Tomato์— ๋” ๊ฐ€๊น๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ถ€์—ฌํ•˜๊ณ ์ž ํ–ˆ๋˜ ์ •๋ณด๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์˜ ์ˆœ์„œ ์ •๋ณด๊ฐ€ ๋„์›€์ด ๋˜๋Š” ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋„ ๋ฌผ๋ก  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๊ฐ ํด๋ž˜์Šค๊ฐ€ ์ˆœ์„œ์˜ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์–ด์„œ ํšŒ๊ท€๋ฅผ ํ†ตํ•ด์„œ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด {baby, child, adolescent, adult}๋‚˜ {1์ธต, 2์ธต, 3์ธต, 4์ธต}์ด๋‚˜ {10๋Œ€, 20๋Œ€, 30๋Œ€, 40๋Œ€}์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ๋ฐ˜์ ์ธ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ๋Š” ๊ฐ ํด๋ž˜์Šค๋Š” ์ˆœ์„œ์˜ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ฐ ํด๋ž˜์Šค ๊ฐ„์˜ ์˜ค์ฐจ๋Š” ๊ท ๋“ฑํ•œ ๊ฒƒ์ด ์˜ณ์Šต๋‹ˆ๋‹ค. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ๋‹ฌ๋ฆฌ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์€ ๋ถ„๋ฅ˜ ๋ฌธ์ œ ๋ชจ๋“  ํด๋ž˜์Šค ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๊ท ๋“ฑํ•˜๊ฒŒ ๋ถ„๋ฐฐํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์„ธ ๊ฐœ์˜ ์นดํ…Œ๊ณ ๋ฆฌ์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ†ตํ•ด์„œ ๋ ˆ์ด๋ธ”์„ ์ธ์ฝ”๋”ฉํ–ˆ์„ ๋•Œ ๊ฐ ํด๋ž˜์Šค ๊ฐ„์˜ ์ œ๊ณฑ ์˜ค์ฐจ๊ฐ€ ๊ท ๋“ฑํ•จ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ( ( , , ) ( , , ) ) = ( โˆ’ ) + ( โˆ’ ) + ( โˆ’ ) = ( ( , , ) ( , , ) ) = ( โˆ’ ) + ( โˆ’ ) + ( โˆ’ ) = ๋‹ค๋ฅด๊ฒŒ ํ‘œํ˜„ํ•˜๋ฉด ๋ชจ๋“  ํด๋ž˜์Šค์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ†ตํ•ด ์–ป์€ ์›-ํ•ซ ๋ฒกํ„ฐ๋“ค์€ ๋ชจ๋“  ์Œ์— ๋Œ€ํ•ด์„œ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด๋„ ์ „๋ถ€ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ์ด์ฒ˜๋Ÿผ ๊ฐ ํด๋ž˜์Šค์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด ๋ฌด์ž‘์œ„์„ฑ์„ ๊ฐ€์ง„๋‹ค๋Š” ์ ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋‹ค์‹œ ์–ธ๊ธ‰๋˜๊ฒ ์ง€๋งŒ ์ด๋Ÿฌํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๊ด€๊ณ„์˜ ๋ฌด์ž‘์œ„์„ฑ์€ ๋•Œ๋กœ๋Š” ๋‹จ์–ด์˜ ์œ ์‚ฌ์„ฑ์„ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์œผ๋กœ ์–ธ๊ธ‰๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 4. ๋น„์šฉ ํ•จ์ˆ˜(Cost function) ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ๋Š” ๋น„์šฉ ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๋‹ค์–‘ํ•œ ํ‘œ๊ธฐ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜ ์•„๋ž˜์—์„œ๋Š” ์‹ค์ œ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ,๋Š” ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. j ๋Š” ์‹ค์ œ ๊ฐ’ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, j ๋Š” ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฒˆ์งธ ํด๋ž˜์Šค์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํ‘œ๊ธฐ์— ๋”ฐ๋ผ์„œ ^๋กœ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. o t โˆ’ j 1 y l g ( j ) ์ด ํ•จ์ˆ˜๊ฐ€ ์™œ ๋น„์šฉ ํ•จ์ˆ˜๋กœ ์ ํ•ฉํ•œ์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€ ์‹ค์ œ ๊ฐ’ ์›-ํ•ซ ๋ฒกํ„ฐ์—์„œ 1์„ ๊ฐ€์ง„ ์›์†Œ์˜ ์ธ๋ฑ์Šค๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, c 1 y ๊ฐ€๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์— ๋Œ€์ž…ํ•ด ๋ณด๋ฉด 1 o ( ) 0 ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฒฐ๊ณผ์ ์œผ๋กœ ^ y ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์˜ ๊ฐ’์€ 0์ด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, โˆ‘ = k j l g ( j ) ์ด ๊ฐ’์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ํ•™์Šตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐœ์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์ตœ์ข… ๋น„์šฉ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o t โˆ’ n i 1 โˆ‘ = k j ( ) l g ( j ( ) ) 2) ์ด์ง„ ๋ถ„๋ฅ˜์—์„œ์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ๋ฐฐ์šด ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์‹๊ณผ ๋‹ฌ๋ผ ๋ณด์ด์ง€๋งŒ, ๋ณธ์งˆ์ ์œผ๋กœ๋Š” ๋™์ผํ•œ ํ•จ์ˆ˜์‹์ž…๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์‹์œผ๋กœ๋ถ€ํ„ฐ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์‹์„ ๋„์ถœํ•ด ๋ด…์‹œ๋‹ค. o t โˆ’ ( l g ( ) ( โˆ’ ) l g ( โˆ’ ( ) ) ) ์œ„์˜ ์‹์€ ์•ž์„œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ๋ฐฐ์› ๋˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ์˜ ํ•จ์ˆ˜์‹์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์˜ ์‹์—์„œ ๋ฅผ 1 1 y y๋กœ ์น˜ํ™˜ํ•˜๊ณ  ( ) p, โˆ’ ( ) p๋กœ ์น˜ํ™˜ํ•ด ๋ด…์‹œ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์•„๋ž˜์˜ ์‹์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( 1 l g ( 1 ) y l g ( 2 ) ) ์ด ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( i 1 y l g p) ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ๋Š” k์˜ ๊ฐ’์ด ๊ณ ์ •๋œ ๊ฐ’์ด ์•„๋‹ˆ๋ฏ€๋กœ 2๋ฅผ k๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ( i 1 y l g p) ์œ„์˜ ์‹์€ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ์‹๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์—ญ์œผ๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์‹์„ ์–ป๋Š” ๊ฒƒ์€ k๋ฅผ 2๋กœ ํ•˜๊ณ , 1 y๋ฅผ ๊ฐ๊ฐ ์™€ โˆ’๋กœ ์น˜ํ™˜ํ•˜๊ณ , 1 p๋ฅผ ๊ฐ๊ฐ ( ) 1 H ( ) ๋กœ ์น˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ตœ์ข… ๋น„์šฉ ํ•จ์ˆ˜์—์„œ ๊ฐ€ 2๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ๊ฒฐ๊ตญ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ๋น„์šฉ ํ•จ์ˆ˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. o t โˆ’ n i 1 โˆ‘ = k j ( ) l g ( j ( ) ) โˆ’ n i 1 [ ( ) o ( ( ) ) ( โˆ’ 5. ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋‹ค์ด์–ด๊ทธ๋žจ n ๊ฐœ์˜ ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  m ๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ๋˜ํ•œ ํ•˜๋‚˜์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์œ„์˜ ๊ทธ๋ฆผ์€ ์•ž์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์„ค๋ช…ํ–ˆ๋˜ ์•„๋ž˜์˜ ๊ทธ๋ฆผ์—์„œ ํŠน์„ฑ์˜ ๊ฐœ์ˆ˜๋ฅผ ์œผ๋กœ ํ•˜๊ณ , ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๋ฅผ ์œผ๋กœ ์ผ๋ฐ˜ํ™”ํ•œ ๋’ค์— ๊ทธ๋ฆผ์„ ์ข€ ๋” ์š”์•ฝํ•ด์„œ ํ‘œํ˜„ํ•œ ๊ฒƒ์œผ๋กœ ๋ด๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค. 06-10 ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ์‹ค์Šต ์ด๋ฒˆ์— ์‹ค์Šตํ•  ๋ฐ์ดํ„ฐ๋Š” ์•ž์„œ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์˜ˆ์‹œ๋กœ ๋“ค์—ˆ๋˜ ๋ถ“๊ฝƒ ํ’ˆ์ข… ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ , ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํƒ์ƒ‰ ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://www.kaggle.com/saurabh00007/iriscsv 1. ์•„์ด๋ฆฌ์Šค ํ’ˆ์ข… ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import urllib.request from sklearn.model_selection import train_test_split from tensorflow.keras.utils import to_categorical iris.csv ํŒŒ์ผ์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•œ ํ›„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/06.%20Machine%20Learning/dataset/Iris.csv", filename="Iris.csv") data = pd.read_csv('Iris.csv', encoding='latin1') print('์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ :', len(data)) print(data[:5]) ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 150 Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species 0 1 5.1 3.5 1.4 0.2 Iris-setosa 1 2 4.9 3.0 1.4 0.2 Iris-setosa 2 3 4.7 3.2 1.3 0.2 Iris-setosa 3 4 4.6 3.1 1.5 0.2 Iris-setosa 4 5 5.0 3.6 1.4 0.2 Iris-setosa ๋ฐ์ดํ„ฐ๋Š” 6๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋œ ์ด 150๊ฐœ์˜ ์ƒ˜ํ”Œ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์—ด์ธ Id๋Š” ์‹ค์งˆ์ ์œผ๋กœ ์˜๋ฏธ๋Š” ์—†๋Š” ์—ด์ž…๋‹ˆ๋‹ค. ๊ทธ ํ›„ ํŠน์„ฑ(feature)์— ํ•ด๋‹นํ•˜๋Š” SepalLengthCm, SepalWidthCm, PetalLengthCm, PetalWidthCm 4๊ฐœ์˜ ์—ด์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์—ด Species๋Š” ํ•ด๋‹น ์ƒ˜ํ”Œ์ด ์–ด๋–ค ํ’ˆ์ข…์ธ์ง€๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ ์—ฌ๊ธฐ์„œ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. Species ์—ด์—์„œ ํ’ˆ์ข…์ด ๋ช‡ ๊ฐœ ์กด์žฌํ•˜๋Š”์ง€ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. # ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ , ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ๋ชจ๋“  ์ข…๋ฅ˜๋ฅผ ์ถœ๋ ฅ print("ํ’ˆ์ข… ์ข…๋ฅ˜:", data["Species"].unique(), sep="\n") ํ’ˆ์ข… ์ข…๋ฅ˜: ['Iris-setosa' 'Iris-versicolor' 'Iris-virginica'] Species๋Š” Iris-setosa, Iris-versicolor, Iris-virginica๋ผ๋Š” 3๊ฐœ์˜ ํ’ˆ์ข…์œผ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ด๋ฒˆ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ํ‘ธ๋Š” ๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ์˜ 4๊ฐœ์˜ ํŠน์„ฑ์œผ๋กœ๋ถ€ํ„ฐ 3๊ฐœ ์ค‘ ์–ด๋–ค ํ’ˆ์ข…์ธ์ง€๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. 3๊ฐœ์˜ ํ’ˆ์ข…์ด 4๊ฐœ์˜ ํŠน์„ฑ์— ๋Œ€ํ•ด์„œ ์–ด๋–ค ๋ถ„ํฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์‹œ๊ฐํ™”ํ•ด๋ด…์‹œ๋‹ค. seaborn์˜ pairplot์€ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ๊ฐ ์—ด์˜ ์กฐํ•ฉ์— ๋”ฐ๋ผ์„œ ์‚ฐ์ ๋„(scatter plot)๋ฅผ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค. sns.set(style="ticks", color_codes=True) g = sns.pairplot(data, hue="Species", palette="husl") ํ•ด๋‹น ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ์—๋Š” 4๊ฐœ์˜ ํŠน์„ฑ์— ํ•ด๋‹นํ•˜๋Š” SepalLengthCm, SepalWidthCm, PetalLengthCm, PetalWidthCm์— ๋Œ€ํ•ด์„œ ๋ชจ๋“  ์Œ(pair)์˜ ์กฐํ•ฉ์ธ 16๊ฐœ์˜ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ์‚ฐ์ ๋„๋ฅผ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋™์ผํ•œ ํŠน์„ฑ์˜ ์Œ์ผ ๊ฒฝ์šฐ์—๋Š” ํžˆ์Šคํ† ๊ทธ๋žจ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ ๊ฐ€๋ น, SepalLengthCm์™€ SepalLengthCm์˜ ์กฐํ•ฉ์ด ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. seaborn์˜ barplot์„ ํ†ตํ•ด ์ข…๊ณผ ํŠน์„ฑ์— ๋Œ€ํ•œ ์—ฐ๊ด€ ๊ด€๊ณ„๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ์ข…์— ๋”ฐ๋ฅธ SepalWidthCm์˜ ๊ฐ’์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. # ๊ฐ์ข…๊ณผ ํŠน์„ฑ์— ๋Œ€ํ•œ ์—ฐ๊ด€ ๊ด€๊ณ„ sns.barplot(x='Species', y='SepalWidthCm', data=data, ci=None) 150๊ฐœ์˜ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ Species ์—ด์—์„œ ๊ฐ ํ’ˆ์ข…์ด ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. data['Species'].value_counts().plot(kind='bar') ๋™์ผํ•˜๊ฒŒ 50๊ฐœ์”ฉ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ๋ถ„ํฌ๊ฐ€ ๊ท ์ผํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ๋ชจ๋ธ์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” Species ์—ด์— ๋Œ€ํ•ด์„œ ์ „๋ถ€ ์ˆ˜์น˜ํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ •์ƒ์ ์œผ๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ฐ’์˜ ๋ถ„ํฌ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. # Iris-virginica๋Š” 0, Iris-setosa๋Š” 1, Iris-versicolor๋Š” 2๊ฐ€ ๋จ. data['Species'] = data['Species'].replace(['Iris-virginica','Iris-setosa','Iris-versicolor'],[0,1,2]) data['Species'].value_counts().plot(kind='bar') ์—ฌ์ „ํžˆ ๋™์ผํ•œ ๋ถ„ํฌ๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. ํŠน์„ฑ๊ณผ ํ’ˆ์ข…์„ ๊ฐ๊ฐ ์ข…์† ๋ณ€์ˆ˜์™€ ๋…๋ฆฝ ๋ณ€์ˆ˜ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ์ •ํ™•ํ•˜๊ฒŒ ๋ถ„๋ฆฌ๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ ์ค‘ ์ƒ์œ„ 5๊ฐœ์”ฉ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # X ๋ฐ์ดํ„ฐ. ํŠน์„ฑ์€ ์ด 4๊ฐœ. data_X = data[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']].values # Y ๋ฐ์ดํ„ฐ. ์˜ˆ์ธก ๋Œ€์ƒ. data_y = data['Species'].values print(data_X[:5]) print(data_y[:5]) [[5.1 3.5 1.4 0.2] [4.9 3. 1.4 0.2] [4.7 3.2 1.3 0.2] [4.6 3.1 1.5 0.2] [5. 3.6 1.4 0.2]] [1 1 1 1 1] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ดํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ์ง„ํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”๊ณผ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”์„ ์ƒ์œ„ 5๊ฐœ์”ฉ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 8:2๋กœ ๋‚˜๋ˆˆ๋‹ค. (X_train, X_test, y_train, y_test) = train_test_split(data_X, data_y, train_size=0.8, random_state=1) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ y_train = to_categorical(y_train) y_test = to_categorical(y_test) print(y_train[:5]) print(y_test[:5]) [[0. 0. 1.] [1. 0. 0.] [0. 0. 1.] [1. 0. 0.] [1. 0. 0.]] [[0. 1. 0.] [0. 0. 1.] [0. 0. 1.] [0. 1. 0.] [1. 0. 0.]] ์ „์ฒ˜๋ฆฌ ๋‹จ๊ณ„๊ฐ€ ๋ชจ๋‘ ๋์ด ๋‚ฌ์Šต๋‹ˆ๋‹ค. 2. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ์ž…๋ ฅ์˜ ์ฐจ์›์ด 4์ด๋ฏ€๋กœ input_dim์˜ ์ธ์ž ๊ฐ’์ด 4๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์˜ ์ฐจ์›์ด 3์ด๋ฏ€๋กœ input_dim=4 ์•ž์˜ ์ธ์ž ๊ฐ’์ด 3์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ activation์˜ ์ธ์ž ๊ฐ’์œผ๋กœ 'softmax'๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ค์ฐจ ํ•จ์ˆ˜๋กœ๋Š” ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ๋Š” binary_crossentropy๋ฅผ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ๋Š” 'categorical_crossentropy๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ตํ‹ฐ๋งˆ์ด์ €๋กœ๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์˜ ์ผ์ข…์ธ ์•„๋‹ด(adam)์„ ์‚ฌ์šฉํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์•„๋‹ด์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๋”ฅ ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ›ˆ๋ จ ํšŸ์ˆ˜๋Š” 200์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ„๋„๋กœ ๋ถ„๋ฆฌํ•ด์„œ ํ‰๊ฐ€์— ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ, validation_data=()์— ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์žฌํ•ด ์ฃผ๋ฉด ์‹ค์ œ๋กœ๋Š” ํ›ˆ๋ จ์—๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๊ฐ ํ›ˆ๋ จ ํšŸ์ˆ˜๋งˆ๋‹ค ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ •ํ™•๋„๊ฐ€ ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ›ˆ๋ จ 1ํšŒ(1 ์—ํฌํฌ)๋งˆ๋‹ค ์ธก์ •๋˜๊ณ ๋Š” ์žˆ์ง€๋งŒ ๊ธฐ๊ณ„๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential() model.add(Dense(3, input_dim=4, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) history = model.fit(X_train, y_train, epochs=200, batch_size=1, validation_data=(X_test, y_test)) ์ถœ๋ ฅ์—์„œ accuracy์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์ด๊ณ , val_accuracy์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ฐ ์—ํฌํฌ๋‹น ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ–ˆ์œผ๋ฏ€๋กœ ํ•œ ๋ฒˆ ์—ํฌํฌ์— ๋”ฐ๋ฅธ ์ •ํ™•๋„๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. epochs = range(1, len(history.history['accuracy']) + 1) plt.plot(epochs, history.history['loss']) plt.plot(epochs, history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'val'], loc='upper left') plt.show() ์—ํฌํฌ๊ฐ€ ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์˜ค์ฐจ(loss)๊ฐ€ ์ ์ฐจ์ ์œผ๋กœ ์ค„์–ด๋“œ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์—์„œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•˜๋Š” ์šฉ๋„๋กœ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š” evaluate()๋ฅผ ํ†ตํ•ด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ๋‹ค์‹œ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (model.evaluate(X_test, y_test)[1])) ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.9667 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ 96.67%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. 07. ๋”ฅ ๋Ÿฌ๋‹(Deep Learning) ๊ฐœ์š” ๋”ฅ ๋Ÿฌ๋‹(Deep Learning)์€ ๋จธ์‹  ๋Ÿฌ๋‹(Machine Learning)์˜ ํŠน์ •ํ•œ ํ•œ ๋ถ„์•ผ๋กœ์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง(Artificial Neural Network)์˜ ์ธต์„ ์—ฐ์†์ ์œผ๋กœ ๊นŠ๊ฒŒ ์Œ“์•„ ์˜ฌ๋ ค ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐฉ์‹์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋”ฅ ๋Ÿฌ๋‹์ด ํ™”๋‘๊ฐ€ ๋˜๊ธฐ ์‹œ์ž‘ํ•œ ๊ฒƒ์€ 2010๋…„๋Œ€์˜ ๋น„๊ต์  ์ตœ๊ทผ์˜ ์ผ์ด์ง€๋งŒ, ๋”ฅ ๋Ÿฌ๋‹์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ์ธ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์—ญ์‚ฌ๋Š” ์ƒ๊ฐ๋ณด๋‹ค ์˜ค๋ž˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋”ฅ ๋Ÿฌ๋‹์„ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด 1957๋…„์˜ ์ดˆ๊ธฐ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ธ ํผ์…‰ํŠธ๋ก ์—์„œ๋ถ€ํ„ฐ ์„ค๋ช…์„ ์‹œ์ž‘ํ•˜์—ฌ ์ธต์„ ๊นŠ๊ฒŒ ์Œ“์•„ ํ•™์Šตํ•˜๋Š” ๋”ฅ ๋Ÿฌ๋‹๊นŒ์ง€ ๊ฐœ๋…์„ ์ ์ฐจ์ ์œผ๋กœ ํ™•์žฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง๊ณผ ๊ฐ™์€ ๊ธฐ๋ณธ์ ์ธ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์šฉ์–ด๋“ค๊ณผ ์ผ€๋ผ์Šค์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 07-01 ํผ์…‰ํŠธ๋ก (Perceptron) ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ์ˆ˜๋งŽ์€ ๋จธ์‹  ๋Ÿฌ๋‹ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ตœ๊ทผ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๋ณต์žกํ•˜๊ฒŒ ์Œ“์•„ ์˜ฌ๋ฆฐ ๋”ฅ ๋Ÿฌ๋‹์ด ๋‹ค๋ฅธ ๋จธ์‹  ๋Ÿฌ๋‹ ๋ฐฉ๋ฒ•๋“ค์„ ๋›ฐ์–ด๋„˜๋Š” ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๋Š” ์‚ฌ๋ก€๊ฐ€ ๋Š˜๋ฉด์„œ, ์ „ํ†ต์ ์ธ ๋จธ์‹  ๋Ÿฌ๋‹๊ณผ ๋”ฅ ๋Ÿฌ๋‹์„ ๊ตฌ๋ถ„ํ•ด์„œ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋ชฉ์†Œ๋ฆฌ๊ฐ€ ์ปค์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฅ ๋Ÿฌ๋‹์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•œ๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ์ดˆ๊ธฐ์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ธ ํผ์…‰ํŠธ๋ก (Perceptron)์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 1. ํผ์…‰ํŠธ๋ก (Perceptron) ํผ์…‰ํŠธ๋ก (Perceptron)์€ ํ”„๋ž‘ํฌ ๋กœ์  ๋ธ”๋ผํŠธ(Frank Rosenblatt)๊ฐ€ 1957๋…„์— ์ œ์•ˆํ•œ ์ดˆ๊ธฐ ํ˜•ํƒœ์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์œผ๋กœ ๋‹ค์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์€ ์‹ค์ œ ๋‡Œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์‹ ๊ฒฝ ์„ธํฌ ๋‰ด๋Ÿฐ์˜ ๋™์ž‘๊ณผ ์œ ์‚ฌํ•œ๋ฐ, ์‹ ๊ฒฝ ์„ธํฌ ๋‰ด๋Ÿฐ์˜ ๊ทธ๋ฆผ์„ ๋จผ์ € ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‰ด๋Ÿฐ์€ ๊ฐ€์ง€๋Œ๊ธฐ์—์„œ ์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„๋“ค์ด๊ณ , ์ด ์‹ ํ˜ธ๊ฐ€ ์ผ์ •์น˜ ์ด์ƒ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋ฉด ์ถ•์‚ญ๋Œ๊ธฐ๋ฅผ ํ†ตํ•ด์„œ ์‹ ํ˜ธ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ˆ˜์˜ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ํผ์…‰ํŠธ๋ก ์˜ ๊ทธ๋ฆผ์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ ๊ฒฝ ์„ธํฌ ๋‰ด๋Ÿฐ์˜ ์ž…๋ ฅ ์‹ ํ˜ธ์™€ ์ถœ๋ ฅ ์‹ ํ˜ธ๊ฐ€ ํผ์…‰ํŠธ๋ก ์—์„œ ๊ฐ๊ฐ ์ž…๋ ฅ๊ฐ’๊ณผ ์ถœ๋ ฅ๊ฐ’์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค.๋Š” ์ž…๋ ฅ๊ฐ’์„ ์˜๋ฏธํ•˜๋ฉฐ,๋Š” ๊ฐ€์ค‘์น˜(Weight),๋Š” ์ถœ๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆผ ์•ˆ์˜ ์›์€ ์ธ๊ณต ๋‰ด๋Ÿฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ์‹ ๊ฒฝ ์„ธํฌ ๋‰ด๋Ÿฐ์—์„œ์˜ ์‹ ํ˜ธ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์ถ•์‚ญ๋Œ๊ธฐ์˜ ์—ญํ• ์„ ํผ์…‰ํŠธ๋ก ์—์„œ๋Š” ๊ฐ€์ค‘์น˜๊ฐ€ ๋Œ€์‹ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ธ๊ณต ๋‰ด๋Ÿฐ์—์„œ ๋ณด๋‚ด์ง„ ์ž…๋ ฅ๊ฐ’ ๋Š” ๊ฐ๊ฐ์˜ ๊ฐ€์ค‘์น˜ ์™€ ํ•จ๊ป˜ ์ข…์ฐฉ์ง€์ธ ์ธ๊ณต ๋‰ด๋Ÿฐ์— ์ „๋‹ฌ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ž…๋ ฅ๊ฐ’์—๋Š” ๊ฐ๊ฐ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ, ์ด๋•Œ ๊ฐ€์ค‘์น˜์˜ ๊ฐ’์ด ํฌ๋ฉด ํด์ˆ˜๋ก ํ•ด๋‹น ์ž…๋ ฅ ๊ฐ’์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž…๋ ฅ๊ฐ’์ด ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ ธ์„œ ์ธ๊ณต ๋‰ด๋Ÿฐ์— ๋ณด๋‚ด์ง€๊ณ , ๊ฐ ์ž…๋ ฅ๊ฐ’๊ณผ ๊ทธ์— ํ•ด๋‹น๋˜๋Š” ๊ฐ€์ค‘์น˜์˜ ๊ณฑ์˜ ์ „์ฒด ํ•ฉ์ด ์ž„๊ณ„์น˜(threshold)๋ฅผ ๋„˜์œผ๋ฉด ์ข…์ฐฉ์ง€์— ์žˆ๋Š” ์ธ๊ณต ๋‰ด๋Ÿฐ์€ ์ถœ๋ ฅ ์‹ ํ˜ธ๋กœ์„œ 1์„ ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” 0์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋ฅผ ๊ณ„๋‹จ ํ•จ์ˆ˜(Step function)๋ผ๊ณ  ํ•˜๋ฉฐ, ์•„๋ž˜๋Š” ๊ทธ๋ž˜ํ”„๋Š” ๊ณ„๋‹จ ํ•จ์ˆ˜์˜ ํ•˜๋‚˜์˜ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋•Œ ๊ณ„๋‹จ ํ•จ์ˆ˜์— ์‚ฌ์šฉ๋œ ์ด ์ž„๊ณ„์น˜ ๊ฐ’์„ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ๋ณดํ†ต ์„ธํƒ€(ฮ˜)๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. f i w x โ‰ฅ โ†’ = i โˆ‘ n i i < โ†’ = ์œ„์˜ ์‹์—์„œ ์ž„๊ณ„์น˜๋ฅผ ์ขŒ๋ณ€์œผ๋กœ ๋„˜๊ธฐ๊ณ  ํŽธํ–ฅ (bias)๋กœ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํŽธํ–ฅ ๋˜ํ•œ ํผ์…‰ํŠธ๋ก ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ์ž…๋ ฅ๊ฐ’์ด 1๋กœ ๊ณ ์ •๋˜๊ณ  ํŽธํ–ฅ ๊ฐ€ ๊ณฑํ•ด์ง€๋Š” ๋ณ€์ˆ˜๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. f i w x + โ‰ฅ โ†’ = i โˆ‘ n i i b 0 y 0 ์ด ์ฑ…์„ ํฌํ•จํ•œ ๋งŽ์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์ž๋ฃŒ์—์„œ ํŽธ์˜์ƒ ํŽธํ–ฅ ๊ฐ€ ๊ทธ๋ฆผ์ด๋‚˜ ์ˆ˜์‹์—์„œ ์ƒ๋žต๋ผ์„œ ํ‘œํ˜„๋˜๊ธฐ๋„ ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ํŽธํ–ฅ ๋˜ํ•œ ๋”ฅ ๋Ÿฌ๋‹์ด ์ตœ์ ์˜ ๊ฐ’์„ ์ฐพ์•„์•ผ ํ•  ๋ณ€์ˆ˜ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒ ์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋‰ด๋Ÿฐ์—์„œ ์ถœ๋ ฅ๊ฐ’์„ ๋ณ€๊ฒฝ์‹œํ‚ค๋Š” ํ•จ์ˆ˜๋ฅผ ํ™œ์„ฑํ™” ํ•จ์ˆ˜(Activation Function)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ดˆ๊ธฐ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์ธ ํผ์…‰ํŠธ๋ก ์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ๊ณ„๋‹จ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, ๊ทธ ๋’ค์— ๋“ฑ์žฅํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐœ์ „๋œ ์‹ ๊ฒฝ๋ง๋“ค์€ ๊ณ„๋‹จ ํ•จ์ˆ˜ ์™ธ์—๋„ ์—ฌ๋Ÿฌ ๋‹ค์–‘ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์•ž์„œ ๋ฐฐ์šด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋‚˜ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜ ๋˜ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์„ ๋ฐฐ์šฐ๊ธฐ ์ „์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ๋จผ์ € ๋ฐฐ์šด ์ด์œ ๋„ ์—ฌ๊ธฐ์— ์žˆ์Šต๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ๊ณ„๋‹จ ํ•จ์ˆ˜์ด์ง€๋งŒ ์—ฌ๊ธฐ์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฐฉ๊ธˆ ๋ฐฐ์šด ํผ์…‰ํŠธ๋ก ์€ ๊ณง ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์™€ ๋™์ผํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜๋ฉด ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจ๋ธ์ด ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ๋Š” ํ•˜๋‚˜์˜ ์ธ๊ณต ๋‰ด๋Ÿฐ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์ธ๊ณต ๋‰ด๋Ÿฐ๊ณผ ์œ„์—์„œ ๋ฐฐ์šด ํผ์…‰ํŠธ๋ก ์˜ ์ฐจ์ด๋Š” ์˜ค์ง ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ์ฐจ์ด์ž…๋‹ˆ๋‹ค. ์ธ๊ณต ๋‰ด๋Ÿฐ : ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ( i w x + ) ์œ„์˜ ํผ์…‰ํŠธ๋ก (์ธ๊ณต ๋‰ด๋Ÿฐ ์ข…๋ฅ˜ ์ค‘ ํ•˜๋‚˜) : ๊ณ„๋‹จ ํ•จ์ˆ˜ ( i w x + ) 2. ๋‹จ์ธต ํผ์…‰ํŠธ๋ก (Single-Layer Perceptron) ์œ„์—์„œ ๋ฐฐ์šด ํผ์…‰ํŠธ๋ก ์„ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์€ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ๊ณผ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง€๋Š”๋ฐ, ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ ๊ฐ’์„ ๋ณด๋‚ด๋Š” ๋‹จ๊ณ„๊ณผ ๊ฐ’์„ ๋ฐ›์•„์„œ ์ถœ๋ ฅํ•˜๋Š” ๋‘ ๋‹จ๊ณ„๋กœ๋งŒ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ ์ด ๊ฐ ๋‹จ๊ณ„๋ฅผ ๋ณดํ†ต ์ธต(layer)์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ์ด ๋‘ ๊ฐœ์˜ ์ธต์„ ์ž…๋ ฅ์ธต(input layer)๊ณผ ์ถœ๋ ฅ์ธต(output layer)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์ด ์–ด๋–ค ์ผ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ•œ๊ณ„๋Š” ๋ฌด์—‡์ธ์ง€ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ๋Š” ๋‘ ๊ฐœ์˜ ๊ฐ’ 0๊ณผ 1์„ ์ž…๋ ฅํ•ด ํ•˜๋‚˜์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํšŒ๋กœ๊ฐ€ ๋ชจ์—ฌ ๋งŒ๋“ค์–ด์ง€๋Š”๋ฐ, ์ด ํšŒ๋กœ๋ฅผ ๊ฒŒ์ดํŠธ(gate)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ดˆ๊ธฐ ํ˜•ํƒœ์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ธ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ ๊ฐ„๋‹จํ•œ XOR ๊ฒŒ์ดํŠธ์กฐ์ฐจ๋„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†๋Š” ๋ถ€์กฑํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด๋ผ๋Š” ์ง€์ ์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์„ ์ด์šฉํ•˜๋ฉด AND, NAND, OR ๊ฒŒ์ดํŠธ๋Š” ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ฒŒ์ดํŠธ ์—ฐ์‚ฐ์— ์“ฐ์ด๋Š” ๊ฒƒ์€ ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’๊ณผ ํ•˜๋‚˜์˜ ์ถœ๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค. AND ๊ฒŒ์ดํŠธ๋ž€ ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’ 1 x ์ด ๊ฐ๊ฐ 0 ๋˜๋Š” 1์˜ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฉด์„œ ๋ชจ๋‘ 1์ธ ๊ฒฝ์šฐ์—๋งŒ ์ถœ๋ ฅ๊ฐ’ ๊ฐ€ 1์ด ๋‚˜์˜ค๋Š” ๊ตฌ์กฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์‹์„ ํ†ตํ•ด AND ๊ฒŒ์ดํŠธ๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๋‘ ๊ฐœ์˜ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ ๊ฐ’์—๋Š” ๋ญ๊ฐ€ ์žˆ์„๊นŒ์š”? ๊ฐ๊ฐ 1 w,๋ผ๊ณ  ํ•œ๋‹ค๋ฉด [0.5, 0.5, -0.7], [0.5, 0.5, -0.8] ๋˜๋Š” [1.0, 1.0, -1.0] ๋“ฑ ์ด ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ์กฐํ•ฉ์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด์„œ AND ๊ฒŒ์ดํŠธ๋ฅผ ์œ„ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ ๊ฐ€์ง„ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์‹์„ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. def AND_gate(x1, x2): w1 = 0.5 w2 = 0.5 b = -0.7 result = x1*w1 + x2*w2 + b if result <= 0: return 0 else: return 1 ์œ„์˜ ํ•จ์ˆ˜์— AND ๊ฒŒ์ดํŠธ์˜ ์ž…๋ ฅ๊ฐ’์„ ๋ชจ๋‘ ๋„ฃ์–ด๋ณด๋ฉด ์˜ค์ง ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ์—๋งŒ 1์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. AND_gate(0, 0), AND_gate(0, 1), AND_gate(1, 0), AND_gate(1, 1) (0, 0, 0, 1) ๊ทธ๋ ‡๋‹ค๋ฉด ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ์—๋งŒ ์ถœ๋ ฅ๊ฐ’์ด 0, ๋‚˜๋จธ์ง€ ์ž…๋ ฅ๊ฐ’์˜ ์Œ(pair)์— ๋Œ€ํ•ด์„œ๋Š” ๋ชจ๋‘ ์ถœ๋ ฅ๊ฐ’์ด 1์ด ๋‚˜์˜ค๋Š” NAND ๊ฒŒ์ดํŠธ๋Š” ์–ด๋–จ๊นŒ์š”? ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋˜ AND ๊ฒŒ์ดํŠธ๋ฅผ ์ถฉ์กฑํ•˜๋Š” ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ ๊ฐ’์ธ [0.5, 0.5, -0.7]์— -๋ฅผ ๋ถ™์—ฌ์„œ [-0.5, -0.5, +0.7]์„ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์‹์— ๋„ฃ์–ด๋ณด๋ฉด NAND ๊ฒŒ์ดํŠธ๋ฅผ ์ถฉ์กฑํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ด๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. def NAND_gate(x1, x2): w1 = -0.5 w2 = -0.5 b = 0.7 result = x1*w1 + x2*w2 + b if result <= 0: return 0 else: return 1 ๋‹จ์ง€ ๊ฐ™์€ ์ฝ”๋“œ์— ํ•จ์ˆ˜ ์ด๋ฆ„๊ณผ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ๋งŒ ๋ฐ”๊ฟจ์„ ๋ฟ์ž…๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์˜ ๊ตฌ์กฐ๋Š” ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. NAND_gate(0, 0), NAND_gate(0, 1), NAND_gate(1, 0), NAND_gate(1, 1) (1, 1, 1, 0) NAND ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ ํŒŒ์ด์ฌ ์ฝ”๋“œ์— ์ž…๋ ฅ๊ฐ’์„ ๋„ฃ์ž, ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ์—๋งŒ 0์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์œผ๋กœ NAND ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. [-0.5, -0.5, -0.7] ์™ธ์—๋„ ํผ์…‰ํŠธ๋ก ์ด NAND ๊ฒŒ์ดํŠธ์˜ ๋™์ž‘์„ ํ•˜๋„๋ก ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ๊ฐ’๋“ค์ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์ด ๋ชจ๋‘ 0์ธ ๊ฒฝ์šฐ์— ์ถœ๋ ฅ๊ฐ’์ด 0์ด๊ณ  ๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋‘ ์ถœ๋ ฅ๊ฐ’์ด 1์ธ OR ๊ฒŒ์ดํŠธ ๋˜ํ•œ ์ ์ ˆํ•œ ๊ฐ€์ค‘์น˜ ๊ฐ’๊ณผ ํŽธํ–ฅ ๊ฐ’๋งŒ ์ฐพ์œผ๋ฉด ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์‹์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ๊ฐ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์— ๋Œ€ํ•ด์„œ [0.6, 0.6, -0.5]๋ฅผ ์„ ํƒํ•˜๋ฉด OR ๊ฒŒ์ดํŠธ๋ฅผ ์ถฉ์กฑํ•ฉ๋‹ˆ๋‹ค. def OR_gate(x1, x2): w1 = 0.6 w2 = 0.6 b = -0.5 result = x1*w1 + x2*w2 + b if result <= 0: return 0 else: return 1 OR_gate(0, 0), OR_gate(0, 1), OR_gate(1, 0), OR_gate(1, 1) (0, 1, 1, 1) ์ด ์™ธ์—๋„ ์ด๋ฅผ ์ถฉ์กฑํ•˜๋Š” ๋‹ค์–‘ํ•œ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ๊ฐ’์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ AND ๊ฒŒ์ดํŠธ, NAND ๊ฒŒ์ดํŠธ, OR ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ์ง€๊ธˆ๋ถ€ํ„ฐ ์„ค๋ช…ํ•  XOR ๊ฒŒ์ดํŠธ๋Š” ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. XOR ๊ฒŒ์ดํŠธ๋Š” ์ž…๋ ฅ๊ฐ’ ๋‘ ๊ฐœ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์„ ๋•Œ์—๋งŒ ์ถœ๋ ฅ๊ฐ’์ด 1์ด ๋˜๊ณ , ์ž…๋ ฅ๊ฐ’ ๋‘ ๊ฐœ๊ฐ€ ์„œ๋กœ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋ฉด ์ถœ๋ ฅ๊ฐ’์ด 0์ด ๋˜๋Š” ๊ฒŒ์ดํŠธ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ํŒŒ์ด์ฌ ์ฝ”๋“œ์— ์•„๋ฌด๋ฆฌ ์ˆ˜๋งŽ์€ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ๋„ฃ์–ด๋ด๋„ XOR ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ ์ง์„  ํ•˜๋‚˜๋กœ ๋‘ ์˜์—ญ์„ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ๋งŒ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด AND ๊ฒŒ์ดํŠธ์— ๋Œ€ํ•œ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์„ ์‹œ๊ฐํ™”ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ๋Š” ์ถœ๋ ฅ๊ฐ’ 0์„ ํ•˜์–€์ƒ‰ ์›, 1์„ ๊ฒ€์€์ƒ‰ ์›์œผ๋กœ ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. AND ๊ฒŒ์ดํŠธ๋ฅผ ์ถฉ์กฑํ•˜๋ ค๋ฉด ํ•˜์–€์ƒ‰ ์›๊ณผ ๊ฒ€์€์ƒ‰ ์›์„ ์ง์„ ์œผ๋กœ ๋‚˜๋ˆ„๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ NAND ๊ฒŒ์ดํŠธ๋‚˜ OR ๊ฒŒ์ดํŠธ์— ๋Œ€ํ•ด์„œ๋„ ์‹œ๊ฐํ™”๋ฅผ ํ–ˆ์„ ๋•Œ ์ง์„ ์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด XOR ๊ฒŒ์ดํŠธ๋Š” ์–ด๋–จ๊นŒ์š”? XOR ๊ฒŒ์ดํŠธ๋Š” ์ž…๋ ฅ๊ฐ’ ๋‘ ๊ฐœ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์„ ๋•Œ์—๋งŒ ์ถœ๋ ฅ๊ฐ’์ด 1์ด ๋˜๊ณ , ์ž…๋ ฅ๊ฐ’ ๋‘ ๊ฐœ๊ฐ€ ์„œ๋กœ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋ฉด ์ถœ๋ ฅ๊ฐ’์ด 0์ด ๋˜๋Š” ๊ฒŒ์ดํŠธ์ž…๋‹ˆ๋‹ค. XOR ๊ฒŒ์ดํŠธ๋ฅผ ์‹œ๊ฐํ™”ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•˜์–€์ƒ‰ ์›๊ณผ ๊ฒ€์€์ƒ‰ ์›์„ ์ง์„  ํ•˜๋‚˜๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ๋Š” XOR ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ขŒ์ธก ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ ์–ด๋„ ๋‘ ๊ฐœ์˜ ์„ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ด์— ๋Œ€ํ•œ ํ•ด๋‹ต์€ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์ž…๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์„ ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก (MultiLayer Perceptron, MLP) XOR ๊ฒŒ์ดํŠธ๋Š” ๊ธฐ์กด์˜ AND, NAND, OR ๊ฒŒ์ดํŠธ๋ฅผ ์กฐํ•ฉํ•˜๋ฉด ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก  ๊ด€์ ์—์„œ ๋งํ•˜๋ฉด ์ธต์„ ๋” ์Œ“์œผ๋ฉด ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ๊ณผ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์ฐจ์ด๋Š” ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต๋งŒ ์กด์žฌํ•˜์ง€๋งŒ, ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์ค‘๊ฐ„์— ์ธต์„ ๋” ์ถ”๊ฐ€ํ•˜์˜€๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต ์‚ฌ์ด์— ์กด์žฌํ•˜๋Š” ์ธต์„ ์€๋‹‰์ธต(hidden layer)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์ค‘๊ฐ„์— ์€๋‹‰์ธต์ด ์กด์žฌํ•œ๋‹ค๋Š” ์ ์ด ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์ค„์—ฌ์„œ MLP๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ AND, NAND, OR ๊ฒŒ์ดํŠธ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ XOR ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ์˜ˆ์ž…๋‹ˆ๋‹ค. (์‹ค์ œ ๊ตฌํ˜„์€ ์ˆ™์ œ๋กœ ๋‚จ๊ฒจ๋‘๊ฒ ์Šต๋‹ˆ๋‹ค. ํžŒํŠธ๋ฅผ ๋“œ๋ฆฌ์ž๋ฉด ์œ„์˜ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์—์„œ ์‚ฌ์šฉํ•œ ํ•จ์ˆ˜๋“ค์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.) XOR ์˜ˆ์ œ์—์„œ๋Š” ์€๋‹‰์ธต 1๊ฐœ๋งŒ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ, ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ๋ณธ๋ž˜ ์€๋‹‰์ธต์ด 1๊ฐœ ์ด์ƒ์ธ ํผ์…‰ํŠธ๋ก ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, XOR ๋ฌธ์ œ๋‚˜ ๊ธฐํƒ€ ๋ณต์žกํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์ค‘๊ฐ„์— ์ˆ˜๋งŽ์€ ์€๋‹‰์ธต์„ ๋” ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์€๋‹‰์ธต์˜ ๊ฐœ์ˆ˜๋Š” 2๊ฐœ์ผ ์ˆ˜๋„ ์žˆ๊ณ , ์ˆ˜์‹ญ ๊ฐœ์ผ ์ˆ˜๋„ ์žˆ๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•˜๊ธฐ ๋‚˜๋ฆ„์ž…๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋” ์–ด๋ ค์šด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ ์€๋‹‰์ธต์ด ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€๋˜๊ณ (์ด ๊ฒฝ์šฐ์—๋Š” ์€๋‹‰์ธต์ด 2๊ฐœ), ๋‰ด๋Ÿฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ฆฐ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์€๋‹‰์ธต์ด 2๊ฐœ ์ด์ƒ์ธ ์‹ ๊ฒฝ๋ง์„ ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง(Deep Neural Network, DNN)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง์€ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ๋งŒ ์ด์•ผ๊ธฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์—ฌ๋Ÿฌ ๋ณ€ํ˜•๋œ ๋‹ค์–‘ํ•œ ์‹ ๊ฒฝ๋ง๋“ค๋„ ์€๋‹‰์ธต์ด 2๊ฐœ ์ด์ƒ์ด ๋˜๋ฉด ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” OR, AND, XOR ๊ฒŒ์ดํŠธ ๋“ฑ. ํผ์…‰ํŠธ๋ก ์ด ์ œ๋Œ€๋กœ ๋œ ์ •๋‹ต์„ ์ถœ๋ ฅํ•  ๋•Œ๊นŒ์ง€ ์ €์ž๊ฐ€ ์ง์ ‘ ๊ฐ€์ค‘์น˜๋ฅผ ๋ฐ”๊ฟ”๋ณด๋ฉด์„œ ์ ์ ˆํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด์ œ๋Š” ๊ธฐ๊ณ„๊ฐ€ ๊ฐ€์ค‘์น˜๋ฅผ ์Šค์Šค๋กœ ์ฐพ์•„๋‚ด๋„๋ก ์ž๋™ํ™”์‹œ์ผœ์•ผ ํ•˜๋Š”๋ฐ, ์ด๊ฒƒ์ด ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ๋งํ•˜๋Š” ํ›ˆ๋ จ(training) ๋˜๋Š” ํ•™์Šต(learning) ๋‹จ๊ณ„์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์„ ํ˜• ํšŒ๊ท€์™€ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ๋ณด์•˜๋“ฏ์ด ์†์‹ค ํ•จ์ˆ˜(Loss function)์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €(Optimizer)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ ํ•™์Šต์„ ์‹œํ‚ค๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง์ผ ๊ฒฝ์šฐ์—๋Š” ์ด๋ฅผ ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง์„ ํ•™์Šต์‹œํ‚จ๋‹ค๊ณ  ํ•˜์—ฌ, ๋”ฅ ๋Ÿฌ๋‹(Deep Learning)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 07-02 ์ธ๊ณต ์‹ ๊ฒฝ๋ง(Artificial Neural Network) ํ›‘์–ด๋ณด๊ธฐ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ๋“ค์„ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. 1. ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง(Feed-Forward Neural Network, FFNN) ์œ„ ๊ทธ๋ฆผ์˜ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก (MLP)๊ณผ ๊ฐ™์ด ์˜ค์ง ์ž…๋ ฅ์ธต์—์„œ ์ถœ๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ์‚ฐ์ด ์ „๊ฐœ๋˜๋Š” ์‹ ๊ฒฝ๋ง์„ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง(Feed-Forward Neural Network, FFNN)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ FFNN์— ์†ํ•˜์ง€ ์•Š๋Š” RNN์ด๋ผ๋Š” ์‹ ๊ฒฝ๋ง์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด ์‹ ๊ฒฝ๋ง์€ ์€๋‹‰์ธต์˜ ์ถœ๋ ฅ๊ฐ’์„ ์ถœ๋ ฅ์ธต์œผ๋กœ๋„ ๊ฐ’์„ ๋ณด๋‚ด์ง€๋งŒ, ๋™์‹œ์— ์€๋‹‰์ธต์˜ ์ถœ๋ ฅ๊ฐ’์ด ๋‹ค์‹œ ์€๋‹‰์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 2. ์ „๊ฒฐํ•ฉ์ธต(Fully-connected layer, FC, Dense layer) ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์€๋‹‰์ธต๊ณผ ์ถœ๋ ฅ์ธต์— ์žˆ๋Š” ๋ชจ๋“  ๋‰ด๋Ÿฐ์€ ๋ฐ”๋กœ ์ด์ „ ์ธต์˜ ๋ชจ๋“  ๋‰ด๋Ÿฐ๊ณผ ์—ฐ๊ฒฐ๋ผ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์™€ ๊ฐ™์ด ์–ด๋–ค ์ธต์˜ ๋ชจ๋“  ๋‰ด๋Ÿฐ์ด ์ด์ „ ์ธต์˜ ๋ชจ๋“  ๋‰ด๋Ÿฐ๊ณผ ์—ฐ๊ฒฐ๋ผ ์žˆ๋Š” ์ธต์„ ์ „๊ฒฐํ•ฉ์ธต(Fully-connected layer) ๋˜๋Š” ์™„์ „์—ฐ๊ฒฐ์ธต์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ค„์—ฌ์„œ FC๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ๋ณธ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ๋ชจ๋“  ์€๋‹‰์ธต๊ณผ ์ถœ๋ ฅ์ธต์€ ์ „๊ฒฐํ•ฉ์ธต์ž…๋‹ˆ๋‹ค. ๋™์ผํ•œ ์˜๋ฏธ๋กœ ๋ฐ€์ง‘์ธต(Dense layer)์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•˜๋Š”๋ฐ, ์ผ€๋ผ์Šค์—์„œ๋Š” ๋ฐ€์ง‘์ธต์„ ๊ตฌํ˜„ํ•  ๋•Œ Dense()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 3. ํ™œ์„ฑํ™” ํ•จ์ˆ˜(Activation Function) ์•ž์„œ ๋ฐฐ์šด ํผ์…‰ํŠธ๋ก ์—์„œ๋Š” ๊ณ„๋‹จ ํ•จ์ˆ˜(Step function)๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅ๊ฐ’์ด 0์ด ๋ ์ง€, 1์ด ๋ ์ง€๋ฅผ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ์‹ค์ œ ๋‡Œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์‹ ๊ฒฝ ์„ธํฌ ๋‰ด๋Ÿฐ์ด ์ „์œ„๊ฐ€ ์ผ์ •์น˜ ์ด์ƒ์ด ๋˜๋ฉด ์‹œ๋ƒ…์Šค๊ฐ€ ์„œ๋กœ ํ™”ํ•™์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜๋Š” ๋ชจ์Šต์„ ๋ชจ๋ฐฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์€๋‹‰์ธต๊ณผ ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ์—์„œ ์ถœ๋ ฅ๊ฐ’์„ ๊ฒฐ์ •ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ™œ์„ฑํ™” ํ•จ์ˆ˜(Activation function)๋ผ๊ณ  ํ•˜๋Š”๋ฐ ๊ณ„๋‹จ ํ•จ์ˆ˜๋Š” ์ด๋Ÿฌํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ํ•˜๋‚˜์˜ ์˜ˆ์ œ์— ๋ถˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. ์ผ๋ถ€๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ์—์„œ ์ด๋ฏธ ๋ดค๋˜ ํ•จ์ˆ˜๋“ค์ž…๋‹ˆ๋‹ค. (1) ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ํŠน์ง• - ๋น„์„ ํ˜• ํ•จ์ˆ˜(Nonlinear function) ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ํŠน์ง•์€ ์„ ํ˜• ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๋น„์„ ํ˜• ํ•จ์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์„ ํ˜• ํ•จ์ˆ˜๋ž€ ์ถœ๋ ฅ์ด ์ž…๋ ฅ์˜ ์ƒ์ˆ˜๋ฐฐ๋งŒํผ ๋ณ€ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์„ ํ˜•ํ•จ์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ( ) w +๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ, ์™€๋Š” ์ƒ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ์‹์„ ๊ทธ๋ž˜ํ”„๋กœ ์‹œ๊ฐํ™”ํ•˜๋ฉด ์ง์„ ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ๋น„์„ ํ˜• ํ•จ์ˆ˜๋Š” ์ง์„  1๊ฐœ๋กœ๋Š” ๊ทธ๋ฆด ์ˆ˜ ์—†๋Š” ํ•จ์ˆ˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ๋น„์„ ํ˜• ํ•จ์ˆ˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ํผ์…‰ํŠธ๋ก ์—์„œ๋„ ๊ณ„๋‹จ ํ•จ์ˆ˜๋ผ๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ๊ณ„๋‹จ ํ•จ์ˆ˜ ๋˜ํ•œ ๋น„์„ ํ˜• ํ•จ์ˆ˜์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋Šฅ๋ ฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ๋Š” ์€๋‹‰์ธต์„ ๊ณ„์†ํ•ด์„œ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์„ ํ˜• ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์€๋‹‰์ธต์„ ์Œ“์„ ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์„ ํ˜• ํ•จ์ˆ˜๋ฅผ ์„ ํƒํ•˜๊ณ , ์ธต์„ ๊ณ„์† ์Œ“๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ( ) w๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๋‹ค๊ฐ€ ์€๋‹‰์ธต์„ ๋‘ ๊ฐœ ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์ถœ๋ ฅ์ธต์„ ํฌํ•จํ•ด์„œ ( ) f ( ( ( ) ) ) ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ร— ร— ร—์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Š” ์ž˜ ์ƒ๊ฐํ•ด ๋ณด๋ฉด์˜ ์„ธ ์ œ๊ณฑ๊ฐ’์„ ๋ผ๊ณ  ์ •์˜ํ•ด๋ฒ„๋ฆฌ๋ฉด ( ) k ์™€ ๊ฐ™์ด ๋‹ค์‹œ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์„ ํ˜• ํ•จ์ˆ˜๋กœ ์€๋‹‰์ธต์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถ”๊ฐ€ํ•˜๋”๋ผ๋„ 1ํšŒ ์ถ”๊ฐ€ํ•œ ๊ฒƒ๊ณผ ์ฐจ์ด๊ฐ€ ์—†์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ™œ์„ฑํ™” ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์„ ํ˜• ํ•จ์ˆ˜ ์ธต์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ์ข…์ข… ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ธต์„ ๋น„์„ ํ˜• ์ธต๋“ค๊ณผ ํ•จ๊ป˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ผ๋ถ€๋กœ์„œ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋Š”๋ฐ, ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜๊ฐ€ ์ƒˆ๋กœ ์ƒ๊ธด๋‹ค๋Š” ์ ์—์„œ ์˜๋ฏธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์„ ํ˜• ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์ธต์„ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์€๋‹‰์ธต๊ณผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด ์ฑ…์—์„œ๋Š” ์„ ํ˜•์ธต(linear layer)์ด๋‚˜ ํˆฌ์‚ฌ์ธต(projection layer) ๋“ฑ์˜ ๋‹ค๋ฅธ ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋’ค์˜ ์ฑ•ํ„ฐ์—์„œ ์–ธ๊ธ‰ํ•  ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)๋„ ์ผ์ข…์˜ ์„ ํ˜•์ธต์ž…๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ์ธต์—๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์€๋‹‰์ธต์„ ์„ ํ˜•์ธต๊ณผ ๋Œ€๋น„๋˜๋Š” ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋ฉด ๋น„์„ ํ˜•์ธต(nonlinear layer)์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์„ ํ†ตํ•ด ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๊ทธ๋ ค๋ด…์‹œ๋‹ค. import numpy as np import matplotlib.pyplot as plt (2) ๊ณ„๋‹จ ํ•จ์ˆ˜(Step function) def step(x): return np.array(x > 0, dtype=np.int) x = np.arange(-5.0, 5.0, 0.1) # -5.0๋ถ€ํ„ฐ 5.0๊นŒ์ง€ 0.1 ๊ฐ„๊ฒฉ ์ƒ์„ฑ y = step(x) plt.title('Step Function') plt.plot(x, y) plt.show() ๊ณ„๋‹จ ํ•จ์ˆ˜๋Š” ๊ฑฐ์˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š์ง€๋งŒ ํผ์…‰ํŠธ๋ก ์„ ํ†ตํ•ด ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ฒ˜์Œ ๋ฐฐ์šธ ๋•Œ ์ ‘ํ•˜๊ฒŒ ๋˜๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. (3) ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜(Sigmoid function)์™€ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ•™์Šต ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์ˆœ์ „ํŒŒ(forward propagation) ์—ฐ์‚ฐ์„ ํ•˜๊ณ , ๊ทธ๋ฆฌ๊ณ  ์ˆœ์ „ํŒŒ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋‚˜์˜จ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ์†์‹ค ํ•จ์ˆ˜(loss function)์„ ํ†ตํ•ด ๊ณ„์‚ฐํ•˜๊ณ , ๊ทธ๋ฆฌ๊ณ  ์ด ์†์‹ค(์˜ค์ฐจ๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. loss)์„ ๋ฏธ๋ถ„์„ ํ†ตํ•ด์„œ ๊ธฐ์šธ๊ธฐ(gradient)๋ฅผ ๊ตฌํ•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅ์ธต์—์„œ ์ž…๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ณผ์ •์ธ ์—ญ์ „ํŒŒ(back propagation)๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ญ์ „ํŒŒ์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ๋” ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๊ฒ ์ง€๋งŒ ์ผ๋‹จ ์—ฌ๊ธฐ์—์„œ๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ ์ถœ๋ ฅ์ธต์—์„œ ์ž…๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ณผ์ •์ด๋ผ๊ณ ๋งŒ ์–ธ๊ธ‰ํ•ด๋‘๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ๋ฌธ์ œ์ ์€ ๋ฏธ๋ถ„์„ ํ•ด์„œ ๊ธฐ์šธ๊ธฐ(gradient)๋ฅผ ๊ตฌํ•  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. # ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ์ฝ”๋“œ def sigmoid(x): return 1/(1+np.exp(-x)) x = np.arange(-5.0, 5.0, 0.1) y = sigmoid(x) plt.plot(x, y) plt.plot([0,0],[1.0,0.0], ':') # ๊ฐ€์šด๋ฐ ์ ์„  ์ถ”๊ฐ€ plt.title('Sigmoid Function') plt.show() ์œ„ ๊ทธ๋ž˜ํ”„๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์ด 0 ๋˜๋Š” 1์— ๊ฐ€๊นŒ์›Œ์ง€๋ฉด, ๊ทธ๋ž˜ํ”„์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์™„๋งŒํ•ด์ง€๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ๊ฐ€ ์™„๋งŒํ•ด์ง€๋Š” ๊ตฌ๊ฐ„์„ ์ฃผํ™ฉ์ƒ‰, ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ตฌ๊ฐ„์„ ์ดˆ๋ก์ƒ‰์œผ๋กœ ์น ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผํ™ฉ์ƒ‰ ๊ตฌ๊ฐ„์—์„œ๋Š” ๋ฏธ๋ถ„ ๊ฐ’์ด 0์— ๊ฐ€๊นŒ์šด ์•„์ฃผ ์ž‘์€ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ดˆ๋ก์ƒ‰ ๊ตฌ๊ฐ„์—์„œ์˜ ๋ฏธ๋ถ„ ๊ฐ’์€ ์ตœ๋Œ“๊ฐ’์ด 0.25์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•œ ๊ฐ’์€ ์ ์–ด๋„ 0.25 ์ดํ•˜์˜ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ํ•˜๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ธต์„ ์Œ“๋Š”๋‹ค๋ฉด, ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ณผ์ •์ธ ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ 0์— ๊ฐ€๊นŒ์šด ๊ฐ’์ด ๋ˆ„์ ํ•ด์„œ ๊ณฑํ•ด์ง€๊ฒŒ ๋˜๋ฉด์„œ, ์•ž๋‹จ์—๋Š” ๊ธฐ์šธ๊ธฐ(๋ฏธ๋ถ„ ๊ฐ’)๊ฐ€ ์ž˜ ์ „๋‹ฌ๋˜์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ˜„์ƒ์„ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค(Vanishing Gradient) ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์€๋‹‰์ธต์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค์ˆ˜๊ฐ€ ๋  ๊ฒฝ์šฐ์—๋Š” 0์— ๊ฐ€๊นŒ์šด ๊ธฐ์šธ๊ธฐ๊ฐ€ ๊ณ„์† ๊ณฑํ•ด์ง€๋ฉด ์•ž๋‹จ์—์„œ๋Š” ๊ฑฐ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ์ „ํŒŒ ๋ฐ›์„ ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š์•„ ํ•™์Šต์ด ๋˜์ง€๋ฅผ ์•Š์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์€๋‹‰์ธต์ด ๊นŠ์€ ์‹ ๊ฒฝ๋ง์—์„œ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋ฌธ์ œ๋กœ ์ธํ•ด ์ถœ๋ ฅ์ธต๊ณผ ๊ฐ€๊นŒ์šด ์€๋‹‰์ธต์—์„œ๋Š” ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ž˜ ์ „ํŒŒ๋˜์ง€๋งŒ, ์•ž๋‹จ์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ œ๋Œ€๋กœ ์ „ํŒŒ๋˜์ง€ ์•Š๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์€๋‹‰์ธต์—์„œ์˜ ์‚ฌ์šฉ์€ ์ง€์–‘๋ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” ์ฃผ๋กœ ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด ์ถœ๋ ฅ์ธต์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (4) ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜(Hyperbolic tangent function) ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜(tanh)๋Š” ์ž…๋ ฅ๊ฐ’์„ -1๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. x = np.arange(-5.0, 5.0, 0.1) # -5.0๋ถ€ํ„ฐ 5.0๊นŒ์ง€ 0.1 ๊ฐ„๊ฒฉ ์ƒ์„ฑ y = np.tanh(x) plt.plot(x, y) plt.plot([0,0],[1.0, -1.0], ':') plt.axhline(y=0, color='orange', linestyle='--') plt.title('Tanh Function') plt.show() ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋„ -1๊ณผ 1์— ๊ฐ€๊นŒ์šด ์ถœ๋ ฅ๊ฐ’์„ ์ถœ๋ ฅํ•  ๋•Œ, ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์™€ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ์—๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์™€๋Š” ๋‹ฌ๋ฆฌ 0์„ ์ค‘์‹ฌ์œผ๋กœ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ–ˆ์„ ๋•Œ์˜ ์ตœ๋Œ“๊ฐ’์€ 1๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์ธ 0.25๋ณด๋‹ค๋Š” ํฝ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ฏธ๋ถ„ํ–ˆ์„ ๋•Œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ณด๋‹ค๋Š” ์ „๋ฐ˜์ ์œผ๋กœ ํฐ ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ณด๋‹ค๋Š” ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ์ฆ์ƒ์ด ์ ์€ ํŽธ์ด๋ฉฐ ์€๋‹‰์ธต์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ณด๋‹ค๋Š” ์„ ํ˜ธ๋ฉ๋‹ˆ๋‹ค. (5) ๋ ๋ฃจ ํ•จ์ˆ˜(ReLU) ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์€๋‹‰์ธต์—์„œ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ˆ˜์‹์€ ( ) m x ( , ) ๋กœ ์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. def relu(x): return np.maximum(0, x) x = np.arange(-5.0, 5.0, 0.1) y = relu(x) plt.plot(x, y) plt.plot([0,0],[5.0,0.0], ':') plt.title('Relu Function') plt.show() ๋ ๋ฃจ ํ•จ์ˆ˜๋Š” ์Œ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 0์„ ์ถœ๋ ฅํ•˜๊ณ , ์–‘์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ž…๋ ฅ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ํŠน์ง•์ธ ํ•จ์ˆ˜๋กœ ์ถœ๋ ฅ๊ฐ’์ด ํŠน์ • ์–‘์ˆ˜ ๊ฐ’์— ์ˆ˜๋ ดํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 0 ์ด์ƒ์˜ ์ž…๋ ฅ๊ฐ’์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฏธ๋ถ„ ๊ฐ’์ด ํ•ญ์ƒ 1์ž…๋‹ˆ๋‹ค. ๊นŠ์€ ์‹ ๊ฒฝ๋ง์˜ ์€๋‹‰์ธต์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ณด๋‹ค ํ›จ์”ฌ ๋” ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๋ ๋ฃจ ํ•จ์ˆ˜๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์™€ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜์™€ ๊ฐ™์ด ์–ด๋–ค ์—ฐ์‚ฐ์ด ํ•„์š”ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹จ์ˆœ ์ž„๊ณ„๊ฐ’์ด๋ฏ€๋กœ ์—ฐ์‚ฐ ์†๋„๋„ ๋น ๋ฆ…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ ๋ฌธ์ œ์ ์ด ์กด์žฌํ•˜๋Š”๋ฐ, ์ž…๋ ฅ๊ฐ’์ด ์Œ์ˆ˜ ๋ฉด ๊ธฐ์šธ๊ธฐ. ์ฆ‰, ๋ฏธ๋ถ„ ๊ฐ’๋„ 0์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‰ด๋Ÿฐ์€ ๋‹ค์‹œ ํšŒ์ƒํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ์ฃฝ์€ ๋ ๋ฃจ(dying ReLU)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. (6) ๋ฆฌํ‚ค ๋ ๋ฃจ(Leaky ReLU) ์ฃฝ์€ ๋ ๋ฃจ๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ReLU์˜ ๋ณ€ํ˜• ํ•จ์ˆ˜๋“ค์ด ๋“ฑ์žฅํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ณ€ํ˜• ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” Leaky ReLU์— ๋Œ€ํ•ด์„œ๋งŒ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. Leaky ReLU๋Š” ์ž…๋ ฅ๊ฐ’์ด ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ์— 0์ด ์•„๋‹ˆ๋ผ 0.001๊ณผ ๊ฐ™์€ ๋งค์šฐ ์ž‘์€ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜์‹์€ ( ) m x ( x x ) ๋กœ ์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. a๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ Leaky('์ƒˆ๋Š”') ์ •๋„๋ฅผ ๊ฒฐ์ •ํ•˜๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” 0.01์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” '์ƒˆ๋Š” ์ •๋„'๋ผ๋Š” ๊ฒƒ์€ ์ž…๋ ฅ๊ฐ’์˜ ์Œ์ˆ˜์ผ ๋•Œ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๋น„์œ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. a = 0.1 def leaky_relu(x): return np.maximum(a*x, x) x = np.arange(-5.0, 5.0, 0.1) y = leaky_relu(x) plt.plot(x, y) plt.plot([0,0],[5.0,0.0], ':') plt.title('Leaky ReLU Function') plt.show() ์œ„์˜ ๊ทธ๋ž˜ํ”„์—์„œ๋Š” ์ƒˆ๋Š” ๋ชจ์Šต์„ ํ™•์‹คํžˆ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด a๋ฅผ 0.1๋กœ ์žก์•˜์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅ๊ฐ’์ด ์Œ์ˆ˜๋ผ๋„ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ด ๋˜์ง€ ์•Š์œผ๋ฉด ReLU๋Š” ์ฃฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. (7) ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜(Softmax function) ์€๋‹‰์ธต์—์„œ๋Š” ReLU(๋˜๋Š” ReLU ๋ณ€ํ˜•) ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ถœ๋ ฅ์ธต์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๊ฐ€ ๋‘ ๊ฐ€์ง€ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ (Binary Classification) ๋ฌธ์ œ์— ์‚ฌ์šฉ๋œ๋‹ค๋ฉด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ์„ธ ๊ฐ€์ง€ ์ด์ƒ์˜ (์ƒํ˜ธ ๋ฐฐํƒ€์ ์ธ) ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(MultiClass Classification) ๋ฌธ์ œ์— ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ ๋”ฅ ๋Ÿฌ๋‹์œผ๋กœ ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ํ•  ๋•Œ๋Š” ์ถœ๋ ฅ์ธต์— ์•ž์„œ ๋ฐฐ์šด ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋”ฅ ๋Ÿฌ๋‹์œผ๋กœ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ๋Š” ์ถœ๋ ฅ์ธต์— ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. x = np.arange(-5.0, 5.0, 0.1) # -5.0๋ถ€ํ„ฐ 5.0๊นŒ์ง€ 0.1 ๊ฐ„๊ฒฉ ์ƒ์„ฑ y = np.exp(x) / np.sum(np.exp(x)) plt.plot(x, y) plt.title('Softmax Function') plt.show() 07-03 ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ์ดํ•ดํ•˜๋Š” ์‹ ๊ฒฝ๋ง ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ ์ž…๋ ฅ์ธต์—์„œ ์ถœ๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•˜๋Š” ๊ณผ์ •์„ ์ˆœ์ „ํŒŒ(Forward Propagation)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅด๊ฒŒ ๋งํ•˜๋ฉด ์ฃผ์–ด์ง„ ์ž…๋ ฅ์ด ์ž…๋ ฅ์ธต์œผ๋กœ ๋“ค์–ด๊ฐ€์„œ ์€๋‹‰์ธต์„ ์ง€๋‚˜ ์ถœ๋ ฅ์ธต์—์„œ ์˜ˆ์ธก๊ฐ’์„ ์–ป๋Š” ๊ณผ์ •์„ ์ˆœ์ „ ํŒŒ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‹ ๊ฒฝ๋ง์˜ ์ˆœ์ „ ํŒŒ๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ๊ณผ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก  ๋‚ด์˜ ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ ๊ฐ€์ค‘์น˜ ์™€ ํŽธํ–ฅ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถ”์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ์ˆœ์ „ํŒŒ(Foward Propagation) ํ™œ์„ฑํ™” ํ•จ์ˆ˜, ์€๋‹‰์ธต์˜ ์ˆ˜, ๊ฐ ์€๋‹‰์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜ ๋“ฑ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•˜๊ณ  ๋‚˜๋ฉด ์ž…๋ ฅ๊ฐ’์€ ์ž…๋ ฅ์ธต, ์€๋‹‰์ธต์„ ์ง€๋‚˜๋ฉด์„œ ๊ฐ ์ธต์—์„œ์˜ ๊ฐ€์ค‘์น˜์™€ ํ•จ๊ป˜ ์—ฐ์‚ฐ๋˜๋ฉฐ ์ถœ๋ ฅ์ธต์œผ๋กœ ํ–ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ถœ๋ ฅ์ธต์—์„œ ๋ชจ๋“  ์—ฐ์‚ฐ์„ ๋งˆ์นœ ์˜ˆ์ธก๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์ž…๋ ฅ์ธต์—์„œ ์ถœ๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ์˜ˆ์ธก๊ฐ’์˜ ์—ฐ์‚ฐ์ด ์ง„ํ–‰๋˜๋Š” ๊ณผ์ •์„ ์ˆœ์ „ ํŒŒ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ์ˆœ์ „ํŒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„์™€ ๊ฐ™์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ž…๋ ฅ์˜ ์ฐจ์›์ด 3, ์ถœ๋ ฅ์˜ ์ฐจ์›์ด 2์ธ ์œ„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๊ตฌํ˜„ํ•ด ๋ณธ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential() # 3๊ฐœ์˜ ์ž…๋ ฅ๊ณผ 2๊ฐœ์˜ ์ถœ๋ ฅ model.add(Dense(2, input_dim=3, activation='softmax')) ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ž„์˜๋กœ ๊ธฐ์žฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด๋ž€ ํ‘œํ˜„์ด ์•„์ง ์–ด์ƒ‰ํ•œ๋‹ค๋ฉด ์•ž์—์„œ ๋ฐฐ์šด ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ๋ชจ๋ธ์„ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋„ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋Š” ์ถœ๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ 2๋กœ ๋‘๋ฉด ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ด ๋ฉ๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๊ฐ€ ์•„๋‹Œ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋กœ๋„ ์ด์ง„ ๋ถ„๋ฅ˜๋Š” ์ˆ˜ํ–‰ ๊ฐ€๋Šฅํ•จ์„ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ์ผ€๋ผ์Šค์—์„œ๋Š” .summary()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ๋ชจ๋ธ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜(๊ฐ€์ค‘์น˜ ์™€ ํŽธํ–ฅ์˜ ๊ฐœ์ˆ˜)๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. model.summary() Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense (Dense) (None, 2) 8 ================================================================= Total params: 8 Trainable params: 8 Non-trainable params: 0 _________________________________________________________________ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜๊ฐ€ 8๊ฐœ๋ผ๊ณ  ๋‚˜์˜ต๋‹ˆ๋‹ค. ์œ„ ์‹ ๊ฒฝ๋ง์—์„œ ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ ์™€์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ด ํ•ฉํ•ด์„œ 8๊ฐœ๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๊ทธ๋Ÿฐ์ง€ ์œ„ ์‹ ๊ฒฝ๋ง์„ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ ๊ด€์ ์—์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์œ„ ๋ชจ๋ธ์€ ์ž…๋ ฅ์˜ ์ฐจ์›์ด 3, ์ถœ๋ ฅ์˜ ์ฐจ์›์ด 2์ž…๋‹ˆ๋‹ค. ๋˜๋Š” ์‹ ๊ฒฝ๋ง์˜ ์šฉ์–ด๋กœ์„œ ํ‘œํ˜„ํ•œ๋‹ค๋ฉด, ์ž…๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ์ด 3๊ฐœ, ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ์ด 2๊ฐœ๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์‹ ๊ฒฝ๋ง ๊ทธ๋ฆผ์—์„œ ํ™”์‚ดํ‘œ ๊ฐ๊ฐ์€ ๊ฐ€์ค‘์น˜๋ฅผ ์˜๋ฏธํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 3๊ฐœ์˜ ๋‰ด๋Ÿฐ๊ณผ 2๊ฐœ์˜ ๋‰ด๋Ÿฐ ์‚ฌ์ด์—๋Š” ์ด 6๊ฐœ์˜ ํ™”์‚ดํ‘œ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ, ์ด๋Š” ์œ„ ์‹ ๊ฒฝ๋ง์—์„œ ๊ฐ€์ค‘์น˜์˜ ๊ฐœ์ˆ˜๊ฐ€ 6๊ฐœ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ–‰๋ ฌ๊ณฑ ๊ด€์ ์—์„œ๋Š” 3์ฐจ์› ๋ฒกํ„ฐ์—์„œ 2์ฐจ์› ๋ฒกํ„ฐ๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„œ 3 ร— 2 ํ–‰๋ ฌ์„ ๊ณฑํ–ˆ๋‹ค๊ณ  ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ–‰๋ ฌ ๊ฐ๊ฐ์˜ ์›์†Œ๊ฐ€ ๊ฐ๊ฐ์˜ ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ๋Š” 1 ์— ์—ฐ๊ฒฐ๋˜๋Š” ํ™”์‚ดํ‘œ 1 w, 3 ๋ฅผ ์ฃผํ™ฉ์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ , 2 ์— ์—ฐ๊ฒฐ๋˜๋Š” ํ™”์‚ดํ‘œ 4 w, 6 ๋ฅผ ์ดˆ๋ก์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋™๊ทธ๋ž€ ๋‰ด๋Ÿฐ๊ณผ ํ™”์‚ดํ‘œ๋กœ ํ‘œํ˜„ํ•˜๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ํŽธํ–ฅ์˜ ๊ฒฝ์šฐ์—๋Š” ํŽธ์˜์ƒ ์ƒ๋žต๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์ง€๋งŒ, ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” ํŽธํ–ฅ์˜ ์—ฐ์‚ฐ ๋˜ํ•œ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ๋‰ด๋Ÿฐ๊ณผ ํ™”์‚ดํ‘œ๋กœ ํ‘œํ˜„ํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ํŽธํ–ฅ์„ ํ‘œํ˜„ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ, ํ–‰๋ ฌ ์—ฐ์‚ฐ์‹์—์„œ๋Š” 1 b๋ฅผ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํŽธํ–ฅ์˜ ๊ฐœ์ˆ˜๋Š” ํ•ญ์ƒ ์ถœ๋ ฅ์˜ ์ฐจ์›์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ฒฝ์šฐ์—๋Š” ์ถœ๋ ฅ์˜ ์ฐจ์›์ด 2์ธ๋ฐ, ์ด์— ๋”ฐ๋ผ์„œ ํŽธํ–ฅ ๋˜ํ•œ 1 b๋กœ ๋‘ ๊ฐœ์ž…๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜์˜ ๊ฐœ์ˆ˜๊ฐ€ 1 w, 3 w, 5 w๋กœ ์ด 6๊ฐœ์ด๋ฉฐ ํŽธํ–ฅ์˜ ๊ฐœ์ˆ˜๊ฐ€ 1 b๋กœ ๋‘ ๊ฐœ์ด๋ฏ€๋กœ ์ด ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜๋Š” 8๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์•ž์„œ model.summary()๋ฅผ ํ•˜์˜€์„ ๋•Œ ํ™•์ธํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜์ธ 8๊ฐœ์™€ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. 1 y๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์„ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1 x w + 2 2 x w + 1 2 x w + 2 5 x w + 2 [ 1 y ] s f m x ( [ 1 h ] ) ์ข€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์‹์„ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ 1 x, 3 ์„ ๋ฒกํ„ฐ๋กœ ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. = [ 1 x, 3 ] ๊ทธ๋ฆฌ๊ณ  1 w, 3 w, 5 w๋ฅผ ์›์†Œ๋กœ ํ•˜๋Š” 3 ร— 2 ํ–‰๋ ฌ์„ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ, ๊ทธ๋ฆฌ๊ณ  ํŽธํ–ฅ 1 2 ๋ฅผ ์›์†Œ๋กœ ํ•˜๋Š” ๋ฒกํ„ฐ๋ฅผ, ๊ทธ๋ฆฌ๊ณ  1 y๋ฅผ ์›์†Œ๋กœ ํ•˜๋Š” ์ถœ๋ ฅ ๋ฒกํ„ฐ๋ฅผ ๋กœ ๋ช…๋ช…ํ•ฉ์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ, ์œ„์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. = W B 3. ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ๋ณ‘๋ ฌ ์—ฐ์‚ฐ ์ดํ•ดํ•˜๊ธฐ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ๊ตฌํ˜„ํ•  ๋•Œ์˜ ํฅ๋ฏธ๋กœ์šด ์ ์€ ํ–‰๋ ฌ ๊ณฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ‘๋ ฌ ์—ฐ์‚ฐ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์ค‘ 1๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์Šต์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด 4๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•ด ๋ณธ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. 4๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ํ•˜๋‚˜์˜ ํ–‰๋ ฌ๋กœ ์ •์˜ํ•˜๊ณ  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ˆœ์ „ํŒŒ๋ฅผ ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ˜ผ๋™ํ•˜์ง€ ๋ง์•„์•ผ ํ•  ๊ฒƒ์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ 4๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜๋Š” ์—ฌ์ „ํžˆ 8๊ฐœ๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ๋‹ค์ˆ˜์˜ ์ƒ˜ํ”Œ์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ์šฐ๋ฆฌ๋Š” '๋ฐฐ์น˜ ์—ฐ์‚ฐ'์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋‚œ์ด๋„๋ฅผ ์˜ฌ๋ ค์„œ ์ค‘๊ฐ„์— ์ธต์„ ๋” ์ถ”๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. 4. ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ์ˆœ์ „ํŒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„์™€ ๊ฐ™์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ฃผ์–ด์ง„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ผ€๋ผ์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ณธ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1) ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential() # 4๊ฐœ์˜ ์ž…๋ ฅ๊ณผ 8๊ฐœ์˜ ์ถœ๋ ฅ model.add(Dense(8, input_dim=4, activation='relu')) # ์ด์–ด์„œ 8๊ฐœ์˜ ์ถœ๋ ฅ model.add(Dense(8, activation='relu')) # ์ด์–ด์„œ 3๊ฐœ์˜ ์ถœ๋ ฅ model.add(Dense(3, activation='softmax')) ์œ„์˜ ์ฝ”๋“œ์˜ ์ฃผ์„์—์„œ () ๊ด„ํ˜ธ ์•ˆ์˜ ๊ฐ’์€ ๊ฐ ์ธต์—์„œ์˜ ๋‰ด๋Ÿฐ์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ ์ž…๋ ฅ์ธต๋ถ€ํ„ฐ ์ถœ๋ ฅ์ธต๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ธต์„ ํ•œ ์ธต์”ฉ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ธต์„ ๋”ฅํ•˜๊ฒŒ ์Œ“์€ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ํ–‰๋ ฌ์˜ ํฌ๊ธฐ ์ถ”์ •ํ•ด ๋ณด๊ธฐ ์šฐ์„  ๊ฐ ์ธต์„ ๊ธฐ์ค€์œผ๋กœ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ์ธต : 4๊ฐœ์˜ ์ž…๋ ฅ๊ณผ 8๊ฐœ์˜ ์ถœ๋ ฅ ์€๋‹‰์ธต 1 : 8๊ฐœ์˜ ์ž…๋ ฅ๊ณผ 8๊ฐœ์˜ ์ถœ๋ ฅ ์€๋‹‰์ธต 2 : 8๊ฐœ์˜ ์ž…๋ ฅ๊ณผ 3๊ฐœ์˜ ์ถœ๋ ฅ ์ถœ๋ ฅ์ธต : 3๊ฐœ์˜ ์ž…๋ ฅ๊ณผ 3๊ฐœ์˜ ์ถœ๋ ฅ ์œ„์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์ธต๋งˆ๋‹ค ์ƒ๊ธฐ๋Š” ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ์ถ”์ •ํ•ด ๋ด…์‹œ๋‹ค. ๋‹จ, ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 1์„ ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. 1. ์ž…๋ ฅ์ธต โ‡’ ์€๋‹‰์ธต 1 ์•ž์„œ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ์„ค๋ช…ํ•˜๋ฉฐ ๋ฐฐ์šด ๋ฐ”์— ๋”ฐ๋ฅด๋ฉด, ์ž…๋ ฅ ํ–‰๋ ฌ, ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ, ํŽธํ–ฅ ํ–‰๋ ฌ, ์ถœ๋ ฅ ํ–‰๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํฌ๊ธฐ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. m ร— n W ร— j B ร— j Y ร— j layer 1์˜ ์ž…๋ ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” 1 ร— 4์ž…๋‹ˆ๋‹ค. layer 1์˜ ์ถœ๋ ฅ์€ 8๊ฐœ์ด๋ฏ€๋กœ, ๊ทธ์— ๋”ฐ๋ผ ์ถœ๋ ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” 1 ร— 8์ด ๋ฉ๋‹ˆ๋‹ค. 1 ร— 4 W ร— j B ร— j Y ร— 8 ๊ทธ๋Ÿฐ๋ฐ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์˜ ํ–‰์€ ์ž…๋ ฅ ํ–‰๋ ฌ์˜ ์—ด๊ณผ ๊ฐ™์•„์•ผ ํ•˜๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1 ร— 4 W ร— j B ร— j Y ร— 8 ํŽธํ–ฅ ํ–‰๋ ฌ ๋Š” ์ถœ๋ ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์œผ๋ฏ€๋กœ ํŽธํ–ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ์ถœ๋ ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1 ร— 4 W ร— j B ร— 8 Y ร— 8 ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์˜ ์—ด์€ ์ถœ๋ ฅ ํ–‰๋ ฌ์˜ ์—ด๊ณผ ๋™์ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1 4 W ร— + 1 8 Y ร— ์ž…๋ ฅ์ธต๊ณผ ์€๋‹‰์ธต 1 ์‚ฌ์ด์˜ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ ํŽธํ–ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. 2. ์€๋‹‰์ธต 1 โ‡’ ์€๋‹‰์ธต 2 ์ด์ œ ์ž…๋ ฅ์ธต โ‡’ ์€๋‹‰์ธต 1์—์„œ์˜ ์ถœ๋ ฅ ํ–‰๋ ฌ ๋Š” ์€๋‹‰์ธต 2์—์„œ๋Š” ์ž…๋ ฅ ํ–‰๋ ฌ๋กœ ๋‹ค์‹œ ๋ช…๋ช…ํ•ด ๋ด…์‹œ๋‹ค. ์€๋‹‰์ธต 1 โ‡’ ์€๋‹‰์ธต 2 : 1 8 W ร— + 1 8 Y ร— 3. ์€๋‹‰์ธต 2 โ‡’ ์€๋‹‰์ธต 3 ์ด์ œ ์€๋‹‰์ธต 2 โ‡’ ์€๋‹‰์ธต 3์—์„œ์˜ ์ถœ๋ ฅ ํ–‰๋ ฌ ๋Š” ์€๋‹‰์ธต 3์—์„œ๋Š” ์ž…๋ ฅ ํ–‰๋ ฌ๋กœ ๋‹ค์‹œ ๋ช…๋ช…ํ•ด ๋ด…์‹œ๋‹ค. ์€๋‹‰์ธต 2 โ‡’ ์€๋‹‰์ธต 3 : 1 8 W ร— + 1 3 Y ร— ์€๋‹‰์ธต๊ณผ ์ถœ๋ ฅ์ธต์— ํ™œ์„ฑํ™” ํ•จ์ˆ˜ relu์™€ softmax๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์ž…๋ ฅ์ธต์—์„œ ์€๋‹‰์ธต์„ ์ง€๋‚˜ ์ถœ๋ ฅ์ธต์—์„œ ์˜ˆ์ธก๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ๊นŒ์ง€์˜ ๊ณผ์ •์„ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ๊ฐ€์ •ํ•˜๊ณ  ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ์ถ”์ •ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์ˆœ์ „ํŒŒ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ์˜ˆ์ธก๊ฐ’์„ ๊ตฌํ•˜๊ณ  ๋‚˜์„œ ์ด๋‹ค์Œ์— ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ํ•ด์•ผ ํ•  ์ผ์€ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ์˜ค์ฐจ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ•™์Šต ๋‹จ๊ณ„์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ์ˆœ์ „ํŒŒ์™€๋Š” ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•˜๋ฉฐ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š”๋ฐ, ์ด ๊ณผ์ •์„ ์—ญ์ „ํŒŒ(BackPropagation)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์—ญ์ „ํŒŒ์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 07-04 ๋”ฅ ๋Ÿฌ๋‹์˜ ํ•™์Šต ๋ฐฉ๋ฒ• ๋”ฅ ๋Ÿฌ๋‹์˜ ํ•™์Šต ๋ฐฉ๋ฒ•์˜ ์ดํ•ด๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ ๊ฐœ๋…์ธ ์†์‹ค ํ•จ์ˆ˜, ์˜ตํ‹ฐ๋งˆ์ด์ €, ์—ํฌํฌ์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. 1. ์†์‹ค ํ•จ์ˆ˜(Loss function) ์†์‹ค ํ•จ์ˆ˜๋Š” ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์˜ ์ฐจ์ด๋ฅผ ์ˆ˜์น˜ํ™”ํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐ’์˜ ์ฐจ์ด. ์ฆ‰, ์˜ค์ฐจ๊ฐ€ ํด์ˆ˜๋ก ์†์‹ค ํ•จ์ˆ˜์˜ ๊ฐ’์€ ํฌ๊ณ  ์˜ค์ฐจ๊ฐ€ ์ž‘์„์ˆ˜๋ก ์†์‹ค ํ•จ์ˆ˜์˜ ๊ฐ’์€ ์ž‘์•„์ง‘๋‹ˆ๋‹ค. ํšŒ๊ท€์—์„œ๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ, ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ๋Š” ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ๋ฅผ ์ฃผ๋กœ ์†์‹ค ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์†์‹ค ํ•จ์ˆ˜์˜ ๊ฐ’์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ ๊ฐ€์ค‘์น˜ ์™€ ํŽธํ–ฅ์˜ ๊ฐ’์„ ์ฐพ๋Š” ๊ฒƒ์ด ๋”ฅ ๋Ÿฌ๋‹์˜ ํ•™์Šต ๊ณผ์ •์ด๋ฏ€๋กœ ์†์‹ค ํ•จ์ˆ˜์˜ ์„ ์ •์€ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์„ค๋ช…ํ–ˆ๋˜ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. 1) MSE(Mean Squared Error, MSE) ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋Š” ์„ ํ˜• ํšŒ๊ท€๋ฅผ ํ•™์Šตํ•  ๋•Œ ๋ฐฐ์› ๋˜ ์†์‹ค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์—ฐ์†ํ˜• ๋ณ€์ˆ˜๋ฅผ ์˜ˆ์ธกํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด compile์˜ loss์— ๋ฌธ์ž์—ด 'mse'๋ผ๊ณ  ๊ธฐ์žฌํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. model.compile(optimizer='adam', loss='mse', metrics=['mse']) compile์˜ loss๋Š” tf.keras.losses.Loss ์ธ์Šคํ„ด์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ ์œ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. model.compile(optimizer='adam', loss=tf.keras.losses.MeanSquaredError(), metrics=['mse']) ๋”ฅ ๋Ÿฌ๋‹ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋Š” ๋Œ€๋ถ€๋ถ„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ด๋ฏ€๋กœ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ณด๋‹ค๋Š” ์•„๋ž˜์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋“ค์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 2) ์ด์ง„ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ(Binary Cross-Entropy) ์ดํ•ญ ๊ต์ฐจ ์—”ํŠธ๋กœํ”ผ๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋Š” ์†์‹ค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ (Binary Classification)์˜ ๊ฒฝ์šฐ binary_crossentropy๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. compile์˜ loss์— ๋ฌธ์ž์—ด๋กœ 'binary_crossentropy'๋ฅผ ๊ธฐ์žฌํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ์†์‹ค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc']) compile์˜ loss๋Š” tf.keras.losses.Loss ์ธ์Šคํ„ด์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ ์œ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer='adam', metrics=['acc']) 3) ์นดํ…Œ๊ณ ๋ฆฌ์นผ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ(Categorical Cross-Entropy) ๋ฒ”์ฃผํ˜• ๊ต์ฐจ ์—”ํŠธ๋กœํ”ผ๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋Š” ์†์‹ค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์—์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-Class Classification)์ผ ๊ฒฝ์šฐ categorical_crossentropy๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. compile์˜ loss์— ๋ฌธ์ž์—ด๋กœ 'categorical_crossentropy'๋ฅผ ๊ธฐ์žฌํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ์†์‹ค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc']) compile์˜ loss๋Š” tf.keras.losses.Loss ์ธ์Šคํ„ด์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ ์œ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer='adam', metrics=['acc']) ๋งŒ์•ฝ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์„ ์ƒ๋žตํ•˜๊ณ , ์ •์ˆซ๊ฐ’์„ ๊ฐ€์ง„ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด 'sparse_categorical_crossentropy'๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc']) ์œ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), optimizer='adam', metrics=['acc']) 4) ๊ทธ ์™ธ์— ๋‹ค์–‘ํ•œ ์†์‹ค ํ•จ์ˆ˜๋“ค ์•„๋ž˜์˜ ํ…์„œ ํ”Œ๋กœ ๊ณต์‹ ๋ฌธ์„œ ๋งํฌ์—์„œ ๋ฐฉ๊ธˆ ์–ธ๊ธ‰ํ•˜์ง€ ์•Š์€ ์†์‹ค ํ•จ์ˆ˜ ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์†์‹ค ํ•จ์ˆ˜๋“ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. https://www.tensorflow.org/api_docs/python/tf/keras/losses ์ง€๊ธˆ๊นŒ์ง€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์†์‹ค ํ•จ์ˆ˜ ๋ช‡ ๊ฐ€์ง€์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. ์œ„ compile ์ฝ”๋“œ์—์„œ optimizer='adam'์ด๋ผ๋Š” ๋ถ€๋ถ„์— ์ฃผ๋ชฉํ•ด ๋ด…์‹œ๋‹ค. ์ด๋Š” ์•„๋‹ด์ด๋ผ๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์†์‹ค ํ•จ์ˆ˜์˜ ์„ ์ •๋งŒํผ์ด๋‚˜ ์˜ตํ‹ฐ๋งˆ์ด์ €์˜ ์„ ์ • ๋˜ํ•œ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด์–ด์„œ ์˜ตํ‹ฐ๋งˆ์ด์ €์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. 2. ๋ฐฐ์น˜ ํฌ๊ธฐ(Batch Size)์— ๋”ฐ๋ฅธ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ• ์†์‹ค ํ•จ์ˆ˜์˜ ๊ฐ’์„ ์ค„์—ฌ๋‚˜๊ฐ€๋ฉด์„œ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์–ด๋–ค ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ฐฐ์น˜(Batch)๋ผ๋Š” ๊ฐœ๋…์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜๋Š” ๊ฐ€์ค‘์น˜ ๋“ฑ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์กฐ์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์ •ํ•ด์ค€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ง€๊ณ ๋„ ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1) ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Batch Gradient Descent) ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Batch Gradient Descent)์€ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ค‘ ํ•˜๋‚˜๋กœ ์˜ค์ฐจ(loss)๋ฅผ ๊ตฌํ•  ๋•Œ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. ๋”ฅ ๋Ÿฌ๋‹์—์„œ๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ•œ ๋ฒˆ์˜ ํ›ˆ๋ จ ํšŸ์ˆ˜๋ฅผ 1 ์—ํฌํฌ๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ํ•œ ๋ฒˆ์˜ ์—ํฌํฌ์— ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜ ์—…๋ฐ์ดํŠธ๋ฅผ ๋‹จ ํ•œ ๋ฒˆ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ๋ คํ•ด์„œ ํ•™์Šตํ•˜๋ฏ€๋กœ ํ•œ ๋ฒˆ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์—…๋ฐ์ดํŠธ์— ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋ฉฐ, ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํฌ๊ฒŒ ์š”๊ตฌํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. model.fit(X_train, y_train, batch_size=len(X_train)) 2) ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1์ธ ํ™•๋ฅ ์  ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Stochastic Gradient Descent, SGD) ๊ธฐ์กด์˜ ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๊ณ„์‚ฐ์„ ํ•˜๋‹ค ๋ณด๋‹ˆ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1์ธ ํ™•๋ฅ ์  ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ ์กฐ์ • ์‹œ ์ „์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ ๋žœ๋ค์œผ๋กœ ์„ ํƒํ•œ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋งŒ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋” ์ ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋” ๋น ๋ฅด๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ์ขŒ์ธก์€ ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•, ์šฐ์ธก์€ ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1์ธ ํ™•๋ฅ ์  ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์ด ์ตœ์ ํ•ด๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ™•๋ฅ ์  ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋ณ€๊ฒฝํญ์ด ๋ถˆ์•ˆ์ •ํ•˜๊ณ , ๋•Œ๋กœ๋Š” ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•๋ณด๋‹ค ์ •ํ™•๋„๊ฐ€ ๋‚ฎ์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์ž์›์ด ์ ์€ ์ปดํ“จํ„ฐ์—์„œ๋„ ์‰ฝ๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. model.fit(X_train, y_train, batch_size=1) 3) ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Mini-Batch Gradient Descent) ์ „์ฒด ๋ฐ์ดํ„ฐ๋„, 1๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋„ ์•„๋‹ ๋•Œ, ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋งŒํผ์— ๋Œ€ํ•ด์„œ ๊ณ„์‚ฐํ•˜์—ฌ ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์กฐ์ •ํ•˜๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋น ๋ฅด๋ฉฐ, SGD๋ณด๋‹ค ์•ˆ์ •์ ์ด๋ผ๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์œผ๋กœ ์•ž์œผ๋กœ ์ด ์ฑ…์—์„œ๋„ ์ฃผ๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜์—ฌ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์œผ๋กœ ํ•™์Šตํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 128๋กœ ์ง€์ •ํ–ˆ์„ ๊ฒฝ์šฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. model.fit(X_train, y_train, batch_size=128) ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ 2์˜ n ์ œ๊ณฑ์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๋กœ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ๋ณดํŽธ์ ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, model.fit()์—์„œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ๋ณ„๋„๋กœ ์ง€์ •ํ•ด ์ฃผ์ง€ ์•Š์„ ๊ฒฝ์šฐ์— ๊ธฐ๋ณธ๊ฐ’์€ 2์˜ 5์ œ๊ณฑ์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž์ธ 32๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์น˜ ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ํ•™์Šต ๋ฐฉ๋ฒ•์˜ ์ฐจ์ด๋ฅผ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด๋ฅผ ์กฐ๊ธˆ์”ฉ ๋‹ฌ๋ฆฌํ•œ ๋‹ค์–‘ํ•œ ์˜ตํ‹ฐ๋งˆ์ด์ €์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 3. ์˜ตํ‹ฐ๋งˆ์ด์ €(Optimizer) 1) ๋ชจ๋ฉ˜ํ…€(Momentum) ๋ชจ๋ฉ˜ํ…€(Momentum)์€ ๊ด€์„ฑ์ด๋ผ๋Š” ๋ฌผ๋ฆฌํ•™์˜ ๋ฒ•์น™์„ ์‘์šฉํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ชจ๋ฉ˜ํ…€ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์— ๊ด€์„ฑ์„ ๋” ํ•ด์ค๋‹ˆ๋‹ค. ๋ชจ๋ฉ˜ํ…€์€ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์—์„œ ๊ณ„์‚ฐ๋œ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ์— ํ•œ ์‹œ์  ์ „์˜ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ ๊ฐ’์„ ์ผ์ •ํ•œ ๋น„์œจ๋งŒํผ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋งˆ์น˜ ์–ธ๋•์—์„œ ๊ณต์ด ๋‚ด๋ ค์˜ฌ ๋•Œ, ์ค‘๊ฐ„์— ์ž‘์€ ์›…๋ฉ์ด์— ๋น ์ง€๋”๋ผ๋„ ๊ด€์„ฑ์˜ ํž˜์œผ๋กœ ๋„˜์–ด์„œ๋Š” ํšจ๊ณผ๋ฅผ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ํ•จ์ˆ˜์— ๊ฑธ์ณ ์ตœ์†Ÿ๊ฐ’์„ ๊ธ€๋กœ๋ฒŒ ๋ฏธ๋‹ˆ๋ฉˆ(Global Minimum)์ด๋ผ๊ณ  ํ•˜๊ณ , ๊ธ€๋กœ๋ฒŒ ๋ฏธ๋‹ˆ๋ฉˆ์ด ์•„๋‹Œ ํŠน์ • ๊ตฌ์—ญ์—์„œ์˜ ์ตœ์†Ÿ๊ฐ’์ธ ๋กœ์ปฌ ๋ฏธ๋‹ˆ๋ฉˆ(Local Minimum)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋กœ์ปฌ ๋ฏธ๋‹ˆ๋ฉˆ์— ๋„๋‹ฌํ•˜์˜€์„ ๋•Œ ๊ธ€๋กœ๋ฒŒ ๋ฏธ๋‹ˆ๋ฉˆ์œผ๋กœ ์ž˜๋ชป ์ธ์‹ํ•˜์—ฌ ํƒˆ์ถœํ•˜์ง€ ๋ชปํ•˜์˜€์„ ์ƒํ™ฉ์—์„œ ๋ชจ๋ฉ˜ํ…€. ์ฆ‰, ๊ด€์„ฑ์˜ ํž˜์„ ๋นŒ๋ฆฌ๋ฉด ๊ฐ’์ด ์กฐ์ ˆ๋˜๋ฉด์„œ ํ˜„์žฌ์˜ ๋กœ์ปฌ ๋ฏธ๋‹ˆ๋ฉˆ์—์„œ ํƒˆ์ถœํ•˜๊ณ  ๊ธ€๋กœ๋ฒŒ ๋ฏธ๋‹ˆ๋ฉˆ ๋‚ด์ง€๋Š” ๋” ๋‚ฎ์€ ๋กœ์ปฌ ๋ฏธ๋‹ˆ๋ฉˆ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. tf.keras.optimizers.SGD(lr=0.01, momentum=0.9) 2) ์•„๋‹ค ๊ทธ๋ผ๋“œ(Adagrad) ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์€ ๊ฐ์ž ์˜๋ฏธํ•˜๋Š” ๋ฐ”๊ฐ€ ๋‹ค๋ฅธ๋ฐ, ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋™์ผํ•œ ํ•™์Šต๋ฅ (learning rate)์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ์•„๋‹ค ๊ทธ๋ผ๋“œ๋Š” ๊ฐ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์„œ๋กœ ๋‹ค๋ฅธ ํ•™์Šต๋ฅ ์„ ์ ์šฉ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋•Œ ๋ณ€ํ™”๊ฐ€ ๋งŽ์€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•™์Šต๋ฅ ์ด ์ž‘๊ฒŒ ์„ค์ •๋˜๊ณ  ๋ณ€ํ™”๊ฐ€ ์ ์€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•™์Šต๋ฅ ์„ ๋†’๊ฒŒ ์„ค์ •์‹œํ‚ต๋‹ˆ๋‹ค. tf.keras.optimizers.Adagrad(lr=0.01, epsilon=1e-6) 3) ์•Œ์— ์—์Šคํ”„๋กญ(RMSprop) ์•„๋‹ค ๊ทธ๋ผ๋“œ๋Š” ํ•™์Šต์„ ๊ณ„์† ์ง„ํ–‰ํ•œ ๊ฒฝ์šฐ์—๋Š”, ๋‚˜์ค‘์— ๊ฐ€์„œ๋Š” ํ•™์Šต๋ฅ ์ด ์ง€๋‚˜์น˜๊ฒŒ ๋–จ์–ด์ง„๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋Š”๋ฐ ์ด๋ฅผ ๋‹ค๋ฅธ ์ˆ˜์‹์œผ๋กœ ๋Œ€์ฒดํ•˜์—ฌ ์ด๋Ÿฌํ•œ ๋‹จ์ ์„ ๊ฐœ์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. tf.keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-06) 4) ์•„๋‹ด(Adam) ์•„๋‹ด์€ ์•Œ์— ์—์Šคํ”„๋กญ๊ณผ ๋ชจ๋ฉ˜ํ…€ ๋‘ ๊ฐ€์ง€๋ฅผ ํ•ฉ์นœ ๋“ฏํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ, ๋ฐฉํ–ฅ๊ณผ ํ•™์Šต๋ฅ  ๋‘ ๊ฐ€์ง€๋ฅผ ๋ชจ๋‘ ์žก๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. tf.keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) 5) ์‚ฌ์šฉ ๋ฐฉ๋ฒ• ๊ฐ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ธ์Šคํ„ด์Šค๋Š” compile์˜ optimizer์—์„œ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋‹ด(adam)์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. adam = tf.keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['acc']) ํ•˜์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹จ์ˆœํžˆ ๋ฌธ์ž์—ด๋กœ 'adam'์œผ๋กœ ์ž‘์„ฑํ•˜๋”๋ผ๋„ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc']) ๋‹ค๋ฅธ ์˜ตํ‹ฐ๋งˆ์ด์ €๋“ค๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. optimizer='sgd', optimizer='rmsprop'์™€ ๊ฐ™์ด ๊ฐ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ๋ฌธ์ž์—ด๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์˜ ์˜ตํ‹ฐ๋งˆ์ด์ € ์‚ฌ์šฉ๋ฒ•์€ ์•„๋ž˜์˜ ๋งํฌ์—์„œ ์ข€ ๋” ์ƒ์„ธํžˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://www.tensorflow.org/api_docs/python/tf/keras/optimizers 4. ์—ญ์ „ํŒŒ(BackPropagation) ์ด ๋ถ€๋ถ„์€ 05) ์—ญ์ „ํŒŒ ์ฑ•ํ„ฐ๋กœ ๋ณ„๋„ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 5. ์—ํฌํฌ์™€ ๋ฐฐ์น˜ ํฌ๊ธฐ์™€ ์ดํ„ฐ๋ ˆ์ด์…˜(Epochs and Batch size and Iteration) ๊ธฐ๊ณ„๋Š” ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์˜ ์˜ค์ฐจ๋กœ๋ถ€ํ„ฐ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ํ†ตํ•ด์„œ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ๋Š” ์ด ๊ณผ์ •์„ ํ•™์Šต์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ˜„์‹ค์˜ ํ•™์Šต์— ๋น„์œ ํ•˜๋ฉด ์‚ฌ๋žŒ์€ ๋ฌธ์ œ์ง€์˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ , ์ •๋‹ต์ง€์˜ ์ •๋‹ต์„ ๋ณด๋ฉด์„œ ์ฑ„์ ์„ ํ•˜๋ฉด์„œ ๋ถ€์กฑํ–ˆ๋˜ ์ ์„ ๊นจ๋‹ฌ์œผ๋ฉฐ ๋จธ๋ฆฟ์†์˜ ์ง€์‹์ด ์—…๋ฐ์ดํŠธ๋˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‚ฌ๋žŒ๋งˆ๋‹ค ๋™์ผํ•œ ๋ฌธ์ œ์ง€์™€ ์ •๋‹ต์ง€๋ฅผ ์ฃผ๋”๋ผ๋„ ๊ณต๋ถ€ ๋ฐฉ๋ฒ•์€ ์‚ฌ์‹ค ์ฒœ์ฐจ๋งŒ๋ณ„์ž…๋‹ˆ๋‹ค. ์–ด๋–ค ์‚ฌ๋žŒ์€ ๋ฌธ์ œ์ง€ ํ•˜๋‚˜๋ฅผ ๋‹ค ํ’€๊ณ  ๋‚˜์„œ ์ •๋‹ต์„ ์ฑ„์ ํ•˜๋Š”๋ฐ ์–ด๋–ค ์‚ฌ๋žŒ์€ ๋ฌธ์ œ์ง€์˜ ๋ฌธ์ œ๋ฅผ 10๊ฐœ ๋‹จ์œ„๋กœ ๋Š์–ด์„œ ๊ณต๋ถ€ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ 10๊ฐœ๋ฅผ ํ’€๊ณ  ์ฑ„์ ํ•˜๊ณ  ๋‹ค์‹œ ๋‹ค์Œ ๋ฌธ์ œ 10๊ฐœ๋ฅผ ํ’€๊ณ  ์ฑ„์ ํ•˜๊ณ  ๋ฐ˜๋ณตํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•™์Šตํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฒŒ์œผ๋ฅธ ์‚ฌ๋žŒ์€ ๋ฌธ์ œ์ง€๋ฅผ ์„ธ ๋ฒˆ ๊ณต๋ถ€ํ•˜๋Š”๋ฐ, ์„ฑ์‹คํ•œ ์‚ฌ๋žŒ์€ ๋ฌธ์ œ์ง€์˜ ๋ฌธ์ œ๋ฅผ ๋‹ฌ๋‹ฌ ์™ธ์šธ ๋งŒํผ ๋ฌธ์ œ์ง€๋ฅผ 100๋ฒˆ ๊ณต๋ถ€ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋„ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ๋ฌธ์ œ์ง€์™€ ์ •๋‹ต์ง€๋ฅผ ์ฃผ๋”๋ผ๋„ ๊ณต๋ถ€ ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ์—ํฌํฌ์™€ ๋ฐฐ์น˜ ํฌ๊ธฐ์™€ ์ดํ„ฐ๋ ˆ์ด์…˜์˜ ์ฐจ์ด๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 1) ์—ํฌํฌ(Epoch) ์—ํฌํฌ๋ž€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ˆœ์ „ํŒŒ์™€ ์—ญ์ „ํŒŒ๊ฐ€ ๋๋‚œ ์ƒํƒœ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ œ์ง€์— ๋น„์œ ํ•œ๋‹ค๋ฉด ๋ฌธ์ œ์ง€์˜ ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ๋๊นŒ์ง€ ๋‹ค ํ’€๊ณ , ์ •๋‹ต์ง€๋กœ ์ฑ„์ ์„ ํ•˜์—ฌ ๋ฌธ์ œ์ง€์— ๋Œ€ํ•œ ๊ณต๋ถ€๋ฅผ ํ•œ ๋ฒˆ ๋๋‚ธ ์ƒํƒœ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์—ํฌํฌ๊ฐ€ 50์ด๋ผ๊ณ  ํ•˜๋ฉด, ์ „์ฒด ๋ฐ์ดํ„ฐ ๋‹จ์œ„๋กœ๋Š” ์ด 50๋ฒˆ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ์ง€์— ๋น„์œ ํ•˜๋ฉด ๋ฌธ์ œ์ง€๋ฅผ 50๋ฒˆ ํ‘ผ ์…ˆ์ž…๋‹ˆ๋‹ค. ์ด ์—ํฌํฌ ํšŸ์ˆ˜๊ฐ€ ์ง€๋‚˜์น˜๊ฑฐ๋‚˜ ๋„ˆ๋ฌด ์ ์œผ๋ฉด ์•ž์„œ ๋ฐฐ์šด ๊ณผ์ ํ•ฉ๊ณผ ๊ณผ์†Œ ์ ํ•ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ๋ฐฐ์น˜ ํฌ๊ธฐ(Batch size) ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” ๋ช‡ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ๋‹จ์œ„๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š”์ง€๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ํ˜„์‹ค์— ๋น„์œ ํ•˜๋ฉด ๋ฌธ์ œ์ง€์—์„œ ๋ช‡ ๊ฐœ์”ฉ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๋‚˜์„œ ์ •๋‹ต์ง€๋ฅผ ํ™•์ธํ•˜๋Š๋ƒ์˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์€ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ์ •๋‹ต์„ ๋ณด๋Š” ์ˆœ๊ฐ„ ๋ถ€์กฑํ–ˆ๋˜ ์ ์„ ๊นจ๋‹ฌ์œผ๋ฉฐ ์ง€์‹์ด ์—…๋ฐ์ดํŠธ๋œ๋‹ค๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ ์ž…์žฅ์—์„œ๋Š” ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ํฌ์ธํŠธ๋Š” ์—…๋ฐ์ดํŠธ๊ฐ€ ์‹œ์ž‘๋˜๋Š” ์‹œ์ ์ด ์ •๋‹ต์ง€/์‹ค์ œ ๊ฐ’์„ ํ™•์ธํ•˜๋Š” ์‹œ์ ์ด๋ผ๋Š” ๊ฒ๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์ด 2,000 ๋ฌธ์ œ๊ฐ€ ์ˆ˜๋ก๋˜์–ด ์žˆ๋Š” ๋ฌธ์ œ์ง€์˜ ๋ฌธ์ œ๋ฅผ 200๊ฐœ ๋‹จ์œ„๋กœ ํ’€๊ณ  ์ฑ„์ ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์ด๋•Œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 200์ž…๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 200์ด๋ฉด 200๊ฐœ์˜ ์ƒ˜ํ”Œ ๋‹จ์œ„๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ ๋ฐฐ์น˜ ํฌ๊ธฐ์™€ ๋ฐฐ์น˜์˜ ์ˆ˜๋Š” ๋‹ค๋ฅธ ๊ฐœ๋…์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ 2,000์ผ ๋•Œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 200์œผ๋กœ ์ค€๋‹ค๋ฉด ๋ฐฐ์น˜์˜ ์ˆ˜๋Š” 10์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์—ํฌํฌ์—์„œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ๋‚˜๋ˆ ์ค€ ๊ฐ’(2,000/200 = 10)์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋ฐฐ์น˜์˜ ์ˆ˜๋ฅผ ์ดํ„ฐ๋ ˆ์ด์…˜์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3) ์ดํ„ฐ๋ ˆ์ด์…˜(Iteration) ๋˜๋Š” ์Šคํ…(Step) ์ดํ„ฐ๋ ˆ์ด์…˜์ด๋ž€ ํ•œ ๋ฒˆ์˜ ์—ํฌํฌ๋ฅผ ๋๋‚ด๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ๋ฐฐ์น˜์˜ ์ˆ˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ํ•œ ๋ฒˆ์˜ ์—ํฌํฌ ๋‚ด์—์„œ ์ด๋ฃจ์–ด์ง€๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์—…๋ฐ์ดํŠธ ํšŸ์ˆ˜์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ 2,000์ผ ๋•Œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 200์œผ๋กœ ํ•œ๋‹ค๋ฉด ์ดํ„ฐ๋ ˆ์ด์…˜์˜ ์ˆ˜๋Š” ์ด 10์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ•œ ๋ฒˆ์˜ ์—ํฌํฌ ๋‹น ๋งค๊ฐœ๋ณ€์ˆ˜ ์—…๋ฐ์ดํŠธ๊ฐ€ 10๋ฒˆ ์ด๋ฃจ์–ด์ง„๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1์ธ ํ™•๋ฅ ์  ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ด ๊ฐœ๋…์„ ๊ฐ€์ง€๊ณ  ๋‹ค์‹œ ์„ค๋ช…ํ•˜๋ฉด ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1์ด๋ฏ€๋กœ ๋ชจ๋“  ์ดํ„ฐ๋ ˆ์ด์…˜๋งˆ๋‹ค ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•˜์—ฌ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ดํ„ฐ๋ ˆ์ด์…˜์€ ์Šคํ…(Step)์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•˜๋ฏ€๋กœ ๋‘ ์šฉ์–ด ๋ชจ๋‘ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. 07-05 ์—ญ์ „ํŒŒ(BackPropagation) ์ดํ•ดํ•˜๊ธฐ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์ˆœ์ „ํŒŒ ๊ณผ์ •์„ ์ง„ํ–‰ํ•˜์—ฌ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜์˜€์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š”์ง€ ์ง์ ‘ ๊ณ„์‚ฐ์„ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 1. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ดํ•ด(Neural Network Overview) ์šฐ์„  ์˜ˆ์ œ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์—ญ์ „ํŒŒ์˜ ์ดํ•ด๋ฅผ ์œ„ํ•ด์„œ ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ์ž…๋ ฅ์ธต, ์€๋‹‰์ธต, ์ถœ๋ ฅ์ธต ์ด๋ ‡๊ฒŒ 3๊ฐœ์˜ ์ธต์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•ด๋‹น ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ณผ, ๋‘ ๊ฐœ์˜ ์€๋‹‰์ธต ๋‰ด๋Ÿฐ, ๋‘ ๊ฐœ์˜ ์ถœ๋ ฅ์ธต ๋‰ด๋Ÿฐ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต๊ณผ ์ถœ๋ ฅ์ธต์˜ ๋ชจ๋“  ๋‰ด๋Ÿฐ์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์€๋‹‰์ธต๊ณผ ์ถœ๋ ฅ์ธต์˜ ๋ชจ๋“  ๋‰ด๋Ÿฐ์—์„œ ๋ณ€์ˆ˜ ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ๋ณ€์ˆ˜๋Š” ์ด์ „์ธต์˜ ๋ชจ๋“  ์ž…๋ ฅ์ด ๊ฐ๊ฐ์˜ ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ง„ ๊ฐ’๋“ค์ด ๋ชจ๋‘ ๋”ํ•ด์ง„ ๊ฐ€์ค‘ํ•ฉ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ ๋‰ด๋Ÿฐ์—์„œ ์•„์ง ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š์€ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์šฐ์ธก์˜ |๋ฅผ ์ง€๋‚˜์„œ ์กด์žฌํ•˜๋Š” ๋ณ€์ˆ˜ ๋˜๋Š” ๋Š” ๊ฐ€ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ํ›„์˜ ๊ฐ’์œผ๋กœ ๊ฐ ๋‰ด๋Ÿฐ์˜ ์ถœ๋ ฅ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์—ญ์ „ํŒŒ ์˜ˆ์ œ์—์„œ๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ ์—ญ์ „ํŒŒ๋ฅผ ํ†ตํ•ด ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ํŽธํ–ฅ ๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 2. ์ˆœ์ „ํŒŒ(Forward Propagation) ์ฃผ์–ด์ง„ ๊ฐ’์ด ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์„ ๋•Œ ์ˆœ์ „ํŒŒ๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์†Œ์ˆ˜์  ์•ž์˜ 0์€ ์ƒ๋žตํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด. 25๋Š” 0.25๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํŒŒ๋ž€์ƒ‰ ์ˆซ์ž๋Š” ์ž…๋ ฅ๊ฐ’์„ ์˜๋ฏธํ•˜๋ฉฐ, ๋นจ๊ฐ„์ƒ‰ ์ˆซ์ž๋Š” ๊ฐ ๊ฐ€์ค‘์น˜์˜ ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ง„ํ–‰ํ•˜๋Š” ๊ณ„์‚ฐ์˜ ๊ฒฐ๊ด๊ฐ’์€ ์†Œ์ˆ˜์  ์•„๋ž˜ ์—ฌ๋Ÿ ๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž…๋ ฅ์€ ์ž…๋ ฅ์ธต์—์„œ ์€๋‹‰์ธต ๋ฐฉํ–ฅ์œผ๋กœ ํ–ฅํ•˜๋ฉด์„œ ๊ฐ ์ž…๋ ฅ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ง€๊ณ , ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ฐ€์ค‘ ํ•ฉ์œผ๋กœ ๊ณ„์‚ฐ๋˜์–ด ์€๋‹‰์ธต ๋‰ด๋Ÿฐ์˜ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. 1 z๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ๊ฐ์˜ ๊ฐ’์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 1 w x + 2 2 0.3 0.1 0.25 0.2 0.08 2 w x + 4 2 0.4 0.1 0.35 0.2 0.11 1 z๋Š” ๊ฐ๊ฐ์˜ ์€๋‹‰์ธต ๋‰ด๋Ÿฐ์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ฒŒ ๋˜๋Š”๋ฐ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒฐ๊ด๊ฐ’์€ ์€๋‹‰์ธต ๋‰ด๋Ÿฐ์˜ ์ตœ์ข… ์ถœ๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค. ์‹์—์„œ๋Š” ๊ฐ๊ฐ 1 h์— ํ•ด๋‹น๋˜๋ฉฐ, ์•„๋ž˜์˜ ๊ฒฐ๊ณผ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1 s g o d ( 1 ) 0.51998934 2 s g o d ( 2 ) 0.52747230 1 h ์ด ๋‘ ๊ฐ’์€ ๋‹ค์‹œ ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ์œผ๋กœ ํ–ฅํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋•Œ ๋‹ค์‹œ ๊ฐ๊ฐ์˜ ๊ฐ’์— ํ•ด๋‹น๋˜๋Š” ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ง€๊ณ , ๋‹ค์‹œ ๊ฐ€์ค‘ ํ•ฉ ๋˜์–ด ์ถœ๋ ฅ์ธต ๋‰ด๋Ÿฐ์˜ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ์‹์—์„œ๋Š” ๊ฐ๊ฐ 3 z์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 3 w h + 6 2 0.45 h + 0.4 h = 0.44498412 4 w h + 8 2 0.7 h + 0.6 h = 0.68047592 3 z ์ด ์ถœ๋ ฅ์ธต ๋‰ด๋Ÿฐ์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ๊ฐ’์€ ์ด ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์ตœ์ข…์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ ์ถœ๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ’์œผ๋กœ์„œ ์˜ˆ์ธก๊ฐ’์ด๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 1 s g o d ( 3 ) 0.60944600 2 s g o d ( 4 ) 0.66384491 ์ด์ œ ํ•ด์•ผ ํ•  ์ผ์€ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ์˜ค์ฐจ ํ•จ์ˆ˜๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ค์ฐจ(Error)๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ์†์‹ค ํ•จ์ˆ˜(Loss function)๋กœ๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ MSE๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‹์—์„œ๋Š” ์‹ค์ œ ๊ฐ’์„ target์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜์˜€์œผ๋ฉฐ, ์ˆœ์ „ํŒŒ๋ฅผ ํ†ตํ•ด ๋‚˜์˜จ ์˜ˆ์ธก๊ฐ’์„ output์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์˜ค์ฐจ๋ฅผ ๋ชจ๋‘ ๋”ํ•˜๋ฉด ์ „์ฒด ์˜ค์ฐจ t t l ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. o = 2 ( a g t 1 o t u o) = 0.02193381 o = 2 ( a g t 2 o t u o) = 0.00203809 t t l E 1 E 2 0.02397190 3. ์—ญ์ „ํŒŒ 1๋‹จ๊ณ„(BackPropagation Step 1) ์ˆœ์ „ํŒŒ๊ฐ€ ์ž…๋ ฅ์ธต์—์„œ ์ถœ๋ ฅ์ธต์œผ๋กœ ํ–ฅํ•œ๋‹ค๋ฉด ์—ญ์ „ํŒŒ๋Š” ๋ฐ˜๋Œ€๋กœ ์ถœ๋ ฅ์ธต์—์„œ ์ž…๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ๊ณ„์‚ฐํ•˜๋ฉด์„œ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ด๊ฐ‘๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต ๋ฐ”๋กœ ์ด์ „์˜ ์€๋‹‰์ธต์„ N ์ธต์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ถœ๋ ฅ์ธต๊ณผ N ์ธต ์‚ฌ์ด์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋‹จ๊ณ„๋ฅผ ์—ญ์ „ํŒŒ 1๋‹จ๊ณ„, ๊ทธ๋ฆฌ๊ณ  N ์ธต๊ณผ N ์ธต์˜ ์ด์ „์ธต ์‚ฌ์ด์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋‹จ๊ณ„๋ฅผ ์—ญ์ „ํŒŒ 2๋‹จ๊ณ„๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์—ญ์ „ํŒŒ 1๋‹จ๊ณ„์—์„œ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•  ๊ฐ€์ค‘์น˜๋Š” 5 w, 7 w ์ด 4๊ฐœ์ž…๋‹ˆ๋‹ค. ์›๋ฆฌ ์ž์ฒด๋Š” ๋™์ผํ•˜๋ฏ€๋กœ ์šฐ์„  5 ์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์—…๋ฐ์ดํŠธ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๊ฐ€์ค‘์น˜ 5 ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด์„œ E o a โˆ‚ 5 ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. E o a โˆ‚ 5 ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฏธ๋ถ„์˜ ์—ฐ์‡„ ๋ฒ•์น™(Chain rule)์— ๋”ฐ๋ผ์„œ ์ด์™€ ๊ฐ™์ด ํ’€์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 5 โˆ‚ t t l o ร— o โˆ‚ 3 โˆ‚ 3 w ์œ„์˜ ์‹์—์„œ ์šฐ๋ณ€์˜ ์„ธ ๊ฐœ์˜ ๊ฐ ํ•ญ์— ๋Œ€ํ•ด์„œ ์ˆœ์„œ๋Œ€๋กœ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ์ฒซ ๋ฒˆ์งธ ํ•ญ์— ๋Œ€ํ•ด์„œ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฏธ๋ถ„์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— t t l ์˜ ๊ฐ’์„ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. t t l ์€ ์•ž์„œ ์ˆœ์ „ํŒŒ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ๊ณ„์‚ฐํ–ˆ๋˜ ์ „์ฒด ์˜ค์ฐจ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. t t l 1 ( a g t 1 o t u o) + 2 ( a g t 2 o t u o) ์ด์— E o a โˆ‚ 1 ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 1 2 1 ( a g t 1 o t u o) โˆ’ ร— ( 1 ) 0 E o a โˆ‚ 1 โˆ’ ( a g t 1 o t u o) โˆ’ ( 0.4 0.60944600 ) 0.20944600 ์ด์ œ ๋‘ ๋ฒˆ์งธ ํ•ญ์„ ์ฃผ๋ชฉํ•ด ๋ด…์‹œ๋‹ค. 1 ์ด๋ผ๋Š” ๊ฐ’์€ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ๋ฏธ๋ถ„์€ ( ) ( โˆ’ ( ) ) ์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ๊ณ„์‚ฐ ๊ณผ์ •์—์„œ๋„ ๊ณ„์†ํ•ด์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์ƒ๊ธฐ๋ฏ€๋กœ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ์ด์— ๋”ฐ๋ผ์„œ ๋‘ ๋ฒˆ์งธ ํ•ญ์˜ ๋ฏธ๋ถ„ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜ ๋ฏธ๋ถ„ ์ฐธ๊ณ  ๋งํฌ : https://en.wikipedia.org/wiki/Logistic_function#Derivative) o โˆ‚ 3 o ร— ( โˆ’ 1 ) 0.60944600 ( โˆ’ 0.60944600 ) 0.23802157 ๋งˆ์ง€๋ง‰์œผ๋กœ ์„ธ ๋ฒˆ์งธ ํ•ญ์€ 1 ์˜ ๊ฐ’๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. z โˆ‚ 5 h = 0.51998934 ์šฐ๋ณ€์˜ ๋ชจ๋“  ํ•ญ์„ ๊ณ„์‚ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด ๊ฐ’์„ ๋ชจ๋‘ ๊ณฑํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. E o a โˆ‚ 5 0.20944600 0.23802157 0.51998934 0.02592286 ์ด์ œ ์•ž์„œ ๋ฐฐ์› ๋˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ํ†ตํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋•Œ๊ฐ€ ์™”์Šต๋‹ˆ๋‹ค! ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์— ํ•ด๋‹น๋˜๋Š” ํ•™์Šต๋ฅ (learning rate)๋Š” 0.5๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. 5 = 5 ฮฑ E o a โˆ‚ 5 0.45 0.5 0.02592286 0.43703857 ์ด์™€ ๊ฐ™์€ ์›๋ฆฌ๋กœ 6 , w + w + ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 6 โˆ‚ t t l o ร— o โˆ‚ 3 โˆ‚ 3 w โ†’ 6 = 0.38685205 E o a โˆ‚ 7 โˆ‚ t t l o ร— o โˆ‚ 4 โˆ‚ 4 w โ†’ 7 = 0.69629578 E o a โˆ‚ 8 โˆ‚ t t l o ร— o โˆ‚ 4 โˆ‚ 4 w โ†’ 8 = 0.59624247 4. ์—ญ์ „ํŒŒ 2๋‹จ๊ณ„(BackPropagation Step 2) 1๋‹จ๊ณ„๋ฅผ ์™„๋ฃŒํ•˜์˜€๋‹ค๋ฉด ์ด์ œ ์ž…๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๋ฉฐ ๋‹ค์‹œ ๊ณ„์‚ฐ์„ ์ด์–ด๊ฐ‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ๋นจ๊ฐ„์ƒ‰ ํ™”์‚ดํ‘œ๋Š” ์ˆœ์ „ํŒŒ์˜ ์ •๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์ธ ์—ญ์ „ํŒŒ์˜ ๋ฐฉํ–ฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ˜„์žฌ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ์€๋‹‰์ธต์ด 1๊ฐœ๋ฐ–์— ์—†์œผ๋ฏ€๋กœ ์ด๋ฒˆ ๋‹จ๊ณ„๊ฐ€ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์€๋‹‰์ธต์ด ๋” ๋งŽ์€ ๊ฒฝ์šฐ๋ผ๋ฉด ์ž…๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ๋‹จ๊ณ„์”ฉ ๊ณ„์†ํ•ด์„œ ๊ณ„์‚ฐํ•ด๊ฐ€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๋‹จ๊ณ„์—์„œ ๊ณ„์‚ฐํ•  ๊ฐ€์ค‘์น˜๋Š” 1 w, 3 w์ž…๋‹ˆ๋‹ค. ์›๋ฆฌ ์ž์ฒด๋Š” ๋™์ผํ•˜๋ฏ€๋กœ ์šฐ์„  1 ์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์—…๋ฐ์ดํŠธ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๊ฐ€์ค‘์น˜ 1 ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด์„œ E o a โˆ‚ 1 ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. E o a โˆ‚ 1 ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฏธ๋ถ„์˜ ์—ฐ์‡„ ๋ฒ•์น™(Chain rule)์— ๋”ฐ๋ผ์„œ ์ด์™€ ๊ฐ™์ด ํ’€์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 1 โˆ‚ t t l h ร— h โˆ‚ 1 โˆ‚ 1 w ์œ„์˜ ์‹์—์„œ ์šฐ๋ณ€์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ์ธ E o a โˆ‚ 1 ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹ค์‹œ ์‹์„ ํ’€์–ด์„œ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 1 โˆ‚ o โˆ‚ 1 โˆ‚ o โˆ‚ 1 ์œ„์˜ ์‹์˜ ์šฐ๋ณ€์˜ ๋‘ ํ•ญ์„ ๊ฐ๊ฐ ๊ตฌํ•ด๋ด…์‹œ๋‹ค. ์šฐ์„  ์ฒซ ๋ฒˆ์งธ ํ•ญ E 1 h์— ๋Œ€ํ•ด์„œ ํ•ญ์„ ๋ถ„ํ•ด ๋ฐ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. E 1 h = E 1 z ร— z โˆ‚ 1 โˆ‚ o โˆ‚ 1 โˆ‚ 1 z ร— z โˆ‚ 1 โˆ’ ( a g t 1 o t u o) o ร— ( โˆ’ 1 ) w = 0.20944600 0.23802157 0.45 0.02243370 ์ด์™€ ๊ฐ™์€ ์›๋ฆฌ๋กœ E 2 h ๋˜ํ•œ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. E 2 h = E 2 z ร— z โˆ‚ 1 โˆ‚ o โˆ‚ 2 โˆ‚ 2 z ร— z โˆ‚ 1 0.00997311 E o a โˆ‚ 1 0.02243370 0.00997311 0.03240681 ์ด์ œ E o a โˆ‚ 1 ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ์ฒซ ๋ฒˆ์งธ ํ•ญ์„ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€ ๋‘ ํ•ญ์— ๋Œ€ํ•ด์„œ ๊ตฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. h โˆ‚ 1 h ร— ( โˆ’ 1 ) 0.51998934 ( โˆ’ 0.51998934 ) 0.24960043 z โˆ‚ 1 x = 0.1 ์ฆ‰, E o a โˆ‚ 1 ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 1 0.03240681 0.24960043 0.1 0.00080888 ์ด์ œ ์•ž์„œ ๋ฐฐ์› ๋˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ํ†ตํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1 = 1 ฮฑ E o a โˆ‚ 1 0.3 0.5 0.00080888 0.29959556 ์ด์™€ ๊ฐ™์€ ์›๋ฆฌ๋กœ 2 , w + w + ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 2 โˆ‚ t t l h ร— h โˆ‚ 1 โˆ‚ 1 w โ†’ 2 = 0.24919112 E o a โˆ‚ 3 โˆ‚ t t l h ร— h โˆ‚ 2 โˆ‚ 2 w โ†’ 3 = 0.39964496 E o a โˆ‚ 4 โˆ‚ t t l h ร— h โˆ‚ 2 โˆ‚ 2 w โ†’ 4 = 0.34928991 5. ๊ฒฐ๊ณผ ํ™•์ธ ์—…๋ฐ์ดํŠธ๋œ ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ˆœ์ „ํŒŒ๋ฅผ ์ง„ํ–‰ํ•˜์—ฌ ์˜ค์ฐจ๊ฐ€ ๊ฐ์†Œํ•˜์˜€๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1 w x + 2 2 0.29959556 0.1 0.24919112 0.2 0.07979778 2 w x + 4 2 0.39964496 0.1 0.34928991 0.2 0.10982248 1 s g o d ( 1 ) 0.51993887 2 s g o d ( 2 ) 0.52742806 3 w h + 6 2 0.43703857 h + 0.38685205 h = 0.43126996 4 w h + 8 2 0.69629578 h + 0.59624247 h = 0.67650625 1 s g o d ( 3 ) 0.60617688 2 s g o d ( 4 ) 0.66295848 o = 2 ( a g t 1 o t u o) = 0.02125445 o = 2 ( a g t 2 o t u o) = 0.00198189 t t l E 1 E 2 0.02323634 ๊ธฐ์กด์˜ ์ „์ฒด ์˜ค์ฐจ t t l ๊ฐ€ 0.02397190์˜€์œผ๋ฏ€๋กœ 1๋ฒˆ์˜ ์—ญ์ „ํŒŒ๋กœ ์˜ค์ฐจ๊ฐ€ ๊ฐ์†Œํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ•™์Šต์€ ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ์ฐพ๋Š” ๋ชฉ์ ์œผ๋กœ ์ˆœ์ „ํŒŒ์™€ ์—ญ์ „ํŒŒ๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. 07-06 ๊ณผ ์ ํ•ฉ(Overfitting)์„ ๋ง‰๋Š” ๋ฐฉ๋ฒ•๋“ค ํ•™์Šต ๋ฐ์ดํ„ฐ์— ๋ชจ๋ธ์ด ๊ณผ์ ํ•ฉ๋˜๋Š” ํ˜„์ƒ์€ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋–จ์–ดํŠธ๋ฆฌ๋Š” ์ฃผ์š” ์ด์Šˆ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ๊ณผ์ ํ•ฉ๋˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋Š” ๋†’์„์ง€๋ผ๋„, ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ. ์ฆ‰, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋‚˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆํ•„์š”ํ•  ์ •๋„๋กœ ๊ณผํ•˜๊ฒŒ ์•”๊ธฐํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ํฌํ•จ๋œ ๋…ธ์ด์ฆˆ๊นŒ์ง€ ํ•™์Šตํ•œ ์ƒํƒœ๋ผ๊ณ  ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ชจ๋ธ์˜ ๊ณผ์ ํ•ฉ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋…ผ์˜ํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ์ด ์ฑ…์€ ๋”ฅ ๋Ÿฌ๋‹์„ ๋‹ค๋ฃจ๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ณผ์ ํ•ฉ์„ ๋ง‰๋Š” ๋ฐฉ๋ฒ•์— ์ดˆ์ ์„ ๋‘ก๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๋Š˜๋ฆฌ๊ธฐ ๋ชจ๋ธ์€ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ ์„ ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ฐ์ดํ„ฐ์˜ ํŠน์ • ํŒจํ„ด์ด๋‚˜ ๋…ธ์ด์ฆˆ๊นŒ์ง€ ์‰ฝ๊ฒŒ ์•”๊ธฐํ•˜๊ธฐ ๋˜๋ฏ€๋กœ ๊ณผ์ ํ•ฉ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•  ํ™•๋ฅ ์ด ๋Š˜์–ด๋‚ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๋Š˜๋ฆด์ˆ˜๋ก ๋ชจ๋ธ์€ ๋ฐ์ดํ„ฐ์˜ ์ผ๋ฐ˜์ ์ธ ํŒจํ„ด์„ ํ•™์Šตํ•˜์—ฌ ๊ณผ์ ํ•ฉ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ ์„ ๊ฒฝ์šฐ์—๋Š” ์˜๋„์ ์œผ๋กœ ๊ธฐ์กด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ๊ธˆ์”ฉ ๋ณ€ํ˜•ํ•˜๊ณ  ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๋Š˜๋ฆฌ๊ธฐ๋„ ํ•˜๋Š”๋ฐ ์ด๋ฅผ ๋ฐ์ดํ„ฐ ์ฆ์‹ ๋˜๋Š” ์ฆ๊ฐ•(Data Augmentation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฐ์ดํ„ฐ ์ฆ์‹์ด ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ ์ด๋ฏธ์ง€๋ฅผ ๋Œ๋ฆฌ๊ฑฐ๋‚˜ ๋…ธ์ด์ฆˆ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์ผ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•˜๋Š” ๋“ฑ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ์‹์‹œํ‚ต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ๊ฐ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฒˆ์—ญ ํ›„ ์žฌ๋ฒˆ์—ญ์„ ํ†ตํ•ด ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์—ญ ๋ฒˆ์—ญ(Back Translation) ๋“ฑ์˜ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. 2. ๋ชจ๋ธ์˜ ๋ณต์žก๋„ ์ค„์ด๊ธฐ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋ณต์žก๋„๋Š” ์€๋‹‰์ธต(hidden layer)์˜ ์ˆ˜๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜ ๋“ฑ์œผ๋กœ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ๊ณผ์ ํ•ฉ ํ˜„์ƒ์ด ํฌ์ฐฉ๋˜์—ˆ์„ ๋•Œ, ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ ๊ฐ€์ง€ ์กฐ์น˜๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋ณต์žก๋„๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ๋Š” ๋ชจ๋ธ์— ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์˜ ์ˆ˜๋ฅผ ๋ชจ๋ธ์˜ ์ˆ˜์šฉ๋ ฅ(capacity)์ด๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 3. ๊ฐ€์ค‘์น˜ ๊ทœ์ œ(Regularization) ์ ์šฉํ•˜๊ธฐ ๋ณต์žกํ•œ ๋ชจ๋ธ์ด ๊ฐ„๋‹จํ•œ ๋ชจ๋ธ๋ณด๋‹ค ๊ณผ์ ํ•ฉ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ„๋‹จํ•œ ๋ชจ๋ธ์€ ์ ์€ ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋ณต์žกํ•œ ๋ชจ๋ธ์„ ์ข€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ€์ค‘์น˜ ๊ทœ์ œ(Regularization)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. L1 ๊ทœ์ œ : ๊ฐ€์ค‘์น˜ w๋“ค์˜ ์ ˆ๋Œ“๊ฐ’ ํ•ฉ๊ณ„๋ฅผ ๋น„์šฉ ํ•จ์ˆ˜์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. L1 ๋…ธ๋ฆ„์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. L2 ๊ทœ์ œ : ๋ชจ๋“  ๊ฐ€์ค‘์น˜ w๋“ค์˜ ์ œ๊ณฑํ•ฉ์„ ๋น„์šฉ ํ•จ์ˆ˜์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. L2 ๋…ธ๋ฆ„์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. L1 ๊ทœ์ œ๋Š” ๊ธฐ์กด์˜ ๋น„์šฉ ํ•จ์ˆ˜์— ๋ชจ๋“  ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ โˆฃ โˆฃ ๋ฅผ ๋” ํ•œ ๊ฐ’์„ ๋น„์šฉ ํ•จ์ˆ˜๋กœ ํ•˜๊ณ , L2 ๊ทœ์ œ๋Š” ๊ธฐ์กด์˜ ๋น„์šฉ ํ•จ์ˆ˜์— ๋ชจ๋“  ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ 2 w๋ฅผ ๋” ํ•œ ๊ฐ’์„ ๋น„์šฉ ํ•จ์ˆ˜๋กœ ํ•ฉ๋‹ˆ๋‹ค.๋Š” ๊ทœ์ œ์˜ ๊ฐ•๋„๋ฅผ ์ •ํ•˜๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. ๊ฐ€ ํฌ๋‹ค๋ฉด ๋ชจ๋ธ์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ ํ•ฉํ•œ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ๋ณด๋‹ค ๊ทœ์ œ๋ฅผ ์œ„ํ•ด ์ถ”๊ฐ€๋œ ํ•ญ๋“ค์„ ์ž‘๊ฒŒ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์„ ์šฐ์„ ํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ์‹ ๋ชจ๋‘ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ€์ค‘์น˜ w๋“ค์˜ ๊ฐ’์ด ์ž‘์•„์ ธ์•ผ ํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. L1 ๊ทœ์ œ๋กœ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. L1 ๊ทœ์ œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋น„์šฉ ํ•จ์ˆ˜๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๊ฒŒ ํ•˜๋Š” ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์ฐพ๋Š” ๋™์‹œ์— ๊ฐ€์ค‘์น˜๋“ค์˜ ์ ˆ๋Œ“๊ฐ’์˜ ํ•ฉ๋„ ์ตœ์†Œ๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด, ๊ฐ€์ค‘์น˜ w์˜ ๊ฐ’๋“ค์€ 0 ๋˜๋Š” 0์— ๊ฐ€๊นŒ์ด ์ž‘์•„์ ธ์•ผ ํ•˜๋ฏ€๋กœ ์–ด๋–ค ํŠน์„ฑ๋“ค์€ ๋ชจ๋ธ์„ ๋งŒ๋“ค ๋•Œ ๊ฑฐ์˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ( ) w x + 2 2 w x + 4 4 ๋ผ๋Š” ์ˆ˜์‹์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์— L1 ๊ทœ์ œ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋”๋‹ˆ, 3 ์˜ ๊ฐ’์ด 0์ด ๋˜์—ˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋Š” 3 ํŠน์„ฑ์€ ์‚ฌ์‹ค ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ์— ๋ณ„ ์˜ํ–ฅ์„ ์ฃผ์ง€ ๋ชปํ•˜๋Š” ํŠน์„ฑ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. L2 ๊ทœ์ œ๋Š” L1 ๊ทœ์ œ์™€๋Š” ๋‹ฌ๋ฆฌ ๊ฐ€์ค‘์น˜๋“ค์˜ ์ œ๊ณฑ์„ ์ตœ์†Œํ™”ํ•˜๋ฏ€๋กœ w์˜ ๊ฐ’์ด ์™„์ „ํžˆ 0์ด ๋˜๊ธฐ๋ณด๋‹ค๋Š” 0์— ๊ฐ€๊นŒ์›Œ์ง€๊ธฐ๋Š” ๊ฒฝํ–ฅ์„ ๋•๋‹ˆ๋‹ค. L1 ๊ทœ์ œ๋Š” ์–ด๋–ค ํŠน์„ฑ๋“ค์ด ๋ชจ๋ธ์— ์˜ํ–ฅ์„ ์ฃผ๊ณ  ์žˆ๋Š”์ง€๋ฅผ ์ •ํ™•ํžˆ ํŒ๋‹จํ•˜๊ณ ์ž ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ด๋Ÿฐ ํŒ๋‹จ์ด ํ•„์š” ์—†๋‹ค๋ฉด ๊ฒฝํ—˜์ ์œผ๋กœ๋Š” L2 ๊ทœ์ œ๊ฐ€ ๋” ์ž˜ ๋™์ž‘ํ•˜๋ฏ€๋กœ L2 ๊ทœ์ œ๋ฅผ ๋” ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ L2 ๊ทœ์ œ๋Š” ๊ฐ€์ค‘์น˜ ๊ฐ์‡ (weight decay)๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ฑ…์— ๋”ฐ๋ผ์„œ๋Š” Regularization๋ฅผ ์ •๊ทœํ™”๋กœ ๋ฒˆ์—ญํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ์ด๋Š” ์ •๊ทœํ™”(Normalization)์™€ ํ˜ผ๋™๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ทœ์ œ ๋˜๋Š” ์ •ํ˜•ํ™”๋ผ๋Š” ๋ฒˆ์—ญ์ด ๋ฐ”๋žŒ์งํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ ์ •๊ทœํ™”(Normalization)๋ผ๋Š” ์šฉ์–ด๊ฐ€ ์“ฐ์ด๋Š” ๊ธฐ๋ฒ•์œผ๋กœ๋Š” ๋˜ ๋ฐฐ์น˜ ์ •๊ทœํ™”, ์ธต ์ •๊ทœํ™” ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 4. ๋“œ๋กญ์•„์›ƒ(Dropout) ๋“œ๋กญ์•„์›ƒ์€ ํ•™์Šต ๊ณผ์ •์—์„œ ์‹ ๊ฒฝ๋ง์˜ ์ผ๋ถ€๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๋“œ๋กญ์•„์›ƒ ์ „๊ณผ ํ›„์˜ ์‹ ๊ฒฝ๋ง์„ ๋น„๊ตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋“œ๋กญ์•„์›ƒ์˜ ๋น„์œจ์„ 0.5๋กœ ํ•œ๋‹ค๋ฉด ํ•™์Šต ๊ณผ์ •๋งˆ๋‹ค ๋žœ๋ค์œผ๋กœ ์ ˆ๋ฐ˜์˜ ๋‰ด๋Ÿฐ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์ ˆ๋ฐ˜์˜ ๋‰ด๋Ÿฐ๋งŒ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋“œ๋กญ์•„์›ƒ์€ ์‹ ๊ฒฝ๋ง ํ•™์Šต ์‹œ์—๋งŒ ์‚ฌ์šฉํ•˜๊ณ , ์˜ˆ์ธก ์‹œ์—๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ํ•™์Šต ์‹œ์— ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ํŠน์ • ๋‰ด๋Ÿฐ ๋˜๋Š” ํŠน์ • ์กฐํ•ฉ์— ๋„ˆ๋ฌด ์˜์กด์ ์ด๊ฒŒ ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ด ์ฃผ๊ณ , ๋งค๋ฒˆ ๋žœ๋ค ์„ ํƒ์œผ๋กœ ๋‰ด๋Ÿฐ๋“ค์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ์‹ ๊ฒฝ๋ง๋“ค์„ ์•™์ƒ๋ธ” ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๋‚ด์–ด ๊ณผ์ ํ•ฉ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋“œ๋กญ์•„์›ƒ์„ ๋ชจ๋ธ์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dropout, Dense max_words = 10000 num_classes = 46 model = Sequential() model.add(Dense(256, input_shape=(max_words,), activation='relu')) model.add(Dropout(0.5)) # ๋“œ๋กญ์•„์›ƒ ์ถ”๊ฐ€. ๋น„์œจ์€ 50% model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) # ๋“œ๋กญ์•„์›ƒ ์ถ”๊ฐ€. ๋น„์œจ์€ 50% model.add(Dense(num_classes, activation='softmax')) 07-07 ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค(Gradient Vanishing)๊ณผ ํญ์ฃผ(Exploding) ๊นŠ์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ํ•™์Šตํ•˜๋‹ค ๋ณด๋ฉด ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ์ž…๋ ฅ์ธต์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ๊ธฐ์šธ๊ธฐ(Gradient)๊ฐ€ ์ ์ฐจ์ ์œผ๋กœ ์ž‘์•„์ง€๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ์ธต์— ๊ฐ€๊นŒ์šด ์ธต๋“ค์—์„œ ๊ฐ€์ค‘์น˜๋“ค์ด ์—…๋ฐ์ดํŠธ๊ฐ€ ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์œผ๋ฉด ๊ฒฐ๊ตญ ์ตœ์ ์˜ ๋ชจ๋ธ์„ ์ฐพ์„ ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค(Gradient Vanishing)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ ์ฐจ ์ปค์ง€๋”๋‹ˆ ๊ฐ€์ค‘์น˜๋“ค์ด ๋น„์ •์ƒ์ ์œผ๋กœ ํฐ ๊ฐ’์ด ๋˜๋ฉด์„œ ๊ฒฐ๊ตญ ๋ฐœ์‚ฐ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ธฐ์šธ๊ธฐ ํญ์ฃผ(Gradient Exploding)๋ผ๊ณ  ํ•˜๋ฉฐ, ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šธ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network, RNN)์—์„œ ์‰ฝ๊ฒŒ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋˜๋Š” ๊ธฐ์šธ๊ธฐ ํญ์ฃผ๋ฅผ ๋ง‰๋Š” ๋ฐฉ๋ฒ•๋“ค์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 1. ReLU์™€ ReLU์˜ ๋ณ€ํ˜•๋“ค ์•ž์—์„œ ๋ฐฐ์šด ๋‚ด์šฉ์„ ๊ฐ„๋‹จํžˆ ๋ณต์Šตํ•ด ๋ด…์‹œ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ž…๋ ฅ์˜ ์ ˆ๋Œ“๊ฐ’์ด ํด ๊ฒฝ์šฐ์— ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์ด 0 ๋˜๋Š” 1์— ์ˆ˜๋ ดํ•˜๋ฉด์„œ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์— ๊ฐ€๊นŒ์›Œ์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ์ „ํŒŒ ์‹œํ‚ฌ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ ์ฐจ ์‚ฌ๋ผ์ ธ์„œ ์ž…๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ์ œ๋Œ€๋กœ ์—ญ์ „ํŒŒ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค์„ ์™„ํ™”ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ์€๋‹‰์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ๋‚˜ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜ ๋Œ€์‹ ์— ReLU๋‚˜ ReLU์˜ ๋ณ€ํ˜• ํ•จ์ˆ˜์™€ ๊ฐ™์€ Leaky ReLU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์€๋‹‰์ธต์—์„œ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. Leaky ReLU๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ์ž…๋ ฅ๊ฐ’์— ๋Œ€ํ•ด์„œ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์— ์ˆ˜๋ ดํ•˜์ง€ ์•Š์•„ ์ฃฝ์€ ReLU ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต์—์„œ๋Š” ReLU๋‚˜ Leaky ReLU์™€ ๊ฐ™์€ ReLU ํ•จ์ˆ˜์˜ ๋ณ€ํ˜•๋“ค์„ ์‚ฌ์šฉํ•˜์„ธ์š”. 2. ๊ทธ๋ž˜๋”” ์–ธํŠธ ํด๋ฆฌํ•‘(Gradient Clipping) ๊ทธ๋ž˜๋”” ์–ธํŠธ ํด๋ฆฌํ•‘์€ ๋ง ๊ทธ๋Œ€๋กœ ๊ธฐ์šธ๊ธฐ ๊ฐ’์„ ์ž๋ฅด๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ ํญ์ฃผ๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ์ž„๊ณ—๊ฐ’์„ ๋„˜์ง€ ์•Š๋„๋ก ๊ฐ’์„ ์ž๋ฆ…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ ์ž„๊ณ„์น˜๋งŒํผ ํฌ๊ธฐ๋ฅผ ๊ฐ์†Œ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋Š” ๋’ค์—์„œ ๋ฐฐ์šธ ์‹ ๊ฒฝ๋ง์ธ RNN์—์„œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. RNN์€ ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ์‹œ์ ์„ ์—ญํ–‰ํ•˜๋ฉด์„œ ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š”๋ฐ, ์ด๋•Œ ๊ธฐ์šธ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ์ปค์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ทธ๋ž˜๋”” ์–ธํŠธ ํด๋ฆฌํ•‘์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras import optimizers Adam = optimizers.Adam(lr=0.0001, clipnorm=1.) 3. ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™”(Weight initialization) ๊ฐ™์€ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ค๋”๋ผ๋„ ๊ฐ€์ค‘์น˜๊ฐ€ ์ดˆ๊ธฐ์— ์–ด๋–ค ๊ฐ’์„ ๊ฐ€์กŒ๋Š๋ƒ์— ๋”ฐ๋ผ์„œ ๋ชจ๋ธ์˜ ํ›ˆ๋ จ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™”๋งŒ ์ ์ ˆํžˆ ํ•ด์ค˜๋„ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋ฌธ์ œ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ์™„ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1) ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™”(Xavier Initialization) ๋…ผ๋ฌธ : http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf 2010๋…„ ์„ธ์ด๋น„์–ด ๊ธ€๋กœ๋Ÿฟ๊ณผ ์š”์Šˆ์•„ ๋ฒค ์ง€ ์˜ค๋Š” ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™”๊ฐ€ ๋ชจ๋ธ์— ๋ฏธ์น˜๋Š” ์˜ํ–ฅ์„ ๋ถ„์„ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์€ ์ œ์•ˆํ•œ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ์„ธ์ด๋น„์–ด(Xavier Initialization) ์ดˆ๊ธฐํ™” ๋˜๋Š” ๊ธ€๋กœ๋Ÿฟ ์ดˆ๊ธฐํ™”(Glorot Initialization)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ๊ท ๋“ฑ ๋ถ„ํฌ(Uniform Distribution) ๋˜๋Š” ์ •๊ทœ ๋ถ„ํฌ(Normal distribution)๋กœ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ๋‚˜๋‰˜๋ฉฐ, ์ด์ „ ์ธต์˜ ๋‰ด๋Ÿฐ ๊ฐœ์ˆ˜์™€ ๋‹ค์Œ ์ธต์˜ ๋‰ด๋Ÿฐ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์‹์„ ์„ธ์›๋‹ˆ๋‹ค. ์ด์ „ ์ธต์˜ ๋‰ด๋Ÿฐ์˜ ๊ฐœ์ˆ˜๋ฅผ i, ๋‹ค์Œ ์ธต์˜ ๋‰ด๋Ÿฐ์˜ ๊ฐœ์ˆ˜๋ฅผ o t ์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ธ€๋กœ๋Ÿฟ๊ณผ ๋ฒค ์ง€ ์˜ค์˜ ๋…ผ๋ฌธ์—์„œ๋Š” ๊ท ๋“ฑ ๋ถ„ํฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ท ๋“ฑ ๋ถ„ํฌ ๋ฒ”์œ„๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. โˆผ n f r ( 6 i + o t + n n n u) ๋‹ค์‹œ ๋งํ•ด n n n u ๋ฅผ ์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, m + ์‚ฌ์ด์˜ ๊ท ๋“ฑ ๋ถ„ํฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ •๊ทœ ๋ถ„ํฌ๋กœ ์ดˆ๊ธฐํ™”ํ•  ๊ฒฝ์šฐ์—๋Š” ํ‰๊ท ์ด 0์ด๊ณ , ํ‘œ์ค€ ํŽธ์ฐจ ฯƒ๊ฐ€ ๋‹ค์Œ์„ ๋งŒ์กฑํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. = n n n u ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™”๋Š” ์—ฌ๋Ÿฌ ์ธต์˜ ๊ธฐ์šธ๊ธฐ ๋ถ„์‚ฐ ์‚ฌ์ด์— ๊ท ํ˜•์„ ๋งž์ถฐ์„œ ํŠน์ • ์ธต์ด ๋„ˆ๋ฌด ์ฃผ๋ชฉ์„ ๋ฐ›๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์ธต์ด ๋’ค์ฒ˜์ง€๋Š” ๊ฒƒ์„ ๋ง‰์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™”๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋‚˜ ํ•˜์ดํผ๋ณผ๋ฆญ ํƒ„์  ํŠธ ํ•จ์ˆ˜์™€ ๊ฐ™์€ S์ž ํ˜•ํƒœ์ธ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์ด์ง€๋งŒ, ReLU์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์„ฑ๋Šฅ์ด ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ReLU ํ•จ์ˆ˜ ๋˜๋Š” ReLU์˜ ๋ณ€ํ˜• ํ•จ์ˆ˜๋“ค์„ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๋‹ค๋ฅธ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€๋ฐ, ์ด๋ฅผ He ์ดˆ๊ธฐํ™”(He initialization)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2) He ์ดˆ๊ธฐํ™”(He initialization) ๋…ผ๋ฌธ : https://www.cv-foundation.org/openaccess/content_iccv_2015/papers/He_Delving_Deep_into_ICCV_2015_paper.pdf He ์ดˆ๊ธฐํ™”(He initialization)๋Š” ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™”์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ •๊ทœ ๋ถ„ํฌ์™€ ๊ท ๋“ฑ ๋ถ„ํฌ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, He ์ดˆ๊ธฐํ™”๋Š” ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™”์™€ ๋‹ค๋ฅด๊ฒŒ ๋‹ค์Œ ์ธต์˜ ๋‰ด๋Ÿฐ์˜ ์ˆ˜๋ฅผ ๋ฐ˜์˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ „๊ณผ ๊ฐ™์ด ์ด์ „ ์ธต์˜ ๋‰ด๋Ÿฐ์˜ ๊ฐœ์ˆ˜๋ฅผ i์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. He ์ดˆ๊ธฐํ™”๋Š” ๊ท ๋“ฑ ๋ถ„ํฌ๋กœ ์ดˆ๊ธฐํ™”ํ•  ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ท ๋“ฑ ๋ถ„ํฌ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. โˆผ n f r ( 6 i , + n n ) ์ •๊ทœ ๋ถ„ํฌ๋กœ ์ดˆ๊ธฐํ™”ํ•  ๊ฒฝ์šฐ์—๋Š” ํ‘œ์ค€ ํŽธ์ฐจ ฯƒ๊ฐ€ ๋‹ค์Œ์„ ๋งŒ์กฑํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. = n n ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋‚˜ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์ด ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ReLU ๊ณ„์—ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” He ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์ด ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ReLU + He ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์ด ์ข€ ๋” ๋ณดํŽธ์ ์ž…๋‹ˆ๋‹ค. 4. ๋ฐฐ์น˜ ์ •๊ทœํ™”(Batch Normalization) ReLU ๊ณ„์—ด์˜ ํ•จ์ˆ˜์™€ He ์ดˆ๊ธฐํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ์–ด๋Š ์ •๋„ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค๊ณผ ํญ์ฃผ๋ฅผ ์™„ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด ๋‘ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ํ›ˆ๋ จ ์ค‘์— ์–ธ์ œ๋“  ๋‹ค์‹œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค์ด๋‚˜ ํญ์ฃผ๋ฅผ ์˜ˆ๋ฐฉํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ๋ฐฐ์น˜ ์ •๊ทœํ™”(Batch Normalization)์ž…๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ฐ ์ธต์— ๋“ค์–ด๊ฐ€๋Š” ์ž…๋ ฅ์„ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์œผ๋กœ ์ •๊ทœํ™”ํ•˜์—ฌ ํ•™์Šต์„ ํšจ์œจ์ ์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. 1) ๋‚ด๋ถ€ ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™”(Internal Covariate Shift) ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‚ด๋ถ€ ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™”(Internal Covariate Shift)๋ฅผ ์ดํ•ดํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™”๋ž€ ํ•™์Šต ๊ณผ์ •์—์„œ ์ธต ๋ณ„๋กœ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ๋ถ„ํฌ๊ฐ€ ๋‹ฌ๋ผ์ง€๋Š” ํ˜„์ƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์ธต๋“ค์˜ ํ•™์Šต์— ์˜ํ•ด ์ด์ „ ์ธต์˜ ๊ฐ€์ค‘์น˜ ๊ฐ’์ด ๋ฐ”๋€Œ๊ฒŒ ๋˜๋ฉด, ํ˜„์žฌ ์ธต์— ์ „๋‹ฌ๋˜๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ๊ฐ€ ํ˜„์žฌ ์ธต์ด ํ•™์Šตํ–ˆ๋˜ ์‹œ์ ์˜ ๋ถ„ํฌ์™€ ์ฐจ์ด๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ์—์„œ๋Š” ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค/ํญ์ฃผ ๋“ฑ์˜ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ๋ถˆ์•ˆ์ „์„ฑ์ด ์ธต๋งˆ๋‹ค ์ž…๋ ฅ์˜ ๋ถ„ํฌ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ์ฃผ์žฅํ•ฉ๋‹ˆ๋‹ค. (๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ์ฃผ์žฅํ–ˆ์ง€๋งŒ, ๋’ค์— ์ด์–ด์„œ๋Š” ์ด์— ๋Œ€ํ•œ ๋ฐ˜๋ฐ•๋“ค์ด ๋‚˜์˜ค๊ธฐ๋Š” ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ์ด์œ ๊ฐ€ ์–ด์ฐŒ ๋˜์—ˆ๋“  ๋ฐฐ์น˜ ์ •๊ทœํ™”๊ฐ€ ํ•™์Šต์„ ๋•๋Š”๋‹ค๋Š” ๊ฒƒ์€ ๋ช…๋ฐฑํ•ฉ๋‹ˆ๋‹ค.) ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™”๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™”๋Š” ์‹ ๊ฒฝ๋ง ์ธต ์‚ฌ์ด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ ๋ณ€ํ™”๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 2) ๋ฐฐ์น˜ ์ •๊ทœํ™”(Batch Normalization) ๋ฐฐ์น˜ ์ •๊ทœํ™”(Batch Normalization)๋Š” ํ‘œํ˜„ ๊ทธ๋Œ€๋กœ ํ•œ ๋ฒˆ์— ๋“ค์–ด์˜ค๋Š” ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ์ •๊ทœํ™”ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ๊ฐ ์ธต์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๊ธฐ ์ „์— ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์š”์•ฝํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ์— ๋Œ€ํ•ด ํ‰๊ท ์„ 0์œผ๋กœ ๋งŒ๋“ค๊ณ , ์ •๊ทœํ™”๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ •๊ทœํ™” ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์Šค์ผ€์ผ๊ณผ ์‹œํ”„ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ฮณ์™€ ฮฒ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ฮณ๋Š” ์Šค์ผ€์ผ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๊ณ , ฮฒ๋Š” ์‹œํ”„ํŠธ๋ฅผ ํ•˜๋Š” ๊ฒƒ์— ์‚ฌ์šฉํ•˜๋ฉฐ ๋‹ค์Œ ๋ ˆ์ด์–ด์— ์ผ์ •ํ•œ ๋ฒ”์œ„์˜ ๊ฐ’๋“ค๋งŒ ์ „๋‹ฌ๋˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”์˜ ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ N ์€ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Input : ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ = { ( ) x ( ) . . x ( ) } Output : ( ) B ฮณ ฮฒ ( ( ) ) ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ํ‰๊ท  ๊ณ„์‚ฐ B 1 โˆ‘ = m ( ) # ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ํ‰๊ท  ๊ณ„์‚ฐ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ๋ถ„์‚ฐ ๊ณ„์‚ฐ B โ† m i 1 ( ( ) ฮผ) # ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ๋ถ„์‚ฐ ๊ณ„์‚ฐ ์ •๊ทœํ™” ^ ( ) x ( ) ฮผ ฯƒ 2 ฮต # ์ •๊ทœํ™” ์Šค์ผ€์ผ ์กฐ์ •ฮณ๊ณผ ์‹œํ”„ํŠธฮฒ๋ฅผ ํ†ตํ•œ ์„ ํ˜•์—ฐ์‚ฐ ( ) ฮณ ^ ( ) ฮฒ B ฮณ ฮฒ ( ( ) ) # ์Šค์ผ€์ผ ์กฐ์ •(ฮณ)๊ณผ ์‹œํ”„ํŠธ(ฮฒ)๋ฅผ ํ†ตํ•œ ์„ ํ˜• ์—ฐ์‚ฐ ์€ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ์žˆ๋Š” ์ƒ˜ํ”Œ์˜ ์ˆ˜ B ๋Š” ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ํ‰๊ท . B ๋Š” ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ํ‘œ์ค€ํŽธ์ฐจ. ^ ( ) ์€ ํ‰๊ท ์ด 0์ด๊ณ  ์ •๊ทœํ™” ๋œ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ. ์€ 2 ๊ฐ€ 0์ผ ๋•Œ, ๋ถ„๋ชจ๊ฐ€ 0์ด ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๋Š” ์ž‘์€ ์–‘์ˆ˜. ๋ณดํŽธ์ ์œผ๋กœ 10 5๋Š” ์ •๊ทœํ™” ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์Šค์ผ€์ผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•™์Šต ๋Œ€์ƒ ๋Š” ์ •๊ทœํ™” ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์‹œํ”„ํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•™์Šต ๋Œ€์ƒ ( ) ๋Š” ์Šค์ผ€์ผ๊ณผ ์‹œํ”„ํŠธ๋ฅผ ํ†ตํ•ด ์กฐ์ •ํ•œ N ์˜ ์ตœ์ข… ๊ฒฐ๊ณผ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ํ•™์Šต ์‹œ ๋ฐฐ์น˜ ๋‹จ์œ„์˜ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ๋“ค์„ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐ›์•„ ์ด๋™ ํ‰๊ท ๊ณผ ์ด๋™ ๋ถ„์‚ฐ์„ ์ €์žฅํ•ด๋†“์•˜๋‹ค๊ฐ€ ํ…Œ์ŠคํŠธํ•  ๋•Œ๋Š” ํ•ด๋‹น ๋ฐฐ์น˜์˜ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์„ ๊ตฌํ•˜์ง€ ์•Š๊ณ  ๊ตฌํ•ด๋†“์•˜๋˜ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์œผ๋กœ ์ •๊ทœํ™”๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋‚˜ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋ฌธ์ œ๊ฐ€ ํฌ๊ฒŒ ๊ฐœ์„ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™”์— ํ›จ์”ฌ ๋œ ๋ฏผ๊ฐํ•ด์ง‘๋‹ˆ๋‹ค. ํ›จ์”ฌ ํฐ ํ•™์Šต๋ฅ ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด ํ•™์Šต ์†๋„๋ฅผ ๊ฐœ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋งˆ๋‹ค ํ‰๊ท ๊ณผ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์ผ์ข…์˜ ์žก์Œ ์ฃผ์ž…์˜ ๋ถ€์ˆ˜ ํšจ๊ณผ๋กœ ๊ณผ์ ํ•ฉ์„ ๋ฐฉ์ง€ํ•˜๋Š” ํšจ๊ณผ๋„ ๋ƒ…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ๋งˆ์น˜ ๋“œ๋กญ์•„์›ƒ๊ณผ ๋น„์Šทํ•œ ํšจ๊ณผ๋ฅผ ๋ƒ…๋‹ˆ๋‹ค. ๋ฌผ๋ก , ๋“œ๋กญ์•„์›ƒ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ๋ชจ๋ธ์„ ๋ณต์žกํ•˜๊ฒŒ ํ•˜๋ฉฐ, ์ถ”๊ฐ€ ๊ณ„์‚ฐ์„ ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก ์‹œ์— ์‹คํ–‰ ์‹œ๊ฐ„์ด ๋Š๋ ค์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์„œ๋น„์Šค ์†๋„๋ฅผ ๊ณ ๋ คํ•˜๋Š” ๊ด€์ ์—์„œ๋Š” ๋ฐฐ์น˜ ์ •๊ทœํ™”๊ฐ€ ๊ผญ ํ•„์š”ํ•œ์ง€ ๊ณ ๋ฏผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”์˜ ํšจ๊ณผ๋Š” ๊ต‰์žฅํ•˜์ง€๋งŒ ๋‚ด๋ถ€ ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™” ๋•Œ๋ฌธ์€ ์•„๋‹ˆ๋ผ๋Š” ๋…ผ๋ฌธ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. : https://arxiv.org/pdf/1805.11604.pdf 3) ๋ฐฐ์น˜ ์ •๊ทœํ™”์˜ ํ•œ๊ณ„ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ๋›ฐ์–ด๋‚œ ๋ฐฉ๋ฒ•์ด์ง€๋งŒ ๋ช‡ ๊ฐ€์ง€ ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ํฌ๊ธฐ์— ์˜์กด์ ์ด๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ๋„ˆ๋ฌด ์ž‘์€ ๋ฐฐ์น˜ ํฌ๊ธฐ์—์„œ๋Š” ์ž˜ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์ ์œผ๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 1๋กœ ํ•˜๊ฒŒ ๋˜๋ฉด ๋ถ„์‚ฐ์€ 0์ด ๋ฉ๋‹ˆ๋‹ค. ์ž‘์€ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์—์„œ๋Š” ๋ฐฐ์น˜ ์ •๊ทœํ™”์˜ ํšจ๊ณผ๊ฐ€ ๊ทน๋‹จ์ ์œผ๋กœ ์ž‘์šฉ๋˜์–ด ํ›ˆ๋ จ์— ์•…์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ ์šฉํ•  ๋•Œ๋Š” ์ž‘์€ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ณด๋‹ค๋Š” ํฌ๊ธฐ๊ฐ€ ์–ด๋Š ์ •๋„ ๋˜๋Š” ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์—์„œ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ๋ฐฐ์น˜ ํฌ๊ธฐ์— ์˜์กด์ ์ธ ๋ฉด์ด ์žˆ์Šต๋‹ˆ๋‹ค. 2. RNN์— ์ ์šฉํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒ ์ง€๋งŒ, RNN์€ ๊ฐ ์‹œ์ (time step)๋งˆ๋‹ค ๋‹ค๋ฅธ ํ†ต๊ณ„์น˜๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋Š” RNN์— ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. RNN์—์„œ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ๋…ผ๋ฌธ์ด ์ œ์‹œ๋˜์–ด ์žˆ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ์ด๋ฅผ ์†Œ๊ฐœํ•˜๋Š” ๋Œ€์‹  ๋ฐฐ์น˜ ํฌ๊ธฐ์—๋„ ์˜์กด์ ์ด์ง€ ์•Š์œผ๋ฉฐ, RNN์—๋„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ˆ˜์›”ํ•œ ์ธต ์ •๊ทœํ™”(layer normalization)๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. 5. ์ธต ์ •๊ทœํ™”(Layer Normalization) ์ธต ์ •๊ทœํ™”๋ฅผ ์ดํ•ดํ•˜๊ธฐ์— ์•ž์„œ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ด 3์ด๊ณ , ํŠน์„ฑ์˜ ์ˆ˜๊ฐ€ 4์ผ ๋•Œ์˜ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ž€ ๋™์ผํ•œ ํŠน์„ฑ(feature) ๊ฐœ์ˆ˜๋“ค์„ ๊ฐ€์ง„ ๋‹ค์ˆ˜์˜ ์ƒ˜ํ”Œ๋“ค์„ ์˜๋ฏธํ•จ์„ ์ƒ๊ธฐํ•ฉ์‹œ๋‹ค. ๋ฐ˜๋ฉด, ์ธต ์ •๊ทœํ™”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 07-08 ์ผ€๋ผ์Šค(Keras) ํ›‘์–ด๋ณด๊ธฐ ์ด ์ฑ…์—์„œ๋Š” ๋”ฅ ๋Ÿฌ๋‹์„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ ์ผ€๋ผ์Šค(Keras)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค๋Š” ์œ ์ €๊ฐ€ ์†์‰ฝ๊ฒŒ ๋”ฅ ๋Ÿฌ๋‹์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์ƒ์œ„ ๋ ˆ๋ฒจ์˜ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋”ฅ ๋Ÿฌ๋‹์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ๋“ค์„ ์—ด๊ฑฐํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ํ•œ ๊ถŒ์˜ ์ฑ…์˜ ๋ถ„๋Ÿ‰์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ ์ „๋ถ€ ๋‹ค๋ฃฐ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์ผ€๋ผ ์Šค๋‚˜ ํ…์„œ ํ”Œ๋กœ ๊ณต์‹ ๋ฌธ์„œ( https://keras.io/ or https://www.tensorflow.org/guide/keras? hl=ko)๋ฅผ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ผ€๋ผ์Šค์˜ ๋„๊ตฌ๋“ค์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 1. ์ „์ฒ˜๋ฆฌ(Preprocessing) Tokenizer() : ํ† ํฐํ™”์™€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•˜๊ณ , ํ•ด๋‹น ๋‹จ์–ด ์ง‘ํ•ฉ์œผ๋กœ ์ž„์˜์˜ ๋ฌธ์žฅ์„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences tokenizer = Tokenizer() train_text = "The earth is an awesome place live" # ๋‹จ์–ด ์ง‘ํ•ฉ ์ƒ์„ฑ tokenizer.fit_on_texts([train_text]) # ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ sub_text = "The earth is an great place live" sequences = tokenizer.texts_to_sequences([sub_text])[0] print("์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : ",sequences) print("๋‹จ์–ด ์ง‘ํ•ฉ : ",tokenizer.word_index) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [1, 2, 3, 4, 6, 7] ๋‹จ์–ด ์ง‘ํ•ฉ : {'the': 1, 'earth': 2, 'is': 3, 'an': 4, 'awesome': 5, 'place': 6, 'live': 7} ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด great๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์— ์—†์œผ๋ฏ€๋กœ ์ถœ๋ ฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. pad_sequence() : ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” ์„œ๋กœ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ๊ฐ ๋ฌธ์„œ ๋˜๋Š” ๊ฐ ๋ฌธ์žฅ์€ ๋‹จ์–ด์˜ ์ˆ˜๊ฐ€ ์ œ๊ฐ๊ฐ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งž์ถ”์–ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ๋Š” ํŒจ๋”ฉ(padding) ์ž‘์—…์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ๋ณดํ†ต ์ˆซ์ž 0์„ ๋„ฃ์–ด์„œ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋ฅผ ๋งž์ถฐ์ค๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์—์„œ๋Š” pad_sequence()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. pad_sequence()๋Š” ์ •ํ•ด์ค€ ๊ธธ์ด๋ณด๋‹ค ๊ธธ์ด๊ฐ€ ๊ธด ์ƒ˜ํ”Œ์€ ๊ฐ’์„ ์ผ๋ถ€ ์ž๋ฅด๊ณ , ์ •ํ•ด์ค€ ๊ธธ์ด๋ณด๋‹ค ๊ธธ์ด๊ฐ€ ์งง์€ ์ƒ˜ํ”Œ์€ ๊ฐ’์„ 0์œผ๋กœ ์ฑ„์›๋‹ˆ๋‹ค. pad_sequences([[1, 2, 3], [3, 4, 5, 6], [7, 8]], maxlen=3, padding='pre') array([[1, 2, 3], [4, 5, 6], [0, 7, 8]], dtype=int32) ์ฒซ ๋ฒˆ์งธ ์ธ์ž = ํŒจ๋”ฉ์„ ์ง„ํ–‰ํ•  ๋ฐ์ดํ„ฐ maxlen = ๋ชจ๋“  ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •๊ทœํ™” ํ•  ๊ธธ์ด padding = 'pre'๋ฅผ ์„ ํƒํ•˜๋ฉด ์•ž์— 0์„ ์ฑ„์šฐ๊ณ  'post'๋ฅผ ์„ ํƒํ•˜๋ฉด ๋’ค์— 0์„ ์ฑ„์›€. 2. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding) ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃจ๊ฒ ์ง€๋งŒ, ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด๋ž€ ํ…์ŠคํŠธ ๋‚ด์˜ ๋‹จ์–ด๋“ค์„ ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ๊ฐœ๋…์ธ ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋Œ€๋ถ€๋ถ„์ด 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ , ๋‹จ ํ•˜๋‚˜์˜ 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ์ด๋ฉฐ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋Œ€์ฒด์ ์œผ๋กœ ํฌ๋‹ค๋Š” ์„ฑ์งˆ์„ ๊ฐ€์กŒ์Šต๋‹ˆ๋‹ค. Ex) [0 1 0 0 0 0 ... ์ค‘๋žต ... 0 0 0 0 0 0 0] # ์ฐจ์›์ด ๊ต‰์žฅํžˆ ํฌ๋ฉด์„œ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ’์ด 0 ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒํผ ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋ฉฐ ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ„์˜ ์œ ์˜๋ฏธํ•œ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์œผ๋กœ๋ถ€ํ„ฐ ์–ป์€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ์ƒ๋Œ€์ ์œผ๋กœ ์ €์ฐจ์›์„ ๊ฐ€์ง€๋ฉฐ ๋ชจ๋“  ์›์†Œ์˜ ๊ฐ’์ด ์‹ค์ˆ˜์ž…๋‹ˆ๋‹ค. Ex) [0.1 -1.2 0.8 0.2 1.8] # ์ƒ๋Œ€์ ์œผ๋กœ ์ €์ฐจ์›์ด๋ฉฐ ์‹ค์ˆซ๊ฐ’์„ ๊ฐ€์ง ๊ฐ„๋‹จํžˆ ํ‘œ๋กœ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ----- ์›-ํ•ซ ๋ฒกํ„ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ์ฐจ์› ๊ณ ์ฐจ์›(๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ) ์ €์ฐจ์› ๋‹ค๋ฅธ ํ‘œํ˜„ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ’์ด 0์ด ๋Œ€๋ถ€๋ถ„์ธ ํฌ์†Œ ๋ฒกํ„ฐ ๋ชจ๋“  ๊ฐ’์ด ์‹ค์ˆ˜์ธ ๋ฐ€์ง‘ ๋ฒกํ„ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ• ์ˆ˜๋™ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ํ•™์Šตํ•จ ๊ฐ’์˜ ํƒ€์ž… 1๊ณผ 0 ์‹ค์ˆ˜ ๋‹จ์–ด๋ฅผ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š” ์ž‘์—…์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(word embedding)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๊ณผ์ •์„ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ฏ€๋กœ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(embedding vector)๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ์ฃผ๋กœ 20,000 ์ด์ƒ์„ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ์ฃผ๋กœ 256, 512, 1024 ๋“ฑ์˜ ์ฐจ์›์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ์ดˆ๊ธฐ์—๋Š” ๋žœ๋ค ๊ฐ’์„ ๊ฐ€์ง€์ง€๋งŒ, ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ํ•™์Šต๋˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ฐ’์ด ํ•™์Šต๋˜๋ฉฐ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. Embedding() : Embedding()์€ ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์šฉ์–ด๋กœ๋Š” ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์„ ๋งŒ๋“œ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. Embedding()์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋œ ๋‹จ์–ด๋“ค์„ ์ž…๋ ฅ์„ ๋ฐ›์•„์„œ ์ž„๋ฒ ๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. Embedding()์€ (number of samples, input_length)์ธ 2D ์ •์ˆ˜ ํ…์„œ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฐ sample์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋œ ๊ฒฐ๊ณผ๋กœ, ์ •์ˆ˜์˜ ์‹œํ€€์Šค์ž…๋‹ˆ๋‹ค. Embedding()์€ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  (number of samples, input_length, embedding word dimensionality)์ธ 3D ํ…์„œ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์‹ค์ œ ๋™์ž‘๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ์˜์‚ฌ ์ฝ”๋“œ(pseudo-code)๋กœ ์ž„๋ฒ ๋”ฉ์˜ ๊ฐœ๋… ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด์„œ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. # 1. ํ† ํฐํ™” tokenized_text = [['Hope', 'to', 'see', 'you', 'soon'], ['Nice', 'to', 'see', 'you', 'again']] # 2. ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ encoded_text = [[0, 1, 2, 3, 4],[5, 1, 2, 3, 6]] # 3. ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋ž˜์˜ ์ž„๋ฒ ๋”ฉ ์ธต์˜ ์ž…๋ ฅ์ด ๋œ๋‹ค. vocab_size = 7 embedding_dim = 2 Embedding(vocab_size, embedding_dim, input_length=5) # ๊ฐ ์ •์ˆ˜๋Š” ์•„๋ž˜์˜ ํ…Œ์ด๋ธ”์˜ ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ Embedding()์€ ๊ฐ ๋‹จ์–ด๋งˆ๋‹ค ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. +------------+------------+ | index | embedding | +------------+------------+ | 0 | [1.2, 3.1] | | 1 | [0.1, 4.2] | | 2 | [1.0, 3.1] | | 3 | [0.3, 2.1] | | 4 | [2.2, 1.4] | | 5 | [0.7, 1.7] | | 6 | [4.1, 2.0] | +------------+------------+ # ์œ„์˜ ํ‘œ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ๋กœ์„œ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๊ณ  Embedding()์˜ ์ถœ๋ ฅ์ธ 3D ํ…์„œ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹˜. Embedding()์˜ ๋Œ€ํ‘œ์ ์ธ ์ธ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž = ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ. ์ฆ‰, ์ด ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž = ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ถœ๋ ฅ ์ฐจ์›. ๊ฒฐ๊ณผ๋กœ์„œ ๋‚˜์˜ค๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ input_length = ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ธธ์ด 3. ๋ชจ๋ธ๋ง(Modeling) Sequential() : ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์ฑ•ํ„ฐ์—์„œ ์ž…๋ ฅ์ธต, ์€๋‹‰์ธต, ์ถœ๋ ฅ์ธต์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์—์„œ๋Š” ์ด๋Ÿฌํ•œ ์ธต์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด Sequential()์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Sequential()์„ model๋กœ ์„ ์–ธํ•œ ๋’ค์— model.add()๋ผ๋Š” ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์ธต์„ ๋‹จ๊ณ„์ ์œผ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” model.add()๋กœ ์ธต์„ ์ถ”๊ฐ€ํ•˜๋Š” ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋Š” ์„ธ ๊ฐœ์˜ ์˜จ์  ๋Œ€์‹ ์— ์ธต์˜ ์ด๋ฆ„์„ ๊ธฐ์žฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential() model.add(...) # ์ธต ์ถ”๊ฐ€ model.add(...) # ์ธต ์ถ”๊ฐ€ model.add(...) # ์ธต ์ถ”๊ฐ€ Embedding()์„ ํ†ตํ•ด ์ƒ์„ฑํ•˜๋Š” ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์„ ์ถ”๊ฐ€ํ•˜๋Š” ์˜ˆ์‹œ๋ฅผ ๋ด…์‹œ๋‹ค. model = Sequential() model.add(Embedding(vocab_size, output_dim, input_length)) ์ „๊ฒฐํ•ฉ์ธต(fully-connected layer)์„ ์ถ”๊ฐ€ํ•˜๋Š” ์˜ˆ์‹œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model = Sequential() model.add(Dense(1, input_dim=3, activation='relu')) ์œ„์˜ ์ฝ”๋“œ์—์„œ Dense()๋Š” ํ•œ๋ฒˆ ์‚ฌ์šฉ๋˜์—ˆ์ง€๋งŒ ๋” ๋งŽ์€ ์ธต์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Dense()์˜ ๋Œ€ํ‘œ์ ์ธ ์ธ์ž๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž = ์ถœ๋ ฅ ๋‰ด๋Ÿฐ์˜ ์ˆ˜. input_dim = ์ž…๋ ฅ ๋‰ด๋Ÿฐ์˜ ์ˆ˜. (์ž…๋ ฅ์˜ ์ฐจ์›) activation = ํ™œ์„ฑํ™” ํ•จ์ˆ˜. - linear : ๋””ํดํŠธ ๊ฐ’์œผ๋กœ ๋ณ„๋„ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์—†์ด ์ž…๋ ฅ ๋‰ด๋Ÿฐ๊ณผ ๊ฐ€์ค‘์น˜์˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ. - sigmoid : ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ ์ถœ๋ ฅ์ธต์— ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜. - softmax : ์…‹ ์ด์ƒ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ํƒํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ ์ถœ๋ ฅ์ธต์— ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜. - relu : ์€๋‹‰์ธต์— ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜. ์œ„ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉ๋œ Dense()์˜ ์˜๋ฏธ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž์˜ ๊ฐ’์€ 1์ธ๋ฐ ์ด๋Š” ์ด 1๊ฐœ์˜ ์ถœ๋ ฅ ๋‰ด๋Ÿฐ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Dense()์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ input_dim์€ ์ž…๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” 3์ž…๋‹ˆ๋‹ค. 3๊ฐœ์˜ ์ž…๋ ฅ์ธต ๋‰ด๋Ÿฐ๊ณผ 1๊ฐœ์˜ ์ถœ๋ ฅ์ธต ๋‰ด๋Ÿฐ์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹œ๊ฐํ™”ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Dense()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „๊ฒฐํ•ฉ์ธต์„ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model = Sequential() model.add(Dense(8, input_dim=4, activation='relu')) model.add(Dense(1, activation='sigmoid')) # ์ถœ๋ ฅ์ธต ์ด๋ฒˆ์—๋Š” Dense()๊ฐ€ ๋‘ ๋ฒˆ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Dense()๊ฐ€ ์ฒ˜์Œ ์‚ฌ์šฉ๋˜์—ˆ์„ ๋•Œ์™€ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉ๋˜์—ˆ์„ ๋•Œ์˜ ์ธ์ž๋Š” ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์‚ฌ์šฉ๋œ Dense()์˜ 8์ด๋ผ๋Š” ๊ฐ’์€ ๋” ์ด์ƒ ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ์ด ์•„๋‹ˆ๋ผ ์€๋‹‰์ธต์˜ ๋‰ด๋Ÿฐ์ž…๋‹ˆ๋‹ค. ์ธต์ด ํ•˜๋‚˜ ๋” ์ƒ๊ฒผ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ Dense()๋Š” input_dim ์ธ์ž๊ฐ€ ์—†๋Š”๋ฐ, ์ด๋Š” ์ด๋ฏธ ์ด์ „ ์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜๊ฐ€ 8๊ฐœ์ž„์„ ์•Œ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ ๋‘ ๋ฒˆ์งธ Dense()๋Š” ๋งˆ์ง€๋ง‰ ์ธต์ด๋ฏ€๋กœ, ์ฒซ ๋ฒˆ์งธ ์ธ์ž 1์€ ๊ฒฐ๊ตญ ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ ๊ฐœ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹œ๊ฐํ™”ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ์™ธ์—๋„ LSTM, GRU, Convolution2D, BatchNormalization ๋“ฑ ๋‹ค์–‘ํ•œ ์ธต์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ถ€๋Š” ๋’ค์—์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. summary() : ๋ชจ๋ธ์˜ ์ •๋ณด๋ฅผ ์š”์•ฝํ•ด์„œ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. model.summary() _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_1 (Dense) (None, 8) 40 _________________________________________________________________ dense_2 (Dense) (None, 1) 9 ================================================================= Total params: 49 Trainable params: 49 Non-trainable params: 0 _________________________________________________________________ 4. ์ปดํŒŒ์ผ(Compile)๊ณผ ํ›ˆ๋ จ(Training) ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” RNN์„ ์ด์šฉํ•˜์—ฌ ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ํ•˜๋Š” ์ „ํ˜•์ ์ธ ์ฝ”๋“œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. RNN์€ ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ์ธต, ์€๋‹‰์ธต, ์ถœ๋ ฅ์ธต์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•œ ํ›„์—, ๋งˆ์ง€๋ง‰์œผ๋กœ ์ปดํŒŒ์ผ์„ ํ•ฉ๋‹ˆ๋‹ค. compile() : ๋ชจ๋ธ์„ ๊ธฐ๊ณ„๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ์ปดํŒŒ์ผ ํ•ฉ๋‹ˆ๋‹ค. ์†์‹ค ํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €, ๋ฉ”ํŠธ๋ฆญ ํ•จ์ˆ˜๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.layers import SimpleRNN, Embedding, Dense from tensorflow.keras.models import Sequential vocab_size = 10000 embedding_dim = 32 hidden_units = 32 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(SimpleRNN(hidden_units)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc']) optimizer = ํ›ˆ๋ จ ๊ณผ์ •์„ ์„ค์ •ํ•˜๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. loss = ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ์‚ฌ์šฉํ•  ์†์‹ค ํ•จ์ˆ˜(loss function)๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. metrics = ํ›ˆ๋ จ์„ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ธฐ ์œ„ํ•œ ์ง€ํ‘œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์†์‹ค ํ•จ์ˆ˜์™€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ์กฐํ•ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋” ๋งŽ์€ ํ•จ์ˆ˜๋Š” ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋ฌธ์ œ ์œ ํ˜• ์†์‹ค ํ•จ์ˆ˜๋ช… ์ถœ๋ ฅ์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ช… ์ฐธ๊ณ  ์‹ค์Šต ํšŒ๊ท€ ๋ฌธ์ œ mean_squared_error - ์„ ํ˜• ํšŒ๊ท€ ์‹ค์Šต ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ categorical_crossentropy ์†Œํ”„ํŠธ๋งฅ์Šค ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ถ„๋ฅ˜ํ•˜๊ธฐ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ sparse_categorical_crossentropy ์†Œํ”„ํŠธ๋งฅ์Šค ์–‘๋ฐฉํ–ฅ LSTM๋ฅผ ์ด์šฉํ•œ ํ’ˆ์‚ฌ ํƒœ๊น… ์ด์ง„ ๋ถ„๋ฅ˜ binary_crossentropy ์‹œ๊ทธ๋ชจ์ด๋“œ IMDB ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ sparse_categorical_crossentropy๋Š” categorical_crossentropy์™€ ๋™์ผํ•˜๊ฒŒ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜์—์„œ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ๋ ˆ์ด๋ธ”์„ ์›-ํ•ซ ์ธ์ฝ”๋”ฉํ•˜์ง€ ์•Š๊ณ  ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ์ƒํƒœ์—์„œ ์ˆ˜ํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. fit() : ๋ชจ๋ธ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์˜ค์ฐจ๋กœ๋ถ€ํ„ฐ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ณผ์ •์„ ํ•™์Šต, ํ›ˆ๋ จ, ๋˜๋Š” ์ ํ•ฉ(fitting)์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ๋ชจ๋ธ์ด ๋ฐ์ดํ„ฐ์— ์ ํ•ฉํ•ด๊ฐ€๋Š” ๊ณผ์ •์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ์˜๋ฏธ์—์„œ fit()์€ ๋ชจ๋ธ์˜ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. # ์œ„์˜ compile() ์ฝ”๋“œ์˜ ์—ฐ์žฅ์„ ์ƒ์ธ ์ฝ”๋“œ model.fit(X_train, y_train, epochs=10, batch_size=32) ์ฒซ ๋ฒˆ์งธ ์ธ์ž = ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธ์ž = ์ง€๋„ ํ•™์Šต์—์„œ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. epochs = ์—ํฌํฌ. ์—ํฌํฌ 1์€ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ์ฐจ๋ก€ ํ›‘๊ณ  ์ง€๋‚˜๊ฐ”์Œ์„ ์˜๋ฏธํ•จ. ์ •์ˆซ๊ฐ’ ๊ธฐ์žฌ ํ•„์š”. ์ด ํ›ˆ๋ จ ํšŸ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. batch_size = ๋ฐฐ์น˜ ํฌ๊ธฐ. ๊ธฐ๋ณธ๊ฐ’์€ 32. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” batch_size=None์„ ๊ธฐ์žฌํ•ฉ๋‹ˆ๋‹ค. ์ข€ ๋” ๋งŽ์€ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model.fit(X_train, y_train, epochs=10, batch_size=32, verbose=0, validation_data(X_val, y_val)) validation_data(x_val, y_val) = ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ(validation data)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ ์—ํฌํฌ๋งˆ๋‹ค ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„๋‚˜ ์˜ค์ฐจ๋ฅผ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜๋Š”๋ฐ, ์ด ์ •ํ™•๋„๋Š” ํ›ˆ๋ จ์ด ์ž˜ ๋˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค„ ๋ฟ์ด๋ฉฐ ์‹ค์ œ๋กœ ๋ชจ๋ธ์ด ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์˜ค์ฐจ(loss)๊ฐ€ ๋‚ฎ์•„์ง€๋‹ค๊ฐ€ ๋†’์•„์ง€๊ธฐ ์‹œ์ž‘ํ•˜๋ฉด ์ด๋Š” ๊ณผ ์ ํ•ฉ(overfitting)์˜ ์‹ ํ˜ธ์ž…๋‹ˆ๋‹ค. validation_split = validation_data์™€ ๋™์ผํ•˜๊ฒŒ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ validation_data ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”์ธ X_train๊ณผ y_train์—์„œ ์ผ์ • ๋น„์œจ ๋ถ„๋ฆฌํ•˜์—ฌ ์ด๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 20%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉ. model.fit(X_train, y_train, epochs=10, batch_size=32, verbose=0, validation_split=0.2)) verbose = ํ•™์Šต ์ค‘ ์ถœ๋ ฅ๋˜๋Š” ๋ฌธ๊ตฌ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. - 0 : ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. - 1 : ํ›ˆ๋ จ์˜ ์ง„ํ–‰๋„๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ง„ํ–‰ ๋ง‰๋Œ€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. - 2 : ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋งˆ๋‹ค ์†์‹ค ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” verbose์˜ ๊ฐ’์ด 1์ผ ๋•Œ์™€ 2์ผ ๋•Œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. # verbose = 1์ผ ๊ฒฝ์šฐ. Epoch 88/100 7/7 [==============================] - 0s 143us/step - loss: 0.1029 - acc: 1.0000 # verbose = 2์ผ ๊ฒฝ์šฐ. Epoch 88/100 - 0s - loss: 0.1475 - acc: 1.0000 5. ํ‰๊ฐ€(Evaluation)์™€ ์˜ˆ์ธก(Prediction) evaluate() : ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ํ•™์Šตํ•œ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. # ์œ„์˜ fit() ์ฝ”๋“œ์˜ ์—ฐ์žฅ์„ ์ƒ์ธ ์ฝ”๋“œ model.evaluate(X_test, y_test, batch_size=32) ์ฒซ ๋ฒˆ์งธ ์ธ์ž = ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธ์ž = ์ง€๋„ ํ•™์Šต์—์„œ ๋ ˆ์ด๋ธ” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. batch_size = ๋ฐฐ์น˜ ํฌ๊ธฐ. predict() : ์ž„์˜์˜ ์ž…๋ ฅ์— ๋Œ€ํ•œ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # ์œ„์˜ fit() ์ฝ”๋“œ์˜ ์—ฐ์žฅ์„ ์ƒ์ธ ์ฝ”๋“œ model.predict(X_input, batch_size=32) ์ฒซ ๋ฒˆ์งธ ์ธ์ž = ์˜ˆ์ธกํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ. batch_size = ๋ฐฐ์น˜ ํฌ๊ธฐ. 6. ๋ชจ๋ธ์˜ ์ €์žฅ(Save)๊ณผ ๋กœ๋“œ(Load) ๋ณต์Šต์„ ์œ„ํ•œ ์Šคํ„ฐ๋””๋‚˜ ์‹ค์ œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ ๋‹จ๊ณ„์—์„œ ๊ตฌํ˜„ํ•œ ๋ชจ๋ธ์„ ์ €์žฅํ•˜๊ณ  ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ผ์€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์ €์žฅํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํ•™์Šต์ด ๋๋‚œ ์‹ ๊ฒฝ๋ง์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด์กดํ•˜๊ณ  ๊ณ„์†ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. save() : ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์„ hdf5 ํŒŒ์ผ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. model.save("model_name.h5") load_model() : ์ €์žฅํ•ด๋‘” ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. from tensorflow.keras.models import load_model model = load_model("model_name.h5") 07-09 ์ผ€๋ผ์Šค์˜ ํ•จ์ˆ˜ํ˜• API(Keras Functional API) ์•ž์„œ ๊ตฌํ˜„ํ•œ ์„ ํ˜•, ๋กœ์ง€์Šคํ‹ฑ, ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ๋ชจ๋ธ๋“ค๊ณผ ์ผ€๋ผ์Šค ํ›‘์–ด๋ณด๊ธฐ ์‹ค์Šต์—์„œ ๋ฐฐ์šด ์ผ€๋ผ์Šค์˜ ๋ชจ๋ธ ์„ค๊ณ„ ๋ฐฉ์‹์€ Sequential API์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ Sequential API๋Š” ์—ฌ๋Ÿฌ ์ธต์„<NAME>๊ฑฐ๋‚˜ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๋“ฑ์˜ ๋ณต์žกํ•œ ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ์ผ์—๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋”์šฑ ๋ณต์žกํ•œ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์‹์ธ Functional API(ํ•จ์ˆ˜ํ˜• API)์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. Functional API์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์†Œ๊ฐœ๋Š” ์ผ€๋ผ์Šค ๊ณต์‹ ๋ฌธ์„œ์—์„œ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://keras.io/getting-started/functional-api-guide/ 1. Sequential API๋กœ ๋งŒ๋“  ๋ชจ๋ธ ๋‘ ๊ฐ€์ง€ API์˜ ์ฐจ์ด๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ž์„œ ๋ฐฐ์šด Sequential API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ์ ์ธ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential() model.add(Dense(3, input_dim=4, activation='softmax')) ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์€ ์ง๊ด€์ ์ด๊ณ  ํŽธ๋ฆฌํ•˜์ง€๋งŒ ๋‹จ์ˆœํžˆ ์ธต์„ ์Œ“๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋Š” ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†๋Š” ๋ณต์žกํ•œ ์‹ ๊ฒฝ๋ง์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ดˆ์‹ฌ์ž์—๊ฒŒ ์ ํ•ฉํ•œ API์ด์ง€๋งŒ, ์ „๋ฌธ๊ฐ€๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ Functional API๋ฅผ ํ•™์Šตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 2. Functional API๋กœ ๋งŒ๋“  ๋ชจ๋ธ Functional API๋Š” ๊ฐ ์ธต์„ ์ผ์ข…์˜ ํ•จ์ˆ˜(function)๋กœ์„œ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ํ•จ์ˆ˜๋ฅผ ์กฐํ•ฉํ•˜๊ธฐ ์œ„ํ•œ ์—ฐ์‚ฐ์ž๋“ค์„ ์ œ๊ณตํ•˜๋Š”๋ฐ, ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹ ๊ฒฝ๋ง์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. Functional API๋กœ FFNN, RNN ๋“ฑ ๋‹ค์–‘ํ•œ ๋ชจ๋ธ์„ ๋งŒ๋“ค๋ฉด์„œ ๊ธฐ์กด์˜ sequential API์™€์˜ ์ฐจ์ด๋ฅผ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. Functional API๋Š” ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape)๋ฅผ ๋ช…์‹œํ•œ ์ž…๋ ฅ์ธต(Input layer)์„ ๋ชจ๋ธ์˜ ์•ž๋‹จ์— ์ •์˜ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1) ์ „ ๊ฒฐํ•ฉ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง(Fully-connected FFNN) Sequential API์™€๋Š” ๋‹ค๋ฅด๊ฒŒ functional API์—์„œ๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape)๋ฅผ ์ธ์ž๋กœ ์ž…๋ ฅ์ธต์„ ์ •์˜ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง(Fully-connected FFNN)์„ ๋งŒ๋“ ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from tensorflow.keras.layers import Input, Dense from tensorflow.keras.models import Model inputs = Input(shape=(10, )) ์œ„์˜ ์ฝ”๋“œ๋Š” 10๊ฐœ์˜ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ์ž…๋ ฅ์ธต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์— ์€๋‹‰์ธต๊ณผ ์ถœ๋ ฅ์ธต์„ ์ถ”๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. inputs = Input(shape=(10, )) hidden1 = Dense(64, activation='relu')(inputs) # <- ์ƒˆ๋กœ ์ถ”๊ฐ€ hidden2 = Dense(64, activation='relu')(hidden1) # <- ์ƒˆ๋กœ ์ถ”๊ฐ€ output = Dense(1, activation='sigmoid')(hidden2) # <- ์ƒˆ๋กœ ์ถ”๊ฐ€ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜์˜ ๋ชจ๋ธ๋กœ ๊ตฌ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” Model์— ์ž…๋ ฅ ํ…์„œ์™€ ์ถœ๋ ฅ ํ…์„œ๋ฅผ ์ •์˜ํ•˜์—ฌ ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. inputs = Input(shape=(10, )) hidden1 = Dense(64, activation='relu')(inputs) hidden2 = Dense(64, activation='relu')(hidden1) output = Dense(1, activation='sigmoid')(hidden2) model = Model(inputs=inputs, outputs=output) # <- ์ƒˆ๋กœ ์ถ”๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€์˜ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Input() ํ•จ์ˆ˜์— ์ž…๋ ฅ์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „์ธต์„ ๋‹ค์Œ์ธต ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. Model() ํ•จ์ˆ˜์— ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ model๋กœ ์ €์žฅํ•˜๋ฉด sequential API๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ model.compile, model.fit ๋“ฑ์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # model.fit(data, labels) ์ด๋ฒˆ์—๋Š” ๋ณ€์ˆ˜๋ช…์„ ๋‹ฌ๋ฆฌํ•ด์„œ FFNN์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์€๋‹‰์ธต๊ณผ ์ถœ๋ ฅ์ธต์˜ ๋ณ€์ˆ˜๋ฅผ ์ „๋ถ€ x๋กœ ํ†ต์ผํ•˜์˜€์Šต๋‹ˆ๋‹ค. inputs = Input(shape=(10, )) x = Dense(8, activation="relu")(inputs) x = Dense(4, activation="relu")(x) x = Dense(1, activation="linear")(x) model = Model(inputs, x) ์ด๋ฒˆ์—๋Š” ์œ„์—์„œ ๋ฐฐ์šด ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ์„ ํ˜• ํšŒ๊ท€์™€ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ Functional API๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 2) ์„ ํ˜• ํšŒ๊ท€(Linear Regression) ์•ž์„œ ( https://wikidocs.net/111472 ) Sequential API๋กœ ๊ตฌํ˜„ํ–ˆ๋˜ ์„ ํ˜• ํšŒ๊ท€๋ฅผ Functional API๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. from tensorflow.keras.layers import Input, Dense from tensorflow.keras import optimizers from tensorflow.keras.models import Model X = [1, 2, 3, 4, 5, 6, 7, 8, 9] # ๊ณต๋ถ€ํ•˜๋Š” ์‹œ๊ฐ„ y = [11, 22, 33, 44, 53, 66, 77, 87, 95] # ๊ฐ ๊ณต๋ถ€ ํ•˜๋Š” ์‹œ๊ฐ„์— ๋งคํ•‘๋˜๋Š” ์„ฑ์  inputs = Input(shape=(1, )) output = Dense(1, activation='linear')(inputs) linear_model = Model(inputs, output) sgd = optimizers.SGD(lr=0.01) linear_model.compile(optimizer=sgd, loss='mse', metrics=['mse']) linear_model.fit(X, y, epochs=300) ๊ทธ ์™ธ์— ๋‹ค์–‘ํ•œ ๋‹ค๋ฅธ ์˜ˆ์ œ๋“ค์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 3) ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€(Logistic Regression) from tensorflow.keras.layers import Input, Dense from tensorflow.keras.models import Model inputs = Input(shape=(3, )) output = Dense(1, activation='sigmoid')(inputs) logistic_model = Model(inputs, output) 4) ๋‹ค์ค‘ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๋ชจ๋ธ(model that accepts multiple inputs) functional API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ค์ค‘ ์ž…๋ ฅ๊ณผ ๋‹ค์ค‘ ์ถœ๋ ฅ์„ ๊ฐ€์ง€๋Š” ๋ชจ๋ธ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ์ตœ์ข… ์™„์„ฑ๋œ ๋‹ค์ค‘ ์ž…๋ ฅ, ๋‹ค์ค‘ ์ถœ๋ ฅ ๋ชจ๋ธ์˜ ์˜ˆ model = Model(inputs=[a1, a2], outputs=[b1, b2, b3]) ์ด๋ฒˆ์—๋Š” ๋‹ค์ค‘ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๋ชจ๋ธ์„ ์ž…๋ ฅ์ธต๋ถ€ํ„ฐ ์ถœ๋ ฅ์ธต๊นŒ์ง€ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from tensorflow.keras.layers import Input, Dense, concatenate from tensorflow.keras.models import Model # ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์ธต์„ ์ •์˜ inputA = Input(shape=(64, )) inputB = Input(shape=(128, )) # ์ฒซ ๋ฒˆ์งธ ์ž…๋ ฅ์ธต์œผ๋กœ๋ถ€ํ„ฐ ๋ถ„๊ธฐ๋˜์–ด ์ง„ํ–‰๋˜๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ •์˜ x = Dense(16, activation="relu")(inputA) x = Dense(8, activation="relu")(x) x = Model(inputs=inputA, outputs=x) # ๋‘ ๋ฒˆ์งธ ์ž…๋ ฅ์ธต์œผ๋กœ๋ถ€ํ„ฐ ๋ถ„๊ธฐ๋˜์–ด ์ง„ํ–‰๋˜๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ •์˜ y = Dense(64, activation="relu")(inputB) y = Dense(32, activation="relu")(y) y = Dense(8, activation="relu")(y) y = Model(inputs=inputB, outputs=y) # ๋‘ ๊ฐœ์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ถœ๋ ฅ์„ ์—ฐ๊ฒฐ(concatenate) result = concatenate([x.output, y.output]) z = Dense(2, activation="relu")(result) z = Dense(1, activation="linear")(z) model = Model(inputs=[x.input, y.input], outputs=z) ์œ„ ๋ชจ๋ธ์€ ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์ธต์œผ๋กœ๋ถ€ํ„ฐ ๋ถ„๊ธฐ๋˜์–ด ์ง„ํ–‰๋œ ํ›„ ๋งˆ์ง€๋ง‰์—๋Š” ํ•˜๋‚˜์˜ ์ถœ๋ ฅ์„ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. 5) RNN(Recurrence Neural Network) ์€๋‹‰์ธต ์‚ฌ์šฉํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” RNN ์€๋‹‰์ธต์„ ๊ฐ€์ง€๋Š” ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ•˜๋‚˜์˜ ํŠน์„ฑ(feature)์— 50๊ฐœ์˜ ์‹œ์ (time-step)์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š” ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. RNN์— ๋Œ€ํ•œ ๊ตฌ์ฒด์ ์ธ ๋‚ด์šฉ์€ ๋‹ค์Œ ์ฑ•ํ„ฐ์ธ RNN ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. from tensorflow.keras.layers import Input, Dense, LSTM from tensorflow.keras.models import Model inputs = Input(shape=(50,1)) lstm_layer = LSTM(10)(inputs) x = Dense(10, activation='relu')(lstm_layer) output = Dense(1, activation='sigmoid')(x) model = Model(inputs=inputs, outputs=output) ๋‹ค์ˆ˜์˜ ์ž…๋ ฅ๊ณผ ๋‹ค์ˆ˜์˜ ์ถœ๋ ฅ์„ ๊ฐ€์ง€๋Š” ์ข€ ๋” ๋‹ค์–‘ํ•œ ์˜ˆ์ œ๋Š” ์•ž์„œ ์†Œ๊ฐœํ•œ ์ผ€๋ผ์Šค ๊ณต์‹ ๋ฌธ์„œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 6) ๋‹ค๋ฅด๊ฒŒ ๋ณด์ด์ง€๋งŒ ๋™์ผํ•œ ํ‘œ๊ธฐ ์ผ€๋ผ์Šค์˜ Functional API๊ฐ€ ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ Functional API๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ค๊ฐ€ ํ˜ผ๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ํ•œ ๊ฐ€์ง€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๋™์ผํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€์ง€๋งŒ, ํ•˜๋‚˜์˜ ์ค„๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ๋‘ ๊ฐœ์˜ ์ค„๋กœ ํ‘œํ˜„ํ•œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. result = Dense(128)(input) ์œ„ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋‘ ๊ฐœ์˜ ์ค„๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. dense = Dense(128) result = dense(input) 07-10 ์ผ€๋ผ์Šค ์„œ๋ธŒํด๋ž˜์‹ฑ API(Keras Subclassing API) ์ผ€๋ผ์Šค์˜ ๊ตฌํ˜„ ๋ฐฉ์‹์—๋Š” Sequential API, Functional API ์™ธ์—๋„ Subclassing API๋ผ๋Š” ๊ตฌํ˜„ ๋ฐฉ์‹์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 1. ์„œ๋ธŒํด๋ž˜์‹ฑ API๋กœ ๊ตฌํ˜„ํ•œ ์„ ํ˜• ํšŒ๊ท€ ์•ž์„œ ( https://wikidocs.net/111472 )์—์„œ Sequential API๋กœ ๊ตฌํ˜„ํ–ˆ๋˜ ์„ ํ˜• ํšŒ๊ท€๋ฅผ Subclassing API๋กœ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. import tensorflow as tf class LinearRegression(tf.keras.Model): def __init__(self): super(LinearRegression, self).__init__() self.linear_layer = tf.keras.layers.Dense(1, input_dim=1, activation='linear') def call(self, x): y_pred = self.linear_layer(x) return y_pred model = LinearRegression() X = [1, 2, 3, 4, 5, 6, 7, 8, 9] # ๊ณต๋ถ€ํ•˜๋Š” ์‹œ๊ฐ„ y = [11, 22, 33, 44, 53, 66, 77, 87, 95] # ๊ฐ ๊ณต๋ถ€ ํ•˜๋Š” ์‹œ๊ฐ„์— ๋งตํ•‘๋˜๋Š” ์„ฑ์  sgd = tf.keras.optimizers.SGD(lr=0.01) model.compile(optimizer=sgd, loss='mse', metrics=['mse']) model.fit(X, y, epochs=300) ํด๋ž˜์Šค(class) ํ˜•ํƒœ์˜ ๋ชจ๋ธ์€ tf.keras.Model์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  init()์—์„œ ๋ชจ๋ธ์˜ ๊ตฌ์กฐ์™€ ๋™์ ์„ ์ •์˜ํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํŒŒ์ด์ฌ์—์„œ ๊ฐ์ฒด๊ฐ€ ๊ฐ–๋Š” ์†์„ฑ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• ๋กœ, ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์ž๋™์œผํ˜ธ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. super() ํ•จ์ˆ˜๋ฅผ ๋ถ€๋ฅด๋ฉด ์—ฌ๊ธฐ์„œ ๋งŒ๋“  ํด๋ž˜์Šค๋Š” tf.keras.Model ํด๋ž˜์Šค์˜ ์†์„ฑ๋“ค์„ ๊ฐ€์ง€๊ณ  ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. call() ํ•จ์ˆ˜๋Š” ๋ชจ๋ธ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์˜ˆ์ธก๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ํฌ์›Œ๋“œ(forward) ์—ฐ์‚ฐ์„ ์ง„ํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ( ) ์‹์— ์ž…๋ ฅ ๋กœ๋ถ€ํ„ฐ ์˜ˆ์ธก๋œ ๋ฅผ ์–ป๋Š” ๊ฒƒ์„ forward ์—ฐ์‚ฐ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ์–ธ์ œ ์„œ๋ธŒํด๋ž˜์‹ฑ API๋ฅผ ์จ์•ผ ํ• ๊นŒ? Sequential API๋Š” ๊ฐ„๋‹จํ•œ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜๊ธฐ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. Functional API๋กœ๋Š” Sequential API๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†๋Š” ๋ณต์žกํ•œ ๋ชจ๋ธ๋“ค์„ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ Subclassing API๋กœ๋Š” Functional API๊ฐ€ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†๋Š” ๋ชจ๋ธ๋“ค์กฐ์ฐจ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Functional API๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ DAG(directed acyclic graph)๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋Œ€๋ถ€๋ถ„์˜ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์ด ์ด์— ์†ํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ํ•ญ์ƒ ๊ทธ๋ ‡์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์žฌ๊ท€ ๋„คํŠธ์›Œํฌ๋‚˜ ํŠธ๋ฆฌ RNN์€ ์ด ๊ฐ€์ •์„ ๋”ฐ๋ฅด์ง€ ์•Š์œผ๋ฉฐ Functional API์—์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐ˜๋Œ€๋กœ ํ•ด์„ํ•˜๋ฉด ๋Œ€๋ถ€๋ถ„์˜ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์€ Functional API ์ˆ˜์ค€์—์„œ๋„ ์ „๋ถ€ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์˜๋ฏธ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ Subclassing API๋Š” ๋ฐ‘๋ฐ”๋‹ฅ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด ์ˆ˜์ค€์˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ์‹คํ—˜์  ์—ฐ๊ตฌ๋ฅผ ํ•˜๋Š” ์—ฐ๊ตฌ์ž๋“ค์—๊ฒŒ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. 3. ์„ธ ๊ฐ€์ง€ ๊ตฌํ˜„ ๋ฐฉ์‹ ๋น„๊ต. 1) Sequential API ์žฅ์  : ๋‹จ์ˆœํ•˜๊ฒŒ ์ธต์„ ์Œ“๋Š” ๋ฐฉ์‹์œผ๋กœ ์‰ฝ๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ๊ฐ€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์  : ๋‹ค์ˆ˜์˜ ์ž…๋ ฅ(multi-input), ๋‹ค์ˆ˜์˜ ์ถœ๋ ฅ(multi-output)์„ ๊ฐ€์ง„ ๋ชจ๋ธ ๋˜๋Š” ์ธต ๊ฐ„์˜ ์—ฐ๊ฒฐ(concatenate)์ด๋‚˜ ๋ง์…ˆ(Add)๊ณผ ๊ฐ™์€ ์—ฐ์‚ฐ์„ ํ•˜๋Š” ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜๊ธฐ์—๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ชจ๋ธ๋“ค์˜ ๊ตฌํ˜„์€ Functional API๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 2) Functional API ์žฅ์  : Sequential API๋กœ๋Š” ๊ตฌํ˜„ํ•˜๊ธฐ ์–ด๋ ค์šด ๋ณต์žกํ•œ ๋ชจ๋ธ๋“ค์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์  : ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape)๋ฅผ ๋ช…์‹œํ•œ ์ž…๋ ฅ์ธต(Input layer)์„ ๋ชจ๋ธ์˜ ์•ž๋‹จ์— ์ •์˜ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋ด…์‹œ๋‹ค. # ์„ ํ˜• ํšŒ๊ท€ ๊ตฌํ˜„ ์ฝ”๋“œ์˜ ์ผ๋ถ€ ๋ฐœ์ทŒ inputs = Input(shape=(1, )) # <-- ํ•ด๋‹น ๋ถ€๋ถ„ output = Dense(1, activation='linear')(inputs) linear_model = Model(inputs, output) sgd = optimizers.SGD(lr=0.01) linear_model.compile(optimizer=sgd, loss='mse', metrics=['mse']) linear_model.fit(X, y, epochs=300) 3) Subclassing API ์žฅ์  : Functional API๋กœ๋„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†๋Š” ๋ชจ๋ธ๋“ค์กฐ์ฐจ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์  : ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Object-oriented programming)์— ์ต์ˆ™ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ฝ”๋“œ ์‚ฌ์šฉ์ด ๊ฐ€์žฅ ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. 07-11 ๋‹ค์ธต ํผ์…‰ํŠธ๋ก (MultiLayer Perceptron, MLP)์œผ๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก (Multilayer Perceptron, MLP)์œผ๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 1. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก (MultiLayer Perceptron, MLP) ์•ž์„œ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ํ˜•ํƒœ์—์„œ ์€๋‹‰์ธต์ด 1๊ฐœ ์ด์ƒ ์ถ”๊ฐ€๋œ ์‹ ๊ฒฝ๋ง์„ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก (MLP)์ด๋ผ๊ณ  ํ•œ๋‹ค๊ณ  ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง(Feed Forward Neural Network, FFNN)์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์€ ์ž…๋ ฅ์ธต์—์„œ ์ถœ๋ ฅ์ธต์œผ๋กœ ์˜ค์ง ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์—ฐ์‚ฐ ๋ฐฉํ–ฅ์ด ์ •ํ•ด์ ธ ์žˆ๋Š” ์‹ ๊ฒฝ๋ง์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋’ค์—์„œ๋Š” ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(RNN)๊ณผ ๋ถ„์‚ฐ ํ‘œํ˜„(distributed representation)์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐœ๋…๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ข… ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์‹ค์Šต์„ ํ•˜๊ฒŒ ๋  ํ…๋ฐ, ์ด๋ฒˆ ์‹ค์Šต์˜ ๋ชฉ์ ์€ ์œ„ ๋‘ ๊ฐ€์ง€ ๊ฐœ๋… ์—†์ด ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๊ฐœ๋…๋งŒ์œผ๋กœ๋„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. 2. ์ผ€๋ผ์Šค์˜ texts_to_matrix() ์ดํ•ดํ•˜๊ธฐ MLP๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— ์ด๋ฒˆ์— ์‚ฌ์šฉํ•  ๋„๊ตฌ์ธ ์ผ€๋ผ์Šค Tokenizer์˜ texts_to_matrix()๋ฅผ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. import numpy as np from tensorflow.keras.preprocessing.text import Tokenizer ์šฐ์„  ์ผ€๋ผ์Šค์˜ ์ „์ฒ˜๋ฆฌ ๋„๊ตฌ์ธ Tokenizer๋ฅผ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. texts = ['๋จน๊ณ  ์‹ถ์€ ์‚ฌ๊ณผ', '๋จน๊ณ  ์‹ถ์€ ๋ฐ”๋‚˜๋‚˜', '๊ธธ๊ณ  ๋…ธ๋ž€ ๋ฐ”๋‚˜๋‚˜ ๋ฐ”๋‚˜๋‚˜', '์ €๋Š” ๊ณผ์ผ์ด ์ข‹์•„์š”'] ์œ„ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(texts) print(tokenizer.word_index) {'๋ฐ”๋‚˜๋‚˜': 1, '๋จน๊ณ ': 2, '์‹ถ์€': 3, '์‚ฌ๊ณผ': 4, '๊ธธ๊ณ ': 5, '๋…ธ๋ž€': 6, '์ €๋Š”': 7, '๊ณผ์ผ์ด': 8, '์ข‹์•„์š”': 9} ๊ฐ ๋‹จ์–ด์— ์ˆซ์ž 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ •์ˆ˜ ์ธ๋ฑ์Šค๊ฐ€ ๋ถ€์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์— texts_to_matrix()๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. texts_to_matrix()๋ž€ ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด ๋„๊ตฌ๋Š” ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ํ–‰๋ ฌ(matrix)๋ฅผ ๋งŒ๋“œ๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. texts_to_matrx()๋Š” ์ด 4๊ฐœ์˜ ๋ชจ๋“œ๋ฅผ ์ง€์›ํ•˜๋Š”๋ฐ ๊ฐ ๋ชจ๋“œ๋Š” 'binary', 'count', 'freq', 'tfidf'๋กœ ์ด 4๊ฐœ์ž…๋‹ˆ๋‹ค. ์šฐ์„  'count' ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. print(tokenizer.texts_to_matrix(texts, mode = 'count')) # texts_to_matrix์˜ ์ž…๋ ฅ์œผ๋กœ texts๋ฅผ ๋„ฃ๊ณ , ๋ชจ๋“œ๋Š” 'count' [[0. 0. 1. 1. 1. 0. 0. 0. 0. 0.] [0. 1. 1. 1. 0. 0. 0. 0. 0. 0.] [0. 2. 0. 0. 0. 1. 1. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 1. 1. 1.]] ์œ„์˜ ๊ฒฝ์šฐ๋Š” ์ด 4๊ฐœ์˜ ๋ชจ๋“œ ์ค‘์—์„œ 'count' ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. 'count'๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์•ž์„œ ๋ฐฐ์šด ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix, DTM)์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. DTM์—์„œ์˜ ์ธ๋ฑ์Šค๋Š” ์•ž์„œ ํ™•์ธํ•œ word_index์˜ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ฃผ์˜ํ•  ์ ์€ ๊ฐ ๋‹จ์–ด์— ๋ถ€์—ฌ๋˜๋Š” ์ธ๋ฑ์Šค๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋ฐ˜๋ฉด์— ์™„์„ฑ๋˜๋Š” ํ–‰๋ ฌ์˜ ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋Š” 9๊ฐœ์˜€์ง€๋งŒ ์™„์„ฑ๋œ ํ–‰๋ ฌ์˜ ์—ด์˜ ๊ฐœ์ˆ˜๋Š” 10๊ฐœ์ธ ๊ฒƒ๊ณผ ์ฒซ ๋ฒˆ์งธ ์—ด์€ ๋ชจ๋“  ํ–‰์—์„œ ๊ฐ’์ด 0์ธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค 0์—๋Š” ๊ทธ ์–ด๋–ค ๋‹จ์–ด๋„ ํ• ๋‹น๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์šฐ์„ , ๋„ค ๋ฒˆ์งธ ํ–‰์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋„ค ๋ฒˆ์งธ ํ–‰์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ๋„ค ๋ฒˆ์งธ ๋ฌธ์žฅ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋„ค ๋ฒˆ์งธ ํ–‰์€ 8๋ฒˆ์งธ ์—ด, 9๋ฒˆ์งธ ์—ด, 10๋ฒˆ์งธ ์—ด์—์„œ 1์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋Š” 7๋ฒˆ ๋‹จ์–ด, 8๋ฒˆ ๋‹จ์–ด, 9๋ฒˆ ๋‹จ์–ด๊ฐ€ ๋„ค ๋ฒˆ์งธ ๋ฌธ์žฅ์—์„œ 1๊ฐœ์”ฉ ์กด์žฌํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด 7๋ฒˆ ๋‹จ์–ด๋Š” '์ €๋Š”', 8๋ฒˆ ๋‹จ์–ด๋Š” '๊ณผ์ผ์ด', 9๋ฒˆ ๋‹จ์–ด๋Š” '์ข‹์•„์š”'์ž…๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ํ–‰์˜ ์ฒซ ๋ฒˆ์งธ ์—ด์˜ ๊ฐ’์€ 2์ธ๋ฐ, ์ด๋Š” ์„ธ ๋ฒˆ์งธ ๋ฌธ์žฅ์—์„œ 1๋ฒˆ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ ๋ฐ”๋‚˜๋‚˜๊ฐ€ ๋‘ ๋ฒˆ ๋“ฑ์žฅํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์› ๋“ฏ์ด DTM์€ bag of words๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋ฏ€๋กœ ๋‹จ์–ด ์ˆœ์„œ ์ •๋ณด๋Š” ๋ณด์กด๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋” ๊ตฌ์ฒด์ ์œผ๋กœ๋Š” 4๊ฐœ์˜ ๋ชจ๋“  ๋ชจ๋“œ์—์„œ ๋‹จ์–ด ์ˆœ์„œ ์ •๋ณด๋Š” ๋ณด์กด๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 'binary' ๋ชจ๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(tokenizer.texts_to_matrix(texts, mode = 'binary')) [[0. 0. 1. 1. 1. 0. 0. 0. 0. 0.] [0. 1. 1. 1. 0. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 1. 1. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 1. 1. 1.]] DTM๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๋งค์šฐ ์œ ์‚ฌํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์„ธ ๋ฒˆ์งธ ํ–‰, ๋‘ ๋ฒˆ์งธ ์—ด์˜ ๊ฐ’์ด DTM์—์„œ๋Š” 2์˜€๋Š”๋ฐ ์—ฌ๊ธฐ์„œ๋Š” 1๋กœ ๋ฐ”๋€Œ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” 'binary' ๋ชจ๋“œ๋Š” ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€๋งŒ ๊ด€์‹ฌ์„ ๊ฐ€์ง€๊ณ  ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ๋ช‡ ๊ฐœ์˜€๋Š”์ง€๋Š” ๋ฌด์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜๋ฉด 1, ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด 0์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ฆ‰, ๋‹จ์–ด์˜ ์กด์žฌ ์œ ๋ฌด๋กœ๋งŒ ํ–‰๋ ฌ์„ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. 'tfidf' ๋ชจ๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(tokenizer.texts_to_matrix(texts, mode = 'tfidf').round(2)) # ๋‘˜์งธ ์ž๋ฆฌ๊นŒ์ง€ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ์ถœ๋ ฅ [[0. 0. 0.85 0.85 1.1 0. 0. 0. 0. 0. ] [0. 0.85 0.85 0.85 0. 0. 0. 0. 0. 0. ] [0. 1.43 0. 0. 0. 1.1 1.1 0. 0. 0. ] [0. 0. 0. 0. 0. 0. 0. 1.1 1.1 1.1 ]] 'tfidf' ๋ชจ๋“œ๋Š” ๋ง ๊ทธ๋Œ€๋กœ TF-IDF ํ–‰๋ ฌ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, TF-IDF ์‹ค์Šต์—์„œ ๋ฐฐ์šด ๊ธฐ๋ณธ์‹์ด๋‚˜ ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ TfidfVectorizer์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์‹์ด๋ž‘ ๋˜ ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ๊ธฐ๋ณธ์‹์—์„œ TF๋Š” ๊ฐ ๋ฌธ์„œ์—์„œ์˜ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์˜€๋‹ค๋ฉด 'tfidf' ๋ชจ๋“œ์—์„œ๋Š” TF๋ฅผ ๊ฐ ๋ฌธ์„œ์—์„œ์˜ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์— ์ž์—ฐ ๋กœ๊ทธ๋ฅผ ์”Œ์šฐ๊ณ  1์„ ๋”ํ•œ ๊ฐ’์œผ๋กœ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. idf์—์„œ๋Š” ์•ž์„œ ๋ฐฐ์šด ๊ธฐ๋ณธ์‹์—์„œ ๋กœ๊ทธ๋Š” ์ž์—ฐ ๋กœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋กœ๊ทธ ์•ˆ์˜ ๋ถ„์ˆ˜์— 1์„ ์ถ”๊ฐ€๋กœ ๋”ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์ด๋Ÿฌํ•œ ์‹์„ ๊ตณ์ด ๊ธฐ์–ตํ•  ํ•„์š”๋Š” ์—†๊ณ  ์—ฌ์ „ํžˆ TF-IDF์˜ ๊ธฐ์กด ์˜๋„๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. print(tokenizer.texts_to_matrix(texts, mode = 'freq').round(2)) # ๋‘˜์งธ ์ž๋ฆฌ๊นŒ์ง€ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ์ถœ๋ ฅ [[0. 0. 0.33 0.33 0.33 0. 0. 0. 0. 0. ] [0. 0.33 0.33 0.33 0. 0. 0. 0. 0. 0. ] [0. 0.5 0. 0. 0. 0.25 0.25 0. 0. 0. ] [0. 0. 0. 0. 0. 0. 0. 0.33 0.33 0.33]] ๋งˆ์ง€๋ง‰์œผ๋กœ 'freq' ๋ชจ๋“œ์ž…๋‹ˆ๋‹ค. 'freq' ๋ชจ๋“œ๋Š” ๊ฐ ๋ฌธ์„œ์—์„œ์˜ ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ๋ถ„์ž๋กœ, ๊ฐ ๋ฌธ์„œ์˜ ํฌ๊ธฐ(๊ฐ ๋ฌธ์„œ์—์„œ ๋“ฑ์žฅํ•œ ๋ชจ๋“  ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜์˜ ์ดํ•ฉ)๋ฅผ ๋ถ„๋ชจ๋กœ ํ•˜๋Š” ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์„ธ ๋ฒˆ์งธ ํ–‰์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ๋ฌธ์žฅ์€ '๊ธธ๊ณ  ๋…ธ๋ž€ ๋ฐ”๋‚˜๋‚˜ ๋ฐ”๋‚˜๋‚˜'์˜€์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ์˜ ํฌ๊ธฐ๋Š” 4์ธ๋ฐ, ๋ฐ”๋‚˜๋‚˜๋Š” ์ด 2ํšŒ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ์„œ ์„ธ ๋ฒˆ์งธ ๋ฌธ์žฅ์—์„œ์˜ ๋‹จ์–ด '๋ฐ”๋‚˜๋‚˜'์˜ ๊ฐ’์€ ์œ„์˜ ํ–‰๋ ฌ์—์„œ 0.5๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด์— '๊ธธ๊ณ ', '๋…ธ๋ž€'์ด๋ผ๋Š” ๋‘ ๋‹จ์–ด๋Š” ๊ฐ 1ํšŒ ๋“ฑ์žฅํ–ˆ์œผ๋ฏ€๋กœ ๊ฐ์ž 1/4์˜ ๊ฐ’์ธ 0.25์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 3. 20๊ฐœ ๋‰ด์Šค ๊ทธ๋ฃน(Twenty Newsgroups) ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด import pandas as pd from sklearn.datasets import fetch_20newsgroups import matplotlib.pyplot as plt from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.utils import to_categorical ์‚ฌ์ดํ‚ท๋Ÿฐ์—์„œ๋Š” 20๊ฐœ์˜ ๋‹ค๋ฅธ ์ฃผ์ œ๋ฅผ ๊ฐ€์ง„ 18,846๊ฐœ์˜ ๋‰ด์Šค ๊ทธ๋ฃน ์ด๋ฉ”์ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. newsdata = fetch_20newsgroups(subset = 'train') # 'train'์„ ๊ธฐ์žฌํ•˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋งŒ ๋ฆฌํ„ดํ•œ๋‹ค. ์œ„์˜ subset์˜ ๊ฐ’์œผ๋กœ 'all'์„ ๋„ฃ์œผ๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ์ธ 18,846๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, 'train'์„ ๋„ฃ์œผ๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ, 'test'๋ฅผ ๋„ฃ์œผ๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. newsdata.keys()๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ํ•ด๋‹น ๋ฐ์ดํ„ฐ์˜ ์†์„ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(newsdata.keys()) dict_keys(['data', 'filenames', 'target_names', 'target', 'DESCR']) ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” data, filenames, target_names, target, DESCR, description์ด๋ผ๋Š” 6๊ฐœ ์†์„ฑ์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ค‘ ์‹ค์ œ๋กœ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•  ์†์„ฑ์€ ์ด๋ฉ”์ผ ๋ณธ๋ฌธ์ธ data์™€ ๋ฉ”์ผ์ด ์–ด๋–ค ์ฃผ์ œ์ธ์ง€ ๊ธฐ์žฌ๋œ ์ˆซ์ž ๋ ˆ์ด๋ธ”์ธ target์ž…๋‹ˆ๋‹ค. ์šฐ์„  ํ›ˆ๋ จ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('ํ›ˆ๋ จ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : {}'.format(len(newsdata.data))) ํ›ˆ๋ จ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 11314 ํ›ˆ๋ จ ์ƒ˜ํ”Œ์€ 11,314๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. target_names์—๋Š” 20๊ฐœ์˜ ์ฃผ์ œ์˜ ์ด๋ฆ„์„ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ์ฃผ์ œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('์ด ์ฃผ์ œ์˜ ๊ฐœ์ˆ˜ : {}'.format(len(newsdata.target_names))) print(newsdata.target_names) ์ด ์ฃผ์ œ์˜ ๊ฐœ์ˆ˜ : 20 ['alt.atheism', 'comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware', 'comp.windows.x', 'misc.forsale', 'rec.autos', 'rec.motorcycles', 'rec.sport.baseball', 'rec.sport.hockey', 'sci.crypt', 'sci.electronics', 'sci.med', 'sci.space', 'soc.religion.christian', 'talk.politics.guns', 'talk.politics.mideast', 'talk.politics.misc', 'talk.religion.misc'] ์ด๋ฒˆ ์‹ค์Šต์˜ ๋ชฉ์ ์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ์ด๋ฉ”์ผ ๋ณธ๋ฌธ์„ ๋ณด๊ณ  20๊ฐœ์˜ ์ฃผ์ œ ์ค‘ ์–ด๋–ค ์ฃผ์ œ์ธ์ง€๋ฅผ ๋งž์ถ”๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์ธ target์—๋Š” ์ด 0๋ถ€ํ„ฐ 19๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ช‡ ๋ฒˆ ์ฃผ์ œ์ธ์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : {}'.format(newsdata.target[0])) ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : 7 ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ”์˜ ๊ฐ’์€ 7์ž…๋‹ˆ๋‹ค. ์ˆซ์ž๋งŒ์œผ๋กœ๋Š” ์•ž์„œ target_names๋ฅผ ํ†ตํ•ด ํ™•์ธํ•œ 20๊ฐœ์˜ ์ฃผ์ œ ์ค‘์—์„œ ์–ด๋–ค ์ฃผ์ œ๋ฅผ ์˜๋ฏธํ•˜๋Š”์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. 7์ด ์‹ค์ œ๋กœ ์–ด๋–ค ์ฃผ์ œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š”์ง€๋Š” target_names[] ์•ˆ์— ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print('7๋ฒˆ ๋ ˆ์ด๋ธ”์ด ์˜๋ฏธํ•˜๋Š” ์ฃผ์ œ : {}'.format(newsdata.target_names[7])) 7๋ฒˆ ๋ ˆ์ด๋ธ”์ด ์˜๋ฏธํ•˜๋Š” ์ฃผ์ œ : rec.autos 7๋ฒˆ ๋ ˆ์ด๋ธ”์€ rec.autos๋ผ๋Š” ์ฃผ์ œ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ์ฃผ์ œ๋Š” rec.autos์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ณธ๋ฌธ ๋‚ด์šฉ์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(newsdata.data[0]) # ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ์ถœ๋ ฅ From: lerxst@wam.umd.edu (where's my thing) Subject: WHAT car is this!? Nntp-Posting-Host: rac3.wam.umd.edu Organization: University of Maryland, College Park Lines: 15 I was wondering if anyone out there could enlighten me on this car I saw the other day. It was a 2-door sports car, looked to be from the late 60s/ early 70s. It was called a Bricklin. The doors were really small. In addition, the front bumper was separate from the rest of the body. This is all I know. If anyone can tellme a model name, engine specs, years of production, where this car is made, history, or whatever info you have on this funky looking car, please e-mail. Thanks, - IL ---- brought to you by your neighborhood Lerxst ---- ์ด๋ฉ”์ผ์˜ ๋‚ด์šฉ์„ ๋ณด๋‹ˆ ์Šคํฌ์ธ ์นด์— ๋Œ€ํ•œ ๊ธ€๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด ๊ธ€์˜ ๋ ˆ์ด๋ธ”์€ 7์ด๊ณ , 7๋ฒˆ ๋ ˆ์ด๋ธ”์€ rec.autos๋ž€ ์ฃผ์ œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ์— ์‚ฌ์šฉ๋  ๋ฉ”์ผ ๋ณธ๋ฌธ์ธ data์™€ ๋ ˆ์ด๋ธ”์ธ target์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ†ต๊ณ„์ ์ธ ์ •๋ณด๋“ค์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. data๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ƒ์„ฑํ•˜๊ณ , target ์—ด์„ ์ถ”๊ฐ€ํ•œ ๋’ค์— ์ƒ์œ„ 5๊ฐœ์˜ ํ–‰์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. data = pd.DataFrame(newsdata.data, columns = ['email']) data['target'] = pd.Series(newsdata.target) data[:5] ๋ฉ”์ผ ๋ณธ๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” email ์—ด๊ณผ ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” target ์—ด, 2๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. data.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 11314 entries, 0 to 11313 Data columns (total 2 columns): news 11314 non-null object target 11314 non-null int32 dtypes: int32(1), object(1) memory usage: 132.7+ KB news ์—ด์€ ๋ฌธ์ž์—ด, target ์—ด์€ ์ •์ˆ˜ํ˜• ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ํ˜น์‹œ Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์žˆ๋Š”์ง€ isnull().values.any()๋กœ๋„ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. data.isnull().values.any() False False๋Š” ๋ฐ์ดํ„ฐ์— ๋ณ„๋„์˜ Null ๊ฐ’์€ ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. nunique()๋ฅผ ํ†ตํ•ด ์ƒ˜ํ”Œ ์ค‘ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print('์ค‘๋ณต์„ ์ œ์™ธํ•œ ์ƒ˜ํ”Œ์˜ ์ˆ˜ : {}'.format(data['email'].nunique())) print('์ค‘๋ณต์„ ์ œ์™ธํ•œ ์ฃผ์ œ์˜ ์ˆ˜ : {}'.format(data['target'].nunique())) ์ค‘๋ณต์„ ์ œ์™ธํ•œ ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 11314 ์ค‘๋ณต์„ ์ œ์™ธํ•œ ์ฃผ์ œ์˜ ์ˆ˜ : 20 ๋ ˆ์ด๋ธ” ๊ฐ’์˜ ๋ถ„ํฌ๋ฅผ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. data['target'].value_counts().plot(kind='bar'); 10๋ฒˆ ๋ ˆ์ด๋ธ”์˜ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ๊ณ , 19๋ฒˆ ๋ ˆ์ด๋ธ”์˜ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ ์œผ๋ฉฐ ๋Œ€์ฒด์ ์œผ๋กœ 400 ~ 600๊ฐœ ์‚ฌ์ด์˜ ๋ถ„ํฌ๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ฐ ๋ ˆ์ด๋ธ”์ด ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ๊ตฌ์ฒด์ ์ธ ์ˆ˜์น˜๋กœ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(data.groupby('target').size().reset_index(name='count')) target count 0 0 480 1 1 584 2 2 591 3 3 590 4 4 578 5 5 593 6 6 585 7 7 594 8 8 598 9 9 597 10 10 600 11 11 595 12 12 591 13 13 594 14 14 593 15 15 599 16 16 546 17 17 564 18 18 465 19 19 377 ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค์‹œ ๋ฉ”์ผ ๋ณธ๋ฌธ๊ณผ ๋ ˆ์ด๋ธ”์„ ๋ถ„๋ฆฌํ•˜๊ณ , ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋˜ํ•œ ๋ถˆ๋Ÿฌ์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค. subset์— 'test'๋ฅผ ๊ธฐ์žฌํ•˜๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋ณธ๋ฌธ๊ณผ ๋ ˆ์ด๋ธ”์„ ๊ฐ๊ฐ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. newsdata_test = fetch_20newsgroups(subset='test', shuffle=True) train_email = data['email'] train_label = data['target'] test_email = newsdata_test.data test_label = newsdata_test.target ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ ์ค€๋น„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์˜ ํ† ํฌ ๋‚˜์ด์ € ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. vocab_size = 10000 num_classes = 20 ์šฐ์„  ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋“ค์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. vocab_size๋Š” ์ด๋ฒˆ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•  ์ตœ๋Œ€ ๋‹จ์–ด ๊ฐœ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ๋’ค์—์„œ ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋นˆ๋„์ˆ˜ ์ˆœ์œผ๋กœ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜๋ฏ€๋กœ, ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์ƒ์œ„ vocab_size ๊ฐœ์ˆ˜๋งŒํผ์˜ ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. def prepare_data(train_data, test_data, mode): # ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜ tokenizer = Tokenizer(num_words = vocab_size) # vocab_size ๊ฐœ์ˆ˜๋งŒํผ์˜ ๋‹จ์–ด๋งŒ ์‚ฌ์šฉํ•œ๋‹ค. tokenizer.fit_on_texts(train_data) X_train = tokenizer.texts_to_matrix(train_data, mode=mode) # ์ƒ˜ํ”Œ ์ˆ˜ ร— vocab_size ํฌ๊ธฐ์˜ ํ–‰๋ ฌ ์ƒ์„ฑ X_test = tokenizer.texts_to_matrix(test_data, mode=mode) # ์ƒ˜ํ”Œ ์ˆ˜ ร— vocab_size ํฌ๊ธฐ์˜ ํ–‰๋ ฌ ์ƒ์„ฑ return X_train, X_test, tokenizer.index_word ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋กœ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜์ธ prepare_data๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ†ตํ•ด ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , ์•ž์„œ ๋ฐฐ์šด texts_to_matrix()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 'binary', 'count', 'tfidf', 'freq' 4๊ฐœ์˜ ๋ชจ๋“œ ์ค‘ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•œ ๋ชจ๋“œ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. X_train, X_test, index_to_word = prepare_data(train_email, test_email, 'binary') # binary ๋ชจ๋“œ๋กœ ๋ณ€ํ™˜ y_train = to_categorical(train_label, num_classes) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ y_test = to_categorical(test_label, num_classes) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ๋ฉ”์ผ ๋ณธ๋ฌธ์— ๋Œ€ํ•ด์„œ๋Š” 'binary' ๋ชจ๋“œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”์€ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ณธ๋ฌธ์˜ ํฌ๊ธฐ : {}'.format(X_train.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_train.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ณธ๋ฌธ์˜ ํฌ๊ธฐ : {}'.format(X_test.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_test.shape)) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ณธ๋ฌธ์˜ ํฌ๊ธฐ : (11314, 10000) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (11314, 20) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ณธ๋ฌธ์˜ ํฌ๊ธฐ : (7532, 10000) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (7532, 20) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ชจ๋‘ ๋ฉ”์ผ ๋ณธ๋ฌธ์˜ ํฌ๊ธฐ๊ฐ€ ์ƒ˜ํ”Œ์˜ ์ˆ˜ ร— 10,000์˜ ํ–‰๋ ฌ๋กœ ๋ณ€ํ™˜๋˜์—ˆ๋Š”๋ฐ, ์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ 10,000์ธ ๊ฒƒ์€ ์œ„์˜ prepard_data ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ Tokenizer์˜ num_words์˜ ์ธ์ž๋กœ vocab_size๋ฅผ ์ง€์ •ํ•ด ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋‹จ์–ด์˜ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€๋งŒ, ํ–‰๋ ฌ์˜ ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ 0๋ฒˆ ์ธ๋ฑ์Šค๋Š” ์‚ฌ์šฉ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‹ค์ œ๋กœ ํ–‰๋ ฌ์—๋Š” ๋นˆ๋„์ˆ˜ ๊ธฐ์ค€ ์ƒ์œ„ 9,999๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ํ‘œํ˜„๋œ ์…ˆ์ž…๋‹ˆ๋‹ค. ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 1๋ฒˆ ๋‹จ์–ด์™€ 9,999๋ฒˆ ๋‹จ์–ด๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('๋นˆ๋„์ˆ˜ ์ƒ์œ„ 1๋ฒˆ ๋‹จ์–ด : {}'.format(index_to_word[1])) print('๋นˆ๋„์ˆ˜ ์ƒ์œ„ 9999๋ฒˆ ๋‹จ์–ด : {}'.format(index_to_word[9999])) ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 1๋ฒˆ ๋‹จ์–ด : the ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 9999๋ฒˆ ๋‹จ์–ด : mic ๋ถˆ์šฉ์–ด์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด 'the'๊ฐ€ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 1๋ฒˆ ๋‹จ์–ด๊ฐ€ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก (Multilayer Perceptron, MLP)์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ๋ชจ๋ธ ์„ค๊ณ„์— ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. def fit_and_evaluate(X_train, y_train, X_test, y_test): model = Sequential() model.add(Dense(256, input_shape=(vocab_size,), activation='relu')) model.add(Dropout(0.5)) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X_train, y_train, batch_size=128, epochs=5, verbose=1, validation_split=0.1) score = model.evaluate(X_test, y_test, batch_size=128, verbose=0) return score[1] ๋ชจ๋ธ ์„ค๊ณ„๋ฅผ fit_and_evaluate๋ผ๋Š” ํ•จ์ˆ˜ ๋‚ด์— ์ •์˜ํ•˜์˜€๋Š”๋ฐ, ๋ชจ๋ธ์„ ํ•จ์ˆ˜ ๋‚ด์— ์ •์˜ํ•œ ์ด์œ ๋Š” ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์ž…๋ ฅ๊ฐ’์„ ๋ฐ”๊ฟ”๊ฐ€๋ฉด์„œ ๋ชจ๋ธ์„ ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ์šฐ์„ ์€ ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜์— ์ง‘์ค‘ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ํ˜„์žฌ ์„ค๊ณ„ํ•œ ์‹ ๊ฒฝ๋ง์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ˜„์žฌ ์„ค๊ณ„ํ•œ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์ด 4๊ฐœ์˜ ์ธต์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. vocab_size์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ ์ž…๋ ฅ์ธต, 256๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ๊ฐ€์ง„ ์ฒซ ๋ฒˆ์งธ ์€๋‹‰์ธต, 128๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ๊ฐ€์ง„ ๋‘ ๋ฒˆ์งธ ์€๋‹‰์ธต, num_classes์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ ์ถœ๋ ฅ์ธต์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด๋ฒˆ์— ์„ค๊ณ„ํ•œ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์€๋‹‰์ธต์ด 2๊ฐœ์ด๋ฏ€๋กœ ๊นŠ์€ ์‹ ๊ฒฝ๋ง(Deep Neural Network, DNN)์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ๋กœ ๋Œ์•„๊ฐ€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ๋ชจ๋ธ์—์„œ๋Š” ๊ณผ์ ํ•ฉ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ๋‘ ๋ฒˆ์˜ ๋“œ๋กญ์•„์›ƒ(Dropout)์„ ์ ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ํ•˜๋‚˜์˜ ์„ ํƒ์ง€๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ์ธ๋ฐ, ์ด ๊ฒฝ์šฐ 20๊ฐœ์˜ ์ฃผ์ œ ์ค‘์—์„œ ๋ชจ๋ธ์€ ์ž์‹ ์ด ์ •๋‹ต์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š” 1๊ฐœ์˜ ์ฃผ์ œ๋ฅผ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ด๋ฏ€๋กœ ์ถœ๋ ฅ์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ๋Š” ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ(categorical_crossentropy) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์•ž์„œ ๋ฐฐ์šด texts_to_matrix()์˜ 4๊ฐœ์˜ ๋ชจ๋“œ์— ๋Œ€ํ•ด์„œ ์ „๋ถ€ ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. modes = ['binary', 'count', 'tfidf', 'freq'] # 4๊ฐœ์˜ ๋ชจ๋“œ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ. for mode in modes: # 4๊ฐœ์˜ ๋ชจ๋“œ์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ ์•„๋ž˜์˜ ์ž‘์—…์„ ๋ฐ˜๋ณตํ•œ๋‹ค. X_train, X_test, _ = prepare_data(train_email, test_email, mode) # ๋ชจ๋“œ์— ๋”ฐ๋ผ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌ score = fit_and_evaluate(X_train, y_train, X_test, y_test) # ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๊ณ  ํ‰๊ฐ€. print(mode+' ๋ชจ๋“œ์˜ ํ…Œ์ŠคํŠธ ์ •ํ™•๋„:', score) ๊ฐ ๋ชจ๋“œ์— ๋Œ€ํ•ด์„œ ์ด 5ํšŒ์˜ ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ, ๊ฐ ๋ชจ๋“œ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค. binary ๋ชจ๋“œ์˜ ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.8312533 count ๋ชจ๋“œ์˜ ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.8239511 tfidf ๋ชจ๋“œ์˜ ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.8381572 freq ๋ชจ๋“œ์˜ ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.6902549 ๋Œ€์ฒด์ ์œผ๋กœ 82% ~ 83%์˜ ๋น„์Šทํ•œ ์ •ํ™•๋„๋ฅผ ๋ณด์ด๋Š”๋ฐ, 'freq' ๋ชจ๋“œ์—์„œ๋งŒ ์ •ํ™•๋„๊ฐ€ 69%๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๋ž˜๋„ 'freq' ๋ชจ๋“œ๋Š” ์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ์ ์ ˆํ•œ ์ „์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 07-12 ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ(Neural Network Language Model, NNLM) ํŒŒ์ด์ฌ ๋“ฑ๊ณผ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ช…์„ธ๋ผ ์žˆ๋Š” ํŠœํ”Œ, ํด๋ž˜์Šค ๋“ฑ๊ณผ ๊ฐ™์€ ์šฉ์–ด์™€ ์ž‘์„ฑํ•  ๋•Œ ์ง€์ผœ์•ผ ํ•˜๋Š” ๋ฌธ๋ฒ•์„ ๋ฐ”ํƒ•์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ๋ฒ•์— ๋งž์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ๋ช…์„ธ๋œ ๊ทœ์น™์„ ์ง€ํ‚ค๋Š” ๊ฒƒ์€ ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค. ์ž์—ฐ์–ด๋Š” ์–ด๋–จ๊นŒ์š”? ์ž์—ฐ์–ด์—๋„ ๋ฌธ๋ฒ•์ด๋ผ๋Š” ๊ทœ์น™์ด ์žˆ๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ๋งŽ์€ ์˜ˆ์™ธ ์‚ฌํ•ญ, ์‹œ๊ฐ„์— ๋”ฐ๋ฅธ ์–ธ์–ด์˜ ๋ณ€ํ™”, ์ค‘์˜์„ฑ๊ณผ ๋ชจํ˜ธ์„ฑ ๋ฌธ์ œ ๋“ฑ์„ ์ „๋ถ€ ๋ช…์„ธํ•˜๊ธฐ๋ž€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ์ž์—ฐ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋„๋ก ๊ทœ์น™์œผ๋กœ ๋ช…์„ธํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์šด ์ƒํ™ฉ์—์„œ ๋Œ€์•ˆ์€ ๊ทœ์น™ ๊ธฐ๋ฐ˜ ์ ‘๊ทผ์ด ์•„๋‹Œ ๊ธฐ๊ณ„๊ฐ€ ์ฃผ์–ด์ง„ ์ž์—ฐ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ณผ๊ฑฐ์—๋Š” ๊ธฐ๊ณ„๊ฐ€ ์ž์—ฐ์–ด๋ฅผ ํ•™์Šตํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ†ต๊ณ„์ ์ธ ์ ‘๊ทผ์„ ์‚ฌ์šฉํ–ˆ์œผ๋‚˜, ์ตœ๊ทผ์—๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ์–ป๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒˆ์—ญ๊ธฐ, ์Œ์„ฑ ์ธ์‹ ๋“ฑ๊ณผ ๊ฐ™์ด ์ž์—ฐ์–ด ์ƒ์„ฑ(Natural Language Generation, NLG)์˜ ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด ๋ชจ๋ธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ(Statistical Language Model, SLM)์—์„œ ๋‹ค์–‘ํ•œ ๊ตฌ์กฐ์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์‚ฌ์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ๋“ค๋กœ ๋Œ€์ฒด๋˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ์˜ ์‹œ์ดˆ์ธ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ(Feed Forward Neural Network Language Model)์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํžˆ ์ค„์—ฌ NNLM์ด๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๋’ค์—์„œ RNNLM, BiLM ๋“ฑ ๋ณด๋‹ค ๋ฐœ์ „๋œ ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ๋“ค์„ ๋ฐฐ์›๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์ œ์•ˆ๋˜์—ˆ์„ ๋‹น์‹œ NPLM(Neural Probabilistic Language Model)์ด๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ–๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 1. ๊ธฐ์กด N-gram ์–ธ์–ด ๋ชจ๋ธ์˜ ํ•œ๊ณ„ ์–ธ์–ด ๋ชจ๋ธ์€ ๋ฌธ์žฅ์— ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๋Š” ๋ชจ๋ธ์ด๋ฉฐ, ์ฃผ์–ด์ง„ ๋ฌธ๋งฅ์œผ๋กœ๋ถ€ํ„ฐ ์•„์ง ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์„ ์–ธ์–ด ๋ชจ๋ธ๋ง์ด๋ผ๊ณ  ํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ๋ง(Language Modeling)์˜ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. # ๋‹ค์Œ ๋‹จ์–ด ์˜ˆ์ธกํ•˜๊ธฐ An adorable little boy is spreading ____ ์œ„๋ฌธ์žฅ์„ ๊ฐ€์ง€๊ณ  ์•ž์„œ ๋ฐฐ์šด n-gram ์–ธ์–ด ๋ชจ๋ธ์ด ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณต์Šตํ•ด ๋ด…์‹œ๋‹ค. n-gram ์–ธ์–ด ๋ชจ๋ธ์€ ์–ธ์–ด ๋ชจ๋ธ๋ง์— ๋ฐ”๋กœ ์•ž n-1๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ์ฐธ๊ณ ํ•ฉ๋‹ˆ๋‹ค. 4-gram ์–ธ์–ด ๋ชจ๋ธ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ๋ชจ๋ธ์€ ๋ฐ”๋กœ ์•ž 3๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์ฐธ๊ณ ํ•˜๋ฉฐ ๋” ์•ž์˜ ๋‹จ์–ด๋“ค์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ ๋‹ค์Œ ๋‹จ์–ด ์˜ˆ์ธก์— ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์–ด๋Š” boy, is, spreading์ž…๋‹ˆ๋‹ค. ( |boy is spreading ) count(boy is spreading w ) count(boy is spreading) ๊ทธ ํ›„์—๋Š” ํ›ˆ๋ จ ์ฝ”ํผ์Šค์—์„œ (n-1)-gram์„ ์นด์šดํŠธํ•œ ๊ฒƒ์„ ๋ถ„๋ชจ๋กœ, n-gram์„ ์นด์šดํŠธํ•œ ๊ฒƒ์„ ๋ถ„์ž๋กœ ํ•˜์—ฌ ๋‹ค์Œ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅ ํ™•๋ฅ ์„ ์˜ˆ์ธกํ–ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ boy is spreading๊ฐ€ 1,000๋ฒˆ, boy is spreading insults๊ฐ€ 500๋ฒˆ, boy is spreading smiles๊ฐ€ 200๋ฒˆ ๋“ฑ์žฅํ–ˆ๋‹ค๋ฉด ๊ฐ ํ™•๋ฅ ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( insults|boy is spreading ) 0.500 ( smiles|boy is spreading ) 0.200 ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ n-gram ์–ธ์–ด ๋ชจ๋ธ์€ ์ถฉ๋ถ„ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€์ธกํ•˜์ง€ ๋ชปํ•˜๋ฉด ์–ธ์–ด๋ฅผ ์ •ํ™•ํžˆ ๋ชจ๋ธ๋ง ํ•˜์ง€ ๋ชปํ•˜๋Š” ํฌ์†Œ ๋ฌธ์ œ(sparsity problem) ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ›ˆ๋ จ ์ฝ”ํผ์Šค์— ' boy is spreading smile '๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด n-gram ์–ธ์–ด ๋ชจ๋ธ์—์„œ ํ•ด๋‹น ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ  ( smiles|boy is spreading ) ๋Š” 0์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์–ธ์–ด ๋ชจ๋ธ์ด ํŒ๋‹จํ•˜๊ธฐ์— boy is spreading ๋‹ค์Œ์—๋Š” smiles ์ด๋ž€ ๋‹จ์–ด๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์—†๋‹ค๋Š” ์˜๋ฏธ์ด์ง€๋งŒ ํ•ด๋‹น ๋‹จ์–ด ์‹œํ€€์Šค๋Š” ํ˜„์‹ค์—์„œ ์กด์žฌ ๊ฐ€๋Šฅํ•œ ์‹œํ€€์Šค์ด๋ฏ€๋กœ ์ ์ ˆํ•œ ๋ชจ๋ธ๋ง์ด ์•„๋‹™๋‹ˆ๋‹ค. 2. ๋‹จ์–ด์˜ ์˜๋ฏธ์  ์œ ์‚ฌ์„ฑ ํฌ์†Œ ๋ฌธ์ œ๋Š” ๊ธฐ๊ณ„๊ฐ€ ๋‹จ์–ด์˜ ์˜๋ฏธ์  ์œ ์‚ฌ์„ฑ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ์‚ฌ๋žŒ์˜ ์‚ฌ๋ก€๋ฅผ ๋“ค์–ด ์ด์•ผ๊ธฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ €์ž๋Š” ์ตœ๊ทผ 'ํ†บ์•„๋ณด๋‹ค'๋ผ๋Š” ์ƒ์†Œํ•œ ๋‹จ์–ด๋ฅผ ๋ฐฐ์› ๊ณ , 'ํ†บ์•„๋ณด๋‹ค'๊ฐ€ '์ƒ…์ƒ…์ด ์‚ดํŽด๋ณด๋‹ค'์™€ ์œ ์‚ฌํ•œ ์˜๋ฏธ์ž„์„ ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  '๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ์‚ดํŽด๋ณด๋‹ค'๋ผ๋Š” ํ‘œํ˜„ ๋Œ€์‹  '๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ํ†บ์•„๋ณด๋‹ค'๋ผ๋Š” ํ‘œํ˜„์„ ์จ๋ดค์Šต๋‹ˆ๋‹ค. ์ €๋Š” '๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ํ†บ์•„๋ณด๋‹ค'๋ผ๋Š” ์˜ˆ๋ฌธ์„ ์–ด๋””์„œ ์ฝ์€ ์ ์€ ์—†์ง€๋งŒ ๋‘ ๋‹จ์–ด๊ฐ€ ์œ ์‚ฌํ•จ์„ ํ•™์Šตํ•˜์˜€์œผ๋ฏ€๋กœ ๋‹จ์–ด๋ฅผ ๋Œ€์‹  ์„ ํƒํ•˜์—ฌ ์ž์—ฐ์–ด ์ƒ์„ฑ์„ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. '๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ์‚ดํŽด๋ณด๋‹ค'๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๋Š” ์กด์žฌํ•˜์ง€๋งŒ, '๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ํ†บ์•„๋ณด๋‹ค'๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ฝ”ํผ์Šค๋ฅผ ํ•™์Šตํ•œ ์–ธ์–ด ๋ชจ๋ธ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์€ ์•„๋ž˜ ์„ ํƒ์ง€์—์„œ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ†บ์•„๋ณด๋‹ค ๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ( ํ†บ์•„๋ณด๋‹ค|๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ) ๋ƒ ๋ƒ ํ•˜๋‹ค ๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ( ๋ƒ ๋ƒ ํ•˜๋‹ค|๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ) ์ €์ž์˜ ๊ฒฝ์šฐ์—๋Š” '์‚ดํŽด๋ณด๋‹ค'์™€ 'ํ†บ์•„๋ณด๋‹ค'์˜ ์œ ์‚ฌ์„ฑ์„ ํ•™์Šตํ•˜์˜€๊ณ  ์ด๋ฅผ ๊ทผ๊ฑฐ๋กœ ๋‘ ์„ ํƒ์ง€ ์ค‘์—์„œ 'ํ†บ์•„๋ณด๋‹ค'๊ฐ€ ๋” ๋งž๋Š” ์„ ํƒ์ด๋ผ๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ n-gram ์–ธ์–ด ๋ชจ๋ธ์€ '๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ' ๋‹ค์Œ์— 'ํ†บ์•„๋ณด๋‹ค'๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ  ํ†บ์•„๋ณด๋‹ค ๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ( ํ†บ์•„๋ณด๋‹ค|๋ฐœํ‘œ ์ž๋ฃŒ๋ฅผ ๋ฅผ 0์œผ๋กœ ์—ฐ์‚ฐํ•ฉ๋‹ˆ๋‹ค. n-gram ์–ธ์–ด ๋ชจ๋ธ์€ '์‚ดํŽด๋ณด๋‹ค'์™€ 'ํ†บ์•„๋ณด๋‹ค'์˜ ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ์•Œ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์˜ˆ์ธก์— ๊ณ ๋ คํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์–ธ์–ด ๋ชจ๋ธ ๋˜ํ•œ ๋‹จ์–ด์˜ ์˜๋ฏธ์  ์œ ์‚ฌ์„ฑ์„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„ํ•œ๋‹ค๋ฉด, ํ›ˆ๋ จ ์ฝ”ํผ์Šค์— ์—†๋Š” ๋‹จ์–ด ์‹œํ€€์Šค์— ๋Œ€ํ•œ ์˜ˆ์ธก์ด๋ผ๋„ ์œ ์‚ฌํ•œ ๋‹จ์–ด๊ฐ€ ์‚ฌ์šฉ๋œ ๋‹จ์–ด ์‹œํ€€์Šค๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋ณด๋‹ค ์ •ํ™•ํ•œ ์˜ˆ์ธก์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ์•„์ด๋””์–ด๋ฅผ ๋ฐ˜์˜ํ•œ ์–ธ์–ด ๋ชจ๋ธ์ด ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ NNLM์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์•„์ด๋””์–ด๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒกํ„ฐ๋ฅผ ์–ป์–ด๋‚ด๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(word embedding)์˜ ์•„์ด๋””์–ด์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. NNLM์ด ์–ด๋–ป๊ฒŒ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค. 3. ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ(NNLM) NNLM์ด ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ํ•™์Šตํ•˜๋Š” ๊ณผ์ •์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ์œ„ํ•ด ๊ฐ„์†Œํ™”๋œ ํ˜•ํƒœ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฌธ : "what will the fat cat sit on" ์˜ˆ๋ฅผ ๋“ค์–ด ํ›ˆ๋ จ ์ฝ”ํผ์Šค์— ์œ„์™€ ๊ฐ™์€ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์€ ์ฃผ์–ด์ง„ ๋‹จ์–ด ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” 'what will the fat cat'์ด๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋ฉด, ๋‹ค์Œ ๋‹จ์–ด 'sit'์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ›ˆ๋ จ๋ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ์ฝ”ํผ์Šค๊ฐ€ ์ค€๋น„๋œ ์ƒํƒœ์—์„œ ๊ฐ€์žฅ ๋จผ์ € ํ•ด์•ผ ํ•  ์ผ์€ ๊ธฐ๊ณ„๊ฐ€ ๋‹จ์–ด๋ฅผ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ˆ˜์น˜ํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ์ฝ”ํผ์Šค์— 7๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ ์œ„ ๋‹จ์–ด๋“ค์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. what = [1, 0, 0, 0, 0, 0, 0] will = [0, 1, 0, 0, 0, 0, 0] the = [0, 0, 1, 0, 0, 0, 0] fat = [0, 0, 0, 1, 0, 0, 0] cat = [0, 0, 0, 0, 1, 0, 0] sit = [0, 0, 0, 0, 0, 1, 0] on = [0, 0, 0, 0, 0, 0, 1] ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ์ธ 7์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์›-ํ•ซ ๋ฒกํ„ฐ๋“ค์ด ํ›ˆ๋ จ์„ ์œ„ํ•œ NNLM์˜ ์ž…๋ ฅ์ด๋ฉด์„œ ์˜ˆ์ธก์„ ์œ„ํ•œ ๋ ˆ์ด๋ธ”์ด ๋ฉ๋‹ˆ๋‹ค. 'what will the fat cat'๋ฅผ ์ž…๋ ฅ์„ ๋ฐ›์•„์„œ 'sit'์„ ์˜ˆ์ธกํ•˜๋Š” ์ผ์€ ๊ธฐ๊ณ„์—๊ฒŒ what, will, the, fat, cat์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ sit์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. NNLM์€ n-gram ์–ธ์–ด ๋ชจ๋ธ์ฒ˜๋Ÿผ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•  ๋•Œ, ์•ž์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ •ํ•ด์ง„ ๊ฐœ์ˆ˜์˜ ๋‹จ์–ด๋งŒ์„ ์ฐธ๊ณ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐœ์ˆ˜๋ฅผ n์ด๋ผ๊ณ  ํ•˜๊ณ  n์„ 4๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋•Œ, ์–ธ์–ด ๋ชจ๋ธ์€ 'what will the fat cat'๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ์•ž์˜ 4๊ฐœ ๋‹จ์–ด 'will the fat cat'๊นŒ์ง€๋งŒ ์ฐธ๊ณ ํ•˜๊ณ  ๊ทธ ์•ž ๋‹จ์–ด์ธ what์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฒ”์œ„๋ฅผ ์œˆ๋„(window)๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ์œˆ๋„์˜ ํฌ๊ธฐ์ธ n์€ 4์ž…๋‹ˆ๋‹ค. NNLM์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. NNLM์€ ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ด 4๊ฐœ์˜ ์ธต(layer)์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ์ธต(input layer)์„ ๋ณด๋ฉด ์•ž์—์„œ ์œˆ๋„์˜ ํฌ๊ธฐ๋Š” 4๋กœ ์ •ํ•˜์˜€์œผ๋ฏ€๋กœ ์ž…๋ ฅ์€ 4๊ฐœ์˜ ๋‹จ์–ด 'will, the, fat, cat'์˜ ์›-ํ•ซ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต(output layer)์„ ๋ณด๋ฉด ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ์ •๋‹ต์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด sit์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•œ ๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋ ˆ์ด๋ธ”๋กœ์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜ค์ฐจ๋กœ๋ถ€ํ„ฐ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ํ•™์Šต์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๋”ฐ๋ผ๊ฐ€๋ด…์‹œ๋‹ค. 4๊ฐœ์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์€ NNLM์€ ๋‹ค์Œ์ธต์ธ ํˆฌ์‚ฌ์ธต(projection layer)์„ ์ง€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต ์‚ฌ์ด์˜ ์ธต์€ ๋ณดํ†ต ์€๋‹‰์ธต์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ํˆฌ์‚ฌ์ธต์ด๋ผ๊ณ  ๋ช…๋ช…ํ•œ ์ด ์ธต์€ ์ผ๋ฐ˜ ์€๋‹‰์ธต๊ณผ ๋‹ค๋ฅด๊ฒŒ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ์˜ ๊ณฑ์…ˆ์€ ์ด๋ฃจ์–ด์ง€์ง€๋งŒ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํˆฌ์‚ฌ์ธต์˜ ํฌ๊ธฐ๋ฅผ M์œผ๋กœ ์„ค์ •ํ•˜๋ฉด, ๊ฐ ์ž…๋ ฅ ๋‹จ์–ด๋“ค์€ ํˆฌ์‚ฌ์ธต์—์„œ V ร— M ํฌ๊ธฐ์˜ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ ๊ณฑํ•ด์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ V๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 7์ด๊ณ , M์ด 5๋ผ๋ฉด ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W๋Š” 7 ร— 5 ํ–‰๋ ฌ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ๊ฐ€์ค‘์น˜ W ํ–‰๋ ฌ์˜ ๊ณฑ์ด ์–ด๋–ป๊ฒŒ ์ด๋ฃจ์–ด์ง€๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ๋Š” ๊ฐ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋กœ ํ‘œ๊ธฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด i ๋ฒˆ์งธ ์ธ๋ฑ์Šค์— 1์ด๋ผ๋Š” ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๊ทธ ์™ธ์˜ 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ๊ฐ€์ค‘์น˜ W ํ–‰๋ ฌ์˜ ๊ณฑ์€ ์‚ฌ์‹ค W ํ–‰๋ ฌ์˜ i๋ฒˆ์งธ ํ–‰์„ ๊ทธ๋Œ€๋กœ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ๊ณผ(lookup) ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ์ž‘์—…์„ ๋ฃฉ์—… ํ…Œ์ด๋ธ”(lookup table)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฃฉ์—… ํ…Œ์ด๋ธ” ํ›„์—๋Š” V ์ฐจ์›์„ ๊ฐ€์ง€๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ์ด๋ณด๋‹ค ๋” ์ฐจ์›์ด ์ž‘์€ M ์ฐจ์›์˜ ๋ฒกํ„ฐ๋กœ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ๋‹จ์–ด fat์„ ์˜๋ฏธํ•˜๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ f t ์œผ๋กœ ํ‘œํ˜„ํ–ˆ๊ณ , ํ…Œ์ด๋ธ” ๋ฃฉ์—… ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„์˜ ๋‹จ์–ด ๋ฒกํ„ฐ๋Š” f t ์œผ๋กœ ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฒกํ„ฐ๋“ค์€ ์ดˆ๊ธฐ์—๋Š” ๋žœ๋ค ํ•œ ๊ฐ’์„ ๊ฐ€์ง€์ง€๋งŒ ํ•™์Šต ๊ณผ์ •์—์„œ ๊ฐ’์ด ๊ณ„์† ๋ณ€๊ฒฝ๋˜๋Š”๋ฐ ์ด ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(embedding vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด๊ฐ€ ํ…Œ์ด๋ธ” ๋ฃฉ ์—…์„ ํ†ตํ•ด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋ณ€๊ฒฝ๋˜๊ณ , ํˆฌ์‚ฌ์ธต์—์„œ ๋ชจ๋“  ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์˜ ๊ฐ’์€ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค(concatenate). ์—ฌ๊ธฐ์„œ ๋ฒกํ„ฐ์˜ ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์€ ๋ฒกํ„ฐ๋“ค์„ ์ด์–ด๋ถ™์ด๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, 5์ฐจ์› ๋ฒกํ„ฐ 4๊ฐœ๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค๋Š” ์˜๋ฏธ๋Š” 20์ฐจ์› ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š”๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.๋ฅผ ๊ฐ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ, NNLM์ด ์˜ˆ์ธกํ•˜๊ณ ์ž ํ•˜๋Š” ๋‹จ์–ด๊ฐ€ ๋ฌธ์žฅ์—์„œ ๋ฒˆ์งธ ๋‹จ์–ด๋ผ๊ณ  ํ•˜๊ณ , ์œˆ๋„์˜ ํฌ๊ธฐ๋ฅผ, ๋ฃฉ์—… ํ…Œ์ด๋ธ”์„ ์˜๋ฏธํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ o k p , ์„ธ๋ฏธ์ฝœ๋ก (;)์„ ์—ฐ๊ฒฐ ๊ธฐํ˜ธ๋กœ ํ•˜์˜€์„ ๋•Œ ํˆฌ์‚ฌ์ธต์„ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํˆฌ์‚ฌ์ธต : l y r ( o k p ( t n ) . . l o u ( t 2 ) l o u ( t 1 ) ) ( t n. . e โˆ’ ; t 1 ) ์ผ๋ฐ˜์ ์ธ ์€๋‹‰์ธต์ด ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋น„์„ ํ˜•์ธต(nonlinear layer)์ธ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ํˆฌ์‚ฌ์ธต์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์„ ํ˜•์ธต(linear layer)์ด๋ผ๋Š” ์ ์ด ๋‹ค์†Œ ์ƒ์†Œํ•˜์ง€๋งŒ, ์ด๋‹ค์Œ์€ ๋‹ค์‹œ ์€๋‹‰์ธต์„ ์‚ฌ์šฉํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ํˆฌ์‚ฌ์ธต์˜ ๊ฒฐ๊ณผ๋Š” h์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ์€๋‹‰์ธต์„ ์ง€๋‚ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์—์„œ ์€๋‹‰์ธต์„ ์ง€๋‚œ๋‹ค๋Š” ๊ฒƒ์€ ์€๋‹‰์ธต์˜ ์ž…๋ ฅ์€ ๊ฐ€์ค‘์น˜ ๊ณฑํ•ด์ง„ ํ›„ ํŽธํ–ฅ์ด ๋”ํ•ด์ ธ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์ด ๋œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ์˜ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ h b์ด๋ผ๊ณ  ํ•˜๊ณ , ์€๋‹‰์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์€๋‹‰์ธต์„ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์€๋‹‰์ธต : l y r t n ( h l y r b) ์€๋‹‰์ธต์˜ ์ถœ๋ ฅ์€ V์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ์ถœ๋ ฅ์ธต์œผ๋กœ ํ–ฅํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ๋‹ค์‹œ ๋˜ ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ง€๊ณ  ํŽธํ–ฅ์ด ๋”ํ•ด์ง€๋ฉด, ์ž…๋ ฅ์ด์—ˆ๋˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋“ค๊ณผ ๋™์ผํ•˜๊ฒŒ V ์ฐจ์›์˜ ๋ฒกํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 7์ด์—ˆ๋‹ค๋ฉด ํ•ด๋‹น ๋ฒกํ„ฐ๋„ ๋™์ผํ•œ ์ฐจ์› ์ˆ˜๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์—์„œ๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, V ์ฐจ์›์˜ ๋ฒกํ„ฐ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋ฉด์„œ ๋ฒกํ„ฐ์˜ ๊ฐ ์›์†Œ๋Š” 0๊ณผ 1์‚ฌ์ด์˜ ์‹ค์ˆซ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ์ดํ•ฉ์€ 1์ด ๋˜๋Š” ์ƒํƒœ๋กœ ๋ฐ”๋€๋‹ˆ๋‹ค. ์ด ๋ฒกํ„ฐ๋ฅผ NNLM์˜ ์˜ˆ์ธก๊ฐ’์ด๋ผ๋Š” ์˜๋ฏธ์—์„œ ^ ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต : ^ s f m x ( y l y r b) ๋ฒกํ„ฐ ^ ์˜ ๊ฐ ์ฐจ์› ์•ˆ์—์„œ์˜ ๊ฐ’์ด ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ^ ์˜ j ๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ€์ง„ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์€ j ๋ฒˆ์งธ ๋‹จ์–ด๊ฐ€ ๋‹ค์Œ ๋‹จ์–ด์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ^ ๋Š” ์‹ค์ œ ๊ฐ’. ์ฆ‰, ์‹ค์ œ ์ •๋‹ต์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด์ธ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๊ฐ’์— ๊ฐ€๊นŒ์›Œ์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’์— ํ•ด๋‹น๋˜๋Š” ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ์ด ๋‘ ๋ฒกํ„ฐ๊ฐ€ ๊ฐ€๊นŒ์›Œ์ง€๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ NNLM๋Š” ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ(cross-entropy) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ๋‹จ์–ด๋ผ๋Š” V ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ์ •๋‹ต์ธ 'sit'์„ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ญ์ „ํŒŒ๊ฐ€ ์ด๋ฃจ์–ด์ง€๋ฉด ๋ชจ๋“  ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๋“ค์ด ํ•™์Šต๋˜๋Š”๋ฐ, ์—ฌ๊ธฐ์—๋Š” ํˆฌ์‚ฌ์ธต์—์„œ์˜ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๋„ ํฌํ•จ๋˜๋ฏ€๋กœ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’ ๋˜ํ•œ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์˜ˆ์ œ์—์„œ๋Š” 7๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, ๋งŒ์•ฝ ์ถฉ๋ถ„ํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฐ€์ • ํ•˜์— NNLM์ด ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”? NNLM์˜ ํ•ต์‹ฌ์€ ์ถฉ๋ถ„ํ•œ ์–‘์˜ ํ›ˆ๋ จ ์ฝ”ํผ์Šค๋ฅผ ์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ํ•™์Šตํ•œ๋‹ค๋ฉด ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ˆ˜๋งŽ์€ ๋ฌธ์žฅ์—์„œ ์œ ์‚ฌํ•œ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์–ด๋“ค์€ ๊ฒฐ๊ตญ ์œ ์‚ฌํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์„ ์–ป๊ฒŒ ๋˜๋Š” ๊ฒƒ์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํ›ˆ๋ จ์ด ๋๋‚œ ํ›„ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธก ๊ณผ์ •์—์„œ (๋งˆ์น˜ ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์ €์ž์˜ 'ํ†บ์•„๋ณด๊ธฐ'์™€ ๊ฐ™์€ ์˜ˆ์‹œ์ฒ˜๋Ÿผ) ํ›ˆ๋ จ ์ฝ”ํผ์Šค์—์„œ ์—†๋˜ ๋‹จ์–ด ์‹œํ€€์Šค๋ผ ํ•˜๋”๋ผ๋„ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์•„์ด๋””์–ด๋Š” Word2Vec, FastText, GloVe ๋“ฑ์œผ๋กœ ๋ฐœ์ „๋˜์–ด์„œ ๋”ฅ ๋Ÿฌ๋‹ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ชจ๋ธ์—์„œ๋Š” ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ฑ•ํ„ฐ์—์„œ ์ข€ ๋” ์ž์„ธํžˆ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 4. NNLM์˜ ์ด์ ๊ณผ ํ•œ๊ณ„ NNLM์€ ๊ธฐ์กด n-gram ์–ธ์–ด ๋ชจ๋ธ์˜ ํ•œ๊ณ„๋ฅผ ๊ฐœ์„ ํ•˜์˜€์ง€๋งŒ ์—ฌ์ „ํžˆ ๊ฐ€์ง€๋Š” ๋ฌธ์ œ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 1) ๊ธฐ์กด ๋ชจ๋ธ์—์„œ์˜ ๊ฐœ์„ ์  NNLM์€ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ํ†ตํ•ด ํฌ์†Œ ๋ฌธ์ œ(sparsity problem)๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. 2) ๊ณ ์ •๋œ ๊ธธ์ด์˜ ์ž…๋ ฅ(Fixed-length input) NNLM์ด ๊ทน๋ณตํ•˜์ง€ ๋ชปํ•œ ํ•œ๊ณ„ ๋˜ํ•œ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. NNLM์€ n-gram ์–ธ์–ด ๋ชจ๋ธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ์ด์ „ ๋‹จ์–ด๋ฅผ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ •ํ•ด์ง„ n ๊ฐœ์˜ ๋‹จ์–ด๋งŒ์„ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•œ๊ณ„๋ฅผ ๊ทน๋ณตํ•  ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด ๋ชจ๋ธ์ด ์žˆ๋Š”๋ฐ, ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋  RNN(Recurrent Neural Network)์„ ์‚ฌ์šฉํ•œ RNN ์–ธ์–ด ๋ชจ๋ธ(Recurrent Neural Network Language Model, RNNLM)์ž…๋‹ˆ๋‹ค. 08. ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network) ์•ž์„œ ๋ฐฐ์šด ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์€ ์ž…๋ ฅ์˜ ๊ธธ์ด๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ์–ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์‹ ๊ฒฝ๋ง์œผ๋กœ๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๋‹ค์–‘ํ•œ ๊ธธ์ด์˜ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ํ•„์š”ํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ๋ฐ”๋กœ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network, RNN)์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์ธ ๋ฐ”๋‹๋ผ RNN, ์ด๋ฅผ ๊ฐœ์„ ํ•œ LSTM, GRU์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ด ๋ด…์‹œ๋‹ค. LSTM๊ณผ GRU๋ฅผ ์ดํ•ดํ•œ๋‹ค๋ฉด ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋‚˜ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ฌธ์ œ๋“ค์„ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 08-01 ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network, RNN) RNN(Recurrent Neural Network)์€ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์‹œํ€€์Šค ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์‹œํ€€์Šค(Sequence) ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋ฒˆ์—ญ๊ธฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์ž…๋ ฅ์€ ๋ฒˆ์—ญํ•˜๊ณ ์ž ํ•˜๋Š” ๋‹จ์–ด์˜ ์‹œํ€€์Šค์ธ ๋ฌธ์žฅ์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์— ํ•ด๋‹น๋˜๋Š” ๋ฒˆ์—ญ๋œ ๋ฌธ์žฅ ๋˜ํ•œ ๋‹จ์–ด์˜ ์‹œํ€€์Šค์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์‹œํ€€์Šค๋“ค์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ๋ชจ๋ธ๋“ค์„ ์‹œํ€€์Šค ๋ชจ๋ธ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ์ค‘ RNN์€ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์‹œํ€€์Šค ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ฐฐ์šฐ๋Š” LSTM์ด๋‚˜ GRU ๋˜ํ•œ ๊ทผ๋ณธ์ ์œผ๋กœ RNN์— ์†ํ•ฉ๋‹ˆ๋‹ค. RNN์„ ์ดํ•ดํ•˜๊ณ  'RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜' ์ฑ•ํ„ฐ, 'ํƒœ๊น… ์ž‘์—…' ์ฑ•ํ„ฐ, 'RNN์„ ์ด์šฉํ•œ ์ธ์ฝ”๋”-๋””์ฝ”๋”' ์ฑ•ํ„ฐ์—์„œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์šฉ์–ด๋Š” ๋น„์Šทํ•˜์ง€๋งŒ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง๊ณผ ์žฌ๊ท€ ์‹ ๊ฒฝ๋ง(Recursive Neural Network)์€ ์ „ํ˜€ ๋‹ค๋ฅธ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. 1. ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network, RNN) ์•ž์„œ ๋ฐฐ์šด ์‹ ๊ฒฝ๋ง๋“ค์€ ์ „๋ถ€ ์€๋‹‰์ธต์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ๊ฐ’์€ ์˜ค์ง ์ถœ๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ํ–ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ์‹ ๊ฒฝ๋ง๋“ค์„ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง(Feed Forward Neural Network)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ทธ๋ ‡์ง€ ์•Š์€ ์‹ ๊ฒฝ๋ง๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. RNN(Recurrent Neural Network) ๋˜ํ•œ ๊ทธ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. RNN์€ ์€๋‹‰์ธต์˜ ๋…ธ๋“œ์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ๋„ ๋ณด๋‚ด๋ฉด์„œ, ๋‹ค์‹œ ์€๋‹‰์ธต ๋…ธ๋“œ์˜ ๋‹ค์Œ ๊ณ„์‚ฐ์˜ ์ž…๋ ฅ์œผ๋กœ ๋ณด๋‚ด๋Š” ํŠน์ง•์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.๋Š” ์ž…๋ ฅ์ธต์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ,๋Š” ์ถœ๋ ฅ์ธต์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋Š” ํŽธํ–ฅ ๋„ ์ž…๋ ฅ์œผ๋กœ ์กด์žฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์•ž์œผ๋กœ์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. RNN์—์„œ ์€๋‹‰์ธต์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ์—ญํ• ์„ ํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์…€(cell)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ์…€์€ ์ด์ „์˜ ๊ฐ’์„ ๊ธฐ์–ตํ•˜๋ ค๊ณ  ํ•˜๋Š” ์ผ์ข…์˜ ๋ฉ”๋ชจ๋ฆฌ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์ด๋ฅผ ๋ฉ”๋ชจ๋ฆฌ ์…€ ๋˜๋Š” RNN ์…€์ด๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ ๊ฐ๊ฐ์˜ ์‹œ์ (time step)์—์„œ ๋ฐ”๋กœ ์ด์ „ ์‹œ์ ์—์„œ์˜ ์€๋‹‰์ธต์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์—์„œ ๋‚˜์˜จ ๊ฐ’์„ ์ž์‹ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์žฌ๊ท€์  ํ™œ๋™์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ๋Š” ํ˜„์žฌ ์‹œ์ ์„ ๋ณ€์ˆ˜ t๋กœ ํ‘œํ˜„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ˜„์žฌ ์‹œ์  t์—์„œ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ด ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ’์€ ๊ณผ๊ฑฐ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€๋“ค์˜ ๊ฐ’์— ์˜ํ–ฅ์„ ๋ฐ›์€ ๊ฒƒ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ด ๊ฐ–๊ณ  ์žˆ๋Š” ์ด ๊ฐ’์€ ๋ญ๋ผ๊ณ  ๋ถ€๋ฅผ๊นŒ์š”? ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ด ์ถœ๋ ฅ์ธต ๋ฐฉํ–ฅ ๋˜๋Š” ๋‹ค์Œ ์‹œ์ ์ธ t+1์˜ ์ž์‹ ์—๊ฒŒ ๋ณด๋‚ด๋Š” ๊ฐ’์„ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด t ์‹œ์ ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ t-1 ์‹œ์ ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ด ๋ณด๋‚ธ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ ๊ณ„์‚ฐ์„ ์œ„ํ•œ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. RNN์„ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์ขŒ์ธก๊ณผ ๊ฐ™์ด ํ™”์‚ดํ‘œ๋กœ ์‚ฌ์ดํด์„ ๊ทธ๋ ค์„œ ์žฌ๊ท€ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ์šฐ์ธก๊ณผ ๊ฐ™์ด ์‚ฌ์ดํด์„ ๊ทธ๋ฆฌ๋Š” ํ™”์‚ดํ‘œ ๋Œ€์‹  ์—ฌ๋Ÿฌ ์‹œ์ ์œผ๋กœ ํŽผ์ณ์„œ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๊ทธ๋ฆผ์€ ๋™์ผํ•œ ๊ทธ๋ฆผ์œผ๋กœ ๋‹จ์ง€ ์‚ฌ์ดํด์„ ๊ทธ๋ฆฌ๋Š” ํ™”์‚ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„ํ•˜์˜€๋Š๋ƒ, ์‹œ์ ์˜ ํ๋ฆ„์— ๋”ฐ๋ผ์„œ ํ‘œํ˜„ํ•˜์˜€๋Š๋ƒ์˜ ์ฐจ์ด์ผ ๋ฟ ๋‘˜ ๋‹ค ๋™์ผํ•œ RNN์„ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์—์„œ๋Š” ๋‰ด๋Ÿฐ์ด๋ผ๋Š” ๋‹จ์œ„๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, RNN์—์„œ๋Š” ๋‰ด๋Ÿฐ์ด๋ผ๋Š” ๋‹จ์œ„๋ณด๋‹ค๋Š” ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต์—์„œ๋Š” ๊ฐ๊ฐ ์ž…๋ ฅ ๋ฒกํ„ฐ์™€ ์ถœ๋ ฅ ๋ฒกํ„ฐ, ์€๋‹‰์ธต์—์„œ๋Š” ์€๋‹‰ ์ƒํƒœ๋ผ๋Š” ํ‘œํ˜„์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ํšŒ์ƒ‰๊ณผ ์ดˆ๋ก์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฐ ๋„ค๋ชจ๋“ค์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฒกํ„ฐ ๋‹จ์œ„๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง๊ณผ์˜ ์ฐจ์ด๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ RNN์„ ๋‰ด๋Ÿฐ ๋‹จ์œ„๋กœ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 4, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๊ฐ€ 2, ์ถœ๋ ฅ์ธต์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 2์ธ RNN์ด ์‹œ์ ์ด 2์ผ ๋•Œ์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋‰ด๋Ÿฐ ๋‹จ์œ„๋กœ ํ•ด์„ํ•˜๋ฉด ์ž…๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜๋Š” 4, ์€๋‹‰์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜๋Š” 2, ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜๋Š” 2์ž…๋‹ˆ๋‹ค. RNN์€ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ๊ธธ์ด๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ๊ธธ์ด์— ๋”ฐ๋ผ์„œ ๋‹ฌ๋ผ์ง€๋Š” RNN์˜ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ๊ตฌ์กฐ๊ฐ€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. RNN ์…€์˜ ๊ฐ ์‹œ์ ์˜ ์ž…, ์ถœ๋ ฅ์˜ ๋‹จ์œ„๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•˜๊ธฐ ๋‚˜๋ฆ„์ด์ง€๋งŒ ๊ฐ€์žฅ ๋ณดํŽธ์ ์ธ ๋‹จ์œ„๋Š” '๋‹จ์–ด ๋ฒกํ„ฐ'์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•˜๋‚˜์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ถœ๋ ฅ์„ ์˜๋ฏธํ•˜๋Š” ์ผ ๋Œ€๋‹ค(one-to-many) ๊ตฌ์กฐ์˜ ๋ชจ๋ธ์€ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์‚ฌ์ง„์˜ ์ œ๋ชฉ์„ ์ถœ๋ ฅํ•˜๋Š” ์ด๋ฏธ์ง€ ์บก์…”๋‹(Image Captioning) ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ง„์˜ ์ œ๋ชฉ์€ ๋‹จ์–ด๋“ค์˜ ๋‚˜์—ด์ด๋ฏ€๋กœ ์‹œํ€€์Šค ์ถœ๋ ฅ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‹จ์–ด ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ ํ•˜๋‚˜์˜ ์ถœ๋ ฅ์„ ํ•˜๋Š” ๋‹ค ๋Œ€ ์ผ(many-to-one) ๊ตฌ์กฐ์˜ ๋ชจ๋ธ์€ ์ž…๋ ฅ ๋ฌธ์„œ๊ฐ€ ๊ธ์ •์ ์ธ์ง€ ๋ถ€์ •์ ์ธ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ๊ฐ์„ฑ ๋ถ„๋ฅ˜(sentiment classification), ๋˜๋Š” ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜(spam detection) ๋“ฑ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ RNN์œผ๋กœ ์ŠคํŒธ ๋ฉ”์ผ์„ ๋ถ„๋ฅ˜ํ•  ๋•Œ์˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์˜ˆ์ œ๋“ค์€ 'RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜' ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. ๋‹ค ๋Œ€๋‹ค(many-to-many) ๊ตฌ์กฐ์˜ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ์—๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด ๋Œ€๋‹ต ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋Š” ์ฑ—๋ด‡๊ณผ ์ž…๋ ฅ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ๋ฒˆ์—ญ๋œ ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฒˆ์—ญ๊ธฐ, ๋˜๋Š” 'ํƒœ๊น… ์ž‘์—…' ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹์ด๋‚˜ ํ’ˆ์‚ฌ ํƒœ๊น…๊ณผ ๊ฐ™์€ ์ž‘์—…์ด ์†ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์ˆ˜ํ–‰ํ•  ๋•Œ์˜ RNN ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. RNN์— ๋Œ€ํ•œ ์ˆ˜์‹์„ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์‹œ์  t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ t ๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์€๋‹‰์ธต์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ t ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด ๋‘ ๊ฐœ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ์ž…๋ ฅ์ธต์„ ์œ„ํ•œ ๊ฐ€์ค‘์น˜ x ์ด๊ณ , ํ•˜๋‚˜๋Š” ์ด์ „ ์‹œ์  t-1์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์ธ t 1 ์„ ์œ„ํ•œ ๊ฐ€์ค‘์น˜ h ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์€๋‹‰์ธต : t t n ( x t W h โˆ’ + ) ์ถœ๋ ฅ์ธต : t f ( y t b ) ๋‹จ,๋Š” ๋น„์„ ํ˜• ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ค‘ ํ•˜๋‚˜. RNN์˜ ์€๋‹‰์ธต ์—ฐ์‚ฐ์„ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ RNN์˜ ์ž…๋ ฅ t ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋‹จ์–ด ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์„๋ผ๊ณ  ํ•˜๊ณ , ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ h ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ๊ฐ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. t ( ร— ) x ( h d ) h ( h D) t 1 ( h 1 ) : ( h 1 ) ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1์ด๊ณ , ์™€ h ๋‘ ๊ฐ’ ๋ชจ๋‘๋ฅผ 4๋กœ ๊ฐ€์ •ํ•˜์˜€์„ ๋•Œ, RNN์˜ ์€๋‹‰์ธต ์—ฐ์‚ฐ์„ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋•Œ t ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์ฃผ๋กœ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜(tanh)๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์‹์—์„œ ๊ฐ๊ฐ์˜ ๊ฐ€์ค‘์น˜ x W, y ์˜ ๊ฐ’์€ ํ•˜๋‚˜์˜ ์ธต์—์„œ๋Š” ๋ชจ๋“  ์‹œ์ ์—์„œ ๊ฐ’์„ ๋™์ผํ•˜๊ฒŒ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์€๋‹‰์ธต์ด 2๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ์—๋Š” ๊ฐ ์€๋‹‰์ธต์—์„œ์˜ ๊ฐ€์ค‘์น˜๋Š” ์„œ๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์€ ๊ฒฐ๊ณผ๊ฐ’์ธ t ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ํ‘ธ๋Š” ๋ฌธ์ œ์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅผ ํ…๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด ์ถœ๋ ฅ์ธต์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด ์ถœ๋ ฅ์ธต์— ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. ์ผ€๋ผ์Šค(Keras)๋กœ RNN ๊ตฌํ˜„ํ•˜๊ธฐ ์ผ€๋ผ์Šค๋กœ RNN ์ธต์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. from tensorflow.keras.layers import SimpleRNN model.add(SimpleRNN(hidden_units)) ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ถ”๊ฐ€ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ model.add(SimpleRNN(hidden_units, input_shape=(timesteps, input_dim))) # ๋‹ค๋ฅธ ํ‘œ๊ธฐ model.add(SimpleRNN(hidden_units, input_length=M, input_dim=N)) hidden_units = ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜. ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ด๋‹ค์Œ ์‹œ์ ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€๊ณผ ์ถœ๋ ฅ์ธต์œผ๋กœ ๋ณด๋‚ด๋Š” ๊ฐ’์˜ ํฌ๊ธฐ(output_dim)์™€๋„ ๋™์ผ. RNN์˜ ์šฉ๋Ÿ‰(capacity)์„ ๋Š˜๋ฆฐ๋‹ค๊ณ  ๋ณด๋ฉด ๋˜๋ฉฐ, ์ค‘์†Œํ˜• ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ ๋ณดํ†ต 128, 256, 512, 1024 ๋“ฑ์˜ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. timesteps = ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ธธ์ด(input_length)๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•จ. ์‹œ์ ์˜ ์ˆ˜. input_dim = ์ž…๋ ฅ์˜ ํฌ๊ธฐ. RNN ์ธต์€ (batch_size, timesteps, input_dim) ํฌ๊ธฐ์˜ 3D ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. batch_size๋Š” ํ•œ ๋ฒˆ์— ํ•™์Šตํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ํ…์„œ์˜ ๊ฐœ๋…์„ ๋ฐ˜๋“œ์‹œ ์ดํ•ดํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ ์ฑ•ํ„ฐ์˜ ํ…์„œ ์„ค๋ช…์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ด๋Ÿฌํ•œ ํ‘œํ˜„์€ ์‚ฌ๋žŒ์ด๋‚˜ ๋ฌธํ—Œ์— ๋”ฐ๋ผ์„œ, ๋˜๋Š” ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์— ๋”ฐ๋ผ์„œ ์ข…์ข… ๋‹ค๋ฅด๊ฒŒ ๊ธฐ์žฌ๋˜๋Š”๋ฐ์˜ ๊ทธ๋ฆผ์€ ๋ฌธ์ œ์™€ ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅด๊ฒŒ ํ‘œํ˜„๋˜๋Š” ์ž…๋ ฅ 3D ํ…์„œ์˜ ๋Œ€ํ‘œ์ ์ธ ํ‘œํ˜„๋“ค์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์œ„ ์ฝ”๋“œ๋Š” ์ถœ๋ ฅ์ธต๊นŒ์ง€ ํฌํ•จํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์ฝ”๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ์ฃผ๋กœ ์€๋‹‰์ธต์œผ๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜๋‚˜์˜ RNN ์ธต์— ๋Œ€ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒฐ๊ด๊ฐ’์€ ํ•˜๋‚˜์˜ ์€๋‹‰ ์ƒํƒœ ๋˜๋Š” ์ •์˜ํ•˜๊ธฐ์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ๋งŒ์•ฝ ์ „๊ฒฐํ•ฉ์ธต(Fully-connected layer)์„ ์ถœ๋ ฅ์ธต์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€์„ ๊ฒฝ์šฐ์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๊ทธ๋ฆผ๊ณผ ์€๋‹‰์ธต๊นŒ์ง€๋งŒ ํ‘œํ˜„ํ•œ ๊ทธ๋ฆผ์˜ ์ฐจ์ด๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด RNN ์ธต์€ ์œ„์—์„œ ์„ค๋ช…ํ•œ ์ž…๋ ฅ 3D ํ…์„œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ์–ด๋–ป๊ฒŒ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ• ๊นŒ์š”? RNN ์ธต์€ ์‚ฌ์šฉ์ž์˜ ์„ค์ •์— ๋”ฐ๋ผ ๋‘ ๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ์ถœ๋ ฅ์„ ๋‚ด๋ณด๋ƒ…๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ์…€์˜ ์ตœ์ข… ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋งŒ์„ ๋ฆฌํ„ดํ•˜๊ณ  ์ž ํ•œ๋‹ค๋ฉด (batch_size, output_dim) ํฌ๊ธฐ์˜ 2D ํ…์„œ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ ์…€์˜ ๊ฐ ์‹œ์ (time step)์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’๋“ค์„ ๋ชจ์•„์„œ ์ „์ฒด ์‹œํ€€์Šค๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ์ž ํ•œ๋‹ค๋ฉด (batch_size, timesteps, output_dim) ํฌ๊ธฐ์˜ 3D ํ…์„œ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” RNN ์ธต์˜ return_sequences ๋งค๊ฐœ ๋ณ€์ˆ˜์— True๋ฅผ ์„ค์ •ํ•˜์—ฌ ์„ค์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. output_dim์€ ์•ž์„œ ์ฝ”๋“œ์—์„œ ์ •์˜ํ•œ hidden_units์˜ ๊ฐ’์œผ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ time step=3์ผ ๋•Œ, return_sequences = True๋ฅผ ์„ค์ •ํ–ˆ์„ ๋•Œ์™€ ๊ทธ๋ ‡์ง€ ์•Š์•˜์„ ๋•Œ ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. return_sequences=True๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ด ๋ชจ๋“  ์‹œ์ (time step)์— ๋Œ€ํ•ด์„œ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉฐ, ๋ณ„๋„ ๊ธฐ์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ return_sequences=False๋กœ ์„ ํƒํ•  ๊ฒฝ์šฐ์—๋Š” ๋ฉ”๋ชจ๋ฆฌ ์…€์€ ํ•˜๋‚˜์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’๋งŒ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ•˜๋‚˜์˜ ๊ฐ’์€ ๋งˆ์ง€๋ง‰ ์‹œ์ (time step)์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์ž…๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋งŒ ์ „๋‹ฌํ•˜๋„๋ก ํ•˜๋ฉด ๋‹ค ๋Œ€ ์ผ(many-to-one) ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๊ณ , ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ „๋‹ฌํ•˜๋„๋ก ํ•˜๋ฉด, ๋‹ค์Œ์ธต์— RNN ์€๋‹‰์ธต์ด ํ•˜๋‚˜ ๋” ์žˆ๋Š” ๊ฒฝ์šฐ์ด๊ฑฐ๋‚˜ ๋‹ค ๋Œ€๋‹ค(many-to-many) ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ฐฐ์šฐ๋Š” LSTM์ด๋‚˜ GRU๋„ ๋‚ด๋ถ€ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ๋‹ค๋ฅด์ง€๋งŒ model.add()๋ฅผ ํ†ตํ•ด์„œ ์ธต์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋Š” SimpleRNN ์ฝ”๋“œ์™€ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์‹ค์Šต์„ ํ†ตํ•ด ๋ชจ๋ธ ๋‚ด๋ถ€์ ์œผ๋กœ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์–ด๋–ป๊ฒŒ ์ •์˜ํ•˜๋Š”์ง€ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import SimpleRNN model = Sequential() model.add(SimpleRNN(3, input_shape=(2,10))) # model.add(SimpleRNN(3, input_length=2, input_dim=10))์™€ ๋™์ผํ•จ. model.summary() _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= simple_rnn_1 (SimpleRNN) (None, 3) 42 ================================================================= Total params: 42 Trainable params: 42 Non-trainable params: 0 _________________________________________________________________ ์ถœ๋ ฅ๊ฐ’์ด (batch_size, output_dim) ํฌ๊ธฐ์˜ 2D ํ…์„œ์ผ ๋•Œ, output_dim์€ hidden_units์˜ ๊ฐ’์ธ 3์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ batch_size๋ฅผ ํ˜„ ๋‹จ๊ณ„์—์„œ๋Š” ์•Œ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ (None, 3)์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” batch_size๋ฅผ ๋ฏธ๋ฆฌ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model = Sequential() model.add(SimpleRNN(3, batch_input_shape=(8,2,10))) model.summary() _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= simple_rnn_2 (SimpleRNN) (8, 3) 42 ================================================================= Total params: 42 Trainable params: 42 Non-trainable params: 0 _________________________________________________________________ batch_size๋ฅผ 8๋กœ ๊ธฐ์žฌํ•˜๋ฉด ์ถœ๋ ฅ์˜ ํฌ๊ธฐ๊ฐ€ (8, 3)์ด ๋ฉ๋‹ˆ๋‹ค. return_sequences ๋งค๊ฐœ ๋ณ€์ˆ˜์— True๋ฅผ ๊ธฐ์žฌํ•˜์—ฌ ์ถœ๋ ฅ๊ฐ’์œผ๋กœ (batch_size, timesteps, output_dim) ํฌ๊ธฐ์˜ 3D ํ…์„œ๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model = Sequential() model.add(SimpleRNN(3, batch_input_shape=(8,2,10), return_sequences=True)) model.summary() _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= simple_rnn_3 (SimpleRNN) (8, 2, 3) 42 ================================================================= Total params: 42 Trainable params: 42 Non-trainable params: 0 _________________________________________________________________ ์ถœ๋ ฅ์˜ ํฌ๊ธฐ๊ฐ€ (8, 2, 3)์ด ๋ฉ๋‹ˆ๋‹ค. 3. ํŒŒ์ด์ฌ์œผ๋กœ RNN ๊ตฌํ˜„ํ•˜๊ธฐ ์ง์ ‘ Numpy๋กœ RNN ์ธต์„ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ฉ”๋ชจ๋ฆฌ ์…€์—์„œ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์‹์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. t t n ( x t W h โˆ’ + ) ์‹ค์ œ ๊ตฌํ˜„์— ์•ž์„œ ๊ฐ„๋‹จํžˆ ๊ฐ€์ƒ์˜ ์ฝ”๋“œ(pseudocode)๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๊ฐ€์ƒ์˜ ์ฝ”๋“œ(pseudocode)๋กœ ์‹ค์ œ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์•„๋‹˜. hidden_state_t = 0 # ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋ฅผ 0(๋ฒกํ„ฐ)๋กœ ์ดˆ๊ธฐํ™” for input_t in input_length: # ๊ฐ ์‹œ์ ๋งˆ๋‹ค ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค. output_t = tanh(input_t, hidden_state_t) # ๊ฐ ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ž…๋ ฅ๊ณผ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ์‚ฐ hidden_state_t = output_t # ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋Š” ํ˜„์žฌ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ๋œ๋‹ค. ์šฐ์„  t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ hidden_state_t๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜์˜€๊ณ , ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋ฅผ input_length๋กœ ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋Š” ๊ณง ์ด ์‹œ์ ์˜ ์ˆ˜(timesteps)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  t ์‹œ์ ์˜ ์ž…๋ ฅ๊ฐ’์„ input_t๋กœ ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ ๊ฐ ์‹œ์ ๋งˆ๋‹ค input_t์™€ hidden_sate_t(์ด์ „ ์ƒํƒœ์˜ ์€๋‹‰ ์ƒํƒœ)๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์ธ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ˜„์‹œ์ ์˜ hidden_state_t๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์ƒ์˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํžˆ ๊ฐœ๋… ์ •๋ฆฝ์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. RNN ์ธต์„ ์‹ค์ œ ๋™์ž‘๋˜๋Š” ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด (timesteps, input_dim) ํฌ๊ธฐ์˜ 2D ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์˜€์œผ๋‚˜, ์‹ค์ œ๋กœ ์ผ€๋ผ์Šค์—์„œ๋Š” (batch_size, timesteps, input_dim)์˜ ํฌ๊ธฐ์˜ 3D ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•ฉ์‹œ๋‹ค. timesteps๋Š” ์‹œ์ ์˜ ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ๋Š” ๋ณดํ†ต ๋ฌธ์žฅ์˜ ๊ธธ์ด์ž…๋‹ˆ๋‹ค. input_dim์€ ์ž…๋ ฅ์˜ ์ฐจ์›์ž…๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ๋Š” ๋ณดํ†ต ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์ž…๋‹ˆ๋‹ค. hidden_units๋Š” ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์…€์˜ ์šฉ๋Ÿ‰์ž…๋‹ˆ๋‹ค. ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋Š” 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import numpy as np timesteps = 10 input_dim = 4 hidden_units = 8 # ์ž…๋ ฅ์— ํ•ด๋‹น๋˜๋Š” 2D ํ…์„œ inputs = np.random.random((timesteps, input_dim)) # ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋Š” 0(๋ฒกํ„ฐ)๋กœ ์ดˆ๊ธฐํ™” hidden_state_t = np.zeros((hidden_units,)) print('์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ :',hidden_state_t) ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ : [0. 0. 0. 0. 0. 0. 0. 0.] ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ 8๋กœ ์ •์˜ํ•˜์˜€์œผ๋ฏ€๋กœ 8์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” 0์˜ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฒกํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ๊ฐ ํฌ๊ธฐ์— ๋งž๊ฒŒ ์ •์˜ํ•˜๊ณ  ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Wx = np.random.random((hidden_units, input_dim)) # (8, 4) ํฌ๊ธฐ์˜ 2D ํ…์„œ ์ƒ์„ฑ. ์ž…๋ ฅ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜. Wh = np.random.random((hidden_units, hidden_units)) # (8, 8) ํฌ๊ธฐ์˜ 2D ํ…์„œ ์ƒ์„ฑ. ์€๋‹‰ ์ƒํƒœ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜. b = np.random.random((hidden_units,)) # (8, ) ํฌ๊ธฐ์˜ 1D ํ…์„œ ์ƒ์„ฑ. ์ด ๊ฐ’์€ ํŽธํ–ฅ(bias). print('๊ฐ€์ค‘์น˜ Wx์˜ ํฌ๊ธฐ(shape) :',np.shape(Wx)) print('๊ฐ€์ค‘์น˜ Wh์˜ ํฌ๊ธฐ(shape) :',np.shape(Wh)) print('ํŽธํ–ฅ์˜ ํฌ๊ธฐ(shape) :',np.shape(b)) ๊ฐ€์ค‘์น˜ Wx์˜ ํฌ๊ธฐ(shape) : (8, 4) ๊ฐ€์ค‘์น˜ Wh์˜ ํฌ๊ธฐ(shape) : (8, 8) ํŽธํ–ฅ์˜ ํฌ๊ธฐ(shape) : (8, ) ๊ฐ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ํฌ๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Wx๋Š” (์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ ร— ์ž…๋ ฅ์˜ ์ฐจ์›), Wh๋Š” (์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ ร— ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ), b๋Š” (์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , RNN ์ธต์„ ๋™์ž‘์‹œ์ผœ๋ด…์‹œ๋‹ค. total_hidden_states = [] # ๊ฐ ์‹œ์  ๋ณ„ ์ž…๋ ฅ๊ฐ’. for input_t in inputs: # Wx * Xt + Wh * Ht-1 + b(bias) output_t = np.tanh(np.dot(Wx, input_t) + np.dot(Wh, hidden_state_t) + b) # ๊ฐ ์‹œ์  t ๋ณ„ ๋ฉ”๋ชจ๋ฆฌ ์…€์˜ ์ถœ๋ ฅ์˜ ํฌ๊ธฐ๋Š” (timestep t, output_dim) # ๊ฐ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์„ ๊ณ„์†ํ•ด์„œ ๋ˆ„์  total_hidden_states.append(list(output_t)) hidden_state_t = output_t # ์ถœ๋ ฅ ์‹œ ๊ฐ’์„ ๊น”๋”ํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ์šฉ๋„. total_hidden_states = np.stack(total_hidden_states, axis = 0) # (timesteps, output_dim) print('๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ :') print(total_hidden_states) ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ : [[0.85575076 0.71627213 0.87703694 0.83938496 0.81045543 0.86482715 0.76387233 0.60007514] [0.99982366 0.99985897 0.99928638 0.99989791 0.99998252 0.99977656 0.99997677 0.9998397 ] [0.99997583 0.99996057 0.99972541 0.99997993 0.99998684 0.99954936 0.99997638 0.99993143] [0.99997782 0.99996494 0.99966651 0.99997989 0.99999115 0.99980087 0.99999107 0.9999622 ] [0.99997231 0.99996091 0.99976218 0.99998483 0.9999955 0.99989239 0.99999339 0.99997324] [0.99997082 0.99998754 0.99962158 0.99996278 0.99999331 0.99978731 0.99998831 0.99993414] [0.99997427 0.99998367 0.99978331 0.99998173 0.99999579 0.99983689 0.99999058 0.99995531] [0.99992591 0.99996115 0.99941212 0.99991593 0.999986 0.99966571 0.99995842 0.99987795] [0.99997139 0.99997192 0.99960794 0.99996751 0.99998795 0.9996674 0.99998177 0.99993016] [0.99997659 0.99998915 0.99985392 0.99998726 0.99999773 0.99988295 0.99999316 0.99996326]] 4. ๊นŠ์€ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Deep Recurrent Neural Network) ์•ž์„œ RNN๋„ ๋‹ค์ˆ˜์˜ ์€๋‹‰์ธต์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์—์„œ ์€๋‹‰์ธต์ด 1๊ฐœ ๋” ์ถ”๊ฐ€๋˜์–ด ์€๋‹‰์ธต์ด 2๊ฐœ์ธ ๊นŠ์€(deep) ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์€๋‹‰์ธต์„ 2๊ฐœ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. model = Sequential() model.add(SimpleRNN(hidden_units, input_length=10, input_dim=5, return_sequences=True)) model.add(SimpleRNN(hidden_units, return_sequences=True)) ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ฒซ ๋ฒˆ์งธ ์€๋‹‰์ธต์€ ๋‹ค์Œ ์€๋‹‰์ธต์ด ์กด์žฌํ•˜๋ฏ€๋กœ return_sequences = True๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ ๋‹ค์Œ ์€๋‹‰์ธต์œผ๋กœ ๋ณด๋‚ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 5. ์–‘๋ฐฉํ–ฅ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Bidirectional Recurrent Neural Network) ์–‘๋ฐฉํ–ฅ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์€ ์‹œ์  t์—์„œ์˜ ์ถœ๋ ฅ๊ฐ’์„ ์˜ˆ์ธกํ•  ๋•Œ ์ด์ „ ์‹œ์ ์˜ ์ž…๋ ฅ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ดํ›„ ์‹œ์ ์˜ ์ž…๋ ฅ ๋˜ํ•œ ์˜ˆ์ธก์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์•„์ด๋””์–ด์— ๊ธฐ๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. ๋นˆ์นธ ์ฑ„์šฐ๊ธฐ ๋ฌธ์ œ์— ๋น„์œ ํ•˜์—ฌ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šด๋™์„ ์—ด์‹ฌํžˆ ํ•˜๋Š” ๊ฒƒ์€ [ ]์„ ๋Š˜๋ฆฌ๋Š”๋ฐ ํšจ๊ณผ์ ์ด๋‹ค. 1) ๊ทผ์œก 2) ์ง€๋ฐฉ 3) ์ŠคํŠธ๋ ˆ์Šค '์šด๋™์„ ์—ด์‹ฌํžˆ ํ•˜๋Š” ๊ฒƒ์€ [ ]์„ ๋Š˜๋ฆฌ๋Š”๋ฐ ํšจ๊ณผ์ ์ด๋‹ค.'๋ผ๋Š” ๋ฌธ์žฅ์—์„œ ๋ฌธ๋งฅ ์ƒ์œผ๋กœ ์ •๋‹ต์€ '๊ทผ์œก'์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๋นˆ์นธ ์ฑ„์šฐ๊ธฐ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์ด์ „์— ๋‚˜์˜จ ๋‹จ์–ด๋“ค๋งŒ์œผ๋กœ ๋นˆ์นธ์„ ์ฑ„์šฐ๋ ค๊ณ  ์‹œ๋„ํ•ด ๋ณด๋ฉด ์ •๋ณด๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. '์šด๋™์„ ์—ด์‹ฌํžˆ ํ•˜๋Š” ๊ฒƒ์€'๊นŒ์ง€๋งŒ ์ฃผ๊ณ  ๋’ค์˜ ๋‹จ์–ด๋“ค์€ ๊ฐ€๋ฆฐ ์ฑ„ ๋นˆ์นธ์˜ ์ •๋‹ต์ด ๋  ์ˆ˜ ์žˆ๋Š” ์„ธ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ๊ณ ๋ฅด๋Š” ๊ฒƒ์€ ๋’ค์˜ ๋‹จ์–ด๋“ค๊นŒ์ง€ ์•Œ๊ณ  ์žˆ๋Š” ์ƒํƒœ๋ณด๋‹ค ๋ช…๋ฐฑํžˆ ์ •๋‹ต์„ ๊ฒฐ์ •ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. RNN์ด ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ ์ค‘์—์„œ๋Š” ๊ณผ๊ฑฐ ์‹œ์ ์˜ ์ž…๋ ฅ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฏธ๋ž˜ ์‹œ์ ์˜ ์ž…๋ ฅ์— ํžŒํŠธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด์ „๊ณผ ์ดํ›„์˜ ์‹œ์  ๋ชจ๋‘๋ฅผ ๊ณ ๋ คํ•ด์„œ ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก์„ ๋”์šฑ ์ •ํ™•ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ณ ์•ˆ๋œ ๊ฒƒ์ด ์–‘๋ฐฉํ–ฅ RNN์ž…๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ RNN์€ ํ•˜๋‚˜์˜ ์ถœ๋ ฅ๊ฐ’์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‘ ๊ฐœ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ ์•ž์—์„œ ๋ฐฐ์šด ๊ฒƒ์ฒ˜๋Ÿผ ์•ž ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ(Forward States)๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ํ˜„์žฌ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ฃผํ™ฉ์ƒ‰ ๋ฉ”๋ชจ๋ฆฌ ์…€์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ ์•ž์—์„œ ๋ฐฐ์šด ๊ฒƒ๊ณผ๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์•ž ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ ๋’ค ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ(Backward States)๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ํ˜„์žฌ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ์ฝ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ดˆ๋ก์ƒ‰ ๋ฉ”๋ชจ๋ฆฌ ์…€์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‘ ๊ฐœ์˜ ๊ฐ’ ๋ชจ๋‘๊ฐ€ ํ˜„์žฌ ์‹œ์ ์˜ ์ถœ๋ ฅ์ธต์—์„œ ์ถœ๋ ฅ๊ฐ’์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. from tensorflow.keras.layers import Bidirectional timesteps = 10 input_dim = 5 model = Sequential() model.add(Bidirectional(SimpleRNN(hidden_units, return_sequences=True), input_shape=(timesteps, input_dim))) ์–‘๋ฐฉํ–ฅ RNN๋„ ๋‹ค์ˆ˜์˜ ์€๋‹‰์ธต์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์–‘๋ฐฉํ–ฅ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์—์„œ ์€๋‹‰์ธต์ด 1๊ฐœ ๋” ์ถ”๊ฐ€๋˜์–ด ์€๋‹‰์ธต์ด 2๊ฐœ์ธ ๊นŠ์€(deep) ์–‘๋ฐฉํ–ฅ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ๋“ค๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด์ง€๋งŒ, ์€๋‹‰์ธต์„ ๋ฌด์กฐ๊ฑด ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ํ•ด์„œ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ์ข‹์•„์ง€๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์€๋‹‰์ธต์„ ์ถ”๊ฐ€ํ•˜๋ฉด ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” ์–‘์ด ๋งŽ์•„์ง€์ง€๋งŒ ๋ฐ˜๋Œ€๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๋˜ํ•œ ๋งŽ์€ ์–‘์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์€๋‹‰์ธต์ด 4๊ฐœ์ธ ๊ฒฝ์šฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. model = Sequential() model.add(Bidirectional(SimpleRNN(hidden_units, return_sequences=True), input_shape=(timesteps, input_dim))) model.add(Bidirectional(SimpleRNN(hidden_units, return_sequences=True))) model.add(Bidirectional(SimpleRNN(hidden_units, return_sequences=True))) model.add(Bidirectional(SimpleRNN(hidden_units, return_sequences=True))) ์–‘๋ฐฉํ–ฅ RNN์€ ํƒœ๊น… ์ž‘์—… ์ฑ•ํ„ฐ์˜ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 6. ์ ๊ฒ€ ํ€ด์ฆˆ RNN์„ ์ œ๋Œ€๋กœ ์ดํ•ดํ–ˆ๋Š”์ง€ ํ€ด์ฆˆ๋ฅผ ํ†ตํ•ด์„œ ํ™•์ธํ•ด ๋ณด์„ธ์š”! ๋ชจ๋ธ์— ๋Œ€ํ•œ ์„ค๋ช…์ด ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๋•Œ, ์ด ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด๋ณด์„ธ์š”. Embedding์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์˜ ํฌ๊ธฐ๊ฐ€ 5,000์ด๊ณ  ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 100์ž…๋‹ˆ๋‹ค. ์€๋‹‰์ธต์—์„œ๋Š” Simple RNN์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 128์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” 30์œผ๋กœ ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ๋กœ, ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ์€ 1๊ฐœ๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต์€ 1๊ฐœ์ž…๋‹ˆ๋‹ค. ์ •๋‹ต์€ ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์˜ ๋Œ“๊ธ€์— ๋‚จ๊ฒจ๋‘์—ˆ์Šต๋‹ˆ๋‹ค. 08-02 ์žฅ๋‹จ๊ธฐ ๋ฉ”๋ชจ๋ฆฌ(Long Short-Term Memory, LSTM) ๋ฐ”๋‹๋ผ ์•„์ด์Šคํฌ๋ฆผ์ด ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ง›์„ ๊ฐ€์ง„ ์•„์ด์Šคํฌ๋ฆผ์ธ ๊ฒƒ์ฒ˜๋Ÿผ, ์•ž์„œ ๋ฐฐ์šด RNN์„ ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ํ˜•ํƒœ์˜ RNN์ด๋ผ๊ณ  ํ•˜์—ฌ ๋ฐ”๋‹๋ผ RNN(Vanilla RNN)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. (์ผ€ ๋ผ์Šค์—์„œ๋Š” SimpleRNN) ๋ฐ”๋‹๋ผ RNN ์ดํ›„ ๋ฐ”๋‹๋ผ RNN์˜ ํ•œ๊ณ„๋ฅผ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ RNN์˜ ๋ณ€ํ˜•์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์— ๋ฐฐ์šฐ๊ฒŒ ๋  LSTM๋„ ๊ทธ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ์„ค๋ช…์—์„œ LSTM๊ณผ ๋น„๊ตํ•˜์—ฌ RNN์„ ์–ธ๊ธ‰ํ•˜๋Š” ๊ฒƒ์€ ์ „๋ถ€ ๋ฐ”๋‹๋ผ RNN์„ ๋งํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฐ”๋‹๋ผ RNN์˜ ํ•œ๊ณ„ ์•ž์—์„œ ๋ฐ”๋‹๋ผ RNN์€ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ์ด์ „์˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ์— ์˜์กดํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ”๋‹๋ผ RNN์€ ๋น„๊ต์  ์งง์€ ์‹œํ€€์Šค(sequence)์— ๋Œ€ํ•ด์„œ๋งŒ ํšจ๊ณผ๋ฅผ ๋ณด์ด๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋‹๋ผ RNN์˜ ์‹œ์ (time step)์ด ๊ธธ์–ด์งˆ์ˆ˜๋ก ์•ž์˜ ์ •๋ณด๊ฐ€ ๋’ค๋กœ ์ถฉ๋ถ„ํžˆ ์ „๋‹ฌ๋˜์ง€ ๋ชปํ•˜๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ฒซ ๋ฒˆ์งธ ์ž…๋ ฅ๊ฐ’์ธ 1 ์˜ ์ •๋ณด๋Ÿ‰์„ ์ง™์€ ๋‚จ์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ–ˆ์„ ๋•Œ, ์ƒ‰์ด ์ ์ฐจ ์–•์•„์ง€๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ ์ด ์ง€๋‚ ์ˆ˜๋ก 1 ์˜ ์ •๋ณด๋Ÿ‰์ด ์†์‹ค๋˜์–ด๊ฐ€๋Š” ๊ณผ์ •์„ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋’ค๋กœ ๊ฐˆ์ˆ˜๋ก 1 ์˜ ์ •๋ณด๋Ÿ‰์€ ์†์‹ค๋˜๊ณ , ์‹œ์ ์ด ์ถฉ๋ถ„ํžˆ ๊ธด ์ƒํ™ฉ์—์„œ๋Š” 1 ์˜ ์ „์ฒด ์ •๋ณด์— ๋Œ€ํ•œ ์˜ํ–ฅ๋ ฅ์€ ๊ฑฐ์˜ ์˜๋ฏธ๊ฐ€ ์—†์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด์ฉŒ๋ฉด ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ •๋ณด๊ฐ€ ์‹œ์ ์˜ ์•ž ์ชฝ์— ์œ„์น˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. RNN์œผ๋กœ ๋งŒ๋“  ์–ธ์–ด ๋ชจ๋ธ์ด ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ณผ์ •์„ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ''๋ชจ์Šคํฌ๋ฐ”์— ์—ฌํ–‰์„ ์™”๋Š”๋ฐ ๊ฑด๋ฌผ๋„ ์˜ˆ์˜๊ณ  ๋จน์„ ๊ฒƒ๋„ ๋ง›์žˆ์—ˆ์–ด. ๊ทธ๋Ÿฐ๋ฐ ๊ธ€์Ž„ ์ง์žฅ ์ƒ์‚ฌํ•œํ…Œ ์ „ํ™”๊ฐ€ ์™”์–ด. ์–ด๋””๋ƒ๊ณ  ๋ฌป๋”๋ผ๊ณ  ๊ทธ๋ž˜์„œ ๋‚˜๋Š” ๋งํ–ˆ์ง€. ์ € ์—ฌํ–‰ ์™”๋Š”๋ฐ์š”. ์—ฌ๊ธฐ ___'' ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์žฅ์†Œ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์žฅ์†Œ ์ •๋ณด์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด์ธ '๋ชจ์Šคํฌ๋ฐ”'๋Š” ์•ž์— ์œ„์น˜ํ•˜๊ณ  ์žˆ๊ณ , RNN์ด ์ถฉ๋ถ„ํ•œ ๊ธฐ์–ต๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ๋ชปํ•œ๋‹ค๋ฉด ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์—‰๋šฑํ•˜๊ฒŒ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์žฅ๊ธฐ ์˜์กด์„ฑ ๋ฌธ์ œ(the problem of Long-Term Dependencies)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ๋ฐ”๋‹๋ผ RNN ๋‚ด๋ถ€ ์—ด์–ด๋ณด๊ธฐ LSTM์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ธฐ ์ „์— ๋ฐ”๋‹๋ผ RNN์˜ ๋šœ๊ป‘์„ ์—ด์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๋ฐ”๋‹๋ผ RNN์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” RNN ๊ณ„์—ด์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ํŽธํ–ฅ ๋ฅผ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์— ํŽธํ–ฅ ๋ฅผ ๊ทธ๋ฆฐ๋‹ค๋ฉด t ์˜†์— tanh๋กœ ํ–ฅํ•˜๋Š” ๋˜ ํ•˜๋‚˜์˜ ์ž…๋ ฅ์„ ์„ ๊ทธ๋ฆฌ๋ฉด ๋ฉ๋‹ˆ๋‹ค. t t n ( x t W h โˆ’ + ) ๋ฐ”๋‹๋ผ RNN์€ t h โˆ’์ด๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์ด ๊ฐ๊ฐ์˜ ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ ธ์„œ ๋ฉ”๋ชจ๋ฆฌ ์…€์˜ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์ด ๊ฐ’์€ ์€๋‹‰์ธต์˜ ์ถœ๋ ฅ์ธ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 3. LSTM(Long Short-Term Memory) ์œ„์˜ ๊ทธ๋ฆผ์€ LSTM์˜ ์ „์ฒด์ ์ธ ๋‚ด๋ถ€์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ „ํ†ต์ ์ธ RNN์˜ ์ด๋Ÿฌํ•œ ๋‹จ์ ์„ ๋ณด์™„ํ•œ RNN์˜ ์ผ์ข…์„ ์žฅ๋‹จ๊ธฐ ๋ฉ”๋ชจ๋ฆฌ(Long Short-Term Memory)๋ผ๊ณ  ํ•˜๋ฉฐ, ์ค„์—ฌ์„œ LSTM์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. LSTM์€ ์€๋‹‰์ธต์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์— ์ž…๋ ฅ ๊ฒŒ์ดํŠธ, ๋ง๊ฐ ๊ฒŒ์ดํŠธ, ์ถœ๋ ฅ ๊ฒŒ์ดํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๊ธฐ์–ต์„<NAME>๊ณ , ๊ธฐ์–ตํ•ด์•ผ ํ•  ๊ฒƒ๋“ค์„ ์ •ํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด LSTM์€ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์‹์ด ์ „ํ†ต์ ์ธ RNN๋ณด๋‹ค ์กฐ๊ธˆ ๋” ๋ณต์žกํ•ด์กŒ์œผ๋ฉฐ ์…€ ์ƒํƒœ(cell state)๋ผ๋Š” ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” t ์‹œ์ ์˜ ์…€ ์ƒํƒœ๋ฅผ t ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. LSTM์€ RNN๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ธด ์‹œํ€€์Šค์˜ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ํƒ์›”ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ์…€ ์ƒํƒœ๋Š” ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ€๋Š” ๊ตต์€ ์„ ์ž…๋‹ˆ๋‹ค. ์…€ ์ƒํƒœ ๋˜ํ•œ ์ด์ „์— ๋ฐฐ์šด ์€๋‹‰ ์ƒํƒœ์ฒ˜๋Ÿผ ์ด์ „ ์‹œ์ ์˜ ์…€ ์ƒํƒœ๊ฐ€ ๋‹ค์Œ ์‹œ์ ์˜ ์…€ ์ƒํƒœ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์ž…๋ ฅ์œผ๋กœ์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’๊ณผ ์…€ ์ƒํƒœ์˜ ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ 3๊ฐœ์˜ ๊ฒŒ์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๊ฒŒ์ดํŠธ๋Š” ์‚ญ์ œ ๊ฒŒ์ดํŠธ, ์ž…๋ ฅ ๊ฒŒ์ดํŠธ, ์ถœ๋ ฅ ๊ฒŒ์ดํŠธ๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ ์ด 3๊ฐœ์˜ ๊ฒŒ์ดํŠธ์—๋Š” ๊ณตํ†ต์ ์œผ๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋ฉด 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๋ฐ ์ด ๊ฐ’๋“ค์„ ๊ฐ€์ง€๊ณ  ๊ฒŒ์ดํŠธ๋ฅผ ์กฐ์ ˆํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ์ฐธ๊ณ ๋กœ ๊ฐ ๊ฒŒ์ดํŠธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ดํ•˜ ์‹์—์„œ ฯƒ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ดํ•˜ ์‹์—์„œ tanh๋Š” ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. x, x, x, x๋Š” t ์™€ ํ•จ๊ป˜ ๊ฐ ๊ฒŒ์ดํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” 4๊ฐœ์˜ ๊ฐ€์ค‘์น˜์ž…๋‹ˆ๋‹ค. h, h, h, h๋Š” t 1 ์™€ ํ•จ๊ป˜ ๊ฐ ๊ฒŒ์ดํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” 4๊ฐœ์˜ ๊ฐ€์ค‘์น˜์ž…๋‹ˆ๋‹ค. i b, f b๋Š” ๊ฐ ๊ฒŒ์ดํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” 4๊ฐœ์˜ ํŽธํ–ฅ์ž…๋‹ˆ๋‹ค. (1) ์ž…๋ ฅ ๊ฒŒ์ดํŠธ t ฯƒ ( x x + h h โˆ’ + i ) t t n ( x x + h h โˆ’ + g ) ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋Š” ํ˜„์žฌ ์ •๋ณด๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•œ ๊ฒŒ์ดํŠธ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ํ˜„์žฌ ์‹œ์  t์˜ ๊ฐ’๊ณผ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋กœ ์ด์–ด์ง€๋Š” ๊ฐ€์ค‘์น˜ x๋ฅผ ๊ณฑํ•œ ๊ฐ’๊ณผ ์ด์ „ ์‹œ์  t-1์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋กœ ์ด์–ด์ง€๋Š” ๊ฐ€์ค‘์น˜ h๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ๋”ํ•˜์—ฌ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ t ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ ์‹œ์  t์˜ ๊ฐ’๊ณผ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋กœ ์ด์–ด์ง€๋Š” ๊ฐ€์ค‘์น˜ x๋ฅผ ๊ณฑํ•œ ๊ฐ’๊ณผ ์ด์ „ ์‹œ์  t-1์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋กœ ์ด์–ด์ง€๋Š” ๊ฐ€์ค‘์น˜ h๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ๋”ํ•˜์—ฌ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ t ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” t ๊ณผ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜ -1๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” t . ์ด ๋‘ ๊ฐœ์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์ด๋ฒˆ์— ์„ ํƒ๋œ ๊ธฐ์–ตํ•  ์ •๋ณด์˜ ์–‘์„ ์ •ํ•˜๋Š”๋ฐ, ๊ตฌ์ฒด์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๊ฒฐ์ •ํ•˜๋Š”์ง€๋Š” ์•„๋ž˜์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋  ์…€ ์ƒํƒœ ์ˆ˜์‹์„ ์ฐธ๊ณ ํ•ฉ๋‹ˆ๋‹ค. (2) ์‚ญ์ œ ๊ฒŒ์ดํŠธ t ฯƒ ( x x + h h โˆ’ + f ) ์‚ญ์ œ ๊ฒŒ์ดํŠธ๋Š” ๊ธฐ์–ต์„ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•œ ๊ฒŒ์ดํŠธ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ์‹œ์  t์˜ ๊ฐ’๊ณผ ์ด์ „ ์‹œ์  t-1์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋ฉด 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๋ฐ, ์ด ๊ฐ’์ด ๊ณง ์‚ญ์ œ ๊ณผ์ •์„ ๊ฑฐ์นœ ์ •๋ณด์˜ ์–‘์ž…๋‹ˆ๋‹ค. 0์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ์ •๋ณด๊ฐ€ ๋งŽ์ด ์‚ญ์ œ๋œ ๊ฒƒ์ด๊ณ  1์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ์ •๋ณด๋ฅผ ์˜จ์ „ํžˆ ๊ธฐ์–ตํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์…€ ์ƒํƒœ๋ฅผ ๊ตฌํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ๊ตฌ์ฒด์ ์œผ๋กœ๋Š” ์•„๋ž˜์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋  ์…€ ์ƒํƒœ ์ˆ˜์‹์„ ์ฐธ๊ณ ํ•ฉ๋‹ˆ๋‹ค. (3) ์…€ ์ƒํƒœ t f โˆ˜ t 1 i โˆ˜ t ์…€ ์ƒํƒœ t ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ญ์ œ ๊ฒŒ์ดํŠธ์—์„œ ์ผ๋ถ€ ๊ธฐ์–ต์„ ์žƒ์€ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ ๊ฒŒ์ดํŠธ์—์„œ ๊ตฌํ•œ t g ์ด ๋‘ ๊ฐœ์˜ ๊ฐ’์— ๋Œ€ํ•ด์„œ ์›์†Œ๋ณ„ ๊ณฑ(entrywise product)์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ฐ™์€ ํฌ๊ธฐ์˜ ๋‘ ํ–‰๋ ฌ์ด ์žˆ์„ ๋•Œ ๊ฐ™์€ ์œ„์น˜์˜ ์„ฑ๋ถ„๋ผ๋ฆฌ ๊ณฑํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‹์œผ๋กœ ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์ด๋ฒˆ์— ์„ ํƒ๋œ ๊ธฐ์–ตํ•  ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ ๊ฒŒ์ดํŠธ์—์„œ ์„ ํƒ๋œ ๊ธฐ์–ต์„ ์‚ญ์ œ ๊ฒŒ์ดํŠธ์˜ ๊ฒฐ๊ด๊ฐ’๊ณผ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์„ ํ˜„์žฌ ์‹œ์  t์˜ ์…€ ์ƒํƒœ๋ผ๊ณ  ํ•˜๋ฉฐ, ์ด ๊ฐ’์€ ๋‹ค์Œ t+1 ์‹œ์ ์˜ LSTM ์…€๋กœ ๋„˜๊ฒจ์ง‘๋‹ˆ๋‹ค. ์‚ญ์ œ ๊ฒŒ์ดํŠธ์™€ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ์˜ ์˜ํ–ฅ๋ ฅ์„ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ๋งŒ์•ฝ ์‚ญ์ œ ๊ฒŒ์ดํŠธ์˜ ์ถœ๋ ฅ๊ฐ’์ธ t ๊ฐ€ 0์ด ๋œ๋‹ค๋ฉด, ์ด์ „ ์‹œ์ ์˜ ์…€ ์ƒํƒœ์˜ ๊ฐ’์ธ t 1 ์€ ํ˜„์žฌ ์‹œ์ ์˜ ์…€ ์ƒํƒœ์˜ ๊ฐ’์„ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•œ ์˜ํ–ฅ๋ ฅ์ด 0์ด ๋˜๋ฉด์„œ, ์˜ค์ง ์ž…๋ ฅ ๊ฒŒ์ดํŠธ์˜ ๊ฒฐ๊ณผ๋งŒ์ด ํ˜„์žฌ ์‹œ์ ์˜ ์…€ ์ƒํƒœ์˜ ๊ฐ’ t ์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์‚ญ์ œ ๊ฒŒ์ดํŠธ๊ฐ€ ์™„์ „ํžˆ ๋‹ซํžˆ๊ณ  ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋ฅผ ์—ฐ ์ƒํƒœ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ์˜ t ๊ฐ’์„ 0์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ํ˜„์žฌ ์‹œ์ ์˜ ์…€ ์ƒํƒœ์˜ ๊ฐ’ t ๋Š” ์˜ค์ง ์ด์ „ ์‹œ์ ์˜ ์…€ ์ƒํƒœ์˜ ๊ฐ’ t 1 ์˜ ๊ฐ’์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋ฅผ ์™„์ „ํžˆ ๋‹ซ๊ณ  ์‚ญ์ œ ๊ฒŒ์ดํŠธ๋งŒ์„ ์—ฐ ์ƒํƒœ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์‚ญ์ œ ๊ฒŒ์ดํŠธ๋Š” ์ด์ „ ์‹œ์ ์˜ ์ž…๋ ฅ์„ ์–ผ๋งˆ๋‚˜ ๋ฐ˜์˜ํ• ์ง€๋ฅผ ์˜๋ฏธํ•˜๊ณ , ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋Š” ํ˜„์žฌ ์‹œ์ ์˜ ์ž…๋ ฅ์„ ์–ผ๋งˆ๋‚˜ ๋ฐ˜์˜ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. (4) ์ถœ๋ ฅ ๊ฒŒ์ดํŠธ์™€ ์€๋‹‰ ์ƒํƒœ t ฯƒ ( x x + h h โˆ’ + o ) t o โˆ˜ a h ( t ) ์ถœ๋ ฅ ๊ฒŒ์ดํŠธ๋Š” ํ˜„์žฌ ์‹œ์  t์˜ ๊ฐ’๊ณผ ์ด์ „ ์‹œ์  t-1์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ๊ฐ’์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฐ’์€ ํ˜„์žฌ ์‹œ์  t์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์ผ์— ์“ฐ์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์…€ ์ƒํƒœ์˜ ๊ฐ’์ด ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜ -1๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์ด ๋˜๊ณ , ํ•ด๋‹น ๊ฐ’์€ ์ถœ๋ ฅ ๊ฒŒ์ดํŠธ์˜ ๊ฐ’๊ณผ ์—ฐ์‚ฐ๋˜๋ฉด์„œ, ๊ฐ’์ด ๊ฑธ๋Ÿฌ์ง€๋Š” ํšจ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์€ ๋˜ํ•œ ์ถœ๋ ฅ์ธต์œผ๋กœ๋„ ํ–ฅํ•ฉ๋‹ˆ๋‹ค. 08-03 ๊ฒŒ์ดํŠธ ์ˆœํ™˜ ์œ ๋‹›(Gated Recurrent Unit, GRU) GRU(Gated Recurrent Unit)๋Š” 2014๋…„ ๋‰ด์š•๋Œ€ํ•™๊ต ์กฐ๊ฒฝํ˜„ ๊ต์ˆ˜๋‹˜์ด ์ง‘ํ•„ํ•œ ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. GRU๋Š” LSTM์˜ ์žฅ๊ธฐ ์˜์กด์„ฑ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์„ ์œ ์ง€ํ•˜๋ฉด์„œ, ์€๋‹‰ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ณ„์‚ฐ์„ ์ค„์˜€์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ, GRU๋Š” ์„ฑ๋Šฅ์€ LSTM๊ณผ ์œ ์‚ฌํ•˜๋ฉด์„œ ๋ณต์žกํ–ˆ๋˜ LSTM์˜ ๊ตฌ์กฐ๋ฅผ ๊ฐ„๋‹จํ™” ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. 1. GRU(Gated Recurrent Unit) LSTM์—์„œ๋Š” ์ถœ๋ ฅ, ์ž…๋ ฅ, ์‚ญ์ œ ๊ฒŒ์ดํŠธ๋ผ๋Š” 3๊ฐœ์˜ ๊ฒŒ์ดํŠธ๊ฐ€ ์กด์žฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, GRU์—์„œ๋Š” ์—…๋ฐ์ดํŠธ ๊ฒŒ์ดํŠธ์™€ ๋ฆฌ์…‹ ๊ฒŒ์ดํŠธ ๋‘ ๊ฐ€์ง€ ๊ฒŒ์ดํŠธ๋งŒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. GRU๋Š” LSTM๋ณด๋‹ค ํ•™์Šต ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์ง€๋งŒ ์—ฌ๋Ÿฌ ํ‰๊ฐ€์—์„œ GRU๋Š” LSTM๊ณผ ๋น„์Šทํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ธ๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. t ฯƒ ( x x + h h โˆ’ + r ) t ฯƒ ( x x + h h โˆ’ + z ) t t n ( h ( t h โˆ’ ) W g t b) t ( โˆ’ t ) g + t h โˆ’ GRU์™€ LSTM ์ค‘ ์–ด๋–ค ๊ฒƒ์ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ ๋ฉด์—์„œ ๋” ๋‚ซ๋‹ค๊ณ  ๋‹จ์ • ์ง€์–ด ๋งํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๊ธฐ์กด์— LSTM์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ตœ์ ์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฐพ์•„๋‚ธ ์ƒํ™ฉ์ด๋ผ๋ฉด ๊ตณ์ด GRU๋กœ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๊ฒฝํ—˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ์–‘์ด ์ ์„ ๋•Œ๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ์–‘์ด ์ ์€ GRU๊ฐ€ ์กฐ๊ธˆ ๋” ๋‚ซ๊ณ , ๋ฐ์ดํ„ฐ์–‘์ด ๋” ๋งŽ์œผ๋ฉด LSTM์ด ๋” ๋‚ซ๋‹ค๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. GRU๋ณด๋‹ค LSTM์— ๋Œ€ํ•œ ์—ฐ๊ตฌ๋‚˜ ์‚ฌ์šฉ๋Ÿ‰์ด ๋” ๋งŽ์€๋ฐ, ์ด๋Š” LSTM์ด ๋” ๋จผ์ € ๋‚˜์˜จ ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 2. ์ผ€๋ผ์Šค์—์„œ์˜ GRU(Gated Recurrent Unit) ์ผ€๋ผ์Šค์—์„œ๋Š” ์—ญ์‹œ GRU์— ๋Œ€ํ•œ ๊ตฌํ˜„์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ SimpleRNN์ด๋‚˜ LSTM๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. model.add(GRU(hidden_size, input_shape=(timesteps, input_dim))) 08-04 ์ผ€๋ผ์Šค์˜ SimpleRNN๊ณผ LSTM ์ดํ•ดํ•˜๊ธฐ ์ผ€๋ผ์Šค์˜ SimpleRNN๊ณผ LSTM์„ ์ดํ•ดํ•ด ๋ด…๋‹ˆ๋‹ค. 1. ์ž„์˜์˜ ์ž…๋ ฅ ์ƒ์„ฑํ•˜๊ธฐ import numpy as np import tensorflow as tf from tensorflow.keras.layers import SimpleRNN, LSTM, Bidirectional ์šฐ์„  RNN๊ณผ LSTM์„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•œ ์ž„์˜์˜ ์ž…๋ ฅ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. train_X = [[0.1, 4.2, 1.5, 1.1, 2.8], [1.0, 3.1, 2.5, 0.7, 1.1], [0.3, 2.1, 1.5, 2.1, 0.1], [2.2, 1.4, 0.5, 0.9, 1.1]] print(np.shape(train_X)) (4, 5) ์œ„ ์ž…๋ ฅ์€ ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 5์ด๊ณ , ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ 4์ธ ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ •ํ•œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด 4๋ฒˆ์˜ ์‹œ์ (timesteps)์ด ์กด์žฌํ•˜๊ณ , ๊ฐ ์‹œ์ ๋งˆ๋‹ค 5์ฐจ์›์˜ ๋‹จ์–ด ๋ฒกํ„ฐ๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์•ž์„œ RNN์€ 2D ํ…์„œ๊ฐ€ ์•„๋‹ˆ๋ผ 3D ํ…์„œ๋ฅผ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์œ„์—์„œ ๋งŒ๋“  2D ํ…์„œ๋ฅผ 3D ํ…์„œ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐฐ์น˜ ํฌ๊ธฐ 1์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฏ€๋กœ์„œ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. train_X = [[[0.1, 4.2, 1.5, 1.1, 2.8], [1.0, 3.1, 2.5, 0.7, 1.1], [0.3, 2.1, 1.5, 2.1, 0.1], [2.2, 1.4, 0.5, 0.9, 1.1]]] train_X = np.array(train_X, dtype=np.float32) print(train_X.shape) (1, 4, 5) (batch_size, timesteps, input_dim)์— ํ•ด๋‹น๋˜๋Š” (1, 4, 5)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 3D ํ…์„œ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. batch_size๋Š” ํ•œ ๋ฒˆ์— RNN์ด ํ•™์Šตํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ์˜๋ฏธํ•˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ์ƒ˜ํ”Œ์ด 1๊ฐœ๋ฐ–์— ์—†์œผ๋ฏ€๋กœ batch_size๋Š” 1์ž…๋‹ˆ๋‹ค. 2. SimpleRNN ์ดํ•ดํ•˜๊ธฐ ์œ„์—์„œ ์ƒ์„ฑํ•œ ๋ฐ์ดํ„ฐ๋ฅผ SimpleRNN์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ SimpleRNN์˜ ์ถœ๋ ฅ๊ฐ’์„ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. SimpleRNN์—๋Š” ์—ฌ๋Ÿฌ ์ธ์ž๊ฐ€ ์žˆ์œผ๋ฉฐ ๋Œ€ํ‘œ์ ์ธ ์ธ์ž๋กœ return_sequences์™€ return_state๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ๋Š” ๋‘˜ ๋‹ค False๋กœ ์ง€์ •๋ผ ์žˆ์œผ๋ฏ€๋กœ ๋ณ„๋„ ์ง€์ •์„ ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” False๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์šฐ์„ , ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ 3์œผ๋กœ ์ง€์ •ํ•˜๊ณ , ๋‘ ์ธ์ž ๊ฐ’์ด ๋ชจ๋‘ False ์ผ ๋•Œ์˜ ์ถœ๋ ฅ๊ฐ’์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ์‹ค์Šต์—์„œ SimpleRNN์„ ๋งค๋ฒˆ<NAME>์–ธํ•˜๋ฏ€๋กœ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’ ์ž์ฒด๋Š” ๋งค๋ฒˆ ์ดˆ๊ธฐํ™”๋˜์–ด ์ด์ „ ์ถœ๋ ฅ๊ณผ ๊ฐ’์˜ ์ผ๊ด€์„ฑ์€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ถœ๋ ฅ๊ฐ’ ์ž์ฒด๋ณด๋‹ค๋Š” ํ•ด๋‹น ๊ฐ’์˜ ํฌ๊ธฐ(shape)์— ์ฃผ๋ชฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. rnn = SimpleRNN(3) # rnn = SimpleRNN(3, return_sequences=False, return_state=False)์™€ ๋™์ผ. hidden_state = rnn(train_X) print('hidden state : {}, shape: {}'.format(hidden_state, hidden_state.shape)) hidden state : [[-0.866719 0.95010996 -0.99262357]], shape: (1, 3) (1, 3) ํฌ๊ธฐ์˜ ํ…์„œ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š”๋ฐ, ์ด๋Š” ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ 3์œผ๋กœ ์ง€์ •ํ–ˆ์Œ์„ ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ return_sequences๊ฐ€ False์ธ ๊ฒฝ์šฐ์—๋Š” SimpleRNN์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” return_sequences๋ฅผ True๋กœ ์ง€์ •ํ•˜์—ฌ ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. rnn = SimpleRNN(3, return_sequences=True) hidden_states = rnn(train_X) print('hidden states : {}, shape: {}'.format(hidden_states, hidden_states.shape)) hidden states : [[[ 0.92948604 -0.9985648 0.98355013] [ 0.89172053 -0.9984244 0.191779 ] [ 0.6681082 -0.96070355 0.6493537 ] [ 0.95280755 -0.98054564 0.7224146 ]]], shape: (1, 4, 3) (1, 4, 3) ํฌ๊ธฐ์˜ ํ…์„œ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋Š” (1, 4, 5)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 3D ํ…์„œ์˜€๊ณ , ๊ทธ์ค‘ 4๊ฐ€ ์‹œ์ (timesteps)์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด๋ฏ€๋กœ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•˜์—ฌ (1, 4, 3) ํฌ๊ธฐ์˜ ํ…์„œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. return_state๊ฐ€ True ์ผ ๊ฒฝ์šฐ์—๋Š” return_sequences์˜ True/False ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, return_sequences๊ฐ€ True์ด๋ฉด์„œ, return_state๋ฅผ True๋กœ ํ•  ๊ฒฝ์šฐ SimpleRNN์€ ๋‘ ๊ฐœ์˜ ์ถœ๋ ฅ์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. rnn = SimpleRNN(3, return_sequences=True, return_state=True) hidden_states, last_state = rnn(train_X) print('hidden states : {}, shape: {}'.format(hidden_states, hidden_states.shape)) print('last hidden state : {}, shape: {}'.format(last_state, last_state.shape)) hidden states : [[[ 0.29839835 -0.99608386 0.2994854 ] [ 0.9160876 0.01154806 0.86181474] [-0.20252597 -0.9270214 0.9696659 ] [-0.5144398 -0.5037417 0.96605766]]], shape: (1, 4, 3) last hidden state : [[-0.5144398 -0.5037417 0.96605766]], shape: (1, 3) ์ฒซ ๋ฒˆ์งธ ์ถœ๋ ฅ์€ return_sequences=True๋กœ ์ธํ•œ ์ถœ๋ ฅ์œผ๋กœ ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ถœ๋ ฅ์€ return_state=True๋กœ ์ธํ•œ ์ถœ๋ ฅ์œผ๋กœ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ถœ๋ ฅ์„ ๋ณด๋ฉด ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ธ (1, 4, 3) ํ…์„œ์˜ ๋งˆ์ง€๋ง‰ ๋ฒกํ„ฐ ๊ฐ’์ด return_state=True๋กœ ์ธํ•ด ์ถœ๋ ฅ๋œ ๋ฒกํ„ฐ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๋‘˜ ๋‹ค [-0.5144398 -0.5037417 0.96605766]) ๊ทธ๋ ‡๋‹ค๋ฉด return_sequences๋Š” False์ธ๋ฐ, retun_state๊ฐ€ True์ธ ๊ฒฝ์šฐ๋Š” ์–ด๋–จ๊นŒ์š”? rnn = SimpleRNN(3, return_sequences=False, return_state=True) hidden_state, last_state = rnn(train_X) print('hidden state : {}, shape: {}'.format(hidden_state, hidden_state.shape)) print('last hidden state : {}, shape: {}'.format(last_state, last_state.shape)) hidden state : [[0.07532981 0.97772664 0.97351676]], shape: (1, 3) last hidden state : [[0.07532981 0.97772664 0.97351676]], shape: (1, 3) ๋‘ ๊ฐœ์˜ ์ถœ๋ ฅ ๋ชจ๋‘ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 3. LSTM ์ดํ•ดํ•˜๊ธฐ ์‹ค์ œ๋กœ SimpleRNN์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ณด๋‹ค๋Š” LSTM์ด๋‚˜ GRU์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด๋ฒˆ์—๋Š” ์ž„์˜์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ LSTM์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  return_sequences๋ฅผ False๋กœ ๋‘๊ณ , return_state๊ฐ€ True์ธ ๊ฒฝ์šฐ๋ฅผ ๋ด…์‹œ๋‹ค. lstm = LSTM(3, return_sequences=False, return_state=True) hidden_state, last_state, last_cell_state = lstm(train_X) print('hidden state : {}, shape: {}'.format(hidden_state, hidden_state.shape)) print('last hidden state : {}, shape: {}'.format(last_state, last_state.shape)) print('last cell state : {}, shape: {}'.format(last_cell_state, last_cell_state.shape)) hidden state : [[-0.00263056 0.20051427 -0.22501363]], shape: (1, 3) last hidden state : [[-0.00263056 0.20051427 -0.22501363]], shape: (1, 3) last cell state : [[-0.04346419 0.44769213 -0.2644241 ]], shape: (1, 3) ์ด๋ฒˆ์—๋Š” SimpleRNN ๋•Œ์™€๋Š” ๋‹ฌ๋ฆฌ, ์„ธ ๊ฐœ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. return_sequences๊ฐ€ False์ด๋ฏ€๋กœ ์šฐ์„  ์ฒซ ๋ฒˆ์งธ ๊ฒฐ๊ณผ๋Š” ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ LSTM์ด SimpleRNN๊ณผ ๋‹ค๋ฅธ ์ ์€ return_state๋ฅผ True๋กœ ๋‘” ๊ฒฝ์šฐ์—๋Š” ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์…€ ์ƒํƒœ๊นŒ์ง€ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” return_sequences๋ฅผ True๋กœ ๋ฐ”๊ฟ”๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. lstm = LSTM(3, return_sequences=True, return_state=True) hidden_states, last_hidden_state, last_cell_state = lstm(train_X) print('hidden states : {}, shape: {}'.format(hidden_states, hidden_states.shape)) print('last hidden state : {}, shape: {}'.format(last_hidden_state, last_hidden_state.shape)) print('last cell state : {}, shape: {}'.format(last_cell_state, last_cell_state.shape)) hidden states : [[[ 0.1383949 0.01107763 -0.00315794] [ 0.0859854 0.03685492 -0.01836833] [-0.02512104 0.12305924 -0.0891041 ] [-0.27381724 0.05733536 -0.04240693]]], shape: (1, 4, 3) last hidden state : [[-0.27381724 0.05733536 -0.04240693]], shape: (1, 3) last cell state : [[-0.39230722 1.5474017 -0.6344505 ]], shape: (1, 3) return_state๊ฐ€ True์ด๋ฏ€๋กœ ๋‘ ๋ฒˆ์งธ ์ถœ๋ ฅ๊ฐ’์ด ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ, ์„ธ ๋ฒˆ์งธ ์ถœ๋ ฅ๊ฐ’์ด ๋งˆ์ง€๋ง‰ ์…€ ์ƒํƒœ์ธ ๊ฒƒ์€ ๋ณ€ํ•จ์—†์ง€๋งŒ return_sequences๊ฐ€ True์ด๋ฏ€๋กœ ์ฒซ ๋ฒˆ์งธ ์ถœ๋ ฅ๊ฐ’์€ ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. 4. Bidirectional(LSTM) ์ดํ•ดํ•˜๊ธฐ ๋‚œ์ด๋„๋ฅผ ์กฐ๊ธˆ ์˜ฌ๋ ค์„œ ์–‘๋ฐฉํ–ฅ LSTM์˜ ์ถœ๋ ฅ๊ฐ’์„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. return_sequences๊ฐ€ True์ธ ๊ฒฝ์šฐ์™€ False์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์ด ์–ด๋–ป๊ฒŒ ๋ฐ”๋€Œ๋Š”์ง€ ์ง์ ‘ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด๋ฒˆ์—๋Š” ์ถœ๋ ฅ๋˜๋Š” ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์„ ๊ณ ์ •์‹œ์ผœ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. k_init = tf.keras.initializers.Constant(value=0.1) b_init = tf.keras.initializers.Constant(value=0) r_init = tf.keras.initializers.Constant(value=0.1) ์šฐ์„  return_sequences๊ฐ€ False์ด๊ณ , return_state๊ฐ€ True์ธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. bilstm = Bidirectional(LSTM(3, return_sequences=False, return_state=True, \ kernel_initializer=k_init, bias_initializer=b_init, recurrent_initializer=r_init)) hidden_states, forward_h, forward_c, backward_h, backward_c = bilstm(train_X) print('hidden states : {}, shape: {}'.format(hidden_states, hidden_states.shape)) print('forward state : {}, shape: {}'.format(forward_h, forward_h.shape)) print('backward state : {}, shape: {}'.format(backward_h, backward_h.shape)) hidden states : [[0.6303139 0.6303139 0.6303139 0.70387346 0.70387346 0.70387346]], shape: (1, 6) forward state : [[0.6303139 0.6303139 0.6303139]], shape: (1, 3) backward state : [[0.70387346 0.70387346 0.70387346]], shape: (1, 3) ์ด๋ฒˆ์—๋Š” ๋ฌด๋ ค 5๊ฐœ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. return_state๊ฐ€ True์ธ ๊ฒฝ์šฐ์—๋Š” ์ •๋ฐฉํ–ฅ LSTM์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ, ์—ญ๋ฐฉํ–ฅ LSTM์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ 4๊ฐ€์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์…€ ์ƒํƒœ๋Š” ๊ฐ๊ฐ forward_c์™€ backward_c์— ์ €์žฅ๋งŒ ํ•˜๊ณ  ์ถœ๋ ฅํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ถœ๋ ฅ๊ฐ’์˜ ํฌ๊ธฐ๊ฐ€ (1, 6)์ธ ๊ฒƒ์— ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. ์ด๋Š” return_sequences๊ฐ€ False์ธ ๊ฒฝ์šฐ ์ •๋ฐฉํ–ฅ LSTM์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์—ญ๋ฐฉํ–ฅ LSTM์˜ ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์—ฐ๊ฒฐ๋œ ์ฑ„ ๋ฐ˜ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฐ๊ฒฐ๋˜์–ด ๋‹ค์Œ์ธต์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ return_state๊ฐ€ True์ธ ๊ฒฝ์šฐ์— ๋ฐ˜ํ™˜ํ•œ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์ธ forward_h์™€ backward_h๋Š” ๊ฐ๊ฐ ์ •๋ฐฉํ–ฅ LSTM์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์—ญ๋ฐฉํ–ฅ LSTM์˜ ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‘ ๊ฐ’์„ ์—ฐ๊ฒฐํ•œ ๊ฐ’์ด hidden_states์— ์ถœ๋ ฅ๋˜๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•œ ์‹ค์Šต์€ 'RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ฑ•ํ„ฐ'์—์„œ์˜ ํ•œ๊ตญ์–ด ์ŠคํŒ€ ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‹ค์Šต( https://wikidocs.net/94748 )์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. ์ •๋ฐฉํ–ฅ LSTM์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’๊ณผ ์—ญ๋ฐฉํ–ฅ LSTM์˜ ์ฒซ ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ์ •๋ฐฉํ–ฅ LSTM์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’ : [0.6303139 0.6303139 0.6303139] ์—ญ๋ฐฉํ–ฅ LSTM์˜ ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’ : [0.70387346 0.70387346 0.70387346] ํ˜„์žฌ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์„ ๊ณ ์ •์‹œ์ผœ๋‘์—ˆ๊ธฐ ๋•Œ๋ฌธ์— return_sequences๋ฅผ True๋กœ ํ•  ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ด ์–ด๋–ป๊ฒŒ ๋ฐ”๋€Œ๋Š”์ง€ ๋น„๊ต๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. bilstm = Bidirectional(LSTM(3, return_sequences=True, return_state=True, \ kernel_initializer=k_init, bias_initializer=b_init, recurrent_initializer=r_init)) hidden_states, forward_h, forward_c, backward_h, backward_c = bilstm(train_X) print('hidden states : {}, shape: {}'.format(hidden_states, hidden_states.shape)) print('forward state : {}, shape: {}'.format(forward_h, forward_h.shape)) print('backward state : {}, shape: {}'.format(backward_h, backward_h.shape)) hidden states : [[[0.3590648 0.3590648 0.3590648 0.70387346 0.70387346 0.70387346] [0.5511133 0.5511133 0.5511133 0.5886358 0.5886358 0.5886358 ] [0.5911575 0.5911575 0.5911575 0.39516988 0.39516988 0.39516988] [0.6303139 0.6303139 0.6303139 0.21942243 0.21942243 0.21942243]]], shape: (1, 4, 6) forward state : [[0.6303139 0.6303139 0.6303139]], shape: (1, 3) backward state : [[0.70387346 0.70387346 0.70387346]], shape: (1, 3) hidden states์˜ ์ถœ๋ ฅ๊ฐ’์—์„œ๋Š” ์ด์ œ ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์—ญ๋ฐฉํ–ฅ LSTM์˜ ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋Š” ๋” ์ด์ƒ ์ •๋ฐฉํ–ฅ LSTM์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์—ฐ๊ฒฐ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ •๋ฐฉํ–ฅ LSTM์˜ ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—ฐ๊ฒฐ๋˜์–ด ๋‹ค์Œ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 08-05 RNN ์–ธ์–ด ๋ชจ๋ธ(Recurrent Neural Network Language Model, RNNLM) RNN์„ ์ด์šฉํ•˜์—ฌ ์–ธ์–ด ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•œ RNN ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. 1. RNN ์–ธ์–ด ๋ชจ๋ธ(Recurrent Neural Network Language Model, RNNLM) ์•ž์„œ n-gram ์–ธ์–ด ๋ชจ๋ธ๊ณผ NNLM์€ ๊ณ ์ •๋œ ๊ฐœ์ˆ˜์˜ ๋‹จ์–ด๋งŒ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„์•ผ ํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹œ์ (time step)์ด๋ผ๋Š” ๊ฐœ๋…์ด ๋„์ž…๋œ RNN์œผ๋กœ ์–ธ์–ด ๋ชจ๋ธ์„ ๋งŒ๋“ค๋ฉด ์ž…๋ ฅ์˜ ๊ธธ์ด๋ฅผ ๊ณ ์ •ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ RNN์œผ๋กœ ๋งŒ๋“  ์–ธ์–ด ๋ชจ๋ธ์„ RNNLM(Recurrent Neural Network Language Model)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. RNNLM์ด ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ํ•™์Šตํ•˜๋Š” ๊ณผ์ •์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ์œ„ํ•ด ๊ฐ„์†Œํ™”๋œ ํ˜•ํƒœ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฌธ : 'what will the fat cat sit on' ์˜ˆ๋ฅผ ๋“ค์–ด ํ›ˆ๋ จ ์ฝ”ํผ์Šค์— ์œ„์™€ ๊ฐ™์€ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์€ ์ฃผ์–ด์ง„ ๋‹จ์–ด ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ RNNLM์ด ์–ด๋–ป๊ฒŒ ์ด์ „ ์‹œ์ ์˜ ๋‹จ์–ด๋“ค๊ณผ ํ˜„์žฌ ์‹œ์ ์˜ ๋‹จ์–ด๋กœ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. RNNLM์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ˆ์ธก ๊ณผ์ •์—์„œ ์ด์ „ ์‹œ์ ์˜ ์ถœ๋ ฅ์„ ํ˜„์žฌ ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. RNNLM์€ what์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด, will์„ ์˜ˆ์ธกํ•˜๊ณ  ์ด will์€ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์ด ๋˜์–ด the๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  the๋Š” ๋˜๋‹ค์‹œ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์ด ๋˜๊ณ  ํ•ด๋‹น ์‹œ์ ์—์„œ๋Š” fat์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋˜ํ•œ ๋‹ค์‹œ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์„ธ ๋ฒˆ์งธ ์‹œ์ ์—์„œ fat์€ ์•ž์„œ ๋‚˜์˜จ what, will, the๋ผ๋Š” ์‹œํ€€์Šค๋กœ ์ธํ•ด ๊ฒฐ์ •๋œ ๋‹จ์–ด์ด๋ฉฐ, ๋„ค ๋ฒˆ์งธ ์‹œ์ ์˜ cat์€ ์•ž์„œ ๋‚˜์˜จ what, will, the, fat์ด๋ผ๋Š” ์‹œํ€€์Šค๋กœ ์ธํ•ด ๊ฒฐ์ •๋œ ๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์œ„ ๊ณผ์ •์€ ํ›ˆ๋ จ์ด ๋๋‚œ ๋ชจ๋ธ์˜ ํ…Œ์ŠคํŠธ ๊ณผ์ • ๋™์•ˆ(์‹ค์ œ ์‚ฌ์šฉํ•  ๋•Œ)์˜ ์ด์•ผ๊ธฐ์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ์ด์ „ ์‹œ์ ์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์œผ๋ฉด์„œ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, what will the fat cat sit on๋ผ๋Š” ํ›ˆ๋ จ ์ƒ˜ํ”Œ์ด ์žˆ๋‹ค๋ฉด, what will the fat cat sit ์‹œํ€€์Šค๋ฅผ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์œผ๋ฉด, will the fat cat sit on๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ํ›ˆ๋ จ๋ฉ๋‹ˆ๋‹ค. will, the, fat, cat, sit, on๋Š” ๊ฐ ์‹œ์ ์˜ ๋ ˆ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ RNN ํ›ˆ๋ จ ๊ธฐ๋ฒ•์„ ๊ต์‚ฌ ๊ฐ•์š”(teacher forcing)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ต์‚ฌ ๊ฐ•์š”(teacher forcing)๋ž€, ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ t ์‹œ์ ์˜ ์ถœ๋ ฅ์ด t+1 ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” RNN ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ›ˆ๋ จ ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จํ•  ๋•Œ ๊ต์‚ฌ ๊ฐ•์š”๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ๋ชจ๋ธ์ด t ์‹œ์ ์—์„œ ์˜ˆ์ธกํ•œ ๊ฐ’์„ t+1 ์‹œ์ ์— ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , t ์‹œ์ ์˜ ๋ ˆ์ด๋ธ”. ์ฆ‰, ์‹ค์ œ ์•Œ๊ณ  ์žˆ๋Š” ์ •๋‹ต์„ t+1 ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก , ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋„ ์ด์ „ ์‹œ์ ์˜ ์ถœ๋ ฅ์„ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด์„œ ํ›ˆ๋ จ ์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ด๋Š” ํ•œ ๋ฒˆ ์ž˜๋ชป ์˜ˆ์ธกํ•˜๋ฉด ๋’ค์—์„œ์˜ ์˜ˆ์ธก๊นŒ์ง€ ์˜ํ–ฅ์„ ๋ฏธ์ณ ํ›ˆ๋ จ ์‹œ๊ฐ„์ด ๋Š๋ ค์ง€๊ฒŒ ๋˜๋ฏ€๋กœ ๊ต์‚ฌ ๊ฐ•์š”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ RNN์„ ์ข€ ๋” ๋น ๋ฅด๊ณ  ํšจ๊ณผ์ ์œผ๋กœ ํ›ˆ๋ จ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๊ณผ์ • ๋™์•ˆ ์ถœ๋ ฅ์ธต์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•œ ๊ฐ’๊ณผ ์‹ค์ œ ๋ ˆ์ด๋ธ”๊ณผ์˜ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ์•ž์„œ ๋ฐฐ์šด NNLM์˜ ๊ทธ๋ฆผ๊ณผ ์œ ์‚ฌํ•œ ํ˜•ํƒœ๋กœ RNNLM์„ ๋‹ค์‹œ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. RNNLM์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. RNNLM์€ ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ด 4๊ฐœ์˜ ์ธต(layer)์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ์šฐ์„  ์ž…๋ ฅ์ธต(input layer)์„ ๋ด…์‹œ๋‹ค. RNNLM์˜ ํ˜„์‹œ์ (timestep)์€ 4๋กœ ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ 4๋ฒˆ์งธ ์ž…๋ ฅ ๋‹จ์–ด์ธ fat์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต(output layer)์„ ๋ด…์‹œ๋‹ค. ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ์ •๋‹ต์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด cat์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ์ถœ๋ ฅ์ธต์—์„œ ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•œ ๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์˜ค์ฐจ๋กœ๋ถ€ํ„ฐ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ํ•™์Šต์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์กฐ๊ธˆ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ˜„์‹œ์ ์˜ ์ž…๋ ฅ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ t ๋ฅผ ์ž…๋ ฅ๋ฐ›์€ RNNLM์€ ์šฐ์„  ์ž„๋ฒ ๋”ฉ์ธต(embedding layer)์„ ์ง€๋‚ฉ๋‹ˆ๋‹ค. ์ด ์ž„๋ฒ ๋”ฉ์ธต์€ ๊ธฐ๋ณธ์ ์œผ๋กœ NNLM์—์„œ ๋ฐฐ์šด ํˆฌ์‚ฌ์ธต(projection layer)์ž…๋‹ˆ๋‹ค. NNLM์—์„œ๋Š” ๋ฃฉ์—… ํ…Œ์ด๋ธ”์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ธต์„ ํˆฌ์‚ฌ์ธต๋ผ๊ณ  ํ‘œํ˜„ํ–ˆ์ง€๋งŒ, ์ด๋ฏธ ํˆฌ์‚ฌ์ธต์˜ ๊ฒฐ๊ณผ๋กœ ์–ป๋Š” ๋ฒกํ„ฐ๋ฅผ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค๊ณ  NNLM์—์„œ ํ•™์Šตํ•˜์˜€์œผ๋ฏ€๋กœ, ์•ž์œผ๋กœ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ํˆฌ์‚ฌ์ธต์„ ์ž„๋ฒ ๋”ฉ์ธต(embedding layer)์ด๋ผ๋Š” ํ‘œํ˜„์„ ์‚ฌ์šฉํ•  ๊ฒ๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ V ์ผ ๋•Œ, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ M์œผ๋กœ ์„ค์ •ํ•˜๋ฉด, ๊ฐ ์ž…๋ ฅ ๋‹จ์–ด๋“ค์€ ์ž„๋ฒ ๋”ฉ์ธต์—์„œ V ร— M ํฌ๊ธฐ์˜ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ๊ณผ ๊ณฑํ•ด์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ V๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 7์ด๊ณ , M์ด 5๋ผ๋ฉด ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์€ 7 ร— 5 ํ–‰๋ ฌ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์€ ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜๋“ค๊ณผ ํ•จ๊ป˜ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” NNLM์—์„œ ์ด๋ฏธ ๋ฐฐ์šด ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ์ธต : t l o u ( t ) ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ๋‹ค์‹œ RNN์„ ๋ณต์Šตํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ์€๋‹‰์ธต์—์„œ ์ด์ „ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ธ t 1 ๊ณผ ํ•จ๊ป˜ ๋‹ค์Œ์˜ ์—ฐ์‚ฐ์„ ํ•˜์—ฌ ํ˜„์žฌ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ t ๋ฅผ ๊ณ„์‚ฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต : t t n ( x t W h โˆ’ + ) ์ถœ๋ ฅ์ธต์—์„œ๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, V ์ฐจ์›์˜ ๋ฒกํ„ฐ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋ฉด์„œ ๊ฐ ์›์†Œ๋Š” 0๊ณผ 1์‚ฌ์ด์˜ ์‹ค์ˆซ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ์ดํ•ฉ์€ 1์ด ๋˜๋Š” ์ƒํƒœ๋กœ ๋ฐ”๋€๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‚˜์˜จ ๋ฒกํ„ฐ๋ฅผ RNNLM์˜ t ์‹œ์ ์˜ ์˜ˆ์ธก๊ฐ’์ด๋ผ๋Š” ์˜๋ฏธ์—์„œ t๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต : t = o t a ( y t b ) ๋ฒกํ„ฐ t์˜ ๊ฐ ์ฐจ์› ์•ˆ์—์„œ์˜ ๊ฐ’์ด ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. t์˜ j ๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ€์ง„ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์€ j ๋ฒˆ์งธ ๋‹จ์–ด๊ฐ€ ๋‹ค์Œ ๋‹จ์–ด์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  t๋Š” ์‹ค์ œ ๊ฐ’. ์ฆ‰, ์‹ค์ œ ์ •๋‹ต์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด์ธ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๊ฐ’์— ๊ฐ€๊นŒ์›Œ์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’์— ํ•ด๋‹น๋˜๋Š” ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ์ด ๋‘ ๋ฒกํ„ฐ๊ฐ€ ๊ฐ€๊นŒ์›Œ์ง€๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ RNNLM๋Š” ์†์‹ค ํ•จ์ˆ˜๋กœ cross-entropy ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ญ์ „ํŒŒ๊ฐ€ ์ด๋ฃจ์–ด์ง€๋ฉด์„œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๋“ค์ด ํ•™์Šต๋˜๋Š”๋ฐ, ์ด ๊ณผ์ •์—์„œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’๋“ค๋„ ํ•™์Šต์ด ๋ฉ๋‹ˆ๋‹ค. ๋ฃฉ์—… ํ…Œ์ด๋ธ”์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ํ…Œ์ด๋ธ”์ธ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์„๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ๊ฒฐ๊ณผ์ ์œผ๋กœ RNNLM์—์„œ ํ•™์Šต ๊ณผ์ •์—์„œ ํ•™์Šต๋˜๋Š” ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์€ ๋‹ค์Œ์˜, x W, y 4๊ฐœ์ž…๋‹ˆ๋‹ค. ๋’ค์˜ ๊ธ€์ž ๋‹จ์œ„ RNN ์‹ค์Šต์—์„œ RNN ์–ธ์–ด ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด ๋ณด๋ฉด์„œ ํ›ˆ๋ จ ๊ณผ์ •๊ณผ ํ…Œ์ŠคํŠธ ๊ณผ์ •์˜ ์ฐจ์ด๋ฅผ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 08-06 RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ์ƒ์„ฑ(Text Generation using RNN) ๋‹ค ๋Œ€ ์ผ(many-to-one) ๊ตฌ์กฐ์˜ RNN์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•ด์„œ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. 1. RNN์„ ์ด์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ์ƒ์„ฑํ•˜๊ธฐ ์˜ˆ๋ฅผ ๋“ค์–ด์„œ '๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” ๋ง์ด ๋›ฐ๊ณ  ์žˆ๋‹ค'์™€ '๊ทธ์˜ ๋ง์ด ๋ฒ•์ด๋‹ค'์™€ '๊ฐ€๋Š” ๋ง์ด ๊ณ ์™€์•ผ ์˜ค๋Š” ๋ง์ด ๊ณฑ๋‹ค'๋ผ๋Š” ์„ธ ๊ฐ€์ง€ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๋ชจ๋ธ์ด ๋ฌธ๋งฅ์„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„๋ก ์ „์ฒด ๋ฌธ์žฅ์˜ ์•ž์˜ ๋‹จ์–ด๋“ค์„ ์ „๋ถ€ ๊ณ ๋ คํ•˜์—ฌ ํ•™์Šตํ•˜๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ์žฌ๊ตฌ์„ฑํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ด 11๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. samples y 1 ๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” 2 ๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” ๋ง์ด 3 ๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” ๋ง์ด ๋›ฐ๊ณ  4 ๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” ๋ง์ด ๋›ฐ๊ณ  ์žˆ๋‹ค 5 ๊ทธ์˜ ๋ง์ด 6 ๊ทธ์˜ ๋ง์ด ๋ฒ•์ด๋‹ค 7 ๊ฐ€๋Š” ๋ง์ด 8 ๊ฐ€๋Š” ๋ง์ด ๊ณ ์™€์•ผ 9 ๊ฐ€๋Š” ๋ง์ด ๊ณ ์™€์•ผ ์˜ค๋Š” 10 ๊ฐ€๋Š” ๋ง์ด ๊ณ ์™€์•ผ ์˜ค๋Š” ๋ง์ด 11 ๊ฐ€๋Š” ๋ง์ด ๊ณ ์™€์•ผ ์˜ค๋Š” ๋ง์ด ๊ณฑ๋‹ค 1) ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ import numpy as np from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical ์šฐ์„  ์˜ˆ์ œ๋กœ ์–ธ๊ธ‰ํ•œ 3๊ฐœ์˜ ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. text = """๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” ๋ง์ด ๋›ฐ๊ณ  ์žˆ๋‹ค\n ๊ทธ์˜ ๋ง์ด ๋ฒ•์ด๋‹ค\n ๊ฐ€๋Š” ๋ง์ด ๊ณ ์™€์•ผ ์˜ค๋Š” ๋ง์ด ๊ณฑ๋‹ค\n""" ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•˜๊ณ  ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ €์žฅํ•  ๋•Œ๋Š” ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์€ ์ธ๋ฑ์Šค๊ฐ€ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€๋งŒ, ํŒจ๋”ฉ์„ ์œ„ํ•œ 0์„ ๊ณ ๋ คํ•˜์—ฌ +1์„ ํ•ด์ค๋‹ˆ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts([text]) vocab_size = len(tokenizer.word_index) + 1 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : %d' % vocab_size) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 12 ๊ฐ ๋‹จ์–ด์™€ ๋‹จ์–ด์— ๋ถ€์—ฌ๋œ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(tokenizer.word_index) {'๋ง์ด': 1, '๊ฒฝ๋งˆ์žฅ์—': 2, '์žˆ๋Š”': 3, '๋›ฐ๊ณ ': 4, '์žˆ๋‹ค': 5, '๊ทธ์˜': 6, '๋ฒ•์ด๋‹ค': 7, '๊ฐ€๋Š”': 8, '๊ณ ์™€์•ผ': 9, '์˜ค๋Š”': 10, '๊ณฑ๋‹ค': 11} ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. sequences = list() for line in text.split('\n'): # ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์žฅ ํ† ํฐํ™” encoded = tokenizer.texts_to_sequences([line])[0] for i in range(1, len(encoded)): sequence = encoded[:i+1] sequences.append(sequence) print('ํ•™์Šต์— ์‚ฌ์šฉํ•  ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜: %d' % len(sequences)) ํ•™์Šต์— ์‚ฌ์šฉํ•  ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜: 11 ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋Š” ์ด 11๊ฐœ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ „์ฒด ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(sequences) [[2, 3], [2, 3, 1], [2, 3, 1, 4], [2, 3, 1, 4, 5], [6, 1], [6, 1, 7], [8, 1], [8, 1, 9], [8, 1, 9, 10], [8, 1, 9, 10, 1], [8, 1, 9, 10, 1, 11]] ์œ„์˜ ๋ฐ์ดํ„ฐ๋Š” ์•„์ง ๋ ˆ์ด๋ธ”๋กœ ์‚ฌ์šฉ๋  ๋‹จ์–ด๋ฅผ ๋ถ„๋ฆฌํ•˜์ง€ ์•Š์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. [2, 3]์€ [๊ฒฝ๋งˆ์žฅ์—, ์žˆ๋Š”]์— ํ•ด๋‹น๋˜๋ฉฐ [2, 3, 1]์€ [๊ฒฝ๋งˆ์žฅ์—, ์žˆ๋Š”, ๋ง์ด]์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋งจ ์šฐ์ธก์— ์žˆ๋Š” ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋งŒ ๋ ˆ์ด๋ธ”๋กœ ๋ถ„๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ „์ฒด ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๊ธธ์ด๋ฅผ ์ผ์น˜์‹œ์ผœ ์ค๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ์œก์•ˆ์œผ๋กœ ๋ดค์„ ๋•Œ, ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์€ [8, 1, 9, 10, 1, 11]์ด๊ณ  ๊ธธ์ด๋Š” 6์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ฝ”๋“œ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. max_len = max(len(l) for l in sequences) # ๋ชจ๋“  ์ƒ˜ํ”Œ์—์„œ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด ์ถœ๋ ฅ print('์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : {}'.format(max_len)) ์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 6 ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๊ฐ€ 6์ž„์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ „์ฒด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ 6์œผ๋กœ ํŒจ๋”ฉ ํ•ฉ๋‹ˆ๋‹ค. sequences = pad_sequences(sequences, maxlen=max_len, padding='pre') pad_sequences()๋Š” ๋ชจ๋“  ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ 0์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธธ์ด๋ฅผ ๋งž์ถฐ์ค๋‹ˆ๋‹ค. maxlen์˜ ๊ฐ’์œผ๋กœ 6์„ ์ฃผ๋ฉด ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ 6์œผ๋กœ ๋งž์ถฐ์ฃผ๋ฉฐ, padding์˜ ์ธ์ž๋กœ 'pre'๋ฅผ ์ฃผ๋ฉด ๊ธธ์ด๊ฐ€ 6๋ณด๋‹ค ์งง์€ ์ƒ˜ํ”Œ์˜ ์•ž์— 0์œผ๋กœ ์ฑ„์›๋‹ˆ๋‹ค. ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…๋‹ˆ๋‹ค. print(sequences) [[ 0 0 0 0 2 3] [ 0 0 0 2 3 1] [ 0 0 2 3 1 4] [ 0 2 3 1 4 5] [ 0 0 0 0 6 1] [ 0 0 0 6 1 7] [ 0 0 0 0 8 1] [ 0 0 0 8 1 9] [ 0 0 8 1 9 10] [ 0 8 1 9 10 1] [ 8 1 9 10 1 11]] ๊ธธ์ด๊ฐ€ 6๋ณด๋‹ค ์งง์€ ๋ชจ๋“  ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์•ž์— 0์„ ์ฑ„์›Œ์„œ ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ 6์œผ๋กœ ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ ์ƒ˜ํ”Œ์˜ ๋งˆ์ง€๋ง‰ ๋‹จ์–ด๋ฅผ ๋ ˆ์ด๋ธ”๋กœ ๋ถ„๋ฆฌํ•ฉ์‹œ๋‹ค. ๋ ˆ์ด๋ธ”์˜ ๋ถ„๋ฆฌ๋Š” Numpy๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์ œ์™ธํ•˜๊ณ  ์ €์žฅํ•œ ๊ฒƒ์€ X, ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’๋งŒ ์ €์žฅํ•œ ๊ฒƒ์€ y. ์ด๋Š” ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. sequences = np.array(sequences) X = sequences[:,:-1] y = sequences[:,-1] ๋ถ„๋ฆฌ๋œ X์™€ y์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. print(X) [[ 0 0 0 0 2] [ 0 0 0 2 3] [ 0 0 2 3 1] [ 0 2 3 1 4] [ 0 0 0 0 6] [ 0 0 0 6 1] [ 0 0 0 0 8] [ 0 0 0 8 1] [ 0 0 8 1 9] [ 0 8 1 9 10] [ 8 1 9 10 1]] print(y) [ 3 1 4 5 1 7 1 9 10 1 11] ๋ ˆ์ด๋ธ”์ด ๋ถ„๋ฆฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. RNN ๋ชจ๋ธ์— ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ ์‹œํ‚ค๊ธฐ ์ „์— ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. y = to_categorical(y, num_classes=vocab_size) ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋˜์—ˆ๋Š”์ง€ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. print(y) [[0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] # 3์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] # 1์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.] # 4์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] # 5์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] # 1์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] # 7์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] # 1์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] # 9์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.] # 10์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] # 1์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]] # 11์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ ์ •์ƒ์ ์œผ๋กœ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ๋ชจ๋ธ ์„ค๊ณ„ํ•˜๊ธฐ RNN ๋ชจ๋ธ์— ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ์‹œํ‚ต๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Embedding, Dense, SimpleRNN ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 10, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 32์ž…๋‹ˆ๋‹ค. ๋‹ค ๋Œ€ ์ผ ๊ตฌ์กฐ์˜ RNN์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ „๊ฒฐํ•ฉ์ธต(Fully Connected Layer)์„ ์ถœ๋ ฅ์ธต์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ ํฌ๊ธฐ๋งŒํผ์˜ ๋‰ด๋Ÿฐ์„ ๋ฐฐ์น˜ํ•˜์—ฌ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๋‹จ์–ด ์ค‘ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 200 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. embedding_dim = 10 hidden_units = 32 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(SimpleRNN(hidden_units)) model.add(Dense(vocab_size, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X, y, epochs=200, verbose=2) ๋ชจ๋ธ์ด ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•˜๊ณ  ์žˆ๋Š”์ง€ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. def sentence_generation(model, tokenizer, current_word, n): # ๋ชจ๋ธ, ํ† ํฌ ๋‚˜์ด์ €, ํ˜„์žฌ ๋‹จ์–ด, ๋ฐ˜๋ณตํ•  ํšŸ์ˆ˜ init_word = current_word sentence = '' # n ๋ฒˆ ๋ฐ˜๋ณต for _ in range(n): # ํ˜„์žฌ ๋‹จ์–ด์— ๋Œ€ํ•œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ํŒจ๋”ฉ encoded = tokenizer.texts_to_sequences([current_word])[0] encoded = pad_sequences([encoded], maxlen=5, padding='pre') # ์ž…๋ ฅํ•œ X(ํ˜„์žฌ ๋‹จ์–ด)์— ๋Œ€ํ•ด์„œ Y๋ฅผ ์˜ˆ์ธกํ•˜๊ณ  Y(์˜ˆ์ธกํ•œ ๋‹จ์–ด)๋ฅผ result์— ์ €์žฅ. result = model.predict(encoded, verbose=0) result = np.argmax(result, axis=1) for word, index in tokenizer.word_index.items(): # ๋งŒ์•ฝ ์˜ˆ์ธกํ•œ ๋‹จ์–ด์™€ ์ธ๋ฑ์Šค์™€ ๋™์ผํ•œ ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด break if index == result: break # ํ˜„์žฌ ๋‹จ์–ด + ' ' + ์˜ˆ์ธก ๋‹จ์–ด๋ฅผ ํ˜„์žฌ ๋‹จ์–ด๋กœ ๋ณ€๊ฒฝ current_word = current_word + ' ' + word # ์˜ˆ์ธก ๋‹จ์–ด๋ฅผ ๋ฌธ์žฅ์— ์ €์žฅ sentence = sentence + ' ' + word sentence = init_word + sentence return sentence ์ž…๋ ฅ๋œ ๋‹จ์–ด๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ด์„œ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. '๊ฒฝ๋งˆ์žฅ์—'๋ผ๋Š” ๋‹จ์–ด ๋’ค์—๋Š” ์ด 4๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ 4๋ฒˆ ์˜ˆ์ธกํ•ด ๋ด…์‹œ๋‹ค. print(sentence_generation(model, tokenizer, '๊ฒฝ๋งˆ์žฅ์—', 4)) ๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” ๋ง์ด ๋›ฐ๊ณ  ์žˆ๋‹ค print(sentence_generation(model, tokenizer, '๊ทธ์˜', 2)) ๊ทธ์˜ ๋ง์ด ๋ฒ•์ด๋‹ค print(sentence_generation(model, tokenizer, '๊ฐ€๋Š”', 5)) ๊ฐ€๋Š” ๋ง์ด ๊ณ ์™€์•ผ ์˜ค๋Š” ๋ง์ด ๊ณฑ๋‹ค ์•ž์˜ ๋ฌธ๋งฅ์„ ๊ธฐ์ค€์œผ๋กœ '๋ง์ด'๋ผ๋Š” ๋‹จ์–ด ๋‹ค์Œ์— ๋‚˜์˜ฌ ๋‹จ์–ด๋ฅผ ๊ธฐ์กด์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ์ผ์น˜ํ•˜๊ฒŒ ์˜ˆ์ธกํ•จ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์ถฉ๋ถ„ํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ๋ชปํ•˜๋ฏ€๋กœ ์œ„์—์„œ ๋ฌธ์žฅ์˜ ๊ธธ์ด์— ๋งž๊ฒŒ ์ ์ ˆํ•˜๊ฒŒ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ํšŸ์ˆ˜ 4, 2, 5๋ฅผ ๊ฐ๊ฐ ์ธ์ž ๊ฐ’์œผ๋กœ ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ฃผ๋ฉด ๊ธฐ๊ณ„๋Š” '์žˆ๋‹ค', '๋ฒ•์ด๋‹ค', '๊ณฑ๋‹ค' ๋‹ค์Œ์— ๋‚˜์˜ค๋Š” ๋‹จ์–ด๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๋ฐฐ์šด ์ ์ด ์—†์œผ๋ฏ€๋กœ ์ž„์˜ ์˜ˆ์ธก์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋” ๋งŽ์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. 2. LSTM์„ ์ด์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ์ƒ์„ฑํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” LSTM์„ ํ†ตํ•ด ๋ณด๋‹ค ๋งŽ์€ ๋ฐ์ดํ„ฐ๋กœ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณธ์งˆ์ ์œผ๋กœ ์•ž์—์„œ ํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•œ ์‹ค์Šต์ž…๋‹ˆ๋‹ค. 1) ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ๋‰ด์š• ํƒ€์ž„์Šค ๊ธฐ์‚ฌ์˜ ์ œ๋ชฉ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋งํฌ์—์„œ ArticlesApril2018.csv ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://www.kaggle.com/aashita/nyt-comments import pandas as pd import numpy as np from string import punctuation from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical ๋‹ค์šด๋กœ๋“œํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. df = pd.read_csv('ArticlesApril2018.csv') df.head() ์ฑ…์˜ ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ์ด๋ฒˆ ์ถœ๋ ฅ ํ™”๋ฉด์€ ์ƒ๋žต ์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ต‰์žฅํžˆ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ˆˆ์— ๋ณด๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ์—ด์ด ์žˆ๊ณ , ์—ด์ด ์ด ๋ช‡ ๊ฐœ๊ฐ€ ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print('์—ด์˜ ๊ฐœ์ˆ˜: ',len(df.columns)) print(df.columns) ์—ด์˜ ๊ฐœ์ˆ˜: 15 Index(['articleID', 'articleWordCount', 'byline', 'documentType', 'headline', 'keywords', 'multimedia', 'newDesk', 'printPage', 'pubDate', 'sectionName', 'snippet', 'source', 'typeOfMaterial', 'webURL'], dtype='object') ์ด 15๊ฐœ์˜ ์—ด์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  ์—ด์€ ์ œ๋ชฉ์— ํ•ด๋‹น๋˜๋Š” headline ์—ด์ž…๋‹ˆ๋‹ค. Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(df['headline'].isnull().values.any()) False Null ๊ฐ’์€ ๋ณ„๋„๋กœ ์—†๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. headline ์—ด์—์„œ ๋ชจ๋“  ์‹ ๋ฌธ ๊ธฐ์‚ฌ์˜ ์ œ๋ชฉ์„ ๋ฝ‘์•„์„œ ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. headline = [] # ํ—ค๋“œ๋ผ์ธ์˜ ๊ฐ’๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ headline.extend(list(df.headline.values)) headline[:5] headline์ด๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์— ๋ชจ๋“  ์‹ ๋ฌธ ๊ธฐ์‚ฌ์˜ ์ œ๋ชฉ์„ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์ €์žฅํ•œ ๋ฆฌ์ŠคํŠธ์—์„œ ์ƒ์œ„ 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ['Former N.F.L. Cheerleadersโ€™ Settlement Offer: $1 and a Meeting With Goodell', 'E.P.A. to Unveil a New Rule. Its Effect: Less Science in Policymaking.', 'The New Noma, Explained', 'Unknown', 'Unknown'] ๋„ค ๋ฒˆ์งธ์™€ ๋‹ค์„ฏ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์— Unknown ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. headline ์ „์ฒด์— ๊ฑธ์ณ์„œ Unknown ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์žˆ์„ ๊ฒƒ์œผ๋กœ ์ถ”์ •๋ฉ๋‹ˆ๋‹ค. ๋น„๋ก Null ๊ฐ’์€ ์•„๋‹ˆ์ง€๋งŒ ์‹ค์Šต์— ๋„์›€์ด ๋˜์ง€ ์•Š๋Š” ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ์ด๋ฏ€๋กœ ์ œ๊ฑฐํ•ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ œ๊ฑฐํ•˜๊ธฐ ์ „์— ํ˜„์žฌ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ณด๊ณ  ์ œ๊ฑฐ ์ „, ํ›„์˜ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. print('์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : {}'.format(len(headline))) ์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 1324 ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์ „ ์‹ ๋ฌธ ๊ธฐ์‚ฌ์˜ ์ œ๋ชฉ ์ƒ˜ํ”Œ์€ ์ด 1,324๊ฐœ์ž…๋‹ˆ๋‹ค. Unknown ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. headline = [word for word in headline if word != "Unknown"] print('๋…ธ์ด์ฆˆ ๊ฐ’ ์ œ๊ฑฐ ํ›„ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : {}'.format(len(headline))) ๋…ธ์ด์ฆˆ ๊ฐ’ ์ œ๊ฑฐ ํ›„ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 1214 ์ƒ˜ํ”Œ์˜ ์ˆ˜๊ฐ€ 1,324์—์„œ 1,214๋กœ 110๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์ œ๊ฑฐ๋˜์—ˆ๋Š”๋ฐ ๊ธฐ์กด์— ์ถœ๋ ฅํ–ˆ๋˜ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. headline[:5] ['Former N.F.L. Cheerleadersโ€™ Settlement Offer: $1 and a Meeting With Goodell', 'E.P.A. to Unveil a New Rule. Its Effect: Less Science in Policymaking.', 'The New Noma, Explained', 'How a Bag of Texas Dirt Became a Times Tradition', 'Is School a Place for Self-Expression?'] ๊ธฐ์กด์— ๋„ค ๋ฒˆ์งธ, ๋‹ค์„ฏ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์—์„œ๋Š” Unknown ๊ฐ’์ด ์žˆ์—ˆ๋Š”๋ฐ ํ˜„์žฌ๋Š” ์ œ๊ฑฐ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์„ ํƒํ•œ ์ „์ฒ˜๋ฆฌ๋Š” ๊ตฌ๋‘์  ์ œ๊ฑฐ์™€ ๋‹จ์–ด์˜ ์†Œ๋ฌธ์žํ™”์ž…๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , ๋‹ค์‹œ ์ƒ˜ํ”Œ 5๊ฐœ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. def repreprocessing(raw_sentence): preproceseed_sentence = raw_sentence.encode("utf8").decode("ascii",'ignore') # ๊ตฌ๋‘์  ์ œ๊ฑฐ์™€ ๋™์‹œ์— ์†Œ๋ฌธ์žํ™” return ''.join(word for word in preproceseed_sentence if word not in punctuation).lower() preprocessed_headline = [repreprocessing(x) for x in headline] preprocessed_headline[:5] ['former nfl cheerleaders settlement offer 1 and a meeting with goodell', 'epa to unveil a new rule its effect less science in policymaking', 'the new noma explained', 'how a bag of texas dirt became a times tradition', 'is school a place for selfexpression'] ๊ธฐ์กด์˜ ์ถœ๋ ฅ๊ณผ ๋น„๊ตํ•˜๋ฉด ๋ชจ๋“  ๋‹จ์–ด๋“ค์ด ์†Œ๋ฌธ์žํ™”๋˜์—ˆ์œผ๋ฉฐ N.F.L.์ด๋‚˜ Cheerleadersโ€™ ๋“ฑ๊ณผ ๊ฐ™์ด ๊ธฐ์กด์— ๊ตฌ๋‘์ ์ด ๋ถ™์–ด์žˆ๋˜ ๋‹จ์–ด๋“ค์—์„œ ๊ตฌ๋‘์ ์ด ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ๋งŒ๋“ค๊ณ  ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(preprocessed_headline) vocab_size = len(tokenizer.word_index) + 1 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : %d' % vocab_size) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 3494 ์ด 3,494๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•˜๋Š” ๋™์‹œ์— ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์„ ์—ฌ๋Ÿฌ ์ค„๋กœ ๋ถ„ํ•ดํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. sequences = list() for sentence in preprocessed_headline: # ๊ฐ ์ƒ˜ํ”Œ์— ๋Œ€ํ•œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ encoded = tokenizer.texts_to_sequences([sentence])[0] for i in range(1, len(encoded)): sequence = encoded[:i+1] sequences.append(sequence) sequences[:11] [[99, 269], # former nfl [99, 269, 371], # former nfl cheerleaders [99, 269, 371, 1115], # former nfl cheerleaders settlement [99, 269, 371, 1115, 582], # former nfl cheerleaders settlement offer [99, 269, 371, 1115, 582, 52], # 'former nfl cheerleaders settlement offer 1 [99, 269, 371, 1115, 582, 52, 7], # former nfl cheerleaders settlement offer 1 and [99, 269, 371, 1115, 582, 52, 7, 2], # ... ์ดํ•˜ ์ƒ๋žต ... [99, 269, 371, 1115, 582, 52, 7, 2, 372], [99, 269, 371, 1115, 582, 52, 7, 2, 372, 10], [99, 269, 371, 1115, 582, 52, 7, 2, 372, 10, 1116], # ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ์‚ฌ์šฉ๋œ ์™„์ „ํ•œ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ # ๋ฐ”๋กœ ์œ„์˜ ์ค„์€ : former nfl cheerleaders settlement offer 1 and a meeting with goodell [100, 3]] # epa to์— ํ•ด๋‹น๋˜๋ฉฐ ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด ์‹œ์ž‘๋จ. ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ์ถœ๋ ฅ ๊ฒฐ๊ณผ์— ์ฃผ์„์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์™œ ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์„ ์ €๋ ‡๊ฒŒ ๋‚˜๋ˆŒ๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด '๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” ๋ง์ด ๋›ฐ๊ณ  ์žˆ๋‹ค'๋ผ๋Š” ๋ฌธ์žฅ ํ•˜๋‚˜๊ฐ€ ์žˆ์„ ๋•Œ, ์ตœ์ข…์ ์œผ๋กœ ์›ํ•˜๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ์ด์ „์— ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋“ค์„ ๋ชจ๋‘ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. samples y 1 ๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” 2 ๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” ๋ง์ด 3 ๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” ๋ง์ด ๋›ฐ๊ณ  4 ๊ฒฝ๋งˆ์žฅ์— ์žˆ๋Š” ๋ง์ด ๋›ฐ๊ณ  ์žˆ๋‹ค ์œ„์˜ sequences๋Š” ๋ชจ๋“  ๋ฌธ์žฅ์„ ๊ฐ ๋‹จ์–ด๊ฐ€ ๊ฐ ์‹œ์ (time step)๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€์ ์œผ๋กœ ๋“ฑ์žฅํ•˜๋Š” ํ˜•ํƒœ๋กœ ๋งŒ๋“ค๊ธฐ๋Š” ํ–ˆ์ง€๋งŒ, ์•„์ง ์˜ˆ์ธกํ•  ๋‹จ์–ด์— ํ•ด๋‹น๋˜๋Š” ๋ ˆ์ด๋ธ”์„ ๋ถ„๋ฆฌํ•˜๋Š” ์ž‘์—…๊นŒ์ง€๋Š” ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์€ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์–ด๋–ค ์ •์ˆ˜๊ฐ€ ์–ด๋–ค ๋‹จ์–ด๋ฅผ ์˜๋ฏธํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์ธ๋ฑ์Šค๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ ์ฐพ๋Š” index_to_word๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. index_to_word = {} for key, value in tokenizer.word_index.items(): # ์ธ๋ฑ์Šค๋ฅผ ๋‹จ์–ด๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด index_to_word๋ฅผ ์ƒ์„ฑ index_to_word[value] = key print('๋นˆ๋„์ˆ˜ ์ƒ์œ„ 582๋ฒˆ ๋‹จ์–ด : {}'.format(index_to_word[582])) ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 582๋ฒˆ ๋‹จ์–ด : offer 582์ด๋ผ๋Š” ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด๋Š” ๋ณธ๋ž˜ offer๋ผ๋Š” ๋‹จ์–ด์˜€์Šต๋‹ˆ๋‹ค. ์›ํ•œ๋‹ค๋ฉด ๋‹ค๋ฅธ ์ˆซ์ž๋กœ๋„ ์‹œ๋„ํ•ด ๋ณด์„ธ์š”. ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ „์— ์ „์ฒด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ํŒจ๋”ฉ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํŒจ๋”ฉ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. max_len = max(len(l) for l in sequences) print('์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : {}'.format(max_len)) ์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 24 ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด์ธ 24๋กœ ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ํŒจ๋”ฉ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. sequences = pad_sequences(sequences, maxlen=max_len, padding='pre') print(sequences[:3]) [[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 269] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 269 371] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 269 371 1115] padding='pre'๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๊ฐ€ 24๋ณด๋‹ค ์งง์€ ๊ฒฝ์šฐ์— ์•ž์— 0์œผ๋กœ ํŒจ๋”ฉ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋งจ ์šฐ์ธก ๋‹จ์–ด๋งŒ ๋ ˆ์ด๋ธ”๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. sequences = np.array(sequences) X = sequences[:,:-1] y = sequences[:,-1] print(X[:3]) [[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 269] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 269 371] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ X์—์„œ 3๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด์•˜๋Š”๋ฐ, ๋งจ ์šฐ์ธก์— ์žˆ๋˜ ์ •์ˆซ๊ฐ’ 269, 371, 1115๊ฐ€ ์‚ฌ๋ผ์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๊ฐ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๊ฐ€ 24์—์„œ 23์œผ๋กœ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. print(y[:3]) [ 269 371 1115] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ y ์ค‘ 3๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด์•˜๋Š”๋ฐ, ๊ธฐ์กด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋งจ ์šฐ์ธก์— ์žˆ๋˜ ์ •์ˆ˜๋“ค์ด ๋ณ„๋„๋กœ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. y = to_categorical(y, num_classes=vocab_size) ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ y์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. 2) ๋ชจ๋ธ ์„ค๊ณ„ํ•˜๊ธฐ from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Embedding, Dense, LSTM ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 10, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 128์ž…๋‹ˆ๋‹ค. ๋‹ค ๋Œ€ ์ผ ๊ตฌ์กฐ์˜ LSTM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ „๊ฒฐํ•ฉ์ธต(Fully Connected Layer)์„ ์ถœ๋ ฅ์ธต์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ ํฌ๊ธฐ๋งŒํผ์˜ ๋‰ด๋Ÿฐ์„ ๋ฐฐ์น˜ํ•˜์—ฌ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๋‹จ์–ด ์ค‘ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 200 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. embedding_dim = 10 hidden_units = 128 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(LSTM(hidden_units)) model.add(Dense(vocab_size, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X, y, epochs=200, verbose=2) ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜ sentence_generation์„ ๋งŒ๋“ค์–ด์„œ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•ด ๋ด…์‹œ๋‹ค. def sentence_generation(model, tokenizer, current_word, n): # ๋ชจ๋ธ, ํ† ํฌ ๋‚˜์ด์ €, ํ˜„์žฌ ๋‹จ์–ด, ๋ฐ˜๋ณตํ•  ํšŸ์ˆ˜ init_word = current_word sentence = '' # n ๋ฒˆ ๋ฐ˜๋ณต for _ in range(n): encoded = tokenizer.texts_to_sequences([current_word])[0] encoded = pad_sequences([encoded], maxlen=max_len-1, padding='pre') # ์ž…๋ ฅํ•œ X(ํ˜„์žฌ ๋‹จ์–ด)์— ๋Œ€ํ•ด์„œ y๋ฅผ ์˜ˆ์ธกํ•˜๊ณ  y(์˜ˆ์ธกํ•œ ๋‹จ์–ด)๋ฅผ result์— ์ €์žฅ. result = model.predict(encoded, verbose=0) result = np.argmax(result, axis=1) for word, index in tokenizer.word_index.items(): # ๋งŒ์•ฝ ์˜ˆ์ธกํ•œ ๋‹จ์–ด์™€ ์ธ๋ฑ์Šค์™€ ๋™์ผํ•œ ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด if index == result: break # ํ˜„์žฌ ๋‹จ์–ด + ' ' + ์˜ˆ์ธก ๋‹จ์–ด๋ฅผ ํ˜„์žฌ ๋‹จ์–ด๋กœ ๋ณ€๊ฒฝ current_word = current_word + ' ' + word # ์˜ˆ์ธก ๋‹จ์–ด๋ฅผ ๋ฌธ์žฅ์— ์ €์žฅ sentence = sentence + ' ' + word sentence = init_word + sentence return sentence ์ž„์˜์˜ ๋‹จ์–ด 'i'์— ๋Œ€ํ•ด์„œ 10๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ์ถ”๊ฐ€ ์ƒ์„ฑํ•ด ๋ด…์‹œ๋‹ค. print(sentence_generation(model, tokenizer, 'i', 10)) i disapprove of school vouchers can i still apply for them ์ž„์˜์˜ ๋‹จ์–ด 'how'์— ๋Œ€ํ•ด์„œ 10๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ์ถ”๊ฐ€ ์ƒ์„ฑํ•ด ๋ด…์‹œ๋‹ค. print(sentence_generation(model, tokenizer, 'how', 10)) how to make facebook more accountable will so your neighbor chasing 08-07 ๋ฌธ์ž ๋‹จ์œ„ RNN(Char RNN) ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด RNN์€ ์ „๋ถ€ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ๋‹จ์œ„๊ฐ€ ๋‹จ์–ด ๋ฒกํ„ฐ์˜€์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ž…์ถœ๋ ฅ์˜ ๋‹จ์œ„๋ฅผ ๋‹จ์–ด ๋ ˆ๋ฒจ(word-level)์—์„œ ๋ฌธ์ž ๋ ˆ๋ฒจ(character-level)๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ RNN์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๋ฌธ์ž ๋‹จ์œ„ RNN์„ ๋‹ค ๋Œ€๋‹ค(Many-to-Many) ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„ํ•œ ๊ฒฝ์šฐ, ๋‹ค ๋Œ€ ์ผ(Many-to-One) ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„ํ•œ ๊ฒฝ์šฐ ๋‘ ๊ฐ€์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ด ๋‘ ๊ฐ€์ง€ ๋ชจ๋‘ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋กœ ๊ตฌํ˜„ํ•  ๊ฒƒ์€ ๋‹ค ๋Œ€๋‹ค ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. 1. ๋ฌธ์ž ๋‹จ์œ„ RNN ์–ธ์–ด ๋ชจ๋ธ(Char RNNLM) ์ด์ „ ์‹œ์ ์˜ ์˜ˆ์ธก ๋ฌธ์ž๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ž ๋‹จ์œ„ RNN ์–ธ์–ด ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ๋‹จ์–ด ๋‹จ์œ„ RNN ์–ธ์–ด ๋ชจ๋ธ๊ณผ ๋‹ค๋ฅธ ์ ์€ ๋‹จ์–ด ๋‹จ์œ„๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฌธ์ž ๋‹จ์œ„๋ฅผ ์ž…, ์ถœ๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ž„๋ฒ ๋”ฉ์ธต(embedding layer)์„ ์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์–ธ์–ด ๋ชจ๋ธ์˜ ํ›ˆ๋ จ ๊ณผ์ •๊ณผ ํ…Œ์ŠคํŠธ ๊ณผ์ •์˜ ์ฐจ์ด๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ ์ง‘์ค‘ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ๋งํฌ : http://www.gutenberg.org/files/11/11-0.txt ๊ณ ์ „ ์†Œ์„ค๋“ค์€ ์ €์ž‘๊ถŒ์— ๋ณดํ˜ธ๋ฐ›์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฌด๋ฃŒ๋กœ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋งํฌ์—์„œ '์ด์ƒํ•œ ๋‚˜๋ผ์˜ ์•จ๋ฆฌ์Šค(Aliceโ€™s Adventures in Wonderland)'๋ผ๋Š” ์†Œ์„ค์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. 1) ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๊ณ  ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋‹จ์–ด๋ฅผ ์†Œ๋ฌธ์žํ™”ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. import numpy as np import urllib.request from tensorflow.keras.utils import to_categorical # ๋ฐ์ดํ„ฐ ๋กœ๋“œ urllib.request.urlretrieve("http://www.gutenberg.org/files/11/11-0.txt", filename="11-0.txt") f = open('11-0.txt', 'rb') sentences = [] for sentence in f: # ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ํ•œ ์ค„์”ฉ ์ฝ๋Š”๋‹ค. sentence = sentence.strip() # strip()์„ ํ†ตํ•ด \r, \n์„ ์ œ๊ฑฐํ•œ๋‹ค. sentence = sentence.lower() # ์†Œ๋ฌธ์žํ™”. sentence = sentence.decode('ascii', 'ignore') # \xe2\x80\x99 ๋“ฑ๊ณผ ๊ฐ™์€ ๋ฐ”์ดํŠธ ์—ด ์ œ๊ฑฐ if len(sentence) > 0: sentences.append(sentence) f.close() ์ „์ฒ˜๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์—์„œ 5๊ฐœ์˜ ์›์†Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. sentences[:5] ['the project gutenberg e book of alices adventures in wonderland, by lewis carroll', 'this e book is for the use of anyone anywhere in the united states and', 'most other parts of the world at no cost and with almost no restrictions', 'whatsoever. you may copy it, give it away or re-use it under the terms', 'of the project gutenberg license included with this e book or online at'] ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋Š” ๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ๋Š”๋ฐ ์˜๋ฏธ ์žˆ๊ฒŒ ๋ฌธ์žฅ ํ† ํฐ ํ™”๊ฐ€ ๋œ ์ƒํƒœ๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ํ†ตํ•ฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. total_data = ' '.join(sentences) print('๋ฌธ์ž์—ด์˜ ๊ธธ์ด ๋˜๋Š” ์ด ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜: %d' % len(total_data)) ๋ฌธ์ž์—ด์˜ ๊ธธ์ด ๋˜๋Š” ์ด ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜: 159484 ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ํ†ตํ•ฉ๋˜์—ˆ๊ณ , ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” ์•ฝ 15๋งŒ 9์ฒœ์ž…๋‹ˆ๋‹ค. ์ผ๋ถ€ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(total_data[:200]) the project gutenberg e book of alices adventures in wonderland, by lewis carroll this e book is for the use of anyone anywhere in the united states and most other parts of the world at no cost and with ์ด ๋ฌธ์ž์—ด๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž ์ง‘ํ•ฉ์„ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์—๋Š” ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๋‹จ์–ด๋“ค์˜ ๋ชจ์Œ์ธ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ๋งŒ๋“ค์—ˆ์œผ๋‚˜, ์ด๋ฒˆ์— ๋งŒ๋“ค ์ง‘ํ•ฉ์€ ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์•„๋‹ˆ๋ผ ๋ฌธ์ž ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. char_vocab = sorted(list(set(total_data))) vocab_size = len(char_vocab) print ('๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(vocab_size)) ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 56 ์˜์–ด๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์ผ ๋•Œ ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ–ˆ์„ ๊ฒฝ์šฐ๋ณด๋‹ค ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ํ˜„์ €ํžˆ ์ž‘์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๋ฆฌ ํ›ˆ๋ จ ์ฝ”ํผ์Šค์— ์ˆ˜์‹ญ๋งŒ ๊ฐœ ์ด์ƒ์˜ ๋งŽ์€ ์˜์–ด ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„, ์˜์–ด ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ์ž๋Š” 26๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์•ŒํŒŒ๋ฒณ์ด ๋Œ€, ์†Œ๋ฌธ์ž๊ฐ€ ๊ตฌ๋ถ„๋œ ์ƒํƒœ๋ผ๊ณ  ํ•˜๋”๋ผ๋„ ๋ชจ๋“  ์˜์–ด ๋‹จ์–ด๋Š” ์ด 52๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๋ฐฉ๋Œ€ํ•œ ์–‘์˜ ํ…์ŠคํŠธ๋ผ๋„ ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ ๊ฒŒ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์€ ๊ตฌํ˜„๊ณผ ํ…Œ์ŠคํŠธ๋ฅผ ๊ต‰์žฅํžˆ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ด์ ์„ ๊ฐ€์ง€๋ฏ€๋กœ, RNN์˜ ๋™์ž‘ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ดํ•ด๋ฅผ ์œ„ํ•œ ํ† ์ด ํ”„๋กœ์ ํŠธ ์šฉ๋„๋กœ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ๊ฐ ๋ฌธ์ž์— ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜๊ณ  ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ๋ฌธ์ž์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ๋ถ€์—ฌ char_to_index = dict((char, index) for index, char in enumerate(char_vocab)) print('๋ฌธ์ž ์ง‘ํ•ฉ :',char_to_index) ๋ฌธ์ž ์ง‘ํ•ฉ : {' ': 0, '!': 1, '"': 2, '#': 3, '$': 4, '%': 5, "'": 6, '(': 7, ')': 8, '*': 9, ',': 10, '-': 11, '.': 12, '/': 13, '0': 14, '1': 15, '2': 16, '3': 17, '4': 18, '5': 19, '6': 20, '7': 21, '8': 22, '9': 23, ':': 24, ';': 25, '?': 26, '[': 27, ']': 28, '_': 29, 'a': 30, 'b': 31, 'c': 32, 'd': 33, 'e': 34, 'f': 35, 'g': 36, 'h': 37, 'i': 38, 'j': 39, 'k': 40, 'l': 41, 'm': 42, 'n': 43, 'o': 44, 'p': 45, 'q': 46, 'r': 47, 's': 48, 't': 49, 'u': 50, 'v': 51, 'w': 52, 'x': 53, 'y': 54, 'z': 55} ์ •์ˆ˜ 0๋ถ€ํ„ฐ 28๊นŒ์ง€๋Š” ๊ณต๋ฐฑ์„ ํฌํ•จํ•œ ๊ฐ์ข… ๊ตฌ๋‘์ , ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ์กด์žฌํ•˜๊ณ , ์ •์ˆ˜ 29๋ถ€ํ„ฐ 54๊นŒ์ง€๋Š” a๋ถ€ํ„ฐ z๊นŒ์ง€ ์ด 26๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž ์ง‘ํ•ฉ์— ํฌํ•จ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_char์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. index_to_char = {} for key, value in char_to_index.items(): index_to_char[value] = key ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๊ตฌ์„ฑ์„ ์œ„ํ•œ ๊ฐ„์†Œํ™”๋œ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— apple์ด๋ผ๋Š” ์‹œํ€€์Šค๊ฐ€ ์žˆ๊ณ , ์ž…๋ ฅ์˜ ๊ธธ์ด๋ฅผ 4๋ผ๊ณ  ์ •ํ•˜์˜€์„ ๋•Œ ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์„ฑ์€ ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ์ž…๋ ฅ์˜ ๊ธธ์ด๊ฐ€ 4์ด๋ฏ€๋กœ ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ์ถœ๋ ฅ ์‹œํ€€์Šค ๋ชจ๋‘ ๊ธธ์ด๋Š” 4๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด RNN์€ ์ด ๋„ค ๋ฒˆ์˜ ์‹œ์ ์„(timestep)์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. apple์€ ๋‹ค์„ฏ ๊ธ€์ž์ด์ง€๋งŒ ์ž…๋ ฅ์˜ ๊ธธ์ด๋Š” 4์ด๋ฏ€๋กœ 'appl'๊นŒ์ง€๋งŒ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์–ธ์–ด ๋ชจ๋ธ์€ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์„ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ๋ชจ๋ธ์ด๋ฏ€๋กœ 'pple'๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ๋ฐ์ดํ„ฐ๊ฐ€ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. # appl (์ž…๋ ฅ ์‹œํ€€์Šค) -> pple (์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ์‹œํ€€์Šค) train_X = 'appl' train_y = 'pple' ์ด์ œ 15๋งŒ 9์ฒœ์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ ๋ฌธ์ž์—ด๋กœ๋ถ€ํ„ฐ ๋‹ค์ˆ˜์˜ ์ƒ˜ํ”Œ๋“ค์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ์ •ํ•˜๊ณ , ํ•ด๋‹น ๊ธธ์ด๋งŒํผ ๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ๋“ฑ๋ถ„ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์žฅ์˜ ๊ธธ์ด๋ฅผ 60์œผ๋กœ ์ •ํ–ˆ๋Š”๋ฐ, ๊ฒฐ๊ตญ 15๋งŒ 9์ฒœ์„ 60์œผ๋กœ ๋‚˜๋ˆˆ ์ˆ˜๊ฐ€ ์ƒ˜ํ”Œ์˜ ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ช‡ ๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„์ง€ ๊ทธ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. seq_length = 60 # ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ seq_length๋กœ ๋‚˜๋ˆ„๋ฉด ์ „์ฒ˜๋ฆฌ ํ›„ ์ƒ๊ฒจ๋‚  ์ƒ˜ํ”Œ ์ˆ˜ n_samples = int(np.floor((len(total_data) - 1) / seq_length)) print ('์ƒ˜ํ”Œ์˜ ์ˆ˜ : {}'.format(n_samples)) ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 2658 ์—ฌ๊ธฐ์„œ๋Š” ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜๊ฐ€ 2,658๊ฐœ๊ฐ€ ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ง„ํ–‰๋˜์—ˆ๋Š”์ง€๋Š” ์ „์ฒ˜๋ฆฌ ํ›„ ์–ป์€ train_X์™€ train_y๋ฅผ ํ†ตํ•ด ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. train_X = [] train_y = [] for i in range(n_samples): # 0:60 -> 60:120 -> 120:180๋กœ loop๋ฅผ ๋Œ๋ฉด์„œ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ 1๊ฐœ์”ฉ pick. X_sample = total_data[i * seq_length: (i + 1) * seq_length] # ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ X_encoded = [char_to_index[c] for c in X_sample] train_X.append(X_encoded) # ์˜ค๋ฅธ์ชฝ์œผ๋กœ 1์นธ ์‹œํ”„ํŠธ y_sample = total_data[i * seq_length + 1: (i + 1) * seq_length + 1] y_encoded = [char_to_index[c] for c in y_sample] train_y.append(y_encoded) train_X์™€ train_y์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print('X ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :',train_X[0]) print('y ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :',train_y[0]) print('-'*50) print('X ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ๋””์ฝ”๋”ฉ :',[index_to_char[i] for i in train_X[0]]) print('y ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ๋””์ฝ”๋”ฉ :',[index_to_char[i] for i in train_y[0]]) X ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : [49, 37, 34, 0, 45, 47, 44, 39, 34, 32, 49, 0, 36, 50, 49, 34, 43, 31, 34, 47, 36, 0, 34, 31, 44, 44, 40, 0, 44, 35, 0, 30, 41, 38, 32, 34, 48, 0, 30, 33, 51, 34, 43, 49, 50, 47, 34, 48, 0, 38, 43, 0, 52, 44, 43, 33, 34, 47, 41, 30] y ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : [37, 34, 0, 45, 47, 44, 39, 34, 32, 49, 0, 36, 50, 49, 34, 43, 31, 34, 47, 36, 0, 34, 31, 44, 44, 40, 0, 44, 35, 0, 30, 41, 38, 32, 34, 48, 0, 30, 33, 51, 34, 43, 49, 50, 47, 34, 48, 0, 38, 43, 0, 52, 44, 43, 33, 34, 47, 41, 30, 43] -------------------------------------------------- X ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ๋””์ฝ”๋”ฉ : ['t', 'h', 'e', ' ', 'p', 'r', 'o', 'j', 'e', 'c', 't', ' ', 'g', 'u', 't', 'e', 'n', 'b', 'e', 'r', 'g', ' ', 'e', 'b', 'o', 'o', 'k', ' ', 'o', 'f', ' ', 'a', 'l', 'i', 'c', 'e', 's', ' ', 'a', 'd', 'v', 'e', 'n', 't', 'u', 'r', 'e', 's', ' ', 'i', 'n', ' ', 'w', 'o', 'n', 'd', 'e', 'r', 'l', 'a'] y ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ๋””์ฝ”๋”ฉ : ['h', 'e', ' ', 'p', 'r', 'o', 'j', 'e', 'c', 't', ' ', 'g', 'u', 't', 'e', 'n', 'b', 'e', 'r', 'g', ' ', 'e', 'b', 'o', 'o', 'k', ' ', 'o', 'f', ' ', 'a', 'l', 'i', 'c', 'e', 's', ' ', 'a', 'd', 'v', 'e', 'n', 't', 'u', 'r', 'e', 's', ' ', 'i', 'n', ' ', 'w', 'o', 'n', 'd', 'e', 'r', 'l', 'a', 'n'] train_y[0]์€ train_X[0]์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ์‹œํ”„ํŠธ ๋œ ๋ฌธ์žฅ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. train_X์™€ train_y์˜ ๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ. ์ฆ‰, ์ธ๋ฑ์Šค๊ฐ€ 1๋ฒˆ์ธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์„ฑ์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(train_X[1]) [43, 33, 10, 0, 31, 54, 0, 41, 34, 52, 38, 48, 0, 32, 30, 47, 47, 44, 41, 41, 0, 49, 37, 38, 48, 0, 34, 31, 44, 44, 40, 0, 38, 48, 0, 35, 44, 47, 0, 49, 37, 34, 0, 50, 48, 34, 0, 44, 35, 0, 30, 43, 54, 44, 43, 34, 0, 30, 43, 54] print(train_y[1]) [33, 10, 0, 31, 54, 0, 41, 34, 52, 38, 48, 0, 32, 30, 47, 47, 44, 41, 41, 0, 49, 37, 38, 48, 0, 34, 31, 44, 44, 40, 0, 38, 48, 0, 35, 44, 47, 0, 49, 37, 34, 0, 50, 48, 34, 0, 44, 35, 0, 30, 43, 54, 44, 43, 34, 0, 30, 43, 54, 52] ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ train_y[1]์€ train_X[1]์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ์‹œํ”„ํŠธ ๋œ ๋ฌธ์žฅ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ train_X์™€ train_y์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž ๋‹จ์œ„ RNN์—์„œ๋Š” ์ž…๋ ฅ ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ž„๋ฒ ๋”ฉ์ธต(embedding layer)์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ฏ€๋กœ, ์ž…๋ ฅ ์‹œํ€€์Šค์ธ train_X์— ๋Œ€ํ•ด์„œ๋„ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•ฉ๋‹ˆ๋‹ค. train_X = to_categorical(train_X) train_y = to_categorical(train_y) print('train_X์˜ ํฌ๊ธฐ(shape) : {}'.format(train_X.shape)) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ print('train_y์˜ ํฌ๊ธฐ(shape) : {}'.format(train_y.shape)) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ train_X์˜ ํฌ๊ธฐ(shape) : (2658, 60, 56) train_y์˜ ํฌ๊ธฐ(shape) : (2658, 60, 56) train_X์™€ train_y์˜ ํฌ๊ธฐ๋Š” 2,658 ร— 60 ร— 56์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์ƒ˜ํ”Œ์˜ ์ˆ˜(No. of samples)๊ฐ€ 2,658๊ฐœ, ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ธธ์ด(input_length)๊ฐ€ 60, ๊ฐ ๋ฒกํ„ฐ์˜ ์ฐจ์›(input_dim)์ด 55์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์ธ 56์ด์–ด์•ผ ํ•˜๋ฏ€๋กœ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ๋ชจ๋ธ ์„ค๊ณ„ํ•˜๊ธฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 256์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€๋‹ค ๊ตฌ์กฐ์˜ LSTM์„ ์‚ฌ์šฉํ•˜๋ฉฐ, LSTM ์€๋‹‰์ธต์€ ๋‘ ๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ „๊ฒฐํ•ฉ์ธต(Fully Connected Layer)์„ ์ถœ๋ ฅ์ธต์œผ๋กœ ๋ฌธ์ž ์ง‘ํ•ฉ ํฌ๊ธฐ๋งŒํผ์˜ ๋‰ด๋Ÿฐ์„ ๋ฐฐ์น˜ํ•˜์—ฌ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋ชจ๋“  ์‹œ์ ์—์„œ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž ์ค‘ ํ•˜๋‚˜์˜ ๋ฌธ์ž๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์— ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 80 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, LSTM, TimeDistributed hidden_units = 256 model = Sequential() model.add(LSTM(hidden_units, input_shape=(None, train_X.shape[2]), return_sequences=True)) model.add(LSTM(hidden_units, return_sequences=True)) model.add(TimeDistributed(Dense(vocab_size, activation='softmax'))) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(train_X, train_y, epochs=80, verbose=2) ํŠน์ • ๋ฌธ์ž๋ฅผ ์ฃผ๋ฉด ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ๊ณ„์†ํ•ด์„œ ์ƒ์„ฑํ•ด ๋‚ด๋Š” sentence_generation ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ž๋กœ ํ•™์Šตํ•œ ๋ชจ๋ธ. ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋ธ๋กœ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ๋ช‡ ๋ฒˆ ์ƒ์„ฑํ•  ๊ฒƒ์ธ์ง€ ํšŸ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด ์ฃผ๋ฉด, ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ์ž„์˜๋กœ ์‹œ์ž‘ ๋ฌธ์ž๋ฅผ ์ •ํ•œ ๋’ค์— ์ •ํ•ด์ง„ ํšŸ์ˆ˜๋งŒํผ์˜ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์ง€์†์ ์œผ๋กœ ์˜ˆ์ธกํ•˜์—ฌ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•ด๋ƒ…๋‹ˆ๋‹ค. def sentence_generation(model, length): # ๋ฌธ์ž์— ๋Œ€ํ•œ ๋žœ๋ค ํ•œ ์ •์ˆ˜ ์ƒ์„ฑ ix = [np.random.randint(vocab_size)] # ๋žœ๋ค ํ•œ ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋งคํ•‘๋˜๋Š” ๋ฌธ์ž ์ƒ์„ฑ y_char = [index_to_char[ix[-1]]] print(ix[-1],'๋ฒˆ ๋ฌธ์ž',y_char[-1],'๋กœ ์˜ˆ์ธก์„ ์‹œ์ž‘!') # (1, length, 55) ํฌ๊ธฐ์˜ X ์ƒ์„ฑ. ์ฆ‰, LSTM์˜ ์ž…๋ ฅ ์‹œํ€€์Šค ์ƒ์„ฑ X = np.zeros((1, length, vocab_size)) for i in range(length): # X[0][i][์˜ˆ์ธกํ•œ ๋ฌธ์ž์˜ ์ธ๋ฑ์Šค] = 1, ์ฆ‰, ์˜ˆ์ธก ๋ฌธ์ž๋ฅผ ๋‹ค์Œ ์ž…๋ ฅ ์‹œํ€€์Šค์— ์ถ”๊ฐ€ X[0][i][ix[-1]] = 1 print(index_to_char[ix[-1]], end="") ix = np.argmax(model.predict(X[:, :i+1, :])[0], 1) y_char.append(index_to_char[ix[-1]]) return ('').join(y_char) result = sentence_generation(model, 100) print(result) 49 ๋ฒˆ ๋ฌธ์ž u๋กœ ์˜ˆ์ธก์„ ์‹œ์ž‘! ury-men would have done just as well. the twelve jurors were to say in that dide. he went on in a di' ์ž์„ธํžˆ ๋ณด๋ฉด ์‚ฌ์‹ค ๋ง์ด ๋˜์ง€ ์•Š๋Š” ๋ฌธ์žฅ์ด์ง€๋งŒ, ์–ธ๋œป ๋ณด๊ธฐ์— ๊ทธ๋Ÿด๋“ฏํ•ด ๋ณด์ด๋Š” ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•ด๋ƒ…๋‹ˆ๋‹ค. 2. ๋ฌธ์ž ๋‹จ์œ„ RNN(Char RNN)์œผ๋กœ ํ…์ŠคํŠธ ์ƒ์„ฑํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ๋‹ค ๋Œ€ ์ผ ๊ตฌ์กฐ์˜ RNN์„ ๋ฌธ์ž ๋‹จ์œ„๋กœ ํ•™์Šต์‹œํ‚ค๊ณ , ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ import numpy as np from tensorflow.keras.utils import to_categorical ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ œ๊ฐ€ ์ž„์˜๋กœ ๋งŒ๋“  ์—‰ํ„ฐ๋ฆฌ ๋…ธ๋ž˜ ๊ฐ€์‚ฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. raw_text = ''' I get on with life as a programmer, I like to contemplate beer. But when I start to daydream, My mind turns straight to wine. Do I love wine more than beer? I like to use words about beer. But when I stop my talking, My mind turns straight to wine. I hate bugs and errors. But I just think back to wine, And I'm happy once again. I like to hang out with programming and deep learning. But when left alone, My mind turns straight to wine. ''' ์œ„์˜ ํ…์ŠคํŠธ์— ์กด์žฌํ•˜๋Š” ๋‹จ๋ฝ ๊ตฌ๋ถ„์„ ์—†์• ๊ณ  ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ์žฌ์ €์žฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. tokens = raw_text.split() raw_text = ' '.join(tokens) print(raw_text) I get on with life as a programmer, I like to contemplate beer. But when I start to daydream, My mind turns straight to wine. Do I love wine more than beer? I like to use words about beer. But when I stop my talking, My mind turns straight to wine. I hate bugs and errors. But I just think back to wine, And I'm happy once again. I like to hang out with programming and deep learning. But when left alone, My mind turns straight to wine. ๋‹จ๋ฝ ๊ตฌ๋ถ„์ด ์—†์–ด์ง€๊ณ  ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ์žฌ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์—๋Š” ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๋‹จ์–ด๋“ค์˜ ๋ชจ์Œ์ธ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ๋งŒ๋“ค์—ˆ์œผ๋‚˜, ์ด๋ฒˆ์— ๋งŒ๋“ค ์ง‘ํ•ฉ์€ ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์•„๋‹ˆ๋ผ ๋ฌธ์ž ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. # ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๋ฌธ์ž ์ง‘ํ•ฉ ์ƒ์„ฑ char_vocab = sorted(list(set(raw_text))) vocab_size = len(char_vocab) print('๋ฌธ์ž ์ง‘ํ•ฉ :',char_vocab) print ('๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(vocab_size)) ๋ฌธ์ž ์ง‘ํ•ฉ : [' ', "'", ',', '.', '?', 'A', 'B', 'D', 'I', 'M', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'y'] ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 33 ์•ŒํŒŒ๋ฒณ ๋˜๋Š” ๊ตฌ๋‘์  ๋“ฑ์˜ ๋‹จ์œ„์˜ ์ง‘ํ•ฉ์ธ ๋ฌธ์ž ์ง‘ํ•ฉ์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 33์ž…๋‹ˆ๋‹ค. char_to_index = dict((char, index) for index, char in enumerate(char_vocab)) # ๋ฌธ์ž์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ์ธ๋ฑ์Šค ๋ถ€์—ฌ print(char_to_index) {' ': 0, "'": 1, ',': 2, '.': 3, '?': 4, 'A': 5, 'B': 6, 'D': 7, 'I': 8, 'M': 9, 'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15, 'g': 16, 'h': 17, 'i': 18, 'j': 19, 'k': 20, 'l': 21, 'm': 22, 'n': 23, 'o': 24, 'p': 25, 'r': 26, 's': 27, 't': 28, 'u': 29, 'v': 30, 'w': 31, 'y': 32} ์ด๋ฒˆ ์‹ค์Šต์˜ ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ๊ฒฝ์šฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋“ฑ์žฅํ•œ ์•ŒํŒŒ๋ฒณ์˜ ๋Œ€, ์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ณ  ๊ตฌ๋‘์ ๊ณผ ๊ณต๋ฐฑ์„ ํฌํ•จํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•  ๋ฌธ์žฅ ์ƒ˜ํ”Œ๋“ค์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” RNN์„ ์ด์šฉํ•œ ์ƒ์„ฑํ•œ ํ…์ŠคํŠธ ์ฑ•ํ„ฐ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๋‹จ์œ„๊ฐ€ ๋ฌธ์ž ๋‹จ์œ„๋ผ๋Š” ์ ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— student๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ๊ณ , ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ธธ์ด๋ฅผ 5๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. 5๊ฐœ์˜ ์ž…๋ ฅ ๋ฌธ์ž ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋ฌธ์ž ์‹œํ€€์Šค๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, RNN์˜ ์‹œ์ (timesteps)์€ 5๋ฒˆ์ž…๋‹ˆ๋‹ค. stude -> n tuden -> t ์—ฌ๊ธฐ์„œ๋Š” ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ธธ์ด๊ฐ€ 10๊ฐ€ ๋˜๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ธก ๋Œ€์ƒ์ธ ๋ฌธ์ž๋„ ํ•„์š”ํ•˜๋ฏ€๋กœ ๊ธธ์ด๊ฐ€ 11์ด ๋˜๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. length = 11 sequences = [] for i in range(length, len(raw_text)): seq = raw_text[i-length:i] # ๊ธธ์ด 11์˜ ๋ฌธ์ž์—ด์„ ์ง€์†์ ์œผ๋กœ ๋งŒ๋“ ๋‹ค. sequences.append(seq) print('์ด ํ›ˆ๋ จ ์ƒ˜ํ”Œ์˜ ์ˆ˜: %d' % len(sequences)) ์ด ํ›ˆ๋ จ ์ƒ˜ํ”Œ์˜ ์ˆ˜: 426 ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜๋Š” 426๊ฐœ๊ฐ€ ์™„์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ค‘ 10๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. sequences[:10] ['I get on wi', ' get on wit', 'get on with', 'et on with ', 't on with l', ' on with li', 'on with lif', 'n with life', ' with life ', 'with life a'] ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด์—ˆ๋˜ 'I get on with life as a programmer,'๊ฐ€ 10๊ฐœ์˜ ์ƒ˜ํ”Œ๋กœ ๋ถ„๋ฆฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฌธ์žฅ๋“ค์— ๋Œ€ํ•ด์„œ๋„ sequences์— ๋ชจ๋‘ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์›ํ•œ๋‹ค๋ฉด, sequences[30:45] ๋“ฑ๊ณผ ๊ฐ™์ด ์ธ๋ฑ์Šค ๋ฒ”์œ„๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์ถœ๋ ฅํ•ด ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ด์ œ ์•ž์„œ ๋งŒ๋“  char_to_index๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. encoded_sequences = [] for sequence in sequences: # ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ 1๊ฐœ์”ฉ ๊บผ๋‚ธ๋‹ค. encoded_sequence = [char_to_index[char] for char in sequence] # ๋ฌธ์žฅ ์ƒ˜ํ”Œ์—์„œ ๊ฐ ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰. encoded_sequences.append(encoded_sequence) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๊ฐ€ X์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. encoded_sequences[:5] [8, 0, 16, 14, 28, 0, 24, 23, 0, 31, 18] [0, 16, 14, 28, 0, 24, 23, 0, 31, 18, 28] [16, 14, 28, 0, 24, 23, 0, 31, 18, 28, 17] [14, 28, 0, 24, 23, 0, 31, 18, 28, 17, 0] [28, 0, 24, 23, 0, 31, 18, 28, 17, 0, 21] ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ธก ๋Œ€์ƒ์ธ ๋ฌธ์ž๋ฅผ ๋ถ„๋ฆฌ์‹œ์ผœ์ฃผ๋Š” ์ž‘์—…์„ ํ•ด๋ด…์‹œ๋‹ค. ๋ชจ๋“  ์ƒ˜ํ”Œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊ฐ€ ๋ถ„๋ฆฌ๋œ ์ƒ˜ํ”Œ์€ X_data์— ์ €์žฅํ•˜๊ณ , ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋Š” y_data์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. encoded_sequences = np.array(encoded_sequences) # ๋งจ ๋งˆ์ง€๋ง‰ ์œ„์น˜์˜ ๋ฌธ์ž๋ฅผ ๋ถ„๋ฆฌ X_data = encoded_sequences[:,:-1] # ๋งจ ๋งˆ์ง€๋ง‰ ์œ„์น˜์˜ ๋ฌธ์ž๋ฅผ ์ €์žฅ y_data = encoded_sequences[:,-1] ์ •์ƒ์ ์œผ๋กœ ๋ถ„๋ฆฌ๊ฐ€ ๋˜์—ˆ๋Š”์ง€ X์™€ y ๋‘˜ ๋‹ค 5๊ฐœ์”ฉ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(X_data[:5]) print(y_data[:5]) [ 8 0 16 14 28 0 24 23 0 31] [ 0 16 14 28 0 24 23 0 31 18] [16 14 28 0 24 23 0 31 18 28] [14 28 0 24 23 0 31 18 28 17] [28 0 24 23 0 31 18 28 17 0] [18 28 17 0 21] ์•ž์„œ ์ถœ๋ ฅํ•œ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์—์„œ ๊ฐ๊ฐ ๋งจ ๋’ค์˜ ๋ฌธ์ž์˜€๋˜ 18, 28, 17, 0, 21์ด ๋ณ„๋„๋กœ ๋ถ„๋ฆฌ๋˜์–ด y์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ X์™€ y์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ X_data_one_hot = [to_categorical(encoded, num_classes=vocab_size) for encoded in X_data] X_data_one_hot = np.array(X_data_one_hot) y_data_one_hot = to_categorical(y_data, num_classes=vocab_size) ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜ํ–‰ํ•œ ํ›„์˜ X์˜ ํฌ๊ธฐ(shape)๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(X_data_one_hot.shape) (426, 10, 33) ํ˜„์žฌ X์˜ ํฌ๊ธฐ๋Š” 426 ร— 10 ร— 33์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์ƒ˜ํ”Œ์˜ ์ˆ˜(No. of samples)๊ฐ€ 426๊ฐœ, ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ธธ์ด(input_length)๊ฐ€ 10, ๊ฐ ๋ฒกํ„ฐ์˜ ์ฐจ์›(input_dim)์ด 33์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์ธ 33์ด์–ด์•ผ ํ•˜๋ฏ€๋กœ X์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ๋ชจ๋ธ ์„ค๊ณ„ํ•˜๊ธฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 64์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€ ์ผ ๊ตฌ์กฐ์˜ LSTM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ „๊ฒฐํ•ฉ์ธต(Fully Connected Layer)์„ ์ถœ๋ ฅ์ธต์œผ๋กœ ๋ฌธ์ž ์ง‘ํ•ฉ ํฌ๊ธฐ๋งŒํผ์˜ ๋‰ด๋Ÿฐ์„ ๋ฐฐ์น˜ํ•˜์—ฌ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž ์ค‘ ํ•˜๋‚˜์˜ ๋ฌธ์ž๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 100 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, LSTM from tensorflow.keras.preprocessing.sequence import pad_sequences hidden_units = 64 model = Sequential() model.add(LSTM(hidden_units, input_shape=(X_data_one_hot.shape[1], X_data_one_hot.shape[2]))) model.add(Dense(vocab_size, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X_data_one_hot, y_data_one_hot, epochs=100, verbose=2) ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜ sentence_generation์„ ๋งŒ๋“ค์–ด์„œ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•ด ๋ด…์‹œ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๋ฉด, ํ•ด๋‹น ๋ฌธ์ž์—ด๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ๋ฌธ์žฅ์„ ์™„์„ฑํ•ฉ๋‹ˆ๋‹ค. def sentence_generation(model, char_to_index, seq_length, seed_text, n): # ์ดˆ๊ธฐ ์‹œํ€€์Šค init_text = seed_text sentence = '' # ๋‹ค์Œ ๋ฌธ์ž ์˜ˆ์ธก์€ ์ด n ๋ฒˆ๋งŒ ๋ฐ˜๋ณต. for _ in range(n): encoded = [char_to_index[char] for char in seed_text] # ํ˜„์žฌ ์‹œํ€€์Šค์— ๋Œ€ํ•œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ encoded = pad_sequences([encoded], maxlen=seq_length, padding='pre') # ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํŒจ๋”ฉ encoded = to_categorical(encoded, num_classes=len(char_to_index)) # ์ž…๋ ฅํ•œ X(ํ˜„์žฌ ์‹œํ€€์Šค)์— ๋Œ€ํ•ด์„œ y๋ฅผ ์˜ˆ์ธกํ•˜๊ณ  y(์˜ˆ์ธกํ•œ ๋ฌธ์ž)๋ฅผ result์— ์ €์žฅ. result = model.predict(encoded, verbose=0) result = np.argmax(result, axis=1) for char, index in char_to_index.items(): if index == result: break # ํ˜„์žฌ ์‹œํ€€์Šค + ์˜ˆ์ธก ๋ฌธ์ž๋ฅผ ํ˜„์žฌ ์‹œํ€€์Šค๋กœ ๋ณ€๊ฒฝ seed_text = seed_text + char # ์˜ˆ์ธก ๋ฌธ์ž๋ฅผ ๋ฌธ์žฅ์— ์ €์žฅ sentence = sentence + char # n ๋ฒˆ์˜ ๋‹ค์Œ ๋ฌธ์ž ์˜ˆ์ธก์ด ๋๋‚˜๋ฉด ์ตœ์ข… ์™„์„ฑ๋œ ๋ฌธ์žฅ์„ ๋ฆฌํ„ด. sentence = init_text + sentence return sentence print(sentence_generation(model, char_to_index, 10, 'I get on w', 80)) I get on with life as a programmer, I like to hang out with programming and deep learning. ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‘ ๋ฌธ์žฅ์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ๋Š” ์—ฐ์†์ ์œผ๋กœ ๋‚˜์˜จ ์ ์ด ์—†๋Š” ๋‘ ๋ฌธ์žฅ์ž„์—๋„ ๋ชจ๋ธ์ด ์ž„์˜๋กœ ์ƒ์„ฑํ•ด๋ƒˆ์Šต๋‹ˆ๋‹ค. 09. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding) ํ…์ŠคํŠธ๋ฅผ ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•˜๊ณ , ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ…์ŠคํŠธ๋ฅผ ์ ์ ˆํžˆ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ์„œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์„ฑ๋Šฅ์ด ํฌ๊ฒŒ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์–ด๋ฅผ ์ˆ˜์น˜ํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๋งŽ์€ ์—ฐ๊ตฌ๊ฐ€ ์žˆ์—ˆ๊ณ , ํ˜„์žฌ์— ์ด๋ฅด๋Ÿฌ์„œ๋Š” ๊ฐ ๋‹จ์–ด๋ฅผ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ํ•™์Šต์„ ํ†ตํ•ด ๋ฒกํ„ฐํ™”ํ•˜๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 09-01 ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding) ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding)์€ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ํ‘œํ˜„์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํฌ์†Œ ํ‘œํ˜„, ๋ฐ€์ง‘ ํ‘œํ˜„, ๊ทธ๋ฆฌ๊ณ  ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ํฌ์†Œ ํ‘œํ˜„(Sparse Representation) ์•ž์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ†ตํ•ด์„œ ๋‚˜์˜จ ์›-ํ•ซ ๋ฒกํ„ฐ๋“ค์€ ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์˜ ๊ฐ’๋งŒ 1์ด๊ณ , ๋‚˜๋จธ์ง€ ์ธ๋ฑ์Šค์—๋Š” ์ „๋ถ€ 0์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฒกํ„ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฒกํ„ฐ ๋˜๋Š” ํ–‰๋ ฌ(matrix)์˜ ๊ฐ’์ด ๋Œ€๋ถ€๋ถ„์ด 0์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฐฉ๋ฒ•์„ ํฌ์†Œ ํ‘œํ˜„(sparse representation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ํฌ์†Œ ๋ฒกํ„ฐ(sparse vector)์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํฌ์†Œ ๋ฒกํ„ฐ์˜ ๋ฌธ์ œ์ ์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๋ฉด ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ํ•œ์—†์ด ์ปค์ง„๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์— ๋‹จ์–ด๊ฐ€ 10,000๊ฐœ์˜€๋‹ค๋ฉด ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 10,000์ด์–ด์•ผ๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ๊ทธ์ค‘์—์„œ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„๋งŒ 1์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” 0์˜ ๊ฐ’์„ ๊ฐ€์ ธ์•ผ๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์ด ํด์ˆ˜๋ก ๊ณ ์ฐจ์›์˜ ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์–ด๊ฐ€ 10,000๊ฐœ ์žˆ๊ณ  ์ธ๋ฑ์Šค๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉด์„œ ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๋Š” 4์˜€๋‹ค๋ฉด ์› ํ•ซ ๋ฒกํ„ฐ๋Š” ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋˜์–ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. Ex) ๊ฐ•์•„์ง€ = [ 0 0 0 0 1 0 0 0 0 0 0 0 ... ์ค‘๋žต ... 0] # ์ด๋•Œ 1 ๋’ค์˜ 0์˜ ์ˆ˜๋Š” 9995๊ฐœ. ์ด๋Ÿฌํ•œ ๋ฒกํ„ฐ ํ‘œํ˜„์€ ๊ณต๊ฐ„์  ๋‚ญ๋น„๋ฅผ ๋ถˆ๋Ÿฌ์ผ์œผํ‚ต๋‹ˆ๋‹ค. ๊ณต๊ฐ„์  ๋‚ญ๋น„๋ฅผ ์ผ์œผํ‚ค๋Š” ๊ฒƒ์€ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฟ๋งŒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ํฌ์†Œ ํ‘œํ˜„์˜ ์ผ์ข…์ธ DTM๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋„ ํŠน์ • ๋ฌธ์„œ์— ์—ฌ๋Ÿฌ ๋‹จ์–ด๊ฐ€ ๋‹ค์ˆ˜ ๋“ฑ์žฅํ•˜์˜€์œผ๋‚˜, ๋‹ค๋ฅธ ๋งŽ์€ ๋ฌธ์„œ์—์„œ๋Š” ํ•ด๋‹น ํŠน์ • ๋ฌธ์„œ์— ๋“ฑ์žฅํ–ˆ๋˜ ๋‹จ์–ด๋“ค์ด ์ „๋ถ€ ๋“ฑ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์—ญ์‹œ๋‚˜ ํ–‰๋ ฌ์˜ ๋งŽ์€ ๊ฐ’์ด 0์ด ๋˜๋ฉด์„œ ๊ณต๊ฐ„์  ๋‚ญ๋น„๋ฅผ ์ผ์œผํ‚ต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ด€์ ์—์„œ DTM์€ ํฌ์†Œ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ๊ฐ™์€ ํฌ์†Œ ๋ฒกํ„ฐ์˜ ๋ฌธ์ œ์ ์€ ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ํ‘œํ˜„ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. 2. ๋ฐ€์ง‘ ํ‘œํ˜„(Dense Representation) ํฌ์†Œ ํ‘œํ˜„๊ณผ ๋ฐ˜๋Œ€๋˜๋Š” ํ‘œํ˜„์œผ๋กœ ๋ฐ€์ง‘ ํ‘œํ˜„(dense representation)์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ€์ง‘ ํ‘œํ˜„์€ ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋กœ ์ƒ์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ๊ฐ’์œผ๋กœ ๋ชจ๋“  ๋‹จ์–ด์˜ ๋ฒกํ„ฐ ํ‘œํ˜„์˜ ์ฐจ์›์„ ๋งž์ถฅ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ด ๊ณผ์ •์—์„œ ๋” ์ด์ƒ 0๊ณผ 1๋งŒ ๊ฐ€์ง„ ๊ฐ’์ด ์•„๋‹ˆ๋ผ ์‹ค์ˆซ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ํฌ์†Œ ํ‘œํ˜„์˜ ์˜ˆ๋ฅผ ๊ฐ€์ ธ์™€๋ด…์‹œ๋‹ค. Ex) ๊ฐ•์•„์ง€ = [ 0 0 0 0 1 0 0 0 0 0 0 0 ... ์ค‘๋žต ... 0] # ์ด๋•Œ 1 ๋’ค์˜ 0์˜ ์ˆ˜๋Š” 9995๊ฐœ. ์ฐจ์›์€ 10,000 ์˜ˆ๋ฅผ ๋“ค์–ด 10,000๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์„ ๋•Œ ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์™€ ๊ฐ™์€ ํ‘œํ˜„์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ€์ง‘ ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ€์ง‘ ํ‘œํ˜„์˜ ์ฐจ์›์„ 128๋กœ ์„ค์ •ํ•œ๋‹ค๋ฉด, ๋ชจ๋“  ๋‹จ์–ด์˜ ๋ฒกํ„ฐ ํ‘œํ˜„์˜ ์ฐจ์›์€ 128๋กœ ๋ฐ”๋€Œ๋ฉด์„œ ๋ชจ๋“  ๊ฐ’์ด ์‹ค์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. Ex) ๊ฐ•์•„์ง€ = [0.2 1.8 1.1 -2.1 1.1 2.8 ... ์ค‘๋žต ...] # ์ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128 ์ด ๊ฒฝ์šฐ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ์กฐ๋ฐ€ํ•ด์กŒ๋‹ค๊ณ  ํ•˜์—ฌ ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding) ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)์˜ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(word embedding)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋ฅผ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๊ณผ์ •์„ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ผ๊ณ  ํ•˜์—ฌ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(embedding vector)๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์œผ๋กœ๋Š” LSA, Word2Vec, FastText, Glove ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ๋„๊ตฌ์ธ Embedding()๋Š” ์•ž์„œ ์–ธ๊ธ‰ํ•œ ๋ฐฉ๋ฒ•๋“ค์„ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์ง€๋งŒ, ๋‹จ์–ด๋ฅผ ๋žœ๋ค ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค์—, ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ฐ€์ค‘์น˜๋ฅผ ํ•™์Šตํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋Š” ์•ž์„œ ๋ฐฐ์šด ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ์ง€๊ธˆ ๋ฐฐ์šฐ๊ณ  ์žˆ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์ด๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ์ฐจ์› ๊ณ ์ฐจ์›(๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ) ์ €์ฐจ์› ๋‹ค๋ฅธ ํ‘œํ˜„ ํฌ์†Œ ๋ฒกํ„ฐ์˜ ์ผ์ข… ๋ฐ€์ง‘ ๋ฒกํ„ฐ์˜ ์ผ์ข… ํ‘œํ˜„ ๋ฐฉ๋ฒ• ์ˆ˜๋™ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ํ•™์Šตํ•จ ๊ฐ’์˜ ํƒ€์ž… 1๊ณผ 0 ์‹ค์ˆ˜ Embedding()์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ Word2Vec, Glove ๋“ฑ์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ๋น„๊ต๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์‹ค์Šต์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 09-02 ์›Œ๋“œํˆฌ๋ฒกํ„ฐ(Word2Vec) ์•ž์„œ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ„ ์œ ์˜๋ฏธํ•œ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Œ์„ ์–ธ๊ธ‰ํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ„ ์œ ์˜๋ฏธํ•œ ์œ ์‚ฌ๋„๋ฅผ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ์ˆ˜์น˜ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฐฉ๋ฒ•์ด ์›Œ๋“œํˆฌ๋ฒกํ„ฐ(Word2Vec)์ž…๋‹ˆ๋‹ค. Word2Vec์˜ ๊ฐœ๋…์„ ์„ค๋ช…ํ•˜๊ธฐ ์•ž์„œ Word2Vec๊ฐ€ ์–ด๋–ค ์ผ์„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. http://w.elnn.kr/search/ ์œ„ ์‚ฌ์ดํŠธ๋Š” ํ•œ๊ตญ์–ด ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ๋ฒกํ„ฐ ์—ฐ์‚ฐ์„ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค. ์œ„ ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋‹จ์–ด๋“ค(์‹ค์ œ๋กœ๋Š” Word2Vec ๋ฒกํ„ฐ)๋กœ ๋”ํ•˜๊ธฐ, ๋นผ๊ธฐ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์˜ ์‹์—์„œ ์ขŒ๋ณ€์„ ์ง‘์–ด๋„ฃ์œผ๋ฉด, ์šฐ๋ณ€์˜ ๋‹ต๋“ค์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ํ•œ๊ตญ - ์„œ์šธ + ๋„์ฟ„ = ์ผ๋ณธ ๋ฐ•์ฐฌํ˜ธ - ์•ผ๊ตฌ + ์ถ•๊ตฌ = ํ˜ธ๋‚˜์šฐ๋‘ ์‹ ๊ธฐํ•˜๊ฒŒ๋„ ๋‹จ์–ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์˜๋ฏธ๋“ค์„ ๊ฐ€์ง€๊ณ  ์—ฐ์‚ฐ์„ ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” ๊ฐ ๋‹จ์–ด ๋ฒกํ„ฐ๊ฐ€ ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๋ฐ˜์˜ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ผ๊นŒ์š”? 1. ํฌ์†Œ ํ‘œํ˜„(Sparse Representation) ์•ž์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ†ตํ•ด์„œ ์–ป์€ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์˜ ๊ฐ’๋งŒ 1์ด๊ณ , ๋‚˜๋จธ์ง€ ์ธ๋ฑ์Šค์—๋Š” ์ „๋ถ€ 0์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฒกํ„ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋ฒกํ„ฐ ๋˜๋Š” ํ–‰๋ ฌ์˜ ๊ฐ’์ด ๋Œ€๋ถ€๋ถ„์ด 0์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฐฉ๋ฒ•์„ ํฌ์†Œ ํ‘œํ˜„(sparse representation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ๊ฐ ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ„ ์œ ์˜๋ฏธํ•œ ์œ ์‚ฌ์„ฑ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ๊ณ , ๋Œ€์•ˆ์œผ๋กœ ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ๋‹ค์ฐจ์› ๊ณต๊ฐ„์— ๋ฒกํ„ฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ด๋Ÿฌํ•œ ํ‘œํ˜„์„ ๋ถ„์‚ฐ ํ‘œํ˜„(distributed representation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ถ„์‚ฐ ํ‘œํ˜„์„ ์ด์šฉํ•˜์—ฌ ๋‹จ์–ด ๊ฐ„ ์˜๋ฏธ์  ์œ ์‚ฌ์„ฑ์„ ๋ฒกํ„ฐํ™”ํ•˜๋Š” ์ž‘์—…์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(embedding)์ด๋ผ ๋ถ€๋ฅด๋ฉฐ ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋œ ๋ฒกํ„ฐ๋ฅผ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(embedding vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ๋ถ„์‚ฐ ํ‘œํ˜„(Distributed Representation) ๋ถ„์‚ฐ ํ‘œํ˜„(distributed representation) ๋ฐฉ๋ฒ•์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ถ„ํฌ ๊ฐ€์„ค(distributional hypothesis)์ด๋ผ๋Š” ๊ฐ€์ • ํ•˜์— ๋งŒ๋“ค์–ด์ง„ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ€์ •์€ '๋น„์Šทํ•œ ๋ฌธ๋งฅ์—์„œ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋“ค์€ ๋น„์Šทํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค'๋ผ๋Š” ๊ฐ€์ •์ž…๋‹ˆ๋‹ค. ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด๋Š” ๊ท€์—ฝ๋‹ค, ์˜ˆ์˜๋‹ค, ์• ๊ต ๋“ฑ์˜ ๋‹จ์–ด๊ฐ€ ์ฃผ๋กœ ํ•จ๊ป˜ ๋“ฑ์žฅํ•˜๋Š”๋ฐ ๋ถ„ํฌ ๊ฐ€์„ค์— ๋”ฐ๋ผ์„œ ํ•ด๋‹น ๋‚ด์šฉ์„ ๊ฐ€์ง„ ํ…์ŠคํŠธ์˜ ๋‹จ์–ด๋“ค์„ ๋ฒกํ„ฐํ™”ํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์€ ์œ ์‚ฌํ•œ ๋ฒกํ„ฐ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋ถ„์‚ฐ ํ‘œํ˜„์€ ๋ถ„ํฌ ๊ฐ€์„ค์„ ์ด์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ํ•™์Šตํ•˜๊ณ , ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ๋ฒกํ„ฐ์˜ ์—ฌ๋Ÿฌ ์ฐจ์›์— ๋ถ„์‚ฐํ•˜์—ฌ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋œ ๋ฒกํ„ฐ๋“ค์€ ์›-ํ•ซ ๋ฒกํ„ฐ์ฒ˜๋Ÿผ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ์ผ ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ์ƒ๋Œ€์ ์œผ๋กœ ์ €์ฐจ์›์œผ๋กœ ์ค„์–ด๋“ญ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ–๊ณ  ์žˆ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋‹จ์–ด๊ฐ€ 10,000๊ฐœ ์žˆ๊ณ  ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๋Š” 4์˜€๋‹ค๋ฉด ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Ex) ๊ฐ•์•„์ง€ = [ 0 0 0 0 1 0 0 0 0 0 0 0 ... ์ค‘๋žต ... 0] 1์ด๋ž€ ๊ฐ’ ๋’ค์— 9,995๊ฐœ์˜ 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Word2Vec์œผ๋กœ ์ž„๋ฒ ๋”ฉ ๋œ ๋ฒกํ„ฐ๋Š” ๊ตณ์ด ๋ฒกํ„ฐ ์ฐจ์›์ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ๋  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์ฐจ์›์˜ ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๊ฐ€ ๋˜๋ฉฐ ๊ฐ ์ฐจ์›์˜ ๊ฐ’์€ ์‹ค์ˆซ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. Ex) ๊ฐ•์•„์ง€ = [0.2 0.3 0.5 0.7 0.2 ... ์ค‘๋žต ... 0.2] ์š”์•ฝํ•˜๋ฉด ํฌ์†Œ ํ‘œํ˜„์ด ๊ณ ์ฐจ์›์— ๊ฐ ์ฐจ์›์ด ๋ถ„๋ฆฌ๋œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค๋ฉด, ๋ถ„์‚ฐ ํ‘œํ˜„์€ ์ €์ฐจ์›์— ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ์—ฌ๋Ÿฌ ์ฐจ์›์—๋‹ค๊ฐ€ ๋ถ„์‚ฐํ•˜์—ฌ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ„ ์œ ์˜๋ฏธํ•œ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ๋Œ€ํ‘œ์ ์ธ ํ•™์Šต ๋ฐฉ๋ฒ•์ด Word2Vec์ž…๋‹ˆ๋‹ค. 3. CBOW(Continuous Bag of Words) Word2Vec์˜ ํ•™์Šต ๋ฐฉ์‹์—๋Š” CBOW(Continuous Bag of Words)์™€ Skip-Gram ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ์Šต๋‹ˆ๋‹ค. CBOW๋Š” ์ฃผ๋ณ€์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ์ž…๋ ฅ์œผ๋กœ ์ค‘๊ฐ„์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ, Skip-Gram์€ ์ค‘๊ฐ„์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ์ž…๋ ฅ์œผ๋กœ ์ฃผ๋ณ€ ๋‹จ์–ด๋“ค์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ž์ฒด๋Š” ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € CBOW์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ์œ„ํ•ด ๋งค์šฐ ๊ฐ„์†Œํ™”๋œ ์˜ˆ์‹œ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฌธ : "The fat cat sat on the mat" ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์— ์œ„์™€ ๊ฐ™์€ ์˜ˆ๋ฌธ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ['The', 'fat', 'cat', 'on', 'the', 'mat']์œผ๋กœ๋ถ€ํ„ฐ sat์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์€ CBOW๊ฐ€ ํ•˜๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ๋‹จ์–ด sat์„ ์ค‘์‹ฌ ๋‹จ์–ด(center word)๋ผ๊ณ  ํ•˜๊ณ , ์˜ˆ์ธก์— ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์–ด๋“ค์„ ์ฃผ๋ณ€ ๋‹จ์–ด(context word)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ž, ๋’ค๋กœ ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๋ณผ์ง€๋ฅผ ๊ฒฐ์ •ํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด ๋ฒ”์œ„๋ฅผ ์œˆ๋„(window)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ 2์ด๊ณ , ์˜ˆ์ธกํ•˜๊ณ ์ž ํ•˜๋Š” ์ค‘์‹ฌ ๋‹จ์–ด๊ฐ€ sat์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ์•ž์˜ ๋‘ ๋‹จ์–ด์ธ fat์™€ cat, ๊ทธ๋ฆฌ๊ณ  ๋’ค์˜ ๋‘ ๋‹จ์–ด์ธ on, the๋ฅผ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ n์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ์‹ค์ œ ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ์ฐธ๊ณ ํ•˜๋ ค๊ณ  ํ•˜๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋Š” 2n์ž…๋‹ˆ๋‹ค. ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ง€๋ฉด ์œˆ๋„๋ฅผ ์˜†์œผ๋กœ ์›€์ง์—ฌ์„œ ์ฃผ๋ณ€ ๋‹จ์–ด์™€ ์ค‘์‹ฌ ๋‹จ์–ด์˜ ์„ ํƒ์„ ๋ณ€๊ฒฝํ•ด๊ฐ€๋ฉฐ ํ•™์Šต์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“œ๋Š”๋ฐ ์ด ๋ฐฉ๋ฒ•์„ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„(sliding window)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ์ขŒ์ธก์˜ ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ๋ณ€ํ™”๋Š” ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ 2์ผ ๋•Œ, ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„๊ฐ€ ์–ด๋–ค ์‹์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋ฉด์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“œ๋Š”์ง€ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. Word2Vec์—์„œ ์ž…๋ ฅ์€ ๋ชจ๋‘ ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ, ์šฐ์ธก ๊ทธ๋ฆผ์€ ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์–ด๋–ป๊ฒŒ ์„ ํƒํ–ˆ์„ ๋•Œ์— ๋”ฐ๋ผ์„œ ๊ฐ๊ฐ ์–ด๋–ค ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ๋˜๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๊ฒฐ๊ตญ CBOW๋ฅผ ์œ„ํ•œ ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. CBOW์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๊ฐ„๋‹จํžˆ ๋„์‹ํ™”ํ•˜๋ฉด ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ์ธต(Input layer)์˜ ์ž…๋ ฅ์œผ๋กœ์„œ ์•ž, ๋’ค๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•œ ์œˆ๋„ ํฌ๊ธฐ ๋ฒ”์œ„ ์•ˆ์— ์žˆ๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด๋“ค์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๊ณ , ์ถœ๋ ฅ์ธต(Output layer)์—์„œ ์˜ˆ์ธกํ•˜๊ณ ์ž ํ•˜๋Š” ์ค‘๊ฐ„ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ๋ ˆ์ด๋ธ”๋กœ์„œ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์‹ค์€ Word2Vec์€ ์€๋‹‰์ธต์ด ๋‹ค์ˆ˜์ธ ๋”ฅ ๋Ÿฌ๋‹(deep learning) ๋ชจ๋ธ์ด ์•„๋‹ˆ๋ผ ์€๋‹‰์ธต์ด 1๊ฐœ์ธ ์–•์€ ์‹ ๊ฒฝ๋ง(shallow neural network)์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ Word2Vec์˜ ์€๋‹‰์ธต์€ ์ผ๋ฐ˜์ ์ธ ์€๋‹‰์ธต๊ณผ๋Š” ๋‹ฌ๋ฆฌ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉฐ ๋ฃฉ์—… ํ…Œ์ด๋ธ”์ด๋ผ๋Š” ์—ฐ์‚ฐ์„ ๋‹ด๋‹นํ•˜๋Š” ์ธต์œผ๋กœ ํˆฌ์‚ฌ์ธต(projection layer)์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. CBOW์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ข€ ๋” ํ™•๋Œ€ํ•˜์—ฌ, ๋™์ž‘ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ๋Œ€ํ•ด์„œ ์ƒ์„ธํ•˜๊ฒŒ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๊ทธ๋ฆผ์—์„œ ์ฃผ๋ชฉํ•ด์•ผ ํ•  ๊ฒƒ์€ ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ํˆฌ์‚ฌ์ธต์˜ ํฌ๊ธฐ๊ฐ€ M์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. CBOW์—์„œ ํˆฌ์‚ฌ์ธต์˜ ํฌ๊ธฐ M์€ ์ž„๋ฒ ๋”ฉํ•˜๊ณ  ๋‚œ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ํˆฌ์‚ฌ์ธต์˜ ํฌ๊ธฐ๋Š” M=5์ด๋ฏ€๋กœ CBOW๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚˜์„œ ์–ป๋Š” ๊ฐ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 5๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ์ž…๋ ฅ์ธต๊ณผ ํˆฌ์‚ฌ์ธต ์‚ฌ์ด์˜ ๊ฐ€์ค‘์น˜ W๋Š” V ร— M ํ–‰๋ ฌ์ด๋ฉฐ, ํˆฌ์‚ฌ์ธต์—์„œ ์ถœ๋ ฅ์ธต ์‚ฌ์ด์˜ ๊ฐ€์ค‘์น˜ W'๋Š” M ร— V ํ–‰๋ ฌ์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ V๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 7์ด๊ณ , M์€ 5๋ผ๋ฉด ๊ฐ€์ค‘์น˜ W๋Š” 7 ร— 5 ํ–‰๋ ฌ์ด๊ณ , W'๋Š” 5 ร— 7 ํ–‰๋ ฌ์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์ด ๋‘ ํ–‰๋ ฌ์€ ๋™์ผํ•œ ํ–‰๋ ฌ์„ ์ „์น˜(transpose) ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์„œ๋กœ ๋‹ค๋ฅธ ํ–‰๋ ฌ์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ›ˆ๋ จ ์ „์— ์ด ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W์™€ W'๋Š” ๋žœ๋ค ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. CBOW๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด๋กœ ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ๋” ์ •ํ™•ํžˆ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ๊ณ„์†ํ•ด์„œ ์ด W์™€ W'๋ฅผ ํ•™์Šตํ•ด๊ฐ€๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ๊ฐ€์ค‘์น˜ W ํ–‰๋ ฌ์˜ ๊ณฑ์ด ์–ด๋–ป๊ฒŒ ์ด๋ฃจ์–ด์ง€๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ๋Š” ๊ฐ ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋กœ ํ‘œ๊ธฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฒกํ„ฐ๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. i ๋ฒˆ์งธ ์ธ๋ฑ์Šค์— 1์ด๋ผ๋Š” ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๊ทธ ์™ธ์˜ 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ž…๋ ฅ ๋ฒกํ„ฐ์™€ ๊ฐ€์ค‘์น˜ W ํ–‰๋ ฌ์˜ ๊ณฑ์€ ์‚ฌ์‹ค W ํ–‰๋ ฌ์˜ i๋ฒˆ์งธ ํ–‰์„ ๊ทธ๋Œ€๋กœ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ๊ณผ(lookup) ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ๋ฃฉ์—… ํ…Œ์ด๋ธ”(lookup table)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ CBOW์˜ ๋ชฉ์ ์€ W์™€ W'๋ฅผ ์ž˜ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ์ ์ด ์žˆ๋Š”๋ฐ, ๊ทธ ์ด์œ ๊ฐ€ ์—ฌ๊ธฐ์„œ lookup ํ•ด์˜จ W์˜ ๊ฐ ํ–‰๋ฒกํ„ฐ๊ฐ€ Word2Vec ํ•™์Šต ํ›„์—๋Š” ๊ฐ ๋‹จ์–ด์˜ M ์ฐจ์›์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ๊ฐ€์ค‘์น˜ W๊ฐ€ ๊ณฑํ•ด์„œ ์ƒ๊ธด ๊ฒฐ๊ณผ ๋ฒกํ„ฐ๋“ค์€ ํˆฌ์‚ฌ์ธต์—์„œ ๋งŒ๋‚˜ ์ด ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์ธ ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์œˆ๋„ ํฌ๊ธฐ n=2๋ผ๋ฉด, ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ด๊ฐœ์ˆ˜๋Š” 2n์ด๋ฏ€๋กœ ์ค‘๊ฐ„ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด 4๊ฐœ๊ฐ€ ์ž…๋ ฅ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํ‰๊ท ์„ ๊ตฌํ•  ๋•Œ๋Š” 4๊ฐœ์˜ ๊ฒฐ๊ณผ ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ํ‰๊ท ์„ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํˆฌ์‚ฌ์ธต์—์„œ ๋ฒกํ„ฐ์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ๋ถ€๋ถ„์€ CBOW๊ฐ€ Skip-Gram๊ณผ ๋‹ค๋ฅธ ์ฐจ์ด์ ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ณด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ, Skip-Gram์€ ์ž…๋ ฅ์ด ์ค‘์‹ฌ ๋‹จ์–ด ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์— ํˆฌ์‚ฌ์ธต์—์„œ ๋ฒกํ„ฐ์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ•ด์ง„ ํ‰๊ท  ๋ฒกํ„ฐ๋Š” ๋‘ ๋ฒˆ์งธ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W'์™€ ๊ณฑํ•ด์ง‘๋‹ˆ๋‹ค. ๊ณฑ์…ˆ์˜ ๊ฒฐ๊ณผ๋กœ๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋“ค๊ณผ ์ฐจ์›์ด V๋กœ ๋™์ผํ•œ ๋ฒกํ„ฐ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 7์ด์—ˆ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ๋‚˜์˜ค๋Š” ๋ฒกํ„ฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ด ๋ฒกํ„ฐ์— CBOW๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋ฉด์„œ ๋ฒกํ„ฐ์˜ ๊ฐ ์›์†Œ๋“ค์˜ ๊ฐ’์€ 0๊ณผ 1์‚ฌ์ด์˜ ์‹ค์ˆ˜๋กœ, ์ดํ•ฉ์€ 1์ด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์œ„ํ•œ ์ผ์ข…์˜ ์Šค์ฝ”์–ด ๋ฒกํ„ฐ(score vector)์ž…๋‹ˆ๋‹ค. ์Šค์ฝ”์–ด ๋ฒกํ„ฐ์˜ j ๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ€์ง„ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์€ j ๋ฒˆ์งธ ๋‹จ์–ด๊ฐ€ ์ค‘์‹ฌ ๋‹จ์–ด์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์Šค์ฝ”์–ด ๋ฒกํ„ฐ์˜ ๊ฐ’์€ ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” ๋ฒกํ„ฐ์ธ ์ค‘์‹ฌ ๋‹จ์–ด ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๊ฐ’์— ๊ฐ€๊นŒ์›Œ์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์Šค์ฝ”์–ด ๋ฒกํ„ฐ๋ฅผ ^ ๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋กœ ํ–ˆ์„ ๋•Œ, ์ด ๋‘ ๋ฒกํ„ฐ ๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด CBOW๋Š” ์†์‹ค ํ•จ์ˆ˜(loss function)๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ(cross-entropy) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์— ์ค‘์‹ฌ ๋‹จ์–ด์ธ ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ์Šค์ฝ”์–ด ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋„ฃ๊ณ , ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์‹์—์„œ V๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. o t ( ^ y ) โˆ’ j 1 y l g ( j) ์—ญ์ „ํŒŒ(Back Propagation)๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด W์™€ W'๊ฐ€ ํ•™์Šต์ด ๋˜๋Š”๋ฐ, ํ•™์Šต์ด ๋‹ค ๋˜์—ˆ๋‹ค๋ฉด M ์ฐจ์›์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ–๋Š” W์˜ ํ–‰๋ ฌ์˜ ํ–‰์„ ๊ฐ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ W์™€ W' ํ–‰๋ ฌ ๋‘ ๊ฐ€์ง€ ๋ชจ๋‘๋ฅผ ๊ฐ€์ง€๊ณ  ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 4. Skip-gram CBOW์—์„œ๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ํ†ตํ•ด ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ–ˆ๋‹ค๋ฉด, Skip-gram์€ ์ค‘์‹ฌ ๋‹จ์–ด์—์„œ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์˜ˆ๋ฌธ์— ๋Œ€ํ•ด์„œ ๋™์ผํ•˜๊ฒŒ ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ 2์ผ ๋•Œ, ๋ฐ์ดํ„ฐ ์…‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๋„์‹ํ™”ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋ฏ€๋กœ ํˆฌ์‚ฌ์ธต์—์„œ ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์€ ์—†์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋…ผ๋ฌธ์—์„œ ์„ฑ๋Šฅ ๋น„๊ต๋ฅผ ์ง„ํ–‰ํ–ˆ์„ ๋•Œ ์ „๋ฐ˜์ ์œผ๋กœ Skip-gram์ด CBOW๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. 5. NNLM Vs. Word2Vec ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์˜ ๊ฐœ๋… ์ž์ฒด๋Š” ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ(NNLM)์„ ์„ค๋ช…ํ•˜๋ฉฐ ์ด๋ฏธ ํ•™์Šตํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. NNLM์€ ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์˜ ๊ฐœ๋…์„ ๋„์ž…ํ•˜์˜€๊ณ , ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ž์ฒด์— ์ง‘์ค‘ํ•˜์—ฌ NNLM์˜ ๋Š๋ฆฐ ํ•™์Šต ์†๋„์™€ ์ •ํ™•๋„๋ฅผ ๊ฐœ์„ ํ•˜์—ฌ ํƒ„์ƒํ•œ ๊ฒƒ์ด Word2Vec์ž…๋‹ˆ๋‹ค. NNLM๊ณผ Word2Vec์˜ ์ฐจ์ด๋ฅผ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ์˜ˆ์ธกํ•˜๋Š” ๋Œ€์ƒ์ด ๋‹ฌ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. NNLM์€ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ์ด ๋ชฉ์ ์ด๋ฏ€๋กœ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜์ง€๋งŒ, Word2Vec(CBOW)์€ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ž์ฒด๊ฐ€ ๋ชฉ์ ์ด๋ฏ€๋กœ ๋‹ค์Œ ๋‹จ์–ด๊ฐ€ ์•„๋‹Œ ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ฒŒ ํ•˜์—ฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋ฏ€๋กœ NNLM์ด ์˜ˆ์ธก ๋‹จ์–ด์˜ ์ด์ „ ๋‹จ์–ด๋“ค๋งŒ์„ ์ฐธ๊ณ ํ•˜์˜€๋˜ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ, Word2Vec์€ ์˜ˆ์ธก ๋‹จ์–ด์˜ ์ „, ํ›„ ๋‹จ์–ด๋“ค์„ ๋ชจ๋‘ ์ฐธ๊ณ ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ์กฐ๋„ ๋‹ฌ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ n์„ ํ•™์Šต์— ์‚ฌ์šฉํ•˜๋Š” ๋‹จ์–ด์˜ ์ˆ˜, m์„ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›, h๋ฅผ ์€๋‹‰์ธต์˜ ํฌ๊ธฐ, V๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ NNLM๊ณผ Word2Vec์˜ ์ฐจ์ด๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. Word2Vec์€ ์šฐ์„  NNLM์— ์กด์žฌํ•˜๋˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š” ์€๋‹‰์ธต์„ ์ œ๊ฑฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ํˆฌ์‚ฌ์ธต ๋‹ค์Œ์— ๋ฐ”๋กœ ์ถœ๋ ฅ์ธต์œผ๋กœ ์—ฐ๊ฒฐ๋˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. Word2Vec์ด NNLM๋ณด๋‹ค ํ•™์Šต ์†๋„์—์„œ ๊ฐ•์ ์„ ๊ฐ€์ง€๋Š” ์ด์œ ๋Š” ์€๋‹‰์ธต์„ ์ œ๊ฑฐํ•œ ๊ฒƒ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ฒ•๋“ค ๋•๋ถ„์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ๊ธฐ๋ฒ•์œผ๋กœ ๊ณ„์ธต์  ์†Œํ”„ํŠธ๋งฅ์Šค(hierarchical softmax)์™€ ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง(negative sampling)์ด ์žˆ๋Š”๋ฐ ์ด ์ฑ…์—์„œ๋Š” ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” '๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง์„ ์ด์šฉํ•œ Word2Vec ๊ตฌํ˜„' ์‹ค์Šต์„ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. Word2Vec๊ณผ NNLM์˜ ์—ฐ์‚ฐ๋Ÿ‰์„ ๋น„๊ตํ•˜์—ฌ ํ•™์Šต ์†๋„๊ฐ€ ์™œ ์ฐจ์ด ๋‚˜๋Š”์ง€ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ์ž…๋ ฅ์ธต์—์„œ ํˆฌ์‚ฌ์ธต, ํˆฌ์‚ฌ์ธต์—์„œ ์€๋‹‰์ธต, ์€๋‹‰์ธต์—์„œ ์ถœ๋ ฅ์ธต์œผ๋กœ ํ–ฅํ•˜๋ฉฐ ๋ฐœ์ƒํ•˜๋Š” NNLM์˜ ์—ฐ์‚ฐ๋Ÿ‰์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. NNLM : ( ร— ) ( ร— ร— ) ( ร— ) ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋ฒ•๋“ค๊นŒ์ง€ ์‚ฌ์šฉํ•˜์˜€์„ ๋•Œ Word2Vec์€ ์ถœ๋ ฅ์ธต์—์„œ์˜ ์—ฐ์‚ฐ์—์„œ ๋ฅผ o ( ) ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด์— ๋”ฐ๋ผ Word2Vec์˜ ์—ฐ์‚ฐ๋Ÿ‰์€ ์•„๋ž˜์™€ ๊ฐ™์œผ๋ฉฐ ์ด๋Š” NNLM๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฅธ ํ•™์Šต ์†๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. Word2Vec : ( ร— ) ( ร— o ( ) ) 09-03 ์˜์–ด/ํ•œ๊ตญ์–ด Word2Vec ์‹ค์Šต gensim ํŒจํ‚ค์ง€์—์„œ ์ œ๊ณตํ•˜๋Š” ์ด๋ฏธ ๊ตฌํ˜„๋œ Word2Vec์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜์–ด์™€ ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ์˜์–ด Word2Vec ๋งŒ๋“ค๊ธฐ ํŒŒ์ด์ฌ์˜ gensim ํŒจํ‚ค์ง€์—๋Š” Word2Vec์„ ์ง€์›ํ•˜๊ณ  ์žˆ์–ด, gensim ํŒจํ‚ค์ง€๋ฅผ ์ด์šฉํ•˜๋ฉด ์†์‰ฝ๊ฒŒ ๋‹จ์–ด๋ฅผ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด๋กœ ๋œ ์ฝ”ํผ์Šค๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , ์ „์ฒ˜๋ฆฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ Word2Vec ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. import re import urllib.request import zipfile from lxml import etree from nltk.tokenize import word_tokenize, sent_tokenize 1) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์ดํ•ดํ•˜๊ธฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. # ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/09.%20Word%20Embedding/dataset/ted_en-20160408.xml", filename="ted_en-20160408.xml") ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์€ xml ๋ฌธ๋ฒ•์œผ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ์–ด ์ž์—ฐ์–ด๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ „์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์–ป๊ณ ์ž ํ•˜๋Š” ์‹ค์งˆ์  ๋ฐ์ดํ„ฐ๋Š” ์˜์–ด ๋ฌธ์žฅ์œผ๋กœ ๋งŒ ๊ตฌ์„ฑ๋œ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋Š” <content>์™€ </content> ์‚ฌ์ด์˜ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ ์ž‘์—…์„ ํ†ตํ•ด xml ๋ฌธ๋ฒ•๋“ค์€ ์ œ๊ฑฐํ•˜๊ณ , ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, <content>์™€ </content> ์‚ฌ์ด์˜ ๋‚ด์šฉ ์ค‘์—๋Š” (Laughter)๋‚˜ (Applause)์™€ ๊ฐ™์€ ๋ฐฐ๊ฒฝ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹จ์–ด๋„ ๋“ฑ์žฅํ•˜๋Š”๋ฐ ์ด ๋˜ํ•œ ์ œ๊ฑฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. <file id="1"> <head> <url>http://www.ted.com/talks/knut_haanaes_two_reasons_companies_fail_and_how_to_avoid_them</url> <pagesize>72832</pagesize> ... xml ๋ฌธ๋ฒ• ์ค‘๋žต ... <content> Here are two reasons companies fail: they only do more of the same, or they only do what's new. To me the real, real solution to quality growth is figuring out the balance between two activities: ... content ๋‚ด์šฉ ์ค‘๋žต ... To me, the irony about the Facit story is hearing about the Facit engineers, who had bought cheap, small electronic calculators in Japan that they used to double-check their calculators. (Laughter) ... content ๋‚ด์šฉ ์ค‘๋žต ... (Applause) </content> </file> <file id="2"> <head> <url>http://www.ted.com/talks/lisa_nip_how_humans_could_evolve_to_survive_in_space<url> ... ์ดํ•˜ ์ค‘๋žต ... 2) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ ์ „์ฒ˜๋ฆฌ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. targetXML = open('ted_en-20160408.xml', 'r', encoding='UTF8') target_text = etree.parse(targetXML) # xml ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ <content>์™€ </content> ์‚ฌ์ด์˜ ๋‚ด์šฉ๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค. parse_text = '\n'.join(target_text.xpath('//content/text()')) # ์ •๊ทœ ํ‘œํ˜„์‹์˜ sub ๋ชจ๋“ˆ์„ ํ†ตํ•ด content ์ค‘๊ฐ„์— ๋“ฑ์žฅํ•˜๋Š” (Audio), (Laughter) ๋“ฑ์˜ ๋ฐฐ๊ฒฝ์Œ ๋ถ€๋ถ„์„ ์ œ๊ฑฐ. # ํ•ด๋‹น ์ฝ”๋“œ๋Š” ๊ด„ํ˜ธ๋กœ ๊ตฌ์„ฑ๋œ ๋‚ด์šฉ์„ ์ œ๊ฑฐ. content_text = re.sub(r'\([^)]*\)', '', parse_text) # ์ž…๋ ฅ ์ฝ”ํผ์Šค์— ๋Œ€ํ•ด์„œ NLTK๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์žฅ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰. sent_text = sent_tokenize(content_text) # ๊ฐ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๊ตฌ๋‘์ ์„ ์ œ๊ฑฐํ•˜๊ณ , ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜. normalized_text = [] for string in sent_text: tokens = re.sub(r"[^a-z0-9]+", " ", string.lower()) normalized_text.append(tokens) # ๊ฐ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ NLTK๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰. result = [word_tokenize(sentence) for sentence in normalized_text] print('์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : {}'.format(len(result))) ์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 273424 ์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋Š” ์•ฝ 27๋งŒ 3์ฒœ ๊ฐœ์ž…๋‹ˆ๋‹ค. # ์ƒ˜ํ”Œ 3๊ฐœ๋งŒ ์ถœ๋ ฅ for line in result[:3]: print(line) ['here', 'are', 'two', 'reasons', 'companies', 'fail', 'they', 'only', 'do', 'more', 'of', 'the', 'same', 'or', 'they', 'only', 'do', 'what', 's', 'new'] ['to', 'me', 'the', 'real', 'real', 'solution', 'to', 'quality', 'growth', 'is', 'figuring', 'out', 'the', 'balance', 'between', 'two', 'activities', 'exploration', 'and', 'exploitation'] ['both', 'are', 'necessary', 'but', 'it', 'can', 'be', 'too', 'much', 'of', 'a', 'good', 'thing'] ์ƒ์œ„ 3๊ฐœ ๋ฌธ์žฅ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด์•˜๋Š”๋ฐ ํ† ํฐ ํ™”๊ฐ€ ์ˆ˜ํ–‰๋˜์—ˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Word2Vec ๋ชจ๋ธ์— ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ์‹œํ‚ต๋‹ˆ๋‹ค. 3) Word2Vec ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ from gensim.models import Word2Vec from gensim.models import KeyedVectors model = Word2Vec(sentences=result, vector_size=100, window=5, min_count=5, workers=4, sg=0) Word2Vec์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. vector_size = ์›Œ๋“œ ๋ฒกํ„ฐ์˜ ํŠน์ง• ๊ฐ’. ์ฆ‰, ์ž„๋ฒ ๋”ฉ ๋œ ๋ฒกํ„ฐ์˜ ์ฐจ์›. window = ์ปจํ…์ŠคํŠธ ์œˆ๋„ ํฌ๊ธฐ min_count = ๋‹จ์–ด ์ตœ์†Œ ๋นˆ๋„ ์ˆ˜ ์ œํ•œ (๋นˆ๋„๊ฐ€ ์ ์€ ๋‹จ์–ด๋“ค์€ ํ•™์Šตํ•˜์ง€ ์•Š๋Š”๋‹ค.) workers = ํ•™์Šต์„ ์œ„ํ•œ ํ”„๋กœ์„ธ์Šค ์ˆ˜ sg = 0์€ CBOW, 1์€ Skip-gram. Word2Vec์— ๋Œ€ํ•ด์„œ ํ•™์Šต์„ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Word2Vec๋Š” ์ž…๋ ฅํ•œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์„ ์ถœ๋ ฅํ•˜๋Š” model.wv.most_similar์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. man๊ณผ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์€ ์–ด๋–ค ๋‹จ์–ด๋“ค์ผ๊นŒ์š”? model_result = model.wv.most_similar("man") print(model_result) [('woman', 0.842622697353363), ('guy', 0.8178728818893433), ('boy', 0.7774451375007629), ('lady', 0.7767927646636963), ('girl', 0.7583760023117065), ('gentleman', 0.7437191009521484), ('soldier', 0.7413754463195801), ('poet', 0.7060446739196777), ('kid', 0.6925194263458252), ('friend', 0.6572611331939697)] man๊ณผ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋กœ woman, guy, boy, lady, girl, gentleman, soldier, kid ๋“ฑ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Word2Vec๋ฅผ ํ†ตํ•ด ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 4) Word2Vec ๋ชจ๋ธ ์ €์žฅํ•˜๊ณ  ๋กœ๋“œํ•˜๊ธฐ ๊ณต๋“ค์—ฌ ํ•™์Šตํ•œ ๋ชจ๋ธ์„ ์–ธ์ œ๋“  ๋‚˜์ค‘์— ๋‹ค์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ปดํ“จํ„ฐ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ณ  ๋‹ค์‹œ ๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์„ ๊ฐ€์ง€๊ณ  ํ–ฅํ›„ ์‹œ๊ฐํ™”๋ฅผ ์ง„ํ–‰ํ•  ์˜ˆ์ •์ด๋ฏ€๋กœ ๊ผญ ์ €์žฅํ•ด ์ฃผ์„ธ์š”. model.wv.save_word2vec_format('eng_w2v') # ๋ชจ๋ธ ์ €์žฅ loaded_model = KeyedVectors.load_word2vec_format("eng_w2v") # ๋ชจ๋ธ ๋กœ๋“œ ๋กœ๋“œํ•œ ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ man๊ณผ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model_result = loaded_model.most_similar("man") print(model_result) [('woman', 0.842622697353363), ('guy', 0.8178728818893433), ('boy', 0.7774451375007629), ('lady', 0.7767927646636963), ('girl', 0.7583760023117065), ('gentleman', 0.7437191009521484), ('soldier', 0.7413754463195801), ('poet', 0.7060446739196777), ('kid', 0.6925194263458252), ('friend', 0.6572611331939697)] 2. ํ•œ๊ตญ์–ด Word2Vec ๋งŒ๋“ค๊ธฐ(๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ) ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋กœ ํ•œ๊ตญ์–ด Word2Vec์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. import pandas as pd import matplotlib.pyplot as plt import urllib.request from gensim.models.word2vec import Word2Vec from konlpy.tag import Okt ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings.txt", filename="ratings.txt") ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•˜๊ณ  ์ƒ์œ„ 5๊ฐœ์˜ ํ–‰์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. train_data = pd.read_table('ratings.txt') train_data[:5] # ์ƒ์œ„ 5๊ฐœ ์ถœ๋ ฅ ์ด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(len(train_data)) # ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ 200000 ์ด 20๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜๋Š”๋ฐ, ๊ฒฐ์ธก๊ฐ’ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # NULL ๊ฐ’ ์กด์žฌ ์œ ๋ฌด print(train_data.isnull().values.any()) True ๊ฒฐ์ธก๊ฐ’์ด ์กด์žฌํ•˜๋ฏ€๋กœ ๊ฒฐ์ธก๊ฐ’์ด ์กด์žฌํ•˜๋Š” ํ–‰์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. train_data = train_data.dropna(how = 'any') # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ํ–‰ ์ œ๊ฑฐ print(train_data.isnull().values.any()) # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ False ๊ฒฐ์ธก๊ฐ’์ด ์‚ญ์ œ๋œ ํ›„์˜ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print(len(train_data)) # ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ 199992 ์ด 199,992๊ฐœ์˜ ๋ฆฌ๋ทฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ†ตํ•ด ํ•œ๊ธ€์ด ์•„๋‹Œ ๊ฒฝ์šฐ ์ œ๊ฑฐํ•˜๋Š” ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. # ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ†ตํ•œ ํ•œ๊ธ€ ์™ธ ๋ฌธ์ž ์ œ๊ฑฐ train_data['document'] = train_data['document'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") train_data[:5] # ์ƒ์œ„ 5๊ฐœ ์ถœ๋ ฅ ํ•™์Šต ์‹œ์— ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ๋‹จ์–ด๋“ค์ธ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Okt๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์ผ์ข…์˜ ๋‹จ์–ด ๋‚ด์ง€๋Š” ํ˜•ํƒœ์†Œ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š” ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์†Œ ์‹œ๊ฐ„์ด ์†Œ์š”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๋ถˆ์šฉ์–ด ์ •์˜ stopwords = ['์˜','๊ฐ€','์ด','์€','๋“ค','๋Š”','์ข€','์ž˜','๊ทธ๋ƒฅ','๊ณผ','๋„','๋ฅผ','์œผ๋กœ','์ž','์—','์™€','ํ•œ','ํ•˜๋‹ค'] # ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ OKT๋ฅผ ์‚ฌ์šฉํ•œ ํ† ํฐํ™” ์ž‘์—… (๋‹ค์†Œ ์‹œ๊ฐ„ ์†Œ์š”) okt = Okt() tokenized_data = [] for sentence in tqdm(train_data['document']): tokenized_sentence = okt.morphs(sentence, stem=True) # ํ† ํฐํ™” stopwords_removed_sentence = [word for word in tokenized_sentence if not word in stopwords] # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ tokenized_data.append(stopwords_removed_sentence) ํ† ํฐ ํ™”๊ฐ€ ๋œ ์ƒํƒœ์—์„œ๋Š” ๊ฐ ๋ฆฌ๋ทฐ์˜ ๊ธธ์ด ๋ถ„ํฌ ๋˜ํ•œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. # ๋ฆฌ๋ทฐ ๊ธธ์ด ๋ถ„ํฌ ํ™•์ธ print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max(len(review) for review in tokenized_data)) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :',sum(map(len, tokenized_data))/len(tokenized_data)) plt.hist([len(review) for review in tokenized_data], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 72 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 10.716703668146726 Word2Vec์œผ๋กœ ํ† ํฐํ™”๋œ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. from gensim.models import Word2Vec model = Word2Vec(sentences = tokenized_data, vector_size = 100, window = 5, min_count = 5, workers = 4, sg = 0) ํ•™์Šต์ด ๋‹ค ๋˜์—ˆ๋‹ค๋ฉด Word2Vec ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # ์™„์„ฑ๋œ ์ž„๋ฒ ๋”ฉ ๋งคํŠธ๋ฆญ์Šค์˜ ํฌ๊ธฐ ํ™•์ธ model.wv.vectors.shape (16477, 100) ์ด 16,477๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜๋ฉฐ ๊ฐ ๋‹จ์–ด๋Š” 100์ฐจ์›์œผ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. '์ตœ๋ฏผ์‹'๊ณผ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์„ ๋ฝ‘์•„๋ด…์‹œ๋‹ค. print(model.wv.most_similar("์ตœ๋ฏผ์‹")) [('ํ•œ์„๊ทœ', 0.8789200782775879), ('์•ˆ์„ฑ๊ธฐ', 0.8757420778274536), ('๊น€์ˆ˜ํ˜„', 0.855679452419281), ('์ด๋ฏผํ˜ธ', 0.854516863822937), ('๊น€๋ช…๋ฏผ', 0.8525030612945557), ('์ตœ๋ฏผ์ˆ˜', 0.8492398262023926), ('์ด์„ฑ์žฌ', 0.8478372097015381), ('์œค์ œ๋ฌธ', 0.8470626473426819), ('๊น€์ฐฝ์™„', 0.8456774950027466), ('์ด์ฃผ์Šน', 0.8442063927650452)] 'ํžˆ์–ด๋กœ'์™€ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์„ ๋ฝ‘์•„๋ด…์‹œ๋‹ค. print(model.wv.most_similar("ํžˆ์–ด๋กœ")) [('์Šฌ๋ž˜์…”', 0.8747539520263672), ('๋ˆ„์•„๋ฅด', 0.8666149377822876), ('๋ฌดํ˜‘', 0.8423701524734497), ('ํ˜ธ๋Ÿฌ', 0.8372749090194702), ('๋ฌผ์˜', 0.8365858793258667), ('๋ฌด๋น„', 0.8260530233383179), ('๋ฌผ', 0.8197994232177734), ('ํ™์ฝฉ', 0.8120777606964111), ('๋ธ”๋ก๋ฒ„์Šคํ„ฐ', 0.8021541833877563), ('๋ธ”๋ž™', 0.7880141139030457)] 3. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2Vec ์ž„๋ฒ ๋”ฉ(Pre-trained Word2Vec embedding) ์†Œ๊ฐœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์„ ํ•  ๋•Œ, ์ผ€๋ผ์Šค์˜ Embedding()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ–๊ณ  ์žˆ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ๋„ ํ•˜์ง€๋งŒ, ์œ„ํ‚คํ”ผ๋””์•„ ๋“ฑ์˜ ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์ „์— ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(pre-trained word embedding vector)๋ฅผ ๊ฐ€์ง€๊ณ  ์™€์„œ ํ•ด๋‹น ๋ฒกํ„ฐ๋“ค์˜ ๊ฐ’์„ ์›ํ•˜๋Š” ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ ์ž‘์—…์„ ํ•˜๋Š”๋ฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋ถ€์กฑํ•œ ์ƒํ™ฉ์ด๋ผ๋ฉด, ๋‹ค๋ฅธ ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ Word2Vec์ด๋‚˜ GloVe ๋“ฑ์œผ๋กœ ์‚ฌ์ „์— ํ•™์Šต์‹œ์ผœ๋†“์€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋•Œ๋กœ๋Š” ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ๊ฐ€์ ธ์™€์„œ ๊ฐ„๋‹จํžˆ ๋‹จ์–ด๋“ค์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ด๋ณด๋Š” ์‹ค์Šต์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋ชจ๋ธ์— ์ ์šฉํ•ด ๋ณด๋Š” ์‹ค์Šต์€ ํ–ฅํ›„์— ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๊ธ€์ด ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ(๋ฏธ๋ฆฌ ํ•™์Šต๋ผ ์žˆ๋Š”) Word2Vec ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ตฌ๊ธ€์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ 3๋ฐฑ๋งŒ ๊ฐœ์˜ Word2Vec ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 300์ž…๋‹ˆ๋‹ค. gensim์„ ํ†ตํ•ด์„œ ์ด ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฑด ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ๊ธฐ์žฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ : https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM/edit ์••์ถ• ํŒŒ์ผ์˜ ์šฉ๋Ÿ‰์€ ์•ฝ 1.5GB์ด์ง€๋งŒ, ํŒŒ์ผ์˜ ์••์ถ•์„ ํ’€๋ฉด ์•ฝ 3.3GB์˜ ํŒŒ์ผ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. import gensim import urllib.request # ๊ตฌ๊ธ€์˜ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2Vec ๋ชจ๋ธ์„ ๋กœ๋“œ. urllib.request.urlretrieve("https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz", \ filename="GoogleNews-vectors-negative300.bin.gz") word2vec_model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin.gz', binary=True) ๋ชจ๋ธ์˜ ํฌ๊ธฐ(shape)๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(word2vec_model.vectors.shape) (3000000, 300) ๋ชจ๋ธ์˜ ํฌ๊ธฐ๋Š” 3,000,000 x 300์ž…๋‹ˆ๋‹ค. ์ฆ‰, 3๋ฐฑ๋งŒ ๊ฐœ์˜ ๋‹จ์–ด์™€ ๊ฐ ๋‹จ์–ด์˜ ์ฐจ์›์€ 300์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ์˜ ํฌ๊ธฐ๊ฐ€ 3๊ธฐ๊ฐ€๊ฐ€ ๋„˜๋Š” ์ด์œ ๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 3 million words * 300 features * 4bytes/feature = ~3.35GB ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. print(word2vec_model.similarity('this', 'is')) print(word2vec_model.similarity('post', 'book')) 0.407970363878 0.0572043891977 ๋‹จ์–ด 'book'์˜ ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(word2vec_model['book']) [ 0.11279297 -0.02612305 -0.04492188 0.06982422 0.140625 0.03039551 -0.04370117 0.24511719 0.08740234 -0.05053711 0.23144531 -0.07470703 ... 300๊ฐœ์˜ ๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š” ๊ด€๊ณ„๋กœ ์ค‘๋žต ... 0.03637695 -0.16796875 -0.01483154 0.09667969 -0.05761719 -0.00515747] ์ฐธ๊ณ  : Word2vec ๋ชจ๋ธ์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์ด์ง€๋งŒ ์ตœ๊ทผ์— ๋“ค์–ด์„œ๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ๋„˜์–ด์„œ ์ถ”์ฒœ ์‹œ์Šคํ…œ์—๋„ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ ๋‹นํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜์—ดํ•ด ์ฃผ๋ฉด Word2vec์€ ์œ„์น˜๊ฐ€ ๊ทผ์ ‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์‚ฌ๋„๊ฐ€ ๋†’์€ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค๋Š” ์ ์—์„œ ์ฐฉ์•ˆ๋œ ์•„์ด๋””์–ด์ž…๋‹ˆ๋‹ค. ๊ด€์‹ฌ ์žˆ๋Š” ๋ถ„๋“ค์€ ๊ตฌ๊ธ€์— 'item2vec'์„ ๊ฒ€์ƒ‰ํ•ด ๋ณด์„ธ์š”. 09-04 ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง์„ ์ด์šฉํ•œ Word2Vec ๊ตฌํ˜„(Skip-Gram with Negative Sampling, SGNS) ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง(Negative Sampling)์„ ์‚ฌ์šฉํ•˜๋Š” Word2Vec์„ ์ง์ ‘ ์ผ€๋ผ์Šค(Keras)๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 1. ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง(Negative Sampling) Word2Vec์˜ ์ถœ๋ ฅ์ธต์—์„œ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ๋‹จ์–ด ์ง‘ํ•ฉ ํฌ๊ธฐ์˜ ๋ฒกํ„ฐ์™€ ์‹ค์ œ ๊ฐ’์ธ ์›-ํ•ซ ๋ฒกํ„ฐ์™€์˜ ์˜ค์ฐจ๋ฅผ ๊ตฌํ•˜๊ณ  ์ด๋กœ๋ถ€ํ„ฐ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ์ˆ˜๋งŒ ์ด์ƒ์— ๋‹ฌํ•œ๋‹ค๋ฉด ์ด ์ž‘์—…์€ ๊ต‰์žฅํžˆ ๋ฌด๊ฑฐ์šด ์ž‘์—…์ด๋ฏ€๋กœ, Word2Vec์€ ๊ฝค๋‚˜ ํ•™์Šตํ•˜๊ธฐ์— ๋ฌด๊ฑฐ์šด ๋ชจ๋ธ์ด ๋ฉ๋‹ˆ๋‹ค. Word2Vec์€ ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ๋ชจ๋“  ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์˜ ์—…๋ฐ์ดํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€๋งŒ, ๋งŒ์•ฝ ํ˜„์žฌ ์ง‘์ค‘ํ•˜๊ณ  ์žˆ๋Š” ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด๊ฐ€ '๊ฐ•์•„์ง€'์™€ '๊ณ ์–‘์ด', '๊ท€์—ฌ์šด'๊ณผ ๊ฐ™์€ ๋‹จ์–ด๋ผ๋ฉด, ์‚ฌ์‹ค ์ด ๋‹จ์–ด๋“ค๊ณผ ๋ณ„ ์—ฐ๊ด€ ๊ด€๊ณ„๊ฐ€ ์—†๋Š” '๋ˆ๊ฐ€์Šค'๋‚˜ '์ปดํ“จํ„ฐ'์™€ ๊ฐ™์€ ์ˆ˜๋งŽ์€ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’๊นŒ์ง€ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง์€ Word2Vec์ด ํ•™์Šต ๊ณผ์ •์—์„œ ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์•„๋‹ˆ๋ผ ์ผ๋ถ€ ๋‹จ์–ด ์ง‘ํ•ฉ์—๋งŒ ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น, ํ˜„์žฌ ์ง‘์ค‘ํ•˜๊ณ  ์žˆ๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด๊ฐ€ '๊ณ ์–‘์ด', '๊ท€์—ฌ์šด'์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์— '๋ˆ๊ฐ€์Šค', '์ปดํ“จํ„ฐ', 'ํšŒ์˜์‹ค'๊ณผ ๊ฐ™์€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋ฌด์ž‘์œ„๋กœ ์„ ํƒ๋œ ์ฃผ๋ณ€ ๋‹จ์–ด๊ฐ€ ์•„๋‹Œ ๋‹จ์–ด๋“ค์„ ์ผ๋ถ€ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋‚˜์˜ ์ค‘์‹ฌ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ๋ณด๋‹ค ํ›จ์”ฌ ์ž‘์€ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด๋†“๊ณ  ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋ฅผ ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋ณ€ ๋‹จ์–ด๋“ค์„ ๊ธ์ •(positive), ๋žœ๋ค์œผ๋กœ ์ƒ˜ํ”Œ๋ง ๋œ ๋‹จ์–ด๋“ค์„ ๋ถ€์ •(negative)์œผ๋กœ ๋ ˆ์ด๋ธ”๋งํ•œ๋‹ค๋ฉด ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์…‹์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ธฐ์กด์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒํผ์˜ ์„ ํƒ์ง€๋ฅผ ๋‘๊ณ  ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋˜ Word2Vec๋ณด๋‹ค ํ›จ์”ฌ ์—ฐ์‚ฐ๋Ÿ‰์—์„œ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. 2. ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง Skip-Gram(Skip-Gram with Negative Sampling, SGNS) ์•ž์„œ ๋ฐฐ์šด Skip-gram์„ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. Skip-gram์€ ์ค‘์‹ฌ ๋‹จ์–ด๋กœ๋ถ€ํ„ฐ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด, Skip-gram์€ ์ค‘์‹ฌ ๋‹จ์–ด cat์œผ๋กœ๋ถ€ํ„ฐ ์ฃผ๋ณ€ ๋‹จ์–ด The, fat, sat, on์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ Skip-gram ๋ชจ๋ธ์„ ์ผ์ข…์˜ ์ฃผํ™ฉ ๋ฐ•์Šค๋กœ ์ƒ๊ฐํ•ด ๋ณธ๋‹ค๋ฉด, ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ž…๋ ฅ์€ ์ค‘์‹ฌ ๋‹จ์–ด, ๋ชจ๋ธ์˜ ์˜ˆ์ธก์€ ์ฃผ๋ณ€ ๋‹จ์–ด์ธ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง์„ ์‚ฌ์šฉํ•˜๋Š” Skip-gram(Skip-Gram with Negative Sampling, SGNS) ์ดํ•˜ SGNS๋Š” ์ด์™€๋Š” ๋‹ค๋ฅธ ์ ‘๊ทผ ๋ฐฉ์‹์„ ์ทจํ•ฉ๋‹ˆ๋‹ค. SGNS๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด๊ฐ€ ๋ชจ๋‘ ์ž…๋ ฅ์ด ๋˜๊ณ , ์ด ๋‘ ๋‹จ์–ด๊ฐ€ ์‹ค์ œ๋กœ ์œˆ๋„ ํฌ๊ธฐ ๋‚ด์— ์กด์žฌํ•˜๋Š” ์ด์›ƒ ๊ด€๊ณ„์ธ์ง€ ๊ทธ ํ™•๋ฅ ์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ Skip-gram ๋ฐ์ดํ„ฐ ์…‹์„ SGNS์˜ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •์„ ๋ด…์‹œ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์ขŒ์ธก์˜ ํ…Œ์ด๋ธ”์€ ๊ธฐ์กด์˜ Skip-gram์„ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์…‹์ž…๋‹ˆ๋‹ค. Skip-gram์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์ž…๋ ฅ, ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ๋ ˆ์ด๋ธ”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ SGNS๋ฅผ ํ•™์Šตํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์ด ๋ฐ์ดํ„ฐ ์…‹์„ ์šฐ์ธก์˜ ํ…Œ์ด๋ธ”๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„ , ๊ธฐ์กด์˜ Skip-gram ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ๊ฐ๊ฐ ์ž…๋ ฅ 1, ์ž…๋ ฅ 2๋กœ ๋‘ก๋‹ˆ๋‹ค. ์ด ๋‘˜์€ ์‹ค์ œ๋กœ ์œˆ๋„ ํฌ๊ธฐ ๋‚ด์—์„œ ์ด์›ƒ ๊ด€๊ณ„์˜€๋ฏ€๋กœ ๋ ˆ์ด๋ธ”์€ 1๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ ˆ์ด๋ธ”์ด 0์ธ ์ƒ˜ํ”Œ๋“ค์„ ์ค€๋น„ํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋Š” ์ž…๋ ฅ 1(์ค‘์‹ฌ ๋‹จ์–ด)์™€ ์ฃผ๋ณ€ ๋‹จ์–ด ๊ด€๊ณ„๊ฐ€ ์•„๋‹Œ ๋‹จ์–ด๋“ค์„ ์ž…๋ ฅ 2๋กœ ์‚ผ๊ธฐ ์œ„ํ•ด์„œ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋žœ๋ค์œผ๋กœ ์„ ํƒํ•œ ๋‹จ์–ด๋“ค์„ ์ž…๋ ฅ 2๋กœ ํ•˜๊ณ , ๋ ˆ์ด๋ธ”์„ 0์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์ด ๋ฐ์ดํ„ฐ ์…‹์€ ์ž…๋ ฅ 1๊ณผ ์ž…๋ ฅ 2๊ฐ€ ์‹ค์ œ๋กœ ์œˆ๋„ ํฌ๊ธฐ ๋‚ด์—์„œ ์ด์›ƒ ๊ด€๊ณ„์ธ ๊ฒฝ์šฐ์—๋Š” ๋ ˆ์ด๋ธ”์ด 1, ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” ๋ ˆ์ด๋ธ”์ด 0์ธ ๋ฐ์ดํ„ฐ ์…‹์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ œ ๋‘ ๊ฐœ์˜ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‘ ํ…Œ์ด๋ธ” ์ค‘ ํ•˜๋‚˜๋Š” ์ž…๋ ฅ 1์ธ ์ค‘์‹ฌ ๋‹จ์–ด์˜ ํ…Œ์ด๋ธ” ๋ฃฉ ์—…์„ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์ด๊ณ , ํ•˜๋‚˜๋Š” ์ž…๋ ฅ 2์ธ ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ํ…Œ์ด๋ธ” ๋ฃฉ ์—…์„ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด๋Š” ๊ฐ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์„ ํ…Œ์ด๋ธ” ๋ฃฉ ์—…ํ•˜์—ฌ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์„ ํ†ตํ•ด ํ…Œ์ด๋ธ” ๋ฃฉ ์—…ํ•˜์—ฌ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜๋˜์—ˆ๋‹ค๋ฉด ๊ทธ ํ›„์˜ ์—ฐ์‚ฐ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ๋‚ด์  ๊ฐ’์„ ์ด ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’์œผ๋กœ ํ•˜๊ณ , ๋ ˆ์ด๋ธ”๊ณผ์˜ ์˜ค์ฐจ๋กœ๋ถ€ํ„ฐ ์—ญ์ „ํŒŒ ํ•˜์—ฌ ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ํ›„์—๋Š” ์ขŒ์ธก์˜ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์„ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋‘ ํ–‰๋ ฌ์„ ๋”ํ•œ ํ›„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋‘ ํ–‰๋ ฌ์„ ์—ฐ๊ฒฐ(concatenate) ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์‹ค์Šต์—์„œ๋Š” ์ขŒ์ธก์˜ ํ–‰๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. 3. 20๋‰ด์Šค ๊ทธ๋ฃน ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ import pandas as pd import numpy as np import nltk from nltk.corpus import stopwords from sklearn.datasets import fetch_20newsgroups from tensorflow.keras.preprocessing.text import Tokenizer 20๋‰ด์Šค ๊ทธ๋ฃน ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ์— ์ตœ์†Œ ๋‹จ์–ด 2๊ฐœ๋Š” ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ๋งŒ ์ค‘์‹ฌ ๋‹จ์–ด, ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ๊ด€๊ณ„๊ฐ€ ์„ฑ๋ฆฝํ•˜๋ฉฐ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ƒ˜ํ”Œ์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์—†์–ด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ ์ง€์†์ ์œผ๋กœ ์ด๋ฅผ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ์ƒ˜ํ”Œ๋“ค์„ ์ œ๊ฑฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. dataset = fetch_20newsgroups(shuffle=True, random_state=1, remove=('headers', 'footers', 'quotes')) documents = dataset.data print('์ด ์ƒ˜ํ”Œ ์ˆ˜ :',len(documents)) ์ด ์ƒ˜ํ”Œ ์ˆ˜ : 11314 ์ด ์ƒ˜ํ”Œ ์ˆ˜๋Š” 11,314๊ฐœ์ž…๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ๋ถˆํ•„์š”ํ•œ ํ† ํฐ์„ ์ œ๊ฑฐํ•˜๊ณ , ์†Œ๋ฌธ์žํ™”๋ฅผ ํ†ตํ•ด ์ •๊ทœํ™”๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. news_df = pd.DataFrame({'document':documents}) # ํŠน์ˆ˜ ๋ฌธ์ž ์ œ๊ฑฐ news_df['clean_doc'] = news_df['document'].str.replace("[^a-zA-Z]", " ") # ๊ธธ์ด๊ฐ€ 3์ดํ•˜์ธ ๋‹จ์–ด๋Š” ์ œ๊ฑฐ (๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด ์ œ๊ฑฐ) news_df['clean_doc'] = news_df['clean_doc'].apply(lambda x: ' '.join([w for w in x.split() if len(w)>3])) # ์ „์ฒด ๋‹จ์–ด์— ๋Œ€ํ•œ ์†Œ๋ฌธ์ž ๋ณ€ํ™˜ news_df['clean_doc'] = news_df['clean_doc'].apply(lambda x: x.lower()) ํ˜„์žฌ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. news_df.isnull().values.any() False Null ๊ฐ’์ด ์—†์ง€๋งŒ, ๋นˆ ๊ฐ’(empy) ์œ ๋ฌด๋„ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋นˆ ๊ฐ’์„ Null ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ๋‹ค์‹œ Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. news_df.replace("", float("NaN"), inplace=True) news_df.isnull().values.any() True Null ๊ฐ’์ด ์žˆ์Œ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. Null ๊ฐ’์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. news_df.dropna(inplace=True) print('์ด ์ƒ˜ํ”Œ ์ˆ˜ :',len(news_df)) ์ด ์ƒ˜ํ”Œ ์ˆ˜ : 10995 ์ƒ˜ํ”Œ ์ˆ˜๊ฐ€ ์ผ๋ถ€ ์ค„์–ด๋“  ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. NLTK์—์„œ ์ •์˜ํ•œ ๋ถˆ์šฉ์–ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. # ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐ stop_words = stopwords.words('english') tokenized_doc = news_df['clean_doc'].apply(lambda x: x.split()) tokenized_doc = tokenized_doc.apply(lambda x: [item for item in x if item not in stop_words]) tokenized_doc = tokenized_doc.to_list() ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์˜€์œผ๋ฏ€๋กœ ๋‹จ์–ด์˜ ์ˆ˜๊ฐ€ ์ค„์–ด๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ์ƒ˜ํ”Œ ์ค‘ ๋‹จ์–ด๊ฐ€ 1๊ฐœ ์ดํ•˜์ธ ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ์ฐพ์•„ ์ œ๊ฑฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ๋‹จ์–ด๊ฐ€ 1๊ฐœ ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„์„œ ์ €์žฅํ•˜๊ณ , ํ•ด๋‹น ์ƒ˜ํ”Œ๋“ค์€ ์ œ๊ฑฐ. drop_train = [index for index, sentence in enumerate(tokenized_doc) if len(sentence) <= 1] tokenized_doc = np.delete(tokenized_doc, drop_train, axis=0) print('์ด ์ƒ˜ํ”Œ ์ˆ˜ :',len(tokenized_doc)) ์ด ์ƒ˜ํ”Œ ์ˆ˜ : 10940 ์ƒ˜ํ”Œ ์ˆ˜๊ฐ€ ๋‹ค์‹œ ์ค„์–ด๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•˜๊ณ , ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(tokenized_doc) word2idx = tokenizer.word_index idx2word = {value : key for key, value in word2idx.items()} encoded = tokenizer.texts_to_sequences(tokenized_doc) ์ƒ์œ„ 2๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(encoded[:2]) [[9, 59, 603, 207, 3278, 1495, 474, 702, 9470, 13686, 5533, 15227, 702, 442, 702, 70, 1148, 1095, 1036, 20294, 984, 705, 4294, 702, 217, 207, 1979, 15228, 13686, 4865, 4520, 87, 1530, 6, 52, 149, 581, 661, 4406, 4988, 4866, 1920, 755, 10668, 1102, 7837, 442, 957, 10669, 634, 51, 228, 2669, 4989, 178, 66, 222, 4521, 6066, 68, 4295], [1026, 532, 2, 60, 98, 582, 107, 800, 23, 79, 4522, 333, 7838, 864, 421, 3825, 458, 6488, 458, 2700, 4730, 333, 23, 9, 4731, 7262, 186, 310, 146, 170, 642, 1260, 107, 33568, 13, 985, 33569, 33570, 9471, 11491]] ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. vocab_size = len(word2idx) + 1 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', vocab_size) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 64277 ์ด 64,277๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 4. ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง์„ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ์…‹ ๊ตฌ์„ฑํ•˜๊ธฐ ํ† ํฐํ™”, ์ •์ œ, ์ •๊ทœํ™”, ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊นŒ์ง€ ์ผ๋ฐ˜์ ์ธ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์„ ๊ฑฐ์ณค์Šต๋‹ˆ๋‹ค. ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง์„ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ์…‹์„ ๊ตฌ์„ฑํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง์„ ์œ„ํ•ด์„œ ์ผ€๋ผ์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ์ „์ฒ˜๋ฆฌ ๋„๊ตฌ์ธ skipgrams๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ์ „์ฒ˜๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋˜๋Š”์ง€ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ (๊ฝค ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋Š” ์ž‘์—…์ด๋ฏ€๋กœ) ์ƒ์œ„ 10๊ฐœ์˜ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ๋งŒ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. from tensorflow.keras.preprocessing.sequence import skipgrams # ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง skip_grams = [skipgrams(sample, vocabulary_size=vocab_size, window_size=10) for sample in encoded[:10]] ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. 10๊ฐœ์˜ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋ชจ๋‘ ์ˆ˜ํ–‰๋˜์—ˆ์ง€๋งŒ, ์ฒซ ๋ฒˆ์งธ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ๋งŒ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์ธ skip_grams[0] ๋‚ด skipgrams๋กœ ํ˜•์„ฑ๋œ ๋ฐ์ดํ„ฐ ์…‹ ํ™•์ธ pairs, labels = skip_grams[0][0], skip_grams[0][1] for i in range(5): print("({:s} ({:d}), {:s} ({:d})) -> {:d}".format( idx2word[pairs[i][0]], pairs[i][0], idx2word[pairs[i][1]], pairs[i][1], labels[i])) (commited (7837), badar (34572)) -> 0 (whole (217), realize (1036)) -> 1 (reason (149), commited (7837)) -> 1 (letter (705), rediculous (15227)) -> 1 (reputation (5533), midonrnax (47527)) -> 0 ์œˆ๋„ ํฌ๊ธฐ ๋‚ด์—์„œ ์ค‘์‹ฌ ๋‹จ์–ด, ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ์—๋Š” 1์˜ ๋ ˆ์ด๋ธ”์„ ๊ฐ–๋„๋ก ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” 0์˜ ๋ ˆ์ด๋ธ”์„ ๊ฐ€์ง€๋„๋ก ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์…‹์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ ๊ฐ๊ฐ์˜ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋™์ผํ•œ ํ”„๋กœ์„ธ์Šค๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. print('์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜ :',len(skip_grams)) ์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜ : 10 encoded ์ค‘ ์ƒ์œ„ 10๊ฐœ์˜ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ๋งŒ ์ˆ˜ํ–‰ํ•˜์˜€์œผ๋ฏ€๋กœ 10์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  10๊ฐœ์˜ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ ๊ฐ๊ฐ์€ ์ˆ˜๋งŽ์€ ์ค‘์‹ฌ ๋‹จ์–ด, ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ์Œ์œผ๋กœ ๋œ ์ƒ˜ํ”Œ๋“ค์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” pairs์™€ labels์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # ์ฒซ ๋ฒˆ์งธ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ƒ๊ธด pairs์™€ labels์˜ ๊ฐœ์ˆ˜ print(len(pairs)) print(len(labels)) 2220 2220 ์ด ์ž‘์—…์„ ๋ชจ๋“  ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. skip_grams = [skipgrams(sample, vocabulary_size=vocab_size, window_size=10) for sample in encoded] 5. Skip-Gram with Negative Sampling(SGNS) ๊ตฌํ˜„ํ•˜๊ธฐ from tensorflow.keras.models import Sequential, Model from tensorflow.keras.layers import Embedding, Reshape, Activation, Input from tensorflow.keras.layers import Dot from tensorflow.keras.utils import plot_model from IPython.display import SVG ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 100์œผ๋กœ ์ •ํ•˜๊ณ , ๋‘ ๊ฐœ์˜ ์ž„๋ฒ ๋”ฉ ์ธต์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. embedding_dim = 100 # ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ” w_inputs = Input(shape=(1, ), dtype='int32') word_embedding = Embedding(vocab_size, embedding_dim)(w_inputs) # ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ” c_inputs = Input(shape=(1, ), dtype='int32') context_embedding = Embedding(vocab_size, embedding_dim)(c_inputs) ๊ฐ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์€ ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด ๊ฐ๊ฐ์„ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์ด๋ฉฐ ๊ฐ ๋‹จ์–ด๋Š” ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์„ ๊ฑฐ์ณ์„œ ๋‚ด์ ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ๋‚ด์ ์˜ ๊ฒฐ๊ณผ๋Š” 1 ๋˜๋Š” 0์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ๊ฑฐ์ณ ์ตœ์ข… ์˜ˆ์ธก๊ฐ’์„ ์–ป์Šต๋‹ˆ๋‹ค. dot_product = Dot(axes=2)([word_embedding, context_embedding]) dot_product = Reshape((1, ), input_shape=(1, 1))(dot_product) output = Activation('sigmoid')(dot_product) model = Model(inputs=[w_inputs, c_inputs], outputs=output) model.summary() model.compile(loss='binary_crossentropy', optimizer='adam') plot_model(model, to_file='model3.png', show_shapes=True, show_layer_names=True, rankdir='TB') ๋ชจ๋ธ์˜ ํ•™์Šต์€ 5์—ํฌํฌ ์ˆ˜ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. for epoch in range(1, 6): loss = 0 for _, elem in enumerate(skip_grams): first_elem = np.array(list(zip(*elem[0]))[0], dtype='int32') second_elem = np.array(list(zip(*elem[0]))[1], dtype='int32') labels = np.array(elem[1], dtype='int32') X = [first_elem, second_elem] Y = labels loss += model.train_on_batch(X, Y) print('Epoch :',epoch, 'Loss :',loss) Epoch: 1 Loss: 4339.997158139944 Epoch: 2 Loss: 3549.69356325455 Epoch: 3 Loss: 3295.072506020777 Epoch: 4 Loss: 3038.1063768607564 Epoch: 5 Loss: 2790.9479411702487 6. ๊ฒฐ๊ณผ ํ™•์ธํ•˜๊ธฐ ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•™์Šต๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ vector.txt์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ ์ด๋ฅผ gensim์˜ models.KeyedVectors.load_word2vec_format()์œผ๋กœ ๋กœ๋“œํ•˜๋ฉด ์‰ฝ๊ฒŒ ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. import gensim f = open('vectors.txt' ,'w') f.write('{} {}\n'.format(vocab_size-1, embed_size)) vectors = model.get_weights()[0] for word, i in tokenizer.word_index.items(): f.write('{} {}\n'.format(word, ' '.join(map(str, list(vectors[i, :]))))) f.close() # ๋ชจ๋ธ ๋กœ๋“œ w2v = gensim.models.KeyedVectors.load_word2vec_format('./vectors.txt', binary=False) w2v.most_similar(positive=['soldiers']) [('lebanese', 0.7539176940917969), ('troops', 0.7515299916267395), ('occupying', 0.7322258949279785), ('attacking', 0.7247686386108398), ('villagers', 0.7217503786087036), ('israeli', 0.7071422338485718), ('villages', 0.7000206708908081), ('wounded', 0.6976917386054993), ('lebanon', 0.6933401823043823), ('arab', 0.692956268787384)] w2v.most_similar(positive=['doctor']) [('nerve', 0.6576169729232788), ('migraine', 0.6502577066421509), ('patient', 0.6377599835395813), ('disease', 0.6300654411315918), ('quack', 0.6101700663566589), ('cardiac', 0.606243371963501), ('infection', 0.6030253171920776), ('medication', 0.6001783013343811), ('suffering', 0.593578040599823), ('hurt', 0.5818471908569336)] w2v.most_similar(positive=['police']) [('prohibit', 0.6182408332824707), ('provisions', 0.5706381797790527), ('cops', 0.565453290939331), ('army', 0.563193142414093), ('possess', 0.5538119673728943), ('armed', 0.5535427331924438), ('rkba', 0.5533647537231445), ('ksanti', 0.5518242716789246), ('courts', 0.5495947599411011), ('officers', 0.5477950572967529)] w2v.most_similar(positive=['knife']) [('knives', 0.7748741507530212), ('caucasus', 0.7227305769920349), ('defence', 0.7217429280281067), ('males', 0.7207540273666382), ('heretics', 0.7145630717277527), ('azerbaijanis', 0.7136125564575195), ('advocate', 0.7055186629295349), ('officers', 0.7020978927612305), ('punished', 0.7012225389480591), ('taxation', 0.7001351118087769)] w2v.most_similar(positive=['engine']) [('brakes', 0.7013274431228638), ('cylinder', 0.6680346727371216), ('brake', 0.6459399461746216), ('seat', 0.6365581154823303), ('gasoline', 0.6263373494148254), ('honda', 0.611443281173706), ('mounted', 0.6093355417251587), ('ventilator', 0.5999234318733215), ('adjustable', 0.5938659310340881), ('propellants', 0.5935063362121582)] 09-05) ๊ธ€๋กœ๋ธŒ(GloVe) ๊ธ€๋กœ๋ธŒ(Global Vectors for Word Representation, GloVe)๋Š” ์นด์šดํŠธ ๊ธฐ๋ฐ˜๊ณผ ์˜ˆ์ธก ๊ธฐ๋ฐ˜์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋ก ์œผ๋กœ 2014๋…„์— ๋ฏธ๊ตญ ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™์—์„œ ๊ฐœ๋ฐœํ•œ ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์ž…๋‹ˆ๋‹ค. ์•ž์„œ ํ•™์Šตํ•˜์˜€๋˜ ๊ธฐ์กด์˜ ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ LSA(Latent Semantic Analysis)์™€ ์˜ˆ์ธก ๊ธฐ๋ฐ˜์˜ Word2Vec์˜ ๋‹จ์ ์„ ์ง€์ ํ•˜๋ฉฐ ์ด๋ฅผ ๋ณด์™„ํ•œ๋‹ค๋Š” ๋ชฉ์ ์œผ๋กœ ๋‚˜์™”๊ณ , ์‹ค์ œ๋กœ๋„ Word2Vec ๋งŒํผ ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ˜„์žฌ๊นŒ์ง€์˜ ์—ฐ๊ตฌ์— ๋”ฐ๋ฅด๋ฉด ๋‹จ์ •์ ์œผ๋กœ Word2Vec์™€ GloVe ์ค‘์—์„œ ์–ด๋–ค ๊ฒƒ์ด ๋” ๋›ฐ์–ด๋‚˜๋‹ค๊ณ  ๋งํ•  ์ˆ˜๋Š” ์—†๊ณ , ์ด ๋‘ ๊ฐ€์ง€ ์ „๋ถ€๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ณ  ์„ฑ๋Šฅ์ด ๋” ์ข‹์€ ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. 1. ๊ธฐ์กด ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•œ ๋น„ํŒ ๊ธฐ์กด์˜ ๋ฐฉ๋ฒ•๋ก ์„ ์–ธ๊ธ‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. LSA๋Š” DTM์ด๋‚˜ TF-IDF ํ–‰๋ ฌ๊ณผ ๊ฐ™์ด ๊ฐ ๋ฌธ์„œ์—์„œ์˜ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•œ ํ–‰๋ ฌ์ด๋ผ๋Š” ์ „์ฒด์ ์ธ ํ†ต๊ณ„ ์ •๋ณด๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์ฐจ์›์„ ์ถ•์†Œ(Truncated SVD) ํ•˜์—ฌ ์ž ์žฌ๋œ ์˜๋ฏธ๋ฅผ ๋Œ์–ด๋‚ด๋Š” ๋ฐฉ๋ฒ•๋ก ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, Word2Vec๋Š” ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์— ๋Œ€ํ•œ ์˜ค์ฐจ๋ฅผ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ค„์—ฌ๋‚˜๊ฐ€๋ฉฐ ํ•™์Šตํ•˜๋Š” ์˜ˆ์ธก ๊ธฐ๋ฐ˜์˜ ๋ฐฉ๋ฒ•๋ก ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ์ด ๋‘ ๋ฐฉ๋ฒ•๋ก ์€ ๊ฐ๊ฐ ์žฅ, ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. LSA๋Š” ์นด์šดํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ฝ”ํผ์Šค์˜ ์ „์ฒด์ ์ธ ํ†ต๊ณ„ ์ •๋ณด๋ฅผ ๊ณ ๋ คํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ์™•:๋‚จ์ž = ์—ฌ์™•:? (์ •๋‹ต์€ ์—ฌ์ž)์™€ ๊ฐ™์€ ๋‹จ์–ด ์˜๋ฏธ์˜ ์œ ์ถ” ์ž‘์—…(Analogy task)์—๋Š” ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. Word2Vec๋Š” ์˜ˆ์ธก ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹จ์–ด ๊ฐ„ ์œ ์ถ” ์ž‘์—…์—๋Š” LSA๋ณด๋‹ค ๋›ฐ์–ด๋‚˜์ง€๋งŒ, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ์œˆ๋„ ํฌ๊ธฐ ๋‚ด์—์„œ๋งŒ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ๊ณ ๋ คํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”ํผ์Šค์˜ ์ „์ฒด์ ์ธ ํ†ต๊ณ„ ์ •๋ณด๋ฅผ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. GloVe๋Š” ์ด๋Ÿฌํ•œ ๊ธฐ์กด ๋ฐฉ๋ฒ•๋ก ๋“ค์˜ ๊ฐ๊ฐ์˜ ํ•œ๊ณ„๋ฅผ ์ง€์ ํ•˜๋ฉฐ, LSA์˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด์—ˆ๋˜ ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ ๋ฐฉ๋ฒ•๊ณผ Word2Vec์˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด์—ˆ๋˜ ์˜ˆ์ธก ๊ธฐ๋ฐ˜์˜ ๋ฐฉ๋ฒ•๋ก  ๋‘ ๊ฐ€์ง€๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 2. ์œˆ๋„ ๊ธฐ๋ฐ˜ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ(Window based Co-occurrence Matrix) ๋‹จ์–ด์˜ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์€ ํ–‰๊ณผ ์—ด์„ ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ๋‹จ์–ด๋“ค๋กœ ๊ตฌ์„ฑํ•˜๊ณ , i ๋‹จ์–ด์˜ ์œˆ๋„ ํฌ๊ธฐ(Window Size) ๋‚ด์—์„œ k ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ i ํ–‰ k ์—ด์— ๊ธฐ์žฌํ•œ ํ–‰๋ ฌ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ 3๊ฐœ ๋ฌธ์„œ๋กœ ๊ตฌ์„ฑ๋œ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. I like deep learning I like NLP I enjoy flying ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ N ์ผ ๋•Œ๋Š” ์ขŒ, ์šฐ์— ์กด์žฌํ•˜๋Š” N ๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์ฐธ๊ณ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ 1์ผ ๋•Œ, ์œ„์˜ ํ…์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ตฌ์„ฑํ•œ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์นด์šดํŠธ I like enjoy deep learning NLP flying I 0 2 1 0 0 0 0 like 2 0 0 1 0 1 0 enjoy 1 0 0 0 0 0 1 deep 0 1 0 0 1 0 0 learning 0 0 0 1 0 0 0 NLP 0 1 0 0 0 0 0 flying 0 0 1 0 0 0 0 ์œ„ ํ–‰๋ ฌ์€ ํ–‰๋ ฌ์„ ์ „์น˜(Transpose) ํ•ด๋„ ๋™์ผํ•œ ํ–‰๋ ฌ์ด ๋œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” i ๋‹จ์–ด์˜ ์œˆ๋„ ํฌ๊ธฐ ๋‚ด์—์„œ k ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ๋นˆ๋„๋Š” ๋ฐ˜๋Œ€๋กœ k ๋‹จ์–ด์˜ ์œˆ๋„ ํฌ๊ธฐ ๋‚ด์—์„œ i ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ๋นˆ๋„์™€ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ํ…Œ์ด๋ธ”์€ ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™๊ต์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งํฌ : http://web.stanford.edu/class/cs224n/slides/cs224n-2019-lecture02-wordvecs2.pdf 3. ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ (Co-occurrence Probability) ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ–ˆ์œผ๋‹ˆ, ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋Š” ์–ด๋–ค ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์„ ๊ฐ€์ง€๊ณ  ์ •๋ฆฌํ•œ ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ (Co-occurrence Probability)์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”? ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ  ( | i ) ๋Š” ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ๋กœ๋ถ€ํ„ฐ ํŠน์ • ๋‹จ์–ด i์˜ ์ „์ฒด ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๊ณ , ํŠน์ • ๋‹จ์–ด i๊ฐ€ ๋“ฑ์žฅํ–ˆ์„ ๋•Œ ์–ด๋–ค ๋‹จ์–ด k๊ฐ€ ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜์—ฌ ๊ณ„์‚ฐํ•œ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค. ( | i ) ์—์„œ i๋ฅผ ์ค‘์‹ฌ ๋‹จ์–ด(Center Word), k๋ฅผ ์ฃผ๋ณ€ ๋‹จ์–ด(Context Word)๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ์œ„์—์„œ ๋ฐฐ์šด ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์—์„œ ์ค‘์‹ฌ ๋‹จ์–ด i์˜ ํ–‰์˜ ๋ชจ๋“  ๊ฐ’์„ ๋”ํ•œ ๊ฐ’์„ ๋ถ„๋ชจ๋กœ ํ•˜๊ณ  i ํ–‰ k ์—ด์˜ ๊ฐ’์„ ๋ถ„์ž๋กœ ํ•œ ๊ฐ’์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ GloVe์˜ ์ œ์•ˆ ๋…ผ๋ฌธ์—์„œ ๊ฐ€์ ธ์˜จ ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์„ ํ‘œ๋กœ ์ •๋ฆฌํ•œ ํ•˜๋‚˜์˜ ์˜ˆ์ž…๋‹ˆ๋‹ค. ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ๊ณผ ํฌ๊ธฐ ๊ด€๊ณ„ ๋น„(ratio) k=solid k=gas k=water k=fasion P(k l ice) 0.00019 0.000066 0.003 0.000017 P(k l steam) 0.000022 0.00078 0.0022 0.000018 P(k l ice) / P(k l steam) 8.9 0.085 1.36 0.96 ์œ„์˜ ํ‘œ๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์‹ค์€ ice๊ฐ€ ๋“ฑ์žฅํ–ˆ์„ ๋•Œ solid๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ  0.00019์€ steam์ด ๋“ฑ์žฅํ–ˆ์„ ๋•Œ solid๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ ์ธ 0.000022๋ณด๋‹ค ์•ฝ 8.9๋ฐฐ ํฌ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋„ ๊ทธ๋Ÿด ๊ฒƒ์ด solid๋Š” '๋‹จ๋‹จํ•œ'์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์กŒ์œผ๋‹ˆ๊นŒ '์ฆ๊ธฐ'๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š” steam๋ณด๋‹ค๋Š” ๋‹น์—ฐํžˆ '์–ผ์Œ'์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š” ice๋ผ๋Š” ๋‹จ์–ด์™€ ๋” ์ž์ฃผ ๋“ฑ์žฅํ•  ๊ฒ๋‹ˆ๋‹ค. ์ˆ˜์‹์ ์œผ๋กœ ๋‹ค์‹œ ์ •๋ฆฌํ•˜์—ฌ ์–ธ๊ธ‰ํ•˜๋ฉด k๊ฐ€ solid ์ผ ๋•Œ, P(solid l ice) / P(solid l steam)๋ฅผ ๊ณ„์‚ฐํ•œ ๊ฐ’์€ 8.9๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ 1๋ณด๋‹ค๋Š” ๋งค์šฐ ํฐ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์™œ๋ƒ๋ฉด P(solid | ice)์˜ ๊ฐ’์€ ํฌ๊ณ , P(solid | steam)์˜ ๊ฐ’์€ ์ž‘๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ k๋ฅผ solid๊ฐ€ ์•„๋‹ˆ๋ผ gas๋กœ ๋ฐ”๊พธ๋ฉด ์–˜๊ธฐ๋Š” ์™„์ „ํžˆ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. gas๋Š” ice๋ณด๋‹ค๋Š” steam๊ณผ ๋” ์ž์ฃผ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ, P(gas l ice) / P(gas l steam)๋ฅผ ๊ณ„์‚ฐํ•œ ๊ฐ’์€ 1๋ณด๋‹ค ํ›จ์”ฌ ์ž‘์€ ๊ฐ’์ธ 0.085๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, k๊ฐ€ water์ธ ๊ฒฝ์šฐ์—๋Š” solid์™€ steam ๋‘ ๋‹จ์–ด ๋ชจ๋‘์™€ ๋™์‹œ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋ฏ€๋กœ 1์— ๊ฐ€๊นŒ์šด ๊ฐ’์ด ๋‚˜์˜ค๊ณ , k๊ฐ€ fasion์ธ ๊ฒฝ์šฐ์—๋Š” solid์™€ steam ๋‘ ๋‹จ์–ด ๋ชจ๋‘์™€ ๋™์‹œ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ ์œผ๋ฏ€๋กœ 1์— ๊ฐ€๊นŒ์šด ๊ฐ’์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋ณด๊ธฐ ์‰ฝ๋„๋ก ์กฐ๊ธˆ ๋‹จ์ˆœํ™”ํ•ด์„œ ํ‘œํ˜„ํ•œ ํ‘œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ๊ณผ ํฌ๊ธฐ ๊ด€๊ณ„ ๋น„(ratio) k=solid k=gas k=water k=fasion P(k l ice) ํฐ ๊ฐ’ ์ž‘์€ ๊ฐ’ ํฐ ๊ฐ’ ์ž‘์€ ๊ฐ’ P(k l steam) ์ž‘์€ ๊ฐ’ ํฐ ๊ฐ’ ํฐ ๊ฐ’ ์ž‘์€ ๊ฐ’ P(k l ice) / P(k l steam) ํฐ ๊ฐ’ ์ž‘์€ ๊ฐ’ 1์— ๊ฐ€๊นŒ์›€ 1์— ๊ฐ€๊นŒ์›€ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ๊ณผ ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์˜ ์ดํ•ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4. ์†์‹ค ํ•จ์ˆ˜(Loss function) ์šฐ์„  ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์ „์— ๊ฐ ์šฉ์–ด๋ฅผ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. : ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ(Co-occurrence Matrix) i : ์ค‘์‹ฌ ๋‹จ์–ด i๊ฐ€ ๋“ฑ์žฅํ–ˆ์„ ๋•Œ ์œˆ๋„ ๋‚ด ์ฃผ๋ณ€ ๋‹จ์–ด j๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ํšŸ์ˆ˜ i โˆ‘ X j : ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์—์„œ i ํ–‰์˜ ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’ i : ( | i ) X k i : ์ค‘์‹ฌ ๋‹จ์–ด i๊ฐ€ ๋“ฑ์žฅํ–ˆ์„ ๋•Œ ์œˆ๋„ ๋‚ด ์ฃผ๋ณ€ ๋‹จ์–ด k๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ  Ex) P(solid l ice) = ๋‹จ์–ด ice๊ฐ€ ๋“ฑ์žฅํ–ˆ์„ ๋•Œ ๋‹จ์–ด solid๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ  i P k P k P k ๋กœ ๋‚˜๋ˆ ์ค€ ๊ฐ’ Ex) P(solid l ice) / P(solid l steam) = 8.9 i : ์ค‘์‹ฌ ๋‹จ์–ด i์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ k : ์ฃผ๋ณ€ ๋‹จ์–ด k์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ GloVe์˜ ์•„์ด๋””์–ด๋ฅผ ํ•œ ์ค„๋กœ ์š”์•ฝํ•˜๋ฉด '์ž„๋ฒ ๋”ฉ ๋œ ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ๋‚ด์ ์ด ์ „์ฒด ์ฝ”ํผ์Šค์—์„œ์˜ ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์ด ๋˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ'์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ด๋ฅผ ๋งŒ์กฑํ•˜๋„๋ก ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o p o u t ( i w ~ ) P ( | i ) P k ๋’ค์—์„œ ๋ณด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ, ๋” ์ •ํ™•ํžˆ๋Š” GloVe๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋„๋ก ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. o p o u t ( i w ~ ) l g P ( | i ) l g P k ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๋‹จ์–ด ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ž˜ ํ‘œํ˜„ํ•˜๋Š” ํ•จ์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์•ž์„œ ๋ฐฐ์šด ๊ฐœ๋…์ธ i / j๋ฅผ ์‹์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. GloVe์˜ ์—ฐ๊ตฌ์ง„๋“ค์€ ๋ฒกํ„ฐ i w, k๋ฅผ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด, i / j ๊ฐ€ ๋‚˜์˜จ๋‹ค๋Š” ์ดˆ๊ธฐ ์‹์œผ๋กœ๋ถ€ํ„ฐ ์ „๊ฐœ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ( i w , w ~ ) P k j ์•„์ง ์ด ํ•จ์ˆ˜ ๊ฐ€ ์–ด๋–ค ์‹์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€๋Š” ์ •ํ•ด์ง„ ๊ฒŒ ์—†์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋ชฉ์ ์— ๋งž๊ฒŒ ๊ทผ์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์‹์€ ๋ฌด์ˆ˜ํžˆ ๋งŽ๊ฒ ์œผ๋‚˜ ์ตœ์ ์˜ ์‹์— ๋‹ค๊ฐ€๊ฐ€๊ธฐ ์œ„ํ•ด์„œ ์ฐจ๊ทผ, ์ฐจ๊ทผ ๋””ํ…Œ์ผ์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋Š” ๋‘ ๋‹จ์–ด ์‚ฌ์ด์˜ ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์˜ ํฌ๊ธฐ ๊ด€๊ณ„ ๋น„(ratio) ์ •๋ณด๋ฅผ ๋ฒกํ„ฐ ๊ณต๊ฐ„์— ์ธ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด GloVe ์—ฐ๊ตฌ์ง„๋“ค์€ i w๋ผ๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ์ฐจ์ด๋ฅผ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ( i w , w ~ ) P k j ๊ทธ๋Ÿฐ๋ฐ ์šฐ๋ณ€์€ ์Šค์นผ๋ผ ๊ฐ’์ด๊ณ  ์ขŒ๋ณ€์€ ๋ฒกํ„ฐ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์„ฑ๋ฆฝํ•˜๊ธฐ ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ ํ•จ์ˆ˜์˜ ๋‘ ์ž…๋ ฅ์— ๋‚ด์ (Dot product)์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ( ( i w) w ~ ) P k j ์ •๋ฆฌํ•˜๋ฉด, ์„ ํ˜• ๊ณต๊ฐ„(Linear space)์—์„œ ๋‹จ์–ด์˜ ์˜๋ฏธ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋บ„์…ˆ๊ณผ ๋‚ด์ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•จ์ˆ˜ ๊ฐ€ ๋งŒ์กฑํ•ด์•ผ ํ•  ํ•„์ˆ˜ ์กฐ๊ฑด์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด ์™€ ์ฃผ๋ณ€ ๋‹จ์–ด ~ ๋ผ๋Š” ์„ ํƒ ๊ธฐ์ค€์€ ์‹ค์ œ๋กœ๋Š” ๋ฌด์ž‘์œ„ ์„ ํƒ์ด๋ฏ€๋กœ ์ด ๋‘˜์˜ ๊ด€๊ณ„๋Š” ์ž์œ ๋กญ๊ฒŒ ๊ตํ™˜๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์„ฑ๋ฆฝ๋˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ GloVe ์—ฐ๊ตฌ์ง„์€ ํ•จ์ˆ˜ ๊ฐ€ ์‹ค์ˆ˜์˜ ๋ฅ์…ˆ๊ณผ ์–‘์ˆ˜์˜ ๊ณฑ์…ˆ์— ๋Œ€ํ•ด์„œ ์ค€๋™ํ˜•(Homomorphism)์„ ๋งŒ์กฑํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์ƒ์†Œํ•œ ์šฉ์–ด๋ผ์„œ ๋ง์ด ์–ด๋ ค์›Œ ๋ณด์ด๋Š”๋ฐ, ์ •๋ฆฌํ•˜๋ฉด ์™€์— ๋Œ€ํ•ด์„œ ํ•จ์ˆ˜ ๊ฐ€ ( + ) F ( ) ( ) ์™€ ๊ฐ™๋„๋ก ๋งŒ์กฑ์‹œ์ผœ์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( + ) F ( ) ( ) โˆ€, b R ์ด ์ค€๋™<NAME>์„ ํ˜„์žฌ ์ „๊ฐœํ•˜๋˜ GloVe ์‹์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์กฐ๊ธˆ์”ฉ ๋ฐ”๊ฟ”๋ณผ ๊ฒ๋‹ˆ๋‹ค. ์ „๊ฐœํ•˜๋˜ GloVe ์‹์— ๋”ฐ๋ฅด๋ฉด, ํ•จ์ˆ˜๋Š” ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ์Šค์นผ๋ผ ๊ฐ’( i P k )์ด ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ค€๋™<NAME>์—์„œ ์™€ ๊ฐ€ ๊ฐ๊ฐ ๋ฒกํ„ฐ ๊ฐ’์ด๋ผ๋ฉด ํ•จ์ˆ˜์˜ ๊ฒฐ๊ด๊ฐ’์œผ๋กœ๋Š” ์Šค์นผ๋ผ ๊ฐ’์ด ๋‚˜์˜ฌ ์ˆ˜ ์—†์ง€๋งŒ, ์™€ ๊ฐ€ ๊ฐ๊ฐ ์‚ฌ์‹ค ๋‘ ๋ฒกํ„ฐ์˜ ๋‚ด์  ๊ฐ’์ด๋ผ๊ณ  ํ•˜๋ฉด ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ์Šค์นผ๋ผ ๊ฐ’์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์œ„์˜ ์ค€๋™<NAME>์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊ฟ”๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ 1 v , v , v๋Š” ๊ฐ๊ฐ ๋ฒกํ„ฐ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋Š” ๋ฒกํ„ฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ( 1 v + 3 v) F ( 1 v) ( 3 v) โˆ€ 1 v , v , v โˆˆ ๊ทธ๋Ÿฐ๋ฐ ์•ž์„œ ์ž‘์„ฑํ•œ GloVe ์‹์—์„œ๋Š” i w๋ผ๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ์ฐจ์ด๋ฅผ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. GloVe ์‹์— ๋ฐ”๋กœ ์ ์šฉ์„ ์œ„ํ•ด ์ค€๋™ํ˜• ์‹์„ ์ด๋ฅผ ๋บ„์…ˆ์— ๋Œ€ํ•œ ์ค€๋™<NAME>์œผ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๊ณฑ์…ˆ๋„ ๋‚˜๋ˆ—์…ˆ์œผ๋กœ ๋ฐ”๋€๋‹ˆ๋‹ค. ( 1 v โˆ’ 3 v) F ( 1 v) ( 3 v) โˆ€ 1 v , v , v โˆˆ ์ด ์ค€๋™ํ˜• ์‹์„ GloVe ์‹์— ์ ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„ , ํ•จ์ˆ˜์˜ ์šฐ๋ณ€์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๋€Œ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ( ( i w) w ~ ) F ( i w ~ ) ( j w ~ ) ๊ทธ๋Ÿฐ๋ฐ ์ด์ „์˜ ์‹์— ๋”ฐ๋ฅด๋ฉด ์šฐ๋ณ€์€ ๋ณธ๋ž˜ i P k ์˜€์œผ๋ฏ€๋กœ, ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. i P k F ( i w ~ ) ( j w ~ ) ( i w ~ ) P k X k i ์ขŒ๋ณ€์„ ํ’€์–ด์“ฐ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( i w ~ โˆ’ w T k) F ( i w ~ ) ( j w ~ ) ์ด๋Š” ๋บ„์…ˆ์— ๋Œ€ํ•œ ์ค€๋™<NAME>์˜ ํ˜•ํƒœ์™€ ์ •ํ™•ํžˆ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋งŒ์กฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ฐพ์•„์•ผ ํ•  ๋•Œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ๋งŒ์กฑ์‹œํ‚ค๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ ๋ฐ”๋กœ<NAME> ํ•จ์ˆ˜(Exponential function)์ž…๋‹ˆ๋‹ค.๋ฅผ<NAME> ํ•จ์ˆ˜ x๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. x ( i w ~ โˆ’ w T k) e p ( i w ~ ) x ( j w ~ ) x ( i w ~ ) P k X k i ์œ„์˜ ๋‘ ๋ฒˆ์งธ ์‹์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. i w ~ l g P k l g ( i X) l g X k l g X ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ์ƒ๊ธฐํ•ด์•ผ ํ•  ๊ฒƒ์€ ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด, ์‚ฌ์‹ค i w ~ ๋Š” ๋‘ ๊ฐ’์˜ ์œ„์น˜๋ฅผ ์„œ๋กœ ๋ฐ”๊พธ์–ด๋„ ์‹์ด ์„ฑ๋ฆฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. i์˜ ์ •์˜๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด k ์™€๋„ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฒŒ ์„ฑ๋ฆฝ๋˜๋ ค๋ฉด ์œ„์˜ ์‹์—์„œ o X ํ•ญ์ด ๊ฑธ๋ฆผ๋Œ์ž…๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„๋งŒ ์—†๋‹ค๋ฉด ์ด๋ฅผ ์„ฑ๋ฆฝ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ GloVe ์—ฐ๊ตฌํŒ€์€ ์ด o X ํ•ญ์„ i ์— ๋Œ€ํ•œ ํŽธํ–ฅ i ๋ผ๋Š” ์ƒ์ˆ˜ํ•ญ์œผ๋กœ ๋Œ€์ฒดํ•˜๊ธฐ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ์ด์œ ๋กœ k์— ๋Œ€ํ•œ ํŽธํ–ฅ k๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. i w ~ b + k = o X k ์ด ์‹์ด ์†์‹ค ํ•จ์ˆ˜์˜ ํ•ต์‹ฌ์ด ๋˜๋Š” ์‹์ž…๋‹ˆ๋‹ค. ์šฐ๋ณ€์˜ ๊ฐ’๊ณผ์˜ ์ฐจ์ด๋ฅผ ์ตœ์†Œํ™”๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ขŒ๋ณ€์˜ 4๊ฐœ์˜ ํ•ญ์€ ํ•™์Šต์„ ํ†ตํ•ด ๊ฐ’์ด ๋ฐ”๋€Œ๋Š” ๋ณ€์ˆ˜๋“ค์ด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์†์‹ค ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ผ๋ฐ˜ํ™”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. o s f n t o = m n 1 ( m w ~ b + n โˆ’ o X n ) ์—ฌ๊ธฐ์„œ๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์•„์ง ์ตœ์ ์˜ ์†์‹ค ํ•จ์ˆ˜๋ผ๊ธฐ์—๋Š” ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. GloVe ์—ฐ๊ตฌ์ง„์€ o X k ์—์„œ i ๊ฐ’์ด 0์ด ๋  ์ˆ˜ ์žˆ์Œ์„ ์ง€์ ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์•ˆ ์ค‘ ํ•˜๋‚˜๋Š” o X k ํ•ญ์„ o ( + i) ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•ด๋„ ์—ฌ์ „ํžˆ ํ•ด๊ฒฐ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ ๋Š” ๋งˆ์น˜ DTM์ฒ˜๋Ÿผ ํฌ์†Œ ํ–‰๋ ฌ(Sparse Matrix) ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ๋‹ค๋ถ„ํ•˜๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์—๋Š” ๋งŽ์€ ๊ฐ’์ด 0์ด๊ฑฐ๋‚˜, ๋™์‹œ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ์ ์–ด์„œ ๋งŽ์€ ๊ฐ’์ด ์ž‘์€ ์ˆ˜์น˜๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ์ฃผ๋Š” ๊ณ ๋ฏผ์„ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ GloVe ์—ฐ๊ตฌํŒ€์ด ์„ ํƒํ•œ ๊ฒƒ์€ ๋ฐ”๋กœ i์˜ ๊ฐ’์— ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๊ฐ€์ค‘์น˜ ํ•จ์ˆ˜(Weighting function) ( i) ๋ฅผ ์†์‹ค ํ•จ์ˆ˜์— ๋„์ž…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. GloVe์— ๋„์ž…๋˜๋Š” ( i) ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. GloVe์˜ ์†์‹ค ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์ค‘์น˜ ํ•จ์ˆ˜๋Š” ๋™์‹œ ์ถœํ˜„ ๋นˆ๋„๊ฐ€ ๋†’์€ ๋‹จ์–ด ์Œ์— ๋‚ฎ์€ ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ€์—ฌํ•˜๊ณ , ๋™์‹œ ์ถœํ˜„ ๋นˆ๋„๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด ์Œ์— ๋†’์€ ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ด ์ด์œ ๋Š” ์ฃผ๋กœ ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๊ณ ๋นˆ๋„ ๋‹จ์–ด ์Œ์ด ๋ชจ๋ธ์„ ์ง€๋‚˜์น˜๊ฒŒ ์ง€๋ฐฐํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 'the'์™€ ๊ฐ™์€ ๋ถˆ์šฉ์–ด๋Š” ๊ฑฐ์˜ ๋ชจ๋“  ๋‹จ์–ด์™€ ํ•จ๊ป˜ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚˜์ง€๋งŒ, ์ด๋Š” ๊ทธ๊ฒƒ๋“ค์ด ์„œ๋กœ ์˜๋ฏธ์ ์œผ๋กœ ๊ฐ€๊น๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด๋Ÿฌํ•œ ๋‹จ์–ด ์Œ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์€ ๋ชจ๋ธ์ด ๋ณด๋‹ค ๋‹ค์–‘ํ•œ ํŒจํ„ด์„ ํ•™์Šตํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ํฌ์†Œํ•œ ๋‹จ์–ด ์Œ์˜ ์ •๋ณด๋ฅผ ๋ณด์กดํ•ฉ๋‹ˆ๋‹ค. ๋น„๋ก ํฌ์†Œํ•œ ๋‹จ์–ด ์Œ์ด์ง€๋งŒ, ๋‹จ์–ด ์‚ฌ์ด์˜ ๊ด€๊ณ„๋Š” ์—ฌ์ „ํžˆ ์ค‘์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 'astronaut'์™€ 'space'๋Š” ํ•จ๊ป˜ ๋‚˜ํƒ€๋‚˜๋Š” ๋นˆ๋„๊ฐ€ ๋น„๊ต์  ๋‚ฎ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด ๋‘ ๋‹จ์–ด ์‚ฌ์ด์˜ ๊ด€๊ณ„๋Š” ๊ต‰์žฅํžˆ ๊ฐ•ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์ด ๊ฐ€์ค‘์น˜ ํ•จ์ˆ˜๋Š” ๋‹จ์ˆœํžˆ ๋™์‹œ ์ถœํ˜„ ๋นˆ๋„๊ฐ€ ๋†’์€ ๋‹จ์–ด ์Œ๋งŒ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋” ๋‹ค์–‘ํ•œ ๋‹จ์–ด ์Œ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด ๋ณด๋‹ค ์ •ํ™•ํ•˜๊ณ  ๋‹ค์–‘ํ•œ ๋‹จ์–ด ์˜๋ฏธ๋ฅผ ํฌ์ฐฉํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋Š” 1๋ณด๋‹ค ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋„๋ก ํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋Š” 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์€ ๋™์‹œ ์ถœํ˜„ ํšŸ์ˆ˜๊ฐ€ m x ์ฆ‰, ๊ฐ€์žฅ ๋†’์€ ๋™์‹œ ์ถœํ˜„ ํšŸ์ˆ˜๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ๋‹จ์–ด ์Œ์— ๋Œ€ํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ์ œํ•œํ•˜๋ฏ€๋กœ, ์•„์ฃผ ๋†’์€ ๋™์‹œ ์ถœํ˜„ ํšŸ์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด ์Œ์ด ํ•™์Šต์— ๊ณผ๋„ํ•œ ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜์˜ ๊ฐ’์„ ์†์‹ค ํ•จ์ˆ˜์— ๊ณฑํ•ด์ฃผ๋ฉด ๊ฐ€์ค‘์น˜์˜ ์—ญํ• ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜ ( ) ์˜ ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. ( ) m n ( , ( / m x ) / ) ์ตœ์ข…์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ๋ฐ˜ํ™”๋œ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. o s f n t o = m n 1 f ( m) ( m w ~ b + n โˆ’ o X n GloVe ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ ๋ฐ ์‹ค์Šตํ•˜๊ณ  ํ›ˆ๋ จ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 5. GloVe ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ ์‹ค์Šต์„ ์œ„ํ•ด ํ”„๋กฌํ”„ํŠธ์—์„œ ์•„๋ž˜ ์ปค๋งจ๋“œ๋กœ GloVe ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install glove_python_binary GloVe์˜ ์ž…๋ ฅ์ด ๋˜๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” '์˜์–ด์™€ ํ•œ๊ตญ์–ด Word2Vec ํ•™์Šตํ•˜๊ธฐ' ์ฑ•ํ„ฐ์—์„œ ์‚ฌ์šฉํ•œ ์˜์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋™์ผํ•œ ์ „์ฒ˜๋ฆฌ๋ฅผ ๋งˆ์น˜๊ณ  ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ result์— ๊ฒฐ๊ณผ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. from glove import Corpus, Glove corpus = Corpus() # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ GloVe์—์„œ ์‚ฌ์šฉํ•  ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ ์ƒ์„ฑ corpus.fit(result, window=5) glove = Glove(no_components=100, learning_rate=0.05) # ํ•™์Šต์— ์ด์šฉํ•  ์Šค๋ ˆ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” 4๋กœ ์„ค์ •, ์—ํฌํฌ๋Š” 20. glove.fit(corpus.matrix, epochs=20, no_threads=4, verbose=True) glove.add_dictionary(corpus.dictionary) ํ•™์Šต์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. glove.most_similar()๋Š” ์ž…๋ ฅ ๋‹จ์–ด์˜ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. print(glove.most_similar("man")) [('woman', 0.9621753707315267), ('guy', 0.8860281455579162), ('girl', 0.8609057388487154), ('kid', 0.8383640509911114)] print(glove.most_similar("boy")) [('girl', 0.9436601252235809), ('kid', 0.8400949618225224), ('woman', 0.8397250531245034), ('man', 0.8303093585541573)] print(glove.most_similar("university")) [('harvard', 0.8690162017225468), ('cambridge', 0.8373272000675909), ('mit', 0.8288055170365777), ('stanford', 0.8212712738131419)] print(glove.most_similar("water")) [('air', 0.838286550826724), ('clean', 0.8326093688298345), ('fresh', 0.8232884971285377), ('electricity', 0.8097066570385377)] print(glove.most_similar("physics")) [('chemistry', 0.8379143027061764), ('biology', 0.827856517644139), ('economics', 0.775563255616767), ('finance', 0.7736692309034663)] print(glove.most_similar("muscle")) [('skeletal', 0.7977490484723809), ('tissue', 0.7714119298512192), ('nerve', 0.7477850181231441), ('stem', 0.7222964725687838)] print(glove.most_similar("clean")) [('water', 0.8264213732980569), ('fresh', 0.7850091074483321), ('wind', 0.7711854196846724), ('heat', 0.7646505765422197)] ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์ฐธ๊ณ  ์ž๋ฃŒ https://towardsdatascience.com/light-on-math-ml-intuitive-guide-to-understanding-glove-embeddings-b13b4f19c010 09-06 ํŒจ์ŠคํŠธ ํ…์ŠคํŠธ(FastText) ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ํŽ˜์ด์Šค๋ถ์—์„œ ๊ฐœ๋ฐœํ•œ FastText๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Word2Vec ์ดํ›„์— ๋‚˜์˜จ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ž์ฒด๋Š” Word2Vec์˜ ํ™•์žฅ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Word2Vec์™€ FastText์™€์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์ด๋ผ๋ฉด Word2Vec๋Š” ๋‹จ์–ด๋ฅผ ์ชผ๊ฐœ์งˆ ์ˆ˜ ์—†๋Š” ๋‹จ์œ„๋กœ ์ƒ๊ฐํ•œ๋‹ค๋ฉด, FastText๋Š” ํ•˜๋‚˜์˜ ๋‹จ์–ด ์•ˆ์—๋„ ์—ฌ๋Ÿฌ ๋‹จ์–ด๋“ค์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ๋‹จ์–ด. ์ฆ‰, ์„œ๋ธŒ ์›Œ๋“œ(subword)๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ๋‚ด๋ถ€ ๋‹จ์–ด(subword)์˜ ํ•™์Šต FastText์—์„œ๋Š” ๊ฐ ๋‹จ์–ด๋Š” ๊ธ€์ž ๋‹จ์œ„ n-gram์˜ ๊ตฌ์„ฑ์œผ๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. n์„ ๋ช‡์œผ๋กœ ๊ฒฐ์ •ํ•˜๋Š”์ง€์— ๋”ฐ๋ผ์„œ ๋‹จ์–ด๋“ค์ด ์–ผ๋งˆ๋‚˜ ๋ถ„๋ฆฌ๋˜๋Š”์ง€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ n์„ 3์œผ๋กœ ์žก์€ ํŠธ๋ผ์ด ๊ทธ๋žจ(tri-gram)์˜ ๊ฒฝ์šฐ, apple์€ app, ppl, ple๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ์ด๋“ค์„ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํžˆ๋Š” ์‹œ์ž‘๊ณผ ๋์„ ์˜๋ฏธํ•˜๋Š” <, >๋ฅผ ๋„์ž…ํ•˜์—ฌ ์•„๋ž˜์˜ 5๊ฐœ ๋‚ด๋ถ€ ๋‹จ์–ด(subword) ํ† ํฐ์„ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. # n = 3์ธ ๊ฒฝ์šฐ <ap, app, ppl, ple, le> ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์— ์ถ”๊ฐ€์ ์œผ๋กœ ํ•˜๋‚˜๋ฅผ ๋” ๋ฒกํ„ฐํ™”ํ•˜๋Š”๋ฐ, ๊ธฐ์กด ๋‹จ์–ด์— <, ์™€ >๋ฅผ ๋ถ™์ธ ํ† ํฐ์ž…๋‹ˆ๋‹ค. # ํŠน๋ณ„ ํ† ํฐ <apple> ๋‹ค์‹œ ๋งํ•ด n = 3์ธ ๊ฒฝ์šฐ, FastText๋Š” ๋‹จ์–ด apple์— ๋Œ€ํ•ด์„œ ๋‹ค์Œ์˜ 6๊ฐœ์˜ ํ† ํฐ์„ ๋ฒกํ„ฐํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. # n = 3์ธ ๊ฒฝ์šฐ <ap, app, ppl, ple, le>, <apple> ๊ทธ๋Ÿฐ๋ฐ ์‹ค์ œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” n์˜ ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๋ฒ”์œ„๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ๋Š” ๊ฐ๊ฐ 3๊ณผ 6์œผ๋กœ ์„ค์ •๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ตœ์†Ÿ๊ฐ’ = 3, ์ตœ๋Œ“๊ฐ’ = 6์ธ ๊ฒฝ์šฐ๋ผ๋ฉด, ๋‹จ์–ด apple์— ๋Œ€ํ•ด์„œ FastText๋Š” ์•„๋ž˜ ๋‚ด๋ถ€ ๋‹จ์–ด๋“ค์„ ๋ฒกํ„ฐํ™”ํ•ฉ๋‹ˆ๋‹ค. # n = 3 ~ 6์ธ ๊ฒฝ์šฐ <ap, app, ppl, ppl, le>, <app, appl, pple, ple>, <appl, pple>, ..., <apple> ์—ฌ๊ธฐ์„œ ๋‚ด๋ถ€ ๋‹จ์–ด๋“ค์„ ๋ฒกํ„ฐํ™”ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋Š” ์ € ๋‹จ์–ด๋“ค์— ๋Œ€ํ•ด์„œ Word2Vec์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ๋‚ด๋ถ€ ๋‹จ์–ด๋“ค์˜ ๋ฒกํ„ฐ ๊ฐ’์„ ์–ป์—ˆ๋‹ค๋ฉด, ๋‹จ์–ด apple์˜ ๋ฒกํ„ฐ ๊ฐ’์€ ์ € ์œ„ ๋ฒกํ„ฐ ๊ฐ’๋“ค์˜ ์ดํ•ฉ์œผ๋กœ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. apple = <ap + app + ppl + ppl + le> + <app + appl + pple + ple> + <appl + pple> + , ..., +<apple> ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์€ Word2Vec์—์„œ๋Š” ์–ป์„ ์ˆ˜ ์—†์—ˆ๋˜ ๊ฐ•์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 2. ๋ชจ๋ฅด๋Š” ๋‹จ์–ด(Out Of Vocabulary, OOV)์— ๋Œ€ํ•œ ๋Œ€์‘ FastText์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ํ•™์Šตํ•œ ํ›„์—๋Š” ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ชจ๋“  ๋‹จ์–ด์˜ ๊ฐ n-gram์— ๋Œ€ํ•ด์„œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์žฅ์ ์€ ๋ฐ์ดํ„ฐ ์…‹๋งŒ ์ถฉ๋ถ„ํ•œ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์€ ๋‚ด๋ถ€ ๋‹จ์–ด(Subword)๋ฅผ ํ†ตํ•ด ๋ชจ๋ฅด๋Š” ๋‹จ์–ด(Out Of Vocabulary, OOV)์— ๋Œ€ํ•ด์„œ๋„ ๋‹ค๋ฅธ ๋‹จ์–ด์™€์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น, FastText์—์„œ birthplace(์ถœ์ƒ์ง€)๋ž€ ๋‹จ์–ด๋ฅผ ํ•™์Šตํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๋‹จ์–ด์—์„œ birth์™€ place๋ผ๋Š” ๋‚ด๋ถ€ ๋‹จ์–ด๊ฐ€ ์žˆ์—ˆ๋‹ค๋ฉด, FastText๋Š” birthplace์˜ ๋ฒกํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ฅด๋Š” ๋‹จ์–ด์— ์ œ๋Œ€๋กœ ๋Œ€์ฒ˜ํ•  ์ˆ˜ ์—†๋Š” Word2Vec, GloVe์™€๋Š” ๋‹ค๋ฅธ ์ ์ž…๋‹ˆ๋‹ค. 3. ๋‹จ์–ด ์ง‘ํ•ฉ ๋‚ด ๋นˆ๋„ ์ˆ˜๊ฐ€ ์ ์—ˆ๋˜ ๋‹จ์–ด(Rare Word)์— ๋Œ€ํ•œ ๋Œ€์‘ Word2Vec์˜ ๊ฒฝ์šฐ์—๋Š” ๋“ฑ์žฅ ๋นˆ๋„ ์ˆ˜๊ฐ€ ์ ์€ ๋‹จ์–ด(rare word)์— ๋Œ€ํ•ด์„œ๋Š” ์ž„๋ฒ ๋”ฉ์˜ ์ •ํ™•๋„๊ฐ€ ๋†’์ง€ ์•Š๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์ ๋‹ค ๋ณด๋‹ˆ ์ •ํ™•ํ•˜๊ฒŒ ์ž„๋ฒ ๋”ฉ์ด ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ FastText์˜ ๊ฒฝ์šฐ, ๋งŒ์•ฝ ๋‹จ์–ด๊ฐ€ ํฌ๊ท€ ๋‹จ์–ด๋ผ๋„, ๊ทธ ๋‹จ์–ด์˜ n-gram์ด ๋‹ค๋ฅธ ๋‹จ์–ด์˜ n-gram๊ณผ ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด, Word2Vec๊ณผ ๋น„๊ตํ•˜์—ฌ ๋น„๊ต์  ๋†’์€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์„ ์–ป์Šต๋‹ˆ๋‹ค. FastText๊ฐ€ ๋…ธ์ด์ฆˆ๊ฐ€ ๋งŽ์€ ์ฝ”ํผ์Šค์—์„œ ๊ฐ•์ ์„ ๊ฐ€์ง„ ๊ฒƒ ๋˜ํ•œ ์ด์™€ ๊ฐ™์€ ์ด์œ ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ํ›ˆ๋ จ ์ฝ”ํผ์Šค์— ์˜คํƒ€(Typo)๋‚˜ ๋งž์ถค๋ฒ•์ด ํ‹€๋ฆฐ ๋‹จ์–ด๊ฐ€ ์—†์œผ๋ฉด ์ด์ƒ์ ์ด๊ฒ ์ง€๋งŒ, ์‹ค์ œ ๋งŽ์€ ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ์—๋Š” ์˜คํƒ€๊ฐ€ ์„ž์—ฌ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜คํƒ€๊ฐ€ ์„ž์ธ ๋‹จ์–ด๋Š” ๋‹น์—ฐํžˆ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋งค์šฐ ์ ์œผ๋ฏ€๋กœ ์ผ์ข…์˜ ํฌ๊ท€ ๋‹จ์–ด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, Word2Vec์—์„œ๋Š” ์˜คํƒ€๊ฐ€ ์„ž์ธ ๋‹จ์–ด๋Š” ์ž„๋ฒ ๋”ฉ์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์ง€๋งŒ FastText๋Š” ์ด์— ๋Œ€ํ•ด์„œ๋„ ์ผ์ • ์ˆ˜์ค€์˜ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์–ด apple๊ณผ ์˜คํƒ€๋กœ p๋ฅผ ํ•œ ๋ฒˆ ๋” ์ž…๋ ฅํ•œ appple์˜ ๊ฒฝ์šฐ์—๋Š” ์‹ค์ œ๋กœ ๋งŽ์€ ๊ฐœ์ˆ˜์˜ ๋™์ผํ•œ n-gram์„ ๊ฐ€์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 4. ์‹ค์Šต์œผ๋กœ ๋น„๊ตํ•˜๋Š” Word2Vec Vs. FastText ๊ฐ„๋‹จํ•œ ์‹ค์Šต์„ ํ†ตํ•ด Word2Vec์™€ FastText์˜ ์ฐจ์ด๋ฅผ ๋น„๊ตํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ, ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋Š” Word2Vec๋ฅผ ์‹ค์Šตํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ–ˆ๋˜ ์ด์ „ ์ฑ•ํ„ฐ์˜ ๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 1) Word2Vec ์šฐ์„ , ์ด์ „ Word2Vec์˜ ์‹ค์Šต( https://wikidocs.net/50739 )์˜ ์ „์ฒ˜๋ฆฌ ์ฝ”๋“œ์™€ Word2Vec ํ•™์Šต ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์ˆ˜ํ–‰ํ–ˆ์Œ์„ ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋ฅผ ์ฐพ์•„๋‚ด๋Š” ์ฝ”๋“œ์— ์ด๋ฒˆ์—๋Š” electrofishing์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model.wv.most_similar("electrofishing") ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์ •์ƒ ์ž‘๋™ํ•˜์ง€ ์•Š๊ณ  ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. KeyError: "word 'electrofishing' not in vocabulary" ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์— electrofishing์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ Word2Vec๋Š” ํ•™์Šต ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋‹จ์–ด. ์ฆ‰, ๋ชจ๋ฅด๋Š” ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 2) FastText ์ด๋ฒˆ์—๋Š” ์ „์ฒ˜๋ฆฌ ์ฝ”๋“œ๋Š” ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ณ  Word2Vec ํ•™์Šต ์ฝ”๋“œ๋งŒ FastText ํ•™์Šต ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‹คํ–‰ํ•ด ๋ด…์‹œ๋‹ค. from gensim.models import FastText model = FastText(result, size=100, window=5, min_count=5, workers=4, sg=1) electrofishing์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌ ๋‹จ์–ด๋ฅผ ์ฐพ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. model.wv.most_similar("electrofishing") [('electrolux', 0.7934642434120178), ('electrolyte', 0.78279709815979), ('electro', 0.779127836227417), ('electric', 0.7753111720085144), ('airbus', 0.7648627758026123), ('fukushima', 0.7612422704696655), ('electrochemical', 0.7611693143844604), ('gastric', 0.7483425140380859), ('electroshock', 0.7477173805236816), ('overfishing', 0.7435552477836609)] Word2Vec๋Š” ํ•™์Šตํ•˜์ง€ ์•Š์€ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋ฅผ ์ฐพ์•„๋‚ด์ง€ ๋ชปํ–ˆ์ง€๋งŒ, FastText๋Š” ์œ ์‚ฌํ•œ ๋‹จ์–ด๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 5. ํ•œ๊ตญ์–ด์—์„œ์˜ FastText ํ•œ๊ตญ์–ด์˜ ๊ฒฝ์šฐ์—๋„ OOV ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด FastText๋ฅผ ์ ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์‹œ๋„๋“ค์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. (1) ์Œ์ ˆ ๋‹จ์œ„ ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์Œ์ ˆ ๋‹จ์œ„์˜ ์ž„๋ฒ ๋”ฉ์˜ ๊ฒฝ์šฐ์— n=3์ผ ๋•Œ โ€˜์ž์—ฐ์–ด ์ฒ˜๋ฆฌโ€™๋ผ๋Š” ๋‹จ์–ด์— ๋Œ€ํ•ด n-gram์„ ๋งŒ๋“ค์–ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. <์ž์—ฐ, ์ž์—ฐ์–ด, ์—ฐ์–ด์ฒ˜, ์–ด์ฒ˜๋ฆฌ, ์ฒ˜๋ฆฌ> (2) ์ž๋ชจ ๋‹จ์œ„ ์ด์ œ ๋” ๋‚˜์•„๊ฐ€ ์ž๋ชจ ๋‹จ์œ„(์ดˆ์„ฑ, ์ค‘์„ฑ, ์ข…์„ฑ ๋‹จ์œ„)๋กœ ์ž„๋ฒ ๋”ฉํ•˜๋Š” ์‹œ๋„ ๋˜ํ•œ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์Œ์ ˆ ๋‹จ์œ„๊ฐ€ ์•„๋‹ˆ๋ผ, ์ž๋ชจ ๋‹จ์œ„๋กœ ๊ฐ€๊ฒŒ ๋˜๋ฉด ์˜คํƒ€๋‚˜ ๋…ธ์ด์ฆˆ ์ธก๋ฉด์—์„œ ๋” ๊ฐ•ํ•œ ์ž„๋ฒ ๋”ฉ์„ ๊ธฐ๋Œ€ํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜์ž์—ฐ์–ด ์ฒ˜๋ฆฌโ€™๋ผ๋Š” ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ดˆ์„ฑ, ์ค‘์„ฑ, ์ข…์„ฑ์„ ๋ถ„๋ฆฌํ•˜๊ณ , ๋งŒ์•ฝ, ์ข…์„ฑ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด โ€˜_โ€™๋ผ๋Š” ํ† ํฐ์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค๋ฉด โ€˜์ž์—ฐ์–ด ์ฒ˜๋ฆฌโ€™๋ผ๋Š” ๋‹จ์–ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ถ„๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ถ„๋ฆฌ๋œ ๊ฒฐ๊ณผ : ใ…ˆ ใ… _ ใ…‡ ใ…• ใ„ด ใ…‡ ใ…“ _ ใ…Š ใ…“ _ ใ„น ใ…ฃ _ ๊ทธ๋ฆฌ๊ณ  ๋ถ„๋ฆฌ๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ n=3์ผ ๋•Œ, n-gram์„ ์ ์šฉํ•˜์—ฌ, ์ž„๋ฒ ๋”ฉ์„ ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. < ใ…ˆ ใ…, ใ…ˆ ใ… _, ใ… _ ใ…‡, ... ์ค‘๋žต> ์ด์–ด์„œ ์ž๋ชจ ๋‹จ์œ„ FastText๋ฅผ ์‹ค์Šตํ•ดํ•ด๋ด…์‹œ๋‹ค. 09-07 ์ž๋ชจ ๋‹จ์œ„ ํ•œ๊ตญ์–ด FastText ํ•™์Šตํ•˜๊ธฐ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 09-08 ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Pre-trained Word Embedding) ์ด๋ฒˆ์—๋Š” ์ผ€๋ผ์Šค์˜ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer) ๊ณผ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(pre-trained word embedding) ์„ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋น„๊ตํ•ด ๋ด…๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๊ฐ–๊ณ  ์žˆ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋‹จ์–ด๋“ค์„ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์„ ๊ตฌํ˜„ํ•˜์—ฌ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ํ•™์Šตํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์—์„œ๋Š” ์ด๋ฅผ Embedding()์ด๋ผ๋Š” ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚คํ”ผ๋””์•„ ๋“ฑ๊ณผ ๊ฐ™์€ ๋ฐฉ๋Œ€ํ•œ ์ฝ”ํผ์Šค๋ฅผ ๊ฐ€์ง€๊ณ  Word2vec, FastText, GloVe ๋“ฑ์„ ํ†ตํ•ด์„œ ๋ฏธ๋ฆฌ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ˜„์žฌ ๊ฐ–๊ณ  ์žˆ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์ž„๋ฒ ๋”ฉ ์ธต์œผ๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šต์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ๋Š” ๋Œ€์กฐ๋ฉ๋‹ˆ๋‹ค. 1. ์ผ€๋ผ์Šค ์ž„๋ฒ ๋”ฉ ์ธต(Keras Embedding layer) ์ผ€๋ผ์Šค๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋‹จ์–ด๋“ค์— ๋Œ€ํ•ด ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋„๊ตฌ Embedding()์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Embedding()์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๊ตฌ์กฐ ๊ด€์ ์—์„œ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. 1) ์ž„๋ฒ ๋”ฉ ์ธต์€ ๋ฃฉ์—… ํ…Œ์ด๋ธ”์ด๋‹ค. ์ž„๋ฒ ๋”ฉ ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ฐ ๋‹จ์–ด๋“ค์€ ๋ชจ๋‘ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋˜์–ด์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๋‹จ์–ด โ†’ ๋‹จ์–ด์— ๋ถ€์—ฌ๋œ ๊ณ ์œ ํ•œ ์ •์ˆซ๊ฐ’ โ†’ ์ž„๋ฒ ๋”ฉ ์ธต ํ†ต๊ณผ โ†’ ๋ฐ€์ง‘ ๋ฒกํ„ฐ ์ž„๋ฒ ๋”ฉ ์ธต์€ ์ž…๋ ฅ ์ •์ˆ˜์— ๋Œ€ํ•ด ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)๋กœ ๋งคํ•‘ํ•˜๊ณ  ์ด ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ•™์Šต ๊ณผ์ •์—์„œ ๊ฐ€์ค‘์น˜๊ฐ€ ํ•™์Šต๋˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํ›ˆ๋ จ๋ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ๋‹จ์–ด๋Š” ๋ชจ๋ธ์ด ํ’€๊ณ ์ž ํ•˜๋Š” ์ž‘์—…์— ๋งž๋Š” ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋ฅผ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ •์ˆ˜๋ฅผ ๋ฐ€์ง‘ ๋ฒกํ„ฐ ๋˜๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋งคํ•‘ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์–ด๋–ค ์˜๋ฏธ์ผ๊นŒ์š”? ํŠน์ • ๋‹จ์–ด์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋ฅผ ์ธ๋ฑ์Šค๋กœ ๊ฐ€์ง€๋Š” ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฃฉ์—… ํ…Œ์ด๋ธ”์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ…Œ์ด๋ธ”์€ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒํผ์˜ ํ–‰์„ ๊ฐ€์ง€๋ฏ€๋กœ ๋ชจ๋“  ๋‹จ์–ด๋Š” ๊ณ ์œ ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๋‹จ์–ด great์ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ํ›„ ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ ํ•ด๋‹น ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๊บผ๋‚ด์˜ค๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 4๋กœ ์„ค์ •๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹จ์–ด great์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ 1,918์˜ ์ •์ˆ˜๋กœ ์ธ์ฝ”๋”ฉ์ด ๋˜์—ˆ๊ณ  ๊ทธ์— ๋”ฐ๋ผ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒํผ์˜ ํ–‰์„ ๊ฐ€์ง€๋Š” ํ…Œ์ด๋ธ”์—์„œ ์ธ๋ฑ์Šค 1,918๋ฒˆ์— ์œ„์น˜ํ•œ ํ–‰์„ ๋‹จ์–ด great์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ๋ชจ๋ธ์˜ ์ž…๋ ฅ์ด ๋˜๊ณ , ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ๋‹จ์–ด great์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์ด ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ๋ฃฉ์—… ํ…Œ์ด๋ธ”์˜ ๊ฐœ๋…์„ ์ด๋ก ์ ์œผ๋กœ ์šฐ์„  ์ ‘ํ•˜๊ณ , ์ฒ˜์Œ ์ผ€๋ผ์Šค๋ฅผ ๋ฐฐ์šธ ๋•Œ ์–ด๋–ค ๋ถ„๋“ค์€ ์ž„๋ฒ ๋”ฉ ์ธต์˜ ์ž…๋ ฅ์ด ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ์•„๋‹ˆ์–ด๋„ ๋™์ž‘ํ•œ๋‹ค๋Š” ์ ์— ํ—ท๊ฐˆ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ NNLM์ด๋‚˜ Word2Vec์„ ์„ค๋ช…ํ•  ๋•Œ ๋ฃฉ์—… ํ…Œ์ด๋ธ”์„ ์–ธ๊ธ‰ํ•˜๋ฉด์„œ ์ž…๋ ฅ์„ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ๊ฐ€์ •ํ•˜๊ณ  ์„ค๋ช…๋“œ๋ ธ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ, ์ผ€๋ผ์Šค๋Š” ๋‹จ์–ด๋ฅผ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ๋ฐ”๊พธ๊ณ  ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ ํ›„ ์ž„๋ฒ ๋”ฉ ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋‹จ์–ด๋ฅผ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊นŒ์ง€๋งŒ ์ง„ํ–‰ ํ›„ ์ž„๋ฒ ๋”ฉ ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋ฃฉ์—… ํ…Œ์ด๋ธ” ๊ฒฐ๊ณผ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์˜ ์ž„๋ฒ ๋”ฉ ์ธต ๊ตฌํ˜„ ์ฝ”๋“œ๋ฅผ ๋ด…์‹œ๋‹ค. vocab_size = 20000 output_dim = 128 input_length = 500 v = Embedding(vocab_size, output_dim, input_length=input_length) ์ž„๋ฒ ๋”ฉ ์ธต์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ธ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. vocab_size = ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. output_dim = ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ํ›„์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ž…๋‹ˆ๋‹ค. input_length = ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ธธ์ด์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๊ฐ€ 500๊ฐœ์ด๋ผ๋ฉด ์ด ๊ฐ’์€ 500์ž…๋‹ˆ๋‹ค. Embedding()์€ (number of samples, input_length)์ธ 2D ์ •์ˆ˜ ํ…์„œ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฐ sample์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋œ ๊ฒฐ๊ณผ๋กœ ์ •์ˆ˜ ์‹œํ€€์Šค์ž…๋‹ˆ๋‹ค. Embedding()์€ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  (number of samples, input_length, embedding word dimentionality)์ธ 3D ์‹ค์ˆ˜ ํ…์„œ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์˜ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์„ ์‚ฌ์šฉํ•˜๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2) ์ž„๋ฒ ๋”ฉ ์ธต ์‚ฌ์šฉํ•˜๊ธฐ ๋ฌธ์žฅ์˜ ๊ธ, ๋ถ€์ •์„ ํŒ๋‹จํ•˜๋Š” ๊ฐ์„ฑ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ๋ฌธ์žฅ๊ณผ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๊ธ์ •์ธ ๋ฌธ์žฅ์€ ๋ ˆ์ด๋ธ” 1, ๋ถ€์ •์ธ ๋ฌธ์žฅ์€ ๋ ˆ์ด๋ธ”์ด 0์ž…๋‹ˆ๋‹ค. import numpy as np from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences sentences = ['nice great best amazing', 'stop lies', 'pitiful nerd', 'excellent work', 'supreme quality', 'bad', 'highly respectable'] y_train = [1, 0, 0, 1, 1, 0, 1] ์ผ€๋ผ์Šค์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค๊ณ  ๊ทธ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(sentences) vocab_size = len(tokenizer.word_index) + 1 # ํŒจ๋”ฉ์„ ๊ณ ๋ คํ•˜์—ฌ +1 print('๋‹จ์–ด ์ง‘ํ•ฉ :',vocab_size) ๋‹จ์–ด ์ง‘ํ•ฉ : 16 ๊ฐ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. X_encoded = tokenizer.texts_to_sequences(sentences) print('์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ :',X_encoded) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ : [[1, 2, 3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13], [14, 15]] ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋ฌธ์žฅ์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. max_len = max(len(l) for l in X_encoded) print('์ตœ๋Œ€ ๊ธธ์ด :',max_len) ์ตœ๋Œ€ ๊ธธ์ด : 4 ์ตœ๋Œ€ ๊ธธ์ด๋กœ ๋ชจ๋“  ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ํŒจ๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. X_train = pad_sequences(X_encoded, maxlen=max_len, padding='post') y_train = np.array(y_train) print('ํŒจ๋”ฉ ๊ฒฐ๊ณผ :') print(X_train) ํŒจ๋”ฉ ๊ฒฐ๊ณผ : [[ 1 2 3 4] [ 5 6 0 0] [ 7 8 0 0] [ 9 10 0 0] [11 12 0 0] [13 0 0 0] [14 15 0 0]] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ „ํ˜•์ ์ธ ์ด์ง„ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์— 1๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ๋ฐฐ์น˜ํ•˜๊ณ  ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ, ๊ทธ๋ฆฌ๊ณ  ์†์‹ค ํ•จ์ˆ˜๋กœ binary_crossentropy๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ 100 ์—ํฌํฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Embedding, Flatten embedding_dim = 4 model = Sequential() model.add(Embedding(vocab_size, embedding_dim, input_length=max_len)) model.add(Flatten()) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) model.fit(X_train, y_train, epochs=100, verbose=2) ํ•™์Šต ๊ณผ์ •์—์„œ ํ˜„์žฌ ๊ฐ ๋‹จ์–ด๋“ค์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์˜ ๊ฐ’์€ ์ถœ๋ ฅ์ธต์˜ ๊ฐ€์ค‘์น˜์™€ ํ•จ๊ป˜ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. 2. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Pre-Trained Word Embedding) ์‚ฌ์šฉํ•˜๊ธฐ ์ผ€๋ผ์Šค์˜ Embedding()์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์„ ํ•™์Šตํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ๋•Œ๋กœ๋Š” ์ด๋ฏธ ํ›ˆ๋ จ๋ผ ์žˆ๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ๊ฐ€์ ธ์™€์„œ ์ด๋ฅผ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์€ ์ƒํ™ฉ์ด๋ผ๋ฉด ์ผ€๋ผ์Šค์˜ Embedding()์œผ๋กœ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ์— ์ตœ์ ํ™”๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์„ ์–ป๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฌธ์ œ์— ํŠนํ™”๋œ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ๋ณด๋‹ค ๋งŽ์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ์ด๋ฏธ Word2Vec์ด๋‚˜ GloVe ๋“ฑ์œผ๋กœ ํ•™์Šต๋ผ ์žˆ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์˜ ๊ฐœ์„ ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe์™€ Word2Vec ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•ด์„œ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. GloVe ๋‹ค์šด๋กœ๋“œ ๋งํฌ : http://nlp.stanford.edu/data/glove.6B.zip Word2Vec ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์•ž์„œ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐ์ดํ„ฐ์— ๋™์ผํ•œ ์ „์ฒ˜๋ฆฌ๊นŒ์ง€ ์ง„ํ–‰๋œ ์ƒํƒœ๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. print(X_train) [[ 1 2 3 4] [ 5 6 0 0] [ 7 8 0 0] [ 9 10 0 0] [11 12 0 0] [13 0 0 0] [14 15 0 0]] print(y_train) [1, 0, 0, 1, 1, 0, 1] 1) ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe ์‚ฌ์šฉํ•˜๊ธฐ glove.6B.zip๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์••์ถ•์„ ํ’€๋ฉด ๋‹ค์ˆ˜์˜ ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ๋Š” glove.6B.100d.txt ํŒŒ์ผ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. from urllib.request import urlretrieve, urlopen import gzip import zipfile urlretrieve("http://nlp.stanford.edu/data/glove.6B.zip", filename="glove.6B.zip") zf = zipfile.ZipFile('glove.6B.zip') zf.extractall() zf.close() glove.6B.100d.txt์— ์žˆ๋Š” ๋ชจ๋“  ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ ์ž๋ฃŒ๊ตฌ์กฐ ๋”•์…”๋„ˆ๋ฆฌ(dictionary)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋กœ๋“œํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. embedding_dict = dict() f = open('glove.6B.100d.txt', encoding="utf8") for line in f: word_vector = line.split() word = word_vector[0] # 100๊ฐœ์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” array๋กœ ๋ณ€ํ™˜ word_vector_arr = np.asarray(word_vector[1:], dtype='float32') embedding_dict[word] = word_vector_arr f.close() print('%s ๊ฐœ์˜ Embedding vector๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.' % len(embedding_dict)) 400000๊ฐœ์˜ Embedding vector๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด 40๋งŒ ๊ฐœ์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ž„์˜์˜ ๋‹จ์–ด 'respectable'์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’๊ณผ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…๋‹ˆ๋‹ค. print(embedding_dict['respectable']) print('๋ฒกํ„ฐ์˜ ์ฐจ์› ์ˆ˜ :',len(embedding_dict['respectable'])) [-0.049773 0.19903 0.10585 ... ์ค‘๋žต ... -0.032502 0.38025 ] ๋ฒกํ„ฐ์˜ ์ฐจ์› ์ˆ˜ : 100 ๋ฒกํ„ฐ ๊ฐ’์ด ์ถœ๋ ฅ๋˜๋ฉฐ ๋ฒกํ„ฐ์˜ ์ฐจ์› ์ˆ˜๋Š” 100์ž…๋‹ˆ๋‹ค. ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ ํฌ๊ธฐ์˜ ํ–‰๊ณผ 100๊ฐœ์˜ ์—ด์„ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ํ–‰๋ ฌ์˜ ๊ฐ’์€ ์ „๋ถ€ 0์œผ๋กœ ์ฑ„์›๋‹ˆ๋‹ค. ์ด ํ–‰๋ ฌ์— ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๊ฐ’์„ ๋„ฃ์–ด์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. embedding_matrix = np.zeros((vocab_size, 100)) print('์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) :',np.shape(embedding_matrix) ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) : (16, 100) ๊ธฐ์กด ๋ฐ์ดํ„ฐ์˜ ๊ฐ ๋‹จ์–ด์™€ ๋งคํ•‘๋œ ์ •์ˆซ๊ฐ’์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(tokenizer.word_index.items()) dict_items([('nice', 1), ('great', 2), ('best', 3), ('amazing', 4), ('stop', 5), ('lies', 6), ('pitiful', 7), ('nerd', 8), ('excellent', 9), ('work', 10), ('supreme', 11), ('quality', 12), ('bad', 13), ('highly', 14), ('respectable', 15)]) ๋‹จ์–ด 'great'์˜ ๋งคํ•‘๋œ ์ •์ˆ˜๋Š” 2์ž…๋‹ˆ๋‹ค. print('๋‹จ์–ด great์˜ ๋งคํ•‘๋œ ์ •์ˆ˜ :',tokenizer.word_index['great'] ๋‹จ์–ด great์˜ ๋งคํ•‘๋œ ์ •์ˆ˜ : 2 ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe์—์„œ 'great'์˜ ๋ฒกํ„ฐ ๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print(embedding_dict['great']) [-0.013786 0.38216 0.53236 0.15261 -0.29694 -0.20558 .. ์ค‘๋žต ... -0.69183 -1.0426 0.28855 0.63056 ] ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ๋งคํ•‘ ํ•œ ํ›„ 'great'์˜ ๋ฒกํ„ฐ ๊ฐ’์ด ์˜๋„ํ•œ ์ธ๋ฑ์Šค์˜ ์œ„์น˜์— ์‚ฝ์ž…๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. for word, index in tokenizer.word_index.items(): # ๋‹จ์–ด์™€ ๋งคํ•‘๋˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’ vector_value = embedding_dict.get(word) if vector_value is not None: embedding_matrix[index] = vector_value embedding_matrix์˜ ์ธ๋ฑ์Šค 2์—์„œ์˜ ๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. embedding_matrix[2] array([-0.013786 , 0.38216001, 0.53236002, 0.15261 , -0.29694 , ... ์ค‘๋žต ... -0.39346001, -0.69182998, -1.04260004, 0.28854999, 0.63055998]) ์ด์ „์— ํ™•์ธํ•œ ์‚ฌ์ „์— ํ›ˆ๋ จ๋œ GloVe์—์„œ์˜ 'great'์˜ ๋ฒกํ„ฐ ๊ฐ’๊ณผ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ Embedding layer์— embedding_matrix๋ฅผ ์ดˆ๊นƒ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ์‹ค์Šต์—์„œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ 100์ฐจ์›์˜ ๊ฐ’์ธ ๊ฒƒ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž„๋ฒ ๋”ฉ ์ธต์˜ output_dim์˜ ์ธ์ž ๊ฐ’์œผ๋กœ 100์„ ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ถ”๊ฐ€ ํ›ˆ๋ จ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ์—์„œ trainable์˜ ์ธ์ž ๊ฐ’์„ False๋กœ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Embedding, Flatten output_dim = 100 model = Sequential() e = Embedding(vocab_size, output_dim, weights=[embedding_matrix], input_length=max_len, trainable=False) model.add(e) model.add(Flatten()) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) model.fit(X_train, y_train, epochs=100, verbose=2) ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•œ ์˜ˆ์ œ๋Š” ์•„๋ž˜์˜ ์ผ€๋ผ์Šค ๋ธ”๋กœ๊ทธ ๋งํฌ์—๋„ ๊ธฐ์žฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://blog.keras.io/using-pre-trained-word-embeddings-in-a-keras-model.html 2) ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2Vec ์‚ฌ์šฉํ•˜๊ธฐ ๊ตฌ๊ธ€์˜ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2Vec ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜์—ฌ word2vec_model์— ์ €์žฅ ํ›„ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. import gensim urlretrieve("https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz", \ filename="GoogleNews-vectors-negative300.bin.gz") word2vec_model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin.gz', binary=True) print('๋ชจ๋ธ์˜ ํฌ๊ธฐ(shape) :',word2vec_model.vectors.shape) # ๋ชจ๋ธ์˜ ํฌ๊ธฐ ํ™•์ธ ๋ชจ๋ธ์˜ ํฌ๊ธฐ(shape) : (3000000, 300) 300์˜ ์ฐจ์›์„ ๊ฐ€์ง„ Word2Vec ๋ฒกํ„ฐ๊ฐ€ 3,000,000๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ฐ’์ด 0์œผ๋กœ ์ฑ„์›Œ์ง„ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ ํฌ๊ธฐ์˜ ํ–‰๊ณผ 300๊ฐœ์˜ ์—ด์„ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ํ–‰๋ ฌ์˜ ๊ฐ’์€ ์ „๋ถ€ 0์œผ๋กœ ์ฑ„์›๋‹ˆ๋‹ค. ์ด ํ–‰๋ ฌ์— ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๊ฐ’์„ ๋„ฃ์–ด์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. embedding_matrix = np.zeros((vocab_size, 300)) print('์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) :',np.shape(embedding_matrix) ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) : (16, 300) word2vec_model์—์„œ ํŠน์ • ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋ฆฌํ„ด ๋ฐ›์„ ํ…๋ฐ, ๋งŒ์•ฝ word2vec_model์— ํŠน์ • ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ์—†๋‹ค๋ฉด None์„ ๋ฆฌํ„ดํ•˜๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜ get_vector()๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. def get_vector(word): if word in word2vec_model: return word2vec_model[word] else: return None ๋‹จ์–ด ์ง‘ํ•ฉ์œผ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ 1๊ฐœ์”ฉ ํ˜ธ์ถœํ•˜์—ฌ word2vec_model์— ํ•ด๋‹น ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ None์ด ์•„๋‹ˆ๋ผ๋ฉด ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์— ํ•ด๋‹น ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค ์œ„์น˜์˜ ํ–‰์— ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. for word, index in tokenizer.word_index.items(): # ๋‹จ์–ด์™€ ๋งคํ•‘๋˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’ vector_value = get_vector(word) if vector_value is not None: embedding_matrix[index] = vector_value ํ˜„์žฌ ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์˜ 16๊ฐœ์˜ ๋‹จ์–ด์™€ ๋งคํ•‘๋˜๋Š” ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์ด ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ œ๋Œ€๋กœ ๋งคํ•‘์ด ๋๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ๊นŒ์š”? ๊ธฐ์กด์— word2vec_model์— ์ €์žฅ๋˜์–ด ์žˆ๋˜ ๋‹จ์–ด 'nice'์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(word2vec_model['nice']) [ 0.15820312 0.10595703 -0.18945312 0.38671875 0.08349609 -0.26757812 0.08349609 0.11328125 -0.10400391 0.17871094 -0.12353516 -0.22265625 ... ์ค‘๋žต ... -0.16894531 -0.08642578 -0.08544922 0.18945312 -0.14648438 0.13476562 -0.04077148 0.03271484 0.08935547 -0.26757812 0.00836182 -0.21386719] ๋‹จ์–ด 'nice'์˜ ๋งคํ•‘๋œ ์ •์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('๋‹จ์–ด nice์˜ ๋งคํ•‘๋œ ์ •์ˆ˜ :', tokenizer.word_index['nice']) ๋‹จ์–ด nice์˜ ๋งคํ•‘๋œ ์ •์ˆ˜ : 1 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ embedding_matirx์˜ 1๋ฒˆ ์ธ๋ฑ์Šค์—๋Š” ๋‹จ์–ด 'nice'์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. print(embedding_matrix[1]) [ 0.15820312 0.10595703 -0.18945312 0.38671875 0.08349609 -0.26757812 0.08349609 0.11328125 -0.10400391 0.17871094 -0.12353516 -0.22265625 ... ์ค‘๋žต ... -0.16894531 -0.08642578 -0.08544922 0.18945312 -0.14648438 0.13476562 -0.04077148 0.03271484 0.08935547 -0.26757812 0.00836182 -0.21386719] ๊ฐ’์ด word2vec_model์—์„œ ํ™•์ธํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์— ์žˆ๋Š” ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์— ๋Œ€ํ•ด์„œ๋„ ํ™•์ธํ•ด ๋ณด์„ธ์š”. ์ด์ œ Embedding์— ์‚ฌ์ „ ํ›ˆ๋ จ๋œ embedding_matrix๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด์ฃผ๊ณ  ๋ชจ๋ธ์„ ํ•™์Šต์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Embedding, Flatten, Input model = Sequential() model.add(Input(shape=(max_len,), dtype='int32')) e = Embedding(vocab_size, 300, weights=[embedding_matrix], input_length=max_len, trainable=False) model.add(e) model.add(Flatten()) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) model.fit(X_train, y_train, epochs=100, verbose=2) ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋Š” 'NLP๋ฅผ ์ด์šฉํ•œ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง' ์ฑ•ํ„ฐ์˜ ์˜๋„ ๋ถ„๋ฅ˜ ์‹ค์Šต( https://wikidocs.net/86083 )์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. 09-09 ์—˜๋ชจ(Embeddings from Language Model, ELMo) ๋…ผ๋ฌธ ๋งํฌ : https://aclweb.org/anthology/N18-1202 ELMo(Embeddings from Language Model)๋Š” 2018๋…„์— ์ œ์•ˆ๋œ ์ƒˆ๋กœ์šด ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์ž…๋‹ˆ๋‹ค. ELMo๋ผ๋Š” ์ด๋ฆ„์€ ์„ธ์„œ๋ฏธ ์ŠคํŠธ๋ฆฌํŠธ๋ผ๋Š” ๋ฏธ๊ตญ ์ธํ˜•๊ทน์˜ ์บ๋ฆญํ„ฐ ์ด๋ฆ„์ด๊ธฐ๋„ ํ•œ๋ฐ, ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” BERT๋‚˜ ์ตœ๊ทผ ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ๊ฐ€ ์‚ฌ์šฉํ•œ Big Bird๋ผ๋Š” NLP ๋ชจ๋ธ ๋˜ํ•œ ELMo์— ์ด์–ด ์„ธ์„œ๋ฏธ ์ŠคํŠธ๋ฆฌํŠธ์˜ ์บ๋ฆญํ„ฐ์˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ELMo๋Š” Embeddings from Language Model์˜ ์•ฝ์ž์ž…๋‹ˆ๋‹ค. ํ•ด์„ํ•˜๋ฉด '์–ธ์–ด ๋ชจ๋ธ๋กœ ํ•˜๋Š” ์ž„๋ฒ ๋”ฉ'์ž…๋‹ˆ๋‹ค. ELMo์˜ ๊ฐ€์žฅ ํฐ ํŠน์ง•์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ(Pre-trained language model)์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ELMo์˜ ์ด๋ฆ„์— LM์ด ๋“ค์–ด๊ฐ„ ์ด์œ ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ํ…์„œ ํ”Œ๋กœ 2.0์—์„œ๋Š” TF-Hub์˜ ELMo๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋ ค๋ฉด ํ…์„œ ํ”Œ๋กœ ๋ฒ„์ „์„ 1๋ฒ„์ „์œผ๋กœ ๋‚ฎ์ถ”์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Colab์—์„œ ์‹ค์Šตํ•˜์‹œ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ ๋“œ๋ฆฝ๋‹ˆ๋‹ค. Colab์—์„œ๋Š” ์†์‰ฝ๊ฒŒ ํ…์„œ ํ”Œ๋กœ ๋ฒ„์ „์„ 1๋ฒ„์ „์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์‹ค์Šต ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. 1. ELMo(Embeddings from Language Model) Bank๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. Bank Account(์€ํ–‰ ๊ณ„์ขŒ)์™€ River Bank(๊ฐ•๋‘‘)์—์„œ์˜ Bank๋Š” ์ „ํ˜€ ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ, Word2Vec์ด๋‚˜ GloVe ๋“ฑ์œผ๋กœ ํ‘œํ˜„๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์€ ์ด๋ฅผ ์ œ๋Œ€๋กœ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ Word2Vec์ด๋‚˜ GloVe ๋“ฑ์˜ ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์œผ๋กœ Bank๋ž€ ๋‹จ์–ด๋ฅผ [0.2 0.8 -1.2]๋ผ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ์ž„๋ฒ ๋”ฉํ•˜์˜€๋‹ค๊ณ  ํ•˜๋ฉด, ์ด ๋‹จ์–ด๋Š” Bank Account(์€ํ–‰ ๊ณ„์ขŒ)์™€ River Bank(๊ฐ•๋‘‘)์—์„œ์˜ Bank๋Š” ์ „ํ˜€ ๋‹ค๋ฅธ ์˜๋ฏธ์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋‘ ๊ฐ€์ง€ ์ƒํ™ฉ ๋ชจ๋‘์—์„œ [0.2 0.8 -1.2]์˜ ๋ฒกํ„ฐ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ํ‘œ๊ธฐ์˜ ๋‹จ์–ด๋ผ๋„ ๋ฌธ๋งฅ์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅด๊ฒŒ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์„ฑ๋Šฅ์„ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์‹œ ๋ฌธ๋งฅ์„ ๊ณ ๋ คํ•ด์„œ ์ž„๋ฒ ๋”ฉ์„ ํ•˜๊ฒ ๋‹ค๋Š” ์•„์ด๋””์–ด๊ฐ€ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Contextualized Word Embedding)์ž…๋‹ˆ๋‹ค. 2. biLM(Bidirectional Language Model)์˜ ์‚ฌ์ „ ํ›ˆ๋ จ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ž‘์—…์ธ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์€๋‹‰์ธต์ด 2๊ฐœ์ธ ์ผ๋ฐ˜์ ์ธ ๋‹จ๋ฐฉํ–ฅ RNN ์–ธ์–ด ๋ชจ๋ธ์˜ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. RNN ์–ธ์–ด ๋ชจ๋ธ์€ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด ๋‹จ์œ„๋กœ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋ฐ, RNN ๋‚ด๋ถ€์˜ ์€๋‹‰ ์ƒํƒœ t ๋Š” ์‹œ์ (time step)์ด ์ง€๋‚ ์ˆ˜๋ก ์ ์  ์—…๋ฐ์ดํŠธ๋ผ๊ฐ‘๋‹ˆ๋‹ค. ์ด๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ RNN์˜ t ์˜ ๊ฐ’์ด ๋ฌธ์žฅ์˜ ๋ฌธ๋งฅ ์ •๋ณด๋ฅผ ์ ์ฐจ์ ์œผ๋กœ ๋ฐ˜์˜ํ•œ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ELMo๋Š” ์œ„์˜ ๊ทธ๋ฆผ์˜ ์ˆœ๋ฐฉํ–ฅ RNN๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์œ„์˜ ๊ทธ๋ฆผ๊ณผ๋Š” ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฌธ์žฅ์„ ์Šค์บ”ํ•˜๋Š” ์—ญ๋ฐฉํ–ฅ RNN ๋˜ํ•œ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ELMo๋Š” ์–‘์ชฝ ๋ฐฉํ–ฅ์˜ ์–ธ์–ด ๋ชจ๋ธ์„ ๋‘˜ ๋‹ค ํ•™์Šตํ•˜์—ฌ ํ™œ์šฉํ•œ๋‹ค๊ณ  ํ•˜์—ฌ ์ด ์–ธ์–ด ๋ชจ๋ธ์„ biLM(Bidirectional Language Model)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ELMo์—์„œ ๋งํ•˜๋Š” biLM์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์ธต ๊ตฌ์กฐ(Multi-layer)๋ฅผ ์ „์ œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต์ด ์ตœ์†Œ 2๊ฐœ ์ด์ƒ์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์€๋‹‰์ธต์ด 2๊ฐœ์ธ ์ˆœ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ์—ญ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋•Œ biLM์˜ ๊ฐ ์‹œ์ ์˜ ์ž…๋ ฅ์ด ๋˜๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ๋Š” ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ์„ค๋ช…ํ•œ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์„ ์‚ฌ์šฉํ•ด์„œ ์–ป์€ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ(character embedding)์„ ํ†ตํ•ด ์–ป์€ ๋‹จ์–ด ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•œ ์„ค๋ช…์€ 'NLP๋ฅผ ์œ„ํ•œ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง' ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃจ๋Š” ๋‚ด์šฉ์œผ๋กœ ์—ฌ๊ธฐ์„œ๋Š” ์ž„๋ฒ ๋”ฉ์ธต, Word2Vec ๋“ฑ ์™ธ์— ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ์‹๋„ ์žˆ๋‹ค๊ณ ๋งŒ ์•Œ์•„๋‘ก์‹œ๋‹ค. ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์€ ๋งˆ์น˜ ์„œ๋ธŒ ๋‹จ์–ด(subword)์˜ ์ •๋ณด๋ฅผ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ฌธ๋งฅ๊ณผ ์ƒ๊ด€์—†์ด dog๋ž€ ๋‹จ์–ด์™€ doggy๋ž€ ๋‹จ์–ด์˜ ์—ฐ๊ด€์„ฑ์„ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ๋ฐฉ๋ฒ•์€ OOV์—๋„ ๊ฒฌ๊ณ ํ•œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์•ž์„œ ์„ค๋ช…ํ•œ ์–‘๋ฐฉํ–ฅ RNN๊ณผ ELMo์—์„œ์˜ biLM์€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ RNN์€ ์ˆœ๋ฐฉํ–ฅ RNN์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์—ญ๋ฐฉํ–ฅ์˜ RNN์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ๋‹ค์Œ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, biLM์˜ ์ˆœ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ์—ญ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์ด๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ณ„๊ฐœ์˜ ๋ชจ๋ธ๋กœ ๋ณด๊ณ  ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 3. biLM์˜ ํ™œ์šฉ biLM์ด ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ํ†ตํ•ด ํ•™์Šต๋œ ํ›„ ELMo๊ฐ€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ biLM์„ ํ†ตํ•ด ์ž…๋ ฅ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ ์ž„๋ฒ ๋”ฉํ•˜๊ธฐ ์œ„ํ•œ ๊ณผ์ •์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” play๋ž€ ๋‹จ์–ด๊ฐ€ ์ž„๋ฒ ๋”ฉ์ด ๋˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฐ€์ • ํ•˜์— ELMo๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. play๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ž„๋ฒ ๋”ฉ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ELMo๋Š” ์œ„์˜ ์ ์„ ์˜ ์‚ฌ๊ฐํ˜• ๋‚ด๋ถ€์˜ ๊ฐ ์ธต์˜ ๊ฒฐ๊ด๊ฐ’์„ ์žฌ๋ฃŒ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ•ด๋‹น ์‹œ์ (time step)์˜ BiLM์˜ ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ˆœ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ์—ญ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์„ ์—ฐ๊ฒฐ(concatenate) ํ•˜๊ณ  ์ถ”๊ฐ€ ์ž‘์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์ด๋ž€ ์ฒซ ๋ฒˆ์งธ๋Š” ์ž„๋ฒ ๋”ฉ ์ธต์„ ๋งํ•˜๋ฉฐ, ๋‚˜๋จธ์ง€ ์ธต์€ ๊ฐ ์ธต์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ELMo์˜ ์ง๊ด€์ ์ธ ์•„์ด๋””์–ด๋Š” ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์ด ๊ฐ€์ง„ ์ •๋ณด๋Š” ์ „๋ถ€ ์„œ๋กœ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ์„ ๊ฒƒ์ด๋ฏ€๋กœ, ์ด๋“ค์„ ๋ชจ๋‘ ํ™œ์šฉํ•œ๋‹ค๋Š” ์ ์— ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ELMo๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 1) ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์„ ์—ฐ๊ฒฐ(concatenate) ํ•œ๋‹ค. 2) ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’ ๋ณ„๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ์ค€๋‹ค. ์ด ๊ฐ€์ค‘์น˜๋ฅผ ์—ฌ๊ธฐ์„œ๋Š” 1 s, 3 ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. 3) ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•œ๋‹ค. 2) ๋ฒˆ๊ณผ 3) ๋ฒˆ์˜ ๋‹จ๊ณ„๋ฅผ ์š”์•ฝํ•˜์—ฌ ๊ฐ€์ค‘ํ•ฉ(Weighted Sum)์„ ํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4) ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์Šค์นผ๋ผ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ณฑํ•œ๋‹ค. ์ด ์Šค์นผ๋ผ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์—ฌ๊ธฐ์„œ๋Š”์ด๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋ ‡๊ฒŒ ์™„์„ฑ๋œ ๋ฒกํ„ฐ๋ฅผ ELMo ํ‘œํ˜„(representation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” ELMo ํ‘œํ˜„์„ ์–ป๊ธฐ ์œ„ํ•œ ๊ณผ์ •์ด๊ณ  ์ด์ œ ELMo๋ฅผ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์€ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜, ์งˆ์˜์‘๋‹ต ์‹œ์Šคํ…œ ๋“ฑ์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์„ ํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ELMo ํ‘œํ˜„์„ ์–ด๋–ป๊ฒŒ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ELMo ํ‘œํ˜„์„ ๊ธฐ์กด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์„ ์œ„ํ•ด์„œ GloVe์™€ ๊ฐ™์€ ๊ธฐ์กด์˜ ๋ฐฉ๋ฒ•๋ก ์„ ์‚ฌ์šฉํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์ค€๋น„ํ–ˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋•Œ, GloVe๋ฅผ ์‚ฌ์šฉํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋งŒ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ด๋ ‡๊ฒŒ ์ค€๋น„๋œ ELMo ํ‘œํ˜„์„ GloVe ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์™€ ์—ฐ๊ฒฐ(concatenate) ํ•ด์„œ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋•Œ biLM์˜ ๊ฐ€์ค‘์น˜๋Š” ๊ณ ์ •์‹œํ‚ค๊ณ , ์œ„์—์„œ ์‚ฌ์šฉํ•œ 1 s, 3 ฮณ ๋Š” ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ELMo ํ‘œํ˜„์ด ๊ธฐ์กด์˜ GloVe ๋“ฑ๊ณผ ๊ฐ™์€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์™€ ํ•จ๊ป˜ NLP ํƒœ์Šคํฌ์˜ ์ž…๋ ฅ์ด ๋˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 4. ELMo ํ‘œํ˜„์„ ์‚ฌ์šฉํ•ด์„œ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ด๋ฒˆ ์˜ˆ์ œ์˜ ์‹ค์Šต์€ Colab์—์„œ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์‹œ์ž‘ ์ „์— ํ…์„œ ํ”Œ๋กœ ๋ฒ„์ „์„ 1๋ฒ„์ „์œผ๋กœ ์„ค์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. %tensorflow_version 1.x ํ…์„œ ํ”Œ๋กœ ํ—ˆ๋ธŒ๋กœ๋ถ€ํ„ฐ ๋‹ค์–‘ํ•œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ(Pre-tained Model)๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ ELMo ํ‘œํ˜„์„ ์‚ฌ์šฉํ•ด ๋ณด๋Š” ์ •๋„๋กœ ์˜ˆ์ œ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹œ์ž‘ ์ „์— ํ…์„œ ํ”Œ๋กœ ํ—ˆ๋ธŒ๋ฅผ ์ธ์Šคํ†จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œˆ๋„์˜ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋‚˜ UNIX์˜ ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜์˜ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. pip install tensorflow-hub ์„ค์น˜๊ฐ€ ๋๋‚ฌ๋‹ค๋ฉด ์ด์ œ ํ…์„œ ํ”Œ๋กœ ํ—ˆ๋ธŒ๋ฅผ ์ž„ํฌํŠธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import tensorflow_hub as hub import tensorflow as tf from keras import backend as K import urllib.request import pandas as pd import numpy as np ํ…์„œ ํ”Œ๋กœ ํ—ˆ๋ธŒ๋กœ๋ถ€ํ„ฐ ELMo๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. elmo = hub.Module("https://tfhub.dev/google/elmo/1", trainable=True) # ํ…์„œ ํ”Œ๋กœ ํ—ˆ๋ธŒ๋กœ๋ถ€ํ„ฐ ELMo๋ฅผ ๋‹ค์šด๋กœ๋“œ sess = tf.Session() K.set_session(sess) sess.run(tf.global_variables_initializer()) sess.run(tf.tables_initializer()) ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•„์š”ํ•œ ๊ฒƒ๋“ค์„ ์ž„ํฌํŠธ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ , 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ ์›๋ณธ ์ถœ์ฒ˜ : https://www.kaggle.com/uciml/sms-spam-collection-dataset ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/mohitgupta-omg/Kaggle-SMS-Spam-Collection-Dataset-/master/spam.csv", filename="spam.csv") data = pd.read_csv('spam.csv', encoding='latin-1') data[:5] ์—ฌ๊ธฐ์„œ ํ•„์š”ํ•œ ๊ฑด v2 ์—ด๊ณผ v1์—ด์ž…๋‹ˆ๋‹ค. v1์—ด์€ ์ˆซ์ž ๋ ˆ์ด๋ธ”๋กœ ๋ฐ”๊ฟ”์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐ๊ฐ X_data์™€ y_data๋กœ ์ €์žฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. data['v1'] = data['v1'].replace(['ham','spam'],[0,1]) y_data = list(data['v1']) X_data = list(data['v2']) v2์—ด์„ X_data์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. v1 ์—ด์— ์žˆ๋Š” ham๊ณผ spam ๋ ˆ์ด๋ธ”์„ ๊ฐ๊ฐ ์ˆซ์ž 0๊ณผ 1๋กœ ๋ฐ”๊พธ๊ณ  y_data์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ •์ƒ์ ์œผ๋กœ ์ €์žฅ๋˜์—ˆ๋Š”์ง€ ์ด๋ฅผ ๊ฐ๊ฐ 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. X_data[:5] ['Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...', 'Ok lar... Joking wif u oni...', "Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate) T&C's apply 08452810075over18's", 'U dun say so early hor... U c already then say...', "Nah I don't think he goes to usf, he lives around here though"] print(y_data[:5]) [0, 0, 1, 0, 0] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 8:2 ๋น„์œจ๋กœ ๋ถ„ํ• ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ทธ์ „์— ์ด๋ฅผ ์œ„ํ•ด ์ „์ฒด ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜์˜ 80%์™€ 20%๋Š” ๊ฐ๊ฐ ๋ช‡ ๊ฐœ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print(len(X_data)) n_of_train = int(len(X_data) * 0.8) n_of_test = int(len(X_data) - n_of_train) print(n_of_train) print(n_of_test) 5572 4457 1115 ์ „์ฒด ๋ฐ์ดํ„ฐ๋Š” 5,572๊ฐœ์ด๋ฉฐ 8:2๋กœ ๋น„์œจ ํ•˜๋ฉด ๊ฐ๊ฐ 4,457๊ณผ 1,115๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐ๊ฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์–‘์œผ๋กœ ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„ํ• ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. X_train = np.asarray(X_data[:n_of_train]) #X_data ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์•ž์˜ 4457๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅ y_train = np.asarray(y_data[:n_of_train]) #y_data ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์•ž์˜ 4457๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅ X_test = np.asarray(X_data[n_of_train:]) #X_data ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ๋’ค์˜ 1115๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅ y_test = np.asarray(y_data[n_of_train:]) #y_data ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ๋’ค์˜ 1115๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅ ์ด์ œ ํ›ˆ๋ จ์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์ค€๋น„๋Š” ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ด์ œ ELMo์™€ ์„ค๊ณ„ํ•œ ๋ชจ๋ธ์„ ์—ฐ๊ฒฐํ•˜๋Š” ์ž‘์—…๋“ค์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ELMo๋Š” ํ…์„œ ํ”Œ๋กœ ํ—ˆ๋ธŒ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜จ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ผ€๋ผ์Šค์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ผ€๋ผ์Šค์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณ€ํ™˜ํ•ด ์ฃผ๋Š” ์ž‘์—…๋“ค์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. def ELMoEmbedding(x): return elmo(tf.squeeze(tf.cast(x, tf.string)), as_dict=True, signature="default")["default"] # ๋ฐ์ดํ„ฐ์˜ ์ด๋™์ด ์ผ€๋ผ์Šค โ†’ ํ…์„œ ํ”Œ๋กœ โ†’ ์ผ€๋ผ์Šค๊ฐ€ ๋˜๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜ ์ด์ œ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. from keras.models import Model from keras.layers import Dense, Lambda, Input input_text = Input(shape=(1, ), dtype=tf.string) embedding_layer = Lambda(ELMoEmbedding, output_shape=(1024, ))(input_text) hidden_layer = Dense(256, activation='relu')(embedding_layer) output_layer = Dense(1, activation='sigmoid')(hidden_layer) model = Model(inputs=[input_text], outputs=output_layer) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) ๋ชจ๋ธ์€ ELMo๋ฅผ ์ด์šฉํ•œ ์ž„๋ฒ ๋”ฉ ์ธต์„ ๊ฑฐ์ณ์„œ 256๊ฐœ์˜ ๋‰ด๋Ÿฐ์ด ์žˆ๋Š” ์€๋‹‰์ธต์„ ๊ฑฐ์นœ ํ›„ ๋งˆ์ง€๋ง‰ 1๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ํ†ตํ•ด ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ๋งˆ์ง€๋ง‰ ๋‰ด๋Ÿฐ์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์ด๋ฉฐ, ๋ชจ๋ธ์˜ ์†์‹ค ํ•จ์ˆ˜๋Š” binary_crossentropy์ž…๋‹ˆ๋‹ค. history = model.fit(X_train, y_train, epochs=1, batch_size=60) Epoch 1/1 4457/4457 [==============================] - 1508s 338ms/step - loss: 0.1129 - acc: 0.9619 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ๋Š” ์ •ํ™•๋„ 96%๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ‰๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (model.evaluate(X_test, y_test)[1])) 1115/1115 [==============================] - 381s 342ms/step ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.9803 1๋ฒˆ์˜ ์—ํฌํฌ์—์„œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์ •ํ™•๋„ 98%๋ฅผ ์–ป์–ด๋ƒ…๋‹ˆ๋‹ค. 09-10 ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์‹œ๊ฐํ™”(Embedding Visualization) ๊ตฌ๊ธ€์€ ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ(embedding projector)๋ผ๋Š” ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” ๋„๊ตฌ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šตํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ์‹œ๊ฐํ™”ํ•ด๋ด…์‹œ๋‹ค. ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ ๋…ผ๋ฌธ : https://arxiv.org/pdf/1611.05469v1.pdf 1. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ 2๊ฐœ์˜ tsv ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ ํ•™์Šตํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•ด์„œ๋Š” ์ด๋ฏธ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ณ , ํŒŒ์ผ๋กœ ์ €์žฅ๋ผ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ €์žฅ๋ผ ์žˆ๋‹ค๋ฉด ์•„๋ž˜ ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ์‹œ๊ฐํ™”์— ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. !python -m gensim.scripts.word2vec2tensor --input ๋ชจ๋ธ ์ด๋ฆ„ --output ๋ชจ๋ธ ์ด๋ฆ„ ์—ฌ๊ธฐ์„œ๋Š” ํŽธ์˜๋ฅผ ์œ„ํ•ด ์ด์ „ ์ฑ•ํ„ฐ์—์„œ ํ•™์Šตํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ์‹ค์Šต๊นŒ์ง€ ์ง„ํ–‰ํ–ˆ๋˜ ์˜์–ด Word2Vec ๋ชจ๋ธ์ธ 'eng_w2v'๋ฅผ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. eng_w2v๋ผ๋Š” Word2Vec ๋ชจ๋ธ์ด ์ด๋ฏธ ์กด์žฌํ•œ๋‹ค๋Š” ๊ฐ€์ • ํ•˜์— ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ ์•„๋ž˜ ์ปค๋งจ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. !python -m gensim.scripts.word2vec2tensor --input eng_w2v --output eng_w2v ์ปค๋งจ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์ด ์‹œ์ž‘๋˜๋Š” ๊ฒฝ๋กœ์— ๊ธฐ์กด์— ์žˆ๋˜ eng_w2v ์™ธ์—๋„ ๋‘ ๊ฐœ์˜ ํŒŒ์ผ์ด ์ƒ๊น๋‹ˆ๋‹ค. ์ƒˆ๋กœ ์ƒ๊ธด eng_w2v_metadata.tsv์™€ eng_w2v_tensor.tsv ์ด ๋‘ ๊ฐœ ํŒŒ์ผ์ด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•  ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ eng_w2v ๋ชจ๋ธ ํŒŒ์ผ์ด ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋ชจ๋ธ ํŒŒ์ผ ์ด๋ฆ„์œผ๋กœ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, '๋ชจ๋ธ ์ด๋ฆ„_ metadata.tsv'์™€ '๋ชจ๋ธ ์ด๋ฆ„_ tensor.tsv'๋ผ๋Š” ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. 2. ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๊ฐํ™”ํ•˜๊ธฐ ๊ตฌ๊ธ€์˜ ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ์„ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋งํฌ์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://projector.tensorflow.org/ ์‚ฌ์ดํŠธ์— ์ ‘์†ํ•ด์„œ ์ขŒ์ธก ์ƒ๋‹จ์„ ๋ณด๋ฉด Load๋ผ๋Š” ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Load๋ผ๋Š” ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฐฝ์ด ๋œจ๋Š”๋ฐ ์ด ๋‘ ๊ฐœ์˜ Choose file ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์— ์žˆ๋Š” Choose file ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  eng_w2v_tensor.tsv ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜๊ณ , ์•„๋ž˜์— ์žˆ๋Š” Choose file ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  eng_w2v_metadata.tsv ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๋‘ ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜๋ฉด ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ์— ํ•™์Šตํ–ˆ๋˜ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ์ด ์‹œ๊ฐํ™”๋ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„์—๋Š” ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ์˜ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ๋Š” ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจ์›์„ ์ถ•์†Œํ•˜์—ฌ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” PCA, t-SNE ๋“ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ 'man'์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์„ ํƒํ•˜๊ณ , ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์ƒ์œ„ 10๊ฐœ ๋ฒกํ„ฐ๋“ค์„ ํ‘œ์‹œํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. 09-11 ๋ฌธ์„œ ๋ฒกํ„ฐ๋ฅผ ์ด์šฉํ•œ ์ถ”์ฒœ ์‹œ์Šคํ…œ(Recommendation System using Document Embedding) ๋ฌธ์„œ๋“ค์„ ๊ณ ์ •๋œ ๊ธธ์ด์˜ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค๋ฉด ๋ฒกํ„ฐ ๊ฐ„ ๋น„๊ต๋กœ ๋ฌธ์„œ๋“ค์„ ์„œ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฌธ์„œ๋ฅผ ๋ฌธ์„œ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด๋ฏธ ๊ตฌํ˜„๋œ ํŒจํ‚ค์ง€์ธ Doc2Vec์ด๋‚˜ Sent2Vec ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์กด์žฌํ•˜์ง€๋งŒ, ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์–ป์€ ๋’ค ๋ฌธ์„œ์— ์กด์žฌํ•˜๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์„ ๋ฌธ์„œ ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฌธ์„œ ๋‚ด ๊ฐ ๋‹จ์–ด๋“ค์„ Word2Vec์„ ํ†ตํ•ด ๋‹จ์–ด ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ์ด๋“ค์˜ ํ‰๊ท ์œผ๋กœ ๋ฌธ์„œ ๋ฒกํ„ฐ๋ฅผ ์–ป์–ด ์„ ํ˜ธํ•˜๋Š” ๋„์„œ์™€ ์œ ์‚ฌํ•œ ๋„์„œ๋ฅผ ์ฐพ์•„์ฃผ๋Š” ๋„์„œ ์ถ”์ฒœ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ์ด๋ฒˆ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ์ฑ…์˜ ์ด๋ฏธ์ง€์™€ ์ฑ…์˜ ์ค„๊ฑฐ๋ฆฌ๋ฅผ ํฌ๋กค๋ง ํ•œ ๋ฐ์ดํ„ฐ๋กœ ์•„๋ž˜์˜ ๋งํฌ์—์„œ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://drive.google.com/file/d/15Q7DZ7xrJsI2Hji-WbkU9j1mwnODBd5A/view? usp=sharing # ํ˜„์žฌ ์ฝ”๋“œ๊ฐ€ gensim 3.6.0 ๋ฒ„์ „์—์„œ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. import urllib.request import pandas as pd import numpy as np import matplotlib.pyplot as plt import requests import re from PIL import Image from io import BytesIO from nltk.tokenize import RegexpTokenizer import nltk from gensim.models import Word2Vec from gensim.models import KeyedVectors from nltk.corpus import stopwords from sklearn.metrics.pairwise import cosine_similarity ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•˜๊ณ  ์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/09.%20Word%20Embedding/dataset/data.csv", filename="data.csv") df = pd.read_csv("data.csv") print('์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜ :',len(df)) ์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜ : 2382 ์ƒ์œ„ 5๊ฐœ์˜ ํ–‰๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. df[:5] ๋ถˆํ•„์š”ํ•œ ์—ด๋“ค์ด ์กด์žฌํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ์ค„๊ฑฐ๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” ์—ด์ธ 'Desc ์—ด'์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์—ด์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ Word2Vec์„ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์—ด์— ๋Œ€ํ•ด์„œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  'cleaned'๋ผ๋Š” ์—ด์— ์ €์žฅํ•ด ๋ด…์‹œ๋‹ค. def _removeNonAscii(s): return "".join(i for i in s if ord(i)<128) def make_lower_case(text): return text.lower() def remove_stop_words(text): text = text.split() stops = set(stopwords.words("english")) text = [w for w in text if not w in stops] text = " ".join(text) return text def remove_html(text): html_pattern = re.compile('<.*?>') return html_pattern.sub(r'', text) def remove_punctuation(text): tokenizer = RegexpTokenizer(r'[a-zA-Z]+') text = tokenizer.tokenize(text) text = " ".join(text) return text df['cleaned'] = df['Desc'].apply(_removeNonAscii) df['cleaned'] = df.cleaned.apply(make_lower_case) df['cleaned'] = df.cleaned.apply(remove_stop_words) df['cleaned'] = df.cleaned.apply(remove_punctuation) df['cleaned'] = df.cleaned.apply(remove_html) ์ƒ์œ„ 5๊ฐœ์˜ ํ–‰๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. df['cleaned'][:5] 0 know power shifting west east north south pres... 1 following success accidental billionaires mone... 2 tap power social software networks build busin... 3 william j bernstein american financial theoris... 4 amazing book joined steve jobs many akio morit... Name: cleaned, dtype: object ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ ๋นˆ ๊ฐ’์ด ์ƒ๊ธด ํ–‰์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋นˆ ๊ฐ’์ด ์ƒ๊ธด ํ–‰์ด ์žˆ๋‹ค๋ฉด, nan ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ ํ›„์— ํ•ด๋‹น ํ–‰์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. df['cleaned'].replace('', np.nan, inplace=True) df = df[df['cleaned'].notna()] print('์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜ :',len(df)) ์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜ : 2381 ์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜๊ฐ€ 1๊ฐœ ์ค„์–ด๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ corpus๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ corpus๋ฅผ ํ†ตํ•ด Word2Vec์„ ํ›ˆ๋ จํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. corpus = [] for words in df['cleaned']: corpus.append(words.split()) 2. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์‚ฌ์šฉํ•˜๊ธฐ Word2Vec์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๋ฐ์ดํ„ฐ๊ฐ€ ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์€ ์ƒํ™ฉ์—์„œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ดˆ๊นƒ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2Vec์„ ๋กœ๋“œํ•˜๊ณ  ์ดˆ๊ธฐ ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz", \ filename="GoogleNews-vectors-negative300.bin.gz") word2vec_model = Word2Vec(size = 300, window=5, min_count = 2, workers = -1) word2vec_model.build_vocab(corpus) word2vec_model.intersect_word2vec_format('GoogleNews-vectors-negative300.bin.gz', lockf=1.0, binary=True) word2vec_model.train(corpus, total_examples = word2vec_model.corpus_count, epochs = 15) 3. ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ํ‰๊ท  ๊ตฌํ•˜๊ธฐ ๊ฐ ๋ฌธ์„œ์— ์กด์žฌํ•˜๋Š” ๋‹จ์–ด๋“ค์˜ ๋ฒกํ„ฐ ๊ฐ’์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ ํ•ด๋‹น ๋ฌธ์„œ์˜ ๋ฒกํ„ฐ ๊ฐ’์„ ์—ฐ์‚ฐํ•ด ๋ด…์‹œ๋‹ค. def get_document_vectors(document_list): document_embedding_list = [] # ๊ฐ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ for line in document_list: doc2vec = None count = 0 for word in line.split(): if word in word2vec_model.wv.vocab: count += 1 # ํ•ด๋‹น ๋ฌธ์„œ์— ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋“ค์˜ ๋ฒกํ„ฐ ๊ฐ’์„ ๋”ํ•œ๋‹ค. if doc2vec is None: doc2vec = word2vec_model[word] else: doc2vec = doc2vec + word2vec_model[word] if doc2vec is not None: # ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๋ฒกํ„ฐ์˜ ๊ฐ’์„ ๋ฌธ์„œ ๊ธธ์ด๋กœ ๋‚˜๋ˆ ์ค€๋‹ค. doc2vec = doc2vec / count document_embedding_list.append(doc2vec) # ๊ฐ ๋ฌธ์„œ์— ๋Œ€ํ•œ ๋ฌธ์„œ ๋ฒกํ„ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ด return document_embedding_list document_embedding_list = get_document_vectors(df['cleaned']) print('๋ฌธ์„œ ๋ฒกํ„ฐ์˜ ์ˆ˜ :',len(document_embedding_list)) ๋ฌธ์„œ ๋ฒกํ„ฐ์˜ ์ˆ˜ : 2381 4. ์ถ”์ฒœ ์‹œ์Šคํ…œ ๊ตฌํ˜„ํ•˜๊ธฐ ๊ฐ ๋ฌธ์„œ ๋ฒกํ„ฐ ๊ฐ„์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. cosine_similarities = cosine_similarity(document_embedding_list, document_embedding_list) print('์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๋งคํŠธ๋ฆญ์Šค์˜ ํฌ๊ธฐ :',cosine_similarities.shape) ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๋งคํŠธ๋ฆญ์Šค์˜ ํฌ๊ธฐ : (2381, 2381) ์„ ํƒํ•œ ์ฑ…์— ๋Œ€ํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€์žฅ ์ค„๊ฑฐ๋ฆฌ๊ฐ€ ์œ ์‚ฌํ•œ 5๊ฐœ์˜ ์ฑ…์„ ์ฐพ์•„๋‚ด๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. def recommendations(title): books = df[['title', 'image_link']] # ์ฑ…์˜ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ์ œ๋ชฉ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ด ๋ฐ›์•„ idx์— ์ €์žฅ. indices = pd.Series(df.index, index = df['title']).drop_duplicates() idx = indices[title] # ์ž…๋ ฅ๋œ ์ฑ…๊ณผ ์ค„๊ฑฐ๋ฆฌ(document embedding)๊ฐ€ ์œ ์‚ฌํ•œ ์ฑ… 5๊ฐœ ์„ ์ •. sim_scores = list(enumerate(cosine_similarities[idx])) sim_scores = sorted(sim_scores, key = lambda x: x[1], reverse = True) sim_scores = sim_scores[1:6] # ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์ฑ… 5๊ถŒ์˜ ์ธ๋ฑ์Šค book_indices = [i[0] for i in sim_scores] # ์ „์ฒด ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์—์„œ ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ํ–‰๋งŒ ์ถ”์ถœ. 5๊ฐœ์˜ ํ–‰์„ ๊ฐ€์ง„๋‹ค. recommend = books.iloc[book_indices].reset_index(drop=True) fig = plt.figure(figsize=(20, 30)) # ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅ for index, row in recommend.iterrows(): response = requests.get(row['image_link']) img = Image.open(BytesIO(response.content)) fig.add_subplot(1, 5, index + 1) plt.imshow(img) plt.title(row['title']) ์ข‹์•„ํ•˜๋Š” ์ฑ… ์ œ๋ชฉ์„ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น ์ฑ…์˜ ๋ฌธ์„œ ๋ฒกํ„ฐ(์ค„๊ฑฐ๋ฆฌ ๋ฒกํ„ฐ)์™€ ์œ ์‚ฌํ•œ ๋ฌธ์„œ ๋ฒกํ„ฐ ๊ฐ’์„ ๊ฐ€์ง„ ์ฑ…๋“ค์„ ์ถ”์ฒœํ•ด ์ค๋‹ˆ๋‹ค. ์ฑ… ์ œ๋ชฉ๊ณผ ํ‘œ์ง€๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. recommendations("The Da Vinci Code") ๋‹ค๋นˆ์น˜ ์ฝ”๋“œ๋Š” ์ž‘๊ฐ€ ๋Œ„ ๋ธŒ๋ผ์šด์˜ ์ž‘ํ’ˆ์ž…๋‹ˆ๋‹ค. ์ถ”์ฒœ๋˜๋Š” ์ž‘ํ’ˆ๋“ค ๋˜ํ•œ 5๊ฐœ ์ค‘ 3๊ฐœ๊ฐ€ ๋Œ„ ๋ธŒ๋ผ์šด์˜ ์ž‘ํ’ˆ๋“ค์ด ์ถ”์ฒœ๋จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์•„๊ฐ€์‚ฌ ํฌ๋ฆฌ์Šคํ‹ฐ์˜ ์• ํฌ๋กœ์ด๋“œ ์‚ด์ธ์‚ฌ๊ฑด๊ณผ ์œ ์‚ฌํ•œ ๋„์„œ๋ฅผ ์ถ”์ฒœ๋ฐ›์•„ ๋ด…์‹œ๋‹ค. recommendations("The Murder of Roger Ackroyd") ์• ํฌ๋กœ์ด๋“œ ์‚ด์ธ์‚ฌ๊ฑด์€ ๋ฏธ์Šคํ„ฐ๋ฆฌ ์Šค๋ฆด๋Ÿฌ ์†Œ์„ค์ธ๋ฐ ์ด์™€ ์œ ์‚ฌํ•œ ์†Œ์„ค๋“ค์ด ์ถ”์ฒœ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 09-12 ๋ฌธ์„œ ์ž„๋ฒ ๋”ฉ : ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์˜ ํ‰๊ท (Average Word Embedding) ์•ž์„œ ํŠน์ • ๋ฌธ์žฅ ๋‚ด์˜ ๋‹จ์–ด๋“ค์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์ด ๊ทธ ๋ฌธ์žฅ์˜ ๋ฒกํ„ฐ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ์„ ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ž„๋ฒ ๋”ฉ์ด ์ž˜ ๋œ ์ƒํ™ฉ์—์„œ๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ๋งŒ์œผ๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ณด์ด๊ณ , ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์˜ ์ค‘์š”์„ฑ์„ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ํ™” ์‚ฌ์ดํŠธ IMDB ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋Š” ๋ฆฌ๋ทฐ ํ…์ŠคํŠธ์— ๋ฆฌ๋ทฐ๊ฐ€ ๊ธ์ •์ธ ๊ฒฝ์šฐ 1์„, ๋ถ€์ •์ธ ๊ฒฝ์šฐ 0์œผ๋กœ ๋ ˆ์ด๋ธ”๋ง ํ•œ ๋ฐ์ดํ„ฐ๋กœ 25,000๊ฐœ์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ 25,000๊ฐœ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค๋ฅผ ํ†ตํ•ด์„œ ์ด ๋ฐ์ดํ„ฐ ์…‹์„ ๋ฐ”๋กœ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ƒ์„ธ ์„ค๋ช…์€ ๋’ค์˜ RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ฑ•ํ„ฐ์—์„œ IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฐ์ดํ„ฐ๋‚˜ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์„ค๋ช…๋ณด๋‹ค๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ํ‰๊ท ์œผ๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์„ฑ๋Šฅ์— ์ฃผ๋ชฉํ•ด ์ฃผ์„ธ์š”. 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ์™€ ์ „์ฒ˜๋ฆฌ import numpy as np from tensorflow.keras.datasets import imdb from tensorflow.keras.preprocessing.sequence import pad_sequences ์ผ€๋ผ์Šค์—์„œ๋Š” imdb.data_load()๋ฅผ ํ†ตํ•ด์„œ IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•  ๋•Œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ num_words๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ์œ„๋กœ ๋ช‡ ๋ฒˆ์งธ์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๊นŒ์ง€๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ vocab_size๋ฅผ 20,000์œผ๋กœ ์ง€์ •ํ•  ๊ฒฝ์šฐ ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ์œ„๊ฐ€ 20,000๋“ฑ์ด ๋„˜๋Š” ๋‹จ์–ด๋“ค์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•  ๋•Œ ์ „๋ถ€ ์ œ๊ฑฐ ํ›„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. vocab_size = 20000 (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=vocab_size) print('ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(X_train)) print('ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(X_test)) ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 25000 ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 25000 ์ด ๋ฐ์ดํ„ฐ๋Š” ์ด๋ฏธ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊นŒ์ง€์˜ ์ „์ฒ˜๋ฆฌ๊ฐ€ ์ง„ํ–‰๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค๊ณ , ๊ฐ ๋‹จ์–ด๋ฅผ ์ •์ˆ˜๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๊ณผ์ •์„ ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฆฌ๋ทฐ์™€ ์ฒซ ๋ฒˆ์งธ ๋ฆฌ๋ทฐ์˜ ๋ ˆ์ด๋ธ”์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :',X_train[0]) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” :'y_train[0]) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : [1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941, 4, 173, 36, 256, 5, 25, 100, 43, 838, 112, 50, 670, 2, 9, 35, 480, 284, 5, 150, 4, 172, 112, 167, 2, 336, 385, 39, 4, 172, 4536, 1111, 17, 546, 38, 13, 447, 4, 192, 50, 16, 6, 147, 2025, 19, 14, 22, 4, 1920, 4613, 469, 4, 22, 71, 87, 12, 16, 43, 530, 38, 76, 15, 13, 1247, 4, 22, 17, 515, 17, 12, 16, 626, 18, 19193, 5, 62, 386, 12, 8, 316, 8, 106, 5, 4, 2223, 5244, 16, 480, 66, 3785, 33, 4, 130, 12, 16, 38, 619, 5, 25, 124, 51, 36, 135, 48, 25, 1415, 33, 6, 22, 12, 215, 28, 77, 52, 5, 14, 407, 16, 82, 10311, 8, 4, 107, 117, 5952, 15, 256, 4, 2, 7, 3766, 5, 723, 36, 71, 43, 530, 476, 26, 400, 317, 46, 7, 4, 12118, 1029, 13, 104, 88, 4, 381, 15, 297, 98, 32, 2071, 56, 26, 141, 6, 194, 7486, 18, 4, 226, 22, 21, 134, 476, 26, 480, 5, 144, 30, 5535, 18, 51, 36, 28, 224, 92, 25, 104, 4, 226, 65, 16, 38, 1334, 88, 12, 16, 283, 5, 16, 4472, 113, 103, 32, 15, 16, 5345, 19, 178, 32] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : 1 ์ •์ˆ˜ 1์ด ์ถœ๋ ฅ๋˜๋Š”๋ฐ ์ด๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฆฌ๋ทฐ๊ฐ€ ๊ธ์ • ๋ฆฌ๋ทฐ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ํ‰ ๊ทœ ๊ธธ์ด: {}'.format(np.mean(list(map(len, X_train)), dtype=int))) print('ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด: {}'.format(np.mean(list(map(len, X_test)), dtype=int))) ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ํ‰ ๊ทœ ๊ธธ์ด: 238 ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด: 230 ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์™€ ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด๊ฐ€ ๊ฐ๊ฐ 238, 230์ž…๋‹ˆ๋‹ค. ํ‰๊ท ๋ณด๋‹ค๋Š” ํฐ ์ˆ˜์น˜์ธ 400์œผ๋กœ ํŒจ๋”ฉ ํ•ฉ๋‹ˆ๋‹ค. max_len = 400 X_train = pad_sequences(X_train, maxlen=max_len) X_test = pad_sequences(X_test, maxlen=max_len) print('X_train์˜ ํฌ๊ธฐ(shape) :', X_train.shape) print('X_test์˜ ํฌ๊ธฐ(shape) :', X_test.shape) X_train shape : (25000, 400) X_test shape : (25000, 400) 2. ๋ชจ๋ธ ์„ค๊ณ„ํ•˜๊ธฐ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ชจ๋“  ์ „์ฒ˜๋ฆฌ๋ฅผ ๋งˆ์ณค์Šต๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ํ‰๊ท ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ด…์‹œ๋‹ค. GlobalAveragePooling1D()์€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. Embedding() ๋‹ค์Œ์— GlobalAveragePooling1D()์„ ์ถ”๊ฐ€ํ•˜๋ฉด ํ•ด๋‹น ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท  ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๊ทธ ํ›„์—๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋‰ด๋Ÿฐ 1๊ฐœ๋ฅผ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 20%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์ด 10 ์—ํฌํฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential, load_model from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint embedding_dim = 64 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) # ๋ชจ๋“  ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค. model.add(GlobalAveragePooling1D()) model.add(Dense(1, activation='sigmoid')) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('embedding_average_model.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc']) model.fit(X_train, y_train, batch_size=32, epochs=10, callbacks=[es, mc], validation_split=0.2) ํ•™์Šต์ด ๋๋‚œ ํ›„์— ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. loaded_model = load_model('embedding_average_model.h5') print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (loaded_model.evaluate(X_test, y_test)[1])) ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.8876 ๋ณ„๋‹ค๋ฅธ ์‹ ๊ฒฝ๋ง์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ  ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ํ‰๊ท ๋งŒ์œผ๋กœ๋„ 88.76%๋ผ๋Š” ์ค€์ˆ˜ํ•œ ์ •ํ™•๋„๋ฅผ ์–ป์–ด๋ƒ…๋‹ˆ๋‹ค. 09-13 Doc2Vec์œผ๋กœ ๊ณต์‹œ ์‚ฌ์—…๋ณด๊ณ ์„œ ์œ ์‚ฌ๋„ ๊ณ„์‚ฐํ•˜๊ธฐ Word2Vec์€ ๋‹จ์–ด๋ฅผ ์ž„๋ฒ ๋”ฉํ•˜๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์—ˆ์Šต๋‹ˆ๋‹ค. Doc2Vec์€ Word2Vec์„ ๋ณ€ํ˜•ํ•˜์—ฌ ๋ฌธ์„œ์˜ ์ž„๋ฒ ๋”ฉ์„ ์–ป์„ ์ˆ˜ ์žˆ๋„๋ก ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฌธ ์ œ๋ชฉ๊ณผ ๋…ผ๋ฌธ์˜ ๋งํฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฌธ ์ œ๋ชฉ : Distributed Representations of Sentences and Documents ๋…ผ๋ฌธ ๋งํฌ : https://arxiv.org/abs/1405.4053 Word2Vec๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํŒŒ์ด์ฌ ๋จธ์‹  ๋Ÿฌ๋‹ ํŒจํ‚ค์ง€์ธ Gensim์„ ํ†ตํ•ด์„œ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์ €์ž๊ฐ€ ์ˆ˜์ง‘ํ•ด๋†“์€ ์ „์ž๊ณต์‹œ์‹œ์Šคํ…œ(Dart)์— ์˜ฌ๋ผ์™€ ์žˆ๋Š” ๊ฐ ํšŒ์‚ฌ์˜ ์‚ฌ์—…๋ณด๊ณ ์„œ๋ฅผ Doc2Vec์„ ํ†ตํ•ด์„œ ํ•™์Šต์‹œํ‚ค๊ณ , ํŠน์ • ํšŒ์‚ฌ์™€ ์‚ฌ์—… ๋ณด๊ณ ์„œ๊ฐ€ ์œ ์‚ฌํ•œ ํšŒ์‚ฌ๋“ค์„ ์ฐพ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๊ณต์‹œ ์‚ฌ์—… ๋ณด๊ณ ์„œ ๋กœ๋“œ ๋ฐ ์ „์ฒ˜๋ฆฌ ํ•ด๋‹น ์‹ค์Šต์€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์˜ ์›ํ™œํ•œ ์„ค์น˜๋ฅผ ์œ„ํ•ด์„œ ๊ตฌ๊ธ€์˜ Colab์—์„œ ์ง„ํ–‰ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„๋“ค์˜ ์ปดํ“จํ„ฐ์— Mecab์„ ์„ค์น˜ํ•˜์˜€๊ฑฐ๋‚˜, ๋‹ค๋ฅธ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Colab์—์„œ ํ•˜์ง€ ์•Š๋”๋ผ๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. # dart.csv ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ !wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc? export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1XS0UlE8gNNTRjnL6e64sMacOhtVERIqL' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1XS0UlE8gNNTRjnL6e64sMacOhtVERIqL" -O dart.csv && rm -rf /tmp/cookies.txt # ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab ์„ค์น˜ !pip install konlpy !pip install mecab-python !bash <(curl -s https://raw.githubusercontent.com/konlpy/konlpy/master/scripts/mecab.sh) import pandas as pd from konlpy.tag import Mecab from gensim.models.doc2vec import TaggedDocument from tqdm import tqdm dart.csv ํŒŒ์ผ์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฒฐ์ธก๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์„ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. df = pd.read_csv('/content/dart.csv', sep=',') df = df.dropna() df ์ด 2,295๊ฐœ์˜ ํ–‰๊ณผ 4๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์—ด์€ ์ข…๋ชฉ ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” code ์—ด, ๋‘ ๋ฒˆ์งธ ์—ด์€ ํ•ด๋‹น ์ข…๋ชฉ์ด KOSPI ์ธ์ง€ KOSDAQ ์ธ์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” market ์—ด, ์„ธ ๋ฒˆ์งธ ์—ด์€ ํšŒ์‚ฌ๋ช…์— ํ•ด๋‹นํ•˜๋Š” name ์—ด, ๊ทธ๋ฆฌ๊ณ  ๋„ค ๋ฒˆ์งธ business ์—ด์€ ์šฐ๋ฆฌ๊ฐ€ ํ•™์Šตํ•  ์‚ฌ์—… ๋ณด๊ณ ์„œ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ด์ œ business ์—ด์— ๋Œ€ํ•ด์„œ ํ˜•ํƒœ์†Œ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์ด์™€ ๋™์‹œ์— Doc2Vec ํ•™์Šต์„ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ<NAME>์œผ๋กœ ๋ฐ์ดํ„ฐ์˜<NAME>์„ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. Doc2Vec ํ•™์Šต์„ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ๋ฌธ์„œ์˜ '์ œ๋ชฉ'๊ณผ ๋‹จ์–ด ํ† ํฐ ํ™”๊ฐ€ ๋œ ์ƒํƒœ์˜ ํ•ด๋‹น ๋ฌธ์„œ์˜ '๋ณธ๋ฌธ' ๋‘ ๊ฐ€์ง€๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. TaggedDocument์˜ tags์— ํ•ด๋‹น ๋ฌธ์„œ์˜ '์ œ๋ชฉ'์„, ๊ทธ๋ฆฌ๊ณ  words์— ํ•ด๋‹น ๋ฌธ์„œ์˜ '๋ณธ๋ฌธ'์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด ํ† ํฐํ™” ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๊ณ , ์ด ๊ฒฐ๊ณผ๋ฅผ ์›์†Œ๋กœ ํ•˜๋Š” ํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ์ธ tagged_corpus_list๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. mecab = Mecab() tagged_corpus_list = [] for index, row in tqdm(df.iterrows(), total=len(df)): text = row['business'] tag = row['name'] tagged_corpus_list.append(TaggedDocument(tags=[tag], words=mecab.morphs(text))) print('๋ฌธ์„œ์˜ ์ˆ˜ :', len(tagged_corpus_list)) ๋ฌธ์„œ์˜ ์ˆ˜ : 2295 ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ์˜ ์ „์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. tagged_corpus_list[0] TaggedDocument(words=['II', '.', '์‚ฌ์—…', '์˜', '๋‚ด์šฉ', '1', '.', '์‚ฌ์—…', '์˜', '๊ฐœ์š”', '๊ฐ€', '.', '์ผ๋ฐ˜', '์ ', '์ธ', '์‚ฌํ•ญ', '๊ธฐ์—…', 'ํšŒ๊ณ„', '๊ธฐ์ค€', '์„œ', '์ œ', '1110', 'ํ˜ธ', '"', '์—ฐ๊ฒฐ', '์žฌ๋ฌด์ œํ‘œ', '"', '์˜', '์˜ํ•˜', '์—ฌ', '2018', '๋…„', '12', '์›”', '17', '์ผ', '์—', '์„ค๋ฆฝ', 'ํ•œ', '๋™ํ™”', 'ํฌ๋ฆฝํ†ค', '๊ธฐ์—…๊ฐ€', '์ •์‹ ', '์ œ์ผ', 'ํ˜ธ', '์ฐฝ์—…', '๋ฒค์ฒ˜', '์ „๋ฌธ', '์‚ฌ๋ชจ', 'ํˆฌ์ž', 'ํ•ฉ์žํšŒ์‚ฌ', '๋ฅผ', '์ข…์†', 'ํšŒ์‚ฌ', '์—', 'ํŽธ์ž…', 'ํ•˜', '์˜€', '์Šต๋‹ˆ๋‹ค', ... ์ค‘๋žต ... '๋Œ€๊ธฐ', '๊ด€๋ฆฌ', '๊ถŒ', '์—ญ', '์˜', '๋Œ€๊ธฐ', 'ํ™˜๊ฒฝ', '๊ฐœ์„ ', '์—', '๊ด€ํ•œ', 'ํŠน๋ณ„๋ฒ•', '์„', '์ค€', '์ˆ˜', 'ํ•˜', '๊ณ ', '์žˆ', '์Šต๋‹ˆ๋‹ค', '.'], tags=['๋™ํ™”์•ฝํ’ˆ']) TaggedDocument ์•ˆ words์—๋Š” ํ† ํฐํ™”๋œ ์‚ฌ์—… ๋ณด๊ณ ์„œ, tags์—๋Š” ํ•ด๋‹น ๋ฌธ์„œ์˜ ์ œ๋ชฉ์ด ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. 2. Doc2Vec ํ•™์Šต ๋ฐ ํ…Œ์ŠคํŠธ ์ด์ œ ๋ชจ๋ธ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์—… ๋ณด๊ณ ์„œ๊ฐ€ ๊ฝค ๊ธด ๋ฌธ์„œ์ธ๋ฐ๋‹ค ํ† ํฐํ™” ์™ธ์— ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ๋“ฑ ๋ณ„๋„ ์ถ”๊ฐ€ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ €์ž๊ฐ€ Colab์—์„œ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ, ์†Œ์š” ์‹œ๊ฐ„์ด ์ตœ์†Œ 1์‹œ๊ฐ„ ์ด์ƒ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. from gensim.models import doc2vec model = doc2vec.Doc2Vec(vector_size=300, alpha=0.025, min_alpha=0.025, workers=8, window=8) # Vocabulary ๋นŒ๋“œ model.build_vocab(tagged_corpus_list) print(f"Tag Size: {len(model.docvecs.doctags.keys())}", end=' / ') # Doc2Vec ํ•™์Šต model.train(tagged_corpus_list, total_examples=model.corpus_count, epochs=50) # ๋ชจ๋ธ ์ €์žฅ model.save('dart.doc2vec') ์ฝ”๋“œ๋ฅผ ๋‹ค ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚˜๋ฉด 3๊ฐœ์˜ ํŒŒ์ผ์ด ์ƒ๊น๋‹ˆ๋‹ค. dart.doc2vec dart.doc2vec.trainables.syn1neg.npy dart.doc2vec.wv.vectors.npy ์ด์ œ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•ด ๋ด…์‹œ๋‹ค. ํšŒ์‚ฌ ๋™ํ™”์•ฝํ’ˆ๊ณผ ์‚ฌ์—… ๋ณด๊ณ ์„œ๊ฐ€ ์œ ์‚ฌํ•œ ํšŒ์‚ฌ๋“ค์€ ์–ด๋””์ผ๊นŒ์š”? similar_doc = model.docvecs.most_similar('๋™ํ™”์•ฝํ’ˆ') print(similar_doc) [('์ข…๊ทผ๋‹น', 0.5310906171798706), ('์‚ผ์ผ์ œ์•ฝ', 0.5263979434967041), ('์ผ์–‘ ์•ฝํ’ˆ', 0.5260423421859741), ('์˜์ง„์•ฝํ’ˆ', 0.5254894495010376), ('์ œ์ผ์•ฝํ’ˆ', 0.5089458227157593), ('์œ ํ•œ์–‘ํ–‰', 0.5015101432800293), ('๊ตญ์ œ์•ฝํ’ˆ', 0.4985279440879822), ('์‚ผ์•„์ œ์•ฝ', 0.49677950143814087), ('๋™์•„์—์Šคํ‹ฐ', 0.49451446533203125), ('๋Œ€์›…์ œ์•ฝ', 0.48559868335723877)] ์ด ์™ธ์— ๋‹ค๋ฅธ ํ…Œ์ŠคํŠธ์—์„œ๋„ ๊ด€๋ จ ์—…์ข…์˜ ํšŒ์‚ฌ๋“ค์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. similar_doc = model.docvecs.most_similar('ํ•˜์ดํŠธ์ง„๋กœ') print(similar_doc) [('ํ•˜์ดํŠธ์ง„๋กœํ™€๋”ฉ์Šค', 0.8621307611465454), ('๋ฌดํ•™', 0.5214779376983643), ('๋ณดํ•ด์–‘์กฐ', 0.5100635290145874), ('๊ตญ์ˆœ๋‹น', 0.48447638750076294), ('๋กฏ๋ฐ์น ์„ฑ', 0.4617755115032196), ('๊ธˆ๋น„', 0.4156178832054138), ('์‚ผ์–‘ ํŒจํ‚ค์ง•', 0.40965551137924194), ('์‚ผ๊ด‘๊ธ€๋ผ์Šค', 0.40689679980278015), ('๊ฒฝ๋ฐฉ', 0.40154141187667847), ('์˜ค๋š์ด', 0.3958606719970703)] similar_doc = model.docvecs.most_similar('LG์ด๋…ธํ…') print(similar_doc) [('LG์ „์ž', 0.533338725566864), ('LG', 0.523799479007721), ('์‚ผ์„ฑ์ „๊ธฐ', 0.45796477794647217), ('LG๋””์Šคํ”Œ๋ ˆ์ด', 0.4485859274864197), ('์„œ์šธ๋ฐ˜๋„์ฒด', 0.42762115597724915), ('๋ฃจ๋ฉ˜์Šค', 0.42333459854125977), ('์‚ผ์„ฑSDI', 0.4111291170120239), ('ํ์— ์”จ', 0.409035325050354), ('์„œ์šธ๋ฐ”์ด์˜ค ์‹œ์Šค', 0.4087420105934143), ('์‚ผ์„ฑ๊ณต์กฐ', 0.4040142595767975)] similar_doc = model.docvecs.most_similar('๋ฉ”๋ฆฌ์ธ ํ™”์žฌ') print(similar_doc) [('๋ฉ”๋ฆฌ์ธ ๊ธˆ์œต ์ง€์ฃผ', 0.7080470323562622), ('ํ•œํ™”์†ํ•ด๋ณดํ—˜', 0.69782555103302), ('๋กฏ๋ฐ์†ํ•ด๋ณดํ—˜', 0.6945951581001282), ('DB์†ํ•ด๋ณดํ—˜', 0.6699072122573853), ('ํ•œํ™”์ƒ๋ช…', 0.665973961353302), ('ํฅ๊ตญํ™”์žฌ', 0.6471891403198242), ('ํ˜„๋Œ€ํ•ด์ƒ', 0.6267702579498291), ('์ฝ”๋ฆฌ์•ˆ๋ฆฌ', 0.5982924699783325), ('์‚ผ์„ฑํ™”์žฌ', 0.5873793959617615), ('๋™์–‘์ƒ๋ช…', 0.5722818970680237)] similar_doc = model.docvecs.most_similar('์นด์นด์˜ค') print(similar_doc) [('๋„ค์˜ค์œ„์ฆˆ', 0.5055375099182129), ('NAVER', 0.4846588373184204), ('๋„ค์˜ค์œ„์ฆˆ ํ™€๋”ฉ์Šค', 0.47819197177886963), ('ํ“จ์ฒ˜์ŠคํŠธ๋ฆผ๋„คํŠธ์›์Šค', 0.4654642939567566), ('์‹ ํ’์ œ์•ฝ ์šฐ', 0.46335992217063904), ('LG์ƒํ™œ๊ฑด๊ฐ• ์šฐ', 0.4604458212852478), ('๊ธˆํ˜ธ์„์œ ์šฐ', 0.4568769931793213), ('์ปดํˆฌ์Šค', 0.4565160274505615), ('์ฝ”๋ฆฌ์•„์จํ‚คํŠธ 2์šฐ B', 0.45594915747642517), ('์„ธ๋ฐฉ ์šฐ', 0.4553225636482239)] 09-14 ์‹ค์ „! ํ•œ๊ตญ์–ด ์œ„ํ‚คํ”ผ๋””์•„๋กœ Word2Vec ํ•™์Šตํ•˜๊ธฐ ์•„๋ž˜์˜ ์‹ค์Šต์€ ๊ตฌ๊ธ€์˜ Colab์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ™˜๊ฒฝ์—์„œ ์‹ค์Šตํ•˜์…”๋„ ์ƒ๊ด€์€ ์—†์ง€๋งŒ, ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์€ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค์น˜ํ•˜์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1. ์œ„ํ‚คํ”ผ๋””์•„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋ฐ ํ†ตํ•ฉ ์œ„ํ‚คํ”ผ๋””์•„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑ ํ•˜๊ธฐ ์œ„ํ•œ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€์ธ wikiextractor๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install wikiextractor ์œ„ํ‚คํ”ผ๋””์•„ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œ ํ•œ ํ›„์— ์ „์ฒ˜๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์ธ Mecab์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. # Colab์— Mecab ์„ค์น˜ !git clone https://github.com/SOMJANG/Mecab-ko-for-Google-Colab.git %cd Mecab-ko-for-Google-Colab !bash install_mecab-ko_on_colab190912.sh ์œ„ํ‚คํ”ผ๋””์•„ ๋คํ”„(์œ„ํ‚คํ”ผ๋””์•„ ๋ฐ์ดํ„ฐ)๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. !wget https://dumps.wikimedia.org/kowiki/latest/kowiki-latest-pages-articles.xml.bz2 ์œ„ํ‚ค์ต์ŠคํŠธ๋ž™ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„ํ‚คํ”ผ๋””์•„ ๋คํ”„๋ฅผ ํŒŒ์‹ฑ ํ•ฉ๋‹ˆ๋‹ค. !python -m wikiextractor.WikiExtractor kowiki-latest-pages-articles.xml.bz2 ํ˜„์žฌ ๊ฒฝ๋กœ์— ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ํŒŒ์ผ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์•„์˜ต๋‹ˆ๋‹ค. %ls images/ LICENSE install_mecab-ko_on_colab190912.sh README.md install_mecab-ko_on_colab_light_210108.sh text/ kowiki-latest-pages-articles.xml.bz2 text๋ผ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—๋Š” ๋˜ ์–ด๋–ค ๋””๋ ‰ํ„ฐ๋ฆฌ๋“ค์ด ์žˆ๋Š”์ง€ ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. import os import re os.listdir('text') ['AG', 'AI', 'AH', 'AC', 'AE', 'AB', 'AA', 'AD', 'AF'] AA๋ผ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ํŒŒ์ผ๋“ค์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. %ls text/AA wiki_00 wiki_12 wiki_24 wiki_36 wiki_48 wiki_60 wiki_72 wiki_84 wiki_96 wiki_01 wiki_13 wiki_25 wiki_37 wiki_49 wiki_61 wiki_73 wiki_85 wiki_97 wiki_02 wiki_14 wiki_26 wiki_38 wiki_50 wiki_62 wiki_74 wiki_86 wiki_98 wiki_03 wiki_15 wiki_27 wiki_39 wiki_51 wiki_63 wiki_75 wiki_87 wiki_99 wiki_04 wiki_16 wiki_28 wiki_40 wiki_52 wiki_64 wiki_76 wiki_88 wiki_05 wiki_17 wiki_29 wiki_41 wiki_53 wiki_65 wiki_77 wiki_89 wiki_06 wiki_18 wiki_30 wiki_42 wiki_54 wiki_66 wiki_78 wiki_90 wiki_07 wiki_19 wiki_31 wiki_43 wiki_55 wiki_67 wiki_79 wiki_91 wiki_08 wiki_20 wiki_32 wiki_44 wiki_56 wiki_68 wiki_80 wiki_92 wiki_09 wiki_21 wiki_33 wiki_45 wiki_57 wiki_69 wiki_81 wiki_93 wiki_10 wiki_22 wiki_34 wiki_46 wiki_58 wiki_70 wiki_82 wiki_94 wiki_11 wiki_23 wiki_35 wiki_47 wiki_59 wiki_71 wiki_83 wiki_95 ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜๋œ ์œ„ํ‚คํ”ผ๋””์•„ ํ•œ๊ตญ์–ด ๋คํ”„๋Š” ์ด 6๊ฐœ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. AA ~ AF์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๊ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‚ด์—๋Š” 'wiki_00 ~ wiki_์•ฝ 90๋‚ด์™ธ์˜ ์ˆซ์ž'์˜ ํŒŒ์ผ๋“ค์ด ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ์—๋Š” ์•ฝ 90์—ฌ ๊ฐœ์˜ ํŒŒ์ผ๋“ค์ด ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ํŒŒ์ผ๋“ค์„ ์—ด์–ด๋ณด๋ฉด ์ด์™€ ๊ฐ™์€ ๊ตฌ์„ฑ์ด ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. <doc id="๋ฌธ์„œ ๋ฒˆํ˜ธ" url="์‹ค์ œ ์œ„ํ‚คํ”ผ๋””์•„ ๋ฌธ์„œ ์ฃผ์†Œ" title="๋ฌธ์„œ ์ œ๋ชฉ"> ๋‚ด์šฉ </doc> ์˜ˆ๋ฅผ ๋“ค์–ด์„œ AA ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ wiki_00 ํŒŒ์ผ์„ ์ฝ์–ด๋ณด๋ฉด, ์ง€๋ฏธ ์นดํ„ฐ์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. <doc id="5" url="https://ko.wikipedia.org/wiki?curid=5" title="์ง€๋ฏธ ์นดํ„ฐ"> ์ง€๋ฏธ ์นดํ„ฐ ์ œ์ž„์Šค ์–ผ "์ง€๋ฏธ" ์นดํ„ฐ ์ฃผ๋‹ˆ์–ด(, 1924๋…„ 10์›” 1์ผ ~ )๋Š” ๋ฏผ์ฃผ๋‹น ์ถœ์‹  ๋ฏธ๊ตญ 39๋ฒˆ์งธ ๋Œ€ํ†ต๋ น(1977๋…„ ~ 1981๋…„)์ด๋‹ค. ์ง€๋ฏธ ์นดํ„ฐ๋Š” ์กฐ์ง€์•„ ์ฃผ ์„ฌํ„ฐ ์นด์šดํ‹ฐ ํ”Œ๋ ˆ์ธ์Šค ๋งˆ์„์—์„œ ํƒœ์–ด๋‚ฌ๋‹ค. ์กฐ์ง€์•„ ๊ณต๊ณผ๋Œ€ํ•™๊ต๋ฅผ ์กธ์—…ํ•˜์˜€๋‹ค. ๊ทธ ํ›„ ํ•ด๊ตฐ์— ๋“ค์–ด๊ฐ€ ์ „ํ•จยท์›์ž๋ ฅยท์ž ์ˆ˜ํ•จ์˜ ์Šน๋ฌด์›์œผ๋กœ ์ผํ•˜์˜€๋‹ค. 1953๋…„ ๋ฏธ๊ตญ ํ•ด๊ตฐ ๋Œ€ ์œ„๋กœ ์˜ˆํŽธํ•˜์˜€๊ณ  ์ดํ›„ ๋•…์ฝฉยท๋ฉดํ™” ๋“ฑ์„ ๊ฐ€๊ฟ” ๋งŽ์€ ๋ˆ์„ ๋ฒŒ์—ˆ๋‹ค. ... ์ดํ•˜ ์ค‘๋žต... </doc> ์ด์ œ ์ด 6๊ฐœ AA ~ AF ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์˜ wiki ์ˆซ์ž ํ˜•ํƒœ์˜ ์ˆ˜๋งŽ์€ ํŒŒ์ผ๋“ค์„ ํ•˜๋‚˜๋กœ ํ†ตํ•ฉํ•˜๋Š” ๊ณผ์ •์„ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. AA ~ AF ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์˜ ๋ชจ๋“  ํŒŒ์ผ๋“ค์˜ ๊ฒฝ๋กœ๋ฅผ ํŒŒ์ด์ฌ์˜ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. def list_wiki(dirname): filepaths = [] filenames = os.listdir(dirname) for filename in filenames: filepath = os.path.join(dirname, filename) if os.path.isdir(filepath): # ์žฌ๊ท€ ํ•จ์ˆ˜ filepaths.extend(list_wiki(filepath)) else: find = re.findall(r"wiki_[0-9][0-9]", filepath) if 0 < len(find): filepaths.append(filepath) return sorted(filepaths) filepaths = list_wiki('text') ์ด ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. len(filepaths) 850 ์ด ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๋Š” 850๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด์ œ output_file.txt๋ผ๋Š” ํŒŒ์ผ์— 850๊ฐœ์˜ ํŒŒ์ผ์„ ์ „๋ถ€ ํ•˜๋‚˜๋กœ ํ•ฉ์นฉ๋‹ˆ๋‹ค. with open("output_file.txt", "w") as outfile: for filename in filepaths: with open(filename) as infile: contents = infile.read() outfile.write(contents) ํŒŒ์ผ์„ ์ฝ๊ณ  10์ค„๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. f = open('output_file.txt', encoding="utf8") i = 0 while True: line = f.readline() if line != '\n': i = i+1 print("%d ๋ฒˆ์งธ ์ค„ :"%i + line) if i==10: break f.close() 1๋ฒˆ์งธ ์ค„ :<doc id="5" url="https://ko.wikipedia.org/wiki? curid=5" title="์ง€๋ฏธ ์นดํ„ฐ"> 2๋ฒˆ์งธ ์ค„ :์ง€๋ฏธ ์นดํ„ฐ 3๋ฒˆ์งธ ์ค„ :์ œ์ž„์Šค ์–ผ ์นดํ„ฐ ์ฃผ๋‹ˆ์–ด(, 1924๋…„ 10์›” 1์ผ ~ )๋Š” ๋ฏผ์ฃผ๋‹น ์ถœ์‹  ๋ฏธ๊ตญ 39๋Œ€ ๋Œ€ํ†ต๋ น (1977๋…„ ~ 1981๋…„)์ด๋‹ค. 4๋ฒˆ์งธ ์ค„ :์ƒ์• . 5๋ฒˆ์งธ ์ค„ :์–ด๋ฆฐ ์‹œ์ ˆ. 6๋ฒˆ์งธ ์ค„ :์ง€๋ฏธ ์นดํ„ฐ๋Š” ์กฐ์ง€์•„์ฃผ ์„ฌํ„ฐ ์นด์šดํ‹ฐ ํ”Œ๋ ˆ์ธ์Šค ๋งˆ์„์—์„œ ํƒœ์–ด๋‚ฌ๋‹ค. 7๋ฒˆ์งธ ์ค„ :์กฐ์ง€์•„ ๊ณต๊ณผ๋Œ€ํ•™๊ต๋ฅผ ์กธ์—…ํ•˜์˜€๋‹ค. ๊ทธ ํ›„ ํ•ด๊ตฐ์— ๋“ค์–ด๊ฐ€ ์ „ํ•จยท์›์ž๋ ฅยท์ž ์ˆ˜ํ•จ์˜ ์Šน๋ฌด์›์œผ๋กœ ์ผํ•˜์˜€๋‹ค. 1953๋…„ ๋ฏธ๊ตญ ํ•ด๊ตฐ ๋Œ€์œ„๋กœ ์˜ˆํŽธํ•˜์˜€๊ณ  ์ดํ›„ ๋•…์ฝฉยท๋ฉดํ™” ๋“ฑ์„ ๊ฐ€๊ฟ” ๋งŽ์€ ๋ˆ์„ ๋ฒŒ์—ˆ๋‹ค. ๊ทธ์˜ ๋ณ„๋ช…์ด "๋•…์ฝฉ ๋†๋ถ€" (Peanut Farmer)๋กœ ์•Œ๋ ค์กŒ๋‹ค. 8๋ฒˆ์งธ ์ค„ :์ •๊ณ„ ์ž…๋ฌธ. 9๋ฒˆ์งธ ์ค„ :1962๋…„ ์กฐ์ง€์•„ ์ฃผ<NAME> ์˜์› ์„ ๊ฑฐ์—์„œ ๋‚™์„ ํ•˜๋‚˜ ๊ทธ ์„ ๊ฑฐ๊ฐ€ ๋ถ€์ •์„ ๊ฑฐ์˜€์Œ์„ ์ž…์ฆํ•˜๊ฒŒ ๋˜์–ด ๋‹น์„ ๋˜๊ณ , 1966๋…„ ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ ์„ ๊ฑฐ์— ๋‚™์„ ํ•˜์ง€๋งŒ, 1970๋…„ ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ๋ฅผ ์—ญ์ž„ํ–ˆ๋‹ค. ๋Œ€ํ†ต๋ น์ด ๋˜๊ธฐ ์ „ ์กฐ์ง€์•„์ฃผ<NAME> ์˜์›์„ ๋‘ ๋ฒˆ ์—ฐ์ž„ํ–ˆ์œผ๋ฉฐ, 1971๋…„๋ถ€ํ„ฐ 1975๋…„๊นŒ์ง€ ์กฐ์ง€์•„ ์ง€์‚ฌ๋กœ ๊ทผ๋ฌดํ–ˆ๋‹ค. ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ๋กœ ์ง€๋‚ด๋ฉด์„œ, ๋ฏธ๊ตญ์— ์‚ฌ๋Š” ํ‘์ธ ๋“ฑ์šฉ ๋ฒ•์„ ๋‚ด์„ธ์› ๋‹ค. 10๋ฒˆ์งธ ์ค„ :๋Œ€ํ†ต๋ น ์žฌ์ž„. 2. ํ˜•ํƒœ์†Œ ๋ถ„์„ from tqdm import tqdm from konlpy.tag import Mecab ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์„ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. mecab = Mecab() ์šฐ์„  output_file์—๋Š” ์ด ๋ช‡ ์ค„์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. f = open('output_file.txt', encoding="utf8") lines = f.read().splitlines() print(len(lines)) 9718793 9,718,793๊ฐœ์˜ ์ค„์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 10๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. lines[:10] ['<doc id="5" url="https://ko.wikipedia.org/wiki? curid=5" title="์ง€๋ฏธ ์นดํ„ฐ">', '์ง€๋ฏธ ์นดํ„ฐ', '', '์ œ์ž„์Šค ์–ผ ์นดํ„ฐ ์ฃผ๋‹ˆ์–ด(, 1924๋…„ 10์›” 1์ผ ~ )๋Š” ๋ฏผ์ฃผ๋‹น ์ถœ์‹  ๋ฏธ๊ตญ 39๋Œ€ ๋Œ€ํ†ต๋ น (1977๋…„ ~ 1981๋…„)์ด๋‹ค.', '์ƒ์• .', '์–ด๋ฆฐ ์‹œ์ ˆ.', '์ง€๋ฏธ ์นดํ„ฐ๋Š” ์กฐ์ง€์•„์ฃผ ์„ฌํ„ฐ ์นด์šดํ‹ฐ ํ”Œ๋ ˆ์ธ์Šค ๋งˆ์„์—์„œ ํƒœ์–ด๋‚ฌ๋‹ค.', '์กฐ์ง€์•„ ๊ณต๊ณผ๋Œ€ํ•™๊ต๋ฅผ ์กธ์—…ํ•˜์˜€๋‹ค. ๊ทธ ํ›„ ํ•ด๊ตฐ์— ๋“ค์–ด๊ฐ€ ์ „ํ•จยท์›์ž๋ ฅยท์ž ์ˆ˜ํ•จ์˜ ์Šน๋ฌด์›์œผ๋กœ ์ผํ•˜์˜€๋‹ค. 1953๋…„ ๋ฏธ๊ตญ ํ•ด๊ตฐ ๋Œ€์œ„๋กœ ์˜ˆํŽธํ•˜์˜€๊ณ  ์ดํ›„ ๋•…์ฝฉยท๋ฉดํ™” ๋“ฑ์„ ๊ฐ€๊ฟ” ๋งŽ์€ ๋ˆ์„ ๋ฒŒ์—ˆ๋‹ค. ๊ทธ์˜ ๋ณ„๋ช…์ด "๋•…์ฝฉ ๋†๋ถ€" (Peanut Farmer)๋กœ ์•Œ๋ ค์กŒ๋‹ค.', '์ •๊ณ„ ์ž…๋ฌธ.', '1962๋…„ ์กฐ์ง€์•„ ์ฃผ<NAME> ์˜์› ์„ ๊ฑฐ์—์„œ ๋‚™์„ ํ•˜๋‚˜ ๊ทธ ์„ ๊ฑฐ๊ฐ€ ๋ถ€์ •์„ ๊ฑฐ์˜€์Œ์„ ์ž…์ฆํ•˜๊ฒŒ ๋˜์–ด ๋‹น์„ ๋˜๊ณ , 1966๋…„ ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ ์„ ๊ฑฐ์— ๋‚™์„ ํ•˜์ง€๋งŒ, 1970๋…„ ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ๋ฅผ ์—ญ์ž„ํ–ˆ๋‹ค. ๋Œ€ํ†ต๋ น์ด ๋˜๊ธฐ ์ „ ์กฐ์ง€์•„์ฃผ<NAME> ์˜์›์„ ๋‘ ๋ฒˆ ์—ฐ์ž„ํ–ˆ์œผ๋ฉฐ, 1971๋…„๋ถ€ํ„ฐ 1975๋…„๊นŒ์ง€ ์กฐ์ง€์•„ ์ง€์‚ฌ๋กœ ๊ทผ๋ฌดํ–ˆ๋‹ค. ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ๋กœ ์ง€๋‚ด๋ฉด์„œ, ๋ฏธ๊ตญ์— ์‚ฌ๋Š” ํ‘์ธ ๋“ฑ์šฉ ๋ฒ•์„ ๋‚ด์„ธ์› ๋‹ค.'] ๋‘ ๋ฒˆ์งธ ์ค„์„ ๋ณด๋ฉด ์•„๋ฌด๋Ÿฐ ๋‹จ์–ด๋„ ๋“ค์–ด์žˆ์ง€ ์•Š์€ ''์™€ ๊ฐ™์€ ์ค„๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฌธ์ž์—ด์€ ํ˜•ํƒœ์†Œ ๋ถ„์„์—์„œ ์ œ์™ธํ•˜๋„๋ก ํ•˜๊ณ  ํ˜•ํƒœ์†Œ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. result = [] for line in tqdm(lines): # ๋นˆ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ ์ˆ˜ํ–‰ if line: result.append(mecab.morphs(line)) 100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 9718793/9718793 [15:27<00:00, 10478.61it/s] ๋นˆ ๋ฌธ์ž์—ด์€ ์ œ์™ธํ•˜๊ณ  ํ˜•ํƒœ์†Œ ๋ถ„์„์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ช‡ ๊ฐœ์˜ ์ค„. ์ฆ‰, ๋ช‡ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. len(result) 6559314 6,559,314๊ฐœ๋กœ ๋ฌธ์žฅ์˜ ์ˆ˜๊ฐ€ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. 3. Word2Vec ํ•™์Šต ํ˜•ํƒœ์†Œ ๋ถ„์„์„ ํ†ตํ•ด์„œ ํ† ํฐ ํ™”๊ฐ€ ์ง„ํ–‰๋œ ์ƒํƒœ์ด๋ฏ€๋กœ Word2Vec์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. from gensim.models import Word2Vec model = Word2Vec(result, size=100, window=5, min_count=5, workers=4, sg=0) model_result1 = model.wv.most_similar("๋Œ€ํ•œ๋ฏผ๊ตญ") print(model_result1) [('ํ•œ๊ตญ', 0.7382678389549255), ('๋ฏธ๊ตญ', 0.6731516122817993), ('์ผ๋ณธ', 0.6541135907173157), ('๋ถ€์‚ฐ', 0.5798133611679077), ('ํ™์ฝฉ', 0.5752249360084534), ('์„œ์šธ', 0.5541036128997803), ('์˜ค์ŠคํŠธ๋ ˆ์ผ๋ฆฌ์•„', 0.5531408786773682), ('ํƒœ๊ตญ', 0.548468828201294), ('๊ฒฝ์ƒ๋‚จ๋„', 0.5462549924850464), ('์ œ์ฃผํŠน๋ณ„์ž์น˜๋„', 0.5385439395904541)] model_result2 = model.wv.most_similar("์–ด๋ฒค์ €์Šค") print(model_result2) [('์ŠคํŒŒ์ด๋”๋งจ', 0.80271977186203), ('ํŠธ๋žœ์Šคํฌ๋จธ', 0.773989200592041), ('์•„์ด์–ธ๋งจ', 0.7648921012878418), ('์Šคํƒ€ํŠธ๋ ‰', 0.7645636796951294), ('์–ด๋ฒค์ €์Šค', 0.7626765966415405), ('์—‘์Šค๋งจ', 0.7586475610733032), ('ใ€Šใ€‹,', 0.7560415267944336), ('ํŠธ์™€์ผ๋ผ์ž‡', 0.7518032789230347), ('ํผ๋‹ˆ์…”', 0.7391209602355957), ('ํ…Œ์ผ์ฆˆ', 0.7386105060577393)] model_result3 = model.wv.most_similar("๋ฐ˜๋„์ฒด") print(model_result3) [('์ง‘์ ํšŒ๋กœ', 0.7714468836784363), ('์—ฐ๋ฃŒ์ „์ง€', 0.7699108719825745), ('์ „์ž', 0.7606919407844543), ('์›จ์ดํผ', 0.745188295841217), ('์‹ค๋ฆฌ์ฝ˜', 0.743209958076477), ('ํŠธ๋žœ์ง€์Šคํ„ฐ', 0.7398351430892944), ('PCB', 0.7275883555412292), ('TSMC', 0.7156406044960022), ('๊ฐ€์†๊ธฐ', 0.6962155699729919), ('๊ด‘์ „์ž', 0.6957612037658691)] 10. RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜(Text Classification) ํ…์ŠคํŠธ ๋ถ„๋ฅ˜(Text Classification)๋Š” ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ํ…์ŠคํŠธ๊ฐ€ ์–ด๋–ค ์ข…๋ฅ˜์˜ ๋ฒ”์ฃผ์— ์†ํ•˜๋Š”์ง€๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ์ž‘์—…์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๋ฅผ ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด, ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๋Š” ์ผ๋ฐ˜ ๋ฉ”์ผ๊ณผ ์ŠคํŒธ ๋ฉ”์ผ์ด๋ผ๋Š” ๋‘ ๊ฐœ์˜ ๋ฒ”์ฃผ๋ฅผ ์ •ํ•ด๋†“๊ณ  ์ž…๋ ฅ๋ฐ›์€ ๋ฉ”์ผ ๋ณธ๋ฌธ์„ ๋‘ ๊ฐœ์˜ ๋ฉ”์ผ ์ข…๋ฅ˜ ์ค‘ ํ•˜๋‚˜๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ์ž‘์—…์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์—์„œ ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ๋ฒ”์ฃผ๊ฐ€ ๋‘ ๊ฐ€์ง€๋ผ๋ฉด ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification)๋ผ๊ณ  ํ•˜๋ฉฐ, ์„ธ ๊ฐ€์ง€ ์ด์ƒ์ด๋ผ๋ฉด ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-Class Classification)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜ ๋ฉ”์ผ๊ณผ ์ŠคํŒธ ๋ฉ”์ผ ๋‘ ๊ฐœ์˜ ๋ฒ”์ฃผ๋ฅผ ๊ฐ€์ง„ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ ์™ธ์—๋„ ์˜ํ™” ๋ฆฌ๋ทฐ์™€ ๊ฐ™์€ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ด ๋ฆฌ๋ทฐ๊ฐ€ ๊ธ์ • ๋ฆฌ๋ทฐ์ธ์ง€ ๋ถ€์ • ๋ฆฌ๋ทฐ์ธ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” '๊ฐ์„ฑ ๋ถ„์„', ์ž…๋ ฅ๋ฐ›์€ ํ…์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ์‚ฌ์šฉ์ž์˜ ์˜๋„๋ฅผ ์งˆ๋ฌธ, ๋ช…๋ น, ๊ฑฐ์ ˆ ๋“ฑ๊ณผ ๊ฐ™์€ ์˜๋„๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” '์˜๋„ ๋ถ„์„' ๊ณผ ๊ฐ™์€ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” RNN ๊ณ„์—ด์˜ ์‹ ๊ฒฝ๋ง ๋ฐ”๋‹๋ผ RNN, LSTM, GRU๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ณ , ๋”ฅ ๋Ÿฌ๋‹ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ดํ•ด๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค. 10-01 ์ผ€๋ผ์Šค๋ฅผ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๊ฐœ์š”(Text Classification using Keras) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 1. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด ์•ž์œผ๋กœ ์ง„ํ–‰ํ•˜๋Š” ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์‹ค์Šต์€ ์ง€๋„ ํ•™์Šต(Supervised Learning)์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์ง€๋„ ํ•™์Šต์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ๋ ˆ์ด๋ธ”์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฏธ๋ฆฌ ์ •๋‹ต์ด ์ ํ˜€์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋น„์œ ํ•˜๋ฉด, ๊ธฐ๊ณ„๋Š” ์ •๋‹ต์ด ์ ํ˜€์ ธ ์žˆ๋Š” ๋ฌธ์ œ์ง€๋ฅผ ์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ•˜๊ณ , ํ–ฅํ›„์— ์ •๋‹ต์ด ์—†๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ๋„ ์ •๋‹ต์„ ์˜ˆ์ธกํ•ด์„œ ๋Œ€๋‹ตํ•˜๊ฒŒ ๋˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋ฉ”์ผ์˜ ๋‚ด์šฉ๊ณผ ํ•ด๋‹น ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ธ์ง€, ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€ ์ ํ˜€์žˆ๋Š” ๋ ˆ์ด๋ธ”๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€<NAME>์˜ ๋ฉ”์ผ ์ƒ˜ํ”Œ์ด ์•ฝ 20,000๊ฐœ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ํ…์ŠคํŠธ(๋ฉ”์ผ์˜ ๋‚ด์šฉ) ๋ ˆ์ด๋ธ”(์ŠคํŒธ ์—ฌ๋ถ€) ๋‹น์‹ ์—๊ฒŒ ๋“œ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰ ํ˜œํƒ! ... ์ŠคํŒธ ๋ฉ”์ผ ๋‚ด์ผ ๋ต ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธ ๋ถ€ํƒ... ์ •์ƒ ๋ฉ”์ผ ์‰ฟ! ํ˜ผ์ž ๋ณด์„ธ์š”... ์ŠคํŒธ ๋ฉ”์ผ ์–ธ์ œ๊นŒ์ง€ ๋‹ต์žฅ ๊ฐ€๋Šฅํ• ... ์ •์ƒ ๋ฉ”์ผ ... ... (๊ด‘๊ณ ) ๋ฉ‹์žˆ์–ด์งˆ ์ˆ˜ ์žˆ๋Š”... ์ŠคํŒธ ๋ฉ”์ผ 20,000๊ฐœ์˜ ๋ฉ”์ผ ์ƒ˜ํ”Œ์„ ๊ฐ€์ง„ ์ด ๋ฐ์ดํ„ฐ๋Š” ๋ฉ”์ผ์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์™€ ์ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€ ์•„๋‹Œ์ง€๊ฐ€ ์ ํ˜€์žˆ๋Š” ๋ ˆ์ด๋ธ”. ๋‘ ๊ฐ€์ง€ ์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ์ด 20,000๊ฐœ์˜ ๋ฉ”์ผ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ–ˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ์— ๋ฌธ์ œ๊ฐ€ ์—†๊ณ , ๋ชจ๋ธ ๋˜ํ•œ ์ž˜ ์„ค๊ณ„๋ผ ์žˆ๋‹ค๋ฉด ํ•™์Šต์ด ๋‹ค ๋œ ์ด ๋ชจ๋ธ์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜์ง€ ์•Š์•˜๋˜ ์–ด๋–ค ๋ฉ”์ผ ํ…์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์ง€๋”๋ผ๋„ ์ •ํ™•ํ•œ ๋ ˆ์ด๋ธ”์„ ์˜ˆ์ธกํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 2. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์œ„์—์„œ๋Š” 20,000๊ฐœ์˜ ๋ฉ”์ผ ์ƒ˜ํ”Œ์„ ์ „๋ถ€ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ–ˆ์ง€๋งŒ ์‚ฌ์‹ค ๊ฐ–๊ณ  ์žˆ๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋ถ€ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ํ…Œ์ŠคํŠธ์šฉ์€ ์ผ๋ถ€ ๋‚จ๊ฒจ๋†“๋Š” ๊ฒƒ์œผ๋กœ ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 20,000๊ฐœ์˜ ์ƒ˜ํ”Œ ์ค‘ 18,000๊ฐœ์˜ ์ƒ˜ํ”Œ์€ ํ›ˆ๋ จ์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , 2,000๊ฐœ์˜ ์ƒ˜ํ”Œ์€ ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ๋ณด๋ฅ˜ํ•œ ์ฑ„ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„ ํ›ˆ๋ จ์ด ๋๋‚˜๋ฉด, ๋ณด๋ฅ˜ํ•ด๋‘์—ˆ๋˜ 2,000๊ฐœ์˜ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ๋กœ ๋ชจ๋ธ์—๊ฒŒ ๋ ˆ์ด๋ธ”์€ ๋ณด์—ฌ์ฃผ์ง€ ์•Š๊ณ , ๋ชจ๋ธ์—๊ฒŒ ๋ ˆ์ด๋ธ”์„ ๋งž์ถฐ๋ณด๋ผ๊ณ  ์š”๊ตฌํ•œ ๋’ค, ์ฑ„์ ์„ ํ†ตํ•ด ์ •ํ™•๋„(accuracy)๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ค‘ ๋ถ„๋ฅ˜ ๋Œ€์ƒ์ธ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์—ด์„ X, ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ์˜ ์—ด์„ y๋ผ๊ณ  ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ(X_train, y_train)์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ(X_test, y_test)๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ X_train๊ณผ y_train์„ ํ•™์Šตํ•˜๊ณ , X_test์— ๋Œ€ํ•ด์„œ ๋ ˆ์ด๋ธ”์„ ์˜ˆ์ธกํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•œ ๋ ˆ์ด๋ธ”๊ณผ y_test๋ฅผ ๋น„๊ตํ•ด์„œ ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. 3. ๋‹จ์–ด์— ๋Œ€ํ•œ ์ •์ˆ˜ ๋ถ€์—ฌ ์ผ€๋ผ์Šค์˜ Embedding()์€ ๋‹จ์–ด ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์ •์ˆ˜๋กœ ๋ณ€ํ™˜๋œ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์ž„๋ฒ ๋”ฉ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด ๊ฐ๊ฐ์— ์ˆซ์ž ๋งคํ•‘, ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ฑ•ํ„ฐ์—์„œ์™€ ๊ฐ™์ด ๋‹จ์–ด๋ฅผ ๋นˆ๋„์ˆ˜ ์ˆœ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ณ  ์ˆœ์ฐจ์ ์œผ๋กœ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ถ„๋ฅ˜ํ•˜๊ธฐ์™€ IMDB ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‹ค์Šต์—์„œ๋„ ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์˜€์œผ๋ฉฐ, ํ•ด๋‹น ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋“ค์€ ์ด๋ฏธ ์ด ์ž‘์—…์ด ๋๋‚œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ์œผ๋กœ ๋‹จ์–ด๋ฅผ ์ •๋ ฌํ•˜์—ฌ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜์˜€์„ ๋•Œ์˜ ์žฅ์ ์€ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ์ ์€ ๋‹จ์–ด์˜ ์ œ๊ฑฐ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ 25,000๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ˆœ๊ฐ€ ๋†’์€ ์ˆœ์„œ๋กœ 1๋ถ€ํ„ฐ 25,000๊นŒ์ง€ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ–ˆ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ์œผ๋กœ ๋“ฑ์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋œ ๊ฒƒ๊ณผ ๋‹ค๋ฆ„์—†์œผ๋ฏ€๋กœ ์ „์ฒ˜๋ฆฌ ์ž‘์—…์—์„œ 1,000๋ณด๋‹ค ํฐ ์ •์ˆ˜๋กœ ๋งคํ•‘๋œ ๋‹จ์–ด๋“ค์„ ์ œ๊ฑฐํ•œ๋‹ค๋ฉด ๋“ฑ์žฅ ๋นˆ๋„ ์ƒ์œ„ 1,000๊ฐœ์˜ ๋‹จ์–ด๋งŒ ๋‚จ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. RNN์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ model.add(SimpleRNN(hidden_units, input_shape=(timesteps, input_dim))) RNN ์ฝ”๋“œ์˜ ์ธ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. hidden_units = RNN์˜ ์ถœ๋ ฅ์˜ ํฌ๊ธฐ = ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ. timesteps = ์‹œ์ ์˜ ์ˆ˜ = ๊ฐ ๋ฌธ์„œ์—์„œ์˜ ๋‹จ์–ด ์ˆ˜. input_dim = ์ž…๋ ฅ์˜ ํฌ๊ธฐ = ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›. 5. RNN์˜ ๋‹ค-๋Œ€-์ผ(Many-to-One) ๋ฌธ์ œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋Š” RNN์˜ ๋‹ค ๋Œ€ ์ผ(many-to-one) ๋ฌธ์ œ์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋Š” ๋ชจ๋“  ์‹œ์ (time step)์— ๋Œ€ํ•ด์„œ ์ž…๋ ฅ์„ ๋ฐ›์ง€๋งŒ ์ตœ์ข… ์‹œ์ ์˜ RNN ์…€ ๋งŒ์ด ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์ด๊ฒƒ์ด ์ถœ๋ ฅ์ธต์œผ๋กœ ๊ฐ€์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ •๋‹ต์„ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ์ •๋‹ต๋ฅผ ๊ณ ๋ฅด๋Š” ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification) ๋ฌธ์ œ๋ผ๊ณ  ํ•˜๋ฉฐ, ์„ธ ๊ฐœ ์ด์ƒ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ์ •๋‹ต์„ ๊ณ ๋ฅด๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-Class Classification) ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๋ฌธ์ œ์—์„œ๋Š” ๊ฐ๊ฐ ๋ฌธ์ œ์— ๋งž๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜์™€ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜์˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ์ถœ๋ ฅ์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ, ์†์‹ค ํ•จ์ˆ˜๋กœ binary_crossentropy๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ฌธ์ œ๋ผ๋ฉด ์ถœ๋ ฅ์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ, ์†์‹ค ํ•จ์ˆ˜๋กœ categorical_crossentropy๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ์—๋Š” ํด๋ž˜์Šค๊ฐ€ N ๊ฐœ๋ผ๋ฉด ์ถœ๋ ฅ์ธต์— ํ•ด๋‹น๋˜๋Š” ๋ฐ€์ง‘์ธต(dense layer)์˜ ํฌ๊ธฐ๋Š” N์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅด๊ฒŒ ํ‘œํ˜„ํ•˜๋ฉด ์ถœ๋ ฅ์ธต ๋‰ด๋Ÿฐ์˜ ์ˆ˜๋Š” N ๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ๋‚˜ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‹ค์Šต๋“ค์ด ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์— ํ•ด๋‹น๋˜๋ฉฐ, ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ถ„๋ฅ˜ํ•˜๊ธฐ ๋ฌธ์ œ๊ฐ€ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 10-02 ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ(Spam Detection) ์บ๊ธ€์—์„œ ์ œ๊ณตํ•˜๋Š” ์ŠคํŒธ ๋ฉ”์ผ ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šต์‹œ์ผœ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ์ŠคํŒธ ๋ฉ”์ผ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://www.kaggle.com/uciml/sms-spam-collection-dataset import numpy as np import pandas as pd import matplotlib.pyplot as plt import urllib.request from sklearn.model_selection import train_test_split from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences ๋‹ค์šด๋กœ๋“œํ•œ spam.csv ํŒŒ์ผ์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•˜๊ณ  ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/10.%20RNN%20Text%20Classification/dataset/spam.csv", filename="spam.csv") data = pd.read_csv('spam.csv', encoding='latin1') print('์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ :',len(data)) ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 5572 ์ด 5,572๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. data[:5] ์ŠคํŒธ ๋ฉ”์ผ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ 5๊ฐœ์˜ ํ–‰๋งŒ ์ถœ๋ ฅํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ์—๋Š” ์ด 5๊ฐœ์˜ ์—ด์ด ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ Unnamed๋ผ๋Š” ์ด๋ฆ„์˜ 3๊ฐœ์˜ ์—ด์€ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ํ•  ๋•Œ ๋ถˆํ•„์š”ํ•œ ์—ด์ž…๋‹ˆ๋‹ค. v1์—ด์€ ํ•ด๋‹น ๋ฉ”์ผ์ด ์ŠคํŒธ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” ์—ด์ž…๋‹ˆ๋‹ค. ham์€ ์ •์ƒ ๋ฉ”์ผ์„ ์˜๋ฏธํ•˜๊ณ , spam์€ ์ŠคํŒธ ๋ฉ”์ผ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. v2์—ด์€ ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์„ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”๊ณผ ๋ฉ”์ผ ๋‚ด์šฉ์ด ๋‹ด๊ธด v1 ์—ด๊ณผ v2์—ด๋งŒ ํ•„์š”ํ•˜๋ฏ€๋กœ, Unnamed: 2, Unnamed: 3, Unnamed: 4 ์—ด์€ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, v1 ์—ด์— ์žˆ๋Š” ham๊ณผ spam ๋ ˆ์ด๋ธ”์„ ๊ฐ๊ฐ ์ˆซ์ž 0๊ณผ 1๋กœ ๋ฐ”๊พธ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ data์—์„œ 5๊ฐœ์˜ ํ–‰๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. del data['Unnamed: 2'] del data['Unnamed: 3'] del data['Unnamed: 4'] data['v1'] = data['v1'].replace(['ham','spam'],[0,1]) data[:5] ๋ถˆํ•„์š”ํ•œ ์—ด์ด ์ œ๊ฑฐ๋˜๊ณ  v1 ์—ด์˜ ๊ฐ’์ด ์ˆซ์ž๋กœ ๋ณ€ํ™˜๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. data.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 5572 entries, 0 to 5571 Data columns (total 2 columns): v1 5572 non-null int64 v2 5572 non-null object dtypes: int64(1), object(1) memory usage: 87.1+ KB v1์—ด์€ ์ •์ˆ˜ํ˜•, v2์—ด์€ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์žˆ๋Š”์ง€ isnull().values.any()๋กœ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('๊ฒฐ์ธก๊ฐ’ ์—ฌ๋ถ€ :',data.isnull().values.any()) ๊ฒฐ์ธก๊ฐ’ ์—ฌ๋ถ€ : False False๋Š” ๋ณ„๋„์˜ Null ๊ฐ’์€ ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ์— 'Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'์—ด์—๋Š” NaN์ด ์žˆ์—ˆ๋Š”๋ฐ ํ•ด๋‹น ์ƒํƒœ์—์„œ ์ˆ˜ํ–‰ํ•˜๋Š” isnull().values.any()๋Š” True๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. Null ๊ฐ’์ด ์—†๋‹ค๋ฉด ๋ฐ์ดํ„ฐ์— ์ค‘๋ณต์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('v2 ์—ด์˜ ์œ ๋‹ˆํฌํ•œ ๊ฐ’ :',data['v2'].nunique()) v2 ์—ด์˜ ์œ ๋‹ˆํฌํ•œ ๊ฐ’ : 5169 ์ด 5,572๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜๋Š”๋ฐ v2์—ด์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 5,169๊ฐœ๋ผ๋Š” ๊ฒƒ์€ 403๊ฐœ์˜ ์ค‘๋ณต ์ƒ˜ํ”Œ์ด ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ค‘๋ณต ์ƒ˜ํ”Œ์„ ์ œ๊ฑฐํ•˜๊ณ  ์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # v2 ์—ด์—์„œ ์ค‘๋ณต์ธ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ์ค‘๋ณต ์ œ๊ฑฐ data.drop_duplicates(subset=['v2'], inplace=True) print('์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ :',len(data)) ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 5169 ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜๊ฐ€ 5,572๊ฐœ์—์„œ 5,169๊ฐœ๋กœ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ” ๊ฐ’์˜ ๋ถ„ํฌ๋ฅผ ์‹œ๊ฐํ™”ํ•ฉ๋‹ˆ๋‹ค. data['v1'].value_counts().plot(kind='bar') ๋ ˆ์ด๋ธ”์ด ๋Œ€๋ถ€๋ถ„ 0์— ํŽธ์ค‘๋˜์–ด ์žˆ๋Š”๋ฐ, ์ด๋Š” ์ŠคํŒธ ๋ฉ”์ผ ๋ฐ์ดํ„ฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์ˆ˜์น˜๋ฅผ ํŒŒ์•…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('์ •์ƒ ๋ฉ”์ผ๊ณผ ์ŠคํŒธ ๋ฉ”์ผ์˜ ๊ฐœ์ˆ˜') print(data.groupby('v1').size().reset_index(name='count')) ์ •์ƒ ๋ฉ”์ผ๊ณผ ์ŠคํŒธ ๋ฉ”์ผ์˜ ๊ฐœ์ˆ˜ v1 count 0 0 4516 1 1 653 ๋ ˆ์ด๋ธ” 0์€ ์ด 4,516๊ฐœ๊ฐ€ ์กด์žฌํ•˜๊ณ  1์€ 653๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ %๋กœ ํ™˜์‚ฐํ•ฉ๋‹ˆ๋‹ค. print(f'์ •์ƒ ๋ฉ”์ผ์˜ ๋น„์œจ = {round(data["v1"].value_counts()[0]/len(data) * 100,3)}%') print(f'์ŠคํŒธ ๋ฉ”์ผ์˜ ๋น„์œจ = {round(data["v1"].value_counts()[1]/len(data) * 100,3)}%') ์ •์ƒ ๋ฉ”์ผ์˜ ๋น„์œจ = 87.367% ์ŠคํŒธ ๋ฉ”์ผ์˜ ๋น„์œจ = 12.633% v2 ์—ด๊ณผ v1์—ด์„ X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๋ผ๋Š” X_data, y_data๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. X_data = data['v2'] y_data = data['v1'] print('๋ฉ”์ผ ๋ณธ๋ฌธ์˜ ๊ฐœ์ˆ˜: {}'.format(len(X_data))) print('๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜: {}'.format(len(y_data))) ๋ฉ”์ผ ๋ณธ๋ฌธ์˜ ๊ฐœ์ˆ˜: 5169 ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜: 5169 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ํ˜„์žฌ ๋ ˆ์ด๋ธ”์ด ๊ต‰์žฅํžˆ ๋ถˆ๊ท ํ˜•ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ •์ƒ ๋ฉ”์ผ ์ƒ˜ํ”Œ(87%, 4516๊ฐœ)์— ๋น„ํ•ด์„œ ์ŠคํŒธ ๋ฉ”์ผ ์ƒ˜ํ”Œ(12%, 653๊ฐœ)์ด ๊ต‰์žฅํžˆ ์ ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๊ณผ์ •์—์„œ ์šฐ์—ฐํžˆ ๋Œ€๋ถ€๋ถ„์˜ ์ŠคํŒธ ๋ฉ”์ผ ์ƒ˜ํ”Œ์ด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ํฌํ•จ๋˜๊ณ  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—๋Š” ๋Œ€๋ถ€๋ถ„ ์ •์ƒ ๋ฉ”์ผ ์ƒ˜ํ”Œ๋งŒ ํฌํ•จ๋˜๊ฒŒ ๋œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ํ•™์Šต ๊ณผ์ •์—์„œ ๋ชจ๋ธ์€ ์ŠคํŒธ ๋ฉ”์ผ ์ƒ˜ํ”Œ์„ ๊ฑฐ์˜ ๊ด€์ธกํ•˜์ง€ ๋ชปํ•ด์„œ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ ˆ์ด๋ธ”์ด ๋ถˆ๊ท ํ˜•ํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆŒ ๋•Œ์—๋„ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๊ฐ ๋ ˆ์ด๋ธ”์˜ ๋ถ„ํฌ๊ฐ€ ๊ณ ๋ฅด๊ฒŒ ๋ถ„ํฌ๋˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท ๋Ÿฐ์˜ train_test_split์— stratify์˜ ์ธ์ž๋กœ์„œ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์žฌํ•˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•  ๋•Œ ๋ ˆ์ด๋ธ”์˜ ๋ถ„ํฌ๊ฐ€ ๊ณ ๋ฅด๊ฒŒ ๋ถ„ํฌํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. test_size์— 0.2๋ฅผ ๊ธฐ์žฌํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 8:2 ๋น„์œจ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.2, random_state=0, stratify=y_data) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ„๋ฆฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์ด ๊ณ ๋ฅด๊ฒŒ ๋ถ„ํฌ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('--------ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'์ •์ƒ ๋ฉ”์ผ = {round(y_train.value_counts()[0]/len(y_train) * 100,3)}%') print(f'์ŠคํŒธ ๋ฉ”์ผ = {round(y_train.value_counts()[1]/len(y_train) * 100,3)}%') --------ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ์ •์ƒ ๋ฉ”์ผ = 87.376% ์ŠคํŒธ ๋ฉ”์ผ = 12.624% print('--------ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'์ •์ƒ ๋ฉ”์ผ = {round(y_test.value_counts()[0]/len(y_test) * 100,3)}%') print(f'์ŠคํŒธ ๋ฉ”์ผ = {round(y_test.value_counts()[1]/len(y_test) * 100,3)}%') --------ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ์ •์ƒ ๋ฉ”์ผ = 87.331% ์ŠคํŒธ ๋ฉ”์ผ = 12.669% ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ชจ๋‘ ์ •์ƒ ๋ฉ”์ผ์€ 87%, ์ŠคํŒธ ๋ฉ”์ผ์€ 12%๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ†ตํ•ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™”์™€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. X_train_encoded์—๋Š” X_train์˜ ๊ฐ ๋‹จ์–ด๋“ค์ด ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋กœ ์ธ์ฝ”๋”ฉ๋˜์–ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. 5๊ฐœ์˜ ๋ฉ”์ผ๋งŒ ์ถœ๋ ฅํ•ด์„œ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(X_train) X_train_encoded = tokenizer.texts_to_sequences(X_train) print(X_train_encoded[:5]) [[102, 1, 210, 230, 3, 17, 39], [1, 59, 8, 427, 17, 5, 137, 2, 2326], [157, 180, 12, 13, 98, 93, 47, 9, 40, 3485, 247, 8, 7, 87, 6, 80, 1312, 5, 3486, 7, 2327, 11, 660, 306, 20, 25, 467, 708, 1028, 203, 129, 193, 800, 2328, 23, 1, 144, 71, 2, 111, 78, 43, 2, 130, 11, 800, 186, 122, 1512], [1, 1154, 13, 104, 292], [222, 622, 857, 540, 623, 22, 23, 83, 10, 47, 6, 257, 32, 6, 26, 64, 936, 407]] ๊ฐ ๋ฉ”์ผ์ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์ •์ˆ˜๊ฐ€ ์–ด๋–ค ๋‹จ์–ด์— ๋ถ€์—ฌ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. word_to_index = tokenizer.word_index print(word_to_index) {'i': 1, 'to': 2, 'you': 3, 'a': 4, 'the': 5, 'u': 6, 'and': 7, 'in': 8, 'is': 9, 'me': 10, 'my': 11, 'for': 12, 'your': 13, 'it': 14, 'of': 15, 'have': 16, 'on': 17, 'call': 18, 'that': 19, 'are': 20, '2': 21, 'now': 22, 'so': 23, 'but': 24, 'not': 25, 'can': 26, 'or': 27, "i'm": 28, 'get': 29, 'at': 30, 'do': 31, 'if': 32, 'be': 33, 'will': 34, 'just': 35, 'with': 36, 'we': 37, 'no': 38, 'this': 39, 'ur': 40, 'up': 41, '4': 42, 'how': 43, 'gt': 44, 'lt': 45, 'go': 46, 'when': 47, 'from': 48, 'what': 49, 'ok': 50, 'out': 51, 'know': 52, - ์ดํ•˜ ์ƒ๋žต} ๋ฌด์ˆ˜ํžˆ ๋งŽ์€ ๋‹จ์–ด๊ฐ€ ์ถœ๋ ฅ๋˜๋ฏ€๋กœ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์ค‘๊ฐ„์— ์ƒ๋žตํ–ˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ถ€์—ฌ๋œ ๊ฐ ์ •์ˆ˜๋Š” ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์„์ˆ˜๋ก ๋‚ฎ์€ ์ •์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ๋‹จ์–ด i๋Š” ํ˜„์žฌ ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๋Š” tokenizer.word_counts.items()๋ฅผ ์ถœ๋ ฅํ•ด์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์‘์šฉํ•˜์—ฌ ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๋“ค์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ์–ผ๋งˆํผ์˜ ๋น„์ค‘์„ ์ฐจ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 1ํšŒ ๋ฐ–์— ๋˜์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์ด ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์–ผ๋งˆํผ์˜ ๋น„์œจ์„ ์ฐจ์ง€ํ•˜๋ฉฐ, ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„๋กœ ์–ผ๋งˆํผ์˜ ๋น„์œจ์„ ์ฐจ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. threshold = 2 total_cnt = len(word_to_index) # ๋‹จ์–ด์˜ ์ˆ˜ rare_cnt = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ total_freq = 0 # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๋‹จ์–ด ๋นˆ๋„์ˆ˜ ์ดํ•ฉ rare_freq = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜์˜ ์ดํ•ฉ # ๋‹จ์–ด์™€ ๋นˆ๋„์ˆ˜์˜ ์Œ(pair)์„ key์™€ value๋กœ ๋ฐ›๋Š”๋‹ค. for key, value in tokenizer.word_counts.items(): total_freq = total_freq + value # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์œผ๋ฉด if(value < threshold): rare_cnt = rare_cnt + 1 rare_freq = rare_freq + value print('๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ %s ๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: %s'%(threshold - 1, rare_cnt)) print("๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ:", (rare_cnt / total_cnt)*100) print("์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ:", (rare_freq / total_freq)*100) ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 1๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: 4337 ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ: 55.45326684567191 ์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ: 6.65745644331875 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ threshold ๊ฐ’์ธ 2ํšŒ ๋ฏธ๋งŒ. ์ฆ‰, 1ํšŒ ๋ฐ–์— ๋˜์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋ฌด๋ ค ์ ˆ๋ฐ˜ ์ด์ƒ์„ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„๋กœ ์ฐจ์ง€ํ•˜๋Š” ๋น„์ค‘์€ ์ƒ๋Œ€์ ์œผ๋กœ ์ ์€ ์ˆ˜์น˜์ธ 6%๋ฐ–์— ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ด๋Ÿฌํ•œ ๋ถ„์„์„ ํ†ตํ•ด ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ๋‚ฎ์€ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์ œ์™ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ € ์„ ์–ธ ์‹œ์— ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์•„๋ž˜์˜ ์ฝ”๋“œ๋กœ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 1ํšŒ์ธ ๋‹จ์–ด๋“ค์„ ์ œ์™ธํ•  ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. tokenizer = Tokenizer(num_words = total_cnt - rare_cnt + 1) ํ•˜์ง€๋งŒ ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ๋ณ„๋„๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ œํ•œํ•˜์ง„ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ vocab_size์— ์ €์žฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒจ๋”ฉ์„ ์œ„ํ•œ ํ† ํฐ์ธ 0๋ฒˆ ๋‹จ์–ด๋ฅผ ๊ณ ๋ คํ•˜๋ฉฐ +1์„ ํ•ด์„œ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. vocab_size = len(word_to_index) + 1 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ: {}'.format((vocab_size))) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ: 7822 ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 7,822์ž…๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋ฉ”์ผ๊ณผ ์ „์ฒด ๋ฉ”์ผ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('๋ฉ”์ผ์˜ ์ตœ๋Œ€ ๊ธธ์ด : %d' % max(len(sample) for sample in X_train_encoded)) print('๋ฉ”์ผ์˜ ํ‰๊ท  ๊ธธ์ด : %f' % (sum(map(len, X_train_encoded))/len(X_train_encoded))) plt.hist([len(sample) for sample in X_data], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋ฉ”์ผ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 189 ๋ฉ”์ผ์˜ ํ‰๊ท  ๊ธธ์ด : 15.754534 ๊ฐ€์žฅ ๊ธด ๋ฉ”์ผ์˜ ๊ธธ์ด๋Š” 189์ด๋ฉฐ, ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋Š” ๋Œ€์ฒด์ ์œผ๋กœ ์•ฝ 50์ดํ•˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. max_len = 189 X_train_padded = pad_sequences(X_train_encoded, maxlen = max_len) print("ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape):", X_train_padded.shape) maxlen์—๋Š” ๊ฐ€์žฅ ๊ธด ๋ฉ”์ผ์˜ ๊ธธ์ด์˜€๋˜ 189์ด๋ผ๋Š” ์ˆซ์ž๋ฅผ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” 4,135๊ฐœ์˜ X_train_encoded์˜ ๊ธธ์ด๋ฅผ ์ „๋ถ€ 189๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. 189๋ณด๋‹ค ๊ธธ์ด๊ฐ€ ์งง์€ ๋ฉ”์ผ ์ƒ˜ํ”Œ์€ ์ „๋ถ€ ์ˆซ์ž 0์ด ํŒจ๋”ฉ ๋˜์–ด 189์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape): (4135, 189) X_train_encoded ๋ฐ์ดํ„ฐ๋Š” 4,135 ร— 189์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. RNN์œผ๋กœ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 32, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 32์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€ ์ผ ๊ตฌ์กฐ์˜ RNN์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 4๋ฒˆ์˜ ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 64์ด๋ฉฐ, validation_split=0.2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 20%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ , ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ํ›ˆ๋ จ์ด ์ ์ ˆํžˆ ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. from tensorflow.keras.layers import SimpleRNN, Embedding, Dense from tensorflow.keras.models import Sequential embedding_dim = 32 hidden_units = 32 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(SimpleRNN(hidden_units)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc']) history = model.fit(X_train_padded, y_train, epochs=4, batch_size=64, validation_split=0.2) ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•๋„๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. X_test_encoded = tokenizer.texts_to_sequences(X_test) X_test_padded = pad_sequences(X_test_encoded, maxlen = max_len) print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (model.evaluate(X_test_padded, y_test)[1])) 1034/1034 [==============================] - 0s 166us/step ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.9821 ์ •ํ™•๋„๊ฐ€ 98%๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๊ฐ™์ด ์ •ํ™•๋„๋ฅผ ํ™•์ธํ•˜๋ฉด์„œ ํ›ˆ๋ จํ•˜์˜€์œผ๋ฏ€๋กœ, ์ด๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ทธ๋ž˜ํ”„๋กœ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. epochs = range(1, len(history.history['acc']) + 1) plt.plot(epochs, history.history['loss']) plt.plot(epochs, history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'val'], loc='upper left') plt.show() ์ด๋ฒˆ ์‹ค์Šต ๋ฐ์ดํ„ฐ๋Š” ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ ์–ด ๊ณผ์ ํ•ฉ์ด ๋น ๋ฅด๊ฒŒ ์‹œ์ž‘๋˜๋ฏ€๋กœ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ค์ฐจ๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ์‹œ์ ์˜ ๋ฐ”๋กœ ์ง์ „์ธ ์—ํฌํฌ 3~4 ์ •๋„๊ฐ€ ์ ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ์—ํฌํฌ 5๋ฅผ ๋„˜์–ด๊ฐ€๊ธฐ ์‹œ์ž‘ํ•˜๋ฉด ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์˜ค์ฐจ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 10-03 ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ถ„๋ฅ˜ํ•˜๊ธฐ(Reuters News Classification) ์ผ€๋ผ์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ฐ์ดํ„ฐ๋ฅผ LSTM์„ ์ด์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋กœ์ดํ„ฐ ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ฐ์ดํ„ฐ๋Š” ์ด 11,258๊ฐœ์˜ ๋‰ด์Šค ๊ธฐ์‚ฌ๊ฐ€ 46๊ฐœ์˜ ๋‰ด์Šค ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ๋ถ„๋ฅ˜๋˜๋Š” ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. 1. ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด import numpy as np import seaborn as sns import matplotlib.pyplot as plt from tensorflow.keras.datasets import reuters ์ผ€๋ผ์Šค ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ๋ถ€ํ„ฐ ๋กœ์ดํ„ฐ ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๊ณ , ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ์šฉ๊ณผ ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ๋‚˜๋ˆ„๊ฒ ์Šต๋‹ˆ๋‹ค. reuters.load_data()์—์„œ num_words๋Š” ์ด ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ์œ„๋กœ ๋ช‡ ๋ฒˆ์งธ์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๊นŒ์ง€๋งŒ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ์กฐ์ ˆํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ 100์ด๋ž€ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด, ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ์œ„๋กœ ์ƒ์œ„ 1~100 ๋“ฑ์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋งŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด None์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์•„์ง ๋ฌด์Šจ ์˜๋ฏธ์ธ์ง€ ์ดํ•ด๊ฐ€ ์–ด๋ ค์šด ๋ถ„๋“ค์„ ์œ„ํ•ด์„œ ์•„๋ž˜์—์„œ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๋ฉด์„œ ๋‹ค์‹œ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. test_split์€ ์ „์ฒด ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ฐ์ดํ„ฐ ์ค‘ ํ…Œ์ŠคํŠธ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ๋กœ ๋ช‡ ํผ์„ผํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์ „์ฒด ๋‰ด์Šค ๊ธฐ์‚ฌ ์ค‘ 20%๋ฅผ ํ…Œ์ŠคํŠธ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ 0.2๋กœ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ์™€ ํ…Œ์ŠคํŠธ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ๊ฐ€ 8:2๋กœ ์ •์ƒ์ ์œผ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ๋กœ๋“œ๋˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. (X_train, y_train), (X_test, y_test) = reuters.load_data(num_words=None, test_split=0.2) print('ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ : {}'.format(len(X_train))) print('ํ…Œ์ŠคํŠธ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ : {}'.format(len(X_test))) num_classes = len(set(y_train)) print('์นดํ…Œ๊ณ ๋ฆฌ : {}'.format(num_classes)) ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ : 8982 ํ…Œ์ŠคํŠธ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ : 2246 ์นดํ…Œ๊ณ ๋ฆฌ : 46 ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ๋Š” 8,982๊ฐœ, ํ…Œ์ŠคํŠธ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ๋Š” 2,246๊ฐœ, ์นดํ…Œ๊ณ ๋ฆฌ๋Š” 46๊ฐœ์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์„ฑ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ฒซ ๋ฒˆ์งธ ๋‰ด์Šค ๊ธฐ์‚ฌ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. print('์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ :',X_train[0]) print('์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ์˜ ๋ ˆ์ด๋ธ” :',y_train[0]) ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ : [1, 27595, 28842, 8, 43, 10, 447, 5, 25, 207, 270, 5, 3095, 111, 16, 369, 186, 90, 67, 7, 89, 5, 19, 102, 6, 19, 124, 15, 90, 67, 84, 22, 482, 26, 7, 48, 4, 49, 8, 864, 39, 209, 154, 6, 151, 6, 83, 11, 15, 22, 155, 11, 15, 7, 48, 9, 4579, 1005, 504, 6, 258, 6, 272, 11, 15, 22, 134, 44, 11, 15, 16, 8, 197, 1245, 90, 67, 52, 29, 209, 30, 32, 132, 6, 109, 15, 17, 12] ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ์˜ ๋ ˆ์ด๋ธ” : 3 ์œ„์™€ ๊ฐ™์ด ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ฐ์ดํ„ฐ์ธ X_train ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋‰ด์Šค ๊ธฐ์‚ฌ์ธ X_train[0]์—๋Š” ์ •์ˆ˜์˜ ๋‚˜์—ด์ด ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ์„œ ์˜์•„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ˜„์žฌ ์ด ๋ฐ์ดํ„ฐ๋Š” ํ† ํฐํ™”๊ณผ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ(๊ฐ ๋‹จ์–ด๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜)์ด ๋๋‚œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ๋‹จ์–ด๋“ค์ด ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š”์ง€์˜ ๋นˆ๋„์ˆ˜์— ๋”ฐ๋ผ์„œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. 1์ด๋ผ๋Š” ์ˆซ์ž๋Š” ์ด ๋‹จ์–ด๊ฐ€ ์ด ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 1๋“ฑ์ด๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. 27,595๋ผ๋Š” ์ˆซ์ž๋Š” ์ด ๋‹จ์–ด๊ฐ€ ๋ฐ์ดํ„ฐ์—์„œ 27,595๋ฒˆ์งธ๋กœ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ๋‹จ์–ด๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์‹ค์ œ๋กœ๋Š” ๋นˆ๋„๊ฐ€ ๊ต‰์žฅํžˆ ๋‚ฎ์€ ๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ์•ž์„œ num_words์—๋‹ค๊ฐ€ None์„ ๋ถ€์—ฌํ–ˆ๋Š”๋ฐ, ๋งŒ์•ฝ num_words์— 1,000์„ ๋„ฃ์—ˆ๋‹ค๋ฉด ๋นˆ๋„์ˆ˜ ์ˆœ์œ„๊ฐ€ 1,000 ์ดํ•˜์˜ ๋‹จ์–ด๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ๋ฐ์ดํ„ฐ์—์„œ 1,000์„ ๋„˜๋Š” ์ •์ˆ˜๋Š” ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‰ด์Šค ๊ธฐ์‚ฌ๋“ค์˜ ๋ ˆ์ด๋ธ”๋“ค์„ ์˜๋ฏธํ•˜๋Š” y_train์—์„œ ์ฒซ ๋ฒˆ์งธ ๋‰ด์Šค ๊ธฐ์‚ฌ์˜ ๋ ˆ์ด๋ธ”์ธ y_train[0]์—๋Š” 3์ด๋ผ๋Š” ๊ฐ’์ด ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ˆซ์ž๋Š” ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ๊ฐ€ 46๊ฐœ์˜ ์นดํ…Œ๊ณ ๋ฆฌ ์ค‘ 3์— ํ•ด๋‹นํ•˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ฐฉ๊ธˆ ํ™•์ธํ•œ X_train[0]๊ณผ y_train[0]์€ 8,982๊ฐœ์˜ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋‰ด์Šค ๊ธฐ์‚ฌ์˜ ๋ณธ๋ฌธ๊ณผ ๋ ˆ์ด๋ธ”๋งŒ ํ™•์ธํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” 8,982๊ฐœ์˜ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ์˜ ๊ธธ์ด๊ฐ€ ๋Œ€์ฒด์ ์œผ๋กœ ์–ด๋–ค ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('๋‰ด์Šค ๊ธฐ์‚ฌ์˜ ์ตœ๋Œ€ ๊ธธ์ด :{}'.format(max(len(sample) for sample in X_train))) print('๋‰ด์Šค ๊ธฐ์‚ฌ์˜ ํ‰๊ท  ๊ธธ์ด :{}'.format(sum(map(len, X_train))/len(X_train))) plt.hist([len(sample) for sample in X_train], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋‰ด์Šค ๊ธฐ์‚ฌ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 2376 ๋‰ด์Šค ๊ธฐ์‚ฌ์˜ ํ‰๊ท  ๊ธธ์ด : 145.5398574927633 ๋Œ€์ฒด์ ์œผ๋กœ ๋Œ€๋ถ€๋ถ„์˜ ๋‰ด์Šค๊ฐ€ 100~200 ์‚ฌ์ด์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ฐ ๋‰ด์Šค์˜ ๋ ˆ์ด๋ธ” ๊ฐ’์˜ ๋ถ„ํฌ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. fig, axe = plt.subplots(ncols=1) fig.set_size_inches(12,5) sns.countplot(y_train) 3, 4๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ๋ ˆ์ด๋ธ”์„ ์ฐจ์ง€ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ์ •ํ™•ํ•œ ๊ฐœ์ˆ˜๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. unique_elements, counts_elements = np.unique(y_train, return_counts=True) print("๊ฐ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜:") print(np.asarray((unique_elements, counts_elements))) ๊ฐ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜: [[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45] [ 55 432 74 3159 1949 17 48 16 139 101 124 390 49 172 26 20 444 39 66 549 269 100 15 41 62 92 24 15 48 19 45 39 32 11 50 10 49 19 19 24 36 30 13 21 12 18]] 3๋ฒˆ ๋ ˆ์ด๋ธ”์€ ์ด 3,159๊ฐœ๊ฐ€ ์กด์žฌํ•˜๊ณ  4๋ฒˆ ๋ ˆ์ด๋ธ”์€ ์ด 1,949๊ฐœ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. X_train์— ๋“ค์–ด์žˆ๋Š” ์ˆซ์ž๋“ค์ด ๊ฐ์ž ์–ด๋–ค ๋‹จ์–ด๋“ค์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. reuters.get_word_index๋Š” ๊ฐ ๋‹จ์–ด์™€ ๊ทธ ๋‹จ์–ด์— ๋ถ€์—ฌ๋œ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. word_to_index = reuters.get_word_index() print(word_to_index) {'mdbl': 10996, 'fawc': 16260, 'degussa': 12089, 'woods': 8803, 'hanging': 13796, 'localized': 20672, 'sation': 20673, 'chanthaburi': 20675, 'refunding': 10997, 'hermann': 8804, 'passsengers': 20676, 'stipulate': 20677, 'heublein': 8352, 'screaming': 20713, 'tcby': 16261, 'four': 185, 'grains': 1642, 'broiler': 20680, 'wooden': 12090, 'wednesday': 1220, 'highveld': 13797, 'duffour': 7593, '0053': 20681, 'elections': 3914, '270': 2563, '271': 3551, '272': 5113, '273': 3552, '274': 3400, 'rudman': 7975, '276': 3401, '277': 3478, '278': 3632, '279': 4309, 'dormancy': 9381, - ์ดํ•˜ ์ค‘๋žต -} ๋งŽ์€ ๋‹จ์–ด๊ฐ€ ์ถœ๋ ฅ๋˜๋ฏ€๋กœ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์ค‘๋žตํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. word_to_index์—์„œ key์™€ value๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ €์žฅํ•œ index_to_word๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ reuters.get_word_index()์— ์ €์žฅ๋œ ๊ฐ’์— +3์„ ํ•ด์•ผ ์‹ค์ œ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ •ํ•œ ๊ทœ์น™์ž…๋‹ˆ๋‹ค. index_to_word = {} for key, value in word_to_index.items(): index_to_word[value+3] = key index_to_word[ ]์—๋‹ค๊ฐ€ ์ธ๋ฑ์Šค๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋‹จ์–ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +3์„ ํ–ˆ์œผ๋ฏ€๋กœ ๋นˆ๋„์ˆ˜ 1๋“ฑ์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์ˆซ์ž 4๋ฅผ ๋„ฃ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ๋ถˆ์šฉ์–ด๋กœ ๋ถ„๋ฅ˜๋˜๋Š” the๊ฐ€ ์ด ๋ฐ์ดํ„ฐ์—์„œ๋„ ์–ด๊น€์—†์ด ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๋กœ 1์œ„๋ฅผ ์ฐจ์ง€ํ–ˆ์Šต๋‹ˆ๋‹ค. print('๋นˆ๋„์ˆ˜ ์ƒ์œ„ 1๋ฒˆ ๋‹จ์–ด : {}'.format(index_to_word[4])) ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 1๋ฒˆ ๋‹จ์–ด : the ์ด๋ฒˆ์—๋Š” ์ž„์˜๋กœ ๋นˆ๋„์ˆ˜ 128๋“ฑ ๋‹จ์–ด๋ฅผ ์•Œ์•„๋ด…์‹œ๋‹ค. print('๋นˆ๋„์ˆ˜ ์ƒ์œ„ 128๋“ฑ ๋‹จ์–ด : {}'.format(index_to_word[131])) ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 128๋“ฑ ๋‹จ์–ด : tax index_to_word์—์„œ ์ˆซ์ž 0์€ ํŒจ๋”ฉ์„ ์˜๋ฏธํ•˜๋Š” ํ† ํฐ์ธ pad, ์ˆซ์ž 1์€ ๋ฌธ์žฅ์˜ ์‹œ์ž‘์„ ์˜๋ฏธํ•˜๋Š” sos, ์ˆซ์ž 2๋Š” OOV๋ฅผ ์œ„ํ•œ ํ† ํฐ์ธ unk๋ผ๋Š” ํŠน๋ณ„ ํ† ํฐ์— ๋งคํ•‘๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ index_to_word๋ฅผ ์™„์„ฑํ•ด ์ค๋‹ˆ๋‹ค. ์ด ๋˜ํ•œ ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ •ํ•œ ๊ทœ์น™์ด๋ฏ€๋กœ ๋‚ฉ๋“ํ•˜๊ณ  ๋„˜์–ด๊ฐ‘์‹œ๋‹ค. index_to_word๋ฅผ ์ด์šฉํ•ด์„œ ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ์ธ X_train[0]๊ฐ€ ์–ด๋–ค ๋‹จ์–ด๋“ค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ๋ณต์›ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. X_train[0]์— ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋“ค์„ ํ•˜๋‚˜์”ฉ ๋ถˆ๋Ÿฌ์™€์„œ index_to_word์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์—ฐ๊ฒฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. for index, token in enumerate(("<pad>", "<sos>", "<unk>")): index_to_word[index] = token print(' '.join([index_to_word[index] for index in X_train[0]])) <sos> mcgrath rentcorp said as a result of its december acquisition of space co it expects earnings per share in 1987 of 1 15 to 1 30 dlrs per share up from 70 cts in 1986 the company said pretax net should rise to nine to 10 mln dlrs from six mln dlrs in 1986 and rental operation revenues to 19 to 22 mln dlrs from 12 5 mln dlrs it said cash flow per share this year should be 2 50 to three dlrs reuter 3 ์œ„ ๊ฒฐ๊ณผ๋Š” ๋ณต์›๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „ ์–ด๋Š ์ •๋„ ์ „์ฒ˜๋ฆฌ๊ฐ€ ๋œ ์ƒํƒœ๋ผ์„œ ์ œ๋Œ€๋กœ ๋œ ๋ฌธ์žฅ์ด ๋‚˜์˜ค์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค ๊ตฌ์„ฑ์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. LSTM์œผ๋กœ ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ถ„๋ฅ˜ํ•˜๊ธฐ ํ•™์Šต์—์„œ๋Š” ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ์œ„ ์ƒ์œ„ 1,000๊ฐœ์˜ ๋‹จ์–ด๋“ค๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ฐ์ดํ„ฐ๊ณผ ํ…Œ์ŠคํŠธ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๊ฐ๊ฐ์˜ ๋‰ด์Šค์˜ ๊ธธ์ด๋Š” ์„œ๋กœ ๋‹ค๋ฅด๋ฏ€๋กœ ๋ชจ๋“  ๋‰ด์Šค ๊ธฐ์‚ฌ์˜ ๊ธธ์ด๋ฅผ 100์œผ๋กœ ํŒจ๋”ฉ ํ•ด์ค๋‹ˆ๋‹ค. ์ดํ›„ ํ›ˆ๋ จ์šฉ, ํ…Œ์ŠคํŠธ์šฉ ๋‰ด์Šค ๊ธฐ์‚ฌ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”์— ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, LSTM, Embedding from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint from tensorflow.keras.models import load_model vocab_size = 1000 max_len = 100 (X_train, y_train), (X_test, y_test) = reuters.load_data(num_words=vocab_size, test_split=0.2) X_train = pad_sequences(X_train, maxlen=max_len) X_test = pad_sequences(X_test, maxlen=max_len) y_train = to_categorical(y_train) y_test = to_categorical(y_test) ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 128์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” ์•ž์„œ 1,000์œผ๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€ ์ผ ๊ตฌ์กฐ์˜ LSTM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ 46๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜์˜ ์„ ํƒ์ง€๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 128์ด๋ฉฐ, 30 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4)๋Š” ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค(val_loss)์ด ์ฆ๊ฐ€ํ•˜๋ฉด, ๊ณผ์ ํ•ฉ ์ง•ํ›„๋ฏ€๋กœ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค์ด 4ํšŒ ์ฆ๊ฐ€ํ•˜๋ฉด ์ •ํ•ด์ง„ ์—ํฌํฌ์— ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•˜์—ฌ๋„ ํ•™์Šต์„ ์กฐ๊ธฐ ์ข…๋ฃŒ(Early Stopping) ํ•ฉ๋‹ˆ๋‹ค. ModelCheckpoint๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„(val_acc)๊ฐ€ ์ด์ „๋ณด๋‹ค ์ข‹์•„์งˆ ๊ฒฝ์šฐ์—๋งŒ ๋ชจ๋ธ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. validation_data๋กœ๋Š” X_test์™€ y_test๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. val_loss๊ฐ€ ์ค„์–ด๋“ค๋‹ค๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ƒํ™ฉ์ด ์˜ค๋ฉด ๊ณผ์ ํ•ฉ์œผ๋กœ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. embedding_dim = 128 hidden_units = 128 num_classes = 46 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(LSTM(hidden_units)) model.add(Dense(num_classes, activation='softmax')) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('best_model.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc']) history = model.fit(X_train, y_train, batch_size=128, epochs=30, callbacks=[es, mc], validation_data=(X_test, y_test)) ์ €์ž์˜ ๊ฒฝ์šฐ 21 ์—ํฌํฌ์—์„œ ํ›ˆ๋ จ์ด ์กฐ๊ธฐ ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ์ด ๋‹ค ๋˜์—ˆ๋‹ค๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ€์žฅ ๋†’์•˜์„ ๋•Œ ์ €์žฅ๋œ ๋ชจ๋ธ์ธ 'best_model.h5'๋ฅผ ๋กœ๋“œํ•˜๊ณ , ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. loaded_model = load_model('best_model.h5') print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (loaded_model.evaluate(X_test, y_test)[1])) 2246/2246 [==============================] - 1s 656us/sample - loss: 1.2355 - acc: 0.7124 ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.7124 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋Š” 71.24%์ž…๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์˜ model.fit()์—์„œ validation_data๋Š” ์‹ค์ œ ๊ธฐ๊ณ„๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จํ•˜์ง€๋Š” ์•Š๊ณ  ์—ํฌํฌ๋งˆ๋‹ค ์ •ํ™•๋„์™€ loss๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ๊ณผ์ ํ•ฉ์„ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ๋งŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ validation_data์—์„œ ์ด๋ฏธ X_test, y_test๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ๊ธฐ๊ณ„๋Š” ์ด ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šตํ•œ ์ ์ด ์—†์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ํ•™์Šตํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ์ธ X_test, y_test๋ฅผ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ์„œ ์„ฑ๋Šฅ ํ‰๊ฐ€ ์šฉ๋„๋กœ model.evaluate()์—์„œ๋„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๋ชจ๋ธ์€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋™์ผํ•œ ์…ˆ์ž…๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋ฐ์ดํ„ฐ๊ฐ€ ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” ๋‹ค๋ฅด๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‹ค์Šต์—์„œ๋Š” validation_data ๋Œ€์‹  validation_split=0.2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Œ์„ ์ƒ๊ธฐํ•ฉ์‹œ๋‹ค. ์—ํฌํฌ๋งˆ๋‹ค ๋ณ€ํ™”ํ•˜๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ(ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ)์˜ ์†์‹ค์„ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. epochs = range(1, len(history.history['acc']) + 1) plt.plot(epochs, history.history['loss']) plt.plot(epochs, history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.show() ์ „์ฒด์ ์œผ๋กœ๋Š” ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์†์‹ค์ด ์ค„์–ด๋“œ๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์ง€๋งŒ ๋’ค๋กœ ๊ฐˆ์ˆ˜๋ก ์ ์ฐจ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์†์‹ค์ด ์ฆ๊ฐ€ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฒฝํ–ฅ์ด ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๊ณผ์  ํ•ฉ์˜ ์‹ ํ˜ธ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 10-04 IMDB ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ(IMDB Movie Review Sentiment Analysis) ๊ฐ์„ฑ ๋ถ„๋ฅ˜๋ฅผ ์—ฐ์Šตํ•˜๊ธฐ ์œ„ํ•ด ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์˜์–ด ๋ฐ์ดํ„ฐ๋กœ ์˜ํ™” ์‚ฌ์ดํŠธ IMDB์˜ ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ๋ฆฌ๋ทฐ์— ๋Œ€ํ•œ ํ…์ŠคํŠธ์™€ ํ•ด๋‹น ๋ฆฌ๋ทฐ๊ฐ€ ๊ธ์ •์ธ ๊ฒฝ์šฐ 1์„ ๋ถ€์ •์ธ ๊ฒฝ์šฐ 0์œผ๋กœ ํ‘œ์‹œํ•œ ๋ ˆ์ด๋ธ”๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™๊ต์—์„œ 2011๋…„์— ๋‚ธ ๋…ผ๋ฌธ์—์„œ ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์†Œ๊ฐœํ•˜์˜€์œผ๋ฉฐ, ๋‹น์‹œ ๋…ผ๋ฌธ์—์„œ๋Š” ์ด ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 50:50๋Œ€ ๋น„์œจ๋กœ ๋ถ„ํ• ํ•˜์—ฌ 88.89%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์—ˆ๋‹ค๊ณ  ์†Œ๊ฐœํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฌธ ๋งํฌ : http://ai.stanford.edu/~amaas/papers/wvSent_acl2011.pdf ์ผ€๋ผ์Šค์—์„œ๋Š” ํ•ด๋‹น IMDB ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ imdb.load_data() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ”๋กœ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜์—ฌ ๊ฐ์„ฑ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.datasets import imdb ์ผ€๋ผ์Šค ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ๋ถ€ํ„ฐ imdb.data_load()๋ฅผ ํ†ตํ•ด ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋Š” ์•ž์„œ ๋ฐฐ์šด ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ฐ์ดํ„ฐ์—์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ๋น„์œจ์„ ์กฐ์ ˆํ–ˆ๋˜ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ด๋ฏธ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 50:50 ๋น„์œจ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ฐ์ดํ„ฐ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ test_split๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ์„ ์กฐ์ ˆํ•˜๋Š” ์ธ์ž๋Š” imdb.load_data์—์„œ๋Š” ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. imdb.data_load()์˜ ์ธ์ž๋กœ num_words๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ์œ„๋กœ ๋ช‡ ๋“ฑ๊นŒ์ง€์˜ ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ 10,000์„ ๋„ฃ์œผ๋ฉด, ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ์œ„๊ฐ€ 1~10,000์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋งŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 10,000์ด ๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ๋ณ„๋„๋กœ ์ œํ•œํ•˜์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜, ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜, ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (X_train, y_train), (X_test, y_test) = imdb.load_data() print('ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : {}'.format(len(X_train))) print('ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : {}'.format(len(X_test))) num_classes = len(set(y_train)) print('์นดํ…Œ๊ณ ๋ฆฌ : {}'.format(num_classes)) ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 25000 ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 25000 ์นดํ…Œ๊ณ ๋ฆฌ : 2 ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ๋Š” 25,000๊ฐœ, ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ๋Š” 25,000๊ฐœ, ์นดํ…Œ๊ณ ๋ฆฌ๋Š” 2๊ฐœ์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์™€ ๋ ˆ์ด๋ธ”์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. print('์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ :',X_train[0]) print('์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๋ ˆ์ด๋ธ” :',y_train[0]) ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ : [1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941, 4, 173, 36, 256, 5, 25, 100, 43, 838, 112, 50, 670, 22665, 9, 35, 480, 284, 5, 150, 4, 172, 112, 167, 21631, 336, 385, 39, 4, 172, 4536, 1111, 17, 546, 38, 13, 447, 4, 192, 50, 16, 6, 147, 2025, 19, 14, 22, 4, 1920, 4613, 469, 4, 22, 71, 87, 12, 16, 43, 530, 38, 76, 15, 13, 1247, 4, 22, 17, 515, 17, 12, 16, 626, 18, 19193, 5, 62, 386, 12, 8, 316, 8, 106, 5, 4, 2223, 5244, 16, 480, 66, 3785, 33, 4, 130, 12, 16, 38, 619, 5, 25, 124, 51, 36, 135, 48, 25, 1415, 33, 6, 22, 12, 215, 28, 77, 52, 5, 14, 407, 16, 82, 10311, 8, 4, 107, 117, 5952, 15, 256, 4, 31050, 7, 3766, 5, 723, 36, 71, 43, 530, 476, 26, 400, 317, 46, 7, 4, 12118, 1029, 13, 104, 88, 4, 381, 15, 297, 98, 32, 2071, 56, 26, 141, 6, 194, 7486, 18, 4, 226, 22, 21, 134, 476, 26, 480, 5, 144, 30, 5535, 18, 51, 36, 28, 224, 92, 25, 104, 4, 226, 65, 16, 38, 1334, 88, 12, 16, 283, 5, 16, 4472, 113, 103, 32, 15, 16, 5345, 19, 178, 32] ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๋ ˆ์ด๋ธ” : 1 ๋ฆฌ๋ทฐ ๋ณธ๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” X_train[0]์—๋Š” ์ˆซ์ž๋“ค์ด ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ํ† ํฐํ™”์™€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด๋ผ๋Š” ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ ๋‹จ์–ด๋“ค์˜ ๋“ฑ์žฅ ๋นˆ๋„์— ๋”ฐ๋ผ์„œ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ๋‚ฎ์„์ˆ˜๋ก ์ด ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ์œ„๊ฐ€ ๋†’์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ œํ•œํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— 22,665์™€ ๊ฐ™์€ ํฐ ์ˆซ์ž๋„ ๋ณด์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” y_train[0]์˜ ๊ฐ’์€ 1์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ๊ฐ์„ฑ ์ •๋ณด๋กœ์„œ 0 ๋˜๋Š” 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š”๋ฐ ๊ธ์ •์€ 1์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 25,000๊ฐœ์˜ ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐ ๊ธธ์ด๋Š” ์ „๋ถ€ ๋‹ค๋ฅธ๋ฐ, ๋ฆฌ๋ทฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. reviews_length = [len(review) for review in X_train] print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : {}'.format(np.max(reviews_length))) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : {}'.format(np.mean(reviews_length))) plt.subplot(1,2,1) plt.boxplot(reviews_length) plt.subplot(1,2,2) plt.hist(reviews_length, bins=50) plt.show() ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 2494 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 238.71364 ๋Œ€์ฒด์ ์œผ๋กœ 1,000์ดํ•˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋ฉฐ, ํŠนํžˆ 100~500๊ธธ์ด๋ฅผ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๊ฐ€์žฅ ๊ธด ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋Š” ๊ธธ์ด๊ฐ€ 2,000์ด ๋„˜๋Š” ๊ฒƒ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์˜ ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. unique_elements, counts_elements = np.unique(y_train, return_counts=True) print("๊ฐ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜:") print(np.asarray((unique_elements, counts_elements))) ๊ฐ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜: [[ 0 1] [12500 12500]] 25,000๊ฐœ์˜ ๋ฆฌ๋ทฐ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ ๋‘ ๋ ˆ์ด๋ธ” 0๊ณผ 1์€ ๊ฐ๊ฐ 12,500๊ฐœ๋กœ ๊ท ๋“ฑํ•œ ๋ถ„ํฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. X_train์— ๋“ค์–ด์žˆ๋Š” ์ˆซ์ž๋“ค์ด ๊ฐ๊ฐ ์–ด๋–ค ๋‹จ์–ด๋“ค์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. imdb.get_word_index()์— ๊ฐ ๋‹จ์–ด์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๊ฐ€ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ imdb.get_word_index()์— ์ €์žฅ๋œ ๊ฐ’์— +3์„ ํ•ด์•ผ ์‹ค์ œ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ ์…‹์—์„œ ์ •ํ•œ ๊ทœ์น™์ž…๋‹ˆ๋‹ค. word_to_index = imdb.get_word_index() index_to_word = {} for key, value in word_to_index.items(): index_to_word[value+3] = key index_to_word์— ์ธ๋ฑ์Šค๋ฅผ ์ง‘์–ด๋„ฃ์œผ๋ฉด ์ „์ฒ˜๋ฆฌ ์ „์— ์–ด๋–ค ๋‹จ์–ด์˜€๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ ์…‹์—์„œ๋Š” 0, 1, 2, 3์€ ํŠน๋ณ„ ํ† ํฐ์œผ๋กœ ์ทจ๊ธ‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ •์ˆ˜ 4๋ถ€ํ„ฐ๊ฐ€ ์‹ค์ œ IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ ์…‹์—์„œ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์‹ค์ œ ์˜๋‹จ์–ด์ž…๋‹ˆ๋‹ค. print('๋นˆ๋„์ˆ˜ ์ƒ์œ„ 1๋“ฑ ๋‹จ์–ด : {}'.format(index_to_word[4])) ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 1๋“ฑ ๋‹จ์–ด : the print('๋นˆ๋„์ˆ˜ ์ƒ์œ„ 3938๋“ฑ ๋‹จ์–ด : {}'.format(index_to_word[3941])) ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 3938๋“ฑ ๋‹จ์–ด : suited ์ด ๋ฐ์ดํ„ฐ์—์„œ ๋นˆ๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๋‹จ์–ด๋Š” the์ด๊ณ , ๋นˆ๋„๊ฐ€ 3938๋ฒˆ์งธ๋กœ ๋†’์€ ๋‹จ์–ด๋Š” suited์ž…๋‹ˆ๋‹ค. for index, token in enumerate(("<pad>", "<sos>", "<unk>")): index_to_word[index] = token print(' '.join([index_to_word[index] for index in X_train[0]])) ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ X_train[0]์˜ ๊ฐ ๋‹จ์–ด๊ฐ€ ์ •์ˆ˜๋กœ ๋ฐ”๋€Œ๊ธฐ ์ „์— ์–ด๋–ค ๋‹จ์–ด๋“ค์ด์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. <sos> this film was just brilliant casting location scenery story direction everyone's really suited the part they played and you could just imagine being there robert <unk> is an amazing actor and now the same being director <unk> father came from the same scottish island as myself so i loved the fact there was a real connection with this film the witty remarks throughout the film were great it was just brilliant so much that i bought the film as soon as it was released for <unk> and would recommend it to everyone to watch and the fly fishing was amazing really cried at the end it was so sad and you know what they say if you cry at a film it must have been good and this definitely was also <unk> to the two little boy's that played the <unk> of norman and paul they were just brilliant children are often left out of the <unk> list i think because the stars that play them all grown up are such a big profile for the whole film but these children are amazing and should be praised for what they have done don't you think the whole story was so lovely because it was true and was someone's life after all that was shared with us all 2. GRU๋กœ IMDB ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ 10,000์œผ๋กœ ์ œํ•œํ•˜๊ณ , ๋ฆฌ๋ทฐ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 500์œผ๋กœ ์ œํ•œํ•˜์—ฌ ํŒจ๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. import re from tensorflow.keras.datasets import imdb from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, GRU, Embedding from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint from tensorflow.keras.models import load_model vocab_size = 10000 max_len = 500 (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=vocab_size) X_train = pad_sequences(X_train, maxlen=max_len) X_test = pad_sequences(X_test, maxlen=max_len) ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 100, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 128์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€ ์ผ ๊ตฌ์กฐ์˜ GRU๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 64์ด๋ฉฐ, 15 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4)๋Š” ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค(val_loss)์ด ์ฆ๊ฐ€ํ•˜๋ฉด, ๊ณผ์ ํ•ฉ ์ง•ํ›„๋ฏ€๋กœ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค์ด 4ํšŒ ์ฆ๊ฐ€ํ•˜๋ฉด ์ •ํ•ด์ง„ ์—ํฌํฌ๊ฐ€ ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•˜์˜€๋”๋ผ๋„ ํ•™์Šต์„ ์กฐ๊ธฐ ์ข…๋ฃŒ(Early Stopping) ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ModelCheckpoint๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„(val_acc)๊ฐ€ ์ด์ „๋ณด๋‹ค ์ข‹์•„์งˆ ๊ฒฝ์šฐ์—๋งŒ ๋ชจ๋ธ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. validation_split=0.2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 20%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ , ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ํ›ˆ๋ จ์ด ์ ์ ˆํžˆ ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. embedding_dim = 100 hidden_units = 128 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(GRU(hidden_units)) model.add(Dense(1, activation='sigmoid')) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('GRU_model.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc']) history = model.fit(X_train, y_train, epochs=15, callbacks=[es, mc], batch_size=64, validation_split=0.2) ์ €์ž์˜ ๊ฒฝ์šฐ, ์กฐ๊ธฐ ์ข…๋ฃŒ ์กฐ๊ฑด์— ๋”ฐ๋ผ์„œ ์—ํฌํฌ 9์—์„œ ์กฐ๊ธฐ ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ์ด ๋‹ค ๋˜์—ˆ๋‹ค๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•๋„๋ฅผ ํ‰๊ฐ€ํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์•˜์„ ๋•Œ ์ €์žฅ๋œ ๋ชจ๋ธ์ธ 'GRU_model.h5'๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. loaded_model = load_model('GRU_model.h5') print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (loaded_model.evaluate(X_test, y_test)[1])) 25000/25000 [==============================] - 61s 2ms/sample - loss: 0.3440 - acc: 0.8893 ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.8893 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•๋„ 88.93%๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ž„์˜์˜ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋ฆฌ๋ทฐ์˜ ๊ธ, ๋ถ€์ •์„ ์˜ˆ์ธกํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ๋ชจ๋ธ์— ๋„ฃ๊ธฐ ์ „์— ์ž„์˜์˜ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. sentiment_predict์€ ์ž…๋ ฅ๋œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๊ธฐ๋ณธ์ ์ธ ์ „์ฒ˜๋ฆฌ์™€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ, ํŒจ๋”ฉ์„ ํ•œ ํ›„์— ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ธก๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. def sentiment_predict(new_sentence): # ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐ ๋ฐ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์žํ™” new_sentence = re.sub('[^0-9a-zA-Z ]', '', new_sentence).lower() encoded = [] # ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„ ํ† ํฐํ™” ํ›„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ for word in new_sentence.split(): try : # ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ 10,000์œผ๋กœ ์ œํ•œ. if word_to_index[word] <= 10000: encoded.append(word_to_index[word]+3) else: # 10,000 ์ด์ƒ์˜ ์ˆซ์ž๋Š” <unk> ํ† ํฐ์œผ๋กœ ๋ณ€ํ™˜. encoded.append(2) # ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๋Š” <unk> ํ† ํฐ์œผ๋กœ ๋ณ€ํ™˜. except KeyError: encoded.append(2) pad_sequence = pad_sequences([encoded], maxlen=max_len) score = float(loaded_model.predict(pad_sequence)) # ์˜ˆ์ธก if(score > 0.5): print("{:.2f}% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค.".format(score * 100)) else: print("{:.2f}% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค.".format((1 - score) * 100)) IMDB ์‚ฌ์ดํŠธ์— ์ ‘์†ํ•ด์„œ ์˜ํ™” ๋ธ”๋ž™ ํŒฌ์„œ์˜ 1์  ๋ฆฌ๋ทฐ๋ฅผ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ๋ถ€์ •์œผ๋กœ ์˜ˆ์ธกํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. test_input = "This movie was just way too overrated. The fighting was not professional and in slow motion. I was expecting more from a 200 million budget movie. The little sister of T.Challa was just trying too hard to be funny. The story was really dumb as well. Don't watch this movie if you are going because others say its great unless you are a Black Panther fan or Marvels fan." sentiment_predict(test_input) 97.43% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. IMDB ์‚ฌ์ดํŠธ์— ์ ‘์†ํ•ด์„œ ์˜ํ™” ์–ด๋ฒค์ €์Šค์˜ 10์  ๋ฆฌ๋ทฐ๋ฅผ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ๊ธ์ •์œผ๋กœ ์˜ˆ์ธกํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. test_input = " I was lucky enough to be included in the group to see the advanced screening in Melbourne on the 15th of April, 2012. And, firstly, I need to say a big thank-you to Disney and Marvel Studios. \ Now, the film... how can I even begin to explain how I feel about this film? It is, as the title of this review says a 'comic book triumph'. I went into the film with very, very high expectations and I was not disappointed. \ Seeing Joss Whedon's direction and envisioning of the film come to life on the big screen is perfect. The script is amazingly detailed and laced with sharp wit a humor. The special effects are literally mind-blowing and the action scenes are both hard-hitting and beautifully choreographed." sentiment_predict(test_input) 98.95% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. 10-05 ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ(Naive Bayes Classifier) ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด ์ „ํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ถ„๋ฅ˜๊ธฐ๋กœ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์•Œ๊ณ ๋ฆฌ์ฆ˜์—๋Š” ์†ํ•˜์ง€ ์•Š์ง€๋งŒ, ๋จธ์‹  ๋Ÿฌ๋‹์˜ ์ฃผ์š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ถ„๋ฅ˜์— ์žˆ์–ด ์ค€์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. 1) ๋ฒ ์ด์ฆˆ์˜ ์ •๋ฆฌ(Bayes' theorem)๋ฅผ ์ด์šฉํ•œ ๋ถ„๋ฅ˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  ๋ฒ ์ด์ฆˆ์˜ ์ •๋ฆฌ(Bayes' theorem)๋ฅผ ์ดํ•ดํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒ ์ด์ฆˆ ์ •๋ฆฌ๋Š” ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ( ) ๊ฐ€ A๊ฐ€ ์ผ์–ด๋‚  ํ™•๋ฅ , ( ) ๊ฐ€ B๊ฐ€ ์ผ์–ด๋‚  ํ™•๋ฅ , ( | ) ๊ฐ€ A๊ฐ€ ์ผ์–ด๋‚˜๊ณ  ๋‚˜์„œ B๊ฐ€ ์ผ์–ด๋‚  ํ™•๋ฅ , ( | ) ๊ฐ€ B๊ฐ€ ์ผ์–ด๋‚˜๊ณ  ๋‚˜์„œ A๊ฐ€ ์ผ์–ด๋‚  ํ™•๋ฅ ์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋•Œ ( | ) ๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์€ ์‹์„ ํ†ตํ•ด ( | ) ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( | ) P ( | ) ( ) ( ) ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ๋Š” ์ด๋Ÿฌํ•œ ๋ฒ ์ด์ฆˆ ์ •๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ํ†ตํ•ด์„œ ์ŠคํŒธ ๋ฉ”์ผ ํ•„ํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณธ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ž…๋ ฅ ํ…์ŠคํŠธ(๋ฉ”์ผ์˜ ๋ณธ๋ฌธ)์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ž…๋ ฅ ํ…์ŠคํŠธ๊ฐ€ ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ํ™•๋ฅ ์„ ์ด์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. P(์ •์ƒ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = ์ž…๋ ฅ ํ…์ŠคํŠธ๊ฐ€ ์žˆ์„ ๋•Œ ์ •์ƒ ๋ฉ”์ผ์ผ ํ™•๋ฅ  P(์ŠคํŒธ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = ์ž…๋ ฅ ํ…์ŠคํŠธ๊ฐ€ ์žˆ์„ ๋•Œ ์ŠคํŒธ ๋ฉ”์ผ์ผ ํ™•๋ฅ  ์ด๋ฅผ ๋ฒ ์ด์ฆˆ์˜ ์ •๋ฆฌ์— ๋”ฐ๋ผ์„œ ์‹์„ ํ‘œํ˜„ํ•˜๋ฉด ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. P(์ •์ƒ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = (P(์ž…๋ ฅ ํ…์ŠคํŠธ | ์ •์ƒ ๋ฉ”์ผ) ร— P(์ •์ƒ ๋ฉ”์ผ)) / P(์ž…๋ ฅ ํ…์ŠคํŠธ) P(์ŠคํŒธ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = (P(์ž…๋ ฅ ํ…์ŠคํŠธ | ์ŠคํŒธ ๋ฉ”์ผ) ร— P(์ŠคํŒธ ๋ฉ”์ผ)) / P(์ž…๋ ฅ ํ…์ŠคํŠธ) ์ž…๋ ฅ ํ…์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, P(์ •์ƒ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ)๊ฐ€ P(์ŠคํŒธ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ)๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์ •์ƒ ๋ฉ”์ผ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ ๋ฐ˜๋Œ€๋ผ๋ฉด ์ŠคํŒธ ๋ฉ”์ผ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‘ ํ™•๋ฅ  ๋ชจ๋‘ ์‹์„ ๋ณด๋ฉด P(์ž…๋ ฅ ํ…์ŠคํŠธ)๋ฅผ ๋ถ„๋ชจ๋กœ ํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„๋ชจ๋ฅผ ์–‘์ชฝ์—์„œ ์ œ๊ฑฐํ•˜์—ฌ ์‹์„ ๊ฐ„์†Œํ™”ํ•ฉ๋‹ˆ๋‹ค. P(์ •์ƒ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = P(์ž…๋ ฅ ํ…์ŠคํŠธ | ์ •์ƒ ๋ฉ”์ผ) ร— P(์ •์ƒ ๋ฉ”์ผ) P(์ŠคํŒธ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = P(์ž…๋ ฅ ํ…์ŠคํŠธ | ์ŠคํŒธ ๋ฉ”์ผ) ร— P(์ŠคํŒธ ๋ฉ”์ผ) ์ž…๋ ฅ ํ…์ŠคํŠธ๋Š” ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์„ ์–ด๋–ป๊ฒŒ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์„ ๋‹จ์–ด ํ† ํฐํ™”ํ•˜์—ฌ ์ด ๋‹จ์–ด๋“ค์„ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ์˜ ๋ถ„๋ฅ˜๊ธฐ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์— ์žˆ๋Š” ๋‹จ์–ด๊ฐ€ 3๊ฐœ๋ผ๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ๋Š” ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ๋…๋ฆฝ์ ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์— ์žˆ๋Š” ๋‹จ์–ด 3๊ฐœ๋ฅผ 1 w, 3 ๋ผ๊ณ  ํ‘œํ˜„ํ•œ๋‹ค๋ฉด ๊ฒฐ๊ตญ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ์˜ ์ •์ƒ ๋ฉ”์ผ์ผ ํ™•๋ฅ ๊ณผ ์ŠคํŒธ ๋ฉ”์ผ์ผ ํ™•๋ฅ ์„ ๊ตฌํ•˜๋Š” ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. P(์ •์ƒ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = P( 1 | ์ •์ƒ ๋ฉ”์ผ) ร— P( 2 | ์ •์ƒ ๋ฉ”์ผ) ร— P( 3 | ์ •์ƒ ๋ฉ”์ผ) ร— P(์ •์ƒ ๋ฉ”์ผ) P(์ŠคํŒธ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = P( 1 | ์ŠคํŒธ ๋ฉ”์ผ) ร— P( 2 | ์ŠคํŒธ ๋ฉ”์ผ) ร— P( 3 | ์ŠคํŒธ ๋ฉ”์ผ) ร— P(์ŠคํŒธ ๋ฉ”์ผ) ์‹์„ ๋ณด๊ณ  ๋ˆˆ์น˜์ฑ„์‹  ๋ถ„๋“ค๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ์—์„œ ํ† ํฐํ™” ์ด์ „์˜ ๋‹จ์–ด์˜ ์ˆœ์„œ๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, BoW์™€ ๊ฐ™์ด ๋‹จ์–ด์˜ ์ˆœ์„œ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ์˜ค์ง ๋นˆ๋„์ˆ˜๋งŒ์„ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์‹ค์ œ ๋‹จ์–ด๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ ํ™•๋ฅ ์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2) ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ(Spam Detection) ์•ž์„œ ๋ฐฐ์šด ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜์‹์„ ๊ฐ€์ง€๊ณ , ์ž…๋ ฅ ํ…์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ํ•ด๋‹น ํ…์ŠคํŠธ๊ฐ€ ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ์ž‘์—…์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฉ”์ผ๋กœ๋ถ€ํ„ฐ ํ† ํฐํ™” ๋ฐ ์ •์ œ๋œ ๋‹จ์–ด๋“ค ๋ถ„๋ฅ˜ 1 me free lottery ์ŠคํŒธ ๋ฉ”์ผ 2 free get free you ์ŠคํŒธ ๋ฉ”์ผ 3 you free scholarship ์ •์ƒ ๋ฉ”์ผ 4 free to contact me ์ •์ƒ ๋ฉ”์ผ 5 you won award ์ •์ƒ ๋ฉ”์ผ 6 you ticket lottery ์ŠคํŒธ ๋ฉ”์ผ ์ด๋•Œ you free lottery๋ผ๋Š” ์ž…๋ ฅ ํ…์ŠคํŠธ์— ๋Œ€ํ•ด์„œ ์ •์ƒ ๋ฉ”์ผ์ผ ํ™•๋ฅ ๊ณผ ์ŠคํŒธ ๋ฉ”์ผ์ผ ํ™•๋ฅ  ๊ฐ๊ฐ์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. P(์ •์ƒ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = P(you | ์ •์ƒ ๋ฉ”์ผ) ร— P(free | ์ •์ƒ ๋ฉ”์ผ) ร— P(lottery | ์ •์ƒ ๋ฉ”์ผ) ร— P(์ •์ƒ ๋ฉ”์ผ) P(์ŠคํŒธ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = P(you | ์ŠคํŒธ ๋ฉ”์ผ) ร— P(free | ์ŠคํŒธ ๋ฉ”์ผ) ร— P(lottery | ์ŠคํŒธ ๋ฉ”์ผ) ร— P(์ŠคํŒธ ๋ฉ”์ผ) P(์ •์ƒ ๋ฉ”์ผ) = P(์ŠคํŒธ ๋ฉ”์ผ) = ์ด ๋ฉ”์ผ 6๊ฐœ ์ค‘ 3๊ฐœ = 0.5 ์œ„ ์˜ˆ์ œ์—์„œ๋Š” P(์ •์ƒ ๋ฉ”์ผ)๊ณผ P(์ŠคํŒธ ๋ฉ”์ผ)์˜ ๊ฐ’์€ ๊ฐ™์œผ๋ฏ€๋กœ, ๋‘ ์‹์—์„œ ๋‘ ๊ฐœ์˜ ํ™•๋ฅ ์€ ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. P(์ •์ƒ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = P(you | ์ •์ƒ ๋ฉ”์ผ) ร— P(free | ์ •์ƒ ๋ฉ”์ผ) ร— P(lottery | ์ •์ƒ ๋ฉ”์ผ) P(์ŠคํŒธ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = P(you | ์ŠคํŒธ ๋ฉ”์ผ) ร— P(free | ์ŠคํŒธ ๋ฉ”์ผ) ร— P(lottery | ์ŠคํŒธ ๋ฉ”์ผ) P(you | ์ •์ƒ ๋ฉ”์ผ)์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ •์ƒ ๋ฉ”์ผ์— ๋“ฑ์žฅํ•œ ๋ชจ๋“  ๋‹จ์–ด์˜ ๋นˆ๋„ ์ˆ˜์˜ ์ดํ•ฉ์„ ๋ถ„๋ชจ๋กœ ํ•˜๊ณ , ์ •์ƒ ๋ฉ”์ผ์—์„œ you๊ฐ€ ์ด ๋“ฑ์žฅํ•œ ๋นˆ๋„์˜ ์ˆ˜๋ฅผ ๋ถ„์ž๋กœ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” 2/10 = 0.2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ์›๋ฆฌ๋กœ ์‹์„ ์ „๊ฐœํ•˜๋ฉด ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. P(์ •์ƒ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = 2/10 ร— 2/10 ร— 0/10 = 0 P(์ŠคํŒธ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) = 2/10 ร— 3/10 ร— 2/10 = 0.012 ๊ฒฐ๊ณผ์ ์œผ๋กœ P(์ •์ƒ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ) < P(์ŠคํŒธ ๋ฉ”์ผ | ์ž…๋ ฅ ํ…์ŠคํŠธ)์ด๋ฏ€๋กœ ์ž…๋ ฅ ํ…์ŠคํŠธ you free lottery๋Š” ์ŠคํŒธ ๋ฉ”์ผ๋กœ ๋ถ„๋ฅ˜๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์˜ˆ์ œ๋ฅผ ๋ณด๋‹ˆ ์ด์ƒํ•œ ์ ์ด ๋ณด์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์ง๊ด€์ ์œผ๋กœ ๋ณด๊ธฐ์—๋„ you, free, lottery๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ŠคํŒธ ๋ฉ”์ผ์—์„œ ๋นˆ๋„์ˆ˜๊ฐ€ ๋” ๋†’๊ธฐ ๋•Œ๋ฌธ์— ์ŠคํŒธ ๋ฉ”์ผ์ธ ํ™•๋ฅ ์ด ๋” ๋†’์€ ๊ฒƒ์€ ํ™•์‹คํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ž…๋ ฅ ํ…์ŠคํŠธ์— ๋Œ€ํ•ด์„œ ๋‹จ, ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ผ๋„ ํ›ˆ๋ จ ํ…์ŠคํŠธ์— ์—†์—ˆ๋‹ค๋ฉด ํ™•๋ฅ  ์ „์ฒด๊ฐ€ 0์ด ๋˜๋Š” ๊ฒƒ์€ ์ง€๋‚˜์นœ ์ผ๋ฐ˜ํ™”์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ์ •์ƒ ๋ฉ”์ผ์— lottery๊ฐ€ ๋‹จ ํ•œ ๋ฒˆ๋„ ๋“ฑ์žฅํ•˜์ง€ ์•Š์•˜๊ณ , ๊ทธ ์ด์œ ๋กœ ์ •์ƒ ๋ฉ”์ผ์ผ ํ™•๋ฅ  ์ž์ฒด๊ฐ€ 0%๊ฐ€ ๋˜์–ด๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ์—์„œ๋Š” ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ํ™•๋ฅ ์˜ ๋ถ„๋ชจ, ๋ถ„์ž์— ์ „๋ถ€ ์ˆซ์ž๋ฅผ ๋”ํ•ด์„œ ๋ถ„์ž๊ฐ€ 0์ด ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๋ผํ”Œ๋ผ์Šค ์Šค๋ฌด๋”ฉ์„ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 3) ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ(Classification of 20 News Group with Naive Bayes Classifier) ์‚ฌ์ดํ‚ท ๋Ÿฐ์—์„œ๋Š” Twenty Newsgroups์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” 20๊ฐœ์˜ ๋‹ค๋ฅธ ์ฃผ์ œ๋ฅผ ๊ฐ€์ง„ 18,846๊ฐœ์˜ ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. (ํ† ํ”ฝ ๋ชจ๋ธ๋ง์˜ LSA ์ฑ•ํ„ฐ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐ์ดํ„ฐ์™€ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ.) ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ์ด๋ฏธ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ(11,314๊ฐœ)์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ(7,532๊ฐœ)๋ฅผ ๋ฏธ๋ฆฌ ๋ถ„๋ฅ˜ํ•ด๋†“์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฅ˜ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ํ›ˆ๋ จ์„ ํ•ด์„œ ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ , ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์˜ˆ์ธกํ–ˆ์„ ๋•Œ์˜ ์ •ํ™•๋„๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (1) ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ์ด 6๊ฐœ์˜ ์†์„ฑ์„ ๊ฐ–๊ณ  ์žˆ๋Š”๋ฐ, ๊ทธ์ค‘์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ๊ฒƒ์€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ์˜ ๋ณธ๋ฌธ์„ ๊ฐ–๊ณ  ์žˆ๋Š” 'data' ์†์„ฑ๊ณผ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ•˜๋Š”์ง€ 0๋ถ€ํ„ฐ 19๊นŒ์ง€์˜ ๋ผ๋ฒจ์ด ๋ถ™์–ด์žˆ๋Š” 'target' ์†์„ฑ์ด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์„ฑ์„ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from sklearn.datasets import fetch_20newsgroups newsdata=fetch_20newsgroups(subset='train') print(newsdata.keys()) ์œ„์˜ ์ฝ”๋“œ ๋ถ€๋ถ„์— subset ๋ถ€๋ถ„์— 'all'์„ ๋„ฃ์œผ๋ฉด 18,846๊ฐœ์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, 'train'์„ ๋„ฃ์œผ๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ, 'test'๋ฅผ ๋„ฃ์œผ๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. newsdata.keys()๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค ์†์„ฑ์œผ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. dict_keys(['data', 'filenames', 'target_names', 'target', 'DESCR', 'description']) ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” data, filenames, target_names, target, DESCR, description์ด๋ผ๋Š” 6๊ฐœ ์†์„ฑ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. print (len(newsdata.data), len(newsdata.filenames), len(newsdata.target_names), len(newsdata.target)) ํ›ˆ๋ จ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 11314 11314 20 11314 ํ›ˆ๋ จ์šฉ ์ƒ˜ํ”Œ์€ ์ด 11,314๊ฐœ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. newsdata.target_names๋Š” ์ด ๋ฐ์ดํ„ฐ์˜ 20๊ฐœ์˜ ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์ด๋ฆ„์„ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ์นดํ…Œ๊ณ ๋ฆฌ๋“ค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(newsdata.target_names) ['alt.atheism', 'comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware', 'comp.windows.x', 'misc.forsale', 'rec.autos', 'rec.motorcycles', 'rec.sport.baseball', 'rec.sport.hockey', 'sci.crypt', 'sci.electronics', 'sci.med', 'sci.space', 'soc.religion.christian', 'talk.politics.guns', 'talk.politics.mideast', 'talk.politics.misc', 'talk.religion.misc'] target์—๋Š” ์ด 0๋ถ€ํ„ฐ 19๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ช‡ ๋ฒˆ ์นดํ…Œ๊ณ ๋ฆฌ์ธ์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(newsdata.target[0]) ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์€ ์นดํ…Œ๊ณ ๋ฆฌ 7๋ฒˆ์— ์†ํ•œ๋‹ค๊ณ  ๋ผ๋ฒจ์ด ๋ถ™์–ด์žˆ์Šต๋‹ˆ๋‹ค. print(newsdata.target_names[7]) rec.autos 7๋ฒˆ ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์นดํ…Œ๊ณ ๋ฆฌ ์ œ๋ชฉ์€ rec.autos์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์€ rec.autos ์นดํ…Œ๊ณ ๋ฆฌ์— ์†ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์ด ์–ด๋–ค ๋‚ด์šฉ์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(newsdata.data[0]) From: lerxst@wam.umd.edu (where's my thing) Subject: WHAT car is this!? Nntp-Posting-Host: rac3.wam.umd.edu Organization: University of Maryland, College Park Lines: 15 I was wondering if anyone out there could enlighten me on this car I saw the other day. It was a 2-door sports car, looked to be from the late 60s/ early 70s. It was called a Bricklin. The doors were really small. In addition, the front bumper was separate from the rest of the body. This is all I know. If anyone can tellme a model name, engine specs, years of production, where this car is made, history, or whatever info you have on this funky looking car, please e-mail. Thanks, - IL ---- brought to you by your neighborhood Lerxst ---- ๋ฉ”์ผ์˜ ๋‚ด์šฉ์„ ๋ณด๋‹ˆ ์Šคํฌ์ธ ์นด์— ๋Œ€ํ•œ ๊ธ€๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ด ์Šคํฌ์ธ ์นด์— ๋Œ€ํ•œ ๊ธ€์€ ์ด 0๋ถ€ํ„ฐ 19๊นŒ์ง€์˜ ์นดํ…Œ๊ณ ๋ฆฌ ์ค‘ 7๋ฒˆ ๋ ˆ์ด๋ธ”์— ์†ํ•˜๋Š” ๊ธ€์ด๊ณ , 7๋ฒˆ์€ rec.autos ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (2) ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜ ์ด์ œ ๋‹ค์šด๋กœ๋“œํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” newsdata.data์™€ ๊ทธ์— ๋Œ€ํ•œ ์นดํ…Œ๊ณ ๋ฆฌ ๋ ˆ์ด๋ธ”์ด ๋˜์–ด์žˆ๋Š” newsdata.target์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” newsdata.data์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ดค๋“ฏ์ด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ํ† ํฐ ํ™”๊ฐ€ ์ „ํ˜€ ๋˜์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ BoW๋กœ ๋งŒ๋“ค์–ด์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ๋ฅผ ์ž๋™์œผ๋กœ BoW๋กœ ๋งŒ๋“œ๋Š” CountVectorizer๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (BoW ์ฑ•ํ„ฐ ๋ฐ DTM ์ฑ•ํ„ฐ ์ฐธ๊ณ ) from sklearn.feature_extraction.text import CountVectorizer from sklearn.feature_extraction.text import TfidfTransformer from sklearn.naive_bayes import MultinomialNB # ๋‹คํ•ญ๋ถ„ํฌ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ชจ๋ธ from sklearn.metrics import accuracy_score #์ •ํ™•๋„ ๊ณ„์‚ฐ dtmvector = CountVectorizer() X_train_dtm = dtmvector.fit_transform(newsdata.data) print(X_train_dtm.shape) (11314, 130107) ์ด์ œ ์ž๋™์œผ๋กœ DTM์ด ์™„์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 11,314๋Š” ํ›ˆ๋ จ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜์ด๊ณ  DTM ๊ด€์ ์—์„œ๋Š” ๋ฌธ์„œ์˜ ์ˆ˜๊ฐ€ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. 130,107์€ ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋“ฑ์žฅํ•œ ๋‹จ์–ด์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก , DTM์„ ๊ทธ๋Œ€๋กœ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ์— ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ DTM ํ–‰๋ ฌ ๋Œ€์‹  TF-IDF ๊ฐ€์ค‘์น˜๋ฅผ ์ ์šฉํ•œ TF-IDF ํ–‰๋ ฌ์„ ์ž…๋ ฅ์œผ๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด, ์„ฑ๋Šฅ์˜ ๊ฐœ์„ ์„ ์–ป์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. (DTM ์ฑ•ํ„ฐ ์ฐธ๊ณ ) ์ฃผ์˜ํ•  ์ ์€ TF-IDF ํ–‰๋ ฌ์ด ํ•ญ์ƒ DTM์œผ๋กœ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ๋ณด๋‹ค ์„ฑ๋Šฅ์ด ๋›ฐ์–ด๋‚˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์€ TF-IDF๋ฅผ ์ž๋™ ๊ณ„์‚ฐํ•ด ์ฃผ๋Š” TfidVectorizer ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๋ฏ€๋กœ ์ด๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. tfidf_transformer = TfidfTransformer() tfidfv = tfidf_transformer.fit_transform(X_train_dtm) print(tfidfv.shape) (11314, 130107) ์ด์ œ TF-IDF ํ–‰๋ ฌ์ด ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์€ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ชจ๋ธ์„ ์ง€์›ํ•˜๋ฏ€๋กœ, ์ด๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ–๊ณ  ์™€์„œ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. mod = MultinomialNB() mod.fit(tfidfv, newsdata.target) ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ TF-IDF ํ–‰๋ ฌ๊ณผ 11,314๊ฐœ์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ”์ด ์ ํ˜€์žˆ๋Š” newsdata.target์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ด๋Š” ์•ž์„œ ๋ฐฐ์šด ๋ถ„๋ฅ˜ ์˜ˆ์ œ๋“ค์„ ์ƒ๊ธฐํ•ด ๋ณด๋ฉด, ๊ฐ๊ฐ X_train๊ณผ y_train์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ๋“ค์ž…๋‹ˆ๋‹ค. MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True) ์—ฌ๊ธฐ์„œ alpha=1.0์€ ๋ผํ”Œ๋ผ์Šค ์Šค๋ฌด๋”ฉ์ด ์ ์šฉ๋˜์—ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. newsdata_test = fetch_20newsgroups(subset='test', shuffle=True) #ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๊ฐ–๊ณ  ์˜ค๊ธฐ X_test_dtm = dtmvector.transform(newsdata_test.data) #ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ DTM์œผ๋กœ ๋ณ€ํ™˜ tfidfv_test = tfidf_transformer.transform(X_test_dtm) #DTM์„ TF-IDF ํ–‰๋ ฌ๋กœ ๋ณ€ํ™˜ predicted = mod.predict(tfidfv_test) #ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก print("์ •ํ™•๋„:", accuracy_score(newsdata_test.target, predicted)) #์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’ ๋น„๊ต ์ •ํ™•๋„: 0.7738980350504514 77%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ, ์ž ์žฌ ์˜๋ฏธ ๋ถ„์„ ์ฑ•ํ„ฐ์—์„œ ์ง„ํ–‰ํ–ˆ๋˜ ์ „์ฒ˜๋ฆฌ๋ฅผ ๋ชจ๋‘ ์ง„ํ–‰ํ•˜๊ณ  ๋‹ค์‹œ ๋‚˜์ด๋ธŒ ๋ฒ ์ด์ฆˆ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ๋Œ๋ ค๋ณด์„ธ์š”. 80% ์ด์ƒ์˜ ์ •ํ™•๋„๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 10-06 ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ(Naver Movie Review Sentiment Analysis) ์ด๋ฒˆ์— ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์ด 200,000๊ฐœ ๋ฆฌ๋ทฐ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋กœ ์˜ํ™” ๋ฆฌ๋ทฐ์— ๋Œ€ํ•œ ํ…์ŠคํŠธ์™€ ํ•ด๋‹น ๋ฆฌ๋ทฐ๊ฐ€ ๊ธ์ •์ธ ๊ฒฝ์šฐ 1, ๋ถ€์ •์ธ ๊ฒฝ์šฐ 0์„ ํ‘œ์‹œํ•œ ๋ ˆ์ด๋ธ”๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ๊ฐ์„ฑ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://github.com/e9t/nsmc/ import pickle import pandas as pd import numpy as np import matplotlib.pyplot as plt import re import urllib.request from konlpy.tag import Okt from tqdm import tqdm from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences 1) ๋ฐ์ดํ„ฐ ๋กœ๋“œํ•˜๊ธฐ ์œ„ ๋งํฌ๋กœ๋ถ€ํ„ฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ํ•ด๋‹นํ•˜๋Š” ratings_train.txt์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ํ•ด๋‹นํ•˜๋Š” ratings_test.txt๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings_train.txt", filename="ratings_train.txt") urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings_test.txt", filename="ratings_test.txt") pandas๋ฅผ ์ด์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” train_data์— ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” test_data์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. train_data = pd.read_table('ratings_train.txt') test_data = pd.read_table('ratings_test.txt') train_data์— ์กด์žฌํ•˜๋Š” ์˜ํ™” ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(train_data)) # ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 150000 train_data๋Š” ์ด 150,000๊ฐœ์˜ ๋ฆฌ๋ทฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. train_data[:5] # ์ƒ์œ„ 5๊ฐœ ์ถœ๋ ฅ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” id, document, label ์ด 3๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. id๋Š” ๊ฐ์„ฑ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์•ž์œผ๋กœ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด ๋ชจ๋ธ์€ ๋ฆฌ๋ทฐ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋Š” document์™€ ํ•ด๋‹น ๋ฆฌ๋ทฐ๊ฐ€ ๊ธ์ •(1), ๋ถ€์ •(0)์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” label ๋‘ ๊ฐœ์˜ ์—ด์„ ํ•™์Šตํ•˜๋Š” ๋ชจ๋ธ์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‹จ์ง€ ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด์•˜์ง€๋งŒ ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ์™€ ์˜์–ด ๋ฐ์ดํ„ฐ์˜ ์ฐจ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ธ๋ฑ์Šค 2๋ฒˆ ์ƒ˜ํ”Œ์€ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๊ธ€์„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ๊ตญ์–ด์˜ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. test_data์˜ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜์™€ ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(test_data)) # ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 50000 test_data๋Š” ์ด 50,000๊ฐœ์˜ ์˜ํ™” ๋ฆฌ๋ทฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. test_data[:5] test_data๋„ train_data์™€ ๋™์ผํ•œ<NAME>์œผ๋กœ id, document, label 3๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ๋ฐ์ดํ„ฐ ์ •์ œํ•˜๊ธฐ train_data์˜ ๋ฐ์ดํ„ฐ ์ค‘๋ณต ์œ ๋ฌด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # document ์—ด๊ณผ label ์—ด์˜ ์ค‘๋ณต์„ ์ œ์™ธํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ train_data['document'].nunique(), train_data['label'].nunique() (146182, 2) ์ด 150,000๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜๋Š”๋ฐ document ์—ด์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 146,182๊ฐœ๋ผ๋Š” ๊ฒƒ์€ ์•ฝ 4,000๊ฐœ์˜ ์ค‘๋ณต ์ƒ˜ํ”Œ์ด ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. label ์—ด์€ 0 ๋˜๋Š” 1์˜ ๋‘ ๊ฐ€์ง€ ๊ฐ’๋งŒ์„ ๊ฐ€์ง€๋ฏ€๋กœ 2๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ค‘๋ณต ์ƒ˜ํ”Œ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. # document ์—ด์˜ ์ค‘๋ณต ์ œ๊ฑฐ train_data.drop_duplicates(subset=['document'], inplace=True) ์ค‘๋ณต ์ƒ˜ํ”Œ์„ ์ œ๊ฑฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ค‘๋ณต์ด ์ œ๊ฑฐ๋˜์—ˆ๋Š”์ง€ ์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ :',len(train_data)) ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 146183 ์ค‘๋ณต ์ƒ˜ํ”Œ์ด ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. train_data์—์„œ ํ•ด๋‹น ๋ฆฌ๋ทฐ์˜ ๊ธ, ๋ถ€์ • ์œ ๋ฌด๊ฐ€ ๊ธฐ์žฌ๋˜์–ด ์žˆ๋Š” ๋ ˆ์ด๋ธ”(label) ๊ฐ’์˜ ๋ถ„ํฌ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. train_data['label'].value_counts().plot(kind = 'bar') ์•ž์„œ ํ™•์ธํ•˜์˜€๋“ฏ์ด ์•ฝ 146,000๊ฐœ์˜ ์˜ํ™” ๋ฆฌ๋ทฐ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜๋Š”๋ฐ ๊ทธ๋ž˜ํ”„ ์ƒ์œผ๋กœ ๊ธ์ •๊ณผ ๋ถ€์ • ๋‘˜ ๋‹ค ์•ฝ 72,000๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜์—ฌ ๋ ˆ์ด๋ธ”์˜ ๋ถ„ํฌ๊ฐ€ ๊ท ์ผํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ •ํ™•ํ•˜๊ฒŒ ๋ช‡ ๊ฐœ์ธ์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(train_data.groupby('label').size().reset_index(name = 'count')) label count 0 0 73342 1 1 72841 ๋ ˆ์ด๋ธ”์ด 0์ธ ๋ฆฌ๋ทฐ๊ฐ€ ๊ทผ์†Œํ•˜๊ฒŒ ๋งŽ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋ทฐ ์ค‘์— Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print(train_data.isnull().values.any()) True True๊ฐ€ ๋‚˜์™”๋‹ค๋ฉด ๋ฐ์ดํ„ฐ ์ค‘์— Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์–ด๋–ค ์—ด์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(train_data.isnull().sum()) id 0 document 1 label 0 dtype: int64 ๋ฆฌ๋ทฐ๊ฐ€ ์ ํ˜€์žˆ๋Š” document ์—ด์—์„œ Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์ด 1๊ฐœ๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด document ์—ด์—์„œ Null ๊ฐ’์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์กฐ๊ฑด์œผ๋กœ Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์–ด๋Š ์ธ๋ฑ์Šค์˜ ์œ„์น˜์— ์กด์žฌํ•˜๋Š”์ง€ ํ•œ ๋ฒˆ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. train_data.loc[train_data.document.isnull()] ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์„ ์ œ๊ฑฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. train_data = train_data.dropna(how = 'any') # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ํ–‰ ์ œ๊ฑฐ print(train_data.isnull().values.any()) # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ False Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ 1๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์ œ๊ฑฐ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(len(train_data)) 146182 ๋ฐ์ดํ„ฐ์˜ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ train_data์™€ test_data์—์„œ ์˜จ์ (.)์ด๋‚˜? ์™€ ๊ฐ™์€ ๊ฐ์ข… ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ์‚ฌ์šฉ๋œ ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. train_data๋กœ๋ถ€ํ„ฐ ํ•œ๊ธ€๋งŒ ๋‚จ๊ธฐ๊ณ  ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์˜์–ด๋ฅผ ์˜ˆ์‹œ๋กœ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜์–ด์˜ ์•ŒํŒŒ๋ฒณ๋“ค์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์€ [a-zA-Z]์ž…๋‹ˆ๋‹ค. ์ด ์ •๊ทœ ํ‘œํ˜„์‹์€ ์˜์–ด์˜ ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž๋“ค์„ ๋ชจ๋‘ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์œผ๋กœ ์ด๋ฅผ ์‘์šฉํ•˜๋ฉด ์˜์–ด์— ์†ํ•˜์ง€ ์•Š๋Š” ๊ตฌ๋‘์ ์ด๋‚˜ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•ŒํŒŒ๋ฒณ๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐํ•˜๋Š” ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜ˆ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. #์•ŒํŒŒ๋ฒณ๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐ eng_text = 'do!!! you expect... people~ to~ read~ the FAQ, etc. and actually accept hard~! atheism?@@' print(re.sub(r'[^a-zA-Z ]', '', eng_text)) 'do you expect people to read the FAQ etc and actually accept hard atheism' ์œ„์™€ ๊ฐ™์€ ์›๋ฆฌ๋ฅผ ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ์— ์ ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์šฐ์„  ํ•œ๊ธ€์„ ๋ฒ”์œ„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ฐพ์•„๋‚ด๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ž์Œ๊ณผ ๋ชจ์Œ์— ๋Œ€ํ•œ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ž์Œ์˜ ๋ฒ”์œ„๋Š” ใ„ฑ ~ ใ…Ž, ๋ชจ์Œ์˜ ๋ฒ”์œ„๋Š” ใ… ~ ใ…ฃ์™€ ๊ฐ™์ด ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์— ์–ด๋–ค ์ž์Œ๊ณผ ๋ชจ์Œ์ด ์†ํ•˜๋Š”์ง€ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋งํฌ : https://www.unicode.org/charts/PDF/U3130.pdf ใ„ฑ ~ ใ…Ž: 3131 ~ 314E ใ… ~ ใ…ฃ: 314F ~ 3163 ์™„์„ฑํ˜• ํ•œ๊ธ€์˜ ๋ฒ”์œ„๋Š” ๊ฐ€ ~ ํžฃ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์— ํฌํ•จ๋œ ์Œ์ ˆ๋“ค์€ ์•„๋ž˜์˜ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://www.unicode.org/charts/PDF/UAC00.pdf ์œ„ ๋ฒ”์œ„ ์ง€์ •์„ ๋ชจ๋‘ ๋ฐ˜์˜ํ•˜์—ฌ train_data์— ํ•œ๊ธ€๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐํ•˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. # ํ•œ๊ธ€๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐ train_data['document'] = train_data['document'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") train_data[:5] ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ๋‹ค์‹œ ์ถœ๋ ฅํ•ด ๋ณด์•˜๋Š”๋ฐ, ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ˆ˜ํ–‰ํ•˜์ž ๊ธฐ์กด์˜ ๊ณต๋ฐฑ. ์ฆ‰, ๋„์–ด์“ฐ๊ธฐ๋Š” ์œ ์ง€๋˜๋ฉด์„œ ์˜จ์ ๊ณผ ๊ฐ™์€ ๊ตฌ๋‘์  ๋“ฑ์€ ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ๋Š” ํ•œ๊ธ€์ด ์•„๋‹ˆ๋”๋ผ๋„ ์˜์–ด, ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž๋กœ๋„ ๋ฆฌ๋ทฐ๋ฅผ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ธฐ์กด์— ํ•œ๊ธ€์ด ์—†๋Š” ๋ฆฌ๋ทฐ์˜€๋‹ค๋ฉด ๋” ์ด์ƒ ์•„๋ฌด๋Ÿฐ ๊ฐ’๋„ ์—†๋Š” ๋นˆ(empty) ๊ฐ’์ด ๋˜์—ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. train_data์— ๊ณต๋ฐฑ(whitespace)๋งŒ ์žˆ๊ฑฐ๋‚˜ ๋นˆ ๊ฐ’์„ ๊ฐ€์ง„ ํ–‰์ด ์žˆ๋‹ค๋ฉด Null ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋„๋ก ํ•˜๊ณ , Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. train_data['document'] = train_data['document'].str.replace('^ +', "") # white space ๋ฐ์ดํ„ฐ๋ฅผ empty value๋กœ ๋ณ€๊ฒฝ train_data['document'].replace('', np.nan, inplace=True) print(train_data.isnull().sum()) id 0 document 789 label 0 dtype: int64 Null ๊ฐ’์ด 789๊ฐœ๋‚˜ ์ƒˆ๋กœ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. Null ๊ฐ’์ด ์žˆ๋Š” ํ–‰์„ 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ณผ๊นŒ์š”? train_data.loc[train_data.document.isnull()][:5] Null ์ƒ˜ํ”Œ๋“ค์€ ๋ ˆ์ด๋ธ”์ด ๊ธ์ •์ผ ์ˆ˜๋„ ์žˆ๊ณ , ๋ถ€์ •์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๋Ÿฐ ์˜๋ฏธ๋„ ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฏ€๋กœ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. train_data = train_data.dropna(how = 'any') print(len(train_data)) 145393 ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜๊ฐ€ ๋˜๋‹ค์‹œ ์ค„์–ด์„œ 145,393๊ฐœ๊ฐ€ ๋‚จ์•˜์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์•ž์„œ ์ง„ํ–‰ํ•œ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์„ ๋™์ผํ•˜๊ฒŒ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. test_data.drop_duplicates(subset = ['document'], inplace=True) # document ์—ด์—์„œ ์ค‘๋ณต์ธ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ์ค‘๋ณต ์ œ๊ฑฐ test_data['document'] = test_data['document'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") # ์ •๊ทœ ํ‘œํ˜„์‹ ์ˆ˜ํ–‰ test_data['document'] = test_data['document'].str.replace('^ +', "") # ๊ณต๋ฐฑ์€ empty ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ test_data['document'].replace('', np.nan, inplace=True) # ๊ณต๋ฐฑ์€ Null ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ test_data = test_data.dropna(how='any') # Null ๊ฐ’ ์ œ๊ฑฐ print('์ „์ฒ˜๋ฆฌ ํ›„ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ :',len(test_data)) ์ „์ฒ˜๋ฆฌ ํ›„ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 48852 3) ํ† ํฐํ™” ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ํ† ํฐํ™” ๊ณผ์ •์—์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ถˆ์šฉ์–ด๋Š” ์ •์˜ํ•˜๊ธฐ ๋‚˜๋ฆ„์ธ๋ฐ, ํ•œ๊ตญ์–ด์˜ ์กฐ์‚ฌ, ์ ‘์†์‚ฌ ๋“ฑ์˜ ๋ณดํŽธ์ ์ธ ๋ถˆ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ๊ฒฐ๊ตญ ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์† ๊ฒ€ํ† ํ•˜๋ฉด์„œ ๊ณ„์†ํ•ด์„œ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ ๋˜ํ•œ ๋งŽ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ํ˜„์—…์ธ ์ƒํ™ฉ์ด๋ผ๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ์•„๋ž˜์˜ ๋ถˆ์šฉ์–ด๋ณด๋‹ค ๋” ๋งŽ์€ ๋ถˆ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. stopwords = ['์˜','๊ฐ€','์ด','์€','๋“ค','๋Š”','์ข€','์ž˜','๊ทธ๋ƒฅ','๊ณผ','๋„','๋ฅผ','์œผ๋กœ','์ž','์—','์™€','ํ•œ','ํ•˜๋‹ค'] ์—ฌ๊ธฐ์„œ๋Š” ์œ„ ์ •๋„๋กœ๋งŒ ๋ถˆ์šฉ์–ด๋ฅผ ์ •์˜ํ•˜๊ณ , ํ† ํฐํ™”๋ฅผ ์œ„ํ•œ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋Š” KoNLPy์˜ Okt๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Okt๋ฅผ ๋ณต์Šตํ•ด ๋ด…์‹œ๋‹ค. okt = Okt() okt.morphs('์™€ ์ด๋Ÿฐ ๊ฒƒ๋„ ์˜ํ™”๋ผ๊ณ  ์ฐจ๋ผ๋ฆฌ ๋ฎค์ง๋น„๋””์˜ค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒŒ ๋‚˜์„ ๋ป”', stem = True) ['์˜ค๋‹ค', '์ด๋ ‡๋‹ค', '๊ฒƒ', '๋„', '์˜ํ™”', '๋ผ๊ณ ', '์ฐจ๋ผ๋ฆฌ', '๋ฎค์ง๋น„๋””์˜ค', '๋ฅผ', '๋งŒ๋“ค๋‹ค', '๊ฒŒ', '๋‚˜๋‹ค', '๋ป”'] Okt๋Š” ์œ„์™€ ๊ฐ™์ด KoNLPy์—์„œ ์ œ๊ณตํ•˜๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์ž…๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด์„ ํ† ํฐํ™”ํ•  ๋•Œ๋Š” ์˜์–ด์ฒ˜๋Ÿผ ๋„์–ด์“ฐ๊ธฐ ๊ธฐ์ค€์œผ๋กœ ํ† ํฐํ™”๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ฃผ๋กœ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. stem = True๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ผ์ • ์ˆ˜์ค€์˜ ์ •๊ทœํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ด ์ฃผ๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์˜ˆ์ œ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด '์ด๋Ÿฐ'์ด '์ด๋ ‡๋‹ค'๋กœ ๋ณ€ํ™˜๋˜์—ˆ๊ณ  '๋งŒ๋“œ๋Š”'์ด '๋งŒ๋“ค๋‹ค'๋กœ ๋ณ€ํ™˜๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. train_data์— ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™”๋ฅผ ํ•˜๋ฉด์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ X_train์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. X_train = [] for sentence in tqdm(train_data['document']): tokenized_sentence = okt.morphs(sentence, stem=True) # ํ† ํฐํ™” stopwords_removed_sentence = [word for word in tokenized_sentence if not word in stopwords] # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ X_train.append(stopwords_removed_sentence) ์ƒ์œ„ 3๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(X_train[:3]) [['์•„', '๋”๋น™', '์ง„์งœ', '์งœ์ฆ ๋‚˜๋‹ค', '๋ชฉ์†Œ๋ฆฌ'], ['ํ ', 'ํฌ์Šคํ„ฐ', '๋ณด๊ณ ', '์ดˆ๋“ฑํ•™์ƒ', '์˜ํ™”', '์ค„', '์˜ค๋ฒ„', '์—ฐ๊ธฐ', '์กฐ์ฐจ', '๊ฐ€๋ณ๋‹ค', '์•Š๋‹ค'], ['๋„ˆ', '๋ฌด์žฌ', '๋ฐ“์—ˆ', '๋‹ค๊ทธ', '๋ž˜์„œ', '๋ณด๋‹ค', '์ถ”์ฒœ', '๋‹ค']] ํ˜•ํƒœ์†Œ ํ† ํฐ ํ™”๊ฐ€ ์ง„ํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ๋™์ผํ•˜๊ฒŒ ํ† ํฐํ™”๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค. X_test = [] for sentence in tqdm(test_data['document']): tokenized_sentence = okt.morphs(sentence, stem=True) # ํ† ํฐํ™” stopwords_removed_sentence = [word for word in tokenized_sentence if not word in stopwords] # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ X_test.append(stopwords_removed_sentence) ์ง€๊ธˆ๊นŒ์ง€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. 4) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ธฐ๊ณ„๊ฐ€ ํ…์ŠคํŠธ๋ฅผ ์ˆซ์ž๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„ , ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocaburary)์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(X_train) ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์ƒ์„ฑ๋˜๋Š” ๋™์‹œ์— ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” tokenizer.word_index๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. print(tokenizer.word_index) {'์˜ํ™”': 1, '๋ณด๋‹ค': 2, '์„': 3, '์—†๋‹ค': 4, '์ด๋‹ค': 5, '์žˆ๋‹ค': 6, '์ข‹๋‹ค': 7, ... ์ค‘๋žต ... '๋””์ผ€์ด๋“œ': 43751, '์ˆ˜๊ฐ„': 43752} ๋‹จ์–ด๊ฐ€ 43,000๊ฐœ๊ฐ€ ๋„˜๊ฒŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ •์ˆ˜๋Š” ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ๋ถ€์—ฌ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋†’์€ ์ •์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋œ ๋‹จ์–ด๋“ค์€ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋งค์šฐ ๋‚ฎ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ฐฐ์ œํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 3ํšŒ ๋ฏธ๋งŒ์ธ ๋‹จ์–ด๋“ค์ด ์ด ๋ฐ์ดํ„ฐ์—์„œ ์–ผ๋งˆํผ์˜ ๋น„์ค‘์„ ์ฐจ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. threshold = 3 total_cnt = len(tokenizer.word_index) # ๋‹จ์–ด์˜ ์ˆ˜ rare_cnt = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ total_freq = 0 # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๋‹จ์–ด ๋นˆ๋„์ˆ˜ ์ดํ•ฉ rare_freq = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜์˜ ์ดํ•ฉ # ๋‹จ์–ด์™€ ๋นˆ๋„์ˆ˜์˜ ์Œ(pair)์„ key์™€ value๋กœ ๋ฐ›๋Š”๋‹ค. for key, value in tokenizer.word_counts.items(): total_freq = total_freq + value # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์œผ๋ฉด if(value < threshold): rare_cnt = rare_cnt + 1 rare_freq = rare_freq + value print('๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ :',total_cnt) print('๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ %s ๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: %s'%(threshold - 1, rare_cnt)) print("๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ:", (rare_cnt / total_cnt)*100) print("์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ:", (rare_freq / total_freq)*100) ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ : 43752 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 2๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: 24337 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ: 55.62488571950996 ์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ: 1.8715872104872904 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ threshold ๊ฐ’์ธ 3ํšŒ ๋ฏธ๋งŒ. ์ฆ‰, 2ํšŒ ์ดํ•˜์ธ ๋‹จ์–ด๋“ค์€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋ฌด๋ ค ์ ˆ๋ฐ˜ ์ด์ƒ์„ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„๋กœ ์ฐจ์ง€ํ•˜๋Š” ๋น„์ค‘์€ ์ƒ๋Œ€์ ์œผ๋กœ ๋งค์šฐ ์ ์€ ์ˆ˜์น˜์ธ 1.87%๋ฐ–์— ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๋ž˜๋„ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 2ํšŒ ์ดํ•˜์ธ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ณ„๋กœ ์ค‘์š”ํ•˜์ง€ ์•Š์„ ๋“ฏํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๋‹จ์–ด๋“ค์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ ๋ฐฐ์ œ์‹œํ‚ค๊ฒ ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 2์ดํ•˜์ธ ๋‹จ์–ด๋“ค์˜ ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋กœ ์ œํ•œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ „์ฒด ๋‹จ์–ด ๊ฐœ์ˆ˜ ์ค‘ ๋นˆ๋„์ˆ˜ 2์ดํ•˜์ธ ๋‹จ์–ด๋Š” ์ œ๊ฑฐ. # 0๋ฒˆ ํŒจ๋”ฉ ํ† ํฐ์„ ๊ณ ๋ คํ•˜์—ฌ + 1 vocab_size = total_cnt - rare_cnt + 1 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :',vocab_size) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 19416 ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 19,416๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๊ณ  ํ…์ŠคํŠธ ์‹œํ€€์Šค๋ฅผ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. tokenizer = Tokenizer(vocab_size) tokenizer.fit_on_texts(X_train) X_train = tokenizer.texts_to_sequences(X_train) X_test = tokenizer.texts_to_sequences(X_test) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ง„ํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ ์ž X_train์— ๋Œ€ํ•ด์„œ ์ƒ์œ„ 3๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. print(X_train[:3]) [[50, 454, 16, 260, 659], [933, 457, 41, 602, 1, 214, 1449, 24, 961, 675, 19], [386, 2444, 2315, 5671, 2, 222, 9]] ๊ฐ ์ƒ˜ํ”Œ ๋‚ด์˜ ๋‹จ์–ด๋“ค์€ ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋Š” 19,416๊ฐœ๋กœ ์ œํ•œ๋˜์—ˆ์œผ๋ฏ€๋กœ 0๋ฒˆ ๋‹จ์–ด ~ 19,415๋ฒˆ ๋‹จ์–ด๊นŒ์ง€๋งŒ ์‚ฌ์šฉ ์ค‘์ž…๋‹ˆ๋‹ค. 0๋ฒˆ ๋‹จ์–ด๋Š” ํŒจ๋”ฉ์„ ์œ„ํ•œ ํ† ํฐ์ž„์„ ์ƒ๊ธฐํ•ฉ์‹œ๋‹ค. train_data์—์„œ y_train๊ณผ y_test๋ฅผ ๋ณ„๋„๋กœ ์ €์žฅํ•ด ์ค๋‹ˆ๋‹ค. y_train = np.array(train_data['label']) y_test = np.array(test_data['label']) 5) ๋นˆ ์ƒ˜ํ”Œ(empty samples) ์ œ๊ฑฐ ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๊ฐ€ ์‚ญ์ œ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์€ ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋˜์—ˆ๋˜ ์ƒ˜ํ”Œ๋“ค์€ ๋นˆ(empty) ์ƒ˜ํ”Œ์ด ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋นˆ ์ƒ˜ํ”Œ๋“ค์€ ์–ด๋–ค ๋ ˆ์ด๋ธ”์ด ๋ถ™์–ด์žˆ๋˜ ์˜๋ฏธ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋นˆ ์ƒ˜ํ”Œ๋“ค์„ ์ œ๊ฑฐํ•ด ์ฃผ๋Š” ์ž‘์—…์„ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•ด์„œ ๊ธธ์ด๊ฐ€ 0์ธ ์ƒ˜ํ”Œ๋“ค์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ›์•„์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค. drop_train = [index for index, sentence in enumerate(X_train) if len(sentence) < 1] drop_train์—๋Š” X_train์œผ๋กœ๋ถ€ํ„ฐ ์–ป์€ ๋นˆ ์ƒ˜ํ”Œ๋“ค์˜ ์ธ๋ฑ์Šค๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์•ž์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ(X_train, y_train)์˜ ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜๋Š” 145,791๊ฐœ์ž„์„ ํ™•์ธํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋นˆ ์ƒ˜ํ”Œ๋“ค์„ ์ œ๊ฑฐํ•œ ํ›„์˜ ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜๋Š” ๋ช‡ ๊ฐœ์ผ๊นŒ์š”? # ๋นˆ ์ƒ˜ํ”Œ๋“ค์„ ์ œ๊ฑฐ X_train = np.delete(X_train, drop_train, axis=0) y_train = np.delete(y_train, drop_train, axis=0) print(len(X_train)) print(len(y_train)) 145162 145162 145,162๊ฐœ๋กœ ์ƒ˜ํ”Œ์˜ ์ˆ˜๊ฐ€ ์ค„์–ด๋“  ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 6) ํŒจ๋”ฉ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธธ์ด์˜ ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ฃผ๋Š” ํŒจ๋”ฉ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋ฆฌ๋ทฐ์™€ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max(len(review) for review in X_train)) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :',sum(map(len, X_train))/len(X_train)) plt.hist([len(review) for review in X_train], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 69 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 10.812485361182679 ๊ฐ€์žฅ ๊ธด ๋ฆฌ๋ทฐ์˜ ๊ธธ์ด๋Š” 69์ด๋ฉฐ, ๊ทธ๋ž˜ํ”„๋ฅผ ๋ดค์„ ๋•Œ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋Š” ๋Œ€์ฒด์ ์œผ๋กœ ์•ฝ 11๋‚ด์™ธ์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก X_train๊ณผ X_test์˜ ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ํŠน์ • ๊ธธ์ด๋กœ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ๊ธธ์ด ๋ณ€์ˆ˜๋ฅผ max_len์œผ๋กœ ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ฆฌ๋ทฐ๊ฐ€ ๋‚ด์šฉ์ด ์ž˜๋ฆฌ์ง€ ์•Š๋„๋ก ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์ ์˜ max_len์˜ ๊ฐ’์€ ๋ช‡์ผ๊นŒ์š”? ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ max_len ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ์ด ๋ช‡ % ์ธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. def below_threshold_len(max_len, nested_list): count = 0 for sentence in nested_list: if(len(sentence) <= max_len): count = count + 1 print('์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ %s ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: %s'%(max_len, (count / len(nested_list))*100)) ์œ„์˜ ๋ถ„ํฌ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ดค์„ ๋•Œ, max_len = 30์ด ์ ๋‹นํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ’์ด ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๋ฆฌ๋ทฐ ๊ธธ์ด๋ฅผ ์ปค๋ฒ„ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. max_len = 30 below_threshold_len(max_len, X_train) ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ 30 ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: 94.31944999380003 ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์ค‘ ์•ฝ 94%์˜ ๋ฆฌ๋ทฐ๊ฐ€ 30์ดํ•˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ 30์œผ๋กœ ๋งž์ถ”๊ฒ ์Šต๋‹ˆ๋‹ค. X_train = pad_sequences(X_train, maxlen=max_len) X_test = pad_sequences(X_test, maxlen=max_len) 2. LSTM์œผ๋กœ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 100, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 128์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€ ์ผ ๊ตฌ์กฐ์˜ LSTM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 64์ด๋ฉฐ, 15 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4)๋Š” ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค(val_loss)์ด ์ฆ๊ฐ€ํ•˜๋ฉด, ๊ณผ์ ํ•ฉ ์ง•ํ›„๋ฏ€๋กœ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค์ด 4ํšŒ ์ฆ๊ฐ€ํ•˜๋ฉด ์ •ํ•ด์ง„ ์—ํฌํฌ๊ฐ€ ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•˜์˜€๋”๋ผ๋„ ํ•™์Šต์„ ์กฐ๊ธฐ ์ข…๋ฃŒ(Early Stopping) ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ModelCheckpoint๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„(val_acc)๊ฐ€ ์ด์ „๋ณด๋‹ค ์ข‹์•„์งˆ ๊ฒฝ์šฐ์—๋งŒ ๋ชจ๋ธ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. validation_split=0.2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 20%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ , ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ํ›ˆ๋ จ์ด ์ ์ ˆํžˆ ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. from tensorflow.keras.layers import Embedding, Dense, LSTM from tensorflow.keras.models import Sequential from tensorflow.keras.models import load_model from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint embedding_dim = 100 hidden_units = 128 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(LSTM(hidden_units)) model.add(Dense(1, activation='sigmoid')) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('best_model.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc']) history = model.fit(X_train, y_train, epochs=15, callbacks=[es, mc], batch_size=64, validation_split=0.2) ์ €์ž์˜ ๊ฒฝ์šฐ ์กฐ๊ธฐ ์ข…๋ฃŒ ์กฐ๊ฑด์— ๋”ฐ๋ผ์„œ 9 ์—ํฌํฌ์—์„œ ํ›ˆ๋ จ์ด ๋ฉˆ์ท„์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ์ด ๋‹ค ๋˜์—ˆ๋‹ค๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์•˜์„ ๋•Œ ์ €์žฅ๋œ ๋ชจ๋ธ์ธ 'best_model.h5'๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. loaded_model = load_model('best_model.h5') print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (loaded_model.evaluate(X_test, y_test)[1])) ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.8544 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ 85.44%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์œ„ ์ฝ”๋“œ๋Š” ๋’ค์—์„œ ์ด์–ด์งˆ ๋„ค์ด๋ฒ„ ์‡ผํ•‘ ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ ์‹ค์Šต๊ณผ ํ•œ๊ตญ์–ด ์ŠคํŒ€ ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ ์‹ค์Šต์—์„œ๋„ ๊ฑฐ์˜ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ๋  ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ† ํฌ ๋‚˜์ด์ €๋„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒŒ์ผ๋กœ ์ €์žฅ ํ›„ ๋‹ค์‹œ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with open('tokenizer.pickle', 'wb') as handle: pickle.dump(tokenizer, handle) with open('tokenizer.pickle', 'rb') as handle: tokenizer = pickle.load(handle) 3. ๋ฆฌ๋ทฐ ์˜ˆ์ธกํ•ด ๋ณด๊ธฐ ์ž„์˜์˜ ๋ฆฌ๋ทฐ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธกํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ํ˜„์žฌ ํ•™์Šตํ•œ model์— ์ƒˆ๋กœ์šด ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก๊ฐ’์„ ์–ป๋Š” ๊ฒƒ์€ model.predict()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  model.fit()์„ ํ•  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ƒˆ๋กœ์šด ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ๋„ ๋™์ผํ•œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ ํ›„์— model.predict()์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. def sentiment_predict(new_sentence): new_sentence = re.sub(r'[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]','', new_sentence) new_sentence = okt.morphs(new_sentence, stem=True) # ํ† ํฐํ™” new_sentence = [word for word in new_sentence if not word in stopwords] # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ encoded = tokenizer.texts_to_sequences([new_sentence]) # ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ pad_new = pad_sequences(encoded, maxlen = max_len) # ํŒจ๋”ฉ score = float(loaded_model.predict(pad_new)) # ์˜ˆ์ธก if(score > 0.5): print("{:.2f}% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค.\n".format(score * 100)) else: print("{:.2f}% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค.\n".format((1 - score) * 100)) sentiment_predict('์ด ์˜ํ™” ๊ฐœ๊ฟ€ ์žผ ใ…‹ใ…‹ใ…‹') 97.76% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('์ด ์˜ํ™” ํ•ต๋…ธ์žผ ใ… ใ… ') 98.55% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('์ด๋”ด ๊ฒŒ ์˜ํ™”๋ƒ ใ…‰ใ…‰') 99.91% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('๊ฐ๋… ๋ญ ํ•˜๋Š” ๋†ˆ์ด๋ƒ?') 98.21% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('์™€ ๊ฐœ์ฉ๋‹ค ์ •๋ง ์„ธ๊ณ„๊ด€ ์ตœ๊ฐ•์ž๋“ค์˜ ์˜ํ™”๋‹ค') 80.77% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. ์ฐธ๊ณ ํ•˜์„ธ์š”! ใ„ฑ-ใ…Ž์™€ ใ…-ใ…ฃ ์‚ฌ์ด์— ์–ด๋–ค ๊ธ€์ž๋“ค์ด ํฌํ•จ๋ผ ์žˆ๋Š”์ง€๋Š” ์•„๋ž˜์˜ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. https://www.unicode.org/charts/PDF/U3130.pdf ๊ฐ€-ํžฃ ์‚ฌ์ด์— ์–ด๋–ค ๊ธ€์ž๋“ค์ด ํฌํ•จ๋ผ ์žˆ๋Š”์ง€๋Š” ์•„๋ž˜์˜ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. https://www.unicode.org/charts/PDF/UAC00.pdf 10-07 ๋„ค์ด๋ฒ„ ์‡ผํ•‘ ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ(Naver Shopping Review Sentiment Analysis) 1. Colab์— Mecab ์„ค์น˜ ์—ฌ๊ธฐ์„œ๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ €์ž์˜ ๊ฒฝ์šฐ Mecab์„ ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ตฌ๊ธ€์˜ Colab์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ Colab์—์„œ ์‹ค์Šตํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์•„๋ž˜์˜ ๋ฐฉ๋ฒ•์œผ๋กœ Mecab์ด ์„ค์น˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํ•ด๋‹น ํ™˜๊ฒฝ์— ๋งž๊ฒŒ Mecab์„ ์„ค์น˜ํ•˜์‹œ๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. # Colab์— Mecab ์„ค์น˜ !pip install konlpy !pip install mecab-python !bash <(curl -s https://raw.githubusercontent.com/konlpy/konlpy/master/scripts/mecab.sh) 2. ๋„ค์ด๋ฒ„ ์‡ผํ•‘ ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://github.com/bab2min/corpus/tree/master/sentiment import re import pandas as pd import numpy as np import matplotlib.pyplot as plt import urllib.request from collections import Counter from konlpy.tag import Mecab from sklearn.model_selection import train_test_split from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences 1) ๋ฐ์ดํ„ฐ ๋กœ๋“œํ•˜๊ธฐ ์œ„์˜ ๋งํฌ๋กœ๋ถ€ํ„ฐ ์ „์ฒด ๋ฐ์ดํ„ฐ์— ํ•ด๋‹นํ•˜๋Š” ratings_total.txt๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/bab2min/corpus/master/sentiment/naver_shopping.txt", filename="ratings_total.txt") ํ•ด๋‹น ๋ฐ์ดํ„ฐ์—๋Š” ์—ด์ œ๋ชฉ์ด ๋ณ„๋„๋กœ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ž„์˜๋กœ ๋‘ ๊ฐœ์˜ ์—ด์ œ๋ชฉ์ธ 'ratings'์™€ 'reviews'๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. total_data = pd.read_table('ratings_total.txt', names=['ratings', 'reviews']) print('์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(total_data)) # ์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 200000 ์ด 20๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. total_data[:5] 2) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌํ•˜๊ธฐ ํ˜„์žฌ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋ ˆ์ด๋ธ”์„ ๋ณ„๋„๋กœ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ‰์ ์ด 4, 5์ธ ๋ฆฌ๋ทฐ์—๋Š” ๋ ˆ์ด๋ธ” 1์„, ํ‰์ ์ด 1, 2์ธ ๋ฆฌ๋ทฐ์—๋Š” ๋ ˆ์ด๋ธ” 0์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ๋ถ€์—ฌ๋œ ๋ ˆ์ด๋ธ”์€ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ label์ด๋ผ๋Š” ์—ด์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. total_data['label'] = np.select([total_data.ratings > 3], [1], default=0) total_data[:5] ๊ฐ ์—ด์— ๋Œ€ํ•ด์„œ ์ค‘๋ณต์„ ์ œ์™ธํ•œ ์ƒ˜ํ”Œ์˜ ์ˆ˜๋ฅผ ์นด์šดํŠธํ•ฉ๋‹ˆ๋‹ค. total_data['ratings'].nunique(), total_data['reviews'].nunique(), total_data['label'].nunique() (4, 199908, 2) ratings ์—ด์˜ ๊ฒฝ์šฐ 1, 2, 4, 5๋ผ๋Š” ๋„ค ๊ฐ€์ง€ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. reviews ์—ด์—์„œ ์ค‘๋ณต์„ ์ œ์™ธํ•œ ๊ฒฝ์šฐ 199,908๊ฐœ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ 20๋งŒ ๊ฐœ์˜ ๋ฆฌ๋ทฐ๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ ์ด๋Š” ํ˜„์žฌ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ์ค‘๋ณต์ธ ์ƒ˜ํ”Œ๋“ค์ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ค‘๋ณต์ธ ์ƒ˜ํ”Œ๋“ค์„ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. total_data.drop_duplicates(subset=['reviews'], inplace=True) # reviews ์—ด์—์„œ ์ค‘๋ณต์ธ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ์ค‘๋ณต ์ œ๊ฑฐ print('์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ :',len(total_data)) ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 199908 NULL ๊ฐ’ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print(total_data.isnull().values.any()) False ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 3:1 ๋น„์œจ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. train_data, test_data = train_test_split(total_data, test_size = 0.25, random_state = 42) print('ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜ :', len(train_data)) print('ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜ :', len(test_data)) ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜ : 149931 ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜ : 49977 ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฒฝ์šฐ ์•ฝ 14๋งŒ 9,900๊ฐœ. ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฒฝ์šฐ ์•ฝ 4๋งŒ 9,900๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 3) ๋ ˆ์ด๋ธ”์˜ ๋ถ„ํฌ ํ™•์ธ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”์˜ ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. train_data['label'].value_counts().plot(kind = 'bar') print(train_data.groupby('label').size().reset_index(name = 'count')) label count 0 0 74918 1 1 75013 ๋‘ ๋ ˆ์ด๋ธ” ๋ชจ๋‘ ์•ฝ 7๋งŒ 5์ฒœ ๊ฐœ๋กœ 50:50 ๋น„์œจ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 4) ๋ฐ์ดํ„ฐ ์ •์ œํ•˜๊ธฐ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ๊ธ€์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. ๋˜ํ•œ ํ˜น์‹œ ์ด ๊ณผ์ •์—์„œ ๋นˆ ์ƒ˜ํ”Œ์ด ์ƒ๊ธฐ์ง€๋Š” ์•Š๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # ํ•œ๊ธ€๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐ train_data['reviews'] = train_data['reviews'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") train_data['reviews'].replace('', np.nan, inplace=True) print(train_data.isnull().sum()) ratings 0 reviews 0 label 0 dtype: int64 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ๊ฐ™์€ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. test_data.drop_duplicates(subset = ['reviews'], inplace=True) # ์ค‘๋ณต ์ œ๊ฑฐ test_data['reviews'] = test_data['reviews'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") # ์ •๊ทœ ํ‘œํ˜„์‹ ์ˆ˜ํ–‰ test_data['reviews'].replace('', np.nan, inplace=True) # ๊ณต๋ฐฑ์€ Null ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ test_data = test_data.dropna(how='any') # Null ๊ฐ’ ์ œ๊ฑฐ print('์ „์ฒ˜๋ฆฌ ํ›„ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ :',len(test_data)) ์ „์ฒ˜๋ฆฌ ํ›„ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 49977 5) ํ† ํฐํ™” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์„ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ž„์˜์˜ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ํ…Œ์ŠคํŠธํ•œ ํ† ํฐํ™” ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. mecab = Mecab() print(mecab.morphs('์™€ ์ด๋Ÿฐ ๊ฒƒ๋„ ์ƒํ’ˆ์ด๋ผ๊ณ  ์ฐจ๋ผ๋ฆฌ ๋‚ด๊ฐ€ ๋งŒ๋“œ๋Š” ๊ฒŒ ๋‚˜์„ ๋ป”')) ['์™€', '์ด๋Ÿฐ', '๊ฒƒ', '๋„', '์ƒํ’ˆ', '์ด', '๋ผ๊ณ ', '์ฐจ๋ผ๋ฆฌ', '๋‚ด', '๊ฐ€', '๋งŒ๋“œ', '๋Š”', '๊ฒŒ', '๋‚˜์„', '๋ป”'] ๋ถˆ์šฉ์–ด๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ•„์š” ์—†๋Š” ํ† ํฐ๋“ค์€ ์ œ๊ฑฐํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. stopwords = ['๋„', '๋Š”', '๋‹ค', '์˜', '๊ฐ€', '์ด', '์€', 'ํ•œ', '์—', 'ํ•˜', '๊ณ ', '์„', '๋ฅผ', '์ธ', '๋“ฏ', '๊ณผ', '์™€', '๋„ค', '๋“ค', '๋“ฏ', '์ง€', '์ž„', '๊ฒŒ'] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋™์ผํ•œ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. train_data['tokenized'] = train_data['reviews'].apply(mecab.morphs) train_data['tokenized'] = train_data['tokenized'].apply(lambda x: [item for item in x if item not in stopwords]) test_data['tokenized'] = test_data['reviews'].apply(mecab.morphs) test_data['tokenized'] = test_data['tokenized'].apply(lambda x: [item for item in x if item not in stopwords]) 6) ๋‹จ์–ด์™€ ๊ธธ์ด ๋ถ„ํฌ ํ™•์ธํ•˜๊ธฐ ๊ธ์ • ๋ฆฌ๋ทฐ์—๋Š” ์ฃผ๋กœ ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ๋งŽ์ด ๋“ฑ์žฅํ•˜๊ณ , ๋ถ€์ • ๋ฆฌ๋ทฐ์—๋Š” ์ฃผ๋กœ ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ๋“ฑ์žฅํ•˜๋Š”์ง€ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ ˆ์ด๋ธ”์— ๋”ฐ๋ผ์„œ ๋ณ„๋„๋กœ ๋‹จ์–ด๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ €์žฅํ•ด ์ค๋‹ˆ๋‹ค. negative_words = np.hstack(train_data[train_data.label == 0]['tokenized'].values) positive_words = np.hstack(train_data[train_data.label == 1]['tokenized'].values) Counter()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜๋ฅผ ์นด์šดํŠธํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋ถ€์ • ๋ฆฌ๋ทฐ์— ๋Œ€ํ•ด์„œ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ƒ์œ„ 20๊ฐœ ๋‹จ์–ด๋“ค์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. negative_word_count = Counter(negative_words) print(negative_word_count.most_common(20)) [('๋„ค์š”', 31799), ('๋Š”๋ฐ', 20295), ('์•ˆ', 19718), ('์–ด์š”', 14849), ('์žˆ', 13200), ('๋„ˆ๋ฌด', 13058), ('ํ–ˆ', 11783), ('์ข‹', 9812), ('๋ฐฐ์†ก', 9677), ('๊ฐ™', 8997), ('๊ตฌ๋งค', 8876), ('์–ด', 8869), ('๊ฑฐ', 8854), ('์—†', 8670), ('์•„์š”', 8642), ('์Šต๋‹ˆ๋‹ค', 8436), ('๊ทธ๋ƒฅ', 8355), ('๋˜', 8345), ('์ž˜', 8029), ('์•ˆ', 7984)] '๋„ค์š”', '๋Š”๋ฐ', '์•ˆ', '์•ˆ', '๋„ˆ๋ฌด', '์—†' ๋“ฑ๊ณผ ๊ฐ™์€ ๋‹จ์–ด๋“ค์ด ๋ถ€์ • ๋ฆฌ๋ทฐ์—์„œ ์ฃผ๋กœ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ธ์ • ๋ฆฌ๋ทฐ์— ๋Œ€ํ•ด์„œ๋„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. positive_word_count = Counter(positive_words) print(positive_word_count.most_common(20)) [('์ข‹', 39488), ('์•„์š”', 21184), ('๋„ค์š”', 19895), ('์–ด์š”', 18686), ('์ž˜', 18602), ('๊ตฌ๋งค', 16171), ('์Šต๋‹ˆ๋‹ค', 13320), ('์žˆ', 12391), ('๋ฐฐ์†ก', 12275), ('๋Š”๋ฐ', 11670), ('ํ–ˆ', 9818), ('ํ•ฉ๋‹ˆ๋‹ค', 9801), ('๋จน', 9635), ('์žฌ', 9273), ('๋„ˆ๋ฌด', 8397), ('๊ฐ™', 7868), ('๋งŒ์กฑ', 7261), ('๊ฑฐ', 6482), ('์–ด', 6294), ('์“ฐ', 6292)] '์ข‹', '์•„์š”', '๋„ค์š”', '์ž˜', '๋„ˆ๋ฌด', '๋งŒ์กฑ' ๋“ฑ๊ณผ ๊ฐ™์€ ๋‹จ์–ด๋“ค์ด ์ฃผ๋กœ ๋งŽ์ด ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. fig,(ax1, ax2) = plt.subplots(1,2, figsize=(10,5)) text_len = train_data[train_data['label']==1]['tokenized'].map(lambda x: len(x)) ax1.hist(text_len, color='red') ax1.set_title('Positive Reviews') ax1.set_xlabel('length of samples') ax1.set_ylabel('number of samples') print('๊ธ์ • ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :', np.mean(text_len)) text_len = train_data[train_data['label']==0]['tokenized'].map(lambda x: len(x)) ax2.hist(text_len, color='blue') ax2.set_title('Negative Reviews') fig.suptitle('Words in texts') ax2.set_xlabel('length of samples') ax2.set_ylabel('number of samples') print('๋ถ€์ • ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :', np.mean(text_len)) plt.show() ๊ธ์ • ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 13.587751456414221 ๋ถ€์ • ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 17.02953896259911 ๊ธ์ • ๋ฆฌ๋ทฐ๋ณด๋‹ค๋Š” ๋ถ€์ • ๋ฆฌ๋ทฐ๊ฐ€ ์ข€ ๋” ๊ธธ๊ฒŒ ์ž‘์„ฑ๋œ ๊ฒฝํ–ฅ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. X_train = train_data['tokenized'].values y_train = train_data['label'].values X_test= test_data['tokenized'].values y_test = test_data['label'].values 7) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ธฐ๊ณ„๊ฐ€ ํ…์ŠคํŠธ๋ฅผ ์ˆซ์ž๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocaburary)์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(X_train) ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์ƒ์„ฑ๋˜๋Š” ๋™์‹œ์— ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” tokenizer.word_index๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋“ฑ์žฅ ํšŸ์ˆ˜๊ฐ€ 1ํšŒ์ธ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ฐฐ์ œํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‹จ์–ด๋“ค์ด ์ด ๋ฐ์ดํ„ฐ์—์„œ ์–ผ๋งˆํผ์˜ ๋น„์ค‘์„ ์ฐจ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. threshold = 2 total_cnt = len(tokenizer.word_index) # ๋‹จ์–ด์˜ ์ˆ˜ rare_cnt = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ total_freq = 0 # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๋‹จ์–ด ๋นˆ๋„์ˆ˜ ์ดํ•ฉ rare_freq = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜์˜ ์ดํ•ฉ # ๋‹จ์–ด์™€ ๋นˆ๋„์ˆ˜์˜ ์Œ(pair)์„ key์™€ value๋กœ ๋ฐ›๋Š”๋‹ค. for key, value in tokenizer.word_counts.items(): total_freq = total_freq + value # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์œผ๋ฉด if(value < threshold): rare_cnt = rare_cnt + 1 rare_freq = rare_freq + value print('๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ :',total_cnt) print('๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ %s ๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: %s'%(threshold - 1, rare_cnt)) print("๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ:", (rare_cnt / total_cnt)*100) print("์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ:", (rare_freq / total_freq)*100) ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ : 39997 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 1๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: 18212 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ: 45.53341500612546 ์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ: 0.7935245745567578 ๋‹จ์–ด๊ฐ€ ์•ฝ 40,000๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ threshold ๊ฐ’์ธ 2ํšŒ ๋ฏธ๋งŒ. ์ฆ‰, 1ํšŒ์ธ ๋‹จ์–ด๋“ค์€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์•ฝ 45%๋ฅผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„๋กœ ์ฐจ์ง€ํ•˜๋Š” ๋น„์ค‘์€ ๋งค์šฐ ์ ์€ ์ˆ˜์น˜์ธ ์•ฝ 0.8%๋ฐ–์— ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๋ž˜๋„ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 1ํšŒ์ธ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์ค‘์š”ํ•˜์ง€ ์•Š์„ ๊ฒƒ์œผ๋กœ ์ €์ž๋Š” ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‹จ์–ด๋“ค์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ ๋ฐฐ์ œ์‹œํ‚ค๊ฒ ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 1์ธ ๋‹จ์–ด๋“ค์˜ ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋กœ ์ œํ•œํ•ฉ๋‹ˆ๋‹ค. # ์ „์ฒด ๋‹จ์–ด ๊ฐœ์ˆ˜ ์ค‘ ๋นˆ๋„์ˆ˜ 2์ดํ•˜์ธ ๋‹จ์–ด ๊ฐœ์ˆ˜๋Š” ์ œ๊ฑฐ. # 0๋ฒˆ ํŒจ๋”ฉ ํ† ํฐ๊ณผ 1๋ฒˆ OOV ํ† ํฐ์„ ๊ณ ๋ คํ•˜์—ฌ +2 vocab_size = total_cnt - rare_cnt + 2 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :',vocab_size) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 21787 ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 21,787๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ† ํฌ ๋‚˜์ด์ €์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๊ณ , ํ…์ŠคํŠธ ์‹œํ€€์Šค๋ฅผ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ ์ด๋ณด๋‹ค ํฐ ์ˆซ์ž๊ฐ€ ๋ถ€์—ฌ๋œ ๋‹จ์–ด๋“ค์€ OOV๋กœ ๋ณ€ํ™˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. tokenizer = Tokenizer(vocab_size, oov_token = 'OOV') tokenizer.fit_on_texts(X_train) X_train = tokenizer.texts_to_sequences(X_train) X_test = tokenizer.texts_to_sequences(X_test) X_train๊ณผ X_test์— ๋Œ€ํ•ด์„œ ์ƒ์œ„ 3๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. print(X_train[:3]) [[67, 2060, 299, 14260, 263, 73, 6, 236, 168, 137, 805, 2951, 625, 2, 77, 62, 207, 40, 1343, 155, 3, 6], [482, 409, 52, 8530, 2561, 2517, 339, 2918, 250, 2357, 38, 473, 2], [46, 24, 825, 105, 35, 2372, 160, 7, 10, 8061, 4, 1319, 29, 140, 322, 41, 59, 160, 140, 7, 1916, 2, 113, 162, 1379, 323, 119, 136]] print(X_test[:3]) [[14, 704, 767, 116, 186, 252, 12], [339, 3904, 62, 3816, 1651], [11, 69, 2, 49, 164, 3, 27, 15, 6, 513, 289, 17, 92, 110, 564, 59, 7, 2]] 8) ํŒจ๋”ฉ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธธ์ด์˜ ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ฃผ๋Š” ํŒจ๋”ฉ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋ฆฌ๋ทฐ์™€ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max(len(review) for review in X_train)) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :',sum(map(len, X_train))/len(X_train)) plt.hist([len(review) for review in X_train], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 85 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 15.307554808545264 ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 85, ํ‰๊ท  ๊ธธ์ด๋Š” ์•ฝ 15์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„๋กœ ๋ดค์„ ๋•Œ, ์ „์ฒด์ ์œผ๋กœ๋Š” 60์ดํ•˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. def below_threshold_len(max_len, nested_list): count = 0 for sentence in nested_list: if(len(sentence) <= max_len): count = count + 1 print('์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ %s ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: %s'%(max_len, (count / len(nested_list))*100)) ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 85์ด๋ฏ€๋กœ ๋งŒ์•ฝ 80์œผ๋กœ ํŒจ๋”ฉ ํ•  ๊ฒฝ์šฐ, ๋ช‡ ๊ฐœ์˜ ์ƒ˜ํ”Œ๋“ค์„ ์˜จ์ „ํžˆ ๋ณด์ „ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. max_len = 80 below_threshold_len(max_len, X_train) ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ 80 ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: 99.99933302652553 ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ 99.99%๊ฐ€ 80์ดํ•˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ๋ฅผ ๊ธธ์ด 80์œผ๋กœ ํŒจ๋”ฉ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. X_train = pad_sequences(X_train, maxlen=max_len) X_test = pad_sequences(X_test, maxlen=max_len) 3. GRU๋กœ ๋„ค์ด๋ฒ„ ์‡ผํ•‘ ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 100, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 128์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€ ์ผ ๊ตฌ์กฐ์˜ LSTM๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 64์ด๋ฉฐ, 15 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4)๋Š” ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค(val_loss)์ด ์ฆ๊ฐ€ํ•˜๋ฉด, ๊ณผ์ ํ•ฉ ์ง•ํ›„๋ฏ€๋กœ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค์ด 4ํšŒ ์ฆ๊ฐ€ํ•˜๋ฉด ์ •ํ•ด์ง„ ์—ํฌํฌ๊ฐ€ ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•˜์˜€๋”๋ผ๋„ ํ•™์Šต์„ ์กฐ๊ธฐ ์ข…๋ฃŒ(Early Stopping) ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ModelCheckpoint๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„(val_acc)๊ฐ€ ์ด์ „๋ณด๋‹ค ์ข‹์•„์งˆ ๊ฒฝ์šฐ์—๋งŒ ๋ชจ๋ธ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. validation_split=0.2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 20%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ , ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ํ›ˆ๋ จ์ด ์ ์ ˆํžˆ ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. from tensorflow.keras.layers import Embedding, Dense, GRU from tensorflow.keras.models import Sequential from tensorflow.keras.models import load_model from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint embedding_dim = 100 hidden_units = 128 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(GRU(hidden_units)) model.add(Dense(1, activation='sigmoid')) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('best_model.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc']) history = model.fit(X_train, y_train, epochs=15, callbacks=[es, mc], batch_size=64, validation_split=0.2) ์ €์ž์˜ ๊ฒฝ์šฐ ์—ํฌํฌ 10์—์„œ ์กฐ๊ธฐ ์ข…๋ฃŒ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. loaded_model = load_model('best_model.h5') print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (loaded_model.evaluate(X_test, y_test)[1])) 1562/1562 [==============================] - 5s 3ms/step - loss: 0.2108 - acc: 0.9237 ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.9237 4. ๋ฆฌ๋ทฐ ์˜ˆ์ธกํ•ด ๋ณด๊ธฐ ์ž„์˜์˜ ๋ฌธ์žฅ์— ๋Œ€ํ•œ ์˜ˆ์ธก์„ ์œ„ํ•ด์„œ๋Š” ํ•™์Šตํ•˜๊ธฐ ์ „ ์ „์ฒ˜๋ฆฌ๋ฅผ ๋™์ผํ•˜๊ฒŒ ์ ์šฉํ•ด ์ค๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ์˜ ์ˆœ์„œ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ†ตํ•œ ํ•œ๊ตญ์–ด ์™ธ ๋ฌธ์ž ์ œ๊ฑฐ, ํ† ํฐํ™”, ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ, ํŒจ๋”ฉ ์ˆœ์ž…๋‹ˆ๋‹ค. def sentiment_predict(new_sentence): new_sentence = re.sub(r'[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]','', new_sentence) new_sentence = mecab.morphs(new_sentence) new_sentence = [word for word in new_sentence if not word in stopwords] encoded = tokenizer.texts_to_sequences([new_sentence]) pad_new = pad_sequences(encoded, maxlen = max_len) score = float(loaded_model.predict(pad_new)) if(score > 0.5): print("{:.2f}% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค.".format(score * 100)) else: print("{:.2f}% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค.".format((1 - score) * 100)) sentiment_predict('์ด ์ƒํ’ˆ ์ง„์งœ ์ข‹์•„์š”... ์ €๋Š” ๊ฐ•์ถ”ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ฐ•') 98.88% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('์ง„์งœ ๋ฐฐ์†ก๋„ ๋Šฆ๊ณ  ๊ฐœ์งœ์ฆ ๋‚˜๋„ค์š”. ๋ญ ์ด๋Ÿฐ ๊ฑธ ์ƒํ’ˆ์ด๋ผ๊ณ  ๋งŒ๋“ฆ?') 99.31% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('ํŒ๋งค์ž๋‹˜... ๋„ˆ๋ฌด ์ตœ๊ณ ์˜ˆ์š”.. ๋Œ€๋ฐ•๋‚˜์‚ผ') 98.36% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('ใ…ใ„ดใ…‡ใ„ปใ„ดใ…‡ใ„ปใ„ดใ…‡๋ฆฌ๋ทฐ์“ฐ๊ธฐ๋„ ๊ท€์ฐฎ์•„') 91.69% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. 10-08 BiLSTM์œผ๋กœ ํ•œ๊ตญ์–ด ์ŠคํŒ€ ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ๊ฒŒ์ž„ ํ”Œ๋žซํผ ์ŠคํŒ€์— ๋“ฑ๋ก๋œ ํ•œ๊ตญ์–ด ๋ฆฌ๋ทฐ์— ๋Œ€ํ•ด์„œ ๊ฐ์„ฑ ๋ถ„์„์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ๊ธ์ •์ธ ๋ฆฌ๋ทฐ์—๋Š” ๋ ˆ์ด๋ธ” 1์ด, ๋ถ€์ •์ธ ๋ฆฌ๋ทฐ์—๋Š” ๋ ˆ์ด๋ธ” 0์ด ๋ถ€์—ฌ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ์ง„ํ–‰ํ–ˆ๋˜ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’‰๋‹ˆ๋‹ค. 1. BiLSTM์„ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์— ์‚ฌ์šฉํ•˜๊ธฐ ์–‘๋ฐฉํ–ฅ LSTM์€ ๋‘ ๊ฐœ์˜ ๋…๋ฆฝ์ ์ธ LSTM ์•„ํ‚คํ…์ฒ˜๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ์ฃผํ™ฉ์ƒ‰ LSTM ์…€์€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ๋งˆ์น˜ ์‚ฌ๋žŒ์ฒ˜๋Ÿผ ๋ฌธ์žฅ์„ ์™ผ์ชฝ ๋‹จ์–ด๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฝ๋Š” ์…ˆ์ž…๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ LSTM์€ ๋’ค์˜ ๋ฌธ๋งฅ๊นŒ์ง€ ๊ณ ๋ คํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฌธ์žฅ์„ ์˜ค๋ฅธ์ชฝ์—์„œ ๋ฐ˜๋Œ€๋กœ ์ฝ๋Š” ์—ญ๋ฐฉํ–ฅ์˜ LSTM ์…€(์œ„ ๊ทธ๋ฆผ์—์„œ ์ดˆ๋ก์ƒ‰)์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์ถœ๋ ฅ์ธต์—์„œ ์˜ˆ์ธก ์‹œ์— ๋‘ ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๋‹ค ๋Œ€๋‹ค(many-to-many) ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒฝ์šฐ์˜ ์–‘๋ฐฉํ–ฅ LSTM์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์–‘๋ฐฉํ–ฅ LSTM์„ ๋‹ค ๋Œ€ ์ผ(many-to-one) ๋ฌธ์ œ์ธ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์— ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•˜๋ฉด, ํ•œ ๊ฐ€์ง€ ์˜๋ฌธ์ด ์ƒ๊น๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ˆœ๋ฐฉํ–ฅ LSTM์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅ์ธต์œผ๋กœ ๋ณด๋‚ด์„œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์—ญ๋ฐฉํ–ฅ LSTM๋„ ์ˆœ๋ฐฉํ–ฅ๊ณผ ๊ฐ™์€ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ ๊นŒ์š”? ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ, ์—ญ๋ฐฉํ–ฅ LSTM์€ 4 ๋งŒ ๋ณธ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์—ญ๋ฐฉํ–ฅ LSTM์ด ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค๊ณ  ๊ธฐ๋Œ€ํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ผ€๋ผ์Šค์—์„œ๋Š” ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ return_sequences=False๋ฅผ ํƒํ•  ๊ฒฝ์šฐ์—๋Š” ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆœ๋ฐฉํ–ฅ LSTM์˜ ๊ฒฝ์šฐ์—๋Š” ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์—ญ๋ฐฉํ–ฅ LSTM์˜ ๊ฒฝ์šฐ์—๋Š” ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ตฌ์กฐ๋ฅผ ํ†ตํ•ด์„œ ์–‘๋ฐฉํ–ฅ LSTM์œผ๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. Colab์— Mecab ์„ค์น˜ ์—ฌ๊ธฐ์„œ๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ €์ž์˜ ๊ฒฝ์šฐ Mecab์„ ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ตฌ๊ธ€์˜ Colab์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ Colab์—์„œ ์‹ค์Šตํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์•„๋ž˜์˜ ๋ฐฉ๋ฒ•์œผ๋กœ Mecab์ด ์„ค์น˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํ•ด๋‹น ํ™˜๊ฒฝ์— ๋งž๊ฒŒ Mecab์„ ์„ค์น˜ํ•˜์‹œ๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. # Colab์— Mecab ์„ค์น˜ !git clone https://github.com/SOMJANG/Mecab-ko-for-Google-Colab.git %cd Mecab-ko-for-Google-Colab !bash install_mecab-ko_on_colab190912.sh 3. ์ŠคํŒ€ ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://github.com/bab2min/corpus/tree/master/sentiment import pandas as pd import numpy as np import matplotlib.pyplot as plt import urllib.request from collections import Counter from konlpy.tag import Mecab from sklearn.model_selection import train_test_split from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences 1) ๋ฐ์ดํ„ฐ ๋กœ๋“œํ•˜๊ธฐ urllib.request.urlretrieve("https://raw.githubusercontent.com/bab2min/corpus/master/sentiment/steam.txt", filename="steam.txt") ํ•ด๋‹น ๋ฐ์ดํ„ฐ์—๋Š” ์—ด์ œ๋ชฉ์ด ๋ณ„๋„๋กœ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ž„์˜๋กœ ๋‘ ๊ฐœ์˜ ์—ด์ œ๋ชฉ์ธ 'label'๊ณผ 'reviews'๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. total_data = pd.read_table('steam.txt', names=['label', 'reviews']) print('์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(total_data)) # ์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 100000 ์ด 10๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. total_data[:5] ๊ฐ ์—ด์— ๋Œ€ํ•ด์„œ ์ค‘๋ณต์„ ์ œ์™ธํ•œ ์ƒ˜ํ”Œ์˜ ์ˆ˜๋ฅผ ์นด์šดํŠธํ•ฉ๋‹ˆ๋‹ค. total_data['reviews'].nunique(), total_data['label'].nunique() (99892, 2) reviews ์—ด์—์„œ ์ค‘๋ณต์„ ์ œ์™ธํ•œ ๊ฒฝ์šฐ 99,892๊ฐœ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ 10๋งŒ ๊ฐœ์˜ ๋ฆฌ๋ทฐ๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ ์ด๋Š” ํ˜„์žฌ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ์ค‘๋ณต์ธ ์ƒ˜ํ”Œ๋“ค์ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ค‘๋ณต์ธ ์ƒ˜ํ”Œ๋“ค์„ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. total_data.drop_duplicates(subset=['reviews'], inplace=True) # reviews ์—ด์—์„œ ์ค‘๋ณต์ธ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ์ค‘๋ณต ์ œ๊ฑฐ print('์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ :',len(total_data)) ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 99892 NULL ๊ฐ’ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print(total_data.isnull().values.any()) False 2) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌํ•˜๊ธฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 3:1 ๋น„์œจ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. train_data, test_data = train_test_split(total_data, test_size = 0.25, random_state = 42) print('ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜ :', len(train_data)) print('ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜ :', len(test_data)) ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜ : 74919 ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜ : 24973 ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฒฝ์šฐ ์•ฝ 7๋งŒ 5,000๊ฐœ. ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ฒฝ์šฐ ์•ฝ 2๋งŒ 5,000๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 3) ๋ ˆ์ด๋ธ”์˜ ๋ถ„ํฌ ํ™•์ธ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”์˜ ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. train_data['label'].value_counts().plot(kind = 'bar') print(train_data.groupby('label').size().reset_index(name = 'count')) label count 0 0 37376 1 1 37543 ๋‘ ๋ ˆ์ด๋ธ” ๋ชจ๋‘ ์•ฝ 3๋งŒ 7์ฒœ ๊ฐœ๋กœ 50:50 ๋น„์œจ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 4) ๋ฐ์ดํ„ฐ ์ •์ œํ•˜๊ธฐ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ๊ธ€์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. ๋˜ํ•œ ํ˜น์‹œ ์ด ๊ณผ์ •์—์„œ ๋นˆ ์ƒ˜ํ”Œ์ด ์ƒ๊ธฐ์ง€๋Š” ์•Š๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # ํ•œ๊ธ€๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐ train_data['reviews'] = train_data['reviews'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") train_data['reviews'].replace('', np.nan, inplace=True) print(train_data.isnull().sum()) label 0 reviews 0 dtype: int64 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ๊ฐ™์€ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. test_data.drop_duplicates(subset = ['reviews'], inplace=True) # ์ค‘๋ณต ์ œ๊ฑฐ test_data['reviews'] = test_data['reviews'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") # ์ •๊ทœ ํ‘œํ˜„์‹ ์ˆ˜ํ–‰ test_data['reviews'].replace('', np.nan, inplace=True) # ๊ณต๋ฐฑ์€ Null ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ test_data = test_data.dropna(how='any') # Null ๊ฐ’ ์ œ๊ฑฐ print('์ „์ฒ˜๋ฆฌ ํ›„ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ :',len(test_data)) ๋ถˆ์šฉ์–ด๋ฅผ ์ •์˜ํ•ด ์ค๋‹ˆ๋‹ค. stopwords = ['๋„', '๋Š”', '๋‹ค', '์˜', '๊ฐ€', '์ด', '์€', 'ํ•œ', '์—', 'ํ•˜', '๊ณ ', '์„', '๋ฅผ', '์ธ', '๋“ฏ', '๊ณผ', '์™€', '๋„ค', '๋“ค', '๋“ฏ', '์ง€', '์ž„', '๊ฒŒ', '๋งŒ', '๊ฒŒ์ž„', '๊ฒŒ์ž„', '๋˜', '์Œ', '๋ฉด'] 5) ํ† ํฐํ™” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์„ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. mecab = Mecab() train_data['tokenized'] = train_data['reviews'].apply(mecab.morphs) train_data['tokenized'] = train_data['tokenized'].apply(lambda x: [item for item in x if item not in stopwords]) test_data['tokenized'] = test_data['reviews'].apply(mecab.morphs) test_data['tokenized'] = test_data['tokenized'].apply(lambda x: [item for item in x if item not in stopwords]) 6) ๋‹จ์–ด์™€ ๊ธธ์ด ๋ถ„ํฌ ํ™•์ธํ•˜๊ธฐ ๊ธ์ • ๋ฆฌ๋ทฐ์—๋Š” ์ฃผ๋กœ ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ๋งŽ์ด ๋“ฑ์žฅํ•˜๊ณ , ๋ถ€์ • ๋ฆฌ๋ทฐ์—๋Š” ์ฃผ๋กœ ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ๋“ฑ์žฅํ•˜๋Š”์ง€ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ ˆ์ด๋ธ”์— ๋”ฐ๋ผ์„œ ๋ณ„๋„๋กœ ๋‹จ์–ด๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ €์žฅํ•ด ์ค๋‹ˆ๋‹ค. negative_words = np.hstack(train_data[train_data.label == 0]['tokenized'].values) positive_words = np.hstack(train_data[train_data.label == 1]['tokenized'].values) Counter()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜๋ฅผ ์นด์šดํŠธํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋ถ€์ • ๋ฆฌ๋ทฐ์— ๋Œ€ํ•ด์„œ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ƒ์œ„ 20๊ฐœ ๋‹จ์–ด๋“ค์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. negative_word_count = Counter(negative_words) print(negative_word_count.most_common(20)) [('์•ˆ', 8129), ('์—†', 7141), ('๋Š”๋ฐ', 5786), ('์žˆ', 5692), ('๊ฐ™', 4247), ('๋กœ', 4083), ('ํ• ', 3920), ('๊ฑฐ', 3902), ('๋‚˜', 3805), ('ํ•ด', 3653), ('๋„ˆ๋ฌด', 3522), ('์œผ๋กœ', 3351), ('๊ธฐ', 3348), ('ํ–ˆ', 3265), ('์–ด', 3143), ('๋ณด', 2987), ('์Šต๋‹ˆ๋‹ค', 2962), ('๊ฒƒ', 2935), ('์ง€๋งŒ', 2911), ('์ข‹', 2899)] ๊ธ์ • ๋ฆฌ๋ทฐ์— ๋Œ€ํ•ด์„œ๋„ ๋™์ผํ•˜๊ฒŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. positive_word_count = Counter(positive_words) print(positive_word_count.most_common(20)) [('์žˆ', 9987), ('์ข‹', 6542), ('์Šต๋‹ˆ๋‹ค', 5179), ('์žฌ๋ฐŒ', 4997), ('ํ• ', 4838), ('์ง€๋งŒ', 4809), ('ํ•ด', 4354), ('์—†', 4145), ('๋ณด', 3907), ('์œผ๋กœ', 3900), ('๋กœ', 3879), ('์ˆ˜', 3835), ('๋Š”๋ฐ', 3825), ('๊ธฐ', 3592), ('์•ˆ', 3368), ('๊ฒƒ', 3362), ('๊ฐ™', 3356), ('๋„ค์š”', 3189), ('์–ด', 3112), ('๋‚˜', 3055)] ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. fig,(ax1, ax2) = plt.subplots(1,2, figsize=(10,5)) text_len = train_data[train_data['label']==1]['tokenized'].map(lambda x: len(x)) ax1.hist(text_len, color='red') ax1.set_title('Positive Reviews') ax1.set_xlabel('length of samples') ax1.set_ylabel('number of samples') print('๊ธ์ • ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :', np.mean(text_len)) text_len = train_data[train_data['label']==0]['tokenized'].map(lambda x: len(x)) ax2.hist(text_len, color='blue') ax2.set_title('Negative Reviews') fig.suptitle('Words in texts') ax2.set_xlabel('length of samples') ax2.set_ylabel('number of samples') print('๋ถ€์ • ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :', np.mean(text_len)) plt.show() ๊ธ์ • ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 14.948459100231734 ๋ถ€์ • ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 15.284193065068493 ์œ ์˜๋ฏธํ•œ ์ฐจ์ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. X_train = train_data['tokenized'].values y_train = train_data['label'].values X_test= test_data['tokenized'].values y_test = test_data['label'].values 7) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ธฐ๊ณ„๊ฐ€ ํ…์ŠคํŠธ๋ฅผ ์ˆซ์ž๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„ , ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocaburary)์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(X_train) ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์ƒ์„ฑ๋˜๋Š” ๋™์‹œ์— ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” tokenizer.word_index๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋“ฑ์žฅ ํšŸ์ˆ˜๊ฐ€ 1ํšŒ์ธ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ฐฐ์ œํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‹จ์–ด๋“ค์ด ์ด ๋ฐ์ดํ„ฐ์—์„œ ์–ผ๋งˆํผ์˜ ๋น„์ค‘์„ ์ฐจ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. threshold = 2 total_cnt = len(tokenizer.word_index) # ๋‹จ์–ด์˜ ์ˆ˜ rare_cnt = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ total_freq = 0 # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๋‹จ์–ด ๋นˆ๋„์ˆ˜ ์ดํ•ฉ rare_freq = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜์˜ ์ดํ•ฉ # ๋‹จ์–ด์™€ ๋นˆ๋„์ˆ˜์˜ ์Œ(pair)์„ key์™€ value๋กœ ๋ฐ›๋Š”๋‹ค. for key, value in tokenizer.word_counts.items(): total_freq = total_freq + value # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์œผ๋ฉด if(value < threshold): rare_cnt = rare_cnt + 1 rare_freq = rare_freq + value print('๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ :',total_cnt) print('๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ %s ๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: %s'%(threshold - 1, rare_cnt)) print("๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ:", (rare_cnt / total_cnt)*100) print("์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ:", (rare_freq / total_freq)*100) ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ : 32817 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 1๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: 13878 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ: 42.28905750068562 ์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ: 1.2254607619437832 ๋‹จ์–ด๊ฐ€ ์•ฝ 32,000๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ threshold ๊ฐ’์ธ 2ํšŒ ๋ฏธ๋งŒ. ์ฆ‰, 1ํšŒ์ธ ๋‹จ์–ด๋“ค์€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์•ฝ 42%๋ฅผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„๋กœ ์ฐจ์ง€ํ•˜๋Š” ๋น„์ค‘์€ ๋งค์šฐ ์ ์€ ์ˆ˜์น˜์ธ ์•ฝ 1.2%๋ฐ–์— ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๋ž˜๋„ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 1ํšŒ์ธ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ณ„๋กœ ์ค‘์š”ํ•˜์ง€ ์•Š์„ ๋“ฏํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๋‹จ์–ด๋“ค์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ ๋ฐฐ์ œ์‹œํ‚ค๊ฒ ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 1์ธ ๋‹จ์–ด๋“ค์˜ ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋กœ ์ œํ•œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ „์ฒด ๋‹จ์–ด ๊ฐœ์ˆ˜ ์ค‘ ๋นˆ๋„์ˆ˜ 2์ดํ•˜์ธ ๋‹จ์–ด ๊ฐœ์ˆ˜๋Š” ์ œ๊ฑฐ. # 0๋ฒˆ ํŒจ๋”ฉ ํ† ํฐ๊ณผ 1๋ฒˆ OOV ํ† ํฐ์„ ๊ณ ๋ คํ•˜์—ฌ +2 vocab_size = total_cnt - rare_cnt + 2 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :',vocab_size) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 18941 ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 18,941๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ† ํฌ ๋‚˜์ด์ €์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด, ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ…์ŠคํŠธ ์‹œํ€€์Šค๋ฅผ ์ˆซ์ž ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ ์ด๋ณด๋‹ค ํฐ ์ˆซ์ž๊ฐ€ ๋ถ€์—ฌ๋œ ๋‹จ์–ด๋“ค์€ OOV๋กœ ๋ณ€ํ™˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. tokenizer = Tokenizer(vocab_size, oov_token = 'OOV') tokenizer.fit_on_texts(X_train) X_train = tokenizer.texts_to_sequences(X_train) X_test = tokenizer.texts_to_sequences(X_test) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ง„ํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ ์ž X_train๊ณผ X_test์— ๋Œ€ํ•ด์„œ ์ƒ์œ„ 3๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. print(X_train[:3]) [[495, 7, 35, 87, 149, 2429, 599, 26, 8, 70, 47, 235, 111, 38, 44, 52], [161, 300, 18, 20, 63, 3582, 985, 6, 56], [7, 17, 1476, 4]] print(X_test[:3]) [[728, 34, 16, 431, 52, 106, 132, 99, 6461, 453], [4527, 687, 835, 712, 792, 108, 4, 1779, 95, 370, 3518, 81, 558, 1904, 4189, 262, 169, 61, 25, 363, 35, 87, 974, 19, 6294, 6422], [1792, 806, 685, 49, 23, 349]] 8) ํŒจ๋”ฉ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธธ์ด์˜ ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ฃผ๋Š” ํŒจ๋”ฉ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋ฆฌ๋ทฐ์™€ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max(len(review) for review in X_train)) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :',sum(map(len, X_train))/len(X_train)) plt.hist([len(review) for review in X_train], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 64 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 15.115951894712957 ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 64, ํ‰๊ท  ๊ธธ์ด๋Š” ์•ฝ 15์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„๋กœ ๋ดค์„ ๋•Œ, ์ „์ฒด์ ์œผ๋กœ๋Š” 60์ดํ•˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. def below_threshold_len(max_len, nested_list): count = 0 for sentence in nested_list: if(len(sentence) <= max_len): count = count + 1 print('์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ %s ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: %s'%(max_len, (count / len(nested_list))*100)) ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 64์ด๋ฏ€๋กœ ๋งŒ์•ฝ 60์œผ๋กœ ํŒจ๋”ฉ ํ•  ๊ฒฝ์šฐ, ๋ช‡ ๊ฐœ์˜ ์ƒ˜ํ”Œ๋“ค์„ ์˜จ์ „ํžˆ ๋ณด์ „ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. max_len = 60 below_threshold_len(max_len, X_train) ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ 60 ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: 99.99599567532935 ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์˜ 99.99%๊ฐ€ 60์ดํ•˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ๋ฅผ ๊ธธ์ด 60์œผ๋กœ ํŒจ๋”ฉ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. X_train = pad_sequences(X_train, maxlen=max_len) X_test = pad_sequences(X_test, maxlen=max_len) 4. BiLSTM์œผ๋กœ ์ŠคํŒ€ ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ, EarlyStopping๊ณผ ModelCheckpoint์™€ ๊ฐ™์€ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ƒ์„ธ ์ฝ”๋“œ๋Š” ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜์™€ ๋„ค์ด๋ฒ„ ์‡ผํ•‘ ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ ๋•Œ์™€ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฒˆ์—๋Š” ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ์ ์—์„œ ์•ž์„  ์‹ค์Šต๋“ค๊ณผ ์ฐจ์ด๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. LSTM์ด Bidirectional( ) ์•ˆ์— ๊ธฐ์žฌ๋˜์—ˆ๋‹ค๋Š” ์‚ฌ์‹ค์— ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. import re from tensorflow.keras.layers import Embedding, Dense, LSTM, Bidirectional from tensorflow.keras.models import Sequential from tensorflow.keras.models import load_model from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint embedding_dim = 100 hidden_units = 128 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(Bidirectional(LSTM(hidden_units))) # Bidirectional LSTM์„ ์‚ฌ์šฉ model.add(Dense(1, activation='sigmoid')) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('best_model.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc']) history = model.fit(X_train, y_train, epochs=15, callbacks=[es, mc], batch_size=256, validation_split=0.2) ์ €์ž์˜ ๊ฒฝ์šฐ ์—ํฌํฌ 7์—์„œ ์กฐ๊ธฐ ์ข…๋ฃŒ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. loaded_model = load_model('best_model.h5') print("ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (loaded_model.evaluate(X_test, y_test)[1])) 781/781 [==============================] - 3s 4ms/step - loss: 0.4534 - acc: 0.7893 ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.7893 5. ๋ฆฌ๋ทฐ ์˜ˆ์ธกํ•ด ๋ณด๊ธฐ ์ž„์˜์˜ ๋ฌธ์žฅ์— ๋Œ€ํ•œ ์˜ˆ์ธก์„ ์œ„ํ•ด์„œ๋Š” ํ•™์Šตํ•˜๊ธฐ ์ „ ์ „์ฒ˜๋ฆฌ๋ฅผ ๋™์ผํ•˜๊ฒŒ ์ ์šฉํ•ด ์ค๋‹ˆ๋‹ค. def sentiment_predict(new_sentence): new_sentence = re.sub(r'[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]','', new_sentence) new_sentence = mecab.morphs(new_sentence) # ํ† ํฐํ™” new_sentence = [word for word in new_sentence if not word in stopwords] # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ encoded = tokenizer.texts_to_sequences([new_sentence]) # ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ pad_new = pad_sequences(encoded, maxlen = max_len) # ํŒจ๋”ฉ score = float(loaded_model.predict(pad_new)) # ์˜ˆ์ธก if(score > 0.5): print("{:.2f}% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค.".format(score * 100)) else: print("{:.2f}% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค.".format((1 - score) * 100)) sentiment_predict('๋…ธ์žผ .. ์™„์ „ ์žฌ๋ฏธ์—†์Œ ใ…‰ใ…‰') 93.66% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('์กฐ๊ธˆ ์–ด๋ ต์ง€๋งŒ ์žฌ๋ฐŒ์Œใ…‹ใ…‹') 97.43% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('์บ๋ฆญํ„ฐ๊ฐ€ ์˜ˆ๋ป์„œ ์ข‹์•„์š”') 92.49% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. 11. NLP๋ฅผ ์œ„ํ•œ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง(Convolution Neural Network) ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์€ ์ฃผ๋กœ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ. ์ฆ‰, ๋น„์ „ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์ง€๋งŒ ์ด๋ฅผ ์‘์šฉํ•ด์„œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์‹œ๋„๋“ค์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋น„์ „ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ๋™์ž‘ ๋ฐฉ์‹์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜๊ณ , ์ด ๊ฐœ๋…์„ ํ™•์žฅํ•˜์—ฌ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ 1D ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ž์„œ RNN์œผ๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋“ค์„ ๊ฐ€์ง€๊ณ  ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ํ†ตํ•ด์„œ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ด…๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ(Character Embedding)์„ ์–ป๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 11-01 ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง(Convolution Neural Network) ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง(Convolutional Neural Network)์€ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์— ํƒ์›”ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š” ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์œผ๋กœ ํ…์ŠคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•œ ์‹œ๋„๋“ค์ด ์žˆ์—ˆ๊ณ , ์ด๋ฒˆ ์ฑ•ํ„ฐ๋Š” ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ…์ŠคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ํŽธ์„ฑํ•œ ์ฑ•ํ„ฐ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ์ข€ ๋” ์ •ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ์ฃผ์š” ๋ชฉ์ ์ธ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์—์„œ์˜ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง๋ถ€ํ„ฐ ๋จผ์ € ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์€ ํฌ๊ฒŒ ํ•ฉ์„ฑ๊ณฑ์ธต๊ณผ(Convolution layer)์™€ ํ’€๋ง์ธต(Pooling layer)์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ์ผ๋ฐ˜์ ์ธ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. (http://cs231n.github.io/convolutional-networks) ์œ„ ๊ทธ๋ฆผ์—์„œ CONV๋Š” ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์˜๋ฏธํ•˜๊ณ , ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๊ฐ€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ReLU๋ฅผ ์ง€๋‚ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ณผ์ •์„ ํ•ฉ์„ฑ๊ณฑ์ธต์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„์— POOL์ด๋ผ๋Š” ๊ตฌ๊ฐ„์„ ์ง€๋‚˜๋Š”๋ฐ ์ด๋Š” ํ’€๋ง ์—ฐ์‚ฐ์„ ์˜๋ฏธํ•˜๋ฉฐ ํ’€๋ง์ธต์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๊ณผ ํ’€๋ง ์—ฐ์‚ฐ์˜ ์˜๋ฏธ์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ด ๋ด…์‹œ๋‹ค. 1. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ๋Œ€๋‘ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์€ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์— ํƒ์›”ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š” ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ž์„œ ๋ฐฐ์šด ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ํ•œ๊ณ„๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•ŒํŒŒ๋ฒณ ์†๊ธ€์”จ๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์•ŒํŒŒ๋ฒณ Y๋ฅผ ๋น„๊ต์  ์ •์ž๋กœ ์“ด ์†๊ธ€์”จ์™€ ๋‹ค์†Œ ํœ˜๊ฐˆ๊ฒจ ์“ด ์†๊ธ€์”จ ๋‘ ๊ฐœ๋ฅผ 2์ฐจ์› ํ…์„œ์ธ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์ด ๋ณด๊ธฐ์—๋Š” ๋‘ ๊ทธ๋ฆผ ๋ชจ๋‘ ์•ŒํŒŒ๋ฒณ Y๋กœ ์†์‰ฝ๊ฒŒ ํŒ๋‹จ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๊ธฐ๊ณ„๊ฐ€ ๋ณด๊ธฐ์—๋Š” ๊ฐ ํ”ฝ์…€๋งˆ๋‹ค ๊ฐ€์ง„ ๊ฐ’์ด ๋Œ€๋ถ€๋ถ„ ์ƒ์ดํ•˜๋ฏ€๋กœ ์‚ฌ์‹ค์ƒ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์ง„ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋ฏธ์ง€๋ผ๋Š” ๊ฒƒ์€ ์œ„์™€ ๊ฐ™์ด ๊ฐ™์€ ๋Œ€์ƒ์ด๋ผ๋„ ํœ˜์–ด์ง€๊ฑฐ๋‚˜, ์ด๋™๋˜์—ˆ๊ฑฐ๋‚˜, ๋ฐฉํ–ฅ์ด ๋’คํ‹€๋ ธ๊ฑฐ๋‚˜ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ณ€ํ˜•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ๋ช‡ ๊ฐ€์ง€ ํ”ฝ์…€๋งŒ ๊ฐ’์ด ๋‹ฌ๋ผ์ ธ๋„ ๋ฏผ๊ฐํ•˜๊ฒŒ ์˜ˆ์ธก์— ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ข€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ์†๊ธ€์”จ๋ฅผ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ ๋ถ„๋ฅ˜ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด, ์ด๋ฏธ์ง€๋ฅผ 1์ฐจ์› ํ…์„œ์ธ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ์ž…๋ ฅ์ธต์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์†๊ธ€์”จ๋ฅผ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฒกํ„ฐ๋กœ ๋ฐ”๊พธ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1์ฐจ์›์œผ๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ๋Š” ์‚ฌ๋žŒ์ด ๋ณด๊ธฐ์—๋„ ์ด๊ฒŒ ์›๋ž˜ ์–ด๋–ค ์ด๋ฏธ์ง€์˜€๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ธฐ๊ณ„๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ๊ฒฐ๊ณผ๋Š” ๋ณ€ํ™˜ ์ „์— ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๊ณต๊ฐ„์ ์ธ ๊ตฌ์กฐ(spatial structure) ์ •๋ณด๊ฐ€ ์œ ์‹ค๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ณต๊ฐ„์ ์ธ ๊ตฌ์กฐ ์ •๋ณด๋ผ๋Š” ๊ฒƒ์€ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€๊นŒ์šด ์–ด๋–ค ํ”ฝ์…€๋“ค๋ผ๋ฆฌ๋Š” ์–ด๋–ค ์—ฐ๊ด€์ด ์žˆ๊ณ , ์–ด๋–ค ํ”ฝ์…€๋“ค๋ผ๋ฆฌ๋Š” ๊ฐ’์ด ๋น„์Šทํ•˜๊ฑฐ๋‚˜ ๋“ฑ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด๋ฏธ์ง€์˜ ๊ณต๊ฐ„์ ์ธ ๊ตฌ์กฐ ์ •๋ณด๋ฅผ ๋ณด์กดํ•˜๋ฉด์„œ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•ด์กŒ๊ณ , ์ด๋ฅผ ์œ„ํ•ด ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 2. ์ฑ„๋„(Channel) ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์˜ ๊ธฐ๋ณธ์ ์ธ ์šฉ์–ด์ธ ์ฑ„๋„์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ๊ธ€์ž๋‚˜ ์ด๋ฏธ์ง€๋ณด๋‹ค ์ˆซ์ž. ๋‹ค์‹œ ๋งํ•ด, ํ…์„œ๋ฅผ ๋” ์ž˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋Š” (๋†’์ด, ๋„ˆ๋น„, ์ฑ„๋„)์ด๋ผ๋Š” 3์ฐจ์› ํ…์„œ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋†’์ด๋Š” ์ด๋ฏธ์ง€์˜ ์„ธ๋กœ ๋ฐฉํ–ฅ ํ”ฝ์…€ ์ˆ˜, ๋„ˆ๋น„๋Š” ์ด๋ฏธ์ง€์˜ ๊ฐ€๋กœ ๋ฐฉํ–ฅ ํ”ฝ์…€ ์ˆ˜, ์ฑ„๋„์€ ์ƒ‰ ์„ฑ๋ถ„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ‘๋ฐฑ ์ด๋ฏธ์ง€๋Š” ์ฑ„๋„ ์ˆ˜๊ฐ€ 1์ด๋ฉฐ, ๊ฐ ํ”ฝ์…€์€ 0๋ถ€ํ„ฐ 255 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” 28 ร— 28 ํ”ฝ์…€์˜ ์†๊ธ€์”จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ์†๊ธ€์”จ ๋ฐ์ดํ„ฐ๋Š” ํ‘๋ฐฑ ์ด๋ฏธ์ง€๋ฏ€๋กœ ์ฑ„๋„ ์ˆ˜๊ฐ€ 1์ž„์„ ๊ณ ๋ คํ•˜๋ฉด (28 ร— 28 ร— 1)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 3์ฐจ์› ํ…์„œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ‘๋ฐฑ์ด ์•„๋‹ˆ๋ผ ์šฐ๋ฆฌ๊ฐ€ ํ†ต์ƒ์ ์œผ๋กœ ์ ‘ํ•˜๊ฒŒ ๋˜๋Š” ์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€๋Š” ์–ด๋–จ๊นŒ์š”? ์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€๋Š” ์ ์ƒ‰(Red), ๋…น์ƒ‰(Green), ์ฒญ์ƒ‰(Blue) ์ฑ„๋„ ์ˆ˜๊ฐ€ 3๊ฐœ์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ํ”ฝ์…€์€ ์„ธ ๊ฐ€์ง€ ์ƒ‰๊น”, ์‚ผ์›์ƒ‰์˜ ์กฐํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋†’์ด๊ฐ€ 28, ๋„ˆ๋น„๊ฐ€ 28์ธ ์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด ์ด๋ฏธ์ง€์˜ ํ…์„œ๋Š” (28 ร— 28 ร— 3)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 3์ฐจ์› ํ…์„œ์ž…๋‹ˆ๋‹ค. ์ฑ„๋„์€ ๋•Œ๋กœ๋Š” ๊นŠ์ด(depth)๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ด๋ฏธ์ง€๋Š” (๋†’์ด, ๋„ˆ๋น„, ๊นŠ์ด)๋ผ๋Š” 3์ฐจ์› ํ…์„œ๋กœ ํ‘œํ˜„๋œ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. 3. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ(Convolution operation) ํ•ฉ์„ฑ๊ณฑ์ธต์€ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ ์ด๋ฏธ์ง€์˜ ํŠน์ง•์„ ์ถ”์ถœํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„ , ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ํ•ฉ์„ฑ ๊ณฑ์€ ์˜์–ด๋กœ ์ปจ๋ณผ๋ฃจ์…˜์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š”๋ฐ, ์ปค๋„(kernel) ๋˜๋Š” ํ•„ํ„ฐ(filter)๋ผ๋Š” ร— ํฌ๊ธฐ์˜ ํ–‰๋ ฌ๋กœ ๋†’์ด ๋„ˆ๋น„ ๋†’์ด ( e g t ) ๋„ˆ๋น„ ( i t ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๊ฒน์น˜๋ฉฐ ํ›‘์œผ๋ฉด์„œ ร— ํฌ๊ธฐ์˜ ๊ฒน์ณ์ง€๋Š” ๋ถ€๋ถ„์˜ ๊ฐ ์ด๋ฏธ์ง€์™€ ์ปค๋„์˜ ์›์†Œ์˜ ๊ฐ’์„ ๊ณฑํ•ด์„œ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ์ด๋ฏธ์ง€์˜ ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„๋ถ€ํ„ฐ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ํ›‘์Šต๋‹ˆ๋‹ค. ์ปค๋„(kernel)์€ ์ผ๋ฐ˜์ ์œผ๋กœ 3 ร— 3 ๋˜๋Š” 5 ร— 5๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์•„๋ž˜๋Š” ร— ํฌ๊ธฐ์˜ ์ปค๋„๋กœ ร—์˜ ์ด๋ฏธ์ง€ ํ–‰๋ ฌ์— ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ 1 ์Šคํ…(step)์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๋„ค ๋ฒˆ์งธ ์Šคํ…๊นŒ์ง€ ์ด๋ฏธ์ง€์™€ ์‹์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. 1. ์ฒซ ๋ฒˆ์งธ ์Šคํ… (1ร—1) + (2ร—0) + (3ร—1) + (2ร—1) + (1ร—0) + (0ร—1) + (3ร—0) + (0ร—1) + (1ร—0) = 6 2. ๋‘ ๋ฒˆ์งธ ์Šคํ… (2ร—1) + (3ร—0) + (4ร—1) + (1ร—1) + (0ร—0) + (1ร—1) + (0ร—0) + (1ร—1) + (1ร—0) = 9 3. ์„ธ ๋ฒˆ์งธ ์Šคํ… (3ร—1) + (4ร—0) + (5ร—1) + (0ร—1) + (1ร—0) + (2ร—1) + (1ร—0) + (1ร—1) + (0ร—0) = 11 4. ๋„ค ๋ฒˆ์งธ ์Šคํ… (2ร—1) + (1ร—0) + (0ร—1) + (3ร—1) + (0ร—0) + (1ร—1) + (1ร—0) + (4ร—1) + (1ร—0) = 10 ์œ„ ์—ฐ์‚ฐ์„ ์ด 9๋ฒˆ์˜ ์Šคํ…๊นŒ์ง€ ๋งˆ์ณค๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์˜€์„ ๋•Œ, ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ปค๋„์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ฅผ ํŠน์„ฑ ๋งต(feature map)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ 3 ร— 3์ด์—ˆ์ง€๋งŒ, ์ปค๋„์˜ ํฌ๊ธฐ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ปค๋„์˜ ์ด๋™ ๋ฒ”์œ„๊ฐ€ ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” ํ•œ ์นธ์ด์—ˆ์ง€๋งŒ, ์ด ๋˜ํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด๋™ ๋ฒ”์œ„๋ฅผ ์ŠคํŠธ๋ผ์ด๋“œ(stride)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ๋Š” ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ 2์ผ ๊ฒฝ์šฐ์— 5 ร— 5 ์ด๋ฏธ์ง€์— ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” 3 ร— 3 ์ปค๋„์˜ ์›€์ง์ž„์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ 2 ร— 2์˜ ํฌ๊ธฐ์˜ ํŠน์„ฑ ๋งต์„ ์–ป์Šต๋‹ˆ๋‹ค. 4. ํŒจ๋”ฉ(Padding) ์œ„์˜ ์˜ˆ์—์„œ 5 ร— 5 ์ด๋ฏธ์ง€์— 3 ร— 3์˜ ์ปค๋„๋กœ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜์˜€์„ ๋•Œ, ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ 1์ผ ๊ฒฝ์šฐ์—๋Š” 3 ร— 3์˜ ํŠน์„ฑ ๋งต์„ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋กœ ์–ป์€ ํŠน์„ฑ ๋งต์€ ์ž…๋ ฅ๋ณด๋‹ค ํฌ๊ธฐ๊ฐ€ ์ž‘์•„์ง„๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ํ•ฉ์„ฑ๊ณฑ ์ธต์„ ์—ฌ๋Ÿฌ ๊ฐœ ์Œ“์•˜๋‹ค๋ฉด ์ตœ์ข…์ ์œผ๋กœ ์–ป์€ ํŠน์„ฑ ๋งต์€ ์ดˆ๊ธฐ ์ž…๋ ฅ๋ณด๋‹ค ๋งค์šฐ ์ž‘์•„์ง„ ์ƒํƒœ๊ฐ€ ๋ผ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ์ดํ›„์—๋„ ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๊ฐ€ ์ž…๋ ฅ์˜ ํฌ๊ธฐ์™€ ๋™์ผํ•˜๊ฒŒ ์œ ์ง€๋˜๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ํŒจ๋”ฉ(padding)์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํŒจ๋”ฉ์€ (ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜๊ธฐ ์ „์—) ์ž…๋ ฅ์˜ ๊ฐ€์žฅ์ž๋ฆฌ์— ์ง€์ •๋œ ๊ฐœ์ˆ˜์˜ ํญ๋งŒํผ ํ–‰๊ณผ ์—ด์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ข€ ๋” ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด ์ง€์ •๋œ ๊ฐœ์ˆ˜์˜ ํญ๋งŒํผ ํ…Œ๋‘๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ ๊ฐ’์„ 0์œผ๋กœ ์ฑ„์šฐ๋Š” ์ œ๋กœ ํŒจ๋”ฉ(zero padding)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ 5 ร— 5 ์ด๋ฏธ์ง€์— 1ํญ์งœ๋ฆฌ ์ œ๋กœ ํŒจ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ์œ„, ์•„๋ž˜์— ํ•˜๋‚˜์˜ ํ–‰์„ ์ขŒ, ์šฐ์— ํ•˜๋‚˜์˜ ์—ด์„ ์ถ”๊ฐ€ํ•œ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ปค๋„์€ ์ฃผ๋กœ 3 ร— 3 ๋˜๋Š” 5 ร— 5๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ 1์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, 3 ร— 3 ํฌ๊ธฐ์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด 1ํญ์งœ๋ฆฌ ์ œ๋กœ ํŒจ๋”ฉ์„ ์‚ฌ์šฉํ•˜๊ณ , 5 ร— 5 ํฌ๊ธฐ์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด 2ํญ์งœ๋ฆฌ ์ œ๋กœ ํŒจ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ž…๋ ฅ๊ณผ ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๋ฅผ ๋ณด์กดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 5 ร— 5 ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€์— 1ํญ์งœ๋ฆฌ ์ œ๋กœ ํŒจ๋”ฉ์„ ํ•˜๋ฉด 7 ร— 7 ์ด๋ฏธ์ง€๊ฐ€ ๋˜๋Š”๋ฐ, ์—ฌ๊ธฐ์— 3 ร— 3์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•˜์—ฌ 1 ์ŠคํŠธ๋ผ์ด๋“œ๋กœ ํ•ฉ์„ฑ ๊ณฑ์„ ํ•œ ํ›„์˜ ํŠน์„ฑ ๋งต์€ ๊ธฐ์กด์˜ ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์™€ ๊ฐ™์€ 5 ร— 5๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 5. ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์—์„œ์˜ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์šฐ์„  ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ๋ณต์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ๊ฐ€์ค‘์น˜ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ 3 ร— 3 ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ด๋ฏธ์ง€๋ฅผ 1์ฐจ์› ํ…์„œ์ธ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ค๋ฉด, 3 ร— 3 = 9๊ฐ€ ๋˜๋ฏ€๋กœ ์ž…๋ ฅ์ธต์€ 9๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  4๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ๊ฐ€์ง€๋Š” ์€๋‹‰์ธต์„ ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๊ฐ ์—ฐ๊ฒฐ์„ ์€ ๊ฐ€์ค‘์น˜๋ฅผ ์˜๋ฏธํ•˜๋ฏ€๋กœ, ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” 9 ร— 4 = 36๊ฐœ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋น„๊ต๋ฅผ ์œ„ํ•ด ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์œผ๋กœ 3 ร— 3 ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2 ร— 2 ์ปค๋„์„ ์‚ฌ์šฉํ•˜๊ณ , ์ŠคํŠธ๋ผ์ด๋“œ๋Š” 1๋กœ ํ•ฉ๋‹ˆ๋‹ค. (*๋Š” ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.) ์‚ฌ์‹ค ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์—์„œ ๊ฐ€์ค‘์น˜๋Š” ์ปค๋„ ํ–‰๋ ฌ์˜ ์›์†Œ๋“ค์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ํŠน์„ฑ ๋งต์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋™์ผํ•œ ์ปค๋„๋กœ ์ด๋ฏธ์ง€ ์ „์ฒด๋ฅผ ํ›‘์œผ๋ฉฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด๋ฏธ์ง€ ์ „์ฒด๋ฅผ ํ›‘์œผ๋ฉด์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์ค‘์น˜๋Š” 0 w, 2 w 4๊ฐœ๋ฟ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๋งˆ๋‹ค ์ด๋ฏธ์ง€์˜ ๋ชจ๋“  ํ”ฝ์…€์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ปค๋„๊ณผ ๋งคํ•‘๋˜๋Š” ํ”ฝ์…€๋งŒ์„ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์€ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ์‚ฌ์šฉํ•  ๋•Œ๋ณด๋‹ค ํ›จ์”ฌ ์ ์€ ์ˆ˜์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๊ณต๊ฐ„์  ๊ตฌ์กฐ ์ •๋ณด๋ฅผ ๋ณด์กดํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ์€๋‹‰์ธต์—์„œ๋Š” ๊ฐ€์ค‘์น˜ ์—ฐ์‚ฐ ํ›„์— ๋น„์„ ํ˜•์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ์€๋‹‰์ธต์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์–ป์€ ํŠน์„ฑ ๋งต์€ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋น„์„ ํ˜•์„ฑ ์ถ”๊ฐ€๋ฅผ ์œ„ํ•ด์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋ ๋ฃจ ํ•จ์ˆ˜๋‚˜ ๋ ๋ฃจ ํ•จ์ˆ˜์˜ ๋ณ€ํ˜•๋“ค์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ ํŠน์„ฑ ๋งต์„ ์–ป๊ณ , ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋Š” ์—ฐ์‚ฐ์„ ํ•˜๋Š” ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ์ธต์„ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์—์„œ๋Š” ํ•ฉ์„ฑ๊ณฑ ์ธต(convolution layer)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ํŽธํ–ฅ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์—๋„ ํŽธํ–ฅ(bias)๋ฅผ ๋‹น์—ฐํžˆ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ํŽธํ–ฅ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ปค๋„์„ ์ ์šฉํ•œ ๋’ค์— ๋”ํ•ด์ง‘๋‹ˆ๋‹ค. ํŽธํ–ฅ์€ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์กด์žฌํ•˜๋ฉฐ, ์ปค๋„์ด ์ ์šฉ๋œ ๊ฒฐ๊ณผ์˜ ๋ชจ๋“  ์›์†Œ์— ๋”ํ•ด์ง‘๋‹ˆ๋‹ค. 6. ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ• ์ž…๋ ฅ์˜ ํฌ๊ธฐ์™€ ์ปค๋„์˜ ํฌ๊ธฐ, ๊ทธ๋ฆฌ๊ณ  ์ŠคํŠธ๋ผ์ด๋“œ์˜ ๊ฐ’๋งŒ ์•Œ๋ฉด ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ์ธ ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. h : ์ž…๋ ฅ์˜ ๋†’์ด w : ์ž…๋ ฅ์˜ ๋„ˆ๋น„ h : ์ปค๋„์˜ ๋†’์ด w : ์ปค๋„์˜ ๋„ˆ๋น„ : ์ŠคํŠธ๋ผ์ด๋“œ h : ํŠน์„ฑ ๋งต์˜ ๋†’์ด w : ํŠน์„ฑ ๋งต์˜ ๋„ˆ๋น„ ์ด์— ๋”ฐ๋ผ ํŠน์„ฑ ๋งต์˜ ๋†’์ด์™€ ๋„ˆ๋น„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. h f o r ( h K S 1 ) w f o r ( w K S 1 ) ์—ฌ๊ธฐ์„œ l o ํ•จ์ˆ˜๋Š” ์†Œ์ˆ˜์  ๋ฐœ์ƒ ์‹œ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆฌ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ 5 ร— 5 ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€์— 3 ร— 3 ์ปค๋„์„ ์‚ฌ์šฉํ•˜๊ณ  ์ŠคํŠธ๋ผ์ด๋“œ 1๋กœ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๋Š” (5 - 3 + 1 ) ร— (5 - 3 + 1) = 3 ร— 3์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋˜ํ•œ ์ด 9๋ฒˆ์˜ ์Šคํ…์ด ํ•„์š”ํ•จ์„ ์˜๋ฏธํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํŒจ๋”ฉ์˜ ํญ์„ ๋ผ๊ณ  ํ•˜๊ณ , ํŒจ๋”ฉ๊นŒ์ง€ ๊ณ ๋ คํ•œ ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. h f o r ( h K + P + ) w f o r ( w K + P + ) 7. ๋‹ค์ˆ˜์˜ ์ฑ„๋„์„ ๊ฐ€์งˆ ๊ฒฝ์šฐ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ(3์ฐจ์› ํ…์„œ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ) ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ฑ„๋„(channel) ๋˜๋Š” ๊นŠ์ด(depth)๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ , 2์ฐจ์› ํ…์„œ๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ์ž…๋ ฅ์€ '๋‹ค์ˆ˜์˜ ์ฑ„๋„์„ ๊ฐ€์ง„' ์ด๋ฏธ์ง€ ๋˜๋Š” ์ด์ „ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ํŠน์„ฑ ๋งต์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋‹ค์ˆ˜์˜ ์ฑ„๋„์„ ๊ฐ€์ง„ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์ปค๋„์˜ ์ฑ„๋„ ์ˆ˜๋„ ์ž…๋ ฅ์˜ ์ฑ„๋„ ์ˆ˜๋งŒํผ ์กด์žฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์ฑ„๋„ ์ˆ˜์™€ ์ปค๋„์˜ ์ฑ„๋„ ์ˆ˜๋Š” ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฑ„๋„ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฏ€๋กœ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ฑ„๋„๋งˆ๋‹ค ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๋”ํ•˜์—ฌ ์ตœ์ข… ํŠน์„ฑ ๋งต์„ ์–ป์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ 3๊ฐœ์˜ ์ฑ„๋„์„ ๊ฐ€์ง„ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ 3๊ฐœ์˜ ์ฑ„๋„์„ ๊ฐ€์ง„ ์ปค๋„์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ปค๋„์˜ ๊ฐ ์ฑ„๋„๋ผ๋ฆฌ์˜ ํฌ๊ธฐ๋Š” ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ฑ„๋„ ๊ฐ„ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ๋งˆ์น˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๋”ํ•ด์„œ ํ•˜๋‚˜์˜ ์ฑ„๋„์„ ๊ฐ€์ง€๋Š” ํŠน์„ฑ ๋งต์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์œ„์˜ ์—ฐ์‚ฐ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ปค๋„์€ 3๊ฐœ์˜ ์ปค๋„์ด ์•„๋‹ˆ๋ผ 3๊ฐœ์˜ ์ฑ„๋„์„ ๊ฐ€์ง„ 1๊ฐœ์˜ ์ปค๋„์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๋†’์ด 3, ๋„ˆ๋น„ 3, ์ฑ„๋„ 3์˜ ์ž…๋ ฅ์ด ๋†’์ด 2, ๋„ˆ๋น„ 2, ์ฑ„๋„ 3์˜ ์ปค๋„๊ณผ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜์—ฌ ๋†’์ด 2, ๋„ˆ๋น„ 2, ์ฑ„๋„ 1์˜ ํŠน์„ฑ ๋งต์„ ์–ป๋Š”๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋กœ ์–ป์€ ํŠน์„ฑ ๋งต์˜ ์ฑ„๋„ ์ฐจ์›์€ RGB ์ฑ„๋„ ๋“ฑ๊ณผ ๊ฐ™์€ ์ปฌ๋Ÿฌ์˜ ์˜๋ฏธ๋ฅผ ๋‹ด๊ณ  ์žˆ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์ด ์—ฐ์‚ฐ์—์„œ ๊ฐ ์ฐจ์›์„ ๋ณ€์ˆ˜๋กœ ๋‘๊ณ  ์ข€ ๋” ์ผ๋ฐ˜ํ™”์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 8. 3์ฐจ์› ํ…์„œ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ์ผ๋ฐ˜ํ™”๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ ๋ณ€์ˆ˜๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. h : ์ž…๋ ฅ์˜ ๋†’์ด w : ์ž…๋ ฅ์˜ ๋„ˆ๋น„ h : ์ปค๋„์˜ ๋†’์ด w : ์ปค๋„์˜ ๋„ˆ๋น„ h : ํŠน์„ฑ ๋งต์˜ ๋†’์ด w : ํŠน์„ฑ ๋งต์˜ ๋„ˆ๋น„ i : ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์ฑ„๋„ ๋‹ค์Œ์€ 3์ฐจ์› ํ…์„œ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋†’์ด h , ๋„ˆ๋น„ w , ์ฑ„๋„ i ์˜ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋Š” ๋™์ผํ•œ ์ฑ„๋„ ์ˆ˜ i ๋ฅผ ๊ฐ€์ง€๋Š” ๋†’์ด h , ๋„ˆ๋น„ w ์˜ ์ปค๋„๊ณผ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜์—ฌ ๋†’์ด h , ๋„ˆ๋น„ w , ์ฑ„๋„ 1์˜ ํŠน์„ฑ ๋งต์„ ์–ป์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ•˜๋‚˜์˜ ์ž…๋ ฅ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•˜๋Š” ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ ๋‹ค์ˆ˜์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ฐ”๋€Œ๋Š”์ง€ ๋ด…์‹œ๋‹ค. ๋‹ค์Œ์€ o ๋ฅผ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์— ์‚ฌ์šฉํ•˜๋Š” ์ปค๋„์˜ ์ˆ˜๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ ๋‹ค์ˆ˜์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์‚ฌ์šฉํ•œ ์ปค๋„ ์ˆ˜๋Š” ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜ค๋Š” ํŠน์„ฑ ๋งต์˜ ์ฑ„๋„ ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ดํ•ดํ–ˆ๋‹ค๋ฉด ์ปค๋„์˜ ํฌ๊ธฐ์™€ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์ฑ„๋„ ์ˆ˜ i ์™€ ํŠน์„ฑ ๋งต(์ถœ๋ ฅ ๋ฐ์ดํ„ฐ)์˜ ์ฑ„๋„ ์ˆ˜ o ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์ค‘์น˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜๋Š” ์ปค๋„์˜ ์›์†Œ๋“ค์ด๋ฏ€๋กœ ํ•˜๋‚˜์˜ ์ปค๋„์˜ ํ•˜๋‚˜์˜ ์ฑ„๋„์€ i K ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜๋ ค๋ฉด ์ปค๋„์€ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์ฑ„๋„ ์ˆ˜์™€ ๋™์ผํ•œ ์ฑ„๋„ ์ˆ˜๋ฅผ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ํ•˜๋‚˜์˜ ์ปค๋„์ด ๊ฐ€์ง€๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜๋Š” i K ร— i ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฌํ•œ ์ปค๋„์ด ์ด o ๊ฐœ๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ ๊ฐ€์ค‘์น˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด ์ˆ˜ : i K ร— i C 9. ํ’€๋ง(Pooling) ์ผ๋ฐ˜์ ์œผ๋กœ ํ•ฉ์„ฑ๊ณฑ ์ธต(ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ + ํ™œ์„ฑํ™” ํ•จ์ˆ˜) ๋‹ค์Œ์—๋Š” ํ’€๋ง ์ธต์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ํ’€๋ง ์ธต์—์„œ๋Š” ํŠน์„ฑ ๋งต์„ ๋‹ค์šด ์ƒ˜ํ”Œ๋งํ•˜์—ฌ ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š” ํ’€๋ง ์—ฐ์‚ฐ์ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ํ’€๋ง ์—ฐ์‚ฐ์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ตœ๋Œ€ ํ’€๋ง(max pooling)๊ณผ ํ‰๊ท  ํ’€๋ง(average pooling)์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ตœ๋Œ€ ํ’€๋ง์„ ํ†ตํ•ด์„œ ํ’€๋ง ์—ฐ์‚ฐ์„ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ํ’€๋ง ์—ฐ์‚ฐ์—์„œ๋„ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ปค๋„๊ณผ ์ŠคํŠธ๋ผ์ด๋“œ์˜ ๊ฐœ๋…์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ 2์ผ ๋•Œ, 2 ร— 2 ํฌ๊ธฐ ์ปค๋„๋กœ ๋งฅ์Šค ํ’€๋ง ์—ฐ์‚ฐ์„ ํ–ˆ์„ ๋•Œ ํŠน์„ฑ ๋งต์ด ์ ˆ๋ฐ˜์˜ ํฌ๊ธฐ๋กœ ๋‹ค์šด ์ƒ˜ํ”Œ๋ง๋˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋งฅ์Šค ํ’€๋ง์€ ์ปค๋„๊ณผ ๊ฒน์น˜๋Š” ์˜์—ญ ์•ˆ์—์„œ ์ตœ๋Œ“๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋‹ค์šด ์ƒ˜ํ”Œ๋งํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ’€๋ง ๊ธฐ๋ฒ•์ธ ํ‰๊ท  ํ’€๋ง์€ ์ตœ๋Œ“๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ‰๊ท ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ์—ฐ์‚ฐ์ด ๋ฉ๋‹ˆ๋‹ค. ํ’€๋ง ์—ฐ์‚ฐ์€ ์ปค๋„๊ณผ ์ŠคํŠธ๋ผ์ด๋“œ ๊ฐœ๋…์ด ์กด์žฌํ•œ๋‹ค๋Š” ์ ์—์„œ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ, ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๊ณผ์˜ ์ฐจ์ด์ ์€ ํ•™์Šตํ•ด์•ผ ํ•  ๊ฐ€์ค‘์น˜๊ฐ€ ์—†์œผ๋ฉฐ ์—ฐ์‚ฐ ํ›„์— ์ฑ„๋„ ์ˆ˜๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. 11-02 ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ 1D CNN(1D Convolutional Neural Networks) ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ 1D CNN์„ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. 2D ํ•ฉ์„ฑ๊ณฑ(2D Convolutions) ์•ž์„œ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ์„ค๋ช…ํ•˜๋ฉฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์ด๋ž€ ์ปค๋„(kernel) ๋˜๋Š” ํ•„ํ„ฐ(filter)๋ผ๋Š” n ร— m ํฌ๊ธฐ์˜ ํ–‰๋ ฌ๋กœ ๋†’์ด(height) ร— ๋„ˆ๋น„(width) ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๊ฒน์น˜๋ฉฐ ํ›‘์œผ๋ฉด์„œ n ร— m ํฌ๊ธฐ์˜ ๊ฒน์ณ์ง€๋Š” ๋ถ€๋ถ„์˜ ๊ฐ ์ด๋ฏธ์ง€์™€ ์ปค๋„์˜ ์›์†Œ์˜ ๊ฐ’์„ ๊ณฑํ•ด์„œ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ์ด๋ฏธ์ง€์˜ ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„๋ถ€ํ„ฐ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ํ›‘์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์—์„œ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ 2D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 2. 1D ํ•ฉ์„ฑ๊ณฑ(1D Convolutions) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉ๋˜๋Š” 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. LSTM์„ ์ด์šฉํ•œ ์—ฌ๋Ÿฌ ์‹ค์Šต์„ ์ƒ๊ธฐํ•ด ๋ณด๋ฉด, ๊ฐ ๋ฌธ์žฅ์€ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์„ ์ง€๋‚˜์„œ ๊ฐ ๋‹จ์–ด๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ๋œ ์ƒํƒœ๋กœ LSTM์˜ ์ž…๋ ฅ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ๋„ ์ž…๋ ฅ์ด ๋˜๋Š” ๊ฒƒ์€ ๊ฐ ๋‹จ์–ด๊ฐ€ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜๋œ ๋ฌธ์žฅ ํ–‰๋ ฌ๋กœ LSTM๊ณผ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ํ˜•ํƒœ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. 'wait for the video and don't rent it'์ด๋ผ๋Š” ๋ฌธ์žฅ์ด ์žˆ์„ ๋•Œ, ์ด ๋ฌธ์žฅ์ด ํ† ํฐํ™”, ํŒจ๋”ฉ, ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)์„ ๊ฑฐ์นœ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ ํ˜•ํƒœ์˜ ํ–‰๋ ฌ๋กœ ๋ณ€ํ™˜๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ์€ ๋ฌธ์žฅ์˜ ๊ธธ์ด,๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ–‰๋ ฌ์ด ๋งŒ์•ฝ LSTM์˜ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค๋ฉด, LSTM์€ ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์—๋Š” ์ฒซ ๋ฒˆ์งธ ํ–‰์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ณ , ๋‘ ๋ฒˆ์งธ ์‹œ์ ์—๋Š” ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉฐ ์ˆœ์ฐจ์ ์œผ๋กœ ๋‹จ์–ด๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฝ์šฐ์—๋Š” ์ € ํ–‰๋ ฌ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ๊นŒ์š”? 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ ์ปค๋„์˜ ๋„ˆ๋น„๋Š” ๋ฌธ์žฅ ํ–‰๋ ฌ์—์„œ์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ ๋™์ผํ•˜๊ฒŒ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ๋Š” ์ปค๋„์˜ ๋†’์ด๋งŒ์œผ๋กœ ํ•ด๋‹น ์ปค๋„์˜ ํฌ๊ธฐ๋ผ๊ณ  ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ 2์ธ ๊ฒฝ์šฐ์—๋Š” ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋†’์ด๊ฐ€ 2, ๋„ˆ๋น„๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ธ ์ปค๋„์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ปค๋„์˜ ๋„ˆ๋น„๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด๋ผ๋Š” ์˜๋ฏธ๋Š” ์ปค๋„์ด 2D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ๋•Œ์™€๋Š” ๋‹ฌ๋ฆฌ ๋„ˆ๋น„ ๋ฐฉํ–ฅ์œผ๋กœ๋Š” ๋” ์ด์ƒ ์›€์ง์ผ ๊ณณ์ด ์—†๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ๋Š” ์ปค๋„์ด ๋ฌธ์žฅ ํ–‰๋ ฌ์˜ ๋†’์ด ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์›€์ง์ด๊ฒŒ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด, ์œ„ ๊ทธ๋ฆผ์—์„œ ์ปค๋„์€ 2D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ๋•Œ์™€๋Š” ๋‹ฌ๋ฆฌ ์˜ค๋ฅธ์ชฝ์œผ๋กœ๋Š” ์›€์ง์ผ ๊ณต๊ฐ„์ด ์—†์œผ๋ฏ€๋กœ, ์•„๋ž˜์ชฝ์œผ๋กœ๋งŒ ์ด๋™ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ 1 ์Šคํ…(step)์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๋„ค ๋ฒˆ์งธ ์Šคํ…๊นŒ์ง€ ํ‘œํ˜„ํ•œ ์ด๋ฏธ์ง€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํฌ๊ธฐ๊ฐ€ 2์ธ ์ปค๋„์€ ์ฒ˜์Œ์—๋Š” 'wait for'์— ๋Œ€ํ•ด์„œ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์Šคํ…์—๋Š” 'for the'์— ๋Œ€ํ•ด์„œ ์—ฐ์‚ฐ์„, ์„ธ ๋ฒˆ์งธ ์Šคํ…์—๋Š” 'the video'์— ๋Œ€ํ•ด์„œ ์—ฐ์‚ฐ์„, ๋„ค ๋ฒˆ์งธ ์Šคํ…์—์„œ๋Š” 'video and'์— ๋Œ€ํ•ด์„œ ์—ฐ์‚ฐ์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿ ๋ฒˆ์งธ ์Šคํ…๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜์˜€์„ ๋•Œ, ๊ฒฐ๊ณผ์ ์œผ๋กœ๋Š” ์šฐ์ธก์˜ 8์ฐจ์› ๋ฒกํ„ฐ๋ฅผ 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋กœ์„œ ์–ป๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ ๊ผญ 2์ผ ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ์š”? 2D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ 3 ร— 3 ๋˜๋Š” 5 ร— 5 ๋˜๋Š” ๋“ฑ๋“ฑ์˜ ์—ฌ๋Ÿฌ ํฌ๊ธฐ์˜ ์ปค๋„์„ ์ž์œ ์ž์žฌ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋“ฏ์ด, 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ๋„ ์ปค๋„์˜ ํฌ๊ธฐ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์ปค๋„์˜ ํฌ๊ธฐ๋ฅผ 3์œผ๋กœ ํ•œ๋‹ค๋ฉด, ๋„ค ๋ฒˆ์งธ ์Šคํ…์—์„œ์˜ ์—ฐ์‚ฐ์€ ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค๋Š” ๊ฒƒ์€ ์–ด๋–ค ์˜๋ฏธ๊ฐ€ ์žˆ์„๊นŒ์š”? CNN์—์„œ์˜ ์ปค๋„์€ ์‹ ๊ฒฝ๋ง ๊ด€์ ์—์„œ๋Š” ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์ด๋ฏ€๋กœ ์ปค๋„์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ํ•™์Šตํ•˜๊ฒŒ ๋˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ˆ˜๋Š” ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๊ณผ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ด€์ ์—์„œ๋Š” ์ปค๋„์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ์„œ ์ฐธ๊ณ ํ•˜๋Š” ๋‹จ์–ด์˜ ๋ฌถ์Œ์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด๋Š” ์ฐธ๊ณ ํ•˜๋Š” n-gram์ด ๋‹ฌ๋ผ์ง„๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ 2๋ผ๋ฉด ๊ฐ ์—ฐ์‚ฐ์˜ ์Šคํ…์—์„œ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์€ bigram์ž…๋‹ˆ๋‹ค. ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ 3์ด๋ผ๋ฉด ๊ฐ ์—ฐ์‚ฐ์˜ ์Šคํ…์—์„œ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์€ trigram์ž…๋‹ˆ๋‹ค. 3. ๋งฅ์Šค ํ’€๋ง(Max-pooling) ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์—์„œ์˜ CNN์—์„œ ๊ทธ๋žฌ๋“ฏ์ด, ์ผ๋ฐ˜์ ์œผ๋กœ 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜๋Š” 1D CNN์—์„œ๋„ ํ•ฉ์„ฑ๊ณฑ ์ธต(ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ + ํ™œ์„ฑํ™” ํ•จ์ˆ˜) ๋‹ค์Œ์—๋Š” ํ’€๋ง ์ธต์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ์ค‘ ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ๋งฅ์Šค ํ’€๋ง(Max-pooling)์ž…๋‹ˆ๋‹ค. ๋งฅ์Šค ํ’€๋ง์€ ๊ฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์œผ๋กœ๋ถ€ํ„ฐ ์–ป์€ ๊ฒฐ๊ณผ ๋ฒกํ„ฐ์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ฐ€์ง„ ์Šค์นผ๋ผ ๊ฐ’์„ ๋นผ๋‚ด๋Š” ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ํฌ๊ธฐ๊ฐ€ 2์ธ ์ปค๋„๊ณผ ํฌ๊ธฐ๊ฐ€ 3์ธ ์ปค๋„ ๋‘ ๊ฐœ์˜ ์ปค๋„๋กœ๋ถ€ํ„ฐ ๊ฐ๊ฐ ๊ฒฐ๊ณผ ๋ฒกํ„ฐ๋ฅผ ์–ป๊ณ , ๊ฐ ๋ฒกํ„ฐ์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊บผ๋‚ด์˜ค๋Š” ๋งฅ์Šค ํ’€๋ง ์—ฐ์‚ฐ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 4. ์‹ ๊ฒฝ๋ง ์„ค๊ณ„ํ•˜๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๊ฐœ๋…๋“ค์„ ๊ฐ€์ง€๊ณ  ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ CNN์„ ์„ค๊ณ„ํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„ , ์„ค๊ณ„ํ•˜๊ณ ์ž ํ•˜๋Š” ์‹ ๊ฒฝ๋ง์€ ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ๋‹จ, ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์ถœ๋ ฅ์ธต์—์„œ ๋‰ด๋Ÿฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 2์ธ ์‹ ๊ฒฝ๋ง์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ์ปค๋„์€ ํฌ๊ธฐ๊ฐ€ 4์ธ ์ปค๋„ 2๊ฐœ, 3์ธ ์ปค๋„ 2๊ฐœ, 2์ธ ์ปค๋„ 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ 9์ธ ๊ฒฝ์šฐ, ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•œ ํ›„์—๋Š” ๊ฐ๊ฐ 6์ฐจ์› ๋ฒกํ„ฐ 2๊ฐœ, 7์ฐจ์› ๋ฒกํ„ฐ 2๊ฐœ, 8์ฐจ์› ๋ฒกํ„ฐ 2๊ฐœ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋ฒกํ„ฐ๊ฐ€ 6๊ฐœ๋ฏ€๋กœ ๋งฅ์Šค ํ’€๋ง์„ ํ•œ ํ›„์—๋Š” 6๊ฐœ์˜ ์Šค์นผ๋ผ ๊ฐ’์„ ์–ป๋Š”๋ฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋ ‡๊ฒŒ ์–ป์€ ์Šค์นผ๋ผ ๊ฐ’๋“ค์€ ์ „๋ถ€ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์–ป์€ ๋ฒกํ„ฐ๋Š” 1D CNN์„ ํ†ตํ•ด์„œ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์–ป์€ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๋‰ด๋Ÿฐ์ด 2๊ฐœ์ธ ์ถœ๋ ฅ์ธต์— ์™„์ „ ์—ฐ๊ฒฐ์‹œํ‚ค๋ฏ€๋กœ์„œ(Dense layer๋ฅผ ์‚ฌ์šฉ) ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 5. ์ผ€๋ผ์Šค(Keras)๋กœ CNN ๊ตฌํ˜„ํ•˜๊ธฐ ์ผ€๋ผ์Šค๋กœ 1D ํ•ฉ์„ฑ๊ณฑ ์ธต์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. from tensorflow.keras.layers import Conv1D, GlobalMaxPooling1D model = Sequential() model.add(Conv1D(num_filters, kernel_size, padding='valid', activation='relu')) ๊ฐ ์ธ์ž์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. num_filters = ์ปค๋„์˜ ๊ฐœ์ˆ˜. kernel_size = ์ปค๋„์˜ ํฌ๊ธฐ. padding = ํŒจ๋”ฉ ๋ฐฉ๋ฒ•. - valid : ํŒจ๋”ฉ ์—†์Œ. ์ œ๋กœ ํŒจ๋”ฉ ์—†์ด ์œ ํšจํ•œ(valid) ๊ฐ’๋งŒ์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ์—์„œ valid. - same : ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ํ›„์— ์ถœ๋ ฅ์ด ์ž…๋ ฅ๊ณผ ๋™์ผํ•œ ์ฐจ์›์„ ๊ฐ€์ง€๋„๋ก ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ(๋˜๋Š” ์œ„, ์•„๋ž˜)์— ์ œ๋กœ ํŒจ๋”ฉ์„ ์ถ”๊ฐ€. activation = ํ™œ์„ฑํ™” ํ•จ์ˆ˜. ๋งŒ์•ฝ ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋งฅ์Šค ํ’€๋ง์„ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. model = Sequential() model.add(Conv1D(num_filters, kernel_size, padding='valid', activation='relu')) model.add(GlobalMaxPooling1D()) 11-03 1D CNN์œผ๋กœ IMDB ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ 1D CNN์„ ์ด์šฉํ•˜์—ฌ IMDB ๋ฆฌ๋ทฐ๋ฅผ ๋ถ„๋ฅ˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ๋‹ค๋ฃฌ ๋ฐ์ดํ„ฐ์ด๋ฏ€๋กœ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ from tensorflow.keras import datasets from tensorflow.keras.preprocessing.sequence import pad_sequences ์ตœ๋Œ€ 10,000๊ฐœ์˜ ๋‹จ์–ด๋งŒ์„ ํ—ˆ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. vocab_size = 10000 (X_train, y_train), (X_test, y_test) = datasets.imdb.load_data(num_words=vocab_size) X_train์„ ์ƒ์œ„ 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(X_train[:5]) [list([1, 14, 22, 16, 43, 530, 973, 1622, ... ์ค‘๋žต ... 32]) list([1, 194, 1153, 194, 8255, 78, ... ์ค‘๋žต ... 95]) list([1, 14, 47, 8, 30, 31, ... ์ค‘๋žต ... 113]) list([1, 4, 2, 2, 33, 2804, ... ์ค‘๋žต ... 2574]) list([1, 249, 1323, 7, 61, 113, ... ์ค‘๋žต ... 113])] ๊ฐ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๊ฐ€ ๊ธด ๊ด€๊ณ„๋กœ ์ถœ๋ ฅ ์‹œ ์ค‘๊ฐ„ ๋‚ด์šฉ์€ ์ค‘๋žตํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ ์ƒ˜ํ”Œ์€ ์ด๋ฏธ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊นŒ์ง€ ์ „์ฒ˜๋ฆฌ๊ฐ€ ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋Š” ์„œ๋กœ ๋‹ค๋ฅด์ฃ ? ํŒจ๋”ฉ์„ ์ง„ํ–‰ํ•˜์—ฌ ๋ชจ๋“  ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋ฅผ 200์œผ๋กœ ๋งž์ถฅ๋‹ˆ๋‹ค. max_len = 200 X_train = pad_sequences(X_train, maxlen=max_len) X_test = pad_sequences(X_test, maxlen=max_len) ํŒจ๋”ฉ์ด ๋˜์—ˆ๋Š”์ง€ ํฌ๊ธฐ(shape)๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('X_train์˜ ํฌ๊ธฐ(shape) :',X_train.shape) print('X_test์˜ ํฌ๊ธฐ(shape) :',X_test.shape) X_train์˜ ํฌ๊ธฐ(shape) : (25000, 200) X_test์˜ ํฌ๊ธฐ(shape) : (25000, 200) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๊ฐ 25,000 ์ƒ˜ํ”Œ์ด ์ „๋ถ€ ๊ธธ์ด 200์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. y_train๋„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(y_train[:5]) [1 0 0 1 0] 1๊ณผ 0์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์ด๋ฏ€๋กœ ๋ ˆ์ด๋ธ”์—๋Š” ๋” ์ด์ƒ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•  ๊ฒƒ์ด ์—†์Šต๋‹ˆ๋‹ค. 2. 1D CNN์œผ๋กœ IMDB ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ IMDB ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ 1D CNN ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ด…์‹œ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 256, ๋“œ๋กญ์•„์›ƒ ๋น„์œจ์€ 0.3, ์ปค๋„์˜ ํฌ๊ธฐ๋Š” 3์ด๋ฉฐ ํ•ด๋‹น ์ปค๋„์€ ์ด 256๊ฐœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์ธต๊ณผ ๋งฅ์Šค ํ’€๋ง ์—ฐ์‚ฐ ํ›„ ์ „๊ฒฐํ•ฉ์ธต(Fully Connected Layer)์„ ์€๋‹‰์ธต์„ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ์€๋‹‰์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜๋Š” 128์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 20 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=3)๋Š” ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค(val_loss)์ด ์ฆ๊ฐ€ํ•˜๋ฉด, ๊ณผ์ ํ•ฉ ์ง•ํ›„๋ฏ€๋กœ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค์ด 3ํšŒ ์ฆ๊ฐ€ํ•˜๋ฉด ์ •ํ•ด์ง„ ์—ํฌํฌ๊ฐ€ ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•˜์˜€๋”๋ผ๋„ ํ•™์Šต์„ ์กฐ๊ธฐ ์ข…๋ฃŒ(Early Stopping) ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ModelCheckpoint๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„(val_acc)๊ฐ€ ์ด์ „๋ณด๋‹ค ์ข‹์•„์งˆ ๊ฒฝ์šฐ์—๋งŒ ๋ชจ๋ธ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. validation_data๋กœ๋Š” X_test์™€ y_test๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. val_loss๊ฐ€ ์ค„์–ด๋“ค๋‹ค๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ƒํ™ฉ์ด ์˜ค๋ฉด ๊ณผ์ ํ•ฉ์œผ๋กœ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Embedding, Dropout, Conv1D, GlobalMaxPooling1D, Dense from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint from tensorflow.keras.models import load_model embedding_dim = 256 # ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์› dropout_ratio = 0.3 # ๋“œ๋กญ์•„์›ƒ ๋น„์œจ num_filters = 256 # ์ปค๋„์˜ ์ˆ˜ kernel_size = 3 # ์ปค๋„์˜ ํฌ๊ธฐ hidden_units = 128 # ๋‰ด๋Ÿฐ์˜ ์ˆ˜ model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(Dropout(dropout_ratio)) model.add(Conv1D(num_filters, kernel_size, padding='valid', activation='relu')) model.add(GlobalMaxPooling1D()) model.add(Dense(hidden_units, activation='relu')) model.add(Dropout(dropout_ratio)) model.add(Dense(1, activation='sigmoid')) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=3) mc = ModelCheckpoint('best_model.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) history = model.fit(X_train, y_train, epochs=20, validation_data=(X_test, y_test), callbacks=[es, mc]) ์ €์ž์˜ ๊ฒฝ์šฐ ์—ํฌํฌ 4์—์„œ ์กฐ๊ธฐ ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ €์žฅ๋œ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜์—ฌ ํ…Œ์ŠคํŠธ ์ •ํ™•๋„๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. loaded_model = load_model('best_model.h5') print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (loaded_model.evaluate(X_test, y_test)[1])) 25000/25000 [==============================] - 3s 3ms/step - loss: 0.5373 - acc: 0.8873 ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.8873 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ 88.73%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. 11-04 1D CNN์œผ๋กœ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ 1D CNN์„ ์ด์šฉํ•˜์—ฌ ์ŠคํŒธ ๋ฉ”์ผ์„ ๋ถ„๋ฅ˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ๋‹ค๋ฃฌ ๋ฐ์ดํ„ฐ์ด๋ฏ€๋กœ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ ๋ชจ๋“  ์ „์ฒ˜๋ฆฌ๋Š” 11-2. RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ฑ•ํ„ฐ์˜ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ(Spam Detection)( https://wikidocs.net/22894 )์™€ ๋™์ผํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•˜์˜€๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. 2. 1D CNN์œผ๋กœ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 32, ๋“œ๋กญ์•„์›ƒ ๋น„์œจ์€ 0.3, ์ปค๋„์˜ ํฌ๊ธฐ๋Š” 5์ด๋ฉฐ ํ•ด๋‹น ์ปค๋„์€ ์ด 32๊ฐœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์ธต๊ณผ ๋งฅ์Šค ํ’€๋ง ์—ฐ์‚ฐ ํ›„ ์ถœ๋ ฅ์ธต์œผ๋กœ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 64์ด๋ฉฐ, 10 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=3)๋Š” ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค(val_loss)์ด ์ฆ๊ฐ€ํ•˜๋ฉด, ๊ณผ์ ํ•ฉ ์ง•ํ›„๋ฏ€๋กœ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค์ด 3ํšŒ ์ฆ๊ฐ€ํ•˜๋ฉด ์ •ํ•ด์ง„ ์—ํฌํฌ๊ฐ€ ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•˜์˜€๋”๋ผ๋„ ํ•™์Šต์„ ์กฐ๊ธฐ ์ข…๋ฃŒ(Early Stopping) ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ModelCheckpoint๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„(val_acc)๊ฐ€ ์ด์ „๋ณด๋‹ค ์ข‹์•„์งˆ ๊ฒฝ์šฐ์—๋งŒ ๋ชจ๋ธ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. validation_split=0.2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 20%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ , ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ํ›ˆ๋ จ์ด ์ ์ ˆํžˆ ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. from tensorflow.keras.layers import Dense, Conv1D, GlobalMaxPooling1D, Embedding, Dropout, MaxPooling1D from tensorflow.keras.models import Sequential from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint embedding_dim = 32 dropout_ratio = 0.3 num_filters = 32 kernel_size = 5 model = Sequential() model.add(Embedding(vocab_size, embedding_dim)) model.add(Dropout(dropout_ratio)) model.add(Conv1D(num_filters, kernel_size, padding='valid', activation='relu')) model.add(GlobalMaxPooling1D()) model.add(Dropout(dropout_ratio)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=3) mc = ModelCheckpoint('best_model.h5', monitor = 'val_acc', mode='max', verbose=1, save_best_only=True) history = model.fit(X_train_padded, y_train, epochs=10, batch_size=64, validation_split=0.2, callbacks=[es, mc]) ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•๋„๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. X_test_encoded = tokenizer.texts_to_sequences(X_test) X_test_padded = pad_sequences(X_test_encoded, maxlen = max_len) print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (model.evaluate(X_test_padded, y_test)[1])) ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.9797 11-05 Multi-Kernel 1D CNN์œผ๋กœ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ๋‹ค์–‘ํ•œ ํฌ๊ธฐ์˜ ์ปค๋„๋“ค์„ ํ˜ผํ•ฉํ•˜์—ฌ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ๋ฅผ ๋ถ„๋ฅ˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ & ์ „์ฒ˜๋ฆฌ ๋ชจ๋“  ์ „์ฒ˜๋ฆฌ๋Š” RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ฑ•ํ„ฐ์˜ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ( https://wikidocs.net/44249 )์™€ ๋™์ผํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•˜์˜€๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. 2. Multi-Kernel 1D CNN์œผ๋กœ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ผ€๋ผ์Šค์—์„œ ๋‹ค์ˆ˜์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” Funtional API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128, ๋“œ๋กญ์•„์›ƒ ๋น„์œจ์€ 0.5์™€ 0.8 ๋‘ ๊ฐ€์ง€๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ปค๋„์˜ ๊ฐœ์ˆ˜๋Š” 128๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ „๊ฒฐํ•ฉ์ธต(Fully Connected Layer)์„ ์€๋‹‰์ธต์„ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ์€๋‹‰์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜๋Š” 128์ž…๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential, Model from tensorflow.keras.layers import Embedding, Dropout, Conv1D, GlobalMaxPooling1D, Dense, Input, Flatten, Concatenate from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint from tensorflow.keras.models import load_model embedding_dim = 128 dropout_ratio = (0.5, 0.8) num_filters = 128 hidden_units = 128 ์ž…๋ ฅ ์ธต๊ณผ ์ž„๋ฒ ๋”ฉ ์ธต์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ์ธต ์ดํ›„์—๋Š” ๋“œ๋กญ์•„์›ƒ์˜ ์ธ์ž ๊ฐ’์ด 0.5. ์ฆ‰, 50% ๋“œ๋กญ์•„์›ƒ์„ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. model_input = Input(shape = (max_len,)) z = Embedding(vocab_size, embedding_dim, input_length = max_len, name="embedding")(model_input) z = Dropout(dropout_ratio[0])(z) 3, 4, 5์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ์ปค๋„์„ ๊ฐ๊ฐ 128๊ฐœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋“ค์„ ๋งฅ์Šค ํ’€๋ง ํ•ฉ๋‹ˆ๋‹ค. conv_blocks = [] for sz in [3, 4, 5]: conv = Conv1D(filters = num_filters, kernel_size = sz, padding = "valid", activation = "relu", strides = 1)(z) conv = GlobalMaxPooling1D()(conv) conv_blocks.append(conv) ๊ฐ ๋งฅ์Šค ํ’€๋ง ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์—ฐ๊ฒฐ(concatenate) ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์ „๊ฒฐํ•ฉ์ธต(Fully Connected Layer)์„ ์‚ฌ์šฉํ•œ ์€๋‹‰์ธต์œผ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋งˆ์ง€๋ง‰ ์‹œ์ ์—์„œ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 64์ด๋ฉฐ, 10 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4)๋Š” ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค(val_loss)์ด ์ฆ๊ฐ€ํ•˜๋ฉด, ๊ณผ์ ํ•ฉ ์ง•ํ›„๋ฏ€๋กœ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค์ด 4ํšŒ ์ฆ๊ฐ€ํ•˜๋ฉด ์ •ํ•ด์ง„ ์—ํฌํฌ๊ฐ€ ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•˜์˜€๋”๋ผ๋„ ํ•™์Šต์„ ์กฐ๊ธฐ ์ข…๋ฃŒ(Early Stopping) ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ModelCheckpoint๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„(val_acc)๊ฐ€ ์ด์ „๋ณด๋‹ค ์ข‹์•„์งˆ ๊ฒฝ์šฐ์—๋งŒ ๋ชจ๋ธ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. validation_split=0.2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 20%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ , ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ํ›ˆ๋ จ์ด ์ ์ ˆํžˆ ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. z = Concatenate()(conv_blocks) if len(conv_blocks) > 1 else conv_blocks[0] z = Dropout(dropout_ratio[1])(z) z = Dense(hidden_units, activation="relu")(z) model_output = Dense(1, activation="sigmoid")(z) model = Model(model_input, model_output) model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["acc"]) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('CNN_model.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True) model.fit(X_train, y_train, batch_size=64, epochs=10, validation_split=0.2, verbose=2, callbacks=[es, mc]) ํ•™์Šต ํ›„ ์ €์žฅํ•œ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜์—ฌ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. loaded_model = load_model('CNN_model.h5') print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (loaded_model.evaluate(X_test, y_test)[1])) ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.8430 84%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. 3. ๋ฆฌ๋ทฐ ์˜ˆ์ธกํ•ด ๋ณด๊ธฐ RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ฑ•ํ„ฐ์˜ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‹ค์Šต( https://wikidocs.net/44249 )์—์„œ ์‚ฌ์šฉํ•œ ๋™์ผํ•œ ์˜ˆ์ธก ํ•จ์ˆ˜ sentiment_predict๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. sentiment_predict('์ด ์˜ํ™” ๊ฐœ๊ฟ€ ์žผ ใ…‹ใ…‹ใ…‹') 93.73% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('์ด ์˜ํ™” ํ•ต๋…ธ์žผ ใ… ใ… ') 97.03% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('์ด๋”ด ๊ฒŒ ์˜ํ™”๋ƒ ใ…‰ใ…‰') 97.18% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('๊ฐ๋… ๋ญ ํ•˜๋Š” ๋†ˆ์ด๋ƒ?') 82.89% ํ™•๋ฅ ๋กœ ๋ถ€์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. sentiment_predict('์™€ ๊ฐœ์ฉ๋‹ค ์ •๋ง ์„ธ๊ณ„๊ด€ ์ตœ๊ฐ•์ž๋“ค์˜ ์˜ํ™”๋‹ค') 85.93% ํ™•๋ฅ ๋กœ ๊ธ์ • ๋ฆฌ๋ทฐ์ž…๋‹ˆ๋‹ค. 11-06 ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ์ด์šฉํ•œ ์˜๋„ ๋ถ„๋ฅ˜(Intent Classification using Pre-trained Word Embedding) ์˜๋„ ๋ถ„๋ฅ˜(Intent Classification)๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition)๊ณผ ๋”๋ถˆ์–ด ์ฑ—๋ด‡(Chatbot)์˜ ์ค‘์š” ๋ชจ๋“ˆ๋กœ์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Pre-traiend word embedding)์„ ์ž…๋ ฅ์œผ๋กœ ์˜๋„ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์˜๋„ ๋ถ„๋ฅ˜ ์‹ค์Šต์€ ๊ฒฐ๊ตญ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์ž…๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ์™€ ์ „์ฒ˜๋ฆฌ import os import pandas as pd import numpy as np import matplotlib.pyplot as plt import urllib.request from sklearn import preprocessing from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical from sklearn.metrics import classification_report ๊นƒํ—ˆ๋ธŒ๋กœ๋ถ€ํ„ฐ ์˜๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/11.% 201D%20CNN%20Text%20Classification/dataset/intent_train_data.csv", filename="intent_train_data.csv") urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/11.% 201D%20CNN%20Text%20Classification/dataset/intent_test_data.csv", filename="intent_test_data.csv") train_data = pd.read_csv('intent_train_data.csv') test_data = pd.read_csv('intent_test_data.csv') ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. train_data ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. test_data ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๊ทธ๋ฆฌ๊ณ  ๋ ˆ์ด๋ธ”์„ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•˜๊ณ  ์ƒ˜ํ”Œ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. intent_train = train_data['intent'].tolist() label_train = train_data['label'].tolist() intent_test = test_data['intent'].tolist() label_test = test_data['label'].tolist() print('ํ›ˆ๋ จ์šฉ ๋ฌธ์žฅ์˜ ์ˆ˜ :', len(intent_train)) print('ํ›ˆ๋ จ์šฉ ๋ ˆ์ด๋ธ”์˜ ์ˆ˜ :', len(label_train)) print('ํ…Œ์ŠคํŠธ์šฉ ๋ฌธ์žฅ์˜ ์ˆ˜ :', len(intent_test)) print('ํ…Œ์ŠคํŠธ์šฉ ๋ ˆ์ด๋ธ”์˜ ์ˆ˜ :', len(label_test)) ํ›ˆ๋ จ์šฉ ๋ฌธ์žฅ์˜ ์ˆ˜ : 11784 ํ›ˆ๋ จ์šฉ ๋ ˆ์ด๋ธ”์˜ ์ˆ˜ : 11784 ํ…Œ์ŠคํŠธ์šฉ ๋ฌธ์žฅ์˜ ์ˆ˜ : 600 ํ…Œ์ŠคํŠธ์šฉ ๋ ˆ์ด๋ธ”์˜ ์ˆ˜ : 600 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ 5๊ฐœ ์ƒ˜ํ”Œ๊ณผ ๋ ˆ์ด๋ธ”์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(intent_train[:5]) print(label_train[:5]) ['add another song to the cita rom ntica playlist', 'add clem burke in my playlist pre party r b jams', 'add live from aragon ballroom to trapeo', 'add unite and win to my night out', 'add track to my digster future hits'] ['AddToPlaylist', 'AddToPlaylist', 'AddToPlaylist', 'AddToPlaylist', 'AddToPlaylist'] ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ๋ณด๋ฉด 'add another song to the cita rom ntica playlist'๋ผ๋Š” ๋ฌธ์žฅ์˜ ๋ ˆ์ด๋ธ”์€ 'AddToPlaylist'์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์žฅ์˜ ์˜๋„๋Š” ์ด ๊ณก์„ ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ด ์ค˜๋ผ๋Š” ์˜๋„์ž…๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋„ ์–ด๋–ค ์ข…๋ฅ˜์˜ ๋ฒ”์ฃผ๊ฐ€ ์žˆ๋Š”์ง€ ๋ณด๋ ค๋ฉด ์ •ํ™•ํ•˜๊ฒŒ ์ธ๋ฑ์Šค๋ฅผ 2000์”ฉ +ํ•˜๋ฉด์„œ ์ถœ๋ ฅํ•ด ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. print(intent_train[2000:2002]) print(label_train[2000:2002]) ['please book reservations for 3 people at a restaurant in alderwood manor', 'book a table in mt for 3 for now at a pub that serves south indian'] ['BookRestaurant', 'BookRestaurant'] print(intent_train[4000:4002]) print(label_train[4000:4002]) ['what will the weather be like on feb 8 , 2034 in cedar mountain wilderness', "tell me the forecast in the same area here on robert e lee 's birthday"] ['GetWeather', 'GetWeather'] print(intent_train[6000:6002]) print(label_train[6000:6002]) ['rate the current album one points', 'i give a zero rating for this essay'] ['RateBook', 'RateBook'] print(intent_train[8000:8002]) print(label_train[8000:8002]) ["i'm trying to find the show chant ii", 'find spirit of the bush'] ['SearchCreativeWork', 'SearchCreativeWork'] print(intent_train[10000:10002]) print(label_train[10000:10002]) ['when is blood and ice cream trilogie playing at the nearest movie theatre \\?', 'show movie schedules'] ['SearchScreeningEvent', 'SearchScreeningEvent'] ์ด๋ฅผ ํ†ตํ•ด ๋ˆˆ์น˜์ฑ„์…จ๊ฒ ์ง€๋งŒ, ์‚ฌ์‹ค ์ด ๋ฐ์ดํ„ฐ๋Š” ์ผ์ •ํ•œ ์ˆœ์„œ๋กœ ๋ฐฐ์น˜๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋’ค์—์„œ๋Š” ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋žœ๋ค์œผ๋กœ ์„ž์–ด์ฃผ๋Š” ์ž‘์—…์„ ํ•ด์ค๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜๋Š” ๋ ˆ์ด๋ธ”์˜ ๋ถ„ํฌ๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ์‹œ๊ฐํ™”ํ•˜์—ฌ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. train_data['label'].value_counts().plot(kind = 'bar') ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—๋Š” 6๊ฐœ์˜ ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. AddToPlaylist, BookRestaurant, GetWeather , RateBook , SearchCreativeWork, SearchScreeningEvent ๊ฐ๊ฐ์˜ ๋ฐ์ดํ„ฐ๋Š” ์•ฝ 2,000๊ฐœ์”ฉ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. label_train๊ณผ label_test์— ์กด์žฌํ•˜๋Š” 6๊ฐœ์˜ ์นดํ…Œ๊ณ ๋ฆฌ๋“ค์„ ๊ณ ์œ ํ•œ ์ •์ˆ˜๋กœ ์ธ์ฝ”๋”ฉ ํ•ด๋ด…์‹œ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ์‚ฌ์ดํ‚ท๋Ÿฐ(sklearn)์˜ preprocessing.LabelEncoder()๊ฐ€ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. label_idx์—๋Š” ์–ด๋–ค ๋ ˆ์ด๋ธ”์ด ์–ด๋–ค ์ •์ˆ˜์— ๋งคํ•‘๋˜์—ˆ๋Š”์ง€ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. # ๋ ˆ์ด๋ธ” ์ธ์ฝ”๋”ฉ. ๋ ˆ์ด๋ธ”์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌ idx_encode = preprocessing.LabelEncoder() idx_encode.fit(label_train) label_train = idx_encode.transform(label_train) # ์ฃผ์–ด์ง„ ๊ณ ์œ ํ•œ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ label_test = idx_encode.transform(label_test) # ๊ณ ์œ ํ•œ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ label_idx = dict(zip(list(idx_encode.classes_), idx_encode.transform(list(idx_encode.classes_)))) print('๋ ˆ์ด๋ธ”๊ณผ ์ •์ˆ˜์˜ ๋งคํ•‘ ๊ด€๊ณ„ :',label_idx) ๋ ˆ์ด๋ธ”๊ณผ ์ •์ˆ˜์˜ ๋งคํ•‘ ๊ด€๊ณ„ : {'AddToPlaylist': 0, 'BookRestaurant': 1, 'GetWeather': 2, 'RateBook': 3, 'SearchCreativeWork': 4, 'SearchScreeningEvent': 5} ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”๊ณผ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์œ„ 5๊ฐœ์”ฉ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(intent_train[:5]) print(label_train[:5]) ['add another song to the cita rom ntica playlist', 'add clem burke in my playlist pre party r b jams', 'add live from aragon ballroom to trapeo', 'add unite and win to my night out', 'add track to my digster future hits'] [0 0 0 0 0] print(intent_test[:5]) print(label_test[:5]) ["i 'd like to have this track onto my classical relaxations playlist", 'add the album to my flow espa ol playlist', 'add digging now to my young at heart playlist', 'add this song by too poetic to my piano ballads playlist', 'add this album to old school death metal'] [0 0 0 0 0] ์ด์ „์— 'AddToPlaylist'๋ผ๋Š” ๋ฌธ์ž์—ด๋กœ ์ €์žฅ๋ผ ์žˆ์—ˆ๋˜ ๋ ˆ์ด๋ธ”์ด ์ •์ˆ˜ 0์œผ๋กœ ๋ณ€ํ™˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์˜๋„ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ๋„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ์˜๋„ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด์–ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋ฅผ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. tokenizer = Tokenizer() tokenizer.fit_on_texts(intent_train) sequences = tokenizer.texts_to_sequences(intent_train) sequences[:5] # ์ƒ์œ„ 5๊ฐœ ์ƒ˜ํ”Œ ์ถœ๋ ฅ [[11, 191, 61, 4, 1, 4013, 1141, 1572, 15], [11, 2624, 1573, 3, 14, 15, 939, 82, 256, 188, 548], [11, 187, 42, 2625, 4014, 4, 1968], [11, 2626, 22, 2627, 4, 14, 192, 27], [11, 92, 4, 14, 651, 520, 195]] ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. word_index = tokenizer.word_index vocab_size = len(word_index) + 1 print('๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์˜ ํฌ๊ธฐ :',vocab_size) ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์˜ ํฌ๊ธฐ : 9870 ํŒจ๋”ฉ์„ ์œ„ํ•ด์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max(len(l) for l in sequences)) print('๋ฌธ์žฅ์˜ ํ‰๊ท  ๊ธธ์ด :',sum(map(len, sequences))/len(sequences)) plt.hist([len(s) for s in sequences], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 35 ๋ฌธ์žฅ์˜ ํ‰๊ท  ๊ธธ์ด : 9.364392396469789 ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 35์ด๋ฏ€๋กœ, ์ตœ๋Œ€ ๊ธธ์ด 35๋กœ ๋ชจ๋“  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ํŒจ๋”ฉ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์˜ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. max_len = 35 intent_train = pad_sequences(sequences, maxlen = max_len) label_train = to_categorical(np.asarray(label_train)) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape):', intent_train.shape) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ(shape):', label_train.shape) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape) : (11784, 35) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ(shape) : (11784, 6) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :',intent_train[0]) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” :',label_train[0]) ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 191 61 4 1 4013 1141 1572 15] ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : [1. 0. 0. 0. 0. 0.] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์ผ์ •ํ•œ ์ˆœ์„œ๋กœ ๋ฐฐ์น˜๋ผ ์žˆ์œผ๋ฏ€๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ์•ž์˜ 10%๋‚˜ ์ค‘๊ฐ„ 10%๋‚˜ ๋’ค์˜ 10%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ–ˆ๋‹ค๊ฐ€๋Š” ์šด์ด ๋‚˜์˜๋ฉด ํŠน์ • ๋ ˆ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋“ค๋งŒ์„ ๋ถ„๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— 0๋ฒˆ ๋ ˆ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋งŒ ์žˆ๋‹ค๋ฉด ์ œ๋Œ€๋กœ ๋œ ๊ฒ€์ฆ์ด ์•ˆ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ „์— ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๋ฅผ ๋žœ๋ค์œผ๋กœ ์„ž์–ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ˆœ์„œ๊ฐ€ ๋’ค์ฃฝ๋ฐ•์ฃฝ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. indices = np.arange(intent_train.shape[0]) np.random.shuffle(indices) print('๋žœ๋ค ์‹œํ€€์Šค :',indices) ๋žœ๋ค ์‹œํ€€์Šค : [1147 9504 9615 ... 4685 227 8774] ์ด ์ •์ˆ˜์˜ ์ˆœ์„œ๋ฅผ ๊ฐ ์ƒ˜ํ”Œ์˜ ์ˆœ์„œ๊ฐ€ ๋˜๋„๋ก ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์„ž์–ด์ค๋‹ˆ๋‹ค. intent_train = intent_train[indices] label_train = label_train[indices] ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์ค‘ 10%๋งŒ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์— 0.1์„ ๊ณฑํ•˜๋ฉด ๋ช‡์ผ๊นŒ์š”? n_of_val = int(0.1 * intent_train.shape[0]) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :',n_of_val) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 1178 1,178์ด๋„ค์š”. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” 1,178๊ฐœ๋งŒ ์‚ฌ์šฉํ•˜๋„๋ก ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋ถ„๋ฆฌํ•ด ์ค๋‹ˆ๋‹ค. X_train = intent_train[:-n_of_val] y_train = label_train[:-n_of_val] X_val = intent_train[-n_of_val:] y_val = label_train[-n_of_val:] X_test = intent_test y_test = label_test print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape):', X_train.shape) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape):', X_val.shape) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ(shape):', y_train.shape) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ(shape):', y_val.shape) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :', len(X_test)) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ :', len(y_test)) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape): (10606, 35) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape): (1178, 35) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ(shape): (10606, 6) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ(shape): (1178, 6) ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 600 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ : 600 2. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์‚ฌ์šฉํ•˜๊ธฐ ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™๊ต์—์„œ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. !wget http://nlp.stanford.edu/data/glove.6B.zip !unzip glove*.zip ์•„๋ž˜ ์ฝ”๋“œ์˜ ์ƒ์„ธ ๋‚ด์šฉ์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์‹ค์Šต์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. embedding_dict = dict() f = open(os.path.join('glove.6B.100d.txt'), encoding='utf-8') for line in f: word_vector = line.split() word = word_vector[0] word_vector_arr = np.asarray(word_vector[1:], dtype='float32') # 100๊ฐœ์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” array๋กœ ๋ณ€ํ™˜ embedding_dict[word] = word_vector_arr f.close() print('%s ๊ฐœ์˜ Embedding vector๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.' % len(embedding_dict)) 400000๊ฐœ์˜ Embedding vector๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด 40๋งŒ ๊ฐœ์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ž„์˜๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์—์„œ ๋‹จ์–ด 'respectable' ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’๊ณผ ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. print(embedding_dict['respectable']) print(len(embedding_dict['respectable'])) [-0.049773 0.19903 0.10585 0.1391 -0.32395 0.44053 0.3947 -0.22805 -0.25793 0.49768 0.15384 -0.08831 0.0782 -0.8299 -0.037788 0.16772 -0.45197 -0.17085 0.74756 0.98256 0.81872 0.28507 0.16178 -0.48626 -0.006265 -0.92469 -0.30625 -0.067318 -0.046762 -0.76291 -0.0025264 -0.018795 0.12882 -0.52457 0.3586 0.43119 -0.89477 -0.057421 -0.53724 0.25587 0.55195 0.44698 -0.24252 0.29946 0.25776 -0.8717 0.68426 -0.05688 -0.1848 -0.59352 -0.11227 -0.57692 -0.013593 0.18488 -0.32507 -0.90171 0.17672 0.075601 0.54896 -0.21488 -0.54018 -0.45882 -0.79536 0.26331 0.18879 -0.16363 0.3975 0.1099 0.1164 -0.083499 0.50159 0.35802 0.25677 0.088546 0.42108 0.28674 -0.71285 -0.82915 0.15297 -0.82712 0.022112 1.067 -0.31776 0.1211 -0.069755 -0.61327 0.27308 -0.42638 -0.085084 -0.17694 -0.0090944 0.1109 0.62543 -0.23682 -0.44928 -0.3667 -0.21616 -0.19187 -0.032502 0.38025 ] 100 ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 100์ฐจ์›์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•  ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์„ ๊ตฌ์ถ•ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์˜ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 100์ด๋ฏ€๋กœ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์˜ ์—ด๋„ 100์ฐจ์›์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. vocab_size๋ฅผ ํ–‰์˜ ํฌ๊ธฐ๋กœ, ์—ด์˜ ํฌ๊ธฐ๋Š” 100์ธ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. embedding_dim = 100 embedding_matrix = np.zeros((vocab_size, embedding_dim)) print('์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์˜ ํฌ๊ธฐ(shape) :',np.shape(embedding_matrix)) ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์˜ ํฌ๊ธฐ(shape) : (9870, 100) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋‹จ์–ด์™€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ๊ฐ’์„ ๋งคํ•‘ํ•˜์—ฌ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. for word, i in word_index.items(): embedding_vector = embedding_dict.get(word) if embedding_vector is not None: embedding_matrix[i] = embedding_vector 3. 1D CNN์„ ์ด์šฉํ•œ ์˜๋„ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์€ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ Multi-Kernel 1D CNN ๊ตฌ์กฐ๋ฅผ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Model from tensorflow.keras.layers import Embedding, Dropout, Conv1D, GlobalMaxPooling1D, Dense, Input, Flatten, Concatenate kernel_sizes = [2, 3, 5] num_filters = 512 dropout_ratio = 0.5 model_input = Input(shape=(max_len,)) output = Embedding(vocab_size, embedding_dim, weights=[embedding_matrix], input_length=max_len, trainable=False)(model_input) conv_blocks = [] for size in kernel_sizes: conv = Conv1D(filters=num_filters, kernel_size=size, padding="valid", activation="relu", strides=1)(output) conv = GlobalMaxPooling1D()(conv) conv_blocks.append(conv) output = Concatenate()(conv_blocks) if len(conv_blocks) > 1 else conv_blocks[0] output = Dropout(dropout_ratio)(output) model_output = Dense(len(label_idx), activation='softmax')(output) model = Model(model_input, model_output) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc']) model.summary() ์•ž์„œ ๋ถ„๋ฆฌํ•ด๋‘์—ˆ๋˜ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ์„ฑ๋Šฅ์„ ์ ๊ฒ€ํ•˜๋ฉด์„œ ํ›ˆ๋ จ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. history = model.fit(X_train, y_train, batch_size=64, epochs=10, validation_data=(X_val, y_val)) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ 99%, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์—์„œ 98%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. ์ •ํ™•๋„์™€ loss์˜ ๋ณ€ํ™”๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ์‹œ๊ฐํ™”ํ•ด๋ด…์‹œ๋‹ค. epochs = range(1, len(history.history['acc']) + 1) plt.plot(epochs, history.history['acc']) plt.plot(epochs, history.history['val_acc']) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epochs') plt.legend(['train', 'test'], loc='lower right') plt.show() epochs = range(1, len(history.history['loss']) + 1) plt.plot(epochs, history.history['loss']) plt.plot(epochs, history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epochs') plt.legend(['train', 'test'], loc='upper right') plt.show() ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ‰๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. X_test = tokenizer.texts_to_sequences(X_test) X_test = pad_sequences(X_test, maxlen=max_len) y_predicted = model.predict(X_test) y_predicted = y_predicted.argmax(axis=-1) # ์˜ˆ์ธก์„ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ print('์ •ํ™•๋„(Accuracy) : ', sum(y_predicted == y_test) / len(y_test)) ์ •ํ™•๋„(Accuracy) : 0.99 99%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. 11-07 ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ(Character Embedding) ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๊ณผ๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹จ์–ด์˜ ๋ฒกํ„ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ์–ป๋Š” ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ(Character Embedding)์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 'understand'๋ผ๋Š” ๋‹จ์–ด๋Š” '์ดํ•ดํ•˜๋‹ค'๋ผ๋Š” ๋œป์„ ๊ฐ€์ง„ ์˜๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์— mis-๋ฅผ ์•ž์— ๋ถ™์—ฌ์ฃผ๊ฒŒ ๋˜๋ฉด, 'misunderstand'๋ผ๋Š” '์˜คํ•ดํ•˜๋‹ค'๋ผ๋Š” ๋œป์˜ ๋‹ค๋ฅธ ์˜๋ฏธ์˜ ์˜๋‹จ์–ด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋น„์Šทํ•œ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 'underestimate'๋ผ๋Š” ๋‹จ์–ด๋Š” '๊ณผ์†Œํ‰๊ฐ€ํ•˜๋‹ค'๋ผ๋Š” ๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด 'misunderestimate'๋Š” ๋ฌด์Šจ ๋œป์ผ๊นŒ์š”? ์‚ฌ์‹ค ์ด ๋‹จ์–ด๋Š” ์‹ค์กดํ•˜๋Š” ๋‹จ์–ด๊ฐ€ ์•„๋‹˜์—๋„ ์ด ๋‹จ์–ด์˜ ๋œป์„ ์ถ”์ธกํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ mis-๋ผ๋Š” ์ ‘๋‘์‚ฌ๋Š” '์ž˜๋ชป ํŒ๋‹จํ•˜๋Š”'์ด๋ผ๋Š” ์˜๋ฏธ์˜ 'mistaken'์˜ ์˜๋ฏธ๋ฅผ ๋‹ด๊ณ  ์žˆ์œผ๋ฏ€๋กœ '๊ณผ์†Œํ‰๊ฐ€ํ•˜๋‹ค'๋ผ๋Š” ๋‹จ์–ด ์•ž์— mis-๋ผ๋Š” ์ ‘๋‘์‚ฌ๊ฐ€ ๋ถ™์—ˆ๋‹ค๋ฉด 'misunderestimate'๋Š” '์ž˜๋ชป ๊ณผ์†Œํ‰๊ฐ€ํ•˜๋‹ค'๋ผ๋Š” ์ถ”์ธก์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์€ ์‚ฌ๋žŒ์˜ ์ด๋Ÿฌํ•œ ์ดํ•ด ๋Šฅ๋ ฅ์„ ํ‰๋‚ด ๋‚ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” CNN๊ณผ RNN์„ ์ด์šฉํ•œ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค. 1. 1D CNN์„ ์ด์šฉํ•œ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ 1D CNN์€ ์ „์ฒด ์‹œํ€€์Šค ์ž…๋ ฅ ์•ˆ์˜ ๋” ์ž‘์€ ์‹œํ€€์Šค์— ์ง‘์ค‘ํ•˜์—ฌ ์ •๋ณด๋ฅผ ์–ป์–ด๋‚ด๋Š” ๋™์ž‘์„ ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. FastText๊ฐ€ ๋ฌธ์ž์˜ N-gram์˜ ์กฐํ•ฉ์„ ์ด์šฉํ•˜์—ฌ OOV ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋“ฏ์ด, 1D CNN์„ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์— ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์ž์˜ N-gram์œผ๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ์–ป์–ด๋‚ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์•ž์„œ ํ•™์Šตํ–ˆ๋˜ 1D CNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ดํ•ดํ–ˆ๋‹ค๋ฉด ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹จ์–ด๋ฅผ ๋ฌธ์ž ๋‹จ์œ„๋กœ ์ชผ๊ฐœ๊ณ  ๋‚˜์„œ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์™ธ์—๋Š” ๋‹ฌ๋ผ์ง„ ๊ฒƒ์ด ์—†์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ž„์˜์˜ ๋‹จ์–ด 'have'์— ๋Œ€ํ•ด์„œ 1D CNN์„ ํ†ตํ•ด์„œ ๋‹จ์–ด ํ‘œํ˜„ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์šฐ์„ , ๋‹จ์–ด 'have'๋ฅผ 'h', 'a', 'v', 'e'์™€ ๊ฐ™์ด ๋ฌธ์ž ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)์„ ์ด์šฉํ•œ ์ž„๋ฒ ๋”ฉ์„ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ฌธ์ž๋ฅผ ์ž„๋ฒ ๋”ฉํ•ฉ๋‹ˆ๋‹ค. (๋ฌธ์žฅ์˜ ๊ธธ์ด๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ํŒจ๋”ฉ์„ ํ–ˆ๋˜ ์ง€๋‚œ ์‹ค์Šต ์˜ˆ์‹œ๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์—ฌ๊ธฐ์„œ๋„ ํŒจ๋”ฉ์€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.) ๊ทธ ํ›„ 1D CNN์„ ์ ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์œ„์˜ ๊ทธ๋ฆผ์€ ์ปค๋„์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 4์ธ ์ปค๋„ 2๊ฐœ, 3์ธ ์ปค๋„ 2๊ฐœ, 2์ธ ์ปค๋„ 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฒกํ„ฐ๊ฐ€ 6๊ฐœ๋ฏ€๋กœ ๋งฅ์Šค ํ’€๋ง์„ ํ•œ ํ›„์—๋Š” 6๊ฐœ์˜ ์Šค์นผ๋ผ ๊ฐ’์„ ์–ป๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ์–ป์€ ์Šค์นผ๋ผ ๊ฐ’๋“ค์€ ์ „๋ถ€ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ์ด๋ ‡๊ฒŒ ์–ป์€ ๋ฒกํ„ฐ๋ฅผ ๋‹จ์–ด 'have'์˜ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ๋Š” ๋ฌธ์ž ๋ ˆ๋ฒจ ํ‘œํ˜„(Character-level representation)์ด๋ผ๊ณ  ๊ธฐ์žฌ๋œ ๋ฒกํ„ฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์–ป์„ ๊ฒฝ์šฐ, ์–ด๋–ค ๋‹จ์–ด์ด๋“  ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž ๋ ˆ๋ฒจ๋กœ ์ชผ๊ฐœ๋ฏ€๋กœ ๊ธฐ์กด ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์˜ ์ ‘๊ทผ์—์„œ OOV๋ผ๊ณ  ํ•˜๋”๋ผ๋„ ๋ฒกํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, 'docker'๋ผ๋Š” ์˜๋‹จ์–ด๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์—†์—ˆ์œผ๋‚˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜๋Š” ๋‹จ์–ด์˜€๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. Word2Vec์ด๋‚˜ GloVe์˜ ๊ฒฝ์šฐ์—๋Š” OOV ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ, 1D CNN์„ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” 'd', 'o', 'c', 'k', 'e', 'r'๋กœ ์ „๋ถ€ ๋ถ„๋ฆฌ๋˜์–ด ๊ฐ ๋ฌธ์ž๋กœ ์ž„๋ฒ ๋”ฉ์ด ๋˜๊ณ ๋‚˜์„œ 1D CNN์„ ๊ฑฐ์นœ ํ›„์— 'docker'์˜ ๋ฒกํ„ฐ๋ฅผ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 2. BiLSTM์„ ์ด์šฉํ•œ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์–ป๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” BiLSTM์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. 1D CNN ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹จ์–ด๋ฅผ ๋ฌธ์ž๋กœ ์ชผ๊ฐ  ํ›„, ์ž„๋ฒ ๋”ฉ ์ธต์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ๋„ BiLSTM์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ดํ•ดํ–ˆ๋‹ค๋ฉด ์‰ฝ์Šต๋‹ˆ๋‹ค. BiLSTM์˜ ๋‹ค ๋Œ€ ์ผ(many-to-one) ๊ตฌ์กฐ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต๋‹ค๋ฉด BiLSTM์„ ์ด์šฉํ•œ ํ•œ๊ตญ์–ด ์ŠคํŒ€ ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‹ค์Šต์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ์ž„์˜์˜ ๋‹จ์–ด 'have'์— ๋Œ€ํ•ด์„œ BiLSTM์„ ํ†ตํ•ด์„œ ๋‹จ์–ด ํ‘œํ˜„ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์šฐ์„ , ๋‹จ์–ด 'have'๋ฅผ 'h', 'a', 'v', 'e'์™€ ๊ฐ™์ด ๋ฌธ์ž ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)์„ ์ด์šฉํ•œ ์ž„๋ฒ ๋”ฉ์„ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ฌธ์ž๋ฅผ ์ž„๋ฒ ๋”ฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ์ˆœ๋ฐฉํ–ฅ LSTM์€ ๋‹จ์–ด ์ˆœ๋ฐฉํ–ฅ์œผ๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ์—ญ๋ฐฉํ–ฅ LSTM์€ ๋‹จ์–ด์˜ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ˆœ๋ฐฉํ–ฅ LSTM์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์—ญ๋ฐฉํ–ฅ LSTM์˜ ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์—ฐ๊ฒฐ(concatenate) ํ•ฉ๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ์ด๋ ‡๊ฒŒ ์–ป์€ ๋ฒกํ„ฐ๋ฅผ ๋‹จ์–ด 'have'์˜ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ๋Š” ๋ฌธ์ž ๋ ˆ๋ฒจ ํ‘œํ˜„(Character-level representation)์ด๋ผ๊ณ  ๊ธฐ์žฌ๋œ ๋ฒกํ„ฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์˜ ๋Œ€์ฒด์žฌ๋กœ์„œ ์“ฐ๊ฑฐ๋‚˜, ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๊ณผ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ์‹ ๊ฒฝ๋ง์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 12. ํƒœ๊น… ์ž‘์—…(Tagging Task) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ถ„์•ผ์—์„œ ๊ฐ ๋‹จ์–ด๊ฐ€ ์–ด๋–ค ์œ ํ˜•์— ์†ํ•ด์žˆ๋Š”์ง€๋ฅผ ์•Œ์•„๋‚ด๋Š” ํƒœ๊น… ์ž‘์—…์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋‹จ์–ด ํƒœ๊น… ์ž‘์—…์˜ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ ๋‘ ๊ฐ€์ง€๋กœ ๊ฐ ๋‹จ์–ด์˜ ์œ ํ˜•์ด ์‚ฌ๋žŒ, ์žฅ์†Œ, ๋‹จ์ฒด ๋“ฑ ์–ด๋–ค ์œ ํ˜•์ธ์ง€๋ฅผ ์•Œ์•„๋‚ด๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition) ๊ณผ ๊ฐ ๋‹จ์–ด์˜ ํ’ˆ์‚ฌ๊ฐ€ ๋ช…์‚ฌ, ๋™์‚ฌ, ํ˜•์šฉ์‚ฌ ์ธ์ง€๋ฅผ ์•Œ์•„๋‚ด๋Š” ํ’ˆ์‚ฌ ํƒœ๊น…(Part-of-Speech Tagging) ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” RNN์˜ ๋‹ค ๋Œ€๋‹ค(many-to-many) ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๊ธฐ๋ฐ˜์˜ ๊ฐœ์ฒด๋ช… ์ธ์‹ ๊ธฐ์™€ ํ’ˆ์‚ฌ ํƒœ๊ฑฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฏธ๋‹ˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 12-01 ์ผ€๋ผ์Šค๋ฅผ ์ด์šฉํ•œ ํƒœ๊น… ์ž‘์—… ๊ฐœ์š”(Tagging Task using Keras) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ผ€๋ผ์Šค(Keras)๋กœ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•˜์—ฌ ํƒœ๊น… ์ž‘์—…์„ ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ฐœ์ฒด๋ช… ์ธ์‹ ๊ธฐ์™€ ํ’ˆ์‚ฌ ํƒœ๊ฑฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ, ์ด๋Ÿฌํ•œ ๋‘ ์ž‘์—…์˜ ๊ณตํ†ต์ ์€ RNN์˜ ๋‹ค-๋Œ€-๋‹ค(Many-to-Many) ์ž‘์—…์ด๋ฉด์„œ ๋˜ํ•œ ์•ž, ๋’ค ์‹œ์ ์˜ ์ž…๋ ฅ์„ ๋ชจ๋‘ ์ฐธ๊ณ ํ•˜๋Š” ์–‘๋ฐฉํ–ฅ RNN(Bidirectional RNN)์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ „์ฒด์ ์œผ๋กœ ์‹ค์Šต์˜ ์ง„ํ–‰ ๋ฐฉํ–ฅ์„ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์ดํ•ด๋ฅผ ์œ„ํ•ด ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๊ฐœ์š” ์ฑ•ํ„ฐ์™€ ๋น„๊ตํ•˜๋ฉฐ ๊ฐ™์ด ์ฝ๊ธฐ๋ฅผ ๊ถŒํ•ฉ๋‹ˆ๋‹ค. 1. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด ํƒœ๊น… ์ž‘์—…์€ ์•ž์„œ ๋ฐฐ์šด ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…๊ณผ ๋™์ผํ•˜๊ฒŒ ์ง€๋„ ํ•™์Šต(Supervised Learning)์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ•ํ„ฐ์—์„œ๋Š” ํƒœ๊น…์„ ํ•ด์•ผ ํ•˜๋Š” ๋‹จ์–ด ๋ฐ์ดํ„ฐ๋ฅผ X, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” ํƒœ๊น… ์ •๋ณด ๋ฐ์ดํ„ฐ๋Š” y๋ผ๊ณ  ์ด๋ฆ„์„ ๋ถ™์˜€์Šต๋‹ˆ๋‹ค. X์— ๋Œ€ํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” X_train, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” X_test๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ณ  y์— ๋Œ€ํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” y_train, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” y_test๋ผ๊ณ  ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ X์™€ y ๋ฐ์ดํ„ฐ์˜ ์Œ(pair)์€ ๋ณ‘๋ ฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. X์™€ y์˜ ๊ฐ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ 4๊ฐœ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ณธ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # X_train y_train ๊ธธ์ด 0 ['EU', 'rejects', 'German', 'call', 'to', 'boycott', 'British', 'lamb'] ['B-ORG', 'O', 'B-MISC', 'O', 'O', 'O', 'B-MISC', 'O'] 8 1 ['peter', 'blackburn'] ['B-PER', 'I-PER'] 2 2 ['brussels', '1996-08-22' ] ['B-LOC', 'O'] 2 3 ['The', 'European', 'Commission'] ['O', 'B-ORG', 'I-ORG'] 3 ๊ฐ€๋ น, X_train[3]์˜ 'The'์™€ y_train[3]์˜ 'O'๋Š” ํ•˜๋‚˜์˜ ์Œ(pair)์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, X_train[3]์˜ 'European'๊ณผ y_train[3]์˜ 'B-ORG'๋Š” ์Œ์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋ฉฐ, X_train[3]์˜ 'Commision'๊ณผ y_train[3]์˜ 'I-ORG'๋Š” ์Œ์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณ‘๋ ฌ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋Š” ํ† ํฐ ํ™”๊ฐ€ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„, ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ฃผ๊ธฐ ์œ„ํ•œ ํŒจ๋”ฉ(Padding) ์ž‘์—…์„ ๊ฑฐ์นœ ํ›„์— ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 2. ์‹œํ€€์Šค ๋ ˆ์ด๋ธ”๋ง(Sequence Labeling) ์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅ ์‹œํ€€์Šค X = [ 1 x, 3 , ..., n ]์— ๋Œ€ํ•˜์—ฌ ๋ ˆ์ด๋ธ” ์‹œํ€€์Šค y = [ 1 y, 3 , ..., n ]๋ฅผ ๊ฐ๊ฐ ๋ถ€์—ฌํ•˜๋Š” ์ž‘์—…์„ ์‹œํ€€์Šค ๋ ˆ์ด๋ธ”๋ง ์ž‘์—…(Sequence Labeling Task)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํƒœ๊น… ์ž‘์—…์€ ๋Œ€ํ‘œ์ ์ธ ์‹œํ€€์Šค ๋ ˆ์ด๋ธ”๋ง ์ž‘์—…์ž…๋‹ˆ๋‹ค. 3. ์–‘๋ฐฉํ–ฅ LSTM(Bidirectional LSTM) model.add(Bidirectional(LSTM(hidden_units, return_sequences=True))) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—๋Š” ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์‹œ์ ์˜ ๋‹จ์–ด ์ •๋ณด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๋‹ค์Œ ์‹œ์ ์˜ ๋‹จ์–ด ์ •๋ณด๋„ ์ฐธ๊ณ ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ์€ ๊ธฐ์กด์˜ ๋‹จ๋ฐฉํ–ฅ LSTM()์„ Bidirectional() ์•ˆ์— ๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. LSTM์˜ ์ธ์ž ๊ฐ’์€ ๋‹จ๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•  ๋•Œ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ธ์ž ๊ฐ’์„ ํ•˜๋‚˜๋ฅผ ์ค„ ๊ฒฝ์šฐ์—๋Š” ์ด๋Š” ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›์„ ์˜๋ฏธํ•˜๋ฉฐ, ์œ„ ์ฝ”๋“œ ์ƒ์œผ๋กœ๋Š” hidden_units๋กœ ๊ธฐ์žฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 4. RNN์˜ ๋‹ค-๋Œ€-๋‹ค(Many-to-Many) ๋ฌธ์ œ RNN์˜ ์€๋‹‰์ธต์€ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜๋„, ๋งˆ์ง€๋ง‰ ์‹œ์ ์— ๋Œ€ํ•ด์„œ๋งŒ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ์ž๋กœ return_sequences=True๋ฅผ ๋„ฃ์„ ๊ฒƒ์ธ์ง€, ๋„ฃ์ง€ ์•Š์„ ๊ฒƒ์ธ์ง€๋กœ(๋„ฃ์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ False์ด๋ฏ€๋กœ return_sequences=False๋กœ ์ธ์‹.) ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ํƒœ๊น… ์ž‘์—…์˜ ๊ฒฝ์šฐ์—๋Š” ๋‹ค ๋Œ€๋‹ค(many-to-many) ๋ฌธ์ œ๋กœ return_sequences=True๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ถœ๋ ฅ์ธต์— ๋ชจ๋“  ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค. RNN์ด ์–ด๋–ป๊ฒŒ ์„ค๊ณ„๋˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋ฐ์ดํ„ฐ ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ์— ํ•ด๋‹น๋˜๋Š” X_train[0]๋ฅผ ๊ฐ€์ง€๊ณ  4๋ฒˆ์˜ ์‹œ์ (time steps)๊นŒ์ง€ RNN์„ ์ง„ํ–‰ํ•˜์˜€์„ ๋•Œ์˜ ๊ทธ๋ฆผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์–‘๋ฐฉํ–ฅ RNN์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 12-02 ์–‘๋ฐฉํ–ฅ LSTM๋ฅผ ์ด์šฉํ•œ ํ’ˆ์‚ฌ ํƒœ๊น…(Part-of-speech Tagging using Bi-LSTM) ํ’ˆ์‚ฌ ํƒœ๊น…์— ๋Œ€ํ•ด์„œ๋Š” ์ด๋ฏธ ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ ์ฑ•ํ„ฐ์—์„œ ํ† ํฐํ™”์™€ ํ•จ๊ป˜ ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๋‹น์‹œ์—๋Š” NLTK์™€ KoNLPy๋ฅผ ์ด์šฉํ•ด์„œ ํ’ˆ์‚ฌ ํƒœ๊น…์„ ์ˆ˜ํ–‰ํ•˜์˜€์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ์ง์ ‘ ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•œ ํ’ˆ์‚ฌ ํƒœ๊น…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ด…๋‹ˆ๋‹ค. 1. ํ’ˆ์‚ฌ ํƒœ๊น… ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•ด์„œ ํ’ˆ์‚ฌ ํƒœ๊น…์„ ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import nltk import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical from sklearn.model_selection import train_test_split NLTK๋ฅผ ์ด์šฉํ•˜๋ฉด ์˜์–ด ์ฝ”ํผ์Šค์— ํ† ํฐํ™”์™€ ํ’ˆ์‚ฌ ํƒœ๊น… ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•œ ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ์‹œ์ผœ ํ’ˆ์‚ฌ ํƒœ๊น…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ฌธ์žฅ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # ํ† ํฐํ™”์— ํ’ˆ์‚ฌ ํƒœ๊น…์ด ๋œ ๋ฐ์ดํ„ฐ ๋ฐ›์•„์˜ค๊ธฐ tagged_sentences = nltk.corpus.treebank.tagged_sents() print("ํ’ˆ์‚ฌ ํƒœ๊น…์ด ๋œ ๋ฌธ์žฅ ๊ฐœ์ˆ˜: ", len(tagged_sentences)) ํ’ˆ์‚ฌ ํƒœ๊น…์ด ๋œ ๋ฌธ์žฅ ๊ฐœ์ˆ˜: 3914 ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(tagged_sentences[0]) [('Pierre', 'NNP'), ('Vinken', 'NNP'), (',', ','), ('61', 'CD'), ('years', 'NNS'), ('old', 'JJ'), (',', ','), ('will', 'MD'), ('join', 'VB'), ('the', 'DT'), ('board', 'NN'), ('as', 'IN'), ('a', 'DT'), ('nonexecutive', 'JJ'), ('director', 'NN'), ('Nov.', 'NNP'), ('29', 'CD'), ('.', '.')] ํ’ˆ์‚ฌ ํƒœ๊น… ์ „์ฒ˜๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋œ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์ด ์ด 3,914๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ›ˆ๋ จ์„ ์‹œํ‚ค๋ ค๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋‹จ์–ด์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„๊ณผ ํ’ˆ์‚ฌ ํƒœ๊น… ์ •๋ณด์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, [('Pierre', 'NNP'), ('Vinken', 'NNP')]์™€ ๊ฐ™์€ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์ด ์žˆ๋‹ค๋ฉด Pierre๊ณผ Vinken์„ ๊ฐ™์ด ์ €์žฅํ•˜๊ณ , NNP์™€ NNP๋ฅผ ๊ฐ™์ด ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ํŒŒ์ด์ฌ ํ•จ์ˆ˜ ์ค‘์—์„œ zip() ํ•จ์ˆ˜๊ฐ€ ์œ ์šฉํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. zip() ํ•จ์ˆ˜๋Š” ๋™์ผํ•œ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์‹œํ€€์Šค ์ž๋ฃŒํ˜•์—์„œ ๋™์ผํ•œ ์ˆœ์„œ์— ๋“ฑ์žฅํ•˜๋Š” ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. sentences, pos_tags = [], [] for tagged_sentence in tagged_sentences: # 3,914๊ฐœ์˜ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ 1๊ฐœ์”ฉ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. sentence, tag_info = zip(*tagged_sentence) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด๋“ค์€ sentence์— ํ’ˆ์‚ฌ ํƒœ๊น… ์ •๋ณด๋“ค์€ tag_info์— ์ €์žฅํ•œ๋‹ค. sentences.append(list(sentence)) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด ์ •๋ณด๋งŒ ์ €์žฅํ•œ๋‹ค. pos_tags.append(list(tag_info)) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ํ’ˆ์‚ฌ ํƒœ๊น… ์ •๋ณด๋งŒ ์ €์žฅํ•œ๋‹ค. ๊ฐ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด๋Š” sentences์— ํƒœ๊น… ์ •๋ณด๋Š” pos_tags์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(sentences[0]) print(pos_tags[0]) ['Pierre' 'Vinken' ',' '61' 'years' 'old' ',' 'will' 'join' 'the' 'board' 'as' 'a' 'nonexecutive' 'director' 'Nov.' '29' '.'] ['NNP' 'NNP' ',' 'CD' 'NNS' 'JJ' ',' 'MD' 'VB' 'DT' 'NN' 'IN' 'DT' 'JJ' 'NN' 'NNP' 'CD' '.'] ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ sentences[0]์—, ํ’ˆ์‚ฌ์— ๋Œ€ํ•ด์„œ๋งŒ pos_tags[0]์— ์ €์žฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ณด๊ฒ ์ง€๋งŒ, sentences๋Š” ์˜ˆ์ธก์„ ์œ„ํ•œ X์— ํ•ด๋‹น๋˜๋ฉฐ pos_tags๋Š” ์˜ˆ์ธก ๋Œ€์ƒ์ธ y์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ž„์˜๋กœ 8๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ๋„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(sentences[8]) print(pos_tags[8]) ['We', "'re", 'talking', 'about', 'years', 'ago', 'before', 'anyone', 'heard', 'of', 'asbestos', 'having', 'any', 'questionable', 'properties', '.'] ['PRP', 'VBP', 'VBG', 'IN', 'NNS', 'IN', 'IN', 'NN', 'VBD', 'IN', 'NN', 'VBG', 'DT', 'JJ', 'NNS', '.'] ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋งŒ sentences[8]์—, ๋˜ํ•œ ํ’ˆ์‚ฌ์— ๋Œ€ํ•ด์„œ๋งŒ pos_tags[8]์— ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๊ณผ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค 3,914๊ฐœ์˜ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” ์ „๋ถ€ ์ œ๊ฐ๊ฐ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : %d' % max(len(l) for l in sentences)) print('์ƒ˜ํ”Œ์˜ ํ‰๊ท  ๊ธธ์ด : %f' % (sum(map(len, sentences))/len(sentences))) plt.hist([len(s) for s in sentences], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 271 ์ƒ˜ํ”Œ์˜ ํ‰๊ท  ๊ธธ์ด : 25.722024 ์œ„์˜ ๊ทธ๋ž˜ํ”„๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๊ฐ€ 150 ์ด๋‚ด๋ฉฐ ๋Œ€๋ถ€๋ถ„ 0~50์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด์ œ ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ†ตํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. def tokenize(samples): tokenizer = Tokenizer() tokenizer.fit_on_texts(samples) return tokenizer ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” src_tokenizer๋ฅผ, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” ํ’ˆ์‚ฌ ํƒœ๊น… ์ •๋ณด์— ๋Œ€ํ•ด์„œ๋Š” tar_tokenizer๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. src_tokenizer = tokenize(sentences) tar_tokenizer = tokenize(pos_tags) ๋‹จ์–ด ์ง‘ํ•ฉ๊ณผ ํ’ˆ์‚ฌ ํƒœ๊น… ์ •๋ณด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. vocab_size = len(src_tokenizer.word_index) + 1 tag_size = len(tar_tokenizer.word_index) + 1 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(vocab_size)) print('ํƒœ๊น… ์ •๋ณด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(tag_size)) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 11388 ํƒœ๊น… ์ •๋ณด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 47 ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. X_train = src_tokenizer.texts_to_sequences(sentences) y_train = tar_tokenizer.texts_to_sequences(pos_tags) ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋Š” X_train, ํ’ˆ์‚ฌ ํƒœ๊น… ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋Š” y_train์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ์„ ์œ„ํ•ด ์ž„์˜๋กœ 2๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(X_train[:2]) print(y_train[:2]) [[5601, 3746, 1, 2024, 86, 331, 1, 46, 2405, 2, 131, 27, 6, 2025, 332, 459, 2026, 3], [31, 3746, 20, 177, 4, 5602, 2915, 1, 2, 2916, 637, 147, 3]] [[3, 3, 8, 10, 6, 7, 8, 21, 13, 4, 1, 2, 4, 7, 1, 3, 10, 9], [3, 3, 17, 1, 2, 3, 3, 8, 4, 3, 19, 1, 9]] ์•ž์„œ ๋ณธ ๊ทธ๋ž˜ํ”„์— ๋”ฐ๋ฅด๋ฉด, ๋Œ€๋ถ€๋ถ„์˜ ์ƒ˜ํ”Œ์€ ๊ธธ์ด๊ฐ€ 150 ์ด๋‚ด์ž…๋‹ˆ๋‹ค. X์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ X_train์˜ ์ƒ˜ํ”Œ๋“ค๊ณผ y์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ y_train ์ƒ˜ํ”Œ๋“ค์˜ ๋ชจ๋“  ๊ธธ์ด๋ฅผ ์ž„์˜๋กœ 150 ์ •๋„๋กœ ๋งž์ถ”์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค์˜ pad_sequences()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. max_len = 150 X_train = pad_sequences(X_train, padding='post', maxlen=max_len) y_train = pad_sequences(y_train, padding='post', maxlen=max_len) ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๊ฐ€ 150์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 8:2์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=.2, random_state=777) ๊ฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํฌ๊ธฐ(shape)๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_train.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_train.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_test.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_test.shape)) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (3131, 150) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (3131, 150) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (783, 150) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (783, 150) 2. ์–‘๋ฐฉํ–ฅ LSTM(Bi-directional LSTM)์œผ๋กœ POS Tagger ๋งŒ๋“ค๊ธฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ LSTM์˜ ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›์€ 128๋กœ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋Œ€๋‹ค ๋ฌธ์ œ์ด๋ฏ€๋กœ LSTM์˜ return_sequences์˜ ๊ฐ’์€ True๋กœ ์ง€์ •ํ•˜์˜€์œผ๋ฉฐ, ์–‘๋ฐฉํ–ฅ ์‚ฌ์šฉ์„ ์œ„ํ•ด LSTM์„ Bidirectional()๋กœ ๊ฐ์‹ธ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. validation_data๋กœ๋Š” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์žฌํ•˜์—ฌ ํ•™์Šต ์ค‘๊ฐ„์— ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„๋ฅผ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•˜๊ณ  ์†์‹ค ํ•จ์ˆ˜๋ฅผ categorical_crossentropy๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๋งŒ์•ฝ ๋ ˆ์ด๋ธ”์— ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•˜์ง€ ์•Š๊ณ  ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ์†์‹ค ํ•จ์ˆ˜๋ฅผ categorical_crossentropy ๋Œ€์‹  sparse_categorical_crossentropy๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ›„์ž์˜ ๋ฐฉ๋ฒ•์„ ํƒํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, LSTM, InputLayer, Bidirectional, TimeDistributed, Embedding from tensorflow.keras.optimizers import Adam embedding_dim = 128 hidden_units = 128 model = Sequential() model.add(Embedding(vocab_size, embedding_dim, mask_zero=True)) model.add(Bidirectional(LSTM(hidden_units, return_sequences=True))) model.add(TimeDistributed(Dense(tag_size, activation=('softmax')))) model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(0.001), metrics=['accuracy']) model.fit(X_train, y_train, batch_size=128, epochs=7, validation_data=(X_test, y_test)) ์ด 7๋ฒˆ์˜ ์—ํฌํฌ๋ฅผ ๋งˆ์น˜๊ณ  ๋‚˜์„œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค. print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (model.evaluate(X_test, y_test)[1])) 25/25 [==============================] - 0s 6ms/step - loss: 0.0720 - accuracy: 0.9016 ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.9016 ์‹ค์ œ๋กœ ๋งž์ถ”๊ณ  ์žˆ๋Š”์ง€๋ฅผ ํŠน์ • ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ(10๋ฒˆ ์ธ๋ฑ์Šค)์„ ํ†ตํ•ด ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด์™€ ํ’ˆ์‚ฌ ํƒœ๊น… ์ •๋ณด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_word์™€ index_to_tag๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. index_to_word = src_tokenizer.index_word index_to_tag = tar_tokenizer.index_word i = 10 # ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค. y_predicted = model.predict(np.array([X_test[i]])) # ์ž…๋ ฅํ•œ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก๊ฐ’ y๋ฅผ ๋ฆฌํ„ด y_predicted = np.argmax(y_predicted, axis=-1) # ํ™•๋ฅ  ๋ฒกํ„ฐ๋ฅผ ์ •์ˆ˜ ๋ ˆ์ด๋ธ”๋กœ ๋ณ€ํ™˜. print("{:15}|{:5}|{}".format("๋‹จ์–ด", "์‹ค์ œ ๊ฐ’", "์˜ˆ์ธก๊ฐ’")) print(35 * "-") for word, tag, pred in zip(X_test[i], y_test[i], y_predicted[0]): if word != 0: # PAD ๊ฐ’์€ ์ œ์™ธํ•จ. print("{:17}: {:7} {}".format(index_to_word[word], index_to_tag[tag].upper(), index_to_tag[pred].upper())) ๋‹จ์–ด |์‹ค์ œ ๊ฐ’ |์˜ˆ์ธก๊ฐ’ ----------------------------------- in : IN IN addition : NN NN , : , , buick : NNP NNP is : VBZ VBZ a : DT DT relatively : RB RB respected : VBN VBN nameplate : NN NN among : IN IN american : NNP NNP express : NNP NNP card : NN NN holders : NNS NNS , : , , says : VBZ VBZ 0 : -NONE- -NONE- *t*-1 : -NONE- -NONE- an : DT DT american : NNP NNP express : NNP NNP spokeswoman : NN NN . : . . 12-03 ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition) ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ ๊ฐœ์ฒด(entity)์˜ ์œ ํ˜•์„ ์ธ์‹ํ•˜๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition)์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ์‚ฌ๋žŒ, ์žฅ์†Œ, ์กฐ์ง ๋“ฑ์„ ์˜๋ฏธํ•˜๋Š” ๋‹จ์–ด์ธ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition)์ด๋ž€? ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition)์ด๋ž€ ๋ง ๊ทธ๋Œ€๋กœ ์ด๋ฆ„์„ ๊ฐ€์ง„ ๊ฐœ์ฒด(named entity)๋ฅผ ์ธ์‹ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ข€ ๋” ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด, ์–ด๋–ค ์ด๋ฆ„์„ ์˜๋ฏธํ•˜๋Š” ๋‹จ์–ด๋ฅผ ๋ณด๊ณ ๋Š” ๊ทธ ๋‹จ์–ด๊ฐ€ ์–ด๋–ค ์œ ํ˜•์ธ์ง€๋ฅผ ์ธ์‹ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ ์ •์ด๋Š” 2018๋…„์— ๊ณจ๋“œ๋งŒ์‚ญ์Šค์— ์ž…์‚ฌํ–ˆ๋‹ค.๋ผ๋Š” ๋ฌธ์žฅ์ด ์žˆ์„ ๋•Œ, ์‚ฌ๋žŒ(person), ์กฐ์ง(organization), ์‹œ๊ฐ„(time)์— ๋Œ€ํ•ด ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ด๋ผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ ์ • - ์‚ฌ๋žŒ 2018๋…„ - ์‹œ๊ฐ„ ๊ณจ๋“œ๋งŒ์‚ญ์Šค - ์กฐ์ง 2. NLTK๋ฅผ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition using NTLK) NLTK์—์„œ๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ(NER chunker)๋ฅผ ์ง€์›ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ๋ณ„๋„ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•  ํ•„์š” ์—†์ด NLTK๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์•„๋ž˜์˜ ์‹ค์Šต์—์„œ nltk.download('maxent_ne_chunker'), nltk.download('words') ๋“ฑ์˜ ์„ค์น˜๋ฅผ ์š”๊ตฌํ•˜๋Š” ์—๋Ÿฌ ๋ฌธ๊ตฌ๊ฐ€ ๋œฌ๋‹ค๋ฉด, ์ง€์‹œํ•˜๋Š” ๋Œ€๋กœ ์„ค์น˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. from nltk import word_tokenize, pos_tag, ne_chunk sentence = "James is working at Disney in London" # ํ† ํฐํ™” ํ›„ ํ’ˆ์‚ฌ ํƒœ๊น… tokenized_sentence = pos_tag(word_tokenize(sentence)) print(tokenized_sentence) [('James', 'NNP'), ('is', 'VBZ'), ('working', 'VBG'), ('at', 'IN'), ('Disney', 'NNP'), ('in', 'IN'), ('London', 'NNP')] # ๊ฐœ์ฒด๋ช… ์ธ์‹ ner_sentence = ne_chunk(tokenized_sentence) print(ner_sentence) (S (PERSON James/NNP) is/VBZ working/VBG at/IN (ORGANIZATION Disney/NNP) in/IN (GPE London/NNP)) ne_chunk๋Š” ๊ฐœ์ฒด๋ช…์„ ํƒœ๊น… ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ž์„œ ํ’ˆ์‚ฌ ํƒœ๊น…(pos_tag)์ด ์ˆ˜ํ–‰๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ฒฐ๊ณผ์—์„œ James๋Š” PERSON(์‚ฌ๋žŒ), Disney๋Š” ์กฐ์ง(ORGANIZATION), London์€ ์œ„์น˜(GPE)๋ผ๊ณ  ์ •์ƒ์ ์œผ๋กœ ๊ฐœ์ฒด๋ช… ์ธ์‹์ด ์ˆ˜ํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์–ด์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 12-04 ๊ฐœ์ฒด๋ช… ์ธ์‹์˜ BIO ํ‘œํ˜„ ์ดํ•ดํ•˜๊ธฐ ๊ฐœ์ฒด๋ช… ์ธ์‹์€ ์ฑ—๋ด‡ ๋“ฑ์—์„œ ํ•„์š”ํ•œ ์ฃผ์š” ์ „์ฒ˜๋ฆฌ ์ž‘์—…์ด๋ฉด์„œ ๊ทธ ์ž์ฒด๋กœ๋„ ๊นŒ๋‹ค๋กœ์šด ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋„๋ฉ”์ธ ๋˜๋Š” ๋ชฉ์ ์— ํŠนํ™” ๋˜๋„๋ก ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์ •ํ™•ํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” ๊ธฐ์กด์— ๊ณต๊ฐœ๋œ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ง์ ‘ ๋ชฉ์ ์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•˜์—ฌ ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•ด์„œ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ด…๋‹ˆ๋‹ค. 1. BIO ํ‘œํ˜„ ๊ฐœ์ฒด๋ช… ์ธ์‹์—์„œ ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๊ฐœ์ฒด๋ช…์„ ์ธ์‹ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ€์žฅ ๋ณดํŽธ์ ์ธ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ธ BIO ํƒœ๊น… ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. B๋Š” Begin์˜ ์•ฝ์ž๋กœ ๊ฐœ์ฒด๋ช…์ด ์‹œ์ž‘๋˜๋Š” ๋ถ€๋ถ„, I๋Š” Inside์˜ ์•ฝ์ž๋กœ ๊ฐœ์ฒด ๋ช…์˜ ๋‚ด๋ถ€ ๋ถ€๋ถ„์„ ์˜๋ฏธํ•˜๋ฉฐ, O๋Š” Outside์˜ ์•ฝ์ž๋กœ ๊ฐœ์ฒด๋ช…์ด ์•„๋‹Œ ๋ถ€๋ถ„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์˜ํ™”์— ๋Œ€ํ•œ ์ฝ”ํผ์Šค ์ค‘์—์„œ ์˜ํ™” ์ œ๋ชฉ์— ๋Œ€ํ•œ ๊ฐœ์ฒด๋ช…์„ ๋ฝ‘์•„๋‚ด๊ณ  ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ํ•ด B ๋ฆฌ I ํฌ I ํ„ฐ I ๋ณด O ๋Ÿฌ O ๊ฐ€ O ์ž O ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์˜ํ™” ์ œ๋ชฉ์— ๋Œ€ํ•ด์„œ๋งŒ ๊ฐœ์ฒด๋ช…์„ ์ธ์‹ํ•˜๋Š”๋ฐ, ์˜ํ™” ์ œ๋ชฉ์ด ์‹œ์ž‘๋˜๋Š” ๊ธ€์ž์ธ 'ํ•ด'์—์„œ๋Š” B๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ๊ณ , ๊ทธ๋ฆฌ๊ณ  ์˜ํ™” ์ œ๋ชฉ์ด ๋๋‚˜๋Š” ์ˆœ๊ฐ„๊นŒ์ง€ I๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜ํ™” ์ œ๋ชฉ์ด ์•„๋‹Œ ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋งŒ O๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ B์™€ I๋Š” ๊ฐœ์ฒด๋ช…์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๊ณ , O๋Š” ๊ฐœ์ฒด๋ช…์ด ์•„๋‹ˆ๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๊ฐœ์ฒด๋ช… ์ธ์‹์ด๋ผ๋Š” ๊ฒƒ์€ ๋ณดํ†ต ํ•œ ์ข…๋ฅ˜์˜ ๊ฐœ์ฒด์— ๋Œ€ํ•ด์„œ๋งŒ ์–ธ๊ธ‰ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๊ฐœ์ฒด๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์˜ํ™”์— ๋Œ€ํ•œ ๋Œ€ํ™”์—์„œ๋Š” ์˜ํ™” ์ œ๋ชฉ์— ๋Œ€ํ•œ ๊ฐœ์ฒด๋ช…๊ณผ ๊ทน์žฅ์— ๋Œ€ํ•œ ๊ฐœ์ฒด๋ช…์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ฐ ๊ฐœ์ฒด๊ฐ€ ์–ด๋–ค ์ข…๋ฅ˜์ธ์ง€๋„ ํ•จ๊ป˜ ํƒœ๊น…์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•ด B-movie ๋ฆฌ I-movie ํฌ I-movie ํ„ฐ I-movie ๋ณด O ๋Ÿฌ O ๋ฉ” B-theater ๊ฐ€ I-theater ๋ฐ• I-theater ์Šค I-theater ๊ฐ€ O ์ž O 2. ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ฐ์ดํ„ฐ ์ดํ•ดํ•˜๊ธฐ ์‹ค์Šต์„ ํ†ตํ•ด ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹์— ๋Œ€ํ•ด์„œ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. CONLL2003์€ ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์œ„ํ•œ ์ „ํ†ต์ ์ธ ์˜์–ด ๋ฐ์ดํ„ฐ ์…‹์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃฐ ๋ฐ์ดํ„ฐ์˜ ์•ž ๋ถ€๋ถ„์„ ์ผ๋ถ€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. EU NNP B-NP B-ORG rejects VBZ B-VP O German JJ B-NP B-MISC call NN I-NP O to TO B-VP O boycott VB I-VP O British JJ B-NP B-MISC lamb NN I-NP O . . O O Peter NNP B-NP B-PER Blackburn NNP I-NP I-PER ๋ฐ์ดํ„ฐ์˜<NAME>์€ [๋‹จ์–ด] [ํ’ˆ์‚ฌ ํƒœ๊น…] [์ฒญํฌ ํƒœ๊น…] [๊ฐœ์ฒด๋ช… ํƒœ๊น…]์˜<NAME>์œผ๋กœ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ํ’ˆ์‚ฌ ํƒœ๊น…์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ์•„๋ž˜ ๋งํฌ์—์„œ ์ž์„ธํ•˜๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด์„œ EU ์˜†์— ๋ถ™์–ด์žˆ๋Š” NNP๋Š” ๊ณ ์œ  ๋ช…์‚ฌ ๋‹จ์ˆ˜ํ˜•์„ ์˜๋ฏธํ•˜๋ฉฐ, rejects ์˜†์— ์žˆ๋Š” VBZ๋Š” 3์ธ์นญ ๋‹จ์ˆ˜ ๋™์‚ฌ ํ˜„์žฌํ˜•์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html ๊ฐœ์ฒด๋ช… ํƒœ๊น…์˜ ๊ฒฝ์šฐ์—๋Š” LOC๋Š” location, ORG๋Š” organization, PER์€ person, MISC๋Š” miscellaneous๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” BIO ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐœ์ฒด ๋ช…์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์ด๋ฉด์„œ Organization์„ ์˜๋ฏธํ•˜๋Š” EU์—๋Š” B-ORG๋ผ๋Š” ๊ฐœ์ฒด๋ช… ํƒœ๊น…์ด ๋ถ™์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, EU ๊ทธ ์ž์ฒด๋กœ ๊ฐœ์ฒด๋ช… ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฑฐ๊ธฐ์„œ ๊ฐœ์ฒด๋ช… ์ธ์‹์€ ์ข…๋ฃŒ๋˜๋ฉด์„œ ๋’ค์— I๊ฐ€ ๋ณ„๋„๋กœ ๋ถ™๋Š” ๋‹จ์–ด๊ฐ€ ๋‚˜์˜ค์ง€๋Š” ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ด์— EU ๋’ค์— ๋‚˜์˜ค๋Š” call์€ ๊ฐœ์ฒด๋ช…์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— O๊ฐ€ ํƒœ๊น…์ด ๋ฉ๋‹ˆ๋‹ค. ๋˜ ํ•˜๋‚˜ ๊ธฐ์–ตํ•ด ๋‘์–ด์•ผ ํ•  ๊ฒƒ์€ 9๋ฒˆ์งธ ์ค„์ธ. . O O ๋‹ค์Œ์— 11๋ฒˆ์งธ ์ค„ Peter๊ฐ€ ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„ ์‚ฌ์ด์—์„œ 10๋ฒˆ์งธ ์ค„์€ ๊ณต๋ž€์œผ๋กœ ๋˜์–ด ์žˆ๋Š”๋ฐ, ์ด๋Š” 9๋ฒˆ์งธ ์ค„์—์„œ ๋ฌธ์žฅ์ด ๋๋‚˜๊ณ  11๋ฒˆ์งธ ์ค„์—์„œ ์ƒˆ๋กœ์šด ๋ฌธ์žฅ์ด ์‹œ์ž‘๋จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ ๋ฌธ์žฅ์ด ์‹œ์ž‘๋˜๋Š” 11๋ฒˆ์งธ ์ค„์—์„œ๋Š” ๊ฐœ์ฒด๋ช…์ด ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ๋๋‚˜์ง€ ์•Š์•˜์„ ๋•Œ, ์–ด๋–ป๊ฒŒ ๋‹ค์Œ ๋‹จ์–ด๋กœ ๊ฐœ์ฒด๋ช… ์ธ์‹์ด ์ด์–ด์ง€๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. Peter๋Š” ๊ฐœ์ฒด๋ช…์ด ์‹œ์ž‘๋˜๋ฉด์„œ person์— ํ•ด๋‹น๋˜๊ธฐ ๋•Œ๋ฌธ์— B-PER์ด๋ผ๋Š” ๊ฐœ์ฒด๋ช… ํƒœ๊น…์ด ๋ถ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•„์ง ๊ฐœ์ฒด๋ช…์— ๋Œ€ํ•œ ์ธ์‹์€ ๋๋‚˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ๋ถ™๋Š” Blackburn์—์„œ๋Š” I๊ฐ€ ๋‚˜์˜ค๋ฉด์„œ I-PER์ด ๊ฐœ์ฒด๋ช… ํƒœ๊น…์œผ๋กœ ๋ถ™๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, Peter Blackburn์ด person์— ์†ํ•˜๋Š” ํ•˜๋‚˜์˜ ๊ฐœ์ฒด๋ช…์ž…๋‹ˆ๋‹ค. 3. ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ import re import numpy as np import matplotlib.pyplot as plt import urllib.request from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical from sklearn.model_selection import train_test_split ์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/12.%20RNN%20Sequence%20Labeling/dataset/train.txt", filename="train.txt") f = open('train.txt', 'r') tagged_sentences = [] sentence = [] for line in f: if len(line)==0 or line.startswith('-DOCSTART') or line[0]=="\n": if len(sentence) > 0: tagged_sentences.append(sentence) sentence = [] continue splits = line.split(' ') # ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ์†์„ฑ์„ ๊ตฌ๋ถ„ํ•œ๋‹ค. splits[-1] = re.sub(r'\n', '', splits[-1]) # ์ค„๋ฐ”๊ฟˆ ํ‘œ์‹œ \n์„ ์ œ๊ฑฐํ•œ๋‹ค. word = splits[0].lower() # ๋‹จ์–ด๋“ค์€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ์ €์žฅํ•œ๋‹ค. sentence.append([word, splits[-1]]) # ๋‹จ์–ด์™€ ๊ฐœ์ฒด๋ช… ํƒœ๊น…๋งŒ ๊ธฐ๋กํ•œ๋‹ค. ์ „์ฒด ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print("์ „์ฒด ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜: ", len(tagged_sentences)) ์ „์ฒด ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜ : 14041 ์ด ์ค‘ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :',tagged_sentences[0]) ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : [['eu', 'B-ORG'], ['rejects', 'O'], ['german', 'B-MISC'], ['call', 'O'], ['to', 'O'], ['boycott', 'O'], ['british', 'B-MISC'], ['lamb', 'O'], ['.', 'O']] ์œ„์™€ ๊ฐ™์€ ์ƒ˜ํ”Œ์ด ์ด 14,041๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ์„ ์‹œํ‚ค๋ ค๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋‹จ์–ด์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„๊ณผ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, [('eu', 'B-ORG'), ('rejects', 'O')]์™€ ๊ฐ™์€ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์ด ์žˆ๋‹ค๋ฉด eu์™€ rejects๋Š” ๊ฐ™์ด ์ €์žฅํ•˜๊ณ , B-ORG์™€ O๋ฅผ ๊ฐ™์ด ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํŒŒ์ด์ฌ ํ•จ์ˆ˜ ์ค‘์—์„œ zip() ํ•จ์ˆ˜๊ฐ€ ์œ ์šฉํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. zip() ํ•จ์ˆ˜๋Š” ๋™์ผํ•œ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์‹œํ€€์Šค ์ž๋ฃŒํ˜•์—์„œ ๊ฐ ์ˆœ์„œ์— ๋“ฑ์žฅํ•˜๋Š” ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. sentences, ner_tags = [], [] for tagged_sentence in tagged_sentences: # 14,041๊ฐœ์˜ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ 1๊ฐœ์”ฉ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. sentence, tag_info = zip(*tagged_sentence) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด๋“ค์€ sentence์— ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋“ค์€ tag_info์— ์ €์žฅ. sentences.append(list(sentence)) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด ์ •๋ณด๋งŒ ์ €์žฅํ•œ๋‹ค. ner_tags.append(list(tag_info)) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋งŒ ์ €์žฅํ•œ๋‹ค. ๊ฐ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด๋Š” sentences์— ํƒœ๊น… ์ •๋ณด๋Š” ner_tags์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ž„์˜๋กœ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ฌธ์žฅ :',sentences[0]) print('์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” :',ner_tags[0]) ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ฌธ์žฅ : ['eu', 'rejects', 'german', 'call', 'to', 'boycott', 'british', 'lamb', '.'] ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : ['B-ORG', 'O', 'B-MISC', 'O', 'O', 'O', 'B-MISC', 'O', 'O'] ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋งŒ sentences[0]์— ์ €์žฅ๋˜์—ˆ์œผ๋ฉฐ ๊ฐœ์ฒด๋ช…์— ๋Œ€ํ•ด์„œ๋งŒ ner_tags[0]์— ์ €์žฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ณด๊ฒ ์ง€๋งŒ, sentences๋Š” ์˜ˆ์ธก์„ ์œ„ํ•œ X์— ํ•ด๋‹น๋˜๋ฉฐ ner_tags๋Š” ์˜ˆ์ธก ๋Œ€์ƒ์ธ y์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ƒ˜ํ”Œ๋“ค๋„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ž„์˜๋กœ 12๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ๋„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(sentences[12]) print(ner_tags[12]) ['only', 'france', 'and', 'britain', 'backed', 'fischler', "'s", 'proposal', '.'] ['O', 'B-LOC', 'O', 'B-LOC', 'O', 'B-PER', 'O', 'O', 'O'] ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋งŒ sentences[12]์—, ๋˜ํ•œ ๊ฐœ์ฒด๋ช…์— ๋Œ€ํ•ด์„œ๋งŒ ner_tags[12]์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๊ณผ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค 14,041๊ฐœ์˜ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” ์ „๋ถ€ ์ œ๊ฐ๊ฐ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : %d' % max(len(sentence) for sentence in sentences)) print('์ƒ˜ํ”Œ์˜ ํ‰๊ท  ๊ธธ์ด : %f' % (sum(map(len, sentences))/len(sentences))) plt.hist([len(sentence) for sentence in sentences], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 113 ์ƒ˜ํ”Œ์˜ ํ‰๊ท  ๊ธธ์ด : 14.501887 ์œ„์˜ ๊ทธ๋ž˜ํ”„๋Š” ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๊ฐ€ ๋Œ€์ฒด์ ์œผ๋กœ 0~40์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋ฉฐ, ํŠนํžˆ 0~20์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์ƒ๋‹นํ•œ ๋น„์œจ์„ ์ฐจ์ง€ํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” 113์ž…๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ†ตํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋†’์€ ๋นˆ๋„์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ƒ์œ„ ์•ฝ 4,000๊ฐœ์˜ ๋‹จ์–ด๋งŒ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. vocab_size = 4000 src_tokenizer = Tokenizer(num_words=vocab_size, oov_token='OOV') src_tokenizer.fit_on_texts(sentences) tar_tokenizer = Tokenizer() tar_tokenizer.fit_on_texts(ner_tags) ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” src_tokenizer๋ฅผ, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด์— ๋Œ€ํ•ด์„œ๋Š” tar_tokenizer๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. tag_size = len(tar_tokenizer.word_index) + 1 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(vocab_size)) print('๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(tag_size)) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 4000 ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 10 ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. X_train = src_tokenizer.texts_to_sequences(sentences) y_train = tar_tokenizer.texts_to_sequences(ner_tags) ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋Š” X_train, ๊ฐœ์ฒด๋ช… ํƒœ๊น… ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋Š” y_train์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ์„ ์œ„ํ•ด ์ž„์˜๋กœ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ฌธ์žฅ :',X_train[0]) print('์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” :',y_train[0]) ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ฌธ์žฅ : [989, 1, 205, 629, 7, 3939, 216, 1, 3] ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : [4, 1, 7, 1, 1, 1, 7, 1, 1] ํ˜„์žฌ ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์ผ๋ถ€ ๋‹จ์–ด๊ฐ€ 'OOV'๋กœ ๋Œ€์ฒด๋œ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋””์ฝ”๋”ฉ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” index_to_word๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. index_to_word = src_tokenizer.index_word index_to_ner = tar_tokenizer.index_word ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์„ ๋‹ค์‹œ ๋””์ฝ”๋”ฉ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. decoded = [] for index in X_train[0] : # ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ์•ˆ์˜ ๊ฐ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜๋œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ decoded.append(index_to_word[index]) # ๋‹จ์–ด๋กœ ๋ณ€ํ™˜ print('๊ธฐ์กด ๋ฌธ์žฅ : {}'.format(sentences[0])) print('๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๊ฐ€ OOV ์ฒ˜๋ฆฌ๋œ ๋ฌธ์žฅ : {}'.format(decoded)) ๊ธฐ์กด ๋ฌธ์žฅ : ['eu', 'rejects', 'german', 'call', 'to', 'boycott', 'british', 'lamb', '.'] ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๊ฐ€ OOV ์ฒ˜๋ฆฌ๋œ ๋ฌธ์žฅ : ['eu', 'OOV', 'german', 'call', 'to', 'boycott', 'british', 'OOV', '.'] ์ผ๋ถ€ ๋‹จ์–ด๊ฐ€ 'OOV'๋กœ ๋Œ€์ฒด๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ณธ ๊ทธ๋ž˜ํ”„์— ๋”ฐ๋ฅด๋ฉด, ๋Œ€๋ถ€๋ถ„์˜ ์ƒ˜ํ”Œ์€ ๊ธธ์ด๊ฐ€ 70 ์ด๋‚ด์ž…๋‹ˆ๋‹ค. X์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ X_train์˜ ์ƒ˜ํ”Œ๋“ค๊ณผ y์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ y_train ์ƒ˜ํ”Œ๋“ค์˜ ๋ชจ๋“  ๊ธธ์ด๋ฅผ ์ž„์˜๋กœ 70 ์ •๋„๋กœ ๋งž์ถ”์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒจ๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. max_len = 70 X_train = pad_sequences(X_train, padding='post', maxlen=max_len) y_train = pad_sequences(y_train, padding='post', maxlen=max_len) ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๊ฐ€ 70์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 8:2์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=.2, random_state=777) ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” ํƒœ๊น… ์ •๋ณด์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. y_train = to_categorical(y_train, num_classes=tag_size) y_test = to_categorical(y_test, num_classes=tag_size) ๊ฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํฌ๊ธฐ(shape)๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_train.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_train.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_test.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_test.shape)) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (11232, 70) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (11232, 70, 10) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (2809, 70) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (2809, 70, 10) 4. ์–‘๋ฐฉํ–ฅ LSTM(Bi-directional LSTM)์œผ๋กœ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ ๋งŒ๋“ค๊ธฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 128์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€๋‹ค ๊ตฌ์กฐ์˜ LSTM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ LSTM์˜ return_sequences์˜ ์ธ์ž ๊ฐ’์€ True๋กœ ์ฃผ์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต๊ณผ ๊ฐ™์ด ๊ฐ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๊ฐ€ ๋‹ฌ๋ผ์„œ ํŒจ๋”ฉ์„ ํ•˜๋Š๋ผ ์ˆซ์ž 0์ด ๋งŽ์•„์งˆ ๊ฒฝ์šฐ์—๋Š” Embedding()์— mask_zero=True๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ˆซ์ž 0์€ ์—ฐ์‚ฐ์—์„œ ์ œ์™ธํ•œ๋‹ค๋Š” ์˜ต์…˜์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์— TimeDistributed()๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, TimeDistributed()๋Š” LSTM์„ ๋‹ค ๋Œ€๋‹ค ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉํ•˜์—ฌ LSTM์˜ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅ์ธต์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ๊ฐœ์ฒด๋ช… ๋ ˆ์ด๋ธ” ๊ฐœ์ˆ˜๋งŒํผ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 128์ด๋ฉฐ, 8 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. validation_data๋กœ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•˜์—ฌ ํ•™์Šตํ•˜๋Š” ๋™์•ˆ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Embedding, LSTM, Bidirectional, TimeDistributed from tensorflow.keras.optimizers import Adam embedding_dim = 128 hidden_units = 128 model = Sequential() model.add(Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=max_len, mask_zero=True)) model.add(Bidirectional(LSTM(hidden_units, return_sequences=True))) model.add(TimeDistributed(Dense(tag_size, activation='softmax'))) model.compile(loss='categorical_crossentropy', optimizer=Adam(0.001), metrics=['accuracy']) model.fit(X_train, y_train, batch_size=128, epochs=8, validation_data=(X_test, y_test)) ํ•™์Šต์ด ์ข…๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค. print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (model.evaluate(X_test, y_test)[1])) 2809/2809 [==============================] - 9s 3ms/step ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.9573 ์‹ค์ œ๋กœ ๋งž์ถ”๊ณ  ์žˆ๋Š”์ง€๋ฅผ ์ž„์˜์˜ ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ๋กœ๋ถ€ํ„ฐ(์ธ๋ฑ์Šค 10๋ฒˆ) ์ง์ ‘ ์‹ค์ œ ๊ฐ’๊ณผ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. index_to_word์™€ index_to_ner๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์„ ๋น„๊ต ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. i = 10 # ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค. # ์ž…๋ ฅํ•œ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก y๋ฅผ ๋ฆฌํ„ด y_predicted = model.predict(np.array([X_test[i]])) # ํ™•๋ฅ  ๋ฒกํ„ฐ๋ฅผ ์ •์ˆ˜ ๋ ˆ์ด๋ธ”๋กœ ๋ณ€๊ฒฝ. y_predicted = np.argmax(y_predicted, axis=-1) # ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝ. labels = np.argmax(y_test[i], -1) print("{:15}|{:5}|{}".format("๋‹จ์–ด", "์‹ค์ œ ๊ฐ’", "์˜ˆ์ธก๊ฐ’")) print(35 * "-") for word, tag, pred in zip(X_test[i], labels, y_predicted[0]): if word != 0: # PAD ๊ฐ’์€ ์ œ์™ธํ•จ. print("{:17}: {:7} {}".format(index_to_word[word], index_to_ner[tag].upper(), index_to_ner[pred].upper())) ๋‹จ์–ด |์‹ค์ œ ๊ฐ’ |์˜ˆ์ธก๊ฐ’ ----------------------------------- sarah : B-PER B-PER brady : I-PER I-PER , : O O whose : O O republican : B-MISC B-MISC husband : O O was : O O OOV : O O OOV : O O in : O O an : O O OOV : O O attempt : O O on : O O president : O O ronald : B-PER B-PER reagan : I-PER I-PER , : O O took : O O centre : O O stage : O O at : O O the : O O democratic : B-MISC B-MISC national : I-MISC I-MISC convention : I-MISC I-MISC on : O O monday : O O night : O O to : O O OOV : O O president : O O bill : B-PER B-PER clinton : I-PER I-PER 's : O O gun : O O control : O O efforts : O O . : O O ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธกํ•œ ๊ฐœ์ฒด ๋ช…๋„ ์ถœ๋ ฅํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ด๋ฒˆ์— ์‚ฌ์šฉํ•œ ์ •ํ™•๋„ ์ธก์ • ๋ฐฉ๋ฒ•์ด ๊ทธ๋‹ค์ง€ ์ ์ ˆํ•˜์ง€๋Š” ์•Š์•˜๋Š”๋ฐ, ๋Œ€๋ถ€๋ถ„์˜ ๋‹จ์–ด๊ฐ€ ๊ฐœ์ฒด๋ช…์ด ์•„๋‹ˆ๋ผ๋Š” 'O'๊ฐ€ ํƒœ๊น… ๋œ ์ƒํ™ฉ์—์„œ ์ •ํ™•๋„๊ฐ€ ์ˆ˜๋งŽ์€ 'O'๋กœ ์ธํ•ด ๊ฒฐ์ •๋˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ ์‹ค์Šต์—์„œ F1-score๋ฅผ ๋„์ž…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 12-05 BiLSTM์„ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition, NER) ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ๋ฅผ ๋งŒ๋“  ํ›„์— F1-score๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. 1. ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ์•ž์„œ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ์™€๋Š” ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ์•„๋ž˜์˜ ๋งํฌ์—์„œ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://www.kaggle.com/abhinavwalia95/entity-annotated-corpus import pandas as pd import numpy as np import matplotlib.pyplot as plt import urllib.request from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from sklearn.model_selection import train_test_split from tensorflow.keras.utils import to_categorical urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/12.%20Sequence%20Labeling/dataset/ner_dataset.csv", filename="ner_dataset.csv") data = pd.read_csv("ner_dataset.csv", encoding="latin1") data[:5] ๋ฐ์ดํ„ฐ์˜<NAME>์„ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์—ด 'Sentence :#'์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŒจํ„ด์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Sentence: 1์ด ๋“ฑ์žฅํ•˜๊ณ  Null ๊ฐ’์ด ์ด์–ด์ง€๋‹ค๊ฐ€ ๋‹ค์‹œ Sentence: 2๊ฐ€ ๋“ฑ์žฅํ•˜๊ณ  ๋‹ค์‹œ Null ๊ฐ’์ด ์ด์–ด์ง€๋‹ค๊ฐ€ Sentence: 3์ด ๋“ฑ์žฅํ•˜๊ณ  ๋‹ค์‹œ Null ๊ฐ’์ด ์ด์–ด์ง€๋‹ค๊ฐ€๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ด๋Š” ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์„ ์—ฌ๋Ÿฌ ํ–‰์œผ๋กœ ๋‚˜๋ˆ ๋†“์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ˆซ์ž ๊ฐ’์„ t๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ฒซ ๋ฒˆ์งธ Sentence: t๋ถ€ํ„ฐ Null ๊ฐ’์ด ๋‚˜์˜ค๋‹ค๊ฐ€ Sentence: t+1์ด ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€์˜ ๋ชจ๋“  ํ–‰์€ ๊ธฐ์กด์— ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์ด์—ˆ์Šต๋‹ˆ๋‹ค. t ๋ฒˆ์งธ ๋ฌธ์žฅ์„ ๋‹จ์–ด ํ† ํฐํ™” ํ›„ ๊ฐ ํ–‰์œผ๋กœ ๋‚˜๋ˆ ๋†“์€ ๋ฐ์ดํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋’ค์—์„œ Pandas์˜ fillna๋ฅผ ํ†ตํ•ด ํ•˜๋‚˜๋กœ ๋ฌถ๋Š” ์ž‘์—…์„ ํ•ด์ค๋‹ˆ๋‹ค. print('๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ํ–‰์˜ ๊ฐœ์ˆ˜ : {}'.format(len(data))) ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ํ–‰์˜ ๊ฐœ์ˆ˜ : 1048575 ํ˜„์žฌ data์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๋Š” 1,048,575๊ฐœ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋’ค์—์„œ ๋ฌธ์žฅ 1๊ฐœ๋ฅผ ๋‹ค์ˆ˜์˜ ํ–‰๋“ค๋กœ ๋‚˜๋ˆ„์–ด ๋†“์€ ๊ฒƒ์„ ๋‹ค์‹œ 1๊ฐœ์˜ ํ–‰์œผ๋กœ ๋ณ‘ํ•ฉํ•˜๋Š” ์ž‘์—…์„ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ข… ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋Š” ์ด๋ณด๋‹ค ์ค„์–ด๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ์ธก๊ฐ’ ์œ ๋ฌด๋ฅผ ์‚ดํŽด๋ด…์‹œ๋‹ค. print('๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์œ ๋ฌด : ' + str(data.isnull().values.any())) ๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์œ ๋ฌด : True Sentence #์—ด์— Null ๊ฐ’๋“ค์ด ์กด์žฌํ•˜๊ณ  ์žˆ์–ด isnull().values.any()๋ฅผ ์ˆ˜ํ–‰ํ•˜์˜€์„ ๋•Œ True๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. isnull().sum()์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๊ฐ ์—ด๋งˆ๋‹ค์˜ Null ๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. print('์–ด๋–ค ์—ด์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์ถœ๋ ฅ') print('==============================') data.isnull().sum() ์–ด๋–ค ์—ด์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์ถœ๋ ฅ ============================== Sentence # 1000616 Word 0 POS 0 Tag 0 dtype: int64 ๋‹ค๋ฅธ ์—ด์€ 0๊ฐœ์ธ๋ฐ ์˜ค์ง Sentences #์—ด์—์„œ๋งŒ 1,000,616๊ฐœ๊ฐ€ ๋‚˜์˜จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ  ์œ ์ผํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ์…€ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” nunique()๋ฅผ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. print('sentence # ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : {}'.format(data['Sentence #'].nunique())) print('Word ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : {}'.format(data.Word.nunique())) print('Tag ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : {}'.format(data.Tag.nunique())) sentence # ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : 47959 Word ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : 35178 Tag ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : 17 ์ด ๋ฐ์ดํ„ฐ์—๋Š” 47,959๊ฐœ์˜ ๋ฌธ์žฅ์ด ์žˆ์œผ๋ฉฐ ๋ฌธ์žฅ๋“ค์€ 35,178๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๊ฐ€์ง€๊ณ  17๊ฐœ ์ข…๋ฅ˜์˜ ๊ฐœ์ฒด๋ช… ํƒœ๊น…์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 17๊ฐœ์˜ ๊ฐœ์ฒด๋ช… ํƒœ๊น…์ด ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๋ช‡ ๊ฐœ๊ฐ€ ์žˆ๋Š”์ง€, ๊ฐœ์ฒด๋ช… ํƒœ๊น… ๊ฐœ์ˆ˜์˜ ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. print('Tag ์—ด์˜ ๊ฐ๊ฐ์˜ ๊ฐ’์˜ ๊ฐœ์ˆ˜ ์นด์šดํŠธ') print('================================') print(data.groupby('Tag').size().reset_index(name='count')) Tag ์—ด์˜ ๊ฐ๊ฐ์˜ ๊ฐ’์˜ ๊ฐœ์ˆ˜ ์นด์šดํŠธ ================================ Tag count 0 B-art 402 1 B-eve 308 2 B-geo 37644 3 B-gpe 15870 4 B-nat 201 5 B-org 20143 6 B-per 16990 7 B-tim 20333 8 I-art 297 9 I-eve 253 10 I-geo 7414 11 I-gpe 198 12 I-nat 51 13 I-org 16784 14 I-per 17251 15 I-tim 6528 16 O 887908 BIO ํ‘œํ˜„ ๋ฐฉ๋ฒ•์—์„œ ์•„๋ฌด๋Ÿฐ ํƒœ๊น…๋„ ์˜๋ฏธํ•˜์ง€ ์•Š๋Š” O๊ฐ€ ๊ฐ€์žฅ 887,908๊ฐœ๋กœ ๊ฐ€์žฅ ๋งŽ์€ ๊ฐœ์ˆ˜๋ฅผ ์ฐจ์ง€ํ•จ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ๊ฐ€๊ณตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  Null ๊ฐ’์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. Pandas์˜ fillna(method='ffill')๋Š” Null ๊ฐ’์„ ๊ฐ€์ง„ ํ–‰์˜ ๋ฐ”๋กœ ์•ž์˜ ํ–‰์˜ ๊ฐ’์œผ๋กœ Null ๊ฐ’์„ ์ฑ„์šฐ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. t ๋ฒˆ์งธ ๋ฌธ์žฅ์— ์†ํ•˜๋ฉด์„œ Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ๋“ค์€ ์ „๋ถ€ ์ฒซ ๋ฒˆ์งธ ์—ด์— Sentence: t์˜ ๊ฐ’์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋’ค์˜ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด์„œ ์ •์ƒ์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. data = data.fillna(method="ffill") print(data.tail()) Sentence # Word POS Tag 1048570 Sentence: 47959 they PRP O 1048571 Sentence: 47959 responded VBD O 1048572 Sentence: 47959 to TO O 1048573 Sentence: 47959 the DT O 1048574 Sentence: 47959 attack NN O ๋’ค์˜ 5๊ฐœ ์ƒ˜ํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ์—ด์ด Sentence: 47959๋กœ ์ฑ„์›Œ์กŒ์Šต๋‹ˆ๋‹ค. ์ด๋Š” 47,959๋ฒˆ์งธ ๋ฌธ์žฅ์ž„์„ ์˜๋ฏธํ•˜๋ฉฐ, Null ๊ฐ’์„ ๊ฐ€์ง„ ํ–‰๋“ค์˜ ๋ฐ”๋กœ ์•ž ํ–‰์˜ Sentence # ์—ด์˜ ๊ฐ’์ด Sentence: 47959์ด์—ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์œ ๋ฌด : ' + str(data.isnull().values.any())) ๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์œ ๋ฌด : False ์—†๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์†Œ๋ฌธ์žํ™”ํ•˜์—ฌ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์—ฌ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. data['Word'] = data['Word'].str.lower() print('Word ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : {}'.format(data.Word.nunique())) Word ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : 31817 ์ •์ƒ์ ์œผ๋กœ ์†Œ๋ฌธ์ž ํ™”๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ์•ž์˜ ์ƒ˜ํ”Œ 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(data[:5]) Sentence # Word POS Tag 0 Sentence: 1 thousands NNS O 1 Sentence: 1 of IN O 2 Sentence: 1 demonstrators NNS O 3 Sentence: 1 have VBP O 4 Sentence: 1 marched VBN O ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์— ๋“ฑ์žฅํ•œ ๋‹จ์–ด์™€ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋ผ๋ฆฌ ์Œ(pair)์œผ๋กœ ๋ฌถ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. func = lambda temp: [(w, t) for w, t in zip(temp["Word"].values.tolist(), temp["Tag"].values.tolist())] tagged_sentences=[t for t in data.groupby("Sentence #").apply(func)] print("์ „์ฒด ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜: {}".format(len(tagged_sentences))) ์ „์ฒด ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜: 47959 1,000,616๊ฐœ์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ ๋ฌธ์žฅ๋‹น ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ๋กœ ๋ฌถ์ด๋ฉด์„œ 47,959๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ •์ƒ์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜์—ˆ๋Š”์ง€ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(tagged_sentences[0]) # ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ์ถœ๋ ฅ [('thousands', 'O'), ('of', 'O'), ('demonstrators', 'O'), ('have', 'O'), ('marched', 'O'), ('through', 'O'), ('london', 'B-geo'), ('to', 'O'), ('protest', 'O'), ('the', 'O'), ('war', 'O'), ('in', 'O'), ('iraq', 'B-geo'), ('and', 'O'), ('demand', 'O'), ('the', 'O'), ('withdrawal', 'O'), ('of', 'O'), ('british', 'B-gpe'), ('troops', 'O'), ('from', 'O'), ('that', 'O'), ('country', 'O'), ('.', 'O')] ์ „์ฒ˜๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒ˜ํ”Œ์ด ์ด 47,959๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ›ˆ๋ จ์„ ์‹œํ‚ค๋ ค๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋‹จ์–ด์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„๊ณผ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, [('thousands', 'O'), ('of', 'O')]์™€ ๊ฐ™์€ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์ด ์žˆ๋‹ค๋ฉด thousands์™€ of๋Š” ๊ฐ™์ด ์ €์žฅํ•˜๊ณ , O์™€ O๋ฅผ ๊ฐ™์ด ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™์ผํ•œ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์‹œํ€€์Šค ์ž๋ฃŒํ˜•์—์„œ ๊ฐ ์ˆœ์„œ์— ๋“ฑ์žฅํ•˜๋Š” ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” zip()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์–ด์™€ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋ฅผ ๋ถ„๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. sentences, ner_tags = [], [] for tagged_sentence in tagged_sentences: # 47,959๊ฐœ์˜ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ 1๊ฐœ์”ฉ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด๋“ค์€ sentence์— ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋“ค์€ tag_info์— ์ €์žฅ. sentence, tag_info = zip(*tagged_sentence) sentences.append(list(sentence)) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด ์ •๋ณด๋งŒ ์ €์žฅํ•œ๋‹ค. ner_tags.append(list(tag_info)) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋งŒ ์ €์žฅํ•œ๋‹ค. ๊ฐ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด๋Š” sentences์— ํƒœ๊น… ์ •๋ณด๋Š” ner_tags์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ž„์˜๋กœ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(sentences[0]) print(ner_tags[0]) ['thousands', 'of', 'demonstrators', 'have', 'marched', 'through', 'london', 'to', 'protest', 'the', 'war', 'in', 'iraq', 'and', 'demand', 'the', 'withdrawal', 'of', 'british', 'troops', 'from', 'that', 'country', '.'] ['O', 'O', 'O', 'O', 'O', 'O', 'B-geo', 'O', 'O', 'O', 'O', 'O', 'B-geo', 'O', 'O', 'O', 'O', 'O', 'B-gpe', 'O', 'O', 'O', 'O', 'O'] ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋งŒ sentences[0]์—, ๋˜ํ•œ ๊ฐœ์ฒด๋ช…์— ๋Œ€ํ•ด์„œ๋งŒ ner_tags[0]์— ์ €์žฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ณด๊ฒ ์ง€๋งŒ, sentences๋Š” ์˜ˆ์ธก์„ ์œ„ํ•œ X์— ํ•ด๋‹น๋˜๋ฉฐ ner_tags๋Š” ์˜ˆ์ธก ๋Œ€์ƒ์ธ y์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ƒ˜ํ”Œ๋“ค์— ๋Œ€ํ•ด์„œ๋„ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ž„์˜๋กœ 98๋ฒˆ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ๋„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(sentences[98]) print(ner_tags[98]) ['she', 'had', 'once', 'received', 'a', 'kidney', 'transplant', '.'] ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'] ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋งŒ sentences[98]์—, ๋˜ํ•œ ๊ฐœ์ฒด๋ช…์— ๋Œ€ํ•ด์„œ๋งŒ ner_tags[98]์— ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๊ณผ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. 47,959๊ฐœ์˜ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” ์„œ๋กœ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : %d' % max(len(l) for l in sentences)) print('์ƒ˜ํ”Œ์˜ ํ‰๊ท  ๊ธธ์ด : %f' % (sum(map(len, sentences))/len(sentences))) plt.hist([len(s) for s in sentences], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 104 ์ƒ˜ํ”Œ์˜ ํ‰๊ท  ๊ธธ์ด : 21.863987989741236 ์œ„์˜ ๊ทธ๋ž˜ํ”„๋Š” ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๊ฐ€ ๋Œ€์ฒด์ ์œผ๋กœ 0~40์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” 104์ž…๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ†ตํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ์ธ๋ฑ์Šค 1์—๋Š” ๋‹จ์–ด 'OOV'๋ฅผ ํ• ๋‹น. src_tokenizer = Tokenizer(oov_token='OOV') # ํƒœ๊น… ์ •๋ณด๋“ค์€ ๋‚ด๋ถ€์ ์œผ๋กœ ๋Œ€๋ฌธ์ž๋ฅผ<NAME> ์ฑ„ ์ €์žฅ tar_tokenizer = Tokenizer(lower=False) src_tokenizer.fit_on_texts(sentences) tar_tokenizer.fit_on_texts(ner_tags) ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” src_tokenizer๋ฅผ, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด์— ๋Œ€ํ•ด์„œ๋Š” tar_tokenizer๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. vocab_size = len(src_tokenizer.word_index) + 1 tag_size = len(tar_tokenizer.word_index) + 1 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(vocab_size)) print('๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(tag_size)) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 31819 ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 18 ์•ž์„œ src_tokenizer๋ฅผ ๋งŒ๋“ค ๋•Œ Tokenizer์˜ ์ธ์ž๋กœ oov_token='OOV'๋ฅผ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค 1์— ๋‹จ์–ด 'OOV'๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. print('๋‹จ์–ด OOV์˜ ์ธ๋ฑ์Šค : {}'.format(src_tokenizer.word_index['OOV'])) ๋‹จ์–ด OOV์˜ ์ธ๋ฑ์Šค : 1 ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. X_data = src_tokenizer.texts_to_sequences(sentences) y_data = tar_tokenizer.texts_to_sequences(ner_tags) ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋Š” X_data, ๊ฐœ์ฒด๋ช… ํƒœ๊น… ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋Š” y_data์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ์„ ์œ„ํ•ด ์ž„์˜๋กœ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(X_data[0]) print(y_data[0]) [254, 6, 967, 16, 1795, 238, 468, 7, 523, 2, 129, 5, 61, 9, 571, 2, 833, 6, 186, 90, 22, 15, 56, 3] [1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 8, 1, 1, 1, 1, 1] ๋ชจ๋ธ ํ›ˆ๋ จ ํ›„ ๊ฒฐ๊ณผ ํ™•์ธ์„ ์œ„ํ•ด ์ธ๋ฑ์Šค๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_word์™€ ์ธ๋ฑ์Šค๋กœ๋ถ€ํ„ฐ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_ner๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค 0์€ 'PAD'๋ž€ ๋‹จ์–ด๋ฅผ ํ• ๋‹นํ•ด๋‘ก๋‹ˆ๋‹ค. index_to_ner์€ ๊ฐœ์ˆ˜๊ฐ€ ์ ์œผ๋‹ˆ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. word_to_index = src_tokenizer.word_index index_to_word = src_tokenizer.index_word ner_to_index = tar_tokenizer.word_index index_to_ner = tar_tokenizer.index_word index_to_ner[0] = 'PAD' print(index_to_ner) {1: 'O', 2: 'B-geo', 3: 'B-tim', 4: 'B-org', 5: 'I-per', 6: 'B-per', 7: 'I-org', 8: 'B-gpe', 9: 'I-geo', 10: 'I-tim', 11: 'B-art', 12: 'B-eve', 13: 'I-art', 14: 'I-eve', 15: 'B-nat', 16: 'I-gpe', 17: 'I-nat', 0: 'PAD'} index_to_word๋ฅผ ํ†ตํ•ด ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋””์ฝ”๋”ฉ ์ž‘์—…์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. decoded = [] for index in X_data[0] : # ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ์•ˆ์˜ ์ธ๋ฑ์Šค๋“ค์— ๋Œ€ํ•ด์„œ decoded.append(index_to_word[index]) # ๋‹ค์‹œ ๋‹จ์–ด๋กœ ๋ณ€ํ™˜ print('๊ธฐ์กด์˜ ๋ฌธ์žฅ : {}'.format(sentences[0])) print('๋””์ฝ”๋”ฉ ๋ฌธ์žฅ : {}'.format(decoded)) ๊ธฐ์กด์˜ ๋ฌธ์žฅ : ['thousands', 'of', 'demonstrators', 'have', 'marched', 'through', 'london', 'to', 'protest', 'the', 'war', 'in', 'iraq', 'and', 'demand', 'the', 'withdrawal', 'of', 'british', 'troops', 'from', 'that', 'country', '.'] ๋””์ฝ”๋”ฉ ๋ฌธ์žฅ : ['thousands', 'of', 'demonstrators', 'have', 'marched', 'through', 'london', 'to', 'protest', 'the', 'war', 'in', 'iraq', 'and', 'demand', 'the', 'withdrawal', 'of', 'british', 'troops', 'from', 'that', 'country', '.'] X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๊ฐ€ ๊ตฌ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํŒจ๋”ฉ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์•ž์„œ ํ™•์ธํ•˜์˜€๋“ฏ์ด ๋Œ€๋ถ€๋ถ„์˜ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋Š” 40~60์— ๋ถ„ํฌ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด์ธ 104๊ฐ€ ์•„๋‹ˆ๋ผ 70 ์ •๋„๋กœ max_len์„ ์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. max_len = 70 X_data = pad_sequences(X_data, padding='post', maxlen=max_len) y_data = pad_sequences(y_data, padding='post', maxlen=max_len) ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๊ฐ€ 70์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 8:2์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. X_train, X_test, y_train_int, y_test_int = train_test_split(X_data, y_data, test_size=.2, random_state=777) ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” ํƒœ๊น… ์ •๋ณด์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. y_train = to_categorical(y_train_int, num_classes=tag_size) y_test = to_categorical(y_test_int, num_classes=tag_size) ๊ฐ ๋ฐ์ดํ„ฐ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_train.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”(์ •์ˆ˜ ์ธ์ฝ”๋”ฉ)์˜ ํฌ๊ธฐ : {}'.format(y_train_int.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”(์›-ํ•ซ ์ธ์ฝ”๋”ฉ)์˜ ํฌ๊ธฐ : {}'.format(y_train.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_test.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”(์ •์ˆ˜ ์ธ์ฝ”๋”ฉ)์˜ ํฌ๊ธฐ : {}'.format(y_test_int.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”(์›-ํ•ซ ์ธ์ฝ”๋”ฉ)์˜ ํฌ๊ธฐ : {}'.format(y_test.shape)) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (38367, 70) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”(์ •์ˆ˜ ์ธ์ฝ”๋”ฉ)์˜ ํฌ๊ธฐ : (38367, 70) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”(์›-ํ•ซ ์ธ์ฝ”๋”ฉ)์˜ ํฌ๊ธฐ : (38367, 70, 18) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (9592, 70) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”(์ •์ˆ˜ ์ธ์ฝ”๋”ฉ)์˜ ํฌ๊ธฐ : (9592, 70) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”(์›-ํ•ซ ์ธ์ฝ”๋”ฉ)์˜ ํฌ๊ธฐ : (9592, 70, 18) 2. ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 256์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€๋‹ค ๊ตฌ์กฐ์˜ ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ LSTM์˜ return_sequences์˜ ์ธ์ž ๊ฐ’์€ True๋กœ ์ฃผ์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต๊ณผ ๊ฐ™์ด ๊ฐ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๊ฐ€ ๋‹ฌ๋ผ์„œ ํŒจ๋”ฉ์„ ํ•˜๋Š๋ผ ์ˆซ์ž 0์ด ๋งŽ์•„์งˆ ๊ฒฝ์šฐ์—๋Š” Embedding()์— mask_zero=True๋ฅผ ์„ค์ •ํ•˜์—ฌ ์ˆซ์ž 0์€ ์—ฐ์‚ฐ์—์„œ ์ œ์™ธํ•œ๋‹ค๋Š” ์˜ต์…˜์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์— TimeDistributed()๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, TimeDistributed()๋Š” LSTM์„ ๋‹ค ๋Œ€๋‹ค ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉํ•˜์—ฌ LSTM์˜ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅ์ธต์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ๊ฐœ์ฒด๋ช… ๋ ˆ์ด๋ธ” ๊ฐœ์ˆ˜๋งŒํผ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ์ถœ๋ ฅ์ธต์— ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์†์‹ค ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 128์ด๋ฉฐ, 6 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. validation_split=0.1์„ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 10%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ , ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ํ›ˆ๋ จ์ด ์ ์ ˆํžˆ ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, LSTM, InputLayer, Bidirectional, TimeDistributed, Embedding from tensorflow.keras.optimizers import Adam embedding_dim = 128 hidden_units = 256 model = Sequential() model.add(Embedding(vocab_size, embedding_dim, mask_zero=True)) model.add(Bidirectional(LSTM(hidden_units, return_sequences=True))) model.add(TimeDistributed(Dense(tag_size, activation=('softmax')))) model.compile(loss='categorical_crossentropy', optimizer=Adam(0.001), metrics=['accuracy']) history = model.fit(X_train, y_train, batch_size=128, epochs=6, validation_split=0.1) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์•ฝ 95%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ž„์˜์˜ ์ธ๋ฑ์Šค 13๋ฒˆ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์„ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. i = 13 # ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค. y_predicted = model.predict(np.array([X_test[i]])) # ์ž…๋ ฅํ•œ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก y๋ฅผ ๋ฆฌํ„ด y_predicted = np.argmax(y_predicted, axis=-1) # ํ™•๋ฅ  ๋ฒกํ„ฐ๋ฅผ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝํ•จ. labels = np.argmax(y_test[i], -1) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ๋‹ค์‹œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝํ•จ. print("{:15}|{:5}|{}".format("๋‹จ์–ด", "์‹ค์ œ ๊ฐ’", "์˜ˆ์ธก๊ฐ’")) print(35 * "-") for word, tag, pred in zip(X_test[i], labels, y_predicted[0]): if word != 0: # PAD ๊ฐ’์€ ์ œ์™ธํ•จ. print("{:17}: {:7} {}".format(index_to_word[word], index_to_ner[tag], index_to_ner[pred])) ๋‹จ์–ด |์‹ค์ œ ๊ฐ’ |์˜ˆ์ธก๊ฐ’ ----------------------------------- the : O O statement : O O came : O O as : O O u.n. : B-org B-org secretary-general: I-org I-org kofi : B-per B-per annan : I-per I-per met : O O with : O O officials : O O in : O O amman : B-geo B-geo to : O O discuss : O O wednesday : B-tim B-tim 's : O O attacks : O O . : O O ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ–ˆ์Šต๋‹ˆ๋‹ค. F1-score๋ผ๋Š” ์„ฑ๋Šฅ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜๊ณ , ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. 3. F1-score ๊ฐœ์ฒด๋ช… ์ธ์‹์—์„œ๋Š” ๊ทธ ์–ด๋–ค ๊ฐœ์ฒด๋„ ์•„๋‹ˆ๋ผ๋Š” ์˜๋ฏธ์˜ 'O'๋ผ๋Š” ํƒœ๊น…์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฐ ์ •๋ณด๋Š” ๋ณดํ†ต ๋Œ€๋‹ค์ˆ˜์˜ ๋ ˆ์ด๋ธ”์„ ์ฐจ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— ์‚ฌ์šฉํ–ˆ๋˜ ์ •ํ™•๋„(accuracy)๋ฅผ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ชจ๋ธ์ด ๋‹จ 1๊ฐœ์˜ ๊ฐœ์ฒด๋„ ๋งž์ถ”์ง€ ๋ชปํ•˜๊ณ  ์ „๋ถ€ 'O'๋กœ ์˜ˆ์ƒํ–ˆ์„ ๊ฒฝ์šฐ๋ฅผ ๋ด…์‹œ๋‹ค. ์‹ค์ œ ๊ฐ’์€ ์œ„์—์„œ ์ถœ๋ ฅํ–ˆ๋˜ ๊ฐ’์„ ์‹ค์ œ ๊ฐ’์œผ๋กœ ์žฌ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” labels๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐœ์ฒด๋ฅผ ํ•˜๋‚˜๋„ ๋งž์ถ”์ง€ ๋ชปํ–ˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ „๋ถ€ 'O'๋กœ๋งŒ ์ฑ„์›Œ์ง„ ์˜ˆ์ธก๊ฐ’ predicted๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. labels = ['B-PER', 'I-PER', 'O', 'O', 'B-MISC', 'O','O','O','O','O','O','O','O','O','O','B-PER','I-PER','O','O','O','O','O','O','B-MISC','I-MISC','I-MISC','O','O','O','O','O','O','B-PER','I-PER','O','O','O','O','O'] predicted = ['O'] * len(labels) print('์˜ˆ์ธก๊ฐ’ :',predicted) ์˜ˆ์ธก๊ฐ’ : ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'] ์‹ค์ œ๋กœ๋Š” PER, MISC, PER, MISC, PER์ด๋ผ๋Š” ์ด 5๊ฐœ์˜ ๊ฐœ์ฒด๊ฐ€ ์กด์žฌํ•จ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์˜ˆ์ธก๊ฐ’์ธ predicted๋Š” ๋‹จ 1๊ฐœ์˜ ๊ฐœ์ฒด๋„ ๋งž์ถ”์ง€ ๋ชปํ•œ ์ƒํ™ฉ์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. hit = 0 # ์ •๋‹ต ๊ฐœ์ˆ˜ for tag, pred in zip(labels, predicted): if tag == pred: hit +=1 # ์ •๋‹ต์ธ ๊ฒฝ์šฐ์—๋งŒ +1 accuracy = hit/len(labels) # ์ •๋‹ต ๊ฐœ์ˆ˜๋ฅผ ์ด๊ฐœ์ˆ˜๋กœ ๋‚˜๋ˆˆ๋‹ค. print("์ •ํ™•๋„: {:.1%}".format(accuracy)) ์ •ํ™•๋„: 74.4% ์‹ค์ œ ๊ฐ’์—์„œ๋„ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ’์ด 'O'์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์–ด๋–ค ๊ฐœ์ฒด๋„ ์ฐพ์ง€ ๋ชปํ•˜์˜€์Œ์—๋„ 74%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ •ํ™•๋„๊ฐ€ ๋ปฅํŠ€๊ธฐ๋˜์–ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์˜คํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์—ฌ๊ธฐ์„œ๋Š” ์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ๋” ์ ์ ˆํ•œ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์„ ๋„์ž…ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€ seqeval๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install seqeval ์•ž์„œ ๋จธ์‹  ๋Ÿฌ๋‹ ํ›‘์–ด๋ณด๊ธฐ ์ฑ•ํ„ฐ์—์„œ ์ •๋ฐ€๋„(precision)๊ณผ ์žฌํ˜„์œจ(recall)์„ ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ ์ธก์ •์„ ์œ„ํ•ด ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ฌธ์ œ์— ๋งž๋„๋ก ํ•ด์„ํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์žฌํ˜„์œจ ์ •๋ฐ€๋„ ํŠน์ • ๊ฐœ์ฒด๋ผ๊ณ  ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ ์ค‘์—์„œ ์‹ค์ œ ํŠน์ • ๊ฐœ์ฒด๋กœ ํŒ๋ช…๋˜์–ด ์˜ˆ์ธก์ด ์ผ์น˜ํ•œ ๋น„์œจ ์ •๋ฐ€๋„ T T + P ํŠน์ • ๊ฐœ์ฒด๋ผ๊ณ  ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ ์ค‘์—์„œ ์‹ค์ œ ํŠน์ • ๊ฐœ์ฒด๋กœ ํŒ๋ช…๋˜์–ด ์˜ˆ์ธก์ด ์ผ์น˜ํ•œ ๋น„์œจ ์žฌํ˜„์œจ ์ „์ฒด ํŠน์ • ๊ฐœ์ฒด ์ค‘์—์„œ ์‹ค์ œ ํŠน์ • ๊ฐœ์ฒด๋ผ๊ณ  ์ •๋‹ต์„ ๋งžํžŒ ๋น„์œจ ์žฌํ˜„์œจ T T + N ์ „์ฒด ํŠน์ • ๊ฐœ์ฒด ์ค‘์—์„œ ์‹ค์ œ ํŠน์ • ๊ฐœ์ฒด๋ผ๊ณ  ์ •๋‹ต์„ ๋งžํžŒ ๋น„์œจ ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ๋กœ๋ถ€ํ„ฐ ์กฐํ™” ํ‰๊ท (harmonic mean)์„ ๊ตฌํ•œ ๊ฒƒ์„ f1-score๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฐ€๋„ ์žฌํ˜„์œจ ์ •๋ฐ€๋„ ์žฌํ˜„์œจ 1 s o e 2 ์ •๋ฐ€๋„ ร— ์žฌํ˜„์œจ ์ •๋ฐ€๋„ + ์žฌํ˜„์œจ predicted์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ •๋ฐ€๋„, ์žฌํ˜„์œจ, f1-score๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. from seqeval.metrics import classification_report print(classification_report([labels], [predicted])) precision recall f1-score support MISC 0.00 0.00 0.00 2 PER 0.00 0.00 0.00 3 micro avg 0.00 0.00 0.00 5 macro avg 0.00 0.00 0.00 5 weighted avg 0.00 0.00 0.00 5 ์ด๋Ÿฌํ•œ ์ธก์ • ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด PER๊ณผ MISC ๋‘ ํŠน์ • ๊ฐœ์ฒด ์ค‘์—์„œ ์‹ค์ œ predicted๊ฐ€ ๋งž์ถ˜ ๊ฒƒ์€ ๋‹จ 1๊ฐœ๋„ ์—†๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์–ด๋Š ์ •๋„๋Š” ์ •๋‹ต์„ ๋งžํ˜”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์˜ˆ์ธก๊ฐ’์ธ predicted๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์ •๋ฐ€๋„, ์žฌํ˜„์œจ, f1-score๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. labels = ['B-PER', 'I-PER', 'O', 'O', 'B-MISC', 'O','O','O','O','O','O','O','O','O','O','B-PER','I-PER','O','O','O','O','O','O','B-MISC','I-MISC','I-MISC','O','O','O','O','O','O','B-PER','I-PER','O','O','O','O','O'] predicted = ['B-PER', 'I-PER', 'O', 'O', 'B-MISC', 'O','O','O','O','O','O','O','O','O','O','B-PER','I-PER','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O'] print(classification_report([labels], [predicted])) precision recall f1-score support MISC 1.00 0.50 0.67 2 PER 1.00 0.67 0.80 3 micro avg 1.00 0.60 0.75 5 macro avg 1.00 0.58 0.73 5 weighted avg 1.00 0.60 0.75 5 ํŠน์ • ๊ฐœ์ฒด๋กœ ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ๋Š” ๋ชจ๋‘ ์ œ๋Œ€๋กœ ์˜ˆ์ธก์„ ํ•˜์˜€์œผ๋ฏ€๋กœ ์ •๋ฐ€๋„๋Š” 1์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์žฌํ˜„์œจ์—์„œ๋Š” MISC๋Š” ์‹ค์ œ๋กœ๋Š” 4๊ฐœ์ž„์—๋„ 2๊ฐœ๋งŒ์„ ๋งž์ถ”์—ˆ์œผ๋ฏ€๋กœ 0.5, PER์€ ์‹ค์ œ๋กœ๋Š” 3๊ฐœ์ž„์—๋„ 2๊ฐœ๋งŒ์„ ๋งž์ถ”์—ˆ์œผ๋ฏ€๋กœ 0.67์ด ๋‚˜์˜จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. F1-score๋กœ ์„ฑ๋Šฅ ์ธก์ •ํ•˜๊ธฐ F1-score๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐœ์ฒด๋ช… ํƒœ๊น…์˜ ํ™•๋ฅ  ๋ฒกํ„ฐ ๋˜๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ๋ถ€ํ„ฐ ํƒœ๊น… ์ •๋ณด ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ธ sequences_to_tag๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’์ธ y_predicted์™€ ์‹ค์ œ ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” y_test๋ฅผ ํƒœ๊น… ์ •๋ณด ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๊ฐœ๋ฅผ ๋น„๊ตํ•˜์—ฌ f1-score๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. from seqeval.metrics import f1_score, classification_report def sequences_to_tag(sequences): result = [] # ์ „์ฒด ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ์‹œํ€€์Šค๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ธ๋‹ค. for sequence in sequences: word_sequence = [] # ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ํ™•๋ฅ  ๋ฒกํ„ฐ ๋˜๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ธ๋‹ค. for pred in sequence: # ์ •์ˆ˜๋กœ ๋ณ€ํ™˜. ์˜ˆ๋ฅผ ๋“ค์–ด pred๊ฐ€ [0, 0, 1, 0 ,0] ๋ผ๋ฉด 1์˜ ์ธ๋ฑ์Šค์ธ 2๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. pred_index = np.argmax(pred) # index_to_ner์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •์ˆ˜๋ฅผ ํƒœ๊น… ์ •๋ณด๋กœ ๋ณ€ํ™˜. 'PAD'๋Š” 'O'๋กœ ๋ณ€๊ฒฝ. word_sequence.append(index_to_ner[pred_index].replace("PAD", "O")) result.append(word_sequence) return result y_predicted = model.predict([X_test]) pred_tags = sequences_to_tag(y_predicted) test_tags = sequences_to_tag(y_test) print("F1-score: {:.1%}".format(f1_score(test_tags, pred_tags))) print(classification_report(test_tags, pred_tags)) F1-score: 78.5% precision recall f1-score support art 0.11 0.02 0.03 63 eve 0.28 0.29 0.29 52 geo 0.84 0.84 0.84 7620 gpe 0.96 0.94 0.95 3145 nat 0.46 0.30 0.36 37 org 0.57 0.58 0.57 4033 per 0.73 0.70 0.71 3545 tim 0.84 0.85 0.84 4067 micro avg 0.79 0.78 0.78 22562 macro avg 0.60 0.56 0.57 22562 weighted avg 0.79 0.78 0.78 22562 ์ด์–ด์„œ CRF ์ธต์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์„ฑ๋Šฅ์„ ๋†’์—ฌ๋ด…์‹œ๋‹ค. 12-06 BiLSTM-CRF๋ฅผ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹ ์ด๋ฒˆ ์‹ค์Šต์€ ์•„๋ž˜์˜ ์‹ค์Šต์€ ์ด๋ฏธ ์‹คํ–‰ํ•œ ์ƒํƒœ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์‹ค์Šต ๋งํฌ : https://wikidocs.net/147219 ์ด๋ฒˆ์—๋Š” ๊ธฐ์กด์˜ ์–‘๋ฐฉํ–ฅ LSTM ๋ชจ๋ธ์— CRF(Conditional Random Field)๋ผ๋Š” ์ƒˆ๋กœ์šด ์ธต์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ณด๋‹ค ๋ชจ๋ธ์„ ๊ฐœ์„ ํ•œ ์–‘๋ฐฉํ–ฅ LSTM + CRF ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition)์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ ๋งํฌ : https://arxiv.org/pdf/1508.01991v1.pdf ๋…ผ๋ฌธ ๋งํฌ : https://arxiv.org/pdf/1603.01360.pdf 1. CRF(Conditional Random Field) CRF๋Š” Conditional Random Field์˜ ์•ฝ์ž๋กœ ์–‘๋ฐฉํ–ฅ LSTM์„ ์œ„ํ•ด ํƒ„์ƒํ•œ ๋ชจ๋ธ์ด ์•„๋‹ˆ๋ผ ์ด์ „์— ๋…์ž์ ์œผ๋กœ ์กด์žฌํ•ด์™”๋˜ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์–‘๋ฐฉํ–ฅ LSTM ๋ชจ๋ธ ์œ„์— ํ•˜๋‚˜์˜ ์ธต์œผ๋กœ ์ถ”๊ฐ€ํ•˜์—ฌ, ์–‘๋ฐฉํ–ฅ LSTM + CRF ๋ชจ๋ธ์ด ํƒ„์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” CRF์˜ ์ˆ˜์‹์  ์ดํ•ด๊ฐ€ ์•„๋‹ˆ๋ผ ์–‘๋ฐฉํ–ฅ LSTM + CRF ๋ชจ๋ธ์˜ ์ง๊ด€์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. CRF ์ธต์˜ ์—ญํ• ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ„๋‹จํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹ ์ž‘์—…์˜ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ๋žŒ(Person), ์กฐ์ง(Organization) ๋‘ ๊ฐ€์ง€๋งŒ์„ ํƒœ๊น… ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํƒœ๊น… ์ž‘์—…์— BIO ํ‘œํ˜„์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•˜๋Š” ํƒœ๊น…์˜ ์ข…๋ฅ˜๋Š” ์•„๋ž˜์˜ 5๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. B-Per, I-Per, B-Org, I-Org, O ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์œ„์˜ ํƒœ๊น…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ธฐ์กด์˜ ์–‘๋ฐฉํ–ฅ LSTM ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ชจ๋ธ์˜ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ๋ชจ๋ธ์€ ๊ฐ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ๋กœ ์ž…๋ ฅ๋ฐ›๊ณ , ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์ธต์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐœ์ฒด๋ช…์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ž…๋ ฅ ๋‹จ์–ด๋“ค๊ณผ ์‹ค์ œ ๊ฐœ์ฒด๋ช…์ด ๋ฌด์—‡์ธ์ง€ ๋ชจ๋ฅด๋Š” ์ƒํ™ฉ์ด๋ฏ€๋กœ ์ด ๋ชจ๋ธ์ด ์ •ํ™•ํ•˜๊ฒŒ ๊ฐœ์ฒด๋ช…์„ ์˜ˆ์ธกํ–ˆ๋Š”์ง€๋Š” ์œ„ ๊ทธ๋ฆผ๋งŒ์œผ๋กœ๋Š” ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ๋ชจ๋ธ์€ ๋ช…ํ™•ํžˆ ํ‹€๋ฆฐ ์˜ˆ์ธก์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋‹จ์–ด๋“ค๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด ์ด ์‚ฌ์‹ค์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BIO ํ‘œํ˜„์— ๋”ฐ๋ฅด๋ฉด ์šฐ์„ , ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ๋ ˆ์ด๋ธ”์—์„œ I๊ฐ€ ๋“ฑ์žฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ I-Per์€ ๋ฐ˜๋“œ์‹œ B-Per ๋’ค์—์„œ๋งŒ ๋“ฑ์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, I-Org๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ B-Org ๋’ค์—์„œ๋งŒ ๋“ฑ์žฅํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์œ„ ๋ชจ๋ธ์€ ์ด๋Ÿฐ BIO ํ‘œํ˜„ ๋ฐฉ๋ฒ•์˜ ์ œ์•ฝ์‚ฌํ•ญ๋“ค์„ ๋ชจ๋‘ ์œ„๋ฐ˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์–‘๋ฐฉํ–ฅ LSTM ์œ„์— CRF ์ธต์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ ์„ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. CRF ์ธต์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ชจ๋ธ์€ ์˜ˆ์ธก ๊ฐœ์ฒด๋ช…, ๋‹ค์‹œ ๋งํ•ด ๋ ˆ์ด๋ธ” ์‚ฌ์ด์˜ ์˜์กด์„ฑ์„ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์–‘๋ฐฉํ–ฅ LSTM + CRF ๋ชจ๋ธ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์•ž์„œ๋ดค๋“ฏ์ด, ๊ธฐ์กด์— CRF ์ธต์ด ์กด์žฌํ•˜์ง€ ์•Š์•˜๋˜ ์–‘๋ฐฉํ–ฅ LSTM ๋ชจ๋ธ์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ์‹œ์ ์—์„œ ๊ฐœ์ฒด๋ช…์„ ๊ฒฐ์ •ํ–ˆ์ง€๋งŒ, CRF ์ธต์„ ์ถ”๊ฐ€ํ•œ ๋ชจ๋ธ์—์„œ๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋“ค์ด CRF ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด o d์— ๋Œ€ํ•œ ์–‘๋ฐฉํ–ฅ LSTM ์…€๊ณผ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ์ถœ๋ ฅ๊ฐ’ [0.7, 0.12, 0.08, 0.04, 0.06]์€ CRF ์ธต์˜ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ์ถœ๋ ฅ๊ฐ’์€ CRF ์ธต์˜ ์ž…๋ ฅ์ด ๋˜๊ณ , CRF ์ธต์€ ๋ ˆ์ด๋ธ” ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์‹œํ€€์Šค๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ตฌ์กฐ์—์„œ CRF ์ธต์€ ์ ์ฐจ์ ์œผ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์•„๋ž˜์™€ ๊ฐ™์€ ์ œ์•ฝ์‚ฌํ•ญ ๋“ฑ์„ ํ•™์Šตํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์žฅ์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด์—์„œ๋Š” I๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. O-I ํŒจํ„ด์€ ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. B-I-I ํŒจํ„ด์—์„œ ๊ฐœ์ฒด๋ช…์€ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด B-Per ๋‹ค์Œ์— I-Org๋Š” ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด ์–‘๋ฐฉํ–ฅ LSTM์€ ์ž…๋ ฅ ๋‹จ์–ด์— ๋Œ€ํ•œ ์–‘๋ฐฉํ–ฅ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•˜๋ฉฐ, CRF๋Š” ์ถœ๋ ฅ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ์–‘๋ฐฉํ–ฅ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค. 2. CRF ์ธต ์„ค์น˜ํ•˜๊ธฐ CRF ์ธต์„ ์†์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ keras-crf๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install keras-crf ๊นƒํ—ˆ๋ธŒ ๋งํฌ : https://github.com/luozhouyang/keras-crf 3. BiLSTM-CRF๋ฅผ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹ ์ด์ „๊ณผ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋ชจ๋ธ์„ ํ•™์Šตํ•ด ๋ด…์‹œ๋‹ค. ๋งˆ์ง€๋ง‰ ์ธต์— CRF ์ธต์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ํ•จ์ˆ˜ํ˜• API๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 64์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€๋‹ค ๊ตฌ์กฐ์˜ ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ LSTM์˜ return_sequences์˜ ์ธ์ž ๊ฐ’์€ True๋กœ ์ฃผ์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์— TimeDistributed()๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, TimeDistributed()๋Š” LSTM์„ ๋‹ค ๋Œ€๋‹ค ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉํ•˜์—ฌ LSTM์˜ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅ์ธต์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์€ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ๊ฐœ์ฒด๋ช… ๋ ˆ์ด๋ธ” ๊ฐœ์ˆ˜๋งŒํผ์˜ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ตœ์ข… ์ถœ๋ ฅ์ธต์ด CRF ์ธต์œผ๋กœ CRF ์ธต์— ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•˜๋Š” ์„ ํƒ์ง€ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” tag_size๋ฅผ ์ „๋‹ฌํ•ด ์ค๋‹ˆ๋‹ค. import tensorflow as tf from tensorflow.keras import Model from tensorflow.keras.layers import Dense, LSTM, Input, Bidirectional, TimeDistributed, Embedding, Dropout from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint from keras_crf import CRFModel from seqeval.metrics import f1_score, classification_report embedding_dim = 128 hidden_units = 64 dropout_ratio = 0.3 sequence_input = Input(shape=(max_len,),dtype=tf.int32, name='sequence_input') model_embedding = Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=max_len)(sequence_input) model_bilstm = Bidirectional(LSTM(units=hidden_units, return_sequences=True))(model_embedding) model_dropout = TimeDistributed(Dropout(dropout_ratio))(model_bilstm) model_dense = TimeDistributed(Dense(tag_size, activation='relu'))(model_dropout) base = Model(inputs=sequence_input, outputs=model_dense) model = CRFModel(base, tag_size) model.compile(optimizer=tf.keras.optimizers.Adam(0.001), metrics='accuracy') ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 128์ด๋ฉฐ, 15 ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. validation_split=0.1์„ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 10%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ , ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ํ›ˆ๋ จ์ด ์ ์ ˆํžˆ ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ๋˜๊ณ  ์žˆ์ง€๋Š” ์•Š์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์กฐ๊ธฐ ์ข…๋ฃŒ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฝœ๋ฐฑ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. keras-crf๊ฐ€ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ๋œ ๋ ˆ์ด๋ธ”์€ ์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ y_train์ด ์•„๋‹ˆ๋ผ y_train_int๋ฅผ ์‚ฌ์šฉํ•จ์„ ์ฃผ์˜ํ•ฉ๋‹ˆ๋‹ค. es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('bilstm_crf/cp.ckpt', monitor='val_decode_sequence_accuracy', mode='max', verbose=1, save_best_only=True, save_weights_only=True) history = model.fit(X_train, y_train_int, batch_size=128, epochs=15, validation_split=0.1, callbacks=[mc, es]) ์กฐ๊ธฐ ์ข…๋ฃŒ๋กœ ํ•™์Šต์ด ๋๋‚ฌ๋‹ค๋ฉด ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์•˜์„ ๋‹น์‹œ๋ฅผ ์ €์žฅํ•ด๋‘” ๊ฐ€์ค‘์น˜๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ํ›„, ์ž„์˜๋กœ ์„ ์ •ํ•œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ 13๋ฒˆ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธกํ•ด ๋ด…์‹œ๋‹ค. model.load_weights('bilstm_crf/cp.ckpt') i = 13 # ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค. y_predicted = model.predict(np.array([X_test[i]]))[0] # ์ž…๋ ฅํ•œ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก y๋ฅผ ๋ฆฌํ„ด labels = np.argmax(y_test[i], -1) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ๋‹ค์‹œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝ. print("{:15}|{:5}|{}".format("๋‹จ์–ด", "์‹ค์ œ ๊ฐ’", "์˜ˆ์ธก๊ฐ’")) print(35 * "-") for word, tag, pred in zip(X_test[i], labels, y_predicted[0]): if word != 0: # PAD ๊ฐ’์€ ์ œ์™ธํ•จ. print("{:17}: {:7} {}".format(index_to_word[word], index_to_ner[tag], index_to_ner[pred])) ๋‹จ์–ด |์‹ค์ œ ๊ฐ’ |์˜ˆ์ธก๊ฐ’ ----------------------------------- the : O O statement : O O came : O O as : O O u.n. : B-org B-org secretary-general: I-org I-org kofi : B-per B-per annan : I-per I-per met : O O with : O O officials : O O in : O O amman : B-geo B-geo to : O O discuss : O O wednesday : B-tim B-tim 's : O O attacks : O O . : O O ์ •ํ™•ํ•˜๊ฒŒ ์ž˜ ์˜ˆ์ธกํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก ์‹œํ€€์Šค์ธ y_predicted๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. y_predicted = model.predict(X_test)[0] ์ƒ์œ„ 2๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(y_predicted[:2]) [[ 1 3 10 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 1 1 1 1 1 1 3 1 1 1 1 1 1 1 2 9 9 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]] ์˜ˆ์ธก๊ฐ’์œผ๋กœ ํ™•๋ฅ  ๋ฒกํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ ์ •์ˆ˜ ์‹œํ€€์Šค๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ด์ „ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ํ•จ์ˆ˜์ธ sequences_to_tag๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ™•๋ฅ  ๋ฒกํ„ฐ๊ฐ€ ์•„๋‹Œ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„์„œ ํƒœ๊น… ์ •๋ณด ์‹œํ€€์Šค๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋กœ sequences_to_tag_for_crf๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ธก๊ฐ’๊ณผ ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” y_test๋ฅผ ํƒœ๊น… ์ •๋ณด ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ F1-score๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. def sequences_to_tag_for_crf(sequences): result = [] # ์ „์ฒด ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ์‹œํ€€์Šค๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ธ๋‹ค. for sequence in sequences: word_sequence = [] # ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ์˜ˆ์ธก ์ •์ˆ˜ ๋ ˆ์ด๋ธ”์„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ธ๋‹ค. for pred_index in sequence: # index_to_ner์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •์ˆ˜๋ฅผ ํƒœ๊น… ์ •๋ณด๋กœ ๋ณ€ํ™˜. 'PAD'๋Š” 'O'๋กœ ๋ณ€๊ฒฝ. word_sequence.append(index_to_ner[pred_index].replace("PAD", "O")) result.append(word_sequence) return result pred_tags = sequences_to_tag_for_crf(y_predicted) test_tags = sequences_to_tag(y_test) print("F1-score: {:.1%}".format(f1_score(test_tags, pred_tags))) print(classification_report(test_tags, pred_tags)) F1-score: 79.1% precision recall f1-score support art 0.00 0.00 0.00 63 eve 0.91 0.19 0.32 52 geo 0.82 0.85 0.83 7620 gpe 0.95 0.93 0.94 3145 nat 0.00 0.00 0.00 37 org 0.62 0.57 0.60 4033 per 0.76 0.70 0.73 3545 tim 0.87 0.83 0.85 4067 micro avg 0.80 0.78 0.79 22562 macro avg 0.62 0.51 0.53 22562 weighted avg 0.80 0.78 0.79 22562 12-07 ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ(Character Embedding) ํ™œ์šฉํ•˜๊ธฐ ์ด๋ฒˆ ์‹ค์Šต์€ ์•„๋ž˜์˜ ์‹ค์Šต์„ ์ด๋ฏธ ์‹คํ–‰ํ•œ ์ƒํƒœ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์‹ค์Šต ๋งํฌ : https://wikidocs.net/147234 ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ์˜ ์„ฑ๋Šฅ์„ ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๊ณผ ํ•จ๊ป˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์— ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ์„ฑ๋Šฅ์„ ๋†’์—ฌ๋ด…์‹œ๋‹ค. 1. ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ(Char Embedding)์„ ์œ„ํ•œ ์ „์ฒ˜๋ฆฌ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์œ„ํ•ด์„œ ํ•˜๊ณ ์ž ํ•˜๋Š” ์ „์ฒ˜๋ฆฌ๋Š” ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น ๋‹จ์–ด 'book'์ด ์žˆ๊ณ , b๊ฐ€ 21๋ฒˆ o๊ฐ€ 7๋ฒˆ, k๊ฐ€ 11๋ฒˆ์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ๋‹จ์–ด 'book'์„ [21 7 7 11]๋กœ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‹จ์–ด 1๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ ๋‹จ ์–ด๊ตฌ ๋‚ด์ง€๋Š” ๋ฌธ์žฅ์ด๋ผ๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? 'good book'์ด๋ž€ ๋ฌธ์žฅ์ด ์žˆ๊ณ , g๊ฐ€ 12๋ฒˆ, d๊ฐ€ 17๋ฒˆ์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ์ด ๋ฌธ์žฅ์„ ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 'good book์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ' [[12 7 7 17] [21 7 7 11]] ์ด ๊ฐ ๋ฌธ์ž์™€ ๋งคํ•‘๋œ ์ •์ˆ˜๋ฅผ ๊ฐ๊ฐ ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)์„ ๊ฑฐ์น˜๋„๋ก ํ•˜์—ฌ, ๋ฌธ์ž ๋‹จ์œ„ ์ž„๋ฒ ๋”ฉ์„ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ–ฅํ›„ ์ž„๋ฒ ๋”ฉ ์ธต์„ ํ†ต๊ณผ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์— ๋Œ€ํ•œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๋ฌธ์ž ๋ ˆ๋ฒจ๋กœ ๋ถ„ํ•ดํ•˜์—ฌ, ๋ฌธ์ž ์ง‘ํ•ฉ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. # char_vocab ๋งŒ๋“ค๊ธฐ words = list(set(data["Word"].values)) chars = set([w_i for w in words for w_i in w]) chars = sorted(list(chars)) print('๋ฌธ์ž ์ง‘ํ•ฉ :',chars) ๋ฌธ์ž ์ง‘ํ•ฉ : ['!', '"', '#', '$', '%', '&', "'", '(', ')', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '?', '@', '[', ']', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '~', '\x85', '\x91', '\x92', '\x93', '\x94', '\x96', '\x97', '\xa0', 'ยฐ', 'รฉ', 'รซ', 'รถ', 'รผ'] ์ด๋ ‡๊ฒŒ ์–ป์€ ๋ฌธ์ž ์ง‘ํ•ฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋”•์…”๋„ˆ๋ฆฌ์ธ char_to_index์™€ ๋ฐ˜๋Œ€๋กœ ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋”•์…”๋„ˆ๋ฆฌ์ธ index_to_char๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. char_to_index = {c: i + 2 for i, c in enumerate(chars)} char_to_index["OOV"] = 1 char_to_index["PAD"] = 0 index_to_char = {} for key, value in char_to_index.items(): index_to_char[value] = key ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฌธ์ž ์‹œํ€€์Šค์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 15๋กœ ์ œํ•œ ํ›„ ํŒจ๋”ฉ ํ•ฉ๋‹ˆ๋‹ค. max_len_char = 15 # ๋ฌธ์ž ์‹œํ€€์Šค์— ๋Œ€ํ•œ ํŒจ๋”ฉ ํ•˜๋Š” ํ•จ์ˆ˜ def padding_char_indice(char_indice, max_len_char): return pad_sequences( char_indice, maxlen=max_len_char, padding='post', value = 0) # ๊ฐ ๋‹จ์–ด๋ฅผ ๋ฌธ์ž ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ ํ›„ ํŒจ๋”ฉ ์ง„ํ–‰ def integer_coding(sentences): char_data = [] for ts in sentences: word_indice = [word_to_index[t] for t in ts] char_indice = [[char_to_index[char] for char in t] for t in ts] char_indice = padding_char_indice(char_indice, max_len_char) for chars_of_token in char_indice: if len(chars_of_token) > max_len_char: continue char_data.append(char_indice) return char_data # ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ X_char_data = integer_coding(sentences) ๋™์ผํ•œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์˜ ์ฐจ์ด๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ด์ „์˜ ๊ธฐ์กด ๋ฌธ์žฅ print('๊ธฐ์กด ๋ฌธ์žฅ :',sentences[0]) ๊ธฐ์กด ๋ฌธ์žฅ : ['thousands', 'of', 'demonstrators', 'have', 'marched', 'through', 'london', 'to', 'protest', 'the', 'war', 'in', 'iraq', 'and', 'demand', 'the', 'withdrawal', 'of', 'british', 'troops', 'from', 'that', 'country', '.'] ์œ„๋ฌธ์žฅ์„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋ฐ ํŒจ๋”ฉ ํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ๋‹จ์–ด ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ + ํŒจ๋”ฉ print('๋‹จ์–ด ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ :') print(X_data[0]) ๋‹จ์–ด ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [ 254 6 967 16 1795 238 468 7 523 2 129 5 61 9 571 2 833 6 186 90 22 15 56 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 254๋Š” ๊ธฐ์กด์˜ thousands, 6์€ ๊ธฐ์กด์˜ of์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ƒ˜ํ”Œ์„ ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ print('๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ :') print(X_char_data[0]) ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [[53 41 48 54 52 34 47 37 52 0 0 0 0 0 0] [48 39 0 0 0 0 0 0 0 0 0 0 0 0 0] [37 38 46 48 47 52 53 51 34 53 48 51 52 0 0] [41 34 55 38 0 0 0 0 0 0 0 0 0 0 0] [46 34 51 36 41 38 37 0 0 0 0 0 0 0 0] [53 41 51 48 54 40 41 0 0 0 0 0 0 0 0] [45 48 47 37 48 47 0 0 0 0 0 0 0 0 0] [53 48 0 0 0 0 0 0 0 0 0 0 0 0 0] [49 51 48 53 38 52 53 0 0 0 0 0 0 0 0] [53 41 38 0 0 0 0 0 0 0 0 0 0 0 0] [56 34 51 0 0 0 0 0 0 0 0 0 0 0 0] [42 47 0 0 0 0 0 0 0 0 0 0 0 0 0] [42 51 34 50 0 0 0 0 0 0 0 0 0 0 0] [34 47 37 0 0 0 0 0 0 0 0 0 0 0 0] [37 38 46 34 47 37 0 0 0 0 0 0 0 0 0] [53 41 38 0 0 0 0 0 0 0 0 0 0 0 0] [56 42 53 41 37 51 34 56 34 45 0 0 0 0 0] [48 39 0 0 0 0 0 0 0 0 0 0 0 0 0] [35 51 42 53 42 52 41 0 0 0 0 0 0 0 0] [53 51 48 48 49 52 0 0 0 0 0 0 0 0 0] [39 51 48 46 0 0 0 0 0 0 0 0 0 0 0] [53 41 34 53 0 0 0 0 0 0 0 0 0 0 0] [36 48 54 47 53 51 58 0 0 0 0 0 0 0 0] [14 0 0 0 0 0 0 0 0 0 0 0 0 0 0]] ์œ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ ๊ฐ ํ–‰์€ ๊ฐ ๋‹จ์–ด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, thousands๋Š” ์ฒซ ๋ฒˆ์งธ ํ–‰ [53 41 48 54 52 34 47 37 52 0 0 0 0 0 0]์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ 15(max_len_char)๋กœ ์ œํ•œํ•˜์˜€์œผ๋ฏ€๋กœ, ๊ธธ์ด๊ฐ€ 15๋ณด๋‹ค ์งง์€ ๋‹จ์–ด๋Š” ๋’ค์— 0์œผ๋กœ ํŒจ๋”ฉ ๋ฉ๋‹ˆ๋‹ค. 53์€ t, 41์€ h, 48์€ o, 54๋Š” u์— ๊ฐ๊ฐ ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. X_data๋Š” ๋’ค์— 0์œผ๋กœ ํŒจ๋”ฉ ๋˜์–ด ๊ธธ์ด๊ฐ€ 70์ธ ๊ฒƒ์— ๋น„ํ•ด X_char_data๋Š” ํ˜„์žฌ 0๋ฒˆ ๋‹จ์–ด๋Š” ๋ฌด์‹œ๋˜์–ด ๊ธธ์ด๊ฐ€ 70์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์œ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ ํ–‰์˜ ๊ฐœ์ˆ˜๊ฐ€ 70์ด ์•„๋‹Œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๊ธธ์ด 70์œผ๋กœ ๋งž์ถฐ์ฃผ๊ธฐ ์œ„ํ•ด์„œ ๋ฌธ์žฅ ๊ธธ์ด ๋ฐฉํ–ฅ์œผ๋กœ๋„ ํŒจ๋”ฉ์„ ํ•ด์ค๋‹ˆ๋‹ค. X_char_data = pad_sequences(X_char_data, maxlen=max_len, padding='post', value = 0) ๋‹จ์–ด ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋Š” ์ด๋ฏธ X_train, y_train, X_test, y_test๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ„๋ฆฌ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ X_char_train, X_char_test๋กœ ๋‚˜๋ˆ„์–ด์ค๋‹ˆ๋‹ค. X_char_train, X_char_test, _, _ = train_test_split(X_char_data, y_data, test_size=.2, random_state=777) X_char_train = np.array(X_char_train) X_char_test = np.array(X_char_test) ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(X_train[0]) [ 150 928 361 17 2624 9 4131 3567 9 8 2893 1250 880 107 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ ์ƒ˜ํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด์ธ 150๋ฒˆ์€ ์›๋ž˜ ์–ด๋–ค ๋‹จ์–ด์˜€์„๊นŒ์š”? print(index_to_word[150]) soldiers soldiers๋ผ๋Š” ๋‹จ์–ด์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด X_char_train์˜ ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ ์ƒ˜ํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ๋ฌธ์ž ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋กœ๋ถ€ํ„ฐ soldiers๋ผ๋Š” ๋‹จ์–ด์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(' '.join([index_to_char[index] for index in X_char_train[0][0]])) s o l d i e r s PAD PAD PAD PAD PAD PAD PAD ๊ฐ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_train.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_train.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ char ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : {}'.format(X_char_train.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_test.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_test.shape)) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (38367, 70) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (38367, 70, 18) ํ›ˆ๋ จ ์ƒ˜ํ”Œ char ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (38367, 70, 15) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (9592, 70) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (9592, 70, 18) 2. BiLSTM-CNN์„ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹ ์šฐ์„  ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์ด ํ™œ์šฉ๋˜๋Š” ๊ณผ์ •์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋‹จ์–ด๋Š” ๋ฌธ์ž ๋‹จ์œ„๋กœ ํ† ํฐํ™”๋˜์—ˆ๊ณ , ํ† ํฐํ™”๋œ ๊ฐ ๋ฌธ์ž๋Š” ์œ„์˜ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ์ •์ˆ˜๋กœ ๋งคํ•‘๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ •์ˆ˜๋กœ ๋งคํ•‘๋œ ๊ฐ ๋ฌธ์ž๋Š” ์ž„๋ฒ ๋”ฉ ์ธต์„ ํ†ต๊ณผํ•˜๋ฉด 64์ฐจ์›์˜ ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ดํ›„ 1D ํ•ฉ์„ฑ๊ณฑ ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š”๋ฐ, 1D ํ•ฉ์„ฑ๊ณฑ ์ธต์˜ ์ปค๋„์˜ ํฌ๊ธฐ๋Š” 3์ด๋ฉฐ ํ•ด๋‹น ์ปค๋„์€ ์ด 30๊ฐœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 1D ํ•ฉ์„ฑ๊ณฑ ์ธต์˜ ๊ฒฐ๊ณผ๋กœ ํ•˜๋‚˜์˜ ๋‹จ์–ด์— ๋Œ€ํ•œ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋‹จ์–ด ๋ฒกํ„ฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋˜ ๊ณผ์ •์„ ํ†ตํ•ด ์–ป์€ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์™€ ์—ฐ๊ฒฐ(concatenate) ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์–‘๋ฐฉํ–ฅ LSTM์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ดํ›„์—๋Š” ์ด์ „ ์‹ค์Šต๋“ค๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. LSTM์˜ ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 256์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ๋‹ค ๋Œ€๋‹ค ๊ตฌ์กฐ์˜ ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ LSTM์˜ return_sequences์˜ ์ธ์ž ๊ฐ’์€ True๋กœ ์ฃผ์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์— TimeDistributed()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ LSTM์˜ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅ์ธต์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. import tensorflow as tf from tensorflow.keras.layers import Embedding, Input, TimeDistributed, Dropout, concatenate, Bidirectional, LSTM, Conv1D, Dense, MaxPooling1D, Flatten from tensorflow.keras import Model from tensorflow.keras.initializers import RandomUniform from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint from tensorflow.keras.models import load_model from seqeval.metrics import f1_score, classification_report from keras_crf import CRFModel embedding_dim = 128 char_embedding_dim = 64 dropout_ratio = 0.5 hidden_units = 256 num_filters = 30 kernel_size = 3 # ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ word_ids = Input(shape=(None,),dtype='int32', name='words_input') word_embeddings = Embedding(input_dim=vocab_size, output_dim=embedding_dim)(word_ids) # char ์ž„๋ฒ ๋”ฉ char_ids = Input(shape=(None, max_len_char,), name='char_input') embed_char_out = TimeDistributed(Embedding(len(char_to_index), char_embedding_dim, embeddings_initializer=RandomUniform(minval=-0.5, maxval=0.5)), name='char_embedding')(char_ids) dropout = Dropout(dropout_ratio)(embed_char_out) # char ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•ด์„œ๋Š” Conv1D ์ˆ˜ํ–‰ conv1d_out = TimeDistributed(Conv1D(kernel_size=kernel_size, filters=num_filters, padding='same', activation='tanh', strides=1))(dropout) maxpool_out = TimeDistributed(MaxPooling1D(max_len_char))(conv1d_out) char_embeddings = TimeDistributed(Flatten())(maxpool_out) char_embeddings = Dropout(dropout_ratio)(char_embeddings) # char ์ž„๋ฒ ๋”ฉ์„ Conv1D ์ˆ˜ํ–‰ํ•œ ๋’ค์— ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ๊ณผ ์—ฐ๊ฒฐ output = concatenate([word_embeddings, char_embeddings]) # ์—ฐ๊ฒฐํ•œ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฌธ์žฅ์˜ ๊ธธ์ด๋งŒํผ LSTM์„ ์ˆ˜ํ–‰ output = Bidirectional(LSTM(hidden_units, return_sequences=True, dropout=dropout_ratio))(output) # ์ถœ๋ ฅ์ธต output = TimeDistributed(Dense(tag_size, activation='softmax'))(output) model = Model(inputs=[word_ids, char_ids], outputs=[output]) model.compile(loss='categorical_crossentropy', optimizer='nadam', metrics=['acc']) ์กฐ๊ธฐ ์ข…๋ฃŒ๋ฅผ ์กฐ๊ฑด์œผ๋กœ ์ฝœ๋ฐฑ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์‹ค์Šต๊ณผ ๋™์ผํ•œ ์กฐ๊ฑด์œผ๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 128๋กœ ํ•˜๊ณ , 15 ์—ํฌํฌ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 10%๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('bilstm_cnn.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True) history = model.fit([X_train, X_char_train], y_train, batch_size=128, epochs=15, validation_split=0.1, verbose=1, callbacks=[es, mc]) ์กฐ๊ธฐ ์ข…๋ฃŒ๋กœ ํ•™์Šต์ด ๋๋‚ฌ๋‹ค๋ฉด ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์•˜์„ ๋‹น์‹œ๋ฅผ ์ €์žฅํ•ด๋‘” ๊ฐ€์ค‘์น˜๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ 13๋ฒˆ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธกํ•ด ๋ด…์‹œ๋‹ค. model = load_model('bilstm_cnn.h5') i = 13 # ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค. # ์ž…๋ ฅํ•œ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก y๋ฅผ ๋ฆฌํ„ด y_predicted = model.predict([np.array([X_test[i]]), np.array([X_char_test[i]])]) y_predicted = np.argmax(y_predicted, axis=-1) # ํ™•๋ฅ  ๋ฒกํ„ฐ๋ฅผ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝ. labels = np.argmax(y_test[i], -1) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝ. print("{:15}|{:5}|{}".format("๋‹จ์–ด", "์‹ค์ œ ๊ฐ’", "์˜ˆ์ธก๊ฐ’")) print(35 * "-") for word, tag, pred in zip(X_test[i], labels, y_predicted[0]): if word != 0: # PAD ๊ฐ’์€ ์ œ์™ธํ•จ. print("{:17}: {:7} {}".format(index_to_word[word], index_to_ner[tag], index_to_ner[pred])) ๋‹จ์–ด |์‹ค์ œ ๊ฐ’ |์˜ˆ์ธก๊ฐ’ ----------------------------------- the : O O statement : O O came : O O as : O O u.n. : B-org B-org secretary-general: I-org I-org kofi : B-per B-per annan : I-per I-per met : O O with : O O officials : O O in : O O amman : B-geo B-geo to : O O discuss : O O wednesday : B-tim B-tim 's : O O attacks : O O . : O O ์ •ํ™•ํ•˜๊ฒŒ ์ž˜ ์˜ˆ์ธกํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก ์‹œํ€€์Šค์ธ y_predicted๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์— ๋Œ€ํ•œ ํƒœ๊น… ์ •๋ณด ์‹œํ€€์Šค๋ฅผ ์–ป์€ ํ›„ F1-score๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. y_predicted = model.predict([X_test, X_char_test]) pred_tags = sequences_to_tag(y_predicted) test_tags = sequences_to_tag(y_test) print("F1-score: {:.1%}".format(f1_score(test_tags, pred_tags))) print(classification_report(test_tags, pred_tags)) F1-score: 79.0% precision recall f1-score support art 0.00 0.00 0.00 63 eve 1.00 0.08 0.14 52 geo 0.81 0.86 0.84 7620 gpe 0.95 0.94 0.94 3145 nat 0.00 0.00 0.00 37 org 0.59 0.56 0.57 4033 per 0.73 0.72 0.73 3545 tim 0.87 0.84 0.85 4067 micro avg 0.79 0.79 0.79 22562 macro avg 0.62 0.50 0.51 22562 weighted avg 0.79 0.79 0.79 22562 3. BiLSTM-CNN-CRF ์ €์ž์˜ ๊ฒฝ์šฐ '์–‘๋ฐฉํ–ฅ LSTM์— CRF ์ธต์„ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ' ๋˜๋Š” '์–‘๋ฐฉํ–ฅ LSTM์— ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ' ์ด๋ ‡๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ชจ๋ธ์ด '์–‘๋ฐฉํ–ฅ LSTM๋งŒ์„ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ'๋ณด๋‹ค๋Š” ์„ฑ๋Šฅ์ด ๋” ์ข‹์€ ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋‘ ๊ฐ€์ง€ ๋ชจ๋‘๋ฅผ ํ™œ์šฉํ•ด ๋ณด๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”? ์ด๋ฒˆ์—๋Š” ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•œ ์œ„ ๋ชจ๋ธ์— CRF ์ธต๊นŒ์ง€ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. embedding_dim = 128 char_embedding_dim = 64 dropout_ratio = 0.5 hidden_units = 256 num_filters = 30 kernel_size = 3 # ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ word_ids = Input(shape=(None,),dtype='int32', name='words_input') word_embeddings = Embedding(input_dim=vocab_size, output_dim=embedding_dim)(word_ids) # char ์ž„๋ฒ ๋”ฉ char_ids = Input(shape=(None, max_len_char,), name='char_input') embed_char_out = TimeDistributed(Embedding(len(char_to_index), char_embedding_dim, embeddings_initializer=RandomUniform(minval=-0.5, maxval=0.5)), name='char_embedding')(char_ids) dropout = Dropout(dropout_ratio)(embed_char_out) # char ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•ด์„œ๋Š” Conv1D ์ˆ˜ํ–‰ conv1d_out = TimeDistributed(Conv1D(kernel_size=kernel_size, filters=num_filters, padding='same',activation='tanh', strides=1))(dropout) maxpool_out=TimeDistributed(MaxPooling1D(max_len_char))(conv1d_out) char_embeddings = TimeDistributed(Flatten())(maxpool_out) char_embeddings = Dropout(dropout_ratio)(char_embeddings) # char ์ž„๋ฒ ๋”ฉ์„ Conv1D ์ˆ˜ํ–‰ํ•œ ๋’ค์— ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ๊ณผ ์—ฐ๊ฒฐ output = concatenate([word_embeddings, char_embeddings]) # ์—ฐ๊ฒฐํ•œ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฌธ์žฅ์˜ ๊ธธ์ด๋งŒํผ LSTM์„ ์ˆ˜ํ–‰ output = Bidirectional(LSTM(hidden_units, return_sequences=True, dropout=dropout_ratio))(output) # ์ถœ๋ ฅ์ธต output = TimeDistributed(Dense(tag_size, activation='relu'))(output) base = Model(inputs=[word_ids, char_ids], outputs=[output]) model = CRFModel(base, tag_size) model.compile(optimizer=tf.keras.optimizers.Adam(0.001), metrics='accuracy') es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('bilstm_cnn_crf/cp.ckpt', monitor='val_decode_sequence_accuracy', mode='max', verbose=1, save_best_only=True, save_weights_only=True) CRF ์ธต์€ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ๋œ ๋ ˆ์ด๋ธ”์€ ์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ y_train์ด ์•„๋‹ˆ๋ผ y_train_int๋ฅผ ์‚ฌ์šฉํ•จ์„ ์ฃผ์˜ํ•ฉ๋‹ˆ๋‹ค. history = model.fit([X_train, X_char_train], y_train_int, batch_size=128, epochs=15, validation_split=0.1, callbacks=[mc, es]) ์กฐ๊ธฐ ์ข…๋ฃŒ๋กœ ํ•™์Šต์ด ๋๋‚ฌ๋‹ค๋ฉด ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์•˜์„ ๋‹น์‹œ๋ฅผ ์ €์žฅํ•ด๋‘” ๊ฐ€์ค‘์น˜๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ํ›„, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ 13๋ฒˆ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธกํ•ด ๋ด…์‹œ๋‹ค. model.load_weights('bilstm_cnn_crf/cp.ckpt') i = 13 # ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค. # ์ž…๋ ฅํ•œ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก y๋ฅผ ๋ฆฌํ„ด y_predicted = model.predict([np.array([X_test[i]]), np.array([X_char_test[i]])])[0] labels = np.argmax(y_test[i], -1) # ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝ. print("{:15}|{:5}|{}".format("๋‹จ์–ด", "์‹ค์ œ ๊ฐ’", "์˜ˆ์ธก๊ฐ’")) print(35 * "-") for word, tag, pred in zip(X_test[i], labels, y_predicted[0]): if word != 0: # PAD ๊ฐ’์€ ์ œ์™ธํ•จ. print("{:17}: {:7} {}".format(index_to_word[word], index_to_ner[tag], index_to_ner[pred])) ๋‹จ์–ด |์‹ค์ œ ๊ฐ’ |์˜ˆ์ธก๊ฐ’ ----------------------------------- the : O O statement : O O came : O O as : O O u.n. : B-org B-org secretary-general: I-org I-org kofi : B-per B-per annan : I-per I-per met : O O with : O O officials : O O in : O O amman : B-geo B-geo to : O O discuss : O O wednesday : B-tim B-tim 's : O O attacks : O O . : O O ์ •ํ™•ํ•˜๊ฒŒ ์ž˜ ์˜ˆ์ธกํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก ์‹œํ€€์Šค์ธ y_predicted๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์— ๋Œ€ํ•œ ํƒœ๊น… ์ •๋ณด ์‹œํ€€์Šค๋ฅผ ์–ป์€ ํ›„ F1-score๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. y_predicted = model.predict([X_test, X_char_test])[0] pred_tags = sequences_to_tag_for_crf(y_predicted) test_tags = sequences_to_tag(y_test) print("F1-score: {:.1%}".format(f1_score(test_tags, pred_tags))) print(classification_report(test_tags, pred_tags)) F1-score: 81.0% precision recall f1-score support art 0.25 0.06 0.10 63 eve 0.61 0.27 0.37 52 geo 0.85 0.84 0.84 7620 gpe 0.94 0.94 0.94 3145 nat 0.33 0.05 0.09 37 org 0.66 0.60 0.63 4033 per 0.76 0.77 0.77 3545 tim 0.89 0.85 0.87 4067 micro avg 0.82 0.80 0.81 22562 macro avg 0.66 0.55 0.58 22562 weighted avg 0.82 0.80 0.81 22562 4. BiLSTM-BiLSTM-CRF ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์ด ํ™œ์šฉ๋˜๋Š” ๊ณผ์ •์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋‹จ์–ด๋Š” ๋ฌธ์ž ๋‹จ์œ„๋กœ ํ† ํฐํ™”๋˜์—ˆ๊ณ , ํ† ํฐํ™”๋œ ๊ฐ ๋ฌธ์ž๋Š” ์œ„์˜ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ์ •์ˆ˜๋กœ ๋งคํ•‘๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ •์ˆ˜๋กœ ๋งคํ•‘๋œ ๊ฐ ๋ฌธ์ž๋Š” ์ž„๋ฒ ๋”ฉ ์ธต์„ ํ†ต๊ณผํ•˜๋ฉด 64์ฐจ์›์˜ ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ดํ›„ ์–‘๋ฐฉํ–ฅ LSTM ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š”๋ฐ, ์ด๋•Œ ์‚ฌ์šฉ๋˜๋Š” LSTM์˜ ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” 64์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น LSTM์€ ๋‹ค ๋Œ€ ์ผ(many-to-one) ๊ตฌ์กฐ๋กœ ์ˆœ๋ฐฉํ–ฅ LSTM์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์—ญ๋ฐฉํ–ฅ LSTM์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์—ฐ๊ฒฐ(concatenate) ๋œ ๊ฐ’์ด ์–‘๋ฐฉํ–ฅ LSTM์˜ ์ถœ๋ ฅ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ์ถœ๋ ฅ์„ ํ•˜๋‚˜์˜ ๋‹จ์–ด์— ๋Œ€ํ•œ ๋‹จ์–ด ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋‹จ์–ด ๋ฒกํ„ฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋˜ ๊ณผ์ •์„ ํ†ตํ•ด ์–ป์€ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์™€ ์—ฐ๊ฒฐ(concatenate) ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์œ„ํ•œ ์–‘๋ฐฉํ–ฅ LSTM์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ดํ›„์—๋Š” ์ด์ „ ์‹ค์Šต๋“ค๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. CRF ์ธต์€ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ๋œ ๋ ˆ์ด๋ธ”์€ ์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ y_train_int๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. embedding_dim = 128 char_embedding_dim = 64 dropout_ratio = 0.3 hidden_units = 64 # ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ word_ids = Input(batch_shape=(None, None), dtype='int32', name='word_input') word_embeddings = Embedding(input_dim=vocab_size, output_dim=embedding_dim, name='word_embedding')(word_ids) # char ์ž„๋ฒ ๋”ฉ char_ids = Input(batch_shape=(None, None, None), dtype='int32', name='char_input') char_embeddings = Embedding(input_dim=(len(char_to_index)), output_dim=char_embedding_dim, embeddings_initializer=RandomUniform(minval=-0.5, maxval=0.5), name='char_embedding')(char_ids) # char ์ž„๋ฒ ๋”ฉ์„ BiLSTM์„ ํ†ต๊ณผ์‹œ์ผœ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์–ป๊ณ  ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ๊ณผ ์—ฐ๊ฒฐ char_embeddings = TimeDistributed(Bidirectional(LSTM(hidden_units)))(char_embeddings) output = concatenate([word_embeddings, char_embeddings]) # ์—ฐ๊ฒฐํ•œ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฌธ์žฅ์˜ ๊ธธ์ด๋งŒํผ LSTM์„ ์ˆ˜ํ–‰ output = Dropout(dropout_ratio)(output) output = Bidirectional(LSTM(units=hidden_units, return_sequences=True))(output) # ์ถœ๋ ฅ์ธต output = TimeDistributed(Dense(tag_size, activation='relu'))(output) base = Model(inputs=[word_ids, char_ids], outputs=[output]) model = CRFModel(base, tag_size) model.compile(optimizer=tf.keras.optimizers.Adam(0.001), metrics='accuracy') es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4) mc = ModelCheckpoint('bilstm_bilstm_crf/cp.ckpt', monitor='val_decode_sequence_accuracy', mode='max', verbose=1, save_best_only=True, save_weights_only=True) history = model.fit([X_train, X_char_train], y_train_int, batch_size=128, epochs=15, validation_split=0.1, callbacks=[mc, es]) ์กฐ๊ธฐ ์ข…๋ฃŒ๋กœ ํ•™์Šต์ด ๋๋‚ฌ๋‹ค๋ฉด ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •ํ™•๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์•˜์„ ๋‹น์‹œ๋ฅผ ์ €์žฅํ•ด๋‘” ๊ฐ€์ค‘์น˜๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ํ›„, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ 13๋ฒˆ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. model.load_weights('bilstm_bilstm_crf/cp.ckpt') i = 13 # ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค. # ์ž…๋ ฅํ•œ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก y๋ฅผ ๋ฆฌํ„ด y_predicted = model.predict([np.array([X_test[i]]), np.array([X_char_test[i]])])[0] labels = np.argmax(y_test[i], -1) # ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝ. print("{:15}|{:5}|{}".format("๋‹จ์–ด", "์‹ค์ œ ๊ฐ’", "์˜ˆ์ธก๊ฐ’")) print(35 * "-") for word, tag, pred in zip(X_test[i], labels, y_predicted[0]): if word != 0: # PAD ๊ฐ’์€ ์ œ์™ธํ•จ. print("{:17}: {:7} {}".format(index_to_word[word], index_to_ner[tag], index_to_ner[pred])) ๋‹จ์–ด |์‹ค์ œ ๊ฐ’ |์˜ˆ์ธก๊ฐ’ ----------------------------------- the : O O statement : O O came : O O as : O O u.n. : B-org B-org secretary-general: I-org I-org kofi : B-per B-per annan : I-per I-per met : O O with : O O officials : O O in : O O amman : B-geo B-geo to : O O discuss : O O wednesday : B-tim B-tim 's : O O attacks : O O . : O O ์ •ํ™•ํ•˜๊ฒŒ ์ž˜ ์˜ˆ์ธกํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก ์‹œํ€€์Šค์ธ y_predicted๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์— ๋Œ€ํ•œ ํƒœ๊น… ์ •๋ณด ์‹œํ€€์Šค๋ฅผ ์–ป์€ ํ›„ F1-score๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. y_predicted = model.predict([X_test, X_char_test])[0] pred_tags = sequences_to_tag_for_crf(y_predicted) test_tags = sequences_to_tag(y_test) print("F1-score: {:.1%}".format(f1_score(test_tags, pred_tags))) print(classification_report(test_tags, pred_tags)) F1-score: 80.9% precision recall f1-score support art 0.29 0.03 0.06 63 eve 1.00 0.04 0.07 52 geo 0.83 0.86 0.85 7620 gpe 0.95 0.94 0.95 3145 nat 0.35 0.16 0.22 37 org 0.67 0.58 0.62 4033 per 0.79 0.74 0.77 3545 tim 0.88 0.84 0.86 4067 micro avg 0.83 0.79 0.81 22562 macro avg 0.72 0.52 0.55 22562 weighted avg 0.82 0.79 0.80 22562 99) ===ํ…์„œ ํ”Œ๋กœ 1๋ฒ„์ „ ์ฝ”๋“œ (๊ตฌ ๋ฒ„์ „)=== ์ดํ•˜ ์‹ค์Šต์€ ํ…์„œ ํ”Œ๋กœ 1๋ฒ„์ „ ์‹ค์Šต์œผ๋กœ ๊ณผ๊ฑฐ ๋ฒ„์ „ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. 12-01 ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition using Bi-LSTM) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ , ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  F1-Score๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. 1. ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— ์ด๋ฒˆ์— ์‚ฌ์šฉํ•  CRF layer๋Š” ํ˜„์žฌ ํ…์„œ ํ”Œ๋กœ 1.14.0๋ฒ„์ „๊ณผ ์ผ€๋ผ์Šค 2.2.4์—์„œ ์›ํ™œํ•˜๊ฒŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์šฐ์„  ๋ฒ„์ „์„ ๋งž์ถฐ์ค์‹œ๋‹ค. ๋กœ์ปฌ ํ™˜๊ฒฝ์˜ ๋ฒ„์ „์€ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ตฌ๊ธ€ Colab์—์„œ์˜ ์‹ค์Šต์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. !pip uninstall keras-nightly !pip uninstall -y tensorflow !pip install tensorflow==1.14.0 !pip install keras==2.2.4 !pip install tensorflow-gpu==1.14.0 !pip install h5py==2.10.0 CRF๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด keras_contrib๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. !pip install git+https://www.github.com/keras-team/keras-contrib.git 2. ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ์ด๋ฒˆ์—๋Š” ์–‘๋ฐฉํ–ฅ LSTM๊ณผ CRF๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์•ž์„œ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ์™ธ์— ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์ˆ˜ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ์•„๋ž˜์˜ ๋งํฌ์—์„œ ๋‹ค์šด๋กœ๋“œ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://www.kaggle.com/abhinavwalia95/entity-annotated-corpus import pandas as pd import numpy as np %matplotlib inline import matplotlib.pyplot as plt from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from sklearn.model_selection import train_test_split from tensorflow.keras.utils import to_categorical data = pd.read_csv("ner_dataset.csv ํŒŒ์ผ์˜ ๊ฒฝ๋กœ", encoding="latin1") data[:5] ์ด๋ฒˆ์— ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ์•ž์„œ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ๋ž‘ ์–‘์‹์ด ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์—ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŒจํ„ด์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Sentence: 1 ์žˆ๊ณ , Null ๊ฐ’์ด ์ด์–ด์ง€๋‹ค๊ฐ€ ๋‹ค์‹œ Sentence: 2๊ฐ€ ๋‚˜์˜ค๊ณ  ๋‹ค์‹œ Null ๊ฐ’์ด ์ด์–ด์ง€๋‹ค๊ฐ€ Sentence: 3์ด ๋‚˜์˜ค๊ณ  ๋‹ค์‹œ Null ๊ฐ’์ด ์ด์–ด์ง€๋‹ค๊ฐ€๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‚ฌ์‹ค ์ด๋Š” ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์„ ์—ฌ๋Ÿฌ ํ–‰์œผ๋กœ ๋‚˜๋ˆ ๋†“์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ˆซ์ž ๊ฐ’์„ t๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ฒซ ๋ฒˆ์งธ Sentence: t๋ถ€ํ„ฐ Null ๊ฐ’์ด ๋‚˜์˜ค๋‹ค๊ฐ€ Sentence: t+1์ด ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋Š” ์›๋ž˜ ํ•˜๋‚˜์˜ ํ–‰. ์ฆ‰, ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. t ๋ฒˆ์งธ ๋ฌธ์žฅ์„ ๊ฐ ๋‹จ์–ด๋งˆ๋‹ค ๊ฐ ํ•˜๋‚˜์˜ ํ–‰์œผ๋กœ ๋‚˜๋ˆ ๋†“์€ ๋ฐ์ดํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋’ค์—์„œ Pandas์˜ fillna๋ฅผ ํ†ตํ•ด ํ•˜๋‚˜๋กœ ๋ฌถ๋Š” ์ž‘์—…์„ ํ•ด์ค๋‹ˆ๋‹ค. print('๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ํ–‰์˜ ๊ฐœ์ˆ˜ : {}'.format(len(data))) ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ํ–‰์˜ ๊ฐœ์ˆ˜ : 1048575 ํ˜„์žฌ data์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๋Š” 1,048,575๊ฐœ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋’ค์—์„œ ๊ธฐ์กด์— ๋ฌธ์žฅ 1๊ฐœ์˜€๋˜ ํ–‰๋“ค์„ 1๊ฐœ์˜ ํ–‰์œผ๋กœ ๋ณ‘ํ•ฉํ•˜๋Š” ์ž‘์—…์„ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ข… ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋Š” ์ด๋ณด๋‹ค ์ค„์–ด๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์šฐ์„ , ๋ฐ์ดํ„ฐ๋ฅผ ์ข€ ๋” ์‚ดํŽด๋ด…์‹œ๋‹ค. print('๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์œ ๋ฌด : ' + str(data.isnull().values.any())) ๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์œ ๋ฌด : True Sentence #์—ด์— Null ๊ฐ’๋“ค์ด ์กด์žฌํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, isnull().values.any()๋ฅผ ์ˆ˜ํ–‰ํ•˜์˜€์„ ๋•Œ True๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. print('์–ด๋–ค ์—ด์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์ถœ๋ ฅ') print('==============================') data.isnull().sum() ์–ด๋–ค ์—ด์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์ถœ๋ ฅ ============================== Sentence # 1000616 Word 0 POS 0 Tag 0 dtype: int64 isnull().sum()์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๊ฐ ์—ด๋งˆ๋‹ค์˜ Null ๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์—ด์€ 0๊ฐœ์ธ๋ฐ ์˜ค์ง Sentences #์—ด์—์„œ๋งŒ 1,000,616๊ฐœ๊ฐ€ ๋‚˜์˜จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ , ์œ ์ผํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ์…€ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” nunique()๋ฅผ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. print('sentence # ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : {}'.format(data['Sentence #'].nunique())) print('Word ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : {}'.format(data.Word.nunique())) print('Tag ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : {}'.format(data.Tag.nunique())) sentence # ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : 47959 Word ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : 35178 Tag ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : 17 ์ด ๋ฐ์ดํ„ฐ์—๋Š” 47,959๊ฐœ์˜ ๋ฌธ์žฅ์ด ์žˆ์œผ๋ฉฐ ๋ฌธ์žฅ๋“ค์€ 35,178๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๊ฐ€์ง€๊ณ  17๊ฐœ ์ข…๋ฅ˜์˜ ๊ฐœ์ฒด๋ช… ํƒœ๊น…์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 17๊ฐœ์˜ ๊ฐœ์ฒด๋ช… ํƒœ๊น…์ด ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๋ช‡ ๊ฐœ๊ฐ€ ์žˆ๋Š”์ง€, ๊ฐœ์ฒด๋ช… ํƒœ๊น… ๊ฐœ์ˆ˜์˜ ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. print('Tag ์—ด์˜ ๊ฐ๊ฐ์˜ ๊ฐ’์˜ ๊ฐœ์ˆ˜ ์นด์šดํŠธ') print('================================') print(data.groupby('Tag').size().reset_index(name='count')) Tag ์—ด์˜ ๊ฐ๊ฐ์˜ ๊ฐ’์˜ ๊ฐœ์ˆ˜ ์นด์šดํŠธ ================================ Tag count 0 B-art 402 1 B-eve 308 2 B-geo 37644 3 B-gpe 15870 4 B-nat 201 5 B-org 20143 6 B-per 16990 7 B-tim 20333 8 I-art 297 9 I-eve 253 10 I-geo 7414 11 I-gpe 198 12 I-nat 51 13 I-org 16784 14 I-per 17251 15 I-tim 6528 16 O 887908 BIO ํ‘œํ˜„ ๋ฐฉ๋ฒ•์—์„œ ์•„๋ฌด๋Ÿฐ ํƒœ๊น…๋„ ์˜๋ฏธํ•˜์ง€ ์•Š๋Š” O๊ฐ€ ๊ฐ€์žฅ 887,908๊ฐœ๋กœ ๊ฐ€์žฅ ๋งŽ์€ ๊ฐœ์ˆ˜๋ฅผ ์ฐจ์ง€ํ•จ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ๊ฐ€๊ณตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  Null ๊ฐ’์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. data = data.fillna(method="ffill") Pandas์˜ (method='ffill')๋Š” Null ๊ฐ’์„ ๊ฐ€์ง„ ํ–‰์˜ ๋ฐ”๋กœ ์•ž์˜ ํ–‰์˜ ๊ฐ’์œผ๋กœ Null ๊ฐ’์„ ์ฑ„์šฐ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด t ๋ฒˆ์งธ ๋ฌธ์žฅ์— ์†ํ•˜๋ฉด์„œ Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ๋“ค์€ ์ „๋ถ€ ์ฒซ ๋ฒˆ์งธ ์—ด์— Sentence: t์˜ ๊ฐ’์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋’ค์˜ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด์„œ ์ •์ƒ์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(data.tail()) Sentence # Word POS Tag 1048570 Sentence: 47959 they PRP O 1048571 Sentence: 47959 responded VBD O 1048572 Sentence: 47959 to TO O 1048573 Sentence: 47959 the DT O 1048574 Sentence: 47959 attack NN O ๋’ค์˜ 5๊ฐœ ์ƒ˜ํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ์—ด์ด Sentence: 47959๋กœ ์ฑ„์›Œ์กŒ์Šต๋‹ˆ๋‹ค. ์ด๋Š” 47,959๋ฒˆ์งธ ๋ฌธ์žฅ์ž„์„ ์˜๋ฏธํ•˜๋ฉฐ, Null ๊ฐ’์„ ๊ฐ€์ง„ ํ–‰๋“ค์˜ ๋ฐ”๋กœ ์•ž ํ–‰์˜ Sentence # ์—ด์˜ ๊ฐ’์ด Sentence: 47959์ด์—ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์œ ๋ฌด : ' + str(data.isnull().values.any())) ๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ์œ ๋ฌด : False ์—†๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์†Œ๋ฌธ์žํ™”ํ•˜์—ฌ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์—ฌ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. data['Word'] = data['Word'].str.lower() print('Word ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : {}'.format(data.Word.nunique())) Word ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ : 31817 ์ •์ƒ์ ์œผ๋กœ ์†Œ๋ฌธ์ž ํ™”๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ์•ž์˜ ์ƒ˜ํ”Œ 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(data[:5]) Sentence # Word POS Tag 0 Sentence: 1 thousands NNS O 1 Sentence: 1 of IN O 2 Sentence: 1 demonstrators NNS O 3 Sentence: 1 have VBP O 4 Sentence: 1 marched VBN O ์ด์ œ ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์— ๋“ฑ์žฅํ•œ ๋‹จ์–ด์™€ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋ผ๋ฆฌ ์Œ(pair)์œผ๋กœ ๋ฌถ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. func = lambda temp: [(w, t) for w, t in zip(temp["Word"].values.tolist(), temp["Tag"].values.tolist())] tagged_sentences=[t for t in data.groupby("Sentence #").apply(func)] print("์ „์ฒด ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜: {}".format(len(tagged_sentences))) ์ „์ฒด ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜: 47959 1,000,616๊ฐœ์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ ๋ฌธ์žฅ๋‹น ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ๋กœ ๋ฌถ์ด๋ฉด์„œ 47,959๊ฐœ์˜ ์ƒ˜ํ”Œ๋กœ ๋ณ€ํ™˜๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •์ƒ์ ์œผ๋กœ ์ˆ˜ํ–‰์ด ๋˜์—ˆ๋Š”์ง€ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅ์„ ํ•ด๋ด…์‹œ๋‹ค. print(tagged_sentences[0]) # ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ์ถœ๋ ฅ [('thousands', 'O'), ('of', 'O'), ('demonstrators', 'O'), ('have', 'O'), ('marched', 'O'), ('through', 'O'), ('london', 'B-geo'), ('to', 'O'), ('protest', 'O'), ('the', 'O'), ('war', 'O'), ('in', 'O'), ('iraq', 'B-geo'), ('and', 'O'), ('demand', 'O'), ('the', 'O'), ('withdrawal', 'O'), ('of', 'O'), ('british', 'B-gpe'), ('troops', 'O'), ('from', 'O'), ('that', 'O'), ('country', 'O'), ('.', 'O')] ์ „์ฒ˜๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์ด ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒ˜ํ”Œ์ด ์ด 47,959๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ›ˆ๋ จ์„ ์‹œํ‚ค๋ ค๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋‹จ์–ด์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„๊ณผ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, [('thousands', 'O'), ('of', 'O')]์™€ ๊ฐ™์€ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์ด ์žˆ๋‹ค๋ฉด thousands์™€ of๋Š” ๊ฐ™์ด ์ €์žฅํ•˜๊ณ , O์™€ O๋ฅผ ๊ฐ™์ด ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ํŒŒ์ด์ฌ ํ•จ์ˆ˜ ์ค‘์—์„œ zip() ํ•จ์ˆ˜๊ฐ€ ์œ ์šฉํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. zip() ํ•จ์ˆ˜๋Š” ๋™์ผํ•œ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์‹œํ€€์Šค ์ž๋ฃŒํ˜•์—์„œ ๊ฐ ์ˆœ์„œ์— ๋“ฑ์žฅํ•˜๋Š” ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. (2์ฑ•ํ„ฐ์˜ ๋ฐ์ดํ„ฐ์˜ ๋ถ„๋ฆฌ ์ฑ•ํ„ฐ ์ฐธ๊ณ ) sentences, ner_tags = [], [] for tagged_sentence in tagged_sentences: # 47,959๊ฐœ์˜ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ 1๊ฐœ์”ฉ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. sentence, tag_info = zip(*tagged_sentence) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด๋“ค์€ sentence์— ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋“ค์€ tag_info์— ์ €์žฅ. sentences.append(list(sentence)) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด ์ •๋ณด๋งŒ ์ €์žฅํ•œ๋‹ค. ner_tags.append(list(tag_info)) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋งŒ ์ €์žฅํ•œ๋‹ค. ๊ฐ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด๋Š” sentences์— ํƒœ๊น… ์ •๋ณด๋Š” ner_tags์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ž„์˜๋กœ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(sentences[0]) print(ner_tags[0]) ['thousands', 'of', 'demonstrators', 'have', 'marched', 'through', 'london', 'to', 'protest', 'the', 'war', 'in', 'iraq', 'and', 'demand', 'the', 'withdrawal', 'of', 'british', 'troops', 'from', 'that', 'country', '.'] ['O', 'O', 'O', 'O', 'O', 'O', 'B-geo', 'O', 'O', 'O', 'O', 'O', 'B-geo', 'O', 'O', 'O', 'O', 'O', 'B-gpe', 'O', 'O', 'O', 'O', 'O'] ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋งŒ sentences[0]์—, ๋˜ํ•œ ๊ฐœ์ฒด๋ช…์— ๋Œ€ํ•ด์„œ๋งŒ ner_tags[0]์— ์ €์žฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ณด๊ฒ ์ง€๋งŒ, sentences๋Š” ์˜ˆ์ธก์„ ์œ„ํ•œ X์— ํ•ด๋‹น๋˜๋ฉฐ ner_tags๋Š” ์˜ˆ์ธก ๋Œ€์ƒ์ธ y์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ƒ˜ํ”Œ๋“ค์— ๋Œ€ํ•ด์„œ๋„ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ž„์˜๋กœ 99๋ฒˆ์งธ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ๋„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(sentences[98]) print(ner_tags[98]) ['she', 'had', 'once', 'received', 'a', 'kidney', 'transplant', '.'] ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'] ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋งŒ sentences[98]์—, ๋˜ํ•œ ๊ฐœ์ฒด๋ช…์— ๋Œ€ํ•ด์„œ๋งŒ ner_tags[98]์— ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๊ณผ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค 47,959๊ฐœ์˜ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” ์ „๋ถ€ ์ œ๊ฐ๊ฐ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : %d' % max(len(l) for l in sentences)) print('์ƒ˜ํ”Œ์˜ ํ‰๊ท  ๊ธธ์ด : %f' % (sum(map(len, sentences))/len(sentences))) plt.hist([len(s) for s in sentences], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 104 ์ƒ˜ํ”Œ์˜ ํ‰๊ท  ๊ธธ์ด : 21.863987989741236 ์œ„์˜ ๊ทธ๋ž˜ํ”„๋Š” ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๊ฐ€ ๋Œ€์ฒด์ ์œผ๋กœ 0~40์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” 104์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ†ตํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. src_tokenizer = Tokenizer(oov_token='OOV') # ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์ธ๋ฑ์Šค 1์—๋Š” ๋‹จ์–ด 'OOV'๋ฅผ ํ• ๋‹นํ•œ๋‹ค. src_tokenizer.fit_on_texts(sentences) tar_tokenizer = Tokenizer(lower=False) # ํƒœ๊น… ์ •๋ณด๋“ค์€ ๋‚ด๋ถ€์ ์œผ๋กœ ๋Œ€๋ฌธ์ž๋ฅผ<NAME> ์ฑ„๋กœ ์ €์žฅ tar_tokenizer.fit_on_texts(ner_tags) ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” src_tokenizer๋ฅผ, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด์— ๋Œ€ํ•ด์„œ๋Š” tar_tokenizer๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. vocab_size = len(src_tokenizer.word_index) + 1 tag_size = len(tar_tokenizer.word_index) + 1 print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(vocab_size)) print('๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(tag_size)) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 31819 ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 18 ์•ž์„œ src_tokenizer๋ฅผ ๋งŒ๋“ค ๋•Œ Tokenizer์˜ ์ธ์ž๋กœ oov_token='OOV'๋ฅผ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ธ๋ฑ์Šค 1์— ๋‹จ์–ด 'OOV'๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. print('๋‹จ์–ด OOV์˜ ์ธ๋ฑ์Šค : {}'.format(src_tokenizer.word_index['OOV'])) ๋‹จ์–ด OOV์˜ ์ธ๋ฑ์Šค : 1 ์ด์ œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. X_data = src_tokenizer.texts_to_sequences(sentences) y_data = tar_tokenizer.texts_to_sequences(ner_tags) ์ด์ œ ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋Š” X_data, ๊ฐœ์ฒด๋ช… ํƒœ๊น… ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋Š” y_data์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ์„ ์œ„ํ•ด ์ž„์˜๋กœ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(X_data[0]) print(y_data[0]) [254, 6, 967, 16, 1795, 238, 468, 7, 523, 2, 129, 5, 61, 9, 571, 2, 833, 6, 186, 90, 22, 15, 56, 3] [1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 8, 1, 1, 1, 1, 1] ๋ชจ๋ธ ํ›ˆ๋ จ ํ›„ ๊ฒฐ๊ณผ ํ™•์ธ์„ ์œ„ํ•ด ์ธ๋ฑ์Šค๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_word๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ์™€ ๋™์‹œ์— ๋’ค์—์„œ ์‚ฌ์šฉํ•  index_to_ner๋„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋•Œ, ์ธ๋ฑ์Šค 0์€ 'PAD'๋ž€ ๋‹จ์–ด๋ฅผ ํ• ๋‹นํ•ด๋‘๊ฒ ์Šต๋‹ˆ๋‹ค. index_to_ner์€ ๊ฐœ์ˆ˜๊ฐ€ ์ ์œผ๋‹ˆ ์ถœ๋ ฅ๊นŒ์ง€ ํ•ด๋ด…์‹œ๋‹ค. word_to_index = src_tokenizer.word_index index_to_word = src_tokenizer.index_word ner_to_index = tar_tokenizer.word_index index_to_ner = tar_tokenizer.index_word index_to_ner[0] = 'PAD' print(index_to_ner) {1: 'O', 2: 'B-geo', 3: 'B-tim', 4: 'B-org', 5: 'I-per', 6: 'B-per', 7: 'I-org', 8: 'B-gpe', 9: 'I-geo', 10: 'I-tim', 11: 'B-art', 12: 'B-eve', 13: 'I-art', 14: 'I-eve', 15: 'B-nat', 16: 'I-gpe', 17: 'I-nat', 0: 'PAD'} index_to_word๋ฅผ ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ ์‹œํ—˜ ์‚ผ์•„ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ๋””์ฝ”๋”ฉ(์ •์ˆ˜์—์„œ ๋‹ค์‹œ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜) ์ž‘์—…์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. decoded = [] for index in X_data[0] : # ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ์•ˆ์˜ ์ธ๋ฑ์Šค๋“ค์— ๋Œ€ํ•ด์„œ decoded.append(index_to_word[index]) # ๋‹ค์‹œ ๋‹จ์–ด๋กœ ๋ณ€ํ™˜ print('๊ธฐ์กด์˜ ๋ฌธ์žฅ : {}'.format(sentences[0])) print('๋””์ฝ”๋”ฉ ๋ฌธ์žฅ : {}'.format(decoded)) ๊ธฐ์กด์˜ ๋ฌธ์žฅ : ['thousands', 'of', 'demonstrators', 'have', 'marched', 'through', 'london', 'to', 'protest', 'the', 'war', 'in', 'iraq', 'and', 'demand', 'the', 'withdrawal', 'of', 'british', 'troops', 'from', 'that', 'country', '.'] ๋””์ฝ”๋”ฉ ๋ฌธ์žฅ : ['thousands', 'of', 'demonstrators', 'have', 'marched', 'through', 'london', 'to', 'protest', 'the', 'war', 'in', 'iraq', 'and', 'demand', 'the', 'withdrawal', 'of', 'british', 'troops', 'from', 'that', 'country', '.'] ์ด์ œ X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๊ฐ€ ๊ตฌ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํŒจ๋”ฉ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์•ž์„œ ํ™•์ธํ•˜์˜€๋“ฏ์ด ๋Œ€๋ถ€๋ถ„์˜ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋Š” 40~60์— ๋ถ„ํฌ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด์ธ 104๊ฐ€ ์•„๋‹ˆ๋ผ 70 ์ •๋„๋กœ max_len์„ ์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. max_len = 70 # ๋ชจ๋“  ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋ฅผ ๋งž์ถœ ๋•Œ ๋’ค์˜ ๊ณต๊ฐ„์— ์ˆซ์ž 0์œผ๋กœ ์ฑ„์›€. X_data = pad_sequences(X_data, padding='post', maxlen=max_len) y_data = pad_sequences(y_data, padding='post', maxlen=max_len) ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๊ฐ€ 70์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 8:2์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=.2, random_state=777) ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” ํƒœ๊น… ์ •๋ณด์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. y_train = to_categorical(y_train, num_classes=tag_size) y_test = to_categorical(y_test, num_classes=tag_size) ์ด์ œ ๊ฐ ๋ฐ์ดํ„ฐ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_train.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_train.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_test.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_test.shape)) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (38367, 70) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (38367, 70, 18) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (9592, 70) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (9592, 70, 18) 3. F1-Score ์‹œํ€€์Šค ๋ ˆ์ด๋ธ”๋ง ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•  ๋•Œ๋Š” ํ•œ ๊ฐ€์ง€ ์ฃผ์˜ํ•  ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ณดํ†ต ํฐ ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ์•Š๋Š” ๋ ˆ์ด๋ธ” ์ •๋ณด๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐœ์ฒด๋ช… ์ธ์‹์—์„œ๋Š” ๊ทธ ์–ด๋–ค ๊ฐœ์ฒด๋„ ์•„๋‹ˆ๋ผ๋Š” ์˜๋ฏธ์˜ 'O'๋ผ๋Š” ํƒœ๊น…์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฐ ์ •๋ณด๋Š” ๋ณดํ†ต ๋Œ€๋‹ค์ˆ˜์˜ ๋ ˆ์ด๋ธ”์„ ์ฐจ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— ์‚ฌ์šฉํ–ˆ๋˜ ์ •ํ™•๋„ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ชจ๋ธ์ด ๋‹จ 1๊ฐœ์˜ ๊ฐœ์ฒด๋„ ๋งž์ถ”์ง€ ๋ชปํ•˜๊ณ  ์ „๋ถ€ 'O'๋กœ ์˜ˆ์ƒํ–ˆ์„ ๊ฒฝ์šฐ๋ฅผ ๋ด…์‹œ๋‹ค. ์šฐ์„  ์‹ค์ œ ๊ฐ’์€ ๋ฐ”๋กœ ์œ„์—์„œ ์ถœ๋ ฅํ–ˆ๋˜ ๊ฐ’์„ ์‹ค์ œ ๊ฐ’์œผ๋กœ ์žฌ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” true๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐœ์ฒด๋ฅผ ํ•˜๋‚˜๋„ ๋งž์ถ”์ง€ ๋ชปํ–ˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ „๋ถ€ 'O'๋กœ๋งŒ ์ฑ„์›Œ์ง„ ์˜ˆ์ธก๊ฐ’ predicted๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. true=['B-PER', 'I-PER', 'O', 'O', 'B-MISC', 'O','O','O','O','O','O','O','O','O','O','B-PER','I-PER','O','O','O','O','O','O','B-MISC','I-MISC','I-MISC','O','O','O','O','O','O','B-PER','I-PER','O','O','O','O','O'] # ์‹ค์ œ ๊ฐ’ predicted=['O'] * len(true) #์‹ค์ œ ๊ฐ’์˜ ๊ธธ์ด๋งŒํผ ์ „๋ถ€ 'O'๋กœ ์ฑ„์›Œ์ง„ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ. ์˜ˆ์ธก๊ฐ’์œผ๋กœ ์‚ฌ์šฉ. print(predicted) ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'] ์‹ค์ œ๋กœ๋Š” PER, MISC, PER, MISC, PER์ด๋ผ๋Š” ์ด 5๊ฐœ์˜ ๊ฐœ์ฒด๊ฐ€ ์กด์žฌํ•จ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์˜ˆ์ธก๊ฐ’์ธ predicted๋Š” ๋‹จ 1๊ฐœ์˜ ๊ฐœ์ฒด๋„ ๋งž์ถ”์ง€ ๋ชปํ•œ ์ƒํ™ฉ์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ด์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. hit = 0 # ์ •๋‹ต ๊ฐœ์ˆ˜ for t, p in zip(true, predicted): if t == p: hit +=1 # ์ •๋‹ต์ธ ๊ฒฝ์šฐ์—๋งŒ +1 accuracy = hit/len(true) # ์ •๋‹ต ๊ฐœ์ˆ˜๋ฅผ ์ด๊ฐœ์ˆ˜๋กœ ๋‚˜๋ˆˆ๋‹ค. print("์ •ํ™•๋„: {:.1%}".format(accuracy)) ์ •ํ™•๋„: 74.4% ์‹ค์ œ ๊ฐ’์—์„œ๋„ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ’์ด 'O'์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์–ด๋–ค ๊ฐœ์ฒด๋„ ์ฐพ์ง€ ๋ชปํ•˜์˜€์Œ์—๋„ 74%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ •ํ™•๋„๊ฐ€ ๋ปฅํŠ€๊ธฐ๋˜์–ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์˜คํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์—ฌ๊ธฐ์„œ๋Š” ์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ๋” ์ ์ ˆํ•œ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์„ ๋„์ž…ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์œˆ๋„์˜ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋‚˜ UNIX์˜ ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜์˜ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€ seqeval๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. !pip install seqeval ์•ž์„œ ๋จธ์‹  ๋Ÿฌ๋‹ ํ›‘์–ด๋ณด๊ธฐ ์ฑ•ํ„ฐ์—์„œ ์ •๋ฐ€๋„(precision)๊ณผ ์žฌํ˜„์œจ(recall)์„ ๋ฐฐ์šด ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ ์ธก์ •์„ ์œ„ํ•ด ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ฌธ์ œ์— ๋งž๋„๋ก ํ•ด์„ํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ •๋ฐ€๋„ ํŠน์ • ๊ฐœ์ฒด๋ผ๊ณ  ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ ์ค‘์—์„œ ์‹ค์ œ ํŠน์ • ๊ฐœ์ฒด๋กœ ํŒ๋ช…๋˜์–ด ์˜ˆ์ธก์ด ์ผ์น˜ํ•œ ๋น„์œจ ์ •๋ฐ€๋„ T T + P ํŠน์ • ๊ฐœ์ฒด๋ผ๊ณ  ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ ์ค‘์—์„œ ์‹ค์ œ ํŠน์ • ๊ฐœ์ฒด๋กœ ํŒ๋ช…๋˜์–ด ์˜ˆ์ธก์ด ์ผ์น˜ํ•œ ๋น„์œจ ์žฌํ˜„์œจ ์ „์ฒด ํŠน์ • ๊ฐœ์ฒด ์ค‘์—์„œ ์‹ค์ œ ํŠน์ • ๊ฐœ์ฒด๋ผ๊ณ  ์ •๋‹ต์„ ๋งžํžŒ ๋น„์œจ ์žฌํ˜„์œจ T T + N ์ „์ฒด ํŠน์ • ๊ฐœ์ฒด ์ค‘์—์„œ ์‹ค์ œ ํŠน์ • ๊ฐœ์ฒด๋ผ๊ณ  ์ •๋‹ต์„ ๋งžํžŒ ๋น„์œจ ์ •๋ฐ€๋„์™€ ์žฌํ˜„์œจ๋กœ๋ถ€ํ„ฐ ์กฐํ™” ํ‰๊ท (harmonic mean)์„ ๊ตฌํ•œ ๊ฒƒ์„ f1-score๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฐ€๋„ ์žฌํ˜„์œจ ์ •๋ฐ€๋„ ์žฌํ˜„์œจ 1 s o e 2 ์ •๋ฐ€๋„ ร— ์žฌํ˜„์œจ ์ •๋ฐ€๋„ + ์žฌํ˜„์œจ predicted์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ •๋ฐ€๋„, ์žฌํ˜„์œจ, f1-score๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. from seqeval.metrics import classification_report print(classification_report([true], [predicted])) precision recall f1-score support MISC 0.00 0.00 0.00 2 PER 0.00 0.00 0.00 3 micro avg 0.00 0.00 0.00 5 macro avg 0.00 0.00 0.00 5 weighted avg 0.00 0.00 0.00 5 ์ด๋Ÿฌํ•œ ์ธก์ • ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด PER๊ณผ MISC ๋‘ ํŠน์ • ๊ฐœ์ฒด ์ค‘์—์„œ ์‹ค์ œ predicted๊ฐ€ ๋งž์ถ˜ ๊ฒƒ์€ ๋‹จ 1๊ฐœ๋„ ์—†๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์–ด๋Š ์ •๋„๋Š” ์ •๋‹ต์„ ๋งžํ˜”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์˜ˆ์ธก๊ฐ’์ธ predicted๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์ •๋ฐ€๋„, ์žฌํ˜„์œจ, f1-score๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. true=['B-PER', 'I-PER', 'O', 'O', 'B-MISC', 'O','O','O','O','O','O','O','O','O','O','B-PER','I-PER','O','O','O','O','O','O','B-MISC','I-MISC','I-MISC','O','O','O','O','O','O','B-PER','I-PER','O','O','O','O','O'] predicted=['B-PER', 'I-PER', 'O', 'O', 'B-MISC', 'O','O','O','O','O','O','O','O','O','O','B-PER','I-PER','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O','O'] print(classification_report([true], [predicted])) precision recall f1-score support MISC 1.00 0.50 0.67 2 PER 1.00 0.67 0.80 3 micro avg 1.00 0.60 0.75 5 macro avg 1.00 0.58 0.73 5 weighted avg 1.00 0.60 0.75 5 ํŠน์ • ๊ฐœ์ฒด๋กœ ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ๋Š” ๋ชจ๋‘ ์ œ๋Œ€๋กœ ์˜ˆ์ธก์„ ํ•˜์˜€์œผ๋ฏ€๋กœ ์ •๋ฐ€๋„๋Š” 1์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์žฌํ˜„์œจ์—์„œ๋Š” MISC๋Š” ์‹ค์ œ๋กœ๋Š” 4๊ฐœ์ž„์—๋„ 2๊ฐœ๋งŒ์„ ๋งž์ถ”์—ˆ์œผ๋ฏ€๋กœ 0.5, PER์€ ์‹ค์ œ๋กœ๋Š” 3๊ฐœ์ž„์—๋„ 2๊ฐœ๋งŒ์„ ๋งž์ถ”์—ˆ์œผ๋ฏ€๋กœ 0.67์ด ๋‚˜์˜จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. F1-score๋ฅผ ์ธก์ •ํ•˜๋Š” ์ฝœ๋ฐฑ ํด๋ž˜์Šค from keras.callbacks import Callback from seqeval.metrics import f1_score, classification_report ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ F1-score๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ด๋‘๋ฉด ๋ชจ๋ธ์„ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ๊ฒ€์ฆํ•˜๋Š” ๊ณผ์ •์—์„œ F1-score๋ฅผ ์ง€์†์ ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  F1-score๊ฐ€ ๊ฐ€์žฅ ๋†’์•„์งˆ ๋•Œ๋งˆ๋‹ค ๋ชจ๋ธ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. class F1score(Callback): def __init__(self, value = 0.0, use_char=True): super(F1score, self).__init__() self.value = value self.use_char = use_char def sequences_to_tags(self, sequences): # ์˜ˆ์ธก๊ฐ’์„ index_to_ner๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํƒœ๊น… ์ •๋ณด๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜. result = [] for sequence in sequences: # ์ „์ฒด ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ์‹œํ€€์Šค๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ธ๋‹ค. tag = [] for pred in sequence: # ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ์˜ˆ์ธก๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ธ๋‹ค. pred_index = np.argmax(pred) # ์˜ˆ๋ฅผ ๋“ค์–ด [0, 0, 1, 0 ,0] ๋ผ๋ฉด 1์˜ ์ธ๋ฑ์Šค์ธ 2๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. tag.append(index_to_ner[pred_index].replace("PAD", "O")) # 'PAD'๋Š” 'O'๋กœ ๋ณ€๊ฒฝ result.append(tag) return result # ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜ def on_epoch_end(self, epoch, logs={}): # char Embedding์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ if self.use_char: X_test = self.validation_data[0] X_char_test = self.validation_data[1] y_test = self.validation_data[2] y_predicted = self.model.predict([X_test, X_char_test]) else: X_test = self.validation_data[0] y_test = self.validation_data[1] y_predicted = self.model.predict([X_test]) pred_tags = self.sequences_to_tags(y_predicted) test_tags = self.sequences_to_tags(y_test) score = f1_score(pred_tags, test_tags) print(' - f1: {:04.2f}'.format(score * 100)) print(classification_report(test_tags, pred_tags)) # F1-score๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€ ์ค‘ ๊ฐ€์žฅ ๋†’์€ ๊ฒฝ์šฐ if score > self.value: print('f1_score improved from %f to %f, saving model to best_model.h5'%(self.value, score)) self.model.save('best_model.h5') self.value = score else: print('f1_score did not improve from %f'%(self.value)) 5. BiLSTM์„ ์ด์šฉํ•œ ๊ฐœ์ฒด ๋ช…์ธ ์‹๊ธฐ from keras.models import Sequential from keras.layers import Dense, LSTM, InputLayer, Bidirectional, TimeDistributed, Embedding from keras.optimizers import Adam from keras.models import load_model model = Sequential() model.add(Embedding(vocab_size, 128, input_length=max_len, mask_zero=True)) model.add(Bidirectional(LSTM(256, return_sequences=True))) model.add(TimeDistributed(Dense(tag_size, activation=('softmax')))) model.compile(loss='categorical_crossentropy', optimizer=Adam(0.001), metrics=['accuracy']) history = model.fit(X_train, y_train, batch_size=32, epochs=10, validation_split=0.1, callbacks=[F1score(use_char=False)]) bilstm_model = load_model('best_model.h5') i=13 # ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค. y_predicted = bilstm_model.predict(np.array([X_test[i]])) # ์ž…๋ ฅํ•œ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก y๋ฅผ ๋ฆฌํ„ด y_predicted = np.argmax(y_predicted, axis=-1) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ๋‹ค์‹œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝํ•จ. true = np.argmax(y_test[i], -1) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ๋‹ค์‹œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝํ•จ. print("{:15}|{:5}|{}".format("๋‹จ์–ด", "์‹ค์ œ ๊ฐ’", "์˜ˆ์ธก๊ฐ’")) print(35 * "-") for w, t, pred in zip(X_test[i], true, y_predicted[0]): if w != 0: # PAD ๊ฐ’์€ ์ œ์™ธํ•จ. print("{:17}: {:7} {}".format(index_to_word[w], index_to_ner[t], index_to_ner[pred])) ๋‹จ์–ด |์‹ค์ œ ๊ฐ’ |์˜ˆ์ธก๊ฐ’ ----------------------------------- the : O O statement : O O came : O O as : O O u.n. : B-org B-org secretary-general: I-org I-org kofi : B-per B-per annan : I-per I-per met : O O with : O O officials : O O in : O O amman : B-geo B-geo to : O O discuss : O O wednesday : B-tim B-tim 's : O O attacks : O O . : O O f1score = F1score() y_predicted = bilstm_model.predict([X_test]) pred_tags = f1score.sequences_to_tags(y_predicted) test_tags = f1score.sequences_to_tags(y_test) print(classification_report(test_tags, pred_tags)) precision recall f1-score support art 0.00 0.00 0.00 63 eve 0.64 0.31 0.42 52 geo 0.82 0.82 0.82 7620 gpe 0.95 0.94 0.95 3145 nat 1.00 0.08 0.15 37 org 0.57 0.56 0.57 4033 per 0.70 0.75 0.72 3545 tim 0.76 0.83 0.79 4067 micro avg 0.76 0.78 0.77 22562 macro avg 0.68 0.54 0.55 22562 weighted avg 0.76 0.78 0.77 22562 print("F1-score: {:.1%}".format(f1_score(test_tags, pred_tags))) F1-score: 76.9% 12-02 ์–‘๋ฐฉํ–ฅ LSTM๊ณผ CRF(Bidirectional LSTM + CRF) ์ด๋ฒˆ ์ฑ•ํ„ฐ๋Š” ์•„๋ž˜์˜ ์ฑ•ํ„ฐ๋ฅผ ์ด๋ฏธ ์‹คํ–‰ํ•œ ์ƒํƒœ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์ฑ•ํ„ฐ ๋งํฌ : https://wikidocs.net/97519 ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ธฐ์กด์˜ ์–‘๋ฐฉํ–ฅ LSTM ๋ชจ๋ธ์— CRF(Conditional Random Field)๋ผ๋Š” ์ƒˆ๋กœ์šด ์ธต์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ณด๋‹ค ๋ชจ๋ธ์„ ๊ฐœ์„ ํ•œ ์–‘๋ฐฉํ–ฅ LSTM + CRF ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition)์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ ๋งํฌ : https://arxiv.org/pdf/1508.01991v1.pdf ๋…ผ๋ฌธ ๋งํฌ : https://arxiv.org/pdf/1603.01360.pdf ์ด๋ฒˆ ์ฑ•ํ„ฐ๋Š” ํ…์„œ ํ”Œ๋กœ์™€ ์ผ€๋ผ์Šค ๋ฒ„์ „์ด ๋‹ค๋ฅธ ์‹ค์Šต๊ณผ๋Š” ๋‹ค๋ฅธ ๋ฒ„์ „์—์„œ ๋™์ž‘ํ•˜๋ฏ€๋กœ ๊ตฌ๊ธ€์˜ Colab์„ ์‚ฌ์šฉํ•˜๊ธฐ๋ฅผ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. 1. CRF(Conditional Random Field) CRF๋Š” Conditional Random Field์˜ ์•ฝ์ž๋กœ ์–‘๋ฐฉํ–ฅ LSTM์„ ์œ„ํ•ด ํƒ„์ƒํ•œ ๋ชจ๋ธ์ด ์•„๋‹ˆ๋ผ ์ด์ „์— ๋…์ž์ ์œผ๋กœ ์กด์žฌํ•ด์™”๋˜ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์–‘๋ฐฉํ–ฅ LSTM ๋ชจ๋ธ ์œ„์— ํ•˜๋‚˜์˜ ์ธต์œผ๋กœ ์ถ”๊ฐ€ํ•˜์—ฌ, ์–‘๋ฐฉํ–ฅ LSTM + CRF ๋ชจ๋ธ์ด ํƒ„์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” CRF์˜ ์ˆ˜์‹์  ์ดํ•ด๊ฐ€ ์•„๋‹ˆ๋ผ ์–‘๋ฐฉํ–ฅ LSTM + CRF ๋ชจ๋ธ์˜ ์ง๊ด€์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. CRF ์ธต์˜ ์—ญํ• ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ„๋‹จํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹ ์ž‘์—…์˜ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ๋žŒ(Person), ์กฐ์ง(Organization) ๋‘ ๊ฐ€์ง€๋งŒ์„ ํƒœ๊น… ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํƒœ๊น… ์ž‘์—…์— BIO ํ‘œํ˜„์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•˜๋Š” ํƒœ๊น…์˜ ์ข…๋ฅ˜๋Š” ์•„๋ž˜์˜ 5๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. B-Per, I-Per, B-Org, I-Org, O ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์œ„์˜ ํƒœ๊น…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ธฐ์กด์˜ ์–‘๋ฐฉํ–ฅ LSTM ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ชจ๋ธ์˜ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ๋ชจ๋ธ์€ ๊ฐ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ๋กœ ์ž…๋ ฅ๋ฐ›๊ณ , ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์ธต์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐœ์ฒด๋ช…์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ž…๋ ฅ ๋‹จ์–ด๋“ค๊ณผ ์‹ค์ œ ๊ฐœ์ฒด๋ช…์ด ๋ฌด์—‡์ธ์ง€ ๋ชจ๋ฅด๋Š” ์ƒํ™ฉ์ด๋ฏ€๋กœ ์ด ๋ชจ๋ธ์ด ์ •ํ™•ํ•˜๊ฒŒ ๊ฐœ์ฒด๋ช…์„ ์˜ˆ์ธกํ–ˆ๋Š”์ง€๋Š” ์œ„ ๊ทธ๋ฆผ๋งŒ์œผ๋กœ๋Š” ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ๋ชจ๋ธ์€ ๋ช…ํ™•ํžˆ ํ‹€๋ฆฐ ์˜ˆ์ธก์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋‹จ์–ด๋“ค๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด ์ด ์‚ฌ์‹ค์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BIO ํ‘œํ˜„์— ๋”ฐ๋ฅด๋ฉด ์šฐ์„ , ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ๋ ˆ์ด๋ธ”์—์„œ I๊ฐ€ ๋“ฑ์žฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ I-Per์€ ๋ฐ˜๋“œ์‹œ B-Per ๋’ค์—์„œ๋งŒ ๋“ฑ์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, I-Org๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ B-Org ๋’ค์—์„œ๋งŒ ๋“ฑ์žฅํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์œ„ ๋ชจ๋ธ์€ ์ด๋Ÿฐ BIO ํ‘œํ˜„ ๋ฐฉ๋ฒ•์˜ ์ œ์•ฝ์‚ฌํ•ญ๋“ค์„ ๋ชจ๋‘ ์œ„๋ฐ˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์–‘๋ฐฉํ–ฅ LSTM ์œ„์— CRF ์ธต์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ ์„ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. CRF ์ธต์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ชจ๋ธ์€ ์˜ˆ์ธก ๊ฐœ์ฒด๋ช…, ๋‹ค์‹œ ๋งํ•ด ๋ ˆ์ด๋ธ” ์‚ฌ์ด์˜ ์˜์กด์„ฑ์„ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์–‘๋ฐฉํ–ฅ LSTM + CRF ๋ชจ๋ธ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์•ž์„œ๋ดค๋“ฏ์ด, ๊ธฐ์กด์— CRF ์ธต์ด ์กด์žฌํ•˜์ง€ ์•Š์•˜๋˜ ์–‘๋ฐฉํ–ฅ LSTM ๋ชจ๋ธ์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ์‹œ์ ์—์„œ ๊ฐœ์ฒด๋ช…์„ ๊ฒฐ์ •ํ–ˆ์ง€๋งŒ, CRF ์ธต์„ ์ถ”๊ฐ€ํ•œ ๋ชจ๋ธ์—์„œ๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋“ค์ด CRF ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด o d์— ๋Œ€ํ•œ BiLSTM ์…€๊ณผ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ์ถœ๋ ฅ๊ฐ’ [0.7, 0.12, 0.08, 0.04, 0.06]์€ CRF ์ธต์˜ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ์ถœ๋ ฅ๊ฐ’์€ CRF ์ธต์˜ ์ž…๋ ฅ์ด ๋˜๊ณ , CRF ์ธต์€ ๋ ˆ์ด๋ธ” ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์‹œํ€€์Šค๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ตฌ์กฐ์—์„œ CRF ์ธต์€ ์ ์ฐจ์ ์œผ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์•„๋ž˜์™€ ๊ฐ™์€ ์ œ์•ฝ์‚ฌํ•ญ ๋“ฑ์„ ํ•™์Šตํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์žฅ์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด์—์„œ๋Š” I๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. O-I ํŒจํ„ด์€ ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. B-I-I ํŒจํ„ด์—์„œ ๊ฐœ์ฒด๋ช…์€ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด B-Per ๋‹ค์Œ์— I-Org๋Š” ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด ์–‘๋ฐฉํ–ฅ LSTM์€ ์ž…๋ ฅ ๋‹จ์–ด์— ๋Œ€ํ•œ ์–‘๋ฐฉํ–ฅ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•˜๋ฉฐ, CRF๋Š” ์ถœ๋ ฅ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ์–‘๋ฐฉํ–ฅ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค. 2. ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— CRF layer๋Š” ํ˜„์žฌ ํ…์„œ ํ”Œ๋กœ 1.14.0๋ฒ„์ „๊ณผ ์ผ€๋ผ์Šค 2.2.4์—์„œ ๊ฐ€์žฅ ์›ํ™œํ•˜๊ฒŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ํ…์„œ ํ”Œ๋กœ์™€ ์ผ€๋ผ์Šค ๋ฒ„์ „์„ ๋†’์ด๋ฉด CRF layer๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, mask_zero=True๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๋“ฑ์˜ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์šฐ์„  ๋ฒ„์ „์„ ๋งž์ถฐ์ค์‹œ๋‹ค. ๋กœ์ปฌ ํ™˜๊ฒฝ์˜ ๋ฒ„์ „์€ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ตฌ๊ธ€ Colab์—์„œ์˜ ์‹ค์Šต์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. !pip install tensorflow==1.14.0 !pip install keras==2.2.4 !pip install tensorflow-gpu==1.14.0 CRF๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด keras_contrib๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. !pip install git+https://www.github.com/keras-team/keras-contrib.git 3. ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ๋ฐ์ดํ„ฐ ์…‹ ๋กœ๋“œ์™€ ์ „์ฒ˜๋ฆฌ๋Š” 5) ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹์—์„œ ์ง„ํ–‰ํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ง„ํ–‰๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://wikidocs.net/97519 print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_train.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_train.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_test.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_test.shape)) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (38367, 70) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (38367, 70, 18) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (9592, 70) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (9592, 70, 18) 4. F1-score๋ฅผ ์ธก์ •ํ•˜๋Š” ์ฝœ๋ฐฑ ํด๋ž˜์Šค ์•„๋ž˜ ๋งํฌ์—์„œ ์‚ฌ์šฉ๋œ ์ฝœ๋ฐฑ ํด๋ž˜์Šค๋ฅผ ๋™์ผํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์˜€๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://wikidocs.net/97519 class F1score(Callback): ... ์ค‘๋žต ... 5. ์–‘๋ฐฉํ–ฅ LSTM + CRF๋ฅผ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹ from keras.models import Sequential from keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional from keras.models import load_model from keras_contrib.layers import CRF from keras_contrib.losses import crf_loss from keras_contrib.metrics import crf_viterbi_accuracy ์œ„์™€ ๊ฐ™์ด ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. model = Sequential() model.add(Embedding(input_dim=vocab_size, output_dim=64, input_length=max_len, mask_zero=True)) model.add(Bidirectional(LSTM(128, return_sequences=True))) model.add(TimeDistributed(Dense(50, activation="relu"))) crf = CRF(tag_size) model.add(crf) ๋ชจ๋ธ์— ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•˜๊ณ , ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์ธต์— CRF ์ธต์„ ๋ฐฐ์น˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•ฉ๋‹ˆ๋‹ค. model.compile(optimizer="adam", loss=crf.loss_function, metrics=[crf.accuracy]) history = model.fit(X_train, y_train, batch_size = 32, epochs = 10, validation_split = 0.1, verbose = 1, callbacks=[F1score(use_char=False)]) bilstm_crf_model = load_model('best_model.h5', custom_objects={'CRF':CRF, 'crf_loss':crf_loss, 'crf_viterbi_accuracy':crf_viterbi_accuracy}) i=13 # ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ์ธ๋ฑ์Šค. y_predicted = bilstm_crf_model.predict(np.array([X_test[i]])) # ์ž…๋ ฅํ•œ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก y๋ฅผ ๋ฆฌํ„ด y_predicted = np.argmax(y_predicted, axis=-1) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ๋‹ค์‹œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝํ•จ. true = np.argmax(y_test[i], -1) # ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ๋‹ค์‹œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝํ•จ. print("{:15}|{:5}|{}".format("๋‹จ์–ด", "์‹ค์ œ ๊ฐ’", "์˜ˆ์ธก๊ฐ’")) print(35 * "-") for w, t, pred in zip(X_test[i], true, y_predicted[0]): if w != 0: # PAD ๊ฐ’์€ ์ œ์™ธํ•จ. print("{:17}: {:7} {}".format(index_to_word[w], index_to_ner[t], index_to_ner[pred])) ๋‹จ์–ด |์‹ค์ œ ๊ฐ’ |์˜ˆ์ธก๊ฐ’ ----------------------------------- the : O O statement : O O came : O O as : O O u.n. : B-org B-org secretary-general: I-org I-org kofi : B-per B-per annan : I-per I-per met : O O with : O O officials : O O in : O O amman : B-geo B-geo to : O O discuss : O O wednesday : B-tim B-tim 's : O O attacks : O O . : O O 6. ์‹ค์ œ ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ f1 score ๊ตฌํ•˜๊ธฐ ์ด์ œ ์•ž์„œ ๊ตฌํ˜„ํ•œ ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ์œ„์—์„œ ๋ฐฐ์šด f1-score๋ฅผ ์ ์šฉํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ๋ชจ๋ธ์ด ๋ฆฌํ„ดํ•˜๋Š” ์˜ˆ์ธก๊ฐ’์€ ์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์œผ๋ฏ€๋กœ, ์ด๋ฅผ ๋จผ์ € ํƒœ๊น…์ด ๋‚˜์—ด๋˜์–ด ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ์น˜ํ™˜ํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ sequences_to_tag ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. f1score = F1score(use_char=False) y_predicted = bilstm_crf_model.predict([X_test]) pred_tags = f1score.sequences_to_tags(y_predicted) test_tags = f1score.sequences_to_tags(y_test) print(classification_report(test_tags, pred_tags)) precision recall f1-score support art 0.00 0.00 0.00 63 eve 0.82 0.27 0.41 52 geo 0.82 0.87 0.85 7620 gpe 0.97 0.93 0.95 3145 nat 1.00 0.19 0.32 37 org 0.71 0.56 0.63 4033 per 0.79 0.75 0.77 3545 tim 0.89 0.85 0.87 4067 micro avg 0.83 0.80 0.81 22562 macro avg 0.75 0.55 0.60 22562 weighted avg 0.83 0.80 0.81 22562 print("F1-score: {:.1%}".format(f1_score(test_tags, pred_tags))) F1-score: 81.4% 7. ์ƒˆ๋กœ์šด ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธกํ•˜๊ธฐ ์ด์ œ ์ž„์˜๋กœ ๋งŒ๋“  ์ƒˆ๋กœ์šด ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์•ž์„œ ๋งŒ๋“  ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ชจ๋ธ์„ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. new_sentence='Mr. Heo said South Korea has become a worldwide leader'.lower().split() ์ €์ž๊ฐ€ ์ž„์˜๋กœ ๋งŒ๋“  ๋ฌธ์žฅ์„ ๋„์–ด์“ฐ๊ธฐ ์ˆ˜์ค€์˜ ํ† ํฐํ™” ์ƒํƒœ๋กœ new_sentence์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ฅผ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. new_encoded=[] for w in new_sentence: try: new_encoded.append(word_to_index.get(w, 1)) except KeyError: new_encoded.append(word_to_index['OOV']) # ๋ชจ๋ธ์ด ๋ชจ๋ฅด๋Š” ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋Š” 'OOV'์˜ ์ธ๋ฑ์Šค์ธ 1๋กœ ์ธ์ฝ”๋”ฉ print(new_encoded) [38, 1, 18, 117, 243, 12, 762, 8, 1154, 130] ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ Heo์˜ ๊ฒฝ์šฐ OOV๋กœ ์น˜ํ™˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค. new_padded = pad_sequences([new_encoded], padding="post", value=0, maxlen=max_len) ์ž„์˜๋กœ ๋งŒ๋“  ๋ฌธ์žฅ์„ max_len์˜ ๊ธธ์ด๋กœ ํŒจ๋”ฉ ํ•ด์ค๋‹ˆ๋‹ค. ์ด์ œ ์˜ˆ์ธก์„ ์‹œ์ž‘ํ•ด ๋ณผ๊นŒ์š”? p = bilstm_crf_model.predict(np.array([new_padded[0]])) p = np.argmax(p, axis=-1) print("{:15}||{}".format("๋‹จ์–ด", "์˜ˆ์ธก๊ฐ’")) print(30 * "=") for w, pred in zip(new_sentence, p[0]): print("{:15}: {:5}".format(w, index_to_ner[pred])) ๋‹จ์–ด ||์˜ˆ์ธก๊ฐ’ ============================== mr. : B-per heo : I-per said : O south : B-geo korea : I-geo has : O become : O a : O worldwide : O leader : O https://github.com/floydhub/named-entity-recognition-template/blob/master/ner.ipynb 12-03 ์–‘๋ฐฉํ–ฅ LSTM๊ณผ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ(Char embedding) ์ด๋ฒˆ ์ฑ•ํ„ฐ๋Š” ์•„๋ž˜์˜ ์ฑ•ํ„ฐ๋ฅผ ์ด๋ฏธ ์‹คํ–‰ํ•œ ์ƒํƒœ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์ฑ•ํ„ฐ ๋งํฌ : https://wikidocs.net/97519 ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ์˜ ์„ฑ๋Šฅ์„ ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ 12์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šด ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๊ณผ ํ•จ๊ป˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์— ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ์„ฑ๋Šฅ์„ ๋†’์—ฌ๋ด…์‹œ๋‹ค. 1. ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ(Char Embedding)์„ ์œ„ํ•œ ์ „์ฒ˜๋ฆฌ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์œ„ํ•ด์„œ ํ•˜๊ณ ์ž ํ•˜๋Š” ์ „์ฒ˜๋ฆฌ๋Š” ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น ๋‹จ์–ด 'book'์ด ์žˆ๊ณ , b๊ฐ€ 21๋ฒˆ o๊ฐ€ 7๋ฒˆ, k๊ฐ€ 11๋ฒˆ์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ๋‹จ์–ด 'book'์„ [21 7 7 11]๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‹จ์–ด 1๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ ๋‹จ์–ด๊ตฌ๋‚˜ ๋ฌธ์žฅ์ด๋ผ๋ฉด ์–ด๋–จ๊นŒ์š”? 'good book'์ด๋ž€ ๋ฌธ์žฅ์ด ์žˆ๊ณ , g๊ฐ€ 12๋ฒˆ, d๊ฐ€ 17๋ฒˆ์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ์ด ๋ฌธ์žฅ์„ ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 'good book์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ' [[12 7 7 17] [21 7 7 11]] ์ด ๊ฐ ์ •์ˆ˜๋ฅผ ๊ฐ๊ฐ ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)๋ฅผ ๊ฑฐ์น˜๋„๋ก ํ•˜์—ฌ, ๋ฌธ์ž ๋‹จ์œ„ ์ž„๋ฒ ๋”ฉ์„ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ์ธต์€ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•  ๋•Œ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, ์—ฌ๊ธฐ์„œ๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊นŒ์ง€๋งŒ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๋ฌธ์ž ๋ ˆ๋ฒจ๋กœ ๋ถ„ํ•ดํ•˜์—ฌ, ๋ฌธ์ž ์ง‘ํ•ฉ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. # char_vocab ๋งŒ๋“ค๊ธฐ words = list(set(data["Word"].values)) chars = set([w_i for w in words for w_i in w]) chars = sorted(list(chars)) print(chars) ['!', '"', '#', '$', '%', '&', "'", '(', ')', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '?', '@', '[', ']', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '~', '\x85', '\x91', '\x92', '\x93', '\x94', '\x96', '\x97', '\xa0', 'ยฐ', 'รฉ', 'รซ', 'รถ', 'รผ'] ์ด๋ ‡๊ฒŒ ์–ป์€ ๋ฌธ์ž ์ง‘ํ•ฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋”•์…”๋„ˆ๋ฆฌ์ธ char_to_index์™€ ๋ฐ˜๋Œ€๋กœ ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋”•์…”๋„ˆ๋ฆฌ์ธ index_to_char๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. char_to_index = {c: i + 2 for i, c in enumerate(chars)} char_to_index["OOV"] = 1 char_to_index["PAD"] = 0 index_to_char = {} for key, value in char_to_index.items(): index_to_char[value] = key max_len_char = 15 def padding_char_indice(char_indice, max_len_char): return pad_sequences( char_indice, maxlen=max_len_char, padding='post', value = 0) def integer_coding(sentences): char_data = [] for ts in sentences: word_indice = [word_to_index[t] for t in ts] char_indice = [[char_to_index[char] for char in t] for t in ts] char_indice = padding_char_indice(char_indice, max_len_char) for chars_of_token in char_indice: if len(chars_of_token) > max_len_char: continue char_data.append(char_indice) return char_data X_char_data = integer_coding(sentences) ๋™์ผํ•œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์˜ ์ฐจ์ด๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ด์ „์˜ ๊ธฐ์กด ๋ฌธ์žฅ print(sentences[0]) ['thousands', 'of', 'demonstrators', 'have', 'marched', 'through', 'london', 'to', 'protest', 'the', 'war', 'in', 'iraq', 'and', 'demand', 'the', 'withdrawal', 'of', 'british', 'troops', 'from', 'that', 'country', '.'] ์ด ์ƒ˜ํ”Œ์„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•˜๊ณ , ๋‹ค๋ฅธ ์ƒ˜ํ”Œ๋“ค๊ณผ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ํŒจ๋”ฉ ํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ๋‹จ์–ด ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ + ํŒจ๋”ฉ print(X_data[0]) [ 254 6 967 16 1795 238 468 7 523 2 129 5 61 9 571 2 833 6 186 90 22 15 56 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 254๋ฒˆ ๋‹จ์–ด๋Š” thousands, 6๋ฒˆ ๋‹จ์–ด๋Š” of์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ƒ˜ํ”Œ์„ ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ print(X_char_data[0]) [[53 41 48 54 52 34 47 37 52 0 0 0 0 0 0] [48 39 0 0 0 0 0 0 0 0 0 0 0 0 0] [37 38 46 48 47 52 53 51 34 53 48 51 52 0 0] [41 34 55 38 0 0 0 0 0 0 0 0 0 0 0] [46 34 51 36 41 38 37 0 0 0 0 0 0 0 0] [53 41 51 48 54 40 41 0 0 0 0 0 0 0 0] [45 48 47 37 48 47 0 0 0 0 0 0 0 0 0] [53 48 0 0 0 0 0 0 0 0 0 0 0 0 0] [49 51 48 53 38 52 53 0 0 0 0 0 0 0 0] [53 41 38 0 0 0 0 0 0 0 0 0 0 0 0] [56 34 51 0 0 0 0 0 0 0 0 0 0 0 0] [42 47 0 0 0 0 0 0 0 0 0 0 0 0 0] [42 51 34 50 0 0 0 0 0 0 0 0 0 0 0] [34 47 37 0 0 0 0 0 0 0 0 0 0 0 0] [37 38 46 34 47 37 0 0 0 0 0 0 0 0 0] [53 41 38 0 0 0 0 0 0 0 0 0 0 0 0] [56 42 53 41 37 51 34 56 34 45 0 0 0 0 0] [48 39 0 0 0 0 0 0 0 0 0 0 0 0 0] [35 51 42 53 42 52 41 0 0 0 0 0 0 0 0] [53 51 48 48 49 52 0 0 0 0 0 0 0 0 0] [39 51 48 46 0 0 0 0 0 0 0 0 0 0 0] [53 41 34 53 0 0 0 0 0 0 0 0 0 0 0] [36 48 54 47 53 51 58 0 0 0 0 0 0 0 0] [14 0 0 0 0 0 0 0 0 0 0 0 0 0 0]] ์œ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ ๊ฐ ํ–‰์€ ๊ฐ ๋‹จ์–ด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, thousands๋Š” ์ฒซ ๋ฒˆ์งธ ํ–‰ [53 41 48 54 52 34 47 37 52 0 0 0 0 0 0]์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ 15(max_len_char)๋กœ ์ œํ•œํ•˜์˜€์œผ๋ฏ€๋กœ, ๊ธธ์ด๊ฐ€ 15๋ณด๋‹ค ์งง์€ ๋‹จ์–ด๋Š” ๋’ค์— 0์œผ๋กœ ํŒจ๋”ฉ ๋ฉ๋‹ˆ๋‹ค. 53์€ t, 41์€ h, 48์€ o, 54๋Š” u์— ๊ฐ๊ฐ ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. X_data๋Š” ๋’ค์— 0์œผ๋กœ ํŒจ๋”ฉ ๋˜์–ด ๊ธธ์ด๊ฐ€ 70์ธ ๊ฒƒ์— ๋น„ํ•ด, X_char_data๋Š” 0๋ฒˆ ๋‹จ์–ด๋Š” ๋ฌด์‹œ๋˜์–ด ๊ธธ์ด๊ฐ€ 70์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ฆ‰, ์œ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ ํ–‰์˜ ๊ฐœ์ˆ˜๊ฐ€ 70์ด ์•„๋‹Œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋ฌธ์žฅ ๊ธธ์ด ๋ฐฉํ–ฅ์œผ๋กœ๋„ ํŒจ๋”ฉ์„ ํ•ด์ค๋‹ˆ๋‹ค. X_char_data = pad_sequences(X_char_data, maxlen=max_len, padding='post', value = 0) ์ด๋ฏธ ๋‹จ์–ด ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋Š” X_train, y_train, X_test, y_test๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ„๋ฆฌ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ X_char_train, X_char_test๋กœ ๋‚˜๋ˆ„์–ด์ค๋‹ˆ๋‹ค. X_char_train, X_char_test, _, _ = train_test_split(X_char_data, y_data, test_size=.2, random_state=777) X_char_train = np.array(X_char_train) X_char_test = np.array(X_char_test) print(X_train[0]) [ 150 928 361 17 2624 9 4131 3567 9 8 2893 1250 880 107 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] print(index_to_word[150]) soldiers print(' '.join([index_to_char[index] for index in X_char_train[0][0]])) s o l d i e r s PAD PAD PAD PAD PAD PAD PAD print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_train.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_train.shape)) print('ํ›ˆ๋ จ ์ƒ˜ํ”Œ char ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : {}'.format(X_char_train.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : {}'.format(X_test.shape)) print('ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(y_test.shape)) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (38367, 70) ํ›ˆ๋ จ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (38367, 70, 18) ํ›ˆ๋ จ ์ƒ˜ํ”Œ char ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (38367, 70, 15) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ฌธ์žฅ์˜ ํฌ๊ธฐ : (9592, 70) ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (9592, 70, 18) 2. BiLSTM-CNN ์œ„์—์„œ ์ „์ฒ˜๋ฆฌํ•œ ๋ฌธ์ž ๋‹จ์œ„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ž…๋ ฅ์„ 1D CNN์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์–ป๊ณ , ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๊ณผ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ์–‘๋ฐฉํ–ฅ LSTM์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. from keras.layers import Embedding, TimeDistributed, Dropout, concatenate, Bidirectional, LSTM, Conv1D, Dense, MaxPooling1D, Flatten from keras import Input, Model from keras.initializers import RandomUniform from keras.models import load_model # ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ word_ids = Input(shape=(None,),dtype='int32',name='words_input') word_embeddings = Embedding(input_dim = vocab_size, output_dim = 64)(word_ids) # char ์ž„๋ฒ ๋”ฉ char_ids = Input(shape=(None, max_len_char,),name='char_input') embed_char_out = TimeDistributed(Embedding(len(char_to_index), 30, embeddings_initializer=RandomUniform(minval=-0.5, maxval=0.5)), name='char_embedding')(char_ids) dropout = Dropout(0.5)(embed_char_out) # char ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•ด์„œ๋Š” Conv1D ์ˆ˜ํ–‰ conv1d_out= TimeDistributed(Conv1D(kernel_size=3, filters=30, padding='same',activation='tanh', strides=1))(dropout) maxpool_out=TimeDistributed(MaxPooling1D(max_len_char))(conv1d_out) char_embeddings = TimeDistributed(Flatten())(maxpool_out) char_embeddings = Dropout(0.5)(char_embeddings) # char ์ž„๋ฒ ๋”ฉ์„ Conv1D ์ˆ˜ํ–‰ํ•œ ๋’ค์— ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๊ณผ ์—ฐ๊ฒฐ output = concatenate([word_embeddings, char_embeddings]) # ์—ฐ๊ฒฐํ•œ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฌธ์žฅ์˜ ๊ธธ์ด๋งŒํผ LSTM์„ ์ˆ˜ํ–‰ output = Bidirectional(LSTM(50, return_sequences=True, dropout=0.50, recurrent_dropout=0.25))(output) # ์ถœ๋ ฅ์ธต output = TimeDistributed(Dense(tag_size, activation='softmax'))(output) model = Model(inputs=[word_ids, char_ids], outputs=[output]) model.compile(loss='categorical_crossentropy', optimizer='nadam', metrics=['acc']) callbacks์œผ๋กœ F1score๋ฅผ ๋„ฃ์–ด์ค„ ๋•Œ, ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ use_char๋ฅผ True๋กœ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. history = model.fit([X_train, X_char_train], y_train, batch_size = 32, epochs = 10, validation_split = 0.1, verbose = 1, callbacks=[F1score(use_char=True)]) ํ•™์Šต์ด ๋๋‚ฌ๋‹ค๋ฉด ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜์—ฌ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ F1 score๋ฅผ ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. bilstm_cnn_model = load_model('best_model.h5') f1score = F1score(use_char=True) y_predicted = bilstm_cnn_model.predict([X_test, X_char_test]) pred_tags = f1score.sequences_to_tags(y_predicted) test_tags = f1score.sequences_to_tags(y_test) print(classification_report(test_tags, pred_tags)) precision recall f1-score support art 0.27 0.14 0.19 63 eve 0.46 0.35 0.40 52 geo 0.82 0.86 0.84 7620 gpe 0.96 0.95 0.95 3145 nat 0.55 0.46 0.50 37 org 0.62 0.59 0.60 4033 per 0.73 0.71 0.72 3545 tim 0.87 0.84 0.86 4067 micro avg 0.80 0.79 0.80 22562 macro avg 0.66 0.61 0.63 22562 weighted avg 0.80 0.79 0.79 22562 print("F1-score: {:.1%}".format(f1_score(test_tags, pred_tags))) F1-score: 79.5% 3. BiLSTM-CNN-CRF ์ด๋ฒˆ์—๋Š” ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•œ ์œ„์˜ ๋ชจ๋ธ์— CRF ์ธต์„ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from keras_contrib.layers import CRF from keras_contrib.losses import crf_loss from keras_contrib.metrics import crf_viterbi_accuracy # ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ word_ids = Input(shape=(None,),dtype='int32',name='words_input') word_embeddings = Embedding(input_dim = vocab_size, output_dim = 64)(word_ids) # char ์ž„๋ฒ ๋”ฉ char_ids = Input(shape=(None, max_len_char,),name='char_input') embed_char_out = TimeDistributed(Embedding(len(char_to_index), 30, embeddings_initializer=RandomUniform(minval=-0.5, maxval=0.5)), name='char_embedding')(char_ids) dropout = Dropout(0.5)(embed_char_out) # char ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•ด์„œ๋Š” Conv1D ์ˆ˜ํ–‰ conv1d_out= TimeDistributed(Conv1D(kernel_size=3, filters=30, padding='same',activation='tanh', strides=1))(dropout) maxpool_out=TimeDistributed(MaxPooling1D(max_len_char))(conv1d_out) char_embeddings = TimeDistributed(Flatten())(maxpool_out) char_embeddings = Dropout(0.5)(char_embeddings) # char ์ž„๋ฒ ๋”ฉ์„ Conv1D ์ˆ˜ํ–‰ํ•œ ๋’ค์— ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๊ณผ ์—ฐ๊ฒฐ output = concatenate([word_embeddings, char_embeddings]) # ์—ฐ๊ฒฐํ•œ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฌธ์žฅ์˜ ๊ธธ์ด๋งŒํผ LSTM์„ ์ˆ˜ํ–‰ output = Bidirectional(LSTM(50, return_sequences=True, dropout=0.50, recurrent_dropout=0.25))(output) # ์ถœ๋ ฅ์ธต์— CRF ์ธต์„ ์ถ”๊ฐ€ (์œ„์˜ ๋ชจ๋ธ๊ณผ ์ด ๋ถ€๋ถ„์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.) output = TimeDistributed(Dense(50, activation='relu'))(output) crf = CRF(tag_size) output = crf(output) model = Model(inputs=[words_input, character_input], outputs=[output]) model.compile(optimizer="adam", loss=crf.loss_function, metrics=[crf.accuracy]) history = model.fit([X_train, X_char_train], y_train, batch_size = 32, epochs = 15, validation_split = 0.1, verbose = 1, callbacks=[F1score(use_char=True)]) bilstm_cnn_crf_model = load_model('best_model.h5', custom_objects={'CRF':CRF, 'crf_loss':crf_loss, 'crf_viterbi_accuracy':crf_viterbi_accuracy}) f1score = F1score(use_char=True) y_predicted = bilstm_cnn_crf_model.predict([X_test, X_char_test]) pred_tags = f1score.sequences_to_tags(y_predicted) test_tags = f1score.sequences_to_tags(y_test) print(classification_report(test_tags, pred_tags)) precision recall f1-score support per 0.80 0.76 0.78 3545 gpe 0.96 0.94 0.95 3145 geo 0.85 0.86 0.86 7620 tim 0.88 0.87 0.87 4067 org 0.66 0.60 0.63 4033 art 0.00 0.00 0.00 63 eve 0.61 0.27 0.37 52 nat 0.68 0.51 0.58 37 micro avg 0.83 0.81 0.82 22562 macro avg 0.83 0.81 0.82 22562 print("F1-score: {:.1%}".format(f1_score(test_tags, pred_tags))) F1-score: 81.9% 4. BiLSTM-BiLSTM-CRF ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์€ 1D CNN์ด ์•„๋‹ˆ๋ผ ์–‘๋ฐฉํ–ฅ LSTM์„ ํ†ตํ•ด์„œ๋„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•ด ์–ป์€ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๊ณผ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ์–‘๋ฐฉํ–ฅ LSTM์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , ๋งˆ์ง€๋ง‰์œผ๋กœ CRF ์ธต์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ชจ๋ธ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. # ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ word_ids = Input(batch_shape=(None, None), dtype='int32', name='word_input') word_embeddings = Embedding(input_dim=vocab_size, output_dim=64, mask_zero=True, name='word_embedding')(word_ids) # char ์ž„๋ฒ ๋”ฉ char_ids = Input(batch_shape=(None, None, None), dtype='int32', name='char_input') char_embeddings = Embedding(input_dim=(len(char_to_index)), output_dim=30, mask_zero=True, embeddings_initializer=RandomUniform(minval=-0.5, maxval=0.5), name='char_embedding')(char_ids) char_embeddings = TimeDistributed(Bidirectional(LSTM(64)))(char_embeddings) # char ์ž„๋ฒ ๋”ฉ์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๊ณผ ์—ฐ๊ฒฐ word_embeddings = concatenate([word_embeddings, char_embeddings]) word_embeddings = Dropout(0.3)(word_embeddings) z = Bidirectional(LSTM(units=64, return_sequences=True))(word_embeddings) z = Dense(tag_size, activation='tanh')(z) crf = CRF(tag_size) output = crf(z) model = Model(inputs=[word_ids, char_ids], outputs=[output]) model.compile(optimizer="adam", loss=crf.loss_function, metrics=[crf.accuracy]) history = model.fit([X_train, X_char_train], y_train, batch_size = 32, epochs = 15, validation_split = 0.1, verbose = 1, callbacks=[F1score(use_char=True)]) bilstm_bilstm_crf_model = load_model('best_model.h5', custom_objects={'CRF':CRF, 'crf_loss':crf_loss, 'crf_viterbi_accuracy':crf_viterbi_accuracy}) f1score = F1score(use_char=True) y_predicted = bilstm_bilstm_crf_model.predict([X_test, X_char_test]) pred_tags = f1score.sequences_to_tags(y_predicted) test_tags = f1score.sequences_to_tags(y_test) print(classification_report(test_tags, pred_tags)) precision recall f1-score support art 0.25 0.02 0.03 63 eve 0.45 0.29 0.35 52 geo 0.85 0.87 0.86 7620 gpe 0.96 0.95 0.95 3145 nat 0.55 0.16 0.25 37 org 0.73 0.57 0.64 4033 per 0.78 0.78 0.78 3545 tim 0.89 0.86 0.87 4067 micro avg 0.84 0.81 0.82 22562 macro avg 0.68 0.56 0.59 22562 weighted avg 0.84 0.81 0.82 22562 print("F1-score: {:.1%}".format(f1_score(test_tags, pred_tags))) F1-score: 82.4% 13. Part 2. ์‹ฌํ™” ๊ณผ์ • ๋‚œ์ด๋„๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์ฑ…์„ ๋‘ ํŒŒํŠธ๋กœ ๋‚˜๋ˆด์Šต๋‹ˆ๋‹ค. 1์ฑ•ํ„ฐ๋ถ€ํ„ฐ 13์ฑ•ํ„ฐ๋ฅผ ๊ธฐ๋ณธ ๊ณผ์ •, 14์ฑ•ํ„ฐ๋ถ€ํ„ฐ ๋๊นŒ์ง€๋ฅผ ์‹ฌํ™” ๊ณผ์ •์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. 13. ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €(Subword Tokenizer) ๊ธฐ๊ณ„์—๊ฒŒ ์•„๋ฌด๋ฆฌ ๋งŽ์€ ๋‹จ์–ด๋ฅผ ํ•™์Šต์‹œ์ผœ๋„, ์„ธ์ƒ์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์•Œ๋ ค์ค„ ์ˆ˜๋Š” ์—†๋Š” ๋…ธ๋ฆ‡์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๊ธฐ๊ณ„๊ฐ€ ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด ๊ทธ ๋‹จ์–ด๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๋ž€ ์˜๋ฏธ์—์„œ OOV(Out-Of-Vocabulary) ๋˜๋Š” UNK(Unknown Token)๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ, ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด (์‚ฌ๋žŒ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ง€๋งŒ) ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ๊นŒ๋‹ค๋กœ์›Œ์ง‘๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๋กœ ์ธํ•ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ๊นŒ๋‹ค๋กœ์›Œ์ง€๋Š” ์ƒํ™ฉ์„ OOV ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ(Subword segmenation) ์ž‘์—…์€ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋Š” ๋” ์ž‘์€ ๋‹จ์œ„์˜ ์˜๋ฏธ ์žˆ๋Š” ์—ฌ๋Ÿฌ ์„œ๋ธŒ ์›Œ๋“œ๋“ค(Ex) birthplace = birth + place)์˜ ์กฐํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—, ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ ์—ฌ๋Ÿฌ ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ๋‹จ์–ด๋ฅผ ์ธ์ฝ”๋”ฉ ๋ฐ ์ž„๋ฒ ๋”ฉํ•˜๊ฒ ๋‹ค๋Š” ์˜๋„๋ฅผ ๊ฐ€์ง„ ์ „์ฒ˜๋ฆฌ ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด OOV๋‚˜ ํฌ๊ท€ ๋‹จ์–ด, ์‹ ์กฐ์–ด์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ์™„ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์–ธ์–ด์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ ์˜์–ด๊ถŒ ์–ธ์–ด๋‚˜ ํ•œ๊ตญ์–ด๋Š” ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ๋ฅผ ์‹œ๋„ํ–ˆ์„ ๋•Œ ์–ด๋Š ์ •๋„ ์˜๋ฏธ ์žˆ๋Š” ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์ด๋Ÿฐ ์ž‘์—…์„ ํ•˜๋Š” ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €์˜ ์ฃผ์š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๋ฐ”์ดํŠธ ํŽ˜์–ด ์ธ์ฝ”๋”ฉ๊ณผ ์‹ค์ œ ์‹ค๋ฌด์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ € ๊ตฌํ˜„์ฒด์ธ SentencePiece์™€ Huggingface์˜ Tokenizers์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 13-01 ๋ฐ”์ดํŠธ ํŽ˜์–ด ์ธ์ฝ”๋”ฉ(Byte Pair Encoding, BPE) ๊ธฐ๊ณ„์—๊ฒŒ ์•„๋ฌด๋ฆฌ ๋งŽ์€ ๋‹จ์–ด๋ฅผ ํ•™์Šต์‹œ์ผœ๋„ ์„ธ์ƒ์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์•Œ๋ ค์ค„ ์ˆ˜๋Š” ์—†๋Š” ๋…ธ๋ฆ‡์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ธฐ๊ณ„๊ฐ€ ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด ๊ทธ ๋‹จ์–ด๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๋ž€ ์˜๋ฏธ์—์„œ ํ•ด๋‹น ํ† ํฐ์„ UNK(Unknown Token)๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด (์‚ฌ๋žŒ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ง€๋งŒ) ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ๊นŒ๋‹ค๋กœ์›Œ์ง‘๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๋กœ ์ธํ•ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ๊นŒ๋‹ค๋กœ์›Œ์ง€๋Š” ์ƒํ™ฉ์„ OOV(Out-Of-Vocabulary) ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ(Subword segmenation) ์ž‘์—…์€ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋Š” ๋” ์ž‘์€ ๋‹จ์œ„์˜ ์˜๋ฏธ ์žˆ๋Š” ์—ฌ๋Ÿฌ ์„œ๋ธŒ ์›Œ๋“œ๋“ค(Ex) birthplace = birth + place)์˜ ์กฐํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—, ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ ์—ฌ๋Ÿฌ ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ๋‹จ์–ด๋ฅผ ์ธ์ฝ”๋”ฉ ๋ฐ ์ž„๋ฒ ๋”ฉํ•˜๊ฒ ๋‹ค๋Š” ์˜๋„๋ฅผ ๊ฐ€์ง„ ์ „์ฒ˜๋ฆฌ ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด OOV๋‚˜ ํฌ๊ท€ ๋‹จ์–ด, ์‹ ์กฐ์–ด์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ์™„ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์–ธ์–ด์˜ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ์˜์–ด๋‚˜ ํ•œ๊ตญ์–ด๋Š” ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ๋ฅผ ์‹œ๋„ํ–ˆ์„ ๋•Œ ์–ด๋Š ์ •๋„ ์˜๋ฏธ ์žˆ๋Š” ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์ด๋Ÿฐ ์ž‘์—…์„ ํ•˜๋Š” ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €๋ผ๊ณ  ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” OOV(Out-Of-Vocabulary) ๋ฌธ์ œ๋ฅผ ์™„ํ™”ํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ BPE(Byte Pair Encoding) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 1. BPE(Byte Pair Encoding) BPE(Byte pair encoding) ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ 1994๋…„์— ์ œ์•ˆ๋œ ๋ฐ์ดํ„ฐ ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ›„์— ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์‘์šฉ๋˜์—ˆ๋Š”๋ฐ ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์— ์–ธ๊ธ‰ํ•˜๋„๋ก ํ•˜๊ณ , ์šฐ์„  ๊ธฐ์กด์˜ BPE์˜ ์ž‘๋™ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ BPE์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. aaabdaaabac BPE์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์—ฐ์†์ ์œผ๋กœ ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•œ ๊ธ€์ž์˜ ์Œ์„ ์ฐพ์•„์„œ ํ•˜๋‚˜์˜ ๊ธ€์ž๋กœ ๋ณ‘ํ•ฉํ•˜๋Š” ๋ฐฉ์‹์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํƒœ์ƒ์ด ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๋งŒํผ, ์—ฌ๊ธฐ์„œ๋Š” ๊ธ€์ž ๋Œ€์‹  ๋ฐ”์ดํŠธ(byte)๋ผ๋Š” ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ์ž์ฃผ ๋“ฑ์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฐ”์ดํŠธ์˜ ์Œ(byte pair)์€ 'aa'์ž…๋‹ˆ๋‹ค. ์ด 'aa'๋ผ๋Š” ๋ฐ”์ดํŠธ์˜ ์Œ์„ ํ•˜๋‚˜์˜ ๋ฐ”์ดํŠธ์ธ 'Z'๋กœ ์น˜ํ™˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ZabdZabac Z=aa ์œ„ ๋ฌธ์ž์—ด ์ค‘์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฐ”์ดํŠธ์˜ ์Œ์€ 'ab'์ž…๋‹ˆ๋‹ค. ์ด 'ab'๋ฅผ 'Y'๋กœ ์น˜ํ™˜ํ•ด ๋ด…์‹œ๋‹ค. ZYdZYac Y=ab Z=aa ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฐ”์ดํŠธ์˜ ์Œ์€ 'ZY'์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ 'X'๋กœ ์น˜ํ™˜ํ•ด ๋ด…์‹œ๋‹ค. XdXac X=ZY Y=ab Z=aa ๋” ์ด์ƒ ๋ณ‘ํ•ฉํ•  ๋ฐ”์ดํŠธ์˜ ์Œ์€ ์—†์œผ๋ฏ€๋กœ BPE๋Š” ์œ„์˜ ๊ฒฐ๊ณผ๋ฅผ ์ตœ์ข… ๊ฒฐ๊ณผ๋กœ ํ•˜์—ฌ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. 2. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ์˜ BPE(Byte Pair Encoding) ๋…ผ๋ฌธ : https://arxiv.org/pdf/1508.07909.pdf ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ์˜ BPE๋Š” ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ(subword segmentation) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด์— ์žˆ๋˜ ๋‹จ์–ด๋ฅผ ๋ถ„๋ฆฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. BPE์„ ์š”์•ฝํ•˜๋ฉด, ๊ธ€์ž(charcter) ๋‹จ์œ„์—์„œ ์ ์ฐจ์ ์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ๋งŒ๋“ค์–ด ๋‚ด๋Š” Bottom up ๋ฐฉ์‹์˜ ์ ‘๊ทผ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ๋ชจ๋“  ๊ธ€์ž(chracters) ๋˜๋Š” ์œ ๋‹ˆ์ฝ”๋“œ(unicode) ๋‹จ์œ„๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)๋ฅผ ๋งŒ๋“ค๊ณ , ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•˜๋Š” ์œ ๋‹ˆ๊ทธ๋žจ์„ ํ•˜๋‚˜์˜ ์œ ๋‹ˆ๊ทธ๋žจ์œผ๋กœ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. BPE์„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ œ์•ˆํ•œ ๋…ผ๋ฌธ(Sennrich et al. (2016))์—์„œ ์ด๋ฏธ BPE์˜ ์ฝ”๋“œ๋ฅผ ๊ณต๊ฐœํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ”๋กœ ํŒŒ์ด์ฌ ์‹ค์Šต์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— ์œก์•ˆ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ๊ธฐ์กด์˜ ์ ‘๊ทผ ์–ด๋–ค ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๊ฐ ๋‹จ์–ด๋“ค์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์นด์šดํŠธํ–ˆ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋‹จ์–ด์™€ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ธฐ๋ก๋ผ ์žˆ๋Š” ํ•ด๋‹น ๊ฒฐ๊ณผ๋Š” ์ž„์˜๋กœ ๋”•์…”๋„ˆ๋ฆฌ(dictionary)๋ž€ ์ด๋ฆ„์„ ๋ถ™์˜€์Šต๋‹ˆ๋‹ค. # dictionary # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋‹จ์–ด์™€ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ low : 5, lower : 2, newest : 6, widest : 3 ์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—๋Š” 'low'๋ž€ ๋‹จ์–ด๊ฐ€ 5ํšŒ ๋“ฑ์žฅํ•˜์˜€๊ณ , 'lower'๋ž€ ๋‹จ์–ด๋Š” 2ํšŒ ๋“ฑ์žฅํ•˜์˜€์œผ๋ฉฐ, 'newest'๋ž€ ๋‹จ์–ด๋Š” 6ํšŒ, 'widest'๋ž€ ๋‹จ์–ด๋Š” 3ํšŒ ๋“ฑ์žฅํ•˜์˜€๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋”•์…”๋„ˆ๋ฆฌ๋กœ๋ถ€ํ„ฐ ์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ์–ป๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. # vocabulary low, lower, newest, widest ๋‹จ์–ด ์ง‘ํ•ฉ์€ ์ค‘๋ณต์„ ๋ฐฐ์ œํ•œ ๋‹จ์–ด๋“ค์˜ ์ง‘ํ•ฉ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ ๊ธฐ์กด์— ๋ฐฐ์šด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ์ •์˜๋ผ๋ฉด, ์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์—๋Š” 'low', 'lower', 'newest', 'widest'๋ผ๋Š” 4๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฒฝ์šฐ ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ 'lowest'๋ž€ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค๋ฉด ๊ธฐ๊ณ„๋Š” ์ด ๋‹จ์–ด๋ฅผ ํ•™์Šตํ•œ ์ ์ด ์—†์œผ๋ฏ€๋กœ ํ•ด๋‹น ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ œ๋Œ€๋กœ ๋Œ€์‘ํ•˜์ง€ ๋ชปํ•˜๋Š” OOV ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด BPE๋ฅผ ์ ์šฉํ•œ๋‹ค๋ฉด ์–ด๋–จ๊นŒ์š”? 2) BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ์œ„์˜ ๋”•์…”๋„ˆ๋ฆฌ์— BPE๋ฅผ ์ ์šฉํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ๋‹จ์–ด๋“ค์„ ๊ธ€์ž(chracter) ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ž์‹  ๋˜ํ•œ ์—…๋ฐ์ดํŠธ๋˜๋ฉฐ ์•ž์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด ์ง€์†์ ์œผ๋กœ ์ฐธ๊ณ ๋˜๋Š” ์ฐธ๊ณ  ์ž๋ฃŒ์˜ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. # dictionary l o w : 5, l o w e r : 2, n e w e s t : 6, w i d e s t : 3 ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ฐธ๊ณ ๋กœ ํ•œ ์ดˆ๊ธฐ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ๋งํ•ด ์ดˆ๊ธฐ ๊ตฌ์„ฑ์€ ๊ธ€์ž ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. # vocabulary l, o, w, e, r, n, s, t, i, d BPE์˜ ํŠน์ง•์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋™์ž‘์„ ๋ช‡ ํšŒ ๋ฐ˜๋ณต(iteration) ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ด 10ํšŒ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ฐ€์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์Œ์„ ํ•˜๋‚˜์˜ ์œ ๋‹ˆ๊ทธ๋žจ์œผ๋กœ ํ†ตํ•ฉํ•˜๋Š” ๊ณผ์ •์„ ์ด 10ํšŒ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๋”•์…”๋„ˆ๋ฆฌ์— ๋”ฐ๋ฅด๋ฉด ๋นˆ๋„์ˆ˜๊ฐ€ ํ˜„์žฌ ๊ฐ€์žฅ ๋†’์€ ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์Œ์€ (e, s)์ž…๋‹ˆ๋‹ค. 1ํšŒ - ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ฐธ๊ณ ๋กœ ํ•˜์˜€์„ ๋•Œ ๋นˆ๋„์ˆ˜๊ฐ€ 9๋กœ ๊ฐ€์žฅ ๋†’์€ (e, s)์˜ ์Œ์„ es๋กœ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. # dictionary update! l o w : 5, l o w e r : 2, n e w es t : 6, w i d es t : 3 # vocabulary update! l, o, w, e, r, n, s, t, i, d, es 2ํšŒ - ๋นˆ๋„์ˆ˜๊ฐ€ 9๋กœ ๊ฐ€์žฅ ๋†’์€ (es, t)์˜ ์Œ์„ est๋กœ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. # dictionary update! l o w : 5, l o w e r : 2, n e w est : 6, w i d est : 3 # vocabulary update! l, o, w, e, r, n, s, t, i, d, es, est 3ํšŒ - ๋นˆ๋„์ˆ˜๊ฐ€ 7๋กœ ๊ฐ€์žฅ ๋†’์€ (l, o)์˜ ์Œ์„ lo๋กœ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. # dictionary update! lo w : 5, lo w e r : 2, n e w est : 6, w i d est : 3 # vocabulary update! l, o, w, e, r, n, s, t, i, d, es, est, lo ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ด 10ํšŒ ๋ฐ˜๋ณตํ•˜์˜€์„ ๋•Œ ์–ป์€ ๋”•์…”๋„ˆ๋ฆฌ์™€ ๋‹จ์–ด ์ง‘ํ•ฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. # dictionary update! low : 5, low e r : 2, newest : 6, widest : 3 # vocabulary update! l, o, w, e, r, n, s, t, i, d, es, est, lo, low, ne, new, newest, wi, wid, widest ์ด ๊ฒฝ์šฐ ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ 'lowest'๋ž€ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค๋ฉด, ๊ธฐ์กด์—๋Š” OOV์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด๊ฐ€ ๋˜์—ˆ๊ฒ ์ง€๋งŒ BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ ์œ„์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ๋Š” ๋” ์ด์ƒ 'lowest'๋Š” OOV๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ์šฐ์„  'lowest'๋ฅผ ์ „๋ถ€ ๊ธ€์ž ๋‹จ์œ„๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, 'l, o, w, e, s, t'๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ธฐ๊ณ„๋Š” ์œ„์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ฐธ๊ณ ๋กœ ํ•˜์—ฌ 'low'์™€ 'est'๋ฅผ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. ์ฆ‰, 'lowest'๋ฅผ ๊ธฐ๊ณ„๋Š” 'low'์™€ 'est' ๋‘ ๋‹จ์–ด๋กœ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‘ ๋‹จ์–ด๋Š” ๋‘˜ ๋‹ค ๋‹จ์–ด ์ง‘ํ•ฉ์— ์žˆ๋Š” ๋‹จ์–ด์ด๋ฏ€๋กœ OOV๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์ด ๋™์ž‘ ๊ณผ์ •์„ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 3) ์ฝ”๋“œ ์‹ค์Šตํ•˜๊ธฐ ์•„๋ž˜ ์ฝ”๋“œ๋Š” ์› ๋…ผ๋ฌธ์—์„œ ๊ณต๊ฐœํ•œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ๋กœ ํ•˜์—ฌ ์ˆ˜์ •ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import re, collections from IPython.display import display, Markdown, Latex BPE์„ ๋ช‡ ํšŒ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 10ํšŒ๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. num_merges = 10 BPE์— ์‚ฌ์šฉํ•  ๋‹จ์–ด๊ฐ€ low, lower, newest, widest ์ผ ๋•Œ, BPE์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์‹ค์ œ ๋‹จ์–ด ์ง‘ํ•ฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. </w>๋Š” ๋‹จ์–ด์˜ ๋งจ ๋์— ๋ถ™์ด๋Š” ํŠน์ˆ˜ ๋ฌธ์ž์ด๋ฉฐ, ๊ฐ ๋‹จ์–ด๋Š” ๊ธ€์ž(character) ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. dictionary = {'l o w </w>' : 5, 'l o w e r </w>' : 2, 'n e w e s t </w>':6, 'w i d e s t </w>':3 } BPE์˜ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์œ„์—์„œ ์„ค๋ช…ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ๊ฐ€์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์Œ์„ ํ•˜๋‚˜์˜ ์œ ๋‹ˆ๊ทธ๋žจ์œผ๋กœ ํ†ตํ•ฉํ•˜๋Š” ๊ณผ์ •์œผ๋กœ num_merges ํšŒ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. def get_stats(dictionary): # ์œ ๋‹ˆ๊ทธ๋žจ์˜ pair๋“ค์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์นด์šดํŠธ pairs = collections.defaultdict(int) for word, freq in dictionary.items(): symbols = word.split() for i in range(len(symbols)-1): pairs[symbols[i],symbols[i+1]] += freq print('ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ :', dict(pairs)) return pairs def merge_dictionary(pair, v_in): v_out = {} bigram = re.escape(' '.join(pair)) p = re.compile(r'(?<!\S)' + bigram + r'(?!\S)') for word in v_in: w_out = p.sub(''.join(pair), word) v_out[w_out] = v_in[word] return v_out bpe_codes = {} bpe_codes_reverse = {} for i in range(num_merges): display(Markdown("### Iteration {}".format(i + 1))) pairs = get_stats(dictionary) best = max(pairs, key=pairs.get) dictionary = merge_dictionary(best, dictionary) bpe_codes[best] = i bpe_codes_reverse[best[0] + best[1]] = best print("new merge: {}".format(best)) print("dictionary: {}".format(dictionary)) ์ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์œผ๋ฉฐ ์ด๋Š” ๊ธ€์ž๋“ค์˜ ํ†ตํ•ฉ ๊ณผ์ •์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Iteration 1 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('l', 'o'): 7, ('o', 'w'): 7, ('w', '</w>'): 5, ('w', 'e'): 8, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('e', 's'): 9, ('s', 't'): 9, ('t', '</w>'): 9, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'e'): 3} new merge: ('e', 's') dictionary: {'l o w </w>': 5, 'l o w e r </w>': 2, 'n e w es t </w>': 6, 'w i d es t </w>': 3} Iteration 2 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('l', 'o'): 7, ('o', 'w'): 7, ('w', '</w>'): 5, ('w', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('w', 'es'): 6, ('es', 't'): 9, ('t', '</w>'): 9, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'es'): 3} new merge: ('es', 't') dictionary: {'l o w </w>': 5, 'l o w e r </w>': 2, 'n e w est </w>': 6, 'w i d est </w>': 3} Iteration 3 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('l', 'o'): 7, ('o', 'w'): 7, ('w', '</w>'): 5, ('w', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('w', 'est'): 6, ('est', '</w>'): 9, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est'): 3} new merge: ('est', '</w>') dictionary: {'l o w </w>': 5, 'l o w e r </w>': 2, 'n e w est</w>': 6, 'w i d est</w>': 3} Iteration 4 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('l', 'o'): 7, ('o', 'w'): 7, ('w', '</w>'): 5, ('w', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('w', 'est</w>'): 6, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('l', 'o') dictionary: {'lo w </w>': 5, 'lo w e r </w>': 2, 'n e w est</w>': 6, 'w i d est</w>': 3} Iteration 5 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('lo', 'w'): 7, ('w', '</w>'): 5, ('w', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('w', 'est</w>'): 6, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('lo', 'w') dictionary: {'low </w>': 5, 'low e r </w>': 2, 'n e w est</w>': 6, 'w i d est</w>': 3} Iteration 6 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('low', '</w>'): 5, ('low', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('w', 'est</w>'): 6, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('n', 'e') dictionary: {'low </w>': 5, 'low e r </w>': 2, 'ne w est</w>': 6, 'w i d est</w>': 3} Iteration 7 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('low', '</w>'): 5, ('low', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('ne', 'w'): 6, ('w', 'est</w>'): 6, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('ne', 'w') dictionary: {'low </w>': 5, 'low e r </w>': 2, 'new est</w>': 6, 'w i d est</w>': 3} Iteration 8 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('low', '</w>'): 5, ('low', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('new', 'est</w>'): 6, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('new', 'est</w>') dictionary: {'low </w>': 5, 'low e r </w>': 2, 'newest</w>': 6, 'w i d est</w>': 3} Iteration 9 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('low', '</w>'): 5, ('low', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('low', '</w>') dictionary: {'low</w>': 5, 'low e r </w>': 2, 'newest</w>': 6, 'w i d est</w>': 3} Iteration 10 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('low', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('w', 'i') dictionary: {'low</w>': 5, 'low e r </w>': 2, 'newest</w>': 6, 'wi d est</w>': 3} e์™€ s์˜ ์Œ์€ ์ดˆ๊ธฐ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์ด 9ํšŒ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— es๋กœ ํ†ตํ•ฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์œผ๋กœ๋Š” es์™€ t์˜ ์Œ์„, ๊ทธ๋‹ค์Œ์œผ๋กœ๋Š” est์™€ </w>์˜ ์Œ์„ ํ†ตํ•ฉ์‹œํ‚ต๋‹ˆ๋‹ค. ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ํ†ตํ•ฉํ•˜๋Š” ์ด ๊ณผ์ •์„ ์ด num_merges ํšŒ ๋ฐ˜๋ณตํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. bpe_codes๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด merge ํ–ˆ๋˜ ๊ธฐ๋ก์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. print(bpe_codes) {('e', 's'): 0, ('es', 't'): 1, ('est', '</w>'): 2, ('l', 'o'): 3, ('lo', 'w'): 4, ('n', 'e'): 5, ('ne', 'w'): 6, ('new', 'est</w>'): 7, ('low', '</w>'): 8, ('w', 'i'): 9} ์ด ๊ธฐ๋ก์€ ์ƒˆ๋กœ์šด ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜์˜€์„ ๋•Œ, ํ˜„์žฌ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„œ๋ธŒ ์›Œ๋“œ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์˜๊ฑฐํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜๋Š” ์ผ์— ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4) OOV์— ๋Œ€์ฒ˜ํ•˜๊ธฐ def get_pairs(word): """Return set of symbol pairs in a word. Word is represented as a tuple of symbols (symbols being variable-length strings). """ pairs = set() prev_char = word[0] for char in word[1:]: pairs.add((prev_char, char)) prev_char = char return pairs def encode(orig): """Encode word based on list of BPE merge operations, which are applied consecutively""" word = tuple(orig) + ('</w>',) display(Markdown("__word split into characters:__ <tt>{}</tt>".format(word))) pairs = get_pairs(word) if not pairs: return orig iteration = 0 while True: iteration += 1 display(Markdown("__Iteration {}:__".format(iteration))) print("bigrams in the word: {}".format(pairs)) bigram = min(pairs, key = lambda pair: bpe_codes.get(pair, float('inf'))) print("candidate for merging: {}".format(bigram)) if bigram not in bpe_codes: display(Markdown("__Candidate not in BPE merges, algorithm stops.__")) break first, second = bigram new_word = [] i = 0 while i < len(word): try: j = word.index(first, i) new_word.extend(word[i:j]) i = j except: new_word.extend(word[i:]) break if word[i] == first and i < len(word)-1 and word[i+1] == second: new_word.append(first+second) i += 2 else: new_word.append(word[i]) i += 1 new_word = tuple(new_word) word = new_word print("word after merging: {}".format(word)) if len(word) == 1: break else: pairs = get_pairs(word) # ํŠน๋ณ„ ํ† ํฐ์ธ </w>๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. if word[-1] == '</w>': word = word[:-1] elif word[-1].endswith('</w>'): word = word[:-1] + (word[-1].replace('</w>',''),) return word ๋‹จ์–ด 'loki'๊ฐ€ ๋“ค์–ด์˜ค๋ฉด BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ์–ด๋–ป๊ฒŒ ๋ถ„๋ฆฌํ• ๊นŒ์š”? encode("loki") word split into characters: ('l', 'o', 'k', 'i', '') Iteration 1: bigrams in the word: {('i', '</w>'), ('o', 'k'), ('l', 'o'), ('k', 'i')} candidate for merging: ('l', 'o') word after merging: ('lo', 'k', 'i', '</w>') Iteration 2: bigrams in the word: {('i', '</w>'), ('k', 'i'), ('lo', 'k')} candidate for merging: ('i', '</w>') Candidate not in BPE merges, algorithm stops. ('lo', 'k', 'i') ํ˜„์žฌ ์„œ๋ธŒ ์›Œ๋“œ ๋‹จ์–ด ์ง‘ํ•ฉ์—๋Š” 'lo'๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ, 'lo'๋Š” ์œ ์ง€ํ•˜๊ณ  'k'์™€ 'i'๋Š” ๋ถ„๋ฆฌ์‹œํ‚ต๋‹ˆ๋‹ค. ๋‹จ์–ด 'lowest'์— ๋Œ€ํ•ด์„œ๋„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. encode("lowest") word split into characters: ('l', 'o', 'w', 'e', 's', 't', '') Iteration 1: bigrams in the word: {('e', 's'), ('s', 't'), ('t', '</w>'), ('o', 'w'), ('w', 'e'), ('l', 'o')} candidate for merging: ('e', 's') word after merging: ('l', 'o', 'w', 'es', 't', '</w>') Iteration 2: bigrams in the word: {('w', 'es'), ('es', 't'), ('t', '</w>'), ('o', 'w'), ('l', 'o')} candidate for merging: ('es', 't') word after merging: ('l', 'o', 'w', 'est', '</w>') Iteration 3: bigrams in the word: {('o', 'w'), ('l', 'o'), ('est', '</w>'), ('w', 'est')} candidate for merging: ('est', '</w>') word after merging: ('l', 'o', 'w', 'est</w>') Iteration 4: bigrams in the word: {('o', 'w'), ('l', 'o'), ('w', 'est</w>')} candidate for merging: ('l', 'o') word after merging: ('lo', 'w', 'est</w>') Iteration 5: bigrams in the word: {('lo', 'w'), ('w', 'est</w>')} candidate for merging: ('lo', 'w') word after merging: ('low', 'est</w>') Iteration 6: bigrams in the word: {('low', 'est</w>')} candidate for merging: ('low', 'est</w>') Candidate not in BPE merges, algorithm stops. ('low', 'est') ํ˜„์žฌ ์„œ๋ธŒ ์›Œ๋“œ ๋‹จ์–ด ์ง‘ํ•ฉ์— 'low'์™€ 'est'๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ, 'low'์™€ 'est'๋ฅผ ๋ถ„๋ฆฌ์‹œํ‚ต๋‹ˆ๋‹ค. ๋‹จ์–ด 'lowing'์— ๋Œ€ํ•ด์„œ๋„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. encode("lowing") word split into characters: ('l', 'o', 'w', 'i', 'n', 'g', '') Iteration 1: bigrams in the word: {('n', 'g'), ('w', 'i'), ('g', '</w>'), ('i', 'n'), ('o', 'w'), ('l', 'o')} candidate for merging: ('l', 'o') word after merging: ('lo', 'w', 'i', 'n', 'g', '</w>') Iteration 2: bigrams in the word: {('lo', 'w'), ('n', 'g'), ('w', 'i'), ('g', '</w>'), ('i', 'n')} candidate for merging: ('lo', 'w') word after merging: ('low', 'i', 'n', 'g', '</w>') Iteration 3: bigrams in the word: {('n', 'g'), ('g', '</w>'), ('i', 'n'), ('low', 'i')} candidate for merging: ('n', 'g') Candidate not in BPE merges, algorithm stops. ('low', 'i', 'n', 'g') ํ˜„์žฌ ์„œ๋ธŒ ์›Œ๋“œ ๋‹จ์–ด ์ง‘ํ•ฉ์— 'low'๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ, 'i', 'n', 'g'์˜ ๋ฐ”์ด ๊ทธ๋žจ ์กฐํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์„œ๋ธŒ ์›Œ๋“œ๋Š” ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ 'i', 'n', 'g'๋กœ ์ „๋ถ€ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ๋œ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์–ด๋–ค ์„œ๋ธŒ ์›Œ๋“œ๋„ ์กด์žฌํ•˜์ง€ ์•Š๋Š” 'highing'์€ ์–ด๋–จ๊นŒ์š”? encode("highing") word split into characters: ('h', 'i', 'g', 'h', 'i', 'n', 'g', '') Iteration 1: bigrams in the word: {('n', 'g'), ('g', 'h'), ('h', 'i'), ('g', '</w>'), ('i', 'n'), ('i', 'g')} candidate for merging: ('n', 'g') Candidate not in BPE merges, algorithm stops. ('h', 'i', 'g', 'h', 'i', 'n', 'g') ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์ด ๋ถ„๋ฆฌ๋ฉ๋‹ˆ๋‹ค. BPE ์™ธ์—๋„ BPE๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง„ Wordpiece Tokenizer๋‚˜ Unigram Language Model Tokenizer์™€ ๊ฐ™์€ ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ๋Š” ๊ฐ„๋žตํžˆ ์ด๋Ÿฐ ๊ฒƒ๋“ค์ด ์กด์žฌํ•œ๋‹ค ์ •๋„๋กœ๋งŒ ์–ธ๊ธ‰ํ•˜๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. 3. WordPiece Tokenizer ๋…ผ๋ฌธ : https://static.googleusercontent.com/media/research.google.com/ko//pubs/archive/37842.pdf ๊ตฌ๊ธ€์ด ์œ„ WordPiece Tokenizer๋ฅผ ๋ณ€ํ˜•ํ•˜์—ฌ ๋ฒˆ์—ญ๊ธฐ์— ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ๋…ผ๋ฌธ : https://arxiv.org/pdf/1609.08144.pdf WordPiece Tokenizer์€ BPE์˜ ๋ณ€ํ˜• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ BPE๊ฐ€ ๋นˆ๋„์ˆ˜์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•œ ์Œ์„ ๋ณ‘ํ•ฉํ•˜๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ, ๋ณ‘ํ•ฉ๋˜์—ˆ์„ ๋•Œ ์ฝ”ํผ์Šค์˜ ์šฐ๋„(Likelihood)๋ฅผ ๊ฐ€์žฅ ๋†’์ด๋Š” ์Œ์„ ๋ณ‘ํ•ฉํ•ฉ๋‹ˆ๋‹ค. 2016๋…„์˜ ์œ„ ๋…ผ๋ฌธ์—์„œ ๊ตฌ๊ธ€์€ ๊ตฌ๊ธ€ ๋ฒˆ์—ญ๊ธฐ์—์„œ WordPiece Tokenizer๊ฐ€ ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ ๊ธฐ์ˆ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ด์ „์˜ ๋ฌธ์žฅ: Jet makers feud over seat width with big orders at stake WordPiece Tokenizer๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ(wordpieces): _J et _makers _fe ud _over _seat _width _with _big _orders _at _stake Jet๋Š” J์™€ et๋กœ ๋‚˜๋ˆ„์–ด์กŒ์œผ๋ฉฐ, feud๋Š” fe์™€ ud๋กœ ๋‚˜๋ˆ„์–ด์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. WordPiece Tokenizer๋Š” ๋ชจ๋“  ๋‹จ์–ด์˜ ๋งจ ์•ž์— _๋ฅผ ๋ถ™์ด๊ณ , ๋‹จ์–ด๋Š” ์„œ๋ธŒ ์›Œ๋“œ(subword)๋กœ ํ†ต๊ณ„์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๋„์–ด์“ฐ๊ธฐ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์–ธ๋”๋ฐ” _๋Š” ๋ฌธ์žฅ ๋ณต์›์„ ์œ„ํ•œ ์žฅ์น˜์ž…๋‹ˆ๋‹ค. ์˜ˆ์ปจ๋Œ€, WordPiece Tokenizer์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ๋ฌธ์žฅ์„ ๋ณด๋ฉด, Jet โ†’ _J et์™€ ๊ฐ™์ด ๊ธฐ์กด์— ์—†๋˜ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ์„œ๋ธŒ ์›Œ๋“œ(subwords)๋“ค์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ตฌ๋ถ„์ž ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ธฐ์กด์— ์žˆ๋˜ ๋„์–ด์“ฐ๊ธฐ์™€ ๊ตฌ๋ถ„์ž ์—ญํ• ์˜ ๋„์–ด์“ฐ๊ธฐ๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌ๋ณ„ํ• ๊นŒ์š”? ์ด ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋‹จ์–ด๋“ค ์•ž์— ๋ถ™์€ ์–ธ๋”๋ฐ” _์ž…๋‹ˆ๋‹ค. WordPiece Tokenizer์ด ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋กœ๋ถ€ํ„ฐ ๋‹ค์‹œ ์ˆ˜ํ–‰ ์ „์˜ ๊ฒฐ๊ณผ๋กœ ๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ ์žˆ๋Š” ๋ชจ๋“  ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ „๋ถ€ ์ œ๊ฑฐํ•˜๊ณ , ์–ธ๋”๋ฐ”๋ฅผ ๋„์–ด์“ฐ๊ธฐ๋กœ ๋ฐ”๊พธ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์œ ๋ช… ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ BERT๋ฅผ ํ›ˆ๋ จํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 4. Unigram Language Model Tokenizer ๋…ผ๋ฌธ : https://arxiv.org/pdf/1804.10959.pdf ์œ ๋‹ˆ๊ทธ๋žจ ์–ธ์–ด ๋ชจ๋ธ ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ฐ๊ฐ์˜ ์„œ๋ธŒ ์›Œ๋“œ๋“ค์— ๋Œ€ํ•ด์„œ ์†์‹ค(loss)์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์„œ๋ธŒ ๋‹จ์–ด์˜ ์†์‹ค์ด๋ผ๋Š” ๊ฒƒ์€ ํ•ด๋‹น ์„œ๋ธŒ ์›Œ๋“œ๊ฐ€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์ œ๊ฑฐ๋˜์—ˆ์„ ๊ฒฝ์šฐ, ์ฝ”ํผ์Šค์˜ ์šฐ๋„(Likelihood)๊ฐ€ ๊ฐ์†Œํ•˜๋Š” ์ •๋„๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ธก์ •๋œ ์„œ๋ธŒ ์›Œ๋“œ๋“ค์„ ์†์‹ค์˜ ์ •๋„๋กœ ์ •๋ ฌํ•˜์—ฌ, ์ตœ์•…์˜ ์˜ํ–ฅ์„ ์ฃผ๋Š” 10~20%์˜ ํ† ํฐ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์›ํ•˜๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ๋‚˜์ด์ง• ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด์–ด์„œ ์ด๋ฅผ ์‹ค๋ฌด์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํŒจํ‚ค์ง€์ธ ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece)๋‚˜ ํ† ํฌ ๋‚˜์ด ์ €์Šค(tokenizers)์˜ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 13-02 ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece) ์•ž์„œ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฐํ™”๋ฅผ ์œ„ํ•œ BPE(Byte Pair Encoding) ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ทธ ์™ธ BPE์˜ ๋ณ€ํ˜• ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์–ธ๊ธ‰ํ–ˆ์Šต๋‹ˆ๋‹ค. BPE๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ธฐํƒ€ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ๋‚˜์ด์ง• ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์„ ๋‚ด์žฅํ•œ ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece)๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์‹ค๋ฌด์—์„œ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์„ ์˜ ์„ ํƒ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. 1. ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece) ๋…ผ๋ฌธ : https://arxiv.org/pdf/1808.06226.pdf ์„ผํ…์Šค ํ”ผ์Šค ๊นƒํ—ˆ๋ธŒ : https://github.com/google/sentencepiece ๋‚ด๋ถ€ ๋‹จ์–ด ๋ถ„๋ฆฌ๋ฅผ ์œ„ํ•œ ์œ ์šฉํ•œ ํŒจํ‚ค์ง€๋กœ ๊ตฌ๊ธ€์˜ ์„ผํ…์Šค ํ”ผ์Šค(Sentencepiece)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ๊ธ€์€ BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ Unigram Language Model Tokenizer๋ฅผ ๊ตฌํ˜„ํ•œ ์„ผํ…์Šค ํ”ผ์Šค๋ฅผ ๊นƒํ—ˆ๋ธŒ์— ๊ณต๊ฐœํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ๋‹จ์–ด ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ, ๋ฐ์ดํ„ฐ์— ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ๋จผ์ € ์ง„ํ–‰ํ•œ ์ƒํƒœ์—ฌ์•ผ ํ•œ๋‹ค๋ฉด ์ด ๋‹จ์–ด ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ชจ๋“  ์–ธ์–ด์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜์–ด์™€ ๋‹ฌ๋ฆฌ ํ•œ๊ตญ์–ด์™€ ๊ฐ™์€ ์–ธ์–ด๋Š” ๋‹จ์–ด ํ† ํฐํ™”๋ถ€ํ„ฐ๊ฐ€ ์‰ฝ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ์ด๋Ÿฐ ์‚ฌ์ „ ํ† ํฐํ™” ์ž‘์—…(pretokenization) ์—†์ด ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ(raw data)์— ๋ฐ”๋กœ ๋‹จ์–ด ๋ถ„๋ฆฌ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ทธ ์–ด๋–ค ์–ธ์–ด์—๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์„ผํ…์Šค ํ”ผ์Šค๋Š” ์ด ์ด์ ์„ ์‚ด๋ ค์„œ ๊ตฌํ˜„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์„ผํ…์Šค ํ”ผ์Šค๋Š” ์‚ฌ์ „ ํ† ํฐํ™” ์ž‘์—… ์—†์ด ๋‹จ์–ด ๋ถ„๋ฆฌ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์–ธ์–ด์— ์ข…์†๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. pip install sentencepiece 2. IMDB ๋ฆฌ๋ทฐ ํ† ํฐํ™”ํ•˜๊ธฐ import sentencepiece as spm import pandas as pd import urllib.request import csv IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์ด๋ฅผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/LawrenceDuan/IMDb-Review-Analysis/master/IMDb_Reviews.csv", filename="IMDb_Reviews.csv") train_df = pd.read_csv('IMDb_Reviews.csv') train_df['review'] 0 My family and I normally do not watch local mo... 1 Believe it or not, this was at one time the wo... 2 After some internet surfing, I found the "Home... 3 One of the most unheralded great works of anim... 4 It was the Sixties, and anyone with long hair ... ... 49995 the people who came up with this are SICK AND ... 49996 The script is so so laughable... this in turn,... 49997 "So there's this bride, you see, and she gets ... 49998 Your mind will not be satisfied by this noย—bud... 49999 The chaser's war on everything is a weekly sho... Name: review, Length: 50000, dtype: object print('๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(train_df)) # ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 50000 ์ด 5๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์„ผํ…์Šค ํ”ผ์Šค์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ txt ํŒŒ์ผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. with open('imdb_review.txt', 'w', encoding='utf8') as f: f.write('\n'.join(train_df['review'])) ์„ผํ…์Šค ํ”ผ์Šค๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ๊ณผ ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. spm.SentencePieceTrainer.Train('--input=imdb_review.txt --model_prefix=imdb --vocab_size=5000 --model_type=bpe --max_sentence_length=9999') ๊ฐ ์ธ์ž๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. input : ํ•™์Šต์‹œํ‚ฌ ํŒŒ์ผ model_prefix : ๋งŒ๋“ค์–ด์งˆ ๋ชจ๋ธ ์ด๋ฆ„ vocab_size : ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ model_type : ์‚ฌ์šฉํ•  ๋ชจ๋ธ (unigram(default), bpe, char, word) max_sentence_length: ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด pad_id, pad_piece: pad token id, ๊ฐ’ unk_id, unk_piece: unknown token id, ๊ฐ’ bos_id, bos_piece: begin of sentence token id, ๊ฐ’ eos_id, eos_piece: end of sequence token id, ๊ฐ’ user_defined_symbols: ์‚ฌ์šฉ์ž ์ •์˜ ํ† ํฐ vocab ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด imdb.model, imdb.vocab ํŒŒ์ผ ๋‘ ๊ฐœ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. vocab ํŒŒ์ผ์—์„œ ํ•™์Šต๋œ ์„œ๋ธŒ ์›Œ๋“œ๋“ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด vocab ํŒŒ์ผ์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ด ๋ด…์‹œ๋‹ค. vocab_list = pd.read_csv('imdb.vocab', sep='\t', header=None, quoting=csv.QUOTE_NONE) vocab_list.sample(10) ์œ„์—์„œ vocab_size์˜ ์ธ์ž๋ฅผ ํ†ตํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ 5,000๊ฐœ๋กœ ์ œํ•œํ•˜์˜€์œผ๋ฏ€๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 5,000๊ฐœ์ž…๋‹ˆ๋‹ค. len(vocab_list) 5000 model ํŒŒ์ผ์„ ๋กœ๋“œํ•˜์—ฌ ๋‹จ์–ด ์‹œํ€€์Šค๋ฅผ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ฐ”๊พธ๋Š” ์ธ์ฝ”๋”ฉ ์ž‘์—…์ด๋‚˜ ๋ฐ˜๋Œ€๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋””์ฝ”๋”ฉ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. sp = spm.SentencePieceProcessor() vocab_file = "imdb.model" sp.load(vocab_file) True ์•„๋ž˜์˜ ๋‘ ๊ฐ€์ง€ ๋„๊ตฌ๋ฅผ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. encode_as_pieces : ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด ์„œ๋ธŒ ์›Œ๋“œ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. encode_as_ids : ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. lines = [ "I didn't at all think of it this way.", "I have waited a long time for someone to film" ] for line in lines: print(line) print(sp.encode_as_pieces(line)) print(sp.encode_as_ids(line)) print() I didn't at all think of it this way. ['โ– I', 'โ– didn', "'", 't', 'โ– at', 'โ– all', 'โ– think', 'โ– of', 'โ– it', 'โ– this', 'โ– way', '.'] [41, 623, 4950, 4926, 138, 169, 378, 30, 58, 73, 413, 4945] I have waited a long time for someone to film ['โ– I', 'โ– have', 'โ– wa', 'ited', 'โ– a', 'โ– long', 'โ– time', 'โ– for', 'โ– someone', 'โ– to', 'โ– film'] [41, 141, 1364, 1120, 4, 666, 285, 92, 1078, 33, 91] GetPieceSize() : ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. sp.GetPieceSize() 5000 idToPiece : ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋งคํ•‘๋˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.IdToPiece(430) โ– character PieceToId : ์„œ๋ธŒ ์›Œ๋“œ๋กœ๋ถ€ํ„ฐ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.PieceToId('โ– character') 430 DecodeIds : ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.DecodeIds([41, 141, 1364, 1120, 4, 666, 285, 92, 1078, 33, 91]) DecodePieces : ์„œ๋ธŒ ์›Œ๋“œ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. I have waited a long time for someone to film sp.DecodePieces(['โ– I', 'โ– have', 'โ– wa', 'ited', 'โ– a', 'โ– long', 'โ– time', 'โ– for', 'โ– someone', 'โ– to', 'โ– film']) encode : ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์ธ์ž ๊ฐ’์— ๋”ฐ๋ผ์„œ ์ •์ˆ˜ ์‹œํ€€์Šค ๋˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. I have waited a long time for someone to film print(sp.encode('I have waited a long time for someone to film', out_type=str)) print(sp.encode('I have waited a long time for someone to film', out_type=int)) ['โ– I', 'โ– have', 'โ– wa', 'ited', 'โ– a', 'โ– long', 'โ– time', 'โ– for', 'โ– someone', 'โ– to', 'โ– film'] [41, 141, 1364, 1120, 4, 666, 285, 92, 1078, 33, 91] 3. ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ํ† ํฐํ™”ํ•˜๊ธฐ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์œ„์˜ IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์™€ ๋™์ผํ•œ ๊ณผ์ •์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import pandas as pd import sentencepiece as spm import urllib.request import csv ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings.txt", filename="ratings.txt") naver_df = pd.read_table('ratings.txt') naver_df[:5] ์ด 20๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. print('๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(naver_df)) # ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 200000 ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ Null ๊ฐ’์ด ์กด์žฌํ•˜๋ฏ€๋กœ ์ด๋ฅผ ์ œ๊ฑฐํ•œ ํ›„์— ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. print(naver_df.isnull().values.any()) True naver_df = naver_df.dropna(how = 'any') # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ํ–‰ ์ œ๊ฑฐ print(naver_df.isnull().values.any()) # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ False print('๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(naver_df)) # ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 199992 ์ตœ์ข…์ ์œผ๋กœ 199,992๊ฐœ์˜ ์ƒ˜ํ”Œ์„ naver_review.txt ํŒŒ์ผ์— ์ €์žฅํ•œ ํ›„์— ์„ผํ…์Šค ํ”ผ์Šค๋ฅผ ํ†ตํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. with open('naver_review.txt', 'w', encoding='utf8') as f: f.write('\n'.join(naver_df['document'])) spm.SentencePieceTrainer.Train('--input=naver_review.txt --model_prefix=naver --vocab_size=5000 --model_type=bpe --max_sentence_length=9999') vocab ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด naver.model, naver.vocab ํŒŒ์ผ ๋‘ ๊ฐœ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. .vocab์—์„œ ํ•™์Šต๋œ subwords๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. vocab_list = pd.read_csv('naver.vocab', sep='\t', header=None, quoting=csv.QUOTE_NONE) vocab_list[:10] vocab_list.sample(10) Vocabulary์—๋Š” unknown, ๋ฌธ์žฅ์˜ ์‹œ์ž‘, ๋ฌธ์žฅ์˜ ๋์„ ์˜๋ฏธํ•˜๋Š” special token์ด 0, 1, 2์— ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. len(vocab_list) 5000 ์„ค์ •ํ•œ ๋Œ€๋กœ 5000๊ฐœ์˜ ์„œ๋ธŒ ์›Œ๋“œ๊ฐ€ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. sp = spm.SentencePieceProcessor() vocab_file = "naver.model" sp.load(vocab_file) True lines = [ "๋ญ ์ด๋”ด ๊ฒƒ๋„ ์˜ํ™”๋ƒ.", "์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹", ] for line in lines: print(line) print(sp.encode_as_pieces(line)) print(sp.encode_as_ids(line)) print() ๋ญ ์ด๋”ด ๊ฒƒ๋„ ์˜ํ™”๋ƒ. ['โ–๋ญ', 'โ–์ด๋”ด', 'โ–๊ฒƒ๋„', 'โ–์˜ํ™”๋ƒ', '.'] [132, 966, 1296, 2590, 3276] ์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹ ['โ–์ง„์งœ', 'โ–์ตœ๊ณ ์˜', 'โ–์˜ํ™”์ž…๋‹ˆ๋‹ค', 'โ–แ„แ„'] [54, 200, 821, 85] GetPieceSize() : ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. sp.GetPieceSize() 5000 idToPiece : ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋งคํ•‘๋˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.IdToPiece(4) '์˜ํ™”' PieceToId : ์„œ๋ธŒ ์›Œ๋“œ๋กœ๋ถ€ํ„ฐ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.PieceToId('์˜ํ™”') DecodeIds : ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.DecodeIds([54, 200, 821, 85]) ์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค แ„แ„ DecodePieces : ์„œ๋ธŒ ์›Œ๋“œ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.DecodePieces(['โ–์ง„์งœ', 'โ–์ตœ๊ณ ์˜', 'โ–์˜ํ™”์ž…๋‹ˆ๋‹ค', 'โ–แ„แ„']) ์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค แ„แ„ encode : ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์ธ์ž ๊ฐ’์— ๋”ฐ๋ผ์„œ ์ •์ˆ˜ ์‹œํ€€์Šค ๋˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. print(sp.encode('์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹', out_type=str)) print(sp.encode('์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹', out_type=int)) ['โ–์ง„์งœ', 'โ–์ตœ๊ณ ์˜', 'โ–์˜ํ™”์ž…๋‹ˆ๋‹ค', 'โ–แ„แ„'] [54, 200, 821, 85] 13-03 ์„œ๋ธŒ ์›Œ๋“œ ํ…์ŠคํŠธ ์ธ์ฝ”๋”(SubwordTextEncoder) SubwordTextEncoder๋Š” ํ…์„œ ํ”Œ๋กœ๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €์ž…๋‹ˆ๋‹ค. BPE์™€ ์œ ์‚ฌํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ Wordpiece Model์„ ์ฑ„ํƒํ•˜์˜€์œผ๋ฉฐ, ํŒจํ‚ค์ง€๋ฅผ ํ†ตํ•ด ์‰ฝ๊ฒŒ ๋‹จ์–ด๋“ค์„ ์„œ๋ธŒ ์›Œ๋“œ๋“ค๋กœ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SubwordTextEncoder๋ฅผ ํ†ตํ•ด์„œ IMDB ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์™€ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. Tensorflow 2.3+ ๋ฒ„์ „์—์„œ๋Š” tfds.features.text ๋Œ€์‹  tfds.deprecated.text๋ผ๊ณ  ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1. IMDB ๋ฆฌ๋ทฐ ํ† ํฐํ™”ํ•˜๊ธฐ import pandas as pd import urllib.request import tensorflow_datasets as tfds ๋‹ค์šด๋กœ๋“œํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/LawrenceDuan/IMDb-Review-Analysis/master/IMDb_Reviews.csv", filename="IMDb_Reviews.csv") train_df = pd.read_csv('IMDb_Reviews.csv') ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์—์„œ 'review'์— ํ•ด๋‹นํ•˜๋Š” ์—ด์ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. train_df['review'] 0 My family and I normally do not watch local mo... 1 Believe it or not, this was at one time the wo... 2 After some internet surfing, I found the "Home... 3 One of the most unheralded great works of anim... 4 It was the Sixties, and anyone with long hair ... ... 49995 the people who came up with this are SICK AND ... 49996 The script is so so laughable... this in turn,... 49997 "So there's this bride, you see, and she gets ... 49998 Your mind will not be satisfied by this noย—bud... 49999 The chaser's war on everything is a weekly sho... Name: review, Length: 50000, dtype: object tfds.features.text.SubwordTextEncoder.build_from_corpus์˜ ์ธ์ž๋กœ ํ† ํฐํ™”ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ํ†ตํ•ด์„œ ์„œ๋ธŒ ์›Œ๋“œ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ฐ ์„œ๋ธŒ ์›Œ๋“œ์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. tokenizer = tfds.features.text.SubwordTextEncoder.build_from_corpus( train_df['review'], target_vocab_size=2**13) .subwords๋ฅผ ํ†ตํ•ด์„œ ํ† ํฐํ™”๋œ ์„œ๋ธŒ ์›Œ๋“œ๋“ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 100๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(tokenizer.subwords[:100]) ['the_', ', ', '. ', 'a_', 'and_', 'of_', 'to_', 's_', 'is_', 'br', 'in_', 'I_', 'that_', 'this_', 'it_', ' /><', ' />', 'was_', 'The_', 't_', 'as_', 'with_', 'for_', '.<', 'on_', 'but_', 'movie_', 'are_', ' (', 'have_', 'his_', 'film_', 'not_', 'be_', 'you_', 'ing_', ' "', 'ed_', 'it', 'd_', 'an_', 'at_', 'by_', 'he_', 'one_', 'who_', 'from_', 'y_', 'or_', 'e_', 'like_', 'all_', '" ', 'they_', 'so_', 'just_', 'has_', ') ', 'about_', 'her_', 'out_', 'This_', 'some_', 'movie', 'ly_', 'film', 'very_', 'more_', 'It_', 'what_', 'would_', 'when_', 'if_', 'good_', 'up_', 'which_', 'their_', 'only_', 'even_', 'my_', 'really_', 'had_', 'can_', 'no_', 'were_', 'see_', '? ', 'she_', 'than_', '! ', 'there_', 'been_', 'get_', 'into_', 'will_', ' - ', 'much_', 'n_', 'because_', 'ing'] ์ž„์˜๋กœ ์„ ํƒํ•œ 20๋ฒˆ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ณ , ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ์™€ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(train_df['review'][20]) Pretty bad PRC cheapie which I rarely bother to watch over again, and it's no wonder -- it's slow and creaky and dull as a butter knife. Mad doctor George Zucco is at it again, turning a dimwitted farmhand in overalls (Glenn Strange) into a wolf-man. Unfortunately, the makeup is virtually non-existent, consisting only of a beard and dimestore fangs for the most part. If it were not for Zucco and Strange's presence, along with the cute Anne Nagel, this would be completely unwatchable. Strange, who would go on to play Frankenstein's monster for Unuiversal in two years, does a Lenny impression from "Of Mice and Men", it seems.<br /><br />*1/2 (of Four) encode()๋ฅผ ํ†ตํ•ด์„œ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print('Tokenized sample question: {}'.format(tokenizer.encode(train_df['review'][20]))) Tokenized sample question: [1590, 4162, 132, 7107, 1892, 2983, 578, 76, 12, 4632, 3422, 7, 160, 175, 372, 2, 5, 39, 8051, 8, 84, 2652, 497, 39, 8051, 8, 1374, 5, 3461, 2012, 48, 5, 2263, 21, 4, 2992, 127, 4729, 711, 3, 1391, 8044, 3557, 1277, 8102, 2154, 5681, 9, 42, 15, 372, 2, 3773, 4, 3502, 2308, 467, 4890, 1503, 11, 3347, 1419, 8127, 29, 5539, 98, 6099, 58, 94, 4, 1388, 4230, 8057, 213, 3, 1966, 2, 1, 6700, 8044, 9, 7069, 716, 8057, 6600, 2, 4102, 36, 78, 6, 4, 1865, 40, 5, 3502, 1043, 1645, 8044, 1000, 1813, 23, 1, 105, 1128, 3, 156, 15, 85, 33, 23, 8102, 2154, 5681, 5, 6099, 8051, 8, 7271, 1055, 2, 534, 22, 1, 3046, 5214, 810, 634, 8120, 2, 14, 71, 34, 436, 3311, 5447, 783, 3, 6099, 2, 46, 71, 193, 25, 7, 428, 2274, 2260, 6487, 8051, 8, 2149, 23, 1138, 4117, 6023, 163, 11, 148, 735, 2, 164, 4, 5277, 921, 3395, 1262, 37, 639, 1349, 349, 5, 2460, 328, 15, 5349, 8127, 24, 10, 16, 10, 17, 8054, 8061, 8059, 8062, 29, 6, 6607, 8126, 8053] ์ž„์˜๋กœ ์„ ํƒํ•œ ์งง์€ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ณ , ์ด๋ฅผ ๋‹ค์‹œ์—ญ์œผ๋กœ ๋””์ฝ”๋”ฉ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”ฉ ํ•  ๋•Œ๋Š” ์ธ์ฝ”๋”ฉํ•  ๋•Œ encode()๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ decode()๋ฅผ ํ†ตํ•ด์„œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # train_df์— ์กด์žฌํ•˜๋Š” ๋ฌธ์žฅ ์ค‘ ์ผ๋ถ€๋ฅผ ๋ฐœ์ทŒ sample_string = "It's mind-blowing to me that this film was even made." # ์ธ์ฝ”๋”ฉํ•œ ๊ฒฐ๊ณผ๋ฅผ tokenized_string์— ์ €์žฅ tokenized_string = tokenizer.encode(sample_string) print ('์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„์˜ ๋ฌธ์žฅ : {}'.format(tokenized_string)) # ์ด๋ฅผ ๋‹ค์‹œ ๋””์ฝ”๋”ฉ original_string = tokenizer.decode(tokenized_string) print ('๊ธฐ์กด ๋ฌธ์žฅ : {}'.format(original_string)) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„์˜ ๋ฌธ์žฅ : [137, 8051, 8, 910, 8057, 2169, 36, 7, 103, 13, 14, 32, 18, 79, 681, 8058] ๊ธฐ์กด ๋ฌธ์žฅ : It's mind-blowing to me that this film was even made. .vocab_size๋ฅผ ํ†ตํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ(Vocab size) :', tokenizer.vocab_size) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ(Vocab size) : 8268 ํ˜„์žฌ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 8,268๊ฐœ์ž…๋‹ˆ๋‹ค. ๋””์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ๋ณ‘๋ ฌ์ ์œผ๋กœ ๋‚˜์—ดํ•˜์—ฌ ๊ฐ ๋‹จ์–ด์™€ ๋งคํ•‘๋œ ์ •์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. for ts in tokenized_string: print ('{} ----> {}'.format(ts, tokenizer.decode([ts]))) 137 ----> It 8051 ----> ' 8 ----> s 910 ----> mind 8057 ----> - 2169 ----> blow 36 ----> ing 7 ----> to 103 ----> me 13 ----> that 14 ----> this 32 ----> film 18 ----> was 79 ----> even 681 ----> made 8058 ----> . ์ด๋ฒˆ์—๋Š” ๊ธฐ์กด ์˜ˆ์ œ ๋ฌธ์žฅ ์ค‘ even์ด๋ผ๋Š” ๋‹จ์–ด์— ์ž„์˜๋กœ xyz๋ผ๋Š” 3๊ฐœ์˜ ๊ธ€์ž๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ even์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ด๋ฏธ ํ•˜๋‚˜์˜ ์„œ๋ธŒ ์›Œ๋“œ๋กœ ์ธ์‹ํ•˜๊ณ  ์žˆ๋Š” ์ƒํ™ฉ์—์„œ ๋‚˜๋จธ์ง€ xyz๋ฅผ ์–ด๋–ป๊ฒŒ ๋ถ„๋ฆฌํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. # ์•ž์„œ ์‹ค์Šตํ•œ ๋ฌธ์žฅ์— even ๋’ค์— ์ž„์˜๋กœ xyz ์ถ”๊ฐ€ sample_string = "It's mind-blowing to me that this film was evenxyz made." # ์ธ์ฝ”๋”ฉํ•œ ๊ฒฐ๊ณผ๋ฅผ tokenized_string์— ์ €์žฅ tokenized_string = tokenizer.encode(sample_string) print ('์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„์˜ ๋ฌธ์žฅ : {}'.format(tokenized_string)) # ์ด๋ฅผ ๋‹ค์‹œ ๋””์ฝ”๋”ฉ original_string = tokenizer.decode(tokenized_string) print ('๊ธฐ์กด ๋ฌธ์žฅ : {}'.format(original_string)) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„์˜ ๋ฌธ์žฅ [137, 8051, 8, 910, 8057, 2169, 36, 7, 103, 13, 14, 32, 18, 7974, 8132, 8133, 997, 681, 8058] ๊ธฐ์กด ๋ฌธ์žฅ: It's mind-blowing to me that this film was evenxyz made. for ts in tokenized_string: print ('{} ----> {}'.format(ts, tokenizer.decode([ts]))) 137 ----> It 8051 ----> ' 8 ----> s 910 ----> mind 8057 ----> - 2169 ----> blow 36 ----> ing 7 ----> to 103 ----> me 13 ----> that 14 ----> this 32 ----> film 18 ----> was 7974 ----> even 8132 ----> x 8133 ----> y 997 ----> z 681 ----> made 8058 ----> . evenxyz์—์„œ even์„ ๋…๋ฆฝ์ ์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  xyz๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ์„œ ๋“ฑ์žฅํ•œ ์ ์ด ์—†์œผ๋ฏ€๋กœ ๊ฐ๊ฐ ์ „๋ถ€ ๋ถ„๋ฆฌ๋ฉ๋‹ˆ๋‹ค. 2. ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ํ† ํฐํ™”ํ•˜๊ธฐ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ์— ๋Œ€ํ•ด์„œ๋„ ์œ„์—์„œ IMDB ์˜ํ™” ๋ฆฌ๋ทฐ์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰ํ•œ ๋™์ผํ•œ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. import pandas as pd import urllib.request import tensorflow_datasets as tfds ๋‹ค์šด๋กœ๋“œํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings_train.txt", filename="ratings_train.txt") train_data = pd.read_table('ratings_train.txt') ์ด ๋ฐ์ดํ„ฐ์—๋Š” Null ๊ฐ’์ด ์กด์žฌํ•˜๋ฏ€๋กœ ์ด๋ฅผ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. print(train_data.isnull().sum()) id 0 document 5 label 0 dtype: int64 train_data = train_data.dropna(how = 'any') # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ํ–‰ ์ œ๊ฑฐ print(train_data.isnull().values.any()) # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ tfds.features.text.SubwordTextEncoder.build_from_corpus์˜ ์ธ์ž๋กœ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์„œ, ์„œ๋ธŒ ์›Œ๋“œ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ฐ ์„œ๋ธŒ ์›Œ๋“œ์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. tokenizer = tfds.features.text.SubwordTextEncoder.build_from_corpus( train_data['document'], target_vocab_size=2**13) ํ† ํฐํ™”๋œ 100๊ฐœ์˜ ์„œ๋ธŒ ์›Œ๋“œ๋“ค์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(tokenizer.subwords[:100]) ['. ', '..', '์˜ํ™”', '์ด_', '...', '์˜_', '๋Š”_', '๋„_', '๋‹ค', ', ', '์„_', '๊ณ _', '์€_', '๊ฐ€_', '์—_', '.. ', 'ํ•œ_', '๋„ˆ๋ฌด_', '์ •๋ง_', '๋ฅผ_', '๊ณ ', '๊ฒŒ_', '์˜ํ™”_', '์ง€', '... ', '์ง„์งœ_', '์ด', '๋‹ค_', '์š”', '๋งŒ_', '? ', '๊ณผ_', '๋‚˜', '๊ฐ€', '์„œ_', '์ง€_', '๋กœ_', '์œผ๋กœ_', '์•„', '์–ด', '....', '์Œ', 'ํ•œ', '์ˆ˜_', '์™€_', '๋„', '๋„ค', '๊ทธ๋ƒฅ_', '๋‚˜_', '๋”_', '์™œ_', '์ด๋Ÿฐ_', '๋ฉด_', '๊ธฐ', 'ํ•˜๊ณ _', '๋ณด๊ณ _', 'ํ•˜๋Š”_', '์„œ', '์ข€_', '๋ฆฌ', '์ž', '์Šค', '์•ˆ', '! ', '์—์„œ_', '์˜ํ™”๋ฅผ_', '๋ฏธ', 'ใ…‹ใ…‹', '๋„ค์š”', '์‹œ', '์ฃผ', '๋ผ', '๋Š”', '์˜ค', '์—†๋Š”_', '์—', 'ํ•ด', '์‚ฌ', '!!', '์˜ํ™”๋Š”_', '๋งˆ', '์ž˜_', '์ˆ˜', '์˜ํ™”๊ฐ€_', '๋งŒ', '๋ณธ_', '๋กœ', '๊ทธ_', '์ง€๋งŒ_', '๋Œ€', '์€', '๋น„', '์˜', '์ผ', '๊ฐœ', '์žˆ๋Š”_', '์—†๋‹ค', 'ํ•จ', '๊ตฌ', 'ํ•˜'] encode()๋ฅผ ํ†ตํ•ด ์ž„์˜๋กœ ์„ ํƒํ•œ 20๋ฒˆ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ณ , ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ์™€ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(train_data['document'][20]) ๋‚˜๋ฆ„ ์‹ฌ์˜คํ•œ ๋œป๋„ ์žˆ๋Š” ๋“ฏ. ๊ทธ๋ƒฅ ํ•™์ƒ์ด ์„ ์ƒ๊ณผ ๋†€์•„๋‚˜๋Š” ์˜ํ™”๋Š” ์ ˆ๋Œ€ ์•„๋‹˜ print('Tokenized sample question: {}'.format(tokenizer.encode(train_data['document'][20]))) Tokenized sample question: [669, 4700, 17, 1749, 8, 96, 131, 1, 48, 2239, 4, 7466, 32, 1274, 2655, 7, 80, 749, 1254] 21๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ณ , ์ด๋ฅผ ๋‹ค์‹œ์—ญ์œผ๋กœ ๋””์ฝ”๋”ฉ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”ฉ ํ•  ๋•Œ๋Š” ์ธ์ฝ”๋”ฉํ•  ๋•Œ encode()๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ decode()๋ฅผ ํ†ตํ•ด์„œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. sample_string = train_data['document'][21] # ์ธ์ฝ”๋”ฉํ•œ ๊ฒฐ๊ณผ๋ฅผ tokenized_string์— ์ €์žฅ tokenized_string = tokenizer.encode(sample_string) print ('์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„์˜ ๋ฌธ์žฅ : {}'.format(tokenized_string)) # ์ด๋ฅผ ๋‹ค์‹œ ๋””์ฝ”๋”ฉ original_string = tokenizer.decode(tokenized_string) print ('๊ธฐ์กด ๋ฌธ์žฅ : {}'.format(original_string)) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„์˜ ๋ฌธ์žฅ : [570, 892, 36, 584, 159, 7091, 201] ๊ธฐ์กด ๋ฌธ์žฅ : ๋ณด๋ฉด์„œ ์›ƒ์ง€ ์•Š๋Š” ๊ฑด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค for ts in tokenized_string: print ('{} ----> {}'.format(ts, tokenizer.decode([ts]))) 570 ----> ๋ณด๋ฉด์„œ 892 ----> ์›ƒ 36 ----> ์ง€ 584 ----> ์•Š๋Š” 159 ----> ๊ฑด 7091 ----> ๋ถˆ๊ฐ€๋Šฅ 201 ----> ํ•˜๋‹ค 13-04 ํ—ˆ๊น… ํŽ˜์ด์Šค ํ† ํฌ ๋‚˜์ด์ €(Huggingface Tokenizer) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์Šคํƒ€ํŠธ์—… ํ—ˆ๊น… ํŽ˜์ด์Šค๊ฐ€ ๊ฐœ๋ฐœํ•œ ํŒจํ‚ค์ง€ tokenizers๋Š” ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ๋“ค์„ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ์ทจ๊ธ‰ํ•˜๋Š” ๋‹ค์–‘ํ•œ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์ด ์ค‘์—์„œ WordPiece Tokenizer๋ฅผ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์Šต์„ ์œ„ํ•ด ์šฐ์„  tokenizers๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install tokenizers 1. BERT์˜ ์›Œ๋“œ ํ”ผ์Šค ํ† ํฌ ๋‚˜์ด์ €(BertWordPieceTokenizer) ๊ตฌ๊ธ€์ด ๊ณต๊ฐœํ•œ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ BERT์—๋Š” WordPiece Tokenizer๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ—ˆ๊น… ํŽ˜์ด์Šค๋Š” ํ•ด๋‹น ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜์—ฌ tokenizers๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ํ†ตํ•ด ๋ฒ„ํŠธ ์›Œ๋“œ ํ”ผ์Šค ํ† ํฌ ๋‚˜์ด์ €(BertWordPieceTokenizer)๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด๋‹น ํ† ํฌ ๋‚˜์ด์ €์— ํ•™์Šต์‹œํ‚ค๊ณ , ์ด๋กœ๋ถ€ํ„ฐ ์„œ๋ธŒ ์›Œ๋“œ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์„ ์–ป์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž„์˜์˜ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ํ•™์Šต๋œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. import pandas as pd import urllib.request from tokenizers import BertWordPieceTokenizer urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings.txt", filename="ratings.txt") ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece) ์‹ค์Šต์—์„œ ์ง„ํ–‰ํ–ˆ๋˜ ์ „์ฒ˜๋ฆฌ์™€ ๋™์ผํ•œ ๊ณผ์ •์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ratings.txt๋ผ๋Š” ํŒŒ์ผ์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•œ ํ›„, ๊ฒฐ์ธก๊ฐ’์„ ์ œ๊ฑฐํ•˜๊ณ , ์‹ค์งˆ์ ์ธ ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์ธ document ์—ด์— ๋Œ€ํ•ด์„œ naver_review.txt๋ผ๋Š” ํŒŒ์ผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. naver_df = pd.read_table('ratings.txt') naver_df = naver_df.dropna(how='any') with open('naver_review.txt', 'w', encoding='utf8') as f: f.write('\n'.join(naver_df['document'])) ๋ฒ„ํŠธ ์›Œ๋“œ ํ”ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. tokenizer = BertWordPieceTokenizer(lowercase=False, trip_accents=False) ๊ฐ ์ธ์ž๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. lowercase : ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ ์—ฌ๋ถ€. True์ผ ๊ฒฝ์šฐ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Œ. strip_accents : True์ผ ๊ฒฝ์šฐ ์•…์„ผํŠธ ์ œ๊ฑฐ. ex) รฉ โ†’ e, รด โ†’ o ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜์—ฌ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์–ป์–ด๋ด…์‹œ๋‹ค. data_file = 'naver_review.txt' vocab_size = 30000 limit_alphabet = 6000 min_frequency = 5 tokenizer.train(files=data_file, vocab_size=vocab_size, limit_alphabet=limit_alphabet, min_frequency=min_frequency) ๊ฐ ์ธ์ž๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. files : ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์–ป๊ธฐ ์œ„ํ•ด ํ•™์Šตํ•  ๋ฐ์ดํ„ฐ vocab_size : ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ limit_alphabet : ๋ณ‘ํ•ฉ ์ „์˜ ์ดˆ๊ธฐ ํ† ํฐ์˜ ํ—ˆ์šฉ ๊ฐœ์ˆ˜. min_frequency : ์ตœ์†Œ ํ•ด๋‹น ํšŸ์ˆ˜๋งŒํผ ๋“ฑ์žฅํ•œ ์Œ(pair)์˜ ๊ฒฝ์šฐ์—๋งŒ ๋ณ‘ํ•ฉ ๋Œ€์ƒ์ด ๋œ๋‹ค. ํ•™์Šต์ด ๋‹ค ๋˜์—ˆ๋‹ค๋ฉด vocab์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ๋Š” ํ˜„์žฌ ๊ฒฝ๋กœ์— ์ €์žฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # vocab ์ €์žฅ tokenizer.save_model('./') vocab์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. # vocab ๋กœ๋“œ df = pd.read_fwf('vocab.txt', header=None) df ์ด 30,000๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ 30,000์œผ๋กœ ์ง€์ •ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. encoded = tokenizer.encode('์•„ ๋ฐฐ๊ณ ํ”ˆ๋ฐ ์งœ์žฅ๋ฉด ๋จน๊ณ  ์‹ถ๋‹ค') print('ํ† ํฐํ™” ๊ฒฐ๊ณผ :',encoded.tokens) print('์ •์ˆ˜ ์ธ์ฝ”๋”ฉ :',encoded.ids) print('๋””์ฝ”๋”ฉ :',tokenizer.decode(encoded.ids)) ํ† ํฐํ™” ๊ฒฐ๊ณผ : ['์•„', '๋ฐฐ๊ณ ', '##ํ”ˆ', '##๋ฐ', '์งœ์žฅ๋ฉด', '##๋จน๊ณ ', '##์‹ถ๋‹ค'] ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [2111, 20629, 3979, 3244, 24682, 7871, 7379] ๋””์ฝ”๋”ฉ : ์•„ ๋ฐฐ๊ณ ํ”ˆ๋ฐ ์งœ์žฅ๋ฉด ๋จน๊ณ  ์‹ถ๋‹ค .ids๋Š” ์‹ค์งˆ์ ์ธ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. tokens๋Š” ํ•ด๋‹น ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์–ด๋–ป๊ฒŒ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ–ˆ๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. decode()๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณต์›ํ•ฉ๋‹ˆ๋‹ค. encoded = tokenizer.encode('์ปคํ”ผ ํ•œ ์ž”์˜ ์—ฌ์œ ๋ฅผ ์ฆ๊ธฐ๋‹ค') print('ํ† ํฐํ™” ๊ฒฐ๊ณผ :',encoded.tokens) print('์ •์ˆ˜ ์ธ์ฝ”๋”ฉ :',encoded.ids) print('๋””์ฝ”๋”ฉ :',tokenizer.decode(encoded.ids)) ํ† ํฐํ™” ๊ฒฐ๊ณผ : ['์ปคํ”ผ', 'ํ•œ ์ž”', '##์˜', '์—ฌ์œ ', '##๋ฅผ', '์ฆ๊ธฐ', '##๋‹ค'] ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [12825, 25641, 3435, 12696, 3419, 10784, 3260] ๋””์ฝ”๋”ฉ : ์ปคํ”ผ ํ•œ ์ž”์˜ ์—ฌ์œ ๋ฅผ ์ฆ๊ธฐ๋‹ค 2. ๊ธฐํƒ€ ํ† ํฌ ๋‚˜์ด์ € ์ด ์™ธ ByteLevelBPETokenizer, CharBPETokenizer, SentencePieceBPETokenizer ๋“ฑ์ด ์กด์žฌํ•˜๋ฉฐ ์„ ํƒ์— ๋”ฐ๋ผ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BertWordPieceTokenizer : BERT์—์„œ ์‚ฌ์šฉ๋œ ์›Œ๋“œ ํ”ผ์Šค ํ† ํฌ ๋‚˜์ด์ €(WordPiece Tokenizer) CharBPETokenizer : ์˜ค๋ฆฌ์ง€๋„ BPE ByteLevelBPETokenizer : BPE์˜ ๋ฐ”์ดํŠธ ๋ ˆ๋ฒจ ๋ฒ„์ „ SentencePieceBPETokenizer : ์•ž์„œ ๋ณธ ํŒจํ‚ค์ง€ ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece)์™€ ํ˜ธํ™˜๋˜๋Š” BPE ๊ตฌํ˜„์ฒด from tokenizers import ByteLevelBPETokenizer, CharBPETokenizer, SentencePieceBPETokenizer tokenizer = SentencePieceBPETokenizer() tokenizer.train('naver_review.txt', vocab_size=10000, min_frequency=5) encoded = tokenizer.encode("์ด ์˜ํ™”๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ์Šต๋‹ˆ๋‹ค.") print(encoded.tokens) ['โ–์ด', 'โ–์˜ํ™”๋Š”', 'โ–์ •๋ง', 'โ–์žฌ๋ฏธ์žˆ', '์Šต๋‹ˆ๋‹ค.'] 14. RNN์„ ์ด์šฉํ•œ ์ธ์ฝ”๋”-๋””์ฝ”๋” ์•ž์„œ RNN์˜ ๋‹ค ๋Œ€ ์ผ(many-to-one) ๊ตฌ์กฐ๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ํ’€ ์ˆ˜ ์žˆ์—ˆ๊ณ , ๋‹ค ๋Œ€๋‹ค(many-to-many) ๊ตฌ์กฐ๋กœ๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹์ด๋‚˜ ํ’ˆ์‚ฌ ํƒœ๊น…๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์— ์‚ดํŽด๋ณผ RNN์˜ ๊ตฌ์กฐ๋Š” ์•ž์—์„œ ์‚ดํŽด๋ณธ ๊ตฌ์กฐ์™€๋Š” ๋‹ค์†Œ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”๋ฐ, ํ•˜๋‚˜์˜ RNN์„ ์ธ์ฝ”๋”. ๋˜ ๋‹ค๋ฅธ ํ•˜๋‚˜์˜ RNN์„ ๋””์ฝ”๋”๋ผ๋Š” ๋ชจ๋“ˆ๋กœ ๋ช…๋ช…ํ•˜๊ณ  ๋‘ ๊ฐœ์˜ RNN์„ ์—ฐ๊ฒฐํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ์ธ์ฝ”๋”-๋””์ฝ”๋” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๊ตฌ์กฐ๋Š” ์ฃผ๋กœ ์ž…๋ ฅ ๋ฌธ์žฅ๊ณผ ์ถœ๋ ฅ ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๋Œ€ํ‘œ์ ์ธ ๋ถ„์•ผ๊ฐ€ ๋ฒˆ์—ญ๊ธฐ๋‚˜ ํ…์ŠคํŠธ ์š”์•ฝ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด ๋ฌธ์žฅ์„ ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์œผ๋กœ ๋ฒˆ์—ญํ•œ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ž…๋ ฅ ๋ฌธ์žฅ์ธ ์˜์–ด ๋ฌธ์žฅ๊ณผ ๋ฒˆ์—ญ๋œ ๊ฒฐ๊ณผ์ธ ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์˜ ๊ธธ์ด๋Š” ๋˜‘๊ฐ™์„ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์š”์•ฝ์˜ ๊ฒฝ์šฐ์—๋Š” ์ถœ๋ ฅ ๋ฌธ์žฅ์ด ์š”์•ฝ๋œ ๋ฌธ์žฅ์ด๋ฏ€๋กœ ์ž…๋ ฅ ๋ฌธ์žฅ๋ณด๋‹ค๋Š” ๋‹น์—ฐํžˆ ๊ธธ์ด๊ฐ€ ์งง์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” RNN์˜ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๊ฐ€ ๊ฐ๊ฐ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•˜์—ฌ ์ž…๋ ฅ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์ถœ๋ ฅ ๋ฌธ์žฅ์„ ์—ฐ์‚ฐํ•ด ๋‚ด๋Š”์ง€ ๋ฒˆ์—ญ๊ธฐ ๊ตฌํ˜„ ํ”„๋กœ์ ํŠธ๋ฅผ ํ†ตํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฒˆ์—ญ์ด๋ผ๋Š” ์„ฌ์„ธํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ํƒœ์Šคํฌ๋ฅผ ๊ธฐ๊ณ„์ ์œผ๋กœ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ธ BLEU(Bilingual Evaluation Understudy Score)๋ผ๋Š” ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 14-01 ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค(Sequence-to-Sequence, seq2seq) ์ด๋ฒˆ ์‹ค์Šต์€ ์ผ€๋ผ์Šค ํ•จ์ˆ˜ํ˜• API์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ํ˜• API(functional API, https://wikidocs.net/38861 )์— ๋Œ€ํ•ด์„œ ์šฐ์„  ์ˆ™์ง€ ํ›„ ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ์ฃผ์„ธ์š”. ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค(Sequence-to-Sequence, seq2seq)๋Š” ์ž…๋ ฅ๋œ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ๋„๋ฉ”์ธ์˜ ์‹œํ€€์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ฑ—๋ด‡(Chatbot)๊ณผ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ(Machine Translation)์ด ๊ทธ๋Ÿฌํ•œ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ธ๋ฐ, ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ๊ฐ๊ฐ ์งˆ๋ฌธ๊ณผ ๋Œ€๋‹ต์œผ๋กœ ๊ตฌ์„ฑํ•˜๋ฉด ์ฑ—๋ด‡์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ๊ฐ๊ฐ ์ž…๋ ฅ ๋ฌธ์žฅ๊ณผ ๋ฒˆ์—ญ ๋ฌธ์žฅ์œผ๋กœ ๋งŒ๋“ค๋ฉด ๋ฒˆ์—ญ๊ธฐ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋„ ๋‚ด์šฉ ์š”์•ฝ(Text Summarization), STT(Speech to Text) ๋“ฑ์—์„œ ์“ฐ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ธฐ๊ณ„ ๋ฒˆ์—ญ์„ ์˜ˆ์ œ๋กœ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ค„์—ฌ์„œ seq2seq์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ช…๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค(Sequence-to-Sequence) seq2seq๋Š” ๋ฒˆ์—ญ๊ธฐ์—์„œ ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ์„ค๋ช… ๋ฐฉ์‹์€ ๋‚ด๋ถ€๊ฐ€ ๋ณด์ด์ง€ ์•Š๋Š” ์ปค๋‹ค๋ž€ ๋ธ”๋ž™๋ฐ•์Šค์—์„œ ์ ์ฐจ์ ์œผ๋กœ ํ™•๋Œ€ํ•ด๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์„ค๋ช…ํ•˜๋Š” ๋‚ด์šฉ์˜ ๋Œ€๋ถ€๋ถ„์€ RNN ์ฑ•ํ„ฐ์—์„œ ์–ธ๊ธ‰ํ•œ ๋‚ด์šฉ๋“ค๋กœ ๋‹จ์ง€ RNN์„ ์–ด๋–ป๊ฒŒ ์กฐ๋ฆฝํ–ˆ๋Š๋ƒ์— ๋”ฐ๋ผ์„œ seq2seq๋ผ๋Š” ๊ตฌ์กฐ๊ฐ€ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ seq2seq ๋ชจ๋ธ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋ฒˆ์—ญ๊ธฐ๊ฐ€ 'I am a student'๋ผ๋Š” ์˜์–ด ๋ฌธ์žฅ์„ ์ž…๋ ฅ๋ฐ›์•„์„œ, 'je suis รฉtudiant'๋ผ๋Š” ํ”„๋ž‘์Šค ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, seq2seq ๋ชจ๋ธ ๋‚ด๋ถ€์˜ ๋ชจ์Šต์€ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์—ˆ์„๊นŒ์š”? seq2seq๋Š” ํฌ๊ฒŒ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋ผ๋Š” ๋‘ ๊ฐœ์˜ ๋ชจ๋“ˆ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ž…๋ ฅ๋ฐ›์€ ๋’ค์— ๋งˆ์ง€๋ง‰์— ์ด ๋ชจ๋“  ๋‹จ์–ด ์ •๋ณด๋“ค์„ ์••์ถ•ํ•ด์„œ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š”๋ฐ, ์ด๋ฅผ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ(context vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ์ •๋ณด๊ฐ€ ํ•˜๋‚˜์˜ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋กœ ๋ชจ๋‘ ์••์ถ•๋˜๋ฉด ์ธ์ฝ”๋”๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๋””์ฝ”๋”๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ๋ฒˆ์—ญ๋œ ๋‹จ์–ด๋ฅผ ํ•œ ๊ฐœ์”ฉ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ๋‹ค์‹œ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ 4์˜ ์‚ฌ์ด์ฆˆ๋กœ ํ‘œํ˜„ํ•˜์˜€์ง€๋งŒ, ์‹ค์ œ ํ˜„์—…์—์„œ ์‚ฌ์šฉ๋˜๋Š” seq2seq ๋ชจ๋ธ์—์„œ๋Š” ๋ณดํ†ต ์ˆ˜๋ฐฑ ์ด์ƒ์˜ ์ฐจ์›์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ๋‚ด๋ถ€๋ฅผ ์ข€ ๋” ํ™•๋Œ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋” ์•„ํ‚คํ…์ฒ˜์™€ ๋””์ฝ”๋” ์•„ํ‚คํ…์ฒ˜์˜ ๋‚ด๋ถ€๋Š” ์‚ฌ์‹ค ๋‘ ๊ฐœ์˜ RNN ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฌธ์žฅ์„ ๋ฐ›๋Š” RNN ์…€์„ ์ธ์ฝ”๋”๋ผ๊ณ  ํ•˜๊ณ , ์ถœ๋ ฅ ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋Š” RNN ์…€์„ ๋””์ฝ”๋”๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ธ์ฝ”๋”์˜ RNN ์…€์„ ์ฃผํ™ฉ์ƒ‰์œผ๋กœ, ๋””์ฝ”๋”์˜ RNN ์…€์„ ์ดˆ๋ก์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์„ฑ๋Šฅ ๋ฌธ์ œ๋กœ ์ธํ•ด ์‹ค์ œ๋กœ๋Š” ๋ฐ”๋‹๋ผ RNN์ด ์•„๋‹ˆ๋ผ LSTM ์…€ ๋˜๋Š” GRU ์…€๋“ค๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ธ์ฝ”๋”๋ฅผ ์ž์„ธํžˆ ๋ณด๋ฉด, ์ž…๋ ฅ ๋ฌธ์žฅ์€ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ํ†ตํ•ด์„œ ๋‹จ์–ด ๋‹จ์œ„๋กœ ์ชผ๊ฐœ์ง€๊ณ  ๋‹จ์–ด ํ† ํฐ ๊ฐ๊ฐ์€ RNN ์…€์˜ ๊ฐ ์‹œ์ ์˜ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋” RNN ์…€์€ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋’ค์— ์ธ์ฝ”๋” RNN ์…€์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๋””์ฝ”๋” RNN ์…€๋กœ ๋„˜๊ฒจ์ฃผ๋Š”๋ฐ ์ด๋ฅผ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋Š” ๋””์ฝ”๋” RNN ์…€์˜ ์ฒซ ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ RNNLM(RNN Language Model)์ž…๋‹ˆ๋‹ค. RNNLM์˜ ๊ฐœ๋…์„ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์ข€ ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” ์ดˆ๊ธฐ ์ž…๋ ฅ์œผ๋กœ ๋ฌธ์žฅ์˜ ์‹œ์ž‘์„ ์˜๋ฏธํ•˜๋Š” ์‹ฌ๋ฒŒ <sos>๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” <sos>๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด, ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ํ™•๋ฅ ์ด ๋†’์€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์‹œ์ (time step)์˜ ๋””์ฝ”๋” RNN ์…€์€ ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ๋‹จ์–ด๋กœ je๋ฅผ ์˜ˆ์ธกํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์˜ ๋””์ฝ”๋” RNN ์…€์€ ์˜ˆ์ธก๋œ ๋‹จ์–ด je๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ RNN ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์‹œ์ ์˜ ๋””์ฝ”๋” RNN ์…€์€ ์ž…๋ ฅ๋œ ๋‹จ์–ด je๋กœ๋ถ€ํ„ฐ ๋‹ค์‹œ ๋‹ค์Œ์— ์˜ฌ ๋‹จ์–ด์ธ suis๋ฅผ ์˜ˆ์ธกํ•˜๊ณ , ๋˜๋‹ค์‹œ ์ด๊ฒƒ์„ ๋‹ค์Œ ์‹œ์ ์˜ RNN ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์Œ์— ์˜ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ณ , ๊ทธ ์˜ˆ์ธกํ•œ ๋‹จ์–ด๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ RNN ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ๋Š” ํ–‰์œ„๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ํ–‰์œ„๋Š” ๋ฌธ์žฅ์˜ ๋์„ ์˜๋ฏธํ•˜๋Š” ์‹ฌ๋ฒŒ์ธ <eos>๊ฐ€ ๋‹ค์Œ ๋‹จ์–ด๋กœ ์˜ˆ์ธก๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์€ ํ…Œ์ŠคํŠธ ๊ณผ์ • ๋™์•ˆ์˜ ์ด์•ผ๊ธฐ์ž…๋‹ˆ๋‹ค. seq2seq๋Š” ํ›ˆ๋ จ ๊ณผ์ •๊ณผ ํ…Œ์ŠคํŠธ ๊ณผ์ •(๋˜๋Š” ์‹ค์ œ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ์‚ฌ๋žŒ์ด ์“ธ ๋•Œ)์˜ ์ž‘๋™ ๋ฐฉ์‹์ด ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ๋””์ฝ”๋”์—๊ฒŒ ์ธ์ฝ”๋”๊ฐ€ ๋ณด๋‚ธ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์™€ ์‹ค์ œ ์ •๋‹ต์ธ ์ƒํ™ฉ์ธ <sos> je suis รฉtudiant๋ฅผ ์ž…๋ ฅ๋ฐ›์•˜์„ ๋•Œ, je suis รฉtudiant <eos>๊ฐ€ ๋‚˜์™€์•ผ ๋œ๋‹ค๊ณ  ์ •๋‹ต์„ ์•Œ๋ ค์ฃผ๋ฉด์„œ ํ›ˆ๋ จํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์— ๊ต์‚ฌ ๊ฐ•์š”(teacher forcing)๋ฅผ ์„ค๋ช…ํ•˜๋ฉด์„œ ์žฌ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ๋Š” ์•ž์„œ ์„ค๋ช…ํ•œ ๊ณผ์ •๊ณผ ๊ฐ™์ด ๋””์ฝ”๋”๋Š” ์˜ค์ง ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์™€ <sos>๋งŒ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์€ ํ›„์— ๋‹ค์Œ์— ์˜ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ณ , ๊ทธ ๋‹จ์–ด๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ RNN ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ๋Š” ํ–‰์œ„๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์•ž์„œ ์„ค๋ช…ํ•œ ๊ณผ์ •๊ณผ ์œ„์˜ ๊ทธ๋ฆผ์€ ํ…Œ์ŠคํŠธ ๊ณผ์ •์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ž…, ์ถœ๋ ฅ์— ์“ฐ์ด๋Š” ๋‹จ์–ด ํ† ํฐ๋“ค์ด ์žˆ๋Š” ๋ถ€๋ถ„์„ ์ข€ ๋” ํ™•๋Œ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ํ…์ŠคํŠธ๋ณด๋‹ค ์ˆซ์ž๋ฅผ ์ž˜ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํ…์ŠคํŠธ๋ฅผ ๋ฒกํ„ฐ๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ฃผ๋กœ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด ์‚ฌ์šฉ๋œ๋‹ค๊ณ  ์„ค๋ช…ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, seq2seq์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“  ๋‹จ์–ด๋“ค์€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ ํ›„ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ž„๋ฒ ๋”ฉ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ฒŒ ํ•˜๋Š” ๋‹จ๊ณ„์ธ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด I, am, a, student๋ผ๋Š” ๋‹จ์–ด๋“ค์— ๋Œ€ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ์œ„์™€ ๊ฐ™์€ ๋ชจ์Šต์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ ์ž ์‚ฌ์ด์ฆˆ๋ฅผ 4๋กœ ํ•˜์˜€์ง€๋งŒ, ๋ณดํ†ต ์‹ค์ œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ์ˆ˜๋ฐฑ ๊ฐœ์˜ ์ฐจ์›์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. RNN ์…€์— ๋Œ€ํ•ด์„œ ํ™•๋Œ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ์„ค๋ช…ํ•˜์˜€์ง€๋งŒ, ํ•˜๋‚˜์˜ RNN ์…€์€ ๊ฐ๊ฐ์˜ ์‹œ์ (time step)๋งˆ๋‹ค ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์‹œ์ (time step)์„ t๋ผ๊ณ  ํ•  ๋•Œ, RNN ์…€์€ t-1์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ์™€ t์—์„œ์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ณ , t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋•Œ t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋Š” ๋ฐ”๋กœ ์œ„์— ๋˜ ๋‹ค๋ฅธ ์€๋‹‰์ธต์ด๋‚˜ ์ถœ๋ ฅ์ธต์ด ์กด์žฌํ•  ๊ฒฝ์šฐ์—๋Š” ์œ„์˜ ์ธต์œผ๋กœ ๋ณด๋‚ด๊ฑฐ๋‚˜, ํ•„์š” ์—†์œผ๋ฉด ๊ฐ’์„ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  RNN ์…€์€ ๋‹ค์Œ ์‹œ์ ์— ํ•ด๋‹นํ•˜๋Š” t+1์˜ RNN ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ํ˜„์žฌ t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. RNN ์ฑ•ํ„ฐ์—์„œ๋„ ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ, ์ด๋Ÿฐ ๊ตฌ์กฐ์—์„œ ํ˜„์žฌ ์‹œ์  t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋Š” ๊ณผ๊ฑฐ ์‹œ์ ์˜ ๋™์ผํ•œ RNN ์…€์—์„œ์˜ ๋ชจ๋“  ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’๋“ค์˜ ์˜ํ–ฅ์„ ๋ˆ„์ ํ•ด์„œ ๋ฐ›์•„์˜จ ๊ฐ’์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋˜ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋Š” ์‚ฌ์‹ค ์ธ์ฝ”๋”์—์„œ์˜ ๋งˆ์ง€๋ง‰ RNN ์…€์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ ๋งํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ์ด๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด ํ† ํฐ๋“ค์˜ ์ •๋ณด๋ฅผ ์š”์•ฝํ•ด์„œ ๋‹ด๊ณ  ์žˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ RNN ์…€์˜ ์€๋‹‰ ์ƒํƒœ์ธ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ์ฒซ ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ RNN ์…€์€ ์ด ์ฒซ ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’๊ณผ, ํ˜„์žฌ t์—์„œ์˜ ์ž…๋ ฅ๊ฐ’์ธ <sos>๋กœ๋ถ€ํ„ฐ, ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์˜ˆ์ธก๋œ ๋‹จ์–ด๋Š” ๋‹ค์Œ ์‹œ์ ์ธ t+1 RNN์—์„œ์˜ ์ž…๋ ฅ๊ฐ’์ด ๋˜๊ณ , ์ด t+1์—์„œ์˜ RNN ๋˜ํ•œ ์ด ์ž…๋ ฅ๊ฐ’๊ณผ t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋กœ๋ถ€ํ„ฐ t+1์—์„œ์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ. ์ฆ‰, ๋˜๋‹ค์‹œ ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋””์ฝ”๋”๊ฐ€ ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ถ€๋ถ„์„ ํ™•๋Œ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๋‹จ์–ด๋กœ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๋“ค์€ ๋‹ค์–‘ํ•œ ๋‹จ์–ด๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. seq2seq ๋ชจ๋ธ์€ ์„ ํƒ๋  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ ๊ณจ๋ผ์„œ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ์“ธ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋กœ๋Š” ๋ญ๊ฐ€ ์žˆ์„๊นŒ์š”? ๋ฐ”๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋””์ฝ”๋”์—์„œ ๊ฐ ์‹œ์ (time step)์˜ RNN ์…€์—์„œ ์ถœ๋ ฅ ๋ฒกํ„ฐ๊ฐ€ ๋‚˜์˜ค๋ฉด, ํ•ด๋‹น ๋ฒกํ„ฐ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅ ์‹œํ€€์Šค์˜ ๊ฐ ๋‹จ์–ด๋ณ„ ํ™•๋ฅ  ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๋””์ฝ”๋”๋Š” ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ seq2seq์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›Œ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค seq2seq๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ์„œ ์ถฉ๋ถ„ํžˆ ๋” ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๋””์ฝ”๋”์˜ ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋กœ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๊ฑฐ๊ธฐ์„œ ๋” ๋‚˜์•„๊ฐ€ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๋””์ฝ”๋”๊ฐ€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋งค ์‹œ์ ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ ๊ฑฐ๊ธฐ์„œ ๋” ๋‚˜์•„๊ฐ€๋ฉด ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์ง€๊ธˆ ์•Œ๊ณ  ์žˆ๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ณด๋‹ค ๋”์šฑ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•˜์—ฌ ๋งค ์‹œ์ ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. 2. ๋ฌธ์ž ๋ ˆ๋ฒจ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ(Character-Level Neural Machine Translation) ๊ตฌํ˜„ํ•˜๊ธฐ seq2seq๋ฅผ ์ด์šฉํ•ด์„œ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์€ ๊ฒŒ์‹œ๋ฌผ์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ธํ„ฐ๋„ท์— ์ผ€๋ผ์Šค๋กœ seq2seq๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋งŽ์€ ์œ ์‚ฌ ์˜ˆ์ œ๋“ค์ด ๋‚˜์™€์žˆ์ง€๋งŒ ๋Œ€๋ถ€๋ถ„์€ ์ผ€๋ผ์Šค ๊ฐœ๋ฐœ์ž ํ”„๋ž‘์ˆ˜์•„ ์ˆ„๋ ˆ์˜ ๋ธ”๋กœ๊ทธ์˜ ์œ ๋ช… ๊ฒŒ์‹œ๋ฌผ์ธ 'sequence-to-sequence 10๋ถ„ ๋งŒ์— ์ดํ•ดํ•˜๊ธฐ'๊ฐ€ ์›๋ณธ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต ๋˜ํ•œ ํ•ด๋‹น ๊ฒŒ์‹œ๋ฌผ์˜ ์˜ˆ์ œ์— ๋งŽ์ด ์˜ํ–ฅ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฒŒ์‹œ๋ฌผ ๋งํฌ : https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html ์‹ค์ œ ์„ฑ๋Šฅ์ด ์ข‹์€ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ์ •๋ง ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋Š” ๋ฐฉ๊ธˆ ๋ฐฐ์šด seq2seq๋ฅผ ์‹ค์Šตํ•ด ๋ณด๋Š” ์ˆ˜์ค€์—์„œ ์•„์ฃผ ๊ฐ„๋‹จํ•œ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๊ตฌ์ถ•ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค(parallel corpus)๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค๋ž€, ๋‘ ๊ฐœ ์ด์ƒ์˜ ์–ธ์–ด๊ฐ€ ๋ณ‘๋ ฌ์ ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ฝ”ํผ์Šค๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ๋งํฌ : http://www.manythings.org/anki ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ”„๋ž‘์Šค-์˜์–ด ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค์ธ fra-eng.zip ํŒŒ์ผ์„ ์‚ฌ์šฉํ•  ๊ฒ๋‹ˆ๋‹ค. ์œ„์˜ ๋งํฌ์—์„œ ํ•ด๋‹น ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํŒŒ์ผ์˜ ์••์ถ•์„ ํ’€๋ฉด fra.txt๋ผ๋Š” ํŒŒ์ผ์ด ์žˆ๋Š”๋ฐ ์ด ํŒŒ์ผ์ด ์ด๋ฒˆ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•  ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. 1) ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ์šฐ์„  ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ๋ผ๊ณ  ํ•˜๋ฉด ์•ž์„œ ์ˆ˜ํ–‰ํ•œ ํƒœ๊น… ์ž‘์—…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์•ž์„œ ์ˆ˜ํ–‰ํ•œ ํƒœ๊น… ์ž‘์—…์˜ ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ์™€ seq2seq๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ๋Š” ์„ฑ๊ฒฉ์ด ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํƒœ๊น… ์ž‘์—…์˜ ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ๋Š” ์Œ์ด ๋˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ๊ธธ์ด๊ฐ€ ๊ฐ™์•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ์Œ์ด ๋œ๋‹ค๊ณ  ํ•ด์„œ ๊ธธ์ด๊ฐ€ ๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๊ตฌ๊ธ€ ๋ฒˆ์—ญ๊ธฐ์— '๋‚˜๋Š” ํ•™์ƒ์ด๋‹ค.'๋ผ๋Š” ํ† ํฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 2์ธ ๋ฌธ์žฅ์„ ๋„ฃ์—ˆ์„ ๋•Œ 'I am a student.'๋ผ๋Š” ํ† ํฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 4์ธ ๋ฌธ์žฅ์ด ๋‚˜์˜ค๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์ด์น˜์ž…๋‹ˆ๋‹ค. seq2seq๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์ถœ๋ ฅ ์‹œํ€€์Šค์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๊ฐ€ ์˜ˆ์ œ์ง€๋งŒ seq2seq์˜ ๋˜ ๋‹ค๋ฅธ ์œ ๋ช…ํ•œ ์˜ˆ์ œ ์ค‘ ํ•˜๋‚˜์ธ ์ฑ—๋ด‡์„ ๋งŒ๋“ ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๋ฉด, ๋Œ€๋‹ต์˜ ๊ธธ์ด๊ฐ€ ์งˆ๋ฌธ์˜ ๊ธธ์ด์™€ ํ•ญ์ƒ ๋˜‘๊ฐ™์•„์•ผ ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๊ทธ ๋˜ํ•œ ์ด์ƒํ•ฉ๋‹ˆ๋‹ค. Watch me. Regardez-moi ! ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  fra.txt ๋ฐ์ดํ„ฐ๋Š” ์œ„์™€ ๊ฐ™์ด ์™ผ์ชฝ์˜ ์˜์–ด ๋ฌธ์žฅ๊ณผ ์˜ค๋ฅธ์ชฝ์˜ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ ์‚ฌ์ด์— ํƒญ์œผ๋กœ ๊ตฌ๋ถ„๋˜๋Š” ๊ตฌ์กฐ๊ฐ€ ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์™€ ๊ฐ™์€<NAME>์˜ ์•ฝ 16๋งŒ ๊ฐœ์˜ ๋ณ‘๋ ฌ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ์ฝ”๋“œ์—์„œ src๋Š” source์˜ ์ค„์ž„๋ง๋กœ ์ž…๋ ฅ ๋ฌธ์žฅ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, tar๋Š” target์˜ ์ค„์ž„๋ง๋กœ ๋ฒˆ์—ญํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์žฅ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. import os import shutil import zipfile import pandas as pd import tensorflow as tf import urllib3 from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' } def download_zip(url, output_path): response = requests.get(url, headers=headers, stream=True) if response.status_code == 200: with open(output_path, 'wb') as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk) print(f"ZIP file downloaded to {output_path}") else: print(f"Failed to download. HTTP Response Code: {response.status_code}") url = "http://www.manythings.org/anki/fra-eng.zip" output_path = "fra-eng.zip" download_zip(url, output_path) path = os.getcwd() zipfilename = os.path.join(path, output_path) with zipfile.ZipFile(zipfilename, 'r') as zip_ref: zip_ref.extractall(path) lines = pd.read_csv('fra.txt', names=['src', 'tar', 'lic'], sep='\t') del lines['lic'] print('์ „์ฒด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ :',len(lines)) ์ „์ฒด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 191954 ์ „์ฒด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋Š” ์ด์•ฝ 19๋งŒ 2์ฒœ ๊ฐœ์ž…๋‹ˆ๋‹ค. lines = lines.loc[:, 'src':'tar'] lines = lines[0:60000] # 6๋งŒ ๊ฐœ๋งŒ ์ €์žฅ lines.sample(10) ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ์•ฝ 19๋งŒ 2์ฒœ ๊ฐœ์˜ ๋ณ‘๋ ฌ ๋ฌธ์žฅ ์ƒ˜ํ”Œ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํžˆ 60,000๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ๊ฐ€์ง€๊ณ  ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๊ตฌ์ถ•ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ „์ฒด ๋ฐ์ดํ„ฐ ์ค‘ 60,000๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ €์žฅํ•˜๊ณ  ํ˜„์žฌ ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค ๊ตฌ์„ฑ์ด ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ํ…Œ์ด๋ธ”์€ ๋žœ๋ค์œผ๋กœ ์„ ํƒ๋œ 10๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฒˆ์—ญ ๋ฌธ์žฅ์— ํ•ด๋‹น๋˜๋Š” ํ”„๋ž‘์Šค์–ด ๋ฐ์ดํ„ฐ๋Š” ์•ž์„œ ๋ฐฐ์› ๋“ฏ์ด ์‹œ์ž‘์„ ์˜๋ฏธํ•˜๋Š” ์‹ฌ๋ฒŒ <sos>๊ณผ ์ข…๋ฃŒ๋ฅผ ์˜๋ฏธํ•˜๋Š” ์‹ฌ๋ฒŒ <eos>์„ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” <sos>์™€ <eos> ๋Œ€์‹  \t๋ฅผ ์‹œ์ž‘ ์‹ฌ๋ฒŒ, \n์„ ์ข…๋ฃŒ ์‹ฌ๋ฒŒ๋กœ ๊ฐ„์ฃผํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜๊ณ  ๋‹ค์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. lines.tar = lines.tar.apply(lambda x : '\t '+ x + ' \n') lines.sample(10) ๋žœ๋ค์œผ๋กœ 10๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์„ ํƒํ•˜์—ฌ ์ถœ๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ”„๋ž‘์Šค์–ด ๋ฐ์ดํ„ฐ์—์„œ ์‹œ์ž‘ ์‹ฌ๋ฒŒ๊ณผ ์ข…๋ฃŒ ์‹ฌ๋ฒŒ์ด ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์•„๋‹ˆ๋ผ ๋ฌธ์ž ์ง‘ํ•ฉ์ด๋ผ๊ณ  ํ•˜๋Š” ์ด์œ ๋Š” ํ† ํฐ ๋‹จ์œ„๊ฐ€ ๋‹จ์–ด๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฌธ์ž์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. # ๋ฌธ์ž ์ง‘ํ•ฉ ๊ตฌ์ถ• src_vocab = set() for line in lines.src: # 1์ค„์”ฉ ์ฝ์Œ for char in line: # 1๊ฐœ์˜ ๋ฌธ์ž์”ฉ ์ฝ์Œ src_vocab.add(char) tar_vocab = set() for line in lines.tar: for char in line: tar_vocab.add(char) ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. src_vocab_size = len(src_vocab)+1 tar_vocab_size = len(tar_vocab)+1 print('source ๋ฌธ์žฅ์˜ char ์ง‘ํ•ฉ :',src_vocab_size) print('target ๋ฌธ์žฅ์˜ char ์ง‘ํ•ฉ :',tar_vocab_size) source ๋ฌธ์žฅ์˜ char ์ง‘ํ•ฉ : 79 target ๋ฌธ์žฅ์˜ char ์ง‘ํ•ฉ : 105 ์˜์–ด์™€ ํ”„๋ž‘์Šค์–ด๋Š” ๊ฐ๊ฐ 79๊ฐœ์™€ 105๊ฐœ์˜ ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด ์ค‘์—์„œ ์ธ๋ฑ์Šค๋ฅผ ์ž„์˜๋กœ ๋ถ€์—ฌํ•˜์—ฌ ์ผ๋ถ€๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. ํ˜„ ์ƒํƒœ์—์„œ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ •๋ ฌํ•˜์—ฌ ์ˆœ์„œ๋ฅผ ์ •ํ•ด์ค€ ๋’ค์— ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. src_vocab = sorted(list(src_vocab)) tar_vocab = sorted(list(tar_vocab)) print(src_vocab[45:75]) print(tar_vocab[45:75]) ['W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] ['T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w'] ๋ฌธ์ž ์ง‘ํ•ฉ์— ๋ฌธ์ž ๋‹จ์œ„๋กœ ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฌธ์ž์— ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. src_to_index = dict([(word, i+1) for i, word in enumerate(src_vocab)]) tar_to_index = dict([(word, i+1) for i, word in enumerate(tar_vocab)]) print(src_to_index) print(tar_to_index) {' ': 1, '!': 2, '"': 3, '$': 4, '%': 5, ... ์ค‘๋žต ... 'x': 73, 'y': 74, 'z': 75, 'รฉ': 76, 'โ€™': 77, 'โ‚ฌ': 78} {'\t': 1, '\n': 2, ' ': 3, '!': 4, '"': 5, ... ์ค‘๋žต ... 'รป': 98, 'ล“': 99, 'ะก': 100, '\u2009': 101, 'โ€˜': 102, 'โ€™': 103, '\u202f': 104} ์ธ๋ฑ์Šค๊ฐ€ ๋ถ€์—ฌ๋œ ๋ฌธ์ž ์ง‘ํ•ฉ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ–๊ณ  ์žˆ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์ด ๋  ์˜์–ด ๋ฌธ์žฅ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ณ , 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. encoder_input = [] # 1๊ฐœ์˜ ๋ฌธ์žฅ for line in lines.src: encoded_line = [] # ๊ฐ ์ค„์—์„œ 1๊ฐœ์˜ char for char in line: # ๊ฐ char์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ encoded_line.append(src_to_index[char]) encoder_input.append(encoded_line) print('source ๋ฌธ์žฅ์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ :',encoder_input[:5]) source ๋ฌธ์žฅ์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [[30, 64, 10], [30, 64, 10], [30, 64, 10], [31, 58, 10], [31, 58, 10]] ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์ด ๋  ํ”„๋ž‘์Šค์–ด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. decoder_input = [] for line in lines.tar: encoded_line = [] for char in line: encoded_line.append(tar_to_index[char]) decoder_input.append(encoded_line) print('target ๋ฌธ์žฅ์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ :',decoder_input[:5]) target ๋ฌธ์žฅ์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [[1, 3, 48, 53, 3, 4, 3, 2], [1, 3, 39, 53, 70, 55, 60, 57, 14, 3, 2], [1, 3, 28, 67, 73, 59, 57, 3, 4, 3, 2], [1, 3, 45, 53, 64, 73, 72, 3, 4, 3, 2], [1, 3, 45, 53, 64, 73, 72, 14, 3, 2]] ์ •์ƒ์ ์œผ๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„์ง ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ํ•˜๋‚˜ ๋” ๋‚จ์•˜์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์˜ˆ์ธก๊ฐ’๊ณผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•œ ์‹ค์ œ ๊ฐ’์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ์‹ค์ œ ๊ฐ’์—๋Š” ์‹œ์ž‘ ์‹ฌ๋ฒŒ์— ํ•ด๋‹น๋˜๋Š” <sos>๊ฐ€ ์žˆ์„ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ด์ „ ํŽ˜์ด์ง€์˜ ๊ทธ๋ฆผ์œผ๋กœ ๋Œ์•„๊ฐ€ Dense์™€ Softmax ์œ„์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ๋‹ค์‹œ ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋ฒˆ์—๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ <sos>๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ชจ๋“  ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ์˜ ๋งจ ์•ž์— ๋ถ™์–ด์žˆ๋Š” '\t'๋ฅผ ์ œ๊ฑฐํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. decoder_target = [] for line in lines.tar: timestep = 0 encoded_line = [] for char in line: if timestep > 0: encoded_line.append(tar_to_index[char]) timestep = timestep + 1 decoder_target.append(encoded_line) print('target ๋ฌธ์žฅ ๋ ˆ์ด๋ธ”์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ :',decoder_target[:5]) target ๋ฌธ์žฅ ๋ ˆ์ด๋ธ”์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [[3, 48, 53, 3, 4, 3, 2], [3, 39, 53, 70, 55, 60, 57, 14, 3, 2], [3, 28, 67, 73, 59, 57, 3, 4, 3, 2], [3, 45, 53, 64, 73, 72, 3, 4, 3, 2], [3, 45, 53, 64, 73, 72, 14, 3, 2]] ์•ž์„œ ๋จผ์ € ๋งŒ๋“ค์—ˆ๋˜ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ๊ฐ’์— ํ•ด๋‹น๋˜๋Š” decoder_input ๋ฐ์ดํ„ฐ์™€ ๋น„๊ตํ•˜๋ฉด decoder_input์—์„œ๋Š” ๋ชจ๋“  ๋ฌธ์žฅ์˜ ์•ž์— ๋ถ™์–ด์žˆ๋˜ ์ˆซ์ž 1์ด decoder_target์—์„œ๋Š” ์ œ๊ฑฐ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. '\t'๊ฐ€ ์ธ๋ฑ์Šค๊ฐ€ 1์ด๋ฏ€๋กœ ์ •์ƒ์ ์œผ๋กœ ์ œ๊ฑฐ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ๋ณ€๊ฒฝํ•˜์˜€์œผ๋‹ˆ ํŒจ๋”ฉ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํŒจ๋”ฉ์„ ์œ„ํ•ด์„œ ์˜์–ด ๋ฌธ์žฅ๊ณผ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ ๊ฐ๊ฐ์— ๋Œ€ํ•ด์„œ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. max_src_len = max([len(line) for line in lines.src]) max_tar_len = max([len(line) for line in lines.tar]) print('source ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max_src_len) print('target ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max_tar_len) source ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 23 target ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 76 ๊ฐ๊ฐ 23์™€ 76์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋ฒˆ ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ๋Š” ์˜์–ด์™€ ํ”„๋ž‘์Šค์–ด์˜ ๊ธธ์ด๋Š” ํ•˜๋‚˜์˜ ์Œ์ด๋ผ๊ณ  ํ•˜๋”๋ผ๋„ ์ „๋ถ€ ๋‹ค๋ฅด๋ฏ€๋กœ ํŒจ๋”ฉ์„ ํ•  ๋•Œ๋„ ์ด ๋‘ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋ฅผ ์ „๋ถ€ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ค„ ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์˜์–ด ๋ฐ์ดํ„ฐ๋Š” ์˜์–ด ์ƒ˜ํ”Œ๋“ค๋ผ๋ฆฌ, ํ”„๋ž‘์Šค์–ด๋Š” ํ”„๋ž‘์Šค์–ด ์ƒ˜ํ”Œ๋“ค๋ผ๋ฆฌ ๊ธธ์ด๋ฅผ ๋งž์ถ”์–ด์„œ ํŒจ๋”ฉ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด์— ๋งž์ถฐ์„œ ์˜์–ด ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ์€ ์ „๋ถ€ ๊ธธ์ด๊ฐ€ 23์ด ๋˜๋„๋ก ํŒจ๋”ฉํ•˜๊ณ , ํ”„๋ž‘์Šค์–ด ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ์€ ์ „๋ถ€ ๊ธธ์ด๊ฐ€ 76์ด ๋˜๋„๋ก ํŒจ๋”ฉ ํ•ฉ๋‹ˆ๋‹ค. encoder_input = pad_sequences(encoder_input, maxlen=max_src_len, padding='post') decoder_input = pad_sequences(decoder_input, maxlen=max_tar_len, padding='post') decoder_target = pad_sequences(decoder_target, maxlen=max_tar_len, padding='post') ๋ชจ๋“  ๊ฐ’์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž ๋‹จ์œ„ ๋ฒˆ์—ญ๊ธฐ๋ฏ€๋กœ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์€ ๋ณ„๋„๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š์œผ๋ฉฐ, ์˜ˆ์ธก๊ฐ’๊ณผ์˜ ์˜ค์ฐจ ์ธก์ •์— ์‚ฌ์šฉ๋˜๋Š” ์‹ค์ œ ๊ฐ’๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ž…๋ ฅ๊ฐ’๋„ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. encoder_input = to_categorical(encoder_input) decoder_input = to_categorical(decoder_input) decoder_target = to_categorical(decoder_target) ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ๊ฐ€ ๋ชจ๋‘ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ๋ณธ๊ฒฉ์ ์œผ๋กœ seq2seq ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2) ๊ต์‚ฌ ๊ฐ•์š”(Teacher forcing) ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•˜๊ธฐ ์ „์— ํ˜น์‹œ ์˜์•„ํ•œ ์ ์€ ์—†์œผ์‹ ๊ฐ€์š”? ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์€ ์˜ค์ง ์ด์ „ ๋””์ฝ”๋” ์…€์˜ ์ถœ๋ ฅ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š”๋‹ค๊ณ  ์„ค๋ช…ํ•˜์˜€๋Š”๋ฐ decoder_input์ด ์™œ ํ•„์š”ํ• ๊นŒ์š”? ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ์ด์ „ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ถœ๋ ฅ์„ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด์ฃผ์ง€ ์•Š๊ณ , ์ด์ „ ์‹œ์ ์˜ ์‹ค์ œ ๊ฐ’์„ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๊ฒ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ด์ „ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์˜ˆ์ธก์ด ํ‹€๋ ธ๋Š”๋ฐ ์ด๋ฅผ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์˜ˆ์ธก๋„ ์ž˜๋ชป๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๊ณ  ์ด๋Š” ์—ฐ์‡„ ์ž‘์šฉ์œผ๋กœ ๋””์ฝ”๋” ์ „์ฒด์˜ ์˜ˆ์ธก์„ ์–ด๋ ต๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์ด ๋ฐ˜๋ณต๋˜๋ฉด ํ›ˆ๋ จ ์‹œ๊ฐ„์ด ๋Š๋ ค์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ์ƒํ™ฉ์„ ์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ด์ „ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์˜ˆ์ธก๊ฐ’ ๋Œ€์‹  ์‹ค์ œ ๊ฐ’์„ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด RNN์˜ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ด์ „ ์‹œ์ ์˜ ์˜ˆ์ธก๊ฐ’ ๋Œ€์‹  ์‹ค์ œ ๊ฐ’์„ ์ž…๋ ฅ์œผ๋กœ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ๊ต์‚ฌ ๊ฐ•์š”๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3) seq2seq ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ seq2seq ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•˜๊ณ  ๊ต์‚ฌ ๊ฐ•์š”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. from tensorflow.keras.layers import Input, LSTM, Embedding, Dense from tensorflow.keras.models import Model import numpy as np encoder_inputs = Input(shape=(None, src_vocab_size)) encoder_lstm = LSTM(units=256, return_state=True) # encoder_outputs์€ ์—ฌ๊ธฐ์„œ๋Š” ๋ถˆํ•„์š” encoder_outputs, state_h, state_c = encoder_lstm(encoder_inputs) # LSTM์€ ๋ฐ”๋‹๋ผ RNN ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ƒํƒœ๊ฐ€ ๋‘ ๊ฐœ. ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ. encoder_states = [state_h, state_c] ์ธ์ฝ”๋”๋ฅผ ์ฃผ๋ชฉํ•ด ๋ณด๋ฉด functional API๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ ์™ธ์—๋Š” ์•ž์„œ ๋‹ค๋ฅธ ์‹ค์Šต์—์„œ ๋ณธ LSTM ์„ค๊ณ„์™€ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์šฐ์„  LSTM์˜ ์€๋‹‰ ์ƒํƒœ ํฌ๊ธฐ๋Š” 256์œผ๋กœ ์„ ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์˜ ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ๋””์ฝ”๋”๋กœ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— return_state=True๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์— ์ž…๋ ฅ์„ ๋„ฃ์œผ๋ฉด ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. LSTM์—์„œ state_h, state_c๋ฅผ ๋ฆฌํ„ด ๋ฐ›๋Š”๋ฐ, ์ด๋Š” ๊ฐ๊ฐ LSTM์„ ์„ค๋ช…ํ•  ๋•Œ ์–ธ๊ธ‰ํ•˜์˜€๋˜ ๋ฐฐ์šด ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์ด๋ก ์„ ์„ค๋ช…ํ•  ๋•Œ๋Š” ์…€ ์ƒํƒœ๋Š” ์„ค๋ช…์—์„œ ์ƒ๋žตํ•˜๊ณ  ์€๋‹‰ ์ƒํƒœ๋งŒ ์–ธ๊ธ‰ํ•˜์˜€์œผ๋‚˜ ์‚ฌ์‹ค LSTM์€ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ์ƒํƒœ๋ฅผ ๊ฐ€์ง„๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊ธฐ์–ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ‘์ž๊ธฐ ์–ด๋ ค์›Œ์ง„ ๊ฒŒ ์•„๋‹™๋‹ˆ๋‹ค. ๋‹จ์ง€ ์€๋‹‰ ์ƒํƒœ๋งŒ ์ „๋‹ฌํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ ๋‘ ๊ฐ€์ง€๋ฅผ ์ „๋‹ฌํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐ€์ง€ ์ƒํƒœ๋ฅผ encoder_states์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. encoder_states๋ฅผ ๋””์ฝ”๋”์— ์ „๋‹ฌํ•จ์œผ๋กœ์จ ์ด ๋‘ ๊ฐ€์ง€ ์ƒํƒœ ๋ชจ๋‘๋ฅผ ๋””์ฝ”๋”๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์•ž์„œ ๋ฐฐ์šด ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. decoder_inputs = Input(shape=(None, tar_vocab_size)) decoder_lstm = LSTM(units=256, return_sequences=True, return_state=True) # ๋””์ฝ”๋”์—๊ฒŒ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ, ์…€ ์ƒํƒœ๋ฅผ ์ „๋‹ฌ. decoder_outputs, _, _= decoder_lstm(decoder_inputs, initial_state=encoder_states) decoder_softmax_layer = Dense(tar_vocab_size, activation='softmax') decoder_outputs = decoder_softmax_layer(decoder_outputs) model = Model([encoder_inputs, decoder_inputs], decoder_outputs) model.compile(optimizer="rmsprop", loss="categorical_crossentropy") ๋””์ฝ”๋”๋Š” ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ initial_state์˜ ์ธ์ž ๊ฐ’์œผ๋กœ encoder_states๋ฅผ ์ฃผ๋Š” ์ฝ”๋“œ๊ฐ€ ์ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋™์ผํ•˜๊ฒŒ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ ํฌ๊ธฐ๋„ 256์œผ๋กœ ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋„ ์€๋‹‰ ์ƒํƒœ, ์…€ ์ƒํƒœ๋ฅผ ๋ฆฌํ„ดํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„ ์ถœ๋ ฅ์ธต์— ํ”„๋ž‘์Šค์–ด์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒํผ ๋‰ด๋Ÿฐ์„ ๋ฐฐ์น˜ํ•œ ํ›„ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹ค์ œ ๊ฐ’๊ณผ์˜ ์˜ค์ฐจ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. model.fit(x=[encoder_input, decoder_input], y=decoder_target, batch_size=64, epochs=40, validation_split=0.2) ์ž…๋ ฅ์œผ๋กœ๋Š” ์ธ์ฝ”๋” ์ž…๋ ฅ๊ณผ ๋””์ฝ”๋” ์ž…๋ ฅ์ด ๋“ค์–ด๊ฐ€๊ณ , ๋””์ฝ”๋”์˜ ์‹ค์ œ ๊ฐ’์ธ decoder_target๋„ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 64๋กœ ํ•˜์˜€์œผ๋ฉฐ ์ด 40 ์—ํฌํฌ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ์„ค์ •ํ•œ ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ์™€ ์—ํฌํฌ ์ˆ˜๋Š” ์‹ค์ œ๋กœ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ ์ƒํƒœ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์ค‘๊ฐ„๋ถ€ํ„ฐ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ค์ฐจ์ธ val_loss์˜ ๊ฐ’์ด ์˜ฌ๋ผ๊ฐ€๋Š”๋ฐ, ์‚ฌ์‹ค ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์˜ ์–‘๊ณผ ํƒœ์Šคํฌ์˜ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„์™€ ๊ณผ์ ํ•ฉ ๋ฐฉ์ง€๋ผ๋Š” ๋‘ ๋งˆ๋ฆฌ ํ† ๋ผ๋ฅผ ๋™์‹œ์— ์žก๊ธฐ์—๋Š” ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์šฐ์„  seq2seq์˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜๊ณผ ์งง์€ ๋ฌธ์žฅ๊ณผ ๊ธด ๋ฌธ์žฅ์— ๋Œ€ํ•œ ์„ฑ๋Šฅ ์ฐจ์ด์— ๋Œ€ํ•œ ํ™•์ธ์„ ์ค‘์ ์œผ๋กœ ๋‘๊ณ  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ ๋œ ์ƒํƒœ๋กœ ๋™์ž‘ ๋‹จ๊ณ„๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. 4) seq2seq ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ ๋™์ž‘์‹œํ‚ค๊ธฐ ์•ž์„œ seq2seq๋Š” ํ›ˆ๋ จํ•  ๋•Œ์™€ ๋™์ž‘ํ•  ๋•Œ์˜ ๋ฐฉ์‹์ด ๋‹ค๋ฅด๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ž…๋ ฅํ•œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ์„ ํ•˜๋„๋ก ๋ชจ๋ธ์„ ์กฐ์ •ํ•˜๊ณ  ๋™์ž‘์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด์ ์ธ ๋ฒˆ์—ญ ๋™์ž‘ ๋‹จ๊ณ„๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1. ๋ฒˆ์—ญํ•˜๊ณ ์ž ํ•˜๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์ด ์ธ์ฝ”๋”์— ๋“ค์–ด๊ฐ€์„œ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. 2. ์ƒํƒœ์™€ <SOS>์— ํ•ด๋‹นํ•˜๋Š” \t๋ฅผ ๋””์ฝ”๋”๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. 3. ๋””์ฝ”๋”๊ฐ€ <EOS>์— ํ•ด๋‹นํ•˜๋Š” \n์ด ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ํ–‰๋™์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. encoder_model = Model(inputs=encoder_inputs, outputs=encoder_states) ์šฐ์„  ์ธ์ฝ”๋”๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. encoder_inputs์™€ encoder_states๋Š” ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ์ด๋ฏธ ์ •์˜ํ•œ ๊ฒƒ๋“ค์„ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋ฅผ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ด์ „ ์‹œ์ ์˜ ์ƒํƒœ๋“ค์„ ์ €์žฅํ•˜๋Š” ํ…์„œ decoder_state_input_h = Input(shape=(256, )) decoder_state_input_c = Input(shape=(256, )) decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c] # ๋ฌธ์žฅ์˜ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ดˆ๊ธฐ ์ƒํƒœ(initial_state)๋ฅผ ์ด์ „ ์‹œ์ ์˜ ์ƒํƒœ๋กœ ์‚ฌ์šฉ. # ๋’ค์˜ ํ•จ์ˆ˜ decode_sequence()์— ๋™์ž‘์„ ๊ตฌํ˜„ ์˜ˆ์ • decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs) # ํ›ˆ๋ จ ๊ณผ์ •์—์„œ์™€ ๋‹ฌ๋ฆฌ LSTM์˜ ๋ฆฌํ„ดํ•˜๋Š” ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ ๋ฒ„๋ฆฌ์ง€ ์•Š์Œ. decoder_states = [state_h, state_c] decoder_outputs = decoder_softmax_layer(decoder_outputs) decoder_model = Model(inputs=[decoder_inputs] + decoder_states_inputs, outputs=[decoder_outputs] + decoder_states) index_to_src = dict((i, char) for char, i in src_to_index.items()) index_to_tar = dict((i, char) for char, i in tar_to_index.items()) ๋‹จ์–ด๋กœ๋ถ€ํ„ฐ ์ธ๋ฑ์Šค๋ฅผ ์–ป๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ธ๋ฑ์Šค๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” index_to_src์™€ index_to_tar๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. def decode_sequence(input_seq): # ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ธ์ฝ”๋”์˜ ์ƒํƒœ๋ฅผ ์–ป์Œ states_value = encoder_model.predict(input_seq) # <SOS>์— ํ•ด๋‹นํ•˜๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ ์ƒ์„ฑ target_seq = np.zeros((1, 1, tar_vocab_size)) target_seq[0, 0, tar_to_index['\t']] = 1. stop_condition = False decoded_sentence = "" # stop_condition์ด True๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฃจํ”„ ๋ฐ˜๋ณต while not stop_condition: # ์ด์  ์‹œ์ ์˜ ์ƒํƒœ states_value๋ฅผ ํ˜„์‹œ์ ์˜ ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ์‚ฌ์šฉ output_tokens, h, c = decoder_model.predict([target_seq] + states_value) # ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ sampled_token_index = np.argmax(output_tokens[0, -1, :]) sampled_char = index_to_tar[sampled_token_index] # ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก ๋ฌธ์ž๋ฅผ ์˜ˆ์ธก ๋ฌธ์žฅ์— ์ถ”๊ฐ€ decoded_sentence += sampled_char # <eos>์— ๋„๋‹ฌํ•˜๊ฑฐ๋‚˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ๋„˜์œผ๋ฉด ์ค‘๋‹จ. if (sampled_char == '\n' or len(decoded_sentence) > max_tar_len): stop_condition = True # ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ €์žฅ target_seq = np.zeros((1, 1, tar_vocab_size)) target_seq[0, 0, sampled_token_index] = 1. # ํ˜„์žฌ ์‹œ์ ์˜ ์ƒํƒœ๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ ์ƒํƒœ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ €์žฅ states_value = [h, c] return decoded_sentence for seq_index in [3,50,100,300,1001]: # ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ์ธ๋ฑ์Šค input_seq = encoder_input[seq_index:seq_index+1] decoded_sentence = decode_sequence(input_seq) print(35 * "-") print('์ž…๋ ฅ ๋ฌธ์žฅ:', lines.src[seq_index]) print('์ •๋‹ต ๋ฌธ์žฅ:', lines.tar[seq_index][2:len(lines.tar[seq_index])-1]) # '\t'์™€ '\n'์„ ๋นผ๊ณ  ์ถœ๋ ฅ print('๋ฒˆ์—ญ ๋ฌธ์žฅ:', decoded_sentence[1:len(decoded_sentence)-1]) # '\n'์„ ๋นผ๊ณ  ์ถœ๋ ฅ ----------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ: Hi. ์ •๋‹ต ๋ฌธ์žฅ: Salut ! ๋ฒˆ์—ญ ๋ฌธ์žฅ: Salut. ----------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ: I see. ์ •๋‹ต ๋ฌธ์žฅ: Aha. ๋ฒˆ์—ญ ๋ฌธ์žฅ: Je change. ----------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ: Hug me. ์ •๋‹ต ๋ฌธ์žฅ: Serrez-moi dans vos bras ! ๋ฒˆ์—ญ ๋ฌธ์žฅ: Serre-moi dans vos patents ! ----------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ: Help me. ์ •๋‹ต ๋ฌธ์žฅ: Aidez-moi. ๋ฒˆ์—ญ ๋ฌธ์žฅ: Aidez-moi. ----------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ: I beg you. ์ •๋‹ต ๋ฌธ์žฅ: Je vous en prie. ๋ฒˆ์—ญ ๋ฌธ์žฅ: Je vous en prie. ์ง€๊ธˆ๊นŒ์ง€ ๋ฌธ์ž ๋‹จ์œ„์˜ seq2seq๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์‹ค์Šต์—์„œ๋Š” ์ด๋ฒˆ ์‹ค์Šต์—์„œ ๋ฐฐ์šด ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ๋ฌธ์ž ๋‹จ์œ„์—์„œ ๋‹จ์–ด ๋‹จ์œ„๋กœ ํ™•์žฅํ•ด์„œ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 14-02 Word-Level ๋ฒˆ์—ญ๊ธฐ ๋งŒ๋“ค๊ธฐ(Neural Machine Translation (seq2seq) Tutorial) seq2seq๋ฅผ ์ด์šฉํ•ด์„œ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์€ ๊ฒŒ์‹œ๋ฌผ์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ธํ„ฐ๋„ท์— ์ผ€๋ผ์Šค๋กœ seq2seq๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋งŽ์€ ์œ ์‚ฌ ์˜ˆ์ œ๋“ค์ด ๋‚˜์™€์žˆ์ง€๋งŒ ๋Œ€๋ถ€๋ถ„์€ ์ผ€๋ผ์Šค ๊ฐœ๋ฐœ์ž ํ”„๋ž‘์ˆ˜์•„ ์ˆ„๋ ˆ์˜ ๋ธ”๋กœ๊ทธ์˜ ์œ ๋ช… ๊ฒŒ์‹œ๋ฌผ์ธ 'sequence-to-sequence 10๋ถ„ ๋งŒ์— ์ดํ•ดํ•˜๊ธฐ'๊ฐ€ ์›๋ณธ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต ๋˜ํ•œ ํ•ด๋‹น ๊ฒŒ์‹œ๋ฌผ์˜ ์˜ˆ์ œ์— ๋งŽ์ด ์˜ํ–ฅ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ์ „์ฒ˜๋ฆฌ ์‹ค์ œ ์„ฑ๋Šฅ์ด ์ข‹์€ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋Š” seq2seq๋ฅผ ๊ฐ„๋‹จํžˆ ์‹ค์Šตํ•ด ๋ณด๋Š” ์ˆ˜์ค€์˜ ๊ฐ„๋‹จํ•œ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค(parallel corpus)๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค๋ž€, ๋‘ ๊ฐœ ์ด์ƒ์˜ ์–ธ์–ด๊ฐ€ ๋ณ‘๋ ฌ์ ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ฝ”ํผ์Šค๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : http://www.manythings.org/anki ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ”„๋ž‘์Šค์–ด-์˜์–ด ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค์ธ fra-eng.zip ํŒŒ์ผ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๋งํฌ์—์„œ ํ•ด๋‹น ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•œ ํ›„ ์••์ถ•์„ ํ’€๋ฉด fra.txt๋ผ๋Š” ํŒŒ์ผ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ํ•ด๋‹น ํŒŒ์ผ์„ ์ด ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ๋ผ๊ณ  ํ•˜๋ฉด ์•ž์„œ ์ˆ˜ํ–‰ํ•œ ํƒœ๊น… ์ž‘์—… ์ฑ•ํ„ฐ์˜ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์•ž์„œ ์ˆ˜ํ–‰ํ•œ ํƒœ๊น… ์ž‘์—…์˜ ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ์™€ seq2seq๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ๋Š” ์„ฑ๊ฒฉ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํƒœ๊น… ์ž‘์—…์˜ ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ๋Š” ์Œ์ด ๋˜๋Š” ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ”์ด ๊ธธ์ด๊ฐ€ ๋™์ผํ•˜์˜€์œผ๋‚˜ ์—ฌ๊ธฐ์„œ๋Š” ์Œ์ด ๋œ๋‹ค๊ณ  ํ•ด์„œ ๋ฐ˜๋“œ์‹œ ๊ธธ์ด๊ฐ€ ๊ฐ™์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๊ตฌ๊ธ€ ๋ฒˆ์—ญ๊ธฐ์— '๋‚˜๋Š” ํ•™์ƒ์ด๋‹ค.'๋ผ๋Š” ํ† ํฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 2์ธ ๋ฌธ์žฅ์„ ๋„ฃ์—ˆ์„ ๋•Œ 'I am a student.'๋ผ๋Š” ํ† ํฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 4์ธ ๋ฌธ์žฅ์ด ๋‚˜์˜ค๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์ด์น˜์ž…๋‹ˆ๋‹ค. seq2seq๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์ถœ๋ ฅ ์‹œํ€€์Šค์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ ๊ตฌํ˜„ ์˜ˆ์ œ๋Š” ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ์ด์ง€๋งŒ seq2seq๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ์˜ˆ์ œ์ธ ์ฑ—๋ด‡์„ ๋งŒ๋“ ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๋ฉด, ๋Œ€๋‹ต์˜ ๊ธธ์ด๊ฐ€ ์งˆ๋ฌธ์˜ ๊ธธ์ด์™€ ํ•ญ์ƒ ๋˜‘๊ฐ™์•„์•ผ ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๊ทธ ๋˜ํ•œ ์ด์ƒํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  fra.txt ๋ฐ์ดํ„ฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์™ผ์ชฝ์˜ ์˜์–ด ๋ฌธ์žฅ๊ณผ ์˜ค๋ฅธ์ชฝ์˜ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ ์‚ฌ์ด์— ํƒญ์œผ๋กœ ๊ตฌ๋ถ„๋˜๋Š”<NAME>์ด ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ์ž…๋‹ˆ๋‹ค. Watch me. Regardez-moi ! ๋ฐ์ดํ„ฐ๋Š” ์œ„์™€ ๋™์ผํ•œ<NAME>์˜ ์•ฝ 19๋งŒ ๊ฐœ์˜ ๋ณ‘๋ ฌ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ์ฝ”๋“œ์—์„œ src๋Š” source์˜ ์ค„์ž„๋ง๋กœ ์ž…๋ ฅ ๋ฌธ์žฅ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, tar๋Š” target์˜ ์ค„์ž„๋ง๋กœ ๋ฒˆ์—ญํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์žฅ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. import os import re import shutil import zipfile import numpy as np import pandas as pd import tensorflow as tf import unicodedata import urllib3 from tensorflow.keras.layers import Embedding, GRU, Dense from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.preprocessing.text import Tokenizer fra-eng.zip ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์••์ถ•์„ ํ’€๊ฒ ์Šต๋‹ˆ๋‹ค. import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' } def download_zip(url, output_path): response = requests.get(url, headers=headers, stream=True) if response.status_code == 200: with open(output_path, 'wb') as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk) print(f"ZIP file downloaded to {output_path}") else: print(f"Failed to download. HTTP Response Code: {response.status_code}") url = "http://www.manythings.org/anki/fra-eng.zip" output_path = "fra-eng.zip" download_zip(url, output_path) path = os.getcwd() zipfilename = os.path.join(path, output_path) with zipfile.ZipFile(zipfilename, 'r') as zip_ref: zip_ref.extractall(path) ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์•ฝ 19๋งŒ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์ค‘ 33,000๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ์„ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. num_samples = 33000 ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋“ค์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋‘์  ๋“ฑ์„ ์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜ ๋‹จ์–ด์™€ ๊ตฌ๋ถ„ํ•ด ์ฃผ๊ธฐ ์œ„ํ•œ ์ „์ฒ˜๋ฆฌ์ž…๋‹ˆ๋‹ค. def to_ascii(s): # ํ”„๋ž‘์Šค์–ด ์•…์„ผํŠธ(accent) ์‚ญ์ œ # ์˜ˆ์‹œ : 'dรฉjร  dinรฉ' -> deja dine return ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn') def preprocess_sentence(sent): # ์•…์„ผํŠธ ์ œ๊ฑฐ ํ•จ์ˆ˜ ํ˜ธ์ถœ sent = to_ascii(sent.lower()) # ๋‹จ์–ด์™€ ๊ตฌ๋‘์  ์‚ฌ์ด์— ๊ณต๋ฐฑ ์ถ”๊ฐ€. # ex) "I am a student." => "I am a student ." sent = re.sub(r"([?.!,ยฟ])", r" \1", sent) # (a-z, A-Z, ".", "?", "!", ",") ์ด๋“ค์„ ์ œ์™ธํ•˜๊ณ ๋Š” ์ „๋ถ€ ๊ณต๋ฐฑ์œผ๋กœ ๋ณ€ํ™˜. sent = re.sub(r"[^a-zA-Z!.?]+", r" ", sent) # ๋‹ค์ˆ˜ ๊ฐœ์˜ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ์น˜ํ™˜ sent = re.sub(r"\s+", " ", sent) return sent ๊ตฌํ˜„ํ•œ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋“ค์„ ์ž„์˜์˜ ๋ฌธ์žฅ์„ ์ž…๋ ฅ์œผ๋กœ ํ…Œ์ŠคํŠธํ•ด ๋ด…์‹œ๋‹ค. # ์ „์ฒ˜๋ฆฌ ํ…Œ์ŠคํŠธ en_sent = u"Have you had dinner?" fr_sent = u"Avez-vous dรฉjร  dinรฉ?" print('์ „์ฒ˜๋ฆฌ ์ „ ์˜์–ด ๋ฌธ์žฅ :', en_sent) print('์ „์ฒ˜๋ฆฌ ํ›„ ์˜์–ด ๋ฌธ์žฅ :',preprocess_sentence(en_sent)) print('์ „์ฒ˜๋ฆฌ ์ „ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ :', fr_sent) print('์ „์ฒ˜๋ฆฌ ํ›„ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ :', preprocess_sentence(fr_sent)) ์ „์ฒ˜๋ฆฌ ์ „ ์˜์–ด ๋ฌธ์žฅ : Have you had dinner? ์ „์ฒ˜๋ฆฌ ํ›„ ์˜์–ด ๋ฌธ์žฅ : have you had dinner ? ์ „์ฒ˜๋ฆฌ ์ „ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ : Avez-vous dรฉjร  dinรฉ? ์ „์ฒ˜๋ฆฌ ํ›„ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ : avez vous deja dine ? ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ 33,000๊ฐœ์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ๊ต์‚ฌ ๊ฐ•์š”(Teacher Forcing)์„ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋ฏ€๋กœ, ํ›ˆ๋ จ ์‹œ ์‚ฌ์šฉํ•  ๋””์ฝ”๋”์˜ ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์‹ค์ œ ๊ฐ’. ์ฆ‰, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ์‹œํ€€์Šค์—๋Š” ์‹œ์ž‘์„ ์˜๋ฏธํ•˜๋Š” ํ† ํฐ์ธ <sos>๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์ถœ๋ ฅ ์‹œํ€€์Šค์—๋Š” ์ข…๋ฃŒ๋ฅผ ์˜๋ฏธํ•˜๋Š” ํ† ํฐ์ธ <eos>๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. def load_preprocessed_data(): encoder_input, decoder_input, decoder_target = [], [], [] with open("fra.txt", "r") as lines: for i, line in enumerate(lines): # source ๋ฐ์ดํ„ฐ์™€ target ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌ src_line, tar_line, _ = line.strip().split('\t') # source ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ src_line = [w for w in preprocess_sentence(src_line).split()] # target ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ tar_line = preprocess_sentence(tar_line) tar_line_in = [w for w in ("<sos> " + tar_line).split()] tar_line_out = [w for w in (tar_line + " <eos>").split()] encoder_input.append(src_line) decoder_input.append(tar_line_in) decoder_target.append(tar_line_out) if i == num_samples - 1: break return encoder_input, decoder_input, decoder_target ์ด๋ ‡๊ฒŒ ์–ป์€ 3๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์…‹ ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ, ๋””์ฝ”๋”์˜ ์ž…๋ ฅ, ๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ”์„ ์ƒ์œ„ 5๊ฐœ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. sents_en_in, sents_fra_in, sents_fra_out = load_preprocessed_data() print('์ธ์ฝ”๋”์˜ ์ž…๋ ฅ :',sents_en_in[:5]) print('๋””์ฝ”๋”์˜ ์ž…๋ ฅ :',sents_fra_in[:5]) print('๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ” :',sents_fra_out[:5]) ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ : [['go', '.'], ['go', '.'], ['go', '.'], ['hi', '.'], ['hi', '.']] ๋””์ฝ”๋”์˜ ์ž…๋ ฅ : [['<sos>', 'va', '!'], ['<sos>', 'marche', '.'], ['<sos>', 'bouge', '!'], ['<sos>', 'salut', '!'], ['<sos>', 'salut', '.']] ๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ” : [['va', '!', '<eos>'], ['marche', '.', '<eos>'], ['bouge', '!', '<eos>'], ['salut', '!', '<eos>'], ['salut', '.', '<eos>']] ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•˜๊ธฐ ์ „ ์˜์•„ํ•œ ์ ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์€ ์˜ค์ง ์ด์ „ ๋””์ฝ”๋” ์…€์˜ ์ถœ๋ ฅ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š”๋‹ค๊ณ  ์„ค๋ช…ํ•˜์˜€๋Š”๋ฐ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ์ธ sents_fra_in์ด ์™œ ํ•„์š”ํ• ๊นŒ์š”? ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ์ด์ „ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ถœ๋ ฅ์„ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด์ฃผ์ง€ ์•Š๊ณ , ์ด์ „ ์‹œ์ ์˜ ์‹ค์ œ ๊ฐ’์„ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๊ฒ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ด์ „ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์˜ˆ์ธก์ด ํ‹€๋ ธ๋Š”๋ฐ ์ด๋ฅผ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์˜ˆ์ธก๋„ ์ž˜๋ชป๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๊ณ  ์ด๋Š” ์—ฐ์‡„ ์ž‘์šฉ์œผ๋กœ ๋””์ฝ”๋” ์ „์ฒด์˜ ์˜ˆ์ธก์„ ์–ด๋ ต๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์ด ๋ฐ˜๋ณต๋˜๋ฉด ํ›ˆ๋ จ ์‹œ๊ฐ„์ด ๋Š๋ ค์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ์ƒํ™ฉ์„ ์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ด์ „ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์˜ˆ์ธก๊ฐ’ ๋Œ€์‹  ์‹ค์ œ ๊ฐ’์„ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด RNN์˜ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ด์ „ ์‹œ์ ์˜ ์˜ˆ์ธก๊ฐ’ ๋Œ€์‹  ์‹ค์ œ ๊ฐ’์„ ์ž…๋ ฅ์œผ๋กœ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ๊ต์‚ฌ ๊ฐ•์š”๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ํ†ตํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ƒ์„ฑ, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ ํ›„ ์ด์–ด์„œ ํŒจ๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. tokenizer_en = Tokenizer(filters="", lower=False) tokenizer_en.fit_on_texts(sents_en_in) encoder_input = tokenizer_en.texts_to_sequences(sents_en_in) encoder_input = pad_sequences(encoder_input, padding="post") tokenizer_fra = Tokenizer(filters="", lower=False) tokenizer_fra.fit_on_texts(sents_fra_in) tokenizer_fra.fit_on_texts(sents_fra_out) decoder_input = tokenizer_fra.texts_to_sequences(sents_fra_in) decoder_input = pad_sequences(decoder_input, padding="post") decoder_target = tokenizer_fra.texts_to_sequences(sents_fra_out) decoder_target = pad_sequences(decoder_target, padding="post") ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape)๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape) :',encoder_input.shape) print('๋””์ฝ”๋”์˜ ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape) :',decoder_input.shape) print('๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ(shape) :',decoder_target.shape) ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape) : (33000, 8) ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape) : (33000, 16) ๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ(shape) : (33000, 16) ์ƒ˜ํ”Œ์€ ์ด 33,000๊ฐœ ์กด์žฌํ•˜๋ฉฐ ์˜์–ด ๋ฌธ์žฅ์˜ ๊ธธ์ด๋Š” 8, ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ์˜ ๊ธธ์ด๋Š” 16์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. src_vocab_size = len(tokenizer_en.word_index) + 1 tar_vocab_size = len(tokenizer_fra.word_index) + 1 print("์˜์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {:d}, ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {:d}".format(src_vocab_size, tar_vocab_size)) ์˜์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 4647, ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 8022 ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” ๊ฐ๊ฐ 4,647๊ฐœ์™€ 8,022๊ฐœ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜๋ฅผ ์–ป๋Š” ๋”•์…”๋„ˆ๋ฆฌ์™€ ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ ์–ป๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ฐ๊ฐ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ด๋“ค์€ ํ›ˆ๋ จ์„ ๋งˆ์น˜๊ณ  ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๋‹จ๊ณ„์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. src_to_index = tokenizer_en.word_index index_to_src = tokenizer_en.index_word tar_to_index = tokenizer_fra.word_index index_to_tar = tokenizer_fra.index_word ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ „ ๋ฐ์ดํ„ฐ๋ฅผ ์„ž์–ด์ค๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ์ˆœ์„œ๊ฐ€ ์„ž์ธ ์ •์ˆ˜ ์‹œํ€€์Šค ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. indices = np.arange(encoder_input.shape[0]) np.random.shuffle(indices) print('๋žœ๋ค ์‹œํ€€์Šค :',indices) ๋žœ๋ค ์‹œํ€€์Šค : [16412 5374 8832 ... 5652 24040 10002] ์ด๋ฅผ ๋ฐ์ดํ„ฐ ์…‹์˜ ์ˆœ์„œ๋กœ ์ง€์ •ํ•ด ์ฃผ๋ฉด ์ƒ˜ํ”Œ๋“ค์ด ๊ธฐ์กด ์ˆœ์„œ์™€ ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ์„ž์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. encoder_input = encoder_input[indices] decoder_input = decoder_input[indices] decoder_target = decoder_target[indices] ์ž„์˜๋กœ 30,997๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. ์ด๋•Œ decoder_input๊ณผ decoder_target์€ ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ์ƒ์œผ๋กœ ์•ž์— ๋ถ™์€ <sos> ํ† ํฐ๊ณผ ๋’ค์— ๋ถ™์€ <eos>์„ ์ œ์™ธํ•˜๋ฉด ๋™์ผํ•œ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. encoder_input[30997] array([ 5, 7, 638, 1, 0, 0, 0, 0], dtype=int32) decoder_input[30997] array([ 2, 18, 5, 16, 173, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int32) decoder_target[30997] array([ 18, 5, 16, 173, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int32) ์ €์ž์˜ ๊ฒฝ์šฐ 18, 5, 16, 173, 1์ด๋ผ๋Š” ๋™์ผ ์‹œํ€€์Šค๋ฅผ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ 10%๋ฅผ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. n_of_val = int(33000*0.1) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :',n_of_val) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 3300 33,000๊ฐœ์˜ 10%์— ํ•ด๋‹น๋˜๋Š” 3,300๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. encoder_input_train = encoder_input[:-n_of_val] decoder_input_train = decoder_input[:-n_of_val] decoder_target_train = decoder_target[:-n_of_val] encoder_input_test = encoder_input[-n_of_val:] decoder_input_test = decoder_input[-n_of_val:] decoder_target_test = decoder_target[-n_of_val:] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape)๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ source ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',encoder_input_train.shape) print('ํ›ˆ๋ จ target ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',decoder_input_train.shape) print('ํ›ˆ๋ จ target ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ :',decoder_target_train.shape) print('ํ…Œ์ŠคํŠธ source ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',encoder_input_test.shape) print('ํ…Œ์ŠคํŠธ target ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',decoder_input_test.shape) print('ํ…Œ์ŠคํŠธ target ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ :',decoder_target_test.shape) ํ›ˆ๋ จ source ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (29700, 8) ํ›ˆ๋ จ target ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (29700, 16) ํ›ˆ๋ จ target ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (29700, 16) ํ…Œ์ŠคํŠธ source ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (3300, 8) ํ…Œ์ŠคํŠธ target ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (3300, 16) ํ…Œ์ŠคํŠธ target ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (3300, 16) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ์€ 29,700๊ฐœ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ์€ 3,300๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. 2. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ ๋งŒ๋“ค๊ธฐ from tensorflow.keras.layers import Input, LSTM, Embedding, Dense, Masking from tensorflow.keras.models import Model ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ LSTM์˜ ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ 64๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. embedding_dim = 64 hidden_units = 64 ์ธ์ฝ”๋”๋ฅผ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”๋ฅผ ์ฃผ๋ชฉํ•ด ๋ณด๋ฉด ํ•จ์ˆ˜ํ˜• API(functional API)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ ์™ธ์—๋Š” ์•ž์„œ ๋‹ค๋ฅธ ์‹ค์Šต์—์„œ ๋ณธ LSTM ์„ค๊ณ„์™€ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. Masking์€ ํŒจ๋”ฉ ํ† ํฐ์ธ ์ˆซ์ž 0์˜ ๊ฒฝ์šฐ์—๋Š” ์—ฐ์‚ฐ์„ ์ œ์™ธํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์˜ ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ๋””์ฝ”๋”๋กœ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— return_state=True๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์— ์ž…๋ ฅ์„ ๋„ฃ์œผ๋ฉด ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. LSTM์—์„œ state_h, state_c๋ฅผ ๋ฆฌํ„ด ๋ฐ›๋Š”๋ฐ, ์ด๋Š” ๊ฐ๊ฐ RNN ์ฑ•ํ„ฐ์—์„œ LSTM์„ ์ฒ˜์Œ ์„ค๋ช…ํ•  ๋•Œ ์–ธ๊ธ‰ํ•˜์˜€๋˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐ€์ง€ ์ƒํƒœ๋ฅผ encoder_states์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. encoder_states๋ฅผ ๋””์ฝ”๋”์— ์ „๋‹ฌํ•จ์œผ๋กœ์จ ์ด ๋‘ ๊ฐ€์ง€ ์ƒํƒœ ๋ชจ๋‘๋ฅผ ๋””์ฝ”๋”๋กœ ์ „๋‹ฌํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์•ž์„œ ๋ฐฐ์šด ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. # ์ธ์ฝ”๋” encoder_inputs = Input(shape=(None,)) enc_emb = Embedding(src_vocab_size, embedding_dim)(encoder_inputs) # ์ž„๋ฒ ๋”ฉ ์ธต enc_masking = Masking(mask_value=0.0)(enc_emb) # ํŒจ๋”ฉ 0์€ ์—ฐ์‚ฐ์—์„œ ์ œ์™ธ encoder_lstm = LSTM(hidden_units, return_state=True) # ์ƒํƒœ ๊ฐ’ ๋ฆฌํ„ด์„ ์œ„ํ•ด return_state๋Š” True encoder_outputs, state_h, state_c = encoder_lstm(enc_masking) # ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ ๋ฆฌํ„ด encoder_states = [state_h, state_c] # ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ ์ €์žฅ ๋””์ฝ”๋”๋Š” ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋กœ๋ถ€ํ„ฐ ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. initial_state์˜ ์ธ์ž ๊ฐ’์œผ๋กœ encoder_states๋ฅผ ์ฃผ๋Š” ์ฝ”๋“œ๊ฐ€ ์ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋„ ์€๋‹‰ ์ƒํƒœ, ์…€ ์ƒํƒœ๋ฅผ ๋ฆฌํ„ดํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. seq2seq์˜ ๋””์ฝ”๋”๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ ์‹œ์ ๋งˆ๋‹ค ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งค ์‹œ์ ๋งˆ๋‹ค ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ(tar_vocab_size)์˜ ์„ ํƒ์ง€์—์„œ ๋‹จ์–ด๋ฅผ 1๊ฐœ ์„ ํƒํ•˜์—ฌ ์ด๋ฅผ ์ด๋ฒˆ ์‹œ์ ์—์„œ ์˜ˆ์ธกํ•œ ๋‹จ์–ด๋กœ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ด๋ฏ€๋กœ ์ถœ๋ ฅ์ธต์œผ๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์™€ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. categorical_crossentropy๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ ˆ์ด๋ธ”์€ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ๋œ ์ƒํƒœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ˜„์žฌ decoder_outputs์˜ ๊ฒฝ์šฐ์—๋Š” ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•˜์ง€ ์•Š์€ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ์ •์ˆ˜ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” categorical_crossentropy๊ฐ€ ์•„๋‹ˆ๋ผ sparse_categorical_crossentropy๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. # ๋””์ฝ”๋” decoder_inputs = Input(shape=(None,)) dec_emb_layer = Embedding(tar_vocab_size, hidden_units) # ์ž„๋ฒ ๋”ฉ ์ธต dec_emb = dec_emb_layer(decoder_inputs) # ํŒจ๋”ฉ 0์€ ์—ฐ์‚ฐ์—์„œ ์ œ์™ธ dec_masking = Masking(mask_value=0.0)(dec_emb) # ์ƒํƒœ ๊ฐ’ ๋ฆฌํ„ด์„ ์œ„ํ•ด return_state๋Š” True, ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด return_sequences๋Š” True decoder_lstm = LSTM(hidden_units, return_sequences=True, return_state=True) # ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ(initial_state)๋กœ ์‚ฌ์šฉ decoder_outputs, _, _ = decoder_lstm(dec_masking, initial_state=encoder_states) # ๋ชจ๋“  ์‹œ์ ์˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์ถœ๋ ฅ์ธต์„ ํ†ตํ•ด ๋‹จ์–ด ์˜ˆ์ธก decoder_dense = Dense(tar_vocab_size, activation='softmax') decoder_outputs = decoder_dense(decoder_outputs) # ๋ชจ๋ธ์˜ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์ •์˜. model = Model([encoder_inputs, decoder_inputs], decoder_outputs) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc']) ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•ฉ๋‹ˆ๋‹ค. 128๊ฐœ์˜ ๋ฐฐ์น˜ ํฌ๊ธฐ๋กœ ์ด 50 ์—ํฌํฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ์ด ์ œ๋Œ€๋กœ ๋˜๊ณ  ์žˆ๋Š”์ง€ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. model.fit(x=[encoder_input_train, decoder_input_train], y=decoder_target_train, \ validation_data=([encoder_input_test, decoder_input_test], decoder_target_test), batch_size=128, epochs=50) ์ €์ž์˜ ๊ฒฝ์šฐ ์ตœ์ข… ์—ํฌํฌ์—์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” 92%์˜ ์ •ํ™•๋„๋ฅผ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ๋Š” 86%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. 3. seq2seq ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ ๋™์ž‘์‹œํ‚ค๊ธฐ seq2seq๋Š” ํ›ˆ๋ จ ๊ณผ์ •(๊ต์‚ฌ ๊ฐ•์š”)๊ณผ ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ์˜ ๋™์ž‘ ๋ฐฉ์‹์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ…Œ์ŠคํŠธ ๊ณผ์ •์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋‹ค์‹œ ์„ค๊ณ„ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ๋””์ฝ”๋”๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฒˆ์—ญ ๋‹จ๊ณ„๋ฅผ ์œ„ํ•ด ๋ชจ๋ธ์„ ์ˆ˜์ •ํ•˜๊ณ  ๋™์ž‘์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด์ ์ธ ๋ฒˆ์—ญ ๋‹จ๊ณ„๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฒˆ์—ญํ•˜๊ณ ์ž ํ•˜๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์ด ์ธ์ฝ”๋”๋กœ ์ž…๋ ฅ๋˜์–ด ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ, ๊ทธ๋ฆฌ๊ณ  ํ† ํฐ <sos>๋ฅผ ๋””์ฝ”๋”๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. ๋””์ฝ”๋”๊ฐ€ ํ† ํฐ <eos>๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ํ–‰๋™์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์˜ ์ž…, ์ถœ๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” encoder_inputs์™€ encoder_states๋Š” ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ์ด๋ฏธ ์ •์˜ํ•œ ๊ฒƒ๋“ค์„ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํ›ˆ๋ จ ๋‹จ๊ณ„์— encoder_inputs์™€ encoder_states ์‚ฌ์ด์— ์žˆ๋Š” ๋ชจ๋“  ์ธต๊นŒ์ง€ ์ „๋ถ€ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ๋˜๋ฏ€๋กœ ๊ฒฐ๊ณผ์ ์œผ๋กœ ํ›ˆ๋ จ ๋‹จ๊ณ„์—์„œ ์‚ฌ์šฉํ•œ ์ธ์ฝ”๋”๋ฅผ ๊ทธ๋Œ€๋กœ ์žฌ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์–ด์„œ ๋””์ฝ”๋”๋ฅผ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„์—์„œ๋Š” ๋””์ฝ”๋”๋ฅผ ๋งค ์‹œ์  ๋ณ„๋กœ ์ปจํŠธ๋กคํ•  ์˜ˆ์ •์œผ๋กœ, ์ด๋ฅผ ์œ„ํ•ด์„œ ์ด์ „ ์‹œ์ ์˜ ์ƒํƒœ๋ฅผ ์ €์žฅํ•  ํ…์„œ์ธ decoder_state_input_h, decoder_state_input_c๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๋งค ์‹œ์  ๋ณ„๋กœ ๋””์ฝ”๋”๋ฅผ ์ปจํŠธ๋กคํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋’ค์—์„œ ์ •์˜ํ•  decode_sequence()๋กœ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # ์ธ์ฝ”๋” encoder_model = Model(encoder_inputs, encoder_states) # ๋””์ฝ”๋” ์„ค๊ณ„ ์‹œ์ž‘ # ์ด์ „ ์‹œ์ ์˜ ์ƒํƒœ๋ฅผ ๋ณด๊ด€ํ•  ํ…์„œ decoder_state_input_h = Input(shape=(hidden_units,)) decoder_state_input_c = Input(shape=(hidden_units,)) decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c] # ํ›ˆ๋ จ ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ ์ž„๋ฒ ๋”ฉ ์ธต์„ ์žฌ์‚ฌ์šฉ dec_emb2 = dec_emb_layer(decoder_inputs) # ๋‹ค์Œ ๋‹จ์–ด ์˜ˆ์ธก์„ ์œ„ํ•ด ์ด์ „ ์‹œ์ ์˜ ์ƒํƒœ๋ฅผ ํ˜„์‹œ์ ์˜ ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ์‚ฌ์šฉ decoder_outputs2, state_h2, state_c2 = decoder_lstm(dec_emb2, initial_state=decoder_states_inputs) decoder_states2 = [state_h2, state_c2] # ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ์˜ˆ์ธก decoder_outputs2 = decoder_dense(decoder_outputs2) # ์ˆ˜์ •๋œ ๋””์ฝ”๋” decoder_model = Model( [decoder_inputs] + decoder_states_inputs, [decoder_outputs2] + decoder_states2) ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„์—์„œ์˜ ๋™์ž‘์„ ์œ„ํ•œ decode_sequence ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฌธ์žฅ์ด ๋“ค์–ด์˜ค๋ฉด ์ธ์ฝ”๋”๋Š” ๋งˆ์ง€๋ง‰ ์‹œ์ ๊นŒ์ง€ ์ „๊ฐœํ•˜์—ฌ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐœ์˜ ๊ฐ’์„ states_value์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋””์ฝ”๋”์˜ ์ดˆ๊ธฐ ์ž…๋ ฅ์œผ๋กœ <SOS>๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ target_seq์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐ€์ง€ ์ž…๋ ฅ์„ ๊ฐ€์ง€๊ณ  while ๋ฌธ์•ˆ์œผ๋กœ ์ง„์ž…ํ•˜์—ฌ ์ด ๋‘ ๊ฐ€์ง€๋ฅผ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋””์ฝ”๋”๋Š” ํ˜„์žฌ ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก์„ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก ๋ฒกํ„ฐ๊ฐ€ output_tokens, ํ˜„์žฌ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ h, ํ˜„์žฌ ์‹œ์ ์˜ ์…€ ์ƒํƒœ๊ฐ€ c์ž…๋‹ˆ๋‹ค. ์˜ˆ์ธก ๋ฒกํ„ฐ๋กœ๋ถ€ํ„ฐ ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก ๋‹จ์–ด์ธ target_seq๋ฅผ ์–ป๊ณ , h์™€ c ์ด ๋‘ ๊ฐœ์˜ ๊ฐ’์€ states_value์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  while ๋ฌธ์˜ ๋‹ค์Œ ๋ฃจํ”„. ์ฆ‰, ๋‘ ๋ฒˆ์งธ ์‹œ์ ์˜ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์œผ๋กœ ๋‹ค์‹œ target_seq์™€ states_value๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก ๋‹จ์–ด๋กœ <eos>๋ฅผ ์˜ˆ์ธกํ•˜๊ฑฐ๋‚˜ ๋ฒˆ์—ญ ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ 50์ด ๋„˜๋Š” ์ˆœ๊ฐ„๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์‹œ์ ๋งˆ๋‹ค ๋ฒˆ์—ญ๋œ ๋‹ค์–ด๋Š” decoded_sentence์— ๋ˆ„์ ํ•˜์—ฌ ์ €์žฅํ•˜์˜€๋‹ค๊ฐ€ ์ตœ์ข… ๋ฒˆ์—ญ ์‹œํ€€์Šค๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. def decode_sequence(input_seq): # ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์ƒํƒœ(์€๋‹‰ ์ƒํƒœ, ์…€ ์ƒํƒœ)๋ฅผ ์–ป์Œ states_value = encoder_model.predict(input_seq) # <SOS>์— ํ•ด๋‹นํ•˜๋Š” ์ •์ˆ˜ ์ƒ์„ฑ target_seq = np.zeros((1,1)) target_seq[0, 0] = tar_to_index['<sos>'] stop_condition = False decoded_sentence = '' # stop_condition์ด True๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฃจํ”„ ๋ฐ˜๋ณต # ๊ตฌํ˜„์˜ ๊ฐ„์†Œํ™”๋ฅผ ์œ„ํ•ด์„œ ์ด ํ•จ์ˆ˜๋Š” ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 1๋กœ ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. while not stop_condition: # ์ด์  ์‹œ์ ์˜ ์ƒํƒœ states_value๋ฅผ ํ˜„์‹œ์ ์˜ ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ์‚ฌ์šฉ output_tokens, h, c = decoder_model.predict([target_seq] + states_value) # ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋‹จ์–ด๋กœ ๋ณ€ํ™˜ sampled_token_index = np.argmax(output_tokens[0, -1, :]) sampled_char = index_to_tar[sampled_token_index] # ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก ๋‹จ์–ด๋ฅผ ์˜ˆ์ธก ๋ฌธ์žฅ์— ์ถ”๊ฐ€ decoded_sentence += ' '+sampled_char # <eos>์— ๋„๋‹ฌํ•˜๊ฑฐ๋‚˜ ์ •ํ•ด์ง„ ๊ธธ์ด๋ฅผ ๋„˜์œผ๋ฉด ์ค‘๋‹จ. if (sampled_char == '<eos>' or len(decoded_sentence) > 50): stop_condition = True # ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ €์žฅ target_seq = np.zeros((1,1)) target_seq[0, 0] = sampled_token_index # ํ˜„์žฌ ์‹œ์ ์˜ ์ƒํƒœ๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ ์ƒํƒœ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ €์žฅ states_value = [h, c] return decoded_sentence ๊ฒฐ๊ณผ ํ™•์ธ์„ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. seq_to_src ํ•จ์ˆ˜๋Š” ์˜์–ด ๋ฌธ์žฅ์— ํ•ด๋‹นํ•˜๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ์˜์–ด ๋‹จ์–ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_src๋ฅผ ํ†ตํ•ด ์˜์–ด ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. seq_to_tar์€ ํ”„๋ž‘์Šค์–ด์— ํ•ด๋‹นํ•˜๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_tar์„ ํ†ตํ•ด ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. # ์›๋ฌธ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ def seq_to_src(input_seq): sentence = '' for encoded_word in input_seq: if(encoded_word != 0): sentence = sentence + index_to_src[encoded_word] + ' ' return sentence # ๋ฒˆ์—ญ๋ฌธ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ def seq_to_tar(input_seq): sentence = '' for encoded_word in input_seq: if(encoded_word != 0 and encoded_word != tar_to_index['<sos>'] and encoded_word != tar_to_index['<eos>']): sentence = sentence + index_to_tar[encoded_word] + ' ' return sentence ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ž„์˜๋กœ ์„ ํƒํ•œ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. for seq_index in [3, 50, 100, 300, 1001]: input_seq = encoder_input_train[seq_index: seq_index + 1] decoded_sentence = decode_sequence(input_seq) print("์ž…๋ ฅ ๋ฌธ์žฅ :",seq_to_src(encoder_input_train[seq_index])) print("์ •๋‹ต ๋ฌธ์žฅ :",seq_to_tar(decoder_input_train[seq_index])) print("๋ฒˆ์—ญ ๋ฌธ์žฅ :",decoded_sentence[1:-5]) print("-"*50) ์ž…๋ ฅ ๋ฌธ์žฅ : when does it end ? ์ •๋‹ต ๋ฌธ์žฅ : quand est ce que ca finit ? ๋ฒˆ์—ญ ๋ฌธ์žฅ : quand est ce que ca marche ? -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : it s sand . ์ •๋‹ต ๋ฌธ์žฅ : c est du sable . ๋ฒˆ์—ญ ๋ฌธ์žฅ : c est de l eau . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : i didn t go . ์ •๋‹ต ๋ฌธ์žฅ : je n y suis pas allee . ๋ฒˆ์—ญ ๋ฌธ์žฅ : je ne suis pas encore . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : it was a mistake . ์ •๋‹ต ๋ฌธ์žฅ : ce fut une erreur . ๋ฒˆ์—ญ ๋ฌธ์žฅ : il s agit d une blague . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : it boggles my mind . ์ •๋‹ต ๋ฌธ์žฅ : ca me laisse perplexe . ๋ฒˆ์—ญ ๋ฌธ์žฅ : ca m en femme . -------------------------------------------------- ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ž„์˜๋กœ ์„ ํƒํ•œ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. for seq_index in [3, 50, 100, 300, 1001]: input_seq = encoder_input_test[seq_index: seq_index + 1] decoded_sentence = decode_sequence(input_seq) print("์ž…๋ ฅ ๋ฌธ์žฅ :",seq_to_src(encoder_input_test[seq_index])) print("์ •๋‹ต ๋ฌธ์žฅ :",seq_to_tar(decoder_input_test[seq_index])) print("๋ฒˆ์—ญ ๋ฌธ์žฅ :",decoded_sentence[1:-5]) print("-"*50) ์ž…๋ ฅ ๋ฌธ์žฅ : we are busy men . ์ •๋‹ต ๋ฌธ์žฅ : nous sommes des hommes occupes . ๋ฒˆ์—ญ ๋ฌธ์žฅ : nous sommes tres vieux . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : it was very ugly . ์ •๋‹ต ๋ฌธ์žฅ : ce n etait vraiment pas beau a voir . ๋ฒˆ์—ญ ๋ฌธ์žฅ : c etait tres fort . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : tom looks shocked . ์ •๋‹ต ๋ฌธ์žฅ : tom a l air choque . ๋ฒˆ์—ญ ๋ฌธ์žฅ : tom a l air bien . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : cross the street . ์ •๋‹ต ๋ฌธ์žฅ : traversez la rue . ๋ฒˆ์—ญ ๋ฌธ์žฅ : la ? -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : you nearly died . ์ •๋‹ต ๋ฌธ์žฅ : tu es presque mort . ๋ฒˆ์—ญ ๋ฌธ์žฅ : tu es presque mort . -------------------------------------------------- 14-03 BLEU Score(Bilingual Evaluation Understudy Score) ์•ž์„œ ์–ธ์–ด ๋ชจ๋ธ(Language Model)์˜ ์„ฑ๋Šฅ ์ธก์ •์„ ์œ„ํ•œ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์œผ๋กœ ํŽ„ ํ”Œ๋ ‰์„œํ‹ฐ(perplexity, PPL)๋ฅผ ์†Œ๊ฐœํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ์—๋„ PPL์„ ํ‰๊ฐ€์— ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, PPL์€ ๋ฒˆ์—ญ์˜ ์„ฑ๋Šฅ์„ ์ง์ ‘์ ์œผ๋กœ ๋ฐ˜์˜ํ•˜๋Š” ์ˆ˜์น˜๋ผ ๋ณด๊ธฐ์—” ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ๋Š” ๊ทธ ์™ธ์—๋„ ์ˆ˜๋งŽ์€ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•๋“ค์ด ์กด์žฌํ•˜๋Š”๋ฐ, ๊ธฐ๊ณ„ ๋ฒˆ์—ญ์˜ ์„ฑ๋Šฅ์ด ์–ผ๋งˆ๋‚˜ ๋›ฐ์–ด๋‚œ๊ฐ€๋ฅผ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฐฉ๋ฒ•์ธ BLEU(Bilingual Evaluation Understudy) ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ง„ํ–‰๋˜๋Š” ์„ค๋ช…์€ ๋…ผ๋ฌธ BLEU: a Method for Automatic Evaluation of Machine Translation๋ฅผ ์ฐธ๊ณ ๋กœ ํ•˜์—ฌ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. import numpy as np from collections import Counter from nltk import ngrams 1. BLEU(Bilingual Evaluation Understudy) BLEU๋Š” ๊ธฐ๊ณ„ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ์™€ ์‚ฌ๋žŒ์ด ์ง์ ‘ ๋ฒˆ์—ญํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€ ๋น„๊ตํ•˜์—ฌ ๋ฒˆ์—ญ์— ๋Œ€ํ•œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ธก์ • ๊ธฐ์ค€์€ n-gram์— ๊ธฐ๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. n-gram์˜ ์ •์˜๋Š” ์–ธ์–ด ๋ชจ๋ธ ์ฑ•ํ„ฐ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. BLEU๋Š” ์™„๋ฒฝํ•œ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ ๋Š” ํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ ๋ช‡ ๊ฐ€์ง€ ์ด์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์–ธ์–ด์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ณ„์‚ฐ ์†๋„๊ฐ€ ๋น ๋ฆ…๋‹ˆ๋‹ค. BLEU๋Š” PPL ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๋†’์„์ˆ˜๋ก ์„ฑ๋Šฅ์ด ๋” ์ข‹์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. BLEU๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๊ณ„ ๋ฒˆ์—ญ ์„ฑ๋Šฅ ํ‰๊ฐ€๋ฅผ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ์ง๊ด€์ ์ธ ๋ฐฉ๋ฒ•์„ ๋จผ์ € ์ œ์‹œํ•˜๊ณ , ๋ฌธ์ œ์ ์„ ๋ณด์™„ํ•ด๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 1) ๋‹จ์–ด ๊ฐœ์ˆ˜ ์นด์šดํŠธ๋กœ ์ธก์ •ํ•˜๊ธฐ(Unigram Precision) ํ•œ๊ตญ์–ด-์˜์–ด ๋ฒˆ์—ญ๊ธฐ์˜ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ๋‘ ๊ฐœ์˜ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๊ฐ€ ์กด์žฌํ•˜๊ณ  ๋‘ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ์— ๊ฐ™์€ ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์—ฌ ๋ฒˆ์—ญ๋œ ์˜์–ด ๋ฌธ์žฅ์˜ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋ฒˆ์—ญ๋œ ๋ฌธ์žฅ์„ ๊ฐ๊ฐ Candidate1, 2๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด ๋ฌธ์žฅ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ •๋‹ต์œผ๋กœ ๋น„๊ต๋˜๋Š” ๋ฌธ์žฅ์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์„ธ ๋ช…์˜ ์‚ฌ๋žŒ์—๊ฒŒ ํ•œ๊ตญ์–ด๋ฅผ ๋ณด๊ณ  ์˜์ž‘ํ•ด ๋ณด๋ผ๊ณ  ํ•˜์—ฌ ์„ธ ๊ฐœ์˜ ๋ฒˆ์—ญ ๋ฌธ์žฅ์„ ๋งŒ๋“ค์–ด๋ƒˆ์Šต๋‹ˆ๋‹ค. ์ด ์„ธ ๋ฌธ์žฅ์„ ๊ฐ๊ฐ Reference1, 2, 3๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. Example 1 Candidate1 : It is a guide to action which ensures that the military always obeys the commands of the party. Candidate2 : It is to insure the troops forever hearing the activity guidebook that party direct. Reference1 : It is a guide to action that ensures that the military will forever heed Party commands. Reference2 : It is the guiding principle which guarantees the military forces always being under the command of the Party. Reference3 : It is the practical guide for the army always to heed the directions of the party. ํŽธ์˜์ƒ Candidate๋ฅผ Ca๋กœ, Reference๋ฅผ Ref๋กœ ์ถ•์•ฝํ•˜์—ฌ ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. Ca 1, 2๋ฅผ Ref 1, 2, 3๊ณผ ๋น„๊ตํ•˜์—ฌ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ง๊ด€์ ์ธ ์„ฑ๋Šฅ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์€ Ref 1, 2, 3 ์ค‘ ์–ด๋Š ํ•œ ๋ฌธ์žฅ์ด๋ผ๋„ ๋“ฑ์žฅํ•œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ Ca์—์„œ ์„ธ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ํ›„์— Ca์˜ ๋ชจ๋“  ๋‹จ์–ด์˜ ์นด์šดํŠธ์˜ ํ•ฉ. ์ฆ‰, Ca์—์„œ์˜ ์ด ๋‹จ์–ด์˜ ์ˆ˜์œผ๋กœ ๋‚˜๋ˆ ์ค๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ธก์ • ๋ฐฉ๋ฒ•์„ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„(Unigram Precision)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋“ค ์ค‘์—์„œ ์กด์žฌํ•˜๋Š”์˜ ๋‹จ์–ด์˜ ์ˆ˜ ์˜์ด ๋‹จ์–ด ์ˆ˜ Unigram Precision = Ref๋“ค ์ค‘์—์„œ ์กด์žฌํ•˜๋Š” Ca์˜ ๋‹จ์–ด์˜ ์ˆ˜ Ca์˜ ์ด ๋‹จ์–ด ์ˆ˜ the number of Ca words(unigrams) which occur in any Ref the total number of words in the Ca Ca1์˜ ๋‹จ์–ด๋“ค์€ ์–ผ์ถ” ํ›‘์–ด๋งŒ ๋ด๋„ Ref1, Ref2, Ref3์—์„œ ์ „๋ฐ˜์ ์œผ๋กœ ๋“ฑ์žฅํ•˜๋Š” ๋ฐ˜๋ฉด, Ca2๋Š” ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” Ca1์ด Ca2๋ณด๋‹ค ๋” ์ข‹์€ ๋ฒˆ์—ญ ๋ฌธ์žฅ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Ca1์˜ It is a guide to action์€ Ref1์—์„œ, which๋Š” Ref2์—์„œ, ensures that the militrary๋Š” Ref1์—์„œ, always๋Š” Ref2์™€ Ref3์—์„œ, commands๋Š” Ref1์—์„œ, of the party๋Š” Ref2์—์„œ ๋“ฑ์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. (๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์€ ์—†๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค.) Ca1์— ์žˆ๋Š” ๋‹จ์–ด ์ค‘ Ref1, Ref2, Ref3 ์–ด๋””์—๋„ ๋“ฑ์žฅํ•˜์ง€ ์•Š์€ ๋‹จ์–ด๋Š” obeys๋ฟ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, Ca2๋Š” Ca1๊ณผ ๋น„๊ตํ•˜์—ฌ ์ƒ๋Œ€์ ์œผ๋กœ Ref1, 2, 3์— ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋“ค์ด ์ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์— ๋”ฐ๋ฅด๋ฉด Ca1๊ณผ Ca2์˜ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” ๊ฐ๊ฐ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. Ca1 Unigram Precision = 17 18 Ca2 Unigram Precision = 14 ์ด์ œ๋ถ€ํ„ฐ๋Š” ๋‹จ์–ด๋ผ๋Š” ํ‘œํ˜„๋ณด๋‹ค๋Š” ์œ ๋‹ˆ๊ทธ๋žจ์ด๋ผ๋Š” ์šฉ์–ด๋กœ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” ๋‚˜๋ฆ„ ์˜๋ฏธ ์žˆ๋Š” ์ธก์ • ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณด์ด์ง€๋งŒ ์‚ฌ์‹ค ํ—ˆ์ˆ ํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒˆ๋กœ์šด ์˜ˆ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. 2) ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜์—ฌ ๋ณด์ •ํ•˜๊ธฐ(Modified Unigram Precision) Example 2 Candidate : the the the the the the the Reference1 : the cat is on the mat Reference2 : there is a cat on the mat ์œ„์˜ Ca๋Š” the๋งŒ 7๊ฐœ๊ฐ€ ๋“ฑ์žฅํ•œ ํ„ฐ๋ฌด๋‹ˆ์—†๋Š” ๋ฒˆ์—ญ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๋ฒˆ์—ญ์€ ์•ž์„œ ๋ฐฐ์šด ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์— ๋”ฐ๋ฅด๋ฉด 7 1 ์ด๋ผ๋Š” ์ตœ๊ณ ์˜ ์„ฑ๋Šฅ ํ‰๊ฐ€๋ฅผ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์— ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ๋‹ค์†Œ ๋ณด์ •ํ•  ํ•„์š”๋ฅผ ๋Š๋‚๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ณด์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ •๋ฐ€๋„์˜ ๋ถ„์ž๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด Ref์™€ ๋งค์นญํ•˜๋ฉฐ ์นด์šดํŠธํ•˜๋Š” ๊ณผ์ •์—์„œ Ca์˜ ์œ ๋‹ˆ๊ทธ๋žจ์ด ์ด๋ฏธ Ref์—์„œ ๋งค์นญ๋œ ์ ์ด ์žˆ์—ˆ๋Š”์ง€๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋“ค๊ณผ๋ฅผ ๊ณ ๋ คํ•œ ์ƒˆ๋กœ์šด ์นด์šดํŠธ ๋ฐฉ๋ฒ•์ด ํ•„์š” ์˜์ด ์œ ๋‹ˆ ๊ทธ๋žจ ์ˆ˜ Unigram Precision = Ref ๋“ค๊ณผ Ca๋ฅผ ๊ณ ๋ คํ•œ ์ƒˆ๋กœ์šด ์นด์šดํŠธ ๋ฐฉ๋ฒ•์ด ํ•„์š”! Ca์˜ ์ด ์œ ๋‹ˆ๊ทธ๋žจ ์ˆ˜ ์ •๋ฐ€๋„์˜ ๋ถ„์ž๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์นด์šดํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ฉ์‹œ๋‹ค. ์šฐ์„ , ์œ ๋‹ˆ๊ทธ๋žจ์ด ํ•˜๋‚˜์˜ Ref์—์„œ ์ตœ๋Œ€ ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ–ˆ๋Š”์ง€๋ฅผ ์นด์šดํŠธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์„ maximum reference count๋ฅผ ์ค„์ธ ์˜๋ฏธ์—์„œ Max_Ref_Count๋ผ๊ณ  ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. Max_Ref_Count๊ฐ€ ๊ธฐ์กด์˜ ๋‹จ์ˆœ ์นด์šดํŠธํ•œ ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ์—๋Š” ์ด ๊ฐ’์„ ์ตœ์ข… ์นด์šดํŠธ ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฐ€๋„์˜ ๋ถ„์ž ๊ณ„์‚ฐ์„ ์œ„ํ•œ ์ƒˆ๋กœ์šด ์นด์šดํŠธ ๋ฐฉ์‹์„ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o n c i = m n ( o n , M x R f C u t ) ์œ„์˜ ์นด์šดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„์ž๋ฅผ ๊ณ„์‚ฐํ•œ ์ •๋ฐ€๋„๋ฅผ ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„(Modified Unigram Precision)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜๊ฐ์œ ๋‹ˆ๊ทธ๋žจ์—๋Œ€ํ•ด์„์ˆ˜ํ–‰ํ•œ๊ฐ’์˜์ดํ•ฉ ์˜์ด ์œ ๋‹ˆ ๊ทธ๋žจ ์ˆ˜ Modified Unigram Precision = Ca์˜ ๊ฐ ์œ ๋‹ˆ๊ทธ๋žจ์— ๋Œ€ํ•ด o n c i ์„ ์ˆ˜ํ–‰ํ•œ ๊ฐ’์˜ ์ดํ•ฉ Ca์˜ ์ด ์œ ๋‹ˆ๊ทธ๋žจ ์ˆ˜ ๋ถ„๋ชจ์˜ ๊ฒฝ์šฐ์—๋Š” ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ Ca์˜ ๋ชจ๋“  ์œ ๋‹ˆ๊ทธ๋žจ์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ o n ํ•˜๊ณ  ๋ชจ๋‘ ํ•ฉํ•œ ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. Example 2๋ฅผ ๋ณผ๊นŒ์š”? the์˜ ๊ฒฝ์šฐ์—๋Š” Ref1์—์„œ ์ด ๋‘ ๋ฒˆ ๋“ฑ์žฅํ•˜์˜€์œผ๋ฏ€๋กœ, the์˜ ์นด์šดํŠธ๋Š” 2๋กœ ๋ณด์ •๋ฉ๋‹ˆ๋‹ค. Ca์˜ ๊ธฐ์กด ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 7 1 ์ด์—ˆ์œผ๋‚˜ ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 7 ์™€ ๊ฐ™์ด ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์˜ˆ๋กœ Example 1์—์„œ์˜ Ca1์˜ ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๋ฉด ๋ณด์ •๋˜๊ธฐ ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ 17 18 ์ด์ง€๋งŒ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ๊ณผ์ •์€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. Ca1์—์„œ the๋Š” 3๋ฒˆ ๋“ฑ์žฅํ•˜์ง€๋งŒ, Re2์™€ Ref3์—์„œ the๊ฐ€ 4๋ฒˆ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ 3์ด 4๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ the๋Š” 3์œผ๋กœ ์นด์šดํŠธ๋ฉ๋‹ˆ๋‹ค. the ์™ธ์— Ca1์˜ ๋ชจ๋“  ์œ ๋‹ˆ๊ทธ๋žจ์€ ์ „๋ถ€ 1๊ฐœ์”ฉ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ ๋ณด์ • ์ „๊ณผ ๋™์ผํ•˜๊ฒŒ ์นด์šดํŠธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ณด์ • ์ด์ „์˜ ์ •๋ฐ€๋„์™€ ๋™์ผํ•˜๊ฒŒ 17 18 ์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 3) ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„ (Modified Unigram Precision) ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ํŒŒ์ด์ฌ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ ๋‹ˆ๊ทธ๋žจ์„ ์นด์šดํŠธํ•˜๋Š” o n ํ•จ์ˆ˜์™€ o n c i ํ•จ์ˆ˜ ๋‘ ๊ฐ€์ง€ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ถ„๋ชจ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ o n ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋ถ„์ž๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ o n c i ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์œ ๋‹ˆ๊ทธ๋žจ์„ ๋‹จ์ˆœํžˆ o n ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ simple_count๋ผ๋Š” ์ด๋ฆ„์˜ ์•„๋ž˜ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. # ํ† ํฐํ™”๋œ ๋ฌธ์žฅ(tokens)์—์„œ n-gram์„ ์นด์šดํŠธ def simple_count(tokens, n): return Counter(ngrams(tokens, n)) ์œ„ ํ•จ์ˆ˜๋Š” ํ† ํฐํ™”๋œ ๋ฌธ์žฅ์„ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋ฌธ์žฅ ๋‚ด์˜ n-gram์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์€ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์ด๋ฏ€๋กœ ์นด์šดํŠธํ•˜๊ณ ์ž ํ•˜๋Š” n-gram์˜ ๋‹จ์œ„๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” simple_count ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ n์˜ ๊ฐ’์„ 1๋กœ ํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Example 1์˜ Ca1๋ฅผ ๊ฐ€์ ธ์™€ ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ค ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. candidate = "It is a guide to action which ensures that the military always obeys the commands of the party." tokens = candidate.split() # ํ† ํฐํ™” result = simple_count(tokens, 1) # n = 1์€ ์œ ๋‹ˆ๊ทธ๋žจ print('์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ :',result) ์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ : Counter({('the',): 3, ('It',): 1, ('is',): 1, ('a',): 1, ('guide',): 1, ('to',): 1, ('action',): 1, ('which',): 1, ('ensures',): 1, ('that',): 1, ('military',): 1, ('always',): 1, ('obeys',): 1, ('commands',): 1, ('of',): 1, ('party.',): 1}) ์œ„์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋ชจ๋“  ์œ ๋‹ˆ๊ทธ๋žจ์„ ์นด์šดํŠธํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์œ ๋‹ˆ๊ทธ๋žจ์ด 1๊ฐœ์”ฉ ์นด์šดํŠธ๋˜์—ˆ์œผ๋‚˜ ์œ ๋‹ˆ๊ทธ๋žจ the๋Š” ๋ฌธ์žฅ์—์„œ 3๋ฒˆ ๋“ฑ์žฅํ•˜์˜€์œผ๋ฏ€๋กœ ์œ ์ผํ•˜๊ฒŒ 3์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” Example 2์˜ Ca๋ฅผ ๊ฐ€์ง€๊ณ  ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. candidate = 'the the the the the the the' tokens = candidate.split() # ํ† ํฐํ™” result = simple_count(tokens, 1) # n = 1์€ ์œ ๋‹ˆ๊ทธ๋žจ print('์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ :',result) ์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ : Counter({('the',): 7}) simple_count ํ•จ์ˆ˜๋Š” ๋‹จ์ˆœ ์นด์šดํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ the์— ๋Œ€ํ•ด์„œ 7์ด๋ผ๋Š” ์นด์šดํŠธ ๊ฐ’์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. o n์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์œผ๋‹ˆ ์ด๋ฒˆ์—๋Š” o n c i ์„ ์•„๋ž˜์˜ count_clip ์ด๋ฆ„์„ ๊ฐ€์ง„ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. def count_clip(candidate, reference_list, n): # Ca ๋ฌธ์žฅ์—์„œ n-gram ์นด์šดํŠธ ca_cnt = simple_count(candidate, n) max_ref_cnt_dict = dict() for ref in reference_list: # Ref ๋ฌธ์žฅ์—์„œ n-gram ์นด์šดํŠธ ref_cnt = simple_count(ref, n) # ๊ฐ Ref ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋น„๊ตํ•˜์—ฌ n-gram์˜ ์ตœ๋Œ€ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ๊ณ„์‚ฐ. for n_gram in ref_cnt: if n_gram in max_ref_cnt_dict: max_ref_cnt_dict[n_gram] = max(ref_cnt[n_gram], max_ref_cnt_dict[n_gram]) else: max_ref_cnt_dict[n_gram] = ref_cnt[n_gram] return { # count_clip = min(count, max_ref_count) n_gram: min(ca_cnt.get(n_gram, 0), max_ref_cnt_dict.get(n_gram, 0)) for n_gram in ca_cnt } count_clip ํ•จ์ˆ˜๋Š” candidate ๋ฌธ์žฅ๊ณผ reference ๋ฌธ์žฅ๋“ค, ๊ทธ๋ฆฌ๊ณ  ์นด์šดํŠธ ๋‹จ์œ„๊ฐ€ ๋˜๋Š” n-gram์—์„œ์˜ n์˜ ๊ฐ’ ์ด ์„ธ ๊ฐ€์ง€๋ฅผ ์ธ์ž๋กœ ์ž…๋ ฅ๋ฐ›์•„์„œ o n c i ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์—ญ์‹œ๋‚˜ n=1๋กœ ํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ count_clip ํ•จ์ˆ˜ ๋‚ด๋ถ€์—๋Š” ๊ธฐ์กด์— ๊ตฌํ˜„ํ–ˆ๋˜ simple_count ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. o n c i ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” a _ e _ o n ๊ฐ’๊ณผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด o n ๊ฐ’์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. Example 2๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜๊ฐ€ ์ •์ƒ ์ž‘๋™๋˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. candidate = 'the the the the the the the' references = [ 'the cat is on the mat', 'there is a cat on the mat' ] result = count_clip(candidate.split(),list(map(lambda ref: ref.split(), references)),1) print('๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ :',result) ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ : {('the',): 2} ๋™์ผํ•œ ์˜ˆ์ œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์œ„์˜ simple_count ํ•จ์ˆ˜๋Š” the๊ฐ€ 7๊ฐœ๋กœ ์นด์šดํŠธ๋˜์—ˆ๋˜ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ด๋ฒˆ์—๋Š” 2๊ฐœ๋กœ ์นด์šดํŠธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋‘ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋ณด์ •๋œ ์ •๋ฐ€๋„๋ฅผ ์—ฐ์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ modified_precision๋ž€ ์ด๋ฆ„์˜ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. def modified_precision(candidate, reference_list, n): clip_cnt = count_clip(candidate, reference_list, n) total_clip_cnt = sum(clip_cnt.values()) # ๋ถ„์ž cnt = simple_count(candidate, n) total_cnt = sum(cnt.values()) # ๋ถ„๋ชจ # ๋ถ„๋ชจ๊ฐ€ 0์ด ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ if total_cnt == 0: total_cnt = 1 # ๋ถ„์ž : count_clip์˜ ํ•ฉ, ๋ถ„๋ชจ : ๋‹จ์ˆœ count์˜ ํ•ฉ ==> ๋ณด์ •๋œ ์ •๋ฐ€๋„ return (total_clip_cnt / total_cnt) result = modified_precision(candidate.split(), list(map(lambda ref: ref.split(), references)), n=1) print('๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„ :',result) ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„ : 0.2857142857142857 ์†Œ์ˆ˜ ๊ฐ’์ด ๋‚˜์˜ค๋Š”๋ฐ ์ด๋Š” 7 ์˜ ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์•ž์„œ ์œก์•ˆ์œผ๋กœ ๊ณ„์‚ฐํ–ˆ๋˜ Example 2์—์„œ Ca์˜ ๋ณด์ •๋œ ์ •๋ฐ€๋„์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜๊ณ , ์ง์ ‘ ๊ตฌํ˜„๊นŒ์ง€ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์„ค๋ช…์—์„œ ์–ธ๊ธ‰ํ•˜๋Š” '์ •๋ฐ€๋„'๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณด์ •๋œ ์ •๋ฐ€๋„(Modified Precision)๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฐ€๋„๋ฅผ ๋ณด์ •ํ•จ์œผ๋กœ์จ Ca์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋‹จ์–ด ์ค‘๋ณต์— ๋Œ€ํ•œ ๋ฌธ์ œ์ ์€ ํ•ด๊ฒฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๊ฐ€ ๊ฐ€์ง€๋Š” ๋ณธ์งˆ์ ์ธ ๋ฌธ์ œ์ ์ด ์žˆ๊ธฐ์— ์œ ๋‹ˆ๊ทธ๋žจ์„ ๋„˜์–ด ๋ฐ”์ด ๊ทธ๋žจ, ํŠธ๋ผ์ด ๊ทธ๋žจ ๋“ฑ๊ณผ ๊ฐ™์ด n-gram์œผ๋กœ ํ™•์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ์ ์ด ๋ฌด์—‡์ธ์ง€ ์ดํ•ดํ•˜๊ณ , ์–ด๋–ป๊ฒŒ n-gram์œผ๋กœ ํ™•์žฅํ•˜๋Š”์ง€ ํ•™์Šตํ•ด ๋ด…์‹œ๋‹ค. 4) ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜๊ธฐ ์œ„ํ•ด์„œ n-gram์œผ๋กœ ํ™•์žฅํ•˜๊ธฐ BoW ํ‘œํ˜„๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ, ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์™€ ๊ฐ™์ด ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฒฐ๊ตญ ๋‹จ์–ด์˜ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. Example 1์— Ca3์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์žฅ์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ณ  ๊ธฐ์กด์˜ Ca1๊ณผ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. Example 1 Candidate1 : It is a guide to action which ensures that the military always obeys the commands of the party. Candidate2 : It is to insure the troops forever hearing the activity guidebook that party direct. Candidate3 : the that military a is It guide ensures which to commands the of action obeys always party the. Reference1 : It is a guide to action that ensures that the military will forever heed Party commands. Reference2 : It is the guiding principle which guarantees the military forces always being under the command of the Party. Reference3 : It is the practical guide for the army always to heed the directions of the party. Ca3์€ ์‚ฌ์‹ค Ca1์—์„œ ๋ชจ๋“  ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์ˆœ์„œ๋ฅผ ๋žœ๋ค์œผ๋กœ ์„ž์€ ์‹ค์ œ ์˜์–ด ๋ฌธ๋ฒ•์— ๋งž์ง€ ์•Š์€ ๋ฌธ์žฅ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Ref 1, 2, 3๊ณผ ๋น„๊ตํ•˜์—ฌ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ์ ์šฉํ•˜๋ฉด Ca1๊ณผ Ca3์˜ ๋‘ ์ •๋ฐ€๋„๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์ˆœ์„œ๋ฅผ ์ „ํ˜€ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ๋Œ€์•ˆ์œผ๋กœ ๊ฐœ๋ณ„์ ์ธ ์œ ๋‹ˆ๊ทธ๋žจ/๋‹จ์–ด๋กœ์„œ ์นด์šดํŠธํ•˜๋Š” ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์—์„œ ๋‹ค์Œ์— ๋“ฑ์žฅํ•œ ๋‹จ์–ด๊นŒ์ง€ ํ•จ๊ป˜ ๊ณ ๋ คํ•˜์—ฌ ์นด์šดํŠธํ•˜๋„๋ก ์œ ๋‹ˆ๊ทธ๋žจ ์™ธ์—๋„ Bigram, Trigram, 4-gram ๋‹จ์œ„ ๋“ฑ์œผ๋กœ ๊ณ„์‚ฐํ•œ ์ •๋ฐ€๋„. ์ฆ‰, n-gram์„ ์ด์šฉํ•œ ์ •๋ฐ€๋„๋ฅผ ๋„์ž…ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด๋“ค ๊ฐ๊ฐ์€ ์นด์šดํŠธ ๋‹จ์œ„๋ฅผ 2๊ฐœ, 3๊ฐœ, 4๊ฐœ๋กœ ๋ณด๋Š๋ƒ์˜ ์ฐจ์ด๋กœ 2-gram Precision, 3-gram Precision, 4-gram Precision์ด๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ์˜๋ฏธ์ธ์ง€ ๋ฐ”์ด ๊ทธ๋žจ(Bigram) ๋‹จ์œ„๋กœ ์นด์šดํŠธํ•˜์—ฌ Example 1, 2์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„(Bigram Precision)๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ข€ ๋” ์‰ฌ์šด Example 2๋ถ€ํ„ฐ ๋ณผ๊นŒ์š”? Example 2 Candidate1 : the the the the the the the Candidate2 : the cat the cat on the mat Reference1 : the cat is on the mat Reference2 : there is a cat on the mat ์ดํ•ด๋ฅผ ๋•๊ณ ์ž Example 2์— Ca2๋ฅผ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. Ca2 ๋ฐ”์ด ๊ทธ๋žจ์˜ o n ์™€ o n c i ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฐ”์ด ๊ทธ๋žจ the cat cat the cat on on the the mat SUM o n 2 1 1 1 1 6 o n c i 1 0 1 1 1 4 ๊ฒฐ๊ณผ์ ์œผ๋กœ Ca2์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 6 ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๋‹น์—ฐํ•˜๊ฒŒ๋„ Ca1์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 0์ž…๋‹ˆ๋‹ค. Example 1์€ ์–ด๋–จ๊นŒ์š”? Example 1์—์„œ Ca1์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 10 17 ์ด๋ฉฐ, Ca2์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 13 ์ž…๋‹ˆ๋‹ค. Ca1์—์„œ ๋‹จ์–ด์˜ ์ˆœ์„œ๋ฅผ ๋’ค์„ž์€ Ca3์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” ๋…์ž๋ถ„๋“ค์˜ ์ˆ™์ œ๋กœ ๋‚จ๊น๋‹ˆ๋‹ค. ๋ณด์ •๋œ ์ •๋ฐ€๋„๋ฅผ ์‹์œผ๋กœ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. n ์—์„œ ์€ n-gram์—์„œ์˜ ์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, ์•ž์„œ ๋ฐฐ์šด ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์˜ ์‹์„ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. 1 โˆ‘ n g a โˆˆ a d d t C u t l p ( n g a) u i r m C n i a e C u t ( n g a) ์ด๋ฅผ n-gram์œผ๋กœ ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. n โˆ‘ - r m C n i a e C u t l p ( - r m ) n g a โˆˆ a d d t C u t ( - r m ) ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์—์„œ๋Š” ์ด 1์ด๋ฏ€๋กœ 1 ๋กœ ํ‘œํ˜„ํ•˜์˜€์œผ๋‚˜, ์ผ๋ฐ˜ํ™”๋œ ์‹์—์„œ๋Š” n ์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ณด์ •๋œ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„ 2 , ๋ณด์ •๋œ ํŠธ๋ผ์ด ๊ทธ๋žจ ์ •๋ฐ€๋„ 3 ๋“ฑ์— ๋Œ€ํ•œ ํŒŒ์ด์ฌ ์‹ค์Šต์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค n ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ ๋ณ„๋„๋กœ ๋‹ค์‹œ ๊ตฌํ˜„ํ•  ํ•„์š”๋Š” ์—†๋Š”๋ฐ, ์•ž์„œ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜ simple_count, count_clip, modified_precision์€ ๋ชจ๋‘ n-gram์˜ n์„ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋ฐ›์œผ๋ฏ€๋กœ, n์„ 1๋Œ€์‹  ๋‹ค๋ฅธ ๊ฐ’์„ ๋„ฃ์–ด์„œ ์‹ค์Šตํ•ด ๋ณด๋ฉด ๋ฐ”์ด ๊ทธ๋žจ, ํŠธ๋ผ์ด ๊ทธ๋žจ ๋“ฑ์— ๋Œ€ํ•ด์„œ๋„ ๋ณด์ •๋œ ์ •๋ฐ€๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. n-gram ์ •๋ฐ€๋„ ์‹์„ ์ดํ•ดํ•˜์˜€๋‹ค๋ฉด BLEU์˜ ์ตœ์ข… ์‹๊นŒ์ง€ ๋‹ค ์™”์Šต๋‹ˆ๋‹ค. BLEU๋Š” ๋ณด์ •๋œ ์ •๋ฐ€๋„ 1 p, . , n ๋ฅผ ๋ชจ๋‘ ์กฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ชจ๋‘ ์กฐํ•ฉํ•œ BLEU์˜ ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. L U e p ( n 1 w log p) n : ๊ฐ gram์˜ ๋ณด์ •๋œ ์ •๋ฐ€๋„์ž…๋‹ˆ๋‹ค. : n-gram์—์„œ์˜ ์ตœ๋Œ€ ์ˆซ์ž์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต์€ 4์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด 4๋ผ๋Š” ๊ฒƒ์€ 1 p, 3 p๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. n : ๊ฐ gram์˜ ๋ณด์ •๋œ ์ •๋ฐ€๋„์— ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€์ค‘์น˜์˜ ํ•ฉ์€ 1๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด 4๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, 1 p, 3 p์— ๋Œ€ํ•ด์„œ ๋™์ผํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ์ฃผ๊ณ ์ž ํ•œ๋‹ค๋ฉด ๋ชจ๋‘ 0.25๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BLEU์˜ ์ตœ์ข…์‹์— ๊ฑฐ์˜ ๋‹ค ๋„๋‹ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์—ฌ์ „ํžˆ ์œ„์˜ BLEU ์‹์—๋„ ๋ฌธ์ œ์ ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 5) ์งง์€ ๋ฌธ์žฅ ๊ธธ์ด์— ๋Œ€ํ•œ ํŽ˜๋„ํ‹ฐ(Brevity Penalty) n-gram์œผ๋กœ ๋‹จ์–ด์˜ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ์—ฌ์ „ํžˆ ๋‚จ์•„์žˆ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”๋ฐ, ๋ฐ”๋กœ Ca์˜ ๊ธธ์ด์— BLEU์˜ ์ ์ˆ˜๊ฐ€ ๊ณผํ•œ ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด Example 1์— ๋‹ค์Œ์˜ Ca๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Example 1 Candidate4 : it is ์ด ๋ฌธ์žฅ์€ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋‚˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๊ฐ€ ๊ฐ๊ฐ 2 1๋กœ ๋‘ ์ •๋ฐ€๋„ ๋ชจ๋‘ 1์ด๋ผ๋Š” ๋†’์€ ์ •๋ฐ€๋„๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ด๊ณผ๊ฐ™์ด ์ œ๋Œ€๋กœ ๋œ ๋ฒˆ์—ญ์ด ์•„๋‹˜์—๋„ ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ ์งง๋‹ค๋Š” ์ด์œ ๋กœ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์€ ์ด์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ Ca๊ฐ€ Ref๋ณด๋‹ค ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ ์งง์€ ๊ฒฝ์šฐ์—๋Š” ์ ์ˆ˜์— ํŽ˜๋„ํ‹ฐ๋ฅผ ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ(Brevity Penalty)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. (์ง์—ญํ•˜๋ฉด ์งง์Œ ํŽ˜๋„ํ‹ฐ) ์ด์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šฐ๊ธฐ ์ „์—, ๋งŒ์•ฝ ๋ฐ˜๋Œ€๋กœ Ca์˜ ๊ธธ์ด๊ฐ€ Ref๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ์—๋„ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Example 3 Candidate 1: I always invariably perpetually do. Candidate 2: I always do. Reference 1: I always do. Reference 2: I invariably do. Reference 3: I perpetually do. Example 3์—์„œ Ca1์€ ๊ฐ€์žฅ ๋งŽ์€ ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ Ca2๋ณด๋‹ค ์ข‹์ง€ ๋ชปํ•œ ๋ฒˆ์—ญ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด Ref์˜ ๋‹จ์–ด๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•œ ๊ฒƒ์ด ๊ผญ ์ข‹์€ ๋ฒˆ์—ญ์ด๋ผ๋Š” ์˜๋ฏธ๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‹คํ–‰ํžˆ๋„ ์œ„์™€ ๊ฐ™์ด Ca์˜ ๊ธธ์ด๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ Ref๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ์—๋Š” BLEU ์ˆ˜์‹์—์„œ ์ •๋ฐ€๋„๋ฅผ n-gram์œผ๋กœ ํ™•์žฅํ•˜์—ฌ ๋ฐ”์ด ๊ทธ๋žจ, ํŠธ๋ผ์ด ๊ทธ๋žจ ์ •๋ฐ€๋„ ๋“ฑ์„ ๋ชจ๋‘ ๊ณ„์‚ฐ์— ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ์ด๋ฏธ ํŽ˜๋„ํ‹ฐ๋ฅผ ๋ฐ›๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ, ์ด ๊ฒฝ์šฐ๊นŒ์ง€ ๊ณ ๋ คํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ Ref๋ณด๋‹ค Ca์˜ ๊ธธ์ด๊ฐ€ ์งง์„ ๊ฒฝ์šฐ์— ํŽ˜๋„ํ‹ฐ๋ฅผ ์ฃผ๋Š” ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ์˜ ์ด์•ผ๊ธฐ๋กœ ๋Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ๋Š” ์•ž์„œ ๋ฐฐ์šด BLEU์˜ ์‹์— ๊ณฑํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ๋ฅผ ์ค„์—ฌ์„œ P ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ตœ์ข… BLEU์˜ ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. L U B ร— x ( n 1 w log p) ์œ„์˜ ์ˆ˜์‹์€ ํŽ˜๋„ํ‹ฐ๋ฅผ ์ค„ ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” P ์˜ ๊ฐ’์ด 1์ด์–ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐ˜์˜ํ•œ P ์˜ ์ˆ˜์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. P { if c r ( โˆ’ / ) if c r : Candidate์˜ ๊ธธ์ด : Candidate์™€ ๊ฐ€์žฅ ๊ธธ์ด ์ฐจ์ด๊ฐ€ ์ž‘์€ Reference์˜ ๊ธธ์ด Ref๊ฐ€ 1๊ฐœ๋ผ๋ฉด Ca์™€ Ref์˜ ๋‘ ๋ฌธ์žฅ์˜ ๊ธธ์ด๋งŒ์„ ๊ฐ€์ง€๊ณ  ๊ณ„์‚ฐํ•˜๋ฉด ๋˜๊ฒ ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” Ref๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์€ ๋ชจ๋“  Ref๋“ค ์ค‘์—์„œ Ca์™€ ๊ฐ€์žฅ ๊ธธ์ด ์ฐจ์ด๊ฐ€ ์ž‘์€ Ref์˜ ๊ธธ์ด๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์„ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. # Ca ๊ธธ์ด์™€ ๊ฐ€์žฅ ๊ทผ์ ‘ํ•œ Ref์˜ ๊ธธ์ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ def closest_ref_length(candidate, reference_list): ca_len = len(candidate) # ca ๊ธธ์ด ref_lens = (len(ref) for ref in reference_list) # Ref๋“ค์˜ ๊ธธ์ด # ๊ธธ์ด ์ฐจ์ด๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” Ref๋ฅผ ์ฐพ์•„์„œ Ref์˜ ๊ธธ์ด๋ฅผ ๋ฆฌํ„ด closest_ref_len = min(ref_lens, key=lambda ref_len: (abs(ref_len - ca_len), ref_len)) return closest_ref_len ๋งŒ์•ฝ Ca์™€ ๊ธธ์ด๊ฐ€ ์ •ํ™•ํžˆ ๋™์ผํ•œ Ref๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ธธ์ด ์ฐจ์ด๊ฐ€ 0์ธ ์ตœ๊ณ  ์ˆ˜์ค€์˜ ๋งค์น˜(best match length)์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋งŒ์•ฝ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธธ์ด์˜ Ref์ด์ง€๋งŒ Ca์™€ ๊ธธ์ด ์ฐจ์ด๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ์—๋Š” ๋” ์ž‘์€ ๊ธธ์ด์˜ Ref๋ฅผ ํƒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Ca๊ฐ€ ๊ธธ์ด๊ฐ€ 10์ธ๋ฐ, Ref 1, 2๊ฐ€ ๊ฐ๊ฐ 9์™€ 11์ด๋ผ๋ฉด ๊ธธ์ด ์ฐจ์ด๋Š” ๋™์ผํ•˜๊ฒŒ 1๋ฐ–์— ๋‚˜์ง€ ์•Š์ง€๋งŒ 9๋ฅผ ํƒํ•ฉ๋‹ˆ๋‹ค. closest_ref_length ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์„ ๊ตฌํ–ˆ๋‹ค๋ฉด, P ๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ brevity_penalty๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. def brevity_penalty(candidate, reference_list): ca_len = len(candidate) ref_len = closest_ref_length(candidate, reference_list) if ca_len > ref_len: return 1 # candidate๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด BP = 0 โ†’ BLEU = 0.0 elif ca_len == 0 : return 0 else: return np.exp(1 - ref_len/ca_len) ์œ„ ํ•จ์ˆ˜๋Š” ์•ž์„œ ๋ฐฐ์šด P ์˜ ์ˆ˜์‹์ฒ˜๋Ÿผ ๊ฐ€ ๋ณด๋‹ค ํด ๊ฒฝ์šฐ์—๋Š” 1์„ ๋ฆฌํ„ดํ•˜๊ณ , ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” 1 r c ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ BLEU ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜ bleu_score๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. def bleu_score(candidate, reference_list, weights=[0.25, 0.25, 0.25, 0.25]): bp = brevity_penalty(candidate, reference_list) # ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ, BP p_n = [modified_precision(candidate, reference_list, n=n) for n, _ in enumerate(weights, start=1)] # p1, p2, p3, ..., pn score = np.sum([w_i * np.log(p_i) if p_i != 0 else 0 for w_i, p_i in zip(weights, p_n)]) return bp * np.exp(score) ์œ„์˜ bleu_score ํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ์ด 4์— ๊ฐ gram์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๋Š” ๋™์ผํ•˜๊ฒŒ 0.25๋ผ ์ฃผ์–ด์ง„๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•จ์ˆ˜ ๋‚ด์—์„œ๋Š” P ๋ฅผ ๊ตฌํ•˜๊ณ  bp์—, 1 p, . , n ๋ฅผ ๊ตฌํ•˜์—ฌ p_n์— ์ €์žฅํ•˜๋„๋ก ๊ตฌํ˜„๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ž์„œ ๋ฐฐ์šด BLEU์˜ ์‹์— ๋”ฐ๋ผ ์ถ”๊ฐ€ ์—ฐ์‚ฐํ•˜์—ฌ ์ตœ์ข… ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์œ„ ํ•จ์ˆ˜๊ฐ€ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•ž์„œ ๊ตฌํ˜„ํ•œ simple_count, count_clip, modified_precision, brevity_penalty 4๊ฐœ์˜ ํ•จ์ˆ˜ ๋˜ํ•œ ๋ชจ๋‘ ๊ตฌํ˜„๋ผ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๊ตฌํ˜„ํ•œ BLEU ์ฝ”๋“œ๋กœ ๊ณ„์‚ฐ๋œ ์ ์ˆ˜์™€ NLTK ํŒจํ‚ค์ง€์— ์ด๋ฏธ ๊ตฌํ˜„๋ผ ์žˆ๋Š” BLEU ์ฝ”๋“œ๋กœ ๊ณ„์‚ฐ๋œ ์ ์ˆ˜๋ฅผ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. 2. NLTK๋ฅผ ์‚ฌ์šฉํ•œ BLEU ์ธก์ •ํ•˜๊ธฐ ํŒŒ์ด์ฌ์—์„œ๋Š” NLTK ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ BLEU๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. import nltk.translate.bleu_score as bleu candidate = 'It is a guide to action which ensures that the military always obeys the commands of the party' references = [ 'It is a guide to action that ensures that the military will forever heed Party commands', 'It is the guiding principle which guarantees the military forces always being under the command of the Party', 'It is the practical guide for the army always to heed the directions of the party' ] print('์‹ค์Šต ์ฝ”๋“œ์˜ BLEU :',bleu_score(candidate.split(),list(map(lambda ref: ref.split(), references)))) print('ํŒจํ‚ค์ง€ NLTK์˜ BLEU :',bleu.sentence_bleu(list(map(lambda ref: ref.split(), references)),candidate.split())) ์‹ค์Šต ์ฝ”๋“œ์˜ BLEU : 0.5045666840058485 ํŒจํ‚ค์ง€ NLTK์˜ BLEU : 0.5045666840058485 15. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜ (Attention Mechanism) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์‹ ๊ฒฝ๋ง๋“ค์˜ ์„ฑ๋Šฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด์ž, ์ด์ œ๋Š” AI ๋ถ„์•ผ์—์„œ ๋Œ€์„ธ ๋ชจ๋“ˆ๋กœ์„œ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 15-01 ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜ (Attention Mechanism) ์•ž์„œ ๋ฐฐ์šด seq2seq ๋ชจ๋ธ์€ ์ธ์ฝ”๋”์—์„œ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ผ๋Š” ํ•˜๋‚˜์˜ ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ ๋ฒกํ„ฐ ํ‘œํ˜„์œผ๋กœ ์••์ถ•ํ•˜๊ณ , ๋””์ฝ”๋”๋Š” ์ด ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ƒˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ RNN์— ๊ธฐ๋ฐ˜ํ•œ seq2seq ๋ชจ๋ธ์—๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ์งธ, ํ•˜๋‚˜์˜ ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ ๋ฒกํ„ฐ์— ๋ชจ๋“  ์ •๋ณด๋ฅผ ์••์ถ•ํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ ์ •๋ณด ์†์‹ค์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋‘˜์งธ, RNN์˜ ๊ณ ์งˆ์ ์ธ ๋ฌธ์ œ์ธ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค(vanishing gradient) ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด๋Š” ๊ธฐ๊ณ„ ๋ฒˆ์—ญ ๋ถ„์•ผ์—์„œ ์ž…๋ ฅ ๋ฌธ์žฅ์ด ๊ธธ๋ฉด ๋ฒˆ์—ญ ํ’ˆ์งˆ์ด ๋–จ์–ด์ง€๋Š” ํ˜„์ƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ๋Œ€์•ˆ์œผ๋กœ ์ž…๋ ฅ ์‹œํ€€์Šค๊ฐ€ ๊ธธ์–ด์ง€๋ฉด ์ถœ๋ ฅ ์‹œํ€€์Šค์˜ ์ •ํ™•๋„๊ฐ€ ๋–จ์–ด์ง€๋Š” ๊ฒƒ์„ ๋ณด์ •ํ•ด ์ฃผ๊ธฐ ์œ„ํ•œ ๋“ฑ์žฅํ•œ ๊ธฐ๋ฒ•์ธ ์–ดํ…์…˜(attention)์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 1. ์–ดํ…์…˜(Attention)์˜ ์•„์ด๋””์–ด ์–ดํ…์…˜์˜ ๊ธฐ๋ณธ ์•„์ด๋””์–ด๋Š” ๋””์ฝ”๋”์—์„œ ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋งค ์‹œ์ (time step)๋งˆ๋‹ค, ์ธ์ฝ”๋”์—์„œ์˜ ์ „์ฒด ์ž…๋ ฅ ๋ฌธ์žฅ์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ฐธ๊ณ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋‹จ, ์ „์ฒด ์ž…๋ ฅ ๋ฌธ์žฅ์„ ์ „๋ถ€ ๋‹ค ๋™์ผํ•œ ๋น„์œจ๋กœ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํ•ด๋‹น ์‹œ์ ์—์„œ ์˜ˆ์ธกํ•ด์•ผ ํ•  ๋‹จ์–ด์™€ ์—ฐ๊ด€์ด ์žˆ๋Š” ์ž…๋ ฅ ๋‹จ์–ด ๋ถ€๋ถ„์„ ์ข€ ๋” ์ง‘์ค‘(attention) ํ•ด์„œ ๋ณด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 2. ์–ดํ…์…˜ ํ•จ์ˆ˜(Attention Function) ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์–ธ๊ธ‰ํ•˜๊ธฐ ์ „์— ์ปดํ“จํ„ฐ๊ณตํ•™์˜ ๋งŽ์€ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” Key-Value๋กœ ๊ตฌ์„ฑ๋˜๋Š” ์ž๋ฃŒํ˜•์— ๋Œ€ํ•ด์„œ ์ž ๊น ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์ด ์ฑ…์˜ ์ฃผ ์–ธ์–ด๋กœ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ด์ฌ์—๋„ Key-Value๋กœ ๊ตฌ์„ฑ๋˜๋Š” ์ž๋ฃŒํ˜•์ธ ๋”•์…”๋„ˆ๋ฆฌ(Dict) ์ž๋ฃŒํ˜•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์€ ํ‚ค(Key)์™€ ๊ฐ’(Value)์ด๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š”๋ฐ, ํ‚ค๋ฅผ ํ†ตํ•ด์„œ ๋งคํ•‘๋œ ๊ฐ’์„ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. # ํŒŒ์ด์ฌ์˜ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์„ ์„ ์–ธ # ํ‚ค(Key) : ๊ฐ’(value)์˜<NAME>์œผ๋กœ ํ‚ค์™€ ๊ฐ’์˜ ์Œ(Pair)์„ ์„ ์–ธํ•œ๋‹ค. dict = {"2017" : "Transformer", "2018" : "BERT"} ์œ„์˜ ์ž๋ฃŒํ˜•์—์„œ 2017์€ ํ‚ค์— ํ•ด๋‹น๋˜๋ฉฐ, Transformer๋Š” 2017์˜ ํ‚ค์™€ ๋งคํ•‘๋˜๋Š” ๊ฐ’์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๊ทธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 2018์€ ํ‚ค์— ํ•ด๋‹น๋˜๋ฉฐ, BERT๋Š” 2018์ด๋ผ๋Š” ํ‚ค์™€ ๋งคํ•‘๋˜๋Š” ๊ฐ’์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. print(dict["2017"]) #2017์ด๋ผ๋Š” ํ‚ค์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์„ ์ถœ๋ ฅ Transformer print(dict["2018"]) #2018์ด๋ผ๋Š” ํ‚ค์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์„ ์ถœ๋ ฅ BERT Key-Value ์ž๋ฃŒํ˜•์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ๊ฐ€์ง€๊ณ  ์–ดํ…์…˜ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜์„ ํ•จ์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์ฃผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. Attention(Q, K, V) = Attention Value ์–ดํ…์…˜ ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ '์ฟผ๋ฆฌ(Query)'์— ๋Œ€ํ•ด์„œ ๋ชจ๋“  'ํ‚ค(Key)'์™€์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ตฌํ•ด๋‚ธ ์ด ์œ ์‚ฌ๋„๋ฅผ ํ‚ค์™€ ๋งคํ•‘๋˜์–ด์žˆ๋Š” ๊ฐ๊ฐ์˜ '๊ฐ’(Value)'์— ๋ฐ˜์˜ํ•ด ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ ์‚ฌ๋„๊ฐ€ ๋ฐ˜์˜๋œ '๊ฐ’(Value)'์„ ๋ชจ๋‘ ๋”ํ•ด์„œ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ด๋ฅผ ์–ดํ…์…˜ ๊ฐ’(Attention Value)์ด๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” seq2seq + ์–ดํ…์…˜ ๋ชจ๋ธ์—์„œ Q, K, V์— ํ•ด๋‹น๋˜๋Š” ๊ฐ๊ฐ์˜ Query, Keys, Values๋Š” ๊ฐ๊ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Q = Query : t ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ K = Keys : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค V = Values : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค ๊ฐ„๋‹จํ•œ ์–ดํ…์…˜ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์–ดํ…์…˜์„ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3. ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜(Dot-Product Attention) ์–ดํ…์…˜์€ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋Š”๋ฐ ๊ทธ์ค‘์—์„œ๋„ ๊ฐ€์žฅ ์ˆ˜์‹์ ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์ˆ˜์‹์„ ์ ์šฉํ•œ ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜(Dot-Product Attention)์„ ํ†ตํ•ด ์–ดํ…์…˜์„ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. seq2seq์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์–ดํ…์…˜ ์ค‘์—์„œ ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜๊ณผ ๋‹ค๋ฅธ ์–ดํ…์…˜์˜ ์ฐจ์ด๋Š” ์ฃผ๋กœ ์ค‘๊ฐ„ ์ˆ˜์‹์˜ ์ฐจ์ด๋กœ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ž์ฒด๋Š” ๊ฑฐ์˜ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๋””์ฝ”๋”์˜ ์„ธ ๋ฒˆ์งธ LSTM ์…€์—์„œ ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•  ๋•Œ, ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ LSTM ์…€์€ ์ด๋ฏธ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ํ†ตํ•ด je์™€ suis๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณค๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ๋Œ€ํ•ด ์ƒ์„ธํžˆ ์„ค๋ช…ํ•˜๊ธฐ ์ „์— ์œ„์˜ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์ „์ฒด์ ์ธ ๊ฐœ์š”๋งŒ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์„ธ ๋ฒˆ์งธ LSTM ์…€์€ ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ธ์ฝ”๋”์˜ ๋ชจ๋“  ์ž…๋ ฅ ๋‹จ์–ด๋“ค์˜ ์ •๋ณด๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ฐธ๊ณ ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ค‘๊ฐ„ ๊ณผ์ •์— ๋Œ€ํ•œ ์„ค๋ช…์€ ํ˜„์žฌ๋Š” ์ƒ๋žตํ•˜๊ณ  ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•  ๊ฒƒ์€ ์ธ์ฝ”๋”์˜ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ด๊ฐ’์€ I, am, a, student ๋‹จ์–ด ๊ฐ๊ฐ์ด ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•  ๋•Œ ์–ผ๋งˆ๋‚˜ ๋„์›€์ด ๋˜๋Š”์ง€์˜ ์ •๋„๋ฅผ ์ˆ˜์น˜ํ™”ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ๋นจ๊ฐ„ ์ง์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ๊ฒฐ๊ด๊ฐ’์˜ ํฌ๊ธฐ๋ฅผ ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ง์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๊ฐ€ ํด์ˆ˜๋ก ๋„์›€์ด ๋˜๋Š” ์ •๋„์˜ ํฌ๊ธฐ๊ฐ€ ํฝ๋‹ˆ๋‹ค. ๊ฐ ์ž…๋ ฅ ๋‹จ์–ด๊ฐ€ ๋””์ฝ”๋”์˜ ์˜ˆ์ธก์— ๋„์›€์ด ๋˜๋Š” ์ •๋„๊ฐ€ ์ˆ˜์น˜ํ™”ํ•˜์—ฌ ์ธก์ •๋˜๋ฉด ์ด๋ฅผ ํ•˜๋‚˜์˜ ์ •๋ณด๋กœ ๋‹ด์•„์„œ ๋””์ฝ”๋”๋กœ ์ „์†ก๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ดˆ๋ก์ƒ‰ ์‚ผ๊ฐํ˜•์ด ์ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ, ๋””์ฝ”๋”๋Š” ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ๋” ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•  ํ™•๋ฅ ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค. ์ข€ ๋” ์ƒ์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์–ดํ…์…˜ ์Šค์ฝ”์–ด(Attention Score)๋ฅผ ๊ตฌํ•œ๋‹ค. ์ธ์ฝ”๋”์˜ ์‹œ์ (time step)์„ ๊ฐ๊ฐ 1, 2, ... N์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ ๊ฐ๊ฐ 1 h, ... N ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๋””์ฝ”๋”์˜ ํ˜„์žฌ ์‹œ์ (time step) t์—์„œ์˜ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ t ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๋˜ํ•œ ์—ฌ๊ธฐ์„œ๋Š” ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›์ด ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์˜ ๊ฒฝ์šฐ์—๋Š” ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ๋™์ผํ•˜๊ฒŒ ์ฐจ์›์ด 4์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ์ฒซ๊ฑธ์Œ์ธ ์–ดํ…์…˜ ์Šค์ฝ”์–ด(Attention score)์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šฐ๊ธฐ ์ „์—, ์ด์ „ ์ฑ•ํ„ฐ ๋ฐฐ์› ๋˜ ๋””์ฝ”๋”์˜ ํ˜„์žฌ ์‹œ์  t์—์„œ ํ•„์š”ํ•œ ์ž…๋ ฅ๊ฐ’์„ ๋‹ค์‹œ ์ƒ๊ธฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹œ์  t์—์„œ ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋””์ฝ”๋”์˜ ์…€์€ ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’์„ ํ•„์š”๋กœ ํ•˜๋Š”๋ฐ, ๋ฐ”๋กœ ์ด์ „ ์‹œ์ ์ธ t-1์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์ด์ „ ์‹œ์  t-1์— ๋‚˜์˜จ ์ถœ๋ ฅ ๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ๋Š” ์ถœ๋ ฅ ๋‹จ์–ด ์˜ˆ์ธก์— ๋˜ ๋‹ค๋ฅธ ๊ฐ’์„ ํ•„์š”๋กœ ํ•˜๋Š”๋ฐ ๋ฐ”๋กœ ์–ดํ…์…˜ ๊ฐ’(Attention Value)์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐ’์ž…๋‹ˆ๋‹ค. t ๋ฒˆ์งธ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ์–ดํ…์…˜ ๊ฐ’์„ t ์ด๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๊ฐ’์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐœ๋…์ด ๋“ฑ์žฅํ•œ ๋งŒํผ, ์–ดํ…์…˜ ๊ฐ’์ด ํ˜„์žฌ ์‹œ์  t์—์„œ์˜ ์ถœ๋ ฅ ์˜ˆ์ธก์— ๊ตฌ์ฒด์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๋ฐ˜์˜๋˜๋Š”์ง€๋Š” ๋’ค์—์„œ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ๋ฐฐ์šฐ๋Š” ๋ชจ๋“  ๊ณผ์ •์€ t ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ์ •์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์—ฌ์ •์˜ ์ฒซ๊ฑธ์Œ์€ ๋ฐ”๋กœ ์–ดํ…์…˜ ์Šค์ฝ”์–ด(Attention Score)๋ฅผ ๊ตฌํ•˜๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ž€ ํ˜„์žฌ ๋””์ฝ”๋”์˜ ์‹œ์  t์—์„œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด, ์ธ์ฝ”๋”์˜ ๋ชจ๋“  ์€๋‹‰ ์ƒํƒœ ๊ฐ๊ฐ์ด ๋””์ฝ”๋”์˜ ํ˜„์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ t ์™€ ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์Šค์ฝ”์–ด ๊ฐ’์ž…๋‹ˆ๋‹ค. ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์—์„œ๋Š” ์ด ์Šค์ฝ”์–ด ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด t ๋ฅผ ์ „์น˜(transpose) ํ•˜๊ณ  ๊ฐ ์€๋‹‰ ์ƒํƒœ์™€ ๋‚ด์ (dot product)์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ชจ๋“  ์–ดํ…์…˜ ์Šค์ฝ”์–ด ๊ฐ’์€ ์Šค์นผ๋ผ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด t ๊ณผ ์ธ์ฝ”๋”์˜ i ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด์˜ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. c r ( t h) s T i t ์™€ ์ธ์ฝ”๋”์˜ ๋ชจ๋“  ์€๋‹‰ ์ƒํƒœ์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด์˜ ๋ชจ์Œ ๊ฐ’์„ t ๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. t ์˜ ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. t [ t h, . , t h ] 2) ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์–ดํ…์…˜ ๋ถ„ํฌ(Attention Distribution)๋ฅผ ๊ตฌํ•œ๋‹ค. t ์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ, ๋ชจ๋“  ๊ฐ’์„ ํ•ฉํ•˜๋ฉด 1์ด ๋˜๋Š” ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ์–ป์–ด๋ƒ…๋‹ˆ๋‹ค. ์ด๋ฅผ ์–ดํ…์…˜ ๋ถ„ํฌ(Attention Distribution)๋ผ๊ณ  ํ•˜๋ฉฐ, ๊ฐ๊ฐ์˜ ๊ฐ’์€ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜(Attention Weight)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ์–ป์€ ์ถœ๋ ฅ๊ฐ’์ธ I, am, a, student์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ๊ฐ 0.1, 0.4, 0.1, 0.4๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋“ค์˜ ํ•ฉ์€ 1์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๊ฐ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์—์„œ์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์˜ ํฌ๊ธฐ๋ฅผ ์ง์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๋ฅผ ํ†ตํ•ด ์‹œ๊ฐํ™”ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜๊ฐ€ ํด์ˆ˜๋ก ์ง์‚ฌ๊ฐํ˜•์ด ํฝ๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์‹œ์  t์—์„œ์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์˜ ๋ชจ์Œ ๊ฐ’์ธ ์–ดํ…์…˜ ๋ถ„ํฌ๋ฅผ t ์ด๋ผ๊ณ  ํ•  ๋•Œ, t ์„ ์‹์œผ๋กœ ์ •์˜ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. t s f m x ( t ) 3) ๊ฐ ์ธ์ฝ”๋”์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์™€ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ฐ€์ค‘ ํ•ฉํ•˜์—ฌ ์–ดํ…์…˜ ๊ฐ’(Attention Value)์„ ๊ตฌํ•œ๋‹ค. ์ด์ œ ์ง€๊ธˆ๊นŒ์ง€ ์ค€๋น„ํ•ด์˜จ ์ •๋ณด๋“ค์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜์˜ ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜ ๊ฐ’๋“ค์„ ๊ณฑํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ ๋ชจ๋‘ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด ๊ฐ€์ค‘ํ•ฉ(Weighted Sum)์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์–ดํ…์…˜์˜ ์ตœ์ข… ๊ฒฐ๊ณผ. ์ฆ‰, ์–ดํ…์…˜ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์ธ ์–ดํ…์…˜ ๊ฐ’(Attention Value) t ์— ๋Œ€ํ•œ ์‹์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. t โˆ‘ = N i h ์ด๋Ÿฌํ•œ ์–ดํ…์…˜ ๊ฐ’ t ์€ ์ข…์ข… ์ธ์ฝ”๋”์˜ ๋ฌธ๋งฅ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜์—ฌ, ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ(context vector)๋ผ๊ณ ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ seq2seq์—์„œ๋Š” ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๊ฒƒ๊ณผ ๋Œ€์กฐ๋ฉ๋‹ˆ๋‹ค. 4) ์–ดํ…์…˜ ๊ฐ’๊ณผ ๋””์ฝ”๋”์˜ t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.(Concatenate) ์–ดํ…์…˜ ํ•จ์ˆ˜์˜ ์ตœ์ข… ๊ฐ’์ธ ์–ดํ…์…˜ ๊ฐ’ t ์„ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์–ดํ…์…˜ ๊ฐ’์ด ๊ตฌํ•ด์ง€๋ฉด ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ t s ์™€ ๊ฒฐํ•ฉ(concatenate) ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ t ๋ผ๊ณ  ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด t y ์˜ˆ์ธก ์—ฐ์‚ฐ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ธ์ฝ”๋”๋กœ๋ถ€ํ„ฐ ์–ป์€ ์ •๋ณด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ^ ๋ฅผ ์ข€ ๋” ์ž˜ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. 5) ์ถœ๋ ฅ์ธต ์—ฐ์‚ฐ์˜ ์ž…๋ ฅ์ด ๋˜๋Š” ~๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” t ๋ฅผ ๋ฐ”๋กœ ์ถœ๋ ฅ์ธต์œผ๋กœ ๋ณด๋‚ด๊ธฐ ์ „์— ์‹ ๊ฒฝ๋ง ์—ฐ์‚ฐ์„ ํ•œ ๋ฒˆ ๋” ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ ๊ณฑํ•œ ํ›„์— ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋„๋ก ํ•˜์—ฌ ์ถœ๋ ฅ์ธต ์—ฐ์‚ฐ์„ ์œ„ํ•œ ์ƒˆ๋กœ์šด ๋ฒกํ„ฐ์ธ ~๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” seq2seq์—์„œ๋Š” ์ถœ๋ ฅ์ธต์˜ ์ž…๋ ฅ์ด t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ธ t ์˜€๋˜ ๋ฐ˜๋ฉด, ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ๋Š” ์ถœ๋ ฅ์ธต์˜ ์ž…๋ ฅ์ด ~ ๊ฐ€ ๋˜๋Š” ์…ˆ์ž…๋‹ˆ๋‹ค. ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. c ๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ, c ๋Š” ํŽธํ–ฅ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ ํŽธํ–ฅ์€ ์ƒ๋žตํ–ˆ์Šต๋‹ˆ๋‹ค. ~ = tanh ( c [ t s ] b) 6) ~๋ฅผ ์ถœ๋ ฅ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ~๋ฅผ ์ถœ๋ ฅ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ธก ๋ฒกํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ^ = Softmax ( y ~ + y ) 4. ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์–ดํ…์…˜(Attention) ์•ž์„œ seq2seq + ์–ดํ…์…˜(attention) ๋ชจ๋ธ์— ์“ฐ์ผ ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์–ดํ…์…˜ ์ข…๋ฅ˜๊ฐ€ ์žˆ์ง€๋งŒ, ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜๊ณผ ๋‹ค๋ฅธ ์–ดํ…์…˜๋“ค์˜ ์ฐจ์ด๋Š” ์ค‘๊ฐ„ ์ˆ˜์‹์˜ ์ฐจ์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” ์ค‘๊ฐ„ ์ˆ˜์‹์€ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ฐฐ์šด ์–ดํ…์…˜์ด ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์ธ ์ด์œ ๋Š” ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‚ด์ ์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์ œ์‹œ๋˜์–ด ์žˆ์œผ๋ฉฐ, ํ˜„์žฌ ์ œ์‹œ๋œ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฆ„ ์Šค์ฝ”์–ด ํ•จ์ˆ˜ Defined by o s o e ( t h) s T i Luong et al. (2015) c l d o s o e ( t h) s T i Vaswani et al. (2017) e e a s o e ( t h) s T a i // ๋‹จ, a ๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ Luong et al. (2015) o c t c r ( t h) W T t n ( b [ t h ] ) s o e ( t h) W T t n ( b t W h) Bahdanau et al. (2015) o a i n b s ฮฑ = o t a ( a t ) // t ์‚ฐ์ถœ ์‹œ์— t ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•. Luong et al. (2015) ์œ„์—์„œ t ๋Š” Query, i ๋Š” Keys, a W๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„์ด dot์ด๋ผ๊ณ  ๋ถ™์—ฌ์ง„ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๊ฐ€ ์ด๋ฒˆ์— ๋ฐฐ์šด ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์ž…๋‹ˆ๋‹ค. ์ด ์–ดํ…์…˜์€ ์ œ์•ˆํ•œ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ๋ฃจ ์˜น(Luong) ์–ดํ…์…˜์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ œ์•ˆํ•œ ์ด๋“ค์˜ ์ด๋ฆ„์€ ์œ„ ํ…Œ์ด๋ธ”์—์„œ Defined By์— ์ ํ˜€์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. concat์ด๋ผ๋Š” ์ด๋ฆ„์˜ ์–ดํ…์…˜์€ ๋งŒ๋“  ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ๋ฐ”๋‹ค๋‚˜ ์šฐ(Bahdanau) ์–ดํ…์…˜์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋ฉฐ ๋’ค์—์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ seq2seq์—์„œ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œ์ผœ์ฃผ๊ธฐ ์œ„ํ•œ ๊ธฐ๋ฒ•์ธ ์–ดํ…์…˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜์€ ์ฒ˜์Œ์—๋Š” RNN ๊ธฐ๋ฐ˜์˜ seq2seq์˜ ์„ฑ๋Šฅ์„ ๋ณด์ •ํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์†Œ๊ฐœ๋˜์—ˆ์ง€๋งŒ, ํ˜„์žฌ์— ์ด๋ฅด๋Ÿฌ์„œ๋Š” ์–ดํ…์…˜ ์Šค์Šค๋กœ๊ฐ€ ๊ธฐ์กด์˜ seq2seq๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋˜์–ด๊ฐ€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ ์ฑ•ํ„ฐ์ธ ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ์ฑ•ํ„ฐ์—์„œ ๋” ์ž์„ธํžˆ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 15-02 ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜(Bahdanau Attention) ์•ž์„œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ๋ชฉ์ ๊ณผ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ์ผ์ข…์ธ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜(๋ฃจ ์˜น ์–ดํ…์…˜)์˜ ์ „์ฒด์ ์ธ ๊ฐœ์š”๋ฅผ ์‚ดํŽด๋ณด๊ณ , ๋งˆ์ง€๋ง‰์— ํ‘œ๋ฅผ ํ†ตํ•ด ๊ทธ ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์กด์žฌํ•œ๋‹ค๊ณ  ์†Œ๊ฐœํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜๋ณด๋‹ค๋Š” ์กฐ๊ธˆ ๋” ๋ณต์žกํ•˜๊ฒŒ ์„ค๊ณ„๋œ ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 1. ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜ ํ•จ์ˆ˜(Bahdanau Attention Function) ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ํ•จ์ˆ˜ Attention()์œผ๋กœ ์ •์˜ํ•˜์˜€์„ ๋•Œ, ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜ ํ•จ์ˆ˜์˜ ์ž…, ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์˜ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Attention(Q, K, V) = Attention Value t = ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์ˆ˜ํ–‰๋˜๋Š” ๋””์ฝ”๋” ์…€์˜ ํ˜„์žฌ ์‹œ์ ์„ ์˜๋ฏธ. Q = Query : t-1 ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ K = Keys : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค V = Values : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค ์—ฌ๊ธฐ์„œ๋Š” ์–ดํ…์…˜ ํ•จ์ˆ˜์˜ Query๊ฐ€ ๋””์ฝ”๋” ์…€์˜ t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ t-1 ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ž„์„ ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. 2. ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜(Bahdanau Attention) ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜์˜ ์—ฐ์‚ฐ ์ˆœ์„œ๋ฅผ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 1) ์–ดํ…์…˜ ์Šค์ฝ”์–ด(Attention Score)๋ฅผ ๊ตฌํ•œ๋‹ค. ์ธ์ฝ”๋”์˜ ์‹œ์ (time step)์„ ๊ฐ๊ฐ 1, 2, ... N์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ ๊ฐ๊ฐ 1 h, ... N ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๋””์ฝ”๋”์˜ ํ˜„์žฌ ์‹œ์ (time step) t์—์„œ์˜ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ t ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๋˜ํ•œ ์—ฌ๊ธฐ์„œ๋Š” ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›์ด ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์˜ ๊ฒฝ์šฐ์—๋Š” ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ๋™์ผํ•˜๊ฒŒ ์ฐจ์›์ด 4์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋ฃจ ์˜น ์–ดํ…์…˜์—์„œ๋Š” Query๋กœ ๋””์ฝ”๋”์˜ t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ด๋ฒˆ์—๋Š” t-1 ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ t 1 ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜. ์ฆ‰, t 1 ๊ณผ ์ธ์ฝ”๋”์˜ i ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. c r ( t 1 h) W T t n ( b t 1 W h) ๋‹จ, a W, c ๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. t 1 h, 2 h, 4 ์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ณ‘๋ ฌ ์—ฐ์‚ฐ์„ ์œ„ํ•ด 1 h, 3 h๋ฅผ ํ•˜๋‚˜์˜ ํ–‰๋ ฌ๋กœ ๋‘๊ฒ ์Šต๋‹ˆ๋‹ค. ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. c r ( t 1 H ) W T t n ( b t 1 W H ) ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  b t 1 W H ๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋“ค์„ ๋”ํ•œ ํ›„, ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ง„ํ–‰๋œ ์—ฐ์‚ฐ์˜ ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. a h ( b t 1 W H ) ์ด์ œ a ์™€ ๊ณฑํ•˜์—ฌ t 1 h, 2 h, 4 ์˜ ์œ ์‚ฌ๋„๊ฐ€ ๊ธฐ๋ก๋œ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ๋ฒกํ„ฐ t ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. t W T t n ( b t 1 W H ) 2) ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์–ดํ…์…˜ ๋ถ„ํฌ(Attention Distribution)๋ฅผ ๊ตฌํ•œ๋‹ค. t ์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ, ๋ชจ๋“  ๊ฐ’์„ ํ•ฉํ•˜๋ฉด 1์ด ๋˜๋Š” ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ์–ป์–ด๋ƒ…๋‹ˆ๋‹ค. ์ด๋ฅผ ์–ดํ…์…˜ ๋ถ„ํฌ(Attention Distribution)๋ผ๊ณ  ํ•˜๋ฉฐ, ๊ฐ๊ฐ์˜ ๊ฐ’์€ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜(Attention Weight)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3) ๊ฐ ์ธ์ฝ”๋”์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์™€ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ฐ€์ค‘ ํ•ฉํ•˜์—ฌ ์–ดํ…์…˜ ๊ฐ’(Attention Value)์„ ๊ตฌํ•œ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ค€๋น„ํ•ด์˜จ ์ •๋ณด๋“ค์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜์˜ ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜ ๊ฐ’๋“ค์„ ๊ณฑํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ ๋ชจ๋‘ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด ๊ฐ€์ค‘ํ•ฉ(Weighted Sum)์„ ํ•œ๋‹ค๊ณ  ๋งํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ฒกํ„ฐ๋Š” ์ธ์ฝ”๋”์˜ ๋ฌธ๋งฅ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜์—ฌ, ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ(context vector)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 4) ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋กœ๋ถ€ํ„ฐ t ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ LSTM์ด t ๋ฅผ ๊ตฌํ•  ๋•Œ๋ฅผ ์•„๋ž˜ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. ๊ธฐ์กด์˜ LSTM์€ ์ด์ „ ์‹œ์ ์˜ ์…€๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์€๋‹‰ ์ƒํƒœ t 1 ์™€ ํ˜„์žฌ ์‹œ์ ์˜ ์ž…๋ ฅ t ๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ์‚ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ LSTM์€ seq2seq์˜ ๋””์ฝ”๋”์ด๋ฉฐ ํ˜„์žฌ ์‹œ์ ์˜ ์ž…๋ ฅ t ๋Š” ์ž„๋ฒ ๋”ฉ๋œ ๋‹จ์–ด ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ๋Š” ์–ด๋–จ๊นŒ์š”? ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์™€ ํ˜„์žฌ ์‹œ์ ์˜ ์ž…๋ ฅ์ธ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์—ฐ๊ฒฐ(concatenate) ํ•˜๊ณ , ํ˜„์žฌ ์‹œ์ ์˜ ์ƒˆ๋กœ์šด ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ „ ์‹œ์ ์˜ ์…€๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์€๋‹‰ ์ƒํƒœ t 1 ์™€ ํ˜„์žฌ ์‹œ์ ์˜ ์ƒˆ๋กœ์šด ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ t ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ LSTM์ด ์ž„๋ฒ ๋”ฉ๋œ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์—์„œ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์™€ ์ž„๋ฒ ๋”ฉ๋œ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋‹ฌ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. ์ดํ›„์—๋Š” ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. t ๋Š” ์ถœ๋ ฅ์ธต์œผ๋กœ ์ „๋‹ฌ๋˜์–ด ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก๊ฐ’์„ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 15-03 ์–‘๋ฐฉํ–ฅ LSTM๊ณผ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜(BiLSTM with Attention mechanism) ๋‹จ๋ฑกํ•ญ LSTM์œผ๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋•Œ๋กœ๋Š” ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๊ฐ•๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์ถ”๊ฐ€์ ์œผ๋กœ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ LSTM๊ณผ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ IMDB ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. 1. IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ from tensorflow.keras.datasets import imdb from tensorflow.keras.utils import to_categorical from tensorflow.keras.preprocessing.sequence import pad_sequences IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋Š” ์•ž์„œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃฌ ๋ฐ” ์žˆ์œผ๋ฏ€๋กœ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ƒ์„ธ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. ์ตœ๋Œ€ ๋‹จ์–ด ๊ฐœ์ˆ˜๋ฅผ 10,000์œผ๋กœ ์ œํ•œํ•˜๊ณ  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ต๋‹ˆ๋‹ค. vocab_size = 10000 (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words = vocab_size) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ์ด์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ”์ด ๊ฐ๊ฐ X_train, y_train์— ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์™€ ์ด์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ”์ด ๊ฐ๊ฐ X_test, y_test์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋Š” ์ด๋ฏธ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋œ ์ƒํƒœ๋ฏ€๋กœ ๋‚จ์€ ์ „์ฒ˜๋ฆฌ๋Š” ํŒจ๋”ฉ๋ฟ์ž…๋‹ˆ๋‹ค. ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด์™€ ํ‰๊ท  ๊ธธ์ด๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : {}'.format(max(len(l) for l in X_train))) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : {}'.format(sum(map(len, X_train))/len(X_train))) ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 2494 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 238.71364 ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 2,494์ด๋ฉฐ ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด๋Š” ์•ฝ 238๋กœ ํ™•์ธ๋ฉ๋‹ˆ๋‹ค. ํ‰๊ท  ๊ธธ์ด๋ณด๋‹ค๋Š” ์กฐ๊ธˆ ํฌ๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ํŒจ๋”ฉ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. max_len = 500 X_train = pad_sequences(X_train, maxlen=max_len) X_test = pad_sequences(X_test, maxlen=max_len) ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ์™€ ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ์˜ ๊ธธ์ด๊ฐ€ ๋‘˜ ๋‹ค 500์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 2. ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜(Bahdanau Attention) ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  ์–ดํ…์…˜์€ ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜(Bahdanau attention)์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์•ž์„œ ๋ฐฐ์šด ๊ฐ€์žฅ ์‰ฌ์šด ์–ดํ…์…˜์ด์—ˆ๋˜ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜๊ณผ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜์˜ ์ •์˜๋ฅผ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋ž€ ์ฃผ์–ด์ง„ query์™€ ๋ชจ๋“  key์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌ๋„๋ฅผ ์ธก์ •ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์—์„œ๋Š” query์™€ key์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‚ด์ (dot product)์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. c r ( u r , k y ) q e y k y ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜์€ ์•„๋ž˜์™€ ๊ฐ™์€ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. c r ( u r , k y ) V t n ( 1 e + 2 u r) ์ด ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์—์„œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? RNN์˜ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋Š” ์˜ˆ์ธก์„ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด RNN์˜ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋Š” ๋ช‡ ๊ฐ€์ง€ ์œ ์šฉํ•œ ์ •๋ณด๋“ค์„ ์†์‹คํ•œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ RNN์ด time step์„ ์ง€๋‚˜๋ฉฐ ์†์‹คํ–ˆ๋˜ ์ •๋ณด๋“ค์„ ๋‹ค์‹œ ์ฐธ๊ณ ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์‹œ ๋งํ•ด RNN์˜ ๋ชจ๋“  ์€๋‹‰ ์ƒํƒœ๋“ค์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ฐธ๊ณ ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์œ„ํ•ด์„œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. import tensorflow as tf class BahdanauAttention(tf.keras.Model): def __init__(self, units): super(BahdanauAttention, self).__init__() self.W1 = Dense(units) self.W2 = Dense(units) self.V = Dense(1) def call(self, values, query): # ๋‹จ, key์™€ value๋Š” ๊ฐ™์Œ # query shape == (batch_size, hidden size) # hidden_with_time_axis shape == (batch_size, 1, hidden size) # score ๊ณ„์‚ฐ์„ ์œ„ํ•ด ๋’ค์—์„œ ํ•  ๋ง์…ˆ์„ ์œ„ํ•ด์„œ ์ฐจ์›์„ ๋ณ€๊ฒฝํ•ด์ค๋‹ˆ๋‹ค. hidden_with_time_axis = tf.expand_dims(query, 1) # score shape == (batch_size, max_length, 1) # we get 1 at the last axis because we are applying score to self.V # the shape of the tensor before applying self.V is (batch_size, max_length, units) score = self.V(tf.nn.tanh( self.W1(values) + self.W2(hidden_with_time_axis))) # attention_weights shape == (batch_size, max_length, 1) attention_weights = tf.nn.softmax(score, axis=1) # context_vector shape after sum == (batch_size, hidden_size) context_vector = attention_weights * values context_vector = tf.reduce_sum(context_vector, axis=1) return context_vector, attention_weights 3. ์–‘๋ฐฉํ–ฅ LSTM + ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜(BiLSTM with Attention Mechanism) from tensorflow.keras.layers import Dense, Embedding, Bidirectional, LSTM, Concatenate, Dropout from tensorflow.keras import Input, Model from tensorflow.keras import optimizers import os ์ด์ œ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ผ€๋ผ์Šค์˜ ํ•จ์ˆ˜ํ˜• API๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ž…๋ ฅ์ธต๊ณผ ์ž„๋ฒ ๋”ฉ์ธต์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. sequence_input = Input(shape=(max_len,), dtype='int32') embedded_sequences = Embedding(vocab_size, 128, input_length=max_len, mask_zero = True)(sequence_input) 10,000๊ฐœ์˜ ๋‹จ์–ด๋“ค์„ 128์ฐจ์›์˜ ๋ฒกํ„ฐ๋กœ ์ž„๋ฒ ๋”ฉํ•˜๋„๋ก ์„ค๊ณ„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ์–‘๋ฐฉํ–ฅ LSTM์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์—ฌ๊ธฐ์„œ๋Š” ์–‘๋ฐฉํ–ฅ LSTM์„ ๋‘ ์ธต์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„ , ์ฒซ ๋ฒˆ์งธ ์ธต์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธต์„ ์œ„์— ์Œ“์„ ์˜ˆ์ •์ด๋ฏ€๋กœ return_sequences๋ฅผ True๋กœ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. lstm = Bidirectional(LSTM(64, dropout=0.5, return_sequences = True))(embedded_sequences) ๋‘ ๋ฒˆ์งธ ์ธต์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ์ƒํƒœ๋ฅผ ๋ฆฌํ„ด ๋ฐ›์•„์•ผ ํ•˜๋ฏ€๋กœ return_state๋ฅผ True๋กœ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. lstm, forward_h, forward_c, backward_h, backward_c = Bidirectional \ (LSTM(64, dropout=0.5, return_sequences=True, return_state=True))(lstm) ๊ฐ ์ƒํƒœ์˜ ํฌ๊ธฐ(shape)๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(lstm.shape, forward_h.shape, forward_c.shape, backward_h.shape, backward_c.shape) (None, 500, 128) (None, 64) (None, 64) (None, 64) (None, 64) ์ˆœ๋ฐฉํ–ฅ LSTM์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€์ƒํƒœ๋ฅผ forward_h, forward_c์— ์ €์žฅํ•˜๊ณ , ์—ญ๋ฐฉํ–ฅ LSTM์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ backward_h, backward_c์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์€๋‹‰ ์ƒํƒœ๋‚˜ ์…€ ์ƒํƒœ์˜ ๊ฒฝ์šฐ์—๋Š” 128์ฐจ์›์„ ๊ฐ€์ง€๋Š”๋ฐ, lstm์˜ ๊ฒฝ์šฐ์—๋Š” (500 ร— 128)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. foward ๋ฐฉํ–ฅ๊ณผ backward ๋ฐฉํ–ฅ์ด ์—ฐ๊ฒฐ๋œ hidden state ๋ฒกํ„ฐ๊ฐ€ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์กด์žฌํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์ˆœ๋ฐฉํ–ฅ LSTM๊ณผ ์—ญ๋ฐฉํ–ฅ LSTM ๊ฐ๊ฐ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ, ์–‘๋ฐฉํ–ฅ LSTM์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‘ ๋ฐฉํ–ฅ์˜ LSTM์˜ ์ƒํƒœ๋“ค์„ ์—ฐ๊ฒฐ(concatenate) ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. state_h = Concatenate()([forward_h, backward_h]) # ์€๋‹‰ ์ƒํƒœ state_c = Concatenate()([forward_c, backward_c]) # ์…€ ์ƒํƒœ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ๋Š” ์€๋‹‰ ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ž…๋ ฅ์œผ๋กœ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ(context vector)๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. attention = BahdanauAttention(64) # ๊ฐ€์ค‘์น˜ ํฌ๊ธฐ ์ •์˜ context_vector, attention_weights = attention(lstm, state_h) ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๋ฐ€์ง‘์ธต(dense layer)์— ํ†ต๊ณผ์‹œํ‚ค๊ณ , ์ด์ง„ ๋ถ„๋ฅ˜์ด๋ฏ€๋กœ ์ตœ์ข… ์ถœ๋ ฅ์ธต์— 1๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ๋ฐฐ์น˜ํ•˜๊ณ , ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. dense1 = Dense(20, activation="relu")(context_vector) dropout = Dropout(0.5)(dense1) output = Dense(1, activation="sigmoid")(dropout) model = Model(inputs=sequence_input, outputs=output) ์˜ตํ‹ฐ๋งˆ์ด์ €๋กœ ์•„๋‹ด ์˜ตํ‹ฐ๋งˆ์ด์ € ์‚ฌ์šฉํ•˜๊ณ , ๋ชจ๋ธ์„ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค. model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์†์‹ค ํ•จ์ˆ˜๋กœ binary_crossentropy๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. history = model.fit(X_train, y_train, epochs = 3, batch_size = 256, validation_data=(X_test, y_test), verbose=1) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Train on 25000 samples, validate on 25000 samples Epoch 1/3 25000/25000 [==============================] - 566s 23ms/sample - loss: 0.4941 - accuracy: 0.7570 - val_loss: 0.3110 - val_accuracy: 0.8721 Epoch 2/3 25000/25000 [==============================] - 541s 22ms/sample - loss: 0.2530 - accuracy: 0.9074 - val_loss: 0.2852 - val_accuracy: 0.8835 Epoch 3/3 25000/25000 [==============================] - 543s 22ms/sample - loss: 0.1901 - accuracy: 0.9352 - val_loss: 0.3375 - val_accuracy: 0.8793 print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (model.evaluate(X_test, y_test)[1])) 25000/25000 [============================] - 183s 7ms/sample - loss: 0.1901 - acc: 0.8793 ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.8793 87.93%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. 16. ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ์ด๋ฒˆ ํŠธ๋žœ์Šคํฌ๋จธ ์ฑ•ํ„ฐ์—์„œ๋Š” seq2seq์˜ ๋‹จ์ ์„ ๊ฐœ์„ ํ•˜๋ฉด์„œ๋„ ์—ฌ์ „ํžˆ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๊ตฌ์กฐ๋ฅผ ์œ ์ง€ํ•˜๊ณ  ์žˆ๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์„ ํ†ตํ•ด ์ฑ—๋ด‡์„ ๊ตฌํ˜„ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 16-01 ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ์ด๋ฒˆ ์ฑ•ํ„ฐ๋Š” ์•ž์„œ ์„ค๋ช…ํ•œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ฑ•ํ„ฐ์— ๋Œ€ํ•œ ์‚ฌ์ „ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ(Transformer)๋Š” 2017๋…„ ๊ตฌ๊ธ€์ด ๋ฐœํ‘œํ•œ ๋…ผ๋ฌธ์ธ "Attention is all you need"์—์„œ ๋‚˜์˜จ ๋ชจ๋ธ๋กœ ๊ธฐ์กด์˜ seq2seq์˜ ๊ตฌ์กฐ์ธ ์ธ์ฝ”๋”-๋””์ฝ”๋”๋ฅผ ๋”ฐ๋ฅด๋ฉด์„œ๋„, ๋…ผ๋ฌธ์˜ ์ด๋ฆ„์ฒ˜๋Ÿผ ์–ดํ…์…˜(Attention) ๋งŒ์œผ๋กœ ๊ตฌํ˜„ํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ RNN์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์ธ์ฝ”๋”-๋””์ฝ”๋” ๊ตฌ์กฐ๋ฅผ ์„ค๊ณ„ํ•˜์˜€์Œ์—๋„ ๋ฒˆ์—ญ ์„ฑ๋Šฅ์—์„œ๋„ RNN๋ณด๋‹ค ์šฐ์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹น์žฅ ๊ตฌํ˜„์— ๊ด€์‹ฌ์ด ์—†๋‹ค๋ฉด ์ฝ”๋“œ ๋ถ€๋ถ„๋งŒ ์Šคํ‚ต ํ•ด์„œ ์ด๋ก ๋งŒ ์ฝ์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ ์ „์ฒด ์ฝ”๋“œ๋Š” ์•„๋ž˜์˜ ๋งํฌ์— ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ๊นƒํ—ˆ๋ธŒ ๋งํฌ : https://github.com/ukairia777/tensorflow-transformer import numpy as np import matplotlib.pyplot as plt import tensorflow as tf 1. ๊ธฐ์กด์˜ seq2seq ๋ชจ๋ธ์˜ ํ•œ๊ณ„ ํŠธ๋žœ์Šคํฌ๋จธ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šฐ๊ธฐ ์ „์— ๊ธฐ์กด์˜ seq2seq๋ฅผ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. ๊ธฐ์กด์˜ seq2seq ๋ชจ๋ธ์€ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ธ์ฝ”๋”๋Š” ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ ํ‘œํ˜„์œผ๋กœ ์••์ถ•ํ•˜๊ณ , ๋””์ฝ”๋”๋Š” ์ด ๋ฒกํ„ฐ ํ‘œํ˜„์„ ํ†ตํ•ด์„œ ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ƒˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ๊ตฌ์กฐ๋Š” ์ธ์ฝ”๋”๊ฐ€ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ์••์ถ•ํ•˜๋Š” ๊ณผ์ •์—์„œ ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ์ •๋ณด๊ฐ€ ์ผ๋ถ€ ์†์‹ค๋œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ๊ณ , ์ด๋ฅผ ๋ณด์ •ํ•˜๊ธฐ ์œ„ํ•ด ์–ดํ…์…˜์ด ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์–ดํ…์…˜์„ RNN์˜ ๋ณด์ •์„ ์œ„ํ•œ ์šฉ๋„๋กœ์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์–ดํ…์…˜๋งŒ์œผ๋กœ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? 2. ํŠธ๋žœ์Šคํฌ๋จธ(Transformer)์˜ ์ฃผ์š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์‹œ์ž‘์— ์•ž์„œ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์˜๋ฏธ์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ์„ค๋ช…ํ•˜๊ธฐ๋กœ ํ•˜๊ณ , ์—ฌ๊ธฐ์„œ๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์—๋Š” ์ด๋Ÿฌํ•œ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š” ์ •๋„๋กœ๋งŒ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์ •์˜ํ•˜๋Š” ์ˆ˜์น˜๋Š” ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ์—์„œ ์‚ฌ์šฉํ•œ ์ˆ˜์น˜๋กœ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ชจ๋ธ ์„ค๊ณ„ ์‹œ ์ž„์˜๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’๋“ค์ž…๋‹ˆ๋‹ค. m d l 512 ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์—์„œ์˜ ์ •ํ•ด์ง„ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์› ๋˜ํ•œ m d l ์ด๋ฉฐ, ๊ฐ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๊ฐ€ ๋‹ค์Œ ์ธต์˜ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋กœ ๊ฐ’์„ ๋ณด๋‚ผ ๋•Œ์—๋„ ์ด ์ฐจ์›์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” 512์ž…๋‹ˆ๋‹ค. num_layers 6 ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ ํ•˜๋‚˜์˜ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋ฅผ ์ธต์œผ๋กœ ์ƒ๊ฐํ•˜์˜€์„ ๋•Œ, ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์—์„œ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๊ฐ€ ์ด ๋ช‡ ์ธต์œผ๋กœ ๊ตฌ์„ฑ๋˜์—ˆ๋Š”์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋ฅผ ๊ฐ๊ฐ ์ด 6๊ฐœ ์Œ“์•˜์Šต๋‹ˆ๋‹ค. num_heads 8 ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋Š” ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ, ํ•œ ๋ฒˆ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ๋ถ„ํ• ํ•ด์„œ ๋ณ‘๋ ฌ๋กœ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฒฐ๊ด๊ฐ’์„ ๋‹ค์‹œ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๋ฐฉ์‹์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ด ๋ณ‘๋ ฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. f = 2048 ํŠธ๋žœ์Šคํฌ๋จธ ๋‚ด๋ถ€์—๋Š” ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์ด ์กด์žฌํ•˜๋ฉฐ ํ•ด๋‹น ์‹ ๊ฒฝ๋ง์˜ ์€๋‹‰์ธต์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์˜ ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต์˜ ํฌ๊ธฐ๋Š” m d l ์ž…๋‹ˆ๋‹ค. 3. ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ํŠธ๋žœ์Šคํฌ๋จธ๋Š” RNN์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์ง€๋งŒ ๊ธฐ์กด์˜ seq2seq์ฒ˜๋Ÿผ ์ธ์ฝ”๋”์—์„œ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ๋””์ฝ”๋”์—์„œ ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ธ์ฝ”๋”-๋””์ฝ”๋” ๊ตฌ์กฐ๋ฅผ ์œ ์ง€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „ seq2seq ๊ตฌ์กฐ์—์„œ๋Š” ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์—์„œ ๊ฐ๊ฐ ํ•˜๋‚˜์˜ RNN์ด t ๊ฐœ์˜ ์‹œ์ (time step)์„ ๊ฐ€์ง€๋Š” ๊ตฌ์กฐ์˜€๋‹ค๋ฉด ์ด๋ฒˆ์—๋Š” ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋ผ๋Š” ๋‹จ์œ„๊ฐ€ N ๊ฐœ๋กœ ๊ตฌ์„ฑ๋˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ์—์„œ๋Š” ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ๊ฐ 6๊ฐœ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๊ฐ€ 6๊ฐœ์”ฉ ์กด์žฌํ•˜๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๊ฐ€ ๊ฐ๊ฐ ์—ฌ๋Ÿฌ ๊ฐœ ์Œ“์—ฌ์žˆ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์•ŒํŒŒ๋ฒณ s๋ฅผ ๋’ค์— ๋ถ™์—ฌ encoders, decoders๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ธ์ฝ”๋”๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ๋””์ฝ”๋”๊ฐ€ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ๊ตฌ์กฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” ๋งˆ์น˜ ๊ธฐ์กด์˜ seq2seq ๊ตฌ์กฐ์ฒ˜๋Ÿผ ์‹œ์ž‘ ์‹ฌ๋ฒŒ <sos>๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์ข…๋ฃŒ ์‹ฌ๋ฒŒ <eos>๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” RNN์€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์ง€๋งŒ ์—ฌ์ „ํžˆ ์ธ์ฝ”๋”-๋””์ฝ”๋”์˜ ๊ตฌ์กฐ๋Š” ์œ ์ง€๋˜๊ณ  ์žˆ์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋ฅผ ์กฐ๊ธˆ์”ฉ ํ™•๋Œ€ํ•ด๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ๊ตฌ์กฐ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์ „์— ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋Š” ๋‹จ์ˆœํžˆ ๊ฐ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ์ž…๋ ฅ๋ฐ›๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์—์„œ ์กฐ์ •๋œ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๋Š”๋ฐ ์ด์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์ž…๋ ฅ ๋ถ€๋ถ„์„ ํ™•๋Œ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4. ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ(Positional Encoding) ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋‚ด๋ถ€๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์ „ ์šฐ์„  ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. RNN์ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์œ ์šฉํ–ˆ๋˜ ์ด์œ ๋Š” ๋‹จ์–ด์˜ ์œ„์น˜์— ๋”ฐ๋ผ ๋‹จ์–ด๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” RNN์˜ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ๊ฐ ๋‹จ์–ด์˜ ์œ„์น˜ ์ •๋ณด(position information)๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์— ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ๋‹จ์–ด ์ž…๋ ฅ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐ›๋Š” ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ฏ€๋กœ ๋‹จ์–ด์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์•Œ๋ ค์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ๋‹จ์–ด์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์— ์œ„์น˜ ์ •๋ณด๋“ค์„ ๋”ํ•˜์—ฌ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด๋ฅผ ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ(positional encoding)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์ด ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ธฐ ์ „์— ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ์˜ ๊ฐ’์ด ๋”ํ•ด์ง€๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ธฐ ์ „ ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ ๊ฐ’์ด ๋”ํ•ด์ง€๋Š” ๊ณผ์ •์„ ์‹œ๊ฐํ™”ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ ๊ฐ’๋“ค์€ ์–ด๋–ค ๊ฐ’์ด๊ธฐ์— ์œ„์น˜ ์ •๋ณด๋ฅผ ๋ฐ˜์˜ํ•ด ์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ผ๊นŒ์š”? ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ์œ„์น˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ ๊ฐ’์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜์˜ ๋‘ ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. E ( o , 2 ) s n ( o / 10000 i d o e) E ( o , 2 + ) c s ( o / 10000 i d o e) ์‚ฌ์ธ ํ•จ์ˆ˜์™€ ์ฝ”์‚ฌ์ธ ํ•จ์ˆ˜์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ๊ธฐํ•ด ๋ณด๋ฉด ์š”๋™์น˜๋Š” ๊ฐ’์˜ ํ˜•ํƒœ๋ฅผ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ์‚ฌ์ธ ํ•จ์ˆ˜์™€ ์ฝ”์‚ฌ์ธ ํ•จ์ˆ˜์˜ ๊ฐ’์„ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์— ๋”ํ•ด์ฃผ๋ฏ€๋กœ์„œ ๋‹จ์–ด์˜ ์ˆœ์„œ ์ •๋ณด๋ฅผ ๋”ํ•˜์—ฌ ์ค๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ๋‘ ํ•จ์ˆ˜์—๋Š” o, , m d l ๋“ฑ์˜ ์ƒ์†Œํ•œ ๋ณ€์ˆ˜๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ํ•จ์ˆ˜๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์—์„œ ๋ณธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์™€ ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ์˜ ๋ง์…ˆ์€ ์‚ฌ์‹ค ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ๋ชจ์—ฌ ๋งŒ๋“ค์–ด์ง„ ๋ฌธ์žฅ ํ–‰๋ ฌ๊ณผ ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ ํ–‰๋ ฌ์˜ ๋ง์…ˆ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง„๋‹ค๋Š” ์ ์„ ์ดํ•ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. o๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์—์„œ์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ,๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๋‚ด์˜ ์ฐจ์›์˜ ์ธ๋ฑ์Šค๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์‹์— ๋”ฐ๋ฅด๋ฉด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๋‚ด์˜ ๊ฐ ์ฐจ์›์˜ ์ธ๋ฑ์Šค๊ฐ€ ์ง์ˆ˜์ธ ๊ฒฝ์šฐ์—๋Š” ์‚ฌ์ธ ํ•จ์ˆ˜์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ณ  ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ์—๋Š” ์ฝ”์‚ฌ์ธ ํ•จ์ˆ˜์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ˆ˜์‹์—์„œ ( o , 2 ) ์ผ ๋•Œ๋Š” ์‚ฌ์ธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ( o , 2 + ) ์ผ ๋•Œ๋Š” ์ฝ”์‚ฌ์ธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Œ์„ ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. ๋˜ํ•œ ์œ„์˜ ์‹์—์„œ m d l ์€ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋ชจ๋“  ์ธต์˜ ์ถœ๋ ฅ ์ฐจ์›์„ ์˜๋ฏธํ•˜๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ๋ณด๊ฒŒ ๋  ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๊ฐ์ข… ๊ตฌ์กฐ์—์„œ m d l ์˜ ๊ฐ’์ด ๊ณ„์†ํ•ด์„œ ๋“ฑ์žฅํ•˜๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๋˜ํ•œ m d l ์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋Š”๋ฐ ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ๋งˆ์น˜ 4๋กœ ํ‘œํ˜„๋˜์—ˆ์ง€๋งŒ ์‹ค์ œ ๋…ผ๋ฌธ์—์„œ๋Š” 512์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์ˆœ์„œ ์ •๋ณด๊ฐ€ ๋ณด์กด๋˜๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์— ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ์˜ ๊ฐ’์„ ๋”ํ•˜๋ฉด ๊ฐ™์€ ๋‹จ์–ด๋ผ๊ณ  ํ•˜๋”๋ผ๋„ ๋ฌธ์žฅ ๋‚ด์˜ ์œ„์น˜์— ๋”ฐ๋ผ์„œ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด๊ฐ€๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ๊ฐ’์ด ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ž…๋ ฅ์€ ์ˆœ์„œ ์ •๋ณด๊ฐ€ ๊ณ ๋ ค๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. class PositionalEncoding(tf.keras.layers.Layer): def __init__(self, position, d_model): super(PositionalEncoding, self).__init__() self.pos_encoding = self.positional_encoding(position, d_model) def get_angles(self, position, i, d_model): angles = 1 / tf.pow(10000, (2 * (i // 2)) / tf.cast(d_model, tf.float32)) return position * angles def positional_encoding(self, position, d_model): angle_rads = self.get_angles( position=tf.range(position, dtype=tf.float32)[:, tf.newaxis], i=tf.range(d_model, dtype=tf.float32)[tf.newaxis, :], d_model=d_model) # ๋ฐฐ์—ด์˜ ์ง์ˆ˜ ์ธ๋ฑ์Šค(2i)์—๋Š” ์‚ฌ์ธ ํ•จ์ˆ˜ ์ ์šฉ sines = tf.math.sin(angle_rads[:, 0::2]) # ๋ฐฐ์—ด์˜ ํ™€์ˆ˜ ์ธ๋ฑ์Šค(2i+1)์—๋Š” ์ฝ”์‚ฌ์ธ ํ•จ์ˆ˜ ์ ์šฉ cosines = tf.math.cos(angle_rads[:, 1::2]) angle_rads = np.zeros(angle_rads.shape) angle_rads[:, 0::2] = sines angle_rads[:, 1::2] = cosines pos_encoding = tf.constant(angle_rads) pos_encoding = pos_encoding[tf.newaxis, ...] print(pos_encoding.shape) return tf.cast(pos_encoding, tf.float32) def call(self, inputs): return inputs + self.pos_encoding[:, :tf.shape(inputs)[1], :] 50 ร— 128์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ ํ–‰๋ ฌ์„ ์‹œ๊ฐํ™”ํ•˜์—ฌ ์–ด๋–ค ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. ์ด๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๋‹จ์–ด๊ฐ€ 50๊ฐœ์ด๋ฉด์„œ, ๊ฐ ๋‹จ์–ด๊ฐ€ 128์ฐจ์›์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์งˆ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. # ๋ฌธ์žฅ์˜ ๊ธธ์ด 50, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์› 128 sample_pos_encoding = PositionalEncoding(50, 128) plt.pcolormesh(sample_pos_encoding.pos_encoding.numpy()[0], cmap='RdBu') plt.xlabel('Depth') plt.xlim((0, 128)) plt.ylabel('Position') plt.colorbar() plt.show() (1, 50, 128) 5. ์–ดํ…์…˜(Attention) ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์„ธ ๊ฐ€์ง€์˜ ์–ดํ…์…˜์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. ์ง€๊ธˆ์€ ํฐ ๊ทธ๋ฆผ์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์—๋งŒ ์ง‘์ค‘ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๊ทธ๋ฆผ์ธ ์…€ํ”„ ์–ดํ…์…˜์€ ์ธ์ฝ”๋”์—์„œ ์ด๋ฃจ์–ด์ง€์ง€๋งŒ, ๋‘ ๋ฒˆ์งธ ๊ทธ๋ฆผ์ธ ์…€ํ”„ ์–ดํ…์…˜๊ณผ ์„ธ ๋ฒˆ์งธ ๊ทธ๋ฆผ์ธ ์ธ์ฝ”๋”-๋””์ฝ”๋” ์–ดํ…์…˜์€ ๋””์ฝ”๋”์—์„œ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์…€ํ”„ ์–ดํ…์…˜์€ ๋ณธ์งˆ์ ์œผ๋กœ Query, Key, Value๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ์„ธ ๋ฒˆ์งธ ๊ทธ๋ฆผ ์ธ์ฝ”๋”-๋””์ฝ”๋” ์–ดํ…์…˜์—์„œ๋Š” Query๊ฐ€ ๋””์ฝ”๋”์˜ ๋ฒกํ„ฐ์ธ ๋ฐ˜๋ฉด์— Key์™€ Value๊ฐ€ ์ธ์ฝ”๋”์˜ ๋ฒกํ„ฐ์ด๋ฏ€๋กœ ์…€ํ”„ ์–ดํ…์…˜์ด๋ผ๊ณ  ๋ถ€๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์—ฌ๊ธฐ์„œ Query, Key ๋“ฑ์ด ๊ฐ™๋‹ค๋Š” ๊ฒƒ์€ ๋ฒกํ„ฐ์˜ ๊ฐ’์ด ๊ฐ™๋‹ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฒกํ„ฐ์˜ ์ถœ์ฒ˜๊ฐ€ ๊ฐ™๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์˜ ์…€ํ”„ ์–ดํ…์…˜ : Query = Key = Value ๋””์ฝ”๋”์˜ ๋งˆ์Šคํฌ ๋“œ ์…€ํ”„ ์–ดํ…์…˜ : Query = Key = Value ๋””์ฝ”๋”์˜ ์ธ์ฝ”๋”-๋””์ฝ”๋” ์–ดํ…์…˜ : Query : ๋””์ฝ”๋” ๋ฒกํ„ฐ / Key = Value : ์ธ์ฝ”๋” ๋ฒกํ„ฐ ์œ„ ๊ทธ๋ฆผ์€ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์•„ํ‚คํ…์ฒ˜์—์„œ ์„ธ ๊ฐ€์ง€ ์–ดํ…์…˜์ด ๊ฐ๊ฐ ์–ด๋””์—์„œ ์ด๋ฃจ์–ด์ง€๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์„ธ ๊ฐœ์˜ ์–ดํ…์…˜์— ์ถ”๊ฐ€์ ์œผ๋กœ '๋ฉ€ํ‹ฐ ํ—ค๋“œ'๋ผ๋Š” ์ด๋ฆ„์ด ๋ถ™์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ์„ค๋ช…ํ•˜๊ฒ ์ง€๋งŒ ํŠธ๋žœ์Šคํฌ๋จธ๊ฐ€ ์–ดํ…์…˜์„ ๋ณ‘๋ ฌ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 6. ์ธ์ฝ”๋”(Encoder) ์ธ์ฝ”๋”์˜ ๊ตฌ์กฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ num_layers ๊ฐœ์ˆ˜์˜ ์ธ์ฝ”๋” ์ธต์„ ์Œ“์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” ์ด 6๊ฐœ์˜ ์ธ์ฝ”๋” ์ธต์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”๋ฅผ ํ•˜๋‚˜์˜ ์ธต์ด๋ผ๋Š” ๊ฐœ๋…์œผ๋กœ ์ƒ๊ฐํ•œ๋‹ค๋ฉด, ํ•˜๋‚˜์˜ ์ธ์ฝ”๋” ์ธต์€ ํฌ๊ฒŒ ์ด 2๊ฐœ์˜ ์„œ๋ธŒ์ธต(sublayer)์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง‘๋‹ˆ๋‹ค. ์…€ํ”„ ์–ดํ…์…˜๊ณผ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์…€ํ”„ ์–ดํ…์…˜๊ณผ ํฌ์ง€์…˜ ์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์ด๋ผ๊ณ  ์ ํ˜€์žˆ์ง€๋งŒ, ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์…€ํ”„ ์–ดํ…์…˜์€ ์…€ํ”„ ์–ดํ…์…˜์„ ๋ณ‘๋ ฌ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€๋‹ค๋Š” ์˜๋ฏธ๊ณ , ํฌ์ง€์…˜ ์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์€ ์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” ์ผ๋ฐ˜์ ์ธ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ์šฐ์„  ์…€ํ”„ ์–ดํ…์…˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค. 7. ์ธ์ฝ”๋”์˜ ์…€ํ”„ ์–ดํ…์…˜ ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋Š” ์…€ํ”„ ์–ดํ…์…˜์ด๋ผ๋Š” ์–ดํ…์…˜ ๊ธฐ๋ฒ•์ด ๋“ฑ์žฅํ•˜๋Š”๋ฐ ์•ž์„œ ๋ฐฐ์› ๋˜ ์–ดํ…์…˜ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ๋ณต์Šตํ•˜๊ณ , ์…€ํ”„ ์–ดํ…์…˜์ด ์•ž์„œ ๋ฐฐ์› ๋˜ ์–ดํ…์…˜๊ณผ ๋ฌด์—‡์ด ๋‹ค๋ฅธ์ง€ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์…€ํ”„ ์–ดํ…์…˜์˜ ์˜๋ฏธ์™€ ์ด์  ์–ดํ…์…˜ ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ '์ฟผ๋ฆฌ(Query)'์— ๋Œ€ํ•ด์„œ ๋ชจ๋“  'ํ‚ค(Key)'์™€์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ตฌํ•ด๋‚ธ ์ด ์œ ์‚ฌ๋„๋ฅผ ๊ฐ€์ค‘์น˜๋กœ ํ•˜์—ฌ ํ‚ค์™€ ๋งคํ•‘๋˜์–ด์žˆ๋Š” ๊ฐ๊ฐ์˜ '๊ฐ’(Value)'์— ๋ฐ˜์˜ํ•ด ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ ์‚ฌ๋„๊ฐ€ ๋ฐ˜์˜๋œ '๊ฐ’(Value)'์„ ๋ชจ๋‘ ๊ฐ€์ค‘ ํ•ฉํ•˜์—ฌ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€๋Š” ์•ž์„œ ๋ฐฐ์šด ์–ดํ…์…˜์˜ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์–ดํ…์…˜ ์ค‘์—์„œ๋Š” ์…€ํ”„ ์–ดํ…์…˜(self-attention)์ด๋ผ๋Š” ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜์„ ์ž๊ธฐ ์ž์‹ ์—๊ฒŒ ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด seq2seq์—์„œ ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์˜ Q, K, V์˜ ์ •์˜๋ฅผ ๋‹ค์‹œ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. Q = Query : t ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ K = Keys : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค V = Values : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค ์‚ฌ์‹ค t ์‹œ์ ์ด๋ผ๋Š” ๊ฒƒ์€ ๊ณ„์† ๋ณ€ํ™”ํ•˜๋ฉด์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ๊ฒฐ๊ตญ ์ „์ฒด ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ผ๋ฐ˜ํ™”๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Q = Querys : ๋ชจ๋“  ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋“ค K = Keys : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค V = Values : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค ์ด์ฒ˜๋Ÿผ ๊ธฐ์กด์—๋Š” ๋””์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ Q์ด๊ณ  ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ K๋ผ๋Š” ์ ์—์„œ Q์™€ K๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์…€ํ”„ ์–ดํ…์…˜์—์„œ๋Š” Q, K, V๊ฐ€ ์ „๋ถ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์…€ํ”„ ์–ดํ…์…˜์—์„œ์˜ Q, K, V๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. Q : ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค K : ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค V : ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค ์…€ํ”„ ์–ดํ…์…˜์— ๋Œ€ํ•œ ๊ตฌ์ฒด์ ์ธ ์‚ฌํ•ญ์„ ๋ฐฐ์šฐ๊ธฐ ์ „์— ์…€ํ”„ ์–ดํ…์…˜์„ ํ†ตํ•ด ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋Œ€ํ‘œ์ ์ธ ํšจ๊ณผ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ํŠธ๋žœ์Šคํฌ๋จธ์— ๋Œ€ํ•œ ๊ตฌ๊ธ€ AI ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŠธ์—์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ ๋ฌธ์žฅ์„ ๋ฒˆ์—ญํ•˜๋ฉด '๊ทธ ๋™๋ฌผ์€ ๊ธธ์„ ๊ฑด๋„ˆ์ง€ ์•Š์•˜๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๊ทธ๊ฒƒ์€ ๋„ˆ๋ฌด ํ”ผ๊ณคํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.'๋ผ๋Š” ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ๊ทธ๊ฒƒ(it)์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์€ ๊ณผ์—ฐ ๊ธธ(street)์ผ๊นŒ์š”? ๋™๋ฌผ(animal)์ผ๊นŒ์š”? ์šฐ๋ฆฌ๋Š” ํ”ผ๊ณคํ•œ ์ฃผ์ฒด๊ฐ€ ๋™๋ฌผ์ด๋ผ๋Š” ๊ฒƒ์„ ์•„์ฃผ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ธฐ๊ณ„๋Š” ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์…€ํ”„ ์–ดํ…์…˜์€ ์ž…๋ ฅ ๋ฌธ์žฅ ๋‚ด์˜ ๋‹จ์–ด๋“ค๋ผ๋ฆฌ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•จ์œผ๋กœ์จ ๊ทธ๊ฒƒ(it)์ด ๋™๋ฌผ(animal)๊ณผ ์—ฐ๊ด€๋˜์—ˆ์„ ํ™•๋ฅ ์ด ๋†’๋‹ค๋Š” ๊ฒƒ์„ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ์˜ ์…€ํ”„ ์–ดํ…์…˜์˜ ๋™์ž‘ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์•Œ์•„๋ด…์‹œ๋‹ค. 2) Q, K, V ๋ฒกํ„ฐ ์–ป๊ธฐ ์•ž์„œ ์…€ํ”„ ์–ดํ…์…˜์€ ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์„ ๊ฐ€์ง€๊ณ  ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ํ•˜์˜€๋Š”๋ฐ, ์‚ฌ์‹ค ์…€ํ”„ ์–ดํ…์…˜์€ ์ธ์ฝ”๋”์˜ ์ดˆ๊ธฐ ์ž…๋ ฅ์ธ m d l ์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ์…€ํ”„ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์šฐ์„  ๊ฐ ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค๋กœ๋ถ€ํ„ฐ Q ๋ฒกํ„ฐ, K ๋ฒกํ„ฐ, V ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ์ž‘์—…์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ด Q ๋ฒกํ„ฐ, K ๋ฒกํ„ฐ, V ๋ฒกํ„ฐ๋“ค์€ ์ดˆ๊ธฐ ์ž…๋ ฅ์ธ m d l ์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค๋ณด๋‹ค ๋” ์ž‘์€ ์ฐจ์›์„ ๊ฐ€์ง€๋Š”๋ฐ, ๋…ผ๋ฌธ์—์„œ๋Š” m d l =512์˜ ์ฐจ์›์„ ๊ฐ€์กŒ๋˜ ๊ฐ ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์„ 64์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” Q ๋ฒกํ„ฐ, K ๋ฒกํ„ฐ, V ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 64๋ผ๋Š” ๊ฐ’์€ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋˜ ๋‹ค๋ฅธ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ num_heads ๋กœ ์ธํ•ด ๊ฒฐ์ •๋˜๋Š”๋ฐ, ํŠธ๋žœ์Šคํฌ๋จธ๋Š” m d l num_heads ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ ๊ฐ Q ๋ฒกํ„ฐ, K ๋ฒกํ„ฐ, V ๋ฒกํ„ฐ์˜ ์ฐจ์›์œผ๋กœ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” num_heads ๋ฅผ 8๋กœ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์˜ˆ๋ฌธ ์ค‘ student๋ผ๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ Q, K, V์˜ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ๋ฒกํ„ฐ๋กœ๋ถ€ํ„ฐ ๋” ์ž‘์€ ๋ฒกํ„ฐ๋Š” ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์„ ๊ณฑํ•จ์œผ๋กœ์จ ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์€ m d l ( m d l /num_heads ) ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์€ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋…ผ๋ฌธ๊ณผ ๊ฐ™์ด m d l =512์ด๊ณ  num_heads =8๋ผ๋ฉด, ๊ฐ ๋ฒกํ„ฐ์— 3๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์„ ๊ณฑํ•˜๊ณ  64์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” Q, K, V ๋ฒกํ„ฐ๋ฅผ ์–ป์–ด๋ƒ…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๋‹จ์–ด ๋ฒกํ„ฐ ์ค‘ student ๋ฒกํ„ฐ๋กœ๋ถ€ํ„ฐ Q, K, V ๋ฒกํ„ฐ๋ฅผ ์–ป์–ด๋‚ด๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ชจ๋“  ๋‹จ์–ด ๋ฒกํ„ฐ์— ์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด I, am, a, student๋Š” ๊ฐ๊ฐ์˜ Q, K, V ๋ฒกํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. 3) ์Šค์ผ€์ผ๋“œ ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜(Scaled dot-product Attention) Q, K, V ๋ฒกํ„ฐ๋ฅผ ์–ป์—ˆ๋‹ค๋ฉด ์ง€๊ธˆ๋ถ€ํ„ฐ๋Š” ๊ธฐ์กด์— ๋ฐฐ์šด ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๊ฐ Q ๋ฒกํ„ฐ๋Š” ๋ชจ๋“  K ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ฅผ ๊ตฌํ•˜๊ณ , ์–ดํ…์…˜ ๋ถ„ํฌ๋ฅผ ๊ตฌํ•œ ๋’ค์— ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  V ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ค‘ ํ•ฉํ•˜์—ฌ ์–ดํ…์…˜ ๊ฐ’ ๋˜๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ๋ชจ๋“  Q ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์•ž์„œ ์–ดํ…์…˜ ์ฑ•ํ„ฐ์—์„œ ์–ดํ…์…˜ ํ•จ์ˆ˜์˜ ์ข…๋ฅ˜๋Š” ๋‹ค์–‘ํ•˜๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋Š” ์–ดํ…์…˜ ์ฑ•ํ„ฐ์— ์‚ฌ์šฉํ–ˆ๋˜ ๋‚ด์ ๋งŒ์„ ์‚ฌ์šฉํ•˜๋Š” ์–ดํ…์…˜ ํ•จ์ˆ˜ c r ( , ) q k ๊ฐ€ ์•„๋‹ˆ๋ผ ์—ฌ๊ธฐ์— ํŠน์ • ๊ฐ’์œผ๋กœ ๋‚˜๋ˆ ์ค€ ์–ดํ…์…˜ ํ•จ์ˆ˜์ธ c r ( , ) q k n ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์–ดํ…์…˜์„ ์–ดํ…์…˜ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šด ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜(dot-product attention)์—์„œ ๊ฐ’์„ ์Šค์ผ€์ผ๋งํ•˜๋Š” ๊ฒƒ์„ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค๊ณ  ํ•˜์—ฌ ์Šค์ผ€์ผ๋“œ ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜(Scaled dot-product Attention)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ๋‹จ์–ด I์— ๋Œ€ํ•œ Q ๋ฒกํ„ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ์„ค๋ช…ํ•˜๋Š” ๊ณผ์ •์€ am์— ๋Œ€ํ•œ Q ๋ฒกํ„ฐ, a์— ๋Œ€ํ•œ Q ๋ฒกํ„ฐ, student์— ๋Œ€ํ•œ Q ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ๋ชจ๋‘ ๋™์ผํ•œ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๋‹จ์–ด I์— ๋Œ€ํ•œ Q ๋ฒกํ„ฐ๊ฐ€ ๋ชจ๋“  K ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์˜ 128๊ณผ 32๋Š” ์ €์ž๊ฐ€ ์ž„์˜๋กœ ๊ฐ€์ •ํ•œ ์ˆ˜์น˜๋กœ ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š์•„๋„ ์ข‹์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋Š” ๊ฐ๊ฐ ๋‹จ์–ด I๊ฐ€ ๋‹จ์–ด I, am, a, student์™€ ์–ผ๋งˆ๋‚˜ ์—ฐ๊ด€๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ˆ˜์น˜์ž…๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ๋‚ด์  ๊ฐ’์„ ์Šค์ผ€์ผ๋งํ•˜๋Š” ๊ฐ’์œผ๋กœ K ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ ๋‚˜ํƒ€๋‚ด๋Š” k ์— ๋ฃจํŠธ๋ฅผ ์”Œ์šด k ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ•˜์˜€๋“ฏ์ด ๋…ผ๋ฌธ์—์„œ k d o e /num_heads ๋ผ๋Š” ์‹์— ๋”ฐ๋ผ์„œ 64์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ k ๋Š” 8์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด์ œ ์–ดํ…์…˜ ์Šค์ฝ”์–ด์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ดํ…์…˜ ๋ถ„ํฌ(Attention Distribution)์„ ๊ตฌํ•˜๊ณ , ๊ฐ V ๋ฒกํ„ฐ์™€ ๊ฐ€์ค‘ ํ•ฉํ•˜์—ฌ ์–ดํ…์…˜ ๊ฐ’(Attention Value)์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋‹จ์–ด I์— ๋Œ€ํ•œ ์–ดํ…์…˜ ๊ฐ’ ๋˜๋Š” ๋‹จ์–ด I์— ๋Œ€ํ•œ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ(context vector)๋ผ๊ณ ๋„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. am์— ๋Œ€ํ•œ Q ๋ฒกํ„ฐ, a์— ๋Œ€ Q ๋ฒกํ„ฐ, student์— ๋Œ€ํ•œ Q ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ๋ชจ๋‘ ๋™์ผํ•œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ ๊ฐ๊ฐ์— ๋Œ€ํ•œ ์–ดํ…์…˜ ๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ•œ ๊ฐ€์ง€ ์˜๋ฌธ์ด ๋‚จ์Šต๋‹ˆ๋‹ค. ๊ตณ์ด ์ด๋ ‡๊ฒŒ ๊ฐ Q ๋ฒกํ„ฐ๋งˆ๋‹ค ์ผ์ผ์ด ๋”ฐ๋กœ ์—ฐ์‚ฐํ•  ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ์š”? 4) ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ผ๊ด„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์‚ฌ์‹ค ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ Q, K, V ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•˜๊ณ  ์Šค์ผ€์ผ๋“œ ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•˜์˜€๋˜ ์œ„์˜ ๊ณผ์ •๋“ค์€ ๋ฒกํ„ฐ ์—ฐ์‚ฐ์ด ์•„๋‹ˆ๋ผ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ผ๊ด„ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋ฒกํ„ฐ ์—ฐ์‚ฐ์œผ๋กœ ์„ค๋ช…ํ•˜์˜€๋˜ ์ด์œ ๋Š” ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•œ ๊ณผ์ •์ด๊ณ , ์‹ค์ œ๋กœ๋Š” ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ณผ์ •์„ ๋ฒกํ„ฐ๊ฐ€ ์•„๋‹Œ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„ , ๊ฐ ๋‹จ์–ด ๋ฒกํ„ฐ๋งˆ๋‹ค ์ผ์ผ์ด ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์„ ๊ณฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฌธ์žฅ ํ–‰๋ ฌ์— ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์„ ๊ณฑํ•˜์—ฌ Q ํ–‰๋ ฌ, K ํ–‰๋ ฌ, V ํ–‰๋ ฌ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์—ฌ๊ธฐ์„œ Q ํ–‰๋ ฌ์„ K ํ–‰๋ ฌ์„ ์ „์น˜ํ•œ ํ–‰๋ ฌ๊ณผ ๊ณฑํ•ด์ค€๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๊ฐ๊ฐ์˜ ๋‹จ์–ด์˜ Q ๋ฒกํ„ฐ์™€ K ๋ฒกํ„ฐ์˜ ๋‚ด์ ์ด ๊ฐ ํ–‰๋ ฌ์˜ ์›์†Œ๊ฐ€ ๋˜๋Š” ํ–‰๋ ฌ์ด ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์œ„์˜ ๊ทธ๋ฆผ์˜ ๊ฒฐ๊ณผ ํ–‰๋ ฌ์˜ ๊ฐ’์— ์ „์ฒด์ ์œผ๋กœ k ๋ฅผ ๋‚˜๋ˆ„์–ด์ฃผ๋ฉด ์ด๋Š” ๊ฐ ํ–‰๊ณผ ์—ด์ด ์–ดํ…์…˜ ์Šค์ฝ”์–ด ๊ฐ’์„ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ์ด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด I ํ–‰๊ณผ student ์—ด์˜ ๊ฐ’์€ I์˜ Q ๋ฒกํ„ฐ์™€ student์˜ K ๋ฒกํ„ฐ์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ๊ฐ’์ž…๋‹ˆ๋‹ค. ์œ„ ํ–‰๋ ฌ์„ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์ด๋ผ ํ•ฉ์‹œ๋‹ค. ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์„ ๊ตฌํ•˜์˜€๋‹ค๋ฉด ๋‚จ์€ ๊ฒƒ์€ ์–ดํ…์…˜ ๋ถ„ํฌ๋ฅผ ๊ตฌํ•˜๊ณ , ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•œ ์–ดํ…์…˜ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , V ํ–‰๋ ฌ์„ ๊ณฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ•ด๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๊ฐ ๋‹จ์–ด์˜ ์–ดํ…์…˜ ๊ฐ’์„ ๋ชจ๋‘ ๊ฐ€์ง€๋Š” ์–ดํ…์…˜ ๊ฐ’ ํ–‰๋ ฌ์ด ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋ชจ๋“  ๊ฐ’์ด ์ผ๊ด„ ๊ณ„์‚ฐ๋˜๋Š” ๊ณผ์ •์„ ์‹์œผ๋กœ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ•ด๋‹น ์‹์€ ์‹ค์ œ ํŠธ๋žœ์Šคํฌ๋จธ ๋…ผ๋ฌธ์— ๊ธฐ์žฌ๋œ ์•„๋ž˜์˜ ์ˆ˜์‹๊ณผ ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•˜๋Š” ์‹์ž…๋‹ˆ๋‹ค. t e t o ( , , ) s f m x ( K d) ์œ„์˜ ํ–‰๋ ฌ ์—ฐ์‚ฐ์— ์‚ฌ์šฉ๋œ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ๋ชจ๋‘ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๊ธธ์ด๋ฅผ seq_len์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌธ์žฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ( seq_len d o e) ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— 3๊ฐœ์˜ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์„ ๊ณฑํ•ด์„œ Q, K, V ํ–‰๋ ฌ์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ํ–‰๋ ฌ์˜ ๊ฐ ํ–‰์— ํ•ด๋‹น๋˜๋Š” Q ๋ฒกํ„ฐ์™€ K ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ k ๋ผ๊ณ  ํ•˜๊ณ , V ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ v ๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด Q ํ–‰๋ ฌ๊ณผ K ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ( seq_len d) ์ด๋ฉฐ, V ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ( seq_len d) ๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌธ์žฅ ํ–‰๋ ฌ๊ณผ Q, K, V ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ ์ถ”์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Q W๋Š” ( m d l d) ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋ฉฐ, V ( m d l d) ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋‹จ, ๋…ผ๋ฌธ์—์„œ๋Š” k d์˜ ์ฐจ์›์€ m d l /num_heads ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฆ‰, m d l /num_heads d = v ์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ o t a ( K d) ์‹์„ ์ ์šฉํ•˜์—ฌ ๋‚˜์˜ค๋Š” ์–ดํ…์…˜ ๊ฐ’ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ( seq_len d) ์ด ๋ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 5) ์Šค์ผ€์ผ๋“œ ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜ ๊ตฌํ˜„ํ•˜๊ธฐ def scaled_dot_product_attention(query, key, value, mask): # query ํฌ๊ธฐ : (batch_size, num_heads, query์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model/num_heads) # key ํฌ๊ธฐ : (batch_size, num_heads, key์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model/num_heads) # value ํฌ๊ธฐ : (batch_size, num_heads, value์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model/num_heads) # padding_mask : (batch_size, 1, 1, key์˜ ๋ฌธ์žฅ ๊ธธ์ด) # Q์™€ K์˜ ๊ณฑ. ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ. matmul_qk = tf.matmul(query, key, transpose_b=True) # ์Šค์ผ€์ผ๋ง # dk์˜ ๋ฃจํŠธ ๊ฐ’์œผ๋กœ ๋‚˜๋ˆ ์ค€๋‹ค. depth = tf.cast(tf.shape(key)[-1], tf.float32) logits = matmul_qk / tf.math.sqrt(depth) # ๋งˆ์Šคํ‚น. ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์˜ ๋งˆ์Šคํ‚น ํ•  ์œ„์น˜์— ๋งค์šฐ ์ž‘์€ ์Œ์ˆ˜ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค. # ๋งค์šฐ ์ž‘์€ ๊ฐ’์ด๋ฏ€๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋ฉด ํ–‰๋ ฌ์˜ ํ•ด๋‹น ์œ„์น˜์˜ ๊ฐ’์€ 0์ด ๋œ๋‹ค. if mask is not None: logits += (mask * -1e9) # ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ๋งˆ์ง€๋ง‰ ์ฐจ์›์ธ key์˜ ๋ฌธ์žฅ ๊ธธ์ด ๋ฐฉํ–ฅ์œผ๋กœ ์ˆ˜ํ–‰๋œ๋‹ค. # attention weight : (batch_size, num_heads, query์˜ ๋ฌธ์žฅ ๊ธธ์ด, key์˜ ๋ฌธ์žฅ ๊ธธ์ด) attention_weights = tf.nn.softmax(logits, axis=-1) # output : (batch_size, num_heads, query์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model/num_heads) output = tf.matmul(attention_weights, value) return output, attention_weights Q ํ–‰๋ ฌ๊ณผ K ํ–‰๋ ฌ์„ ์ „์น˜ํ•œ ํ–‰๋ ฌ์„ ๊ณฑํ•˜๊ณ , ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ดํ…์…˜ ๋ถ„ํฌ ํ–‰๋ ฌ์„ ์–ป์€ ๋’ค์— V ํ–‰๋ ฌ๊ณผ ๊ณฑํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ์—์„œ mask๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” if ๋ฌธ์€ ์•„์ง ๋ฐฐ์šฐ์ง€ ์•Š์€ ๋‚ด์šฉ์œผ๋กœ ์ง€๊ธˆ์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. scaled_dot_product_attention ํ•จ์ˆ˜๊ฐ€ ์ •์ƒ ์ž‘๋™ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  temp_q, temp_k, temp_v๋ผ๋Š” ์ž„์˜์˜ Query, Key, Value ํ–‰๋ ฌ์„ ๋งŒ๋“ค๊ณ , ์ด๋ฅผ scaled_dot_product_attention ํ•จ์ˆ˜์— ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด ํ•จ์ˆ˜๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ž„์˜์˜ Query, Key, Value์ธ Q, K, V ํ–‰๋ ฌ ์ƒ์„ฑ np.set_printoptions(suppress=True) temp_k = tf.constant([[10,0,0], [0,10,0], [0,0,10], [0,0,10]], dtype=tf.float32) # (4, 3) temp_v = tf.constant([[ 1,0], [ 10,0], [ 100,5], [1000,6]], dtype=tf.float32) # (4, 2) temp_q = tf.constant([[0, 10, 0]], dtype=tf.float32) # (1, 3) ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•  ์ ์€ Query์— ํ•ด๋‹นํ•˜๋Š” temp_q์˜ ๊ฐ’ [0, 10, 0]์€ Key์— ํ•ด๋‹นํ•˜๋Š” temp_k์˜ ๋‘ ๋ฒˆ์งธ ๊ฐ’ [0, 10, 0]๊ณผ ์ผ์น˜ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์–ดํ…์…˜ ๋ถ„ํฌ์™€ ์–ดํ…์…˜ ๊ฐ’์€ ๊ณผ์—ฐ ์–ด๋–ค ๊ฐ’์ด ๋‚˜์˜ฌ๊นŒ์š”? # ํ•จ์ˆ˜ ์‹คํ–‰ temp_out, temp_attn = scaled_dot_product_attention(temp_q, temp_k, temp_v, None) print(temp_attn) # ์–ดํ…์…˜ ๋ถ„ํฌ(์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์˜ ๋‚˜์—ด) print(temp_out) # ์–ดํ…์…˜ ๊ฐ’ tf.Tensor([[0. 1. 0. 0.]], shape=(1, 4), dtype=float32) tf.Tensor([[10. 0.]], shape=(1, 2), dtype=float32) Query๋Š” 4๊ฐœ์˜ Key ๊ฐ’ ์ค‘ ๋‘ ๋ฒˆ์งธ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋ฏ€๋กœ ์–ดํ…์…˜ ๋ถ„ํฌ๋Š” [0, 1, 0, 0]์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ๊ฒฐ๊ณผ์ ์œผ๋กœ Value์˜ ๋‘ ๋ฒˆ์งธ ๊ฐ’์ธ [10, 0]์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” Query์˜ ๊ฐ’๋งŒ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”๋ณด๊ณ  ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฒˆ์— ์‚ฌ์šฉํ•  Query ๊ฐ’ [0, 0, 10]์€ Key์˜ ์„ธ ๋ฒˆ์งธ ๊ฐ’๊ณผ, ๋„ค ๋ฒˆ์งธ ๊ฐ’ ๋‘ ๊ฐœ์˜ ๊ฐ’ ๋ชจ๋‘์™€ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. temp_q = tf.constant([[0, 0, 10]], dtype=tf.float32) temp_out, temp_attn = scaled_dot_product_attention(temp_q, temp_k, temp_v, None) print(temp_attn) # ์–ดํ…์…˜ ๋ถ„ํฌ(์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์˜ ๋‚˜์—ด) print(temp_out) # ์–ดํ…์…˜ ๊ฐ’ tf.Tensor([[0. 0. 0.5 0.5]], shape=(1, 4), dtype=float32) tf.Tensor([[550. 5.5]], shape=(1, 2), dtype=float32) Query์˜ ๊ฐ’์€ Key์˜ ์„ธ ๋ฒˆ์งธ ๊ฐ’๊ณผ ๋„ค ๋ฒˆ์งธ ๊ฐ’ ๋‘ ๊ฐœ์˜ ๊ฐ’๊ณผ ๋ชจ๋‘ ์œ ์‚ฌํ•˜๋‹ค๋Š” ์˜๋ฏธ์—์„œ ์–ดํ…์…˜ ๋ถ„ํฌ๋Š” [0, 0, 0.5, 0.5]์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‚˜์˜ค๋Š” ๊ฐ’ [550, 5.5]๋Š” Value์˜ ์„ธ ๋ฒˆ์งธ ๊ฐ’ [100, 5]์— 0.5๋ฅผ ๊ณฑํ•œ ๊ฐ’๊ณผ ๋„ค ๋ฒˆ์งธ ๊ฐ’ [1000, 6]์— 0.5๋ฅผ ๊ณฑํ•œ ๊ฐ’์˜ ์›์†Œ๋ณ„ ํ•ฉ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํ•˜๋‚˜๊ฐ€ ์•„๋‹Œ 3๊ฐœ์˜ Query์˜ ๊ฐ’์„ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. temp_q = tf.constant([[0, 0, 10], [0, 10, 0], [10, 10, 0]], dtype=tf.float32) # (3, 3) temp_out, temp_attn = scaled_dot_product_attention(temp_q, temp_k, temp_v, None) print(temp_attn) # ์–ดํ…์…˜ ๋ถ„ํฌ(์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์˜ ๋‚˜์—ด) print(temp_out) # ์–ดํ…์…˜ ๊ฐ’ tf.Tensor( [[0. 0. 0.5 0.5] [0. 1. 0. 0. ] [0.5 0.5 0. 0. ]], shape=(3, 4), dtype=float32) tf.Tensor( [[550. 5.5] [ 10. 0. ] [ 5.5 0. ]], shape=(3, 2), dtype=float32) 6) ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜(Multi-head Attention) ์•ž์„œ ๋ฐฐ์šด ์–ดํ…์…˜์—์„œ๋Š” m d l ์˜ ์ฐจ์›์„ ๊ฐ€์ง„ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ num_heads ๋กœ ๋‚˜๋ˆˆ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” Q, K, V ๋ฒกํ„ฐ๋กœ ๋ฐ”๊พธ๊ณ  ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฌธ ๊ธฐ์ค€์œผ๋กœ๋Š” 512์˜ ์ฐจ์›์˜ ๊ฐ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ 8๋กœ ๋‚˜๋ˆ„์–ด 64์ฐจ์›์˜ Q, K, V ๋ฒกํ„ฐ๋กœ ๋ฐ”๊พธ์–ด์„œ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•œ ์…ˆ์ธ๋ฐ, ์ด์ œ num_heads ์˜ ์˜๋ฏธ์™€ ์™œ m d l ์˜ ์ฐจ์›์„ ๊ฐ€์ง„ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์–ดํ…์…˜์„ ํ•˜์ง€ ์•Š๊ณ  ์ฐจ์›์„ ์ถ•์†Œ์‹œํ‚จ ๋ฒกํ„ฐ๋กœ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•˜์˜€๋Š”์ง€ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ ์—ฐ๊ตฌ์ง„์€ ํ•œ ๋ฒˆ์˜ ์–ดํ…์…˜์„ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์—ฌ๋Ÿฌ ๋ฒˆ์˜ ์–ดํ…์…˜์„ ๋ณ‘๋ ฌ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ๊ณผ์ ์ด๋ผ๊ณ  ํŒ๋‹จํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ m d l ์˜ ์ฐจ์›์„ num_heads ๊ฐœ๋กœ ๋‚˜๋ˆ„์–ด m d l /num_heads ์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” Q, K, V์— ๋Œ€ํ•ด์„œ num_heads ๊ฐœ์˜ ๋ณ‘๋ ฌ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ num_heads ์˜ ๊ฐ’์„ 8๋กœ ์ง€์ •ํ•˜์˜€๊ณ , 8๊ฐœ์˜ ๋ณ‘๋ ฌ ์–ดํ…์…˜์ด ์ด๋ฃจ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์œ„์—์„œ ์„ค๋ช…ํ•œ ์–ดํ…์…˜์ด 8๊ฐœ๋กœ ๋ณ‘๋ ฌ๋กœ ์ด๋ฃจ์–ด์ง€๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋•Œ ๊ฐ๊ฐ์˜ ์–ดํ…์…˜ ๊ฐ’ ํ–‰๋ ฌ์„ ์–ดํ…์…˜ ํ—ค๋“œ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ Q W, V ์˜ ๊ฐ’์€ 8๊ฐœ์˜ ์–ดํ…์…˜ ํ—ค๋“œ๋งˆ๋‹ค ์ „๋ถ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ ์–ดํ…์…˜์œผ๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ํšจ๊ณผ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? ๊ทธ๋ฆฌ์Šค ๋กœ๋งˆ์‹ ํ™”์—๋Š” ๋จธ๋ฆฌ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ดด๋ฌผ ํžˆ๋“œ๋ผ๋‚˜ ์ผ€๋กœ๋ฒ ๋กœ์Šค๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด ๊ดด๋ฌผ๋“ค์˜ ํŠน์ง•์€ ๋จธ๋ฆฌ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ ์‹œ์ ์—์„œ ์ƒ๋Œ€๋ฐฉ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์‹œ๊ฐ์—์„œ ๋†“์น˜๋Š” ๊ฒŒ ๋ณ„๋กœ ์—†์„ ํ…Œ๋‹ˆ๊นŒ ์ด๋Ÿฐ ๊ดด๋ฌผ๋“ค์—๊ฒŒ ๊ธฐ์Šต์„ ํ•˜๋Š” ๊ฒƒ์ด ๊ต‰์žฅํžˆ ํž˜์ด ๋“ค ๊ฒ๋‹ˆ๋‹ค. ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜๋„ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜์„ ๋ณ‘๋ ฌ๋กœ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋‹ค๋ฅธ ์‹œ๊ฐ์œผ๋กœ ์ •๋ณด๋“ค์„ ์ˆ˜์ง‘ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ์‚ฌ์šฉํ•œ ์˜ˆ๋ฌธ '๊ทธ ๋™๋ฌผ์€ ๊ธธ์„ ๊ฑด๋„ˆ์ง€ ์•Š์•˜๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๊ทธ๊ฒƒ์€ ๋„ˆ๋ฌด ํ”ผ๊ณคํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.'๋ฅผ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. ๋‹จ์–ด ๊ทธ๊ฒƒ(it)์ด ์ฟผ๋ฆฌ์˜€๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ฆ‰, it์— ๋Œ€ํ•œ Q ๋ฒกํ„ฐ๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ๋‹จ์–ด์™€์˜ ์—ฐ๊ด€๋„๋ฅผ ๊ตฌํ•˜์˜€์„ ๋•Œ ์ฒซ ๋ฒˆ์งธ ์–ดํ…์…˜ ํ—ค๋“œ๋Š” '๊ทธ๊ฒƒ(it)'๊ณผ '๋™๋ฌผ(animal)'์˜ ์—ฐ๊ด€๋„๋ฅผ ๋†’๊ฒŒ ๋ณธ๋‹ค๋ฉด, ๋‘ ๋ฒˆ์งธ ์–ดํ…์…˜ ํ—ค๋“œ๋Š” '๊ทธ๊ฒƒ(it)'๊ณผ 'ํ”ผ๊ณคํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค(tired)'์˜ ์—ฐ๊ด€๋„๋ฅผ ๋†’๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์–ดํ…์…˜ ํ—ค๋“œ๋Š” ์ „๋ถ€ ๋‹ค๋ฅธ ์‹œ๊ฐ์—์„œ ๋ณด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ ์–ดํ…์…˜์„ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•˜์˜€๋‹ค๋ฉด ๋ชจ๋“  ์–ดํ…์…˜ ํ—ค๋“œ๋ฅผ ์—ฐ๊ฒฐ(concatenate) ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋‘ ์—ฐ๊ฒฐ๋œ ์–ดํ…์…˜ ํ—ค๋“œ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ( seq_len d o e) ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๊ทธ๋ฆผ์—์„œ๋Š” ์ฑ…์˜ ์ง€๋ฉด ์ƒ์˜ ํ•œ๊ณ„๋กœ 4์ฐจ์›์„ m d l =512๋กœ ํ‘œํ˜„ํ•˜๊ณ , 2์ฐจ์›์„ v =64๋กœ ํ‘œํ˜„ํ•ด์™”๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ๊ทธ๋ฆผ์˜ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์— ํ˜ผ๋™์˜ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋‚˜ 8๊ฐœ์˜ ์–ดํ…์…˜ ํ—ค๋“œ์˜ ์—ฐ๊ฒฐ(concatenate) ๊ณผ์ •์˜ ์ดํ•ด๋ฅผ ์œ„ํ•ด ์ด๋ฒˆ ํ–‰๋ ฌ๋งŒ ์˜ˆ์™ธ๋กœ ์œ„์™€ ๊ฐ™์ด m d l ์˜ ํฌ๊ธฐ๋ฅผ v ์˜ 8๋ฐฐ์ธ 16์ฐจ์›์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ๋‹ค์‹œ m d l ๋ฅผ 4์ฐจ์›์œผ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ํ—ค๋“œ๋ฅผ ๋ชจ๋‘ ์—ฐ๊ฒฐํ•œ ํ–‰๋ ฌ์€ ๋˜ ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ o ์„ ๊ณฑํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ๋‚˜์˜จ ๊ฒฐ๊ณผ ํ–‰๋ ฌ์ด ๋ฉ€ํ‹ฐ-ํ—ค๋“œ ์–ดํ…์…˜์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฌผ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์–ดํ…์…˜ ํ—ค๋“œ๋ฅผ ๋ชจ๋‘ ์—ฐ๊ฒฐํ•œ ํ–‰๋ ฌ์ด ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ o ๊ณผ ๊ณฑํ•ด์ง€๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฒฐ๊ณผ๋ฌผ์ธ ๋ฉ€ํ‹ฐ-ํ—ค๋“œ ์–ดํ…์…˜ ํ–‰๋ ฌ์€ ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์ด์—ˆ๋˜ ๋ฌธ์žฅ ํ–‰๋ ฌ์˜ ( seq_len d o e) ํฌ๊ธฐ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ธ์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ๋ฉ€ํ‹ฐ-ํ—ค๋“œ ์–ดํ…์…˜ ๋‹จ๊ณ„๋ฅผ ๋๋งˆ์ณค์„ ๋•Œ, ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์™”๋˜ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๊ฐ€ ์•„์ง ์œ ์ง€๋˜๊ณ  ์žˆ์Œ์„ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ๋ฉ€ํ‹ฐ-ํ—ค๋“œ ์–ดํ…์…˜๊ณผ ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ํฌ์ง€์…˜ ์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์„ ์ง€๋‚˜๋ฉด์„œ ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ฌ ๋•Œ์˜ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ๊ณ„์† ์œ ์ง€๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ๋™์ผํ•œ ๊ตฌ์กฐ์˜ ์ธ์ฝ”๋”๋ฅผ ์Œ“์€ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฌธ ๊ธฐ์ค€์œผ๋กœ๋Š” ์ธ์ฝ”๋”๊ฐ€ ์ด 6๊ฐœ์ž…๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์—์„œ์˜ ์ž…๋ ฅ์˜ ํฌ๊ธฐ๊ฐ€ ์ถœ๋ ฅ์—์„œ๋„ ๋™์ผ ํฌ๊ธฐ๋กœ ๊ณ„์† ์œ ์ง€๋˜์–ด์•ผ๋งŒ ๋‹ค์Œ ์ธ์ฝ”๋”์—์„œ๋„ ๋‹ค์‹œ ์ž…๋ ฅ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 7) ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜(Multi-head Attention) ๊ตฌํ˜„ํ•˜๊ธฐ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์—์„œ๋Š” ํฌ๊ฒŒ ๋‘ ์ข…๋ฅ˜์˜ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค. Q, K, V ํ–‰๋ ฌ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์ธ WQ, WK, WV ํ–‰๋ ฌ๊ณผ ๋ฐ”๋กœ ์–ดํ…์…˜ ํ—ค๋“œ๋“ค์„ ์—ฐ๊ฒฐ(concatenation) ํ›„์— ๊ณฑํ•ด์ฃผ๋Š” WO ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์„ ๊ณฑํ•˜๋Š” ๊ฒƒ์„ ๊ตฌํ˜„ ์ƒ์—์„œ๋Š” ์ž…๋ ฅ์„ ์ „๊ฒฐํ•ฉ์ธต. ์ฆ‰, ๋ฐ€์ง‘์ธต(Dense layer)์„ ์ง€๋‚˜๊ฒŒ ํ•˜์—ฌ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค ์ฝ”๋“œ ์ƒ์œผ๋กœ ์ง€๊ธˆ๊นŒ์ง€ ์‚ฌ์šฉํ•ด์™”๋˜ Dense()์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. Dense(units) ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์˜ ๊ตฌํ˜„์€ ํฌ๊ฒŒ ๋‹ค์„ฏ ๊ฐ€์ง€ ํŒŒํŠธ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. WQ, WK, WV์— ํ•ด๋‹นํ•˜๋Š” d_model ํฌ๊ธฐ์˜ ๋ฐ€์ง‘์ธต(Dense layer)์„ ์ง€๋‚˜๊ฒŒ ํ•œ๋‹ค. ์ง€์ •๋œ ํ—ค๋“œ ์ˆ˜(num_heads) ๋งŒํผ ๋‚˜๋ˆˆ๋‹ค(split). ์Šค์ผ€์ผ๋“œ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜. ๋‚˜๋ˆ ์กŒ๋˜ ํ—ค๋“œ๋“ค์„ ์—ฐ๊ฒฐ(concatenatetion) ํ•œ๋‹ค. WO์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ€์ง‘์ธต์„ ์ง€๋‚˜๊ฒŒ ํ•œ๋‹ค. ์ด๋ก ์œผ๋กœ ์„ค๋ช…ํ•  ๋•Œ๋ณด๋‹ค ์‹ฌํ”Œํ•˜๊ฒŒ ๊ตฌ์„ฑ๋˜์—ˆ๋Š”๋ฐ ๊ฒฐ๊ตญ ๊ทผ๋ณธ์ ์œผ๋กœ ๋™์ผํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. class MultiHeadAttention(tf.keras.layers.Layer): def __init__(self, d_model, num_heads, name="multi_head_attention"): super(MultiHeadAttention, self).__init__(name=name) self.num_heads = num_heads self.d_model = d_model assert d_model % self.num_heads == 0 # d_model์„ num_heads๋กœ ๋‚˜๋ˆˆ ๊ฐ’. # ๋…ผ๋ฌธ ๊ธฐ์ค€ : 64 self.depth = d_model // self.num_heads # WQ, WK, WV์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ€์ง‘์ธต ์ •์˜ self.query_dense = tf.keras.layers.Dense(units=d_model) self.key_dense = tf.keras.layers.Dense(units=d_model) self.value_dense = tf.keras.layers.Dense(units=d_model) # WO์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ€์ง‘์ธต ์ •์˜ self.dense = tf.keras.layers.Dense(units=d_model) # num_heads ๊ฐœ์ˆ˜๋งŒํผ q, k, v๋ฅผ split ํ•˜๋Š” ํ•จ์ˆ˜ def split_heads(self, inputs, batch_size): inputs = tf.reshape( inputs, shape=(batch_size, -1, self.num_heads, self.depth)) return tf.transpose(inputs, perm=[0, 2, 1, 3]) def call(self, inputs): query, key, value, mask = inputs['query'], inputs['key'], inputs[ 'value'], inputs['mask'] batch_size = tf.shape(query)[0] # 1. WQ, WK, WV์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ€์ง‘์ธต ์ง€๋‚˜๊ธฐ # q : (batch_size, query์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model) # k : (batch_size, key์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model) # v : (batch_size, value์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model) # ์ฐธ๊ณ ) ์ธ์ฝ”๋”(k, v)-๋””์ฝ”๋”(q) ์–ดํ…์…˜์—์„œ๋Š” query ๊ธธ์ด์™€ key, value์˜ ๊ธธ์ด๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. query = self.query_dense(query) key = self.key_dense(key) value = self.value_dense(value) # 2. ํ—ค๋“œ ๋‚˜๋ˆ„๊ธฐ # q : (batch_size, num_heads, query์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model/num_heads) # k : (batch_size, num_heads, key์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model/num_heads) # v : (batch_size, num_heads, value์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model/num_heads) query = self.split_heads(query, batch_size) key = self.split_heads(key, batch_size) value = self.split_heads(value, batch_size) # 3. ์Šค์ผ€์ผ๋“œ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜. ์•ž์„œ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜ ์‚ฌ์šฉ. # (batch_size, num_heads, query์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model/num_heads) scaled_attention, _ = scaled_dot_product_attention(query, key, value, mask) # (batch_size, query์˜ ๋ฌธ์žฅ ๊ธธ์ด, num_heads, d_model/num_heads) scaled_attention = tf.transpose(scaled_attention, perm=[0, 2, 1, 3]) # 4. ํ—ค๋“œ ์—ฐ๊ฒฐ(concatenate) ํ•˜๊ธฐ # (batch_size, query์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model) concat_attention = tf.reshape(scaled_attention, (batch_size, -1, self.d_model)) # 5. WO์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ€์ง‘์ธต ์ง€๋‚˜๊ธฐ # (batch_size, query์˜ ๋ฌธ์žฅ ๊ธธ์ด, d_model) outputs = self.dense(concat_attention) return outputs ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ํ•œ ๊ฐ€์ง€๋งŒ ๋” ์„ค๋ช…ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์— ๋Œ€ํ•œ ์„ค๋ช…์œผ๋กœ ๋„˜์–ด๊ฐ€ ๋ด…์‹œ๋‹ค. 8) ํŒจ๋”ฉ ๋งˆ์Šคํฌ(Padding Mask) ์•„์ง ์„ค๋ช…ํ•˜์ง€ ์•Š์€ ๋‚ด์šฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๊ตฌํ˜„ํ•œ ์Šค์ผ€์ผ๋“œ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€๋ฅผ ๋ณด๋ฉด mask๋ผ๋Š” ๊ฐ’์„ ์ธ์ž๋กœ ๋ฐ›์•„์„œ, ์ด mask ๊ฐ’์—๋‹ค๊ฐ€ -1e9๋ผ๋Š” ์•„์ฃผ ์ž‘์€ ์Œ์ˆ˜ ๊ฐ’์„ ๊ณฑํ•œ ํ›„ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์— ๋”ํ•ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์—ฐ์‚ฐ์˜ ์ •์ฒด๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? def scaled_dot_product_attention(query, key, value, mask): ... ์ค‘๋žต ... logits += (mask * -1e9) # ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์ธ logits์— mask*-1e9 ๊ฐ’์„ ๋”ํ•ด์ฃผ๊ณ  ์žˆ๋‹ค. ... ์ค‘๋žต ... ์ด๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์— <PAD> ํ† ํฐ์ด ์žˆ์„ ๊ฒฝ์šฐ ์–ดํ…์…˜์—์„œ ์‚ฌ์‹ค์ƒ ์ œ์™ธํ•˜๊ธฐ ์œ„ํ•œ ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด <PAD>๊ฐ€ ํฌํ•จ๋œ ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ์…€ํ”„ ์–ดํ…์…˜์˜ ์˜ˆ์ œ๋ฅผ ๋ด…์‹œ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์„ ์–ป๋Š” ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‚ฌ์‹ค ๋‹จ์–ด <PAD>์˜ ๊ฒฝ์šฐ์—๋Š” ์‹ค์งˆ์ ์ธ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋Š” Key์˜ ๊ฒฝ์šฐ์— <PAD> ํ† ํฐ์ด ์กด์žฌํ•œ๋‹ค๋ฉด ์ด์— ๋Œ€ํ•ด์„œ๋Š” ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜์ง€ ์•Š๋„๋ก ๋งˆ์Šคํ‚น(Masking)์„ ํ•ด์ฃผ๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งˆ์Šคํ‚น์ด๋ž€ ์–ดํ…์…˜์—์„œ ์ œ์™ธํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ’์„ ๊ฐ€๋ฆฐ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์—์„œ ํ–‰์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์žฅ์€ Query์ด๊ณ , ์—ด์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์žฅ์€ Key์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Key์— <PAD>๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์—ด ์ „์ฒด๋ฅผ ๋งˆ์Šคํ‚น์„ ํ•ด์ค๋‹ˆ๋‹ค. ๋งˆ์Šคํ‚น์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์˜ ๋งˆ์Šคํ‚น ์œ„์น˜์— ๋งค์šฐ ์ž‘์€ ์Œ์ˆ˜ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งค์šฐ ์ž‘์€ ์Œ์ˆ˜ ๊ฐ’์ด๋ผ๋Š” ๊ฒƒ์€ -1,000,000,000๊ณผ ๊ฐ™์€ -๋ฌดํ•œ๋Œ€์— ๊ฐ€๊นŒ์šด ์ˆ˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜์ง€ ์•Š์€ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ์—ฐ์‚ฐ ์ˆœ์„œ๋ผ๋ฉด ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ณ , ๊ทธ ํ›„ Value ํ–‰๋ ฌ๊ณผ ๊ณฑํ•ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ˜„์žฌ ๋งˆ์Šคํ‚น ์œ„์น˜์— ๋งค์šฐ ์ž‘์€ ์Œ์ˆ˜ ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์ด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ํ›„์—๋Š” ํ•ด๋‹น ์œ„์น˜์˜ ๊ฐ’์€ 0์ด ๋˜์–ด ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ์ผ์— <PAD> ํ† ํฐ์ด ๋ฐ˜์˜๋˜์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ํ›„๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋ฉด ๊ฐ ํ–‰์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์˜ ์ดํ•ฉ์€ 1์ด ๋˜๋Š”๋ฐ, ๋‹จ์–ด <PAD>์˜ ๊ฒฝ์šฐ์—๋Š” 0์ด ๋˜์–ด ์–ด๋–ค ์œ ์˜๋ฏธํ•œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŒจ๋”ฉ ๋งˆ์Šคํฌ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ž…๋ ฅ๋œ ์ •์ˆ˜ ์‹œํ€€์Šค์—์„œ ํŒจ๋”ฉ ํ† ํฐ์˜ ์ธ๋ฑ์Šค์ธ์ง€, ์•„๋‹Œ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ•จ์ˆ˜๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค์—์„œ 0์ธ ๊ฒฝ์šฐ์—๋Š” 1๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” 0์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. def create_padding_mask(x): mask = tf.cast(tf.math.equal(x, 0), tf.float32) # (batch_size, 1, 1, key์˜ ๋ฌธ์žฅ ๊ธธ์ด) return mask[:, tf.newaxis, tf.newaxis, :] ์ž„์˜์˜ ์ •์ˆ˜ ์‹œํ€€์Šค ์ž…๋ ฅ์„ ๋„ฃ์–ด์„œ ์–ด๋–ป๊ฒŒ ๋ณ€ํ™˜๋˜๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(create_padding_mask(tf.constant([[1, 21, 777, 0, 0]]))) tf.Tensor([[[[0. 0. 0. 1. 1.]]]], shape=(1, 1, 1, 5), dtype=float32) ์œ„ ๋ฒกํ„ฐ๋ฅผ ํ†ตํ•ด์„œ 1์˜ ๊ฐ’์„ ๊ฐ€์ง„ ์œ„์น˜์˜ ์—ด์„ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์—์„œ ๋งˆ์Šคํ‚น ํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๋ฒกํ„ฐ๋ฅผ ์Šค์ผ€์ผ๋“œ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋ฉด, ์Šค์ผ€์ผ๋“œ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์—์„œ๋Š” ์œ„ ๋ฒกํ„ฐ์—๋‹ค๊ฐ€ ๋งค์šฐ ์ž‘์€ ์Œ์ˆ˜ ๊ฐ’์ธ -1e9๋ฅผ ๊ณฑํ•˜๊ณ , ์ด๋ฅผ ํ–‰๋ ฌ์— ๋”ํ•ด์ฃผ์–ด ํ•ด๋‹น ์—ด์„ ์ „๋ถ€ ๋งˆ์Šคํ‚น ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์„ ๊ตฌํ˜„ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์•ž์„œ ์ธ์ฝ”๋”๋Š” ๋‘ ๊ฐœ์˜ ์„œ๋ธŒ ์„œ๋ธŒ์ธต(sublayer)์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง„๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ์ ์ด ์žˆ๋Š”๋ฐ, ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ํฌ์ง€์…˜-์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 8. ํฌ์ง€์…˜-์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง(Position-wise FFNN) ์ง€๊ธˆ์€ ์ธ์ฝ”๋”๋ฅผ ์„ค๋ช…ํ•˜๊ณ  ์žˆ์ง€๋งŒ, ํฌ์ง€์…˜ ์™€์ด์ฆˆ FFNN์€ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„œ๋ธŒ์ธต์ž…๋‹ˆ๋‹ค. ํฌ์ง€์…˜-์™€์ด์ฆˆ FFNN๋Š” ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ์™„์ „ ์—ฐ๊ฒฐ FFNN(Fully-connected FFNN)์ด๋ผ๊ณ  ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ๊ฒฐ๊ตญ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ์Œ์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ํฌ์ง€์…˜ ์™€์ด์ฆˆ FFNN์˜ ์ˆ˜์‹์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. F N ( ) M X ( , W + 1 ) 2 b ์‹์„ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์•ž์„œ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ( seq_len d o e) ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ 1 ( m d l d f ) ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ , ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ 2 ( f , d o e) ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ ์€๋‹‰์ธต์˜ ํฌ๊ธฐ์ธ f๋Š” ์•ž์„œ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•  ๋•Œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด 2,048์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ 1 b, 2 b๋Š” ํ•˜๋‚˜์˜ ์ธ์ฝ”๋” ์ธต ๋‚ด์—์„œ๋Š” ๋‹ค๋ฅธ ๋ฌธ์žฅ, ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค๋งˆ๋‹ค ์ •ํ™•ํ•˜๊ฒŒ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ธ์ฝ”๋” ์ธต๋งˆ๋‹ค๋Š” ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์ขŒ์ธก์€ ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์„ ๋ฒกํ„ฐ ๋‹จ์œ„๋กœ ๋ดค์„ ๋•Œ, ๊ฐ ๋ฒกํ„ฐ๋“ค์ด ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜ ์ธต์ด๋ผ๋Š” ์ธ์ฝ”๋” ๋‚ด ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ ์ธต์„ ์ง€๋‚˜ FFNN์„ ํ†ต๊ณผํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋Š” ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ Position-wise FFNN์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์‹ค์ œ๋กœ๋Š” ๊ทธ๋ฆผ์˜ ์šฐ์ธก๊ณผ ๊ฐ™์ด ํ–‰๋ ฌ๋กœ ์—ฐ์‚ฐ๋˜๋Š”๋ฐ, ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์„ ์ง€๋‚œ ์ธ์ฝ”๋”์˜ ์ตœ์ข… ์ถœ๋ ฅ์€ ์—ฌ์ „ํžˆ ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์˜ ํฌ๊ธฐ์˜€๋˜ ( seq_len d o e) ์˜ ํฌ๊ธฐ๊ฐ€ ๋ณด์กด๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ธ์ฝ”๋” ์ธต์„ ์ง€๋‚œ ์ด ํ–‰๋ ฌ์€ ๋‹ค์Œ ์ธ์ฝ”๋” ์ธต์œผ๋กœ ์ „๋‹ฌ๋˜๊ณ , ๋‹ค์Œ ์ธต์—์„œ๋„ ๋™์ผํ•œ ์ธ์ฝ”๋” ์—ฐ์‚ฐ์ด ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ๋‹ค์Œ์˜ ์ฝ”๋“œ๋Š” ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋” ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. outputs = tf.keras.layers.Dense(units=dff, activation='relu')(attention) outputs = tf.keras.layers.Dense(units=d_model)(outputs) 9. ์ž”์ฐจ ์—ฐ๊ฒฐ(Residual connection)๊ณผ ์ธต ์ •๊ทœํ™”(Layer Normalization) ์ธ์ฝ”๋”์˜ ๋‘ ๊ฐœ์˜ ์„œ๋ธŒ์ธต์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜์˜€๋‹ค๋ฉด ์ธ์ฝ”๋”์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๊ฑฐ์˜ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค! ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๋‘ ๊ฐœ์˜ ์„œ๋ธŒ์ธต์„ ๊ฐ€์ง„ ์ธ์ฝ”๋”์— ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ๋ฐ”๋กœ Add & Norm์ž…๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํžˆ๋Š” ์ž”์ฐจ ์—ฐ๊ฒฐ(residual connection)๊ณผ ์ธต ์ •๊ทœํ™”(layer normalization)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์•ž์„œ Position-wise FFNN๋ฅผ ์„ค๋ช…ํ•  ๋•Œ ์‚ฌ์šฉํ•œ ์•ž์„  ๊ทธ๋ฆผ์—์„œ ํ™”์‚ดํ‘œ์™€ Add & Norm(์ž”์ฐจ ์—ฐ๊ฒฐ๊ณผ ์ •๊ทœํ™” ๊ณผ์ •)์„ ์ถ”๊ฐ€ํ•œ ๊ทธ๋ฆผ์ž…๋‹ˆ๋‹ค. ์ถ”๊ฐ€๋œ ํ™”์‚ดํ‘œ๋“ค์€ ์„œ๋ธŒ์ธต ์ด์ „์˜ ์ž…๋ ฅ์—์„œ ์‹œ์ž‘๋˜์–ด ์„œ๋ธŒ์ธต์˜ ์ถœ๋ ฅ ๋ถ€๋ถ„์„ ํ–ฅํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. ์ถ”๊ฐ€๋œ ํ™”์‚ดํ‘œ๊ฐ€ ์–ด๋–ค ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€๋Š” ์ž”์ฐจ ์—ฐ๊ฒฐ๊ณผ ์ธต ์ •๊ทœํ™”๋ฅผ ๋ฐฐ์šฐ๊ณ  ๋‚˜๋ฉด ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1) ์ž”์ฐจ ์—ฐ๊ฒฐ(Residual connection) ์ž”์ฐจ ์—ฐ๊ฒฐ(residual connection)์˜ ์˜๋ฏธ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์–ด๋–ค ํ•จ์ˆ˜ ( ) ์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ์ž…๋ ฅ ์™€์— ๋Œ€ํ•œ ์–ด๋–ค ํ•จ์ˆ˜ ( ) ์˜ ๊ฐ’์„ ๋”ํ•œ ํ•จ์ˆ˜ ( ) ์˜ ๊ตฌ์กฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์–ด๋–ค ํ•จ์ˆ˜ ( ) ๊ฐ€ ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋Š” ์„œ๋ธŒ์ธต์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ž”์ฐจ ์—ฐ๊ฒฐ์€ ์„œ๋ธŒ์ธต์˜ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ๋”ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ ์„œ๋ธŒ์ธต์˜ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์€ ๋™์ผํ•œ ์ฐจ์›์„ ๊ฐ–๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ์„œ๋ธŒ์ธต์˜ ์ž…๋ ฅ๊ณผ ์„œ๋ธŒ์ธต์˜ ์ถœ๋ ฅ์€ ๋ง์…ˆ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ ์œ„์˜ ์ธ์ฝ”๋” ๊ทธ๋ฆผ์—์„œ ๊ฐ ํ™”์‚ดํ‘œ๊ฐ€ ์„œ๋ธŒ์ธต์˜ ์ž…๋ ฅ์—์„œ ์ถœ๋ ฅ์œผ๋กœ ํ–ฅํ•˜๋„๋ก ๊ทธ๋ ค์กŒ๋˜ ์ด์œ ์ž…๋‹ˆ๋‹ค. ์ž”์ฐจ ์—ฐ๊ฒฐ์€ ์ปดํ“จํ„ฐ ๋น„์ „ ๋ถ„์•ผ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋ธ์˜ ํ•™์Šต์„ ๋•๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด + u l y r ( ) ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์„œ๋ธŒ์ธต์ด ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์ด์—ˆ๋‹ค๋ฉด ์ž”์ฐจ ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) x M l i h a A t n i n ( ) ์œ„ ๊ทธ๋ฆผ์€ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์˜ ์ž…๋ ฅ๊ณผ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋”ํ•ด์ง€๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ด€๋ จ ๋…ผ๋ฌธ : https://arxiv.org/pdf/1512.03385.pdf 2) ์ธต ์ •๊ทœํ™”(Layer Normalization) ์ž”์ฐจ ์—ฐ๊ฒฐ์„ ๊ฑฐ์นœ ๊ฒฐ๊ณผ๋Š” ์ด์–ด์„œ ์ธต ์ •๊ทœํ™” ๊ณผ์ •์„ ๊ฑฐ์น˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ž”์ฐจ ์—ฐ๊ฒฐ์˜ ์ž…๋ ฅ์„, ์ž”์ฐจ ์—ฐ๊ฒฐ๊ณผ ์ธต ์ •๊ทœํ™” ๋‘ ๊ฐ€์ง€ ์—ฐ์‚ฐ์„ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•œ ํ›„์˜ ๊ฒฐ๊ณผ ํ–‰๋ ฌ์„ N ์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ž”์ฐจ ์—ฐ๊ฒฐ ํ›„ ์ธต ์ •๊ทœํ™” ์—ฐ์‚ฐ์„ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. N L y r o m ( + u l y r ( ) ) ์ธต ์ •๊ทœํ™”๋ฅผ ํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์ธต ์ •๊ทœํ™”๋Š” ํ…์„œ์˜ ๋งˆ์ง€๋ง‰ ์ฐจ์›์— ๋Œ€ํ•ด์„œ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์„ ๊ตฌํ•˜๊ณ , ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ์ˆ˜์‹์„ ํ†ตํ•ด ๊ฐ’์„ ์ •๊ทœํ™”ํ•˜์—ฌ ํ•™์Šต์„ ๋•์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ…์„œ์˜ ๋งˆ์ง€๋ง‰ ์ฐจ์›์ด๋ž€ ๊ฒƒ์€ ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋Š” m d l ์ฐจ์›์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ m d l ์ฐจ์›์˜ ๋ฐฉํ–ฅ์„ ํ™”์‚ดํ‘œ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ธต ์ •๊ทœํ™”๋ฅผ ์œ„ํ•ด์„œ ์šฐ์„ , ํ™”์‚ดํ‘œ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ๊ฐ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ 2 ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ํ™”์‚ดํ‘œ ๋ฐฉํ–ฅ์˜ ๋ฒกํ„ฐ๋ฅผ i ๋ผ๊ณ  ๋ช…๋ช…ํ•ด ๋ด…์‹œ๋‹ค. ์ธต ์ •๊ทœํ™”๋ฅผ ์ˆ˜ํ–‰ํ•œ ํ›„์—๋Š” ๋ฒกํ„ฐ i l i ๋ผ๋Š” ๋ฒกํ„ฐ๋กœ ์ •๊ทœํ™”๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. n = a e N r ( i ) ์ธต ์ •๊ทœํ™”์˜ ์ˆ˜์‹์„ ์•Œ์•„๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ธต ์ •๊ทœํ™”๋ฅผ ๋‘ ๊ฐ€์ง€ ๊ณผ์ •์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์„ ํ†ตํ•œ ์ •๊ทœํ™”, ๋‘ ๋ฒˆ์งธ๋Š” ๊ฐ๋งˆ์™€ ๋ฒ ํƒ€๋ฅผ ๋„์ž…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ์„ , ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์„ ํ†ตํ•ด ๋ฒกํ„ฐ i ๋ฅผ ์ •๊ทœํ™” ํ•ด์ค๋‹ˆ๋‹ค. i ๋Š” ๋ฒกํ„ฐ์ธ ๋ฐ˜๋ฉด, ํ‰๊ท  i ๊ณผ ๋ถ„์‚ฐ i ์€ ์Šค์นผ๋ผ์ž…๋‹ˆ๋‹ค. ๋ฒกํ„ฐ i ์˜ ๊ฐ ์ฐจ์›์„๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, i k ๋Š” ๋‹ค์Œ์˜ ์ˆ˜์‹๊ณผ ๊ฐ™์ด ์ •๊ทœํ™” ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ฒกํ„ฐ i ์˜ ๊ฐ ์ฐจ์›์˜ ๊ฐ’์ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •๊ทœํ™” ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ^ , = i k ฮผ ฯƒ 2 ฯต (์ž…์‹ค๋ก )์€ ๋ถ„๋ชจ๊ฐ€ 0์ด ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด์ œ (๊ฐ๋งˆ)์™€ (๋ฒ ํƒ€)๋ผ๋Š” ๋ฒกํ„ฐ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์ด๋“ค์˜ ์ดˆ๊นƒ๊ฐ’์€ ๊ฐ๊ฐ 1๊ณผ 0์ž…๋‹ˆ๋‹ค. ์™€๋ฅผ ๋„์ž…ํ•œ ์ธต ์ •๊ทœํ™”์˜ ์ตœ์ข… ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ ์™€๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. n = x i ฮฒ L y r o m ( i ) ๊ด€๋ จ ๋…ผ๋ฌธ : https://arxiv.org/pdf/1607.06450.pdf ์ผ€๋ผ์Šค์—์„œ๋Š” ์ธต ์ •๊ทœํ™”๋ฅผ ์œ„ํ•œ LayerNormalization()๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์ด๋ฅผ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 10. ์ธ์ฝ”๋” ๊ตฌํ˜„ํ•˜๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ธ์ฝ”๋”๋ฅผ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด๊ฐ€๋Š” ๋ฌธ์žฅ์—๋Š” ํŒจ๋”ฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์–ดํ…์…˜ ์‹œ ํŒจ๋”ฉ ํ† ํฐ์„ ์ œ์™ธํ•˜๋„๋ก ํŒจ๋”ฉ ๋งˆ์Šคํฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” MultiHeadAttention ํ•จ์ˆ˜์˜ mask์˜ ์ธ์ž ๊ฐ’์œผ๋กœ padding_mask๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค. ์ธ์ฝ”๋”๋Š” ์ด ๋‘ ๊ฐœ์˜ ์„œ๋ธŒ์ธต์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋Š”๋ฐ, ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜๊ณผ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ๊ฐ ์„œ๋ธŒ์ธต ์ดํ›„์—๋Š” ๋“œ๋กญ์•„์›ƒ, ์ž”์ฐจ ์—ฐ๊ฒฐ๊ณผ ์ธต ์ •๊ทœํ™”๊ฐ€ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. def encoder_layer(dff, d_model, num_heads, dropout, name="encoder_layer"): inputs = tf.keras.Input(shape=(None, d_model), name="inputs") # ์ธ์ฝ”๋”๋Š” ํŒจ๋”ฉ ๋งˆ์Šคํฌ ์‚ฌ์šฉ padding_mask = tf.keras.Input(shape=(1, 1, None), name="padding_mask") # ๋ฉ€ํ‹ฐ-ํ—ค๋“œ ์–ดํ…์…˜ (์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต / ์…€ํ”„ ์–ดํ…์…˜) attention = MultiHeadAttention( d_model, num_heads, name="attention")({ 'query': inputs, 'key': inputs, 'value': inputs, # Q = K = V 'mask': padding_mask # ํŒจ๋”ฉ ๋งˆ์Šคํฌ ์‚ฌ์šฉ }) # ๋“œ๋กญ์•„์›ƒ + ์ž”์ฐจ ์—ฐ๊ฒฐ๊ณผ ์ธต ์ •๊ทœํ™” attention = tf.keras.layers.Dropout(rate=dropout)(attention) attention = tf.keras.layers.LayerNormalization( epsilon=1e-6)(inputs + attention) # ํฌ์ง€์…˜ ์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง (๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต) outputs = tf.keras.layers.Dense(units=dff, activation='relu')(attention) outputs = tf.keras.layers.Dense(units=d_model)(outputs) # ๋“œ๋กญ์•„์›ƒ + ์ž”์ฐจ ์—ฐ๊ฒฐ๊ณผ ์ธต ์ •๊ทœํ™” outputs = tf.keras.layers.Dropout(rate=dropout)(outputs) outputs = tf.keras.layers.LayerNormalization( epsilon=1e-6)(attention + outputs) return tf.keras.Model( inputs=[inputs, padding_mask], outputs=outputs, name=name) ์œ„ ์ฝ”๋“œ๋Š” ํ•˜๋‚˜์˜ ์ธ์ฝ”๋” ๋ธ”๋ก. ์ฆ‰, ํ•˜๋‚˜์˜ ์ธ์ฝ”๋” ์ธต์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ํŠธ๋žœ์Šคํฌ๋จธ๋Š” num_layers ๊ฐœ์ˆ˜๋งŒํผ์˜ ์ธ์ฝ”๋” ์ธต์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ด๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์Œ“๋Š” ์ฝ”๋“œ๋ฅผ ๋ณ„๋„ ๊ตฌํ˜„ํ•ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 11. ์ธ์ฝ”๋” ์Œ“๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ์ธ์ฝ”๋” ์ธต์˜ ๋‚ด๋ถ€ ์•„ํ‚คํ…์ฒ˜์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ธ์ฝ”๋” ์ธต์„ num_layers ๊ฐœ๋งŒํผ ์Œ“๊ณ , ๋งˆ์ง€๋ง‰ ์ธ์ฝ”๋” ์ธต์—์„œ ์–ป๋Š” (seq_len, d_model) ํฌ๊ธฐ์˜ ํ–‰๋ ฌ์„ ๋””์ฝ”๋”๋กœ ๋ณด๋‚ด์ฃผ๋ฏ€๋กœ์„œ ํŠธ๋žœ์Šคํฌ๋จธ ์ธ์ฝ”๋”์˜ ์ธ์ฝ”๋”ฉ ์—ฐ์‚ฐ์ด ๋๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์ธ์ฝ”๋” ์ธต์„ num_layers ๊ฐœ๋งŒํผ ์Œ“๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. def encoder(vocab_size, num_layers, dff, d_model, num_heads, dropout, name="encoder"): inputs = tf.keras.Input(shape=(None,), name="inputs") # ์ธ์ฝ”๋”๋Š” ํŒจ๋”ฉ ๋งˆ์Šคํฌ ์‚ฌ์šฉ padding_mask = tf.keras.Input(shape=(1, 1, None), name="padding_mask") # ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ + ๋“œ๋กญ์•„์›ƒ embeddings = tf.keras.layers.Embedding(vocab_size, d_model)(inputs) embeddings *= tf.math.sqrt(tf.cast(d_model, tf.float32)) embeddings = PositionalEncoding(vocab_size, d_model)(embeddings) outputs = tf.keras.layers.Dropout(rate=dropout)(embeddings) # ์ธ์ฝ”๋”๋ฅผ num_layers ๊ฐœ ์Œ“๊ธฐ for i in range(num_layers): outputs = encoder_layer(dff=dff, d_model=d_model, num_heads=num_heads, dropout=dropout, name="encoder_layer_{}".format(i), )([outputs, padding_mask]) return tf.keras.Model( inputs=[inputs, padding_mask], outputs=outputs, name=name) 12. ์ธ์ฝ”๋”์—์„œ ๋””์ฝ”๋”๋กœ(From Encoder To Decoder) ์ง€๊ธˆ๊นŒ์ง€ ์ธ์ฝ”๋”์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„๋œ ์ธ์ฝ”๋”๋Š” ์ด num_layers ๋งŒํผ์˜ ์ธต ์—ฐ์‚ฐ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ํ•œ ํ›„์— ๋งˆ์ง€๋ง‰ ์ธต์˜ ์ธ์ฝ”๋”์˜ ์ถœ๋ ฅ์„ ๋””์ฝ”๋”์—๊ฒŒ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋” ์—ฐ์‚ฐ์ด ๋๋‚ฌ์œผ๋ฉด ๋””์ฝ”๋” ์—ฐ์‚ฐ์ด ์‹œ์ž‘๋˜์–ด ๋””์ฝ”๋” ๋˜ํ•œ num_layers ๋งŒํผ์˜ ์—ฐ์‚ฐ์„ ํ•˜๋Š”๋ฐ, ์ด๋•Œ๋งˆ๋‹ค ์ธ์ฝ”๋”๊ฐ€ ๋ณด๋‚ธ ์ถœ๋ ฅ์„ ๊ฐ ๋””์ฝ”๋” ์ธต ์—ฐ์‚ฐ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋”์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 13. ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต : ์…€ํ”„ ์–ดํ…์…˜๊ณผ ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋””์ฝ”๋”๋„ ์ธ์ฝ”๋”์™€ ๋™์ผํ•˜๊ฒŒ ์ž„๋ฒ ๋”ฉ ์ธต๊ณผ ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ์„ ๊ฑฐ์นœ ํ›„์˜ ๋ฌธ์žฅ ํ–‰๋ ฌ์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ ๋˜ํ•œ seq2seq์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ต์‚ฌ ๊ฐ•์š”(Teacher Forcing)์„ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ๋˜๋ฏ€๋กœ ํ•™์Šต ๊ณผ์ •์—์„œ ๋””์ฝ”๋”๋Š” ๋ฒˆ์—ญํ•  ๋ฌธ์žฅ์— ํ•ด๋‹น๋˜๋Š” <sos> je suis รฉtudiant์˜ ๋ฌธ์žฅ ํ–‰๋ ฌ์„ ํ•œ ๋ฒˆ์— ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋””์ฝ”๋”๋Š” ์ด ๋ฌธ์žฅ ํ–‰๋ ฌ๋กœ๋ถ€ํ„ฐ ๊ฐ ์‹œ์ ์˜ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ํ›ˆ๋ จ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. seq2seq์˜ ๋””์ฝ”๋”์— ์‚ฌ์šฉ๋˜๋Š” RNN ๊ณ„์—ด์˜ ์‹ ๊ฒฝ๋ง์€ ์ž…๋ ฅ ๋‹จ์–ด๋ฅผ ๋งค ์‹œ์ ๋งˆ๋‹ค ์ˆœ์ฐจ์ ์œผ๋กœ ์ž…๋ ฅ๋ฐ›์œผ๋ฏ€๋กœ ๋‹ค์Œ ๋‹จ์–ด ์˜ˆ์ธก์— ํ˜„์žฌ ์‹œ์ ์„ ํฌํ•จํ•œ ์ด์ „ ์‹œ์ ์— ์ž…๋ ฅ๋œ ๋‹จ์–ด๋“ค๋งŒ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ๋ฌธ์žฅ ํ–‰๋ ฌ๋กœ ์ž…๋ ฅ์„ ํ•œ ๋ฒˆ์— ๋ฐ›์œผ๋ฏ€๋กœ ํ˜„์žฌ ์‹œ์ ์˜ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ณ ์ž ํ•  ๋•Œ, ์ž…๋ ฅ ๋ฌธ์žฅ ํ–‰๋ ฌ๋กœ๋ถ€ํ„ฐ ๋ฏธ๋ž˜ ์‹œ์ ์˜ ๋‹จ์–ด๊นŒ์ง€๋„ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, suis๋ฅผ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ์‹œ์ ์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. RNN ๊ณ„์—ด์˜ seq2seq์˜ ๋””์ฝ”๋”๋ผ๋ฉด ํ˜„์žฌ๊นŒ์ง€ ๋””์ฝ”๋”์— ์ž…๋ ฅ๋œ ๋‹จ์–ด๋Š” <sos>์™€ je๋ฟ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ์ด๋ฏธ ๋ฌธ์žฅ ํ–‰๋ ฌ๋กœ <sos> je suis รฉtudiant๋ฅผ ์ž…๋ ฅ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋””์ฝ”๋”์—์„œ๋Š” ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก์—์„œ ํ˜„์žฌ ์‹œ์ ๋ณด๋‹ค ๋ฏธ๋ž˜์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ์ฐธ๊ณ ํ•˜์ง€ ๋ชปํ•˜๋„๋ก ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ(look-ahead mask)๋ฅผ ๋„์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ง์—ญํ•˜๋ฉด '๋ฏธ๋ฆฌ ๋ณด๊ธฐ์— ๋Œ€ํ•œ ๋งˆ์Šคํฌ'์ž…๋‹ˆ๋‹ค. ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ(look-ahead mask)๋Š” ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์—์„œ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์…€ํ”„ ์–ดํ…์…˜ ์ธต์€ ์ธ์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์…€ํ”„ ์–ดํ…์…˜ ์ธต๊ณผ ๋™์ผํ•œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ค์ง ๋‹ค๋ฅธ ์ ์€ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์—์„œ ๋งˆ์Šคํ‚น์„ ์ ์šฉํ•œ๋‹ค๋Š” ์ ๋งŒ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์šฐ์„  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์…€ํ”„ ์–ดํ…์…˜์„ ํ†ตํ•ด ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์„ ์–ป์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ž๊ธฐ ์ž์‹ ๋ณด๋‹ค ๋ฏธ๋ž˜์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์€ ์ฐธ๊ณ ํ•˜์ง€ ๋ชปํ•˜๋„๋ก ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งˆ์Šคํ‚น ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์Šคํ‚น ๋œ ํ›„์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์˜ ๊ฐ ํ–‰์„ ๋ณด๋ฉด ์ž๊ธฐ ์ž์‹ ๊ณผ ๊ทธ ์ด์ „ ๋‹จ์–ด๋“ค๋งŒ์„ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋Š” ๊ทผ๋ณธ์ ์œผ๋กœ ์…€ํ”„ ์–ดํ…์…˜์ด๋ผ๋Š” ์ ๊ณผ, ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ์ ์—์„œ ์ธ์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ์˜ ๊ตฌํ˜„์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค. ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ๋Š” ํŒจ๋”ฉ ๋งˆ์Šคํฌ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์•ž์„œ ๊ตฌํ˜„ํ•œ ์Šค์ผ€์ผ๋“œ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜ ํ•จ์ˆ˜์— mask๋ผ๋Š” ์ธ์ž๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ํŒจ๋”ฉ ๋งˆ์Šคํ‚น์„ ์จ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์Šค์ผ€์ผ๋“œ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜ ํ•จ์ˆ˜์— ํŒจ๋”ฉ ๋งˆ์Šคํฌ๋ฅผ ์ „๋‹ฌํ•˜๊ณ , ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํ‚น์„ ์จ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์Šค์ผ€์ผ๋“œ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜ ํ•จ์ˆ˜์— ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. # ์Šค์ผ€์ผ๋“œ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜ ํ•จ์ˆ˜๋ฅผ ๋‹ค์‹œ ๋ณต์Šตํ•ด ๋ด…์‹œ๋‹ค. def scaled_dot_product_attention(query, key, value, mask): ... ์ค‘๋žต ... logits += (mask * -1e9) # ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์ธ logits์— mask*-1e9 ๊ฐ’์„ ๋”ํ•ด์ฃผ๊ณ  ์žˆ๋‹ค. ... ์ค‘๋žต ... ํŠธ๋žœ์Šคํฌ๋จธ์—๋Š” ์ด ์„ธ ๊ฐ€์ง€ ์–ดํ…์…˜์ด ์กด์žฌํ•˜๋ฉฐ, ๋ชจ๋‘ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์Šค์ผ€์ผ๋“œ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ๊ฐ ์–ดํ…์…˜ ์‹œ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๋Š” ๋งˆ์Šคํ‚น์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์˜ ์…€ํ”„ ์–ดํ…์…˜ : ํŒจ๋”ฉ ๋งˆ์Šคํฌ๋ฅผ ์ „๋‹ฌ ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ๋งˆ์Šคํฌ ๋“œ ์…€ํ”„ ์–ดํ…์…˜ : ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ๋ฅผ ์ „๋‹ฌ <-- ์ง€๊ธˆ ์„ค๋ช…ํ•˜๊ณ  ์žˆ์Œ. ๋””์ฝ”๋”์˜ ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ์ธ์ฝ”๋”-๋””์ฝ”๋” ์–ดํ…์…˜ : ํŒจ๋”ฉ ๋งˆ์Šคํฌ๋ฅผ ์ „๋‹ฌ ์ด๋•Œ ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ๋ฅผ ํ•œ๋‹ค๊ณ  ํ•ด์„œ ํŒจ๋”ฉ ๋งˆ์Šคํฌ๊ฐ€ ๋ถˆํ•„์š”ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ฏ€๋กœ ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ๋Š” ํŒจ๋”ฉ ๋งˆ์Šคํฌ๋ฅผ ํฌํ•จํ•˜๋„๋ก ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํŒจ๋”ฉ ๋งˆ์Šคํฌ ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋งˆ์Šคํ‚น์„ ํ•˜๊ณ ์ž ํ•˜๋Š” ์œ„์น˜์—๋Š” 1์„, ๋งˆ์Šคํ‚น์„ ํ•˜์ง€ ์•Š๋Š” ์œ„์น˜์—๋Š” 0์„ ๋ฆฌํ„ดํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. # ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต(sublayer)์—์„œ ๋ฏธ๋ž˜ ํ† ํฐ์„ Mask ํ•˜๋Š” ํ•จ์ˆ˜ def create_look_ahead_mask(x): seq_len = tf.shape(x)[1] look_ahead_mask = 1 - tf.linalg.band_part(tf.ones((seq_len, seq_len)), -1, 0) padding_mask = create_padding_mask(x) # ํŒจ๋”ฉ ๋งˆ์Šคํฌ๋„ ํฌํ•จ return tf.maximum(look_ahead_mask, padding_mask) ์ž„์˜์˜ ์ •์ˆ˜ ์‹œํ€€์Šค ์ž…๋ ฅ์„ ๋„ฃ์–ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ด…์‹œ๋‹ค. ํŒจ๋”ฉ ๋งˆ์Šคํฌ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ์„ธ ๋ฒˆ์งธ ์œ„์น˜์— ์ •์ˆ˜ 0์„ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค. print(create_look_ahead_mask(tf.constant([[1, 2, 0, 4, 5]]))) tf.Tensor( [[[[0. 1. 1. 1. 1.] [0. 0. 1. 1. 1.] [0. 0. 1. 1. 1.] [0. 0. 1. 0. 1.] [0. 0. 1. 0. 0.]]]], shape=(1, 1, 5, 5), dtype=float32) ๋ฃฉ-์–ดํ—ค๋“œ ๋งˆ์Šคํฌ์ด๋ฏ€๋กœ ์‚ผ๊ฐํ˜• ๋ชจ์–‘์˜ ๋งˆ์Šคํ‚น์ด ํ˜•์„ฑ๋˜๋ฉด์„œ ํŒจ๋”ฉ ๋งˆ์Šคํฌ๊ฐ€ ํฌํ•จ๋ผ ์žˆ๋Š” ์„ธ ๋ฒˆ์งธ ์—ด์ด ๋งˆ์Šคํ‚น ๋ฉ๋‹ˆ๋‹ค. 14. ๋””์ฝ”๋”์˜ ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต : ์ธ์ฝ”๋”-๋””์ฝ”๋” ์–ดํ…์…˜ ๋””์ฝ”๋”์˜ ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ๋””์ฝ”๋”์˜ ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์€ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ์ ์—์„œ๋Š” ์ด์ „์˜ ์–ดํ…์…˜๋“ค(์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต)๊ณผ๋Š” ๊ณตํ†ต์ ์ด ์žˆ์œผ๋‚˜ ์ด๋ฒˆ์—๋Š” ์…€ํ”„ ์–ดํ…์…˜์ด ์•„๋‹™๋‹ˆ๋‹ค. ์…€ํ”„ ์–ดํ…์…˜์€ Query, Key, Value๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ๋งํ•˜๋Š”๋ฐ, ์ธ์ฝ”๋”-๋””์ฝ”๋” ์–ดํ…์…˜์€ Query๊ฐ€ ๋””์ฝ”๋”์ธ ํ–‰๋ ฌ์ธ ๋ฐ˜๋ฉด, Key์™€ Value๋Š” ์ธ์ฝ”๋” ํ–‰๋ ฌ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ฐ ์„œ๋ธŒ์ธต์—์„œ์˜ Q, K, V์˜ ๊ด€๊ณ„๋ฅผ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. ์ธ์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต : Query = Key = Value ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต : Query = Key = Value ๋””์ฝ”๋”์˜ ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต : Query : ๋””์ฝ”๋” ํ–‰๋ ฌ / Key = Value : ์ธ์ฝ”๋” ํ–‰๋ ฌ ๋””์ฝ”๋”์˜ ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์„ ํ™•๋Œ€ํ•ด ๋ณด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ธ์ฝ”๋”๋กœ๋ถ€ํ„ฐ ๋‘ ๊ฐœ์˜ ํ™”์‚ดํ‘œ๊ฐ€ ๊ทธ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ํ™”์‚ดํ‘œ๋Š” ๊ฐ๊ฐ Key์™€ Value๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ ์ด๋Š” ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์ธต์—์„œ ์˜จ ํ–‰๋ ฌ๋กœ๋ถ€ํ„ฐ ์–ป์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด Query๋Š” ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์˜ ๊ฒฐ๊ณผ ํ–‰๋ ฌ๋กœ๋ถ€ํ„ฐ ์–ป๋Š”๋‹ค๋Š” ์ ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. Query๊ฐ€ ๋””์ฝ”๋” ํ–‰๋ ฌ, Key๊ฐ€ ์ธ์ฝ”๋” ํ–‰๋ ฌ์ผ ๋•Œ, ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ–‰๋ ฌ์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์— ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ณผ์ •์€ ๋‹ค๋ฅธ ์–ดํ…์…˜๋“ค๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 15. ๋””์ฝ”๋” ๊ตฌํ˜„ํ•˜๊ธฐ ๋””์ฝ”๋”๋Š” ์ด ์„ธ ๊ฐœ์˜ ์„œ๋ธŒ์ธต์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต ๋ชจ๋‘ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์ด์ง€๋งŒ, ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์€ mask์˜ ์ธ์ž ๊ฐ’์œผ๋กœ look_ahead_mask๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๋ฐ˜๋ฉด, ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์€ mask์˜ ์ธ์ž ๊ฐ’์œผ๋กœ padding_mask๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์€ ๋งˆ์Šคํฌ ๋“œ ์…€ํ”„ ์–ดํ…์…˜์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์„ธ ๊ฐœ์˜ ์„œ๋ธŒ์ธต ๋ชจ๋‘ ์„œ๋ธŒ์ธต ์—ฐ์‚ฐ ํ›„์—๋Š” ๋“œ๋กญ์•„์›ƒ, ์ž”์ฐจ ์—ฐ๊ฒฐ, ์ธต ์ •๊ทœํ™”๊ฐ€ ์ˆ˜ํ–‰๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. def decoder_layer(dff, d_model, num_heads, dropout, name="decoder_layer"): inputs = tf.keras.Input(shape=(None, d_model), name="inputs") enc_outputs = tf.keras.Input(shape=(None, d_model), name="encoder_outputs") # ๋ฃฉ ์–ดํ—ค๋“œ ๋งˆ์Šคํฌ(์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต) look_ahead_mask = tf.keras.Input( shape=(1, None, None), name="look_ahead_mask") # ํŒจ๋”ฉ ๋งˆ์Šคํฌ(๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต) padding_mask = tf.keras.Input(shape=(1, 1, None), name='padding_mask') # ๋ฉ€ํ‹ฐ-ํ—ค๋“œ ์–ดํ…์…˜ (์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต / ๋งˆ์Šคํฌ ๋“œ ์…€ํ”„ ์–ดํ…์…˜) attention1 = MultiHeadAttention( d_model, num_heads, name="attention_1")(inputs={ 'query': inputs, 'key': inputs, 'value': inputs, # Q = K = V 'mask': look_ahead_mask # ๋ฃฉ ์–ดํ—ค๋“œ ๋งˆ์Šคํฌ }) # ์ž”์ฐจ ์—ฐ๊ฒฐ๊ณผ ์ธต ์ •๊ทœํ™” attention1 = tf.keras.layers.LayerNormalization( epsilon=1e-6)(attention1 + inputs) # ๋ฉ€ํ‹ฐ-ํ—ค๋“œ ์–ดํ…์…˜ (๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต / ๋””์ฝ”๋”-์ธ์ฝ”๋” ์–ดํ…์…˜) attention2 = MultiHeadAttention( d_model, num_heads, name="attention_2")(inputs={ 'query': attention1, 'key': enc_outputs, 'value': enc_outputs, # Q != K = V 'mask': padding_mask # ํŒจ๋”ฉ ๋งˆ์Šคํฌ }) # ๋“œ๋กญ์•„์›ƒ + ์ž”์ฐจ ์—ฐ๊ฒฐ๊ณผ ์ธต ์ •๊ทœํ™” attention2 = tf.keras.layers.Dropout(rate=dropout)(attention2) attention2 = tf.keras.layers.LayerNormalization( epsilon=1e-6)(attention2 + attention1) # ํฌ์ง€์…˜ ์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง (์„ธ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต) outputs = tf.keras.layers.Dense(units=dff, activation='relu')(attention2) outputs = tf.keras.layers.Dense(units=d_model)(outputs) # ๋“œ๋กญ์•„์›ƒ + ์ž”์ฐจ ์—ฐ๊ฒฐ๊ณผ ์ธต ์ •๊ทœํ™” outputs = tf.keras.layers.Dropout(rate=dropout)(outputs) outputs = tf.keras.layers.LayerNormalization( epsilon=1e-6)(outputs + attention2) return tf.keras.Model( inputs=[inputs, enc_outputs, look_ahead_mask, padding_mask], outputs=outputs, name=name) ์ธ์ฝ”๋”์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋””์ฝ”๋”๋„ num_layers ๊ฐœ๋งŒํผ ์Œ“๋Š” ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. 16. ๋””์ฝ”๋” ์Œ“๊ธฐ ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ ํ›„ ๋””์ฝ”๋” ์ธต์„ num_layers์˜ ๊ฐœ์ˆ˜๋งŒํผ ์Œ“๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. def decoder(vocab_size, num_layers, dff, d_model, num_heads, dropout, name='decoder'): inputs = tf.keras.Input(shape=(None,), name='inputs') enc_outputs = tf.keras.Input(shape=(None, d_model), name='encoder_outputs') # ๋””์ฝ”๋”๋Š” ๋ฃฉ ์–ดํ—ค๋“œ ๋งˆ์Šคํฌ(์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต)์™€ ํŒจ๋”ฉ ๋งˆ์Šคํฌ(๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต) ๋‘˜ ๋‹ค ์‚ฌ์šฉ. look_ahead_mask = tf.keras.Input( shape=(1, None, None), name='look_ahead_mask') padding_mask = tf.keras.Input(shape=(1, 1, None), name='padding_mask') # ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ + ๋“œ๋กญ์•„์›ƒ embeddings = tf.keras.layers.Embedding(vocab_size, d_model)(inputs) embeddings *= tf.math.sqrt(tf.cast(d_model, tf.float32)) embeddings = PositionalEncoding(vocab_size, d_model)(embeddings) outputs = tf.keras.layers.Dropout(rate=dropout)(embeddings) # ๋””์ฝ”๋”๋ฅผ num_layers ๊ฐœ ์Œ“๊ธฐ for i in range(num_layers): outputs = decoder_layer(dff=dff, d_model=d_model, num_heads=num_heads, dropout=dropout, name='decoder_layer_{}'.format(i), )(inputs=[outputs, enc_outputs, look_ahead_mask, padding_mask]) return tf.keras.Model( inputs=[inputs, enc_outputs, look_ahead_mask, padding_mask], outputs=outputs, name=name) 17. ํŠธ๋žœ์Šคํฌ๋จธ ๊ตฌํ˜„ํ•˜๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ๊ตฌํ˜„ํ•œ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋” ํ•จ์ˆ˜๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์กฐ๋ฆฝํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์˜ ์ถœ๋ ฅ์€ ๋””์ฝ”๋”์—์„œ ์ธ์ฝ”๋”-๋””์ฝ”๋” ์–ดํ…์…˜์—์„œ ์‚ฌ์šฉ๋˜๊ธฐ ์œ„ํ•ด ๋””์ฝ”๋”๋กœ ์ „๋‹ฌํ•ด ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋””์ฝ”๋”์˜ ๋๋‹จ์—๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋„๋ก, vocab_size ๋งŒํผ์˜ ๋‰ด๋Ÿฐ์„ ๊ฐ€์ง€๋Š” ์ถœ๋ ฅ์ธต์„ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค. def transformer(vocab_size, num_layers, dff, d_model, num_heads, dropout, name="transformer"): # ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ inputs = tf.keras.Input(shape=(None,), name="inputs") # ๋””์ฝ”๋”์˜ ์ž…๋ ฅ dec_inputs = tf.keras.Input(shape=(None,), name="dec_inputs") # ์ธ์ฝ”๋”์˜ ํŒจ๋”ฉ ๋งˆ์Šคํฌ enc_padding_mask = tf.keras.layers.Lambda( create_padding_mask, output_shape=(1, 1, None), name='enc_padding_mask')(inputs) # ๋””์ฝ”๋”์˜ ๋ฃฉ ์–ดํ—ค๋“œ ๋งˆ์Šคํฌ(์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต) look_ahead_mask = tf.keras.layers.Lambda( create_look_ahead_mask, output_shape=(1, None, None), name='look_ahead_mask')(dec_inputs) # ๋””์ฝ”๋”์˜ ํŒจ๋”ฉ ๋งˆ์Šคํฌ(๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต) dec_padding_mask = tf.keras.layers.Lambda( create_padding_mask, output_shape=(1, 1, None), name='dec_padding_mask')(inputs) # ์ธ์ฝ”๋”์˜ ์ถœ๋ ฅ์€ enc_outputs. ๋””์ฝ”๋”๋กœ ์ „๋‹ฌ๋œ๋‹ค. enc_outputs = encoder(vocab_size=vocab_size, num_layers=num_layers, dff=dff, d_model=d_model, num_heads=num_heads, dropout=dropout, )(inputs=[inputs, enc_padding_mask]) # ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์€ ์ž…๋ ฅ ๋ฌธ์žฅ๊ณผ ํŒจ๋”ฉ ๋งˆ์Šคํฌ # ๋””์ฝ”๋”์˜ ์ถœ๋ ฅ์€ dec_outputs. ์ถœ๋ ฅ์ธต์œผ๋กœ ์ „๋‹ฌ๋œ๋‹ค. dec_outputs = decoder(vocab_size=vocab_size, num_layers=num_layers, dff=dff, d_model=d_model, num_heads=num_heads, dropout=dropout, )(inputs=[dec_inputs, enc_outputs, look_ahead_mask, dec_padding_mask]) # ๋‹ค์Œ ๋‹จ์–ด ์˜ˆ์ธก์„ ์œ„ํ•œ ์ถœ๋ ฅ์ธต outputs = tf.keras.layers.Dense(units=vocab_size, name="outputs")(dec_outputs) return tf.keras.Model(inputs=[inputs, dec_inputs], outputs=outputs, name=name) 18. ํŠธ๋žœ์Šคํฌ๋จธ ํ•˜์ด ํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์ •ํ•˜๊ธฐ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ž„์˜๋กœ ์ •ํ•˜๊ณ  ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ํ˜„์žฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” ์ž„์˜๋กœ 9,000์œผ๋กœ ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋กœ๋ถ€ํ„ฐ ๋ฃฉ์—… ํ…Œ์ด๋ธ”์„ ์ˆ˜ํ–‰ํ•  ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”๊ณผ ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ ํ–‰๋ ฌ์˜ ํ–‰์˜ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ ์ œ์‹œํ•œ ๊ฒƒ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ์ธต์˜ ๊ฐœ์ˆ˜ num_layers ๋Š” 4๊ฐœ, ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ํฌ์ง€์…˜ ์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์˜ ์€๋‹‰์ธต f ์€ 128, ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ์ž…, ์ถœ๋ ฅ์˜ ์ฐจ์› m d l ์€ 128, ๋ฉ€ํ‹ฐ-ํ—ค๋“œ ์–ดํ…์…˜์—์„œ ๋ณ‘๋ ฌ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ํ—ค๋“œ์˜ ์ˆ˜ num_heads ๋Š” 4๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. 128/4์˜ ๊ฐ’์ธ 32๊ฐ€ v ์˜ ๊ฐ’์ด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. small_transformer = transformer( vocab_size = 9000, num_layers = 4, dff = 512, d_model = 128, num_heads = 4, dropout = 0.3, name="small_transformer") tf.keras.utils.plot_model( small_transformer, to_file='small_transformer.png', show_shapes=True) 19. ์†์‹ค ํ•จ์ˆ˜ ์ •์˜ํ•˜๊ธฐ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ์˜ˆ์ •์ด๋ฏ€๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์†์‹ค ํ•จ์ˆ˜๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. def loss_function(y_true, y_pred): y_true = tf.reshape(y_true, shape=(-1, MAX_LENGTH - 1)) loss = tf.keras.losses.SparseCategoricalCrossentropy( from_logits=True, reduction='none')(y_true, y_pred) mask = tf.cast(tf.not_equal(y_true, 0), tf.float32) loss = tf.multiply(loss, mask) return tf.reduce_mean(loss) 20. ํ•™์Šต๋ฅ  ํ•™์Šต๋ฅ  ์Šค์ผ€์ค„๋Ÿฌ(Learning rate Scheduler)๋Š” ๋ฏธ๋ฆฌ ํ•™์Šต ์ผ์ •์„ ์ •ํ•ด๋‘๊ณ  ๊ทธ ์ผ์ •์— ๋”ฐ๋ผ ํ•™์Šต๋ฅ ์ด ์กฐ์ •๋˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•œ ๋‹จ๊ณ„๊นŒ์ง€๋Š” ํ•™์Šต๋ฅ ์„ ์ฆ๊ฐ€์‹œ์ผฐ๋‹ค๊ฐ€ ๋‹จ๊ณ„์— ์ด๋ฅด๋ฉด ํ•™์Šต๋ฅ ์„ ์ ์ฐจ์ ์œผ๋กœ ๋–จ์–ดํŠธ๋ฆฌ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ข€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ๋ด…์‹œ๋‹ค. step_num(๋‹จ๊ณ„)์ด๋ž€ ์˜ตํ‹ฐ๋งˆ์ด์ €๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ํ•œ ๋ฒˆ์˜ ์ง„ํ–‰ ํšŸ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋Š” warmup_steps์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ •ํ•˜๊ณ  step_num์ด warmup_steps๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ๋Š” ํ•™์Šต๋ฅ ์„ ์„ ํ˜•์ ์œผ๋กœ ์ฆ๊ฐ€์‹œํ‚ค๊ณ , step_num์ด warmup_steps์— ๋„๋‹ฌํ•˜๊ฒŒ ๋˜๋ฉด ํ•™์Šต๋ฅ ์„ step_num์˜ ์—ญ ์ œ๊ณฑ๊ทผ์— ๋”ฐ๋ผ์„œ ๊ฐ์†Œ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. warmup_steps์˜ ๊ฐ’์œผ๋กœ๋Š” 4,000์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. r t = m d l 0.5 m n ( step_num 0.5 step_num class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule): def __init__(self, d_model, warmup_steps=4000): super(CustomSchedule, self).__init__() self.d_model = d_model self.d_model = tf.cast(self.d_model, tf.float32) self.warmup_steps = warmup_steps def __call__(self, step): step = tf.cast(step, tf.float32) arg1 = tf.math.rsqrt(step) arg2 = step * (self.warmup_steps**-1.5) return tf.math.rsqrt(self.d_model) * tf.math.minimum(arg1, arg2) ํ•™์Šต๋ฅ ์˜ ๋ณ€ํ™”๋ฅผ ์‹œ๊ฐํ™”ํ•ด๋ด…์‹œ๋‹ค. sample_learning_rate = CustomSchedule(d_model=128) plt.plot(sample_learning_rate(tf.range(200000, dtype=tf.float32))) plt.ylabel("Learning Rate") plt.xlabel("Train Step") Text(0.5, 0, 'Train Step') ์—ฌ๊ธฐ์„œ ๊ตฌํ˜„ํ•œ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์„ ๋ฐ”ํƒ•์œผ๋กœ ๋‹ค์Œ ์‹ค์Šต์—์„œ ํ•œ๊ตญ์–ด ์ฑ—๋ด‡์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” ์–ธ๊ธ‰๋˜์ง€ ์•Š์€ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๊ตฌํ˜„ ์ด์•ผ๊ธฐ๊ฐ€ ๊ถ๊ธˆํ•˜์‹œ๋‹ค๋ฉด ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. ๋งํฌ : https://tunz.kr/post/4 16-02 ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ด์šฉํ•œ ํ•œ๊ตญ์–ด ์ฑ—๋ด‡(Transformer Chatbot Tutorial) ์•ž์„œ ๊ตฌํ˜„ํ•œ ํŠธ๋žœ์Šคํฌ๋จธ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ผ์ƒ ๋Œ€ํ™” ์ฑ—๋ด‡์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์€ ๋ฐ”๋กœ ์ด์ „์˜ ํŠธ๋žœ์Šคํฌ๋จธ ์‹ค์Šต ์ฝ”๋“œ๋ฅผ ๋ชจ๋‘ ์‹คํ–‰ํ•˜์˜€๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฏ€๋กœ ์ด์ „ ํŠธ๋žœ์Šคํฌ๋จธ ์‹ค์Šต์„ ์ง„ํ–‰ ํ›„์— ์ด์–ด์„œ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ ์ฑ—๋ด‡ ์ „์ฒด ์ฝ”๋“œ๋Š” ์•„๋ž˜์˜ ๋งํฌ์— ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ๊นƒํ—ˆ๋ธŒ ๋งํฌ : https://github.com/ukairia777/tensorflow-transformer 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œํ•˜๊ธฐ import pandas as pd import numpy as np import matplotlib.pyplot as plt import re import urllib.request import time import tensorflow_datasets as tfds import tensorflow as tf ์ฑ—๋ด‡ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜์—ฌ ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/songys/Chatbot_data/master/ChatbotData.csv", filename="ChatBotData.csv") train_data = pd.read_csv('ChatBotData.csv') train_data.head() ์ด ๋ฐ์ดํ„ฐ๋Š” ์งˆ๋ฌธ(Q)๊ณผ ๋Œ€๋‹ต(A)์˜ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. print('์ฑ—๋ด‡ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ :', len(train_data)) ์ฑ—๋ด‡ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 11823 ์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋Š” 11,823๊ฐœ์ž…๋‹ˆ๋‹ค. ๋ถˆํ•„์š”ํ•œ Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(train_data.isnull().sum()) Q 0 A 0 label 0 dtype: int64 Null ๊ฐ’์€ ๋ณ„๋„๋กœ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ† ํฐํ™”๋ฅผ ์œ„ํ•ด ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ธ ํ•™์Šต ๊ธฐ๋ฐ˜์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์› ๋ฐ์ดํ„ฐ์—์„œ ?, ., !์™€ ๊ฐ™์€ ๊ตฌ๋‘์ ์„ ๋ฏธ๋ฆฌ ์ฒ˜๋ฆฌํ•ด๋‘์–ด์•ผ ํ•˜๋Š”๋ฐ, ๊ตฌ๋‘์ ๋“ค์„ ๋‹จ์ˆœํžˆ ์ œ๊ฑฐํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ๊ตฌ๋‘์  ์•ž์— ๊ณต๋ฐฑ. ์ฆ‰, ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋‹ค๋ฅธ ๋ฌธ์ž๋“ค๊ณผ ๊ตฌ๋ถ„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, '12์‹œ ๋•ก!'์ด๋ผ๋Š” ๋ฌธ์žฅ์ด ์žˆ๋‹ค๋ฉด '12์‹œ ๋•ก !'์œผ๋กœ ๋•ก๊ณผ! ์‚ฌ์ด์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด ์ „์ฒ˜๋ฆฌ๋Š” ์งˆ๋ฌธ ๋ฐ์ดํ„ฐ์™€ ๋‹ต๋ณ€ ๋ฐ์ดํ„ฐ ๋ชจ๋‘์— ์ ์šฉํ•ด ์ค๋‹ˆ๋‹ค. questions = [] for sentence in train_data['Q']: # ๊ตฌ๋‘์ ์— ๋Œ€ํ•ด์„œ ๋„์–ด์“ฐ๊ธฐ # ex) 12์‹œ ๋•ก! -> 12์‹œ ๋•ก! sentence = re.sub(r"([?.!,])", r" \1 ", sentence) sentence = sentence.strip() questions.append(sentence) answers = [] for sentence in train_data['A']: # ๊ตฌ๋‘์ ์— ๋Œ€ํ•ด์„œ ๋„์–ด์“ฐ๊ธฐ # ex) 12์‹œ ๋•ก! -> 12์‹œ ๋•ก! sentence = re.sub(r"([?.!,])", r" \1 ", sentence) sentence = sentence.strip() answers.append(sentence) ์งˆ๋ฌธ๊ณผ ๋Œ€๋‹ต์— ๋Œ€ํ•ด์„œ ์ƒ์œ„ 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•˜์—ฌ ๊ตฌ๋‘์ ๋“ค์ด ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ†ตํ•ด ๋ถ„๋ฆฌ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(questions[:5]) print(answers[:5]) ['12์‹œ ๋•ก !', '1์ง€๋ง ํ•™๊ต ๋–จ์–ด์กŒ์–ด', '3๋ฐ• 4์ผ ๋†€๋Ÿฌ ๊ฐ€๊ณ  ์‹ถ๋‹ค', '3๋ฐ• 4์ผ ์ •๋„ ๋†€๋Ÿฌ ๊ฐ€๊ณ  ์‹ถ๋‹ค', 'PPL ์‹ฌํ•˜๋„ค'] ['ํ•˜๋ฃจ๊ฐ€ ๋˜ ๊ฐ€๋„ค์š” .', '์œ„๋กœํ•ด ๋“œ๋ฆฝ๋‹ˆ๋‹ค .', '์—ฌํ–‰์€ ์–ธ์ œ๋‚˜ ์ข‹์ฃ  .', '์—ฌํ–‰์€ ์–ธ์ œ๋‚˜ ์ข‹์ฃ  .', '๋ˆˆ์‚ด์ด ์ฐŒํ‘ธ๋ ค์ง€์ฃ  .'] 'ํ•˜๋ฃจ๊ฐ€ ๋˜ ๊ฐ€๋„ค์š” .'์™€ ๊ฐ™์ด ๊ตฌ๋‘์  ์•ž์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ๋ถ„๋ฆฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. ๋‹จ์–ด ์ง‘ํ•ฉ ์ƒ์„ฑ ์•ž์„œ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ € ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์› ๋˜ ์„œ๋ธŒ ์›Œ๋“œ ํ…์ŠคํŠธ ์ธ์ฝ”๋”๋ฅผ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ ๋‹จ์œ„๋กœ ํ† ํฐ์„ ๋ถ„๋ฆฌํ•˜๋Š” ํ† ํฌ ๋‚˜์ด์ €๋กœ ํ•™์Šต ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ํ•™์Šตํ•˜์—ฌ ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๊ตฌ์„ฑ๋œ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. # ์„œ๋ธŒ ์›Œ๋“œ ํ…์ŠคํŠธ ์ธ์ฝ”๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์งˆ๋ฌธ, ๋‹ต๋ณ€ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary) ์ƒ์„ฑ tokenizer = tfds.deprecated.text.SubwordTextEncoder.build_from_corpus( questions + answers, target_vocab_size=2**13) ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ seq2seq ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์› ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ ๊ณ„์—ด์—๋Š” ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์‹œ์ž‘์„ ์˜๋ฏธํ•˜๋Š” ์‹œ์ž‘ ํ† ํฐ SOS์™€ ์ข…๋ฃŒ ํ† ํฐ EOS ๋˜ํ•œ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ† ํฐ๋“ค๋„ ๋‹จ์–ด ์ง‘ํ•ฉ์— ํฌํ•จ์‹œํ‚ฌ ํ•„์š”๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์ด ๋‘ ํ† ํฐ์— ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ด ์ค๋‹ˆ๋‹ค. # ์‹œ์ž‘ ํ† ํฐ๊ณผ ์ข…๋ฃŒ ํ† ํฐ์— ๋Œ€ํ•œ ์ •์ˆ˜ ๋ถ€์—ฌ. START_TOKEN, END_TOKEN = [tokenizer.vocab_size], [tokenizer.vocab_size + 1] # ์‹œ์ž‘ ํ† ํฐ๊ณผ ์ข…๋ฃŒ ํ† ํฐ์„ ๊ณ ๋ คํ•˜์—ฌ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ + 2 VOCAB_SIZE = tokenizer.vocab_size + 2 ์‹œ์ž‘ ํ† ํฐ๊ณผ ์ข…๋ฃŒ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์œผ๋‚˜ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋„ +2๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค. ์‹œ์ž‘ ํ† ํฐ์˜ ๋ฒˆํ˜ธ์™€ ์ข…๋ฃŒ ํ† ํฐ์˜ ๋ฒˆํ˜ธ, ๊ทธ๋ฆฌ๊ณ  ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print('์‹œ์ž‘ ํ† ํฐ ๋ฒˆํ˜ธ :',START_TOKEN) print('์ข…๋ฃŒ ํ† ํฐ ๋ฒˆํ˜ธ :',END_TOKEN) print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :',VOCAB_SIZE) ์‹œ์ž‘ ํ† ํฐ ๋ฒˆํ˜ธ : [8178] ์ข…๋ฃŒ ํ† ํฐ ๋ฒˆํ˜ธ : [8179] ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 8180 ํŒจ๋”ฉ์— ์‚ฌ์šฉ๋  0๋ฒˆ ํ† ํฐ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ํ† ํฐ์ธ 8,179๋ฒˆ ํ† ํฐ๊นŒ์ง€์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๋ฉด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 8,180๊ฐœ์ž…๋‹ˆ๋‹ค. 3. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ํŒจ๋”ฉ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•œ ํ›„์—๋Š” ์„œ๋ธŒ ์›Œ๋“œ ํ…์ŠคํŠธ ์ธ์ฝ”๋”์˜ ํ† ํฌ ๋‚˜์ด์ €๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ† ํฌ ๋‚˜์ด์ €์˜. encode()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ž„์˜๋กœ ์„ ํƒํ•œ 20๋ฒˆ ์งˆ๋ฌธ ์ƒ˜ํ”Œ. ์ฆ‰, questions[20]์„ ๊ฐ€์ง€๊ณ  ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. # ์„œ๋ธŒ ์›Œ๋“œ ํ…์ŠคํŠธ ์ธ์ฝ”๋” ํ† ํฌ ๋‚˜์ด์ €์˜. encode()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋ฅผ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜. print('์ž„์˜์˜ ์งˆ๋ฌธ ์ƒ˜ํ”Œ์„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : {}'.format(tokenizer.encode(questions[20]))) ์ž„์˜์˜ ์งˆ๋ฌธ ์ƒ˜ํ”Œ์„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [5766, 611, 3509, 141, 685, 3747, 849] ์ž„์˜์˜ ์งˆ๋ฌธ ๋ฌธ์žฅ์ด ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์‹œ decode()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ์กด์˜ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋ณต์›ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 20๋ฒˆ ์งˆ๋ฌธ ์ƒ˜ํ”Œ์„ ๊ฐ€์ง€๊ณ  ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•˜๊ณ , ๋‹ค์‹œ ์ด๋ฅผ ๋””์ฝ”๋”ฉ ํ•˜๋Š” ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ์„œ๋ธŒ ์›Œ๋“œ ํ…์ŠคํŠธ ์ธ์ฝ”๋” ํ† ํฌ ๋‚˜์ด์ €์˜. encode()์™€. decode() ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ธฐ # ์ž„์˜์˜ ์ž…๋ ฅ ๋ฌธ์žฅ์„ sample_string์— ์ €์žฅ sample_string = questions[20] # encode() : ํ…์ŠคํŠธ ์‹œํ€€์Šค --> ์ •์ˆ˜ ์‹œํ€€์Šค tokenized_string = tokenizer.encode(sample_string) print ('์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„์˜ ๋ฌธ์žฅ {}'.format(tokenized_string)) # decode() : ์ •์ˆ˜ ์‹œํ€€์Šค --> ํ…์ŠคํŠธ ์‹œํ€€์Šค original_string = tokenizer.decode(tokenized_string) print ('๊ธฐ์กด ๋ฌธ์žฅ: {}'.format(original_string)) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„์˜ ๋ฌธ์žฅ [5766, 611, 3509, 141, 685, 3747, 849] ๊ธฐ์กด ๋ฌธ์žฅ: ๊ฐ€์Šค๋น„ ๋น„์‹ผ๋ฐ ๊ฐ๊ธฐ ๊ฑธ๋ฆฌ๊ฒ ์–ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๋ฌธ์žฅ์„. decode()์„ ํ•˜๋ฉด ์ž๋™์œผ๋กœ ์„œ๋ธŒ ์›Œ๋“œ๋“ค๊นŒ์ง€ ๋‹ค์‹œ ๋ถ™์—ฌ์„œ ๊ธฐ์กด ๋‹จ์–ด๋กœ ๋ณต์›ํ•ด ์ค๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋ฌธ์žฅ์„ ๋ณด๋ฉด ์ •์ˆ˜๊ฐ€ 7๊ฐœ์ธ๋ฐ ๊ธฐ์กด ๋ฌธ์žฅ์˜ ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„์ธ ์–ด์ ˆ์€ 4๊ฐœ๋ฐ–์— ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” '๊ฐ€์Šค๋น„'๋‚˜ '๋น„์‹ผ๋ฐ'๋ผ๋Š” ํ•œ ์–ด์ ˆ์ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„์—๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ์ •์ˆ˜์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ฐ ์ •์ˆ˜๊ฐ€ ์–ด๋–ค ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋งคํ•‘๋˜๋Š”์ง€ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # ๊ฐ ์ •์ˆ˜๋Š” ๊ฐ ๋‹จ์–ด์™€ ์–ด๋–ป๊ฒŒ mapping ๋˜๋Š”์ง€ ๋ณ‘๋ ฌ๋กœ ์ถœ๋ ฅ # ์„œ๋ธŒ ์›Œ๋“œ ํ…์ŠคํŠธ ์ธ์ฝ”๋”๋Š” ์˜๋ฏธ ์žˆ๋Š” ๋‹จ์œ„์˜ ์„œ๋ธŒ ์›Œ๋“œ๋กœ ํ† ํฌ๋‚˜์ด์ง•ํ•œ๋‹ค. ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„ X ํ˜•ํƒœ์†Œ ๋ถ„์„ ๋‹จ์œ„ X for ts in tokenized_string: print ('{} ----> {}'.format(ts, tokenizer.decode([ts]))) 5766 ----> ๊ฐ€์Šค 611 ----> ๋น„ 3509 ----> ๋น„์‹ผ 141 ----> ๋ฐ 685 ----> ๊ฐ๊ธฐ 3747 ----> ๊ฑธ๋ฆฌ 849 ----> ๊ฒ ์–ด ์ƒ˜ํ”Œ 1๊ฐœ๋ฅผ ๊ฐ€์ง€๊ณ  ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ๋””์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ํŒจ๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜๋กœ tokenize_and_filter()๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ž„์˜๋กœ ํŒจ๋”ฉ์˜ ๊ธธ์ด๋Š” 40์œผ๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. # ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ 40์œผ๋กœ ์ •์˜ MAX_LENGTH = 40 # ํ† ํฐํ™” / ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ / ์‹œ์ž‘ ํ† ํฐ๊ณผ ์ข…๋ฃŒ ํ† ํฐ ์ถ”๊ฐ€ / ํŒจ๋”ฉ def tokenize_and_filter(inputs, outputs): tokenized_inputs, tokenized_outputs = [], [] for (sentence1, sentence2) in zip(inputs, outputs): # encode(ํ† ํฐํ™” + ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ), ์‹œ์ž‘ ํ† ํฐ๊ณผ ์ข…๋ฃŒ ํ† ํฐ ์ถ”๊ฐ€ sentence1 = START_TOKEN + tokenizer.encode(sentence1) + END_TOKEN sentence2 = START_TOKEN + tokenizer.encode(sentence2) + END_TOKEN tokenized_inputs.append(sentence1) tokenized_outputs.append(sentence2) # ํŒจ๋”ฉ tokenized_inputs = tf.keras.preprocessing.sequence.pad_sequences( tokenized_inputs, maxlen=MAX_LENGTH, padding='post') tokenized_outputs = tf.keras.preprocessing.sequence.pad_sequences( tokenized_outputs, maxlen=MAX_LENGTH, padding='post') return tokenized_inputs, tokenized_outputs questions, answers = tokenize_and_filter(questions, answers) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ํŒจ๋”ฉ์ด ์ง„ํ–‰๋œ ํ›„์˜ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('์งˆ๋ฌธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape) :', questions.shape) print('๋‹ต๋ณ€ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape) :', answers.shape) ์งˆ๋ฌธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape) : (11823, 40) ๋‹ต๋ณ€ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape) : (11823, 40) ์งˆ๋ฌธ๊ณผ ๋‹ต๋ณ€ ๋ฐ์ดํ„ฐ์˜ ๋ชจ๋“  ๋ฌธ์žฅ์ด ๋ชจ๋‘ ๊ธธ์ด 40์œผ๋กœ ๋ณ€ํ™˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ž„์˜๋กœ 0๋ฒˆ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # 0๋ฒˆ ์ƒ˜ํ”Œ์„ ์ž„์˜๋กœ ์ถœ๋ ฅ print(questions[0]) print(answers[0]) [8178 7915 4207 3060 41 8179 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [8178 3844 74 7894 1 8179 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] ๊ธธ์ด 40์„ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ๋’ค์— 0์ด ํŒจ๋”ฉ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ, ๊ทธ๋ฆฌ๊ณ  ๋ ˆ์ด๋ธ” ๋งŒ๋“ค๊ธฐ. tf.data.Dataset์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ํ…์„œ ํ”Œ๋กœ dataset์„ ์ด์šฉํ•˜์—ฌ ์…”ํ”Œ(shuffle)์„ ์ˆ˜ํ–‰ํ•˜๋˜, ๋ฐฐ์น˜ ํฌ๊ธฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ๋Š”๋‹ค. # ๋˜ํ•œ ์ด ๊ณผ์ •์—์„œ ๊ต์‚ฌ ๊ฐ•์š”(teacher forcing)์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ๊ณผ ์‹ค์ œ ๊ฐ’ ์‹œํ€€์Šค๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. BATCH_SIZE = 64 BUFFER_SIZE = 20000 # ๋””์ฝ”๋”์˜ ์‹ค์ œ ๊ฐ’ ์‹œํ€€์Šค์—์„œ๋Š” ์‹œ์ž‘ ํ† ํฐ์„ ์ œ๊ฑฐํ•ด์•ผ ํ•œ๋‹ค. dataset = tf.data.Dataset.from_tensor_slices(( { 'inputs': questions, 'dec_inputs': answers[:, :-1] # ๋””์ฝ”๋”์˜ ์ž…๋ ฅ. ๋งˆ์ง€๋ง‰ ํŒจ๋”ฉ ํ† ํฐ์ด ์ œ๊ฑฐ๋œ๋‹ค. }, { 'outputs': answers[:, 1:] # ๋งจ ์ฒ˜์Œ ํ† ํฐ์ด ์ œ๊ฑฐ๋œ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์‹œ์ž‘ ํ† ํฐ์ด ์ œ๊ฑฐ๋œ๋‹ค. }, )) dataset = dataset.cache() dataset = dataset.shuffle(BUFFER_SIZE) dataset = dataset.batch(BATCH_SIZE) dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE) # ์ž„์˜์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ [:, :-1]๊ณผ [:, 1:]์ด ์–ด๋–ค ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ด ๋ณธ๋‹ค. print(answers[0]) # ๊ธฐ์กด ์ƒ˜ํ”Œ print(answers[:1][:, :-1]) # ๋งˆ์ง€๋ง‰ ํŒจ๋”ฉ ํ† ํฐ ์ œ๊ฑฐํ•˜๋ฉด์„œ ๊ธธ์ด๊ฐ€ 39๊ฐ€ ๋œ๋‹ค. print(answers[:1][:, 1:]) # ๋งจ ์ฒ˜์Œ ํ† ํฐ์ด ์ œ๊ฑฐ๋œ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์‹œ์ž‘ ํ† ํฐ์ด ์ œ๊ฑฐ๋œ๋‹ค. ๊ธธ์ด๋Š” ์—ญ์‹œ 39๊ฐ€ ๋œ๋‹ค. [8178 3844 74 7894 1 8179 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [[8178 3844 74 7894 1 8179 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]] [[3844 74 7894 1 8179 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]] 5. ํŠธ๋žœ์Šคํฌ๋จธ ๋งŒ๋“ค๊ธฐ ์ด์ œ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์กฐ์ •ํ•˜์—ฌ ์‹ค์ œ ๋…ผ๋ฌธ์˜ ํŠธ๋žœ์Šคํฌ๋จธ๋ณด๋‹ค๋Š” ์ž‘์€ ๋ชจ๋ธ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์„ ํƒํ•œ ์ฃผ์š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. m d l = 256 num_layers = 2 num_heads = 8 f = 512 tf.keras.backend.clear_session() # ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ D_MODEL = 256 NUM_LAYERS = 2 NUM_HEADS = 8 DFF = 512 DROPOUT = 0.1 model = transformer( vocab_size=VOCAB_SIZE, num_layers=NUM_LAYERS, dff=DFF, d_model=D_MODEL, num_heads=NUM_HEADS, dropout=DROPOUT) ํ•™์Šต๋ฅ ๊ณผ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ •์˜ํ•˜๊ณ  ๋ชจ๋ธ์„ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค. learning_rate = CustomSchedule(D_MODEL) optimizer = tf.keras.optimizers.Adam( learning_rate, beta_1=0.9, beta_2=0.98, epsilon=1e-9) def accuracy(y_true, y_pred): # ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ๋Š” (batch_size, MAX_LENGTH - 1) y_true = tf.reshape(y_true, shape=(-1, MAX_LENGTH - 1)) return tf.keras.metrics.sparse_categorical_accuracy(y_true, y_pred) model.compile(optimizer=optimizer, loss=loss_function, metrics=[accuracy]) ์ด 50ํšŒ ๋ชจ๋ธ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. EPOCHS = 50 model.fit(dataset, epochs=EPOCHS) 6. ์ฑ—๋ด‡ ํ‰๊ฐ€ํ•˜๊ธฐ. ์ฑ—๋ด‡์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ์„ธ ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. predict ํ•จ์ˆ˜์—์„œ evaluate ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  evaluate ํ•จ์ˆ˜์—์„œ preprocess_sentence ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์ด ํŒŒ์ด์ฌ์˜ ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅ๋˜๋ฉด preprocess_sentence ํ•จ์ˆ˜์—์„œ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ „์ฒ˜๋ฆฌ๋Š” ํ•™์Šต ์ „ ์งˆ๋ฌธ ๋ฐ์ดํ„ฐ์™€ ๋‹ต๋ณ€ ๋ฐ์ดํ„ฐ์—์„œ ํ–ˆ๋˜ ์ „์ฒ˜๋ฆฌ์™€ ๋™์ผํ•œ ์ „์ฒ˜๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ๊ฐ€ ์ง„ํ–‰๋œ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ evaluate ํ•จ์ˆ˜๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ์— ์ „์ฒ˜๋ฆฌ๊ฐ€ ์ง„ํ–‰๋œ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ์ „๋‹ฌํ•˜๊ณ , ๋””์ฝ”๋”๋ฅผ ํ†ตํ•ด ๊ณ„์†ํ•ด์„œ ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก. ๋‹ค์‹œ ๋งํ•ด ์ฑ—๋ด‡์˜ ๋Œ€๋‹ต์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์˜ˆ์ธก๋œ ๋‹จ์–ด๋“ค์€ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ ์ •์ˆ˜์ธ ์ƒํƒœ์ด๋ฏ€๋กœ evaluate ํ•จ์ˆ˜๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์€ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ •์ˆ˜ ์‹œํ€€์Šค์ž…๋‹ˆ๋‹ค. predict ํ•จ์ˆ˜๋Š” evaluate ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์ฑ—๋ด‡์˜ ๋Œ€๋‹ต์— ํ•ด๋‹นํ•˜๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋‹ค์‹œ ๋””์ฝ”๋”ฉ์„ ํ•˜๊ณ  ์‚ฌ์šฉ์ž์—๊ฒŒ ์ฑ—๋ด‡์˜ ๋Œ€๋‹ต์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. def preprocess_sentence(sentence): # ๋‹จ์–ด์™€ ๊ตฌ๋‘์  ์‚ฌ์ด์— ๊ณต๋ฐฑ ์ถ”๊ฐ€. # ex) 12์‹œ ๋•ก! -> 12์‹œ ๋•ก! sentence = re.sub(r"([?.!,])", r" \1 ", sentence) sentence = sentence.strip() return sentence def evaluate(sentence): # ์ž…๋ ฅ ๋ฌธ์žฅ์— ๋Œ€ํ•œ ์ „์ฒ˜๋ฆฌ sentence = preprocess_sentence(sentence) # ์ž…๋ ฅ ๋ฌธ์žฅ์— ์‹œ์ž‘ ํ† ํฐ๊ณผ ์ข…๋ฃŒ ํ† ํฐ์„ ์ถ”๊ฐ€ sentence = tf.expand_dims( START_TOKEN + tokenizer.encode(sentence) + END_TOKEN, axis=0) output = tf.expand_dims(START_TOKEN, 0) # ๋””์ฝ”๋”์˜ ์˜ˆ์ธก ์‹œ์ž‘ for i in range(MAX_LENGTH): predictions = model(inputs=[sentence, output], training=False) # ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก ๋‹จ์–ด๋ฅผ ๋ฐ›์•„์˜จ๋‹ค. predictions = predictions[:, -1:, :] predicted_id = tf.cast(tf.argmax(predictions, axis=-1), tf.int32) # ๋งŒ์•ฝ ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก ๋‹จ์–ด๊ฐ€ ์ข…๋ฃŒ ํ† ํฐ์ด๋ผ๋ฉด ์˜ˆ์ธก์„ ์ค‘๋‹จ if tf.equal(predicted_id, END_TOKEN[0]): break # ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก ๋‹จ์–ด๋ฅผ output(์ถœ๋ ฅ)์— ์—ฐ๊ฒฐํ•œ๋‹ค. # output์€ for ๋ฌธ์˜ ๋‹ค์Œ ๋ฃจํ”„์—์„œ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์ด ๋œ๋‹ค. output = tf.concat([output, predicted_id], axis=-1) # ๋‹จ์–ด ์˜ˆ์ธก์ด ๋ชจ๋‘ ๋๋‚ฌ๋‹ค๋ฉด output์„ ๋ฆฌํ„ด. return tf.squeeze(output, axis=0) def predict(sentence): prediction = evaluate(sentence) # prediction == ๋””์ฝ”๋”๊ฐ€ ๋ฆฌํ„ด ํ•œ ์ฑ—๋ด‡์˜ ๋Œ€๋‹ต์— ํ•ด๋‹นํ•˜๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค # tokenizer.decode()๋ฅผ ํ†ตํ•ด ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋””์ฝ”๋”ฉ. predicted_sentence = tokenizer.decode( [i for i in prediction if i < tokenizer.vocab_size]) print('Input: {}'.format(sentence)) print('Output: {}'.format(predicted_sentence)) return predicted_sentence ํ•™์Šต๋œ ํŠธ๋žœ์Šคํฌ๋จธ์— ์ž„์˜๋กœ ์ƒ๊ฐ๋‚˜๋Š” ๋ง๋“ค์„ ์ ์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. output = predict("์˜ํ™” ๋ณผ๋ž˜?") Input: ์˜ํ™” ๋ณผ๋ž˜? Output: ์ตœ์‹  ์˜ํ™”๊ฐ€ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. output = predict("๊ณ ๋ฏผ์ด ์žˆ์–ด") Input: ๊ณ ๋ฏผ์ด ์žˆ์–ด Output: ์ƒ๊ฐ์„ ์ข…์ด์— ๋„์ น์—ฌ์—ฌ ๋ณด๋Š” ๊ฒŒ ๋„์›€์ด ๋  ์ˆ˜๋„ ์žˆ์–ด์š”. output = predict("๋„ˆ๋ฌด ํ™”๊ฐ€ ๋‚˜") Input: ๋„ˆ๋ฌด ํ™”๊ฐ€ ๋‚˜ Output: ๊ทธ๋Ÿด์ˆ˜๋ก ๋‹น์‹ ์ด ํž˜๋“ค ๊ฑฐ์˜ˆ์š”. output = predict("์นดํŽ˜ ๊ฐˆ๋ž˜?") Input: ์นดํŽ˜ ๊ฐˆ๋ž˜? Output: ์นดํŽ˜ ๊ฐ€์„œ ์ฐจ ๋งˆ์…”๋„ ๋ผ์š”. output = predict("๊ฒŒ์ž„ํ•˜๊ณ  ์‹ถ๋‹ค") Input: ๊ฒŒ์ž„ํ•˜๊ณ  ์‹ถ๋‹ค Output: ์ €๋„์š”! output = predict("๊ฒŒ์ž„ํ•˜์ž") Input: ๊ฒŒ์ž„ํ•˜์ž Output: ๊ฒŒ์ž„ํ•˜์„ธ์š”! ๊ฐ„๋‹จํ•œ ๋Œ€ํ™”์ธ๋งŒํผ ๊ทธ๋Ÿญ์ €๋Ÿญ ๊ดœ์ฐฎ์€ ๋‹ต๋ณ€์„ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ๋” ๋งŽ๋‹ค๋ฉด, ๋” ๋‹ค์–‘ํ•œ ๋Œ€๋‹ต์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ฑ—๋ด‡์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 16-03 ์…€ํ”„ ์–ดํ…์…˜์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜(Multi-head Self Attention for Text Classification) ํŠธ๋žœ์Šคํฌ๋จธ๋Š” RNN ๊ณ„์—ด์˜ seq2seq๋ฅผ ๋Œ€์ฒดํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋”๋Š” RNN ์ธ์ฝ”๋”๋ฅผ, ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋””์ฝ”๋”๋Š” RNN ๋””์ฝ”๋”๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋”๋Š” ์…€ํ”„ ์–ดํ…์…˜์ด๋ผ๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ํ†ตํ•ด ๋ฌธ์žฅ์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. RNN๊ณผ ๊ทธ ๋™์ž‘ ๋ฐฉ์‹์€ ๋‹ค๋ฅด์ง€๋งŒ, RNN์ด ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋‚˜ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ํƒœ์Šคํฌ์— ์“ฐ์ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋” ๋˜ํ•œ ๊ฐ€๋Šฅํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋”๋Š” ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ํƒœ์Šคํฌ์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์—ˆ๊ณ , ์ด ์•„์ด๋””์–ด๋Š” ํ›„์— ๋ฐฐ์šฐ๊ฒŒ ๋  BERT๋ผ๋Š” ๋ชจ๋ธ๋กœ ์ด์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜ ์šฐ์„  ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์ธต์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. import tensorflow as tf class MultiHeadAttention(tf.keras.layers.Layer): def __init__(self, embedding_dim, num_heads=8): super(MultiHeadAttention, self).__init__() self.embedding_dim = embedding_dim # d_model self.num_heads = num_heads assert embedding_dim % self.num_heads == 0 self.projection_dim = embedding_dim // num_heads self.query_dense = tf.keras.layers.Dense(embedding_dim) self.key_dense = tf.keras.layers.Dense(embedding_dim) self.value_dense = tf.keras.layers.Dense(embedding_dim) self.dense = tf.keras.layers.Dense(embedding_dim) def scaled_dot_product_attention(self, query, key, value): matmul_qk = tf.matmul(query, key, transpose_b=True) depth = tf.cast(tf.shape(key)[-1], tf.float32) logits = matmul_qk / tf.math.sqrt(depth) attention_weights = tf.nn.softmax(logits, axis=-1) output = tf.matmul(attention_weights, value) return output, attention_weights def split_heads(self, x, batch_size): x = tf.reshape(x, (batch_size, -1, self.num_heads, self.projection_dim)) return tf.transpose(x, perm=[0, 2, 1, 3]) def call(self, inputs): # x.shape = [batch_size, seq_len, embedding_dim] batch_size = tf.shape(inputs)[0] # (batch_size, seq_len, embedding_dim) query = self.query_dense(inputs) key = self.key_dense(inputs) value = self.value_dense(inputs) # (batch_size, num_heads, seq_len, projection_dim) query = self.split_heads(query, batch_size) key = self.split_heads(key, batch_size) value = self.split_heads(value, batch_size) scaled_attention, _ = self.scaled_dot_product_attention(query, key, value) # (batch_size, seq_len, num_heads, projection_dim) scaled_attention = tf.transpose(scaled_attention, perm=[0, 2, 1, 3]) # (batch_size, seq_len, embedding_dim) concat_attention = tf.reshape(scaled_attention, (batch_size, -1, self.embedding_dim)) outputs = self.dense(concat_attention) return outputs 2. ์ธ์ฝ”๋” ์„ค๊ณ„ํ•˜๊ธฐ ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜์— ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต์ธ ํฌ์ง€์…˜ ์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ธ์ฝ”๋” ํด๋ž˜์Šค๋ฅผ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. class TransformerBlock(tf.keras.layers.Layer): def __init__(self, embedding_dim, num_heads, dff, rate=0.1): super(TransformerBlock, self).__init__() self.att = MultiHeadAttention(embedding_dim, num_heads) self.ffn = tf.keras.Sequential( [tf.keras.layers.Dense(dff, activation="relu"), tf.keras.layers.Dense(embedding_dim),] ) self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6) self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6) self.dropout1 = tf.keras.layers.Dropout(rate) self.dropout2 = tf.keras.layers.Dropout(rate) def call(self, inputs, training): attn_output = self.att(inputs) # ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต : ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์–ดํ…์…˜ attn_output = self.dropout1(attn_output, training=training) out1 = self.layernorm1(inputs + attn_output) # Add & Norm ffn_output = self.ffn(out1) # ๋‘ ๋ฒˆ์งธ ์„œ๋ธŒ์ธต : ํฌ์ง€์…˜ ์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง ffn_output = self.dropout2(ffn_output, training=training) return self.layernorm2(out1 + ffn_output) # Add & Norm 3. ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ ์•ž์„œ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์„ค๋ช…ํ•  ๋•Œ๋Š” ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, ์ด๋ฒˆ์—๋Š” ์œ„์น˜ ์ •๋ณด ์ž์ฒด๋ฅผ ํ•™์Šต์„ ํ•˜๋„๋ก ํ•˜๋Š” ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋  BERT์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ์€ ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)๋ฅผ ์‚ฌ์šฉํ•˜๋˜, ์œ„์น˜ ๋ฒกํ„ฐ๋ฅผ ํ•™์Šตํ•˜๋„๋ก ํ•˜๋ฏ€๋กœ ์ž„๋ฒ ๋”ฉ ์ธต์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. class TokenAndPositionEmbedding(tf.keras.layers.Layer): def __init__(self, max_len, vocab_size, embedding_dim): super(TokenAndPositionEmbedding, self).__init__() self.token_emb = tf.keras.layers.Embedding(vocab_size, embedding_dim) self.pos_emb = tf.keras.layers.Embedding(max_len, embedding_dim) def call(self, x): max_len = tf.shape(x)[-1] positions = tf.range(start=0, limit=max_len, delta=1) positions = self.pos_emb(positions) x = self.token_emb(x) return x + positions 4. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ์ „์ฒ˜๋ฆฌ vocab_size = 20000 # ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 2๋งŒ ๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์‚ฌ์šฉ max_len = 200 # ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด (X_train, y_train), (X_test, y_test) = tf.keras.datasets.imdb.load_data(num_words=vocab_size) print('ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : {}'.format(len(X_train))) print('ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : {}'.format(len(X_test))) ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 25000 ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 25000 X_train = tf.keras.preprocessing.sequence.pad_sequences(X_train, maxlen=max_len) X_test = tf.keras.preprocessing.sequence.pad_sequences(X_test, maxlen=max_len) 5. ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ด์šฉํ•œ IMDB ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ embedding_dim = 32 # ๊ฐ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์› num_heads = 2 # ์–ดํ…์…˜ ํ—ค๋“œ์˜ ์ˆ˜ dff = 32 # ํฌ์ง€์…˜ ์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์˜ ์€๋‹‰์ธต์˜ ํฌ๊ธฐ inputs = tf.keras.layers.Input(shape=(max_len,)) embedding_layer = TokenAndPositionEmbedding(max_len, vocab_size, embedding_dim) x = embedding_layer(inputs) transformer_block = TransformerBlock(embedding_dim, num_heads, dff) x = transformer_block(x) x = tf.keras.layers.GlobalAveragePooling1D()(x) x = tf.keras.layers.Dropout(0.1)(x) x = tf.keras.layers.Dense(20, activation="relu")(x) x = tf.keras.layers.Dropout(0.1)(x) outputs = tf.keras.layers.Dense(2, activation="softmax")(x) model = tf.keras.Model(inputs=inputs, outputs=outputs) model.compile("adam", "sparse_categorical_crossentropy", metrics=["accuracy"]) history = model.fit(X_train, y_train, batch_size=32, epochs=2, validation_data=(X_test, y_test)) print("ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (model.evaluate(X_test, y_test)[1])) ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.8695 17. BERT(Bidirectional Encoder Representations from Transformers) ํŠธ๋žœ์Šคํฌ๋จธ(transformer)์˜ ๋“ฑ์žฅ ์ดํ›„, ๋‹ค์–‘ํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ํƒœ์Šคํฌ์—์„œ ์‚ฌ์šฉ๋˜์—ˆ๋˜ RNN ๊ณ„์—ด์˜ ์‹ ๊ฒฝ๋ง์ธ LSTM, GRU๋Š” ํŠธ๋žœ์Šคํฌ๋จธ๋กœ ๋Œ€์ฒด๋˜์–ด๊ฐ€๋Š” ์ถ”์„ธ์ž…๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ํŠธ๋žœ์Šคํฌ๋จธ ๊ณ„์—ด์˜ BERT, GPT, T5 ๋“ฑ ๋‹ค์–‘ํ•œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ๋“ค์ด ๊ณ„์†ํ•ด์„œ ๋“ฑ์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ ์ค‘ ํ•˜๋‚˜์ธ BERT์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ์†Œ๊ฐœํ•˜๋Š” BERT์˜ ๊ธฐ๋ณธ ๊ฐœ๋…์„ ์ดํ•ดํ•˜๊ณ , ALBERT, RoBERTa, ELECTRA์™€ ๊ฐ™์€ BERT์˜ ํŒŒ์ƒ ๋ชจ๋ธ์„ ๊ณต๋ถ€ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํŠธ๋žœ์Šคํฌ๋จธ ๊ณ„์—ด ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•œ ๊ณต๋ถ€๋ฅผ ์ด์–ด๋‚˜๊ฐ€์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ALBERT : A Lite BERT for Self-supervised Learning of Language Representations RoBERTa : A Robustly Optimized BERT Pretraining Approach ELECTRA : Efficiently Learning an Encoder that Classifies Token Replacements Accurately 17-01 NLP์—์„œ์˜ ์‚ฌ์ „ ํ›ˆ๋ จ(Pre-training) 2018๋…„ ๋”ฅ ๋Ÿฌ๋‹ ์—ฐ๊ตฌ์› ์„ธ๋ฐ”์Šค์ฐฌ ๋ฃจ๋”๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ์˜ ์•ฝ์ง„์„ ๋ณด๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ง์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. "์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ์ด ๋ชจ๋“  NLP ์‹ค๋ฌด์ž์˜ ๋„๊ตฌ ์ƒ์ž์—์„œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ๋กœ ๋Œ€์ฒด๋˜๋Š” ๊ฒƒ์€ ์‹œ๊ฐ„๋ฌธ์ œ์ด๋‹ค." BERT(Bidirectional Encoder Representations from Transformers)์™€ ๊ฐ™์€ ํŠธ๋žœ์Šคํฌ๋จธ ๊ณ„์—ด์˜ ๋ชจ๋ธ๋“ค์ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์ง€๋ฐฐํ–ˆ๋˜ 19๋…„๊ณผ 20๋…„์„ ํšŒ๊ณ ํ•˜๋ฉด ์ด ๋ง์€ ์ด๋ฏธ ํ˜„์‹ค์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. BERT๋ฅผ ๋ฐฐ์šฐ๊ธฐ์— ์•ž์„œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์—์„œ๋ถ€ํ„ฐ ELMo, ๊ทธ๋ฆฌ๊ณ  ํŠธ๋žœ์Šคํฌ๋จธ์— ์ด๋ฅด๊ธฐ๊นŒ์ง€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๊ฐ€ ๋ฐœ์ „๋˜์–ด์˜จ ํ๋ฆ„์„ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. 1. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์•ž์„œ Word2Vec, FastText, GloVe์™€ ๊ฐ™์€ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ๋“ค์„ ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ํƒœ์Šคํฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ, ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)์„ ๋žœ๋ค ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•. ๊ทธ๋ฆฌ๊ณ  ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋กœ Word2Vec ๋“ฑ๊ณผ ๊ฐ™์€ ์ž„๋ฒ ๋”ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์‚ฌ์ „์— ํ•™์Šต๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ํƒœ์Šคํฌ์— ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ ๋‹ค๋ฉด, ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๊ธฐ๋Œ€ํ•ด ๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ๋ชจ๋‘ ํ•˜๋‚˜์˜ ๋‹จ์–ด๊ฐ€ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ ๊ฐ’์œผ๋กœ ๋งคํ•‘๋˜๋ฏ€๋กœ, ๋ฌธ๋งฅ์„ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ•˜์—ฌ ๋‹ค์˜์–ด๋‚˜ ๋™์Œ์ด์˜์–ด๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด์—๋Š” '์‚ฌ๊ณผ'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ ์ด '์‚ฌ๊ณผ'๋Š” ์šฉ์„œ๋ฅผ ๋นˆ๋‹ค๋Š” ์˜๋ฏธ๋กœ๋„ ์“ฐ์ด์ง€๋งŒ, ๋จน๋Š” ๊ณผ์ผ์˜ ์˜๋ฏธ๋กœ๋„ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” '์‚ฌ๊ณผ'๋ผ๋Š” ๋ฒกํ„ฐ์— ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ ๊ฐ’์„ ๋งคํ•‘ํ•˜๋ฏ€๋กœ ์ด ๋‘ ๊ฐ€์ง€ ์˜๋ฏธ๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•œ๊ณ„๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๊ทน๋ณตํ•  ์ˆ˜ ์žˆ์—ˆ์œผ๋ฉฐ ์•„๋ž˜์—์„œ ์–ธ๊ธ‰ํ•  ELMo๋‚˜ BERT ๋“ฑ์ด ์ด๋Ÿฌํ•œ ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ์ฑ…์ž…๋‹ˆ๋‹ค. 2. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ 2015๋…„ ๊ตฌ๊ธ€์€ 'Semi-supervised Sequence Learning'๋ผ๋Š” ๋…ผ๋ฌธ์—์„œ LSTM ์–ธ์–ด ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ณ  ๋‚˜์„œ ์ด๋ ‡๊ฒŒ ํ•™์Šตํ•œ LSTM์„ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์— ์ถ”๊ฐ€ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์˜€์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์šฐ์„  LSTM ์–ธ์–ด ๋ชจ๋ธ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์€ ์ฃผ์–ด์ง„ ํ…์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ํ•™์Šตํ•˜๋ฏ€๋กœ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณ„๋„์˜ ๋ ˆ์ด๋ธ”์ด ๋ถ€์ฐฉ๋˜์ง€ ์•Š์€ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ๋„ ํ•™์Šต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฐ•์ ์€ ํ•™์Šต ์ „ ์‚ฌ๋žŒ์ด ๋ณ„๋„ ๋ ˆ์ด๋ธ”์„ ์ง€์ •ํ•ด ์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๋ ˆ์ด๋ธ”์ด ์—†๋Š” ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šต๋œ LSTM๊ณผ ๊ฐ€์ค‘์น˜๊ฐ€ ๋žœ๋ค์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ LSTM ๋‘ ๊ฐ€์ง€๋ฅผ ๋‘๊ณ , ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ•™์Šตํ•˜์—ฌ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ์ „์ž์˜ ๊ฒฝ์šฐ๊ฐ€ ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฐ€๋Šฅ์„ฑ์„ ๋ณด์˜€์Šต๋‹ˆ๋‹ค. ๋ฐฉ๋Œ€ํ•œ ํ…์ŠคํŠธ๋กœ LSTM ์–ธ์–ด ๋ชจ๋ธ์„ ํ•™์Šตํ•ด๋‘๊ณ , ์–ธ์–ด ๋ชจ๋ธ์„ ๋‹ค๋ฅธ ํƒœ์Šคํฌ์—์„œ ๋†’์€ ์„ฑ๋Šฅ์„ ์–ป๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ด์ „์— ์„ค๋ช…ํ•œ ELMo์™€ ๊ฐ™์€ ์•„์ด๋””์–ด๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ELMo๋Š” ์ˆœ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ์—ญ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์„ ๊ฐ๊ฐ ๋”ฐ๋กœ ํ•™์Šต์‹œํ‚จ ํ›„์—, ์ด๋ ‡๊ฒŒ ์‚ฌ์ „ ํ•™์Šต๋œ ์–ธ์–ด ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ ์ž„๋ฒ ๋”ฉ ๊ฐ’์„ ์–ป๋Š”๋‹ค๋Š” ์•„์ด๋””์–ด์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ž„๋ฒ ๋”ฉ์€ ๋ฌธ๋งฅ์— ๋”ฐ๋ผ์„œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์ด ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ, ๊ธฐ์กด ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ธ Word2Vec์ด๋‚˜ GloVe ๋“ฑ์ด ๋‹ค์˜์–ด๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์—†์—ˆ๋˜ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์–ด ์–ธ์–ด ๋ชจ๋ธ์€ RNN ๊ณ„์—ด์˜ ์‹ ๊ฒฝ๋ง์—์„œ ํƒˆํ”ผํ•˜๊ธฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ๊ฐ€ ๋ฒˆ์—ญ๊ธฐ์™€ ๊ฐ™์€ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๊ตฌ์กฐ์—์„œ LSTM์„ ๋›ฐ์–ด๋„˜๋Š” ์ข‹์€ ์„ฑ๋Šฅ์„ ์–ป์ž, LSTM์ด ์•„๋‹Œ ํŠธ๋žœ์Šคํฌ๋จธ๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š” ์‹œ๋„๊ฐ€ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ Trm์€ ํŠธ๋žœ์Šคํฌ๋จธ(Transformer)์˜ ์•ฝ์ž์ž…๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋””์ฝ”๋”๋Š” LSTM ์–ธ์–ด ๋ชจ๋ธ์ฒ˜๋Ÿผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. Open AI๋Š” ํŠธ๋žœ์Šคํฌ๋จธ ๋””์ฝ”๋”๋กœ ์ด 12๊ฐœ์˜ ์ธต์„ ์Œ“์€ ํ›„์— ๋ฐฉ๋Œ€ํ•œ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šต์‹œํ‚จ ์–ธ์–ด ๋ชจ๋ธ GPT-1์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. Open AI๋Š” GPT-1์— ์—ฌ๋Ÿฌ ๋‹ค์–‘ํ•œ ํƒœ์Šคํฌ๋ฅผ ์œ„ํ•ด ์ถ”๊ฐ€ ํ•™์Šต์„ ์ง„ํ–‰ํ•˜์˜€์„ ๋•Œ, ๋‹ค์–‘ํ•œ ํƒœ์Šคํฌ์—์„œ ๋†’์€ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Œ์„ ์ž…์ฆํ–ˆ์Šต๋‹ˆ๋‹ค. NLP์˜ ์ฃผ์š” ํŠธ๋ Œ๋“œ๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ ํŠน์ • ํƒœ์Šคํฌ์— ์ถ”๊ฐ€ ํ•™์Šต์‹œ์ผœ ํ•ด๋‹น ํƒœ์Šคํฌ์—์„œ ๋†’์€ ์„ฑ๋Šฅ์„ ์–ป๋Š” ๊ฒƒ์œผ๋กœ ์ ‘์–ด๋“ค์—ˆ๊ณ , ์–ธ์–ด ๋ชจ๋ธ์˜ ํ•™์Šต ๋ฐฉ๋ฒ•์— ๋ณ€ํ™”๋ฅผ ์ฃผ๋Š” ๋ชจ๋ธ๋“ค์ด ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ขŒ์ธก ๊ทธ๋ฆผ์— ์žˆ๋Š” ๋‹จ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์€ ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ์ „ํ˜•์ ์ธ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์‹œ์ž‘ ํ† ํฐ <SOS>๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด, ๋‹ค์Œ ๋‹จ์–ด I๋ฅผ ์˜ˆ์ธกํ•˜๊ณ  ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋‹ค์Œ ๋‹จ์–ด am์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ์šฐ์ธก์— ์žˆ๋Š” ์–‘๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์€ ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ์  ์—†๋˜ ํ˜•ํƒœ์˜ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†๋Š”๋ฐ ๊ทธ ์ด์œ ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ์š”? ๊ฐ€๋ น, ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•ด์„œ ์šฐ์ธก๊ณผ ๊ฐ™์€ ์–ธ์–ด ๋ชจ๋ธ์„ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ดˆ๋ก์ƒ‰ LSTM ์…€์€ ์ˆœ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ๋กœ <sos>๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ I๋ฅผ ์˜ˆ์ธกํ•˜๊ณ , ๊ทธ ํ›„์— am์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ am์„ ์˜ˆ์ธกํ•  ๋•Œ, ์ถœ๋ ฅ์ธต์€ ์ฃผํ™ฉ์ƒ‰ LSTM ์…€์ธ ์—ญ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์˜ ์ •๋ณด๋„ ํ•จ๊ป˜ ๋ฐ›๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ am์„ ์˜ˆ์ธกํ•˜๋Š” ์‹œ์ ์—์„œ ์—ญ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์ด ์ด๋ฏธ ๊ด€์ธกํ•œ ๋‹จ์–ด๋Š” a, am, I ์ด๋ ‡๊ฒŒ 3๊ฐœ์˜ ๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์—ญ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์„ ํ†ตํ•ด ๋ฏธ๋ฆฌ ๊ด€์ธกํ•œ ์…ˆ์ด๋ฏ€๋กœ ์–ธ์–ด ๋ชจ๋ธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์–ธ์–ด์˜ ๋ฌธ๋งฅ์ด๋ผ๋Š” ๊ฒƒ์€ ์‹ค์ œ๋กœ๋Š” ์–‘๋ฐฉํ–ฅ์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋‚˜ ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋“ฑ์—์„œ ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด์„œ ์ข‹์€ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ์„ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. ํ•˜์ง€๋งŒ ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ์˜ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์–‘๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, ๊ทธ ๋Œ€์•ˆ์œผ๋กœ ELMo์—์„œ๋Š” ์ˆœ๋ฐฉํ–ฅ๊ณผ ์—ญ๋ฐฉํ–ฅ์ด๋ผ๋Š” ๋‘ ๊ฐœ์˜ ๋‹จ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์„ ๋”ฐ๋กœ ์ค€๋น„ํ•˜์—ฌ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๊ธฐ์กด ์–ธ์–ด ๋ชจ๋ธ๋กœ๋Š” ์–‘๋ฐฉํ–ฅ ๊ตฌ์กฐ๋ฅผ ๋„์ž…ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, ์–‘๋ฐฉํ–ฅ ๊ตฌ์กฐ๋ฅผ ๋„์ž…ํ•˜๊ธฐ ์œ„ํ•ด์„œ 2018๋…„์—๋Š” ์ƒˆ๋กœ์šด ๊ตฌ์กฐ์˜ ์–ธ์–ด ๋ชจ๋ธ์ด ํƒ„์ƒํ–ˆ๋Š”๋ฐ ๋ฐ”๋กœ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. 3. ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ(Masked Language Model) ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์€ ์ž…๋ ฅ ํ…์ŠคํŠธ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ 15%์˜ ๋‹จ์–ด๋ฅผ ๋žœ๋ค์œผ๋กœ ๋งˆ์Šคํ‚น(Masking) ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งˆ์Šคํ‚น์ด๋ž€ ์›๋ž˜์˜ ๋‹จ์–ด๊ฐ€ ๋ฌด์—‡์ด์—ˆ๋Š”์ง€ ๋ชจ๋ฅด๊ฒŒ ํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—๊ฒŒ ์ด๋ ‡๊ฒŒ ๋งˆ์Šคํ‚น ๋œ ๋‹จ์–ด๋“ค์„(Masked words) ์˜ˆ์ธกํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์žฅ ์ค‘๊ฐ„์— ๊ตฌ๋ฉ์„ ๋šซ์–ด๋†“๊ณ , ๊ตฌ๋ฉ์— ๋“ค์–ด๊ฐˆ ๋‹จ์–ด๋“ค์„ ์˜ˆ์ธกํ•˜๊ฒŒ ํ•˜๋Š” ์‹์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์˜์–ด ์‹œํ—˜์„ ๋ณผ ๋•Œ ์ข…์ข… ๋งˆ์ฃผํ•˜๋Š” ๋นˆ์นธ ์ฑ„์šฐ๊ธฐ ๋ฌธ์ œ์— ๋น„์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด '๋‚˜๋Š” [MASK]์— ๊ฐ€์„œ ๊ทธ๊ณณ์—์„œ ๋นต๊ณผ [MASK]๋ฅผ ์ƒ€๋‹ค'๋ฅผ ์ฃผ๊ณ  [MASK]์— ๋“ค์–ด๊ฐˆ ๋‹จ์–ด๋ฅผ ๋งž์ถ”๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ๋Š” ์ด๋ฒˆ ์ฑ•ํ„ฐ์˜ ์ฃผ์ œ์ธ BERT๋ฅผ ์„ค๋ช…ํ•˜๋ฉฐ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 17-02 ๋ฒ„ํŠธ(Bidirectional Encoder Representations from Transformers, BERT) ํŠธ๋žœ์Šคํฌ๋จธ ์ฑ•ํ„ฐ์— ๋Œ€ํ•œ ์‚ฌ์ „ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. BERT(Bidirectional Encoder Representations from Transformers)๋Š” 2018๋…„์— ๊ตฌ๊ธ€์ด ๊ณต๊ฐœํ•œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. BERT๋ผ๋Š” ์ด๋ฆ„์€ ์„ธ์„œ๋ฏธ ์ŠคํŠธ๋ฆฌํŠธ๋ผ๋Š” ๋ฏธ๊ตญ ์ธํ˜•๊ทน์˜ ์บ๋ฆญํ„ฐ ์ด๋ฆ„์ด๊ธฐ๋„ ํ•œ๋ฐ, ์•ž์„œ ์†Œ๊ฐœํ•œ ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์ธ ELMo์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„ธ์„œ๋ฏธ ์ŠคํŠธ๋ฆฌํŠธ์˜ ์บ๋ฆญํ„ฐ ์ด๋ฆ„์„ ๋”ฐ์˜จ ๊ฒƒ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. BERT๋Š” 2018๋…„์— ๊ณต๊ฐœ๋˜์–ด ๋“ฑ์žฅ๊ณผ ๋™์‹œ์— ์ˆ˜๋งŽ์€ NLP ํƒœ์Šคํฌ์—์„œ ์ตœ๊ณ  ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๋ฉด์„œ ๋ช…์‹ค๊ณตํžˆ NLP์˜ ํ•œ ํš์„ ๊ทธ์€ ๋ชจ๋ธ๋กœ ํ‰๊ฐ€๋ฐ›๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” BERT์˜ ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ณ , ์ด์–ด์„œ BERT๋ฅผ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ BERT ์‹ค์Šต์„ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด transformers๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. pip install transformers 1. BERT์˜ ๊ฐœ์š” BERT๋Š” ์ด์ „ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์› ๋˜ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„๋˜์—ˆ์œผ๋ฉฐ, ์œ„ํ‚คํ”ผ๋””์•„(25์–ต ๋‹จ์–ด)์™€ BooksCorpus(8์–ต ๋‹จ์–ด)์™€ ๊ฐ™์€ ๋ ˆ์ด๋ธ”์ด ์—†๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. BERT๊ฐ€ ๋†’์€ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ์€, ๋ ˆ์ด๋ธ”์ด ์—†๋Š” ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ๋ชจ๋ธ์„ ๊ฐ€์ง€๊ณ , ๋ ˆ์ด๋ธ”์ด ์žˆ๋Š” ๋‹ค๋ฅธ ์ž‘์—…(Task)์—์„œ ์ถ”๊ฐ€ ํ›ˆ๋ จ๊ณผ ํ•จ๊ป˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์žฌ์กฐ์ •ํ•˜์—ฌ ์ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์ด ๋†’๊ฒŒ ๋‚˜์˜ค๋Š” ๊ธฐ์กด์˜ ์‚ฌ๋ก€๋“ค์„ ์ฐธ๊ณ ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ž‘์—…์— ๋Œ€ํ•ด์„œ ํŒŒ๋ผ๋ฏธํ„ฐ ์žฌ์กฐ์ •์„ ์œ„ํ•œ ์ถ”๊ฐ€ ํ›ˆ๋ จ ๊ณผ์ •์„ ํŒŒ์ธ ํŠœ๋‹(Fine-tuning)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ BERT์˜ ํŒŒ์ธ ํŠœ๋‹ ์‚ฌ๋ก€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํ•˜๊ณ  ์‹ถ์€ ํƒœ์Šคํฌ๊ฐ€ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ด๋ฏธ ์œ„ํ‚คํ”ผ๋””์•„ ๋“ฑ์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ BERT ์œ„์— ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ์‹ ๊ฒฝ๋ง์„ ํ•œ ์ธต ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋น„์œ ํ•˜์ž๋ฉด BERT๊ฐ€ ์–ธ์–ด ๋ชจ๋ธ ์‚ฌ์ „ ํ•™์Šต ๊ณผ์ •์—์„œ ์–ป์€ ์ง€์‹์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜์—์„œ ๋ณด๋‹ค ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „์— ์–ธ๊ธ‰ํ•œ ELMo๋‚˜ OpenAI GPT-1 ๋“ฑ์ด ์ด๋Ÿฌํ•œ ํŒŒ์ธ ํŠœ๋‹ ์‚ฌ๋ก€์˜ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ž…๋‹ˆ๋‹ค. 2. BERT์˜ ํฌ๊ธฐ BERT์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋”๋ฅผ ์Œ“์•„ ์˜ฌ๋ฆฐ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. Base ๋ฒ„์ „์—์„œ๋Š” ์ด 12๊ฐœ๋ฅผ ์Œ“์•˜์œผ๋ฉฐ, Large ๋ฒ„์ „์—์„œ๋Š” ์ด 24๊ฐœ๋ฅผ ์Œ“์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋„ Large ๋ฒ„์ „์€ Base ๋ฒ„์ „๋ณด๋‹ค d_model์˜ ํฌ๊ธฐ๋‚˜ ์…€ํ”„ ์–ดํ…์…˜ ํ—ค๋“œ(Self Attention Heads)์˜ ์ˆ˜๊ฐ€ ๋” ํฝ๋‹ˆ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ ์ธ์ฝ”๋” ์ธต์˜ ์ˆ˜๋ฅผ L, d_model์˜ ํฌ๊ธฐ๋ฅผ D, ์…€ํ”„ ์–ดํ…์…˜ ํ—ค๋“œ์˜ ์ˆ˜๋ฅผ A๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ๊ฐ๊ฐ์˜ ํฌ๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. BERT-Base : L=12, D=768, A=12 : 110M ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ BERT-Large : L=24, D=1024, A=16 : 340M ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ดˆ๊ธฐ ํŠธ๋žœ์Šคํฌ๋จธ ๋ชจ๋ธ(https://wikidocs.net/31379)์ด L=6, D=512, A=8์ด์—ˆ๋‹ค๋Š” ๊ฒƒ๊ณผ ๋น„๊ตํ•˜๋ฉด Base ๋˜ํ•œ ์ดˆ๊ธฐ ํŠธ๋žœ์Šคํฌ๋จธ๋ณด๋‹ค๋Š” ํฐ ๋„คํŠธ์›Œํฌ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ BERT-base๋Š” BERT๋ณด๋‹ค ์•ž์„œ ๋“ฑ์žฅํ•œ Open AI GPT-1๊ณผ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋™์ผํ•œ๋ฐ, ์ด๋Š” BERT ์—ฐ๊ตฌ์ง„์ด ์ง์ ‘์ ์œผ๋กœ GPT-1๊ณผ ์„ฑ๋Šฅ์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ GPT-1๊ณผ ๋™๋“ฑํ•œ ํฌ๊ธฐ๋กœ BERT-Base๋ฅผ ์„ค๊ณ„ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, BERT-Large๋Š” BERT์˜ ์ตœ๋Œ€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. BERT๊ฐ€ ์„ธ์šด ๊ธฐ๋ก๋“ค์€ ๋Œ€๋ถ€๋ถ„ BERT-Large๋ฅผ ํ†ตํ•ด ์ด๋ฃจ์–ด์กŒ์Šต๋‹ˆ๋‹ค. ์ด ๊ธ€์—์„œ๋Š” ์•ž์œผ๋กœ ํŽธ์˜๋ฅผ ์œ„ํ•ด BERT-BASE๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 3. BERT์˜ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ์ž„๋ฒ ๋”ฉ(Contextual Embedding) BERT๋Š” ELMo๋‚˜ GPT-1๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ์ž„๋ฒ ๋”ฉ(Contextual Embedding)์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. BERT์˜ ์ž…๋ ฅ์€ ์•ž์„œ ๋ฐฐ์šด ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)๋ฅผ ์ง€๋‚œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์ž…๋‹ˆ๋‹ค. d_model์„ 768๋กœ ์ •์˜ํ•˜์˜€์œผ๋ฏ€๋กœ, ๋ชจ๋“  ๋‹จ์–ด๋“ค์€ 768์ฐจ์›์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ๋˜์–ด BERT์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. BERT๋Š” ๋‚ด๋ถ€์ ์ธ ์—ฐ์‚ฐ์„ ๊ฑฐ์นœ ํ›„, ๋™์ผํ•˜๊ฒŒ ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ 768์ฐจ์›์˜ ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” BERT๊ฐ€ ๊ฐ 768์ฐจ์›์˜ [CLS], I, love, you๋ผ๋Š” 4๊ฐœ์˜ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ(์ž…๋ ฅ ์ž„๋ฒ ๋”ฉ) ๋™์ผํ•˜๊ฒŒ 768์ฐจ์›์˜ 4๊ฐœ์˜ ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ชจ์Šต(์ถœ๋ ฅ ์ž„๋ฒ ๋”ฉ)์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. BERT์˜ ์—ฐ์‚ฐ์„ ๊ฑฐ์นœ ํ›„์˜ ์ถœ๋ ฅ ์ž„๋ฒ ๋”ฉ์€ ๋ฌธ์žฅ์˜ ๋ฌธ๋งฅ์„ ๋ชจ๋‘ ์ฐธ๊ณ ํ•œ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ์ž„๋ฒ ๋”ฉ์ด ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ขŒ์ธก ๊ทธ๋ฆผ์—์„œ [CLS]๋ผ๋Š” ๋ฒกํ„ฐ๋Š” BERT์˜ ์ดˆ๊ธฐ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜์—ˆ์„ ์ž…๋ ฅ ์ž„๋ฒ ๋”ฉ ๋‹น์‹œ์—๋Š” ๋‹จ์ˆœํžˆ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)๋ฅผ ์ง€๋‚œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜€์ง€๋งŒ, BERT๋ฅผ ์ง€๋‚˜๊ณ  ๋‚˜์„œ๋Š” [CLS], I, love, you๋ผ๋Š” ๋ชจ๋“  ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์„ ๋ชจ๋‘ ์ฐธ๊ณ ํ•œ ํ›„์— ๋ฌธ๋งฅ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ขŒ์ธก ๊ทธ๋ฆผ์—์„œ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ฐธ๊ณ ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ ์„ ์˜ ํ™”์‚ดํ‘œ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Š” [CLS]๋ผ๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋ฒกํ„ฐ๋“ค๋„ ์ „๋ถ€ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์šฐ์ธก์˜ ๊ทธ๋ฆผ์—์„œ ์ถœ๋ ฅ ์ž„๋ฒ ๋”ฉ ๋‹จ๊ณ„์˜ love๋ฅผ ๋ณด๋ฉด BERT์˜ ์ž…๋ ฅ์ด์—ˆ๋˜ ๋ชจ๋“  ๋‹จ์–ด๋“ค์ธ [CLS], I, love, you๋ฅผ ์ฐธ๊ณ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋‹จ์–ด๊ฐ€ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ฐธ๊ณ ํ•˜๋Š” ์—ฐ์‚ฐ์€ ์‚ฌ์‹ค BERT์˜ 12๊ฐœ์˜ ์ธต์—์„œ ์ „๋ถ€ ์ด๋ฃจ์–ด์ง€๋Š” ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ 12๊ฐœ์˜ ์ธต์„ ์ง€๋‚œ ํ›„์— ์ตœ์ข…์ ์œผ๋กœ ์ถœ๋ ฅ ์ž„๋ฒ ๋”ฉ์„ ์–ป๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์œ„์˜ ๊ทธ๋ฆผ์€ BERT์˜ ์ฒซ ๋ฒˆ์งธ ์ธต์— ์ž…๋ ฅ๋œ ๊ฐ ๋‹จ์–ด๊ฐ€ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ฐธ๊ณ ํ•œ ํ›„์— ์ถœ๋ ฅ๋˜๋Š” ๊ณผ์ •์„ ํ™”์‚ดํ‘œ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. BERT์˜ ์ฒซ ๋ฒˆ์งธ ์ธต์˜ ์ถœ๋ ฅ ์ž„๋ฒ ๋”ฉ์€ BERT์˜ ๋‘ ๋ฒˆ์งธ ์ธต์—์„œ๋Š” ์ž…๋ ฅ ์ž„๋ฒ ๋”ฉ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด BERT๋Š” ์–ด๋–ป๊ฒŒ ๋ชจ๋“  ๋‹จ์–ด๋“ค์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ์ถœ๋ ฅ ์ž„๋ฒ ๋”ฉ์„ ์–ป๊ฒŒ ๋˜๋Š” ๊ฒƒ์ผ๊นŒ์š”? ์‚ฌ์‹ค ์ด์— ๋Œ€ํ•œ ํ•ด๋‹ต์„ ์—ฌ๋Ÿฌ๋ถ„์€ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋Š”๋ฐ, ๋ฐ”๋กœ '์…€ํ”„ ์–ดํ…์…˜'์ž…๋‹ˆ๋‹ค. BERT๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํŠธ๋žœ์Šคํฌ๋จธ ์ธ์ฝ”๋”๋ฅผ 12๋ฒˆ ์Œ“์€ ๊ฒƒ์ด๋ฏ€๋กœ ๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ ์ธต๋งˆ๋‹ค ๋ฉ€ํ‹ฐ ํ—ค๋“œ ์…€ํ”„ ์–ดํ…์…˜๊ณผ ํฌ์ง€์…˜ ์™€์ด์ฆˆ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 4. BERT์˜ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ € : WordPiece BERT๋Š” ๋‹จ์–ด๋ณด๋‹ค ๋” ์ž‘์€ ๋‹จ์œ„๋กœ ์ชผ๊ฐœ๋Š” ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. BERT๊ฐ€ ์‚ฌ์šฉํ•œ ํ† ํฌ ๋‚˜์ด์ €๋Š” WordPiece ํ† ํฌ ๋‚˜์ด์ €๋กœ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ € ์ฑ•ํ„ฐ์—์„œ ๊ณต๋ถ€ํ•œ ๋ฐ”์ดํŠธ ํŽ˜์–ด ์ธ์ฝ”๋”ฉ(Byte Pair Encoding, BPE)์˜ ์œ ์‚ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๋™์ž‘ ๋ฐฉ์‹์€ BPE์™€ ์กฐ๊ธˆ ๋‹ค๋ฅด์ง€๋งŒ, ๊ธ€์ž๋กœ๋ถ€ํ„ฐ ์„œ๋ธŒ ์›Œ๋“œ๋“ค์„ ๋ณ‘ํ•ฉํ•ด๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ์ตœ์ข… ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ BPE์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋Š” ๊ทธ๋Œ€๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์ถ”๊ฐ€ํ•˜์ง€๋งŒ, ์ž์ฃผ ๋“ฑ์žฅํ•˜์ง€ ์•Š๋Š” ๋‹จ์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋” ์ž‘์€ ๋‹จ์œ„์ธ ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์„œ๋ธŒ ์›Œ๋“œ๋“ค์ด ๋‹จ์–ด ์ง‘ํ•ฉ์— ์ถ”๊ฐ€๋œ๋‹ค๋Š” ์•„์ด๋””์–ด๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‹จ์–ด ์ง‘ํ•ฉ์ด ๋งŒ๋“ค์–ด์ง€๊ณ  ๋‚˜๋ฉด, ์ด ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋Œ€ํ‘œ์ ์ธ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ € ํŒจํ‚ค์ง€์ธ SentencePiece ์‹ค์Šต์„ ํ†ตํ•ด ์ดํ•ดํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. BERT์˜ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €๋„ ์ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. BERT์—์„œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ค€๋น„๋ฌผ : ์ด๋ฏธ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ๋‹จ์–ด ์ง‘ํ•ฉ 1. ํ† ํฐ์ด ๋‹จ์–ด ์ง‘ํ•ฉ์— ์กด์žฌํ•œ๋‹ค. => ํ•ด๋‹น ํ† ํฐ์„ ๋ถ„๋ฆฌํ•˜์ง€ ์•Š๋Š”๋‹ค. 2. ํ† ํฐ์ด ๋‹จ์–ด ์ง‘ํ•ฉ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. => ํ•ด๋‹น ํ† ํฐ์„ ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ถ„๋ฆฌํ•œ๋‹ค. => ํ•ด๋‹น ํ† ํฐ์˜ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธŒ ์›Œ๋“œ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์„œ๋ธŒ ์›Œ๋“œ๋“ค์€ ์•ž์— "##"๋ฅผ ๋ถ™์ธ ๊ฒƒ์„ ํ† ํฐ์œผ๋กœ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด embeddings์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์™”์„ ๋•Œ, BERT์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์— ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•˜๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๋งŒ์•ฝ, ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์•„๋‹Œ ํ† ํฌ ๋‚˜์ด์ €๋ผ๋ฉด ์—ฌ๊ธฐ์„œ OOV ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €์˜ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์กด์žฌํ•˜์ง€ ์•Š์•˜๋‹ค๊ณ  ํ•ด์„œ, ์„œ๋ธŒ ์›Œ๋“œ ๋˜ํ•œ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ๋Š” ์•„๋‹ˆ๋ฏ€๋กœ ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ๋” ์ชผ๊ฐœ๋ ค๊ณ  ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, BERT์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์— em, ##bed, ##ding, #s๋ผ๋Š” ์„œ๋ธŒ ์›Œ๋“œ๋“ค์ด ์กด์žฌํ•œ๋‹ค๋ฉด, embeddings๋Š” em, ##bed, ##ding, #s๋กœ ๋ถ„๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ##์€ ์ด ์„œ๋ธŒ ์›Œ๋“œ๋“ค์€ ๋‹จ์–ด์˜ ์ค‘๊ฐ„๋ถ€ํ„ฐ ๋“ฑ์žฅํ•˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ ์ƒ์„ฑ ์‹œ ํ‘œ์‹œํ•ด๋‘” ๊ธฐํ˜ธ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํ‘œ์‹œ๊ฐ€ ์žˆ์–ด์•ผ๋งŒ em, ##bed, ##ding, #s๋ฅผ ๋‹ค์‹œ ์†์‰ฝ๊ฒŒ embeddings๋กœ ๋ณต์›ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹ค์Šต์„ ํ†ตํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. transformers๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ BERT ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. import pandas as pd from transformers import BertTokenizer tokenizer = BertTokenizer.from_pretrained("bert-base-uncased") # Bert-base์˜ ํ† ํฌ ๋‚˜์ด์ € 'Here is the sentence I want embeddings for.'๋ผ๋Š” ๋ฌธ์žฅ์„ BERT์˜ ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์–ด๋–ป๊ฒŒ ํ† ํฐํ™”ํ•˜๋Š”์ง€ ๋ด…์‹œ๋‹ค. result = tokenizer.tokenize('Here is the sentence I want embeddings for.') print(result) ['here', 'is', 'the', 'sentence', 'i', 'want', 'em', '##bed', '##ding', '##s', 'for', '.'] embeddings๋ผ๋Š” ๋‹จ์–ด๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ em, ##bed, ##ding, #s๋กœ ๋ถ„๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ BERT์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์— ํŠน์ • ๋‹จ์–ด๊ฐ€ ์žˆ๋Š”์ง€ ์กฐํšŒํ•˜๋ ค๋ฉด. vocab[]์„ ํ†ตํ•ด์„œ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋‹จ์–ด 'here'์„ ์กฐํšŒํ•ด ๋ด…์‹œ๋‹ค. print(tokenizer.vocab['here']) 2182 ์ •์ˆ˜ 2182๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹จ์–ด here์ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์œ„ํ•ด์„œ ๋‹จ์–ด ์ง‘ํ•ฉ ๋‚ด๋ถ€์ ์œผ๋กœ 2182๋ผ๋Š” ์ •์ˆ˜๋กœ ๋งคํ•‘๋˜์–ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด embeddings๋ฅผ ์กฐํšŒํ•ด ๋ด…์‹œ๋‹ค. print(tokenizer.vocab['embeddings']) KeyError: 'embeddings' ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ์—์„œ KeyError๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๋‹จ์–ด em, ##bed, ##ing, ##s๋Š” ๋ชจ๋‘ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. print(tokenizer.vocab['em']) 7861 print(tokenizer.vocab['##bed']) 8270 print(tokenizer.vocab['##ding']) 4667 print(tokenizer.vocab['##s']) 2015 ์ด๋ฒˆ์—๋Š” BERT์˜ ๋‹จ์–ด ์ง‘ํ•ฉ ์ „์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. BERT์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์„ vocabulary.txt์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. # BERT์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์„ vocabulary.txt์— ์ €์žฅ with open('vocabulary.txt', 'w') as f: for token in tokenizer.vocab.keys(): f.write(token + '\n') vocabulary.txt ํŒŒ์ผ์„ ์ง์ ‘ ์—ด์–ด์„œ ์‚ดํŽด๋ณผ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ํ˜•ํƒœ๋กœ ์ €์žฅํ•ด์„œ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. df = pd.read_fwf('vocabulary.txt', header=None) df print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :',len(df)) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 30522 BERT์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 30,522์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์ธ๋ฑ์Šค๊ฐ€ ํ•ด๋‹น ๋‹จ์–ด์™€ ๋งคํ•‘๋œ ์ •์ˆ˜์ด๋ฏ€๋กœ ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋งคํ•‘๋œ ๋‹จ์–ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, 4667๋ฒˆ ๋‹จ์–ด๋Š” ##ding์ž…๋‹ˆ๋‹ค. df.loc[4667].values[0] ##ding ์ฐธ๊ณ ๋กœ BERT์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํŠน๋ณ„ ํ† ํฐ๋“ค๊ณผ ๊ทธ์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. [PAD] - 0 [UNK] - 100 [CLS] - 101 [SEP] - 102 [MASK] - 103 ์ž„์˜๋กœ 102๋ฒˆ ํ† ํฐ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. df.loc[102].values[0] [SEP] ์ด ํŠน๋ณ„ ํ† ํฐ๋“ค์ด ์–ด๋–ค ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 5. ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ(Position Embedding) ํŠธ๋žœ์Šคํฌ๋จธ์—์„œ๋Š” ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ(Positional Encoding)์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด์„œ ๋‹จ์–ด์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ํฌ์ง€์…”๋„ ์ธ์ฝ”๋”ฉ์€ ์‚ฌ์ธ ํ•จ์ˆ˜์™€ ์ฝ”์‚ฌ์ธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์น˜์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ์„ ๋งŒ๋“ค์–ด ์ด๋ฅผ ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค๊ณผ ๋”ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. BERT์—์„œ๋Š” ์ด์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, ์œ„์น˜ ์ •๋ณด๋ฅผ ์‚ฌ์ธ ํ•จ์ˆ˜์™€ ์ฝ”์‚ฌ์ธ ํ•จ์ˆ˜๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํ•™์Šต์„ ํ†ตํ•ด์„œ ์–ป๋Š” ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ(Position Embedding)์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์šฐ์„ , ์œ„์˜ ๊ทธ๋ฆผ์—์„œ WordPiece Embedding์€ ์šฐ๋ฆฌ๊ฐ€ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋Š” ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ์œผ๋กœ ์‹ค์งˆ์ ์ธ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์ž…๋ ฅ์— ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ์„ ํ†ตํ•ด์„œ ์œ„์น˜ ์ •๋ณด๋ฅผ ๋”ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ์˜ ์•„์ด๋””์–ด๋Š” ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•œ๋ฐ, ์œ„์น˜ ์ •๋ณด๋ฅผ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)์„ ํ•˜๋‚˜ ๋” ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ 4๋ผ๋ฉด 4๊ฐœ์˜ ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  BERT์˜ ์ž…๋ ฅ๋งˆ๋‹ค ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋”ํ•ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ + 0๋ฒˆ ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๋‘ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ + 1๋ฒˆ ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ์„ธ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ + 2๋ฒˆ ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๋„ค ๋ฒˆ์งธ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ + 3๋ฒˆ ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ์‹ค์ œ BERT์—์„œ๋Š” ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ 512๋กœ ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ์ด 512๊ฐœ์˜ ํฌ์ง€์…˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ ํ˜„์žฌ ์„ค๋ช…ํ•œ ๋‚ด์šฉ์„ ๊ธฐ์ค€์œผ๋กœ๋Š” BERT์—์„œ๋Š” ์ด ๋‘ ๊ฐœ์˜ ์ž„๋ฒ ๋”ฉ ์ธต์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ 30,522๊ฐœ์ธ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ ์ธต๊ณผ ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 512์ด๋ฏ€๋กœ 512๊ฐœ์˜ ํฌ์ง€์…˜ ๋ฒกํ„ฐ๋ฅผ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ ์ธต์ž…๋‹ˆ๋‹ค. ์‚ฌ์‹ค BERT๋Š” ์„ธ๊ทธ๋จผํŠธ ์ž„๋ฒ ๋”ฉ(Segment Embedding)์ด๋ผ๋Š” 1๊ฐœ์˜ ์ž„๋ฒ ๋”ฉ ์ธต์„ ๋” ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์— ์–ธ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. 6. BERT์˜ ์‚ฌ์ „ ํ›ˆ๋ จ(Pre-training) ์œ„์˜ ๊ทธ๋ฆผ์€ BERT์˜ ๋…ผ๋ฌธ์— ์ฒจ๋ถ€๋œ ๊ทธ๋ฆผ์œผ๋กœ ELMo์™€ GPT-1, ๊ทธ๋ฆฌ๊ณ  BERT์˜ ๊ตฌ์กฐ์ ์ธ ์ฐจ์ด๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์šฐ์ธก ๊ทธ๋ฆผ์˜ ELMo๋Š” ์ •๋ฐฉํ–ฅ LSTM๊ณผ ์—ญ๋ฐฉํ–ฅ LSTM์„ ๊ฐ๊ฐ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ๋ฐฉ์‹์œผ๋กœ ์–‘๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์šด๋ฐ ๊ทธ๋ฆผ์˜ GPT-1์€ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋””์ฝ”๋”๋ฅผ ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋‹จ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. Trm์€ ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‹จ๋ฐฉํ–ฅ(โ†’)์œผ๋กœ ์„ค๊ณ„๋œ Open AI GPT์™€ ๋‹ฌ๋ฆฌ ๊ฐ€์žฅ ์ขŒ์ธก ๊ทธ๋ฆผ์˜ BERT๋Š” ํ™”์‚ดํ‘œ๊ฐ€ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ๋ป—์–ด๋‚˜๊ฐ€๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋Š” ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ(Masked Language Model)์„ ํ†ตํ•ด ์–‘๋ฐฉํ–ฅ์„ฑ์„ ์–ป์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. BERT์˜ ์‚ฌ์ „ ํ›ˆ๋ จ ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š” ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์ด๊ณ , ๋‘ ๋ฒˆ์งธ๋Š” ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก(Next sentence prediction, NSP)์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์— ๋”ฐ๋ฅด๋ฉด BERT๋Š” BookCorpus(8์–ต ๋‹จ์–ด)์™€ ์œ„ํ‚คํ”ผ๋””์•„(25์–ต ๋‹จ์–ด)๋กœ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 1) ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ(Masked Language Model, MLM) BERT๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ์„ ์œ„ํ•ด์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด๊ฐ€๋Š” ์ž…๋ ฅ ํ…์ŠคํŠธ์˜ 15%์˜ ๋‹จ์–ด๋ฅผ ๋žœ๋ค์œผ๋กœ ๋งˆ์Šคํ‚น(Masking) ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—๊ฒŒ ์ด ๊ฐ€๋ ค์ง„ ๋‹จ์–ด๋“ค์„(Masked words) ์˜ˆ์ธกํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์ค‘๊ฐ„์— ๋‹จ์–ด๋“ค์— ๊ตฌ๋ฉ์„ ๋šซ์–ด๋†“๊ณ , ๊ตฌ๋ฉ์— ๋“ค์–ด๊ฐˆ ๋‹จ์–ด๋“ค์„ ์˜ˆ์ธกํ•˜๊ฒŒ ํ•˜๋Š” ์‹์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด '๋‚˜๋Š” [MASK]์— ๊ฐ€์„œ ๊ทธ๊ณณ์—์„œ ๋นต๊ณผ [MASK]๋ฅผ ์ƒ€๋‹ค'๋ฅผ ์ฃผ๊ณ  '์Šˆํผ'์™€ '์šฐ์œ '๋ฅผ ๋งž์ถ”๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํžˆ๋Š” ์ „๋ถ€ [MASK]๋กœ ๋ณ€๊ฒฝํ•˜์ง€๋Š” ์•Š๊ณ , ๋žœ๋ค์œผ๋กœ ์„ ํƒ๋œ 15%์˜ ๋‹จ์–ด๋“ค์€ ๋‹ค์‹œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋น„์œจ๋กœ ๊ทœ์น™์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. 80%์˜ ๋‹จ์–ด๋“ค์€ [MASK]๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. Ex) The man went to the store โ†’ The man went to the [MASK] 10%์˜ ๋‹จ์–ด๋“ค์€ ๋žœ๋ค์œผ๋กœ ๋‹จ์–ด๊ฐ€ ๋ณ€๊ฒฝ๋œ๋‹ค. Ex) The man went to the store โ†’ The man went to the dog 10%์˜ ๋‹จ์–ด๋“ค์€ ๋™์ผํ•˜๊ฒŒ ๋‘”๋‹ค. Ex) The man went to the store โ†’ The man went to the store ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ์ด์œ ๋Š” [MASK]๋งŒ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” [MASK] ํ† ํฐ์ด ํŒŒ์ธ ํŠœ๋‹ ๋‹จ๊ณ„์—์„œ๋Š” ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‚ฌ์ „ ํ•™์Šต ๋‹จ๊ณ„์™€ ํŒŒ์ธ ํŠœ๋‹ ๋‹จ๊ณ„์—์„œ์˜ ๋ถˆ์ผ์น˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ์™„ํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋žœ๋ค์œผ๋กœ ์„ ํƒ๋œ 15%์˜ ๋‹จ์–ด๋“ค์˜ ๋ชจ๋“  ํ† ํฐ์„ [MASK]๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ „์ฒด ๋‹จ์–ด ๊ด€์ ์—์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ํ†ตํ•ด ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋‹จ์–ด์˜ 85%๋Š” ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์˜ ํ•™์Šต์— ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์˜ ํ•™์Šต์— ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์–ด๋Š” ์ „์ฒด ๋‹จ์–ด์˜ 15%์ž…๋‹ˆ๋‹ค. ํ•™์Šต์— ์‚ฌ์šฉ๋˜๋Š” 12%๋Š” [MASK]๋กœ ๋ณ€๊ฒฝ ํ›„์— ์›๋ž˜ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. 1.5%๋Š” ๋žœ๋ค์œผ๋กœ ๋‹จ์–ด๊ฐ€ ๋ณ€๊ฒฝ๋œ ํ›„์— ์›๋ž˜ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. 1.5%๋Š” ๋‹จ์–ด๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€๋Š” ์•Š์•˜์ง€๋งŒ, BERT๋Š” ์ด ๋‹จ์–ด๊ฐ€ ๋ณ€๊ฒฝ๋œ ๋‹จ์–ด์ธ์ง€ ์›๋ž˜ ๋‹จ์–ด๊ฐ€ ๋งž๋Š”์ง€๋Š” ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์—๋„ BERT๋Š” ์›๋ž˜ ๋‹จ์–ด๊ฐ€ ๋ฌด์—‡์ธ์ง€๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 'My dog is cute. he likes playing'์ด๋ผ๋Š” ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์•ฝ๊ฐ„์˜ ์ „์ฒ˜๋ฆฌ์™€ BERT์˜ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €์— ์˜ํ•ด ์ด ๋ฌธ์žฅ์€ ['my', 'dog', 'is' 'cute', 'he', 'likes', 'play', '##ing']๋กœ ํ† ํฐ ํ™”๊ฐ€ ๋˜์–ด BERT์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์–ธ์–ด ๋ชจ๋ธ ํ•™์Šต์„ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. 'dog' ํ† ํฐ์€ [MASK]๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ 'dog' ํ† ํฐ์ด [MASK]๋กœ ๋ณ€๊ฒฝ๋˜์–ด์„œ BERT ๋ชจ๋ธ์ด ์›๋ž˜ ๋‹จ์–ด๋ฅผ ๋งž์ถ”๋ ค๊ณ  ํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ถœ๋ ฅ์ธต์— ์žˆ๋Š” ๋‹ค๋ฅธ ์œ„์น˜์˜ ๋ฒกํ„ฐ๋“ค์€ ์˜ˆ์ธก๊ณผ ํ•™์Šต์— ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ , ์˜ค์ง 'dog' ์œ„์น˜์˜ ์ถœ๋ ฅ์ธต์˜ ๋ฒกํ„ฐ๋งŒ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ๋Š” BERT์˜ ์†์‹ค ํ•จ์ˆ˜์—์„œ ๋‹ค๋ฅธ ์œ„์น˜์—์„œ์˜ ์˜ˆ์ธก์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์—์„œ๋Š” ์˜ˆ์ธก์„ ์œ„ํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒํผ์˜ ๋ฐ€์ง‘์ธต(Dense layer)์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋œ 1๊ฐœ์˜ ์ธต์„ ์‚ฌ์šฉํ•˜์—ฌ ์›๋ž˜ ๋‹จ์–ด๊ฐ€ ๋ฌด์—‡์ธ์ง€๋ฅผ ๋งž์ถ”๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ 'dog'๋งŒ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ ์…‹์ด ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค๋ฉด ์–ด๋–จ๊นŒ์š”? ์ด๋ฒˆ์—๋Š” ์„ธ ๊ฐ€์ง€ ์œ ํ˜• ๋ชจ๋‘์— ๋Œ€ํ•ด์„œ ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. 'dog' ํ† ํฐ์€ [MASK]๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 'he'๋Š” ๋žœ๋ค ๋‹จ์–ด 'king'์œผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 'play'๋Š” ๋ณ€๊ฒฝ๋˜์ง„ ์•Š์•˜์ง€๋งŒ ์˜ˆ์ธก์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. BERT๋Š” ๋žœ๋ค ๋‹จ์–ด 'king'์œผ๋กœ ๋ณ€๊ฒฝ๋œ ํ† ํฐ์— ๋Œ€ํ•ด์„œ๋„ ์›๋ž˜ ๋‹จ์–ด๊ฐ€ ๋ฌด์—‡์ธ์ง€, ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์€ ๋‹จ์–ด 'play'์— ๋Œ€ํ•ด์„œ๋„ ์›๋ž˜ ๋‹จ์–ด๊ฐ€ ๋ฌด์—‡์ธ์ง€๋ฅผ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 'play'๋Š” ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์•˜์ง€๋งŒ BERT ์ž…์žฅ์—์„œ๋Š” ์ด๊ฒƒ์ด ๋ณ€๊ฒฝ๋œ ๋‹จ์–ด์ธ์ง€ ์•„๋‹Œ์ง€ ๋ชจ๋ฅด๋ฏ€๋กœ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์›๋ž˜ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. BERT๋Š” ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ ์™ธ์—๋„ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก์ด๋ผ๋Š” ๋˜ ๋‹ค๋ฅธ ํƒœ์Šคํฌ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 2) ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก(Next Sentence Prediction, NSP) BERT๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ์ค€ ํ›„์— ์ด ๋ฌธ์žฅ์ด ์ด์–ด์ง€๋Š” ๋ฌธ์žฅ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋งž์ถ”๋Š” ๋ฐฉ์‹์œผ๋กœ ํ›ˆ๋ จ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ 50:50 ๋น„์œจ๋กœ ์‹ค์ œ ์ด์–ด์ง€๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ๊ณผ ๋žœ๋ค์œผ๋กœ ์ด์–ด๋ถ™์ธ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ์ฃผ๊ณ  ํ›ˆ๋ จ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐ๊ฐ Sentence A์™€ Sentence B๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ๋‹ค์Œ์˜ ์˜ˆ๋Š” ๋ฌธ์žฅ์˜ ์—ฐ์†์„ฑ์„ ํ™•์ธํ•œ ๊ฒฝ์šฐ์™€ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด์–ด์ง€๋Š” ๋ฌธ์žฅ์˜ ๊ฒฝ์šฐ Sentence A : The man went to the store. Sentence B : He bought a gallon of milk. Label = IsNextSentence ์ด์–ด์ง€๋Š” ๋ฌธ์žฅ์ด ์•„๋‹Œ ๊ฒฝ์šฐ ๊ฒฝ์šฐ Sentence A : The man went to the store. Sentence B : dogs are so cute. Label = NotNextSentence BERT์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์„ ๋•Œ์—๋Š” [SEP]๋ผ๋Š” ํŠน๋ณ„ ํ† ํฐ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์˜ ๋์— [SEP] ํ† ํฐ์„ ๋„ฃ๊ณ , ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด ๋๋‚˜๋ฉด ์—ญ์‹œ [SEP] ํ† ํฐ์„ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‘ ๋ฌธ์žฅ์ด ์‹ค์ œ ์ด์–ด์ง€๋Š” ๋ฌธ์žฅ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ [CLS] ํ† ํฐ์˜ ์œ„์น˜์˜ ์ถœ๋ ฅ์ธต์—์„œ ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. [CLS] ํ† ํฐ์€ BERT๊ฐ€ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€๋œ ํŠน๋ณ„ ํ† ํฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ๋‚˜ํƒ€๋‚œ ๊ฒƒ๊ณผ ๊ฐ™์ด ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก์€ ๋”ฐ๋กœ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ loss๋ฅผ ํ•ฉํ•˜์—ฌ ํ•™์Šต์ด ๋™์‹œ์— ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. BERT๊ฐ€ ์–ธ์–ด ๋ชจ๋ธ ์™ธ์—๋„ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก์ด๋ผ๋Š” ํƒœ์Šคํฌ๋ฅผ ํ•™์Šตํ•˜๋Š” ์ด์œ ๋Š” BERT๊ฐ€ ํ’€๊ณ ์ž ํ•˜๋Š” ํƒœ์Šคํฌ ์ค‘์—์„œ๋Š” QA(Question Answering)๋‚˜ NLI(Natural Language Inference)์™€ ๊ฐ™์ด ๋‘ ๋ฌธ์žฅ์˜ ๊ด€๊ณ„๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ ํƒœ์Šคํฌ๋“ค์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 7. ์„ธ๊ทธ๋จผํŠธ ์ž„๋ฒ ๋”ฉ(Segment Embedding) ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด BERT๋Š” QA ๋“ฑ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ ์ž…๋ ฅ์ด ํ•„์š”ํ•œ ํƒœ์Šคํฌ๋ฅผ ํ’€๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์žฅ ๊ตฌ๋ถ„์„ ์œ„ํ•ด์„œ BERT๋Š” ์„ธ๊ทธ๋จผํŠธ ์ž„๋ฒ ๋”ฉ์ด๋ผ๋Š” ๋˜ ๋‹ค๋ฅธ ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์—๋Š” Sentence 0 ์ž„๋ฒ ๋”ฉ, ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์—๋Š” Sentence 1 ์ž„๋ฒ ๋”ฉ์„ ๋”ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด๋ฉฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ๋‘ ๊ฐœ๋งŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ BERT๋Š” ์ด 3๊ฐœ์˜ ์ž„๋ฒ ๋”ฉ ์ธต์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. WordPiece Embedding : ์‹ค์งˆ์ ์ธ ์ž…๋ ฅ์ด ๋˜๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ข…๋ฅ˜๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋กœ 30,522๊ฐœ. Position Embedding : ์œ„์น˜ ์ •๋ณด๋ฅผ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ข…๋ฅ˜๋Š” ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด์ธ 512๊ฐœ. Segment Embedding : ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ข…๋ฅ˜๋Š” ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜์ธ 2๊ฐœ. ์ฃผ์˜ํ•  ์ ์€ ๋งŽ์€ ๋ฌธํ—Œ์—์„œ BERT๊ฐ€ ๋ฌธ์žฅ ์ค‘๊ฐ„์˜ [SEP] ํ† ํฐ๊ณผ ๋‘ ์ข…๋ฅ˜์˜ ์„ธ๊ทธ๋จผํŠธ ์ž„๋ฒ ๋”ฉ์„ ํ†ตํ•ด์„œ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์„ค๋ช…ํ•˜๊ณ  ์žˆ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ BERT์— ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ๋“ค์–ด๊ฐ„๋‹ค๋Š” ํ‘œํ˜„์—์„œ์˜ ๋ฌธ์žฅ์ด๋ผ๋Š” ๊ฒƒ์€ ์‹ค์ œ ์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” ๋ฌธ์žฅ์˜ ๋‹จ์œ„๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด QA ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒฝ์šฐ์—๋Š” [SEP]์™€ ์„ธ๊ทธ๋จผํŠธ ์ž„๋ฒ ๋”ฉ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜๋Š” [์งˆ๋ฌธ(Question), ๋ณธ๋ฌธ(Paragraph)] ๋‘ ์ข…๋ฅ˜์˜ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›์ง€๋งŒ, Paragraph 1๊ฐœ๋Š” ์‹ค์ œ๋กœ๋Š” ๋‹ค์ˆ˜์˜ ๋ฌธ์žฅ์œผ๋กœ ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด [SEP]์™€ ์„ธ๊ทธ๋จผํŠธ ์ž„๋ฒ ๋”ฉ์œผ๋กœ ๊ตฌ๋ถ„๋˜๋Š” BERT์˜ ์ž…๋ ฅ์—์„œ์˜ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์€ ์‹ค์ œ๋กœ๋Š” ๋‘ ์ข…๋ฅ˜์˜ ํ…์ŠคํŠธ, ๋‘ ๊ฐœ์˜ ๋ฌธ์„œ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BERT๊ฐ€ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ์ž…๋ ฅ๋ฐ›์„ ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜๋‚˜ IMDB ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜์™€ ๊ฐ™์€ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ ํƒœ์Šคํฌ์—์„œ๋Š” ํ•œ ๊ฐœ์˜ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ๋งŒ ๋ถ„๋ฅ˜๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ, ์ด ๊ฒฝ์šฐ์—๋Š” BERT์˜ ์ „์ฒด ์ž…๋ ฅ์— Sentence 0 ์ž„๋ฒ ๋”ฉ๋งŒ์„ ๋”ํ•ด์ค๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์„ค๋ช…ํ•  1)๊ณผ 2) ํŒŒ์ธ ํŠœ๋‹ ์œ ํ˜•์ด ๊ทธ ์˜ˆ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. 8. BERT๋ฅผ ํŒŒ์ธ ํŠœ๋‹(Fine-tuning) ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์‚ฌ์ „ ํ•™์Šต๋œ BERT์— ์šฐ๋ฆฌ๊ฐ€ ํ’€๊ณ ์ž ํ•˜๋Š” ํƒœ์Šคํฌ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ํ•™์Šต ์‹œ์ผœ์„œ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋‹จ๊ณ„์ธ ํŒŒ์ธ ํŠœ๋‹ ๋‹จ๊ณ„์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์งˆ์ ์œผ๋กœ ํƒœ์Šคํฌ์— BERT๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋‹จ๊ณ„์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 1) ํ•˜๋‚˜์˜ ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์œ ํ˜•(Single Text Classification) BERT๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์œ ํ˜•์€ ํ•˜๋‚˜์˜ ๋ฌธ์„œ์— ๋Œ€ํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์œ ํ˜•์ž…๋‹ˆ๋‹ค. ์ด ์œ ํ˜•์€ ์˜ํ™” ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜, ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ถ„๋ฅ˜ ๋“ฑ๊ณผ ๊ฐ™์ด ์ž…๋ ฅ๋œ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ๋ถ„๋ฅ˜๋ฅผ ํ•˜๋Š” ์œ ํ˜•์œผ๋กœ ๋ฌธ์„œ์˜ ์‹œ์ž‘์— [CLS]๋ผ๋Š” ํ† ํฐ์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์‚ฌ์ „ ํ›ˆ๋ จ ๋‹จ๊ณ„์—์„œ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก์„ ์„ค๋ช…ํ•  ๋•Œ, [CLS] ํ† ํฐ์€ BERT๊ฐ€ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ํŠน๋ณ„ ํ† ํฐ์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” BERT๋ฅผ ์‹ค์งˆ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋‹จ๊ณ„์ธ ํŒŒ์ธ ํŠœ๋‹ ๋‹จ๊ณ„์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ [CLS] ํ† ํฐ์˜ ์œ„์น˜์˜ ์ถœ๋ ฅ์ธต์—์„œ ๋ฐ€์ง‘์ธต(Dense layer) ๋˜๋Š” ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ๋Š” ์™„์ „ ์—ฐ๊ฒฐ์ธต(fully-connected layer)์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ์ธต๋“ค์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ถ„๋ฅ˜์— ๋Œ€ํ•œ ์˜ˆ์ธก์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 2) ํ•˜๋‚˜์˜ ํ…์ŠคํŠธ์— ๋Œ€ํ•œ ํƒœ๊น… ์ž‘์—…(Tagging) BERT๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋‘ ๋ฒˆ์งธ ์œ ํ˜•์€ ํƒœ๊น… ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์•ž์„œ RNN ๊ณ„์—ด์˜ ์‹ ๊ฒฝ๋ง๋“ค์„ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋˜ ํƒœ์Šคํฌ์ž…๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ๋ฌธ์žฅ์˜ ๊ฐ ๋‹จ์–ด์— ํ’ˆ์‚ฌ๋ฅผ ํƒœ๊น… ํ•˜๋Š” ํ’ˆ์‚ฌ ํƒœ๊น… ์ž‘์—…๊ณผ ๊ฐœ์ฒด๋ฅผ ํƒœ๊น… ํ•˜๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹ ์ž‘์—…์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์—์„œ๋Š” ์ž…๋ ฅ ํ…์ŠคํŠธ์˜ ๊ฐ ํ† ํฐ์˜ ์œ„์น˜์— ๋ฐ€์ง‘์ธต์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„๋ฅ˜์— ๋Œ€ํ•œ ์˜ˆ์ธก์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 3) ํ…์ŠคํŠธ์˜ ์Œ์— ๋Œ€ํ•œ ๋ถ„๋ฅ˜ ๋˜๋Š” ํšŒ๊ท€ ๋ฌธ์ œ(Text Pair Classification or Regression) BERT๋Š” ํ…์ŠคํŠธ์˜ ์Œ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š” ํƒœ์Šคํฌ๋„ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ์˜ ์Œ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š” ๋Œ€ํ‘œ์ ์ธ ํƒœ์Šคํฌ๋กœ ์ž์—ฐ์–ด ์ถ”๋ก (Natural language inference)์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ถ”๋ก  ๋ฌธ์ œ๋ž€, ๋‘ ๋ฌธ์žฅ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์ด ๋‹ค๋ฅธ ๋ฌธ์žฅ๊ณผ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์–ด๋–ค ๊ด€๊ณ„์— ์žˆ๋Š”์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ ํ˜•์œผ๋กœ๋Š” ๋ชจ์ˆœ ๊ด€๊ณ„(contradiction), ํ•จ์˜ ๊ด€๊ณ„(entailment), ์ค‘๋ฆฝ ๊ด€๊ณ„(neutral)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ์˜ ์Œ์„ ์ž…๋ ฅ๋ฐ›๋Š” ์ด๋Ÿฌํ•œ ํƒœ์Šคํฌ์˜ ๊ฒฝ์šฐ์—๋Š” ์ž…๋ ฅ ํ…์ŠคํŠธ๊ฐ€ 1๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ, ํ…์ŠคํŠธ ์‚ฌ์ด์— [SEP] ํ† ํฐ์„ ์ง‘์–ด๋„ฃ๊ณ , Sentence 0 ์ž„๋ฒ ๋”ฉ๊ณผ Sentence 1 ์ž„๋ฒ ๋”ฉ์ด๋ผ๋Š” ๋‘ ์ข…๋ฅ˜์˜ ์„ธ๊ทธ๋จผํŠธ ์ž„๋ฒ ๋”ฉ์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. 4) ์งˆ์˜์‘๋‹ต(Question Answering) ํ…์ŠคํŠธ์˜ ์Œ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š” ๋˜ ๋‹ค๋ฅธ ํƒœ์Šคํฌ๋กœ QA(Question Answering)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. BERT๋กœ QA๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ ์งˆ๋ฌธ๊ณผ ๋ณธ๋ฌธ์ด๋ผ๋Š” ๋‘ ๊ฐœ์˜ ํ…์ŠคํŠธ์˜ ์Œ์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด ํƒœ์Šคํฌ์˜ ๋Œ€ํ‘œ์ ์ธ ๋ฐ์ดํ„ฐ ์…‹์œผ๋กœ SQuAD(Stanford Question Answering Dataset) v1.1์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ ์…‹์„ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์€ ์งˆ๋ฌธ๊ณผ ๋ณธ๋ฌธ์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด, ๋ณธ๋ฌธ์˜ ์ผ๋ถ€๋ถ„์„ ์ถ”์ถœํ•ด์„œ ์งˆ๋ฌธ์— ๋‹ต๋ณ€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ด ๋ฐ์ดํ„ฐ ์…‹์€ ์˜์–ด๋กœ ๋˜์–ด์žˆ์ง€๋งŒ ํ•œ๊ตญ์–ด๋กœ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. "๊ฐ•์šฐ๊ฐ€ ๋–จ์–ด์ง€๋„๋ก ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒƒ์€?"๋ผ๋Š” ์งˆ๋ฌธ์ด ์ฃผ์–ด์ง€๊ณ , "๊ธฐ์ƒํ•™์—์„œ ๊ฐ•์šฐ๋Š” ๋Œ€๊ธฐ ์ˆ˜์ฆ๊ธฐ๊ฐ€ ์‘๊ฒฐ๋˜์–ด ์ค‘๋ ฅ์˜ ์˜ํ–ฅ์„ ๋ฐ›๊ณ  ๋–จ์–ด์ง€๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ•์šฐ์˜ ์ฃผ์š” ํ˜•ํƒœ๋Š” ์ด์Šฌ๋น„, ๋น„, ์ง„๋ˆˆ๊นจ๋น„, ๋ˆˆ, ์‹ธ๋ฝ๋ˆˆ ๋ฐ ์šฐ๋ฐ•์ด ์žˆ์Šต๋‹ˆ๋‹ค."๋ผ๋Š” ๋ณธ๋ฌธ์ด ์ฃผ์–ด์กŒ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ์ด ๊ฒฝ์šฐ, ์ •๋‹ต์€ "์ค‘๋ ฅ"์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 9. ๊ทธ ์™ธ ๊ธฐํƒ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์œ„ํ‚คํ”ผ๋””์•„(25์–ต ๋‹จ์–ด)์™€ BooksCorpus(8์–ต ๋‹จ์–ด) โ‰ˆ 33์–ต ๋‹จ์–ด WordPiece ํ† ํฌ ๋‚˜์ด์ €๋กœ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ ํ›„ 15% ๋น„์œจ์— ๋Œ€ํ•ด์„œ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ ํ•™์Šต ๋‘ ๋ฌธ์žฅ Sentence A์™€ B์˜ ํ•ฉํ•œ ๊ธธ์ด. ์ฆ‰, ์ตœ๋Œ€ ์ž…๋ ฅ์˜ ๊ธธ์ด๋Š” 512๋กœ ์ œํ•œ 100๋งŒ step ํ›ˆ๋ จ โ‰ˆ (์ดํ•ฉ 33์–ต ๋‹จ์–ด ์ฝ”ํผ์Šค์— ๋Œ€ํ•ด 40 ์—ํฌํฌ ํ•™์Šต) ์˜ตํ‹ฐ๋งˆ์ด์ € : ์•„๋‹ด(Adam) ํ•™์Šต๋ฅ (learning rate) : 10 4 ๊ฐ€์ค‘์น˜ ๊ฐ์†Œ(Weight Decay) : L2 ์ •๊ทœํ™”๋กœ 0.01 ์ ์šฉ ๋“œ๋กญ์•„์›ƒ : ๋ชจ๋“  ๋ ˆ์ด์–ด์— ๋Œ€ํ•ด์„œ 0.1 ์ ์šฉ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ : relu ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ gelu ํ•จ์ˆ˜ ๋ฐฐ์น˜ ํฌ๊ธฐ(Batch size) : 256 10. ์–ดํ…์…˜ ๋งˆ์Šคํฌ(Attention Mask) BERT๋ฅผ ์‹ค์ œ๋กœ ์‹ค์Šตํ•˜๊ฒŒ ๋˜๋ฉด ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋ผ๋Š” ์‹œํ€€์Šค ์ž…๋ ฅ์ด ์ถ”๊ฐ€๋กœ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋Š” BERT๊ฐ€ ์–ดํ…์…˜ ์—ฐ์‚ฐ์„ ํ•  ๋•Œ, ๋ถˆํ•„์š”ํ•˜๊ฒŒ ํŒจ๋”ฉ ํ† ํฐ์— ๋Œ€ํ•ด์„œ ์–ดํ…์…˜์„ ํ•˜์ง€ ์•Š๋„๋ก ์‹ค์ œ ๋‹จ์–ด์™€ ํŒจ๋”ฉ ํ† ํฐ์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ์•Œ๋ ค์ฃผ๋Š” ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ 0๊ณผ 1 ๋‘ ๊ฐ€์ง€ ๊ฐ’์„ ๊ฐ€์ง€๋Š”๋ฐ, ์ˆซ์ž 1์€ ํ•ด๋‹น ํ† ํฐ์€ ์‹ค์ œ ๋‹จ์–ด์ด๋ฏ€๋กœ ๋งˆ์Šคํ‚น์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ผ๋Š” ์˜๋ฏธ์ด๊ณ , ์ˆซ์ž 0์€ ํ•ด๋‹น ํ† ํฐ์€ ํŒจ๋”ฉ ํ† ํฐ์ด๋ฏ€๋กœ ๋งˆ์Šคํ‚น์„ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์‹ค์ œ ๋‹จ์–ด์˜ ์œ„์น˜์—๋Š” 1, ํŒจ๋”ฉ ํ† ํฐ์˜ ์œ„์น˜์—๋Š” 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์‹œํ€€์Šค๋ฅผ ๋งŒ๋“ค์–ด BERT์˜ ๋˜ ๋‹ค๋ฅธ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ BERT๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹ค์ œ๋กœ ์ด์ง„ ๋ถ„๋ฅ˜, ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜, ๊ฐœ์ฒด๋ช… ์ธ์‹, QA(Question Answering)์„ ํ’€๋ฉฐ BERT์— ๋Œ€ํ•œ ํŒŒ์ธ ํŠœ๋‹ ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 17-03 ๊ตฌ๊ธ€ BERT์˜ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ(Masked Language Model) ์‹ค์Šต ๋ชจ๋“  BERT ์‹ค์Šต์€ Colab์—์„œ ์ง„ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต๋œ ํ•œ๊ตญ์–ด BERT๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์„ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์„ ์œ„ํ•ด์„œ๋งŒ์ด ์•„๋‹ˆ๋ผ ์•ž์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ BERT๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” transformers๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‹ค์Šต ํ™˜๊ฒฝ์— transformers ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด๋‘ก์‹œ๋‹ค. pip install transformers 1. ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ € transformers ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. BERT๋Š” ์ด๋ฏธ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ํ•™์Šตํ•ด๋‘” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ•ญ์ƒ ๋งคํ•‘ ๊ด€๊ณ„์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ A๋ผ๋Š” ์ด๋ฆ„์˜ BERT๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, B๋ผ๋Š” ์ด๋ฆ„์˜ BERT์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ์€ ํ…์ŠคํŠธ๋ฅผ ์ œ๋Œ€๋กœ ์ดํ•ดํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. A๋ผ๋Š” BERT์˜ ํ† ํฌ ๋‚˜์ด์ €๋Š” '์‚ฌ๊ณผ'๋ผ๋Š” ๋‹จ์–ด๋ฅผ 36๋ฒˆ์œผ๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋ฐ˜๋ฉด์—, B๋ผ๋Š” BERT์˜ ํ† ํฌ ๋‚˜์ด์ €๋Š” '์‚ฌ๊ณผ'๋ผ๋Š” ๋‹จ์–ด๋ฅผ 42๋ฒˆ์œผ๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋“ฑ ๋‹จ์–ด์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ ์ •๋ณด ์ž์ฒด๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. from transformers import TFBertForMaskedLM from transformers import AutoTokenizer TFBertForMaskedLM.from_pretrained('BERT ๋ชจ๋ธ ์ด๋ฆ„')์„ ๋„ฃ์œผ๋ฉด [MASK]๋ผ๊ณ  ๋˜์–ด์žˆ๋Š” ๋‹จ์–ด๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•œ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์œ„ํ•œ ๊ตฌ์กฐ๋กœ BERT๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ BERT๋ฅผ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ ํ˜•ํƒœ๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. AutoTokenizer.from_pretrained('๋ชจ๋ธ ์ด๋ฆ„')์„ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น ๋ชจ๋ธ์ด ํ•™์Šต๋˜์—ˆ์„ ๋‹น์‹œ์— ์‚ฌ์šฉ๋˜์—ˆ๋˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. model = TFBertForMaskedLM.from_pretrained('bert-large-uncased') tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased") 2. BERT์˜ ์ž…๋ ฅ ''Soccer is a really fun [MASK]'๋ผ๋Š” ์ž„์˜์˜ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋ฅผ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์œผ๋ฉด, ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์€ [MASK]์˜ ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด์„œ bert-large-uncased์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ฌธ์žฅ์„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ•ด๋ด…์‹œ๋‹ค. inputs = tokenizer('Soccer is a really fun [MASK].', return_tensors='tf') ํ† ํฌ ๋‚˜์ด์ €๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ์—์„œ input_ids๋ฅผ ํ†ตํ•ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(inputs['input_ids']) tf.Tensor([[ 101 4715 2003 1037 2428 4569 103 1012 102]], shape=(1, 9), dtype=int32) ํ† ํฌ ๋‚˜์ด์ €๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ์—์„œ token_type_ids๋ฅผ ํ†ตํ•ด์„œ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์„ธ๊ทธ๋จผํŠธ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(inputs['token_type_ids']) tf.Tensor([[0 0 0 0 0 0 0 0 0]], shape=(1, 9), dtype=int32) ํ˜„์žฌ์˜ ์ž…๋ ฅ์€ ๋ฌธ์žฅ์ด ๋‘ ๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•œ ๊ฐœ์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์žฅ ๊ธธ์ด๋งŒํผ์˜ 0 ์‹œํ€€์Šค๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฌธ์žฅ์ด ๋‘ ๊ฐœ์˜€๋‹ค๋ฉด ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด ์‹œ์ž‘๋˜๋Š” ๊ตฌ๊ฐ„๋ถ€ํ„ฐ๋Š” 1์˜ ์‹œํ€€์Šค๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ํ•ด๋‹น๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ์—์„œ attention_mask๋ฅผ ํ†ตํ•ด์„œ ์‹ค์ œ ๋‹จ์–ด์™€ ํŒจ๋”ฉ ํ† ํฐ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์šฉ๋„์ธ ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(inputs['attention_mask']) tf.Tensor([[1 1 1 1 1 1 1 1 1]], shape=(1, 9), dtype=int32) ํ˜„์žฌ์˜ ์ž…๋ ฅ์—์„œ๋Š” ํŒจ๋”ฉ์ด ์—†์œผ๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์žฅ ๊ธธ์ด๋งŒํผ์˜ 1 ์‹œํ€€์Šค๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋’ค์— ํŒจ๋”ฉ์ด ์žˆ์—ˆ๋‹ค๋ฉด ํŒจ๋”ฉ์ด ์‹œ์ž‘๋˜๋Š” ๊ตฌ๊ฐ„๋ถ€ํ„ฐ๋Š” 0์˜ ์‹œํ€€์Šค๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ํ•ด๋‹น๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ข€ ๋” ๋‹ค์–‘ํ•œ ํŒจํ„ด์˜ ์ž…๋ ฅ์€ ๋’ค์˜ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜, ๊ฐœ์ฒด๋ช… ์ธ์‹, ์งˆ์˜์‘๋‹ต ์‹ค์Šต์—์„œ ์ด์–ด์„œ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3. [MASK] ํ† ํฐ ์˜ˆ์ธกํ•˜๊ธฐ FillMaskPipeline์€ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ง€์ •ํ•˜๋ฉด ์†์‰ฝ๊ฒŒ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ์ •๋ฆฌํ•ด์„œ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. FillMaskPipeline์— ์šฐ์„  ์•ž์„œ ๋ถˆ๋Ÿฌ์˜จ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค. from transformers import FillMaskPipeline pip = FillMaskPipeline(model=model, tokenizer=tokenizer) ์ด์ œ ์ž…๋ ฅ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ [MASK]์˜ ์œ„์น˜์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ƒ์œ„ 5๊ฐœ์˜ ํ›„๋ณด ๋‹จ์–ด๋“ค์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. pip('Soccer is a really fun [MASK].') [{'score': 0.762112021446228, 'sequence': 'soccer is a really fun sport.', 'token': 4368, 'token_str': 'sport'}, {'score': 0.2034197747707367, 'sequence': 'soccer is a really fun game.', 'token': 2208, 'token_str': 'game'}, {'score': 0.012208552099764347, 'sequence': 'soccer is a really fun thing.', 'token': 2518, 'token_str': 'thing'}, {'score': 0.0018630230333656073, 'sequence': 'soccer is a really fun activity.', 'token': 4023, 'token_str': 'activity'}, {'score': 0.001335485139861703, 'sequence': 'soccer is a really fun field.', 'token': 2492, 'token_str': 'field'}] pip('The Avengers is a really fun [MASK].') [{'score': 0.2562903165817261, 'sequence': 'the avengers is a really fun show.', 'token': 2265, 'token_str': 'show'}, {'score': 0.1728411316871643, 'sequence': 'the avengers is a really fun movie.', 'token': 3185, 'token_str': 'movie'}, {'score': 0.11107689887285233, 'sequence': 'the avengers is a really fun story.', 'token': 2466, 'token_str': 'story'}, {'score': 0.07248972356319427, 'sequence': 'the avengers is a really fun series.', 'token': 2186, 'token_str': 'series'}, {'score': 0.07046619802713394, 'sequence': 'the avengers is a really fun film.', 'token': 2143, 'token_str': 'film'}] pip('I went to [MASK] this morning.') [{'score': 0.35730746388435364, 'sequence': 'i went to work this morning.', 'token': 2147, 'token_str': 'work'}, {'score': 0.23304426670074463, 'sequence': 'i went to bed this morning.', 'token': 2793, 'token_str': 'bed'}, {'score': 0.12845049798488617, 'sequence': 'i went to school this morning.', 'token': 2082, 'token_str': 'school'}, {'score': 0.062305748462677, 'sequence': 'i went to sleep this morning.', 'token': 3637, 'token_str': 'sleep'}, {'score': 0.04695260152220726, 'sequence': 'i went to class this morning.', 'token': 2465, 'token_str': 'class'}] 17-04 ํ•œ๊ตญ์–ด BERT์˜ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ(Masked Language Model) ์‹ค์Šต ๋ชจ๋“  BERT ์‹ค์Šต์€ Colab์—์„œ ์ง„ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต๋œ ํ•œ๊ตญ์–ด BERT๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์„ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์„ ์œ„ํ•ด์„œ๋งŒ์ด ์•„๋‹ˆ๋ผ ์•ž์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ BERT๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” transformers๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‹ค์Šต ํ™˜๊ฒฝ์— transformers ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด๋‘ก์‹œ๋‹ค. pip install transformers 1. ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ € transformers ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. BERT๋Š” ์ด๋ฏธ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ํ•™์Šตํ•ด๋‘” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ•ญ์ƒ ๋งคํ•‘ ๊ด€๊ณ„์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ A๋ผ๋Š” ์ด๋ฆ„์˜ BERT๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, B๋ผ๋Š” ์ด๋ฆ„์˜ BERT์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ์€ ํ…์ŠคํŠธ๋ฅผ ์ œ๋Œ€๋กœ ์ดํ•ดํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. A๋ผ๋Š” BERT์˜ ํ† ํฌ ๋‚˜์ด์ €๋Š” '์‚ฌ๊ณผ'๋ผ๋Š” ๋‹จ์–ด๋ฅผ 36๋ฒˆ์œผ๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋ฐ˜๋ฉด์—, B๋ผ๋Š” BERT์˜ ํ† ํฌ ๋‚˜์ด์ €๋Š” '์‚ฌ๊ณผ'๋ผ๋Š” ๋‹จ์–ด๋ฅผ 42๋ฒˆ์œผ๋กœ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋“ฑ ๋‹จ์–ด์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ ์ •๋ณด ์ž์ฒด๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. klue/bert-base๋Š” ๋Œ€ํ‘œ์ ์ธ ํ•œ๊ตญ์–ด BERT์ž…๋‹ˆ๋‹ค. klue/bert-base์˜ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ๊ณผ klue/bert-base์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ด ๋ด…์‹œ๋‹ค. from transformers import TFBertForMaskedLM from transformers import AutoTokenizer TFBertForMaskedLM.from_pretrained('BERT ๋ชจ๋ธ ์ด๋ฆ„')์„ ๋„ฃ์œผ๋ฉด [MASK]๋ผ๊ณ  ๋˜์–ด์žˆ๋Š” ๋‹จ์–ด๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•œ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์œ„ํ•œ ๊ตฌ์กฐ๋กœ BERT๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ BERT๋ฅผ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ ํ˜•ํƒœ๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. from_pt=True๋Š” ํ•ด๋‹น ๋ชจ๋ธ์ด ๊ธฐ์กด์—๋Š” ํ…์„œ ํ”Œ๋กœ๊ฐ€ ์•„๋‹ˆ๋ผ ํŒŒ์ด ํ† ์น˜๋กœ ํ•™์Šต๋œ ๋ชจ๋ธ์ด์—ˆ์ง€๋งŒ ์ด๋ฅผ ํ…์„œ ํ”Œ๋กœ์—์„œ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. AutoTokenizer.from_pretrained('๋ชจ๋ธ ์ด๋ฆ„')์„ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น ๋ชจ๋ธ์ด ํ•™์Šต๋˜์—ˆ์„ ๋‹น์‹œ์— ์‚ฌ์šฉ๋˜์—ˆ๋˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. model = TFBertForMaskedLM.from_pretrained('klue/bert-base', from_pt=True) tokenizer = AutoTokenizer.from_pretrained("klue/bert-base") 2. BERT์˜ ์ž…๋ ฅ '์ถ•๊ตฌ๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” [MASK]๋‹ค'๋ผ๋Š” ์ž„์˜์˜ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋ฅผ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์œผ๋ฉด, ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์€ [MASK]์˜ ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด์„œ klue/bert-base์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ฌธ์žฅ์„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ•ด๋ด…์‹œ๋‹ค. inputs = tokenizer('์ถ•๊ตฌ๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” [MASK]๋‹ค.', return_tensors='tf') ํ† ํฌ ๋‚˜์ด์ €๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ์—์„œ input_ids๋ฅผ ํ†ตํ•ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(inputs['input_ids']) tf.Tensor([[ 2 4713 2259 3944 6001 2259 4 809 18 3]], shape=(1, 10), dtype=int32) ํ† ํฌ ๋‚˜์ด์ €๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ์—์„œ token_type_ids๋ฅผ ํ†ตํ•ด์„œ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์„ธ๊ทธ๋จผํŠธ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(inputs['token_type_ids']) tf.Tensor([[0 0 0 0 0 0 0 0 0 0]], shape=(1, 10), dtype=int32) ํ˜„์žฌ์˜ ์ž…๋ ฅ์€ ๋ฌธ์žฅ์ด ๋‘ ๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•œ ๊ฐœ์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์žฅ ๊ธธ์ด๋งŒํผ์˜ 0 ์‹œํ€€์Šค๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฌธ์žฅ์ด ๋‘ ๊ฐœ์˜€๋‹ค๋ฉด ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด ์‹œ์ž‘๋˜๋Š” ๊ตฌ๊ฐ„๋ถ€ํ„ฐ๋Š” 1์˜ ์‹œํ€€์Šค๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ํ•ด๋‹น๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ์—์„œ attention_mask๋ฅผ ํ†ตํ•ด์„œ ์‹ค์ œ ๋‹จ์–ด์™€ ํŒจ๋”ฉ ํ† ํฐ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์šฉ๋„์ธ ์–ดํ…์…˜ ๋งˆ์Šคํฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(inputs['attention_mask']) tf.Tensor([[1 1 1 1 1 1 1 1 1 1]], shape=(1, 10), dtype=int32) ํ˜„์žฌ์˜ ์ž…๋ ฅ์—์„œ๋Š” ํŒจ๋”ฉ์ด ์—†์œผ๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์žฅ ๊ธธ์ด๋งŒํผ์˜ 1 ์‹œํ€€์Šค๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋’ค์— ํŒจ๋”ฉ์ด ์žˆ์—ˆ๋‹ค๋ฉด ํŒจ๋”ฉ์ด ์‹œ์ž‘๋˜๋Š” ๊ตฌ๊ฐ„๋ถ€ํ„ฐ๋Š” 0์˜ ์‹œํ€€์Šค๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ํ•ด๋‹น๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ข€ ๋” ๋‹ค์–‘ํ•œ ํŒจํ„ด์˜ ์ž…๋ ฅ์€ ๋’ค์˜ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜, ๊ฐœ์ฒด๋ช… ์ธ์‹, ์งˆ์˜์‘๋‹ต ์‹ค์Šต์—์„œ ์ด์–ด์„œ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3. [MASK] ํ† ํฐ ์˜ˆ์ธกํ•˜๊ธฐ FillMaskPipeline์€ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ง€์ •ํ•˜๋ฉด ์†์‰ฝ๊ฒŒ ๋งˆ์Šคํฌ ๋“œ ์–ธ์–ด ๋ชจ๋ธ์˜ ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ์ •๋ฆฌํ•ด์„œ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. FillMaskPipeline์— ์šฐ์„  ์•ž์„œ ๋ถˆ๋Ÿฌ์˜จ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค. from transformers import FillMaskPipeline pip = FillMaskPipeline(model=model, tokenizer=tokenizer) ์ด์ œ ์ž…๋ ฅ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ [MASK]์˜ ์œ„์น˜์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ƒ์œ„ 5๊ฐœ์˜ ํ›„๋ณด ๋‹จ์–ด๋“ค์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. pip('์ถ•๊ตฌ๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” [MASK]๋‹ค.') [{'score': 0.8963505625724792, 'sequence': '์ถ•๊ตฌ๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” ์Šคํฌ์ธ  ๋‹ค.', 'token': 4559, 'token_str': '์Šคํฌ์ธ '}, {'score': 0.02595764957368374, 'sequence': '์ถ•๊ตฌ๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” ๊ฑฐ ๋‹ค.', 'token': 568, 'token_str': '๊ฑฐ'}, {'score': 0.010033931583166122, 'sequence': '์ถ•๊ตฌ๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” ๊ฒฝ๊ธฐ ๋‹ค.', 'token': 3682, 'token_str': '๊ฒฝ๊ธฐ'}, {'score': 0.007924391888082027, 'sequence': '์ถ•๊ตฌ๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” ์ถ•๊ตฌ ๋‹ค.', 'token': 4713, 'token_str': '์ถ•๊ตฌ'}, {'score': 0.00784421805292368, 'sequence': '์ถ•๊ตฌ๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” ๋†€์ด ๋‹ค.', 'token': 5845, 'token_str': '๋†€์ด'}] pip('์–ด๋ฒค์ €์Šค๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” [MASK]๋‹ค.') [{'score': 0.8382411599159241, 'sequence': '์–ด๋ฒค์ €์Šค๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” ์˜ํ™” ๋‹ค.', 'token': 3771, 'token_str': '์˜ํ™”'}, {'score': 0.028275618329644203, 'sequence': '์–ด๋ฒค์ €์Šค๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” ๊ฑฐ ๋‹ค.', 'token': 568, 'token_str': '๊ฑฐ'}, {'score': 0.017189407721161842, 'sequence': '์–ด๋ฒค์ €์Šค๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” ๋“œ๋ผ๋งˆ ๋‹ค.', 'token': 4665, 'token_str': '๋“œ๋ผ๋งˆ'}, {'score': 0.014989694580435753, 'sequence': '์–ด๋ฒค์ €์Šค๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” ์ด์•ผ๊ธฐ ๋‹ค.', 'token': 3758, 'token_str': '์ด์•ผ๊ธฐ'}, {'score': 0.009382619522511959, 'sequence': '์–ด๋ฒค์ €์Šค๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ๋Š” ์žฅ์†Œ ๋‹ค.', 'token': 4938, 'token_str': '์žฅ์†Œ'}] pip('๋‚˜๋Š” ์˜ค๋Š˜ ์•„์นจ์— [MASK]์— ์ถœ๊ทผ์„ ํ–ˆ๋‹ค.') [{'score': 0.08012567460536957, 'sequence': '๋‚˜๋Š” ์˜ค๋Š˜ ์•„์นจ์— ํšŒ์‚ฌ์— ์ถœ๊ทผ์„ ํ–ˆ๋‹ค.', 'token': 3769, 'token_str': 'ํšŒ์‚ฌ'}, {'score': 0.06124098226428032, 'sequence': '๋‚˜๋Š” ์˜ค๋Š˜ ์•„์นจ์— ์— ์ถœ๊ทผ์„ ํ–ˆ๋‹ค.', 'token': 1, 'token_str': '[UNK]'}, {'score': 0.017486684024333954, 'sequence': '๋‚˜๋Š” ์˜ค๋Š˜ ์•„์นจ์— ๊ณต์žฅ์— ์ถœ๊ทผ์„ ํ–ˆ๋‹ค.', 'token': 4345, 'token_str': '๊ณต์žฅ'}, {'score': 0.016131816431879997, 'sequence': '๋‚˜๋Š” ์˜ค๋Š˜ ์•„์นจ์— ์‚ฌ๋ฌด์‹ค์— ์ถœ๊ทผ์„ ํ–ˆ๋‹ค.', 'token': 5841, 'token_str': '์‚ฌ๋ฌด์‹ค'}, {'score': 0.015360789373517036, 'sequence': '๋‚˜๋Š” ์˜ค๋Š˜ ์•„์นจ์— ์„œ์šธ์— ์ถœ๊ทผ์„ ํ–ˆ๋‹ค.', 'token': 3671, 'token_str': '์„œ์šธ'}] 17-05 ๊ตฌ๊ธ€ BERT์˜ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก(Next Sentence Prediction) ๋ชจ๋“  BERT ์‹ค์Šต์€ Colab์—์„œ ์ง„ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต๋œ ํ•œ๊ตญ์–ด BERT๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก์„ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์„ ์œ„ํ•ด์„œ๋งŒ์ด ์•„๋‹ˆ๋ผ ์•ž์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ BERT๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” transformers๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‹ค์Šต ํ™˜๊ฒฝ์— transformers ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด๋‘ก์‹œ๋‹ค. pip install transformers 1. ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ € transformers ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. BERT๋Š” ์ด๋ฏธ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ํ•™์Šตํ•ด๋‘” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ•ญ์ƒ ๋งคํ•‘ ๊ด€๊ณ„์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. import tensorflow as tf from transformers import TFBertForNextSentencePrediction from transformers import AutoTokenizer TFBertForNextSentencePrediction.from_pretrained('BERT ๋ชจ๋ธ ์ด๋ฆ„')์„ ๋„ฃ์œผ๋ฉด ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์ด์–ด์ง€๋Š” ๋ฌธ์žฅ ๊ด€๊ณ„์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” BERT ๊ตฌ์กฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. AutoTokenizer.from_pretrained('๋ชจ๋ธ ์ด๋ฆ„')์„ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น ๋ชจ๋ธ์ด ํ•™์Šต๋˜์—ˆ์„ ๋‹น์‹œ์— ์‚ฌ์šฉ๋˜์—ˆ๋˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. model = TFBertForNextSentencePrediction.from_pretrained('bert-base-uncased') tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased') 2. BERT์˜ ์ž…๋ ฅ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก์„ ์œ„ํ•ด ๋ฌธ๋งฅ ์ƒ์œผ๋กœ ์‹ค์ œ๋กœ ์ด์–ด์ง€๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced." next_sentence = "pizza is eaten with the use of a knife and fork. In casual settings, however, it is cut into wedges to be eaten while held in the hand." ์•ž์„œ ์ค€๋น„ํ•œ bert-base-uncased์˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ•ด๋ด…์‹œ๋‹ค. encoding = tokenizer(prompt, next_sentence, return_tensors='tf') ํ† ํฌ ๋‚˜์ด์ €๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ์—์„œ input_ids๋ฅผ ํ†ตํ•ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(encoding['input_ids']) <tf.Tensor: shape=(1, 58), dtype=int32, numpy= array([[ 101, 1999, 3304, 1010, 10733, 2366, 1999, 5337, 10906, 1010, 2107, 2004, 2012, 1037, 4825, 1010, 2003, 3591, 4895, 14540, 6610, 2094, 1012, 102, 10733, 2003, 8828, 2007, 1996, 2224, 1997, 1037, 5442, 1998, 9292, 1012, 1999, 10017, 10906, 1010, 2174, 1010, 2009, 2003, 3013, 2046, 17632, 2015, 2000, 2022, 8828, 2096, 2218, 1999, 1996, 2192, 1012, 102]], dtype=int32)> ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ ์—ฌ๊ธฐ์„œ 101๊ณผ 102๋Š” ํŠน๋ณ„ ํ† ํฐ์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ํ•ด๋‹น ํ† ํฌ ๋‚˜์ด์ €์˜ [CLS] ํ† ํฐ๊ณผ [SEP] ํ† ํฐ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(tokenizer.cls_token, ':', tokenizer.cls_token_id) print(tokenizer.sep_token, ':' , tokenizer.sep_token_id) [CLS] : 101 [SEP] : 102 ์œ„์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ๋””์ฝ”๋”ฉ ํ•ด๋ณด๋ฉด ํ˜„์žฌ ์ž…๋ ฅ์˜ ๊ตฌ์„ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(tokenizer.decode(encoding['input_ids'][0])) [CLS] in italy, pizza served in formal settings, such as at a restaurant, is presented unsliced. [SEP] pizza is eaten with the use of a knife and fork. in casual settings, however, it is cut into wedges to be eaten while held in the hand. [SEP] BERT์—์„œ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด๊ฐˆ ๊ฒฝ์šฐ์—๋Š” ๋งจ ์•ž์—๋Š” [CLS] ํ† ํฐ์ด ์กด์žฌํ•˜๊ณ , ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด ๋๋‚˜๋ฉด [SEP] ํ† ํฐ, ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด ์ข…๋ฃŒ๋˜์—ˆ์„ ๋•Œ ๋‹ค์‹œ ์ถ”๊ฐ€์ ์œผ๋กœ [SEP] ํ† ํฐ์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ํ† ํฌ ๋‚˜์ด์ €๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ์—์„œ token_type_ids๋ฅผ ํ†ตํ•ด์„œ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์„ธ๊ทธ๋จผํŠธ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(encoding['token_type_ids']) tf.Tensor( [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]], shape=(1, 58), dtype=int32) 0์ด ์—ฐ์†์ ์œผ๋กœ ๋“ฑ์žฅํ•˜๋‹ค๊ฐ€ ์–ด๋Š ์ˆœ๊ฐ„๋ถ€ํ„ฐ 1์ด ์—ฐ์†์ ์œผ๋กœ ๋“ฑ์žฅํ•˜๋Š”๋ฐ, ์ด๋Š” [CLS] ํ† ํฐ์˜ ์œ„์น˜๋ถ€ํ„ฐ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ์ด ๋๋‚˜๊ณ  ๋‚˜์„œ ๋“ฑ์žฅํ•œ [SEP] ํ† ํฐ๊นŒ์ง€์˜ ์œ„์น˜์—๋Š” 0์ด ๋“ฑ์žฅํ•˜๊ณ , ๋‹ค์Œ ๋‘ ๋ฒˆ์งธ ๋ฌธ์žฅ๋ถ€ํ„ฐ๋Š” 1์ด ๋“ฑ์žฅํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. token_type_ids์—์„œ๋Š” 0๊ณผ 1๋กœ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 3. ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธกํ•˜๊ธฐ ์ด์ œ TFBertForNextSentencePrediction๋ฅผ ํ†ตํ•ด์„œ ๋‹ค์Œ ๋ฌธ์žฅ์„ ์˜ˆ์ธกํ•ด ๋ด…์‹œ๋‹ค. ๋ชจ๋ธ์— ์ž…๋ ฅ์„ ๋„ฃ์œผ๋ฉด, ํ•ด๋‹น ๋ชจ๋ธ์€ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ธฐ ์ „์˜ ๊ฐ’์ธ logits์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฐ’์„ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผ์‹œํ‚จ ํ›„์— ๊ฐ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•œ ํ™•๋ฅ  ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. logits = model(encoding['input_ids'], token_type_ids=encoding['token_type_ids'])[0] softmax = tf.keras.layers.Softmax() probs = softmax(logits) print(probs) tf.Tensor([[9.9999714e-01 2.8381858e-06]], shape=(1, 2), dtype=float32) 0๋ฒˆ ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ํ™•๋ฅ  ๊ฐ’์ด 1๋ฒˆ ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ํ™•๋ฅ  ๊ฐ’๋ณด๋‹ค ํ›จ์”ฌ ํฝ๋‹ˆ๋‹ค. ์‹ค์งˆ์ ์œผ๋กœ ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•œ ๋ ˆ์ด๋ธ”์€ 0์ด๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋‘ ๊ฐœ์˜ ๊ฐ’ ์ค‘ ๋” ํฐ ๊ฐ’์„ ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’์œผ๋กœ ํŒ๋‹จํ•˜๋„๋ก ๋” ํฐ ํ™•๋ฅ  ๊ฐ’์„ ๊ฐ€์ง„ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. print('์ตœ์ข… ์˜ˆ์ธก ๋ ˆ์ด๋ธ” :', tf.math.argmax(probs, axis=-1).numpy()) ์ตœ์ข… ์˜ˆ์ธก ๋ ˆ์ด๋ธ” : [0] ์ตœ์ข… ์˜ˆ์ธก ๋ ˆ์ด๋ธ”์€ 0์ž…๋‹ˆ๋‹ค. ์ด๋Š” BERT๊ฐ€ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก์„ ํ•™์Šตํ–ˆ์„ ๋‹น์‹œ์— ์‹ค์งˆ์ ์œผ๋กœ ์ด์–ด์ง€๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์˜ ๋ ˆ์ด๋ธ”์€ 0. ์ด์–ด์ง€์ง€ ์•Š๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ ˆ์ด๋ธ”์„ 1๋กœ ๋‘๊ณ ์„œ ์ด์ง„ ๋ถ„๋ฅ˜๋กœ ํ•™์Šต์„ ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ด์–ด์ง€์ง€ ์•Š๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์œผ๋กœ ํ…Œ์ŠคํŠธํ•ด ๋ด…์‹œ๋‹ค. ์ „์ฒด์ ์ธ ๊ณผ์ •์€ ์ด์ „๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ์ƒ๊ด€์—†๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced." next_sentence = "The sky is blue due to the shorter wavelength of blue light." encoding = tokenizer(prompt, next_sentence, return_tensors='tf') logits = model(encoding['input_ids'], token_type_ids=encoding['token_type_ids'])[0] softmax = tf.keras.layers.Softmax() probs = softmax(logits) print('์ตœ์ข… ์˜ˆ์ธก ๋ ˆ์ด๋ธ” :', tf.math.argmax(probs, axis=-1).numpy()) ์ตœ์ข… ์˜ˆ์ธก ๋ ˆ์ด๋ธ” : [1] 17-06 ํ•œ๊ตญ์–ด BERT์˜ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก(Next Sentence Prediction) ๋ชจ๋“  BERT ์‹ค์Šต์€ Colab์—์„œ ์ง„ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต๋œ ํ•œ๊ตญ์–ด BERT๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก์„ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์„ ์œ„ํ•ด์„œ๋งŒ์ด ์•„๋‹ˆ๋ผ ์•ž์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ BERT๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” transformers๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‹ค์Šต ํ™˜๊ฒฝ์— transformers ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด๋‘ก์‹œ๋‹ค. pip install transformers 1. ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ € transformers ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. BERT๋Š” ์ด๋ฏธ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ํ•™์Šตํ•ด๋‘” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ•ญ์ƒ ๋งคํ•‘ ๊ด€๊ณ„์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. import tensorflow as tf from transformers import TFBertForNextSentencePrediction from transformers import AutoTokenizer TFBertForNextSentencePrediction.from_pretrained('BERT ๋ชจ๋ธ ์ด๋ฆ„')์„ ๋„ฃ์œผ๋ฉด ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์ด์–ด์ง€๋Š” ๋ฌธ์žฅ ๊ด€๊ณ„์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” BERT ๊ตฌ์กฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. AutoTokenizer.from_pretrained('๋ชจ๋ธ ์ด๋ฆ„')์„ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น ๋ชจ๋ธ์ด ํ•™์Šต๋˜์—ˆ์„ ๋‹น์‹œ์— ์‚ฌ์šฉ๋˜์—ˆ๋˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. model = TFBertForNextSentencePrediction.from_pretrained('klue/bert-base', from_pt=True) tokenizer = AutoTokenizer.from_pretrained("klue/bert-base") 2. ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธกํ•˜๊ธฐ ์ด์ œ TFBertForNextSentencePrediction๋ฅผ ํ†ตํ•ด์„œ ๋‹ค์Œ ๋ฌธ์žฅ์„ ์˜ˆ์ธกํ•ด ๋ด…์‹œ๋‹ค. ๋ชจ๋ธ์— ์ž…๋ ฅ์„ ๋„ฃ์œผ๋ฉด, ํ•ด๋‹น ๋ชจ๋ธ์€ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ธฐ ์ „์˜ ๊ฐ’์ธ logits์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฐ’์„ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผ์‹œํ‚จ ํ›„ ๋‘ ๊ฐœ์˜ ๊ฐ’ ์ค‘ ๋” ํฐ ๊ฐ’์„ ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’์œผ๋กœ ํŒ๋‹จํ•˜๋„๋ก ๋” ํฐ ํ™•๋ฅ  ๊ฐ’์„ ๊ฐ€์ง„ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. # ์ด์–ด์ง€๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ prompt = "2002๋…„ ์›”๋“œ์ปต ์ถ•๊ตฌ ๋Œ€ํšŒ๋Š” ์ผ๋ณธ๊ณผ ๊ณต๋™์œผ๋กœ ๊ฐœ์ตœ๋˜์—ˆ๋˜ ์„ธ๊ณ„์ ์ธ ํฐ ์ž”์น˜์ž…๋‹ˆ๋‹ค." next_sentence = "์—ฌํ–‰์„ ๊ฐ€๋ณด๋‹ˆ ํ•œ๊ตญ์˜ 2002๋…„ ์›”๋“œ์ปต ์ถ•๊ตฌ ๋Œ€ํšŒ์˜ ์ค€๋น„๋Š” ์™„๋ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค." encoding = tokenizer(prompt, next_sentence, return_tensors='tf') logits = model(encoding['input_ids'], token_type_ids=encoding['token_type_ids'])[0] softmax = tf.keras.layers.Softmax() probs = softmax(logits) print('์ตœ์ข… ์˜ˆ์ธก ๋ ˆ์ด๋ธ” :', tf.math.argmax(probs, axis=-1).numpy()) ์ตœ์ข… ์˜ˆ์ธก ๋ ˆ์ด๋ธ” : [0] ์ตœ์ข… ์˜ˆ์ธก ๋ ˆ์ด๋ธ”์€ 0์ž…๋‹ˆ๋‹ค. ์ด๋Š” BERT๊ฐ€ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก์„ ํ•™์Šตํ–ˆ์„ ๋‹น์‹œ์— ์‹ค์งˆ์ ์œผ๋กœ ์ด์–ด์ง€๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์˜ ๋ ˆ์ด๋ธ”์€ 0. ์ด์–ด์ง€์ง€ ์•Š๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ ˆ์ด๋ธ”์„ 1๋กœ ๋‘๊ณ ์„œ ์ด์ง„ ๋ถ„๋ฅ˜๋กœ ํ•™์Šต์„ ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ด์–ด์ง€์ง€ ์•Š๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์œผ๋กœ ํ…Œ์ŠคํŠธํ•ด ๋ด…์‹œ๋‹ค. # ์ƒ๊ด€์—†๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ prompt = "2002๋…„ ์›”๋“œ์ปต ์ถ•๊ตฌ ๋Œ€ํšŒ๋Š” ์ผ๋ณธ๊ณผ ๊ณต๋™์œผ๋กœ ๊ฐœ์ตœ๋˜์—ˆ๋˜ ์„ธ๊ณ„์ ์ธ ํฐ ์ž”์น˜์ž…๋‹ˆ๋‹ค." next_sentence = "๊ทน์žฅ ๊ฐ€์„œ ๋กœ๋งจ์Šค ์˜ํ™”๋ฅผ ๋ณด๊ณ  ์‹ถ์–ด์š”" encoding = tokenizer(prompt, next_sentence, return_tensors='tf') logits = model(encoding['input_ids'], token_type_ids=encoding['token_type_ids'])[0] softmax = tf.keras.layers.Softmax() probs = softmax(logits) print('์ตœ์ข… ์˜ˆ์ธก ๋ ˆ์ด๋ธ” :', tf.math.argmax(probs, axis=-1).numpy()) ์ตœ์ข… ์˜ˆ์ธก ๋ ˆ์ด๋ธ” : [1] 17-07 ์„ผํ…์Šค ๋ฒ„ํŠธ(Sentence BERT, SBERT) BERT๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์„ผํ…์Šค ๋ฒ„ํŠธ(Sentence BERT, SBERT)์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 1. BERT์˜ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ BERT๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ์ด ์„ธ ๊ฐ€์ง€์— ๋Œ€ํ•ด์„œ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์‚ฌ์ „ ํ•™์Šต๋œ BERT์— 'I love you'๋ผ๋Š” ๋ฌธ์žฅ์ด ์ž…๋ ฅ๋œ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ด ๋ฌธ์žฅ์— ๋Œ€ํ•œ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ [CLS] ํ† ํฐ์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ๋ฅผ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•ž์„œ BERT๋กœ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ, [CLS] ํ† ํฐ์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ–ˆ๋˜ ์ ์„ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. ์ด๋Š” [CLS] ํ† ํฐ์ด ์ž…๋ ฅ๋œ ๋ฌธ์žฅ์— ๋Œ€ํ•œ ์ด์ฒด์  ํ‘œํ˜„์ด๋ผ๊ณ  ๊ฐ„์ฃผํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด [CLS] ํ† ํฐ ์ž์ฒด๋ฅผ ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์žฅ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ [CLS] ํ† ํฐ๋งŒ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ BERT์˜ ๋ชจ๋“  ์ถœ๋ ฅ ๋ฒกํ„ฐ๋“ค์„ ํ‰๊ท  ๋‚ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•ž์„œ 9์ฑ•ํ„ฐ์—์„œ ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์„ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์„ค๋ช…ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” BERT์—์„œ๋„ ์ ์šฉ๋˜๋Š”๋ฐ, BERT์˜ ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ์ถœ๋ ฅ ๋ฒกํ„ฐ๋“ค์— ๋Œ€ํ•ด์„œ ํ‰๊ท ์„ ๋‚ด๊ณ  ์ด๋ฅผ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ๋Š” ์ถœ๋ ฅ ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์„ 'pooling'์ด๋ผ๊ณ  ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ‰๊ท  ํ’€๋ง(mean pooling)์„ ํ•˜์˜€๋‹ค๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ’€๋ง์—๋Š” ํ‰๊ท  ํ’€๋ง๋งŒ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ 11์ฑ•ํ„ฐ์˜ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ๋‹ค๋ฃฐ ๋•Œ ์„ค๋ช…ํ–ˆ๋˜ ๋งฅ์Šค ํ’€๋ง(max pooling) ๋˜ํ•œ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ BERT์˜ ๊ฐ ๋‹จ์–ด์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ๋“ค์— ๋Œ€ํ•ด์„œ ํ‰๊ท  ํ’€๋ง ๋Œ€์‹  ๋งฅ์Šค ํ’€๋ง์„ ์ง„ํ–‰ํ•œ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด ์‚ฌ์ „ ํ•™์Šต๋œ BERT๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. BERT์˜ [CLS] ํ† ํฐ์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ๋ฅผ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค. BERT์˜ ๋ชจ๋“  ๋‹จ์–ด์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ํ‰๊ท  ํ’€๋ง์„ ์ˆ˜ํ–‰ํ•œ ๋ฒกํ„ฐ๋ฅผ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค. BERT์˜ ๋ชจ๋“  ๋‹จ์–ด์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ๋งฅ์Šค ํ’€๋ง์„ ์ˆ˜ํ–‰ํ•œ ๋ฒกํ„ฐ๋ฅผ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค. ์ด๋•Œ ํ‰๊ท  ํ’€๋ง์„ ํ•˜๋Š๋ƒ์™€ ๋งฅ์Šค ํ’€๋ง์„ ํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ์„œ ํ•ด๋‹น ๋ฌธ์žฅ ๋ฒกํ„ฐ๊ฐ€ ๊ฐ€์ง€๋Š” ์˜๋ฏธ๋Š” ๋‹ค์†Œ ๋‹ค๋ฅธ๋ฐ, ํ‰๊ท  ํ’€๋ง์„ ์–ป์€ ๋ฌธ์žฅ ๋ฒกํ„ฐ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“  ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ๋ฐ˜์˜ํ•˜๋Š” ์ชฝ์— ๊ฐ€๊น๋‹ค๋ฉด, ๋งฅ์Šค ํ’€๋ง์„ ์–ป์€ ๋ฌธ์žฅ ๋ฒกํ„ฐ์˜ ๊ฒฝ์šฐ์—๋Š” ์ค‘์š”ํ•œ ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ๋ฐ˜์˜ํ•˜๋Š” ์ชฝ์— ๊ฐ€๊น์Šต๋‹ˆ๋‹ค. 2. SBERT(์„ผํ…์Šค ๋ฒ„ํŠธ, Sentence-BERT) SBERT๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ BERT์˜ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ์˜ ์„ฑ๋Šฅ์„ ์šฐ์ˆ˜ํ•˜๊ฒŒ ๊ฐœ์„ ํ•œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. SBERT๋Š” ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ BERT์˜ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ์„ ์‘์šฉํ•˜์—ฌ BERT๋ฅผ ํŒŒ์ธ ํŠœ๋‹ํ•ฉ๋‹ˆ๋‹ค. SBERT๊ฐ€ ์–ด๋–ค ์‹์œผ๋กœ ํ•™์Šต๋˜๋Š”์ง€ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. 1) ๋ฌธ์žฅ ์Œ ๋ถ„๋ฅ˜ ํƒœ์Šคํฌ๋กœ ํŒŒ์ธ ํŠœ๋‹ SBERT๋ฅผ ํ•™์Šตํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋ฌธ์žฅ ์Œ ๋ถ„๋ฅ˜ ํƒœ์Šคํฌ. ๋Œ€ํ‘œ์ ์œผ๋กœ๋Š” NLI(Natural Language Inferencing) ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ํ•œ๊ตญ์–ด ๋ฒ„์ „์˜ NLI ๋ฐ์ดํ„ฐ์ธ KorNLI ๋ฌธ์ œ๋ฅผ BERT๋กœ ํ’€์–ด๋ณผ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. NLI๋Š” ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์ฃผ์–ด์ง€๋ฉด ์ˆ˜๋ฐ˜(entailment) ๊ด€๊ณ„์ธ์ง€, ๋ชจ์ˆœ(contradiction) ๊ด€๊ณ„์ธ์ง€, ์ค‘๋ฆฝ(neutral) ๊ด€๊ณ„์ธ์ง€๋ฅผ ๋งž์ถ”๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ NLI ๋ฐ์ดํ„ฐ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. ๋ฌธ์žฅ A ๋ฌธ์žฅ B ๋ ˆ์ด๋ธ” A lady sits on a bench that is against a shopping mall. A person sits on the seat. Entailment A lady sits on a bench that is against a shopping mall. A woman is sitting against a building. Entailment A lady sits on a bench that is against a shopping mall. Nobody is sitting on the bench. Contradiction Two women are embracing while holding to go packages. The sisters are hugging goodbye while holding to go packages after just eating lunch. Neutral SBERT๋Š” NLI ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์šฐ์„  ๋ฌธ์žฅ A์™€ ๋ฌธ์žฅ B ๊ฐ๊ฐ์„ BERT์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ๊ณ , ์•ž์„œ BERT์˜ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ์„ ์–ป๊ธฐ ์œ„ํ•œ ๋ฐฉ์‹์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ–ˆ๋˜ ํ‰๊ท  ํ’€๋ง ๋˜๋Š” ๋งฅ์Šค ํ’€๋ง์„ ํ†ตํ•ด์„œ ๊ฐ๊ฐ์— ๋Œ€ํ•œ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ด๋ฅผ ๊ฐ๊ฐ u์™€ v๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ u ๋ฒกํ„ฐ์™€ v ๋ฒกํ„ฐ์˜ ์ฐจ์ด ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฒกํ„ฐ๋Š” ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด |u-v|์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์„ธ ๊ฐ€์ง€ ๋ฒกํ„ฐ๋ฅผ ์—ฐ๊ฒฐ(concatenation) ํ•ฉ๋‹ˆ๋‹ค. ์„ธ๋ฏธ์ฝœ๋ก (;)์„ ์—ฐ๊ฒฐ ๊ธฐํ˜ธ๋กœ ํ•œ๋‹ค๋ฉด ์—ฐ๊ฒฐ๋œ ๋ฒกํ„ฐ์˜ ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. = ( ; ; u v) ๋งŒ์•ฝ BERT์˜ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ์ด๋ผ๋ฉด ์„ธ ๊ฐœ์˜ ๋ฒกํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•œ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ n ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅ์ธต์œผ๋กœ ๋ณด๋‚ด ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ผ๋ฉด, ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ n k ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ y ์„ ๊ณฑํ•œ ํ›„์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผ์‹œํ‚จ๋‹ค๊ณ ๋„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. = o t a ( y) ์ด์ œ ์‹ค์ œ ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๋ ˆ์ด๋ธ”๋กœ๋ถ€ํ„ฐ ์˜ค์ฐจ๋ฅผ ์ค„์ด๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค. 2) ๋ฌธ์žฅ ์Œ ํšŒ๊ท€ ํƒœ์Šคํฌ๋กœ ํŒŒ์ธ ํŠœ๋‹ SBERT๋ฅผ ํ•™์Šตํ•˜๋Š” ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋ฌธ์žฅ ์Œ์œผ๋กœ ํšŒ๊ท€ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์œผ๋กœ ๋Œ€ํ‘œ์ ์œผ๋กœ STS(Semantic Textual Similarity) ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. STS๋ž€ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์˜๋ฏธ์  ์œ ์‚ฌ์„ฑ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ STS ๋ฐ์ดํ„ฐ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ ˆ์ด๋ธ”์€ ๋‘ ๋ฌธ์žฅ์˜ ์œ ์‚ฌ๋„๋กœ ๋ฒ”์œ„ ๊ฐ’์€ 0~5์ž…๋‹ˆ๋‹ค. ๋ฌธ์žฅ A ๋ฌธ์žฅ B ๋ ˆ์ด๋ธ” A plane is taking off. An air plane is taking off. 5.00 A man is playing a large flute. A man is playing a flute. 3.80 A man is spreading shreded cheese on a pizza. A man is spreading shredded cheese on an uncoo... 3.80 Three men are playing chess. Two men are playing chess. 2.60 A man is playing the cello. A man seated is playing the cello. 4.25 ์ฐธ๊ณ ) ํ•œ๊ตญ์–ด ๋ฒ„์ „์˜ STS ๋ฐ์ดํ„ฐ ์…‹์ธ KorSTS ๋ฐ์ดํ„ฐ ์…‹๋„ ์žˆ์œผ๋ฏ€๋กœ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. ๋งํฌ : https://github.com/kakaobrain/KorNLUDatasets SBERT๋Š” STS ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋ฌธ์žฅ A์™€ ๋ฌธ์žฅ B ๊ฐ๊ฐ์„ BERT์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ๊ณ , ํ‰๊ท  ํ’€๋ง ๋˜๋Š” ๋งฅ์Šค ํ’€๋ง์„ ํ†ตํ•ด์„œ ๊ฐ๊ฐ์— ๋Œ€ํ•œ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐ๊ฐ u์™€ v๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ์ด ๋‘ ๋ฒกํ„ฐ์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ์œ ์‚ฌ๋„์™€ ๋ ˆ์ด๋ธ” ์œ ์‚ฌ๋„์™€์˜ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ(Mean Squared Error, MSE)๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„์˜ ๊ฐ’์˜ ๋ฒ”์œ„๋Š” -1๊ณผ 1์‚ฌ์ด๋ฏ€๋กœ ์œ„ ๋ฐ์ดํ„ฐ์™€ ๊ฐ™์ด ๋ ˆ์ด๋ธ” ์Šค์ฝ”์–ด์˜ ๋ฒ”์œ„๊ฐ€ 0~5์ ์ด๋ผ๋ฉด ํ•™์Šต ์ „ ํ•ด๋‹น ๋ ˆ์ด๋ธ”๋“ค์˜ ๊ฐ’๋“ค์„ 5๋กœ ๋‚˜๋ˆ„์–ด ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ์ค„์ธ ํ›„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ ํƒ์— ๋”ฐ๋ผ์„œ 1) ๋ฌธ์žฅ ์Œ ๋ถ„๋ฅ˜ ํƒœ์Šคํฌ๋กœ๋งŒ ํŒŒ์ธ ํŠœ๋‹ ํ•  ์ˆ˜๋„ ์žˆ๊ณ , 2) ๋ฌธ์žฅ ์Œ ํšŒ๊ท€ ํƒœ์Šคํฌ๋กœ๋งŒ ํŒŒ์ธ ํŠœ๋‹ ํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ 1)์„ ํ•™์Šตํ•œ ํ›„์— 2)๋ฅผ ํ•™์Šตํ•˜๋Š” ์ „๋žต์„ ์„ธ์šธ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ํŒŒ์ธ ํŠœ๋‹๋œ SBERT๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•œ๊ตญ์–ด ์ฑ—๋ด‡์„ ๊ตฌํ˜„ํ•˜๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 18. ์‹ค์ „! BERT ์‹ค์Šตํ•˜๊ธฐ ์ด์ „ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์› ๋˜ BERT์˜ ์ด๋ก ์  ์ง€์‹์„ ๋ฐ”ํƒ•์œผ๋กœ BERT๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋“ค์„ ํ’€์–ด๋ด…์‹œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ transformers ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠธ๋žœ์Šคํฌ๋จธ ๊ณ„์—ด๋“ค์˜ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋  BERT์˜ ์‚ฌ์šฉ๋ฒ•๊ณผ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ํ•™์Šตํ•œ ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ Huggingface์˜ transformers ๊ณต์‹ ๋ฌธ์„œ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค๋ฉด, ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์— ๋งž๋Š” ์ตœ๊ณ  ์„ฑ๋Šฅ์˜ ๋ชจ๋ธ๋“ค์„ ์†์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Huggingface์˜ ๊ณต์‹ ๋ฌธ์„œ ๋งํฌ : https://huggingface.co/docs/transformers/index ์ด๋ฒˆ 18์ฑ•ํ„ฐ์˜ ๋ชจ๋“  ์‹ค์Šต์€ ๊ตฌ๊ธ€์˜ Colab์—์„œ ์ง„ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ฑ•ํ„ฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 18์ฑ•ํ„ฐ์˜ ๋ชจ๋“  ์‹ค์Šต ์ฝ”๋“œ๋“ค์€ ์ €์ž์˜ ๊นƒํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ๋ผ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์„ธ์š”. 18์ฑ•ํ„ฐ๋Š” ํŠนํžˆ ์ €์ž์˜ ๊นƒํ—ˆ๋ธŒ๋ฅผ ํ•จ๊ป˜ ์ฐธ๊ณ ํ•˜๋ฉด์„œ ํ•™์Šตํ•˜์‹œ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ ๋“œ๋ฆฝ๋‹ˆ๋‹ค. e-book์—์„œ ์ง€๋ฉด์˜ ํ•œ๊ณ„์ƒ ์„ค๋ช…ํ•˜์ง€ ๋ชปํ–ˆ๋˜ ์—ฌ๋Ÿฌ ๋ณ€ํ˜• ๋ฒ„์ „์˜ ์ฝ”๋“œ๋“ค๋„ ์ €์ž์˜ ๊นƒํ—ˆ๋ธŒ์—๋Š” ์ถ”๊ฐ€๋ผ ์žˆ์œผ๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ์ฐธ๊ณ ํ•˜์—ฌ ์‹ค์Šตํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ €์ž์˜ ๊นƒํ—ˆ๋ธŒ : https://github.com/ukairia777/tensorflow-nlp-tutorial 18-01 ์ฝ”๋žฉ(Colab)์—์„œ TPU ์‚ฌ์šฉํ•˜๊ธฐ ์ง€๊ธˆ๊นŒ์ง€๋Š” GPU ์‚ฌ์šฉ๋งŒ์œผ๋กœ๋„ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š”๋ฐ ํฐ ๋ฌด๋ฆฌ๊ฐ€ ์—†์—ˆ์ง€๋งŒ, BERT์˜ ๊ฒฝ์šฐ ์ง€๊ธˆ๊นŒ์ง€ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ๋ณด๋‹ค ๋ฌด๊ฑฐ์šด ํŽธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ•™์Šต ์†๋„๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ๋Š๋ฆฐ ํŽธ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” Colab์—์„œ GPU๋ณด๋‹ค ๋” ๋น ๋ฅธ TPU๋„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ์ฝ”๋žฉ(Colab)์—์„œ TPU๋ฅผ ์„ ํƒ Colab์—์„œ GPU๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋Ÿฐํƒ€์ž„ ์œ ํ˜• ๋ณ€๊ฒฝ์—์„œ GPU๋ฅผ ์„ ํƒํ•˜์˜€๋“ฏ์ด TPU๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” TPU๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. Colab์—์„œ ๋Ÿฐํƒ€์ž„ > ๋Ÿฐํƒ€์ž„ ์œ ํ˜• ๋ณ€๊ฒฝ > ํ•˜๋“œ์›จ์–ด ๊ฐ€์†๊ธฐ์—์„œ 'TPU' ์„ ํƒ GPU๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋Ÿฐํƒ€์ž„ ์œ ํ˜• ๋ณ€๊ฒฝ์—์„œ GPU๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ GPU๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ, TPU์˜ ๊ฒฝ์šฐ์—๋Š” ๋Ÿฐํƒ€์ž„ ์œ ํ˜• ๋ณ€๊ฒฝ์—์„œ TPU๋ฅผ ์„ ํƒํ•œ๋‹ค๊ณ  ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์•ˆ๋‚ดํ•  ์ถ”๊ฐ€์ ์ธ ์ฝ”๋“œ ์„ค์ •์„ ํ•ด์ฃผ์ง€ ์•Š์œผ์‹œ๋ฉด ์‹ค์ œ๋กœ๋Š” TPU๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์•„๋ž˜์˜ ์ฝ”๋“œ ์„ค์ •์„ ๋ฐ˜๋“œ์‹œ ํ•ด์ฃผ์„ธ์š”. 2. TPU ์ดˆ๊ธฐํ™” ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ์ •์˜ํ•˜๊ธฐ ์ „์— ์•„๋ž˜์˜ ์„ค์ •์„ ๋ฏธ๋ฆฌ ํ•ด์ฃผ์–ด์•ผ ํ•˜๋ฏ€๋กœ ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์ดˆ๋ฐ˜๋ถ€์— ์‹คํ–‰ํ•ด ์ค๋‹ˆ๋‹ค. import tensorflow as tf import os resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR']) tf.config.experimental_connect_to_cluster(resolver) tf.tpu.experimental.initialize_tpu_system(resolver) 3. TPU Strategy ์„ธํŒ… tf.distribute.Strategy๋Š” ํ›ˆ๋ จ์„ ์—ฌ๋Ÿฌ GPU ๋˜๋Š” ์—ฌ๋Ÿฌ ์žฅ๋น„, ์—ฌ๋Ÿฌ TPU๋กœ ๋‚˜๋ˆ„์–ด ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํ…์„œ ํ”Œ๋กœ API์ž…๋‹ˆ๋‹ค. ์ด API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด์˜ ๋ชจ๋ธ์ด๋‚˜ ํ›ˆ๋ จ ์ฝ”๋“œ๋ฅผ ๋ถ„์‚ฐ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. TPU ์‚ฌ์šฉ์„ ์œ„ํ•ด์„œ๋„ Strategy๋ฅผ ์„ธํŒ…ํ•ด ์ค๋‹ˆ๋‹ค. strategy = tf.distribute.TPUStrategy(resolver) 4. ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์ปดํŒŒ์ผ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ์ปดํŒŒ์ผ์„ ํ•  ๋•Œ๋„ ์ถ”๊ฐ€์ ์ธ ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ์ปดํŒŒ์ผ์€ strategy.scope ๋‚ด์—์„œ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์˜ ์ธต์„ ์Œ“๋Š” create_model()์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด strategy.scope ๋‚ด์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ชจ๋ธ์„ ์ปดํŒŒ์ผํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋ธ์˜ ์ธต์„ ์Œ“๋Š” create_model()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. def create_model(): return tf.keras.Sequential( [tf.keras.layers.Conv2D(256, 3, activation='relu', input_shape=(28, 28, 1)), tf.keras.layers.Conv2D(256, 3, activation='relu'), tf.keras.layers.Flatten(), tf.keras.layers.Dense(256, activation='relu'), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(10)]) with strategy.scope(): ๋‹ค์Œ์— ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•˜๊ณ , create_model() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๋ชจ๋ธ์„ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด strategy.scope ๋‚ด์—์„œ ๋ชจ๋ธ์„ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค. with strategy.scope(): model = create_model() model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['sparse_categorical_accuracy']) ์ด ๋ชจ๋ธ์„ fit() ํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ๋ชจ๋ธ์€ TPU๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ํ•™์Šตํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค! ์•ž์œผ๋กœ Colab์—์„œ TPU๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ BERT ๋ชจ๋ธ์„ ํŒŒ์ธ ํŠœ๋‹ ํ•  ๋•Œ์—๋„ ์œ„ ๊ณผ์ •๊ณผ ๋™์ผํ•œ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. ์œ„์˜ 1~3๋ฒˆ๊นŒ์ง€์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ , BERT์™€ ์ปค์Šคํ…€ ๋ ˆ์ด์–ด์— ๋Œ€ํ•œ ๋ชจ๋ธ์˜ ์ปดํŒŒ์ผ์„ strategy.scope ๋‚ด์—์„œ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 5. GPU ์‹ค์Šต ์ฝ”๋“œ๋กœ ๋˜๋Œ๋ฆฌ๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ TPU๋กœ ์‹ค์Šต์„ ์ง„ํ–‰ํ•œ ์ฝ”๋“œ๋“ค์„ GPU ํ™˜๊ฒฝ์—์„œ ์‹ค์Šตํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์œ„์—์„œ ๋ชจ๋ธ์˜ ์ธต์„ ์Œ“๊ณ , ์ปดํŒŒ์ผํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ œ์™ธํ•œ TPU ์ง„ํ–‰๋งŒ์„ ์œ„ํ•œ ์ฝ”๋“œ๋“ค์„ ์ „๋ถ€ ์ œ๊ฑฐํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 18-02 transformers์˜ ๋ชจ๋ธ ํด๋ž˜์Šค ๋ถˆ๋Ÿฌ์˜ค๊ธฐ transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ๋Š” ๊ฐ์ข… ํƒœ์Šคํฌ์— ๋งž๊ฒŒ BERT ์œ„์— ์ถœ๋ ฅ์ธต์„ ์ถ”๊ฐ€ํ•œ ๋ชจ๋ธ ํด๋ž˜์Šค ๊ตฌํ˜„์ฒด๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ตฌํ˜„์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ๋ณ„๋„์˜ ์ถœ๋ ฅ์ธต์„ ์„ค๊ณ„ํ•  ํ•„์š” ์—†์ด ํƒœ์Šคํฌ์— ๋งž๊ฒŒ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ๋ชจ๋ธ ๊ตฌ์กฐ์˜ ์ดํ•ด๋ฅผ ์œ„ํ•ด ๋ชจ๋“  ์‹ค์Šต์—์„œ ์ถœ๋ ฅ์ธต์„ ์ง์ ‘ ์„ค๊ณ„ํ•˜์—ฌ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜์ง€๋งŒ, ์ด๋ฏธ ๋ชจ๋ธ์˜ ๊ตฌ์กฐ๋ฅผ ์ดํ•ดํ•œ ์ƒํ™ฉ์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ด๋ฏธ ์ถœ๋ ฅ์ธต์ด ์„ค๊ณ„๋œ ๋ชจ๋ธ๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์ฝ”๋“œ ์ž‘์„ฑ์ด ๊ฐ„ํŽธํ•ฉ๋‹ˆ๋‹ค. ์ €์ž์˜ ๊นƒํ—ˆ๋ธŒ์—์„œ ํŒŒ์ผ๋ช…์— 'model_from_transformers'๊ฐ€ ๋“ค์–ด๊ฐ„ ํŒŒ์ผ๋“ค์ด ์•„๋ž˜์˜ ๊ตฌํ˜„์ฒด๋“ค์„ ์‚ฌ์šฉํ•œ ์‹ค์Šต ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. 1. ๋‹ค ๋Œ€ ์ผ ์œ ํ˜• ๋‹ค ๋Œ€ ์ผ(many-to-one) ์œ ํ˜•์€ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜์— ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. '18-4. TFBertForSequenceClassification' ์‹ค์Šต์—์„œ๋Š” ์•„๋ž˜์˜ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ด ๋ด…๋‹ˆ๋‹ค. from transformers import TFBertForSequenceClassification model = TFBertForSequenceClassification.from_pretrained("๋ชจ๋ธ ์ด๋ฆ„", num_labels=๋ถ„๋ฅ˜ํ•  ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜) 2. ๋‹ค ๋Œ€๋‹ค ์œ ํ˜• ๋‹ค ๋Œ€๋‹ค(many-to-one) ์œ ํ˜•์€ ๋‹ค์ˆ˜์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ๋‹ค์ˆ˜์˜ ์ถœ๋ ฅ์ด ํ•„์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. '18-6. ๊ฐœ์ฒด๋ช… ์ธ์‹'์ด ํ•ด๋‹น ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. from transformers import TFBertForTokenClassification model = TFBertForTokenClassification.from_pretrained("๋ชจ๋ธ ์ด๋ฆ„", num_labels=๋ถ„๋ฅ˜ํ•  ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜) 3. ์งˆ์˜์‘๋‹ต ์œ ํ˜• ์งˆ์˜์‘๋‹ต(Question Answering) ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ๋กœ '18-7. ๊ธฐ๊ณ„ ๋…ํ•ด' ์‹ค์Šต์ด ํ•ด๋‹น ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. from transformers import TFBertForQuestionAnswering model = TFBertForQuestionAnswering.from_pretrained('๋ชจ๋ธ ์ด๋ฆ„') 18-03 KoBERT๋ฅผ ์ด์šฉํ•œ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด BERT๋ฅผ ์ด์šฉํ•˜์—ฌ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜์—์„œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ 90%์— ๊ฐ€๊นŒ์šด ์„ฑ๋Šฅ์„ ์–ป๋Š” ์‹ค์Šต์ž…๋‹ˆ๋‹ค. 18-04 TFBertForSequenceClassification ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 18-05 KoBERT๋ฅผ ์ด์šฉํ•œ KorNLI ํ’€์–ด๋ณด๊ธฐ (๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜) ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 18-06 KoBERT๋ฅผ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition) ํ•œ๊ตญ์–ด BERT๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ํ•™์Šตํ•˜๋Š” ์‹ค์Šต์ž…๋‹ˆ๋‹ค. ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 18-07 KoBERT๋ฅผ ์ด์šฉํ•œ ๊ธฐ๊ณ„ ๋…ํ•ด(Machine Reading Comprehension) KorQuad 1.0 ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์œผ๋กœ๋ถ€ํ„ฐ ๋ณธ๋ฌธ์œผ๋กœ๋ถ€ํ„ฐ ์ •๋‹ต์„ ์ฐพ์•„ ๋Œ€๋‹ตํ•˜๋Š” ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์—…์—์„œ๋Š” ๊ธฐ๊ณ„ ๋…ํ•ด๋ฅผ ํ†ตํ•ด ์ฑ—๋ด‡์„ ๊ตฌํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ ์˜ˆ์‹œ ๋ณธ๋ฌธ : [CLS] " ๋‚ด๊ฐ๊ณผ ์žฅ๊ด€๋“ค์ด ์†Œ์™ธ๋˜๊ณ  ๋Œ€ํ†ต๋ น ๋น„์„œ์‹ค์˜ ๊ถŒํ•œ์ด ๋„ˆ๋ฌด ํฌ๋‹ค ", " ํ–‰๋ณด๊ฐ€ ๋น„์„œ ๋ณธ์—ฐ์˜ ์—ญํ• ์„ ๋ฒ—์–ด๋‚œ๋‹ค "๋ผ๋Š” ์˜๊ฒฌ์ด ์ œ๊ธฐ๋˜์—ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๊ฐ€ 10์ฐจ ๊ฐœํ—Œ์•ˆ ๋ฐœํ‘œ์ด๋‹ค. ์›๋กœ ํ—Œ๋ฒ•ํ•™์ž์ธ ํ—ˆ์˜ ๊ฒฝํฌ๋Œ€ ์„์ขŒ๊ต์ˆ˜๋Š” ์ •๋ถ€์˜ ํ—Œ๋ฒ• ๊ฐœ์ •์•ˆ ์ค€๋น„ ๊ณผ์ •์— ๋Œ€ํ•ด " ์ฒญ์™€๋Œ€ ๋น„์„œ์‹ค์ด ์•„๋‹Œ ๊ตญ๋ฌดํšŒ์˜ ์ค‘์‹ฌ์œผ๋กœ ์ด๋ค„์กŒ์–ด์•ผ ํ–ˆ๋‹ค "๋ผ๊ณ  ์ง€์ ํ–ˆ๋‹ค.'๊ตญ๋ฌดํšŒ์˜์˜ ์‹ฌ์˜๋ฅผ ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค'( ์ œ89์กฐ )๋Š” ํ—Œ๋ฒ• ๊ทœ์ •์— ์ถฉ์‹คํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด์„œ " ๋ฒ•๋ฌด๋ถ€ ์žฅ๊ด€์„ ์ œ์ณ๋†“๊ณ  ๋ฏผ์ •์ˆ˜์„์ด ๊ฐœ์ •์•ˆ์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒŒ ์ดํ•ด๊ฐ€ ์•ˆ ๋œ๋‹ค "๋ผ๊ณ  ์ง€์ ํ–ˆ๋‹ค. ๋ฏผ์ •์ˆ˜์„์€ ๊ตญํšŒ์˜์›์— ๋Œ€ํ•ด ์ฑ…์ž„์ง€๋Š” ๋ฒ•๋ฌด๋ถ€ ์žฅ๊ด€๋„ ์•„๋‹ˆ๊ณ , ๊ตญ๋ฏผ์— ๋Œ€ํ•ด ์ฑ…์ž„์ง€๋Š” ์‚ฌ๋žŒ๋„ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •๋‹น์„ฑ์ด ์—†๊ณ , ๋‹จ์ง€ ๋Œ€ํ†ต๋ น์˜ ์‹ ์ž„์ด ์žˆ์„ ๋ฟ์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ ๊ตญ๋ฌด์ด๋ฆฌ ์„ ์ถœ ๋ฐฉ์‹์— ๋Œ€ํ•œ ๊ธฐ์ž์˜ ์งˆ๋ฌธ์— " ๋ฌธ ๋Œ€ํ†ต๋ น๋„ ์ทจ์ž„ ์ „์— ๊ตญ๋ฌด์ด๋ฆฌ์—๊ฒŒ ์‹ค์งˆ์  ๊ถŒํ•œ์„ ์ฃผ๊ฒ ๋‹ค๊ณ  ํ–ˆ์ง€๋งŒ ๊ทธ๋Ÿฌ์ง€ ๋ชปํ•˜๊ณ  ์žˆ๋‹ค. ๋Œ€ํ†ต๋ น ๋น„์„œ์‹ค์žฅ๋งŒ๋„ ๋ชปํ•œ ๊ถŒํ•œ์„ ํ–‰์‚ฌํ•˜๊ณ  ์žˆ๋‹ค. "๋ผ๊ณ  ๋‹ต๋ณ€ํ–ˆ๋‹ค. ์งˆ๋ฌธ : ๊ตญ๋ฌดํšŒ์˜์˜ ์‹ฌ์˜๋ฅผ ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค๋Š” ํ—Œ๋ฒ• ์ œ ๋ช‡ ์กฐ์˜ ๋‚ด์šฉ์ธ๊ฐ€? ์ •๋‹ต : ์ œ89์กฐ ์˜ˆ์ธก : ์ œ89์กฐ ---------------------------------------- ๋ณธ๋ฌธ : [CLS] " ๋‚ด๊ฐ๊ณผ ์žฅ๊ด€๋“ค์ด ์†Œ์™ธ๋˜๊ณ  ๋Œ€ํ†ต๋ น ๋น„์„œ์‹ค์˜ ๊ถŒํ•œ์ด ๋„ˆ๋ฌด ํฌ๋‹ค ", " ํ–‰๋ณด๊ฐ€ ๋น„์„œ ๋ณธ์—ฐ์˜ ์—ญํ• ์„ ๋ฒ—์–ด๋‚œ๋‹ค "๋ผ๋Š” ์˜๊ฒฌ์ด ์ œ๊ธฐ๋˜์—ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๊ฐ€ 10์ฐจ ๊ฐœํ—Œ์•ˆ ๋ฐœํ‘œ์ด๋‹ค. ์›๋กœ ํ—Œ๋ฒ•ํ•™์ž์ธ ํ—ˆ์˜ ๊ฒฝํฌ๋Œ€ ์„์ขŒ๊ต์ˆ˜๋Š” ์ •๋ถ€์˜ ํ—Œ๋ฒ• ๊ฐœ์ •์•ˆ ์ค€๋น„ ๊ณผ์ •์— ๋Œ€ํ•ด " ์ฒญ์™€๋Œ€ ๋น„์„œ์‹ค์ด ์•„๋‹Œ ๊ตญ๋ฌดํšŒ์˜ ์ค‘์‹ฌ์œผ๋กœ ์ด๋ค„์กŒ์–ด์•ผ ํ–ˆ๋‹ค "๋ผ๊ณ  ์ง€์ ํ–ˆ๋‹ค.'๊ตญ๋ฌดํšŒ์˜์˜ ์‹ฌ์˜๋ฅผ ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค'( ์ œ89์กฐ )๋Š” ํ—Œ๋ฒ• ๊ทœ์ •์— ์ถฉ์‹คํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด์„œ " ๋ฒ•๋ฌด๋ถ€ ์žฅ๊ด€์„ ์ œ์ณ๋†“๊ณ  ๋ฏผ์ •์ˆ˜์„์ด ๊ฐœ์ •์•ˆ์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒŒ ์ดํ•ด๊ฐ€ ์•ˆ ๋œ๋‹ค "๋ผ๊ณ  ์ง€์ ํ–ˆ๋‹ค. ๋ฏผ์ •์ˆ˜์„์€ ๊ตญํšŒ์˜์›์— ๋Œ€ํ•ด ์ฑ…์ž„์ง€๋Š” ๋ฒ•๋ฌด๋ถ€ ์žฅ๊ด€๋„ ์•„๋‹ˆ๊ณ , ๊ตญ๋ฏผ์— ๋Œ€ํ•ด ์ฑ…์ž„์ง€๋Š” ์‚ฌ๋žŒ๋„ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •๋‹น์„ฑ์ด ์—†๊ณ , ๋‹จ์ง€ ๋Œ€ํ†ต๋ น์˜ ์‹ ์ž„์ด ์žˆ์„ ๋ฟ์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ ๊ตญ๋ฌด์ด๋ฆฌ ์„ ์ถœ ๋ฐฉ์‹์— ๋Œ€ํ•œ ๊ธฐ์ž์˜ ์งˆ๋ฌธ์— " ๋ฌธ ๋Œ€ํ†ต๋ น๋„ ์ทจ์ž„ ์ „์— ๊ตญ๋ฌด์ด๋ฆฌ์—๊ฒŒ ์‹ค์งˆ์  ๊ถŒํ•œ์„ ์ฃผ๊ฒ ๋‹ค๊ณ  ํ–ˆ์ง€๋งŒ ๊ทธ๋Ÿฌ์ง€ ๋ชปํ•˜๊ณ  ์žˆ๋‹ค. ๋Œ€ํ†ต๋ น ๋น„์„œ์‹ค์žฅ๋งŒ๋„ ๋ชปํ•œ ๊ถŒํ•œ์„ ํ–‰์‚ฌํ•˜๊ณ  ์žˆ๋‹ค. "๋ผ๊ณ  ๋‹ต๋ณ€ํ–ˆ๋‹ค. ์งˆ๋ฌธ : ๋ฒ•๋ฌด๋ถ€ ์žฅ๊ด€์„ ์ œ์ณ๋†“๊ณ  ๋ฏผ์ •์ˆ˜์„์ด ๊ฐœ์ •์•ˆ์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒŒ ์ดํ•ด๊ฐ€ ์•ˆ ๋œ๋‹ค๊ณ  ์ง€์ ํ•œ ๊ฒฝํฌ๋Œ€ ์„์ขŒ๊ต์ˆ˜ ์ด๋ฆ„์€? ์ •๋‹ต : ํ—ˆ์˜ ์˜ˆ์ธก : ํ—ˆ์˜ ---------------------------------------- ๋ณธ๋ฌธ : [CLS] ์•Œ๋ ‰์‚ฐ๋” ๋ฉ”์ด๊ทธ์Šค ํ—ค์ด๊ทธ 2์„ธ ( ์˜์–ด : Alexander Meigs Haig, Jr., 1924๋…„ 12์›” 2์ผ ~ 2010๋…„ 2์›” 20์ผ )๋Š” ๋ฏธ๊ตญ์˜ ๊ตญ๋ฌด ์žฅ๊ด€์„ ์ง€๋‚ธ ๋ฏธ๊ตญ์˜ ๊ตฐ์ธ, ๊ด€๋ฃŒ ๋ฐ ์ •์น˜์ธ์ด๋‹ค. ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ๋Œ€ํ†ต๋ น ๋ฐ‘์—์„œ ๊ตญ๋ฌด์žฅ๊ด€์„ ์ง€๋ƒˆ์œผ๋ฉฐ, ๋ฆฌ์ฒ˜๋“œ ๋‹‰์Šจ๊ณผ ์ œ๋Ÿด๋“œ ํฌ๋“œ ๋Œ€ํ†ต๋ น ๋ฐ‘์—์„œ ๋ฐฑ์•…๊ด€ ๋น„์„œ์‹ค์žฅ์„ ์ง€๋ƒˆ๋‹ค. ๋˜ํ•œ ๊ทธ๋Š” ๋ฏธ๊ตญ ๊ตฐ๋Œ€์—์„œ 2๋ฒˆ์งธ๋กœ ๋†’์€ ์ง์œ„์ธ ๋ฏธ๊ตญ ์œก๊ตฐ ๋ถ€์ฐธ๋ชจ ์ด์žฅ๊ณผ ๋‚˜ํ†  ๋ฐ ๋ฏธ๊ตญ ๊ตฐ๋Œ€์˜ ์œ ๋Ÿฝ์—ฐํ•ฉ๊ตฐ ์ตœ๊ณ ์‚ฌ๋ น๊ด€์ด์—ˆ๋‹ค. ํ•œ๊ตญ ์ „์Ÿ ์‹œ์ ˆ ๋”๊ธ€๋Ÿฌ์Šค ๋งฅ์•„๋” ์œ ์—”๊ตฐ ์‚ฌ๋ น๊ด€์˜ ์ฐธ๋ชจ๋กœ ์ง์ ‘ ์ฐธ์ „ํ•˜์˜€์œผ๋ฉฐ, ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ์ •๋ถ€ ์ถœ๋ฒ” ๋‹น์‹œ ์ดˆ๋Œ€ ๊ตญ๋ฌด์žฅ๊ด€์ง์„ ๋งก์•„ 1980๋…„๋Œ€ ๋Œ€ํ•œ๋ฏผ๊ตญ๊ณผ ๋ฏธ๊ตญ์˜ ๊ด€๊ณ„๋ฅผ ์กฐ์œจํ•ด ์™”๋‹ค. ์ €์„œ๋กœ ํšŒ๊ณ ๋ก ใ€Š ๊ฒฝ๊ณ  : ํ˜„์‹ค์ฃผ์˜, ๋ ˆ์ด๊ฑด๊ณผ ์™ธ๊ต ์ •์ฑ… ใ€‹ ( 1984๋…„ ๋ฐœ๊ฐ„ ) ์ด ์žˆ๋‹ค. ์งˆ๋ฌธ : ๋ฏธ๊ตญ ๊ตฐ๋Œ€ ๋‚ด ๋‘ ๋ฒˆ์งธ๋กœ ๋†’์€ ์ง์œ„๋Š” ๋ฌด์—‡์ธ๊ฐ€? ์ •๋‹ต : ๋ฏธ๊ตญ ์œก๊ตฐ ๋ถ€์ฐธ๋ชจ ์ด์žฅ ์˜ˆ์ธก : ์œก๊ตฐ ๋ถ€์ฐธ๋ชจ ์ด์žฅ ---------------------------------------- ๋ณธ๋ฌธ : [CLS] ์•Œ๋ ‰์‚ฐ๋” ๋ฉ”์ด๊ทธ์Šค ํ—ค์ด๊ทธ 2์„ธ ( ์˜์–ด : Alexander Meigs Haig, Jr., 1924๋…„ 12์›” 2์ผ ~ 2010๋…„ 2์›” 20์ผ )๋Š” ๋ฏธ๊ตญ์˜ ๊ตญ๋ฌด ์žฅ๊ด€์„ ์ง€๋‚ธ ๋ฏธ๊ตญ์˜ ๊ตฐ์ธ, ๊ด€๋ฃŒ ๋ฐ ์ •์น˜์ธ์ด๋‹ค. ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ๋Œ€ํ†ต๋ น ๋ฐ‘์—์„œ ๊ตญ๋ฌด์žฅ๊ด€์„ ์ง€๋ƒˆ์œผ๋ฉฐ, ๋ฆฌ์ฒ˜๋“œ ๋‹‰์Šจ๊ณผ ์ œ๋Ÿด๋“œ ํฌ๋“œ ๋Œ€ํ†ต๋ น ๋ฐ‘์—์„œ ๋ฐฑ์•…๊ด€ ๋น„์„œ์‹ค์žฅ์„ ์ง€๋ƒˆ๋‹ค. ๋˜ํ•œ ๊ทธ๋Š” ๋ฏธ๊ตญ ๊ตฐ๋Œ€์—์„œ 2๋ฒˆ์งธ๋กœ ๋†’์€ ์ง์œ„์ธ ๋ฏธ๊ตญ ์œก๊ตฐ ๋ถ€์ฐธ๋ชจ ์ด์žฅ๊ณผ ๋‚˜ํ†  ๋ฐ ๋ฏธ๊ตญ ๊ตฐ๋Œ€์˜ ์œ ๋Ÿฝ์—ฐํ•ฉ๊ตฐ ์ตœ๊ณ ์‚ฌ๋ น๊ด€์ด์—ˆ๋‹ค. ํ•œ๊ตญ ์ „์Ÿ ์‹œ์ ˆ ๋”๊ธ€๋Ÿฌ์Šค ๋งฅ์•„๋” ์œ ์—”๊ตฐ ์‚ฌ๋ น๊ด€์˜ ์ฐธ๋ชจ๋กœ ์ง์ ‘ ์ฐธ์ „ํ•˜์˜€์œผ๋ฉฐ, ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ์ •๋ถ€ ์ถœ๋ฒ” ๋‹น์‹œ ์ดˆ๋Œ€ ๊ตญ๋ฌด์žฅ๊ด€์ง์„ ๋งก์•„ 1980๋…„๋Œ€ ๋Œ€ํ•œ๋ฏผ๊ตญ๊ณผ ๋ฏธ๊ตญ์˜ ๊ด€๊ณ„๋ฅผ ์กฐ์œจํ•ด ์™”๋‹ค. ์ €์„œ๋กœ ํšŒ๊ณ ๋ก ใ€Š ๊ฒฝ๊ณ  : ํ˜„์‹ค์ฃผ์˜, ๋ ˆ์ด๊ฑด๊ณผ ์™ธ๊ต ์ •์ฑ… ใ€‹ ( 1984๋…„ ๋ฐœ๊ฐ„ ) ์ด ์žˆ๋‹ค. ์งˆ๋ฌธ : ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ์ •๋ถ€ ์ถœ๋ฒ” ๋‹น์‹œ ์•Œ๋ ‰์‚ฐ๋” ํ—ค์ด๊ทธ๋Š” ์–ด๋–ค ์ง์ฑ…์„ ๋งก์•˜๋Š”๊ฐ€? ์ •๋‹ต : ์ดˆ๋Œ€ ๊ตญ๋ฌด์žฅ๊ด€์ง ์˜ˆ์ธก : ๊ตญ๋ฌด์žฅ๊ด€์ง ---------------------------------------- ๋ณธ๋ฌธ : [CLS] ์•Œ๋ ‰์‚ฐ๋” ๋ฉ”์ด๊ทธ์Šค ํ—ค์ด๊ทธ 2์„ธ ( ์˜์–ด : Alexander Meigs Haig, Jr., 1924๋…„ 12์›” 2์ผ ~ 2010๋…„ 2์›” 20์ผ )๋Š” ๋ฏธ๊ตญ์˜ ๊ตญ๋ฌด ์žฅ๊ด€์„ ์ง€๋‚ธ ๋ฏธ๊ตญ์˜ ๊ตฐ์ธ, ๊ด€๋ฃŒ ๋ฐ ์ •์น˜์ธ์ด๋‹ค. ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ๋Œ€ํ†ต๋ น ๋ฐ‘์—์„œ ๊ตญ๋ฌด์žฅ๊ด€์„ ์ง€๋ƒˆ์œผ๋ฉฐ, ๋ฆฌ์ฒ˜๋“œ ๋‹‰์Šจ๊ณผ ์ œ๋Ÿด๋“œ ํฌ๋“œ ๋Œ€ํ†ต๋ น ๋ฐ‘์—์„œ ๋ฐฑ์•…๊ด€ ๋น„์„œ์‹ค์žฅ์„ ์ง€๋ƒˆ๋‹ค. ๋˜ํ•œ ๊ทธ๋Š” ๋ฏธ๊ตญ ๊ตฐ๋Œ€์—์„œ 2๋ฒˆ์งธ๋กœ ๋†’์€ ์ง์œ„์ธ ๋ฏธ๊ตญ ์œก๊ตฐ ๋ถ€์ฐธ๋ชจ ์ด์žฅ๊ณผ ๋‚˜ํ†  ๋ฐ ๋ฏธ๊ตญ ๊ตฐ๋Œ€์˜ ์œ ๋Ÿฝ์—ฐํ•ฉ๊ตฐ ์ตœ๊ณ ์‚ฌ๋ น๊ด€์ด์—ˆ๋‹ค. ํ•œ๊ตญ ์ „์Ÿ ์‹œ์ ˆ ๋”๊ธ€๋Ÿฌ์Šค ๋งฅ์•„๋” ์œ ์—”๊ตฐ ์‚ฌ๋ น๊ด€์˜ ์ฐธ๋ชจ๋กœ ์ง์ ‘ ์ฐธ์ „ํ•˜์˜€์œผ๋ฉฐ, ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ์ •๋ถ€ ์ถœ๋ฒ” ๋‹น์‹œ ์ดˆ๋Œ€ ๊ตญ๋ฌด์žฅ๊ด€์ง์„ ๋งก์•„ 1980๋…„๋Œ€ ๋Œ€ํ•œ๋ฏผ๊ตญ๊ณผ ๋ฏธ๊ตญ์˜ ๊ด€๊ณ„๋ฅผ ์กฐ์œจํ•ด ์™”๋‹ค. ์ €์„œ๋กœ ํšŒ๊ณ ๋ก ใ€Š ๊ฒฝ๊ณ  : ํ˜„์‹ค์ฃผ์˜, ๋ ˆ์ด๊ฑด๊ณผ ์™ธ๊ต ์ •์ฑ… ใ€‹ ( 1984๋…„ ๋ฐœ๊ฐ„ ) ์ด ์žˆ๋‹ค. ์งˆ๋ฌธ : ์•Œ๋ ‰์‚ฐ๋” ํ—ค์ด๊ทธ๋Š” ์–ด๋Š ๋Œ€ํ†ต๋ น์˜ ๋ฐ‘์—์„œ ๊ตญ๋ฌด์žฅ๊ด€์„ ์ง€๋ƒˆ๋Š”๊ฐ€? ์ •๋‹ต : ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ๋Œ€ํ†ต๋ น ์˜ˆ์ธก : ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ---------------------------------------- ๋ณธ๋ฌธ : [CLS] ์•Œ๋ ‰์‚ฐ๋” ๋ฉ”์ด๊ทธ์Šค ํ—ค์ด๊ทธ 2์„ธ ( ์˜์–ด : Alexander Meigs Haig, Jr., 1924๋…„ 12์›” 2์ผ ~ 2010๋…„ 2์›” 20์ผ )๋Š” ๋ฏธ๊ตญ์˜ ๊ตญ๋ฌด ์žฅ๊ด€์„ ์ง€๋‚ธ ๋ฏธ๊ตญ์˜ ๊ตฐ์ธ, ๊ด€๋ฃŒ ๋ฐ ์ •์น˜์ธ์ด๋‹ค. ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ๋Œ€ํ†ต๋ น ๋ฐ‘์—์„œ ๊ตญ๋ฌด์žฅ๊ด€์„ ์ง€๋ƒˆ์œผ๋ฉฐ, ๋ฆฌ์ฒ˜๋“œ ๋‹‰์Šจ๊ณผ ์ œ๋Ÿด๋“œ ํฌ๋“œ ๋Œ€ํ†ต๋ น ๋ฐ‘์—์„œ ๋ฐฑ์•…๊ด€ ๋น„์„œ์‹ค์žฅ์„ ์ง€๋ƒˆ๋‹ค. ๋˜ํ•œ ๊ทธ๋Š” ๋ฏธ๊ตญ ๊ตฐ๋Œ€์—์„œ 2๋ฒˆ์งธ๋กœ ๋†’์€ ์ง์œ„์ธ ๋ฏธ๊ตญ ์œก๊ตฐ ๋ถ€์ฐธ๋ชจ ์ด์žฅ๊ณผ ๋‚˜ํ†  ๋ฐ ๋ฏธ๊ตญ ๊ตฐ๋Œ€์˜ ์œ ๋Ÿฝ์—ฐํ•ฉ๊ตฐ ์ตœ๊ณ ์‚ฌ๋ น๊ด€์ด์—ˆ๋‹ค. ํ•œ๊ตญ ์ „์Ÿ ์‹œ์ ˆ ๋”๊ธ€๋Ÿฌ์Šค ๋งฅ์•„๋” ์œ ์—”๊ตฐ ์‚ฌ๋ น๊ด€์˜ ์ฐธ๋ชจ๋กœ ์ง์ ‘ ์ฐธ์ „ํ•˜์˜€์œผ๋ฉฐ, ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ์ •๋ถ€ ์ถœ๋ฒ” ๋‹น์‹œ ์ดˆ๋Œ€ ๊ตญ๋ฌด์žฅ๊ด€์ง์„ ๋งก์•„ 1980๋…„๋Œ€ ๋Œ€ํ•œ๋ฏผ๊ตญ๊ณผ ๋ฏธ๊ตญ์˜ ๊ด€๊ณ„๋ฅผ ์กฐ์œจํ•ด ์™”๋‹ค. ์ €์„œ๋กœ ํšŒ๊ณ ๋ก ใ€Š ๊ฒฝ๊ณ  : ํ˜„์‹ค์ฃผ์˜, ๋ ˆ์ด๊ฑด๊ณผ ์™ธ๊ต ์ •์ฑ… ใ€‹ ( 1984๋…„ ๋ฐœ๊ฐ„ ) ์ด ์žˆ๋‹ค. ์งˆ๋ฌธ : ๋กœ๋„๋“œ ๋ ˆ์ด๊ฑด ๋Œ€ํ†ต๋ น ๋ฐ‘์—์„œ ์ผํ•œ ๊ตญ๋ฌด ์žฅ๊ด€์€ ๋ˆ„๊ตฌ์ธ๊ฐ€? ์ •๋‹ต : ์•Œ๋ ‰์‚ฐ๋” ๋ฉ”์ด๊ทธ์Šค ํ—ค์ด๊ทธ 2์„ธ ์˜ˆ์ธก : ์•Œ๋ ‰์‚ฐ๋” ๋ฉ”์ด๊ทธ์Šค ํ—ค์ด๊ทธ 2์„ธ ---------------------------------------- 18-08 BERT์˜ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ(SBERT)์„ ์ด์šฉํ•œ ํ•œ๊ตญ์–ด ์ฑ—๋ด‡ SBERT๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€์ธ sentence_transformers๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‰ฝ๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•œ๊ตญ์–ด ์ฑ—๋ด‡์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์‹ค์Šต์— ์•ž์„œ sentence_transformers๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install sentence_transformers ํŠธ๋žœ์Šคํฌ๋จธ๋ฅผ ์ด์šฉํ•œ ํ•œ๊ตญ์–ด ์ฑ—๋ด‡ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. import numpy as np import pandas as pd from numpy import dot from numpy.linalg import norm import urllib.request from sentence_transformers import SentenceTransformer urllib.request.urlretrieve("https://raw.githubusercontent.com/songys/Chatbot_data/master/ChatbotData.csv", filename="ChatBotData.csv") train_data = pd.read_csv('ChatBotData.csv') train_data.head() ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ BERT๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ•œ๊ตญ์–ด๋„ ํฌํ•จ๋˜์–ด ํ•™์Šต๋œ ๋‹ค๊ตญ์–ด ๋ชจ๋ธ์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. model = SentenceTransformer('sentence-transformers/xlm-r-100langs-bert-base-nli-stsb-mean-tokens') ๋ชจ๋ธ์˜ ์ด๋ฆ„์€ 'xlm-r-100langs-bert-base-nli-stsb-mean-tokens'์ธ๋ฐ ์ด๋ฆ„์ด ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” 100๊ฐ€์ง€ ์–ธ์–ด๋ฅผ ์ง€์›(ํ•œ๊ตญ์–ด ํฌํ•จ) ํ•˜๋Š” ๋‹ค๊ตญ์–ด BERT BASE ๋ชจ๋ธ๋กœ SNLI ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šต ํ›„ STS-B ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šต๋˜์—ˆ์œผ๋ฉฐ, ๋ฌธ์žฅ ํ‘œํ˜„์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ‰๊ท  ํ’€๋ง(mean-tokens)์„ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ NLI ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šต ํ›„์— STS ๋ฐ์ดํ„ฐ๋กœ ์ถ”๊ฐ€ ํŒŒ์ธ ํŠœ๋‹ํ•œ ๋ชจ๋ธ์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. SentenceTransformer๋กœ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ฆฌ์ŠคํŠธ๋Š” ์•„๋ž˜์˜ ๋งํฌ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋งํฌ์— ํ•œ๊ตญ์–ด ๋ฒ„์ „์˜ ๋ชจ๋ธ๋“ค ๋˜ํ•œ ๊ณต๊ฐœ๋˜์–ด ์žˆ์œผ๋‹ˆ ๋ฐฉ๋ฌธํ•ด ๋ณด์„ธ์š”. ๋งํฌ : https://huggingface.co/models? library=sentence-transformers ๋ฐ์ดํ„ฐ์—์„œ ๋ชจ๋“  ์งˆ๋ฌธ์—ด. ์ฆ‰, train_data['Q']์— ๋Œ€ํ•ด์„œ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ ๊ฐ’์„ ๊ตฌํ•œ ํ›„ embedding์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ์—ด์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. train_data['embedding'] = train_data.apply(lambda row: model.encode(row.Q), axis = 1) ๋‘ ๊ฐœ์˜ ๋ฒกํ„ฐ๋กœ๋ถ€ํ„ฐ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ cos_sim๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. def cos_sim(A, B): return dot(A, B)/(norm(A)*norm(B)) return_answer ํ•จ์ˆ˜๋Š” ์ž„์˜์˜ ์งˆ๋ฌธ์ด ๋“ค์–ด์˜ค๋ฉด ํ•ด๋‹น ์งˆ๋ฌธ์˜ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ ๊ฐ’๊ณผ ์ฑ—๋ด‡ ๋ฐ์ดํ„ฐ์˜ ์ž„๋ฒ ๋”ฉ ์—ด. ์ฆ‰, train_data['embedding']์— ์ €์žฅํ•ด๋‘” ๋ชจ๋“  ์งˆ๋ฌธ ์ƒ˜ํ”Œ๋“ค์˜ ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ ๊ฐ’๋“ค์„ ์ „๋ถ€ ๋น„๊ตํ•˜์—ฌ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๊ฐ’์ด ๊ฐ€์žฅ ๋†’์€ ์งˆ๋ฌธ ์ƒ˜ํ”Œ์„ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ์งˆ๋ฌธ ์ƒ˜ํ”Œ๊ณผ ์ง์ด ๋˜๋Š” ๋‹ต๋ณ€ ์ƒ˜ํ”Œ์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. def return_answer(question) embedding = model.encode(question) train_data['score'] = train_data.apply(lambda x: cos_sim(x['embedding'], embedding), axis=1) return train_data.loc[train_data['score'].idxmax()]['A'] ์ด์ œ ์ฑ—๋ด‡์„ ํ…Œ์ŠคํŠธํ•ด ๋ด…์‹œ๋‹ค. return_answer('๊ฒฐํ˜ผํ•˜๊ณ  ์‹ถ์–ด') ์ข‹์€ ์‚ฌ๋žŒ์ด๋ž‘ ๊ฒฐํ˜ผํ•  ์ˆ˜ ์žˆ์„ ๊ฑฐ์˜ˆ์š”. return_answer('๋‚˜๋ž‘ ์ปคํ”ผ ๋จน์„๋ž˜?') ์นดํŽ˜์ธ์ด ํ•„์š”ํ•œ ์‹œ๊ฐ„์ธ๊ฐ€ ๋ด์š”. return_answer('๋ฐ˜๊ฐ€์›Œ') ์ €๋„ ๋ฐ˜๊ฐ€์›Œ์š”. return_answer('์‚ฌ๋ž‘ํ•ด') ์ƒ๋Œ€๋ฐฉ์—๊ฒŒ ์ „ํ•ด๋ณด์„ธ์š”. return_answer('๋„ˆ๋Š” ๋ˆ„๊ตฌ๋‹ˆ?') ์ €๋Š” ์œ„ ๋กœ๋ด‡์ž…๋‹ˆ๋‹ค. return_answer('๋„ˆ๋ฌด ์งœ์ฆ ๋‚˜') ์งœ์ฆ ๋‚  ๋• ์งœ์žฅ๋ฉด return_answer('ํ™”๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค') ํ™”๋ฅผ ์ฐธ๋Š” ์—ฐ์Šต์„ ํ•ด๋ณด์„ธ์š”. return_answer('๋‚˜๋ž‘ ๋†€์ž') ์ง€๊ธˆ ๊ทธ๋Ÿฌ๊ณ  ์žˆ์–ด์š”. return_answer('๋‚˜๋ž‘ ๊ฒŒ์ž„ํ•˜์ž') ๊ฐ™์ด ๋†€์•„์š”. return_answer('์ถœ๊ทผํ•˜๊ธฐ ์‹ซ์–ด') ์”ป๊ณ  ํ‘น ์‰ฌ์„ธ์š”. return_answer('์—ฌํ–‰ ๊ฐ€๊ณ  ์‹ถ๋‹ค') ์ด๊น€์— ๋– ๋‚˜๋ณด์„ธ์š”. return_answer('๋„ˆ ๋ง ์ž˜ํ•œ๋‹ค') ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ์žˆ์œผ๋ฉด ์ € ์ข€ ์†Œ๊ฐœํ•ด ์ฃผ์„ธ์š”. ์งง์€ ์งˆ๋ฌธ๋“ค์ด์ง€๋งŒ ๋Œ€๋ถ€๋ถ„์˜ ์งˆ๋ฌธ์—์„œ ๊ทธ๋Ÿด๋“ฏํ•œ ๋‹ต๋ณ€์„ ํ•˜๋Š” ๋ชจ์Šต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 18-09 Faiss์™€ SBERT๋ฅผ ์ด์šฉํ•œ ์‹œ๋งจํ‹ฑ ๊ฒ€์ƒ‰๊ธฐ(Semantic Search) ์‹œ๋งจํ‹ฑ ๊ฒ€์ƒ‰(Semantic search)์€ ๊ธฐ์กด์˜ ํ‚ค์›Œ๋“œ ๋งค์นญ์ด ์•„๋‹Œ ๋ฌธ์žฅ์˜ ์˜๋ฏธ์— ์ดˆ์ ์„ ๋งž์ถ˜ ์ •๋ณด ๊ฒ€์ƒ‰ ์‹œ์Šคํ…œ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” SBERT์™€ FAISS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ๊ฒ€์ƒ‰ ์—”์ง„์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. Faiss๋Š” ๋ฒกํ„ฐํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ธ๋ฑ์‹ฑํ•˜๊ณ  ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํšจ์œจ์ ์ธ ๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด Facebook AI์—์„œ ๊ตฌ์ถ•ํ•œ C ++ ๊ธฐ๋ฐ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. CPU ํ™˜๊ฒฝ์—์„œ ์ง„ํ–‰ํ•˜์‹ ๋‹ค๋ฉด faiss-gpu๊ฐ€ ์•„๋‹ˆ๋ผ faiss-cpu๋ฅผ ์„ค์น˜ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค !pip install faiss-gpu !pip install -U sentence-transformers 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ import numpy as np import os import pandas as pd import urllib.request import faiss import time from sentence_transformers import SentenceTransformer ์—ฌ๊ธฐ์„œ๋Š” ์•ฝ 100๋งŒ ๊ฐœ์˜ ๋‰ด์Šค ๊ธฐ์‚ฌ ์ œ๋ชฉ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/19.%20TopiC%20Modeling%20(LDA% 2C%20BERT-Based)/dataset/abcnews-date-text.csv", filename="abcnews-date-text.csv") df = pd.read_csv("abcnews-date-text.csv") data = df.headline_text.to_list() ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ ์ถœ๋ ฅ data[:5] ['aba decides against community broadcasting licence', 'act fire witnesses must be aware of defamation', 'a g calls for infrastructure protection summit', 'air nz staff in aust strike for pay rise', 'air nz strike to affect australian travellers'] ์ƒ˜ํ”Œ ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด ์•ฝ 108๋งŒ ๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. print('์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ :', len(data)) ์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 1082168 2. SBERT ์ž„๋ฒ ๋”ฉ ๋ชจ๋“  ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ SBERT๋กœ ์ž„๋ฒ ๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. model = SentenceTransformer('distilbert-base-nli-mean-tokens') encoded_data = model.encode(data) print('์ž„๋ฒ ๋”ฉ ๋œ ๋ฒกํ„ฐ ์ˆ˜ :', len(encoded_data)) ์ž„๋ฒ ๋”ฉ ๋œ ๋ฒกํ„ฐ ์ˆ˜ : 1082168 3. ์ธ๋ฑ์Šค ์ •์˜ ๋ฐ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ ์ธ๋ฑ์Šค๋ฅผ ์ •์˜ํ•˜๊ณ  ์—ฌ๊ธฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. index = faiss.IndexIDMap(faiss.IndexFlatIP(768)) index.add_with_ids(encoded_data, np.array(range(0, len(data)))) faiss.write_index(index, 'abc_news') 4. ๊ฒ€์ƒ‰ ๋ฐ ์‹œ๊ฐ„ ์ธก์ • ์‹ค์ œ ๊ฒ€์ƒ‰์„ ์ง„ํ–‰ํ•ด ๋ณด๊ณ  ์‹œ๊ฐ„์„ ์ธก์ •ํ•ด ๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฃผ์–ด์ง„ ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌ๋„๊ฐ€ ๋†’์€ ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถ”์ถœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. def search(query): t = time.time() query_vector = model.encode([query]) k = 5 top_k = index.search(query_vector, k) print('total time: {}'.format(time.time() - t)) return [data[_id] for _id in top_k[1].tolist()[0]] query = str(input()) results = search(query) print('results :') for result in results: print('\t', result) 'Underwater Forest Discovered'๋ผ๋Š” ์ž„์˜์˜ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Underwater Forest Discovered total time: 1.069244384765625 results : underwater loop thriving underwater antarctic garden discovered baton goes underwater in wa underwater footage shows inside doomed costa underwater uluru found off wa coast ์•ฝ 108๋งŒ ๊ฐœ์˜ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ์‹œ๋งจํ‹ฑ ๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜์˜€์Œ์—๋„ ์•ฝ 1์ดˆ ๋‚ด์™ธ์˜ ์‹œ๊ฐ„๋ฐ–์— ๊ฑธ๋ฆฌ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. 19. ํ† ํ”ฝ ๋ชจ๋ธ๋ง(Topic Modeling) ํ† ํ”ฝ(Topic)์€ ํ•œ๊ตญ์–ด๋กœ๋Š” ์ฃผ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ† ํ”ฝ ๋ชจ๋ธ๋ง(Topic Modeling)์ด๋ž€ ๊ธฐ๊ณ„ ํ•™์Šต ๋ฐ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ถ„์•ผ์—์„œ ํ† ํ”ฝ์ด๋ผ๋Š” ๋ฌธ์„œ ์ง‘ํ•ฉ์˜ ์ถ”์ƒ์ ์ธ ์ฃผ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ธฐ ์œ„ํ•œ ํ†ต๊ณ„์  ๋ชจ๋ธ ์ค‘ ํ•˜๋‚˜๋กœ, ํ…์ŠคํŠธ ๋ณธ๋ฌธ์˜ ์ˆจ๊ฒจ์ง„ ์˜๋ฏธ ๊ตฌ์กฐ๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํ…์ŠคํŠธ ๋งˆ์ด๋‹ ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. 19-01 ์ž ์žฌ ์˜๋ฏธ ๋ถ„์„(Latent Semantic Analysis, LSA) LSA๋Š” ์ •ํ™•ํžˆ๋Š” ํ† ํ”ฝ ๋ชจ๋ธ๋ง์„ ์œ„ํ•ด ์ตœ์ ํ™”๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•„๋‹ˆ์ง€๋งŒ, ํ† ํ”ฝ ๋ชจ๋ธ๋ง์ด๋ผ๋Š” ๋ถ„์•ผ์— ์•„์ด๋””์–ด๋ฅผ ์ œ๊ณตํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์— ํ† ํ”ฝ ๋ชจ๋ธ๋ง ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ LDA์— ์•ž์„œ ๋ฐฐ์›Œ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” LDA๋Š” LSA์˜ ๋‹จ์ ์„ ๊ฐœ์„ ํ•˜์—ฌ ํƒ„์ƒํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ† ํ”ฝ ๋ชจ๋ธ๋ง์— ๋ณด๋‹ค ์ ํ•ฉํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. BoW์— ๊ธฐ๋ฐ˜ํ•œ DTM์ด๋‚˜ TF-IDF๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹จ์–ด์˜ ๋นˆ๋„ ์ˆ˜๋ฅผ ์ด์šฉํ•œ ์ˆ˜์น˜ํ™” ๋ฐฉ๋ฒ•์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. (์ด๋ฅผ ํ† ํ”ฝ ๋ชจ๋ธ๋ง ๊ด€์ ์—์„œ๋Š” ๋‹จ์–ด์˜ ํ† ํ”ฝ์„ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ•œ๋‹ค๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค.) ์ด๋ฅผ ์œ„ํ•œ ๋Œ€์•ˆ์œผ๋กœ DTM์˜ ์ž ์žฌ๋œ(Latent) ์˜๋ฏธ๋ฅผ ์ด๋Œ์–ด๋‚ด๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ž ์žฌ ์˜๋ฏธ ๋ถ„์„(Latent Semantic Analysis, LSA)์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ž ์žฌ ์˜๋ฏธ ๋ถ„์„(Latent Semantic Indexing, LSI)์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ดํ•˜ LSA๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ ํ˜•๋Œ€์ˆ˜ํ•™์˜ ํŠน์ด ๊ฐ’ ๋ถ„ํ•ด(Singular Value Decomposition, SVD)๋ฅผ ์ดํ•ดํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ•˜ ์ด๋ฅผ SVD๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์‹ค์Šต์—์„œ๋Š” SVD๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ตฌ์ฒด์ ์ธ ์„ ํ˜•๋Œ€์ˆ˜ํ•™์— ๋Œ€ํ•ด์„œ๋Š” ์„ค๋ช…ํ•˜์ง€ ์•Š๊ณ , SVD๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ์˜๋ฏธ๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์— ์ดˆ์ ์„ ๋งž์ถฅ๋‹ˆ๋‹ค. 1. ํŠน์ด ๊ฐ’ ๋ถ„ํ•ด(Singular Value Decomposition, SVD) ์‹œ์ž‘ํ•˜๊ธฐ ์•ž์„œ, ์—ฌ๊ธฐ์„œ์˜ ํŠน์ด ๊ฐ’ ๋ถ„ํ•ด(Singular Value Decomposition, SVD)๋Š” ์‹ค์ˆ˜ ๋ฒกํ„ฐ ๊ณต๊ฐ„์— ํ•œ์ •ํ•˜์—ฌ ๋‚ด์šฉ์„ ์„ค๋ช…ํ•จ์„ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค. SVD๋ž€ A๊ฐ€ m ร— n ํ–‰๋ ฌ์ผ ๋•Œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด 3๊ฐœ์˜ ํ–‰๋ ฌ์˜ ๊ณฑ์œผ๋กœ ๋ถ„ํ•ด(decomposition) ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. = ฮฃ T ์—ฌ๊ธฐ์„œ ๊ฐ 3๊ฐœ์˜ ํ–‰๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ฉ๋‹ˆ๋‹ค. ์ง๊ตํ–‰๋ ฌ : ร— ์ง๊ตํ–‰๋ ฌ ( A = ( ฮฃ) T ) ์ง๊ตํ–‰๋ ฌ : ร— ์ง๊ตํ–‰๋ ฌ ( T = ( T) T ) ์ง์‚ฌ๊ฐ ๋Œ€๊ฐํ–‰๋ ฌ : ร— ์ง์‚ฌ๊ฐ ๋Œ€๊ฐํ–‰๋ ฌ ์—ฌ๊ธฐ์„œ ์ง๊ตํ–‰๋ ฌ(orthogonal matrix)์ด๋ž€ ์ž์‹ ๊ณผ ์ž์‹ ์˜ ์ „์น˜ ํ–‰๋ ฌ(transposed matrix)์˜ ๊ณฑ ๋˜๋Š” ์ด๋ฅผ ๋ฐ˜๋Œ€๋กœ ๊ณฑํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‹จ์œ„ํ–‰๋ ฌ(identity matrix)์ด ๋˜๋Š” ํ–‰๋ ฌ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋Œ€๊ฐํ–‰๋ ฌ(diagonal matrix)์ด๋ž€ ์ฃผ๋Œ€๊ฐ์„ ์„ ์ œ์™ธํ•œ ๊ณณ์˜ ์›์†Œ๊ฐ€ ๋ชจ๋‘ 0์ธ ํ–‰๋ ฌ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ SVD๋กœ ๋‚˜์˜จ ๋Œ€๊ฐ ํ–‰๋ ฌ์˜ ๋Œ€๊ฐ ์›์†Œ์˜ ๊ฐ’์„ ํ–‰๋ ฌ A์˜ ํŠน์ด ๊ฐ’(singular value)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋งŽ์€ ์šฉ์–ด๊ฐ€ ํ•œ๊บผ๋ฒˆ์— ๋‚˜์™€์„œ ๋ณต์žกํ•ด ๋ณด์ด๋Š”๋ฐ ์ฐจ๊ทผ, ์ฐจ๊ทผ ์šฉ์–ด๋ฅผ ์ •๋ฆฌํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์ „์น˜ ํ–‰๋ ฌ(Transposed Matrix) ์ „์น˜ ํ–‰๋ ฌ(transposed matrix)์€ ์›๋ž˜์˜ ํ–‰๋ ฌ์—์„œ ํ–‰๊ณผ ์—ด์„ ๋ฐ”๊พผ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ฃผ๋Œ€๊ฐ์„ ์„ ์ถ•์œผ๋กœ ๋ฐ˜์‚ฌ ๋Œ€์นญ์„ ํ•˜์—ฌ ์–ป๋Š” ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ๊ธฐํ˜ธ๋Š” ๊ธฐ์กด ํ–‰๋ ฌ ํ‘œํ˜„์˜ ์šฐ์ธก ์œ„์—๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๊ธฐ์กด์˜ ํ–‰๋ ฌ์„์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ์ „์น˜ ํ–‰๋ ฌ์€ T ์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. = [ 2 4 6 ] M = [ 3 5 4 6 ] 2) ๋‹จ์œ„ํ–‰๋ ฌ(Identity Matrix) ๋‹จ์œ„ํ–‰๋ ฌ(identity matrix)์€ ์ฃผ๋Œ€๊ฐ์„ ์˜ ์›์†Œ๊ฐ€ ๋ชจ๋‘ 1์ด๋ฉฐ ๋‚˜๋จธ์ง€ ์›์†Œ๋Š” ๋ชจ๋‘ 0์ธ ์ •์‚ฌ๊ฐ ํ–‰๋ ฌ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ์ค„์—ฌ์„œ ๋Œ€๋ฌธ์ž๋กœ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•˜๋Š”๋ฐ, 2 ร— 2 ๋‹จ์œ„ํ–‰๋ ฌ๊ณผ 3 ร— 3 ๋‹จ์œ„ํ–‰๋ ฌ์„ ํ‘œํ˜„ํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. = [ 0 1 ] I [ 0 0 1 0 0 1 ] 3) ์—ญํ–‰๋ ฌ(Inverse Matrix) ๋‹จ์œ„ํ–‰๋ ฌ(identity matrix)๋ฅผ ์ดํ•ดํ–ˆ๋‹ค๋ฉด ์—ญํ–‰๋ ฌ(inverse matrix)์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ–‰๋ ฌ ์™€ ์–ด๋–ค ํ–‰๋ ฌ์„ ๊ณฑํ–ˆ์„ ๋•Œ, ๊ฒฐ๊ณผ๋กœ์„œ ๋‹จ์œ„ํ–‰๋ ฌ์ด ๋‚˜์˜จ๋‹ค๋ฉด ์ด๋•Œ์˜ ์–ด๋–ค ํ–‰๋ ฌ์„ ์˜ ์—ญํ–‰๋ ฌ์ด๋ผ๊ณ  ํ•˜๋ฉฐ, โˆ’๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ร— โˆ’ = [ 2 3 5 6 8 9 ] [ ? ] [ 0 0 1 0 0 1 ] 4) ์ง๊ต ํ–‰๋ ฌ(Orthogonal matrix) ๋‹ค์‹œ ์ง๊ต ํ–‰๋ ฌ(orthogonal matrix)์˜ ์ •์˜๋กœ ๋Œ์•„๊ฐ€์„œ, ์‹ค์ˆ˜ ร— ํ–‰๋ ฌ์— ๋Œ€ํ•ด์„œ ร— T I ๋ฅผ ๋งŒ์กฑํ•˜๋ฉด์„œ T A I ์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰๋ ฌ ๋ฅผ ์ง๊ต ํ–‰๋ ฌ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ญํ–‰๋ ฌ์˜ ์ •์˜๋ฅผ ๋‹ค์‹œ ์ƒ๊ฐํ•ด ๋ณด๋ฉด, ๊ฒฐ๊ตญ ์ง๊ต ํ–‰๋ ฌ์€ โˆ’ = T ๋ฅผ ๋งŒ์กฑํ•ฉ๋‹ˆ๋‹ค. 5) ๋Œ€๊ฐ ํ–‰๋ ฌ(Diagonal matrix) ๋Œ€๊ฐํ–‰๋ ฌ(diagonal matrix)์€ ์ฃผ๋Œ€๊ฐ์„ ์„ ์ œ์™ธํ•œ ๊ณณ์˜ ์›์†Œ๊ฐ€ ๋ชจ๋‘ 0์ธ ํ–‰๋ ฌ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ฃผ๋Œ€๊ฐ์„ ์˜ ์›์†Œ๋ฅผ ๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋Œ€๊ฐ ํ–‰๋ ฌ ฮฃ๊ฐ€ 3 ร— 3 ํ–‰๋ ฌ์ด๋ผ๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชจ์–‘์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. = [ 0 0 a 0 0 a ] ์—ฌ๊ธฐ๊นŒ์ง„ ์ •์‚ฌ๊ฐ ํ–‰๋ ฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ง๊ด€์ ์œผ๋กœ ์ดํ•ด๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ •์‚ฌ๊ฐ ํ–‰๋ ฌ์ด ์•„๋‹ˆ๋ผ ์ง์‚ฌ๊ฐ ํ–‰๋ ฌ์ด ๋  ๊ฒฝ์šฐ๋ฅผ ์ž˜ ๋ณด์•„์•ผ ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ–‰์˜ ํฌ๊ธฐ๊ฐ€ ์—ด์˜ ํฌ๊ธฐ๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชจ์–‘์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ฆ‰, m ร— n ํ–‰๋ ฌ์ผ ๋•Œ, m > n์ธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. = [ 0 0 a 0 0 a 0 0 ] ๋ฐ˜๋ฉด n > m์ธ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชจ์–‘์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. = [ 0 0 0 a 0 0 0 a 0 ] ์—ฌ๊ธฐ๊นŒ์ง€๋Š” ์ผ๋ฐ˜์ ์ธ ๋Œ€๊ฐ ํ–‰๋ ฌ์— ๋Œ€ํ•œ ์ •์˜์ž…๋‹ˆ๋‹ค. SVD๋ฅผ ํ†ตํ•ด ๋‚˜์˜จ ๋Œ€๊ฐ ํ–‰๋ ฌ ฮฃ๋Š” ์ถ”๊ฐ€์ ์ธ ์„ฑ์งˆ์„ ๊ฐ€์ง€๋Š”๋ฐ, ๋Œ€๊ฐ ํ–‰๋ ฌ ฮฃ์˜ ์ฃผ๋Œ€ ๊ฐ์›์†Œ๋ฅผ ํ–‰๋ ฌ A์˜ ํŠน์ด ๊ฐ’(singular value)๋ผ๊ณ  ํ•˜๋ฉฐ, ์ด๋ฅด๋ ค๊ณ  ํ‘œํ˜„ํ•œ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ ํŠน์ด ๊ฐ’ ์€ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ํŠน์ด ๊ฐ’ 12.4, 9.5, 1.3์ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋ผ ์žˆ๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. = [ 12.4 0 0 9.5 0 0 1.3 ] 2. ์ ˆ๋‹จ๋œ SVD(Truncated SVD) ์œ„์—์„œ ์„ค๋ช…ํ•œ SVD๋ฅผ ํ’€ SVD(full SVD)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ LSA์˜ ๊ฒฝ์šฐ ํ’€ SVD์—์„œ ๋‚˜์˜จ 3๊ฐœ์˜ ํ–‰๋ ฌ์—์„œ ์ผ๋ถ€ ๋ฒกํ„ฐ๋“ค์„ ์‚ญ์ œ์‹œํ‚จ ์ ˆ๋‹จ๋œ SVD(truncated SVD)๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ ˆ๋‹จ๋œ SVD๋Š” ๋Œ€๊ฐ ํ–‰๋ ฌ ฮฃ์˜ ๋Œ€๊ฐ ์›์†Œ์˜ ๊ฐ’ ์ค‘์—์„œ ์ƒ์œ„ ๊ฐ’ t ๊ฐœ๋งŒ ๋‚จ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ ˆ๋‹จ๋œ SVD๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๊ฐ’์˜ ์†์‹ค์ด ์ผ์–ด๋‚˜๋ฏ€๋กœ ๊ธฐ์กด์˜ ํ–‰๋ ฌ A๋ฅผ ๋ณต๊ตฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, U ํ–‰๋ ฌ๊ณผ V ํ–‰๋ ฌ์˜ t ์—ด๊นŒ์ง€๋งŒ ๋‚จ๊น๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ t๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๊ณ ์ž ํ•˜๋Š” ํ† ํ”ฝ์˜ ์ˆ˜๋ฅผ ๋ฐ˜์˜ํ•œ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ž…๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ž€ ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ๊ฐ’์„ ์„ ํƒํ•˜๋ฉฐ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. t๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์ง€ ์•Š์€ ์ผ์ž…๋‹ˆ๋‹ค. t๋ฅผ ํฌ๊ฒŒ ์žก์œผ๋ฉด ๊ธฐ์กด์˜ ํ–‰๋ ฌ A๋กœ๋ถ€ํ„ฐ ๋‹ค์–‘ํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์žˆ์ง€๋งŒ, t๋ฅผ ์ž‘๊ฒŒ ์žก์•„์•ผ๋งŒ ๋…ธ์ด์ฆˆ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ผ๋ถ€ ๋ฒกํ„ฐ๋“ค์„ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์„ ๋ฐ์ดํ„ฐ์˜ ์ฐจ์›์„ ์ค„์ธ๋‹ค๊ณ ๋„ ๋งํ•˜๋Š”๋ฐ, ๋ฐ์ดํ„ฐ์˜ ์ฐจ์›์„ ์ค„์ด๊ฒŒ ๋˜๋ฉด ๋‹น์—ฐํžˆ ํ’€ SVD๋ฅผ ํ•˜์˜€์„ ๋•Œ๋ณด๋‹ค ์ง๊ด€์ ์œผ๋กœ ๊ณ„์‚ฐ ๋น„์šฉ์ด ๋‚ฎ์•„์ง€๋Š” ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ณ„์‚ฐ ๋น„์šฉ์ด ๋‚ฎ์•„์ง€๋Š” ๊ฒƒ ์™ธ์—๋„ ์ƒ๋Œ€์ ์œผ๋กœ ์ค‘์š”ํ•˜์ง€ ์•Š์€ ์ •๋ณด๋ฅผ ์‚ญ์ œํ•˜๋Š” ํšจ๊ณผ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š”๋ฐ, ์ด๋Š” ์˜์ƒ ์ฒ˜๋ฆฌ ๋ถ„์•ผ์—์„œ๋Š” ๋…ธ์ด์ฆˆ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๋ถ„์•ผ์—์„œ๋Š” ์„ค๋ช…๋ ฅ์ด ๋‚ฎ์€ ์ •๋ณด๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์„ค๋ช…๋ ฅ์ด ๋†’์€ ์ •๋ณด๋ฅผ ๋‚จ๊ธด๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๋‹ค์‹œ ๋งํ•˜๋ฉด ๊ธฐ์กด์˜ ํ–‰๋ ฌ์—์„œ๋Š” ๋“œ๋Ÿฌ๋‚˜์ง€ ์•Š์•˜๋˜ ์‹ฌ์ธต์ ์ธ ์˜๋ฏธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. 3. ์ž ์žฌ ์˜๋ฏธ ๋ถ„์„(Latent Semantic Analysis, LSA) ๊ธฐ์กด์˜ DTM์ด๋‚˜ DTM์— ๋‹จ์–ด์˜ ์ค‘์š”๋„์— ๋”ฐ๋ฅธ ๊ฐ€์ค‘์น˜๋ฅผ ์ฃผ์—ˆ๋˜ TF-IDF ํ–‰๋ ฌ์€ ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ์ „ํ˜€ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋‹จ์ ์„ ๊ฐ–๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. LSA๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ DTM์ด๋‚˜ TF-IDF ํ–‰๋ ฌ์— ์ ˆ๋‹จ๋œ SVD(truncated SVD)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฐจ์›์„ ์ถ•์†Œ์‹œํ‚ค๊ณ , ๋‹จ์–ด๋“ค์˜ ์ž ์žฌ์ ์ธ ์˜๋ฏธ๋ฅผ ๋Œ์–ด๋‚ธ๋‹ค๋Š” ์•„์ด๋””์–ด๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์Šต์„ ํ†ตํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ณผ์ผ์ด ๊ธธ๊ณ  ๋…ธ๋ž€ ๋จน๊ณ  ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์‹ถ์€ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 0 0 0 1 0 1 1 0 ๋ฌธ์„œ 2 0 0 0 1 1 0 1 0 ๋ฌธ์„œ 3 0 1 1 0 2 0 0 0 ๋ฌธ์„œ 4 1 0 0 0 0 0 0 1 1) Full SVD import numpy as np ์œ„์™€ ๊ฐ™์€ DTM์„ ์‹ค์ œ๋กœ ํŒŒ์ด์ฌ์„ ํ†ตํ•ด์„œ ๋งŒ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. A = np.array([[0,0,0,1,0,1,1,0,0],[0,0,0,1,1,0,1,0,0],[0,1,1,0,2,0,0,0,0],[1,0,0,0,0,0,0,1,1]]) print('DTM์˜ ํฌ๊ธฐ(shape) :', np.shape(A)) DTM์˜ ํฌ๊ธฐ(shape) : (4, 9) 4 ร— 9์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” DTM์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ ํ’€ SVD(full SVD)๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ, ์—ฌ๊ธฐ์„œ๋Š” ๋Œ€๊ฐ ํ–‰๋ ฌ์˜ ๋ณ€์ˆ˜๋ช…์„ ฮฃ๊ฐ€ ์•„๋‹ˆ๋ผ S๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ V์˜ ์ „์น˜ ํ–‰๋ ฌ์„ VT๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์†Œ์ˆ˜์ ์˜ ๊ธธ์ด๊ฐ€ ๋„ˆ๋ฌด ๊ธธ๊ฒŒ ์ถœ๋ ฅํ•˜๋ฉด ๋ณด๊ธฐ ํž˜๋“ค์–ด์„œ ๋‘ ๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ. round(2)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. U, s, VT = np.linalg.svd(A, full_matrices = True) print('ํ–‰๋ ฌ U :') print(U.round(2)) print('ํ–‰๋ ฌ U์˜ ํฌ๊ธฐ(shape) :',np.shape(U)) ํ–‰๋ ฌ U : [[-0.24 0.75 0. -0.62] [-0.51 0.44 -0. 0.74] [-0.83 -0.49 -0. -0.27] [-0. -0. 1. 0. ]] ํ–‰๋ ฌ U์˜ ํฌ๊ธฐ(shape) : (4, 4) 4 ร— 4์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ์ง๊ต ํ–‰๋ ฌ U๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋Œ€๊ฐ ํ–‰๋ ฌ S๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('ํŠน์ด ๊ฐ’ ๋ฒกํ„ฐ :') print(s.round(2)) print('ํŠน์ด ๊ฐ’ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ(shape) :',np.shape(s)) ํŠน์ด ๊ฐ’ ๋ฒกํ„ฐ : [2.69 2.05 1.73 0.77] ํŠน์ด ๊ฐ’ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ(shape) : (4, ) Numpy์˜ linalg.svd()๋Š” ํŠน์ด ๊ฐ’ ๋ถ„ํ•ด์˜ ๊ฒฐ๊ณผ๋กœ ๋Œ€๊ฐ ํ–‰๋ ฌ์ด ์•„๋‹ˆ๋ผ ํŠน์ด ๊ฐ’์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์•ž์„œ ๋ณธ ์ˆ˜์‹์˜<NAME>์œผ๋กœ ๋ณด๋ ค๋ฉด ์ด๋ฅผ ๋‹ค์‹œ ๋Œ€๊ฐ ํ–‰๋ ฌ๋กœ ๋ฐ”๊พธ์–ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ํŠน์ด ๊ฐ’์„ s์— ์ €์žฅํ•˜๊ณ  ๋Œ€๊ฐ ํ–‰๋ ฌ ํฌ๊ธฐ์˜ ํ–‰๋ ฌ์„ ์ƒ์„ฑํ•œ ํ›„์— ๊ทธ ํ–‰๋ ฌ์— ํŠน์ด ๊ฐ’์„ ์‚ฝ์ž…ํ•ด๋„ ๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ๋Œ€๊ฐ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์ธ 4 x 9์˜ ์ž„์˜์˜ ํ–‰๋ ฌ ์ƒ์„ฑ S = np.zeros((4, 9)) # ํŠน์ด ๊ฐ’์„ ๋Œ€๊ฐํ–‰๋ ฌ์— ์‚ฝ์ž… S[:4, :4] = np.diag(s) print('๋Œ€๊ฐ ํ–‰๋ ฌ S :') print(S.round(2)) print('๋Œ€๊ฐ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) :') print(np.shape(S)) ๋Œ€๊ฐ ํ–‰๋ ฌ S : [[2.69 0. 0. 0. 0. 0. 0. 0. 0. ] [0. 2.05 0. 0. 0. 0. 0. 0. 0. ] [0. 0. 1.73 0. 0. 0. 0. 0. 0. ] [0. 0. 0. 0.77 0. 0. 0. 0. 0. ]] ๋Œ€๊ฐ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) : (4, 9) 4 ร— 9์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ๋Œ€๊ฐ ํ–‰๋ ฌ S๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 2.69 > 2.05 > 1.73 > 0.77 ์ˆœ์œผ๋กœ ๊ฐ’์ด ๋‚ด๋ฆผ์ฐจ์ˆœ์„ ๋ณด์ด๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print('์ง๊ตํ–‰๋ ฌ VT :') print(VT.round(2)) print('์ง๊ต ํ–‰๋ ฌ VT์˜ ํฌ๊ธฐ(shape) :') print(np.shape(VT)) ์ง๊ตํ–‰๋ ฌ VT : [[-0. -0.31 -0.31 -0.28 -0.8 -0.09 -0.28 -0. -0. ] [ 0. -0.24 -0.24 0.58 -0.26 0.37 0.58 -0. -0. ] [ 0.58 -0. 0. 0. -0. 0. -0. 0.58 0.58] [ 0. -0.35 -0.35 0.16 0.25 -0.8 0.16 -0. -0. ] [-0. -0.78 -0.01 -0.2 0.4 0.4 -0.2 0. 0. ] [-0.29 0.31 -0.78 -0.24 0.23 0.23 0.01 0.14 0.14] [-0.29 -0.1 0.26 -0.59 -0.08 -0.08 0.66 0.14 0.14] [-0.5 -0.06 0.15 0.24 -0.05 -0.05 -0.19 0.75 -0.25] [-0.5 -0.06 0.15 0.24 -0.05 -0.05 -0.19 -0.25 0.75]] ์ง๊ต ํ–‰๋ ฌ VT์˜ ํฌ๊ธฐ(shape) : (9, 9) 9 ร— 9์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ์ง๊ต ํ–‰๋ ฌ VT(V์˜ ์ „์น˜ ํ–‰๋ ฌ)๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, U ร— S ร— VT๋ฅผ ํ•˜๋ฉด ๊ธฐ์กด์˜ ํ–‰๋ ฌ A๊ฐ€ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. Numpy์˜ allclose()๋Š” 2๊ฐœ์˜ ํ–‰๋ ฌ์ด ๋™์ผํ•˜๋ฉด True๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ง๋กœ ๊ธฐ์กด์˜ ํ–‰๋ ฌ A์™€ ๋™์ผํ•œ์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. np.allclose(A, np.dot(np.dot(U, S), VT).round(2)) True 2) ์ ˆ๋‹จ๋œ SVD(Truncated SVD) ์ง€๊ธˆ๊นŒ์ง€ ์ˆ˜ํ–‰ํ•œ ๊ฒƒ์€ ํ’€ SVD(Full SVD)์ž…๋‹ˆ๋‹ค. ์ด์ œ t๋ฅผ ์ •ํ•˜๊ณ , ์ ˆ๋‹จ๋œ SVD(Truncated SVD)๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” t=2๋กœ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ๋Œ€๊ฐ ํ–‰๋ ฌ S ๋‚ด์˜ ํŠน์ด ๊ฐ’ ์ค‘์—์„œ ์ƒ์œ„ 2๊ฐœ๋งŒ ๋‚จ๊ธฐ๊ณ  ์ œ๊ฑฐํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ํŠน์ด ๊ฐ’ ์ƒ์œ„ 2๊ฐœ๋งŒ ๋ณด์กด S = S[:2, :2] print('๋Œ€๊ฐ ํ–‰๋ ฌ S :') print(S.round(2)) [[2.69 0. ] [0. 2.05]] ์ƒ์œ„ 2๊ฐœ์˜ ๊ฐ’๋งŒ ๋‚จ๊ธฐ๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋ชจ๋‘ ์ œ๊ฑฐ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ง๊ต ํ–‰๋ ฌ U์— ๋Œ€ํ•ด์„œ๋„ 2๊ฐœ์˜ ์—ด๋งŒ ๋‚จ๊ธฐ๊ณ  ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. U = U[:,:2] print('ํ–‰๋ ฌ U :') print(U.round(2)) ํ–‰๋ ฌ U : [[-0.24 0.75] [-0.51 0.44] [-0.83 -0.49] [-0. -0. ]] 2๊ฐœ์˜ ์—ด๋งŒ ๋‚จ๊ธฐ๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐ๊ฐ€ ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ–‰๋ ฌ V์˜ ์ „์น˜ ํ–‰๋ ฌ์ธ VT์— ๋Œ€ํ•ด์„œ 2๊ฐœ์˜ ํ–‰๋งŒ ๋‚จ๊ธฐ๊ณ  ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” V ๊ด€์ ์—์„œ๋Š” 2๊ฐœ์˜ ์—ด๋งŒ ๋‚จ๊ธฐ๊ณ  ์ œ๊ฑฐํ•œ ๊ฒƒ์ด ๋ฉ๋‹ˆ๋‹ค. VT = VT[:2, :] print('์ง๊ตํ–‰๋ ฌ VT :') print(VT.round(2)) ์ง๊ตํ–‰๋ ฌ VT : [[-0. -0.31 -0.31 -0.28 -0.8 -0.09 -0.28 -0. -0. ] [ 0. -0.24 -0.24 0.58 -0.26 0.37 0.58 -0. -0. ]] ์ด์ œ ์ถ•์†Œ๋œ ํ–‰๋ ฌ U, S, VT์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ U ร— S ร— VT ์—ฐ์‚ฐ์„ ํ•˜๋ฉด ๊ธฐ์กด์˜ A์™€๋Š” ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ’์ด ์†์‹ค๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ์„ธ ๊ฐœ์˜ ํ–‰๋ ฌ๋กœ๋Š” ์ด์ œ ๊ธฐ์กด์˜ A ํ–‰๋ ฌ์„ ๋ณต๊ตฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. U ร— S ร— VT ์—ฐ์‚ฐ์„ ํ•ด์„œ ๋‚˜์˜ค๋Š” ๊ฐ’์„ A_prime์ด๋ผ ํ•˜๊ณ  ๊ธฐ์กด์˜ ํ–‰๋ ฌ A์™€ ๊ฐ’์„ ๋น„๊ตํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A_prime = np.dot(np.dot(U, S), VT) print(A) print(A_prime.round(2)) [[0 0 0 1 0 1 1 0 0] [0 0 0 1 1 0 1 0 0] [0 1 1 0 2 0 0 0 0] [1 0 0 0 0 0 0 1 1]] [[ 0. -0.17 -0.17 1.08 0.12 0.62 1.08 -0. -0. ] [ 0. 0.2 0.2 0.91 0.86 0.45 0.91 0. 0. ] [ 0. 0.93 0.93 0.03 2.05 -0.17 0.03 0. 0. ] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] ๋Œ€์ฒด์ ์œผ๋กœ ๊ธฐ์กด์— 0์ธ ๊ฐ’๋“ค์€ 0์— ๊ฐ€๊ฐ€์šด ๊ฐ’์ด ๋‚˜์˜ค๊ณ , 1์ธ ๊ฐ’๋“ค์€ 1์— ๊ฐ€๊นŒ์šด ๊ฐ’์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฐ’์ด ์ œ๋Œ€๋กœ ๋ณต๊ตฌ๋˜์ง€ ์•Š์€ ๊ตฌ๊ฐ„๋„ ์กด์žฌํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ ‡๊ฒŒ ์ฐจ์›์ด ์ถ•์†Œ๋œ U, S, VT์˜ ํฌ๊ธฐ๊ฐ€ ์–ด๋–ค ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค. ์ถ•์†Œ๋œ U๋Š” 4 ร— 2์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ, ์ด๋Š” ์ž˜ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๋ฌธ์„œ์˜ ๊ฐœ์ˆ˜ ร— ํ† ํ”ฝ์˜ ์ˆ˜ t์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜์ธ 9๋Š” ์œ ์ง€๋˜์ง€ ์•Š๋Š”๋ฐ ๋ฌธ์„œ์˜ ๊ฐœ์ˆ˜์ธ 4์˜ ํฌ๊ธฐ๊ฐ€ ์œ ์ง€๋˜์—ˆ์œผ๋‹ˆ 4๊ฐœ์˜ ๋ฌธ์„œ ๊ฐ๊ฐ์„ 2๊ฐœ์˜ ๊ฐ’์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, U์˜ ๊ฐ ํ–‰์€ ์ž ์žฌ ์˜๋ฏธ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜์น˜ํ™”๋œ ๊ฐ๊ฐ์˜ ๋ฌธ์„œ ๋ฒกํ„ฐ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ•์†Œ๋œ VT๋Š” 2 ร— 9์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ, ์ด๋Š” ์ž˜ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ํ† ํ”ฝ์˜ ์ˆ˜ t ร— ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. VT์˜ ๊ฐ ์—ด์€ ์ž ์žฌ ์˜๋ฏธ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜์น˜ํ™”๋œ ๊ฐ๊ฐ์˜ ๋‹จ์–ด ๋ฒกํ„ฐ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์„œ ๋ฒกํ„ฐ๋“ค๊ณผ ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์„ ํ†ตํ•ด ๋‹ค๋ฅธ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„, ๋‹ค๋ฅธ ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„, ๋‹จ์–ด(์ฟผ๋ฆฌ)๋กœ๋ถ€ํ„ฐ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ๋“ค์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค. 4. ์‹ค์Šต์„ ํ†ตํ•œ ์ดํ•ด ์‚ฌ์ดํ‚ท๋Ÿฐ์—์„œ๋Š” Twenty Newsgroups์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” 20๊ฐœ์˜ ๋‹ค๋ฅธ ์ฃผ์ œ๋ฅผ ๊ฐ€์ง„ ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด LSA๊ฐ€ ํ† ํ”ฝ ๋ชจ๋ธ๋ง์— ์ตœ์ ํ™”๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•„๋‹ˆ์ง€๋งŒ, ํ† ํ”ฝ ๋ชจ๋ธ๋ง์ด๋ผ๋Š” ๋ถ„์•ผ์˜ ์‹œ์ดˆ๊ฐ€ ๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” LSA๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์„œ์˜ ์ˆ˜๋ฅผ ์›ํ•˜๋Š” ํ† ํ”ฝ์˜ ์ˆ˜๋กœ ์••์ถ•ํ•œ ๋’ค์— ๊ฐ ํ† ํ”ฝ๋‹น ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋‹จ์–ด 5๊ฐœ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์‹ค์Šต์œผ๋กœ ํ† ํ”ฝ ๋ชจ๋ธ๋ง์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ๋Š” ๋‰ด์Šค ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. 1) ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด import pandas as pd from sklearn.datasets import fetch_20newsgroups import nltk from nltk.corpus import stopwords from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.decomposition import TruncatedSVD dataset = fetch_20newsgroups(shuffle=True, random_state=1, remove=('headers', 'footers', 'quotes')) documents = dataset.data print('์ƒ˜ํ”Œ์˜ ์ˆ˜ :',len(documents)) ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 11314 ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•  ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ๋Š” ์ด 11,314๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด ์ค‘ ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. documents[1] "\n\n\n\n\n\n\nYeah, do you expect people to read the FAQ, etc. and actually accept hard\natheism? No, you need a little leap of faith, Jimmy. Your logic runs out\nof steam!\n\n\n\n\n\n\n\nJim,\n\nSorry I can't pity you, Jim. And I'm sorry that you have these feelings of\ndenial about the faith you need to get by. Oh well, just pretend that it will\nall end happily ever after anyway. Maybe if you start a new newsgroup,\nalt.atheist.hard, you won't be bummin' so much?\n\n\n\n\n\n\nBye-Bye, Big Jim. Don't forget your Flintstone's Chewables! :) \n--\nBake Timmons, III" ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ์—๋Š” ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ํฌํ•จ๋œ ๋‹ค์ˆ˜์˜ ์˜์–ด ๋ฌธ์žฅ์œผ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ<NAME>์˜ ์ƒ˜ํ”Œ์ด ์ด 11,314๊ฐœ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์ด ์ œ๊ณตํ•˜๋Š” ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ์—์„œ target_name์—๋Š” ๋ณธ๋ž˜ ์ด ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค 20๊ฐœ์˜ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๊ฐ–๊ณ  ์žˆ์—ˆ๋Š”์ง€๊ฐ€ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(dataset.target_names) ['alt.atheism', 'comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware', 'comp.windows.x', 'misc.forsale', 'rec.autos', 'rec.motorcycles', 'rec.sport.baseball', 'rec.sport.hockey', 'sci.crypt', 'sci.electronics', 'sci.med', 'sci.space', 'soc.religion.christian', 'talk.politics.guns', 'talk.politics.mideast', 'talk.politics.misc', 'talk.religion.misc'] 2) ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ ์‹œ์ž‘ํ•˜๊ธฐ ์•ž์„œ, ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๊ฐ€๋Šฅํ•œ ํ•œ ์ •์ œ ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ์•„์ด๋””์–ด๋Š” ์•ŒํŒŒ๋ฒณ์„ ์ œ์™ธํ•œ ๊ตฌ๋‘์ , ์ˆซ์ž, ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ ์ฑ•ํ„ฐ์—์„œ ์ •์ œ ๊ธฐ๋ฒ•์œผ๋กœ ๋ฐฐ์› ๋˜ ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ†ตํ•ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์งง์€ ๋‹จ์–ด๋Š” ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์ง€ ์•Š๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์„ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ด๋Š” ์ž‘์—…์„ ํ•ฉ๋‹ˆ๋‹ค. news_df = pd.DataFrame({'document':documents}) # ํŠน์ˆ˜ ๋ฌธ์ž ์ œ๊ฑฐ news_df['clean_doc'] = news_df['document'].str.replace("[^a-zA-Z]", " ") # ๊ธธ์ด๊ฐ€ 3์ดํ•˜์ธ ๋‹จ์–ด๋Š” ์ œ๊ฑฐ (๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด ์ œ๊ฑฐ) news_df['clean_doc'] = news_df['clean_doc'].apply(lambda x: ' '.join([w for w in x.split() if len(w)>3])) # ์ „์ฒด ๋‹จ์–ด์— ๋Œ€ํ•œ ์†Œ๋ฌธ์ž ๋ณ€ํ™˜ news_df['clean_doc'] = news_df['clean_doc'].apply(lambda x: x.lower()) ๋ฐ์ดํ„ฐ์˜ ์ •์ œ๊ฐ€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•˜์—ฌ ์ •์ œ ์ „, ํ›„์— ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. news_df['clean_doc'][1] 'yeah expect people read actually accept hard atheism need little leap faith jimmy your logic runs steam sorry pity sorry that have these feelings denial about faith need well just pretend that will happily ever after anyway maybe start newsgroup atheist hard bummin much forget your flintstone chewables bake timmons' ์šฐ์„  ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ์ œ๊ฑฐ๋˜์—ˆ์œผ๋ฉฐ, if๋‚˜ you์™€ ๊ฐ™์€ ๊ธธ์ด๊ฐ€ 3์ดํ•˜์ธ ๋‹จ์–ด๊ฐ€ ์ œ๊ฑฐ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋Œ€๋ฌธ์ž๊ฐ€ ์ „๋ถ€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๋€Œ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ์—์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ† ํฐํ™”๋ฅผ ์šฐ์„  ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐํ™”์™€ ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. # NLTK๋กœ๋ถ€ํ„ฐ ๋ถˆ์šฉ์–ด๋ฅผ ๋ฐ›์•„์˜จ๋‹ค. stop_words = stopwords.words('english') tokenized_doc = news_df['clean_doc'].apply(lambda x: x.split()) # ํ† ํฐํ™” tokenized_doc = tokenized_doc.apply(lambda x: [item for item in x if item not in stop_words]) # ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. print(tokenized_doc[1]) ['yeah', 'expect', 'people', 'read', 'actually', 'accept', 'hard', 'atheism', 'need', 'little', 'leap', 'faith', 'jimmy', 'logic', 'runs', 'steam', 'sorry', 'pity', 'sorry', 'feelings', 'denial', 'faith', 'need', 'well', 'pretend', 'happily', 'ever', 'anyway', 'maybe', 'start', 'newsgroup', 'atheist', 'hard', 'bummin', 'much', 'forget', 'flintstone', 'chewables', 'bake', 'timmons'] ๊ธฐ์กด์— ์žˆ์—ˆ๋˜ ๋ถˆ์šฉ์–ด์— ์†ํ•˜๋˜ your, about, just, that, will, after ๋‹จ์–ด๋“ค์ด ์‚ฌ๋ผ์กŒ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ํ† ํฐ ํ™”๊ฐ€ ์ˆ˜ํ–‰๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3) TF-IDF ํ–‰๋ ฌ ๋งŒ๋“ค๊ธฐ ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด ํ† ํฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์˜€์ง€๋งŒ, TfidfVectorizer(TF-IDF ์‹ค์Šต ์ฐธ๊ณ )๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ† ํฐ ํ™”๊ฐ€ ๋˜์–ด์žˆ์ง€ ์•Š์€ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— TfidfVectorizer๋ฅผ ์‚ฌ์šฉํ•ด์„œ TF-IDF ํ–‰๋ ฌ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์‹œ ํ† ํฐํ™” ์ž‘์—…์„ ์—ญ์œผ๋กœ ์ทจ์†Œํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์—ญ ํ† ํฐ ํ™”(Detokenization)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. # ์—ญ ํ† ํฐ ํ™” (ํ† ํฐํ™” ์ž‘์—…์„ ์—ญ์œผ๋กœ ๋˜๋Œ๋ฆผ) detokenized_doc = [] for i in range(len(news_df)): t = ' '.join(tokenized_doc[i]) detokenized_doc.append(t) news_df['clean_doc'] = detokenized_doc ์—ญ ํ† ํฐ ํ™”๊ฐ€ ์ œ๋Œ€๋กœ ๋˜์—ˆ๋Š”์ง€ ๋‹ค์‹œ ์ฒซ ๋ฒˆ์งธ ํ›ˆ๋ จ์šฉ ๋‰ด์Šค๊ทธ๋ฃน ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•˜์—ฌ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. news_df['clean_doc'][1] 'yeah expect people read actually accept hard atheism need little leap faith jimmy logic runs steam sorry pity sorry feelings denial faith need well pretend happily ever anyway maybe start newsgroup atheist hard bummin much forget flintstone chewables bake timmons' ์ •์ƒ์ ์œผ๋กœ ๋ถˆ์šฉ์–ด๊ฐ€ ์ œ๊ฑฐ๋œ ์ƒํƒœ์—์„œ ์—ญ ํ† ํฐ ํ™”๊ฐ€ ์ˆ˜ํ–‰๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ TfidfVectorizer๋ฅผ ํ†ตํ•ด ๋‹จ์–ด 1,000๊ฐœ์— ๋Œ€ํ•œ TF-IDF ํ–‰๋ ฌ์„ ๋งŒ๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๊ฐ€์ง€๊ณ  ํ–‰๋ ฌ์„ ๋งŒ๋“ค ์ˆ˜๋Š” ์žˆ๊ฒ ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” 1,000๊ฐœ์˜ ๋‹จ์–ด๋กœ ์ œํ•œํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. vectorizer = TfidfVectorizer(stop_words='english', max_features= 1000, # ์ƒ์œ„ 1,000๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๋ณด์กด max_df = 0.5, smooth_idf=True) X = vectorizer.fit_transform(news_df['clean_doc']) # TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ ํ™•์ธ print('TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ :',X.shape) TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ : (11314, 1000) 11,314 ร— 1,000์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ TF-IDF ํ–‰๋ ฌ์ด ์ƒ์„ฑ๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4) ํ† ํ”ฝ ๋ชจ๋ธ๋ง(Topic Modeling) ์ด์ œ TF-IDF ํ–‰๋ ฌ์„ ๋‹ค์ˆ˜์˜ ํ–‰๋ ฌ๋กœ ๋ถ„ํ•ดํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ ์ ˆ๋‹จ๋œ SVD(Truncated SVD)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ ˆ๋‹จ๋œ SVD๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฐจ์›์„ ์ถ•์†Œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›๋ž˜ ๊ธฐ์กด ๋‰ด์Šค๊ทธ๋ฃน ๋ฐ์ดํ„ฐ๊ฐ€ 20๊ฐœ์˜ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๊ฐ–๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, 20๊ฐœ์˜ ํ† ํ”ฝ์„ ๊ฐ€์กŒ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ํ† ํ”ฝ ๋ชจ๋ธ๋ง์„ ์‹œ๋„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ† ํ”ฝ์˜ ์ˆซ์ž๋Š” n_components์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ง€์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. svd_model = TruncatedSVD(n_components=20, algorithm='randomized', n_iter=100, random_state=122) svd_model.fit(X) len(svd_model.components_) 20 ์—ฌ๊ธฐ์„œ svd_model.componets_๋Š” ์•ž์„œ ๋ฐฐ์šด LSA์—์„œ VT์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. np.shape(svd_model.components_) (20, 1000) ์ •ํ™•ํ•˜๊ฒŒ ํ† ํ”ฝ์˜ ์ˆ˜ t ร— ๋‹จ์–ด์˜ ์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. terms = vectorizer.get_feature_names() # ๋‹จ์–ด ์ง‘ํ•ฉ. 1,000๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์ €์žฅ๋จ. def get_topics(components, feature_names, n=5): for idx, topic in enumerate(components): print("Topic %d:" % (idx+1), [(feature_names[i], topic[i].round(5)) for i in topic.argsort()[:-n - 1:-1]]) get_topics(svd_model.components_,terms) ๊ฐ 20๊ฐœ์˜ ํ–‰์˜ ๊ฐ 1,000๊ฐœ์˜ ์—ด ์ค‘ ๊ฐ€์žฅ ๊ฐ’์ด ํฐ 5๊ฐœ์˜ ๊ฐ’์„ ์ฐพ์•„์„œ ๋‹จ์–ด๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. Topic 1: [('like', 0.2138), ('know', 0.20031), ('people', 0.19334), ('think', 0.17802), ('good', 0.15105)] Topic 2: [('thanks', 0.32918), ('windows', 0.29093), ('card', 0.18016), ('drive', 0.1739), ('mail', 0.15131)] Topic 3: [('game', 0.37159), ('team', 0.32533), ('year', 0.28205), ('games', 0.25416), ('season', 0.18464)] Topic 4: [('drive', 0.52823), ('scsi', 0.20043), ('disk', 0.15518), ('hard', 0.15511), ('card', 0.14049)] Topic 5: [('windows', 0.40544), ('file', 0.25619), ('window', 0.1806), ('files', 0.16196), ('program', 0.14009)] Topic 6: [('government', 0.16085), ('chip', 0.16071), ('mail', 0.15626), ('space', 0.15047), ('information', 0.13582)] Topic 7: [('like', 0.67121), ('bike', 0.14274), ('know', 0.11189), ('chip', 0.11043), ('sounds', 0.10389)] Topic 8: [('card', 0.44948), ('sale', 0.21639), ('video', 0.21318), ('offer', 0.14896), ('monitor', 0.1487)] Topic 9: [('know', 0.44869), ('card', 0.35699), ('chip', 0.17169), ('video', 0.15289), ('government', 0.15069)] Topic 10: [('good', 0.41575), ('know', 0.23137), ('time', 0.18933), ('bike', 0.11317), ('jesus', 0.09421)] Topic 11: [('think', 0.7832), ('chip', 0.10776), ('good', 0.10613), ('thanks', 0.08985), ('clipper', 0.07882)] Topic 12: [('thanks', 0.37279), ('right', 0.21787), ('problem', 0.2172), ('good', 0.21405), ('bike', 0.2116)] Topic 13: [('good', 0.36691), ('people', 0.33814), ('windows', 0.28286), ('know', 0.25238), ('file', 0.18193)] Topic 14: [('space', 0.39894), ('think', 0.23279), ('know', 0.17956), ('nasa', 0.15218), ('problem', 0.12924)] Topic 15: [('space', 0.3092), ('good', 0.30207), ('card', 0.21615), ('people', 0.20208), ('time', 0.15716)] Topic 16: [('people', 0.46951), ('problem', 0.20879), ('window', 0.16), ('time', 0.13873), ('game', 0.13616)] Topic 17: [('time', 0.3419), ('bike', 0.26896), ('right', 0.26208), ('windows', 0.19632), ('file', 0.19145)] Topic 18: [('time', 0.60079), ('problem', 0.15209), ('file', 0.13856), ('think', 0.13025), ('israel', 0.10728)] Topic 19: [('file', 0.4489), ('need', 0.25951), ('card', 0.1876), ('files', 0.17632), ('problem', 0.1491)] Topic 20: [('problem', 0.32797), ('file', 0.26268), ('thanks', 0.23414), ('used', 0.19339), ('space', 0.13861)] 5. LSA์˜ ์žฅ๋‹จ์ (Pros and Cons of LSA) ์ •๋ฆฌํ•ด ๋ณด๋ฉด LSA๋Š” ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹จ์–ด์˜ ์ž ์žฌ์ ์ธ ์˜๋ฏธ๋ฅผ ์ด๋Œ์–ด๋‚ผ ์ˆ˜ ์žˆ์–ด ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„ ๊ณ„์‚ฐ ๋“ฑ์—์„œ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค€๋‹ค๋Š” ์žฅ์ ์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ SVD์˜ ํŠน์„ฑ์ƒ ์ด๋ฏธ ๊ณ„์‚ฐ๋œ LSA์— ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ณ„์‚ฐํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋ณดํ†ต ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ƒˆ๋กœ์šด ์ •๋ณด์— ๋Œ€ํ•ด ์—…๋ฐ์ดํŠธ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ตœ๊ทผ LSA ๋Œ€์‹  Word2Vec ๋“ฑ ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ๋ฒกํ„ฐํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋ก ์ธ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๊ธฐ๋ฐ˜์˜ ๋ฐฉ๋ฒ•๋ก ์ด ๊ฐ๊ด‘๋ฐ›๋Š” ์ด์œ ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 19-02 ์ž ์žฌ ๋””๋ฆฌํด๋ ˆ ํ• ๋‹น(Latent Dirichlet Allocation, LDA) ํ† ํ”ฝ ๋ชจ๋ธ๋ง์€ ๋ฌธ์„œ์˜ ์ง‘ํ•ฉ์—์„œ ํ† ํ”ฝ์„ ์ฐพ์•„๋‚ด๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ฒ€์ƒ‰ ์—”์ง„, ๊ณ ๊ฐ ๋ฏผ์› ์‹œ์Šคํ…œ ๋“ฑ๊ณผ ๊ฐ™์ด ๋ฌธ์„œ์˜ ์ฃผ์ œ๋ฅผ ์•Œ์•„๋‚ด๋Š” ์ผ์ด ์ค‘์š”ํ•œ ๊ณณ์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ž ์žฌ ๋””๋ฆฌํด๋ ˆ ํ• ๋‹น(Latent Dirichlet Allocation, LDA)์€ ํ† ํ”ฝ ๋ชจ๋ธ๋ง์˜ ๋Œ€ํ‘œ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ค„์—ฌ์„œ LDA๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. LDA๋Š” ๋ฌธ์„œ๋“ค์€ ํ† ํ”ฝ๋“ค์˜ ํ˜ผํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์œผ๋ฉฐ, ํ† ํ”ฝ๋“ค์€ ํ™•๋ฅ  ๋ถ„ํฌ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๋‹จ์–ด๋“ค์„ ์ƒ์„ฑํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, LDA๋Š” ๋ฌธ์„œ๊ฐ€ ์ƒ์„ฑ๋˜๋˜ ๊ณผ์ •์„ ์—ญ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ  ๋งํฌ : https://lettier.com/projects/lda-topic-modeling/ ์œ„์˜ ์‚ฌ์ดํŠธ๋Š” ์ฝ”๋“œ ์ž‘์„ฑ ์—†์ด ์ž…๋ ฅํ•œ ๋ฌธ์„œ๋“ค๋กœ๋ถ€ํ„ฐ DTM์„ ๋งŒ๋“ค๊ณ  LDA๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์›น ์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค. 1. ์ž ์žฌ ๋””๋ฆฌํด๋ ˆ ํ• ๋‹น(Latent Dirichlet Allocation, LDA) ๊ฐœ์š” ์šฐ์„  LDA์˜ ๋‚ด๋ถ€ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜๊ธฐ ์ „์—, LDA๋ฅผ ์ผ์ข…์˜ ๋ธ”๋ž™๋ฐ•์Šค๋กœ ๋ณด๊ณ  LDA์— ๋ฌธ์„œ ์ง‘ํ•ฉ์„ ์ž…๋ ฅํ•˜๋ฉด, ์–ด๋–ค ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š”์ง€ ๊ฐ„์†Œํ™”๋œ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ 3๊ฐœ์˜ ๋ฌธ์„œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ง€๊ธˆ์˜ ์˜ˆ์ œ๋Š” ๊ฐ„๋‹จํ•ด์„œ ๋ˆˆ์œผ๋กœ๋„ ํ† ํ”ฝ ๋ชจ๋ธ๋ง์„ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์ง€๋งŒ, ์‹ค์ œ ์ˆ˜์‹ญ๋งŒ ๊ฐœ ์ด์ƒ์˜ ๋ฌธ์„œ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์ง์ ‘ ํ† ํ”ฝ์„ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์ด ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— LDA์˜ ๋„์›€์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ 1 : ์ €๋Š” ์‚ฌ๊ณผ๋ž‘ ๋ฐ”๋‚˜๋‚˜๋ฅผ ๋จน์–ด์š” ๋ฌธ์„œ 2 : ์šฐ๋ฆฌ๋Š” ๊ท€์—ฌ์šด ๊ฐ•์•„์ง€๊ฐ€ ์ข‹์•„์š” ๋ฌธ์„œ 3 : ์ €์˜ ๊นœ์ฐํ•˜๊ณ  ๊ท€์—ฌ์šด ๊ฐ•์•„์ง€๊ฐ€ ๋ฐ”๋‚˜๋‚˜๋ฅผ ๋จน์–ด์š” LDA๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ๋ฌธ์„œ ์ง‘ํ•ฉ์—์„œ ํ† ํ”ฝ์ด ๋ช‡ ๊ฐœ๊ฐ€ ์กด์žฌํ• ์ง€ ๊ฐ€์ •ํ•˜๋Š” ๊ฒƒ์€ ์‚ฌ์šฉ์ž๊ฐ€ ํ•ด์•ผ ํ•  ์ผ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” LDA์— 2๊ฐœ์˜ ํ† ํ”ฝ์„ ์ฐพ์œผ๋ผ๊ณ  ์š”์ฒญํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ† ํ”ฝ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ k๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, k๋ฅผ 2๋กœ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. k์˜ ๊ฐ’์„ ์ž˜๋ชป ์„ ํƒํ•˜๋ฉด ์›์น˜ ์•Š๋Š” ์ด์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์„ ํƒํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋จธ์‹  ๋Ÿฌ๋‹ ์šฉ์–ด๋กœ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์„ ํƒ์€ ์—ฌ๋Ÿฌ ์‹คํ—˜์„ ํ†ตํ•ด ์–ป์€ ๊ฐ’์ผ ์ˆ˜๋„ ์žˆ๊ณ , ์šฐ์„  ์‹œ๋„ํ•ด ๋ณด๋Š” ๊ฐ’์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. LDA๊ฐ€ ์œ„์˜ ์„ธ ๋ฌธ์„œ๋กœ๋ถ€ํ„ฐ 2๊ฐœ์˜ ํ† ํ”ฝ์„ ์ฐพ์€ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” LDA ์ž…๋ ฅ ์ „์— ์ฃผ์–ด์™€ ๋ถˆํ•„์š”ํ•œ ์กฐ์‚ฌ ๋“ฑ์„ ์ œ๊ฑฐํ•˜๋Š” ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์€ ๊ฑฐ์ณค๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์„ ๊ฑฐ์นœ DTM์ด LDA์˜ ์ž…๋ ฅ์ด ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. LDA๋Š” ๊ฐ ๋ฌธ์„œ์˜ ํ† ํ”ฝ ๋ถ„ํฌ์™€ ๊ฐ ํ† ํ”ฝ ๋‚ด์˜ ๋‹จ์–ด ๋ถ„ํฌ๋ฅผ ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค. <๊ฐ ๋ฌธ์„œ์˜ ํ† ํ”ฝ ๋ถ„ํฌ> ๋ฌธ์„œ 1 : ํ† ํ”ฝ A 100% ๋ฌธ์„œ 2 : ํ† ํ”ฝ B 100% ๋ฌธ์„œ 3 : ํ† ํ”ฝ B 60%, ํ† ํ”ฝ A 40% <๊ฐ ํ† ํ”ฝ์˜ ๋‹จ์–ด ๋ถ„ํฌ> ํ† ํ”ฝ A : ์‚ฌ๊ณผ 20%, ๋ฐ”๋‚˜๋‚˜ 40%, ๋จน์–ด์š” 40%, ๊ท€์—ฌ์šด 0%, ๊ฐ•์•„์ง€ 0%, ๊นœ์ฐํ•˜๊ณ  0%, ์ข‹์•„์š” 0% ํ† ํ”ฝ B : ์‚ฌ๊ณผ 0%, ๋ฐ”๋‚˜๋‚˜ 0%, ๋จน์–ด์š” 0%, ๊ท€์—ฌ์šด 33%, ๊ฐ•์•„์ง€ 33%, ๊นœ์ฐํ•˜๊ณ  16%, ์ข‹์•„์š” 16% LDA๋Š” ํ† ํ”ฝ์˜ ์ œ๋ชฉ์„ ์ •ํ•ด์ฃผ์ง€ ์•Š์ง€๋งŒ, ์ด ์‹œ์ ์—์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‚ฌ์šฉ์ž๋Š” ์œ„ ๊ฒฐ๊ณผ๋กœ๋ถ€ํ„ฐ ๋‘ ํ† ํ”ฝ์ด ๊ฐ๊ฐ ๊ณผ์ผ์— ๋Œ€ํ•œ ํ† ํ”ฝ๊ณผ ๊ฐ•์•„์ง€์— ๋Œ€ํ•œ ํ† ํ”ฝ์ด๋ผ๊ณ  ํŒ๋‹จํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ LDA์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค. ์‹ค์ œ๋กœ LDA๋Š” ์•„๋ž˜์˜ ์„ค๋ช… ๋ณด๋‹ค ํ›จ์”ฌ ๋” ๋ณต์žกํ•˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ์ˆ˜ํ•™์ ์ธ ์ˆ˜์‹์€ ๋ฐฐ์ œํ•˜๊ณ  ๊ฐœ๋…์  ์ดํ•ด์— ์ดˆ์ ์„ ๋‘ก๋‹ˆ๋‹ค. 2. LDA์˜ ๊ฐ€์ • LDA๋Š” ๋ฌธ์„œ์˜ ์ง‘ํ•ฉ์œผ๋กœ๋ถ€ํ„ฐ ์–ด๋–ค ํ† ํ”ฝ์ด ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ์•Œ์•„๋‚ด๊ธฐ ์œ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. LDA๋Š” ์•ž์„œ ๋ฐฐ์šด ๋นˆ๋„์ˆ˜ ๊ธฐ๋ฐ˜์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ธ BoW์˜ ํ–‰๋ ฌ DTM ๋˜๋Š” TF-IDF ํ–‰๋ ฌ์„ ์ž…๋ ฅ์œผ๋กœ ํ•˜๋Š”๋ฐ, ์ด๋กœ๋ถ€ํ„ฐ ์•Œ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์‹ค์€ LDA๋Š” ๋‹จ์–ด์˜ ์ˆœ์„œ๋Š” ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š๊ฒ ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. LDA๋Š” ๋ฌธ์„œ๋“ค๋กœ๋ถ€ํ„ฐ ํ† ํ”ฝ์„ ๋ฝ‘์•„๋‚ด๊ธฐ ์œ„ํ•ด์„œ ์ด๋Ÿฌํ•œ ๊ฐ€์ •์„ ์—ผ๋‘์— ๋‘๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฌธ์„œ ํ•˜๋‚˜, ํ•˜๋‚˜๊ฐ€ ์ž‘์„ฑ๋  ๋•Œ ๊ทธ ๋ฌธ์„œ์˜ ์ž‘์„ฑ์ž๋Š” ์ด๋Ÿฌํ•œ ์ƒ๊ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. '๋‚˜๋Š” ์ด ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด๋Ÿฐ ์ฃผ์ œ๋“ค์„ ๋„ฃ์„ ๊ฑฐ๊ณ , ์ด๋Ÿฐ ์ฃผ์ œ๋“ค์„ ์œ„ํ•ด์„œ๋Š” ์ด๋Ÿฐ ๋‹จ์–ด๋“ค์„ ๋„ฃ์„ ๊ฑฐ์•ผ.' ์กฐ๊ธˆ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ฌธ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์„ ๊ฑฐ์ณ์„œ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. 1) ๋ฌธ์„œ์— ์‚ฌ์šฉํ•  ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ N์„ ์ •ํ•ฉ๋‹ˆ๋‹ค. - Ex) 5๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 2) ๋ฌธ์„œ์— ์‚ฌ์šฉํ•  ํ† ํ”ฝ์˜ ํ˜ผํ•ฉ์„ ํ™•๋ฅ  ๋ถ„ํฌ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. - Ex) ์œ„ ์˜ˆ์ œ์™€ ๊ฐ™์ด ํ† ํ”ฝ์ด 2๊ฐœ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ๊ฐ•์•„์ง€ ํ† ํ”ฝ์„ 60%, ๊ณผ์ผ ํ† ํ”ฝ์„ 40%์™€ ๊ฐ™์ด ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3) ๋ฌธ์„œ์— ์‚ฌ์šฉํ•  ๊ฐ ๋‹จ์–ด๋ฅผ (์•„๋ž˜์™€ ๊ฐ™์ด) ์ •ํ•ฉ๋‹ˆ๋‹ค. 3-1) ํ† ํ”ฝ ๋ถ„ํฌ์—์„œ ํ† ํ”ฝ T๋ฅผ ํ™•๋ฅ ์ ์œผ๋กœ ๊ณ ๋ฆ…๋‹ˆ๋‹ค. - Ex) 60% ํ™•๋ฅ ๋กœ ๊ฐ•์•„์ง€ ํ† ํ”ฝ์„ ์„ ํƒํ•˜๊ณ , 40% ํ™•๋ฅ ๋กœ ๊ณผ์ผ ํ† ํ”ฝ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3-2) ์„ ํƒํ•œ ํ† ํ”ฝ T์—์„œ ๋‹จ์–ด์˜ ์ถœํ˜„ ํ™•๋ฅ  ๋ถ„ํฌ์— ๊ธฐ๋ฐ˜ํ•ด ๋ฌธ์„œ์— ์‚ฌ์šฉํ•  ๋‹จ์–ด๋ฅผ ๊ณ ๋ฆ…๋‹ˆ๋‹ค. - Ex) ๊ฐ•์•„์ง€ ํ† ํ”ฝ์„ ์„ ํƒํ•˜์˜€๋‹ค๋ฉด, 33% ํ™•๋ฅ ๋กœ ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ 3)์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๋ฌธ์„œ๋ฅผ ์™„์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณผ์ •์„ ํ†ตํ•ด ๋ฌธ์„œ๊ฐ€ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๋Š” ๊ฐ€์ • ํ•˜์— LDA๋Š” ํ† ํ”ฝ์„ ๋ฝ‘์•„๋‚ด๊ธฐ ์œ„ํ•˜์—ฌ ์œ„ ๊ณผ์ •์„ ์—ญ์œผ๋กœ ์ถ”์ ํ•˜๋Š” ์—ญ๊ณตํ•™(reverse engneering)์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 3. LDA์˜ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ด์ œ LDA์˜ ์ˆ˜ํ–‰ ๊ณผ์ •์„ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์‚ฌ์šฉ์ž๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์—๊ฒŒ ํ† ํ”ฝ์˜ ๊ฐœ์ˆ˜ k๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ์•ž์„œ ๋งํ•˜์˜€๋“ฏ์ด LDA์—๊ฒŒ ํ† ํ”ฝ์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์€ ์‚ฌ์šฉ์ž์˜ ์—ญํ• ์ž…๋‹ˆ๋‹ค. LDA๋Š” ํ† ํ”ฝ์˜ ๊ฐœ์ˆ˜ k๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด, k ๊ฐœ์˜ ํ† ํ”ฝ์ด M ๊ฐœ์˜ ์ „์ฒด ๋ฌธ์„œ์— ๊ฑธ์ณ ๋ถ„ํฌ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. 2) ๋ชจ๋“  ๋‹จ์–ด๋ฅผ k ๊ฐœ ์ค‘ ํ•˜๋‚˜์˜ ํ† ํ”ฝ์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ LDA๋Š” ๋ชจ๋“  ๋ฌธ์„œ์˜ ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ k ๊ฐœ ์ค‘ ํ•˜๋‚˜์˜ ํ† ํ”ฝ์„ ๋žœ๋ค์œผ๋กœ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์ด ๋๋‚˜๋ฉด ๊ฐ ๋ฌธ์„œ๋Š” ํ† ํ”ฝ์„ ๊ฐ€์ง€๋ฉฐ, ํ† ํ”ฝ์€ ๋‹จ์–ด ๋ถ„ํฌ๋ฅผ ๊ฐ€์ง€๋Š” ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋žœ๋ค์œผ๋กœ ํ• ๋‹นํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์‹ค ์ด ๊ฒฐ๊ณผ๋Š” ์ „๋ถ€ ํ‹€๋ฆฐ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•œ ๋‹จ์–ด๊ฐ€ ํ•œ ๋ฌธ์„œ์—์„œ 2ํšŒ ์ด์ƒ ๋“ฑ์žฅํ•˜์˜€๋‹ค๋ฉด, ๊ฐ ๋‹จ์–ด๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํ† ํ”ฝ์— ํ• ๋‹น๋˜์—ˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 3) ์ด์ œ ๋ชจ๋“  ๋ฌธ์„œ์˜ ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์˜ ์‚ฌํ•ญ์„ ๋ฐ˜๋ณต ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. (iterative) 3-1) ์–ด๋–ค ๋ฌธ์„œ์˜ ๊ฐ ๋‹จ์–ด w๋Š” ์ž์‹ ์€ ์ž˜๋ชป๋œ ํ† ํ”ฝ์— ํ• ๋‹น๋ผ ์žˆ์ง€๋งŒ, ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์€ ์ „๋ถ€ ์˜ฌ๋ฐ”๋ฅธ ํ† ํ”ฝ์— ํ• ๋‹น๋ผ ์žˆ๋Š” ์ƒํƒœ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ๋‹จ์–ด w๋Š” ์•„๋ž˜์˜ ๋‘ ๊ฐ€์ง€ ๊ธฐ์ค€์— ๋”ฐ๋ผ์„œ ํ† ํ”ฝ์ด ์žฌํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. - p(topic t | document d) : ๋ฌธ์„œ d์˜ ๋‹จ์–ด๋“ค ์ค‘ ํ† ํ”ฝ t์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋“ค์˜ ๋น„์œจ - p(word w | topic t) : ๊ฐ ํ† ํ”ฝ๋“ค t์—์„œ ํ•ด๋‹น ๋‹จ์–ด w์˜ ๋ถ„ํฌ ์ด๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด, ๋ชจ๋“  ํ• ๋‹น์ด ์™„๋ฃŒ๋œ ์ˆ˜๋ ด ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ๊ธฐ์ค€์ด ์–ด๋–ค ์˜๋ฏธ์ธ์ง€ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์„ค๋ช…์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด์„œ ๋‘ ๊ฐœ์˜ ๋ฌธ์„œ๋ผ๋Š” ์ƒˆ๋กœ์šด ์˜ˆ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๋‘ ๊ฐœ์˜ ๋ฌธ์„œ doc1๊ณผ doc2๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” doc1์˜ ์„ธ ๋ฒˆ์งธ ๋‹จ์–ด apple์˜ ํ† ํ”ฝ์„ ๊ฒฐ์ •ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ฒซ ๋ฒˆ์งธ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์ค€์€ ๋ฌธ์„œ doc1์˜ ๋‹จ์–ด๋“ค์ด ์–ด๋–ค ํ† ํ”ฝ์— ํ•ด๋‹นํ•˜๋Š”์ง€๋ฅผ ๋ด…๋‹ˆ๋‹ค. doc1์˜ ๋ชจ๋“  ๋‹จ์–ด๋“ค์€ ํ† ํ”ฝ A์™€ ํ† ํ”ฝ B์— 50 ๋Œ€ 50์˜ ๋น„์œจ๋กœ ํ• ๋‹น๋ผ ์žˆ์œผ๋ฏ€๋กœ, ์ด ๊ธฐ์ค€์— ๋”ฐ๋ฅด๋ฉด ๋‹จ์–ด apple์€ ํ† ํ”ฝ A ๋˜๋Š” ํ† ํ”ฝ B ๋‘˜ ์ค‘ ์–ด๋””์—๋„ ์†ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๊ธฐ์ค€์€ ๋‹จ์–ด apple์ด ์ „์ฒด ๋ฌธ์„œ์—์„œ ์–ด๋–ค ํ† ํ”ฝ์— ํ• ๋‹น๋ผ ์žˆ๋Š”์ง€๋ฅผ ๋ด…๋‹ˆ๋‹ค. ์ด ๊ธฐ์ค€์— ๋”ฐ๋ฅด๋ฉด ๋‹จ์–ด apple์€ ํ† ํ”ฝ B์— ํ• ๋‹น๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋‘ ๊ฐ€์ง€ ๊ธฐ์ค€์„ ์ฐธ๊ณ ํ•˜์—ฌ LDA๋Š” doc1์˜ apple์„ ์–ด๋–ค ํ† ํ”ฝ์— ํ• ๋‹นํ• ์ง€ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. 4. ์ž ์žฌ ๋””๋ฆฌํด๋ ˆ ํ• ๋‹น๊ณผ ์ž ์žฌ ์˜๋ฏธ ๋ถ„์„์˜ ์ฐจ์ด LSA : DTM์„ ์ฐจ์› ์ถ•์†Œํ•˜์—ฌ ์ถ•์†Œ ์ฐจ์›์—์„œ ๊ทผ์ ‘ ๋‹จ์–ด๋“ค์„ ํ† ํ”ฝ์œผ๋กœ ๋ฌถ๋Š”๋‹ค. LDA : ๋‹จ์–ด๊ฐ€ ํŠน์ • ํ† ํ”ฝ์— ์กด์žฌํ•  ํ™•๋ฅ ๊ณผ ๋ฌธ์„œ์— ํŠน์ • ํ† ํ”ฝ์ด ์กด์žฌํ•  ํ™•๋ฅ ์„ ๊ฒฐํ•ฉ ํ™•๋ฅ ๋กœ ์ถ”์ •ํ•˜์—ฌ ํ† ํ”ฝ์„ ์ถ”์ถœํ•œ๋‹ค. 5. ์‹ค์Šต์„ ํ†ตํ•œ ์ดํ•ด ์ด์ œ LDA๋ฅผ ์‹ค์Šต์„ ํ†ตํ•ด ์ง์ ‘ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. LSA ์‹ค์Šต์—์„œ๋Š” ์‚ฌ์ดํ‚ท๋Ÿฐ(sklearn)์„ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, ์ด๋ฒˆ์—๋Š” gensim์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์•ž ์‹ค์Šต๊ณผ ์‹ค์Šต ๊ณผ์ •์ด ํ™•์—ฐํžˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์„ ํ†ตํ•ด LDA๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ์‹ค์Šต์€ ์•„๋ž˜์˜ ๋งํฌ์— ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋งํฌ์—์„œ๋Š” LSA ์‹ค์Šต์˜ ์‹ค์Šต๊ณผ ์ง„ํ–‰ ๊ณผ์ •์ด ๊ฑฐ์˜ ์œ ์‚ฌํ•˜๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์œผ๋กœ LDA ์‹ค์Šต : https://wikidocs.net/40710 1) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ๋‹จ์–ด ์ง‘ํ•ฉ ๋งŒ๋“ค๊ธฐ ๋ฐ”๋กœ ์ด์ „ ์‹ค์Šต์ธ LSA ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•˜์˜€๋˜ Twenty Newsgroups์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” 20๊ฐœ์˜ ๋‹ค๋ฅธ ์ฃผ์ œ๋ฅผ ๊ฐ€์ง„ ๋‰ด์Šค ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์€ ์ด์ „ ์‹ค์Šต๊ณผ ์ค‘๋ณต๋˜๋ฏ€๋กœ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„์— tokenized_doc์œผ๋กœ ์ €์žฅํ•œ ์ƒํƒœ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ํ›ˆ๋ จ์šฉ ๋‰ด์Šค๋ฅผ 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. tokenized_doc[:5] 0 [well, sure, about, story, seem, biased, what,... 1 [yeah, expect, people, read, actually, accept,... 2 [although, realize, that, principle, your, str... 3 [notwithstanding, legitimate, fuss, about, thi... 4 [well, will, have, change, scoring, playoff, p... Name: clean_doc, dtype: object ์ด์ œ ๊ฐ ๋‹จ์–ด์— ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ํ•˜๋Š” ๋™์‹œ์—, ๊ฐ ๋‰ด์Šค์—์„œ์˜ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ๋กํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ ๋‹จ์–ด๋ฅผ (word_id, word_frequency)์˜ ํ˜•ํƒœ๋กœ ๋ฐ”๊พธ๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. word_id๋Š” ๋‹จ์–ด๊ฐ€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๋œ ๊ฐ’์ด๊ณ , word_frequency๋Š” ํ•ด๋‹น ๋‰ด์Šค์—์„œ์˜ ํ•ด๋‹น ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” gensim์˜ corpora.Dictionary()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ๋‰ด์Šค์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ๋‰ด์Šค๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. from gensim import corpora dictionary = corpora.Dictionary(tokenized_doc) corpus = [dictionary.doc2bow(text) for text in tokenized_doc] print(corpus[1]) # ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ์—์„œ ๋‘ ๋ฒˆ์งธ ๋‰ด์Šค ์ถœ๋ ฅ. ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ์˜ ์ธ๋ฑ์Šค๋Š” 0 [(52, 1), (55, 1), (56, 1), (57, 1), (58, 1), (59, 1), (60, 1), (61, 1), (62, 1), (63, 1), (64, 1), (65, 1), (66, 2), (67, 1), (68, 1), (69, 1), (70, 1), (71, 2), (72, 1), (73, 1), (74, 1), (75, 1), (76, 1), (77, 1), (78, 2), (79, 1), (80, 1), (81, 1), (82, 1), (83, 1), (84, 1), (85, 2), (86, 1), (87, 1), (88, 1), (89, 1)] ๋‘ ๋ฒˆ์งธ ๋‰ด์Šค์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ด…์‹œ๋‹ค. ์œ„์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ค‘์—์„œ (66, 2)๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด 66์œผ๋กœ ํ• ๋‹น๋œ ๋‹จ์–ด๊ฐ€ ๋‘ ๋ฒˆ์งธ ๋‰ด์Šค์—์„œ๋Š” ๋‘ ๋ฒˆ ๋“ฑ์žฅํ•˜์˜€์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 66์ด๋ผ๋Š” ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋‹จ์–ด๊ฐ€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋˜๊ธฐ ์ „์—๋Š” ์–ด๋–ค ๋‹จ์–ด์˜€๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๋ด…์‹œ๋‹ค. ์ด๋Š” dictionary[]์— ๊ธฐ์กด ๋‹จ์–ด๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ๊ณ ์ž ํ•˜๋Š” ์ •์ˆซ๊ฐ’์„ ์ž…๋ ฅํ•˜์—ฌ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(dictionary[66]) faith ๊ธฐ์กด์—๋Š” ๋‹จ์–ด 'faith'์ด์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•™์Šต๋œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” dictionary์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. len(dictionary) 65284 ์ด 65,284๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ํ•™์Šต๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ LDA ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2) LDA ๋ชจ๋ธ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ ๊ธฐ์กด์˜ ๋‰ด์Šค ๋ฐ์ดํ„ฐ๊ฐ€ ์ด 20๊ฐœ์˜ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์œผ๋ฏ€๋กœ ํ† ํ”ฝ์˜ ๊ฐœ์ˆ˜๋ฅผ 20์œผ๋กœ ํ•˜์—ฌ LDA ๋ชจ๋ธ์„ ํ•™์Šต์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. import gensim NUM_TOPICS = 20 # 20๊ฐœ์˜ ํ† ํ”ฝ, k=20 ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics = NUM_TOPICS, id2word=dictionary, passes=15) topics = ldamodel.print_topics(num_words=4) for topic in topics: print(topic) (0, '0.015*"drive" + 0.014*"thanks" + 0.012*"card" + 0.012*"system"') (1, '0.009*"back" + 0.009*"like" + 0.009*"time" + 0.008*"went"') (2, '0.012*"colorado" + 0.010*"david" + 0.006*"decenso" + 0.005*"tyre"') (3, '0.020*"number" + 0.018*"wire" + 0.013*"bits" + 0.013*"filename"') (4, '0.038*"space" + 0.013*"nasa" + 0.011*"research" + 0.010*"medical"') (5, '0.014*"price" + 0.010*"sale" + 0.009*"good" + 0.008*"shipping"') (6, '0.012*"available" + 0.009*"file" + 0.009*"information" + 0.008*"version"') (7, '0.021*"would" + 0.013*"think" + 0.012*"people" + 0.011*"like"') (8, '0.035*"window" + 0.021*"display" + 0.017*"widget" + 0.013*"application"') (9, '0.012*"people" + 0.010*"jesus" + 0.007*"armenian" + 0.007*"israel"') (10, '0.008*"government" + 0.007*"system" + 0.006*"public" + 0.006*"encryption"') (11, '0.013*"germany" + 0.008*"sweden" + 0.008*"switzerland" + 0.007*"gaza"') (12, '0.020*"game" + 0.018*"team" + 0.015*"games" + 0.013*"play"') (13, '0.024*"apple" + 0.014*"water" + 0.013*"ground" + 0.011*"cable"') (14, '0.011*"evidence" + 0.010*"believe" + 0.010*"truth" + 0.010*"church"') (15, '0.016*"president" + 0.010*"states" + 0.007*"united" + 0.007*"year"') (16, '0.047*"file" + 0.035*"output" + 0.033*"entry" + 0.021*"program"') (17, '0.008*"dept" + 0.008*"devils" + 0.007*"caps" + 0.007*"john"') (18, '0.011*"year" + 0.009*"last" + 0.007*"first" + 0.006*"runs"') (19, '0.013*"outlets" + 0.013*"norton" + 0.012*"quantum" + 0.008*"neck"') ๊ฐ ๋‹จ์–ด ์•ž์— ๋ถ™์€ ์ˆ˜์น˜๋Š” ๋‹จ์–ด์˜ ํ•ด๋‹น ํ† ํ”ฝ์— ๋Œ€ํ•œ ๊ธฐ์—ฌ๋„๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋˜ํ•œ ๋งจ ์•ž์— ์žˆ๋Š” ํ† ํ”ฝ ๋ฒˆํ˜ธ๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ ์ด 20๊ฐœ์˜ ํ† ํ”ฝ์€ 0๋ถ€ํ„ฐ 19๊นŒ์ง€์˜ ๋ฒˆํ˜ธ๊ฐ€ ํ• ๋‹น๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. passes๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋™์ž‘ ํšŸ์ˆ˜๋ฅผ ๋งํ•˜๋Š”๋ฐ, ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ฒฐ์ •ํ•˜๋Š” ํ† ํ”ฝ์˜ ๊ฐ’์ด ์ ์ ˆํžˆ ์ˆ˜๋ ดํ•  ์ˆ˜ ์žˆ๋„๋ก ์ถฉ๋ถ„ํžˆ ์ ๋‹นํ•œ ํšŸ์ˆ˜๋ฅผ ์ •ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ด 15ํšŒ๋ฅผ ์ˆ˜ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” num_words=4๋กœ ์ด 4๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ 10๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. print(ldamodel.print_topics()) 3) LDA ์‹œ๊ฐํ™”ํ•˜๊ธฐ LDA ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•ด์„œ๋Š” pyLDAvis์˜ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์œˆ๋„์˜ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋‚˜ MAC/UNIX์˜ ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜์˜ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜์—ฌ pyLDAvis๋ฅผ ์„ค์น˜ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. pip install pyLDAvis ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด LDA ์‹œ๊ฐํ™” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. import pyLDAvis.gensim_models pyLDAvis.enable_notebook() vis = pyLDAvis.gensim_models.prepare(ldamodel, corpus, dictionary) pyLDAvis.display(vis) ์ขŒ์ธก์˜ ์›๋“ค์€ ๊ฐ๊ฐ์˜ 20๊ฐœ์˜ ํ† ํ”ฝ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ฐ ์›๊ณผ์˜ ๊ฑฐ๋ฆฌ๋Š” ๊ฐ ํ† ํ”ฝ๋“ค์ด ์„œ๋กœ ์–ผ๋งˆ๋‚˜ ๋‹ค๋ฅธ์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‘ ๊ฐœ์˜ ์›์ด ๊ฒน์นœ๋‹ค๋ฉด, ์ด ๋‘ ๊ฐœ์˜ ํ† ํ”ฝ์€ ์œ ์‚ฌํ•œ ํ† ํ”ฝ์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” 10๋ฒˆ ํ† ํ”ฝ์„ ํด๋ฆญํ•˜์˜€๊ณ , ์ด์— ๋”ฐ๋ผ ์šฐ์ธก์—๋Š” 10๋ฒˆ ํ† ํ”ฝ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ํ•œ ๊ฐ€์ง€ ์ฃผ์˜ํ•  ์ ์€ LDA ๋ชจ๋ธ์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ๋Š” ํ† ํ”ฝ ๋ฒˆํ˜ธ๊ฐ€ 0๋ถ€ํ„ฐ ํ• ๋‹น๋˜์–ด 0~19์˜ ์ˆซ์ž๊ฐ€ ์‚ฌ์šฉ๋œ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์œ„์˜ LDA ์‹œ๊ฐํ™”์—์„œ๋Š” ํ† ํ”ฝ์˜ ๋ฒˆํ˜ธ๊ฐ€ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ ๊ฐ ํ† ํ”ฝ ๋ฒˆํ˜ธ๋Š” ์ด์ œ +1์ด ๋œ ๊ฐ’์ธ 1~20๊นŒ์ง€์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 4) ๋ฌธ์„œ ๋ณ„ ํ† ํ”ฝ ๋ถ„ํฌ ๋ณด๊ธฐ ์œ„์—์„œ ํ† ํ”ฝ ๋ณ„ ๋‹จ์–ด ๋ถ„ํฌ๋Š” ํ™•์ธํ•˜์˜€์œผ๋‚˜, ์•„์ง ๋ฌธ์„œ ๋ณ„ ํ† ํ”ฝ ๋ถ„ํฌ์— ๋Œ€ํ•ด์„œ๋Š” ํ™•์ธํ•˜์ง€ ๋ชปํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์šฐ์„  ๋ฌธ์„œ ๋ณ„ ํ† ํ”ฝ ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฌธ์„œ์˜ ํ† ํ”ฝ ๋ถ„ํฌ๋Š” ์ด๋ฏธ ํ›ˆ๋ จ๋œ LDA ๋ชจ๋ธ์ธ ldamodel[]์— ์ „์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋„ฃ์€ ํ›„์— ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฑ…์˜ ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ์ƒ์œ„ 5๊ฐœ์˜ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ๋งŒ ํ† ํ”ฝ ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. for i, topic_list in enumerate(ldamodel[corpus]): if i==5: break print(i,'๋ฒˆ์งธ ๋ฌธ์„œ์˜ topic ๋น„์œจ์€',topic_list) 0 ๋ฒˆ์งธ ๋ฌธ์„œ์˜ topic ๋น„์œจ์€ [(7, 0.3050222), (9, 0.5070568), (11, 0.1319604), (18, 0.042834017)] 1 ๋ฒˆ์งธ ๋ฌธ์„œ์˜ topic ๋น„์œจ์€ [(0, 0.031606797), (7, 0.7529218), (13, 0.02924682), (14, 0.12861845), (17, 0.037851967)] 2 ๋ฒˆ์งธ ๋ฌธ์„œ์˜ topic ๋น„์œจ์€ [(7, 0.52241164), (9, 0.36602455), (16, 0.09760969)] 3 ๋ฒˆ์งธ ๋ฌธ์„œ์˜ topic ๋น„์œจ์€ [(1, 0.16926806), (5, 0.04912094), (6, 0.04034211), (7, 0.11710636), (10, 0.5854137), (15, 0.02776434)] 4 ๋ฒˆ์งธ ๋ฌธ์„œ์˜ topic ๋น„์œจ์€ [(7, 0.42152268), (12, 0.21917087), (17, 0.32781804)] ์œ„์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ (์ˆซ์ž, ํ™•๋ฅ )์€ ๊ฐ๊ฐ ํ† ํ”ฝ ๋ฒˆํ˜ธ์™€ ํ•ด๋‹น ํ† ํ”ฝ์ด ํ•ด๋‹น ๋ฌธ์„œ์—์„œ ์ฐจ์ง€ํ•˜๋Š” ๋ถ„ํฌ๋„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 0๋ฒˆ์งธ ๋ฌธ์„œ์˜ ํ† ํ”ฝ ๋น„์œจ์—์„œ (7, 0.3050222)์€ 7๋ฒˆ ํ† ํ”ฝ์ด 30%์˜ ๋ถ„ํฌ๋„๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‘์šฉํ•˜์—ฌ ์ข€ ๋” ๊น”๋”ํ•œ ํ˜•ํƒœ์ธ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„<NAME>์œผ๋กœ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. def make_topictable_per_doc(ldamodel, corpus): topic_table = pd.DataFrame() # ๋ช‡ ๋ฒˆ์งธ ๋ฌธ์„œ์ธ์ง€๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ฌธ์„œ ๋ฒˆํ˜ธ์™€ ํ•ด๋‹น ๋ฌธ์„œ์˜ ํ† ํ”ฝ ๋น„์ค‘์„ ํ•œ ์ค„์”ฉ ๊บผ๋‚ด์˜จ๋‹ค. for i, topic_list in enumerate(ldamodel[corpus]): doc = topic_list[0] if ldamodel.per_word_topics else topic_list doc = sorted(doc, key=lambda x: (x[1]), reverse=True) # ๊ฐ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ๋น„์ค‘์ด ๋†’์€ ํ† ํ”ฝ์ˆœ์œผ๋กœ ํ† ํ”ฝ์„ ์ •๋ ฌํ•œ๋‹ค. # EX) ์ •๋ ฌ ์ „ 0๋ฒˆ ๋ฌธ์„œ : (2๋ฒˆ ํ† ํ”ฝ, 48.5%), (8๋ฒˆ ํ† ํ”ฝ, 25%), (10๋ฒˆ ํ† ํ”ฝ, 5%), (12๋ฒˆ ํ† ํ”ฝ, 21.5%), # Ex) ์ •๋ ฌ ํ›„ 0๋ฒˆ ๋ฌธ์„œ : (2๋ฒˆ ํ† ํ”ฝ, 48.5%), (8๋ฒˆ ํ† ํ”ฝ, 25%), (12๋ฒˆ ํ† ํ”ฝ, 21.5%), (10๋ฒˆ ํ† ํ”ฝ, 5%) # 48 > 25 > 21 > 5 ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด ๋œ ๊ฒƒ. # ๋ชจ๋“  ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ ์•„๋ž˜๋ฅผ ์ˆ˜ํ–‰ for j, (topic_num, prop_topic) in enumerate(doc): # ๋ช‡ ๋ฒˆ ํ† ํ”ฝ์ธ์ง€ ์™€ ๋น„์ค‘์„ ๋‚˜๋ˆ ์„œ ์ €์žฅํ•œ๋‹ค. if j == 0: # ์ •๋ ฌ์„ ํ•œ ์ƒํƒœ์ด๋ฏ€๋กœ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๋น„์ค‘์ด ๋†’์€ ํ† ํ”ฝ topic_table = topic_table.append(pd.Series([int(topic_num), round(prop_topic, 4), topic_list]), ignore_index=True) # ๊ฐ€์žฅ ๋น„์ค‘์ด ๋†’์€ ํ† ํ”ฝ๊ณผ, ๊ฐ€์žฅ ๋น„์ค‘์ด ๋†’์€ ํ† ํ”ฝ์˜ ๋น„์ค‘๊ณผ, ์ „์ฒด ํ† ํ”ฝ์˜ ๋น„์ค‘์„ ์ €์žฅํ•œ๋‹ค. else: break return(topic_table) topictable = make_topictable_per_doc(ldamodel, corpus) topictable = topictable.reset_index() # ๋ฌธ์„œ ๋ฒˆํ˜ธ์„ ์˜๋ฏธํ•˜๋Š” ์—ด(column)๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ธ๋ฑ์Šค ์—ด์„ ํ•˜๋‚˜ ๋” ๋งŒ๋“ ๋‹ค. topictable.columns = ['๋ฌธ์„œ ๋ฒˆํ˜ธ', '๊ฐ€์žฅ ๋น„์ค‘์ด ๋†’์€ ํ† ํ”ฝ', '๊ฐ€์žฅ ๋†’์€ ํ† ํ”ฝ์˜ ๋น„์ค‘', '๊ฐ ํ† ํ”ฝ์˜ ๋น„์ค‘'] topictable[:10] 19-03 ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ ์ž ์žฌ ๋””๋ฆฌํด๋ ˆ ํ• ๋‹น(LDA) ์‹ค์Šต ์•ž์„œ gensim์„ ํ†ตํ•ด์„œ LDA๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , ์‹œ๊ฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” LSA ์‹ค์Šต์—์„œ์ฒ˜๋Ÿผ ์‚ฌ์ดํ‚ท๋Ÿฐ์„ ์‚ฌ์šฉํ•˜์—ฌ LDA๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ „๋ฐ˜์ ์ธ ๊ณผ์ •์€ LSA ์‹ค์Šต๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. 1. ์‹ค์Šต์„ ํ†ตํ•œ ์ดํ•ด 1) ๋‰ด์Šค ๊ธฐ์‚ฌ ์ œ๋ชฉ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด ์•ฝ 15๋…„ ๋™์•ˆ ๋ฐœํ–‰๋˜์—ˆ๋˜ ๋‰ด์Šค ๊ธฐ์‚ฌ ์ œ๋ชฉ์„ ๋ชจ์•„๋†“์€ ์˜์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์•„๋ž˜ ๋งํฌ์—์„œ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://www.kaggle.com/therohk/million-headlines import pandas as pd import urllib.request import nltk from nltk.corpus import stopwords from nltk.stem import WordNetLemmatizer from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.decomposition import LatentDirichletAllocation urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/19.%20TopiC%20Modeling%20(LDA% 2C%20BERT-Based)/dataset/abcnews-date-text.csv") data = pd.read_csv('abcnews-date-text.csv', error_bad_lines=False) print('๋‰ด์Šค ์ œ๋ชฉ ๊ฐœ์ˆ˜ :',len(data)) ๋‰ด์Šค ์ œ๋ชฉ ๊ฐœ์ˆ˜ : 1082168 ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ์•ฝ 100๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(data.head(5)) publish_date headline_text 0 20030219 aba decides against community broadcasting lic... 1 20030219 act fire witnesses must be aware of defamation 2 20030219 a g calls for infrastructure protection summit 3 20030219 air nz staff in aust strike for pay rise 4 20030219 air nz strike to affect australian travellers ์ด ๋ฐ์ดํ„ฐ๋Š” publish_data์™€ headline_text๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์—ด์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ ๋‰ด์Šค๊ฐ€ ๋‚˜์˜จ ๋‚ ์งœ์™€ ๋‰ด์Šค ๊ธฐ์‚ฌ ์ œ๋ชฉ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋Š” ์ด ์ค‘์—์„œ headline_text ์—ด. ์ฆ‰, ๋‰ด์Šค ๊ธฐ์‚ฌ ์ œ๋ชฉ์ด๋ฏ€๋กœ ์ด ๋ถ€๋ถ„๋งŒ ๋ณ„๋„๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. text = data[['headline_text']] text.head(5) headline_text 0 aba decides against community broadcasting lic... 1 act fire witnesses must be aware of defamation 2 a g calls for infrastructure protection summit 3 air nz staff in aust strike for pay rise 4 air nz strike to affect australian travellers ์ •์ƒ์ ์œผ๋กœ headline_text ์—ด๋งŒ ์ถ”์ถœ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ, ํ‘œ์ œ์–ด ์ถ”์ถœ, ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด ์ œ๊ฑฐ๋ผ๋Š” ์„ธ ๊ฐ€์ง€ ์ „์ฒ˜๋ฆฌ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. text['headline_text'] = text.apply(lambda row: nltk.word_tokenize(row['headline_text']), axis=1) NLTK์˜ word_tokenize๋ฅผ ํ†ตํ•ด ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. print(text.head(5)) headline_text 0 [aba, decides, against, community, broadcastin... 1 [act, fire, witnesses, must, be, aware, of, de... 2 [a, g, calls, for, infrastructure, protection,... 3 [air, nz, staff, in, aust, strike, for, pay, r... 4 [air, nz, strike, to, affect, australian, trav... ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•˜์—ฌ ๋‹จ์–ด ํ† ํฐํ™” ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. stop_words = stopwords.words('english') text['headline_text'] = text['headline_text'].apply(lambda x: [word for word in x if word not in (stop_words)]) ์—ฌ๊ธฐ์„œ๋Š” NLTK๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์˜์–ด ๋ถˆ์šฉ์–ด๋ฅผ ํ†ตํ•ด์„œ text ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. print(text.head(5)) headline_text 0 [aba, decides, community, broadcasting, licence] 1 [act, fire, witnesses, must, aware, defamation] 2 [g, calls, infrastructure, protection, summit] 3 [air, nz, staff, aust, strike, pay, rise] 4 [air, nz, strike, affect, australian, travellers] ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์ „๊ณผ ํ›„์˜ ๋ฐ์ดํ„ฐ๋งŒ ๋น„๊ตํ•ด๋„ ํ™•์‹คํžˆ ๋ช‡ ๊ฐ€์ง€ ๋‹จ์–ด๋“ค์ด ์‚ฌ๋ผ์ง„ ๊ฒƒ์ด ๋ณด์ž…๋‹ˆ๋‹ค. against, be, of, a, in, to ๋“ฑ์˜ ๋‹จ์–ด๊ฐ€ ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ‘œ์ œ์–ด ์ถ”์ถœ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ‘œ์ œ์–ด ์ถ”์ถœ๋กœ 3์ธ์นญ ๋‹จ์ˆ˜ ํ‘œํ˜„์„ 1์ธ์นญ์œผ๋กœ ๋ฐ”๊พธ๊ณ , ๊ณผ๊ฑฐ ํ˜„์žฌํ˜• ๋™์‚ฌ๋ฅผ ํ˜„์žฌํ˜•์œผ๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. text['headline_text'] = text['headline_text'].apply(lambda x: [WordNetLemmatizer().lemmatize(word, pos='v') for word in x]) print(text.head(5)) headline_text 0 [aba, decide, community, broadcast, licence] 1 [act, fire, witness, must, aware, defamation] 2 [g, call, infrastructure, protection, summit] 3 [air, nz, staff, aust, strike, pay, rise] 4 [air, nz, strike, affect, australian, travellers] ํ‘œ์ œ์–ด ์ถ”์ถœ์ด ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ธธ์ด๊ฐ€ 3์ดํ•˜์ธ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ œ๊ฑฐํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. tokenized_doc = text['headline_text'].apply(lambda x: [word for word in x if len(word) > 3]) print(tokenized_doc[:5]) 0 [decide, community, broadcast, licence] 1 [fire, witness, must, aware, defamation] 2 [call, infrastructure, protection, summit] 3 [staff, aust, strike, rise] 4 [strike, affect, australian, travellers] ๊ธธ์ด๊ฐ€ 3์ดํ•˜์ธ ๋‹จ์–ด๋“ค์— ๋Œ€ํ•ด์„œ ์ œ๊ฑฐ๊ฐ€ ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ TF-IDF ํ–‰๋ ฌ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3) TF-IDF ํ–‰๋ ฌ ๋งŒ๋“ค๊ธฐ TF-IDF ์‹ค์Šต์—์„œ ๋ฐฐ์šด TfidfVectorizer๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ† ํฐ ํ™”๊ฐ€ ๋˜์–ด์žˆ์ง€ ์•Š์€ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์‹œ ํ† ํฐํ™” ์ž‘์—…์„ ์—ญ์œผ๋กœ ์ทจ์†Œํ•˜๋Š” ์—ญ ํ† ํฐ ํ™”(Detokenization) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์—ญ ํ† ํฐ ํ™” (ํ† ํฐํ™” ์ž‘์—…์„ ๋˜๋Œ๋ฆผ) detokenized_doc = [] for i in range(len(text)): t = ' '.join(tokenized_doc[i]) detokenized_doc.append(t) # ๋‹ค์‹œ text['headline_text']์— ์žฌ์ €์žฅ text['headline_text'] = detokenized_doc ์—ญ ํ† ํฐ ํ™”๊ฐ€ ๋˜์—ˆ๋Š”์ง€ text['headline_text']์˜ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. text['headline_text'][:5] 0 decide community broadcast licence 1 fire witness must aware defamation 2 call infrastructure protection summit 3 staff aust strike rise 4 strike affect australian travellers Name: headline_text, dtype: object ์ •์ƒ์ ์œผ๋กœ ์—ญ ํ† ํฐ ํ™”๊ฐ€ ์ˆ˜ํ–‰๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ TfidfVectorizer๋ฅผ TF-IDF ํ–‰๋ ฌ์„ ๋งŒ๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๊ฐ€์ง€๊ณ  ํ–‰๋ ฌ์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํžˆ 1,000๊ฐœ์˜ ๋‹จ์–ด๋กœ ์ œํ•œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ƒ์œ„ 1,000๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๋ณด์กด vectorizer = TfidfVectorizer(stop_words='english', max_features= 1000) X = vectorizer.fit_transform(text['headline_text']) # TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ ํ™•์ธ print('TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ :',X.shape) TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ : (1082168, 1000) 1,082,168 ร— 1,000์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ ๊ฐ€์ง„ TF-IDF ํ–‰๋ ฌ์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด์— LDA๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 4) ํ† ํ”ฝ ๋ชจ๋ธ๋ง lda_model = LatentDirichletAllocation(n_components=10, learning_method='online',random_state=777, max_iter=1) lda_top = lda_model.fit_transform(X) print(lda_model.components_) print(lda_model.components_.shape) [[1.00001533e-01 1.00001269e-01 1.00004179e-01 ... 1.00006124e-01 1.00003111e-01 1.00003064e-01] [1.00001199e-01 1.13513398e+03 3.50170830e+03 ... 1.00009349e-01 1.00001896e-01 1.00002937e-01] [1.00001811e-01 1.00001151e-01 1.00003566e-01 ... 1.00002693e-01 1.00002061e-01 7.53381835e+02] ... [1.00001065e-01 1.00001689e-01 1.00003278e-01 ... 1.00006721e-01 1.00004902e-01 1.00004759e-01] [1.00002401e-01 1.00000732e-01 1.00002989e-01 ... 1.00003517e-01 1.00001428e-01 1.00005266e-01] [1.00003427e-01 1.00002313e-01 1.00007340e-01 ... 1.00003732e-01 1.00001207e-01 1.00005153e-01]] (10, 1000) # ๋‹จ์–ด ์ง‘ํ•ฉ. 1,000๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์ €์žฅ๋จ. terms = vectorizer.get_feature_names() def get_topics(components, feature_names, n=5): for idx, topic in enumerate(components): print("Topic %d:" % (idx+1), [(feature_names[i], topic[i].round(2)) for i in topic.argsort()[:-n - 1:-1]]) get_topics(lda_model.components_,terms) Topic 1: [('government', 8725.19), ('sydney', 8393.29), ('queensland', 7720.12), ('change', 5874.27), ('home', 5674.38)] Topic 2: [('australia', 13691.08), ('australian', 11088.95), ('melbourne', 7528.43), ('world', 6707.7), ('south', 6677.03)] Topic 3: [('death', 5935.06), ('interview', 5924.98), ('kill', 5851.6), ('jail', 4632.85), ('life', 4275.27)] Topic 4: [('house', 6113.49), ('2016', 5488.19), ('state', 4923.41), ('brisbane', 4857.21), ('tasmania', 4610.97)] Topic 5: [('court', 7542.74), ('attack', 6959.64), ('open', 5663.0), ('face', 5193.63), ('warn', 5115.01)] Topic 6: [('market', 5545.86), ('rural', 5502.89), ('plan', 4828.71), ('indigenous', 4223.4), ('power', 3968.26)] Topic 7: [('charge', 8428.8), ('election', 7561.63), ('adelaide', 6758.36), ('make', 5658.99), ('test', 5062.69)] Topic 8: [('police', 12092.44), ('crash', 5281.14), ('drug', 4290.87), ('beat', 3257.58), ('rise', 2934.92)] Topic 9: [('fund', 4693.03), ('labor', 4047.69), ('national', 4038.68), ('council', 4006.62), ('claim', 3604.75)] Topic 10: [('trump', 11966.41), ('perth', 6456.53), ('report', 5611.33), ('school', 5465.06), ('woman', 5456.76)] 19-04 BERT๋ฅผ ์ด์šฉํ•œ ํ‚ค์›Œ๋“œ ์ถ”์ถœ : ํ‚ค ๋ฒ„ํŠธ(KeyBERT) ํ‚ค ๋ฒ„ํŠธ ์‹ค์Šต์„ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  SBERT๋ฅผ ์œ„ํ•œ ํŒจํ‚ค์ง€์ธ sentence_transformers๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. !pip install sentence_transformers 1. ๊ธฐ๋ณธ KeyBERT import numpy as np import itertools from sklearn.feature_extraction.text import CountVectorizer from sklearn.metrics.pairwise import cosine_similarity from sentence_transformers import SentenceTransformer ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์ง€๋„ ํ•™์Šต์— ๋Œ€ํ•œ ์˜์–ด ๋ฌธ์„œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์ด๋ฏธ ์นœ์ˆ™ํ•œ ์ฃผ์ œ์— ๋Œ€ํ•œ ๋ฌธ์„œ์ด๋ฏ€๋กœ ํ‚ค์›Œ๋“œ ์ถ”์ถœ์ด ์ž˜ ๋˜๊ณ  ์žˆ๋Š”์ง€ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์ง๊ด€์ ์œผ๋กœ ํŒ๋‹จํ•˜๊ธฐ์— ์ข‹์€ ์˜ˆ์‹œ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. doc = """ Supervised learning is the machine learning task of learning a function that maps an input to an output based on example input-output pairs.[1] It infers a function from labeled training data consisting of a set of training examples.[2] In supervised learning, each example is a pair consisting of an input object (typically a vector) and a desired output value (also called the supervisory signal). A supervised learning algorithm analyzes the training data and produces an inferred function, which can be used for mapping new examples. An optimal scenario will allow for the algorithm to correctly determine the class labels for unseen instances. This requires the learning algorithm to generalize from the training data to unseen situations in a 'reasonable' way (see inductive bias). """ ์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์–ด๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” n_gram_range์˜ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ n-gram์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, (3, 3)๋กœ ์„ค์ •ํ•˜๋ฉด ๊ฒฐ๊ณผ ํ›„๋ณด๋Š” 3๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ํ•œ ๋ฌถ์Œ์œผ๋กœ ๊ฐ„์ฃผํ•˜๋Š” trigram์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. # 3๊ฐœ์˜ ๋‹จ์–ด ๋ฌถ์Œ์ธ ๋‹จ ์–ด๊ตฌ ์ถ”์ถœ n_gram_range = (3, 3) stop_words = "english" count = CountVectorizer(ngram_range=n_gram_range, stop_words=stop_words).fit([doc]) candidates = count.get_feature_names_out() print('trigram ๊ฐœ์ˆ˜ :',len(candidates)) print('trigram ๋‹ค์„ฏ ๊ฐœ๋งŒ ์ถœ๋ ฅ :',candidates[:5]) trigram ๊ฐœ์ˆ˜ : 72 trigram ๋‹ค์„ฏ ๊ฐœ๋งŒ ์ถœ๋ ฅ : ['algorithm analyzes training' 'algorithm correctly determine' 'algorithm generalize training' 'allow algorithm correctly' 'analyzes training data'] ๋‹ค์Œ์œผ๋กœ ์ด์ œ ๋ฌธ์„œ์™€ ๋ฌธ์„œ๋กœ๋ถ€ํ„ฐ ์ถ”์ถœํ•œ ํ‚ค์›Œ๋“œ๋“ค์„ SBERT๋ฅผ ํ†ตํ•ด์„œ ์ˆ˜์น˜ํ™”ํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. model = SentenceTransformer('distilbert-base-nli-mean-tokens') doc_embedding = model.encode([doc]) candidate_embeddings = model.encode(candidates) ์ด์ œ ๋ฌธ์„œ์™€ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ํ‚ค์›Œ๋“œ๋“ค์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์„œ์™€ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ํ‚ค์›Œ๋“œ๋“ค์€ ๋ฌธ์„œ๋ฅผ ๋Œ€ํ‘œํ•˜๊ธฐ ์œ„ํ•œ ์ข‹์€ ํ‚ค์›Œ๋“œ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. top_n = 5 distances = cosine_similarity(doc_embedding, candidate_embeddings) keywords = [candidates[index] for index in distances.argsort()[0][-top_n:]] print(keywords) ['algorithm analyzes training', 'learning algorithm generalize', 'learning machine learning', 'learning algorithm analyzes', 'algorithm generalize training'] 5๊ฐœ์˜ ํ‚ค์›Œ๋“œ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š”๋ฐ, ์ด๋“ค์˜ ์˜๋ฏธ๊ฐ€ ์ข€ ๋น„์Šทํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. ๋น„์Šทํ•œ ์˜๋ฏธ์˜ ํ‚ค์›Œ๋“œ๋“ค์ด ๋ฆฌํ„ด๋˜๋Š” ๋ฐ๋Š” ์ด์œ ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์—ฐํžˆ ์ด ํ‚ค์›Œ๋“œ๋“ค์ด ๋ฌธ์„œ๋ฅผ ๊ฐ€์žฅ ์ž˜ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ง€๊ธˆ ์ถœ๋ ฅํ•œ ๊ฒƒ๋ณด๋‹ค๋Š” ์ข€ ๋” ๋‹ค์–‘ํ•œ ์˜๋ฏธ์˜ ํ‚ค์›Œ๋“œ๋“ค์ด ์ถœ๋ ฅ๋œ๋‹ค๋ฉด ์ด๋“ค์„ ๊ทธ๋ฃน์œผ๋กœ ๋ณธ๋‹ค๋Š” ๊ด€์ ์—์„œ๋Š” ํ•ด๋‹น ํ‚ค์›Œ๋“œ๋“ค์ด ๋ฌธ์„œ๋ฅผ ์ž˜ ๋‚˜ํƒ€๋‚ผ ๊ฐ€๋Šฅ์„ฑ์ด ์ ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ‚ค์›Œ๋“œ๋“ค์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ํ‚ค์›Œ๋“œ ์„ ์ •์˜ ์ •ํ™•์„ฑ๊ณผ ํ‚ค์›Œ๋“œ๋“ค์˜ ๋‹ค์–‘์„ฑ ์‚ฌ์ด์˜ ๋ฏธ๋ฌ˜ํ•œ ๊ท ํ˜•์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ๋“ค์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ ๋‘ ๊ฐ€์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Max Sum Similarity Maximal Marginal Relevance 2. Max Sum Similarity ๋ฐ์ดํ„ฐ ์Œ ์‚ฌ์ด์˜ ์ตœ๋Œ€ ํ•ฉ ๊ฑฐ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ ์Œ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์ตœ๋Œ€ํ™”๋˜๋Š” ๋ฐ์ดํ„ฐ ์Œ์œผ๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ์˜ ์˜๋„๋Š” ํ›„๋ณด ๊ฐ„์˜ ์œ ์‚ฌ์„ฑ์„ ์ตœ์†Œํ™”ํ•˜๋ฉด์„œ ๋ฌธ์„œ์™€์˜ ํ›„๋ณด ์œ ์‚ฌ์„ฑ์„ ๊ทน๋Œ€ํ™”ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. def max_sum_sim(doc_embedding, candidate_embeddings, words, top_n, nr_candidates): # ๋ฌธ์„œ์™€ ๊ฐ ํ‚ค์›Œ๋“œ๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„ distances = cosine_similarity(doc_embedding, candidate_embeddings) # ๊ฐ ํ‚ค์›Œ๋“œ๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„ distances_candidates = cosine_similarity(candidate_embeddings, candidate_embeddings) # ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ํ‚ค์›Œ๋“œ๋“ค ์ค‘ ์ƒ์œ„ top_n ๊ฐœ์˜ ๋‹จ์–ด๋ฅผ pick. words_idx = list(distances.argsort()[0][-nr_candidates:]) words_vals = [candidates[index] for index in words_idx] distances_candidates = distances_candidates[np.ix_(words_idx, words_idx)] # ๊ฐ ํ‚ค์›Œ๋“œ๋“ค ์ค‘์—์„œ ๊ฐ€์žฅ ๋œ ์œ ์‚ฌํ•œ ํ‚ค์›Œ๋“œ๋“ค ๊ฐ„์˜ ์กฐํ•ฉ์„ ๊ณ„์‚ฐ min_sim = np.inf candidate = None for combination in itertools.combinations(range(len(words_idx)), top_n): sim = sum([distances_candidates[i][j] for i in combination for j in combination if i != j]) if sim < min_sim: candidate = combination min_sim = sim return [words_vals[idx] for idx in candidate] ์ด๋ฅผ ์œ„ํ•ด ์ƒ์œ„ 10๊ฐœ์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์„ ํƒํ•˜๊ณ  ์ด 10๊ฐœ ์ค‘์—์„œ ์„œ๋กœ ๊ฐ€์žฅ ์œ ์‚ฌ์„ฑ์ด ๋‚ฎ์€ 5๊ฐœ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋‚ฎ์€ nr_candidates๋ฅผ ์„ค์ •ํ•˜๋ฉด ๊ฒฐ๊ณผ๋Š” ์ถœ๋ ฅ๋œ ํ‚ค์›Œ๋“œ 5๊ฐœ๋Š” ๊ธฐ์กด์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋งŒ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. max_sum_sim(doc_embedding, candidate_embeddings, candidates, top_n=5, nr_candidates=10) ['requires learning algorithm', 'signal supervised learning', 'learning function maps', 'algorithm analyzes training', 'learning machine learning'] ๊ทธ๋Ÿฌ๋‚˜ ์ƒ๋Œ€์ ์œผ๋กœ ๋†’์€ nr_candidates๋Š” ๋” ๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ 5๊ฐœ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. max_sum_sim(doc_embedding, candidate_embeddings, candidates, top_n=5, nr_candidates=20) ['set training examples', 'generalize training data', 'requires learning algorithm', 'supervised learning algorithm', 'learning machine learning'] 3. Maximal Marginal Relevance ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์–‘ํ™”ํ•˜๋Š” ๋งˆ์ง€๋ง‰ ๋ฐฉ๋ฒ•์€ MMR(Maximal Marginal Relevance)์ž…๋‹ˆ๋‹ค. MMR์€ ํ…์ŠคํŠธ ์š”์•ฝ ์ž‘์—…์—์„œ ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•˜๊ณ  ๊ฒฐ๊ณผ์˜ ๋‹ค์–‘์„ฑ์„ ๊ทน๋Œ€ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๋กœ EmbedRank(https://arxiv.org/pdf/1801.04470.pdf)๋ผ๋Š” ํ‚ค์›Œ๋“œ ์ถ”์ถœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ‚ค์›Œ๋“œ/ํ‚ค ํ”„๋ ˆ์ด์ฆˆ๋ฅผ ๋‹ค์–‘ํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” MMR์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๋ฌธ์„œ์™€ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ํ‚ค์›Œ๋“œ/ํ‚ค ํ”„๋ ˆ์ด์ฆˆ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ฌธ์„œ์™€ ์œ ์‚ฌํ•˜๊ณ  ์ด๋ฏธ ์„ ํƒ๋œ ํ‚ค์›Œ๋“œ/ํ‚ค ํ”„๋ ˆ์ด์ฆˆ์™€ ์œ ์‚ฌํ•˜์ง€ ์•Š์€ ์ƒˆ๋กœ์šด ํ›„๋ณด๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. def mmr(doc_embedding, candidate_embeddings, words, top_n, diversity): # ๋ฌธ์„œ์™€ ๊ฐ ํ‚ค์›Œ๋“œ๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„๊ฐ€ ์ ํ˜€์žˆ๋Š” ๋ฆฌ์ŠคํŠธ word_doc_similarity = cosine_similarity(candidate_embeddings, doc_embedding) # ๊ฐ ํ‚ค์›Œ๋“œ๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„ word_similarity = cosine_similarity(candidate_embeddings) # ๋ฌธ์„œ์™€ ๊ฐ€์žฅ ๋†’์€ ์œ ์‚ฌ๋„๋ฅผ ๊ฐ€์ง„ ํ‚ค์›Œ๋“œ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ถ”์ถœ. # ๋งŒ์•ฝ, 2๋ฒˆ ๋ฌธ์„œ๊ฐ€ ๊ฐ€์žฅ ์œ ์‚ฌ๋„๊ฐ€ ๋†’์•˜๋‹ค๋ฉด # keywords_idx = [2] keywords_idx = [np.argmax(word_doc_similarity)] # ๊ฐ€์žฅ ๋†’์€ ์œ ์‚ฌ๋„๋ฅผ ๊ฐ€์ง„ ํ‚ค์›Œ๋“œ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ œ์™ธํ•œ ๋ฌธ์„œ์˜ ์ธ๋ฑ์Šค๋“ค # ๋งŒ์•ฝ, 2๋ฒˆ ๋ฌธ์„œ๊ฐ€ ๊ฐ€์žฅ ์œ ์‚ฌ๋„๊ฐ€ ๋†’์•˜๋‹ค๋ฉด # ==> candidates_idx = [0, 1, 3, 4, 5, 6, 7, 8, 9, 10 ... ์ค‘๋žต ...] candidates_idx = [i for i in range(len(words)) if i != keywords_idx[0]] # ์ตœ๊ณ ์˜ ํ‚ค์›Œ๋“œ๋Š” ์ด๋ฏธ ์ถ”์ถœํ–ˆ์œผ๋ฏ€๋กœ top_n-1 ๋ฒˆ๋งŒํผ ์•„๋ž˜๋ฅผ ๋ฐ˜๋ณต. # ex) top_n = 5๋ผ๋ฉด, ์•„๋ž˜์˜ loop๋Š” 4๋ฒˆ ๋ฐ˜๋ณต๋จ. for _ in range(top_n - 1): candidate_similarities = word_doc_similarity[candidates_idx, :] target_similarities = np.max(word_similarity[candidates_idx][:, keywords_idx], axis=1) # MMR์„ ๊ณ„์‚ฐ mmr = (1-diversity) * candidate_similarities - diversity * target_similarities.reshape(-1, 1) mmr_idx = candidates_idx[np.argmax(mmr)] # keywords & candidates๋ฅผ ์—…๋ฐ์ดํŠธ keywords_idx.append(mmr_idx) candidates_idx.remove(mmr_idx) return [words[idx] for idx in keywords_idx] ๋งŒ์•ฝ ์ƒ๋Œ€์ ์œผ๋กœ ๋‚ฎ์€ diversity ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค๋ฉด, ๊ฒฐ๊ณผ๋Š” ๊ธฐ์กด์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋งŒ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. mmr(doc_embedding, candidate_embeddings, candidates, top_n=5, diversity=0.2) ['algorithm generalize training', 'supervised learning algorithm', 'learning machine learning', 'learning algorithm analyzes', 'learning algorithm generalize'] ๊ทธ๋Ÿฌ๋‚˜ ์ƒ๋Œ€์ ์œผ๋กœ ๋†’์€ diversity ๊ฐ’์€ ๋‹ค์–‘ํ•œ ํ‚ค์›Œ๋“œ 5๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด๋ƒ…๋‹ˆ๋‹ค. mmr(doc_embedding, candidate_embeddings, candidates, top_n=5, diversity=0.7) ['algorithm generalize training', 'labels unseen instances', 'new examples optimal', 'determine class labels', 'supervised learning algorithm'] 19-05 ํ•œ๊ตญ์–ด ํ‚ค ๋ฒ„ํŠธ(Korean KeyBERT)๋ฅผ ์ด์šฉํ•œ ํ‚ค์›Œ๋“œ ์ถ”์ถœ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 19-06 BERT ๊ธฐ๋ฐ˜ ๋ณตํ•ฉ ํ† ํ”ฝ ๋ชจ๋ธ(Combined Topic Models, CTM) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ „ํ†ต์ ์ธ ๋นˆ๋„์ˆ˜ ๊ธฐ๋ฐ˜ ๋ฌธ์„œ ๋ฒกํ„ฐํ™” ๋ฐฉ์‹์ธ Bag of Words์™€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ์˜ ๋ฌธ์„œ ์ž„๋ฒ ๋”ฉ ๋ฐฉ์‹์ธ SBERT๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ณตํ•ฉ ํ† ํ”ฝ ๋ชจ๋ธ(Combined Topic Models, CTM)์— ๋Œ€ํ•ด์„œ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. ์‹ค์Šต์„ ์œ„ํ•ด ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ํ† ํ”ฝ ๋ชจ๋ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ contextualized-topic-models์™€ LDA ์‹œ๊ฐํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ pyldavis๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install contextualized-topic-models==2.2.0 pip install pyldavis 1. ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ํ† ํ”ฝ ๋ชจ๋ธ(Contextualized Topic Models) ์‹œ์ž‘์— ์•ž์„œ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ํ† ํ”ฝ ๋ชจ๋ธ(Contextualized Topic Models)์˜ ๊ฐœ๋…์„ ์†Œ๊ฐœํ•ด ๋ด…์‹œ๋‹ค. ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ํ† ํ”ฝ ๋ชจ๋ธ(Contextualized Topic Models)์€ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ BERT์˜ ๋ฌธ์„œ ์ž„๋ฒ ๋”ฉ์˜ ํ‘œํ˜„๋ ฅ๊ณผ ๊ธฐ์กด ํ† ํ”ฝ ๋ชจ๋ธ์˜ ๋น„์ง€๋„ ํ•™์Šต ๋Šฅ๋ ฅ์„ ๊ฒฐํ•ฉํ•˜์—ฌ ๋ฌธ์„œ์—์„œ ์ฃผ์ œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ† ํ”ฝ ๋ชจ๋ธ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์„œ ์†Œ๊ฐœํ•  ๋ณตํ•ฉ ํ† ํ”ฝ ๋ชจ๋ธ(Combined Topic Models, CTM)์€ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ํ† ํ”ฝ ๋ชจ๋ธ์˜ ์ผ์ข…์ž…๋‹ˆ๋‹ค. 2. ๋ฐ์ดํ„ฐ ๋กœ๋“œํ•˜๊ธฐ ํ•™์Šต์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋กœ ํ•˜๋‚˜์˜ ๋ผ์ธ(line)์— ํ•˜๋‚˜์˜ ๋ฌธ์„œ๋กœ ๊ตฌ์„ฑ๋œ ํŒŒ์ผ์ด ํ•„์š”ํ•œ๋ฐ์š”. ์šฐ์„ , ์—ฌ๋Ÿฌ๋ถ„๋“ค์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ์ค€๋น„ํ•œ ์˜๋ฌธ ์œ„ํ‚คํ”ผ๋””์•„ ํŒŒ์ผ๋กœ ์‹ค์Šต์„ ํ•ด๋ด…์‹œ๋‹ค. # ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ !wget https://raw.githubusercontent.com/vinid/data/master/dbpedia_sample_abstract_20k_unprep.txt ์ƒ์œ„ 3๊ฐœ์˜ ๋ผ์ธ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. !head -n 3 dbpedia_sample_abstract_20k_unprep.txt The Mid-Peninsula Highway is a proposed freeway across the Niagara Peninsula in the Canadian province of Ontario. Although plans for a highway connecting Hamilton to Fort Erie south of the Niagara Escarpment have surfaced for decades, it was not until The Niagara Frontier International Gateway Study was published by the Ministry Monte Zucker (died March 15, 2007) was an American photographer. He specialized in wedding photography, entering it as a profession in 1947. In the 1970s he operated a studio in Silver Spring, Maryland. Later he lived in Florida. He was Brides Magazine's Wedding Photographer of the Year for 1990 and Henry Howard, 13th Earl of Suffolk, 6th Earl of Berkshire (8 August 1779 โ€“ 10 August 1779) was a British peer, the son of Henry Howard, 12th Earl of Suffolk. His father died on 7 March 1779, leaving behind his pregnant widow. The Earldom of Suffolk became dormant until she ์‹ค์ œ ํŒŒ์ผ์—๋Š” ์ค„๋ฐ”๊ฟˆ์ด ์—†์œผ๋‚˜ ์œ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—์„œ๋Š” ์ž„์˜๋กœ ๋ณด๊ธฐ ํŽธํ•˜๋„๋ก ๋ผ์ธ ๋ณ„๋กœ ์ค„๋ฐ”๊ฟˆ์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ๋ช…์„ ๋ณ€์ˆ˜์— ์ €์žฅํ•ด๋‘ก์‹œ๋‹ค. text_file = "dbpedia_sample_abstract_20k_unprep.txt" 3. ์ „์ฒ˜๋ฆฌ from contextualized_topic_models.models.ctm import CombinedTM from contextualized_topic_models.utils.data_preparation import TopicModelDataPreparation from contextualized_topic_models.utils.preprocessing import WhiteSpacePreprocessing import nltk Bag of Words ๊ธฐ๋ฐ˜์˜ ์ ‘๊ทผ์—์„œ๋Š” ์ „์ฒ˜๋ฆฌ ์ฑ•ํ„ฐ์—์„œ ํ•™์Šตํ–ˆ๋˜ ์ •๊ทœํ™”(Normalization) ๊ณผ์ •์ด ๊ต‰์žฅํžˆ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํŠน์ˆ˜ ๋ฌธ์ž๊ฐ€ ๋ถ™์–ด์„œ ๋™์ผํ•œ ๋‹จ์–ด๊ฐ€ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ์ธ์‹๋˜์ง€ ์•Š๋„๋ก ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ด ์ฃผ๊ณ , ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๋‹จ์–ด๋“ค์„ ์ œ๊ฑฐํ•œ ํ›„์— ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ๋‹จ์–ด๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” NLTK์—์„œ ์ œ๊ณตํ•˜๋Š” ์˜์–ด ๋ถˆ์šฉ์–ด๋“ค์„ ์ œ๊ฑฐํ•˜๊ณ , ๋‹จ์–ด์™€ ๋ถ™์–ด์žˆ๋Š” ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๋ฉฐ, ์˜๋‹จ์–ด์˜ ์†Œ๋ฌธ์žํ™”๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. nltk.download('stopwords') documents = [line.strip() for line in open(text_file, encoding="utf-8").readlines()] sp = WhiteSpacePreprocessing(documents, stopwords_language='english') preprocessed_documents, unpreprocessed_corpus, vocab = sp.preprocess() ์ „์ฒ˜๋ฆฌ ์ „, ํ›„์˜ ๋ฌธ์„œ๋ฅผ ์ƒ์œ„ 2๊ฐœ๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. # ์ „์ฒ˜๋ฆฌ ์ „ ๋ฌธ์„œ unpreprocessed_corpus[:2] ['The Mid-Peninsula Highway is a proposed freeway across the Niagara Peninsula in the Canadian province of Ontario. Although plans for a highway connecting Hamilton to Fort Erie south of the Niagara Escarpment have surfaced for decades, it was not until The Niagara Frontier International Gateway Study was published by the Ministry', "Monte Zucker (died March 15, 2007) was an American photographer. He specialized in wedding photography, entering it as a profession in 1947. In the 1970s he operated a studio in Silver Spring, Maryland. Later he lived in Florida. He was Brides Magazine's Wedding Photographer of the Year for 1990 and"] # normalization ์ „์ฒ˜๋ฆฌ ํ›„ ๋ฌธ์„œ preprocessed_documents[:2] ['mid peninsula highway proposed across peninsula canadian province ontario although highway connecting hamilton fort south international study published ministry', 'died march american photographer specialized photography operated studio silver spring maryland later lived florida magazine photographer year'] ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ print('bag of words์— ์‚ฌ์šฉ๋  ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :',len(vocab)) bag of words์— ์‚ฌ์šฉ๋  ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 2000 ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 2,000์ž…๋‹ˆ๋‹ค. WhiteSpacePreprocessing()์˜ vocabulary_size ์ธ์ž์˜ ๊ธฐ๋ณธ๊ฐ’์ด 2000์ด ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ ๋˜์ง€ ์•Š์€ ๋ฌธ์„œ๋Š” ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ SBERT์˜ ๋ฌธ์„œ ์ž„๋ฒ ๋”ฉ์„ ์–ป๊ธฐ ์œ„ํ•œ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ œ๊ฑฐํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ ์ „ ๋ฌธ์„œ์™€ ์ „์ฒ˜๋ฆฌ ํ›„ ๋ฌธ์„œ๋ฅผ TopicModelDataPreparation ๊ฐ์ฒด์— ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด๋Š” bag of words์™€ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ๋ฌธ์„œ์˜ BERT ์ž„๋ฒ ๋”ฉ์„ ์–ป์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  pretrained BERT๋Š” paraphrase-distilroberta-base-v1์ž…๋‹ˆ๋‹ค. tp = TopicModelDataPreparation("paraphrase-distilroberta-base-v1") training_dataset = tp.fit(text_for_contextual=unpreprocessed_corpus, text_for_bow=preprocessed_documents) ์ด์ œ tp.vocab์„ ํ•˜๋ฉด ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Š” ์•ž์—์„œ vocab์— ์ €์žฅ๋œ ๋‹จ์–ด ์ง‘ํ•ฉ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. len(tp.vocab) 2000 4. Combined TM ํ•™์Šตํ•˜๊ธฐ ์ด์ œ ํ† ํ”ฝ ๋ชจ๋ธ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์— ํ•ด๋‹นํ•˜๋Š” ํ† ํ”ฝ์˜ ๊ฐœ์ˆ˜(n_components)๋กœ๋Š” 50๊ฐœ๋ฅผ ์„ ์ •ํ•ฉ๋‹ˆ๋‹ค. ctm = CombinedTM(bow_size=len(tp.vocab), contextual_size=768, n_components=50, num_epochs=20) ctm.fit(training_dataset) Epoch: [20/20] Seen Samples: [400000/400000] Train Loss: 135.494220703125 Time: 0:00:05.428048: : 20it [01:49, 5.47s/it] 5. ๊ฒฐ๊ณผ ์ถœ๋ ฅ ํ•™์Šต ํ›„์—๋Š” ํ† ํ”ฝ ๋ชจ๋ธ์ด ์„ ์ •ํ•œ ํ† ํ”ฝ๋“ค์„ ๋ณด๋ ค๋ฉด get_topic_lists๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฉ”์„œ๋“œ์—๋Š” ๊ฐ ํ† ํ”ฝ๋งˆ๋‹ค ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๋ณด๊ณ  ์‹ถ์€์ง€์— ํ•ด๋‹นํ•˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„ฃ์–ด ์ฆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 5๊ฐœ๋ฅผ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ† ํ”ฝ๋“ค์€ ์œ„ํ‚คํ”ผ๋””์•„(์ผ๋ฐ˜์ ์ธ ์ฃผ์ œ)์œผ๋กœ๋ถ€ํ„ฐ ์–ป์€ ํ† ํ”ฝ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์˜์–ด ๋ฌธ์„œ๋กœ ํ•™์Šตํ•˜์˜€์œผ๋ฏ€๋กœ ๊ฐ ํ† ํ”ฝ์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋“ค๋„ ์˜์–ด ๋‹จ์–ด๋“ค์ž…๋‹ˆ๋‹ค. ctm.get_topic_lists(5) [['mi', 'kilometres', 'village', 'north', 'municipality'], ['national', 'house', 'built', 'historic', 'county'], ['used', 'defined', 'mathematics', 'number', 'typically'], ['film', 'directed', 'best', 'films', 'produced'], ['united', 'states', 'company', 'air', 'international'], ['species', 'family', 'found', 'native', 'genus'], ['composer', 'january', 'painter', 'studied', 'son'], ['station', 'line', 'located', 'railway', 'street'], ['church', 'roman', 'century', 'ancient', 'catholic'], ['county', 'school', 'high', 'state', 'located'], ['published', 'book', 'work', 'books', 'writer'], ['held', 'season', 'tournament', 'cup', 'championship'], ['made', 'born', 'english', 'first', 'played'], ['built', 'house', 'story', 'building', 'style'], ['war', 'world', 'series', 'television', 'first'], ['government', 'political', 'party', 'council', 'act'], ['american', 'new', 'york', 'known', 'born'], ['school', 'high', 'college', 'located', 'secondary'], ['published', 'written', 'book', 'fictional', 'novel'], ['area', 'river', 'located', 'park', 'lake'], ['born', 'summer', 'world', 'olympics', 'competed'], ['de', 'french', 'son', 'king', 'daughter'], ['university', 'served', 'american', 'law', 'born'], ['film', 'directed', 'written', 'drama', 'produced'], ['university', 'research', 'professor', 'india', 'institute'], ['war', 'class', 'royal', 'navy', 'army'], ['music', 'band', 'rock', 'singer', 'best'], ['born', 'played', 'former', 'professional', 'american'], ['album', 'band', 'released', 'rock', 'music'], ['used', 'use', 'system', 'software', 'model'], ['radio', 'company', 'station', 'broadcasting', 'owned'], ['played', 'born', 'football', 'former', 'league'], ['world', 'held', 'women', 'championships', 'place'], ['family', 'found', 'species', 'plant', 'mm'], ['member', 'party', 'politician', 'election', 'parliament'], ['island', 'point', 'antarctic', 'land', 'expedition'], ['states', 'united', 'county', 'list', 'national'], ['district', 'also', 'population', 'iran', 'persian'], ['enzyme', 'gene', 'belongs', 'chemical', 'humans'], ['region', 'area', 'province', 'municipality', 'part'], ['team', 'season', 'football', 'division', 'university'], ['released', 'album', 'band', 'studio', 'recorded'], ['city', 'town', 'england', 'located', 'miles'], ['mi', 'west', 'south', 'km', 'village'], ['mi', 'west', 'south', 'east', 'km'], ['league', 'club', 'football', 'team', 'season'], ['series', 'game', 'television', 'show', 'video'], ['line', 'station', 'railway', 'chinese', 'near'], ['company', 'based', 'founded', 'business', 'research'], ['politician', 'member', 'john', 'served', 'became']] 6. ์‹œ๊ฐํ™” ํ† ํ”ฝ๋“ค์„ ์‹œ๊ฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” PyLDAvis๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. lda_vis_data = ctm.get_ldavis_data_format(tp.vocab, training_dataset, n_samples=10) Sampling: [10/10]: : 10it [00:52, 5.25s/it] import pyLDAvis as vis lda_vis_data = ctm.get_ldavis_data_format(tp.vocab, training_dataset, n_samples=10) ctm_pd = vis.prepare(**lda_vis_data) vis.display(ctm_pd) 7. ์˜ˆ์ธก import numpy as np ์ž„์˜์˜ ๋ฌธ์„œ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์–ด๋–ค ํ† ํ”ฝ์ด ํ• ๋‹น๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฐ˜๋„(peninsula)์— ๋Œ€ํ•œ ์ฃผ์ œ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ์ฒซ ๋ฒˆ์งธ ์ „์ฒ˜๋ฆฌ ๋œ ๋ฌธ์„œ์˜ ํ† ํ”ฝ์„ ์˜ˆ์ธกํ•ด ๋ด…์‹œ๋‹ค. topics_predictions = ctm.get_thetas(training_dataset, n_samples=5) # get all the topic predictions Sampling: [5/5]: : 5it [00:23, 4.74s/it] # ์ „์ฒ˜๋ฆฌ ๋ฌธ์„œ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ print(preprocessed_documents[0]) mid peninsula highway proposed across peninsula canadian province ontario although highway connecting hamilton fort south international study published ministry ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ์— ๋Œ€ํ•œ ํ† ํ”ฝ์„ ์ถ”์ถœํ•ด ๋ด…์‹œ๋‹ค. ctm.get_topic_lists(5)[topic_number] ['located', 'river', 'north', 'state', 'km'] 8. ๋ชจ๋ธ ์ €์žฅ ๋ฐ ๋กœ๋“œํ•˜๊ธฐ ํ˜„์žฌ ๊ฒฝ๋กœ์— ๋ชจ๋ธ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ctm.save(models_dir="./") ํ˜„์žฌ ctm์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ ๋ชจ๋ธ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. del ctm ์ €์žฅํ•œ ๋ชจ๋ธ์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ctm = CombinedTM(bow_size=len(tp.vocab), contextual_size=768, num_epochs=100, n_components=50) ctm.load("/content/contextualized_topic_model_nc_50_tpm_0.0_tpv_0.98_hs_prodLDA_ac_(100, 100)_do_softplus_lr_0.2_mo_0.002_rp_0.99", epoch=19) ctm.get_topic_lists(5) [['politician', 'member', 'born', 'party', 'served'], ['mi', 'county', 'km', 'south', 'east'], ['church', 'century', 'roman', 'greek', 'latin'], ['team', 'season', 'home', 'football', 'games'], ['album', 'released', 'band', 'first', 'music'], ['school', 'university', 'college', 'education', 'public'], ['university', 'professor', 'author', 'research', 'work'], ['party', 'member', 'election', 'council', 'elections'], ['located', 'river', 'north', 'state', 'km'], ['mi', 'village', 'approximately', 'km', 'south'], ['mi', 'west', 'approximately', 'east', 'kilometres'], ['school', 'high', 'county', 'district', 'located'], ['built', 'house', 'story', 'style', 'building'], ['station', 'radio', 'licensed', 'fm', 'owned'], ['war', 'air', 'royal', 'navy', 'army'], ['american', 'born', 'former', 'football', 'college'], ['state', 'city', 'area', 'county', 'located'], ['station', 'railway', 'city', 'line', 'airport'], ['published', 'game', 'book', 'first', 'released'], ['family', 'genus', 'brown', 'species', 'found'], ['season', 'league', 'club', 'football', 'team'], ['american', 'team', 'season', 'football', 'played'], ['war', 'world', 'ii', 'cross', 'summer'], ['son', 'de', 'king', 'french', 'daughter'], ['series', 'produced', 'television', 'film', 'directed'], ['united', 'states', 'county', 'national', 'park'], ['railway', 'bridge', 'river', 'island', 'station'], ['written', 'film', 'novel', 'directed', 'published'], ['born', 'played', 'made', 'english', 'first'], ['film', 'directed', 'produced', 'best', 'stars'], ['born', 'world', 'competed', 'silver', 'summer'], ['island', 'mountain', 'islands', 'range', 'land'], ['held', 'world', 'championship', 'tournament', 'cup'], ['system', 'data', 'systems', 'computer', 'software'], ['organization', 'established', 'education', 'research', 'international'], ['either', 'used', 'term', 'space', 'using'], ['album', 'released', 'band', 'rock', 'music'], ['family', 'species', 'found', 'mm', 'native'], ['government', 'established', 'political', 'responsible', 'act'], ['company', 'based', 'founded', 'group', 'business'], ['american', 'known', 'born', 'best', 'york'], ['village', 'town', 'england', 'parish', 'civil'], ['states', 'united', 'state', 'served', 'member'], ['also', 'district', 'population', 'persian', 'iran'], ['painter', 'studied', 'artist', 'work', 'composer'], ['television', 'radio', 'show', 'series', 'music'], ['played', 'professional', 'born', 'league', 'player'], ['often', 'chemical', 'different', 'means', 'associated'], ['game', 'series', 'car', 'held', 'racing'], ['region', 'municipality', 'area', 'province', 'located']] 19-07 BERT ๊ธฐ๋ฐ˜ ํ•œ๊ตญ์–ด ๋ณตํ•ฉ ํ† ํ”ฝ ๋ชจ๋ธ(Korean CTM) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ „ํ†ต์ ์ธ ๋นˆ๋„์ˆ˜ ๊ธฐ๋ฐ˜ ๋ฌธ์„œ ๋ฒกํ„ฐํ™” ๋ฐฉ์‹์ธ Bag of Words์™€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ์˜ ๋ฌธ์„œ ์ž„๋ฒ ๋”ฉ ๋ฐฉ์‹์ธ SBERT๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ณตํ•ฉ ํ† ํ”ฝ ๋ชจ๋ธ(Combined Topic Models, CTM)์— ๋Œ€ํ•ด์„œ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. ์‹ค์Šต์„ ์œ„ํ•ด ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ํ† ํ”ฝ ๋ชจ๋ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ contextualized-topic-models์™€ LDA ์‹œ๊ฐํ™” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ pyldavis, ๊ทธ๋ฆฌ๊ณ  ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install contextualized-topic-models==2.2.0 pip install pyldavis # Colab์— Mecab ์„ค์น˜ !git clone https://github.com/SOMJANG/Mecab-ko-for-Google-Colab.git %cd Mecab-ko-for-Google-Colab !bash install_mecab-ko_on_colab190912.sh 1. ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ํ† ํ”ฝ ๋ชจ๋ธ(Contextualized Topic Models) ์‹œ์ž‘์— ์•ž์„œ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ํ† ํ”ฝ ๋ชจ๋ธ(Contextualized Topic Models)์˜ ๊ฐœ๋…์„ ์†Œ๊ฐœํ•ด ๋ด…์‹œ๋‹ค. ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ํ† ํ”ฝ ๋ชจ๋ธ(Contextualized Topic Models)์€ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ BERT์˜ ๋ฌธ์„œ ์ž„๋ฒ ๋”ฉ์˜ ํ‘œํ˜„๋ ฅ๊ณผ ๊ธฐ์กด ํ† ํ”ฝ ๋ชจ๋ธ์˜ ๋น„์ง€๋„ ํ•™์Šต ๋Šฅ๋ ฅ์„ ๊ฒฐํ•ฉํ•˜์—ฌ ๋ฌธ์„œ์—์„œ ์ฃผ์ œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ† ํ”ฝ ๋ชจ๋ธ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์„œ ์†Œ๊ฐœํ•  ๋ณตํ•ฉ ํ† ํ”ฝ ๋ชจ๋ธ(Combined Topic Models, CTM)์€ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ํ† ํ”ฝ ๋ชจ๋ธ์˜ ์ผ์ข…์ž…๋‹ˆ๋‹ค. 2. ํ•œ๊ตญ์–ด์— ์ ์šฉํ•˜๊ธฐ ๋ณตํ•ฉ ํ† ํ”ฝ ๋ชจ๋ธ์„ ํ•œ๊ตญ์–ด์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ถ”๊ฐ€์ ์ธ ์ฝ”๋“œ ์ˆ˜์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  CTM์€ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋‚˜, CountVectorizer๋Š” ๋‹จ์ˆœํžˆ ๋„์–ด์“ฐ๊ธฐ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ํ•œ๊ตญ์–ด์—๋Š” ์ ์ ˆํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด์— ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์„ ์‚ฌ์šฉํ•˜์˜€์œผ๋ฉฐ, SBERT๋„ ํ•œ๊ตญ์–ด์— ๋Œ€ํ•œ ๋ฌธ์„œ ์ž„๋ฒ ๋”ฉ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ SBERT์˜ ์ž„๋ฒ ๋”ฉ ๋˜ํ•œ ํ•œ๊ตญ์–ด๋ฅผ ํฌํ•จํ•œ ๋‹ค๊ตญ์–ด BERT๋กœ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ƒ์„ธ ์ฝ”๋“œ๋Š” github์— ๊ณต๊ฐœํ•˜์˜€์œผ๋ฉฐ ์—ฌ๊ธฐ์„œ๋Š” ๊ฒฐ๊ณผ๋งŒ<NAME>๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณ„๋„์˜ ๋ถˆ์šฉ์–ด ์ฒ˜๋ฆฌ๋Š” ํ•ด์ฃผ์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์ข€ ๋” ์„ฌ์„ธํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด ๋ถˆ์šฉ์–ด๋„ ์ถ”๊ฐ€ํ•ด ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊นƒํ—ˆ๋ธŒ ๋งํฌ : https://github.com/ukairia777/bert-topic-models ctm.get_topics(5) defaultdict(list, {0: ['๋œ๋‹ค๋Š”', '์ธ์›', '์ถฉ๋ถ„', '๊ณ ํ†ต', '๋ถ„๋ช…'], 1: ['์ œ์ž‘', '๋ฐœํ‘œํšŒ', '์Šคํ€˜์–ด', '์••๊ตฌ์ •', '์กฑ๋ณด'], 2: ['๋Œ€ํ†ต๋ น', '์œ ์šฉ', '๋ถˆ๋ฒ•ํ–‰์œ„', '๊ฐค๋Ÿฌ๋ฆฌ', '์žฌ๋‹จ'], 3: ['๊ณต์‹œ', 'ํˆฌ๋ฐ์ด', '๋จธ๋‹ˆ', '๋ฆฌ์–ผํƒ€์ž„', '์ทจ๋“'], 4: ['์•„์ดํ…œ', '์ปฌ๋Ÿฌ', '์žฌํ‚ท', '์†Œ์žฌ', '๋‹ค์šด'], 5: ['์ˆ˜์ˆ˜๋ฃŒ', 'ํŽธ์˜์ ', '์ธ์ถœ', 'ํ˜„๊ธˆ', '์„œ๋น„์Šค'], 6: ['๋„์–ด', '๊ธฐ๊ด€์‚ฌ', '์ถœ์ž…๋ฌธ', '์Šคํฌ๋ฆฐ', '์ „๋™์ฐจ'], 7: ['์ œ์ž‘', '๋‚จ์ž', '์‚ฌ๋ž‘', '์••๊ตฌ์ •', '๊น€์˜๊ด‘'], 8: ['๋งˆํฌ๊ตฌ', '์ผ€์ด์Šค', '์ทจํ•˜', '์—”ํ„ฐ', '๊ฐ•๋‚จ๊ตฌ'], 9: ['ํ™˜์ž', '์ˆ˜์ˆ ', '๊ถŒ์—ญ', '์‘๊ธ‰', '์™ธ์ƒ'], 10: ['ํŒจ์…˜', '๋””์ž์ด๋„ˆ', '๋””์ž์ธ', '2017', '์œ„ํฌ'], 11: ['์—์„œ', '์–ด์š”', '์ง€๋งŒ', '์†Œ์„ค', '์‚ฌ๋žŒ'], 12: ['์บ”๋””', '๊ทธ๋žจ', '์ „ํ˜„๋ฌด', '์žฅ๊ทผ์„', '๊ณ ์„ฑํฌ'], 13: ['๋ฐฉ์†ก', '๊ฐ€์ด๋“œ๋ผ์ธ', '์ง€์ƒํŒŒ', '์‚ฌ์—…์ž', '์œ ๋ฃŒ'], 14: ['์ž์œ ', '๋œ๋‹ค๋Š”', 'ํฌ๊ธฐ', '๊ณ ์–‘', 'ํ‰์ฐฝ'], 15: ['๊ธฐ๊ถŒ', '์žฅ๊ด€', 'ํšŒ๊ณ ๋ก', '๋‚จ๋ถ', '์•ˆ๋ณด'], 16: ['๋‰ด์Šค', '์—ฌ์˜๋„', '์ฝ”๋ฆฌ์•„', 'ํšŒ๊ฒฌ', '์„ ์–ธ'], 17: ['๋„ค์ด๋ฒ„', '์ด์‚ฌํšŒ', '์˜์žฅ', 'ํ•œ์„ฑ์ˆ™', '์„ ์ž„'], 18: ['๋””์ž์ด๋„ˆ', 'ํŒจ์…˜', '๋””์ž์ธ', '์‹ ์ง„', '2017'], 19: ['๋Š” ๋‹ค๋Š”', '๋”ฐ๋กœ', '2008', '๋œ๋‹ค๋Š”', 'ํ†ตํ•ด์„œ'], 20: ['์Šต๋‹ˆ๋‹ค', '๋ฒ”์ธ', '๊ฒฝ์ฐฐ', '๊ฒฝ์ฐฐ๊ด€', '์‚ฌ์ œ'], 21: ['์„œ์šธ', '๋ฒ•์›', 'ํŒ๊ฒฐ', '๊ฐ€์ •', '์ง€๋ฒ•'], 22: ['๊ฐ€๊ณ„', '๋ถ€์ฑ„', '๊ฒฝ์ œ', '๊ฑด์ „', '์œผ๋กœ'], 23: ['์œผ๋กœ', '์—์„œ', 'ํˆฌ์ž', '์ผ์ž', '์„ผ์„œ์Šค'], 24: ['๋Œ€๋ณธ', '์—ฐ๊ธฐ', '๋„๊นจ๋น„', '๋“œ๋ผ๋งˆ', '๋ชจ์Šต'], 25: ['์Šต๋‹ˆ๋‹ค', '์œผ๋กœ', '๋•Œ๋ฌธ', '๊ณต์ œ', '๋Š”๋ฐ์š”'], 26: ['๊ตญํšŒ', '๋ณ‘์šฐ', '๊ตญ๊ฐ', '์šด์˜', '์ˆ˜์„'], 27: ['๊ธฐ์˜จ', '์•„์นจ', '๋™ํ•ด', '๊ตฌ๋ฆ„', '๊ธฐ์ƒ์ฒญ'], 28: ['์ค‘๊ตฌ', 'ํฌํ† ', '๋™์•„๋‹ท์ปด', '์„์ง€๋กœ', '์—ด๋ฆฐ'], 29: ['๋‰ด์‹œ์Šค', '์˜์ƒ', '๊ณต๊ฐ', '์–ธ๋ก ', '์ œ๋ณด'], 30: ['ํ…Œ์ŠคํŠธ', '์„ฑ๋Šฅ', '20', 'ํ…Œ์Šฌ๋ผ', '์†Œํ”„ํŠธ์›จ์–ด'], 31: ['84', '์ง€์ƒ', '๊ฐ€๊ตฌ', '๋ฉด์ ', '๋ธ”๋ก'], 32: ['๊ณต๊ฐœ', '๋ฐ•ํ•ด์ง„', 'ํ™”๋ณด', '๋ฉ”์ดํฌ์—…', '์ดฌ์˜'], 33: ['์œผ๋กœ', '์—์„œ', '๊ธฐ์—…', 'ํ•œ๋‹ค', '๊ฒฝ์ œ'], 34: ['์ง€๊ฒ€', '์†Œํ™˜', '๋ฌธ์ฒด', '๊ธฐ์†Œ', '๊ฒ€์ฐฐ'], 35: ['๊ทธ๋ฃน', '์ด์Šค', '๋ฐ๋ท”', '์Œ์›', '์ฐจํŠธ'], 36: ['๋ฐฐ์šฐ', '์˜ํ™”', '์ฐจํƒœํ˜„', '์‚ฌ๋ž‘', '์ œ์ž‘'], 37: ['์—๊ฒŒ', '์ปคํ”Œ', '์‚ฌ๋žŒ', '๋‚˜๋ฆฌ', '์ •์›'], 38: ['๋Œ€ํ‘œ', '๊ฐœํ—Œ', 'ํƒˆ๋‹น', '๊ณตํ™”๊ตญ', '์ง€๋Œ€'], 39: ['๋ถ€ํ„ฐ', 'ํ–‰์‚ฌ', '์ด๋ฒคํŠธ', '์—ฌํ–‰', '์ฒดํ—˜'], 40: ['๋ชจ์—ฌ', '์„ธ์›”', '2008', '๋ฐ”ํ€ด', '์ƒ์ธ'], 41: ['์›๋Œ€', '๋งค๋ฌผ', '์ฃผ์ฒด', '๊ฑฐ๋ž˜๋Ÿ‰', '์œผ๋กœ'], 42: ['๊ต์œก', '์ฒญ์†Œ๋…„', '๋งˆ๋‹น', '์ง„๋กœ', '๋Œ€ํšŒ'], 43: ['๋‰ด์‹œ์Šค', '์˜์ƒ', '๊ณต๊ฐ', '์–ธ๋ก ', '์‚ฌ์ง„'], 44: ['ํŽธ์ž…', '๊ฒฝ๋ ฅ', '๊ธฐ์—…', '์ฑ„์šฉ', 'ํ•œ์ „'], 45: ['ํŠธ๋Ÿผํ”„', 'ํ† ๋ก ', '๋Œ€์„ ', '๊ณตํ™”', 'ํ›„๋ณด'], 46: ['์˜์ƒ', '๋‰ด์‹œ์Šค', '์–ธ๋ก ', '๊ณต๊ฐ', '์ œ๋ณด'], 47: ['ํ™”์„ฑ', '๋ฏธ๊ตญ', '์ฐฉ๋ฅ™', '์œ„ํ˜‘', '๋ฐฉ์–ด'], 48: ['์›์œ ', '์œ ๊ฐ€', '๋‰ด์š•', '์˜ค๋ฅธ', '์—ฐ๋ฐฉ'], 49: ['์ง€์—ญ', '์‚ฌ์—…', '์ง€์›', '๋ณต์ง€', '์‹œํฅ']}) ์œ„์—์„œ ์ถœ๋ ฅํ•œ ํ† ํ”ฝ ๋ฒˆํ˜ธ๋Š” pyLDAvis์—์„œ ํ• ๋‹นํ•œ ํ† ํ”ฝ ๋ฒˆํ˜ธ์™€ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ฃผ์˜ํ•ฉ์‹œ๋‹ค. ๊ฐ€๋ น, 48๋ฒˆ ํ† ํ”ฝ์ด์—ˆ๋˜ ['์›์œ ', '์œ ๊ฐ€', '๋‰ด์š•', '์˜ค๋ฅธ', '์—ฐ๋ฐฉ']๊ฐ€ ์•„๋ž˜์˜ PyLDAvis์—์„œ๋Š” 24๋ฒˆ ํ† ํ”ฝ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 19-08 ๋ฒ„ํ† ํ”ฝ(BERTopic) SBERT๋ฅผ ์ด์šฉํ•œ ํ† ํ”ฝ ๋ชจ๋ธ์ธ BERTopic์€ ๋ณ„๋„ ๋…ผ๋ฌธ์€ ๋‚˜์˜ค์ง€ ์•Š์€ ๋ชจ๋ธ์ด์ง€๋งŒ, github์—์„œ 2k ์ด์ƒ์˜ ์Šคํƒ€๋ฅผ ๋ฐ›์•˜์„ ๋งŒํผ ๊ต‰์žฅํžˆ ์ฃผ๋ชฉ๋ฐ›๊ณ  ์žˆ๋Š” ํ† ํ”ฝ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž๋Š” BERTopic์ด LDA๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์„ ๋งŒํผ์˜ ๊ธฐ์ˆ ์ด๋ผ๊ณ  ํ™•์‹ ์„ ์–ป์—ˆ๋‹ค๊ณ  ์ฃผ์žฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” BERT ๊ธฐ๋ฐ˜์˜ ํ† ํ”ฝ ๋ชจ๋ธ๋ง ๊ตฌํ˜„์ฒด์ธ BERTopic์˜ ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. !pip install bertopic[visualization] 1. BERTopic BERTopic์€ BERT embeddings๊ณผ ํด๋ž˜์Šค ๊ธฐ๋ฐ˜(class-based) TF-IDF๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ฃผ์ œ ์„ค๋ช…์—์„œ ์ค‘์š”ํ•œ ๋‹จ์–ด๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ๋„ ์‰ฝ๊ฒŒ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๋ฐ€ํ•œ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ํ† ํ”ฝ ๋ชจ๋ธ๋ง ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. BERTopic ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํฌ๊ฒŒ ์„ธ ๊ฐ€์ง€ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. 1) ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ SBERT๋กœ ์ž„๋ฒ ๋”ฉํ•ฉ๋‹ˆ๋‹ค. SBERT๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ์ž„๋ฒ ๋”ฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, BERTopic์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์•„๋ž˜์˜ BERT๋“ค์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. "paraphrase-MiniLM-L6-v2" : ์˜์–ด ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šต๋œ SBERT "paraphrase-multilingual-MiniLM-L12-v2" : 50๊ฐœ ์ด์ƒ์˜ ์–ธ์–ด๋กœ ํ•™์Šต๋œ ๋‹ค๊ตญ์–ด SBERT 2) ๋ฌธ์„œ๋ฅผ ๊ตฐ์ง‘ํ™”ํ•ฉ๋‹ˆ๋‹ค. UMAP์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž„๋ฒ ๋”ฉ์˜ ์ฐจ์›์„ ์ค„์ด๊ณ  HDBSCAN ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฐจ์› ์ถ•์†Œ๋œ ์ž„๋ฒ ๋”ฉ์„ ํด๋Ÿฌ์Šคํ„ฐ๋งํ•˜๊ณ  ์˜๋ฏธ์ ์œผ๋กœ ์œ ์‚ฌํ•œ ๋ฌธ์„œ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 3) ํ† ํ”ฝ ํ‘œํ˜„์„ ์ƒ์„ฑ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ TF-IDF๋กœ ํ† ํ”ฝ์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. 2. ๋ฐ์ดํ„ฐ ๋กœ๋“œ from bertopic import BERTopic from sklearn.datasets import fetch_20newsgroups ์‚ฌ์ดํ‚ท๋Ÿฐ์—์„œ ์ œ๊ณตํ•˜๋Š” ์œ ๋ช… ๋ฐ์ดํ„ฐ ์…‹์ธ 20๋‰ด์Šค ๊ทธ๋ฃน ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๊ณ  ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. docs = fetch_20newsgroups(subset='all', remove=('headers', 'footers', 'quotes'))['data'] docs[:5] ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ์—ฌ๊ธฐ์„œ๋Š” ๋ณ„๋„๋กœ ๋ณด์—ฌ๋“œ๋ฆฌ์ง„ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ docs๋Š” ๋ฌธ์ž์—ด์˜ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print('์ด ๋ฌธ์„œ์˜ ์ˆ˜ :', len(docs)) ์ด ๋ฌธ์„œ์˜ ์ˆ˜ : 18846 ์ด ๋ฌธ์„œ์˜ ์ˆ˜๋Š” 18,846๊ฐœ์ž…๋‹ˆ๋‹ค. 3. ํ† ํ”ฝ ๋ชจ๋ธ๋ง BERTopic์˜ ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , fit_transform ๋ฉ”์„œ๋“œ์— ๋ฌธ์ž์—ด๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์œผ๋ฉด ํ† ํ”ฝ ๋ชจ๋ธ๋ง์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. model = BERTopic() topics, probabilities = model.fit_transform(docs) print('๊ฐ ๋ฌธ์„œ์˜ ํ† ํ”ฝ ๋ฒˆํ˜ธ ๋ฆฌ์ŠคํŠธ :',len(topics)) print('์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ์˜ ํ† ํ”ฝ ๋ฒˆํ˜ธ :', topics[0]) ๊ฐ ๋ฌธ์„œ์˜ ํ† ํ”ฝ ๋ฒˆํ˜ธ ๋ฆฌ์ŠคํŠธ : 18846 ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ์˜ ํ† ํ”ฝ ๋ฒˆํ˜ธ : 0 get_topic_info() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํ”ฝ์˜ ๊ฐœ์ˆ˜, ํ† ํ”ฝ์˜ ํฌ๊ธฐ, ๊ฐ ํ† ํ”ฝ์— ํ• ๋‹น๋œ ๋‹จ์–ด๋“ค์„ ์ผ๋ถ€ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. model.get_topic_info() Count ์—ด์˜ ๊ฐ’์„ ๋ชจ๋‘ ํ•ฉํ•˜๋ฉด ์ด ๋ฌธ์„œ์˜ ์ˆ˜์ž…๋‹ˆ๋‹ค. model.get_topic_info()['Count'].sum() 18846 ์œ„์˜ ์ถœ๋ ฅ์—์„œ Topic -1์ด ๊ฐ€์žฅ ํฐ ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. -1์€ ํ† ํ”ฝ์ด ํ• ๋‹น๋˜์ง€ ์•Š์€ ๋ชจ๋“  ์ด์ƒ์น˜ ๋ฌธ์„œ(outliers)๋“ค์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํ˜„์žฌ 0๋ฒˆ ํ† ํ”ฝ๋ถ€ํ„ฐ 210๋ฒˆ ํ† ํ”ฝ๊นŒ์ง€ ์žˆ๋Š”๋ฐ, ์ž„์˜๋กœ 5๋ฒˆ ํ† ํ”ฝ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด๋“ค์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. get_topic() ๋ฉ”์„œ๋“œ์˜ ์ž…๋ ฅ์œผ๋กœ ๋ณด๊ณ ์ž ํ•˜๋Š” ํ† ํ”ฝ์˜ ๋ฒˆํ˜ธ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. model.get_topic(5) [('drive', 0.036501379524217024), ('scsi', 0.027358077330910547), ('drives', 0.0229861502896249), ('ide', 0.019274207233754368), ('disk', 0.01808211458113983), ('controller', 0.016803056719952875), ('hard', 0.013004806725656367), ('scsi2', 0.012107882273732159), ('bios', 0.009949766797753059), ('scsi1', 0.009350150086818809)] 4. ํ† ํ”ฝ ์‹œ๊ฐํ™” BERTopic์„ ์‚ฌ์šฉํ•˜๋ฉด LDAvis์™€ ๋งค์šฐ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑ๋œ ํ† ํ”ฝ์„ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹œ๊ฐํ™”๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋œ ํ† ํ”ฝ์— ๋Œ€ํ•ด ๋” ๋งŽ์€ ํ†ต์ฐฐ๋ ฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  visualize_topics() ๋ฉ”์„œ๋“œ๋กœ ์‹œ๊ฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. model.visualize_topics() 5. ๋‹จ์–ด ์‹œ๊ฐํ™” Visualization_barchart() ๋ฉ”์„œ๋“œ๋Š” c-TF-IDF ์ ์ˆ˜์—์„œ ๋ง‰๋Œ€ ์ฐจํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๊ฐ ํ† ํ”ฝ์— ๋Œ€ํ•ด ์„ ํƒ๋œ ๋‹จ์–ด๋“ค์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ํ† ํ”ฝ์— ๋Œ€ํ•ด์„œ ์„ ํƒ๋œ ๋‹จ์–ด๋“ค์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 6. ํ† ํ”ฝ ์œ ์‚ฌ๋„ ์‹œ๊ฐํ™” ๊ฐ ํ† ํ”ฝ๋“ค์ด ์„œ๋กœ ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€ ์‹œ๊ฐํ™”ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. visualize_heatmap() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํžˆํŠธ๋งต์„ ์‹œ๊ฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํžˆํŠธ๋งต์˜ ์›ํ•˜๋Š” ์œ„์น˜์— ๋งˆ์šฐ์Šค๋ฅผ ๊ฐ–๋‹ค ๋Œ€๋ฉด ์‹ค์งˆ์ ์ธ ์œ ์‚ฌ๋„ ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. model.visualize_heatmap() 7. ํ† ํ”ฝ์˜ ์ˆ˜ ์ •ํ•˜๊ธฐ ๋•Œ๋•Œ๋กœ ๋„ˆ๋ฌด ๋งŽ์€ ํ† ํ”ฝ์ด ์ƒ์„ฑ๋˜๊ฑฐ๋‚˜ ๋„ˆ๋ฌด ์ ์€ ํ† ํ”ฝ์ด ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํ”ฝ์˜ ์ˆ˜๋ฅผ ์ง์ ‘ ์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋ชจ๋ธ ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์— nr_topics ๊ฐ’์œผ๋กœ ์›ํ•˜๋Š” ํ† ํ”ฝ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์›ํ•˜๋Š” ํ† ํ”ฝ์˜ ์ˆ˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BERTopic์€ ์œ ์‚ฌํ•œ ํ† ํ”ฝ๋“ค์„ ์ฐพ์•„ ํ•˜๋‚˜์˜ ํ† ํ”ฝ์œผ๋กœ ๋ณ‘ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ 20๊ฐœ์˜ ํ† ํ”ฝ์œผ๋กœ ํ† ํ”ฝ์˜ ์ˆ˜๋ฅผ ์ถ•์†Œํ•œ ํ›„ ์‹œ๊ฐํ™”ํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. model = BERTopic(nr_topics=20) topics, probabilities = model.fit_transform(docs) model.visualize_topics() ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ๋ชจ๋ธ์ด ์ž๋™์œผ๋กœ ํ† ํ”ฝ์˜ ์ˆ˜๋ฅผ ์ค„์ด๋„๋ก ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ชจ๋ธ ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์— "nr_topics"์˜ ๊ฐ’์„ "auto"๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. model = BERTopic(nr_topics="auto") topics, probabilities = model.fit_transform(docs) model.get_topic_info() ํ† ํ”ฝ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ๋Š” 0๋ฒˆ ํ† ํ”ฝ๋ถ€ํ„ฐ 210๋ฒˆ ํ† ํ”ฝ๊นŒ์ง€ ์ด 211๊ฐœ์˜ ํ† ํ”ฝ์ด ์กด์žฌํ•˜์˜€์œผ๋‚˜, ์ž๋™์œผ๋กœ ํ† ํ”ฝ์˜ ์ˆ˜๊ฐ€ ์ค„์–ด๋“ค๋„๋ก ์„ค์ •ํ•˜์ž ํ† ํ”ฝ์˜ ์ˆ˜๊ฐ€ 0๋ฒˆ๋ถ€ํ„ฐ 143๋ฒˆ๊นŒ์ง€ ์ด 144๊ฐœ๋กœ ์ค„์–ด๋“  ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 8. ์ž„์˜์˜ ๋ฌธ์„œ์— ๋Œ€ํ•œ ์˜ˆ์ธก ํ•™์Šต๋œ ํ† ํ”ฝ ๋ชจ๋ธ์— ์–ด๋–ค ์ž„์˜์˜ ๋ฌธ์„œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ํ•ด๋‹น ๋ฌธ์„œ์˜ ์ฃผ์š” ํ† ํ”ฝ์ด ๋ฌด์—‡์ธ์ง€๋ฅผ ์˜ˆ์ธกํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด transform()์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต์— ์‚ฌ์šฉํ–ˆ๋˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ•˜์—ฌ ํ•ด๋‹น ๋ฌธ์„œ์˜ ์ฃผ์š” ํ† ํ”ฝ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. new_doc = docs[0] print(new_doc) I am sure some bashers of Pens fans are pretty confused about the lack of any kind of posts about the recent Pens massacre of the Devils. Actually, I am bit puzzled too and a bit relieved. However, I am going to put an end to non-PIttsburghers' relief with a bit of praise for the Pens. Man, they are killing those Devils worse than I thought. Jagr just showed you why he is much better than his regular season stats. He is also a lot fo fun to watch in the playoffs. Bowman should let JAgr have a lot of fun in the next couple of games since the Pens are going to beat the pulp out of Jersey anyway. I was very disappointed not to see the Islanders lose the final regular season game. PENS RULE!!! topics, probs = model.transform([new_doc]) print('์˜ˆ์ธกํ•œ ํ† ํ”ฝ ๋ฒˆํ˜ธ :', topics) ์˜ˆ์ธกํ•œ ํ† ํ”ฝ ๋ฒˆํ˜ธ : [0] 9. ๋ชจ๋ธ ์ €์žฅ๊ณผ ๋กœ๋“œ save()์™€ load() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์ €์žฅํ•˜๊ณ  ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. model.save("my_topics_model") BerTopic_model = BERTopic.load("my_topics_model") 19-09 ํ•œ๊ตญ์–ด ๋ฒ„ํ† ํ”ฝ(Korean BERTopic) SBERT๋ฅผ ์ด์šฉํ•œ ํ† ํ”ฝ ๋ชจ๋ธ์ธ BERTopic์€ github์—์„œ 2k ์ด์ƒ์˜ ์Šคํƒ€๋ฅผ ๋ฐ›์•˜์„ ๋งŒํผ ๊ต‰์žฅํžˆ ์ฃผ๋ชฉ๋ฐ›๊ณ  ์žˆ๋Š” ํ† ํ”ฝ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž๋Š” BERTopic์ด LDA๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์„ ๋งŒํผ์˜ ๊ธฐ์ˆ ์ด๋ผ๊ณ  ํ™•์‹ ์„ ์–ป์—ˆ๋‹ค๊ณ  ์ฃผ์žฅํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” BERT ๊ธฐ๋ฐ˜์˜ ํ† ํ”ฝ ๋ชจ๋ธ๋ง ๊ตฌํ˜„์ฒด์ธ BERTopic์˜ ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. !pip install bertopic[visualization] # Colab์— Mecab ์„ค์น˜ !git clone https://github.com/SOMJANG/Mecab-ko-for-Google-Colab.git %cd Mecab-ko-for-Google-Colab !bash install_mecab-ko_on_colab190912.sh 1. BERTopic BERTopic์€ BERT embeddings๊ณผ ํด๋ž˜์Šค ๊ธฐ๋ฐ˜(class-based) TF-IDF๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ฃผ์ œ ์„ค๋ช…์—์„œ ์ค‘์š”ํ•œ ๋‹จ์–ด๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ๋„ ์‰ฝ๊ฒŒ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๋ฐ€ํ•œ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ํ† ํ”ฝ ๋ชจ๋ธ๋ง ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. BERTopic ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํฌ๊ฒŒ ์„ธ ๊ฐ€์ง€ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. 1) ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ SBERT๋กœ ์ž„๋ฒ ๋”ฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ SBERT๋กœ ํ•œ๊ตญ์–ด BERT๋‚˜ ํ•œ๊ตญ์–ด๊ฐ€ ํฌํ•จ๋œ ๋‹ค๊ตญ์–ด BERT๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 2) ๋ฌธ์„œ๋ฅผ ๊ตฐ์ง‘ํ™”ํ•ฉ๋‹ˆ๋‹ค. UMAP์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž„๋ฒ ๋”ฉ์˜ ์ฐจ์›์„ ์ค„์ด๊ณ  HDBSCAN ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฐจ์› ์ถ•์†Œ๋œ ์ž„๋ฒ ๋”ฉ์„ ํด๋Ÿฌ์Šคํ„ฐ๋งํ•˜๊ณ  ์˜๋ฏธ์ ์œผ๋กœ ์œ ์‚ฌํ•œ ๋ฌธ์„œ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 3) ํ† ํ”ฝ ํ‘œํ˜„์„ ์ƒ์„ฑ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ TF-IDF๋กœ ํ† ํ”ฝ์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, CountVectorizer๋Š” ๋ณ„๋„ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ง€์ •ํ•ด ์ฃผ์ง€ ์•Š์œผ๋ฉด ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ํ•œ๊ตญ์–ด์—์„œ ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„ ํ† ํฐํ™”๋Š” ์ง€์–‘๋˜๋ฏ€๋กœ ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 2. ํ•œ๊ตญ์–ด์— ์ ์šฉํ•˜๊ธฐ ๋ณตํ•ฉ ํ† ํ”ฝ ๋ชจ๋ธ์„ ํ•œ๊ตญ์–ด์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ถ”๊ฐ€์ ์ธ ์ฝ”๋“œ ์ˆ˜์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  BERTopic์€ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋‚˜, CountVectorizer๋Š” ๋‹จ์ˆœํžˆ ๋„์–ด์“ฐ๊ธฐ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ํ•œ๊ตญ์–ด์—๋Š” ์ ์ ˆํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด์— ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์„ ์‚ฌ์šฉํ•˜์˜€์œผ๋ฉฐ, SBERT๋„ ํ•œ๊ตญ์–ด์— ๋Œ€ํ•œ ๋ฌธ์„œ ์ž„๋ฒ ๋”ฉ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ SBERT์˜ ์ž„๋ฒ ๋”ฉ ๋˜ํ•œ ํ•œ๊ตญ์–ด๋ฅผ ํฌํ•จํ•œ ๋‹ค๊ตญ์–ด BERT๋กœ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ƒ์„ธ ์ฝ”๋“œ๋Š” github์— ๊ณต๊ฐœํ•˜์˜€์œผ๋ฉฐ ์—ฌ๊ธฐ์„œ๋Š” ๊ฒฐ๊ณผ๋งŒ<NAME>๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณ„๋„์˜ ๋ถˆ์šฉ์–ด ์ฒ˜๋ฆฌ๋Š” ํ•ด์ฃผ์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์ข€ ๋” ์„ฌ์„ธํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด ๋ถˆ์šฉ์–ด๋„ ์ถ”๊ฐ€ํ•ด ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊นƒํ—ˆ๋ธŒ ๋งํฌ : https://github.com/ukairia777/KoBERTopic 20. ํ…์ŠคํŠธ ์š”์•ฝ(Text Summarization) ํ…์ŠคํŠธ ์š”์•ฝ์€ ์ƒ๋Œ€์ ์œผ๋กœ ํฐ ์›๋ฌธ์„ ํ•ต์‹ฌ ๋‚ด์šฉ๋งŒ ๊ฐ„์ถ”๋ ค์„œ ์ƒ๋Œ€์ ์œผ๋กœ ์ž‘์€ ์š”์•ฝ๋ฌธ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋’ค์—์„œ ์ข€ ๋” ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๊ฒ ์ง€๋งŒ, ํ…์ŠคํŠธ ์š”์•ฝ์€ ์ฃผ๋กœ ์ถ”์ถœ์  ์š”์•ฝ๊ณผ ์ถ”์ƒ์  ์š”์•ฝ์œผ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ์ถ”์ƒ์  ์š”์•ฝ์€ ์ถœ์ฒ˜์— ๋”ฐ๋ผ์„œ๋Š” ์ƒ์„ฑ ์š”์•ฝ์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” LSTM์„ ์ด์šฉํ•˜์—ฌ seq2seq๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์ƒ์„ฑ ์š”์•ฝ(์ถ”์ƒ์  ์š”์•ฝ)๊ณผ ํ…์ŠคํŠธ ๋žญํฌ๋ผ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”์ถœ์  ์š”์•ฝ์„ ํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ์˜ˆ์‹œ์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์ข€ ๋” ๊ณ ๋„ํ™”๋œ ์ถ”์ถœ์  ์š”์•ฝ๊ณผ ์ƒ์„ฑ ์š”์•ฝ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” BERT๋กœ ์ถ”์ถœ์  ์š”์•ฝ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ, GPT-2๋‚˜ BART ๋˜๋Š” T5๋กœ ์ƒ์„ฑ ์š”์•ฝ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์ด ์ง‘ํ•„๋  ๋‹น์‹œ์—๋Š” ์•„์‰ฝ๊ฒŒ๋„ ์“ธ๋งŒํ•œ ๊ณต๊ฐœ๋œ ํ•œ๊ตญ์–ด ์š”์•ฝ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ณต๊ฐœ๋˜์ง€ ์•Š์•˜๋˜ ์‹œ์ ์ด๋ผ์„œ ๊ณ ๋„ํ™”๋œ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค๋ฃจ์ง€ ๋ชปํ•˜์˜€์ง€๋งŒ, ์ถ”ํ›„ ์ด ์ฑ…์˜ 2ํŒ์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค๋ฉด ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ๋กœ BERT์™€ GPT-2๋ฅผ ์ด์šฉํ•œ ๊ณ ๋„ํ™”๋œ ์˜ˆ์‹œ๋ฅผ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๊ธฐ๋ฅผ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๋‹ค. 20-01 ์–ดํ…์…˜์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ์š”์•ฝ(Text Summarization with Attention mechanism) ํ…์ŠคํŠธ ์š”์•ฝ์€ ์ƒ๋Œ€์ ์œผ๋กœ ํฐ ์›๋ฌธ์„ ํ•ต์‹ฌ ๋‚ด์šฉ๋งŒ ๊ฐ„์ถ”๋ ค์„œ ์ƒ๋Œ€์ ์œผ๋กœ ์ž‘์€ ์š”์•ฝ๋ฌธ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฝ๋Š” ์‚ฌ๋žŒ์ด ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•ด์„œ ๋‚ด์šฉ์„ ๋น ๋ฅด๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ๊ธ€์„ ๋งŽ์ด ์“ฐ๋Š” ์‚ฌ๋žŒ๋“ค์—๊ฒŒ๋Š” ๊ผญ ํ•„์š”ํ•œ ๋Šฅ๋ ฅ ์ค‘ ํ•˜๋‚˜์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ๊ธฐ๊ณ„๊ฐ€ ์ด๋ฅผ ์ž๋™์œผ๋กœ ํ•ด์ค„ ์ˆ˜๋งŒ ์žˆ๋‹ค๋ฉด ์–ผ๋งˆ๋‚˜ ์ข‹์„๊นŒ์š”? ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ๊ทธ์ค‘ ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ธ seq2seq๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜(attention mechanism)์„ ์ ์šฉํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ๋Š” ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค(Sequences-to-Sequence, seq2seq) ์ฑ•ํ„ฐ๋ฅผ ์„ ํ–‰ํ•˜์‹œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. 1. ํ…์ŠคํŠธ ์š”์•ฝ(Text Summarization) ํ…์ŠคํŠธ ์š”์•ฝ์€ ํฌ๊ฒŒ ์ถ”์ถœ์  ์š”์•ฝ(extractive summarization)๊ณผ ์ถ”์ƒ์  ์š”์•ฝ(abstractive summarization)์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. 1) ์ถ”์ถœ์  ์š”์•ฝ(extractive summarization) ์ถ”์ถœ์  ์š”์•ฝ์€ ์›๋ฌธ์—์„œ ์ค‘์š”ํ•œ ํ•ต์‹ฌ ๋ฌธ์žฅ ๋˜๋Š” ๋‹จ ์–ด๊ตฌ๋ฅผ ๋ช‡ ๊ฐœ ๋ฝ‘์•„์„œ ์ด๋“ค๋กœ ๊ตฌ์„ฑ๋œ ์š”์•ฝ๋ฌธ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ถ”์ถœ์  ์š”์•ฝ์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ์š”์•ฝ๋ฌธ์˜ ๋ฌธ์žฅ์ด๋‚˜ ๋‹จ ์–ด๊ตฌ๋“ค์€ ์ „๋ถ€ ์›๋ฌธ์— ์žˆ๋Š” ๋ฌธ์žฅ๋“ค์ž…๋‹ˆ๋‹ค. ์ถ”์ถœ์  ์š”์•ฝ์˜ ๋Œ€ํ‘œ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋จธ์‹  ๋Ÿฌ๋‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ํ…์ŠคํŠธ ๋žญํฌ(TextRank)๊ฐ€ ์žˆ๋Š”๋ฐ, ์•„๋ž˜์˜ ๋งํฌ์—์„œ ํ…์ŠคํŠธ ๋žญํฌ๋กœ ๊ตฌํ˜„๋œ ์„ธ ์ค„ ์š”์•ฝ๊ธฐ๋ฅผ ์‹œํ—˜ํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://summariz3.herokuapp.com/ ์œ„ ๋งํฌ๋กœ ์ด๋™ํ•˜์—ฌ ์ธํ„ฐ๋„ท ๋‰ด์Šค๋‚˜ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ธ€์„์„ ๋ณต์‚ฌ + ๋ถ™์—ฌ๋„ฃ๊ธฐํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”. ์„ธ ๊ฐœ์˜ ๋ฌธ์žฅ์€ ์ „๋ถ€ ์›๋ฌธ์— ์กด์žฌํ•˜๋˜ ๋ฌธ์žฅ๋“ค์ž…๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์˜ ๋‹จ์ ์ด๋ผ๋ฉด, ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ฌธ์žฅ์ด๋‚˜ ๋‹จ์–ด ๊ตฌ๋กœ๋งŒ ๊ตฌ์„ฑํ•˜๋ฏ€๋กœ ๋ชจ๋ธ์˜ ์–ธ์–ด ํ‘œํ˜„ ๋Šฅ๋ ฅ์ด ์ œํ•œ๋œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋งˆ์น˜ ์‚ฌ๋žŒ์ฒ˜๋Ÿผ ์›๋ฌธ์— ์—†๋˜ ๋‹จ์–ด๋‚˜ ๋ฌธ์žฅ์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ํ•ต์‹ฌ๋งŒ ๊ฐ„์ถ”๋ ค์„œ ํ‘œํ˜„ํ•˜๋Š” ์š”์•ฝ ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ์š”? 2) ์ถ”์ƒ์  ์š”์•ฝ(abstractive summarization) ์ถ”์ƒ์  ์š”์•ฝ์€ ์›๋ฌธ์— ์—†๋˜ ๋ฌธ์žฅ์ด๋ผ๋„ ํ•ต์‹ฌ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ์ƒˆ๋กœ์šด ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•ด์„œ ์›๋ฌธ์„ ์š”์•ฝํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋งˆ์น˜ ์‚ฌ๋žŒ์ด ์š”์•ฝํ•˜๋Š” ๊ฒƒ ๊ฐ™์€ ๋ฐฉ์‹์ธ๋ฐ, ๋‹น์—ฐํžˆ ์ถ”์ถœ์  ์š”์•ฝ๋ณด๋‹ค๋Š” ๋‚œ๋„๊ฐ€ ๋†’์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์ฃผ๋กœ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์‚ฌ์šฉํ•˜๋ฉฐ ๋Œ€ํ‘œ์ ์ธ ๋ชจ๋ธ๋กœ seq2seq๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์˜ ๋‹จ์ ์ด๋ผ๋ฉด seq2seq์™€ ๊ฐ™์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง๋“ค์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€๋„ ํ•™์Šต์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ถ”์ƒ์  ์š”์•ฝ์„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์œผ๋กœ ํ›ˆ๋ จํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” '์›๋ฌธ' ๊ทธ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ '์‹ค์ œ ์š”์•ฝ๋ฌธ'์ด๋ผ๋Š” ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ ์ž์ฒด๊ฐ€ ํ•˜๋‚˜์˜ ๋ถ€๋‹ด์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ด๋ฏธ ๊ณต๊ฐœ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถ”์ƒ์  ์š”์•ฝ์„ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ์•„๋งˆ์กด ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ์•„๋งˆ์กด ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋งํฌ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://www.kaggle.com/snap/amazon-fine-food-reviews ์šฐ์„  ์‹ค์Šต์— ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import numpy as np import pandas as pd import re import matplotlib.pyplot as plt from nltk.corpus import stopwords from bs4 import BeautifulSoup from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences import urllib.request np.random.seed(seed=0) 1) ๋ฐ์ดํ„ฐ ๋กœ๋“œํ•˜๊ธฐ Reviews.csv ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์™€ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ์‹ค์ œ๋กœ๋Š” ์•ฝ 56๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํžˆ 10๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” pd.read_csv์˜ nrows์˜ ์ธ์ž๋กœ 10๋งŒ์ด๋ผ๋Š” ์ˆซ์ž๋ฅผ ์ ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. # Reviews.csv ํŒŒ์ผ์„ data๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅ. ๋‹จ, 10๋งŒ ๊ฐœ์˜ ํ–‰(rows)์œผ๋กœ ์ œํ•œ. data = pd.read_csv("Reviews.csv ํŒŒ์ผ์˜ ๊ฒฝ๋กœ", nrows = 100000) print('์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',(len(data))) ์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 100000 ์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜๊ฐ€ 10๋งŒ ๊ฐœ์ธ ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. 5๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. data.head() ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ์ƒ๋žต 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๋ฉด 'Id', 'ProductId', 'UserId', 'ProfileName', 'HelpfulnessNumerator', 'HelpfulnessDenominator', 'Score', 'Time', 'Summary', 'Text'์ด๋ผ๋Š” 10๊ฐœ์˜ ์—ด์ด ์กด์žฌํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‚ฌ์‹ค ์ด ์ค‘ ํ•„์š”ํ•œ ์—ด์€ 'Text'์—ด๊ณผ 'Summary'์—ด๋ฟ์ž…๋‹ˆ๋‹ค. Text ์—ด๊ณผ Summary ์—ด๋งŒ์„ ๋ถ„๋ฆฌํ•˜๊ณ , ๋‹ค๋ฅธ ์—ด๋“ค์€ ๋ฐ์ดํ„ฐ์—์„œ ์ œ์™ธํ•ด์„œ ์žฌ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. data = data[['Text','Summary']] data.head() Text ์—ด๊ณผ Summary ์—ด๋งŒ ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Text ์—ด์ด ์›๋ฌธ์ด๊ณ , Summary ์—ด์ด Text ์—ด์— ๋Œ€ํ•œ ์š”์•ฝ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ชจ๋ธ์€ Text(์›๋ฌธ)์œผ๋กœ๋ถ€ํ„ฐ Summary(์š”์•ฝ)์„ ์˜ˆ์ธกํ•˜๋„๋ก ํ›ˆ๋ จ๋ฉ๋‹ˆ๋‹ค. ๋žœ๋ค์œผ๋กœ ์ƒ˜ํ”Œ ๋ช‡ ๊ฐ€์ง€๋ฅผ ๋” ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # ๋žœ๋ค์œผ๋กœ 10๊ฐœ์˜ ์ƒ˜ํ”Œ ์ถœ๋ ฅ data.sample(10) ์—ฌ๊ธฐ์„œ๋Š” data.sample(10)๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰ํ–ˆ์ง€๋งŒ ์ง€์†์ ์œผ๋กœ ๋ช‡ ์ฐจ๋ก€ ๋” ์‹คํ–‰ํ•˜๋ฉด์„œ ์ƒ˜ํ”Œ์˜ ๊ตฌ์กฐ๋ฅผ ํ™•์ธํ•ด ๋ณด์„ธ์š”. ์›๋ฌธ์€ ๊ฝค ๊ธด ๋ฐ˜๋ฉด์—, Summary์—๋Š” 3~4๊ฐœ์˜ ๋‹จ์–ด๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฒฝ์šฐ๋„ ๋งŽ์•„ ๋ณด์ž…๋‹ˆ๋‹ค. 2) ๋ฐ์ดํ„ฐ ์ •์ œํ•˜๊ธฐ ๋ฐ์ดํ„ฐ์— ์ค‘๋ณต ์ƒ˜ํ”Œ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('Text ์—ด์—์„œ ์ค‘๋ณต์„ ๋ฐฐ์ œํ•œ ์œ ์ผํ•œ ์ƒ˜ํ”Œ์˜ ์ˆ˜ :', data['Text'].nunique()) print('Summary ์—ด์—์„œ ์ค‘๋ณต์„ ๋ฐฐ์ œํ•œ ์œ ์ผํ•œ ์ƒ˜ํ”Œ์˜ ์ˆ˜ :', data['Summary'].nunique()) Text ์—ด์—์„œ ์ค‘๋ณต์„ ๋ฐฐ์ œํ•œ ์œ ์ผํ•œ ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 88426 Summary ์—ด์—์„œ ์ค‘๋ณต์„ ๋ฐฐ์ œํ•œ ์œ ์ผํ•œ ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 72348 ์ „์ฒด ๋ฐ์ดํ„ฐ๋Š” 10๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” ๊ฝค ๋งŽ์€ ์›๋ฌธ์ด ์ค‘๋ณต๋˜์–ด ์ค‘๋ณต์„ ๋ฐฐ์ œํ•œ ์œ ์ผํ•œ ์›๋ฌธ์˜ ๊ฐœ์ˆ˜๋Š” 88,426๊ฐœ์ž…๋‹ˆ๋‹ค. ์ค‘๋ณต ์ƒ˜ํ”Œ์ด ๋ฌด๋ ค ์•ฝ 1,200๊ฐœ๋‚˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ง€์š”. Summary๋Š” ์ค‘๋ณต์ด ๋” ๋งŽ์ง€๋งŒ, ์›๋ฌธ์€ ๋‹ค๋ฅด๋”๋ผ๋„ ์งง์€ ๋ฌธ์žฅ์ธ ์š”์•ฝ์€ ๋‚ด์šฉ์ด ๊ฒน์น  ์ˆ˜ ์žˆ์Œ์„ ๊ฐ€์ •ํ•˜๊ณ  ์ผ๋‹จ ๋‘๊ฒ ์Šต๋‹ˆ๋‹ค. Summary์˜ ๊ธธ์ด ๋ถ„ํฌ๋Š” ๋’ค์—์„œ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # text ์—ด์—์„œ ์ค‘๋ณต์ธ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ์ค‘๋ณต ์ œ๊ฑฐ data.drop_duplicates(subset=['Text'], inplace=True) print("์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜ :", len(data)) ์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜ : 88426 ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜์—ฌ 88,426๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ Null ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(data.isnull().sum()) Text 0 Summary 1 dtype: int64 Summary์—์„œ 1๊ฐœ์˜ Null ์ƒ˜ํ”Œ์ด ๋‚จ์•„์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. # Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ ์ œ๊ฑฐ data.dropna(axis=0, inplace=True) print('์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜ :',(len(data))) ์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜ : 88425 ์ด์ œ ๋‚จ์€ ์ƒ˜ํ”Œ ์ˆ˜๋Š” 88,425๊ฐœ์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” ๋ถˆํ•„์š”ํ•œ ์ƒ˜ํ”Œ์˜ ์ˆ˜๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•œ ์ •์ œ ๊ณผ์ •์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ƒ˜ํ”Œ ๋‚ด๋ถ€๋ฅผ ์ „์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด ์ •๊ทœํ™”์™€ ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด ๊ฐ๊ฐ์˜ ์ฐธ๊ณ  ์ž๋ฃŒ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์กŒ์ง€๋งŒ ์ŠคํŽ ๋ง์ด ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์„ ์ •๊ทœํ™”ํ•˜๊ธฐ ์œ„ํ•œ ์‚ฌ์ „์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด ์‚ฌ์ „์€ ์•„๋ž˜์˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง„ ์‚ฌ์ „์ž…๋‹ˆ๋‹ค. ๋งํฌ : https://stackoverflow.com/questions/19790188/expanding-english-language-contractions-in-python # ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ๋‚ด ์‚ฌ์šฉ contractions = {"'cause": 'because', "I'd": 'I would', "I'd've": 'I would have', "I'll": 'I will', "I'll've": 'I will have', "I'm": 'I am', "I've": 'I have', "ain't": 'is not', "aren't": 'are not', "can't": 'cannot', "could've": 'could have', "couldn't": 'could not', "didn't": 'did not', "doesn't": 'does not', "don't": 'do not', "hadn't": 'had not', "hasn't": 'has not', "haven't": 'have not', "he'd": 'he would', "he'll": 'he will', "he's": 'he is', "here's": 'here is', "how'd": 'how did', "how'd'y": 'how do you', "how'll": 'how will', "how's": 'how is', "i'd": 'i would', "i'd've": 'i would have', "i'll": 'i will', "i'll've": 'i will have', "i'm": 'i am', "i've": 'i have', "isn't": 'is not', "it'd": 'it would', "it'd've": 'it would have', "it'll": 'it will', "it'll've": 'it will have', "it's": 'it is', "let's": 'let us', "ma'am": 'madam', "mayn't": 'may not', "might've": 'might have', "mightn't": 'might not', "mightn't've": 'might not have', "must've": 'must have', "mustn't": 'must not', "mustn't've": 'must not have', "needn't": 'need not', "needn't've": 'need not have', "o'clock": 'of the clock', "oughtn't": 'ought not', "oughtn't've": 'ought not have', "sha'n't": 'shall not', "shan't": 'shall not', "shan't've": 'shall not have', "she'd": 'she would', "she'd've": 'she would have', "she'll": 'she will', "she'll've": 'she will have', "she's": 'she is', "should've": 'should have', "shouldn't": 'should not', "shouldn't've": 'should not have', "so's": 'so as', "so've": 'so have', "that'd": 'that would', "that'd've": 'that would have', "that's": 'that is', "there'd": 'there would', "there'd've": 'there would have', "there's": 'there is', "they'd": 'they would', "they'd've": 'they would have', "they'll": 'they will', "they'll've": 'they will have', "they're": 'they are', "they've": 'they have', "this's": 'this is', "to've": 'to have', "wasn't": 'was not', "we'd": 'we would', "we'd've": 'we would have', "we'll": 'we will', "we'll've": 'we will have', "we're": 'we are', "we've": 'we have', "weren't": 'were not', "what'll": 'what will', "what'll've": 'what will have', "what're": 'what are', "what's": 'what is', "what've": 'what have', "when's": 'when is', "when've": 'when have', "where'd": 'where did', "where's": 'where is', "where've": 'where have', "who'll": 'who will', "who'll've": 'who will have', "who's": 'who is', "who've": 'who have', "why's": 'why is', "why've": 'why have', "will've": 'will have', "won't": 'will not', "won't've": 'will not have', "would've": 'would have', "wouldn't": 'would not', "wouldn't've": 'would not have', "y'all": 'you all', "y'all'd": 'you all would', "y'all'd've": 'you all would have', "y'all're": 'you all are', "y'all've": 'you all have', "you'd": 'you would', "you'd've": 'you would have', "you'll": 'you will', "you'll've": 'you will have', "you're": 'you are', "you've": 'you have'} NLTK์˜ ๋ถˆ์šฉ์–ด๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. # NLTK์˜ ๋ถˆ์šฉ์–ด stop_words = set(stopwords.words('english')) print('๋ถˆ์šฉ์–ด ๊ฐœ์ˆ˜ :', len(stop_words)) print(stop_words) ๋ถˆ์šฉ์–ด ๊ฐœ์ˆ˜ : 179 {'this', "doesn't", 'until', 'as', ... ์ค‘๋žต ... ,'whom', 'here', 'ma', "it's", 'am', 'your'} ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. # ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜ def preprocess_sentence(sentence, remove_stopwords = True): sentence = sentence.lower() # ํ…์ŠคํŠธ ์†Œ๋ฌธ์žํ™” sentence = BeautifulSoup(sentence, "lxml").text # <br />, <a href = ...> ๋“ฑ์˜ html ํƒœ๊ทธ ์ œ๊ฑฐ sentence = re.sub(r'\([^)]*\)', '', sentence) # ๊ด„ํ˜ธ๋กœ ๋‹ซํžŒ ๋ฌธ์ž์—ด ์ œ๊ฑฐ Ex) my husband (and myself) for => my husband for sentence = re.sub('"','', sentence) # ์Œ๋”ฐ์˜ดํ‘œ " ์ œ๊ฑฐ sentence = ' '.join([contractions[t] if t in contractions else t for t in sentence.split(" ")]) # ์•ฝ์–ด ์ •๊ทœํ™” sentence = re.sub(r"'s\b","",sentence) # ์†Œ์œ ๊ฒฉ ์ œ๊ฑฐ. Ex) roland's -> roland sentence = re.sub("[^a-zA-Z]", " ", sentence) # ์˜์–ด ์™ธ ๋ฌธ์ž(์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž ๋“ฑ) ๊ณต๋ฐฑ์œผ๋กœ ๋ณ€ํ™˜ sentence = re.sub('[m]{2, }', 'mm', sentence) # m์ด 3๊ฐœ ์ด์ƒ์ด๋ฉด 2๊ฐœ๋กœ ๋ณ€๊ฒฝ. Ex) ummmmmmm yeah -> umm yeah # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ (Text) if remove_stopwords: tokens = ' '.join(word for word in sentence.split() if not word in stop_words if len(word) > 1) # ๋ถˆ์šฉ์–ด ๋ฏธ ์ œ๊ฑฐ (Summary) else: tokens = ' '.join(word for word in sentence.split() if len(word) > 1) return tokens ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ๊ฐ ์ค„์— ์ฃผ์„์„ ๋‹ฌ์•˜์œผ๋ฏ€๋กœ ์ž์„ธํ•œ ์„ค๋ช…์€ ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” Text ์—ด์—์„œ๋Š” ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , Summary ์—ด์—์„œ๋Š” ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. Summary๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ•  ๋•Œ๋Š” ๋‘ ๋ฒˆ์งธ ์ธ์ž๋ฅผ 0์œผ๋กœ ์ค˜์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ์•Š๋Š” ๋ฒ„์ „์„ ์‹คํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž„์˜์˜ Text ๋ฌธ์žฅ๊ณผ Summary ๋ฌธ์žฅ์„ ๋งŒ๋“ค์–ด ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•œ ์ „์ฒ˜๋ฆฌ ํ›„์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. temp_text = 'Everything I bought was great, infact I ordered twice and the third ordered was<br />for my mother and father.' temp_summary = 'Great way to start (or finish) the day!!!' print(preprocess_sentence(temp_text)) print(preprocess_sentence(temp_summary, 0)) everything bought great infact ordered twice third ordered wasfor mother father great way to start the day ์šฐ์„  Text ์—ด์— ๋Œ€ํ•ด์„œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ ํ›„์—๋Š” 5๊ฐœ์˜ ์ „์ฒ˜๋ฆฌ ๋œ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. # Text ์—ด ์ „์ฒ˜๋ฆฌ clean_text = [] for s in data['Text']: clean_text.append(preprocess_sentence(s)) clean_text[:5] ['bought several vitality canned dog food products found good quality product looks like stew processed meat smells better labrador finicky appreciates product better', 'product arrived labeled jumbo salted peanuts peanuts actually small sized unsalted sure error vendor intended represent product jumbo', 'confection around centuries light pillowy citrus gelatin nuts case filberts cut tiny squares liberally coated powdered sugar tiny mouthful heaven chewy flavorful highly recommend yummy treat familiar story lewis lion witch wardrobe treat seduces edmund selling brother sisters witch', 'looking secret ingredient robitussin believe found got addition root beer extract ordered made cherry soda flavor medicinal', 'great taffy great price wide assortment yummy taffy delivery quick taffy lover deal'] ์ด์ œ Summary ์—ด์— ๋Œ€ํ•ด์„œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ ํ›„์—๋Š” 5๊ฐœ์˜ ์ „์ฒ˜๋ฆฌ ๋œ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. # Summary ์—ด ์ „์ฒ˜๋ฆฌ clean_summary = [] for s in data['Summary']: clean_summary.append(preprocess_sentence(s, 0)) clean_summary[:5] ['good quality dog food', 'not as advertised', 'delight says it all', 'cough medicine', 'great taffy'] ์ „์ฒ˜๋ฆฌ ํ›„์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. data['Text'] = clean_text data['Summary'] = clean_summary ํ˜น์‹œ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ ๋นˆ ๊ฐ’์ด ์ƒ๊ฒผ๋‹ค๋ฉด Null ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•œ ํ›„์— Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์ƒ๊ฒผ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # ๊ธธ์ด๊ฐ€ ๊ณต๋ฐฑ์ธ ์ƒ˜ํ”Œ์€ NULL ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ data.replace('', np.nan, inplace=True) print(data.isnull().sum()) Text 0 Summary 70 dtype: int64 Summary ์—ด์—์„œ 70๊ฐœ์˜ ์ƒ˜ํ”Œ์ด Null ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด ์ƒ˜ํ”Œ๋“ค์„ ์ œ๊ฑฐํ•ด ์ฃผ๊ณ , ์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. data.dropna(axis = 0, inplace = True) print('์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜ :',(len(data))) ์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜ : 88355 ์ด์ œ Text ์—ด๊ณผ Summary ์—ด์— ๋Œ€ํ•ด์„œ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ๊ธธ์ด ๋ถ„ํฌ ์ถœ๋ ฅ text_len = [len(s.split()) for s in data['Text']] summary_len = [len(s.split()) for s in data['Summary']] print('ํ…์ŠคํŠธ์˜ ์ตœ์†Œ ๊ธธ์ด : {}'.format(np.min(text_len))) print('ํ…์ŠคํŠธ์˜ ์ตœ๋Œ€ ๊ธธ์ด : {}'.format(np.max(text_len))) print('ํ…์ŠคํŠธ์˜ ํ‰๊ท  ๊ธธ์ด : {}'.format(np.mean(text_len))) print('์š”์•ฝ์˜ ์ตœ์†Œ ๊ธธ์ด : {}'.format(np.min(summary_len))) print('์š”์•ฝ์˜ ์ตœ๋Œ€ ๊ธธ์ด : {}'.format(np.max(summary_len))) print('์š”์•ฝ์˜ ํ‰๊ท  ๊ธธ์ด : {}'.format(np.mean(summary_len))) plt.subplot(1,2,1) plt.boxplot(summary_len) plt.title('Summary') plt.subplot(1,2,2) plt.boxplot(text_len) plt.title('Text') plt.tight_layout() plt.show() plt.title('Summary') plt.hist(summary_len, bins=40) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() plt.title('Text') plt.hist(text_len, bins=40) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ํ…์ŠคํŠธ์˜ ์ตœ์†Œ ๊ธธ์ด : 2 ํ…์ŠคํŠธ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 1235 ํ…์ŠคํŠธ์˜ ํ‰๊ท  ๊ธธ์ด : 38.792428272310566 ์š”์•ฝ์˜ ์ตœ์†Œ ๊ธธ์ด : 1 ์š”์•ฝ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 28 ์š”์•ฝ์˜ ํ‰๊ท  ๊ธธ์ด : 4.010729443721352 ์›๋ฌธ ํ…์ŠคํŠธ๋Š” ๋Œ€์ฒด์ ์œผ๋กœ 100์ดํ•˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋˜ํ•œ, ํ‰๊ท  ๊ธธ์ด๋Š” 38์ž…๋‹ˆ๋‹ค. ์š”์•ฝ์˜ ๊ฒฝ์šฐ์—๋Š” ๋Œ€์ฒด์ ์œผ๋กœ 15์ดํ•˜์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋ฉฐ ํ‰๊ท  ๊ธธ์ด๋Š” 4์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํŒจ๋”ฉ์˜ ๊ธธ์ด๋ฅผ ์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ‰๊ท  ๊ธธ์ด๋ณด๋‹ค๋Š” ํฌ๊ฒŒ ์žก์•„ ๊ฐ๊ฐ 50๊ณผ 8๋กœ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. text_max_len = 50 summary_max_len = 8 50๊ณผ 8์ด๋ผ๋Š” ์ด ๋‘ ๊ธธ์ด๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋ณด๋‹ค ํฐ์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. def below_threshold_len(max_len, nested_list): cnt = 0 for s in nested_list: if(len(s.split()) <= max_len): cnt = cnt + 1 print('์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ %s ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: %s'%(max_len, (cnt / len(nested_list)))) ์šฐ์„  Text ์—ด์— ๋Œ€ํ•ด์„œ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. below_threshold_len(text_max_len, data['Text']) ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ 50 ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: 0.7745119121724859 Text ์—ด์€ ๊ธธ์ด๊ฐ€ 50 ์ดํ•˜์ธ ๋น„์œจ์ด 77%์ž…๋‹ˆ๋‹ค. ์•ฝ 23%์˜ ์ƒ˜ํ”Œ์ด ๊ธธ์ด 50๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค. Summary ์—ด์— ๋Œ€ํ•ด์„œ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. below_threshold_len(summary_max_len, data['Summary']) ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ 8 ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: 0.9424593967517402 Summary ์—ด์€ ๊ธธ์ด๊ฐ€ 8 ์ดํ•˜์ธ ๊ฒฝ์šฐ๊ฐ€ 94%์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ •ํ•ด์ค€ ์ตœ๋Œ€ ๊ธธ์ด๋ณด๋‹ค ํฐ ์ƒ˜ํ”Œ๋“ค์€ ์ œ๊ฑฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. data = data[data['Text'].apply(lambda x: len(x.split()) <= text_max_len)] data = data[data['Summary'].apply(lambda x: len(x.split()) <= summary_max_len)] print('์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜ :',(len(data))) ์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜ : 65818 ์ด์ œ ์ƒ˜ํ”Œ ์ˆ˜๊ฐ€ 65,818๊ฐœ๋กœ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. ์ •์ œ ์ž‘์—…์ด ์™„๋ฃŒ๋œ ์ƒ์œ„ ์ƒ˜ํ”Œ 5๊ฐœ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. data.head() seq2seq ํ›ˆ๋ จ์„ ์œ„ํ•ด์„œ๋Š” ๋””์ฝ”๋”์˜ ์ž…๋ ฅ๊ณผ ๋ ˆ์ด๋ธ”์— ์‹œ์ž‘ ํ† ํฐ๊ณผ ์ข…๋ฃŒ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹œ์ž‘ ํ† ํฐ์€ 'sostoken', ์ข…๋ฃŒ ํ† ํฐ์€ 'eostoken'์ด๋ผ ๋ช…๋ช…ํ•˜๊ณ  ์•ž, ๋’ค๋กœ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ์š”์•ฝ ๋ฐ์ดํ„ฐ์—๋Š” ์‹œ์ž‘ ํ† ํฐ๊ณผ ์ข…๋ฃŒ ํ† ํฐ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. data['decoder_input'] = data['Summary'].apply(lambda x : 'sostoken '+ x) data['decoder_target'] = data['Summary'].apply(lambda x : x + ' eostoken') data.head() ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ, ๋””์ฝ”๋”์˜ ์ž…๋ ฅ๊ณผ ๋ ˆ์ด๋ธ”์„ ๊ฐ๊ฐ ์ €์žฅํ•ด ์ค๋‹ˆ๋‹ค. encoder_input = np.array(data['Text']) decoder_input = np.array(data['decoder_input']) decoder_target = np.array(data['decoder_target']) 3) ๋ฐ์ดํ„ฐ์˜ ๋ถ„๋ฆฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„ , ์ˆœ์„œ๊ฐ€ ์„ž์ธ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. indices = np.arange(encoder_input.shape[0]) np.random.shuffle(indices) print(indices) [29546 43316 24839 ... 45891 42613 43567] ์ด ์ •์ˆ˜ ์‹œํ€€์Šค ์ˆœ์„œ๋ฅผ ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ ์ˆœ์„œ๋กœ ์ •์˜ํ•ด ์ฃผ๋ฉด ์ƒ˜ํ”Œ์˜ ์ˆœ์„œ๋Š” ์„ž์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. encoder_input = encoder_input[indices] decoder_input = decoder_input[indices] decoder_target = decoder_target[indices] ์ด์ œ ์„ž์ธ ๋ฐ์ดํ„ฐ๋ฅผ 8:2์˜ ๋น„์œจ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„๋ฆฌํ•ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. n_of_val = int(len(encoder_input)*0.2) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜ :',n_of_val) ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜ : 13163 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ 20%์— ํ•ด๋‹นํ•˜๋Š” 13,163๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. encoder_input_train = encoder_input[:-n_of_val] decoder_input_train = decoder_input[:-n_of_val] decoder_target_train = decoder_target[:-n_of_val] encoder_input_test = encoder_input[-n_of_val:] decoder_input_test = decoder_input[-n_of_val:] decoder_target_test = decoder_target[-n_of_val:] print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :', len(encoder_input_train)) print('ํ›ˆ๋ จ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ :',len(decoder_input_train)) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :',len(encoder_input_test)) print('ํ…Œ์ŠคํŠธ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ :',len(decoder_input_test)) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 52655 ํ›ˆ๋ จ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ : 52655 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 13163 ํ…Œ์ŠคํŠธ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ : 13163 4) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ด์ œ ๊ธฐ๊ณ„๊ฐ€ ํ…์ŠคํŠธ๋ฅผ ์ˆซ์ž๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocaburary)์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ์šฐ์„ , ์›๋ฌธ์— ํ•ด๋‹น๋˜๋Š” encoder_input_train์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. src_tokenizer = Tokenizer() src_tokenizer.fit_on_texts(encoder_input_train) ์ด์ œ ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์ƒ์„ฑ๋˜๋Š” ๋™์‹œ์— ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” src_tokenizer.word_index์— ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ฐฐ์ œํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 7ํšŒ ๋ฏธ๋งŒ์ธ ๋‹จ์–ด๋“ค์ด ์ด ๋ฐ์ดํ„ฐ์—์„œ ์–ผ๋งˆํผ์˜ ๋น„์ค‘์„ ์ฐจ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. threshold = 7 total_cnt = len(src_tokenizer.word_index) # ๋‹จ์–ด์˜ ์ˆ˜ rare_cnt = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ total_freq = 0 # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๋‹จ์–ด ๋นˆ๋„์ˆ˜ ์ดํ•ฉ rare_freq = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜์˜ ์ดํ•ฉ # ๋‹จ์–ด์™€ ๋นˆ๋„์ˆ˜์˜ ์Œ(pair)์„ key์™€ value๋กœ ๋ฐ›๋Š”๋‹ค. for key, value in src_tokenizer.word_counts.items(): total_freq = total_freq + value # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์œผ๋ฉด if(value < threshold): rare_cnt = rare_cnt + 1 rare_freq = rare_freq + value print('๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ :',total_cnt) print('๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ %s ๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: %s'%(threshold - 1, rare_cnt)) print('๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด๋ฅผ ์ œ์™ธํ•  ๊ฒฝ์šฐ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ %s'%(total_cnt - rare_cnt)) print("๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ:", (rare_cnt / total_cnt)*100) print("์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ:", (rare_freq / total_freq)*100) ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ : 32031 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 6๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: 23779 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด๋ฅผ ์ œ์™ธํ•  ๊ฒฝ์šฐ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ 8252 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ: 74.23745746308263 ์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ: 3.393443023084609 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ threshold ๊ฐ’์ธ 7ํšŒ ๋ฏธ๋งŒ. ์ฆ‰, 6ํšŒ ์ดํ•˜์ธ ๋‹จ์–ด๋“ค์€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋ฌด๋ ค 70% ์ด์ƒ์„ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„๋กœ ์ฐจ์ง€ํ•˜๋Š” ๋น„์ค‘์€ ์ƒ๋Œ€์ ์œผ๋กœ ์ ์€ ์ˆ˜์น˜์ธ 3.39%๋ฐ–์— ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 6ํšŒ ์ดํ•˜์ธ ๋‹จ์–ด๋“ค์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ ๋ฐฐ์ œ์‹œํ‚ค๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ์ด๋ฅผ ์ œ์™ธํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ 8,233์œผ๋กœ ๊ณ„์‚ฐํ–ˆ๋Š”๋ฐ, ์ €์ž๋Š” ๊น”๋”ํ•œ ๊ฐ’์„ ์„ ํ˜ธํ•˜์—ฌ ์ด์™€ ๋น„์Šทํ•œ ๊ฐ’์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ 8000์œผ๋กœ ์ œํ•œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. src_vocab = 8000 src_tokenizer = Tokenizer(num_words = src_vocab) src_tokenizer.fit_on_texts(encoder_input_train) # ํ…์ŠคํŠธ ์‹œํ€€์Šค๋ฅผ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ encoder_input_train = src_tokenizer.texts_to_sequences(encoder_input_train) encoder_input_test = src_tokenizer.texts_to_sequences(encoder_input_test) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ •์ƒ ์ง„ํ–‰๋˜์—ˆ๋Š”์ง€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ 3๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(encoder_input_train[:3]) [[1882, 805, 844, 1855, 1120, 72, 131, 203, 1120, 83, 3896, 1, 1013, 844, 757, 167, 601, 350, 519, 435, 2482, 626, 72, 302, 1120, 132, 4281, 1007, 102, 449, 3450, 1, 75, 90, 343, 2307, 1188, 114, 1639, 166, 431, 1333, 1847, 70], [53, 4, 15, 901, 355, 37, 784, 97, 9, 8, 217, 441, 129, 101], [40, 1261, 473, 3, 909, 39, 3249, 2978, 221, 24, 37, 287, 2719, 6125, 56, 1371, 83, 390, 378]] ์ด์ œ ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” ์š”์•ฝ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ์ˆ˜ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. tar_tokenizer = Tokenizer() tar_tokenizer.fit_on_texts(decoder_input_train) ์ด์ œ ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์ƒ์„ฑ๋˜๋Š” ๋™์‹œ์— ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” tar_tokenizer.word_index์— ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 6ํšŒ ๋ฏธ๋งŒ์ธ ๋‹จ์–ด๋“ค์ด ์ด ๋ฐ์ดํ„ฐ์—์„œ ์–ผ๋งˆํผ์˜ ๋น„์ค‘์„ ์ฐจ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. threshold = 6 total_cnt = len(tar_tokenizer.word_index) # ๋‹จ์–ด์˜ ์ˆ˜ rare_cnt = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ total_freq = 0 # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๋‹จ์–ด ๋นˆ๋„์ˆ˜ ์ดํ•ฉ rare_freq = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜์˜ ์ดํ•ฉ # ๋‹จ์–ด์™€ ๋นˆ๋„์ˆ˜์˜ ์Œ(pair)์„ key์™€ value๋กœ ๋ฐ›๋Š”๋‹ค. for key, value in tar_tokenizer.word_counts.items(): total_freq = total_freq + value # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์œผ๋ฉด if(value < threshold): rare_cnt = rare_cnt + 1 rare_freq = rare_freq + value print('๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ :',total_cnt) print('๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ %s ๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: %s'%(threshold - 1, rare_cnt)) print('๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด๋ฅผ ์ œ์™ธํ•  ๊ฒฝ์šฐ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ %s'%(total_cnt - rare_cnt)) print("๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ:", (rare_cnt / total_cnt)*100) print("์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ:", (rare_freq / total_freq)*100) ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ : 10510 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 5๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: 8128 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด๋ฅผ ์ œ์™ธํ•  ๊ฒฝ์šฐ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ 2382 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ: 77.33587059942911 ์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ: 5.896286343062141 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 5ํšŒ ์ดํ•˜์ธ ๋‹จ์–ด๋“ค์€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์•ฝ 77%๋ฅผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„๋กœ ์ฐจ์ง€ํ•˜๋Š” ๋น„์ค‘์€ ์ƒ๋Œ€์ ์œผ๋กœ ๋งค์šฐ ์ ์€ ์ˆ˜์น˜์ธ 5.89%๋ฐ–์— ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๋‹จ์–ด๋“ค์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ ๋ฐฐ์ œ์‹œํ‚ค๊ฒ ์Šต๋‹ˆ๋‹ค. tar_vocab = 2000 tar_tokenizer = Tokenizer(num_words = tar_vocab) tar_tokenizer.fit_on_texts(decoder_input_train) tar_tokenizer.fit_on_texts(decoder_target_train) # ํ…์ŠคํŠธ ์‹œํ€€์Šค๋ฅผ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ decoder_input_train = tar_tokenizer.texts_to_sequences(decoder_input_train) decoder_target_train = tar_tokenizer.texts_to_sequences(decoder_target_train) decoder_input_test = tar_tokenizer.texts_to_sequences(decoder_input_test) decoder_target_test = tar_tokenizer.texts_to_sequences(decoder_target_test) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ •์ƒ ์ง„ํ–‰๋˜์—ˆ๋Š”์ง€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(decoder_input_train[:5]) [[1, 687], [1, 53, 21, 182, 1162, 240], [1, 6, 480, 113, 278, 181], [1, 15, 108, 215], [1, 54, 178, 21]] print(decoder_target_train[:5]) [[687, 2], [53, 21, 182, 1162, 240, 2], [6, 480, 113, 278, 181, 2], [15, 108, 215, 2], [54, 178, 21, 2]] 5) ๋นˆ ์ƒ˜ํ”Œ(empty samples) ์ œ๊ฑฐ ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๊ฐ€ ์‚ญ์ œ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์€ ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋˜์—ˆ๋˜ ์ƒ˜ํ”Œ๋“ค์€ ์ด์ œ ๋นˆ(empty) ์ƒ˜ํ”Œ์ด ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ํ˜„์ƒ์€ ๊ธธ์ด๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ๊ธธ์—ˆ๋˜ ์›๋ฌธ(Text)์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์ œ๊ฐ€ ๋ณ„๋กœ ์—†๊ฒ ์ง€๋งŒ, ์• ์ดˆ์— ํ‰๊ท  ๊ธธ์ด๊ฐ€ 4๋ฐ–์— ๋˜์ง€ ์•Š์•˜๋˜ ์š”์•ฝ๋ฌธ(Summary)์˜ ๊ฒฝ์šฐ์—๋Š” ์ด ํ˜„์ƒ์ด ๊ต‰์žฅํžˆ ๋‘๋“œ๋Ÿฌ์กŒ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์š”์•ฝ๋ฌธ์—์„œ ๊ธธ์ด๊ฐ€ 0์ด ๋œ ์ƒ˜ํ”Œ๋“ค์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ›์•„์˜ต์‹œ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์š”์•ฝ๋ฌธ์ธ decoder_input์—๋Š” sostoken ๋˜๋Š” decoder_target์—๋Š” eostoken์ด ์ถ”๊ฐ€๋œ ์ƒํƒœ์ด๊ณ , ์ด ๋‘ ํ† ํฐ์€ ๋ชจ๋“  ์ƒ˜ํ”Œ์—์„œ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ ๋นˆ๋„์ˆ˜๊ฐ€ ์ƒ˜ํ”Œ ์ˆ˜์™€ ๋™์ผํ•˜์—ฌ ๋‹จ์–ด ์ง‘ํ•ฉ ์ œํ•œ์—๋„ ์‚ญ์ œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด์ œ ๊ธธ์ด๊ฐ€ 0์ด ๋œ ์š”์•ฝ๋ฌธ์˜ ์‹ค์งˆ์  ๊ธธ์ด๋Š” 1์ž…๋‹ˆ๋‹ค. decoder_input์—๋Š” sostoken, decoder_target์—๋Š” eostoken๋งŒ ๋‚จ์•˜์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. drop_train = [index for index, sentence in enumerate(decoder_input_train) if len(sentence) == 1] drop_test = [index for index, sentence in enumerate(decoder_input_test) if len(sentence) == 1] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์š”์•ฝ๋ฌธ์˜ ๊ธธ์ด๊ฐ€ 1์ธ ๊ฒฝ์šฐ์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ๊ฐ drop_train๊ณผ drop_test์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด ์ƒ˜ํ”Œ๋“ค์„ ๋ชจ๋‘ ์‚ญ์ œํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์‚ญ์ œํ•  ๊ฐœ์ˆ˜๋Š” ๊ฐ๊ฐ ๋ช‡ ๊ฐœ์ผ๊นŒ์š”? print('์‚ญ์ œํ•  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :',len(drop_train)) print('์‚ญ์ œํ•  ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :',len(drop_test)) ์‚ญ์ œํ•  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 1235 ์‚ญ์ œํ•  ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 337 ์‚ญ์ œ ํ›„์˜ ๊ฐœ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. encoder_input_train = np.delete(encoder_input_train, drop_train, axis=0) decoder_input_train = np.delete(decoder_input_train, drop_train, axis=0) decoder_target_train = np.delete(decoder_target_train, drop_train, axis=0) encoder_input_test = np.delete(encoder_input_test, drop_test, axis=0) decoder_input_test = np.delete(decoder_input_test, drop_test, axis=0) decoder_target_test = np.delete(decoder_target_test, drop_test, axis=0) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :', len(encoder_input_train)) print('ํ›ˆ๋ จ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ :',len(decoder_input_train)) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :',len(encoder_input_test)) print('ํ…Œ์ŠคํŠธ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ :',len(decoder_input_test)) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 51420 ํ›ˆ๋ จ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ : 51420 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 12826 ํ…Œ์ŠคํŠธ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ : 12826 6) ํŒจ๋”ฉ ํ•˜๊ธฐ ์•ž์„œ ๊ณ„์‚ฐํ•ด๋‘” ์ตœ๋Œ€ ๊ธธ์ด๋กœ ๋งž์ถ”์–ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํŒจ๋”ฉ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. encoder_input_train = pad_sequences(encoder_input_train, maxlen = text_max_len, padding='post') encoder_input_test = pad_sequences(encoder_input_test, maxlen = text_max_len, padding='post') decoder_input_train = pad_sequences(decoder_input_train, maxlen = summary_max_len, padding='post') decoder_target_train = pad_sequences(decoder_target_train, maxlen = summary_max_len, padding='post') decoder_input_test = pad_sequences(decoder_input_test, maxlen = summary_max_len, padding='post') decoder_target_test = pad_sequences(decoder_target_test, maxlen = summary_max_len, padding='post') 3. seq2seq + attention์œผ๋กœ ์š”์•ฝ ๋ชจ๋ธ ์„ค๊ณ„ ๋ฐ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.layers import Input, LSTM, Embedding, Dense, Concatenate from tensorflow.keras.models import Model from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint ์ธ์ฝ”๋”๋ฅผ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ธ์ฝ”๋”๋Š” LSTM ์ธต์„ 3๊ฐœ ์Œ“์Šต๋‹ˆ๋‹ค. embedding_dim = 128 hidden_size = 256 # ์ธ์ฝ”๋” encoder_inputs = Input(shape=(text_max_len,)) # ์ธ์ฝ”๋”์˜ ์ž„๋ฒ ๋”ฉ ์ธต enc_emb = Embedding(src_vocab, embedding_dim)(encoder_inputs) # ์ธ์ฝ”๋”์˜ LSTM 1 encoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True , dropout = 0.4, recurrent_dropout = 0.4) encoder_output1, state_h1, state_c1 = encoder_lstm1(enc_emb) # ์ธ์ฝ”๋”์˜ LSTM 2 encoder_lstm2 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4, recurrent_dropout=0.4) encoder_output2, state_h2, state_c2 = encoder_lstm2(encoder_output1) # ์ธ์ฝ”๋”์˜ LSTM 3 encoder_lstm3 = LSTM(hidden_size, return_state=True, return_sequences=True, dropout=0.4, recurrent_dropout=0.4) encoder_outputs, state_h, state_c= encoder_lstm3(encoder_output2) ๋””์ฝ”๋”๋ฅผ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ, ์ถœ๋ ฅ์ธต์€ ์ œ์™ธํ•˜๊ณ  ์„ค๊ณ„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์„ค๊ณ„๋Š” ์ธ์ฝ”๋”์™€ ์‚ฌ์‹ค์ƒ ๋™์ผํ•˜์ง€๋งŒ ์ดˆ๊ธฐ ์ƒํƒœ(initial_state)๋ฅผ ์ธ์ฝ”๋”์˜ ์ƒํƒœ๋กœ ์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•ฉ์‹œ๋‹ค. # ๋””์ฝ”๋” decoder_inputs = Input(shape=(None,)) # ๋””์ฝ”๋”์˜ ์ž„๋ฒ ๋”ฉ ์ธต dec_emb_layer = Embedding(tar_vocab, embedding_dim) dec_emb = dec_emb_layer(decoder_inputs) # ๋””์ฝ”๋”์˜ LSTM decoder_lstm = LSTM(hidden_size, return_sequences = True, return_state = True, dropout = 0.4, recurrent_dropout=0.2) decoder_outputs, _, _ = decoder_lstm(dec_emb, initial_state = [state_h, state_c]) ์ด์ œ ๋””์ฝ”๋”์˜ ์ถœ๋ ฅ์ธต์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. # ๋””์ฝ”๋”์˜ ์ถœ๋ ฅ์ธต decoder_softmax_layer = Dense(tar_vocab, activation = 'softmax') decoder_softmax_outputs = decoder_softmax_layer(decoder_outputs) # ๋ชจ๋ธ ์ •์˜ model = Model([encoder_inputs, decoder_inputs], decoder_softmax_outputs) model.summary() model.summary() ๊ฒฐ๊ณผ๋Š” ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ์—ฌ๊ธฐ์— ์˜ฌ๋ฆฌ์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด 3,633,104๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ seq2seq ๋ชจ๋ธ์ด ์„ค๊ณ„๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€์˜ ๋ชจ๋ธ ์„ค๊ณ„๋Š” ์•ž์„œ seq2seq ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์› ๋˜ ๋‚ด์šฉ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋ฏ€๋กœ ์œ„์—์„œ ์„ค๊ณ„ํ•œ ์ถœ๋ ฅ์ธต์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ๊ฒฐํ•ฉ๋œ ์ƒˆ๋กœ์šด ์ถœ๋ ฅ์ธต์„ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ์ด๋ฏธ ์ €์ž์˜ ๊นƒํ—ˆ๋ธŒ์— ์ž‘์„ฑ๋œ ์–ดํ…์…˜์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด attention.py ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ , AttentionLayer๋ฅผ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. (๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜์ž…๋‹ˆ๋‹ค.) urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/20.%20Text%20Summarization%20with%20Attention/attention.py", filename="attention.py") from attention import AttentionLayer ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ด์šฉํ•ด ๋””์ฝ”๋”์˜ ์ถœ๋ ฅ์ธต์„ ์ƒˆ๋กญ๊ฒŒ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. # ์–ดํ…์…˜ ์ธต(์–ดํ…์…˜ ํ•จ์ˆ˜) attn_layer = AttentionLayer(name='attention_layer') attn_out, attn_states = attn_layer([encoder_outputs, decoder_outputs]) # ์–ดํ…์…˜์˜ ๊ฒฐ๊ณผ์™€ ๋””์ฝ”๋”์˜ hidden state๋“ค์„ ์—ฐ๊ฒฐ decoder_concat_input = Concatenate(axis = -1, name='concat_layer')([decoder_outputs, attn_out]) # ๋””์ฝ”๋”์˜ ์ถœ๋ ฅ์ธต decoder_softmax_layer = Dense(tar_vocab, activation='softmax') decoder_softmax_outputs = decoder_softmax_layer(decoder_concat_input) # ๋ชจ๋ธ ์ •์˜ model = Model([encoder_inputs, decoder_inputs], decoder_softmax_outputs) model.summary() ์ด 4,276,432๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์ด ์„ค๊ณ„๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค. model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy') ์กฐ๊ธฐ ์ข…๋ฃŒ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜๊ณ  ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค. es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience = 2) history = model.fit(x = [encoder_input_train, decoder_input_train], y = decoder_target_train, \ validation_data = ([encoder_input_test, decoder_input_test], decoder_target_test), batch_size = 256, callbacks=[es], epochs = 50) Train on 51420 samples, validate on 12826 samples Epoch 1/50 51404/51404 [==============================] - 79s 2ms/sample - loss: 3.0293 - val_loss: 2.7390 ... ์ค‘๋žต ... Epoch 26/50 51404/51404 [==============================] - 67s 1ms/sample - loss: 1.7364 - val_loss: 2.0805 Epoch 00026: early stopping ํ•™์Šต ๊ณผ์ • ํ•˜๋ฉด์„œ ๊ธฐ๋ก๋œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์†์‹ค๊ณผ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์†์‹ค ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ์‹œ๊ฐํ™”ํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค plt.plot(history.history['loss'], label='train') plt.plot(history.history['val_loss'], label='test') plt.legend() plt.show() ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์†์‹ค์ด ์ง€์†์ ์œผ๋กœ ์ค„์–ด๋“ค๋‹ค๊ฐ€ ์–ด๋Š ์ˆœ๊ฐ„๋ถ€ํ„ฐ ์ •์ฒดํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 4. seq2seq + attention์œผ๋กœ ์š”์•ฝ ๋ชจ๋ธ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ 3๊ฐœ์˜ ์‚ฌ์ „์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. src_index_to_word = src_tokenizer.index_word # ์›๋ฌธ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์ •์ˆ˜ -> ๋‹จ์–ด๋ฅผ ์–ป์Œ tar_word_to_index = tar_tokenizer.word_index # ์š”์•ฝ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋‹จ์–ด -> ์ •์ˆ˜๋ฅผ ์–ป์Œ tar_index_to_word = tar_tokenizer.index_word # ์š”์•ฝ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์ •์ˆ˜ -> ๋‹จ์–ด๋ฅผ ์–ป์Œ seq2seq๋Š” ํ›ˆ๋ จ ๋‹จ๊ณ„์™€ ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„์˜ ๋™์ž‘์ด ๋‹ค๋ฅด๋ฏ€๋กœ ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„์˜ ๋ชจ๋ธ์„ ๋ณ„๋„๋กœ ๋‹ค์‹œ ์„ค๊ณ„ํ•ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ƒˆ๋กœ์šด seq2seq ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ธ์ฝ”๋”๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. # ์ธ์ฝ”๋” ์„ค๊ณ„ encoder_model = Model(inputs=encoder_inputs, outputs=[encoder_outputs, state_h, state_c]) ์ด์ œ ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„์˜ ๋””์ฝ”๋”๋ฅผ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. # ์ด์ „ ์‹œ์ ์˜ ์ƒํƒœ๋“ค์„ ์ €์žฅํ•˜๋Š” ํ…์„œ decoder_state_input_h = Input(shape=(hidden_size,)) decoder_state_input_c = Input(shape=(hidden_size,)) dec_emb2 = dec_emb_layer(decoder_inputs) # ๋ฌธ์žฅ์˜ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ดˆ๊ธฐ ์ƒํƒœ(initial_state)๋ฅผ ์ด์ „ ์‹œ์ ์˜ ์ƒํƒœ๋กœ ์‚ฌ์šฉ. ์ด๋Š” ๋’ค์˜ ํ•จ์ˆ˜ decode_sequence()์— ๊ตฌํ˜„ # ํ›ˆ๋ จ ๊ณผ์ •์—์„œ์™€ ๋‹ฌ๋ฆฌ LSTM์˜ ๋ฆฌํ„ดํ•˜๋Š” ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ์ธ state_h์™€ state_c๋ฅผ ๋ฒ„๋ฆฌ์ง€ ์•Š์Œ. decoder_outputs2, state_h2, state_c2 = decoder_lstm(dec_emb2, initial_state=[decoder_state_input_h, decoder_state_input_c]) # ์–ดํ…์…˜ ํ•จ์ˆ˜ decoder_hidden_state_input = Input(shape=(text_max_len, hidden_size)) attn_out_inf, attn_states_inf = attn_layer([decoder_hidden_state_input, decoder_outputs2]) decoder_inf_concat = Concatenate(axis=-1, name='concat')([decoder_outputs2, attn_out_inf]) # ๋””์ฝ”๋”์˜ ์ถœ๋ ฅ์ธต decoder_outputs2 = decoder_softmax_layer(decoder_inf_concat) # ์ตœ์ข… ๋””์ฝ”๋” ๋ชจ๋ธ decoder_model = Model( [decoder_inputs] + [decoder_hidden_state_input, decoder_state_input_h, decoder_state_input_c], [decoder_outputs2] + [state_h2, state_c2]) ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„๋ฅผ ์œ„ํ•œ ๋ชจ๋ธ์ด ์™„์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜ decode_sequence๋ฅผ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. def decode_sequence(input_seq): # ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ธ์ฝ”๋”์˜ ์ƒํƒœ๋ฅผ ์–ป์Œ e_out, e_h, e_c = encoder_model.predict(input_seq) # <SOS>์— ํ•ด๋‹นํ•˜๋Š” ํ† ํฐ ์ƒ์„ฑ target_seq = np.zeros((1,1)) target_seq[0, 0] = tar_word_to_index['sostoken'] stop_condition = False decoded_sentence = '' while not stop_condition: # stop_condition์ด True๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฃจํ”„ ๋ฐ˜๋ณต output_tokens, h, c = decoder_model.predict([target_seq] + [e_out, e_h, e_c]) sampled_token_index = np.argmax(output_tokens[0, -1, :]) sampled_token = tar_index_to_word[sampled_token_index] if(sampled_token!='eostoken'): decoded_sentence += ' '+sampled_token # <eos>์— ๋„๋‹ฌํ•˜๊ฑฐ๋‚˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ๋„˜์œผ๋ฉด ์ค‘๋‹จ. if (sampled_token == 'eostoken' or len(decoded_sentence.split()) >= (summary_max_len-1)): stop_condition = True # ๊ธธ์ด๊ฐ€ 1์ธ ํƒ€๊นƒ ์‹œํ€€์Šค๋ฅผ ์—…๋ฐ์ดํŠธ target_seq = np.zeros((1,1)) target_seq[0, 0] = sampled_token_index # ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. e_h, e_c = h, c return decoded_sentence ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„์—์„œ ์›๋ฌธ๊ณผ ์‹ค์ œ ์š”์•ฝ๋ฌธ, ์˜ˆ์ธก ์š”์•ฝ๋ฌธ์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. # ์›๋ฌธ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ def seq2text(input_seq): sentence='' for i in input_seq: if(i!=0): sentence = sentence + src_index_to_word[i]+' ' return sentence # ์š”์•ฝ๋ฌธ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ def seq2summary(input_seq): sentence='' for i in input_seq: if((i!=0 and i!=tar_word_to_index['sostoken']) and i!=tar_word_to_index['eostoken']): sentence = sentence + tar_index_to_word[i] + ' ' return sentence ํ…Œ์ŠคํŠธ ์ƒ˜ํ”Œ ์ค‘ 500๋ฒˆ๋ถ€ํ„ฐ 1000๋ฒˆ๊นŒ์ง€ ํ…Œ์ŠคํŠธํ•ด ๋ด…์‹œ๋‹ค. for i in range(500, 1000): print("์›๋ฌธ : ",seq2text(encoder_input_test[i])) print("์‹ค์ œ ์š”์•ฝ๋ฌธ :",seq2summary(decoder_input_test[i])) print("์˜ˆ์ธก ์š”์•ฝ๋ฌธ :",decode_sequence(encoder_input_test[i].reshape(1, text_max_len))) print("\n") ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์œผ๋ฏ€๋กœ ๊ทธ์ค‘ ๋ช‡ ๊ฐ€์ง€๋งŒ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์›๋ฌธ : great product husband eat kind price could little lower even like jerky eater ์‹ค์ œ ์š”์•ฝ๋ฌธ : best jerky there is ์˜ˆ์ธก ์š”์•ฝ๋ฌธ: great jerky ์›๋ฌธ : perfect stress free afternoon aroma tea makes house smell great drink grade honey bliss ์‹ค์ œ ์š”์•ฝ๋ฌธ : relax cup of tea ์˜ˆ์ธก ์š”์•ฝ๋ฌธ: great tea ์›๋ฌธ : dog loves stuff ground sprinkled dry food gobbles additives fillers carbs also use treat best price amazon quick delivery ์‹ค์ œ ์š”์•ฝ๋ฌธ : great ์˜ˆ์ธก ์š”์•ฝ๋ฌธ: great dog food ์›๋ฌธ : got bbq popchips amazon promotion price came taste good wish less salty would certainly purchase came less salty version ์‹ค์ œ ์š”์•ฝ๋ฌธ : tasty but wish it was less salty ์˜ˆ์ธก ์š”์•ฝ๋ฌธ : not the best ์›๋ฌธ : product arrived broken pieces flavor good actually threw garbage disappointing ์‹ค์ œ ์š”์•ฝ๋ฌธ : very disappointed ์˜ˆ์ธก ์š”์•ฝ๋ฌธ: not as described ์›๋ฌธ : buying quaker oats granola bars nature valley chewy bars better tasting make great snack go chocolate peanuts raisins get better ์‹ค์ œ ์š”์•ฝ๋ฌธ : my new granola bar ์˜ˆ์ธก ์š”์•ฝ๋ฌธ: great snack ์›๋ฌธ : yuck worst chocolate ever save money brand find another even taste taste like chocolate threw rest away ์‹ค์ œ ์š”์•ฝ๋ฌธ : horrible chocolate ์˜ˆ์ธก ์š”์•ฝ๋ฌธ : awful ์›๋ฌธ : kit great rd kit made easy follow instructions new making wines really good kit learn product quite tasty good tropical fruit wine purchased store get idea would making like better store bought one ์‹ค์ œ ์š”์•ฝ๋ฌธ : the kit is great ์˜ˆ์ธก ์š”์•ฝ๋ฌธ : great for cooking ์›๋ฌธ : son particularly picky eater occasionally gets fussy matter mood great meal option fall back always eat also pouch sturdy makes good travel meal option little expensive worth every penny ์‹ค์ œ ์š”์•ฝ๋ฌธ : my son loves these ์˜ˆ์ธก ์š”์•ฝ๋ฌธ : great for baby food ์›๋ฌธ : labeled green tea touch pomegranate raspberry essence might rated higher however name pomegranate raspberry therefore expected vary flavors overall taste mild except greater sweetness tea second ingredient chamomile distinctive presence find flavor unpleasant nothing would make reach one teas ์‹ค์ œ ์š”์•ฝ๋ฌธ : the flavor was not what expected ์˜ˆ์ธก ์š”์•ฝ๋ฌธ : not for me ์›๋ฌธ : delighted food looks nice smells really great smaller size kibble old dog teeth comes ziplock pouch importantly though old dog getting little suddenly gotten since starting chow also noticed terrible daily gone fair time switched wet food harmony foods may also factor extremely happy food continue buy ์‹ค์ œ ์š”์•ฝ๋ฌธ : highly recommend this ์˜ˆ์ธก ์š”์•ฝ๋ฌธ : my dog loves this ์‹ค์ œ ์š”์•ฝ๋ฌธ๊ณผ ์™„์ „ํžˆ ๋˜‘๊ฐ™์ง€ ์•Š์œผ๋ฉด์„œ ์›๋ฌธ์˜ ๋งฅ๋ฝ์„ ์ž˜ ์žก์•„์„œ ์˜ˆ์ธก๋œ ์š”์•ฝ๋ฌธ๋“ค์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์—ฌ๋Ÿฌ๋ถ„์ด ๊ธฐ๋Œ€ํ•˜๋Š” ์ˆ˜์ค€์˜ ์ƒ์„ฑ ์š”์•ฝ์„ ํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ์—๋„ ๊ฝค ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์˜ ์ƒ์„ฑ ์š”์•ฝ์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ์ง์ ‘ seq2seq๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์‚ฌ์ „ ํ•™์Šต๋œ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋””์ฝ”๋”๋ฅผ ์žฅ์ฐฉํ•œ GPT-2๋‚˜ BART, T5์™€ ๊ฐ™์€ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” BART์™€ T-5๊นŒ์ง€๋Š” ๋‹ค๋ฃจ์ง€ ์•Š์ง€๋งŒ, ํ–ฅํ›„ ์ž‘์„ฑ๋  22์ฑ•ํ„ฐ์—์„œ GPT-2๋ฅผ ์ด์šฉํ•œ ๋ฌธ์žฅ ์ƒ์„ฑ์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. GPT-2์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์•„์ง ์ง‘ํ•„๋˜์ง€ ์•Š์ง€๋งŒ, GPT-2์— ๋Œ€ํ•œ ์‹ค์Šต ์ฝ”๋“œ๋Š” ์ด๋ฏธ ์ €์ž์˜ ๊นƒํ—ˆ๋ธŒ์— ์—…๋กœ๋“œ๊ฐ€ ๋˜์–ด ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. 20-02 ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ ๊ธฐ๋ฐ˜ ํ…์ŠคํŠธ ๋žญํฌ(TextRank Based on Sentence Embedding) ์•ž์„œ ์ถ”์ƒ์  ์š”์•ฝ(abstractive summarization)์„ ํ†ตํ•œ ํ…์ŠคํŠธ ์š”์•ฝ์„ ์ˆ˜ํ–‰ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ…์ŠคํŠธ ๋žญํฌ(TextRank) ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋˜ ๋‹ค๋ฅธ ํ…์ŠคํŠธ ์š”์•ฝ ๋ฐฉ๋ฒ•์ธ ์ถ”์ถœ์  ์š”์•ฝ์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํ…์ŠคํŠธ ๋žญํฌ(TextRank) ํ…์ŠคํŠธ ๋žญํฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ, ํ…์ŠคํŠธ ๋žญํฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ธฐ๋ฐ˜์ด ๋œ ํŽ˜์ด์ง€๋žญํฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŽ˜์ด์ง€๋žญํฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฒ€์ƒ‰ ์—”์ง„์—์„œ ์›น ํŽ˜์ด์ง€์˜ ์ˆœ์œ„๋ฅผ ์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋žญํฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํŽ˜์ด์ง€๋žญํฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ํ…์ŠคํŠธ ์š”์•ฝ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋žญํฌ์—์„œ ๊ทธ๋ž˜ํ”„์˜ ๋…ธ๋“œ๋“ค์€ ๋ฌธ์žฅ๋“ค์ด๋ฉฐ, ๊ฐ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” ๋ฌธ์žฅ๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋žญํฌ์— ๋Œ€ํ•œ ์ƒ์„ธ ์„ค๋ช…์€ ํ•˜์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ(Pre-trained Embedding) ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๊ฒ ์ง€๋งŒ, ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•์ธ GloVe, FastText, Word2Vec์˜ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ ์ž„๋ฒ ๋”ฉ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋Š ์ •๋„ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋ฏ€๋กœ ์‹ค์Šต์„ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  GloVe๋งŒ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๊ฒƒ์„ ๊ถŒํ•ฉ๋‹ˆ๋‹ค. import numpy as np import gensim from urllib.request import urlretrieve, urlopen import gzip import zipfile 1. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe ๋‹ค์šด๋กœ๋“œ (์‹ค์Šต์—์„œ ์‚ฌ์šฉ) urlretrieve("http://nlp.stanford.edu/data/glove.6B.zip", filename="glove.6B.zip") zf = zipfile.ZipFile('glove.6B.zip') zf.extractall() zf.close() glove_dict = dict() f = open('glove.6B.100d.txt', encoding="utf8") # 100์ฐจ์›์˜ GloVe ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉ for line in f: word_vector = line.split() word = word_vector[0] word_vector_arr = np.asarray(word_vector[1:], dtype='float32') # 100๊ฐœ์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” array๋กœ ๋ณ€ํ™˜ glove_dict[word] = word_vector_arr f.close() ๋งŒ์•ฝ ๋‹จ์–ด 'cat'์— ๋Œ€ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. glove_dict['cat'] 2. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ FastText ๋‹ค์šด๋กœ๋“œ !pip install fasttext # 300์ฐจ์›์˜ FastText ๋ฒกํ„ฐ ์‚ฌ์šฉ import fasttext.util fasttext.util.download_model('en', if_exists='ignore') ft = fasttext.load_model('cc.en.300.bin') ๋งŒ์•ฝ ๋‹จ์–ด 'cat'์— ๋Œ€ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ft.get_word_vector('cat') 3. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2Vec ๋‹ค์šด๋กœ๋“œ # 300์ฐจ์›์˜ Word2Vec ๋ฒกํ„ฐ ์‚ฌ์šฉ urlretrieve("https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz", \ filename="GoogleNews-vectors-negative300.bin.gz") word2vec_model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin.gz', binary=True) ๋งŒ์•ฝ ๋‹จ์–ด 'cat'์— ๋Œ€ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. word2vec_model['cat'] 3. ๋ฌธ์žฅ ์ž„๋ฒ ๋”ฉ(Sentence Embedding) ์—ฌ๋Ÿฌ๋ถ„์ด ์–ด๋–ค ๋‹ค์ˆ˜์˜ ๋ฌธ์žฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฌธ์žฅ๋“ค์„ ์„œ๋กœ ๋น„๊ตํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋ฌธ์žฅ๋“ค์„ ๊ฐ ๋ฌธ์žฅ์„ ํ‘œํ˜„ํ•˜๋Š” ๊ณ ์ •๋œ ๊ธธ์ด์˜ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค๋ฉด ๋ฒกํ„ฐ ๊ฐ„ ๋น„๊ต๋กœ ๋ฌธ์žฅ์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ ๋ฌธ์žฅ์„ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ• ํ•œ ๊ฐ€์ง€๋ฅผ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์žฅ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ๋ฌธ์žฅ์— ์กด์žฌํ•˜๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์•ž์—์„œ ์†Œ๊ฐœํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹ค์šด๋กœ๋“œํ•œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ glove_dict์—๋Š” 100์ฐจ์›์˜ GloVe ๋ฒกํ„ฐ๋“ค์ด ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. OOV ๋ฌธ์ œ. ์ฆ‰, glove_dict์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋‹จ์–ด๊ฐ€ ๋ฌธ์žฅ์— ์กด์žฌํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  100์ฐจ์›์˜ ์˜๋ฒกํ„ฐ๋„ ๋งŒ๋“ค์–ด๋‘ก๋‹ˆ๋‹ค. embedding_dim = 100 zero_vector = np.zeros(embedding_dim) ์•„๋ž˜ ํ•จ์ˆ˜๋Š” ๋ฌธ์žฅ์˜ ๊ฐ ๋‹จ์–ด๋ฅผ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด์„œ, OOV ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ์˜๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๋ชจ์ธ ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. # ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ํ‰๊ท ์œผ๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š”๋‹ค. def calculate_sentence_vector(sentence): return sum([glove_dict.get(word, zero_vector) for word in sentence])/len(sentence) ๋งŒ์•ฝ I am a student๋ผ๋Š” ๋ฌธ์žฅ ๋ฒกํ„ฐ์˜ ๊ฐ’์„ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด ํ•ด๋‹น ๋ฌธ์žฅ์„ calculate_sentence_vector ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฐ˜ํ™˜๋œ ๋ฒกํ„ฐ ๊ฐ’์˜ ํฌ๊ธฐ๋Š” 100์ฐจ์›์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฑ…์˜ ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ๊ฐ’์„ ํ™•์ธํ•˜์ง„ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. eng_sent = ['I', 'am', 'a', 'student'] sentence_vector = calculate_sentence_vector(eng_sent) print(len(sentence_vector)) 100 ํ˜„์žฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe๋Š” ์˜์–ด์— ๋Œ€ํ•ด์„œ ํ•™์Šต๋œ ์ž„๋ฒ ๋”ฉ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ•œ๊ตญ์–ด๋ฅผ ๋„ฃ์œผ๋ฉด ๋‹น์—ฐํžˆ ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ OOV ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ์˜๋ฒกํ„ฐ์ด๋ฏ€๋กœ ํ‰๊ท ์„ ๊ตฌํ•ด๋„ ์˜๋ฒกํ„ฐ๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๊ฐ’์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. kor_sent = ['์ „', '์ข‹์€', 'ํ•™์ƒ', '์ž…๋‹ˆ๋‹ค'] sentence_vector = calculate_sentence_vector(kor_sent) print(sentence_vector) [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] 4. ํ…์ŠคํŠธ ๋žญํฌ๋ฅผ ์ด์šฉํ•œ ํ…์ŠคํŠธ ์š”์•ฝ ์—ฌ๊ธฐ์„œ๋Š” ์•ž์„œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜์˜€๋‹ค๋Š” ๊ฐ€์ • ํ•˜์— ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. import numpy as np import re import pandas as pd import matplotlib.pyplot as plt from nltk.tokenize import sent_tokenize, word_tokenize from nltk.corpus import stopwords from urllib.request import urlretrieve import zipfile from sklearn.metrics.pairwise import cosine_similarity import networkx as nx NLTK์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ถˆ์šฉ์–ด๋ฅผ ๋ฐ›์•„์˜ต๋‹ˆ๋‹ค. stop_words = stopwords.words('english') ํ…์ŠคํŠธ ์š”์•ฝ์— ์‚ฌ์šฉํ•  ํ…Œ๋‹ˆ์Šค ๊ด€๋ จ ๊ธฐ์‚ฌ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ , ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. urlretrieve("https://raw.githubusercontent.com/prateekjoshi565/textrank_text_summarization/master/tennis_articles_v4.csv", filename="tennis_articles_v4.csv") data = pd.read_csv("tennis_articles_v4.csv") data.head() ์ด 3๊ฐœ์˜ ์—ด์ด ์กด์žฌํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  ์—ด์€ ๊ธฐ์‚ฌ ๋ณธ๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” article_text์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น article_text ์—ด๋งŒ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ•˜๊ณ , ํ•ด๋‹น ๊ธฐ์‚ฌ๋ฅผ ๋ฌธ์žฅ ํ† ํฐํ™”ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•œ sentences๋ผ๋Š” ์—ด์„ ์ƒˆ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. data = data[['article_text']] data['sentences'] = data['article_text'].apply(sent_tokenize) data ํ† ํฐํ™”์™€ ์ „์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. # ํ† ํฐํ™” ํ•จ์ˆ˜ def tokenization(sentences): return [word_tokenize(sentence) for sentence in sentences] # ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜ def preprocess_sentence(sentence): # ์˜์–ด๋ฅผ ์ œ์™ธํ•œ ์ˆซ์ž, ํŠน์ˆ˜ ๋ฌธ์ž ๋“ฑ์€ ์ „๋ถ€ ์ œ๊ฑฐ. ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์€ ์†Œ๋ฌธ์žํ™” sentence = [re.sub(r'[^a-zA-z\s]', '', word).lower() for word in sentence] # ๋ถˆ์šฉ์–ด๊ฐ€ ์•„๋‹ˆ๋ฉด์„œ ๋‹จ์–ด๊ฐ€ ์‹ค์ œ๋กœ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค. return [word for word in sentence if word not in stop_words and word] # ์œ„ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋ฅผ ๋ชจ๋“  ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰. ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ชจ๋“  ํ–‰์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰. def preprocess_sentences(sentences): return [preprocess_sentence(sentence) for sentence in sentences] ๋ฌธ์žฅ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•œ 'sentences'์—ด์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ํ† ํฐํ™”์™€ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•œ 'tokenized_sentences' ์—ด์„ ์ƒˆ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. data['tokenized_sentences'] = data['sentences'].apply(tokenization) data['tokenized_sentences'] = data['tokenized_sentences'].apply(preprocess_sentences) data ํ˜„์žฌ ์‚ฌ์šฉํ•  GloVe ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 100์ž…๋‹ˆ๋‹ค. 100์ฐจ์›์˜ ์˜๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. embedding_dim = 100 zero_vector = np.zeros(embedding_dim) ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ๋ฌธ์žฅ ๊ธธ์ด๊ฐ€ 0์ผ ๊ฒฝ์šฐ์—๋Š” 100์ฐจ์›์˜ ์˜๋ฒกํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ๋ถˆ์šฉ์–ด์ธ ๊ฒฝ์šฐ์—๋Š” ๊ธธ์ด๊ฐ€ 0์ธ ๋ฌธ์žฅ์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. # ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ํ‰๊ท ์œผ๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š”๋‹ค. def calculate_sentence_vector(sentence): if len(sentence) != 0: return sum([glove_dict.get(word, zero_vector) for word in sentence])/len(sentence) else: return zero_vector ์ด๋ฅผ ๋ชจ๋“  ํ–‰์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ชจ๋“  ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. # ๊ฐ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ def sentences_to_vectors(sentences): return [calculate_sentence_vector(sentence) for sentence in sentences] ๋ชจ๋“  ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. data['SentenceEmbedding'] = data['tokenized_sentences'].apply(sentences_to_vectors) data[['SentenceEmbedding']] ๋ฌธ์žฅ ๋ฒกํ„ฐ๋“ค ๊ฐ„์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•œ ์œ ์‚ฌ๋„ ํ–‰๋ ฌ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด ์œ ์‚ฌ๋„ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” (๋ฌธ์žฅ ๊ฐœ์ˆ˜ ร— ๋ฌธ์žฅ ๊ฐœ์ˆ˜)์ž…๋‹ˆ๋‹ค. def similarity_matrix(sentence_embedding): sim_mat = np.zeros([len(sentence_embedding), len(sentence_embedding)]) for i in range(len(sentence_embedding)): for j in range(len(sentence_embedding)): sim_mat[i][j] = cosine_similarity(sentence_embedding[i].reshape(1, embedding_dim), sentence_embedding[j].reshape(1, embedding_dim))[0,0] return sim_mat ์ด ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•œ 'SimMatrix'์—ด์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. data['SimMatrix'] = data['SentenceEmbedding'].apply(similarity_matrix) data['SimMatrix'] ๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ๊ธฐ์ค€์œผ๋กœ ์ง€๊ธˆ๊นŒ์ง€ ๋งŒ๋“  ์—ด๋“ค์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ฌธ์žฅ ๊ฐœ์ˆ˜ :',len(data['tokenized_sentences'][1])) print('๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ฌธ์žฅ ๋ฒกํ„ฐ๊ฐ€ ๋ชจ์ธ ๋ฌธ์žฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) :',np.shape(data['SentenceEmbedding'][1])) print('๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ์œ ์‚ฌ๋„ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) :',data['SimMatrix'][1].shape) ๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ฌธ์žฅ ๊ฐœ์ˆ˜ : 12 ๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ฌธ์žฅ ๋ฒกํ„ฐ๊ฐ€ ๋ชจ์ธ ๋ฌธ์žฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) : (12, 100) ๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ์œ ์‚ฌ๋„ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) : (12, 12) ๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๊ฒฝ์šฐ์—๋Š” ์ด ๋ฌธ์žฅ์ด 12๊ฐœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฌธ์žฅ ๋ฒกํ„ฐ ๋˜ํ•œ 12๊ฐœ๊ฐ€ ์กด์žฌํ•˜๋ฉฐ, ๊ฐ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋Š” 100์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์œ ์‚ฌ๋„ ํ–‰๋ ฌ์€ ๊ฐ ๋ฌธ์žฅ ๋ฒกํ„ฐ๋“ค์˜ ์œ ์‚ฌ๋„๊ฐ€ ๊ธฐ๋ก๋œ ์œ ์‚ฌ๋„ ํ–‰๋ ฌ์ด๋ฏ€๋กœ (๋ฌธ์žฅ ๊ฐœ์ˆ˜ ร— ๋ฌธ์žฅ ๊ฐœ์ˆ˜)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์œ ์‚ฌ๋„ ํ–‰๋ ฌ๋กœ๋ถ€ํ„ฐ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋ด…์‹œ๋‹ค. def draw_graphs(sim_matrix): nx_graph = nx.from_numpy_array(sim_matrix) plt.figure(figsize=(10, 10)) pos = nx.spring_layout(nx_graph) nx.draw(nx_graph, with_labels=True, font_weight='bold') nx.draw_networkx_edge_labels(nx_graph, pos, font_color='red') plt.show() ๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ์œ ์‚ฌ๋„ ํ–‰๋ ฌ๋กœ๋ถ€ํ„ฐ ๊ทธ๋ฆฐ ๊ทธ๋ž˜ํ”„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. draw_graphs(data['SimMatrix'][1]) ๋ฌธ์žฅ ๊ฐœ์ˆ˜๊ฐ€ 12๊ฐœ์˜€์œผ๋ฏ€๋กœ ์ด 12๊ฐœ์˜ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ํŽ˜์ด์ง€๋žญํฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋ฌธ์žฅ์˜ ์ ์ˆ˜๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ 'score'๋ผ๋Š” ์—ด์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. def calculate_score(sim_matrix): nx_graph = nx.from_numpy_array(sim_matrix) scores = nx.pagerank(nx_graph) return scores data['score'] = data['SimMatrix'].apply(calculate_score) data[['SimMatrix', 'score']] ๋‘ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๊ฐ ๋ฌธ์žฅ์˜ ์ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. ์ด 12๊ฐœ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์ ์ˆ˜๊ฐ€ ๊ธฐ๋ก๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. data['score'][1] {0: 0.08315094474060455, 1: 0.08498611405296501, 2: 0.08555019786198463, 3: 0.08383717299575927, 4: 0.0813794030791188, 5: 0.08439285067975581, 6: 0.08507725735628792, 7: 0.08092839280412682, 8: 0.07454046000848007, 9: 0.08535836572027003, 10: 0.0849824249168908, 11: 0.08581641578375629} ์ด์ œ ์ด ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๋ฌธ์žฅ๋“ค์„ ์ƒ์œ„ n ๊ฐœ ์„ ํƒํ•˜์—ฌ ์ด ๋ฌธ์„œ์˜ ์š”์•ฝ๋ฌธ์œผ๋กœ ์‚ผ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์ƒ์œ„ 3๊ฐœ์˜ ๋ฌธ์žฅ์„ ์„ ํƒํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ ์ˆ˜์— ๋”ฐ๋ผ์„œ ์ •๋ ฌ ํ›„์— ์ƒ์œ„ 3๊ฐœ ๋ฌธ์žฅ๋งŒ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. def ranked_sentences(sentences, scores, n=3): top_scores = sorted(((scores[i],s) for i, s in enumerate(sentences)), reverse=True) top_n_sentences = [sentence for score, sentence in top_scores[:n]] return " ".join(top_n_sentences) ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 'summary'๋ผ๋Š” ์—ด์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. data['summary'] = data.apply(lambda x: ranked_sentences(x.sentences, x.score), axis=1) ๋ชจ๋“  ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ์›๋ฌธ๊ณผ ์š”์•ฝ๋ฌธ์„ ์ถœ๋ ฅํ•ด ๋ณผ๊นŒ์š”? for i in range(0, len(data)): print(i+1, '๋ฒˆ ๋ฌธ์„œ') print('์›๋ฌธ :',data.loc[i].article_text) print('') print('์š”์•ฝ :',data.loc[i].summary) print('') 1 ๋ฒˆ ๋ฌธ์„œ ์›๋ฌธ : Maria Sharapova has basically no friends as tennis players on the WTA Tour. The Russian player has no problems in openly speaking about it and in a recent interview she said: 'I don't really hide any feelings too much. I think everyone knows this is my job here. When I'm on the courts or when I'm on the court playing, I'm a competitor and I want to beat every single person whether they're in the locker room or across the net.So I'm not the one to strike up a conversation about the weather and know that in the next few minutes I have to go and try to win a tennis match. I'm a pretty competitive girl. I say my hellos, but I'm not sending any players flowers as well. Uhm, I'm not really friendly or close to many players. I have not a lot of friends away from the courts.' When she said she is not really close to a lot of players, is that something strategic that she is doing? Is it different on the men's tour than the women's tour? 'No, not at all. I think just because you're in the same sport doesn't mean that you have to be friends with everyone just because you're categorized, you're a tennis player, so you're going to get along with tennis players. I think every person has different interests. I have friends that have completely different jobs and interests, and I've met them in very different parts of my life. I think everyone just thinks because we're tennis players we should be the greatest of friends. But ultimately tennis is just a very small part of what we do. There are so many other things that we're interested in, that we do.' ์š”์•ฝ : I think just because you're in the same sport doesn't mean that you have to be friends with everyone just because you're categorized, you're a tennis player, so you're going to get along with tennis players. When I'm on the courts or when I'm on the court playing, I'm a competitor and I want to beat every single person whether they're in the locker room or across the net.So I'm not the one to strike up a conversation about the weather and know that in the next few minutes I have to go and try to win a tennis match. I think everyone just thinks because we're tennis players we should be the greatest of friends. 2 ๋ฒˆ ๋ฌธ์„œ ์›๋ฌธ : BASEL, Switzerland (AP), Roger Federer advanced to the 14th Swiss Indoors final of his career by beating seventh-seeded Daniil Medvedev 6-1, 6-4 on Saturday. Seeking a ninth title at his hometown event, and a 99th overall, Federer will play 93th-ranked Marius Copil on Sunday. Federer dominated the 20th-ranked Medvedev and had his first match-point chance to break serve again at 5-1. He then dropped his serve to love, and let another match point slip in Medvedev's next service game by netting a backhand. He clinched on his fourth chance when Medvedev netted from the baseline. Copil upset expectations of a Federer final against Alexander Zverev in a 6-3, 6-7 (6), 6-4 win over the fifth-ranked German in the earlier semifinal. The Romanian aims for a first title after arriving at Basel without a career win over a top-10 opponent. Copil has two after also beating No. 6 Marin Cilic in the second round. Copil fired 26 aces past Zverev and never dropped serve, clinching after 2 1/2 hours with a forehand volley winner to break Zverev for the second time in the semifinal. He came through two rounds of qualifying last weekend to reach the Basel main draw, including beating Zverev's older brother, Mischa. Federer had an easier time than in his only previous match against Medvedev, a three-setter at Shanghai two weeks ago. ์š”์•ฝ : Federer had an easier time than in his only previous match against Medvedev, a three-setter at Shanghai two weeks ago. Federer dominated the 20th-ranked Medvedev and had his first match-point chance to break serve again at 5-1. Copil fired 26 aces past Zverev and never dropped serve, clinching after 2 1/2 hours with a forehand volley winner to break Zverev for the second time in the semifinal. 3 ๋ฒˆ ๋ฌธ์„œ ์›๋ฌธ : Roger Federer has revealed that organisers of the re-launched and condensed Davis Cup gave him three days to decide if he would commit to the controversial competition. Speaking at the Swiss Indoors tournament where he will play in Sundays final against Romanian qualifier Marius Copil, the world number three said that given the impossibly short time frame to make a decision, he opted out of any commitment. "They only left me three days to decide", Federer said. "I didn't to have time to consult with all the people I had to consult. "I could not make a decision in that time, so I told them to do what they wanted." The 20-time Grand Slam champion has voiced doubts about the wisdom of the one-week format to be introduced by organisers Kosmos, who have promised the International Tennis Federation up to $3 billion in prize money over the next quarter-century. The competition is set to feature 18 countries in the November 18-24 finals in Madrid next year, and will replace the classic home-and-away ties played four times per year for decades. Kosmos is headed by Barcelona footballer Gerard Pique, who is hoping fellow Spaniard Rafael Nadal will play in the upcoming event. Novak Djokovic has said he will give precedence to the ATP's intended re-launch of the defunct World Team Cup in January 2020, at various Australian venues. Major players feel that a big event in late November combined with one in January before the Australian Open will mean too much tennis and too little rest. Federer said earlier this month in Shanghai in that his chances of playing the Davis Cup were all but non-existent. "I highly doubt it, of course. We will see what happens," he said. "I do not think this was designed for me, anyhow. This was designed for the future generation of players." Argentina and Britain received wild cards to the new-look event, and will compete along with the four 2018 semi-finalists and the 12 teams who win qualifying rounds next February. "I don't like being under that kind of pressure," Federer said of the deadline Kosmos handed him. ์š”์•ฝ : Major players feel that a big event in late November combined with one in January before the Australian Open will mean too much tennis and too little rest. Speaking at the Swiss Indoors tournament where he will play in Sundays final against Romanian qualifier Marius Copil, the world number three said that given the impossibly short time frame to make a decision, he opted out of any commitment. "They only left me three days to decide", Federer said. 4 ๋ฒˆ ๋ฌธ์„œ ์›๋ฌธ : Kei Nishikori will try to end his long losing streak in ATP finals and Kevin Anderson will go for his second title of the year at the Erste Bank Open on Sunday. The fifth-seeded Nishikori reached his third final of 2018 after beating Mikhail Kukushkin of Kazakhstan 6-4, 6-3 in the semifinals. A winner of 11 ATP events, Nishikori hasn't triumphed since winning in Memphis in February 2016. He has lost eight straight finals since. The second-seeded Anderson defeated Fernando Verdasco 6-3, 3-6, 6-4. Anderson has a shot at a fifth career title and second of the year after winning in New York in February. Nishikori leads Anderson 4-2 on career matchups, but the South African won their only previous meeting this year. With a victory on Sunday, Anderson will qualify for the ATP Finals. Currently in ninth place, Nishikori with a win could move to within 125 points of the cut for the eight-man event in London next month. Nishikori held serve throughout against Kukushkin, who came through qualifying. He used his first break point to close out the first set before going up 3-0 in the second and wrapping up the win on his first match point. Against Verdasco, Anderson hit nine of his 19 aces in the opening set. The Spaniard broke Anderson twice in the second but didn't get another chance on the South African's serve in the final set. ์š”์•ฝ : Kei Nishikori will try to end his long losing streak in ATP finals and Kevin Anderson will go for his second title of the year at the Erste Bank Open on Sunday. The Spaniard broke Anderson twice in the second but didn't get another chance on the South African's serve in the final set. He has lost eight straight finals since. 5 ๋ฒˆ ๋ฌธ์„œ ์›๋ฌธ : Federer, 37, first broke through on tour over two decades ago and he has since gone on to enjoy a glittering career. The 20-time Grand Slam winner is chasing his 99th ATP title at the Swiss Indoors this week and he faces Jan-Lennard Struff in the second round on Thursday (6pm BST). Davenport enjoyed most of her success in the late 1990s and her third and final major tournament win came at the 2000 Australian Open. But she claims the mentality of professional tennis players slowly began to change after the new millennium. "It seems pretty friendly right now," said Davenport. "I think there is a really nice environment and a great atmosphere, especially between some of the veteran players helping some of the younger players out. "It's a very pleasant atmosphere, I'd have to say, around the locker rooms. "I felt like the best weeks that I had to get to know players when I was playing were the Fed Cup weeks or the Olympic weeks, not necessarily during the tournaments. "And even though maybe we had smaller teams, I still think we kept to ourselves quite a bit. "Not always, but I really feel like in the mid-2000 years there was a huge shift of the attitudes of the top players and being more friendly and being more giving, and a lot of that had to do with players like Roger coming up. "I just felt like it really kind of changed where people were a little bit, definitely in the 90s, a lot more quiet, into themselves, and then it started to become better." Meanwhile, Federer is hoping he can improve his service game as he hunts his ninth Swiss Indoors title this week. "I didn't serve very well [against first-round opponent Filip Kranjovic," Federer said. "I think I was misfiring the corners, I was not hitting the lines enough. "Clearly you make your life more difficult, but still I was up 6-2, 3-1, break points, so things could have ended very quickly today, even though I didn't have the best serve percentage stats. "But maybe that's exactly what caught up to me eventually. It's just getting used to it. This is where the first rounds can be tricky." ์š”์•ฝ : "Not always, but I really feel like in the mid-2000 years there was a huge shift of the attitudes of the top players and being more friendly and being more giving, and a lot of that had to do with players like Roger coming up. "I felt like the best weeks that I had to get to know players when I was playing were the Fed Cup weeks or the Olympic weeks, not necessarily during the tournaments. "Clearly you make your life more difficult, but still I was up 6-2, 3-1, break points, so things could have ended very quickly today, even though I didn't have the best serve percentage stats. 6 ๋ฒˆ ๋ฌธ์„œ ์›๋ฌธ : Nadal has not played tennis since he was forced to retire from the US Open semi-finals against Juan Martin Del Porto with a knee injury. The world No 1 has been forced to miss Spain's Davis Cup clash with France and the Asian hard court season. But with the ATP World Tour Finals due to begin next month, Nadal is ready to prove his fitness before the season-ending event at the 02 Arena. Nadal flew to Paris on Friday and footage from the Paris Masters official Twitter account shows the Spaniard smiling as he strides onto court for practice. The Paris Masters draw has been made and Nadal will start his campaign on Tuesday or Wednesday against either Fernando Verdasco or Jeremy Chardy. Nadal could then play defending champion Jack Sock in the third round before a potential quarter-final with either Borna Coric or Dominic Thiem. Nadal's appearance in Paris is a big boost to the tournament organisers who could see Roger Federer withdraw. Federer is in action at the Swiss Indoors in Basel and if he reaches the final, he could pull out of Paris in a bid to stay fresh for London. But as it stands, Federer is in the draw and is scheduled to face either former world No 3 Milos Raonic or Jo-Wilfried Tsonga in the second round. Federer's projected route to the Paris final could also lead to matches against Kevin Anderson and Novak Djokovic. Djokovic could play Marco Cecchinato in the second round. British No 1 Kyle Edmund is the 12th seed in Paris and will get underway in round two against either Karen Khachanov or Filip Krajinovic. ์š”์•ฝ : Nadal's appearance in Paris is a big boost to the tournament organisers who could see Roger Federer withdraw. Federer's projected route to the Paris final could also lead to matches against Kevin Anderson and Novak Djokovic. But as it stands, Federer is in the draw and is scheduled to face either former world No 3 Milos Raonic or Jo-Wilfried Tsonga in the second round. 7 ๋ฒˆ ๋ฌธ์„œ ์›๋ฌธ : Tennis giveth, and tennis taketh away. The end of the season is finally in sight, and with so many players defending, or losing, huge chunks of points in Singapore, Zhuhai and London, podcast co-hosts Nina Pantic and Irina Falconi discuss the art of defending points (02:14). It's no secret that Jack Sock has struggled on the singles court this year (his record is 7-19). He could lose 1,400 points in the next few weeks, but instead of focusing on the negative, it can all be about perspective (06:28). Let's also not forget his two Grand Slam doubles triumphs this season. Two players, Stefanos Tsitsipas and Kyle Edmund, won their first career ATP titles last week (13:26). It's a big deal because you never forget your first. Irina looks back at her WTA title win in Bogota in 2016, and tells an unforgettable story about her semifinal drama (14:04). In Singapore, one of the biggest storylines (aside from the matches, of course) has been the on-court coaching debate. Nina and Irina give their opinions on what coaching should look like in the future, on both tours (18:55). ์š”์•ฝ : Let's also not forget his two Grand Slam doubles triumphs this season. The end of the season is finally in sight, and with so many players defending, or losing, huge chunks of points in Singapore, Zhuhai and London, podcast co-hosts Nina Pantic and Irina Falconi discuss the art of defending points (02:14). In Singapore, one of the biggest storylines (aside from the matches, of course) has been the on-court coaching debate. 8 ๋ฒˆ ๋ฌธ์„œ ์›๋ฌธ : Federer won the Swiss Indoors last week by beating Romanian qualifier Marius Copil in the final. The 37-year-old claimed his 99th ATP title and is hunting the century in the French capital this week. Federer has been handed a difficult draw where could could come across Kevin Anderson, Novak Djokovic and Rafael Nadal in the latter rounds. But first the 20-time Grand Slam winner wants to train on the Paris Masters court this afternoon before deciding whether to appear for his opening match against either Milos Raonic or Jo-Wilfried Tsonga. "On Monday, I am free and will look how I feel," Federer said after winning the Swiss Indoors. "On Tuesday I will fly to Paris and train in the afternoon to be ready for my first match on Wednesday night. "I felt good all week and better every day. "We also had the impression that at this stage it might be better to play matches than to train. "And as long as I fear no injury, I play." Federer's success in Basel last week was the ninth time he has won his hometown tournament. And he was delighted to be watched on by all of his family and friends as he purchased 60 tickets for the final for those dearest to him. "My children, my parents, my sister and my team are all there," Federer added. "It is always very emotional for me to thank my team. And sometimes it tilts with the emotions, sometimes I just stumble. "It means the world to me. It makes me incredibly happy to win my home tournament and make people happy here. "I do not know if it's maybe my last title, so today I try a lot more to absorb that and enjoy the moments much more consciously. "Maybe I should celebrate as if it were my last title. "There are very touching moments: seeing the ball children, the standing ovations, all the familiar faces in the audience. Because it was not always easy in the last weeks." ์š”์•ฝ : "We also had the impression that at this stage it might be better to play matches than to train. "Maybe I should celebrate as if it were my last title. "On Monday, I am free and will look how I feel," Federer said after winning the Swiss Indoors. 21. ์งˆ์˜์‘๋‹ต(Question Answering, QA) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํŽ˜์ด์Šค๋ถ์—์„œ ๊ณต๊ฐœํ•œ Babi QA ์…‹์„ ๋ฉ”๋ชจ๋ฆฌ ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ํ’€์–ด๋ด…์‹œ๋‹ค. 21-01 ๋ฉ”๋ชจ๋ฆฌ ๋„คํŠธ์›Œํฌ(Memory Network, MemN)๋ฅผ ์ด์šฉํ•œ QA ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋„คํŠธ์›Œํฌ์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด ํŽ˜์ด์Šค๋ถ์ด ๊ณต๊ฐœํ•œ QA ๋ฐ์ดํ„ฐ ์…‹์„ ํ’€์–ด๋ด…์‹œ๋‹ค. ์งˆ์˜์‘๋‹ต(Question Answering)์˜ ๋‹ค๋ฅธ ํ’€์ด๋ฒ•์ด ๊ถ๊ธˆํ•˜์‹œ๋ฉด 18์ฑ•ํ„ฐ์˜ KoBERT๋ฅผ ์ด์šฉํ•œ ๊ธฐ๊ณ„ ๋…ํ•ด ์ฑ•ํ„ฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. 1. Babi ๋ฐ์ดํ„ฐ ์…‹ babi Project์˜ ๋ฐ์ดํ„ฐ ์…‹์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€<NAME>์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ID text ID text ID text ID question[tab] answer[tab] supporting_fact ID. ... ID๋Š” ๊ฐ ๋ฌธ์žฅ์˜ ๋ฒˆํ˜ธ๊ณ ์š”. ์Šคํ† ๋ฆฌ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ๋Š” 1๋ฒˆ์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋กœ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณผ๊ฒŒ์š”. ์šฐ์„  4๊ฐœ์˜ ๋ฌธ์žฅ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์Šคํ† ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. 1 Sandra travelled to the kitchen. 2 Sandra travelled to the hallway. 3 Mary went to the bathroom. 4 Sandra moved to the garden. ์ด์ œ ์œ„์— ์žˆ๋Š” 4๊ฐœ์˜ ๋ฌธ์žฅ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์Šคํ† ๋ฆฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์งˆ๋ฌธ๊ณผ ์ •๋‹ต์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. Sandra๋Š” ์–ด๋”” ์žˆ๋‚˜์š”? Where is Sandra? ์Šคํ† ๋ฆฌ์— ์žˆ๋Š” ๋ฌธ์žฅ๋“ค์„ ๋ณด๋ฉด ๋„ค ๋ฒˆ์งธ ๋ฌธ์žฅ์—์„œ ์šฐ๋ฆฌ๋Š” Sandra๊ฐ€ garden์— ๊ฐ”๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” Garden์€ ์งˆ๋ฌธ ๋‹ค์Œ tab ๋’ค์— ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. 5 Where is Sandra? Garden 4 Garden ์˜†์— ์ˆซ์ž 4๋Š” Supporting fact๋ผ๊ณ  ํ•ด์„œ ์‹ค์ œ ์ •๋‹ต์ด ์ฃผ์–ด์ง„ ์Šคํ† ๋ฆฌ์—์„œ ๋ช‡ ๋ฒˆ id ๋ฌธ์žฅ์— ์žˆ์—ˆ๋Š”์ง€๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๊ทผ๋ฐ ์ด๊ฑธ ์•Œ๋ฉด ํ•™์Šต์ด ๋„ˆ๋ฌด ์‰ฝ๊ฒ ์ฃ ? ๊ทธ๋ž˜์„œ ์‹ค์ œ๋กœ๋Š” ์ธ๊ณต์ง€๋Šฅ์˜ ํ›ˆ๋ จ์ด ๋‹จ๊ณ„์—์„œ๋Š” Supporting fact๋Š” ์›ฌ๋งŒํ•˜๋ฉด ํ•™์Šตํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์›์น™์ž…๋‹ˆ๋‹ค. ์ €ํฌ๋„ Supporting fact๋Š” ํ•™์Šตํ•˜์ง€ ์•Š์„ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ์Šคํ† ๋ฆฌ, ์งˆ๋ฌธ, ๋‹ต์ด๋ผ๋Š” ์„ธ ๊ฐ€์ง€๋ฅผ ํ•˜๋‚˜๋กœ ๋ฌถ์€ ๊ฑธ ๋ณธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋Š” ์Šคํ† ๋ฆฌ์™€ ์งˆ๋ฌธ์ด ๊ต‰์žฅํžˆ ๋งŽ๊ฒ ์ฃ ? ๊ทธ๋ž˜์„œ ์‹ค์ œ ๋ฐ์ดํ„ฐ ์…‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. 1 Mary moved to the bathroom. 2 John went to the hallway. 3 Where is Mary? bathroom 1 4 Daniel went back to the hallway. 5 Sandra moved to the garden. 6 Where is Daniel? hallway 4 7 John moved to the office. 8 Sandra journeyed to the bathroom. 9 Where is Daniel? hallway 4 10 Mary moved to the hallway. 11 Daniel travelled to the office. 12 Where is Daniel? office 11 13 John went back to the garden. 14 John moved to the bedroom. 15 Where is Sandra? bathroom 8 1 Sandra travelled to the office. 2 Sandra went to the bathroom. 3 Where is Sandra? bathroom 2 ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ ์ฒซ ๋ฒˆ์งธ ์Šคํ† ๋ฆฌ๋Š” ๋ฌด๋ ค ID๊ฐ€ 15๋ฒˆ๊นŒ์ง€ ์ด์–ด์กŒ๋Š”๋ฐ, ์ค‘๊ฐ„์— ์งˆ๋ฌธ์€ 3๋ฒˆ, 6๋ฒˆ, 9๋ฒˆ, 11๋ฒˆ, 15๋ฒˆ์œผ๋กœ ๋‹ค์„ฏ ๋ฒˆ์ด๋‚˜ ๋‚˜์™”๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์งˆ๋ฌธ์ด ํ•œ ๋ฒˆ ๋‚˜์™”๋‹ค๊ณ  ํ•ด์„œ ์Šคํ† ๋ฆฌ๊ฐ€ ๋๋‚˜๋Š” ๊ฒŒ ์•„๋‹ˆ์—์š”. ์Šคํ† ๋ฆฌ๋Š” ๊ณ„์† ์ด์–ด์ง€๊ณ  ์งˆ๋ฌธ๋„ ๊ณ„์† ์ด์–ด์ง‘๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์Šคํ† ๋ฆฌ๊ฐ€ ์‹œ์ž‘๋˜์ž 15๋ฒˆ ๋‹ค์Œ์—๋Š” ๋‹ค์‹œ ID๊ฐ€ 1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘๋˜๊ณ  ์žˆ๋„ค์š”. 2. ๋ฉ”๋ชจ๋ฆฌ ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ ์œ„ ๊ทธ๋ฆผ์€ ๋ฉ”๋ชจ๋ฆฌ ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ„๋‹จํžˆ ํ‘œํ˜„ํ•œ ๊ทธ๋ฆผ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์˜ ๊ฐ€์žฅ ์ƒ๋‹จ์„ ๋ณด๋ฉด ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์ด ๋“ค์–ด์˜ต๋‹ˆ๋‹ค. ๊ฐ๊ฐ ์Šคํ† ๋ฆฌ ๋ฌธ์žฅ๊ณผ ์งˆ๋ฌธ ๋ฌธ์žฅ์ž…๋‹ˆ๋‹ค. ์ด ๋‘ ๋ฌธ์žฅ์€ ๊ฐ๊ฐ์˜ ์ž„๋ฒ ๋”ฉ ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๊ทธ๋ฆผ์˜ ์šฐ์ธก ์ƒ๋‹จ์— ์žˆ๋Š” ์ดˆ๋ก์ƒ‰ ๋ฐ•์Šค์™€ ํ•˜๋Š˜์ƒ‰ ๋ฐ•์Šค๋ฅผ ์ฃผ๋ชฉํ•ด ์ฃผ์„ธ์š”. ์Šคํ† ๋ฆฌ ๋ฌธ์žฅ์€ Embedding C๋ฅผ ํ†ตํ•ด์„œ ์ž„๋ฒ ๋”ฉ์ด ๋˜๊ณ , ์งˆ๋ฌธ ๋ฌธ์žฅ์€ Embedding B๋ฅผ ํ†ตํ•ด์„œ ์ž„๋ฒ ๋”ฉ์ด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ž„๋ฒ ๋”ฉ์ด๋ž€ ๋ฌธ์žฅ ๋‚ด ๊ฐ ๋‹จ์–ด๊ฐ€ ์ž„๋ฒ ๋”ฉ์ด ๋˜์–ด ๊ฐ ๋‹จ์–ด๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜๋œ ๋ฌธ์žฅ์„ ์–ป๋Š”๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ์ด ๋œ ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์€ ๋‚ด์ (dot product)์„ ํ†ตํ•ด ๊ฐ ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ณ , ์ด๋ ‡๊ฒŒ ๊ตฌํ•ด์ง„ ์œ ์‚ฌ๋„๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜์„œ Embedding A๋กœ ์ž„๋ฒ ๋”ฉ์ด ๋œ ์Šคํ† ๋ฆฌ ๋ฌธ์žฅ์— ๋”ํ•ด์ง‘๋‹ˆ๋‹ค. ํ˜„์žฌ ์œ„ ๊ทธ๋ฆผ์—์„œ '๋ง์…ˆ'์ด๋ผ๊ณ  ์ ํ˜€์žˆ๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ์—ฐ์‚ฐ์ด ์ง„ํ–‰๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. (Embedding A, B, C๋Š” ๊ฐ๊ฐ ๋ณ„ ๊ฐœ์˜ ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)์ž…๋‹ˆ๋‹ค.) ๊ทธ๋Ÿฐ๋ฐ ์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•œ ์—ฐ์‚ฐ์„ ํ‘œํ˜„์„ ์กฐ๊ธˆ ๋ฐ”๊ฟ”์„œ ๋‹ค์‹œ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์Šคํ† ๋ฆฌ ๋ฌธ์žฅ์„ Value์™€ Key๋ผ๊ณ  ํ•˜๊ณ , ์งˆ๋ฌธ ๋ฌธ์žฅ์„ Query๋ผ๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Query๋Š” Key์™€ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ณ , ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ์ •๊ทœํ™”ํ•˜์—ฌ Value์— ๋”ํ•ด์„œ ์ด ์œ ์‚ฌ๋„ ๊ฐ’์„ ๋ฐ˜์˜ํ•ด ์ค๋‹ˆ๋‹ค. ์–ด๋””์„œ ๋งŽ์ด ๋“ค์–ด๋ณธ ์„ค๋ช… ์•„๋‹Œ๊ฐ€์š”? ๊ฒฐ๊ตญ ์ง€๊ธˆ๊นŒ์ง€์˜ ์—ฐ์‚ฐ ๊ณผ์ •์€ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ์˜๋„๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•œ ์œ„ ๊ทธ๋ฆผ์—์„œ '๋ง์…ˆ'์ด๋ผ๊ณ  ์ ํ˜€์žˆ๋Š” ๋ถ€๋ถ„๊นŒ์ง€์˜ ์—ฐ์‚ฐ์€ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ํ†ตํ•ด์„œ ์งˆ๋ฌธ ๋ฌธ์žฅ๊ณผ์˜ ์œ ์‚ฌ๋„๋ฅผ ๋ฐ˜์˜ํ•œ ์Šคํ† ๋ฆฌ ๋ฌธ์žฅ ํ‘œํ˜„์„ ์–ป๊ธฐ ์œ„ํ•œ ์—ฌ์ •์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์Šคํ† ๋ฆฌ ๋ฌธ์žฅ ํ‘œํ˜„์„ ์งˆ๋ฌธ ๋ฌธ์žฅ์„ ์ž„๋ฒ ๋”ฉํ•œ ์งˆ๋ฌธ ํ‘œํ˜„๊ณผ ์—ฐ๊ฒฐ(concatenate) ํ•ด์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ‘œํ˜„์„ LSTM๊ณผ ๋ฐ€์ง‘์ธต(dense layer)์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋‹ต์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. 3. Babi ๋ฐ์ดํ„ฐ ์…‹ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ Babi ๋ฐ์ดํ„ฐ ์…‹ ์ „์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. from tensorflow.keras.utils import get_file from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical import numpy as np import tarfile from nltk import FreqDist from functools import reduce import os import re import matplotlib.pyplot as plt ์ผ€๋ผ์Šค์˜ get_file์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์…‹์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. path = get_file('babi-tasks-v1-2.tar.gz', origin='https://s3.amazonaws.com/text-datasets/' 'babi_tasks_1-20_v1-2.tar.gz') ์••์ถ•์„ ํ’€๊ณ  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ๊ฐ ์–ป์Šต๋‹ˆ๋‹ค. with tarfile.open(path) as tar: tar.extractall() tar.close() DATA_DIR = 'tasks_1-20_v1-2/en-10k' TRAIN_FILE = os.path.join(DATA_DIR, "qa1_single-supporting-fact_train.txt") TEST_FILE = os.path.join(DATA_DIR, "qa1_single-supporting-fact_test.txt") ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์ƒ์œ„ 20๊ฐœ์˜ ๋ผ์ธ(line)๋งŒ ์ฝ๊ณ  ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. i = 0 lines = open(TRAIN_FILE , "rb") for line in lines: line = line.decode("utf-8").strip() # lno, text = line.split(" ", 1) # ID์™€ TEXT ๋ถ„๋ฆฌ i = i + 1 print(line) if i == 20: break 1 Mary moved to the bathroom. 2 John went to the hallway. 3 Where is Mary? bathroom 1 4 Daniel went back to the hallway. 5 Sandra moved to the garden. 6 Where is Daniel? hallway 4 7 John moved to the office. 8 Sandra journeyed to the bathroom. 9 Where is Daniel? hallway 4 10 Mary moved to the hallway. 11 Daniel travelled to the office. 12 Where is Daniel? office 11 13 John went back to the garden. 14 John moved to the bedroom. 15 Where is Sandra? bathroom 8 1 Sandra travelled to the office. 2 Sandra went to the bathroom. 3 Where is Sandra? bathroom 2 4 Mary went to the bedroom. 5 Daniel moved to the hallway. ์ˆซ์ž 1๋ถ€ํ„ฐ 15๊นŒ์ง€ ํ•œ ๊ฐœ์˜ ์Šคํ† ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๊ทธ ์ค‘๊ฐ„์ค‘๊ฐ„์— ์งˆ๋ฌธ์ด ๋‚˜์˜ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 3๋ฒˆ, 6๋ฒˆ, 9๋ฒˆ, 12๋ฒˆ, 15๋ฒˆ ๋ผ์ธ์ด ๊ฐ ์Šคํ† ๋ฆฌ ์ค‘๊ฐ„์— ์ด์–ด์ง€๋Š” ์งˆ๋ฌธ์— ํ•ด๋‹น๋˜์ง€์š”. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฐ๊ฐ์˜ ์งˆ๋ฌธ ์˜†์—๋Š” ์งˆ๋ฌธ์— ํ•ด๋‹น๋˜๋Š” ์ •๋‹ต์ด ์ ํ˜€์ ธ ์žˆ๊ณ , ๊ทธ ์ •๋‹ต ์˜†์— ๋‚˜์˜ค๋Š” ๋ฒˆํ˜ธ๋Š” ํ•ด๋‹น ์ •๋‹ต์ด ๋ช‡ ๋ฒˆ ๋ฒˆํ˜ธ์˜ ๋ผ์ธ์— ์žˆ์—ˆ๋Š”์ง€๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ˆซ์ž 1์ด ๋‹ค์‹œ ๋‚˜์˜ค๋ฉด ์ด์ œ๋ถ€ํ„ฐ๋Š” ๋‹ค์‹œ ๋ณ„๊ฐœ์˜ ์Šคํ† ๋ฆฌ๊ฐ€ ์‹œ์ž‘๋จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณต์žกํ•œ ํ˜•ํƒœ์˜ ํ…์ŠคํŠธ๋ฅผ ๊ธฐ๊ณ„์—๊ฒŒ ๋ฐ”๋กœ ํ•™์Šต์‹œํ‚ค๋Š” ๊ฒƒ์€ ์กฐ๊ธˆ ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒ˜๋ฆฌ๋ฅผ ๊ฑฐ์ณ์„œ ์Šคํ† ๋ฆฌ, ์งˆ๋ฌธ, ๋‹ต๋ณ€์„ ์ „๋ถ€ ๋ณ„๋„๋กœ ์ €์žฅํ•ด๋‘๊ฒ ์Šต๋‹ˆ๋‹ค. def read_data(dir): stories, questions, answers = [], [], [] # ๊ฐ๊ฐ ์Šคํ† ๋ฆฌ, ์งˆ๋ฌธ, ๋‹ต๋ณ€์„ ์ €์žฅํ•  ์˜ˆ์ • story_temp = [] # ํ˜„์žฌ ์‹œ์ ์˜ ์Šคํ† ๋ฆฌ ์ž„์‹œ ์ €์žฅ lines = open(dir, "rb") for line in lines: line = line.decode("utf-8") # b' ์ œ๊ฑฐ line = line.strip() # '\n' ์ œ๊ฑฐ idx, text = line.split(" ", 1) # ๋งจ ์•ž์— ์žˆ๋Š” id number ๋ถ„๋ฆฌ # ์—ฌ๊ธฐ๊นŒ์ง€๋Š” ๋ชจ๋“  ์ค„์— ์ ์šฉ๋˜๋Š” ์ „์ฒ˜๋ฆฌ if int(idx) == 1: story_temp = [] if "\t" in text: # ํ˜„์žฌ ์ฝ๋Š” ์ค„์ด ์งˆ๋ฌธ (tab) ๋‹ต๋ณ€ (tab)์ธ ๊ฒฝ์šฐ question, answer, _ = text.split("\t") # ์งˆ๋ฌธ๊ณผ ๋‹ต๋ณ€์„ ๊ฐ๊ฐ ์ €์žฅ stories.append([x for x in story_temp if x]) # ์ง€๊ธˆ๊นŒ์ง€์˜ ๋ˆ„์  ์Šคํ† ๋ฆฌ๋ฅผ ์Šคํ† ๋ฆฌ์— ์ €์žฅ questions.append(question) answers.append(answer) else: # ํ˜„์žฌ ์ฝ๋Š” ์ค„์ด ์Šคํ† ๋ฆฌ ์ธ ๊ฒฝ์šฐ story_temp.append(text) # ์ž„์‹œ ์ €์žฅ lines.close() return stories, questions, answers train_data = read_data(TRAIN_FILE) test_data = read_data(TEST_FILE) train_stories, train_questions, train_answers = read_data(TRAIN_FILE) test_stories, test_questions, test_answers = read_data(TEST_FILE) print('ํ›ˆ๋ จ์šฉ ์Šคํ† ๋ฆฌ์˜ ๊ฐœ์ˆ˜ :', len(train_stories)) print('ํ›ˆ๋ จ์šฉ ์งˆ๋ฌธ์˜ ๊ฐœ์ˆ˜ :',len(train_questions)) print('ํ›ˆ๋ จ์šฉ ๋‹ต๋ณ€์˜ ๊ฐœ์ˆ˜ :',len(train_answers)) print('ํ…Œ์ŠคํŠธ์šฉ ์Šคํ† ๋ฆฌ์˜ ๊ฐœ์ˆ˜ :',len(test_stories)) print('ํ…Œ์ŠคํŠธ์šฉ ์งˆ๋ฌธ์˜ ๊ฐœ์ˆ˜ :',len(test_questions)) print('ํ…Œ์ŠคํŠธ์šฉ ๋‹ต๋ณ€์˜ ๊ฐœ์ˆ˜ :',len(test_answers)) ํ›ˆ๋ จ์šฉ ์Šคํ† ๋ฆฌ์˜ ๊ฐœ์ˆ˜ : 10000 ํ›ˆ๋ จ์šฉ ์งˆ๋ฌธ์˜ ๊ฐœ์ˆ˜ : 10000 ํ›ˆ๋ จ์šฉ ๋‹ต๋ณ€์˜ ๊ฐœ์ˆ˜ : 10000 ํ…Œ์ŠคํŠธ์šฉ ์Šคํ† ๋ฆฌ์˜ ๊ฐœ์ˆ˜ : 1000 ํ…Œ์ŠคํŠธ์šฉ ์งˆ๋ฌธ์˜ ๊ฐœ์ˆ˜ : 1000 ํ…Œ์ŠคํŠธ์šฉ ๋‹ต๋ณ€์˜ ๊ฐœ์ˆ˜ : 1000 ์ž„์˜๋กœ 3,576๋ฒˆ์งธ ์Šคํ† ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. train_stories[3576] ['John went back to the garden.', 'Mary went to the kitchen.', 'Sandra went back to the bedroom.', 'John travelled to the bedroom.'] ์ž„์˜๋กœ 3,576๋ฒˆ์งธ ์งˆ๋ฌธ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. train_questions[3576] 'Where is John? ' '์กด์€ ์–ด๋””์•ผ'๋ผ๋Š” ์งˆ๋ฌธ์ด๋„ค์š”. ์œ„์˜ ์Šคํ† ๋ฆฌ์— ๋”ฐ๋ฅด๋ฉด ์กด์€ bedroom์— ์žˆ์Šต๋‹ˆ๋‹ค. 3,576๋ฒˆ์งธ ๋‹ต๋ณ€์„ ์ถœ๋ ฅํ•ด ๋ณผ๊นŒ์š”? train_answers[3576] 'bedroom' bedroom์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. def tokenize(sent): return [ x.strip() for x in re.split('(\W+)', sent) if x and x.strip()] def preprocess_data(train_data, test_data): counter = FreqDist() # ๋‘ ๋ฌธ์žฅ์˜ story๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์œผ๋กœ ํ†ตํ•ฉํ•˜๋Š” ํ•จ์ˆ˜ flatten = lambda data: reduce(lambda x, y: x + y, data) # ๊ฐ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฆฌ์ŠคํŠธ story_len = [] question_len = [] for stories, questions, answers in [train_data, test_data]: for story in stories: stories = tokenize(flatten(story)) # ์Šคํ† ๋ฆฌ์˜ ๋ฌธ์žฅ๋“ค์„ ํŽผ์นœ ํ›„ ํ† ํฐํ™” story_len.append(len(stories)) # ๊ฐ story์˜ ๊ธธ์ด ์ €์žฅ for word in stories: # ๋‹จ์–ด ์ง‘ํ•ฉ์— ๋‹จ์–ด ์ถ”๊ฐ€ counter[word] += 1 for question in questions: question = tokenize(question) question_len.append(len(question)) for word in question: counter[word] += 1 for answer in answers: answer = tokenize(answer) for word in answer: counter[word] += 1 # ๋‹จ์–ด ์ง‘ํ•ฉ ์ƒ์„ฑ word2idx = {word : (idx + 1) for idx, (word, _) in enumerate(counter.most_common())} idx2word = {idx : word for word, idx in word2idx.items()} # ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด story_max_len = np.max(story_len) question_max_len = np.max(question_len) return word2idx, idx2word, story_max_len, question_max_len word2idx, idx2word, story_max_len, question_max_len = preprocess_data(train_data, test_data) print(word2idx) {'to': 1, 'the': 2, '.': 3, 'went': 4, 'Sandra': 5, 'John': 6, 'Daniel': 7, 'Mary': 8, 'travelled': 9, 'journeyed': 10, 'back': 11, 'bathroom': 12, 'garden': 13, 'hallway': 14, 'moved': 15, 'office': 16, 'kitchen': 17, 'bedroom': 18, 'Where': 19, 'is': 20, '?': 21} vocab_size = len(word2idx) + 1 print('์Šคํ† ๋ฆฌ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',story_max_len) print('์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',question_max_len) ์Šคํ† ๋ฆฌ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 68 ์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 4 def vectorize(data, word2idx, story_maxlen, question_maxlen): Xs, Xq, Y = [], [], [] flatten = lambda data: reduce(lambda x, y: x + y, data) stories, questions, answers = data for story, question, answer in zip(stories, questions, answers): xs = [word2idx[w] for w in tokenize(flatten(story))] xq = [word2idx[w] for w in tokenize(question)] Xs.append(xs) Xq.append(xq) Y.append(word2idx[answer]) # ์Šคํ† ๋ฆฌ์™€ ์งˆ๋ฌธ์€ ๊ฐ๊ฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉ # ์ •๋‹ต์€ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ return pad_sequences(Xs, maxlen=story_maxlen),\ pad_sequences(Xq, maxlen=question_maxlen),\ to_categorical(Y, num_classes=len(word2idx) + 1) Xstrain, Xqtrain, Ytrain = vectorize(train_data, word2idx, story_max_len, question_max_len) Xstest, Xqtest, Ytest = vectorize(test_data, word2idx, story_max_len, question_max_len) print(Xstrain.shape, Xqtrain.shape, Ytrain.shape, Xstest.shape, Xqtest.shape, Ytest.shape) (10000, 68) (10000, 4) (10000, 22) (1000, 68) (1000, 4) (1000, 22) 4. ๋ฉ”๋ชจ๋ฆฌ ๋„คํŠธ์›Œํฌ๋กœ QA ํƒœ์Šคํฌ ํ’€๊ธฐ from tensorflow.keras.models import Sequential, Model from tensorflow.keras.layers import Embedding from tensorflow.keras.layers import Permute, dot, add, concatenate from tensorflow.keras.layers import LSTM, Dense, Dropout, Input, Activation # ์—ํฌํฌ ํšŸ์ˆ˜ train_epochs = 120 # ๋ฐฐ์น˜ ํฌ๊ธฐ batch_size = 32 # ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ embed_size = 50 # LSTM์˜ ํฌ๊ธฐ lstm_size = 64 # ๊ณผ์ ํ•ฉ ๋ฐฉ์ง€ ๊ธฐ๋ฒ•์ธ ๋“œ๋กญ์•„์›ƒ ์ ์šฉ ๋น„์œจ dropout_rate = 0.30 # ํ”Œ๋ ˆ์ด์Šค ํ™€๋”. ์ž…๋ ฅ์„ ๋‹ด๋Š” ๋ณ€์ˆ˜ input_sequence = Input((story_max_len,)) question = Input((question_max_len,)) print('Stories :', input_sequence) print('Question:', question) Stories : Tensor("input_1:0", shape=(None, 68), dtype=float32) Question: Tensor("input_2:0", shape=(None, 4), dtype=float32) # ์Šคํ† ๋ฆฌ๋ฅผ ์œ„ํ•œ ์ฒซ ๋ฒˆ์งธ ์ž„๋ฒ ๋”ฉ. ๊ทธ๋ฆผ์—์„œ์˜ Embedding A input_encoder_m = Sequential() input_encoder_m.add(Embedding(input_dim=vocab_size, output_dim=embed_size)) input_encoder_m.add(Dropout(dropout_rate)) # ๊ฒฐ๊ณผ : (samples, story_max_len, embedding_dim) / ์ƒ˜ํ”Œ์˜ ์ˆ˜, ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์› # ์Šคํ† ๋ฆฌ๋ฅผ ์œ„ํ•œ ๋‘ ๋ฒˆ์งธ ์ž„๋ฒ ๋”ฉ. ๊ทธ๋ฆผ์—์„œ์˜ Embedding C # ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ question_max_len(์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด)๋กœ ํ•œ๋‹ค. input_encoder_c = Sequential() input_encoder_c.add(Embedding(input_dim=vocab_size, output_dim=question_max_len)) input_encoder_c.add(Dropout(dropout_rate)) # ๊ฒฐ๊ณผ : (samples, story_max_len, question_max_len) / ์ƒ˜ํ”Œ์˜ ์ˆ˜, ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด, ์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด(์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›) # ์งˆ๋ฌธ์„ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ. ๊ทธ๋ฆผ์—์„œ์˜ Embedding B question_encoder = Sequential() question_encoder.add(Embedding(input_dim=vocab_size, output_dim=embed_size, input_length=question_max_len)) question_encoder.add(Dropout(dropout_rate)) # ๊ฒฐ๊ณผ : (samples, question_max_len, embedding_dim) / ์ƒ˜ํ”Œ์˜ ์ˆ˜, ์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์› # ์‹ค์งˆ์ ์ธ ์ž„๋ฒ ๋”ฉ ๊ณผ์ • input_encoded_m = input_encoder_m(input_sequence) input_encoded_c = input_encoder_c(input_sequence) question_encoded = question_encoder(question) print('Input encoded m', input_encoded_m) print('Input encoded c', input_encoded_c) print('Question encoded', question_encoded) # ์Šคํ† ๋ฆฌ ๋‹จ์–ด๋“ค๊ณผ ์งˆ๋ฌธ ๋‹จ์–ด๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ • # ์œ ์‚ฌ๋„๋Š” ๋‚ด์ ์„ ์‚ฌ์šฉํ•œ๋‹ค. match = dot([input_encoded_m, question_encoded], axes=-1, normalize=False) match = Activation('softmax')(match) print('Match shape', match) # ๊ฒฐ๊ณผ : (samples, story_maxlen, question_max_len) / ์ƒ˜ํ”Œ์˜ ์ˆ˜, ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด, ์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด response = add([match, input_encoded_c]) # (samples, story_max_len, question_max_len) response = Permute((2, 1))(response) # (samples, question_max_len, story_max_len) print('Response shape', response) # ์งˆ๋ฌธ ๋ฒกํ„ฐ์™€ ๋‹ต๋ณ€ ๋ฒกํ„ฐ๋ฅผ ์—ฐ๊ฒฐ answer = concatenate([response, question_encoded]) print('Answer shape', answer) answer = LSTM(lstm_size)(answer) answer = Dropout(dropout_rate)(answer) answer = Dense(vocab_size)(answer) answer = Activation('softmax')(answer) Match shape Tensor("activation/Identity:0", shape=(None, 68, 4), dtype=float32) Response shape Tensor("permute/Identity:0", shape=(None, 4, 68), dtype=float32) Answer shape Tensor("concatenate/Identity:0", shape=(None, 4, 118), dtype=float32) Match shape Tensor("activation/Identity:0", shape=(None, 68, 4), dtype=float32) Response shape Tensor("permute/Identity:0", shape=(None, 4, 68), dtype=float32) Answer shape Tensor("concatenate/Identity:0", shape=(None, 4, 118), dtype=float32) model = Model([input_sequence, question], answer) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc']) print(model.summary()) history = model.fit([Xstrain, Xqtrain], Ytrain, batch_size, train_epochs, validation_data=([Xstest, Xqtest], Ytest)) model.save('model.h5') print("\n ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: %.4f" % (model.evaluate([Xstest, Xqtest], Ytest)[1])) ํ…Œ์ŠคํŠธ ์ •ํ™•๋„: 0.9620 plt.subplot(211) plt.title("Accuracy") plt.plot(history.history["acc"], color="g", label="train") plt.plot(history.history["val_acc"], color="b", label="validation") plt.legend(loc="best") plt.subplot(212) plt.title("Loss") plt.plot(history.history["loss"], color="g", label="train") plt.plot(history.history["val_loss"], color="b", label="validation") plt.legend(loc="best") plt.tight_layout() plt.show() ytest = np.argmax(Ytest, axis=1) Ytest_ = model.predict([Xstest, Xqtest]) ytest_ = np.argmax(Ytest_, axis=1) 21-02 MemN์œผ๋กœ ํ•œ๊ตญ์–ด QA ํ•ด๋ณด๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํ•œ๊ตญ์–ด Babi ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•ด์„œ ์ด์ „ ์ฑ•ํ„ฐ์—์„œ ๊ตฌํ˜„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ๋„คํŠธ์›Œํฌ๋ฅผ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์งˆ์˜์‘๋‹ต(Question Answering)์˜ ๋‹ค๋ฅธ ํ’€์ด๋ฒ•์ด ๊ถ๊ธˆํ•˜์‹œ๋ฉด 18์ฑ•ํ„ฐ์˜ KoBERT๋ฅผ ์ด์šฉํ•œ ๊ธฐ๊ณ„ ๋…ํ•ด ์ฑ•ํ„ฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. 1. ์ปค์Šคํ„ฐ๋งˆ์ด์ฆˆ๋“œ KoNLPy ์‚ฌ์šฉํ•˜๊ธฐ ์˜์–ด๊ถŒ ์–ธ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๋งŒ ํ•ด๋„ ๋‹จ์–ด๋“ค์ด ์ž˜ ๋ถ„๋ฆฌ๋˜์ง€๋งŒ, ํ•œ๊ตญ์–ด๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๊ณ  ์•ž์—์„œ ๋ช‡ ์ฐจ๋ก€ ์–ธ๊ธ‰ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋งŒํผ ์ด๋ฒˆ์—๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ์ด๋Ÿฐ ์ƒํ™ฉ์— ๋ด‰์ฐฉํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ํ˜•ํƒœ์†Œ ๋ถ„์„ ์ž…๋ ฅ : '์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.' ํ˜•ํƒœ์†Œ ๋ถ„์„ ๊ฒฐ๊ณผ : ['์€', '๊ฒฝ์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ์‚ฌ์‹ค ์œ„๋ฌธ์žฅ์—์„œ '์€๊ฒฝ์ด'๋Š” ์‚ฌ๋žŒ ์ด๋ฆ„์ด๋ฏ€๋กœ ์ œ๋Œ€๋กœ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” '์€', '๊ฒฝ์ด'์™€ ๊ฐ™์ด ๊ธ€์ž๊ฐ€ ๋ถ„๋ฆฌ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ '์€๊ฒฝ์ด' ๋˜๋Š” ์ตœ์†Œํ•œ '์€๊ฒฝ'์ด๋ผ๋Š” ๋‹จ์–ด ํ† ํฐ์„ ์–ป์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์— ์‚ฌ์šฉ์ž ์‚ฌ์ „์„ ์ถ”๊ฐ€ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. '์€๊ฒฝ์ด'๋Š” ํ•˜๋‚˜์˜ ๋‹จ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„๋ฆฌํ•˜์ง€ ๋ง๋ผ๊ณ  ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์— ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์‚ฌ์ „์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋งˆ๋‹ค ๋‹ค๋ฅธ๋ฐ, ์ƒ๊ฐ๋ณด๋‹ค ๋ณต์žกํ•œ ๊ฒฝ์šฐ๋“ค์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” Customized Konlpy๋ผ๋Š” ์‚ฌ์šฉ์ž ์‚ฌ์ „ ์ถ”๊ฐ€๊ฐ€ ๋งค์šฐ ์‰ฌ์šด ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กฌํ”„ํŠธ์—์„œ ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install customized_konlpy ์ด์ œ customized_konlpy์—์„œ ์ œ๊ณตํ•˜๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Twitter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ž์„œ ์†Œ๊ฐœํ–ˆ๋˜ ์˜ˆ๋ฌธ์„ ๋‹จ์–ด ํ† ํฐํ™”ํ•ด๋ด…์‹œ๋‹ค. from ckonlpy.tag import Twitter twitter = Twitter() twitter.morphs('์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.') ['์€', '๊ฒฝ์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ์•ž์„œ ์†Œ๊ฐœํ•œ ์˜ˆ์‹œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ '์€๊ฒฝ์ด'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ '์€', '๊ฒฝ์ด'์™€ ๊ฐ™์ด ๋ถ„๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Twitter์— add_dictionary('๋‹จ์–ด', 'ํ’ˆ์‚ฌ')์™€ ๊ฐ™์€<NAME>์œผ๋กœ ์‚ฌ์ „ ์ถ”๊ฐ€๋ฅผ ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. twitter.add_dictionary('์€๊ฒฝ์ด', 'Noun') ์ œ๋Œ€๋กœ ๋ฐ˜์˜๋˜์—ˆ๋Š”์ง€ ๋™์ผํ•œ ์˜ˆ๋ฌธ์„ ๋‹ค์‹œ ํ˜•ํƒœ์†Œ ๋ถ„์„ํ•ด ๋ด…์‹œ๋‹ค. twitter.morphs('์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.') ['์€๊ฒฝ์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ์ด์ œ๋Š” '์€๊ฒฝ์ด'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ œ๋Œ€๋กœ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ์ธ์‹๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. ํ•œ๊ตญ์–ด Babi ๋ฐ์ดํ„ฐ ์…‹ ๋กœ๋“œ์™€ ์ „์ฒ˜๋ฆฌ ํ•œ๊ตญ์–ด Babi ๋ฐ์ดํ„ฐ ์…‹์€ ์ €์ž๊ฐ€ ์˜์–ด ๋ฐ์ดํ„ฐ ์…‹์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋งŒ๋“ค์—ˆ์œผ๋ฉฐ, ์•„๋ž˜์˜ ๋งํฌ์—์„œ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ : https://bit.ly/31SqtHy ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : https://bit.ly/3f7rH5g ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. from ckonlpy.tag import Twitter from tensorflow.keras.utils import get_file from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical import numpy as np from nltk import FreqDist from functools import reduce import os import re import matplotlib.pyplot as plt ๋‹ค์šด๋กœ๋“œํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. TRAIN_FILE = os.path.join("qa1_single-supporting-fact_train_kor.txt") TEST_FILE = os.path.join("qa1_single-supporting-fact_test_kor.txt") ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์ƒ์œ„ 20๊ฐœ์˜ ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. i = 0 lines = open(TRAIN_FILE , "rb") for line in lines: line = line.decode("utf-8").strip() i = i + 1 print(line) if i == 20: break 1 ํ•„์›…์ด๋Š” ํ™”์žฅ์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค. 2 ์€๊ฒฝ์ด๋Š” ๋ณต๋„๋กœ ์ด๋™ํ–ˆ์Šต๋‹ˆ๋‹ค. 3 ํ•„์›…์ด๋Š” ์–ด๋””์•ผ? ํ™”์žฅ์‹ค 1 4 ์ˆ˜์ข…์ด๋Š” ๋ณต๋„๋กœ ๋ณต๊ท€ํ–ˆ์Šต๋‹ˆ๋‹ค. 5 ๊ฒฝ์ž„์ด๋Š” ์ •์›์œผ๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค. 6 ์ˆ˜์ข…์ด๋Š” ์–ด๋””์•ผ? ๋ณต๋„ 4 7 ์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค. 8 ๊ฒฝ์ž„์ด๋Š” ํ™”์žฅ์‹ค๋กœ ๋›ฐ์–ด๊ฐ”์Šต๋‹ˆ๋‹ค. 9 ์ˆ˜์ข…์ด๋Š” ์–ด๋””์•ผ? ๋ณต๋„ 4 10 ํ•„์›…์ด๋Š” ๋ณต๋„๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค. 11 ์ˆ˜์ข…์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค. 12 ์ˆ˜์ข…์ด๋Š” ์–ด๋””์•ผ? ์‚ฌ๋ฌด์‹ค 11 13 ์€๊ฒฝ์ด๋Š” ์ •์›์œผ๋กœ ๋ณต๊ท€ํ–ˆ์Šต๋‹ˆ๋‹ค. 14 ์€๊ฒฝ์ด๋Š” ์นจ์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค. 15 ๊ฒฝ์ž„์ด๋Š” ์–ด๋””์•ผ? ํ™”์žฅ์‹ค 8 1 ๊ฒฝ์ž„์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค. 2 ๊ฒฝ์ž„์ด๋Š” ํ™”์žฅ์‹ค๋กœ ์ด๋™ํ–ˆ์Šต๋‹ˆ๋‹ค. 3 ๊ฒฝ์ž„์ด๋Š” ์–ด๋””์•ผ? ํ™”์žฅ์‹ค 2 4 ํ•„์›…์ด๋Š” ์นจ์‹ค๋กœ ์ด๋™ํ–ˆ์Šต๋‹ˆ๋‹ค. 5 ์ˆ˜์ข…์ด๋Š” ๋ณต๋„๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค. ์˜์–ด Babi ๋ฐ์ดํ„ฐ ์…‹๊ณผ<NAME>์ด ๊ฐ™์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. read_data() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. def read_data(dir): // ์˜์–ด ๋ฐ์ดํ„ฐ ์…‹์— ์‚ฌ์šฉํ–ˆ๋˜ ์ด์ „ ์‹ค์Šต์˜ read_data() ํ•จ์ˆ˜์™€ ๋™์ผํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. read_data() ํ•จ์ˆ˜๋Š” ์˜์–ด ๋ฐ์ดํ„ฐ ์…‹์„ ์‚ฌ์šฉํ•œ ์ด์ „ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ํ•จ์ˆ˜์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. train_data = read_data(TRAIN_FILE) test_data = read_data(TEST_FILE) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋กœ๋“œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์„ฑ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์Šคํ† ๋ฆฌ, ์งˆ๋ฌธ, ๋‹ต๋ณ€์„ ๊ฐ๊ฐ ๋ถ„๋ฆฌํ•ด์„œ ๋กœ๋“œํ•ด ๋ด…์‹œ๋‹ค. train_stories, train_questions, train_answers = read_data(TRAIN_FILE) test_stories, test_questions, test_answers = read_data(TEST_FILE) print('ํ›ˆ๋ จ์šฉ ์Šคํ† ๋ฆฌ์˜ ๊ฐœ์ˆ˜ :', len(train_stories)) print('ํ›ˆ๋ จ์šฉ ์งˆ๋ฌธ์˜ ๊ฐœ์ˆ˜ :',len(train_questions)) print('ํ›ˆ๋ จ์šฉ ๋‹ต๋ณ€์˜ ๊ฐœ์ˆ˜ :',len(train_answers)) print('ํ…Œ์ŠคํŠธ์šฉ ์Šคํ† ๋ฆฌ์˜ ๊ฐœ์ˆ˜ :',len(test_stories)) print('ํ…Œ์ŠคํŠธ์šฉ ์งˆ๋ฌธ์˜ ๊ฐœ์ˆ˜ :',len(test_questions)) print('ํ…Œ์ŠคํŠธ์šฉ ๋‹ต๋ณ€์˜ ๊ฐœ์ˆ˜ :',len(test_answers)) ํ›ˆ๋ จ์šฉ ์Šคํ† ๋ฆฌ์˜ ๊ฐœ์ˆ˜ : 10000 ํ›ˆ๋ จ์šฉ ์งˆ๋ฌธ์˜ ๊ฐœ์ˆ˜ : 10000 ํ›ˆ๋ จ์šฉ ๋‹ต๋ณ€์˜ ๊ฐœ์ˆ˜ : 10000 ํ…Œ์ŠคํŠธ์šฉ ์Šคํ† ๋ฆฌ์˜ ๊ฐœ์ˆ˜ : 1000 ํ…Œ์ŠคํŠธ์šฉ ์งˆ๋ฌธ์˜ ๊ฐœ์ˆ˜ : 1000 ํ…Œ์ŠคํŠธ์šฉ ๋‹ต๋ณ€์˜ ๊ฐœ์ˆ˜ : 1000 ์ž„์˜๋กœ 3573๋ฒˆ์งธ ์Šคํ† ๋ฆฌ, ์งˆ๋ฌธ, ๋‹ต๋ณ€์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. train_stories[3572] ['์€๊ฒฝ์ด๋Š” ๋ถ€์—Œ์œผ๋กœ ๊ฐ€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค.', 'ํ•„์›…์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค.', '์ˆ˜์ข…์ด๋Š” ๋ณต๋„๋กœ ๋›ฐ์–ด๊ฐ”์Šต๋‹ˆ๋‹ค.', '์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๋ณต๊ท€ํ–ˆ์Šต๋‹ˆ๋‹ค.', '๊ฒฝ์ž„์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ์ด๋™ํ–ˆ์Šต๋‹ˆ๋‹ค.', '๊ฒฝ์ž„์ด๋Š” ์นจ์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.'] train_questions[3572] ์€๊ฒฝ์ด๋Š” ์–ด๋””์•ผ? train_answers[3572] ์‚ฌ๋ฌด์‹ค ์ด์ œ ํ† ํฐํ™” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ , ์ด๋กœ๋ถ€ํ„ฐ Vocabulary๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ์•„๋ž˜์˜ ํ•จ์ˆ˜๋Š” ์ด์ „ ์ฑ•ํ„ฐ์—์„œ ์˜์–ด ๋ฐ์ดํ„ฐ ์…‹์— ์‚ฌ์šฉํ–ˆ๋˜ ํ† ํฐํ™” ํ•จ์ˆ˜์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” ํ•œ๊ตญ์–ด์ด๋ฏ€๋กœ ์•„๋ž˜์˜ ํ† ํฐํ™” ํ•จ์ˆ˜๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ฐ”๋žŒ์งํ•˜์ง€๋Š” ์•Š์ง€๋งŒ, ์ž„์‹œ๋กœ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ด์ ˆ ๋‹จ์œ„๋กœ ํ–ˆ์„ ๋•Œ ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•ด ๋ณด๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. def tokenize(sent): return [ x.strip() for x in re.split('(\W+)?', sent) if x.strip()] ์ด์ œ Vocabulary๋ฅผ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. def preprocess_data(train_data, test_data): // ์˜์–ด ๋ฐ์ดํ„ฐ ์…‹์— ์‚ฌ์šฉํ–ˆ๋˜ ์ด์ „ ์ฑ•ํ„ฐ์˜ preprocess_data ํ•จ์ˆ˜์™€ ๋™์ผํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ word2idx, idx2word, story_max_len, question_max_len๋ฅผ ๋ฆฌํ„ด ๋ฐ›๊ฒ ์Šต๋‹ˆ๋‹ค. word2idx, idx2word, story_max_len, question_max_len = preprocess_data(train_data, test_data) word2idx๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(word2idx) {'.': 1, '๊ฒฝ์ž„์ด๋Š”': 2, '์€๊ฒฝ์ด๋Š”': 3, '์ˆ˜์ข…์ด๋Š”': 4, 'ํ•„์›…์ด๋Š”': 5, '์ด๋™ํ–ˆ์Šต๋‹ˆ๋‹ค': 6, '๊ฐ€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค': 7, '๋›ฐ์–ด๊ฐ”์Šต๋‹ˆ๋‹ค': 8, '๋ณต๊ท€ํ–ˆ์Šต๋‹ˆ๋‹ค': 9, '๊ฐ”์Šต๋‹ˆ๋‹ค': 10, 'ํ™”์žฅ์‹ค๋กœ': 11, '์ •์›์œผ๋กœ': 12, '๋ณต๋„๋กœ': 13, '์–ด๋””์•ผ': 14, '?': 15, '๋ถ€์—Œ์œผ๋กœ': 16, '์‚ฌ๋ฌด์‹ค๋กœ': 17, '์นจ์‹ค๋กœ': 18, 'ํ™”์žฅ์‹ค': 19, '์ •์›': 20, '์‚ฌ๋ฌด์‹ค': 21, '์นจ์‹ค': 22, '๋ณต๋„': 23, '๋ถ€์—Œ': 24} ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„, ๋‹ค์‹œ ๋งํ•ด ์–ด์ ˆ ๋‹จ์œ„๋กœ ํ–ˆ์„ ๋•Œ ๋‚˜์˜ค๋Š” ์ด ํ† ํฐ์˜ ์ˆ˜๋Š” 24๊ฐœ์ž…๋‹ˆ๋‹ค. 19๋ฒˆ ํ† ํฐ๋ถ€ํ„ฐ 24๋ฒˆ ํ† ํฐ๊นŒ์ง€๋ฅผ ๋ดค์„ ๋•Œ ์žฅ์†Œ์— ํ•ด๋‹น๋˜๋Š” ๋ช…์‚ฌ๋“ค์€ 'ํ™”์žฅ์‹ค', '์ •์›', '์‚ฌ๋ฌด์‹ค', '์นจ์‹ค', '๋ณต๋„', '๋ถ€์—Œ'์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, 11๋ฒˆ ํ† ํฐ๋ถ€ํ„ฐ 19๋ฒˆ ํ† ํฐ ์‚ฌ์ด์— ๋“ฑ์žฅํ•˜๋Š” 'ํ™”์žฅ์‹ค๋กœ', '์ •์›์œผ๋กœ', '๋ณต๋„๋กœ', '๋ถ€์—Œ์œผ๋กœ', '์‚ฌ๋ฌด์‹ค๋กœ', '์นจ์‹ค๋กœ'๋กœ ๋ถ„๋ฆฌ๋œ ํ† ํฐ๋“ค์€ ํ˜•ํƒœ์†Œ ๋ถ„์„์„ ํ•˜์˜€์„ ๋•Œ ์ „๋ถ€ 'ํ™”์žฅ์‹ค', '์ •์›', '์‚ฌ๋ฌด์‹ค', '์นจ์‹ค', '๋ณต๋„', '๋ถ€์—Œ'์œผ๋กœ ๋ถ„๋ฆฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์€ 2๋ฒˆ ํ† ํฐ๋ถ€ํ„ฐ 5๋ฒˆ ํ† ํฐ์„ ์ฐธ๊ณ ํ•˜์˜€์„ ๋•Œ, '๊ฒฝ์ž„์ด๋Š”', '์€๊ฒฝ์ด๋Š”', '์ˆ˜์ข…์ด๋Š”', 'ํ•„์›…์ด๋Š”' ์ด๋ ‡๊ฒŒ 4๊ฐœ์˜ ํ† ํฐ์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ ์–ด๋„ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ํ•˜์˜€์„ ๋•Œ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์ด ์ œ๋Œ€๋กœ ๋ถ„๋ฆฌ๋˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, '์€๊ฒฝ์ด๋Š”'์„ ํ˜•ํƒœ์†Œ ๋ถ„์„ํ•˜์˜€์„ ๋•Œ '์€', '๊ฒฝ์ด', '๋Š”'๊ณผ ๊ฐ™์ด ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์ด ๋ถ„๋ฆฌ๋ผ์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ์—ฌ๊ธฐ์„œ ํ•ด์•ผ ํ•  ์ผ์€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•ด์„œ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์ด์— ๋Œ€ํ•œ ์กฐ์น˜๋ฅผ ์ทจํ•ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Twitter()๋ฅผ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. twitter = Twitter() ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ์‚ฌ๋žŒ ์ด๋ฆ„, ์žฅ์†Œ, ๋™์‚ฌ๊ฐ€ ์ตœ์†Œํ•œ ํ•œ ๋ฒˆ์”ฉ ๋“ฑ์žฅํ•˜๋„๋ก ๋‹ค์Œ์˜ ์—ฌ์„ฏ ๋ฌธ์žฅ์„ ๋งŒ๋“ค์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์‚ฌ๋žŒ ์ด๋ฆ„์€ 4๊ฐœ, ๋™์‚ฌ๊ฐ€ 5๊ฐœ, ์žฅ์†Œ๊ฐ€ 6๊ฐœ๋กœ ์žฅ์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ œ์ผ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์ด ์—ฌ์„ฏ ๋ฌธ์žฅ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ ํ˜•ํƒœ์†Œ ๋ถ„์„ํ•˜์—ฌ ์šฐ๋ฆฌ๊ฐ€ ์˜๋„ํ•˜๋Š” ๋Œ€๋กœ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. print(twitter.morphs('์€๊ฒฝ์ด๋Š” ํ™”์žฅ์‹ค๋กœ ์ด๋™ํ–ˆ์Šต๋‹ˆ๋‹ค.')) print(twitter.morphs('๊ฒฝ์ž„์ด๋Š” ์ •์›์œผ๋กœ ๊ฐ€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค.')) print(twitter.morphs('์ˆ˜์ข…์ด๋Š” ๋ณต๋„๋กœ ๋›ฐ์–ด๊ฐ”์Šต๋‹ˆ๋‹ค.')) print(twitter.morphs('ํ•„์›…์ด๋Š” ๋ถ€์—Œ์œผ๋กœ ๋ณต๊ท€ํ–ˆ์Šต๋‹ˆ๋‹ค.')) print(twitter.morphs('์ˆ˜์ข…์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.')) print(twitter.morphs('์€๊ฒฝ์ด๋Š” ์นจ์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.')) ['์€', '๊ฒฝ์ด', '๋Š”', 'ํ™”์žฅ์‹ค', '๋กœ', '์ด๋™', 'ํ–ˆ์Šต๋‹ˆ๋‹ค', '.'] ['๊ฒฝ', '์ž„', '์ด', '๋Š”', '์ •์›', '์œผ๋กœ', '๊ฐ€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค', '.'] ['์ˆ˜์ข…', '์ด', '๋Š”', '๋ณต๋„', '๋กœ', '๋›ฐ์–ด๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ['ํ•„์›…์ด', '๋Š”', '๋ถ€์—Œ', '์œผ๋กœ', '๋ณต๊ท€', 'ํ–ˆ์Šต๋‹ˆ๋‹ค', '.'] ['์ˆ˜์ข…', '์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ['์€', '๊ฒฝ์ด', '๋Š”', '์นจ์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ํ™”์žฅ์‹ค, ์ •์›, ๋ณต๋„, ๋ถ€์—Œ, ์‚ฌ๋ฌด์‹ค, ์นจ์‹ค์€ ์˜๋„ํ•œ ๋Œ€๋กœ ๋ชจ๋‘ ์กฐ์‚ฌ '๋กœ'์™€ ๋ถ„๋ฆฌ๋˜์–ด ์ œ๋Œ€๋กœ ๋œ ๋ช…์‚ฌ์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ–์ถฅ๋‹ˆ๋‹ค. ๋ฌธ์ œ๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ์œ ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋ถ„๋ฆฌ๋˜๋Š” ๊ฒฝ์šฐ์ธ๋ฐ, 'ํ•„์›…์ด'๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ '--์ด' ํ˜•ํƒœ๋กœ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์‚ฌ์ „์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. twitter.add_dictionary('์€๊ฒฝ์ด', 'Noun') twitter.add_dictionary('๊ฒฝ์ž„์ด', 'Noun') twitter.add_dictionary('์ˆ˜์ข…์ด', 'Noun') ์‚ฌ์ „ ์ถ”๊ฐ€ ํ›„์—๋Š” ์ด๋ฆ„์ด ์ œ๋Œ€๋กœ ๋ถ„๋ฆฌ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(twitter.morphs('์€๊ฒฝ์ด๋Š” ํ™”์žฅ์‹ค๋กœ ์ด๋™ํ–ˆ์Šต๋‹ˆ๋‹ค.')) print(twitter.morphs('๊ฒฝ์ž„์ด๋Š” ์ •์›์œผ๋กœ ๊ฐ€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค.')) print(twitter.morphs('์ˆ˜์ข…์ด๋Š” ๋ณต๋„๋กœ ๋›ฐ์–ด๊ฐ”์Šต๋‹ˆ๋‹ค.')) print(twitter.morphs('ํ•„์›…์ด๋Š” ๋ถ€์—Œ์œผ๋กœ ๋ณต๊ท€ํ–ˆ์Šต๋‹ˆ๋‹ค.')) print(twitter.morphs('์ˆ˜์ข…์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.')) print(twitter.morphs('์€๊ฒฝ์ด๋Š” ์นจ์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.')) ['์€๊ฒฝ์ด', '๋Š”', 'ํ™”์žฅ์‹ค', '๋กœ', '์ด๋™', 'ํ–ˆ์Šต๋‹ˆ๋‹ค', '.'] ['๊ฒฝ์ž„์ด', '๋Š”', '์ •์›', '์œผ๋กœ', '๊ฐ€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค', '.'] ['์ˆ˜์ข…์ด', '๋Š”', '๋ณต๋„', '๋กœ', '๋›ฐ์–ด๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ['ํ•„์›…์ด', '๋Š”', '๋ถ€์—Œ', '์œผ๋กœ', '๋ณต๊ท€', 'ํ–ˆ์Šต๋‹ˆ๋‹ค', '.'] ['์ˆ˜์ข…์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ['์€๊ฒฝ์ด', '๋Š”', '์นจ์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ์ด์ œ ์ด๋ฅผ ์ƒˆ๋กœ์šด ํ† ํฐํ™” ํ•จ์ˆ˜๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. def tokenize(sent): return twitter.morphs(sent) word2idx, idx2word, story_max_len, question_max_len๋ฅผ ๋ฆฌํ„ด ๋ฐ›๊ฒ ์Šต๋‹ˆ๋‹ค. ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ํ† ํฐํ™” ํ•จ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ์ด์ „๋ณด๋‹ค๋Š” ์‹œ๊ฐ„์ด ์ข€ ๋” ์†Œ์š”๋ฉ๋‹ˆ๋‹ค. word2idx, idx2word, story_max_len, question_max_len = preprocess_data(train_data, test_data) ์ƒˆ๋กœ ์ƒ์„ฑ๋œ word2idx๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(word2idx) {'๋Š”': 1, '.': 2, '๋กœ': 3, 'ํ–ˆ์Šต๋‹ˆ๋‹ค': 4, '์œผ๋กœ': 5, '๊ฒฝ์ž„์ด': 6, '์€๊ฒฝ์ด': 7, '์ˆ˜์ข…์ด': 8, 'ํ•„์›…์ด': 9, '์ด๋™': 10, '๊ฐ€๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค': 11, '๋›ฐ์–ด๊ฐ”์Šต๋‹ˆ๋‹ค': 12, '๋ณต๊ท€': 13, 'ํ™”์žฅ์‹ค': 14, '์ •์›': 15, '๋ณต๋„': 16, '๊ฐ”์Šต๋‹ˆ๋‹ค': 17, '์‚ฌ๋ฌด์‹ค': 18, '๋ถ€์—Œ': 19, '์นจ์‹ค': 20, '์–ด๋””': 21, '์•ผ': 22, '?': 23} ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. vocab_size = len(word2idx) + 1 print(vocab_size) 24 ์Šคํ† ๋ฆฌ์™€ ์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print('์Šคํ† ๋ฆฌ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',story_max_len) print('์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',question_max_len) ์Šคํ† ๋ฆฌ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 70 ์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 5 def vectorize(data, word2idx, story_maxlen, question_maxlen): // ์˜์–ด ๋ฐ์ดํ„ฐ ์…‹์— ์‚ฌ์šฉํ–ˆ๋˜ ์ด์ „ ์ฑ•ํ„ฐ์˜ vectorize ํ•จ์ˆ˜์™€ ๋™์ผํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. Xstrain, Xqtrain, Ytrain = vectorize(train_data, word2idx, story_max_len, question_max_len) Xstest, Xqtest, Ytest = vectorize(test_data, word2idx, story_max_len, question_max_len) print(Xstrain.shape, Xqtrain.shape, Ytrain.shape, Xstest.shape, Xqtest.shape, Ytest.shape) (10000, 70) (10000, 5) (10000, 24) (1000, 70) (1000, 5) (1000, 24) 3. ๋ฉ”๋ชจ๋ฆฌ ๋„คํŠธ์›Œํฌ๋กœ QA ํƒœ์Šคํฌ ํ’€๊ธฐ from tensorflow.keras.models import Sequential, Model from tensorflow.keras.layers import Embedding from tensorflow.keras.layers import Permute, dot, add, concatenate from tensorflow.keras.layers import LSTM, Dense, Dropout, Input, Activation # ์—ํฌํฌ ํšŸ์ˆ˜ train_epochs = 120 # ๋ฐฐ์น˜ ํฌ๊ธฐ batch_size = 32 # ์ž„๋ฒ ๋”ฉ ํฌ๊ธฐ embed_size = 50 # LSTM์˜ ํฌ๊ธฐ lstm_size = 64 # ๊ณผ์ ํ•ฉ ๋ฐฉ์ง€ ๊ธฐ๋ฒ•์ธ ๋“œ๋กญ์•„์›ƒ ์ ์šฉ ๋น„์œจ dropout_rate = 0.30 # ํ”Œ๋ ˆ์ด์Šค ํ™€๋”. ์ž…๋ ฅ์„ ๋‹ด๋Š” ๋ณ€์ˆ˜ input_sequence = Input((story_max_len,)) question = Input((question_max_len,)) print('Stories :', input_sequence) print('Question:', question) Stories : Tensor("input_1:0", shape=(None, 70), dtype=float32) Question: Tensor("input_2:0", shape=(None, 5), dtype=float32) # ์Šคํ† ๋ฆฌ๋ฅผ ์œ„ํ•œ ์ฒซ ๋ฒˆ์งธ ์ž„๋ฒ ๋”ฉ. ๊ทธ๋ฆผ์—์„œ์˜ Embedding A input_encoder_m = Sequential() input_encoder_m.add(Embedding(input_dim=vocab_size, output_dim=embed_size)) input_encoder_m.add(Dropout(dropout_rate)) # ๊ฒฐ๊ณผ : (samples, story_max_len, embedding_dim) / ์ƒ˜ํ”Œ์˜ ์ˆ˜, ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์› # ์Šคํ† ๋ฆฌ๋ฅผ ์œ„ํ•œ ๋‘ ๋ฒˆ์งธ ์ž„๋ฒ ๋”ฉ. ๊ทธ๋ฆผ์—์„œ์˜ Embedding C # ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ question_max_len(์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด)๋กœ ํ•œ๋‹ค. input_encoder_c = Sequential() input_encoder_c.add(Embedding(input_dim=vocab_size, output_dim=question_max_len)) input_encoder_c.add(Dropout(dropout_rate)) # ๊ฒฐ๊ณผ : (samples, story_max_len, question_max_len) / ์ƒ˜ํ”Œ์˜ ์ˆ˜, ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด, ์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด(์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›) # ์งˆ๋ฌธ์„ ์œ„ํ•œ ์ž„๋ฒ ๋”ฉ. ๊ทธ๋ฆผ์—์„œ์˜ Embedding B question_encoder = Sequential() question_encoder.add(Embedding(input_dim=vocab_size, output_dim=embed_size, input_length=question_max_len)) question_encoder.add(Dropout(dropout_rate)) # ๊ฒฐ๊ณผ : (samples, question_max_len, embedding_dim) / ์ƒ˜ํ”Œ์˜ ์ˆ˜, ์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์› # ์‹ค์งˆ์ ์ธ ์ž„๋ฒ ๋”ฉ ๊ณผ์ • input_encoded_m = input_encoder_m(input_sequence) input_encoded_c = input_encoder_c(input_sequence) question_encoded = question_encoder(question) print('Input encoded m', input_encoded_m) print('Input encoded c', input_encoded_c) print('Question encoded', question_encoded) Input encoded m Tensor("sequential/Identity:0", shape=(None, 70, 50), dtype=float32) Input encoded c Tensor("sequential_1/Identity:0", shape=(None, 70, 5), dtype=float32) Question encoded Tensor("sequential_2/Identity:0", shape=(None, 5, 50), dtype=float32) # ์Šคํ† ๋ฆฌ ๋‹จ์–ด๋“ค๊ณผ ์งˆ๋ฌธ ๋‹จ์–ด๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ • # ์œ ์‚ฌ๋„๋Š” ๋‚ด์ ์„ ์‚ฌ์šฉํ•œ๋‹ค. match = dot([input_encoded_m, question_encoded], axes=-1, normalize=False) match = Activation('softmax')(match) print('Match shape', match) # ๊ฒฐ๊ณผ : (samples, story_maxlen, question_max_len) / ์ƒ˜ํ”Œ์˜ ์ˆ˜, ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด, ์งˆ๋ฌธ์˜ ์ตœ๋Œ€ ๊ธธ์ด response = add([match, input_encoded_c]) # (samples, story_max_len, question_max_len) response = Permute((2, 1))(response) # (samples, question_max_len, story_max_len) print('Response shape', response) answer = concatenate([response, question_encoded]) print('Answer shape', answer) answer = LSTM(lstm_size)(answer) answer = Dropout(dropout_rate)(answer) answer = Dense(vocab_size)(answer) answer = Activation('softmax')(answer) Match shape Tensor("activation/Identity:0", shape=(None, 70, 5), dtype=float32) Response shape Tensor("permute/Identity:0", shape=(None, 5, 70), dtype=float32) Answer shape Tensor("concatenate/Identity:0", shape=(None, 5, 120), dtype=float32) model = Model([input_sequence, question], answer) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc']) history = model.fit([Xstrain, Xqtrain], Ytrain, batch_size, train_epochs, validation_data=([Xstest, Xqtest], Ytest)) model.save('model.h5') Epoch 1/120 313/313 [==============================] - 4s 13ms/step - loss: 1.8982 - acc: 0.1693 - val_loss: 1.7868 - val_acc: 0.2470 ... ์ค‘๋žต ... Epoch 120/120 313/313 [==============================] - 3s 10ms/step - loss: 0.0397 - acc: 0.9883 - val_loss: 0.5235 - val_acc: 0.9060 plt.subplot(211) plt.title("Accuracy") plt.plot(history.history["acc"], color="g", label="train") plt.plot(history.history["val_acc"], color="b", label="validation") plt.legend(loc="best") plt.subplot(212) plt.title("Loss") plt.plot(history.history["loss"], color="g", label="train") plt.plot(history.history["val_loss"], color="b", label="validation") plt.legend(loc="best") plt.tight_layout() plt.show() ytest = np.argmax(Ytest, axis=1) Ytest_ = model.predict([Xstest, Xqtest]) ytest_ = np.argmax(Ytest_, axis=1) NUM_DISPLAY = 30 print("{:18}|{:5}|{}".format("์งˆ๋ฌธ", "์‹ค์ œ ๊ฐ’", "์˜ˆ์ธก๊ฐ’")) print(39 * "-") for i in range(NUM_DISPLAY): question = " ".join([idx2word[x] for x in Xqtest[i].tolist()]) label = idx2word[ytest[i]] prediction = idx2word[ytest_[i]] print("{:20}: {:7} {}".format(question, label, prediction)) ์งˆ๋ฌธ |์‹ค์ œ ๊ฐ’ |์˜ˆ์ธก๊ฐ’ --------------------------------------- ์€๊ฒฝ์ด๋Š” ์–ด๋”” ์•ผ? : ๋ณต๋„ ๋ณต๋„ ํ•„์›…์ด ๋Š” ์–ด๋”” ์•ผ? : ํ™”์žฅ์‹ค ํ™”์žฅ์‹ค ๊ฒฝ์ž„์ด๋Š” ์–ด๋”” ์•ผ? : ๋ถ€์—Œ ๋ถ€์—Œ ๊ฒฝ์ž„์ด๋Š” ์–ด๋”” ์•ผ? : ๋ณต๋„ ๋ณต๋„ ๊ฒฝ์ž„์ด๋Š” ์–ด๋”” ์•ผ? : ๋ถ€์—Œ ๋ถ€์—Œ ๊ฒฝ์ž„์ด๋Š” ์–ด๋”” ์•ผ? : ๋ณต๋„ ๋ณต๋„ ๊ฒฝ์ž„์ด๋Š” ์–ด๋”” ์•ผ? : ์ •์› ์ •์› ์ˆ˜์ข…์ด๋Š” ์–ด๋”” ์•ผ? : ๋ณต๋„ ๋ณต๋„ ๊ฒฝ์ž„์ด๋Š” ์–ด๋”” ์•ผ? : ์‚ฌ๋ฌด์‹ค ๋ณต๋„ ์ˆ˜์ข…์ด๋Š” ์–ด๋”” ์•ผ? : ์‚ฌ๋ฌด์‹ค ๋ณต๋„ ํ•„์›…์ด ๋Š” ์–ด๋”” ์•ผ? : ๋ถ€์—Œ ๋ถ€์—Œ ํ•„์›…์ด ๋Š” ์–ด๋”” ์•ผ? : ์ •์› ์ •์› ์ˆ˜์ข…์ด๋Š” ์–ด๋”” ์•ผ? : ์‚ฌ๋ฌด์‹ค ์‚ฌ๋ฌด์‹ค ํ•„์›…์ด ๋Š” ์–ด๋”” ์•ผ? : ์นจ์‹ค ์นจ์‹ค ํ•„์›…์ด ๋Š” ์–ด๋”” ์•ผ? : ์นจ์‹ค ์นจ์‹ค ์€๊ฒฝ์ด๋Š” ์–ด๋”” ์•ผ? : ๋ถ€์—Œ ๋ถ€์—Œ ์€๊ฒฝ์ด๋Š” ์–ด๋”” ์•ผ? : ์ •์› ์ •์› ์€๊ฒฝ์ด๋Š” ์–ด๋”” ์•ผ? : ๋ถ€์—Œ ๋ถ€์—Œ ์ˆ˜์ข…์ด๋Š” ์–ด๋”” ์•ผ? : ์‚ฌ๋ฌด์‹ค ๋ถ€์—Œ ์€๊ฒฝ์ด๋Š” ์–ด๋”” ์•ผ? : ๋ถ€์—Œ ์ •์› ํ•„์›…์ด ๋Š” ์–ด๋”” ์•ผ? : ๋ณต๋„ ๋ณต๋„ ์€๊ฒฝ์ด๋Š” ์–ด๋”” ์•ผ? : ์‚ฌ๋ฌด์‹ค ์‚ฌ๋ฌด์‹ค ์€๊ฒฝ์ด๋Š” ์–ด๋”” ์•ผ? : ์‚ฌ๋ฌด์‹ค ์‚ฌ๋ฌด์‹ค ๊ฒฝ์ž„์ด๋Š” ์–ด๋”” ์•ผ? : ๋ณต๋„ ์‚ฌ๋ฌด์‹ค ์ˆ˜์ข…์ด๋Š” ์–ด๋”” ์•ผ? : ์นจ์‹ค ์นจ์‹ค ๊ฒฝ์ž„์ด๋Š” ์–ด๋”” ์•ผ? : ์นจ์‹ค ์นจ์‹ค ํ•„์›…์ด ๋Š” ์–ด๋”” ์•ผ? : ์นจ์‹ค ์นจ์‹ค ์ˆ˜์ข…์ด๋Š” ์–ด๋”” ์•ผ? : ๋ถ€์—Œ ๋ถ€์—Œ ์ˆ˜์ข…์ด๋Š” ์–ด๋”” ์•ผ? : ๋ถ€์—Œ ๋ถ€์—Œ ์ˆ˜์ข…์ด๋Š” ์–ด๋”” ์•ผ? : ๋ถ€์—Œ ๋ณต๋„ 22. GPT(Generative Pre-trained Transformer) ์•„์ง ์ž‘์„ฑ ์ค‘์ธ ์ฑ•ํ„ฐ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ํŽ˜์ด์ง€๋Š” ํ˜„์žฌ E-book์„ ๊ตฌ๋งคํ•˜์…”๋„ ์—†์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ถ”ํ›„ ๊ณต๊ฐœ ์‹œ ๊ธฐ์กด E-book ๊ตฌ๋งค์ž๋ถ„๋“ค์—๊ฒŒ๋งŒ ๋ฉ”์ผ๋กœ ๋ณ„๋„ ์ œ๊ณต๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์‹ค์Šต ์ฝ”๋“œ๋Š” ์ด๋ฏธ ์™„์„ฑ๋˜์–ด ๊นƒํ—ˆ๋ธŒ(github)์— ์—…๋กœ๋“œ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 22-01 ์ง€ํ”ผํ‹ฐ(Generative Pre-trained Transformer, GPT) ํ˜„์žฌ ์ž‘์„ฑ ์™„๋ฃŒ๋˜์—ˆ์œผ๋‚˜ ๊ฒ€์ˆ˜ ์ค‘์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ํŽ˜์ด์ง€๋Š” ํ˜„์žฌ E-book์„ ๊ตฌ๋งคํ•˜์…”๋„ ์—†์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋‹จ, ์ถ”ํ›„ ๊ณต๊ฐœ ์‹œ ์ด๋ก ๊ณผ ์‹ค์Šต์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๊ธฐ์กด E-book ๊ตฌ๋งค์ž๋ถ„๋“ค์—๊ฒŒ๋งŒ ๋ฉ”์ผ๋กœ ๋ณ„๋„ ์ œ๊ณต๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์‹ค์Šต ์ฝ”๋“œ๋Š” ์ด๋ฏธ ์™„์„ฑ๋˜์–ด ๊นƒํ—ˆ๋ธŒ(github)์— ์—…๋กœ๋“œ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 22-02 KoGPT๋ฅผ ์ด์šฉํ•œ ๋ฌธ์žฅ ์ƒ์„ฑ ํ˜„์žฌ ์ž‘์„ฑ ์™„๋ฃŒ๋˜์—ˆ์œผ๋‚˜ ๊ฒ€์ˆ˜ ์ค‘์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ํŽ˜์ด์ง€๋Š” ํ˜„์žฌ E-book์„ ๊ตฌ๋งคํ•˜์…”๋„ ์—†์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋‹จ, ์ถ”ํ›„ ๊ณต๊ฐœ ์‹œ ์ด๋ก ๊ณผ ์‹ค์Šต์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๊ธฐ์กด E-book ๊ตฌ๋งค์ž๋ถ„๋“ค์—๊ฒŒ๋งŒ ๋ฉ”์ผ๋กœ ๋ณ„๋„ ์ œ๊ณต๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์‹ค์Šต ์ฝ”๋“œ๋Š” ์ด๋ฏธ ์™„์„ฑ๋˜์–ด ๊นƒํ—ˆ๋ธŒ(github)์— ์—…๋กœ๋“œ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  GPT ์‹ค์Šต์€ Colab์—์„œ ์ง„ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต๋œ ํ•œ๊ตญ์–ด GPT-2๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค์Œ ๋ฌธ์žฅ ์˜ˆ์ธก์„ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์„ ์œ„ํ•ด์„œ๋งŒ์ด ์•„๋‹ˆ๋ผ ์•ž์œผ๋กœ ์‚ฌ์ „ ํ•™์Šต๋œ GPT๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” transformers๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‹ค์Šต ํ™˜๊ฒฝ์— transformers ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด๋‘ก์‹œ๋‹ค. pip install transformers 1. KoGPT-2๋กœ ๋ฌธ์žฅ ์ƒ์„ฑํ•˜๊ธฐ transformers ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. BERT์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ GPT๋Š” ์ด๋ฏธ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ํ•™์Šตํ•ด๋‘” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ๊ณผ ํ† ํฌ ๋‚˜์ด์ €๋Š” ํ•ญ์ƒ ๋งคํ•‘ ๊ด€๊ณ„์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. import numpy as np import random import tensorflow as tf from transformers import AutoTokenizer from transformers import TFGPT2LMHeadModel TFGPT2LMHeadModel.from_pretrained('GPT ๋ชจ๋ธ ์ด๋ฆ„')์„ ๋„ฃ์œผ๋ฉด ๋‘ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์ด์–ด์ง€๋Š” ๋ฌธ์žฅ ๊ด€๊ณ„์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” GPT ๊ตฌ์กฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. AutoTokenizer.from_pretrained('๋ชจ๋ธ ์ด๋ฆ„')์„ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น ๋ชจ๋ธ์ด ํ•™์Šต๋˜์—ˆ์„ ๋‹น์‹œ์— ์‚ฌ์šฉ๋˜์—ˆ๋˜ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. model = TFGPT2LMHeadModel.from_pretrained('skt/kogpt2-base-v2', from_pt=True) tokenizer = AutoTokenizer.from_pretrained('skt/kogpt2-base-v2') GPT๊ฐ€ ์ƒ์„ฑํ•  ๋ฌธ์žฅ์˜ ๋ฐฉํ–ฅ์„ฑ์„ ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด์„œ ์‹œ์ž‘ ๋ฌธ์ž์—ด์„ ์ •ํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” '๊ทผ์œก์ด ์ปค์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š”'์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ฃผ๊ณ  GPT์—๊ฒŒ ์ด์–ด์„œ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•ด ๋ณด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. sent = '๊ทผ์œก์ด ์ปค์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š”' GPT์˜ ์ž…๋ ฅ์œผ๋กœ๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๊ฐ€ ์ž…๋ ฅ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ tokenizer.encode()๋ฅผ ํ†ตํ•ด์„œ '๊ทผ์œก์ด ์ปค์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š”'์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ด ์ค๋‹ˆ๋‹ค. input_ids = tokenizer.encode(sent) input_ids = tf.convert_to_tensor([input_ids]) print(input_ids) tf.Tensor([[33245 10114 12748 11357]], shape=(1, 4), dtype=int32) 33245 10114 12748 11357๋ผ๋Š” 5๊ฐœ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ GPT์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ GPT๊ฐ€ ์ด์–ด์„œ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๋„๋ก ํ•ด๋ด…์‹œ๋‹ค. ์ฃผ์–ด์ง„ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์ด์–ด์„œ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์€ model.generate()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. output = model.generate(input_ids, max_length=128, repetition_penalty=2.0, use_cache=True) output_ids = output.numpy().tolist()[0] print(output_ids) [33245, 10114, 12748, 11357, 23879, 39306, 9684, 7884, 10211, 15177, 26421, 387, 17339, 7889, 9908, 15768, 6903, 15386, 8146, 12923, 9228, 18651, 42600, 9564, 17764, 9033, 9199, 14441, 7335, 8704, 12557, 32030, 9510, 18595, 9025, 10571, 25741, 10599, 13229, 9508, 7965, 8425, 33102, 9122, 21240, 9801, 32106, 13579, 12442, 13235, 19430, 8022, 12972, 9566, 11178, 9554, 24873, 7198, 9391, 12486, 8711, 9346, 7071, 36736, 9693, 12006, 9038, 10279, 36122, 9960, 8405, 10826, 18988, 25998, 9292, 7671, 9465, 7489, 9277, 10137, 9677, 9248, 9912, 12834, 11488, 13417, 7407, 8428, 8137, 9430, 14222, 11356, 10061, 9885, 19265, 9377, 20305, 7991, 9178, 9648, 9133, 10021, 10138, 30315, 21833, 9362, 9301, 9685, 11584, 9447, 42129, 10124, 7532, 17932, 47123, 37544, 9355, 15632, 9124, 10536, 13530, 12204, 9184, 36152, 9673, 9788, 9029, 11764] ๊ธฐ์กด์˜ 33245 10114 12748 11357๋ผ๋Š” 5๊ฐœ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค ๋’ค์—๋„ ์—ฌ๋Ÿฌ ์ •์ˆ˜๋“ค์ด ์ถ”๊ฐ€๋กœ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •์ˆ˜๋“ค์ด ๋‹จ์ˆœํžˆ ๋‚˜์—ด๋œ ๊ฒƒ๋งŒ์œผ๋กœ๋Š” GPT๊ฐ€ ์‹ค์ œ๋กœ ์–ด๋–ค ๋ฌธ์žฅ์„ ์ƒ์„ฑํ–ˆ๋Š”์ง€ ์•Œ๊ธฐ ์–ด๋ ค์šฐ๋‹ˆ ํ•ด๋‹น ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ•œ๊ตญ์–ด๋กœ ๋ณ€ํ™˜ํ•ด ๋ด…์‹œ๋‹ค. ์ด ๊ณผ์ •์€ tokenizer.decode()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. tokenizer.decode(output_ids) '๊ทผ์œก์ด ์ปค์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฌด์—‡๋ณด๋‹ค ๊ทœ์น™์ ์ธ ์ƒํ™œ์Šต๊ด€์ด ์ค‘์š”ํ•˜๋‹ค. ํŠนํžˆ, ์•„์นจ์‹์‚ฌ๋Š” ๋‹จ๋ฐฑ์งˆ๊ณผ ๋น„ํƒ€๋ฏผ์ด ํ’๋ถ€ํ•œ ๊ณผ์ผ๊ณผ ์ฑ„์†Œ๋ฅผ ๋งŽ์ด ์„ญ์ทจํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๋˜ํ•œ ํ•˜๋ฃจ 30๋ถ„ ์ด์ƒ ์ถฉ๋ถ„ํ•œ ์ˆ˜๋ฉด์„ ์ทจํ•˜๋Š” ๊ฒƒ๋„ ๋„์›€์ด ๋œ๋‹ค. ์•„์นจ ์‹์‚ฌ๋ฅผ ๊ฑฐ๋ฅด์ง€ ์•Š๊ณ  ๊ทœ์น™์ ์œผ๋กœ ์šด๋™์„ ํ•˜๋ฉด ํ˜ˆ์•ก์ˆœํ™˜์— ๋„์›€์„ ์ค„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‹ ์ง„๋Œ€์‚ฌ๋ฅผ ์ด‰์ง„ํ•ด ์ฒด๋‚ด ๋…ธํ๋ฌผ์„ ๋ฐฐ์ถœํ•˜๊ณ  ํ˜ˆ์••์„ ๋‚ฎ์ถฐ์ค€๋‹ค. ์šด๋™์€ ํ•˜๋ฃจ์— 10๋ถ„ ์ •๋„๋งŒ ํ•˜๋Š” ๊ฒŒ ์ข‹์œผ๋ฉฐ ์šด๋™ ํ›„์—๋Š” ๋ฐ˜๋“œ์‹œ ์ŠคํŠธ๋ ˆ์นญ์„ ํ†ตํ•ด ๊ทผ์œก๋Ÿ‰์„ ๋Š˜๋ฆฌ๊ณ  ์œ ์—ฐ์„ฑ์„ ๋†’์—ฌ์•ผ ํ•œ๋‹ค. ์šด๋™ ํ›„ ๋ฐ”๋กœ ์ž ์ž๋ฆฌ์— ๋“œ๋Š” ๊ฒƒ์€ ํ”ผํ•ด์•ผ ํ•˜๋ฉฐ ํŠนํžˆ ์•„์นจ์— ์ผ์–ด๋‚˜๋ฉด ๋ชธ์ด ํ”ผ๊ณคํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด๋ฆฌํ•˜๊ฒŒ ์›€์ง์ด๋ฉด ์˜คํžˆ๋ ค ์—ญํšจ๊ณผ๊ฐ€ ๋‚  ์ˆ˜๋„ ์žˆ๋‹ค. ์šด๋™์„' '๊ทผ์œก์ด ์ปค์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š”'๋ผ๋Š” ๋ฌธ์ž์—ด์— ์ด์–ด์„œ ๊ทธ ๋’ค์— ๊ทผ์œก์ด ์ปค์ง€๊ธฐ ์œ„ํ•œ์ด๋ผ๋Š” ๋ฌธ๋งฅ์— ๋งž๋Š”๋“ฏํ•œ ๊ธ€๋“ค์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  GPT๊ฐ€ ์ƒ์„ฑํ•œ ๋ฌธ์žฅ๋“ค์€ ๋ฌธ๋งฅ์ƒ ๊ทธ๋Ÿด๋“ฏํ•ด ๋ณด์ด์ง€๋งŒ ์‹ค์ œ ์‚ฌ์‹ค ์—ฌ๋ถ€์™€๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ด ์ ์€ ๋Š˜ ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 2. Numpy๋กœ Top 5 ๋ฝ‘๊ธฐ GPT๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ(Language Model)์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์‹ค์Šต์—์„œ ํ™•์ธํ•œ ๋ฐ”์™€ ๊ฐ™์ด '๊ทผ์œก์ด ์ปค์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š”'์ด๋ผ๋Š” ์ž…๋ ฅ์„ ๋„ฃ์—ˆ์„ ๋•Œ GPT๋Š” ๋‹ค์Œ ๋‹จ์–ด๋กœ '๋ฌด์—‡๋ณด๋‹ค'๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ–ˆ์—ˆ๋Š”๋ฐ์š”. ์‹ค์ œ๋กœ๋Š” ์ˆ˜๋งŽ์€ ํ›„๋ณด์˜ ๋‹ค์Œ ๋‹จ์–ด๋“ค์ด ์žˆ์—ˆ์ง€๋งŒ, ๊ทธ์ค‘ ๊ฐ€์žฅ ํ™•๋ฅ ์ด ๋†’์€ ๋‹จ์–ด. ์ฆ‰, Top 1์˜ ๋‹จ์–ด์ธ '๋ฌด์—‡๋ณด๋‹ค'๋ฅผ ์˜ˆ์ธกํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋‹ค์Œ ๋‹จ์–ด๋กœ ๋˜ ์–ด๋–ค ํ›„๋ณด๋“ค์ด ์žˆ์—ˆ๋Š”์ง€ Top 5์˜ ๋‹จ์–ด๋“ค์„ ๋ฝ‘์•„๋ด…์‹œ๋‹ค. model()์—๋‹ค๊ฐ€ '๊ทผ์œก์ด ์ปค์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š”'์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์€ ํ›„ ๊ฐ€์žฅ ํ™•๋ฅ ์ด ๋†’์€ Top 5์˜ ๋‹จ์–ด๋“ค์„ ๋ฝ‘์•„๋ƒ…๋‹ˆ๋‹ค. output = model(input_ids) top5 = tf.math.top_k(output.logits[0, -1], k=5) ๊ทธ ํ›„ Top 5์˜ ๋‹จ์–ด๋ฅผ ํ•œ๊ตญ์–ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. tokenizer.convert_ids_to_tokens(top5.indices.numpy()) ['โ–๋ฌด์—‡๋ณด๋‹ค', 'โ–์šฐ์„ ', 'โ–๋ฐ˜๋“œ์‹œ', 'โ–ํ”ผ๋ถ€', 'โ–๋ฌด์—‡๋ณด๋‹ค๋„'] '๋ฌด์—‡๋ณด๋‹ค'๋ผ๋Š” ๋‹จ์–ด ์™ธ์—๋„ '์šฐ์„ ', '๋ฐ˜๋“œ์‹œ', 'ํ”ผ๋ถ€', '๋ฌด์—‡๋ณด๋‹ค๋„'๋ผ๋Š” 4๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ๋†’์€ ํ™•๋ฅ ๋กœ ์„ ํƒ๋  ์ˆ˜ ์žˆ์—ˆ์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 3. Numpy Top 5๋กœ ๋ฌธ์žฅ ์ƒ์„ฑํ•˜๊ธฐ ์•ž์„œ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ–ˆ์„ ๋‹น์‹œ์—๋Š” ๊ฐ ์‹œ์ (time step)๋งˆ๋‹ค ๊ฐ€์žฅ ํ™•๋ฅ ์ด ๋†’์€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ–ˆ์ง€๋งŒ, ์ด๋ฒˆ์—๋Š” ๋งค ์‹œ์ ๋งˆ๋‹ค Top 5๊ฐœ์˜ ๋‹จ์–ด๋“ค ์ค‘์—์„œ ๋žœ๋ค์œผ๋กœ ์„ ํƒํ•˜๋Š” ๋ฐฉ์‹์„ ํƒํ•˜์—ฌ ๋ฌธ์žฅ์„ ์ƒ์„ฑํ•ด ๋ด…์‹œ๋‹ค. sent = '๊ทผ์œก์ด ์ปค์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š”' input_ids = tokenizer.encode(sent) while len(input_ids) < 50: output = model(np.array([input_ids])) # Top 5์˜ ๋‹จ์–ด๋“ค์„ ์ถ”์ถœ top5 = tf.math.top_k(output.logits[0, -1], k=5) # Top 5์˜ ๋‹จ์–ด๋“ค ์ค‘ ๋žœ๋ค์œผ๋กœ ๋‹ค์Œ ๋‹จ์–ด๋กœ ์„ ํƒ. token_id = random.choice(top5.indices.numpy()) input_ids.append(token_id) tokenizer.decode(input_ids) '๊ทผ์œก์ด ์ปค์ง€๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฌด์—‡๋ณด๋‹ค ๊ท ํ˜• ์žˆ๋Š” ์‹์‚ฌ๋Ÿ‰์ด ํ•„์š”ํ•˜๋‹ค. ๋˜, ๊ทœ์น™์ ์ธ ์šด๋™์„ ํ†ตํ•œ ์˜์–‘์†Œ ์„ญ์ทจ์™€ ๊ทœ์น™์ ์ธ ์šด๋™ ๋“ฑ์„ ํ†ตํ•ด์„œ ์ฒด์ง€๋ฐฉ์„ ์ค„์—ฌ๋‚˜๊ฐ€๋Š” ๊ฒŒ ์ค‘์š”ํ•˜๋‹ค. ์ตœ์†Œ ํ•œ ๋‹ฌ์— 1ํšŒ ์ด์ƒ ์šด๋™์„ ํ•˜๊ณ  ์ฒด์ค‘์ด ๊ฐ์†Œํ•˜๋ฉด ๊ทธ ํšจ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, ๊ทธ ํšจ๊ณผ๊ฐ€ ๋ฐ˜๊ฐ๋˜๋Š”' 22-03 KoGPT-2 ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ์ด์šฉํ•œ ํ•œ๊ตญ์–ด ์ฑ—๋ด‡ ํ˜„์žฌ ์ž‘์„ฑ ์™„๋ฃŒ๋˜์—ˆ์œผ๋‚˜ ๊ฒ€์ˆ˜ ์ค‘์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ํŽ˜์ด์ง€๋Š” ํ˜„์žฌ E-book์„ ๊ตฌ๋งคํ•˜์…”๋„ ์—†์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋‹จ, ์ถ”ํ›„ ๊ณต๊ฐœ ์‹œ ์ด๋ก ๊ณผ ์‹ค์Šต์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๊ธฐ์กด E-book ๊ตฌ๋งค์ž๋ถ„๋“ค์—๊ฒŒ๋งŒ ๋ฉ”์ผ๋กœ ๋ณ„๋„ ์ œ๊ณต๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์‹ค์Šต ์ฝ”๋“œ๋Š” ์ด๋ฏธ ์™„์„ฑ๋˜์–ด ๊นƒํ—ˆ๋ธŒ(github)์— ์—…๋กœ๋“œ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 22-04 KoGPT-2๋ฅผ ์ด์šฉํ•œ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ ํ˜„์žฌ ์ž‘์„ฑ ์™„๋ฃŒ๋˜์—ˆ์œผ๋‚˜ ๊ฒ€์ˆ˜ ์ค‘์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ํŽ˜์ด์ง€๋Š” ํ˜„์žฌ E-book์„ ๊ตฌ๋งคํ•˜์…”๋„ ์—†์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋‹จ, ์ถ”ํ›„ ๊ณต๊ฐœ ์‹œ ์ด๋ก ๊ณผ ์‹ค์Šต์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๊ธฐ์กด E-book ๊ตฌ๋งค์ž๋ถ„๋“ค์—๊ฒŒ๋งŒ ๋ฉ”์ผ๋กœ ๋ณ„๋„ ์ œ๊ณต๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์‹ค์Šต ์ฝ”๋“œ๋Š” ์ด๋ฏธ ์™„์„ฑ๋˜์–ด ๊นƒํ—ˆ๋ธŒ(github)์— ์—…๋กœ๋“œ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 22-05 KoGPT-2๋ฅผ ์ด์šฉํ•œ KorNLI ๋ถ„๋ฅ˜ ํ˜„์žฌ ์ž‘์„ฑ ์™„๋ฃŒ๋˜์—ˆ์œผ๋‚˜ ๊ฒ€์ˆ˜ ์ค‘์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ํŽ˜์ด์ง€๋Š” ํ˜„์žฌ E-book์„ ๊ตฌ๋งคํ•˜์…”๋„ ์—†์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋‹จ, ์ถ”ํ›„ ๊ณต๊ฐœ ์‹œ ์ด๋ก ๊ณผ ์‹ค์Šต์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๊ธฐ์กด E-book ๊ตฌ๋งค์ž๋ถ„๋“ค์—๊ฒŒ๋งŒ ๋ฉ”์ผ๋กœ ๋ณ„๋„ ์ œ๊ณต๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์‹ค์Šต ์ฝ”๋“œ๋Š” ์ด๋ฏธ ์™„์„ฑ๋˜์–ด ๊นƒํ—ˆ๋ธŒ(github)์— ์—…๋กœ๋“œ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 23. ๊ต์œก ๋ฌธ์˜ ์•ˆ๋…•ํ•˜์„ธ์š”. ์ €์ž<NAME>์ž…๋‹ˆ๋‹ค. ์ด ์ฑ…์— ์ž‘์„ฑ๋œ ๊ต์œก ์ฃผ์ œ์™€ ๊ด€๋ จ๋œ ๊ณผ์™ธ ๋˜๋Š” ๊ต์œก์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ํ…์ŠคํŠธ ๋ถ„์„, ๋”ฅ ๋Ÿฌ๋‹ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ, PLM ๋“ฑ๊ณผ ๊ด€๋ จ๋œ ๊ต์œก์œผ๋กœ ๊ธฐ์—… ๊ต์œก ๋ฐ ๋Œ€ํ•™๊ต, ๋Œ€ํ•™์› ํŠน๊ฐ• ๋“ฑ์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ์ƒ์„ธ ๋ฌธ์˜ ๋ฐ ์ปค๋ฆฌํ˜๋Ÿผ์€ <EMAIL>์œผ๋กœ ๋ถ€๋‹ด ์—†์ด ํŽธํ•˜๊ฒŒ ์—ฐ๋ฝ ์ฃผ์‹œ๋ฉด ๋‹ต๋ณ€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ์™•์ดˆ๋ณด๋ฅผ ์œ„ํ•œ Python: ์‰ฝ๊ฒŒ ํ’€์–ด ์“ด ๊ธฐ์ดˆ ๋ฌธ๋ฒ•๊ณผ ์‹ค์Šต ### ๋ณธ๋ฌธ: ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•œ ๋ฒˆ๋„ ํ•ด๋ณธ ์ ์ด ์—†๋Š” ๋ถ„๋“ค์„ ์œ„ํ•ด, ํŒŒ์ด์ฌ์„ ํ†ตํ•ด ์ฒ˜์Œ์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์‹œ์ž‘ํ•˜๋„๋ก ๋„์™€๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๋ชฉ์ฐจ 0 ๋จธ๋ฆฌ๋ง 1 ํŒŒ์ด์ฌ ์‹œ์ž‘ํ•˜๊ธฐ 2 ์ œ์–ด ๊ตฌ์กฐ 3 ํ•จ์ˆ˜ 4 ๋ฐ์ดํ„ฐ ํƒ€์ž… 5 ๋ชจ๋“ˆ 6 ํŒŒ์ผ 7 ๊ฐ์ฒด์ง€ํ–ฅ 8 ์˜ˆ์™ธ 9 ํ…Œ์ŠคํŒ…๊ณผ ์„ฑ๋Šฅ A ๋ถ€๋ก ๊ถ๊ธˆํ•œ ์ ์€ ๋Œ“๊ธ€์„ ๋‚จ๊ฒจ ์ฃผ์‹œ๊ณ , ์ฑ—๋ด‡์—๊ฒŒ๋„ ๋ฌผ์–ด๋ณด์„ธ์š”. ํŒŒ์ด์ฌ ์•Œ๋ ค์ฃผ๋Š” ๋ด‡ 2(GPT-3.5 Turbo) 0. ๋จธ๋ฆฌ๋ง ์˜ˆ์ œ ์ฝ”๋“œ ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๊นƒํ—ˆ๋ธŒ(Github)์— ์žˆ์Šต๋‹ˆ๋‹ค. https://github.com/ychoi-kr/wikidocs-chobo-python 2020๋…„ ์ œ๊ฐ€ ํŒŒ์ด์ฌ์„ ์ฒ˜์Œ ์ ‘ํ•˜๊ณ  ์ด ์ฑ…์„ ์“ฐ๊ธฐ ์‹œ์ž‘ํ•œ ์ง€ 20๋…„์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ๋ฐฐ์šฐ๊ณ  ์‚ฌ์šฉํ•˜๋˜ ์ค‘ ํŒŒ์ด์ฌ์„ ์•Œ๊ฒŒ ๋˜๋ฉด์„œ ๊ทธ ๊ฐ•๋ ฅํ•จ๊ณผ ๊ฐ„๊ฒฐํ•จ์— ๋งค๋ฃŒ๋˜์—ˆ๊ณ , ํŠนํžˆ ๊ต์œก์šฉ ์–ธ์–ด๋กœ์„œ์˜ ๊ฐ€๋Šฅ์„ฑ์— ์ฃผ๋ชฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ดˆ๋“ฑํ•™๊ต ๋•Œ BASIC ์–ธ์–ด๋กœ ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ฒ˜์Œ ๋ฐฐ์› ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ, ์ดˆ๋“ฑํ•™์ƒ๋„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ๊ต์žฌ๋ฅผ ์จ ๋ณด์ž๊ณ  ์ƒ๊ฐํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ ์ง€๊ธˆ ๋ณด๊ณ  ๊ณ„์‹  '์™•์ดˆ๋ณด๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ'์˜ ์‹œ์ž‘์ž…๋‹ˆ๋‹ค. ์ฒ˜์Œ์— ํŒŒ์ด์ฌ 2.1 ๋ฒ„์ „์„ ๊ฐ€์ง€๊ณ  ๊ธ€์„ ์จ์„œ ๊ฐœ์ธ ํ™ˆํŽ˜์ด์ง€์— ์˜ฌ๋ฆฌ๊ธฐ ์‹œ์ž‘ํ–ˆ๊ณ , ์ด๋“ฌํ•ด ๊ฐ™์€ ์ œ๋ชฉ์˜ ์ฑ…์„ ์ถœ๊ฐ„ํ•˜๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์œ„ํ‚ค๋…์Šค๋กœ ์ด์‚ฌ๋ฅผ ์™€์„œ ํŒŒ์ด์ฌ 2.7 ๋ฒ„์ „์„ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…์„ ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ 2.7์€ ํŒŒ์ด์ฌ 2์˜ ๋งˆ์ง€๋ง‰ ๋ฒ„์ „์ด๋ฉด์„œ ํŒŒ์ด์ฌ 3์˜ ๊ตฌ๋ฌธ์„ ์ง€์›ํ•˜๋ฏ€๋กœ, ๋‹น์‹œ๋กœ์„œ๋Š” ํŒŒ์ด์ฌ์„ ํ†ตํ•ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์ฒ˜์Œ ๋ฐฐ์šฐ๋Š” ์‚ฌ๋žŒ์„ ์œ„ํ•œ ๊ต์žฌ๋กœ์„œ ์ตœ์„ ์˜ ์„ ํƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ์ฑ…์—์„œ๋Š” ๊ทธ๋™์•ˆ ํŒŒ์ด์ฌ 2.7์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋˜ ํŒŒ์ด์ฌ 3์˜ ์„ค๋ช…๋„ ๋ง๋ถ™์ด๋Š” ๋ฐฉ์‹์œผ๋กœ ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ ๋ช‡ ๋…„ ์‚ฌ์ด ๋ฐ์ดํ„ฐ ๋ถ„์„๊ณผ ์ธ๊ณต ์ง€๋Šฅ ๋ถ๊ณผ ํ•จ๊ป˜ ํŒŒ์ด์ฌ์— ๋Œ€ํ•œ ๊ด€์‹ฌ์ด ํฌ๊ฒŒ ๋Š˜์—ˆ๊ณ , ์–ด๋ฆฐ์ด๋‚˜ IT ๋ถ„์•ผ์™€ ์ง์ ‘ ๊ด€๋ จ ์—†๋Š” ์ผ๋ฐ˜์ธ์„ ๋Œ€์ƒ์œผ๋กœ ํ•˜๋Š” ์ฝ”๋”ฉ ๊ต์œก์˜ ํ•„์š”์„ฑ์— ๋Œ€ํ•œ ์ธ์‹๋„ ํ™•์‚ฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๋•Œ๋ฌธ์ธ์ง€, ์ง€๊ธˆ๋„ ์ด ์ฑ…์„ ์ฝ๋Š” ๋ถ„์ด ์—ฌ์ „ํžˆ ๋งŽ์ด ๊ณ„์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ํŒŒ์ด์ฌ 3.8๋„ ๋‚˜์™€ ์žˆ๊ณ , ์•„์ง ํŒŒ์ด์ฌ 2๊ฐ€ ๋“ค์–ด ์žˆ๋Š” ์‹œ์Šคํ…œ์ด ์žˆ๊ธฐ๋Š” ํ•˜์ง€๋งŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ต์œก ๊ด€์ ์—์„œ๋Š” ํŒŒ์ด์ฌ 2๋ฅผ ํฌ๊ฒŒ ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ, ์ข€ ๋Šฆ์—ˆ์ง€๋งŒ ํŒŒ์ด์ฌ 3๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…ํ•˜๋˜ ํŒŒ์ด์ฌ 2๋„ ์–ธ๊ธ‰ํ•˜๋Š”<NAME>์œผ๋กœ ๊ณ ์ณ๋‚˜๊ฐ€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „์—๋„ ํŒŒ์ด์ฌ 3๋ฅผ ๋‹ค๋ค˜์œผ๋ฏ€๋กœ ๋‚ด์šฉ์ด ํฌ๊ฒŒ ๋ฐ”๋€Œ์ง€๋Š” ์•Š์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•˜๋Š” ๋™์•ˆ์—๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์˜ ์„ค๋ช…์ด ์„ž์—ฌ ๋ถˆํŽธํ•˜์‹œ๋”๋ผ๋„ ๋„ˆ๊ทธ๋Ÿฝ๊ฒŒ ์–‘ํ•ด ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์šฉ์–ด ๋ณ€๊ฒฝ ๋ฐ ์„ค๋ช… ์ˆ˜์ • ๋„๋ฆฌ ์“ฐ์ด๋Š” ์šฉ์–ด๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ชฉ๋ก โ†’ ๋ฆฌ์ŠคํŠธ ์‚ฌ์ „ โ†’ ๋”•์…”๋„ˆ๋ฆฌ ๋˜ํ•œ, ์„ค๋ช…๊ณผ ์˜ˆ์ œ๋ฅผ ์‰ฝ๊ฒŒ ์œ ์ง€ํ•˜๋ฉด์„œ๋„ ์ข€ ๋” ์ •ํ™•ํ•œ ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ƒˆ๋กœ์šด ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.(์ฃผ์š” ๋ณ€๊ฒฝ ์ด๋ ฅ ์ฐธ์กฐ) ์œ ํŠœ๋ธŒ ์˜์ƒ ์—…๋กœ๋“œ ์ œ ์œ ํŠœ๋ธŒ ์ฑ„๋„(http://youtube.com/c/sk8erchoi)์— ์˜์ƒ์„ ์ƒˆ๋กœ ์˜ฌ๋ ธ์Šต๋‹ˆ๋‹ค. ๊ฐ ํŽ˜์ด์ง€์— ๊ด€๋ จ ์˜์ƒ์„ ๋งํฌํ–ˆ๊ณ , ํŒŒ์ด์ฌ ์žฌ์ƒ ๋ชฉ๋ก์—์„œ๋„ ๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์ž์˜ ๋‹ค๋ฅธ ์ฑ… ์ด ์ฑ… ์™ธ์—๋„ ์ œ๊ฐ€ ์ง‘ํ•„ยท๋ฒˆ์—ญํ•œ ์ฑ…์ด ์žˆ์œผ๋‹ˆ ๊ด€์‹ฌ์„ ๊ฐ€์ ธ์ฃผ์‹œ๋ฉด ๊ณ ๋ง™๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์ฑ…: https://wikidocs.net/profile/info/book/4 ์ง‘ํ•„/๋ฒˆ์—ญํ•œ ์ฑ…(yes24 ๋ฆฌ์ŠคํŠธ): http://list.yes24.com/bloglist/listList.aspx?blogid=sk8erchoi&listseqno=10608554 2013๋…„ 2013๋…„ ํ˜„์žฌ Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋Š” 2.X ๋ฒ„์ „๊ณผ 3.X ๋ฒ„์ „์ด ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์œผ๋ฉฐ, ์•ž์œผ๋กœ๋Š” 3.X ๋ฒ„์ „์ด ์ฃผ๋กœ ์“ฐ์ด๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๋Š” ๋ถ„์—๊ฒŒ๋Š” Python 3์„ ๋ฐฐ์šฐ๊ธฐ๋ฅผ ๊ถŒํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ ๋ณด๊ณ  ๊ณ„์‹œ๋Š” '์™•์ดˆ๋ณด๋ฅผ ์œ„ํ•œ Python 2.7'์€ Python 2๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„์ง ํ•™๊ต๋‚˜ ํšŒ์‚ฌ์—์„œ๋Š” Python 2.X ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ํ•„์š”ํ•œ ๋ถ„๋“ค์„ ์œ„ํ•ด ์œ„ํ‚ค๋…์Šค๋ฅผ ํ†ตํ•ด ๊ณต๊ฐœํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์†Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. http://wikidocs.net/book/2 ์ด ๊ธ€์„ ์ „์ž์ฑ…์œผ๋กœ ์ฝ๊ณ  ๊ณ„์‹œ๋Š” ๋ถ„์ด๋ผ๋ฉด ์œ„ํ‚ค๋…์Šค์—์„œ ๊ตฌ๋งคํ•˜์…จ์œผ๋ฆฌ๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋ถ€์กฑํ•œ ์ฑ…์„ ๊ตฌ์ž…ํ•ด ์ฃผ์…”์„œ ๊ณ ๋ง™์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ์ด ์ฑ…์„ ์ฃผ๋ณ€ ๋ถ„๋“ค์—๊ฒŒ<NAME>๊ณ  ์‹ถ์€ ๋งˆ์Œ์ด ๋“œ์‹ ๋‹ค๋ฉด, ํŒŒ์ผ์„ ์ง์ ‘ ๋ณต์‚ฌํ•ด ์ฃผ์‹œ๊ธฐ๋ณด๋‹ค๋Š” ์ „์ž์ฑ…์„ ๊ตฌ์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ์ฃผ์†Œ๋ฅผ ์•Œ๋ ค์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ €์ž์—๊ฒŒ๋„ ๋„์›€์ด ๋˜๊ณ , ์ข‹์€ ์˜จ๋ผ์ธ ๋ถ์„ ๋ฌด๋ฃŒ๋กœ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ์œ„ํ‚ค๋…์Šค์˜ ์šด์˜์„ ๋„์™€์ฃผ์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2013๋…„ ์—ฌ๋ฆ„, ์—ฌ๋Ÿฌ๋ถ„๋ณด๋‹ค ํ•œ ๊ฑธ์Œ ์•ž์„œ๊ฐ”์„ ๋ฟ์ธ ์™•์ดˆ๋ณด ์ตœ์šฉ ์˜ฌ๋ฆผ 0.1 ์ฃผ์š” ๋ณ€๊ฒฝ ์ด๋ ฅ 2023. 8. ์—ฐ์Šต ๋ฌธ์ œ: ์ง์„ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ ์ถ”๊ฐ€ 2023. 6. ์—ฐ์Šต ๋ฌธ์ œ: ๋๋ง์ž‡๊ธฐ (1), (2), (3) ์ถ”๊ฐ€ 2023. 5. ์—ฐ์Šต ๋ฌธ์ œ: ์›์ฃผ์œจ ๊ตฌํ•˜๊ธฐ ์ถ”๊ฐ€ ์—ฐ์Šต ๋ฌธ์ œ: ํ•œ์ž ์„ฑ์–ด ์ถ”๊ฐ€ 2023. 4. ๋ฉ”์„œ๋“œ ์ƒ์†๊ณผ ์žฌ์ •์˜ ์ถ”๊ฐ€ ์—ฐ์Šต ๋ฌธ์ œ: ๋‚ด์ผ์˜ ๋‚ ์งœ ๊ตฌํ•˜๊ธฐ(2) ์ถ”๊ฐ€ 2023. 3. ์—ฐ์Šต ๋ฌธ์ œ: ์ž๋ฆฟ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ ์ถ”๊ฐ€ ์—ฐ์Šต ๋ฌธ์ œ: ์Œ์„ฑ ์ธ์‹ ์ผ๋ณธ์–ด ํ€ด์ฆˆ ๊ฐœ์„  ์ถ”๊ฐ€ 2023. 2. for-else์™€ while-else ์ถ”๊ฐ€ ์—ฐ์Šต ๋ฌธ์ œ: ๋‚˜์ด์— ๋”ฐ๋ฅธ ์„ธ๋Œ€ ๊ตฌ๋ถ„ (1), (2) ์ถ”๊ฐ€ ์—ฐ์Šต ๋ฌธ์ œ: ๋ฐด๋“œ ์ด๋ฆ„ ์ง“๊ธฐ (1), (2) ์ถ”๊ฐ€ 2022. 11. ์‹œ์ €(์นด์ด์‚ฌ๋ฅด) ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ ์ถ”๊ฐ€ ๋ณธ๋ฌธ๊ณผ ๊ทธ๋ฆผ์„ ์ „์ž์ฑ… PDF ํฌ๋งท์— ์ตœ์ ํ™” ์ „์ž์ฑ… ๊ฐ€๊ฒฉ ์ธ์ƒ(3000์› โ†’ 4000์›) 2022. 10. ํŒŒ์ด์ฌ ์„ค์น˜์™€ ์‹คํ–‰์— ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ ์Šคํ† ์–ด ์†Œ๊ฐœ ์ถ”๊ฐ€ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— โ€˜ํŒŒ์ด์ฌ ์‹คํ–‰ ํŒŒ์ผ ๊ฒฝ๋กœ ํ™•์ธโ€™ ์ถ”๊ฐ€ string๊ณผ random ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ ์ถ”๊ฐ€ 2022. 9. and/or ์—ฐ์‚ฐ์ž ์ถ”๊ฐ€ match-case ๋ฌธ ์ถ”๊ฐ€ (Python 3.10 ๋Œ€์‘) 2022. 8. 7.5. ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ๋“ค ์ˆ˜์ •(__cmp__ ์‚ญ์ œ, __lt__ ์ถ”๊ฐ€) ํ€ด์ฆˆ: ์‚ฌ์น™ ์—ฐ์‚ฐ ์ถ”๊ฐ€ 2022. 5. ์ผํšŒ์šฉ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋ฐ”๊พธ๊ธฐ ์ถ”๊ฐ€ ๋†€์ด๊ณต์› ์—ฐ์Šต ๋ฌธ์ œ (1), (2), (3) ์ถ”๊ฐ€ 2022. 3. ์—ฐ์Šต ๋ฌธ์ œ: ๋‹จ์œ„ ๊ธฐํ˜ธ ์ถ”๊ฐ€ 2022. 2. ์—ฐ์Šต ๋ฌธ์ œ: ํ”„๋ž™ํ„ธ (Rule 90) ์ถ”๊ฐ€ ์˜ˆ์ œ ๊นƒํ—ˆ๋ธŒ ์ €์žฅ์†Œ ์ฃผ์†Œ ๋ณ€๊ฒฝ: https://github.com/ychoi-kr/wikidocs-chobo-python 2021. 7. ์‘์šฉ ์˜ˆ์ œ: ์Œ์„ฑ ์ธ์‹์„ ํ™œ์šฉํ•œ ์ผ๋ณธ์–ด ํ€ด์ฆˆ ์ถ”๊ฐ€ 2021. 6. 1์žฅ์˜ ์ ˆ ์ˆœ์„œ ๋ณ€๊ฒฝ ๊ฐ•์˜ ์˜์ƒ ์ฃผ์†Œ QR ์ฝ”๋“œ ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์‹œ๊ฐ„ ์ธก์ •ํ•˜๊ธฐ ์ถ”๊ฐ€ ์ผ๋Ÿฌ์ŠคํŠธ ์ถ”๊ฐ€(Zdenek Sasek) 2021. 5. ์˜์–ด ํ€ด์ฆˆ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ 2021. 4. ํŒŒ์ผ ํฌ๊ธฐ ๊ณ„์‚ฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์œˆ๋„ CMD์—์„œ ํŒŒ์ด์ฌ ํ™œ์šฉ ํŒ ์ถ”๊ฐ€ ๋ถ€๋ก์œผ๋กœ ์ด๋™ ํ•จ์ˆ˜์˜ ์žฌ๊ท€ ๋ณธ๋ฌธ๊ณผ ์—ฐ์Šต ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ: ์—ฌ๋Ÿฌ ๋Œ€์˜ ์ปดํ“จํ„ฐ์— ์—ฐ์‚ฐ์„ ๋ถ„๋ฐฐํ•˜๊ธฐ ํŒŒ์ด์ฌ์œผ๋กœ PDF ํŒŒ์ผ ํ•ฉ์น˜๊ธฐ matplotlib์œผ๋กœ ํ•˜ํŠธ ๊ทธ๋ฆฌ๊ธฐ ๋ชจ๋“ˆ ์‚ฌ์šฉ๋ฒ• ์•Œ์•„๋‚ด๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ˆซ์ž ์ฝ๊ธฐ(์•„๋ผ๋น„์•„ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ํ•œ๊ธ€์„ ์ถœ๋ ฅ) ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์กฐ๊ฑด๋ฌธ ํ•จ์ˆ˜ ๋”•์…”๋„ˆ๋ฆฌ ํ…Œ์ŠคํŒ… ์„ธํŠธ๋ฅผ ๋ณ„๋„ ํŽ˜์ด์ง€๋กœ ๋ถ„๋ฆฌ ์ฃผ์‚ฌ์œ„ ๋ˆˆ์˜ ํ•ฉ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ 2021. 3. ์• ๊ตญ๊ฐ€ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ •์‹  ์งˆํ™˜ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ํšŒ๋ฌธ ํŒ๋ณ„ ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ง๊ฐ์‚ผ๊ฐํ˜•์˜ ๋น—๋ณ€ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ํ•จ์ˆ˜ ์ •์˜ํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ง„๋ฒ• ๋ณ€ํ™˜๊ณผ ๋น„ํŠธ ์—ฐ์‚ฐ ์ถ”๊ฐ€ ์ง„๋ฒ• ๋ณ€ํ™˜ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ 2์žฅ์—์„œ 4์žฅ์œผ๋กœ ์ด๋™ 2021. 2. ๋‚ด์ผ์˜ ๋‚ ์งœ ๊ตฌํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์‹คํ–‰ ๊ฒฐ๊ณผ ๋งžํžˆ๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ์˜ ๋‘ ๋ฒˆ์งธ ํ’€์ด ์ถ”๊ฐ€ calendar์™€ tkinter ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ๋น„๋ฐ€ ๋ฉ”์‹œ์ง€ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ 2021. 1. ๋ณต๋ฆฌ ์ด์ž๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ๋‹จ๋ฆฌ ์ด์ž ๊ณ„์‚ฐ, ๋ณต๋ฆฌ ์ด์ž ๊ณ„์‚ฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ค„๊ธฐ์™€ ์žŽ ๊ทธ๋ฆผ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ 2020. 12. ๊ฐ ์ž๋ฆฌ ์ˆซ์ž์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜(๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉ) ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ํ…Œ์ŠคํŒ… ์ถ”๊ฐ€ ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ธฐ(for) ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ œ๊ณฑํ‘œ(for) ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ํŒŒ์ด์ฌ์œผ๋กœ PDF ํŒŒ์ผ ํ•ฉ์น˜๊ธฐ ์ถ”๊ฐ€ 2020. 11. ์˜ˆ์ œ ๊นƒํ—ˆ๋ธŒ ์ €์žฅ์†Œ ์ƒ์„ฑ ์–‘์ˆ˜๋งŒ ๋ง์…ˆํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ธฐ(while) ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ œ๊ณฑ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ 2020. 10. ํ™”ํ•™ ์‹คํ—˜์‹ค ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ 2020. 9. ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ 2020. 7. ์œค๋…„ ํŒ๋ณ„ํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์ œ๊ณฑํ‘œ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ๊ฐ ์ž๋ฆฌ ์ˆซ์ž์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜ ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ ์–Œ์ฒด๊ณต ์—ฐ์Šต ๋ฌธ์ œ ์ถ”๊ฐ€ 2011. ์œ„ํ‚ค๋…์Šค๋กœ ์ด์ „ 2003. 10. ์นดํŽ˜24๋กœ ์ด์ „ 2001. ๊ฐ•์ขŒ ์‹œ์ž‘(๋„ค๋ ์•™) ์˜›๋‚  ๋ชจ์Šต 2004. 3. 28.(์นดํŽ˜24) 2002. 2. 1.(๋„ค๋ ์•™) 1. ํŒŒ์ด์ฌ ์‹œ์ž‘ํ•˜๊ธฐ ์ด ๊ฐ•์ขŒ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ฒ˜์Œ ํ•˜๋Š” ๋ถ„๋“ค์„ ์œ„ํ•ด์„œ ํŒŒ์ด์ฌ์„ ํ†ตํ•ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ธฐ์ดˆ๋ฅผ ์ฐจ๊ทผ์ฐจ๊ทผ ์ตํž ์ˆ˜ ์žˆ๋„๋ก ์ง„ํ–‰ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ ํ•จ๊ป˜ ํŒŒ์ด์ฌ ํƒํ—˜์„ ๋– ๋‚˜ ๋ณผ๊นŒ์š”~ ์ด ์žฅ์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฒƒ: ํŒŒ์ด์ฌ ๋ง›๋ณด๊ธฐ ๋ณ€์ˆ˜ ๋ฆฌ์ŠคํŠธ(list) ์ธํ„ฐํ”„๋ฆฌํ„ฐ์™€ ์ปดํŒŒ์ผ๋Ÿฌ ํŒŒ์ด์ฌ ์„ค์น˜ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ 1.1 ํŒŒ์ด์ฌ ๋ง›๋ณด๊ธฐ ๊ฐ•์˜ ์˜์ƒ: https://youtu.be/ymSAvh-hntA/ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์žฌ๋ฏธ์žˆ์Šต๋‹ˆ๋‹ค. ์ง„์งœ๋กœ ์žฌ๋ฏธ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์žฌ๋ฏธ๋ฅผ ์•Œ๋ ค๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ง์ ‘ ํ•ด๋ด์•ผ ํ•˜๋Š”๋ฐ, ์‚ฌ์‹ค ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ์ง€๊ฐ€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์—ฌ๋Ÿฌ๋ถ„๊ณผ ํ•จ๊ป˜ ๋ฐฐ์šฐ๊ธฐ ์‰ฌ์šด ํŒŒ์ด์ฌ์ด๋ผ๋Š” ์–ธ์–ด๋ฅผ ํ•จ๊ป˜ ๊ณต๋ถ€ํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ๋ญ๋ƒ๊ณ ์š”? ์‚ฌ๋žŒ์ด ์ปดํ“จํ„ฐ์—๊ฒŒ ์ผ์„ ์‹œํ‚ค๋ ค๋ฉด ์ปดํ“จํ„ฐ๊ฐ€ ์•Œ์•„๋“ฃ๋Š” ๋ง๋กœ ์ผ์„ ์‹œ์ผœ์•ผ๋งŒ ํ•œ๋‹ต๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ๊ฐ€ ์•Œ์•„๋“ฃ๋Š” ๋ง๋กœ ๊ทธ๋Ÿฐ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ปดํ“จํ„ฐ๊ฐ€ ์•Œ์•„๋“ฃ๋Š” ๋ง์„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ผ๊ณ  ํ•˜์ง€์š”. ์šฐ๋ฆฌ๊ฐ€ ์•ž์œผ๋กœ ๋ฐฐ์šธ ํŒŒ์ด์ฌ ์–ธ์–ด๋Š”, ๋ฐฐ์šฐ๊ธฐ ์‰ฌ์šฐ๋ฉด์„œ๋„ ํ”„๋กœ๊ทธ๋žจ์„ ๋นจ๋ฆฌ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๊ณ , ๊ธฐ๋Šฅ๋„ ๋›ฐ์–ด๋‚˜๋‹ต๋‹ˆ๋‹ค. ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•˜๊ธฐ ํŒŒ์ด์ฌ์„ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ ๋„ ์‚ฌ์šฉํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์˜ ์ฒซ ํ™”๋ฉด์— ์žˆ๋Š” ๋…ธ๋ž€์ƒ‰ Launch Interactive Shell ์•„์ด์ฝ˜์„ ํด๋ฆญํ•ด ๋ณด์„ธ์š”. ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ํŒŒ์ด์ฌ ์…ธ์ด ๋œฐ ๊ฑฐ์˜ˆ์š”. ํŒŒ์ด์ฌ ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€: http://python.org ๋˜๋Š”, ํŒŒ์ด์ฌ ํŠœํ„ฐ(http://pythontutor.com/)๋‚˜ ideone(https://ideone.com/) ๊ฐ™์€ ์›น์‚ฌ์ดํŠธ์—์„œ๋„ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•ด ๋ณผ ์ˆ˜ ์žˆ์–ด์š”! (ํŒŒ์ด์ฌ ํŠœํ„ฐ๋Š” 2์žฅ์˜ while ๋ฌธ์„ ์„ค๋ช…ํ•˜๋Š” ์˜์ƒ์—์„œ ์†Œ๊ฐœ) ๋ง์…ˆ ๊ทธ๋Ÿผ ๋ญ๋ถ€ํ„ฐ ํ•ด๋ณผ๊นŒ์š”? ์“ฑ์‹น์“ฑ์‹น(์† ๋น„๋น„๋Š” ์†Œ๋ฆฌ)โ€ฆ ๋”ํ•˜๊ธฐ ๋นผ๊ธฐ๊ฐ€ ์ข‹๊ฒ ๊ตฐ์š”. >>> 1 + 2 ์œ„์™€ ๊ฐ™์ด ์“ฐ๊ณ  Enter ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ๋ณด์„ธ์š”. ๋‹ต์ด ๋‚˜์˜ต๋‹ˆ๊นŒ? ๋ฌด์ง€ ์‰ฝ๊ตฐ์š”. ๋บ„์…ˆ ๊ทธ๋Ÿผ ๋นผ๊ธฐ๋„ ํ•ด๋ณผ๊นŒ์š”? >>> 50 - 4 46 ์—ญ์‹œ ์ƒ๊ฐ๋Œ€๋กœ ์ž˜ ๋˜๋Š”๊ตฐ์š”. ์œˆ๋„ ์ฒจ ๋ฐฐ์šธ ๋•Œ๋ณด๋‹ค๋„ ํ›จ์”ฌ ์‰ฝ๋‹ต๋‹ˆ๋‹ค. ํ•˜ํ•˜ํ•˜. ์ปดํ“จํ„ฐ ์ผœ๊ณ  ๋„๋Š” ๋ฒ•๋ณด๋‹ค ๋” ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๊นŒ??? ๊ณฑ์…ˆ ๊ณฑํ•˜๊ธฐ ๋‚˜๋ˆ„๊ธฐ๊นŒ์ง€๋งŒ ํ•˜๊ณ  ์˜ค๋Š˜์€ ์‰ด๊นŒ์š”? ์›๋ž˜ ์ฒซ ์ˆ˜์—…์€ ๊ฐ€๋ณ๊ฒŒ ์‹œ์ž‘ํ•˜๋Š” ๊ฑฐ๋‹ˆ๊นŒ์š”. ๊ทธ๋Ÿผ ์ด๋ฒˆ์—” ์ข€ ๋” ์–ด๋ ค์šด ์ˆซ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๊ณฑ์…ˆ์„ ์‹œ์ผœ ๋ด…์‹œ๋‹ค. >>> 12345678 * 3 37037034 ๊ณฑํ•˜๊ธฐ๋„ ์„ฑ๊ณต! ์•Œ๊ณ  ๊ณ„์‹œ๊ฒ ์ง€๋งŒ ์ปดํ“จํ„ฐ์—์„œ๋Š” *๊ฐ€ ๊ณฑํ•˜๊ธฐ๋ฅผ ๋œปํ•œ๋‹ต๋‹ˆ๋‹ค. ๋‚˜๋ˆ„๊ธฐ๋Š”์š”? ๋‚˜๋ˆ—์…ˆ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. /๊ฐ€ ๋‚˜๋ˆ„๊ธฐ์ง€์š”. ์ด ์ •๋„๋Š” ๊ธฐ๋ณธ์ด์ง€์š”? ๊ทธ๋Ÿผ ๋‚˜๋ˆ—์…ˆ๋„ ํ•ด๋ณผ๊นŒ์š”? (๋‚˜๋ˆ—์…ˆ์€ ํŒŒ์ด์ฌ 3์™€ ํŒŒ์ด์ฌ 2์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์š”) Python 3: >>> 5000 / 3 1666.6666666666667 >>> 5000 // 3 1666 Python 2: >>> 5000 / 3 1666 ๋‚˜๋จธ์ง€ ๋˜, ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•  ๋• %๋ผ๋Š” ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ, 50์„ 8๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•ด ๋ณผ๊นŒ์š”? >>> 50 % 8 50 8 6 2 , ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ 50์„ 8๋กœ ๋‚˜๋ˆˆ ๋ชซ์€ 6์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” 2๋‹ˆ๊นŒ 50 % 8์˜ ๊ฒฐ๊ณผ๋Š” 2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ›Œ๋ฅญํ•˜๊ตฐ์š”. ๋‚˜๋ˆ—์…ˆ๋„ ๋ฉ‹์ง€๊ฒŒ ํ•ด์น˜์šฐ๋Š” ์šฐ๋ฆฌ์˜ ํŒŒ์ด์ฌ! ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ํ•œ ๋ฒˆ์— ๊ตฌํ•˜๊ธฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด divmod() ํ•จ์ˆ˜๋ฅผ ์จ์„œ ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ํ•œ ๋ฒˆ์— ๊ณ„์‚ฐํ•  ์ˆ˜๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค. >>> divmod(50, 8) (6, 2) ํ•จ์ˆ˜(function)๋Š” ์ด์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๋‹จ๊ณ„์˜ ๊ณ„์‚ฐ์„ ๋ฏธ๋ฆฌ ์ •ํ•ด๋‘” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3์žฅ์—์„œ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๋งŒ๋“œ๋Š” ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์˜ค๋Š˜์˜ ์ˆ˜์—…์€ ์ด๋งŒ ์ค„์ด๊ณ  ์นœ๊ตฌ ๋งŒ๋‚˜๋Ÿฌ ๊ฐ€๋ณผ๊นŒ์š”? tip ์ฑ— GPT๋ฅผ ์—๋ฎฌ๋ ˆ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค! โŸช์ƒ์„ฑ AI ํ™œ์šฉ๊ธฐโŸซ์˜ โŸจ์ฑ— GPT๋ฅผ ์—๋ฎฌ๋ ˆ์ดํ„ฐ๋กœ ํ™œ์šฉโŸฉ ํŽ˜์ด์ง€๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. 1.1.1 ํ€ด์ฆˆ: ์‚ฌ์น™ ์—ฐ์‚ฐ ์ƒ๊ฐํ•ด ๋ณผ ๋งŒํ•œ ๋ฌธ์ œ๊ฐ€ ์žˆ์–ด์„œ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํ•™์—์„œ ๋‹ค์Œ์„ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ๋Š” ์–ผ๋งˆ์ผ๊นŒ์š”? รท ( + ) ๊ทธ๋ ‡๋‹ค๋ฉด, ํŒŒ์ด์ฌ ์…ธ์—์„œ ๋‹ค์Œ ์‹์„ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ๋Š” ์–ผ๋งˆ๊ฐ€ ๋‚˜์˜ฌ๊นŒ์š”? ์‹คํ–‰ํ•ด ๋ณด๊ธฐ ์ „์— ๋ฏธ๋ฆฌ ๋‹ต์„ ์˜ˆ์ƒํ•ด ๋ณด์„ธ์š”. 8 // 2 * (2 + 2) ์ƒ๊ฐํ•œ ๋Œ€๋กœ ๋‹ต์ด ๋‚˜์™”๋‚˜์š”? ์ฐธ๊ณ  [๊นจ Talk] ์ „ ์„ธ๊ณ„ ํŽ˜์ด์Šค๋ถ, ์œ ํŠœ๋ธŒ๋ฅผ ๋œจ๊ฒ๊ฒŒ ๋‹ฌ๊ตฐ ๋…ผ๋ž€ _ 8รท2(2+2) = ? 1.1.2 ์—ฐ์Šต ๋ฌธ์ œ: ์ง์„ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ ๋ฌธ์ œ ๋ฏธ๊ตญ ๋‰ด์š•์˜ ๋งจํ•ดํŠผ(Manhattan)์€ ๋ฐ”๋‘‘ํŒ์ฒ˜๋Ÿผ ๊ตฌํš๋˜์–ด ์žˆ์–ด ์ŠคํŠธ๋ฆฌํŠธ์™€ ์• ๋น„๋‰ด๊ฐ€ ์ง๊ตํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ตฌ๊ธ€ ์ง€๋„์—์„œ ํ•œ์‹๋‹น์—์„œ ๋ณ‘์›๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋Š” 550๋ฏธํ„ฐ, ๋ณ‘์›์—์„œ ํ–„๋ฒ„๊ฑฐ ๊ฐ€๊ฒŒ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋Š” 600๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. ํ•œ์‹๋‹น๊ณผ ํ–„๋ฒ„๊ฑฐ ๊ฐ€๊ฒŒ ์‚ฌ์ด์˜ ์ง์„ ๊ฑฐ๋ฆฌ๋ฅผ ํŒŒ์ด์ฌ ์…ธ์„ ์‚ฌ์šฉํ•ด ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ค‘ํ•™๊ต์—์„œ ๋ฐฐ์šฐ๋Š” ์ˆ˜ํ•™ ์ง€์‹์„ ํ™œ์šฉํ•ด์„œ ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง๊ฐ์‚ผ๊ฐํ˜•์˜ ๋‘ ์ง๊ฐ๋ณ€,๋ฅผ ๊ฐ๊ฐ ํ•œ ๋ณ€์œผ๋กœ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜• ๋ฉด์ ์˜ ํ•ฉ์€ ๋น—๋ณ€(hypotenuse)๋ฅผ ํ•œ ๋ณ€์œผ๋กœ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜•์˜ ๋ฉด์ ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ โ€˜ํ”ผํƒ€๊ณ ๋ผ์Šค ์ •๋ฆฌโ€™๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. (์œ„ํ‚ค๋ฐฑ๊ณผ) 2 b = 2 ์™€์˜ ๊ธธ์ด๋ฅผ ์•Œ๋ฉด์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. = 2 b ์ด๋•Œ ๋ฃจํŠธ ๊ฐ’(์–‘์˜ ์ œ๊ณฑ๊ทผ)์„ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๋Š”๋ฐ, ํŒŒ์ด์ฌ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด math ๋ชจ๋“ˆ์˜ sqrt ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.(๋ชจ๋“ˆ์— ๊ด€ํ•ด์„œ๋Š” 5์žฅ์—์„œ ๋ฐฐ์›๋‹ˆ๋‹ค.) >>> import math >>> math.sqrt(______________) ํ•œ์‹๋‹น๊ณผ ํ–„๋ฒ„๊ฑฐ ๊ฐ€๊ฒŒ ์‚ฌ์ด์˜ ์ง์„ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด ๋นˆ์นธ์— ์–ด๋–ค ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ• ๊นŒ์š”? ํ’€์ด ๋‹ต์„ ์•„์‹œ๋Š” ๋ถ„์€ ๋Œ“๊ธ€์— ์จ์ฃผ์„ธ์š”. ์ฐธ๊ณ ๋กœ, ์ง€๋„์—์„œ ์ง์„ ๊ฑฐ๋ฆฌ๋ฅผ ์ธก์ •ํ•ด ๋ณด๋ฉด ์•ฝ 814๋ฏธํ„ฐ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. tip ์ด ๋ฌธ์ œ์—์„œ ๊ตฌํ•œ ์ง์„ ๊ฑฐ๋ฆฌ๋ฅผ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ(Euclidean distance)๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ง์ ‘ ๊ฑธ์–ด๊ฐ€๋ ค๋ฉด ๊ฑด๋ฌผ์„ ๋šซ๊ณ  ๊ฐˆ ์ˆ˜ ์—†์œผ๋‹ˆ ๊ธธ์„ ๋”ฐ๋ผ 550 + 600 = 1150๋ฏธํ„ฐ๋ฅผ ์ด๋™ํ•ด์•ผ๊ฒ ์ฃ ? ์ด๋ฅผ ๋งจํ•ดํŠผ ๊ฑฐ๋ฆฌ(Manhattan distance)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 1.2 ๋ณ€์ˆ˜ ์ด๋ฒˆ ์‹œ๊ฐ„์— ๊ฐ–๊ณ  ๋†€ ๊ฒƒ์€ ๋ณ€์ˆ˜(variable)์ž…๋‹ˆ๋‹ค. ๋ณ€์ˆ˜๊ฐ€ ๋ฌด์Šจ ๋œป์ผ๊นŒ์š”? 9์‹œ ๋‰ด์Šค์— ๊ฐ€๋” ๋“ฑ์žฅํ•˜๋Š” ๊ฑธ ๋“ค์–ด๋ณด์‹  ๋ถ„์ด๋ผ๋ฉด ๋Œ€์ถฉ ์ง์ž‘์ด ๊ฐ€์‹ค ํ…Œ์ง€์š”. ์ˆ˜ํ•™์—์„œ๋„ ๋‚˜์˜ค๊ณ ์š”. ํ”Œ๋ฐ์—์„œ์˜ ๋ณ€์ˆ˜๋„ ๊ทธ์™€ ๋น„์Šทํ•œ ๋œป์„ ๊ฐ–๊ณ  ์žˆ๋‹ต๋‹ˆ๋‹ค. ์ •์ˆซ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜ ์ž, ์ œ ์ฑ…์ƒ์—๋Š” ์‹œ๊ณ„, ๋ผ์ดํ„ฐ, ์นซ์†”, ํŽœ, ์ผ์ฃผ์ผ์งธ ๋ฌผ ๋งˆ์‹ค ๋•Œ ์“ฐ๊ณ  ์žˆ๋Š” ์ข…์ด์ปต ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ œ ์‹œ๊ณ„๊ฐ€ ์–ผ๋งˆ์ผ๊นŒ์š”? 5์ฒœ ์›? ์•„๋‹™๋‹ˆ๋‹ค. ํžŒํŠธโ€ฆ ์ œ ์‹œ๊ณ„๋Š” ๊ฒฐํ˜ผ ์˜ˆ๋ฌผ์ด๋ž๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ, ์ œ ์‹œ๊ณ„ ๊ฐ’์€ 10๋งŒ ์›์ด๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ œ ์‹œ๊ณ„๋ฅผ ์‚ฌ๊ณ  ์‹ถ์€ ์‚ฌ๋žŒ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ œ ์‹œ๊ณ„ ๊ฐ’์€ ์–ผ๋งˆ์ผ๊นŒ์š”? ์˜ˆ, ๊ทธ๋ ‡์ฃ . ๋ถ€๋ฅด๋Š” ๊ฒŒ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ œ๊ฐ€ ๋ฐฑ๋งŒ ์› ๋‹ฌ๋ผ๊ณ  ํ•˜๋ฉด ๋ฐฑ๋งŒ ์›์งœ๋ฆฌ ์‹œ๊ณ„๊ฐ€ ๋˜๋Š” ๊ฑฐ์ง€์š”. ํ•˜ํ•˜~. ์‹œ๊ณ„ ํ•˜๋‚˜๊ฐ€ 10๋งŒ ์›๋„ ๋˜๊ณ  ๋ฐฑ๋งŒ ์›๋„ ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์‹œ๊ณ„ ๊ฐ’์„ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณผ๊นŒ์š”? >>> watch = 100000 ์ œ ์‹œ๊ณ„๊ฐ€ ์‹ญ๋งŒ ์›์ด๋ผ๋Š” ๊ฒƒ์„ ์ด๋ ‡๊ฒŒ ์จ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ, ๋ผ์ดํ„ฐ๋„ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณผ๊นŒ์š”? >>> lighter = 300 ๋ผ์ดํ„ฐ๋Š” ๊ธธ์—์„œ ๋„์šฐ๋ฏธํ•œํ…Œ ๋ฐ›์€ ๊ฑด๋ฐ 300์›์ด๋ผ๊ณ  ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ˆซ์ž๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ๊ทธ๋Ÿผ, ์ €ํ•œํ…Œ์„œ ์‹œ๊ณ„์™€ ๋ผ์ดํ„ฐ๋ฅผ ์‚ฌ๋ ค๋ฉด ์–ผ๋งˆ๋ฅผ ์ฃผ์…”์•ผ ํ• ๊นŒ์š”? ์‹œ๊ณ„๋ฅผ ์‚ฌ๋ ค๋ฉด ์‹ญ๋งŒ ์›์ด ์•„๋‹ˆ๋ผ ๋ฐฑ๋งŒ ์›์„ ์ฃผ์…”์•ผ ํ•œ๋‹ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์‹œ๊ณ„ ๊ฐ’์„ ์˜ฌ๋ ค์•ผ๊ฒ ์ฃ ? >>> watch = 1000000 ์•„๊นŒ ์‹ญ๋งŒ ์›์ด๋˜ ์‹œ๊ณ„๊ฐ€ ์ด์   ๋ฐฑ๋งŒ ์›์ด ๋˜์—ˆ๊ตฐ์š”. ๊ทธ๋Ÿผ ํ•ฉ์ด ์–ผ๋งˆ์ธ๊ฐ€์š”? ์†๊ฐ€๋ฝ ๊ผฝ์œผ๋ฉด์„œ ๊ณ„์‚ฐํ•˜๋Š” ๋ถ„ ๊ณ„์‹œ์ฃ ? ํ•˜ํ•˜. ๊ทธ๊ฑธ ์ปดํ“จํ„ฐํ•œํ…Œ ์‹œ์ผœ ๋ด…์‹œ๋‹ค. >>> watch + lighter 1000300 ์˜ค์šฐ, ์—ญ์‹œ ๋จธ๋ฆฌ ์ž˜ ๋Œ์•„๊ฐ€๋Š” ์ปดํ“จํ„ฐ๊ฐ€ ์ˆœ์‹๊ฐ„์— ๊ณ„์‚ฐ์„ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณ€์ˆ˜์—๋Š” ๊ฐ’์„ ์—ฌ๋Ÿฌ ๋ฒˆ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊ณ„์‚ฐํ•ด ๊ฐ™์€ ๋ณ€์ˆ˜์— ๋‹ค์‹œ ๋Œ€์ž…ํ•˜๊ธฐ ์‹œ๊ณ„๋ฅผ ์ค‘๊ณ ๋กœ ํŒ”์•„์„œ ๊ฒŒ์ž„๊ธฐ๋ฅผ ์‚ฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฑ๋งŒ ์›์งœ๋ฆฌ ์‹œ๊ณ„๋ฅผ 15% ํ• ์ธํ•œ ๊ฐ€๊ฒฉ์€ ์–ผ๋งˆ์ผ๊นŒ์š”? >>> 1000000 * 0.85 850000.0 (๋ฌผ๋ก  1000000 - 1000000 * 0.15๋ผ๋“ ์ง€ 1000000 * (1 - 0.15)๋กœ ๊ณ„์‚ฐํ•ด๋„ ๋˜๊ฒ ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ์œ„์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค๋ช…ํ• ๊ฒŒ์š”.) ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์–ด๋–ค ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์„ 15% ํ• ์ธํ•˜๋Š” ๊ฒƒ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. >>> price = 5000 >>> price = price * 0.85 >>> price 4250.0 ์œ„์—์„œ ๋ณ€์ˆ˜ = ๋ณ€์ˆ˜ * ๊ฐ’<NAME>์˜ ๋ฌธ์žฅ์€ ๋ณ€์ˆ˜ *= ๊ฐ’์œผ๋กœ ์ค„์—ฌ์„œ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> price = 5000 >>> price *= 0.85 >>> price 4250.0 ๋ฌธ์ž์—ด์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜ ๋ณ€์ˆ˜์—๋Š” ์ˆซ์ž ๋ง๊ณ  ๊ธ€์ž๋„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. >>> a = 'pig' ์š”๋ ‡๊ฒŒ ์“ฐ๋ฉด a๋ผ๋Š” ๋ณ€์ˆ˜์— 'pig'๋ผ๋Š” ๋ฌธ์ž์—ด(๊ธ€์ž ์—ฌ๋Ÿฌ ๊ฐœ)์„ ๋„ฃ์œผ๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ a๋Š” 'pig'์™€ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์ด์ง€์š”. ์—ฌ๊ธฐ์„œ 'pig'์— ๋”ฐ์˜ดํ‘œ๊ฐ€ ๋‘˜๋Ÿฌ์ ธ ์žˆ๋Š” ๊ฒƒ์„ ์ฃผ์˜ํ•ด์„œ ๋ณด์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ์˜ดํ‘œ๊ฐ€ ์—†์œผ๋ฉด pig๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ์ฐฉ๊ฐ์„ ํ•˜๊ฑฐ๋“ ์š”. "pig๋Š” ๋ฌธ์ž์—ด์ด๋‹ค"๋ผ๋Š” ๋œป์œผ๋กœ ๋”ฐ์˜ดํ‘œ๊ฐ€ ์“ฐ์ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—” b๋ผ๋Š” ๋ณ€์ˆ˜์— 'dad'๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด๋ด…์‹œ๋‹ค. >>> b = 'dad' ์ •๋ง ์‰ฝ์ฃ ? ๊ทธ๋Ÿผ ์ด๋ฒˆ์— ๋ฌธ์ž์—ด๋ผ๋ฆฌ ์ด์–ด ๋ถ™์—ฌ ๋ณผ๊นŒ์š”? >>> a + b pigdad ์•„๊นŒ ์‹œ๊ณ„๋ž‘ ๋ผ์ดํ„ฐ ๊ฐ’์„ ๋”ํ•  ๋•Œ๋ž‘ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์ผ๋”๋‹ˆ ๋ฌธ์ž์—ด๋“ค์ด ํ•ฉ์ณ์ง€์ง€์š”? ์žฌ๋ฏธ์žˆ์ง€ ์•Š์Šต๋‹ˆ๊นŒ? ์žฌ๋ฏธ์—†์œผ์‹œ๋‹ค๊ณ ์š”? ๊ทธ๋ ‡๋‹ค๋ฉด ์ข€ ๋” ์žฌ๋ฏธ์žˆ๋Š” ๊ฒƒ์„ ํ•ด๋ด…์‹œ๋‹ค. >>> a + ' ' + b pig dad ์—ฌ๊ธฐ์„  a๋ผ๋Š” ๋ณ€์ˆ˜์™€ ' '(๊ณต๋ฐฑ ๋ฌธ์ž ํ•œ ๊ฐœ)์™€ b๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋ถ™์—ฌ์„œ 'pig dad'๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์–ด ์ค€ ๊ฒƒ์ด์ง€์š”. ์ดํ•ด๋˜์‹œ์ง€์š”? ์˜ค๋Š˜์˜ ๊ฐ•์ขŒ๋Š” ์—ฌ๊ธฐ๊นŒ์ง€์ž…๋‹ˆ๋‹ค. 1.2.1 ์—ฐ์Šต ๋ฌธ์ œ: ํŒŒ์ผ ํฌ๊ธฐ ๊ณ„์‚ฐ ๋ฌธ์ œ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•  ๋•Œ์˜ ํ‰๊ท  ์†๋„(average rate)๋ฅผ ์ด๋ผ ํ•˜๊ณ , ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„(time)์„๋ผ๊ณ  ํ•  ๋•Œ, ๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ์˜ ์šฉ๋Ÿ‰์€ ร—๋กœ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ์†๋„๊ฐ€ ์ดˆ๋‹น 800kB์ด๊ณ  ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์ด 110์ดˆ๋ผ๊ณ  ํ•  ๋•Œ, ๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ์˜ ํฌ๊ธฐ๋Š” ๋ช‡ MB์ผ๊นŒ์š”? ๋‹จ, MB 1000 kB ๋กœ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ch01/download.txt 1.3 ๋ฆฌ์ŠคํŠธ(list) ๊ฐ•์˜ ์˜์ƒ: https://youtu.be/-BA3lbvggCM ์•ˆ๋…•ํ•˜์„ธ์š”~ ์—ฌ๋Ÿฌ๋ถ„~ ์˜ค๋Š˜๋„ ์—ฌ๋Ÿฌ๋ถ„๊ณผ ํ•จ๊ป˜ ํŒŒ์ด์ฌ ๋†€์ดํ•˜๋Ÿฌ ์™”์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜์ด ์ œ๊ฐ€ ์ด ๊ฐ•์ขŒ๋ฅผ ์‹œ์ž‘ํ•œ ์ง€ ์ดํ‹€์งธ ๋˜๋Š” ๋‚ ์ž…๋‹ˆ๋‹ค. ์ž‘์‹ฌ์‚ผ์ผ์ด ๋˜์ง€ ์•Š๋„๋ก ์ €์—๊ฒŒ ํž˜์„ ์ฃผ์„ธ์š”. ์ €๋„ ์—Š๊ทธ์ œ๊นŒ์ง€๋งŒ ํ•ด๋„ ํŒŒ์ด์ฌ์— ๋Œ€ํ•ด ์•„๋ฌด๊ฒƒ๋„ ๋ชฐ๋ž๋‹ต๋‹ˆ๋‹ค. ๋ฌด์‹ํ•˜๋ฉด ์šฉ๊ฐํ•˜๋‹ค๊ณ  ์ €๋„ ๊ณต๋ถ€ํ•ด ๊ฐ€๋ฉด์„œ ์šฉ๊ฐํ•˜๊ฒŒ ๊ธ€์„ ์“ฐ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ^^; ์˜ค๋Š˜์€ ์ €ํฌ ๊ฐ€์กฑ ์ด์•ผ๊ธฐ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ €ํฌ ์‹๊ตฌ๋Š” ๋„ค ๋ช…์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์–ด๋จธ๋‹ˆ, ์•„๋ฒ„์ง€, ์ €, ๋™์ƒ. ๋Œ€์žฅ์€ ์–ด๋จธ๋‹ˆ, ๊ทธ๋‹ค์Œ์ด ์•„๋ฒ„์ง€์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์ €๋Š” ๋„˜๋ฒ„ ์“ฐ๋ฆฌ์˜€๋˜ ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹คโ€ฆ ์œผํํโ€ฆ ํŒŒ์ด์ฌ์—์„œ๋Š” ์ €ํฌ ๊ฐ€์กฑ์„ ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. >>> family = ['mother', 'father', 'gentleman', 'sexy lady'] ์ €ํฌ ๊ฐ€์กฑ์—๋Š” ์•„๋ฒ„์ง€, ์–ด๋จธ๋‹ˆ, ์ €, ๋™์ƒ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ฆฌ์ŠคํŠธ(list)๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด์ง€์š”. ์ดํ•ด๊ฐ€ ๊ฐ‘๋‹ˆ๊นŒ? ์˜ต๋‹ˆ๊นŒ? ์˜ˆ, ๊ทธ๋ƒฅ ๋™์ƒ์ด๋ผ๊ณ  ํ–ˆ๋Š”๋ฐ ์‹ค์€ ์—ฌ๋™์ƒ์ž…๋‹ˆ๋‹ค. ์นจ ๋‹ฆ์œผ์‹ญ์‡ผ. --# len() ๊ทธ๋Ÿผ, ์ปดํ“จํ„ฐํ•œํ…Œ ์ €ํฌ ๊ฐ€์กฑ์ด ๋ช‡ ๋ช…์ธ์ง€ ๋ฌผ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> len(family) len() ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ์— ์›์†Œ(element)๊ฐ€ ๋ช‡ ๊ฐœ ๋“ค์–ด ์žˆ๋Š”์ง€ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ €ํฌ ๊ฐ€์กฑ์„ family๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„ํ–ˆ์œผ๋‹ˆ๊นŒ 4๋ผ๊ณ  ๋Œ€๋‹ต์„ ํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž๋ฃŒ๋ฅผ ๋ฌถ์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ณด์‹  ๊ฒƒ์ฒ˜๋Ÿผ ๋Œ€๊ด„ํ˜ธ([ ])๋ž‘ ์ฝค๋งˆ(,)๋ฅผ ์จ์„œ ํ‘œํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ €ํฌ ๊ฐ€์กฑ ์ค‘์— ๋„˜๋ฒ„ ์“ฐ๋ฆฌ๊ฐ€ ๋ˆ„๊ตฌ์ธ์ง€ ๋ฌผ์–ด๋ณผ๊นŒ์š”? ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜๊ณ  Enter๋ฅผ ์‚ด์ง ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”. >>> family[3] ๋‹ต์ด ๋ญ๋ผ๊ณ  ๋‚˜์˜ค์ฃ ? ๋‹น๊ทผ 'gentleman'์ด๋ผ๊ณ  ๋‚˜์˜ค๊ฒ ์ฃ ? ๊ทธ๋Ÿฌ๋‚˜! ์ด์ƒํ•˜๊ฒŒ๋„ ํŒŒ์ด์ฌ์€ ๋‹ค๋ฅธ ๋Œ€๋‹ต์„ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ์ง์ ‘ ํ™•์ธํ•ด ๋ณด์„ธ์š”. ... ํ™•์ธํ•ด ๋ณด์…จ๋‚˜์š”? ์™œ ๊ทธ๋Ÿฐ ๋‹ต์ด ๋‚˜์™”์„๊นŒ์š”? ๊ทธ ์ด์œ ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์ž๋ฆฌ๋ฅผ ์ฐจ๊ณ ์•‰์€ 'mother'๊ฐ€ 1๋ฒˆ์ด ์•„๋‹ˆ๋ผ 0๋ฒˆ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์™œ 1์ด ์•„๋‹ˆ๋ผ 0์ผ๊นŒ์š”? ๊ทธ๊ฑด ์ €๋„ ์ž˜ ๋ชจ๋ฆ…๋‹ˆ๋‹ค.--; ํŒŒ์ด์ฌ ๋ง๊ณ  ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ๋„ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋ฌถ์–ด๋‘์—ˆ์„ ๋• ๋ฒˆํ˜ธ๋ฅผ 0๋ฒˆ๋ถ€ํ„ฐ ๋ถ™์—ฌ์ฃผ๋”๊ตฐ์š”. ๊ทธ๋ ‡๋‹ค๋ฉด ์ €๋Š” ๋ช‡ ๋ฒˆ์ผ๊นŒ์š”? 'mother'๊ฐ€ 0์ด๊ณ , 'father'๊ฐ€ 1์ด๋‹ˆ๊นŒ ์ €๋Š” 2๊ฒ ๊ตฐ์š”. ๋งž๋Š”์ง€ ํ™•์ธ์„ ํ•ด๋ด์•ผ์ฃ ? >>> family[2] 'gentleman' ์ œ๊ฐ€ ๋„˜๋ฒ„ ํˆฌ๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฐ ๊ฐ๊ฒฉ์Šค๋Ÿฌ์šด ์ˆœ๊ฐ„์ž…๋‹ˆ๋‹ค!!! ๊ธฐ๋ปํ•ด ์ฃผ์‹ญ์‹œ์˜ค, ์—ฌ๋Ÿฌ๋ถ„~. ํ•˜์ง€๋งŒ ๊ทธ ๊ธฐ์จ๋„ ์ž ์‹œ, ์ €์˜ ๋จธ๋ฆฌ๊ฐ€ 5400rpm์œผ๋กœ ํšŒ์ „ํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค(rpm์€ 1๋ถ„์— ๋ช‡ ๋ฐ”ํ€ด๋ฅผ ๋„๋Š”์ง€ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹จ์œ„๋กœ, ์ปดํ“จํ„ฐ์˜ ํ•˜๋“œ ๋””์Šคํฌ ํšŒ์ „์†๋„๊ฐ€ ๋ณดํ†ต 5400rpm ๋˜๋Š” 7200rpm์ž…๋‹ˆ๋‹ค). ์ €๋Š” ๋„˜๋ฒ„ ํˆฌ์— ๋งŒ์กฑํ•  ๋†ˆ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋จธ๋ฆฌ๋ฅผ ํ•œ์ฐธ ๊ตด๋ฆฌ๋˜ ์ €๋Š” ๋„˜๋ฒ„ 0์ด ๋  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋‚ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฌด์Šจ ๋ฐฉ๋ฒ•์ด๋ƒ๊ณ ์š”? ๋…๋ฆฝ์ž…๋‹ˆ๋‹ค. ๋Œ€ํ•œ๋…๋ฆฝ๋งŒ์„ธ~ remove() ์šฐ์„  ๊ฐ€์กฑ์—์„œ ์ œ ์ด๋ฆ„์„ ๋บ๋‹ˆ๋‹ค. >>> family.remove('gentleman') remove๋Š” ๋ญ”๊ฐ€๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ๋œป์„ ๊ฐ–๊ณ  ์žˆ์ฃ . ์œ„์˜ ๋ฌธ์žฅ์€ family์—์„œ 'gentleman'์ด๋ผ๋Š” ๋†ˆ์„ ์—†์• ๋ผ๋Š” ๋ง์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ œ๊ฐ€ ํ™•์‹คํžˆ ์—†์–ด์กŒ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> family ['mother', 'father', 'sexy lady'] ์˜ˆ, ์ œ๊ฐ€ ํ™•์‹คํžˆ ์ œ๊ฑฐ๋˜์—ˆ๊ตฐ์š”. ์ด์ œ ์ƒˆ๋กœ์šด ์„ธ๋Œ€๋ฅผ ๊ตฌ์„ฑํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋Š” family ๋ง๊ณ  ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์ง€์–ด์ฃผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์ƒˆ๋กœ์šด ์ด๋ฆ„์œผ๋กœ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”. 1.4 ์ธํ„ฐํ”„๋ฆฌํ„ฐ์™€ ์ปดํŒŒ์ผ๋Ÿฌ ์ปดํ“จํ„ฐ๊ฐ€ ์•Œ์•„๋“ฃ๋Š” ๋ง ์—„์ฒญ๋‚œ ์–‘์˜ ์ •๋ณด๋“ค์„ ์ž˜๋„ ์ฃผ๋ฌด๋ฅด๋Š” ์ด ์ปดํ“จํ„ฐ๋ž€ ๋…€์„์€ ๋Œ€์ฒด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š” ๊ฑธ๊นŒ์š”? ์ปดํ“จํ„ฐ๋Š” ์ „๊ธฐ๊ฐ€ ํ†ตํ•˜๋Š”์ง€ ์•ˆ ํ†ตํ•˜๋Š”์ง€์˜ ๋”ฑ ๋‘ ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ €๋…์ด ๋˜๋ฉด ์ „๊นƒ๋ถˆ์„ ์ผœ๊ณ , ์ž ์ž˜ ๋• ๋„๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ €๋Š” ๋ถˆ์„ ์ผœ๊ณ ๋„ ์ž˜ ์ž์ง€๋งŒ์š”.^^ ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•œ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ ์‹ถ์ง€๋งŒ, ์Šค์œ„์น˜๊ฐ€ ๋‘ ๊ฐœ ์žˆ์œผ๋ฉด ๋„ค ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ์ƒ๊ฐํ•ด ๋ณด์„ธ์š”. ํฐ ๋ฐฉ ๋ถˆ ์ผœ๊ณ , ์ž‘์€๋ฐฉ ๋ถˆ ๊บผ. ํฐ ๋ฐฉ ๋ถˆ ์ผœ๊ณ , ์ž‘์€๋ฐฉ๋„ ๋ถˆ ์ผœ. ํฐ ๋ฐฉ ๋ถˆ ๋„๊ณ , ์ž‘์€๋ฐฉ ๋ถˆ ์ผœ. ํฐ ๋ฐฉ ๋ถˆ ๋„๊ณ , ์ž‘์€๋ฐฉ๋„ ๋ถˆ ๊บผ. ์Šค์œ„์น˜๊ฐ€ 10๊ฐœ ์žˆ์œผ๋ฉด ๋ช‡ ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์Šค์œ„์น˜๊ฐ€ ๋‘ ๊ฐœ ์žˆ์œผ๋ฉด 2์˜ ์ œ๊ณฑ์ธ 4 ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ ์š”, ์Šค์œ„์น˜๊ฐ€ 10๊ฐœ ์žˆ์œผ๋ฉด 2์˜ 10 ์ œ๊ณฑ, ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ 1024๊ฐ€์ง€ ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ์—์„  ๋ถˆ์ด ์ผœ์ง€๊ณ  ๊บผ์ง„ ๊ฒƒ์„ 1๊ณผ 0์œผ๋กœ ๋‚˜ํƒ€๋‚ด๊ณ ์š”, ์Šค์œ„์น˜ ํ•˜๋‚˜์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์„ ๋น„ํŠธ(bit)๋ผ๊ณ  ๋งํ•˜์ง€์š”. ์ปดํ“จํ„ฐ์— ์ผ์„ ์‹œํ‚ค๋ ค๋ฉด ์ปดํ“จํ„ฐ๊ฐ€ ์•Œ์•„๋“ค์„ ์ˆ˜ ์žˆ๋Š” ๋ง๋กœ ์ง€์‹œ(instruction)๋ฅผ ๋‚ด๋ ค์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ ์“ฐ์ด๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์ €๊ธ‰ ์–ธ์–ด(low-level language)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ €๊ธ‰ ์–ธ์–ด๋กœ๋Š” ๊ธฐ๊ณ„์–ด์™€ ์–ด์…ˆ๋ธ”๋ฆฌ์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‚ฌ๋žŒ์ด 1๊ณผ 0์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ• ๊นŒ์š”? โ€œ์ปดํ“จํ„ฐ์•ผ, 10111101ํ•œ ๋‹ค์Œ์— 01001011 ํ•˜๊ณ , ๋งŒ์•ฝ์— 10011010์ด๋ฉด 10101100 ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 11010011 ํ•ด๋‹ค์˜ค, ์•Œ์•˜์ง€??โ€ ์–ผ๋งˆ๋‚˜ ์งœ์ฆ ๋‚˜๊ฒ ์Šต๋‹ˆ๊นŒ? ๊ทธ๋Ÿฐ ์–ด๋ ค์›€์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ๋žŒ์ด ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ์„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์š”์ฆ˜ ๋งŽ์ด ์“ฐ๋Š” C/C++, ํŒŒ์ด์ฌ, ์ž๋ฐ” ๊ฐ™์€ ๊ณ ๊ธ‰ ์–ธ์–ด(high-level language)๊ฐ€ ์ƒ๊ฒจ๋‚ฌ๋‹ต๋‹ˆ๋‹ค. ์ธํ„ฐํ”„๋ฆฌํ„ฐ์™€ ์ปดํŒŒ์ผ๋Ÿฌ ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฐ ๊ณ ๊ธ‰ ์–ธ์–ด๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์ง  ๋‹ค์Œ์—” ์ปดํ“จํ„ฐ๊ฐ€ ์•Œ์•„๋“ค์„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฒˆ์—ญ์„ ํ•ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ๊ฒ ์ฃ ? ๊ทธ๋ ‡๊ฒŒ ๋ฒˆ์—ญ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋„ ๋‘ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ต๋‹ˆ๋‹ค. (์˜ˆ, ์••๋‹ˆ๋‹ค. ์ €๋„ ์ข…๋ฅ˜ ๋งŽ์€ ๊ฑฐ ๋”ฑ ์งˆ์ƒ‰์ž…๋‹ˆ๋‹คโ€ฆ^^;) ํ•œ๋งˆ๋”” ํ•  ๋•Œ๋งˆ๋‹ค ๋™์‹œํ†ต์—ญํ•ด ์ฃผ๋Š” ๋ฐฉ์‹์„ ์ธํ„ฐํ”„๋ฆฌํ„ฐ(interpret) ๋ฐฉ์‹์ด๋ผ๊ณ  ํ•˜๊ณ , ๋งํ•˜๋Š” ๊ฒƒ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋“ฃ๊ณ  ๋‚˜์„œ ํ•œ๊บผ๋ฒˆ์— ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ์„ ์ปดํŒŒ์ผ(compile) ๋ฐฉ์‹์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋ฐฐ์šฐ๋Š” ํŒŒ์ด์ฌ์€ ์–ด๋–ค ๋ฐฉ์‹์ผ๊นŒ์š”? ํŒŒ์ด์ฌ์€ ์šฐ๋ฆฌ์˜ ๋ช…๋ น์„ ํ•œ ์ค„์”ฉ ํ•ด์„ํ•ด์„œ ์ผ์„ ํ•˜๋Š” ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์ด ํŒŒ์ด์ฌ ์–ธ์–ด๋กœ ์ž‘์„ฑํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ปดํ“จํ„ฐ์— ๋ฒˆ์—ญํ•ด ์ฃผ๋Š” ํŒŒ์ด์ฌ ์…ธ์ด ๋ฐ”๋กœ ์ธํ„ฐํ”„๋ฆฌํ„ฐ(interpreter)๋ž๋‹ˆ๋‹ค. ์ธํ„ฐํ”„๋ฆฌํ„ฐ์™€ ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ์„ค๋ช…ํ•œ ์• ๋‹ˆ๋ฉ”์ด์…˜(์šฐ๋ฆฌ๋ง ์ž๋ง‰ ์žˆ์Œ): https://youtu.be/Dx2tSsd3aFc 1.5 ํŒŒ์ด์ฌ ์„ค์น˜์™€ ์‹คํ–‰ ํŒŒ์ด์ฌ์ด๋ผ๋Š” ์–ธ์–ด๋ฅผ ์ข€ ๋” ๊ฐ–๊ณ  ๋†€์•„๋ณด๊ณ  ์‹ถ์€ ๋งˆ์Œ์ด ๋“œ์…จ๋‚˜์š”? ์ €๋Š” โ€˜๊ณต๋ถ€โ€™ํ•œ๋‹ค๋Š” ๋ง๋ณด๋‹ค๋Š” โ€˜๋…ผ๋‹คโ€™๋Š” ๊ฒƒ์ด ๋” ๋งˆ์Œ์— ๋“œ๋Š”๊ตฐ์š”. ์žฌ๋ฏธ์žˆ์œผ๋‹ˆ๊นŒ์š”. ํŒŒ์ด์ฌ ์„ค์น˜ ํŒŒ์ด์ฌ์„ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋ฐฐ์šฐ๊ธฐ์— ์•ž์„œ, ์ปดํ“จํ„ฐ์— ํŒŒ์ด์ฌ์„ ์„ค์น˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ฑ…์—๋Š” ์ปดํ“จํ„ฐ์˜ ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ์‹ค์Šต๋„ ์žˆ์œผ๋ฏ€๋กœ ํŒŒ์ด์ฌ์„ ์„ค์น˜ํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•˜์ง€๋งŒ, ์„ค์น˜ํ•˜๊ธฐ ๊ณค๋ž€ํ•œ ๋ถ„์€ ๋„˜์–ด๊ฐ€์…”๋„ ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์†Œ๊ฐœํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ํŒŒ์ด์ฌ ๋ฐฐํฌํŒ ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด ์„ค์น˜ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ์„ค์น˜ python.org์˜ ๋‹ค์šด๋กœ๋“œ ํŽ˜์ด์ง€์—์„œ ํŒŒ์ด์ฌ ์„ค์น˜ ํ”„๋กœ๊ทธ๋žจ์„ ๋‚ด๋ ค๋ฐ›์œผ์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์‹  ๋ฒ„์ „(2023๋…„ 1์›” 7์ผ ํ˜„์žฌ 3.11.1)์„ ๋‹ค์šด๋กœ๋“œํ•ด ์ฃผ์„ธ์š”. ํŒŒ์ผ์„ ๋ฐ›์œผ์…จ์œผ๋ฉด ์ธ์Šคํ†จ๋Ÿฌ๋ฅผ ์‹คํ–‰ํ•ด โ€˜Install Nowโ€™ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์„ค์น˜ํ•ด ์ฃผ์„ธ์š”. ํŒŒ์ด์ฌ ์„ค์น˜<NAME>์ƒ: https://youtu.be/mk8lP7WLQ9E ๊ณผํ•™, ์ˆ˜ํ•™, ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ชฉ์ ์˜ ๋ฐฐํฌํŒ ์„ค์น˜ ๊ณผํ•™ ๊ณ„์‚ฐ์šฉ์œผ๋กœ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋“ค์„ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐํฌํŒ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘ ๋Œ€ํ‘œ์ ์ธ ๊ฒƒ์ด ์•„๋‚˜์ฝ˜๋‹ค(Anaconda)์ž…๋‹ˆ๋‹ค. ์ด ์ฑ…์œผ๋กœ ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•์„ ์ตํžŒ ํ›„ ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ฐ ๋จธ์‹ ๋Ÿฌ๋‹์„ ํ•˜๋ ค๋Š” ๋ถ„์€ ์ฒ˜์Œ๋ถ€ํ„ฐ ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•ด์„œ ์‹ค์Šตํ•˜์…”๋„ ๋ฉ๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค ์„ค์น˜<NAME>์ƒ: https://youtu.be/CNGBwFcmBZM tip Q: ๋‹ค๋ฅธ ํŒŒ์ด์ฌ ์ฑ…์—์„œ 3.7 ๋ฒ„์ „์œผ๋กœ ์‹ค์Šตํ•˜๋ผ๋Š”๋ฐ, ํŒŒ์ด์ฌ์„<NAME>๊ณ  ์ƒˆ๋กœ ์„ค์น˜ํ•ด์•ผ ํ•˜๋‚˜์š”? A: ํŒŒ์ด์ฌ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์„ ๋ฐฐ์šธ ๋•Œ๋Š” ๋” ๋†’์€ ๋ฒ„์ „์œผ๋กœ ์‹ค์Šตํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํŒŒ์ด์ฌ 3.7์„ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ํŒŒ์ด์ฌ 3.9์—์„œ๋„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์ถ”๊ฐ€๋กœ ์„ค์น˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋‹ค ๋ณด๋ฉด ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ๋งž์ถฐ์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ ํŒŒ์ด์ฌ ๊ฐ€์ƒ ํ™˜๊ฒฝ(venv ๋˜๋Š” conda env)์„ ํ™œ์šฉํ•˜๋ฉด ๊ฐ€์ƒ ํ™˜๊ฒฝ๋งˆ๋‹ค ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ๋‹ค๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ์–ด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค ์„ค์น˜<NAME>์ƒ์˜ ๊ฐ€์ƒ ํ™˜๊ฒฝ ์„ค๋ช…(https://youtu.be/CNGBwFcmBZM? t=494)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ ์Šคํ† ์–ด ์œˆ๋„ ์šด์˜์ฒด์ œ์— ํฌํ•จ๋œ โ€˜๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ ์Šคํ† ์–ด(Microsoft Store)โ€™์—์„œ โ€˜Pythonโ€™์„ ๊ฒ€์ƒ‰ํ•ด ์„ค์น˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งฅ ๋˜๋Š” ๋ฆฌ๋ˆ…์Šค ํ™˜๊ฒฝ ๋งฅ์ด๋‚˜ ๋ฆฌ๋ˆ…์Šค๋ฅผ ์“ฐ์‹œ๋Š” ๋ถ„๋“ค์€ ํŒŒ์ด์ฌ์ด ์ด๋ฏธ ์„ค์น˜๋˜์–ด ์žˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ„ฐ๋ฏธ๋„์—์„œ python ๋˜๋Š” python3์ด๋ผ๊ณ  ์ณ๋ณด์„ธ์š”. Mac% python Python 2.7.10 (default, Feb 7 2017, 00:08:15) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> ๊ทธ๋Ÿฐ๋ฐ ์‹œ์Šคํ…œ์— ๊ธฐ๋ณธ์œผ๋กœ ์„ค์น˜๋œ ํŒŒ์ด์ฌ ๋ฒ„์ „์ด 2.7.X์ธ ๊ฒฝ์šฐ, ์ด ์ฑ…์—์„œ๋Š” ํŒŒ์ด์ฌ 3๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…ํ•˜๋ฏ€๋กœ ๊ฐ„ํ˜น ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน๋ณ„ํ•œ ์ด์œ ๊ฐ€ ์—†๋‹ค๋ฉด ํŒŒ์ด์ฌ 3.8 ์ด์ƒ์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋งฅ OS์— ํŒŒ์ด์ฌ 3 ์„ค์น˜ (1) ์ œ๊ฐ€ ์ƒˆ๋กœ ์‚ฐ ๋งฅ๋ถ(2022๋…„ํ˜•, M2)์— ํŒŒ์ด์ฌ์ด ์„ค์น˜๋ผ ์žˆ๋Š”์ง€ ๋ณด๋ ค๊ณ  ํ„ฐ๋ฏธ๋„์—์„œ ๋ช…๋ น์„ ์‹คํ–‰ํ–ˆ๋”๋‹ˆ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ํŒ์—… ์ฐฝ์ด ๋–ด์Šต๋‹ˆ๋‹ค. yong@MacBookPro ~ % python --version zsh: command not found: python yong@MacBookPro ~ % python3 --version xcode-select: note: no developer tools were found at '/Applications/Xcode.app', requesting install. Choose an option in the dialog to download the command line developer tools. ํŒ์—… ์ฐฝ์˜ โ€˜์„ค์น˜โ€™ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  ์ž ์‹œ ๊ธฐ๋‹ค๋ ธ๋”๋‹ˆ ํŒŒ์ด์ฌ์ด ์„ค์น˜๋์Šต๋‹ˆ๋‹ค. yong@MacBookPro ~ % python3 --version Python 3.9.6 yong@MacBookPro ~ % python3 Python 3.9.6 (default, Oct 18 2022, 12:41:40) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> ๋งฅ OS์— ์„ค์น˜ (2) ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ, https://www.python.org/downloads/macos/์—์„œ ๋งฅ์šฉ ์ธ์Šคํ†จ๋Ÿฌ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด์„œ ์„ค์น˜ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค. ๋งฅ์—๋Š” ์ธํ…” CPU๊ฐ€ ๋“ค์–ด๊ฐ„ ๊ฒƒ๋„ ์žˆ๊ณ  ์• ํ”Œ ์‹ค๋ฆฌ์ฝ˜(M1, M2)์ด ๋“ค์–ด๊ฐ„ ๊ฒƒ๋„ ์žˆ๋Š”๋ฐ, ํŒŒ์ด์ฌ ์ตœ์‹  ๋ฒ„์ „์„ ์„ค์น˜ํ•  ๋•Œ๋Š” ์–ด๋Š CPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๊ด€๊ณ„์—†์ด macOS 64-bit universal2 installer๋ฅผ ์‚ฌ์šฉํ•ด ์„ค์น˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 2020๋…„ 11์›” ~ 2022๋…„ 5์›” ์‚ฌ์ด์— ๋‚˜์˜จ ํŒŒ์ด์ฌ ๋ฐฐํฌํŒ์˜ ๋งฅ์šฉ ์ธ์Šคํ†จ๋Ÿฌ๋Š” CPU ๋ณ„๋กœ ๋”ฐ๋กœ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘์—์„œ ํ•˜๋‚˜๋ฅผ ์„ค์น˜ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ๋ฐ”ํƒ• ํ™”๋ฉด ์™ผ์ชฝ ์œ„์˜ ์‚ฌ๊ณผ ๋ชจ์–‘ ์•„์ด์ฝ˜์„ ๋ˆ„๋ฅด๊ณ  โ€˜์ด Mac์— ๊ด€ํ•˜์—ฌโ€™๋ฅผ ๋ˆ„๋ฅธ ๋’ค ์นฉ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์นฉ์ด Apple M1 ๋˜๋Š” Apple M2๋กœ ํ‘œ์‹œ๋  ๋•Œ๋Š” macOS 64-bit universal2 installer๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ์ธํ…” ์นฉ์ผ ๋•Œ๋Š” macOS 64-bit Intel installer๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด์—์„œ ํŒŒ์ด์ฌ ์‚ฌ์šฉ ๋‹ค์Œ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. https://wikidocs.net/3168 ํŒŒ์ด์ฌ ์…ธ ์‹คํ–‰ํ•˜๊ธฐ ๊ทธ๋Ÿผ ํŒŒ์ด์ฌ์„ ์‹คํ–‰ํ•ด ๋ณผ๊นŒ์š”? ์œˆ๋„์˜ ์‹œ์ž‘ ๋ฉ”๋‰ด์—์„œ ํŒŒ์ด์ฌ์„ ์‹คํ–‰ํ•ด ์ฃผ์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด ์œˆ๋„ 11์— Python 3.10(64๋น„ํŠธ)์„ ์„ค์น˜ํ•œ ๊ฒฝ์šฐ, ์œˆ๋„ ๋ฒ„ํŠผ โ†’ ๋ชจ๋“  ์•ฑ โ†’ Python 3.10 โ†’ Python 3.10 (64-bit)์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒŒ์ด์ฌ ์…ธ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ์•ž์œผ๋กœ ์ด๊ฒƒ์„ ์ด์šฉํ•ด์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๊ฒŒ ๋  ๊ฒ๋‹ˆ๋‹ค. 1.6 ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ๋Œ€ํ™”์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ–ˆ๋˜, ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— ๋ช…๋ น์„ ํ•œ ์ค„์”ฉ ์ž…๋ ฅํ•˜๋ฉด ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ๊ทธ๋•Œ๊ทธ๋•Œ ๋‹ต์„ ๋Œ๋ ค์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ๋‘๊ณ  โ€œ๋Œ€ํ™”์‹์œผ๋กœ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹คโ€๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹น์—ฐํ•œ ๋ง์ด์ง€๋งŒ ๋Œ€ํ™”์‹์€ ์•„์ฃผ ์‰ฝ๊ณ  ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋„ ํŒŒ์ด์ฌ์— ๋Œ€ํ•ด ์•„๋ฌด๊ฒƒ๋„ ๋ชจ๋ฅด๋Š” ์ƒํƒœ์—์„œ ๋‹น์žฅ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ๊ณ„์‚ฐ๊ธฐ์ฒ˜๋Ÿผ ์“ฐ๊ธฐ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์—ˆ์ง€์š”. ๋Œ€ํ™”์‹ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ๊ทธ๋ƒฅ ์‰ฝ๊ธฐ๋งŒ ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํŒŒ์ด์ฌ์˜ ๋ฌธ๋ฒ•์„ ๋ฐฐ์šฐ๊ณ  ์—ฐ์Šตํ•ด ๋ณด๊ฑฐ๋‚˜, ๋ณต์žกํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์€ ๋ถ€๋ถ„์„ ํ…Œ์ŠคํŠธํ•  ๋•Œ๋„ ์•„์ฃผ ํŽธ๋ฆฌํ•˜๋‹ต๋‹ˆ๋‹ค. ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋กœ ํ”„๋กœ๊ทธ๋žจ ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๊ธฐ ๋Œ€ํ™”์‹ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ๋ชจ๋กœ ํŽธ๋ฆฌํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ๋•Œ๋Š” ์—ญ์‹œ ํ”„๋กœ๊ทธ๋žจ์„ ํŒŒ์ผ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด ๋‘์—ˆ๋‹ค๊ฐ€ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ์ฃ ? ํŒŒ์ด์ฌ์—์„œ๋Š” ํ”„๋กœ๊ทธ๋žจ ํŒŒ์ผ์„ ์–ด๋–ป๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฑธ๊นŒ์š”? ํŒŒ์ด์ฌ์— ํฌํ•จ๋œ IDLE ๋˜๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์ฆ๊ฒจ ์“ฐ๋Š” ํ…์ŠคํŠธ ํŽธ์ง‘๊ธฐ(๋ฉ”๋ชจ์žฅ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ)์—์„œ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”. ํŒŒ์ด์ฌ 3: print('์ง๊ฐ์‚ผ๊ฐํ˜• ๊ทธ๋ฆฌ๊ธฐ\n') leg = int(input('๋ณ€์˜ ๊ธธ์ด: ')) for i in range(leg): print('* ' * (i + 1)) area = (leg ** 2) / 2 print('๋„“์ด:', area) ํŒŒ์ด์ฌ 2: print('Right triangle\n') leg = int(raw_input('leg: ')) for i in range(leg): print('* ' * (i + 1)) area = (leg ** 2) / 2.0 print('area:', area) ์ฒ˜์Œ ๋ณด๋Š” ๊ฒƒ๋“ค์ด ๋งŽ์ง€๋งŒ ๋‚˜์ค‘์— ๋ฐฐ์šธ ๊ฒƒ๋“ค์ด๋‹ˆ ๊ฒ๋จน์ง€ ๋งˆ์‹œ๊ณ  ๊ทธ๋ƒฅ ๋˜‘๊ฐ™์ด ์“ฐ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•˜์‹ค ์ ์€, ๋ฐ˜๋“œ์‹œ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ์ง€์ผœ์ฃผ์…”์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•˜์‹ค ๋• ๊ณต๋ฐฑ ๋„ค ์นธ์”ฉ ๋“ค์—ฌ ์“ฐ๊ธฐ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ triangle.py๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•ด ์ฃผ์„ธ์š”. ํŒŒ์ด์ฌ ์ฝ”๋“œ ํŒŒ์ผ์—๋Š” py๋ผ๋Š” ํ™•์žฅ์ž๊ฐ€ ๋ถ™์Šต๋‹ˆ๋‹ค. ์ €์žฅ ์œ„์น˜๋Š” \wikidocs-chobo-python\ch01์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ ์ž‘์„ฑ๊ณผ ์‹คํ–‰๊นŒ์ง€ IDLE์—์„œ ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ํ…์ŠคํŠธ ํŽธ์ง‘๊ธฐ์—์„œ ์ž‘์„ฑํ•œ ํ›„ ์œˆ๋„ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ์–ด์š”. tip Tip: ํ…์ŠคํŠธ ํŽธ์ง‘๊ธฐ์— ๊ณต๋ฐฑ์„ ํ‘œ์‹œํ•˜๊ธฐ ํ…์ŠคํŠธ ํŽธ์ง‘๊ธฐ ์ค‘์—๋Š” ํƒญ๊ณผ ์ŠคํŽ˜์ด์Šค๋ฅผ ๊ธฐํ˜ธ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋Š” ๊ฒƒ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Notepad++๋ฅผ ์˜ˆ๋กœ ๋“ค๋ฉด, ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ณด๊ธฐโ†’๊ธฐํ˜ธ ๋ณด๊ธฐโ†’๊ณต๋ฐฑ๊ณผ ํƒญ ํ‘œ์‹œ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. IDLE์—์„œ ์‹คํ–‰ ์œ„ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ IDLE์—์„œ ์ž‘์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•˜๋Š” ์˜์ƒ์ž…๋‹ˆ๋‹ค. (ํŒŒ์ด์ฌ 3.8) https://youtu.be/P4xAx4HFahU ์œˆ๋„ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์—์„œ ์‹คํ–‰ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ ์ฐฝ ์—ด๊ธฐ ๋จผ์ € ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋„์›Œ๋ณด์„ธ์š”. ๋‹ค์Œ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ํŽธํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œˆ๋„ ๋ฉ”๋‰ด์—์„œ Windows ์‹œ์Šคํ…œ - ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋ฅผ ์„ ํƒ + R์„ ๋ˆ„๋ฅด๊ณ  ์‹คํ–‰ ์ฐฝ์—์„œ cmd ์ž…๋ ฅ ์œˆ๋„ ๋ฒ„์ „์— ๋”ฐ๋ผ ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅด๊ธฐ๋Š” ํ•˜์ง€๋งŒ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๋น„์Šทํ•œ ์ฐฝ์ด ๋œฐ ๊ฑฐ์˜ˆ์š”. ๋ช…๋ น์–ด ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์‚ฌ์šฉํ•˜๊ธฐ ๋ฒˆ๊ฑฐ๋กญ๊ธฐ๋Š” ํ•ด๋„ ์ข€ ๋” ์ž์„ธํ•œ ์กฐ์ž‘์„ ํ•˜๊ธฐ์— ์ ํ•ฉํ•˜์ง€์š”. ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ๋•Œ๋„ ์ข…์ข… ์“ฐ์ด๋‹ˆ๊นŒ ์ด๋ฒˆ ๊ธฐํšŒ์— ์ตํ˜€๋‘์ž๊ณ ์š”. ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์‚ฌ์šฉ์ž์˜ ํ™ˆ ํด๋”์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ ์ œ ์ปดํ“จํ„ฐ์—์„œ๋Š” C:\Users\yong์ด ํ˜„์žฌ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์‹คํ–‰ ํŒŒ์ผ ๊ฒฝ๋กœ ํ™•์ธ ํŒŒ์ด์ฌ ์‹คํ–‰ ํŒŒ์ผ์ด ์–ด๋””์— ์žˆ๋Š”์ง€๋Š” where(์œˆ๋„)๋‚˜ which(๋ฆฌ๋ˆ…์Šค, ๋งฅ) ๋ช…๋ น์„ ์จ์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œˆ๋„์—์„œ: C:\Users\yong>where python C:\Users\yong\AppData\Local\Programs\Python\Python310\python.exe C:\Users\yong\AppData\Local\Microsoft\WindowsApps\python.exe tip where ๋ช…๋ น์€ ํ˜„์žฌ ํด๋” ๋ฐ PATH ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ๋“ฑ๋ก๋œ ๊ฒฝ๋กœ์—์„œ ํŒŒ์ผ์„ ์ฐพ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŒŒ์ด์ฌ ์„ค์น˜ ์‹œ PATH ๋“ฑ๋ก ์˜ต์…˜์„ ์„ ํƒํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, where์˜ ๊ฒฐ๊ณผ์— ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋ˆ…์Šค์—์„œ: (base) yong@thinkpad:~$ which python /home/yong/anaconda3/bin/python ํŒŒ์ด์ฌ ์„ค์น˜ ๊ฒฝ๋กœ๋กœ ์ด๋™ ํŒŒ์ด์ฌ์ด ์„ค์น˜๋œ ๊ณณ์œผ๋กœ ์ด๋™ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด cd ๋ช…๋ น์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. cd Appdata\Local\Programs\Python dir cd Python310 ํ˜„์žฌ ๊ฒฝ๋กœ๋Š” C:\Users\yong\AppData\Local\Programs\Python\Python310์ž…๋‹ˆ๋‹ค. ์ด๊ณณ์— ํŒŒ์ด์ฌ ์‹คํ–‰ ํŒŒ์ผ์ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. dir python* ํŒŒ์ด์ฌ์œผ๋กœ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ํŒŒ์ด์ฌ์ด ์„ค์น˜๋œ ํด๋”๋ฅผ ์ž˜ ์ฐพ์•„์˜ค์…จ์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ น์„ ๋‚ด๋ ค์ฃผ์„ธ์š”. python \wikidocs-chobo-python\ch01\triangle.py ์ด ๋ช…๋ น์€ 'ํŒŒ์ด์ฌ์•„, \wikidocs-chobo-python\ch01 ํด๋”์— ์žˆ๋Š” triangle.py๋ฅผ ์‹คํ–‰ํ•˜์ž๊พธ๋‚˜'๋ผ๋Š” ๋œป์ด๋ž๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์ด ํ”„๋กœ๊ทธ๋žจ ์ „์ฒด๋ฅผ ๋ฒˆ์—ญํ•˜๋ฉด์„œ ์‹คํ–‰์‹œ์ผœ ์ค„ ๊ฑฐ์˜ˆ์š”. ์ง๊ฐ์‚ผ๊ฐํ˜• ๊ทธ๋ฆฌ๊ธฐ ๋ณ€์˜ ๊ธธ์ด: 4 * * * * * * * * * ๋„“์ด: 8.0 ๋งŒ์•ฝ ์—ฌ๊ธฐ์—์„œ ๊ทธ๋ƒฅ python์ด๋ผ๊ณ ๋งŒ ์ž…๋ ฅํ•˜๋ฉด ์–ด๋””์„œ ๋งŽ์ด ๋ณธ ๊ฒƒ์ด ๋‚˜ํƒ€๋‚  ๊ฒ๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๋Œ€ํ™”์‹ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์ฃ . ํ”„๋กœ๊ทธ๋žจ ํŒŒ์ผ์˜ ์‹คํ–‰์ด ์ž˜ ์•ˆ๋˜๋Š” ๋ถ„์€ ์˜ˆ์ œ์™€ ์ž์‹ ์ด ์ง์ ‘ ์ง  ํ”„๋กœ๊ทธ๋žจ์„ ์ž˜ ๋น„๊ตํ•ด์„œ ์ž˜๋ชป๋œ ๋ถ€๋ถ„์ด ์žˆ๋Š”์ง€ ์ž˜ ์ฐพ์•„๋ณด์„ธ์š”. ์—ฐ์Šตํ•  ๋•Œ ์‹ค์ˆ˜ํ•˜๋Š” ๋ถ€๋ถ„์ด ๋งŽ์„์ˆ˜๋ก ๊ณต๋ถ€์— ๋” ๋งŽ์€ ๋„์›€์ด ๋œ๋‹ต๋‹ˆ๋‹ค. ์ž, ์ด๋ ‡๊ฒŒ ํ•ด์„œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ”„๋กœ๊ทธ๋žจ ํŒŒ์ผ์„ ์‹คํ–‰์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•๋„ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์•Œ๋ ค ๋“œ๋ฆด ๊ฒƒ์€, ์ด ํŒŒ์ผ์„ ๊ทธ๋ƒฅ ๋”๋ธ”ํด๋ฆญํ•ด๋„ ์‹คํ–‰๋œ๋‹ค๋Š” ์‚ฌ์‹คโ€ฆ ํ•˜์ง€๋งŒ ๋”๋ธ” ํด๋ฆญ์ด ํ•ญ์ƒ ํŽธํ•œ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์€ ๊ธˆ๋ฐฉ ์•Œ๊ฒŒ ๋˜์‹ค ๊ฑฐ์˜ˆ์š”. ๊ทธ๋Ÿผ ์ข‹์€ ๊ฟˆ ๊พธ์„ธ์š”~. ์ ํ”„ ํˆฌ ํŒŒ์ด์ฌ - ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ๋„ ์ฝ์–ด๋ณด์„ธ์š” ๊ฑฐ๋ถ์ด Python ํ„ฐํ‹€ ๊ทธ๋ž˜ํ”ฝ์Šค๋ฅผ ์‚ฌ์šฉํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋„ํ˜• ๊ทธ๋ฆฌ๊ธฐ: https://youtu.be/ZEV3pGTdlxw 1.6.1 ์—ฐ์Šต ๋ฌธ์ œ: ์ œ๊ณฑ ๋ฌธ์ œ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ๊ทธ ์ˆ˜์˜ ์ œ๊ณฑ์„ ๊ณ„์‚ฐํ•ด ์ถœ๋ ฅํ•˜๋Š” ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์˜ˆ 1 ์ž…๋ ฅ: ์ถœ๋ ฅ: ์˜ˆ 2 ์ž…๋ ฅ: ์ถœ๋ ฅ: 25 ์ฝ”๋“œ: ch01/square.py 2. ์ œ์–ด ๊ตฌ์กฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํ•ต์‹ฌ์ ์ธ ์š”์†Œ์ธ ๋ถ„๊ธฐ์™€ ๋ฐ˜๋ณต์„ ํŒŒ์ด์ฌ์—์„œ๋Š” ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์‚ดํŽด๋ณผ๊ฒŒ์š”. ์ด ์žฅ์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฒƒ: while์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ ์กฐ๊ฑด๋ฌธ(if-elif-else) for๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ 2.1 while์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ ๊ฐ•์˜ ์˜์ƒ https://youtu.be/j_NPpCNsfIM ์˜ค๋Š˜์€ ํ•œ์„๋ด‰ ์ด์•ผ๊ธฐ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํฐ ๋œป์„ ํ’ˆ๊ณ  ์–ด๋จธ๋‹ˆ๋ฅผ ๋– ๋‚˜ ์ˆซ์ž ๊ณต๋ถ€๋ฅผ ํ•˜๋˜ ์„๋ด‰์€ ์–ด๋จธ๋‹ˆ๊ฐ€ ๋„ˆ๋ฌด ๊ทธ๋ฆฌ์›Œ ์ง‘์„ ์ฐพ์•„๊ฐ”์ง€์š”. ๋Œ์•„์˜จ ์„๋ด‰์„ ๋ณธ ์–ด๋จธ๋‹ˆ๋Š” ๋ง์”€ํ•˜์…จ์Šต๋‹ˆ๋‹ค. "๋‚˜๋Š” ๋–ก์„ ์ฐ ํ…Œ๋‹ˆ, ๋„ˆ๋Š” ๊ธ€์„ ์“ฐ๊ฑฐ๋ผ." ๋ถˆ์„ ๋ˆ ์–ด๋จธ๋‹ˆ๋Š” ๋–ก์„ ์ฐ๊ธฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋จธ๋‹ˆ๋Š” ๋‚ด์ผ ์•„์นจ์— ์•„๋“ค์—๊ฒŒ ๋–ก๊ตญ์„ ๋“์—ฌ์ฃผ๋ ค๊ณ  ๊ธฐ๋‹ค๋ž€ ๋–ก์„ ์จ์…จ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋™์•ˆ์— ์„๋ด‰์€ ์ง€๊ธˆ๊นŒ์ง€ ์—ฐ๋งˆํ•œ ์‹ค๋ ฅ์„ ์ด๋™์›ํ•˜์—ฌ 1๋ถ€ํ„ฐ 10๊นŒ์ง€ ์ˆซ์ž๋ฅผ ์”๋‹ˆ๋‹ค. 1, 2, 3, โ€ฆ ์ด ์ƒํ™ฉ์„ ํŒŒ์ด์ฌ์œผ๋กœ ๋งŒ๋“ค๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊นŒ์š”? ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์› ๋˜ ๊ฒƒ๋งŒ ๊ฐ€์ง€๊ณ  ํ•ด๋ณผ๊นŒ์š”? >>> print(1) >>> print(2) >>> print(3) >>> ์ข€ ๊ท€์ฐฎ๊ธฐ๋Š” ํ•ด๋„ ์“ธ๋งŒํ•˜์ง€์š”? ๊ทธ๋Ÿฐ๋ฐ, ์–ด๋จธ๋‹ˆ๊ป˜์„œ 1๋ถ€ํ„ฐ 100๊นŒ์ง€ ์จ๋ณด๋ผ๊ณ  ํ•˜์‹ญ๋‹ˆ๋‹ค. ์•„, ๋ง‰๋ง‰ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋Š ์„ธ์›”์— ๋‹ค ์”๋‹ˆ๊นŒ. 100๊นŒ์ง€ ์“ฐ๋ฉด ์•„์นจ์ด ๋ฐ์•„์˜ค๊ฒ ๊ตฐ์š”. ์ด๋Ÿด ๋• ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผ๊ฒ ์ง€์š”? ์˜ค๋Š˜ ๊ทธ ๋น„๋ฐ€์„ ์•Œ๋ ค ๋“œ๋ฆฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋กœ while์ด๋ผ๋Š” ๋…€์„์ž…๋‹ˆ๋‹ค. while ๋ฌธ 1, 2, 3, โ€ฆ, 98, 99, 100 1๋ถ€ํ„ฐ 100๊นŒ์ง€๋Š” ์ €๋Ÿฐ ๋ชจ์–‘์ด ๋  ํ…๋ฐ์š”, ๊ฐ€๋งŒ ๋ณด๋ฉด ๋‹ค์Œ์— ๋‚˜์˜ค๋Š” ์ˆซ์ž๋Š” ์•ž์˜ ์ˆซ์ž๋ณด๋‹ค 1์ด ๋” ํฝ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜๋ฉด ์•ž์˜ ์ˆซ์ž์— 1์„ ๋”ํ•˜๋ฉด ๋‹ค์Œ ์ˆซ์ž๊ฐ€ ๋‚˜์˜จ๋‹ค๋Š” ๊ฒƒ์ด์ง€์š”. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๊ณ„์† ์•ž์˜ ์ˆซ์ž์— 1์„ ๋”ํ•ด๋‚˜๊ฐ€๋‹ค๊ฐ€ 100๊นŒ์ง€ ์“ฐ๊ณ  ๊ทธ๋งŒ๋‘๋ฉด ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ๋‹ค์Œ์„ ์ž˜ ๋ณด์„ธ์š”. >>> num = 1 >>> while num <= 100: ... print(num) ... num = num + 1 ... ์šฐ๋ฆฌ๊ฐ€ ์“ธ ์ˆซ์ž๋ฅผ num์ด๋ผ๊ณ  ํ–ˆ๊ณ , ์—ฌ๊ธฐ์— 1์„ ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋˜์‹œ๋Š” ๋ถ„์€ ๋ณ€์ˆ˜ ๊ฐ•์ขŒ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ณด์„ธ์š”. ๊ทธ๋‹ค์Œ์— while์ด๋ผ๋Š” ๊ฒƒ์ด ๋‚˜์˜ค์ฃ ? while์€ ์šฐ๋ฆฌ๋ง๋กœ 'โ€ฆํ•˜๋Š” ๋™์•ˆ์—'๋ผ๋Š” ๋œป์„ ๊ฐ–๊ณ  ์žˆ์ฃ ? ์—ฌ๊ธฐ์„œ๋Š” 'num์ด 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๋™์•ˆ์—'๋ผ๋Š” ๋œป์œผ๋กœ ์“ด ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„์ง ์•Œ์ญ๋‹ฌ์ญํ•˜์‹œ์ฃ ? ์ผ๋‹จ ๋‹ค์Œ ๋ฌธ์žฅ์œผ๋กœ ๋„˜์–ด๊ฐ€ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. print(num) # ์ฒ˜์Œ ๋„ค ์นธ์„ ๋„์–ด ์จ์ฃผ์„ธ์š” num์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๋ฅผ ํ™”๋ฉด์— ๋ฟŒ๋ ค๋‹ฌ๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€์˜ ๊ฐ•์ขŒ ๋‚ด์šฉ์„ ์ดํ•ดํ•˜์…จ๋‹ค๋ฉด ์ดํ•ดํ•˜์‹ค ๊ฒ๋‹ˆ๋‹ค. ์ง€๊ธˆ num์—๋Š” 1์ด ๋“ค์–ด์žˆ์œผ๋‹ˆ ๋‹น๊ทผ 1์„ ์ฐ์–ด์ฃผ๊ฒ ์ง€์š”. while ๋ธ”๋ก ๋‚ด๋ถ€์— ์žˆ๋‹ค๋Š” ํ‘œ์‹œ๋กœ ๊ณต๋ฐฑ ๋„ค ์นธ์„ ์จ์„œ ๋“ค์—ฌ์“ฐ๊ธฐ ํ•ด์ฃผ์„ธ์š”. ๊ทธ๋‹ค์Œ ๋ฌธ์žฅ์„ ๋ด…์‹œ๋‹ค. ๋ณ€์ˆซ๊ฐ’ ์ฆ๊ฐ€ num = num + 1 # ์œ„ ์ค„๊ณผ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ๋งž์ถฐ์ฃผ์„ธ์š” ์ด๋ฒˆ์—” ์ง„์งœ๋กœ ์ด์ƒํ•œ ๊ฒƒ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค. num์ด 1์ด๋ผ๋ฉด 1 = 1 + 1์ด ๋œ๋‹ค๋Š” ์–ผํ† ๋‹นํ† ์•Š์€ ์†Œ๋ฆฌ๊ตฐ์š”. ๊ทธ๋Ÿฐ๋ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ = ํ‘œ์‹œ๋Š” '๊ฐ™๋‹ค'๋ผ๋Š” ๋œป ๋ง๊ณ  ๋‹ค๋ฅธ ๋œป์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ œ ์‹œ๊ณ„๊ฐ€ ๋ฐฑ๋งŒ ์›์ด๋ผ๋Š” ๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ–ˆ์ง€์š”? ๊ทธ๋ ‡์ฃ , watch = 1000000์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ watch๋ผ๋Š” ๋ณ€์ˆ˜์— 1000000์ด๋ผ๋Š” ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ผ๋Š” ๋œป์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์กฐ๊ธˆ ์ „์— num = num + 1์ด๋ผ๊ณ  ์“ด ๊ฒƒ์€ num์ด๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์ง„ ๊ฐ’์— 1์„ ๋”ํ•ด์„œ ๋‹ค์‹œ num์—๊ฒŒ ๋„ฃ์–ด์ฃผ๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ์ดํ•ด๊ฐ€ ์•ˆ ๋˜์‹ ๋‹ค๋ฉด ๋‹ค๋ฅธ ๊ฑธ ๋ณด์—ฌ๋“œ๋ฆฌ๋„๋ก ํ•˜์ง€์š”. >>> a = 3 >>> b = a + 2 ์œ„์˜ ๋ฌธ์žฅ์„ ๋ณด์„ธ์š”. b์˜ ๊ฐ’์ด ์–ผ๋งˆ๊ฐ€ ๋˜์—ˆ์„๊นŒ์š”? ์ด์ œ ์ดํ•ด๊ฐ€ ๊ฐ€์‹œ๊ฒ ์ฃ ? tip num = num + 1 ๋Œ€์‹  num += 1๋กœ ์จ๋„ ๋˜‘๊ฐ™์€ ์ผ์„ ํ•œ๋‹ต๋‹ˆ๋‹ค. while ๋ฌธ ์ˆ˜ํ–‰ ๊ณผ์ • ์ž, ์„ค๋ช…์„ ๋“œ๋ฆฌ๋‹ค ๋ณด๋‹ˆ ์ด์•ผ๊ธฐ๊ฐ€ ์ž ์‹œ ๋”ด ๋ฐ๋กœ ์ƒœ์Šต๋‹ˆ๋‹ค. ๋ณด์‹œ๊ธฐ ํŽธํ•˜๋„๋ก ์•ž์˜ while ๋ฌธ ์˜ˆ์ œ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ์จ๋ณผ๊ฒŒ์š”. >>> num = 1 >>> while num <= 100: ... print(num) ... num = num + 1 ... while์€ ์–ด๋–ค ์กฐ๊ฑด์ด ๋งŒ์กฑ๋˜๋Š” ๋™์•ˆ ๊ทธ ์•„๋ž˜์— ์“ด ๋ฌธ์žฅ๋“ค์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” num์ด 100์ด ๋  ๋•Œ๊นŒ์ง€ print(num)๊ณผ num = num + 1์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์ด์ง€์š”. ์ œ๊ฐ€ ๋ฐ˜๋ณต์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ์—” num ๊ฐ’์ด 1์ด๋‹ˆ๊นŒ 100๋ณด๋‹ค ์ž‘์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ทธ๋‹ค์Œ ๋ฌธ์žฅ์„ ์ˆ˜ํ–‰ํ•ด์•ผ๊ฒ ์ง€์š”? print(num)์ด๋‹ˆ๊นŒ ํ™”๋ฉด์— 1์„ ์ฐ๊ณ  num = num + 1 ํ•ด์„œ num์€ 2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ณ ๋Š” ๋‹ค์‹œ ์œ„์˜ while๋กœ ๋Œ์•„๊ฐ€์ง€์š”. ๊ทธ๋Ÿฌ๋ฉด num ๊ฐ’์ด 2์ด๋ฏ€๋กœ print(num)์ด 2๋ฅผ ์ฐ๊ณ  num = num + 1 ํ•ด์„œ num์€ 3์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์—” num ๊ฐ’์ด 3์ด๋ฏ€๋กœ print(num)์ด 3์„ ์ฐ๊ณ  num = num + 1 ํ•ด์„œ num์€ 4๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์—” num ๊ฐ’์ด 4์ด๋ฏ€๋กœ print(num)์ด 4๋ฅผ ์ฐ๊ณ  num = num + 1 ํ•ด์„œ num์€ 5๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ—‰ํ—‰ ํ—‰โ€ฆ ๊ทธ๋‹ค์Œ์—”, โ€ฆ ํ—‰ํ—‰ ํ—‰โ€ฆ ๊ทธ๋ ‡๊ฒŒ ํ•˜๋‹ค ๋ณด๋ฉด ์–ธ์  ๊ฐ€๋Š” num ๊ฐ’์ด 99๊นŒ์ง€ ์˜ฌ๋ผ๊ฐ‘๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋„ 100๋ณด๋‹ค๋Š” ์ž‘์œผ๋‹ˆ๊นŒ ๋˜ 99 ์ฐ๊ณ , num์€ ๋“œ๋””์–ด 100 ๋˜์ง€์š”. ์ด์ œ ๋˜๋‹ค์‹œ while ๋ฌธ์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค. while ๋‹ค์Œ์— ๋ญ๋ผ๊ณ  ์“ฐ์—ฌ์žˆ์ฃ ? num <= 100: ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. num์ด 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์„ ๋•Œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํ•˜๋˜ ์ผ์„ ๊ณ„์†ํ•ด์•ผ๊ฒ ์ฃ ? print(num) ํ•˜๋ฉด ํ™”๋ฉด์— 100์„ ์ฐ๊ณ  num = num + 1 ํ•ด์„œ num์—๋Š” 101์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์— while์„ ๋งŒ๋‚˜๋ฉด ์ด๋ฒˆ์—” num์ด 100๋ณด๋‹ค ํฌ๋‹ˆ๊นŒ ๊ทธ๋‹ค์Œ์˜ ๋ฌธ์žฅ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  ๋์ด ๋‚˜๊ณ ์•ผ ๋ง™๋‹ˆ๋‹ค. ์ฐ๋ ~. ์šฐ๋ฆฌ๋Š” ๋จธ๋ฆฌ๋ฅผ ์•ฝ๊ฐ„ ๊ตด๋ฆฌ๊ณ  ํ”„๋กœ๊ทธ๋žจ ๋„ค ์ค„๋งŒ ์น˜๋ฉด 1๋ถ€ํ„ฐ 100๊นŒ์ง€๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฐฑ๋งŒ, ์ฒœ๋งŒ, ์–ต๊นŒ์ง€๋„ ์ˆซ์ž๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋†€๋ž์ง€ ์•Š์Šต๋‹ˆ๊นŒ, ์—ฌ๋Ÿฌ๋ถ„? ๋ฏฟ๊ธฐ์ง€ ์•Š์œผ์‹ ๋‹ค๊ณ ์š”? ๊ทธ๋Ÿผ ํ•œ ๋ฒˆ ๋”ฐ๋ผ ํ•ด ๋ณด์‹ค๊นŒ์š”? while ๋ฌธ ์‹ค์Šต ๋จผ์ € ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์„ ๋„์šฐ๊ณ , ์ฒ˜์Œ ๋‘ ๋ฌธ์žฅ์„ ์ณ๋ณด์„ธ์š”. while ๋ฌธ ๋งˆ์ง€๋ง‰์— ์ฝœ๋ก (:)์ด ๊ผญ ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋‹ˆ ๋นผ๋จน์ง€ ๋งˆ์‹œ๊ณ ์š”. >>> num = 1 >>> while num <= 100: ... ๋‘˜์งธ ์ค„๊นŒ์ง€ ์น˜๋ฉด ๋‹ค์Œ ์ค„์— ์  ์„ธ ๊ฐœ๊ฐ€ ์ž๋™์œผ๋กœ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. while ๋ฌธ์€ ์—ฌ๋Ÿฌ ์ค„๋กœ ๊ตฌ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ์ค„์„ ๊ณ„์† ์ž…๋ ฅํ•˜๋ผ๋Š” ๋œป์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์ด์ง€์š”. ๊ทธ๋Ÿผ ๋‹ค์Œ ์ค„์„ ์ž…๋ ฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์  ์„ธ ๊ฐœ ๋’ค์— ๋ฐ”๋กœ ์“ฐ์ง€ ๋งˆ์‹œ๊ณ  ๊ณต๋ฐฑ ํ‚ค ๋„ค ๋ฒˆ, ๋˜๋Š” Tab ํ‚ค๋ฅผ ํ•œ ๋ฒˆ ๋ˆŒ๋Ÿฌ์„œ ๊ฐ„๊ฒฉ์„ ๋„์šด ๋‹ค์Œ์— ๋ช…๋ น์„ ์ž…๋ ฅํ•˜์„ธ์š”. ํŒŒ์ด์ฌ์—์„œ๋Š” ์—ฌ๋Ÿฌ ์ค„๋กœ ์ด๋ฃจ์–ด์ง„ ๋ช…๋ น์„ ์ž…๋ ฅํ•  ๋•Œ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ๋Š” ๋ฐ˜๋“œ์‹œ ๋“ค์—ฌ ์“ฐ๊ธฐ๋ฅผ ํ•ด์ค˜์•ผ ํ•˜๋‹ˆ๊นŒ์š”. ... print(num) ... num = num + 1 ... ๊ทธ๋ ‡๊ฒŒ ์…‹์งธ, ๋„ท์งธ ์ค„๊นŒ์ง€ ์น˜๊ณ  ... ์ด ๋‚˜์˜ค๋ฉด Enter๋ฅผ ํ•œ ๋ฒˆ ๋” ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”. ๊ทธ๋Ÿฌ๋ฉด ๋” ์ด์ƒ ์ž…๋ ฅํ•  ๊ฒƒ์ด ์—†๋‹ค๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  while ๋ฌธ์ด ๋๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ œ๋Œ€๋กœ ๋”ฐ๋ผ ํ•˜์…จ๋‹ค๋ฉด ์ˆœ์‹๊ฐ„์— 1๋ถ€ํ„ฐ 100๊นŒ์ง€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚  ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌํ•˜์—ฌ ์„๋ด‰์€ 100๊นŒ์ง€ ์ˆซ์ž๋ฅผ ์“ฐ๊ณ  ๊ทธ๋ฆฌ์šด ์–ด๋จธ๋‹ˆ ํ’ˆ์—์„œ ํŽธ์•ˆํžˆ ์ž ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค๋Š” ๋ง์”€. 2.1.1 ์—ฐ์Šต ๋ฌธ์ œ: ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ธฐ(while) ๋ฌธ์ œ input()์œผ๋กœ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜๋ฅผ ํ•œ ๊ฐœ ์ž…๋ ฅ๋ฐ›์•„, ๊ทธ ์ˆซ์ž๋ฅผ ์ˆซ์ž ํฌ๊ธฐ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์ด๋•Œ ์ถœ๋ ฅ ์•ž์— ๊ณต๋ฐฑ์„ ํ•œ ์นธ ์ฃผ์–ด์„œ, ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์ด ๊ตฌ๋ถ„๋˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, while ๋ฌธ์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ์˜ˆ 1 ์ž…๋ ฅ: ์ถœ๋ ฅ: 3 3 3 ์˜ˆ 2 ์ž…๋ ฅ: ์ถœ๋ ฅ: 5 5 5 5 5 ์ฝ”๋“œ: ch02/repeat_while.py 2.1.2 ์—ฐ์Šต ๋ฌธ์ œ: ์ œ๊ณฑํ‘œ(while) ๋ฌธ์ œ ์ •์ˆ˜๋ฅผ ํ•œ ๊ฐœ ์ž…๋ ฅ๋ฐ›์•„, 1๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๊นŒ์ง€ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์ œ๊ณฑ์„ ๊ตฌํ•ด ํ”„๋ฆฐํŠธํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”. ๋‹จ, while ๋ฌธ์„ ์‚ฌ์šฉํ•˜์„ธ์š”. 1 ์˜ˆ 1 ์ž…๋ ฅ: ์ถœ๋ ฅ: 1 1 2 4 3 9 ์˜ˆ 2 ์ž…๋ ฅ: ์ถœ๋ ฅ: 1 1 2 4 3 9 4 16 5 25 ์ฝ”๋“œ: ch02/square_table.py C ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ํ˜„๋Œ€์  ์ ‘๊ทผ์— ์†Œ๊ฐœ๋œ ๋‚ด์šฉ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ๋งŒ๋“ค์–ด ๋ดค์–ด์š”. โ†ฉ 2.1.3 ์—ฐ์Šต ๋ฌธ์ œ: ์–Œ์ฒด๊ณต ๋ฌธ์ œ ๊ณ ๋ฌด๊ณต์„ 100 ๋ฏธํ„ฐ ๋†’์ด์—์„œ ๋–จ์–ด๋œจ๋ฆฌ๋Š”๋ฐ, ์ด ๊ณต์€ ๋•…์— ๋‹ฟ์„ ๋•Œ๋งˆ๋‹ค ์›๋ž˜ ๋†’์ด์˜ 3/5 ๋งŒํผ ํŠ€์–ด ์˜ค๋ฆ…๋‹ˆ๋‹ค. ๊ณต์ด ์—ด ๋ฒˆ ํŠˆ ๋™์•ˆ, ๊ทธ๋•Œ๋งˆ๋‹ค ๊ณต์˜ ๋†’์ด๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. 1 1 60.0 2 36.0 3 21.599999999999998 4 12.959999999999999 5 7.775999999999999 6 4.6655999999999995 7 2.7993599999999996 8 1.6796159999999998 9 1.0077695999999998 10 0.6046617599999998 round() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์†Œ์ˆ˜์  ์•„๋ž˜ ๋„ค ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•ด ๋ณด์„ธ์š”. 1 60.0 2 36.0 3 21.6 4 12.96 5 7.776 6 4.6656 7 2.7994 8 1.6796 9 1.0078 10 0.6047 tip round()๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ˜์˜ฌ๋ฆผ์„ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. >>> round(1.23456, 2) # 1.23456์„ ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ๋กœ(์…‹์งธ ์ž๋ฆฌ์—์„œ) ๋ฐ˜์˜ฌ๋ฆผ 1.23 >>> round(1.23456, 3) # 1.23456์„ ์†Œ์ˆ˜์  ์…‹์งธ ์ž๋ฆฌ๋กœ(๋„ท์งธ ์ž๋ฆฌ์—์„œ) ๋ฐ˜์˜ฌ๋ฆผ 1.235 ์ฐธ๊ณ ๋กœ, round(2.675, 2)๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๊ฒฐ๊ณผ๊ฐ€ 2.68์ด ์•„๋‹Œ 2.67๋กœ ๋‚˜์˜ค๋Š”๋ฐ, ์ด๊ฒƒ์€ ๋ฒ„๊ทธ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ถ€๋™์†Œ์ˆ˜์ (floating point) ์—ฐ์‚ฐ์˜ ํ•œ๊ณ„์ž…๋‹ˆ๋‹ค. >>> round(2.675, 2) 2.67 ์ด ๋ฌธ์ œ์˜ ํ•ด๋‹ต์€ ์•„๋ž˜ ์ฃผ์†Œ์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ: ch02/ball.py ๋‹ต์„ ๋ณด์ง€ ๋ง๊ณ  ์ง์ ‘ ํ’€์–ด ๋ณด๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์ฃ ! (ํŒŒ์ด์ฌ 2๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๋ถ„์€ ํ•ด๋‹ต ์ฝ”๋“œ์—์„œ 3/5๋ฅผ 3.0/5.0์œผ๋กœ ๋ฐ”๊ฟ”์„œ ํ•ด ๋ณด์„ธ์š”) ์ œ๊ฐ€ ๋ฒˆ์—ญํ•œ <์‹ค์šฉ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ>์— ์‹ค๋ฆฐ ๋ฌธ์ œ๊ฐ€ ์žฌ๋ฏธ์žˆ์–ด์„œ ๊ฐ€์ ธ์˜จ ๊ฒƒ์ž…๋‹ˆ๋‹ค. โ†ฉ 2.1.4 ์—ฐ์Šต ๋ฌธ์ œ: ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์‹คํ–‰ ๊ฒฐ๊ณผ ๋งžํžˆ๊ธฐ ๋ฌธ์ œ ๋‹ค์Œ ์ฝ”๋“œ 1์„ ์ฝ๊ณ , ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์•Œ์•„๋งžํ˜€ ๋ณด์„ธ์š”. number = 358 rem = rev = 0 while number >= 1: rem = number % 10 rev = rev * 10 + rem number = number // 10 print(rev) ํ’€์ด ์ง์ ‘ ์‹คํ–‰ํ•ด์„œ ์˜ˆ์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์„ธ์š”. ์ฝ”๋“œ: http://bit.ly/3peNrR2 ํ’€์ด ์˜์ƒ: https://youtu.be/OAxEFA44jU4 edx.org์˜ Linux Basics: The Command Line Interface์— ๋‚˜์˜ค๋Š” ์—ฐ์Šต ๋ฌธ์ œ์˜ C ์–ธ์–ด ์ฝ”๋“œ๋ฅผ ํŒŒ์ด์ฌ์œผ๋กœ ์˜ฎ๊ธด ๊ฒƒ์ž…๋‹ˆ๋‹ค. โ†ฉ 2.2 ์กฐ๊ฑด๋ฌธ(if-elif-else) ์ง€๊ธˆ๊นŒ์ง€ ์ €์™€ ํ•จ๊ป˜ ํŒŒ์ด์ฌ์„ ์•Œ์•„๊ฐ€๋ฉด์„œ ์–ด๋–ค ์ƒ๊ฐ์ด ๋“œ์…จ๋‚˜์š”? ๋„ˆ๋ฌด ์‰ฝ๋‹ค๋Š” ๋ถ„๋„ ๊ณ„์‹ค ํ…Œ๊ณ , ์ด๋Ÿฐ ๊ฒƒ๋“ค ๋ฐฐ์›Œ์„œ ์–ด๋””์— ์จ๋จน๋Š” ๊ฑด์ง€ ๊ถ๊ธˆํ•œ ๋ถ„๋„ ๊ณ„์‹ค ๊ฒƒ ๊ฐ™๋„ค์š”. ์ด ๊ฐ•์˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ „ํ˜€ ๋ชจ๋ฅด๋Š” ๋ถ„์„ ์œ„ํ•ด ์ตœ๋Œ€ํ•œ ์‰ฝ๊ฒŒ ์“ฐ๋ ค๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์–ธ์–ด๋ฅผ ์ ‘ํ•ด๋ณด์‹  ๋ถ„์—๊ฒŒ๋Š” ์ง€๋ฃจํ•  ๊ฒƒ ๊ฐ™๋„ค์š”. ๊ทธ๋Ÿฐ ๋ถ„์ด๋ผ๋ฉด ์•„๋งˆ ์—ฌ๊ธฐ๊นŒ์ง€ ์ฝ๊ธฐ ์ „์— ๋‹ค๋ฅธ ์‚ฌ์ดํŠธ๋ฅผ ์ฐพ์•„๊ฐ€์…จ๊ฒ ์ฃ ? ๋˜, ํŒŒ์ด์ฌ์„ ๋ฐฐ์›Œ์„œ ์–ด๋””์— ์จ๋จน๋Š๋ƒ๊ณ  ํ•˜์‹ ๋‹ค๋ฉดโ€ฆ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“œ๋Š” ๋ฐ ์“ฐ์ง€์š”. ์›น์‚ฌ์ดํŠธ๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ๋„ ์”๋‹ˆ๋‹ค. ์ง€๊ธˆ ๋ฐฐ์šฐ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ํ•˜์ฐฎ์€ ๊ฒƒ๋“ค์ด ๋ชจ์—ฌ์„œ ์—„์ฒญ๋‚œ ํ”„๋กœ๊ทธ๋žจ๋„ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ๊ฒƒ์ด์ง€์š”. ์กฐ๊ทธ๋งŒ ๋ ˆ๊ณ  ๋ธ”๋ก๋“ค์ด ๋ชจ์—ฌ์„œ ํฐ ๋ชจํ˜•์„ ์ด๋ฃจ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฐจ๊ทผ์ฐจ๊ทผ ๊ณต๋ถ€ํ•ด๊ฐ€๋‹ค ๋ณด๋ฉด ์ ์  ๋” ๋ณต์žกํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ์‹ค ์ˆ˜ ์žˆ์„ ๊ฑฐ์˜ˆ์š”. ๊ทธ๋Ÿผ ๋˜ ์ƒˆ๋กœ์šด ๊ฒƒ์„ ๋ฐฐ์›Œ ๋ณผ๊นŒ์š”? ์ด๋ฒˆ์—” if ๋ฌธ์ž…๋‹ˆ๋‹ค. If๋Š” '๋งŒ์•ฝ โ€ฆ์ด๋ฉด'์ด๋ผ๋Š” ๋œป์ด์ง€์š”? ํŒŒ์ด์ฌ์—์„œ๋„ ๊ฐ™์€ ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. "๋‹ฌ๋ฉด ์‚ผํ‚ค๊ณ  ์“ฐ๋ฉด ๋ฑ‰๋Š”๋‹ค."๋ผ๋Š” ์†๋‹ด์ด ์žˆ์ง€์š”. ๊ทธ๊ฒƒ์„ ํŒŒ์ด์ฌ์—์„œ๋Š” ์“ฐ๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์จ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‹ฌ๋‹ค๋ฉด: ์‚ผํ‚จ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด: ๋ฑ‰๋Š”๋‹ค. ์ด๋ฒˆ์—” ์˜์–ด๋ฅผ ์กฐ๊ธˆ ์„ž์–ด์„œ ์จ๋ณผ๊นŒ์š”? if ๋‹ฌ๋‹ค๋ฉด: ์‚ผํ‚จ๋‹ค. else: ๋ฑ‰๋Š”๋‹ค. ์œ„์— ๋“  ์˜ˆ๋“ค์€ ์„ค๋ช…์„ ์œ„ํ•ด์„œ ์จ ๋ณธ ๊ฒƒ์ธ๋ฐ, ๊ทธ๋Œ€๋กœ ์ž‘์„ฑํ•˜๋ฉด ํŒŒ์ด์ฌ์ด ์ดํ•ด๋ฅผ ๋ชป ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ if์™€ else ๊ทธ๋Ÿผ ์ด๋ฒˆ์—” ์‹ค์Šต์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋‘ ์ˆ˜ a์™€ b ์ค‘์— ์–ด๋Š ์ชฝ์ด ๋” ํด๊นŒ์š”? >>> a = 1234 * 4 >>> b = 13456 / 2 if ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ a๊ฐ€ ํฌ๋ฉด 'a'๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  b๊ฐ€ ํฌ๋ฉด 'b'๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณผ๊นŒ์š”? ํ•œ๋ฒˆ ๋”ฐ๋ผ์„œ ์ณ๋ณด์„ธ์š”. >>> if a > b: # ๋งŒ์•ฝ a๊ฐ€ b๋ณด๋‹ค ํฌ๋ฉด ... print('a') # 'a'๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ... else: # ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ... print('b') # 'b'๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ... # ์ดํ›„์— ์žˆ๋Š” ๊ฒƒ๋“ค์€ ์ฃผ์„(์„ค๋ช…)์ด๋ฏ€๋กœ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. a > b๋ผ๊ณ  ์“ด ๊ฒƒ์€ โ€˜a๊ฐ€ b๋ณด๋‹ค ํฐ๊ฐ€?โ€™๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์–ด๋ ต์ง€ ์•Š์ฃ ? elif ์กฐ๊ฑด์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ฃผ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—” c์™€ d๋ฅผ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> c = 15 * 5 >>> d = 15 + 15 + 15 + 15 + 15 >>> if c > d: ... print('c is greater than d') ... elif c == d: ... print('c is equal to d') ... elif c < d: ... print('c is less than d') ... else: ... print('I don\'t know') ... c is equal to d ์ด๋ ‡๊ฒŒ elif๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•ด์„œ ๊ทธ์ค‘์—์„œ ๋ง˜์— ๋“œ๋Š” ๊ฒƒ์„ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. == ์—ฐ์‚ฐ์ž ์—ฌ๊ธฐ์„œ ์ƒˆ๋กœ์šด ๊ฒƒ์ด ๋˜ ์žˆ๋Š”๋ฐ, ๋ฐ”๋กœ ==(๋“ฑํ˜ธ ๋‘ ๊ฐœ)์ž…๋‹ˆ๋‹ค. ==๋Š” ์ง€๊ธˆ๊นŒ์ง€ ์•Œ๊ณ  ์žˆ๋˜ =(๋“ฑํ˜ธ ํ•œ ๊ฐœ)์™€๋Š” ์“ฐ์ž„์ƒˆ๊ฐ€ ๋‹ค๋ฅด๋‹ˆ ํ˜ผ๋™ํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•˜์„ธ์š”. c == d๋ผ๊ณ  ์“ฐ๋ฉด 'c์™€ d์˜ ๊ฐ’์ด ๊ฐ™์€๊ฐ€?'๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ง€๊ธˆ์ฒ˜๋Ÿผ ๋‘ ๊ฐ’์„ ๋น„๊ตํ•  ๋•Œ ์‚ฌ์šฉํ•˜์ง€์š”. ์ง€๊ธˆ๊นŒ์ง€ ๋“ฑํ˜ธ ํ•˜๋‚˜๋ฅผ ์จ์„œ c = d๋ผ๊ณ  ์“ด ๊ฒƒ์€ d์˜ ๊ฐ’์„ c์— ๋„ฃ์œผ๋ผ๋Š” ๋œป์ด์—ˆ๊ณ ์š”. >>> watch = 1000000 ๊ธฐ์–ต๋‚˜์‹œ์ฃ ? ์ด์ œ ๊ทธ ๋‘˜์„ ๊ตฌ๋ณ„ํ•˜์‹ค ์ˆ˜ ์žˆ๊ฒ ์ฃ ? if ๋ฌธ ์ž…๋ ฅ<NAME>์ƒ https://youtu.be/pspPgQJ6CFE ๋‚˜๋จธ์ง€ ๊ณ„์‚ฐ์„ ์ด์šฉํ•˜๋Š” if ๋ฌธ ์–ด๋–ค ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ์ˆ˜๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๋ฉด โ€˜๋‚˜๋ˆ„์–ด๋–จ์–ด์ง„๋‹คโ€™๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 48์„ 4๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋Š” 0์ด๋ฏ€๋กœ, 48์€ 4๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง‘๋‹ˆ๋‹ค. >>> 48 % 4 ์–ด๋–ค ์ˆ˜ a๊ฐ€ ๋‹ค๋ฅธ ์ˆ˜ b๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š”์ง€๋ฅผ ํŒŒ์ด์ฌ์˜ if ๋ฌธ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> a = 48 >>> b = 4 >>> if a % b == 0: ... print(f'{a}๋Š” {b}๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง‘๋‹ˆ๋‹ค.') ... elif a % b != 0: ... print(f'{a}๋Š” {b}๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.') ... 48๋Š” 4๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง‘๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ elif a % b != 0: ๋Œ€์‹  else:๋ผ๊ณ  ํ•ด๋„ ๊ฒฐ๊ณผ๋Š” ๊ฐ™๊ฒ ์ฃ ? ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ˜๋ณต๋ฌธ ์ค‘๋‹จํ•˜๊ธฐ ์–ด๋ฆด ๋•Œ๋Š” ํฐ ์ˆ˜๋ฅผ ์ž˜ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜์ฃ ? ํ•˜๋‚˜๋ถ€ํ„ฐ ์—ด๊นŒ์ง€๋ฐ–์— ๋ชจ๋ฅด๋Š” ์•„์ด์ฒ˜๋Ÿผ, 10๋ณด๋‹ค ํฐ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๋ฉˆ์ถ”๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•ด ๋ณผ๊นŒ์š”? # filename: ten.py max = 10 while True: num = int(input()) if num > max: print(num, 'is too big!') break ์ด์™€ ๊ฐ™์ด ๋ฐ˜๋ณต๋ฌธ์—์„œ break๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋น ์ ธ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. ์ž…๋ ฅ: 6 12 ์ถœ๋ ฅ: 12 is too big! ์˜ค๋Š˜์˜ ๊ฐ•์˜๋Š” ์—ฌ๊ธฐ๊นŒ์ง€์ž…๋‹ˆ๋‹ค. ๊ฐ•์˜๋Š” ์ดํ•ด๊ฐ€ ๋œ๋‹ค๊ณ  ํ•ด์„œ ๊ทธ๋ƒฅ ํ›‘์–ด๋ณด์ง€ ๋งˆ์‹œ๊ณ  ๊ผญ ์˜ˆ์ œ๋ฅผ ๋”ฐ๋ผ์„œ ์ณ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฒƒ๊ณผ ๋น„์Šทํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์Šค์Šค๋กœ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. '๋ฐฑํƒ€๊ฐ€ ๋ถˆ์—ฌ์ผ์ž‘'์ด๋ผ๋Š” ๋ง๋„ ์žˆ๊ฑฐ๋“ ์š”. ๋ฐฑ ๋ฒˆ ๋”ฐ๋ผ ํ•ด๋ณด๋Š” ๊ฒƒ๋ณด๋‹ค ํ•œ ๋ฒˆ ์ง์ ‘ ๋งŒ๋“ค์–ด ๋ณด๋Š” ๊ฒƒ์ด ๋‚ซ๋‹ค ๋โ€ฆ 2.2.1 ์—ฐ์Šต ๋ฌธ์ œ: ์ˆซ์ž ์ฝ๊ธฐ(1~3) ๋ฌธ์ œ input()์„ ์‚ฌ์šฉํ•ด ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๋ฅผ ํ•œ๊ธ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, ์‚ฌ์šฉ์ž๋Š” 1 ์ด์ƒ 3 ์ดํ•˜์˜ ์ •์ˆ˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ 1 ์ž…๋ ฅ: ์ถœ๋ ฅ: ์˜ˆ 2 ์ž…๋ ฅ: ์ถœ๋ ฅ: ๋‹ต ์ฝ”๋“œ: ch02/korean_1_to_3.py 2.2.2 ์—ฐ์Šต ๋ฌธ์ œ: ๋‚˜์ด์— ๋”ฐ๋ฅธ ์„ธ๋Œ€ ๊ตฌ๋ถ„ (1) ๋ฌธ์ œ input()์œผ๋กœ ์‚ฌ์šฉ์ž์˜ ๋‚˜์ด๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ํ›„, ๋‹ค์Œ ํ‘œ์˜ ์–ด๋Š ์„ธ๋Œ€์— ์†ํ•˜๋Š”์ง€ ์ถœ๋ ฅํ•˜์„ธ์š”. ์ž…์ถœ๋ ฅ ๋ฌธ๊ตฌ๋Š” ์ž์œ ๋กญ๊ฒŒ ์ง€์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ถœ์ƒ ์—ฐ๋„ ์„ธ๋Œ€ 1 ~1924 ๊ฐ€์žฅ ์œ„๋Œ€ํ•œ ์„ธ๋Œ€(The Greatest Generation) 1925~1945 ์นจ๋ฌต ์„ธ๋Œ€(The Silent Generation) 1946~1964 ๋ฒ ์ด๋น„๋ถ ์„ธ๋Œ€(baby boomer) 1965~1980 X์„ธ๋Œ€(Generation X) 1981~1996 ๋ฐ€๋ ˆ๋‹ˆ์–ผ(millennial) 1997~ Z์„ธ๋Œ€(Generation Z) $ python3 generations1.py What year were you born? 1945 You're the Silent Generation. $ python3 generations1.py What year were you born? 1946 You're a baby boomer. $ python3 generations1.py What year were you born? 1964 You're a baby boomer. $ python3 generations1.py What year were you born? 1965 You're a Gen X. ์ฝ”๋“œ: ch02/generations1.py ใ€ˆ American Generations Fast Facts ใ€‰, CNN โ†ฉ 2.2.3 ์—ฐ์Šต ๋ฌธ์ œ: ๋‹จ์œ„ ๊ธฐํ˜ธ ๊ธธ์ด, ๋ถ€ํ”ผ, ๋ฌด๊ฒŒ๋‚˜ ๊ธˆ์•ก์„ ํ‘œ๊ธฐํ•  ๋•Œ 1000์„ โ€˜kโ€™๋กœ ํ‘œ๊ธฐํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 3000m๋Š” 3km๋กœ ํ‘œ๊ธฐํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๊ฐ€ 1000๋ณด๋‹ค ํฌ๋ฉด 1์ž๋ฆฌ๋ถ€ํ„ฐ 100์ž๋ฆฌ๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์ƒ๋žตํ•˜๊ณ  โ€˜kโ€™๋ฅผ ๋ถ™์—ฌ์ฃผ๋Š” ํŒŒ์ด์ฌ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ๋ช…์€ affix.py๋กœ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1 num = int(input()) 2 result = str(num) 3 4 if num >= 1000: 5 result = str(num // 1000) + 'k' 6 elif num >= 0: 7 pass 8 9 print(result) 10 ์ฝ”๋“œ ์„ค๋ช…: 1๋ฒˆ์งธ ์ค„: ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ์ •์ˆ˜(int)๋กœ ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์ˆซ์ž๋ผ๋ฆฌ ๋น„๊ตํ•  ๋•Œ ์“ฐ๋ ค๊ณ  ๊ทธ๋ ‡๊ฒŒ ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 2๋ฒˆ์งธ ์ค„: if ๋ฌธ์˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ๋ณ€์ˆ˜๋ฅผ result๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. 4~5๋ฒˆ์งธ ์ค„: ์ˆซ์ž๊ฐ€ 1000 ์ด์ƒ์ธ์ง€ ๊ฒ€์‚ฌํ•ด์„œ ๊ฒฐ๊ด๊ฐ’(result)์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. 1~100์ž๋ฆฌ ์ˆซ์ž๋ฅผ ์ƒ๋žตํ•˜๋ ค๊ณ  ์ •์ˆ˜์˜ ๋‚˜๋ˆ—์…ˆ(//)์„ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. 6~7๋ฒˆ์งธ ์ค„: ์ˆซ์ž๊ฐ€ (1000๋ณด๋‹ค ์ž‘๊ณ ) 0 ์ด์ƒ์ด๋ฉด ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค(pass). 9๋ฒˆ์งธ ์ค„: result๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์‹คํ–‰ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋‚˜ ํ„ฐ๋ฏธ๋„์—์„œ python affix.pyEnter๋ฅผ ์ž…๋ ฅํ•˜๊ณ , 1000Enter๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 1k๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. >python affix.py 1000 1k ๋ฌธ์ œ 1. ๋ฐฑ๋งŒ ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•˜์„ ๋•Œ 1~10๋งŒ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ์ƒ๋žตํ•˜๊ณ  โ€˜Mโ€™์„ ๋ถ™์—ฌ์„œ ์ถœ๋ ฅํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด์„ธ์š”. >python affix.py 1000000 1M 2. ์„ฑ๊ณตํ–ˆ๋‹ค๋ฉด, ๊ทธ ์ด์ƒ๋„ ๊ตฌํ˜„ํ•ด ๋ณด์„ธ์š”(10์˜ ์Šน์ˆ˜ ๋‹จ์œ„ ์ฐธ์กฐ). >python affix.py 1000000000 1G ... ํ’€์ด ch02/affix.py 2.2.4 ์—ฐ์Šต ๋ฌธ์ œ: ์–‘์ˆ˜๋งŒ ๋ง์…ˆํ•˜๊ธฐ ๋ฌธ์ œ input()์œผ๋กœ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ •์ˆ˜๋ฅผ ๊ณ„์† ๋”ํ•ด๋‚˜๊ฐ€๋‹ค๊ฐ€, ์Œ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์ค‘๋‹จํ•˜๊ณ  ๊ทธ์ „๊นŒ์ง€ ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์ž…๋ ฅ: 2 -1 ์ถœ๋ ฅ: ์˜ˆ 2 ์ž…๋ ฅ: 50 60 70 -100 ์ถœ๋ ฅ: 180 ์ฝ”๋“œ: ch02/sum_positive.py 2.2.5 ์—ฐ์Šต ๋ฌธ์ œ: ์œค๋…„ ํŒ๋ณ„ํ•˜๊ธฐ ๋ฌธ์ œ ์œค๋…„์€ ์—ญ๋ฒ•์„ ์‹ค์ œ ํƒœ์–‘๋…„์— ๋งž์ถ”๊ธฐ ์œ„ํ•ด ์—ฌ๋ถ„์˜ ํ•˜๋ฃจ ๋˜๋Š” ์›”์„ ๋ผ์šฐ๋Š” ํ•ด์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ์‚ฌ์šฉํ•˜๋Š” ๊ทธ๋ ˆ๊ณ ๋ฆฌ๋ ฅ์˜ ์œค๋…„ ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์„œ๋ ฅ๊ธฐ์› ์—ฐ์ˆ˜๊ฐ€ 4๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š” ํ•ด๋Š” ์œค๋…„์œผ๋กœ ํ•œ๋‹ค. (1988๋…„, 1992๋…„, 1996๋…„, 2004๋…„, 2008๋…„, 2012๋…„, 2016๋…„, 2020๋…„, 2024๋…„, 2028๋…„, 2032๋…„, 2036๋…„, 2040๋…„, 2044๋…„ ...) ์„œ๋ ฅ๊ธฐ์› ์—ฐ์ˆ˜๊ฐ€ 4, 100์œผ๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š” ํ•ด๋Š” ํ‰๋…„์œผ๋กœ ํ•œ๋‹ค. (1900๋…„, 2100๋…„, 2200๋…„, 2300๋…„, 2500๋…„...) ์„œ๋ ฅ๊ธฐ์› ์—ฐ์ˆ˜๊ฐ€ 4, 100, 400์œผ๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š” ํ•ด๋Š” ์œค๋…„์œผ๋กœ ๋‘”๋‹ค. (2000๋…„, 2400๋…„...) ์ถœ์ฒ˜: ์œ„ํ‚ค๋ฐฑ๊ณผ ์ด ๊ทœ์น™์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ๋ฆ„๋„๋ฅผ ๊ทธ๋ ค๋ณด์•˜์Šต๋‹ˆ๋‹ค. 1 ์ด ๊ทœ์น™์— ๋”ฐ๋ผ, ์—ฐ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„์„œ ์œค๋…„์ธ์ง€ ์•„๋‹Œ์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”. ์ฝ”๋“œ: ch02/leap_year.py ์ฝ”๋“œ: ์ œ๊ฐ€ ํ’€์ดํ•œ<NAME>์ƒ์„ ์œ ํŠœ๋ธŒ์— ์˜ฌ๋ ค ๋‘์—ˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์„ธ์š”. https://youtu.be/PZlyWHAvDCQ ์ฐธ๊ณ  ์ œ๊ฐ€ ํ‘ผ ๊ฒƒ๋ณด๋‹ค ์ข€ ๋” ๋‚˜์€ ํ’€์ด๋ฅผ ๋ณด๋‚ด์ฃผ์‹  ๋…์ž๊ฐ€ ์žˆ์–ด์„œ ํ…Œ์ŠคํŒ…์—์„œ ์†Œ๊ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—๋Š” ์ฃผ์–ด์ง„ ํ•ด๊ฐ€ ์œค๋…„์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋“ˆ ์‚ฌ์šฉ๋ฒ• ์•Œ์•„๋‚ด๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ด…๋‹ˆ๋‹ค. Flowgorithm์„ ์‚ฌ์šฉํ•ด์„œ ๊ทธ๋ ธ์Šต๋‹ˆ๋‹ค. Flowgorithm์— ๋Œ€ํ•ด ๋” ์•Œ๊ณ  ์‹ถ์€ ๋ถ„์€ ใ€Šํ๋ฆ„๋Œ€๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š” Flowgorithm ใ€‹์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. โ†ฉ 2.2.6 and/or ์—ฐ์‚ฐ์ž ์ด๋ฒˆ์—๋Š” ์กฐ๊ฑด๋ฌธ์—์„œ ๋งŽ์ด ์“ฐ์ด๋Š” and์™€ or ์—ฐ์‚ฐ์ž๋ฅผ ์•Œ์•„๋ณผ๊ฒŒ์š”. if ๋ฌธ์— and/or๋ฅผ ์‚ฌ์šฉ if ๋ฌธ์— and, or๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌธ์ž์—ด s๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ, >>> s = 'banana' ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ค‘์ฒฉ๋œ if ๋ฌธ์€, >>> if 'a' in s: ... if 'b' in 'banana': ... print('banana์—๋Š” a๋„ ์žˆ๊ณ  b๋„ ์žˆ์–ด์š”!') ... banana์—๋Š” a๋„ ์žˆ๊ณ  b๋„ ์žˆ์–ด์š”! and๋ฅผ ์จ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> if 'a' in s and 'b' in s: ... print('banana์—๋Š” a๋„ ์žˆ๊ณ  b๋„ ์žˆ์–ด์š”!') ... banana์—๋Š” a๋„ ์žˆ๊ณ  b๋„ ์žˆ์–ด์š”! ์ค‘์ฒฉ๋˜์—ˆ๋˜ if ๋ฌธ์„ if ํ•œ ๋ฒˆ์œผ๋กœ ์ค„์˜€์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ€ด์ฆˆ! and ๋Œ€์‹  or๋ฅผ ์จ์„œ, โ€˜banana์—๋Š” a ๋˜๋Š” c๊ฐ€ ์žˆ์–ด์š”!โ€™๋ผ๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์žฅ์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. if ๋ฌธ ์—†์ด and/or๋งŒ ์‚ฌ์šฉ ์‚ฌ์‹ค, and์™€ or๋Š” ๋ฐ˜๋“œ์‹œ if ๋ฌธ์— ๋“ค์–ด๊ฐ€์•ผ๋งŒ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์ด and ๋˜๋Š” or๋ฅผ ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ต๋‹ˆ๋‹ค. >>> 'a' in s True >>> 'b' in s True >>> 'c' in s False ์ด๋Ÿฌํ•œ True/False ๊ฐ’์„ ๋ถˆ(bool)์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ๋ถˆ๊ฐ’์„ ๋ณ€์ˆ˜์— ๋„ฃ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> a_in_s = 'a' in s >>> a_in_s True and/or ์—ฐ์‚ฐ ์ˆœ์„œ ํŒŒ์ด์ฌ์—์„œ๋Š” and/or์˜ ์™ผ์ชฝ ํ•ญ์„ ๋จผ์ € ํ‰๊ฐ€(๊ณ„์‚ฐ) ํ•˜๊ณ , ์˜ค๋ฅธ์ชฝ ํ•ญ์€ ํ•„์š”ํ•  ๋•Œ๋งŒ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค(๋Œ€๋ถ€๋ถ„์˜ ๊ณ ๊ธ‰ ์–ธ์–ด์—์„œ ์ด๋ ‡๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค). ์˜ˆ๋ฅผ ๋“ค์–ด๋ณผ๊ฒŒ์š”. a์™€ b ๊ฐ’์ด ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๋•Œ, >>> a = 3 >>> b = 0 b ๊ฐ’์ด 0์ด๋ฏ€๋กœ b๋ฅผ ๋ถ„๋ชจ๋กœ ํ•˜์—ฌ ๋‚˜๋ˆ—์…ˆ์„ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ZeroDivisionError๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. >>> a / b Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: division by zero ๊ทธ๋Ÿฐ๋ฐ ์ด ๋‚˜๋ˆ—์…ˆ ๊ณ„์‚ฐ์„ ์•„๋ž˜์ฒ˜๋Ÿผ and์˜ ์˜ค๋ฅธ์ชฝ ํ•ญ์— ๋„ฃ์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. >>> (a * b) > 0 and (a / b) > 0 False ํŒŒ์ด์ฌ์ด โ€˜์™ผ์ชฝ ํ•ญ์„ ํ‰๊ฐ€ํ•ด ๋ณด๋‹ˆ, ์˜ค๋ฅธ์ชฝ ํ•ญ์€ ํ‰๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†๊ฒ ๊ตฌ๋‚˜โ€™ ํ•˜๊ณ  ๋„˜์–ด๊ฐ€ ๋ฒ„๋ฆฐ ๊ฒƒ์ด์ฃ . ์œ„๋ฌธ์žฅ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”์„œ ๋‚˜๋ˆ—์…ˆ์„ ๋จผ์ € ์‹œ์ผœ๋ณด๋ฉด ZeroDivisionError๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> (a / b) > 0 and (a * b) > 0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: division by zero 2.2.7 ์—ฐ์Šต ๋ฌธ์ œ: ๋‚˜์ด์— ๋”ฐ๋ฅธ ์„ธ๋Œ€ ๊ตฌ๋ถ„ (2) ๋ฌธ์ œ ๋ฏธ๊ตญ๊ณผ ๋‹ฌ๋ฆฌ, ์šฐ๋ฆฌ๋‚˜๋ผ์—์„œ๋Š” ๋ณดํ†ต 1955~1963๋…„์ƒ์„ โ€˜๋ฒ ์ด๋น„๋ถ ์„ธ๋Œ€โ€™๋กœ ๋ด…๋‹ˆ๋‹ค. 1 ์‚ฌ์šฉ์ž๊ฐ€ ํ•œ๊ตญ์ธ์ธ์ง€์— ๋”ฐ๋ผ ์„ธ๋Œ€๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์„ ๊ณ ์ณ ๋ณด์„ธ์š”. (๋ฌธ์ œ๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์œ„ํ•ด, ์‚ฐ์—…ํ™” ์„ธ๋Œ€์™€ 386 ์„ธ๋Œ€ 2๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‚ฌ์šฉ์ž๋Š” ๋ฏธ๊ตญ์ธ ๋˜๋Š” ํ•œ๊ตญ์ธ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.) $ python3 generations2.py What year were you born? 1954 Are you Korean?(y/n) y You're the Silent Generation. $ python3 generations2.py What year were you born? 1954 Are you Korean?(y/n) n You're a baby boomer. $ python3 generations2.py What year were you born? 1955 You're a baby boomer. $ python3 generations2.py What year were you born? 1963 You're a baby boomer. $ python3 generations2.py What year were you born? 1964 Are you Korean?(y/n) n You're a baby boomer. $ python3 generations2.py What year were you born? 1964 Are you Korean?(y/n) y You're a Gen X. tip ๋ฌธ์ž์—ด์˜ lower() ๋ฉ”์„œ๋“œ๋Š” ์˜๋ฌธ ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๊ฐ’์„ ๋Œ๋ ค์ค๋‹ˆ๋‹ค. >>> 'Yes'.lower() 'yes' ๋ฌธ์ž์—ด ๋’ค์— [0]์„ ๋ถ™์ด๋ฉด ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> 'Yes'.lower()[0] 'y' ํ’€์ด ์ €๋„ ๋ฌธ์ œ ๋งŒ๋“ค๊ณ  ํ’€๋ฉด์„œ ์ข€ ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ์š”, ์กฐ๊ฑด์„ ๋”ฐ์ง€๊ธฐ ๋ณต์žกํ•  ๋• ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œ๋ฅผ ๋จผ์ € ๋งŒ๋“ค๊ณ  ๋‚˜์„œ ์กฐ๊ฑด ๋…ผ๋ฆฌ(conditional logic)๋ฅผ ์„ธ์šฐ๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. ์ €๋Š” ์ฝ”๋“œ๋ฅผ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ: ch02/generations2.py ์นจ๋ฌต ์„ธ๋Œ€์™€ ๋ฒ ์ด๋น„๋ถ ์„ธ๋Œ€๋ฅผ ํŒ์ •ํ•˜๋Š” ์ฝ”๋“œ์— ๊ตญ์ ์„ ๋ฌป๋Š” ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฒ ์ด๋น„๋ถ ์„ธ๋Œ€ ํ™•์ธ ์ฝ”๋“œ๋ฅผ ์„ค๋ช…๋“œ๋ฆฌ๋ฉด, โ‘  1963๋…„์ƒ๊นŒ์ง€๋Š” ๋ฏธ๊ตญ์ธ๊ณผ ํ•œ๊ตญ์ธ ๋ชจ๋‘ ๋ฒ ์ด๋น„๋ถ ์„ธ๋Œ€์ด๋ฏ€๋กœ ๊ตญ์ ์„ ๋ฌป์ง€ ์•Š์•„๋„ ๋˜๊ณ , โ‘ก 1964๋…„์ƒ์ด๋ฉด์„œ โ‘ข ๋ฏธ๊ตญ์ธ์ผ ๊ฒฝ์šฐ(ํ•œ๊ตญ์ธ์ด ์•„๋‹ ๊ฒฝ์šฐ) โ‘ฃ ๋ฒ ์ด๋น„๋ถ ์„ธ๋Œ€์ž…๋‹ˆ๋‹ค. elif y <= 1963 or ( # โ‘  y <= 1964 and # โ‘ก input("Are you Korean?(y/n) ").lower()[0] != 'y' # โ‘ข ): gen = 'a baby boomer' # โ‘ฃ ์ฐธ๊ณ ๋กœ ํŒŒ์ด์ฌ์—์„œ A or B ํ˜•ํƒœ์˜ ์กฐ๊ฑด์„ ํ‰๊ฐ€ํ•  ๋•Œ๋Š”, A๋ฅผ ๋จผ์ € ํ‰๊ฐ€ํ•ด์„œ A๊ฐ€ ์ฐธ์ด๋ฉด B๋ฅผ ํ‰๊ฐ€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด A๊ฐ€ ์ฐธ์ด๋ฉด, B๊ฐ€ ์ฐธ์ธ์ง€ ์•„๋‹Œ์ง€์— ๊ด€๊ณ„์—†์ด ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ด๋ฏ€๋กœ, B๋ฅผ ํ‰๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  โ‘ข์—์„œ != ๋Œ€์‹  ==๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฏธ๊ตญ์ธ์ด๋ƒ๊ณ  ๋ฌผ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ด๋ ‡๊ฒŒ ํ—ท๊ฐˆ๋ฆฌ๋Š” ์กฐ๊ฑด๋ฌธ์„ ๋งŒ๋“ค๋ฉด ๋ฒ„๊ทธ๊ฐ€ ์ƒ๊ธฐ๊ธฐ ์‰ฝ๊ณ  ๋””๋ฒ„๊น…๋„ ์–ด๋ ค์›Œ์„œ ์ถ”์ฒœํ•˜๋Š” ๋ฐฉ์‹์€ ์•„๋‹ˆ์ง€๋งŒ, ๋•Œ๋กœ๋Š” ๋ถˆ๊ฐ€ํ”ผํ•˜๊ฒŒ ๋ณต์žกํ•œ ์กฐ๊ฑด์„ ๋”ฐ์ ธ์•ผ ํ•  ๋•Œ๋„ ์žˆ์œผ๋ฏ€๋กœ ๊ทธ๋Ÿฌํ•œ ์—ฐ์Šต์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ถœ์ œํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ถœ์ƒ ์—ฐ๋„๋ฅผ ๋จผ์ € ๋ฌป๊ณ  ๊ตญ์ ์€ ํ•„์š”ํ•  ๋•Œ๋งŒ ๋ฌผ์–ด๋ณด๊ฒŒ ํ•˜๋ ค๋‹ค ๋ณด๋‹ˆ ๋ณต์žกํ•ด์กŒ๋Š”๋ฐ, ์ €์ฒ˜๋Ÿผ ํ•˜์ง€ ์•Š๊ณ  ๊ตญ์ ์„ ๋จผ์ € ๋ฌป๊ณ  ๋‚˜์„œ ์ถœ์ƒ ์—ฐ๋„๋ฅผ ๋ฌผ์œผ๋ฉด ์กฐ๊ฑด๋ฌธ์ด ๋‹จ์ˆœํ•ด์ ธ์„œ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šธ ๊ฑฐ์˜ˆ์š”. ใ€ˆ๋‹น์‚ฌ์ž๋„ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ฒ ์ด๋น„๋ถ ์„ธ๋Œ€ ๊ธฐ์ค€โ€ฆ์ƒ๋ฌผํ•™์— ์‚ฌํšŒยท์—ญ์‚ฌ ํ˜ผํ•ฉ ๋•Œ๋ฌธใ€‰, ๋ธŒ๋ผ๋ณด ๋งˆ์ด ๋ผ์ดํ”„ โ†ฉ ใ€ˆ์‚ฐ์—…ํ™”์„ธ๋Œ€โ†’๋ฒ ์ด๋น„๋ถ€๋จธโ†’X์„ธ๋Œ€โ†’๋ฐ€๋ ˆ๋‹ˆ์–ผ์„ธ๋Œ€โ†’Z์„ธ๋Œ€โ€ฆ์„ธ๋Œ€๋ณ„๋กœ ์„ฑ์žฅ ๋ฐฐ๊ฒฝ๊ณผ ์†Œ๋น„ ํŒจํ„ดยท๊ฐ€์น˜๊ด€์ด ๋ชจ๋‘ ๋‹ค๋ฅด์ฃ ~ใ€‰, ์ƒ๊ธ€์ƒ๊ธ€ โ†ฉ 2.3 for๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ ๊ฐ•์˜ ์˜์ƒ: https://youtu.be/TdFn4dnERHk ์ด๋ฒˆ์—” for ๋ฌธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผ ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ for ๋ฌธ์˜ ์“ฐ์ž„์ƒˆ๋Š” ๋‹ค๋ฅธ ์–ธ์–ด์™€ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜๋„ค์š”. ์ €๋„ ๊ทธ๊ฑธ ๋ชจ๋ฅด๊ณ  ํ•œ์ฐธ ๊ธ€์„ ์“ฐ๋‹ค ๋ณด๋‹ˆ ๋ญ”๊ฐ€ ์ด์ƒํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. ^^; for ๋ฌธ์€ ์šฐ๋ฆฌ๊ฐ€ ์ „์— ๋ฐฐ์› ๋˜ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™์€ ์‹œํ€€์Šค(sequence)๋ฅผ ์ด์šฉํ•ด์„œ ์›ํ•˜๋Š” ๋ช…๋ น์„ ๋ฐ˜๋ณตํ•  ๋•Œ ์“ฐ์ž…๋‹ˆ๋‹ค. ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ๋Š” ๋‚˜์ค‘์— ์ž์„ธํ•˜๊ฒŒ ์•Œ๋ ค๋“œ๋ฆฌ๊ธฐ๋กœ ํ•˜๊ณ , ์ „์— ๋ฐฐ์› ๋˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ณผ๊นŒ์š”? >>> family = ['mother', 'father', 'gentleman', 'sexy lady'] ์ €ํฌ ๊ฐ€์กฑ์ด ์ด๋žฌ์—ˆ๋Š”๋ฐ ๊ธฐ์–ต๋‚˜์‹œ์ง€์š”? ๊ทธ๋ƒฅ ๋”ฐ๋ผ ์น˜์ง€ ๋งˆ์‹œ๊ณ  ์—ฌ๋Ÿฌ๋ถ„์˜ ๊ฐ€์กฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. for ๋ฌธ ๋‹ค์Œ์€ for ๋ฌธ์„ ์ด์šฉํ•ด์„œ ์ €ํฌ ๊ฐ€์กฑ๋“ค์˜ ์ด๋ฆ„๊ณผ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. >>> for x in family: # family์˜ ๊ฐ ํ•ญ๋ชฉ x์— ๋Œ€ํ•˜์—ฌ: ... print(x, len(x)) # x์™€ x์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋ผ. ... ๋‹ต์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜ค๊ฒŒ ๋˜์ง€์š”. mother 6 father 6 gentleman 9 sexy lady 9 in family for x:๋ผ๊ณ  ์“ฐ๋ฉด ์•ˆ ๋˜๋ƒ๊ณ ์š”? ์•ˆ ๋˜๋„ค์š”. --; ๋ฌธ๋ฒ•์ด ๊ทธ๋Ÿฐ ๊ฑฐ๋‹ˆ๊นŒ ๊ทธ๋Œ€๋กœ ์จ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. range() ์ด๋ฒˆ์—” range()๋ผ๋Š” ๊ฒƒ์„ ๋ฐฐ์›Œ๋ณด๋„๋ก ํ•˜์ง€์š”. range๋Š” ๋ฒ”์œ„๋ผ๋Š” ๋œป์ธ๋ฐ ์—ฌ๊ธฐ์„œ๋Š” ์–ด๋–ค ์ •์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ฃผ๋ฉด ๊ทธ ๋ฒ”์œ„ ์•ˆ์˜ ์ •์ˆ˜๋“ค์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ๋ง์€ ์ข€ ์–ด๋ ต์ง€๋งŒ ๋ณ„๊ฑฐ ์•„๋‹ˆ๋ž๋‹ˆ๋‹ค. >>> list(range(2, 7)) # ํŒŒ์ด์ฌ 3 >>> range(2, 7) # ํŒŒ์ด์ฌ 2 ์ด๋ ‡๊ฒŒ ์ณ ๋ณด์„ธ์š”. ์–ด๋–ค ๋‹ต์ด ๋‚˜์˜ค๋‚˜์š”? [2, 3, 4, 5, 6] ์˜ˆ, 2 ์ด์ƒ 7 ๋ฏธ๋งŒ์ธ ์ˆซ์ž๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๊ตฐ์š”. ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋ง์ด ์ดํ•ด๋˜์‹œ์ฃ ? ๊ทธ๋Ÿฐ๋ฐ, for๋ฅผ ์„ค๋ช…ํ•˜๋‹ค๊ฐ€ ๊ฐ‘์ž๊ธฐ ์›ฌ range()๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฑธ๊นŒ์š”? ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. for ๋ฌธ์— range()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> a = [4, 5, 6, 7] >>> for i in a: ... print(i) ... ์œ„์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์ œ์™€ ์•„๋ž˜์˜ range()๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์ œ๋Š” ์ถœ๋ ฅ์ด ๊ฐ™์Šต๋‹ˆ๋‹ค. >>> for i in range(4, 8): ... print(i) ... ๋‹ต์ด ์–ด๋–ป๊ฒŒ ๋‚˜์˜ฌ๊นŒ์š”? ๋”ฐ๋ผ์„œ ์น˜์‹œ๊ธฐ ์ „์— ๋จผ์ € ์ƒ๊ฐ์„ ํ•ด๋ณด์„ธ์š”. ๊ทธ๋ฆฌ ์–ด๋ ต์ง€ ์•Š์ฃ ? ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์•„๋ฌด๋ฆฌ ์‰ฌ์šด ๊ฒƒ๋„ ์ง์ ‘ ํ•ด๋ณด์ง€ ์•Š์œผ๋ฉด ์ž๊ธฐ ๊ฒƒ์œผ๋กœ ๋งŒ๋“ค๊ธฐ ํž˜๋“ค๋‹ต๋‹ˆ๋‹ค. ๋˜, ์ƒ๊ฐ ์—†์ด ์ฑ…๋งŒ ๋ณด๊ณ  ๋”ฐ๋ผ ํ•œ๋‹ค๊ณ  ํ•ด์„œ ๋นจ๋ฆฌ ๋Š˜์ง€๋„ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฐฐ์šฐ๋Š” ๊ณผ์ •์„ ์ฆ๊ธฐ๋ฉด์„œ ์ฐจ๊ทผ์ฐจ๊ทผ ์—ฐ์Šตํ•˜๋‹ค ๋ณด๋ฉด ์‹ค๋ ฅ์ด ๋Š˜๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค. ๊ฑฐ๋ถ์ด Python ํ„ฐํ‹€ ๊ทธ๋ž˜ํ”ฝ์Šค์™€ ๋ฐ˜๋ณต๋ฌธ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ https://youtu.be/WHRrp-t3a64 2.3.1 ์—ฐ์Šต ๋ฌธ์ œ: ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ธฐ(for) ๋ฌธ์ œ 2.1.1 ์—ฐ์Šต ๋ฌธ์ œ์™€ ๊ฐ™์ด, ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ input()์œผ๋กœ ์ •์ˆ˜๋ฅผ ํ•œ ๊ฐœ ์ž…๋ ฅ๋ฐ›์•„, ๊ทธ ์ˆซ์ž๋ฅผ ์ˆซ์ž ํฌ๊ธฐ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์ด๋•Œ ์ถœ๋ ฅ ์•ž์— ๊ณต๋ฐฑ์„ ํ•œ ์นธ ์ฃผ์–ด์„œ, ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์ด ๊ตฌ๋ถ„๋˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์ด๋ฒˆ์—๋Š” for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ์˜ˆ 1 ์ž…๋ ฅ: ์ถœ๋ ฅ: 3 3 3 ์˜ˆ 2 ์ž…๋ ฅ: ์ถœ๋ ฅ: 5 5 5 5 5 ์ฝ”๋“œ: ch02/repeat_for.py 2.3.2 ์—ฐ์Šต ๋ฌธ์ œ: ์ œ๊ณฑํ‘œ(for) ๋ฌธ์ œ ์—ฐ์Šต ๋ฌธ์ œ 2.2์™€ ๊ฐ™์ด, ์ •์ˆ˜๋ฅผ ํ•œ ๊ฐœ ์ž…๋ ฅ๋ฐ›์•„ 1๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๊นŒ์ง€ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์ œ๊ณฑ์„ ๊ตฌํ•ด ํ”„๋ฆฐํŠธํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”. ๋‹จ, ์ด๋ฒˆ์—๋Š” for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ์˜ˆ 1 ์ž…๋ ฅ: ์ถœ๋ ฅ: 1 1 2 4 3 9 ์˜ˆ 2 ์ž…๋ ฅ: ์ถœ๋ ฅ: 1 1 2 4 3 9 4 16 5 25 ์ฝ”๋“œ: ch02/square_table_for.py 2.3.3 ์—ฐ์Šต ๋ฌธ์ œ: ํ™”ํ•™ ์‹คํ—˜์‹ค ๋ฌธ์ œ 1 ๋Œ€ํ•™๊ต์˜ ํ™”ํ•™์ž๋“ค์€ ์ƒ์ฒ˜๋ฅผ ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ์น˜๋ฃŒํ•˜๋Š” ์•ฝ๋ฌผ์„ ์ œ์กฐํ•˜๋Š” ์ƒˆ๋กœ์šด ๊ณผ์ •์„ ๊ฐœ๋ฐœํ–ˆ๋‹ค. ์ œ์กฐ ๊ณผ์ •์€ ๋งค์šฐ ๊ธธ๊ณ  ํ™”ํ•™ ์•ฝํ’ˆ์„ ๋งค๋ฒˆ ๋ชจ๋‹ˆํ„ฐ๋งํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ช‡ ์‹œ๊ฐ„์”ฉ ๊ฑธ๋ฆฐ๋‹ค! ํ•™์ƒ๋“ค์€ ์กธ๊ฑฐ๋‚˜ ๋”ด์ง“์„ ํ•˜๋ฏ€๋กœ ์ด ์ผ์„ ๋ฏฟ๊ณ  ๋งก๊ธธ ์ˆ˜๊ฐ€ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์•ฝ๋ฌผ์˜ ์ œ์กฐ๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๋Š” ์ž๋™ ์žฅ์น˜๋ฅผ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•ด์•ผ ํ•œ๋‹ค. ์žฅ์น˜๋Š” 15์ดˆ๋งˆ๋‹ค ์˜จ๋„๋ฅผ ์ธก์ •ํ•ด ํ”„๋กœ๊ทธ๋žจ์— ์ œ๊ณตํ•œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์€ ๋จผ์ € ์ตœ์†Œ์™€ ์ตœ๋Œ€์˜ ์•ˆ์ „ ์˜จ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ฝ๋Š”๋‹ค. ๊ทธ๋‹ค์Œ์—, ์žฅ์น˜๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์˜จ๋„(์ •์ˆ˜)๋ฅผ ๊ณ„์† ์ฝ๋Š”๋‹ค. ํ™”ํ•™ ๋ฐ˜์‘์ด ์™„๋ฃŒ๋˜๋ฉด ์žฅ์น˜๋Š” ๋์„ ์•Œ๋ฆฌ๋Š” -999๋ฅผ ๋ณด๋‚ธ๋‹ค. ๊ธฐ๋ก๋œ ์˜จ๋„๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๋ฒ”์œ„์— ์žˆ์„ ๊ฒฝ์šฐ(์ตœ์†Ÿ๊ฐ’ ๋˜๋Š” ์ตœ๋Œ“๊ฐ’๊ณผ ๊ฐ™์•„๋„ ๋œ๋‹ค) Nothing to report๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์˜จ๋„๊ฐ€ ์œ„ํ—˜ ์ˆ˜์ค€์— ๋„๋‹ฌํ•˜๋ฉด Alert!๋ฅผ ํ‘œ์‹œํ•˜๊ณ  ์˜จ๋„ ์ธก์ •์„ ์ค‘๋‹จํ•œ๋‹ค(์žฅ์น˜๊ฐ€ ์˜จ๋„๊ฐ’์„ ๊ณ„์† ๋ณด๋‚ด๋”๋ผ๋„). ์˜ˆ 1 ์ž…๋ ฅ: 10 20 15 10 20 0 15 -999 ์ถœ๋ ฅ: Nothing to report Nothing to report Nothing to report Alert! ์˜ˆ 2 ์ž…๋ ฅ: 0 100 15 50 75 -999 ์ถœ๋ ฅ: Nothing to report Nothing to report Nothing to report tip split() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> '0 100'.split() ['0', '100'] ๋‹ค์Œ๊ณผ ๊ฐ™์ด split()์œผ๋กœ ์–ป์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”๋กœ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> freezing_point, boiling_point = '0 100'.split() >>> freezing_point '0' >>> boiling_point '100' input()์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ์—๋„, ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ–ˆ์„ ๋•Œ ๋ฆฌ์ŠคํŠธ ์›์†Œ๊ฐ€ ๋ช‡ ๊ฐœ๊ฐ€ ๋ ์ง€ ๋ฏธ๋ฆฌ ์ •ํ•ด์ ธ ์žˆ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›์†Œ ๊ฐœ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ์•Œ ์ˆ˜ ์—†๋‹ค๋ฉด for ๋ฌธ์„ ์ด์šฉํ•˜์„ธ์š”. ์ €๋Š” ์˜จ๋„๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์ธก์ •ํ•œ ๊ฐ’์ด ํ•œ ๋ฒˆ์— ์ž…๋ ฅ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ธก์ •ํ•  ๋•Œ๋งˆ๋‹ค ๋”ฐ๋กœ ๋“ค์–ด์˜จ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ: ch02/chemical_lab.py ์•„๋ž˜ ์ฃผ์†Œ์˜ ํ’€์ด๋„ ์ฐธ๊ณ ํ•˜์„ธ์š”. https://pybo.kr/pybo/question/detail/80/ edX์˜ C Programming: Language Foundations ์ฝ”์Šค์— ๋‚˜์˜จ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. โ†ฉ 2.4 match-case ๋ฌธ ํŒŒ์ด์ฌ์„ ๋ฐฐ์šฐ๊ธฐ ์ „์— ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ๋ฐฐ์šด ์ ์ด ์žˆ๋‹ค๋ฉด switch-case ๋ฌธ๋ฒ•์„ ์ ‘ํ•˜์…จ์„ ํ…๋ฐ์š”, ํŒŒ์ด์ฌ์—๋Š” ๊ทธ๋Ÿฌํ•œ ๊ตฌ๋ฌธ์ด ์—†๋‹ค๊ฐ€ ํŒŒ์ด์ฌ 3.10์— match-case ๊ตฌ๋ฌธ์ด ์ถ”๊ฐ€๋์Šต๋‹ˆ๋‹ค. match-case๋Š” switch-case์™€ ๋น„์Šทํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ ์ข€ ๋” ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ™€์ˆ˜, ์ง์ˆ˜ ํŒ๋ณ„ ์šฐ์„  ๊ฐ„๋‹จํ•œ ์˜ˆ๋ถ€ํ„ฐ ๋“ค์–ด๋ณผ๊ฒŒ์š”. ๋‹ค์Œ ์ฝ”๋“œ๋Š” 1๋ถ€ํ„ฐ 10๊นŒ์ง€์˜ ์ •์ˆ˜์— ๋Œ€ํ•ด, ๊ฐ๊ฐ์ด ํ™€์ˆ˜์ธ์ง€ ์ง์ˆ˜์ธ์ง€๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. for n in range(1, 11): match n % 2: case 0: print(f"{n} is even.") case 1: print(f"{n} is odd.") n์„ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๋ฉด ์ง์ˆ˜(even number), 1์ด๋ฉด ํ™€์ˆ˜(odd number)๋ผ๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด์ฃ . ๊ฒฐ๊ณผ: $ python odd_even.py 1 is odd. 2 is even. 3 is odd. 4 is even. 5 is odd. 6 is even. 7 is odd. 8 is even. 9 is odd. 10 is even. ์—ฌ๊ธฐ๊นŒ์ง€๋งŒ ๋ณด๋ฉด switch-case์™€ ๋ณ„๋ฐ˜ ๋‹ค๋ฅผ ๊ฒƒ์ด ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹ค ์ˆ˜ ์žˆ๋Š”๋ฐ์š”, ์—ฌ๊ธฐ์„œ ๋์ด ์•„๋‹™๋‹ˆ๋‹ค. match-case ๋ฌธ์„ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ์ƒ๋‹นํžˆ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. ํ”ผ์ฆˆ ๋ฒ„์ฆˆ match-case๋ฅผ ์ด์šฉํ•ด ํ”ผ์ฆˆ ๋ฒ„์ฆˆ(FizzBuzz) ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ”ผ์ฆˆ ๋ฒ„์ฆˆ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๋ชจ๋ฅด์‹œ๋Š” ๋ถ„์€ ์•„๋ž˜ ๋งํฌํ•œ ๊ธ€์„ ์ž ์‹œ ๋ณด๊ณ  ์˜ค์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. if-else ๋ฌธ์œผ๋กœ ์ง์ ‘ ํ’€์–ด๋ณด์‹œ๋ฉด ๋” ์ข‹์Šต๋‹ˆ๋‹ค. https://wikidocs.net/168132 ๊ทธ๋Ÿผ, match-case๋ฅผ ์ด์šฉํ•œ ํ”ผ์ฆˆ ๋ฒ„์ฆˆ ํ’€์ด ์ฝ”๋“œ๋ฅผ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. for n in range(1, 101): match (n % 3, n % 5): case (0, 0): print("FizzBuzz") case (0, _): print("Fizz") case (_, 0): print("Buzz") case _: print(n) ์—ฌ๊ธฐ์„œ match (n % 3, n % 5)๋ผ๋Š” ๊ตฌ๋ฌธ์„ ๋ณด์‹ค ์ˆ˜ ์žˆ๋Š”๋ฐ, ์†Œ๊ด„ํ˜ธ๋กœ ๋‘˜๋Ÿฌ์‹ธ์ธ ๋ถ€๋ถ„์€ 4์žฅ์—์„œ ๋ฐฐ์šธ ํŠœํ”Œ์ž…๋‹ˆ๋‹ค. ํŠœํ”Œ์— ๊ด€ํ•ด์„œ๋Š” ๋‚˜์ค‘์— ๋ฐฐ์šฐ๊ธฐ๋กœ ํ•˜๊ณ , ์ผ๋‹จ์€ ๊ด„ํ˜ธ ์•ˆ์— n % 3๊ณผ n % 5๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์‹์ด ์žˆ๊ณ , ๊ทธ ์•„๋ž˜์˜ case์—์„œ ๊ฐ๊ฐ์„ ํ‰๊ฐ€ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. case (0, 0):์€ โ€œn์„ 3์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋„ 0์ด๊ณ , 5๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋„ 0์ธ ๊ฒฝ์šฐโ€๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ โ€˜FizzBuzzโ€™๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. case ๋ฌธ์—์„œ ๋ฐ‘์ค„(_)์€ ์•„๋ฌด ๊ฐ’์ด๋‚˜ ์ƒ๊ด€์—†๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์ฆ‰ case (0, _):์€ โ€œn์„ 3์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ธ ๊ฒฝ์šฐโ€์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ โ€˜Fizzโ€™๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ case (_, 0):๋Š” n์„ 5๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ์ด๊ณ , โ€˜Buzzโ€™๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. case _:๋Š” ๊ทธ ๋ฐ–์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. 3์œผ๋กœ๋„, 5๋กœ๋„ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€์ง€ ์•Š๋Š” ์ˆซ์ž๋“ค์ด ์ด์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. $ python fizzbuzz.py 2 Fizz Buzz Fizz 8 Fizz Buzz 11 (์ดํ•˜ ์ƒ๋žต) ์ฐธ๊ณ  ใ€Š๋งŒ๋“ค๋ฉด์„œ ๋ฐฐ์šฐ๋Š” ๋Ÿฌ์ŠคํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐใ€‹์—์„œ Rust์˜ match-case ๋ฌธ์„ ์‚ฌ์šฉํ•œ FizzBuzz ์˜ˆ์ œ๋ฅผ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค. 2.5 for-else์™€ while-else ์กฐ๊ฑด๋ฌธ์— else๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑด ์•ž์—์„œ ์‚ดํŽด๋ดค๋Š”๋ฐ์š”, ํŒŒ์ด์ฌ์—์„œ๋Š” ๋ฐ˜๋ณต๋ฌธ์—๋„ else๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. for-else ๋‹ค์Œ for ๋ฌธ์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ณ  ๋‚˜์„œ, ๋ชจ๋‘ ์ถœ๋ ฅํ–ˆ๋‹ค๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. >>> for x in [1, 2, 3, 4]: ... print(x) ... else: ... print("๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ–ˆ์–ด์š”") ... 2 4 ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ–ˆ์–ด์š” ์œ„์˜ ์ฝ”๋“œ๋งŒ ๋ณด๋ฉด, ๋งˆ์ง€๋ง‰ ์ถœ๋ ฅ์„ ๊ตณ์ด else ๋ธ”๋ก์— ๋„ฃ์ง€ ์•Š๊ณ  ๋ฐ˜๋ณต๋ฌธ ๋ฐ”๊นฅ์— ๋‘์–ด๋„ ๋  ๊ฒƒ ๊ฐ™์ฃ ? ํ•˜์ง€๋งŒ ์•„๋ž˜์ฒ˜๋Ÿผ break๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด ์–˜๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. >>> for x in [1, 2, 3, 4]: ... if x % 3: ... print(x) # x๊ฐ€ 3์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด ์ถœ๋ ฅ ... else: ... break # x๊ฐ€ 3์˜ ๋ฐฐ์ˆ˜์ด๋ฉด ๋ฐ˜๋ณต๋ฌธ์—์„œ ๋น ์ ธ๋‚˜๊ฐ ... else: ... print("๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ–ˆ์–ด์š”") ... 2 ์—ฌ๊ธฐ์„œ๋Š” ๋ฐ˜๋ณต๋ฌธ์„ break ํ–ˆ๋Š”๋ฐ, ๊ทธ๋Ÿด ๋•Œ๋Š” else ๋ธ”๋ก์ด ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. while-else while ๋ฌธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. while ๋ฌธ์ด break๋  ๊ฒฝ์šฐ์—๋Š” else ๋ธ”๋ก์ด ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. >>> countdown = 5 >>> while countdown > 0: ... print(countdown) ... countdown -= 1 ... if input() == '์ค‘๋‹จ': ... break ... else: ... print('๋ฐœ์‚ฌ!') ... 4 ์ค‘๋‹จ 3. ํ•จ์ˆ˜ ๋น„์Šท๋น„์Šทํ•œ ์ฝ”๋“œ๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์ž‘์„ฑํ•˜๊ธฐ ๊ท€์ฐฎ์œผ์‹ ๊ฐ€์š”? ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฐ˜๋ณต์ ์ธ ์ฝ”๋“œ๋ฅผ ์ค„์ด๋ฉด์„œ, ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹๊ฒŒ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด์š”. ์ด ์žฅ์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฒƒ: ํ•จ์ˆ˜ ๋ฐ˜ํ™˜ ๋ฌธ ์ง€์—ญ๋ณ€์ˆ˜, ์ „์—ญ๋ณ€์ˆ˜ ๋žŒ๋‹ค 3.1 ํ•จ์ˆ˜ ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ฝ”๋“œ๋ฅผ ํ•œ ์ค„, ํ•œ ์ค„ ์ž…๋ ฅํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ธด ํ–ˆ์ง€๋งŒ, ์ปดํ“จํ„ฐ์—๊ฒŒ ์ผ์„ ์‹œํ‚ค๋Š” ๊ฑด์ง€, ์šฐ๋ฆฌ๊ฐ€ ์ผ์„ ํ•˜๋Š” ๊ฑด์ง€ ํ—ท๊ฐˆ๋ฆด ์ •๋„๋กœ ๊ท€์ฐฎ์œผ์…จ์„ ๊ฑฐ์˜ˆ์š”. ์˜ค๋Š˜ ๋ฐฐ์šฐ์‹ค ํ•จ์ˆ˜๋ฅผ ์•„์‹œ๊ณ  ๋‚˜๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ์ข€ ๋” ์ฆ๊ฑฐ์›Œ์ง€์ง€ ์•Š์„๊นŒ ์‹ถ๋„ค์š”. ๊ทธ๋Ÿผ ์‹œ์ž‘ํ•ด ๋ณผ๊นŒ์š”? [1, 2, 3, 4, 5]๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ณผ๊ฒŒ์š”. ์ด ๋ฆฌ์ŠคํŠธ์—๋Š” ์›์†Œ๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ์„๊นŒ์š”? ์˜ˆ, 5๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—” [3, 4, 62, 27, 83, 956, 26, 58, 3, 78, 168, 64, 78]์ด๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ์นฉ์‹œ๋‹ค. ์—๊ตฌ๊ตฌโ€ฆ ์›์†Œ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์œผ๋‹ˆ๊นŒ a_list๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์—ฌ๋†“๋„๋ก ํ•˜์ฃ . >>> a_list = [3, 4, 62, 27, 83, 956, 26, 58, 3, 78, 168, 64, 78] ์ด ๋ฆฌ์ŠคํŠธ์—” ์›์†Œ๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ์„๊นŒ์š”? ์—ฌ๊ธฐ์„œ ๊ณต๋ถ€๋ฅผ ์—ด์‹ฌํžˆ ํ•˜์…จ๋˜ ๋ถ„์€ ๋ญ”๊ฐ€ ์ƒ๊ฐ์ด ๋‚˜์‹ค ๋ฒ•๋„ ํ•œ๋ฐโ€ฆ len()์ด๋ผ๋Š” ๊ฒƒ, ๊ธฐ์–ต๋‚˜์„ธ์š”? ์•„ํ•˜, len()์€ ๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด์žˆ๋Š” ์›์†Œ ๊ฐœ์ˆ˜, ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๋ฅผ ์•Œ๋ ค์ฃผ์ฃ . ์ด len()์ด ๋ฐ”๋กœ 'ํ•จ์ˆ˜'์˜€๋˜ ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ฒ˜๋Ÿผ len() ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด ์•„๋ฌด ๋ฆฌ์ŠคํŠธ๋‚˜ ์‰ฝ๊ฒŒ ํฌ๊ธฐ๋ฅผ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์ฃ . >>> len([1, 2, 3, 4, 5]) >>> len(a_list) 13 ๋งŒ์•ฝ len() ํ•จ์ˆ˜๊ฐ€ ์—†์—ˆ๋‹ค๋ฉด, ์šฐ๋ฆฌ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๋ฅผ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ๋งˆ๋‹ค ๋ณต์žกํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด์•ผ ํ–ˆ์„์ง€๋„ ๋ชฐ๋ผ์š”. ํ•จ์ˆ˜๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ข€ ๋” ์‰ฝ๊ฒŒ ํ•  ์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด์ง€์š”. ํ•จ์ˆ˜๋Š” len()์ฒ˜๋Ÿผ ์ฒ˜์Œ๋ถ€ํ„ฐ ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•ด ์ฃผ๋Š” ๊ฒƒ๋„ ์žˆ๊ณ , ์šฐ๋ฆฌ๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ๊ฒƒ์„ ์ง์ ‘ ๋งŒ๋“ค์–ด ์“ธ ์ˆ˜๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค. ๋˜๋Š” ๋‹ค๋ฅธ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๋งŒ๋“  ํ•จ์ˆ˜๋ฅผ ์–ป์–ด์„œ ์“ธ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€์š”. ์ €๋Š” ์ฒ˜์Œ์— ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ๋ฐฐ์šธ ๋•Œ ๋งค์šฐ ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•ด์„œ ์• ๋ฅผ ๋จน์—ˆ๋Š”๋ฐ, ์‚ฌ์‹ค์€ ์ด๋ ‡๊ฒŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๋„์™€์ฃผ๋Š” ๊ณ ๋งˆ์šด ์กด์žฌ๋ผ๋Š” ๊ฒƒ์„ ๋‚˜์ค‘์—์•ผ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ฒˆ์—” ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด ๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ์ง€๋‚œ ๊ฐ•์ขŒ์—์„œ ๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด์žˆ๋Š” ์›์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅํ–ˆ๋˜ ๊ฒƒ ๊ธฐ์–ต๋‚˜์‹œ์ฃ ? ๊ทธ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ•œ๋ฒˆ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜์ง€์š”. ๊ฐ™์ด ๋”ฐ๋ผ ํ•ด๋ณด์„ธ์š”. >>> def print_list(a): # ์ง€๊ธˆ๋ถ€ํ„ฐ print_list ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ฒ ๋‹ค๋Š” ๋œป ... for i in a: ... print(i) ... ๋ฐฉ๊ธˆ ์šฐ๋ฆฌ๋Š” print_list()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•˜์ง€์š”? ์ฒซ์งธ ์ค„์€ ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ์ง€์–ด์ฃผ๋Š” ๋ถ€๋ถ„์ด๊ณ ์š”, ๊ด„ํ˜ธ ์•ˆ์˜ a๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” print_list([1, 2])์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์“ฐ๋ฉด ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ด์ง€์š”. ์ด๋•Œ [1, 2]๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•จ์ˆ˜์— ๋„ฃ์–ด์ฃผ๋ฉด ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋Š” a = [1, 2]๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์ผ์„ ํ•˜๊ฒŒ ๋˜๊ณ ์š”. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ๋Š” ์–ด์ œ ํ•ด๋ณธ ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์ฃ . a๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋ช…๋ น์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ํ•จ์ˆ˜๊ฐ€ ์ œ๋Œ€๋กœ ๋™์ž‘์„ ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ด…์‹œ๋‹ค. ์•„๊นŒ ๋งŒ๋“ค์–ด ๋‘” a_list์— ๋“ค์–ด์žˆ๋Š” ์›์†Œ๋“ค์„ ์ฐ์–ด๋ณผ๊นŒ์š”? >>> print_list(a_list) ํ•จ์ˆ˜๋ฅผ ์ œ๋Œ€๋กœ ๋งŒ๋“œ์…จ๋‹ค๋ฉด ๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด์žˆ๋Š” ์›์†Œ๋“ค์ด ์ฃผ๋ฅด๋ฅต ๋‚˜์—ด๋ฉ๋‹ˆ๋‹ค. ์–ด๋–ป์Šต๋‹ˆ๊นŒ? ์ œ๋ฒ• ์“ธ๋งŒํ•˜์ฃ ? ๋ฐฉ๊ธˆ ๋งŒ๋“ค์–ด ๋ณธ ํ•จ์ˆ˜์—์„œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์„œ ์ผ์„ ํ–ˆ์—ˆ์ฃ ? ํ•˜์ง€๋งŒ ์•„๋ž˜์ฒ˜๋Ÿผ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. >>> def boy(): ... print('I am a boy.') ... print('You are a girl.') ... ์ž, ์ด๋ฒˆ์—” ์—ฌ๋Ÿฌ๋ถ„์ด ์ง์ ‘ ๋งŒ๋“ค์–ด๋ณผ ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. a์™€ b ๊ฐ€์šด๋ฐ a๊ฐ€ ํฌ๋ฉด 'a > b'๋ผ๊ณ  ํ‘œ์‹œํ•˜๊ณ , b๊ฐ€ ํฌ๋ฉด 'a < b', ๋‘ ์ˆซ์ž๊ฐ€ ๊ฐ™์œผ๋ฉด 'a == b'๋ผ๊ณ  ํ‘œ์‹œํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. ์ด ํ•จ์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‘ ๊ฐœ๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ ๊ด„ํ˜ธ ์•ˆ์— (x, y)์™€ ๊ฐ™์€<NAME>์œผ๋กœ ํ•ด์ฃผ๋ฉด ๋˜๊ฒ ์ฃ ? ๋‹ต ๋‹ค ๊ฐ€๋ฅด์ณ ๋“œ๋ ธ๋„ค์š”.^^ ๊ฑฐ๋ถ์ด ์ •์‚ฌ๊ฐํ˜•์„ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜, ์ •์‚ผ๊ฐํ˜•์„ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜: https://youtu.be/C6n_b7M7eFI ๋‹ค๊ฐํ˜•์„ ๊ทธ๋ฆฌ๋Š” ํ•จ์ˆ˜: https://youtu.be/h5bUrCQyom4 3.1.1 ์—ฐ์Šต ๋ฌธ์ œ: ์ž๋ฆฟ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ example.com์ฒ˜๋Ÿผ ์šฐ๋ฆฌ๊ฐ€ ๋ณดํ†ต โ€˜ํ™ˆํŽ˜์ด์ง€ ์ฃผ์†Œโ€™๋ผ๊ณ  ๋ถ€๋ฅด๋Š” URL์ด IP ์ฃผ์†Œ๋กœ ๋ณ€ํ™˜๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ๊ณ„์‹œ๋‚˜์š”? ์ฃผ์†Œ๋ฅผ 192.0.2.10๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ด๋Š” IPv4์—์„œ๋Š” 2564๊ฐœ์˜ ์ฃผ์†Œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ด์š”. >>> 256 ** 4 4294967296 ๊ทธ๋Ÿฌ๋‹ˆ๊นŒโ‹ฏ ์ด๊ฒŒ ๋ช‡ ๊ฐœ๋ƒ๋ฉดโ‹ฏ >>> len(str(_)) # ๋ฐฉ๊ธˆ ๊ตฌํ•œ ๋‹ต(_)์„ ๋ฌธ์ž์—ด(str)๋กœ ๋ฐ”๊พผ ๊ฒƒ์˜ ๊ธธ์ด(len) 10 10์ž๋ฆฌ ์ˆ˜์ด๋‹ˆ๊นŒ 4 ๋’ค์— 0์ด 9๊ฐœ ๋ถ™์—ˆ๋‹ค๊ณ  ํ•˜๋ฉด, 43์–ต ๊ฐœ ์ •๋„ ๋˜๋„ค์š”. ์ฐธ๊ณ  2564๋Š” 232์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. >>> 256 ** 4 == (2 ** 8) ** 4 == 2 ** 32 True ์š”์ฆ˜์€ ์ธํ„ฐ๋„ท์— ์ ‘์†ํ•˜๋Š” ๊ธฐ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ๋‹ค ๋ณด๋‹ˆ ์ฃผ์†Œ๊ฐ€ ์–ผ๋งˆ ๋‚จ์ง€ ์•Š์•„์„œ, ์•„์ฃผ์•„์ฃผ์•„์ฃผ ๋งŽ์€ ์ฃผ์†Œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” IPv6๋กœ ๋ฐ”๊ฟ” ๊ฐ€๊ณ  ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. IPv6 ์ฃผ์†Œ๋Š” 2001:0db8:85a3:0000:0000:8a2e:0370:7334์ฒ˜๋Ÿผ ์ƒ๊ฒผ๊ณ  2128๊ฐœ์˜ ์ฃผ์†Œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> 2 ** 128 340282366920938463463374607431768211456 >>> len(str(_)) 39 39์ž๋ฆฌ ์ˆซ์ž๋„ค์š”. ์ „ ์„ธ๊ณ„ ์ธ๊ตฌ๋ฅผ 80์–ต ๋ช…์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด, >>> 2 ** 128 / 8000_000_000 4.253529586511731e+28 1์ธ๋‹น 4์–‘(็ฉฐ) ๊ฐœ์”ฉ ์“ธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ณ„์‚ฐ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.1 ๋ฌธ์ œ ์–‘(้™ฝ)์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ๊ทธ ์ˆ˜๊ฐ€ ๋ช‡ ์ž๋ฆฌ ์ˆซ์ž์ธ์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ numOfDigits()๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. >>> numOfDigits(12345) >>> numOfDigits(1234567890) 10 ํ’€์ด ์ฝ”๋“œ: ch03/numdigits.py ํฐ ์ˆซ์ž๋ฅผ ์ฝ๋Š” ๋ฒ•์€ https://ko.wikipedia.org/wiki/ํฐ_์ˆ˜์˜_์ด๋ฆ„์— ๋‚˜์˜ค๋Š” ํ‘œ์˜ โ€˜์ด๋งŒ์ฒด์ง„โ€™ ์—ด์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. โ†ฉ 3.1.2 ์—ฐ์Šต ๋ฌธ์ œ: ๊ตฌ๊ตฌ๋‹จ ์งœ์ž”~! ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„~. ์ œ ๊ฐ•์ขŒ๋ฅผ ์ฐพ์•„์ฃผ์…”์„œ ์ •๋ง ๊ธฐ๋ป์š”. ๊ฒŒ์‹œํŒ์— ์˜ฌ๋ฆฌ์‹œ๋Š” ๊ธ€์„ ๋ณด๋Š” ๊ฒƒ์ด ๋‚™์ด๋ž๋‹ˆ๋‹ค. ์ „ ์š”์ฆ˜ ํ•™๊ต ์‹œํ—˜๊ณต๋ถ€๋ž‘ ๊ณผ์ œ๋ฌผ, ํšŒ์‚ฌ์ผ๋กœ ์กฐ๊ธˆ ๋ฐ”์˜๋‹ต๋‹ˆ๋‹ค(๋งˆ์Œ๋งŒ ๋ฐ”์˜์ง€ TV ๋ณด๊ณ , ์ž  ์ž˜ ์ž๊ณ , ๋”ด์ง“๋„ ๋งŽ์ด ํ•œ๋‹ต๋‹ˆ๋‹ค^^;). ์˜ค๋Š˜์€ ๊ตฌ๊ตฌ๋‹จ์„ ์ค€๋น„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ๊ตฌ๋‹จ์€ ์–ด๋Š ์–ธ์–ด๋“  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฐ์šธ ๋•Œ ๋น ์ง€์ง€ ์•Š๋Š” ์•ฝ๋ฐฉ์˜ ๊ฐ์ดˆ์ž…๋‹ˆ๋‹ค. ์ €๋Š” ์ดˆ๋“ฑํ•™๊ต 2ํ•™๋…„ ๋•Œ ๊ทธ๋„ค ํƒ€๋ฉด์„œ ๊ตฌ๊ตฌ๋‹จ์„ ์™ธ์› ๋˜ ๊ธฐ์–ต์ด ๋‚˜๋Š”๋ฐ, ์ง€๊ธˆ์€ ๋ช‡ ํ•™๋…„ ๋•Œ ๋ฐฐ์šฐ๋Š”์ง€ ๊ถ๊ธˆํ•˜๊ตฐ์š”. ์ €๋Š” ์™ ์ง€ 8๋‹จ์ด ์–ด๋ ต๋”๊ตฐ์š”. ์–ด๋จธ๋‹ˆ๋Š” ๋ญ๊ฐ€ ์–ด๋ ต๋ƒ๊ณ  ํ•˜์…จ์ง€๋งŒโ€ฆ ๊ทธ๋ž˜์„œ ์‚ฐ์ˆ˜ ์‹œ๊ฐ„์— ๊ณฑ์…ˆ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ๋Š” ๊ฐ€๋Šฅํ•˜๋ฉด ๋ง์…ˆ์œผ๋กœ ๋ฐ”๊ฟ”์„œ ํ’€๊ณค ํ–ˆ์ง€์š”. ๋ฌธ์ œ ๋‹ค์Œ ์˜ˆ์™€ ๊ฐ™์ด ๊ตฌ๊ตฌ๋‹จ์„ 2๋‹จ๋ถ€ํ„ฐ 9๋‹จ๊นŒ์ง€ ๊ณ„์‚ฐํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋ณด์„ธ์š”. ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๋‚ด์šฉ๋งŒ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜์‹œ๋ฉด ์ถฉ๋ถ„ํžˆ ํ•˜์‹ค ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. ์ถœ๋ ฅ: 2 * 1 = 2 2 * 2 = 4 โ€ฆ 9 * 9 = 81 ํžŒํŠธ ์Œ, ํž˜๋“œ์‹ญ๋‹ˆ๊นŒโ€ฆ ๋ชจ๋ฅด๊ฒ ๋‹ค๊ณ  ํ•˜๋Š” ๋ถ„์ด ์ •์ƒ์ž…๋‹ˆ๋‹คโ€ฆ ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•˜๋Š” ๋ถ„์€ ์•„๋งˆ ์˜ˆ์ „์— ๋น„์Šทํ•œ ๊ฒƒ์„ ๋ฐฐ์› ๊ฑฐ๋‚˜ ๋จธ๋ฆฌ๊ฐ€ ์ข‹์€ ๋ถ„์ผ ๊ฑฐ์˜ˆ์š”. ๊ทธ๋Ÿผ ์ •์ƒ์ธ์„ ์œ„ํ•ด์„œ ํžŒํŠธ๋ฅผ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ํžŒํŠธ๋ž„ ๊ฑด ์—†๊ณ ์š”, ์ „์ฒด๋ฅผ ํ•œ ๋ฒˆ์— ๋งŒ๋“ค๊ธฐ ํž˜๋“ค๋ฉด ์กฐ๊ธˆ์”ฉ ๋‚˜๋ˆ ์„œ ํ•ด๋ณด์‹œ๋ผ๋Š” ๊ฒ๋‹ˆ๋‹ค. ์šฐ์„  2๋‹จ๋งŒ ๋˜‘๊ฐ™์ด ๋งŒ๋“ค์–ด๋ณด์„ธ์š”. ์ด ๊ฐ•์ขŒ๋Š” ๋” ์ด์ƒ ๋ณด์ง€ ๋งˆ์‹œ๊ณ  ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์› ๋˜ ๊ฒƒ์„ ์ฐธ๊ณ ํ•ด์„œ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. ํ’€์ด ํ•œ๋ฒˆ ํ•ด๋ณด์…จ๋‚˜์š”? ํ•ด๋ณด์‹  ๋ถ„์€ ์ œ๊ฐ€ ์ง  ๊ฒƒ๊ณผ ๋น„๊ตํ•ด ๋ณด์„ธ์š”. 2 ๊ณฑํ•˜๊ธฐ 1์„ ์ถœ๋ ฅ ์šฐ์„  2 ๊ณฑํ•˜๊ธฐ 1๋ถ€ํ„ฐ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜ m์œผ๋กœ, ๊ณฑํ•˜๋Š” ์ˆ˜๋ฅผ n์œผ๋กœ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. >>> m = 2 >>> n = 1 >>> print(m, '*', n, '=', m*n) 2 * 1 = 2 print() ํ•จ์ˆ˜์—๋Š” ๋ณ€์ˆ˜ m, ๋ฌธ์ž์—ด '*', ๋ณ€์ˆ˜ n, ๋ฌธ์ž์—ด '=', ๊ทธ๋‹ค์Œ์— m*n์ด๋ผ๋Š” ์‹์„ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•ด ์ฐจ๋ก€๋กœ ๋„ฃ์–ด์คฌ์Šต๋‹ˆ๋‹ค. print() ํ•จ์ˆ˜์˜ ์ธ์ž๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„ ํƒ€์ž ์น˜๊ธฐ๋„ ๋ฒˆ๊ฑฐ๋กญ๊ณ  ๋ˆˆ์— ์ž˜ ๋“ค์–ด์˜ค์ง€ ์•Š์œผ๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด f-๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋‚ซ๊ฒ ๋„ค์š”. f-๋ฌธ์ž์—ด์€ ํŒŒ์ด์ฌ 3.6 ์ด์ƒ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> print(f'{m} * {n} = {m*n}') 2 * 1 = 2 2๋‹จ์„ ์ถœ๋ ฅ ๋‹ค์Œ์€ ๊ตฌ๊ตฌ๋‹จ์˜ 2๋‹จ์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. >>> m = 2 >>> for n in range(1, 10): ... print(f'{m} * {n} = {m*n:2d}') ... 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 ๋ณ€์ˆ˜ m ๊ฐ’์€ ์•„๊นŒ์ฒ˜๋Ÿผ ์ˆซ์ž 2๋กœ ํ–ˆ์ง€๋งŒ, ๋ณ€์ˆ˜ n์€ for ๋ฌธ๊ณผ range() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ์ฆ๊ฐ€์‹œ์ผœ๊ฐ€๋ฉด์„œ 2๋‹จ์„ ์ถœ๋ ฅํ•˜๊ฒŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. print ๋ฌธ์—์„œ๋Š” m๊ณผ n์„ ๊ณฑํ•œ ๊ฒฐ๊ณผ์˜ ์ž๋ฆฟ์ˆ˜๋ฅผ ๋งž์ถ”๋ ค๊ณ  f-๋ฌธ์ž์—ด์˜ ์„ธ ๋ฒˆ์งธ ์ค‘๊ด„ํ˜ธ์— 2d๋ฅผ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•œ ๋‹จ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜ ์ด๋ฒˆ์—” ํ•œ ๋‹จ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ 2๋ฅผ ๋„ฃ์œผ๋ฉด 2๋‹จ์„ ์ถœ๋ ฅํ•˜๊ณ , 5๋ฅผ ๋„ฃ์œผ๋ฉด 5๋‹จ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฑฐ์ฃ . >>> def multi(m): ... for n in range(1, 10): ... print(f'{m} * {n} = {m*n:2d}') ์•„๊นŒ ๊ฑฐ๋ž‘ ๊ฑฐ์˜ ๋น„์Šทํ•œ๋ฐ ์ฝ”๋“œ๋ฅผ ์ „๋ถ€ ํ•จ์ˆ˜์— ์ง‘์–ด๋„ฃ์—ˆ์ฃ ? ๊ทธ๋ž˜๋„ ํ›จ์”ฌ ํผ ๋‚ฉ๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ๋Œ๋ ค๋ณด์„ธ์š”. 3๋‹จ ๋‚˜์™€๋ผ ๋š๋”ฑ! >>> multi(3) 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด์…จ๋‚˜์š”? ์“ธ ๋งŒํ•˜์ฃ ? ๊ตฌ๊ตฌ๋‹จ ์ „์ฒด ์ด์ œ ์ฒ˜์Œ ๋ฌธ์ œ๋„ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์ง€ ์•Š๋‚˜์š”? ์œ„์—์„œ ๋งŒ๋“  ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด 2๋‹จ๋ถ€ํ„ฐ 9๋‹จ๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๊ฒ ์ฃ ? ์ž˜ ๋˜์…จ๋‚˜์š”? ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ง€๊ธˆ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์— ๊ตณ์ด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ํ•„์š”๋Š” ์—†์—ˆ์ง€์š”. ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐ˜๋ณต๋ฌธ ์•ˆ์— ๋˜ ๋ฐ˜๋ณต๋ฌธ์„ ์ง‘์–ด๋„ฃ์–ด๋„ ๋œ๋‹ต๋‹ˆ๋‹ค. ํ•จ์ˆ˜์— ๋Œ€ํ•ด ๋ณต์Šต๋„ ํ•  ๊ฒธ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋„๋ก ๋งŒ๋“  ๊ฒƒ์ด์ง€์š”. ๋ฐ˜๋ณต๋ฌธ์„ ์ค‘์ฒฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ง์ ‘ ํ•ด ๋ณด์‹œ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ฝ”๋“œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ์ง€ ์•Š์•„๋„ ์ž˜ ํ•˜์‹œ๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋ชจ๋‘ ์ฆ๊ฑฐ์šด ํ•˜๋ฃจ ๋ณด๋‚ด์‹œ๊ณ ์š”, ์ €๋Š” ๋‹ค์Œ ์ด ์‹œ๊ฐ„์—โ€ฆ ์ฝ”๋“œ ์ฝ”๋“œ: ch03/multiplication_table.py Flowgorithm์œผ๋กœ ์ž‘์„ฑํ•œ ๊ตฌ๊ตฌ๋‹จ ํ๋ฆ„๋„: https://wikidocs.net/167057 3.2 ๋ฐ˜ํ™˜(return) ๋ฌธ ์—ฌ๋Ÿฌ๋ถ„, ํ•จ์ˆ˜๊ฐ€ ๋ฌด์—‡์ผ๊นŒ์š”? ์ง€๊ธˆ๊นŒ์ง€ ๋จธ๋ฆฌ ์•„ํ”„๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๊ณต๋ถ€ํ–ˆ๋Š”๋ฐ ๋˜ ๋ฌด์Šจ ์†Œ๋ฆฌ๋ƒ๊ณ ์š”? ๋ฌผ๋ก  ํ•จ์ˆ˜์— ๋Œ€ํ•ด ๊ณ„์† ๋ฐฐ์›Œ์™”์ง€๋งŒ, ๊ฒฐ์ •์ ์œผ๋กœ ๋น ์ง„ ๋‚ด์šฉ์ด ํ•˜๋‚˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค์€ ์šฐ๋ฆฌ๊ฐ€ ์ดˆ๋“ฑํ•™๊ต ๋•Œ๋ถ€ํ„ฐ ๋ฐฐ์›Œ์™”๋˜ ๊ฒƒ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ณผ์—ฐ ๊ทธ๊ฒƒ์ด ๋ฌด์—‡์ผ๊นŒ์š”? ์ด ๊ทธ๋ฆผ ๋‚ฏ์ต์œผ์‹œ์ฃ ? ํ•จ์ˆ˜์— x๋ฅผ ์ง‘์–ด๋„ฃ์œผ๋ฉด ํ•จ์ˆ˜๊ฐ€ ์ฃผ๋ฌผ๋Ÿญ์ฃผ๋ฌผ๋Ÿญ ๊ณ„์‚ฐํ•ด์„œ y๋ผ๋Š” ๊ฐ’์„ ๋Œ๋ ค์ฃผ๋Š” ๊ทธ๋ฆผ์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜์— ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ํ•จ์ˆ˜๋Š” ๊ณ„์‚ฐ๋œ ๊ฐ’์„ ๋Œ๋ ค์ค€๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ ํ•จ์ˆ˜์˜ ํ•ต์‹ฌ์ด์ง€์š”. ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ํ•จ์ˆ˜๋“ค์€ ์ผ์€ ์—ด์‹ฌํžˆ ํ•˜์ง€๋งŒ ๋Œ๋ ค์ฃผ๋Š” ๊ฒƒ์€ ์—†์—ˆ์ง€์š”. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด์   ์ผ๋„ ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋Œ๋ ค์ฃผ๊ธฐ๋„ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ด์•ผ๊ฒ ์ฃ ? >>> def f1(x): ... a = 3 ... b = 5 ... y = a * x + b ... return y # y ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค ... >>> c = f1(10) # c = 35 >>> print(c) 35 ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•˜๋Š” ํ•จ์ˆ˜ f1()์„ ๋งŒ๋“ค์–ด๋ดค์Šต๋‹ˆ๋‹ค. ๊ฐ’์„ ๋Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด return์ด๋ผ๋Š” ๊ฒƒ์ด ์“ฐ์˜€์ง€์š”? ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ํ•จ์ˆ˜์— 10์ด๋ผ๋Š” ์ธ์ž๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ํ•จ์ˆ˜๋Š” 35๋ผ๋Š” ๊ฐ’์„ ๋Œ๋ ค์ค๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๊ทธ ๊ฐ’์„ ๋‹ค์‹œ c๋ผ๋Š” ๋ณ€์ˆ˜์— ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋Š” ๊ฑฐ์ฃ . ๋งŒ์•ฝ, ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ return y ๋Œ€์‹ ์— print(y)๋ผ๊ณ  ์ผ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? >>> def f2(x): ... a = 3 ... b = 5 ... y = a * x + b ... print(y) # y ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค ... >>> d = f2(10) # d = ? 35 >>> print(d) None d = f2(10)์ด๋ผ๊ณ  ํ•˜๋ฉด f2() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜์–ด 35๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜์ง€๋งŒ d์—๊ฒŒ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋Š” ์•Š์ฃ . ๊ทธ๋ž˜์„œ d๋ฅผ ํ”„๋ฆฐํŠธํ•ด ๋ณด๋ฉด ์•„๋ฌด ๊ฐ’์ด ์—†๋‹ค๋Š” ๋œป์œผ๋กœ None์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์–ด๋– ์„ธ์š”? ์ด์ œ ํ•จ์ˆ˜๊ฐ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ ์ดํ•ด๊ฐ€ ๋˜์‹œ๋‚˜์š”? ๊ทธ๋ ‡๋‹ค๋ฉด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด ๋ณด์‹ค ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ์‚ผ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด์„ธ์š”. ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ๋Š” ์‚ผ๊ฐํ˜•์˜ ๋ฐ‘๋ณ€๊ณผ ๋†’์ด๊ฐ€ ์ฃผ์–ด์ง€๊ณ , ๋ฐ˜ํ™˜(return) ๊ฐ’์€ ์‚ผ๊ฐํ˜•์˜ ๋„“์ด๊ฐ€ ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒ ์ฃ ? ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์‹  ๋ถ„๊ป˜๋Š” ์‹ ๊ธฐํ•œ ๊ฒƒ์„ ํ•˜๋‚˜ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„์ง ์•ˆ ํ’€์–ด๋ณด์…จ์œผ๋ฉด ๋นจ๋ฆฌํ•ด๋ณด์„ธ์š”. ๋‹ค ํ’€์–ด๋ณด์…จ์ฃ ? ๊ทธ๋Ÿผ, ์•Œ๋ ค๋“œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณผ ๊ฑฐ์ง“ 1 ๋”ํ•˜๊ธฐ 1์€ 2 ๋งž์ฃ ? '์ฐธ', '๊ฑฐ์ง“'์œผ๋กœ ๋Œ€๋‹ตํ•ด ๋ณด์„ธ์š”. '์ฐธ'์ด๋ผ๊ณ  ๋Œ€๋‹ตํ•˜์…จ๋‚˜์š”? ๊ทธ๋Ÿผ ํŒŒ์ด์ฌ์€ ์ด ์งˆ๋ฌธ์— ์–ด๋–ป๊ฒŒ ๋Œ€๋‹ตํ• ๊นŒ์š”? >>> 1 + 1 == 2 True ์ฐธ์ด๋ผ๊ณ  ๋‹ต์„ ํ•˜๋„ค์š”. >>> 1 + 1 == 3 False ์ด๊ฑด ๊ฑฐ์ง“์ด๋ผ๊ณ  ํ•˜๊ณ ์š”. ๋‹ค์Œ์˜ if ๋ฌธ์„ ๋ณด์„ธ์š”. 1 + 1์ด 2๊ฐ€ ๋งž์œผ๋ฉด 'yes'๋ผ๊ณ  ๋Œ€๋‹ตํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 'no'๋ผ๊ณ  ๋Œ€๋‹ตํ•˜๊ฒ ์ฃ ? >>> if 1 + 1 == 2: ... print('yes') ... else: ... print('no') ... yes ๋ฐฉ๊ธˆ ์•Œ๋ ค๋“œ๋ฆฐ ๊ฒƒ๊ณผ ํ•จ๊ป˜ ์ƒ๊ฐํ•ด ๋ณด๋ฉด 1 + 1 == 2๋ผ๋Š” ์‹์ด True(์ฐธ)์ด๋ฉด yes๋ฅผ, False(๊ฑฐ์ง“)์ด๋ฉด no๋ฅผ ํ”„๋ฆฐํŠธํ•œ๋‹ค๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ ์ด๋Ÿฐ ์„ฑ์งˆ์„ ํ™œ์šฉํ•˜๋ฉด ๋„์›€์ด ๋˜๊ฒ ์ฃ ? ์‰ฌ์šด ๋ง์…ˆ ๋ฌธ์ œ๋ฅผ ๋‚ด๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> def quiz(): ... ans = input('1 + 2 = ') ... return 1 + 2 == int(ans) ... input()์ด๋ผ๋Š” ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž์—ด ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋ฐ ์“ฐ์ด๊ณ ์š”, int() ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ๋Š” input() ํ•จ์ˆ˜๊ฐ€ 1 + 2 =์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ ๋‹ค์Œ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๊ทธ ๊ฐ’์„ ans๋ผ๋Š” ๋ณ€์ˆ˜์— ๋„ฃ์–ด์คฌ์Šต๋‹ˆ๋‹ค. ์…‹์งธ ์ค„์—์„œ๋Š” 1 + 2์˜ ๊ฐ’๊ณผ int(ans)์˜ ๊ฐ’์ด ๊ฐ™์€์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” True๋‚˜ False๋กœ ๋ฐ˜ํ™˜ํ•˜๊ฒ ์ฃ ? ๋‹ต์„ ๋งžํžˆ๋ฉด True๋ฅผ ๋Œ๋ ค์ฃผ๊ณ , ํ‹€๋ฆฌ๋ฉด False๋ฅผ ๋Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ด์ฃ . ์ดํ•ด๊ฐ€ ๋˜์‹œ๋Š”์ง€์š”? ์œ„์—์„œ ๋งŒ๋“  ํ€ด์ฆˆ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ’€์–ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> quiz() 1 + 2 = 3 True >>> quiz() 1 + 2 = 4 False ํ•œ๋ฒˆ ํ…Œ์ŠคํŠธํ•ด ๋ณด์„ธ์š”. ์žฌ๋ฏธ์žˆ์ฃ ? ์šฐ๋ฆฌ๊ฐ€ ๋ฐฐ์›Œ์˜จ ๊ฒƒ๋“ค์ด ์ ์  ๊ทธ๋Ÿด๋“ฏํ•˜๊ฒŒ ๋ชจ์–‘์„ ๊ฐ–์ถฐ๊ฐ€๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”. ์˜ค๋Š˜์€ ์—ฌ๊ธฐ๊นŒ์ง€~ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค~ 3.2.1 ์—ฐ์Šต ๋ฌธ์ œ: ์ˆซ์ž ์ฝ๊ธฐ ํ•จ์ˆ˜(1~10) ๋ฌธ์ œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ์ •์ˆ˜๋ฅผ ํ•œ๊ตญ์–ด๋กœ ํ‘œ๊ธฐํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ korean_number()๋ฅผ ์ •์˜ํ•˜์„ธ์š”. ๋‹จ, ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” 1 ์ด์ƒ 10 ์ดํ•˜์˜ ์ •์ˆ˜๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. >>> korean_number(1) '์ผ' >>> korean_number(3) '์‚ผ' >>> korean_number(10) '์‹ญ' ์ฝ”๋“œ: ch03/korean_1_to_10.py 3.2.2 ์—ฐ์Šต ๋ฌธ์ œ: ํ•จ์ˆ˜ ์ •์˜ํ•˜๊ธฐ ๋ฌธ์ œ ๋ฌธ์ œ 1 ๋‹ค์Œ triple() ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”. >>> def triple(x): ... โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ โ–ˆ โ–ˆ โ–ˆ ... >>> triple(2) >>> triple('x') 'xxx' ๋ฌธ์ œ 2 ์˜ค๋Š˜์˜ ๋‚ ์งœ ๊ฐ์ฒด๋ฅผ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜์ง€ ๋ชปํ•ด๋„ ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) >>> from datetime import datetime >>> today = datetime.today() >>> today datetime.datetime(2021, 3, 21, 15, 46, 1, 94942) ์œ„ ์ฝ”๋“œ์˜ today์—์„œ ์—ฐ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. >>> today.year 2021 ํƒœ์–ด๋‚œ ํ•ด๋ฅผ ๋„ค ์ž๋ฆฌ ์ˆซ์ž๋กœ ์ž…๋ ฅํ•˜๋ฉด ํ•œ๊ตญ ๋‚˜์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ korean_age()๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์ฝ”๋“œ: ch03/define_functions.py 3.2.3 ์—ฐ์Šต ๋ฌธ์ œ: ์ด์ž(๋‹จ๋ฆฌ) ๊ณ„์‚ฐ ์ง์žฅ์ธ A ์”จ๋Š” 1๋…„ ๋™์•ˆ ์—ด์‹ฌํžˆ ์ผํ•ด์„œ ์—ฐ๋ง์— ์„ฑ๊ณผ๊ธ‰์œผ๋กœ ์ฒœ๋งŒ ์›์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์—ฐ์ด์œจ 3.875%(๋‹จ๋ฆฌ)์ธ ๊ณ ์ •๊ธˆ๋ฆฌ ์ƒํ’ˆ์— ์˜ˆ๊ธˆํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 5๋…„ ๋™์•ˆ ๋„ฃ์–ด๋‘๋ฉด ์ด์ž๊ฐ€ ์–ผ๋งˆ ๋ถ™๋Š”์ง€ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซํ•ด์— ์›๊ธˆ 10,000,000์›์— ๋Œ€ํ•œ ์ด์ž 10,000,000 * 0.03875 = 387500์›์ด ๋ถ™์Šต๋‹ˆ๋‹ค. ๋‘˜์งธ ํ•ด์— ์›๊ธˆ 10,000,000์›์— ๋Œ€ํ•œ ์ด์ž 387500์›์ด ๋ถ™์Šต๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์…‹์งธ, ๋„ท์งธ, ๋‹ค์„ฏ์งธ ํ•ด์—๋„ ํ•ด๋งˆ๋‹ค ๊ฐ™์€ ๊ธˆ์•ก์˜ ์ด์ž๊ฐ€ ๋ถ™์Šต๋‹ˆ๋‹ค. ๋งŒ๊ธฐ๊ฐ€ ๋˜์–ด ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ด์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. >>> 10000000 * 0.03875 * 5 1937500.0 ์›๊ธˆ๊ณผ ์ด์ž๋ฅผ ํ•ฉํ•œ ์ด์•ก, ์ฆ‰ ์›๋ฆฌ๊ธˆ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. >>> 10000000 + 10000000 * 0.03875 * 5 11937500.0 ์†Œ์ˆ˜์  ์ดํ•˜๋Š” ํ•„์š” ์—†์ง€๋งŒ ์ง€๊ธˆ์€ ๊ทธ๋Œ€๋กœ ๋‘˜๊ฒŒ์š”. ์›๊ธˆ(Principal), ์ด์œจ(rate), ๊ธฐ๊ฐ„(time)์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด์ž(Interest)๋ฅผ ๊ตฌํ•˜๋Š” ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. = r ๊ทธ๋ฆฌ๊ณ  ์›๋ฆฌ๊ธˆ(Amount)์„ ๊ตฌํ•˜๋Š” ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. = ( + t ) ๋ฌธ์ œ ๋ฌธ์ œ 1 ์›๊ธˆ(p), ๋‹จ๋ฆฌ ์ด์œจ(r), ๊ธฐ๊ฐ„(t)์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ์ด์ž๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ simple_interest()๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์˜ˆ 1 >>> simple_interest(10000000, 0.03875, 5) 1937500.0 ์˜ˆ 2 >>> simple_interest(1100000, 0.05, 5/12) 22916.666666666668 ๋ฌธ์ œ 2 ์›๊ธˆ(p), ๋‹จ๋ฆฌ ์ด์œจ(r), ๊ธฐ๊ฐ„(t)์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ์›๋ฆฌ๊ธˆ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜ simple_interest_amount()๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์˜ˆ 1 >>> simple_interest_amount(10000000, 0.03875, 5) 11937500.0 ์˜ˆ 2 >>> simple_interest_amount(1100000, 0.05, 5/12) 1122916.6666666665 ์ฝ”๋“œ: ch03/simpleInterest.py ๊ตฌ๊ธ€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ ์ฐธ๊ณ  Simple Interest Calculator A = P(1 + rt), CaculatorSoup Business Math (Olivier), LibreTexts 3.2.4 ์—ฐ์Šต ๋ฌธ์ œ: ๋ณต๋ฆฌ ๊ณ„์‚ฐ ์ด๋ฒˆ์—๋Š” ๋ณต๋ฆฌ(่ค‡ๅˆฉ, compound interest)๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณผ๊ฒŒ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด 1,500,000์›์„ 3๊ฐœ์›” ๋™์•ˆ ๋„ฃ์–ด๋‘๋ฉด ์—ฐ 4.3%์˜ ์ด์ž๋ฅผ ์ฃผ๋Š” ์ƒํ’ˆ์ด ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ, ๋งŒ๊ธฐ๊ฐ€ ๋  ๋•Œ๋งˆ๋‹ค ๋ฐ›๋Š” ์ด์ž์™€ ์›๊ธˆ์„ ํ•ฉํ•œ ๊ธˆ์•ก์„ ์žฌ์˜ˆ์น˜ํ•˜์—ฌ 6๋…„๊ฐ„ ์šด์šฉํ–ˆ์„ ๋•Œ ๋ฐ›๋Š” ์ด์•ก์„ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> r = 0.043 >>> n = 4 ์œ„์˜ r์€ ์—ฐ์ด์œจ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜๊ณ , n์€ 1๋…„ ๋™์•ˆ์— ๋ณต๋ฆฌ๊ฐ€ ๋ช‡ ๋ฒˆ ์ ์šฉ๋˜๋Š”์ง€๋Š” ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. 3๊ฐœ์›”์ด 4๋ฒˆ ์ง€๋‚˜์•ผ 1๋…„์ด ๋˜๋Š”๋ฐ, ์ด๋•Œ 4๊ฐ€ ๋ฐ”๋กœ n ๋ณ€์ˆซ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ์ด์œ ๋Š” ์‹ค์ œ ๊ณ„์‚ฐ์„ ํ•ด๋ณด๋ฉด ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ฒซํ•ด๋งŒ ๊ณ„์‚ฐํ•ด ๋ณผ๊ฒŒ์š”. >>> 1500000 * (1 + r / n) # ์ฒ˜์Œ 3๊ฐœ์›” 1516125.0 >>> _ * (1 + r / n) # ๊ทธ๋‹ค์Œ 3๊ฐœ์›” 1532423.34375 >>> _ * (1 + r / n) # ๊ทธ๋‹ค์Œ 3๊ฐœ์›” 1548896.8946953125 >>> _ * (1 + r / n) # ๊ทธ๋‹ค์Œ 3๊ฐœ์›” 1565547.5363132872 ์ด๋ ‡๊ฒŒ ํ•ด์„œ ์ฒ˜์Œ 1๋…„ ๋™์•ˆ์˜ ์›๋ฆฌ๊ธˆ์„ ๊ณ„์‚ฐํ–ˆ์Šต๋‹ˆ๋‹ค. 2๋…„ ์ฐจ๋„ ๊ณ„์‚ฐํ•ด ๋ณผ๊นŒ์š”? >>> _ * (1 + r / n) 1582377.1723286551 >>> _ * (1 + r / n) 1599387.7269311883 >>> _ * (1 + r / n) 1616581.1449956987 >>> _ * (1 + r / n) 1633959.3923044025 ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ 6๋…„ ์น˜๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด, >>> _ * (1 + r / n) 1938836.8221341053 ์ตœ์ข…์ ์œผ๋กœ ์œ„ ๊ธˆ์•ก์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ํ•ด๋ณด์„ธ์š”. ์—„์ฒญ ๊ท€์ฐฎ์Šต๋‹ˆ๋‹ค. ์†Œ์ˆ˜์  ์ดํ•˜๊ฐ€ ๊ธธ์–ด์„œ ๋ณด๊ธฐ ๋ถˆํŽธํ•˜์ง€๋งŒ ์ง€๊ธˆ์€ ๊ทธ๋Œ€๋กœ ๋‘˜๊ฒŒ์š”. ์ด ๊ณ„์‚ฐ์„ ์‰ฝ๊ฒŒ ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ณต๋ฆฌ ๊ณ„์‚ฐ ๊ณต์‹์„ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. โ€ฒ P ( + n ) t โ€ฒ : ์›๋ฆฌ๊ธˆ : ์›๊ธˆ : ์—ฐ์ด์œจ : ๊ธฐ๊ฐ„ : ๋ณต๋ฆฌ ํšŸ์ˆ˜ ์•ž์—์„œ ์†์ˆ˜ ๊ณ„์‚ฐํ–ˆ๋˜ ๊ฒƒ์„ ์ด๋ฒˆ์—๋Š” ๊ณต์‹์— ๋Œ€์ž…ํ•ด ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜๋ช…์€ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ํ• ๊ฒŒ์š”. >>> p = 1500000 >>> r = 0.043 >>> t = 6 >>> n = 4 >>> p * (1 + r / n) ** (n * t) 1938836.8221341055 ๋ฌธ์ œ ๋ณต๋ฆฌ ์˜ˆ๊ธˆ์˜ ์›๊ธˆ(p), ์—ฐ์ด์œจ(r), ๊ธฐ๊ฐ„(t), ๋ณต๋ฆฌ ํšŸ์ˆ˜(n)์— ๋Œ€ํ•œ ์›๋ฆฌ๊ธˆ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜ compound_interest_amount()๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์˜ˆ 1 6๋…„๊ฐ„ ๋งค ๋ถ„๊ธฐ ์ด์ž๋ฅผ ์ฃผ๋Š” ๊ฒฝ์šฐ(t=6, n=4) >>> compound_interest_amount(1500000, 0.043, 6, 4) 1938836.8221341055 ์˜ˆ 2 6๋…„๊ฐ„ 2๋…„๋งˆ๋‹ค ์ด์ž๋ฅผ ์ฃผ๋Š” ๊ฒฝ์šฐ(t=6, n=1/2) >>> compound_interest_amount(1500000, 0.043, 6, 1/2) 1921236.0840000005 ์ฝ”๋“œ: ch03/compoundInterest.py ๊ตฌ๊ธ€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ ์ฐธ๊ณ  https://en.wikipedia.org/wiki/Compound_interest 3.3 ์ง€์—ญ๋ณ€์ˆ˜, ์ „์—ญ๋ณ€์ˆ˜ ์—ฌ๋Ÿฌ๋ถ„ ์•ˆ๋…•ํ•˜์„ธ์š”~. ์˜ค๋Š˜(์ด ์žฅ์„ ์ฒ˜์Œ ์“ด ๋‚ )์€ ์ œ๊ฐ€ ์ค‘๊ฐ„์‹œํ—˜ ๋ณด๋Š” ๋‚ ์ด๋ž๋‹ˆ๋‹ค. ์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ž‘, ์„ ํ˜•๋Œ€์ˆ˜ ๊ณผ๋ชฉ์ด๊ณ ์š”, ์‹œ์Šคํ…œ ๋ถ„์„ ์„ค๊ณ„ ๊ณผ์ œ๋ฌผ๋„ ๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์ปดํ“จํ„ฐ์˜ CPU ๊ตฌ์กฐ๋ž‘, ์–ด์…ˆ๋ธ”๋ฆฌ ์–ธ์–ด, ์–ด์…ˆ๋ธ”๋ฆฌ ์–ธ์–ด๋ฅผ ํ•ด์„ํ•ด์„œ ์ปดํ“จํ„ฐ๊ฐ€ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๊ธฐ๊ณ„์–ด๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ์–ด์…ˆ๋ธ”๋Ÿฌ์˜ ์ž‘๋™์›๋ฆฌ, ์šด์˜์ฒด์ œ๋„ ํฌํ•จ๋œ ๊ณผ๋ชฉ์ž…๋‹ˆ๋‹ค. ํ•œ๋งˆ๋””๋กœ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ๊ณผ ์ปดํ“จํ„ฐ ์žฅ์น˜ ์‚ฌ์ด์—์„œ ์ผํ•˜๋Š” ๊ฒƒ์ด ์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋žจ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์ง€์š”. ์„ ํ˜•๋Œ€์ˆ˜๋Š” ์ˆ˜ํ•™์˜ ํ–‰๋ ฌ, ๋ฒกํ„ฐ ๊ฐ™์€ ๊ฒƒ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ๋‚˜์˜ค๋Š”๋ฐ, ์ปดํ“จํ„ฐ ๊ทธ๋ž˜ํ”ฝ์„ ๊ตฌํ˜„ํ•  ๋•Œ๋„ ์ด๋Ÿฐ ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋”๊ตฐ์š”. ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ๋ถ„์•ผ์—๋„ ์‘์šฉ์ด ๋˜๊ฒ ์ฃ . ์‹œ์Šคํ…œ ๋ถ„์„ ์„ค๊ณ„๋Š” ๊ฑด๋ฌผ์„ ์ง“๊ธฐ ์ „์— ๋จผ์ € ์„ค๊ณ„๋ฅผ ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ”„๋กœ๊ทธ๋žจ์„ ์งค ๋•Œ ์ „์ฒด์ ์ธ ์„ค๊ณ„๋ฅผ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋˜๋ ค๋ฉด ์ž๋ฐ”, C, ๋น„์ฃผ์–ผ ๋ฒ ์ด์ง ๊ฐ™์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋งŒ ๋ฐฐ์šฐ๋ฉด ๋˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐ์„ ํ•˜๊ธฐ ์‰ฌ์šด๋ฐ, ์‚ฌ์‹ค ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋Š” ํ•œ ๋ถ€๋ถ„์— ๋ถˆ๊ณผํ•˜๋‹ต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ข‹์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„  ๋งŽ์€ ๊ต์œก๊ณผ ๊ฒฝํ—˜์ด ํ•„์š”ํ•˜์ง€์š”. ์ œ ๊ฐ•์ขŒ๋ฅผ ๋ณด๊ณ  ๊ณ„์‹  ์—ฌ๋Ÿฌ๋ถ„ ์ค‘์— ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ๋ถ„์ด ์žˆ๋‹ค๋ฉด ํ•™๊ต ๊ณต๋ถ€(ํŠนํžˆ ์˜์–ด, ์ˆ˜ํ•™)๋„ ์—ด์‹ฌํžˆ ํ•˜์‹œ๊ณ , ์ฃผ์œ„์˜ ์„ ๋ฐฐ๋“ค, ๊ด€๋ จ ํ™ˆํŽ˜์ด์ง€๋‚˜ ์›”๊ฐ„์ง€๋ฅผ ํ†ตํ•ด์„œ ์ •๋ณด๋ฅผ ์–ป๋Š”๋‹ค๋ฉด ๋„์›€์ด ๋˜์‹ค ๊ฑฐ์˜ˆ์š”. ์˜ค๋Š˜๋„ ํ•จ์ˆ˜์— ๊ด€ํ•œ ์ด์•ผ๊ธฐ๋ž๋‹ˆ๋‹ค. ์ œ๋ชฉ์—” ๋ณ€์ˆ˜๋ผ๊ณ  ๋‚˜์™€์žˆ์ง€๋งŒ์š”. ๋จผ์ € ์˜ˆ์ œ๋ฅผ ๋ณด์‹ค๊นŒ์š”? ํ•™๊ต ๋‹ค๋‹ ๋•Œ, ์ €ํฌ ํ•™๊ต ์งฑ์€ ์˜๊ตฌ์˜€์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ „ํ•™ ๊ฐ€๊ธฐ ์ „๊นŒ์ง€๋Š”โ€ฆ >>> jjang = '09' ์ œ๊ฐ€ ๊ฐ€์„œ ๋ฐ”๋กœ ์งฑ ๋จน์—ˆ์ง€์š”. ํํํโ€ฆ >>> jjang = 'pig dad' ์• ๋“คํ•œํ…Œ ๋ฌผ์–ด๋ณด๋ฉด ๋ˆ„๊ฐ€ ์ตœ๊ณ ๋ผ๊ณ  ํ• ๊นŒ์š”? ๋‹น๊ทผโ€ฆ >>> jjang 'pig dad' ๊ทธ๋Ÿฐ๋ฐ ์ž๊ธฐ๋„ค ๋ฐ˜์—์„œ ์ตœ๊ณ ๋ผ๊ณ  ๊น์ฃฝ๊ฑฐ๋ฆฌ๋Š” ๋…€์„์ด ์žˆ์—ˆ์œผ๋‹ˆ, ๋ฐ”๋กœ ๋•ก์น ์ด๋ผ๋Š” ์นœ๊ตฌ์˜€๋‹ต๋‹ˆ๋‹ค. ๋ฐ˜์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ฃ . >>> def ban(): ... jjang = '07' ... print('jjang =', jjang) ... >>> ban() jjang = 07 ๊ทธ๋Ÿฌ๋‚˜โ€ฆ ๋•ก์น ์ด๋„ ์ œ ์•ž์—์„  ๊นจ๊ฐฑ~์ด๋ž๋‹ˆ๋‹ค. ์šธ ํ•™๊ต ์งฑ์€ ๋ณ€ํ•จ์—†์ด ์ €๊ฑธ๋ž‘์š”โ€ฆ >>> jjang 'pig dad' ban() ํ•จ์ˆ˜ ์•ˆ์—์„œ jjang = '07'์ด๋ผ๊ณ  ํ•˜๋ฉด jjang ์ด๋ž€ ๋ณ€์ˆ˜๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“œ๋Š” ๊ฑฐ๊ณ ์š”, ๊ธฐ์กด์˜ jjang์—๋Š” ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , ban ํ•จ์ˆ˜๊ฐ€ ๋๋‚  ๋• ๊ทธ ํ•จ์ˆ˜ ๋‚ด์—์„œ ๋งŒ๋“ค์—ˆ๋˜ ๋ณ€์ˆ˜๋“ค์€ ๋ชจ๋‘ ์—†์–ด์ง€๋Š” ๊ฑฐ์ฃ . ์ด์™€ ๊ฐ™์ด ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋งŒ๋“ค์–ด์ง„ ๋ณ€์ˆ˜๋ฅผ ์ง€์—ญ๋ณ€์ˆ˜๋ผ๊ณ  ํ•˜๊ณ , ํ•จ์ˆ˜ ๋ฐ–์—์„œ ๋งŒ๋“ค์–ด์ง„ ๋ณ€์ˆ˜๋ฅผ ์ „์—ญ๋ณ€์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ง€์—ญ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ๋งŒ๋“ค์–ด์ ธ์„œ, ํ•จ์ˆ˜์˜ ์‹คํ–‰์ด ๋๋‚  ๋•Œ ํ•จ๊ป˜ ์—†์–ด์ง€๋Š” ๋ฐ˜๋ฉด, ์ „์—ญ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜์™€๋Š” ๊ด€๊ณ„์—†์ด ํ•ญ์ƒ ๊ฟ‹๊ฟ‹์ด ์ง€๊ตฌ๋ฅผ ์ง€ํ‚จ๋‹ต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์˜์–ด๋กœ ์ „์—ญ๋ณ€์ˆ˜๋ฅผ global์ด๋ผ๋Š” ๋ง๋กœ ํ‘œํ˜„ํ•˜์ง€์š”โ€ฆ ์ง€์—ญ๋ณ€์ˆ˜๋ฅผ ํ•จ์ˆ˜ ๋ฐ–์—์„œ ํ•œ๋ฒˆ ๋ถˆ๋Ÿฌ๋ณผ๊นŒ์š”? >>> def d_is_10(): ... d = 10 # ์ง€์—ญ๋ณ€์ˆ˜ ... print('d ๊ฐ’์€ ', d, '์ž…๋‹ˆ๋‹ค') ... >>> d_is_10() d ๊ฐ’์€ 10์ž…๋‹ˆ๋‹ค >>> d Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'd' is not defined d๋ฅผ ๋ถˆ๋Ÿฌ๋ด๋„ 'd๋ผ๋Š” ์ด๋ฆ„์ด ์—†๋‹ค'๋ผ๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋งŒ ๋œจ์ง€์š”? d_is_10() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋™์•ˆ์€ d๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ, ํ•จ์ˆ˜์˜ ์‹คํ–‰์ด ๋๋‚œ ๋‹ค์Œ์— ํ•จ๊ป˜ ์‚ฌ๋ผ์ ธ๋ฒ„๋ ธ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ, ์ „์—ญ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ์•ˆ์—์„œ๋„ ์–ผ๋งˆ๋“ ์ง€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. >>> x = 10 # ์ „์—ญ๋ณ€์ˆ˜ >>> def printx(): ... print(x) ... >>> printx() 10 ๊ทธ๋ ‡๋‹ค๋ฉด ์ง€์—ญ๋ณ€์ˆ˜ ๋Œ€์‹  ์ „์—ญ ๋ณ€์ˆ˜๋งŒ ์“ฐ๋Š” ๊ฒƒ์ด ํŽธํ•˜๊ฒ ๋‹ค๊ณ ์š”? ๊ธ€์Ž„์š”โ€ฆ ์ „์—ญ๋ณ€์ˆ˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ๋ณต์žกํ•ด์งˆ์ˆ˜๋ก ๊ณจ์นซ๊ฑฐ๋ฆฌ๊ฐ€ ๋œ๋‹ต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์—‰๋šฑํ•œ ํ•จ์ˆ˜ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด๋ฒ„๋ฆฌ๋Š” ์ˆ˜๊ฐ€ ์ข…์ข… ์žˆ๊ฑฐ๋“ ์š”. ๊ทธ๋ž˜์„œ ํ•„์š”์— ๋”ฐ๋ผ ์ง€์—ญ๋ณ€์ˆ˜์™€ ์ „์—ญ๋ณ€์ˆ˜๋ฅผ ๊ณจ๋ผ ์“ฐ๋Š” ๊ฒƒ์ด ์ข‹๋‹ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ „์—ญ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค. ์–ด๋–ค ๋ณ€์ˆ˜๋ฅผ ์ „์—ญ๋ณ€์ˆ˜(global)๋กœ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ๋ช…์‹œํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์ฃ . >>> def e_is_10(): ... global e # ์ „์—ญ๋ณ€์ˆ˜ ... e = 10 ... print('e ๊ฐ’์€ ', e, '์ž…๋‹ˆ๋‹ค') ... >>> e_is_10() e ๊ฐ’์€ 10์ž…๋‹ˆ๋‹ค >>> e 10 ์—ฌ๊ธฐ์„œ๋Š” e_is_10() ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ e๋ผ๋Š” ์ „์—ญ๋ณ€์ˆ˜๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ , ์ด ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜์˜ ์‹คํ–‰์ด ๋๋‚œ ๋‹ค์Œ์—๋„ ์—†์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๊ทธ๋งŒ ์‹œํ—˜ ๋ณด๋Ÿฌ ๊ฐ€์•ผ๊ฒ ๋„ค์š”. ์ฆ๊ฑฐ์šด ์ฃผ๋ง ๋ณด๋‚ด์„ธ์š”~. 3.5 ๋žŒ๋‹ค(lambda) ์˜ค๋Š˜์€ ๋žŒ๋‹ค<NAME>๊ณผ ๊ทธ๊ฒƒ์„ ์ด์šฉํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ•จ์ˆ˜๋“ค์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹น์žฅ ์™„๋ฒฝํ•˜๊ฒŒ ์†Œํ™”ํ•˜์‹ค ํ•„์š”๋Š” ์—†์„ ๊ฒƒ ๊ฐ™๊ณ ์š”, ๊ฐ€๋ฒผ์šด ๋งˆ์Œ์œผ๋กœ ์ด๋Ÿฐ ๊ฒƒ์ด ์žˆ๋‹ค๋Š” ์ •๋„๋งŒ ์•„์…”๋„ ๋˜์ง€ ์•Š์„๊นŒ ํ•ฉ๋‹ˆ๋‹ค. ๋žŒ๋‹ค<NAME>์€ ์ธ๊ณต์ง€๋Šฅ ๋ถ„์•ผ๋‚˜ AutoCAD๋ผ๋Š” ์„ค๊ณ„ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์“ฐ์ด๋Š” Lisp ์–ธ์–ด์—์„œ ๋ฌผ๋ ค๋ฐ›์•˜๋‹ค๊ณ  ํ•˜๋Š”๋ฐ์š”, ํ•จ์ˆ˜๋ฅผ ๋”ฑ ํ•œ ์ค„๋งŒ์œผ๋กœ ๋งŒ๋“ค๊ฒŒ ํ•ด์ฃผ๋Š” ํ›Œ๋ฅญํ•œ ๋…€์„์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์จ์ฃผ๋ฉด ๋˜์ง€์š”. lambda ๋งค๊ฐœ๋ณ€์ˆ˜ : ํ‘œํ˜„์‹ ๋‹ค์Œ์€ ๋‘ ์ˆ˜๋ฅผ ๋”ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. >>> def hap(x, y): ... return x + y ... >>> hap(10, 20) 30 ์ด๊ฒƒ์„ ๋žŒ๋‹ค<NAME>์œผ๋กœ๋Š” ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ• ๊นŒ์š”? >>> (lambda x, y: x + y)(10, 20) 30 ๋„ˆ๋ฌด๋‚˜ ๊ฐ„๋‹จํ•˜์ฃ ? ํ•จ์ˆ˜๊ฐ€ ์ด๋ฆ„์กฐ์ฐจ๋„ ์—†์Šต๋‹ˆ๋‹ค. '๊ทธ๋ƒฅ 10 + 20์ด๋ผ๊ณ  ํ•˜๋ฉด ๋˜์ง€'๋ผ๊ณ  ๋ง์”€ํ•˜์‹œ๋ฉด ๋ฏธ์›Œ์ž‰~. ๋ช‡ ๊ฐ€์ง€ ํ•จ์ˆ˜๋ฅผ ๋” ๋ฐฐ์›Œ๋ณด๋ฉด์„œ ๋žŒ๋‹ค๊ฐ€ ์–ด๋–ป๊ฒŒ ์ด์šฉ๋˜๋Š”์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ฃ . map() ๋จผ์ € map ํ•จ์ˆ˜๋ฅผ ๋ณผ๊นŒ์š”? map(ํ•จ์ˆ˜, ๋ฆฌ์ŠคํŠธ) ์ด ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜์™€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ธ์ž๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ฃ ? ๊ทธ๋ฆฌ๊ณ , ๋ฆฌ์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ์›์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ํ•จ์ˆ˜๋ฅผ ์ ์šฉ์‹œํ‚จ ๋‹ค์Œ, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„์ค€๋‹ต๋‹ˆ๋‹ค. ๋ง์ด ์ข€ ๋ณต์žกํ•˜์ฃ ? ๊ทธ๋Ÿด ๋•Œ ์˜ˆ์ œ๋ฅผ ๋ณด๋Š” ๊ฒŒ ์ตœ๊ณ ์ฃ . >>> map(lambda x: x ** 2, range(5)) # ํŒŒ์ด์ฌ 2 [0, 1, 4, 9, 16] >>> list(map(lambda x: x ** 2, range(5))) # ํŒŒ์ด์ฌ 2 ๋ฐ ํŒŒ์ด์ฌ 3 [0, 1, 4, 9, 16] ์œ„์˜ map ํ•จ์ˆ˜๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ํ•จ์ˆ˜๋Š” lambda x: x ** 2๊ตฌ์š”, ๋ฆฌ์ŠคํŠธ๋กœ๋Š” range(5)๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. range ํ•จ์ˆ˜๋Š” ์•Œ๊ณ  ๊ณ„์‹œ์ฃ ? range(5)๋ผ๊ณ  ์จ์ฃผ๋ฉด [0, 1, 2, 3, 4]๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ ค์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  x ** 2๋ผ๋Š” ๊ฒƒ์€ x ๊ฐ’์„ ์ œ๊ณฑํ•˜๋ผ๋Š” ์—ฐ์‚ฐ์ž์ฃ . map ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ์›์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ํ•จ์ˆ˜๋ฅผ ์ ์šฉ์‹œํ‚จ ๊ฒฐ๊ณผ๋ฅผ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„์ฃผ๋‹ˆ๊นŒ, ์œ„์˜ ์˜ˆ์ œ๋Š” 0์„ ์ œ๊ณฑํ•˜๊ณ , 1์„ ์ œ๊ณฑํ•˜๊ณ , 2, 3, 4๋ฅผ ์ œ๊ณฑํ•œ ๊ฒƒ์„ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ๋ฅผ ๋žŒ๋‹ค๊ฐ€ ์•„๋‹Œ ๋ณดํ†ต์˜ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? reduce() ์ด๋ฒˆ์—” reduce ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ด…์‹œ๋‹ค. reduce(ํ•จ์ˆ˜, ์‹œํ€€์Šค) ํ˜•์‹์€ ์œ„์™€ ๊ฐ™๊ณ ์š”, ์‹œํ€€์Šค(๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ)์˜ ์›์†Œ๋“ค์„ ๋ˆ„์ ์ ์œผ๋กœ(?) ํ•จ์ˆ˜์— ์ ์šฉ์‹œํ‚จ๋‹ต๋‹ˆ๋‹ค. ๋ง์ด ์ง„์งœ ์–ด๋ ต๊ตฐ์š”. ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. >>> from functools import reduce # ํŒŒ์ด์ฌ 3์—์„œ๋Š” ์จ์ฃผ์…”์•ผ ํ•ด์š” >>> reduce(lambda x, y: x + y, [0, 1, 2, 3, 4]) 10 ์œ„์˜ ์˜ˆ์ œ๋Š” ๋จผ์ € 0๊ณผ 1์„ ๋”ํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ์— 2๋ฅผ ๋”ํ•˜๊ณ , ๊ฑฐ๊ธฐ๋‹ค๊ฐ€ 3์„ ๋”ํ•˜๊ณ , ๋˜ 4๋ฅผ ๋”ํ•œ ๊ฐ’์„ ๋Œ๋ ค์ค๋‹ˆ๋‹ค. ํ•œ๋งˆ๋””๋กœ ์ „๋ถ€ ๋‹ค ๋”ํ•˜๋ผ๋Š” ๊ฒ๋‹ˆ๋‹ค. ์ƒ๊ฐ๋ณด๋‹ค ์‰ฝ์ฃ ? ํ•˜โ€ฆ ํ•˜โ€ฆ ํ•˜โ€ฆ ์˜๊ธฐ์–‘์–‘ํ•˜๊ฒŒ '์˜ˆ'๋ผ๊ณ  ๋Œ€๋‹ตํ•˜์‹  ๋ถ„๋“ค์„ ์œ„ํ•ด ์งœ์ฆ ๋‚˜๋Š” ์˜ˆ์ œ๋ฅผ ๊ถŒํ•ด๋“œ๋ฆฌ์ฃ ~. >>> reduce(lambda x, y: y + x, 'abcde') 'edcba' ์ „ ์›๋ž˜ ๋ญ ํ•˜๋‚˜๋ฅผ ๋ฐฐ์šฐ๋ฉด ๊ผญ ์—ฝ๊ธฐ์ ์ธ ์‹คํ—˜์„ ํ•ด๋ณธ๋‹ต๋‹ˆ๋‹ค. ๋” ์งœ์ฆ ๋‚˜๋ผ๊ณ  ์„ค๋ช… ์•ˆ ํ•ด๋“œ๋ฆด๋ž๋‹ˆ๋‹ค~ filter() ๊ทธ๋‹ค์Œ์€ filter๋ฅผ ์‚ดํŽด๋ณผ ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ํ•„ํ„ฐ๊ฐ€ ๋ญ์ฃ ? ์ •์ˆ˜๊ธฐ์—์„œ ๋ฌผ์„ ๊ฑธ๋Ÿฌ์ฃผ๋Š” ๊ฒƒ์ด ํ•„ํ„ฐ์ฃ ? ์—์–ด์ปจ์˜ ๋ฐ”๋žŒ ๋“ค์–ด๊ฐ€๋Š” ๊ณณ์—๋„ ํ•„ํ„ฐ๊ฐ€ ๋‹ฌ๋ ค์žˆ๊ณ ์š”. filter(ํ•จ์ˆ˜, ๋ฆฌ์ŠคํŠธ) ํŒŒ์ด์ฌ์˜ ํ•„ํ„ฐ๋Š” ์ด๋ ‡๊ฒŒ ์ƒ๊ฒผ๋Š”๋ฐ์š”, ๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด์žˆ๋Š” ์›์†Œ๋“ค์„ ํ•จ์ˆ˜์— ์ ์šฉ์‹œ์ผœ์„œ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ธ ๊ฐ’๋“ค๋กœ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ๋‹ค์Œ์€ 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ๋ฆฌ์ŠคํŠธ ์ค‘์—์„œ 5๋ณด๋‹ค ์ž‘์€ ๊ฒƒ๋งŒ ๋Œ๋ ค์ฃผ๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. >>> filter(lambda x: x < 5, range(10)) # ํŒŒ์ด์ฌ 2 [0, 1, 2, 3, 4] >>> list(filter(lambda x: x < 5, range(10))) # ํŒŒ์ด์ฌ 2 ๋ฐ ํŒŒ์ด์ฌ 3 [0, 1, 2, 3, 4] lambda x: x<5๋ผ๊ณ  ์“ฐ๋‹ˆ๊นŒ ์™ ์ง€ ์ˆ˜ํ•™ ์ฑ…์—์„œ ๋ณธ ๋“ฏํ•œ ๋Š๋‚Œ์ด ๋“ค์ง€ ์•Š์Šต๋‹ˆ๊นŒ? ์ˆ˜ํ•™์ž๋“ค์ด ํŒŒ์ด์ฌ์„ ์ข‹์•„ํ•œ๋‹ค๋˜๋ฐโ€ฆ ์œ„์˜ ์˜ˆ์ œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋Œ์•„๊ฐ€๋Š”์ง€๋Š” ์ฒ™ ๋ณด๋ฉด ์•„์‹œ๊ฒ ์ฃ ? 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ๋ฆฌ์ŠคํŠธ์—์„œ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋ƒ…๋‹ˆ๋‹ค. ๊ทธ ์ˆซ์ž๋ฅผ x๋ผ ํ•˜๊ณ , x < 5 ๊ฐ€ '์ฐธ'์ด๋ฉด ์‚ด๋ ค์ค๋‹ˆ๋‹ค. ์‚ด์•„๋‚จ์€ ๊ฒƒ๋“ค์€ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ๋. ์ž, ์ด๋ฒˆ์—” ํ™€์ˆ˜๋งŒ ๋Œ๋ ค์ฃผ๋Š” filter๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค. ๋จผ์ € ํ™€์ˆ˜๊ฐ€ ๋ญ”์ง€ ์ƒ๊ฐํ•ด ๋ณผ๊นŒ์š”? ์ง์ˆ˜๋Š” 2๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š” ์ˆ˜์ด๊ณ , ํ™€์ˆ˜๋Š” 2๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€์ง€ ์•Š๋Š” ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ง์ˆ˜๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋Š” 0์ด๊ณ , ํ™€์ˆ˜๋ฅผ 2๋กœ ๋‚˜๋ˆ„๋ฉด ๋‚˜๋จธ์ง€๊ฐ€ 1์ด์ฃ . ๋˜, ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•  ๋• %๋ผ๋Š” ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ, 50์„ 8๋กœ ๋‚˜๋ˆ„๋ฉด ๋ชซ์€ 6์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” 2๋‹ˆ๊นŒ 50 % 8์€ 2๊ฐ€ ๋˜๋Š” ๊ฑฐ์ง€์š”. ์ด์ œ ํ™€์ˆ˜๋ฅผ ๋Œ๋ ค์ฃผ๋Š” ํ•„ํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> filter(lambda x: x % 2, range(10)) # ํŒŒ์ด์ฌ 2 [1, 3, 5, 7, 9] >>> list(filter(lambda x: x % 2, range(10))) # ํŒŒ์ด์ฌ 2 ๋ฐ ํŒŒ์ด์ฌ 3 [1, 3, 5, 7, 9] ์ง€๋‚œ ์‹œ๊ฐ„์— '์ฐธ'์€ 1์ด๊ณ  '๊ฑฐ์ง“'์€ 0์ด๋ผ๊ณ  ํ–ˆ์ฃ ? ์œ„์˜ filter ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด, 0์„ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋Š” 0์ด๋‹ˆ๊นŒ ๋žŒ๋‹ค ํ•จ์ˆ˜์˜ ๊ฒฐ๊ด๊ฐ’์€ 0์ด๊ณ , 0์€ '๊ฑฐ์ง“'์ด๋‹ˆ๊นŒ ๋ฒ„๋ ค์ง‘๋‹ˆ๋‹ค. 1์„ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋Š” 1์ด๋‹ˆ๊นŒ ๋žŒ๋‹ค ํ•จ์ˆ˜์˜ ๊ฒฐ๊ด๊ฐ’์€ 1์ด๊ณ , 1์€ '์ฐธ'์ด๋‹ˆ๊นŒ ํ†ต๊ณผํ•˜์ง€์š”. ์ด๋Ÿฐ ์‹์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ํ™€์ˆ˜๋งŒ ๋Œ๋ ค์ฃผ๊ฒŒ ๋˜๋Š” ๊ฑฐ์ง€์š”. ํœด~ ์ด๋ฒˆ ๊ฐ•์ขŒ๋Š” ์„ค๋ช…ํ•˜๊ธฐ ํž˜๋“ค์–ด์„œ ์“ฐ๋ฉด์„œ ํ•œ์ฐธ ์• ๋จน์—ˆ๋„ค์š”. ์ž˜ ์ดํ•ด๋˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์„ธ์š”. ๊ทธ๋Ÿผ ์•ˆ๋…•~ 3.5.1 ์—ฐ์Šต ๋ฌธ์ œ: ๋†€์ด๊ณต์› (1) ๋‘˜๋ฆฌ์™€ ๋„์šฐ๋„ˆ, ๋งˆ์ด์ฝœ์ด ๋†€์ด๊ณต์›์— ๊ฐ”์Šต๋‹ˆ๋‹ค. ๋†€์ด ๊ธฐ๊ตฌ ์ค‘์—๋Š” ํƒ‘์Šน์ž์˜ ํ‚ค๋ฅผ ์ œํ•œํ•˜๋Š” ๊ฒƒ์ด ์žˆ๋„ค์š”. ๋ฌธ์ œ ๋†€์ด ๊ธฐ๊ตฌ์˜ ์ด๋ฆ„๊ณผ ํ‚ค ์ œํ•œ์„ ๋‚˜ํƒ€๋‚ธ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„์„œ, ๋†€์ด ๊ธฐ๊ตฌ์˜ ์ด๋ฆ„, ํƒ‘์Šน ๊ฐ€๋Šฅํ•œ ํ‚ค์˜ ํ•˜ํ•œ(ไธ‹้™)๊ณผ ์ƒํ•œ(ไธŠ้™)์„ ๊ฐ ํ–‰์— ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉฐ, ch03 ํด๋” ์•„๋ž˜์— ํŒŒ์ผ๋ช…์„ ridereader.py๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. def read(text): # ์ด๊ณณ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. return ridename, cmmin, cmmax if __name__ == "__main__": ridename, cmmin, cmmax = read(input()) print("์ด๋ฆ„:", ridename) print("ํ•˜ํ•œ:", cmmin) print("์ƒํ•œ:", cmmax) ์ž…์ถœ๋ ฅ ์˜ˆ 1 ์ž…๋ ฅ: ์™€์ผ๋“œ ์œ™: 110cm ์ด์ƒ ์ถœ๋ ฅ: ์ด๋ฆ„: ์™€์ผ๋“œ ์œ™ ํ•˜ํ•œ: 110 ์ƒํ•œ: None ์˜ˆ 2 ์ž…๋ ฅ: ํˆผ ์˜ค๋ธŒ ํ˜ธ๋Ÿฌ: - ์ถœ๋ ฅ: ์ด๋ฆ„: ํˆผ ์˜ค๋ธŒ ํ˜ธ๋Ÿฌ ํ•˜ํ•œ: None ์ƒํ•œ: None ์˜ˆ 3 ์ž…๋ ฅ: ํ”Œ๋ผ์ด๋ฒค์ฒ˜: 140cm~195cm ์ถœ๋ ฅ: ์ด๋ฆ„: ํ”Œ๋ผ์ด๋ฒค์ฒ˜ ํ•˜ํ•œ: 140 ์ƒํ•œ: 195 tip ๋ฌธ์ž์—ด์˜ split ๋ฉ”์„œ๋“œ๋ฅผ ์จ์„œ, ๊ตฌ๋ถ„์ž(delimiter)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์—์„œ๋Š” ์ฝœ๋ก (:)์„ ๊ตฌ๋ถ„์ž๋กœ ์‚ผ์•˜์Šต๋‹ˆ๋‹ค. >>> hms = "13:48:03" >>> hms.split(':') ['13', '48', '03'] str.strip์œผ๋กœ ๋ฌธ์ž์—ด ์•ž๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> str.strip(" I am a boy. ") 'I am a boy.' ๊ทธ๋ฆฌ๊ณ  replace() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž์—ด ์ผ๋ถ€๋ฅผ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> 'Python'.replace('P', 'J') 'Jython' ch03/ridereader_nolambda.py (for ๋ฌธ์„ ์‚ฌ์šฉ) ch03/ridereader.py (๋žŒ๋‹ค๋ฅผ ์‚ฌ์šฉ) 4. ๋ฐ์ดํ„ฐ ํƒ€์ž… ํŒŒ์ด์ฌ์˜ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ ํƒ€์ž…(data type)์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ์ด ์žฅ์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฒƒ: ์ž๋ฃŒํ˜• ๋ฌธ์ž์—ด(str)๊ณผ ๋ฆฌ์ŠคํŠธ(list) ํŠœํ”Œ(tuple) ๋”•์…”๋„ˆ๋ฆฌ(dict) ์„ธํŠธ(set) 4.1 ์ž๋ฃŒํ˜• ์—ฌ๋Ÿฌ๋ถ„ ์ด์ง„์ˆ˜์— ๋Œ€ํ•ด์„œ ์•Œ๊ณ  ๊ณ„์‹œ์ง€์š”? ์•„๋งˆ ์ค‘ํ•™๊ต ๋•Œ ๋ฐฐ์› ๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”. ์•„์ง ๋ฐฐ์šฐ์ง€ ์•Š์€ ๋ถ„์€ ๊ทธ๋Ÿฐ ๊ฒƒ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ๋งŒ ์•Œ๊ณ  ๊ณ„์‹œ๊ณ ์š”. ์ปดํ“จํ„ฐ์—์„œ๋Š” ์ด์ง„์ˆ˜๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ปดํ“จํ„ฐ ๋‚ด๋ถ€์—์„œ๋Š” ๋ชจ๋“  ์ •๋ณด๋ฅผ ์ด์ง„์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ์˜ˆ๋ฅผ ๋“ค์–ด์„œ 65๋ผ๋Š” ์ˆซ์ž๋ฅผ ์ปดํ“จํ„ฐ ๋‚ด๋ถ€์—์„œ๋Š” ์ด์ง„์ˆ˜ 01000001๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ 65 + 30์ด๋ผ๊ณ  ๋ช…๋ น์„ ๋‚ด๋ฆฌ๋ฉด ์ปดํ“จํ„ฐ๋Š” ๊ทธ๊ฒƒ๋“ค์„ ๋ชจ๋‘ ์ด์ง„์ˆ˜๋กœ ๋ฐ”๊ฟ”์„œ ๊ณ„์‚ฐ์„ ํ•œ ๋‹ค์Œ์— ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์šฐ๋ฆฌ๊ฐ€ ์“ฐ๋Š” ์‹ญ์ง„์ˆ˜๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฑฐ์ฃ . ์ปดํ“จํ„ฐ๋Š” ์ˆซ์ž๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฌธ์ž๋ผ๋“ ์ง€, ์ œ์•„๋ฌด๋ฆฌ ๋ณต์žกํ•œ ์ •๋ณด๋„ ๋ชจ๋‘ 2์ง„์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ต๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ์—์„œ ์˜์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•ŒํŒŒ๋ฒณ ํ•œ ์ž ํ•œ ์ž๋งˆ๋‹ค ์ˆซ์ž๋กœ ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ฒจ์„œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€์š”. ์•ŒํŒŒ๋ฒณ์— ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ด๋Š” ๊ทœ์น™ ์ค‘์—์„œ ๋„๋ฆฌ ์“ฐ์ด๋Š” ๊ฒƒ์œผ๋กœ ASCII(์•„์Šคํ‚ค)๋ผ๋Š” ๊ทœ์•ฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„์Šคํ‚ค์—์„œ๋Š” ์•ŒํŒŒ๋ฒณ A๋ฅผ ์ˆซ์ž 65๋กœ ํ‘œํ˜„ํ•˜๋Š”๋ฐ์š”, ์–ด์ฐจํ”ผ ์ˆซ์ž 65๋Š” ๋‹ค์‹œ ์ด์ง„์ˆ˜๋กœ ๋ฐ”๊ฟ”์„œ ์ฒ˜๋ฆฌํ•˜๊ฒ ์ฃ ? ๊ทธ๋ ‡๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ์ด์ƒํ•œ ์ ์ด ์ƒ๊น๋‹ˆ๋‹ค. ์ด์ง„์ˆ˜ 01000001์ด ์žˆ๋Š”๋ฐ ์ปดํ“จํ„ฐ๋Š” ์ด๊ฒƒ์ด ์ˆซ์ž 65์ธ์ง€, ์•„๋‹ˆ๋ฉด ๋ฌธ์ž A ์ธ์ง€ ์–ด๋–ป๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ์š”? ๋„ˆ๋ฌด ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•˜์‹ค ๊ฑด ์—†๋‹ต๋‹ˆ๋‹ค. ์ˆซ์ž์ธ์ง€ ๋ฌธ์ž์ธ์ง€ ํ‘œ์‹œ๋ฅผ ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฑฐ์ฃ . ์‚ฌ๋žŒ์ด ํ•ด์ฃผ๋“ ์ง€, ์ปดํ“จํ„ฐ๊ฐ€ ์•Œ์•„์„œ ํ•˜๋“ ์ง€ ๋ง์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ‘œ์‹œ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ์ž๋ฃŒํ˜•์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์ž๋ฃŒํ˜•์— ๋Œ€ํ•ด์„œ ์ž˜ ๋ชฐ๋ผ๋„ ํ”„๋กœ๊ทธ๋žจ์„ ์งค ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ์€ ์šฐ๋ฆฌ๊ฐ€ ์ž๋ฃŒ๋ฅผ ๋งŒ๋“ค ๋•Œ๋งˆ๋‹ค ํŒŒ์ด์ฌ์—์„œ ์ž๋™์œผ๋กœ ์ž๋ฃŒํ˜•์„ ์ •ํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ์š”, ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ง์ ‘ ์ •ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋งˆ๋‹ค ์ œ๊ณตํ•ด ์ฃผ๋Š” ์ž๋ฃŒํ˜•์— ์ฐจ์ด๊ฐ€ ์žˆ์ง€์š”. type() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ž๋ฃŒํ˜•์„ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> type(6) # ์ •์ˆ˜ <type 'int'> >>> type('A') # ๋ฌธ์ž์—ด <type 'str'> ๊ทธ๋ ‡๋‹ค๋ฉด ํŒŒ์ด์ฌ์—๋Š” ์–ด๋–ค ์ž๋ฃŒํ˜•์ด ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณผ๊นŒ์š”? ํŒŒ์ด์ฌ์˜ ์ž๋ฃŒํ˜•์€ ํฌ๊ฒŒ ์ˆซ์ž(numbers), ์‹œํ€€์Šค(sequence), ๋งคํ•‘(mapping) ๋“ฑ์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆซ์ž ์ˆซ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ž๋ฃŒํ˜•์œผ๋กœ๋Š” ์ •์ˆ˜(int), ๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜(float), ๋ณต์†Œ์ˆ˜(complex)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. int int๋Š” ์ •์ˆ˜(integer)๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. >>> type(100000000) # ์ •์ˆ˜ <class 'int'> ์ •์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์„œ ์ฝ๊ธฐ ํž˜๋“ค๋ฉด ๋ฐ‘์ค„์„ ๋„ฃ์–ด๋„ ๋ผ์š”.(ํŒŒ์ด์ฌ 3.6 ์ด์ƒ) >>> 100_000_000 # ์„ธ ์ž๋ฆฌ๋งˆ๋‹ค 100000000 >>> 1_0000_0000 # ๋„ค ์ž๋ฆฌ๋งˆ๋‹ค 100000000 float float๋Š” ์›๋ž˜ ๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜(floating-point number)๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”๋ฐ, ์ง€๊ธˆ์€ ๋‹จ์ˆœํžˆ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋ผ๊ณ  ์ƒ๊ฐํ•˜์…”๋„ ์ข‹์Šต๋‹ˆ๋‹ค. >>> type(2.8) # ๋ถ€๋™์†Œ์ˆ˜์ ์ˆ˜ <type 'float'> 1์žฅ์—์„œ ๋ดค๋“ฏ์ด, int๋ผ๋ฆฌ ์—ฐ์‚ฐํ•œ ๊ฒฐ๊ณผ๊ฐ€ float๋กœ ๋‚˜์˜ค๊ธฐ๋„ ํ•ด์š”. >>> 5 / 3 1.6666666666666667 complex ๊ทธ๋ฆฌ๊ณ  ๊ณ ๋“ฑํ•™๊ต์—์„œ ๋ฐฐ์šฐ๋Š” ๋ณต์†Œ์ˆ˜๋ฅผ complex๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. >>> type(3+4j) # ๋ณต์†Œ์ˆ˜ <type 'complex'> ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์— ๋ณต์†Œ์ˆ˜๋ผ๋Š” ์ž๋ฃŒํ˜•์ด ์žˆ๋Š” ๊ฒƒ์€ ํŒŒ์ด์ฌ์—์„œ ์ฒ˜์Œ ๋ดค๋„ค์š”. ์ œ๊ณฑํ•˜๋ฉด -1์ด ๋˜๋Š” ์ˆ˜๋ฅผ โ€˜ํ—ˆ์ˆ˜(imaginary number)โ€™๋ผ๊ณ  ํ•˜์ฃ . 2 โˆ’ ํŒŒ์ด์ฌ์—์„œ๋Š” ํ—ˆ์ˆ˜๋ฅผ j๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. >>> (1j) ** 2 (-1+0j) ๊ณ ๋“ฑํ•™์ƒ ์ด์ƒ ์–ธ๋‹ˆ๋“ค์„ ์œ„ํ•ด ํ•œ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๋ฉด, ๋ณต์†Œ์ˆ˜์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ ( + ) 10 ์„ ์†์œผ๋กœ ๊ณ„์‚ฐํ•˜๋Š” ๊ณผ์ •์€ ์ด๋ ‡๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.1 ( + ) = + i i = i ( + ) 10 { ( + ) } = ( i ) = 5 i = 32 ( 2 ) i 32 ํŒŒ์ด์ฌ์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ์–ด์š”. >>> (1 + 1j) ** 10 32j ์‹œํ€€์Šค ๋ฌธ์ž์—ด(str), ๋ฆฌ์ŠคํŠธ(list), ํŠœํ”Œ(tuple), ์‚ฌ์šฉ์ž ์ •์˜ ํด๋ž˜์Šค๊ฐ€ ์‹œํ€€์Šค์— ์†ํ•ฉ๋‹ˆ๋‹ค. >>> type("Love your Enemies, for they tell you your Faults.") <class 'str'> >>> type(['love', 'enemy', 'fault']) <class 'list'> >>> type(('love', 'enemy', 'fault')) <class 'tuple'> for ๋ฌธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์ด ๋ฐ”๋กœ ์‹œํ€€์Šค์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์ด ์‹œํ€€์Šค์— ์†ํ•˜๋„ค์š”. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ํ•œ ์ค„๋กœ ์„ธ์›Œ๋’€์œผ๋‹ˆ ๊ทธ๋Ÿด ๋ฒ•๋„ ํ•˜๊ฒ ์ฃ ? ํŠœํ”Œ๊ณผ ์‚ฌ์šฉ์ž ์ •์˜ ํด๋ž˜์Šค์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ์„ค๋ช…๋“œ๋ฆฌ์ง€์š”. ๋งคํ•‘ ๋”•์…”๋„ˆ๋ฆฌ(dict)๋Š” ํ‚ค(key)์™€ ๊ฐ’(value)์˜ ์ง์œผ๋กœ ์ด๋ค„์ง‘๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์„ ๋งคํ•‘์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. >>> type({'one': 1, 'two': 2, 'three': 3}) <class 'dict'> ์ฐธ, ๊ฑฐ์ง“์„ ํ‘œํ˜„ํ•˜๋Š” ๋ถˆ(bool)๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> type(False) <class 'bool'> >>> type(3 >= 1) <class 'bool'> >>> type(True == 'True') <class 'bool'> ์„ธํŠธ ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•˜๋Š” ์„ธํŠธ(set)๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> fruits = {'apple', 'banana', 'orange'} ์„ธํŠธ๋Š” ์›์†Œ์˜ ์ˆœ์„œ๊ฐ€ ์œ ์ง€๋˜์ง€ ์•Š๊ณ  ์ค‘๋ณต ์›์†Œ๋ฅผ ๊ฐ–์ง€ ์•Š๋Š” โ€˜์ง‘ํ•ฉโ€™์œผ๋กœ์„œ์˜ ํŠน์ง•์ด ์žˆ์œผ๋ฉฐ, ์ง‘ํ•ฉ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ•์ขŒ๊ฐ€ ํšŸ์ˆ˜๋ฅผ ๊ฑฐ๋“ญํ• ์ˆ˜๋ก ๋จธ๋ฆฌ๊ฐ€ ๋ณต์žกํ•ด์ง€๋„ค์š”. ์ œ๊ฐ€ ํ˜ผ์ž ๊ณต๋ถ€ํ–ˆ์œผ๋ฉด ํ์ง€๋ถ€์ง€ํ•  ๋•Œ๊ฐ€ ๋œ ๊ฑฐ์ง€์š”. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ๋Ÿฌ๋ถ„์ด ์ง€์ผœ๋ณด๊ณ  ๊ณ„์‹  ํ•œ ํฌ๊ธฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ๋ฐ”๋กœ ์ด๋Ÿฐ ์ ์„ ๋…ธ๋ฆฌ๊ณ  ํ˜ผ์ž ๊ณต๋ถ€ํ•˜์ง€ ์•Š๊ณ  ๊ตณ์ด ๊ฐ•์ขŒ๋ฅผ ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํํํ... ๊ทธ๋Ÿฌ๋‹ˆ ์—ฌ๋Ÿฌ๋ถ„๋„ ํž˜๋‚ด์„ธ์š”. ์šฐ๋ฆฌ ํ•จ๊ป˜ ํŒŒ์ด์ฌ ์ •๋ณตํ•˜๋Š” ๊ทธ๋‚ ๊นŒ์ง€~ ๊น€๋™์‹, โŸช์•Œ๊ธฐ ์‰ฝ๊ฒŒ ํ’€์–ด์“ด ๊ธฐ์ดˆ ๊ณตํ•™์ˆ˜ํ•™โŸซ, 2022, ์ƒ๋Šฅ์ถœํŒ โ†ฉ 4.2 ๋ฌธ์ž์—ด๊ณผ ๋ฆฌ์ŠคํŠธ ๋ฌธ์ž์—ด๊ณผ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ข€ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ด…์‹œ๋‹ค. ๋ฌธ์ž์—ด ๋ฌธ์ž์—ด์—์„œ๋Š” ์š”๋Ÿฐ ์‹์œผ๋กœ ํ•œ ๊ธ€์ž๋งˆ๋‹ค ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ธด๋‹ต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์–ด์„œ ์ด๊ฒƒ์ €๊ฒƒ ์‹œ์ผœ๋ณด์„ธ์š”. >>> x = 'banana' >>> x[0] # 0๋ฒˆ ๊ธ€์ž๋Š”? 'b' >>> x[2:4] # 2๋ฒˆ๋ถ€ํ„ฐ 4๋ฒˆ ์•ž(3๋ฒˆ)๊นŒ์ง€๋Š”? 'na' >>> x[:3] # ์ฒ˜์Œ๋ถ€ํ„ฐ 3๋ฒˆ ์•ž(2๋ฒˆ)๊นŒ์ง€๋Š”? 'ban' >>> x[3:] # 3๋ฒˆ๋ถ€ํ„ฐ ๋๊นŒ์ง€๋Š”? 'ana' ์ด๋ ‡๊ฒŒ ๋ฌธ์ž์—ด์˜ ๊ฐ ๊ธ€์ž ์œ„์น˜(์ธ๋ฑ์Šค)๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ์จ๋Š” ๋ฐฉ๋ฒ•์„ ์Šฌ๋ผ์ด์‹ฑ(slicing)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๊ด€ํ•ด์„œ๋Š” โ€˜์Šฌ๋ผ์ด์‹ฑโ€™์—์„œ ์ข€ ๋” ์•Œ์•„๋ณผ๊ฒŒ์š”. ๊ทธ๋Ÿฐ๋ฐ banana๋ฅผ nanana๋กœ ๋ฐ”๊ฟ€ ์ˆ˜๋Š” ์žˆ์„๊นŒ์š”? >>> x[0] = 'n' ์œ„์™€ ๊ฐ™์ด ํ•˜๋ฉดโ€ฆ ๋ ๊นŒ์š”? ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ TypeError๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ฃ . Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment ์ด๋ ‡๋“ฏ, ๋ฌธ์ž์—ด์— ๋“ค์–ด์žˆ๋Š” ๊ธ€์ž๋Š” ๋ฐ”๊ฟ€ ์ˆ˜๊ฐ€ ์—†๋‹ต๋‹ˆ๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ ํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, >>> x = 'n' + x[1:] >>> x 'nanana' ์ด๊ฑด ๋ฌธ์ž์—ด x์˜ 'b'๋ฅผ 'n'์œผ๋กœ ๋ฐ”๊พผ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, 'n'๊ณผ 'anana'๋ฅผ ํ•ฉ์นœ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด 'nanana'์— x๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์— ์–ด๋–ค ๊ธ€์ž๊ฐ€ ๋ช‡ ๋ฒˆ์งธ ์ž๋ฆฌ์— ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ๋Š” find()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. >>> s = "hello Python!" >>> s.find('P') 'P'๊ฐ€ 6๋ฒˆ ์ธ๋ฑ์Šค์— ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜์œผ๋‹ˆ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์Šฌ๋ผ์ด์‹ฑํ•ด์„œ ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ฃ ? >>> s[0:6] 'hello ' >>> h = s[0:6] >>> h 'hello ' ์œ„์˜ h ๋ณ€์ˆ˜์˜ ๋์—๋Š” ๊ณต๋ฐฑ์ด ํฌํ•จ๋˜์—ˆ๋Š”๋ฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์Šฌ๋ผ์ด์‹ฑ์„ ํ•˜๊ฑฐ๋‚˜ rstrip()์œผ๋กœ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> h[0:5] 'hello' >>> h.rstrip() 'hello' ๋˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” split()์„ ์ด์šฉํ•ด ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด๋ฅผ ์•Œ์•„๋‚ด๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> s.split() ['hello', 'Python!'] >>> s.split()[0] 'hello' ๋ฆฌ์ŠคํŠธ ์ด๋ฒˆ์—” ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ํ•ด๋ณผ๊นŒ์š”? >>> prime = [3, 7, 11] # 3, 7, 11์„ ์›์†Œ๋กœ ๊ฐ–๋Š” ๋ฆฌ์ŠคํŠธ prime์„ ๋งŒ๋“ฆ >>> prime.append(5) # prime์— ์›์†Œ 5๋ฅผ ์ถ”๊ฐ€ >>> prime [3, 7, 11, 5] sort ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ •๋ ฌ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ณ ์š”. >>> prime.sort() # prime์„ ์›์†Œ ํฌ๊ธฐ ์ˆœ์œผ๋กœ ์ •๋ ฌ >>> prime [3, 5, 7, 11] ์•„์ฐจ, 2๋ฅผ ๋น ๋œจ๋ ธ๋„ค์š”. ๋งจ ์•ž(0๋ฒˆ)์— 2๋ฅผ ์‚ฝ์ž…(insert) ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. >>> prime.insert(0, 2) >>> prime [2, 3, 5, 7, 11] ์›์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ๋„ ๋˜์ง€์š”. 4๋ฒˆ ์›์†Œ๋ฅผ ์‚ญ์ œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> del prime[4] # prime์˜ 4๋ฒˆ ์›์†Œ๋ฅผ ์‚ญ์ œ >>> prime [2, 3, 5, 7] ์›์†Œ๋ฅผ ์‚ญ์ œํ•  ๋•Œ pop()์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. pop()์€ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ญ์ œํ•œ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜(return) ํ•˜๋ฏ€๋กœ ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„์„œ ๋‚˜์ค‘์— ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ฃ . >>> a = prime.pop() # ์‚ญ์ œํ•œ ์›์†Œ๋ฅผ a ๋ณ€์ˆ˜๋กœ ๋ฐ›์Œ >>> prime [2, 3, 5] >>> a ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> prime[0] = 1 >>> prime [1, 3, 5] ๋ฆฌ์ŠคํŠธ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ง‘์–ด๋„ฃ์„ ์ˆ˜๋„ ์žˆ์ง€์š”. ํ”ผ์ž๊ฐ€๊ฒŒ์—์„œ ์ฃผ๋ฌธํ•  ์Œ์‹ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณผ๊นŒ์š”? >>> orders = ['potato', ['pizza', 'Coke', 'salad'], 'hamburger'] >>> orders[1] ['pizza', 'Coke', 'salad'] >>> orders[1][2] 'salad' ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ„๋‹จํžˆ ํ–‰๋ ฌ์„ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ–‰๋ ฌ์€ ์•„๋งˆ ๊ณ ๋“ฑํ•™๊ต ๋•Œ ๋ฐฐ์šฐ์ฃ ? >>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ๋ฌธ์ž์—ด์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ”๊พธ๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ๋ฌธ์ž์—ด๊ณผ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋”ฐ๋กœ๋”ฐ๋กœ ์•Œ์•„๋ดค๋Š”๋ฐ์š”, ์ด๋ฒˆ์—” ๋‘˜ ๋‹ค ๊ฐ–๊ณ  ๋†€์•„๋ด…์‹œ๋‹ค. ๋ฌธ์ž์—ด์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ”๊ฟ”๋ณด๋„๋ก ํ•˜์ฃ . >>> characters = [] >>> sentence = 'Be happy!' >>> for char in sentence: ... characters.append(char) ... >>> print(characters) ['B', 'e', ' ', 'h', 'a', 'p', 'p', 'y', '!'] ์ฒ˜์Œ์— characters๋ผ๋Š”, ๋น„์–ด์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , sentence๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ Be happy!๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ–ˆ์ง€์š”. ์ „์— for ๋ฌธ์„ ๋ฐฐ์šธ ๋•Œ์—๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ˆ˜ํ–‰ํ•˜์˜€๋Š”๋ฐ, ์ด๋ฒˆ์—๋Š” ๋ฌธ์ž์—ด์„ ์ด์šฉํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„  sentence๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” Be happy!์˜ ๊ธ€์ž ํ•˜๋‚˜ํ•˜๋‚˜์— ๋Œ€ํ•ด์„œ ์–ด๋–ค ์ผ์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜์ฃ . ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž์ธ B๋ฅผ characters๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋กœ ๋„ฃ๊ณ , ๋‘ ๋ฒˆ์งธ ๊ธ€์ž์ธ e๋ฅผ characters์˜ ๋‘ ๋ฒˆ์งธ ์›์†Œ๋กœ ๋„ฃ๋Š” ์‹์ž…๋‹ˆ๋‹ค. ์‚ฌ์‹ค์€ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฌธ์ž์—ด์„ ๋ฐ”๋กœ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•ด๋„ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. >>> list('Be happy!') ['B', 'e', ' ', 'h', 'a', 'p', 'p', 'y', '!'] ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๊ธฐ ์ •์ˆ˜(int) 123์„ ๊ฐ€๋ฆฌํ‚ค๋Š” my_int ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. >>> my_int = 123 >>> type(my_int) <class 'int'> ๋ฌธ์ž์—ด '123'์„ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> str(my_int) '123' ์œ„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์‹œ๋ฉด ์ž‘์€๋”ฐ์˜ดํ‘œ๊ฐ€ ๋ถ™์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํƒ€์ž…์„ ํ™•์ธํ•ด ๋ณด๋ฉด ๋” ์ •ํ™•ํžˆ ์•Œ ์ˆ˜ ์žˆ์ฃ . >>> type(str(my_int)) <class 'str'> ์ด๋ ‡๊ฒŒ ์–ป์€ ๋ฌธ์ž์—ด์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. >>> my_str = str(my_int) ์—„๋ฐ€ํžˆ ๋งํ•˜์ž๋ฉด '์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พผ' ๊ฒƒ์ด๋ผ๊ธฐ๋ณด๋‹ค๋Š”, '์ˆซ์ž ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์–ป์—ˆ๋‹ค'๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋งž๊ฒ ์ฃ . ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ฐ”๊พธ๊ธฐ ์—ญ์œผ๋กœ, ์ˆซ์ž๋ฅผ ๋‚˜ํƒ€๋‚ธ ๋ฌธ์ž์—ด์—์„œ ์ˆซ์ž๋ฅผ ์–ป์–ด๋‚ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> int('123') 123 >>> float('123') 123.0 ๋ฆฌ์ŠคํŠธ ์›์†Œ๋“ค์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ 1๋ถ€ํ„ฐ 10๊นŒ์ง€์˜ ์ •์ˆ˜๋ฅผ ์›์†Œ๋กœ ๊ฐ–๋Š” ๋ฆฌ์ŠคํŠธ one_to_ten์ด ์žˆ์Šต๋‹ˆ๋‹ค. >>> one_to_ten = list(range(1, 11)) >>> one_to_ten [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1๋ถ€ํ„ฐ 10๊นŒ์ง€๋ฅผ ๋”ํ•œ ๊ฐ’์€ ์–ผ๋งˆ์ผ๊นŒ์š”? for ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๊ณ„์‚ฐํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, sum()์„ ์ด์šฉํ•˜๋ฉด ์†์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. >>> sum(one_to_ten) 55 ์„ฑ์ ํ‘œ ์˜ค๋Š˜์˜ ์ข…ํ•ฉ ํŽธ! ์šฐ๋ฆฌ ๋ฐ˜ ์„ฑ์ ํ‘œ๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค! ํ•™์ƒ ์ด๋ฆ„์— ๊ตญ, ์˜, ์ˆ˜ ์„ฑ์ ์„ ๋„ฃ์–ด์ฃผ๊ณ , >>> chulsu = [90, 85, 70] >>> younghee = [88, 79, 92] >>> yong = [100, 100, 100] # ๋ฐ”๋กœ ์ ‘๋‹ˆ๋‹ค.. >>> minsu = [90, 60, 70] ์šฐ๋ฆฌ ๋ฐ˜ ํ•™์ƒ๋“ค์„ ์ „๋ถ€ students ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. >>> students = [chulsu, younghee, yong, minsu] ํ•™์ƒ๋“ค์˜ ์„ฑ์ ์ด ์–ด๋–ค์ง€ ๋ถˆ๋Ÿฌ๋‚ด๋ณผ๊นŒ์š”? >>> for scores in students: ... print(scores) ... [90, 85, 70] [88, 79, 92] [100, 100, 100] [90, 60, 70] ๊ฐœ์ธ์˜ ์„ฑ์ ์„ ๋”ํ•ด์„œ ์ด์ , ํ‰๊ท ๋„ ๋‚ด ๋ณด์„ธ์š”. >>> for scores in students: ... total = 0 ... for s in scores: ... total = total + s ... average = total / 3 ... print(scores, total, average) ... [90, 85, 70] 245 81.66666666666667 [88, 79, 92] 259 86.33333333333333 [100, 100, 100] 300 100.0 [90, 60, 70] 220 73.33333333333333 ์œ„์—์„œ๋Š” ์ด์ ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด total์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ˆซ์ž๋ฅผ ๋ˆ„์ ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์„ ์จ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ, ์˜ค๋Š˜ ๊ฐ•์ขŒ ๋~. 4.2.1 ์Šฌ๋ผ์ด์‹ฑ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์Šฌ๋ผ์ด์‹ฑ(slicing, ์ฐ๊ธฐ) ๊ธฐ๋ฒ•์ด ์•„์ฃผ ๋งŽ์ด ์“ฐ์ด๋‹ˆ๊นŒ ๋‹ค์‹œ ํ•œ๋ฒˆ ์•Œ์•„๋ด…์‹œ๋‹ค. ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฌธ์ž์—ด ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ฅผ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> p = 'Python' >>> p[0:2] 'Py' ์Šฌ๋ผ์ด์‹ฑํ•  ์ฒซ ์ธ๋ฑ์Šค๊ฐ€ 0์ผ ๋•Œ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์ฝœ๋ก (:) ์•ž์˜ 0์„ ์ƒ๋žตํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> p[:2] 'Py' ๋ฌธ์ž์—ด ์ผ๋ถ€๋ฅผ ์Šฌ๋ผ์ด์‹ฑํ•ด์„œ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๊ณผ ๋ถ™์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> h = 'Hello world!' >>> h[:6] + p + '!' 'Hello Python!' ์Œ์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด์˜ ๋’ท๋ถ€๋ถ„์„ ๋ณต์‚ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> h[-1:] '!' >>> h[:6] + p + h[-1:] 'Hello Python!' tip replace() ๋ฉ”์„œ๋“œ๋ฅผ ์จ์„œ ํŠน์ • ๋‹จ์–ด๋ฅผ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ์–ด์š”. >>> h.replace('world', 'Python') 'Hello Python!' ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝค๋งˆ์˜ ์•ž๋’ค ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ์ƒ๋žตํ•˜๋ฉด ๋ฌธ์ž์—ด ์ „๋ถ€๋ฅผ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> p[:] 'Python' ์—ญ์ˆœ์œผ๋กœ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. >>> p[::-1] 'nohtyP' ๋ฆฌ์ŠคํŠธ ์Šฌ๋ผ์ด์‹ฑ ๋ฆฌ์ŠคํŠธ๋กœ๋„ ์Šฌ๋ผ์ด์‹ฑํ•  ์ˆ˜ ์žˆ์–ด์š”. ์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ฒƒ๊ณผ ์›๋ฆฌ๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์„ธ์š”. >>> N = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> N[0] # 0๋ฒˆ(์ฒซ ๋ฒˆ์งธ) ์›์†Œ >>> N[0:5] # 0๋ฒˆ๋ถ€ํ„ฐ 5๋ฒˆ ์•ž(4๋ฒˆ)๊นŒ์ง€ [1, 2, 3, 4, 5] >>> N[:5] # ์ฒ˜์Œ๋ถ€ํ„ฐ 5๋ฒˆ ์•ž๊นŒ์ง€ [1, 2, 3, 4, 5] >>> N[5:] # 5๋ฒˆ๋ถ€ํ„ฐ ๋๊นŒ์ง€ [6, 7, 8, 9, 10] >>> N[-3:] # ๋’ค์—์„œ 3๋ฒˆ์งธ๋ถ€ํ„ฐ ๋๊นŒ์ง€ [8, 9, 10] >>> N[::-1] # ์—ญ์ˆœ์œผ๋กœ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] ๋ฌธ์ž์—ด์˜ ๋ฆฌ์ŠคํŠธ๋„ ๋งŒ๋“ค์–ด์„œ ์‹คํ—˜ํ•ด ๋ณด์„ธ์š”. >>> fruit = ['apple', 'banana', 'cherry', 'mango', 'orange'] >>> fruit[0:2] ['apple', 'banana'] >>> fruit[2] 'cherry' >>> fruit[2:] ['cherry', 'mango', 'orange'] 4.2.2 ์—ฐ์Šต ๋ฌธ์ œ: ํšŒ๋ฌธ ํŒ๋ณ„ ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ ๋ฌธ์ œ ๊ฑฐ๊พธ๋กœ ๋ฐฐ์—ดํ•ด๋„ ๊ฐ™์€ ๋‹จ์–ด ํ˜น์€ ๋ฌธ์žฅ์ด ๋˜๋Š” ๊ฒƒ์„ ํšŒ๋ฌธ(palindrome)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ํšŒ๋ฌธ์˜ ์˜ˆ์ž…๋‹ˆ๋‹ค. Anna Civic Kayak Level ... ๋ฌธ์ œ 1 ์ฃผ์–ด์ง„ ๋‹จ์–ด๊ฐ€ ํšŒ๋ฌธ์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜ palindrome()์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, ๋ฌธ์ž์—ด ์ž…๋ ฅ์€ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ์ด๋ค„์ง€๋ฉฐ ๊ณต๋ฐฑ์„ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. >>> palindrome('anna') True >>> palindrome('banana') False ๋ฌธ์ œ 2 ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ์„ž์—ฌ ์žˆ๋”๋ผ๋„ ํšŒ๋ฌธ์œผ๋กœ ํŒ์ •ํ•˜๋„๋ก ํ•จ์ˆ˜๋ฅผ ๊ฐœ์„ ํ•˜์„ธ์š”. >>> palindrome('Anna') True ๋ฌธ์ œ 3 ๊ณต๋ฐฑ์ด ์„ž์—ฌ ์žˆ๋”๋ผ๋„ ํšŒ๋ฌธ์œผ๋กœ ํŒ์ •ํ•˜๋„๋ก ํ•จ์ˆ˜๋ฅผ ๊ฐœ์„ ํ•˜์„ธ์š”. >>> palindrome('My gym') True tip ๋ฌธ์ž์—ด์˜ lower() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์†Œ๋ฌธ์ž๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> 'Python'.lower() 'python' ๊ทธ๋ฆฌ๊ณ  replace() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž์—ด ์ผ๋ถ€๋ฅผ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> 'Python'.replace('P', 'J') 'Jython' ใ€Šํ๋ฆ„๋Œ€๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š” Flowgorithm ใ€‹์—์„œ ํšŒ๋ฌธ ํŒ๋ณ„ ํ•จ์ˆ˜์˜ ํ๋ฆ„๋„๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ํŒŒ์ด์ฌ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ํ๋ฆ„๋„์™€ ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ๋น„๊ตํ•ด ๋ณด๊ณ , ์ฝ”๋“œ๋ฅผ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ch04/palindrome.py 4.2.3 ์—ฐ์Šต ๋ฌธ์ œ: ๊ฐ ์ž๋ฆฌ ์ˆซ์ž์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜(๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉ) ๋ฌธ์ œ ์ •์ˆ˜ num์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ๊ฐ ์ž๋ฆฌ ์ˆซ์ž(digit)์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋Š” sumOfDigits() ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, ๋‚˜๋ˆ—์…ˆ์„ ์ด์šฉํ•˜์ง€ ๋ง๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์–ด๋ณด์„ธ์š”. ์˜ˆ 1 ์ž…๋ ฅ: 643 ์ถœ๋ ฅ(6 + 4 + 3 = 13): 13 ์˜ˆ 2 ์ž…๋ ฅ: 47253 ์ถœ๋ ฅ: 21 ํžŒํŠธ ์ •์ˆ˜์˜ ๋‚˜๋ˆ—์…ˆ๊ณผ ๋‚˜๋จธ์ง€ ๊ตฌํ•˜๋Š” ๋ฒ•: 1.2 ์ˆซ์ž ๊ณ„์‚ฐ์„ ๋ณด์„ธ์š”. ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•: 1.5. ๋ช…๋ น ํ•ด์„๊ธฐ๋ฅผ ๋ณด์„ธ์š”. ์ฝ”๋“œ: ch04/sumOfDigits_non-recursive_list.py 4.2.4 ์—ฐ์Šต ๋ฌธ์ œ: ์ค„๊ธฐ์™€ ์žŽ ๊ทธ๋ฆผ ์ค‘ํ•™๊ต 1ํ•™๋…„ ์ˆ˜ํ•™ ์‹œ๊ฐ„์— ๋ฐฐ์šฐ๋Š” ์ค„๊ธฐ์™€ ์žŽ ๊ทธ๋ฆผ์„ ํŒŒ์ด์ฌ์œผ๋กœ ๋งŒ๋“ค์–ด๋ณผ๊ฒŒ์š”. ๋ฐฐ์šด ์ง€ ์˜ค๋ž˜๋ผ์„œ ๊ธฐ์–ต์ด ๋‚˜์ง€ ์•Š๊ฑฐ๋‚˜ ์•„์ง ๋ฐฐ์šฐ์ง€ ์•Š์•˜๋”๋ผ๋„ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค. ์นธ ์•„์นด๋ฐ๋ฏธ์˜ ์ค„๊ธฐ์™€ ์žŽ ๊ทธ๋ฆผ ์ˆ˜์—…์„ ๋“ค์–ด๋ณด๋ฉด ๊ธˆ๋ฐฉ ์ดํ•ดํ•˜์‹ค ๊ฑฐ์˜ˆ์š”. ๋ฌธ์ œ ๋†๊ตฌํŒ€์— ์†ํ•œ 12๋ช…์˜ ๋“์ ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด score ๋ฆฌ์ŠคํŠธ๋กœ ๋‚˜ํƒ€๋ƒˆ์Šต๋‹ˆ๋‹ค. >>> score = [0, 0, 2, 4, 7, 7, 9] >>> score += [11, 11, 13, 18] >>> score += [20] >>> score [0, 0, 2, 4, 7, 7, 9, 11, 11, 13, 18, 20] ๋‹ค์Œ๊ณผ ๊ฐ™์ด stem_leaf ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์„ ์ˆ˜๋“ค์˜ ๋“์ ์„ ์ค„๊ธฐ์™€ ์žŽ ๊ทธ๋ฆผ<NAME>์œผ๋กœ ์ €์žฅํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. >>> stem_leaf = [[], [], []] ๋ฌธ์ œ 1 stem_leaf์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฑ„์šฐ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋ฐ์ดํ„ฐ๊ฐ€ ์ฑ„์›Œ์ง„ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. >>> stem_leaf [[0, 0, 2, 4, 7, 7, 9], [1, 1, 3, 8], [0]] >>> stem_leaf[0] [0, 0, 2, 4, 7, 7, 9] >>> stem_leaf[1] [1, 1, 3, 8] >>> stem_leaf[2] [0] ๋ฌธ์ œ 2 stem_leaf๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ํ”„๋ฆฐํŠธํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. 0: [0, 0, 2, 4, 7, 7, 9] 1: [1, 1, 3, 8] 2: [0] ์ฝ”๋“œ: ch04/stem_leaf.py 4.2.5 ์—ฐ์Šต ๋ฌธ์ œ: ๊ฐ ์ž๋ฆฌ ์ˆซ์ž์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜(map()์„ ์ด์šฉ) ๋ฌธ์ œ ์ •์ˆ˜ num์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ๊ฐ ์ž๋ฆฌ ์ˆซ์ž(digit)์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋Š” sumOfDigits() ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, ๋‚˜๋ˆ—์…ˆ์„ ์ด์šฉํ•˜์ง€ ๋ง๊ณ , ๋ฆฌ์ŠคํŠธ์™€ map() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ํ’€์–ด๋ณด์„ธ์š”. ์˜ˆ 1 ์ž…๋ ฅ: 47253 ์ถœ๋ ฅ: 21 ์˜ˆ 2 ์ž…๋ ฅ: 643 ์ถœ๋ ฅ: 13 ์ฝ”๋“œ: ch04/sumOfDigits_non-recursive_map.py 4.2.6 ์—ฐ์Šต ๋ฌธ์ œ: ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ ๋ฌธ์ œ ์†Œ์ˆ˜(็ด ๆ•ธ, prime number)๋Š” 1 ๊ณผ ๊ทธ ์ž์ฒด๋งŒ์„ ์ธ์ˆ˜(factor)๋กœ ๊ฐ–๋Š” ์ˆ˜์ž…๋‹ˆ๋‹ค 1. ๋˜๋Š” โ€œ1๋ณด๋‹ค ํฐ ์ž์—ฐ์ˆ˜ ์ค‘ 1๊ณผ ์ž๊ธฐ ์ž์‹ ๋งŒ์„ ์•ฝ์ˆ˜๋กœ ๊ฐ€์ง€๋Š” ์ˆ˜โ€๋ผ๊ณ  ์„ค๋ช…ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค 2. ๋‹ค์Œ์€ ์†Œ์ˆ˜์ž…๋‹ˆ๋‹ค. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ... ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฒ”์œ„์˜ ์ž์—ฐ์ˆ˜๋ฅผ ๋‚˜์—ดํ•œ๋‹ค. 2๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ, 2์˜ ๋ฐฐ์ˆ˜๋ฅผ ์ง€์›Œ๋‚˜๊ฐ„๋‹ค. ๋‹ค์Œ ์†Œ์ˆ˜์˜ ๋ฐฐ์ˆ˜๋ฅผ ๋ชจ๋‘<NAME>๋‹ค. ๋‹ค์Œ์€ ํŒŒ์ด์ฌ ์…ธ์—์„œ ์ˆ˜์ž‘์—…์œผ๋กœ 10 ์ดํ•˜์˜ ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์˜ˆ์ž…๋‹ˆ๋‹ค. >>> L = list(range(2, 11)) # ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฒ”์œ„์˜ ์ž์—ฐ์ˆ˜๋ฅผ ๋‚˜์—ด >>> L [2, 3, 4, 5, 6, 7, 8, 9, 10] >>> L.remove(4); L.remove(6); L.remove(8); L.remove(10) # 2์˜ ๋ฐฐ์ˆ˜๋ฅผ ์ง€์›€ >>> L.remove(9) # 3์˜ ๋ฐฐ์ˆ˜๋ฅผ ์ง€์›€ >>> L # ๊ฒฐ๊ณผ [2, 3, 5, 7] ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฒ”์œ„(์˜ˆ: 30 ์ดํ•˜)์˜ ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ˆ˜์ž‘์—…์œผ๋กœ ํ•˜์ง€ ๋ง๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ์˜ˆ 1 ์ž…๋ ฅ: 10 ์ถœ๋ ฅ: [2, 3, 5, 7] ์˜ˆ 2 ์ž…๋ ฅ: 20 ์ถœ๋ ฅ: [2, 3, 5, 7, 11, 13, 17, 19] ์˜ˆ 3 ์ž…๋ ฅ: 30 ์ถœ๋ ฅ: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] ์ฒซ ๋ฒˆ์งธ ํ’€์ด ์ฒ˜์Œ์—๋Š” ๋ฆฌ์ŠคํŠธ์˜ remove() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€๊ณ , ๊ทธ ๊ณผ์ •์„ ์œ ํŠœ๋ธŒ<NAME>์ƒ์œผ๋กœ ์˜ฌ๋ ค๋‘์—ˆ์Šต๋‹ˆ๋‹ค. https://youtu.be/KChXYFu2rYo ๊ทธ๋Ÿฐ๋ฐ ์ด ๋ฐฉ๋ฒ•์ด ์ดํ•ดํ•˜๊ธฐ๋Š” ์‰ฝ์ง€๋งŒ ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜์ค‘์— ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  for ๋ฃจํ”„๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋„์ค‘์— ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐ”๋žŒ์— ๋ชจ๋“  ์›์†Œ๋ฅผ ์ œ๋Œ€๋กœ ๊ฒ€์‚ฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜<NAME>์ƒ์€ ๊ทธ ๋ฌธ์ œ๋ฅผ ์•Œ์ง€ ๋ชปํ•˜๊ณ  ์ฐ์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์‚ฌ์šฉํ•˜๋„๋ก ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. L2 = L[:] ์•„๋ž˜ ์ฃผ์†Œ์˜ ์ฝ”๋“œ๋Š” ์œ„์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ๋ฐ˜์˜๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ch04/prime.py ๊ทธ๋Ÿฐ๋ฐ ์ด ์ฝ”๋“œ์—๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 2์˜ ๋ฐฐ์ˆ˜๋ฅผ ๋ชจ๋‘ ๊ฒ€์‚ฌํ•˜๊ณ  ๋‚˜์„œ 3์˜ ๋ฐฐ์ˆ˜๋ฅผ ๊ฒ€์‚ฌํ•  ๋•Œ๋Š” ์ˆซ์ž 2๋Š” ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์—†๋Š”๋ฐ, ๊ทธ ์ ์„ ๋ฏธ์ฒ˜ ์ƒ๊ฐ์ง€ ๋ชปํ•˜๊ณ  ๋ชจ๋‘ ๊ฒ€์‚ฌํ•˜๊ฒŒ ๋˜์–ด ์žˆ์–ด ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ํ’€์ด ์ฒซ ๋ฒˆ์งธ ํ’€์ด์˜ ๋ฌธ์ œ์ ๋“ค์„ ํ•ด๊ฒฐํ•œ ์ฝ”๋“œ๋ฅผ ์ƒˆ๋กœ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. for ๋ฃจํ”„ ๋Œ€์‹  while ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ–ˆ๊ณ , while ๋ฃจํ”„์˜ ์กฐ๊ฑด์‹์„ ์ด์šฉํ•ด ๋ถˆํ•„์š”ํ•œ ๊ฒ€์‚ฌ๋ฅผ ํ•˜์ง€ ์•Š๊ฒŒ ํ•ด ํšจ์œจ์„ ๋†’์˜€์Šต๋‹ˆ๋‹ค. ch04/prime2.py ์ „๋ณด๋‹ค ๋‚˜์•„์ง€๊ธด ํ–ˆ์ง€๋งŒ, ๋‹ค๋ฅธ ์ฑ…์— ์‹ค๋ฆฐ ์ฝ”๋“œ์™€ ๋น„๊ตํ•ด ๋ณด๋‹ˆ ๋ณต์žกํ•˜๋ฉด์„œ ๋น„ํšจ์œจ์ ์ธ ๋ถ€๋ถ„์ด ์—ฌ์ „ํžˆ ๋‚จ์•„ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋…์ž๋ถ„๋“ค์˜ ํ’€์ด ๋…์ž๋ถ„๋“ค๊ป˜์„œ ์ œ์•ˆํ•ด ์ฃผ์‹  ํ’€์ด์ž…๋‹ˆ๋‹ค. ๊ณ ๋ง™์Šต๋‹ˆ๋‹ค! ๊น€ ๋‹˜์˜ ํ’€์ด(if ์ ˆ์„ ์ถ”๊ฐ€): ch04/prime_kim.py fateindestiny.dev ๋‹˜์˜ ํ’€์ด(filter๋ฅผ ํ™œ์šฉ): ch04/prime_fate.py aaaa ๋‹˜์˜ ํ’€์ด: ch04/prime_aaaa.py ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์‹œ๊ฐ„ ๋น„๊ต ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ์˜ ์„ฑ๋Šฅ์„ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์‹œ๊ฐ„ ์ธก์ •ํ•˜๊ธฐ์—์„œ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. ๋” ์ฝ์„๊ฑฐ๋ฆฌ ์œ„ํ‚ค๋…์Šค์— ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ๋ฅผ ๋‹ค๋ฃฌ ์ฑ…๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ’€์ด ๋ฐฉ๋ฒ•์„ ๋น„๊ตํ•ด ๋ณด์„ธ์š”. ๊น€์ค‘์šด, ํŒŒ์ด์ฌ ๊ณ„๋‹จ ๋ฐŸ๊ธฐ ์ด์—ฐํ™, ์ฝ”๋”ฉ์œผ๋กœ ์ˆ˜ํ•™ํ•˜๊ธฐ Kyoungwon, ์ค‘ํ•™ ์ˆ˜ํ•™ ์ฝ”๋”ฉ์˜ ์ •์„ ์นธ ์•„์นด๋ฐ๋ฏธ, Prime Numbers โ†ฉ https://ko.wikipedia.org/wiki/์†Œ์ˆ˜_(์ˆ˜๋ก ) โ†ฉ 4.2.7 ์—ฐ์Šต ๋ฌธ์ œ: ์ง„๋ฒ• ๋ณ€ํ™˜ ๋‚˜๋ˆ—์…ˆ์„ ์ด์šฉํ•ด ์‹ญ์ง„์ˆ˜๋ฅผ ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹ญ์ง„์ˆ˜๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋ชซ์„ ๊ตฌํ•˜๊ณ  ๊ทธ ์ˆ˜๋ฅผ ๋‹ค์‹œ 2๋กœ ๋‚˜๋ˆ„๋Š” ์ผ์„ ๋ชซ์ด 0์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ณ , ๊ฐ ๋‹จ๊ณ„์—์„œ ๊ตฌํ•œ ๋‚˜๋จธ์ง€๋ฅผ ๊ฑฐ๊พธ๋กœ ์“ฐ๋ฉด ์ด์ง„์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‹ญ์ง„์ˆ˜ 13์— ํ•ด๋‹นํ•˜๋Š” ์ด์ง„์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ํ•œ ๋ฒˆ์— ๊ตฌํ•˜๊ธฐ ์ฐธ์กฐ). >>> divmod(13, 2) # 13์„ 2๋กœ ๋‚˜๋ˆˆ ๋ชซ์€ 6, ๋‚˜๋จธ์ง€๋Š” 1 (6, 1) >>> divmod(6, 2) # 6์„ 2๋กœ ๋‚˜๋ˆˆ ๋ชซ์€ 3, ๋‚˜๋จธ์ง€๋Š” 0 (3, 0) >>> divmod(3, 2) # 3์„ 2๋กœ ๋‚˜๋ˆˆ ๋ชซ์€ 1, ๋‚˜๋จธ์ง€๋„ 1 (1, 1) >>> divmod(1, 2) # 1์„ 2๋กœ ๋‚˜๋ˆˆ ๋ชซ์€ 0, ๋‚˜๋จธ์ง€๋Š” 1 (0, 1) ์œ„์˜ ๊ฐ ๋‹จ๊ณ„์—์„œ ๊ตฌํ•œ ๋‚˜๋จธ์ง€ 1, 0, 1, 1์„ ์—ญ์ˆœ์œผ๋กœ ์“ด 1101์ด ์‹ญ์ง„์ˆ˜ 13์— ํ•ด๋‹นํ•˜๋Š” ์ด์ง„์ˆ˜์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ bin() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๋ฒˆ๊ฑฐ๋กœ์šด ๊ณ„์‚ฐ์„ ์ง์ ‘ ํ•˜์ง€ ์•Š๊ณ ๋„ ์ด์ง„์ˆ˜๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์ฃ . >>> bin(13) '0b1101' 0b๋Š” ๋’ค์˜ ์ˆซ์ž๊ฐ€ ์ด์ง„์ˆ˜์ž„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ฌธ์ œ ์‹ญ์ง„์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ทธ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ์ด์ง„์ˆ˜์˜ ๊ฐ ์ž๋ฆฌ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (์ˆœ์„œ์— ์œ ์˜) ์˜ˆ 1 ์ž…๋ ฅ: 13 ์ถœ๋ ฅ: [1, 1, 0, 1] ์˜ˆ 2 ์ž…๋ ฅ: 87 ์ถœ๋ ฅ: [1, 0, 1, 0, 1, 1, 1] ์ฝ”๋“œ: ch04/dec2bin.py 4.3 ํŠœํ”Œ(tuple) ์•ˆ๋…•ํ•˜์„ธ์š”! ์˜ค๋Š˜ ๋‚ ์”จ๊ฐ€ ์•„์ฃผ ์ข‹๋„ค์š”. ์ถœ๊ทผํ•˜์ง€ ๋ง๊ณ  ์–ด๋”” ๋†€๋Ÿฌ ๊ฐ€๊ณ  ์‹ถ๋”๋ผ๊ณ ์š”. ๊ฐ•์ขŒ๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆฌ๊ณ  ๊ฐ€๋” ๋จธ๋ฆฌ ์•„ํ”„๊ธด ํ•ด๋„ ๋„ˆ๋ฌด ์žฌ๋ฏธ์žˆ๋„ค์š”. ๊ทธ๋ž˜์„œ ์š”์ฆ˜์€ ํ‹ˆ๋งŒ ๋‚˜๋ฉด ์—ฌ๊ธฐ์— ๋งค๋‹ฌ๋ ค์žˆ๋‹ต๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ํŠœํ”Œ์ด๋ผ๋Š” ์ž๋ฃŒํ˜•์ด ์–ด๋–ค ์“ธ๋ชจ๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ฃ . ์˜์–ด์˜ tuple์„ 'ํŠœํ”Œ' ํ˜น์€ 'ํ„ฐํ”Œ'์ด๋ผ๊ณ  ์ฝ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์™€ ๋น„์Šทํ•œ ์ž๋ฃŒํ˜•์ด๋ผ๋Š” ์ •๋„๋งŒ ์•Œ๊ณ  ์‹œ์ž‘ํ•ด ๋ด…์‹œ๋‹ค. ๋‹ค๋ฅธ ์–ธ์–ด๋ฅผ ๊ณต๋ถ€ํ•ด ๋ณด์‹  ๋ถ„์€ ๋‘ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์„œ๋กœ ๋ฐ”๊พธ์–ด ๋ณธ ์ ์ด ์žˆ์œผ์‹ค ํ…๋ฐ์š”, ๋ณดํ†ต ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. >>> a = 10 >>> b = 20 >>> temp = a # a ๊ฐ’์„ temp์— ์ €์žฅ (temp = 10) >>> a = b # b ๊ฐ’์„ a์— ์ €์žฅ (a = 20) >>> b = temp # temp ๊ฐ’์„ b์— ์ €์žฅ (b = 10) >>> print(a, b) 20 10 ์ด๋ ‡๊ฒŒ ๋‘ ๋ณ€์ˆซ๊ฐ’์„ ๋งž๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด์„  ๋˜ ๋‹ค๋ฅธ ๋ณ€์ˆ˜ temp๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ข€ ๋ฒˆ๊ฑฐ๋กญ์ฃ ? ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์„์ˆ˜๋ก ๋” ๊ท€์ฐฎ์•„์งˆ ํ…Œ๊ณ ์š”. ๊ทธ๋Ÿฐ๋ฐ ํŒŒ์ด์ฌ์—์„  ์ด๋Ÿฐ ์ผ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ง‰ํžŒ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. >>> c = 10 >>> d = 20 >>> c, d = d, c >>> print(c, d) 20 10 ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•˜์ง€์š”? ์ €๋Š” ์ด๊ฒƒ์„ ๋ณด๊ณ  ์›ƒ์–ด๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค. ํ—ฌ ํ—ฌ ํ—ฌ... ์„ธ ๋ฒˆ์งธ ์ค„์—์„œ ๋“ฑํ˜ธ ์™ผ์ชฝ์€ c, d๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋‹ด๊ธด ํŠœํ”Œ์ด๊ตฌ์š”, ์˜ค๋ฅธ์ชฝ์€ d์™€ c์˜ ๊ฐ’์ด ๋‹ด๊ธด ํŠœํ”Œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ d์˜ ๊ฐ’์€ c๋กœ ๋“ค์–ด๊ฐ€๊ณ , c์˜ ๊ฐ’์€ d๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ผ๋“ค์ด ์ฐจ๋ก€์ฐจ๋ก€ ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ , ๋™์‹œ์— ์ฒ˜๋ฆฌ๋œ๋‹ค๋Š”๊ตฐ์š”. ์ด๋ฒˆ์—” ํ•จ์ˆ˜์—์„œ ํŠœํ”Œ์ด ์š”๊ธดํ•˜๊ฒŒ ์“ฐ์ด๋Š” ๊ฒƒ์„ ๋ณด์—ฌ๋“œ๋ฆฌ์ง€์š”. ์•„๋ž˜์˜ ํ•จ์ˆ˜๋Š” ์ธ์ž(๋งค๊ฐœ๋ณ€์ˆ˜)๋ฅผ ์ฃผ๋Š” ๋Œ€๋กœ ๋ฐ›์•„๋จน๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. >>> def magu_print(x, y, *rest): # ๋งˆ๊ตฌ ์ฐ์–ด ํ•จ์ˆ˜ ... print(x, y, rest) ... >>> magu_print(1, 2, 3, 5, 6, 7, 9, 10) 1 2 (3, 5, 6, 7, 9, 10) ์œ„ ํ•จ์ˆ˜๋Š” ์ธ์ž๋ฅผ ๋‘ ๊ฐœ ์ด์ƒ๋งŒ ์ฃผ๋ฉด ๋‚˜๋จธ์ง„ ๋‹ค ์•Œ์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ์ธ์ž์— ๋ณ„ํ‘œ๋ฅผ ๋ถ™์—ฌ๋‘๋ฉด ๊ทธ ์ดํ›„์— ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์€ ๋ชจ๋‘ ํŠœํ”Œ์— ์ง‘์–ด๋„ฃ๋Š” ๊ฒƒ์ด์ฃ . ์œ„์—์„  (3, 5, 6, 7, 9, 10)๊ฐ€ ํ•˜๋‚˜์˜ ํŠœํ”Œ๋กœ ๋ฌถ์˜€์Šต๋‹ˆ๋‹ค. ๊ฝค ์“ธ๋งŒํ•  ๊ฒƒ ๊ฐ™์ฃ ? ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ์ด๋Ÿฐ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด ๊ณ ์ƒ ๊ฝค๋‚˜ ํ•ด์•ผ ํ•  ๊ฑฐ์˜ˆ์š”. ์ธ์ž๋ฅผ ๋‘ ๊ฐœ, ์„ธ ๊ฐœ ๋„ฃ์–ด์„œ๋„ ์‹คํ—˜ํ•ด ๋ณด์„ธ์š”. ํŠœํ”Œ์˜ ์ข‹์€ ์ ๋“ค์„ ๊ตฌ๊ฒฝํ–ˆ์œผ๋‹ˆ ์ด์ œ ๋ฌธ๋ฒ•์„ ์‚ดํŽด๋ด…์‹œ๋‹ค. >>> t = ('a', 'b', 'c') ํŠœํ”Œ์„ ๋งŒ๋“ค ๋•Œ๋Š” ์œ„์™€ ๊ฐ™์ด ๊ด„ํ˜ธ๋ฅผ ์จ๋„ ๋˜๊ณ  ์•ˆ ์จ๋„ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์›์†Œ๊ฐ€ ์—†๋Š” ํŠœํ”Œ์„ ๋งŒ๋“ค ๋•Œ๋Š” ๊ด„ํ˜ธ๋ฅผ ๊ผญ ์จ์ฃผ์„ธ์š”. >>> empty = () ์›์†Œ๋ฅผ ํ•˜๋‚˜๋งŒ ๊ฐ€์ง„ ํŠœํ”Œ์„ ๋งŒ๋“ค ๋• ์›์†Œ ๋’ค์— ์ฝค๋งˆ(,)๋ฅผ ๊ผญ ์ฐ์–ด์ฃผ์‹œ๊ณ ์š”. >>> one = 5, >>> one (5, ) ๊ทธ๋ฆฌ๊ณ  ํŠœํ”Œ์€ ๋ฆฌ์ŠคํŠธ์™€ ๋‹ฌ๋ฆฌ ์›์†Œ ๊ฐ’์„ ์ง์ ‘ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ๋ฌธ์ž์—ด์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์˜ค๋ ค ๋ถ™์ด๋Š” ๋ฐฉ๋ฒ•์„ ์จ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋‘์„ธ์š”. >>> p = (1,2,3) >>> q = p[:1] + (5, ) + p[2:] >>> q (1, 5, 3) >>> r = p[:1], 5, p[2:] >>> r ((1, ), 5, (3, )) ํŠœํ”Œ์„ ๋ฆฌ์ŠคํŠธ๋กœ, ๋ฆฌ์ŠคํŠธ๋ฅผ ํŠœํ”Œ๋กœ ์‰ฝ๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค. >>> p = (1, 2, 3) >>> q = list(p) # ํŠœํ”Œ p๋กœ ๋ฆฌ์ŠคํŠธ q๋ฅผ ๋งŒ๋“ฆ >>> q [1, 2, 3] >>> r = tuple(q) # ๋ฆฌ์ŠคํŠธ q๋กœ ํŠœํ”Œ r์„ ๋งŒ๋“ฆ >>> r (1, 2, 3) ๊ทธ๋Ÿผ, ์—ฌ๋Ÿฌ๋ถ„ ์•ˆ๋…•~. 4.3.1 ์—ฐ์Šต ๋ฌธ์ œ: ๋‚ด์ผ์˜ ๋‚ ์งœ ๊ตฌํ•˜๊ธฐ(1) ๋ฌธ์ œ 1 ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋‚ ์งœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋Š” ์—ฐ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋„ค ์ž๋ฆฌ ์ˆซ์ž์ด๊ณ , ๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋Š” ์›”์„, ์„ธ ๋ฒˆ์งธ ์ˆซ์ž๋Š” ์ผ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ž…๋ ฅ๋ฐ›์€ ๋‚ ์งœ๋ฅผ mm/dd/yyyy<NAME>์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์›”์„ ๋‘ ์ž๋ฆฌ ์ˆซ์ž(01, 02, 03, ..., 12)๋กœ, ์ผ์„ ๋‘ ์ž๋ฆฌ ์ˆซ์ž(01, 02, 03, ..., 31)๋กœ, ์—ฐ๋„๋ฅผ ๋„ค ์ž๋ฆฌ ์ˆซ์ž๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ž…๋ ฅ๋ฐ›์€ ๋‚ ์งœ์˜ ๋‹ค์Œ ๋‚ ์— ํ•ด๋‹นํ•˜๋Š” ๋‚ ์งœ๋„ ๊ฐ™์€<NAME>์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์œค๋…„์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค(2์›”์€ ํ•ญ์ƒ 28์ผ๊นŒ์ง€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค). ์˜ˆ 1 ์ž…๋ ฅ: 2018 10 2 ์ถœ๋ ฅ: 10/02/2018 10/03/2018 ์˜ˆ 2 ์ž…๋ ฅ: 2018 10 31 ์ถœ๋ ฅ: 10/31/2018 11/01/2018 ์˜ˆ 3 ์ž…๋ ฅ: 2018 11 30 ์ถœ๋ ฅ: 11/30/2018 12/01/2018 ์˜ˆ 4 ์ž…๋ ฅ: 2018 12 31 ์ถœ๋ ฅ: 12/31/2018 01/01/2019 ์ฝ”๋“œ: ch04/tomorrow.py edx.org์˜ C Programming: Advanced Data Types ๊ฐ•์ขŒ์— ๋‚˜์˜จ ์—ฐ์Šต ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. โ†ฉ 4.4 ๋”•์…”๋„ˆ๋ฆฌ(dict) ์˜ค๋Š˜ ์ œ๊ฐ€ ์—ฌ๋Ÿฌ๋ถ„๊ณผ ํ•จ๊ป˜ ๊ณต๋ถ€ํ•  ๊ฒƒ์€ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์ด์—์š”. ์‚ฌ์ „์„ ํ•œ ๋ฒˆ๋„ ๋ชป ๋ณด์‹  ๋ถ„์€ ์•ˆ ๊ณ„์‹œ์ฃ ? dictionary n. pl. dictionaries A reference book containing an alphabetical list of words, โ€ฆ python n. Any of various nonvenomous snakes of the family Pythonidae, โ€ฆ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์œผ๋กœ ๊ผญ ๊ตญ์–ด์‚ฌ์ „์ด๋‚˜ ๋ฐฑ๊ณผ์‚ฌ์ „ ๊ฐ™์€ ๊ฒƒ์„ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ๊ฑด ์•„๋‹ˆ์ง€๋งŒ, ๊ธฐ์–ตํ•˜๊ธฐ ์‰ฝ๋„๋ก ์˜์–ด ์‚ฌ์ „์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ž๋ฃŒ๋“ค์„ ์šฐ๋ฆฌ๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•ด์„œ ์ €์žฅํ•˜๋ ค๋ฉด ์–ด๋–ค ๊ฒƒ์ด ์ข‹์„๊นŒ์š”? ์ €์žฅํ•ด๋’€๋‹ค๊ฐ€ dictionary๋ผ๊ณ  ์น˜๋ฉด 'A reference book ์ฃผ์ ˆ์ฃผ์ ˆโ€ฆ'ํ•˜๊ณ  ๋‚˜์˜ค๊ณ , python์ด๋ผ๊ณ  ํ•˜๋ฉด ๋˜ '๊ตฌ์‹œ๋ ๊ตฌ์‹œ๋ โ€ฆ' ํ•˜๋„๋ก ๋ง์ด์ฃ . ๋ฆฌ์ŠคํŠธ? ํŠœํ”Œ? ๋ฌผ๋ก  ๊ทธ๋Ÿฐ ๊ฒƒ๋“ค์„ ์‚ฌ์šฉํ•ด๋„ ๋งŒ๋“ค ์ˆ˜๋Š” ์žˆ๊ฒ ์ง€๋งŒ ๊ฒฐ๊ตญ ์šฐ๋ฆฌ๊ฐ€ ๋ฐฐ์šธ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•๊ณผ ๋น„์Šทํ•œ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋  ๋“ฏ์‹ถ๋„ค์š”. ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> dic = {} # dic์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋น„์–ด์žˆ๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค. >>> dic['dictionary'] = '1. A reference book containing an ...' >>> dic['python'] = 'Any of various nonvenomous snakes of the ...' >>> dic['dictionary'] # dic์•„, 'dictionary'๊ฐ€ ๋ญ๋‹ˆ? '1. A reference book containing an ...' ์ฒ˜์Œ์— dic์ด๋ผ๋Š” ์‚ฌ์ „์„ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ , ๋‘˜์งธ, ์…‹์งธ ์ค„์—์„œ๋Š” dic์—๋‹ค๊ฐ€ ์ž๋ฃŒ๋ฅผ ์ข€ ์ง‘์–ด๋„ฃ์—ˆ์ง€์š”. ๊ทธ๋ฆฌ๊ณ , ๋งˆ์ง€๋ง‰ ์ค„์—์„  dictionary์˜ ๋œป์ด ๋ญ”์ง€ ์กฐํšŒ๋ฅผ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ์˜์–ด๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์„œ ๊ฒ์ด ๋‚˜์‹ญ๋‹ˆ๊นŒ? --; ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์˜์–ด ๊ฒ๋‚ด์„œ ์“ฐ๋‚˜์š”. ์•ž์œผ๋กœ ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด ํ• ์ˆ˜๋ก ์˜์–ด์˜ ์ค‘์š”์„ฑ์„ ๋Š๋ผ์‹œ๊ฒŒ ๋  ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ์˜์–ด์— ์•ฝํ•˜์‹  ๋ถ„์„ ์œ„ํ•ด ํฌ์ผ“์šฉ ์‚ฌ์ „์„ ๋งŒ๋“ค์–ด ๋ณผ๊นŒ์š”? >>> smalldic = {'dictionary': 'reference', 'python': 'snake'} >>> smalldic['python'] # ํฌ์ผ“์šฉ ์‚ฌ์ „์•„, 'python'์ด ๋ญ๋‹ˆ?? 'snake' >>> smalldic {'dictionary': 'reference', 'python': 'snake'} ์ข€ ๋” ๊น”๋”ํ•ด์กŒ์ฃ ? ์œ ์‹ฌํžˆ ๋ณด์‹œ๋ฉด ์•„๊นŒ์™€๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๋Š” ๊ฒƒ๋„ ์•„์‹ค ์ˆ˜ ์žˆ๊ฒ ์ง€์š”? ์ด์™€ ๊ฐ™์ด ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์€ ํ‚ค(key)์™€ ๊ฐ’(value)์˜ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. ํ‚ค๊ฐ’ dictionary ์ฃผ์ ˆ์ฃผ์ ˆ... python ๊ตฌ์‹œ๋ ๊ตฌ์‹œ๋ ... zoo ๋™๋ฌผ์› ๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ์€ ์ˆซ์ž๋กœ ๋œ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ์กฐํšŒํ•˜๋Š”๋ฐ, ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ‚ค๋ฅผ ์ด์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์ด ํฐ ์ฐจ์ด์ ์ด์ฃ . ๋˜, ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์€ ํ•ด์‹ฑ(hashing) ๊ธฐ๋ฒ•์„ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฃŒ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•˜๋„ค์š”. ํ•ด์‹ฑ ๊ธฐ๋ฒ•์ด ๋ฌด์—‡์ผ๊นŒ์š”? ์„ ์ƒ๋‹˜์ด ํ•™์ƒ๋“ค์˜ ์‹œํ—˜์ง€๋ฅผ ๋ณด๋‹ˆ ๋งŒ๋“์ด๊ฐ€ ๋นต์ ์„ ๋ฐ›์•˜๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ž˜์„œ ๋งŒ๋“์ด๋ฅผ ์ฐพ์•„์„œ ํ˜ผ๋‚ด์ฃผ๋ ค๊ณ  ํ•˜๋Š”๋ฐ, "1๋ฒˆ, ๋„ค๊ฐ€ ๋งŒ๋“์ด๋‹ˆ?" "์•„๋‹ˆ์š”." "2๋ฒˆ, ๋„ค๊ฐ€ ๋งŒ๋“์ด๋‹ˆ?" "์•„๋‹ˆ์˜ค." "3๋ฒˆ, ๋„ค๊ฐ€ ๋งŒ๋“์ด๋‹ˆ?" "์•„๋‹ˆ์˜ค." "40๋ฒˆ, ๋„ค๊ฐ€ ๋งŒ๋“์ด๋‹ˆ?" "์•„๋‹ˆ์˜ค." ์ด๋ ‡๊ฒŒ ๋งŒ๋“์ด๋ฅผ ์ฐพ์œผ๋ ค๋ฉด ์ข€ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ฒ ์ฃ ? ํ•˜์ง€๋งŒ, ์„ ์ƒ๋‹˜์ด "๋งŒ๋“์ด ๋‚˜์™€!"๋ผ๊ณ  ํ•˜์‹œ๋ฉด ๋ฐ”๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๊นŒ? ์ด ๋ฐฉ๋ฒ•์ด ๋ฐ”๋กœ ํ•ด์‹ฑ ๊ธฐ๋ฒ•๊ณผ ๋น„์Šทํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ž๋ฃŒ๋ฅผ ์•„์ฃผ ๋นจ๋ฆฌ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด์ง€์š”. ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์„ ๋งŒ๋“ค๊ณ , ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์œ„์—์„œ ๋ณด์‹  ๋Œ€๋กœ์ด๊ณ ์š”, ์›์†Œ๋ฅผ ์‚ญ์ œํ•  ๋• ์ด๋ ‡๊ฒŒ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. >>> del smalldic['dictionary'] ์‚ญ์ œ๊ฐ€ ์ž˜ ๋˜์—ˆ๋Š”์ง€ ํ•œ๋ฒˆ ํ™•์ธํ•ด ๋ณด์„ธ์š”. ์ด๋ฒˆ์—๋Š” family๋ผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ๊ฒŒ์š”. >>> family = {'mom': 'Kim', 'dad': 'Choi', 'baby': 'Choi'} >>> family {'mom': 'Kim', 'dad': 'Choi', 'baby': 'Choi'} family์˜ ํ‚ค๋“ค์„ ์–ป์œผ๋ ค๋ฉด ๋”•์…”๋„ˆ๋ฆฌ ์ด๋ฆ„ ๋’ค์—. keys()๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. >>> family.keys() dict_keys(['mom', 'dad', 'baby']) family์˜ ๊ฐ’๋“ค์„ ์–ป์œผ๋ ค๋ฉด ๋”•์…”๋„ˆ๋ฆฌ ์ด๋ฆ„ ๋’ค์—. values()๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. >>> family.values() dict_values(['Kim', 'Choi', 'Choi']) family์˜ ์›์†Œ(ํ‚ค/๊ฐ’ ์Œ)๋“ค์„ ์–ป์œผ๋ ค๋ฉด ์ด๋ฆ„ ๋’ค์—. items()๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. >>> family.items() dict_items([('mom', 'Kim'), ('dad', 'Choi'), ('baby', 'Choi')]) ๋”•์…”๋„ˆ๋ฆฌ์— ์–ด๋–ค ํ‚ค๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€๋Š” in์„ ์จ์„œ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์žˆ์œผ๋ฉด True, ์—†์œผ๋ฉด False๋ผ๊ณ  ๋Œ€๋‹ตํ•ด ์ฃผ์ฃ . >>> 'dad' in family True >>> 'sister' in family False ์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ์™ธ์šฐ๋ ค๊ณ  ์• ์“ฐ์‹ค ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์‚ฌ์šฉ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์„œ ์“ฐ๋ฉด ๋˜๋Š” ๊ฑฐ์ง€์š”. ๊ทธ๋Ÿผ ๋ชจ๋‘ ์ฆ๊ฑฐ์šด ํ•˜๋ฃจ ๋ณด๋‚ด์„ธ์š”~. 4.4.1 ์—ฐ์Šต ๋ฌธ์ œ: ์ˆซ์ž ์ฝ๊ธฐ(0~9) ๋ฌธ์ œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์€ ์ •์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ํ•œ๊ธ€ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ korean_number()๋ฅผ if ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ๋ง๊ณ  ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, ์‚ฌ์šฉ์ž๋Š” 0 ์ด์ƒ 9 ์ดํ•˜์˜ ์ •์ˆ˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. >>> korean_number(3) '์‚ผ' >>> korean_number(6) '์œก' >>> korean_number(9) '๊ตฌ' ์ฝ”๋“œ: ch04/korean_0_to_9.py 4.4.2 ์—ฐ์Šต ๋ฌธ์ œ: ํ•œ์ž ์„ฑ์–ด ๋ฌธ์ œ ๋‹ค์Œ ํ‘œ์˜ ํ•œ์ž ์„ฑ์–ด์™€ ๋œป์„ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋Š” ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. 1 ํ•œ์ž ์„ฑ์–ด ๋œป ๆฑŸๆน–ไน‹(๊ฐ•ํ˜ธ์ง€๋ฝ) ์ž์—ฐ์„ ๋ฒ— ์‚ผ์•„ ๋ˆ„๋ฆฌ๋Š” ์ฆ๊ฑฐ์›€ ๆฌฒ้€Ÿไธ้”(์š•์†๋ถ€๋‹ฌ) ๋นจ๋ฆฌํ•˜๊ณ ์ž ํ•˜๋ฉด ์ด๋ฃจ์ง€ ๋ชปํ•จ ็ฉๅฐๆˆๅคง(์ ์†Œ์„ฑ๋Œ€) ์ž‘์€ ๊ฒƒ์„ ์Œ“์•„ ํฐ ๊ฒƒ์„ ์ด๋ฃธ ๅ‹คๅ„‰็ฏ€็ด„(๊ทผ๊ฒ€์ ˆ์•ฝ) ๋ถ€์ง€๋Ÿฐํ•˜๊ณ  ์•Œ๋œฐํ•˜๊ฒŒ ์žฌ๋ฌผ์„ ์•„๋‚Œ ็ถ“ไธ–ๆฟŸๆฐ‘(๊ฒฝ์„ธ์ œ๋ฏผ) ์„ธ์ƒ์„ ๋‹ค์Šค๋ฆฌ๊ณ  ๋ฐฑ์„ฑ์„ ๊ตฌ์ œํ•จ ๅกž็ฟไน‹้ฆฌ(์ƒˆ์˜น์ง€๋งˆ) ์ธ์ƒ์˜ ๊ธธํ‰ํ™”๋ณต์€ ๋ณ€ํ™”๊ฐ€ ๋งŽ์•„์„œ ์˜ˆ์ธกํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›€ ๅฅฝไบ‹ๅคš้ญ”(ํ˜ธ์‚ฌ๋‹ค๋งˆ) ์ข‹์€ ์ผ์—๋Š” ํ”ํžˆ ๋ฐฉํ•ด๋˜๋Š” ์ผ์ด ๋งŽ์Œ ๆก‘็”ฐ็ขงๆตท(์ƒ์ „๋ฒฝํ•ด) ์„ธ์ƒ์ผ์˜ ๋ณ€์ฒœ์ด ์‹ฌํ•จ ่‡ชๆฅญ่‡ชๅพ—(์ž์—…์ž๋“) ์ž๊ธฐ๊ฐ€ ์ €์ง€๋ฅธ ์ผ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ž๊ธฐ๊ฐ€ ๋ฐ›์Œ ๅ› ๆžœๆ‡‰ๅ ฑ(์ธ๊ณผ์‘๋ณด) ์›์ธ๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ์ƒ์‘ํ•˜์—ฌ ๋ณด๋‹ตํ•œ๋‹ค ๆ„šๅ…ฌ็งปๅฑฑ(์šฐ๊ณต์ด์‚ฐ) ์–ด๋–ค ์ผ์ด๋“  ๋Š์ž„์—†์ด ๋…ธ๋ ฅํ•˜๋ฉด ๋ฐ˜๋“œ์‹œ ์ด๋ฃจ์–ด์ง ์‹คํ–‰ ๊ฒฐ๊ณผ: Enter๋ฅผ ๋ˆ„๋ฅด์„ธ์š”... ๆฑŸๆน–ไน‹(๊ฐ•ํ˜ธ์ง€๋ฝ) ์ž์—ฐ์„ ๋ฒ— ์‚ผ์•„ ๋ˆ„๋ฆฌ๋Š” ์ฆ๊ฑฐ์›€ Enter๋ฅผ ๋ˆ„๋ฅด์„ธ์š”... ๆฌฒ้€Ÿไธ้”(์š•์†๋ถ€๋‹ฌ) ๋นจ๋ฆฌํ•˜๊ณ ์ž ํ•˜๋ฉด ์ด๋ฃจ์ง€ ๋ชปํ•จ Enter๋ฅผ ๋ˆ„๋ฅด์„ธ์š”... ็ฉๅฐๆˆๅคง(์ ์†Œ์„ฑ๋Œ€) ์ž‘์€ ๊ฒƒ์„ ์Œ“์•„ ํฐ ๊ฒƒ์„ ์ด๋ฃธ (์ดํ•˜ ์ƒ๋žต) ํ’€์ด ์ฝ”๋“œ: ch04/hanja_idioms.py ์ถœ์ฒ˜: ์„œ์šธ์‚ฌ์ด๋ฒ„๋Œ€ํ•™๊ต โ€˜ํ•œ์ž์„ฑ์–ด์™€ ํผ์ฆโ€™ ์ˆ˜์—… โ†ฉ 4.4.3 ์—ฐ์Šต ๋ฌธ์ œ: ์ •์‹  ์งˆํ™˜ ๋ฌธ์ œ ๋‹ค์Œ txt๋Š” ์ •์‹ ์งˆํ™˜์˜ ๋ช…์นญ์„ ํ•œ๊ตญ์–ด์™€ ์˜์–ด ์šฉ์–ด๋กœ ๋‚˜์—ดํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.1 txt = '''์‹ ๊ฒฝ ๋ฐœ๋‹ฌ์žฅ์•  Neurodevelopmental Disorders ์กฐํ˜„๋ณ‘ ์ŠคํŽ™ํŠธ๋Ÿผ ๋ฐ ๊ธฐํƒ€ ์ •์‹ ๋ณ‘์  ์žฅ์•  Schizophrenia Spectrum and Other Psychotic Disorders ์–‘๊ทน์„ฑ ๋ฐ ๊ด€๋ จ ์žฅ์•  Bipolar and Related Disorders ์šฐ์šธ์žฅ์•  Depressive Disorders ๋ถˆ์•ˆ์žฅ์•  Anxiety Disorder ๊ฐ•๋ฐ• ๋ฐ ๊ด€๋ จ ์žฅ์•  ObsessiveCompulsive and Related Disorders ์™ธ์ƒ ๋ฐ ์ŠคํŠธ๋ ˆ์Šค ๊ด€๋ จ ์žฅ์•  Traumaand StressorRelated Disorders ํ•ด๋ฆฌ์žฅ์•  Dissociative Disorders ์‹ ์ฒด์ฆ์ƒ ๋ฐ ๊ด€๋ จ ์žฅ์•  Somatic Symptom and Related Disorders ๊ธ‰์‹ ๋ฐ ์„ญ์‹์žฅ์•  Feeding and Eating Disorders ๋ฐฐ์„ค์žฅ์•  Elimination Disorders ์ˆ˜๋ฉด๊ฐ์„ฑ ์žฅ์•  SleepWake Disorders ์„ฑ ๊ธฐ๋Šฅ๋ถ€์ „ Sexual Dysfunctions ์„ฑ๋ณ„ ๋ถˆ์พŒ๊ฐ Gender Dysphoria ํŒŒ๊ดด์ , ์ถฉ๋™์กฐ์ ˆ ๋ฐ ํ’ˆํ–‰ ์žฅ์•  Disruptive, ImpulseControl, and Conduct Disorders ๋ฌผ์งˆ ๊ด€๋ จ ๋ฐ ์ค‘๋… ์žฅ์•  SubstanceRelated and Addictive Disorders ์‹ ๊ฒฝ์ธ์ง€ ์žฅ์•  Neurocognitive Disorders ์„ฑ๊ฒฉ์žฅ์•  Personality Disorders ๋ณ€ํƒœ์„ฑ์š•์žฅ์•  Paraphilic Disorders ๊ธฐํƒ€ ์ •์‹ ์งˆํ™˜ Other Mental Disorders''' txt๋ฅผ ์ฝ์–ด ํ•œ๊ตญ์–ด์™€ ์˜์–ด ๋ช…์นญ์„ ๊ฐ๊ฐ ํ‚ค์™€ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ๊ฒฐ๊ณผ {'์‹ ๊ฒฝ ๋ฐœ๋‹ฌ์žฅ์• ': 'Neurodevelopmental Disorders', '์กฐํ˜„๋ณ‘ ์ŠคํŽ™ํŠธ๋Ÿผ ๋ฐ ๊ธฐํƒ€ ์ •์‹ ๋ณ‘์  ์žฅ์• ': 'Schizophrenia Spectrum and Other Psychotic Disorders', '์–‘๊ทน์„ฑ ๋ฐ ๊ด€๋ จ ์žฅ์• ': 'Bipolar and Related Disorders', '์šฐ์šธ์žฅ์• ': 'Depressive Disorders', '๋ถˆ์•ˆ์žฅ์• ': 'Anxiety Disorder', '๊ฐ•๋ฐ• ๋ฐ ๊ด€๋ จ ์žฅ์• ': 'ObsessiveCompulsive and Related Disorders', '์™ธ์ƒ ๋ฐ ์ŠคํŠธ๋ ˆ์Šค ๊ด€๋ จ ์žฅ์• ': 'Traumaand StressorRelated Disorders', 'ํ•ด๋ฆฌ์žฅ์• ': 'Dissociative Disorders', '์‹ ์ฒด์ฆ์ƒ ๋ฐ ๊ด€๋ จ ์žฅ์• ': 'Somatic Symptom and Related Disorders', '๊ธ‰์‹ ๋ฐ ์„ญ์‹์žฅ์• ': 'Feeding and Eating Disorders', '๋ฐฐ์„ค์žฅ์• ': 'Elimination Disorders', '์ˆ˜๋ฉด๊ฐ์„ฑ ์žฅ์• ': 'SleepWake Disorders', '์„ฑ ๊ธฐ๋Šฅ๋ถ€์ „': 'Sexual Dysfunctions', '์„ฑ๋ณ„ ๋ถˆ์พŒ๊ฐ': 'Gender Dysphoria', 'ํŒŒ๊ดด์ , ์ถฉ๋™์กฐ์ ˆ ๋ฐ ํ’ˆํ–‰ ์žฅ์• ': 'Disruptive, ImpulseControl, and Conduct Disorders', '๋ฌผ์งˆ ๊ด€๋ จ ๋ฐ ์ค‘๋… ์žฅ์• ': 'SubstanceRelated and Addictive Disorders', '์‹ ๊ฒฝ์ธ์ง€ ์žฅ์• ': 'Neurocognitive Disorders', '์„ฑ๊ฒฉ์žฅ์• ': 'Personality Disorders', '๋ณ€ํƒœ์„ฑ์š•์žฅ์• ': 'Paraphilic Disorders', '๊ธฐํƒ€ ์ •์‹ ์งˆํ™˜': 'Other Mental Disorders'} tip ord()์™€ chr() ord() ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์— ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ๊ฐ’์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. >>> ord('A') 65 >>> ord('Z') 90 >>> ord('a') 97 >>> ord('z') 122 >>> ord('0') 48 >>> ord('9') 57 ์—ญ์œผ๋กœ, chr() ํ•จ์ˆ˜์— ์ฝ”๋“œ๊ฐ’์„ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์œผ๋ฉด ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. >>> chr(65) 'A' ํ•œ๊ธ€์— ๋Œ€ํ•ด์„œ๋„ ๋‘ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> ord('๊ฐ€') 44032 >>> chr(55197) 'ํž' tip split()๊ณผ splitlines() split()์€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•œ ๊ฒƒ๋“ค์„ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. >>> 'hello world'.split() ['hello', 'world'] ์—ฌ๋Ÿฌ ํ–‰์œผ๋กœ ์ด๋ค„์ง„ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜๋ ค๋ฉด \n์„ ๊ตฌ๋ถ„์ž๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. >>> love = '''L is for the way you look at me ... O is for the only one I see ... V is very, very extraordinary ... E is even more than anyone that you adore can''' >>> love.split('\n') ['L is for the way you look at me', 'O is for the only one I see', 'V is very, very extraordinary', 'E is even more than anyone that you adore can'] ์œ„์™€ ๊ฐ™์ด ์ค„๋ฐ”๊ฟˆ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•  ๋•Œ๋Š” splitlines()๋ฅผ ์จ๋„ ๋ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ: ch04/mental_disorders.py APA ์ €/๊ถŒ์ค€์ˆ˜,<NAME>, ๋‚จ๊ถ๊ธฐ, ๋ฐ•์›๋ช… ์—ญ, โŸช์ •์‹ ์งˆํ™˜์˜ ์ง„๋‹จ ๋ฐ ํ†ต๊ณ„ ํŽธ๋žŒโŸซ, ํ•™์ง€์‚ฌ, 2015 โ†ฉ 4.4.4 ์—ฐ์Šต ๋ฌธ์ œ: ํ”„๋ž™ํ„ธ 0๊ณผ 1๋กœ ์ด๋ค„์ง„ ๋ฌธ์ž์—ด ํ•œ ํ–‰์ด ์žˆ์„ ๋•Œ, ์ •ํ•ด์ง„ ๊ทœ์น™์— ๋”ฐ๋ผ ๊ทธ๋‹ค์Œ ํ–‰์„ ์ƒ์„ฑํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด์–ด์ง„ ์„ธ ์ˆซ์ž ์กฐํ•ฉ์— ๋”ฐ๋ผ ๋‹ค์Œ์— ์˜ฌ ์ˆซ์ž๊ฐ€ ์ •ํ•ด์ง‘๋‹ˆ๋‹ค. ํ˜„์žฌ ํŒจํ„ด 111 110 101 100 011 010 001 000 ๊ฐ€์šด๋ฐ ์ž๋ฆฌ์— ์ƒˆ๋กœ ์˜ฌ ์ˆซ์ž 0 1 0 1 1 0 1 0 ์˜ˆ๋ฅผ ๋“ค์–ด, ์ฒซ ํ–‰์ด 000010000์ด๋ฉด ๊ทธ๋‹ค์Œ ํ–‰์€ 000101000์ด ๋ฉ๋‹ˆ๋‹ค. 000010000 000101000 ๋‹ค์Œ์€ ํ•œ ํ–‰์— 61๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ์žˆ๋Š” ์˜ˆ์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ–‰์€ ๊ฐ€์šด๋ฐ ์ˆซ์ž๊ฐ€ 1, ๋‚˜๋จธ์ง€๋Š” ๋ชจ๋‘ 0์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ํ–‰๋ถ€ํ„ฐ๋Š” ๊ทœ์น™์— ๋”ฐ๋ผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 0000000000000000000000000000001000000000000000000000000000000 0000000000000000000000000000010100000000000000000000000000000 0000000000000000000000000000100010000000000000000000000000000 0000000000000000000000000001010101000000000000000000000000000 0000000000000000000000000010000000100000000000000000000000000 0000000000000000000000000101000001010000000000000000000000000 0000000000000000000000001000100010001000000000000000000000000 0000000000000000000000010101010101010100000000000000000000000 0000000000000000000000100000000000000010000000000000000000000 0000000000000000000001010000000000000101000000000000000000000 0000000000000000000010001000000000001000100000000000000000000 0000000000000000000101010100000000010101010000000000000000000 0000000000000000001000000010000000100000001000000000000000000 0000000000000000010100000101000001010000010100000000000000000 0000000000000000100010001000100010001000100010000000000000000 0000000000000001010101010101010101010101010101000000000000000 0000000000000010000000000000000000000000000000100000000000000 0000000000000101000000000000000000000000000001010000000000000 0000000000001000100000000000000000000000000010001000000000000 0000000000010101010000000000000000000000000101010100000000000 0000000000100000001000000000000000000000001000000010000000000 0000000001010000010100000000000000000000010100000101000000000 0000000010001000100010000000000000000000100010001000100000000 0000000101010101010101000000000000000001010101010101010000000 0000001000000000000000100000000000000010000000000000001000000 0000010100000000000001010000000000000101000000000000010100000 0000100010000000000010001000000000001000100000000000100010000 0001010101000000000101010100000000010101010000000001010101000 0010000000100000001000000010000000100000001000000010000000100 0101000001010000010100000101000001010000010100000101000001010 ์ฝ”๋“œ: ch04/rule90.py ์ฐธ๊ณ  Rule 90 ใ€Š Nature of Code ใ€‹ 4.5 ์„ธํŠธ(set) ์ด๋ฒˆ์—๋Š” โ€˜์ง‘ํ•ฉโ€™์„ ํ‘œํ˜„ํ•˜๋Š” ์„ธํŠธ(set)๋ฅผ ์ข€ ๋” ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ณผ์ผ์„ ๋‚˜ํƒ€๋‚ด๋Š” fruits ์„ธํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค๋ƒ ~ >>> fruits = {'apple', 'banana', 'orange'} ์‚ฌ๊ณผ, ๋ฐ”๋‚˜๋‚˜, ์˜ค๋ Œ์ง€๋ฅผ ์›์†Œ๋กœ ๊ฐ–๋Š” fruits ์„ธํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์„ธํŠธ๋Š” ์ค‘๊ด„ํ˜ธ({, })๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•„์ฐจ, ๋ง›์žˆ๋Š” ๋ง๊ณ ๋ฅผ ๋น ๋œจ๋ ธ๋„ค์š”. add()๋กœ ์ถ”๊ฐ€ํ• ๊ฒŒ์š”. >>> fruits.add('mango') >>> fruits {'orange', 'apple', 'mango', 'banana'} ์ด๋ฒˆ์—๋Š” ํšŒ์‚ฌ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด๋ณผ๊นŒ์š”? >>> companies = set() ํšŒ์‚ฌ ์ด๋ฆ„์ด ๋– ์˜ค๋ฅด์ง€ ์•Š์•„์„œ ์ผ๋‹จ set()๋กœ ๋นˆ ์„ธํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์•„, ์ƒ๊ฐ๋‚ฌ์–ด์š”. >>> companies = {'apple', 'microsoft', 'google'} ์ด์ œ fruits์™€ companies ์„ธํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํƒ€์ž…์„ ํ™•์ธํ•ด ๋ณผ๊นŒ์š”? >>> type(fruits) <class 'set'> >>> type(companies) <class 'set'> ์„ธํŠธ๋ฅผ ์ด์šฉํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ง‘ํ•ฉ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> fruits & companies # ๊ต์ง‘ํ•ฉ {'apple'} >>> fruits | companies # ํ•ฉ์ง‘ํ•ฉ {'apple', 'mango', 'microsoft', 'orange', 'google', 'banana'} ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ ์„ธํŠธ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ๋‹ด์€ ๋’ค set์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์“ธ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> list_of_sets = [fruits, companies] >>> set.intersection(*list_of_sets) # ๊ต์ง‘ํ•ฉ {'apple'} >>> set.union(*list_of_sets) # ํ•ฉ์ง‘ํ•ฉ {'google', 'apple', 'banana', 'mango', 'microsoft', 'orange'} apple์€ fruits์—๋„ ์†ํ•˜๊ณ  companies์—๋„ ์†ํ•˜๋Š”๋ฐ, ์œ„ ํ•ฉ์ง‘ํ•ฉ์˜ ๊ฒฐ๊ณผ์— ํ•œ ๋ฒˆ๋งŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์„ธํŠธ๋Š” ์ค‘๋ณต ์›์†Œ๋ฅผ ๊ฐ–์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜, ์›์†Œ์˜ ์ˆœ์„œ๊ฐ€ ์œ ์ง€๋˜์ง€ ์•Š๋Š” ํŠน์ง•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. >>> alphabet = list('google') >>> alphabet ['g', 'o', 'o', 'g', 'l', 'e'] >>> set(alphabet) {'e', 'o', 'g', 'l'} ์•„ ์ฐธ, ์ง‘ํ•ฉ๋ผ๋ฆฌ ๋บ„์…ˆ๋„ ํ•  ์ˆ˜ ์žˆ์–ด์š”! >>> S1 = {1, 2, 3, 4, 5, 6, 7} >>> S2 = {3, 6, 9} >>> S1 - S2 {1, 2, 4, 5, 7} 4.5.1 ์—ฐ์Šต ๋ฌธ์ œ: ์ฃผ์‚ฌ์œ„ ๋ˆˆ์˜ ํ•ฉ ๋‹ค์Œ ๊ทธ๋ฆผ์€ ์ฃผ์‚ฌ์œ„ ๋‘ ๊ฐœ๋ฅผ ๋˜์ ธ์„œ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋‚˜์—ดํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. (๊ทธ๋ฆผ ์ถœ์ฒ˜: https://cs50.harvard.edu/ai/2020/notes/2/) ๋‘ ์ฃผ์‚ฌ์œ„ ๋ˆˆ์˜ ํ•ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (๊ทธ๋ฆผ ์ถœ์ฒ˜: https://cs50.harvard.edu/ai/2020/notes/2/) ๋ฌธ์ œ ์ฃผ์‚ฌ์œ„ ๋‘ ๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ๊ฐœ๋Š” ํ‰๋ฒ”ํ•œ ์ฃผ์‚ฌ์œ„์ธ๋ฐ, ๋‹ค๋ฅธ ํ•œ ๊ฐœ์˜ ๊ฐ ๋ฉด์—๋Š” 2์—์„œ 13๊นŒ์ง€์˜ ์†Œ์ˆ˜๊ฐ€ ์ ํ˜€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋‘ ์ฃผ์‚ฌ์œ„์˜ ๋ˆˆ์„ ํŠœํ”Œ dice1๊ณผ dice2๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. dice1 = (1, 2, 3, 4, 5, 6) dice2 = (2, 3, 5, 7, 11, 13) ๋‘ ์ฃผ์‚ฌ์œ„๋ฅผ ๋˜์กŒ์„ ๋•Œ ๋ˆˆ์˜ ํ•ฉ์œผ๋กœ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜์„ธ์š”. ๋‹จ, ๊ฐ™์€ ์ˆซ์ž๋Š” ํ•œ ๋ฒˆ๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ: ch04/sum_dice.py 4.5.2 ์—ฐ์Šต ๋ฌธ์ œ: ๋๋ง์ž‡๊ธฐ (1) ๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์ปดํ“จํ„ฐ์™€ ํ”Œ๋ ˆ์ด์–ด๋Š” ์ž๊ธฐ ํ„ด(turn, ์ฐจ๋ก€)์ด ๋˜๋ฉด ์ด์ „์— ์ƒ๋Œ€๋ฐฉ์ด ๋งํ•œ ๋‹จ์–ด์˜ ๋ ๊ธ€์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ๋งํ•ด์•ผ ํ•˜๋ฉฐ, ์ด์ „์— ์ผ๋˜ ๋‹จ์–ด๋Š” ๋งํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 1. ์ปดํ“จํ„ฐ๊ฐ€ ๋จผ์ € โ€˜๊ธฐ์ฐจโ€™๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. 2. ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์˜ฌ๋ฐ”๋กœ ์ž…๋ ฅํ–ˆ์œผ๋ฉด ๋‹ค์Œ์œผ๋กœ ์ง„ํ–‰ํ•˜๊ณ , ๋‹จ์–ด๋ฅผ ์ž˜๋ชป ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ โ€˜์กŒ์–ดโ€™๋ผ๊ณ  ์ž…๋ ฅํ•˜๋ฉด ์ปดํ“จํ„ฐ๊ฐ€ ์ด๊ธฐ๊ณ  ๋๋ƒ…๋‹ˆ๋‹ค. 3. ์ปดํ“จํ„ฐ๋Š” ๋‹ค์Œ์˜ ๋‹จ์–ด ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ณจ๋ผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋งˆ๋•…ํ•œ ๊ฒƒ์ด ์—†์œผ๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ์ด๊ธฐ๊ณ  ๋๋ƒ…๋‹ˆ๋‹ค. ๊ฒŒ๋ง›์‚ด, ๊ตฌ๋ฉ, ๊ธ€๋ผ์ด๋”, ๊ธฐ์ฐจ, ๋Œ€๋กฑ, ๋”์น˜ํŽ˜์ด, ๋กฑ๋‹ค๋ฆฌ, ๋ฆฌ๋ณธ, ๋ฉ๊ฒŒ, ๋ฐ•์ฅ, ๋ณด๋‹›, ๋นจ๋Œ€, ์‚ด๊ตฌ, ์–‘์‹ฌ, ์ด๋นจ, ์ด์ž, ์ž์œจ, ์ฃผ๊ธฐ, ์ฅ๊ตฌ๋ฉ, ์ฐจ๋ฐ•, ํŠธ๋ผ์ด์•ต๊ธ€ 4. 2~3์„ ๋ฐ˜๋ณต โ€ป ๋‘์Œ ๋ฒ•์น™์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ 1 ํ„ด ์ž…์ถœ๋ ฅ ์ปดํ“จํ„ฐ <์‹œ์ž‘>๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜์ž. ๋‚ด๊ฐ€ ๋จผ์ € ๋งํ• ๊ฒŒ. ๊ธฐ์ฐจ ํ”Œ๋ ˆ์ด์–ด ์ž๋™์ฐจ ์ปดํ“จํ„ฐ ๊ธ€์ž๊ฐ€ ์•ˆ ์ด์–ด์ ธ. ๋‚ด๊ฐ€ ์ด๊ฒผ๋‹ค!<๋> ์˜ˆ 2 ํ„ด ์ž…์ถœ๋ ฅ ์ปดํ“จํ„ฐ <์‹œ์ž‘>๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜์ž. ๋‚ด๊ฐ€ ๋จผ์ € ๋งํ• ๊ฒŒ. ๊ธฐ์ฐจ ํ”Œ๋ ˆ์ด์–ด ์ฐจ ๋ฐ• ์ปดํ“จํ„ฐ ๋ฐ•์ฅ ํ”Œ๋ ˆ์ด์–ด ์ฅ๊ตฌ๋ฉ ์ปดํ“จํ„ฐ ๋ฉ๊ฒŒ ํ”Œ๋ ˆ์ด์–ด ๊ฒŒ์‹œํŒ ์ปดํ“จํ„ฐ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๋‚ด๊ฐ€ ์กŒ์–ด.<๋> ์˜ˆ 3 ํ„ด ์ž…์ถœ๋ ฅ ์ปดํ“จํ„ฐ <์‹œ์ž‘>๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜์ž. ๋‚ด๊ฐ€ ๋จผ์ € ๋งํ• ๊ฒŒ. ๊ธฐ์ฐจ ํ”Œ๋ ˆ์ด์–ด ์ฐจ์ฃผ ์ปดํ“จํ„ฐ ์ฃผ๊ธฐ ํ”Œ๋ ˆ์ด์–ด ๊ธฐ์ฐจ ์ปดํ“จํ„ฐ ์•„๊นŒ ํ–ˆ๋˜ ๋ง์ด์•ผ. ๋‚ด๊ฐ€ ์ด๊ฒผ์–ด!<๋> ์˜ˆ 4 ํ„ด ์ž…์ถœ๋ ฅ ์ปดํ“จํ„ฐ <์‹œ์ž‘>๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜์ž. ๋‚ด๊ฐ€ ๋จผ์ € ๋งํ• ๊ฒŒ. ๊ธฐ์ฐจ ํ”Œ๋ ˆ์ด์–ด ์ฐจ๋Ÿ‰ ์ปดํ“จํ„ฐ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๋‚ด๊ฐ€ ์กŒ์–ด.<๋> ํ’€์ด ์ฝ”๋“œ: ch04/wordgame.py 5. ๋ชจ๋“ˆ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๋งŒ๋“ค์–ด๋†“์€ ๋ชจ๋“ˆ(module)์„ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ์–ด์š”! ์ด ์žฅ์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฒƒ: ๋ชจ๋“ˆ์ด๋ž€ ๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ค๊ธฐ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ชจ๋“ˆ 5.1 ๋ชจ๋“ˆ์ด๋ž€ ์ž๋ฃŒ๊ตฌ์กฐ ๋ถ€๋ถ„์ด ๋“œ๋””์–ด ๋๋‚ฌ์ฃ ? ์ข€ ์ง€๋ฃจํ•˜์…จ์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ปดํ“จํ„ฐ์— ์žˆ์–ด์„œ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์ค‘์š”์„ฑ์€ ์ ˆ๋Œ€์ ์ด๋ผ๊ณ  ํ•  ๋งŒํผ ํฌ๋‹ต๋‹ˆ๋‹ค. ์ž‘๊ฒŒ๋Š” CPU ๋‚ด์˜ ๊ธฐ์–ต์žฅ์†Œ์—์„œ๋ถ€ํ„ฐ, ํฌ๊ฒŒ๋Š” ํŒŒ์ผ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ์ „์ฒด ์‹œ์Šคํ…œ์—๊นŒ์ง€ ๋‘๋ฃจ ์ ์šฉ๋œ๋‹ค๊ณ  ํ•˜๋‹ˆ๊นŒ ํ‹ˆํ‹ˆ์ด ๊ณต๋ถ€ํ•ด๋‘์‹œ๋ฉด ์ข‹๊ฒ ๋„ค์š”. ์šฐ๋ฆฌ๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€๋Š” ํ˜ผ์ž์„œ ๋ณ€์ˆ˜, ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์“ฐ๋ฉด์„œ ์ž๊ธ‰์ž์กฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด, ์ด์ œ๋ถ€ํ„ฐ๋Š” ๋‚จ์ด ๋งŒ๋“ค์–ด ๋†“์€ ๋ถ€ํ’ˆ์„ ๊ฐ€์ ธ๋‹ค๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šธ ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๋ณต์žกํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ๋ชจ๋“  ๊ณผ์ •์„ ์ง์ ‘ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค๋ฉด ์–ด๋–ค ๋ชจ์Šต์ด ๋ ๊นŒ์š”? ์ „์ฒด์ ์ธ ๋ชจ์Šต์—์„œ๋ถ€ํ„ฐ ์ž‘์€ ๊ธฐ๋Šฅ ํ•˜๋‚˜ํ•˜๋‚˜๊นŒ์ง€ ๋ชจ๋‘ ๊ตฌ์ƒํ•ด์„œ, ๋งŒ๋“ค๊ณ , ์˜ค๋ฅ˜๋ฅผ ์ˆ˜์ •ํ•ด์„œ ํ•œ๊ณณ์— ๋ชจ์•„๋‘๋ฉด ๋˜ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๊ณ โ€ฆ ๋”๊ตฌ๋‚˜, ๋˜ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ๋‚˜์™€ ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋ฉด์„œ ๋˜‘๊ฐ™์€ ์‹œํ–‰์ฐฉ์˜ค๋ฅผ ๋‹ต์Šตํ•  ํ…Œ๊ณ ์š”. ๊ทธ๋ž˜์„œ, ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ๋Š” ๋ชจ๋“ˆ์ด๋ผ๋Š” ๊ฐœ๋…์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“ˆ์€ ํ”„๋กœ๊ทธ๋žจ์˜ ๊พธ๋Ÿฌ๋ฏธ๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋˜์ง€์š”. ํ• ๋จธ๋‹ˆ ๋Œ ์ง‘ ์ˆ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•ด์„œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ณต๊ตฌ๋ฅผ ๋“ค๊ณ  ๊ฐ„๋‹ค๊ณ  ์ƒ์ƒํ•ด ๋ด…์‹œ๋‹ค. ์†์— ๋‹ค ๋“ค๊ณ  ๊ฐ€๊ธฐ๋Š” ๋ถˆํŽธํ•˜๋‹ˆ ๊ณต๊ตฌํ†ต์— ๋„ฃ๋Š” ๊ฒŒ ์ข‹๊ฒ ์ฃ ? math ๋ชจ๋“ˆ ์ˆ˜ํ•™์ ์ธ ๊ณ„์‚ฐ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด math๋ผ๋Š” ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. >>> import math # math ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์™€๋ผ ์ œ๊ณฑ๊ทผ(square root)์„ ๊ตฌํ•ด๋ณผ๊นŒ์š”? >>> math.sqrt(2) # 2์˜ ์ œ๊ณฑ๊ทผ 1.4142135623730951 >>> math.sqrt(3) # 3์˜ ์ œ๊ณฑ๊ทผ 1.7320508075688772 >>> math.sqrt(4) # 4์˜ ์ œ๊ณฑ๊ทผ 2.0 ์›์ฃผ์œจ๋„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> math.pi # math ๋ชจ๋“ˆ์˜ ๋ณ€์ˆ˜ pi์˜ ๊ฐ’์€? 3.1415926535897931 ์œ„์—์„œ๋Š” math ๋ชจ๋“ˆ ๋‚ด์— ์ •์˜๋˜์–ด ์žˆ๋Š” pi ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. pi๋Š” ์›์ฃผ์œจ์„ ๋œปํ•˜์ง€์š”. calendar ๋ชจ๋“ˆ ์ด๋ฒˆ์—๋Š” ๋‹ฌ๋ ฅ์„ ๋ถˆ๋Ÿฌ๋ณผ๊นŒ์š”? ๋”ฑ ๋‘ ์ค„๋งŒ ์น˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. >>> import calendar >>> calendar.prmonth(2013, 7) July 2013 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ํ›Œ๋ฅญํ•˜์ฃ ? tkinter ๋ชจ๋“ˆ ์ด๋ฒˆ์—๋Š” ๋”์šฑ ํ›Œ๋ฅญํ•œ ๊ฒƒ์„ ๋ณด์—ฌ๋“œ๋ฆฌ์ง€์š”. >>> from tkinter import * >>> widget = Label(None, text='I love Python!') >>> widget.pack() ์ด๋ ‡๊ฒŒ ํŒŒ์ด์ฌ์—์„œ๋Š” ์ข‹์€ ๊ธฐ๋Šฅ๋“ค์„ ๋ชจ๋“ˆ๋กœ ๋ฌถ์–ด์„œ ์ž์ฒด์ ์œผ๋กœ ์ œ๊ณตํ•ด ์ค€๋‹ต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ ๋Œ€๋ถ€๋ถ„์˜ ์–ธ์–ด์—์„œ ์ด๋Ÿฐ ์‹์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•ด ์ฃผ์ง€์š”. ๋ชจ๋“ˆ์„ ์ž˜ ๋“ค์—ฌ๋‹ค๋ณด๋ฉด ๋ฐฐ์šธ ๊ฒƒ์ด ๋งŽ์„ ๊ฒƒ ๊ฐ™๊ตฐ์š”. ๋‚ด์ผ ๋˜ ๋งŒ๋‚˜์š”~. 5.2 ๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ค๊ธฐ(import) ์–ด๋–ค ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ป˜์„œ ์ด๋ ‡๊ฒŒ ๋ง์”€ํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๋‹จ๊ธฐ๊ฐ„์— ๋›ฐ์–ด๋‚œ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋˜๋ ค๊ณ  ํ•˜๋ฉด ์ ˆ๋Œ€ ์„ฑ๊ณตํ•  ์ˆ˜ ์—†๋Š๋‹ˆ๋ผ. ์˜ค๋Š˜์€ ๋ชจ๋“ˆ์„ ์–ด๋–ป๊ฒŒ ๋ถˆ๋Ÿฌ์˜ค๋Š”์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ฃ . ์–ด์ œ ํ•ด๋ณด์…”์„œ ๋Œ€์ถฉ์€ ์•Œ๊ณ  ๊ณ„์‹œ๊ฒ ์ง€๋งŒ import๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. import๋Š” '์ˆ˜์ž…ํ•˜๋‹ค', '๊ฐ€์ ธ์˜ค๋‹ค'๋ผ๋Š” ๋œป์„ ๊ฐ–๊ณ  ์žˆ๊ณ ์š”, ์ปดํ“จํ„ฐ์—์„œ๋Š” ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๊ณ  ์˜ค๋Š” ๊ฒƒ์„ ๋œปํ•˜์ง€์š”. ํŒŒ์ด์ฌ์—์„œ ์ž„ํฌํŠธ๋ฅผ ํ•˜๋Š” ๋ฐฉ๋ฒ• ๋‘ ๊ฐ€์ง€๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•: import ๋ชจ๋“ˆ ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•: from ๋ชจ๋“ˆ import ์ด๋ฆ„ ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋ชจ๋“ˆ ์ „์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ ์š”, ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋ชจ๋“ˆ ๋‚ด์—์„œ ํ•„์š”ํ•œ ๊ฒƒ๋งŒ ์ฝ• ์ฐ์–ด์„œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ด์ฃ . ๊ณต๊ตฌํ†ต์— ๋น„์œ ํ•˜๋ฉด ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์–ด์š”. ๋‘ ๋ฐฉ๋ฒ•์„ ๋น„๊ตํ•ด ๋ณผ๊นŒ์š”? ์–ด์ œ ์†Œ๊ฐœํ•ด ๋“œ๋ฆฐ tkinter(ํ‹ฐ ์ผ€์ด ์ธํ„ฐ) ๋ชจ๋“ˆ์„ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ž„ํฌํŠธ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> import tkinter >>> tkinter.widget = tkinter.Label(None, text='I love Python!') >>> tkinter.widget.pack() ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ๋ชจ๋“ˆ ๋‚ด์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ชจ๋“ˆ. ๋ณ€์ˆ˜์˜<NAME>์œผ๋กœ ์จ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งค๋ฒˆ ์จ์ฃผ๋ ค๋ฉด ์ข€ ๋ฒˆ๊ฑฐ๋กญ๊ฒ ์ฃ ? >>> from tkinter import * >>> widget = Label(None, text='I love Python!') >>> widget.pack() ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋ชจ๋“ˆ ๋‚ด์˜ ์ด๋ฆ„์„ ์ฝ• ์ฐ์–ด์„œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ธ๋ฐ, ์œ„์—์„œ๋Š” tkinter์— ์žˆ๋Š” ๊ฒƒ์„ ์ „๋ถ€(*) ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ข€ ๋” ํŽธ๋ฆฌํ•˜๊ตฐ์š”. ํ•˜์ง€๋งŒ ๋งˆ๋ƒฅ ์ข‹๊ธฐ๋งŒ ํ•œ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ๋ž๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์—์„œ๋Š” ๋ฌธ์ž์—ด์ด์—ˆ๋˜ Label์ด ์ž„ํฌํŠธ ๋ฌธ ์‹คํ–‰ ํ›„ tkinter์˜ Label๋กœ ๋ฐ”๋€Œ์–ด ๋ฒ„๋ฆฐ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> Label = 'This is a Label' >>> from tkinter import * >>> Label <class 'tkinter.Label'> ์ด๋Ÿฐ ํŠน์„ฑ์„ ์ดํ•ดํ•˜๊ณ  ์ƒํ™ฉ์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ดค๋Š”๋ฐ์š”, ๋ถˆ๋Ÿฌ์˜จ ๋ชจ๋“ˆ์ด ํ•„์š” ์—†์„ ๋• ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”? ํ•„์š” ์—†๋Š” ๋ชจ๋“ˆ์€ ์š”๋ ‡๊ฒŒ ์ง€์›Œ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. del ๋ชจ๋“ˆ ๊ผญ ๊ทธ๋ ‡๊ฒŒ ํ•ด์ค„ ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ ์‹ถ์ง€๋งŒ, ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋‹ค ๋ณด๋ฉด ์ด๋Ÿฐ์ €๋Ÿฐ ์ผ์ด ์ƒ๊ธฐ๋‹ˆ๊นŒ ์•Œ์•„๋‘์ž๊ณ ์š”. ํ•œ ๋ฒˆ ์ž„ํฌํŠธ ํ•œ ๋ชจ๋“ˆ์„ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ค์‹œ ๋กœ๋“œ(reload) ํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. from importlib import reload reload(๋ชจ๋“ˆ) 5.2.1 ์—ฐ์Šต ๋ฌธ์ œ: ๋ชจ๋“ˆ ์‚ฌ์šฉ๋ฒ• ์•Œ์•„๋‚ด๊ธฐ calendar ๋ชจ๋“ˆ์„ ์ข€ ๋” ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ 1. calendar ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•˜๋Š” ๋ฌธ์žฅ์„ ์™„์„ฑํ•˜์„ธ์š”. >>> ____ calendar calendar ๋ชจ๋“ˆ์— ๋ฌด์—‡์ด ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด์„ธ์š”. >>> dir(calendar) ['Calendar', 'EPOCH', 'FRIDAY', 'February', 'HTMLCalendar', 'IllegalMonthError', 'IllegalWeekdayError', 'January', 'LocaleHTMLCalendar', 'LocaleTextCalendar', 'MONDAY', 'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY', 'TextCalendar', 'WEDNESDAY', '_EPOCH_ORD', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_colwidth', '_locale', '_localized_day', '_localized_month', '_monthlen', '_nextmonth', '_prevmonth', '_spacing', 'c', 'calendar', 'datetime', 'day_abbr', 'day_name', 'different_locale', 'error', 'firstweekday', 'format', 'formatstring', 'isleap', 'leapdays', 'main', 'mdays', 'month', 'month_abbr', 'month_name', 'monthcalendar', 'monthrange', 'prcal', 'prmonth', 'prweek', 'repeat', 'setfirstweekday', 'sys', 'timegm', 'week', 'weekday', 'weekheader'] ์ด์™€ ๊ฐ™์ด ๋ชจ๋“ˆ์€ ์ˆ˜๋งŽ์€ ๊ณต๊ตฌ๊ฐ€ ๋“ค์–ด ์žˆ๋Š” '๊ณต๊ตฌํ†ต'์ด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ 2. calendar ๋ชจ๋“ˆ์— leap์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š” ์ด๋ฆ„์€ ์–ด๋–ค ๊ฒƒ์ด ์žˆ๋Š”์ง€ ์ฐพ์•„๋ณด์„ธ์š”. 1 >>> [x for x in dir(calendar) if 'leap' in x] [____, ____] ๋ฌธ์ œ 3. ์ฃผ์–ด์ง„ ํ•ด๊ฐ€ ์œค๋…„์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ•์„ ํ™•์ธํ•ด ๋ณด์„ธ์š”. >>> help(____) Help on function ____ in module calendar: ____(year) Return True for leap years, False for non-leap years. ๋ฌธ์ œ 4. ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ 2077๋…„์ด ์œค๋…„์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•ด ๋ณด์„ธ์š”. >>> ____ False ์ฝ”๋“œ: ch05/using_modules.ipynb ์ด ๊ตฌ๋ฌธ์€ list comprehension์ด๋ผ๋Š” ๊ฒƒ์œผ๋กœ, 'comprehension'์€ '์ปดํ”„๋ฆฌ ํ—จ ์…˜', '๋‚ดํฌ', 'ํ•จ์ถ•', '์กฐ๊ฑด ์ œ์‹œ' ๋“ฑ ์—ฌ๋Ÿฌ ์ด๋ฆ„์œผ๋กœ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. โ†ฉ 5.2.2 ์—ฐ์Šต ๋ฌธ์ œ: ์ง๊ฐ์‚ผ๊ฐํ˜•์˜ ๋น—๋ณ€ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ ๋ฌธ์ œ ํ”ผํƒ€๊ณ ๋ผ์Šค ์ •๋ฆฌ์— ๋”ฐ๋ผ, ์ง๊ฐ์‚ผ๊ฐํ˜•์˜ ๋‘ ์ง๊ฐ๋ณ€ ์™€์˜ ๊ธธ์ด๋ฅผ ์•Œ๋ฉด ๋น—๋ณ€์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. = 2 b ๋ฌธ์ œ 1 = , = ์ผ ๋•Œ ๊ฐ€ ์–ผ๋งˆ์ธ์ง€ ํŒŒ์ด์ฌ ์…ธ์—์„œ ๊ตฌํ•˜์„ธ์š”. ๋ฌธ์ œ 2 ๋‘ ์ง๊ฐ๋ณ€( , )์˜ ๊ธธ์ด๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ๋น—๋ณ€( )์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ hypotenuse()๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์ถœ๋ ฅ์€ ์†Œ์ˆ˜์  ์…‹์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•ฉ๋‹ˆ๋‹ค. >>> hypotenuse(3, 4) 5.0 >>> hypotenuse(10, 20) 22.36 ch05/hypotenuse.txt ch05/right_triangle.py ์ฐธ๊ณ  ํ”ผํƒ€๊ณ ๋ผ์Šค ์ •๋ฆฌ - ์นธ ์•„์นด๋ฐ๋ฏธ 5.2.3 ์—ฐ์Šต ๋ฌธ์ œ: calendar์™€ tkinter ๋ฌธ์ œ 1. calendar ๋‹ค์Œ์€ 2021๋…„ 2์›”์˜ ๋‹ฌ๋ ฅ์„ ๋ณ€์ˆ˜ m์œผ๋กœ ์ €์žฅํ•˜๊ณ  ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๋นˆ์นธ์„ ์ฑ„์šฐ์„ธ์š”. import calendar c = calendar.TextCalendar() m = c. โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ(2021, 2) print(m) ๊ฒฐ๊ณผ: February 2021 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ํžŒํŠธ >>> help(c) 2. tkinter ๋‹ค์Œ์€ tkinter๋ฅผ ํ™œ์šฉํ•ด ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์ฐฝ์„ ๋„์šฐ๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๋นˆ์นธ์„ ์ฑ„์šฐ์„ธ์š”. import tkinter as tk s = "Life is short\nUse Python" root = tk.Tk() t = tk.Text(root, height=โ–ˆ, width=โ–ˆโ–ˆ) t.insert(tk.END, โ–ˆ) t.pack() tk.mainloop() ๊ฒฐ๊ณผ: 3. calendar์™€ tkinter ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 2021๋…„ 3์›”์˜ ๋‹ฌ๋ ฅ์„ ํ‘œ์‹œํ•˜๋Š” ์ฐฝ์„ ๋„์šฐ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ๊ฒฐ๊ณผ: ch05/month2str.py ch05/life_is_short.py ch05/tk_calendar.py 5.2.4 ์—ฐ์Šต ๋ฌธ์ œ: ๋†€์ด๊ณต์› (2) ๋‘˜๋ฆฌ, ๋„์šฐ๋„ˆ, ๋งˆ์ด์ฝœ์ด ๋†€์ด๊ณต์›์—์„œ ํ•จ๊ป˜ ๋†€์ด ๊ธฐ๊ตฌ๋ฅผ ํƒ€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์„ธ ๋ช…์˜ ํ‚ค๊ฐ€ ๋‹ฌ๋ผ์„œ, ๊ฐ์ž ํƒˆ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๊ตฌ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋ฌธ์ œ ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์ „์— ๋†€์ด๊ณต์› (1) ๋ฌธ์ œ๋ฅผ ๋จผ์ € ํ’€์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋†€์ด ๊ธฐ๊ตฌ์˜ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ํ‚ค(์‹ ์žฅ)๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, ํƒˆ ์ˆ˜ ์žˆ๋Š” ๋†€์ด ๊ธฐ๊ตฌ ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์™€์ผ๋“œ ์œ™: 110cm ์ด์ƒ ๋“œ๋ฆผ ๋ณดํŠธ: 120cm ์ด์ƒ ์ž์ด์–ธํŠธ ๋ฃจํ”„: 120cm ์ด์ƒ ํˆผ ์˜ค๋ธŒ ํ˜ธ๋Ÿฌ: - ํ”Œ๋ผ์ด๋ฒค์ฒ˜: 140cm~195cm ํšŒ์ „๋ชฉ๋งˆ: 100cm ์ด์ƒ ๋งค์ง ๋ถ•๋ถ•์นด: 110cm~140cm ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉฐ, ch05 ํด๋”์— park.py๋ผ๋Š” ํŒŒ์ผ๋ช…์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. import sys sys.path.append("../ch03") import # ์ด๊ณณ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. rides = # ์ด๊ณณ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. def allowedrides(height): assert type(height) == int result = [] # ์ด๊ณณ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. return result if __name__ == "__main__": height = int(input()) # ์ด๊ณณ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์ž…์ถœ๋ ฅ ์˜ˆ ์˜ˆ 1 ๋‘˜๋ฆฌ์˜ ํ‚ค๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. 120 ์ถœ๋ ฅ: ์™€์ผ๋“œ ์œ™ ๋“œ๋ฆผ ๋ณดํŠธ ์ž์ด์–ธํŠธ ๋ฃจํ”„ ํˆผ ์˜ค๋ธŒ ํ˜ธ๋Ÿฌ ํšŒ์ „๋ชฉ๋งˆ ๋งค์ง ๋ถ•๋ถ•์นด ์˜ˆ 2 ๋„์šฐ๋„ˆ์˜ ํ‚ค๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. 110 ์ถœ๋ ฅ: ์™€์ผ๋“œ ์œ™ ํˆผ ์˜ค๋ธŒ ํ˜ธ๋Ÿฌ ํšŒ์ „๋ชฉ๋งˆ ๋งค์ง ๋ถ•๋ถ•์นด ์˜ˆ 3 ๋งˆ์ด์ฝœ์˜ ํ‚ค๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. 179 ์ถœ๋ ฅ: ์™€์ผ๋“œ ์œ™ ๋“œ๋ฆผ ๋ณดํŠธ ์ž์ด์–ธํŠธ ๋ฃจํ”„ ํˆผ ์˜ค๋ธŒ ํ˜ธ๋Ÿฌ ํ”Œ๋ผ์ด๋ฒค์ฒ˜ ํšŒ์ „๋ชฉ๋งˆ ch05/park.py 5.2.5 ์—ฐ์Šต ๋ฌธ์ œ: ๋†€์ด๊ณต์› (3) ๋ฌธ์ œ ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์ „์— ๋†€์ด๊ณต์› (1)๊ณผ ๋†€์ด๊ณต์› (2) ๋ฌธ์ œ๋ฅผ ๋จผ์ € ํ’€์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋†€์ด ๊ธฐ๊ตฌ ์ค‘์—์„œ ๋‘˜๋ฆฌ, ๋„์šฐ๋„ˆ, ๋งˆ์ด์ฝœ์ด ๋ชจ๋‘ ํƒˆ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ ์ž…๋ ฅ: 120 110 179 ์ถœ๋ ฅ: ์™€์ผ๋“œ ์œ™ ํˆผ ์˜ค๋ธŒ ํ˜ธ๋Ÿฌ ํšŒ์ „๋ชฉ๋งˆ ch05/together.py 5.3์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ชจ๋“ˆ ์ด๋ฒˆ ์‹œ๊ฐ„๋„ ๊ณ„์†ํ•ด์„œ ๋ชจ๋“ˆ์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ๊นŒ์š”? ํŒŒ์ด์ฌ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์ˆ˜๋งŽ์€ ๋ชจ๋“ˆ ์ค‘์—์„œ ์ž์ฃผ ์“ฐ์ด๋Š” ๊ฒƒ๋“ค์„ ์ด๋ฒˆ ์‹œ๊ฐ„์— ์‚ด์ง ์†Œ๊ฐœํ•ด ๋“œ๋ฆฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. sys ์ฒ˜์Œ์œผ๋กœ ์•Œ๋ ค๋“œ๋ฆด ๊ฒƒ์€ sys ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค. ์š”๋†ˆ์€ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜์ง€์š”. ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ๋„์›Œ์ฃผ์„ธ์š”. ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์šฐ๋ฆฌ์˜ ๋ช…๋ น์„ ๊ธฐ๋‹ค๋ฆฐ๋‹ค๋Š” ๋œป์œผ๋กœ >>>๋ฅผ ํ‘œ์‹œํ•˜๊ณ  ์žˆ์ฃ ? ๋„์Šค์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๊ฒƒ๋„ ํ”„๋กฌํ”„ํŠธ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. sys ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜๊ฐ€ ์žˆ์ง€์š”. >>> import sys >>> sys.ps1 # ํ˜„์žฌ์˜ ํ”„๋กฌํ”„ํŠธ๋Š”? '>>> ' >>> sys.ps1 = '^^; ' # ์š”๊ฑธ๋กœ ๋ฐ”๊ฟ”! ^^; print('hello') hello ^^; 5 * 3 15 ^^; ์žฌ๋ฏธ์žˆ์ง€์š”? ์ด๋ฒˆ์—” ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ๋๋‚ด๋ณผ๊นŒ์š”? ^^; sys.exit() os ๊ทธ๋‹ค์Œ์—๋Š” os ๋ชจ๋“ˆ์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์šด์˜์ฒด์ œ(OS : Operating System)๋ฅผ ์ œ์–ดํ•  ์ˆ˜๊ฐ€ ์žˆ์ง€์š”. ํŒŒ์ด์ฌ์„ ์ƒˆ๋กœ ์‹คํ–‰ํ•˜๊ณ , ํ˜„์žฌ ๊ฒฝ๋กœ๋ฅผ ์•Œ์•„๋ณผ๊ฒŒ์š”. Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.getcwd() 'C:\\Users\\Yong Choi\\AppData\\Local\\Programs\\Python\\Python38-32' os ๋ชจ๋“ˆ์˜ getcwd()๋Š” ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๊ตฌํ•ด๋ผ(get)! ๋ฌด์—‡์„? ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ(current working directory)๋ฅผ~ ์ด๊ณณ์— ์–ด๋–ค ํŒŒ์ผ๋“ค์ด ์žˆ๋Š”์ง€ ์•Œ์•„๋ณผ๊นŒ์š”? ์ด๊ณณ์— ์–ด๋–ค ํŒŒ์ผ๋“ค์ด ์žˆ๋Š”์ง€ ์•Œ์•„๋ณผ๊นŒ์š”? >>> os.listdir() ['DLLs', 'Doc', 'img_read.py', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python38.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll'] ์ด ์ค‘์—์„œ libs ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ์ด๋™ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> os.chdir('libs') >>> os.getcwd() 'C:\\Users\\Yong Choi\\AppData\\Local\\Programs\\Python\\Python38-32\\libs' ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ๋ฐ”๋€Œ์—ˆ์ฃ ? ํŒŒ์ผ ๋ชฉ๋ก๋„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> os.listdir() ['python3.lib', 'python38.lib', '_tkinter.lib'] ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ๋Š” ..์œผ๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. >>> os.chdir('..') >>> os.getcwd() 'C:\\Users\\Yong Choi\\AppData\\Local\\Programs\\Python\\Python38-32' re ์ •๊ทœ ํ‘œํ˜„์‹(regular expression)์„ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” re ๋ชจ๋“ˆ๋„ ์žˆ์–ด์š”. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ ๋‘ ๋ฒˆ์งธ ์ค„์˜ ๊ด„ํ˜ธ ์•ˆ์— ์“ด ๊ฒƒ์ด ์ •๊ทœ ํ‘œํ˜„์‹์ธ๋ฐ์š”, ๋งˆ์นจํ‘œ(.)๋Š” ๋ฌธ์ž ์•„๋ฌด๊ฑฐ๋‚˜ ํ•œ ๊ฐœ๋ฅผ ๋œปํ•˜๊ณ , ๋ณ„ํ‘œ(*)๋Š” 0๊ฐœ ์ด์ƒ์˜ ๋ฌธ์ž๋ฅผ ๋œปํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ p ๋‹ค์Œ์— n์ด ๋‚˜์˜ค๋Š” ์ด๋ฆ„์„ ๊ฐ–๊ณ  ์žˆ๋Š” ํŒŒ์ผ๋“ค์„ ๋ชจ๋‘ ์ฐพ์•„์ฃผ๊ฒŒ ๋˜์ง€์š”. ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ž˜ ๋ณด์‹œ๋ฉด ์ดํ•ด๊ฐ€ ๋˜์‹ค ๊ฑฐ์˜ˆ์š”. >>> import re, glob >>> p = re.compile('.*p.*n.*') >>> for i in glob.glob('*'): ... m = p.match(i) ... if m: ... print(m.group()) ... pycon.ico python.exe pythonw.exe w9xpopen.exe ์ด๋Ÿฐ ๊ฒƒ๋“ค ์™ธ์— ์ฒ˜์Œ์— ๋ชจ๋“ˆ์— ๋Œ€ํ•ด ์„ค๋ช…๋“œ๋ฆด ๋•Œ ๋ณด์—ฌ๋“œ๋ฆฐ math๋‚˜ tkinter๋„ ์ž์ฃผ ์“ฐ์‹ค ๋ฒ•ํ•˜๋„ค์š”. ์ง€๊ธˆ๊นŒ์ง€ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋ณด์—ฌ๋“œ๋ ธ๋Š”๋ฐ ๋ชจ๋“ˆ๋“ค์ด ์ฐธ ์“ธ๋งŒํ•˜์ฃ ? ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ์„ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ์ข‹์€ ํ”„๋กœ๊ทธ๋žจ์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”. ํ•˜์ง€๋งŒ ์ˆ˜๋งŽ์€ ๋ชจ๋“ˆ์˜ ์‚ฌ์šฉ๋ฒ•์„ ๋ชจ๋‘ ๋จธ๋ฆฌ์— ์ง‘์–ด๋„ฃ์œผ์‹ค ํ•„์š”๋Š” ์—†๊ฒ ์ฃ ? ์ž‘์„ฑํ•˜์‹ค ํ”„๋กœ๊ทธ๋žจ์—์„œ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ํ•„์š”๋กœ ํ•˜๋Š”๊ฐ€์— ๋”ฐ๋ผ ์–ด๋–ค ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •ํ•œ ๋‹ค์Œ, ์‚ฌ์šฉ์„ค๋ช…์„œ๋ฅผ ๋ณด๋ฉด์„œ ๋ชจ๋“ˆ์˜ ์‚ฌ์šฉ๋ฒ•์„ ์ตํ˜€์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋“ˆ์˜ ์‚ฌ์šฉ์„ค๋ช…์„œ๋กœ๋Š” ํŒŒ์ด์ฌ๊ณผ ํ•จ๊ป˜ ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์น˜๋˜๋Š” 'Python Library Reference(ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ ˆํผ๋Ÿฐ์Šค)'๋ผ๋Š” ๊ฒƒ๋„ ์žˆ๊ณ , ์ฑ…์ด๋‚˜ ์ธํ„ฐ๋„ท์„ ํ†ตํ•ด ์ž๋ฃŒ๋ฅผ ์ฐพ์•„๋ณผ ์ˆ˜๋„ ์žˆ์ง€์š”. webbrowser ๋์œผ๋กœ ์žฌ๋ฏธ์žˆ๋Š” ๋ชจ๋“ˆ์„ ํ•˜๋‚˜ ๋” ์†Œ๊ฐœํ•ด ๋“œ๋ฆด๊ฒŒ์š”. ํ•œ๋ฒˆ ๋”ฐ๋ผ ํ•ด๋ณด์„ธ์š”. ๊ทธ๋Ÿผ ์ „ ์ด๋งŒโ€ฆ ํœ˜๋ฆฌ๋ฆญ~ >>> import webbrowser >>> url='http://www.python.org/' >>> webbrowser.open(url) True >>> 5.3.1 ๋žœ๋ค(random) ๋ชจ๋“ˆ ์ด๋ฒˆ์—๋Š” ํŒŒ์ด์ฌ์—์„œ์˜ ๋žœ๋ค(random)์— ๋Œ€ํ•ด ๊ฐ€๋ณ๊ฒŒ ์ •๋ฆฌํ•ด ๋ณผ๊นŒ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋žœ๋ค์ด ๋ฌด์—‡์ธ์ง€๋ถ€ํ„ฐ ์‚ดํŽด๋ณผ๊นŒ์š”? ์ฃผ์‚ฌ์œ„๋ฅผ ๋˜์ง€๋Š” ์ƒํ™ฉ์„ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ์ฃผ์‚ฌ์œ„์˜ ๊ฐ ๋ฉด์—๋Š” 1๊ฐœ์—์„œ 6๊ฐœ๊นŒ์ง€์˜ ๋ˆˆ์ด ์ƒˆ๊ฒจ์ ธ ์žˆ์–ด์„œ, ์ฃผ์‚ฌ์œ„๋ฅผ ๋˜์งˆ ๋•Œ๋งˆ๋‹ค ๊ทธ์ค‘ ํ•˜๋‚˜์˜ ์ˆซ์ž๊ฐ€ ์„ ํƒ๋ฉ๋‹ˆ๋‹ค. ์ฃผ์‚ฌ์œ„๋ฅผ ์ง์ ‘ ๋˜์ ธ๋ณด๊ธฐ ์ „์—๋Š” ๋‹ค์Œ๋ฒˆ์— ์–ด๋–ค ์ˆซ์ž๊ฐ€ ๋‚˜์˜ฌ์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†์ฃ . ๊ทธ๋Ÿฐ๋ฐ ์ฃผ์‚ฌ์œ„๋ฅผ 600๋ฒˆ ์ •๋„ ๋˜์ ธ๋ณด๋ฉด ๊ฐ ์ˆซ์ž๊ฐ€ ๋Œ€๋žต 100๋ฒˆ ์ •๋„๋Š” ๋‚˜์˜ค๊ธฐ๋Š” ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์ด ๋ฐ”๋กœ ๋‚œ์ˆ˜(random number)์ž…๋‹ˆ๋‹ค. ๋‚œ์ˆ˜์˜ ์˜ˆ๊ฐ€ ๋  ๋งŒํ•œ ๊ฒƒ์œผ๋กœ ์ฃผ์‚ฌ์œ„ ์™ธ์— ๋˜ ์–ด๋–ค ๊ฒƒ๋“ค์ด ์žˆ์„๊นŒ์š”? ๋ณต๊ถŒ ์ถ”์ฒจ, ์Œ์•… ์žฌ์ƒ ์ˆœ์„œ ์„ž๊ธฐ... ๊ทธ๋Ÿผ ํŒŒ์ด์ฌ์œผ๋กœ ๋‚œ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. >>> import random >>> random.random() 0.90389642027948769 random ๋ชจ๋“ˆ์˜ random() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ๋”๋‹ˆ ๋ณต์žกํ•œ ์ˆซ์ž๋ฅผ ๋Œ๋ ค์ฃผ๋„ค์š”. random() ํ•จ์ˆ˜๋Š” 0 ์ด์ƒ 1 ๋ฏธ๋งŒ์˜ ์ˆซ์ž ์ค‘์—์„œ ์•„๋ฌด ์ˆซ์ž๋‚˜ ํ•˜๋‚˜ ๋ฝ‘์•„์„œ ๋Œ๋ ค์ฃผ๋Š” ์ผ์„ ํ•œ๋‹ต๋‹ˆ๋‹ค. ์ฃผ์‚ฌ์œ„์ฒ˜๋Ÿผ 1์—์„œ 6๊นŒ์ง€์˜ ์ •์ˆ˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ๋ฌด์ž‘์œ„๋กœ ์–ป์œผ๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ์ด๋Ÿด ๋•Œ ํŽธ๋ฆฌํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋Š” randrange()๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. >>> random.randrange(1,7) >>> random.randrange(1,7) ์—ฌ๊ธฐ์—์„œ randrange(1,6)์ด ์•„๋‹ˆ๋ผ randrange(1,7)์ด๋ผ๊ณ  ์ผ๋‹ค๋Š” ์ ์— ์ฃผ์˜ํ•˜์„ธ์š”. "1 ์ด์ƒ 7 ๋ฏธ๋งŒ์˜ ๋‚œ์ˆ˜"๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ์ดํ•ด๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค. ๋‚ด์žฅํ•จ ์ˆ˜์ธ range()๋ฅผ ๋˜์ƒˆ๊ฒจ๋ณด๋Š” ๊ฒƒ๋„ ์ข‹๊ฒ ๊ตฐ์š”. >>> range(1,7) [1, 2, 3, 4, 5, 6] shuffle()์ด๋ผ๋Š” ์žฌ๋ฏธ์žˆ๋Š” ํ•จ์ˆ˜๋„ ์žˆ๊ตฐ์š”. ์‹œํ€€์Šค๋ฅผ ๋’ค์ฃฝ๋ฐ•์ฃฝ์œผ๋กœ ์„ž์–ด๋†“๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. >>> abc = ['a', 'b', 'c', 'd', 'e'] >>> random.shuffle(abc) >>> abc ['a', 'd', 'e', 'b', 'c'] >>> random.shuffle(abc) >>> abc ['e', 'd', 'a', 'c', 'b'] ์•„๋ฌด ์›์†Œ๋‚˜ ํ•˜๋‚˜ ๋ฝ‘์•„์ฃผ๋Š” choice() ํ•จ์ˆ˜๋„ ์žˆ๋„ค์š”. >>> abc ['e', 'd', 'a', 'c', 'b'] >>> random.choice(abc) 'a' >>> random.choice(abc) 'd' >>> menu = '์ซ„๋ฉด', '์œก๊ฐœ์žฅ', '๋น„๋น”๋ฐฅ' >>> random.choice(menu) '์ซ„๋ฉด' ์ฐธ๊ณผ ๊ฑฐ์ง“ ์ค‘์— ํ•˜๋‚˜๋ฅผ ๋ฝ‘๊ณ  ์‹ถ๋‹ค๋ฉด, ๋ญ.. ๊นŒ์ง“๊ฒƒ... ์ด๋ ‡๊ฒŒ ํ•ด์ฃผ์ฃ ... >>> random.choice([True, False]) True >>> random.choice([True, False]) False 5.3.2 ์—ฐ์Šต ๋ฌธ์ œ: ๋ฐด๋“œ ์ด๋ฆ„ ์ง“๊ธฐ (1) ๋ฌธ์ œ ์ƒ‰๊น” ์ด๋ฆ„๊ณผ ์Œ์‹ ์ด๋ฆ„์„ ๋”ํ•˜๋ฉด ๋ฐด๋“œ ์ด๋ฆ„ ๊ฐ™๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.1 ์ƒ‰์ด๋ฆ„๊ณผ ์Œ์‹ ์ด๋ฆ„์„ ๋ฌด์ž‘์œ„๋กœ ๋ฝ‘์•„์„œ ๋ฐด๋“œ ์ด๋ฆ„์„ ์ง€์–ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. $ python bandname1.py ๊ฒ€์€ ์ฝฐ์‚ญ์นฉ $ python bandname1.py ํ™”์ดํŠธ ๊ณฐ๋ณด๋นต $ python bandname1.py ํŒŒ๋ž€ ์ฃผ๊พธ๋ฏธ $ python bandname1.py ์ฒญ์ƒ‰ ์ปคํ”ผ $ python bandname1.py ํŒŒ๋ž€ ์˜น์‹ฌ์ด ์ฝ”๋“œ: ch05/bandname1.py ์ง€๊ธˆ ์ž…๊ณ  ์žˆ๋Š” ํ•˜์˜ ์ƒ‰๊ณผ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋จน์€ ์Œ์‹ ์ด๋ฆ„์„ ๋”ํ•˜๋ฉด ๋ฐด๋“œ ์ด๋ฆ„ ๊ฐ™๋‹ค โ†ฉ 5.3.3 string๊ณผ random ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ ์—ฌ๋Ÿฌ ์ธํ„ฐ๋„ท ์‚ฌ์ดํŠธ์™€ ์•ฑ์„ ์ด์šฉํ•˜๋‹ค ๋ณด๋ฉด ๋น„๋ฐ€๋ฒˆํ˜ธ(password)๋ฅผ ๋งŒ๋“ค๊ณ  ์ž…๋ ฅํ•  ์ผ์ด ๋งŽ์ฃ . ์—ฌ๋Ÿฌ๋ถ„์€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌํ•˜์‹œ๋‚˜์š”? ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ†ต์ผํ•˜๋ฉด ๊ด€๋ฆฌํ•˜๊ธฐ ํŽธํ•˜๊ฒ ์ง€๋งŒ, ๊ณต๊ฒฉ์ž๊ฐ€ ํ•œ ๊ณณ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋งŒ ์•Œ์•„๋‚ด๋ฉด ๋‹ค๋ฅธ ๊ณณ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋„ ๋ชจ๋‘ ์•Œ๊ฒŒ ๋˜๋ฏ€๋กœ, ์‚ฌ์ดํŠธ๋งˆ๋‹ค ๋‹ค๋ฅธ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋˜, ์‚ฌ์ดํŠธ๋งˆ๋‹ค ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅด๋”๋ผ๋„ ์ผ์ •ํ•œ ๊ทœ์น™์— ๋”ฐ๋ผ ๋งŒ๋“ค์–ด์ ธ ์žˆ์œผ๋ฉด ๊ณต๊ฒฉ์ž๊ฐ€ ์œ ์ถ”ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๊ณ ์š”. ๋‹คํ–‰ํžˆ ์š”์ฆ˜์—๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ธฐ์–ตํ•ด ์ฃผ๋Š” ๊ธฐ๋Šฅ์ด ์›น ๋ธŒ๋ผ์šฐ์ €์™€ ์Šค๋งˆํŠธํฐ์— ์žˆ์–ด์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ด€๋ฆฌ๊ฐ€<NAME> ์ˆ˜์›”ํ•ด์กŒ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํŒŒ์ด์ฌ์œผ๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. string ๋ชจ๋“ˆ ๋จผ์ € ์žฌ๋ฃŒ๋ฅผ ์ค€๋น„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์˜๋ฌธ์ž์™€ ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์„ž์–ด์„œ ๋งŒ๋“ค์ฃ . ์˜๋ฌธ์ž๋ถ€ํ„ฐ ํ•ด๋ณผ๊ฒŒ์š”. "ABCD..."๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ํƒ€์žํ•ด๋„ ๋˜๊ฒ ์ง€๋งŒ, string ๋ชจ๋“ˆ์„ ์ด์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. string ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•ด์ฃผ์„ธ์š”. >>> import string string.ascii_uppercase์—๋Š” ๋Œ€๋ฌธ์ž A๋ถ€ํ„ฐ Z๊นŒ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. >>> string.ascii_uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ string.ascii_lowercase์—๋Š” ์˜๋ฌธ ์†Œ๋ฌธ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. >>> string.ascii_lowercase 'abcdefghijklmnopqrstuvwxyz' ์ฐธ, ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ๊ฐ–๊ณ  ์žˆ๋Š” string.ascii_letters๋„ ์žˆ์–ด์š”. >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ์ด๋ฒˆ์—๋Š” ์ˆซ์ž๋ฅผ ์ค€๋น„ํ•ฉ์‹œ๋‹ค. string.digits๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. >>> string.digits '0123456789' ์˜๋ฌธ์ž์™€ ์ˆซ์ž๋ฅผ ํ•œ๋ฐ ๋ชจ์•„ alphanumeric ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค. >>> alphanumeric = string.ascii_letters + string.digits >>> alphanumeric 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ๋ช‡ ๊ธ€์ž์ธ์ง€ ์„ธ์–ด๋ณผ๊นŒ์š”? >>> len(alphanumeric) 62 ์†Œ๋ฌธ์ž ์—˜(l)๊ณผ ๋Œ€๋ฌธ์ž ์•„์ด(I)๊ฐ€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒจ์„œ ํ—ท๊ฐˆ๋ฆฌ๋‹ˆ๊นŒ ๊ทธ ๋‘ ๊ฐœ๋Š” ๋นผ๋Š” ๊ฒŒ ์ข‹๊ฒ ์–ด์š”. ๊ทธ๋ฆฌ๊ณ  ์ˆซ์ž 0๊ณผ ๋Œ€๋ฌธ์ž ์˜ค(O)๋„ ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋นผ๊ณ ์š”. ์ด๋Ÿด ๋•Œ ์„ธํŠธ๋ฅผ ์“ฐ๋ฉด ์ข‹๊ฒ ๋„ค์š”. >>> list(set(alphanumeric) - set('lIO0')) ['L', 'k', 'g', 'j', 'T', '5', 'Z', 'a', 'M', 'w', 'D', 'P', 'n', 'f', 'x', 'd', 'q', 'K', 'J', 'X', '4', 'b', 'F', 'Y', 'A', 'v', 'r', 'i', 'o', '7', 'y', 'S', '1', 'u', 'W', 'V', 'R', 'G', 'h', 'c', 'N', 'U', '2', '6', 'C', 'B', 'e', 'p', 's', 'z', 'H', '9', 't', '8', 'E', 'm', '3', 'Q'] ํŠน์ˆ˜๋ฌธ์ž๋Š” ๋ฐ‘์ค„๋งŒ ์“ฐ๋Š” ๊ฑธ๋กœ ํ• ๊นŒ์š”? >>> chars = list(set(alphanumeric) - set('lIO0')) + ['_'] ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋งŒ๋“ค ๋•Œ ์“ฐ๊ณ  ์‹ถ์€ ๊ธ€์ž 59๊ฐœ๋กœ ์ด๋ค„์ง„ chars๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ค€๋น„๋์Šต๋‹ˆ๋‹ค. >>> len(chars) 59 ๋ฌธ์ž์—ด ์„ž๊ธฐ ์žฌ๋ฃŒ๋ฅผ ์ค€๋น„ํ–ˆ์œผ๋‹ˆ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธ€์ž๋ฅผ ๋ฌด์ž‘์œ„๋กœ ๊ณ ๋ฅด๋ ค๋ฉด ์•ž์—์„œ ๋ฐฐ์šด random ๋ชจ๋“ˆ์„ ์“ฐ๋ฉด ๋˜๊ฒ ์ฃ ? >>> import random ์•„๋ฌด ๊ธ€์ž๋‚˜ ํ•˜๋‚˜ ๋ฝ‘์•„๋ณผ๊ฒŒ์š”. >>> random.choice(chars) 'Y' ๋˜ ๋ฝ‘์•„๋ณผ๊ฒŒ์š”. >>> random.choice(chars) 'Q' >>> random.choice(chars) 'V' >>> random.choice(chars) 'x' ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ฐ˜๋ณตํ•ด์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋งŒ๋“ค๋ฉด ๋˜๊ฒ ๊ตฐ์š”. >>> pw = str() >>> for i in range(16): ... pw += random.choice(chars) ... >>> pw 'yTjFDnEaLvE8S2uo' 16์ž๋กœ ๋œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋งŒ๋“ค์–ด๋ดค์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฐ‘์ค„์€ ๋“ค์–ด์žˆ์ง€ ์•Š๋„ค์š”. ๊ทธ๋„ ๊ทธ๋Ÿด ๊ฒƒ์ด, ๋ฌด์ž‘์œ„๋กœ ๋ฝ‘์œผ๋ฉด ๊ฐ ๊ธ€์ž๊ฐ€ 1/59 ํ™•๋ฅ ๋กœ ๋‚˜์˜ฌ ํ…Œ๋‹ˆ ๋ฐ‘์ค„์ด ๋‚˜์˜ค๊ธฐ ํž˜๋“ค ๊ฒƒ ๊ฐ™๋„ค์š”. ์˜๋ฌธ์ž, ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ทœ์น™์ด ์žˆ๋‹ค๋ฉด ์ข€ ๋” ๋จธ๋ฆฌ๋ฅผ ์จ์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋˜‘๋˜‘ํ•œ ์—ฌ๋Ÿฌ๋ถ„์ด ํ•œ๋ฒˆ ๋„์ „ํ•ด ๋ณด์„ธ์š”. ์ €๋Š” ๊ณง ์น˜ํ‚จ ๋ฐฐ๋‹ฌ์ด ์™€์„œ ์ด๋งŒโ€ฆ. P.S. ์ €๋Š” ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด๋ดค์Šต๋‹ˆ๋‹ค. ch05/gen_password.py 5.3.4 ์‹œ์ €(์นด์ด์‚ฌ๋ฅด) ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ ๋กœ๋งˆ์˜ ์œจ๋ฆฌ์šฐ์Šค ์นด์ด์‚ฌ๋ฅด๊ฐ€ ์‚ฌ์šฉํ–ˆ๋‹ค๊ณ  ํ•ด์„œ โ€˜์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธโ€™ ๋˜๋Š” โ€˜์‹œ์ € ์•”ํ˜ธโ€™๋กœ ์•Œ๋ ค์ง„ ๊ฒƒ์„ ํŒŒ์ด์ฌ์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹œ์ € ์•”ํ˜ธ์˜ ์›๋ฆฌ ์นด์ด์‚ฌ๋ฅด ์•”ํ˜ธ์—์„œ๋Š” ์•ŒํŒŒ๋ฒณ์˜ ๊ฐ ๊ธ€์ž๋ฅผ ๋‹ค๋ฅธ ๊ธ€์ž๋กœ ๋ฐ”๊พธ๋Š” ํ‘œ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋‹ค์Œ ํ‘œ์˜ ์œ„ ์ค„์€ ์†Œ๋ฌธ์ž๋ฅผ a๋ถ€ํ„ฐ ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ๋Š˜์–ด๋†“์€ ๊ฒƒ์ด๊ณ , ์•„๋ž˜ ์ค„์€ ๋Œ€๋ฌธ์ž ์ค‘ ๋„ค ๋ฒˆ์งธ ๊ธ€์ž์ธ D๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ๋Š˜์–ด๋†“์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ์ค„์˜ Z ๋‹ค์Œ์—๋Š” ๋‹ค์‹œ A๊ฐ€ ์˜ต๋‹ˆ๋‹ค. ์•”ํ˜ธํ™”ํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์žฅ์˜ ๊ฐ ๊ธ€์ž์— ๋Œ€ํ•ด, ํ‘œ์˜ ์œ„ ์ค„์— ์žˆ๋Š” ๊ธ€์ž๋ฅผ ์•„๋ž˜ ์ค„์— ์žˆ๋Š” ๊ธ€์ž๋กœ ๋ฐ”๊พธ๋ฉด ์•”ํ˜ธ๋ฌธ์ด ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ํŽธ์˜์ƒ ํ‰๋ฌธ์€ ์†Œ๋ฌธ์ž๋กœ, ์•”ํ˜ธ๋ฌธ์€ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ์‹œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ‰๋ฌธ(โ€˜์ ˆ๋Œ€๋กœ ๋ธŒ๋ฃจํˆฌ์Šค๋ฅผ ์‹ ๋ขฐํ•˜์ง€ ๋งˆ๋ผโ€™๋ผ๋Š” ๋œป): traue nie dem brutus ์•”ํ˜ธ๋ฌธ: WUDXH QLH GHP EUXWXV ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ๊ณผ ๋ฐ›๋Š” ์‚ฌ๋žŒ๋งŒ ์ด ๊ทœ์น™์„ ์•Œ๊ณ  ์žˆ์œผ๋ฉด, ์ „๋ น์ด ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋„์ค‘์— ์ ์ด ๊ฐ€๋กœ์ฑ„๋”๋ผ๋„ ๋œป์„ ์•Œ๊ธฐ ์–ด๋ ต๊ฒ ์ฃ ? ๋˜‘๋˜‘ํ•œ ์—ฌ๋Ÿฌ๋ถ„์ด๋ผ๋ฉด ๊ธˆ์„ธ ์•Œ์•„์ฑŒ์ง€๋„ ๋ชจ๋ฅด๊ฒ ๋„ค์š”. ์•”ํ˜ธํ‘œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ๊ฐœ์˜ ์›ํŒ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ˆ์ชฝ ์›ํŒ์„ ๋Œ๋ ค์„œ ์•”ํ˜ธ ๊ทœ์น™์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ฒ ์ฃ ? ํŒŒ์ด์ฌ์œผ๋กœ ์‹œ์ € ์•”ํ˜ธ ๊ตฌํ˜„ ๋จผ์ € ์•”ํ˜ธํ‘œ์˜ ์œ„ ์ค„(๋ฐ”๊นฅ์ชฝ ์›ํŒ)์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด a๋ถ€ํ„ฐ z๊นŒ์ง€์˜ ์˜๋ฌธ ์†Œ๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์„ ์ฐจ๋ก€๋Œ€๋กœ ๋Š˜์–ด๋†“์Šต๋‹ˆ๋‹ค. ์ง์ ‘ ํƒ€์žํ•ด๋„ ๋˜์ง€๋งŒ string ๋ชจ๋“ˆ์„ ์ด์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. >>> import string >>> string.ascii_lowercase 'abcdefghijklmnopqrstuvwxyz' ์ด๋ฒˆ์—๋Š” ์•”ํ˜ธํ‘œ์˜ ์•„๋ž˜ ์ค„(์•ˆ์ชฝ ์›ํŒ)์— ํ•ด๋‹นํ•˜๋Š” ์˜๋ฌธ ๋Œ€๋ฌธ์ž D~Z, A~C์ž…๋‹ˆ๋‹ค. >>> string.ascii_uppercase[3:] + string.ascii_uppercase[:3] 'DEFGHIJKLMNOPQRSTUVWXYZABC' ์œ„ ์ค„๊ณผ ์•„๋ž˜ ์ค„์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž์—ด๋“ค์„ ์ค€๋น„ํ–ˆ์œผ๋‹ˆ, ๊ฐ ๊ธ€์ž๋ฅผ ๋Œ€์‘์‹œํ‚ค๋Š” ์•”ํ˜ธํ‘œ์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ณต๋ฌธ์„ ์จ์„œ ์ง์ ‘ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ, ํŒŒ์ด์ฌ์—์„œ๋Š” str ๊ฐ์ฒด์˜ maketrans() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (โ€˜๋ฉ”์„œ๋“œโ€™์— ๊ด€ํ•ด์„œ๋Š” 7์žฅ์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ๊ทธ๋ƒฅ ํ•จ์ˆ˜์™€ ๋น„์Šทํ•œ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.) >>> tt = str.maketrans(string.ascii_lowercase, string.ascii_uppercase[3:] + string.ascii_uppercase[:3]) str.maketrans()์˜ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ์— tt๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ, ์•”ํ˜ธํ™”ํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์ž์—ด์„ translate() ๋ฉ”์„œ๋“œ๋ฅผ ์จ์„œ ์•”ํ˜ธํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์•ž์—์„œ ๋งŒ๋“  tt๋ฅผ ์ธ์ž๋กœ ์ค๋‹ˆ๋‹ค. >>> 'traue nie dem brutus'.translate(tt) 'WUDXH QLH GHP EUXWXV' ์ฐธ ์‰ฝ์ฃ ?! ์ฐธ๊ณ  ๋ฃจ๋Œํ”„ ํ‚คํŽœํ•œ ์ง€์Œ / ์ด์ผ์šฐ ์˜ฎ๊น€, ใ€Š์•”ํ˜ธ์˜ ํ•ด์„ใ€‹ 5.3.5. ์—ฐ์Šต ๋ฌธ์ œ: ๋๋ง์ž‡๊ธฐ (2) ๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๊ทœ์น™์€ ๋๋ง์ž‡๊ธฐ (1)๊ณผ ๊ฐ™๋˜, ๋‘์Œ ๋ฒ•์น™์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ 1~3์€ ๋๋ง์ž‡๊ธฐ (1)์—์„œ์™€ ๊ฐ™๊ณ , ์˜ˆ 4๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ 1 ํ„ด ์ž…์ถœ๋ ฅ ์ปดํ“จํ„ฐ <์‹œ์ž‘>๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜์ž. ๋‚ด๊ฐ€ ๋จผ์ € ๋งํ• ๊ฒŒ. ๊ธฐ์ฐจ ํ”Œ๋ ˆ์ด์–ด ์ž๋™์ฐจ ์ปดํ“จํ„ฐ ๊ธ€์ž๊ฐ€ ์•ˆ ์ด์–ด์ ธ. ๋‚ด๊ฐ€ ์ด๊ฒผ๋‹ค!<๋> ์˜ˆ 2 ํ„ด ์ž…์ถœ๋ ฅ ์ปดํ“จํ„ฐ <์‹œ์ž‘>๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜์ž. ๋‚ด๊ฐ€ ๋จผ์ € ๋งํ• ๊ฒŒ. ๊ธฐ์ฐจ ํ”Œ๋ ˆ์ด์–ด ์ฐจ ๋ฐ• ์ปดํ“จํ„ฐ ๋ฐ•์ฅ ํ”Œ๋ ˆ์ด์–ด ์ฅ๊ตฌ๋ฉ ์ปดํ“จํ„ฐ ๋ฉ๊ฒŒ ํ”Œ๋ ˆ์ด์–ด ๊ฒŒ์‹œํŒ ์ปดํ“จํ„ฐ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๋‚ด๊ฐ€ ์กŒ์–ด.<๋> ์˜ˆ 3 ํ„ด ์ž…์ถœ๋ ฅ ์ปดํ“จํ„ฐ <์‹œ์ž‘>๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜์ž. ๋‚ด๊ฐ€ ๋จผ์ € ๋งํ• ๊ฒŒ. ๊ธฐ์ฐจ ํ”Œ๋ ˆ์ด์–ด ์ฐจ์ฃผ ์ปดํ“จํ„ฐ ์ฃผ๊ธฐ ํ”Œ๋ ˆ์ด์–ด ๊ธฐ์ฐจ ์ปดํ“จํ„ฐ ์•„๊นŒ ํ–ˆ๋˜ ๋ง์ด์•ผ. ๋‚ด๊ฐ€ ์ด๊ฒผ์–ด!<๋> ์˜ˆ 4 ํ„ด ์ž…์ถœ๋ ฅ ์ปดํ“จํ„ฐ <์‹œ์ž‘>๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜์ž. ๋‚ด๊ฐ€ ๋จผ์ € ๋งํ• ๊ฒŒ. ๊ธฐ์ฐจ ํ”Œ๋ ˆ์ด์–ด ์ฐจ๋Ÿ‰ ์ปดํ“จํ„ฐ ์–‘์‹ฌ ํ”Œ๋ ˆ์ด์–ด ์‹ฌ์ˆ  ์ปดํ“จํ„ฐ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๋‚ด๊ฐ€ ์กŒ์–ด.<๋> ํ’€์ด ์ฝ”๋“œ: ch05/wordgame2.py 5.3.6 ์—ฐ์Šต ๋ฌธ์ œ: ๋‚ด์ผ์˜ ๋‚ ์งœ ๊ตฌํ•˜๊ธฐ(2) ๋ฌธ์ œ ๋‚ด์ผ์˜ ๋‚ ์งœ ๊ตฌํ•˜๊ธฐ(1) ๋ฌธ์ œ๋ฅผ datetime ๋ชจ๋“ˆ์„ ํ™œ์šฉํ•ด์„œ ํ’€์–ด ๋ณด์„ธ์š”. strftime()๊ณผ strptime timedelta ์ฝ”๋“œ: ch05/tomorrow2.py 5.3.7 ์—ฐ์Šต ๋ฌธ์ œ: ์›์ฃผ์œจ ๊ตฌํ•˜๊ธฐ ์›(circle)์˜ ์ง€๋ฆ„(diameter)์— ๋Œ€ํ•œ ๋‘˜๋ ˆ(circumference)์˜ ๋น„๋ฅผ โ€˜์›์ฃผ์œจโ€™์ด๋ผ๊ณ  ํ•˜์ฃ . ์›์ด ํฌ๋“  ์ž‘๋“ , ๋ชจ๋“  ์›์— ๋Œ€ํ•ด ์›์ฃผ์œจ์€ ์•ฝ 3.14โ‹ฏ๋กœ ์ผ์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ˆ˜ํ•™์—์„œ [ํŒŒ์ด]๋ผ๋Š” ์ƒ์ˆ˜๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ๋„ ๋ดค๋“ฏ์ด, ํŒŒ์ด์ฌ์˜ math ๋ชจ๋“ˆ์—๋Š” ์›์ฃผ์œจ ๊ฐ’์ด ๋“ค์–ด ์žˆ์–ด์„œ ์šฐ๋ฆฌ๊ฐ€ ํž˜๋“ค๊ฒŒ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†์–ด์š”. >>> import math >>> math.pi 3.141592653589793 ๊ตณ์ด ํ•˜์ง€ ์•Š์•„๋„ ๋˜์ง€๋งŒ, ์ง์ ‘ ๊ตฌํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์žฌ๋ฏธ์žˆ๊ฒ ๋„ค์š”. ์›์ฃผ์œจ์„ ๊ตฌํ•˜๋Š” ์›๋ฆฌ ์›์ฃผ์œจ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜๋„ค์š”. ์—ฌ๊ธฐ์„œ๋Š” ๋ฌด์ž‘์œ„๋กœ ์ ์„ ์ฐ์–ด์„œ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฑฐ๋ถ์ด๋กœ ์›๊ณผ ์ •์‚ฌ๊ฐํ˜• ๊ทธ๋ฆฌ๊ธฐ ์ž ์‹œ ํ›„์— ์ˆ˜ํ•™ ๊ณต์‹์„ ์„ค๋ช…ํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ, ๊ทธ๋ฆผ์ด ์žˆ์œผ๋ฉด ์ดํ•ด๊ฐ€ ์ž˜ ๋  ๋“ฏํ•ด์„œ ๊ฑฐ๋ถ์ด(ํ„ฐํ‹€ ๊ทธ๋ž˜ํ”ฝ์Šค)์˜ ๋„์›€์„ ๋ฐ›์•„ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from turtle import * shape('turtle') # ์›์˜ ๋ฐ˜์ง€๋ฆ„ r = 100 # r ๋งŒํผ ์ด๋™ํ•ด์„œ ๊ทธ๋ฆฌ๊ธฐ ์ค€๋น„ up(); forward(r); down(); left(90) # ๋ฐ˜์ง€๋ฆ„์ด r์ธ ์› ๊ทธ๋ฆฌ๊ธฐ circle(r) # ํ•œ ๋ณ€์˜ ๊ธธ์ด๊ฐ€ r์˜ ๋‘ ๋ฐฐ์ธ ์ •์‚ฌ๊ฐํ˜• ๊ทธ๋ฆฌ๊ธฐ # ํ•œ ๋ฒˆ์— 2r์”ฉ ์ง์ง„ํ•ด๋„ ๋˜์ง€๋งŒ ์•„๋ž˜์ฒ˜๋Ÿผ ํ•ด๋„ ๋˜๊ฒ ์ฃ ? forward(r); left(90); forward(r) # ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ forward(r); left(90); forward(r) # ์˜ค๋ฅธ์ชฝ ์œ„ forward(r); left(90); forward(r) # ์™ผ์ชฝ ์œ„ forward(r); left(90); forward(r) # ์™ผ์ชฝ ์•„๋ž˜ ๊ฑฐ๋ถ์ด์—๊ฒŒ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋‹ฌ๋ผ๊ณ  ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์„ ์•„๋ž˜<NAME>์ƒ์— ์†Œ๊ฐœํ–ˆ์œผ๋‹ˆ ๊ถ๊ธˆํ•˜์‹  ๋ถ„์€ ์ฐธ๊ณ ํ•˜์„ธ์š”. https://youtu.be/ZEV3pGTdlxw ๋ชฌํ…Œ์นด๋ฅผ๋กœ ๋ฐฉ๋ฒ•์œผ๋กœ ์›์ฃผ์œจ์„ ๊ตฌํ•˜๋Š” ์›๋ฆฌ ๋‹ค์‹œ ์›์ฃผ์œจ ๊ตฌํ•˜๋Š” ๋ฒ• ์ด์•ผ๊ธฐ๋กœ ๋Œ์•„๊ฐˆ๊ฒŒ์š”. ๊ทธ ์ด๋ฆ„ ๋•Œ๋ฌธ์— ์™ ์ง€ ์–ด๋ ค์šด ๋Š๋‚Œ์ด ๋“ค์ง€๋งŒ, โ€˜๋ชฌํ…Œ ์นด๋ฅผ๋กœโ€™๋Š” ๊ทธ๋ƒฅ ๋™๋„ค ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋‚˜๋ผ๋กœ ์น˜๋ฉด ๊ฐ•์›๋„ ์ •์„ ๊ณผ ๋น„์Šทํ•œ ๊ณณ์ž…๋‹ˆ๋‹ค. ๋ชฌํ…Œ์นด๋ฅผ๋กœ ๋ฐฉ๋ฒ•์œผ๋กœ ์›์ฃผ์œจ์„ ๊ตฌํ•˜๋Š” ์›๋ฆฌ๋ฅผ ์ดํ•ดํ•˜๋ ค๋ฉด, ๋จผ์ € ๋‘ ๊ฐ€์ง€ ์ˆ˜ํ•™ ๊ณต์‹์„ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ •์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋Š” ํ•œ ๋ณ€์˜ ๊ธธ์ด๋ฅผ ์ œ๊ณฑํ•œ ๊ฐ’๊ณผ ๊ฐ™๋‹ค.(๋„“์ด ์ •์‚ฌ๊ฐํ˜• ๋ณ€ ์ด ์‚ฌ ํ˜• ๋ณ€ ) ์› ๋„“์ด๋Š” ๋ฐ˜์ง€๋ฆ„์„ ์ œ๊ณฑํ•œ ๊ฐ’์— ์›์ฃผ์œจ์„ ๊ณฑํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.(๋„“์ด์› ์ด = r) ์ •์‚ฌ๊ฐํ˜•์˜ ํ•œ ๋ณ€ ๊ธธ์ด๊ฐ€ ์›์˜ ์ง€๋ฆ„(๋ฐ˜์ง€๋ฆ„์˜ ๋‘ ๋ฐฐ, ์ฆ‰ r )๊ณผ ๊ฐ™๋‹ค๋ฉด, ์œ„์˜ ์ฒซ ๋ฒˆ์งธ ๊ณต์‹์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ฒ ์ฃ ? ๋„“์ด ์ •์‚ฌ๊ฐํ˜• ์ด ์‚ฌ ํ˜• ( r ) = r ๊ทธ๋ž˜์„œ ์› ๋„“์ด๋ฅผ ์ •์‚ฌ๊ฐํ˜• ๋„“์ด๋กœ ๋‚˜๋ˆ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉ๋‹ˆ๋‹ค. ๋„“์ด์› ๋„“์ด ์ •์‚ฌ๊ฐํ˜• ์ด ๋„“ ์ • ๊ฐ = r 4 2 ฯ€ ๊ทธ๋ž˜์„œ ์›์ฃผ์œจ์€ ๋„“์ด์› ๋„“์ด ์ •์‚ฌ๊ฐํ˜• = ๋„“ ์› ์ด ์‚ฌ ํ˜• ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์› ๋„“์ด์™€ ์ •์‚ฌ๊ฐํ˜• ๋„“์ด๋ฅผ ์•Œ๋ฉด ์›์ฃผ์œจ์„ ์•Œ ์ˆ˜ ์žˆ๊ฒ ๊ตฐ์š”! ์ •์‚ฌ๊ฐํ˜• ๋„“์ด๋Š” ๊ตฌํ•˜๊ธฐ ์‰ฌ์šด๋ฐ, ์› ๋„“์ด๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ•˜์ฃ ? ๋ฐ˜์ง€๋ฆ„ ์ œ๊ณฑ์— ์›์ฃผ์œจ์„ ๊ณฑํ•˜โ‹ฏ ์•„, ์ด๊ฒŒ ์•„๋‹ˆ์ง€โ‹ฏ. -_-;ใ…‹ ์ด๋•Œ โ€˜๊นŒ์ง“๊ฒƒ ๋Œ€์ถฉ ์ฐ์ž!โ€™๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ๋ฐ”๋กœ ๋ชฌํ…Œ์นด๋ฅผ๋กœ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ํ•˜๋ƒ๋ฉด์š”, ์ •์‚ฌ๊ฐํ˜• ์•ˆ์˜ ์•„๋ฌด ๋ฐ๋‚˜ ์ ์„ ๋งˆ๊ตฌ ์ฐ์€ ๋‹ค์Œ์—, ๊ทธ ์ ์ด ์›์— ๋“ค์–ด๊ฐ€๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋”ฐ์ ธ๋ณด๋Š” ๊ฑฐ์˜ˆ์š”. ๊ทธ๋ž˜์„œ ์› ์•ˆ์— ์žˆ๋Š” ์ ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ „์ฒด์—์„œ ์–ผ๋งˆ๋‚˜ ๋˜๋Š”์ง€ ๋น„์œจ์„ ๊ตฌํ•˜๋ฉด, ์ •ํ™•ํ•˜์ง€๋Š” ์•Š์•„๋„ ๋น„์Šทํ•˜๊ฒŒ๋‚˜๋งˆ ์›์ฃผ์œจ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ์›์ฃผ์œจ ์›์•ˆ์— ๋“ค์–ด๊ฐ„ ์ ์˜ ๊ฐœ์ˆ˜ ํ•œ ๋ณ€์˜ ๊ธธ์ด ๊ฐ€์›์˜ ์ง€๋ฆ„๊ณผ ๊ฐ™์€ ์ •์‚ฌ๊ฐํ˜• ์•ˆ์˜ ์ ์˜ ๊ฐœ์ˆ˜ ์ฃผ โ‰ˆ ์› ์•ˆ์— ๋“ค์–ด๊ฐ„ ์ ์˜ ๊ฐœ์ˆ˜ ํ•œ ๋ณ€์˜ ๊ธธ์ด๊ฐ€ ์›์˜ ์ง€๋ฆ„๊ณผ ๊ฐ™์€ ์ •์‚ฌ๊ฐํ˜• ์•ˆ์˜ ์ ์˜ ๊ฐœ์ˆ˜ ์ •๋ง๋กœ ์›์ฃผ์œจ์˜ ๊ทผ์‚ฟ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋Š”, ํ•œ๋ฒˆ ์ฝ”๋”ฉํ•ด์„œ ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜์ฃ . random ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด ์›์ฃผ์œจ ๊ตฌํ•˜๊ธฐ ์‚ฌ๊ฐํ˜• ์•ˆ์— ์ ์„ ์ฐ๋Š” ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ์•Œ์•„๋ด์•ผ๊ฒ ๋„ค์š”. ์‚ฌ๊ฐํ˜• ์•ˆ์— ์ ์ฐ๊ธฐ random ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด์„œ, ๊ฑฐ๋ถ์ด์—๊ฒŒ ์‚ฌ๊ฐํ˜• ์•ˆ์˜ ์•„๋ฌด ๊ณณ์—๋‚˜ ๊ฐ€์„œ ์ ์„ ์ฐ์œผ๋ผ๊ณ  ์‹œ์ผœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ž์ทจ๋ฅผ ๋‚จ๊ธฐ์ง€ ์•Š๊ธฐ penup() from random import randrange def draw_random_dot(): point = randrange(-r, r), randrange(-r, r) goto(point) dot() # ์ ์ฐ๊ธฐ draw_random_dot() draw_random_dot() draw_random_dot() ... ์ ์ด ๋ช‡ ๊ฐœ์ธ์ง€ ์ง์ ‘ ์„ธ์–ด ๋ณผ๊นŒ์š”? ์ ์ด ์ด 8๊ฐœ๊ณ (ํ•œ ๊ฐœ๋Š” ๊ฑฐ๋ถ์ด์— ๊ฐ€๋ ค์ง) ๊ทธ์ค‘์— 6๊ฐœ๊ฐ€ ์› ์•ˆ์— ์žˆ๋„ค์š”. ์•ž์—์„œ ๋งŒ๋“  ์›์ฃผ์œจ ๊ตฌํ•˜๋Š” ์‹์— ๋„ฃ์–ด ๋ณด๋ฉด ร— 8 ์ด ๋˜์ฃ . ํŒŒ์ด์ฌ์œผ๋กœ ๊ณ„์‚ฐํ•˜๋ฉด, >>> 4 * (6 / 8) 3.0 ์›์ฃผ์œจ์€ ์•ฝ 3์ด ๋ฉ๋‹ˆ๋‹ค! ์ ์ด ์› ์•ˆ์— ์žˆ๋Š”์ง€ ํŒ๋ณ„ํ•˜๊ธฐ ์ ์„ ๋” ๋งŽ์ด ์ฐ์–ด์„œ ์‹คํ—˜ํ•˜๋ฉด ์›์ฃผ์œจ์„ ๋” ์ •ํ™•ํ•˜๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ, ์ ์„ ์ƒˆ๊ธฐ๊ฐ€ ํž˜๋“ค์–ด์งˆ ๊ฒƒ ๊ฐ™๊ตฐ์š”. ๊ทธ๋ž˜์„œ ์› ์•ˆ์— ์žˆ๋Š” ์ ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ์ผ์„ ํŒŒ์ด์ฌ์—๊ฒŒ ์‹œ์ผœ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์› ์•ˆ์— ๋“ค์–ด๊ฐ„ ์ ์ด ๋ช‡ ๊ฐœ์ธ์ง€ ์•Œ๋ ค๋ฉด, ์ ์ด ์› ์•ˆ์— ์žˆ๋Š”์ง€ ์› ๋ฐ–์— ์žˆ๋Š”์ง€๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์–ด์•ผ๊ฒ ์ฃ . ๊ทธ๋ž˜์„œ ์ˆ˜ํ•™ ์ง€์‹์ด ๋˜ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. โ€˜์› ์ค‘์‹ฌ๊ณผ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์›์˜ ๋ฐ˜์ง€๋ฆ„๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ์ ์ด ์› ์•ˆ์— ์žˆ๋‹คโ€™๋ผ๊ณ  ์ •ํ•˜๊ธฐ๋กœ ํ•˜์ฃ . ์› ์ค‘์‹ฌ์ด ( , ) ์— ์žˆ๊ณ  ์  ๊ฐ€ ( , ) ์— ์žˆ๋‹ค๊ณ  ํ•˜๋ฉด, ์› ์ค‘์‹ฌ๊ณผ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•  ์ˆ˜ ์žˆ์–ด์š”. = 2 b ์ด๊ฒƒ์„ ์ด์šฉํ•ด์„œ, ์ ์˜ x, y ์ขŒํ‘œ์™€ ๋ฐ˜์ง€๋ฆ„ r์„ ์ž…๋ ฅํ•˜๋ฉด ๊ทธ ์ ์ด ์›์˜ ์•ˆ์— ์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ๊ฒŒ์š”. import math def in_circle(x, y, r): return math.sqrt(x ** 2 + y ** 2) <= r ๊ทธ๋ฆฌ๊ณ  ์•ž์—์„œ ๋งŒ๋“  ์ ์ฐ๊ธฐ ํ•จ์ˆ˜๋ฅผ ๊ณ ์ณ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. def draw_random_dot(): x, y = randrange(-r, r), randrange(-r, r) goto(x, y) if in_circle(x, y, r): color('red') # ์› ์•ˆ์— ์žˆ์œผ๋ฉด ๋นจ๊ฐ„์ƒ‰ ์  else: color('blue') # ์› ๋ฐ–์— ์žˆ์œผ๋ฉด ํŒŒ๋ž€์ƒ‰ ์  dot() ๊ทธ๋ฆผ์„<NAME>๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ๊ทธ๋ ค ๋ณผ๊ฒŒ์š”. # ์žฌ์„ค์ • reset() # r ๋งŒํผ ์ด๋™ํ•ด์„œ ๊ทธ๋ฆฌ๊ธฐ ์ค€๋น„ up(); forward(r); down(); left(90) # ๋ฐ˜์ง€๋ฆ„์ด r์ธ ์› ๊ทธ๋ฆฌ๊ธฐ circle(r) # ํ•œ ๋ณ€์˜ ๊ธธ์ด๊ฐ€ r์˜ ๋‘ ๋ฐฐ์ธ ์ •์‚ฌ๊ฐํ˜• ๊ทธ๋ฆฌ๊ธฐ for i in range(4): forward(r); left(90); forward(r) # ์ž์ทจ๋ฅผ ๋‚จ๊ธฐ์ง€ ์•Š๊ธฐ penup() # ์  30๊ฐœ ์ฐ๊ธฐ for i in range(30): draw_random_dot() # ๊ฑฐ๋ถ์ด ์ˆจ๊ธฐ๊ธฐ hideturtle() ๋ฌธ์ œ ์ ์ด ์› ์•ˆ์— ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฑด ํ•ด๊ฒฐํ–ˆ์œผ๋‹ˆ, ์ด์ œ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ ์ด ์› ์•ˆ์— ์žˆ์œผ๋ฉด ๋ณ€์ˆซ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚ค๋ผ๊ณ  ํ•˜๋ฉด ๋˜๊ฒ ๋„ค์š”. ๊ทธ ๋ถ€๋ถ„์€ ์—ฌ๋Ÿฌ๋ถ„์ด ์ง์ ‘ ์ฝ”๋”ฉํ•ด์„œ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”. ํ’€์ด ch05/montecarlo.py ์ฐธ๊ณ  https://ko.wikipedia.org/wiki/์›์ฃผ์œจ https://en.wikipedia.org/wiki/Pi Estimating ฯ€ using the Monte Carlo Method tip ๋ชฌํ…Œ์นด๋ฅผ๋กœ ๋ฐฉ๋ฒ•์˜ ์œ ๋ž˜ ๋ชฌํ…Œ ์นด๋ฅผ๋กœ(Monte-Carlo)๋Š” ๋„์‹œ๊ตญ๊ฐ€์ธ ๋ชจ๋‚˜์ฝ” ๋ถ๋ถ€์— ์žˆ๋Š” ์ง€์—ญ์œผ๋กœ์„œ ์นด์ง€๋…ธ, ๋„๋ฐ•์œผ๋กœ ์œ ๋ช…ํ•œ ๊ณณ์ด๊ธฐ๋„ ํ•˜๋‹ค. ์ˆ˜ํ•™์—์„œ ํ™•๋ฅ ์ด๋ก ์˜ ํƒ„์ƒ์ด ์›๋ž˜ ๋„๋ฐ•์—์„œ ๋น„๋กฏ๋˜์—ˆ๋Š”๋ฐ, ๊ณผ๊ฑฐ ๋„๋ฐ• ๋„์‹œ์˜ ๋Œ€๋ช…์‚ฌ์˜€๋˜ ๋ชฌํ…Œ์นด๋ฅผ๋กœ ์—ญ์‹œ ์ง€๊ธˆ์€ ํ™•๋ฅ ๋ก ๊ณผ ๋ฐ€์ ‘ํ•œ ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๊ณผํ•™์  ๋ฐฉ๋ฒ•๋ก ์„ ์ง€์นญํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์˜ค๋Š˜๋‚  ๋‹ค์–‘ํ•œ ๊ณผํ•™๊ธฐ์ˆ  ๋ถ„์•ผ์˜ ์ปดํ“จํ„ฐ ์‹œ๋ฎฌ๋ ˆ์ด์…˜, ์˜ˆ์ธก ๋“ฑ์— ๋นˆ๋ฒˆํžˆ ์“ฐ์ด๋Š” ๋ชฌํ…Œ์นด๋ฅผ๋กœ ๋ฐฉ๋ฒ•์ด ์ ์šฉ๋œ ๋งค์šฐ ์œ ๋ช…ํ•œ ์—ญ์‚ฌ์  ์‚ฌ๋ก€๋กœ์„œ, ๋ฏธ๊ตญ์˜ ์›์žํญํƒ„ ๊ฐœ๋ฐœ ๊ณ„ํš์ธ ๋งจํ•ดํŠผ ํ”„๋กœ์ ํŠธ๋ฅผ ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋ฐ”๋กœ ๋ชฌํ…Œ์นด๋ฅผ๋กœ ๋ฐฉ๋ฒ•์ด๋ผ๋Š” ์ด๋ฆ„์ด ์ฒ˜์Œ ์“ฐ์ด๊ฒŒ ๋œ ๊ณ„๊ธฐ์ด๊ธฐ๋„ ํ•˜๋‹ค. ์ฆ‰ ํด๋ž€๋“œ ์ถœ์‹ ์˜ ์ˆ˜ํ•™์ž ์Šคํƒœ๋‹ˆ์Šฌ๋กœ ์šธ๋žŒ(Stanisล‚aw Marcin Ulam, 1909-1984)์€ ์ปดํ“จํ„ฐ์˜ ์•„๋ฒ„์ง€๋กœ ์ž˜ ์•Œ๋ ค์ง„ ํฐ ๋…ธ์ด๋งŒ(Johann Ludwig von Neumann, 1903-1957) ๋“ฑ๊ณผ ํ•จ๊ป˜ ๋งจํ•ดํŠผ ํ”„๋กœ์ ํŠธ์— ์ฐธ์—ฌํ•˜์˜€๋‹ค. ์˜ˆ์ „๋ถ€ํ„ฐ ๋…ธ์ด๋งŒ์˜ ๋™๋ฃŒ์˜€๋˜ ์šธ๋žŒ์€ ๊ทน๋น„์˜ ์ฝ”๋“œ๋ช…์— ์ ํ•ฉํ•˜๋„๋ก ์ƒˆ๋กœ์šด ์ˆ˜ํ•™์  ๋ฐฉ๋ฒ•๋ก ์„ ๋„๋ฐ•์˜ ๋„์‹œ ์ด๋ฆ„์„ ๋”ฐ์„œ ๋ชฌํ…Œ์นด๋ฅผ๋กœ ๋ฐฉ๋ฒ•์ด๋ผ ๋ช…๋ช…ํ•˜์˜€๊ณ , ์ด ๋ฐฉ๋ฒ•์€ ์ค‘์„ฑ์ž๊ฐ€ ์›์žํ•ต๊ณผ ์ถฉ๋Œํ•˜๋Š” ๊ณผ์ •์„ ์ดํ•ดํ•˜๊ณ  ๋ฌ˜์‚ฌํ•˜๋Š” ๋ฐ์— ๊ฒฐ์ •์  ์—ญํ• ์„ ํ•˜์˜€๋‹ค. -<NAME>, โŒฉ๋ชฌํ…Œ ์นด๋ฅผ๋กœ ๋ฐฉ๋ฒ•๊ณผ ์ธ๊ณต์ง€๋ŠฅโŒช, โŸช์‚ฌ์ด์–ธ์Šค ํƒ€์ž„์ŠคโŸซ 6. ํŒŒ์ผ ํŒŒ์ด์ฌ์œผ๋กœ ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ์ด ์žฅ์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฒƒ: ํ…์ŠคํŠธ ํŒŒ์ผ ํ•œ ์ค„์”ฉ ๋‹ค๋ฃจ๊ธฐ ํŒŒ์ผ์„ ์ž…๋ง›๋Œ€๋กœ(pickle, glob, os.path) ์‘์šฉ ์˜ˆ์ œ: ์Œ์„ฑ ์ธ์‹์„ ํ™œ์šฉํ•œ ์ผ๋ณธ์–ด ํ€ด์ฆˆ 6.1 ํ…์ŠคํŠธ ํŒŒ์ผ ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ๋ช…์„ ๋ฐ”๊พผ๋‹ค๊ฑฐ๋‚˜, ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๊ณ ,<NAME>๋Š” ์ผ๋“ค์€ ์ง€๋‚œ ์‹œ๊ฐ„์— ์‚ดํŽด๋ณธ os ๋ชจ๋“ˆ์ด๋‚˜, shutil์ด๋ผ๋Š” ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋˜์ฃ . ์ง€๊ธˆ ๋ฐฐ์šธ ๊ฒƒ์€ ๊ทธ๋Ÿฐ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ๊ณ , ์“ฐ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋ฉด ํ”„๋กœ๊ทธ๋žจ๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์ง€์š”. ๋งŒ์•ฝ ์ฃผ์†Œ๋ก ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ ๋‹ค๋ฉด ์—ฐ๋ฝ์ฒ˜๋ฅผ ๋‹ด์€ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด๋‘์—ˆ๋‹ค๊ฐ€, ์ƒˆ ์นœ๊ตฌ๊ฐ€ ์ƒ๊ธธ ๋•Œ๋งˆ๋‹ค ๊ทธ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์— ์—ฐ๋ฝ์ฒ˜๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด ๋˜๊ฒ ์ฃ ? ๋จผ์ € ๋ฉ”๋ชจ์žฅ์œผ๋กœ ํ…์ŠคํŠธ ํŒŒ์ผ ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. Programming is fun. Very fun! You have to do it yourself... ๊ฐ์ž ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํด๋”๋ฅผ ๋งŒ๋“ค์–ด๋‘์…จ์„ ๊ฑฐ์˜ˆ์š”. ๊ฑฐ๊ธฐ์—๋‹ค๊ฐ€ Python_for_Fun.txt์™€ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•ด ์ฃผ์„ธ์š”. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ๋”ฐ๋ผ ํ•ด๋ณด์„ธ์š”. ํด๋”์™€ ํŒŒ์ผ์˜ ์ด๋ฆ„์€ ๊ฐ์ž ์ง€์œผ์‹  ๋Œ€๋กœ ์จ์ฃผ์‹œ๊ณ ์š”. >>> f = open('C:\\python_newbie\\Python_for_Fun.txt') >>> f.read() 'Programming is fun.\nVery fun!\n\nYou have to do it yourself.' ํŒŒ์ผ์„ ์—ด์–ด์„œ f๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์—ฌ์ฃผ๊ณ  ๋‹ค์‹œ ๊ทธ๊ฒƒ์„ ์ฝ์—ˆ์ฃ ? ์—ฌ๋Ÿฌ๋ถ„๋„ ์ž˜ ์ฝ์–ด์ง€๋‚˜์š”? ๊ทธ๋Ÿฐ๋ฐ, ์ข€ ์ด์ƒํ•œ ๊ฒƒ์ด ์žˆ์ฃ ? ์•„๊นŒ ํŒŒ์ผ์„ ์ž‘์„ฑํ•  ๋•Œ ์ค„์„ ๋ฐ”๊ฟ”๊ฐ€๋ฉด์„œ ์ผ๋˜ ๊ฒƒ์ด ๋ชจ๋‘ ํ•œ ์ค„์— ๋‚˜์˜ต๋‹ˆ๋‹ค. ์“ด ์ ์ด ์—†๋Š” ๊ธ€์ž๋„ ๋ผ์–ด ์žˆ๊ณ ์š”. ์ž˜ ๋ณด๋ฉด ์ค„์„ ๋ฐ”๊พผ ๊ณณ๋งˆ๋‹ค '\n'์ด ๋“ค์–ด์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์ง€์š”. '\n'์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•  ๋•Œ '์ค„ ๋ฐ”๊ฟˆ'์„ ์˜๋ฏธํ•œ๋‹ต๋‹ˆ๋‹ค. >>> print('์•ผํ˜ธ~ ํ˜ธ์•ผ~') ์•ผํ˜ธ~ ํ˜ธ์•ผ~ ์šฐ๋ฆฌ๊ฐ€ ๋ฉ”๋ชจ์žฅ์—์„œ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ž‘์„ฑํ•  ๋•Œ์—๋„ ์ค„์„ ๋ฐ”๊ฟ€ ๋•Œ๋งˆ๋‹ค ์ด๋Ÿฐ ๊ฐœํ–‰ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์—ˆ๋˜ ๊ฒƒ์ด์ง€์š”. ์œ„์—์„œ๋Š” f.read() ํ•จ์ˆ˜๊ฐ€ ๋Œ๋ ค์ค€ ๋ฌธ์ž์—ด์„ ๊ทธ๋Œ€๋กœ ๋ณด์•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๊ฐ€ ๊ฐœํ–‰ ๋ฌธ์ž๋„ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ์ด๊ณ , ์ด ๋ฌธ์ž์—ด์„ print ํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ ์ค„ ๋„˜๊น€์ด ๋œ ์ƒํƒœ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. >>> buffer = f.read() >>> print(buffer) Programming is fun. Very fun! You have to do it yourself... ์ด๋ฒˆ์—” ํŒŒ์ผ์— ๊ธ€์„ ์จ๋ณผ๊นŒ์š”? >>> letter = open('C:\\python_newbie\\letter.txt', 'w') # ์ƒˆ ํŒŒ์ผ ์—ด๊ธฐ >>> letter.write('Dear Father,') # ์•„๋ฒ„๋‹˜ ์ „ ์ƒ์„œ >>> letter.close() # ๋‹ซ๊ธฐ ์ด๋ฒˆ์—” ํŒŒ์ผ๋ช… ๋’ค์— 'w'๋ผ๊ณ  ์จ์ฃผ์—ˆ์ฃ ? ์•„๊นŒ ํŒŒ์ผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ธฐ๋งŒ ํ–ˆ์„ ๋•Œ์™€๋Š” ๋‹ฌ๋ฆฌ, ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๋ ค๊ณ  ํ•  ๋•Œ๋Š” ๋ฏธ๋ฆฌ 'ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๊ฒ ๋‹ค'๋ผ๋Š” ์‚ฌ์‹ค์„ ์•Œ๋ ค์ค„ ํ•„์š”๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์— ๋ช‡ ์ž ์ ์–ด์ฃผ๊ณ  ๋‚˜์„œ ํŒŒ์ผ์„ ๋‹ซ์•˜์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ•ด๋‹น ํด๋”๋ฅผ ๋ณด์‹œ๋ฉด letter.txt ํŒŒ์ผ์ด ์žˆ๊ณ ์š”, ๊ทธ๊ฑธ ๋ฉ”๋ชจ์žฅ์œผ๋กœ ์—ด์–ด๋ณด์‹œ๋ฉด ํŒŒ์ด์ฌ์œผ๋กœ ์จ์ค€ ๊ธ€์ž๊ฐ€ ๊ณ ์Šค๋ž€ํžˆ ์ ํ˜€์žˆ์„ ๊ฑฐ์˜ˆ์š”. ํ›Œ๋ฅญํ•˜์ฃ ? ๋งŒ์•ฝ, ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ด ๋‹ค์Œ์— close()๋กœ ๋‹ซ์•„์ฃผ์ง€ ์•Š์•˜์œผ๋ฉด ๋ฉ”๋ชจ์žฅ์œผ๋กœ ๋ด๋„ ์•„๋ฌด ๊ธ€์ž๋„ ์—†์„ ๊ฑฐ์˜ˆ์š”. ๊ทธ๋Ÿฌ๋‹ˆ ๊ผฌ์˜ฅ~ ๋‹ซ์•„์ฃผ์…”์•ผ ํ•ด์š”~. ์˜ˆ๋ฆฌํ•˜์‹  ๋ถ„๋“ค์€ ๊ทธ์ „์˜ ์˜ˆ์ œ์—์„œ ํŒŒ์ผ์„ ์ฝ์—ˆ์„ ๋•Œ๋Š” ์™œ ์•ˆ ๋‹ซ์•˜๋Š”์ง€ ๊ถ๊ธˆํ•ดํ•˜์‹œ๊ฒ ์ฃ ? ๋‹ซ์•„์ฃผ๋ฉด ์ข‹๋‹ต๋‹ˆ๋‹ค.^^ ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์žŠ์–ด๋ฒ„๋ฆฌ๋”๋ผ๋„ ํŒŒ์ผ์ด ๋” ์ด์ƒ ํ•„์š” ์—†์œผ๋ฉด ํŒŒ์ด์ฌ์ด ์•Œ์•„์„œ ๋‹ซ์•„์ค€๋‹ค๊ณ  ํ•˜๋„ค์š”. ์ฐธ ํŽธํ•˜์ฃ ? ํ˜น์‹œ letter.txt๊ฐ€ ์•„๋‹ˆ๋ผ python.txt ํŒŒ์ผ์—๋‹ค๊ฐ€ 'Dear Father'๋ผ๊ณ  ์“ฐ์‹  ๋ถ„ ๊ณ„์‹œ๋‚˜์š”? ๋ถ„๋ช…ํžˆ ๊ณ„์‹ค ๊ฒƒ ๊ฐ™์€๋ฐโ€ฆ ์›๋ž˜ ๋“ค์–ด์žˆ๋˜ ๋‚ด์šฉ์ด ๋‹ค ๋‚ ์•„๊ฐ”์ฃ ? ์‹œํ‚ค๋Š” ๋Œ€๋กœ ์•ˆ ํ•˜๋‹ˆ๊นŒ ๊ทธ๋ ‡์ฃ . ํžˆํžˆํžˆ ํ•˜์ง€๋งŒ, ์ „ ์‹œํ‚ค๋Š” ๋Œ€๋กœ๋งŒ ํ•˜๋Š” ์‚ฌ๋žŒ์€ ์‹ซ์–ดํ•ฉ๋‹ˆ๋‹ค. ์ž˜ ํ•˜์…จ์–ด์š”. ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์›๋ž˜ ๋“ค์–ด์žˆ์„ ๋•Œ 'w' ๋ชจ๋“œ๋กœ ํŒŒ์ผ์„ ์—ด๋ฉด ์›๋ž˜ ์žˆ๋˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์–ด์ ธ ๋ฒ„๋ฆฐ๋‹ต๋‹ˆ๋‹ค. ํŒŒ์ผ์— ๋“ค์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ<NAME>์ง€ ์•Š๊ณ  ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด 'a+' ๋ชจ๋“œ๋ฅผ ์ง€์ •ํ•ด ์ค˜์•ผ ํ•˜์ฃ . >>> letter = open('C:\\python_newbie\\letter.txt', 'a+') >>> letter.write('\n\nHow are you?') >>> letter.close() ์ด์ œ letter๋ฅผ ๋‹ค์‹œ ์—ด์–ด๋ณด์‹œ๋ฉด ์ œ๋Œ€๋กœ ๋˜์–ด์žˆ์„ ๊ฑฐ์˜ˆ์š”. ์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ์€ ๊ฐ„๋‹จํ•˜๋ฉด์„œ๋„ ์•„์ฃผ ์“ธ๋ชจ ์žˆ๋‹ต๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ๊ฐ€์ง€๊ณ  ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์žˆ์„์ง€ ํ•œ๋ฒˆ ์ƒ๊ฐํ•ด ๋ณด์„ธ์š”. 6.2ํ•œ ์ค„์”ฉ ๋‹ค๋ฃจ๊ธฐ ์ง€๋‚œ ์‹œ๊ฐ„์— ์ด์–ด ์˜ค๋Š˜๋„ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ๊ดด๋กญํ˜€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ํ•œ ์ค„์”ฉ ๋‚œ๋„์งˆ์„โ€ฆ --+ ํŒŒ์ด์ฌ์ด ์„ค์น˜๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” LICENSE.txt ํŒŒ์ผ์„ ์—ด์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> import os >>> os.getcwd() 'C:\\Users\\Yong Choi\\AppData\\Local\\Programs\\Python\\Python38-32' >>> os.listdir() ['DLLs', 'Doc', 'img_read.py', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python38.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll'] >>> f = open('LICENSE.txt') ํŒŒ์ผ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ฝ์„ ๋• read()๋ฅผ ์ผ์ฃ ? ํ•œ ์ค„์”ฉ ์ฝ์„ ๋•Œ๋Š” readline()์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฒซ ์ค„์„ ์ฝ์–ด๋ณผ๊ฒŒ์š”. >>> f.readline() 'A. HISTORY OF THE SOFTWARE\n' ๋‹ค์Œ ์ค„๋„ ์ฝ์–ด๋ณผ๊นŒ์š”? >>> f.readline() '==========================\n' ๊ฐ„๋‹จํ•˜์ฃ ? ๋ณ„๊ฒƒ ์•„๋‹™๋‹ˆ๋‹ค. ํŒŒ์ผ ๋‚ด์šฉ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•œ ๊ธ€์ž์”ฉ ์ฃผ๋ฅด๋ฅต~ ์ฝ์–ด๋‚˜๊ฐ€๋‹ค๊ฐ€ '\n'์ด ๋‚˜ํƒ€๋‚˜๋ฉด ํ•œ ์ค„์ด ๋๋‚œ ์ค„ ์•Œ๊ณ  ๋”ฑ ๋ฉˆ์ถฐ ์„œ๊ฒŒ ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ˜๋ณต ๋ฌธ๊ณผ readline()์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด ํ…์ŠคํŠธ๋ฅผ ์›ํ•˜๋Š” ์ค„ ์ˆ˜๋งŒํผ๋งŒ ์ฝ์–ด๋“ค์ผ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. >>> for x in range(5): ... f.readline() ... '\n' 'Python was created in the early 1990s by Guido van Rossum at Stichting\n' 'Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands\n' "as a successor of a language called ABC. Guido remains Python's\n" 'principal author, although it includes many contributions from others.\n' ํŒŒ์ผ์„ ํ•œ ์ค„์”ฉ ์ฝ์–ด ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ธฐ๋ฅผ ๋‹ค์„ฏ ๋ฒˆ ๋˜ํ’€์ดํ–ˆ์ง€์š”. ์ด๋ฒˆ์—” ๋˜‘๊ฐ™์ด ๋‹ค์„ฏ ์ค„์„ ์ฝ๊ธฐ๋Š” ํ•˜์ง€๋งŒ ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. >>> f = open('LICENSE.txt') >>> lines = f.readlines() >>> lines[:2] ['A. HISTORY OF THE SOFTWARE\n', '==========================\n'] ์—ฌ๊ธฐ์„œ๋Š” 'readline'์— 's' ์ž๊ฐ€ ๋ถ™์€ readlines()๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. readlines()๋กœ ํŒŒ์ผ์„ ์ฝ์œผ๋ฉด ํ•œ ์ค„, ํ•œ ์ค„์ด ๊ฐ๊ฐ ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์œ„์—์„œ๋Š” ํŒŒ์ผ ์ „์ฒด๊ฐ€ lines๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์— ์™ ๋‹ด๊ฒผ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ์—” lines์— ๋“ค์–ด์žˆ๋Š” ๊ฒƒ๋“ค์„ ์ž…๋ง›๋Œ€๋กœ ๊บผ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์“ฐ๋ฉด ์•„์ฃผ ์‰ฝ๊ฒŒ ์›ํ•˜๋Š” ์ค„์„ ์ฝ์–ด๋“ค์ผ ์ˆ˜ ์žˆ๊ฒ ์ฃ ? 26๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ 40๋ฒˆ์งธ ์ค„๊นŒ์ง€๋ฅผ ์ฝ์–ด๋ณผ๊นŒ์š”? >>> for l in lines[26:41]: ... print(l, end='') ... Release Derived Year Owner GPL- from compatible? (1) 0.9.0 thru 1.2 1991-1995 CWI yes 1.3 thru 1.5.2 1.2 1995-1999 CNRI yes 1.6 1.5.2 2000 CNRI no 2.0 1.6 2000 BeOpen.com no 1.6.1 1.6 2001 CNRI yes (2) 2.1 2.0+1.6.1 2001 PSF no 2.0.1 2.0+1.6.1 2001 PSF yes 2.1.1 2.1+2.0.1 2001 PSF yes 2.1.2 2.1.1 2002 PSF yes 2.1.3 2.1.2 2002 PSF yes 2.2 and above 2.1.1 2001-now PSF yes >>> ์™œ 26:40์ด ์•„๋‹ˆ๋ผ 26:41์ด๋ผ๊ณ  ํ•˜๋Š”์ง€ ์•„๋ฆฌ์†กํ•˜์‹  ๋ถ„์€ ๋ฌธ์ž์—ด๊ณผ ๋ฆฌ์ŠคํŠธ ๊ฐ•์ขŒ๋ฅผ ๋ณต์Šตํ•˜์…”์•ผ๊ฒ ๋„ค์š”. ์˜ค๋Š˜์˜ ํ•˜์ด๋ผ์ดํŠธ! ๋์—์„œ ์—ด ์ค„์„ ์ฝ์–ด๋ด…์‹œ๋‹ค. ๋Œ€์ฒด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ํŒŒ์ด์ฌ ํŠœํ† ๋ฆฌ์–ผ์„ ์‚ดํŽด๋ณด์‹œ๋ฉด ์ง์ ‘ ์•Œ์•„๋‚ด์‹ค ์ˆ˜๋„ ์žˆ๋Š”๋ฐโ€ฆ ํžŒํŠธ๋ผ๋„ ๋“œ๋ฆด๊นŒ์š”? ๋ฆฌ์ŠคํŠธ์—์„œ ๋งจ ๋งˆ์ง€๋ง‰ ์›์†Œ์˜ ์ธ๋ฑ์Šค๋Š” -1์ด๋ž๋‹ˆ๋‹ค. ๊ทธ๊ฒŒ ๋ฌด์Šจ ๋ง์ด์ง€โ€ฆ ํ•œ๋ฒˆ ๋„์ „ํ•ด ๋ณด์„ธ์š”. ์„ฑ๊ณตํ•˜์‹  ๋ถ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฑฐ์˜ˆ์š”~ Parts of this software are based on the Tcl/Tk software copyrighted by the Regents of the University of California, Sun Microsystems, Inc., and other parties. The original license terms of the Tcl/Tk software distribution is included in the file docs/license.tcltk. Parts of this software are based on the HTML Library software copyrighted by Sun Microsystems, Inc. The original license terms of the HTML Library software distribution is included in the file docs/license.html_lib. 6.2.1 ์—ฐ์Šต ๋ฌธ์ œ: ๋ฐด๋“œ ์ด๋ฆ„ ์ง“๊ธฐ (2) ๋ฌธ์ œ ์ƒ‰์ด๋ฆ„๊ณผ ์Œ์‹ ์ด๋ฆ„์„ ๋‹ด์€ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ฝ์–ด์„œ ๋ฐด๋“œ ์ด๋ฆ„์„ ์ง€์–ด ์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. ํ…์ŠคํŠธ ํŒŒ์ผ์„ ๋งŒ๋“ค๋ ค๋ฉด ์•„๋ž˜ ๋งํฌ๋ฅผ ์˜ค๋ฅธ์ชฝ ํด๋ฆญํ•˜๊ณ  [๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋งํฌ ์ €์žฅ]์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ํŒŒ์ผ๊ณผ ํŒŒ์ด์ฌ ํŒŒ์ผ์„ ๊ฐ™์€ ํด๋”์— ๋‘์„ธ์š”. ์ƒ‰์ด๋ฆ„ 1: color.txt (https://raw.githubusercontent.com/ychoi-kr/wikidocs-chobo-python/master/ch06/color.txt) ์Œ์‹ ์ด๋ฆ„ 2: food.txt (https://raw.githubusercontent.com/ychoi-kr/wikidocs-chobo-python/master/ch06/food.txt) $ python bandname2.py ์•„์ด๋ณด๋ฆฌ ๋–ก๊ตญ $ python bandname2.py ๋ฏธ๋“œ๋‚˜์ดํŠธ ๋ธ”๋ฃจ ๋น™์ˆ˜ $ python bandname2.py ๋ฏธ๋””์—„ํ„ฐ์ฝฐ์ด์ฆˆ ๊น๋‘๊ธฐ $ python bandname2.py ํ”Œ๋Ÿผ ๋จธ์Šคํ„ฐ๋“œ $ python bandname2.py ๋ผ์ดํŠธ ๊ทธ๋ ˆ์ด ๋””์ €ํŠธ $ python bandname2.py ์”จ์‰˜ ๊ณ„๋ž€๋ฎ๋ฐฅ ์ฝ”๋“œ: ch06/bandname2.py https://supervitamin.tistory.com/76 โ†ฉ https://ko.wiktionary.org/wiki/๋ถ„๋ฅ˜:ํ•œ๊ตญ์–ด_์Œ์‹ โ†ฉ 6.2.2 ์—ฐ์Šต ๋ฌธ์ œ: ๋น„๋ฐ€ ๋ฉ”์‹œ์ง€ ์ œ2์ฐจ ์„ธ๊ณ„๋Œ€์ „์ด ํ•œ์ฐฝ์ด๋˜ 1943๋…„ ๋ฏธ๊ตญ ๋กœ์Šค์•ค์ ค๋ ˆ์Šค์˜ ์ง‘๋ฐฐ์›์€ ์—ฝ์„œ ํ•œ ์žฅ์„ ์ง‘์–ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๋ณด๋‚ธ์ด๋Š” ์ผ๋ณธ์˜ ์ „์Ÿ ํฌ๋กœ์ˆ˜์šฉ์†Œ์— ์ˆ˜๊ฐ๋ผ ์žˆ๋˜ ๋ฏธ๊ตฐ ํ”„๋žญํฌ ์กฐ๋„ฌ๋ฆฌ์Šค, ๋ฐ›๋Š” ์ด๋Š” ํŽ˜๋”๋Ÿด ๋นŒ๋”ฉ ํšŒ์‚ฌ 1619ํ˜ธ์˜ F.B.Iers ์”จ๋กœ ์ ํ˜€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํšŒ์‚ฌ๋„ ์‚ฌ๋žŒ๋„ ์‹ค์ œ๋กœ ์กด์žฌํ•˜์ง€ ์•Š์•˜์ง€๋งŒ, ํ•ด๋‹น ์ฃผ์†Œ์˜ 619ํ˜ธ์—๋Š” ์—ฐ๋ฐฉ์ˆ˜์‚ฌ๊ตญ(FBI) ์‚ฌ๋ฌด์‹ค์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. (๊ทธ๋ฆผ ์ถœ์ฒ˜: http://informatik.rostfrank.de/info/lex09/lex0903.html) ์—ฝ์„œ์˜ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. August 29, 1943 Dear Iers: After surrender, health improved fifty percent. Better food etc. Americans lost confidence in Philippines. Am comfortable in Nippon. Mother: Invest 30%, salary, in business. Love (signed) Frank G. Jonelis "ํ•ญ๋ณต ํ›„, ๊ฑด๊ฐ•์ด 50% ๋‚˜์•„์กŒ๋‹ค. ์ข‹์€ ์Œ์‹ ๋“ฑ. ๋ฏธ๊ตญ์ธ์€ ํ•„๋ฆฌํ•€์—์„œ ์ž์‹ ๊ฐ์„ ์žƒ์—ˆ๋‹ค. ์ผ๋ณธ์—์„œ ํŽธ์•ˆํ•˜๋‹ค. ์–ด๋จธ๋‹ˆ: 30% ํˆฌ์ž, ๋ด‰๊ธ‰, ์‚ฌ์—…์—. ์‚ฌ๋ž‘ํ•ด" ๋ฌธ์žฅ์ด ์•ฝ๊ฐ„ ์–ด์ƒ‰ํ•˜๊ธด ํ–ˆ์ง€๋งŒ ๋ณ„๋‹ค๋ฅธ ๋‚ด์šฉ์ด ์—†์–ด ๋ณด์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์—ฐ๋ฐฉ์ˆ˜์‚ฌ๊ตญ์—์„œ๋Š” ์—ฌ๊ธฐ์— ๋‹ค๋ฅธ ๋ฉ”์‹œ์ง€๊ฐ€ ์ˆจ๊ฒจ์ ธ ์žˆ์„์ง€ ๋ชจ๋ฅธ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹จ์–ด๋ฅผ ๋ฐฐ์—ดํ•ด ๋ณด๋‹ค๊ฐ€, ๊ฐ ์ค„์˜ ์ฒซ ๋‘ ๋‹จ์–ด๋ฅผ ๋ฌธ์žฅ ๋ถ€ํ˜ธ ์—†์ด ์กฐํ•ฉํ•˜๋ฉด ๋‹ค์Œ์˜ ๋ฌธ์žฅ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋ƒˆ์Šต๋‹ˆ๋‹ค.(๋งˆ์ง€๋ง‰ ๋‹จ์–ด ์ƒ๋žต) AFTER SURRENDER FIFTY PERCENT AMERICANS LOST IN PHILIPPINES IN NIPPON 30% "ํ•ญ๋ณตํ•œ ํ›„ ํ•„๋ฆฌํ•€์—์„œ 50% ์ผ๋ณธ์—์„œ 30%์˜ ๋ฏธ๊ตฐ์„ ์žƒ์—ˆ๋‹ค" ๋ฌธ์ œ ํ…์ŠคํŠธ ํŽธ์ง‘๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ด ์œ„์˜ ์—ฝ์„œ ๋‚ด์šฉ์„ postcard.txt ํŒŒ์ผ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. 1. ํŒŒ์ผ ์ฝ๊ธฐ postcard.txt ํŒŒ์ผ์„ ์ฝ์–ด ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜์„ธ์š”. 2. ๋ณธ๋ฌธ ์ถ”๋ ค๋‚ด๊ธฐ ์—ฝ์„œ ๋‚ด์šฉ์€ ๋จธ๋ฆฌ๋ง, ๋ณธ๋ฌธ, ๊ผฌ๋ฆฌ๋ง ํ˜•ํƒœ๋กœ ๋˜์–ด ์žˆ๊ณ  ๊ทธ ์‚ฌ์ด๋Š” ํ•œ ์ค„์”ฉ ๋„์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ๋ฌธ ๋‚ด์šฉ๋งŒ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜์„ธ์š”. ์ถœ๋ ฅ After surrender, health improved fifty percent. Better food etc. Americans lost confidence in Philippines. Am comfortable in Nippon. Mother: Invest 30%, salary, in business. Love 3. ๋ฌธ์žฅ๋ถ€ํ˜ธ ์ œ๊ฑฐ ๋ณธ๋ฌธ์—์„œ ๋งˆ์นจํ‘œ(.), ์‰ผํ‘œ(,), ์ฝœ๋ก (:)์„ ์ œ๊ฑฐํ•ด ์ถœ๋ ฅํ•˜์„ธ์š”. ์ถœ๋ ฅ After surrender health improved fifty percent Better food etc Americans lost confidence in Philippines Am comfortable in Nippon Mother Invest 30% salary in business Love 4. ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ 3๋ฒˆ์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋‚˜ํƒ€๋‚ด์„ธ์š”. ์ถœ๋ ฅ AFTER SURRENDER HEALTH IMPROVED FIFTY PERCENT BETTER FOOD ETC AMERICANS LOST CONFIDENCE IN PHILIPPINES AM COMFORTABLE IN NIPPON MOTHER INVEST 30% SALARY IN BUSINESS LOVE 5. ๋น„๋ฐ€ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ 4๋ฒˆ์˜ ๊ฒฐ๊ณผ์—์„œ ๊ฐ ํ–‰์˜ ์ฒ˜์Œ ๋‘ ๋‹จ์–ด๋งŒ ์ถ”๋ ค์„œ ์ถœ๋ ฅํ•˜์„ธ์š”. AFTER SURRENDER FIFTY PERCENT AMERICANS LOST IN PHILIPPINES IN NIPPON 30% SALARY ์—ฝ์„œ ๋‚ด์šฉ: ch06/postcard.txt ํŒŒ์ด์ฌ ์ฝ”๋“œ: ch06/secret_message.py ์ฐธ๊ณ  ๋ฃจ๋Œํ”„ ํ‚คํŽœํ•œ ์ง€์Œ / ์ด์ผ์šฐ ์˜ฎ๊น€, ใ€Š์•”ํ˜ธ์˜ ํ•ด์„ใ€‹ 6.2.2. ์—ฐ์Šต ๋ฌธ์ œ: ๋๋ง์ž‡๊ธฐ (3) ๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์ปดํ“จํ„ฐ์™€ ์‚ฌ์šฉ์ž๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ์— ์žˆ๋Š” ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฃผ์†Œ์˜ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•ด์„œ ์‚ฌ์šฉํ•˜์„ธ์š”. 1 https://raw.githubusercontent.com/ychoi-kr/wikidocs-chobo-python/master/ch06/korean_words.txt ๊ทธ ๋ฐ–์˜ ๊ทœ์น™์€ ๋๋ง์ž‡๊ธฐ (2)์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ’€์ด ์ฝ”๋“œ: ch06/wordgame3.py โŸช Frequency lists by Neri โŸซ ๋ธ”๋กœ๊ทธ์˜ The 2000 Most Frequently Used Korean Nouns๋ฅผ ๊ตฌ๊ธ€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ๋กœ ๊ฐ€๊ณตํ•œ ๋’ค ์ˆ˜์ž‘์—…์œผ๋กœ ์†์งˆํ•˜๊ณ  ๋‹จ์–ด๋„ ์ข€ ๋” ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. โ†ฉ 6.2.3 ์—ฐ์Šต ๋ฌธ์ œ: ์˜์–ด ํ€ด์ฆˆ ๋ฌธ์ œ ko_en.txt ํŒŒ์ผ์—๋Š” ์šฐ๋ฆฌ๋ง๊ณผ ์˜์–ด ๋ฌธ์žฅ์ด ํƒญ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.(์˜ˆ๋ฌธ ์ถœ์ฒ˜: https://youtu.be/5AGUHrzYzgw) ์ด ํŒŒ์ผ์„ ์ด์šฉํ•ด ์˜์–ด ํ€ด์ฆˆ๋ฅผ ๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ํ™”๋ฉด์— ์šฐ๋ฆฌ๋ง๋กœ ๋œ ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•œ ๋‹ค์Œ, ์‚ฌ์šฉ์ž์—๊ฒŒ ์˜์–ด ๋ฌธ์žฅ์„ ์ž…๋ ฅ๋ฐ›๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ต์„ ๋งžํ˜”๋Š”์ง€ ํ™”๋ฉด์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ: Write the following sentence in English. ๊ทธ๋…€๋Š” ์ž‘๊ฐ€๊ฐ€ ์•„๋‹ˆ๋‹ค. your answer: She is not a writer. result: Correct! ---------------------------------------------------------------------- Write the following sentence in English. ๊ทธ๋“ค์€ ๊ต์‹ค์— ์žˆ๋‹ค. your answer: They are in the classroom. result: Correct! ---------------------------------------------------------------------- Write the following sentence in English. ๋„ˆ๋Š” ์ง‘์— ์žˆ๋‹ค. your answer: I am home. result: Not correct! right answer:You are home. ์ฝ”๋“œ: ch06/english_quiz.py 6.3 ํŒŒ์ผ์„ ์ž…๋ง›๋Œ€๋กœ(pickle, glob, os.path) ํŒŒ์ผ์„ ์ž…๋ง›๋Œ€๋กœ ์š”๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋น„๋ฒ•์„ ์ „์ˆ˜ํ•ด ๋“œ๋ฆฌ์ง€์š”. pickle ๋จผ์ € ์กฐ๊ธˆ ๋ณต์žกํ•œ ์ž๋ฃŒ๋ฅผ ํŒŒ์ผ์— ์“ฐ๊ณ  ์ฝ๋Š” ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ์•Œ์•„๋ด…์‹œ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” pickle ์ด๋ž€ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ”ผ์ž ๋จน์„ ๋•Œ ๋‚˜์˜ค๋Š” ํ”ผํด๊ณผ ์ด๋ฆ„์ด ๊ฐ™๋„ค์š”.^^ ์˜ˆ์ œ๋กœ๋Š” ํšŒ์›์˜ ID์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํŒŒ์ผ์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด ์•”ํ˜ธํ™”ํ•ด์„œ ์ €์žฅํ•ด์•ผ๊ฒ ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ํ‰๋ฌธ(plain text)์œผ๋กœ ์ €์žฅํ• ๊ฒŒ์š”. >>> users = {'kim':'3kid9', 'sun80':'393948', 'ljm':'py90390'} >>> f = open('users', 'wb') >>> import pickle >>> pickle.dump(users, f) >>> f.close() ์ฒ˜์Œ์— ID์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ users๋ผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ์— ๋‹ด์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  users๋ผ๋Š” ํŒŒ์ผ์„ ์ƒˆ๋กœ ์—ด์–ด์„œ f๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์˜€๊ณ ์š”. ์ด๋•Œ open() ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ wb๋Š” ์ผ๋ฐ˜ ํ…์ŠคํŠธ๊ฐ€ ์•„๋‹Œ ๋ฐ”์ดํŠธ(byte)<NAME>์œผ๋กœ ์“ฐ๊ฒ ๋‹ค(write)๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์—๋Š” pickle ๋ชจ๋“ˆ์˜ dump()๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ณต์‚ฌ์žฅ์— ํ™์„ ์‹ค์–ด ๋‚˜๋ฅด๋Š” ๋คํ”„ํŠธ๋Ÿญ์ด ๋’ค์ชฝ ์ง์นธ์„ ๋“ค์–ด ์˜ฌ๋ ค์„œ ํ™์„ ์™€๋ฅด๋ฅด ์Ÿ์•„๋‚ด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, dump๋Š” users๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ํŒŒ์ผ f์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์ด ์‹คํ–‰๋˜๋Š” ๊ฒฝ๋กœ์— users ํŒŒ์ผ์ด ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฑฐ์˜ˆ์š”. ์œˆ๋„ ํƒ์ƒ‰๊ธฐ์—์„œ ํ™•์ธํ•ด๋„ ๋˜์ง€๋งŒ, ์•„๋ž˜์™€ ๊ฐ™์ด ํŒŒ์ด์ฌ ์…ธ์—์„œ os.path.exists()๋กœ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์–ด์š”. >>> import os >>> os.path.exists('users') True ์ด์ œ ์ด ํŒŒ์ผ ๋‚ด์šฉ์„ ํŒŒ์ด์ฌ์—์„œ ์ฝ์–ด๋“ค์—ฌ ๋ณผ๊นŒ์š”? >>> f = open('users', 'rb') >>> a = pickle.load(f) >>> print(a) {'sun80': '393948', 'kim': '3kid9', 'ljm': 'py90390'} ์‚ฌ์‹ค ๋ฐฉ๊ธˆ ๋ณด์—ฌ๋“œ๋ฆฐ ๊ฒƒ์€ ๊ทธ๋ฆฌ ๋ณต์žกํ•  ๊ฒƒ๋„ ์—†์ง€๋งŒ, pickle ๋ชจ๋“ˆ์€ ํŒŒ์ด์ฌ์—์„œ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒƒ์€ ๋ญ๋“ ์ง€ ๋‹ค ํŒŒ์ผ์— ์ ์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ”ผํด์€ ์ด์ฏค ํ•ด๋‘๊ณ , ์ „์— ์ž ๊น ๊ตฌ๊ฒฝํ–ˆ๋˜ glob ๋ชจ๋“ˆ์„ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ฃ . glob glob๋Š” ํŒŒ์ผ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฝ‘์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ช…์„ ์ด์šฉํ•ด์„œ ์ž…๋ง›๋Œ€๋กœ ์š”๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. >>> from glob import glob >>> glob('*.exe') # ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜. exe ํŒŒ์ผ ['python.exe', 'pythonw.exe'] >>> glob('*.txt') # ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜. txt ํŒŒ์ผ ['LICENSE.txt', 'NEWS.txt'] ์œ„์˜ glob() ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ ๋ฐ›์€ ํŒจํ„ด๊ณผ ์ด๋ฆ„์ด ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํŒจํ„ด์„ ๊ทธ๋ƒฅ *๋ผ๊ณ  ์ฃผ๋ฉด ๋ชจ๋“  ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์–ด์š”. ํ˜„์žฌ ๊ฒฝ๋กœ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฒฝ๋กœ์— ๋Œ€ํ•ด์„œ๋„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> glob(r'C:\U*') # C:\์—์„œ ์ด๋ฆ„์ด U๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋‚˜ ํŒŒ์ผ์„ ์ฐพ๊ธฐ ['C:\\Users', 'C:\\usr'] ์œ„์—์„œ๋Š” r๋กœ ์‹œ์ž‘ํ•˜๋Š” ์›์‹œ(raw) ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์— ์œ ์˜ํ•˜์„ธ์š”. os.path ๋‹ค์Œ์€ glob๊ณผ ํ•จ๊ป˜ os.path ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. from glob import glob from os.path import isdir for x in glob('*'): if isdir(x): # ๋””๋ ‰ํ„ฐ๋ฆฌ์ธ๊ฐ€? print(x, '<DIR>') else: print(x) ์–ด๋–ค ์ผ์„ ํ•˜๋Š” ์ฝ”๋“œ์ธ์ง€ ์ง์ž‘์ด ๊ฐ€์‹œ๋Š”์ง€์š”? glob('*')์„ ์‚ฌ์šฉํ•ด ์–ป์€ ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ x๋ฅผ ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•˜๋˜, ๊ทธ๊ฒƒ์ด ๋””๋ ‰ํ„ฐ๋ฆฌ์ด๋ฉด <DIR>์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋’ค์— ๋ถ™์—ฌ์„œ ์ถœ๋ ฅํ•˜๊ฒŒ ํ–ˆ๋‹ต๋‹ˆ๋‹ค. ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. DLLs <DIR> Doc <DIR> img_read.py include <DIR> Lib <DIR> libs <DIR> LICENSE.txt NEWS.txt python.exe python3.dll python38.dll pythonw.exe Scripts <DIR> tcl <DIR> Tools <DIR> vcruntime140.dll 6.3.1 ์—ฐ์Šต ๋ฌธ์ œ: ์• ๊ตญ๊ฐ€ ๋ฌธ์ œ korean_national_anthem_1.txt ํŒŒ์ผ์—๋Š” ์• ๊ตญ๊ฐ€ 1์ ˆ ๊ฐ€์‚ฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™ํ•ด ๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก ํ•˜๋Š๋‹˜์ด ๋ณด์šฐํ•˜์‚ฌ ์šฐ๋ฆฌ๋‚˜๋ผ ๋งŒ์„ธ. ๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค ๊ฐ•์‚ฐ ๋Œ€ํ•œ ์‚ฌ๋žŒ, ๋Œ€ํ•œ์œผ๋กœ ๊ธธ์ด ๋ณด์ „ํ•˜์„ธ. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 4์ ˆ๊นŒ์ง€์˜ ๊ฐ€์‚ฌ๊ฐ€ ํ…์ŠคํŠธ ํŒŒ์ผ์— ๋‚˜๋‰˜์–ด ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. korean_national_anthem_2.txt korean_national_anthem_3.txt korean_national_anthem_4.txt ์ด ํŒŒ์ผ๋“ค์„ ์ฝ์–ด out.txt ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ €์žฅํ•˜๋Š” ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. korean_national_anthem_1.txt ---------------------------- ๋™ํ•ด ๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก ํ•˜๋Š๋‹˜์ด ๋ณด์šฐํ•˜์‚ฌ ์šฐ๋ฆฌ๋‚˜๋ผ ๋งŒ์„ธ. ๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค ๊ฐ•์‚ฐ ๋Œ€ํ•œ ์‚ฌ๋žŒ, ๋Œ€ํ•œ์œผ๋กœ ๊ธธ์ด ๋ณด์ „ํ•˜์„ธ. korean_national_anthem_2.txt ---------------------------- ๋‚จ์‚ฐ ์œ„์— ์ € ์†Œ๋‚˜๋ฌด, ์ฒ ๊ฐ‘์„ ๋‘๋ฅธ ๋“ฏ ๋ฐ”๋žŒ์„œ๋ฆฌ ๋ถˆ๋ณ€ํ•จ์€ ์šฐ๋ฆฌ ๊ธฐ์ƒ์ผ์„ธ. ๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค ๊ฐ•์‚ฐ ๋Œ€ํ•œ ์‚ฌ๋žŒ, ๋Œ€ํ•œ์œผ๋กœ ๊ธธ์ด ๋ณด์ „ํ•˜์„ธ. korean_national_anthem_3.txt ---------------------------- ๊ฐ€์„ ํ•˜๋Š˜ ๊ณตํ™œํ•œ๋ฐ ๋†’๊ณ  ๊ตฌ๋ฆ„ ์—†์ด ๋ฐ์€ ๋‹ฌ์€ ์šฐ๋ฆฌ ๊ฐ€์Šด ์ผํŽธ๋‹จ์‹ฌ์ผ์„ธ. ๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค ๊ฐ•์‚ฐ ๋Œ€ํ•œ ์‚ฌ๋žŒ, ๋Œ€ํ•œ์œผ๋กœ ๊ธธ์ด ๋ณด์ „ํ•˜์„ธ. korean_national_anthem_4.txt ---------------------------- ์ด ๊ธฐ์ƒ๊ณผ ์ด ๋ง˜์œผ๋กœ ์ถฉ์„ฑ์„ ๋‹คํ•˜์—ฌ ๊ดด๋กœ์šฐ๋‚˜ ์ฆ๊ฑฐ์šฐ๋‚˜ ๋‚˜๋ผ ์‚ฌ๋ž‘ํ•˜์„ธ. ๋ฌด๊ถํ™” ์‚ผ์ฒœ๋ฆฌ ํ™”๋ ค ๊ฐ•์‚ฐ ๋Œ€ํ•œ ์‚ฌ๋žŒ, ๋Œ€ํ•œ์œผ๋กœ ๊ธธ์ด ๋ณด์ „ํ•˜์„ธ. ์ฝ”๋“œ: ch06/text_file_merge.py 6.4 ์‘์šฉ ์˜ˆ์ œ: ์Œ์„ฑ ์ธ์‹์„ ํ™œ์šฉํ•œ ์ผ๋ณธ์–ด ํ€ด์ฆˆ ์š”์ฆ˜ ์ผ๋ณธ์–ด ๊ณต๋ถ€๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ์˜›๋‚ ์— ์ž ๊น ๋ฐฐ์šด ์ ์ด ์žˆ์ง€๋งŒ ๋„ˆ๋ฌด ์˜ค๋ž˜๋ผ์„œ ํžˆ๋ผ๊ฐ€๋‚˜๋„ ์žŠ์–ด๋ฒ„๋ ธ์–ด์š”. ใ…  ๊ทธ๋ž˜์„œ ์ผ๋ณธ์–ด ๊ณต๋ถ€๋ฅผ ๋„์™€์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ํŒŒ์ด์ฌ์œผ๋กœ ๋งŒ๋“ค์–ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌํ˜„ํ•˜๋ ค๋Š” ๊ธฐ๋Šฅ์€ ๋‹จ์ˆœํ•ฉ๋‹ˆ๋‹ค. ํ™”๋ฉด์— ์ผ๋ณธ์–ด๋กœ ์ถœ๋ ฅ๋œ ๋‹จ์–ด๋ฅผ ์ฝ์œผ๋ฉด, ๋งˆ์ดํฌ๋กœ ์†Œ๋ฆฌ๋ฅผ ๋ฐ›์•„์„œ ๋งž๊ฒŒ ์ฝ์—ˆ๋Š”์ง€ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ด์ฃ . ์Œ์„ฑ(speech)์„ ํ…์ŠคํŠธ(text)๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์˜์–ด ํ€ด์ฆˆ ์—ฐ์Šต ๋ฌธ์ œ์™€๋„ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์œผ๋กœ ์Œ์„ฑ์„ ํ…์ŠคํŠธ๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์„ ๊ฒ€์ƒ‰ํ•ด ๋ณด๋‹ˆ SpeechRecognition์ด๋ผ๋Š” ํŒจํ‚ค์ง€๊ฐ€ ์žˆ๋„ค์š”. ์ด๊ฒƒ์„ ์„ค์น˜ํ•˜๊ณ  ๊ฐ„๋‹จํžˆ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ค€๋น„ ์•„๋‚˜์ฝ˜๋‹ค ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ ์ €๋Š” ์•„๋‚˜์ฝ˜๋‹ค ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค์–ด์„œ ์ž‘์—…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ์—์„œ ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. conda create -n japanese_study Python=3.8 conda activate japanese_study SpeechRecognition ์„ค์น˜ pip๋กœ SpeechRecognition ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install SpeechRecognition PyAudio ์„ค์น˜ SpeechRecognition์—์„œ ๋งˆ์ดํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด PyAudio๋„ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Windows์—๋Š” ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install pipwin pipwin install pyaudio macOS์—๋Š” ๋‹ค์Œ ๋ช…๋ น์œผ๋กœ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. FLAC๋„ ํ•จ๊ป˜ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. xcode-select --install brew remove portaudio brew install portaudio brew install flac pip install pyaudio ๋งˆ์ดํฌ ํ…Œ์ŠคํŠธ ํŒŒ์ด์ฌ ์…ธ์„ ์‹คํ–‰ํ•˜๊ณ , ์•ž์—์„œ ์„ค์น˜ํ•œ ํŒจํ‚ค์ง€๋“ค์ด ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. >>> import speech_recognition as sr >>> sr.__version__ '3.7.1' >>> r = sr.Recognizer() >>> mic = sr.Microphone() ์˜์–ด: >>> with mic as source: ... audio = r.listen(source) ... >>> r.recognize_google(audio) 'hello how are you' ์˜์–ด๊ฐ€ ์ž˜ ์ธ์‹๋œ๋‹ค๋ฉด ํ•œ๊ตญ์–ด์™€ ์ผ๋ณธ์–ด๋„ ๋ณ„๋ฌธ์ œ ์—†์ด ์ž˜๋  ๊ฑฐ์˜ˆ์š”. ํ•œ๊ตญ์–ด: >>> with mic as source: ... audio = r.listen(source) ... >>> r.recognize_google(audio, language="ko-KR") '์•„ ์•„ ๋งˆ์ดํฌ ํ…Œ์ŠคํŠธ ํ•˜๋‚˜ ๋‘˜ ์…‹' ์ผ๋ณธ์–ด: >>> with mic as source: ... audio = r.listen(source) ... >>> r.recognize_google(audio, language="ja") 'ใŠใฏใ‚ˆใ†ใ”ใ–ใ„ใพใ™' ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ ์—ฌ๊ธฐ๊นŒ์ง€ ์ž˜ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์œผ๋‹ˆ ์ด์ œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํŒŒ์ผ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์€ ์™€์„ธ๋‹ค ๋Œ€ํ•™๊ต ์ผ๋ณธ์–ด ๊ธฐ์ดˆ ์ˆ˜์—… ์˜์ƒ์„ ์ฐธ๊ณ ํ•ด์„œ ์ œ๊ฐ€ ์ง์ ‘ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ํžˆ๋ผ๊ฐ€๋‚˜์™€ ์˜์–ด ๋ฐœ์Œ๋งŒ ๋„ฃ์—ˆ๋Š”๋ฐ, ๋‚˜์ค‘์— ํ…Œ์ŠคํŠธํ•˜๋‹ค ๋ณด๋‹ˆ ์Œ์„ฑ์ธ์‹ ๊ฒฐ๊ณผ๋Š” ์ฃผ๋กœ ํ•œ์ž๋กœ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํžˆ๋ผ๊ฐ€๋‚˜, ํ•œ์ž์–ด, ์˜์–ด ๋ฐœ์Œ์˜ ๋ฐ์ดํ„ฐ ํ˜•ํƒœ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ: ch06/japanese_word.csv ํŒŒ์ด์ฌ ์ฝ”๋“œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ: ch06/japanese_quiz.py import random import time import speech_recognition as sr f = open('japanese_words.csv', encoding='utf-8') lines = f.readlines() random.shuffle(lines) r = sr.Recognizer() mic = sr.Microphone() for line in lines: hiragana, kanji, pronunciation = line.split(',') correct = None # ํ‹€๋ฆฌ๋ฉด ๋งžํž ๋•Œ๊นŒ์ง€ ๊ฐ™์€ ๋‹จ์–ด๋ฅผ ๋ฐ˜๋ณต while not correct: if correct is None: print('Read this word!:') else: print('Let\'s try again:') print(hiragana) # ๋‹ต์„ ์ƒ๊ฐํ•˜๋Š๋ผ ๋จธ๋ญ‡๊ฑฐ๋ฆฌ๊ณ  ์žˆ์œผ๋ฉด ์Œ์„ฑ ์ธ์‹์ด ์ข…๋ฃŒ๋˜์–ด ๋ฒ„๋ ค์„œ # ์—”ํ„ฐ ํ‚ค ์ž…๋ ฅ ํ›„๋ถ€ํ„ฐ ์ธ์‹ํ•˜๊ฒŒ ํ•˜๋ ค๊ณ  ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค. input('Press Enter when you ready and then read it ...') with mic as source: audio = r.listen(source) a = r.recognize_google(audio, language='ja') print('Your answer:', a) if kanji == a: correct = True print('You\'re right!') else: correct = False print(hiragana, 'is pronounced', pronunciation) print('-' * 20) time.sleep(2) ์‹คํ–‰ ๊ฒฐ๊ณผ: (japanese_study) C:\wikidocs-chobo-python\ch06>python japanese_quiz.py Read this word!: ใ•ใ‹ใช Press Enter when you ready and then read it ... Your answer: ใŸใ‹ใช ใ•ใ‹ใช is pronounced sakana Let's try again: ใ•ใ‹ใช Press Enter when you ready and then read it ... Your answer: ้ญš You're right! -------------------- Read this word!: ใ‚ใ• Press Enter when you ready and then read it ... Your answer: ็พฝ็”ฐ ใ‚ใ• is pronounced asa Let's try again: ใ‚ใ• Press Enter when you ready and then read it ... Your answer: ็พฝ็”ฐ ใ‚ใ• is pronounced asa Let's try again: ใ‚ใ• Press Enter when you ready and then read it ... Your answer: ๆœ You're right! -------------------- Read this word!: ใ‹ใŠ Press Enter when you ready and then read it ... Process finished with exit code -1 ํ…Œ์ŠคํŠธ ์˜์ƒ: https://youtu.be/AqWlbhSk7A8 ์ฐธ๊ณ  ๋‹ค์Œ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค. https://realpython.com/python-speech-recognition/ https://stackoverflow.com/a/59048571/1558946 7. ๊ฐ์ฒด์ง€ํ–ฅ ๊ฐ์ฒด์ง€ํ–ฅ(Object-Oriented) ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํŒŒ์ด์ฌ์—์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค. ์ด ์žฅ์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฒƒ: ํด๋ž˜์Šค์™€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ ์ƒ์† ๊ฐ์ฒด ์†์˜ ๊ฐ์ฒด ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ๋“ค 7.1. ํด๋ž˜์Šค(class)์™€ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด์ง€ํ–ฅ ๊ฐœ๋…์ด ๋‚˜ํƒ€๋‚˜๊ธฐ ์ด์ „์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ๋ฒ•์—์„œ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์–ด๋–ค ์ผ์„ ํ•˜๊ณ  ๋‚˜์„œ, ๊ทธ๋‹ค์Œ์—” ์–ด๋–ค ์ผ์„ ํ•˜๊ณ , ๋˜ ๊ทธ๋‹ค์Œ์—” ๋ญ˜ ํ•˜๋ผ๋Š” ์‹์œผ๋กœ ์ปดํ“จํ„ฐ๊ฐ€ ํ•ด์•ผ ํ•  ์ผ์„ ์•Œ๋ ค์ฃผ๊ธฐ์— ๋ฐ”๋นด์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Object-Oriented Programming)์—์„œ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ๋Œ€์ƒ์ด ๋˜๋Š” ์‹ค์ œ ์„ธ๊ณ„์˜ ์‚ฌ๋ฌผ(๊ฐ์ฒด)์„ ๊ทธ๋Œ€๋กœ ํ‘œํ˜„ํ•˜๊ณ , ๊ทธ๊ฒƒ๋“ค์ด ์–ด๋–ป๊ฒŒ ์›€์ง์ด๋Š”์ง€ ์ •ํ•ด์ฃผ๊ณ  ๋‚˜์„œ์•ผ ๋น„๋กœ์†Œ ๊ทธ ๊ฐ์ฒด๋“ค์—๊ฒŒ ์ผ์„ ์‹œํ‚ต๋‹ˆ๋‹ค. ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ž˜ ์‚ฌ์šฉํ•˜๋ฉด ๋ณด๋‹ค ์ข‹์€ ํ”„๋กœ๊ทธ๋žจ์„ ๋นจ๋ฆฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๋‚˜์ค‘์— ์ˆ˜์ •ํ•˜๊ธฐ๋„ ํŽธํ•ด์ง„๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋“ค์ด ๋ชจ๋‘ ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ์š”์ฆ˜์— ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด ์ค‘์—๋Š” ๊ฐ์ฒด์ง€ํ–ฅ์„ ์ง€์›ํ•˜๋Š” ๊ฒƒ์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์€ ๊ผญ ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋ง์”€๋“œ๋ ค์„œ, ์•ž์œผ๋กœ์˜ ๊ฐ•์ขŒ๋ฅผ ๋ณด์ง€ ์•Š์œผ์…”๋„ ๊ฐ„๋‹จํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์ด์ฃ . ํ•˜์ง€๋งŒ ๊ฐ์ฒด์ง€ํ–ฅ์— ๋Œ€ํ•ด ์ดํ•ดํ•˜์‹œ๊ณ  ๋‚˜๋ฉด ํŒŒ์ด์ฌ์œผ๋กœ ์œˆ๋„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๊ฑฐ๋‚˜, ๋ณต์žกํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ๋งŽ์€ ๋„์›€์ด ๋œ๋‹ต๋‹ˆ๋‹ค. ๋˜, ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์ดํ•ดํ•˜๊ธฐ๋„ ์ˆ˜์›”ํ•ด์ง€์ง€์š”. ๊ทธ๋Ÿผ ํ•จ๊ป˜ ์‹œ์ž‘ํ•ด ๋ณผ๊นŒ์š”? ํด๋ž˜์Šค? ์ธ์Šคํ„ด์Šค? โ€˜๊น€์—ฐ์•„โ€™๋Š” ์‹ค์ œ๋กœ ์กด์žฌํ•˜์ฃ ? ๋„ค, ์—ฌ๋Ÿฌ๋ถ„์ด ์ƒ๊ฐํ•˜์‹œ๋Š” ๊ทธ ๊น€์—ฐ์•„ ๋งž์•„์š”. ใ…Žใ…Ž โ€˜๊น€๋™์„ฑโ€™๋„ ์‹ค์ œ๋กœ ์กด์žฌํ•˜์ฃ ? ๋‘ ์‚ฌ๋žŒ ๋‹ค ์‹ค์ œ๋กœ ์กด์žฌํ•˜๋Š” ์‚ฌ๋žŒ์ž…๋‹ˆ๋‹ค. ๋‘ ์‚ฌ๋žŒ์˜ ๊ณตํ†ต์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”? ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ฅผ ๋“ค ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ ๋‘˜ ๋‹ค โ€˜์Šค์ผ€์ดํ„ฐโ€™๋ผ๋Š” ๊ณตํ†ต์ ์„ ๊ฐ–๊ณ  ์žˆ์ง€์š”. โ€˜์Šค์ผ€์ดํ„ฐโ€™๋ผ๋Š” ๋‹จ ํ•˜๋‚˜์˜ ์‚ฌ๋žŒ์ด๋‚˜ ๋ฌผ๊ฑด์ด ์‹ค์ œ๋กœ ์กด์žฌํ• ๊นŒ์š”? ๊ทธ๋ ‡์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” โ€˜์Šค์ผ€์ดํŠธ ํƒ€๋Š” ์‚ฌ๋žŒโ€™์„ โ€˜์Šค์ผ€์ดํ„ฐโ€™๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์„ ์ผ์ปซ๋Š” ๋ง์ด ํด๋ž˜์Šค(class)์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋ง๋กœ ์˜ฎ๊ธฐ๊ธฐ๋Š” ์‰ฝ์ง€ ์•Š์ง€๋งŒ โ€˜๋ถ€๋ฅ˜โ€™๋ผ๋Š” ์˜๋ฏธ๋กœ ์ƒ๊ฐํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. ๋‹ค๋ฅธ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณผ๊นŒ์š”? โ€˜์‚ฌ๊ณผโ€™๋Š” ํด๋ž˜์Šค์ด๊ณ ์š”, โ€˜๋‚ด๊ฐ€ ์—Š์ €๋…์— ๋จน์€ ์‚ฌ๊ณผ ๋‹ค์„ฏ ๊ฐœ ์ค‘์— ๋‘ ๋ฒˆ์งธ ๊ฒƒโ€™์ด๋ผ๊ณ  ์ฝ• ์ฐ์–ด์„œ ๋งํ•ด์ฃผ๋ฉด ์‹ค์ฒด(instance)๋กœ ๋ด์ค„ ๋งŒํ•ฉ๋‹ˆ๋‹ค. โ€˜์ข‹์€ ์ง‘โ€™์€ ์‹ค์ฒด์ผ๊นŒ์š”? ์–ด๋Š ํ•œ ์ง‘๋งŒ์„ ์ฝ• ์ฐ์–ด์„œ โ€˜์ข‹์€ ์ง‘โ€™์ด๋ผ๊ณ  ํ•˜๊ธฐ๋Š” ํž˜๋“ค ๊ฒƒ ๊ฐ™๊ตฐ์š”. ๊ทธ๋Ÿผ โ€˜์šฐ๋ฆฌ ์ง‘โ€™์€ ์‹ค์ฒด์ผ๊นŒ์š”? ๊ทธ๊ฑด ์‹ค์ฒด๋ผ๊ณ  ํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™๋„ค์š”. ๋‹จ, ์ง‘์„ ์—ฌ๋Ÿฌ ์ฑ„ ๊ฐ€์ง„ ์‚ฌ๋žŒ์ด โ€˜์šฐ๋ฆฌ ์ง‘โ€™์ด๋ผ๊ณ  ๋งํ•  ๋•Œ๋Š” ์ •ํ™•ํžˆ ์–ด๋Š ์ง‘์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ธ์ง€ ์•Œ ์ˆ˜ ์—†๊ฒ ์ฃ . ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ์„ ์œ„ํ•ด ํด๋ž˜์Šค๋ฅผ ์„ค๊ณ„ํ•˜๋‹ค ๋ณด๋ฉด ์ด๋Ÿฐ ์• ๋งคํ•œ ๋ฌธ์ œ๋ฅผ ๋งŒ๋‚  ๋•Œ๋„ ์žˆ์ง€์š”. ํŒŒ์ด์ฌ์˜ ํด๋ž˜์Šค ์ด์ œ ํŒŒ์ด์ฌ์œผ๋กœ ๋ถ€๋ฅ˜์™€ ์‹ค์ฒด๋ฅผ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> class Singer: # ๊ฐ€์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ฒ ๋Š๋‹ˆ๋ผโ€ฆ ... def sing(self): # ๋…ธ๋ž˜ํ•˜๊ธฐ ๋ฉ”์„œ๋“œ ... return "Lalala~" ... >>> taeji = Singer() # ํƒœ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ผ! >>> taeji.sing() # ๋…ธ๋ž˜ ํ•œ ๊ณก ๋ถ€ํƒํ•ด์š”~ 'Lalala~' ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ์œ„์™€ ๊ฐ™์ด class ํด๋ž˜์Šค ์ด๋ฆ„:<NAME>์œผ๋กœ ์‹œ์ž‘ํ•ด์„œ ๊ทธ๋‹ค์Œ๋ถ€ํ„ฐ ๊ทธ ํด๋ž˜์Šค์˜ ์„ฑ์งˆ์ด๋‚˜ ํ–‰๋™์„ ์ •์˜ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์ฃ ? ์ด์™€ ๊ฐ™์ด ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ •์˜๋œ ํ•จ์ˆ˜๋ฅผ ๋ฉ”์„œ๋“œ(method)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ sing ๋ฉ”์„œ๋“œ๋Š” Singer๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ ํ•˜๋Š” ํ–‰๋™์„ ์ •์˜ํ•˜๊ณ  ์žˆ์ฃ . Singer ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“  ๋‹ค์Œ์—๋Š” taeji๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ธ์Šคํ„ด์Šค๋ช… = ํด๋ž˜์Šค()์™€ ๊ฐ™์ด ๋งŒ๋“ค๋ฉด ๋˜์ฃ . ๊ทธ๋‹ค์Œ์—” ๊ทธ๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ taeji์—๊ฒŒ ๋…ธ๋ž˜๋ฅผ ์‹œ์ผœ๋ดค์Šต๋‹ˆ๋‹ค. Singer ํด๋ž˜์Šค์— sing ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•ด ์คฌ๊ธฐ ๋•Œ๋ฌธ์— Singer ํด๋ž˜์Šค์— ์†ํ•œ taeji ๊ฐ์ฒด๋„ sing ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€์š”. ๋‹ค์‹œ ๋งํ•ด์„œ ๊ฐ€์ˆ˜๋Š” ๋…ธ๋ž˜ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ํƒœ์ง€๋ผ๋Š” ๊ฐ€์ˆ˜๋„ ์—ญ์‹œ ๋…ธ๋ž˜๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์–ด๋–ค ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ฐ์ฒด. ๋ฉ”์„œ๋“œ<NAME>์œผ๋กœ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—” ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฆฌํ‚ค ๋งˆํ‹ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋…ธ๋ž˜๋ฅผ ์ฒญํ•ด๋ณด์„ธ์š”. >>> ricky = Singer() >>> ricky.sing() 'Lalala~' ๋‹ค๋ฅธ ํด๋ž˜์Šค๋“ค๋„ ํ•œ ๋ฒˆ์”ฉ ๋งŒ๋“ค์–ด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. 7.2. ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ ์ง€๋‚œ ์‹œ๊ฐ„์— ํด๋ž˜์Šค์™€ ๊ฐ์ฒด๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ž ๊น ์‚ดํŽด๋ณด์•˜์ฃ ? ์‹ค์ œ ์„ธ๊ณ„์— ์กด์žฌํ•˜๋Š” ์‹ค์ฒด(instance)๋ฅผ ๊ฐ์ฒด(object)๋ผ๊ณ  ํ•˜๊ณ , ๊ฐ์ฒด๋“ค์˜ ๊ณตํ†ต์ ์„ ๊ฐ„์ถ”๋ ค์„œ ๊ฐœ๋…์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด ํด๋ž˜์Šค(class)๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด ๊ทธ ๊ฐ์ฒด๊ฐ€ ๊ฐ–๋Š” ์„ฑ์งˆ๊ณผ ๊ทธ ๊ฐ์ฒด๊ฐ€ ํ•˜๋Š” ํ–‰๋™์„ ์ •์˜ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค๊ณ ๋„ ํ–ˆ๊ณ ์š”. ๋””์•„๋ธ”๋กœ2 ๊ฒŒ์ž„์˜ ์•„๋งˆ์กด์ด๋ผ๋Š” ์บ๋ฆญํ„ฐ๋ฅผ ํด๋ž˜์Šค๋กœ ํ‘œํ˜„ํ•ด ๋ณผ๊นŒ์š”? class Amazon: strength = 20 dexterity = 25 vitality = 20 energy = 15 def attack(self): return 'Jab!!!' ์•„๋งˆ์กด ํด๋ž˜์Šค๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ํž˜, ๊ธฐ์ˆ , ์ฒด๋ ฅ, ์—๋„ˆ์ง€๋ผ๋Š” ๋„ค ๊ฐ€์ง€ ์„ฑ์งˆ์€ ๋ณ€์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ด์—ˆ๊ณ ์š”, '๊ณต๊ฒฉ'ํ•˜๋Š” ํ–‰๋™์€ ๋ฉ”์„œ๋“œ๋กœ ๋‚˜ํƒ€๋‚ด์—ˆ์Šต๋‹ˆ๋‹ค. IDLE(Python GUI)์—์„œ File - New Window ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜์—ฌ ์ƒˆ ์ฐฝ์„ ๋„์šฐ๊ณ  ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ diablo2.py๋ผ๋Š” ํŒŒ์ผ๋กœ ์ €์žฅํ•ด ์ฃผ์„ธ์š”. F5 ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ ๋ชจ๋“ˆ์„ ์‹คํ–‰ํ•œ ๋‹ค์Œ, Shell ์ฐฝ์œผ๋กœ ๋˜๋Œ์•„๊ฐ€์„œ diablo2 ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•œ ๋‹ค์Œ, Amazon ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค. >>> import diablo2 >>> jane = diablo2.Amazon() >>> mary = diablo2.Amazon() ๋‘ ๋ช…์˜ ์—ฌ์ „์‚ฌ๊ฐ€ ํƒ„์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์งœ์ž”~ jane๊ณผ mary๋Š” ๋‘˜ ๋‹ค Amazon์œผ๋กœ์„œ ํ•„์š”ํ•œ ์ž์งˆ์„ ๋ชจ๋‘ ๊ฐ–์ถ”๊ณ  ์žˆ๊ฒ ์ฃ ? ๊ทธ๋ ‡๋‹ค๋ฉด jane์˜ ํž˜๋„, ๊ณต๊ฒฉํ•˜๋Š” ํ–‰๋™๋„ Amazon ํด๋ž˜์Šค์—์„œ ์ •์˜ํ•œ ๊ทธ๋Œ€๋กœ์ด๊ฒ ๊ณ ์š”. >>> jane.strength 20 >>> jane.attack() 'Jab!!!' ์ด๋ ‡๊ฒŒ ๊ฐ์ฒด๋Š” ํด๋ž˜์Šค์—์„œ ์ •์˜ํ•ด ์ค€ ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ณ„๋กœ ์–ด๋ ต์ง€ ์•Š์ฃ ? ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์„œ ๋ณด์‹ค ๊ฒƒ์€ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•  ๋•Œ์™€ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. Amazon ํด๋ž˜์Šค์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•  ๋•Œ๋Š” def attack(self):์™€ ๊ฐ™์ด self๋ผ๋Š” ์ธ์ž๋ฅผ ๋ฐ›์•˜๋Š”๋ฐ, jane ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ๊ทธ๋ƒฅ attack()์ด๋ผ๊ณ  ํ–ˆ์ง€์š”? tip IDLE์—์„œ Run Module ์•ˆ ๋  ๋•Œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• https://yong-it.blog spot.com/2018/10/python-idle.html self self๋ผ๋Š” ๊ฒƒ์€ ๋ฐ”๋กœ ๊ทธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”๋ฐ, jane๊ณผ mary๊ฐ€ ๋˜‘๊ฐ™์€ attack ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•œ๋งˆ๋””๋กœ, ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•  ๋•Œ๋Š” ํ•ญ์ƒ self๋ผ๋Š” ์ธ์ž๋ฅผ ์จ์ค€๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋˜๊ฒ ๋„ค์š”. self๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์ข€ ๋” ์‚ดํŽด๋ณด๊ธฐ ์œ„ํ•ด Amazon ํด๋ž˜์Šค์— ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. def exercise(self): self.strength += 2 self.dexterity += 3 self.vitality += 1 ์ด ๋ฉ”์„œ๋“œ๋Š” ํ›ˆ๋ จ์„ ํ•˜๋ฉด ํž˜, ๊ธฐ์ˆ , ์ฒด๋ ฅ์ด ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ–ˆ์ง€์š”. diablo2.py ํŒŒ์ผ์˜ Amazon ํด๋ž˜์Šค์— ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์ €์žฅํ•ด ์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ €์žฅํ•˜์…จ์œผ๋ฉด F5 ํ‚ค๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ˆŒ๋Ÿฌ์„œ diablo2 ๋ชจ๋“ˆ์„ ๋‹ค์‹œ ์‹คํ–‰ํ•˜๊ณ , ์ž„ํฌํŠธ๋„ ํ•ด์ค€ ๋‹ค์Œ์—, ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ›ˆ๋ จ์„ ์‹œ์ผœ๋ณด์„ธ์š”. >>> import diablo2 >>> eve = diablo2.Amazon() >>> eve.exercise() >>> eve.strength 22 ํž˜(strength)์ด ์„ธ์กŒ๋„ค์š”. ํ›ˆ๋ จํ•œ ๋ณด๋žŒ์ด ์žˆ์ง€์š”? 7.3. ์ƒ์† ์ง€๋‚œ ๋‘ ๊ฐ•์ขŒ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ์ง€ํ–ฅ์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•ด ๋“œ๋ ธ๋Š”๋ฐ, ์—ฌ๋Ÿฌ๋ถ„์€ ์–ด๋–ป๊ฒŒ ๋Š๋ผ์…จ๋Š”์ง€์š”. ๊ฐœ๋…์ ์œผ๋กœ๋Š” ์ดํ•ด๊ฐ€ ๋  ๋“ฏ๋„ ํ•œ๋ฐ, ์‹ค์ œ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ์ ์šฉํ•ด์•ผ ํ• ์ง€ ๋‚œ๊ฐํ•ด ํ•˜์‹œ๋Š” ๋ถ„์ด ๋งŽ์ง€ ์•Š์„๊นŒ ํ•˜๋Š” ์ƒ๊ฐ๋„ ๋“œ๋Š”๊ตฐ์š”. ์‚ฌ์‹ค ๊ฐ์ฒด์ง€ํ–ฅ์˜ ๊ฐœ๋…์„ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๊ณ  ํ™œ์šฉํ•˜๊ธฐ๊นŒ์ง€๋Š” ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆฐ๋‹ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋„ˆ๋ฌด ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์‹œ๊ณ  ์ฒœ์ฒœํžˆ, ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณตํ•ด์„œ ๊ณต๋ถ€ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์•„๋ฌด๋ฆฌ ์ข‹์€ ๊ฐœ๋…๋„ ๊ฐ‘์ž๊ธฐ ๋งŽ์ด ๊ณต๋ถ€ํ•˜๋ฉด ์˜คํžˆ๋ ค ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‰ฝ์ง€์š”. ๊ทธ๋Ÿฌ๋‹ˆ ์ œ๊ฐ€ ์„ค๋ช…๋“œ๋ฆฌ๋Š” ๊ฒƒ๋ถ€ํ„ฐ ํ”„๋กœ๊ทธ๋žจ์— ์ ์šฉ์‹œ์ผœ ๋ณด๋ฉด์„œ ์ฐจ๊ทผ์ฐจ๊ทผ ๊ณต๋ถ€ํ•˜์…จ์œผ๋ฉด ํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋Š˜ ์•Œ์•„๋ณผ ๋‚ด์šฉ์€ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํ•ต์‹ฌ์ ์ธ ๊ฐœ๋… ๊ฐ€์šด๋ฐ ํ•˜๋‚˜์ธ ์ƒ์†(inheritance)์ž…๋‹ˆ๋‹ค. ์ƒ์†์ด๋ž€ ์–ด๋–ค ํด๋ž˜์Šค๊ฐ€ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ์„ฑ์งˆ์„ ๋ฌผ๋ ค๋ฐ›๋Š” ๊ฒƒ์„ ๋งํ•˜์ง€์š”. ์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ชจ๋“  ๊ฒƒ์„ ์ƒˆ๋กœ ๋งŒ๋“ค ํ•„์š” ์—†์ด, ํ•ต์‹ฌ์ ์ธ ์„ฑ์งˆ์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†์„ ๋ฐ›์•„์„œ ์กฐ๊ธˆ๋งŒ ์†์„ ๋ณด๋ฉด ์“ธ๋งŒํ•œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. ์˜ˆ์ œ๋ฅผ ๋ณด์‹ค๊นŒ์š”? class Person: # ๋ˆˆ ๋‘ ๊ฐœ, ์ฝ” ํ•˜๋‚˜, ์ž… ํ•˜๋‚˜... eyes = 2 nose = 1 mouth = 1 ears = 2 arms = 2 legs = 2 # ๋จน๊ณ  ์ž๊ณ  ์ด์•ผ๊ธฐํ•˜๊ณ ... def eat(self): print('์–Œ๋ƒ ...') def sleep(self): print('์ฟจ์ฟจ...') def talk(self): print('์ฃผ์ ˆ์ฃผ์ ˆ...') ์œ„์˜ Person์ด๋ผ๋Š” ํด๋ž˜์Šค๋Š” ๋ณดํ†ต ์‚ฌ๋žŒ์„ ๋‚˜ํƒ€๋‚ธ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ๋ˆˆ, ์ฝ”, ์ž…, ํŒ”๋‹ค๋ฆฌ๊ฐ€ ๋‹ค ์žˆ๊ณ , ๋จน๊ณ , ์ž๊ณ  ์ด์•ผ๊ธฐ๋„ ํ•˜์ง€์š”. ์ด๋ฒˆ์—๋Š” ํ•™์ƒ์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค. ํ•™์ƒ๋„ ์‚ฌ๋žŒ์ด๋‹ˆ๊นŒ ์‚ฌ๋žŒ์ด ๊ฐ–๋Š” ์—ฌ๋Ÿฌ ์„ฑ์งˆ์ด๋‚˜ ํ–‰๋™์€ ๋ชจ๋‘ ๊ฐ–๊ณ  ์žˆ์„ ๊ฒƒ์ด๊ณ , ๊ฑฐ๊ธฐ์— ํ•™์ƒ๋งŒ์˜ ํŠน์ง•์„ ์ข€ ๋” ๊ฐ–๋„๋ก ํ•˜๋ฉด ๋˜๊ฒ ์ง€์š”? ํ•˜์ง€๋งŒ, ์‚ฌ๋žŒ ํด๋ž˜์Šค๋„ ํ•œ์ฐธ ๊ฑธ๋ ค์„œ ์ž…๋ ฅํ–ˆ๋Š”๋ฐ, ๋˜๋‹ค์‹œ ํ•™์ƒ ํด๋ž˜์Šค์˜ ๋ˆˆ, ์ฝ”, ์ž…๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋ชจ๋“  ๊ฒƒ์„ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋ ค๋ฉด ๋„ˆ๋ฌด ๊ท€์ฐฎ๊ฒ ๋„ค์š”. ๋ฐ”๋กœ ์ด๋Ÿด ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์†์„ ์ด์šฉํ•˜๋ฉด ์†์‰ฝ๊ฒŒ ํ•™์ƒ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. class Student(Person): # Person ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์Œ def study(self): print('์—ด๊ณต์—ด๊ณต...') ์œ„์˜ Student ํด๋ž˜์Šค๋Š” Person ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ Student ํด๋ž˜์Šค๋ฅผ ๋ˆˆ, ์ฝ”, ์ž…๋ถ€ํ„ฐ ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค์‹œ ๋งŒ๋“ค์–ด ์ฃผ์ง€ ์•Š๋”๋ผ๋„ Person์˜ ์„ฑ์งˆ๋“ค์„ ๋ชจ๋‘ ๋ฌผ๋ ค๋ฐ›์•„์„œ ๊ฐ–๊ฒŒ ๋œ ๊ฒƒ์ด์ฃ . ์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ์— study๋ผ๋Š” ๋ฉ”์„œ๋“œ๋งŒ ํ•˜๋‚˜ ๋” ์จ์ฃผ์–ด์„œ ์šฐ์•„ํ•˜๊ฒŒ ๋งˆ๋ฌด๋ฆฌ๋ฅผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ตณ์ด ์ƒ์†์„ ๋ฐ›์ง€ ๋ง๊ณ  ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ๋ถ™์ด๋ฉด ๋˜์ง€ ์•Š๋Š๋ƒ๊ณ ์š”? ๋ฌผ๋ก  ๊ทธ๋ ‡๊ฒŒ ํ•ด๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‚˜์ค‘์— ์‚ฌ๋žŒ๊ณผ ํ•™์ƒ ํด๋ž˜์Šค์— '์˜ท ์ƒ‰๊น”'์ด๋ผ๋“ ์ง€, '์‹ธ์šฐ๋‹ค' ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์–ด์ง„๋‹ค๋ฉด, ๊ทธ๋•Œ๋งˆ๋‹ค ์‚ฌ๋žŒ ํด๋ž˜์Šค์™€ ํ•™์ƒ ํด๋ž˜์Šค๋ฅผ ๊ฐ๊ฐ ์ˆ˜์ •ํ•ด์•ผ ๋˜๊ฒ ์ง€์š”. ์‚ฌ๋žŒ๊ณผ ํ•™์ƒ์˜ ๊ด€๊ณ„๋ฅผ ์œ„์™€ ๊ฐ™์ด ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ๊ฐํ˜•์€ ๊ฐ๊ฐ์˜ ํด๋ž˜์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , ๊ทธ ์•ˆ์— ํด๋ž˜์Šค์˜ ์ด๋ฆ„๊ณผ ๋ณ€์ˆ˜, ๋ฉ”์„œ๋“œ๋ฅผ ์ ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ํ™”์‚ดํ‘œ๋Š” '์ƒ์† ๊ด€๊ณ„'๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , ๊ทธ ๋ฐฉํ–ฅ์€ ํ•˜์œ„ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์œ„ ํด๋ž˜์Šค๋ฅผ ํ–ฅํ•ฉ๋‹ˆ๋‹ค. ์ด ํ™”์‚ดํ‘œ๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ 'is a'๋ผ๊ณ  ์ฝ์œผ๋ฉด ๋‘ ํด๋ž˜์Šค์˜ ๊ด€๊ณ„๋ฅผ ์‰ฝ๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์ง€์š”. "A Student is a Person.(ํ•™์ƒ์€ ์‚ฌ๋žŒ์ด๋‹ค)"์ด ๋˜๋Š”๊ตฐ์š”. ํด๋ž˜์Šค๋“ค์˜ ๊ด€๊ณ„๋ฅผ ์ด๋ ‡๊ฒŒ ๊ทธ๋ฆผ์œผ๋กœ ๊ทธ๋ฆฌ๋ฉด ํด๋ž˜์Šค๋ฅผ ์„ค๊ณ„ํ•˜๊ฑฐ๋‚˜ ๋ถ„์„ํ•  ๋•Œ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋‹ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ, ๊ณผ์—ฐ Student ํด๋ž˜์Šค๊ฐ€ Person ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์„ฑ์งˆ์„ ๋˜‘๊ฐ™์ด ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒƒ์ธ์ง€๋„ ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜์ฃ . ๋จผ์ € ์œ„์˜ ์˜ˆ์ œ๋“ค์„ ํŒŒ์ผ๋กœ ์ €์žฅํ•ด์„œ import ํ•˜์‹œ๊ฑฐ๋‚˜, ๊ทธ๋ƒฅ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์ž…๋ ฅํ•˜์‹  ๋‹ค์Œ์— ์•„๋ž˜์™€ ๊ฐ™์ด ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์„ธ์š”. >>> lee = Person() >>> lee.mouth >>> lee.talk() ์ฃผ์ ˆ์ฃผ์ ˆ... >>> kim = Student() >>> kim.mouth >>> kim.talk() ์ฃผ์ ˆ์ฃผ์ ˆ... Person ํด๋ž˜์Šค์˜ ๊ฐ์ฒด์ธ lee์™€ Student ํด๋ž˜์Šค์˜ ๊ฐ์ฒด kim์ด ํ•˜๋Š” ์ง“๋“ค์ด ๋˜‘๊ฐ™์ง€์š”? Person ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ ‡๋‹ค๋Š” ๊ฒƒ์„ ์•„์‹ค ์ˆ˜ ์žˆ๊ฒ ์ฃ ? ๊ทธ๋Ÿฌ๋‚˜, ์ƒ์†๋ฐ›์€ ๊ฒƒ์œผ๋กœ ๋์ด ์•„๋‹ˆ์ง€์š”โ€ฆ Student๋Š” ๊ณต๋ถ€๋ผ๋Š” ๋น„์žฅ์˜ ์นด๋“œ๋„ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š์•˜๊ฒ ์Šต๋‹ˆ๊นŒ? >>> kim.study() ์—ด๊ณต์—ด๊ณต... ์—ญ์‹œ kim์€ ํ•™์ƒ๋‹ต๊ฒŒ ๊ณต๋ถ€๋„ ์—ด์‹ฌํžˆ ํ•˜๋Š”๊ตฐ์š”โ€ฆ 7.3.1 ๋ฉ”์„œ๋“œ ์ƒ์†๊ณผ ์žฌ์ •์˜ ์ด๋ฒˆ์—๋Š” ๋™๋ฌผ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค๋“ค์„ ๋งŒ๋“ค์–ด ๋ณผ๊ฒŒ์š”. ์ฝ”๋“œ: ch07/animal.py class ๋™๋ฌผ: def ์šธ์–ด(self): print('...') class ๊ณ ์–‘์ž‡๊ณผ(๋™๋ฌผ): pass class ํ˜ธ๋ž‘์ด(๊ณ ์–‘์ž‡๊ณผ): def ์šธ์–ด(self): print('์–ดํฅ') class ๊ณ ์–‘์ด(๊ณ ์–‘์ž‡๊ณผ): def ์šธ์–ด(self): print('์•ผ์˜น') ์ด ํด๋ž˜์Šค๋“ค์˜ ์ƒ์† ๊ด€๊ณ„๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์ด๋ ‡์Šต๋‹ˆ๋‹ค. animal.py ๋ชจ๋“ˆ์ด ์žˆ๋Š” ๊ณณ์—์„œ ํŒŒ์ด์ฌ ์…ธ์„ ์—ด์–ด์„œ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. >>> import animal ๋‚˜๋น„๋ผ๋Š” ์ด๋ฆ„์˜ ๊ณ ์–‘์ด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. (์ƒˆ๋กœ์šด ๊ณ ์–‘์ด ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๋ฏ€๋กœ ๊ด„ํ˜ธ๋ฅผ ๋ถ™์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.) >>> ๋‚˜๋น„ = animal. ๊ณ ์–‘์ด() ๋‚˜๋น„๋Š” ์•ผ์˜น ํ•˜๊ณ  ์šธ์–ด์š”. >>> ๋‚˜๋น„. ์šธ์–ด() ์•ผ์˜น ์ด๋ฒˆ์—๋Š” ํ˜ธ๋Œ์ด๋ผ๋Š” ํ˜ธ๋ž‘์ด๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ๊นŒ์š”? >>> ํ˜ธ๋Œ์ด = animal. ํ˜ธ๋ž‘์ด() >>> ํ˜ธ๋Œ์ด. ์šธ์–ด() ์–ดํฅ ์ˆ˜ํ˜ธ๋ž‘์ด๋ผ๋Š” ์ด๋ฆ„์˜ ํ˜ธ๋ž‘์ด๋„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. isinstance() ์œ„์—์„œ ๋งŒ๋“  ๋‚˜๋น„๋Š” ๊ณ ์–‘์ด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ด๊ณ , ํ˜ธ๋Œ์ด์™€ ์ˆ˜ํ˜ธ๋ž‘์€ ํ˜ธ๋ž‘์ด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ ๋‚ด์žฅ ํ•จ์ˆ˜์ธ isinstance()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ฐ์ฒด๊ฐ€ ํŠน์ • ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”. (์ด๋•Œ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ฏ€๋กœ animal. ๊ณ ์–‘์ด ๋’ค์— ๊ด„ํ˜ธ๋ฅผ ๋ถ™์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.) >>> isinstance(๋‚˜๋น„, animal. ๊ณ ์–‘์ด) True >>> isinstance(๋‚˜๋น„, animal. ํ˜ธ๋ž‘์ด) False ๊ทธ๋Ÿฐ๋ฐ ๊ณ ์–‘์ด๋Š” ๊ณ ์–‘์ž‡๊ณผ๋ฅผ ์ƒ์†ํ•œ ํด๋ž˜์Šค์ด๊ณ , ๊ณ ์–‘์ž‡๊ณผ๋Š” ๋™๋ฌผ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•œ ํด๋ž˜์Šค์˜€์ฃ ? ๋”ฐ๋ผ์„œ ๋‚˜๋น„๋Š” ๊ณ ์–‘์ด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ธ ๋™์‹œ์—, ๊ณ ์–‘์ž‡๊ณผ ํด๋ž˜์Šค์™€ ๋™๋ฌผ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ž…๋‹ˆ๋‹ค. >>> isinstance(๋‚˜๋น„, animal. ๊ณ ์–‘์ž‡๊ณผ) True >>> isinstance(๋‚˜๋น„, animal. ๋™๋ฌผ) True issubclass() A๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ B๋ผ๋Š” ํด๋ž˜์Šค์˜ ํ•˜์œ„ ํด๋ž˜์Šค(subclass)์ธ์ง€, ์ฆ‰ A๊ฐ€ B๋ฅผ ์ƒ์†ํ–ˆ๋Š”์ง€ ์•Œ๋ ค๋ฉด issubclass()๋ผ๋Š” ๋‚ด์žฅ ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•ž์—์„œ ๋งŒ๋“  ๊ณ ์–‘์ด ํด๋ž˜์Šค๊ฐ€ ๊ณ ์–‘์ž‡๊ณผ์™€ ๋™๋ฌผ ํด๋ž˜์Šค์˜ ํ•˜์œ„ ํด๋ž˜์Šค์ธ์ง€ ํ™•์ธํ•ด ๋ณผ๊นŒ์š”? >>> issubclass(animal. ๊ณ ์–‘์ด, animal. ๊ณ ์–‘์ž‡๊ณผ) True >>> issubclass(animal. ๊ณ ์–‘์ด, animal. ๋™๋ฌผ) True ์šธ์–ด() ๋ฉ”์„œ๋“œ์˜ ์ƒ์†๊ณผ ์žฌ์ •์˜ ์•ž์—์„œ ๋™๋ฌผ ํด๋ž˜์Šค์—๋Š” ์šธ์–ด()๋ผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ, ๊ณ ์–‘์ž‡๊ณผ ํด๋ž˜์Šค์—๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ”์—ˆ์ฃ (pass). ๋ณด๋น„๋ผ๋Š” ํ“จ๋งˆ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ“จ๋งˆ ํด๋ž˜์Šค๊ฐ€ ๋”ฐ๋กœ ์—†์œผ๋‹ˆ ๊ณ ์–‘์ž‡๊ณผ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค์–ด ์ค„๊ฒŒ์š”. >>> ๋ณด๋น„ = animal. ๊ณ ์–‘์ž‡๊ณผ() ๊ณ ์–‘์ž‡๊ณผ ํด๋ž˜์Šค์—๋Š” ์šธ์–ด() ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ, ๋ณด๋น„๋Š” ์šธ ์ˆ˜ ์žˆ์„๊นŒ์š”? >>> ๋ณด๋น„. ์šธ์–ด() ... ๊ณ ์–‘์ž‡๊ณผ๊ฐ€ ๋™๋ฌผ์„ ์ƒ์†ํ–ˆ์œผ๋ฏ€๋กœ ์šธ์–ด() ๋ฉ”์„œ๋“œ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ์ฃผ์ง€ ์•Š์•˜์Œ์—๋„ ... ์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ณ ์–‘์ด์™€ ํ˜ธ๋ž‘์ด์—์„œ๋Š” ๋™๋ฌผ์˜ ์šธ์–ด()๋ฅผ ๊ทธ๋Œ€๋กœ ์“ฐ์ง€ ์•Š๊ณ  ์ƒˆ๋กœ ์ •์˜ํ•ด์„œ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์šธ์Œ์†Œ๋ฆฌ๋ฅผ ๋ƒˆ์—ˆ์ฃ ? ์ด์™€ ๊ฐ™์ด ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜(override) ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 7.4. ๊ฐ์ฒด ์†์˜ ๊ฐ์ฒด ์ฝ”๋ผ๋ฆฌ๋ฅผ ๋ƒ‰์žฅ๊ณ ์— ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์„ ์•„์‹œ๋‚˜์š”? ์•„๋งˆ ๋ชจ๋ฅด์‹œ๋Š” ๋ถ„์ด ์—†๊ฒ ์ฃ ? ์ œ๊ฐ€ ์•„๋Š” ์œ ๋จธ๋Š” ์˜จ ๊ตญ๋ฏผ์ด ๋‹ค ์•Œ๊ณ  ๊ณ„์‹œ๋‹ˆ๊นŒ์š”. 1๋ฒˆ, ๋ƒ‰์žฅ๊ณ  ๋ฌธ์„ ์—ฐ๋‹ค. 2๋ฒˆ, ์ฝ”๋ผ๋ฆฌ๋ฅผ ๋„ฃ๋Š”๋‹ค. 3๋ฒˆ, ๋ƒ‰์žฅ๊ณ  ๋ฌธ์„ ๋‹ซ๋Š”๋‹ค. ^^; ์ด๊ฑธ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ํ•œ๋ฒˆ ์งœ๋ณผ๊นŒ์š”? class Fridge: def __init__(self): self.isOpened = False self.foods = [] def open(self): self.isOpened = True print('๋ƒ‰์žฅ๊ณ  ๋ฌธ์„ ์—ด์—ˆ์–ด์š”...') def put(self, thing): if self.isOpened: self.foods.append(thing) print('๋ƒ‰์žฅ๊ณ  ์†์— ์Œ์‹์ด ๋“ค์–ด๊ฐ”๋„ค...') else: print('๋ƒ‰์žฅ๊ณ  ๋ฌธ์ด ๋‹ซํ˜€์žˆ์–ด์„œ ๋ชป ๋„ฃ๊ฒ ์–ด์š”...') def close(self): self.isOpened = False print('๋ƒ‰์žฅ๊ณ  ๋ฌธ์„ ๋‹ซ์•˜์–ด์š”...') class Food: pass ์œ„์™€ ๊ฐ™์ด ๋ƒ‰์žฅ๊ณ ์™€ ์Œ์‹ ํด๋ž˜์Šค๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” fridge.py ๋ชจ๋“ˆ์„ ๋งŒ๋“ค์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋ƒ‰์žฅ๊ณ  ํด๋ž˜์Šค์—๋Š” ๋ฌธ์ด ์—ด๋ ค์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” isOpened๋ผ๋Š” ๋ณ€์ˆ˜์™€ ๋ƒ‰์žฅ๊ณ  ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ์Œ์‹๋“ค์˜ ๋ฆฌ์ŠคํŠธ์ธ foods๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜, ๋ƒ‰์žฅ๊ณ  ๋ฌธ์„ ์—ด๊ณ , ์Œ์‹์„ ์ง‘์–ด๋„ฃ๊ณ , ๋ฌธ์„ ๋‹ซ๋Š” ๋ฉ”์„œ๋“œ๋„ ๊ฐ๊ฐ ๊ฐ–๊ณ  ์žˆ์ง€์š”. ์Œ์‹์— ๋Œ€ํ•ด์„œ๋Š” ๋ณ„๋กœ ์“ธ ๋ง์ด ์—†๋”๊ตฐ์š”. ์“ธ ๊ฒƒ์ด ์—†์„ ๋•Œ๋Š” pass๋ผ๊ณ ๋งŒ ์จ์ฃผ๋ฉด ๋œ๋‹ค๊ณ  ํ•ด์„œ ๊ทธ๋ ‡๊ฒŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์†์ด ๋นˆ ์Œ์‹ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“  ๊ฑฐ์ง€์š”. ์ด์ œ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ๋„์›Œ์„œ ๋ƒ‰์žฅ๊ณ ์—๋‹ค๊ฐ€ ์ฝ”๋ผ๋ฆฌ๋ฅผ ์ง‘์–ด๋„ฃ์–ด ๋ด…์‹œ๋‹ค. >>> import fridge >>> f = fridge.Fridge() >>> apple = fridge.Food() >>> elephant = fridge.Food() ๋จผ์ € ๋ƒ‰์žฅ๊ณ  ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋กœ f๋ผ๋Š” ๊ฒƒ์„ ๋งŒ๋“ค๊ณ  ์Œ์‹ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋Š” apple๊ณผ elephant๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. >>> f.open() ๋ƒ‰์žฅ๊ณ  ๋ฌธ์„ ์—ด์—ˆ์–ด์š”... >>> f.put(apple) ๋ƒ‰์žฅ๊ณ  ์†์— ์Œ์‹์ด ๋“ค์–ด๊ฐ”๋„ค... ๋ƒ‰์žฅ๊ณ  ๋ฌธ์„ ์—ด๊ณ , ์ผ๋‹จ ์ค€๋น„์šด๋™์œผ๋กœ ๋ƒ‰์žฅ๊ณ ์— ์‚ฌ๊ณผ๋ฅผ ๋„ฃ์–ด๋ดค๋Š”๋ฐ ์ž˜ ๋“ค์–ด๊ฐ”์ง€์š”? ๊ทธ๋Ÿผ ์ฝ”๋ผ๋ฆฌ๋„ ๋„ฃ์–ด๋ณผ๊นŒ์š”? >>> f.put(elephant) ๋ƒ‰์žฅ๊ณ  ์†์— ์Œ์‹์ด ๋“ค์–ด๊ฐ”๋„ค... ์ฝ”๋ผ๋ฆฌ๋„ ์™ ๋“ค์–ด๊ฐ”์Šต๋‹ˆ๋‹ค~ ๋ƒ‰์žฅ๊ณ  ์†์— ์‚ฌ๊ณผ๋ž‘ ์ฝ”๋ผ๋ฆฌ๊ฐ€ ์ž˜ ๋“ค์–ด๊ฐ”๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ๊นŒ์š”? ๋ƒ‰์žฅ๊ณ  f์˜ foods ๋ฆฌ์ŠคํŠธ์— ๋ญ๊ฐ€ ๋“ค์–ด์žˆ๋Š”์ง€ ๋ด…์‹œ๋‹ค. >>> f.foods [<fridge.Food instance at 007924AC>, <fridge.Food instance at 0079153C>] Food ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค(instance, ์‹ค์ฒด) ๋‘ ๊ฐœ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค๊ณ  ๋‚˜์˜ค๋Š”๊ตฐ์š”. ์‹ค์ฒด๋‚˜ ๊ฐ์ฒด๋‚˜ ๋น„์Šทํ•œ ๋ง์ด๊ฒ ์ฃ ? ์ž, ๋ƒ‰์žฅ๊ณ  ๊ฐ์ฒด๋Š” foods๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ณ ์š”, foods ๋ฆฌ์ŠคํŠธ๋Š” ์Œ์‹ ํด๋ž˜์Šค์˜ ์‚ฌ๊ณผ์™€ ์ฝ”๋ผ๋ฆฌ ๊ฐ์ฒด๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๋ƒ‰์žฅ๊ณ  ๊ฐ์ฒด๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค์„ ๊ฐ–๊ณ  ์žˆ๋‹ค๊ณ ๋„ ํ•  ์ˆ˜ ์žˆ๊ฒ ์ฃ ? ์˜ค๋Š˜ ๋ณด์‹  ๊ฒƒ์ฒ˜๋Ÿผ ๊ฐ์ฒด๋Š” ๋˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•  ์ˆ˜๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค. ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋Š” ์ด๋Ÿฐ ๊ฒƒ์„ composition(ํ•ฉ์„ฑ, ๋ณตํ•ฉ)์ด๋ผ๊ณ  ํ•˜๊ณ ์š”, 'has-a' ๊ด€๊ณ„๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. "f๋Š” elephant๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค(f has an elephant)." ๋ง ๋˜์ฃ ? ๋ณ„๋กœ ์–ด๋ ต์ง€๋Š” ์•Š์ง€๋งŒ ์ž˜ ์จ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๊ฐœ๋…์ด๋‹ˆ๊นŒ ์ตํ˜€๋‘์„ธ์š”. ์ด๋ ‡๊ฒŒ ํ•ด์„œ ์˜ค๋Š˜์˜ ์ž„๋ฌด์ธ ๊ฐ์ฒด ์†์— ๊ฐ์ฒด ์ง‘์–ด๋„ฃ๊ธฐ๋Š” ํ›Œ๋ฅญํ•˜๊ฒŒ ์™„์ˆ˜ํ–ˆ๊ตฐ์š”. ์ฐธ๊ณ ๋กœ 'has-a' ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ทธ๋ฆผ์„ ๋ณด์—ฌ๋“œ๋ฆฌ๋ฉด์„œ ๋งˆ์น˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Fridge ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋Š” Food ํด๋ž˜์Šค์˜ ๊ฐ์ฒด(elephant ๋“ฑโ€ฆ)๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ(*) ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์—ฐ๊ฒฐ์„  ์œ„์•„๋ž˜์— *์™€ elephant๋ผ๊ณ  ์จ์ค˜์„œ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด์ง€์š”. 7.5. ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ๋“ค ์ด์ œ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค๋“ค ์•Œ๊ณ  ๊ณ„์‹œ๊ฒ ์ฃ ? ๋ฉ”์„œ๋“œ๋ผ๋Š” ๊ฒƒ์€ ์šฐ๋ฆฌ๊ฐ€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋ฉด์„œ ๊ทธ ์•ˆ์— ๋งŒ๋“ค์–ด ๋„ฃ์€ ํ•จ์ˆ˜๋ฅผ ๋งํ•˜์ง€์š”? ๋งŒ๋“ค์–ด์ง„ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ฐ์ฒด. ๋ฉ”์„œ๋“œ()์™€ ๊ฐ™์€<NAME>์œผ๋กœ ํ˜ธ์ถœ์„ ํ•ด์ฃผ์—ˆ๊ณ ์š”. ์˜ค๋Š˜์€ ๊ทธ๋Ÿฐ ์ผ๋ฐ˜์ ์ธ ๋ฉ”์„œ๋“œ๋“ค๊ณผ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅธ ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ๋“ค์— ๋Œ€ํ•ด ํ•จ๊ป˜ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. __init__ ๋ฉ”์„œ๋“œ (์ดˆ๊ธฐํ™”) # bookstore.py class Book: def setData(self, title, price, author): self.title = title self.price = price self.author = author def printData(self): print('์ œ๋ชฉ : ', self.title) print('๊ฐ€๊ฒฉ : ', self.price) print('์ €์ž : ', self.author) def __init__(self): print('์ฑ… ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์—ˆ์–ด์š”~') ์˜ˆ์ œ๋กœ Book(์ฑ…) ํด๋ž˜์Šค๋ฅผ ๊ฐ–๋Š” bookstore(์ฑ…๋ฐฉ) ๋ชจ๋“ˆ์„ ๋งŒ๋“ค์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ฑ… ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋กœ๋Š” ์ฑ… ์ œ๋ชฉ, ๊ฐ€๊ฒฉ, ์ €์ž์™€ ๊ฐ™์€ ์ž๋ฃŒ๋“ค์„ ์ž…๋ ฅํ•  ๋•Œ ์‚ฌ์šฉํ•  setData()์™€ ์ด๋Ÿฐ ์ž๋ฃŒ๋“ค์„ ์ถœ๋ ฅํ•ด ์ฃผ๋Š” printData()๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์ง€์š”. ๊ทธ๋ฆฌ๊ณ  __init__์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋„ ์žˆ์ง€์š”? ์ด๊ฒƒ์ด ๋ฐ”๋กœ ํŒŒ์ด์ฌ์—์„œ ํŠน๋ณ„ํ•˜๊ฒŒ ์•ฝ์†๋œ ๋ฉ”์„œ๋“œ ๊ฐ€์šด๋ฐ ํ•˜๋‚˜๋กœ, ์ดˆ๊ธฐํ™”(initialize) ๋ฉ”์„œ๋“œ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์งˆ ๋•Œ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋˜์–ด์„œ ๊ทธ ๊ฐ์ฒด๊ฐ€ ๊ฐ–๊ฒŒ ๋  ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์„ฑ์งˆ์„ ์ •ํ•ด์ฃผ๋Š” ์ผ์„ ํ•˜์ง€์š”. ๊ทธ๋Ÿผ ์ฑ… ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด๋ณผ๊นŒ์š”? >>> import bookstore >>> b = bookstore.Book() ์ฑ… ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์—ˆ์–ด์š”~ Book() ํ•ด์„œ Book ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์ž๋งˆ์ž ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜์—ˆ๊ตฐ์š”. ๋‚˜๋จธ์ง€ setData์™€ printData ๋ฉ”์„œ๋“œ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. >>> b.setData('๋ˆ„๊ฐ€ ๋‚ด ์น˜์ฆˆ๋ฅผ ๋จน์—ˆ์„๊นŒ', '300์›', '๋ฏธํ‚ค') >>> b.printData() ์ œ๋ชฉ : ๋ˆ„๊ฐ€ ๋‚ด ์น˜์ฆˆ๋ฅผ ๋จน์—ˆ์„๊นŒ ๊ฐ€๊ฒฉ : 300์› ์ €์ž : ๋ฏธํ‚ค ์ด์ œ ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ๊ฐ€ ๋ญ”์ง€ ๋Œ€์ถฉ ๊ฐ์„ ์žก์œผ์…จ์œผ๋ฉด __init__ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์‹ค์ œ๋กœ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. __init__ ๋ฉ”์„œ๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด ๋ณด์„ธ์š”. def __init__(self, title, price, author): self.setData(title, price, author) print('์ฑ… ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์—ˆ์–ด์š”~') ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์‹œํ‚ฌ ๋•Œ ์ œ๋ชฉ, ๊ฐ€๊ฒฉ, ์ €์ž๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ, setData ๋ฉ”์„œ๋“œ์—๊ฒŒ ๋„˜๊ฒจ์ฃผ๋„๋ก ํ–ˆ์ฃ ? ๋ฌผ๋ก  ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ์—์„œ ์ง์ ‘ ๋ณ€์ˆ˜๋ฅผ ๋‹ค๋ค„๋„ ์ƒ๊ด€์—†์ง€๋งŒ setData ๋ฉ”์„œ๋“œ๋ฅผ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋’€์œผ๋‹ˆ๊นŒ ์ด์šฉ์„ ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์ฑ… ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ธ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. >>> from importlib import reload >>> reload(bookstore) >>> b2 = bookstore.Book('๋‚ด๊ฐ€ ๋จน์—ˆ์ง€', '200์›', '๋ฏธ๋‹ˆ') ์ฑ… ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์—ˆ์–ด์š”~ ๊ทธ๋Ÿฐ๋Œ€๋กœ ์“ธ ๋งŒํ•˜์ฃ ? ๊ฐ’์ด ์ž˜ ๋“ค์–ด๊ฐ”๋Š”์ง€๋„ ํ™•์ธํ•ด ๋ณด์„ธ์š”. ์ฐธ๊ณ ๋กœ ๋ง์”€๋“œ๋ฆฌ๋ฉด, ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ์™€ ๊ฐ™์€ ๊ฒƒ์„ ๋‹ค๋ฅธ ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด์—์„œ๋Š” ์ƒ์„ฑ์ž(constructor)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ต๋‹ˆ๋‹ค. __del__ ๋ฉ”์„œ๋“œ (์†Œ๋ฉธ์ž) __init__ ๋ฉ”์„œ๋“œ์™€ ๋ฐ˜๋Œ€๋กœ ๊ฐ์ฒด๊ฐ€ ์—†์–ด์งˆ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์„ ์†Œ๋ฉธ์ž(destructor)๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ํŒŒ์ด์ฌ์—์„œ๋Š” __del__ ๋ฉ”์„œ๋“œ๊ฐ€ ์†Œ๋ฉธ์ž์˜ ์—ญํ• ์„ ๋งก๊ณ  ์žˆ์ฃ . ๊ฐ์ฒด๊ฐ€ ์—†์–ด์ง€๋Š” ์ˆ˜๋„ ์žˆ๋ƒ๊ณ ์š”? ๋ญํ•˜๋ ค๊ณ  ์—†์• ๋ƒ๊ณ ์š”? del ๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋ณด์„ธ์š”. ๋‹น์žฅ ์—†์–ด์ง‘๋‹ˆ๋‹ค. ๋˜, ๋งŒ๋“ค์–ด ๋‘” ๊ฐ์ฒด๊ฐ€ ๋” ์ด์ƒ ํ•„์š” ์—†์–ด์ง€๋ฉด ํŒŒ์ด์ฌ์ด ์•Œ์•„์„œ ์ฒ˜๋ฆฌํ•ด ์ฃผ๊ธฐ๋„ ํ•˜๊ณ ์š”. ๊ทธ๊ฑด ์ง์ ‘ __del__ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ…Œ์ŠคํŠธํ•ด ๋ณด์‹œ๋ฉด ์ž˜ ์•„์‹ค ์ˆ˜ ์žˆ๊ฒ ์ฃ ? ๊ทธ๋ƒฅ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ์™€ ๋˜‘๊ฐ™์ด ์ž‘์„ฑํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ์—†์–ด์ง€๊ธฐ ์ „์— ๋ญ”๊ฐ€ ์ฒ˜๋ฆฌ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค๋ฉด ์†Œ๋ฉธ์ž๊ฐ€ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ด๊ฒ ์ง€์š”? __repr__ ๋ฉ”์„œ๋“œ (ํ”„๋ฆฐํŒ…) ์ด๋ฒˆ์—” printData์™€ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋Œ€์‹ , ํŒŒ์ด์ฌ์˜ ๊ธฐ๋ณธ ๋ฌธ์ธ print ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ์ฑ… ์ œ๋ชฉ์„ ์ฐ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ผ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ์€ ๋ฐ”๋กœ __repr__ ๋ฉ”์„œ๋“œ์ด์ง€์š”. ์ฑ… ํด๋ž˜์Šค์— ์•„๋ž˜์™€ ๊ฐ™์ด __repr__ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์„ธ์š”. def __repr__(self): return self.title ๋ณ„๋‹ค๋ฅธ ๊ฒƒ์€ ์—†๊ณ ์š”, return ๋ฌธ์„ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ๊ฒƒ๋งŒ ๋ˆˆ์—ฌ๊ฒจ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. __repr__ ๋ฉ”์„œ๋“œ๋Š” '๋ฌธ์ž์—ด'์„ 'return' ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋˜๊ฒ ์ฃ ? ๊ทธ๋Ÿผ ์ฑ…๋ฐฉ ๋ชจ๋“ˆ์„ ์žฌ์ ์žฌํ•˜๊ณ  ์ƒˆ ์ฑ…์„ ๋งŒ๋“ค์–ด์„œ ํ…Œ์ŠคํŠธํ•ด ๋ณด์„ธ์š”. >>> b3 = bookstore.Book('๋‚˜๋„ ์ข€ ์ค˜', '100์›', '์ฅ๋ฒผ๋ฃฉ') ์ฑ… ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์—ˆ์–ด์š”~ >>> print(b3) ๋‚˜๋„ ์ข€ ์ค˜ __add__ ๋ฉ”์„œ๋“œ (๋ง์…ˆ) ์ด์ œ ์ฑ…๋ฐฉ์€ ๋ฌธ์„ ๋‹ซ๊ณ  ์„ธ๋ชจ, ๋„ค๋ชจ, ๋™๊ทธ๋ผ๋ฏธ ๊ฐ™์€ ๋„ํ˜•์„ ๋งŒ๋“ค์–ด๋ณผ๊นŒ์š”? # shape.py class Shape: area = 0 def __add__(self, other): return self.area + other.area ํ•™๊ต์—์„œ ๋„ํ˜•์— ๋Œ€ํ•ด ๋ฐฐ์šธ ๋•Œ๋Š” ๋Š˜ ๋„“์ด์— ๋Œ€ํ•ด ์ƒ๊ฐ์„ ํ•˜์ง€์š”? ์—ฌ๊ธฐ์„œ๋Š” ๋‘ ๋„ํ˜•์˜ ๋„“์ด๋ฅผ ๋”ํ•˜๋Š” __add__ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ๊ฐ์ฒด self์™€ other๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ ๊ทธ ๋‘˜์˜ ๋„“์ด๋ฅผ ๋”ํ•œ ๊ฐ’์„ ๋Œ๋ ค์ฃผ๋Š” ์ผ์„ ํ•˜๋„๋ก ํ–ˆ์ง€์š”. >>> a = shape.Shape() >>> a.area = 20 >>> b = shape.Shape() >>> b.area = 10 >>> a + b 30 ๋„ํ˜• a์™€ b๋ฅผ ๋ง์…ˆ ์—ฐ์‚ฐ์ž(+)๋กœ ๋”ํ–ˆ๋”๋‹ˆ ๋‘ ๋„ํ˜•์˜ ๋„“์ด๊ฐ€ ๋”ํ•ด์กŒ์ฃ ? ๋งˆ์น˜ ๋ณดํ†ต์˜ ๋‘ ์ˆซ์ž๋ฅผ ๋”ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ง์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์—ฐ์‚ฐ์ž๊ฐ€ ํ•˜๋Š” ์ผ์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์„ ์—ฐ์‚ฐ์ž ์ค‘๋ณต(overload)์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ต๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์ž ์ค‘๋ณต์„ ์ด์šฉํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ๋งŒ๋“  ํด๋ž˜์Šค์˜ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ๋„ ์—ฐ์‚ฐ์ž๋ฅผ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์ง€์š”. ๋งˆ์น˜ ํŒŒ์ด์ฌ ์ž์ฒด์—์„œ ์ œ๊ณตํ•˜๋Š” ์ž๋ฃŒํ˜•์ฒ˜๋Ÿผ ๋ง์ž…๋‹ˆ๋‹ค. ์•„์ง๋„ ์‚ฌํƒœ์˜ ์‹ฌ๊ฐ์„ฑ์„ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๊ณ  "a + b๊ฐ€ ๋ญ ์–ด์จŒ๊ธธ๋ž˜? ์›๋ž˜ ๊ทธ๋ƒฅ ๋”ํ•˜๋ฉด ๋˜๋Š” ๊ฑฐ์ž–์•„~"๋ผ๊ณ  ํ•˜์‹œ๋Š” ๋ถ„๋“ค! -- Shape ํด๋ž˜์Šค์— __add__ ๋ฉ”์„œ๋“œ๋ฅผ ๋„ฃ์ง€ ๋ง๊ณ  ๊ฐ์ฒด ๋‘ ๊ฐœ๋ฅผ ๋งŒ๋“  ๋‹ค์Œ์— ๋”ํ•ด๋ณด์„ธ์š”. ์ง€๋‚˜๊ฐ€๋˜ ๋ฑ€์ด ์›ƒ์Šต๋‹ˆ๋‹คโ€ฆ--; ๊ทธ๋ฆฌ๊ณ  ๋ฒŒ์„œ ๋ˆˆ์น˜์ฑ„์‹  ๋ถ„๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๋ง์…ˆ ์—ฐ์‚ฐ์ž ๋Œ€์‹  __add__ ๋ฉ”์„œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•ด๋„ ๊ทธ ๊ฒฐ๊ณผ๋Š” ๋˜‘๊ฐ™๋‹ต๋‹ˆ๋‹ค. >>> a.__add__(b) 30 ๊ทธ๋Ÿผ ๋„ํ˜• ๊ฐ์ฒด ๊ฐ„์— ๋บ„์…ˆ๋„ ํ•  ์ˆ˜ ์žˆ๋„๋ก __sub__ ๋ฉ”์„œ๋“œ๋„ ๋งŒ๋“ค์–ด๋ณด์„ธ์š”~. __lt__ ๋ฉ”์„œ๋“œ (๋น„๊ต) ์ด์ œ ์—ฐ์‚ฐ์ž ์ค‘๋ณต์— ๋Œ€ํ•ด ์–ด๋Š ์ •๋„ ๊ฐ์ด ์žกํžˆ์‹œ์ฃ ? ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ์—ฐ์‚ฐ์ž ์ค‘๋ณต ๋ฉ”์„œ๋“œ๋Š” ์ด์™ธ์—๋„ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋‘ ์‚ดํŽด๋ณด๊ธฐ๋Š” ํž˜๋“ค๊ฒ ๋„ค์š”. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‘ ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•˜๋Š” ๋น„๊ต ์—ฐ์‚ฐ์ž(<, >, ==)๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด์„œ ์ด ๋ถ€๋ถ„์„ ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ผ๋‹จ Shape ํด๋ž˜์Šค์— ์•„๋ž˜์™€ ๊ฐ™์ด __lt__ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์„ธ์š”(โ€˜less thanโ€™์„ ์˜๋ฏธ). def __lt__(self, other): return self.area < other.area ๊ฐ„๋‹จํžˆ ๋‘ ๊ฐ์ฒด self์™€ other์˜ area๋ฅผ ๋น„๊ตํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋Œ๋ ค์ฃผ๋„๋ก ํ–ˆ๋Š”๋ฐ์š”, ์ด์ œ ๋‘ ์ˆซ์ž๋ฅผ ๋น„๊ตํ•˜๋“ฏ ๋‘ ๊ฐ์ฒด๋ฅผ ๋ถ€๋“ฑํ˜ธ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. shape ๋ชจ๋“ˆ์„ ์žฌ์ ์žฌํ•˜๊ณ  ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋“ค์„ ๋งŒ๋“ค์–ด์„œ ๊ฐ๊ฐ area ๊ฐ’์„ ์ •ํ•ด ์ค€ ๋‹ค์Œ, ๋‘ ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> import shape >>> c = shape.Shape() >>> c.area = 30 >>> d = shape.Shape() >>> d.area = 20 >>> c > d True >>> if c > d: print('c๊ฐ€ ๋” ๋„“์–ด์š”~') ... c๊ฐ€ ๋” ๋„“์–ด์š”~ ๋น„๊ต๊ฐ€ ์ž˜ ๋˜๋‚˜์š”? ์˜ค๋Š˜ ๊ฐ•์ขŒ๋Š” ์ข€ ๊ธธ์–ด์กŒ๊ตฐ์š”. ์•„๊นŒ๋„ ๋ง์”€๋“œ๋ ธ์ง€๋งŒ ์—ฐ์‚ฐ์ž ์ค‘๋ณต ๋ฉ”์„œ๋“œ๋Š” ์˜ค๋Š˜ ๋ณด์—ฌ๋“œ๋ฆฐ ๊ฒƒ ๋ง๊ณ ๋„ ๋งŽ์ด ์žˆ์œผ๋‹ˆ ๋‹ค๋ฅธ ์ž๋ฃŒ๋„ ์ฐพ์•„๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ทธ๋Ÿผโ€ฆ I'll be back โ€ฆ ^^ 8. ์˜ˆ์™ธ ์˜ˆ์™ธ(exception)๋ž€ ๋ฌด์—‡์ด๋ฉฐ ์–ด๋–ป๊ฒŒ ๋‹ค๋ค„์•ผ ํ• ์ง€ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ์ด ์žฅ์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฒƒ: ์˜ˆ์™ธ ์ฒ˜๋ฆฌ(try-except) 8.1 ์˜ˆ์™ธ ์ฒ˜๋ฆฌ(try, except) ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ๋ฐฐ์šธ ๋•Œ์—๋Š” ์ง์ ‘ ๋”ฐ๋ผ ํ•ด๋ณด๊ณ , ๋งŒ๋“ค์–ด๋ณด๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„๋„ ์ง€๊ธˆ๊นŒ์ง€ ํ•จ๊ป˜ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์—ฐ์Šต์„ ๋งŽ์ด ํ•ด๋ณด์…จ๊ฒ ์ฃ ? ๊ทธ๋ ‡๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๋„ ๋งŽ์ด ๋ณด์…จ์„ ๊ฒƒ ๊ฐ™๋„ค์š”. >>> print ๋ฐฉ๊ฐ€~ File "<stdin>", line 1 print ๋ฐฉ๊ฐ€~ ^ SyntaxError: invalid syntax ์œ„์—์„  '๋ฐฉ๊ฐ€~'๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ๋ญ”๊ฐ€ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด ๊ฒƒ ๊ฐ™์ฃ ? ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚œํ•ดํ•œ ๊ฒƒ ๊ฐ™์€๋ฐโ€ฆ ๋ฌด์Šจ ๋œป์ธ์ง€ ํ•ด๋…์„ ํ•ด๋ณผ๊นŒ์š”? File "<stdin>", line 1 ํŒŒ์ผ์˜ 1๋ฒˆ์งธ ์ค„์—์„œ print ๋ฐฉ๊ฐ€~ ^ ^๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„์— SyntaxError: invalid syntax ์ž˜๋ชป๋œ ๊ตฌ๋ฌธ์œผ๋กœ ์ธํ•ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Œ ํ•œ๋งˆ๋””๋กœ ๋ฌธ๋ฒ•์ด ํ‹€๋ ธ๋‹ค๋Š” ์–˜๊ธฐ๊ตฐ์š”. ์—ฌ๊ธฐ์„œ์ด๋ผ๋Š” ๊ฑด ํ‘œ์ค€ ์ž…๋ ฅ(standard input), ์ฆ‰ ํ‚ค๋ณด๋“œ๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ™”์‹์œผ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ํŒŒ์ผ๋กœ ์ž‘์„ฑํ•ด์„œ ์‹คํ–‰์‹œ์ผฐ๋‹ค๋ฉด ๊ทธ ํŒŒ์ผ์˜ ์ด๋ฆ„์ด ๋‚˜์™”๊ฒ ์ง€์š”. ์ด๋ ‡๊ฒŒ ํŒŒ์ด์ฌ์€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋ฉด, ์–ด๋””๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž˜๋ชป๋๋Š”์ง€ ํŒ๋‹จํ•ด์„œ ์šฐ๋ฆฌ์—๊ฒŒ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์šฐ๋ฆฌ๋Š” ๊ทธ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ด์„œ ์ž˜ ์ž‘๋™ํ•˜๋„๋ก ํ•˜๋ฉด ๋˜์ง€์š”. ๊ทธ๋Ÿฐ๋ฐ, ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋‹ค ๋ณด๋ฉด ํ‰์†Œ์—๋Š” ์ž˜ ๋Œ์•„๊ฐ€๋‹ค๊ฐ€ ๊ฐ€๋”์”ฉ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ์ œ๊ฐ€ ๋ฐ”๋กœ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ์ธ๋ฐ์š”, ๋‘ ์ˆ˜๋ฅผ ๊ณฑํ•˜๊ณ  ๋‚˜๋ˆ ์„œ ๋”ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. >>> def f(a, b): ... return (a * b) + (a / b) ... >>> f(4,2) 10 ์•„์ง์€ ๋ณ„๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์—†์–ด ๋ณด์ด์ฃ ? ํ•˜์ง€๋งŒ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ 0์„ ๋„˜๊ฒจ์ฃผ๋ฉด ๋‚œ๋ฆฌ๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค. >>> f(3,0) Traceback (most recent call last): File "<stdin>", line 1, in ? File "<stdin>", line 2, in f ZeroDivisionError: integer division or modulo by zero ์ •์ˆ˜๋ฅผ 0์œผ๋กœ ๋‚˜๋ˆ„๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค๊ณ  ํ•˜๋Š”๊ตฐ์š”. ์›๋ž˜ ์ˆซ์ž๋ฅผ 0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜๊ฐ€ ์—†์ฃ ? ๊ทธ๋ ‡๋‹ค๋ฉด ์ด ํ•จ์ˆ˜๋Š” ์†์„ ์ข€ ๋ด์•ผ๊ฒ ๋„ค์š”. >>> def f(a, b): ... if a and b: # a์™€ b๊ฐ€ ๋‘˜ ๋‹ค 0์ด ์•„๋‹ ๋•Œ ... return (a * b) + (a / b) ... elif a: # ๊ทธ๋ ‡์ง€ ์•Š๊ณ  a๋งŒ 0์ด ์•„๋‹ ๋•Œ ... return '๋ถˆ๋Šฅ' ... else: # ๋‘˜ ๋‹ค 0์ผ ๋•Œ ... return '๋ถ€์ •' ... ์ด์ œ ์ข€ ๊ทธ๋Ÿด ๋“ฏํ•˜๋„ค์š”. >>> f(3, 0) '๋ถˆ๋Šฅ' >>> f(0, 0) '๋ถ€์ •' OX ํ€ด์ฆˆ!! ์ด์ œ ์ด ํ•จ์ˆ˜๋Š” ๋” ์ด์ƒ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธธ ์ผ์ด ์—†๊ฒ ์ฃ ? ๊ทธ๋ ‡๋‹ค๊ณ ์š”? ๊ณผ์—ฐ ๊ทธ๋Ÿด๊นŒ์š”โ€ฆ f(300000, 500000)๋ฅผ ํ•œ๋ฒˆ ์‹คํ–‰์‹œ์ผœ๋ณด์„ธ์š”. ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, >>> f(300000, 500000) 150000000000.6 ํŒŒ์ด์ฌ ๋ฒ„์ „์ด ๋‚ฎ์„ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. >>> f(300000, 500000) Traceback (most recent call last): File "<stdin>", line 1, in ? File "<stdin>", line 3, in f OverflowError: integer multiplication ์ธ์ž๋กœ ๋ฐ›์€ ๋‘ ์ˆ˜์˜ ๊ณฑ์ด ์ •์ˆ˜ํ˜•์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ์—๋Š” ๋„ˆ๋ฌด ํฐ ๊ฐ’์ด๋ผ์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๊ตฐ์š”. ์Œโ€ฆ ๋˜ ์ด๋Ÿฐ ๊ฑดโ€ฆ? >>> f(์ด์‹ญ, ์˜ค) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name '์ด์‹ญ' is not defined ์ฉโ€ฆ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋‹ค ๋ณด๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ๋๋„ ์—†์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ด๋Ÿฐ ์˜ค๋ฅ˜๋ฅผ ๋ชจ๋‘ ์˜ˆ์ƒํ•ด์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์ •๋ง๋กœ '์‚ฝ์งˆ'์ด๋ผ๊ณ  ๋ฐ–์— ํ•  ์ˆ˜ ์—†๊ฒ ๋„ค์š”. ์•ž์˜ ์˜ˆ์—์„œ ๋ถˆ๋Šฅ๊ณผ ๋ถ€์ •์„ ์ •ํ•ด์ค€ ๊ฒƒ์€ ๋‚˜์˜์ง€ ์•Š๋‹ค๊ณ  ํ•˜๋”๋ผ๋„, ๋‹ค๋ฅธ ๋‘ ๊ฒฝ์šฐ๊นŒ์ง€ ๊ฐ๊ฐ ์ฒ˜๋ฆฌํ•ด ์ฃผ๋Š” ๊ฑด ์‹œ๊ฐ„ ๋‚ญ๋น„์ด๊ฒ ์ฃ ? ๋‹คํ–‰ํžˆ ํŒŒ์ด์ฌ์—์„  ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์˜๋„์™€ ๋™๋–จ์–ด์ง„ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ์˜ˆ์™ธ(exception)๋ผ๊ณ  ํ•ด์„œ, ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์–ด๋–ค ์กฐ์น˜๋ฅผ ์ทจํ•  ๊ฒƒ์ธ์ง€ ์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด์ฃ . ๊ทธ๋Ÿผ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด๋„๋ก ํ•˜์ง€์š”. >>> def f(a, b): ... try: ... if a and b: ... return (a * b) + (a / b) ... elif a: ... return '๋ถˆ๋Šฅ' ... else: ... return '๋ถ€์ •' ... except: ... return '๋˜‘๋ฐ”๋กœ ์‚ด์•„๋ผ' ๋ณด์‹œ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์žฅ๋“ค์„ try ๋ฐ‘์— ๋„ฃ์–ด์ฃผ๊ณ , ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ฒ˜๋ฆฌํ•  ๋ถ€๋ถ„์€ except ๋ฐ‘์— ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ผ๋‹จ ์‹œ๋„(try) ํ•ด ๋ณด๋‹ค๊ฐ€ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋ฉด(except) ์ฒ˜๋ฆฌํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์ฃ . ์˜ˆ์™ธ๋Š” ์˜ค๋ฅ˜(error)๋ณด๋‹ค ๋” ๋„“์€ ๊ฐœ๋…์ด๊ธด ํ•˜์ง€๋งŒ ์ง€๊ธˆ์€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฐํ•˜์…”๋„ ๋˜๊ณ ์š”, C++๊ณผ Java์—์„œ๋„ ๋น„์Šทํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ„๋‹จํžˆ ์ž‘์„ฑํ•œ ๊ฒƒ์ด๋‹ˆ ์ฐธ๊ณ ๋งŒ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ๋ง‰๋Š” ๊ฒƒ์ด ๋Šฅ์‚ฌ๊ฐ€ ์•„๋‹ˆ๋ผ, ํ•„์š”ํ•œ ๊ณณ์—์„œ ์ ์ ˆํ•œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก ๊ถ๋ฆฌ๋ฅผ ๋งŽ์ด ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. tip ์—ฌ๊ธฐ์„œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด ์˜ˆ์ƒํ•œ ๊ฒƒ๊ณผ ๋‹ฌ๋ผ์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋ฅผ try-except๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์˜ˆ๋กœ ๋“ค์—ˆ์ง€๋งŒ, assert ๋ฌธ์„ ์ด์šฉํ•ด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ๋ฏธ๋ฆฌ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์–ด์š”. ๋” ์ฝ์„๊ฑฐ๋ฆฌ ์˜ˆ์™ธ์— ๋Œ€ํ•ด ๋” ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ใ€Š์‹ค์šฉ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐใ€‹์˜ โ€˜์˜ค๋ฅ˜ ๊ฒ€์‚ฌโ€™ ์ ˆ์„ ์ฝ์–ด๋ณด์„ธ์š”. 8.2 ์—ฐ์Šต ๋ฌธ์ œ: ์Œ์„ฑ ์ธ์‹ ์ผ๋ณธ์–ด ํ€ด์ฆˆ ๊ฐœ์„  ๋ฌธ์ œ 6์žฅ์—์„œ ์‘์šฉ ์˜ˆ์ œ๋กœ ์†Œ๊ฐœ ๋“œ๋ฆฐ ์Œ์„ฑ ์ธ์‹์„ ํ™œ์šฉํ•œ ์ผ๋ณธ์–ด ํ€ด์ฆˆ๋ฅผ ํ’€๋‹ค ๋ณด๋‹ˆ, ํ”„๋กœ๊ทธ๋žจ์ด ๋ง์„ ์ž˜ ์•Œ์•„๋“ฃ์ง€ ๋ชปํ•˜๊ณ  speech_recognition.UnknownValueError๋ผ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋‚ผ ๋•Œ๊ฐ€ ์žˆ๋„ค์š”. ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์ด์–ด์„œ ๊ณต๋ถ€ํ•˜๊ณ  ์‹ถ์€๋ฐ ํ”„๋กœ๊ทธ๋žจ์ด ๋ฉˆ์ถฐ๋ฒ„๋ฆฌ๋‹ˆ ์ข€ ๋ถˆํŽธํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋ฅ˜๊ฐ€ ๋‚  ๊ฒฝ์šฐ ๊ทธ ๋‹จ์–ด๋Š” ํ†ต๊ณผํ•˜๊ณ  ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ๊ณต๋ถ€ํ•˜๋„๋ก ๊ณ ์ณ๋ณด์„ธ์š”. ํ’€์ด ์ €๋Š” ์ด๋ ‡๊ฒŒ ํ–ˆ์–ด์š”. ์ฝ”๋“œ: ch08/japanese_quiz2.py ์ €๋Š” except โ‹ฏ as โ‹ฏ ๊ตฌ๋ฌธ์„ ์ผ๋Š”๋ฐ, ์–ด๋ ค์šธ๊นŒ ๋ด ์•ž์—์„œ๋Š” ์†Œ๊ฐœํ•˜์ง€ ์•Š์•˜์–ด์š”. ๊ถ๊ธˆํ•˜์‹  ๋ถ„์€ โŸช์‹ค์šฉ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐโŸซ 3.3์ ˆ์„ ์ฝ์–ด๋ณด์„ธ์š”. 9. ํ…Œ์ŠคํŒ…๊ณผ ์„ฑ๋Šฅ ์ฝ”๋“œ์— ์˜ค๋ฅ˜๊ฐ€ ์—†๋Š”์ง€ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฒ•๊ณผ, ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ์ด ์žฅ์—์„œ ๋ฐฐ์šฐ๋Š” ๊ฒƒ: ํ…Œ์ŠคํŒ… ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์‹œ๊ฐ„ ์ธก์ •ํ•˜๊ธฐ 9.1 ํ…Œ์ŠคํŒ… ์˜ค๋Š˜์€ ํฌ๋ฆฌ์Šค๋งˆ์Šค์ž…๋‹ˆ๋‹ค. ๋ฉ”๋ฆฌ ํฌ๋ฆฌ์Šค๋งˆ์Šค~ ๋…์ž๊ป˜์„œ ์œค๋…„ ํŒ๋ณ„ํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ๋ฅผ ํ‘ผ ๊ฒƒ์„ ๋ด๋‹ฌ๋ผ๊ณ  ๋ฉ”์ผ๋กœ ๋ณด๋‚ด์ฃผ์…”์„œ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” if ๋ฌธ์„ ์ค‘์ฒฉํ•ด์„œ ํ’€์—ˆ์—ˆ๋Š”๋ฐ, ์ด ์ฝ”๋“œ๋Š” ์ค‘์ฒฉ ์—†์ด ํ‰ํ‰ํ•˜๊ฒŒ ๋˜์–ด ์žˆ์–ด์„œ ๋ณด๊ธฐ๊ฐ€ ์ข‹๋„ค์š”. ์‹œ๊ฐ์ ์œผ๋กœ ๋ณด๊ธฐ ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ์‹ค์ œ๋กœ๋„ ์ข‹์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์ฝ”๋“œ๋ฅผ ๋ˆˆ์œผ๋กœ๋งŒ ๋ด์„œ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์ œ๋Œ€๋กœ ๋‚˜์˜ฌ์ง€ ํ™•์‹ ํ•˜๊ธฐ ํž˜๋“ค๋ฏ€๋กœ, ์‹ค์ œ ์ˆซ์ž๋ฅผ ๋„ฃ์–ด์„œ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ณ , ๊ทธ๋‹ค์Œ์—๋Š” ํŒŒ์ด์ฌ ์œ ๋‹› ํ…Œ์ŠคํŠธ(unittest) ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด ํ…Œ์ŠคํŠธ๋ฅผ ์ž๋™ํ™”ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์—ฐ๋„๊ฐ€ ์œค๋…„์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ ์šฐ์„  ํ…Œ์ŠคํŠธํ•˜๊ธฐ ํŽธํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์•ฝ๊ฐ„ ๋ฐ”๊ฟ”๋ณผ๊ฒŒ์š”. ์›๋ž˜๋Š” input() ํ•จ์ˆ˜๋กœ ์ž…๋ ฅ์„ ๋ฐ›์•„์„œ print() ๋ฌธ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜์–ด ์žˆ์—ˆ๋Š”๋ฐ, is_leap_year() ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” year ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์œค๋…„์— ํ•ด๋‹นํ•˜๋ฉด True๋ฅผ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. # ํŒŒ์ผ๋ช…: LeapYear.py def is_leap_year(year): if year % 4 != 0: return False elif year % 100 != 0: return True elif year % 400 != 0: return False else: return True IDLE์—์„œ ์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด LeapYear.py๋กœ ์ €์žฅํ•˜๊ณ  F5 ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. >>> = RESTART: C:\Users\ychoi\OneDrive\๋ฌธ์„œ\GitHub\wikidocs-chobo-python\ch09\LeapYear.py LeapYear.py๊ฐ€ ๋‹ค์‹œ ์‹œ์ž‘๋˜์—ˆ๋‹ค๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ๋–ด์Šต๋‹ˆ๋‹ค. ์ฆ‰, is_leap_year() ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋˜์—ˆ๊ฒ ์ฃ ? ๊ด„ํ˜ธ ์—†์ด ํ•จ์ˆ˜ ์ด๋ฆ„๋งŒ ๋„ฃ์–ด์„œ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> is_leap_year <function is_leap_year at 0x000002007E874DC0> ๋„ค, is_leap_year๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ 0x000002007E874DC0์— ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋Š” ์ง‘ ์ฃผ์†Œ์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์˜จ๋ผ์ธ ์‡ผํ•‘์—์„œ ์ฃผ๋ฌธํ•œ ๋ฌผ๊ฑด์„ ๋ฐฐ์†ก๋ฐ›์œผ๋ ค๋ฉด ์ง‘ ์ฃผ์†Œ๋ฅผ ์•Œ๋ ค์ค˜์•ผ ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ์ปดํ“จํ„ฐ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•ด๋‘” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ์ด์ฃ . ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๋ชฐ๋ผ๋„ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ is_leap_year() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ฌํ•ด๋Š” 2020๋…„์ด๋‹ˆ๊นŒ 2020์„ ๋„ฃ์–ด๋ณด์ฃ . >>> is_leap_year(2020) True 2020๋…„์€ ์œค๋…„์ด๋ฏ€๋กœ True๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” 2077๋กœ ํ…Œ์ŠคํŠธํ•ด ๋ด…์‹œ๋‹ค. 2077๋…„์€ ํ‰๋…„์ด๋ฏ€๋กœ False๊ฐ€ ๋‚˜์™€์•ผ๊ฒ ์ฃ ? >>> is_leap_year(2077) False ๋„ค, ์ข‹์Šต๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์—ฐ๋„๊ฐ€ ์œค๋…„์ธ์ง€์˜ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” is_leap_year() ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์ˆ˜์ž‘์—…์œผ๋กœ ๊ฐ„๋‹จํžˆ ํ…Œ์ŠคํŠธํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์œ ๋‹› ํ…Œ์ŠคํŠธ ์ž‘์„ฑ๊ณผ ์‹คํ–‰ ์ด์ œ, ์œ„์—์„œ ์ˆ˜์ž‘์—…์œผ๋กœ ์ˆ˜ํ–‰ํ–ˆ๋˜ ํ…Œ์ŠคํŠธ ๊ณผ์ •์„ ์ž๋™ํ™”ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์–ธ์–ด์— ๊ธฐ๋ณธ์œผ๋กœ ํฌํ•จ๋œ unittest ๋ชจ๋“ˆ์„ ์ด์šฉํ•  ๊ฑฐ๊ณ ์š”, ์‹ค์ œ ์ฝ”๋“œ์™€ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ๊ฐ™์€ ํŒŒ์ผ์— ๋„ฃ์–ด๋„ ๋˜๊ฒ ์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š๊ณ  test_LeapYear๋ผ๋Š” ์ด๋ฆ„์˜ ํŒŒ์ผ์„ ๋”ฐ๋กœ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ํŒŒ์ผ๋ช…: test_LeapYear.py 1 import LeapYear 2 import unittest 3 4 class TestLeapYear(unittest.TestCase): 5 def test_2020(self): 6 r = LeapYear.is_leap_year(2020) 7 self.assertEqual(r, True) 8 9 if __name__ == '__main__': 10 unittest.main() 11 ์ฝ”๋“œ ์„ค๋ช…: 1ํ–‰: ์œ„์—์„œ ์ž‘์„ฑํ•œ LeapYear ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ 2ํ–‰: unittest ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ 4~7ํ–‰: LeapYear ๋ชจ๋“ˆ์„ ํ…Œ์ŠคํŠธํ•˜๋Š” TestLeapYear ํด๋ž˜์Šค ์ •์˜ 5~7ํ–‰: is_leap_year() ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฉ”์„œ๋“œ 6ํ–‰: is_leap_year(2020)์„ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ r์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. r์˜ ๊ฐ’์€ 2020๋…„์ด ์œค๋…„์ธ์ง€ ์•„๋‹Œ์ง€ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถˆ๋ฆฐ ๊ฐ’(True ๋˜๋Š” False)์ด ๋ฉ๋‹ˆ๋‹ค. 7ํ–‰: 'r ๊ฐ’์ด True ์—ฌ์•ผ ์˜ฌ๋ฐ”๋ฅธ ๊ฒฐ๊ณผ์ด๋‹ค'๋ผ๋Š” ์˜๋„๋ฅผ ์ฝ”๋“œ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 9~10ํ–‰: test_LeapYear๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ IDLE์—์„œ ์œ„์™€ ๊ฐ™์€ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด test_LeapYear.py๋กœ ์ €์žฅํ•œ ๋‹ค์Œ, F5 ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ ์‹คํ–‰ํ•˜๋ฉด IDLE Shell ์ฐฝ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. >>> = RESTART: C:\Users\ychoi\OneDrive\๋ฌธ์„œ\GitHub\wikidocs-chobo-python\ch09\test_LeapYear.py ---------------------------------------------------------------------- Ran 1 test in 0.011s OK ํ…Œ์ŠคํŠธ๋ฅผ ํ•œ ๊ฐœ ์ˆ˜ํ–‰ํ–ˆ๊ณ , ๋ชจ๋“  ํ…Œ์ŠคํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์ข‹๋‹ค๋Š” ๋œป์œผ๋กœ OK๋ผ๊ณ  ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€ 2020๋…„์— ๋Œ€ํ•ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•ด ๋ดค์œผ๋‹ˆ, 2077๋…„์— ๋Œ€ํ•ด์„œ๋„ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ ์ž‘์„ฑํ•œ TestLeapYear ํด๋ž˜์Šค์— test_2077() ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. # ํŒŒ์ผ๋ช…: test_LeapYear.py (์•ž ๋ถ€๋ถ„ ์ƒ๋žต) 8 9 def test_2077(self): 10 r = LeapYear.is_leap_year(2077) 11 self.assertEqual(r, False) 12 13 if __name__ == '__main__': 14 unittest.main() ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์•ž์—์„œ ์ž‘์„ฑํ•œ test_2020() ๋ฉ”์„œ๋“œ์™€ ๋น„๊ตํ•ด ๋ณด๋ฉด ํ•œ ๊ฐ€์ง€ ๋‹ค๋ฅธ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐพ์œผ์…จ๋‚˜์š”? ๋งž์Šต๋‹ˆ๋‹ค. 11ํ–‰์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž๊ฐ€ False๋กœ ๋˜์–ด ์žˆ์ฃ . 2077๋…„์€ ์œค๋…„์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์—, is_leap_year(2077)์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” False ์—ฌ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ด๋ ‡๊ฒŒ ๋‚˜ํƒ€๋ƒˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ, ํ…Œ์ŠคํŠธ๋ฅผ ๋‹ค์‹œ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> = RESTART: C:\Users\ychoi\OneDrive\๋ฌธ์„œ\GitHub\wikidocs-chobo-python\ch09\test_LeapYear.py .. ---------------------------------------------------------------------- Ran 2 tests in 0.012s OK ์ด๋ฒˆ์—๋Š” ํ…Œ์ŠคํŠธ ๋‘ ๊ฐœ๊ฐ€ ์‹คํ–‰๋˜์—ˆ๊ณ , ๋‘˜ ๋‹ค ํ†ต๊ณผํ•ด์„œ OK๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ถ„๊ธฐ๋ฅผ ํ…Œ์ŠคํŠธํ–ˆ๋‚˜? ์ง€๊ธˆ๊นŒ์ง€ LeapYear.py ์ฝ”๋“œ๋ฅผ ํ…Œ์ŠคํŠธํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ…Œ์ŠคํŠธ๊ฐ€ ์ด๊ฒƒ์œผ๋กœ ์ถฉ๋ถ„ํ• ๊นŒ์š”, ์•„๋‹ˆ๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ ์ข€ ๋” ํ•ด๋ด์•ผ ํ• ๊นŒ์š”? ์šฐ๋ฆฌ๋Š” ์ฝ”๋“œ๋ฅผ ๊ฐ–๊ณ  ์žˆ์œผ๋ฏ€๋กœ, if ๋ฌธ์„ ์‚ฌ์šฉํ•œ ๋ถ„๊ธฐ๊ฐ€ ์–ด๋–ค ๊ตฌ์กฐ์ธ์ง€ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋„ค ๊ฐ€์ง€ ์žˆ๋Š”๋ฐ, ๊ฐ๊ฐ ํ•œ ๋ฒˆ์”ฉ์€ ํ…Œ์ŠคํŠธํ•ด ๋ณด๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์•ž์—์„œ ํ…Œ์ŠคํŠธํ•œ ๋ถ„๊ธฐ๋Š” ์–ด๋Š ๊ฒƒ์ด๊ณ , ํ…Œ์ŠคํŠธํ•˜์ง€ ์•Š์€ ๋ถ„๊ธฐ๋Š” ์–ด๋Š ๊ฒƒ์ผ๊นŒ์š”? ํ…Œ์ŠคํŠธ ๋Œ€์ƒ์ธ LeapYear.py ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ๋ณด๋ฉด์„œ ์ƒ๊ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ํŒŒ์ผ๋ช…: LeapYear.py def is_leap_year(year): if year % 4 != 0: # (1) 2077 return False elif year % 100 != 0: # (2) 2020 return True elif year % 400 != 0: # (3) ? return False else: # (4) ? return True ์œ„ ์ฝ”๋“œ์—์„œ (1) '์—ฐ๋„๋ฅผ 4๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฉด ์œค๋…„์ด ์•„๋‹ˆ๋‹ค'๋ผ๋Š” ๋…ผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ์ด๋ฅผ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์—์„œ๋Š” 2077์„ ๊ฐ€์ง€๊ณ  ํ…Œ์ŠคํŠธํ–ˆ์—ˆ์ฃ ? ๋‹ค์Œ์œผ๋กœ, (2) '์—ฐ๋„๋ฅผ 4๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋˜ 100์œผ๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฉด ์œค๋…„์ด๋‹ค'๋ผ๋Š” ๋…ผ๋ฆฌ๋ฅผ 2020์œผ๋กœ ํ…Œ์ŠคํŠธํ–ˆ์Šต๋‹ˆ๋‹ค. (3) ๋ฒˆ๊ณผ (4) ๋ฒˆ ๋…ผ๋ฆฌ๋Š” ํ…Œ์ŠคํŠธํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ, ์ด๊ฒƒ๋“ค์ด ์˜ฌ๋ฐ”๋กœ ์ž‘์„ฑ๋˜์—ˆ๋Š”์ง€๋„ ์ถ”๊ฐ€๋กœ ํ…Œ์ŠคํŠธํ•  ํ•„์š”๊ฐ€ ์žˆ๊ฒ ๋„ค์š”. ์ˆ™์ œ ๊ทธ๋Ÿฌ๋ฉด (3) ๋ฒˆ๊ณผ (4) ๋ฒˆ์„ ํ…Œ์ŠคํŠธํ•˜๋ ค๋ฉด ๋ช‡ ๋…„์„ ๊ฐ€์ง€๊ณ  ํ…Œ์ŠคํŠธํ•ด์•ผ ํ• ๊นŒ์š”? 400๋…„ ์ฃผ๊ธฐ๊ฐ€ ์ค‘์š”ํ•˜๋ฏ€๋กœ 2000๋…„๊ณผ 1900๋…„์„ ์‚ดํŽด๋ณด๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”. ์ž˜ ์ž‘์„ฑํ–ˆ๋‹ค๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜ฌ ๊ฑฐ์˜ˆ์š”. = RESTART: C:\Users\ychoi\OneDrive\๋ฌธ์„œ\GitHub\wikidocs-chobo-python\ch09\test_LeapYear.py .... ---------------------------------------------------------------------- Ran 4 tests in 0.013s OK ๋” ์ฝ์„๊ฑฐ๋ฆฌ ์‹ค์šฉ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - 8.1 ํ…Œ์ŠคํŒ…: https://wikidocs.net/84431 9.1.1 ์—ฐ์Šต ๋ฌธ์ œ: ์ˆซ์ž ์ฝ๊ธฐ(0~100) ๋ฌธ์ œ 0 ์ด์ƒ 100 ์ดํ•˜์˜ ์ •์ˆ˜๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์•„ ๊ทธ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ํ•œ๊ธ€ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ korean_number()๋ฅผ ํฌํ•จํ•˜๋Š” korean_0_to_100.py๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. ์•„๋ž˜ ํ…Œ์ŠคํŠธ(test_korean_number.py)๋ฅผ ์ด์šฉํ•˜๋˜, ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค. from korean_0_to_100 import korean_number import unittest class TestKoreanNumber(unittest.TestCase): def test_0(self): self.assertEqual(korean_number(0), '์˜') def test_1(self): self.assertEqual(korean_number(1), '์ผ') def test_2(self): self.assertEqual(korean_number(2), '์ด') if __name__ == '__main__': unittest.main() ch09/test_korean_number.py ch09/korean_0_to_100.py ์˜์ƒ https://youtu.be/m2q1uR7GsXU 9.2 ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์‹œ๊ฐ„ ์ธก์ •ํ•˜๊ธฐ ๊ฐ™์€ ๋ชฉ์ ์œผ๋กœ ์ž‘์„ฑํ•œ ํ”„๋กœ๊ทธ๋žจ์ด๋ผ ํ•˜๋”๋ผ๋„, ๊ทธ ๋…ผ๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ–ˆ๋Š”์ง€์— ๋”ฐ๋ผ ๊ณ„์‚ฐ์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ธฐ๋„ ํ•˜๊ณ , ์ƒ๋Œ€์ ์œผ๋กœ ์ผ์ฐ ๋๋‚˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์–ด๋–ป๊ฒŒ ์ธก์ •ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์‹œ๊ณ„๋ฅผ ๋ณด๋ฉด์„œ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹œ์ž‘ ์‹œ๊ฐ๊ณผ ์ข…๋ฃŒ ์‹œ๊ฐ์„ ํ™•์ธํ•ด์„œ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜๋ฉด ๋ ๊นŒ์š”? time.process_time() ์ด๋ ‡๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์‹ค์ œ๋กœ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„(๋ฒฝ์‹œ๊ณ„์— ๋น„์œ ํ•ด wall time์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค)์„ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์ปดํ“จํ„ฐ์˜ ์ฒ˜๋ฆฌ ์‹œ๊ฐ„(process time)์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด ๋‚˜์„ ๋•Œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์˜ ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์„ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ time ๋ชจ๋“ˆ์˜ process_time์œผ๋กœ ์‹œ๊ฐ„์„ ์ธก์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. wall time๊ณผ process time ์ธํ˜•์— ๋ˆˆ์„ ๋ถ™์ด๋Š” ์•„๋ฅด๋ฐ”์ดํŠธ๋ฅผ ํ•œ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ธํ˜• ํ•œ ๊ฐœ์˜ ๋ˆˆ์„ ๋ถ™์ด๋Š” ๋ฐ 10์ดˆ๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค๋ฉด, ์‰ฌ์ง€ ์•Š๊ณ  ์ผ์ •ํ•œ ์†๋„๋กœ ์ผํ•  ๊ฒฝ์šฐ 1๋ถ„์— ์ธํ˜• ์—ฌ์„ฏ ๊ฐœ์˜ ๋ˆˆ์„ ๋ถ™์ผ ์ˆ˜ ์žˆ๊ณ  ํ•œ ์‹œ๊ฐ„์—๋Š” 360๊ฐœ๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ฒ ์ฃ . ๊ทธ๋Ÿฐ๋ฐ ํ•œ ์‹œ๊ฐ„ ์ผํ•  ๋•Œ๋งˆ๋‹ค ์‹ญ ๋ถ„์”ฉ ์‰ฌ์–ด์•ผ ํ•˜๋ฏ€๋กœ ํ•œ ์‹œ๊ฐ„์— ์ธํ˜• 300๊ฐœ์˜ ๋ˆˆ์„ ๋ถ™์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ์‚ฌ๋žŒ์ด ์ธํ˜• 1200๊ฐœ์˜ ๋ˆˆ์„ ๋ถ™์ธ๋‹ค๋ฉด, ํ•œ ์‚ฌ๋žŒ์ด 600๊ฐœ์”ฉ ์ž‘์—…ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๊ฒฝ๊ณผ ์‹œ๊ฐ„(wall time ๋˜๋Š” elapsed time)์€ 1์‹œ๊ฐ„ 50๋ถ„(50๋ถ„ ์ž‘์—… + 10๋ถ„ ํœด์‹ + 50๋ถ„ ์ž‘์—…)์ž…๋‹ˆ๋‹ค. ์ฒ˜๋ฆฌ ์‹œ๊ฐ„(process time ๋˜๋Š” CPU time)์€ 50๋ถ„ * 4 = 200๋ถ„ = 3์‹œ๊ฐ„ 20๋ถ„์ž…๋‹ˆ๋‹ค. 4์žฅ ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ ์—ฐ์Šต ๋ฌธ์ œ์˜ ์ฒซ ๋ฒˆ์งธ ํ’€์ด(prime.py)์˜ ์‹คํ–‰ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œˆ๋„ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์˜ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ch09์—์„œ ch04๋กœ ๋ฐ”๊พผ ๋’ค ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์‹คํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. wikidocs-chobo-python\ch09> cd .. wikidocs-chobo-python> cd ch04 wikidocs-chobo-python\ch04> python Python 3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import prime >>> from time import process_time >>> start = process_time() >>> prime.prime(2 ** 8) # 256 ์ดํ•˜์˜ ์†Œ์ˆ˜ [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251] >>> end = process_time() >>> end - start 0.03125 >>> exit() wikidocs-chobo-python\ch09> ์ฒ˜๋ฆฌ ์‹œ๊ฐ„(end - start)์ด 0.03125์ดˆ ๊ฑธ๋ ธ๋‹ค๊ณ  ๋‚˜์™”์Šต๋‹ˆ๋‹ค. sys.path.append() ์ด๋ฒˆ์—๋Š” ๋‘ ๋ฒˆ์งธ ํ’€์ด(prime2.py)์˜ ์‹คํ–‰ ์‹œ๊ฐ„์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ๋Š” ์œˆ๋„ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์—์„œ ์ง์ ‘ ch04 ํด๋”๋กœ ์ด๋™ํ•ด์„œ ํŒŒ์ด์ฌ์„ ์‹คํ–‰ํ–ˆ์ง€๋งŒ, ์•„๋ž˜์™€ ๊ฐ™์ด ch09์—์„œ ํŒŒ์ด์ฌ์„ ์‹คํ–‰ํ•ด sys.path.append()๋กœ ch04๋ฅผ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ์— ์ถ”๊ฐ€ํ•˜๊ณ  ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. wikidocs-chobo-python\ch04> wikidocs-chobo-python\ch04> cd ../ch09 wikidocs-chobo-python\ch09> python Python 3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path.append('../ch04') >>> import prime2 >>> from time import process_time >>> start = process_time() >>> prime2.prime(2 ** 8) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251] >>> end = process_time() >>> end - start 0.0 >>> ์ด๋ฒˆ์—๋Š” ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์ด 0.0์œผ๋กœ, ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐ ์‹œ๊ฐ„์ด ํ›จ์”ฌ ์ ๊ฒŒ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. importlib.import_module() ๊ทธ ๋ฐ–์— ๋…์ž๋ถ„๋“ค์ด ์•Œ๋ ค์ฃผ์‹  ํ’€์ด๋„ ์žˆ์–ด์„œ ์ด๋ฆ„์ด prime์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ๋“ค์˜ ์‹คํ–‰ ์‹œ๊ฐ„์„ ๋ชจ๋‘ ๋น„๊ตํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ๋ชจ๋“ˆ๋ช…์„ ์ œ๊ฐ€ ์ง์ ‘ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  glob์„ ์ด์šฉํ•ด ์•Œ์•„๋‚ธ ํ›„ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ•˜๋‚˜์”ฉ ์‹คํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋ชจ๋“ˆ๋ช…์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด์„ ๊ฐ€์ง€๊ณ  ์ž„ํฌํŠธ ํ•˜๊ธฐ ์œ„ํ•ด importlib์˜ import_module() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ”„๋กœ๊ทธ๋žจ์˜ ์„ฑ๋Šฅ์„ ๋” ์ž˜ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด N์„ ์•„๊นŒ๋ณด๋‹ค ๋” ํฐ ์ˆ˜๋กœ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. [์ฝ”๋“œ: ch09/prime_process_time.py] import sys import os import importlib from glob import glob from time import process_time N = 2 ** 12 # 4096 # N = 2 ** 13 # 8192 # ํ˜„์žฌ ์žฅ์€ 9์žฅ์ธ๋ฐ ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ ์ฝ”๋“œ๋Š” 4์žฅ์— ์žˆ์–ด, ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๋„ ๋ณ€๊ฒฝ sys.path.append('../ch04') os.chdir('../ch04') for pth in glob('prime*'): # prime์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํŒŒ์ผ๋ช… ๊ฐ๊ฐ์— ๋Œ€ํ•ด... name = os.path.splitext(pth)[0] # ๋ชจ๋“ˆ๋ช…(ํŒŒ์ผ๋ช…์—์„œ ํ™•์žฅ์ž ์•ž๊นŒ์ง€) ๊ตฌํ•˜๊ธฐ print(f'\nRunning {pth} ...') mod = importlib.import_module(name) # ๋ชจ๋“ˆ ์ž„ํฌํŠธ # ์‹คํ–‰ ์‹œ๊ฐ„ ์ธก์ • start = process_time() mod.prime(N) end = process_time() print('elapsed:', end - start) ์‹คํ–‰ ๊ฒฐ๊ณผ(N = 2 ** 12์ผ ๋•Œ) wikidocs-chobo-python\ch09>prime_process_time.py Running prime.py ... [2, 3, 5, (์ƒ๋žต), 4079, 4091, 4093] elapsed: 57.15625 Running prime2.py ... [2, 3, 5, (์ƒ๋žต), 4079, 4091, 4093] elapsed: 0.0 Running prime_aaaa.py ... Prime Number List Of 4096 [2, 3, 5, (์ƒ๋žต), 4079, 4091, 4093] elapsed: 0.015625 Running prime_fate.py ... [2, 3, 5, (์ƒ๋žต), 4079, 4091, 4093] elapsed: 0.015625 Running prime_kim.py ... [2, 3, 5, (์ƒ๋žต), 4079, 4091, 4093] elapsed: 0.671875 ์‹คํ–‰ ๊ฒฐ๊ณผ(N = 2 ** 13์ผ ๋•Œ) ๋‹ค์Œ์€ N ๊ฐ’์„ 2 ** 13์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์‹คํ–‰ํ•ด ๋ณธ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. wikidocs-chobo-python\ch09>prime_process_time.py Running prime.py ... [2, 3, 5, (์ƒ๋žต), 8171, 8179, 8191] elapsed: 444.125 Running prime2.py ... [2, 3, 5, (์ƒ๋žต), 8171, 8179, 8191] elapsed: 0.015625 Running prime_aaaa.py ... Prime Number List Of 8192 [2, 3, 5, (์ƒ๋žต), 8171, 8179, 8191] elapsed: 0.09375 Running prime_fate.py ... [2, 3, 5, (์ƒ๋žต), 8171, 8179, 8191] elapsed: 0.046875 Running prime_kim.py ... [2, 3, 5, (์ƒ๋žต), 8171, 8179, 8191] elapsed: 4.078125 A. ๋ถ€๋ก ์—ฌ๊ธฐ๊นŒ์ง€ ์ฝ์œผ์‹  ์—ฌ๋Ÿฌ๋ถ„์€ ์ด์ œ '์™•์ดˆ๋ณด'๋Š” ์•„๋‹ˆ๊ณ  '์ดˆ๋ณด'์ž…๋‹ˆ๋‹ค!? ๋…์ž ์—ฌ๋Ÿฌ๋ถ„์ด ํŠนํžˆ ์–ด๋ ค์›Œํ•˜์…จ๋˜ ๋ฌธ์ œ ๋ช‡ ๊ฐœ๋Š” ํŒŒ์ด์ฌ์— ์ต์ˆ™ํ•ด์ง„ ํ›„์— ํ’€๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์„œ ์ด๊ณณ์œผ๋กœ ์˜ฎ๊ฒผ์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์— ํŒŒ์ด์ฌ ๊ธฐ์ดˆ ๋ฌธ๋ฒ•์€ ์•„๋‹ˆ์ง€๋งŒ ์ตํ˜€๋‘๋ฉด ์œ ์šฉํ•œ ๊ฒƒ๋“ค์„ ๋ชจ์•˜์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜์˜ ์žฌ๊ท€ ์Šค์ผ€์ค„๋ง ์•Œ๊ณ ๋ฆฌ๋“ฌ ์ง„๋ฒ• ๋ณ€ํ™˜๊ณผ ๋น„ํŠธ ์—ฐ์‚ฐ ํŒŒ์ด์ฌ์œผ๋กœ PDF ํŒŒ์ผ ํ•ฉ์น˜๊ธฐ ๋งทํ”Œ๋กฏ๋ฆฝ์œผ๋กœ ํ•˜ํŠธ ๊ทธ๋ฆฌ๊ธฐ ์œˆ๋„์—์„œ ํŒŒ์ด์ฌ ํ™œ์šฉ ํŒ A.1 ํ•จ์ˆ˜์˜ ์žฌ๊ท€ ์ด๋ฒˆ์— ๋ฐฐ์šธ ๊ฒƒ์€ ์ƒˆ๋กœ์šด ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•์€ ์•„๋‹ˆ๊ณ ์š”, ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋Š” ํ…Œํฌ๋‹‰ ์ค‘์˜ ํ•œ ๊ฐ€์ง€์ธ๋ฐ ์กฐ๊ธˆ ๋จธ๋ฆฌ๊ฐ€ ์•„ํ”Œ ์ˆ˜๋„ ์žˆ๋Š” ๋‚ด์šฉ์ด๋ž๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ตœ๋Œ€ํ•œ ์‰ฝ๊ฒŒ ์•Œ๋ ค๋“œ๋ฆด ํ…Œ๋‹ˆ๊นŒ ๋„ˆ๋ฌด ๊ฑฑ์ • ๋งˆ์‹œ๊ณ  ํ•จ๊ป˜ ์•Œ์•„๋ณด๋„๋ก ํ•ด์š”. (3์žฅ์— ์žˆ๋˜ ๊ฒƒ์„ ๋ถ€๋ก์œผ๋กœ ์˜ฎ๊ฒผ์Šต๋‹ˆ๋‹ค.) ํ•จ์ˆ˜๊ฐ€ ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š” โ€˜์žฌ๊ท€(recursion)โ€™ ํ˜น์€ โ€˜์ˆœํ™˜โ€™์ž…๋‹ˆ๋‹ค. ๊ฐ‘์ž๊ธฐ ์–ด๋ ค์šด ๊ฒƒ์„ ๋ฐฐ์šฐ๋ฉด ๋จธ๋ฆฌ๊ฐ€ ๋งŽ์ด ์•„ํ”„์‹ค ํ…Œ๋‹ˆ๊นŒ ๋จผ์ € ์ค€๋น„์šด๋™์„ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ๊ตฐ์š”. ๋‹ค์Œ์˜ ์˜ˆ์ œ๋ฅผ ๋ด์ฃผ์„ธ์š”. ๋จผ์ € ์ฃผ์–ด์ง„ ๋‘ ์ˆ˜๋ฅผ ํ•ฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> def hap(a, b): ... print(a + b) ... ์ œ๋Œ€๋กœ ๋งŒ๋“ค์—ˆ๋Š”์ง€ ํ™•์ธ์„ ํ•ด๋ณด์„ธ์š”. ์ด ํ•จ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์“ฐ๋Š”์ง€ ์•„์‹œ๊ฒ ์ฃ ? ํ™•์ธํ•ด ๋ณด์…จ์œผ๋ฉด ๋‘ ์ˆ˜๋ฅผ ๊ณฑํ•˜๋Š” ํ•จ์ˆ˜๋„ ๋งŒ๋“ค์–ด๋ณด์„ธ์š”. >>> def gop(a, b): ... print(a * b) ... ๊ทธ๋Ÿผ ์ด๋ฒˆ์—” ๋‘ ์ˆ˜๋ฅผ ํ•ฉํ•ด๋ณด๊ณ  ๊ณฑํ•ด๋ณด๊ณ , ๋‘ ๊ฐ€์ง€ ์ผ์„ ๋‹คํ•˜๋Š” ํ•จ์ˆ˜๋„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> def hap_gop(a, b): ... hap(a, b) ... gop(a, b) ... ์ด ํ•จ์ˆ˜๋Š” ์ž๊ธฐ์—๊ฒŒ ๋งก๊ฒจ์ง„ ์ผ์„ ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค์—๊ฒŒ ์‹œ์ผœ๋ฒ„๋ฆฌ์ฃ ? ๋‹ค์‹œ ๋งํ•˜๋ฉด, ์ด ํ•จ์ˆ˜๋Š” hap() ํ•จ์ˆ˜์™€ gop() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ํ…Œ์ŠคํŠธํ•ด ๋ณด์„ธ์š”. ์žฌ๋ฏธ์žˆ์œผ์‹ ๊ฐ€์š”? ์ด์   ๋ณธ๋ก ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. def countdown(n): if n == 0: print("Blastoff!") else: print(n) countdown(n-1) ์ถœ์ฒ˜: How to Think Like a Computer Scientist ์ด ํ•จ์ˆ˜๋Š” ๋ฌด์Šจ ์ผ์„ ํ•˜๋Š” ํ•จ์ˆ˜์ผ๊นŒ์š”? ํ•จ์ˆ˜ ์ด๋ฆ„์„ ๋ณด๋ฉด ์ถ”์ธก์„ ํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐโ€ฆ ์ž˜ ๋ชจ๋ฅด์‹œ๊ฒ ๋‚˜์š”? ๊ทธ๋ ‡๋‹ค๋ฉด ๋ช‡ ์ค„ ์•ˆ๋˜๋‹ˆ๊นŒ ์ง์ ‘ ์ณ๋ณด์‹œ์ง€์š”. ์ œ๋Œ€๋กœ ๋งŒ๋“ค์—ˆ์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•ด ๋ณด์‹œ๊ณ ์š”. >>> countdown(3) 2 Blastoff! ์ด ํ•จ์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ์ˆ˜๋ถ€ํ„ฐ ์นด์šดํŠธ๋‹ค์šด์„ ํ•˜๋‹ค๊ฐ€ 0๊นŒ์ง€ ์˜ค๋ฉด ๊ฝ! ํ•˜๋Š” ์ผ์„ ํ•œ๋‹ต๋‹ˆ๋‹ค. '๊ทธ๋Ÿฐ ๊ฑฐ๋ผ๋ฉด for ๋ฌธ์ด๋‚˜ while ๋ฌธ๊ณผ ๋˜‘๊ฐ™์ž–์•„'ํ•˜๊ณ  ์ƒ๊ฐํ•˜๋Š” ๋ถ„๋„ ๊ณ„์‹œ๊ฒ ์ง€๋งŒ ์ฝ”๋“œ๋ฅผ ๊ฐ€๋งŒํžˆ ๋ณด์‹œ๋ฉด ์ฐจ์ด์ ์„ ๋ฐœ๊ฒฌํ•˜์‹ค ๊ฑฐ์˜ˆ์š”. ์ด ํ•จ์ˆ˜๋Š” if, else ๊ตฌ์กฐ๋กœ์„œ n์ด 0์ธ์ง€ ์•„๋‹Œ์ง€ ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ์ผ์„ ํ•˜๋„๋ก ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด if ๋ฌธ์—์„œ ๊ฒ€์‚ฌํ•˜๋Š” n์€ ์ฒซ์งธ ์ค„์—์„œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ •ํ•ด์ค€ n๊ณผ ๊ฐ™์€ ๋…€์„์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋งค๊ฐœ๋ณ€์ˆ˜์— 3์„ ๋„ฃ์–ด์„œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, ์ฆ‰ countdown(3)์ด๋ผ๊ณ  ์“ฐ๋ฉด ํ•จ์ˆ˜ ๋ณธ์ฒด์˜ n ๊ฐ’์œผ๋กœ 3์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด์ง€์š”. n ๊ฐ’์œผ๋กœ 3์ด ๋“ค์–ด์˜ค๋ฉด ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋Š” ์–ด๋–ค ์ผ์ด ๋ฒŒ์–ด์งˆ๊นŒ์š”? ๋จผ์ € if n == 0:์—์„œ n ๊ฐ’์ด 0๊ณผ ๊ฐ™์€์ง€ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. n์ด 3์ด๋ฏ€๋กœ n์˜ ๊ฐ’๊ณผ 0์€ ๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด else: ์ดํ›„์˜ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜๊ฒ ์ง€์š”. ๋‹ค์Œ ์ค„์— ์žˆ๋Š” print(n)์€ n ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ผ๋Š” ๋ช…๋ น์ด๋‹ˆ๊นŒ ํ™”๋ฉด์— 3์„ ์ถœ๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์—” countdown(n-1)์ด๋ผ๊ณ  ๋˜์–ด์žˆ์ง€์š”. countdown์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ n-1 ๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„ฃ์–ด์ฃผ๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ countdown(2)์™€ ๊ฐ™์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜์ง€์š”. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ๋ญ”๊ฐ€ ์ด์ƒํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋ˆˆ์น˜์ฑ„์…จ์„ ๊ฒ๋‹ˆ๋‹ค. countdown์ด๋ผ๋Š” ํ•จ์ˆ˜์—์„œ countdown์„ ํ˜ธ์ถœํ•œ๋‹ค? ์˜ˆ, ๊ทธ๊ฒƒ์ด ๋ฐ”๋กœ ์˜ค๋Š˜์˜ ์ฃผ์ธ๊ณต, ์žฌ๊ท€์  ํ˜ธ์ถœ์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด์ฃ . ๋จธ๋ฆฌ๊ฐ€ ๋ณต์žกํ•ด์ง€๊ธฐ ์‹œ์ž‘ํ•˜์‹ ๋‹ค๋ฉด ์ƒ๊ฐ์„ ์ž ์‹œ ๋ฎ์œผ์‹œ๊ณ  countdown(2)๋ผ๊ณ  ํ˜ธ์ถœํ•˜๋ฉด ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚ ์ง€๋ถ€ํ„ฐ ํ•จ๊ป˜ ๋”ฐ์ ธ๋ณด์ฃ . ๋จผ์ € if ๋ฌธ์—์„œ n ๊ฐ’์ด 0๊ณผ ๊ฐ™์€์ง€ ๊ฒ€์‚ฌ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ๊ฐ€ ๊ฑฐ์ง“์ด๋ฏ€๋กœ else: ์ดํ›„์˜ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜๊ฒ ์ฃ . print(n)์—์„œ n ๊ฐ’์ธ 2๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•ด ์ฃผ๊ณ  ๊ทธ๋‹ค์Œ ์ค„์—์„œ countdown(1)์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์˜คํ˜ธ~. ๋ญ”๊ฐ€ ๊ฐ์ด ์žกํžˆ์‹œ๋‚˜์š”? ์ฒ˜์Œ์— countdown ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์œผ๋กœ 3์„ ๋„ฃ์–ด์คฌ๋Š”๋ฐ ํ•จ์ˆ˜๊ฐ€ ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ ์  ์ž‘์•„์ง€์ฃ ? ๊ทธ๋ ‡๋‹ค๋ฉด countdown(1)์€ ๋‹ค์‹œ countdown(0)์„ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด ํ‹€๋ฆผ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด countdown(0)์—์„œ๋Š” ๋ฌด์Šจ ์ผ์„ ํ• ๊นŒ์š”? n == 0์ด๋ฉด print("Blastoff!") ํ•˜๋ผ๊ณ  ๋˜์–ด์žˆ์œผ๋‹ˆ ํ™”๋ฉด์— ๊ทธ๋ ‡๊ฒŒ ์ถœ๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. '๋ปฅ์ด์•ผ~!' ๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ countdown ํ•จ์ˆ˜๊ฐ€ ๋์ด ๋‚ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ๊ฒƒ์ด ๋ชจ๋‘ countdown(3)์„ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ์ผ์–ด๋‚˜๋Š” ์ผ๋“ค์ž…๋‹ˆ๋‹ค. ์ด ์ผ๋“ค์„ ํ•˜๋Š” ๋™์•ˆ countdown ํ•จ์ˆ˜๋Š” ๋„ค ๋ฒˆ์ด๋‚˜ ํ˜ธ์ถœ๋˜์—ˆ์ง€์š”. countdown(3), countdown(2), countdown(1), countdown(0). ๋งž๋‚˜์š”? ์–ด๋– ์„ธ์š”? ์ดํ•ดํ•  ๋งŒํ•œ๊ฐ€์š”? ์ข€ ์–ด๋ ต๊ธด ํ•˜์ง€๋งŒ ์•Œ๊ณ  ๋‚˜๋ฉด ๊ฝค ์žฌ๋ฏธ๊ฐ€ ์žˆ์ง€์š”. ์‚ฌ์‹ค ์ด ์˜ˆ์ œ์—์„œ๋Š” for ๋ฌธ์„ ์ด์šฉํ•ด๋„ ์–ผ๋งˆ๋“ ์ง€ ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์„ ์˜ˆ๋กœ ๋“ค์—ˆ์ง€๋งŒ ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ์žฌ๊ท€์  ํ˜ธ์ถœ์„ ์ž˜ ์‚ฌ์šฉํ•˜๋ฉด ๋ณต์žกํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์•„์ฃผ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ํ•จ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœํ•ด์„œ ์ผ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ๋žŒ์ด ํŽธํ•œ ๋งŒํผ ์ปดํ“จํ„ฐ์—๊ฒŒ๋Š” ํž˜์ด ๋” ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด๊ธฐ๋„ ํ•˜๋‹ต๋‹ˆ๋‹ค. ์ด ๋‚ด์šฉ์€ ์ง€๊ธˆ ๊ผญ ์ดํ•ดํ•˜์ง€ ๋ชปํ•ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณต๋ถ€๋ฅผ ๊ณ„์†ํ•˜์‹œ๋‹ค ๋ณด๋ฉด ๋‹ค์‹œ ๋ฐฐ์šฐ๊ฒŒ ๋  ํ…Œ๋‹ˆ๊นŒ์š”. ์˜ˆ์ „์— ๋ฐฐ์šด ๋‚ด์šฉ์„ ํ™•์‹คํžˆ ๋ชฐ๋ผ์„œ ์ด ๋‚ด์šฉ์„ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜์…จ๋‹ค๋ฉด ๋ณต์Šต์„ ์ข€ ๋” ํ•˜์‹  ํ›„์— ๋‹ค์‹œ ๋ณด์‹œ๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ๋„ค์š”. A.1.1 ์—ฐ์Šต ๋ฌธ์ œ: ๊ฐ ์ž๋ฆฌ ์ˆซ์ž์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜ ๋ฌธ์ œ 1 ์–ด๋–ค ์ˆ˜(number)์˜ ๊ฐ ์ž๋ฆฌ ์ˆซ์ž(digit)์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋Š” sumOfDigits()๋ผ๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์ž. ์ž…๋ ฅํ•œ ์ˆ˜๋ฅผ ์ฝ์–ด sumOfDigits() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉฐ, ์ด ํ•จ์ˆ˜๋Š” ํ•ฉ์‚ฐํ•  ์ˆซ์ž๊ฐ€ ๋‚จ์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ์ž์‹ ์„ ํ˜ธ์ถœํ•ด, ์ตœ์ข…์ ์ธ ํ•ฉ์„ ์‚ฌ์šฉ์ž์—๊ฒŒ ํ‘œ์‹œํ•œ๋‹ค. sumOfDigits()๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์›๋ฆฌ๋กœ ์ž‘๋™ํ•œ๋‹ค.(์ฃผ์˜: ์‹ค์ œ ์ฝ”๋“œ๊ฐ€ ์•„๋‹˜) sumOfDigits(6452) = 2 + sumOfDigits(645) sumOfDigits(645) = 5 + sumOfDigits(64) ... sumOfDigits(6) = 6 ์˜ˆ 1 ์ž…๋ ฅ: 47253 ์ถœ๋ ฅ: 21 ์˜ˆ 2 ์ž…๋ ฅ: 643 ์ถœ๋ ฅ: 13 ํ’€์ด ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•œ ๋Œ€๋กœ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ: ch10/sumOfDigits_recursive.py ํ๋ฆ„๋„ ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์žฌ๊ท€์ ์ด์ง€ ์•Š์€ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ch10/sumOfDigits_non-recursive_div-mod.py edX.org์˜ C Programming: Modular Programming and Memory Management์— ์‹ค๋ฆฐ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. โ†ฉ A.1.2 ์—ฐ์Šต ๋ฌธ์ œ: ๋ณต๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜ ๋ณต๋ฆฌ ๊ณ„์‚ฐ ๋ฌธ์ œ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํ’€์–ด๋ด…์‹œ๋‹ค. ๋ฌธ์ œ ๋ฌธ์ œ 1 ๋ฌธ์ œ๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋ณต๋ฆฌ ๊ณ„์‚ฐ ๊ณต์‹์„ ์กฐ๊ธˆ ๋ฐ”๊ฟ” ๋ณผ๊ฒŒ์š”. ์›๋ž˜ ๊ณต์‹์—๋Š” ๋ณต๋ฆฌ ํšŸ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ, ๋ณต๋ฆฌ ํšŸ์ˆ˜๊ฐ€ 1์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ๊ณต์‹์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„๋‹จํžˆ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. โ€ฒ P ( + ) P : ์›๊ธˆ โ€ฒ : ์›๋ฆฌ๊ธˆ : ์ด์œจ : ๊ธฐ๊ฐ„ ๊ทธ๋Ÿฐ๋ฐ ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์œ„ ๊ณต์‹์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์ง€ ๋ง๊ณ  ์žฌ๊ท€์ ์œผ๋กœ ํ’€์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ด๊ฐ’์˜ ์†Œ์ˆ˜์  ์ดํ•˜๋Š” ๊ทธ๋Œ€๋กœ ๋‘์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ 1 ์—ฐ์ด์œจ 5.8%์ธ ์—ฐ ๋ณต๋ฆฌ ์ƒํ’ˆ์— 360๋งŒ ์›์„ 2๋…„๊ฐ„ ์˜ˆ์น˜ํ–ˆ์„ ๋•Œ ๋งŒ๊ธฐ ์ˆ˜๋ น์•ก: >>> compound_interest_amount_withoutN(3600000, 0.058, 2) 4029710.4000000004 ์˜ˆ 2 ์ฒœ๋งŒ ์›์„ ์—ฐ์ด์œจ 5% ์›”๋ณต๋ฆฌ ์˜ˆ๊ธˆ์— 1๋…„ ๋„ฃ์—ˆ์„ ๋•Œ ๋งŒ๊ธฐ ์ˆ˜๋ น์•ก: >>> 0.05 / 12 0.004166666666666667 >>> compound_interest_amount_withoutN(10000000, _, 12) 10511618.978817329 ๋ฌธ์ œ 2 (์‹ฌํ™”) ์œ„ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค๋ฉด ์•„๋ž˜ ๊ณต์‹์œผ๋กœ๋„ ํ’€์–ด๋ณด์„ธ์š”. โ€ฒ P ( + n ) t : ์›๊ธˆ โ€ฒ : ์›๋ฆฌ๊ธˆ : ์ด์œจ : ๋ณต๋ฆฌ ํšŸ์ˆ˜ : ๊ธฐ๊ฐ„ ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์—„์ฒญ ์ž˜ ํ•˜์‹œ๋Š” ๊ฒƒ์œผ๋กœ ์ธ์ •! ์•„, ์žฌ๊ท€์ ์œผ๋กœ ํ’€์—ˆ์„ ๋•Œ๋งŒ์š”. ์˜ˆ 1 >>> compound_interest_amount(1500000, 0.043, 6, 4) 1938836.8221341053 ์˜ˆ 2 >>> compound_interest_amount(1500000, 0.043, 6, 1/2) 1921236.0840000005 ์˜ˆ 3 ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด ๋งˆ์ง€๋ง‰ ์ธ์ž๋กœ 1์„ ์ „๋‹ฌํ•ด์„œ 1๋ฒˆ ๋ฌธ์ œ๋„ ํ’€ ์ˆ˜ ์žˆ์–ด์š”. >>> compound_interest_amount(3600000, 0.058, 2, 1) 4029710.4000000004 ํ’€์ด ์ฝ”๋“œ: ch10/compoundInterest_recursive.py ๊ตฌ๊ธ€ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ ํ๋ฆ„๋„ ์ฐธ๊ณ  ๋ณต๋ฆฌ ๊ณ„์‚ฐ๋ฒ•, ํƒฑ์Šคํƒ€ ์˜ˆ์ ๊ธˆ ๋‹จ๋ฆฌ ๋ณต๋ฆฌ ์ฐจ์ด์  ์•Œ์•„๋ณด๊ธฐ, ์น˜ ํ‚จ ์š”์ •์˜ ๊ฒฝ์ œ ๊ณต๋ถ€๋ฐฉ A.2 ์—ฐ์Šต ๋ฌธ์ œ: ์—ฌ๋Ÿฌ ๋Œ€์˜ ์ปดํ“จํ„ฐ์— ์—ฐ์‚ฐ์„ ๋ถ„๋ฐฐํ•˜๊ธฐ 4์žฅ์— ์žˆ๋˜ ์—ฐ์Šต ๋ฌธ์ œ์ธ๋ฐ ์ข€ ์–ด๋ ค์›Œ์„œ ๋’ค๋กœ ์˜ฎ๊ฒผ์Šต๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ํ’€์–ด๋ณด์„ธ์š”. ๋ฌธ์ œ ์ปดํ“จํ„ฐ๊ฐ€ ๋ช‡ ๋Œ€ ์žˆ๊ณ  ์—ฐ์‚ฐํ•ด์•ผ ํ•  ํ”„๋กœ๊ทธ๋žจ๋„ ๋ช‡ ๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ๋“ค์„ ์ปดํ“จํ„ฐ์— ๊ฐ€์žฅ ์ ์ ˆํ•˜๊ฒŒ ๋ถ„๋ฐฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์˜ˆ) ์ปดํ“จํ„ฐ๋Š” 2๋Œ€๊ฐ€ ์žˆ๊ณ , ํ”„๋กœ๊ทธ๋žจ์˜ ์ˆ˜ํ–‰ ์‹œ๊ฐ„์€ ๊ฐ 3๋ถ„, 5๋ถ„, 2๋ถ„์ด๋ผ๋ฉด, ์ปดํ“จํ„ฐ ํ•˜๋‚˜๋Š” 3๋ถ„, 2๋ถ„์งœ๋ฆฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ๋Š” 5๋ถ„์งœ๋ฆฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ computer : 2 program : 3, 5, 2 ์ถœ๋ ฅ computer1 : 5 computer2 : 3, 2 ์–ด๋– ์„ธ์š”? ์ข€ ์–ด๋ ต์ฃ ? ์ €๋„ ์ด ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋ฌธ์ œ๊ฐ€ ๋ฌด์Šจ ๋œป์ธ์ง€ ๋ชฐ๋ผ์„œ ํ•œ์ฐธ ํ—ค๋งธ๋‹ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋ช…ํ•˜์‹  ์—ฌ๋Ÿฌ๋ถ„์€ ๊ธˆ๋ฐฉ ์ดํ•ดํ•˜์…จ์œผ๋ฆฌ๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณผ ํ…๋ฐ์š”, ์•„๋ž˜์˜ ํ•ด์„ค์„ ๋ณด์‹œ๊ธฐ ์ „์— ๋ฌธ์ œ๋ฅผ ๋‹ค๋ฅธ ๊ณณ์— ์˜ฎ๊ฒจ๋†“๊ณ  ์ž ์‹œ ์ƒ๊ฐ์„ ํ•ด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•˜๋ฉด ์ง์ ‘ ํ’€์–ด๋ณด๋Š” ๊ฒƒ์ด ์ข‹์œผ๋‹ˆ๊นŒ์š”. ์ปดํ“จํ„ฐ ์—ฌ๋Ÿฌ ๋Œ€๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์„ ๋‚˜๋ˆ ์„œ ์ˆ˜ํ–‰ํ•œ๋‹ค๋ฉด, ๊ฐ๊ฐ์˜ ์ปดํ“จํ„ฐ์—๊ฒŒ ๊ฐ™์€ ์–‘์˜ ์ผ์„ ์ค˜์„œ ๊ฐ™์ด ๋๋‚ด๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์ฃ ? ์˜ˆ์—์„œ๋Š” ์ด 10๋ถ„ ๋™์•ˆ ํ•  ์ผ์„ ์ปดํ“จํ„ฐ ๋‘ ๋Œ€์—๊ฒŒ ๋‚˜๋ˆ ์ฃผ๋Š” ๊ฑฐ๋‹ˆ๊นŒ ๊ฐ๊ฐ 5๋ถ„์”ฉ์ผ์„ ์‹œํ‚ค๋ฉด ๋˜๋Š” ๊ฑฐ๊ณ ์š”. ๋งŒ์•ฝ, ์ˆ˜ํ–‰ํ•  ํ”„๋กœ๊ทธ๋žจ ์ค‘์— ํ‰๊ท ๋ณด๋‹ค ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ์ด ์žˆ๋‹ค๋ฉด, ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ํ”„๋กœ๊ทธ๋žจ์ด 7๋ถ„, 3๋ถ„ ๋‘ ๊ฐœ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์–ด๋–ป๊ฒŒ ๋‚˜๋ˆ ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์„๊นŒ์š”? ํ•  ์ˆ˜ ์—†์ด ํ•œ ๋Œ€๋Š” 7๋ถ„ ๋™์•ˆ, ๋‹ค๋ฅธ ํ•œ ๋Œ€๋Š” 3๋ถ„ ๋™์•ˆ ์ผ์„ ํ•ด์•ผ๊ฒ ์ฃ ? 7๋ถ„์งœ๋ฆฌ๋ฅผ 5๋ถ„, 2๋ถ„์œผ๋กœ ์ชผ๊ฐœ์„œ ๋„˜๊ฒจ์ฃผ๋ผ๊ณ ์š”? ์‹œ๋กœ~. ํ•ด๋ฒ• ๋ฌธ์ œ๋ฅผ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ sol1() ํ•จ์ˆ˜, ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ sol2() ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๋‘ ํ•จ์ˆ˜์— ๊ณตํ†ต์œผ๋กœ ๋“ค์–ด๊ฐ„ ์ฝ”๋“œ๋ฅผ ์„ค๋ช…๋“œ๋ฆด๊ฒŒ์š”. ๊ณตํ†ต์ ์ธ ๋ถ€๋ถ„ ํ•จ์ˆ˜ ์ž…์ถœ๋ ฅ<NAME> ์˜ˆ์ œ์—์„œ๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ ๋ฆฌ์ŠคํŠธ์™€ ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ„ํŽธํ•˜๊ณ  ๋ฌธ์ œ์— ์ง‘์ค‘ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์„œ์š”. ์ž…๋ ฅ: ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜: ๋ฆฌ์ŠคํŠธ(์˜ˆ: [3,5,2]) ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜: ์ •์ˆ˜(์˜ˆ: 2) ์ถœ๋ ฅ: ๋ฆฌ์ŠคํŠธ(์˜ˆ: [[5], [3, 2]]) ์ดˆ๊ธฐํ™” ๋‘ ํ•จ์ˆ˜์˜ ์ฒซ ๋ถ€๋ถ„์—๋Š” ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ๊ณตํ†ต์œผ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. _inlist = copy.deepcopy(inlist) outlist = [] sumout = [] for x in range(coms): outlist.append([]) sumout.append(0) ์ฒซ์งธ ์ค„์—์„œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ inlist๋ฅผ _inlist๋กœ ๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ํ–ˆ์Šต๋‹ˆ๋‹ค. outlist๊ฐ€ ๋ฐ”๋กœ ์ปดํ“จํ„ฐ๋“ค์˜ ๋ฆฌ์ŠคํŠธ์ด๊ณ , sumout์€ ์ปดํ“จํ„ฐ๋งˆ๋‹ค ์ˆ˜ํ–‰ํ•  ํ”„๋กœ๊ทธ๋žจ์˜ ์ˆ˜ํ–‰ ์‹œ๊ฐ„ ํ•ฉ๊ณ„๋ฅผ ๊ฐ–๋Š” ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. coms๋Š” ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ์ปดํ“จํ„ฐ ๋Œ€์ˆ˜์ž…๋‹ˆ๋‹ค. coms ๊ฐ’์ด 3์ด๋ผ๋ฉด ์œ„์˜ for ๋ฌธ์„ ์ˆ˜ํ–‰ํ•œ ํ›„์— outlist๋Š” [[], [], []]์™€ ๊ฐ™์ด ๋˜๊ณ , sumout์€ [0, 0, 0]์ด ๋ฉ๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ํ•จ์ˆ˜๊ฐ€ ๋ชจ๋‘ ์‹คํ–‰๋˜๊ณ  ๋‚˜๋ฉด outlist์—๋Š” ๊ฒฐ๊ด๊ฐ’์ด [[22], [15, 3, 2], [13, 6]]์™€ ๊ฐ™์ด ๋‹ด๊ธฐ๊ณ , ๊ทธ๋•Œ sumout์€ [22, 20, 19]์™€ ๊ฐ™์ด ๋˜์–ด์žˆ๋„๋ก ํ•  ์ƒ๊ฐ์ด์ง€์š”. ํ•ด๋ฒ• 1 ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์ฒซ ๋ฒˆ์งธ ํ•ด๋ฒ•์˜ ๋…ผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ๋“ค์„ ์ „๋ถ€ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ด ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค. ์ด ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ์ปดํ“จํ„ฐ ๋Œ€์ˆ˜๋กœ ๋‚˜๋ˆ„์–ด ๊ฐ๊ฐ์˜ ์ปดํ“จํ„ฐ์˜ ํ‰๊ท  ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค. ๋งŒ์•ฝ ํ‰๊ท  ์ˆ˜ํ–‰ ์‹œ๊ฐ„๋ณด๋‹ค ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์žˆ์œผ๋ฉด: ๊ทธ๋ƒฅ ์ปดํ“จํ„ฐ์—๊ฒŒ ์ค€๋‹ค. ํ‰๊ท  ์ˆ˜ํ–‰ ์‹œ๊ฐ„๋ณด๋‹ค ์งง๊ฒŒ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ๋“ค์€: ํ‰๊ท  ์ˆ˜ํ–‰ ์‹œ๊ฐ„๋งŒํผ ๋ชจ์•„์„œ ์ปดํ“จํ„ฐ์—๊ฒŒ ์ค€๋‹ค. ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. def sol1(inlist, coms): ... # ํ”„๋กœ๊ทธ๋žจ๋“ค์„ ์ „๋ถ€ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ด ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค. total_time = sum(_inlist) # ์ด ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ์ปดํ“จํ„ฐ ๋Œ€์ˆ˜๋กœ ๋‚˜๋ˆ  ๊ฐ ์ปดํ“จํ„ฐ์˜ ํ‰๊ท  ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค. avg_time = total_time / coms for j in range(coms): # ํ‰๊ท  ์ˆ˜ํ–‰ ์‹œ๊ฐ„๋ณด๋‹ค ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์žˆ์œผ๋ฉด ๊ทธ๋ƒฅ ์ปดํ“จํ„ฐ์—๊ฒŒ ์ค€๋‹ค. if True in [k >= avg_time for k in _inlist]: for m in _inlist: if m >= avg_time: outlist[j].append(m) _inlist.remove(m) sumout[j] += m break # ํ‰๊ท  ์ˆ˜ํ–‰ ์‹œ๊ฐ„๋ณด๋‹ค ์งง๊ฒŒ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ๋“ค์€ ๋ชจ์•„์„œ ์ปดํ“จํ„ฐ์—๊ฒŒ ์ค€๋‹ค. else: for n in range(len(_inlist)): if _inlist[n] == 0: continue v = _inlist[n] outlist[j].append(v) _inlist[n] = 0 sumout[j] += v if sumout[j] >= avg_time: break return outlist ์ฒ˜์Œ์—๋Š” ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋Š”๋ฐ, ๋‚˜์ค‘์— ๋‹ค์‹œ ๋ณด๋‹ˆ๊นŒ ๋ณต์žกํ•˜๋ฉด์„œ๋„ ๋น„ํšจ์œจ์ ์ด๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค๋”๊ตฐ์š”. ํ•ด๋ฒ• 2 ๊ทธ๋ž˜์„œ ๋ช‡ ๋‚  ๋ฉฐ์น ์„ ๊ณ ๋ฏผํ•˜๋‹ค ๋ณด๋‹ˆ ๊ธฐ๋ฐœํ•œ ์•„์ด๋””์–ด๊ฐ€ ๋– ์˜ฌ๋ž์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์˜ ํ•ต์‹ฌ์€ '๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ๋ฐ”๊ตฌ๋‹ˆ์— ๋นต์„ ๋‹ด๋Š”๋‹ค'๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. '์ปดํ“จํ„ฐ'์™€ '์ˆ˜ํ–‰ํ•  ํ”„๋กœ๊ทธ๋žจ' ๋Œ€์‹  '๋ฐ”๊ตฌ๋‹ˆ'์™€ '๋นต'์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ ๊ฒƒ์ด์ฃ . ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด, ์ปดํ“จํ„ฐ์—๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์„ ๋ถ„๋ฐฐํ•˜๋Š” ๊ฒƒ์€ ๋ฐ”๊ตฌ๋‹ˆ์— ๋นต์„ ํ•˜๋‚˜์”ฉ ๋‹ด๋Š” ๊ฒƒ๊ณผ ํก์‚ฌํ•˜์ง€์š”. ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ค€๋น„ํ•œ๋‹ค. ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด์„ ๋นต๋“ค์„ ํฌ๊ธฐ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. ๋นต์„ ๋ชจ๋‘ ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด์„ ๋•Œ๊นŒ์ง€: ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ๋ฐ”๊ตฌ๋‹ˆ์— ๊ฐ€์žฅ ํฐ ๋นต์„ ๋‹ด๋Š”๋‹ค. ๊ฒฐ๊ณผ๋ฅผ ๋Œ๋ ค์ค€๋‹ค. ์ด ๋…ผ๋ฆฌ๋ฅผ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด ์•„๋ž˜์˜ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ๋Œ€ํ™”์‹์œผ๋กœ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ…์ŠคํŠธ ์—๋””ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ”„๋กœ๊ทธ๋žจ ํŒŒ์ผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์ฃ ? def sol2(inlist, coms): ... _inlist.sort(reverse=True) # ์ˆ˜ํ–‰ํ•  ์ž‘์—…์„ ๋นต(bread)์—, ์ปดํ“จํ„ฐ๋ฅผ ๋ฐ”๊ตฌ๋‹ˆ(basket)์— ๋น„์œ  for bread in _inlist: lowbasket = sumout.index(min(sumout)) # ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ๋ฐ”๊ตฌ๋‹ˆ์— outlist[lowbasket].append(bread) # ๋นต์„ ๋‹ด๋Š”๋‹ค sumout[lowbasket] += bread return outlist ์ •๋ง ๊ฐ„๋‹จํ•˜์ฃ ? ์˜ˆ์ „์— ์†Œ๊ฐœํ•ด ๋“œ๋ ธ๋˜ ํ’€์ด๋ฅผ ๋ณด์…จ๋˜ ๋ถ„์ด๋ผ๋ฉด ๋†€๋ผ์‹ค ๊ฑฐ์˜ˆ์š”. ์ € ์Šค์Šค๋กœ๋„ ๊ฐํƒ„ํ–ˆ๊ฑฐ๋“ ์š”. ํ”„๋กœ๊ทธ๋žจ์„ ์ „์ฒด์ ์œผ๋กœ ๋ณด๋ฉด sol2()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ •์˜ํ•ด๋‘๊ณ , ๋’ค์— ๊ฐ€์„œ ๊ทธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , sol2() ํ•จ์ˆ˜๋Š” inlist์™€ coms๋ผ๋Š” ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์„œ, outlist๋ฅผ ๊ฒฐ๊ณผ๋กœ ๋Œ๋ ค์ค๋‹ˆ๋‹ค. ์ด์ œ ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€๋ฅผ ์‚ดํŽด๋ด…์‹œ๋‹ค. "๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด์„ ๋นต๋“ค์„ ํฌ๊ธฐ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค." _inlist.sort(reverse=True) _inlist๋ฅผ sort() ๋ฉ”์„œ๋“œ๋ฅผ ์จ์„œ ์ •๋ ฌํ•˜๋˜, reverse ์˜ต์…˜์— True๋ฅผ ์ง€์ •ํ•จ์œผ๋กœ์จ ์ •๋ ฌ์ด ์—ญ์ˆœ(๋‚ด๋ฆผ์ฐจ์ˆœ)์œผ๋กœ ์ด๋ฃจ์–ด์ง€๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. 1 ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ์ค„๋กœ ์“ด ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. _inlist.sort() _inlist.reverse() ์ด์ œ ๊ฐ๊ฐ์˜ ๋นต์„ ๋ฐ”๊ตฌ๋‹ˆ๋กœ ๋ถ„๋ฐฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. for bread in _inlist: lowbasket = sumout.index(min(sumout)) # ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ๋ฐ”๊ตฌ๋‹ˆ์— outlist[lowbasket].append(bread) # ๋นต์„ ๋‹ด๋Š”๋‹ค sumout[lowbasket] += bread ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด min(sumout)์„ ํ–ˆ๊ณ , ๊ทธ ๋ฐ”๊ตฌ๋‹ˆ์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ตฌํ•ด์„œ lowbasket์ด๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ฐ”๊ตฌ๋‹ˆ๋Š” outlist์™€ sumout์—์„œ ๊ฐ™์€ ์ธ๋ฑ์Šค๋ฅผ ์“ด๋‹ค๋Š” ์ ์„ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด์ง€์š”. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ, ๋งŒ์•ฝ outlist ์ „์ฒด๊ฐ€ [[22], [15], [13, 6]]์ธ ์ƒํƒœ๋ผ๋ฉด sumout์€ [22, 15, 19]๊ฐ€ ๋˜์–ด์žˆ์„ ํ…Œ๊ณ ์š”, ํ˜„์žฌ ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ๋ฐ”๊ตฌ๋‹ˆ๋Š” 15๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฐ”๊ตฌ๋‹ˆ์ž…๋‹ˆ๋‹ค. >>> outlist = [[22], [15], [13, 6]] >>> sumout = [22, 15, 19] >>> min(sumout) 15 ์ด๊ฒƒ์˜ ์ธ๋ฑ์Šค๋ฅผ ์•Œ์•„๋ณด๋ฉด 1์ด๋ผ๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ์ง€์š”. >>> sumout.index(15) ์ด์ œ ๊ตฌํ•ด์ง„ ์ธ๋ฑ์Šค๋ฅผ lowbasket์ด๋ผ๊ณ  ํ•˜๊ณ , outlist์™€ sumout์—์„œ ์ธ๋ฑ์Šค 1์ธ ๋ฐ”๊ตฌ๋‹ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. >>> outlist[1] [15] >>> sumout[1] 15 ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์œผ๋กœ ๋‘ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ™์€ ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ถ€๋ถ„์„ ์ฐพ์•„์„œ ๋ฐ”๊ตฌ๋‹ˆ์— ๋นต์„ ๋‹ด์„ ๋•Œ, ๊ทธ ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ์–ผ๋งˆ๋‚˜ ์ฐผ๋Š”์ง€๋„ ํ•จ๊ป˜ ์ฒดํฌํ•ด๋‘๋Š” ๊ฒ๋‹ˆ๋‹ค. _inlist์˜ ๋ชจ๋“  ์›์†Œ์— ๋Œ€ํ•ด ์ด ์ผ์„ ๋ฐ˜๋ณตํ–ˆ์œผ๋ฉด, ์ฆ‰ ๋ชจ๋“  ๋นต์„ ๋ฐ”๊ตฌ๋‹ˆ์— ๋‚˜๋ˆ  ๋‹ด์•˜์œผ๋ฉด, ๊ตฌํ•ด์ง„ outlist๋ฅผ ํ•จ์ˆ˜์˜ ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ๋Œ๋ ค์ค๋‹ˆ๋‹ค. return outlist ๋์ด์—์š”~. ์ด์ œ ํ”„๋กœ๊ทธ๋žจ์„ ํ•œ๋ฒˆ ๋Œ๋ ค๋ณด์„ธ์š”. ์•„์ฃผ ์ž˜~ ๋  ๊ฑฐ์˜ˆ์š”~ 2 ๋ณด์ถฉ ์„ค๋ช… ์•„๋ž˜๋Š”<NAME>๋‹˜๊ป˜์„œ ์„ค๋ช…ํ•ด ์ฃผ์‹  ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํ”„๋กœ์„ธ์Šค ์Šค์ผ€์ค„๋ง ๋ฐฉ์‹ ์ค‘์— LPT(Longest Processing Time) ์Šค์ผ€์ค„๋ง์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๊ธด ์‹œ๊ฐ„์„ ์š”ํ•˜๋Š” Job๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ€์žฅ ์ ์€ ์—…๋ฌด๋ฅผ ๋งก์€ Line์— ๋ฐฐ๋ถ„ํ•˜๋Š” ๋ฐฉ์‹์ด์ง€์š”~ ์‹ค์ œ๋กœ ์ด ๋ฐฉ์‹์˜ ๊ฒฝ์šฐ 100% ์ตœ์ ํ•ด๋ฅผ ์ฐพ๋Š”๋‹ค๊ณ  ๋งํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ, ์ด ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด Parallel Line(์ปดํ“จํ„ฐ๊ฐ€ 2๋Œ€)์—์„œ ์ตœ์ ํ•ด ๋ณด๋‹ค 33% ์ด์ƒ ๋‚˜์˜์ง€ ์•Š๊ณ , Line์˜ ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ• ์ˆ˜๋ก ์ตœ์ ํ•ด์— ๊ฐ€๊นŒ์šด ๊ฒฐ๊ด๊ฐ’์„ ๋„์ถœํ•˜๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค! Line์˜ ์ˆ˜๊ฐ€ ๋งŽ์•„์ง€๊ณ  Job์˜ ์ˆ˜๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์ตœ์ ํ•ด๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋Š” ๊ฒฐ๊ณผ์˜ ๊ฐ€์ง“์ˆ˜๊ฐ€ ๋งค์šฐ ๋งŽ์•„์ง€๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋Ÿฐ ๋‹จ์ˆœ ์Šค์ผ€์ค„๋ง์˜ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜๊ธฐ ๋งค์šฐ ๊ฒฝ์ œ์ ์ธ ๋ฐฉ๋ฒ•์ด์ฃ ! ๋‹ค์Œ์€ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค~! Jobs 1 2 3 4 5 6 7 8 9 10 11 Pt 9 9 8 8 7 7 6 6 5 5 5 Pt: Processing time, # of Machines = 5 ์ตœ์ ํ•ด: Hour 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Line 1 [ Job 1 ] [ Job 8 ] Line 2 [ Job 2 ] [ Job 7 ] Line 3 [ Job 3 ] [ Job 6 ] Line 4 [ Job 4 ] [ Job 5 ] Line 5 [ Job 9 ] [ Job 10 ] [ Job 11 ] LPT: Hour 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Line 1 [ Job 1 ] [ Job 10 ] [ Job 11 ] Line 2 [ Job 2 ] [ Job 9 ] Line 3 [ Job 3 ] [ Job 8 ] Line 4 [ Job 4 ] [ Job 7 ] Line 5 [ Job 5 ] [ Job 6 ] ์ฝ”๋“œ ch10/prg2com.py Lilly ๋‹˜๊ป˜์„œ ์•Œ๋ ค์ฃผ์…จ์Šต๋‹ˆ๋‹ค. โ†ฉ ํ•ด๋ฒ• 2๋„ ์™„๋ฒฝํ•œ ํ•ด๊ฒฐ์ฑ…์€ ์•„๋‹™๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, prg2com([7,8,9,10,11], 2)์˜ ์ด์ƒ์ ์ธ ๊ฒฐ๊ณผ๋Š” [[11,10], [9,8,7]]์ด์ง€๋งŒ, ์œ„์—์„œ ์ž‘์„ฑํ•œ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด [[11, 8, 7], [10, 9]]๋ผ๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. kds079 ๋‹˜๊ป˜์„œ ์•Œ๋ ค์ฃผ์…จ์Šต๋‹ˆ๋‹ค. โ†ฉ A.3 ์ง„๋ฒ• ๋ณ€ํ™˜๊ณผ ๋น„ํŠธ ์—ฐ์‚ฐ ํŒŒ์ด์ฌ์œผ๋กœ ์ง„๋ฒ• ๋ณ€ํ™˜๊ณผ ๋น„ํŠธ ์—ฐ์‚ฐ(bitwise operation)์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ธˆ ์–ด๋ ค์šธ ์ˆ˜๋„ ์žˆ๊ณ  ๊ผญ ํ•„์š”ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ, ๊ด€์‹ฌ ์žˆ๋Š” ๋ถ„๋งŒ ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ง„๋ฒ• ๋ณ€ํ™˜ ์ด์ง„์ˆ˜ ํŒŒ์ด์ฌ์—์„œ ์ด์ง„์ˆ˜(binary)๋Š” 0b๋ฅผ ์•ž์— ๋ถ™์—ฌ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. >>> 0b0 # ์ด์ง„์ˆ˜ 0์€ ์‹ญ์ง„์ˆ˜ 0 >>> 0b1 # ์ด์ง„์ˆ˜ 1์€ ์‹ญ์ง„์ˆ˜ 1 >>> 0b10 # ์ด์ง„์ˆ˜ 10์€ ์‹ญ์ง„์ˆ˜ 2 >>> 0b11 # ์ด์ง„์ˆ˜ 11์€ ์‹ญ์ง„์ˆ˜ 3 ์‹ญ์ง„์ˆ˜๋ฅผ ์ด์ง„์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ๋‹ค๋ฉด bin() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. >>> bin(0) # ์‹ญ์ง„์ˆ˜ 0์€ ์ด์ง„์ˆ˜ 0 '0b0' >>> bin(1) # ์‹ญ์ง„์ˆ˜ 0์€ ์ด์ง„์ˆ˜ 1 '0b1' >>> bin(2) # ์‹ญ์ง„์ˆ˜ 2๋Š” ์ด์ง„์ˆ˜ 10 '0b10' >>> bin(3) # ์‹ญ์ง„์ˆ˜ 3์€ ์ด์ง„์ˆ˜ 11 '0b11' 8์ง„์ˆ˜์™€ 16์ง„์ˆ˜ ๋น„์Šทํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์‹ญ์ง„์ˆ˜๋ฅผ 8์ง„์ˆ˜์™€ 16์ง„์ˆ˜๋กœ๋„ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์”ฉ ์„ค๋ช…ํ•ด ๋“œ๋ฆฌ์ง€ ์•Š์•„๋„ ์•„์‹ค ๊ฒƒ ๊ฐ™์œผ๋‹ˆ, ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ•œ๊บผ๋ฒˆ์— ๋ณด์—ฌ๋“œ๋ฆด๊ฒŒ์š”. >>> for i in range(20): ... print(i, bin(i), oct(i), hex(i)) # ์‹ญ์ง„์ˆ˜, ์ด์ง„์ˆ˜, 8์ง„์ˆ˜, 16์ง„์ˆ˜๋ฅผ ์ถœ๋ ฅ ... 0 0b0 0o0 0x0 1 0b1 0o1 0x1 2 0b10 0o2 0x2 3 0b11 0o3 0x3 4 0b100 0o4 0x4 5 0b101 0o5 0x5 6 0b110 0o6 0x6 7 0b111 0o7 0x7 8 0b1000 0o10 0x8 9 0b1001 0o11 0x9 10 0b1010 0o12 0xa 11 0b1011 0o13 0xb 12 0b1100 0o14 0xc 13 0b1101 0o15 0xd 14 0b1110 0o16 0xe 15 0b1111 0o17 0xf 16 0b10000 0o20 0x10 17 0b10001 0o21 0x11 18 0b10010 0o22 0x12 19 0b10011 0o23 0x13 ๋ถˆ๊ฐ’์˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ ๋น„ํŠธ ์—ฐ์‚ฐ์„ ์•Œ์•„๋ณด๊ธฐ ์ „์— ๋ชธํ’€๊ธฐ๋กœ ๋ถˆ(bool) ๊ฐ’์˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ๋ถ€ํ„ฐ ์•Œ์•„๋ณผ๊ฒŒ์š”. ๋ถˆ์€ ์ฐธ(True)๊ณผ ๊ฑฐ์ง“(False)์˜ ๋‘ ๊ฐ’๋งŒ ์žˆ๋Š” ์ž๋ฃŒํ˜•์ž…๋‹ˆ๋‹ค. ๋จผ์ € and ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ๋ชจ๋‘ True ์—ฌ์•ผ ๊ฒฐ๊ณผ๋„ True๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. >>> True and True True >>> True and False False >>> False and True False >>> False and False False ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„๋ฆฌํ‘œ๋กœ๋„ ๋‚˜ํƒ€๋‚ด๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. and True False True True False False False False ๋‹ค์Œ์œผ๋กœ or ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค. ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ True ์ด๋ฉด ๊ฒฐ๊ณผ๋Š” True๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค False ์ผ ๋•Œ๋งŒ ๊ฒฐ๊ณผ๊ฐ€ False๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. >>> True or True True >>> True or False True >>> False or True True >>> False or False False ์ง„๋ฆฌํ‘œ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. or True False True True True False True False and์™€ or ์—ฐ์‚ฐ์ž๋Š” ๋‘ ๊ฐœ์˜ ํ•ญ(ํ”ผ์—ฐ์‚ฐ์ž)์„ ๊ฐ–๋Š”๋‹ค๋Š” ๋œป์œผ๋กœ '์ดํ•ญ(ไบŒ้ …, binary)'๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” not ์—ฐ์‚ฐ์ž๋ฅผ ์•Œ์•„๋ณผ๊ฒŒ์š”. not True๋Š” False, not False๋Š” True์ž…๋‹ˆ๋‹ค. >>> not True False >>> not False True not ์—ฐ์‚ฐ์ž๋Š” '๋‹จํ•ญ(ๅ–ฎ้ …, unary) ์—ฐ์‚ฐ์ž'์ž…๋‹ˆ๋‹ค. ๋ถˆ๊ณผ ์ •์ˆ˜์˜ ๊ด€๊ณ„ ๋‹ค์Œ์œผ๋กœ ํŒŒ์ด์ฌ์—์„œ ๋ถˆ๊ฐ’๊ณผ ์ •์ˆซ๊ฐ’์˜ ๊ด€๊ณ„๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ True๋Š” ์ˆซ์ž 1๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. >>> True == 1 # ๋“ฑํ˜ธ๊ฐ€ ๋‘ ๊ฐœ์ž„์— ์œ ์˜(ํ• ๋‹น ์—ฐ์‚ฐ์ด ์•„๋‹ˆ๋ผ ๋น„๊ต) True ๊ทธ๋ฆฌ๊ณ  False๋Š” ์ˆซ์ž 0๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. >>> False == 0 True ๋น„ํŠธ ์—ฐ์‚ฐ &๋Š” ๋น„ํŠธ AND ์—ฐ์‚ฐ์ž๋กœ, ์•ž์—์„œ ์‚ดํŽด๋ณธ and์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  |๋Š” ๋น„ํŠธ OR ์—ฐ์‚ฐ์ž๋กœ, or์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ํ•œ์ž๋ฆฌ ์ด์ง„์ˆ˜์˜ ๋น„ํŠธ ์—ฐ์‚ฐ ์šฐ์„  ์•„์ฃผ ์ž‘์€ ์ˆ˜๋ถ€ํ„ฐ ๊ณ„์‚ฐํ•ด ๋ณผ๊ฒŒ์š”. ์ด์ง„์ˆ˜ 1(0b1)๊ณผ ์ด์ง„์ˆ˜ 0(0b0)์ž…๋‹ˆ๋‹ค. ๋น„ํŠธ AND ์—ฐ์‚ฐ ๋จผ์ € ๋น„ํŠธ AND(&) ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. >>> bin(0b1 & 0b1) '0b1' >>> bin(0b1 & 0b0) '0b0' >>> bin(0b0 & 0b1) '0b0' >>> bin(0b0 & 0b0) '0b0' ๋น„ํŠธ OR ์—ฐ์‚ฐ ๋‹ค์Œ์€ ๋น„ํŠธ OR(|) ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. >>> bin(0b1 | 0b1) '0b1' >>> bin(0b1 | 0b0) '0b1' >>> bin(0b0 | 0b1) '0b1' >>> bin(0b0 | 0b0) '0b0' ๋น„ํŠธ NOT ์—ฐ์‚ฐ ๋น„ํŠธ NOT(~) ์—ฐ์‚ฐ๋„ ํ•ด๋ณผ๊นŒ์š”? >>> bin(~0b1) '-0b10' ๊ฒฐ๊ณผ๊ฐ€ ์Œ์ˆ˜๋กœ ๋‚˜์™”๋„ค์š”. ~ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋Š” ์›๋ž˜ ์ˆ˜์— ๋งˆ์ด๋„ˆ์Šค ๊ธฐํ˜ธ๋ฅผ ๋ถ™์ธ ๋’ค 1์„ ๋บ€ ๊ฐ’๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. >>> bin(~0b0) '-0b1' >>> bin(-0b0 - 0b1) '-0b1' ์œ„์—์„œ ๋‘ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. XOR ์—ฐ์‚ฐ XOR(๋ฐฐํƒ€์  OR, exclusive OR)์ด๋ผ๋Š” ์—ฐ์‚ฐ(^)๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐ’์ด ๋ชจ๋‘ 1์ด๊ฑฐ๋‚˜ ๋ชจ๋‘ 0์ด๋ฉด ๊ฒฐ๊ณผ๊ฐ€ 0์ด๊ณ , ๋‘ ๊ฐ’ ์ค‘ ํ•˜๋‚˜๋Š” 1์ด๊ณ  ๋‹ค๋ฅธ ํ•˜๋‚˜๊ฐ€ 0์ด๋ฉด ๊ฒฐ๊ณผ๊ฐ€ 1์ด ๋ฉ๋‹ˆ๋‹ค. >>> bin(0b1 ^ 0b1) '0b0' >>> bin(0b1 ^ 0b0) '0b1' >>> bin(0b0 ^ 0b1) '0b1' >>> bin(0b0 ^ 0b0) '0b0' ๊ทธ ์™ธ์˜ ๋น„ํŠธ ์—ฐ์‚ฐ์ž๋Š” ๋’ค์—์„œ ์•Œ์•„๋ณผ๊ฒŒ์š”. ๋„ค ์ž๋ฆฌ ์ด์ง„์ˆ˜์˜ ๋น„ํŠธ ์—ฐ์‚ฐ ํ•œ์ž๋ฆฌ์˜ ๋น„ํŠธ ์—ฐ์‚ฐ์ž๋ฅผ ์ดํ•ดํ–ˆ์œผ๋ฉด ๋„ค ์ž๋ฆฌ ์ด์ง„์ˆ˜๋กœ ๋น„ํŠธ ์—ฐ์‚ฐ์„ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  & ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. >>> bin(0b1111 & 0b1100) '0b1100' >>> bin(0b1010 & 0b1100) '0b1000' ๋น„ํŠธ ์—ฐ์‚ฐ์€ ํ•œ์ž๋ฆฌ์”ฉ ๋”ฐ๋กœ๋”ฐ๋กœ ๊ณ„์‚ฐ์„ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ฒฐ๊ณผ๋ฅผ ์ž˜ ์‚ดํŽด๋ณด์„ธ์š”. ๋‹ค์Œ์€ | ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. >>> bin(0b1111 | 0b1100) '0b1111' >>> bin(0b1010 | 0b1100) '0b1110' ~ ์—ฐ์‚ฐ๋„ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. >>> bin(~0b1100) '-0b1101' >>> bin(~0b1001) '-0b1010' ^ ์—ฐ์‚ฐ์€ ์–ด๋–ป๊ฒŒ ๋ ์ง€ ๊ถ๊ธˆํ•˜๊ตฐ์š”. >>> bin(0b1111 ^ 0b1100) '0b11' >>> bin(0b1010 ^ 0b1100) '0b110' ์‹œํ”„ํŠธ ์—ฐ์‚ฐ ์ด๋ฒˆ์—๋Š” ์‹œํ”„ํŠธ(shift) ์—ฐ์‚ฐ์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์™ผ์ชฝ ์‹œํ”„ํŠธ(<<)์ž…๋‹ˆ๋‹ค. >>> bin(0b1 << 1) '0b10' >>> bin(0b1 << 2) '0b100' >>> bin(0b1 << 3) '0b1000' ์œ„์—์„œ 1์ด ์ ์  ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์˜ค๋ฅธ์ชฝ ์‹œํ”„ํŠธ(>>)์ž…๋‹ˆ๋‹ค. >>> bin(0b1010 >> 1) '0b101' 1์ด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ์ž๋ฆฌ์”ฉ ๋ฐ€๋ฆฐ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ๋งŽ์ด ์‹œํ”„ํŠธ ํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? >>> bin(0b1010 >> 2) '0b10' >>> bin(0b1010 >> 3) '0b1' >>> bin(0b1010 >> 4) '0b0' ์ ์  ๋ฐ€๋ ค์„œ ๊ฒฐ๊ตญ 0์ด ๋˜์—ˆ๋„ค์š”. ์‹ญ์ง„์ˆ˜์˜ ๋น„ํŠธ ์—ฐ์‚ฐ ๊ทธ๋Ÿฐ๋ฐ ๋น„ํŠธ ์—ฐ์‚ฐ์„ ์ด์ง„์ˆ˜์—๋งŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋ž๋‹ˆ๋‹ค. ์•ž์—์„œ ์ด์ง„์ˆ˜๋กœ ํ–ˆ๋˜ ๊ณ„์‚ฐ์„ ์‹ญ์ง„์ˆ˜๋กœ ๋‹ค์‹œ ํ•ด๋ด๋„ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >>> 0b1111 15 >>> 0b1100 12 >>> >>> 15 & 12 12 >>> bin(12) '0b1100' ๊ทธ๋„ ๊ทธ๋Ÿด ๊ฒƒ์ด, ๊ฒ‰์œผ๋กœ ๋ณด๊ธฐ์— ์ด์ง„์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ด๋“  ์‹ญ์ง„์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ด๋“ , ์ปดํ“จํ„ฐ๋Š” ์ด์ง„์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ๊ณ„์‚ฐํ•˜๊ณ  ์žˆ์„ ํ…Œ๋‹ˆ๊นŒ์š”. A.4 ํŒŒ์ด์ฌ์œผ๋กœ PDF ํŒŒ์ผ ํ•ฉ์น˜๊ธฐ PDF ๋ฌธ์„œ๋ฅผ ํƒœ๋ธ”๋ฆฟ์œผ๋กœ ์ฝ์„ ๋•Œ๊ฐ€ ๋งŽ์€๋ฐ, PDF ํŒŒ์ผ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์ข…์ข… ์žˆ๋”๋ผ๊ณ ์š”. ํŒŒ์ด์ฌ์„ ์ด์šฉํ•ด ๊ฐ„๋‹จํžˆ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. PyPDF2 ์„ค์น˜ ์ด ์žฅ์—์„œ ์†Œ๊ฐœํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด PyPDF2 ํŒจํ‚ค์ง€๋ผ๋Š” ํŒจํ‚ค์ง€๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ๋‚˜ ๋ฆฌ๋ˆ…์Šค ์…ธ์—์„œ pip install PyPDF2 ๋ช…๋ น์œผ๋กœ ์„ค์น˜ํ•ด ์ฃผ์„ธ์š”. PDF ํŒŒ์ผ ์ค€๋น„ ์˜ˆ์ œ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด ๋ณ‘ํ•ฉํ•  ๋Œ€์ƒ์ด ๋˜๋Š” PDF ํŒŒ์ผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ–๊ณ  ๊ณ„์‹  PDF ํŒŒ์ผ์„ ๋ณต์‚ฌํ•ด์„œ mybook_01.pdf, mybook_02.pdf, mybook_03.pdf, mybook_04.pdf๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ค€๋น„ํ•ด ์ฃผ์„ธ์š”. ์ฒซ ๋ฒˆ์งธ ๋ฒ„์ „ ๊ทธ๋Ÿผ ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ๋ฒ„์ „์˜ ์ฝ”๋“œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆด๊ฒŒ์š”. from PyPDF2 import PdfFileMerger pdfs = ['mybook_01.pdf', 'mybook_02.pdf', 'mybook_03.pdf', 'mybook_04.pdf'] merger = PdfFileMerger() for pdf in pdfs: merger.append(pdf) merger.write("mybook.pdf") merger.close() ์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํŒŒ์ด์ฌ์ด ์‹คํ–‰๋˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์žˆ๋Š” ๋„ค ๊ฐœ์˜ PDF ํŒŒ์ผ mybook_01.pdf, mybook_02.pdf, mybook_03.pdf, mybook_04.pdf๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ํ•ฉ์ณ์ง„ mybook.pdf๋ผ๋Š” ํŒŒ์ผ์ด ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฒ„์ „ ๊ทธ๋Ÿฐ๋ฐ ํ•ฉ์น˜๊ณ  ์‹ถ์€ ํŒŒ์ผ์ด ๊ผญ ๋„ค ๊ฐœ๋งŒ ์žˆ์œผ๋ผ๋Š” ๋ฒ•์€ ์—†์ฃ . ๊ทธ๋ž˜์„œ ์•ž์—์„œ ๋ฐฐ์šด glob ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด์„œ ํŒŒ์ผ๋ช…์ด mybook์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  PDF ํŒŒ์ผ์„ ํ•ฉ์น˜๋„๋ก ๊ฐœ์„ ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. from glob import glob from PyPDF2 import PdfFileMerger BOOK = 'mybook' merger = PdfFileMerger() for f in glob('*' + BOOK + '*.pdf'): merger.append(f) merger.write(BOOK + ".pdf") merger.close() ์ด์ œ ์ฒ˜์Œ๋ณด๋‹ค๋Š” ์ข€ ๋” ์“ธ๋งŒํ•œ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ณ„์† ์“ฐ๋‹ค ๋ณด๋‹ˆ ๋ช‡ ๊ฐ€์ง€ ๋ถˆํŽธํ•œ ์ ์ด ๋” ์žˆ๋”๋ผ๊ณ ์š”. ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์žˆ๋Š” ํด๋”์™€ ๊ฐ™์€ ๊ณณ์— ์žˆ๋Š” ํŒŒ์ผ๋งŒ ํ•ฉ์น  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํŒŒ์ผ๋“ค์„ ํ•ฉ์น˜๋ ค๋ฉด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ณต์‚ฌํ•ด์•ผ ํ•˜๊ณ , ํŒŒ์ผ๋ช…๋„ ์Šคํฌ๋ฆฝํŠธ์— ๋“ค์–ด์žˆ๋‹ค ๋ณด๋‹ˆ ๊ทธ๋•Œ๊ทธ๋•Œ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณ‘ํ•ฉ๋œ ํŒŒ์ผ์˜ ์ด๋ฆ„์„ ์›๋ณธ๊ณผ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์ง€์„ ๊ฒฝ์šฐ, ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋‘ ๋ฒˆ ์ด์ƒ ์‹คํ–‰ํ•˜๋ฉด ์ฒ˜์Œ ์‹คํ–‰ํ•  ๋•Œ ๋ณ‘ํ•ฉ๋œ ํŒŒ์ผ์ด ๋‘ ๋ฒˆ์งธ ์‹คํ–‰ ๋•Œ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด๊ฐ€๋Š” ๋ฌธ์ œ์ ๋„ ์žˆ์—ˆ๊ณ ์š”. ์„ธ ๋ฒˆ์งธ ๋ฒ„์ „ ๊ทธ๋ž˜์„œ ์ด๋ฒˆ์—๋Š” ๋ณ‘ํ•ฉํ•  ๋Œ€์ƒ ํŒŒ์ผ์ด ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ์ฑ… ์ œ๋ชฉ์„ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์ธ์ž๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊ฟ”๋ดค์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ํ˜„์žฌ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ์ž…๋‹ˆ๋‹ค. import argparse from glob import glob import os from PyPDF2 import PdfFileMerger def main(book_title, directory=".", sub_dir='merged'): merger = PdfFileMerger() for f in glob(f"{directory}/{book_title}*.pdf"): merger.append(f) os.chdir(directory) if not os.path.isdir(sub_dir): os.mkdir(sub_dir) merger.write(f"{directory}/{sub_dir}/{bookname}.pdf") merger.close() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("-d", "--directory", help="directory where files to be merged live") parser.add_argument("bookname") args = parser.parse_args() directory = args.directory bookname = args.bookname main(args.bookname, args.directory) ์ธ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ๊ธฐ๋Šฅ์„ ์œ„ํ•ด ํŒŒ์ด์ฌ์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” argparse ๋ชจ๋“ˆ์„ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์Šคํฌ๋ฆฝํŠธ(pdf_merge.py)๋ฅผ ์•„๋ฌด ์ธ์ž ์—†์ด ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ๋ฒ•๊ณผ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. >python pdf_merge.py usage: pdf_merge.py [-h] [-d DIRECTORY] bookname pdf_merge.py: error: the following arguments are required: bookname ์ด ์Šคํฌ๋ฆฝํŠธ์˜ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. python pdf_merge.py -d <๋””๋ ‰ํ„ฐ๋ฆฌ> <์ฑ… ์ด๋ฆ„> ์‚ฌ์šฉ ์˜ˆ ์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•œ ์˜ˆ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆด๊ฒŒ์š”. ๋‹ค์Œ ๊ทธ๋ฆผ์˜ ์œˆ๋„ ํƒ์ƒ‰๊ธฐ์—๋Š” ํ•ฉ์น˜๊ณ ์ž ํ•˜๋Š” ์›๋ณธ PDF ํŒŒ์ผ๋“ค์ด ๋ณด์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์—์„œ pdf_merge.py๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์‹คํ–‰ ๊ฒฐ๊ณผ, ์›๋ณธ PDF ํŒŒ์ผ๋“ค์„ ๋ณ‘ํ•ฉํ•œ ํŒŒ์ผ์ด merged ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. ๊ฐœ์„ ํ•  ์  ์ด ์Šคํฌ๋ฆฝํŠธ๋Š” ์ œ๊ฐ€ ํ˜ผ์ž ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ผ ๋„์›€๋ง๋„ ์นœ์ ˆํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋Šฅ๋„ ๋งŒ๋“ค๋‹ค ๋ง์•˜์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅํ•  ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ช…์€ main() ํ•จ์ˆ˜์˜ sub_dir ์ธ์ž์— merged๋ผ๊ณ  ์ง€์ •ํ–ˆ๋Š”๋ฐ, ์ด๊ฒƒ์„ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด argparse๋ฅผ ์ด์šฉํ•ด ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ main ํ•จ์ˆ˜์— ๋„˜๊ฒจ์ค„ ์ˆ˜๋„ ์žˆ๊ฒ ์ฃ . ์ฒ˜์Œ์— ๊ทธ๋ ‡๊ฒŒ ํ•  ์ƒ๊ฐ์ด์—ˆ๋Š”๋ฐ, ๋ณ„๋กœ ํ•„์š”ํ•˜์ง€ ์•Š์•„์„œ ์™„์„ฑํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ ๋ถˆํŽธํ•œ ์ ์ด ์žˆ์œผ๋ฉด ์ง์ ‘ ์ˆ˜์ •ํ•ด ๋ณด์„ธ์š”. A.5 matplotlib์œผ๋กœ ํ•˜ํŠธ ๊ทธ๋ฆฌ๊ธฐ matplotlib ํŒจํ‚ค์ง€ matplotlib์€ ํ”Œ๋กฏ(๊ทธ๋ž˜ํ”„)์„ ๊ทธ๋ฆด ๋•Œ ๋งŽ์ด ์“ฐ์ด๋Š” ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. matplotlib ํ”„๋กœ์ ํŠธ์˜ ํ™ˆํŽ˜์ด์ง€ ์ฃผ์†Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. matplotlib ํŽ˜์ด์ง€ http://sourceforge.net/projects/matplotlib/ ํŒŒ์ด์ฌ ์…ธ์—์„œ ๋‹ค์Œ ๋ช…๋ น์„ ์‹คํ–‰ํ•ด ๋ณด์„ธ์š”. from pylab import * ModuleNotFoundError: No module named 'pylab'์ด๋ผ๋Š” ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚˜์˜จ๋‹ค๋ฉด ํŒŒ์ด์ฌ ์…ธ์—์„œ ๋น ์ ธ๋‚˜์™€์„œ pip install matplotlib ๋ช…๋ น(์•„๋‚˜์ฝ˜๋‹ค ํ™˜๊ฒฝ์—์„œ๋Š” conda install matplotlib)์œผ๋กœ matplotlib์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ํ•˜ํŠธ ๊ทธ๋ฆฌ๊ธฐ ํ•˜ํŠธ๋ฅผ ๊ทธ๋ ค๋ณผ๊ฒŒ์š”~ 1 from pylab import * 2 x = linspace(-1.6, 1.6, 10000) 3 f = lambda x: (sqrt(cos(x)) * cos(200 * x) + sqrt(abs(x)) - 0.7) * \ 4 pow((4 - x * x), 0.01) 5 plot(x, list(map(f, x))) 6 show() ์‹คํ–‰ํ•˜๋ฉด ์˜ˆ์œ ํ•˜ํŠธ๊ฐ€ ๊ทธ๋ ค์ง‘๋‹ˆ๋‹ค. ์ฝ”๋“œ ์„ค๋ช… 2ํ–‰: linspace()๋Š” ์ฃผ์–ด์ง„ ์ˆซ์ž ๋ฒ”์œ„ ๋‚ด์—์„œ ๋™์ผํ•œ ๊ฐ„๊ฒฉ์œผ๋กœ ๋–จ์–ด์ง„ ์ˆซ์ž๋“ค์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. >>> import pylab >>> pylab.linspace(5, 7, 20) array([ 5. , 5.10526316, 5.21052632, 5.31578947, 5.42105263, 5.52631579, 5.63157895, 5.73684211, 5.84210526, 5.94736842, 6.05263158, 6.15789474, 6.26315789, 6.36842105, 6.47368421, 6.57894737, 6.68421053, 6.78947368, 6.89473684, 7. ]) 3~4ํ–‰: ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ f๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๊ธธ์–ด์„œ ์—ญ์Šฌ๋ž˜์‹œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‘ ์ค„์— ๋‚˜๋ˆ„์–ด ์ผ์Šต๋‹ˆ๋‹ค. lambda์™€ map์— ๋Œ€ํ•ด์„œ๋Š” 3.5. ๋žŒ๋‹ค์˜ ์„ค๋ช…์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. pow()๋Š” ์ œ๊ณฑ์„ ๊ตฌํ•˜๋Š” ๋‚ด์žฅ(built-in) ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. >>> pow(2, 3) sqrt()๋Š” ์ œ๊ณฑ๊ทผ์„, cos()๋Š” ์ฝ”์‚ฌ์ธ์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋กœ, pylab์—์„œ ์ž„ํฌํŠธ ํ–ˆ์Šต๋‹ˆ๋‹ค. 5ํ–‰: plot()์€ ํ”Œ๋กฏ์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ , 6ํ–‰: show()๋ฅผ ํ•˜๋ฉด ํ™”๋ฉด์— ๋ณด์ž…๋‹ˆ๋‹ค. matplotlib์˜ ์‚ฌ์šฉ๋ฒ•์„ ์ตํžˆ๋ฉด ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ํ”Œ๋กฏ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๊ณ , ์ œ๋ชฉ์ด๋‚˜ x์ถ•, y ์ถ•์˜ ๋ผ๋ฒจ์„ ๋ถ™์ด๋Š” ๊ฒƒ๋„ ์†์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค. Python ํ™œ์šฉ: ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์™€ ๋ฐ์ดํ„ฐ ๋ถ„์„ https://www.slideshare.net/sk8erchoi/anaconda-50854172 A.6 ์œˆ๋„ CMD์—์„œ ํŒŒ์ด์ฌ ํ™œ์šฉ ํŒ ์œˆ๋„ ์šด์˜์ฒด์ œ์˜ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ(CMD)์—์„œ ํŒŒ์ด์ฌ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ํ™œ์šฉํ•˜๋Š” ํŒ์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ๋Ÿฐ์ฒ˜(Python Launcher) ๋ช…๋ นํ–‰์—์„œ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๊ฒฝ๋กœ์™€ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํŒŒ์ด์ฌ์ด C:\Users\ychoi\AppData\Local\Programs\Python\Python39\์— ์„ค์น˜๋๋‹ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณต์žกํ•œ ๋ช…๋ น์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค(๋งจ ์•ž์˜ >๋Š” ์œˆ๋„ ํ”„๋กฌํ”„ํŠธ ๋ฌธ์ž์—ด์„ ๋œปํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ž…๋ ฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค). > C:\Users\ychoi\AppData\Local\Programs\Python\Python39\python.exe hello.py ์œ„์˜ ๋ฐฉ๋ฒ•์€ ์•„๋ฌด๋ž˜๋„ ๋ฒˆ๊ฑฐ๋กœ์šด๋ฐ์š”, python.org์—์„œ ์œˆ๋„์šฉ ํŒŒ์ด์ฌ์„ ๋‹ค์šด๋กœ๋“œํ•ด ์„ค์น˜ํ–ˆ๋‹ค๋ฉด ํ•จ๊ป˜ ์„ค์น˜๋œ ํŒŒ์ด์ฌ ๋Ÿฐ์ฒ˜(Python Launcher)๋ฅผ ์ด์šฉํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„ํŽธํ•˜๊ฒŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. > py hello.py ๋˜๋Š” ๊ฐ„๋‹จํžˆ ์Šคํฌ๋ฆฝํŠธ ๊ฒฝ๋กœ๋งŒ ์ž…๋ ฅํ•ด ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ๊ณ ์š”. > hello.py ๋˜ํ•œ ํŒŒ์ด์ฌ ๋Ÿฐ์ฒ˜์—๋Š” ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ํ–‰์— ์ง€์ •๋œ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์ฐพ์•„ ์‹คํ–‰ํ•ด ์ฃผ๋Š” ๊ธฐ๋Šฅ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. #!/usr/bin/python3 ์Šคํฌ๋ฆฝํŠธ์˜ ์ฒซ ํ–‰์ด ์œ„์™€ ๊ฐ™์ด ๋˜์–ด ์žˆ๋‹ค๋ฉด, ์‹œ์Šคํ…œ์— ์„ค์น˜๋œ ํŒŒ์ด์ฌ 3 ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•ด ์ค๋‹ˆ๋‹ค. tip ์œˆ๋„ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์—์„œ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ์‹œ ์ธ์ž๊ฐ€ ์ œ๋Œ€๋กœ ์ „๋‹ฌ๋˜์ง€ ์•Š์„ ๋•Œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ์„ค์ • > ์•ฑ > ๊ธฐ๋ณธ ์•ฑ > ํŒŒ์ผ ์œ ํ˜•๋ณ„ ๊ธฐ๋ณธ๊ฐ’ ์„ ํƒ์—์„œ. py๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ. py ํŒŒ์ผ์„ ์—ด ๋•Œ ์‚ฌ์šฉํ•  ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜์„ธ์š” ์ฐฝ์—์„œ ์ด PC์—์„œ ๋‹ค๋ฅธ ์•ฑ ์ฐพ๊ธฐ๋ฅผ ์„ ํƒํ•˜๊ณ  C:\Windows\py.exe๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ํ•ด๊ฒฐ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์œˆ๋„ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Path ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋ช…๋ นํ–‰์—์„œ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ๋Š” ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ(ํด๋”)๋กœ ์ด๋™ํ•ด์„œ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜, ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ์ด๋™ํ•ด ์‹คํ–‰ํ•˜๋Š” ์˜ˆ(C:\GitHub\ychoi\utils์— txt_merge.py ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •): C:\Users\ychoi>cd ..\.. C:\>cd GitHub\ychoi\utils C:\GitHub\ychoi\utils>txt_merge.py ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด ์‹คํ–‰ํ•˜๋Š” ์˜ˆ: C:\Users\ychoi>\GitHub\ychoi\utils\txt_merge.py ์œ„์™€ ๊ฐ™์ด ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ฐพ์•„๊ฐ€๊ฑฐ๋‚˜ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ๋ฒˆ๊ฑฐ๋กญ๋‹ค๋ฉด, ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์žˆ๋Š” ๊ณณ์„ PATH ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ๋“ฑ๋กํ•ด๋‘๋ฉด ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ๋ช…๋งŒ์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์–ด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. + r ํ‚ค๋ฅผ ๋ˆ„๋ฅด๊ณ  sysdm.cpl ์ž…๋ ฅ ํ›„ ํ™•์ธ ๋ฒ„ํŠผ์„ ํด๋ฆญ ์‹œ์Šคํ…œ ์†์„ฑ ์ฐฝ์˜ ๊ณ ๊ธ‰ ํƒญ์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋ฒ„ํŠผ์„ ํด๋ฆญ ์‚ฌ์šฉ์ž ๋ณ€์ˆ˜์—์„œ Path๋ฅผ ์„ ํƒํ•˜๊ณ  ํŽธ์ง‘ ๋ฒ„ํŠผ์„ ํด๋ฆญ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ํŽธ์ง‘ ์ฐฝ์˜ ์ƒˆ๋กœ ๋งŒ๋“ค๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ณ  ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ํ™•์ธ ๋ฒ„ํŠผ์„ ํด๋ฆญ ์ด์ œ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ ์ฐฝ์„ ๋‹ซ๊ณ  ์ƒˆ๋กœ์šด ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ ์ฐฝ์„ ์—ด๋ฉด ์–ด๋Š ์œ„์น˜์—์„œ๋‚˜ (Path์— ๋“ฑ๋ก๋œ ๊ฒฝ๋กœ์˜) ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. C:\Users\ychoi>txt_merge.py PATHEXT ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ™•์žฅ์ž(.py) ์—†์ด ํ˜ธ์ถœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์‹œ์Šคํ…œ PATHEXT ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—. PY๋ฅผ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. (๊ธฐ์กด ํ™˜๊ฒฝ ๋ณ€์ˆ˜์˜ ๋งจ ๋’ค์— ;๋ฅผ ๋ถ™์ด๊ณ . PY๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.) ์ด์ œ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ ์ฐฝ์„ ๋‹ซ๊ณ  ์ƒˆ๋กœ์šด ์ฐฝ์„ ์—ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™•์žฅ์ž ์—†์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. >txt_merge A.7 ์ผํšŒ์šฉ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋ฐ”๊พธ๊ธฐ ์šฐ๋ฆฌ๋Š” ๊ท€์ฐฎ์€ ์ž‘์—…์„ ํŽธํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋ ค๊ณ , ๋˜๋Š” ์ˆ˜์ž‘์—…์„ ํ•˜๋‹ค๊ฐ€ ์‹ค์ˆ˜๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ ค๊ณ  ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์ฃ . ๊ทธ๋Ÿฐ๋ฐ ๊ฐ€๋”์€ ๋”ฑ ํ•œ ๋ฒˆ๋งŒ ์“ฐ๊ณ  ๋ง ์ƒ๊ฐ์œผ๋กœ, ๋˜๋Š” ๋‹จ ํ•œ ๊ฐ€์ง€ ์šฉ๋„๋กœ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๊ทธ ์˜ˆ์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ๋ช…: wikidocs_toc_chobopython.py 1 import urllib.request 2 from bs4 import BeautifulSoup 3 4 5 url = 'https://wikidocs.net/book/2' 6 with urllib.request.urlopen(url) as f: 7 html = f.read().decode('utf-8') 8 9 soup = BeautifulSoup(html, 'html.parser') 10 titles = soup.select('.list-group-item > span') 11 for title in titles[1:]: 12 s = title.select('span')[0].text.strip() 13 print(s) ์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์œ„ํ‚ค๋…์Šค์˜ ใ€Š์™•์ดˆ๋ณด๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌใ€‹(https://wikidocs.net/book/2) ๋ชฉ์ฐจ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. $ python3 wikidocs_toc_chobopython.py 0 ๋จธ๋ฆฌ๋ง 0.1 ์ฃผ์š” ๋ณ€๊ฒฝ ์ด๋ ฅ 1 ํŒŒ์ด์ฌ ์‹œ์ž‘ํ•˜๊ธฐ 1.1 ํŒŒ์ด์ฌ ๋ง›๋ณด๊ธฐ 1.2 ๋ณ€์ˆ˜ 1.2.1 ์—ฐ์Šต ๋ฌธ์ œ: ํŒŒ์ผ ํฌ๊ธฐ ๊ณ„์‚ฐ ... A.4 ํŒŒ์ด์ฌ์œผ๋กœ PDF ํŒŒ์ผ ํ•ฉ์น˜๊ธฐ A.5 matplotlib์œผ๋กœ ํ•˜ํŠธ ๊ทธ๋ฆฌ๊ธฐ A.6 ์œˆ๋„ CMD์—์„œ ํŒŒ์ด์ฌ ํ™œ์šฉ ํŒ Bye~ ์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋งŒ ํ•ด๋„ ์ด ์ฑ… ์™ธ์— ๋‹ค๋ฅธ ์ฑ…์˜ ๋ชฉ์ฐจ๋ฅผ ์ถœ๋ ฅํ•  ์ผ์ด ์—†์„ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ๋‚˜์ค‘์— ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์ƒ๊ฒผ๋‹ค๋ฉด ์–ด์ฉŒ๋ฉด ์ข‹์„๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด, ใ€Š SQLite3๋กœ ๊ฐ€๋ณ๊ฒŒ ๋ฐฐ์šฐ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šคใ€‹(https://wikidocs.net/book/1530)์˜ ๋ชฉ์ฐจ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด์š”? ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์ƒˆ๋กœ์šด ํŒŒ์ผ(์˜ˆ: wikidocs_toc_sqlite3.py)์„ ๋งŒ๋“  ๋’ค, ์ƒˆ๋กœ ๋งŒ๋“  ์Šคํฌ๋ฆฝํŠธ์˜ 5๋ฒˆ์งธ ์ค„์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด๊ฒ ์ฃ . url = 'https://wikidocs.net/book/1530' ์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. $ python3 wikidocs_toc_sqlite3.py A01 ๋„์ž… A02 ์‹ค์Šต ํ™˜๊ฒฝ ๊ฐ–์ถ”๊ธฐ A03 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ A04 ์ถ”๊ฐ€, ์‚ญ์ œ, ๊ฐฑ์‹ , ์กฐํšŒ (1) ... B03 ์‹ค์ „ ์˜ˆ์ œ - ์ „๊ธฐ์ฐจ ์ถฉ์ „์†Œ ๋ฐ์ดํ„ฐ ์ •์ œ B04 ์‹ค์ „ ์˜ˆ์ œ - ์ƒˆ์˜ GPS ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต(1) B05 ์‹ค์ „ ์˜ˆ์ œ - ์ƒˆ์˜ GPS ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต(2) ํ˜„์žฌ ์ฝ”๋“œ ํ˜„์žฌ ์ฝ”๋“œ๋Š” ๋‹ค์Œ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. wikidocs_toc_chobopython.py wikidocs_toc_sqlite3.py ์Šคํฌ๋ฆฝํŠธ์˜ ์‚ฌ์šฉ์„ฑ์„ ๊ฐœ์„ ํ•˜๋Š” ๊ณผ์ • ๊ทธ๋Ÿฐ๋ฐ ๋˜ ๋‹ค๋ฅธ ์ฑ…์˜ ๋ชฉ์ฐจ๋ฅผ ์•Œ๊ณ  ์‹ถ์–ด์ง€๋ฉด, ๊ทธ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ๊ธฐ๋Šฅ์ด ๊ฑฐ์˜ ๋น„์Šทํ•˜์ง€๋งŒ ๋˜‘๊ฐ™์ง€๋Š” ์•Š์€ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ํ•„์š”ํ•ด์ง€๋ฉด, ์ƒˆ๋กœ์šด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•ด์•ผ๋งŒ ํ• ๊นŒ์š”? ์ด๋Ÿฐ ์ผ์„ ๋‹ค์Œ์— ๋˜ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ํ•˜๋ฃจ์—๋„ ์—ฌ๋Ÿฌ ๋ฒˆ ํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ๊ทธ๋•Œ๋งˆ๋‹ค ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ˆ˜์ •ํ•ด์„œ ์ €์žฅํ•œ ๋‹ค์Œ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์กฐ์ฐจ ๊ท€์ฐฎ์€ ์ผ์ด ๋˜์–ด๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ๋‹จ ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ์ฒ˜๋ฆฌํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค, ์Šคํฌ๋ฆฝํŠธ ํ•˜๋‚˜๋กœ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ผ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ, ์ฆ‰ โ€˜์žฌ์‚ฌ์šฉโ€™์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ, ์ด์ „์— ๋งŒ๋“ค์–ด๋‘” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์†์งˆํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ ์Šคํฌ๋ฆฝ ํŠธ์„ ์–ด๋–ป๊ฒŒ ๋ฐ”๊ฟ” ๋‚˜๊ฐ€๋Š” ๊ฒƒ์ด ์ข‹์„์ง€๋ฅผ ๋‹จ๊ณ„๋ณ„๋กœ ์†Œ๊ฐœํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋‹จ๊ณ„๋ฅผ ๋๋‚ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํ•œ ๋‹จ๊ณ„๋ฅผ ๋งˆ์น  ๋•Œ๋งˆ๋‹ค ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉด์„œ๋„ ์ด์ „๋ณด๋‹ค๋Š” ์กฐ๊ธˆ์”ฉ ๋” ํŽธ๋ฆฌํ•ด์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์†Œ๊ฐœํ•˜๋Š” ์ˆœ์„œ๋‚˜ ๋ฐฉ๋ฒ•์ด ๋ฐ˜๋“œ์‹œ ์˜ณ๋‹ค๊ธฐ๋ณด๋‹ค๋Š”, ์ด๋Ÿฐ ์‹์œผ๋กœ๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜ˆ์‹œ๋กœ ์ดํ•ดํ•ด ์ฃผ์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ๋Œ€์ƒ์„ ์ธ์ž๋กœ ๋ฐ›๊ธฐ ๋ช…๋ น์„ ์ธ์ž๋กœ ๋ฐ›๊ธฐ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ๋„์›€๋ง ์ถ”๊ฐ€ A.7.1 ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ๋ชฉํ‘œ ํ„ฐ๋ฏธ๋„(๋˜๋Š” ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ)์— python3 wikidocs_toc_chobopython.py๋ฅผ ํƒ€์žํ•˜๋Š” ๋Œ€์‹  ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ๋ช…์ธ wikidocs_toc_chobopython.py๋งŒ ํƒ€์žํ•ด์„œ ์‹คํ–‰ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ด๋ฒˆ ๋‹จ๊ณ„์˜ ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ•œ ๋ฒˆ ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ํƒ€์žํ•  ๊ธ€์ž ์ˆ˜๋ฅผ 7~8 ๊ธ€์ž ์ค„์ด๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์ฃ . ์ ˆ์ฐจ ์Šคํฌ๋ฆฝํŠธ์— ์‹คํ–‰ ๊ถŒํ•œ(execute permission)์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ: $ cmod +x wikidocs_toc_chobopython.py ์ฒซ ๋ฒˆ์งธ ์ค„์— ๋‹ค์Œ ํ–‰์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์„ โ€˜์‹œ๋ฑ…(shebang)โ€™์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. #!/usr/bin/python3 ์ปค๋ฐ‹ ๋กœ๊ทธ d454ebe ํ˜„์žฌ ์ฝ”๋“œ wikidocs_toc_chobopython.py wikidocs_toc_sqlite3.py ์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ ์˜ˆ ์ด์ œ ํ„ฐ๋ฏธ๋„์—์„œ python ๋˜๋Š” python3๋ฅผ ์—†์ด, ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ๋ช…๋งŒ ํƒ€์žํ•ด์„œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. $ wikidocs_toc_chobopython.py 0. ๋จธ๋ฆฌ๋ง 0.1 ์ฃผ์š” ๋ณ€๊ฒฝ ์ด๋ ฅ 1. ํŒŒ์ด์ฌ ์‹œ์ž‘ํ•˜๊ธฐ 1.1 ํŒŒ์ด์ฌ ๋ง›๋ณด๊ธฐ ... $ wikidocs_toc_sqlite3.py A01 ๋„์ž… A02 ์‹ค์Šต ํ™˜๊ฒฝ ๊ฐ–์ถ”๊ธฐ A03 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ A04 ์ถ”๊ฐ€, ์‚ญ์ œ, ๊ฐฑ์‹ , ์กฐํšŒ (1) ... A.7.2 ๋Œ€์ƒ์„ ์ธ์ž๋กœ ๋ฐ›๊ธฐ ๋ชฉํ‘œ ์ง€๊ธˆ์€ ์ฑ…์˜ URL์ด ํ•˜๋“œ ์ฝ”๋”ฉ๋˜์–ด ์žˆ์–ด์„œ, ๋‹ค๋ฅธ ์ฑ…์˜ ๋ชฉ์ฐจ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๊ทธ๋•Œ๋งˆ๋‹ค ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์—ด์–ด์„œ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ๋ถˆํŽธ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์ฑ… ๋ฒˆํ˜ธ(์˜ˆ: ์™•์ดˆ๋ณด๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ์˜ URL https://wikidocs.net/book/2์—์„œ ์ฑ… ๋ฒˆํ˜ธ๋Š” 2)๋ฅผ ์ธ์ž๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•จ์œผ๋กœ์จ, ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งค๋ฒˆ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ ๋„ ๋‹ค๋ฅธ ์ฑ…์˜ ๋ชฉ์ฐจ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์ด๋ฒˆ ๋‹จ๊ณ„์˜ ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ์ ˆ์ฐจ ํŒŒ์ด์ฌ์—์„œ ๋ช…๋ นํ–‰์˜ ์ธ์ž๋ฅผ ์•Œ์•„๋‚ด๋Š” ๊ฐ€์žฅ ์›์ดˆ์ (?)์ธ ๋ฐฉ๋ฒ•์€ sys ๋ชจ๋“ˆ์˜ argv๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ์˜ ์•ž๋ถ€๋ถ„์— ๋‹ค์Œ ๋ฌธ์žฅ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. import sys ํ•˜๋“œ์ฝ”๋”ฉํ–ˆ๋˜ ๊ฒƒ์„ ์ธ์žฃ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›๋„๋ก ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค. url = 'https://wikidocs.net/book/' + sys.argv[1] ์ด์ œ ์ƒˆ๋กœ์šด ์ฑ…์˜ ๋ชฉ์ฐจ๋ฅผ ์–ป์„ ๋•Œ๋งˆ๋‹ค ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์ฑ…๋งˆ๋‹ค ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†์–ด์กŒ์Šต๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ ์ด๋ฆ„์€ wikidocs_toc_chobopython.py์—์„œ wikidocs_toc.py๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  wikidocs_toc_sqlite3.py๋Š” ์ด์ œ ํ•„์š” ์—†์œผ๋ฏ€๋กœ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ์ปค๋ฐ‹ ๋กœ๊ทธ 376573d ํ˜„์žฌ ์ฝ”๋“œ ๋‘ ๊ฐœ์˜€๋˜ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ํ•˜๋‚˜๋กœ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. wikidocs_toc.py ์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ ์˜ˆ $ wikidocs_toc.py 6038 ๊ฐ•์ขŒ ์†Œ๊ฐœ 0. ํƒ์ƒ‰ 0.1 ํƒ์ƒ‰ ๋ฌธ์ œ ์ •์˜ 0.2 ํƒ์ƒ‰ ๋ฌธ์ œ ํ’€๊ธฐ 0.3 ๋ฌด์ •๋ณด(uninformed) ํƒ์ƒ‰ - ๊นŠ์ด ์šฐ์„ (depth-first), ๋„ˆ๋น„ ์šฐ์„ (breadth-first) 0.3.1 DFS์™€ BFS์˜ ์žฅ๋‹จ์  ... A.7.3 ๋ช…๋ น์„ ์ธ์ž๋กœ ๋ฐ›๊ธฐ ์ฑ…์˜ ๋ชฉ์ฐจ๋ฅผ ์–ป์–ด์˜ค๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ž˜ ์“ฐ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ์ด๋ฒˆ์—๋Š” ๋ณธ๋ฌธ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ฌ ์ผ์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ๋ณธ๋ฌธ๋งŒ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ , ๊ธฐ์กด ์Šคํฌ๋ฆฝํŠธ์— ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ (1) ๋ช…๋ น์„ ์ธ์ž๋กœ ๋ฐ›๊ธฐ์™€ (2) ์ƒˆ๋กœ์šด ๋ช…๋ น์„ ์ถ”๊ฐ€ํ•˜๊ธฐ์˜ ๋‘ ๋‹จ๊ณ„๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋ชฉํ‘œ ๋ช…๋ น์„ ๋ฐ›๋Š” ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ ˆ์ฐจ ์ธ์ž: ์ฒซ ๋ฒˆ์งธ ์ธ์ž: ๋ช…๋ น(toc) ๋‘ ๋ฒˆ์งธ ์ธ์ž: ์ฑ… ๋ฒˆํ˜ธ ํŒŒ์ผ๋ช…์„ wikidocs_toc.py์—์„œ wikidocs.py๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. ์ปค๋ฐ‹ ๋กœ๊ทธ 8c72b45 ํ˜„์žฌ ์ฝ”๋“œ wikidocs.py ์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ ์˜ˆ $ wikidocs.py toc 2 0. ๋จธ๋ฆฌ๋ง 0.1 ์ฃผ์š” ๋ณ€๊ฒฝ ์ด๋ ฅ 1. ํŒŒ์ด์ฌ ์‹œ์ž‘ํ•˜๊ธฐ ... A.7.4 ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ๋ชฉํ‘œ ๋ณธ๋ฌธ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ ˆ์ฐจ ์ฑ… ๋‚ด์šฉ์„ ๋ฐ›์•„์˜ค๋Š” ๋ช…๋ น์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ž: ์ฒซ ๋ฒˆ์งธ ์ธ์ž: ๋ช…๋ น(toc ๋˜๋Š” content) ๋‘ ๋ฒˆ์งธ ์ธ์ž: ์ฑ… ๋ฒˆํ˜ธ ๋˜๋Š” ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ ์ปค๋ฐ‹ ๋กœ๊ทธ 2dff0a4 ํ˜„์žฌ ์ฝ”๋“œ wikidocs.py ์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ ์˜ˆ $ wikidocs.py content 43 ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์žฌ๋ฏธ์žˆ์Šต๋‹ˆ๋‹ค. ์ง„์งœ๋กœ ์žฌ๋ฏธ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์žฌ๋ฏธ๋ฅผ ์•Œ๋ ค๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ง์ ‘ ํ•ด๋ด์•ผ ํ•˜๋Š”๋ฐ, ์‚ฌ์‹ค ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ์ง€๊ฐ€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์—ฌ๋Ÿฌ๋ถ„๊ณผ ํ•จ๊ป˜ ๋ฐฐ์šฐ๊ธฐ ์‰ฌ์šด ํŒŒ์ด์ฌ์ด๋ผ๋Š” ์–ธ์–ด๋ฅผ ํ•จ๊ป˜ ๊ณต๋ถ€ํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ... A.7.5 ๋„์›€๋ง ์ถ”๊ฐ€ ์Šคํฌ๋ฆฝํŠธ์— ๋„์›€๋ง(help) ๊ธฐ๋Šฅ์ด ์žˆ์œผ๋ฉด ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ํ™œ์šฉํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“  ๋ณธ์ธ๋„ ์‹œ๊ฐ„์ด ํ๋ฅด๋ฉด ์‚ฌ์šฉ๋ฒ•์ด ์ž˜ ์ƒ๊ฐ๋‚˜์ง€ ์•Š์•„์„œ ์ฝ”๋“œ๋ฅผ ๋“ค์—ฌ๋‹ค๋ณด๊ฒŒ ๋˜๋‹ˆ, ์ž์‹ ์—๊ฒŒ๋„ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ๋ชฉํ‘œ ์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ๋ฒ•์„ ์•Œ๋ ค์ฃผ๋Š” help ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ ˆ์ฐจ ๋„์›€๋ง์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋„์›€๋ง์„ ์ถœ๋ ฅํ•˜๋Š” ๋ช…๋ น์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์—์„œ๋Š” sys.argv๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, argparse๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค. ์ปค๋ฐ‹ ๋กœ๊ทธ 4a53548 ํ˜„์žฌ ์ฝ”๋“œ wikidocs.py ์Šคํฌ๋ฆฝํŠธ ์‚ฌ์šฉ ์˜ˆ $ wikidocs.py Number of arguments is not matched Try "python wikidocs.py help". $ wikidocs.py help Usage: python wikidocs.py <command> [content id] Description: Available commands are toc, content and help. Content id should be a number. Examples: python wikidocs.py toc 2 python wikidocs.py content 43 Bye~ ๋๊นŒ์ง€ ์™„์ฃผํ•˜์‹  ์—ฌ๋Ÿฌ๋ถ„ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! ๊ทธ๋ฆฌ๊ณ  ์ฝ์–ด์ฃผ์…”์„œ ๊ณ ๋ง™์Šต๋‹ˆ๋‹ค. ๋Š๋ผ์‹  ์ ์„ ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์„ ์ข€ ๋” ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์ œ๊ฐ€ ๋ฒˆ์—ญํ•œ ใ€Š์‹ค์šฉ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐใ€‹๋„ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด์„ธ์š”.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: PyTorch๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋”ฅ ๋Ÿฌ๋‹ ์ž…๋ฌธ ### ๋ณธ๋ฌธ: ์ด ์ฑ…์€ ๋”ฅ ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ PyTorch๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋”ฅ ๋Ÿฌ๋‹์— ์ž…๋ฌธํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์€ ํŒŒ์ด์ฌ์€ ์–ด๋Š ์ •๋„ ํ•  ์ค„ ์•ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ด์ƒ์˜ ์‹ฌํ™” ๋‚ด์šฉ(ํŠธ๋žœ์Šคํฌ๋จธ์™€ ์–ธ์–ด ๋ชจ๋ธ)์„ ์›ํ•˜์‹œ๋Š” ๋ถ„์€ ์•„๋ž˜์˜ ์ฑ…์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋™์ผ ์ €์ž์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ํ•™์Šต ์ž๋ฃŒ ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ : https://wikidocs.net/book/2155 ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. 00. ์ฑ…๊ณผ ์ €์ž ์†Œ๊ฐœํ•˜๊ธฐ ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด ์ฑ…์€ 8๋…„ ์ฐจ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์—”์ง€๋‹ˆ์–ด๋“ค์ด ์ž…๋ฌธ์ž๋“ค์„ ์œ„ํ•ด ์ œ์ž‘ํ•˜๊ณ  ์žˆ๋Š” ๋”ฅ ๋Ÿฌ๋‹ ์ž…๋ฌธ ํ•™์Šต ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. ๋น„์ „๊ณต์ž๋ถ„๋“ค๋„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์‰ฝ๊ฒŒ ์ž…๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๊ธ‰์  ์งˆ๋ฌธ์— ๋‹ต๋ณ€๋“œ๋ฆฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค๋งŒ ์‹ค์ˆ˜๋กœ ๋†“์น˜๊ธฐ๋„ ํ•˜๊ณ , ์„ฑ์˜ ์—†๋Š” ์งˆ๋ฌธ, ์‚๋”ฑํ•œ ๋งํˆฌ ๋“ฑ์€ ๋‹ต๋ณ€๋“œ๋ฆฌ์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์ƒ์ด์‹  ๋ถ„๋“ค์˜ ๊ณผ์ œ, ๋…ผ๋ฌธ์„ ์œ„ํ•œ ์ฝ”๋“œ ๋Œ€ํ–‰์€ ํ•ด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ์žˆ์œผ๋‹ˆ ๋ฌธ์˜ํ•˜์ง€ ๋ง์•„์ฃผ์„ธ์š”. E-mail : <EMAIL> (์ €์ž ์ด๋ฉ”์ผ) <NAME>(๋Œ€๊ธฐ์—… NLP ์—”์ง€๋‹ˆ์–ด) - IT ๋Œ€๊ธฐ์—…์—์„œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์—”์ง€๋‹ˆ์–ด๋กœ ์žฌ์งํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. BERT, BART, T5์™€ ๊ฐ™์€ PLM, ๊ทธ๋ฆฌ๊ณ  LLM์„ ํŠœ๋‹ํ•˜๋ฉฐ ํ˜„์‹ค์˜ ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์— ๊ด€์‹ฌ์ด ๋งŽ์Šต๋‹ˆ๋‹ค. <NAME>(์ธ๊ณต์ง€๋Šฅ ์—”์ง€๋‹ˆ์–ด / ๊ฐ•์‚ฌ / ๊ฒธ์ž„ ๊ต์ˆ˜) : - ์‚ผ์„ฑ์—์„œ ์ธ๊ณต์ง€๋Šฅ ์—ฐ๊ตฌ์›์„ ์‹œ์ž‘์œผ๋กœ ์ปค๋ฆฌ์–ด๋ฅผ ์‹œ์ž‘ํ•˜์—ฌ ํ˜„์žฌ๋Š” AI ๊ฒธ์ž„ ๊ต์ˆ˜ ๋ฐ ์ปจ์„คํ„ดํŠธ๋กœ ํ™œ๋™ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฐ์„ธ๋Œ€ํ•™๊ต, ์ธ์ฒœ๋Œ€ํ•™๊ต, ์„œ์šธ์˜ˆ์ˆ ๋Œ€ํ•™๊ต, ์„œ์šธ๊ณผํ•™์ข…ํ•ฉ๋Œ€ํ•™์›, ๊ฒฝํฌ๋Œ€ํ•™๊ต, ์ดํ™”์—ฌ์ž๋Œ€ํ•™๊ต ๋“ฑ ๋‹ค์ˆ˜์˜ ํ•™๊ต์™€ ๊ธฐ์—…์—์„œ ์ธ๊ณต์ง€๋Šฅ ๊ฐ•์˜ ๋ฐ ๋ฉ˜ํ† ๋ง์„ ์ง„ํ–‰ํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณผ์™ธ ๋ฐ ๊ต์œก ๋ฌธ์˜๋Š” ์ด๋ฉ”์ผ๋กœ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋”ฅ ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ๋Š” Pytorch๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ตฌ๊ธ€ Colab์—์„œ ์‹ค์Šต์„ ํ•˜๋Š” ๊ฒƒ์„ ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ, ๊ธฐ๊ณ„, ๋ชจ๋ธ์„ ๊ฐ™์€ ์˜๋ฏธ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต(learning)๊ณผ ํ›ˆ๋ จ(training) ์ด ๊ฐ™์€ ์˜๋ฏธ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋น„์šฉ ํ•จ์ˆ˜(cost function), ์†์‹ค ํ•จ์ˆ˜(loss function)๋Š” ๊ฐ™์€ ์˜๋ฏธ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. References : ์œ„ํ‚ค๋…์Šค : ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ (๋™์ผ ์ €์ž) ํŒŒ์ด ํ† ์น˜ ๊ณต์‹ ํŠœํ† ๋ฆฌ์–ผ 01. [๊ธฐ์ดˆ โœ”] - ๋”ฅ ๋Ÿฌ๋‹์„ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์‹ค์Šต ํ™˜๊ฒฝ ๋ฐ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•„์š”ํ•œ ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•˜๋ฉฐ ๋”ฅ ๋Ÿฌ๋‹ ์‹ค์Šต์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. 01-01 ์ฝ”๋žฉ(Colab)๊ณผ ์•„๋‚˜์ฝ˜๋‹ค ๋จธ์‹  ๋Ÿฌ๋‹ ์‹ค์Šต์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งŽ์€ ํŒจํ‚ค์ง€๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ผ์ผ์ด ์„ค์น˜ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋“ค์„ ๋ชจ์•„๋†“์€ ํŒŒ์ด์ฌ ๋ฐฐํฌํŒ '์•„๋‚˜์ฝ˜๋‹ค'๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋Š” Numpy, Pandas, Jupyter Notebook, IPython, scikit-learn, matplotlib, seaborn, nltk ๋“ฑ ์ด ์ฑ…์—์„œ ์‚ฌ์šฉํ•  ๋Œ€๋ถ€๋ถ„์˜ ํŒจํ‚ค์ง€๋ฅผ ์ „๋ถ€ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์œˆ๋„ ํ™˜๊ฒฝ์„ ๊ธฐ์ค€์œผ๋กœ ๋‘๊ณ  ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ธํ„ฐ๋„ท์„ ํ†ตํ•ด ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ด์ฌ ์‹ค์Šต ํ™˜๊ฒฝ์ธ ๊ตฌ๊ธ€์˜ ์ฝ”๋žฉ(Colab)์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. GPU ์‹ค์Šต์„ ์œ„ํ•ด์„œ ์•„๋‚˜์ฝ˜๋‹ค์™€ ์ฝ”๋žฉ ๋‘˜ ์ค‘์—์„œ๋Š” ๊ฐ€๊ธ‰์ ์ด๋ฉด ์ฝ”๋žฉ์—์„œ ์‹ค์Šตํ•˜์‹œ๊ธฐ๋ฅผ ๊ถŒํ•ฉ๋‹ˆ๋‹ค. 1. ์•„๋‚˜์ฝ˜๋‹ค(Anaconda) ์„ค์น˜ ๋งํฌ : https://www.anaconda.com/distribution/ ์œ„ ์‚ฌ์ดํŠธ ๋งํฌ๋กœ ์ด๋™ํ•˜์—ฌ ์‚ฌ์ดํŠธ ํ•˜๋‹จ์œผ๋กœ ์ด๋™ํ•˜๋ฉด (์ €์ž๊ฐ€ ์ด ์ฑ…์„ ์ž‘์„ฑํ•  ๋‹น์‹œ ๊ธฐ์ค€) ์ขŒ์ธก์— ํŒŒ์ด์ฌ 3.7 ๋ฒ„์ „๊ณผ ์šฐ์ธก์— ํŒŒ์ด์ฌ 2.7 ๋ฒ„์ „์˜ ์•„๋‚˜์ฝ˜๋‹ค ์„ค์น˜ ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํŒŒ์ด์ฌ 3.7 ๋ฒ„์ „ 64 ๋น„ํŠธ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜ ํŒŒ์ผ์„ ์‹คํ–‰ํ•œ ํ›„์— ๋‹ค๋ฅธ ์œˆ๋„ ํ”„๋กœ๊ทธ๋žจ์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Next >๋ฅผ ๋ˆ„๋ฅด๋ฉด์„œ ์„ค์น˜๋ฅผ ์™„๋ฃŒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜๋ฉด ๋จธ์‹  ๋Ÿฌ๋‹์„ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋“ค์€ ์ž๋™์œผ๋กœ ์„ค์น˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ํ…์„œ ํ”Œ๋กœ, ์ผ€๋ผ์Šค, ์  ์‹ฌ, ์ฝ”์—”์—˜ํŒŒ์ด์™€ ๊ฐ™์€ ํŒจํ‚ค์ง€๋“ค์€ ๋ณ„๋„ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•œ๋ฐ ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ๋‹ค ์„ค์น˜ํ–ˆ๋‹ค๋ฉด ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ๋ฅผ ์˜คํ”ˆํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ๋ฅผ ์—ด์—ˆ๋‹ค๋ฉด ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ์— ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์•„๋‚˜์ฝ˜๋‹ค ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์ „๋ถ€ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. > conda update -n base conda > conda update --all ์ด ์ฑ…์ด ์ž‘์„ฑ๋˜์—ˆ์„ ๋‹น์‹œ์—๋Š” ํŒŒ์ด์ฌ 3.7 ๋ฒ„์ „์ด ์ตœ์‹  ๋ฒ„์ „์ด์—ˆ์ง€๋งŒ, ๋…์ž๋ถ„๋“ค์ด ํŒŒ์ด์ฌ์„ ์„ค์น˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•„๋‚˜์ฝ˜๋‹ค ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜์˜€์„ ๋•Œ๋Š” 3.7๋ณด๋‹ค ๋”์šฑ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋˜์—ˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋ฌด์ž‘์ • ํŒŒ์ด์ฌ ์ตœ์‹  ๋ฒ„์ „์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์•„๋ž˜์˜ ๋งํฌ์—์„œ ํŒŒ์ด์ฌ ๋ฒ„์ „๊ณผ ํ˜ธํ™˜๋˜๋Š” ํ…์„œ ํ”Œ๋กœ ๋ฒ„์ „์— ๋Œ€ํ•œ ์•ˆ๋‚ด๊ฐ€ ๋‚˜์™€์žˆ์œผ๋‹ˆ ๋ฐ˜๋“œ์‹œ ์„ค์น˜ ์ „ ํ™•์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://www.tensorflow.org/install/pip? hl=ko ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ํŽ˜์ด์ง€์—์„œ 'Python 3.9 ์ง€์›์—๋Š” Tensorflow 2.5 ์ด์ƒ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.'๋ผ๊ณ  ๊ธฐ์žฌ๋ผ ์žˆ๋‹ค๋ฉด, ํŒŒ์ด์ฌ 3.9๋ฅผ ์„ค์น˜ํ•˜์˜€์„ ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ Tensorflow๋Š” 2.5 ์ด์ƒ์„ ์„ค์น˜ํ•ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. 2. ๊ตฌ๊ธ€์˜ ์ฝ”๋žฉ(Colab) ํ…์„œ ํ”Œ๋กœ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 64๋น„ํŠธ ํ”Œ๋žซํผ๋งŒ์„ ์ง€์›ํ•˜๋ฏ€๋กœ 32๋น„ํŠธ ํ™˜๊ฒฝ์—์„œ๋Š” ๋”ฅ ๋Ÿฌ๋‹ ์‹ค์Šต ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•˜๊ธฐ์—๋Š” ๋งŽ์€ ์• ๋กœ ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ๊ฐœ์ธ์˜ ์ปดํ“จํ„ฐ ์‚ฌ์–‘์ด๋‚˜ ๋‹ค๋ฅธ ์ด์œ ๋กœ ์•„๋‚˜์ฝ˜๋‹ค๋‚˜ ์—ฌ๋Ÿฌ ํŒจํ‚ค์ง€ ์„ค์น˜๊ฐ€ ์–ด๋ ค์šด ๊ฒฝ์šฐ๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ์ธํ„ฐ๋„ท๋งŒ ๋œ๋‹ค๋ฉด ๋ฐ”๋กœ ํŒŒ์ด์ฌ์„ ์‹ค์Šตํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ๊ธ€์˜ ์ฝ”๋žฉ(Colab)์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ๊ธ€์˜ Colab์€ ๋’ค์—์„œ ์„ค๋ช…ํ•˜๊ฒŒ ๋  '์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ'๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•œ ์‹ค์Šต ํ™˜๊ฒฝ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Colab ์ฃผ์†Œ : https://colab.research.google.com/ ๊ตฌ๊ธ€์˜ Colab์— ์ ‘์†ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์œ„์˜ URL์„ ํ†ตํ•ด์„œ ์ ‘์†ํ•˜๊ฑฐ๋‚˜, ๊ตฌ๊ธ€(http://www.google.co.kr/)์—์„œ Colab์ด๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•ด์„œ ์ ‘์†ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1) ํŒŒ์ด์ฌ ์‹ค์Šตํ•˜๊ธฐ Colab ์‚ฌ์šฉ ์‹œ์—๋Š” ๊ตฌ๊ธ€ ๊ณ„์ •์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ๊ตฌ๊ธ€ ์•„์ด๋””๊ฐ€ ์—†์œผ์‹  ๋ถ„๋“ค์€ ๋จผ์ € ํšŒ์›๊ฐ€์ž… ํ›„ ๋กœ๊ทธ์ธ๋ถ€ํ„ฐ ํ•ด์ฃผ์„ธ์š”. ๋กœ๊ทธ์ธ ํ›„ ์ขŒ์ธก ์ƒ๋‹จ์—์„œ ํŒŒ์ผ > ์ƒˆ ๋…ธํŠธ๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ธˆ๋งŒ ๊ธฐ๋‹ค๋ฆฌ๋ฉด ํŒŒ์ด์ฌ์„ ์‹ค์Šตํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์Šต ํ™˜๊ฒฝ ์ฐฝ์ด ๋œจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด Colab์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ถ€๋ถ„์˜ ๋‹จ์œ„๋ฅผ '์…€'์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ ๋ณด์ด๋Š” ์ขŒ์ธก ์ƒ๋‹จ์˜ '+ ์ฝ”๋“œ' ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ƒˆ๋กœ์šด ์…€์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์…€์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  Shift + Enter ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์…€์— 3 + 5๋ผ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , Shift + Enter๋ฅผ ๋ˆ„๋ฅด๋ฉด 8์ด๋ผ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ขŒ์ธก์— [1]์€ ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ๋ช‡ ๋ฒˆ์งธ๋กœ ์‹คํ–‰๋˜์—ˆ๋Š”์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์…€์„ ์ถ”๊ฐ€ํ•ด ๋ณด๋ฉด์„œ ๋‹ค๋ฅธ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋„ ์ถ”๊ฐ€์ ์œผ๋กœ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”. 2) ๋ฌด๋ฃŒ๋กœ GPU ์‚ฌ์šฉํ•˜๊ธฐ ๋”ฅ ๋Ÿฌ๋‹์—์„œ๋Š” CPU๋ณด๋‹ค๋Š” GPU๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Colab์—์„œ ์‹ค์Šตํ•  ๋•Œ์˜ ์žฅ์ ์€ GPU๋ฅผ ๋ฌด๋ฃŒ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. GPU๊ฐ€ ์žฅ์ฐฉ๋œ ์ปดํ“จํ„ฐ๊ฐ€ ์—†๋Š” ๋”ฅ ๋Ÿฌ๋‹ ์ž…๋ฌธ์ž๋“ค์€ ํ–ฅํ›„ ์ด ์ฑ…์˜ ์‹ค์Šต์„ ์ง„ํ–‰ํ•  ๋•Œ Colab์—์„œ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋”ฅ ๋Ÿฌ๋‹์„ ๊ณต๋ถ€ํ•˜๋Š” ๊ฒƒ์„ ๊ฐ•ํ•˜๊ฒŒ ๊ถŒ์žฅ ๋“œ๋ฆฝ๋‹ˆ๋‹ค. GPU๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๋ฉด ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๋Š” ์‹œ๊ฐ„์ด ์ง€๋‚˜์น˜๊ฒŒ ์†Œ์š”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Colab์—์„œ GPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ƒˆ ๋…ธํŠธ์— ์ง„์ž…ํ–ˆ์„ ๋•Œ ์ƒ๋‹จ์—์„œ ๋Ÿฐํƒ€์ž„ > ๋Ÿฐํƒ€์ž„ ์œ ํ˜• ๋ณ€๊ฒฝ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ๋…ธํŠธ ์„ค์ •์˜ ํ•˜๋“œ์›จ์–ด ๊ฐ€์†๊ธฐ > GPU๋ฅผ ์„ ํƒ ํ›„ ์ €์žฅ์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค. ์ดํ›„ ์‹ค์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 3) ํŒŒ์ผ ์—…๋กœ๋“œ ๊ตฌ๊ธ€์˜ Colab์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋กœ๋“œํ•˜์—ฌ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋กœ ์‹ค์Šต์„ ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, ์ขŒ์ธก ์ƒ๋‹จ์—์„œ ํด๋” ๋ชจ์–‘์˜ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ ์œ„ ๋ฐฉํ–ฅ์˜ ํ™”์‚ดํ‘œ(โ†‘)๊ฐ€ ๊ทธ๋ ค์ง„ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ์ˆซ์ž 1๋ฒˆ ๋ฒ„ํŠผ๊ณผ ์ˆซ์ž 2๋ฒˆ ๋ฒ„ํŠผ์ด ๊ฐ๊ฐ ์ด์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด test.txt ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•œ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์—…๋กœ๋“œ ํ›„์—๋Š” ํŒŒ์ผ ๋ชฉ๋ก์— test.txt ํŒŒ์ผ์ด ๋ณด์ž…๋‹ˆ๋‹ค. 01-02 NLTK์™€ KoNLPy ์„ค์น˜ํ•˜๊ธฐ ์ฝ”๋žฉ์—์„œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜์‹œ๋Š” ๋ถ„๋“ค์€ ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ž์‹ ๋งŒ์˜ ํ™˜๊ฒฝ์—์„œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๋ ค๊ณ  ํ•˜์‹œ๋Š” ๋ถ„๋“ค์€ NLTK์™€ KoNLPy ์„ค์น˜์— ์–ด๋ ค์›€์„ ๊ฒช๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„์„œ ์ค€๋น„ํ•œ ์ฑ•ํ„ฐ์ž…๋‹ˆ๋‹ค. 1. NLTK์™€ NLTK Data ์„ค์น˜ ์—”์—˜ํ‹ฐ์ผ€์ด(NLTK)๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์˜€๋‹ค๋ฉด NLTK๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์น˜๊ฐ€ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ NLTK๋ฅผ ๋ณ„๋„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. > pip install nltk > ipython ... In [1]: import nltk In [2]: nltk.__version__ Out[2]: '3.4.5' NLTK์˜ ๊ธฐ๋Šฅ์„ ์ œ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” NLTK Data๋ผ๋Š” ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ํŒŒ์ด์ฌ ์ฝ”๋“œ ๋‚ด์—์„œ import nltk ์ดํ›„์— nltk.download()๋ผ๋Š” ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. In [3]: nltk.download() ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ ํ›„์— NLTK ์‹ค์Šต์— ํ•„์š”ํ•œ ๊ฐ์ข… ํŒจํ‚ค์ง€์™€ ์ฝ”ํผ์Šค๋ฅผ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ต์นญํ•˜์—ฌ NLTK Data๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, NLTK ์‹ค์Šต์„ ์ˆ˜ํ–‰ํ•˜๋˜ ๋„์ค‘์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ์•„๋ž˜์˜ 2๋ฒˆ๊ณผ 3๋ฒˆ ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. 2. NLTK Data๊ฐ€ ์„ค์น˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ NLTK๋Š” ๊ฐ ์‹ค์Šต๋งˆ๋‹ค ํ•„์š”ํ•œ NLTK Data๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น ์‹ค์Šต์— ํ•„์š”ํ•œ NLTK Data๊ฐ€ ์„ค์น˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ์ฝ”๋“œ ์‹คํ–‰ ์‹œ์— ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ๊ณ ๋ฌธ์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. LookupError: ********************************************************************** Resource treebank not found. Please use the NLTK Downloader to obtain the resource: >>> import nltk >>> nltk.download('treebank') ********************************************************************** ์œ„์˜ ๊ฒฝ์šฐ์—๋Š” NLTK Data ์ค‘์—์„œ 'treebank'๋ผ๋Š” ๋ฆฌ์†Œ์Šค๊ฐ€ ์„ค์น˜๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์œ„์˜ ์•ˆ๋‚ด์ฒ˜๋Ÿผ ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ ๋˜๋Š” iPython ์‰˜ ์•ˆ์—์„œ ๋™์ผํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. In [1]: import nltk In [2]: nltk.download('treebank') 3. NLTK Data ์„ค์น˜ ์‹œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ๋งํฌ : https://github.com/nltk/nltk_data ์„ค์น˜ ์‹œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด, ์ˆ˜๋™ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•˜์—ฌ ์ •ํ•ด์ง„ ๊ฒฝ๋กœ์— ์œ„์น˜์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜๋™ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋Š” nltk_data์˜ ๊นƒํ—ˆ๋ธŒ ์ฃผ์†Œ์™€ nltk_data ๊ณต์‹ ์‚ฌ์ดํŠธ ๋‘ ๊ณณ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  nltk_data์˜ ๊นƒํ—ˆ๋ธŒ ์‚ฌ์ดํŠธ์˜ ๋งํฌ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œ„ ๋งํฌ์˜ packages ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ํ•„์š”ํ•œ nltk_data ํŒŒ์ผ๋“ค์„ ๋ชจ๋‘ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ํ† ํฐํ™” ์ž‘์—…์„ ์œ„ํ•ด 'punkt' ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด, nltk_data/packages/tokenizer ๊ฒฝ๋กœ์—์„œ punkt.zip ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค์„ ๋‹ค์šด๋กœ๋“œํ•œ ํ›„, ๊ฐ O/S ๋ณ„ ์ •ํ•ด์ง„ ๊ฒฝ๋กœ์— ์œ„์น˜์‹œํ‚ต๋‹ˆ๋‹ค. ๊ฐ O/S ๋ณ„ ์ •ํ•ด์ง„ ๊ฒฝ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œˆ๋„ : C:/nltk_data ๋˜๋Š” D:/nltk_data UNIX : /usr/local/share/nltk_data/ ๋˜๋Š” /usr/share/nltk_data ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” packages ๋””๋ ‰ํ„ฐ๋ฆฌ ์ „์ฒด๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ๊ฒฝ๋กœ์— ์œ„์น˜์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : http://www.nltk.org/nltk_data/ ์ˆ˜๋™ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ๋กœ nltk_data ๊ณต์‹ ์‚ฌ์ดํŠธ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ† ํฐํ™” ์ž‘์—…์ด ํ•„์š”ํ•œ ์ƒํ™ฉ์ด๋ผ๊ณ  ๋‹ค์‹œ ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์œ„ ๋งํฌ๋กœ ์ด๋™ํ•ด CTRL + F๋ฅผ ๋ˆŒ๋Ÿฌ 'tokenizer'๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ , ๊ฒ€์ƒ‰์— ๋‚˜์˜ค๋Š” 106. Punkt Tokenizer Models [ download | source ] ํ•ด๋‹น ์ค„์„ ์ฐพ์€ ๋’ค, download ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด punkt.zip ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ํ›„ ์œ„์น˜์‹œ์ผœ์•ผ ํ•˜๋Š” ์•Œ๋งž์€ ๊ฒฝ๋กœ๋Š” ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๊ฒฝ๋กœ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. 4. KoNLPy ์„ค์น˜ ์ฝ”์—”์—˜ํŒŒ์ด(KoNLPy)๋Š” ํ•œ๊ตญ์–ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ํ”„๋กฌํ”„ํŠธ์—์„œ ์•„๋ž˜ ์ปค๋งจ๋“œ๋กœ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. > pip install konlpy > ipython ... In [1]: import konlpy In [2]: konlpy.__version__ Out[2]: '0.5.1' 5. ์œˆ๋„์—์„œ KoNLPy ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ์œˆ๋„์—์„œ KoNLPy๋ฅผ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ์‹คํ–‰ ์‹œ JDK ๊ด€๋ จ ์˜ค๋ฅ˜๋‚˜ JPype ์˜ค๋ฅ˜์— ๋ถ€๋”ชํžˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” KoNLPy๊ฐ€ JAVA๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ ์˜ค๋ฅ˜ ํ•ด๊ฒฐ์„ ์œ„ํ•ด์„œ๋Š” JDK 1.7 ์ด์ƒ์˜ ๋ฒ„์ „๊ณผ JPype๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1) JDK ์„ค์น˜ ์šฐ์„  JDK๋ฅผ 1.7 ๋ฒ„์ „ ์ด์ƒ์œผ๋กœ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜ ์ฃผ์†Œ : https://www.oracle.com/technetwork/java/javase/downloads/index.html ์„ค์น˜ํ•œ ํ›„์—๋Š” JDK๊ฐ€ ์„ค์น˜๋œ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ €์ž์˜ ๊ฒฝ์šฐ์—๋Š” jdk๊ฐ€ ์•„๋ž˜์˜ ๊ฒฝ๋กœ์— ์„ค์น˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฝ๋กœ : C:\Program Files\Java\jdk-11.0.1 11.0.1๊ณผ ๊ฐ™์ด ๋ฒ„์ „์— ๋Œ€ํ•œ ์ˆซ์ž๋Š” ์–ด๋–ค ๋ฒ„์ „์„ ์„ค์น˜ํ–ˆ๋Š๋ƒ์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) JDK ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์น˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•˜๋‹ค๋ฉด ํ•ด๋‹น ๊ฒฝ๋กœ๋ฅผ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฒฝ๋กœ๋ฅผ ์œˆ๋„ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์œˆ๋„ 10๊ธฐ์ค€) ์ œ์–ดํŒ > ์‹œ์Šคํ…œ ๋ฐ ๋ณด์•ˆ > ์‹œ์Šคํ…œ > ๊ณ ๊ธ‰ ์‹œ์Šคํ…œ ์„ค์ • > ๊ณ ๊ธ‰ > ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ƒˆ๋กœ ๋งŒ๋“ค๊ธฐ(N)...๋ฅผ ๋ˆ„๋ฅด๊ณ  JAVA_HOME์ด๋ผ๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ™˜๊ฒฝ ๋ณ€์ˆ˜์˜ ๊ฐ’์€ ์•ž์„œ ์ฐพ์•˜๋˜ jdk ์„ค์น˜ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค. ์ด์ œ KoNLPy๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ JDK ์„ค์ •์„ ๋งˆ์ณค์Šต๋‹ˆ๋‹ค. 3) JPype ์„ค์น˜ ์ด์ œ JAVA์™€ Python์„ ์—ฐ๊ฒฐํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” JPype๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜ ์ฃผ์†Œ : https://github.com/jpype-project/jpype/releases ํ•ด๋‹น ๋งํฌ์—์„œ Assets๋ผ๊ณ  ๊ธฐ์žฌ๋œ ๊ณณ์—์„œ ์ ์ ˆํ•œ ๋ฒ„์ „์„ ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š”๋ฐ cp27์€ ํŒŒ์ด์ฌ 2.7, cp36์€ ํŒŒ์ด์ฌ 3.6์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ €์ž๊ฐ€ ์ฑ…์„ ์ง‘ํ•„ํ•  ๋‹น์‹œ์—๋Š” ํŒŒ์ด์ฌ 3.6์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ์œผ๋ฏ€๋กœ cp36์ด๋ผ๊ณ  ์ ํžŒ JPype๋ฅผ ์„ค์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ ์‚ฌ์šฉํ•˜๋Š” ์œˆ๋„ O/S๊ฐ€ 32๋น„ํŠธ์ธ์ง€, 64๋น„ํŠธ์ธ์ง€์— ๋”ฐ๋ผ์„œ ์„ค์น˜ JPype๊ฐ€ ๋‹ค๋ฅธ๋ฐ, ์œˆ๋„ 32๋น„ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด win32๋ฅผ, ์œˆ๋„ 64๋น„ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด win_amd64๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํŒŒ์ด์ฌ 3.6, ์œˆ๋„ 64๋น„ํŠธ๋ฅผ ์‚ฌ์šฉ ์ค‘์ด๋ผ๋ฉด JPype1-0.6.3-cp36-cp36m-win_amd64.whl๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กฌํ”„ํŠธ์—์„œ ํ•ด๋‹น ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•˜์—ฌ ์•„๋ž˜ ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. > pip install JPype1-0.6.3-cp36-cp36m-win_amd64.whl ์ด์ œ JPype์˜ ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด, KoNLPy๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  : KoNLPy ์ˆ˜ํ–‰ ์‹œ ์ž๋ฐ” ์˜ค๋ฅ˜๋Š”, ํŒŒ์ด์ฌ bit์™€ ์ž๋ฐ” bit๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋„ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ํ™•์‹คํ•œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ์„ค์น˜๋œ ์ž๋ฐ”๋ฅผ ์ „๋ถ€<NAME>๊ณ  ์ตœ์‹  ๋ฒ„์ „(JRE, JDK)์œผ๋กœ ์ƒˆ๋กœ ๊น”๋ฉด ๋Œ€๋ถ€๋ถ„ ํ•ด๊ฒฐ๋ฉ๋‹ˆ๋‹ค. 01-03 ํŒ๋‹ค์Šค(Pandas) and ๋„˜ํŒŒ์ด(Numpy) and ๋งทํ”Œ๋กญ๋ฆฝ(Matplotlib) ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•œ ํ•„์ˆ˜ ํŒจํ‚ค์ง€ ์‚ผ๋Œ€์žฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ Pandas์™€ Numpy ๊ทธ๋ฆฌ๊ณ  Matplotlib์ž…๋‹ˆ๋‹ค. ์„ธ ๊ฐœ์˜ ํŒจํ‚ค์ง€ ๋ชจ๋‘ ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ–ˆ๋‹ค๋ฉด ์ถ”๊ฐ€ ์„ค์น˜ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„ธ ๊ฐœ์˜ ํŒจํ‚ค์ง€๋ฅผ ๊ฐ„๋‹จํžˆ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. 1. ํŒ๋‹ค์Šค(Pandas) ํŒ๋‹ค์Šค(Pandas)๋Š” ํŒŒ์ด์ฌ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์„ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ๋ถ„์„๊ณผ ๊ฐ™์€ ์ž‘์—…์—์„œ ํ•„์ˆ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š” Pandas ๋งํฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋งํฌ : http://pandas.pydata.org/pandas-docs/stable/ ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ Pandas๋ฅผ ๋ณ„๋„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pip install pandas > ipython ... In [1]: import pandas as pd In [2]: pd.__version__ Out[2]: '0.25.1' Pandas์˜ ๊ฒฝ์šฐ pd๋ผ๋Š” ๋ช…์นญ์œผ๋กœ ์ž„ํฌํŠธ ํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ก€์ž…๋‹ˆ๋‹ค. import pandas as pd Pandas๋Š” ์ด ์„ธ ๊ฐ€์ง€์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‹œ๋ฆฌ์ฆˆ(Series) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„(DataFrame) ํŒจ๋„(Panel) ์ด ์ค‘ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์ด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋ฉฐ ์—ฌ๊ธฐ์„œ๋Š” ์‹œ๋ฆฌ์ฆˆ์™€ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 1) ์‹œ๋ฆฌ์ฆˆ(Series) ์‹œ๋ฆฌ์ฆˆ ํด๋ž˜์Šค๋Š” 1์ฐจ์› ๋ฐฐ์—ด์˜ ๊ฐ’(values)์— ๊ฐ ๊ฐ’์— ๋Œ€์‘๋˜๋Š” ์ธ๋ฑ์Šค(index)๋ฅผ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. sr = pd.Series([17000, 18000, 1000, 5000], index=["ํ”ผ์ž", "์น˜ํ‚จ", "์ฝœ๋ผ", "๋งฅ์ฃผ"]) print('์‹œ๋ฆฌ์ฆˆ ์ถœ๋ ฅ :') print('-'*15) print(sr) ์‹œ๋ฆฌ์ฆˆ ์ถœ๋ ฅ : --------------- ํ”ผ์ž 17000 ์น˜ํ‚จ 18000 ์ฝœ๋ผ 1000 ๋งฅ์ฃผ 5000 dtype: int64 ๊ฐ’(values)๊ณผ ์ธ๋ฑ์Šค(index)๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. print('์‹œ๋ฆฌ์ฆˆ์˜ ๊ฐ’ : {}'.format(sr.values)) print('์‹œ๋ฆฌ์ฆˆ์˜ ์ธ๋ฑ์Šค : {}'.format(sr.index)) ์‹œ๋ฆฌ์ฆˆ์˜ ๊ฐ’ : [17000 18000 1000 5000] ์‹œ๋ฆฌ์ฆˆ์˜ ์ธ๋ฑ์Šค : Index(['ํ”ผ์ž', '์น˜ํ‚จ', '์ฝœ๋ผ', '๋งฅ์ฃผ'], dtype='object') 2) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„(DataFrame) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์€ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. 2์ฐจ์›์ด๋ฏ€๋กœ ํ–‰๋ฐฉํ–ฅ ์ธ๋ฑ์Šค(index)์™€ ์—ด ๋ฐฉํ–ฅ ์ธ๋ฑ์Šค(column)๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ–‰๊ณผ ์—ด์„ ๊ฐ€์ง€๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์‹œ๋ฆฌ์ฆˆ๊ฐ€ ์ธ๋ฑ์Šค(index)์™€ ๊ฐ’(values)์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค๋ฉด, ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์€ ์—ด(columns)๊นŒ์ง€ ์ถ”๊ฐ€๋˜์–ด ์—ด(columns), ์ธ๋ฑ์Šค(index), ๊ฐ’(values)์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด ์„ธ ๊ฐœ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ƒ์„ฑํ•ด ๋ด…์‹œ๋‹ค. values = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] index = ['one', 'two', 'three'] columns = ['A', 'B', 'C'] df = pd.DataFrame(values, index=index, columns=columns) print('๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ถœ๋ ฅ :') print('-'*18) print(df) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ถœ๋ ฅ : ------------------ A B C one 1 2 3 two 4 5 6 three 7 8 9 ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ๋ถ€ํ„ฐ ์ธ๋ฑ์Šค(index), ๊ฐ’(values), ์—ด(columns)์„ ๊ฐ๊ฐ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์ธ๋ฑ์Šค : {}'.format(df.index)) print('๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์—ด์ด๋ฆ„: {}'.format(df.columns)) print('๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ๊ฐ’ :') print('-'*18) print(df.values) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์ธ๋ฑ์Šค : Index(['one', 'two', 'three'], dtype='object') ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์—ด์ด๋ฆ„: Index(['A', 'B', 'C'], dtype='object') ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ๊ฐ’ : ------------------ [[1 2 3] [4 5 6] [7 8 9]] 3) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์ƒ์„ฑ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์€ ๋ฆฌ์ŠคํŠธ(List), ์‹œ๋ฆฌ์ฆˆ(Series), ๋”•์…”๋„ˆ๋ฆฌ(dict), Numpy์˜ ndarrays, ๋˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฆฌ์ŠคํŠธ์™€ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ด์ค‘ ๋ฆฌ์ŠคํŠธ๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. # ๋ฆฌ์ŠคํŠธ๋กœ ์ƒ์„ฑํ•˜๊ธฐ data = [ ['1000', 'Steve', 90.72], ['1001', 'James', 78.09], ['1002', 'Doyeon', 98.43], ['1003', 'Jane', 64.19], ['1004', 'Pilwoong', 81.30], ['1005', 'Tony', 99.14], ] df = pd.DataFrame(data) print(df) 0 1 2 0 1000 Steve 90.72 1 1001 James 78.09 2 1002 Doyeon 98.43 3 1003 Jane 64.19 4 1004 Pilwoong 81.30 5 1005 Tony 99.14 ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์—ด(columns)์„ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ด์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ณ  ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. df = pd.DataFrame(data, columns=['ํ•™๋ฒˆ', '์ด๋ฆ„', '์ ์ˆ˜']) print(df) ํ•™๋ฒˆ ์ด๋ฆ„ ์ ์ˆ˜ 0 1000 Steve 90.72 1 1001 James 78.09 2 1002 Doyeon 98.43 3 1003 Jane 64.19 4 1004 Pilwoong 81.30 5 1005 Tony 99.14 ํŒŒ์ด์ฌ ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜์ธ ๋”•์…”๋„ˆ๋ฆฌ(dictionary)๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ƒ์„ฑํ•˜๊ธฐ data = { 'ํ•™๋ฒˆ' : ['1000', '1001', '1002', '1003', '1004', '1005'], '์ด๋ฆ„' : [ 'Steve', 'James', 'Doyeon', 'Jane', 'Pilwoong', 'Tony'], '์ ์ˆ˜': [90.72, 78.09, 98.43, 64.19, 81.30, 99.14] } df = pd.DataFrame(data) print(df) ํ•™๋ฒˆ ์ด๋ฆ„ ์ ์ˆ˜ 0 1000 Steve 90.72 1 1001 James 78.09 2 1002 Doyeon 98.43 3 1003 Jane 64.19 4 1004 Pilwoong 81.30 5 1005 Tony 99.14 4) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์กฐํšŒํ•˜๊ธฐ ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋Š” ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์—์„œ ์›ํ•˜๋Š” ๊ตฌ๊ฐ„๋งŒ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์–ด๋กœ์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. df.head(n) - ์•ž ๋ถ€๋ถ„์„ n ๊ฐœ๋งŒ ๋ณด๊ธฐ df.tail(n) - ๋’ท๋ถ€๋ถ„์„ n ๊ฐœ๋งŒ ๋ณด๊ธฐ df['์—ด ์ด๋ฆ„'] - ํ•ด๋‹น๋˜๋Š” ์—ด์„ ํ™•์ธ ์œ„์—์„œ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. # ์•ž ๋ถ€๋ถ„์„ 3๊ฐœ๋งŒ ๋ณด๊ธฐ print(df.head(3)) ํ•™๋ฒˆ ์ด๋ฆ„ ์ ์ˆ˜ 0 1000 Steve 90.72 1 1001 James 78.09 2 1002 Doyeon 98.43 # ๋’ท๋ถ€๋ถ„์„ 3๊ฐœ๋งŒ ๋ณด๊ธฐ print(df.tail(3)) ํ•™๋ฒˆ ์ด๋ฆ„ ์ ์ˆ˜ 3 1003 Jane 64.19 4 1004 Pilwoong 81.30 5 1005 Tony 99.14 # 'ํ•™๋ฒˆ'์— ํ•ด๋‹น๋˜๋Š” ์—ด์„ ๋ณด๊ธฐ print(df['ํ•™๋ฒˆ']) 0 1000 1 1001 2 1002 3 1003 4 1004 5 1005 Name: ํ•™๋ฒˆ, dtype: object 5) ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ Pandas๋Š” CSV, ํ…์ŠคํŠธ, Excel, SQL, HTML, JSON ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ์ฝ๊ณ  ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด csv ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ๋Š” pandas.read_csv()๋ฅผ ํ†ตํ•ด ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ example.csv ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. df = pd.read_csv('example.csv') print(df) student id name score 0 1000 Steve 90.72 1 1001 James 78.09 2 1002 Doyeon 98.43 3 1003 Jane 64.19 4 1004 Pilwoong 81.30 5 1005 Tony 99.14 ์ด ๊ฒฝ์šฐ ์ธ๋ฑ์Šค๊ฐ€ ์ž๋™์œผ๋กœ ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(df.index) RangeIndex(start=0, stop=6, step=1) 2. ๋„˜ํŒŒ์ด(Numpy) ๋„˜ํŒŒ์ด(Numpy)๋Š” ์ˆ˜์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. Numpy์˜ ํ•ต์‹ฌ์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๋‹ค์ฐจ์› ํ–‰๋ ฌ ์ž๋ฃŒ๊ตฌ์กฐ์ธ ndarray๋ฅผ ํ†ตํ•ด ๋ฒกํ„ฐ ๋ฐ ํ–‰๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ์„ ํ˜• ๋Œ€์ˆ˜ ๊ณ„์‚ฐ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Numpy๋Š” ํŽธ์˜์„ฑ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์†๋„ ๋ฉด์—์„œ๋„ ์ˆœ์ˆ˜ ํŒŒ์ด์ฌ์— ๋น„ํ•ด ์••๋„์ ์œผ๋กœ ๋น ๋ฅด๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ Numpy๋ฅผ ๋ณ„๋„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pip install numpy > ipython ... In [1]: import numpy as np In [2]: np.__version__ Out[2]: '1.16.5' Numpy์˜ ๊ฒฝ์šฐ np๋ผ๋Š” ๋ช…์นญ์œผ๋กœ ์ž„ํฌํŠธ ํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ก€์ž…๋‹ˆ๋‹ค. import numpy as np 1) np.array() Numpy์˜ ํ•ต์‹ฌ์€ ndarray์ž…๋‹ˆ๋‹ค. np.array()๋Š” ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ndarray๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘ ํ•˜๋‚˜์ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  1์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # 1์ฐจ์› ๋ฐฐ์—ด vec = np.array([1, 2, 3, 4, 5]) print(vec) [1 2 3 4 5] 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ array() ์•ˆ์— ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋งŒ ๋“ค์–ด๊ฐ€๋ฏ€๋กœ ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋„ฃ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # 2์ฐจ์› ๋ฐฐ์—ด mat = np.array([[10, 20, 30], [ 60, 70, 80]]) print(mat) [[10 20 30] [60 70 80]] ๋‘ ๋ฐฐ์—ด์˜ ํƒ€์ž…์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('vec์˜ ํƒ€์ž… :',type(vec)) print('mat์˜ ํƒ€์ž… :',type(mat)) vec์˜ ํƒ€์ž… : <class 'numpy.ndarray'> mat์˜ ํƒ€์ž… : <class 'numpy.ndarray'> ๋™์ผํ•˜๊ฒŒ ํƒ€์ž…์ด numpy.ndarray๋ผ๊ณ  ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Numpy ๋ฐฐ์—ด์—๋Š” ์ถ•์˜ ๊ฐœ์ˆ˜(ndim)์™€ ํฌ๊ธฐ(shape)๋ผ๋Š” ๊ฐœ๋…์ด ์กด์žฌํ•˜๋Š”๋ฐ, ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ์ •ํ™•ํžˆ ์ˆ™์ง€ํ•˜๋Š” ๊ฒƒ์€ ๋”ฅ ๋Ÿฌ๋‹์—์„œ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ถ•์˜ ๊ฐœ์ˆ˜์™€ ํฌ๊ธฐ๊ฐ€ ์–ด๋–ค ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ์—์„œ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ์„ค๋ช…ํ•  ๋•Œ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. print('vec์˜ ์ถ•์˜ ๊ฐœ์ˆ˜ :',vec.ndim) # ์ถ•์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ print('vec์˜ ํฌ๊ธฐ(shape) :',vec.shape) # ํฌ๊ธฐ ์ถœ๋ ฅ vec์˜ ์ถ•์˜ ๊ฐœ์ˆ˜ : 1 vec์˜ ํฌ๊ธฐ(shape) : (5, ) print('mat์˜ ์ถ•์˜ ๊ฐœ์ˆ˜ :',mat.ndim) # ์ถ•์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ print('mat์˜ ํฌ๊ธฐ(shape) :',mat.shape) # ํฌ๊ธฐ ์ถœ๋ ฅ mat์˜ ์ถ•์˜ ๊ฐœ์ˆ˜ : 2 mat์˜ ํฌ๊ธฐ(shape) : (2, 3) 2) ndarray์˜ ์ดˆ๊ธฐํ™” ์œ„์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ndarray๋ฅผ ์ƒ์„ฑํ–ˆ์ง€๋งŒ ndarray๋ฅผ ๋งŒ๋“œ๋Š” ๋‹ค์–‘ํ•œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•˜๋ฏ€๋กœ ํ•„์š”์— ๋”ฐ๋ผ์„œ ๋‹ค์–‘ํ•œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. np.zeros()๋Š” ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์›์†Œ์— 0์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋“  ๊ฐ’์ด 0์ธ 2x3 ๋ฐฐ์—ด ์ƒ์„ฑ. zero_mat = np.zeros((2,3)) print(zero_mat) [[0. 0. 0.] [0. 0. 0.]] np.ones()๋Š” ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์›์†Œ์— 1์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋“  ๊ฐ’์ด 1์ธ 2x3 ๋ฐฐ์—ด ์ƒ์„ฑ. one_mat = np.ones((2,3)) print(one_mat) [[1. 1. 1.] [1. 1. 1.]] np.full()์€ ๋ฐฐ์—ด์— ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ๊ฐ’์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋“  ๊ฐ’์ด ํŠน์ • ์ƒ์ˆ˜์ธ ๋ฐฐ์—ด ์ƒ์„ฑ. ์ด ๊ฒฝ์šฐ 7. same_value_mat = np.full((2,2), 7) print(same_value_mat) [[7 7] [7 7]] np.eye()๋Š” ๋Œ€๊ฐ์„ ์œผ๋กœ๋Š” 1์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” 0์ธ 2์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. # ๋Œ€๊ฐ์„  ๊ฐ’์ด 1์ด๊ณ  ๋‚˜๋จธ์ง€ ๊ฐ’์ด 0์ธ 2์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑ. eye_mat = np.eye(3) print(eye_mat) [[1. 0. 0.] [0. 1. 0.]] [0. 0. 1.]] np.random.random()์€ ์ž„์˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. # ์ž„์˜์˜ ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์ง„ ๋ฐฐ์—ด ์ƒ์„ฑ random_mat = np.random.random((2,2)) # ์ž„์˜์˜ ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์ง„ ๋ฐฐ์—ด ์ƒ์„ฑ print(random_mat) [[0.3111881 0.72996102] [0.65667734 0.40758328]] ์ด ์™ธ์—๋„ Numpy์—๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•˜๋ฏ€๋กœ ํ•„์š”ํ•œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3) np.arange() np.arange(n)์€ 0๋ถ€ํ„ฐ n-1๊นŒ์ง€์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. # 0๋ถ€ํ„ฐ 9๊นŒ์ง€ range_vec = np.arange(10) print(range_vec) [0 1 2 3 4 5 6 7 8 9] np.arange(i, j, k)๋Š” i๋ถ€ํ„ฐ j-1๊นŒ์ง€ k์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. # 1๋ถ€ํ„ฐ 9๊นŒ์ง€ +2์”ฉ ์ ์šฉ๋˜๋Š” ๋ฒ”์œ„ n = 2 range_n_step_vec = np.arange(1, 10, n) print(range_n_step_vec) [1 3 5 7 9] 4) np.reshape() np.reshape()์€ ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋Š” ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋ฐฐ์—ด์˜ ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. 0๋ถ€ํ„ฐ 29๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” arange(30)์„ ์ˆ˜ํ–‰ํ•œ ํ›„, ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 30๊ฐœ์ด๋ฏ€๋กœ 5ํ–‰ 6์—ด์˜ ํ–‰๋ ฌ๋กœ ๋ณ€๊ฒฝํ•ด ๋ด…์‹œ๋‹ค. reshape_mat = np.array(np.arange(30)).reshape((5,6)) print(reshape_mat) [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23] [24 25 26 27 28 29]] 5) Numpy ์Šฌ๋ผ์ด์‹ฑ ndarray๋ฅผ ํ†ตํ•ด ๋งŒ๋“  ๋‹ค์ฐจ์› ๋ฐฐ์—ด์€ ํŒŒ์ด์ฌ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์ธ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ์Šฌ๋ผ์ด์‹ฑ(slicing) ๊ธฐ๋Šฅ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์Šฌ๋ผ์ด์‹ฑ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ํ–‰์ด๋‚˜ ์—ด๋“ค์˜ ์›์†Œ๋“ค์„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. mat = np.array([[1, 2, 3], [4, 5, 6]]) print(mat) [[1 2 3] [4 5 6]] # ์ฒซ ๋ฒˆ์งธ ํ–‰ ์ถœ๋ ฅ slicing_mat = mat[0, :] print(slicing_mat) [1 2 3] # ๋‘ ๋ฒˆ์งธ ์—ด ์ถœ๋ ฅ slicing_mat = mat[:, 1] print(slicing_mat) [2 5] 6) Numpy ์ •์ˆ˜ ์ธ๋ฑ์‹ฑ(integer indexing) ์Šฌ๋ผ์ด์‹ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์—ฐ์†์ ์ด์ง€ ์•Š์€ ์›์†Œ๋กœ ๋ฐฐ์—ด์„ ๋งŒ๋“ค ๊ฒฝ์šฐ์—๋Š” ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ๋Š” ๋งŒ๋“ค ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ 2ํ–‰ 2์—ด์˜ ์›์†Œ์™€ 5ํ–‰ 5์—ด์˜ ์›์†Œ๋ฅผ ๋ฝ‘์•„์„œ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ์ธ๋ฑ์‹ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์‹ฑ์€ ์›ํ•˜๋Š” ์œ„์น˜์˜ ์›์†Œ๋“ค์„ ๋ฝ‘์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. mat = np.array([[1, 2], [4, 5], [7, 8]]) print(mat) [[1 2] [4 5] [7 8]] ํŠน์ • ์œ„์น˜์˜ ์›์†Œ๋งŒ์„ ๊ฐ€์ ธ์™€๋ด…์‹œ๋‹ค. # 1ํ–‰ 0์—ด์˜ ์›์†Œ # => 0๋ถ€ํ„ฐ ์นด์šดํŠธํ•˜๋ฏ€๋กœ ๋‘ ๋ฒˆ์งธ ํ–‰ ์ฒซ ๋ฒˆ์งธ ์—ด์˜ ์›์†Œ. print(mat[1, 0]) ํŠน์ • ์œ„์น˜์˜ ์›์†Œ ๋‘ ๊ฐœ๋ฅผ ๊ฐ€์ ธ์™€ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. # mat[[2ํ–‰, 1ํ–‰],[0์—ด, 1์—ด]] # ๊ฐ ํ–‰๊ณผ ์—ด์˜ ์Œ์„ ๋งค์นญํ•˜๋ฉด 2ํ–‰ 0์—ด, 1ํ–‰ 1์—ด์˜ ๋‘ ๊ฐœ์˜ ์›์†Œ. indexing_mat = mat[[2, 1],[0, 1]] print(indexing_mat) [7 5] 7) Numpy ์—ฐ์‚ฐ Numpy๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด ๊ฐ„ ์—ฐ์‚ฐ์„ ์†์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ง์…ˆ, ๋บ„์…ˆ, ๊ณฑ์…ˆ, ๋‚˜๋ˆ—์…ˆ์„ ์œ„ํ•ด์„œ๋Š” ์—ฐ์‚ฐ์ž +, -, *, /๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋˜๋Š” np.add(), np.subtract(), np.multiply(), np.divide()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. x = np.array([1,2,3]) y = np.array([4,5,6]) # result = np.add(x, y)์™€ ๋™์ผ. result = x + y print(result) [5 7 9] # result = np.subtract(x, y)์™€ ๋™์ผ. result = x - y print(result) [-3 -3 -3] # result = np.multiply(result, x)์™€ ๋™์ผ. result = result * x print(result) [-3 -6 -9] # result = np.divide(result, x)์™€ ๋™์ผ. result = result / x print(result) [-3. -3. -3.] ์œ„์—์„œ *๋ฅผ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•œ ๊ฒƒ์€ ์š”์†Œ๋ณ„ ๊ณฑ์ž…๋‹ˆ๋‹ค. Numpy์—์„œ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ๊ณฑ ๋˜๋Š” ํ–‰๋ ฌ ๊ณฑ์„ ์œ„ํ•ด์„œ๋Š” dot()์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. mat1 = np.array([[1,2],[3,4]]) mat2 = np.array([[5,6],[7,8]]) mat3 = np.dot(mat1, mat2) print(mat3) [[19 22] [43 50]] 3. ๋งทํ”Œ๋กฏ๋ฆฝ(Matplotlib) ๋งทํ”Œ๋กฏ๋ฆฝ(Matplotlib)์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจํŠธ(chart)๋‚˜ ํ”Œ๋กฏ(plot)์œผ๋กœ ์‹œ๊ฐํ™”ํ•˜๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถ„์„์—์„œ Matplotlib์€ ๋ฐ์ดํ„ฐ ๋ถ„์„ ์ด์ „์— ๋ฐ์ดํ„ฐ ์ดํ•ด๋ฅผ ์œ„ํ•œ ์‹œ๊ฐํ™”๋‚˜, ๋ฐ์ดํ„ฐ ๋ถ„์„ ํ›„์— ๊ฒฐ๊ณผ๋ฅผ ์‹œ๊ฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ Matplotlib๋ฅผ ๋ณ„๋„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. pip install matplotlib > ipython ... In [1]: import matplotlib as mpl In [2]: mpl.__version__ Out[2]: '2.2.3' Matplotlib์„ ๋‹ค ์„ค์น˜ํ•˜์˜€๋‹ค๋ฉด Matplotlib์˜ ์ฃผ์š” ๋ชจ๋“ˆ์ธ pyplot๋ฅผ ๊ด€๋ก€์ƒ plt๋ผ๋Š” ๋ช…์นญ์œผ๋กœ ์ž„ํฌํŠธ ํ•ด๋ด…์‹œ๋‹ค. import matplotlib.pyplot as plt 1) ๋ผ์ธ ํ”Œ๋กฏ ๊ทธ๋ฆฌ๊ธฐ plot()์€ ๋ผ์ธ ํ”Œ๋กฏ์„ ๊ทธ๋ฆฌ๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. plot()์— x์ถ•๊ณผ y ์ถ•์˜ ๊ฐ’์„ ๊ธฐ์žฌํ•˜๊ณ  ๊ทธ๋ฆผ์„ ํ‘œ์‹œํ•˜๋Š” show()๋ฅผ ํ†ตํ•ด์„œ ์‹œ๊ฐํ™”ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋ž˜ํ”„์—๋Š” title('์ œ๋ชฉ')์„ ์‚ฌ์šฉํ•˜์—ฌ ์ œ๋ชฉ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ๋ž˜ํ”„์— 'test'๋ผ๋Š” ์ œ๋ชฉ์„ ๋„ฃ์–ด๋ด…์‹œ๋‹ค. ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ๋Š” show()๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ž๋™์œผ๋กœ ๋ Œ๋”๋ง ๋˜๋ฏ€๋กœ ๊ทธ๋ž˜ํ”„๊ฐ€ ์‹œ๊ฐํ™”๊ฐ€ ๋˜์ง€๋งŒ ๋‹ค๋ฅธ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ๋ฅผ ๊ฐ€์ •ํ•˜์—ฌ show()๋ฅผ ์ฝ”๋“œ์— ์‚ฝ์ž…ํ•˜์˜€์Šต๋‹ˆ๋‹ค. plt.title('test') plt.plot([1,2,3,4],[2,4,8,6]) plt.show() 2) ์ถ• ๋ ˆ์ด๋ธ” ์‚ฝ์ž…ํ•˜๊ธฐ x์ถ•๊ณผ y ์ถ• ๊ฐ๊ฐ์— ์ถ•์ด๋ฆ„์„ ์‚ฝ์ž…ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด xlabel('๋„ฃ๊ณ  ์‹ถ์€ ์ถ•์ด๋ฆ„')๊ณผ ylabel('๋„ฃ๊ณ  ์‹ถ์€ ์ถ•์ด๋ฆ„')์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ž˜ํ”„์— hours์™€ score๋ผ๋Š” ์ถ•์ด๋ฆ„์„ ๊ฐ๊ฐ ์ถ”๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. plt.title('test') plt.plot([1,2,3,4],[2,4,8,6]) plt.xlabel('hours') plt.ylabel('score') plt.show() 3) ๋ผ์ธ ์ถ”๊ฐ€์™€ ๋ฒ”๋ก€ ์‚ฝ์ž…ํ•˜๊ธฐ ๋‹ค์ˆ˜์˜ plot()์„ ํ•˜๋‚˜์˜ ๊ทธ๋ž˜ํ”„์— ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ผ์ธ ํ”Œ๋กฏ์„ ๋™์‹œ์— ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๊ฐ ์„ ์ด ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ๋ฒ”๋ก€(legend)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. plt.title('students') plt.plot([1,2,3,4],[2,4,8,6]) plt.plot([1.5,2.5,3.5,4.5],[3,5,8,10]) # ๋ผ์ธ ์ƒˆ๋กœ ์ถ”๊ฐ€ plt.xlabel('hours') plt.ylabel('score') plt.legend(['A student', 'B student']) # ๋ฒ”๋ก€ ์‚ฝ์ž… plt.show() ์ข€ ๋” ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ์‹ค์Šต์€ ๋”ฅ ๋Ÿฌ๋‹ ์ฑ•ํ„ฐ์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ํ›‘์–ด๋ณด๊ธฐ ์‹ค์Šต์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 01-04 ๋จธ์‹  ๋Ÿฌ๋‹ ์›Œํฌํ”Œ๋กœ(Machine Learning Workflow) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์‚ฌ์ด์–ธ์Šค(Data Science) ๋˜๋Š” ๋จธ์‹  ๋Ÿฌ๋‹(Machine Learning) ๊ณผ์ •์—์„œ ๊ฑฐ์น˜๋Š” ์ „๋ฐ˜์ ์ธ ๊ณผ์ •์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ฑ…์˜ ์ œ๋ชฉ์€ ๋”ฅ ๋Ÿฌ๋‹(Deep Learning)์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์ด์ง€๋งŒ, ๋”ฅ ๋Ÿฌ๋‹ ๋˜ํ•œ ๋จธ์‹  ๋Ÿฌ๋‹์˜ ํ•œ ๊ฐˆ๋ž˜๋กœ ๋”ฅ ๋Ÿฌ๋‹ ์›Œํฌํ”Œ๋กœ ๋˜ํ•œ ๋จธ์‹  ๋Ÿฌ๋‹ ์›Œํฌํ”Œ๋กœ์šฐ๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. ๋จธ์‹  ๋Ÿฌ๋‹ ์›Œํฌํ”Œ๋กœ(Machine Learning Workflow) ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ๋จธ์‹  ๋Ÿฌ๋‹์„ ํ•˜๋Š” ๊ณผ์ •์„ ํฌ๊ฒŒ 6๊ฐ€์ง€๋กœ ๋‚˜๋ˆ„๋ฉด, ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1) ์ˆ˜์ง‘(Acquisition) ๋จธ์‹  ๋Ÿฌ๋‹์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ๊ณ„์— ํ•™์Šต์‹œ์ผœ์•ผ ํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ๊ฒฝ์šฐ, ์ž์—ฐ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ง๋ญ‰์น˜ ๋˜๋Š” ์ฝ”ํผ์Šค(corpus)๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ ์ฝ”ํผ์Šค์˜ ์˜๋ฏธ๋ฅผ ํ’€์ดํ•˜๋ฉด, ์กฐ์‚ฌ๋‚˜ ์—ฐ๊ตฌ ๋ชฉ์ ์— ์˜ํ•ด์„œ ํŠน์ • ๋„๋ฉ”์ธ์œผ๋กœ๋ถ€ํ„ฐ ์ˆ˜์ง‘๋œ ํ…์ŠคํŠธ ์ง‘ํ•ฉ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํŒŒ์ผ<NAME>์€ txt ํŒŒ์ผ, csv ํŒŒ์ผ, xml ํŒŒ์ผ ๋“ฑ ๋‹ค์–‘ํ•˜๋ฉฐ ๊ทธ ์ถœ์ฒ˜๋„ ์Œ์„ฑ ๋ฐ์ดํ„ฐ, ์›น ์ˆ˜์ง‘๊ธฐ๋ฅผ ํ†ตํ•ด ์ˆ˜์ง‘๋œ ๋ฐ์ดํ„ฐ, ์˜ํ™” ๋ฆฌ๋ทฐ ๋“ฑ ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. 2) ์ ๊ฒ€ ๋ฐ ํƒ์ƒ‰(Inspection and exploration) ๋ฐ์ดํ„ฐ๊ฐ€ ์ˆ˜์ง‘๋˜์—ˆ๋‹ค๋ฉด, ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์ ๊ฒ€ํ•˜๊ณ  ํƒ์ƒ‰ํ•˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ, ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ, ๋จธ์‹  ๋Ÿฌ๋‹ ์ ์šฉ์„ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ •์ œํ•ด์•ผ ํ•˜๋Š”์ง€ ๋“ฑ์„ ํŒŒ์•…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„๋ฅผ ํƒ์ƒ‰์  ๋ฐ์ดํ„ฐ ๋ถ„์„(Exploratory Data Analysis, EDA) ๋‹จ๊ณ„๋ผ๊ณ ๋„ ํ•˜๋Š”๋ฐ ์ด๋Š” ๋…๋ฆฝ ๋ณ€์ˆ˜, ์ข…์† ๋ณ€์ˆ˜, ๋ณ€์ˆ˜ ์œ ํ˜•, ๋ณ€์ˆ˜์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋“ฑ์„ ์ ๊ฒ€ํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ์˜ ํŠน์ง•๊ณผ ๋‚ด์žฌํ•˜๋Š” ๊ตฌ์กฐ์  ๊ด€๊ณ„๋ฅผ ์•Œ์•„๋‚ด๋Š” ๊ณผ์ •์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ ์‹œ๊ฐํ™”์™€ ๊ฐ„๋‹จํ•œ ํ†ต๊ณ„ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 3) ์ „์ฒ˜๋ฆฌ ๋ฐ ์ •์ œ(Preprocessing and Cleaning) ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํŒŒ์•…์ด ๋๋‚ฌ๋‹ค๋ฉด, ๋จธ์‹  ๋Ÿฌ๋‹ ์›Œํฌํ”Œ๋กœ์—์„œ ๊ฐ€์žฅ ๊นŒ๋‹ค๋กœ์šด ์ž‘์—… ์ค‘ ํ•˜๋‚˜์ธ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ด ๋‹จ๊ณ„๋Š” ๋งŽ์€ ๋‹จ๊ณ„๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๊ฐ€๋ น ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ผ๋ฉด ํ† ํฐํ™”, ์ •์ œ, ์ •๊ทœํ™”, ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ๋“ฑ์˜ ๋‹จ๊ณ„๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ๋น ๋ฅด๊ณ  ์ •ํ™•ํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ํˆด(์ด ์ฑ…์—์„œ๋Š” ํŒŒ์ด์ฌ)์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์ง€์‹์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ •๋ง ๊นŒ๋‹ค๋กœ์šด ์ „์ฒ˜๋ฆฌ์˜ ๊ฒฝ์šฐ์—๋Š” ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ ๋จธ์‹  ๋Ÿฌ๋‹์ด ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 4) ๋ชจ๋ธ๋ง ๋ฐ ํ›ˆ๋ จ(Modeling and Training) ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚ฌ๋‹ค๋ฉด, ๋จธ์‹  ๋Ÿฌ๋‹์— ๋Œ€ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋‹จ๊ณ„์ธ ๋ชจ๋ธ๋ง ๋‹จ๊ณ„์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ ์ ˆํ•œ ๋จธ์‹  ๋Ÿฌ๋‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ ํƒํ•˜์—ฌ ๋ชจ๋ธ๋ง์ด ๋๋‚ฌ๋‹ค๋ฉด, ์ „์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋จธ์‹  ๋Ÿฌ๋‹ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ๊ธฐ๊ณ„์—๊ฒŒ ํ•™์Šต(training) ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ›ˆ๋ จ์ด๋ผ๊ณ ๋„ ํ•˜๋Š”๋ฐ, ์ด ๋‘ ์šฉ์–ด๋ฅผ ํ˜ผ์šฉํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ•™์Šต์„ ๋งˆ์น˜๊ณ  ๋‚˜์„œ ํ›ˆ๋ จ์ด ์ œ๋Œ€๋กœ ๋˜์—ˆ๋‹ค๋ฉด ๊ทธ ํ›„์— ๊ธฐ๊ณ„๋Š” ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ํƒœ์Šคํฌ(task)์ธ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ, ์Œ์„ฑ ์ธ์‹, ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๋“ฑ์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๊ณ„์—๊ฒŒ ํ•™์Šต์‹œ์ผœ์„œ๋Š” ์•ˆ ๋œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋’ค์˜ ์‹ค์Šต์—์„œ ๋ณด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ ๋ฐ์ดํ„ฐ ์ค‘ ์ผ๋ถ€๋Š” ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ๋‚จ๊ฒจ๋‘๊ณ  ํ›ˆ๋ จ์šฉ ๋ฐ์ดํ„ฐ๋งŒ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ๋งŒ ๊ธฐ๊ณ„๊ฐ€ ํ•™์Šต์„ ํ•˜๊ณ  ๋‚˜์„œ, ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ํ˜„์žฌ ์„ฑ๋Šฅ์ด ์–ผ๋งˆ๋‚˜ ๋˜๋Š”์ง€๋ฅผ ์ธก์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ณผ ์ ํ•ฉ(overfitting) ์ƒํ™ฉ์„ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ตœ์„ ์€ ํ›ˆ๋ จ์šฉ, ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ๋‘ ๊ฐ€์ง€๋งŒ ๋‚˜๋ˆ„๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ํ›ˆ๋ จ์šฉ, ๊ฒ€์ฆ์šฉ, ํ…Œ์ŠคํŠธ์šฉ. ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋ ‡๊ฒŒ ์„ธ ๊ฐ€์ง€๋กœ ๋‚˜๋ˆ„๊ณ  ํ›ˆ๋ จ์šฉ ๋ฐ์ดํ„ฐ๋งŒ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฒ€์ฆ์šฉ๊ณผ ํ…Œ์ŠคํŠธ์šฉ์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ์š”? ์ˆ˜๋Šฅ ์‹œํ—˜์— ๋น„์œ ํ•˜์ž๋ฉด ํ›ˆ๋ จ์šฉ์€ ํ•™์Šต์ง€, ๊ฒ€์ฆ์šฉ์€ ๋ชจ์˜๊ณ ์‚ฌ, ํ…Œ์ŠคํŠธ์šฉ์€ ์ˆ˜๋Šฅ ์‹œํ—˜์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•™์Šต์ง€๋ฅผ ํ’€๊ณ  ์ˆ˜๋Šฅ ์‹œํ—˜์„ ๋ณผ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๋ชจ์˜๊ณ ์‚ฌ๋ฅผ ํ’€๋ฉฐ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด ๋ฌด์—‡์ธ์ง€ ๊ฒ€์ฆํ•˜๊ณ  ๋ณด์™„ํ•˜๋Š” ๋‹จ๊ณ„๋ฅผ ํ•˜๋‚˜ ๋” ๋†“๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ฒ ์ง€์š”. ์‚ฌ์‹ค ํ˜„์—…์˜ ๊ฒฝ์šฐ๋ผ๋ฉด ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋Š” ๊ฑฐ์˜ ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค. ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋Š” ํ˜„์žฌ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ. ์ฆ‰, ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ์šฉ ๋ฐ์ดํ„ฐ๋กœ ์–ผ๋งˆ๋‚˜ ์ œ๋Œ€๋กœ ํ•™์Šต์ด ๋˜์—ˆ๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์šฉ์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋ธ์˜ ์ตœ์ข… ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๋Š” ์ผ์— ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์ˆ˜์น˜ํ™”ํ•˜์—ฌ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด ์‹œํ—˜์— ๋น„์œ ํ•˜๋ฉด ์ฑ„์ ํ•˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์‹ค์Šต ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ํ›ˆ๋ จ์šฉ, ๊ฒ€์ฆ์šฉ, ํ…Œ์ŠคํŠธ์šฉ ์„ธ ๊ฐ€์ง€๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋•Œ๋กœ๋Š” ํ›ˆ๋ จ์šฉ, ํ…Œ์ŠคํŠธ์šฉ ๋‘ ๊ฐ€์ง€๋งŒ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์—…์—์„œ ์ตœ์„ ์€ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ ๋˜ํ•œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž„์„ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. 5) ํ‰๊ฐ€(Evaluation) ๋ฏธ๋ฆฌ ์–ธ๊ธ‰ํ•˜์˜€๋Š”๋ฐ, ๊ธฐ๊ณ„๊ฐ€ ๋‹ค ํ•™์Šต์ด ๋˜์—ˆ๋‹ค๋ฉด ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ๋กœ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์€ ๊ธฐ๊ณ„๊ฐ€ ์˜ˆ์ธกํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ์˜ ์‹ค์ œ ์ •๋‹ต๊ณผ ์–ผ๋งˆ๋‚˜ ๊ฐ€๊นŒ์šด์ง€๋ฅผ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค. 6) ๋ฐฐํฌ(Deployment) ํ‰๊ฐ€ ๋‹จ๊ณ„์—์„œ ๊ธฐ๊ณ„๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ํ›ˆ๋ จ์ด ๋œ ๊ฒƒ์œผ๋กœ ํŒ๋‹จ๋œ๋‹ค๋ฉด ์™„์„ฑ๋œ ๋ชจ๋ธ์ด ๋ฐฐํฌ๋˜๋Š” ๋‹จ๊ณ„๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์—ฌ๊ธฐ์„œ ์™„์„ฑ๋œ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ „์ฒด์ ์ธ ํ”ผ๋“œ๋ฐฑ์œผ๋กœ ์ธํ•ด ๋ชจ๋ธ์„ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์˜จ๋‹ค๋ฉด ์ˆ˜์ง‘ ๋‹จ๊ณ„๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 01-05 ๋ฐ์ดํ„ฐ์˜ ๋ถ„๋ฆฌ(Splitting Data) ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ณ  ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ๋ถ„๋ฆฌํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ์ง€๋„ ํ•™์Šต(Supervised Learning)์„ ๋‹ค๋ฃจ๋Š”๋ฐ, ์ด๋ฒˆ์—๋Š” ์ง€๋„ ํ•™์Šต์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌ ์ž‘์—…์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. import pandas as pd import numpy as np from sklearn.model_selection import train_test_split 1. ์ง€๋„ ํ•™์Šต(Supervised Learning) ์ง€๋„ ํ•™์Šต์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ๋ฌธ์ œ์ง€๋ฅผ ์—ฐ์ƒ์ผ€ ํ•ฉ๋‹ˆ๋‹ค. ์ง€๋„ ํ•™์Šต์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์ •๋‹ต์ด ๋ฌด์—‡์ธ์ง€ ๋งž์ถฐ ํ•˜๋Š” '๋ฌธ์ œ'์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ”์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” '์ •๋‹ต'์ด ์ ํ˜€์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ๋น„์œ ํ•˜๋ฉด, ๊ธฐ๊ณ„๋Š” ์ •๋‹ต์ด ์ ํ˜€์ ธ ์žˆ๋Š” ๋ฌธ์ œ์ง€๋ฅผ ๋ฌธ์ œ์™€ ์ •๋‹ต์„ ํ•จ๊ป˜ ๋ณด๋ฉด์„œ ์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ•˜๊ณ , ํ–ฅํ›„์— ์ •๋‹ต์ด ์—†๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ๋„ ์ •๋‹ต์„ ์ž˜ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ๊ณผ ํ•ด๋‹น ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ธ์ง€, ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€ ์ ํ˜€์žˆ๋Š” ๋ ˆ์ด๋ธ”๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์€<NAME>์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์•ฝ 20,000๊ฐœ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ๋‘ ๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋˜๋Š”๋ฐ, ๋ฐ”๋กœ ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์— ํ•ด๋‹น๋˜๋Š” ์ฒซ ๋ฒˆ์งธ ์—ด๊ณผ ํ•ด๋‹น ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๊ฐ€ ์ ํ˜€์žˆ๋Š” ์ •๋‹ต์— ํ•ด๋‹น๋˜๋Š” ๋‘ ๋ฒˆ์งธ ์—ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์ด ์ด 20,000๊ฐœ์˜ ํ–‰์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ํ…์ŠคํŠธ(๋ฉ”์ผ์˜ ๋‚ด์šฉ) ๋ ˆ์ด๋ธ”(์ŠคํŒธ ์—ฌ๋ถ€) ๋‹น์‹ ์—๊ฒŒ ๋“œ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰ ํ˜œํƒ! ... ์ŠคํŒธ ๋ฉ”์ผ ๋‚ด์ผ ๋ต ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธ ๋ถ€ํƒ... ์ •์ƒ ๋ฉ”์ผ ... ... (๊ด‘๊ณ ) ๋ฉ‹์žˆ์–ด์งˆ ์ˆ˜ ์žˆ๋Š”... ์ŠคํŒธ ๋ฉ”์ผ ๊ธฐ๊ณ„๋ฅผ ์ง€๋„ํ•˜๋Š” ์„ ์ƒ๋‹˜์˜ ์ž…์žฅ์ด ๋˜์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋ฅผ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ด 4๊ฐœ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ์šฐ์„  ๋ฉ”์ผ์˜ ๋‚ด์šฉ์ด ๋‹ด๊ธด ์ฒซ ๋ฒˆ์งธ ์—ด์„ X์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฉ”์ผ์ด ์ŠคํŒธ์ธ์ง€ ์ •์ƒ์ธ์ง€ ์ •๋‹ต์ด ์ ํ˜€์žˆ๋Š” ๋‘ ๋ฒˆ์งธ ์—ด์„ y์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ฌธ์ œ์ง€์— ํ•ด๋‹น๋˜๋Š” 20,000๊ฐœ์˜ X์™€ ์ •๋‹ต์ง€์— ํ•ด๋‹น๋˜๋Š” 20,000๊ฐœ์˜ y๊ฐ€ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ œ ์ด X์™€ y์— ๋Œ€ํ•ด์„œ ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋˜๋‹ค์‹œ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ฌธ์ œ์ง€๋ฅผ ๋‹ค ๊ณต๋ถ€ํ•˜๊ณ  ๋‚˜์„œ ์‹ค๋ ฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‹œํ—˜(test) ์šฉ์œผ๋กœ ์ผ๋ถ€๋กœ ์ผ๋ถ€ ๋ฌธ์ œ์™€ ํ•ด๋‹น ๋ฌธ์ œ์˜ ์ •๋‹ต์ง€๋ฅผ ๋ถ„๋ฆฌํ•ด๋†“๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 2,000๊ฐœ๋ฅผ ๋ถ„๋ฆฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋ถ„๋ฆฌ ์‹œ์—๋Š” ์—ฌ์ „ํžˆ X์™€ y์˜ ๋งคํ•‘ ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค X(๋ฌธ์ œ)์— ๋Œ€ํ•œ ์–ด๋–ค y(์ •๋‹ต)์ธ์ง€ ๋ฐ”๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํ•™์Šต์šฉ์— ํ•ด๋‹น๋˜๋Š” 18,000๊ฐœ์˜ X, y์˜ ์Œ๊ณผ ์‹œํ—˜์šฉ์— ํ•ด๋‹น๋˜๋Š” 2000๊ฐœ์˜ X, y์˜ ์Œ์ด ์ƒ๊น๋‹ˆ๋‹ค ์ด ์ฑ…์—์„œ๋Š” ์ด ์œ ํ˜•์˜ ๋ฐ์ดํ„ฐ๋“ค์—๊ฒŒ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ณ€์ˆ˜๋ช…์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. <ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ> X_train : ๋ฌธ์ œ์ง€ ๋ฐ์ดํ„ฐ y_train : ๋ฌธ์ œ์ง€์— ๋Œ€ํ•œ ์ •๋‹ต ๋ฐ์ดํ„ฐ. <ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ> X_test : ์‹œํ—˜์ง€ ๋ฐ์ดํ„ฐ. y_test : ์‹œํ—˜์ง€์— ๋Œ€ํ•œ ์ •๋‹ต ๋ฐ์ดํ„ฐ. ๊ธฐ๊ณ„๋Š” ์ด์ œ๋ถ€ํ„ฐ X_train๊ณผ y_train์— ๋Œ€ํ•ด์„œ ํ•™์Šต์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ํ•™์Šต ์ƒํƒœ์—์„œ๋Š” ์ •๋‹ต์ง€์ธ y_train์„ ๋ณผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 18,000๊ฐœ์˜ ๋ฌธ์ œ์ง€ X_train๊ณผ y_train์„ ํ•จ๊ป˜ ๋ณด๋ฉด์„œ ์–ด๋–ค ๋ฉ”์ผ ๋‚ด์šฉ์ผ ๋•Œ ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๋ฅผ ์—ด์‹ฌํžˆ ๊ทœ์น™์„ ๋„์ถœํ•ด๋‚˜๊ฐ€๋ฉด์„œ ์ •๋ฆฌํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต์„ ๋‹ค ํ•œ ๊ธฐ๊ณ„์—๊ฒŒ y_test๋Š” ๋ณด์—ฌ์ฃผ์ง€ ์•Š๊ณ , X_test์— ๋Œ€ํ•ด์„œ ์ •๋‹ต์„ ์˜ˆ์ธกํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ธฐ๊ณ„๊ฐ€ ์˜ˆ์ธกํ•œ ๋‹ต๊ณผ ์‹ค์ œ ์ •๋‹ต์ธ y_test๋ฅผ ๋น„๊ตํ•˜๋ฉด์„œ ๊ธฐ๊ณ„๊ฐ€ ์ •๋‹ต์„ ์–ผ๋งˆ๋‚˜ ๋งž์ท„๋Š”์ง€๋ฅผ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ˆ˜์น˜๊ฐ€ ๊ธฐ๊ณ„์˜ ์ •ํ™•๋„(Accuracy)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 2. X์™€ y ๋ถ„๋ฆฌํ•˜๊ธฐ 1) zip ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜๊ธฐ zip() ํ•จ์ˆ˜๋Š” ๋™์ผํ•œ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ์‹œํ€€์Šค ์ž๋ฃŒํ˜•์—์„œ ๊ฐ ์ˆœ์„œ์— ๋“ฑ์žฅํ•˜๋Š” ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ ๊ตฌ์„ฑ์—์„œ zip ํ•จ์ˆ˜๋Š” X์™€ y๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š”๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  zip ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. X, y = zip(['a', 1], ['b', 2], ['c', 3]) print('X ๋ฐ์ดํ„ฐ :',X) print('y ๋ฐ์ดํ„ฐ :',y) X ๋ฐ์ดํ„ฐ : ('a', 'b', 'c') y ๋ฐ์ดํ„ฐ : (1, 2, 3) ๊ฐ ๋ฐ์ดํ„ฐ์—์„œ ์ฒซ ๋ฒˆ์งธ๋กœ ๋“ฑ์žฅํ•œ ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์ด๊ณ , ๋‘ ๋ฒˆ์งธ๋กœ ๋“ฑ์žฅํ•œ ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์ธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๋ฆฌ์ŠคํŠธ์˜ ๋ฆฌ์ŠคํŠธ ๋˜๋Š” ํ–‰๋ ฌ ๋˜๋Š” ๋’ค์—์„œ ๋ฐฐ์šธ ๊ฐœ๋…์ธ 2D ํ…์„œ. sequences = [['a', 1], ['b', 2], ['c', 3]] X, y = zip(*sequences) print('X ๋ฐ์ดํ„ฐ :',X) print('y ๋ฐ์ดํ„ฐ :',y) X ๋ฐ์ดํ„ฐ : ('a', 'b', 'c') y ๋ฐ์ดํ„ฐ : (1, 2, 3) ๊ฐ ๋ฐ์ดํ„ฐ์—์„œ ์ฒซ ๋ฒˆ์งธ๋กœ ๋“ฑ์žฅํ•œ ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์ด๊ณ , ๋‘ ๋ฒˆ์งธ๋กœ ๋“ฑ์žฅํ•œ ์›์†Œ๋“ค๋ผ๋ฆฌ ๋ฌถ์ธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐ๊ฐ X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์ด์šฉํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜๊ธฐ values = [['๋‹น์‹ ์—๊ฒŒ ๋“œ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰ ํ˜œํƒ!', 1], ['๋‚ด์ผ ๋ต ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธ ๋ถ€ํƒ๋“œ...', 0], ['๋„์—ฐ ์”จ. ์ž˜ ์ง€๋‚ด์‹œ์ฃ ? ์˜ค๋žœ ๋งŒ์ž…...', 0], ['(๊ด‘๊ณ ) AI๋กœ ์ฃผ๊ฐ€๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋‹ค!', 1]] columns = ['๋ฉ”์ผ ๋ณธ๋ฌธ', '์ŠคํŒธ ๋ฉ”์ผ ์œ ๋ฌด'] df = pd.DataFrame(values, columns=columns) df ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์€ ์—ด์˜ ์ด๋ฆ„์œผ๋กœ ๊ฐ ์—ด์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด ์†์‰ฝ๊ฒŒ X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. X = df['๋ฉ”์ผ ๋ณธ๋ฌธ'] y = df['์ŠคํŒธ ๋ฉ”์ผ ์œ ๋ฌด'] X์™€ y ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('X ๋ฐ์ดํ„ฐ :',X.to_list()) print('y ๋ฐ์ดํ„ฐ :',y.to_list()) X ๋ฐ์ดํ„ฐ : ['๋‹น์‹ ์—๊ฒŒ ๋“œ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰ ํ˜œํƒ!', '๋‚ด์ผ ๋ต ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธ ๋ถ€ํƒ๋“œ...', '๋„์—ฐ ์”จ. ์ž˜ ์ง€๋‚ด์‹œ์ฃ ? ์˜ค๋žœ ๋งŒ์ž…...', '(๊ด‘๊ณ ) AI๋กœ ์ฃผ๊ฐ€๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋‹ค!'] y ๋ฐ์ดํ„ฐ : [1, 0, 0, 1] 3) Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ž„์˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์„œ Numpy์˜ ์Šฌ๋ผ์ด์‹ฑ(slicing)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. np_array = np.arange(0,16).reshape((4,4)) print('์ „์ฒด ๋ฐ์ดํ„ฐ :') print(np_array) ์ „์ฒด ๋ฐ์ดํ„ฐ : [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] ๋งˆ์ง€๋ง‰ ์—ด์„ ์ œ์™ธํ•˜๊ณ  X ๋ฐ์ดํ„ฐ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์—ด๋งŒ์„ y ๋ฐ์ดํ„ฐ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. X = np_array[:, :3] y = np_array[:,3] print('X ๋ฐ์ดํ„ฐ :') print(X) print('y ๋ฐ์ดํ„ฐ :',y) X ๋ฐ์ดํ„ฐ : [[ 0 1 2] [ 4 5 6] [ 8 9 10] [12 13 14]] y ๋ฐ์ดํ„ฐ : [ 3 7 11 15] 3. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ์ด๋ฏธ X์™€ y๊ฐ€ ๋ถ„๋ฆฌ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์‚ฌ์ดํ‚ท ๋Ÿฐ์„ ์ด์šฉํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜๊ธฐ ์‚ฌ์ดํ‚ท๋Ÿฐ์€ ํ•™์Šต์šฉ ํ…Œ์ŠคํŠธ์™€ ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” train_test_split()๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.2, random_state=1234) ๊ฐ ์ธ์ž๋Š” ๋‹ค์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. train_size์™€ test_size๋Š” ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ๊ธฐ์žฌํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค. X : ๋…๋ฆฝ ๋ณ€์ˆ˜ ๋ฐ์ดํ„ฐ. (๋ฐฐ์—ด์ด๋‚˜ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„) y : ์ข…์† ๋ณ€์ˆ˜ ๋ฐ์ดํ„ฐ. ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ. test_size : ํ…Œ์ŠคํŠธ์šฉ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค. 1๋ณด๋‹ค ์ž‘์€ ์‹ค์ˆ˜๋ฅผ ๊ธฐ์žฌํ•  ๊ฒฝ์šฐ, ๋น„์œจ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. train_size : ํ•™์Šต์šฉ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค. 1๋ณด๋‹ค ์ž‘์€ ์‹ค์ˆ˜๋ฅผ ๊ธฐ์žฌํ•  ๊ฒฝ์šฐ, ๋น„์œจ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. random_state : ๋‚œ์ˆ˜ ์‹œ๋“œ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž„์˜๋กœ X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. # ์ž„์˜๋กœ X์™€ y ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ X, y = np.arange(10).reshape((5, 2)), range(5) print('X ์ „์ฒด ๋ฐ์ดํ„ฐ :') print(X) print('y ์ „์ฒด ๋ฐ์ดํ„ฐ :') print(list(y)) X ์ „์ฒด ๋ฐ์ดํ„ฐ : [[0 1] [2 3] [4 5] [6 7] [8 9]] y ์ „์ฒด ๋ฐ์ดํ„ฐ : [0, 1, 2, 3, 4] ์—ฌ๊ธฐ์„œ๋Š” 7:3์˜ ๋น„์œจ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. train_test_split()์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๋ฅผ ์„ž๊ณ  ๋‚˜์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, random_state์˜ ๊ฐ’์„ ํŠน์ • ์ˆซ์ž๋กœ ๊ธฐ์žฌํ•ด ์ค€ ๋’ค์— ๋‹ค์Œ์—๋„ ๋™์ผํ•œ ์ˆซ์ž๋กœ ๊ธฐ์žฌํ•ด ์ฃผ๋ฉด ํ•ญ์ƒ ๋™์ผํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ์„ž์ธ ์ฑ„ ๋ถ„๋ฆฌ๋˜๋ฏ€๋กœ ์ด์ „๊ณผ ๋‹ค๋ฅธ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์‹ค์Šต์„ ํ†ตํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. random_state ๊ฐ’์„ ์ž„์˜๋กœ 1234๋กœ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. # 7:3์˜ ๋น„์œจ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1234) 70%์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌ๋œ X์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ 30%์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌ๋œ X์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. print('X ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ :') print(X_train) print('X ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(X_test) X ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ : [[2 3] [4 5] [6 7]] X ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [[8 9] [0 1]] 70%์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌ๋œ y์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ 30%์˜ ๋น„์œจ๋กœ ๋ถ„๋ฆฌ๋œ y์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. print('y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ :') print(y_train) print('y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(y_test) y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ : [1, 2, 3] y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [4, 0] ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋Š ์ค‘๊ฐ„ ๋ถ€๋ถ„์—์„œ ์•ž๊ณผ ๋’ค๋กœ ์ž๋ฅธ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์•ž์— ์žˆ๋˜ ์ƒ˜ํ”Œ์ด ๋’ค๋กœ ๊ฐ€๊ธฐ๋„ ํ•˜๊ณ , ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๊ฐ€ ์ „๋ฐ˜์ ์œผ๋กœ ์„ž์ด๋ฉด์„œ ๋ถ„๋ฆฌ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. random_state์˜ ์˜๋ฏธ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด๋ฒˆ์—๋Š” random_state์˜ ๊ฐ’์„ ์ž„์˜๋กœ ๋‹ค๋ฅธ ๊ฐ’์ธ 1์„ ์ฃผ๊ณ  ๋‹ค์‹œ ๋ถ„๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  y ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # random_state์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) print('y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ :') print(y_train) print('y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(y_test) y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ : [4, 0, 3] y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [2, 1] random_state ๊ฐ’์ด 1234์ผ ๋•Œ์™€ ์ „ํ˜€ ๋‹ค๋ฅธ y ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ์„ž์˜€๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋‹ค์‹œ random_state์˜ ๊ฐ’์„ 1234๋กœ ์ฃผ๊ณ  ๋‹ค์‹œ y ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # random_state์„ ์ด์ „์˜ ๊ฐ’์ด์—ˆ๋˜ 1234๋กœ ๋ณ€๊ฒฝ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1234) print('y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ :') print(y_train) print('y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(y_test) y ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ : [1, 2, 3] y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [4, 0] ์ด์ „๊ณผ ๋™์ผํ•œ y ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. random_state์˜ ๊ฐ’์„ ๊ณ ์ •ํ•ด๋‘๋ฉด ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ํ•ญ์ƒ ๋™์ผํ•œ ์ˆœ์„œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์„ž์œผ๋ฏ€๋กœ, ๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ์— ์žฌํ˜„ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ์ˆ˜๋™์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” ์ˆ˜๋™์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ์ž„์˜๋กœ X ๋ฐ์ดํ„ฐ์™€ y ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์‹ค์Šต์„ ์œ„ํ•ด ์ž„์˜๋กœ X์™€ y๊ฐ€ ์ด๋ฏธ ๋ถ„๋ฆฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ X, y = np.arange(0,24).reshape((12,2)), range(12) print('X ์ „์ฒด ๋ฐ์ดํ„ฐ :') print(X) print('y ์ „์ฒด ๋ฐ์ดํ„ฐ :') print(list(y)) X ์ „์ฒด ๋ฐ์ดํ„ฐ : [[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9] [10 11] [12 13] [14 15] [16 17] [18 19] [20 21] [22 23]] y ์ „์ฒด ๋ฐ์ดํ„ฐ : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. num_of_train์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, num_of_test๋Š” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. num_of_train = int(len(X) * 0.8) # ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๊ธธ์ด์˜ 80%์— ํ•ด๋‹นํ•˜๋Š” ๊ธธ์ด๊ฐ’์„ ๊ตฌํ•œ๋‹ค. num_of_test = int(len(X) - num_of_train) # ์ „์ฒด ๊ธธ์ด์—์„œ 80%์— ํ•ด๋‹นํ•˜๋Š” ๊ธธ์ด๋ฅผ ๋บ€๋‹ค. print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',num_of_train) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',num_of_test) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : 9 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : 3 ์•„์ง ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆˆ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ด ๋‘ ๊ฐœ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ช‡ ๊ฐœ๋กœ ํ• ์ง€ ์ •ํ•˜๊ธฐ๋งŒ ํ•œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ num_of_test๋ฅผ len(X) * 0.2๋กœ ๊ณ„์‚ฐํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์— ๋ˆ„๋ฝ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 4,518์ด๋ผ๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ 4,518์˜ 80%์˜ ๊ฐ’์€ 3,614.4๋กœ ์†Œ์ˆ˜์ ์„ ๋‚ด๋ฆฌ๋ฉด 3,614๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ 4,518์˜ 20%์˜ ๊ฐ’์€ 903.6์œผ๋กœ ์†Œ์ˆ˜์ ์„ ๋‚ด๋ฆฌ๋ฉด 903์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  3,614 + 903 = 4517์ด๋ฏ€๋กœ ๋ฐ์ดํ„ฐ 1๊ฐœ๊ฐ€ ๋ˆ„๋ฝ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์–ด๋Š ํ•œ ์ชฝ์„ ๋จผ์ € ๊ณ„์‚ฐํ•˜๊ณ  ๊ทธ ๊ฐ’๋งŒํผ ์ œ์™ธํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. X_test = X[num_of_train:] # ์ „์ฒด ๋ฐ์ดํ„ฐ ์ค‘์—์„œ 20%๋งŒํผ ๋’ค์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ y_test = y[num_of_train:] # ์ „์ฒด ๋ฐ์ดํ„ฐ ์ค‘์—์„œ 20%๋งŒํผ ๋’ค์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ X_train = X[:num_of_train] # ์ „์ฒด ๋ฐ์ดํ„ฐ ์ค‘์—์„œ 80%๋งŒํผ ์•ž์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ y_train = y[:num_of_train] # ์ „์ฒด ๋ฐ์ดํ„ฐ ์ค‘์—์„œ 80%๋งŒํผ ์•ž์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆŒ ๋•Œ๋Š” num_of_train์™€ ๊ฐ™์ด ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ์˜ ๋ˆ„๋ฝ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ ๊ตฌํ•œ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋งŒํผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ์ •์ƒ์ ์œผ๋กœ ๋ถ„๋ฆฌ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('X ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(X_test) print('y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ :') print(list(y_test)) X ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [[18 19] [20 21] [22 23]] y ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ : [9, 10, 11] ๊ฐ ๊ธธ์ด๊ฐ€ 3์ธ ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. train_test_split()๊ณผ ๋‹ค๋ฅธ ์ ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์„ž์ด์ง€ ์•Š์€ ์ฑ„ ์–ด๋Š ์ง€์ ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์•ž๊ณผ ๋’ค๋กœ ๋ถ„๋ฆฌํ–ˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ˆ˜๋™์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ „์— ์ˆ˜๋™์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์„ž๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋’ค์—์„œ ์ด๋Ÿฌํ•œ ์‹ค์Šต๋“ค์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 02. [๊ธฐ์ดˆ โœ”] - ํŒŒ์ด ํ† ์น˜ ๊ธฐ์ดˆ(PyTorch Basic) 2์ฑ•ํ„ฐ์—์„œ๋Š” ํŒŒ์ด ํ† ์น˜์— ๋Œ€ํ•œ ๊ธฐ์ดˆ๋ฅผ ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค. 02-01 ํŒŒ์ด ํ† ์น˜ ํŒจํ‚ค์ง€์˜ ๊ธฐ๋ณธ ๊ตฌ์„ฑ ์•„๋ž˜์˜ ๋‚ด์šฉ์€ ํŒŒ์ด ํ† ์น˜ ํŒจํ‚ค์ง€์˜ ์ „๋ฐ˜์ ์ธ ๊ตฌ์„ฑ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 1. torch ๋ฉ”์ธ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. ํ…์„œ ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ์ˆ˜ํ•™ ํ•จ์ˆ˜๊ฐ€ ํฌํ•จ๋ผ ์žˆ์œผ๋ฉฐ Numpy์™€ ์œ ์‚ฌํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 2. torch.autograd ์ž๋™ ๋ฏธ๋ถ„์„ ์œ„ํ•œ ํ•จ์ˆ˜๋“ค์ด ํฌํ•จ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž๋™ ๋ฏธ๋ถ„์˜ on/off๋ฅผ ์ œ์–ดํ•˜๋Š” ์ฝ˜ํ…์ŠคํŠธ ๋งค๋‹ˆ์ €(enable_grad/no_grad)๋‚˜ ์ž์ฒด ๋ฏธ๋ถ„ ๊ฐ€๋Šฅ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์ธ 'Function' ๋“ฑ์ด ํฌํ•จ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. 3. torch.nn ์‹ ๊ฒฝ๋ง์„ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋‚˜ ๋ ˆ์ด์–ด ๋“ฑ์ด ์ •์˜๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด RNN, LSTM๊ณผ ๊ฐ™์€ ๋ ˆ์ด์–ด, ReLU์™€ ๊ฐ™์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜, MSELoss์™€ ๊ฐ™์€ ์†์‹ค ํ•จ์ˆ˜๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. 4. torch.optim ํ™•๋ฅ ์  ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Stochastic Gradient Descent, SGD)๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ตœ์ ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ตฌํ˜„๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. 5. torch.utils.data SGD์˜ ๋ฐ˜๋ณต ์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์šฉ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๊ฐ€ ํฌํ•จ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. 6. torch.onnx ONNX(Open Neural Network Exchange)์˜ ํฌ๋งท์œผ๋กœ ๋ชจ๋ธ์„ ์ต์ŠคํฌํŠธ(export)ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ONNX๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋”ฅ ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ ๊ฐ„์— ๋ชจ๋ธ์„ ๊ณต์œ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํฌ๋งท์ž…๋‹ˆ๋‹ค. 02-02 ํ…์„œ ์กฐ์ž‘ํ•˜๊ธฐ(Tensor Manipulation) 1 ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šธ ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ ๋ฆฌ๋ทฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฒกํ„ฐ, ํ–‰๋ ฌ, ํ…์„œ์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜๊ณ , Numpy์™€ ํŒŒ์ด ํ† ์น˜๋กœ ๋ฒกํ„ฐ, ํ–‰๋ ฌ, ํ…์„œ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. ๋ฒกํ„ฐ, ํ–‰๋ ฌ ๊ทธ๋ฆฌ๊ณ  ํ…์„œ(Vector, Matrix and Tensor) ๋”ฅ ๋Ÿฌ๋‹์„ ์œ„ํ•œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ˆ˜ํ•™์  ์ง€์‹์ธ ๋ฒกํ„ฐ, ํ–‰๋ ฌ, ํ…์„œ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ๋„˜ํŒŒ์ด ํ›‘์–ด๋ณด๊ธฐ(Numpy Review) ํŒŒ์ด ํ† ์น˜๋Š” ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€ ๋„˜ํŒŒ์ด(Numpy)์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋„˜ํŒŒ์ด์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜ ํ…์„œ ์„ ์–ธํ•˜๊ธฐ(PyTorch Tensor Allocation) ๋„˜ํŒŒ์ด๋กœ ์‹ค์Šต์„ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ์ด์ œ ํŒŒ์ด ํ† ์น˜ ํ…์„œ ์„ ์–ธ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ํ–‰๋ ฌ ๊ณฑ์…ˆ(Maxtrix Multiplication) ํ–‰๋ ฌ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์˜คํผ๋ ˆ์ด์…˜๋“ค(Other Basic Ops) ๋‹ค๋ฅธ ๊ธฐ๋ณธ์ ์ธ ์˜คํผ๋ ˆ์ด์…˜๋“ค์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…๋‹ˆ๋‹ค. 1. ๋ฒกํ„ฐ, ํ–‰๋ ฌ ๊ทธ๋ฆฌ๊ณ  ํ…์„œ(Vector, Matrix and Tensor) 1) ๋ฒกํ„ฐ, ํ–‰๋ ฌ, ํ…์„œ ๊ทธ๋ฆผ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ๋”ฅ ๋Ÿฌ๋‹์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค๋ฃจ๊ฒŒ ๋˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋‹จ์œ„๋Š” ๋ฒกํ„ฐ, ํ–‰๋ ฌ, ํ…์„œ์ž…๋‹ˆ๋‹ค. ์ฐจ์›์ด ์—†๋Š” ๊ฐ’์„ ์Šค์นผ๋ผ(์œ„์˜ ๊ทธ๋ฆผ์—๋Š” ์—†์Œ), 1์ฐจ์›์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฐ’์„ ์šฐ๋ฆฌ๋Š” ๋ฒกํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2์ฐจ์›์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฐ’์„ ํ–‰๋ ฌ(Matrix)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  3์ฐจ์›์ด ๋˜๋ฉด ์šฐ๋ฆฌ๋Š” ํ…์„œ(Tensor)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์šฐ๋ฆฌ๋Š” 3์ฐจ์›์˜ ์„ธ์ƒ์— ์‚ด๊ณ  ์žˆ์œผ๋ฏ€๋กœ, 4์ฐจ์› ์ด์ƒ๋ถ€ํ„ฐ๋Š” ๋จธ๋ฆฌ๋กœ ์ƒ๊ฐํ•˜๊ธฐ๋Š” ์–ด๋ ต์Šต๋‹ˆ๋‹ค. 4์ฐจ์›์€ 3์ฐจ์›์˜ ํ…์„œ๋ฅผ ์œ„๋กœ ์Œ“์•„ ์˜ฌ๋ฆฐ ๋ชจ์Šต์œผ๋กœ ์ƒ์ƒํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 5์ฐจ์›์€ ๊ทธ 4์ฐจ์›์„ ๋‹ค์‹œ ์˜†์œผ๋กœ ํ™•์žฅํ•œ ๋ชจ์Šต์œผ๋กœ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. 6์ฐจ์›์€ 5์ฐจ์›์„ ๋’ค๋กœ ํ™•์žฅํ•œ ๋ชจ์Šต์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์‚ฌ์ด์–ธ์Šค ๋ถ„์•ผ ํ•œ์ •์œผ๋กœ 3์ฐจ์› ์ด์ƒ์˜ ํ…์„œ๋Š” ๊ทธ๋ƒฅ ๋‹ค์ฐจ์› ํ–‰๋ ฌ ๋˜๋Š” ๋ฐฐ์—ด๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ฃผ๋กœ 3์ฐจ์› ์ด์ƒ์„ ํ…์„œ๋ผ๊ณ  ํ•˜๊ธด ํ•˜์ง€๋งŒ, 1์ฐจ์› ๋ฒกํ„ฐ๋‚˜ 2์ฐจ์›์ธ ํ–‰๋ ฌ๋„ ํ…์„œ๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ํ‘œํ˜„์ž…๋‹ˆ๋‹ค. ๋ฒกํ„ฐ = 1์ฐจ์› ํ…์„œ, 2์ฐจ์› ํ–‰๋ ฌ = 2์ฐจ์› ํ…์„œ. ๊ทธ๋ฆฌ๊ณ  3์ฐจ์› ํ…์„œ, 4์ฐจ์› ํ…์„œ, 5์ฐจ์› ํ…์„œ ๋“ฑ... 2) PyTorch Tensor Shape Convention ์‚ฌ์‹ค ๋”ฅ ๋Ÿฌ๋‹์„ ํ•  ๋•Œ ๋‹ค๋ฃจ๊ณ  ์žˆ๋Š” ํ–‰๋ ฌ ๋˜๋Š” ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์€ ํ•ญ์ƒ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์•ž์œผ๋กœ ํ–‰๋ ฌ๊ณผ ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ๋‹ค๋ฃจ๊ฒŒ ๋  ํ…์„œ ์ค‘ ๊ฐ€์žฅ ์ „ํ˜•์ ์ธ 2์ฐจ์› ํ…์„œ๋ฅผ ์˜ˆ๋กœ ๋“ค์–ด๋ณผ๊นŒ์š”? * 2D Tensor(Typical Simple Setting) |t| = (Batch size, dim) ์œ„์˜ ๊ฒฝ์šฐ๋Š” 2์ฐจ์› ํ…์„œ์˜ ํฌ๊ธฐ |t|๋ฅผ (batch size ร— dimension)์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์„ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์กฐ๊ธˆ ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด, ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ํ–‰๋ ฌ์—์„œ ํ–‰์˜ ํฌ๊ธฐ๊ฐ€ batch size, ์—ด์˜ ํฌ๊ธฐ๊ฐ€ dim์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ํ•˜๋‚˜์˜ ํฌ๊ธฐ๋ฅผ 256์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. [3, 1, 2, 5, ...] ์ด๋Ÿฐ ์ˆซ์ž๋“ค์˜ ๋‚˜์—ด์ด 256์˜ ๊ธธ์ด๋กœ ์žˆ๋‹ค๊ณ  ์ƒ์ƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ํ•˜๋‚˜ = ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 256์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด๋Ÿฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 3000๊ฐœ๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ํ˜„์žฌ ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋Š” 3,000 ร— 256์ž…๋‹ˆ๋‹ค. ํ–‰๋ ฌ์ด๋‹ˆ๊นŒ 2D ํ…์„œ๋„ค์š”. 3,000๊ฐœ๋ฅผ 1๊ฐœ์”ฉ ๊บผ๋‚ด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ปดํ“จํ„ฐ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ณดํ†ต ๋ฉ์–ด๋ฆฌ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. 3,000๊ฐœ์—์„œ 64๊ฐœ์”ฉ ๊บผ๋‚ด์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด ์ด๋•Œ batch size๋ฅผ 64๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ปดํ“จํ„ฐ๊ฐ€ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋Š” 2D ํ…์„œ์˜ ํฌ๊ธฐ๋Š” (batch size ร— dim) = 64 ร— 256์ž…๋‹ˆ๋‹ค. * 3D Tensor(Typical Computer Vision) - ๋น„์ „ ๋ถ„์•ผ์—์„œ์˜ 3์ฐจ์› ํ…์„œ |t| = (batch size, width, height) ์ผ๋ฐ˜์ ์œผ๋กœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ณด๋‹ค ๋น„์ „ ๋ถ„์•ผ(์ด๋ฏธ์ง€, ์˜์ƒ ์ฒ˜๋ฆฌ)๋ฅผ ํ•˜์‹œ๊ฒŒ ๋œ๋‹ค๋ฉด ์ข€ ๋” ๋ณต์žกํ•œ ํ…์„œ๋ฅผ ๋‹ค๋ฃจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋ผ๋Š” ๊ฒƒ์€ ๊ฐ€๋กœ, ์„ธ๋กœ๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๋Ÿฌ ์žฅ์˜ ์ด๋ฏธ์ง€, ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ batch size๋กœ ๊ตฌ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด 3์ฐจ์›์˜ ํ…์„œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์„ธ๋กœ๋Š” batch size, ๊ฐ€๋กœ๋Š” ๋„ˆ๋น„(width), ๊ทธ๋ฆฌ๊ณ  ์•ˆ์ชฝ์œผ๋กœ๋Š” ๋†’์ด(height)๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. * 3D Tensor(Typical Natural Language Processing) - NLP ๋ถ„์•ผ์—์„œ์˜ 3์ฐจ์› ํ…์„œ |t| = (batch size, length, dim) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋Š” ๋ณดํ†ต (batch size, ๋ฌธ์žฅ ๊ธธ์ด, ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์›)์ด๋ผ๋Š” 3์ฐจ์› ํ…์„œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. * NLP ๋ถ„์•ผ์˜ 3D ํ…์„œ ์˜ˆ์ œ๋กœ ์ดํ•ดํ•˜๊ธฐ - ์˜ฎ๊ธด์ด ์•„๋ž˜์™€ ๊ฐ™์ด 4๊ฐœ์˜ ๋ฌธ์žฅ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. [[๋‚˜๋Š” ์‚ฌ๊ณผ๋ฅผ ์ข‹์•„ํ•ด], [๋‚˜๋Š” ๋ฐ”๋‚˜๋‚˜๋ฅผ ์ข‹์•„ํ•ด], [๋‚˜๋Š” ์‚ฌ๊ณผ๋ฅผ ์‹ซ์–ดํ•ด], [๋‚˜๋Š” ๋ฐ”๋‚˜๋‚˜๋ฅผ ์‹ซ์–ดํ•ด]] ์ปดํ“จํ„ฐ๋Š” ์•„์ง ์ด ์ƒํƒœ๋กœ๋Š” '๋‚˜๋Š” ์‚ฌ๊ณผ๋ฅผ ์ข‹์•„ํ•ด'๊ฐ€ ๋‹จ์–ด๊ฐ€ 1๊ฐœ์ธ์ง€ 3๊ฐœ์ธ์ง€ ์ดํ•ดํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ปดํ“จํ„ฐ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹จ์–ด๋ณ„๋กœ ๋‚˜๋ˆ ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. [['๋‚˜๋Š”', '์‚ฌ๊ณผ๋ฅผ', '์ข‹์•„ํ•ด'], ['๋‚˜๋Š”', '๋ฐ”๋‚˜๋‚˜๋ฅผ', '์ข‹์•„ํ•ด'], ['๋‚˜๋Š”', '์‚ฌ๊ณผ๋ฅผ', '์‹ซ์–ดํ•ด'], ['๋‚˜๋Š”', '๋ฐ”๋‚˜๋‚˜๋ฅผ', '์‹ซ์–ดํ•ด']] ์ด์ œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋Š” 4 ร— 3์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 2D ํ…์„œ์ž…๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ๋Š” ํ…์ŠคํŠธ๋ณด๋‹ค๋Š” ์ˆซ์ž๋ฅผ ๋” ์ž˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต ๋‹ˆ๋ฐ. ์ด์ œ ๊ฐ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ค ๊ฒ๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹จ์–ด๋ฅผ 3์ฐจ์›์˜ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ–ˆ๋‹ค๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. '๋‚˜๋Š”' = [0.1, 0.2, 0.9] '์‚ฌ๊ณผ๋ฅผ' = [0.3, 0.5, 0.1] '๋ฐ”๋‚˜๋‚˜๋ฅผ' = [0.3, 0.5, 0.2] '์ข‹์•„ํ•ด' = [0.7, 0.6, 0.5] '์‹ซ์–ดํ•ด' = [0.5, 0.6, 0.7] ์œ„ ๊ธฐ์ค€์„ ๋”ฐ๋ผ์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์žฌ๊ตฌ์„ฑํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. [[[0.1, 0.2, 0.9], [0.3, 0.5, 0.1], [0.7, 0.6, 0.5]], [[0.1, 0.2, 0.9], [0.3, 0.5, 0.2], [0.7, 0.6, 0.5]], [[0.1, 0.2, 0.9], [0.3, 0.5, 0.1], [0.5, 0.6, 0.7]], [[0.1, 0.2, 0.9], [0.3, 0.5, 0.2], [0.5, 0.6, 0.7]]] ์ด์ œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” 4 ร— 3 ร— 3์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 3D ํ…์„œ์ž…๋‹ˆ๋‹ค. ์ด์ œ batch size๋ฅผ 2๋กœ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฐฐ์น˜ #1 [[[0.1, 0.2, 0.9], [0.3, 0.5, 0.1], [0.7, 0.6, 0.5]], [[0.1, 0.2, 0.9], [0.3, 0.5, 0.2], [0.7, 0.6, 0.5]]] ๋‘ ๋ฒˆ์งธ ๋ฐฐ์น˜ #2 [[[0.1, 0.2, 0.9], [0.3, 0.5, 0.1], [0.5, 0.6, 0.7]], [[0.1, 0.2, 0.9], [0.3, 0.5, 0.2], [0.5, 0.6, 0.7]]] ์ปดํ“จํ„ฐ๋Š” ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ๊ฐ€์ ธ๊ฐ€์„œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ ๊ฐ ๋ฐฐ์น˜์˜ ํ…์„œ์˜ ํฌ๊ธฐ๋Š” (2 ร— 3 ร— 3)์ž…๋‹ˆ๋‹ค. ์ด๋Š” (batch size, ๋ฌธ์žฅ ๊ธธ์ด, ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์›)์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. 2. ๋„˜ํŒŒ์ด๋กœ ํ…์„œ ๋งŒ๋“ค๊ธฐ(๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ๋งŒ๋“ค๊ธฐ) PyTorch๋กœ ํ…์„œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ธฐ ์ „์— ์šฐ์„  Numpy๋กœ ํ…์„œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  numpy๋ฅผ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import numpy as np Numpy๋กœ ํ…์„œ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•œ๋ฐ [์ˆซ์ž, ์ˆซ์ž, ์ˆซ์ž]์™€ ๊ฐ™์€<NAME>์œผ๋กœ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ np.array()๋กœ ๊ฐ์‹ธ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 1) 1D with Numpy Numpy๋กœ 1์ฐจ์› ํ…์„œ์ธ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. t = np.array([0., 1., 2., 3., 4., 5., 6.]) # ํŒŒ์ด์ฌ์œผ๋กœ ์„ค๋ช…ํ•˜๋ฉด List๋ฅผ ์ƒ์„ฑํ•ด์„œ np.array๋กœ 1์ฐจ์› array๋กœ ๋ณ€ํ™˜ํ•จ. print(t) [0. 1. 2. 3. 4. 5. 6.] ์ด์ œ 1์ฐจ์› ํ…์„œ์ธ ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('Rank of t: ', t.ndim) print('Shape of t: ', t.shape) Rank of t: 1 Shape of t: (7, ) .ndim์€ ๋ช‡ ์ฐจ์›์ธ์ง€๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. 1์ฐจ์›์€ ๋ฒกํ„ฐ, 2์ฐจ์›์€ ํ–‰๋ ฌ, 3์ฐจ์›์€ 3์ฐจ์› ํ…์„œ์˜€์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” ๋ฒกํ„ฐ์ด๋ฏ€๋กœ 1์ฐจ์›์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. .shape๋Š” ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (7, )๋Š” (1, 7)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด (1 ร— 7)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์˜ฎ๊ธด์ด ์ฃผ : ํ…์„œ์˜ ํฌ๊ธฐ(shape)๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ,(์ฝค๋งˆ)๋ฅผ ์“ฐ๊ธฐ๋„ ํ•˜๊ณ  ร—(๊ณฑํ•˜๊ธฐ)๋ฅผ ์“ฐ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 2ํ–‰ 3์—ด์˜ 2D ํ…์„œ๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ (2, 3)๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•˜๊ณ  (2 ร— 3)์ด๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. (5, )์˜<NAME>์€ (1 ร— 5)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 1-1) Numpy ๊ธฐ์ดˆ ์ดํ•ดํ•˜๊ธฐ ์ด์ œ Numpy์—์„œ ๊ฐ ๋ฒกํ„ฐ์˜ ์›์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Numpy์—์„œ ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. print('t[0] t[1] t[-1] = ', t[0], t[1], t[-1]) # ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•œ ์›์†Œ ์ ‘๊ทผ t[0] t[1] t[-1] = 0.0 1.0 6.0 ์œ„์˜ ๊ฒฐ๊ณผ๋Š” 0๋ฒˆ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ ์›์†Œ์ธ 0.0, 1๋ฒˆ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ ์›์†Œ์ธ 1.0, -1๋ฒˆ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ ์›์†Œ์ธ 6.0์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. -1๋ฒˆ ์ธ๋ฑ์Šค๋Š” ๋งจ ๋’ค์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค์ž…๋‹ˆ๋‹ค. ๋ฒ”์œ„ ์ง€์ •์œผ๋กœ ์›์†Œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์Šฌ๋ผ์ด์‹ฑ(Slicing)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ [์‹œ์ž‘ ๋ฒˆํ˜ธ : ๋ ๋ฒˆํ˜ธ]๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์Šฌ๋ผ์ด์‹ฑ์€ [์‹œ์ž‘ ๋ฒˆํ˜ธ : ๋ ๋ฒˆํ˜ธ]๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ๋ ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์€ ํฌํ•จํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. print('t[2:5] t[4:-1] = ', t[2:5], t[4:-1]) # [์‹œ์ž‘ ๋ฒˆํ˜ธ : ๋ ๋ฒˆํ˜ธ]๋กœ ๋ฒ”์œ„ ์ง€์ •์„ ํ†ตํ•ด ๊ฐ€์ ธ์˜จ๋‹ค. t[2:5] t[4:-1] = [2. 3. 4.] [4. 5.] ์œ„์˜ ์Šฌ๋ผ์ด์‹ฑ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. [2:5]๋ผ๊ณ  ํ•œ๋‹ค๋ฉด 2๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ 4๋ฒˆ ์ธ๋ฑ์Šค๊นŒ์ง€์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. [4:-1]์€ 4๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋์—์„œ ์ฒซ ๋ฒˆ์งธ ๊ฒƒ๊นŒ์ง€์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์‹œ์ž‘ ๋ฒˆํ˜ธ ๋˜๋Š” ๋ ๋ฒˆํ˜ธ๋ฅผ ์ƒ๋žตํ•ด์„œ ์Šฌ๋ผ์ด์‹ฑ์„ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. [์‹œ์ž‘ ๋ฒˆํ˜ธ:๋ ๋ฒˆํ˜ธ]์—์„œ ์‹œ์ž‘ ๋ฒˆํ˜ธ๋ฅผ ์ƒ๋žตํ•˜๋ฉด ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ ๋ฒˆํ˜ธ๊นŒ์ง€ ๋ฝ‘์•„๋ƒ…๋‹ˆ๋‹ค, ๋ฐ˜๋ฉด์— [์‹œ์ž‘ ๋ฒˆํ˜ธ:๋ ๋ฒˆํ˜ธ]์—์„œ ๋ ๋ฒˆํ˜ธ๋ฅผ ์ƒ๋žตํ•˜๋ฉด ์‹œ์ž‘ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฝ‘์•„๋ƒ…๋‹ˆ๋‹ค. print('t[:2] t[3:] = ', t[:2], t[3:]) # ์‹œ์ž‘ ๋ฒˆํ˜ธ๋ฅผ ์ƒ๋žตํ•œ ๊ฒฝ์šฐ์™€ ๋ ๋ฒˆํ˜ธ๋ฅผ ์ƒ๋žตํ•œ ๊ฒฝ์šฐ t[:2] t[3:] = [0. 1.] [3. 4. 5. 6.] 2) 2D with Numpy Numpy๋กœ 2์ฐจ์› ํ–‰๋ ฌ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. t = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.], [10., 11., 12.]]) print(t) [[ 1. 2. 3.] [ 4. 5. 6.] [ 7. 8. 9.] [10. 11. 12.]] print('Rank of t: ', t.ndim) print('Shape of t: ', t.shape) Rank of t: 2 Shape of t: (4, 3) .ndim์€ ๋ช‡ ์ฐจ์›์ธ์ง€๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. 1์ฐจ์›์€ ๋ฒกํ„ฐ, 2์ฐจ์›์€ ํ–‰๋ ฌ, 3์ฐจ์›์€ 3์ฐจ์› ํ…์„œ์˜€์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” ํ–‰๋ ฌ์ด๋ฏ€๋กœ 2์ฐจ์›์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. .shape๋Š” ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (4, 3)์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ‘œํ˜„์œผ๋กœ๋Š” (4 ร— 3)์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ–‰๋ ฌ์ด 4ํ–‰ 3์—ด์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Numpy๋กœ๋„ 3์ฐจ์› ํ…์„œ๋„ ๋งŒ๋“ค ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ์ด ์‹œ์ ์—์„œ Numpy์™€ PyTorch๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด PyTorch๋กœ ์‹ค์Šต์„ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. 3. ํŒŒ์ด ํ† ์น˜ ํ…์„œ ์„ ์–ธํ•˜๊ธฐ(PyTorch Tensor Allocation) ํŒŒ์ด ํ† ์น˜๋Š” Numpy์™€ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋” ๋‚ซ์Šต๋‹ˆ๋‹ค(better). ์šฐ์„  torch๋ฅผ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch Numpy๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง„ํ–‰ํ–ˆ๋˜ ์‹ค์Šต์„ ํŒŒ์ด ํ† ์น˜๋กœ ๋˜‘๊ฐ™์ด ํ•ด๋ด…์‹œ๋‹ค. 1) 1D with PyTorch ํŒŒ์ด ํ† ์น˜๋กœ 1์ฐจ์› ํ…์„œ์ธ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. t = torch.FloatTensor([0., 1., 2., 3., 4., 5., 6.]) print(t) dim()์„ ์‚ฌ์šฉํ•˜๋ฉด ํ˜„์žฌ ํ…์„œ์˜ ์ฐจ์›์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. shape๋‚˜ size()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(t.dim()) # rank. ์ฆ‰, ์ฐจ์› print(t.shape) # shape print(t.size()) # shape torch.Size([7]) torch.Size([7]) ํ˜„์žฌ 1์ฐจ์› ํ…์„œ์ด๋ฉฐ, ์›์†Œ๋Š” 7๊ฐœ์ž…๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ๊ณผ ์Šฌ๋ผ์ด์‹ฑ์„ ํ•ด๋ด…์‹œ๋‹ค. ๋ฐฉ๋ฒ•์€ Numpy ์‹ค์Šต๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. print(t[0], t[1], t[-1]) # ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผ print(t[2:5], t[4:-1]) # ์Šฌ๋ผ์ด์‹ฑ print(t[:2], t[3:]) # ์Šฌ๋ผ์ด์‹ฑ tensor(0.) tensor(1.) tensor(6.) tensor([2., 3., 4.]) tensor([4., 5.]) tensor([0., 1.]) tensor([3., 4., 5., 6.]) 2) 2D with PyTorch ํŒŒ์ด ํ† ์น˜๋กœ 2์ฐจ์› ํ…์„œ์ธ ํ–‰๋ ฌ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. t = torch.FloatTensor([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.], [10., 11., 12.] ]) print(t) tensor([[ 1., 2., 3.], [ 4., 5., 6.], [ 7., 8., 9.], [10., 11., 12.]]) dim()์„ ์‚ฌ์šฉํ•˜๋ฉด ํ˜„์žฌ ํ…์„œ์˜ ์ฐจ์›์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. size()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(t.dim()) # rank. ์ฆ‰, ์ฐจ์› print(t.size()) # shape torch.Size([4, 3]) ํ˜„์žฌ ํ…์„œ์˜ ์ฐจ์›์€ 2์ฐจ์›์ด๋ฉฐ, (4, 3)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์Šฌ๋ผ์ด์‹ฑ์„ ํ•ด๋ด…์‹œ๋‹ค. print(t[:, 1]) # ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์„ ์ „์ฒด ์„ ํƒํ•œ ์ƒํ™ฉ์—์„œ ๋‘ ๋ฒˆ์งธ ์ฐจ์›์˜ ์ฒซ ๋ฒˆ์งธ ๊ฒƒ๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค. print(t[:, 1].size()) # โ†‘ ์œ„์˜ ๊ฒฝ์šฐ์˜ ํฌ๊ธฐ tensor([ 2., 5., 8., 11.]) torch.Size([4]) ์œ„์˜ ๊ฒฐ๊ณผ๋Š” ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์„ ์ „์ฒด ์„ ํƒํ•˜๊ณ , ๊ทธ ์ƒํ™ฉ์—์„œ ๋‘ ๋ฒˆ์งธ ์ฐจ์›์˜ 1๋ฒˆ ์ธ๋ฑ์Šค ๊ฐ’๋งŒ์„ ๊ฐ€์ ธ์˜จ ๊ฒฝ์šฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ…์„œ์—์„œ ๋‘ ๋ฒˆ์งธ ์—ด์— ์žˆ๋Š” ๋ชจ๋“  ๊ฐ’์„ ๊ฐ€์ ธ์˜จ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ ๊ฒฝ์šฐ์˜ ํฌ๊ธฐ๋Š” 4์ž…๋‹ˆ๋‹ค. (1์ฐจ์› ๋ฒกํ„ฐ) print(t[:, :-1]) # ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์„ ์ „์ฒด ์„ ํƒํ•œ ์ƒํ™ฉ์—์„œ ๋‘ ๋ฒˆ์งธ ์ฐจ์›์—์„œ๋Š” ๋งจ ๋งˆ์ง€๋ง‰์—์„œ ์ฒซ ๋ฒˆ์งธ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋‹ค ๊ฐ€์ ธ์˜จ๋‹ค. tensor([[ 1., 2.], [ 4., 5.], [ 7., 8.], [10., 11.]]) ์œ„์˜ ๊ฒฐ๊ณผ๋Š” ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์„ ์ „์ฒด ์„ ํƒํ•œ ์ƒํ™ฉ์—์„œ ๋‘ ๋ฒˆ์งธ ์ฐจ์›์—์„œ๋Š” ๋งจ ๋งˆ์ง€๋ง‰์—์„œ ์ฒซ ๋ฒˆ์งธ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋‹ค ๊ฐ€์ ธ์˜ค๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. 3) ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…(Broadcasting) ๋‘ ํ–‰๋ ฌ A, B๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ํ–‰๋ ฌ์˜ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ์— ๋Œ€ํ•ด ์•Œ๊ณ  ๊ณ„์‹ ๋‹ค๋ฉด, ์ด ๋ง์…ˆ๊ณผ ๋บ„์…ˆ์„ ํ•  ๋•Œ์—๋Š” ๋‘ ํ–‰๋ ฌ A, B์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ๊ณ„์‹ค ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ํ–‰๋ ฌ์ด ๊ณฑ์…ˆ์„ ํ•  ๋•Œ์—๋Š” A์˜ ๋งˆ์ง€๋ง‰ ์ฐจ์›๊ณผ B์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์ด ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์ด๋Ÿฐ ๊ทœ์น™๋“ค์ด ์žˆ์ง€๋งŒ ๋”ฅ ๋Ÿฌ๋‹์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋ถˆ๊ฐ€ํ”ผํ•˜๊ฒŒ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ํ–‰๋ ฌ ๋˜๋Š” ํ…์„œ์— ๋Œ€ํ•ด์„œ ์‚ฌ์น™ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ํ•„์š”๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ์ž๋™์œผ๋กœ ํฌ๊ธฐ๋ฅผ ๋งž์ถฐ์„œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…์ด๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๊ฐ™์€ ํฌ๊ธฐ์ผ ๋•Œ ์—ฐ์‚ฐ์„ ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. m1 = torch.FloatTensor([[3, 3]]) m2 = torch.FloatTensor([[2, 2]]) print(m1 + m2) tensor([[5., 5.]]) ์—ฌ๊ธฐ์„œ m1, ๊ณผ m2์˜ ํฌ๊ธฐ๋Š” ๋‘˜ ๋‹ค (1, 2)์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฌธ์ œ์—†์ด ๋ง์…ˆ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ํ…์„œ๋“ค ๊ฐ„์˜ ์—ฐ์‚ฐ์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋ฒกํ„ฐ์™€ ์Šค์นผ๋ผ๊ฐ€ ๋ง์…ˆ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์ˆ˜ํ•™์ ์œผ๋กœ๋Š” ์›๋ž˜ ์—ฐ์‚ฐ์ด ์•ˆ ๋˜๋Š” ๊ฒŒ ๋งž์ง€๋งŒ ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…์„ ํ†ตํ•ด ์ด๋ฅผ ์—ฐ์‚ฐํ•ฉ๋‹ˆ๋‹ค. # Vector + scalar m1 = torch.FloatTensor([[1, 2]]) m2 = torch.FloatTensor([3]) # [3] -> [3, 3] print(m1 + m2) tensor([[4., 5.]]) ์›๋ž˜ m1์˜ ํฌ๊ธฐ๋Š” (1, 2)์ด๋ฉฐ m2์˜ ํฌ๊ธฐ๋Š” (1, )์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํŒŒ์ด ํ† ์น˜๋Š” m2์˜ ํฌ๊ธฐ๋ฅผ (1, 2)๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฒกํ„ฐ ๊ฐ„ ์—ฐ์‚ฐ์—์„œ ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…์ด ์ ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # 2 x 1 Vector + 1 x 2 Vector m1 = torch.FloatTensor([[1, 2]]) m2 = torch.FloatTensor([[3], [4]]) print(m1 + m2) tensor([4., 5.], [5., 6.]]) m1์˜ ํฌ๊ธฐ๋Š” (1, 2) m2์˜ ํฌ๊ธฐ๋Š” (2, 1)์˜€์Šต๋‹ˆ๋‹ค. ์ด ๋‘ ๋ฒกํ„ฐ๋Š” ์›๋ž˜ ์ˆ˜ํ•™์ ์œผ๋กœ๋Š” ๋ง์…ˆ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํŒŒ์ด ํ† ์น˜๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ (2, 2)๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ๋ง์…ˆ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. # ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ… ๊ณผ์ •์—์„œ ์‹ค์ œ๋กœ ๋‘ ํ…์„œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€๊ฒฝ๋˜๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. [1, 2] ==> [[1, 2], [1, 2]] [3] [4] ==> [[3, 3], [4, 4]] ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…์€ ํŽธ๋ฆฌํ•˜์ง€๋งŒ, ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๋Š” ๊ธฐ๋Šฅ์ด๋ฏ€๋กœ ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ ๊ต‰์žฅํžˆ ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด A ํ…์„œ์™€ B ํ…์„œ๊ฐ€ ์žˆ์„ ๋•Œ, ์‚ฌ์šฉ์ž๋Š” ์ด ๋‘ ํ…์„œ์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ™๋‹ค๊ณ  ์ฐฉ๊ฐํ•˜๊ณ  ๋ง์…ˆ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ์ด ๋‘ ํ…์„œ์˜ ํฌ๊ธฐ๋Š” ๋‹ฌ๋ž๊ณ  ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…์ด ์ˆ˜ํ–‰๋˜์–ด ๋ง์…ˆ ์—ฐ์‚ฐ์ด ์ˆ˜ํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋‘ ํ…์„œ์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅด๋‹ค๊ณ  ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค๋ฉด ์‚ฌ์šฉ์ž๋Š” ์ด ์—ฐ์‚ฐ์ด ์ž˜๋ชป๋˜์—ˆ์Œ์„ ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…์€ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰๋˜๋ฏ€๋กœ ์‚ฌ์šฉ์ž๋Š” ๋‚˜์ค‘์— ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์•˜๋”๋ผ๋„ ์–ด๋””์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์ฐพ๊ธฐ๊ฐ€ ๊ต‰์žฅํžˆ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4) ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋Šฅ๋“ค 1) ํ–‰๋ ฌ ๊ณฑ์…ˆ๊ณผ ๊ณฑ์…ˆ์˜ ์ฐจ์ด(Matrix Multiplication Vs. Multiplication) ํ–‰๋ ฌ๋กœ ๊ณฑ์…ˆ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ํ–‰๋ ฌ ๊ณฑ์…ˆ(.matmul)๊ณผ ์›์†Œ ๋ณ„ ๊ณฑ์…ˆ(.mul)์ž…๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜ ํ…์„œ์˜ ํ–‰๋ ฌ ๊ณฑ์…ˆ์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” matmul()์„ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. m1 = torch.FloatTensor([[1, 2], [3, 4]]) m2 = torch.FloatTensor([[1], [2]]) print('Shape of Matrix 1: ', m1.shape) # 2 x 2 print('Shape of Matrix 2: ', m2.shape) # 2 x 1 print(m1.matmul(m2)) # 2 x 1 Shape of Matrix 1: torch.Size([2, 2]) Shape of Matrix 2: torch.Size([2, 1]) tensor([[ 5.], [11.]]) ์œ„์˜ ๊ฒฐ๊ณผ๋Š” 2 x 2 ํ–‰๋ ฌ๊ณผ 2 x 1 ํ–‰๋ ฌ(๋ฒกํ„ฐ)์˜ ํ–‰๋ ฌ ๊ณฑ์…ˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ–‰๋ ฌ ๊ณฑ์…ˆ์ด ์•„๋‹ˆ๋ผ element-wise ๊ณฑ์…ˆ์ด๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋™์ผํ•œ ํฌ๊ธฐ์˜ ํ–‰๋ ฌ์ด ๋™์ผํ•œ ์œ„์น˜์— ์žˆ๋Š” ์›์†Œ๋ผ๋ฆฌ ๊ณฑํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํฌ๊ธฐ์˜ ํ–‰๋ ฌ์ด ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…์ด ๋œ ํ›„์— element-wise ๊ณฑ์…ˆ์ด ์ˆ˜ํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋Š” * ๋˜๋Š” mul()์„ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. m1 = torch.FloatTensor([[1, 2], [3, 4]]) m2 = torch.FloatTensor([[1], [2]]) print('Shape of Matrix 1: ', m1.shape) # 2 x 2 print('Shape of Matrix 2: ', m2.shape) # 2 x 1 print(m1 * m2) # 2 x 2 print(m1.mul(m2)) Shape of Matrix 1: torch.Size([2, 2]) Shape of Matrix 2: torch.Size([2, 1]) tensor([[1., 2.], [6., 8.]]) tensor([[1., 2.], [6., 8.]]) m1 ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” (2, 2)์ด์—ˆ์Šต๋‹ˆ๋‹ค. m2 ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” (2, 1)์˜€์Šต๋‹ˆ๋‹ค. ์ด๋•Œ element-wise ๊ณฑ์…ˆ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด, ๋‘ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…์ด ๋œ ํ›„์— ๊ณฑ์…ˆ์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํžˆ๋Š” ์—ฌ๊ธฐ์„œ m2์˜ ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. # ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ… ๊ณผ์ •์—์„œ m2 ํ…์„œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€๊ฒฝ๋˜๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. [1] [2] ==> [[1, 1], [2, 2]] 2) ํ‰๊ท (Mean) ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” Numpy์—์„œ์˜ ์‚ฌ์šฉ๋ฒ•๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  1์ฐจ์›์ธ ๋ฒกํ„ฐ๋ฅผ ์„ ์–ธํ•˜์—ฌ. mean()์„ ์‚ฌ์šฉํ•˜์—ฌ ์›์†Œ์˜ ํ‰๊ท ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. t = torch.FloatTensor([1, 2]) print(t.mean()) tensor(1.5000) 1๊ณผ 2์˜ ํ‰๊ท ์ธ 1.5๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” 2์ฐจ์›์ธ ํ–‰๋ ฌ์„ ์„ ์–ธํ•˜์—ฌ. mean()์„ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  2์ฐจ์› ํ–‰๋ ฌ์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. t = torch.FloatTensor([[1, 2], [3, 4]]) print(t) tensor([[1., 2.], [3., 4.]]) 2์ฐจ์› ํ–‰๋ ฌ์ด ์„ ์–ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ. mean()์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. print(t.mean()) tensor(2.5000) 4๊ฐœ์˜ ์›์†Œ์˜ ํ‰๊ท ์ธ 2.5๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” dim. ์ฆ‰, ์ฐจ์›(dimension)์„ ์ธ์ž๋กœ ์ฃผ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(t.mean(dim=0)) tensor([2., 3.]) dim=0์ด๋ผ๋Š” ๊ฒƒ์€ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ–‰๋ ฌ์—์„œ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์€ 'ํ–‰'์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ธ์ž๋กœ dim์„ ์ค€๋‹ค๋ฉด ํ•ด๋‹น ์ฐจ์›์„ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ–‰๋ ฌ์—์„œ '์—ด'๋งŒ์„ ๋‚จ๊ธฐ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” (2, 2)์˜€์ง€๋งŒ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์—ด์˜ ์ฐจ์›๋งŒ ๋ณด์กด๋˜๋ฉด์„œ (1, 2)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” (2, )์™€ ๊ฐ™์œผ๋ฉฐ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์—ด์˜ ์ฐจ์›์„ ๋ณด์กดํ•˜๋ฉด์„œ ํ‰๊ท ์„ ๊ตฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฐ์‚ฐํ•ฉ๋‹ˆ๋‹ค. # ์‹ค์ œ ์—ฐ์‚ฐ ๊ณผ์ • t.mean(dim=0)์€ ์ž…๋ ฅ์—์„œ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์„ ์ œ๊ฑฐํ•œ๋‹ค. [[1., 2.], [3., 4.]] 1๊ณผ 3์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๊ณ , 2์™€ 4์˜ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค. ๊ฒฐ๊ณผ ==> [2., 3.] ์ด๋ฒˆ์—๋Š” ์ธ์ž๋กœ dim=1์„ ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋‘ ๋ฒˆ์งธ ์ฐจ์›์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์—ด์ด ์ œ๊ฑฐ๋œ ํ…์„œ๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. print(t.mean(dim=1)) tensor([1.5000, 3.5000]) ์—ด์˜ ์ฐจ์›์ด ์ œ๊ฑฐ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ (2, 2)์˜ ํฌ๊ธฐ์—์„œ (2, 1)์˜ ํฌ๊ธฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” 1๊ณผ 2์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๊ณ  3๊ณผ 4์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ์‹ค์ œ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋Š” (2 ร— 1) [1. 5] [3. 5] ํ•˜์ง€๋งŒ (2 ร— 1)์€ ๊ฒฐ๊ตญ 1์ฐจ์›์ด๋ฏ€๋กœ (1 ร— 2)์™€ ๊ฐ™์ด ํ‘œํ˜„๋˜๋ฉด์„œ ์œ„์™€ ๊ฐ™์ด [1.5, 3.5]๋กœ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” dim=-1์„ ์ฃผ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋งˆ์ง€๋ง‰ ์ฐจ์›์„ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๊ณ , ๊ฒฐ๊ตญ ์—ด์˜ ์ฐจ์›์„ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ์˜๋ฏธ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์œ„์™€ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์Šต๋‹ˆ๋‹ค. print(t.mean(dim=-1)) tensor([1.5000, 3.5000]) 3) ๋ง์…ˆ(Sum) ๋ง์…ˆ(Sum)์€ ํ‰๊ท (Mean)๊ณผ ์—ฐ์‚ฐ ๋ฐฉ๋ฒ•์ด๋‚˜ ์ธ์ž๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ์ •ํ™•ํžˆ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ํ‰๊ท ์ด ์•„๋‹ˆ๋ผ ๋ง์…ˆ์„ ํ•  ๋ฟ์ž…๋‹ˆ๋‹ค. t = torch.FloatTensor([[1, 2], [3, 4]]) print(t) tensor([[1., 2.], [3., 4.]]) print(t.sum()) # ๋‹จ์ˆœํžˆ ์›์†Œ ์ „์ฒด์˜ ๋ง์…ˆ์„ ์ˆ˜ํ–‰ print(t.sum(dim=0)) # ํ–‰์„ ์ œ๊ฑฐ print(t.sum(dim=1)) # ์—ด์„ ์ œ๊ฑฐ print(t.sum(dim=-1)) # ์—ด์„ ์ œ๊ฑฐ tensor(10.) tensor([4., 6.]) tensor([3., 7.]) tensor([3., 7.]) 4) ์ตœ๋Œ€(Max)์™€ ์•„ ๊ทธ ๋งฅ์Šค(ArgMax) ์ตœ๋Œ€(Max)๋Š” ์›์†Œ์˜ ์ตœ๋Œ“๊ฐ’์„ ๋ฆฌํ„ดํ•˜๊ณ , ์•„ ๊ทธ ๋งฅ์Šค(ArgMax)๋Š” ์ตœ๋Œ“๊ฐ’์„ ๊ฐ€์ง„ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. (2, 2) ํฌ๊ธฐ์˜ ํ–‰๋ ฌ์„ ์„ ์–ธํ•˜๊ณ  Max๋ฅผ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. t = torch.FloatTensor([[1, 2], [3, 4]]) print(t) tensor([[1., 2.], [3., 4.]]) ์šฐ์„  (2, 2) ํ–‰๋ ฌ์„ ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ. max()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. print(t.max()) # Returns one value: max tensor(4.) ์›์†Œ ์ค‘ ์ตœ๋Œ“๊ฐ’์ธ 4๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ธ์ž๋กœ dim=0์„ ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์„ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. print(t.max(dim=0)) # Returns two values: max and argmax (tensor([3., 4.]), tensor([1, 1])) ํ–‰์˜ ์ฐจ์›์„ ์ œ๊ฑฐํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ (1, 2) ํ…์„œ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋Š” [3, 4]์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ [1, 1]์ด๋ผ๋Š” ๊ฐ’๋„ ํ•จ๊ป˜ ๋ฆฌํ„ด๋˜์—ˆ์Šต๋‹ˆ๋‹ค. max์— dim ์ธ์ž๋ฅผ ์ฃผ๋ฉด argmax๋„ ํ•จ๊ป˜ ๋ฆฌํ„ดํ•˜๋Š” ํŠน์ง• ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์—ด์—์„œ 3์˜ ์ธ๋ฑ์Šค๋Š” 1์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์—ด์—์„œ 4์˜ ์ธ๋ฑ์Šค๋Š” 1์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ [1, 1]์ด ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ์˜๋ฏธ์ธ์ง€๋Š” ์•„๋ž˜ ์„ค๋ช…ํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. # [1, 1]๊ฐ€ ๋ฌด์Šจ ์˜๋ฏธ์ธ์ง€ ๋ด…์‹œ๋‹ค. ๊ธฐ์กด ํ–‰๋ ฌ์„ ๋‹ค์‹œ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. [[1, 2], [3, 4]] ์ฒซ ๋ฒˆ์งธ ์—ด์—์„œ 0๋ฒˆ ์ธ๋ฑ์Šค๋Š” 1, 1๋ฒˆ ์ธ๋ฑ์Šค๋Š” 3์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์—ด์—์„œ 0๋ฒˆ ์ธ๋ฑ์Šค๋Š” 2, 1๋ฒˆ ์ธ๋ฑ์Šค๋Š” 4์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด 3๊ณผ 4์˜ ์ธ๋ฑ์Šค๋Š” [1, 1]์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‘ ๊ฐœ๋ฅผ ํ•จ๊ป˜ ๋ฆฌํ„ด ๋ฐ›๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ max ๋˜๋Š” argmax๋งŒ ๋ฆฌํ„ด ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฆฌํ„ด ๊ฐ’์—๋„ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 0๋ฒˆ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด max ๊ฐ’๋งŒ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๊ณ , 1๋ฒˆ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด argmax ๊ฐ’๋งŒ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print('Max: ', t.max(dim=0)[0]) print('Argmax: ', t.max(dim=0)[1]) Max: tensor([3., 4.]) Argmax: tensor([1, 1]) ์ด๋ฒˆ์—๋Š” dim=1๋กœ ์ธ์ž๋ฅผ ์ฃผ์—ˆ์„ ๋•Œ์™€ dim=-1๋กœ ์ธ์ž๋ฅผ ์ฃผ์—ˆ์„ ๋•Œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(t.max(dim=1)) print(t.max(dim=-1)) (tensor([2., 4.]), tensor([1, 1])) (tensor([2., 4.]), tensor([1, 1])) ๋ฐ์ดํ„ฐ ์‚ฌ์ด์–ธ์Šค ๋ถ„์•ผ ํ•œ์ •์œผ๋กœ 3์ฐจ์› ์ด์ƒ์˜ ํ…์„œ๋Š” ๊ทธ๋ƒฅ ๋‹ค์ฐจ์› ํ–‰๋ ฌ ๋˜๋Š” ๋ฐฐ์—ด๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ฃผ๋กœ 3์ฐจ์› ์ด์ƒ์„ ํ…์„œ๋ผ๊ณ  ํ•˜๊ธด ํ•˜์ง€๋งŒ, 1์ฐจ์› ๋ฒกํ„ฐ๋‚˜ 2์ฐจ์›์ธ ํ–‰๋ ฌ๋„ ํ…์„œ๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ํ‘œํ˜„์ž…๋‹ˆ๋‹ค. ๋ฒกํ„ฐ = 1์ฐจ์› ํ…์„œ, 2์ฐจ์› ํ–‰๋ ฌ = 2์ฐจ์› ํ…์„œ, ๊ทธ๋ฆฌ๊ณ  3์ฐจ์› ํ…์„œ, 4์ฐจ์› ํ…์„œ, 5์ฐจ์› ํ…์„œ ๋“ฑ... ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ํ•˜๋‚˜์˜ ํฌ๊ธฐ๋ฅผ 256์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. [3, 1, 2, 5, ...] ์ด๋Ÿฐ ์ˆซ์ž๋“ค์˜ ๋‚˜์—ด์ด 256์˜ ๊ธธ์ด๋กœ ์žˆ๋‹ค๊ณ  ์ƒ์ƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ํ•˜๋‚˜ = ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 256์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด๋Ÿฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 3000๊ฐœ๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ํ˜„์žฌ ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋Š” 3,000 ร— 256์ž…๋‹ˆ๋‹ค. ํ–‰๋ ฌ์ด๋‹ˆ๊นŒ 2D ํ…์„œ๋„ค์š”. 3,000๊ฐœ๋ฅผ 1๊ฐœ์”ฉ ๊บผ๋‚ด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ปดํ“จํ„ฐ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ณดํ†ต ๋ฉ์–ด๋ฆฌ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. 3,000๊ฐœ์—์„œ 64๊ฐœ์”ฉ ๊บผ๋‚ด์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด ์ด๋•Œ batch size๋ฅผ 64๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ปดํ“จํ„ฐ๊ฐ€ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋Š” 2D ํ…์„œ์˜ ํฌ๊ธฐ๋Š” (batch size ร— dim) = 64 ร— 256์ž…๋‹ˆ๋‹ค. ํ…์„œ์˜ ํฌ๊ธฐ(shape)๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ,(์ฝค๋งˆ)๋ฅผ ์“ฐ๊ธฐ๋„ ํ•˜๊ณ  ร—(๊ณฑํ•˜๊ธฐ)๋ฅผ ์“ฐ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 2ํ–‰ 3์—ด์˜ 2D ํ…์„œ๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ (2, 3)๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•˜๊ณ  (2 ร— 3)์ด๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. (5, )์˜<NAME>์€ (1 ร— 5)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ต‰์žฅํžˆ ๋งŽ์„ ๋•Œ, ์ปดํ“จํ„ฐ๊ฐ€ ํ•œ ๋ฒˆ์— ๋“ค๊ณ  ๊ฐ€์„œ ์ฒ˜๋ฆฌํ•  ์–‘์„ ๋ฐฐ์น˜ ํฌ๊ธฐ(batch size)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์Šฌ๋ผ์ด์‹ฑ์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ์˜ˆ์ œ : https://wikidocs.net/13 Pytorch ํ…์„œ์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์˜ˆ์ œ : https://datascienceschool.net/view-notebook/4f3606fd839f4320a4120a56eec1e228/ 02-03 ํ…์„œ ์กฐ์ž‘ํ•˜๊ธฐ(Tensor Manipulation) 2 ์ด์–ด์„œ ํ…์„œ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4) ๋ทฐ(View) - ์›์†Œ์˜ ์ˆ˜๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ํ…์„œ์˜ ํฌ๊ธฐ ๋ณ€๊ฒฝ. ๋งค์šฐ ์ค‘์š”! ํŒŒ์ด ํ† ์น˜ ํ…์„œ์˜ ๋ทฐ(View)๋Š” ๋„˜ํŒŒ์ด์—์„œ์˜ ๋ฆฌ์‰์ดํ”„(Reshape)์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. Reshape๋ผ๋Š” ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด, ํ…์„œ์˜ ํฌ๊ธฐ(Shape)๋ฅผ ๋ณ€๊ฒฝํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์Šต์„ ์œ„ํ•ด ์šฐ์„  ์ž„์˜๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด 3์ฐจ์› ํ…์„œ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. t = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]) ft = torch.FloatTensor(t) ft๋ผ๋Š” ์ด๋ฆ„์˜ 3์ฐจ์› ํ…์„œ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํฌ๊ธฐ(shape)๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(ft.shape) torch.Size([2, 2, 3]) ํ˜„์žฌ ์œ„ ํ…์„œ์˜ ํฌ๊ธฐ๋Š” (2, 2, 3)์ž…๋‹ˆ๋‹ค. ์ด ft๋ผ๋Š” ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ๊ธฐ์–ตํ•ด ์ฃผ์„ธ์š”. ์ด ํ…์„œ๋ฅผ ๊ฐ€์ง€๊ณ  ๋‘ ๋ฒˆ์˜ ์‹ค์Šต์„ ํ•  ๊ฒ๋‹ˆ๋‹ค. 4-1) 3์ฐจ์› ํ…์„œ์—์„œ 2์ฐจ์› ํ…์„œ๋กœ ๋ณ€๊ฒฝ ์ด์ œ ft ํ…์„œ๋ฅผ view๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํฌ๊ธฐ(shape)๋ฅผ 2์ฐจ์› ํ…์„œ๋กœ ๋ณ€๊ฒฝํ•ด ๋ด…์‹œ๋‹ค. print(ft.view([-1, 3])) # ft๋ผ๋Š” ํ…์„œ๋ฅผ (?, 3)์˜ ํฌ๊ธฐ๋กœ ๋ณ€๊ฒฝ print(ft.view([-1, 3]).shape) tensor([[ 0., 1., 2.], [ 3., 4., 5.], [ 6., 7., 8.], [ 9., 10., 11.]]) torch.Size([4, 3]) view([-1, 3])์ด ๊ฐ€์ง€๋Š” ์˜๋ฏธ๋Š” ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. -1์€ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์œผ๋‹ˆ ํŒŒ์ด ํ† ์น˜์— ๋งก๊ธฐ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๊ณ , 3์€ ๋‘ ๋ฒˆ์งธ ์ฐจ์›์˜ ๊ธธ์ด๋Š” 3์„ ๊ฐ€์ง€๋„๋ก ํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ˜„์žฌ 3์ฐจ์› ํ…์„œ๋ฅผ 2์ฐจ์› ํ…์„œ๋กœ ๋ณ€๊ฒฝํ•˜๋˜ (?, 3)์˜ ํฌ๊ธฐ๋กœ ๋ณ€๊ฒฝํ•˜๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ (4, 3)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํ…์„œ๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ ํฌ๊ธฐ ๋ณ€ํ™˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ฃจ์–ด์กŒ์Šต๋‹ˆ๋‹ค. (2, 2, 3) -> (2 ร— 2, 3) -> (4, 3) ๊ทœ์น™์„ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. view๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณ€๊ฒฝ ์ „๊ณผ ๋ณ€๊ฒฝ ํ›„์˜ ํ…์„œ ์•ˆ์˜ ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์œ ์ง€๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜์˜ view๋Š” ์‚ฌ์ด์ฆˆ๊ฐ€ -1๋กœ ์„ค์ •๋˜๋ฉด ๋‹ค๋ฅธ ์ฐจ์›์œผ๋กœ๋ถ€ํ„ฐ ํ•ด๋‹น ๊ฐ’์„ ์œ ์ถ”ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ์ „ ํ…์„œ์˜ ์›์†Œ์˜ ์ˆ˜๋Š” (2 ร— 2 ร— 3) = 12๊ฐœ์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ณ€๊ฒฝ ํ›„ ํ…์„œ์˜ ์›์†Œ์˜ ๊ฐœ์ˆ˜ ๋˜ํ•œ (4 ร— 3) = 12๊ฐœ์˜€์Šต๋‹ˆ๋‹ค. 4-2) 3์ฐจ์› ํ…์„œ์˜ ํฌ๊ธฐ ๋ณ€๊ฒฝ ์ด๋ฒˆ์—๋Š” 3์ฐจ์› ํ…์„œ์—์„œ 3์ฐจ์› ํ…์„œ๋กœ ์ฐจ์›์€ ์œ ์ง€ํ•˜๋˜, ํฌ๊ธฐ(shape)๋ฅผ ๋ฐ”๊พธ๋Š” ์ž‘์—…์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. view๋กœ ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋”๋ผ๋„ ์›์†Œ์˜ ์ˆ˜๋Š” ์œ ์ง€๋˜์–ด์•ผ ํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด (2 ร— 2 ร— 3) ํ…์„œ๋ฅผ (? ร— 1 ร— 3) ํ…์„œ๋กœ ๋ณ€๊ฒฝํ•˜๋ผ๊ณ  ํ•˜๋ฉด?๋Š” ๋ช‡ ์ฐจ์›์ธ๊ฐ€์š”? (2 ร— 2 ร— 3) = (? ร— 1 ร— 3) = 12๋ฅผ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋ฏ€๋กœ?๋Š” 4๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹ค์Šต์œผ๋กœ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(ft.view([-1, 1, 3])) print(ft.view([-1, 1, 3]).shape) tensor([[[ 0., 1., 2.]], [[ 3., 4., 5.]], [[ 6., 7., 8.]], [[ 9., 10., 11.]]]) torch.Size([4, 1, 3]) 5) ์Šคํ€ด์ฆˆ(Squeeze) - 1์ธ ์ฐจ์›์„ ์ œ๊ฑฐํ•œ๋‹ค. ์Šคํ€ด์ฆˆ๋Š” ์ฐจ์›์ด 1์ธ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์ฐจ์›์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์‹ค์Šต์„ ์œ„ํ•ด ์ž„์˜๋กœ (3 ร— 1)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 2์ฐจ์› ํ…์„œ๋ฅผ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค. ft = torch.FloatTensor([[0], [1], [2]]) print(ft) print(ft.shape) tensor([[0.], [1.], [2.]]) torch.Size([3, 1]) ํ•ด๋‹น ํ…์„œ๋Š” (3 ร— 1)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ฐจ์›์ด 1์ด๋ฏ€๋กœ squeeze๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด (3, )์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํ…์„œ๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. print(ft.squeeze()) print(ft.squeeze().shape) tensor([0., 1., 2.]) torch.Size([3]) ์œ„์˜ ๊ฒฐ๊ณผ๋Š” 1์ด์—ˆ๋˜ ๋‘ ๋ฒˆ์งธ ์ฐจ์›์ด ์ œ๊ฑฐ๋˜๋ฉด์„œ (3, )์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํ…์„œ๋กœ ๋ณ€๊ฒฝ๋˜์–ด 1์ฐจ์› ๋ฒกํ„ฐ๊ฐ€ ๋œ ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 6) ์–ธ์Šค ํ€ด์ฆˆ(Unsqueeze) - ํŠน์ • ์œ„์น˜์— 1์ธ ์ฐจ์›์„ ์ถ”๊ฐ€ํ•œ๋‹ค. ์–ธ์Šค ํ€ด์ฆˆ๋Š” ์Šคํ€ด์ฆˆ์™€ ์ •๋ฐ˜๋Œ€์ž…๋‹ˆ๋‹ค. ํŠน์ • ์œ„์น˜์— 1์ธ ์ฐจ์›์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์Šต์„ ์œ„ํ•ด ์ž„์˜๋กœ (3, )์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 1์ธ ์ฐจ์› ํ…์„œ๋ฅผ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค. ft = torch.Tensor([0, 1, 2]) print(ft.shape) torch.Size([3]) ํ˜„์žฌ๋Š” ์ฐจ์›์ด 1๊ฐœ์ธ 1์ฐจ์› ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์— 1์ธ ์ฐจ์›์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์˜ ์ธ๋ฑ์Šค๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ˆซ์ž 0์„ ์ธ์ž๋กœ ๋„ฃ์œผ๋ฉด ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์— 1์ธ ์ฐจ์›์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. print(ft.unsqueeze(0)) # ์ธ๋ฑ์Šค๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ 0์€ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์„ ์˜๋ฏธํ•œ๋‹ค. print(ft.unsqueeze(0).shape) tensor([[0., 1., 2.]]) torch.Size([1, 3]) ์œ„ ๊ฒฐ๊ณผ๋Š” (3, )์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์กŒ๋˜ 1์ฐจ์› ๋ฒกํ„ฐ๊ฐ€ (1, 3)์˜ 2์ฐจ์› ํ…์„œ๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฐฉ๊ธˆ ํ•œ ์—ฐ์‚ฐ์„ ์•ž์„œ ๋ฐฐ์šด view๋กœ๋„ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 2์ฐจ์›์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ์œผ๋ฉด์„œ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์€ 1์ด๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋ฉด view์—์„œ (1, -1)์„ ์ธ์ž๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. print(ft.view(1, -1)) print(ft.view(1, -1).shape) tensor([[0., 1., 2.]]) torch.Size([1, 3]) ์œ„์˜ ๊ฒฐ๊ณผ๋Š” unsqueeze์™€ view๊ฐ€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“  ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” unsqueeze์˜ ์ธ์ž๋กœ 1์„ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ ์ด๋Š” ๋‘ ๋ฒˆ์งธ ์ฐจ์›์— 1์„ ์ถ”๊ฐ€ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ํฌ๊ธฐ๋Š” (3, )์ด์—ˆ์œผ๋ฏ€๋กœ ๋‘ ๋ฒˆ์งธ ์ฐจ์›์— 1์ธ ์ฐจ์›์„ ์ถ”๊ฐ€ํ•˜๋ฉด (3, 1)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(ft.unsqueeze(1)) print(ft.unsqueeze(1).shape) tensor([[0.], [1.], [2.]]) torch.Size([3, 1]) ์ด๋ฒˆ์—๋Š” unsqueeze์˜ ์ธ์ž๋กœ -1์„ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. -1์€ ์ธ๋ฑ์Šค ์ƒ์œผ๋กœ ๋งˆ์ง€๋ง‰ ์ฐจ์›์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ํฌ๊ธฐ๋Š” (3, )์ด์—ˆ์œผ๋ฏ€๋กœ ๋งˆ์ง€๋ง‰ ์ฐจ์›์— 1์ธ ์ฐจ์›์„ ์ถ”๊ฐ€ํ•˜๋ฉด (3, 1)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ˜„์žฌ ํ…์„œ์˜ ๊ฒฝ์šฐ์—๋Š” 1์„ ๋„ฃ์€ ๊ฒฝ์šฐ์™€ -1์„ ๋„ฃ์€ ๊ฒฝ์šฐ๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(ft.unsqueeze(-1)) print(ft.unsqueeze(-1).shape) tensor([[0.], [1.], [2.]]) torch.Size([3, 1]) ๋งจ ๋’ค์— 1์ธ ์ฐจ์›์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ 1์ฐจ์› ๋ฒกํ„ฐ๊ฐ€ (3, 1)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 2์ฐจ์› ํ…์„œ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. view(), squeeze(), unsqueeze()๋Š” ํ…์„œ์˜ ์›์†Œ ์ˆ˜๋ฅผ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ชจ์–‘๊ณผ ์ฐจ์›์„ ์กฐ์ ˆํ•ฉ๋‹ˆ๋‹ค. 7) ํƒ€์ž… ์บ์ŠคํŒ…(Type Casting) ํ…์„œ์—๋Š” ์ž๋ฃŒํ˜•์ด๋ผ๋Š” ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฐ์ดํ„ฐํ˜• ๋ณ„๋กœ ์ •์˜๋ผ ์žˆ๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด 32๋น„ํŠธ์˜ ๋ถ€๋™ ์†Œ์ˆ˜์ ์€ torch.FloatTensor๋ฅผ, 64๋น„ํŠธ์˜ ๋ถ€ํ˜ธ ์žˆ๋Š” ์ •์ˆ˜๋Š” torch.LongTensor๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. GPU ์—ฐ์‚ฐ์„ ์œ„ํ•œ ์ž๋ฃŒํ˜•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด torch.cuda.FloatTensor๊ฐ€ ๊ทธ ์˜ˆ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์ž๋ฃŒํ˜•์„ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ํƒ€์ž… ์บ์ŠคํŒ…์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์‹ค์Šต์„ ์œ„ํ•ด long ํƒ€์ž…์˜ lt๋ผ๋Š” ํ…์„œ๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. lt = torch.LongTensor([1, 2, 3, 4]) print(lt) ํ…์„œ์—๋‹ค๊ฐ€. float()๋ฅผ ๋ถ™์ด๋ฉด ๋ฐ”๋กœ floatํ˜•์œผ๋กœ ํƒ€์ž…์ด ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. print(lt.float()) tensor([1., 2., 3., 4.]) ์ด๋ฒˆ์—๋Š” Byte ํƒ€์ž…์˜ bt๋ผ๋Š” ํ…์„œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. bt = torch.ByteTensor([True, False, False, True]) print(bt) tensor([1, 0, 0, 1], dtype=torch.uint8) ์—ฌ๊ธฐ์—. long()์ด๋ผ๊ณ  ํ•˜๋ฉด long ํƒ€์ž…์˜ ํ…์„œ๋กœ ๋ณ€๊ฒฝ๋˜๊ณ . float()์ด๋ผ๊ณ  ํ•˜๋ฉด float ํƒ€์ž…์˜ ํ…์„œ๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. print(bt.long()) print(bt.float()) tensor([1, 0, 0, 1]) tensor([1., 0., 0., 1.]) 8) ์—ฐ๊ฒฐํ•˜๊ธฐ(concatenate) ์ด๋ฒˆ์—๋Š” ๋‘ ํ…์„œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  (2 ร— 2) ํฌ๊ธฐ์˜ ํ…์„œ๋ฅผ ๋‘ ๊ฐœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. x = torch.FloatTensor([[1, 2], [3, 4]]) y = torch.FloatTensor([[5, 6], [7, 8]]) ์ด์ œ ๋‘ ํ…์„œ๋ฅผ torch.cat([ ])๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฐ๊ฒฐ ๋ฐฉ๋ฒ•์€ ํ•œ ๊ฐ€์ง€๋งŒ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. torch.cat์€ ์–ด๋Š ์ฐจ์›์„ ๋Š˜๋ฆด ๊ฒƒ์ธ์ง€๋ฅผ ์ธ์ž๋กœ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด dim=0์€ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์„ ๋Š˜๋ฆฌ๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. print(torch.cat([x, y], dim=0)) tensor([[1., 2.], [3., 4.], [5., 6.], [7., 8.]]) dim=0์„ ์ธ์ž๋กœ ํ–ˆ๋”๋‹ˆ ๋‘ ๊ฐœ์˜ (2 ร— 2) ํ…์„œ๊ฐ€ (4 ร— 2) ํ…์„œ๊ฐ€ ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” dim=1์„ ์ธ์ž๋กœ ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. print(torch.cat([x, y], dim=1)) tensor([[1., 2., 5., 6.], [3., 4., 7., 8.]]) dim=1์„ ์ธ์ž๋กœ ํ–ˆ๋”๋‹ˆ ๋‘ ๊ฐœ์˜ (2 ร— 2) ํ…์„œ๊ฐ€ (2 ร— 4) ํ…์„œ๊ฐ€ ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฅ ๋Ÿฌ๋‹์—์„œ๋Š” ์ฃผ๋กœ ๋ชจ๋ธ์˜ ์ž…๋ ฅ ๋˜๋Š” ์ค‘๊ฐ„ ์—ฐ์‚ฐ์—์„œ ๋‘ ๊ฐœ์˜ ํ…์„œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๋‘ ํ…์„œ๋ฅผ ์—ฐ๊ฒฐํ•ด์„œ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋‘ ๊ฐ€์ง€์˜ ์ •๋ณด๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 9) ์Šคํƒœํ‚น(Stacking) ์—ฐ๊ฒฐ(concatenate)์„ ํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์Šคํƒœํ‚น(Stacking)์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํƒœํ‚น์€ ์˜์–ด๋กœ ์Œ“๋Š”๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” ์—ฐ๊ฒฐ์„ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์Šคํƒœํ‚น์ด ๋” ํŽธ๋ฆฌํ•  ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด๋Š” ์Šคํƒœํ‚น์ด ๋งŽ์€ ์—ฐ์‚ฐ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์‹ค์Šต์„ ์œ„ํ•ด ํฌ๊ธฐ๊ฐ€ (2, )๋กœ ๋ชจ๋‘ ๋™์ผํ•œ 3๊ฐœ์˜ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. x = torch.FloatTensor([1, 4]) y = torch.FloatTensor([2, 5]) z = torch.FloatTensor([3, 6]) ์ด์ œ torch.stack์„ ํ†ตํ•ด์„œ 3๊ฐœ์˜ ๋ฒกํ„ฐ๋ฅผ ๋ชจ๋‘ ์Šคํƒํ‚นํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(torch.stack([x, y, z])) tensor([[1., 4.], [2., 5.], [3., 6.]]) ์œ„ ๊ฒฐ๊ณผ๋Š” 3๊ฐœ์˜ ๋ฒกํ„ฐ๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์Œ“์—ฌ (3 ร— 2) ํ…์„œ๊ฐ€ ๋œ ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์Šคํƒœํ‚น์€ ์‚ฌ์‹ค ๋งŽ์€ ์—ฐ์‚ฐ์„ ํ•œ ๋ฒˆ์— ์ถ•์•ฝํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ์ž‘์—…์€ ์•„๋ž˜์˜ ์ฝ”๋“œ์™€ ๋™์ผํ•œ ์ž‘์—…์ž…๋‹ˆ๋‹ค. print(torch.cat([x.unsqueeze(0), y.unsqueeze(0), z.unsqueeze(0)], dim=0)) x, y, z๋Š” ๊ธฐ์กด์—๋Š” ์ „๋ถ€ (2, )์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์กŒ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ. unsqueeze(0)์„ ํ•จ์œผ๋กœ์จ 3๊ฐœ์˜ ๋ฒกํ„ฐ๋Š” ์ „๋ถ€ (1, 2)์˜ ํฌ๊ธฐ์˜ 2์ฐจ์› ํ…์„œ๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์—ฐ๊ฒฐ(concatenate)๋ฅผ ์˜๋ฏธํ•˜๋Š” cat์„ ์‚ฌ์šฉํ•˜๋ฉด (3 x 2) ํ…์„œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. tensor([[1., 4.], [2., 5.], [3., 6.]]) ์œ„์—์„œ๋Š” torch.stack([x, y, z])๋ผ๋Š” ํ•œ ๋ฒˆ์˜ ์ปค๋งจ๋“œ๋กœ ์ˆ˜ํ–‰ํ–ˆ์ง€๋งŒ, ์—ฐ๊ฒฐ(concatenate)๋กœ ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ–ˆ๋”๋‹ˆ ๊ฝค ๋ณต์žกํ•ด์กŒ์Šต๋‹ˆ๋‹ค. ์Šคํƒœํ‚น์— ์ถ”๊ฐ€์ ์œผ๋กœ dim์„ ์ธ์ž๋กœ ์ค„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” dim=์ผ์ธ์ž๋ฅผ ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋‘ ๋ฒˆ์งธ ์ฐจ์›์ด ์ฆ๊ฐ€ํ•˜๋„๋ก ์Œ“์œผ๋ผ๋Š” ์˜๋ฏธ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(torch.stack([x, y, z], dim=1)) tensor([[1., 2., 3.], [4., 5., 6.]]) ์œ„์˜ ๊ฒฐ๊ณผ๋Š” ๋‘ ๋ฒˆ์งธ ์ฐจ์›์ด ์ฆ๊ฐ€ํ•˜๋„๋ก ์Šคํƒœํ‚น์ด ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ (2 ร— 3) ํ…์„œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 10) ones_like์™€ zeros_like - 0์œผ๋กœ ์ฑ„์›Œ์ง„ ํ…์„œ์™€ 1๋กœ ์ฑ„์›Œ์ง„ ํ…์„œ ์‹ค์Šต์„ ์œ„ํ•ด (2 ร— 3) ํ…์„œ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. x = torch.FloatTensor([[0, 1, 2], [2, 1, 0]]) print(x) tensor([[0., 1., 2.], [2., 1., 0.]]) ์œ„ ํ…์„œ์— ones_like๋ฅผ ํ•˜๋ฉด ๋™์ผํ•œ ํฌ๊ธฐ(shape) ์ง€๋งŒ 1์œผ๋กœ๋งŒ ๊ฐ’์ด ์ฑ„์›Œ์ง„ ํ…์„œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. print(torch.ones_like(x)) # ์ž…๋ ฅ ํ…์„œ์™€ ํฌ๊ธฐ๋ฅผ ๋™์ผํ•˜๊ฒŒ ํ•˜๋ฉด์„œ ๊ฐ’์„ 1๋กœ ์ฑ„์šฐ๊ธฐ tensor([[1., 1., 1.], [1., 1., 1.]]) ์œ„ ํ…์„œ์— zeros_like๋ฅผ ํ•˜๋ฉด ๋™์ผํ•œ ํฌ๊ธฐ(shape) ์ง€๋งŒ 0์œผ๋กœ๋งŒ ๊ฐ’์ด ์ฑ„์›Œ์ง„ ํ…์„œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. print(torch.zeros_like(x)) # ์ž…๋ ฅ ํ…์„œ์™€ ํฌ๊ธฐ๋ฅผ ๋™์ผํ•˜๊ฒŒ ํ•˜๋ฉด์„œ ๊ฐ’์„ 0์œผ๋กœ ์ฑ„์šฐ๊ธฐ tensor([[0., 0., 0.], [0., 0., 0.]]) 11) In-place Operation (๋ฎ์–ด์“ฐ๊ธฐ ์—ฐ์‚ฐ) ์‹ค์Šต์„ ์œ„ํ•ด (2 ร— 2) ํ…์„œ๋ฅผ ๋งŒ๋“ค๊ณ  x์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. x = torch.FloatTensor([[1, 2], [3, 4]]) ๊ณฑํ•˜๊ธฐ ์—ฐ์‚ฐ์„ ํ•œ ๊ฐ’๊ณผ ๊ธฐ์กด์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(x.mul(2.)) # ๊ณฑํ•˜๊ธฐ 2๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ print(x) # ๊ธฐ์กด์˜ ๊ฐ’ ์ถœ๋ ฅ tensor([[2., 4.], [6., 8.]]) tensor([[1., 2.], [3., 4.]]) ์ฒซ ๋ฒˆ์งธ ์ถœ๋ ฅ์€ ๊ณฑํ•˜๊ธฐ 2๊ฐ€ ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ , ๋‘ ๋ฒˆ์งธ ์ถœ๋ ฅ์€ ๊ธฐ์กด์˜ ๊ฐ’์ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณฑํ•˜๊ธฐ 2๋ฅผ ์ˆ˜ํ–‰ํ–ˆ์ง€๋งŒ ์ด๋ฅผ x์—๋‹ค๊ฐ€ ๋‹ค์‹œ ์ €์žฅํ•˜์ง€ ์•Š์•˜์œผ๋‹ˆ, ๊ณฑํ•˜๊ธฐ ์—ฐ์‚ฐ์„ ํ•˜๋”๋ผ๋„ ๊ธฐ์กด์˜ ๊ฐ’ x๋Š” ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ๋‹น์—ฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฐ์‚ฐ ๋’ค์— _๋ฅผ ๋ถ™์ด๋ฉด ๊ธฐ์กด์˜ ๊ฐ’์„ ๋ฎ์–ด์“ฐ๊ธฐ ํ•ฉ๋‹ˆ๋‹ค. print(x.mul_(2.)) # ๊ณฑํ•˜๊ธฐ 2๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜ x์— ๊ฐ’์„ ์ €์žฅํ•˜๋ฉด์„œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ print(x) # ๊ธฐ์กด์˜ ๊ฐ’ ์ถœ๋ ฅ tensor([[2., 4.], [6., 8.]]) tensor([[2., 4.], [6., 8.]]) ์ด๋ฒˆ์—๋Š” x์˜ ๊ฐ’์ด ๋ฎ์–ด์“ฐ๊ธฐ ๋˜์–ด 2 ๊ณฑํ•˜๊ธฐ ์—ฐ์‚ฐ์ด ๋œ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. 02-04 ํŒŒ์ด์ฌ ํด๋ž˜์Šค(class) ๋Œ€๋ถ€๋ถ„์˜ ํŒŒ์ด ํ† ์น˜์˜ ๊ตฌํ˜„์ฒด๋“ค์„ ๋ณด๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ํด๋ž˜์Šค(Class)๋ผ๋Š” ๊ฐœ๋…์„ ์• ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ ํ”„ ํˆฌ ํŒŒ์ด์ฌ์˜ ํด๋ž˜์Šค ์ฑ•ํ„ฐ๋ฅผ ์ธ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://wikidocs.net/28 1. ํ•จ์ˆ˜(function)๊ณผ ํด๋ž˜์Šค(Class)์˜ ์ฐจ์ด ์šฐ์„  ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค์˜ ์ฐจ์ด๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ง์…ˆ์„ ์ง€์†์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ๋ฅผ ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค๋กœ ๊ฐ๊ฐ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํ•จ์ˆ˜(function)๋กœ ๋ง์…ˆ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ ์šฐ์„  add ํ•จ์ˆ˜๋ฅผ ํŒŒ์ด์ฌ์œผ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. result๋ผ๋Š” ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. result = 0 ๊ทธ๋ฆฌ๊ณ  add๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. add๋ผ๋Š” ํ•จ์ˆ˜์—์„œ๋Š” ๊ธฐ์กด์˜ result์— ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์˜จ ์ˆซ์ž๋ฅผ ๋”ํ•˜๊ณ  ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. def add(num): global result result += num return result ํ•จ์ˆ˜๋ฅผ ๋‘ ๋ฒˆ ์‹คํ–‰์‹œํ‚ค๋Š”๋ฐ ์ฒ˜์Œ์—๋Š” 3์„ ๋„ฃ๊ณ , ๋‘ ๋ฒˆ์งธ์—๋Š” 4๋ฅผ ๋„ฃ์Šต๋‹ˆ๋‹ค. print(add(3)) print(add(4)) 7 ์ฒ˜์Œ์—๋Š” result๊ฐ€ 0์ด์—ˆ๋‹ค๊ฐ€ 3์ด ๋”ํ•ด์ง€๋ฉด์„œ 3์ด ์ถœ๋ ฅ๋˜๊ณ , ์ถ”๊ฐ€๋กœ 4๋ฅผ ์ž…๋ ฅํ•˜๋ฉด result์˜ ๊ฐ’์ด ์ด๋ฏธ ์•ž์„œ 3์œผ๋กœ ๊ฐฑ์‹ ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— 3+4์˜ ๊ฒฐ๊ณผ๋กœ 7์ด ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค. 2. ํ•จ์ˆ˜(function)๋กœ ๋‘ ๊ฐœ์˜ ๋ง์…ˆ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ๋…๋ฆฝ์ ์ธ ๋‘ ๊ฐœ์˜ ๋ง์…ˆ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๋น„์œ ํ•˜๋ฉด, ์ฑ…์ƒ์— ๋‘ ๊ฐœ์˜ ๊ณ„์‚ฐ๊ธฐ๋ฅผ ๋‘๊ณ  ์„œ๋กœ ๋‹ค๋ฅธ ์—ฐ์‚ฐ์„ ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๊ณ„์‚ฐ๊ธฐ๋กœ๋Š” 3+7์„ ํ•˜๊ณ  ์žˆ๊ณ , ๋‘ ๋ฒˆ์งธ ๊ณ„์‚ฐ๊ธฐ๋กœ๋Š” 3+10์„ ํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋‘ ๊ณ„์‚ฐ๊ธฐ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๊ณ„์‚ฐ๊ธฐ์ด๋ฏ€๋กœ ๋…๋ฆฝ์ ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด, ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋Š” 1๊ฐœ์˜ ๋ง์…ˆ๊ธฐ๋งŒ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ ์ด ๊ฒฝ์šฐ์—๋Š” ๋‘ ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ๋…๋ฆฝ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. result1 = 0 result2 = 0 def add1(num): global result1 result1 += num return result1 def add2(num): global result2 result2 += num return result2 print(add1(3)) print(add1(4)) print(add2(3)) print(add2(7)) 7 10 ์„œ๋กœ์˜ ๊ฐ’์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ณ  ์„œ๋กœ ๋‹ค๋ฅธ ์—ฐ์‚ฐ์„ ํ•˜๊ณ  ์žˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋Ÿฐ ๋‘ ๊ฐœ์˜ ๋ง์…ˆ๊ธฐ๋ฅผ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? 3. ํด๋ž˜์Šค(class)๋กœ ๋ง์…ˆ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. class Calculator: def __init__(self): # ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ํ˜ธ์ถœ๋  ๋•Œ ์‹คํ–‰๋˜๋Š” ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜. ์ด๋ฅผ ์ƒ์„ฑ์ž๋ผ๊ณ  ํ•œ๋‹ค. self.result = 0 def add(self, num): # ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜. self.result += num return self.result ํด๋ž˜์Šค๋Š” ๋งˆ์น˜ ๋ถ•์–ด๋นต ํ‹€๊ณผ ๊ฐ™์•„์„œ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ ํ›„์—๋Š” ์ด๊ฑธ๋กœ ๊ฐ์ฒด๋ผ๋Š” ๊ฒƒ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. cal1์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ถ•์–ด๋นต ํ‹€๋กœ ํ•˜๋‚˜์˜ ๋ถ•์–ด๋นต์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์— ๋น„์œ ๋ฉ๋‹ˆ๋‹ค. cal1 = Calculator() ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ฐฉ๋ฒ•์€ ๊ฐ์ฒด์˜ ์ด๋ฆ„์„ ์ •ํ•œ ๋’ค์— '=ํด๋ž˜์Šค ์ด๋ฆ„()'์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค๊ฐ€ ๋ถ•์–ด๋นต ํ‹€๊ณผ ๊ฐ™๋‹ค๋Š” ์ด์œ ๋Š” ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“  ํ›„์—๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ ํ•˜๋‚˜์˜ ๊ฐ์ฒด cal2๋„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. cal2 = Calculator() ๋‘ ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋‘ ๊ฐœ์˜ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ ๋™์‹œ์— ๋…๋ฆฝ์ ์ธ ๋ง์…ˆ ์—ฐ์‚ฐ์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(cal1.add(3)) print(cal1.add(4)) print(cal2.add(3)) print(cal2.add(7)) 7 10 ๋‘ ๊ฐœ์˜ ๊ฐ์ฒด๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ์—ฐ์‚ฐ๋˜๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ๋…๋ฆฝ์ ์ธ ๋‘ ๊ฐœ์˜ ๋ง์…ˆ๊ธฐ๋ฅผ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค๋ฉด ํ•จ์ˆ˜๋ฅผ ๋‘ ๊ฐœ ๋งŒ๋“ค์–ด์•ผ ํ–ˆ์ง€๋งŒ, ํด๋ž˜์Šค๋ผ๋Š” ๊ฒƒ์„ ํ•˜๋‚˜ ์„ ์–ธํ•˜๊ณ , ์ด ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๋ณ„๋„์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ž ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์กŒ์Šต๋‹ˆ๋‹ค. 03. [ML ์ž…๋ฌธ โœ] - ๋จธ์‹  ๋Ÿฌ๋‹ ์ž…๋ฌธํ•˜๊ธฐ(Machine Learning Basics) 4์ฑ•ํ„ฐ์—์„œ๋Š” ์„ ํ˜• ํšŒ๊ท€์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 03-01 ์„ ํ˜• ํšŒ๊ท€(Linear Regression) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์„ ํ˜• ํšŒ๊ท€ ์ด๋ก ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜๊ณ , ํŒŒ์ด ํ† ์น˜(PyTorch)๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด(Data Definition) ํ•™์Šตํ•  ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ๊ฐ€์„ค(Hypothesis) ์ˆ˜๋ฆฝ ๊ฐ€์„ค์„ ์ˆ˜๋ฆฝํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ์†์‹ค ๊ณ„์‚ฐํ•˜๊ธฐ(Compute loss) ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ์—ฐ์†์ ์œผ๋กœ ๋ชจ๋ธ์„ ๊ฐœ์„ ํ•˜๋Š”๋ฐ ์ด๋•Œ ์†์‹ค(loss)๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Gradient Descent) ํ•™์Šต์„ ์œ„ํ•œ ํ•ต์‹ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Gradient Descent)์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด(Data Definition) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์˜ˆ์ œ๋Š” ๊ณต๋ถ€ํ•œ ์‹œ๊ฐ„๊ณผ ์ ์ˆ˜์— ๋Œ€ํ•œ ์ƒ๊ด€๊ด€๊ณ„์ž…๋‹ˆ๋‹ค. 1. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์…‹๊ณผ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์…‹ ์–ด๋–ค ํ•™์ƒ์ด 1์‹œ๊ฐ„ ๊ณต๋ถ€๋ฅผ ํ–ˆ๋”๋‹ˆ 2์ , ๋‹ค๋ฅธ ํ•™์ƒ์ด 2์‹œ๊ฐ„ ๊ณต๋ถ€๋ฅผ ํ–ˆ๋”๋‹ˆ 4์ , ๋˜ ๋‹ค๋ฅธ ํ•™์ƒ์ด 3์‹œ๊ฐ„์„ ๊ณต๋ถ€ํ–ˆ๋”๋‹ˆ 6์ ์„ ๋งž์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ๋‚ด๊ฐ€ 4์‹œ๊ฐ„์„ ๊ณต๋ถ€ํ•œ๋‹ค๋ฉด ๋ช‡ ์ ์„ ๋งž์„ ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ด ์งˆ๋ฌธ์— ๋Œ€๋‹ตํ•˜๊ธฐ ์œ„ํ•ด์„œ 1์‹œ๊ฐ„, 2์‹œ๊ฐ„, 3์‹œ๊ฐ„์„ ๊ณต๋ถ€ํ–ˆ์„ ๋•Œ ๊ฐ๊ฐ 2์ , 4์ , 6์ ์ด ๋‚˜์™”๋‹ค๋Š” ์•ž์„œ ๋‚˜์˜จ ์ •๋ณด๋ฅผ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์˜ˆ์ธก์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์…‹(training dataset)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต์ด ๋๋‚œ ํ›„, ์ด ๋ชจ๋ธ์ด ์–ผ๋งˆ๋‚˜ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ํŒ๋ณ„ํ•˜๋Š” ๋ฐ์ดํ„ฐ ์…‹์„ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์…‹(test dataset)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ตฌ์„ฑ ์•ž์„œ ํ…์„œ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ๋Š”๋ฐ, ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋Š” ํŒŒ์ด ํ† ์น˜์˜ ํ…์„œ์˜ ํ˜•ํƒœ(torch.tensor)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ํ…์„œ์— ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋ณดํŽธ์ ์œผ๋กœ ์ž…๋ ฅ์€ x, ์ถœ๋ ฅ์€ y๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ x_train์€ ๊ณต๋ถ€ํ•œ ์‹œ๊ฐ„, y_train์€ ๊ทธ์— ๋งคํ•‘๋˜๋Š” ์ ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. x_train = torch.FloatTensor([[1], [2], [3]]) y_train = torch.FloatTensor([[2], [4], [6]]) ์ด์ œ ๋ชจ๋ธ์˜ ๊ฐ€์„ค์„ ์„ธ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ๊ฐ€์„ค(Hypothesis) ์ˆ˜๋ฆฝ ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ์‹์„ ์„ธ์šธ ๋•Œ ์ด ์‹์„ ๊ฐ€์„ค(Hypothesis)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ๊ฐ€์„ค์€ ์ž„์˜๋กœ ์ถ”์ธกํ•ด์„œ ์„ธ์›Œ๋ณด๋Š” ์‹์ผ ์ˆ˜๋„ ์žˆ๊ณ , ๊ฒฝํ—˜์ ์œผ๋กœ ์•Œ๊ณ  ์žˆ๋Š” ์‹์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งž๋Š” ๊ฐ€์„ค์ด ์•„๋‹ˆ๋ผ๊ณ  ํŒ๋‹จ๋˜๋ฉด ๊ณ„์† ์ˆ˜์ •ํ•ด๋‚˜๊ฐ€๊ฒŒ ๋˜๋Š” ์‹์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์„ ํ˜• ํšŒ๊ท€์˜ ๊ฐ€์„ค์€ ์ด๋ฏธ ๋„๋ฆฌ ์•Œ๋ ค์ ธ ์žˆ์œผ๋ฏ€๋กœ ๊ณ ๋ฏผํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์„ ํ˜• ํšŒ๊ท€๋ž€ ํ•™์Šต ๋ฐ์ดํ„ฐ์™€ ๊ฐ€์žฅ ์ž˜ ๋งž๋Š” ํ•˜๋‚˜์˜ ์ง์„ ์„ ์ฐพ๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ์„ ํ˜• ํšŒ๊ท€์˜ ๊ฐ€์„ค(์ง์„ ์˜ ๋ฐฉ์ •์‹)์€ ์•„๋ž˜์™€ ๊ฐ™์€<NAME>์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. = x b ๊ฐ€์„ค์˜ ๋ฅผ ๋”ฐ์„œ ๋Œ€์‹  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹์„ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ( ) W + ์ด๋•Œ ์™€ ๊ณฑํ•ด์ง€๋Š” ๋ฅผ ๊ฐ€์ค‘์น˜(Weight)๋ผ๊ณ  ํ•˜๋ฉฐ,๋ฅผ ํŽธํ–ฅ(bias)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์™€๋Š” ์ค‘ํ•™๊ต ์ˆ˜ํ•™ ๊ณผ์ •์ธ ์ง์„ ์˜ ๋ฐฉ์ •์‹์—์„œ ๊ธฐ์šธ๊ธฐ์™€ y ์ ˆํŽธ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ง์„ ์˜ ๋ฐฉ์ •์‹ ๋งํฌ : https://mathbang.net/443 3. ๋น„์šฉ ํ•จ์ˆ˜(Cost function)์— ๋Œ€ํ•œ ์ดํ•ด ์•ž์œผ๋กœ ๋”ฅ ๋Ÿฌ๋‹์„ ํ•™์Šตํ•˜๋ฉด์„œ ์ธํ„ฐ๋„ท์—์„œ ์ด๋Ÿฐ ์šฉ์–ด๋“ค์„ ๋ณธ๋‹ค๋ฉด, ์ „๋ถ€ ๊ฐ™์€ ์šฉ์–ด๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋น„์šฉ ํ•จ์ˆ˜(cost function) = ์†์‹ค ํ•จ์ˆ˜(loss function) = ์˜ค์ฐจ ํ•จ์ˆ˜(error function) = ๋ชฉ์  ํ•จ์ˆ˜(objective function) ํŠนํžˆ ๋น„์šฉ ํ•จ์ˆ˜์™€ ์†์‹ค ํ•จ์ˆ˜๋ž€ ์šฉ์–ด๋Š” ๊ธฐ์–ตํ•ด๋‘๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋น„์šฉ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์—ฌ๊ธฐ์„œ๋งŒ ์ž ๊น ์ƒˆ๋กœ์šด ์˜ˆ์ œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค 4๊ฐœ์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๊ณ , ์ด๋ฅผ 2์ฐจ์› ๊ทธ๋ž˜ํ”„์— 4๊ฐœ์˜ ์ ์œผ๋กœ ํ‘œํ˜„ํ•œ ์ƒํƒœ๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ ๋ชฉํ‘œ๋Š” 4๊ฐœ์˜ ์ ์„ ๊ฐ€์žฅ ์ž˜ ํ‘œํ˜„ํ•˜๋Š” ์ง์„ ์„ ๊ทธ๋ฆฌ๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ์ž„์˜๋กœ 3๊ฐœ์˜ ์ง์„ ์„ ๊ทธ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์„œ๋กœ ๋‹ค๋ฅธ ์™€์˜ ๊ฐ’์— ๋”ฐ๋ผ์„œ ์ฒœ์ฐจ๋งŒ๋ณ„๋กœ ๊ทธ๋ ค์ง„ 3๊ฐœ์˜ ์ง์„ ์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด 3๊ฐœ์˜ ์ง์„  ์ค‘์—์„œ 4๊ฐœ์˜ ์ ์„ ๊ฐ€์žฅ ์ž˜ ๋ฐ˜์˜ํ•œ ์ง์„ ์€ ์–ด๋–ค ์ง์„ ์ธ๊ฐ€์š”? ๊ฒ€์€์ƒ‰ ์ง์„ ์ด๋ผ๊ณ  ๋งํ•˜๋Š” ์‚ฌ๋žŒ๋„ ์žˆ์„ ๊ฒƒ์ด๊ณ , ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค๊ณ  ๋งํ•˜๋Š” ์‚ฌ๋žŒ๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฒ€์€์ƒ‰ ์ง์„ ์ด๋ผ๊ณ  ๋งํ•˜๋Š” ์‚ฌ๋žŒ์€ ๊ฒ€์€์ƒ‰ ์ง์„ ์ด ๊ฐ€์žฅ 4๊ฐœ์˜ ์ ์— ๊ฐ€๊น๊ฒŒ ์ง€๋‚˜๊ฐ€๋Š” ๋Š๋‚Œ์„ ๋ฐ›๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ˆ˜ํ•™์—์„œ ๋Š๋‚Œ์ด๋ผ๋Š” ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์•„๋ฌด๋Ÿฐ ์˜๋ฏธ๋„ ์—†์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ์ง์„ ์ด ๊ฐ€์žฅ ์ ์ ˆํ•œ ์ง์„ ์ธ์ง€๋ฅผ ์ˆ˜ํ•™์ ์ธ ๊ทผ๊ฑฐ๋ฅผ ๋Œ€์„œ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์˜ค์ฐจ(error)๋ผ๋Š” ๊ฐœ๋…์„ ๋„์ž…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ์ž„์˜๋กœ ๊ทธ๋ ค์ง„ ์ฃผํ™ฉ์ƒ‰ ์„ ์— ๋Œ€ํ•ด์„œ ๊ฐ ์‹ค์ œ ๊ฐ’(4๊ฐœ์˜ ์ )๊ณผ ์ง์„ ์˜ ์˜ˆ์ธก๊ฐ’(๋™์ผํ•œ ๊ฐ’์—์„œ์˜ ์ง์„ ์˜ ๊ฐ’)์— ๋Œ€ํ•œ ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๋นจ๊ฐ„์ƒ‰ ํ™”์‚ดํ‘œ โ†•๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ ์‹ค์ œ ๊ฐ’๊ณผ ๊ฐ ์˜ˆ์ธก๊ฐ’๊ณผ์˜ ์ฐจ์ด๊ณ , ์ด๋ฅผ ๊ฐ ์‹ค์ œ ๊ฐ’์—์„œ์˜ ์˜ค์ฐจ๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ง์„ ์˜ ์˜ˆ์ธก๊ฐ’๋“ค๊ณผ ์‹ค์ œ ๊ฐ’๋“ค๊ณผ์˜ ์ด ์˜ค์ฐจ(total error)๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌํ• ๊นŒ์š”? ์ง๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•˜๊ธฐ์— ๋ชจ๋“  ์˜ค์ฐจ๋ฅผ ๋‹ค ๋”ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ ์˜ค์ฐจ๋ฅผ ์ „๋ถ€ ๋”ํ•ด๋ด…์‹œ๋‹ค. ์œ„ ์ฃผํ™ฉ์ƒ‰ ์ง์„ ์˜ ์‹์€ = 13 +์ด๋ฉฐ, ๊ฐ ์˜ค์ฐจ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. hours( ) 2 3 4 5 ์‹ค์ œ ๊ฐ’ 25 50 42 61 ์˜ˆ์ธก๊ฐ’ 27 40 53 66 ์˜ค์ฐจ -2 10 -9 -5 ๊ฐ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ˆ˜์‹์ ์œผ๋กœ ๋‹จ์ˆœํžˆ '์˜ค์ฐจ = ์‹ค์ œ ๊ฐ’ - ์˜ˆ์ธก๊ฐ’'์œผ๋กœ ์ •์˜ํ•˜๋ฉด ์˜ค์ฐจ ๊ฐ’์ด ์Œ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ํ‘œ์—์„œ๋งŒ ๋ด๋„ ์˜ค์ฐจ๊ฐ€ ์Œ์ˆ˜์ธ ๊ฒฝ์šฐ๊ฐ€ 3๋ฒˆ์ด๋‚˜ ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์˜ค์ฐจ๋ฅผ ๋ชจ๋‘ ๋”ํ•˜๋ฉด ๋ง์…ˆ ๊ณผ์ •์—์„œ ์˜ค์ฐจ ๊ฐ’์ด +๊ฐ€ ๋˜์—ˆ๋‹ค๊ฐ€ -๋˜์—ˆ๋‹ค๊ฐ€ ํ•˜๋ฏ€๋กœ ์ œ๋Œ€๋กœ ๋œ ์˜ค์ฐจ์˜ ํฌ๊ธฐ๋ฅผ ์ธก์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์˜ค์ฐจ๋ฅผ ๊ทธ๋ƒฅ ์ „๋ถ€ ๋”ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๊ฐ ์˜ค์ฐจ๋“ค์„ ์ œ๊ณฑํ•ด ์ค€ ๋’ค์— ์ „๋ถ€ ๋”ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹จ, ์—ฌ๊ธฐ์„œ ์€ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. i 1 [ ( ) H ( ( ) ) ] = ( 2 ) + 10 + ( 9 ) + ( 5 ) = 210 ์ด๋•Œ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์ธ์œผ๋กœ ๋‚˜๋ˆ„๋ฉด, ์˜ค์ฐจ์˜ ์ œ๊ณฑํ•ฉ์— ๋Œ€ํ•œ ํ‰๊ท ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋ฅผ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ(Mean Squared Error, MSE)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. n i 1 [ ( ) H ( ( ) ) ] = 210 4 52.5 ์ด๋ฅผ ์‹ค์ œ๋กœ ๊ณ„์‚ฐํ•˜๋ฉด 52.5๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” = 13 +์˜ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ์˜ ๊ฐ’์ด 52.5์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋Š” ์ด๋ฒˆ ํšŒ๊ท€ ๋ฌธ์ œ์—์„œ ์ ์ ˆํ•œ ์™€๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ ์ตœ์ ํ™”๋œ ์‹์ž…๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ์˜ ๊ฐ’์„ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ๋งŒ๋“œ๋Š” ์™€๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜ ๋ฐ˜์˜ํ•œ ์ง์„ ์„ ์ฐพ์•„๋‚ด๋Š” ์ผ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ์™€์— ์˜ํ•œ ๋น„์šฉ ํ•จ์ˆ˜(Cost function)๋กœ ์žฌ์ •์˜ํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o t ( , ) 1 โˆ‘ = n [ ( ) H ( ( ) ) ] ๋‹ค์‹œ ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. o t ( , ) ๋ฅผ ์ตœ์†Œ๊ฐ€ ๋˜๊ฒŒ ๋งŒ๋“œ๋Š” ์™€๋ฅผ ๊ตฌํ•˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์žฅ ์ž˜ ๋‚˜ํƒ€๋‚ด๋Š” ์ง์„ ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. ์˜ตํ‹ฐ๋งˆ์ด์ € - ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Gradient Descent) ์ด์ œ ์•ž์„œ ์ •์˜ํ•œ ๋น„์šฉ ํ•จ์ˆ˜(Cost Function)์˜ ๊ฐ’์„ ์ตœ์†Œ๋กœ ํ•˜๋Š” ์™€๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šธ ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ์˜ตํ‹ฐ๋งˆ์ด์ €(Optimizer) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ตœ์ ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์˜ตํ‹ฐ๋งˆ์ด์ € ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ์ ์ ˆํ•œ ์™€๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ณผ์ •์„ ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ํ•™์Šต(training)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์˜ตํ‹ฐ๋งˆ์ด์ € ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Gradient Descent)์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. ์ด๋ฒˆ ์„ค๋ช…์—์„œ ํŽธํ–ฅ ๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๊ฐ€ 0์ด๋ผ๊ณ  ๊ฐ€์ •ํ•œ = x ์™€ ๊ฐ™์€ ์‹์„ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ๊ฐ€ ์ง์„ ์˜ ๋ฐฉ์ •์‹์—์„œ๋Š” ๊ธฐ์šธ๊ธฐ์˜€์Œ์„ ๊ธฐ์–ตํ•ฉ์‹œ๋‹ค. ์ด์ œ๋ฅผ ๊ธฐ์šธ๊ธฐ๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ณ  ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์ฃผํ™ฉ์ƒ‰์„ ์€ ๊ธฐ์šธ๊ธฐ ๊ฐ€ 20์ผ ๋•Œ, ์ดˆ๋ก์ƒ‰์„ ์€ ๊ธฐ์šธ๊ธฐ ๊ฐ€ 1์ผ ๋•Œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜๋ฉด ๊ฐ๊ฐ = 20 , =์— ํ•ด๋‹น๋˜๋Š” ์ง์„ ์ž…๋‹ˆ๋‹ค. โ†•๋Š” ๊ฐ ์ ์—์„œ์˜ ์‹ค์ œ ๊ฐ’๊ณผ ๋‘ ์ง์„ ์˜ ์˜ˆ์ธก๊ฐ’๊ณผ์˜ ์˜ค์ฐจ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋Š” ์•ž์„œ ์˜ˆ์ธก์— ์‚ฌ์šฉํ–ˆ๋˜ = 13 + ์ง์„ ๋ณด๋‹ค ํ™•์—ฐํžˆ ํฐ ์˜ค์ฐจ ๊ฐ’๋“ค์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ํฌ๋ฉด ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์˜ ์˜ค์ฐจ๊ฐ€ ์ปค์ง€๊ณ , ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ์ž‘์•„๋„ ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์˜ ์˜ค์ฐจ๊ฐ€ ์ปค์ง‘๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋˜ํ•œ ๋งˆ์ฐฌ๊ฐ€์ง€์ธ๋ฐ ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ํฌ๊ฑฐ๋‚˜ ์ž‘์œผ๋ฉด ์˜ค์ฐจ๊ฐ€ ์ปค์ง‘๋‹ˆ๋‹ค. ์„ค๋ช…์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด ํŽธํ–ฅ ๊ฐ€ ์—†์ด ๋‹จ์ˆœํžˆ ๊ฐ€์ค‘์น˜ ๋งŒ์„ ์‚ฌ์šฉํ•œ ( ) W๋ผ๋Š” ๊ฐ€์„ค์„ ๊ฐ€์ง€๊ณ , ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋น„์šฉ ํ•จ์ˆ˜์˜ ๊ฐ’ o t ( ) ๋Š” cost๋ผ๊ณ  ์ค„์—ฌ์„œ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ์™€ cost์˜ ๊ด€๊ณ„๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ ๊ฐ€ ๋ฌดํ•œ๋Œ€๋กœ ์ปค์ง€๋ฉด ์ปค์งˆ์ˆ˜๋ก cost์˜ ๊ฐ’ ๋˜ํ•œ ๋ฌดํ•œ๋Œ€๋กœ ์ปค์ง€๊ณ , ๋ฐ˜๋Œ€๋กœ ๊ธฐ์šธ๊ธฐ ๊ฐ€ ๋ฌดํ•œ๋Œ€๋กœ ์ž‘์•„์ ธ๋„ cost์˜ ๊ฐ’์€ ๋ฌดํ•œ๋Œ€๋กœ ์ปค์ง‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ž˜ํ”„์—์„œ cost๊ฐ€ ๊ฐ€์žฅ ์ž‘์„ ๋•Œ๋Š” ๋งจ ์•„๋ž˜์˜ ๋ณผ๋กํ•œ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ํ•ด์•ผ ํ•  ์ผ์€ cost๊ฐ€ ๊ฐ€์žฅ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ํ•˜๋Š” ๋ฅผ ์ฐพ๋Š” ์ผ์ด๋ฏ€๋กœ, ๋งจ ์•„๋ž˜์˜ ๋ณผ๋กํ•œ ๋ถ€๋ถ„์˜์˜ ๊ฐ’์„ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ์ž„์˜์˜ ์ดˆ๊นƒ๊ฐ’ ๊ฐ’์„ ์ •ํ•œ ๋’ค์—, ๋งจ ์•„๋ž˜์˜ ๋ณผ๋กํ•œ ๋ถ€๋ถ„์„ ํ–ฅํ•ด ์ ์ฐจ์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๊ฐ’์ด ์ ์ฐจ ์ˆ˜์ •๋˜๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Gradient Descent)์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ณ ๋“ฑํ•™๊ต ์ˆ˜ํ•™ ๊ณผ์ •์ธ ๋ฏธ๋ถ„์„ ์ดํ•ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ๋ฏธ๋ถ„์„ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋ฉด ๊ฐ€์žฅ ์ฒ˜์Œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” ๊ฐœ๋…์ธ ํ•œ ์ ์—์„œ์˜ ์ˆœ๊ฐ„ ๋ณ€ํ™”์œจ ๋˜๋Š” ์ ‘์„ ์—์„œ์˜ ๊ธฐ์šธ๊ธฐ์˜ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์ดˆ๋ก์ƒ‰ ์„ ์€ ๊ฐ€ ์ž„์˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š” ๋„ค ๊ฐ€์ง€์˜ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ, ๊ทธ๋ž˜ํ”„ ์ƒ์œผ๋กœ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ฃผ๋ชฉํ•  ๊ฒƒ์€ ๋งจ ์•„๋ž˜์˜ ๋ณผ๋กํ•œ ๋ถ€๋ถ„์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ ์ฐจ ์ž‘์•„์ง„๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งจ ์•„๋ž˜์˜ ๋ณผ๋กํ•œ ๋ถ€๋ถ„์—์„œ๋Š” ๊ฒฐ๊ตญ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„ ์ƒ์œผ๋กœ๋Š” ์ดˆ๋ก์ƒ‰ ํ™”์‚ดํ‘œ๊ฐ€ ์ˆ˜ํ‰์ด ๋˜๋Š” ์ง€์ ์ž…๋‹ˆ๋‹ค. ์ฆ‰, cost๊ฐ€ ์ตœ์†Œํ™”๊ฐ€ ๋˜๋Š” ์ง€์ ์€ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ด ๋˜๋Š” ์ง€์ ์ด๋ฉฐ, ๋˜ํ•œ ๋ฏธ๋ถ„ ๊ฐ’์ด 0์ด ๋˜๋Š” ์ง€์ ์ž…๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์˜ ์•„์ด๋””์–ด๋Š” ๋น„์šฉ ํ•จ์ˆ˜(Cost function)๋ฅผ ๋ฏธ๋ถ„ํ•˜์—ฌ ํ˜„์žฌ์—์„œ์˜ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ตฌํ•˜๊ณ , ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ๋‚ฎ์€ ๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐ˜๋ณต ์ž‘์—…์—๋Š” ํ˜„์žฌ์— ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ตฌํ•ด ํŠน์ • ์ˆซ์ž ฮฑ๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ๋นผ์„œ ์ƒˆ๋กœ์šด ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์‹์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ ์šธ = c s ( ) W ๊ธฐ์šธ๊ธฐ๊ฐ€ ์Œ์ˆ˜์ผ ๋•Œ์™€ ์–‘์ˆ˜์ผ ๋•Œ ์–ด๋–ป๊ฒŒ ๊ฐ’์ด ์กฐ์ •๋˜๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ๊ฐ€ ์Œ์ˆ˜์ผ ๋•Œ :์˜ ๊ฐ’์ด ์ฆ๊ฐ€ ์Œ์ˆ˜ ๊ธฐ์šธ๊ธฐ ์–‘์ˆ˜ ๊ธฐ์šธ๊ธฐ := โˆ’ ร— ( ์ˆ˜ ์šธ ) W ฮฑ ( ์ˆ˜ ์šธ ) ๊ธฐ์šธ๊ธฐ๊ฐ€ ์Œ์ˆ˜ ๋ฉด์˜ ๊ฐ’์ด ์ฆ๊ฐ€ํ•˜๋Š”๋ฐ ์ด๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ธ ๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฐ’์ด ์กฐ์ •๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์–‘์ˆ˜๋ผ๋ฉด ์œ„์˜ ์ˆ˜์‹์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ๊ฐ€ ์–‘์ˆ˜์ผ ๋•Œ :์˜ ๊ฐ’์ด ๊ฐ์†Œ ์–‘์ˆ˜ ๊ธฐ์šธ๊ธฐ := โˆ’ ร— ( ์ˆ˜ ์šธ ) ๊ธฐ์šธ๊ธฐ๊ฐ€ ์–‘์ˆ˜๋ฉด์˜ ๊ฐ’์ด ๊ฐ์†Œํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ธ ๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฐ’์ด ์กฐ์ •๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์•„๋ž˜์˜ ์ˆ˜์‹์€ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์Œ์ˆ˜๊ฑฐ๋‚˜, ์–‘์ˆ˜์ผ ๋•Œ ๋ชจ๋‘ ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ธ ๋ฐฉํ–ฅ์œผ๋กœ์˜ ๊ฐ’์„ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. := โˆ’ โˆ‚ W o t ( ) ๊ทธ๋ ‡๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ํ•™์Šต๋ฅ (learning rate)์ด๋ผ๊ณ  ๋งํ•˜๋Š” ๋Š” ์–ด๋–ค ์˜๋ฏธ๋ฅผ ๊ฐ€์งˆ๊นŒ์š”? ํ•™์Šต๋ฅ  ์€์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ, ์–ผ๋งˆ๋‚˜ ํฌ๊ฒŒ ๋ณ€๊ฒฝํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ๋ฅผ ๊ทธ๋ž˜ํ”„์˜ ํ•œ ์ ์œผ๋กœ ๋ณด๊ณ  ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ผ ๋•Œ๊นŒ์ง€ ๊ฒฝ์‚ฌ๋ฅผ ๋”ฐ๋ผ ๋‚ด๋ ค๊ฐ„๋‹ค๋Š” ๊ด€์ ์—์„œ๋Š” ์–ผ๋งˆ๋‚˜ ํฐ ํญ์œผ๋กœ ์ด๋™ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์ง๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•˜๊ธฐ์— ํ•™์Šต๋ฅ ์˜ ๊ฐ’์„ ๋ฌด์ž‘์ • ํฌ๊ฒŒ ํ•˜๋ฉด ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ตœ์†Œ๊ฐ’์ด ๋˜๋Š” ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์ง€๋งŒ ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ํ•™์Šต๋ฅ  ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ๋†’์€ ๊ฐ’์„ ๊ฐ€์งˆ ๋•Œ, ์ ‘์„ ์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ด ๋˜๋Š” ๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ o t ์˜ ๊ฐ’์ด ๋ฐœ์‚ฐํ•˜๋Š” ์ƒํ™ฉ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ํ•™์Šต๋ฅ  ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ๋‚ฎ์€ ๊ฐ’์„ ๊ฐ€์ง€๋ฉด ํ•™์Šต ์†๋„๊ฐ€ ๋Š๋ ค์ง€๋ฏ€๋กœ ์ ๋‹นํ•œ ์˜ ๊ฐ’์„ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” ๋Š” ๋ฐฐ์ œ์‹œํ‚ค๊ณ  ์ตœ์ ์˜ ๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์—๋งŒ ์ดˆ์ ์„ ๋งž์ถ”์–ด ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์˜ ์›๋ฆฌ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ๋Š”๋ฐ, ์‹ค์ œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ์™€์— ๋Œ€ํ•ด์„œ ๋™์‹œ์— ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ์ตœ์ ์˜ ์™€์˜ ๊ฐ’์„ ์ฐพ์•„๊ฐ‘๋‹ˆ๋‹ค. ๊ฐ€์„ค, ๋น„์šฉ ํ•จ์ˆ˜, ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํฌ๊ด„์  ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ํ’€๊ณ ์ž ํ•˜๋Š” ๊ฐ ๋ฌธ์ œ์— ๋”ฐ๋ผ ๊ฐ€์„ค, ๋น„์šฉ ํ•จ์ˆ˜, ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ์ „๋ถ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์„ ํ˜• ํšŒ๊ท€์— ๊ฐ€์žฅ ์ ํ•ฉํ•œ ๋น„์šฉ ํ•จ์ˆ˜๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ, ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ€์„ค, ๋น„์šฉ ํ•จ์ˆ˜, ์˜ตํ‹ฐ๋งˆ์ด์ €์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜์˜€์œผ๋‹ˆ ํŒŒ์ด ํ† ์น˜๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4. ํŒŒ์ด ํ† ์น˜๋กœ ์„ ํ˜• ํšŒ๊ท€ ๊ตฌํ˜„ํ•˜๊ธฐ ์šฐ์„  ์‹ค์Šต์„ ์œ„ํ•ด ํŒŒ์ด ํ† ์น˜์˜ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•˜๋Š” ๊ธฐ๋ณธ ์„ธํŒ…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 1. ๊ธฐ๋ณธ ์„ธํŒ… import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim # ํ˜„์žฌ ์‹ค์Šตํ•˜๊ณ  ์žˆ๋Š” ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ์žฌ์‹คํ–‰ํ•ด๋„ ๋‹ค์Œ์—๋„ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก ๋žœ๋ค ์‹œ๋“œ(random seed)๋ฅผ ์ค๋‹ˆ๋‹ค. torch.manual_seed(1) ์‹ค์Šต์„ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ์„ธํŒ…์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์ธ x_train๊ณผ y_train์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. 2. ๋ณ€์ˆ˜ ์„ ์–ธ x_train = torch.FloatTensor([[1], [2], [3]]) y_train = torch.FloatTensor([[2], [4], [6]]) x_train๊ณผ x_train์˜ ํฌ๊ธฐ(shape)๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(x_train) print(x_train.shape) tensor([[1.], [2.], [3.]]) torch.Size([3, 1]) x_train์˜ ๊ฐ’์ด ์ถœ๋ ฅ๋˜๊ณ , x_train์˜ ํฌ๊ธฐ๊ฐ€ (3 ร— 1) ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. y_train๊ณผ y_train์˜ ํฌ๊ธฐ(shape)๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(y_train) print(y_train.shape) tensor([[2.], [4.], [6.]]) torch.Size([3, 1]) y_train์˜ ๊ฐ’์ด ์ถœ๋ ฅ๋˜๊ณ , y_train์˜ ํฌ๊ธฐ๊ฐ€ (3 ร— 1) ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ์ดˆ๊ธฐํ™” ์„ ํ˜• ํšŒ๊ท€๋ž€ ํ•™์Šต ๋ฐ์ดํ„ฐ์™€ ๊ฐ€์žฅ ์ž˜ ๋งž๋Š” ํ•˜๋‚˜์˜ ์ง์„ ์„ ์ฐพ๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ์ž˜ ๋งž๋Š” ์ง์„ ์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์€ ๋ฐ”๋กœ ์™€์ž…๋‹ˆ๋‹ค. ์„ ํ˜• ํšŒ๊ท€์˜ ๋ชฉํ‘œ๋Š” ๊ฐ€์žฅ ์ž˜ ๋งž๋Š” ์ง์„ ์„ ์ •์˜ํ•˜๋Š” ์™€์˜ ๊ฐ’์„ ์ฐพ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ๊ฐ€์ค‘์น˜ W๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , ์ด ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ๊ฐ€์ค‘์น˜ W๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ํ•™์Šต์„ ํ†ตํ•ด ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋Š” ๋ณ€์ˆ˜์ž„์„ ๋ช…์‹œํ•จ. W = torch.zeros(1, requires_grad=True) # ๊ฐ€์ค‘์น˜ W๋ฅผ ์ถœ๋ ฅ print(W) tensor([0.], requires_grad=True) ๊ฐ€์ค‘์น˜ W๊ฐ€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋˜์–ด์žˆ์œผ๋ฏ€๋กœ 0์ด ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ requires_grad=True๊ฐ€ ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ด ๋ณ€์ˆ˜๋Š” ํ•™์Šต์„ ํ†ตํ•ด ๊ณ„์† ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋Š” ๋ณ€์ˆ˜์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํŽธํ–ฅ ๋„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , ํ•™์Šต์„ ํ†ตํ•ด ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋Š” ๋ณ€์ˆ˜์ž„์„ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค. b = torch.zeros(1, requires_grad=True) print(b) tensor([0.], requires_grad=True) ํ˜„์žฌ ๊ฐ€์ค‘์น˜ ์™€ ๋‘˜ ๋‹ค 0์ด๋ฏ€๋กœ ํ˜„ ์ง์„ ์˜ ๋ฐฉ์ •์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. = ร— + ์ง€๊ธˆ ์ƒํƒœ์— ์„ ์— ์–ด๋–ค ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋„ ๊ฐ€์„ค์€ 0์„ ์˜ˆ์ธกํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์•„์ง ์ ์ ˆํ•œ ์™€์˜ ๊ฐ’์ด ์•„๋‹™๋‹ˆ๋‹ค. 4. ๊ฐ€์„ค ์„ธ์šฐ๊ธฐ ํŒŒ์ด ํ† ์น˜ ์ฝ”๋“œ ์ƒ์œผ๋กœ ์ง์„ ์˜ ๋ฐฉ์ •์‹์— ํ•ด๋‹น๋˜๋Š” ๊ฐ€์„ค์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. ( ) W + hypothesis = x_train * W + b print(hypothesis) 5. ๋น„์šฉ ํ•จ์ˆ˜ ์„ ์–ธํ•˜๊ธฐ ํŒŒ์ด ํ† ์น˜ ์ฝ”๋“œ ์ƒ์œผ๋กœ ์„ ํ˜• ํšŒ๊ท€์˜ ๋น„์šฉ ํ•จ์ˆ˜์— ํ•ด๋‹น๋˜๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. o t ( , ) 1 โˆ‘ = n [ ( ) H ( ( ) ) ] # ์•ž์„œ ๋ฐฐ์šด torch.mean์œผ๋กœ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค. cost = torch.mean((hypothesis - y_train) ** 2) print(cost) tensor(18.6667, grad_fn=<MeanBackward1>) 6. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ• ๊ตฌํ˜„ํ•˜๊ธฐ ์ด์ œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ 'SGD'๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์˜ ์ผ์ข…์ž…๋‹ˆ๋‹ค. lr์€ ํ•™์Šต๋ฅ (learning rate)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ๋Œ€์ƒ์ธ W์™€ b๊ฐ€ SGD์˜ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. optimizer = optim.SGD([W, b], lr=0.01) optimizer.zero_grad()๋ฅผ ์‹คํ–‰ํ•จ์œผ๋กœ์จ ๋ฏธ๋ถ„์„ ํ†ตํ•ด ์–ป์€ ๊ธฐ์šธ๊ธฐ๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผ๋งŒ ์ƒˆ๋กœ์šด ๊ฐ€์ค‘์น˜ ํŽธํ–ฅ์— ๋Œ€ํ•ด์„œ ์ƒˆ๋กœ์šด ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ cost.backward() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๊ฐ€์ค‘์น˜ W์™€ ํŽธํ–ฅ b์— ๋Œ€ํ•œ ๊ธฐ์šธ๊ธฐ๊ฐ€ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ• ์ตœ์ ํ™” ํ•จ์ˆ˜ opimizer์˜. step() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ธ์ˆ˜๋กœ ๋“ค์–ด๊ฐ”๋˜ W์™€ b์—์„œ ๋ฆฌํ„ด๋˜๋Š” ๋ณ€์ˆ˜๋“ค์˜ ๊ธฐ์šธ๊ธฐ์— ํ•™์Šต๋ฅ (learining rate) 0.01์„ ๊ณฑํ•˜์—ฌ ๋นผ์คŒ์œผ๋กœ์จ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. # gradient๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” optimizer.zero_grad() # ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•˜์—ฌ gradient ๊ณ„์‚ฐ cost.backward() # W์™€ b๋ฅผ ์—…๋ฐ์ดํŠธ optimizer.step() requires_grad=True์™€ backward()์— ๋Œ€ํ•œ ์ •๋ฆฌ๋Š” ์ž๋™ ๋ฏธ๋ถ„(Autograd) ์ฑ•ํ„ฐ์— ๋ณ„๋„ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. 7. ์ „์ฒด ์ฝ”๋“œ # ๋ฐ์ดํ„ฐ x_train = torch.FloatTensor([[1], [2], [3]]) y_train = torch.FloatTensor([[2], [4], [6]]) # ๋ชจ๋ธ ์ดˆ๊ธฐํ™” W = torch.zeros(1, requires_grad=True) b = torch.zeros(1, requires_grad=True) # optimizer ์„ค์ • optimizer = optim.SGD([W, b], lr=0.01) nb_epochs = 1999 # ์›ํ•˜๋Š” ๋งŒํผ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ๋ฐ˜๋ณต for epoch in range(nb_epochs + 1): # H(x) ๊ณ„์‚ฐ hypothesis = x_train * W + b # cost ๊ณ„์‚ฐ cost = torch.mean((hypothesis - y_train) ** 2) # cost๋กœ H(x) ๊ฐœ์„  optimizer.zero_grad() cost.backward() optimizer.step() # 100๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ if epoch % 100 == 0: print('Epoch {:4d}/{} W: {:.3f}, b: {:.3f} Cost: {:.6f}'.format( epoch, nb_epochs, W.item(), b.item(), cost.item() )) ๊ฒฐ๊ณผ์ ์œผ๋กœ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ์™€๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ์ž˜ ๋งž๋Š” ์ง์„ ์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ ์ ˆํ•œ ๊ฐ’์œผ๋กœ ๋ณ€ํ™”ํ•ด๊ฐ‘๋‹ˆ๋‹ค. Epoch 0/2000 W: 0.187, b: 0.080 Cost: 18.666666 Epoch 100/2000 W: 1.746, b: 0.578 Cost: 0.048171 Epoch 200/2000 W: 1.800, b: 0.454 Cost: 0.029767 Epoch 300/2000 W: 1.843, b: 0.357 Cost: 0.018394 Epoch 400/2000 W: 1.876, b: 0.281 Cost: 0.011366 Epoch 500/2000 W: 1.903, b: 0.221 Cost: 0.007024 Epoch 600/2000 W: 1.924, b: 0.174 Cost: 0.004340 Epoch 700/2000 W: 1.940, b: 0.136 Cost: 0.002682 Epoch 800/2000 W: 1.953, b: 0.107 Cost: 0.001657 Epoch 900/2000 W: 1.963, b: 0.084 Cost: 0.001024 Epoch 1000/2000 W: 1.971, b: 0.066 Cost: 0.000633 Epoch 1100/2000 W: 1.977, b: 0.052 Cost: 0.000391 Epoch 1200/2000 W: 1.982, b: 0.041 Cost: 0.000242 Epoch 1300/2000 W: 1.986, b: 0.032 Cost: 0.000149 Epoch 1400/2000 W: 1.989, b: 0.025 Cost: 0.000092 Epoch 1500/2000 W: 1.991, b: 0.020 Cost: 0.000057 Epoch 1600/2000 W: 1.993, b: 0.016 Cost: 0.000035 Epoch 1700/2000 W: 1.995, b: 0.012 Cost: 0.000022 Epoch 1800/2000 W: 1.996, b: 0.010 Cost: 0.000013 Epoch 1900/2000 W: 1.997, b: 0.008 Cost: 0.000008 Epoch 2000/2000 W: 1.997, b: 0.006 Cost: 0.000005 ์—ํฌํฌ(Epoch)๋Š” ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•™์Šต์— ํ•œ ๋ฒˆ ์‚ฌ์šฉ๋œ ์ฃผ๊ธฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์˜ ๊ฒฝ์šฐ 2,000๋ฒˆ์„ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ข… ํ›ˆ๋ จ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์ตœ์ ์˜ ๊ธฐ์šธ๊ธฐ๋Š” 2์— ๊ฐ€๊น๊ณ ,๋Š” 0์— ๊ฐ€๊นŒ์šด ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ x_train์€ [[1], [2], [3]]์ด๊ณ  y_train์€ [[2], [4], [6]]์ธ ๊ฒƒ์„ ๊ฐ์•ˆํ•˜๋ฉด ์‹ค์ œ ์ •๋‹ต์€ ๊ฐ€ 2์ด๊ณ , ๊ฐ€ 0์ธ ( ) 2์ด๋ฏ€๋กœ ๊ฑฐ์˜ ์ •๋‹ต์„ ์ฐพ์€ ์…ˆ์ž…๋‹ˆ๋‹ค. 5. optimizer.zero_grad()๊ฐ€ ํ•„์š”ํ•œ ์ด์œ  ํŒŒ์ด ํ† ์น˜๋Š” ๋ฏธ๋ถ„์„ ํ†ตํ•ด ์–ป์€ ๊ธฐ์šธ๊ธฐ๋ฅผ ์ด์ „์— ๊ณ„์‚ฐ๋œ ๊ธฐ์šธ๊ธฐ ๊ฐ’์— ๋ˆ„์ ์‹œํ‚ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. import torch w = torch.tensor(2.0, requires_grad=True) nb_epochs = 20 for epoch in range(nb_epochs + 1): z = 2*w z.backward() print('์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : {}'.format(w.grad)) ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 2.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 4.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 6.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 8.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 10.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 12.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 14.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 16.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 18.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 20.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 22.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 24.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 26.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 28.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 30.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 32.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 34.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 36.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 38.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 40.0 ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 42.0 ๊ณ„์†ํ•ด์„œ ๋ฏธ๋ถ„ ๊ฐ’์ธ 2๊ฐ€ ๋ˆ„์ ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— optimizer.zero_grad()๋ฅผ ํ†ตํ•ด ๋ฏธ๋ถ„ ๊ฐ’์„ ๊ณ„์† 0์œผ๋กœ ์ดˆ๊ธฐํ™”์‹œ์ผœ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. 6. torch.manual_seed()๋ฅผ ํ•˜๋Š” ์ด์œ  torch.manual_seed()๋ฅผ ์‚ฌ์šฉํ•œ ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฒฐ๊ณผ๋Š” ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ์—์„œ ์‹คํ–‰์‹œ์ผœ๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” torch.manual_seed()๋Š” ๋‚œ์ˆ˜ ๋ฐœ์ƒ ์ˆœ์„œ์™€ ๊ฐ’์„ ๋™์ผํ•˜๊ฒŒ ๋ณด์žฅํ•ด ์ค€๋‹ค๋Š” ํŠน์ง• ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ๋žœ๋ค ์‹œ๋“œ๊ฐ€ 3์ผ ๋•Œ ๋‘ ๋ฒˆ ๋‚œ์ˆ˜๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ๋ณด๊ณ , ๋‹ค๋ฅธ ๋žœ๋ค ์‹œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ํ›„์— ๋‹ค์‹œ ๋žœ๋ค ์‹œ๋“œ๋ฅผ 3์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋‚œ์ˆ˜ ๋ฐœ์ƒ ๊ฐ’์ด ๋™์ผํ•˜๊ฒŒ ๋‚˜์˜ค๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import torch torch.manual_seed(3) print('๋žœ๋ค ์‹œ๋“œ๊ฐ€ 3์ผ ๋•Œ') for i in range(1,3): print(torch.rand(1)) ๋žœ๋ค ์‹œ๋“œ๊ฐ€ 3์ผ ๋•Œ tensor([0.0043]) tensor([0.1056]) ๋žœ๋ค ์‹œ๋“œ๊ฐ€ 3์ผ ๋•Œ ๋‘ ๊ฐœ์˜ ๋‚œ์ˆ˜๋ฅผ ๋ฐœ์ƒ์‹œ์ผฐ๋”๋‹ˆ 0.0043๊ณผ 0.1056์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด์ œ ๋žœ๋ค ์‹œ๋“œ ๊ฐ’์„ ๋ฐ”๊ฟ”๋ด…์‹œ๋‹ค. torch.manual_seed(5) print('๋žœ๋ค ์‹œ๋“œ๊ฐ€ 5์ผ ๋•Œ') for i in range(1,3): print(torch.rand(1)) ๋žœ๋ค ์‹œ๋“œ๊ฐ€ 5์ผ ๋•Œ tensor([0.8303]) tensor([0.1261]) 0.8303๊ณผ 0.1261์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด์ œ ๋‹ค์‹œ ๋žœ๋ค ์‹œ๋“œ ๊ฐ’์„ 3์œผ๋กœ ๋Œ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์‹œ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹คํ–‰ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋‚œ์ˆ˜ ๋ฐœ์ƒ ์ˆœ์„œ๊ฐ€ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. torch.manual_seed(3) print('๋žœ๋ค ์‹œ๋“œ๊ฐ€ ๋‹ค์‹œ 3์ผ ๋•Œ') for i in range(1,3): print(torch.rand(1)) ๋žœ๋ค ์‹œ๋“œ๊ฐ€ ๋‹ค์‹œ 3์ผ ๋•Œ tensor([0.0043]) tensor([0.1056]) ๋‹ค์‹œ ๋™์ผํ•˜๊ฒŒ 0.0043๊ณผ 0.1056์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ํ…์„œ์—๋Š” requires_grad๋ผ๋Š” ์†์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ True๋กœ ์„ค์ •ํ•˜๋ฉด ์ž๋™ ๋ฏธ๋ถ„ ๊ธฐ๋Šฅ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์„ ํ˜• ํšŒ๊ท€๋ถ€ํ„ฐ ์‹ ๊ฒฝ๋ง๊ณผ ๊ฐ™์€ ๋ณต์žกํ•œ ๊ตฌ์กฐ์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์ด ๋ชจ๋‘ ์ด ๊ธฐ๋Šฅ์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. requires_grad = True๊ฐ€ ์ ์šฉ๋œ ํ…์„œ์— ์—ฐ์‚ฐ์„ ํ•˜๋ฉด, ๊ณ„์‚ฐ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ƒ์„ฑ๋˜๋ฉฐ backward ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๊ทธ๋ž˜ํ”„๋กœ๋ถ€ํ„ฐ ์ž๋™์œผ๋กœ ๋ฏธ๋ถ„์ด ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. 03-02 ์ž๋™ ๋ฏธ๋ถ„(Autograd) ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ• ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์žˆ์œผ๋ฉด requires_grad=True, backward() ๋“ฑ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด๋Š” ํŒŒ์ด ํ† ์น˜์—์„œ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š” ์ž๋™ ๋ฏธ๋ถ„(Autograd) ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜์˜ ํ•™์Šต ๊ณผ์ •์„ ๋ณด๋‹ค ๋” ์ž˜ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž๋™ ๋ฏธ๋ถ„์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 1. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ• ๋ฆฌ๋ทฐ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ๊ฐ„๋‹จํžˆ ๋ณต์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•˜์—ฌ ์ด ํ•จ์ˆ˜์˜ ๊ธฐ์šธ๊ธฐ(gradient)๋ฅผ ๊ตฌํ•ด์„œ ๋น„์šฉ์ด ์ตœ์†Œํ™”๋˜๋Š” ๋ฐฉํ–ฅ์„ ์ฐพ์•„๋‚ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์†์‹ค ํ•จ์ˆ˜, ์˜ค์ฐจ ํ•จ์ˆ˜๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋ฏ€๋กœ ๋น„์šฉ์ด ์ตœ์†Œํ™”๋˜๋Š” ๋ฐฉํ–ฅ์ด๋ผ๋Š” ํ‘œํ˜„ ๋Œ€์‹  ์†์‹ค์ด ์ตœ์†Œํ™”๋˜๋Š” ๋ฐฉํ–ฅ ๋˜๋Š” ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”๋˜๋Š” ๋ฐฉํ–ฅ์ด๋ผ๊ณ ๋„ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ๋ณต์žกํ•ด์งˆ์ˆ˜๋ก ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ๋„˜ํŒŒ์ด ๋“ฑ์œผ๋กœ ์ง์ ‘ ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ์€ ๊นŒ๋‹ค๋กœ์šด ์ผ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ์ด๋Ÿฐ ์ˆ˜๊ณ ๋ฅผ ํ•˜์ง€ ์•Š๋„๋ก ์ž๋™ ๋ฏธ๋ถ„(Autograd)์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ž๋™ ๋ฏธ๋ถ„์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฏธ๋ถ„ ๊ณ„์‚ฐ์„ ์ž๋™ํ™”ํ•˜์—ฌ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์†์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. 2. ์ž๋™ ๋ฏธ๋ถ„(Autograd) ์‹ค์Šตํ•˜๊ธฐ ์ž๋™ ๋ฏธ๋ถ„์— ๋Œ€ํ•ด์„œ ์‹ค์Šต์„ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์ž„์˜๋กœ w +๋ผ๋Š” ์‹์„ ์„ธ์›Œ๋ณด๊ณ ,์— ๋Œ€ํ•ด ๋ฏธ๋ถ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import torch ๊ฐ’์ด 2์ธ ์ž„์˜์˜ ์Šค์นผ๋ผ ํ…์„œ w๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ required_grad๋ฅผ True๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ด ํ…์„œ์— ๋Œ€ํ•œ ๊ธฐ์šธ๊ธฐ๋ฅผ ์ €์žฅํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ณด๊ฒ ์ง€๋งŒ, ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด w.grad์— w์— ๋Œ€ํ•œ ๋ฏธ๋ถ„ ๊ฐ’์ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. w = torch.tensor(2.0, requires_grad=True) ์ด์ œ ์ˆ˜์‹์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. y = w**2 z = 2*y + 5 ์ด์ œ ํ•ด๋‹น ์ˆ˜์‹์„ w์— ๋Œ€ํ•ด์„œ ๋ฏธ๋ถ„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. .backward()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ•ด๋‹น ์ˆ˜์‹์˜ w์— ๋Œ€ํ•œ ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. z.backward() ์ด์ œ w.grad๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด w๊ฐ€ ์†ํ•œ ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’์ด ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print('์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : {}'.format(w.grad)) ์ˆ˜์‹์„ w๋กœ ๋ฏธ๋ถ„ํ•œ ๊ฐ’ : 8.0 ์ฐธ๊ณ  ์ž๋ฃŒ : https://tutorials.pytorch.kr/beginner/blitz/autograd_tutorial.html? highlight=autograd ์ž๋™ ๋ฏธ๋ถ„์— ๋Œ€ํ•œ ์œ ํŠœ๋ธŒ ์˜์ƒ : https://www.youtube.com/watch? v=E0R9Xf_GyUc 03-03 ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€(Multivariable Linear regression) ์•ž์„œ ๋ฐฐ์šด ๊ฐ€ 1๊ฐœ์ธ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€(Simple Linear Regression)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋‹ค์ˆ˜์˜ ๋กœ๋ถ€ํ„ฐ ๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€(Multivariable Linear Regression)์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด(Data Definition) ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€์™€ ๋‹ค๋ฅธ ์ ์€ ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ด์ œ 1๊ฐœ๊ฐ€ ์•„๋‹Œ 3๊ฐœ๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. 3๊ฐœ์˜ ํ€ด์ฆˆ ์ ์ˆ˜๋กœ๋ถ€ํ„ฐ ์ตœ์ข… ์ ์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ๋ฏ€๋กœ ์ด๋ฅผ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) w x + 2 2 w x + 2. ํŒŒ์ด ํ† ์น˜๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธํ•˜๊ณ  ๋žœ๋ค ์‹œ๋“œ๋ฅผ ๊ณ ์ •ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim torch.manual_seed(1) ์ด์ œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ์–ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ( ) w x + 2 2 w x + ์œ„์˜ ์‹์„ ๋ณด๋ฉด ์ด๋ฒˆ์—๋Š” ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€์™€ ๋‹ค๋ฅด๊ฒŒ์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ๋ฅผ 3๊ฐœ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ x1_train = torch.FloatTensor([[73], [93], [89], [96], [73]]) x2_train = torch.FloatTensor([[80], [88], [91], [98], [66]]) x3_train = torch.FloatTensor([[75], [93], [90], [100], [70]]) y_train = torch.FloatTensor([[152], [185], [180], [196], [142]]) ์ด์ œ ๊ฐ€์ค‘์น˜ ์™€ ํŽธํ–ฅ ๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ๋„ 3๊ฐœ ์„ ์–ธํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # ๊ฐ€์ค‘์น˜ w์™€ ํŽธํ–ฅ b ์ดˆ๊ธฐํ™” w1 = torch.zeros(1, requires_grad=True) w2 = torch.zeros(1, requires_grad=True) w3 = torch.zeros(1, requires_grad=True) b = torch.zeros(1, requires_grad=True) ์ด์ œ ๊ฐ€์„ค, ๋น„์šฉ ํ•จ์ˆ˜, ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์„ ์–ธํ•œ ํ›„์— ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ 1,000ํšŒ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. # optimizer ์„ค์ • optimizer = optim.SGD([w1, w2, w3, b], lr=1e-5) nb_epochs = 1000 for epoch in range(nb_epochs + 1): # H(x) ๊ณ„์‚ฐ hypothesis = x1_train * w1 + x2_train * w2 + x3_train * w3 + b # cost ๊ณ„์‚ฐ cost = torch.mean((hypothesis - y_train) ** 2) # cost๋กœ H(x) ๊ฐœ์„  optimizer.zero_grad() cost.backward() optimizer.step() # 100๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ if epoch % 100 == 0: print('Epoch {:4d}/{} w1: {:.3f} w2: {:.3f} w3: {:.3f} b: {:.3f} Cost: {:.6f}'.format( epoch, nb_epochs, w1.item(), w2.item(), w3.item(), b.item(), cost.item() )) ์œ„์˜ ๊ฒฝ์šฐ ๊ฐ€์„ค์„ ์„ ์–ธํ•˜๋Š” ๋ถ€๋ถ„์ธ hypothesis = x1_train * w1 + x2_train * w2 + x3_train * w3 + b์—์„œ๋„ x_train์˜ ๊ฐœ์ˆ˜๋งŒํผ w์™€ ๊ณฑํ•ด์ฃผ๋„๋ก ์ž‘์„ฑํ•ด ์ค€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ์˜€์œผ๋‹ˆ๊นŒ x1_train, x2_train, x3_train์™€ w1, w2, w3๋ฅผ ์ผ์ผ์ด ์„ ์–ธํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 1,000๊ฐœ๋ผ๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์„ ๊ณ ์ˆ˜ํ•  ๊ฒฝ์šฐ x_train1 ~ x_train1000์„ ์ „๋ถ€ ์„ ์–ธํ•˜๊ณ , w1 ~ w1000์„ ์ „๋ถ€ ์„ ์–ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์™€ ๋ณ€์ˆ˜ ์„ ์–ธ๋งŒ ์ดํ•ฉ 2,000๊ฐœ๋ฅผ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฐ€์„ค์„ ์„ ์–ธํ•˜๋Š” ๋ถ€๋ถ„์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ x_train๊ณผ w์˜ ๊ณฑ์…ˆ์ด ์ด๋ฃจ์–ด์ง€๋Š” ํ•ญ์„ 1,000๊ฐœ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ต‰์žฅํžˆ ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ํ–‰๋ ฌ ๊ณฑ์…ˆ ์—ฐ์‚ฐ(๋˜๋Š” ๋ฒกํ„ฐ์˜ ๋‚ด์ )์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ ๊ณผ์ •์—์„œ ์ด๋ฃจ์–ด์ง€๋Š” ๋ฒกํ„ฐ ์—ฐ์‚ฐ์„ ๋ฒกํ„ฐ์˜ ๋‚ด์ (Dot Product)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ํ–‰๋ ฌ ๊ณฑ์…ˆ ์—ฐ์‚ฐ ๊ณผ์ •์—์„œ ๋ฒกํ„ฐ์˜ ๋‚ด์ ์œผ๋กœ 1 ร— 7 + 2 ร— 9 + 3 ร— 11 = 58์ด ๋˜๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด ํ–‰๋ ฌ ์—ฐ์‚ฐ์ด ์–ด๋–ป๊ฒŒ ํ˜„์žฌ ๋ฐฐ์šฐ๊ณ  ์žˆ๋Š” ๊ฐ€์„ค๊ณผ ์ƒ๊ด€์ด ์žˆ๋‹ค๋Š” ๊ฑธ๊นŒ์š”? ๋ฐ”๋กœ ๊ฐ€์„ค์„ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 1. ๋ฒกํ„ฐ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ( ) w x + 2 2 w x ์œ„ ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‘ ๋ฒกํ„ฐ์˜ ๋‚ด์ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒกํ„ฐ๋ฅผ ๊ฐ๊ฐ ์™€๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋ฉด, ๊ฐ€์„ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) X x ์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ์˜€์Œ์—๋„ ์ด์ œ๋Š” ์™€๋ผ๋Š” ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜๋กœ ํ‘œํ˜„๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ดํŽด๋ณด๊ณ , ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๊ฐ€์„ค ( ) ๋ฅผ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์…€ ์ˆ˜ ์žˆ๋Š” 1๊ฐœ์˜ ๋‹จ์œ„๋ฅผ ์ƒ˜ํ”Œ(sample)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ์ƒ˜ํ”Œ์˜ ์ˆ˜๋Š” ์ด 5๊ฐœ์ž…๋‹ˆ๋‹ค. ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋ฅผ ๊ฒฐ์ •ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฐ๊ฐ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜๋ฅผ ํŠน์„ฑ(feature)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ํŠน์„ฑ์€ 3๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋…๋ฆฝ ๋ณ€์ˆ˜ ๋“ค์˜ ์ˆ˜๊ฐ€ (์ƒ˜ํ”Œ์˜ ์ˆ˜ ร— ํŠน์„ฑ์˜ ์ˆ˜) = 15๊ฐœ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋…๋ฆฝ ๋ณ€์ˆ˜ ๋“ค์„ (์ƒ˜ํ”Œ์˜ ์ˆ˜ ร— ํŠน์„ฑ์˜ ์ˆ˜)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํ•˜๋‚˜์˜ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ–‰๋ ฌ์„๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ( 11 x 12 x 13 x 21 x 22 x 23 x 31 x 32 x 33 x 41 x 42 x 43 x 51 x 52 x 53 ) ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์— ๊ฐ€์ค‘์น˜ 1 w, 3 ์„ ์›์†Œ๋กœ ํ•˜๋Š” ๋ฒกํ„ฐ๋ฅผ ๋ผ ํ•˜๊ณ  ์ด๋ฅผ ๊ณฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ( 11 x 12 x 13 x 21 x 22 x 23 x 31 x 32 x 33 x 41 x 42 x 43 x 51 x 52 x 53 ) ( 1 2 3 ) = ( 11 1 x 12 2 x 13 3 x 21 1 x 22 2 x 23 3 x 31 1 x 32 2 x 33 3 x 41 1 x 42 2 x 43 3 x 51 1 x 52 2 x 53 3 ) ์œ„์˜ ์‹์€ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) X ์ด ๊ฐ€์„ค์— ๊ฐ ์ƒ˜ํ”Œ์— ๋”ํ•ด์ง€๋Š” ํŽธํ–ฅ ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. ์ƒ˜ํ”Œ ์ˆ˜๋งŒํผ์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” ํŽธํ–ฅ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด ๋”ํ•ฉ๋‹ˆ๋‹ค. ( 11 x 12 x 13 x 21 x 22 x 23 x 31 x 32 x 33 x 41 x 42 x 43 x 51 x 52 x 53 ) ( 1 2 3 ) ( b b) = ( 11 1 x 12 2 x 13 3 b 21 1 x 22 2 x 23 3 b 31 1 x 32 2 x 33 3 b 41 1 x 42 2 x 43 3 b 51 1 x 52 2 x 53 3 b ์œ„์˜ ์‹์€ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) X + ๊ฒฐ๊ณผ์ ์œผ๋กœ ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐ€์„ค ์—ฐ์‚ฐ์„ 3๊ฐœ์˜ ๋ณ€์ˆ˜๋งŒ์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์€ ์‹์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด์ค„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค์ˆ˜์˜ ์ƒ˜ํ”Œ์˜ ๋ณ‘๋ ฌ ์—ฐ์‚ฐ์ด๋ฏ€๋กœ ์†๋„์˜ ์ด์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋ฅผ ์ฐธ๊ณ ๋กœ ํŒŒ์ด ํ† ์น˜๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 4. ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ๊ณ ๋ คํ•˜์—ฌ ํŒŒ์ด ํ† ์น˜๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ๊ณ ๋ คํ•˜์—ฌ ํŒŒ์ด ํ† ์น˜๋กœ ์žฌ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๋˜ํ•œ ํ–‰๋ ฌ๋กœ ์„ ์–ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. x_train = torch.FloatTensor([[73, 80, 75], [93, 88, 93], [89, 91, 80], [96, 98, 100], [73, 66, 70]]) y_train = torch.FloatTensor([[152], [185], [180], [196], [142]]) ์ด์ „์— x_train์„ 3๊ฐœ๋‚˜ ๊ตฌํ˜„ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋‹ค๋ฅด๊ฒŒ ์ด๋ฒˆ์—๋Š” x_train ํ•˜๋‚˜์— ๋ชจ๋“  ์ƒ˜ํ”Œ์„ ์ „๋ถ€ ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด (5 x 3) ํ–‰๋ ฌ์„ ์„ ์–ธํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. x_train๊ณผ y_train์˜ ํฌ๊ธฐ(shape)๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(x_train.shape) print(y_train.shape) torch.Size([5, 3]) torch.Size([5, 1]) ๊ฐ๊ฐ (5 ร— 3) ํ–‰๋ ฌ๊ณผ (5 ร— 1) ํ–‰๋ ฌ(๋˜๋Š” ๋ฒกํ„ฐ)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ€์ค‘์น˜ ์™€ ํŽธํ–ฅ ๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. # ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ ์„ ์–ธ W = torch.zeros((3, 1), requires_grad=True) b = torch.zeros(1, requires_grad=True) ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•  ์ ์€ ๊ฐ€์ค‘์น˜์˜ ํฌ๊ธฐ๊ฐ€ (3 ร— 1) ๋ฒกํ„ฐ๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์ด ์„ฑ๋ฆฝ๋˜๋ ค๋ฉด ๊ณฑ์…ˆ์˜ ์ขŒ์ธก์— ์žˆ๋Š” ํ–‰๋ ฌ์˜ ์—ด์˜ ํฌ๊ธฐ์™€ ์šฐ์ธก์— ์žˆ๋Š” ํ–‰๋ ฌ์˜ ํ–‰์˜ ํฌ๊ธฐ๊ฐ€ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ X_train์˜ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” (5 ร— 3)์ด๋ฉฐ, ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋Š” (3 ร— 1)์ด๋ฏ€๋กœ ๋‘ ํ–‰๋ ฌ๊ณผ ๋ฒกํ„ฐ๋Š” ํ–‰๋ ฌ๊ณฑ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ๊ฐ€์„ค์„ ์„ ์–ธํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. hypothesis = x_train.matmul(W) + b ๊ฐ€์„ค์„ ํ–‰๋ ฌ๊ณฑ์œผ๋กœ ๊ฐ„๋‹จํžˆ ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์•ž์„œ x_train๊ณผ w์˜ ๊ณฑ์…ˆ์ด ์ด๋ฃจ์–ด์ง€๋Š” ๊ฐ ํ•ญ์„ ์ „๋ถ€ ๊ธฐ์žฌํ•˜์—ฌ ๊ฐ€์„ค์„ ์„ ์–ธํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋Œ€๋น„๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์‚ฌ์šฉ์ž๊ฐ€ ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ์ˆ˜๋ฅผ ํ›„์— ์ถ”๊ฐ€์ ์œผ๋กœ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์ด๋”๋ผ๋„ ์œ„์˜ ๊ฐ€์„ค ์„ ์–ธ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ•ด์•ผ ํ•  ์ผ์€ ๋น„์šฉ ํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ •์˜ํ•˜๊ณ , ์ •ํ•ด์ง„ ์—ํฌํฌ๋งŒํผ ํ›ˆ๋ จ์„ ์ง„ํ–‰ํ•˜๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐ˜์˜ํ•œ ์ „์ฒด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. x_train = torch.FloatTensor([[73, 80, 75], [93, 88, 93], [89, 91, 80], [96, 98, 100], [73, 66, 70]]) y_train = torch.FloatTensor([[152], [185], [180], [196], [142]]) # ๋ชจ๋ธ ์ดˆ๊ธฐํ™” W = torch.zeros((3, 1), requires_grad=True) b = torch.zeros(1, requires_grad=True) # optimizer ์„ค์ • optimizer = optim.SGD([W, b], lr=1e-5) nb_epochs = 20 for epoch in range(nb_epochs + 1): # H(x) ๊ณ„์‚ฐ # ํŽธํ–ฅ b๋Š” ๋ธŒ๋กœ๋“œ ์บ์ŠคํŒ… ๋˜์–ด ๊ฐ ์ƒ˜ํ”Œ์— ๋”ํ•ด์ง‘๋‹ˆ๋‹ค. hypothesis = x_train.matmul(W) + b # cost ๊ณ„์‚ฐ cost = torch.mean((hypothesis - y_train) ** 2) # cost๋กœ H(x) ๊ฐœ์„  optimizer.zero_grad() cost.backward() optimizer.step() print('Epoch {:4d}/{} hypothesis: {} Cost: {:.6f}'.format( epoch, nb_epochs, hypothesis.squeeze().detach(), cost.item() )) 03-04 nn.Module๋กœ ๊ตฌํ˜„ํ•˜๋Š” ์„ ํ˜• ํšŒ๊ท€ ์ด์ „ ์ฑ•ํ„ฐ๊นŒ์ง€๋Š” ์„ ํ˜• ํšŒ๊ท€๋ฅผ ์ข€ ๋” ์ง์ ‘์ ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์„ค, ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ์ •์˜ํ•ด์„œ ์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํŒŒ์ด ํ† ์น˜์—์„œ ์ด๋ฏธ ๊ตฌํ˜„๋ผ ์ œ๊ณต๋˜๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์œผ๋กœ ๋” ์‰ฝ๊ฒŒ ์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ์ด nn.Linear()๋ผ๋Š” ํ•จ์ˆ˜๋กœ, ๋˜ ํ‰๊ท  ์ œ๊ณฑ์˜ค์ฐจ๊ฐ€ nn.functional.mse_loss()๋ผ๋Š” ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์ด๋ฒˆ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•  ๋‘ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ ์˜ˆ์ œ๋ฅผ ๊ฐ„๋‹จํžˆ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. import torch.nn as nn model = nn.Linear(input_dim, output_dim) import torch.nn.functional as F cost = F.mse_loss(prediction, y_train) 1. ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€ ๊ตฌํ˜„ํ•˜๊ธฐ ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F torch.manual_seed(1) ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๋ฐ์ดํ„ฐ๋Š” = x ๋ฅผ ๊ฐ€์ •๋œ ์ƒํƒœ์—์„œ ๋งŒ๋“ค์–ด์ง„ ๋ฐ์ดํ„ฐ๋กœ ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ ์ •๋‹ต์ด W=2, b=0์ž„์„ ์•Œ๊ณ  ์žˆ๋Š” ์‚ฌํƒœ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ด ๋‘ W์™€ b์˜ ๊ฐ’์„ ์ œ๋Œ€๋กœ ์ฐพ์•„๋‚ด๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. # ๋ฐ์ดํ„ฐ x_train = torch.FloatTensor([[1], [2], [3]]) y_train = torch.FloatTensor([[2], [4], [6]]) ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•˜์˜€์œผ๋‹ˆ ์ด์ œ ์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. nn.Linear()๋Š” ์ž…๋ ฅ์˜ ์ฐจ์›, ์ถœ๋ ฅ์˜ ์ฐจ์›์„ ์ธ์ˆ˜๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. # ๋ชจ๋ธ์„ ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”. ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€์ด๋ฏ€๋กœ input_dim=1, output_dim=1. model = nn.Linear(1,1) ์œ„ torch.nn.Linear ์ธ์ž๋กœ 1, 1์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ํ•˜๋‚˜์˜ ์ถœ๋ ฅ์„ ๊ฐ€์ง€๋ฏ€๋กœ, ์ž…๋ ฅ ์ฐจ์›๊ณผ ์ถœ๋ ฅ ์ฐจ์› ๋ชจ๋‘ 1์„ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. model์—๋Š” ๊ฐ€์ค‘์น˜ W์™€ ํŽธํ–ฅ b๊ฐ€ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ model.parameters()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ•œ ๋ฒˆ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(list(model.parameters())) [Parameter containing: tensor([[0.5153]], requires_grad=True), Parameter containing: tensor([-0.4414], requires_grad=True)] 2๊ฐœ์˜ ๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด W๊ณ , ๋‘ ๋ฒˆ์งธ ๊ฐ’์ด b์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐ’ ๋ชจ๋‘ ํ˜„์žฌ๋Š” ๋žœ๋ค ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๊ฐ’ ๋ชจ๋‘ ํ•™์Šต์˜ ๋Œ€์ƒ์ด๋ฏ€๋กœ requires_grad=True๊ฐ€ ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. model.parameters()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ W์™€ b๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต๋ฅ (learning rate)์€ 0.01๋กœ ์ •ํ•ฉ๋‹ˆ๋‹ค. # optimizer ์„ค์ •. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ• SGD๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  learning rate๋ฅผ ์˜๋ฏธํ•˜๋Š” lr์€ 0.01 optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ 2,000ํšŒ ๋ฐ˜๋ณต nb_epochs = 2000 for epoch in range(nb_epochs+1): # H(x) ๊ณ„์‚ฐ prediction = model(x_train) # cost ๊ณ„์‚ฐ cost = F.mse_loss(prediction, y_train) # <== ํŒŒ์ด ํ† ์น˜์—์„œ ์ œ๊ณตํ•˜๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ ํ•จ์ˆ˜ # cost๋กœ H(x) ๊ฐœ์„ ํ•˜๋Š” ๋ถ€๋ถ„ # gradient๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” optimizer.zero_grad() # ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•˜์—ฌ gradient ๊ณ„์‚ฐ cost.backward() # backward ์—ฐ์‚ฐ # W์™€ b๋ฅผ ์—…๋ฐ์ดํŠธ optimizer.step() if epoch % 100 == 0: # 100๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ print('Epoch {:4d}/{} Cost: {:.6f}'.format( epoch, nb_epochs, cost.item() )) Epoch 0/2000 Cost: 13.103540 ... ์ค‘๋žต ... Epoch 2000/2000 Cost: 0.000000 ํ•™์Šต์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Cost์˜ ๊ฐ’์ด ๋งค์šฐ ์ž‘์Šต๋‹ˆ๋‹ค. W์™€ b์˜ ๊ฐ’๋„ ์ตœ์ ํ™”๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค.์— ์ž„์˜์˜ ๊ฐ’ 4๋ฅผ ๋„ฃ์–ด ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•˜๋Š”์˜ ๊ฐ’์„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ž„์˜์˜ ์ž…๋ ฅ 4๋ฅผ ์„ ์–ธ new_var = torch.FloatTensor([[4.0]]) # ์ž…๋ ฅํ•œ ๊ฐ’ 4์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก๊ฐ’ y๋ฅผ ๋ฆฌํ„ด ๋ฐ›์•„์„œ pred_y์— ์ €์žฅ pred_y = model(new_var) # forward ์—ฐ์‚ฐ # y = 2x์ด๋ฏ€๋กœ ์ž…๋ ฅ์ด 4๋ผ๋ฉด y๊ฐ€ 8์— ๊ฐ€๊นŒ์šด ๊ฐ’์ด ๋‚˜์™€์•ผ ์ œ๋Œ€๋กœ ํ•™์Šต์ด ๋œ ๊ฒƒ print("ํ›ˆ๋ จ ํ›„ ์ž…๋ ฅ์ด 4์ผ ๋•Œ์˜ ์˜ˆ์ธก๊ฐ’ :", pred_y) ํ›ˆ๋ จ ํ›„ ์ž…๋ ฅ์ด 4์ผ ๋•Œ์˜ ์˜ˆ์ธก๊ฐ’ : tensor([[7.9989]], grad_fn=<AddmmBackward>) ์‚ฌ์‹ค ์ด ๋ฌธ์ œ์˜ ์ •๋‹ต์€ = x ๊ฐ€ ์ •๋‹ต์ด๋ฏ€๋กœ y ๊ฐ’์ด 8์— ๊ฐ€๊นŒ์šฐ๋ฉด W์™€ b์˜ ๊ฐ’์ด ์–ด๋Š ์ •๋„ ์ตœ์ ํ™”๊ฐ€ ๋œ ๊ฒƒ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์˜ˆ์ธก๋œ y ๊ฐ’์€ 7.9989๋กœ 8์— ๋งค์šฐ ๊ฐ€๊น์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ•™์Šต ํ›„์˜ W์™€ b์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(list(model.parameters())) [Parameter containing: tensor([[1.9994]], requires_grad=True), Parameter containing: tensor([0.0014], requires_grad=True)] W์˜ ๊ฐ’์ด 2์— ๊ฐ€๊น๊ณ , b์˜ ๊ฐ’์ด 0์— ๊ฐ€๊นŒ์šด ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( ) ์‹์— ์ž…๋ ฅ ๋กœ๋ถ€ํ„ฐ ์˜ˆ์ธก๋œ ๋ฅผ ์–ป๋Š” ๊ฒƒ์„ forward ์—ฐ์‚ฐ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต ์ „, prediction = model(x_train)์€ x_train์œผ๋กœ๋ถ€ํ„ฐ ์˜ˆ์ธก๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋ฏ€๋กœ forward ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ํ•™์Šต ํ›„, pred_y = model(new_var)๋Š” ์ž„์˜์˜ ๊ฐ’ new_var๋กœ๋ถ€ํ„ฐ ์˜ˆ์ธก๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋ฏ€๋กœ forward ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ํ•™์Šต ๊ณผ์ •์—์„œ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•˜์—ฌ ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์„ backward ์—ฐ์‚ฐ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. cost.backward()๋Š” ๋น„์šฉ ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ตฌํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋ฉฐ backward ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. 2. ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด์ œ nn.Linear()์™€ nn.functional.mse_loss()๋กœ ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์‚ฌ์‹ค ์ฝ”๋“œ ์ž์ฒด๋Š” ๋‹ฌ๋ผ์ง€๋Š” ๊ฑด ๊ฑฐ์˜ ์—†๋Š”๋ฐ, nn.Linear()์˜ ์ธ์ž ๊ฐ’๊ณผ ํ•™์Šต๋ฅ (learning rate)๋งŒ ์กฐ์ ˆํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F torch.manual_seed(1) ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ์–ธํ•ด ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 3๊ฐœ์˜๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๊ฐ€์„ค ์ˆ˜์‹์€ ( ) w x + 2 2 w x +์ž…๋‹ˆ๋‹ค. # ๋ฐ์ดํ„ฐ x_train = torch.FloatTensor([[73, 80, 75], [93, 88, 93], [89, 91, 90], [96, 98, 100], [73, 66, 70]]) y_train = torch.FloatTensor([[152], [185], [180], [196], [142]]) ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•˜์˜€์œผ๋‹ˆ ์ด์ œ ์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค. nn.Linear()๋Š” ์ž…๋ ฅ์˜ ์ฐจ์›, ์ถœ๋ ฅ์˜ ์ฐจ์›์„ ์ธ์ˆ˜๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. # ๋ชจ๋ธ์„ ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”. ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€์ด๋ฏ€๋กœ input_dim=3, output_dim=1. model = nn.Linear(3,1) ์œ„ torch.nn.Linear ์ธ์ž๋กœ 3, 1์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. 3๊ฐœ์˜ ์ž…๋ ฅ x์— ๋Œ€ํ•ด์„œ ํ•˜๋‚˜์˜ ์ถœ๋ ฅ y์„ ๊ฐ€์ง€๋ฏ€๋กœ, ์ž…๋ ฅ ์ฐจ์›์€ 3, ์ถœ๋ ฅ ์ฐจ์›์€ 1์„ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. model์—๋Š” 3๊ฐœ์˜ ๊ฐ€์ค‘์น˜ w์™€ ํŽธํ–ฅ b๊ฐ€ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ model.parameters()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ•œ ๋ฒˆ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(list(model.parameters())) [Parameter containing: tensor([[ 0.2975, -0.2548, -0.1119]], requires_grad=True), Parameter containing: tensor([0.2710], requires_grad=True)] ์ฒซ ๋ฒˆ์งธ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์ด 3๊ฐœ์˜ w๊ณ , ๋‘ ๋ฒˆ์งธ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์ด b์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐ’ ๋ชจ๋‘ ํ˜„์žฌ๋Š” ๋žœ๋ค ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๋ชจ๋‘ ํ•™์Šต์˜ ๋Œ€์ƒ์ด๋ฏ€๋กœ requires_grad=True๊ฐ€ ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. model.parameters()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 3๊ฐœ์˜ w์™€ b๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ํ•™์Šต๋ฅ (learning rate)์€ 0.00001๋กœ ์ •ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์ฝ”๋“œ๋กœ๋Š” 1e-5๋กœ๋„ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. 0.01๋กœ ํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” ๊ธฐ์šธ๊ธฐ๊ฐ€ ๋ฐœ์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ํ•ด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์•ž์„œ ๋ฐฐ์› ๋˜ ๋‚ด์šฉ์œผ๋กœ, ํ•™์Šต๋ฅ (learning rate)์ด ๋ชจ๋ธ์˜ ํ•„์š”ํ•œ ํฌ๊ธฐ๋ณด๋‹ค ๋†’์„ ๋•Œ, ๊ธฐ์šธ๊ธฐ๊ฐ€ ๋ฐœ์‚ฐํ•˜๋Š” ํ˜„์ƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. optimizer = torch.optim.SGD(model.parameters(), lr=1e-5) ์ดํ•˜ ์ฝ”๋“œ๋Š” ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. nb_epochs = 2000 for epoch in range(nb_epochs+1): # H(x) ๊ณ„์‚ฐ prediction = model(x_train) # model(x_train)์€ model.forward(x_train)์™€ ๋™์ผํ•จ. # cost ๊ณ„์‚ฐ cost = F.mse_loss(prediction, y_train) # <== ํŒŒ์ด ํ† ์น˜์—์„œ ์ œ๊ณตํ•˜๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ ํ•จ์ˆ˜ # cost๋กœ H(x) ๊ฐœ์„ ํ•˜๋Š” ๋ถ€๋ถ„ # gradient๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” optimizer.zero_grad() # ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•˜์—ฌ gradient ๊ณ„์‚ฐ cost.backward() # W์™€ b๋ฅผ ์—…๋ฐ์ดํŠธ optimizer.step() if epoch % 100 == 0: # 100๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ print('Epoch {:4d}/{} Cost: {:.6f}'.format( epoch, nb_epochs, cost.item() )) Epoch 0/2000 Cost: 31667.597656 ... ์ค‘๋žต ... Epoch 2000/2000 Cost: 0.199777 ํ•™์Šต์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Cost์˜ ๊ฐ’์ด ๋งค์šฐ ์ž‘์Šต๋‹ˆ๋‹ค. 3๊ฐœ์˜ w์™€ b์˜ ๊ฐ’๋„ ์ตœ์ ํ™”๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค.์— ์ž„์˜์˜ ์ž…๋ ฅ [73, 80, 75]๋ฅผ ๋„ฃ์–ด ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•˜๋Š”์˜ ๊ฐ’์„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ž„์˜์˜ ์ž…๋ ฅ [73, 80, 75]๋ฅผ ์„ ์–ธ new_var = torch.FloatTensor([[73, 80, 75]]) # ์ž…๋ ฅํ•œ ๊ฐ’ [73, 80, 75]์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก๊ฐ’ y๋ฅผ ๋ฆฌํ„ด ๋ฐ›์•„์„œ pred_y์— ์ €์žฅ pred_y = model(new_var) print("ํ›ˆ๋ จ ํ›„ ์ž…๋ ฅ์ด 73, 80, 75์ผ ๋•Œ์˜ ์˜ˆ์ธก๊ฐ’ :", pred_y) ํ›ˆ๋ จ ํ›„ ์ž…๋ ฅ์ด 73, 80, 75์ผ ๋•Œ์˜ ์˜ˆ์ธก๊ฐ’ : tensor([[151.2305]], grad_fn=<AddmmBackward>) ์‚ฌ์‹ค 3๊ฐœ์˜ ๊ฐ’ 73, 80, 75๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉ๋˜์—ˆ๋˜ ๊ฐ’์ž…๋‹ˆ๋‹ค. ๋‹น์‹œ y์˜ ๊ฐ’์€ 152์˜€๋Š”๋ฐ, ํ˜„์žฌ ์˜ˆ์ธก๊ฐ’์ด 151์ด ๋‚˜์˜จ ๊ฒƒ์œผ๋กœ ๋ณด์•„ ์–ด๋Š ์ •๋„๋Š” 3๊ฐœ์˜ w์™€ b์˜ ๊ฐ’์ด ์ตœ์ ํ™”๋œ ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด์ œ ํ•™์Šต ํ›„์˜ 3๊ฐœ์˜ w์™€ b์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(list(model.parameters())) [Parameter containing: tensor([[0.9778, 0.4539, 0.5768]], requires_grad=True), Parameter containing: tensor([0.2802], requires_grad=True)] https://tutorials.pytorch.kr/beginner/pytorch_with_examples.html#tensorflow-static-graph https://www.geeksforgeeks.org/linear-regression-using-pytorch/ https://www.yceffort.kr/2019/02/19/pytorch-02-linear-regression/ 03-05 ํด๋ž˜์Šค๋กœ ํŒŒ์ด ํ† ์น˜ ๋ชจ๋ธ ๊ตฌํ˜„ํ•˜๊ธฐ ํŒŒ์ด ํ† ์น˜์˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ตฌํ˜„์ฒด๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•  ๋•Œ ํด๋ž˜์Šค(Class)๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ์„ ํ˜• ํšŒ๊ท€๋ฅผ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ์™€ ๋‹ค๋ฅธ ์ ์€ ์˜ค์ง ํด๋ž˜์Šค๋กœ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ–ˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. 1. ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์•ž์„œ ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. # ๋ชจ๋ธ์„ ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”. ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€์ด๋ฏ€๋กœ input_dim=1, output_dim=1. model = nn.Linear(1,1) ์ด๋ฅผ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. class LinearRegressionModel(nn.Module): # torch.nn.Module์„ ์ƒ์†๋ฐ›๋Š” ํŒŒ์ด์ฌ ํด๋ž˜์Šค def __init__(self): # super().__init__() self.linear = nn.Linear(1, 1) # ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€์ด๋ฏ€๋กœ input_dim=1, output_dim=1. def forward(self, x): return self.linear(x) model = LinearRegressionModel() ์œ„์™€ ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ ๊ตฌํ˜„<NAME>์€ ๋Œ€๋ถ€๋ถ„์˜ ํŒŒ์ด ํ† ์น˜ ๊ตฌํ˜„์ฒด์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ˜๋“œ์‹œ ์ˆ™์ง€ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค(class) ํ˜•ํƒœ์˜ ๋ชจ๋ธ์€ nn.Module ์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  __init__()์—์„œ ๋ชจ๋ธ์˜ ๊ตฌ์กฐ์™€ ๋™์ž‘์„ ์ •์˜ํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํŒŒ์ด์ฌ์—์„œ ๊ฐ์ฒด๊ฐ€ ๊ฐ–๋Š” ์†์„ฑ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• ๋กœ, ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. super() ํ•จ์ˆ˜๋ฅผ ๋ถ€๋ฅด๋ฉด ์—ฌ๊ธฐ์„œ ๋งŒ๋“  ํด๋ž˜์Šค๋Š” nn.Module ํด๋ž˜์Šค์˜ ์†์„ฑ๋“ค์„ ๊ฐ€์ง€๊ณ  ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. foward() ํ•จ์ˆ˜๋Š” ๋ชจ๋ธ์ด ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ forward ์—ฐ์‚ฐ์„ ์ง„ํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด forward() ํ•จ์ˆ˜๋Š” model ๊ฐ์ฒด๋ฅผ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜๋ฉด ์ž๋™์œผ๋กœ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด model ์ด๋ž€ ์ด๋ฆ„์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ ํ›„, model(์ž…๋ ฅ ๋ฐ์ดํ„ฐ)์™€ ๊ฐ™์€<NAME>์œผ๋กœ ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ž๋™์œผ๋กœ forward ์—ฐ์‚ฐ์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ( ) ์‹์— ์ž…๋ ฅ ๋กœ๋ถ€ํ„ฐ ์˜ˆ์ธก๋œ ๋ฅผ ์–ป๋Š” ๊ฒƒ์„ forward ์—ฐ์‚ฐ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. # ๋ชจ๋ธ์„ ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”. ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€์ด๋ฏ€๋กœ input_dim=3, output_dim=1. model = nn.Linear(3,1) ์ด๋ฅผ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. class MultivariateLinearRegressionModel(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(3, 1) # ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€์ด๋ฏ€๋กœ input_dim=3, output_dim=1. def forward(self, x): return self.linear(x) model = MultivariateLinearRegressionModel() 2. ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด์ œ ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ฌ๋ผ์ง„ ์ ์€ ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๋Š” ์ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ฝ”๋“œ๋Š” ์ „๋ถ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F torch.manual_seed(1) # ๋ฐ์ดํ„ฐ x_train = torch.FloatTensor([[1], [2], [3]]) y_train = torch.FloatTensor([[2], [4], [6]]) class LinearRegressionModel(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(1, 1) def forward(self, x): return self.linear(x) model = LinearRegressionModel() # optimizer ์„ค์ •. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ• SGD๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  learning rate๋ฅผ ์˜๋ฏธํ•˜๋Š” lr์€ 0.01 optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ 2,000ํšŒ ๋ฐ˜๋ณต nb_epochs = 2000 for epoch in range(nb_epochs+1): # H(x) ๊ณ„์‚ฐ prediction = model(x_train) # cost ๊ณ„์‚ฐ cost = F.mse_loss(prediction, y_train) # <== ํŒŒ์ด ํ† ์น˜์—์„œ ์ œ๊ณตํ•˜๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ ํ•จ์ˆ˜ # cost๋กœ H(x) ๊ฐœ์„ ํ•˜๋Š” ๋ถ€๋ถ„ # gradient๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” optimizer.zero_grad() # ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•˜์—ฌ gradient ๊ณ„์‚ฐ cost.backward() # backward ์—ฐ์‚ฐ # W์™€ b๋ฅผ ์—…๋ฐ์ดํŠธ optimizer.step() if epoch % 100 == 0: # 100๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ print('Epoch {:4d}/{} Cost: {:.6f}'.format( epoch, nb_epochs, cost.item() )) 3. ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด์ œ ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ฌ๋ผ์ง„ ์ ์€ ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๋Š” ์ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ฝ”๋“œ๋Š” ์ „๋ถ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F torch.manual_seed(1) # ๋ฐ์ดํ„ฐ x_train = torch.FloatTensor([[73, 80, 75], [93, 88, 93], [89, 91, 90], [96, 98, 100], [73, 66, 70]]) y_train = torch.FloatTensor([[152], [185], [180], [196], [142]]) class MultivariateLinearRegressionModel(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(3, 1) # ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€์ด๋ฏ€๋กœ input_dim=3, output_dim=1. def forward(self, x): return self.linear(x) model = MultivariateLinearRegressionModel() optimizer = torch.optim.SGD(model.parameters(), lr=1e-5) nb_epochs = 2000 for epoch in range(nb_epochs+1): # H(x) ๊ณ„์‚ฐ prediction = model(x_train) # model(x_train)์€ model.forward(x_train)์™€ ๋™์ผํ•จ. # cost ๊ณ„์‚ฐ cost = F.mse_loss(prediction, y_train) # <== ํŒŒ์ด ํ† ์น˜์—์„œ ์ œ๊ณตํ•˜๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ ํ•จ์ˆ˜ # cost๋กœ H(x) ๊ฐœ์„ ํ•˜๋Š” ๋ถ€๋ถ„ # gradient๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” optimizer.zero_grad() # ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•˜์—ฌ gradient ๊ณ„์‚ฐ cost.backward() # W์™€ b๋ฅผ ์—…๋ฐ์ดํŠธ optimizer.step() if epoch % 100 == 0: # 100๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ print('Epoch {:4d}/{} Cost: {:.6f}'.format( epoch, nb_epochs, cost.item() )) 03-06 ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์™€ ๋ฐ์ดํ„ฐ ๋กœ๋“œ(Mini Batch and Data Load) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šฐ๋Š” ๋‚ด์šฉ์€ ์„ ํ˜• ํšŒ๊ท€์— ํ•œ์ •๋˜๋Š” ๋‚ด์šฉ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•(Minibatch Gradient Descent)์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์™€ ๋ฐฐ์น˜ ํฌ๊ธฐ(Mini Batch and Batch Size) ์•ž์„œ ๋ฐฐ์šด ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. x_train = torch.FloatTensor([[73, 80, 75], [93, 88, 93], [89, 91, 90], [96, 98, 100], [73, 66, 70]]) y_train = torch.FloatTensor([[152], [185], [180], [196], [142]]) ์œ„ ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋Š” 5๊ฐœ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ํ–‰๋ ฌ๋กœ ์„ ์–ธํ•˜์—ฌ ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œ„ ๋ฐ์ดํ„ฐ๋Š” ํ˜„์—…์—์„œ ๋‹ค๋ฃจ๊ฒŒ ๋˜๋Š” ๋ฐฉ๋Œ€ํ•œ ์–‘์˜ ๋ฐ์ดํ„ฐ์— ๋น„ํ•˜๋ฉด ๊ต‰์žฅํžˆ ์ ์€ ์–‘์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋ฐ์ดํ„ฐ๊ฐ€ ์ˆ˜์‹ญ๋งŒ ๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ๋Š๋ฆด ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋งŽ์€ ๊ณ„์‚ฐ๋Ÿ‰์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ •๋ง ์–ด์ฉŒ๋ฉด ๋ฉ”๋ชจ๋ฆฌ์˜ ํ•œ๊ณ„๋กœ ๊ณ„์‚ฐ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋„ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋” ์ž‘์€ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด์„œ ํ•ด๋‹น ๋‹จ์œ„๋กœ ํ•™์Šตํ•˜๋Š” ๊ฐœ๋…์ด ๋‚˜์˜ค๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‹จ์œ„๋ฅผ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜(Mini Batch)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ํ•™์Šต์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋งŒํผ๋งŒ ๊ฐ€์ ธ๊ฐ€์„œ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ๋Œ€ํ•œ ๋น„์šฉ(cost)๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ฅผ ๊ฐ€์ ธ๊ฐ€์„œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๊นŒ์ง€ ์ด๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ•™์Šต์ด 1ํšŒ ๋๋‚˜๋ฉด 1 ์—ํฌํฌ(Epoch)๊ฐ€ ๋๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—ํฌํฌ(Epoch)๋Š” ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•™์Šต์— ํ•œ ๋ฒˆ ์‚ฌ์šฉ๋œ ์ฃผ๊ธฐ๋ฅผ ๋งํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ํ•™์Šต์—์„œ๋Š” ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์˜ ๊ฐœ์ˆ˜๋งŒํผ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•ด์•ผ ์ „์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ ํ•œ ๋ฒˆ ์ „๋ถ€ ์‚ฌ์šฉ๋˜์–ด 1 ์—ํฌํฌ(Epoch)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์˜ ๊ฐœ์ˆ˜๋Š” ๊ฒฐ๊ตญ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์˜ ํฌ๊ธฐ๋ฅผ ๋ช‡์œผ๋กœ ํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ์„œ ๋‹ฌ๋ผ์ง€๋Š”๋ฐ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์˜ ํฌ๊ธฐ๋ฅผ ๋ฐฐ์น˜ ํฌ๊ธฐ(batch size)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ•œ ๋ฒˆ์— ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ '๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•'์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ '๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•'์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ํ•  ๋•Œ, ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๊ฐ€์ค‘์น˜ ๊ฐ’์ด ์ตœ์ ๊ฐ’์— ์ˆ˜๋ ดํ•˜๋Š” ๊ณผ์ •์ด ๋งค์šฐ ์•ˆ์ •์ ์ด์ง€๋งŒ, ๊ณ„์‚ฐ๋Ÿ‰์ด ๋„ˆ๋ฌด ๋งŽ์ด ๋“ญ๋‹ˆ๋‹ค. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ํ•  ๋•Œ, ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ์ผ๋ถ€๋งŒ์„ ๋ณด๊ณ  ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์ตœ์ ๊ฐ’์œผ๋กœ ์ˆ˜๋ ดํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ฐ’์ด ์กฐ๊ธˆ ํ—ค๋งค๊ธฐ๋„ ํ•˜์ง€๋งŒ ํ›ˆ๋ จ ์†๋„๊ฐ€ ๋น ๋ฆ…๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” ๋ณดํ†ต 2์˜ ์ œ๊ณฑ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ex) 2, 4, 8, 16, 32, 64... ๊ทธ ์ด์œ ๋Š” CPU์™€ GPU์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ 2์˜ ๋ฐฐ์ˆ˜์ด๋ฏ€๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 2์˜ ์ œ๊ณฑ์ˆ˜์ผ ๊ฒฝ์šฐ์— ๋ฐ์ดํ„ฐ ์†ก์ˆ˜์‹ ์˜ ํšจ์œจ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ์ดํ„ฐ๋ ˆ์ด์…˜(Iteration) ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์™€ ๋ฐฐ์น˜ ํฌ๊ธฐ์˜ ์ •์˜์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜์˜€๋‹ค๋ฉด ์ดํ„ฐ๋ ˆ์ด์…˜(iteration)์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์—ํฌํฌ์™€ ๋ฐฐ์น˜ ํฌ๊ธฐ์™€ ์ดํ„ฐ๋ ˆ์ด์…˜์˜ ๊ด€๊ณ„๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์˜ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ดํ„ฐ๋ ˆ์ด์…˜์€ ํ•œ ๋ฒˆ์˜ ์—ํฌํฌ ๋‚ด์—์„œ ์ด๋ฃจ์–ด์ง€๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์ธ ๊ฐ€์ค‘์น˜ ์™€์˜ ์—…๋ฐ์ดํŠธ ํšŸ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ „์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ 2,000์ผ ๋•Œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 200์œผ๋กœ ํ•œ๋‹ค๋ฉด ์ดํ„ฐ๋ ˆ์ด์…˜์˜ ์ˆ˜๋Š” ์ด 10๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ•œ ๋ฒˆ์˜ ์—ํฌํฌ ๋‹น ๋งค๊ฐœ๋ณ€์ˆ˜ ์—…๋ฐ์ดํŠธ๊ฐ€ 10๋ฒˆ ์ด๋ฃจ์–ด์ง์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ํ•™์Šต์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ํŒŒ์ด ํ† ์น˜์˜ ๋„๊ตฌ๋“ค์„ ์•Œ์•„๋ด…์‹œ๋‹ค. 3. ๋ฐ์ดํ„ฐ ๋กœ๋“œํ•˜๊ธฐ(Data Load) ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ข€ ๋” ์‰ฝ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ์œ ์šฉํ•œ ๋„๊ตฌ๋กœ์„œ ๋ฐ์ดํ„ฐ ์…‹(Dataset)๊ณผ ๋ฐ์ดํ„ฐ ๋กœ๋”(DataLoader)๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ํ•™์Šต, ๋ฐ์ดํ„ฐ ์…”ํ”Œ(shuffle), ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๊นŒ์ง€ ๊ฐ„๋‹จํžˆ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ Dataset์„ ์ •์˜ํ•˜๊ณ , ์ด๋ฅผ DataLoader์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Dataset์„ ์ปค์Šคํ…€ ํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ํ…์„œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ Dataset์˜ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ๋Š” TensorDataset์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์Šต์„ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•„์š”ํ•œ ํŒŒ์ด ํ† ์น˜์˜ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F TensorDataset๊ณผ DataLoader๋ฅผ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. from torch.utils.data import TensorDataset # ํ…์„œ ๋ฐ์ดํ„ฐ ์…‹ from torch.utils.data import DataLoader # ๋ฐ์ดํ„ฐ ๋กœ๋” TensorDataset์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. ํ…์„œ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. x_train = torch.FloatTensor([[73, 80, 75], [93, 88, 93], [89, 91, 90], [96, 98, 100], [73, 66, 70]]) y_train = torch.FloatTensor([[152], [185], [180], [196], [142]]) ์ด์ œ ์ด๋ฅผ TensorDataset์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  dataset์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. dataset = TensorDataset(x_train, y_train) ํŒŒ์ด ํ† ์น˜์˜ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด ๋ฐ์ดํ„ฐ ๋กœ๋”๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋กœ๋”๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 2๊ฐœ์˜ ์ธ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ํ•˜๋‚˜๋Š” ๋ฐ์ดํ„ฐ ์…‹, ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์˜ ํฌ๊ธฐ๋Š” ํ†ต์ƒ์ ์œผ๋กœ 2์˜ ๋ฐฐ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (ex) 64, 128, 256...) ๊ทธ๋ฆฌ๊ณ  ์ถ”๊ฐ€์ ์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์ธ์ž๋กœ shuffle์ด ์žˆ์Šต๋‹ˆ๋‹ค. shuffle=True๋ฅผ ์„ ํƒํ•˜๋ฉด Epoch๋งˆ๋‹ค ๋ฐ์ดํ„ฐ ์…‹์„ ์„ž์–ด์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•™์Šต๋˜๋Š” ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. ์‚ฌ๋žŒ๋„ ๊ฐ™์€ ๋ฌธ์ œ์ง€๋ฅผ ๊ณ„์† ํ’€๋ฉด ์–ด๋Š ์ˆœ๊ฐ„ ๋ฌธ์ œ์˜ ์ˆœ์„œ์— ์ต์ˆ™ํ•ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์–ด๋–ค ๋ฌธ์ œ์ง€์˜ 12๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ, '13๋ฒˆ ๋ฌธ์ œ๊ฐ€ ๋ญ”์ง€๋Š” ๊ธฐ์–ต์€ ์•ˆ ๋‚˜์ง€๋งŒ ์–ด์ œ ํ’€์—ˆ๋˜ ๊ธฐ์–ต์œผ๋กœ ์ •๋‹ต์€ 5๋ฒˆ์ด์—ˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ' ํ•˜๋ฉด์„œ ๋ฌธ์ œ ์ž์ฒด๋ณด๋‹จ ์ˆœ์„œ์— ์ต์ˆ™ํ•ด์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ ๋ฌธ์ œ์ง€๋ฅผ ํ’€ ๋•Œ๋งˆ๋‹ค ๋ฌธ์ œ ์ˆœ์„œ๋ฅผ ๋žœ๋ค์œผ๋กœ ๋ฐ”๊พธ๋ฉด ๋„์›€์ด ๋  ๊ฒ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋ธ์ด ๋ฐ์ดํ„ฐ ์…‹์˜ ์ˆœ์„œ์— ์ต์ˆ™ํ•ด์ง€๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜์—ฌ ํ•™์Šตํ•  ๋•Œ๋Š” ์ด ์˜ต์…˜์„ True๋ฅผ ์ฃผ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. dataloader = DataLoader(dataset, batch_size=2, shuffle=True) ์ด์ œ ๋ชจ๋ธ๊ณผ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. model = nn.Linear(3,1) optimizer = torch.optim.SGD(model.parameters(), lr=1e-5) ์ด์ œ ํ›ˆ๋ จ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” batch_idx์™€ samples๋ฅผ ์ฃผ์„ ์ฒ˜๋ฆฌํ–ˆ๋Š”๋ฐ ์–ด๋–ค ์‹์œผ๋กœ ํ›ˆ๋ จ๋˜๊ณ  ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์ฃผ์„ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ œํ•˜๊ณ  ํ›ˆ๋ จ์‹œ์ผœ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. nb_epochs = 20 for epoch in range(nb_epochs + 1): for batch_idx, samples in enumerate(dataloader): # print(batch_idx) # print(samples) x_train, y_train = samples # H(x) ๊ณ„์‚ฐ prediction = model(x_train) # cost ๊ณ„์‚ฐ cost = F.mse_loss(prediction, y_train) # cost๋กœ H(x) ๊ณ„์‚ฐ optimizer.zero_grad() cost.backward() optimizer.step() print('Epoch {:4d}/{} Batch {}/{} Cost: {:.6f}'.format( epoch, nb_epochs, batch_idx+1, len(dataloader), cost.item() )) Epoch 0/20 Batch 1/3 Cost: 26085.919922 Epoch 0/20 Batch 2/3 Cost: 3660.022949 Epoch 0/20 Batch 3/3 Cost: 2922.390869 ... ์ค‘๋žต ... Epoch 20/20 Batch 1/3 Cost: 6.315856 Epoch 20/20 Batch 2/3 Cost: 13.519956 Epoch 20/20 Batch 3/3 Cost: 4.262849 Cost์˜ ๊ฐ’์ด ์ ์ฐจ ์ž‘์•„์ง‘๋‹ˆ๋‹ค. (์‚ฌ์‹ค ์•„์ง ์—ํฌํฌ๋ฅผ ๋” ๋Š˜๋ ค์„œ ํ›ˆ๋ จํ•˜๋ฉด Cost์˜ ๊ฐ’์ด ๋” ์ž‘์•„์งˆ ์—ฌ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ํฌํฌ๋ฅผ ๋Š˜๋ ค์„œ๋„ ํ›ˆ๋ จํ•ด ๋ณด์„ธ์š”.) ์ด์ œ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์ž„์˜์˜ ๊ฐ’์„ ๋„ฃ์–ด ์˜ˆ์ธก๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # ์ž„์˜์˜ ์ž…๋ ฅ [73, 80, 75]๋ฅผ ์„ ์–ธ new_var = torch.FloatTensor([[73, 80, 75]]) # ์ž…๋ ฅํ•œ ๊ฐ’ [73, 80, 75]์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก๊ฐ’ y๋ฅผ ๋ฆฌํ„ด ๋ฐ›์•„์„œ pred_y์— ์ €์žฅ pred_y = model(new_var) print("ํ›ˆ๋ จ ํ›„ ์ž…๋ ฅ์ด 73, 80, 75์ผ ๋•Œ์˜ ์˜ˆ์ธก๊ฐ’ :", pred_y) ํ›ˆ๋ จ ํ›„ ์ž…๋ ฅ์ด 73, 80, 75์ผ ๋•Œ์˜ ์˜ˆ์ธก๊ฐ’ : tensor([[154.3850]], grad_fn=<AddmmBackward>) ๋ฐฐ์น˜ ํฌ๊ธฐ ์ด์•ผ๊ธฐ : https://hongdoki.github.io/2017/10/07/optimization-difficulty-and-generlization-performance-as-batch-size-increases.html ํด๋ž˜์Šค๋กœ ํŒŒ์ด ํ† ์น˜ ๋ชจ๋ธ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๊ณผ ๋ฐ์ดํ„ฐ ๋กœ๋“œ์— ๋Œ€ํ•œ ์„ค๋ช… : https://www.youtube.com/watch? v=KXiDzNai9tI 03-07 ์ปค์Šคํ…€ ๋ฐ์ดํ„ฐ ์…‹(Custom Dataset) ์•ž ๋‚ด์šฉ์„ ์ž ๊น ๋ณต์Šตํ•ด ๋ด…์‹œ๋‹ค. ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ๋ฐ์ดํ„ฐ ์…‹์„ ์ข€ ๋” ์‰ฝ๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋„๋ก ์œ ์šฉํ•œ ๋„๊ตฌ๋กœ์„œ torch.utils.data.Dataset๊ณผ torch.utils.data.DataLoader๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ํ•™์Šต, ๋ฐ์ดํ„ฐ ์…”ํ”Œ(shuffle), ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๊นŒ์ง€ ๊ฐ„๋‹จํžˆ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ Dataset์„ ์ •์˜ํ•˜๊ณ , ์ด๋ฅผ DataLoader์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 1. ์ปค์Šคํ…€ ๋ฐ์ดํ„ฐ ์…‹(Custom Dataset) ๊ทธ๋Ÿฐ๋ฐ torch.utils.data.Dataset์„ ์ƒ์†๋ฐ›์•„ ์ง์ ‘ ์ปค์Šคํ…€ ๋ฐ์ดํ„ฐ ์…‹(Custom Dataset)์„ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. torch.utils.data.Dataset์€ ํŒŒ์ด ํ† ์น˜์—์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ์ œ๊ณตํ•˜๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. Dataset์„ ์ƒ์†๋ฐ›์•„ ๋‹ค์Œ ๋ฉ”์„œ๋“œ๋“ค์„ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜์—ฌ ์ปค์Šคํ…€ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ปค์Šคํ…€ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค ๋•Œ, ์ผ๋‹จ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ผˆ๋Œ€๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•„์š”ํ•œ ๊ธฐ๋ณธ์ ์ธ define์€ 3๊ฐœ์ž…๋‹ˆ๋‹ค. class CustomDataset(torch.utils.data.Dataset): def __init__(self): def __len__(self): def __getitem__(self, idx): ์ด๋ฅผ ์ข€ ๋” ์ž์„ธํžˆ ๋ด…์‹œ๋‹ค. class CustomDataset(torch.utils.data.Dataset): def __init__(self): ๋ฐ์ดํ„ฐ ์…‹์˜ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„ def __len__(self): ๋ฐ์ดํ„ฐ ์…‹์˜ ๊ธธ์ด. ์ฆ‰, ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜๋ฅผ ์ ์–ด์ฃผ๋Š” ๋ถ€๋ถ„ def __getitem__(self, idx): ๋ฐ์ดํ„ฐ ์…‹์—์„œ ํŠน์ • 1๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜ len(dataset)์„ ํ–ˆ์„ ๋•Œ ๋ฐ์ดํ„ฐ ์…‹์˜ ํฌ๊ธฐ๋ฅผ ๋ฆฌํ„ดํ•  len dataset[i]์„ ํ–ˆ์„ ๋•Œ i ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ๊ฐ€์ ธ์˜ค๋„๋ก ํ•˜๋Š” ์ธ๋ฑ์‹ฑ์„ ์œ„ํ•œ get_item 2. ์ปค์Šคํ…€ ๋ฐ์ดํ„ฐ ์…‹(Custom Dataset)์œผ๋กœ ์„ ํ˜• ํšŒ๊ท€ ๊ตฌํ˜„ํ•˜๊ธฐ import torch import torch.nn.functional as F from torch.utils.data import Dataset from torch.utils.data import DataLoader # Dataset ์ƒ์† class CustomDataset(Dataset): def __init__(self): self.x_data = [[73, 80, 75], [93, 88, 93], [89, 91, 90], [96, 98, 100], [73, 66, 70]] self.y_data = [[152], [185], [180], [196], [142]] # ์ด ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ด def __len__(self): return len(self.x_data) # ์ธ๋ฑ์Šค๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ทธ์— ๋งคํ•‘๋˜๋Š” ์ž…์ถœ๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด ํ† ์น˜์˜ Tensor ํ˜•ํƒœ๋กœ ๋ฆฌํ„ด def __getitem__(self, idx): x = torch.FloatTensor(self.x_data[idx]) y = torch.FloatTensor(self.y_data[idx]) return x, y dataset = CustomDataset() dataloader = DataLoader(dataset, batch_size=2, shuffle=True) model = torch.nn.Linear(3,1) optimizer = torch.optim.SGD(model.parameters(), lr=1e-5) nb_epochs = 20 for epoch in range(nb_epochs + 1): for batch_idx, samples in enumerate(dataloader): # print(batch_idx) # print(samples) x_train, y_train = samples # H(x) ๊ณ„์‚ฐ prediction = model(x_train) # cost ๊ณ„์‚ฐ cost = F.mse_loss(prediction, y_train) # cost๋กœ H(x) ๊ณ„์‚ฐ optimizer.zero_grad() cost.backward() optimizer.step() print('Epoch {:4d}/{} Batch {}/{} Cost: {:.6f}'.format( epoch, nb_epochs, batch_idx+1, len(dataloader), cost.item() )) Epoch 0/20 Batch 1/3 Cost: 29410.156250 Epoch 0/20 Batch 2/3 Cost: 7150.685059 Epoch 0/20 Batch 3/3 Cost: 3482.803467 ... ์ค‘๋žต ... Epoch 20/20 Batch 1/3 Cost: 0.350531 Epoch 20/20 Batch 2/3 Cost: 0.653316 Epoch 20/20 Batch 3/3 Cost: 0.010318 # ์ž„์˜์˜ ์ž…๋ ฅ [73, 80, 75]๋ฅผ ์„ ์–ธ new_var = torch.FloatTensor([[73, 80, 75]]) # ์ž…๋ ฅํ•œ ๊ฐ’ [73, 80, 75]์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก๊ฐ’ y๋ฅผ ๋ฆฌํ„ด ๋ฐ›์•„์„œ pred_y์— ์ €์žฅ pred_y = model(new_var) print("ํ›ˆ๋ จ ํ›„ ์ž…๋ ฅ์ด 73, 80, 75์ผ ๋•Œ์˜ ์˜ˆ์ธก๊ฐ’ :", pred_y) ํ›ˆ๋ จ ํ›„ ์ž…๋ ฅ์ด 73, 80, 75์ผ ๋•Œ์˜ ์˜ˆ์ธก๊ฐ’ : tensor([[151.2319]], grad_fn=<AddmmBackward>) 03-08 ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ ๋ณต์Šตํ•˜๊ธฐ ์•ž์„œ ๋…๋ฆฝ ๋ณ€์ˆ˜ ๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ์ดํ›„์— ๋ฐฐ์šฐ๊ฒŒ ๋  ์‹ค์Šต์ธ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ๋Š” ์ข…์† ๋ณ€์ˆ˜์˜ ์ข…๋ฅ˜๋„ 3๊ฐœ ์ด์ƒ์ด ๋˜๋ฉด์„œ ๋”์šฑ ๋ณต์žกํ•ด์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ์‹๋“ค์ด ๊ฒน๊ฒน์ด ๋ˆ„์ ๋˜๋ฉด ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ฐœ๋…์ด ๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ฐ ๋ณ€์ˆ˜๋“ค์˜ ์—ฐ์‚ฐ์„ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ์ดํ„ฐ์™€ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋กœ๋ถ€ํ„ฐ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ, ๋” ๋‚˜์•„๊ฐ€ ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ์‚ฐ์ •ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ํ…์„œ ์กฐ์ž‘ํ•˜๊ธฐ ์‹ค์Šต์„ ํ†ตํ•ด์„œ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์— ๋Œ€ํ•ด์„œ ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ธฐ๋ณธ์ ์ธ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ๋ณต์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ๊ณผ ํ…์„œ ๋ฒกํ„ฐ๋Š” ํฌ๊ธฐ์™€ ๋ฐฉํ–ฅ์„ ๊ฐ€์ง„ ์–‘์ž…๋‹ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ๋‚˜์—ด๋œ ํ˜•์ƒ์ด๋ฉฐ ํŒŒ์ด์ฌ์—์„œ๋Š” 1์ฐจ์› ๋ฐฐ์—ด ๋˜๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ํ–‰๋ ฌ์€ ํ–‰๊ณผ ์—ด์„ ๊ฐ€์ง€๋Š” 2์ฐจ์› ํ˜•์ƒ์„ ๊ฐ€์ง„ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” 2์ฐจ์› ๋ฐฐ์—ด๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋กœ์ค„์„ ํ–‰(row)๋ผ๊ณ  ํ•˜๋ฉฐ, ์„ธ๋กœ์ค„์„ ์—ด(column)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3์ฐจ์›๋ถ€ํ„ฐ๋Š” ์ฃผ๋กœ ํ…์„œ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ํ…์„œ๋Š” ํŒŒ์ด์ฌ์—์„œ๋Š” 3์ฐจ์› ์ด์ƒ์˜ ๋ฐฐ์—ด๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. 2. ํ…์„œ(Tensor) ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ๋ณต์žกํ•œ ๋ชจ๋ธ ๋‚ด์˜ ์—ฐ์‚ฐ์„ ์ฃผ๋กœ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” ํ–‰๋ ฌ ์—ฐ์‚ฐ์ด๋ž€ ๋‹จ์ˆœํžˆ 2์ฐจ์› ๋ฐฐ์—ด์„ ํ†ตํ•œ ํ–‰๋ ฌ ์—ฐ์‚ฐ๋งŒ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹์˜ ์ž…, ์ถœ๋ ฅ์ด ๋ณต์žกํ•ด์ง€๋ฉด 3์ฐจ์› ํ…์„œ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์ˆ˜๋กœ ์š”๊ตฌ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ ์ค‘ ํ•˜๋‚˜์ธ RNN์—์„œ๋Š” 3์ฐจ์› ํ…์„œ์— ๋Œ€ํ•œ ๊ฐœ๋… ์ดํ•ด ์—†์ด๋Š” ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Numpy๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์„œ๋ฅผ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import numpy as np 1) 0์ฐจ์› ํ…์„œ(์Šค์นผ๋ผ) ์Šค์นผ๋ผ๋Š” ํ•˜๋‚˜์˜ ์‹ค์ˆซ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ 0์ฐจ์› ํ…์„œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฐจ์›์„ ์˜์–ด๋กœ Dimension์ด๋ผ๊ณ  ํ•˜๋ฏ€๋กœ 0D ํ…์„œ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. d = np.array(5) print('ํ…์„œ์˜ ์ฐจ์› :',d.ndim) print('ํ…์„œ์˜ ํฌ๊ธฐ(shape) :',d.shape) ํ…์„œ์˜ ์ฐจ์› : 0 ํ…์„œ์˜ ํฌ๊ธฐ(shape) : () Numpy์˜ ndim์„ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ ๋‚˜์˜ค๋Š” ๊ฐ’์— ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. ndim์„ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ ๋‚˜์˜ค๋Š” ๊ฐ’์„ ์šฐ๋ฆฌ๋Š” ์ถ•(axis)์˜ ๊ฐœ์ˆ˜ ๋˜๋Š” ํ…์„œ์˜ ์ฐจ์›์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ ์ด ๋‘ ์šฉ์–ด๋ฅผ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. 2) 1์ฐจ์› ํ…์„œ(๋ฒกํ„ฐ) ์ˆซ์ž๋ฅผ ๋ฐฐ์—ดํ•œ ๊ฒƒ์„ ๋ฒกํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฒกํ„ฐ๋Š” 1์ฐจ์› ํ…์„œ์ž…๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ๋ฒกํ„ฐ์—์„œ๋„ ์ฐจ์›์ด๋ผ๋Š” ์šฉ์–ด๋ฅผ ์“ฐ๋Š”๋ฐ, ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ ํ…์„œ์˜ ์ฐจ์›์€ ๋‹ค๋ฅธ ๊ฐœ๋…์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ๋Š” 4์ฐจ์› ๋ฒกํ„ฐ์ด์ง€๋งŒ, 1์ฐจ์› ํ…์„œ์ž…๋‹ˆ๋‹ค. 1D ํ…์„œ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. d = np.array([1, 2, 3, 4]) print('ํ…์„œ์˜ ์ฐจ์› :',d.ndim) print('ํ…์„œ์˜ ํฌ๊ธฐ(shape) :',d.shape) ํ…์„œ์˜ ์ฐจ์› : 1 ํ…์„œ์˜ ํฌ๊ธฐ(shape) : (4, ) ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ ํ…์„œ์˜ ์ฐจ์›์˜ ์ •์˜๋กœ ์ธํ•ด ํ˜ผ๋™ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๋ฒกํ„ฐ์—์„œ์˜ ์ฐจ์›์€ ํ•˜๋‚˜์˜ ์ถ•์— ๋†“์ธ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด๊ณ , ํ…์„œ์—์„œ์˜ ์ฐจ์›์€ ์ถ•์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 3) 2์ฐจ์› ํ…์„œ(ํ–‰๋ ฌ) ํ–‰๊ณผ ์—ด์ด ์กด์žฌํ•˜๋Š” ๋ฒกํ„ฐ์˜ ๋ฐฐ์—ด. ์ฆ‰, ํ–‰๋ ฌ(matrix)์„ 2์ฐจ์› ํ…์„œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2D ํ…์„œ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. # 3ํ–‰ 4์—ด์˜ ํ–‰๋ ฌ d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) print('ํ…์„œ์˜ ์ฐจ์› :',d.ndim) print('ํ…์„œ์˜ ํฌ๊ธฐ(shape) :',d.shape) ํ…์„œ์˜ ์ฐจ์› : 2 ํ…์„œ์˜ ํฌ๊ธฐ(shape) : (3, 4) ํ…์„œ์˜ ํฌ๊ธฐ(shape)์— ๋Œ€ํ•ด์„œ๋„ ์ •๋ฆฌํ•ฉ์‹œ๋‹ค. ํ…์„œ์˜ ํฌ๊ธฐ๋ž€, ๊ฐ ์ถ•์„ ๋”ฐ๋ผ์„œ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์ฐจ์›์ด ์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฐ’์ž…๋‹ˆ๋‹ค. ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ”๋กœ ๋จธ๋ฆฟ์†์œผ๋กœ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์œผ๋ฉด ๋ชจ๋ธ ์„ค๊ณ„ ์‹œ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ์–ด๋ ค์šธ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ์ˆœ์ฐจ์ ์œผ๋กœ ํ™•์žฅํ•ด๋‚˜๊ฐ€๋ฉฐ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ๋„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ฒฝ์šฐ 3๊ฐœ์˜ ์ปค๋‹ค๋ž€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”๋ฐ ๊ทธ ๊ฐ๊ฐ์˜ ์ปค๋‹ค๋ž€ ๋ฐ์ดํ„ฐ๋Š” ์ž‘์€ ๋ฐ์ดํ„ฐ 4๊ฐœ๋กœ ์ด๋ฃจ์–ด์กŒ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4) 3์ฐจ์› ํ…์„œ(๋‹ค์ฐจ์› ๋ฐฐ์—ด) ํ–‰๋ ฌ ๋˜๋Š” 2์ฐจ์› ํ…์„œ๋ฅผ ๋‹จ์œ„๋กœ ํ•œ ๋ฒˆ ๋” ๋ฐฐ์—ดํ•˜๋ฉด 3์ฐจ์› ํ…์„œ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 3D ํ…์„œ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ 0์ฐจ์› ~ 2์ฐจ์› ํ…์„œ๋Š” ๊ฐ๊ฐ ์Šค์นผ๋ผ, ๋ฒกํ„ฐ, ํ–‰๋ ฌ์ด๋ผ๊ณ  ํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋ฏ€๋กœ 3์ฐจ์› ์ด์ƒ์˜ ํ…์„œ๋ถ€ํ„ฐ ๋ณธ๊ฒฉ์ ์œผ๋กœ ํ…์„œ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์‚ฌ์ด์–ธ์Šค ๋ถ„์•ผ ํ•œ์ •์œผ๋กœ ์ฃผ๋กœ 3์ฐจ์› ์ด์ƒ์˜ ๋ฐฐ์—ด์„ ํ…์„œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค๊ณ  ์ดํ•ดํ•ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค. 3D ํ…์„œ๋Š” ์ ์–ด๋„ ์—ฌ๊ธฐ์„œ๋Š” 3์ฐจ์› ๋ฐฐ์—ด๋กœ ์ดํ•ดํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด 3์ฐจ์› ํ…์„œ์˜ ๊ตฌ์กฐ๋ฅผ ์ดํ•ดํ•˜์ง€ ์•Š์œผ๋ฉด, ๋ณต์žกํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ž…, ์ถœ๋ ฅ๊ฐ’์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐœ๋… ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์ง€๋งŒ ๋ฐ˜๋“œ์‹œ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. d = np.array([ [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [10, 11, 12, 13, 14]], [[15, 16, 17, 18, 19], [19, 20, 21, 22, 23], [23, 24, 25, 26, 27]] ]) print('ํ…์„œ์˜ ์ฐจ์› :',d.ndim) print('ํ…์„œ์˜ ํฌ๊ธฐ(shape) :',d.shape) ํ…์„œ์˜ ์ฐจ์› : 3 ํ…์„œ์˜ ํฌ๊ธฐ(shape) : (2, 3, 5) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํŠนํžˆ ์ž์ฃผ ๋ณด๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด ์ด 3D ํ…์„œ์ž…๋‹ˆ๋‹ค. 3D ํ…์„œ๋Š” ์‹œํ€€์Šค ๋ฐ์ดํ„ฐ(sequence data)๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‹œํ€€์Šค ๋ฐ์ดํ„ฐ๋Š” ์ฃผ๋กœ ๋‹จ์–ด์˜ ์‹œํ€€์Šค๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ์‹œํ€€์Šค๋Š” ์ฃผ๋กœ ๋ฌธ์žฅ์ด๋‚˜ ๋ฌธ์„œ, ๋‰ด์Šค ๊ธฐ์‚ฌ ๋“ฑ์˜ ํ…์ŠคํŠธ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ 3D ํ…์„œ๋Š” (samples, timesteps, word_dim)์ด ๋ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ์ผ๊ด„๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ๋Š” ๋‹จ์œ„์ธ ๋ฐฐ์น˜์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ๋’ค์—์„œ ๋ฐฐ์šธ ํ…๋ฐ (batch_size, timesteps, word_dim)์ด๋ผ๊ณ ๋„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. samples ๋˜๋Š” batch_size๋Š” ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜, timesteps๋Š” ์‹œํ€€์Šค์˜ ๊ธธ์ด, word_dim์€ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋” ์ƒ์„ธํ•œ ์„ค๋ช…์€ RNN ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์™œ 3D ํ…์„œ์˜ ๊ฐœ๋…์ด ์‚ฌ์šฉ๋˜๋Š”์ง€ ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ 3๊ฐœ์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๋ฌธ์„œ 1 : I like NLP ๋ฌธ์„œ 2 : I like DL ๋ฌธ์„œ 3 : DL is AI ์ด๋ฅผ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ ํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด๋‚˜ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ๋Œ€ํ‘œ์ ์ž…๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์€ ์•„์ง ๋ฐฐ์šฐ์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๊ฐ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด One-hot vector I [1 0 0 0 0 0] like [0 1 0 0 0 0] NLP [0 0 1 0 0 0] DL [0 0 0 1 0 0] is [0 0 0 0 1 0] AI [0 0 0 0 0 1] ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋‹จ์–ด๋“ค์„ ๋ชจ๋‘ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ๋ฐ”๊ฟ”์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ž…๋ ฅ์œผ๋กœ ํ•œ๊บผ๋ฒˆ์— ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์ˆ˜ ๋ฌถ์–ด ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋”ฅ ๋Ÿฌ๋‹์—์„œ๋Š” ๋ฐฐ์น˜(Batch)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. [[[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0]], [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0]], [[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]]] ์ด๋Š” (3, 3, 6)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 3D ํ…์„œ์ž…๋‹ˆ๋‹ค. 5) ๊ทธ ์ด์ƒ์˜ ํ…์„œ 3์ฐจ์› ํ…์„œ๋ฅผ ๋ฐฐ์—ด๋กœ ํ•ฉ์น˜๋ฉด 4์ฐจ์› ํ…์„œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 4์ฐจ์› ํ…์„œ๋ฅผ ๋ฐฐ์—ด๋กœ ํ•ฉ์น˜๋ฉด 5์ฐจ์› ํ…์„œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ…์„œ๋Š” ๋‹ค์ฐจ์› ๋ฐฐ์—ด๋กœ์„œ ๊ณ„์†ํ•ด์„œ ํ™•์žฅ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๊ฐ ํ…์„œ๋ฅผ ๋„ํ˜•์œผ๋กœ ์‹œ๊ฐํ™”ํ•œ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 6) PyTorch์—์„œ์˜ ํ…์„œ 2์ฑ•ํ„ฐ์˜ 'ํ…์„œ ์กฐ์ž‘ํ•˜๊ธฐ' ์‹ค์Šต์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. 3. ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ์—ฐ์‚ฐ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ๊ธฐ๋ณธ์ ์ธ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import numpy as np 1) ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ ๊ฐ™์€ ํฌ๊ธฐ์˜ ๋‘ ๊ฐœ์˜ ๋ฒกํ„ฐ๋‚˜ ํ–‰๋ ฌ์€ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ฐ™์€ ์œ„์น˜์˜ ์›์†Œ๋ผ๋ฆฌ ์—ฐ์‚ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์—ฐ์‚ฐ์„ ์š”์†Œ๋ณ„(element-wise) ์—ฐ์‚ฐ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด A์™€ B๋ผ๋Š” ๋‘ ๊ฐœ์˜ ๋ฒกํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. = [ 4 ] B [ 2 ] ์ด๋•Œ ๋‘ ๋ฒกํ„ฐ A์™€ B์˜ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. + = [ 4 ] [ 2 ] [ 6 ] โˆ’ = [ 4 ] [ 2 ] [ 2 ] Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A = np.array([8, 4, 5]) B = np.array([1, 2, 3]) print('๋„ ๋ฒกํ„ฐ์˜ ํ•ฉ :',A+B) print('๋„ ๋ฒกํ„ฐ์˜ ์ฐจ :',A-B) ๋‘ ํ–‰๋ ฌ์˜ ํ•ฉ : [9 6 8] ๋‘ ํ–‰๋ ฌ์˜ ์ฐจ : [7 2 2] ํ–‰๋ ฌ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. A์™€ B๋ผ๋Š” ๋‘ ๊ฐœ์˜ ํ–‰๋ ฌ์ด ์žˆ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, ๋‘ ํ–‰๋ ฌ A์™€ B์˜ ๋ง์…ˆ๊ณผ ๋บ„์…ˆ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. = [ 10 20 30 40 50 60 70 80 ] B [ 6 7 8 2 3 4 ] + = [ 10 20 30 40 50 60 70 80 ] [ 6 7 8 2 3 4 ] [ 15 26 37 48 51 62 73 84 ] โˆ’ = [ 10 20 30 40 50 60 70 80 ] [ 6 7 8 2 3 4 ] [ 14 23 32 49 58 67 76 ] Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A = np.array([[10, 20, 30, 40], [50, 60, 70, 80]]) B = np.array([[5, 6, 7, 8],[1, 2, 3, 4]]) print('๋„ ํ–‰๋ ฌ์˜ ํ•ฉ :') print(A + B) print('๋„ ํ–‰๋ ฌ์˜ ์ฐจ :') print(A - B) ๋‘ ํ–‰๋ ฌ์˜ ํ•ฉ : [[15 26 37 48] [51 62 73 84]] ๋‘ ํ–‰๋ ฌ์˜ ์ฐจ : [[ 5 14 23 32] [49 58 67 76]] 2) ๋ฒกํ„ฐ์˜ ๋‚ด์ ๊ณผ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ ๋ฒกํ„ฐ์˜ ์ ๊ณฑ(dot product) ๋˜๋Š” ๋‚ด์ (inner product)์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค. ๋ฒกํ„ฐ์˜ ๋‚ด์ ์€ ์—ฐ์‚ฐ์„ ์ (dot)์œผ๋กœ ํ‘œํ˜„ํ•˜์—ฌ โ‹… ์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋‚ด์ ์ด ์„ฑ๋ฆฝํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๊ฐ™์•„์•ผ ํ•˜๋ฉฐ, ๋‘ ๋ฒกํ„ฐ ์ค‘ ์•ž์˜ ๋ฒกํ„ฐ๊ฐ€ ํ–‰๋ฒกํ„ฐ(๊ฐ€๋กœ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ)์ด๊ณ  ๋’ค์˜ ๋ฒกํ„ฐ๊ฐ€ ์—ด๋ฒกํ„ฐ(์„ธ๋กœ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ) ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๊ฐ™๊ณ  ๊ณฑ์…ˆ์˜ ๋Œ€์ƒ์ด ๊ฐ๊ฐ ํ–‰๋ฒกํ„ฐ์ด๊ณ  ์—ด๋ฒกํ„ฐ์ผ ๋•Œ ๋‚ด์ ์ด ์ด๋ฃจ์–ด์ง€๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฒกํ„ฐ์˜ ๋‚ด์ ์˜ ๊ฒฐ๊ณผ๋Š” ์Šค์นผ๋ผ๊ฐ€ ๋œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์Šค์นผ๋ผ โ‹… = [ 2 3 ] [ 5 ] 1 4 2 5 3 6 32 (์Šค์นผ๋ผ) Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A = np.array([1, 2, 3]) B = np.array([4, 5, 6]) print('๋„ ๋ฒกํ„ฐ์˜ ๋‚ด์  :',np.dot(A, B)) ๋‘ ๋ฒกํ„ฐ์˜ ๋‚ด์  : 32 ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฒกํ„ฐ์˜ ๋‚ด์ ์„ ์ดํ•ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์€ ์™ผ์ชฝ ํ–‰๋ ฌ์˜ ํ–‰๋ฒกํ„ฐ(๊ฐ€๋กœ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ)์™€ ์˜ค๋ฅธ์ชฝ ํ–‰๋ ฌ์˜ ์—ด๋ฒกํ„ฐ(์„ธ๋กœ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ)์˜ ๋‚ด์ (๋Œ€์‘ํ•˜๋Š” ์›์†Œ๋“ค์˜ ๊ณฑ์˜ ํ•ฉ)์ด ๊ฒฐ๊ณผ ํ–‰๋ ฌ์˜ ์›์†Œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด A์™€ B๋ผ๋Š” ๋‘ ๊ฐœ์˜ ํ–‰๋ ฌ์ด ์žˆ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, ๋‘ ํ–‰๋ ฌ A์™€ B์˜ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. = [ 3 4 ] B [ 7 8 ] B [ 3 4 ] [ 7 8 ] [ ร— + ร— 1 7 3 8 ร— + ร— 2 7 4 8 ] [ 23 31 34 46 ] Numpy๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A = np.array([[1, 3],[2, 4]]) B = np.array([[5, 7],[6, 8]]) print('๋„ ํ–‰๋ ฌ์˜ ํ–‰๋ ฌ๊ณฑ :') print(np.matmul(A, B)) ๋‘ ํ–‰๋ ฌ์˜ ํ–‰๋ ฌ๊ณฑ : [[23 31] [34 46]] ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์€ ๋”ฅ ๋Ÿฌ๋‹์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ํ•„์ˆ˜์ ์ธ ๊ฐœ๋…์ด๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ์ˆ™์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ–‰๋ ฌ ๊ณฑ์…ˆ์—์„œ์˜ ์ฃผ์š”ํ•œ ๋‘ ๊ฐ€์ง€ ์กฐ๊ฑด ๋˜ํ•œ ๋ฐ˜๋“œ์‹œ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ๋‘ ํ–‰๋ ฌ์˜ ๊ณฑ A ร— B์ด ์„ฑ๋ฆฝ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ–‰๋ ฌ A์˜ ์—ด์˜ ๊ฐœ์ˆ˜์™€ ํ–‰๋ ฌ B์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๋Š” ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ๋‘ ํ–‰๋ ฌ์˜ ๊ณฑ A ร— B์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ํ–‰๋ ฌ AB์˜ ํฌ๊ธฐ๋Š” A์˜ ํ–‰์˜ ๊ฐœ์ˆ˜์™€ B์˜ ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง„๋‹ค. ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ๊ณฑ ๋˜๋Š” ํ–‰๋ ฌ๊ณผ ๋ฒกํ„ฐ์˜ ๊ณฑ ๋˜ํ•œ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ๊ณผ ๋™์ผํ•œ ์›๋ฆฌ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. 4. ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ๋…๋ฆฝ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ผ ๋•Œ, 1๊ฐœ์˜ ์ข…์† ๋ณ€์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ–‰๋ ฌ์˜ ์—ฐ์‚ฐ์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€๋‚˜ ๋‹ค์ค‘ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๊ฐ€ ์ด๋Ÿฌํ•œ ์—ฐ์‚ฐ์˜ ์˜ˆ์ธ๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ํ†ตํ•ด ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋…๋ฆฝ ๋ณ€์ˆ˜ ๊ฐ€ n ๊ฐœ์ธ ๋‹ค์ค‘ ์„ ํ˜• ํšŒ๊ท€ ์ˆ˜์‹์ž…๋‹ˆ๋‹ค. = 1 1 w x + 3 3. . w x + ์ด๋Š” ์ž…๋ ฅ ๋ฒกํ„ฐ [ 1. . n ] ์™€ ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ [ 1. . w ] ์˜ ๋‚ด์ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. = [ 1 x x โ‹… โ‹… x ] [ 1 2 3 โ‹… w ] b x w + 2 2 x w + ๋˜๋Š” ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ [ 1. . w ] ์™€ ์ž…๋ ฅ ๋ฒกํ„ฐ [ 1. . n ] ์˜ ๋‚ด์ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. = [ 1 w w โ‹… โ‹… w ] [ 1 2 3 โ‹… x ] b x w + 2 2 x w ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ์—๋Š” ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์œผ๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ง‘์˜ ํฌ๊ธฐ, ๋ฐฉ์˜ ์ˆ˜, ์ธต์˜ ์ˆ˜, ์ง‘์ด ์–ผ๋งˆ๋‚˜ ์˜ค๋ž˜๋˜์—ˆ๋Š”์ง€์™€ ์ง‘์˜ ๊ฐ€๊ฒฉ์ด ๊ธฐ๋ก๋œ ๋ถ€๋™์‚ฐ ๋ฐ์ดํ„ฐ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ง‘์˜ ์ •๋ณด๊ฐ€ ๋“ค์–ด์™”์„ ๋•Œ, ์ง‘์˜ ๊ฐ€๊ฒฉ์„ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•œ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. size( e t)( 1 ) number of bedrooms( 2 ) number of floors( 3 ) age of home( 4 ) price($1000)(y) 1800 2 1 10 207 1200 4 2 20 176 1700 3 2 15 213 1500 5 1 10 234 1100 2 2 10 155 ์œ„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ž…๋ ฅ ํ–‰๋ ฌ ์™€ ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ์˜ ๊ณฑ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. [ 11 x 12 x 13 x 14 21 x 22 x 23 x 24 31 x 32 x 33 x 34 41 x 42 x 43 x 44 51 x 52 x 53 x 54 ] [ 1 2 3 4 ] [ 11 1 x 12 2 x 13 3 x 14 4 21 1 x 22 2 x 23 3 x 24 4 31 1 x 32 2 x 33 3 x 34 4 41 1 x 42 2 x 43 3 x 44 4 51 1 x 52 2 x 53 3 x 54 4 ์—ฌ๊ธฐ์— ํŽธํ–ฅ ๋ฒกํ„ฐ๋ฅผ ๋” ํ•ด์ฃผ๋ฉด ์œ„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ „์ฒด ๊ฐ€์„ค ์ˆ˜์‹ ( ) ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [ 11 1 x 12 2 x 13 3 x 14 4 21 1 x 22 2 x 23 3 x 24 4 31 1 x 32 2 x 33 3 x 34 4 41 1 x 42 2 x 43 3 x 44 4 51 1 x 52 2 x 53 3 x 54 4 ] [ b b ] [ 1 2 3 4 5 ] ( ) X + ์œ„์˜ ์ˆ˜์‹์—์„œ ์ž…๋ ฅ ํ–‰๋ ฌ ๋Š” 5ํ–‰ 4์—ด์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๋ฒกํ„ฐ๋ฅผ ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ๋Š” 5ํ–‰ 1์—ด์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ณฑ์…ˆ์ด ์„ฑ๋ฆฝํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋Š” 4ํ–‰ 1์—ด์„ ๊ฐ€์ ธ์•ผ ํ•จ์„ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ๋ฅผ ์•ž์— ๋‘๊ณ  ์ž…๋ ฅ ํ–‰๋ ฌ์„ ๋’ค์— ๋‘๊ณ  ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ํ•œ๋‹ค๋ฉด ์ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. [ 1 w w w ] [ 11 x 21 x 31 x 41 x 51 12 x 22 x 32 x 42 x 52 13 x 23 x 33 x 43 x 53 14 x 24 x 34 x 44 x 54 ] [ b b b b ] [ 1 ์ˆ˜ํ•™์  ๊ด€๋ก€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ์ฃผ๋กœ ๊ฐ€์ค‘์น˜ ๊ฐ€ ์ž…๋ ฅ์˜ ์•ž์— ์˜ค๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. ( ) W + ์ธ๊ณต ์‹ ๊ฒฝ๋ง๋„ ๋ณธ์งˆ์ ์œผ๋กœ ์œ„์™€ ๊ฐ™์€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. 5. ์ƒ˜ํ”Œ(Sample)๊ณผ ํŠน์„ฑ(Feature) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ž…๋ ฅ ํ–‰๋ ฌ์„๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ์ƒ˜ํ”Œ(Sample)๊ณผ ํŠน์„ฑ(Feature)์˜ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์…€ ์ˆ˜ ์žˆ๋Š” ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•  ๋•Œ, ๊ฐ๊ฐ์„ ์ƒ˜ํ”Œ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ์ข…์† ๋ณ€์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ๊ฐ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜๋ฅผ ํŠน์„ฑ์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 6. ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ ๊ฒฐ์ • ์•ž์„œ ์–ธ๊ธ‰ํ•˜์˜€๋˜ ํ–‰๋ ฌ ๊ณฑ์…ˆ์˜ ๋‘ ๊ฐ€์ง€ ์ฃผ์š”ํ•œ ์กฐ๊ฑด์„ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ๋‘ ํ–‰๋ ฌ์˜ ๊ณฑ J ร— K์ด ์„ฑ๋ฆฝ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ–‰๋ ฌ J์˜ ์—ด์˜ ๊ฐœ์ˆ˜์™€ ํ–‰๋ ฌ K์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๋Š” ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ๋‘ ํ–‰๋ ฌ์˜ ๊ณฑ J ร— K์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ํ–‰๋ ฌ JK์˜ ํฌ๊ธฐ๋Š” J์˜ ํ–‰์˜ ๊ฐœ์ˆ˜์™€ K์˜ ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง„๋‹ค. ์ด๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W์™€ ํŽธํ–ฅ ํ–‰๋ ฌ B์˜ ํฌ๊ธฐ๋ฅผ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋…๋ฆฝ ๋ณ€์ˆ˜ ํ–‰๋ ฌ์„ X, ์ข…์† ๋ณ€์ˆ˜ ํ–‰๋ ฌ์„ Y๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ด๋•Œ ํ–‰๋ ฌ X๋ฅผ ์ž…๋ ฅ ํ–‰๋ ฌ(Input Matrix), Y๋ฅผ ์ถœ๋ ฅ ํ–‰๋ ฌ(Output Matrix)์ด๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด์ œ ์ž…๋ ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์™€ ์ถœ๋ ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋กœ๋ถ€ํ„ฐ W ํ–‰๋ ฌ๊ณผ B ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ์ถ”๋ก ํ•ด ๋ด…์‹œ๋‹ค. ํ–‰๋ ฌ์˜ ๋ง์…ˆ์— ํ•ด๋‹น๋˜๋Š” B ํ–‰๋ ฌ์€ Y ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ B ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” Y ํ–‰๋ ฌ์˜ ํฌ๊ธฐ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์ด ์„ฑ๋ฆฝ๋˜๋ ค๋ฉด ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์—์„œ ์•ž์— ์žˆ๋Š” ํ–‰๋ ฌ์˜ ์—ด์˜ ํฌ๊ธฐ์™€ ๋’ค์— ์žˆ๋Š” ํ–‰๋ ฌ์˜ ํ–‰์˜ ํฌ๊ธฐ๋Š” ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ž…๋ ฅ ํ–‰๋ ฌ X๋กœ๋ถ€ํ„ฐ W ํ–‰๋ ฌ์˜ ํ–‰์˜ ํฌ๊ธฐ๊ฐ€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ๋‘ ํ–‰๋ ฌ์˜ ๊ณฑ์˜ ๊ฒฐ๊ณผ๋กœ์„œ ๋‚˜์˜จ ํ–‰๋ ฌ์˜ ์—ด์˜ ํฌ๊ธฐ๋Š” ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์—์„œ ๋’ค์— ์žˆ๋Š” ํ–‰๋ ฌ์˜ ์—ด์˜ ํฌ๊ธฐ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ถœ๋ ฅ ํ–‰๋ ฌ Y๋กœ๋ถ€ํ„ฐ W ํ–‰๋ ฌ์˜ ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ํ–‰๋ ฌ๊ณผ ์ถœ๋ ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ ํŽธํ–ฅ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜์˜€์„ ๋•Œ ํ•ด๋‹น ๋ชจ๋ธ์— ์กด์žฌํ•˜๋Š” ์ด ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์ด ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋Š” ํ•ด๋‹น ๋ชจ๋ธ์— ์กด์žฌํ•˜๋Š” ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ ํŽธํ–ฅ ํ–‰๋ ฌ์˜ ๋ชจ๋“  ์›์†Œ์˜ ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 04. [ML ์ž…๋ฌธ โœ] - ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€(Logistic Regression) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 04-01 ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€(Logistic Regression) ์ผ์ƒ ์† ํ’€๊ณ ์ž ํ•˜๋Š” ๋งŽ์€ ๋ฌธ์ œ ์ค‘์—์„œ๋Š” ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ์ •๋‹ต์„ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‹œํ—˜์„ ๋ดค๋Š”๋ฐ ์ด ์‹œํ—˜ ์ ์ˆ˜๊ฐ€ ํ•ฉ๊ฒฉ์ธ์ง€ ๋ถˆํ•ฉ๊ฒฉ์ธ์ง€๊ฐ€ ๊ถ๊ธˆํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์–ด๋–ค ๋ฉ”์ผ์„ ๋ฐ›์•˜์„ ๋•Œ ์ด๊ฒŒ ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฌธ์ œ๋„ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ๋Œ€ํ‘œ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€(Logistic Regression)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ด๋ฆ„์€ ํšŒ๊ท€์ด์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ๋ถ„๋ฅ˜(Classification) ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification) ํ•™์ƒ๋“ค์ด ์‹œํ—˜ ์„ฑ์ ์— ๋”ฐ๋ผ์„œ ํ•ฉ๊ฒฉ, ๋ถˆํ•ฉ๊ฒฉ์ด ๊ธฐ์žฌ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ์‹œํ—˜ ์„ฑ์ ์ด๋ผ๋ฉด, ํ•ฉ๋ถˆ ๊ฒฐ๊ณผ๋Š”์ž…๋‹ˆ๋‹ค. ์ด ์‹œํ—˜์˜ ์ปคํŠธ๋ผ์ธ์€ ๊ณต๊ฐœ๋˜์ง€ ์•Š์•˜๋Š”๋ฐ ์ด ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ํŠน์ • ์ ์ˆ˜๋ฅผ ์–ป์—ˆ์„ ๋•Œ์˜ ํ•ฉ๊ฒฉ, ๋ถˆํ•ฉ๊ฒฉ ์—ฌ๋ถ€๋ฅผ ํŒ์ •ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ ์ž ํ•ฉ์‹œ๋‹ค. score( ) result( ) 45 ๋ถˆํ•ฉ๊ฒฉ 50 ๋ถˆํ•ฉ๊ฒฉ 55 ๋ถˆํ•ฉ๊ฒฉ 60 ํ•ฉ๊ฒฉ 65 ํ•ฉ๊ฒฉ 70 ํ•ฉ๊ฒฉ ์œ„์˜ ๋ฐ์ดํ„ฐ์—์„œ ํ•ฉ๊ฒฉ์„ 1, ๋ถˆํ•ฉ๊ฒฉ์„ 0์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ ๋“ค์„ ํ‘œํ˜„ํ•˜๋Š” ๊ทธ๋ž˜ํ”„๋Š” ์•ŒํŒŒ๋ฒณ์˜ S์ž ํ˜•ํƒœ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์™€์˜ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” x b ์™€ ๊ฐ™์€ ์ง์„  ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ S์ž ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ์— ์ง์„ ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ถ„๋ฅ˜ ์ž‘์—…์ด ์ž˜ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋ฒˆ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ๊ฐ€์„ค์€ ์„ ํ˜• ํšŒ๊ท€ ๋•Œ์˜ ( ) W + ๊ฐ€ ์•„๋‹ˆ๋ผ, ์œ„์™€ ๊ฐ™์ด S์ž ๋ชจ์–‘์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์–ด๋–ค ํŠน์ • ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ( ) f ( x b ) ์˜ ๊ฐ€์„ค์„ ์‚ฌ์šฉํ•  ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„์™€ ๊ฐ™์ด S์ž ๋ชจ์–‘์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ์–ด๋–ค ํ•จ์ˆ˜ ๊ฐ€ ์ด๋ฏธ ๋„๋ฆฌ ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. 2. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜(Sigmoid function) ์œ„์™€ ๊ฐ™์ด S์ž ํ˜•ํƒœ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค์ฃผ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ๋ฐฉ์ •์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) s g o d ( x b ) 1 + โˆ’ ( x b ) ฯƒ ( x b ) ์„ ํ˜• ํšŒ๊ท€์—์„œ๋Š” ์ตœ์ ์˜ ์™€๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์˜€์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์„ ํ˜• ํšŒ๊ท€์—์„œ๋Š” ๊ฐ€ ์ง์„ ์˜ ๊ธฐ์šธ๊ธฐ, ๊ฐ€ y ์ ˆํŽธ์„ ์˜๋ฏธํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์—ฌ๊ธฐ์—์„œ๋Š” ์™€ ๊ฐ€ ํ•จ์ˆ˜์˜ ๊ทธ๋ž˜ํ”„์— ์–ด๋–ค ์˜ํ–ฅ์„ ์ฃผ๋Š”์ง€ ์ง์ ‘ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ๋กœ์„œ Matplotlib์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  Matplotlib๊ณผ Numpy๋ฅผ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. %matplotlib inline import numpy as np # ๋„˜ํŒŒ์ด ์‚ฌ์šฉ import matplotlib.pyplot as plt # ๋งทํ”Œ๋กฏ๋ฆฝ์‚ฌ์šฉ Numpy๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. def sigmoid(x): # ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜ ์ •์˜ return 1/(1+np.exp(-x)) 1. W๊ฐ€ 1์ด๊ณ  b๊ฐ€ 0์ธ ๊ทธ๋ž˜ํ”„ ๊ฐ€์žฅ ๋จผ์ € ๊ฐ€ 1์ด๊ณ , ๊ฐ€ 0์ธ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋ด…์‹œ๋‹ค. x = np.arange(-5.0, 5.0, 0.1) y = sigmoid(x) plt.plot(x, y, 'g') plt.plot([0,0],[1.0,0.0], ':') # ๊ฐ€์šด๋ฐ ์ ์„  ์ถ”๊ฐ€ plt.title('Sigmoid Function') plt.show() ์œ„์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ํ†ตํ•ด์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” ์ถœ๋ ฅ๊ฐ’์„ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ์กฐ์ •ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€ 0์ผ ๋•Œ 0.5์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ฐ€ ๋งค์šฐ ์ปค์ง€๋ฉด 1์— ์ˆ˜๋ ดํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๊ฐ€ ๋งค์šฐ ์ž‘์•„์ง€๋ฉด 0์— ์ˆ˜๋ ดํ•ฉ๋‹ˆ๋‹ค. 2. W ๊ฐ’์˜ ๋ณ€ํ™”์— ๋”ฐ๋ฅธ ๊ฒฝ์‚ฌ๋„์˜ ๋ณ€ํ™” ์ด์ œ ์˜ ๊ฐ’์„ ๋ณ€ํ™”์‹œํ‚ค๊ณ  ์ด์— ๋”ฐ๋ฅธ ๊ทธ๋ž˜ํ”„๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. x = np.arange(-5.0, 5.0, 0.1) y1 = sigmoid(0.5*x) y2 = sigmoid(x) y3 = sigmoid(2*x) plt.plot(x, y1, 'r', linestyle='--') # W์˜ ๊ฐ’์ด 0.5์ผ ๋•Œ plt.plot(x, y2, 'g') # W์˜ ๊ฐ’์ด 1์ผ ๋•Œ plt.plot(x, y3, 'b', linestyle='--') # W์˜ ๊ฐ’์ด 2์ผ ๋•Œ plt.plot([0,0],[1.0,0.0], ':') # ๊ฐ€์šด๋ฐ ์ ์„  ์ถ”๊ฐ€ plt.title('Sigmoid Function') plt.show() ์œ„์˜ ๊ทธ๋ž˜ํ”„๋Š” ์˜ ๊ฐ’์ด 0.5์ผ ๋•Œ ๋นจ๊ฐ„์ƒ‰ ์„ ,์˜ ๊ฐ’์ด 1์ผ ๋•Œ๋Š” ์ดˆ๋ก์ƒ‰์„ , ์˜ ๊ฐ’์ด 2์ผ ๋•Œ ํŒŒ๋ž€์ƒ‰ ์„ ์ด ๋‚˜์˜ค๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ž์„ธํžˆ ๋ณด๋ฉด์˜ ๊ฐ’์— ๋”ฐ๋ผ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์‚ฌ๋„๊ฐ€ ๋ณ€ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ์„ ํ˜• ํšŒ๊ท€์—์„œ ๊ฐ€์ค‘์น˜๋Š” ์ง์„ ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ์˜๋ฏธํ–ˆ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์‚ฌ๋„๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.์˜ ๊ฐ’์ด ์ปค์ง€๋ฉด ๊ฒฝ์‚ฌ๊ฐ€ ์ปค์ง€๊ณ ์˜ ๊ฐ’์ด ์ž‘์•„์ง€๋ฉด ๊ฒฝ์‚ฌ๊ฐ€ ์ž‘์•„์ง‘๋‹ˆ๋‹ค. 3. b ๊ฐ’์˜ ๋ณ€ํ™”์— ๋”ฐ๋ฅธ ์ขŒ, ์šฐ ์ด๋™ ์ด์ œ ์˜ ๊ฐ’์— ๋”ฐ๋ผ์„œ ๊ทธ๋ž˜ํ”„๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. x = np.arange(-5.0, 5.0, 0.1) y1 = sigmoid(x+0.5) y2 = sigmoid(x+1) y3 = sigmoid(x+1.5) plt.plot(x, y1, 'r', linestyle='--') # x + 0.5 plt.plot(x, y2, 'g') # x + 1 plt.plot(x, y3, 'b', linestyle='--') # x + 1.5 plt.plot([0,0],[1.0,0.0], ':') # ๊ฐ€์šด๋ฐ ์ ์„  ์ถ”๊ฐ€ plt.title('Sigmoid Function') plt.show() ์œ„์˜ ๊ทธ๋ž˜ํ”„๋Š” ์˜ ๊ฐ’์— ๋”ฐ๋ผ์„œ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ขŒ, ์šฐ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 4. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ๋ถ„๋ฅ˜ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๊ฐ’์ด ํ•œ์—†์ด ์ปค์ง€๋ฉด 1์— ์ˆ˜๋ ดํ•˜๊ณ , ์ž…๋ ฅ๊ฐ’์ด ํ•œ์—†์ด ์ž‘์•„์ง€๋ฉด 0์— ์ˆ˜๋ ดํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์€ 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š”๋ฐ ์ด ํŠน์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๋ถ„๋ฅ˜ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž„๊ณ—๊ฐ’์„ 0.5๋ผ๊ณ  ์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ๊ฐ’์ด 0.5 ์ด์ƒ์ด๋ฉด 1(True), 0.5์ดํ•˜๋ฉด 0(False)์œผ๋กœ ํŒ๋‹จํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ™•๋ฅ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ํ•ด๋‹น ๋ ˆ์ด๋ธ”์— ์†ํ•  ํ™•๋ฅ ์ด 50%๊ฐ€ ๋„˜์œผ๋ฉด ํ•ด๋‹น ๋ ˆ์ด๋ธ”๋กœ ํŒ๋‹จํ•˜๊ณ , ํ•ด๋‹น ๋ ˆ์ด๋ธ”์— ์†ํ•  ํ™•๋ฅ ์ด 50%๋ณด๋‹ค ๋‚ฎ์œผ๋ฉด ์•„๋‹ˆ๋ผ๊ณ  ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ๋น„์šฉ ํ•จ์ˆ˜(Cost function) ์ด์ œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ๊ฐ€์„ค์ด ( ) s g o d ( x b ) ์ธ ๊ฒƒ์€ ์•Œ์•˜์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ตœ์ ์˜ ์™€๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๋น„์šฉ ํ•จ์ˆ˜(cost function)๋ฅผ ์ •์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ˜น์‹œ ์•ž์„œ ์„ ํ˜• ํšŒ๊ท€์—์„œ ๋ฐฐ์šด ๋น„์šฉ ํ•จ์ˆ˜์ธ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ(Mean Square Error, MSE)๋ฅผ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ๋น„์šฉ ํ•จ์ˆ˜๋กœ ๊ทธ๋ƒฅ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋ ๊นŒ์š”? ๋‹ค์Œ์€ ์„ ํ˜• ํšŒ๊ท€์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ์˜ ์ˆ˜์‹์ž…๋‹ˆ๋‹ค. o t ( , ) 1 โˆ‘ = n [ ( ) H ( ( ) ) ] ์œ„์˜ ๋น„์šฉ ํ•จ์ˆ˜ ์ˆ˜์‹์—์„œ ๊ฐ€์„ค์€ ์ด์ œ ( ) W + ๊ฐ€ ์•„๋‹ˆ๋ผ ( ) s g o d ( x b ) ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•˜๋ฉด ์„ ํ˜• ํšŒ๊ท€ ๋•Œ์™€ ๋‹ฌ๋ฆฌ ๋‹ค์Œ์˜ ๊ทธ๋ฆผ๊ณผ ์œ ์‚ฌํ•œ ์‹ฌํ•œ ๋น„๋ณผ๋ก(non-convex) ํ˜•ํƒœ์˜ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๊ทธ๋ž˜ํ”„์— ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์˜ ๋ฌธ์ œ์ ์€ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์ด ์˜ค์ฐจ๊ฐ€ ์ตœ์†Ÿ๊ฐ’์ด ๋˜๋Š” ๊ตฌ๊ฐ„์— ๋„์ฐฉํ–ˆ๋‹ค๊ณ  ํŒ๋‹จํ•œ ๊ทธ ๊ตฌ๊ฐ„์ด ์‹ค์ œ ์˜ค์ฐจ๊ฐ€ ์™„์ „ํžˆ ์ตœ์†Ÿ๊ฐ’์ด ๋˜๋Š” ๊ตฌ๊ฐ„์ด ์•„๋‹ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์ด ๋“ฑ์‚ฐ ํ›„์— ์‚ฐ์„ ๋‚ด๋ ค์˜ฌ ๋•Œ๋„, ๊ฐ€ํŒŒ๋ฅธ ๊ฒฝ์‚ฌ๋ฅผ ๋‚ด๋ ค์˜ค๋‹ค๊ฐ€ ๋„“์€ ํ‰์ง€๊ฐ€ ๋‚˜์˜ค๋ฉด ์ˆœ๊ฐ„์ ์œผ๋กœ ๋‹ค ๋‚ด๋ ค์™”๋‹ค๊ณ  ์ฐฉ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ๊ทธ๊ณณ์ด ๋‹ค ๋‚ด๋ ค์˜จ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ž ๊น ํ‰์ง€๊ฐ€ ๋‚˜์™”์„ ๋ฟ์ด๋ผ๋ฉด ๊ธธ์„ ๋” ์ฐพ์•„์„œ ๋” ๋‚ด๋ ค๊ฐ€์•ผ ํ•  ๊ฒ๋‹ˆ๋‹ค. ๋ชจ๋ธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‹ค์ œ ์˜ค์ฐจ๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๊ตฌ๊ฐ„์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์‹ค์ œ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๊ตฌ๊ฐ„์„ ์ž˜๋ชป ํŒ๋‹จํ•˜๋ฉด ์ตœ์ ์˜ ๊ฐ€์ค‘์น˜ ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฐ’์„ ํƒํ•ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ๋” ์˜ค๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ „์ฒด ํ•จ์ˆ˜์— ๊ฑธ์ณ ์ตœ์†Ÿ๊ฐ’์ธ ๊ธ€๋กœ๋ฒŒ ๋ฏธ๋‹ˆ๋ฉˆ(Global Minimum)์ด ์•„๋‹Œ ํŠน์ • ๊ตฌ์—ญ์—์„œ์˜ ์ตœ์†Ÿ๊ฐ’์ธ ๋กœ์ปฌ ๋ฏธ๋‹ˆ๋ฉˆ(Local Minimum)์— ๋„๋‹ฌํ–ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” cost๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ์ฐพ๋Š”๋‹ค๋Š” ๋น„์šฉ ํ•จ์ˆ˜์˜ ๋ชฉ์ ์— ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ํŠน์ง•์€ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์ด 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์‹ค์ œ ๊ฐ’์ด 1์ผ ๋•Œ ์˜ˆ์ธก๊ฐ’์ด 0์— ๊ฐ€๊นŒ์›Œ์ง€๋ฉด ์˜ค์ฐจ๊ฐ€ ์ปค์ ธ์•ผ ํ•˜๋ฉฐ, ์‹ค์ œ ๊ฐ’์ด 0์ผ ๋•Œ, ์˜ˆ์ธก๊ฐ’์ด 1์— ๊ฐ€๊นŒ์›Œ์ง€๋ฉด ์˜ค์ฐจ๊ฐ€ ์ปค์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์ถฉ์กฑํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋ฐ”๋กœ ๋กœ๊ทธ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ = 0.5 ์— ๋Œ€์นญํ•˜๋Š” ๋‘ ๊ฐœ์˜ ๋กœ๊ทธ ํ•จ์ˆ˜ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’์ด 1์ผ ๋•Œ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ์ฃผํ™ฉ์ƒ‰ ์„ ์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์œผ๋ฉฐ, ์‹ค์ œ ๊ฐ’์ด 0์ผ ๋•Œ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ์ดˆ๋ก์ƒ‰ ์„ ์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’์ด 1์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ, ์˜ˆ์ธก๊ฐ’์ธ ( ) ์˜ ๊ฐ’์ด 1์ด๋ฉด ์˜ค์ฐจ๊ฐ€ 0์ด๋ฏ€๋กœ ๋‹น์—ฐํžˆ cost๋Š” 0์ด ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ( ) ๊ฐ€ 0์œผ๋กœ ์ˆ˜๋ ดํ•˜๋ฉด cost๋Š” ๋ฌดํ•œ๋Œ€๋กœ ๋ฐœ์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’์ด 0์ธ ๊ฒฝ์šฐ๋Š” ๊ทธ ๋ฐ˜๋Œ€๋กœ ์ดํ•ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐœ์˜ ๋กœ๊ทธ ํ•จ์ˆ˜๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. if = โ†’ cost ( ( ) y ) โˆ’ log ( ( ) ) if = โ†’ cost ( ( ) y ) โˆ’ log ( โˆ’ ( ) )์˜ ์‹ค์ œ ๊ฐ’์ด 1์ผ ๋•Œ l g ( ) ๊ทธ๋ž˜ํ”„๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์˜ ์‹ค์ œ ๊ฐ’์ด 0์ผ ๋•Œ l g ( โˆ’ ( ) ) ๊ทธ๋ž˜ํ”„๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜๋‚˜์˜ ์‹์œผ๋กœ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. cost ( ( ) y ) โˆ’ [ l g ( ) ( โˆ’ ) o ( โˆ’ ( ) ) ] ์™œ ์œ„ ์‹์ด ๋‘ ๊ฐœ์˜ ์‹์„ ํ†ตํ•ฉํ•œ ์‹์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์„๊นŒ์š”? ์‹ค์ œ ๊ฐ’ ๊ฐ€ 1์ด๋ผ๊ณ  ํ•˜๋ฉด ๋ง์…ˆ ๊ธฐํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์šฐ์ธก์˜ ํ•ญ์ด ์—†์–ด์ง‘๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ์‹ค์ œ ๊ฐ’ ๊ฐ€ 0์ด๋ผ๊ณ  ํ•˜๋ฉด ๋ง์…ˆ ๊ธฐํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์ธก์˜ ํ•ญ์ด ์—†์–ด์ง‘๋‹ˆ๋‹ค. ์„ ํ˜• ํšŒ๊ท€์—์„œ๋Š” ๋ชจ๋“  ์˜ค์ฐจ์˜ ํ‰๊ท ์„ ๊ตฌํ•ด ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ๋ฅผ ์‚ฌ์šฉํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์—ฌ๊ธฐ์—์„œ๋„ ๋ชจ๋“  ์˜ค์ฐจ์˜ ํ‰๊ท ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. o t ( ) โˆ’ n i 1 [ ( ) o H ( ( ) ) ( โˆ’ ( ) ) o ( โˆ’ ( ( ) ) ) ] ์ •๋ฆฌํ•˜๋ฉด, ์œ„ ๋น„์šฉ ํ•จ์ˆ˜๋Š” ์‹ค์ œ ๊ฐ’ ์™€ ์˜ˆ์ธก๊ฐ’ ( ) ์˜ ์ฐจ์ด๊ฐ€ ์ปค์ง€๋ฉด cost๊ฐ€ ์ปค์ง€๊ณ , ์‹ค์ œ ๊ฐ’ ์™€ ์˜ˆ์ธก๊ฐ’ ( ) ์˜ ์ฐจ์ด๊ฐ€ ์ž‘์•„์ง€๋ฉด cost๋Š” ์ž‘์•„์ง‘๋‹ˆ๋‹ค. ์ด์ œ ์œ„ ๋น„์šฉ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ์ตœ์ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์ฐพ์•„๊ฐ‘๋‹ˆ๋‹ค. := โˆ’ โˆ‚ W o t ( ) 4. ํŒŒ์ด ํ† ์น˜๋กœ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด์ œ ํŒŒ์ด ํ† ์น˜๋กœ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ์ค‘์—์„œ๋„ ๋‹ค์ˆ˜์˜ ๋กœ ๋ถ€ํ„ฐ ๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋‹ค์ค‘ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim torch.manual_seed(1) x_train๊ณผ y_train์„ ํ…์„œ๋กœ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. x_data = [[1, 2], [2, 3], [3, 1], [4, 3], [5, 3], [6, 2]] y_data = [[0], [0], [0], [1], [1], [1]] x_train = torch.FloatTensor(x_data) y_train = torch.FloatTensor(y_data) ์•ž์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ํ–‰๋ ฌ๋กœ ์„ ์–ธํ•˜๊ณ , ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ๊ฐ€์„ค์„ ์„ธ์šฐ๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์„ค์‹์„ ์„ธ์šธ ๊ฒ๋‹ˆ๋‹ค. x_train๊ณผ y_train์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(x_train.shape) print(y_train.shape) torch.Size([6, 2]) torch.Size([6, 1]) ํ˜„์žฌ x_train์€ 6 ร— 2์˜ ํฌ๊ธฐ(shape)๋ฅผ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ์ด๋ฉฐ, y_train์€ 6 ร— 1์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. x_train์„๋ผ๊ณ  ํ•˜๊ณ , ์ด์™€ ๊ณฑํ•ด์ง€๋Š” ๊ฐ€์ค‘์น˜ ๋ฒกํ„ฐ๋ฅผ ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, W ๊ฐ€ ์„ฑ๋ฆฝ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋Š” 2 ร— 1์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ W์™€ b๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. W = torch.zeros((2, 1), requires_grad=True) # ํฌ๊ธฐ๋Š” 2 x 1 b = torch.zeros(1, requires_grad=True) ์ด์ œ ๊ฐ€์„ค์‹์„ ์„ธ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” x ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ torch.exp(x)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•œ ๊ฐ€์„ค์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. hypothesis = 1 / (1 + torch.exp(-(x_train.matmul(W) + b))) ์•ž์„œ W์™€ b๋Š” torch.zeros๋ฅผ ํ†ตํ•ด ์ „๋ถ€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ด ์ƒํƒœ์—์„œ ์˜ˆ์ธก๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(hypothesis) # ์˜ˆ์ธก๊ฐ’์ธ H(x) ์ถœ๋ ฅ tensor([[0.5000], [0.5000], [0.5000], [0.5000], [0.5000], [0.5000]], grad_fn=<MulBackward>) ์‹ค์ œ ๊ฐ’ y_train๊ณผ ํฌ๊ธฐ๊ฐ€ ๋™์ผํ•œ 6 ร— 1์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ์˜ˆ์ธก๊ฐ’ ๋ฒกํ„ฐ๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ ๋ชจ๋“  ๊ฐ’์ด 0.5์ž…๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๊ฐ€์„ค์‹์„ ์ข€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ๋„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ด๋ฏธ ๊ตฌํ˜„ํ•˜์—ฌ ์ œ๊ณตํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ torch.sigmoid๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ข€ ๋” ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ํ•œ ๊ฐ€์„ค์‹์ž…๋‹ˆ๋‹ค. hypothesis = torch.sigmoid(x_train.matmul(W) + b) ์•ž์„œ ๊ตฌํ˜„ํ•œ ์‹๊ณผ ๋ณธ์งˆ์ ์œผ๋กœ ๋™์ผํ•œ ์‹์ž…๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ W์™€ b๊ฐ€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ ์ƒํƒœ์—์„œ ์˜ˆ์ธก๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(hypothesis) tensor([[0.5000], [0.5000], [0.5000], [0.5000], [0.5000], [0.5000]], grad_fn=<SigmoidBackward>) ์•ž์„  ๊ฒฐ๊ณผ์™€ ๋™์ผํ•˜๊ฒŒ y_train๊ณผ ํฌ๊ธฐ๊ฐ€ ๋™์ผํ•œ 6 ร— 1์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ์˜ˆ์ธก๊ฐ’ ๋ฒกํ„ฐ๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ ๋ชจ๋“  ๊ฐ’์ด 0.5์ž…๋‹ˆ๋‹ค. ์ด์ œ ์•„๋ž˜์˜ ๋น„์šฉ ํ•จ์ˆซ๊ฐ’. ์ฆ‰, ํ˜„์žฌ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’ ์‚ฌ์ด์˜ cost๋ฅผ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. o t ( ) โˆ’ n i 1 [ ( ) o H ( ( ) ) ( โˆ’ ( ) ) o ( โˆ’ ( ( ) ) ) ] ์šฐ์„ , ํ˜„์žฌ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(hypothesis) print(y_train) tensor([[0.5000], [0.5000], [0.5000], [0.5000], [0.5000], [0.5000]], grad_fn=<SigmoidBackward>) tensor([[0.], [0.], [0.], [1.], [1.], [1.]]) ํ˜„์žฌ ์ด 6๊ฐœ์˜ ์›์†Œ๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ. ์ฆ‰, ํ•˜๋‚˜์˜ ์›์†Œ์— ๋Œ€ํ•ด์„œ๋งŒ ์˜ค์ฐจ๋ฅผ ๊ตฌํ•˜๋Š” ์‹์„ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. -(y_train[0] * torch.log(hypothesis[0]) + (1 - y_train[0]) * torch.log(1 - hypothesis[0])) tensor([0.6931], grad_fn=<NegBackward>) ์ด์ œ ๋ชจ๋“  ์›์†Œ์— ๋Œ€ํ•ด์„œ ์˜ค์ฐจ๋ฅผ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. losses = -(y_train * torch.log(hypothesis) + (1 - y_train) * torch.log(1 - hypothesis)) print(losses) tensor([[0.6931], [0.6931], [0.6931], [0.6931], [0.6931], [0.6931]], grad_fn=<NegBackward>) ๊ทธ๋ฆฌ๊ณ  ์ด ์ „์ฒด ์˜ค์ฐจ์— ๋Œ€ํ•œ ํ‰๊ท ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. cost = losses.mean() print(cost) tensor(0.6931, grad_fn=<MeanBackward1>) ๊ฒฐ๊ณผ์ ์œผ๋กœ ์–ป์€ cost๋Š” 0.6931์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋น„์šฉ ํ•จ์ˆ˜์˜ ๊ฐ’์„ ์ง์ ‘ ๊ตฌํ˜„ํ•˜์˜€๋Š”๋ฐ, ์‚ฌ์‹ค ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์ด๋ฏธ ๊ตฌํ˜„ํ•ด์„œ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ torch.nn.functional as F์™€ ๊ฐ™์ด ์ž„ํฌํŠธ ํ•œ ํ›„์— F.binary_cross_entropy(์˜ˆ์ธก๊ฐ’, ์‹ค์ œ ๊ฐ’)๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. F.binary_cross_entropy(hypothesis, y_train) tensor(0.6931, grad_fn=<BinaryCrossEntropyBackward>) ๋™์ผํ•˜๊ฒŒ cost๊ฐ€ 0.6931์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ํ›ˆ๋ จ ๊ณผ์ •๊นŒ์ง€ ์ถ”๊ฐ€ํ•œ ์ „์ฒด ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. x_data = [[1, 2], [2, 3], [3, 1], [4, 3], [5, 3], [6, 2]] y_data = [[0], [0], [0], [1], [1], [1]] x_train = torch.FloatTensor(x_data) y_train = torch.FloatTensor(y_data) # ๋ชจ๋ธ ์ดˆ๊ธฐํ™” W = torch.zeros((2, 1), requires_grad=True) b = torch.zeros(1, requires_grad=True) # optimizer ์„ค์ • optimizer = optim.SGD([W, b], lr=1) nb_epochs = 1000 for epoch in range(nb_epochs + 1): # Cost ๊ณ„์‚ฐ hypothesis = torch.sigmoid(x_train.matmul(W) + b) cost = -(y_train * torch.log(hypothesis) + (1 - y_train) * torch.log(1 - hypothesis)).mean() # cost๋กœ H(x) ๊ฐœ์„  optimizer.zero_grad() cost.backward() optimizer.step() # 100๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ if epoch % 100 == 0: print('Epoch {:4d}/{} Cost: {:.6f}'.format( epoch, nb_epochs, cost.item() )) Epoch 0/1000 Cost: 0.693147 ... ์ค‘๋žต ... Epoch 1000/1000 Cost: 0.019852 ํ•™์Šต์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ›ˆ๋ จํ–ˆ๋˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ, ์ œ๋Œ€๋กœ ์˜ˆ์ธกํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ W์™€ b๋Š” ํ›ˆ๋ จ ํ›„์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ W์™€ b๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ˆ์ธก๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. hypothesis = torch.sigmoid(x_train.matmul(W) + b) print(hypothesis) tensor([[2.7648e-04], [3.1608e-02], [3.8977e-02], [9.5622e-01], [9.9823e-01], [9.9969e-01]], grad_fn=<SigmoidBackward>) ํ˜„์žฌ ์œ„ ๊ฐ’๋“ค์€ 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ 0.5๋ฅผ ๋„˜์œผ๋ฉด True, ๋„˜์ง€ ์•Š์œผ๋ฉด False๋กœ ๊ฐ’์„ ์ •ํ•˜์—ฌ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. prediction = hypothesis >= torch.FloatTensor([0.5]) print(prediction) tensor([[False], [False], [False], [ True], [ True], [ True]]) ์‹ค์ œ ๊ฐ’์€ [[0], [0], [0], [1], [1], [1]]์ด๋ฏ€๋กœ, ์ด๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ False, False, False, True, True, True์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๊ธฐ์กด์˜ ์‹ค์ œ ๊ฐ’๊ณผ ๋™์ผํ•˜๊ฒŒ ์˜ˆ์ธกํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ์ด ๋œ ํ›„์˜ W์™€ b์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(W) print(b) tensor([[3.2530], [1.5179]], requires_grad=True) tensor([-14.4819], requires_grad=True) 04-02 nn.Module๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ์ž ๊น๋งŒ ๋ณต์Šต์„ ํ•ด๋ณด๋ฉด ์„ ํ˜• ํšŒ๊ท€ ๋ชจ๋ธ์˜ ๊ฐ€์„ค์‹์€ ( ) W + ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฐ€์„ค์‹์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํŒŒ์ด ํ† ์น˜์˜ nn.Linear()๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ๊ฐ€์„ค์‹์€ ( ) s g o d ( x b ) ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” nn.Sigmoid()๋ฅผ ํ†ตํ•ด์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋ฏ€๋กœ ๊ฒฐ๊ณผ์ ์œผ๋กœ nn.Linear()์˜ ๊ฒฐ๊ณผ๋ฅผ nn.Sigmoid()๋ฅผ ๊ฑฐ์น˜๊ฒŒ ํ•˜๋ฉด ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ๊ฐ€์„ค์‹์ด ๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜๋ฅผ ํ†ตํ•ด ์ด๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 1. ํŒŒ์ด ํ† ์น˜์˜ nn.Linear์™€ nn.Sigmoid๋กœ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๊ตฌํ˜„ํ•˜๊ธฐ ์šฐ์„  ๊ตฌํ˜„์„ ์œ„ํ•ด ํ•„์š”ํ•œ ํŒŒ์ด ํ† ์น˜์˜ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim torch.manual_seed(1) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ํ…์„œ๋กœ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. x_data = [[1, 2], [2, 3], [3, 1], [4, 3], [5, 3], [6, 2]] y_data = [[0], [0], [0], [1], [1], [1]] x_train = torch.FloatTensor(x_data) y_train = torch.FloatTensor(y_data) nn.Sequential()์€ nn.Module ์ธต์„ ์ฐจ๋ก€๋กœ ์Œ“์„ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ๋’ค์—์„œ ์ด๋ฅผ ์ด์šฉํ•ด์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๊ตฌํ˜„ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ์œผ๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค. ์กฐ๊ธˆ ์‰ฝ๊ฒŒ ๋งํ•ด์„œ nn.Sequential()์€ x b ์™€ ๊ฐ™์€ ์ˆ˜์‹๊ณผ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜ ๋“ฑ๊ณผ ๊ฐ™์€ ์—ฌ๋Ÿฌ ํ•จ์ˆ˜๋“ค์„ ์—ฐ๊ฒฐํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•ด์„œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. model = nn.Sequential( nn.Linear(2, 1), # input_dim = 2, output_dim = 1 nn.Sigmoid() # ์ถœ๋ ฅ์€ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ๊ฑฐ์นœ๋‹ค ) ํ˜„์žฌ W์™€ b๋Š” ๋žœ๋ค ์ดˆ๊ธฐํ™”๊ฐ€ ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด ์˜ˆ์ธก๊ฐ’์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. model(x_train) tensor([[0.4020], [0.4147], [0.6556], [0.5948], [0.6788], [0.8061]], grad_fn=<SigmoidBackward>) 6 ร— 1 ํฌ๊ธฐ์˜ ์˜ˆ์ธก๊ฐ’ ํ…์„œ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ˜„์žฌ W์™€ b๋Š” ์ž„์˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ ํ˜„์žฌ์˜ ์˜ˆ์ธก์€ ์˜๋ฏธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ํ›ˆ๋ จํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด 100๋ฒˆ์˜ ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. # optimizer ์„ค์ • optimizer = optim.SGD(model.parameters(), lr=1) nb_epochs = 1000 for epoch in range(nb_epochs + 1): # H(x) ๊ณ„์‚ฐ hypothesis = model(x_train) # cost ๊ณ„์‚ฐ cost = F.binary_cross_entropy(hypothesis, y_train) # cost๋กœ H(x) ๊ฐœ์„  optimizer.zero_grad() cost.backward() optimizer.step() # 20๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ if epoch % 10 == 0: prediction = hypothesis >= torch.FloatTensor([0.5]) # ์˜ˆ์ธก๊ฐ’์ด 0.5๋ฅผ ๋„˜์œผ๋ฉด True๋กœ ๊ฐ„์ฃผ correct_prediction = prediction.float() == y_train # ์‹ค์ œ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ๋งŒ True๋กœ ๊ฐ„์ฃผ accuracy = correct_prediction.sum().item() / len(correct_prediction) # ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐ print('Epoch {:4d}/{} Cost: {:.6f} Accuracy {:2.2f}%'.format( # ๊ฐ ์—ํฌํฌ๋งˆ๋‹ค ์ •ํ™•๋„๋ฅผ ์ถœ๋ ฅ epoch, nb_epochs, cost.item(), accuracy * 100, )) ๊ฐ ์—ํฌํฌ๋งˆ๋‹ค ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์ •ํ™•๋„๋„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Epoch 0/1000 Cost: 0.539713 Accuracy 83.33% ... ์ค‘๋žต ... Epoch 1000/1000 Cost: 0.019843 Accuracy 100.00% ์ค‘๊ฐ„๋ถ€ํ„ฐ ์ •ํ™•๋„๋Š” 100%๊ฐ€ ๋‚˜์˜ค๊ธฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์˜ˆ์ธก๊ฐ’์„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model(x_train) tensor([[0.0240], [0.1476], [0.2739], [0.7967], [0.9491], [0.9836]], grad_fn=<SigmoidBackward>) 0.5๋ฅผ ๋„˜์œผ๋ฉด True, ๊ทธ๋ณด๋‹ค ๋‚ฎ์œผ๋ฉด False๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’์€ [[0], [0], [0], [1], [1], [1]]์ž…๋‹ˆ๋‹ค. ์ด๋Š” False, False, False, True, True, True์— ํ•ด๋‹น๋˜๋ฏ€๋กœ ์ „๋ถ€ ์‹ค์ œ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋„๋ก ์˜ˆ์ธกํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ํ›„์˜ W์™€ b์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(list(model.parameters())) [Parameter containing: tensor([[3.2534, 1.5181]], requires_grad=True), Parameter containing: tensor([-14.4839], requires_grad=True)] ์ถœ๋ ฅ๊ฐ’์ด ์•ž ์ฑ•ํ„ฐ์—์„œ nn.Module์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ•œ ์‹ค์Šต์—์„œ ์–ป์—ˆ๋˜ W์™€ b์™€ ๊ฑฐ์˜ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. 2. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€. ์‚ฌ์‹ค ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์œผ๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๊ทธ๋ฆผ์—์„œ ๊ฐ ํ™”์‚ดํ‘œ๋Š” ์ž…๋ ฅ๊ณผ ๊ณฑํ•ด์ง€๋Š” ๊ฐ€์ค‘์น˜ ๋˜๋Š” ํŽธํ–ฅ์ž…๋‹ˆ๋‹ค. ๊ฐ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ๊ฒ€์€์ƒ‰ ํ™”์‚ดํ‘œ๋Š” ๊ฐ€์ค‘์น˜, ํšŒ์ƒ‰ ํ™”์‚ดํ‘œ๋Š” ํŽธํ–ฅ์ด ๊ณฑํ•ด์ง‘๋‹ˆ๋‹ค. ๊ฐ ์ž…๋ ฅ ๋Š” ๊ฐ ์ž…๋ ฅ์˜ ๊ฐ€์ค‘์น˜ ์™€ ๊ณฑํ•ด์ง€๊ณ , ํŽธํ–ฅ ๋Š” ์ƒ์ˆ˜ 1๊ณผ ๊ณฑํ•ด์ง€๋Š” ๊ฒƒ์œผ๋กœ ํ‘œํ˜„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ถœ๋ ฅํ•˜๊ธฐ ์ „์— ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์œ„์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹ค์ค‘ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ( ) s g o d ( 1 1 x w + ) ๋’ค์—์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๋ฐฐ์šฐ๋ฉด์„œ ์–ธ๊ธ‰ํ•˜๊ฒ ์ง€๋งŒ, ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์€๋‹‰์ธต์—์„œ๋Š” ๊ฑฐ์˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์™€ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ : https://hyeonnii.tistory.com/239 04-03 ํด๋ž˜์Šค๋กœ ํŒŒ์ด ํ† ์น˜ ๋ชจ๋ธ ๊ตฌํ˜„ํ•˜๊ธฐ ํŒŒ์ด ํ† ์น˜์˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ตฌํ˜„์ฒด๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•  ๋•Œ ํด๋ž˜์Šค(Class)๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ์™€ ๋‹ค๋ฅธ ์ ์€ ์˜ค์ง ํด๋ž˜์Šค๋กœ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ–ˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. 1. ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์•ž์„œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจ๋ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. model = nn.Sequential( nn.Linear(2, 1), # input_dim = 2, output_dim = 1 nn.Sigmoid() # ์ถœ๋ ฅ์€ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ๊ฑฐ์นœ๋‹ค ) ์ด๋ฅผ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. class BinaryClassifier(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(2, 1) self.sigmoid = nn.Sigmoid() def forward(self, x): return self.sigmoid(self.linear(x)) ์œ„์™€ ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ ๊ตฌํ˜„<NAME>์€ ๋Œ€๋ถ€๋ถ„์˜ ํŒŒ์ด ํ† ์น˜ ๊ตฌํ˜„์ฒด์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ˜๋“œ์‹œ ์ˆ™์ง€ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋ž˜์Šค(class) ํ˜•ํƒœ์˜ ๋ชจ๋ธ์€ nn.Module ์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  __init__()์—์„œ ๋ชจ๋ธ์˜ ๊ตฌ์กฐ์™€ ๋™์ ์„ ์ •์˜ํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํŒŒ์ด์ฌ์—์„œ ๊ฐ์ฒด๊ฐ€ ๊ฐ–๋Š” ์†์„ฑ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• ๋กœ, ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. super() ํ•จ์ˆ˜๋ฅผ ๋ถ€๋ฅด๋ฉด ์—ฌ๊ธฐ์„œ ๋งŒ๋“  ํด๋ž˜์Šค๋Š” nn.Module ํด๋ž˜์Šค์˜ ์†์„ฑ๋“ค์„ ๊ฐ€์ง€๊ณ  ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. foward() ํ•จ์ˆ˜๋Š” ๋ชจ๋ธ์ด ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ forward ์—ฐ์‚ฐ์„ ์ง„ํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด forward() ํ•จ์ˆ˜๋Š” model ๊ฐ์ฒด๋ฅผ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜๋ฉด ์ž๋™์œผ๋กœ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด model ์ด๋ž€ ์ด๋ฆ„์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ ํ›„, model(์ž…๋ ฅ ๋ฐ์ดํ„ฐ)์™€ ๊ฐ™์€<NAME>์œผ๋กœ ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ž๋™์œผ๋กœ forward ์—ฐ์‚ฐ์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ( ) ์‹์— ์ž…๋ ฅ ๋กœ๋ถ€ํ„ฐ ์˜ˆ์ธก๋œ ๋ฅผ ์–ป๋Š” ๊ฒƒ์„ forward ์—ฐ์‚ฐ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด์ œ ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ฌ๋ผ์ง„ ์ ์€ ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๋Š” ์ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ฝ”๋“œ๋Š” ์ „๋ถ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim torch.manual_seed(1) x_data = [[1, 2], [2, 3], [3, 1], [4, 3], [5, 3], [6, 2]] y_data = [[0], [0], [0], [1], [1], [1]] x_train = torch.FloatTensor(x_data) y_train = torch.FloatTensor(y_data) class BinaryClassifier(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(2, 1) self.sigmoid = nn.Sigmoid() def forward(self, x): return self.sigmoid(self.linear(x)) model = BinaryClassifier() # optimizer ์„ค์ • optimizer = optim.SGD(model.parameters(), lr=1) nb_epochs = 1000 for epoch in range(nb_epochs + 1): # H(x) ๊ณ„์‚ฐ hypothesis = model(x_train) # cost ๊ณ„์‚ฐ cost = F.binary_cross_entropy(hypothesis, y_train) # cost๋กœ H(x) ๊ฐœ์„  optimizer.zero_grad() cost.backward() optimizer.step() # 20๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ if epoch % 10 == 0: prediction = hypothesis >= torch.FloatTensor([0.5]) # ์˜ˆ์ธก๊ฐ’์ด 0.5๋ฅผ ๋„˜์œผ๋ฉด True๋กœ ๊ฐ„์ฃผ correct_prediction = prediction.float() == y_train # ์‹ค์ œ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ๋งŒ True๋กœ ๊ฐ„์ฃผ accuracy = correct_prediction.sum().item() / len(correct_prediction) # ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐ print('Epoch {:4d}/{} Cost: {:.6f} Accuracy {:2.2f}%'.format( # ๊ฐ ์—ํฌํฌ๋งˆ๋‹ค ์ •ํ™•๋„๋ฅผ ์ถœ๋ ฅ epoch, nb_epochs, cost.item(), accuracy * 100, )) 05. [ML ์ž…๋ฌธ โœ] - ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€(Softmax Regression) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” 3๊ฐœ ์ด์ƒ์˜ ์„ ํƒ์ง€๋กœ๋ถ€ํ„ฐ 1๊ฐœ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฌธ์ œ์ธ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-Class classification)๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 05-01 ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-Hot Encoding) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋ฒ”์ฃผํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ๋ ˆ์ด๋ธ”์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›Œ๋ด…์‹œ๋‹ค. 1. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-hot encoding)์ด๋ž€? ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์€ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ์„ ํƒ์ง€์˜ ๊ฐœ์ˆ˜๋งŒํผ์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋ฉด์„œ, ๊ฐ ์„ ํƒ์ง€์˜ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ์›์†Œ์—๋Š” 1, ๋‚˜๋จธ์ง€ ์›์†Œ๋Š” 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก ํ•˜๋Š” ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ•์•„์ง€, ๊ณ ์–‘์ด, ๋ƒ‰์žฅ๊ณ ๋ผ๋Š” 3๊ฐœ์˜ ์„ ํƒ์ง€๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  ๊ฐ ์„ ํƒ์ง€์— ์ˆœ์ฐจ์ ์œผ๋กœ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ž„์˜๋กœ ๊ฐ•์•„์ง€๋Š” 0๋ฒˆ ์ธ๋ฑ์Šค, ๊ณ ์–‘์ด๋Š” 1๋ฒˆ ์ธ๋ฑ์Šค, ๋ƒ‰์žฅ๊ณ ๋Š” 2๋ฒˆ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜์˜€๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋•Œ ๊ฐ ์„ ํƒ์ง€์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ๋œ ๋ฒกํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ•์•„์ง€ = [1, 0, 0] ๊ณ ์–‘์ด = [0, 1, 0] ๋ƒ‰์žฅ๊ณ  = [0, 0, 1] ์ด ์„ ํƒ์ง€๋Š” 3๊ฐœ์˜€์œผ๋ฏ€๋กœ ์œ„ ๋ฒกํ„ฐ๋“ค์€ ์ „๋ถ€ 3์ฐจ์›์˜ ๋ฒกํ„ฐ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์„ ํƒ์ง€์˜ ๋ฒกํ„ฐ๋“ค์„ ๋ณด๋ฉด ํ•ด๋‹น ์„ ํƒ์ง€์˜ ์ธ๋ฑ์Šค์—๋งŒ 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ , ๋‚˜๋จธ์ง€ ์›์†Œ๋“ค์€ 0์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ณ ์–‘์ด๋Š” 1๋ฒˆ ์ธ๋ฑ์Šค์˜€์œผ๋ฏ€๋กœ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์œผ๋กœ ์–ป์€ ๋ฒกํ„ฐ์—์„œ 1๋ฒˆ ์ธ๋ฑ์Šค๋งŒ 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์œผ๋กœ ํ‘œํ˜„๋œ ๋ฒกํ„ฐ๋ฅผ ์›-ํ•ซ ๋ฒกํ„ฐ(one-hot vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๋ฌด์ž‘์œ„์„ฑ ๊ผญ ์‹ค์ œ ๊ฐ’์„ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•ด์•ผ๋งŒ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„์˜ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๊ฐ€ ๊ฐ ํด๋ž˜์Šค ๊ฐ„์˜ ๊ด€๊ณ„๊ฐ€ ๊ท ๋“ฑํ•˜๋‹ค๋Š” ์ ์—์„œ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ์ด๋Ÿฌํ•œ ์ ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ ˆํ•œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋‹ค์ˆ˜์˜ ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฌธ์ œ์—์„œ๋Š” ์ด์ง„ ๋ถ„๋ฅ˜์ฒ˜๋Ÿผ 2๊ฐœ์˜ ์ˆซ์ž ๋ ˆ์ด๋ธ”์ด ์•„๋‹ˆ๋ผ ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ˆซ์ž ๋ ˆ์ด๋ธ”์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ง๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ ˆ์ด๋ธ”๋ง ๋ฐฉ๋ฒ•์€ ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ํด๋ž˜์Šค ์ „์ฒด์— ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ๋ ˆ์ด๋ธ”์ด {red, green, blue}์™€ ๊ฐ™์ด 3๊ฐœ๋ผ๋ฉด ๊ฐ๊ฐ 0, 1, 2๋กœ ๋ ˆ์ด๋ธ”์„ ํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ํด๋ž˜์Šค๊ฐ€ 4๊ฐœ๊ณ  ์ธ๋ฑ์Šค๋ฅผ ์ˆซ์ž 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ํ•˜๋ฉด {baby, child, adolescent, adult}๋ผ๋ฉด 1, 2, 3, 4๋กœ ๋ ˆ์ด๋ธ”์„ ํ•ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ผ๋ฐ˜์ ์ธ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ ๋ ˆ์ด๋ธ”๋ง ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์œ„์™€ ๊ฐ™์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์•„๋‹ˆ๋ผ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ณด๋‹ค ํด๋ž˜์Šค์˜ ์„ฑ์งˆ์„ ์ž˜ ํ‘œํ˜„ํ•˜์˜€๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋ฅผ ์•Œ์•„๋ด…์‹œ๋‹ค. Banana, Tomato, Apple๋ผ๋Š” 3๊ฐœ์˜ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๋ ˆ์ด๋ธ”์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ๊ฐ 1, 2, 3์„ ๋ถ€์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์†์‹ค ํ•จ์ˆ˜๋กœ ์„ ํ˜• ํšŒ๊ท€ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šด ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ MSE๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์–ด๋–ค ์˜คํ•ด๋ฅผ ๋ถˆ๋Ÿฌ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์‹์€ ์•ž์„œ ์„ ํ˜• ํšŒ๊ท€์—์„œ ๋ฐฐ์› ๋˜ MSE๋ฅผ ๋‹ค์‹œ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜จ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ^ ๋Š” ์˜ˆ์ธก๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. o s f n t o = n i ( i y ^ ) ์ง๊ด€์ ์ธ ์˜ค์ฐจ ํฌ๊ธฐ ๋น„๊ต๋ฅผ ์œ„ํ•ด ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ์ˆ˜์‹์€ ์ œ์™ธํ•˜๊ณ  ์ œ๊ณฑ ์˜ค์ฐจ๋กœ๋งŒ ํŒ๋‹จํ•ด ๋ด…์‹œ๋‹ค. ์‹ค์ œ ๊ฐ’์ด Tomato ์ผ ๋•Œ ์˜ˆ์ธก๊ฐ’์ด Banana์ด์—ˆ๋‹ค๋ฉด ์ œ๊ณฑ ์˜ค์ฐจ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( โˆ’ ) = ์‹ค์ œ ๊ฐ’์ด Apple ์ผ ๋•Œ ์˜ˆ์ธก๊ฐ’์ด Banana์ด์—ˆ๋‹ค๋ฉด ์ œ๊ณฑ ์˜ค์ฐจ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( โˆ’ ) = ์ฆ‰, Banana๊ณผ Tomato ์‚ฌ์ด์˜ ์˜ค์ฐจ๋ณด๋‹ค Banana๊ณผ Apple์˜ ์˜ค์ฐจ๊ฐ€ ๋” ํฝ๋‹ˆ๋‹ค. ์ด๋Š” ๊ธฐ๊ณ„์—๊ฒŒ Banana๊ฐ€ Apple๋ณด๋‹ค๋Š” Tomato์— ๋” ๊ฐ€๊น๋‹ค๋Š” ์ •๋ณด๋ฅผ ์ฃผ๋Š” ๊ฒƒ๊ณผ ๋‹ค๋ฆ„์—†์Šต๋‹ˆ๋‹ค. ๋” ๋งŽ์€ ํด๋ž˜์Šค์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ–ˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. {Banana :1, Tomato :2, Apple :3, Strawberry :4, ... Water melon :10} ์ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์€ Banana๊ฐ€ Water melon๋ณด๋‹ค๋Š” Tomato์— ๋” ๊ฐ€๊น๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ถ€์—ฌํ•˜๊ณ ์ž ํ–ˆ๋˜ ์ •๋ณด๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์˜ ์ˆœ์„œ ์ •๋ณด๊ฐ€ ๋„์›€์ด ๋˜๋Š” ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋„ ๋ฌผ๋ก  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๊ฐ ํด๋ž˜์Šค๊ฐ€ ์ˆœ์„œ์˜ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์–ด์„œ ํšŒ๊ท€๋ฅผ ํ†ตํ•ด์„œ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด {baby, child, adolescent, adult}๋‚˜ {1์ธต, 2์ธต, 3์ธต, 4์ธต}์ด๋‚˜ {10๋Œ€, 20๋Œ€, 30๋Œ€, 40๋Œ€}์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ๋ฐ˜์ ์ธ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์—์„œ๋Š” ๊ฐ ํด๋ž˜์Šค๋Š” ์ˆœ์„œ์˜ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ฐ ํด๋ž˜์Šค ๊ฐ„์˜ ์˜ค์ฐจ๋Š” ๊ท ๋“ฑํ•œ ๊ฒƒ์ด ์˜ณ์Šต๋‹ˆ๋‹ค. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ๊ณผ ๋‹ฌ๋ฆฌ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์€ ๋ถ„๋ฅ˜ ๋ฌธ์ œ ๋ชจ๋“  ํด๋ž˜์Šค ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๊ท ๋“ฑํ•˜๊ฒŒ ๋ถ„๋ฐฐํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์„ธ ๊ฐœ์˜ ์นดํ…Œ๊ณ ๋ฆฌ์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ†ตํ•ด์„œ ๋ ˆ์ด๋ธ”์„ ์ธ์ฝ”๋”ฉํ–ˆ์„ ๋•Œ ๊ฐ ํด๋ž˜์Šค ๊ฐ„์˜ ์ œ๊ณฑ ์˜ค์ฐจ๊ฐ€ ๊ท ๋“ฑํ•จ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ( ( , , ) ( , , ) ) = ( โˆ’ ) + ( โˆ’ ) + ( โˆ’ ) = ( ( , , ) ( , , ) ) = ( โˆ’ ) + ( โˆ’ ) + ( โˆ’ ) = ๋‹ค๋ฅด๊ฒŒ ํ‘œํ˜„ํ•˜๋ฉด ๋ชจ๋“  ํด๋ž˜์Šค์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ†ตํ•ด ์–ป์€ ์›-ํ•ซ ๋ฒกํ„ฐ๋“ค์€ ๋ชจ๋“  ์Œ์— ๋Œ€ํ•ด์„œ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด๋„ ์ „๋ถ€ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ์ด์ฒ˜๋Ÿผ ๊ฐ ํด๋ž˜์Šค์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด ๋ฌด์ž‘์œ„์„ฑ์„ ๊ฐ€์ง„๋‹ค๋Š” ์ ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋‹ค์‹œ ์–ธ๊ธ‰๋˜๊ฒ ์ง€๋งŒ ์ด๋Ÿฌํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๊ด€๊ณ„์˜ ๋ฌด์ž‘์œ„์„ฑ์€ ๋•Œ๋กœ๋Š” ๋‹จ์–ด์˜ ์œ ์‚ฌ์„ฑ์„ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์œผ๋กœ ์–ธ๊ธ‰๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 05-02 ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€(Softmax Regression) ์ดํ•ดํ•˜๊ธฐ ์•ž์„œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ํ†ตํ•ด 2๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ 1๊ฐœ๋ฅผ ๊ณ ๋ฅด๋Š” ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification)๋ฅผ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ํ†ตํ•ด 3๊ฐœ ์ด์ƒ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ 1๊ฐœ๋ฅผ ๊ณ ๋ฅด๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-Class Classification)๋ฅผ ์‹ค์Šตํ•ด ๋ด…์‹œ๋‹ค. 1. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-class Classification) ์ด์ง„ ๋ถ„๋ฅ˜๊ฐ€ ๋‘ ๊ฐœ์˜ ๋‹ต ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ์˜€๋‹ค๋ฉด, ์„ธ ๊ฐœ ์ด์ƒ์˜ ๋‹ต ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ๋ฅผ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-class Classification)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋ฌธ์ œ๋Š” ๊ฝƒ๋ฐ›์นจ ๊ธธ์ด, ๊ฝƒ๋ฐ›์นจ ๋„“์ด, ๊ฝƒ์žŽ ๊ธธ์ด, ๊ฝƒ์žŽ ๋„“์ด๋ผ๋Š” 4๊ฐœ์˜ ํŠน์„ฑ(feature)๋กœ๋ถ€ํ„ฐ setosa, versicolor, virginica๋ผ๋Š” 3๊ฐœ์˜ ๋ถ“๊ฝƒ ํ’ˆ์ข… ์ค‘ ์–ด๋–ค ํ’ˆ์ข…์ธ์ง€๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ๋กœ ์ „ํ˜•์ ์ธ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. SepalLengthCm( 1 ) SepalWidthCm( 2 ) PetalLengthCm( 3 ) PetalWidthCm( 4 ) Species(y) 5.1 3.5 1.4 0.2 setosa 4.9 3.0 1.4 0.2 setosa 5.8 2.6 4.0 1.2 versicolor 6.7 3.0 5.2 2.3 virginica 5.6 2.8 4.9 2.0 virginica ์œ„ ๋ถ“๊ฝƒ ํ’ˆ์ข… ๋ถ„๋ฅ˜ํ•˜๊ธฐ ๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ’€์ง€ ๊ณ ๋ฏผํ•˜๊ธฐ ์œ„ํ•ด ์•ž์„œ ๋ฐฐ์šด ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ๋ณต์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์˜ ์„ค๋ช…์—์„œ ์ž…๋ ฅ์€, ๊ฐ€์ค‘์น˜๋Š”, ํŽธํ–ฅ์€, ์ถœ๋ ฅ์€ ^ ๋กœ ๊ฐ ๋ณ€์ˆ˜๋Š” ๋ฒกํ„ฐ ๋˜๋Š” ํ–‰๋ ฌ๋กœ ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ^ ์€ ์˜ˆ์ธก๊ฐ’์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๊ฐ€์„ค์‹์—์„œ ( ) ๋Œ€์‹  ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 1. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” ์˜ˆ์ธก๊ฐ’์„ 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋กœ ๊ตฌํ˜„ํ•˜์˜€์„ ๋•Œ, ์ถœ๋ ฅ์ด 0.75์ด๋ผ๋ฉด ์ด๋Š” ์ด๋ฉ”์ผ์ด ์ŠคํŒธ์ผ ํ™•๋ฅ ์ด 75%๋ผ๋Š” ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ, ์ŠคํŒธ ๋ฉ”์ผ์ด ์•„๋‹ ํ™•๋ฅ ์€ 25%๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ํ™•๋ฅ ์˜ ์ดํ•ฉ์€ 1์ž…๋‹ˆ๋‹ค. ๊ฐ€์„ค : ( ) s g o d ( X B ) 2. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋Š” ํ™•๋ฅ ์˜ ์ดํ•ฉ์ด 1์ด ๋˜๋Š” ์ด ์•„์ด๋””์–ด๋ฅผ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์— ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋Š” ๊ฐ ํด๋ž˜์Šค. ์ฆ‰, ๊ฐ ์„ ํƒ์ง€๋งˆ๋‹ค ์†Œ์ˆ˜ ํ™•๋ฅ ์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ด ํ™•๋ฅ ์˜ ํ•ฉ์€ 1์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๊ฐ ์„ ํƒ์ง€๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋Š” ์„ ํƒ์ง€์˜ ๊ฐœ์ˆ˜๋งŒํผ์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค๊ณ , ํ•ด๋‹น ๋ฒกํ„ฐ๊ฐ€ ๋ฒกํ„ฐ์˜ ๋ชจ๋“  ์›์†Œ์˜ ํ•ฉ์ด 1์ด ๋˜๋„๋ก ์›์†Œ๋“ค์˜ ๊ฐ’์„ ๋ณ€ํ™˜์‹œํ‚ค๋Š” ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๋ถ“๊ฝƒ ํ’ˆ์ข… ๋ถ„๋ฅ˜ํ•˜๊ธฐ ๋ฌธ์ œ ๋“ฑ๊ณผ ๊ฐ™์ด ์„ ํƒ์ง€์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ์ผ ๋•Œ, 3์ฐจ์› ๋ฒกํ„ฐ๊ฐ€ ์–ด๋–ค ํ•จ์ˆ˜?๋ฅผ ์ง€๋‚˜ ์›์†Œ์˜ ์ดํ•ฉ์ด 1์ด ๋˜๋„๋ก ์›์†Œ๋“ค์˜ ๊ฐ’์ด ๋ณ€ํ™˜๋˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒ ์ง€๋งŒ, ์ด ํ•จ์ˆ˜๋ฅผ ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์„ค : ( ) s f m x ( X B ) 2. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜(Softmax function) ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•˜๋Š” ์ •๋‹ต์ง€(ํด๋ž˜์Šค)์˜ ์ด๊ฐœ์ˆ˜๋ฅผ k๋ผ๊ณ  ํ•  ๋•Œ, k ์ฐจ์›์˜ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ํ™•๋ฅ ์„ ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ˆ˜์‹์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ณ , ๊ทธ ํ›„์—๋Š” ๊ทธ๋ฆผ์œผ๋กœ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ดํ•ด k ์ฐจ์›์˜ ๋ฒกํ„ฐ์—์„œ i ๋ฒˆ์งธ ์›์†Œ๋ฅผ i , i ๋ฒˆ์งธ ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์„ i ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” i ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. i e i j 1 e j f r i 1 2. . ์œ„์—์„œ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ฐจ๊ทผ์ฐจ๊ทผ ์ ์šฉํ•ด ๋ด…์‹œ๋‹ค. ์œ„์—์„œ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ k=3์ด๋ฏ€๋กœ 3์ฐจ์› ๋ฒกํ„ฐ = [ 1 z z ] ์˜ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ถœ๋ ฅ์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ธก๊ฐ’ o t a ( ) [ z โˆ‘ = 3 z e 2 j 1 e j e 3 j 1 e j ] [ 1 p, 3 ] p, 2 p ๊ฐ๊ฐ์€ 1๋ฒˆ ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , 2๋ฒˆ ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , 3๋ฒˆ ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ ๊ฐ๊ฐ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ์ดํ•ฉ์€ 1์ด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” 3๊ฐœ์˜ ํด๋ž˜์Šค๋Š” virginica, setosa, versicolor์ด๋ฏ€๋กœ ์ด๋Š” ๊ฒฐ๊ตญ ์ฃผ์–ด์ง„ ์ž…๋ ฅ์ด virginica ์ผ ํ™•๋ฅ , setosa ์ผ ํ™•๋ฅ , versicolor ์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” i๊ฐ€ 1์ผ ๋•Œ๋Š” virginica ์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๊ณ , 2์ผ ๋•Œ๋Š” setosa ์ผ ํ™•๋ฅ , 3์ผ ๋•Œ๋Š” versicolor ์ผ ํ™•๋ฅ ์ด๋ผ๊ณ  ์ง€์ •ํ•˜์˜€๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ์ง€์ • ์ˆœ์„œ๋Š” ๋ฌธ์ œ๋ฅผ ํ’€๊ณ ์ž ํ•˜๋Š” ์‚ฌ๋žŒ์˜ ๋ฌด์ž‘์œ„ ์„ ํƒ์ž…๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ์‹์„ ๋ฌธ์ œ์— ๋งž๊ฒŒ ๋‹ค์‹œ ์“ฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. o t a ( ) [ z โˆ‘ = 3 z e 2 j 1 e j e 3 j 1 e j ] [ 1 p, 3 ] ๋‹ค์†Œ ๋ณต์žกํ•ด ๋ณด์ด์ง€๋งŒ ์–ด๋ ค์šด ๊ฐœ๋…์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ k ๊ฐœ์ผ ๋•Œ, k ์ฐจ์›์˜ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋ชจ๋“  ๋ฒกํ„ฐ ์›์†Œ์˜ ๊ฐ’์„ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์—ฌ ๋‹ค์‹œ k ์ฐจ์›์˜ ๋ฒกํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ๋‚ด์šฉ์„ ์‹์œผ๋กœ ๊ธฐ์žฌํ•˜์˜€์„ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋ฐฉ๊ธˆ ๋ฐฐ์šด ๊ฐœ๋…์„ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ๋‹ค์‹œ ์„ค๋ช…ํ•˜๋ฉด์„œ ๋” ๊นŠ์ด ๋“ค์–ด๊ฐ€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2) ๊ทธ๋ฆผ์„ ํ†ตํ•œ ์ดํ•ด ์œ„์˜ ๊ทธ๋ฆผ์— ์ ์ฐจ ์‚ด์„ ๋ถ™์—ฌ๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ 1๊ฐœ์”ฉ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ์ฆ‰, ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—๋Š” ๋‘ ๊ฐ€์ง€ ์งˆ๋ฌธ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์งˆ๋ฌธ์€ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์งˆ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋Š” 4๊ฐœ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ ์ด๋Š” ๋ชจ๋ธ์ด 4์ฐจ์› ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์†Œํ”„ํŠธ๋งฅ์Šค์˜ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฒกํ„ฐ๋Š” ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ์–ด๋–ค ๊ฐ€์ค‘์น˜ ์—ฐ์‚ฐ์„ ํ†ตํ•ด 3์ฐจ์› ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” 3์ฐจ์› ๋ฒกํ„ฐ๋ฅผ ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ๋ฒกํ„ฐ๋ฅผ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ๋กœ ์ฐจ์›์„ ์ถ•์†Œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์› ์ˆ˜๋งŒํผ ๊ฒฐ๊ด๊ฐ’์˜ ๋‚˜์˜ค๋„๋ก ๊ฐ€์ค‘์น˜ ๊ณฑ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ํ™”์‚ดํ‘œ๋Š” ์ด (4 ร— 3 = 12) 12๊ฐœ์ด๋ฉฐ ์ „๋ถ€ ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง€๊ณ , ํ•™์Šต ๊ณผ์ •์—์„œ ์ ์ฐจ์ ์œผ๋กœ ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฐ€์ค‘์น˜๋กœ ๊ฐ’์ด ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์งˆ๋ฌธ์€ ์˜ค์ฐจ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์งˆ๋ฌธ์ž…๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ์€ ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๋กœ ๊ฐ ์›์†Œ๋Š” 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด ๊ฐ๊ฐ์€ ํŠน์ • ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ์›์†Œ์ธ 1 ์€ virginica๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , ๋‘ ๋ฒˆ์งธ ์›์†Œ์ธ 2 ๋Š” setosa๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , ์„ธ ๋ฒˆ์งธ ์›์†Œ์ธ 3 ์€ versicolor๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ๋กœ ๊ณ ๋ คํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด ์˜ˆ์ธก๊ฐ’๊ณผ ๋น„๊ต๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค์ œ ๊ฐ’์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ๋Š” ์‹ค์ œ ๊ฐ’์„ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ 1 ๊ฐ€ virginica๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , ๋‘ ๋ฒˆ์งธ ์›์†Œ 2 ๊ฐ€ setosa๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ , ์„ธ ๋ฒˆ์งธ ์›์†Œ 3 ๊ฐ€ versicolor๊ฐ€ ์ •๋‹ต์ผ ํ™•๋ฅ ์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, ๊ฐ ์‹ค์ œ ๊ฐ’์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์€ 1, 2, 3์ด ๋˜๊ณ  ์ด์— ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์‹ค์ œ ๊ฐ’์„ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ์ˆ˜์น˜ํ™”ํ•œ ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ˜„์žฌ ํ’€๊ณ  ์žˆ๋Š” ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ์˜ ์‹ค์ œ ๊ฐ’์ด setosa๋ผ๋ฉด setosa์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” [0 1 0]์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์˜ค์ฐจ๊ฐ€ 0์ด ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๊ฐ€ [0 1 0]์ด ๋˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ด ๋‘ ๋ฒกํ„ฐ์˜ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋Š” ๋น„์šฉ ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด๋Š” ๋’ค์—์„œ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ๋‹ค์‹œ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์•ž์„œ ๋ฐฐ์šด ์„ ํ˜• ํšŒ๊ท€๋‚˜ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์˜ค์ฐจ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํžˆ๋Š” ์„ ํ˜• ํšŒ๊ท€๋‚˜ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํŽธํ–ฅ ๋˜ํ•œ ์—…๋ฐ์ดํŠธ์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์ž…๋ ฅ์„ ํŠน์„ฑ(feature)์˜ ์ˆ˜๋งŒํผ์˜ ์ฐจ์›์„ ๊ฐ€์ง„ ์ž…๋ ฅ ๋ฒกํ„ฐ๋ผ๊ณ  ํ•˜๊ณ , ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์„, ํŽธํ–ฅ์„๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ ์˜ˆ์ธก๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์„ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํŠน์„ฑ์˜ ์ˆ˜์ด๋ฉฐ๋Š” ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 3. ๋ถ“๊ฝƒ ํ’ˆ์ข… ๋ถ„๋ฅ˜ํ•˜๊ธฐ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ์œ„์˜ ๋ถ“๊ฝƒ ํ’ˆ์ข… ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฐ€์„ค์‹์„ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์œ„์˜ ์˜ˆ์ œ์˜ ๋ฐ์ดํ„ฐ๋Š” ์ „์ฒด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 5๊ฐœ, ํŠน์„ฑ์ด 4๊ฐœ์ด๋ฏ€๋กœ 5 ร— 4 ํ–‰๋ ฌ๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. = ( 5.1 3.5 1.4 0.2 4.9 3.0 1.4 0.2 5.8 2.6 4.0 1.2 6.7 3.0 5.2 2.3 5.6 2.8 4.9 2.0 ) ํŽธ์˜๋ฅผ ์œ„ํ•ด ๊ฐ ํ–‰๋ ฌ์˜ ์›์†Œ ์œ„์น˜๋ฅผ ๋ฐ˜์˜ํ•œ ๋ณ€์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. = ( 11 x 12 x 13 x 14 x 21 x 22 x 23 x 24 x 31 x 32 x 33 x 34 x 41 x 42 x 43 x 44 x 51 x 52 x 53 x 54 ) ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์„ ํƒ์ง€๊ฐ€ ์ด 3๊ฐœ์ธ ๋ฌธ์ œ์ด๋ฏ€๋กœ ๊ฐ€์„ค์˜ ์˜ˆ์ธก๊ฐ’์œผ๋กœ ์–ป๋Š” ํ–‰๋ ฌ ^ ์˜ ์—ด์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ํ–‰์€ ํ–‰๋ ฌ์˜ ๊ฐ ํ–‰์˜ ์˜ˆ์ธก๊ฐ’์ด๋ฏ€๋กœ ํ–‰์˜ ํฌ๊ธฐ๋Š” ๋™์ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ํ–‰๋ ฌ ^ ์˜ ํฌ๊ธฐ๋Š” 5 ร— 3์ž…๋‹ˆ๋‹ค. ^ ( 11 y 12 y 13 y 21 y 22 y 23 y 31 y 32 y 33 y 41 y 42 y 43 y 51 y 52 y 53 ) ํฌ๊ธฐ 5 ร— 3์˜ ํ–‰๋ ฌ ^ ๋Š” ํฌ๊ธฐ 5 ร— 4 ์ž…๋ ฅ ํ–‰๋ ฌ๊ณผ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์˜ ๊ณฑ์œผ๋กœ ์–ป์–ด์ง€๋Š” ํ–‰๋ ฌ์ด๋ฏ€๋กœ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ์ถ”์ •์„ ํ†ตํ•ด 4 ร— 3์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ ํ–‰๋ ฌ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. = ( 11 w 12 w 13 w 21 w 22 w 23 w 31 w 32 w 33 w 41 w 42 w 43 ) ํŽธํ–ฅ ํ–‰๋ ฌ ๋Š” ์˜ˆ์ธก๊ฐ’ ํ–‰๋ ฌ ^ ์™€ ํฌ๊ธฐ๊ฐ€ ๋™์ผํ•ด์•ผ ํ•˜๋ฏ€๋กœ 5 ร— 3์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. = ( 1 b b b b b b b b b b b b b b) ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ฐ€์„ค์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ^ s f m x ( W B ) ( 11 y 12 y 13 y 21 y 22 y 23 y 31 y 32 y 33 y 41 y 42 y 43 y 51 y 52 y 53 ) s f m x ( ( 11 x 12 x 13 x 14 x 21 x 22 x 23 x 24 x 31 x 32 x 33 x 34 x 41 x 42 x 43 x 44 x 51 x 52 x 53 x 54 ) ( 11 w 12 w 13 w 21 w 22 w 23 w 31 w 32 w 33 w 41 w 42 w 43 4. ๋น„์šฉ ํ•จ์ˆ˜(Cost function) ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ๋Š” ๋น„์šฉ ํ•จ์ˆ˜๋กœ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๋‹ค์–‘ํ•œ ํ‘œ๊ธฐ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜ ์•„๋ž˜์—์„œ๋Š” ์‹ค์ œ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ,๋Š” ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. j ๋Š” ์‹ค์ œ ๊ฐ’ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, j ๋Š” ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฒˆ์งธ ํด๋ž˜์Šค์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํ‘œ๊ธฐ์— ๋”ฐ๋ผ์„œ ^๋กœ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. o t ( ) โˆ’ j 1 y l g ( j ) ์ด ํ•จ์ˆ˜๊ฐ€ ์™œ ๋น„์šฉ ํ•จ์ˆ˜๋กœ ์ ํ•ฉํ•œ์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€ ์‹ค์ œ ๊ฐ’ ์›-ํ•ซ ๋ฒกํ„ฐ์—์„œ 1์„ ๊ฐ€์ง„ ์›์†Œ์˜ ์ธ๋ฑ์Šค๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, c 1 y ๊ฐ€๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์— ๋Œ€์ž…ํ•ด ๋ณด๋ฉด 1 o ( ) 0 ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฒฐ๊ณผ์ ์œผ๋กœ ^ y ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์˜ ๊ฐ’์€ 0์ด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, โˆ‘ = k j l g ( j ) ์ด ๊ฐ’์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ํ•™์Šตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ฅผ ๊ฐœ์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์ตœ์ข… ๋น„์šฉ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o t ( ) โˆ’ n i 1 โˆ‘ = k j ( ) l g ( j ( ) ) 2) ์ด์ง„ ๋ถ„๋ฅ˜์—์„œ์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ๋ฐฐ์šด ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์‹๊ณผ ๋‹ฌ๋ผ ๋ณด์ด์ง€๋งŒ, ๋ณธ์งˆ์ ์œผ๋กœ๋Š” ๋™์ผํ•œ ํ•จ์ˆ˜์‹์ž…๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์‹์œผ๋กœ๋ถ€ํ„ฐ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์‹์„ ๋„์ถœํ•ด ๋ด…์‹œ๋‹ค. o t ( ) โˆ’ ( l g ( ) ( โˆ’ ) l g ( โˆ’ ( ) ) ) ์œ„์˜ ์‹์€ ์•ž์„œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ๋ฐฐ์› ๋˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ์˜ ํ•จ์ˆ˜์‹์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์˜ ์‹์—์„œ ๋ฅผ 1 1 y y๋กœ ์น˜ํ™˜ํ•˜๊ณ  ( ) p, โˆ’ ( ) p๋กœ ์น˜ํ™˜ํ•ด ๋ด…์‹œ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์•„๋ž˜์˜ ์‹์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( 1 l g ( 1 ) y l g ( 2 ) ) ์ด ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( i 1 y l g p) ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ๋Š” k์˜ ๊ฐ’์ด ๊ณ ์ •๋œ ๊ฐ’์ด ์•„๋‹ˆ๋ฏ€๋กœ 2๋ฅผ k๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ( i 1 y l g p) ์œ„์˜ ์‹์€ ๊ฒฐ๊ณผ์ ์œผ๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ์‹๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์—ญ์œผ๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์—์„œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์‹์„ ์–ป๋Š” ๊ฒƒ์€ k๋ฅผ 2๋กœ ํ•˜๊ณ , 1 y๋ฅผ ๊ฐ๊ฐ ์™€ โˆ’๋กœ ์น˜ํ™˜ํ•˜๊ณ , 1 p๋ฅผ ๊ฐ๊ฐ ( ) 1 H ( ) ๋กœ ์น˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ตœ์ข… ๋น„์šฉ ํ•จ์ˆ˜์—์„œ ๊ฐ€ 2๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ๊ฒฐ๊ตญ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์˜ ๋น„์šฉ ํ•จ์ˆ˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. o t ( ) โˆ’ n i 1 โˆ‘ = k j ( ) l g ( j ( ) ) โˆ’ n i 1 [ ( ) o ( ( ) ) ( ์œ ํŠœ๋ธŒ ๊ฐ•์˜(ํ•œ๊ตญ์–ด ์ž๋ง‰ ํ‚ค๊ณ  ๋ณด์„ธ์š”) : https://www.youtube.com/watch? v=LLux1SW--oM https://blog-st.tistory.com/entry/Softmax-Classification-with-Pytorch? category=678300 05-03 ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ๋น„์šฉ ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์•ž์œผ๋กœ์˜ ๋ชจ๋“  ์‹ค์Šต์€ ์•„๋ž˜์˜ ์ฝ”๋“œ๊ฐ€ ์ด๋ฏธ ์ง„ํ–‰๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn.functional as F torch.manual_seed(1) 1. ํŒŒ์ด ํ† ์น˜๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค์˜ ๋น„์šฉ ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ (๋กœ์šฐ-๋ ˆ๋ฒจ) ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ•จ์— ์žˆ์–ด ์šฐ์„  ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๋กœ์šฐ-๋ ˆ๋ฒจ๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 3๊ฐœ์˜ ์›์†Œ๋ฅผ ๊ฐ€์ง„ ๋ฒกํ„ฐ ํ…์„œ๋ฅผ ์ •์˜ํ•˜๊ณ , ์ด ํ…์„œ๋ฅผ ํ†ตํ•ด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. z = torch.FloatTensor([1, 2, 3]) ์ด ํ…์„œ๋ฅผ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. hypothesis = F.softmax(z, dim=0) print(hypothesis) tensor([0.0900, 0.2447, 0.6652]) 3๊ฐœ์˜ ์›์†Œ์˜ ๊ฐ’์ด 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์›์†Œ๋“ค์˜ ๊ฐ’์˜ ํ•ฉ์ด 1์ธ์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. hypothesis.sum() tensor(1.) ์ด ์›์†Œ์˜ ๊ฐ’์˜ ํ•ฉ์€ 1์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ž„์˜์˜ 3 ร— 5 ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ ํ…์„œ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. z = torch.rand(3, 5, requires_grad=True) ์ด์ œ ์ด ํ…์„œ์— ๋Œ€ํ•ด์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ๊ฐ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ์•ผ ํ•˜๋ฏ€๋กœ ๋‘ ๋ฒˆ์งธ ์ฐจ์›์— ๋Œ€ํ•ด์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ์—์„œ dim=1์„ ์จ์ค๋‹ˆ๋‹ค. hypothesis = F.softmax(z, dim=1) print(hypothesis) tensor([[0.2645, 0.1639, 0.1855, 0.2585, 0.1277], [0.2430, 0.1624, 0.2322, 0.1930, 0.1694], [0.2226, 0.1986, 0.2326, 0.1594, 0.1868]], grad_fn=<SoftmaxBackward>) ์ด์ œ ๊ฐ ํ–‰์˜ ์›์†Œ๋“ค์˜ ํ•ฉ์€ 1์ด ๋˜๋Š” ํ…์„œ๋กœ ๋ณ€ํ™˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์€ ๊ฒฐ๊ตญ ์˜ˆ์ธก๊ฐ’์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์œ„ ํ…์„œ๋Š” 3๊ฐœ์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ 5๊ฐœ์˜ ํด๋ž˜์Šค ์ค‘ ์–ด๋–ค ํด๋ž˜์Šค๊ฐ€ ์ •๋‹ต์ธ์ง€๋ฅผ ์˜ˆ์ธกํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ž„์˜์˜ ๋ ˆ์ด๋ธ”์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. y = torch.randint(5, (3, )).long() print(y) tensor([0, 2, 1]) ์ด์ œ ๊ฐ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋“  ์›์†Œ๊ฐ€ 0์˜ ๊ฐ’์„ ๊ฐ€์ง„ 3 ร— 5 ํ…์„œ ์ƒ์„ฑ y_one_hot = torch.zeros_like(hypothesis) y_one_hot.scatter_(1, y.unsqueeze(1), 1) tensor([[1., 0., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 1., 0., 0., 0.]]) ์œ„์˜ ์—ฐ์‚ฐ์—์„œ ์–ด๋–ป๊ฒŒ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ์ˆ˜ํ–‰๋˜์—ˆ๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„ , torch.zeros_like(hypothesis)๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ์›์†Œ๊ฐ€ 0์˜ ๊ฐ’์„ ๊ฐ€์ง„ 3 ร— 5 ํ…์„œ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ…์„œ๋Š” y_one_hot์— ์ €์žฅ์ด ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„์„ ํ•ด์„ํ•ด ๋ด…์‹œ๋‹ค. y.unsqueeze(1)๋ฅผ ํ•˜๋ฉด (3, )์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์กŒ๋˜ y ํ…์„œ๋Š” (3 ร— 1) ํ…์„œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋‹ค์‹œ ๋งํ•ด์„œ y.unsqueeze(1)์˜ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. print(y.unsqueeze(1)) tensor([[0], [2], [1]]) ๊ทธ๋ฆฌ๊ณ  scatter์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ dim=1์— ๋Œ€ํ•ด์„œ ์ˆ˜ํ–‰ํ•˜๋ผ๊ณ  ์•Œ๋ ค์ฃผ๊ณ , ์„ธ ๋ฒˆ์งธ ์ธ์ž์— ์ˆซ์ž 1์„ ๋„ฃ์–ด์ฃผ๋ฏ€๋กœ์„œ ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ y_unsqeeze(1)์ด ์•Œ๋ ค์ฃผ๋Š” ์œ„์น˜์— ์ˆซ์ž 1์„ ๋„ฃ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ํ…์„œ ์กฐ์ž‘ํ•˜๊ธฐ 2์ฑ•ํ„ฐ์—์„œ ์—ฐ์‚ฐ ๋’ค์— _๋ฅผ ๋ถ™์ด๋ฉด In-place Operation (๋ฎ์–ด์“ฐ๊ธฐ ์—ฐ์‚ฐ) ์ž„์„ ๋ฐฐ์šด ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ์„œ y_one_hot์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” ๊ฒฐ๊ตญ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. print(y_one_hot) tensor([[1., 0., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 1., 0., 0., 0.]]) ์ด์ œ ๋น„์šฉ ํ•จ์ˆ˜ ์—ฐ์‚ฐ์„ ์œ„ํ•œ ์žฌ๋ฃŒ๋“ค์„ ์ „๋ถ€ ์†์งˆํ–ˆ์Šต๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ๋น„์šฉ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค. o t ( ) โˆ’ n i 1 โˆ‘ = k j ( ) l g ( j ( ) ) ๋งˆ์ด๋„ˆ์Šค ๋ถ€ํ˜ธ๋ฅผ ๋’ค๋กœ ๋นผ๋ฉด ๋‹ค์Œ ์‹๊ณผ๋„ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. o t ( ) 1 โˆ‘ = n j 1 y ( ) ร— ( l g ( j ( ) ) ) ์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. j 1 ์€ sum(dim=1)์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ณ , n i 1 ์€ mean()์œผ๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. cost = (y_one_hot * -torch.log(hypothesis)).sum(dim=1).mean() print(cost) tensor(1.4689, grad_fn=<MeanBackward1>) 2. ํŒŒ์ด ํ† ์น˜๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค์˜ ๋น„์šฉ ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ (ํ•˜์ด-๋ ˆ๋ฒจ) ์ด์ œ ์†Œํ”„ํŠธ๋งฅ์Šค์˜ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์ข€ ๋” ํ•˜์ด-๋ ˆ๋ฒจ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค. 1. F.softmax() + torch.log() = F.log_softmax() ์•ž์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ์— ๋กœ๊ทธ๋ฅผ ์”Œ์šธ ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์„ ๋กœ๊ทธ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. # Low level torch.log(F.softmax(z, dim=1)) tensor([[-1.3301, -1.8084, -1.6846, -1.3530, -2.0584], [-1.4147, -1.8174, -1.4602, -1.6450, -1.7758], [-1.5025, -1.6165, -1.4586, -1.8360, -1.6776]], grad_fn=<LogBackward>) ๊ทธ๋Ÿฐ๋ฐ ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ๋‘ ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ๊ฒฐํ•ฉํ•œ F.log_softmax()๋ผ๋Š” ๋„๊ตฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. # High level F.log_softmax(z, dim=1) tensor([[-1.3301, -1.8084, -1.6846, -1.3530, -2.0584], [-1.4147, -1.8174, -1.4602, -1.6450, -1.7758], [-1.5025, -1.6165, -1.4586, -1.8360, -1.6776]], grad_fn=<LogSoftmaxBackward>) ๋‘ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. F.log_softmax() + F.nll_loss() = F.cross_entropy() ์•ž์„œ ๋กœ์šฐ-๋ ˆ๋ฒจ๋กœ ๊ตฌํ˜„ํ•œ ๋น„์šฉ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค. # Low level # ์ฒซ ๋ฒˆ์งธ ์ˆ˜์‹ (y_one_hot * -torch.log(F.softmax(z, dim=1))).sum(dim=1).mean() tensor(1.4689, grad_fn=<MeanBackward1>) ๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ์ˆ˜์‹์—์„œ torch.log(F.softmax(z, dim=1))๋ฅผ ๋ฐฉ๊ธˆ ๋ฐฐ์šด F.log_softmax()๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๋‘ ๋ฒˆ์งธ ์ˆ˜์‹ (y_one_hot * - F.log_softmax(z, dim=1)).sum(dim=1).mean() tensor(1.4689, grad_fn=<MeanBackward0>) ์ด๋ฅผ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. F.nll_loss()๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋„ฃ์„ ํ•„์š” ์—†์ด ๋ฐ”๋กœ ์‹ค์ œ ๊ฐ’์„ ์ธ์ž๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. # High level # ์„ธ ๋ฒˆ์งธ ์ˆ˜์‹ F.nll_loss(F.log_softmax(z, dim=1), y) tensor(1.4689, grad_fn=<NllLossBackward>) ์—ฌ๊ธฐ์„œ nll ์ด๋ž€ Negative Log Likelihood์˜ ์•ฝ์ž์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ nll_loss๋Š” F.log_softmax()๋ฅผ ์ˆ˜ํ–‰ํ•œ ํ›„์— ๋‚จ์€ ์ˆ˜์‹๋“ค์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. F.cross_entropy()๋Š” F.log_softmax()์™€ F.nll_loss()๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. # ๋„ค ๋ฒˆ์งธ ์ˆ˜์‹ F.cross_entropy(z, y) tensor(1.4689, grad_fn=<NllLossBackward>) F.cross_entropy๋Š” ๋น„์šฉ ํ•จ์ˆ˜์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๊นŒ์ง€ ํฌํ•จํ•˜๊ณ  ์žˆ์Œ์„ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ์–ด์•ผ ๊ตฌํ˜„ ์‹œ ํ˜ผ๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 05-04 ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ๋กœ์šฐ-๋ ˆ๋ฒจ๊ณผ F.cross_entropy๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ๋ชจ๋“  ์‹ค์Šต์€ ์•„๋ž˜์˜ ๊ณผ์ •์ด ์ด๋ฏธ ์ง„ํ–‰๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim torch.manual_seed(1) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ”์„ ํ…์„œ๋กœ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. x_train = [[1, 2, 1, 1], [2, 1, 3, 2], [3, 1, 3, 4], [4, 1, 5, 5], [1, 7, 5, 5], [1, 2, 5, 6], [1, 6, 6, 6], [1, 7, 7, 7]] y_train = [2, 2, 2, 1, 1, 1, 0, 0] x_train = torch.FloatTensor(x_train) y_train = torch.LongTensor(y_train) x_train์˜ ๊ฐ ์ƒ˜ํ”Œ์€ 4๊ฐœ์˜ ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์ด 8๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. y_train์€ ๊ฐ ์ƒ˜ํ”Œ์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ”์ธ๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” 0, 1, 2์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์•„ ์ด 3๊ฐœ์˜ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 1. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ๊ตฌํ˜„ํ•˜๊ธฐ(๋กœ์šฐ-๋ ˆ๋ฒจ) ์ด์ œ x_train์˜ ํฌ๊ธฐ์™€ y_train์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print(x_train.shape) print(y_train.shape) torch.Size([8, 4]) torch.Size([8]) x_train์˜ ํฌ๊ธฐ๋Š” 8 ร— 4์ด๋ฉฐ, y_train์˜ ํฌ๊ธฐ๋Š” 8 ร— 1์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ตœ์ข… ์‚ฌ์šฉํ•  ๋ ˆ์ด๋ธ”์€ y_train์—์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•œ ๊ฒฐ๊ณผ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜๋Š” 3๊ฐœ์ด๋ฏ€๋กœ y_train์— ์›-ํ•ซ ์ธ์ฝ”๋”ฉํ•œ ๊ฒฐ๊ณผ๋Š” 8 ร— 3์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. y_one_hot = torch.zeros(8, 3) y_one_hot.scatter_(1, y_train.unsqueeze(1), 1) print(y_one_hot.shape) torch.Size([8, 3]) y_train์—์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•œ ๊ฒฐ๊ณผ์ธ y_one_hot์˜ ํฌ๊ธฐ๋Š” 8 ร— 3์ž…๋‹ˆ๋‹ค. ์ฆ‰, W ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” 4 ร— 3์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. W์™€ b๋ฅผ ์„ ์–ธํ•˜๊ณ , ์˜ตํ‹ฐ๋งˆ์ด์ €๋กœ๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต๋ฅ ์€ 0.1๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋ธ ์ดˆ๊ธฐํ™” W = torch.zeros((4, 3), requires_grad=True) b = torch.zeros((1, 3), requires_grad=True) # optimizer ์„ค์ • optimizer = optim.SGD([W, b], lr=0.1) F.softmax()์™€ torch.log()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์„ค๊ณผ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ , ์ด 1,000๋ฒˆ์˜ ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. nb_epochs = 1000 for epoch in range(nb_epochs + 1): # ๊ฐ€์„ค hypothesis = F.softmax(x_train.matmul(W) + b, dim=1) # ๋น„์šฉ ํ•จ์ˆ˜ cost = (y_one_hot * -torch.log(hypothesis)).sum(dim=1).mean() # cost๋กœ H(x) ๊ฐœ์„  optimizer.zero_grad() cost.backward() optimizer.step() # 100๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ if epoch % 100 == 0: print('Epoch {:4d}/{} Cost: {:.6f}'.format( epoch, nb_epochs, cost.item() )) 2. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ๊ตฌํ˜„ํ•˜๊ธฐ(ํ•˜์ด-๋ ˆ๋ฒจ) ์ด์ œ๋Š” F.cross_entropy()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ F.cross_entropy()๋Š” ๊ทธ ์ž์ฒด๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๊ฐ€์„ค์—์„œ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๋™์ผํ•œ x_train๊ณผ y_train์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋ธ ์ดˆ๊ธฐํ™” W = torch.zeros((4, 3), requires_grad=True) b = torch.zeros((1, 3), requires_grad=True) # optimizer ์„ค์ • optimizer = optim.SGD([W, b], lr=0.1) nb_epochs = 1000 for epoch in range(nb_epochs + 1): # Cost ๊ณ„์‚ฐ z = x_train.matmul(W) + b cost = F.cross_entropy(z, y_train) # cost๋กœ H(x) ๊ฐœ์„  optimizer.zero_grad() cost.backward() optimizer.step() # 100๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ if epoch % 100 == 0: print('Epoch {:4d}/{} Cost: {:.6f}'.format( epoch, nb_epochs, cost.item() )) 3. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ nn.Module๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด๋ฒˆ์—๋Š” nn.Module๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์„ ํ˜• ํšŒ๊ท€์—์„œ ๊ตฌํ˜„์— ์‚ฌ์šฉํ–ˆ๋˜ nn.Linear()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. output_dim์ด 1์ด์—ˆ๋˜ ์„ ํ˜• ํšŒ๊ท€ ๋•Œ์™€ ๋‹ฌ๋ฆฌ output_dim์€ ์ด์ œ ํด๋ž˜์Šค์˜ ๊ฐœ์ˆ˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋ธ์„ ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”. 4๊ฐœ์˜ ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  3๊ฐœ์˜ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฅ˜. input_dim=4, output_dim=3. model = nn.Linear(4, 3) ์•„๋ž˜์—์„œ F.cross_entropy()๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ๋”ฐ๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ๊ฐ€์„ค์— ์ •์˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. # optimizer ์„ค์ • optimizer = optim.SGD(model.parameters(), lr=0.1) nb_epochs = 1000 for epoch in range(nb_epochs + 1): # H(x) ๊ณ„์‚ฐ prediction = model(x_train) # cost ๊ณ„์‚ฐ cost = F.cross_entropy(prediction, y_train) # cost๋กœ H(x) ๊ฐœ์„  optimizer.zero_grad() cost.backward() optimizer.step() # 20๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ if epoch % 100 == 0: print('Epoch {:4d}/{} Cost: {:.6f}'.format( epoch, nb_epochs, cost.item() )) 4. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด์ œ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ nn.Module์„ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. class SoftmaxClassifierModel(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(4, 3) # Output์ด 3! def forward(self, x): return self.linear(x) model = SoftmaxClassifierModel() # optimizer ์„ค์ • optimizer = optim.SGD(model.parameters(), lr=0.1) nb_epochs = 1000 for epoch in range(nb_epochs + 1): # H(x) ๊ณ„์‚ฐ prediction = model(x_train) # cost ๊ณ„์‚ฐ cost = F.cross_entropy(prediction, y_train) # cost๋กœ H(x) ๊ฐœ์„  optimizer.zero_grad() cost.backward() optimizer.step() # 20๋ฒˆ๋งˆ๋‹ค ๋กœ๊ทธ ์ถœ๋ ฅ if epoch % 100 == 0: print('Epoch {:4d}/{} Cost: {:.6f}'.format( epoch, nb_epochs, cost.item() )) ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€์˜ ๋ ˆ์ด๋ธ”์€ ์™œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๊ฐ€? https://stackoverflow.com/questions/55549843/pytorch-doesnt-support-one-hot-vector 05-05 ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋กœ MNIST ๋ฐ์ดํ„ฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” MNIST ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜๊ณ , ํŒŒ์ด ํ† ์น˜(PyTorch)๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ MNIST ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. MNIST ๋ฐ์ดํ„ฐ๋Š” ์•„๋ž˜์˜ ๋งํฌ์— ๊ณต๊ฐœ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : http://yann.lecun.com/exdb/mnist 1. MNIST ๋ฐ์ดํ„ฐ ์ดํ•ดํ•˜๊ธฐ MNIST๋Š” ์ˆซ์ž 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ด๋ฏธ์ง€๋กœ ๊ตฌ์„ฑ๋œ ์†๊ธ€์”จ ๋ฐ์ดํ„ฐ ์…‹์ž…๋‹ˆ๋‹ค. ์ด ๋ฐ์ดํ„ฐ๋Š” ๊ณผ๊ฑฐ์— ์šฐ์ฒด๊ตญ์—์„œ ํŽธ์ง€์˜ ์šฐํŽธ ๋ฒˆํ˜ธ๋ฅผ ์ธ์‹ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋งŒ๋“ค์–ด์ง„ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์ด 60,000๊ฐœ์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ”, ์ด 10,000๊ฐœ์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ”๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์€ 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ด 10๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹์„ ์ฒ˜์Œ ๋ฐฐ์šธ ๋•Œ ์ ‘ํ•˜๊ฒŒ ๋˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์˜ˆ์ œ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. MNIST ๋ฌธ์ œ๋Š” ์†๊ธ€์”จ๋กœ ์ ํžŒ ์ˆซ์ž ์ด๋ฏธ์ง€๊ฐ€ ๋“ค์–ด์˜ค๋ฉด, ๊ทธ ์ด๋ฏธ์ง€๊ฐ€ ๋ฌด์Šจ ์ˆซ์ž์ธ์ง€ ๋งž์ถ”๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ˆซ์ž 5์˜ ์ด๋ฏธ์ง€๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋ฉด ์ด๊ฒŒ ์ˆซ์ž 5๋‹ค!๋ผ๋Š” ๊ฒƒ์„ ๋งž์ถฐ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋Š” ์‚ฌ๋žŒ์—๊ฒŒ๋Š” ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•˜์ง€๋งŒ ๊ธฐ๊ณ„์—๊ฒŒ๋Š” ๊ทธ๋ ‡์ง€๊ฐ€ ์•Š์Šต๋‹ˆ๋‹ค. ์šฐ์„  MNIST ๋ฌธ์ œ๋ฅผ ๋” ์ž์„ธํžˆ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ด๋ฏธ์ง€๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด 28 ํ”ฝ์…€ ร— 28 ํ”ฝ์…€์˜ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ์—ฌ๊ธฐ์„œ๋Š” 28 ํ”ฝ์…€ ร— 28 ํ”ฝ์…€ = 784 ํ”ฝ์…€์ด๋ฏ€๋กœ, ๊ฐ ์ด๋ฏธ์ง€๋ฅผ ์ด 784์˜ ์›์†Œ๋ฅผ ๊ฐ€์ง„ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ค์–ด์ค„ ๊ฒ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ด 784๊ฐœ์˜ ํŠน์„ฑ์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ๋˜๋Š”๋ฐ, ์ด๋Š” ์•ž์„œ ์šฐ๋ฆฌ๊ฐ€ ํ’€์—ˆ๋˜ ๊ทธ ์–ด๋–ค ๋ฌธ์ œ๋“ค๋ณด๋‹ค ํŠน์„ฑ์ด ๊ต‰์žฅํžˆ ๋งŽ์€ ์ƒ˜ํ”Œ์ž…๋‹ˆ๋‹ค. 784์ฐจ์›์˜ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ๋ฅผ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ๋กœ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. for X, Y in data_loader: # ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ [batch_size ร— 784]์˜ ํฌ๊ธฐ๋กœ reshape # ๋ ˆ์ด๋ธ”์€ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ X = X.view(-1, 28*28) ์œ„์˜ ์ฝ”๋“œ์—์„œ X๋Š” for ๋ฌธ์—์„œ ํ˜ธ์ถœ๋  ๋•Œ๋Š” (๋ฐฐ์น˜ ํฌ๊ธฐ ร— 1 ร— 28 ร— 28)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€์ง€๋งŒ, view๋ฅผ ํ†ตํ•ด์„œ (๋ฐฐ์น˜ ํฌ๊ธฐ ร— 784)์˜ ํฌ๊ธฐ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. 2. ํ† ์น˜ ๋น„์ „(torchvision) ์†Œ๊ฐœํ•˜๊ธฐ ๋ณธ๊ฒฉ์ ์ธ ์‹ค์Šต์— ๋“ค์–ด๊ฐ€๊ธฐ์— ์•ž์„œ ํ† ์น˜ ๋น„์ „(torchvision)์ด๋ผ๋Š” ๋„๊ตฌ๋ฅผ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. torchvision์€ ์œ ๋ช…ํ•œ ๋ฐ์ดํ„ฐ ์…‹๋“ค, ์ด๋ฏธ ๊ตฌํ˜„๋ผ ์žˆ๋Š” ์œ ๋ช…ํ•œ ๋ชจ๋ธ๋“ค, ์ผ๋ฐ˜์ ์ธ ์ด๋ฏธ์ง€ ์ „์ฒ˜๋ฆฌ ๋„๊ตฌ๋“ค์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋งํฌ๋Š” torchvision์— ์–ด๋–ค ๋ฐ์ดํ„ฐ ์…‹๋“ค(datasets)๊ณผ ๋ชจ๋ธ๋“ค(models) ๊ทธ๋ฆฌ๊ณ  ์–ด๋–ค ์ „์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•๋“ค(transforms)์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š”์ง€ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋งํฌ : https://pytorch.org/docs/stable/torchvision/index.html ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ๋Š” ํ† ์น˜ ํ…์ŠคํŠธ(torchtext)๋ผ๋Š” ํŒจํ‚ค์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ๋ถ„๋ฅ˜๊ธฐ ๊ตฌํ˜„์„ ์œ„ํ•œ ์‚ฌ์ „ ์„ค์ • ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torchvision.datasets as dsets import torchvision.transforms as transforms from torch.utils.data import DataLoader import torch.nn as nn import matplotlib.pyplot as plt import random ํ˜„์žฌ ํ™˜๊ฒฝ์—์„œ GPU ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด GPU ์—ฐ์‚ฐ์„ ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด CPU ์—ฐ์‚ฐ์„ ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. USE_CUDA = torch.cuda.is_available() # GPU๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉด True, ์•„๋‹ˆ๋ผ๋ฉด False๋ฅผ ๋ฆฌํ„ด device = torch.device("cuda" if USE_CUDA else "cpu") # GPU ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉด ์‚ฌ์šฉํ•˜๊ณ  ์•„๋‹ˆ๋ฉด CPU ์‚ฌ์šฉ print("๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค:", device) ๊ตฌ๊ธ€์˜ Colab์—์„œ '๋Ÿฐํƒ€์ž„ > ๋Ÿฐํƒ€์ž„ ์œ ํ˜• ๋ณ€๊ฒฝ > ํ•˜๋“œ์›จ์–ด ๊ฐ€์†๊ธฐ > GPU'๋ฅผ ์„ ํƒํ•˜๋ฉด USE_CUDA์˜ ๊ฐ’์ด True๊ฐ€ ๋˜๋ฉด์„œ '๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค: cuda'๋ผ๋Š” ์ถœ๋ ฅ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ฆ‰, GPU๋กœ ์—ฐ์‚ฐํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด์— 'ํ•˜๋“œ์›จ์–ด ๊ฐ€์†๊ธฐ > None'์„ ์„ ํƒํ•˜๋ฉด USE_CUDA์˜ ๊ฐ’์ด False๊ฐ€ ๋˜๋ฉด์„œ '๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค: cpu'๋ผ๋Š” ์ถœ๋ ฅ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ฆ‰, CPU๋กœ ์—ฐ์‚ฐํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๋ฐฉ๋ฒ•์€ ์•ž์œผ๋กœ ์ž์ฃผ ์“ฐ์ด๊ฒŒ ๋˜๋ฏ€๋กœ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ๋žœ๋ค ์‹œ๋“œ๋ฅผ ๊ณ ์ •ํ•ฉ๋‹ˆ๋‹ค. # for reproducibility random.seed(777) torch.manual_seed(777) if device == 'cuda': torch.cuda.manual_seed_all(777) ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ณ€์ˆ˜๋กœ ๋‘ก๋‹ˆ๋‹ค. # hyperparameters training_epochs = 15 batch_size = 100 4. MNIST ๋ถ„๋ฅ˜๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ torchvision.datasets.dsets.MNIST๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ MNIST ๋ฐ์ดํ„ฐ ์…‹์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # MNIST dataset mnist_train = dsets.MNIST(root='MNIST_data/', train=True, transform=transforms.ToTensor(), download=True) mnist_test = dsets.MNIST(root='MNIST_data/', train=False, transform=transforms.ToTensor(), download=True) ์ฒซ ๋ฒˆ์งธ ์ธ์ž root๋Š” MNIST ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•  ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธ์ž train์€ ์ธ์ž๋กœ True๋ฅผ ์ฃผ๋ฉด, MNIST์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌํ„ด ๋ฐ›์œผ๋ฉฐ False๋ฅผ ์ฃผ๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌํ„ด ๋ฐ›์Šต๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ์ธ์ž transform์€ ํ˜„์žฌ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด ํ† ์น˜ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•ด ์ค๋‹ˆ๋‹ค. ๋„ค ๋ฒˆ์งธ ์ธ์ž download๋Š” ํ•ด๋‹น ๊ฒฝ๋กœ์— MNIST ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋‹ค๋ฉด ๋‹ค์šด๋กœ๋“œํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ–ˆ๋‹ค๋ฉด ์•ž์„œ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์™€ ๋ฐ์ดํ„ฐ ๋กœ๋“œ ์ฑ•ํ„ฐ์—์„œ ํ•™์Šตํ–ˆ๋˜ ๋ฐ์ดํ„ฐ ๋กœ๋”(DataLoader)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. # dataset loader data_loader = DataLoader(dataset=mnist_train, batch_size=batch_size, # ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 100 shuffle=True, drop_last=True) ์ด๋•Œ DataLoader์—๋Š” 4๊ฐœ์˜ ์ธ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž์ธ dataset์€ ๋กœ๋“œํ•  ๋Œ€์ƒ์„ ์˜๋ฏธํ•˜๋ฉฐ, ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ batch_size๋Š” ๋ฐฐ์น˜ ํฌ๊ธฐ, shuffle์€ ๋งค ์—ํฌํฌ๋งˆ๋‹ค ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ฅผ ์…”ํ”Œ ํ•  ๊ฒƒ์ธ์ง€์˜ ์—ฌ๋ถ€, drop_last๋Š” ๋งˆ์ง€๋ง‰ ๋ฐฐ์น˜๋ฅผ ๋ฒ„๋ฆด ๊ฒƒ์ธ์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. drop_last๋ฅผ ํ•˜๋Š” ์ด์œ ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ 1,000๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 128์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. 1,000์„ 128๋กœ ๋‚˜๋ˆ„๋ฉด ์ด 7๊ฐœ๊ฐ€ ๋‚˜์˜ค๊ณ  ๋‚˜๋จธ์ง€๋กœ 104๊ฐœ๊ฐ€ ๋‚จ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ 104๊ฐœ๋ฅผ ๋งˆ์ง€๋ง‰ ๋ฐฐ์น˜๋กœ ํ•œ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ 128๊ฐœ๋ฅผ ์ถฉ์กฑํ•˜์ง€ ๋ชปํ•˜์˜€์œผ๋ฏ€๋กœ 104๊ฐœ๋ฅผ ๊ทธ๋ƒฅ ๋ฒ„๋ฆด ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋งˆ์ง€๋ง‰ ๋ฐฐ์น˜๋ฅผ ๋ฒ„๋ฆฌ๋ ค๋ฉด drop_last=True๋ฅผ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค๋ฅธ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ณด๋‹ค ๊ฐœ์ˆ˜๊ฐ€ ์ ์€ ๋งˆ์ง€๋ง‰ ๋ฐฐ์น˜๋ฅผ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์— ์‚ฌ์šฉํ•˜์—ฌ ๋งˆ์ง€๋ง‰ ๋ฐฐ์น˜๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ๊ณผ๋Œ€ํ‰๊ฐ€๋˜๋Š” ํ˜„์ƒ์„ ๋ง‰์•„์ค๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. input_dim์€ 784์ด๊ณ , output_dim์€ 10์ž…๋‹ˆ๋‹ค. # MNIST data image of shape 28 * 28 = 784 linear = nn.Linear(784, 10, bias=True).to(device) to() ํ•จ์ˆ˜๋Š” ์—ฐ์‚ฐ์„ ์–ด๋””์„œ ์ˆ˜ํ–‰ํ• ์ง€๋ฅผ ์ •ํ•ฉ๋‹ˆ๋‹ค. to() ํ•จ์ˆ˜๋Š” ๋ชจ๋ธ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•œ ์žฅ์น˜์˜ ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. CPU๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ํ•„์š”๊ฐ€ ์—†์ง€๋งŒ, GPU๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด to('cuda')๋ฅผ ํ•ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๊ฒƒ๋„ ์ง€์ •ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” CPU ์—ฐ์‚ฐ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. bias๋Š” ํŽธํ–ฅ b๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ True์ด๋ฏ€๋กœ ๊ตณ์ด ํ•  ํ•„์š”๋Š” ์—†์ง€๋งŒ ๋ช…์‹œ์ ์œผ๋กœ True๋ฅผ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋น„์šฉ ํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. # ๋น„์šฉ ํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ •์˜ criterion = nn.CrossEntropyLoss().to(device) # ๋‚ด๋ถ€์ ์œผ๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Œ. optimizer = torch.optim.SGD(linear.parameters(), lr=0.1) ์•ž์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ๋ฐฐ์šธ ๋•Œ๋Š” torch.nn.functional.cross_entropy()๋ฅผ ์‚ฌ์šฉํ•˜์˜€์œผ๋‚˜ ์—ฌ๊ธฐ์„œ๋Š” torch.nn.CrossEntropyLoss()์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค ํŒŒ์ด ํ† ์น˜์—์„œ ์ œ๊ณตํ•˜๋Š” ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋กœ ๋‘˜ ๋‹ค ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. for epoch in range(training_epochs): # ์•ž์„œ training_epochs์˜ ๊ฐ’์€ 15๋กœ ์ง€์ •ํ•จ. avg_cost = 0 total_batch = len(data_loader) for X, Y in data_loader: # ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 100์ด๋ฏ€๋กœ ์•„๋ž˜์˜ ์—ฐ์‚ฐ์—์„œ X๋Š” (100, 784)์˜ ํ…์„œ๊ฐ€ ๋œ๋‹ค. X = X.view(-1, 28 * 28).to(device) # ๋ ˆ์ด๋ธ”์€ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์ด ๋œ ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ 0 ~ 9์˜ ์ •์ˆ˜. Y = Y.to(device) optimizer.zero_grad() hypothesis = linear(X) cost = criterion(hypothesis, Y) cost.backward() optimizer.step() avg_cost += cost / total_batch print('Epoch:', '% 04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost)) print('Learning finished') Epoch: 0001 cost = 0.535468459 Epoch: 0002 cost = 0.359274209 Epoch: 0003 cost = 0.331187516 Epoch: 0004 cost = 0.316578060 Epoch: 0005 cost = 0.307158142 Epoch: 0006 cost = 0.300180763 Epoch: 0007 cost = 0.295130193 Epoch: 0008 cost = 0.290851474 Epoch: 0009 cost = 0.287417054 Epoch: 0010 cost = 0.284379572 Epoch: 0011 cost = 0.281825274 Epoch: 0012 cost = 0.279800713 Epoch: 0013 cost = 0.277808994 Epoch: 0014 cost = 0.276154339 Epoch: 0015 cost = 0.274440885 Learning finished # ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•œ๋‹ค. with torch.no_grad(): # torch.no_grad()๋ฅผ ํ•˜๋ฉด gradient ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. X_test = mnist_test.test_data.view(-1, 28 * 28).float().to(device) Y_test = mnist_test.test_labels.to(device) prediction = linear(X_test) correct_prediction = torch.argmax(prediction, 1) == Y_test accuracy = correct_prediction.float().mean() print('Accuracy:', accuracy.item()) # MNIST ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ๋ฌด์ž‘์œ„๋กœ ํ•˜๋‚˜๋ฅผ ๋ฝ‘์•„์„œ ์˜ˆ์ธก์„ ํ•ด๋ณธ๋‹ค r = random.randint(0, len(mnist_test) - 1) X_single_data = mnist_test.test_data[r:r + 1].view(-1, 28 * 28).float().to(device) Y_single_data = mnist_test.test_labels[r:r + 1].to(device) print('Label: ', Y_single_data.item()) single_prediction = linear(X_single_data) print('Prediction: ', torch.argmax(single_prediction, 1).item()) plt.imshow(mnist_test.test_data[r:r + 1].view(28, 28), cmap='Greys', interpolation='nearest') plt.show() ์ฐธ๊ณ  ์ž๋ฃŒ : https://excelsior-cjh.tistory.com/180 torch.nn.functional๊ณผ torch.nn์˜ ์ฐจ์ด : https://discuss.pytorch.org/t/what-is-the-difference-between-torch-nn-and-torch-nn-functional/33597 06. [DL ์ž…๋ฌธ ] - ์ธ๊ณต ์‹ ๊ฒฝ๋ง(Aritificial Neural Network) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์— ๋Œ€ํ•œ ์ „๋ฐ˜์ ์ธ ๊ฐœ๋…์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 06-01 ๋จธ์‹  ๋Ÿฌ๋‹ ์šฉ์–ด ์ดํ•ดํ•˜๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹์˜ ํŠน์ง•๋“ค์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. ๋”ฅ ๋Ÿฌ๋‹ ๋˜ํ•œ ๋จธ์‹  ๋Ÿฌ๋‹์— ์†ํ•˜๋ฏ€๋กœ, ์•„๋ž˜์˜ ๋จธ์‹  ๋Ÿฌ๋‹์˜ ํŠน์ง•๋“ค์€ ๋ชจ๋‘ ๋”ฅ ๋Ÿฌ๋‹์˜ ํŠน์ง•์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 1. ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ํ‰๊ฐ€ ์‹ค์ œ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ์šฉ, ๊ฒ€์ฆ์šฉ, ํ…Œ์ŠคํŠธ์šฉ ์ด๋ ‡๊ฒŒ ์„ธ ๊ฐ€์ง€๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ด ์ฑ…์˜ ๋ชฉ์ ์€ ๊ฐœ๋… ํ•™์Šต์ด๋ฏ€๋กœ ์ผ๋ถ€ ์‹ค์Šต์—์„œ๋Š” ๋ณ„๋„๋กœ ์„ธ ๊ฐ€์ง€๋กœ ๋ถ„๋ฆฌํ•˜์ง€ ์•Š๊ณ  ํ›ˆ๋ จ์šฉ, ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ๋งŒ ๋ถ„๋ฆฌํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ›ˆ๋ จ์šฉ, ํ…Œ์ŠคํŠธ์šฉ ๋‘ ๊ฐ€์ง€๋กœ๋งŒ ๋‚˜๋ˆ ์„œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ํ•œ ๋ฒˆ๋งŒ ํ…Œ์ŠคํŠธํ•˜๋ฉด ๋” ํŽธํ•  ํ…๋ฐ ๊ตณ์ด ์™œ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด ๋†“๋Š” ๊ฒƒ์ผ๊นŒ์š”? ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๊ฐ€ ์•„๋‹ˆ๋ผ, ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์กฐ์ •ํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„์ž…๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํžˆ๋Š” ๊ณผ์ ํ•ฉ์ด ๋˜๊ณ  ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๊ฑฐ๋‚˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์กฐ์ •์„ ์œ„ํ•œ ์šฉ๋„์ž…๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ(์ดˆ ๋งค๊ฐœ๋ณ€์ˆ˜)๋ž€ ๊ฐ’์— ๋”ฐ๋ผ์„œ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ๊ณผ ๊ฐ™์€ ํ•™์Šต์„ ํ†ตํ•ด ๋ฐ”๋€Œ์–ด๊ฐ€๋Š” ๋ณ€์ˆ˜๋ฅผ ์ด ์ฑ…์—์„œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐ’ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋ณดํ†ต ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์„ ํ˜• ํšŒ๊ท€ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์—์„œ ํ•™์Šต๋ฅ (learning rate)์ด ์ด์— ํ•ด๋‹น๋˜๋ฉฐ ๋”ฅ ๋Ÿฌ๋‹์—์„œ๋Š” ์€๋‹‰์ธต์˜ ์ˆ˜, ๋‰ด๋Ÿฐ์˜ ์ˆ˜, ๋“œ๋กญ์•„์›ƒ ๋น„์œจ ๋“ฑ์ด ์ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒฐ์ •ํ•ด ์ฃผ๋Š” ๊ฐ’์ด ์•„๋‹ˆ๋ผ ๋ชจ๋ธ์ด ํ•™์Šตํ•˜๋Š” ๊ณผ์ •์—์„œ ์–ป์–ด์ง€๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์‚ฌ๋žŒ์ด ์ •ํ•˜๋Š” ๋ณ€์ˆ˜์ธ ๋ฐ˜๋ฉด, ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ์„ ํ†ตํ•ด์„œ ๋ฐ”๊พธ๋Š” ๋ณ€์ˆ˜๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด ์ฑ…์—์„œ๋Š” ์ด์™€ ๊ฐ™์€ ๊ธฐ์ค€์œผ๋กœ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ์šฉ ๋ฐ์ดํ„ฐ๋กœ ํ›ˆ๋ จ์„ ๋ชจ๋‘ ์‹œํ‚จ ๋ชจ๋ธ์€ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •ํ™•๋„๋ฅผ ๊ฒ€์ฆํ•˜๋ฉฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํŠœ๋‹(tuning) ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ๋ชจ๋ธ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋กœ ์ •ํ™•๋„๊ฐ€ ๊ฒ€์ฆ๋˜๋Š” ๊ณผ์ •์—์„œ ์ ์ฐจ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ์— ์ ์  ๋งž์ถ”์–ด์ ธ ๊ฐ€๊ธฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ํŠœ๋‹์ด ๋๋‚ฌ๋‹ค๋ฉด, ์ด์ œ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์€ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ์ผ์ • ๋ถ€๋ถ„ ์ตœ์ ํ™”๊ฐ€ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์— ๋Œ€ํ•œ ํ‰๊ฐ€๋Š” ๋ชจ๋ธ์ด ์•„์ง๊นŒ์ง€ ๋ณด์ง€ ๋ชปํ•œ ๋ฐ์ดํ„ฐ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ์ด ๋๋‚ฌ๋‹ค๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ชจ๋ธ์˜ ์ง„์งœ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋น„์œ ํ•˜์ž๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ๋ฌธ์ œ์ง€, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ์˜๊ณ ์‚ฌ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” ์‹ค๋ ฅ์„ ์ตœ์ข…์ ์œผ๋กœ ํ‰๊ฐ€ํ•˜๋Š” ์ˆ˜๋Šฅ ์‹œํ—˜์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆŒ ๋งŒํผ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถฉ๋ถ„ํ•˜์ง€ ์•Š๋‹ค๋ฉด k-ํด๋“œ ๊ต์ฐจ ๊ฒ€์ฆ์ด๋ผ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 2. ๋ถ„๋ฅ˜(Classification)์™€ ํšŒ๊ท€(Regression) ์ „๋ถ€๋ผ๊ณ ๋Š” ํ•  ์ˆ˜ ์—†์ง€๋งŒ, ๋จธ์‹  ๋Ÿฌ๋‹์˜ ๋งŽ์€ ๋ฌธ์ œ๋Š” ๋ถ„๋ฅ˜ ๋˜๋Š” ํšŒ๊ท€ ๋ฌธ์ œ์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์•ž์„œ ๋จธ์‹  ๋Ÿฌ๋‹ ๊ธฐ๋ฒ• ์ค‘ ์„ ํ˜• ํšŒ๊ท€(Lineare Regression)๊ณผ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€(Logistic Rgression)๋ฅผ ๋‹ค๋ฃจ๋Š”๋ฐ ์„ ํ˜• ํšŒ๊ท€๋ฅผ ํ†ตํ•ด ํšŒ๊ท€ ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜๊ณ , ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ํ†ตํ•ด (์ด๋ฆ„์€ ํšŒ๊ท€์ด์ง€๋งŒ) ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜๋Š” ๋˜ํ•œ ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification)๊ณผ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-Class Classification)๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์—„๋ฐ€ํžˆ๋Š” ๋‹ค์ค‘ ๋ ˆ์ด๋ธ” ๋ถ„๋ฅ˜(Multi-lable Classification)๋ผ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ, ์ด ์ฑ…์—์„œ๋Š” ์ด์ง„ ๋ถ„๋ฅ˜์™€ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋งŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 1) ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ(Binary Classification) ์ด์ง„ ๋ถ„๋ฅ˜๋Š” ์ฃผ์–ด์ง„ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ๋‘˜ ์ค‘ ํ•˜๋‚˜์˜ ๋‹ต์„ ์ •ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์‹œํ—˜ ์„ฑ์ ์— ๋Œ€ํ•ด์„œ ํ•ฉ๊ฒฉ, ๋ถˆํ•ฉ๊ฒฉ์ธ์ง€ ํŒ๋‹จํ•˜๊ณ  ๋ฉ”์ผ๋กœ๋ถ€ํ„ฐ ์ •์ƒ ๋ฉ”์ผ, ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋ฌธ์ œ ๋“ฑ์ด ์ด์— ์†ํ•ฉ๋‹ˆ๋‹ค. 2) ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-class Classification) ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜๋Š” ์ฃผ์–ด์ง„ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์„ธ ๊ฐœ ์ด์ƒ์˜ ์ •ํ•ด์ง„ ์„ ํƒ์ง€ ์ค‘์—์„œ ๋‹ต์„ ์ •ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์„œ์  ์•„๋ฅด๋ฐ”์ดํŠธ๋ฅผ ํ•˜๋Š”๋ฐ ๊ณผํ•™, ์˜์–ด, IT, ํ•™์Šต์ง€, ๋งŒํ™”๋ผ๋Š” ๋ ˆ์ด๋ธ”์ด ๊ฐ๊ฐ ๋ถ™์—ฌ์ ธ ์žˆ๋Š” 5๊ฐœ์˜ ์ฑ…์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ƒˆ ์ฑ…์ด ์ž…๊ณ ๋˜๋ฉด, ์ด ์ฑ…์€ ๋‹ค์„ฏ ๊ฐœ์˜ ์ฑ…์žฅ ์ค‘์—์„œ ๋ถ„์•ผ์— ๋งž๋Š” ์ ์ ˆํ•œ ์ฑ…์žฅ์— ์ฑ…์„ ๋„ฃ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ์˜ ๋‹ค์„ฏ ๊ฐœ์˜ ์„ ํƒ์ง€๋ฅผ ์ฃผ๋กœ ์นดํ…Œ๊ณ ๋ฆฌ ๋˜๋Š” ๋ฒ”์ฃผ ๋˜๋Š” ํด๋ž˜์Šค๋ผ๊ณ  ํ•˜๋ฉฐ, ์ฃผ์–ด์ง„ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ •ํ•ด์ง„ ํด๋ž˜์Šค ์ค‘ ํ•˜๋‚˜๋กœ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์„ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3) ํšŒ๊ท€ ๋ฌธ์ œ(Regression) ํšŒ๊ท€ ๋ฌธ์ œ๋Š” ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ฒ˜๋Ÿผ 0 ๋˜๋Š” 1์ด๋‚˜ ๊ณผํ•™ ์ฑ…์žฅ, IT ์ฑ…์žฅ ๋“ฑ๊ณผ ๊ฐ™์ด ๋ถ„๋ฆฌ๋œ(๋น„์—ฐ์†์ ์ธ) ๋‹ต์ด ๊ฒฐ๊ณผ๊ฐ€ ์•„๋‹ˆ๋ผ ์—ฐ์†๋œ ๊ฐ’์„ ๊ฒฐ๊ณผ๋กœ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‹œํ—˜ ์„ฑ์ ์„ ์˜ˆ์ธกํ•˜๋Š”๋ฐ 5์‹œ๊ฐ„ ๊ณต๋ถ€ํ•˜์˜€์„ ๋•Œ 80์ , 5์‹œ๊ฐ„ 1๋ถ„ ๊ณต๋ถ€ํ•˜์˜€์„ ๋•Œ๋Š” 80.5์ , 7์‹œ๊ฐ„ ๊ณต๋ถ€ํ•˜์˜€์„ ๋•Œ๋Š” 90์  ๋“ฑ์ด ๋‚˜์˜ค๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋„ ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•œ ์ฃผ๊ฐ€ ์˜ˆ์ธก, ์ƒ์‚ฐ๋Ÿ‰ ์˜ˆ์ธก,<NAME> ์˜ˆ์ธก ๋“ฑ์ด ์ด์— ์†ํ•ฉ๋‹ˆ๋‹ค. 3. ์ง€๋„ ํ•™์Šต(Supervised Learning)๊ณผ ๋น„์ง€๋„ ํ•™์Šต(Unsupervised Learning) ๋จธ์‹  ๋Ÿฌ๋‹์€ ํฌ๊ฒŒ ์ง€๋„ ํ•™์Šต, ๋น„์ง€๋„ ํ•™์Šต, ๊ฐ•ํ™” ํ•™์Šต์œผ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ•ํ™” ํ•™์Šต์€ ์ด ์ฑ…์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฏ€๋กœ ์„ค๋ช…ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ์ฑ…์€ ์ฃผ๋กœ ์ง€๋„ ํ•™์Šต์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 1) ์ง€๋„ ํ•™์Šต ์ง€๋„ ํ•™์Šต์ด๋ž€ ๋ ˆ์ด๋ธ”(Label)์ด๋ผ๋Š” ์ •๋‹ต๊ณผ ํ•จ๊ป˜ ํ•™์Šตํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์•ž์„œ 2์ฑ•ํ„ฐ์˜ ๋ฐ์ดํ„ฐ์˜ ๋ถ„๋ฆฌ ์ฑ•ํ„ฐ์—์„œ ์ƒ์„ธํžˆ ์„ค๋ช…ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์ด๋ผ๋Š” ๋ง ์™ธ์—๋„, ์‹ค์ œ ๊ฐ’ ๋“ฑ์œผ๋กœ ๋ถ€๋ฅด๊ธฐ๋„ ํ•˜๋Š”๋ฐ ์ด ์ฑ…์—์„œ๋Š” ์ด ์šฉ์–ด๋“ค์„ ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๊ธฐ๊ณ„๋Š” ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์ฐจ์ด์ธ ์˜ค์ฐจ๋ฅผ ์ค„์ด๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•™์Šต์„ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์˜ˆ์ธก๊ฐ’์€ ^ ๊ณผ๊ฐ™์ด ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 2) ๋น„์ง€๋„ ํ•™์Šต ๋น„์ง€๋„ ํ•™์Šต์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชฉ์  ๋ฐ์ดํ„ฐ(๋˜๋Š” ๋ ˆ์ด๋ธ”)์ด ์—†๋Š” ํ•™์Šต ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ๊ตฐ์ง‘(clustering)์ด๋‚˜ ์ฐจ์› ์ถ•์†Œ์™€ ๊ฐ™์€ ํ•™์Šต ๋ฐฉ๋ฒ•๋“ค์„ ๋น„์ง€๋„ ํ•™์Šต์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3) ๊ฐ•ํ™” ํ•™์Šต ๊ฐ•ํ™” ํ•™์Šต์€ ์ด ์ฑ…์—์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š๋Š” ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ๊ฐ•ํ™” ํ•™์Šต์€ ์–ด๋–ค ํ™˜๊ฒฝ ๋‚ด์—์„œ ์ •์˜๋œ ์—์ด์ „ํŠธ๊ฐ€ ํ˜„์žฌ์˜ ์ƒํƒœ๋ฅผ ์ธ์‹ํ•˜์—ฌ, ์„ ํƒ ๊ฐ€๋Šฅํ•œ ํ–‰๋™๋“ค ์ค‘ ๋ณด์ƒ์„ ์ตœ๋Œ€ํ™”ํ•˜๋Š” ํ–‰๋™ ํ˜น์€ ํ–‰๋™ ์ˆœ์„œ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. (์ถœ์ฒ˜ : ์œ„ํ‚ค ๋ฐฑ๊ณผ) 4. ์ƒ˜ํ”Œ(Sample)๊ณผ ํŠน์„ฑ(Feature) ๋งŽ์€ ๋จธ์‹  ๋Ÿฌ๋‹ ๋ฌธ์ œ๊ฐ€ 1๊ฐœ ์ด์ƒ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์ข…์† ๋ณ€์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋งŽ์€ ๋จธ์‹  ๋Ÿฌ๋‹ ๋ชจ๋ธ๋“ค, ํŠนํžˆ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์€ ๋…๋ฆฝ ๋ณ€์ˆ˜, ์ข…์† ๋ณ€์ˆ˜, ๊ฐ€์ค‘์น˜, ํŽธํ–ฅ ๋“ฑ์„ ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์—ฐ์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์•ž์œผ๋กœ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งŽ์ด ๋ณด๊ฒŒ ๋  ๊ฒ๋‹ˆ๋‹ค. ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ํ–‰๋ ฌ์„ X๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ๋…๋ฆฝ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ n ๊ฐœ๊ณ  ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ m์ธ ํ–‰๋ ฌ X๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ๋Š” ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ, ํ•˜๋‚˜์˜ ํ–‰์„ ์ƒ˜ํ”Œ(Sample)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. (๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ๋Š” ๋ ˆ์ฝ”๋“œ๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๋‹จ์œ„์ž…๋‹ˆ๋‹ค.) ์ข…์† ๋ณ€์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ๊ฐ์˜ ๋…๋ฆฝ ๋ณ€์ˆ˜๋ฅผ ํŠน์„ฑ(Feature)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 5. ํ˜ผ๋™ ํ–‰๋ ฌ(Confusion Matrix) ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ๋Š” ๋งž์ถ˜ ๋ฌธ์ œ ์ˆ˜๋ฅผ ์ „์ฒด ๋ฌธ์ œ ์ˆ˜๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ ์ •ํ™•๋„(Accuracy)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ •ํ™•๋„๋Š” ๋งž์ถ˜ ๊ฒฐ๊ณผ์™€ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์„ธ๋ถ€์ ์ธ ๋‚ด์šฉ์„ ์•Œ๋ ค์ฃผ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ˜ผ๋™ ํ–‰๋ ฌ(Confusion Matrix)์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์–‘์„ฑ(Positive)๊ณผ ์Œ์„ฑ(Negative)์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ ํ˜ผ๋™ ํ–‰๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ ์—ด์€ ์˜ˆ์ธก๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๊ฐ ํ–‰์€ ์‹ค์ œ ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. - ์ฐธ ๊ฑฐ์ง“ ์ฐธ TP FN ๊ฑฐ์ง“ FP TN ์ด๋ฅผ ๊ฐ๊ฐ TP(True Positive), TN(True Negative), FP(False Postivie), FN(False Negative)๋ผ๊ณ  ํ•˜๋Š”๋ฐ True๋Š” ์ •๋‹ต์„ ๋งžํžŒ ๊ฒฝ์šฐ๊ณ  False๋Š” ์ •๋‹ต์„ ๋งžํžˆ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Positive์™€ Negative๋Š” ๊ฐ๊ฐ ์ œ์‹œํ–ˆ๋˜ ์ •๋‹ต์ž…๋‹ˆ๋‹ค. ์ฆ‰, TP๋Š” ์–‘์„ฑ(Postive)์ด๋ผ๊ณ  ๋Œ€๋‹ตํ•˜์˜€๊ณ  ์‹ค์ œ๋กœ ์–‘์„ฑ์ด๋ผ์„œ ์ •๋‹ต์„ ๋งžํžŒ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. TN์€ ์Œ์„ฑ(Negative)์ด๋ผ๊ณ  ๋Œ€๋‹ตํ•˜์˜€๋Š”๋ฐ ์‹ค์ œ๋กœ ์Œ์„ฑ์ด๋ผ์„œ ์ •๋‹ต์„ ๋งžํžŒ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ FP๋Š” ์–‘์„ฑ์ด๋ผ๊ณ  ๋Œ€๋‹ตํ•˜์˜€๋Š”๋ฐ, ์Œ์„ฑ์ด๋ผ์„œ ์ •๋‹ต์„ ํ‹€๋ฆฐ ๊ฒฝ์šฐ์ด๋ฉฐ FN์€ ์Œ์„ฑ์ด๋ผ๊ณ  ๋Œ€๋‹ตํ•˜์˜€๋Š”๋ฐ ์–‘์„ฑ์ด๋ผ์„œ ์ •๋‹ต์„ ํ‹€๋ฆฐ ๊ฒฝ์šฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜๋ฉด ๋˜ ์ƒˆ๋กœ์šด ๊ฐœ๋…์ธ ์ •๋ฐ€๋„(Precision)๊ณผ ์žฌํ˜„์œจ(Recall)์ด ๋ฉ๋‹ˆ๋‹ค. 1) ์ •๋ฐ€๋„(Precision) ์ •๋ฐ€๋„์€ ์–‘์„ฑ์ด๋ผ๊ณ  ๋Œ€๋‹ตํ•œ ์ „์ฒด ์ผ€์ด์Šค์— ๋Œ€ํ•œ TP์˜ ๋น„์œจ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ •๋ฐ€๋„๋ฅผ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ •๋ฐ€๋„ ๋ฐ€ = P P F 2) ์žฌํ˜„์œจ(Recall) ์žฌํ˜„์œจ์€ ์‹ค์ œ ๊ฐ’์ด ์–‘์„ฑ์ธ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๊ฐœ์ˆ˜์— ๋Œ€ํ•ด์„œ TP์˜ ๋น„์œจ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์–‘์„ฑ์ธ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์–ผ๋งˆ๋‚˜ ์–‘์„ฑ์ธ์ง€๋ฅผ ์˜ˆ์ธก(์žฌํ˜„) ํ–ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์žฌํ˜„์œจ ํ˜„ = P P F 6. ๊ณผ ์ ํ•ฉ(Overfitting)๊ณผ ๊ณผ์†Œ ์ ํ•ฉ(Underfitting) ํ•™์ƒ์˜ ์ž…์žฅ์ด ๋˜์–ด ๊ฐ™์€ ๋ฌธ์ œ์ง€๋ฅผ ๊ณผํ•˜๊ฒŒ ๋งŽ์ด ํ’€์–ด์„œ ๋ฌธ์ œ ๋ฒˆํ˜ธ๋งŒ ๋ด๋„ ์ •๋‹ต์„ ๋งžํž ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‹ค๋ฅธ ๋ฌธ์ œ์ง€๋‚˜ ์‹œํ—˜์„ ๋ณด๋ฉด ์ ์ˆ˜๊ฐ€ ์•ˆ ์ข‹๋‹ค๋ฉด ๊ทธ๊ฒŒ ์˜๋ฏธ๊ฐ€ ์žˆ์„๊นŒ์š”? ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ๊ณผ ์ ํ•ฉ(Overfitting)์ด๋ž€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณผํ•˜๊ฒŒ ํ•™์Šตํ•œ ๊ฒฝ์šฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์‹ค์ œ๋กœ ์กด์žฌํ•˜๋Š” ๋งŽ์€ ๋ฐ์ดํ„ฐ์˜ ์ผ๋ถ€์— ๋ถˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋งŒ ๊ณผํ•˜๊ฒŒ ํ•™์Šตํ•˜๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋‚˜ ์‹ค์ œ ์„œ๋น„์Šค์—์„œ์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์ •ํ™•๋„๊ฐ€ ์ข‹์ง€ ์•Š์€ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ•์•„์ง€ ์‚ฌ์ง„๊ณผ ๊ณ ์–‘์ด ์‚ฌ์ง„์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ๊ณ„๊ฐ€ ์žˆ์„ ๋•Œ, ๊ฒ€์€์ƒ‰ ๊ฐ•์•„์ง€ ์‚ฌ์ง„ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณผํ•˜๊ฒŒ ํ•™์Šตํ•˜๋ฉด ๊ธฐ๊ณ„๋Š” ๋‚˜์ค‘์— ๊ฐ€์„œ๋Š” ํฐ์ƒ‰ ๊ฐ•์•„์ง€๋‚˜, ๊ฐˆ์ƒ‰ ๊ฐ•์•„์ง€๋ฅผ ๋ณด๊ณ ๋„ ๊ฐ•์•„์ง€๊ฐ€ ์•„๋‹ˆ๋ผ๊ณ  ํŒ๋‹จํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ง€๋‚˜์นœ ์ผ๋ฐ˜ํ™”๋ฅผ ํ•œ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ๊ณผ์ ํ•ฉ ์ƒํ™ฉ์—์„œ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์˜ค์ฐจ๊ฐ€ ๋‚ฎ์ง€๋งŒ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์˜ค์ฐจ๊ฐ€ ๋†’์•„์ง€๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ž˜ํ”„๋Š” ๊ณผ์ ํ•ฉ ์ƒํ™ฉ์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ํ›ˆ๋ จ ํšŸ์ˆ˜์— ๋”ฐ๋ฅธ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์˜ค์ฐจ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์˜ค์ฐจ์˜ ๋ณ€ํ™”๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. X ์ถ•์˜ ์—ํฌํฌ(epoch)๋Š” ์ „์ฒด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํ›ˆ๋ จ ํšŸ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ž˜ํ”„๋Š” ์—ํฌํฌ๊ฐ€ 3~4๋ฅผ ๋„˜์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด ๊ณผ์ ํ•ฉ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ž˜ํ”„๋Š” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ค์ฐจ๊ฐ€ ์ ์ฐจ ์ฆ๊ฐ€ํ•˜๋Š” ์–‘์ƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ๋งํ•˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋Š” ๋†’์ง€๋งŒ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” ์ •ํ™•๋„๊ฐ€ ๋‚ฎ์€ ์ƒํ™ฉ์ด๋ผ๊ณ  ๋งํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์˜ค์ฐจ๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ธฐ ์ „์ด๋‚˜, ์ •ํ™•๋„๊ฐ€ ๊ฐ์†Œํ•˜๊ธฐ ์ „์— ํ›ˆ๋ จ์„ ๋ฉˆ์ถ”๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ๊ณผ์ ํ•ฉ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์„ฑ๋Šฅ์ด ๋‚ฎ์•„์ง€๊ธฐ ์ „์— ํ›ˆ๋ จ์„ ๋ฉˆ์ถ”๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•˜๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์„ฑ๋Šฅ์ด ์˜ฌ๋ผ๊ฐˆ ์—ฌ์ง€๊ฐ€ ์žˆ์Œ์—๋„ ํ›ˆ๋ จ์„ ๋œ ํ•œ ์ƒํƒœ๋ฅผ ๋ฐ˜๋Œ€๋กœ ๊ณผ์†Œ ์ ํ•ฉ(Underfitting)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ณผ์†Œ ์ ํ•ฉ์€ ํ›ˆ๋ จ ์ž์ฒด๊ฐ€ ๋ถ€์กฑํ•œ ์ƒํƒœ์ด๋ฏ€๋กœ ๊ณผ๋Œ€ ์ ํ•ฉ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ๋ณดํ†ต ์ •ํ™•๋„๊ฐ€ ๋‚ฎ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋‘ ๊ฐ€์ง€ ํ˜„์ƒ์„ ๊ณผ์ ํ•ฉ๊ณผ ๊ณผ์†Œ ์ ํ•ฉ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ์ด์œ ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ํ•™์Šต ๋˜๋Š” ํ›ˆ๋ จ์ด๋ผ๊ณ  ํ•˜๋Š” ๊ณผ์ •์„ ์ ํ•ฉ(fitting)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ ํ•ฉํ•ด์ ธ๊ฐ€๋Š” ๊ณผ์ •์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฅ ๋Ÿฌ๋‹์„ ํ•  ๋•Œ๋Š” ๊ณผ์ ํ•ฉ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋Š” ๋“œ๋กญ์•„์›ƒ(Dropout), ์กฐ๊ธฐ ์ข…๋ฃŒ(Early Stopping)๊ณผ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•˜๋Š”๋ฐ ์ด๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์ฑ•ํ„ฐ์—์„œ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 06-02 ํผ์…‰ํŠธ๋ก (Perceptron) ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ์ˆ˜๋งŽ์€ ๋จธ์‹  ๋Ÿฌ๋‹ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ตœ๊ทผ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๋ณต์žกํ•˜๊ฒŒ ์Œ“์•„ ์˜ฌ๋ฆฐ ๋”ฅ ๋Ÿฌ๋‹์ด ๋‹ค๋ฅธ ๋จธ์‹  ๋Ÿฌ๋‹ ๋ฐฉ๋ฒ•๋“ค์„ ๋›ฐ์–ด๋„˜๋Š” ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๋Š” ์‚ฌ๋ก€๊ฐ€ ๋Š˜๋ฉด์„œ, ์ „ํ†ต์ ์ธ ๋จธ์‹  ๋Ÿฌ๋‹๊ณผ ๋”ฅ ๋Ÿฌ๋‹์„ ๊ตฌ๋ถ„ํ•ด์„œ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋ชฉ์†Œ๋ฆฌ๊ฐ€ ์ปค์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฅ ๋Ÿฌ๋‹์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•œ๋ฐ, ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ดˆ๊ธฐ์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ธ ํผ์…‰ํŠธ๋ก (Perceptron)์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 1. ํผ์…‰ํŠธ๋ก (Perceptron) ํผ์…‰ํŠธ๋ก (Perceptron)์€ ํ”„๋ž‘ํฌ ๋กœ์  ๋ธ”๋ผํŠธ(Frank Rosenblatt)๊ฐ€ 1957๋…„์— ์ œ์•ˆํ•œ ์ดˆ๊ธฐ ํ˜•ํƒœ์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์œผ๋กœ ๋‹ค์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์€ ์‹ค์ œ ๋‡Œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์‹ ๊ฒฝ ์„ธํฌ ๋‰ด๋Ÿฐ์˜ ๋™์ž‘๊ณผ ์œ ์‚ฌํ•œ๋ฐ, ์‹ ๊ฒฝ ์„ธํฌ ๋‰ด๋Ÿฐ์˜ ๊ทธ๋ฆผ์„ ๋จผ์ € ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‰ด๋Ÿฐ์€ ๊ฐ€์ง€๋Œ๊ธฐ์—์„œ ์‹ ํ˜ธ๋ฅผ ๋ฐ›์•„๋“ค์ด๊ณ , ์ด ์‹ ํ˜ธ๊ฐ€ ์ผ์ •์น˜ ์ด์ƒ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋ฉด ์ถ•์‚ญ๋Œ๊ธฐ๋ฅผ ํ†ตํ•ด์„œ ์‹ ํ˜ธ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋‹ค์ˆ˜์˜ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ํผ์…‰ํŠธ๋ก ์˜ ๊ทธ๋ฆผ์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ ๊ฒฝ ์„ธํฌ ๋‰ด๋Ÿฐ์˜ ์ž…๋ ฅ ์‹ ํ˜ธ์™€ ์ถœ๋ ฅ ์‹ ํ˜ธ๊ฐ€ ํผ์…‰ํŠธ๋ก ์—์„œ ๊ฐ๊ฐ ์ž…๋ ฅ๊ฐ’๊ณผ ์ถœ๋ ฅ๊ฐ’์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค.๋Š” ์ž…๋ ฅ๊ฐ’์„ ์˜๋ฏธํ•˜๋ฉฐ,๋Š” ๊ฐ€์ค‘์น˜(Weight),๋Š” ์ถœ๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆผ ์•ˆ์˜ ์›์€ ์ธ๊ณต ๋‰ด๋Ÿฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ์‹ ๊ฒฝ ์„ธํฌ ๋‰ด๋Ÿฐ์—์„œ์˜ ์‹ ํ˜ธ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์ถ•์‚ญ๋Œ๊ธฐ์˜ ์—ญํ• ์„ ํผ์…‰ํŠธ๋ก ์—์„œ๋Š” ๊ฐ€์ค‘์น˜๊ฐ€ ๋Œ€์‹ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ธ๊ณต ๋‰ด๋Ÿฐ์—์„œ ๋ณด๋‚ด์ง„ ์ž…๋ ฅ๊ฐ’ ๋Š” ๊ฐ๊ฐ์˜ ๊ฐ€์ค‘์น˜ ์™€ ํ•จ๊ป˜ ์ข…์ฐฉ์ง€์ธ ์ธ๊ณต ๋‰ด๋Ÿฐ์— ์ „๋‹ฌ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ž…๋ ฅ๊ฐ’์—๋Š” ๊ฐ๊ฐ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ, ์ด๋•Œ ๊ฐ€์ค‘์น˜์˜ ๊ฐ’์ด ํฌ๋ฉด ํด์ˆ˜๋ก ํ•ด๋‹น ์ž…๋ ฅ ๊ฐ’์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž…๋ ฅ๊ฐ’์ด ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ ธ์„œ ์ธ๊ณต ๋‰ด๋Ÿฐ์— ๋ณด๋‚ด์ง€๊ณ , ๊ฐ ์ž…๋ ฅ๊ฐ’๊ณผ ๊ทธ์— ํ•ด๋‹น๋˜๋Š” ๊ฐ€์ค‘์น˜์˜ ๊ณฑ์˜ ์ „์ฒด ํ•ฉ์ด ์ž„๊ณ„์น˜(threshold)๋ฅผ ๋„˜์œผ๋ฉด ์ข…์ฐฉ์ง€์— ์žˆ๋Š” ์ธ๊ณต ๋‰ด๋Ÿฐ์€ ์ถœ๋ ฅ ์‹ ํ˜ธ๋กœ์„œ 1์„ ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” 0์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋ฅผ ๊ณ„๋‹จ ํ•จ์ˆ˜(Step function)๋ผ๊ณ  ํ•˜๋ฉฐ, ์•„๋ž˜๋Š” ๊ทธ๋ž˜ํ”„๋Š” ๊ณ„๋‹จ ํ•จ์ˆ˜์˜ ํ•˜๋‚˜์˜ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋•Œ ๊ณ„๋‹จ ํ•จ์ˆ˜์— ์‚ฌ์šฉ๋œ ์ด ์ž„๊ณ„์น˜ ๊ฐ’์„ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ๋ณดํ†ต ์„ธํƒ€(ฮ˜)๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. f i W x โ‰ฅ โ†’ = i โˆ‘ n i i < โ†’ = ๋‹จ, ์œ„์˜ ์‹์—์„œ ์ž„๊ณ„์น˜๋ฅผ ์ขŒ๋ณ€์œผ๋กœ ๋„˜๊ธฐ๊ณ  ํŽธํ–ฅ (bias)๋กœ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํŽธํ–ฅ ๋˜ํ•œ ํผ์…‰ํŠธ๋ก ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ์ž…๋ ฅ๊ฐ’์ด 1๋กœ ๊ณ ์ •๋˜๊ณ  ํŽธํ–ฅ ๊ฐ€ ๊ณฑํ•ด์ง€๋Š” ๋ณ€์ˆ˜๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. f i W x + โ‰ฅ โ†’ = i โˆ‘ n i i b 0 y 0 ์ด ์ฑ…์„ ํฌํ•จํ•œ ๋งŽ์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์ž๋ฃŒ์—์„œ ํŽธ์˜์ƒ ํŽธํ–ฅ ๊ฐ€ ๊ทธ๋ฆผ์ด๋‚˜ ์ˆ˜์‹์—์„œ ์ƒ๋žต๋ผ์„œ ํ‘œํ˜„๋˜๊ธฐ๋„ ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ํŽธํ–ฅ ๋˜ํ•œ ๋”ฅ ๋Ÿฌ๋‹์ด ์ตœ์ ์˜ ๊ฐ’์„ ์ฐพ์•„์•ผ ํ•  ๋ณ€์ˆ˜ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šฐ๊ฒ ์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋‰ด๋Ÿฐ์—์„œ ์ถœ๋ ฅ๊ฐ’์„ ๋ณ€๊ฒฝ์‹œํ‚ค๋Š” ํ•จ์ˆ˜๋ฅผ ํ™œ์„ฑํ™” ํ•จ์ˆ˜(Activation Function)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ดˆ๊ธฐ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์ธ ํผ์…‰ํŠธ๋ก ์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ๊ณ„๋‹จ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, ๊ทธ ๋’ค์— ๋“ฑ์žฅํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐœ์ „๋œ ์‹ ๊ฒฝ๋ง๋“ค์€ ๊ณ„๋‹จ ํ•จ์ˆ˜ ์™ธ์—๋„ ์—ฌ๋Ÿฌ ๋‹ค์–‘ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์•ž์„œ ๋ฐฐ์šด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋‚˜ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜ ๋˜ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์„ ๋ฐฐ์šฐ๊ธฐ ์ „์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ๋จผ์ € ๋ฐฐ์šด ์ด์œ ๋„ ์—ฌ๊ธฐ์— ์žˆ์Šต๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ๊ณ„๋‹จ ํ•จ์ˆ˜์ด์ง€๋งŒ ์—ฌ๊ธฐ์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฐฉ๊ธˆ ๋ฐฐ์šด ํผ์…‰ํŠธ๋ก ์€ ๊ณง ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์™€ ๋™์ผํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜๋ฉด ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจ๋ธ์ด ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ๋Š” ํ•˜๋‚˜์˜ ์ธ๊ณต ๋‰ด๋Ÿฐ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์ธ๊ณต ๋‰ด๋Ÿฐ๊ณผ ์œ„์—์„œ ๋ฐฐ์šด ํผ์…‰ํŠธ๋ก ์˜ ์ฐจ์ด๋Š” ์˜ค์ง ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ์ฐจ์ด์ž…๋‹ˆ๋‹ค. ์ธ๊ณต ๋‰ด๋Ÿฐ : ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ( i W x + ) ์œ„์˜ ํผ์…‰ํŠธ๋ก (์ธ๊ณต ๋‰ด๋Ÿฐ ์ข…๋ฅ˜ ์ค‘ ํ•˜๋‚˜) : ๊ณ„๋‹จ ํ•จ์ˆ˜ ( i W x + ) 2. ๋‹จ์ธต ํผ์…‰ํŠธ๋ก (Single-Layer Perceptron) ์œ„์—์„œ ๋ฐฐ์šด ํผ์…‰ํŠธ๋ก ์„ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์€ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ๊ณผ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง€๋Š”๋ฐ, ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ ๊ฐ’์„ ๋ณด๋‚ด๋Š” ๋‹จ๊ณ„๊ณผ ๊ฐ’์„ ๋ฐ›์•„์„œ ์ถœ๋ ฅํ•˜๋Š” ๋‘ ๋‹จ๊ณ„๋กœ๋งŒ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ ์ด ๊ฐ ๋‹จ๊ณ„๋ฅผ ๋ณดํ†ต ์ธต(layer)๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ์ด ๋‘ ๊ฐœ์˜ ์ธต์„ ์ž…๋ ฅ์ธต(input layer)๊ณผ ์ถœ๋ ฅ์ธต(output layer)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ํ•œ๊ณ„๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด ํ–ฅํ›„์— ๋‚˜์˜จ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋ฉด ๋‹จ์ธต๊ณผ ๋‹ค์ธต ์ด ๋‘ ํผ์…‰ํŠธ๋ก ์ด ์–ด๋–ค ์ฐจ์ด๋ฅผ ๊ฐ€์ง€๋Š”์ง€ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์ด ์–ด๋–ค ์ผ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ•œ๊ณ„๋Š” ๋ฌด์—‡์ธ์ง€ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์„ ์ด์šฉํ•˜๋ฉด AND, NAND, OR ๊ฒŒ์ดํŠธ๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒŒ์ดํŠธ ์—ฐ์‚ฐ์— ์“ฐ์ด๋Š” ๊ฒƒ์€ ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’๊ณผ ํ•˜๋‚˜์˜ ์ถœ๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด AND ๊ฒŒ์ดํŠธ์˜ ๊ฒฝ์šฐ์—๋Š” ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ ๊ฐ’์ด ๋ชจ๋‘ 1์ธ ๊ฒฝ์šฐ์—๋งŒ ์ถœ๋ ฅ๊ฐ’์ด 1์ด ๋‚˜์˜ค๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์‹์„ ํ†ตํ•ด AND ๊ฒŒ์ดํŠธ๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๋‘ ๊ฐœ์˜ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ ๊ฐ’์—๋Š” ๋ญ๊ฐ€ ์žˆ์„๊นŒ์š”? ๊ฐ๊ฐ 1 w,๋ผ๊ณ  ํ•œ๋‹ค๋ฉด [0.5, 0.5, -0.7], [0.5, 0.5, -0.8] ๋˜๋Š” [1.0, 1.0, -1.0] ๋“ฑ ์ด ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ์กฐํ•ฉ์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด์„œ AND ๊ฒŒ์ดํŠธ๋ฅผ ์œ„ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ ๊ฐ€์ง„ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์‹์„ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. def AND_gate(x1, x2): w1=0.5 w2=0.5 b=-0.7 result = x1*w1 + x2*w2 + b if result <= 0: return 0 else: return 1 ์œ„์˜ ํ•จ์ˆ˜์— AND ๊ฒŒ์ดํŠธ์˜ ์ž…๋ ฅ๊ฐ’์„ ๋ชจ๋‘ ๋„ฃ์–ด๋ณด๋ฉด ์˜ค์ง ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ์—๋งŒ 1์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. AND_gate(0, 0), AND_gate(0, 1), AND_gate(1, 0), AND_gate(1, 1) (0, 0, 0, 1) ๊ทธ๋ ‡๋‹ค๋ฉด ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ์—๋งŒ ์ถœ๋ ฅ๊ฐ’์ด 0, ๋‚˜๋จธ์ง€ ์ž…๋ ฅ๊ฐ’์˜ ์Œ(pair)์— ๋Œ€ํ•ด์„œ๋Š” ๋ชจ๋‘ ์ถœ๋ ฅ๊ฐ’์ด 1์ด ๋‚˜์˜ค๋Š” NAND ๊ฒŒ์ดํŠธ๋Š” ์–ด๋–จ๊นŒ์š”? ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋˜ AND ๊ฒŒ์ดํŠธ๋ฅผ ์ถฉ์กฑํ•˜๋Š” ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ ๊ฐ’์ธ [0.5, 0.5, -0.7]์— -๋ฅผ ๋ถ™์—ฌ์„œ [-0.5, -0.5, +0.7]์„ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์‹์— ๋„ฃ์–ด๋ณด๋ฉด NAND ๊ฒŒ์ดํŠธ๋ฅผ ์ถฉ์กฑํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ด๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. def NAND_gate(x1, x2): w1=-0.5 w2=-0.5 b=0.7 result = x1*w1 + x2*w2 + b if result <= 0: return 0 else: return 1 ๋‹จ์ง€ ๊ฐ™์€ ์ฝ”๋“œ์— ํ•จ์ˆ˜ ์ด๋ฆ„๊ณผ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ๋งŒ ๋ฐ”๊ฟจ์„ ๋ฟ์ž…๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์˜ ๊ตฌ์กฐ๋Š” ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. NAND_gate(0, 0), NAND_gate(0, 1), NAND_gate(1, 0), NAND_gate(1, 1) (1, 1, 1, 0) NAND ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ ํŒŒ์ด์ฌ ์ฝ”๋“œ์— ์ž…๋ ฅ๊ฐ’์„ ๋„ฃ์ž, ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ์—๋งŒ 0์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก ์œผ๋กœ NAND ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. [-0.5, -0.5, -0.7] ์™ธ์—๋„ ํผ์…‰ํŠธ๋ก ์ด NAND ๊ฒŒ์ดํŠธ์˜ ๋™์ž‘์„ ํ•˜๋„๋ก ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ๊ฐ’๋“ค์ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์ด ๋ชจ๋‘ 0์ธ ๊ฒฝ์šฐ์— ์ถœ๋ ฅ๊ฐ’์ด 0์ด๊ณ  ๋‚˜๋จธ์ง€ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋‘ ์ถœ๋ ฅ๊ฐ’์ด 1์ธ OR ๊ฒŒ์ดํŠธ ๋˜ํ•œ ์ ์ ˆํ•œ ๊ฐ€์ค‘์น˜ ๊ฐ’๊ณผ ํŽธํ–ฅ ๊ฐ’๋งŒ ์ฐพ์œผ๋ฉด ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์‹์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ๊ฐ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์— ๋Œ€ํ•ด์„œ [0.6, 0.6, -0.5]๋ฅผ ์„ ํƒํ•˜๋ฉด OR ๊ฒŒ์ดํŠธ๋ฅผ ์ถฉ์กฑํ•ฉ๋‹ˆ๋‹ค. def OR_gate(x1, x2): w1=0.6 w2=0.6 b=-0.5 result = x1*w1 + x2*w2 + b if result <= 0: return 0 else: return 1 OR_gate(0, 0), OR_gate(0, 1), OR_gate(1, 0), OR_gate(1, 1) (0, 1, 1, 1) ๋ฌผ๋ก , ์ด ์™ธ์—๋„ ์ด๋ฅผ ์ถฉ์กฑํ•˜๋Š” ๋‹ค์–‘ํ•œ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ๊ฐ’์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ AND ๊ฒŒ์ดํŠธ, NAND ๊ฒŒ์ดํŠธ, OR ๊ฒŒ์ดํŠธ ๋˜ํ•œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ ๊ตฌํ˜„์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒŒ์ดํŠธ๊ฐ€ ์žˆ๋Š”๋ฐ ๋ฐ”๋กœ XOR ๊ฒŒ์ดํŠธ์ž…๋‹ˆ๋‹ค. XOR ๊ฒŒ์ดํŠธ๋Š” ์ž…๋ ฅ๊ฐ’ ๋‘ ๊ฐœ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์„ ๋•Œ์—๋งŒ ์ถœ๋ ฅ๊ฐ’์ด 1์ด ๋˜๊ณ , ์ž…๋ ฅ๊ฐ’ ๋‘ ๊ฐœ๊ฐ€ ์„œ๋กœ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋ฉด ์ถœ๋ ฅ๊ฐ’์ด 0์ด ๋˜๋Š” ๊ฒŒ์ดํŠธ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ํŒŒ์ด์ฌ ์ฝ”๋“œ์— ์•„๋ฌด๋ฆฌ ์ˆ˜๋งŽ์€ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ๋„ฃ์–ด๋ด๋„ XOR ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ ์ง์„  ํ•˜๋‚˜๋กœ ๋‘ ์˜์—ญ์„ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ๋งŒ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด AND ๊ฒŒ์ดํŠธ์— ๋Œ€ํ•œ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์„ ์‹œ๊ฐํ™”ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ๋Š” ์ถœ๋ ฅ๊ฐ’ 0์„ ํ•˜์–€์ƒ‰ ์›, 1์„ ๊ฒ€์€์ƒ‰ ์›์œผ๋กœ ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. AND ๊ฒŒ์ดํŠธ๋ฅผ ์ถฉ์กฑํ•˜๋ ค๋ฉด ํ•˜์–€์ƒ‰ ์›๊ณผ ๊ฒ€์€์ƒ‰ ์›์„ ์ง์„ ์œผ๋กœ ๋‚˜๋ˆ„๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ NAND ๊ฒŒ์ดํŠธ๋‚˜ OR ๊ฒŒ์ดํŠธ์— ๋Œ€ํ•ด์„œ๋„ ์‹œ๊ฐํ™”๋ฅผ ํ–ˆ์„ ๋•Œ ์ง์„ ์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด XOR ๊ฒŒ์ดํŠธ๋Š” ์–ด๋–จ๊นŒ์š”? XOR ๊ฒŒ์ดํŠธ๋Š” ์ž…๋ ฅ๊ฐ’ ๋‘ ๊ฐœ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์„ ๋•Œ์—๋งŒ ์ถœ๋ ฅ๊ฐ’์ด 1์ด ๋˜๊ณ , ์ž…๋ ฅ๊ฐ’ ๋‘ ๊ฐœ๊ฐ€ ์„œ๋กœ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋ฉด ์ถœ๋ ฅ๊ฐ’์ด 0์ด ๋˜๋Š” ๊ฒŒ์ดํŠธ์ž…๋‹ˆ๋‹ค. XOR ๊ฒŒ์ดํŠธ๋ฅผ ์‹œ๊ฐํ™”ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•˜์–€์ƒ‰ ์›๊ณผ ๊ฒ€์€์ƒ‰ ์›์„ ์ง์„  ํ•˜๋‚˜๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ๋Š” XOR ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ ์„ ํ˜• ์˜์—ญ์— ๋Œ€ํ•ด์„œ๋งŒ ๋ถ„๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค XOR ๊ฒŒ์ดํŠธ๋Š” ์ง์„ ์ด ์•„๋‹Œ ๊ณก์„ . ๋น„์„ ํ˜• ์˜์—ญ์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๊ณก์„ ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํ•˜์–€์ƒ‰ ์›๊ณผ ๊ฒ€์€์ƒ‰ ์›์„ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Œ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด์ œ XOR ๊ฒŒ์ดํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 3. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก (MultiLayer Perceptron, MLP) XOR ๊ฒŒ์ดํŠธ๋Š” ๊ธฐ์กด์˜ AND, NAND, OR ๊ฒŒ์ดํŠธ๋ฅผ ์กฐํ•ฉํ•˜๋ฉด ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํผ์…‰ํŠธ๋ก  ๊ด€์ ์—์„œ ๋งํ•˜๋ฉด, ์ธต์„ ๋” ์Œ“์œผ๋ฉด ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ๊ณผ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์ฐจ์ด๋Š” ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต๋งŒ ์กด์žฌํ•˜์ง€๋งŒ, ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์ค‘๊ฐ„์— ์ธต์„ ๋” ์ถ”๊ฐ€ํ•˜์˜€๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต ์‚ฌ์ด์— ์กด์žฌํ•˜๋Š” ์ธต์„ ์€๋‹‰์ธต(hidden layer)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์ค‘๊ฐ„์— ์€๋‹‰์ธต์ด ์กด์žฌํ•œ๋‹ค๋Š” ์ ์ด ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์ค„์—ฌ์„œ MLP๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ AND, NAND, OR ๊ฒŒ์ดํŠธ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ XOR ๊ฒŒ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ์˜ˆ์ž…๋‹ˆ๋‹ค. (์‹ค์ œ ๊ตฌํ˜„์€ ์ˆ™์ œ๋กœ ๋‚จ๊ฒจ๋‘๊ฒ ์Šต๋‹ˆ๋‹ค. ํžŒํŠธ๋ฅผ ๋“œ๋ฆฌ์ž๋ฉด ์œ„์˜ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์—์„œ ์‚ฌ์šฉํ•œ ํ•จ์ˆ˜๋“ค์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.) XOR ์˜ˆ์ œ์—์„œ๋Š” ์€๋‹‰์ธต 1๊ฐœ๋งŒ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ, ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ๋ณธ๋ž˜ ์€๋‹‰์ธต์ด 1๊ฐœ ์ด์ƒ์ธ ํผ์…‰ํŠธ๋ก ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, XOR ๋ฌธ์ œ๋ณด๋‹ค ๋”์šฑ ๋ณต์žกํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ์ค‘๊ฐ„์— ์ˆ˜๋งŽ์€ ์€๋‹‰์ธต์„ ๋” ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์€๋‹‰์ธต์˜ ๊ฐœ์ˆ˜๋Š” 2๊ฐœ์ผ ์ˆ˜๋„ ์žˆ๊ณ , ์ˆ˜์‹ญ ๊ฐœ์ผ ์ˆ˜๋„ ์žˆ๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•˜๊ธฐ ๋‚˜๋ฆ„์ž…๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ๋” ์–ด๋ ค์šด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ ์€๋‹‰์ธต์ด ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€๋˜๊ณ (์ด ๊ฒฝ์šฐ์—๋Š” ์€๋‹‰์ธต์ด 2๊ฐœ), ๋‰ด๋Ÿฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ฆฐ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์€๋‹‰์ธต์ด 2๊ฐœ ์ด์ƒ์ธ ์‹ ๊ฒฝ๋ง์„ ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง(Deep Neural Network, DNN)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง์€ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ๋งŒ ์ด์•ผ๊ธฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์—ฌ๋Ÿฌ ๋ณ€ํ˜•๋œ ๋‹ค์–‘ํ•œ ์‹ ๊ฒฝ๋ง๋“ค๋„ ์€๋‹‰์ธต์ด 2๊ฐœ ์ด์ƒ์ด ๋˜๋ฉด ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” OR, AND, XOR ๊ฒŒ์ดํŠธ ๋“ฑ. ํผ์…‰ํŠธ๋ก ์ด ๊ฐ€์•ผ ํ•  ์ •๋‹ต์„ ์ฐธ๊ณ ๋กœ ํผ์…‰ํŠธ๋ก ์ด ์ •๋‹ต์„ ์ถœ๋ ฅํ•  ๋•Œ๊นŒ์ง€ ๊ฐ€์ค‘์น˜๋ฅผ ๋ฐ”๊ฟ”๋ณด๋ฉด์„œ ๋งž๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๊ฐ€์ค‘์น˜๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด์ œ๋Š” ๊ธฐ๊ณ„๊ฐ€ ๊ฐ€์ค‘์น˜๋ฅผ ์Šค์Šค๋กœ ์ฐพ์•„๋‚ด๋„๋ก ์ž๋™ํ™”์‹œ์ผœ์•ผ ํ•˜๋Š”๋ฐ, ์ด๊ฒƒ์ด ๋จธ์‹  ๋Ÿฌ๋‹์—์„œ ๋งํ•˜๋Š” ํ•™์Šต(training) ๋‹จ๊ณ„์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์„ ํ˜• ํšŒ๊ท€์™€ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์—์„œ ๋ณด์•˜๋“ฏ์ด ์†์‹ค ํ•จ์ˆ˜(Loss function)์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €(Optimizer)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ ํ•™์Šต์„ ์‹œํ‚ค๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง์ผ ๊ฒฝ์šฐ์—๋Š” ์ด๋ฅผ ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง์„ ํ•™์Šต์‹œํ‚จ๋‹ค๊ณ  ํ•˜์—ฌ, ๋”ฅ ๋Ÿฌ๋‹(Deep Learning)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก  : http://www.aistudy.com/neural/multilayer_perceptron.htm 06-03 XOR ๋ฌธ์ œ - ๋‹จ์ธต ํผ์…‰ํŠธ๋ก  ๊ตฌํ˜„ํ•˜๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํŒŒ์ด ํ† ์น˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์„ ๊ตฌํ˜„ํ•˜์—ฌ XOR ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋Š” ๊ฒƒ์„ ์‹œ๋„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํŒŒ์ด ํ† ์น˜๋กœ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก  ๊ตฌํ˜„ํ•˜๊ธฐ ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋ฅผ ์ž„ํฌํŠธํ•˜๊ณ , GPU ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•  ๊ฒฝ์šฐ์—๋Š” GPU ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. import torch device = 'cuda' if torch.cuda.is_available() else 'cpu' torch.manual_seed(777) if device == 'cuda': torch.cuda.manual_seed_all(777) ์ด์ œ XOR ๋ฌธ์ œ์— ํ•ด๋‹น๋˜๋Š” ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. X = torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]).to(device) Y = torch.FloatTensor([[0], [1], [1], [0]]).to(device) ์ด์ œ 1๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ๊ฐ€์ง€๋Š” ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์ด ์ฒ˜์Œ ์†Œ๊ฐœ๋˜์—ˆ์„ ๋•Œ๋Š” ๊ณ„๋‹จ ํ•จ์ˆ˜์˜€์ง€๋งŒ, ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ ๋˜ ๋‹ค๋ฅธ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์ธ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์•Œ๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. linear = nn.Linear(2, 1, bias=True) sigmoid = nn.Sigmoid() model = nn.Sequential(linear, sigmoid).to(device) 0 ๋˜๋Š” 1์„ ์˜ˆ์ธกํ•˜๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ด๋ฏ€๋กœ ๋น„์šฉ ํ•จ์ˆ˜๋กœ๋Š” ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. nn.BCELoss()๋Š” ์ด์ง„ ๋ถ„๋ฅ˜์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. # ๋น„์šฉ ํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ •์˜ criterion = torch.nn.BCELoss().to(device) optimizer = torch.optim.SGD(model.parameters(), lr=1) #10,001๋ฒˆ์˜ ์—ํฌํฌ ์ˆ˜ํ–‰. 0๋ฒˆ ์—ํฌํฌ๋ถ€ํ„ฐ 10,000๋ฒˆ ์—ํฌํฌ๊นŒ์ง€. for step in range(10001): optimizer.zero_grad() hypothesis = model(X) # ๋น„์šฉ ํ•จ์ˆ˜ cost = criterion(hypothesis, Y) cost.backward() optimizer.step() if step % 100 == 0: # 100๋ฒˆ์งธ ์—ํฌํฌ๋งˆ๋‹ค ๋น„์šฉ ์ถœ๋ ฅ print(step, cost.item()) ์ด์ œ ๋น„์šฉ์ด ์ค„์–ด๋“œ๋Š” ๊ณผ์ •์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 0 0.7273974418640137 100 0.6931476593017578 200 0.6931471824645996 ... ์ค‘๋žต ... 10000 0.6931471824645996 200๋ฒˆ ์—ํฌํฌ์— ๋น„์šฉ์ด 0.6931471824645996๊ฐ€ ์ถœ๋ ฅ๋œ ์ดํ›„์—๋Š” 10,000๋ฒˆ ์—ํฌํฌ๊ฐ€ ๋˜๋Š” ์ˆœ๊ฐ„๊นŒ์ง€ ๋” ์ด์ƒ ๋น„์šฉ์ด ์ค„์–ด๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์€ XOR ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 2. ํ•™์Šต๋œ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์˜ˆ์ธก๊ฐ’ ํ™•์ธํ•˜๊ธฐ ์ด 10,001ํšŒ ํ•™์Šตํ•œ ๋‹จ์ธต ํผ์…‰ํŠธ๋ก ์˜ ์˜ˆ์ธก๊ฐ’๋„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. with torch.no_grad(): hypothesis = model(X) predicted = (hypothesis > 0.5).float() accuracy = (predicted == Y).float().mean() print('๋ชจ๋ธ์˜ ์ถœ๋ ฅ๊ฐ’(Hypothesis): ', hypothesis.detach().cpu().numpy()) print('๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’(Predicted): ', predicted.detach().cpu().numpy()) print('์‹ค ์ œ๊ฐ’(Y): ', Y.cpu().numpy()) print('์ •ํ™•๋„(Accuracy): ', accuracy.item()) ๋ชจ๋ธ์˜ ์ถœ๋ ฅ๊ฐ’(Hypothesis): [[0.5] [0.5] [0.5] [0.5]] ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’(Predicted): [[0.] [0.] [0.] [0.]] ์‹ค์ œ ๊ฐ’(Y): [[0.] [1.] [1.] [0.]] ์ •ํ™•๋„(Accuracy): 0.5 ์‹ค์ œ ๊ฐ’์€ 0, 1, 1, 0์ž„์—๋„ ์˜ˆ์ธก๊ฐ’์€ 0, 0, 0, 0์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์ง€ ๋ชปํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 06-04 ์—ญ์ „ํŒŒ(BackPropagation) ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์ˆœ์ „ํŒŒ ๊ณผ์ •์„ ์ง„ํ–‰ํ•˜์—ฌ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜์˜€์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š”์ง€ ์ง์ ‘ ๊ณ„์‚ฐ์„ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 1. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์ดํ•ด(Neural Network Overview) ์šฐ์„  ์˜ˆ์ œ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์—ญ์ „ํŒŒ์˜ ์ดํ•ด๋ฅผ ์œ„ํ•ด์„œ ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ์ž…๋ ฅ์ธต, ์€๋‹‰์ธต, ์ถœ๋ ฅ์ธต ์ด๋ ‡๊ฒŒ 3๊ฐœ์˜ ์ธต์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•ด๋‹น ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ณผ, ๋‘ ๊ฐœ์˜ ์€๋‹‰์ธต ๋‰ด๋Ÿฐ, ๋‘ ๊ฐœ์˜ ์ถœ๋ ฅ์ธต ๋‰ด๋Ÿฐ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต๊ณผ ์ถœ๋ ฅ์ธต์˜ ๋ชจ๋“  ๋‰ด๋Ÿฐ์€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์€๋‹‰์ธต๊ณผ ์ถœ๋ ฅ์ธต์˜ ๋ชจ๋“  ๋‰ด๋Ÿฐ์—์„œ ๋ณ€์ˆ˜ ๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ๋ณ€์ˆ˜๋Š” ์ด์ „์ธต์˜ ๋ชจ๋“  ์ž…๋ ฅ์ด ๊ฐ๊ฐ์˜ ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ง„ ๊ฐ’๋“ค์ด ๋ชจ๋‘ ๋”ํ•ด์ง„ ๊ฐ€์ค‘ํ•ฉ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ ๋‰ด๋Ÿฐ์—์„œ ์•„์ง ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š์€ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์šฐ์ธก์˜ |๋ฅผ ์ง€๋‚˜์„œ ์กด์žฌํ•˜๋Š” ๋ณ€์ˆ˜ ๋˜๋Š” ๋Š” ๊ฐ€ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ํ›„์˜ ๊ฐ’์œผ๋กœ ๊ฐ ๋‰ด๋Ÿฐ์˜ ์ถœ๋ ฅ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์—ญ์ „ํŒŒ ์˜ˆ์ œ์—์„œ๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ ์—ญ์ „ํŒŒ๋ฅผ ํ†ตํ•ด ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ํŽธํ–ฅ ๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 2. ์ˆœ์ „ํŒŒ(Forward Propagation) ์ฃผ์–ด์ง„ ๊ฐ’์ด ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์„ ๋•Œ ์ˆœ์ „ํŒŒ๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์†Œ์ˆ˜์  ์•ž์˜ 0์€ ์ƒ๋žตํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด. 25๋Š” 0.25๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํŒŒ๋ž€์ƒ‰ ์ˆซ์ž๋Š” ์ž…๋ ฅ๊ฐ’์„ ์˜๋ฏธํ•˜๋ฉฐ, ๋นจ๊ฐ„์ƒ‰ ์ˆซ์ž๋Š” ๊ฐ ๊ฐ€์ค‘์น˜์˜ ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ง„ํ–‰ํ•˜๋Š” ๊ณ„์‚ฐ์˜ ๊ฒฐ๊ด๊ฐ’์€ ์†Œ์ˆ˜์  ์•„๋ž˜ ์—ฌ๋Ÿ ๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž…๋ ฅ์€ ์ž…๋ ฅ์ธต์—์„œ ์€๋‹‰์ธต ๋ฐฉํ–ฅ์œผ๋กœ ํ–ฅํ•˜๋ฉด์„œ ๊ฐ ์ž…๋ ฅ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ง€๊ณ , ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ฐ€์ค‘ ํ•ฉ์œผ๋กœ ๊ณ„์‚ฐ๋˜์–ด ์€๋‹‰์ธต ๋‰ด๋Ÿฐ์˜ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. 1 z๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ๊ฐ์˜ ๊ฐ’์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 1 W x + 2 2 0.3 0.1 0.25 0.2 0.08 2 W x + 4 2 0.4 0.1 0.35 0.2 0.11 1 z๋Š” ๊ฐ๊ฐ์˜ ์€๋‹‰์ธต ๋‰ด๋Ÿฐ์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ฒŒ ๋˜๋Š”๋ฐ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒฐ๊ด๊ฐ’์€ ์€๋‹‰์ธต ๋‰ด๋Ÿฐ์˜ ์ตœ์ข… ์ถœ๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค. ์‹์—์„œ๋Š” ๊ฐ๊ฐ 1 h์— ํ•ด๋‹น๋˜๋ฉฐ, ์•„๋ž˜์˜ ๊ฒฐ๊ณผ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1 s g o d ( 1 ) 0.51998934 2 s g o d ( 2 ) 0.52747230 1 h ์ด ๋‘ ๊ฐ’์€ ๋‹ค์‹œ ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ์œผ๋กœ ํ–ฅํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋•Œ ๋‹ค์‹œ ๊ฐ๊ฐ์˜ ๊ฐ’์— ํ•ด๋‹น๋˜๋Š” ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ง€๊ณ , ๋‹ค์‹œ ๊ฐ€์ค‘ ํ•ฉ ๋˜์–ด ์ถœ๋ ฅ์ธต ๋‰ด๋Ÿฐ์˜ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ์‹์—์„œ๋Š” ๊ฐ๊ฐ 3 z์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 3 W h + 6 2 0.45 h + 0.4 h = 0.44498412 4 W h + 8 2 0.7 h + 0.6 h = 0.68047592 3 z ์ด ์ถœ๋ ฅ์ธต ๋‰ด๋Ÿฐ์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ๊ฐ’์€ ์ด ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์ตœ์ข…์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ ์ถœ๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ๊ฐ’์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ’์œผ๋กœ์„œ ์˜ˆ์ธก๊ฐ’์ด๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 1 s g o d ( 3 ) 0.60944600 2 s g o d ( 4 ) 0.66384491 ์ด์ œ ํ•ด์•ผ ํ•  ์ผ์€ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ์˜ค์ฐจ ํ•จ์ˆ˜๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ค์ฐจ(Error)๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ์†์‹ค ํ•จ์ˆ˜(Loss function)๋กœ๋Š” ํ‰๊ท  ์ œ๊ณฑ ์˜ค์ฐจ MSE๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‹์—์„œ๋Š” ์‹ค์ œ ๊ฐ’์„ target์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜์˜€์œผ๋ฉฐ, ์ˆœ์ „ํŒŒ๋ฅผ ํ†ตํ•ด ๋‚˜์˜จ ์˜ˆ์ธก๊ฐ’์„ output์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์˜ค์ฐจ๋ฅผ ๋ชจ๋‘ ๋”ํ•˜๋ฉด ์ „์ฒด ์˜ค์ฐจ t t l ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. o = 2 ( a g t 1 o t u o) = 0.02193381 o = 2 ( a g t 2 o t u o) = 0.00203809 t t l E 1 E 2 0.02397190 3. ์—ญ์ „ํŒŒ 1๋‹จ๊ณ„(BackPropagation Step 1) ์ˆœ์ „ํŒŒ๊ฐ€ ์ž…๋ ฅ์ธต์—์„œ ์ถœ๋ ฅ์ธต์œผ๋กœ ํ–ฅํ•œ๋‹ค๋ฉด ์—ญ์ „ํŒŒ๋Š” ๋ฐ˜๋Œ€๋กœ ์ถœ๋ ฅ์ธต์—์„œ ์ž…๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ๊ณ„์‚ฐํ•˜๋ฉด์„œ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•ด๊ฐ‘๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต ๋ฐ”๋กœ ์ด์ „์˜ ์€๋‹‰์ธต์„ N ์ธต์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ถœ๋ ฅ์ธต๊ณผ N ์ธต ์‚ฌ์ด์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋‹จ๊ณ„๋ฅผ ์—ญ์ „ํŒŒ 1๋‹จ๊ณ„, ๊ทธ๋ฆฌ๊ณ  N ์ธต๊ณผ N ์ธต์˜ ์ด์ „์ธต ์‚ฌ์ด์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋‹จ๊ณ„๋ฅผ ์—ญ์ „ํŒŒ 2๋‹จ๊ณ„๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์—ญ์ „ํŒŒ 1๋‹จ๊ณ„์—์„œ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•  ๊ฐ€์ค‘์น˜๋Š” 5 W, 7 W ์ด 4๊ฐœ์ž…๋‹ˆ๋‹ค. ์›๋ฆฌ ์ž์ฒด๋Š” ๋™์ผํ•˜๋ฏ€๋กœ ์šฐ์„  5 ์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์—…๋ฐ์ดํŠธ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๊ฐ€์ค‘์น˜ 5 ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด์„œ E o a โˆ‚ 5 ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. E o a โˆ‚ 5 ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฏธ๋ถ„์˜ ์—ฐ์‡„ ๋ฒ•์น™(Chain rule)์— ๋”ฐ๋ผ์„œ ์ด์™€ ๊ฐ™์ด ํ’€์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 5 โˆ‚ t t l o ร— o โˆ‚ 3 โˆ‚ 3 W ์œ„์˜ ์‹์—์„œ ์šฐ๋ณ€์˜ ์„ธ ๊ฐœ์˜ ๊ฐ ํ•ญ์— ๋Œ€ํ•ด์„œ ์ˆœ์„œ๋Œ€๋กœ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ์ฒซ ๋ฒˆ์งธ ํ•ญ์— ๋Œ€ํ•ด์„œ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฏธ๋ถ„์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— t t l ์˜ ๊ฐ’์„ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. t t l ์€ ์•ž์„œ ์ˆœ์ „ํŒŒ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ๊ณ„์‚ฐํ–ˆ๋˜ ์ „์ฒด ์˜ค์ฐจ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. t t l 1 ( a g t 1 o t u o) + 2 ( a g t 2 o t u o) ์ด์— E o a โˆ‚ 1 ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 1 2 1 ( a g t 1 o t u o) โˆ’ ร— ( 1 ) 0 E o a โˆ‚ 1 โˆ’ ( a g t 1 o t u o) โˆ’ ( 0.4 0.60944600 ) 0.20944600 ์ด์ œ ๋‘ ๋ฒˆ์งธ ํ•ญ์„ ์ฃผ๋ชฉํ•ด ๋ด…์‹œ๋‹ค. 1 ์ด๋ผ๋Š” ๊ฐ’์€ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ๋ฏธ๋ถ„์€ ( ) ( โˆ’ ( ) ) ์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ๊ณ„์‚ฐ ๊ณผ์ •์—์„œ๋„ ๊ณ„์†ํ•ด์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ถ„ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์ƒ๊ธฐ๋ฏ€๋กœ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ์ด์— ๋”ฐ๋ผ์„œ ๋‘ ๋ฒˆ์งธ ํ•ญ์˜ ๋ฏธ๋ถ„ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜ ๋ฏธ๋ถ„ ์ฐธ๊ณ  ๋งํฌ : https://en.wikipedia.org/wiki/Logistic_function#Derivative) o โˆ‚ 3 o ร— ( โˆ’ 1 ) 0.60944600 ( โˆ’ 0.60944600 ) 0.23802157 ๋งˆ์ง€๋ง‰์œผ๋กœ ์„ธ ๋ฒˆ์งธ ํ•ญ์€ 1 ์˜ ๊ฐ’๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. z โˆ‚ 5 h = 0.51998934 ์šฐ๋ณ€์˜ ๋ชจ๋“  ํ•ญ์„ ๊ณ„์‚ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด ๊ฐ’์„ ๋ชจ๋‘ ๊ณฑํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. E o a โˆ‚ 5 0.20944600 0.23802157 0.51998934 0.02592286 ์ด์ œ ์•ž์„œ ๋ฐฐ์› ๋˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ํ†ตํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋•Œ๊ฐ€ ์™”์Šต๋‹ˆ๋‹ค! ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์— ํ•ด๋‹น๋˜๋Š” ํ•™์Šต๋ฅ (learning rate)๋Š” 0.5๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. 5 = 5 ฮฑ E o a โˆ‚ 5 0.45 0.5 0.02592286 0.43703857 ์ด์™€ ๊ฐ™์€ ์›๋ฆฌ๋กœ 6 , W + W + ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 6 โˆ‚ t t l o ร— o โˆ‚ 3 โˆ‚ 3 W โ†’ 6 = 0.38685205 E o a โˆ‚ 7 โˆ‚ t t l o ร— o โˆ‚ 4 โˆ‚ 4 W โ†’ 7 = 0.69629578 E o a โˆ‚ 8 โˆ‚ t t l o ร— o โˆ‚ 4 โˆ‚ 4 W โ†’ 8 = 0.59624247 4. ์—ญ์ „ํŒŒ 2๋‹จ๊ณ„(BackPropagation Step 2) 1๋‹จ๊ณ„๋ฅผ ์™„๋ฃŒํ•˜์˜€๋‹ค๋ฉด ์ด์ œ ์ž…๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๋ฉฐ ๋‹ค์‹œ ๊ณ„์‚ฐ์„ ์ด์–ด๊ฐ‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ๋นจ๊ฐ„์ƒ‰ ํ™”์‚ดํ‘œ๋Š” ์ˆœ์ „ํŒŒ์˜ ์ •๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์ธ ์—ญ์ „ํŒŒ์˜ ๋ฐฉํ–ฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ˜„์žฌ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ์€๋‹‰์ธต์ด 1๊ฐœ๋ฐ–์— ์—†์œผ๋ฏ€๋กœ ์ด๋ฒˆ ๋‹จ๊ณ„๊ฐ€ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์€๋‹‰์ธต์ด ๋” ๋งŽ์€ ๊ฒฝ์šฐ๋ผ๋ฉด ์ž…๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ๋‹จ๊ณ„์”ฉ ๊ณ„์†ํ•ด์„œ ๊ณ„์‚ฐํ•ด๊ฐ€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๋‹จ๊ณ„์—์„œ ๊ณ„์‚ฐํ•  ๊ฐ€์ค‘์น˜๋Š” 1 W, 3 W์ž…๋‹ˆ๋‹ค. ์›๋ฆฌ ์ž์ฒด๋Š” ๋™์ผํ•˜๋ฏ€๋กœ ์šฐ์„  1 ์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์—…๋ฐ์ดํŠธ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ๊ฐ€์ค‘์น˜ 1 ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด์„œ E o a โˆ‚ 1 ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. E o a โˆ‚ 1 ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฏธ๋ถ„์˜ ์—ฐ์‡„ ๋ฒ•์น™(Chain rule)์— ๋”ฐ๋ผ์„œ ์ด์™€ ๊ฐ™์ด ํ’€์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 1 โˆ‚ t t l h ร— h โˆ‚ 1 โˆ‚ 1 W ์œ„์˜ ์‹์—์„œ ์šฐ๋ณ€์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ์ธ E o a โˆ‚ 1 ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹ค์‹œ ์‹์„ ํ’€์–ด์„œ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 1 โˆ‚ o โˆ‚ 1 โˆ‚ o โˆ‚ 1 ์œ„์˜ ์‹์˜ ์šฐ๋ณ€์˜ ๋‘ ํ•ญ์„ ๊ฐ๊ฐ ๊ตฌํ•ด๋ด…์‹œ๋‹ค. ์šฐ์„  ์ฒซ ๋ฒˆ์งธ ํ•ญ E 1 h์— ๋Œ€ํ•ด์„œ ํ•ญ์„ ๋ถ„ํ•ด ๋ฐ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. E 1 h = E 1 z ร— z โˆ‚ 1 โˆ‚ o โˆ‚ 1 โˆ‚ 1 z ร— z โˆ‚ 1 โˆ’ ( a g t 1 o t u o) o ร— ( โˆ’ 1 ) W = 0.20944600 0.23802157 0.45 0.02243370 ์ด์™€ ๊ฐ™์€ ์›๋ฆฌ๋กœ E 2 h ๋˜ํ•œ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. E 2 h = E 2 z ร— z โˆ‚ 1 โˆ‚ o โˆ‚ 2 โˆ‚ 2 z ร— z โˆ‚ 1 0.00997311 E o a โˆ‚ 1 0.02243370 0.00997311 0.03240681 ์ด์ œ E o a โˆ‚ 1 ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ์ฒซ ๋ฒˆ์งธ ํ•ญ์„ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€ ๋‘ ํ•ญ์— ๋Œ€ํ•ด์„œ ๊ตฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. h โˆ‚ 1 h ร— ( โˆ’ 1 ) 0.51998934 ( โˆ’ 0.51998934 ) 0.24960043 z โˆ‚ 1 x = 0.1 ์ฆ‰, E o a โˆ‚ 1 ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 1 0.03240681 0.24960043 0.1 0.00080888 ์ด์ œ ์•ž์„œ ๋ฐฐ์› ๋˜ ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ํ†ตํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1 = 1 ฮฑ E o a โˆ‚ 1 0.1 0.5 0.00080888 0.29959556 ์ด์™€ ๊ฐ™์€ ์›๋ฆฌ๋กœ 2 , W + W + ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. E o a โˆ‚ 2 โˆ‚ t t l h ร— h โˆ‚ 1 โˆ‚ 1 W โ†’ 2 = 0.24919112 E o a โˆ‚ 3 โˆ‚ t t l h ร— h โˆ‚ 2 โˆ‚ 2 W โ†’ 3 = 0.39964496 E o a โˆ‚ 4 โˆ‚ t t l h ร— h โˆ‚ 2 โˆ‚ 2 W โ†’ 4 = 0.34928991 5. ๊ฒฐ๊ณผ ํ™•์ธ ์—…๋ฐ์ดํŠธ๋œ ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ˆœ์ „ํŒŒ๋ฅผ ์ง„ํ–‰ํ•˜์—ฌ ์˜ค์ฐจ๊ฐ€ ๊ฐ์†Œํ•˜์˜€๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1 W x + 2 2 0.29959556 0.1 0.24919112 0.2 0.07979778 2 W x + 4 2 0.39964496 0.1 0.34928991 0.2 0.10982248 1 s g o d ( 1 ) 0.51993887 2 s g o d ( 2 ) 0.52742806 3 W h + 6 2 0.43703857 h + 0.38685205 h = 0.43126996 4 W h + 8 2 0.69629578 h + 0.59624247 h = 0.67650625 1 s g o d ( 3 ) 0.60617688 2 s g o d ( 4 ) 0.66295848 o = 2 ( a g t 1 o t u o) = 0.02125445 o = 2 ( a g t 2 o t u o) = 0.00198189 t t l E 1 E 2 0.02323634 ๊ธฐ์กด์˜ ์ „์ฒด ์˜ค์ฐจ t t l ๊ฐ€ 0.02397190์˜€์œผ๋ฏ€๋กœ 1๋ฒˆ์˜ ์—ญ์ „ํŒŒ๋กœ ์˜ค์ฐจ๊ฐ€ ๊ฐ์†Œํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ•™์Šต์€ ์˜ค์ฐจ๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฐ€์ค‘์น˜๋ฅผ ์ฐพ๋Š” ๋ชฉ์ ์œผ๋กœ ์ˆœ์ „ํŒŒ์™€ ์—ญ์ „ํŒŒ๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. https://medium.com/@14prakash/back-propagation-is-very-simple-who-made-it-complicated-97b794c97e5c https://www.youtube.com/watch? v=ZMgax46Rd3g 06-05 XOR ๋ฌธ์ œ - ๋‹ค์ธต ํผ์…‰ํŠธ๋ก  ๊ตฌํ˜„ํ•˜๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํŒŒ์ด ํ† ์น˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ๊ตฌํ˜„ํ•˜์—ฌ XOR ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋Š” ๊ฒƒ์„ ์‹œ๋„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํŒŒ์ด ํ† ์น˜๋กœ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก  ๊ตฌํ˜„ํ•˜๊ธฐ import torch import torch.nn as nn GPU ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด GPU ์—ฐ์‚ฐ์„ ํ•˜๋„๋ก ํ•˜๊ณ , ๋žœ๋ค ์‹œ๋“œ๋ฅผ ๊ณ ์ •ํ•ด ์ค๋‹ˆ๋‹ค. device = 'cuda' if torch.cuda.is_available() else 'cpu' # for reproducibility torch.manual_seed(777) if device == 'cuda': torch.cuda.manual_seed_all(777) XOR ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์ •์˜ํ•ด ์ค๋‹ˆ๋‹ค. X = torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]).to(device) Y = torch.FloatTensor([[0], [1], [1], [0]]).to(device) ์ด์ œ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์ž…๋ ฅ์ธต, ์€๋‹‰์ธต 1, ์€๋‹‰์ธต 2, ์€๋‹‰์ธต 3, ์ถœ๋ ฅ์ธต์„ ๊ฐ€์ง€๋Š” ์€๋‹‰์ธต์ด 3๊ฐœ์ธ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. model = nn.Sequential( nn.Linear(2, 10, bias=True), # input_layer = 2, hidden_layer1 = 10 nn.Sigmoid(), nn.Linear(10, 10, bias=True), # hidden_layer1 = 10, hidden_layer2 = 10 nn.Sigmoid(), nn.Linear(10, 10, bias=True), # hidden_layer2 = 10, hidden_layer3 = 10 nn.Sigmoid(), nn.Linear(10, 1, bias=True), # hidden_layer3 = 10, output_layer = 1 nn.Sigmoid() ).to(device) ์œ„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋น„์šฉ ํ•จ์ˆ˜์™€ ์˜ตํƒ€๋งˆ์ด์ €๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. nn.BCELoss()๋Š” ์ด์ง„ ๋ถ„๋ฅ˜์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. criterion = torch.nn.BCELoss().to(device) optimizer = torch.optim.SGD(model.parameters(), lr=1) # modified learning rate from 0.1 to 1 ์ด 10,001๋ฒˆ์˜ ์—ํฌํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์—ํฌํฌ๋งˆ๋‹ค ์—ญ์ „ํŒŒ๊ฐ€ ์ˆ˜ํ–‰๋œ๋‹ค๊ณ  ๋ณด๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. for epoch in range(10001): optimizer.zero_grad() # forward ์—ฐ์‚ฐ hypothesis = model(X) # ๋น„์šฉ ํ•จ์ˆ˜ cost = criterion(hypothesis, Y) cost.backward() optimizer.step() # 100์˜ ๋ฐฐ์ˆ˜์— ํ•ด๋‹น๋˜๋Š” ์—ํฌํฌ๋งˆ๋‹ค ๋น„์šฉ์„ ์ถœ๋ ฅ if epoch % 100 == 0: print(epoch, cost.item()) ๋น„์šฉ์ด ์ตœ์†Œํ™”๋˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์ด ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” 100๋ฐฐ์ˆ˜์˜ ์—ํฌํฌ๋งˆ๋‹ค ๋น„์šฉ์ด ์ค„์–ด๋“œ๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 0 0.6948983669281006 100 0.693155825138092 200 0.6931535601615906 ... ์ค‘๋žต ... 5400 0.009766248054802418 2. ํ•™์Šต๋œ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ์˜ˆ์ธก๊ฐ’ ํ™•์ธํ•˜๊ธฐ ์ด์ œ ๋ชจ๋ธ์ด XOR ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ด๋ด…์‹œ๋‹ค. with torch.no_grad(): hypothesis = model(X) predicted = (hypothesis > 0.5).float() accuracy = (predicted == Y).float().mean() print('๋ชจ๋ธ์˜ ์ถœ๋ ฅ๊ฐ’(Hypothesis): ', hypothesis.detach().cpu().numpy()) print('๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’(Predicted): ', predicted.detach().cpu().numpy()) print('์‹ค ์ œ๊ฐ’(Y): ', Y.cpu().numpy()) print('์ •ํ™•๋„(Accuracy): ', accuracy.item()) ๋ชจ๋ธ์˜ ์ถœ๋ ฅ๊ฐ’(Hypothesis): [[1.1169249e-04] [9.9982882e-01] [9.9984229e-01] [1.8529959e-04]] ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’(Predicted): [[0.] [1.] [1.] [0.]] ์‹ค์ œ ๊ฐ’(Y): [[0.] [1.] [1.] [0.]] ์ •ํ™•๋„(Accuracy): 1.0 ์‹ค์ œ ๊ฐ’์€ 0, 1, 1, 0์ด๋ฉฐ ์˜ˆ์ธก๊ฐ’์€ 0, 1, 1, 0์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ : http://alexlenail.me/NN-SVG/index.html 06-06 ๋น„์„ ํ˜• ํ™œ์„ฑํ™” ํ•จ์ˆ˜(Activation function) ๋น„์„ ํ˜• ํ™œ์„ฑํ™” ํ•จ์ˆ˜(Activation function)๋Š” ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ˆ˜ํ•™์  ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋‚˜ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ๋Œ€ํ‘œ์ ์ธ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ์€๋‹‰์ธต์—์„œ ์™œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ(sigmoid) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์™œ ์ง€์–‘ํ•ด์•ผ ํ•˜๋Š”์ง€์™€ ์€๋‹‰์ธต์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜์ธ ๋ ๋ฃจ(ReLU) ํ•จ์ˆ˜๋ฅผ ์†Œ๊ฐœํ•˜๊ณ  ๊ทธ ์™ธ์˜ ๋‹ค๋ฅธ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋“ค์— ๋Œ€ํ•ด์„œ๋„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ์ง์ ‘ ๊ทธ๋ฆฌ๋ฉด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์•„๋ž˜์˜ ๋„๊ตฌ๋“ค์„ ๋ชจ๋‘ ์ž„ํฌํŠธ ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. import numpy as np # ๋„˜ํŒŒ์ด ์‚ฌ์šฉ import matplotlib.pyplot as plt # ๋งทํ”Œ๋กฏ๋ฆฝ ์‚ฌ์šฉ 1. ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ํŠน์ง• - ๋น„์„ ํ˜• ํ•จ์ˆ˜(Nonlinear function) ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ํŠน์ง•์€ ์„ ํ˜• ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๋น„์„ ํ˜• ํ•จ์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์„ ํ˜• ํ•จ์ˆ˜๋ž€ ์ถœ๋ ฅ์ด ์ž…๋ ฅ์˜ ์ƒ์ˆ˜๋ฐฐ๋งŒํผ ๋ณ€ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์„ ํ˜•ํ•จ์ˆ˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ( ) W +๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ, W์™€ b๋Š” ์ƒ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ์‹์€ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋ฉด ์ง์„ ์ด ๊ทธ๋ ค์ง‘๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ๋น„์„ ํ˜• ํ•จ์ˆ˜๋Š” ์ง์„  1๊ฐœ๋กœ๋Š” ๊ทธ๋ฆด ์ˆ˜ ์—†๋Š” ํ•จ์ˆ˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋Šฅ๋ ฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ๋Š” ์€๋‹‰์ธต์„ ๊ณ„์†ํ•ด์„œ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์„ ํ˜• ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์€๋‹‰์ธต์„ ์Œ“์„ ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์„ ํ˜• ํ•จ์ˆ˜๋ฅผ ์„ ํƒํ•˜๊ณ , ์ธต์„ ๊ณ„์† ์Œ“๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋Š” ( ) W๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๋‹ค๊ฐ€ ์€๋‹‰์ธต์„ ๋‘ ๊ฐœ ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์ถœ๋ ฅ์ธต์„ ํฌํ•จํ•ด์„œ ( ) f ( ( ( ) ) ) ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ร— ร— ร—์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Š” ์ž˜ ์ƒ๊ฐํ•ด ๋ณด๋ฉด์˜ ์„ธ ์ œ๊ณฑ๊ฐ’์„ ๋ผ๊ณ  ์ •์˜ํ•ด๋ฒ„๋ฆฌ๋ฉด ( ) k ์™€ ๊ฐ™์ด ๋‹ค์‹œ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์„ ํ˜• ํ•จ์ˆ˜๋กœ๋Š” ์€๋‹‰์ธต์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถ”๊ฐ€ํ•˜๋”๋ผ๋„ 1ํšŒ ์ถ”๊ฐ€ํ•œ ๊ฒƒ๊ณผ ์ฐจ์ด๋ฅผ ์ค„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์„ ํ˜• ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์€๋‹‰์ธต์„ 1ํšŒ ์ถ”๊ฐ€ํ•œ ๊ฒƒ๊ณผ ์—ฐ์†์œผ๋กœ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์ด ์ฐจ์ด๊ฐ€ ์—†๋‹ค๋Š” ๋œป์ด์ง€, ์„ ํ˜• ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์ธต์ด ์•„๋ฌด ์˜๋ฏธ๊ฐ€ ์—†๋‹ค๋Š” ๋œป์ด ์•„๋‹™๋‹ˆ๋‹ค. ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜๊ฐ€ ์ƒˆ๋กœ ์ƒ๊ธด๋‹ค๋Š” ์ ์—์„œ ๋ถ„๋ช…ํžˆ ์˜๋ฏธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์„ ํ˜• ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์ธต์„ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์€๋‹‰์ธต๊ณผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์„ ํ˜•์ธต(linear layer)์ด๋‚˜ ํˆฌ์‚ฌ์ธต(projection layer) ๋“ฑ์˜ ๋‹ค๋ฅธ ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ์€๋‹‰์ธต์„ ์„ ํ˜•์ธต๊ณผ ๋Œ€๋น„๋˜๋Š” ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋ฉด ๋น„์„ ํ˜•์ธต(nonlinear layer)์ž…๋‹ˆ๋‹ค. 2. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜(Sigmoid function)์™€ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์–ด๋–ค ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ•™์Šต ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์ˆœ์ „ํŒŒ(forward propagation) ์—ฐ์‚ฐ์„ ํ•˜๊ณ , ๊ทธ๋ฆฌ๊ณ  ์ˆœ์ „ํŒŒ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋‚˜์˜จ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ์†์‹ค ํ•จ์ˆ˜(loss function)์„ ํ†ตํ•ด ๊ณ„์‚ฐํ•˜๊ณ , ๊ทธ๋ฆฌ๊ณ  ์ด ์†์‹ค(loss)์„ ๋ฏธ๋ถ„์„ ํ†ตํ•ด์„œ ๊ธฐ์šธ๊ธฐ(gradient)๋ฅผ ๊ตฌํ•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด ์—ญ์ „ํŒŒ(back propagation)๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ๋ฌธ์ œ์ ์€ ๋ฏธ๋ถ„์„ ํ•ด์„œ ๊ธฐ์šธ๊ธฐ(gradient)๋ฅผ ๊ตฌํ•  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. # ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ์ฝ”๋“œ def sigmoid(x): return 1/(1+np.exp(-x)) x = np.arange(-5.0, 5.0, 0.1) y = sigmoid(x) plt.plot(x, y) plt.plot([0,0],[1.0,0.0], ':') # ๊ฐ€์šด๋ฐ ์ ์„  ์ถ”๊ฐ€ plt.title('Sigmoid Function') plt.show() ์œ„์˜ ๊ทธ๋ž˜ํ”„๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ž˜ํ”„๋ฅผ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์ด 0 ๋˜๋Š” 1์— ๊ฐ€๊นŒ์›Œ์ง€๋ฉด, ๊ทธ๋ž˜ํ”„์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์™„๋งŒํ•ด์ง€๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ๊ฐ€ ์™„๋งŒํ•ด์ง€๋Š” ๊ตฌ๊ฐ„์„ ์ฃผํ™ฉ์ƒ‰, ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ตฌ๊ฐ„์„ ์ดˆ๋ก์ƒ‰์œผ๋กœ ์น ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผํ™ฉ์ƒ‰ ๋ถ€๋ถ„์€ ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด 0์— ๊ฐ€๊นŒ์šด ์•„์ฃผ ์ž‘์€ ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ 0์— ๊ฐ€๊นŒ์šด ์•„์ฃผ ์ž‘์€ ๊ธฐ์šธ๊ธฐ๊ฐ€ ๊ณฑํ•ด์ง€๊ฒŒ ๋˜๋ฉด, ์•ž๋‹จ์—๋Š” ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ž˜ ์ „๋‹ฌ๋˜์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ˜„์ƒ์„ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค(Vanishing Gradient) ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์€๋‹‰์ธต์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค์ˆ˜๊ฐ€ ๋  ๊ฒฝ์šฐ์—๋Š” 0์— ๊ฐ€๊นŒ์šด ๊ธฐ์šธ๊ธฐ๊ฐ€ ๊ณ„์† ๊ณฑํ•ด์ง€๋ฉด ์•ž๋‹จ์—์„œ๋Š” ๊ฑฐ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ์ „ํŒŒ ๋ฐ›์„ ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š์•„ ํ•™์Šต์ด ๋˜์ง€๋ฅผ ์•Š์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์€๋‹‰์ธต์ด ๊นŠ์€ ์‹ ๊ฒฝ๋ง์—์„œ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋ฌธ์ œ๋กœ ์ธํ•ด ์ถœ๋ ฅ์ธต๊ณผ ๊ฐ€๊นŒ์šด ์€๋‹‰์ธต์—์„œ๋Š” ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ž˜ ์ „ํŒŒ๋˜์ง€๋งŒ, ์•ž๋‹จ์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ œ๋Œ€๋กœ ์ „ํŒŒ๋˜์ง€ ์•Š๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์€๋‹‰์ธต์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ง€์–‘๋ฉ๋‹ˆ๋‹ค. 3. ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜(Hyperbolic tangent function) ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜(tanh)๋Š” ์ž…๋ ฅ๊ฐ’์„ -1๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. x = np.arange(-5.0, 5.0, 0.1) # -5.0๋ถ€ํ„ฐ 5.0๊นŒ์ง€ 0.1 ๊ฐ„๊ฒฉ ์ƒ์„ฑ y = np.tanh(x) plt.plot(x, y) plt.plot([0,0],[1.0, -1.0], ':') plt.axhline(y=0, color='orange', linestyle='--') plt.title('Tanh Function') plt.show() ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋„ -1๊ณผ 1์— ๊ฐ€๊นŒ์šด ์ถœ๋ ฅ๊ฐ’์„ ์ถœ๋ ฅํ•  ๋•Œ, ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์™€ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ์—๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์™€๋Š” ๋‹ฌ๋ฆฌ 0์„ ์ค‘์‹ฌ์œผ๋กœ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์ด ๋•Œ๋ฌธ์— ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์™€ ๋น„๊ตํ•˜๋ฉด ๋ฐ˜ํ™˜๊ฐ’์˜ ๋ณ€ํ™” ํญ์ด ๋” ํฝ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ณด๋‹ค๋Š” ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ์ฆ์ƒ์ด ์ ์€ ํŽธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์€๋‹‰์ธต์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ณด๋‹ค๋Š” ๋งŽ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 4. ๋ ๋ฃจ ํ•จ์ˆ˜(ReLU) ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ ๊ฐ€์žฅ ์ตœ๊ณ ์˜ ์ธ๊ธฐ๋ฅผ ์–ป๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ˆ˜์‹์€ ( ) m x ( , ) ๋กœ ์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. def relu(x): return np.maximum(0, x) x = np.arange(-5.0, 5.0, 0.1) y = relu(x) plt.plot(x, y) plt.plot([0,0],[5.0,0.0], ':') plt.title('Relu Function') plt.show() ๋ ๋ฃจ ํ•จ์ˆ˜๋Š” ์Œ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 0์„ ์ถœ๋ ฅํ•˜๊ณ , ์–‘์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ž…๋ ฅ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ ๋ฃจ ํ•จ์ˆ˜๋Š” ํŠน์ • ์–‘์ˆ˜ ๊ฐ’์— ์ˆ˜๋ ดํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊นŠ์€ ์‹ ๊ฒฝ๋ง์—์„œ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ณด๋‹ค ํ›จ์”ฌ ๋” ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๋ ๋ฃจ ํ•จ์ˆ˜๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์™€ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜์™€ ๊ฐ™์ด ์–ด๋–ค ์—ฐ์‚ฐ์ด ํ•„์š”ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹จ์ˆœ ์ž„๊ณ„๊ฐ’์ด๋ฏ€๋กœ ์—ฐ์‚ฐ ์†๋„๋„ ๋น ๋ฆ…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ ๋ฌธ์ œ์ ์ด ์กด์žฌํ•˜๋Š”๋ฐ, ์ž…๋ ฅ๊ฐ’์ด ์Œ์ˆ˜ ๋ฉด ๊ธฐ์šธ๊ธฐ๋„ 0์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‰ด๋Ÿฐ์€ ๋‹ค์‹œ ํšŒ์ƒํ•˜๋Š” ๊ฒƒ์ด ๋งค์šฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ์ฃฝ์€ ๋ ๋ฃจ(dying ReLU)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 5. ๋ฆฌํ‚ค ๋ ๋ฃจ(Leaky ReLU) ์ฃฝ์€ ๋ ๋ฃจ๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ReLU์˜ ๋ณ€ํ˜• ํ•จ์ˆ˜๋“ค์ด ๋“ฑ์žฅํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ณ€ํ˜• ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” Leaky ReLU์— ๋Œ€ํ•ด์„œ๋งŒ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. Leaky ReLU๋Š” ์ž…๋ ฅ๊ฐ’์ด ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ์— 0์ด ์•„๋‹ˆ๋ผ 0.001๊ณผ ๊ฐ™์€ ๋งค์šฐ ์ž‘์€ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜์‹์€ ( ) m x ( x x ) ๋กœ ์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. a๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ Leaky('์ƒˆ๋Š”') ์ •๋„๋ฅผ ๊ฒฐ์ •ํ•˜๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” 0.01์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” '์ƒˆ๋Š” ์ •๋„'๋ผ๋Š” ๊ฒƒ์€ ์ž…๋ ฅ๊ฐ’์˜ ์Œ์ˆ˜์ผ ๋•Œ์˜ ๊ธฐ์šธ๊ธฐ๋ฅผ ๋น„์œ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. a = 0.1 def leaky_relu(x): return np.maximum(a*x, x) x = np.arange(-5.0, 5.0, 0.1) y = leaky_relu(x) plt.plot(x, y) plt.plot([0,0],[5.0,0.0], ':') plt.title('Leaky ReLU Function') plt.show() ์œ„์˜ ๊ทธ๋ž˜ํ”„์—์„œ๋Š” ์ƒˆ๋Š” ๋ชจ์Šต์„ ํ™•์‹คํžˆ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด a๋ฅผ 0.1๋กœ ์žก์•˜์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅ๊ฐ’์ด ์Œ์ˆ˜๋ผ๋„ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์ด ๋˜์ง€ ์•Š์œผ๋ฉด ReLU๋Š” ์ฃฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 6. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜(Softamx function) ์€๋‹‰์ธต์—์„œ ReLU(๋˜๋Š” ReLU ๋ณ€ํ˜•) ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด์ง€๋งŒ ๊ทธ๋ ‡๋‹ค๊ณ  ํ•ด์„œ ์•ž์„œ ๋ฐฐ์šด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋‚˜ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์™€ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์ถœ๋ ฅ์ธต์— ์ ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. x = np.arange(-5.0, 5.0, 0.1) # -5.0๋ถ€ํ„ฐ 5.0๊นŒ์ง€ 0.1 ๊ฐ„๊ฒฉ ์ƒ์„ฑ y = np.exp(x) / np.sum(np.exp(x)) plt.plot(x, y) plt.title('Softmax Function') plt.show() ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š”๋ฐ, ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๊ฐ€ ๋‘ ๊ฐ€์ง€ ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด๋Š” ์ด์ง„ ๋ถ„๋ฅ˜ (Binary Classification) ๋ฌธ์ œ์— ์‚ฌ์šฉ๋œ๋‹ค๋ฉด ์„ธ ๊ฐ€์ง€ ์ด์ƒ์˜ (์ƒํ˜ธ ๋ฐฐํƒ€์ ์ธ) ์„ ํƒ์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(MultiClass Classification) ๋ฌธ์ œ์— ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 7. ์ถœ๋ ฅ์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์™€ ์˜ค์ฐจ ํ•จ์ˆ˜์˜ ๊ด€๊ณ„ ์€๋‹‰์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ReLU ๋˜๋Š” Leaky ReLU์™€ ๊ฐ™์€ ReLU์˜ ๋ณ€ํ˜•์„ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ์ •๋ฆฌํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ถœ๋ ฅ์ธต์€ ์–ด๋–จ๊นŒ์š”? ๊ฐ ๋ฌธ์ œ์— ๋”ฐ๋ฅธ ์ถœ๋ ฅ์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์™€ ๋น„์šฉ ํ•จ์ˆ˜์˜ ๊ด€๊ณ„๋ฅผ ์ •๋ฆฌํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ์™ธ์—๋„ ๋‹ค์ค‘ ๋ ˆ์ด๋ธ” ๋ถ„๋ฅ˜๋„ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ๋น„์šฉ ํ•จ์ˆ˜ ์ด์ง„ ๋ถ„๋ฅ˜ ์‹œ๊ทธ๋ชจ์ด๋“œ nn.BCELoss() ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ์†Œํ”„ํŠธ๋งฅ์Šค nn.CrossEntropyLoss() ํšŒ๊ท€ ์—†์Œ MSE ์ฃผ์˜ํ•  ์ ์€ nn.CrossEntropyLoss()๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ด๋ฏธ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  ์ž๋ฃŒ : https://excelsior-cjh.tistory.com/177 ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ์ ์€ ์›์  ์ค‘์‹ฌ์ด ์•„๋‹ˆ๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค(Not zero-centered). ๋”ฐ๋ผ์„œ, ํ‰๊ท ์ด 0์ด ์•„๋‹ˆ๋ผ 0.5์ด๋ฉฐ, ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ ์–‘์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅ์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ž…๋ ฅ์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ๋ณด๋‹ค ์ปค์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ํŽธํ–ฅ ์ด๋™(bias shift)์ด๋ผ ํ•˜๋ฉฐ, ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ๊ฐ ๋ ˆ์ด์–ด๋ฅผ ์ง€๋‚  ๋•Œ๋งˆ๋‹ค ๋ถ„์‚ฐ์ด ๊ณ„์† ์ปค์ ธ ๊ฐ€์žฅ ๋†’์€ ๋ ˆ์ด์–ด์—์„œ๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ์ด 0์ด๋‚˜ 1๋กœ ์ˆ˜๋ ดํ•˜๊ฒŒ ๋˜์–ด ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋ฌธ์ œ๊ฐ€ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋Š” ์›์  ์ค‘์‹ฌ(zero-centered)์ด๊ธฐ ๋•Œ๋ฌธ์—, ์‹œ๊ทธ๋ชจ์ด๋“œ์™€ ๋‹ฌ๋ฆฌ ํŽธํ–ฅ<NAME> ์ผ์–ด๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜ ๋˜ํ•œ ์ž…๋ ฅ์˜ ์ ˆ๋Œ“๊ฐ’์ด ํด ๊ฒฝ์šฐ -1์ด๋‚˜ 1๋กœ ์ˆ˜๋ ดํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๋•Œ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์™„๋งŒํ•ด์ง€๋ฏ€๋กœ ์—ญ์‹œ๋‚˜ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋ฌธ์ œ๊ฐ€ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™๊ต์˜ ๋”ฅ ๋Ÿฌ๋‹ ๊ฐ•์˜ cs231n์—์„œ๋Š” ReLU๋ฅผ ๋จผ์ € ์‹œ๋„ํ•ด ๋ณด๊ณ , ๊ทธ๋‹ค์Œ์œผ๋กœ LeakyReLU๋‚˜ ELU ๊ฐ™์€ ReLU์˜ ๋ณ€ํ˜•๋“ค์„ ์‹œ๋„ํ•ด ๋ณด๋ฉฐ, sigmoid๋Š” ์‚ฌ์šฉํ•˜์ง€ ๋ง๋ผ๊ณ  ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. 06-07 ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ ์†๊ธ€์”จ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ๊ตฌํ˜„ํ•˜๊ณ , ๋”ฅ ๋Ÿฌ๋‹์„ ํ†ตํ•ด์„œ ์ˆซ์ž ํ•„๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฅ˜ํ•ด ๋ด…์‹œ๋‹ค. MNIST ๋ฐ์ดํ„ฐ๋ž‘ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. 1. ์ˆซ์ž ํ•„๊ธฐ ๋ฐ์ดํ„ฐ ์†Œ๊ฐœ ์ˆซ์ž ํ•„๊ธฐ ๋ฐ์ดํ„ฐ๋Š” ์‚ฌ์ดํ‚ท๋Ÿฐ ํŒจํ‚ค์ง€์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ถ„๋ฅ˜์šฉ ์˜ˆ์ œ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์†์œผ๋กœ ์“ด ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋กœ load_digits() ๋ช…๋ น์œผ๋กœ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์ด๋ฏธ์ง€๋Š” 0๋ถ€ํ„ฐ 15๊นŒ์ง€์˜ ๋ช…์•”์„ ๊ฐ€์ง€๋Š” 8 ร— 8 = 64 ํ”ฝ์…€ ํ•ด์ƒ๋„์˜ ํ‘๋ฐฑ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ์ด๋ฏธ์ง€๊ฐ€ 1,797๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. load_digits()๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ๋“œํ•œ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ digits์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. %matplotlib inline import matplotlib.pyplot as plt # ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•œ ๋งทํ”Œ๋กฏ๋ฆฝ from sklearn.datasets import load_digits digits = load_digits() # 1,979๊ฐœ์˜ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ๋กœ๋“œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. .images[์ธ๋ฑ์Šค]๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์ด๋ฏธ์ง€๋ฅผ ํ–‰๋ ฌ๋กœ์„œ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. print(digits.images[0]) [[ 0. 0. 5. 13. 9. 1. 0. 0.] [ 0. 0. 13. 15. 10. 15. 5. 0.] [ 0. 3. 15. 2. 0. 11. 8. 0.] [ 0. 4. 12. 0. 0. 8. 8. 0.] [ 0. 5. 8. 0. 0. 9. 8. 0.] [ 0. 4. 11. 0. 1. 12. 7. 0.] [ 0. 2. 14. 5. 10. 12. 0. 0.] [ 0. 0. 6. 13. 10. 0. 0. 0.]] ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์ด 8 ร— 8 ํ–‰๋ ฌ๋กœ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 0์„ ํฐ์ƒ‰ ๋„ํ™”์ง€, 0๋ณด๋‹ค ํฐ ์ˆซ์ž๋“ค์„ ๊ฒ€์€์ƒ‰ ์ ์ด๋ผ๊ณ  ์ƒ์ƒํ•ด ๋ณด๋ฉด ์ˆซ์ž 0์˜ ์‹ค๋ฃจ์—ฃ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋ ˆ์ด๋ธ”๋„ ์ˆซ์ž 0์ธ์ง€ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ”์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(digits.target[0]) ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ”์€ 0์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ƒ˜ํ”Œ์ด ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('์ „์ฒด ์ƒ˜ํ”Œ์˜ ์ˆ˜ : {}'.format(len(digits.images))) ์ „์ฒด ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 1797 ์ „์ฒด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋Š” 1,797๊ฐœ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘์—์„œ ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์‹œ๊ฐํ™”ํ•ด๋ด…์‹œ๋‹ค. images_and_labels = list(zip(digits.images, digits.target)) for index, (image, label) in enumerate(images_and_labels[:5]): # 5๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅ plt.subplot(2, 5, index + 1) plt.axis('off') plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest') plt.title('sample: %i' % label) ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์‹œ๊ฐํ™”ํ•ด๋ดค๋Š”๋ฐ, ์ˆœ์„œ๋Œ€๋กœ ์ˆซ์ž 0, 1, 2, 3, 4์˜ ์†๊ธ€์”จ์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ”์„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. for i in range(5): print(i,'๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : ',digits.target[i]) 0 ๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : 0 1 ๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : 1 2 ๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : 2 3 ๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : 3 4 ๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์˜ ๋ ˆ์ด๋ธ” : 4 ์ด์ œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ”์„ ๊ฐ๊ฐ X, Y์— ์ €์žฅํ•ด ๋ด…์‹œ๋‹ค. digits.images๋Š” ๋ชจ๋“  ์ƒ˜ํ”Œ์„ 8 ร— 8 ํ–‰๋ ฌ๋กœ ์ €์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์€ digts.data๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” 8 ร— 8 ํ–‰๋ ฌ์„ ์ „๋ถ€ 64์ฐจ์›์˜ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ €์žฅํ•œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. digits.data๋ฅผ ์ด์šฉํ•ด์„œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(digits.data[0]) [ 0. 0. 5. 13. 9. 1. 0. 0. 0. 0. 13. 15. 10. 15. 5. 0. 0. 3. 15. 2. 0. 11. 8. 0. 0. 4. 12. 0. 0. 8. 8. 0. 0. 5. 8. 0. 0. 9. 8. 0. 0. 4. 11. 0. 1. 12. 7. 0. 0. 2. 14. 5. 10. 12. 0. 0. 0. 0. 6. 13. 10. 0. 0. 0.] 8 ร— 8 ํ–‰๋ ฌ์ด ์•„๋‹ˆ๋ผ 64์ฐจ์›์˜ ๋ฒกํ„ฐ๋กœ ์ €์žฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ X๋กœ ์ €์žฅํ•˜๊ณ , ๋ ˆ์ด๋ธ”์„ Y์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. X = digits.data # ์ด๋ฏธ์ง€. ์ฆ‰, ํŠน์„ฑ ํ–‰๋ ฌ Y = digits.target # ๊ฐ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ” 2. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก  ๋ถ„๋ฅ˜๊ธฐ ๋งŒ๋“ค๊ธฐ import torch import torch.nn as nn from torch import optim model = nn.Sequential( nn.Linear(64, 32), # input_layer = 64, hidden_layer1 = 32 nn.ReLU(), nn.Linear(32, 16), # hidden_layer2 = 32, hidden_layer3 = 16 nn.ReLU(), nn.Linear(16, 10) # hidden_layer3 = 16, output_layer = 10 ) X = torch.tensor(X, dtype=torch.float32) Y = torch.tensor(Y, dtype=torch.int64) loss_fn = nn.CrossEntropyLoss() # ์ด ๋น„์šฉ ํ•จ์ˆ˜๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Œ. optimizer = optim.Adam(model.parameters()) losses = [] for epoch in range(100): optimizer.zero_grad() y_pred = model(X) # forwar ์—ฐ์‚ฐ loss = loss_fn(y_pred, Y) loss.backward() optimizer.step() if epoch % 10 == 0: print('Epoch {:4d}/{} Cost: {:.6f}'.format( epoch, 100, loss.item() )) losses.append(loss.item()) Epoch 0/100 Cost: 2.380815 Epoch 10/100 Cost: 2.059323 ... ์ค‘๋žต ... Epoch 90/100 Cost: 0.205398 plt.plot(losses) 06-08 ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ MNIST ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์•ž์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋กœ MNIST ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ์‹ค์Šต์„ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€ ๋˜ํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต๋งŒ ์กด์žฌํ•˜๋ฏ€๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•œ '๋‹จ์ธต ํผ์…‰ํŠธ๋ก '์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์€๋‹‰์ธต์„ ์ถ”๊ฐ€๋กœ ๋„ฃ์–ด ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ๊ตฌํ˜„ํ•˜๊ณ , ๋”ฅ ๋Ÿฌ๋‹์„ ํ†ตํ•ด์„œ MNIST ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฅ˜ํ•ด ๋ด…์‹œ๋‹ค. MNIST ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์„ค๋ช… : https://wikidocs.net/60324 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œํ•˜๊ธฐ import numpy as np import matplotlib.pyplot as plt % matplotlib inline from sklearn.datasets import fetch_openml mnist = fetch_openml('mnist_784', version=1, cache=True, as_frame=False) mnist.data[0] array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 3., 18., 18., 18., 126., 136., 175., 26., 166., 255., 247., 127., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 30., 36., 94., 154., 170., 253., 253., 253., 253., 253., 225., 172., 253., 242., 195., 64., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 49., 238., 253., 253., 253., 253., 253., 253., 253., 253., 251., 93., 82., 82., 56., 39., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 18., 219., 253., 253., 253., 253., 253., 198., 182., 247., 241., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 80., 156., 107., 253., 253., 205., 11., 0., 43., 154., 0., 0., 0., 0., 0., ... ์ค‘๋žต ... 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) mnist.target[0] '5' mnist.target = mnist.target.astype(np.int8) X = mnist.data / 255 # 0-255๊ฐ’์„ [0,1] ๊ตฌ๊ฐ„์œผ๋กœ ์ •๊ทœํ™” y = mnist.target X[0] array([0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , ... ์ค‘๋žต ... 0. , 0. , 0.01176471, 0.07058824, 0.07058824, 0.07058824, 0.49411765, 0.53333333, 0.68627451, 0.10196078, 0.65098039, 1. , 0.96862745, 0.49803922, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.11764706, 0.14117647, 0.36862745, 0.60392157, 0.66666667, 0.99215686, 0.99215686, 0.99215686, 0.99215686, 0.99215686, 0.88235294, 0.6745098 , 0.99215686, 0.94901961, 0.76470588, 0.25098039, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.19215686, 0.93333333, 0.99215686, 0.99215686, 0.99215686, 0.99215686, 0.99215686, 0.99215686, 0.99215686, 0.99215686, 0.98431373, 0.36470588, 0.32156863, 0.32156863, 0.21960784, 0.15294118, 0. , ... ์ค‘๋žต ... 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]) y[0] plt.imshow(X[0].reshape(28, 28), cmap='gray') print("์ด ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ”์€ {:.0f}์ด๋‹ค".format(y[0])) 2. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋ถ„๋ฆฌ import torch from torch.utils.data import TensorDataset, DataLoader from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/7, random_state=0) X_train = torch.Tensor(X_train) X_test = torch.Tensor(X_test) y_train = torch.LongTensor(y_train) y_test = torch.LongTensor(y_test) ds_train = TensorDataset(X_train, y_train) ds_test = TensorDataset(X_test, y_test) loader_train = DataLoader(ds_train, batch_size=64, shuffle=True) loader_test = DataLoader(ds_test, batch_size=64, shuffle=False) 3. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก  from torch import nn model = nn.Sequential() model.add_module('fc1', nn.Linear(28*28*1, 100)) model.add_module('relu1', nn.ReLU()) model.add_module('fc2', nn.Linear(100, 100)) model.add_module('relu2', nn.ReLU()) model.add_module('fc3', nn.Linear(100, 10)) print(model) Sequential( (fc1): Linear(in_features=784, out_features=100, bias=True) (relu1): ReLU() (fc2): Linear(in_features=100, out_features=100, bias=True) (relu2): ReLU() (fc3): Linear(in_features=100, out_features=10, bias=True) ) from torch import optim # ์˜ค์ฐจ ํ•จ์ˆ˜ ์„ ํƒ loss_fn = nn.CrossEntropyLoss() # ๊ฐ€์ค‘์น˜๋ฅผ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์ ํ™” ๊ธฐ๋ฒ• ์„ ํƒ optimizer = optim.Adam(model.parameters(), lr=0.01) def train(epoch): model.train() # ์‹ ๊ฒฝ๋ง์„ ํ•™์Šต ๋ชจ๋“œ๋กœ ์ „ํ™˜ # ๋ฐ์ดํ„ฐ ๋กœ๋”์—์„œ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด ํ•™์Šต์„ ์ˆ˜ํ–‰ for data, targets in loader_train: optimizer.zero_grad() # ๊ฒฝ์‚ฌ๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” outputs = model(data) # ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ถœ๋ ฅ์„ ๊ณ„์‚ฐ loss = loss_fn(outputs, targets) # ์ถœ๋ ฅ๊ณผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์ •๋‹ต ๊ฐ„์˜ ์˜ค์ฐจ๋ฅผ ๊ณ„์‚ฐ loss.backward() # ์˜ค์ฐจ๋ฅผ ์—ญ์ „ํŒŒ ๊ณ„์‚ฐ optimizer.step() # ์—ญ์ „ํŒŒ ๊ณ„์‚ฐํ•œ ๊ฐ’์œผ๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ์ˆ˜์ • print("epoch{}์™„๋ฃŒ\n".format(epoch)) def test(): model.eval() # ์‹ ๊ฒฝ๋ง์„ ์ถ”๋ก  ๋ชจ๋“œ๋กœ ์ „ํ™˜ correct = 0 # ๋ฐ์ดํ„ฐ ๋กœ๋”์—์„œ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด ์ถ”๋ก ์„ ์ˆ˜ํ–‰ with torch.no_grad(): # ์ถ”๋ก  ๊ณผ์ •์—๋Š” ๋ฏธ๋ถ„์ด ํ•„์š” ์—†์Œ for data, targets in loader_test: outputs = model(data) # ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ถœ๋ ฅ์„ ๊ณ„์‚ฐ # ์ถ”๋ก  ๊ณ„์‚ฐ _, predicted = torch.max(outputs.data, 1) # ํ™•๋ฅ ์ด ๊ฐ€์žฅ ๋†’์€ ๋ ˆ์ด๋ธ”์ด ๋ฌด์—‡์ธ์ง€ ๊ณ„์‚ฐ correct += predicted.eq(targets.data.view_as(predicted)).sum() # ์ •๋‹ต๊ณผ ์ผ์น˜ํ•œ ๊ฒฝ์šฐ ์ •๋‹ต ์นด์šดํŠธ๋ฅผ ์ฆ๊ฐ€ # ์ •ํ™•๋„ ์ถœ๋ ฅ data_num = len(loader_test.dataset) # ๋ฐ์ดํ„ฐ ์ด๊ฑด์ˆ˜ print(' ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ์˜ˆ์ธก ์ •ํ™•๋„: {}/{} ({:.0f}%)\n'.format(correct, data_num, 100. * correct / data_num)) test() ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ์˜ˆ์ธก ์ •ํ™•๋„: 862/10000 (9%) for epoch in range(3): train(epoch) test() epoch0์™„๋ฃŒ epoch1์™„๋ฃŒ epoch2์™„๋ฃŒ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ์˜ˆ์ธก ์ •ํ™•๋„: 9570/10000 (96%) index = 2018 model.eval() # ์‹ ๊ฒฝ๋ง์„ ์ถ”๋ก  ๋ชจ๋“œ๋กœ ์ „ํ™˜ data = X_test[index] output = model(data) # ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ถœ๋ ฅ์„ ๊ณ„์‚ฐ _, predicted = torch.max(output.data, 0) # ํ™•๋ฅ ์ด ๊ฐ€์žฅ ๋†’์€ ๋ ˆ์ด๋ธ”์ด ๋ฌด์—‡์ธ์ง€ ๊ณ„์‚ฐ print("์˜ˆ์ธก ๊ฒฐ๊ณผ : {}".format(predicted)) X_test_show = (X_test[index]).numpy() plt.imshow(X_test_show.reshape(28, 28), cmap='gray') print("์ด ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ์˜ ์ •๋‹ต ๋ ˆ์ด๋ธ”์€ {:.0f}์ž…๋‹ˆ๋‹ค".format(y_test[index])) 06-09 ๊ณผ ์ ํ•ฉ(Overfitting)์„ ๋ง‰๋Š” ๋ฐฉ๋ฒ•๋“ค ํ•™์Šต ๋ฐ์ดํ„ฐ์— ๋ชจ๋ธ์ด ๊ณผ์ ํ•ฉ๋˜๋Š” ํ˜„์ƒ์€ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋–จ์–ดํŠธ๋ฆฌ๋Š” ์ฃผ์š” ์ด์Šˆ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ๊ณผ์ ํ•ฉ๋˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„๋Š” ๋†’์„์ง€๋ผ๋„, ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ. ์ฆ‰, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋‚˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ์ด ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆํ•„์š”ํ•  ์ •๋„๋กœ ๊ณผํ•˜๊ฒŒ ์•”๊ธฐํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ํฌํ•จ๋œ ๋…ธ์ด์ฆˆ๊นŒ์ง€ ํ•™์Šตํ•œ ์ƒํƒœ๋ผ๊ณ  ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋ชจ๋ธ์˜ ๊ณผ์ ํ•ฉ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋…ผ์˜ํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ์ด ์ฑ…์€ ๋”ฅ ๋Ÿฌ๋‹์„ ๋‹ค๋ฃจ๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ณผ์ ํ•ฉ์„ ๋ง‰๋Š” ๋ฐฉ๋ฒ•์— ์ดˆ์ ์„ ๋‘ก๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๋Š˜๋ฆฌ๊ธฐ ๋ชจ๋ธ์€ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ ์„ ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ฐ์ดํ„ฐ์˜ ํŠน์ • ํŒจํ„ด์ด๋‚˜ ๋…ธ์ด์ฆˆ๊นŒ์ง€ ์‰ฝ๊ฒŒ ์•”๊ธฐํ•˜๊ธฐ ๋˜๋ฏ€๋กœ ๊ณผ์ ํ•ฉ ํ˜„์ƒ์ด ๋ฐœ์ƒํ•  ํ™•๋ฅ ์ด ๋Š˜์–ด๋‚ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๋Š˜๋ฆด์ˆ˜๋ก ๋ชจ๋ธ์€ ๋ฐ์ดํ„ฐ์˜ ์ผ๋ฐ˜์ ์ธ ํŒจํ„ด์„ ํ•™์Šตํ•˜์—ฌ ๊ณผ์ ํ•ฉ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ์ ์„ ๊ฒฝ์šฐ์—๋Š” ์˜๋„์ ์œผ๋กœ ๊ธฐ์กด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ๊ธˆ์”ฉ ๋ณ€ํ˜•ํ•˜๊ณ  ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ๋Š˜๋ฆฌ๊ธฐ๋„ ํ•˜๋Š”๋ฐ ์ด๋ฅผ ๋ฐ์ดํ„ฐ ์ฆ์‹ ๋˜๋Š” ์ฆ๊ฐ•(Data Augmentation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฐ์ดํ„ฐ ์ฆ์‹์ด ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ ์ด๋ฏธ์ง€๋ฅผ ๋Œ๋ฆฌ๊ฑฐ๋‚˜ ๋…ธ์ด์ฆˆ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์ผ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•˜๋Š” ๋“ฑ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ์‹์‹œํ‚ต๋‹ˆ๋‹ค. 2. ๋ชจ๋ธ์˜ ๋ณต์žก๋„ ์ค„์ด๊ธฐ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋ณต์žก๋„๋Š” ์€๋‹‰์ธต(hidden layer)์˜ ์ˆ˜๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜ ๋“ฑ์œผ๋กœ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ๊ณผ์ ํ•ฉ ํ˜„์ƒ์ด ํฌ์ฐฉ๋˜์—ˆ์„ ๋•Œ, ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ ๊ฐ€์ง€ ์กฐ์น˜๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋ณต์žก๋„๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. class Architecture1(nn.Module): def __init__(self, input_size, hidden_size, num_classes): super(Architecture1, self).__init__() self.fc1 = nn.Linear(input_size, hidden_size) self.relu = nn.ReLU() self.fc2 = nn.Linear(hidden_size, hidden_size) self.relu = nnReLU() self.fc3 = nn.Linear(hidden_size, num_classes) def forward(self, x): out = self.fc1(x) out = self.relu(out) out = self.fc2(out) out = self.relu(out) out = self.fc3(out) return out ์œ„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ 3๊ฐœ์˜ ์„ ํ˜• ๋ ˆ์ด์–ด(Linear)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์— ๊ณผ์ ํ•ฉ ํ˜„์ƒ์„ ๋ณด์ธ๋‹ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๋ณต์žก๋„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. class Architecture1(nn.Module): def __init__(self, input_size, hidden_size, num_classes): super(Architecture1, self).__init__() self.fc1 = nn.Linear(input_size, hidden_size) self.relu = nn.ReLU() self.fc2 = nn.Linear(hidden_size, num_classes) def forward(self, x): out = self.fc1(x) out = self.relu(out) out = self.fc2(out) return out ์œ„ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์€ 2๊ฐœ์˜ ์„ ํ˜• ๋ ˆ์ด์–ด(Linear)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ๋Š” ๋ชจ๋ธ์— ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์˜ ์ˆ˜๋ฅผ ๋ชจ๋ธ์˜ ์ˆ˜์šฉ๋ ฅ(capacity)์ด๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 3. ๊ฐ€์ค‘์น˜ ๊ทœ์ œ(Regularization) ์ ์šฉํ•˜๊ธฐ ๋ณต์žกํ•œ ๋ชจ๋ธ์ด ๊ฐ„๋‹จํ•œ ๋ชจ๋ธ๋ณด๋‹ค ๊ณผ์ ํ•ฉ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ„๋‹จํ•œ ๋ชจ๋ธ์€ ์ ์€ ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋ณต์žกํ•œ ๋ชจ๋ธ์„ ์ข€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ€์ค‘์น˜ ๊ทœ์ œ(Regularizaiton)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. L1 ๊ทœ์ œ : ๊ฐ€์ค‘์น˜ w๋“ค์˜ ์ ˆ๋Œ“๊ฐ’ ํ•ฉ๊ณ„๋ฅผ ๋น„์šฉ ํ•จ์ˆ˜์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. L1 ๋…ธ๋ฆ„์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. L2 ๊ทœ์ œ : ๋ชจ๋“  ๊ฐ€์ค‘์น˜ w๋“ค์˜ ์ œ๊ณฑํ•ฉ์„ ๋น„์šฉ ํ•จ์ˆ˜์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. L2 ๋…ธ๋ฆ„์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. L1 ๊ทœ์ œ๋Š” ๊ธฐ์กด์˜ ๋น„์šฉ ํ•จ์ˆ˜์— ๋ชจ๋“  ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ โˆฃ โˆฃ ๋ฅผ ๋” ํ•œ ๊ฐ’์„ ๋น„์šฉ ํ•จ์ˆ˜๋กœ ํ•˜๊ณ , L2 ๊ทœ์ œ๋Š” ๊ธฐ์กด์˜ ๋น„์šฉ ํ•จ์ˆ˜์— ๋ชจ๋“  ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ 2 w๋ฅผ ๋” ํ•œ ๊ฐ’์„ ๋น„์šฉ ํ•จ์ˆ˜๋กœ ํ•ฉ๋‹ˆ๋‹ค.๋Š” ๊ทœ์ œ์˜ ๊ฐ•๋„๋ฅผ ์ •ํ•˜๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. ๊ฐ€ ํฌ๋‹ค๋ฉด ๋ชจ๋ธ์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ ํ•ฉํ•œ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ๋ณด๋‹ค ๊ทœ์ œ๋ฅผ ์œ„ํ•ด ์ถ”๊ฐ€๋œ ํ•ญ๋“ค์„ ์ž‘๊ฒŒ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์„ ์šฐ์„ ํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ์‹ ๋ชจ๋‘ ๋น„์šฉ ํ•จ์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ€์ค‘์น˜ w๋“ค์˜ ๊ฐ’์ด ์ž‘์•„์ ธ์•ผ ํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. L1 ๊ทœ์ œ๋กœ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. L1 ๊ทœ์ œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋น„์šฉ ํ•จ์ˆ˜๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๊ฒŒ ํ•˜๋Š” ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์ฐพ๋Š” ๋™์‹œ์— ๊ฐ€์ค‘์น˜๋“ค์˜ ์ ˆ๋Œ“๊ฐ’์˜ ํ•ฉ๋„ ์ตœ์†Œ๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด, ๊ฐ€์ค‘์น˜ w์˜ ๊ฐ’๋“ค์€ 0 ๋˜๋Š” 0์— ๊ฐ€๊นŒ์ด ์ž‘์•„์ ธ์•ผ ํ•˜๋ฏ€๋กœ ์–ด๋–ค ํŠน์„ฑ๋“ค์€ ๋ชจ๋ธ์„ ๋งŒ๋“ค ๋•Œ ๊ฑฐ์˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ( ) w x + 2 2 w x + 4 4 ๋ผ๋Š” ์ˆ˜์‹์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์— L1 ๊ทœ์ œ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋”๋‹ˆ, 3 ์˜ ๊ฐ’์ด 0์ด ๋˜์—ˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋Š” 3 ํŠน์„ฑ์€ ์‚ฌ์‹ค ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ์— ๋ณ„ ์˜ํ–ฅ์„ ์ฃผ์ง€ ๋ชปํ•˜๋Š” ํŠน์„ฑ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. L2 ๊ทœ์ œ๋Š” L1 ๊ทœ์ œ์™€๋Š” ๋‹ฌ๋ฆฌ ๊ฐ€์ค‘์น˜๋“ค์˜ ์ œ๊ณฑ์„ ์ตœ์†Œํ™”ํ•˜๋ฏ€๋กœ w์˜ ๊ฐ’์ด ์™„์ „ํžˆ 0์ด ๋˜๊ธฐ๋ณด๋‹ค๋Š” 0์— ๊ฐ€๊นŒ์›Œ์ง€๊ธฐ๋Š” ๊ฒฝํ–ฅ์„ ๋•๋‹ˆ๋‹ค. L1 ๊ทœ์ œ๋Š” ์–ด๋–ค ํŠน์„ฑ๋“ค์ด ๋ชจ๋ธ์— ์˜ํ–ฅ์„ ์ฃผ๊ณ  ์žˆ๋Š”์ง€๋ฅผ ์ •ํ™•ํžˆ ํŒ๋‹จํ•˜๊ณ ์ž ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ด๋Ÿฐ ํŒ๋‹จ์ด ํ•„์š” ์—†๋‹ค๋ฉด ๊ฒฝํ—˜์ ์œผ๋กœ๋Š” L2 ๊ทœ์ œ๊ฐ€ ๋” ์ž˜ ๋™์ž‘ํ•˜๋ฏ€๋กœ L2 ๊ทœ์ œ๋ฅผ ๋” ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ L2 ๊ทœ์ œ๋Š” ๊ฐ€์ค‘์น˜ ๊ฐ์‡ (weight decay)๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ €์˜ weight_decay ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•จ์œผ๋กœ์จ L2 ๊ทœ์ œ๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. weight_decay ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’์€ 0์ž…๋‹ˆ๋‹ค. weight_decay ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋‹ค๋ฅธ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. model = Architecture1(10, 20, 2) optimizer = torch.optim.Adam(model.parameters(), lr=1e-4, weight_decay=1e-5) ์ฑ…์— ๋”ฐ๋ผ์„œ๋Š” Regularization๋ฅผ ์ •๊ทœํ™”๋กœ ๋ฒˆ์—ญํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ์ด๋Š” ์ •๊ทœํ™”(Normalization)์™€ ํ˜ผ๋™๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ทœ์ œ ๋˜๋Š” ์ •ํ˜•ํ™”๋ผ๋Š” ๋ฒˆ์—ญ์ด ๋ฐ”๋žŒ์งํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ •๊ทœํ™”์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๋งํฌ : http://blog.naver.com/angryking/221330145300๋ฅผ ์ฐธ๊ณ . ์ธ๊ณต ์‹ ๊ฒฝ๋ง์—์„œ ์ •๊ทœํ™”(Normalization)๋ผ๋Š” ์šฉ์–ด๊ฐ€ ์“ฐ์ด๋Š” ๊ธฐ๋ฒ•์œผ๋กœ๋Š” ๋˜ ๋ฐฐ์น˜ ์ •๊ทœํ™”, ์ธต ์ •๊ทœํ™” ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 4. ๋“œ๋กญ์•„์›ƒ(Dropout) ๋“œ๋กญ์•„์›ƒ์€ ํ•™์Šต ๊ณผ์ •์—์„œ ์‹ ๊ฒฝ๋ง์˜ ์ผ๋ถ€๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋“œ๋กญ์•„์›ƒ์˜ ๋น„์œจ์„ 0.5๋กœ ํ•œ๋‹ค๋ฉด ํ•™์Šต ๊ณผ์ •๋งˆ๋‹ค ๋žœ๋ค์œผ๋กœ ์ ˆ๋ฐ˜์˜ ๋‰ด๋Ÿฐ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์ ˆ๋ฐ˜์˜ ๋‰ด๋Ÿฐ๋งŒ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋“œ๋กญ์•„์›ƒ์€ ์‹ ๊ฒฝ๋ง ํ•™์Šต ์‹œ์—๋งŒ ์‚ฌ์šฉํ•˜๊ณ , ์˜ˆ์ธก ์‹œ์—๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ํ•™์Šต ์‹œ์— ์ธ๊ณต ์‹ ๊ฒฝ๋ง์ด ํŠน์ • ๋‰ด๋Ÿฐ ๋˜๋Š” ํŠน์ • ์กฐํ•ฉ์— ๋„ˆ๋ฌด ์˜์กด์ ์ด๊ฒŒ ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ด ์ฃผ๊ณ , ๋งค๋ฒˆ ๋žœ๋ค ์„ ํƒ์œผ๋กœ ๋‰ด๋Ÿฐ๋“ค์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ์‹ ๊ฒฝ๋ง๋“ค์„ ์•™์ƒ๋ธ” ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๋‚ด์–ด ๊ณผ์ ํ•ฉ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ๊ทœ์ œ ์ฐธ๊ณ  ๋งํฌ : https://bskyvision.com/193 06-10 ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค(Gradient Vanishing)๊ณผ ํญ์ฃผ(Exploding) ๊นŠ์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ํ•™์Šตํ•˜๋‹ค ๋ณด๋ฉด ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ์ž…๋ ฅ์ธต์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ๊ธฐ์šธ๊ธฐ(Gradient)๊ฐ€ ์ ์ฐจ์ ์œผ๋กœ ์ž‘์•„์ง€๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ์ธต์— ๊ฐ€๊นŒ์šด ์ธต๋“ค์—์„œ ๊ฐ€์ค‘์น˜๋“ค์ด ์—…๋ฐ์ดํŠธ๊ฐ€ ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์œผ๋ฉด ๊ฒฐ๊ตญ ์ตœ์ ์˜ ๋ชจ๋ธ์„ ์ฐพ์„ ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค(Gradient Vanishing)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ ์ฐจ ์ปค์ง€๋”๋‹ˆ ๊ฐ€์ค‘์น˜๋“ค์ด ๋น„์ •์ƒ์ ์œผ๋กœ ํฐ ๊ฐ’์ด ๋˜๋ฉด์„œ ๊ฒฐ๊ตญ ๋ฐœ์‚ฐ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ธฐ์šธ๊ธฐ ํญ์ฃผ(Gradient Exploding)์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ๋’ค์—์„œ ๋ฐฐ์šธ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network, RNN)์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋˜๋Š” ๊ธฐ์šธ๊ธฐ ํญ์ฃผ๋ฅผ ๋ง‰๋Š” ๋ฐฉ๋ฒ•๋“ค์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 1. ReLU์™€ ReLU์˜ ๋ณ€ํ˜•๋“ค ์•ž์—์„œ ๋ฐฐ์šด ๋‚ด์šฉ์„ ๊ฐ„๋‹จํžˆ ๋ณต์Šตํ•ด ๋ด…์‹œ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ž…๋ ฅ์˜ ์ ˆ๋Œ“๊ฐ’์ด ํด ๊ฒฝ์šฐ์— ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์ด 0 ๋˜๋Š” 1์— ์ˆ˜๋ ดํ•˜๋ฉด์„œ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์— ๊ฐ€๊นŒ์›Œ์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ์ „ํŒŒ ์‹œํ‚ฌ ๊ธฐ์šธ๊ธฐ๊ฐ€ ์ ์ฐจ ์‚ฌ๋ผ์ ธ์„œ ์ž…๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ์ œ๋Œ€๋กœ ์—ญ์ „ํŒŒ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค์„ ์™„ํ™”ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ์€๋‹‰์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ๋‚˜ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜ ๋Œ€์‹ ์— ReLU๋‚˜ ReLU์˜ ๋ณ€ํ˜• ํ•จ์ˆ˜์™€ ๊ฐ™์€ Leaky ReLU๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์€๋‹‰์ธต์—์„œ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. Leaky ReLU๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ์ž…๋ ฅ๊ฐ’์— ๋Œ€ํ•ด์„œ ๊ธฐ์šธ๊ธฐ๊ฐ€ 0์— ์ˆ˜๋ ดํ•˜์ง€ ์•Š์•„ ์ฃฝ์€ ReLU ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต์—์„œ๋Š” ReLU๋‚˜ Leaky ReLU์™€ ๊ฐ™์€ ReLU ํ•จ์ˆ˜์˜ ๋ณ€ํ˜•๋“ค์„ ์‚ฌ์šฉํ•˜์„ธ์š”. 2. ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™”(Weight initialization) ๊ฐ™์€ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ค๋”๋ผ๋„ ๊ฐ€์ค‘์น˜๊ฐ€ ์ดˆ๊ธฐ์— ์–ด๋–ค ๊ฐ’์„ ๊ฐ€์กŒ๋Š๋ƒ์— ๋”ฐ๋ผ์„œ ๋ชจ๋ธ์˜ ํ›ˆ๋ จ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™”๋งŒ ์ ์ ˆํžˆ ํ•ด์ค˜๋„ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋ฌธ์ œ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ์™„ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™”(Xavier Initialization) ๋…ผ๋ฌธ : http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf 2010๋…„ ์„ธ์ด๋น„์–ด ๊ธ€๋กœ๋Ÿฟ๊ณผ ์š”์Šˆ์•„ ๋ฒค ์ง€ ์˜ค๋Š” ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™”๊ฐ€ ๋ชจ๋ธ์— ๋ฏธ์น˜๋Š” ์˜ํ–ฅ์„ ๋ถ„์„ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์€ ์ œ์•ˆํ•œ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ์„ธ์ด๋น„์–ด(Xavier Initialization) ์ดˆ๊ธฐํ™” ๋˜๋Š” ๊ธ€๋กœ๋Ÿฟ ์ดˆ๊ธฐํ™”(Glorot Initialization)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ๊ท ๋“ฑ ๋ถ„ํฌ(Uniform Distribution) ๋˜๋Š” ์ •๊ทœ ๋ถ„ํฌ(Normal distribution)๋กœ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ๋‚˜๋‰˜๋ฉฐ, ์ด์ „ ์ธต์˜ ๋‰ด๋Ÿฐ ๊ฐœ์ˆ˜์™€ ๋‹ค์Œ ์ธต์˜ ๋‰ด๋Ÿฐ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์‹์„ ์„ธ์›๋‹ˆ๋‹ค. ์ด์ „ ์ธต์˜ ๋‰ด๋Ÿฐ์˜ ๊ฐœ์ˆ˜๋ฅผ i, ๋‹ค์Œ ์ธต์˜ ๋‰ด๋Ÿฐ์˜ ๊ฐœ์ˆ˜๋ฅผ o t ์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ธ€๋กœ๋Ÿฟ๊ณผ ๋ฒค ์ง€ ์˜ค์˜ ๋…ผ๋ฌธ์—์„œ๋Š” ๊ท ๋“ฑ ๋ถ„ํฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ท ๋“ฑ ๋ถ„ํฌ ๋ฒ”์œ„๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. โˆผ n f r ( 6 i + o t + n n n u) ๋‹ค์‹œ ๋งํ•ด n n n u ๋ฅผ ์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, m + ์‚ฌ์ด์˜ ๊ท ๋“ฑ ๋ถ„ํฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ •๊ทœ ๋ถ„ํฌ๋กœ ์ดˆ๊ธฐํ™”ํ•  ๊ฒฝ์šฐ์—๋Š” ํ‰๊ท ์ด 0์ด๊ณ , ํ‘œ์ค€ ํŽธ์ฐจ ฯƒ๊ฐ€ ๋‹ค์Œ์„ ๋งŒ์กฑํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. = n n n u ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™”๋Š” ์—ฌ๋Ÿฌ ์ธต์˜ ๊ธฐ์šธ๊ธฐ ๋ถ„์‚ฐ ์‚ฌ์ด์— ๊ท ํ˜•์„ ๋งž์ถฐ์„œ ํŠน์ • ์ธต์ด ๋„ˆ๋ฌด ์ฃผ๋ชฉ์„ ๋ฐ›๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์ธต์ด ๋’ค์ฒ˜์ง€๋Š” ๊ฒƒ์„ ๋ง‰์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™”๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋‚˜ ํ•˜์ดํผ๋ณผ๋ฆญ ํƒ„์  ํŠธ ํ•จ์ˆ˜์™€ ๊ฐ™์€ S์ž ํ˜•ํƒœ์ธ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์ด์ง€๋งŒ, ReLU์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์„ฑ๋Šฅ์ด ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ReLU ํ•จ์ˆ˜ ๋˜๋Š” ReLU์˜ ๋ณ€ํ˜• ํ•จ์ˆ˜๋“ค์„ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๋‹ค๋ฅธ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€๋ฐ, ์ด๋ฅผ He ์ดˆ๊ธฐํ™”(He initialization)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. He ์ดˆ๊ธฐํ™”(He initialization) ๋…ผ๋ฌธ : https://www.cv-foundation.org/openaccess/content_iccv_2015/papers/He_Delving_Deep_into_ICCV_2015_paper.pdf He ์ดˆ๊ธฐํ™”(He initialization)๋Š” ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™”์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ •๊ทœ ๋ถ„ํฌ์™€ ๊ท ๋“ฑ ๋ถ„ํฌ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, He ์ดˆ๊ธฐํ™”๋Š” ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™”์™€ ๋‹ค๋ฅด๊ฒŒ ๋‹ค์Œ ์ธต์˜ ๋‰ด๋Ÿฐ์˜ ์ˆ˜๋ฅผ ๋ฐ˜์˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ „๊ณผ ๊ฐ™์ด ์ด์ „ ์ธต์˜ ๋‰ด๋Ÿฐ์˜ ๊ฐœ์ˆ˜๋ฅผ i์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. He ์ดˆ๊ธฐํ™”๋Š” ๊ท ๋“ฑ ๋ถ„ํฌ๋กœ ์ดˆ๊ธฐํ™”ํ•  ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ท ๋“ฑ ๋ถ„ํฌ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. โˆผ n f r ( 6 i , + n n ) ์ •๊ทœ ๋ถ„ํฌ๋กœ ์ดˆ๊ธฐํ™”ํ•  ๊ฒฝ์šฐ์—๋Š” ํ‘œ์ค€ ํŽธ์ฐจ ฯƒ๊ฐ€ ๋‹ค์Œ์„ ๋งŒ์กฑํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. = n n ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋‚˜ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์„ธ์ด๋น„์–ด ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์ด ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ReLU ๊ณ„์—ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” He ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์ด ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ReLU + He ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์ด ์ข€ ๋” ๋ณดํŽธ์ ์ž…๋‹ˆ๋‹ค. 3. ๋ฐฐ์น˜ ์ •๊ทœํ™”(Batch Normalization) ReLU ๊ณ„์—ด์˜ ํ•จ์ˆ˜์™€ He ์ดˆ๊ธฐํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ์–ด๋Š ์ •๋„ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค๊ณผ ํญ์ฃผ๋ฅผ ์™„ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด ๋‘ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ํ›ˆ๋ จ ์ค‘์— ์–ธ์ œ๋“  ๋‹ค์‹œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค์ด๋‚˜ ํญ์ฃผ๋ฅผ ์˜ˆ๋ฐฉํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ๋ฐฐ์น˜ ์ •๊ทœํ™”(Batch Normalization)์ž…๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ฐ ์ธต์— ๋“ค์–ด๊ฐ€๋Š” ์ž…๋ ฅ์„ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์œผ๋กœ ์ •๊ทœํ™”ํ•˜์—ฌ ํ•™์Šต์„ ํšจ์œจ์ ์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. 1. ๋‚ด๋ถ€ ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™”(Internal Covariate Shift) ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‚ด๋ถ€ ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™”(Internal Covariate Shift)๋ฅผ ์ดํ•ดํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™”๋ž€ ํ•™์Šต ๊ณผ์ •์—์„œ ์ธต ๋ณ„๋กœ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ๋ถ„ํฌ๊ฐ€ ๋‹ฌ๋ผ์ง€๋Š” ํ˜„์ƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์ธต๋“ค์˜ ํ•™์Šต์— ์˜ํ•ด ์ด์ „ ์ธต์˜ ๊ฐ€์ค‘์น˜ ๊ฐ’์ด ๋ฐ”๋€Œ๊ฒŒ ๋˜๋ฉด, ํ˜„์žฌ ์ธต์— ์ „๋‹ฌ๋˜๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ๊ฐ€ ํ˜„์žฌ ์ธต์ด ํ•™์Šตํ–ˆ๋˜ ์‹œ์ ์˜ ๋ถ„ํฌ์™€ ์ฐจ์ด๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ œ์•ˆํ•œ ๋…ผ๋ฌธ์—์„œ๋Š” ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค/ํญ์ฃผ ๋“ฑ์˜ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ๋ถˆ์•ˆ์ „์„ฑ์ด ์ธต๋งˆ๋‹ค ์ž…๋ ฅ์˜ ๋ถ„ํฌ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ์ฃผ์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™”๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™”๋Š” ์‹ ๊ฒฝ๋ง ์ธต ์‚ฌ์ด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ ๋ณ€ํ™”๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 2. ๋ฐฐ์น˜ ์ •๊ทœํ™”(Batch Normalization) ๋ฐฐ์น˜ ์ •๊ทœํ™”(Batch Normalization)๋Š” ํ‘œํ˜„ ๊ทธ๋Œ€๋กœ ํ•œ ๋ฒˆ์— ๋“ค์–ด์˜ค๋Š” ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ์ •๊ทœํ™”ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ๊ฐ ์ธต์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๊ธฐ ์ „์— ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์š”์•ฝํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ์— ๋Œ€ํ•ด ํ‰๊ท ์„ 0์œผ๋กœ ๋งŒ๋“ค๊ณ , ์ •๊ทœํ™”๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ •๊ทœํ™” ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์Šค์ผ€์ผ๊ณผ ์‹œํ”„ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ฮณ์™€ ฮฒ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ฮณ๋Š” ์Šค์ผ€์ผ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๊ณ , ฮฒ๋Š” ์‹œํ”„ํŠธ๋ฅผ ํ•˜๋Š” ๊ฒƒ์— ์‚ฌ์šฉํ•˜๋ฉฐ ๋‹ค์Œ ๋ ˆ์ด์–ด์— ์ผ์ •ํ•œ ๋ฒ”์œ„์˜ ๊ฐ’๋“ค๋งŒ ์ „๋‹ฌ๋˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”์˜ ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ N ์€ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Input : ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ = { ( ) x ( ) . . x ( ) } Output : ( ) B ฮณ ฮฒ ( ( ) ) ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ํ‰๊ท  B 1 โˆ‘ = m ( ) # ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ํ‰๊ท  ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ๋ถ„์‚ฐ B โ† m i 1 ( ( ) ฮผ) # ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ๋ถ„์‚ฐ ์ •๊ทœํ™” ^ ( ) x ( ) ฮผ ฯƒ 2 ฮต # ์ •๊ทœํ™” ์Šค์ผ€์ผ ์กฐ์ •๊ณผ ์‹œํ”„ํŠธ ( ) ฮณ ^ ( ) ฮฒ B ฮณ ฮฒ ( ( ) ) # ์Šค์ผ€์ผ ์กฐ์ •๊ณผ ์‹œํ”„ํŠธ ์€ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ์žˆ๋Š” ์ƒ˜ํ”Œ์˜ ์ˆ˜ B ๋Š” ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ํ‰๊ท . B ๋Š” ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์— ๋Œ€ํ•œ ํ‘œ์ค€ํŽธ์ฐจ. ^ ( ) ์€ ํ‰๊ท ์ด 0์ด๊ณ  ์ •๊ทœํ™” ๋œ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ. ์€ ๋ถ„๋ชจ๊ฐ€ 0์ด ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๋Š” ์ž‘์€ ์ˆ˜. ๋ณดํŽธ์ ์œผ๋กœ 10 5๋Š” ์ •๊ทœํ™” ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์Šค์ผ€์ผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•™์Šต ๋Œ€์ƒ ๋Š” ์ •๊ทœํ™” ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์‹œํ”„ํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•™์Šต ๋Œ€์ƒ ( ) ๋Š” ์Šค์ผ€์ผ๊ณผ ์‹œํ”„ํŠธ๋ฅผ ํ†ตํ•ด ์กฐ์ •ํ•œ N ์˜ ์ตœ์ข… ๊ฒฐ๊ณผ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ํ•™์Šต ์‹œ ๋ฐฐ์น˜ ๋‹จ์œ„์˜ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ๋“ค์„ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐ›์•„ ์ด๋™ ํ‰๊ท ๊ณผ ์ด๋™ ๋ถ„์‚ฐ์„ ์ €์žฅํ•ด๋†“์•˜๋‹ค๊ฐ€ ํ…Œ์ŠคํŠธํ•  ๋•Œ๋Š” ํ•ด๋‹น ๋ฐฐ์น˜์˜ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์„ ๊ตฌํ•˜์ง€ ์•Š๊ณ  ๊ตฌํ•ด๋†“์•˜๋˜ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์œผ๋กœ ์ •๊ทœํ™”๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋‚˜ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค ๋ฌธ์ œ๊ฐ€ ํฌ๊ฒŒ ๊ฐœ์„ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™”์— ํ›จ์”ฌ ๋œ ๋ฏผ๊ฐํ•ด์ง‘๋‹ˆ๋‹ค. ํ›จ์”ฌ ํฐ ํ•™์Šต๋ฅ ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด ํ•™์Šต ์†๋„๋ฅผ ๊ฐœ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋งˆ๋‹ค ํ‰๊ท ๊ณผ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ณ„์‚ฐํ•˜๋ฏ€๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์ผ์ข…์˜ ์žก์Œ์„ ๋„ฃ๋Š” ๋ถ€์ˆ˜ ํšจ๊ณผ๋กœ ๊ณผ์ ํ•ฉ์„ ๋ฐฉ์ง€ํ•˜๋Š” ํšจ๊ณผ๋„ ๋ƒ…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ถ€์ˆ˜์  ํšจ๊ณผ์ด๋ฏ€๋กœ ๋“œ๋กญ์•„์›ƒ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ๋ชจ๋ธ์„ ๋ณต์žกํ•˜๊ฒŒ ํ•˜๋ฉฐ, ์ถ”๊ฐ€ ๊ณ„์‚ฐ์„ ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์˜ˆ์ธก ์‹œ์— ์‹คํ–‰ ์‹œ๊ฐ„์ด ๋Š๋ ค์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์„œ๋น„์Šค ์†๋„๋ฅผ ๊ณ ๋ คํ•˜๋Š” ๊ด€์ ์—์„œ๋Š” ๋ฐฐ์น˜ ์ •๊ทœํ™”๊ฐ€ ๊ผญ ํ•„์š”ํ•œ์ง€ ๊ณ ๋ฏผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”์˜ ํšจ๊ณผ๋Š” ๊ต‰์žฅํ•˜์ง€๋งŒ ๋‚ด๋ถ€ ๊ณต๋ณ€๋Ÿ‰ ๋ณ€ํ™” ๋•Œ๋ฌธ์€ ์•„๋‹ˆ๋ผ๋Š” ๋…ผ๋ฌธ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. : https://arxiv.org/pdf/1805.11604.pdf 3) ๋ฐฐ์น˜ ์ •๊ทœํ™”์˜ ํ•œ๊ณ„ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ๋›ฐ์–ด๋‚œ ๋ฐฉ๋ฒ•์ด์ง€๋งŒ ๋ช‡ ๊ฐ€์ง€ ํ•œ๊ณ„๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ํฌ๊ธฐ์— ์˜์กด์ ์ด๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ๋„ˆ๋ฌด ์ž‘์€ ๋ฐฐ์น˜ ํฌ๊ธฐ์—์„œ๋Š” ์ž˜ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์ ์œผ๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 1๋กœ ํ•˜๊ฒŒ ๋˜๋ฉด ๋ถ„์‚ฐ์€ 0์ด ๋ฉ๋‹ˆ๋‹ค. ์ž‘์€ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์—์„œ๋Š” ๋ฐฐ์น˜ ์ •๊ทœํ™”์˜ ํšจ๊ณผ๊ฐ€ ๊ทน๋‹จ์ ์œผ๋กœ ์ž‘์šฉ๋˜์–ด ํ›ˆ๋ จ์— ์•…์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ ์šฉํ•  ๋•Œ๋Š” ์ž‘์€ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ณด๋‹ค๋Š” ํฌ๊ธฐ๊ฐ€ ์–ด๋Š ์ •๋„ ๋˜๋Š” ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์—์„œ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ๋ฐฐ์น˜ ํฌ๊ธฐ์— ์˜์กด์ ์ธ ๋ฉด์ด ์žˆ์Šต๋‹ˆ๋‹ค. 2. RNN์— ์ ์šฉํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒ ์ง€๋งŒ, RNN์€ ๊ฐ ์‹œ์ (time step)๋งˆ๋‹ค ๋‹ค๋ฅธ ํ†ต๊ณ„์น˜๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋Š” RNN์— ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. RNN์—์„œ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ๋…ผ๋ฌธ์ด ์ œ์‹œ๋˜์–ด ์žˆ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” ์ด๋ฅผ ์†Œ๊ฐœํ•˜๋Š” ๋Œ€์‹  ๋ฐฐ์น˜ ํฌ๊ธฐ์—๋„ ์˜์กด์ ์ด์ง€ ์•Š์œผ๋ฉฐ, RNN์—๋„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ˆ˜์›”ํ•œ ์ธต ์ •๊ทœํ™”(layer normalization)๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. 5. ์ธต ์ •๊ทœํ™”(Layer Normalization) ์ธต ์ •๊ทœํ™”๋ฅผ ์ดํ•ดํ•˜๊ธฐ์— ์•ž์„œ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ด 3์ด๊ณ , ํŠน์„ฑ์˜ ์ˆ˜๊ฐ€ 4์ผ ๋•Œ์˜ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋ฏธ๋‹ˆ ๋ฐฐ์น˜๋ž€ ๋™์ผํ•œ ํŠน์„ฑ(feature) ๊ฐœ์ˆ˜๋“ค์„ ๊ฐ€์ง„ ๋‹ค์ˆ˜์˜ ์ƒ˜ํ”Œ๋“ค์„ ์˜๋ฏธํ•จ์„ ์ƒ๊ธฐํ•ฉ์‹œ๋‹ค. ๋ฐ˜๋ฉด, ์ธต ์ •๊ทœํ™”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™” ์ฐธ๊ณ  ์ž๋ฃŒ : http://nlp.jbnu.ac.kr/AI2019/slides/ch05-1.pdf https://reniew.github.io/13/ https://calcifer1009-dev.tistory.com/11 ๋ฐฐ์น˜ ์ •๊ทœํ™” ์ธํ„ฐ๋„ท ๊ฐ•์˜(ํ•œ๊ตญ์–ด ์ž๋ง‰) : https://www.youtube.com/watch?v=tNIpEZLv_eg https://www.youtube.com/watch? v=em6dfRxYkYU https://www.youtube.com/watch? v=nUUqwaxLnWs ๋ฐฐ์น˜ ์ •๊ทœํ™” ์ฐธ๊ณ  ์ž๋ฃŒ : https://light-tree.tistory.com/139 https://sacko.tistory.com/44? category=632408 http://funmv2013.blog spot.com/2016/09/batch-normalization.html https://excelsior-cjh.tistory.com/178 https://www.youtube.com/watch?v=HCEr5f-LfVE&list=PLQ28Nx3M4JrhkqBVIXg-i5_CVVoS1UzAv&index=17 07. [DL ์ž…๋ฌธ ] - ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” RNN(์ˆœํ™˜ ์‹ ๊ฒฝ๋ง)์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜๊ณ , RNN์˜ ์žฅ๊ธฐ ์˜์กด์„ฑ ๋ฌธ์ œ๋ฅผ ๋ณด์™„ํ•œ LSTM, ๊ทธ๋ฆฌ๊ณ  RNN๊ณผ LSTM์„ ์ด์šฉํ•˜์—ฌ ๊ฐ์ข… ๋”ฅ ๋Ÿฌ๋‹ ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 07-01 ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network, RNN) RNN(Recurrent Neural Network)์€ ์‹œํ€€์Šค(Sequence) ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์‹œํ€€์Šค ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋ฒˆ์—ญ๊ธฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์ž…๋ ฅ์€ ๋ฒˆ์—ญํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์žฅ. ์ฆ‰, ๋‹จ์–ด ์‹œํ€€์Šค์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์— ํ•ด๋‹น๋˜๋Š” ๋ฒˆ์—ญ๋œ ๋ฌธ์žฅ ๋˜ํ•œ ๋‹จ์–ด ์‹œํ€€์Šค์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์‹œํ€€์Šค๋“ค์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ๋ชจ๋ธ๋“ค์„ ์‹œํ€€์Šค ๋ชจ๋ธ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ์ค‘์—์„œ๋„ RNN์€ ๋”ฅ ๋Ÿฌ๋‹์— ์žˆ์–ด ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์‹œํ€€์Šค ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์šฉ์–ด๋Š” ๋น„์Šทํ•˜์ง€๋งŒ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง๊ณผ ์žฌ๊ท€ ์‹ ๊ฒฝ๋ง(Recursive Neural Network)์€ ์ „ํ˜€ ๋‹ค๋ฅธ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. 1. ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network, RNN) ์•ž์„œ ๋ฐฐ์šด ์‹ ๊ฒฝ๋ง๋“ค์€ ์ „๋ถ€ ์€๋‹‰์ธต์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ๊ฐ’์€ ์˜ค์ง ์ถœ๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ํ–ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ์‹ ๊ฒฝ๋ง๋“ค์„ ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง(Feed Forward Neural Network)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ทธ๋ ‡์ง€ ์•Š์€ ์‹ ๊ฒฝ๋ง๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. RNN(Recurrent Neural Network) ๋˜ํ•œ ๊ทธ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. RNN์€ ์€๋‹‰์ธต์˜ ๋…ธ๋“œ์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ๋„ ๋ณด๋‚ด๋ฉด์„œ, ๋‹ค์‹œ ์€๋‹‰์ธต ๋…ธ๋“œ์˜ ๋‹ค์Œ ๊ณ„์‚ฐ์˜ ์ž…๋ ฅ์œผ๋กœ ๋ณด๋‚ด๋Š” ํŠน์ง•์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.๋Š” ์ž…๋ ฅ์ธต์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ,๋Š” ์ถœ๋ ฅ์ธต์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋Š” ํŽธํ–ฅ ๋„ ์ž…๋ ฅ์œผ๋กœ ์กด์žฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์•ž์œผ๋กœ์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. RNN์—์„œ ์€๋‹‰์ธต์—์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ์—ญํ• ์„ ํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์…€(cell)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ์…€์€ ์ด์ „์˜ ๊ฐ’์„ ๊ธฐ์–ตํ•˜๋ ค๊ณ  ํ•˜๋Š” ์ผ์ข…์˜ ๋ฉ”๋ชจ๋ฆฌ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์ด๋ฅผ ๋ฉ”๋ชจ๋ฆฌ ์…€ ๋˜๋Š” RNN ์…€์ด๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ ๊ฐ๊ฐ์˜ ์‹œ์ (time step)์—์„œ ๋ฐ”๋กœ ์ด์ „ ์‹œ์ ์—์„œ์˜ ์€๋‹‰์ธต์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์—์„œ ๋‚˜์˜จ ๊ฐ’์„ ์ž์‹ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์žฌ๊ท€์  ํ™œ๋™์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ๋Š” ํ˜„์žฌ ์‹œ์ ์„ ๋ณ€์ˆ˜ t๋กœ ํ‘œํ˜„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ˜„์žฌ ์‹œ์  t์—์„œ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ด ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ’์€ ๊ณผ๊ฑฐ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€๋“ค์˜ ๊ฐ’์— ์˜ํ–ฅ์„ ๋ฐ›์€ ๊ฒƒ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ด ๊ฐ–๊ณ  ์žˆ๋Š” ์ด ๊ฐ’์€ ๋ญ๋ผ๊ณ  ๋ถ€๋ฅผ๊นŒ์š”? ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ด ์ถœ๋ ฅ์ธต ๋ฐฉํ–ฅ์œผ๋กœ ๋˜๋Š” ๋‹ค์Œ ์‹œ์  t+1์˜ ์ž์‹ ์—๊ฒŒ ๋ณด๋‚ด๋Š” ๊ฐ’์„ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด t ์‹œ์ ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ t-1 ์‹œ์ ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€ ์ด ๋ณด๋‚ธ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ ๊ณ„์‚ฐ์„ ์œ„ํ•œ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. RNN์„ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์ขŒ์ธก๊ณผ ๊ฐ™์ด ํ™”์‚ดํ‘œ๋กœ ์‚ฌ์ดํด์„ ๊ทธ๋ ค์„œ ์žฌ๊ท€ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ์šฐ์ธก๊ณผ ๊ฐ™์ด ์‚ฌ์ดํด์„ ๊ทธ๋ฆฌ๋Š” ํ™”์‚ดํ‘œ ๋Œ€์‹  ์—ฌ๋Ÿฌ ์‹œ์ ์œผ๋กœ ํŽผ์ณ์„œ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๊ทธ๋ฆผ์€ ๋™์ผํ•œ ๊ทธ๋ฆผ์œผ๋กœ ๋‹จ์ง€ ์‚ฌ์ดํด์„ ๊ทธ๋ฆฌ๋Š” ํ™”์‚ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„ํ•˜์˜€๋Š๋ƒ, ์‹œ์ ์˜ ํ๋ฆ„์— ๋”ฐ๋ผ์„œ ํ‘œํ˜„ํ•˜์˜€๋Š๋ƒ์˜ ์ฐจ์ด์ผ ๋ฟ ๋‘˜ ๋‹ค ๋™์ผํ•œ RNN์„ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง์—์„œ๋Š” ๋‰ด๋Ÿฐ์ด๋ผ๋Š” ๋‹จ์œ„๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, RNN์—์„œ๋Š” ๋‰ด๋Ÿฐ์ด๋ผ๋Š” ๋‹จ์œ„๋ณด๋‹ค๋Š” ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต์—์„œ๋Š” ๊ฐ๊ฐ ์ž…๋ ฅ ๋ฒกํ„ฐ์™€ ์ถœ๋ ฅ ๋ฒกํ„ฐ, ์€๋‹‰์ธต์—์„œ๋Š” ์€๋‹‰ ์ƒํƒœ๋ผ๋Š” ํ‘œํ˜„์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์‚ฌ์‹ค ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ํšŒ์ƒ‰๊ณผ ์ดˆ๋ก์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฐ ๋„ค๋ชจ๋“ค์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฒกํ„ฐ ๋‹จ์œ„๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ”ผ๋“œ ํฌ์›Œ๋“œ ์‹ ๊ฒฝ๋ง๊ณผ์˜ ์ฐจ์ด๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ RNN์„ ๋‰ด๋Ÿฐ ๋‹จ์œ„๋กœ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 4, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๊ฐ€ 2, ์ถœ๋ ฅ์ธต์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 2์ธ RNN์ด ์‹œ์ ์ด 2์ผ ๋•Œ์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋‰ด๋Ÿฐ ๋‹จ์œ„๋กœ ํ•ด์„ํ•˜๋ฉด ์ž…๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜๋Š” 4, ์€๋‹‰์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜๋Š” 2, ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ ์ˆ˜๋Š” 2์ž…๋‹ˆ๋‹ค. RNN์€ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ๊ธธ์ด๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ๊ธธ์ด์— ๋”ฐ๋ผ์„œ ๋‹ฌ๋ผ์ง€๋Š” RNN์˜ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ๊ตฌ์กฐ๊ฐ€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. RNN ์…€์˜ ๊ฐ ์‹œ์  ๋ณ„ ์ž…, ์ถœ๋ ฅ์˜ ๋‹จ์œ„๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•˜๊ธฐ ๋‚˜๋ฆ„์ด์ง€๋งŒ ๊ฐ€์žฅ ๋ณดํŽธ์ ์ธ ๋‹จ์œ„๋Š” '๋‹จ์–ด ๋ฒกํ„ฐ'์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•˜๋‚˜์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ถœ๋ ฅ(one-to-many)์˜ ๋ชจ๋ธ์€ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์‚ฌ์ง„์˜ ์ œ๋ชฉ์„ ์ถœ๋ ฅํ•˜๋Š” ์ด๋ฏธ์ง€ ์บก์…”๋‹(Image Captioning) ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ง„์˜ ์ œ๋ชฉ์€ ๋‹จ์–ด๋“ค์˜ ๋‚˜์—ด์ด๋ฏ€๋กœ ์‹œํ€€์Šค ์ถœ๋ ฅ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‹จ์–ด ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ ํ•˜๋‚˜์˜ ์ถœ๋ ฅ(many-to-one)์„ ํ•˜๋Š” ๋ชจ๋ธ์€ ์ž…๋ ฅ ๋ฌธ์„œ๊ฐ€ ๊ธ์ •์ ์ธ์ง€ ๋ถ€์ •์ ์ธ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ๊ฐ์„ฑ ๋ถ„๋ฅ˜(sentiment classification), ๋˜๋Š” ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜(spam detection)์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ RNN์œผ๋กœ ์ŠคํŒธ ๋ฉ”์ผ์„ ๋ถ„๋ฅ˜ํ•  ๋•Œ์˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋‹ค ๋Œ€๋‹ค(many-to-many)์˜ ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ์—๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ๋Œ€๋‹ต ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋Š” ์ฑ—๋ด‡๊ณผ ์ž…๋ ฅ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ๋ฒˆ์—ญ๋œ ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฒˆ์—ญ๊ธฐ, ๊ฐœ์ฒด๋ช… ์ธ์‹์ด๋‚˜ ํ’ˆ์‚ฌ ํƒœ๊น…๊ณผ ๊ฐ™์€ ์ž‘์—…์ด ์†ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์ˆ˜ํ–‰ํ•  ๋•Œ์˜ RNN ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด์ œ RNN์— ๋Œ€ํ•œ ์ˆ˜์‹์„ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์‹œ์  t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ t ๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์€๋‹‰์ธต์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ t ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด ๋‘ ๊ฐœ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ์ž…๋ ฅ์ธต์—์„œ ์ž…๋ ฅ๊ฐ’์„ ์œ„ํ•œ ๊ฐ€์ค‘์น˜ x ์ด๊ณ , ํ•˜๋‚˜๋Š” ์ด์ „ ์‹œ์  t-1์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์ธ t 1 ์„ ์œ„ํ•œ ๊ฐ€์ค‘์น˜ h ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์€๋‹‰์ธต : t t n ( x t W h โˆ’ + ) ์ถœ๋ ฅ์ธต : t f ( y t b ) ๋‹จ,๋Š” ๋น„์„ ํ˜• ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ค‘ ํ•˜๋‚˜. RNN์˜ ์€๋‹‰์ธต ์—ฐ์‚ฐ์„ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ ์—ฐ์‚ฐ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ RNN์˜ ์ž…๋ ฅ t ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ๋‹จ์–ด ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์„๋ผ๊ณ  ํ•˜๊ณ , ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ h ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ๊ฐ ๋ฒกํ„ฐ์™€ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. t ( ร— ) x ( h d ) h ( h D) t 1 ( h 1 ) : ( h 1 ) ๋ฐฐ์น˜ ํฌ๊ธฐ๊ฐ€ 1์ด๊ณ , ์™€ h ๋‘ ๊ฐ’ ๋ชจ๋‘๋ฅผ 4๋กœ ๊ฐ€์ •ํ•˜์˜€์„ ๋•Œ, RNN์˜ ์€๋‹‰์ธต ์—ฐ์‚ฐ์„ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋•Œ t ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์ฃผ๋กœ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜(tanh)๊ฐ€ ์‚ฌ์šฉ๋˜์ง€๋งŒ, ReLU๋กœ ๋ฐ”๊ฟ” ์‚ฌ์šฉํ•˜๋Š” ์‹œ๋„๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์‹์—์„œ ๊ฐ๊ฐ์˜ ๊ฐ€์ค‘์น˜ x W, y ์˜ ๊ฐ’์€ ๋ชจ๋“  ์‹œ์ ์—์„œ ๊ฐ’์„ ๋™์ผํ•˜๊ฒŒ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์€๋‹‰์ธต์ด 2๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ์—๋Š” ์€๋‹‰์ธต 2๊ฐœ์˜ ๊ฐ€์ค‘์น˜๋Š” ์„œ๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์€ ๊ฒฐ๊ณผ๊ฐ’์ธ t ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ํ…๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ๋‹ค์–‘ํ•œ ์นดํ…Œ๊ณ ๋ฆฌ ์ค‘์—์„œ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๋ผ๋ฉด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. 2. ํŒŒ์ด์ฌ์œผ๋กœ RNN ๊ตฌํ˜„ํ•˜๊ธฐ ์ง์ ‘ Numpy๋กœ RNN ์ธต์„ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ฉ”๋ชจ๋ฆฌ ์…€์—์„œ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์‹์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. t t n ( x t W h โˆ’ + ) ์‹ค์ œ ๊ตฌํ˜„์— ์•ž์„œ ๊ฐ„๋‹จํžˆ ์˜์‚ฌ ์ฝ”๋“œ(pseudocode)๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์˜์‚ฌ ์ฝ”๋“œ(pseudocode)๋กœ ์‹ค์ œ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์•„๋‹˜. hidden_state_t = 0 # ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋ฅผ 0(๋ฒกํ„ฐ)๋กœ ์ดˆ๊ธฐํ™” for input_t in input_length: # ๊ฐ ์‹œ์ ๋งˆ๋‹ค ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค. output_t = tanh(input_t, hidden_state_t) # ๊ฐ ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ž…๋ ฅ๊ณผ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ์‚ฐ hidden_state_t = output_t # ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋Š” ํ˜„์žฌ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ๋œ๋‹ค. ์šฐ์„  t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ hidden_state_t๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜์˜€๊ณ , ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋ฅผ input_length๋กœ ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋Š” ๊ณง ์ด ์‹œ์ ์˜ ์ˆ˜(timesteps)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  t ์‹œ์ ์˜ ์ž…๋ ฅ๊ฐ’์„ input_t๋กœ ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ ๊ฐ ์‹œ์ ๋งˆ๋‹ค input_t์™€ hidden_sate_t(์ด์ „ ์ƒํƒœ์˜ ์€๋‹‰ ์ƒํƒœ)๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์ธ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ˜„์‹œ์ ์˜ hidden_state_t๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์˜์‚ฌ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํžˆ ๊ฐœ๋… ์ •๋ฆฝ์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด์ œ RNN ์ธต์„ ์‹ค์ œ ๋™์ž‘๋˜๋Š” ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด (timesteps, input_size) ํฌ๊ธฐ์˜ 2D ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์˜€์œผ๋‚˜, ์‹ค์ œ๋กœ ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” (batch_size, timesteps, input_size)์˜ ํฌ๊ธฐ์˜ 3D ํ…์„œ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•ฉ์‹œ๋‹ค. import numpy as np timesteps = 10 # ์‹œ์ ์˜ ์ˆ˜. NLP์—์„œ๋Š” ๋ณดํ†ต ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ ๋œ๋‹ค. input_size = 4 # ์ž…๋ ฅ์˜ ์ฐจ์›. NLP์—์„œ๋Š” ๋ณดํ†ต ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋œ๋‹ค. hidden_size = 8 # ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ. ๋ฉ”๋ชจ๋ฆฌ ์…€์˜ ์šฉ๋Ÿ‰์ด๋‹ค. inputs = np.random.random((timesteps, input_size)) # ์ž…๋ ฅ์— ํ•ด๋‹น๋˜๋Š” 2D ํ…์„œ hidden_state_t = np.zeros((hidden_size,)) # ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋Š” 0(๋ฒกํ„ฐ)๋กœ ์ดˆ๊ธฐํ™” # ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ hidden_size๋กœ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๋งŒ๋“ฆ. ์šฐ์„  ์‹œ์ , ์ž…๋ ฅ์˜ ์ฐจ์›, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ, ๊ทธ๋ฆฌ๊ณ  ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋Š” 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๋กœ ์ดˆ๊ธฐํ™”๊ฐ€ ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(hidden_state_t) # 8์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ์€๋‹‰ ์ƒํƒœ. ํ˜„์žฌ๋Š” ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋กœ ๋ชจ๋“  ์ฐจ์›์ด 0์˜ ๊ฐ’์„ ๊ฐ€์ง. [0. 0. 0. 0. 0. 0. 0. 0.] ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ 8๋กœ ์ •์˜ํ•˜์˜€์œผ๋ฏ€๋กœ 8์˜ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” 0์˜ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฒกํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. Wx = np.random.random((hidden_size, input_size)) # (8, 4) ํฌ๊ธฐ์˜ 2D ํ…์„œ ์ƒ์„ฑ. ์ž…๋ ฅ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜. Wh = np.random.random((hidden_size, hidden_size)) # (8, 8) ํฌ๊ธฐ์˜ 2D ํ…์„œ ์ƒ์„ฑ. ์€๋‹‰ ์ƒํƒœ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜. b = np.random.random((hidden_size,)) # (8, ) ํฌ๊ธฐ์˜ 1D ํ…์„œ ์ƒ์„ฑ. ์ด ๊ฐ’์€ ํŽธํ–ฅ(bias). ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ๊ฐ ํฌ๊ธฐ์— ๋งž๊ฒŒ ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(np.shape(Wx)) print(np.shape(Wh)) print(np.shape(b)) (8, 4) (8, 8) (8, ) ๊ฐ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์˜ ํฌ๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Wx๋Š” (์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ ร— ์ž…๋ ฅ์˜ ์ฐจ์›), Wh๋Š” (์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ ร— ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ), b๋Š” (์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , RNN ์ธต์„ ๋™์ž‘์‹œ์ผœ๋ด…์‹œ๋‹ค. total_hidden_states = [] # ๋ฉ”๋ชจ๋ฆฌ ์…€ ๋™์ž‘ for input_t in inputs: # ๊ฐ ์‹œ์ ์— ๋”ฐ๋ผ์„œ ์ž…๋ ฅ๊ฐ’์ด ์ž…๋ ฅ๋จ. output_t = np.tanh(np.dot(Wx, input_t) + np.dot(Wh, hidden_state_t) + b) # Wx * Xt + Wh * Ht-1 + b(bias) total_hidden_states.append(list(output_t)) # ๊ฐ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์„ ๊ณ„์†ํ•ด์„œ ์ถ•์  print(np.shape(total_hidden_states)) # ๊ฐ ์‹œ์  t ๋ณ„ ๋ฉ”๋ชจ๋ฆฌ ์…€์˜ ์ถœ๋ ฅ์˜ ํฌ๊ธฐ๋Š” (timestep, output_dim) hidden_state_t = output_t total_hidden_states = np.stack(total_hidden_states, axis = 0) # ์ถœ๋ ฅ ์‹œ ๊ฐ’์„ ๊น”๋”ํ•˜๊ฒŒ ํ•ด์ค€๋‹ค. print(total_hidden_states) # (timesteps, output_dim)์˜ ํฌ๊ธฐ. ์ด ๊ฒฝ์šฐ (10, 8)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ๋ฉ”๋ชจ๋ฆฌ ์…€์˜ 2D ํ…์„œ๋ฅผ ์ถœ๋ ฅ. (1, 8) (2, 8) (3, 8) (4, 8) (5, 8) (6, 8) (7, 8) (8, 8) (9, 8) (10, 8) [[0.85575076 0.71627213 0.87703694 0.83938496 0.81045543 0.86482715 0.76387233 0.60007514] [0.99982366 0.99985897 0.99928638 0.99989791 0.99998252 0.99977656 0.99997677 0.9998397 ] [0.99997583 0.99996057 0.99972541 0.99997993 0.99998684 0.99954936 0.99997638 0.99993143] [0.99997782 0.99996494 0.99966651 0.99997989 0.99999115 0.99980087 0.99999107 0.9999622 ] [0.99997231 0.99996091 0.99976218 0.99998483 0.9999955 0.99989239 0.99999339 0.99997324] [0.99997082 0.99998754 0.99962158 0.99996278 0.99999331 0.99978731 0.99998831 0.99993414] [0.99997427 0.99998367 0.99978331 0.99998173 0.99999579 0.99983689 0.99999058 0.99995531] [0.99992591 0.99996115 0.99941212 0.99991593 0.999986 0.99966571 0.99995842 0.99987795] [0.99997139 0.99997192 0.99960794 0.99996751 0.99998795 0.9996674 0.99998177 0.99993016] [0.99997659 0.99998915 0.99985392 0.99998726 0.99999773 0.99988295 0.99999316 0.99996326]] 3. ํŒŒ์ด ํ† ์น˜์˜ nn.RNN() ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” nn.RNN()์„ ํ†ตํ•ด์„œ RNN ์…€์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์Šต์„ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ํ•„์š”ํ•œ ํŒŒ์ด ํ† ์น˜์˜ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn ์ด์ œ ์ž…๋ ฅ์˜ ํฌ๊ธฐ์™€ ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋Š” ๋Œ€ํ‘œ์ ์ธ RNN์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ž…๋ ฅ์˜ ํฌ๊ธฐ๋Š” ๋งค ์‹œ์ ๋งˆ๋‹ค ๋“ค์–ด๊ฐ€๋Š” ์ž…๋ ฅ์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. input_size = 5 # ์ž…๋ ฅ์˜ ํฌ๊ธฐ hidden_size = 8 # ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ ์ด์ œ ์ž…๋ ฅ ํ…์„œ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ํ…์„œ๋Š” (๋ฐฐ์น˜ ํฌ๊ธฐ ร— ์‹œ์ ์˜ ์ˆ˜ ร— ๋งค ์‹œ์ ๋งˆ๋‹ค ๋“ค์–ด๊ฐ€๋Š” ์ž…๋ ฅ)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฐฐ์น˜ ํฌ๊ธฐ๋Š” 1, 10๋ฒˆ์˜ ์‹œ์  ๋™์•ˆ 5์ฐจ์›์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๋„๋ก ํ…์„œ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. # (batch_size, time_steps, input_size) inputs = torch.Tensor(1, 10, 5) ์ด์ œ nn.RNN()์„ ์‚ฌ์šฉํ•˜์—ฌ RNN์˜ ์…€์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ธ์ž๋กœ ์ž…๋ ฅ์˜ ํฌ๊ธฐ, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•ด ์ฃผ๊ณ , batch_first=True๋ฅผ ํ†ตํ•ด์„œ ์ž…๋ ฅ ํ…์„œ์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์ด ๋ฐฐ์น˜ ํฌ๊ธฐ์ž„์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. cell = nn.RNN(input_size, hidden_size, batch_first=True) ์ž…๋ ฅ ํ…์„œ๋ฅผ RNN ์…€์— ์ž…๋ ฅํ•˜์—ฌ ์ถœ๋ ฅ์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. outputs, _status = cell(inputs) RNN ์…€์€ ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์„ ๋ฆฌํ„ดํ•˜๋Š”๋ฐ, ์ฒซ ๋ฒˆ์งธ ๋ฆฌํ„ด ๊ฐ’์€ ๋ชจ๋“  ์‹œ์ (timesteps)์˜ ์€๋‹‰ ์ƒํƒœ๋“ค์ด๋ฉฐ, ๋‘ ๋ฒˆ์งธ ๋ฆฌํ„ด ๊ฐ’์€ ๋งˆ์ง€๋ง‰ ์‹œ์ (timestep)์˜ ์€๋‹‰ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ์ฒซ ๋ฒˆ์งธ ๋ฆฌํ„ด ๊ฐ’์— ๋Œ€ํ•ด์„œ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(outputs.shape) # ๋ชจ๋“  time-step์˜ hidden_state torch.Size([1, 10, 8]) ์ฒซ ๋ฒˆ์งธ ๋ฆฌํ„ด ๊ฐ’์˜ ์€๋‹‰ ์ƒํƒœ๋“ค์€ (1, 10, 8)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋Š” 10๋ฒˆ์˜ ์‹œ์  ๋™์•ˆ 8์ฐจ์›์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฆฌํ„ด ๊ฐ’. ๋‹ค์‹œ ๋งํ•ด ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(_status.shape) # ์ตœ์ข… time-step์˜ hidden_state torch.Size([1, 1, 8]) ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋Š” (1, 1, 8)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 4. ๊นŠ์€ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Deep Recurrent Neural Network) ์•ž์„œ RNN๋„ ๋‹ค์ˆ˜์˜ ์€๋‹‰์ธต์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์—์„œ ์€๋‹‰์ธต์ด 1๊ฐœ ๋” ์ถ”๊ฐ€๋˜์–ด ์€๋‹‰์ธต์ด 2๊ฐœ์ธ ๊นŠ์€(deep) ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ ์ฒซ ๋ฒˆ์งธ ์€๋‹‰์ธต์€ ๋‹ค์Œ ์€๋‹‰์ธต์— ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ ๋‹ค์Œ ์€๋‹‰์ธต์œผ๋กœ ๋ณด๋‚ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊นŠ์€ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์„ ํŒŒ์ด ํ† ์น˜๋กœ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” nn.RNN()์˜ ์ธ์ž์ธ num_layers์— ๊ฐ’์„ ์ „๋‹ฌํ•˜์—ฌ ์ธต์„ ์Œ“์Šต๋‹ˆ๋‹ค. ์ธต์ด 2๊ฐœ์ธ ๊นŠ์€ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์˜ ๊ฒฝ์šฐ, ์•ž์„œ ์‹ค์Šตํ–ˆ๋˜ ์ž„์˜์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅ์ด ์–ด๋–ป๊ฒŒ ๋‹ฌ๋ผ์ง€๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. # (batch_size, time_steps, input_size) inputs = torch.Tensor(1, 10, 5) cell = nn.RNN(input_size = 5, hidden_size = 8, num_layers = 2, batch_first=True) print(outputs.shape) # ๋ชจ๋“  time-step์˜ hidden_state torch.Size([1, 10, 8]) ์ฒซ ๋ฒˆ์งธ ๋ฆฌํ„ด ๊ฐ’์˜ ํฌ๊ธฐ๋Š” ์ธต์ด 1๊ฐœ์˜€๋˜ RNN ์…€ ๋•Œ์™€ ๋‹ฌ๋ผ์ง€์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋งˆ์ง€๋ง‰ ์ธต์˜ ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋“ค์ž…๋‹ˆ๋‹ค. print(_status.shape) # (์ธต์˜ ๊ฐœ์ˆ˜, ๋ฐฐ์น˜ ํฌ๊ธฐ, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ) torch.Size([2, 1, 8]) ๋‘ ๋ฒˆ์งธ ๋ฆฌํ„ด ๊ฐ’์˜ ํฌ๊ธฐ๋Š” ์ธต์ด 1๊ฐœ์˜€๋˜ RNN ์…€ ๋•Œ์™€ ๋‹ฌ๋ผ์กŒ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ํฌ๊ธฐ๋Š” (์ธต์˜ ๊ฐœ์ˆ˜, ๋ฐฐ์น˜ ํฌ๊ธฐ, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ)์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. 5. ์–‘๋ฐฉํ–ฅ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Bidirectional Recurrent Neural Network) ์–‘๋ฐฉํ–ฅ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์€ ์‹œ์  t์—์„œ์˜ ์ถœ๋ ฅ๊ฐ’์„ ์˜ˆ์ธกํ•  ๋•Œ ์ด์ „ ์‹œ์ ์˜ ๋ฐ์ดํ„ฐ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ดํ›„ ๋ฐ์ดํ„ฐ๋กœ๋„ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์•„์ด๋””์–ด์— ๊ธฐ๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. ์˜์–ด ๋นˆ์นธ ์ฑ„์šฐ๊ธฐ ๋ฌธ์ œ์— ๋น„์œ ํ•˜์—ฌ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Exercise is very effective at [ ] belly fat. 1) reducing 2) increasing 3) multiplying '์šด๋™์€ ๋ณต๋ถ€ ์ง€๋ฐฉ์„ [ ] ํšจ๊ณผ์ ์ด๋‹ค'๋ผ๋Š” ์˜์–ด ๋ฌธ์žฅ์ด๊ณ , ์ •๋‹ต์€ reducing(์ค„์ด๋Š” ๊ฒƒ)์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ์˜์–ด ๋นˆ์นธ ์ฑ„์šฐ๊ธฐ ๋ฌธ์ œ๋ฅผ ์ž˜ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์ •๋‹ต์„ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด์ „์— ๋‚˜์˜จ ๋‹จ์–ด๋“ค๋งŒ์œผ๋กœ๋Š” ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. ๋ชฉ์ ์–ด์ธ belly fat(๋ณต๋ถ€ ์ง€๋ฐฉ)๋ฅผ ๋ชจ๋ฅด๋Š” ์ƒํƒœ๋ผ๋ฉด ์ •๋‹ต์„ ๊ฒฐ์ •ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ฆ‰, RNN์ด ๊ณผ๊ฑฐ ์‹œ์ (time step)์˜ ๋ฐ์ดํ„ฐ๋“ค์„ ์ฐธ๊ณ ํ•ด์„œ, ์ฐพ๊ณ ์ž ํ•˜๋Š” ์ •๋‹ต์„ ์˜ˆ์ธกํ•˜์ง€๋งŒ ์‹ค์ œ ๋ฌธ์ œ์—์„œ๋Š” ๊ณผ๊ฑฐ ์‹œ์ ์˜ ๋ฐ์ดํ„ฐ๋งŒ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ–ฅํ›„ ์‹œ์ ์˜ ๋ฐ์ดํ„ฐ์— ํžŒํŠธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด์ „ ์‹œ์ ์˜ ๋ฐ์ดํ„ฐ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ดํ›„ ์‹œ์ ์˜ ๋ฐ์ดํ„ฐ๋„ ํžŒํŠธ๋กœ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ณ ์•ˆ๋œ ๊ฒƒ์ด ์–‘๋ฐฉํ–ฅ RNN์ž…๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ RNN์€ ํ•˜๋‚˜์˜ ์ถœ๋ ฅ๊ฐ’์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‘ ๊ฐœ์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ ์•ž์—์„œ ๋ฐฐ์šด ๊ฒƒ์ฒ˜๋Ÿผ ์•ž ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ(Forward States)๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ํ˜„์žฌ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ฃผํ™ฉ์ƒ‰ ๋ฉ”๋ชจ๋ฆฌ ์…€์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฉ”๋ชจ๋ฆฌ ์…€์€ ์•ž์—์„œ ๋ฐฐ์šด ๊ฒƒ๊ณผ๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์•ž ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ ๋’ค ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ(Backward States)๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ํ˜„์žฌ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ดˆ๋ก์ƒ‰ ๋ฉ”๋ชจ๋ฆฌ ์…€์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‘ ๊ฐœ์˜ ๊ฐ’ ๋ชจ๋‘๊ฐ€ ์ถœ๋ ฅ์ธต์—์„œ ์ถœ๋ ฅ๊ฐ’์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์–‘๋ฐฉํ–ฅ RNN๋„ ๋‹ค์ˆ˜์˜ ์€๋‹‰์ธต์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์–‘๋ฐฉํ–ฅ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์—์„œ ์€๋‹‰์ธต์ด 1๊ฐœ ๋” ์ถ”๊ฐ€๋˜์–ด ์€๋‹‰์ธต์ด 2๊ฐœ์ธ ๊นŠ์€(deep) ์–‘๋ฐฉํ–ฅ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ๋ชจ๋ธ๋“ค๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด์ง€๋งŒ, ์€๋‹‰์ธต์„ ๋ฌด์กฐ๊ฑด ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ํ•ด์„œ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ์ข‹์•„์ง€๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์€๋‹‰์ธต์„ ์ถ”๊ฐ€ํ•˜๋ฉด, ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” ์–‘์ด ๋งŽ์•„์ง€์ง€๋งŒ ๋˜ํ•œ ๋ฐ˜๋Œ€๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๋˜ํ•œ ๊ทธ๋งŒํผ ๋งŽ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์„ ํŒŒ์ด ํ† ์น˜๋กœ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” nn.RNN()์˜ ์ธ์ž์ธ bidirectional์— ๊ฐ’์„ True๋กœ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ธต์ด 2๊ฐœ์ธ ๊นŠ์€ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง์ด๋ฉด์„œ ์–‘๋ฐฉํ–ฅ์ธ ๊ฒฝ์šฐ, ์•ž์„œ ์‹ค์Šตํ–ˆ๋˜ ์ž„์˜์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅ์ด ์–ด๋–ป๊ฒŒ ๋‹ฌ๋ผ์ง€๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. # (batch_size, time_steps, input_size) inputs = torch.Tensor(1, 10, 5) cell = nn.RNN(input_size = 5, hidden_size = 8, num_layers = 2, batch_first=True, bidirectional = True) outputs, _status = cell(inputs) print(outputs.shape) # (๋ฐฐ์น˜ ํฌ๊ธฐ, ์‹œํ€€์Šค ๊ธธ์ด, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ x 2) torch.Size([1, 10, 16]) ์ฒซ ๋ฒˆ์งธ ๋ฆฌํ„ด ๊ฐ’์˜ ํฌ๊ธฐ๋Š” ๋‹จ๋ฑก RNN ์…€ ๋•Œ๋ณด๋‹ค ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ์˜ ๊ฐ’์ด ๋‘ ๋ฐฐ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” (๋ฐฐ์น˜ ํฌ๊ธฐ, ์‹œํ€€์Šค ๊ธธ์ด, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ x 2)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋Š” ์–‘๋ฐฉํ–ฅ์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’๋“ค์ด ์—ฐ๊ฒฐ(concatenate) ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. print(_status.shape) # (์ธต์˜ ๊ฐœ์ˆ˜ x 2, ๋ฐฐ์น˜ ํฌ๊ธฐ, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ) torch.Size([4, 1, 8]) ๋‘ ๋ฒˆ์งธ ๋ฆฌํ„ด ๊ฐ’์˜ ํฌ๊ธฐ๋Š” (์ธต์˜ ๊ฐœ์ˆ˜ x 2, ๋ฐฐ์น˜ ํฌ๊ธฐ, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ)๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋Š” ์ •๋ฐฉํ–ฅ ๊ธฐ์ค€์œผ๋กœ๋Š” ๋งˆ์ง€๋ง‰ ์‹œ์ ์— ํ•ด๋‹น๋˜๋ฉด์„œ, ์—ญ๋ฐฉํ–ฅ ๊ธฐ์ค€์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์— ํ•ด๋‹น๋˜๋Š” ์‹œ์ ์˜ ์ถœ๋ ฅ๊ฐ’์„ ์ธต์˜ ๊ฐœ์ˆ˜๋งŒํผ ์Œ“์•„ ์˜ฌ๋ฆฐ ๊ฒฐ๊ณผ๊ฐ’์ž…๋‹ˆ๋‹ค. 07-02 ์žฅ๋‹จ๊ธฐ ๋ฉ”๋ชจ๋ฆฌ(Long Short-Term Memory, LSTM) ๋ฐ”๋‹๋ผ ์•„์ด์Šคํฌ๋ฆผ์ด ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ง›์„ ๊ฐ€์ง„ ์•„์ด์Šคํฌ๋ฆผ์ธ ๊ฒƒ์ฒ˜๋Ÿผ, ์•ž์„œ ๋ฐฐ์šด RNN์„ ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ํ˜•ํƒœ์˜ RNN์ด๋ผ๊ณ  ํ•˜์—ฌ ๋ฐ”๋‹๋ผ RNN(Vanilla RNN)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. (์ผ€ ๋ผ์Šค์—์„œ๋Š” SimpleRNN) ๋ฐ”๋‹๋ผ RNN ์ดํ›„ ๋ฐ”๋‹๋ผ RNN์˜ ํ•œ๊ณ„๋ฅผ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ RNN์˜ ๋ณ€ํ˜•์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋  LSTM๋„ ๊ทธ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ์„ค๋ช…์—์„œ LSTM๊ณผ ๋น„๊ตํ•˜์—ฌ RNN์„ ์–ธ๊ธ‰ํ•˜๋Š” ๊ฒƒ์€ ์ „๋ถ€ ๋ฐ”๋‹๋ผ RNN์„ ๋งํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฐ”๋‹๋ผ RNN์˜ ํ•œ๊ณ„ ์•ž ์ฑ•ํ„ฐ์—์„œ ๋ฐ”๋‹๋ผ RNN์€ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ์ด์ „์˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ์— ์˜์กดํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ”๋‹๋ผ RNN์€ ๋น„๊ต์  ์งง์€ ์‹œํ€€์Šค(sequence)์— ๋Œ€ํ•ด์„œ๋งŒ ํšจ๊ณผ๋ฅผ ๋ณด์ด๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋‹๋ผ RNN์˜ ์‹œ์ (time step)์ด ๊ธธ์–ด์งˆ์ˆ˜๋ก ์•ž์˜ ์ •๋ณด๊ฐ€ ๋’ค๋กœ ์ถฉ๋ถ„ํžˆ ์ „๋‹ฌ๋˜์ง€ ๋ชปํ•˜๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ฒซ ๋ฒˆ์งธ ์ž…๋ ฅ๊ฐ’์ธ 1 ์˜ ์ •๋ณด๋Ÿ‰์„ ์ง™์€ ๋‚จ์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ–ˆ์„ ๋•Œ, ์ƒ‰์ด ์ ์ฐจ ์–•์•„์ง€๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ ์ด ์ง€๋‚ ์ˆ˜๋ก 1 ์˜ ์ •๋ณด๋Ÿ‰์ด ์†์‹ค๋˜์–ด๊ฐ€๋Š” ๊ณผ์ •์„ ํ‘œํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋’ค๋กœ ๊ฐˆ์ˆ˜๋ก 1 ์˜ ์ •๋ณด๋Ÿ‰์€ ์†์‹ค๋˜๊ณ , ์‹œ์ ์ด ์ถฉ๋ถ„ํžˆ ๊ธด ์ƒํ™ฉ์—์„œ๋Š” 1 ์˜ ์ „์ฒด ์ •๋ณด์— ๋Œ€ํ•œ ์˜ํ–ฅ๋ ฅ์€ ๊ฑฐ์˜ ์˜๋ฏธ๊ฐ€ ์—†์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด์ฉŒ๋ฉด ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ •๋ณด๊ฐ€ ์‹œ์ ์˜ ์•ž ์ชฝ์— ์œ„์น˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. RNN์œผ๋กœ ๋งŒ๋“  ์–ธ์–ด ๋ชจ๋ธ์ด ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ณผ์ •์„ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ''๋ชจ์Šคํฌ๋ฐ”์— ์—ฌํ–‰์„ ์™”๋Š”๋ฐ ๊ฑด๋ฌผ๋„ ์˜ˆ์˜๊ณ  ๋จน์„ ๊ฒƒ๋„ ๋ง›์žˆ์—ˆ์–ด. ๊ทธ๋Ÿฐ๋ฐ ๊ธ€์Ž„ ์ง์žฅ ์ƒ์‚ฌํ•œํ…Œ ์ „ํ™”๊ฐ€ ์™”์–ด. ์–ด๋””๋ƒ๊ณ  ๋ฌป๋”๋ผ๊ณ  ๊ทธ๋ž˜์„œ ๋‚˜๋Š” ๋งํ–ˆ์ง€. ์ € ์—ฌํ–‰ ์™”๋Š”๋ฐ์š”. ์—ฌ๊ธฐ ___'' ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์žฅ์†Œ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์žฅ์†Œ ์ •๋ณด์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด์ธ '๋ชจ์Šคํฌ๋ฐ”'๋Š” ์•ž์— ์œ„์น˜ํ•˜๊ณ  ์žˆ๊ณ , RNN์ด ์ถฉ๋ถ„ํ•œ ๊ธฐ์–ต๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ๋ชปํ•œ๋‹ค๋ฉด ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์—‰๋šฑํ•˜๊ฒŒ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์žฅ๊ธฐ ์˜์กด์„ฑ ๋ฌธ์ œ(the problem of Long-Term Dependencies)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ๋ฐ”๋‹๋ผ RNN ๋‚ด๋ถ€ ์—ด์–ด๋ณด๊ธฐ LSTM์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ธฐ ์ „์— ๋ฐ”๋‹๋ผ RNN์˜ ๋šœ๊ป‘์„ ์—ด์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๋ฐ”๋‹๋ผ RNN์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” RNN ๊ณ„์—ด์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ํŽธํ–ฅ ๋ฅผ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์— ํŽธํ–ฅ ๋ฅผ ๊ทธ๋ฆฐ๋‹ค๋ฉด t ์˜†์— tanh๋กœ ํ–ฅํ•˜๋Š” ๋˜ ํ•˜๋‚˜์˜ ์ž…๋ ฅ์„ ์„ ๊ทธ๋ฆฌ๋ฉด ๋ฉ๋‹ˆ๋‹ค. t t n ( x t W h โˆ’ + ) ๋ฐ”๋‹๋ผ RNN์€ t h โˆ’์ด๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์ด ๊ฐ๊ฐ์˜ ๊ฐ€์ค‘์น˜์™€ ๊ณฑํ•ด์ ธ์„œ ๋ฉ”๋ชจ๋ฆฌ ์…€์˜ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์ด ๊ฐ’์€ ์€๋‹‰์ธต์˜ ์ถœ๋ ฅ์ธ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 3. LSTM(Long Short-Term Memory) ์œ„์˜ ๊ทธ๋ฆผ์€ LSTM์˜ ์ „์ฒด์ ์ธ ๋‚ด๋ถ€์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ „ํ†ต์ ์ธ RNN์˜ ์ด๋Ÿฌํ•œ ๋‹จ์ ์„ ๋ณด์™„ํ•œ RNN์˜ ์ผ์ข…์„ ์žฅ๋‹จ๊ธฐ ๋ฉ”๋ชจ๋ฆฌ(Long Short-Term Memory)๋ผ๊ณ  ํ•˜๋ฉฐ, ์ค„์—ฌ์„œ LSTM์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. LSTM์€ ์€๋‹‰์ธต์˜ ๋ฉ”๋ชจ๋ฆฌ ์…€์— ์ž…๋ ฅ ๊ฒŒ์ดํŠธ, ๋ง๊ฐ ๊ฒŒ์ดํŠธ, ์ถœ๋ ฅ ๊ฒŒ์ดํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๊ธฐ์–ต์„<NAME>๊ณ , ๊ธฐ์–ตํ•ด์•ผ ํ•  ๊ฒƒ๋“ค์„ ์ •ํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด LSTM์€ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์‹์ด ์ „ํ†ต์ ์ธ RNN๋ณด๋‹ค ์กฐ๊ธˆ ๋” ๋ณต์žกํ•ด์กŒ์œผ๋ฉฐ ์…€ ์ƒํƒœ(cell state)๋ผ๋Š” ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” t ์‹œ์ ์˜ ์…€ ์ƒํƒœ๋ฅผ t ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. LSTM์€ RNN๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ธด ์‹œํ€€์Šค์˜ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ํƒ์›”ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ์…€ ์ƒํƒœ๋Š” ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ€๋Š” ๊ตต์€ ์„ ์ž…๋‹ˆ๋‹ค. ์…€ ์ƒํƒœ ๋˜ํ•œ ์ด์ „์— ๋ฐฐ์šด ์€๋‹‰ ์ƒํƒœ์ฒ˜๋Ÿผ ์ด์ „ ์‹œ์ ์˜ ์…€ ์ƒํƒœ๊ฐ€ ๋‹ค์Œ ์‹œ์ ์˜ ์…€ ์ƒํƒœ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์ž…๋ ฅ์œผ๋กœ์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์€๋‹‰ ์ƒํƒœ ๊ฐ’๊ณผ ์…€ ์ƒํƒœ ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ 3๊ฐœ์˜ ๊ฒŒ์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๊ฒŒ์ดํŠธ๋Š” ์‚ญ์ œ ๊ฒŒ์ดํŠธ, ์ž…๋ ฅ ๊ฒŒ์ดํŠธ, ์ถœ๋ ฅ ๊ฒŒ์ดํŠธ๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ ์ด 3๊ฐœ์˜ ๊ฒŒ์ดํŠธ์—๋Š” ๊ณตํ†ต์ ์œผ๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋ฉด 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๋ฐ ์ด ๊ฐ’๋“ค์„ ๊ฐ€์ง€๊ณ  ๊ฒŒ์ดํŠธ๋ฅผ ์กฐ์ ˆํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ๋จผ์ € ์ดํ•ดํ•˜๊ณ  ๊ฐ ๊ฒŒ์ดํŠธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ดํ•˜ ์‹์—์„œ ฯƒ๋Š” ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ดํ•˜ ์‹์—์„œ tanh๋Š” ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. x, x, x, x๋Š” t ์™€ ํ•จ๊ป˜ ๊ฐ ๊ฒŒ์ดํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” 4๊ฐœ์˜ ๊ฐ€์ค‘์น˜์ž…๋‹ˆ๋‹ค. h, h, h, h๋Š” t 1 ์™€ ํ•จ๊ป˜ ๊ฐ ๊ฒŒ์ดํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” 4๊ฐœ์˜ ๊ฐ€์ค‘์น˜์ž…๋‹ˆ๋‹ค. i b, f b๋Š” ๊ฐ ๊ฒŒ์ดํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” 4๊ฐœ์˜ ํŽธํ–ฅ์ž…๋‹ˆ๋‹ค. (1) ์ž…๋ ฅ ๊ฒŒ์ดํŠธ t ฯƒ ( x x + h h โˆ’ + i ) t t n ( x x + h h โˆ’ + g ) ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋Š” ํ˜„์žฌ ์ •๋ณด๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•œ ๊ฒŒ์ดํŠธ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ํ˜„์žฌ ์‹œ์  t์˜ ๊ฐ’๊ณผ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋กœ ์ด์–ด์ง€๋Š” ๊ฐ€์ค‘์น˜ x๋ฅผ ๊ณฑํ•œ ๊ฐ’๊ณผ ์ด์ „ ์‹œ์  t-1์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋กœ ์ด์–ด์ง€๋Š” ๊ฐ€์ค‘์น˜ h๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ๋”ํ•˜์—ฌ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ t ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ ์‹œ์  t์˜ ๊ฐ’๊ณผ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋กœ ์ด์–ด์ง€๋Š” ๊ฐ€์ค‘์น˜ x๋ฅผ ๊ณฑํ•œ ๊ฐ’๊ณผ ์ด์ „ ์‹œ์  t-1์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋กœ ์ด์–ด์ง€๋Š” ๊ฐ€์ค‘์น˜ h๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ๋”ํ•˜์—ฌ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ t ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜ 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’๊ณผ ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜ -1๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’ ๋‘ ๊ฐœ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐœ์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์ด๋ฒˆ์— ์„ ํƒ๋œ ๊ธฐ์–ตํ•  ์ •๋ณด์˜ ์–‘์„ ์ •ํ•˜๋Š”๋ฐ, ๊ตฌ์ฒด์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๊ฒฐ์ •ํ•˜๋Š”์ง€๋Š” ์•„๋ž˜์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋  ์…€ ์ƒํƒœ ์ˆ˜์‹์„ ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. (2) ์‚ญ์ œ ๊ฒŒ์ดํŠธ t ฯƒ ( x x + h h โˆ’ + f ) ์‚ญ์ œ ๊ฒŒ์ดํŠธ๋Š” ๊ธฐ์–ต์„ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•œ ๊ฒŒ์ดํŠธ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ์‹œ์  t์˜ ๊ฐ’๊ณผ ์ด์ „ ์‹œ์  t-1์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋ฉด 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๋ฐ, ์ด ๊ฐ’์ด ๊ณง ์‚ญ์ œ ๊ณผ์ •์„ ๊ฑฐ์นœ ์ •๋ณด์˜ ์–‘์ž…๋‹ˆ๋‹ค. 0์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ์ •๋ณด๊ฐ€ ๋งŽ์ด ์‚ญ์ œ๋œ ๊ฒƒ์ด๊ณ  1์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ์ •๋ณด๋ฅผ ์˜จ์ „ํžˆ ๊ธฐ์–ตํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์…€ ์ƒํƒœ๋ฅผ ๊ตฌํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ๊ตฌ์ฒด์ ์œผ๋กœ๋Š” ์•„๋ž˜์˜ ์…€ ์ƒํƒœ ์ˆ˜์‹์„ ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. (3) ์…€ ์ƒํƒœ(์žฅ๊ธฐ ์ƒํƒœ) t f โˆ˜ t 1 i โˆ˜ t ์…€ ์ƒํƒœ t ๋ฅผ LSTM์—์„œ๋Š” ์žฅ๊ธฐ ์ƒํƒœ๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์…€ ์ƒํƒœ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ญ์ œ ๊ฒŒ์ดํŠธ์—์„œ ์ผ๋ถ€ ๊ธฐ์–ต์„ ์žƒ์€ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ ๊ฒŒ์ดํŠธ์—์„œ ๊ตฌํ•œ t g ์ด ๋‘ ๊ฐœ์˜ ๊ฐ’์— ๋Œ€ํ•ด์„œ ์›์†Œ๋ณ„ ๊ณฑ(entrywise product)์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ฐ™์€ ํฌ๊ธฐ์˜ ๋‘ ํ–‰๋ ฌ์ด ์žˆ์„ ๋•Œ ๊ฐ™์€ ์œ„์น˜์˜ ์„ฑ๋ถ„๋ผ๋ฆฌ ๊ณฑํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‹์œผ๋กœ โˆ˜๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์ด๋ฒˆ์— ์„ ํƒ๋œ ๊ธฐ์–ตํ•  ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ ๊ฒŒ์ดํŠธ์—์„œ ์„ ํƒ๋œ ๊ธฐ์–ต์„ ์‚ญ์ œ ๊ฒŒ์ดํŠธ์˜ ๊ฒฐ๊ด๊ฐ’๊ณผ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์„ ํ˜„์žฌ ์‹œ์  t์˜ ์…€ ์ƒํƒœ๋ผ๊ณ  ํ•˜๋ฉฐ, ์ด ๊ฐ’์€ ๋‹ค์Œ t+1 ์‹œ์ ์˜ LSTM ์…€๋กœ ๋„˜๊ฒจ์ง‘๋‹ˆ๋‹ค. ์‚ญ์ œ ๊ฒŒ์ดํŠธ์™€ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ์˜ ์˜ํ–ฅ๋ ฅ์„ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ๋งŒ์•ฝ ์‚ญ์ œ ๊ฒŒ์ดํŠธ์˜ ์ถœ๋ ฅ๊ฐ’์ธ t ๊ฐ€ 0์ด ๋œ๋‹ค๋ฉด, ์ด์ „ ์‹œ์ ์˜ ์…€ ์ƒํƒœ ๊ฐ’์ธ t 1 ์€ ํ˜„์žฌ ์‹œ์ ์˜ ์…€ ์ƒํƒœ ๊ฐ’์„ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•œ ์˜ํ–ฅ๋ ฅ์ด 0์ด ๋˜๋ฉด์„œ, ์˜ค์ง ์ž…๋ ฅ ๊ฒŒ์ดํŠธ์˜ ๊ฒฐ๊ณผ๋งŒ์ด ํ˜„์žฌ ์‹œ์ ์˜ ์…€ ์ƒํƒœ ๊ฐ’ t ์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์‚ญ์ œ ๊ฒŒ์ดํŠธ๊ฐ€ ์™„์ „ํžˆ ๋‹ซํžˆ๊ณ  ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋ฅผ ์—ฐ ์ƒํƒœ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ์ž…๋ ฅ ๊ฒŒ์ดํŠธ์˜ t ๊ฐ’์„ 0์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ํ˜„์žฌ ์‹œ์ ์˜ ์…€ ์ƒํƒœ ๊ฐ’ t ๋Š” ์˜ค์ง ์ด์ „ ์‹œ์ ์˜ ์…€ ์ƒํƒœ ๊ฐ’ t 1 ์˜ ๊ฐ’์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋ฅผ ์™„์ „ํžˆ ๋‹ซ๊ณ  ์‚ญ์ œ ๊ฒŒ์ดํŠธ๋งŒ์„ ์—ฐ ์ƒํƒœ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ์‚ญ์ œ ๊ฒŒ์ดํŠธ๋Š” ์ด์ „ ์‹œ์ ์˜ ์ž…๋ ฅ์„ ์–ผ๋งˆ๋‚˜ ๋ฐ˜์˜ํ• ์ง€๋ฅผ ์˜๋ฏธํ•˜๊ณ , ์ž…๋ ฅ ๊ฒŒ์ดํŠธ๋Š” ํ˜„์žฌ ์‹œ์ ์˜ ์ž…๋ ฅ์„ ์–ผ๋งˆ๋‚˜ ๋ฐ˜์˜ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. (4) ์ถœ๋ ฅ ๊ฒŒ์ดํŠธ์™€ ์€๋‹‰ ์ƒํƒœ(๋‹จ๊ธฐ ์ƒํƒœ) t ฯƒ ( x x + h h โˆ’ + o ) t o โˆ˜ a h ( t ) ์ถœ๋ ฅ ๊ฒŒ์ดํŠธ๋Š” ํ˜„์žฌ ์‹œ์  t์˜ ๊ฐ’๊ณผ ์ด์ „ ์‹œ์  t-1์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚œ ๊ฐ’์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฐ’์€ ํ˜„์žฌ ์‹œ์  t์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์ผ์— ์“ฐ์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์€๋‹‰ ์ƒํƒœ๋ฅผ ๋‹จ๊ธฐ ์ƒํƒœ๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰ ์ƒํƒœ๋Š” ์žฅ๊ธฐ ์ƒํƒœ์˜ ๊ฐ’์ด ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜ -1๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฐ’์€ ์ถœ๋ ฅ ๊ฒŒ์ดํŠธ์˜ ๊ฐ’๊ณผ ์—ฐ์‚ฐ๋˜๋ฉด์„œ, ๊ฐ’์ด ๊ฑธ๋Ÿฌ์ง€๋Š” ํšจ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋‹จ๊ธฐ ์ƒํƒœ์˜ ๊ฐ’์€ ๋˜ํ•œ ์ถœ๋ ฅ์ธต์œผ๋กœ๋„ ํ–ฅํ•ฉ๋‹ˆ๋‹ค. 4. ํŒŒ์ด ํ† ์น˜์˜ nn.LSTM() ํŒŒ์ด ํ† ์น˜์—์„œ LSTM ์…€์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์— RNN ์…€์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ์„ ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. nn.RNN(input_dim, hidden_size, batch_fisrt=True) LSTM ์…€์€ ์ด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. nn.LSTM(input_dim, hidden_size, batch_fisrt=True) ์ฐธ๊ณ  ์ž๋ฃŒ : http://colah.github.io/posts/2015-08-Understanding-LSTMs/ https://www.quora.com/In-LSTM-how-do-you-figure-out-what-size-the-weights-are-supposed-to-be 07-03 ๊ฒŒ์ดํŠธ ์ˆœํ™˜ ์œ ๋‹›(Gated Recurrent Unit, GRU) GRU(Gated Recurrent Unit)๋Š” 2014๋…„ ๋‰ด์š•๋Œ€ํ•™๊ต ์กฐ๊ฒฝํ˜„ ๊ต์ˆ˜๋‹˜์ด ์ง‘ํ•„ํ•œ ๋…ผ๋ฌธ์—์„œ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. GRU๋Š” LSTM์˜ ์žฅ๊ธฐ ์˜์กด์„ฑ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์„ ์œ ์ง€ํ•˜๋ฉด์„œ, ์€๋‹‰ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ณ„์‚ฐ์„ ์ค„์˜€์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ, GRU๋Š” ์„ฑ๋Šฅ์€ LSTM๊ณผ ์œ ์‚ฌํ•˜๋ฉด์„œ ๋ณต์žกํ–ˆ๋˜ LSTM์˜ ๊ตฌ์กฐ๋ฅผ ๊ฐ„๋‹จํ™” ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. 1. GRU(Gated Recurrent Unit) LSTM์—์„œ๋Š” ์ถœ๋ ฅ, ์ž…๋ ฅ, ์‚ญ์ œ ๊ฒŒ์ดํŠธ๋ผ๋Š” 3๊ฐœ์˜ ๊ฒŒ์ดํŠธ๊ฐ€ ์กด์žฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, GRU์—์„œ๋Š” ์—…๋ฐ์ดํŠธ ๊ฒŒ์ดํŠธ์™€ ๋ฆฌ์…‹ ๊ฒŒ์ดํŠธ ๋‘ ๊ฐ€์ง€ ๊ฒŒ์ดํŠธ๋งŒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. GRU๋Š” LSTM๋ณด๋‹ค ํ•™์Šต ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์ง€๋งŒ ์—ฌ๋Ÿฌ ํ‰๊ฐ€์—์„œ GRU๋Š” LSTM๊ณผ ๋น„์Šทํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ธ๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. t ฯƒ ( x x + h h โˆ’ + r ) t ฯƒ ( x x + h h โˆ’ + z ) t t n ( h ( t h โˆ’ ) W g t b) t ( โˆ’ t ) g + t h โˆ’ GRU์™€ LSTM ์ค‘ ์–ด๋–ค ๊ฒƒ์ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ ๋ฉด์—์„œ ๋” ๋‚ซ๋‹ค๊ณ  ๋‹จ์ • ์ง€์–ด ๋งํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๊ธฐ์กด์— LSTM์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ตœ์ ์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฐพ์•„๋‚ธ ์ƒํ™ฉ์ด๋ผ๋ฉด ๊ตณ์ด GRU๋กœ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๊ฒฝํ—˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ์–‘์ด ์ ์„ ๋•Œ๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ์–‘์ด ์ ์€ GRU๊ฐ€ ์กฐ๊ธˆ ๋” ๋‚ซ๊ณ , ๋ฐ์ดํ„ฐ์–‘์ด ๋” ๋งŽ์œผ๋ฉด LSTM์ด ๋” ๋‚ซ๋‹ค๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. GRU๋ณด๋‹ค LSTM์— ๋Œ€ํ•œ ์—ฐ๊ตฌ๋‚˜ ์‚ฌ์šฉ๋Ÿ‰์ด ๋” ๋งŽ์€๋ฐ, ์ด๋Š” LSTM์ด ๋” ๋จผ์ € ๋‚˜์˜จ ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 2. ํŒŒ์ด ํ† ์น˜์˜ nn.GRU() ํŒŒ์ด ํ† ์น˜์—์„œ GRU ์…€์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์— RNN ์…€์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ์„ ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. nn.RNN(input_dim, hidden_size, batch_fisrt=True) GRU ์…€์€ ์ด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. nn.GRU(input_dim, hidden_size, batch_fisrt=True) 07-04 ๋ฌธ์ž ๋‹จ์œ„ RNN(Char RNN) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋ชจ๋“  ์‹œ์ ์˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ถœ๋ ฅ์„ ํ•˜๋Š” ๋‹ค๋Œ€๋‹ค RNN์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ๋‹ค๋Œ€๋‹ค RNN์€ ๋Œ€ํ‘œ์ ์œผ๋กœ ํ’ˆ์‚ฌ ํƒœ๊น…, ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋“ฑ์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 1. ๋ฌธ์ž ๋‹จ์œ„ RNN(Char RNN) RNN์˜ ์ž…์ถœ๋ ฅ์˜ ๋‹จ์œ„๊ฐ€ ๋‹จ์–ด ๋ ˆ๋ฒจ(word-level)์ด ์•„๋‹ˆ๋ผ ๋ฌธ์ž ๋ ˆ๋ฒจ(character-level)๋กœ ํ•˜์—ฌ RNN์„ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด, ์ด๋ฅผ ๋ฌธ์ž ๋‹จ์œ„ RNN์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. RNN ๊ตฌ์กฐ ์ž์ฒด๊ฐ€ ๋‹ฌ๋ผ์ง„ ๊ฒƒ์€ ์•„๋‹ˆ๊ณ , ์ž…, ์ถœ๋ ฅ์˜ ๋‹จ์œ„๊ฐ€ ๋ฌธ์ž๋กœ ๋ฐ”๋€Œ์—ˆ์„ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž ๋‹จ์œ„ RNN์„ ๋‹ค๋Œ€๋‹ค ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.optim as optim import numpy as np 1. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์ž ์‹œํ€€์Šค apple์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด pple!๋ฅผ ์ถœ๋ ฅํ•˜๋Š” RNN์„ ๊ตฌํ˜„ํ•ด ๋ณผ ๊ฒ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ์–ด๋–ค ์˜๋ฏธ๊ฐ€ ์žˆ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ์ € RNN์˜ ๋™์ž‘์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋ฌธ์ž ์ง‘ํ•ฉ(voabulary)์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ฌธ์ž ์ง‘ํ•ฉ์€ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๋ฌธ์ž๋“ค์˜ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. input_str = 'apple' label_str = 'pple!' char_vocab = sorted(list(set(input_str+label_str))) vocab_size = len(char_vocab) print ('๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(vocab_size)) ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 5 ํ˜„์žฌ ๋ฌธ์ž ์ง‘ํ•ฉ์—๋Š” ์ด 5๊ฐœ์˜ ๋ฌธ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. !, a, e, l, p์ž…๋‹ˆ๋‹ค. ์ด์ œ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•ด ์ค๋‹ˆ๋‹ค. ์ด๋•Œ ์ž…๋ ฅ์€ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์ž…๋ ฅ์˜ ํฌ๊ธฐ๋Š” ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์—ฌ์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. input_size = vocab_size # ์ž…๋ ฅ์˜ ํฌ๊ธฐ๋Š” ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ hidden_size = 5 output_size = 5 learning_rate = 0.1 ์ด์ œ ๋ฌธ์ž ์ง‘ํ•ฉ์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. char_to_index = dict((c, i) for i, c in enumerate(char_vocab)) # ๋ฌธ์ž์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ์ธ๋ฑ์Šค ๋ถ€์—ฌ print(char_to_index) {'!': 0, 'a': 1, 'e': 2, 'l': 3, 'p': 4} !์€ 0, a๋Š” 1, e๋Š” 2, l์€ 3, p๋Š” 4๊ฐ€ ๋ถ€์—ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ๋ฌธ์ž ์‹œํ€€์Šค๋กœ ๋ณด๊ธฐ ์œ„ํ•ด์„œ ๋ฐ˜๋Œ€๋กœ ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” index_to_char์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. index_to_char={} for key, value in char_to_index.items(): index_to_char[value] = key print(index_to_char) {0: '!', 1: 'a', 2: 'e', 3: 'l', 4: 'p'} ์ด์ œ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ์˜ ๊ฐ ๋ฌธ์ž๋“ค์„ ์ •์ˆ˜๋กœ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค. x_data = [char_to_index[c] for c in input_str] y_data = [char_to_index[c] for c in label_str] print(x_data) print(y_data) [1, 4, 4, 3, 2] # a, p, p, l, e์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. [4, 4, 3, 2, 0] # p, p, l, e, !์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜์˜ nn.RNN()์€ ๊ธฐ๋ณธ์ ์œผ๋กœ 3์ฐจ์› ํ…์„œ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์น˜ ์ฐจ์›์„ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค. # ๋ฐฐ์น˜ ์ฐจ์› ์ถ”๊ฐ€ # ํ…์„œ ์—ฐ์‚ฐ์ธ unsqueeze(0)๋ฅผ ํ†ตํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ์—ˆ์Œ. x_data = [x_data] y_data = [y_data] print(x_data) print(y_data) [[1, 4, 4, 3, 2]] [[4, 4, 3, 2, 0]] ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ฐ ๋ฌธ์ž๋“ค์„ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. x_one_hot = [np.eye(vocab_size)[x] for x in x_data] print(x_one_hot) [array([[0., 1., 0., 0., 0.], [0., 0., 0., 0., 1.], [0., 0., 0., 0., 1.], [0., 0., 0., 1., 0.], [0., 0., 1., 0., 0.]])] ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ํ…์„œ๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. X = torch.FloatTensor(x_one_hot) Y = torch.LongTensor(y_data) ์ด์ œ ๊ฐ ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : {}'.format(X.shape)) print('๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(Y.shape)) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : torch.Size([1, 5, 5]) ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : torch.Size([1, 5]) 2. ๋ชจ๋ธ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด์ œ RNN ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์•„๋ž˜์—์„œ fc๋Š” ์™„์ „ ์—ฐ๊ฒฐ์ธต(fully-connected layer)์„ ์˜๋ฏธํ•˜๋ฉฐ ์ถœ๋ ฅ์ธต์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. class Net(torch.nn.Module): def __init__(self, input_size, hidden_size, output_size): super(Net, self).__init__() self.rnn = torch.nn.RNN(input_size, hidden_size, batch_first=True) # RNN ์…€ ๊ตฌํ˜„ self.fc = torch.nn.Linear(hidden_size, output_size, bias=True) # ์ถœ๋ ฅ์ธต ๊ตฌํ˜„ def forward(self, x): # ๊ตฌํ˜„ํ•œ RNN ์…€๊ณผ ์ถœ๋ ฅ์ธต์„ ์—ฐ๊ฒฐ x, _status = self.rnn(x) x = self.fc(x) return x ํด๋ž˜์Šค๋กœ ์ •์˜ํ•œ ๋ชจ๋ธ์„ net์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. net = Net(input_size, hidden_size, output_size) ์ด์ œ ์ž…๋ ฅ๋œ ๋ชจ๋ธ์— ์ž…๋ ฅ์„ ๋„ฃ์–ด์„œ ์ถœ๋ ฅ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. outputs = net(X) print(outputs.shape) # 3์ฐจ์› ํ…์„œ torch.Size([1, 5, 5]) (1, 5, 5)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ ๊ฐ๊ฐ ๋ฐฐ์น˜ ์ฐจ์›, ์‹œ์ (timesteps), ์ถœ๋ ฅ์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•  ๋•Œ๋Š” ์ด๋ฅผ ๋ชจ๋‘ ํŽผ์ณ์„œ ๊ณ„์‚ฐํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋•Œ๋Š” view๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์น˜ ์ฐจ์›๊ณผ ์‹œ์  ์ฐจ์›์„ ํ•˜๋‚˜๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. print(outputs.view(-1, input_size).shape) # 2์ฐจ์› ํ…์„œ๋กœ ๋ณ€ํ™˜ torch.Size([5, 5]) ์ฐจ์›์ด (5, 5)๊ฐ€ ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ๋‹ค์‹œ ๋ณต์Šต ๋ด…์‹œ๋‹ค. print(Y.shape) print(Y.view(-1).shape) torch.Size([1, 5]) torch.Size([5]) ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋Š” (1, 5)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ, ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‚˜์ค‘์— ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•  ๋•Œ๋Š” ์ด๊ฑธ ํŽผ์ณ์„œ ๊ณ„์‚ฐํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ (5)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ์˜ตํ‹ฐ๋งˆ์ด์ €์™€ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. criterion = torch.nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), learning_rate) ์ด 100๋ฒˆ์˜ ์—ํฌํฌ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. for i in range(100): optimizer.zero_grad() outputs = net(X) loss = criterion(outputs.view(-1, input_size), Y.view(-1)) # view๋ฅผ ํ•˜๋Š” ์ด์œ ๋Š” Batch ์ฐจ์› ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด loss.backward() # ๊ธฐ์šธ๊ธฐ ๊ณ„์‚ฐ optimizer.step() # ์•„๊นŒ optimizer ์„ ์–ธ ์‹œ ๋„ฃ์–ด๋‘” ํŒŒ๋ผ๋ฏธํ„ฐ ์—…๋ฐ์ดํŠธ # ์•„๋ž˜ ์„ธ ์ค„์€ ๋ชจ๋ธ์ด ์‹ค์ œ ์–ด๋–ป๊ฒŒ ์˜ˆ์ธกํ–ˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ. result = outputs.data.numpy().argmax(axis=2) # ์ตœ์ข… ์˜ˆ์ธก๊ฐ’์ธ ๊ฐ time-step ๋ณ„ 5์ฐจ์› ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ๊ฐ€์žฅ ๋†’์€ ๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ์„ ํƒ result_str = ''.join([index_to_char[c] for c in np.squeeze(result)]) print(i, "loss: ", loss.item(), "prediction: ", result, "true Y: ", y_data, "prediction str: ", result_str) 0 loss: 1.3871121406555176 prediction: [[4 4 0 4 0]] true Y: [[4, 4, 3, 2, 0]] prediction str: pp! p! ... ์ค‘๋žต ... 99 loss: 0.0003285939747001976 prediction: [[4 4 3 2 0]] true Y: [[4, 4, 3, 2, 0]] prediction str: pple! 07-05 ๋ฌธ์ž ๋‹จ์œ„ RNN(Char RNN) - ๋” ๋งŽ์€ ๋ฐ์ดํ„ฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋” ๋งŽ์€ ๋ฐ์ดํ„ฐ ๋ฌธ์ž ๋‹จ์œ„ RNN์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. 1. ๋ฌธ์ž ๋‹จ์œ„ RNN(Char RNN) ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.optim as optim ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž„์˜์˜ ์ƒ˜ํ”Œ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. 1. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ sentence = ("if you want to build a ship, don't drum up people together to " "collect wood and don't assign them tasks and work, but rather " "teach them to long for the endless immensity of the sea.") ๋ฌธ์ž ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•˜๊ณ , ๊ฐ ๋ฌธ์ž์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. char_set = list(set(sentence)) # ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๋ฌธ์ž ์ง‘ํ•ฉ ์ƒ์„ฑ char_dic = {c: i for i, c in enumerate(char_set)} # ๊ฐ ๋ฌธ์ž์— ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ print(char_dic) # ๊ณต๋ฐฑ๋„ ์—ฌ๊ธฐ์„œ๋Š” ํ•˜๋‚˜์˜ ์›์†Œ {'k': 0, 'o': 1, 'r': 2, 'a': 3, 'f': 4, 'b': 5, 'g': 6, 'w': 7, ',': 8, ' ': 9, 'h': 10, 'l': 11, "'": 12, 'e': 13, '.': 14, 'd': 15, 's': 16, 'y': 17, 'u': 18, 't': 19, 'n': 20, 'i': 21, 'm': 22, 'c': 23, 'p': 24} ๊ฐ ๋ฌธ์ž์— ์ •์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋˜์—ˆ์œผ๋ฉฐ, ์ด 25๊ฐœ์˜ ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. dic_size = len(char_dic) print('๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(dic_size)) ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 25 ๋ฌธ์ž ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 25์ด๋ฉฐ, ์ž…๋ ฅ์„ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์ด๋Š” ๋งค ์‹œ์ ๋งˆ๋‹ค ๋“ค์–ด๊ฐˆ ์ž…๋ ฅ์˜ ํฌ๊ธฐ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. hidden_size(์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ)๋ฅผ ์ž…๋ ฅ์˜ ํฌ๊ธฐ์™€ ๋™์ผํ•˜๊ฒŒ ์คฌ๋Š”๋ฐ, ์ด๋Š” ์‚ฌ์šฉ์ž์˜ ์„ ํƒ์œผ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ์ค˜๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  sequence_length๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ–ˆ๋Š”๋ฐ, ์šฐ๋ฆฌ๊ฐ€ ์•ž์„œ ๋งŒ๋“  ์ƒ˜ํ”Œ์„ 10๊ฐœ ๋‹จ์œ„๋กœ ๋Š์–ด์„œ ์ƒ˜ํ”Œ์„ ๋งŒ๋“ค ์˜ˆ์ •์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋’ค์—์„œ ๋” ์ž์„ธํžˆ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์„ค์ • hidden_size = dic_size sequence_length = 10 # ์ž„์˜ ์ˆซ์ž ์ง€์ • learning_rate = 0.1 ๋‹ค์Œ์€ ์ž„์˜๋กœ ์ง€์ •ํ•œ sequence_length ๊ฐ’์ธ 10์˜ ๋‹จ์œ„๋กœ ์ƒ˜ํ”Œ๋“ค์„ ์ž˜๋ผ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. # ๋ฐ์ดํ„ฐ ๊ตฌ์„ฑ x_data = [] y_data = [] for i in range(0, len(sentence) - sequence_length): x_str = sentence[i:i + sequence_length] y_str = sentence[i + 1: i + sequence_length + 1] print(i, x_str, '->', y_str) x_data.append([char_dic[c] for c in x_str]) # x str to index y_data.append([char_dic[c] for c in y_str]) # y str to index 0 if you wan -> f you want 1 f you want -> you want 2 you want -> you want t 3 you want t -> ou want to 4 ou want to -> u want to ... ์ค‘๋žต ... 165 ity of the -> ty of the 166 ty of the -> y of the s 167 y of the s -> of the se 168 of the se -> of the sea 169 of the sea -> f the sea. ์ด 170๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์ƒ˜ํ”Œ์˜ ๊ฐ ๋ฌธ์ž๋“ค์€ ๊ณ ์œ ํ•œ ์ •์ˆ˜๋กœ ์ธ์ฝ”๋”ฉ์ด ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(x_data[0]) print(y_data[0]) [21, 4, 9, 17, 1, 18, 9, 7, 3, 20] # if you wan์— ํ•ด๋‹น๋จ. [4, 9, 17, 1, 18, 9, 7, 3, 20, 19] # f you want์— ํ•ด๋‹น๋จ. ํ•œ ์นธ์”ฉ ์‹œํ”„ํŠธ ๋œ ์‹œํ€€์Šค๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ž…๋ ฅ ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. x_one_hot = [np.eye(dic_size)[x] for x in x_data] # x ๋ฐ์ดํ„ฐ๋Š” ์›-ํ•ซ ์ธ์ฝ”๋”ฉ X = torch.FloatTensor(x_one_hot) Y = torch.LongTensor(y_data) ์ด์ œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : {}'.format(X.shape)) print('๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : {}'.format(Y.shape)) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : torch.Size([170, 10, 25]) ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : torch.Size([170, 10]) ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด์„œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(X[0]) tensor([ [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], # i [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], # f [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], # ๊ณต๋ฐฑ [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], # y [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], # o [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], # y [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], # ๊ณต๋ฐฑ [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], # w [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], # a [0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) # n ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๋„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(Y[0]) tensor([ 1, 2, 5, 21, 14, 2, 16, 19, 9, 12]) ์œ„ ๋ ˆ์ด๋ธ” ์‹œํ€€์Šค๋Š” f you want์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. 2. ๋ชจ๋ธ ๊ตฌํ˜„ํ•˜๊ธฐ ๋ชจ๋ธ์€ ์•ž์„œ ์‹ค์Šตํ•œ ๋ฌธ์ž ๋‹จ์œ„ RNN ์ฑ•ํ„ฐ์™€ ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ด๋ฒˆ์—๋Š” ์€๋‹‰์ธต์„ ๋‘ ๊ฐœ ์Œ“์„ ๊ฒ๋‹ˆ๋‹ค. class Net(torch.nn.Module): def __init__(self, input_dim, hidden_dim, layers): # ํ˜„์žฌ hidden_size๋Š” dic_size์™€ ๊ฐ™์Œ. super(Net, self).__init__() self.rnn = torch.nn.RNN(input_dim, hidden_dim, num_layers=layers, batch_first=True) self.fc = torch.nn.Linear(hidden_dim, hidden_dim, bias=True) def forward(self, x): x, _status = self.rnn(x) x = self.fc(x) return x net = Net(dic_size, hidden_size, 2) # ์ด๋ฒˆ์—๋Š” ์ธต์„ ๋‘ ๊ฐœ ์Œ“์Šต๋‹ˆ๋‹ค. nn.RNN() ์•ˆ์— num_layers๋ผ๋Š” ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์€๋‹‰์ธต์„ ๋ช‡ ๊ฐœ ์Œ“์„ ๊ฒƒ์ธ์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ์„ ์–ธ ์‹œ layers๋ผ๋Š” ์ธ์ž์— 2๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์€๋‹‰์ธต์„ ๋‘ ๊ฐœ ์Œ“์Šต๋‹ˆ๋‹ค. ๋น„์šฉ ํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. criterion = torch.nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), learning_rate) ์ด์ œ ๋ชจ๋ธ์— ์ž…๋ ฅ์„ ๋„ฃ์–ด์„œ ์ถœ๋ ฅ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. outputs = net(X) print(outputs.shape) # 3์ฐจ์› ํ…์„œ torch.Size([170, 10, 25]) (170, 10, 25)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ ๊ฐ๊ฐ ๋ฐฐ์น˜ ์ฐจ์›, ์‹œ์ (timesteps), ์ถœ๋ ฅ์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•  ๋•Œ๋Š” ์ด๋ฅผ ๋ชจ๋‘ ํŽผ์ณ์„œ ๊ณ„์‚ฐํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋•Œ๋Š” view๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์น˜ ์ฐจ์›๊ณผ ์‹œ์  ์ฐจ์›์„ ํ•˜๋‚˜๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. print(outputs.view(-1, dic_size).shape) # 2์ฐจ์› ํ…์„œ๋กœ ๋ณ€ํ™˜. torch.Size([1700, 25]) ์ฐจ์›์ด (1700, 25)๊ฐ€ ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ๋‹ค์‹œ ๋ณต์Šต ๋ด…์‹œ๋‹ค. print(Y.shape) print(Y.view(-1).shape) torch.Size([170, 10]) torch.Size([1700]) ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋Š” (170, 10)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ, ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‚˜์ค‘์— ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•  ๋•Œ๋Š” ์ด๊ฑธ ํŽผ์ณ์„œ ๊ณ„์‚ฐํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ (1700)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ์˜ตํ‹ฐ๋งˆ์ด์ €์™€ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. for i in range(100): optimizer.zero_grad() outputs = net(X) # (170, 10, 25) ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ ํ…์„œ๋ฅผ ๋งค ์—ํฌํฌ๋งˆ๋‹ค ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ loss = criterion(outputs.view(-1, dic_size), Y.view(-1)) loss.backward() optimizer.step() # results์˜ ํ…์„œ ํฌ๊ธฐ๋Š” (170, 10) results = outputs.argmax(dim=2) predict_str = "" for j, result in enumerate(results): if j == 0: # ์ฒ˜์Œ์—๋Š” ์˜ˆ์ธก ๊ฒฐ๊ณผ๋ฅผ ์ „๋ถ€ ๊ฐ€์ ธ์˜ค์ง€๋งŒ predict_str += ''.join([char_set[t] for t in result]) else: # ๊ทธ๋‹ค์Œ์—๋Š” ๋งˆ์ง€๋ง‰ ๊ธ€์ž๋งŒ ๋ฐ˜๋ณต ์ถ”๊ฐ€ predict_str += char_set[result[-1]] print(predict_str) hahhahrrhhhahaahahhhhahhahhhhhhhhhahhahhhhhhhahrahhahhhahhhhaahhhrhahhahahhahhhhhhhhaahhhhhhhahhhhahhhhahhhrhhhhhhahhhahahhhhaahahhahhhhaahahhahahhhahhhhhhahhahahhhhhhhahhhhahhhaa ... ์ค‘๋žต ... p you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. ์ฒ˜์Œ์—๋Š” ์ด์ƒํ•œ ์˜ˆ์ธก์„ ํ•˜์ง€๋งŒ ๋งˆ์ง€๋ง‰ ์—ํฌํฌ์—์„œ๋Š” ๊ฝค ์ •ํ™•ํ•œ ๋ฌธ์ž์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 08. [DL ์ž…๋ฌธ ] - ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง(Convolutional Neural Network) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ฃผ๋กœ ์ด๋ฏธ์ง€, ๋น„์ „ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” (ํ•˜์ง€๋งŒ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ๋„ ์ผ๋ถ€ ์‚ฌ์šฉ๋˜๋Š”) ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง(Convolutional Neural Network)์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. 08-01 ํ•ฉ์„ฑ๊ณฑ๊ณผ ํ’€๋ง(Convolution and Pooling) ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง(Convolutional Neural Network)์€ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์— ํƒ์›”ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š” ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์€ ํฌ๊ฒŒ ํ•ฉ์„ฑ๊ณฑ์ธต๊ณผ(Convolution layer)์™€ ํ’€๋ง์ธต(Pooling layer)์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ์ผ๋ฐ˜์ ์ธ ์˜ˆ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. (http://cs231n.github.io/convolutional-networks) ์œ„์˜ ๊ทธ๋ฆผ์—์„œ CONV๋Š” ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์˜๋ฏธํ•˜๊ณ , ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๊ฐ€ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ReLU๋ฅผ ์ง€๋‚ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ณผ์ •์„ ํ•ฉ์„ฑ๊ณฑ์ธต์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„์— POOL์ด๋ผ๋Š” ๊ตฌ๊ฐ„์„ ์ง€๋‚˜๋Š”๋ฐ ์ด๋Š” ํ’€๋ง ์—ฐ์‚ฐ์„ ์˜๋ฏธํ•˜๋ฉฐ ํ’€๋ง์ธต์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๊ณผ ํ’€๋ง ์—ฐ์‚ฐ์˜ ์˜๋ฏธ์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ๋Œ€๋‘ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์€ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์— ํƒ์›”ํ•œ ์„ฑ๋Šฅ์„ ๋ณด์ด๋Š” ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ž์„œ ๋ฐฐ์šด ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ํ•œ๊ณ„๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•ŒํŒŒ๋ฒณ ์†๊ธ€์”จ๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์•ŒํŒŒ๋ฒณ Y๋ฅผ ๋น„๊ต์  ์ •์ž๋กœ ์“ด ์†๊ธ€์”จ์™€ ๋‹ค์†Œ ํœ˜๊ฐˆ๊ฒจ ์“ด ์†๊ธ€์”จ ๋‘ ๊ฐœ๋ฅผ 2์ฐจ์› ํ…์„œ์ธ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌ๋žŒ์ด ๋ณด๊ธฐ์—๋Š” ๋‘ ๊ทธ๋ฆผ ๋ชจ๋‘ ์•ŒํŒŒ๋ฒณ Y๋กœ ์†์‰ฝ๊ฒŒ ํŒ๋‹จ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๊ธฐ๊ณ„๊ฐ€ ๋ณด๊ธฐ์—๋Š” ๊ฐ ํ”ฝ์…€๋งˆ๋‹ค ๊ฐ€์ง„ ๊ฐ’์ด ๊ฑฐ์˜ ์ƒ์ดํ•˜๋ฏ€๋กœ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์ง„ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋ฏธ์ง€๋ผ๋Š” ๊ฒƒ์€ ์œ„์™€ ๊ฐ™์ด ๊ฐ™์€ ๋Œ€์ƒ์ด๋ผ๋„ ํœ˜์–ด์ง€๊ฑฐ๋‚˜, ์ด๋™๋˜์—ˆ๊ฑฐ๋‚˜, ๋ฐฉํ–ฅ์ด ๋’คํ‹€๋ ธ๊ฑฐ๋‚˜ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ณ€ํ˜•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์€ ๋ช‡ ๊ฐ€์ง€ ํ”ฝ์…€๋งŒ ๊ฐ’์ด ๋‹ฌ๋ผ์ ธ๋„ ๋ฏผ๊ฐํ•˜๊ฒŒ ์˜ˆ์ธก์— ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ข€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ์†๊ธ€์”จ๋ฅผ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ ๋ถ„๋ฅ˜ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด, ์ด๋ฏธ์ง€๋ฅผ 1์ฐจ์› ํ…์„œ์ธ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ์ž…๋ ฅ์ธต์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์†๊ธ€์”จ๋ฅผ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฒกํ„ฐ๋กœ ๋ฐ”๊พธ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1์ฐจ์›์œผ๋กœ ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ๋Š” ์‚ฌ๋žŒ์ด ๋ณด๊ธฐ์—๋„ ์ด๊ฒŒ ์›๋ž˜ ์–ด๋–ค ์ด๋ฏธ์ง€์˜€๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ธฐ๊ณ„๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ๊ฒฐ๊ณผ๋Š” ๋ณ€ํ™˜ ์ „์— ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๊ณต๊ฐ„์ ์ธ ๊ตฌ์กฐ(spatial structure) ์ •๋ณด๊ฐ€ ์œ ์‹ค๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ณต๊ฐ„์ ์ธ ๊ตฌ์กฐ ์ •๋ณด๋ผ๋Š” ๊ฒƒ์€ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€๊นŒ์šด ์–ด๋–ค ํ”ฝ์…€๋“ค๋ผ๋ฆฌ๋Š” ์–ด๋–ค ์—ฐ๊ด€์ด ์žˆ๊ณ , ์–ด๋–ค ํ”ฝ์…€๋“ค๋ผ๋ฆฌ๋Š” ๊ฐ’์ด ๋น„์Šทํ•˜๊ฑฐ๋‚˜ ๋“ฑ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด๋ฏธ์ง€์˜ ๊ณต๊ฐ„์ ์ธ ๊ตฌ์กฐ ์ •๋ณด๋ฅผ ๋ณด์กดํ•˜๋ฉด์„œ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•ด์กŒ๊ณ , ์ด๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. 2. ์ฑ„๋„(Channel) ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์˜ ๊ธฐ๋ณธ์ ์ธ ์šฉ์–ด์ธ ์ฑ„๋„์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ๊ธ€์ž๋‚˜ ์ด๋ฏธ์ง€๋ณด๋‹ค ์ˆซ์ž. ๋‹ค์‹œ ๋งํ•ด, ํ…์„œ๋ฅผ ๋” ์ž˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋Š” (๋†’์ด, ๋„ˆ๋น„, ์ฑ„๋„)์ด๋ผ๋Š” 3์ฐจ์› ํ…์„œ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋†’์ด๋Š” ์ด๋ฏธ์ง€์˜ ์„ธ๋กœ ๋ฐฉํ–ฅ ํ”ฝ์…€ ์ˆ˜, ๋„ˆ๋น„๋Š” ์ด๋ฏธ์ง€์˜ ๊ฐ€๋กœ ๋ฐฉํ–ฅ ํ”ฝ์…€ ์ˆ˜, ์ฑ„๋„์€ ์ƒ‰ ์„ฑ๋ถ„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ‘๋ฐฑ ์ด๋ฏธ์ง€๋Š” ์ฑ„๋„ ์ˆ˜๊ฐ€ 1์ด๋ฉฐ, ๊ฐ ํ”ฝ์…€์€ 0๋ถ€ํ„ฐ 255 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” 28 ร— 28 ํ”ฝ์…€์˜ ์†๊ธ€์”จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ์†๊ธ€์”จ ๋ฐ์ดํ„ฐ๋Š” ํ‘๋ฐฑ ์ด๋ฏธ์ง€๋ฏ€๋กœ ์ฑ„๋„ ์ˆ˜๊ฐ€ 1์ž„์„ ๊ณ ๋ คํ•˜๋ฉด (28 ร— 28 ร— 1)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 3์ฐจ์› ํ…์„œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ‘๋ฐฑ์ด ์•„๋‹ˆ๋ผ ์šฐ๋ฆฌ๊ฐ€ ํ†ต์ƒ์ ์œผ๋กœ ์ ‘ํ•˜๊ฒŒ ๋˜๋Š” ์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€๋Š” ์–ด๋–จ๊นŒ์š”? ์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€๋Š” ์ ์ƒ‰(Red), ๋…น์ƒ‰(Green), ์ฒญ์ƒ‰(Blue) ์ฑ„๋„ ์ˆ˜๊ฐ€ 3๊ฐœ์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ํ”ฝ์…€์€ ์„ธ ๊ฐ€์ง€ ์ƒ‰๊น”, ์‚ผ์›์ƒ‰์˜ ์กฐํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋†’์ด๊ฐ€ 28, ๋„ˆ๋น„๊ฐ€ 28์ธ ์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด ์ด๋ฏธ์ง€์˜ ํ…์„œ๋Š” (28 ร— 28 ร— 3)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” 3์ฐจ์› ํ…์„œ์ž…๋‹ˆ๋‹ค. ์ฑ„๋„์€ ๋•Œ๋กœ๋Š” ๊นŠ์ด(depth)๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ด๋ฏธ์ง€๋Š” (๋†’์ด, ๋„ˆ๋น„, ๊นŠ์ด)๋ผ๋Š” 3์ฐจ์› ํ…์„œ๋กœ ํ‘œํ˜„๋œ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. 3. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ(Convolution operation) ํ•ฉ์„ฑ๊ณฑ์ธต์€ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ ์ด๋ฏธ์ง€์˜ ํŠน์ง•์„ ์ถ”์ถœํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„ , ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ํ•ฉ์„ฑ ๊ณฑ์€ ์˜์–ด๋กœ ์ปจ๋ณผ๋ฃจ์…˜์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š”๋ฐ, ์ปค๋„(kernel) ๋˜๋Š” ํ•„ํ„ฐ(filter)๋ผ๋Š” ร— ํฌ๊ธฐ์˜ ํ–‰๋ ฌ๋กœ ๋†’์ด ๋„ˆ๋น„ ์ด ( e g t ) ๋„ˆ ( i t) ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๊ฒน์น˜๋ฉฐ ํ›‘์œผ๋ฉด์„œ ร— ํฌ๊ธฐ์˜ ๊ฒน์ณ์ง€๋Š” ๋ถ€๋ถ„์˜ ๊ฐ ์ด๋ฏธ์ง€์™€ ์ปค๋„์˜ ์›์†Œ์˜ ๊ฐ’์„ ๊ณฑํ•ด์„œ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ์ด๋ฏธ์ง€์˜ ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„๋ถ€ํ„ฐ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ํ›‘์Šต๋‹ˆ๋‹ค. ์ปค๋„(kernel)์€ ์ผ๋ฐ˜์ ์œผ๋กœ 3 ร— 3 ๋˜๋Š” 5 ร— 5๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์•„๋ž˜๋Š” ร— ํฌ๊ธฐ์˜ ์ปค๋„๋กœ ร—์˜ ์ด๋ฏธ์ง€ ํ–‰๋ ฌ์— ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ 1 ์Šคํ…(step)์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๋„ค ๋ฒˆ์งธ ์Šคํ…๊นŒ์ง€ ์ด๋ฏธ์ง€์™€ ์‹์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. 1. ์ฒซ ๋ฒˆ์งธ ์Šคํ… (1ร—1) + (2ร—0) + (3ร—1) + (2ร—1) + (1ร—0) + (0ร—1) + (3ร—0) + (0ร—1) + (1ร—0) = 6 2. ๋‘ ๋ฒˆ์งธ ์Šคํ… (2ร—1) + (3ร—0) + (4ร—1) + (1ร—1) + (0ร—0) + (1ร—1) + (0ร—0) + (1ร—1) + (1ร—0) = 9 3. ์„ธ ๋ฒˆ์งธ ์Šคํ… (3ร—1) + (4ร—0) + (5ร—1) + (0ร—1) + (1ร—0) + (2ร—1) + (1ร—0) + (1ร—1) + (0ร—0) = 11 4. ๋„ค ๋ฒˆ์งธ ์Šคํ… (2ร—1) + (1ร—0) + (0ร—1) + (3ร—1) + (0ร—0) + (1ร—1) + (1ร—0) + (4ร—1) + (1ร—0) = 10 ์œ„ ์—ฐ์‚ฐ์„ ์ด 9๋ฒˆ์˜ ์Šคํ…๊นŒ์ง€ ๋งˆ์ณค๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์˜€์„ ๋•Œ, ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ปค๋„์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ฅผ ํŠน์„ฑ ๋งต(feature map)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ 3 ร— 3์ด์—ˆ์ง€๋งŒ, ์ปค๋„์˜ ํฌ๊ธฐ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ปค๋„์˜ ์ด๋™ ๋ฒ”์œ„๊ฐ€ ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” ํ•œ ์นธ์ด์—ˆ์ง€๋งŒ, ์ด ๋˜ํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด๋™ ๋ฒ”์œ„๋ฅผ ์ŠคํŠธ๋ผ์ด๋“œ(stride)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ๋Š” ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ 2์ผ ๊ฒฝ์šฐ์— 5 ร— 5 ์ด๋ฏธ์ง€์— ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” 3 ร— 3 ์ปค๋„์˜ ์›€์ง์ž„์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ 2 ร— 2์˜ ํฌ๊ธฐ์˜ ํŠน์„ฑ ๋งต์„ ์–ป์Šต๋‹ˆ๋‹ค. 4. ํŒจ๋”ฉ(Padding) ์œ„์˜ ์˜ˆ์—์„œ 5 ร— 5 ์ด๋ฏธ์ง€์— 3 ร— 3์˜ ์ปค๋„๋กœ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜์˜€์„ ๋•Œ, ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ 1์ผ ๊ฒฝ์šฐ์—๋Š” 3 ร— 3์˜ ํŠน์„ฑ ๋งต์„ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋กœ ์–ป์€ ํŠน์„ฑ ๋งต์€ ์ž…๋ ฅ๋ณด๋‹ค ํฌ๊ธฐ๊ฐ€ ์ž‘์•„์ง„๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ํ•ฉ์„ฑ๊ณฑ ์ธต์„ ์—ฌ๋Ÿฌ ๊ฐœ ์Œ“์•˜๋‹ค๋ฉด ์ตœ์ข…์ ์œผ๋กœ ์–ป์€ ํŠน์„ฑ ๋งต์€ ์ดˆ๊ธฐ ์ž…๋ ฅ๋ณด๋‹ค ๋งค์šฐ ์ž‘์•„์ง„ ์ƒํƒœ๊ฐ€ ๋ผ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ์ดํ›„์—๋„ ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๊ฐ€ ์ž…๋ ฅ์˜ ํฌ๊ธฐ์™€ ๋™์ผํ•˜๊ฒŒ ์œ ์ง€๋˜๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ํŒจ๋”ฉ(padding)์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํŒจ๋”ฉ์€ (ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜๊ธฐ ์ „์—) ์ž…๋ ฅ์˜ ๊ฐ€์žฅ์ž๋ฆฌ์— ์ง€์ •๋œ ๊ฐœ์ˆ˜์˜ ํญ๋งŒํผ ํ–‰๊ณผ ์—ด์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ข€ ๋” ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด ์ง€์ •๋œ ๊ฐœ์ˆ˜์˜ ํญ๋งŒํผ ํ…Œ๋‘๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ ๊ฐ’์„ 0์œผ๋กœ ์ฑ„์šฐ๋Š” ์ œ๋กœ ํŒจ๋”ฉ(zero padding)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ 5 ร— 5 ์ด๋ฏธ์ง€์— 1ํญ์งœ๋ฆฌ ์ œ๋กœ ํŒจ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ์œ„, ์•„๋ž˜์— ํ•˜๋‚˜์˜ ํ–‰์„ ์ขŒ, ์šฐ์— ํ•˜๋‚˜์˜ ์—ด์„ ์ถ”๊ฐ€ํ•œ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ปค๋„์€ ์ฃผ๋กœ 3 ร— 3 ๋˜๋Š” 5 ร— 5๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ 1์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, 3 ร— 3 ํฌ๊ธฐ์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด 1ํญ์งœ๋ฆฌ ์ œ๋กœ ํŒจ๋”ฉ์„ ์‚ฌ์šฉํ•˜๊ณ , 5 ร— 5 ํฌ๊ธฐ์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด 2ํญ์งœ๋ฆฌ ์ œ๋กœ ํŒจ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ž…๋ ฅ๊ณผ ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๋ฅผ ๋ณด์กดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 5 ร— 5 ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€์— 1ํญ์งœ๋ฆฌ ์ œ๋กœ ํŒจ๋”ฉ์„ ํ•˜๋ฉด 7 ร— 7 ์ด๋ฏธ์ง€๊ฐ€ ๋˜๋Š”๋ฐ, ์—ฌ๊ธฐ์— 3 ร— 3์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•˜์—ฌ 1 ์ŠคํŠธ๋ผ์ด๋“œ๋กœ ํ•ฉ์„ฑ ๊ณฑ์„ ํ•œ ํ›„์˜ ํŠน์„ฑ ๋งต์€ ๊ธฐ์กด์˜ ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์™€ ๊ฐ™์€ 5 ร— 5๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 5. ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์—์„œ์˜ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์šฐ์„  ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ๋ณต์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ๊ฐ€์ค‘์น˜ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์œผ๋กœ 3 ร— 3 ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ด๋ฏธ์ง€๋ฅผ 1์ฐจ์› ํ…์„œ์ธ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ค๋ฉด, 3 ร— 3 = 9๊ฐ€ ๋˜๋ฏ€๋กœ ์ž…๋ ฅ์ธต์€ 9๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  4๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ๊ฐ€์ง€๋Š” ์€๋‹‰์ธต์„ ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๊ฐ ์—ฐ๊ฒฐ์„ ์€ ๊ฐ€์ค‘์น˜๋ฅผ ์˜๋ฏธํ•˜๋ฏ€๋กœ, ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” 9 ร— 4 = 36๊ฐœ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด์ œ ๋น„๊ต๋ฅผ ์œ„ํ•ด ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์œผ๋กœ 3 ร— 3 ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2 ร— 2 ์ปค๋„์„ ์‚ฌ์šฉํ•˜๊ณ , ์ŠคํŠธ๋ผ์ด๋“œ๋Š” 1๋กœ ํ•ฉ๋‹ˆ๋‹ค. (*๋Š” ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.) ์‚ฌ์‹ค ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์—์„œ ๊ฐ€์ค‘์น˜๋Š” ์ปค๋„ ํ–‰๋ ฌ์˜ ์›์†Œ๋“ค์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ํŠน์„ฑ ๋งต์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋™์ผํ•œ ์ปค๋„๋กœ ์ด๋ฏธ์ง€ ์ „์ฒด๋ฅผ ํ›‘์œผ๋ฉฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด๋ฏธ์ง€ ์ „์ฒด๋ฅผ ํ›‘์œผ๋ฉด์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์ค‘์น˜๋Š” 0 w, 2 w 4๊ฐœ๋ฟ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๋งˆ๋‹ค ์ด๋ฏธ์ง€์˜ ๋ชจ๋“  ํ”ฝ์…€์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ปค๋„๊ณผ ๋งคํ•‘๋˜๋Š” ํ”ฝ์…€๋งŒ์„ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์€ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์„ ์‚ฌ์šฉํ•  ๋•Œ๋ณด๋‹ค ํ›จ์”ฌ ์ ์€ ์ˆ˜์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๊ณต๊ฐ„์  ๊ตฌ์กฐ ์ •๋ณด๋ฅผ ๋ณด์กดํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์ธต ํผ์…‰ํŠธ๋ก ์˜ ์€๋‹‰์ธต์—์„œ๋Š” ๊ฐ€์ค‘์น˜ ์—ฐ์‚ฐ ํ›„์— ๋น„์„ ํ˜•์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ์€๋‹‰์ธต์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์–ป์€ ํŠน์„ฑ ๋งต์€ ๋‹ค์ธต ํผ์…‰ํŠธ๋ก  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋น„์„ ํ˜•์„ฑ ์ถ”๊ฐ€๋ฅผ ์œ„ํ•ด์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋ ๋ฃจ ํ•จ์ˆ˜๋‚˜ ๋ ๋ฃจ ํ•จ์ˆ˜์˜ ๋ณ€ํ˜•๋“ค์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต์—์„œ ๋ ๋ฃจ ํ•จ์ˆ˜๊ฐ€ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ด์œ ๋Š” ์•ž์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ค˜์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ ํŠน์„ฑ ๋งต์„ ์–ป๊ณ , ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋Š” ์—ฐ์‚ฐ์„ ํ•˜๋Š” ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ์€๋‹‰์ธต์„ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์—์„œ๋Š” ํ•ฉ์„ฑ๊ณฑ ์ธต(convolution layer)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ํŽธํ–ฅ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์—๋„ ํŽธํ–ฅ(bias)๋ฅผ ๋‹น์—ฐํžˆ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ํŽธํ–ฅ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ปค๋„์„ ์ ์šฉํ•œ ๋’ค์— ๋”ํ•ด์ง‘๋‹ˆ๋‹ค. ํŽธํ–ฅ์€ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์กด์žฌํ•˜๋ฉฐ, ์ปค๋„์ด ์ ์šฉ๋œ ๊ฒฐ๊ณผ์˜ ๋ชจ๋“  ์›์†Œ์— ๋”ํ•ด์ง‘๋‹ˆ๋‹ค. 6. ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ• ์ž…๋ ฅ์˜ ํฌ๊ธฐ์™€ ์ปค๋„์˜ ํฌ๊ธฐ, ๊ทธ๋ฆฌ๊ณ  ์ŠคํŠธ๋ผ์ด๋“œ์˜ ๊ฐ’๋งŒ ์•Œ๋ฉด ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ์ธ ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. h : ์ž…๋ ฅ์˜ ๋†’์ด w : ์ž…๋ ฅ์˜ ๋„ˆ๋น„ h : ์ปค๋„์˜ ๋†’์ด w : ์ปค๋„์˜ ๋„ˆ๋น„ : ์ŠคํŠธ๋ผ์ด๋“œ h : ํŠน์„ฑ ๋งต์˜ ๋†’์ด w : ํŠน์„ฑ ๋งต์˜ ๋„ˆ๋น„ ์ด์— ๋”ฐ๋ผ ํŠน์„ฑ ๋งต์˜ ๋†’์ด์™€ ๋„ˆ๋น„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. h f o r ( h K S 1 ) w f o r ( w K S 1 ) ์—ฌ๊ธฐ์„œ l o ํ•จ์ˆ˜๋Š” ์†Œ์ˆ˜์  ๋ฐœ์ƒ ์‹œ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆฌ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ 5 ร— 5 ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€์— 3 ร— 3 ์ปค๋„์„ ์‚ฌ์šฉํ•˜๊ณ  ์ŠคํŠธ๋ผ์ด๋“œ 1๋กœ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๋Š” (5 - 3 + 1 ) ร— (5 - 3 + 1) = 3 ร— 3์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋˜ํ•œ ์ด 9๋ฒˆ์˜ ์Šคํ…์ด ํ•„์š”ํ•จ์„ ์˜๋ฏธํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํŒจ๋”ฉ์˜ ํญ์„ ๋ผ๊ณ  ํ•˜๊ณ , ํŒจ๋”ฉ๊นŒ์ง€ ๊ณ ๋ คํ•œ ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. h f o r ( h K + P + ) w f o r ( w K + P + ) 7. ๋‹ค์ˆ˜์˜ ์ฑ„๋„์„ ๊ฐ€์งˆ ๊ฒฝ์šฐ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ(3์ฐจ์› ํ…์„œ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ) ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ฑ„๋„(channel) ๋˜๋Š” ๊นŠ์ด(depth)๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ , 2์ฐจ์› ํ…์„œ๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ์ž…๋ ฅ์€ '๋‹ค์ˆ˜์˜ ์ฑ„๋„์„ ๊ฐ€์ง„' ์ด๋ฏธ์ง€ ๋˜๋Š” ์ด์ „ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ํŠน์„ฑ ๋งต์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋‹ค์ˆ˜์˜ ์ฑ„๋„์„ ๊ฐ€์ง„ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์ปค๋„์˜ ์ฑ„๋„ ์ˆ˜๋„ ์ž…๋ ฅ์˜ ์ฑ„๋„ ์ˆ˜๋งŒํผ ์กด์žฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์ฑ„๋„ ์ˆ˜์™€ ์ปค๋„์˜ ์ฑ„๋„ ์ˆ˜๋Š” ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฑ„๋„ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฏ€๋กœ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ฑ„๋„๋งˆ๋‹ค ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๋”ํ•˜์—ฌ ์ตœ์ข… ํŠน์„ฑ ๋งต์„ ์–ป์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ 3๊ฐœ์˜ ์ฑ„๋„์„ ๊ฐ€์ง„ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ 3๊ฐœ์˜ ์ฑ„๋„์„ ๊ฐ€์ง„ ์ปค๋„์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ปค๋„์˜ ๊ฐ ์ฑ„๋„๋ผ๋ฆฌ์˜ ํฌ๊ธฐ๋Š” ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ฑ„๋„ ๊ฐ„ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ๋งˆ์น˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ๋”ํ•ด์„œ ํ•˜๋‚˜์˜ ์ฑ„๋„์„ ๊ฐ€์ง€๋Š” ํŠน์„ฑ ๋งต์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์œ„์˜ ์—ฐ์‚ฐ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ปค๋„์€ 3๊ฐœ์˜ ์ปค๋„์ด ์•„๋‹ˆ๋ผ 3๊ฐœ์˜ ์ฑ„๋„์„ ๊ฐ€์ง„ 1๊ฐœ์˜ ์ปค๋„์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๋†’์ด 3, ๋„ˆ๋น„ 3, ์ฑ„๋„ 3์˜ ์ž…๋ ฅ์ด ๋†’์ด 2, ๋„ˆ๋น„ 2, ์ฑ„๋„ 3์˜ ์ปค๋„๊ณผ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜์—ฌ ๋†’์ด 2, ๋„ˆ๋น„ 2, ์ฑ„๋„ 1์˜ ํŠน์„ฑ ๋งต์„ ์–ป๋Š”๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋กœ ์–ป์€ ํŠน์„ฑ ๋งต์˜ ์ฑ„๋„ ์ฐจ์›์€ RGB ์ฑ„๋„ ๋“ฑ๊ณผ ๊ฐ™์€ ์ปฌ๋Ÿฌ์˜ ์˜๋ฏธ๋ฅผ ๋‹ด๊ณ  ์žˆ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด ์—ฐ์‚ฐ์—์„œ ๊ฐ ์ฐจ์›์„ ๋ณ€์ˆ˜๋กœ ๋‘๊ณ  ์ข€ ๋” ์ผ๋ฐ˜ํ™”์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 8. 3์ฐจ์› ํ…์„œ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ์ผ๋ฐ˜ํ™”๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ ๋ณ€์ˆ˜๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. h : ์ž…๋ ฅ์˜ ๋†’์ด w : ์ž…๋ ฅ์˜ ๋„ˆ๋น„ h : ์ปค๋„์˜ ๋†’์ด w : ์ปค๋„์˜ ๋„ˆ๋น„ h : ํŠน์„ฑ ๋งต์˜ ๋†’์ด w : ํŠน์„ฑ ๋งต์˜ ๋„ˆ๋น„ i : ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์ฑ„๋„ ๋‹ค์Œ์€ 3์ฐจ์› ํ…์„œ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋†’์ด h , ๋„ˆ๋น„ w , ์ฑ„๋„ i ์˜ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋Š” ๋™์ผํ•œ ์ฑ„๋„ ์ˆ˜ i ๋ฅผ ๊ฐ€์ง€๋Š” ๋†’์ด h , ๋„ˆ๋น„ w ์˜ ์ปค๋„๊ณผ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜์—ฌ ๋†’์ด h , ๋„ˆ๋น„ w , ์ฑ„๋„ 1์˜ ํŠน์„ฑ ๋งต์„ ์–ป์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ•˜๋‚˜์˜ ์ž…๋ ฅ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•˜๋Š” ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ ๋‹ค์ˆ˜์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ฐ”๋€Œ๋Š”์ง€ ๋ด…์‹œ๋‹ค. ๋‹ค์Œ์€ o ๋ฅผ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์— ์‚ฌ์šฉํ•˜๋Š” ์ปค๋„์˜ ์ˆ˜๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ ๋‹ค์ˆ˜์˜ ์ปค๋„์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์‚ฌ์šฉํ•œ ์ปค๋„ ์ˆ˜๋Š” ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜ค๋Š” ํŠน์„ฑ ๋งต์˜ ์ฑ„๋„ ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ดํ•ดํ–ˆ๋‹ค๋ฉด ์ปค๋„์˜ ํฌ๊ธฐ์™€ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์ฑ„๋„ ์ˆ˜ i ์™€ ํŠน์„ฑ ๋งต(์ถœ๋ ฅ ๋ฐ์ดํ„ฐ)์˜ ์ฑ„๋„ ์ˆ˜ o ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์ค‘์น˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜๋Š” ์ปค๋„์˜ ์›์†Œ๋“ค์ด๋ฏ€๋กœ ํ•˜๋‚˜์˜ ์ปค๋„์˜ ํ•˜๋‚˜์˜ ์ฑ„๋„์€ i K ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜๋ ค๋ฉด ์ปค๋„์€ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์ฑ„๋„ ์ˆ˜์™€ ๋™์ผํ•œ ์ฑ„๋„ ์ˆ˜๋ฅผ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ํ•˜๋‚˜์˜ ์ปค๋„์ด ๊ฐ€์ง€๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆ˜๋Š” i K ร— i ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฌํ•œ ์ปค๋„์ด ์ด o ๊ฐœ๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ ๊ฐ€์ค‘์น˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด ์ˆ˜ : i K ร— i C 9. ํ’€๋ง(Pooling) ์ผ๋ฐ˜์ ์œผ๋กœ ํ•ฉ์„ฑ๊ณฑ ์ธต(ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ + ํ™œ์„ฑํ™” ํ•จ์ˆ˜) ๋‹ค์Œ์—๋Š” ํ’€๋ง ์ธต์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ํ’€๋ง ์ธต์—์„œ๋Š” ํŠน์„ฑ ๋งต์„ ๋‹ค์šด ์ƒ˜ํ”Œ๋งํ•˜์—ฌ ํŠน์„ฑ ๋งต์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š” ํ’€๋ง ์—ฐ์‚ฐ์ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ํ’€๋ง ์—ฐ์‚ฐ์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ตœ๋Œ€ ํ’€๋ง(max pooling)๊ณผ ํ‰๊ท  ํ’€๋ง(average pooling)์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ตœ๋Œ€ ํ’€๋ง์„ ํ†ตํ•ด์„œ ํ’€๋ง ์—ฐ์‚ฐ์„ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ํ’€๋ง ์—ฐ์‚ฐ์—์„œ๋„ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ปค๋„๊ณผ ์ŠคํŠธ๋ผ์ด๋“œ์˜ ๊ฐœ๋…์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ 2์ผ ๋•Œ, 2 x 2 ํฌ๊ธฐ ์ปค๋„๋กœ ๋งฅ์Šค ํ’€๋ง ์—ฐ์‚ฐ์„ ํ–ˆ์„ ๋•Œ ํŠน์„ฑ ๋งต์ด ์ ˆ๋ฐ˜์˜ ํฌ๊ธฐ๋กœ ๋‹ค์šด ์ƒ˜ํ”Œ๋ง๋˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋งฅ์Šค ํ’€๋ง์€ ์ปค๋„๊ณผ ๊ฒน์น˜๋Š” ์˜์—ญ ์•ˆ์—์„œ ์ตœ๋Œ“๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋‹ค์šด ์ƒ˜ํ”Œ๋งํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ’€๋ง ๊ธฐ๋ฒ•์ธ ํ‰๊ท  ํ’€๋ง์€ ์ตœ๋Œ“๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ‰๊ท ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ์—ฐ์‚ฐ์ด ๋ฉ๋‹ˆ๋‹ค. ํ’€๋ง ์—ฐ์‚ฐ์€ ์ปค๋„๊ณผ ์ŠคํŠธ๋ผ์ด๋“œ ๊ฐœ๋…์ด ์กด์žฌํ•œ๋‹ค๋Š” ์ ์—์„œ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ, ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๊ณผ์˜ ์ฐจ์ด์ ์€ ํ•™์Šตํ•ด์•ผ ํ•  ๊ฐ€์ค‘์น˜๊ฐ€ ์—†์œผ๋ฉฐ ์—ฐ์‚ฐ ํ›„์— ์ฑ„๋„ ์ˆ˜๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. http://taewan.kim/post/cnn/ https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/ https://excelsior-cjh.tistory.com/152 https://buomsoo-kim.github.io/keras/2018/04/28/Easy-deep-learning-with-Keras-7.md/ https://becominghuman.ai/not-just-introduction-to-convolutional-neural-networks-part-1-56a36b938592 https://brohrer.github.io/how_convolutional_neural_networks_work.html https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/ https://www.kdnuggets.com/2018/04/derivation-convolutional-neural-network-fully-connected-step-by-step.html https://www.slideshare.net/leeseungeun/cnn-vgg-72164295 08-02 CNN์œผ๋กœ MNIST ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” CNN์œผ๋กœ MNIST๋ฅผ ๋ถ„๋ฅ˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ชจ๋ธ ์ดํ•ดํ•˜๊ธฐ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์€ ์ถœ์ฒ˜์— ๋”ฐ๋ผ์„œ ํ•ฉ์„ฑ๊ณฑ ์ธต์„ ๋ถ€๋ฅด๋Š” ๋‹จ์œ„๊ฐ€ ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. 1. ์ฒซ ๋ฒˆ์งธ ํ‘œ๊ธฐ ๋ฐฉ๋ฒ• ํ•ฉ์„ฑ๊ณฑ(nn.Cov2d) + ํ™œ์„ฑํ™” ํ•จ์ˆ˜(nn.ReLU)๋ฅผ ํ•˜๋‚˜์˜ ํ•ฉ์„ฑ๊ณฑ ์ธต์œผ๋กœ ๋ณด๊ณ , ๋งฅ์Šค ํ’€๋ง(nn.MaxPoold2d)์€ ํ’€๋ง ์ธต์œผ๋กœ ๋ณ„๋„๋กœ ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. 2. ๋‘ ๋ฒˆ์งธ ํ‘œ๊ธฐ ๋ฐฉ๋ฒ• ํ•ฉ์„ฑ๊ณฑ(nn.Conv2d) + ํ™œ์„ฑํ™” ํ•จ์ˆ˜(nn.ReLU) + ๋งฅ์Šค ํ’€๋ง(nn.MaxPoold2d)์„ ํ•˜๋‚˜์˜ ํ•ฉ์„ฑ๊ณฑ ์ธต์œผ๋กœ ๋ด…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ’€๋ง๋„ ํ•˜๋‚˜์˜ ์ธต์œผ๋กœ ๋ณด๋Š๋ƒ, ์•ˆ ๋ณด๋Š๋ƒ์˜ ๋ฌธ์ œ์ธ๋ฐ ๋ˆ„๊ฐ€ ์˜ณ๊ณ  ํ‹€๋ฆฌ๋ƒ์˜ ๋ฌธ์ œ๋Š” ์•„๋‹ˆ๋ฏ€๋กœ, ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํŽธ์˜๋ฅผ ์œ„ํ•ด ๋งฅ์Šค ํ’€๋ง๊นŒ์ง€๋„ ํฌํ•จํ•ด์„œ ํ•˜๋‚˜์˜ ํ•ฉ์„ฑ๊ณฑ ์ธต์œผ๋กœ ํŒ๋‹จํ•˜๊ณ  ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋‘ ๋ฒˆ์งธ ํ‘œ๊ธฐ ๋ฐฉ๋ฒ•์„ ํƒํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜๋Š” ์ด 3๊ฐœ์˜ ์ธต์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. # 1๋ฒˆ ๋ ˆ์ด์–ด : ํ•ฉ์„ฑ๊ณฑ์ธต(Convolutional layer) ํ•ฉ์„ฑ๊ณฑ(in_channel = 1, out_channel = 32, kernel_size=3, stride=1, padding=1) + ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ReLU ๋งฅ์Šค ํ’€๋ง(kernel_size=2, stride=2)) # 2๋ฒˆ ๋ ˆ์ด์–ด : ํ•ฉ์„ฑ๊ณฑ์ธต(Convolutional layer) ํ•ฉ์„ฑ๊ณฑ(in_channel = 32, out_channel = 64, kernel_size=3, stride=1, padding=1) + ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ReLU ๋งฅ์Šค ํ’€๋ง(kernel_size=2, stride=2)) # 3๋ฒˆ ๋ ˆ์ด์–ด : ์ „๊ฒฐํ•ฉ์ธต(Fully-Connected layer) ํŠน์„ฑ ๋งต์„ ํŽผ์นœ๋‹ค. # batch_size ร— 7 ร— 7 ร— 64 โ†’ batch_size ร— 3136 ์ „๊ฒฐํ•ฉ์ธต(๋‰ด๋Ÿฐ 10๊ฐœ) + ํ™œ์„ฑํ™” ํ•จ์ˆ˜ Softmax ์ด๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•ด ๋ณด๋ฉฐ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 2. ๋ชจ๋ธ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„์˜ 3๊ฐœ์˜ ์ธต์„ ์ง์ ‘ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํ•„์š”ํ•œ ๋„๊ตฌ ์ž„ํฌํŠธ์™€ ์ž…๋ ฅ์˜ ์ •์˜ ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn ์ž„์˜์˜ ํ…์„œ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ…์„œ์˜ ํฌ๊ธฐ๋Š” 1 ร— 1 ร— 28 ร— 28์ž…๋‹ˆ๋‹ค. # ๋ฐฐ์น˜ ํฌ๊ธฐ ร— ์ฑ„๋„ ร— ๋†’์ด(height) ร— ๋„ˆ๋น„(widht)์˜ ํฌ๊ธฐ์˜ ํ…์„œ๋ฅผ ์„ ์–ธ inputs = torch.Tensor(1, 1, 28, 28) print('ํ…์„œ์˜ ํฌ๊ธฐ : {}'.format(inputs.shape)) ํ…์„œ์˜ ํฌ๊ธฐ : torch.Size([1, 1, 28, 28]) 2. ํ•ฉ์„ฑ๊ณฑ์ธต๊ณผ ํ’€๋ง ์„ ์–ธํ•˜๊ธฐ ์ด์ œ ์ฒซ ๋ฒˆ์งธ ํ•ฉ์„ฑ๊ณฑ ์ธต์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 1์ฑ„๋„ ์งœ๋ฆฌ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ 32์ฑ„๋„์„ ๋ฝ‘์•„๋‚ด๋Š”๋ฐ ์ปค๋„ ์‚ฌ์ด์ฆˆ๋Š” 3์ด๊ณ  ํŒจ๋”ฉ์€ 1์ž…๋‹ˆ๋‹ค. conv1 = nn.Conv2d(1, 32, 3, padding=1) print(conv1) Conv2d(1, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) ์ด์ œ ๋‘ ๋ฒˆ์งธ ํ•ฉ์„ฑ๊ณฑ ์ธต์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 32์ฑ„๋„ ์งœ๋ฆฌ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ 64์ฑ„๋„์„ ๋ฝ‘์•„๋‚ด๋Š”๋ฐ ์ปค๋„ ์‚ฌ์ด์ฆˆ๋Š” 3์ด๊ณ  ํŒจ๋”ฉ์€ 1์ž…๋‹ˆ๋‹ค. conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) print(conv2) Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) ์ด์ œ ๋งฅ์Šค ํ’€๋ง์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ์ •์ˆ˜ ํ•˜๋‚˜๋ฅผ ์ธ์ž๋กœ ๋„ฃ์œผ๋ฉด ์ปค๋„ ์‚ฌ์ด์ฆˆ์™€ ์ŠคํŠธ๋ผ์ด๋“œ๊ฐ€ ๋‘˜ ๋‹ค ํ•ด๋‹น ๊ฐ’์œผ๋กœ ์ง€์ •๋ฉ๋‹ˆ๋‹ค. pool = nn.MaxPool2d(2) print(pool) MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) 3. ๊ตฌํ˜„์ฒด๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ชจ๋ธ ๋งŒ๋“ค๊ธฐ ์ง€๊ธˆ๊นŒ์ง€๋Š” ์„ ์–ธ๋งŒ ํ•œ ๊ฒƒ์ด๊ณ  ์•„์ง ์ด๋“ค์„ ์—ฐ๊ฒฐ์‹œํ‚ค์ง€๋Š” ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ด๋“ค์„ ์—ฐ๊ฒฐ์‹œ์ผœ์„œ ๋ชจ๋ธ์„ ์™„์„ฑ์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ž…๋ ฅ์„ ์ฒซ ๋ฒˆ์งธ ํ•ฉ์„ฑ๊ณฑ์ธต์„ ํ†ต๊ณผ์‹œํ‚ค๊ณ  ํ•ฉ์„ฑ๊ณฑ์ธต์„ ํ†ต๊ณผ์‹œํ‚จ ํ›„์˜ ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. out = conv1(inputs) print(out.shape) torch.Size([1, 32, 28, 28]) 32์ฑ„๋„์˜ 28๋„ˆ๋น„ 28๋†’์ด์˜ ํ…์„œ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 32๊ฐ€ ๋‚˜์˜จ ์ด์œ ๋Š” conv1์˜ out_channel๋กœ 32๋ฅผ ์ง€์ •ํ•ด ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, 28๋„ˆ๋น„ 28๋†’์ด๊ฐ€ ๋œ ์ด์œ ๋Š” ํŒจ๋”ฉ์„ 1ํญ์œผ๋กœ ํ•˜๊ณ  3 ร— 3 ์ปค๋„์„ ์‚ฌ์šฉํ•˜๋ฉด ํฌ๊ธฐ๊ฐ€ ๋ณด์กด๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ฅผ ๋งฅ์Šค ํ’€๋ง์„ ํ†ต๊ณผ์‹œํ‚ค๊ณ  ๋งฅ์Šค ํ’€๋ง์„ ํ†ต๊ณผํ•œ ํ›„์˜ ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. out = pool(out) print(out.shape) torch.Size([1, 32, 14, 14]) 32์ฑ„๋„์˜ 14๋„ˆ๋น„ 14๋†’์ด์˜ ํ…์„œ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ฅผ ๋‹ค์‹œ ๋‘ ๋ฒˆ์งธ ํ•ฉ์„ฑ๊ณฑ์ธต์— ํ†ต๊ณผ์‹œํ‚ค๊ณ  ํ†ต๊ณผํ•œ ํ›„์˜ ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. out = conv2(out) print(out.shape) torch.Size([1, 64, 14, 14]) 64์ฑ„๋„์˜ 14๋„ˆ๋น„ 14๋†’์ด์˜ ํ…์„œ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 64๊ฐ€ ๋‚˜์˜จ ์ด์œ ๋Š” conv2์˜ out_channel๋กœ 64๋ฅผ ์ง€์ •ํ•ด ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, 14๋„ˆ๋น„ 14๋†’์ด๊ฐ€ ๋œ ์ด์œ ๋Š” ํŒจ๋”ฉ์„ 1ํญ์œผ๋กœ ํ•˜๊ณ  3 ร— 3 ์ปค๋„์„ ์‚ฌ์šฉํ•˜๋ฉด ํฌ๊ธฐ๊ฐ€ ๋ณด์กด๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ฅผ ๋งฅ์Šค ํ’€๋ง์„ ํ†ต๊ณผ์‹œํ‚ค๊ณ  ๋งฅ์Šค ํ’€๋ง์„ ํ†ต๊ณผํ•œ ํ›„์˜ ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ฅผ ๋งฅ์Šค ํ’€๋ง์„ ํ†ต๊ณผ์‹œํ‚ค๊ณ  ๋งฅ์Šค ํ’€๋ง์„ ํ†ต๊ณผํ•œ ํ›„์˜ ํ…์„œ์˜ ํฌ๊ธฐ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. out = pool(out) print(out.shape) torch.Size([1, 64, 7, 7]) ์ด์ œ ์ด ํ…์„œ๋ฅผ ํŽผ์น˜๋Š” ์ž‘์—…์„ ํ•  ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํŽผ์น˜๊ธฐ์— ์•ž์„œ ํ…์„œ์˜ n ๋ฒˆ์งธ ์ฐจ์›์„ ์ ‘๊ทผํ•˜๊ฒŒ ํ•ด์ฃผ๋Š”. size(n)์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ out์˜ ํฌ๊ธฐ๋Š” 1 ร— 64 ร— 7 ร— 7์ž…๋‹ˆ๋‹ค. out์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์ด ๋ช‡์ธ์ง€ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. out.size(0) out์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์€ 1์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ฐจ์›์ด ๋ช‡์ธ์ง€ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. out.size(1) 64 out์˜ ๋‘ ๋ฒˆ์งธ ์ฐจ์›์€ 64์ž…๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ์ฐจ์›์ด ๋ช‡์ธ์ง€ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. out.size(2) ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ out์˜ ๋„ค ๋ฒˆ์งธ ์ฐจ์›์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. out.size(3) ์ด์ œ ์ด๋ฅผ ๊ฐ€์ง€๊ณ . view()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์„œ๋ฅผ ํŽผ์น˜๋Š” ์ž‘์—…์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ฒซ ๋ฒˆ์งธ ์ฐจ์›์ธ ๋ฐฐ์น˜ ์ฐจ์›์€ ๊ทธ๋Œ€๋กœ ๋‘๊ณ  ๋‚˜๋จธ์ง€๋Š” ํŽผ์ณ๋ผ out = out.view(out.size(0), -1) print(out.shape) torch.Size([1, 3136]) ๋ฐฐ์น˜ ์ฐจ์›์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ํ•˜๋‚˜์˜ ์ฐจ์›์œผ๋กœ ํ†ตํ•ฉ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด์— ๋Œ€ํ•ด์„œ ์ „๊ฒฐํ•ฉ์ธต(Fully-Connteced layer)๋ฅผ ํ†ต๊ณผ์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์œผ๋กœ 10๊ฐœ์˜ ๋‰ด๋Ÿฐ์„ ๋ฐฐ์น˜ํ•˜์—ฌ 10๊ฐœ ์ฐจ์›์˜ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. fc = nn.Linear(3136, 10) # input_dim = 3,136, output_dim = 10 out = fc(out) print(out.shape) torch.Size([1, 10]) 3. CNN์œผ๋กœ MNIST ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torchvision.datasets as dsets import torchvision.transforms as transforms import torch.nn.init ๋งŒ์•ฝ GPU๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด device ๊ฐ’์ด cuda๊ฐ€ ๋˜๊ณ , ์•„๋‹ˆ๋ผ๋ฉด cpu๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. device = 'cuda' if torch.cuda.is_available() else 'cpu' # ๋žœ๋ค ์‹œ๋“œ ๊ณ ์ • torch.manual_seed(777) # GPU ์‚ฌ์šฉ ๊ฐ€๋Šฅ์ผ ๊ฒฝ์šฐ ๋žœ๋ค ์‹œ๋“œ ๊ณ ์ • if device == 'cuda': torch.cuda.manual_seed_all(777) ํ•™์Šต์— ์‚ฌ์šฉํ•  ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. learning_rate = 0.001 training_epochs = 15 batch_size = 100 ๋ฐ์ดํ„ฐ ๋กœ๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ์ •์˜ํ•ด ์ค๋‹ˆ๋‹ค. mnist_train = dsets.MNIST(root='MNIST_data/', # ๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ ์ง€์ • train=True, # True๋ฅผ ์ง€์ •ํ•˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ๋‹ค์šด๋กœ๋“œ transform=transforms.ToTensor(), # ํ…์„œ๋กœ ๋ณ€ํ™˜ download=True) mnist_test = dsets.MNIST(root='MNIST_data/', # ๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ ์ง€์ • train=False, # False๋ฅผ ์ง€์ •ํ•˜๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋‹ค์šด๋กœ๋“œ transform=transforms.ToTensor(), # ํ…์„œ๋กœ ๋ณ€ํ™˜ download=True) ๋ฐ์ดํ„ฐ ๋กœ๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ ์…‹๊ณผ ๋ฐ์ดํ„ฐ ๋กœ๋”๊ฐ€ ๊ธฐ์–ต์ด ์•ˆ ๋‚œ๋‹ค๋ฉด '๋ฏธ๋‹ˆ ๋ฐฐ์น˜์™€ ๋ฐ์ดํ„ฐ ๋กœ๋“œ' ์ฑ•ํ„ฐ๋ฅผ ๊ผญ ๋ณต์Šตํ•˜์„ธ์š”. data_loader = torch.utils.data.DataLoader(dataset=mnist_train, batch_size=batch_size, shuffle=True, drop_last=True) ์ด์ œ ํด๋ž˜์Šค๋กœ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. class CNN(torch.nn.Module): def __init__(self): super(CNN, self).__init__() # ์ฒซ ๋ฒˆ์งธ ์ธต # ImgIn shape=(?, 28, 28, 1) # Conv -> (?, 28, 28, 32) # Pool -> (?, 14, 14, 32) self.layer1 = torch.nn.Sequential( torch.nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, stride=2)) # ๋‘ ๋ฒˆ์งธ ์ธต # ImgIn shape=(?, 14, 14, 32) # Conv ->(?, 14, 14, 64) # Pool ->(?, 7, 7, 64) self.layer2 = torch.nn.Sequential( torch.nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, stride=2)) # ์ „๊ฒฐํ•ฉ์ธต 7x7x64 inputs -> 10 outputs self.fc = torch.nn.Linear(7 * 7 * 64, 10, bias=True) # ์ „๊ฒฐํ•ฉ์ธต ํ•œ์ •์œผ๋กœ ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™” torch.nn.init.xavier_uniform_(self.fc.weight) def forward(self, x): out = self.layer1(x) out = self.layer2(out) out = out.view(out.size(0), -1) # ์ „๊ฒฐํ•ฉ์ธต์„ ์œ„ํ•ด์„œ Flatten out = self.fc(out) return out ๋ชจ๋ธ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. # CNN ๋ชจ๋ธ ์ •์˜ model = CNN().to(device) ๋น„์šฉ ํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. criterion = torch.nn.CrossEntropyLoss().to(device) # ๋น„์šฉ ํ•จ์ˆ˜์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜ ํฌํ•จ๋ผ ์žˆ์Œ. optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) ์ด ๋ฐฐ์น˜์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. total_batch = len(data_loader) print('์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : {}'.format(total_batch)) ์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : 600 ์ด ๋ฐฐ์น˜์˜ ์ˆ˜๋Š” 600์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 100์œผ๋กœ ํ–ˆ์œผ๋ฏ€๋กœ ๊ฒฐ๊ตญ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์ด 60,000๊ฐœ๋ž€ ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (์‹œ๊ฐ„์ด ๊ฝค ์˜ค๋ž˜ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.) for epoch in range(training_epochs): avg_cost = 0 for X, Y in data_loader: # ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ๊บผ๋‚ด์˜จ๋‹ค. X๋Š” ๋ฏธ๋‹ˆ ๋ฐฐ์น˜, Y ๋Š ใ„ด๋ ˆ์ด๋ธ”. # image is already size of (28x28), no reshape # label is not one-hot encoded X = X.to(device) Y = Y.to(device) optimizer.zero_grad() hypothesis = model(X) cost = criterion(hypothesis, Y) cost.backward() optimizer.step() avg_cost += cost / total_batch print('[Epoch: {:>4}] cost = {:>.9}'.format(epoch + 1, avg_cost)) [Epoch: 1] cost = 0.224006683 [Epoch: 2] cost = 0.062186949 [Epoch: 3] cost = 0.0449030139 [Epoch: 4] cost = 0.0355709828 [Epoch: 5] cost = 0.0290450025 [Epoch: 6] cost = 0.0248527844 [Epoch: 7] cost = 0.0207189098 [Epoch: 8] cost = 0.0181982815 [Epoch: 9] cost = 0.0153046707 [Epoch: 10] cost = 0.0124179339 [Epoch: 11] cost = 0.0105423154 [Epoch: 12] cost = 0.00991860125 [Epoch: 13] cost = 0.00894770492 [Epoch: 14] cost = 0.0071221008 [Epoch: 15] cost = 0.00588585297 ์ด์ œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ํ•™์Šต์„ ์ง„ํ–‰ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ฏ€๋กœ torch.no_grad() with torch.no_grad(): X_test = mnist_test.test_data.view(len(mnist_test), 1, 28, 28).float().to(device) Y_test = mnist_test.test_labels.to(device) prediction = model(X_test) correct_prediction = torch.argmax(prediction, 1) == Y_test accuracy = correct_prediction.float().mean() print('Accuracy:', accuracy.item()) Accuracy: 0.9883000254631042 98%์˜ ์ •ํ™•๋„๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ธต์„ ๋” ์Œ“์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. https://www.yceffort.kr/2019/01/29/pytorch-3-convolutional-neural-network/ 08-03 ๊นŠ์€ CNN์œผ๋กœ MNIST ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์•ž์„œ ๋ฐฐ์šด CNN์— ์ธต์„ ๋” ์ถ”๊ฐ€ํ•˜์—ฌ MNIST๋ฅผ ๋ถ„๋ฅ˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ชจ๋ธ ์ดํ•ดํ•˜๊ธฐ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ๋ชจ๋ธ์˜ ์•„ํ‚คํ…์ฒ˜๋Š” ์ด 5๊ฐœ์˜ ์ธต์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ์ฑ•ํ„ฐ์—์„œ 1๋ฒˆ ๋ ˆ์ด์–ด์™€ 2๋ฒˆ ๋ ˆ์ด์–ด๋Š” ๋™์ผํ•˜๋˜, ์ƒˆ๋กœ์šด ํ•ฉ์„ฑ๊ณฑ์ธต๊ณผ ์ „๊ฒฐํ•ฉ์ธต์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. # 1๋ฒˆ ๋ ˆ์ด์–ด : ํ•ฉ์„ฑ๊ณฑ์ธต(Convolutional layer) ํ•ฉ์„ฑ๊ณฑ(in_channel = 1, out_channel = 32, kernel_size=3, stride=1, padding=1) + ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ReLU ๋งฅ์Šค ํ’€๋ง(kernel_size=2, stride=2)) # 2๋ฒˆ ๋ ˆ์ด์–ด : ํ•ฉ์„ฑ๊ณฑ์ธต(Convolutional layer) ํ•ฉ์„ฑ๊ณฑ(in_channel = 32, out_channel = 64, kernel_size=3, stride=1, padding=1) + ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ReLU ๋งฅ์Šค ํ’€๋ง(kernel_size=2, stride=2)) # 3๋ฒˆ ๋ ˆ์ด์–ด : ํ•ฉ์„ฑ๊ณฑ์ธต(Convolutional layer) ํ•ฉ์„ฑ๊ณฑ(in_channel = 64, out_channel = 128, kernel_size=3, stride=1, padding=1) + ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ReLU ๋งฅ์Šค ํ’€๋ง(kernel_size=2, stride=2, padding=1)) # 4๋ฒˆ ๋ ˆ์ด์–ด : ์ „๊ฒฐํ•ฉ์ธต(Fully-Connected layer) ํŠน์„ฑ ๋งต์„ ํŽผ์นœ๋‹ค. # batch_size ร— 4 ร— 4 ร— 128 โ†’ batch_size ร— 2048 ์ „๊ฒฐํ•ฉ์ธต(๋‰ด๋Ÿฐ 625๊ฐœ) + ํ™œ์„ฑํ™” ํ•จ์ˆ˜ ReLU # 5๋ฒˆ ๋ ˆ์ด์–ด : ์ „๊ฒฐํ•ฉ์ธต(Fully-Connected layer) ์ „๊ฒฐํ•ฉ์ธต(๋‰ด๋Ÿฐ 10๊ฐœ) + ํ™œ์„ฑํ™” ํ•จ์ˆ˜ Softmax 2. ๊นŠ์€ CNN์œผ๋กœ MNIST ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์‚ฌ์‹ค ์ด๋ฒˆ ์ฑ•ํ„ฐ์˜ ์ฝ”๋“œ๋Š” ์ด์ „ ์ฑ•ํ„ฐ์—์„œ ์ธต์ด ์กฐ๊ธˆ ๋” ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ ๋ง๊ณ ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. import torch import torchvision.datasets as dsets import torchvision.transforms as transforms import torch.nn.init ๋งŒ์•ฝ GPU๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด device ๊ฐ’์ด cuda๊ฐ€ ๋˜๊ณ , ์•„๋‹ˆ๋ผ๋ฉด cpu๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. device = 'cuda' if torch.cuda.is_available() else 'cpu' # ๋žœ๋ค ์‹œ๋“œ ๊ณ ์ • torch.manual_seed(777) # GPU ์‚ฌ์šฉ ๊ฐ€๋Šฅ์ผ ๊ฒฝ์šฐ ๋žœ๋ค ์‹œ๋“œ ๊ณ ์ • if device == 'cuda': torch.cuda.manual_seed_all(777) ํ•™์Šต์— ์‚ฌ์šฉํ•  ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. learning_rate = 0.001 training_epochs = 15 batch_size = 100 ๋ฐ์ดํ„ฐ ๋กœ๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ์ •์˜ํ•ด ์ค๋‹ˆ๋‹ค. mnist_train = dsets.MNIST(root='MNIST_data/', # ๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ ์ง€์ • train=True, # True๋ฅผ ์ง€์ •ํ•˜๋ฉด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ๋‹ค์šด๋กœ๋“œ transform=transforms.ToTensor(), # ํ…์„œ๋กœ ๋ณ€ํ™˜ download=True) mnist_test = dsets.MNIST(root='MNIST_data/', # ๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ ์ง€์ • train=False, # False๋ฅผ ์ง€์ •ํ•˜๋ฉด ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋‹ค์šด๋กœ๋“œ transform=transforms.ToTensor(), # ํ…์„œ๋กœ ๋ณ€ํ™˜ download=True) ๋ฐ์ดํ„ฐ ๋กœ๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ ์…‹๊ณผ ๋ฐ์ดํ„ฐ ๋กœ๋”๊ฐ€ ๊ธฐ์–ต์ด ์•ˆ ๋‚œ๋‹ค๋ฉด '๋ฏธ๋‹ˆ ๋ฐฐ์น˜์™€ ๋ฐ์ดํ„ฐ ๋กœ๋“œ' ์ฑ•ํ„ฐ๋ฅผ ๊ผญ ๋ณต์Šตํ•˜์„ธ์š”. data_loader = torch.utils.data.DataLoader(dataset=mnist_train, batch_size=batch_size, shuffle=True, drop_last=True) ์ด์ œ ํด๋ž˜์Šค๋กœ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. class CNN(torch.nn.Module): def __init__(self): super(CNN, self).__init__() self.keep_prob = 0.5 # L1 ImgIn shape=(?, 28, 28, 1) # Conv -> (?, 28, 28, 32) # Pool -> (?, 14, 14, 32) self.layer1 = torch.nn.Sequential( torch.nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, stride=2)) # L2 ImgIn shape=(?, 14, 14, 32) # Conv ->(?, 14, 14, 64) # Pool ->(?, 7, 7, 64) self.layer2 = torch.nn.Sequential( torch.nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, stride=2)) # L3 ImgIn shape=(?, 7, 7, 64) # Conv ->(?, 7, 7, 128) # Pool ->(?, 4, 4, 128) self.layer3 = torch.nn.Sequential( torch.nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1), torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=1)) # L4 FC 4x4x128 inputs -> 625 outputs self.fc1 = torch.nn.Linear(4 * 4 * 128, 625, bias=True) torch.nn.init.xavier_uniform_(self.fc1.weight) self.layer4 = torch.nn.Sequential( self.fc1, torch.nn.ReLU(), torch.nn.Dropout(p=1 - self.keep_prob)) # L5 Final FC 625 inputs -> 10 outputs self.fc2 = torch.nn.Linear(625, 10, bias=True) torch.nn.init.xavier_uniform_(self.fc2.weight) def forward(self, x): out = self.layer1(x) out = self.layer2(out) out = self.layer3(out) out = out.view(out.size(0), -1) # Flatten them for FC out = self.layer4(out) out = self.fc2(out) return out ๋ชจ๋ธ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. # CNN ๋ชจ๋ธ ์ •์˜ model = CNN().to(device) ๋น„์šฉ ํ•จ์ˆ˜์™€ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. criterion = torch.nn.CrossEntropyLoss().to(device) # ๋น„์šฉ ํ•จ์ˆ˜์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜ ํฌํ•จ๋ผ ์žˆ์Œ. optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) ์ด ๋ฐฐ์น˜์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. total_batch = len(data_loader) print('์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : {}'.format(total_batch)) ์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : 600 ์ด ๋ฐฐ์น˜์˜ ์ˆ˜๋Š” 600์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 100์œผ๋กœ ํ–ˆ์œผ๋ฏ€๋กœ ๊ฒฐ๊ตญ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์ด 60,000๊ฐœ๋ž€ ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (์‹œ๊ฐ„์ด ๊ฝค ์˜ค๋ž˜ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.) for epoch in range(training_epochs): avg_cost = 0 for X, Y in data_loader: # ๋ฏธ๋‹ˆ ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ๊บผ๋‚ด์˜จ๋‹ค. X๋Š” ๋ฏธ๋‹ˆ ๋ฐฐ์น˜, Y ๋Š ใ„ด๋ ˆ์ด๋ธ”. # image is already size of (28x28), no reshape # label is not one-hot encoded X = X.to(device) Y = Y.to(device) optimizer.zero_grad() hypothesis = model(X) cost = criterion(hypothesis, Y) cost.backward() optimizer.step() avg_cost += cost / total_batch print('[Epoch: {:>4}] cost = {:>.9}'.format(epoch + 1, avg_cost)) [Epoch: 1] cost = 0.17012253403663635 ... ์ค‘๋žต ... [Epoch: 15] cost = 0.007885557599365711 ์ด์ œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ํ•™์Šต์„ ์ง„ํ–‰ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ฏ€๋กœ torch.no_grad() with torch.no_grad(): X_test = mnist_test.test_data.view(len(mnist_test), 1, 28, 28).float().to(device) Y_test = mnist_test.test_labels.to(device) prediction = model(X_test) correct_prediction = torch.argmax(prediction, 1) == Y_test accuracy = correct_prediction.float().mean() print('Accuracy:', accuracy.item()) Accuracy: 0.9763999581336975 ์ธต์„ ๋” ๊นŠ๊ฒŒ ์Œ“์•˜๋Š”๋ฐ ์˜คํžˆ๋ ค ์ •ํ™•๋„๊ฐ€ ์ค„์–ด๋“œ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ธต์„ ๊นŠ๊ฒŒ ์Œ“๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜์ง€๋งŒ, ๊ผญ ๊นŠ๊ฒŒ ์Œ“๋Š” ๊ฒƒ์ด ์ •ํ™•๋„๋ฅผ ์˜ฌ๋ ค์ฃผ์ง€๋Š” ์•Š์œผ๋ฉฐ ํšจ์œจ์ ์œผ๋กœ ์Œ“๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. 09. [NLP ์ž…๋ฌธ ] - ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ธฐ์ดˆ(NLP Basics) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 03-01 ํ† ํฐํ™”(Tokenization) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํฌ๋กค๋ง ๋“ฑ์œผ๋กœ ์–ป์–ด๋‚ธ ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”์— ๋งž๊ฒŒ ์ „์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ์ƒํƒœ๋ผ๋ฉด, ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์šฉ๋„์— ๋งž๊ฒŒ ํ† ํฐํ™”(tokenization) & ์ •์ œ(cleaning) & ์ •๊ทœํ™”(normalization) ํ•˜๋Š” ์ผ์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ทธ์ค‘์—์„œ๋„ ํ† ํฐํ™”์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ฝ”ํผ์Šค(corpus)์—์„œ ํ† ํฐ(token)์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š” ์ž‘์—…์„ ํ† ํฐํ™”(tokenization)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐ์˜ ๋‹จ์œ„๊ฐ€ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅด์ง€๋งŒ, ๋ณดํ†ต ์˜๋ฏธ ์žˆ๋Š” ๋‹จ์œ„๋กœ ํ† ํฐ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ† ํฐํ™”์— ๋Œ€ํ•œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ƒํ™ฉ์— ๋Œ€ํ•ด์„œ ์–ธ๊ธ‰ํ•˜์—ฌ ํ† ํฐํ™”์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. ์ด์–ด์„œ NLTK, KoNLPY๋ฅผ ํ†ตํ•ด ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๋ฉฐ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 1. ๋‹จ์–ด ํ† ํฐํ™”(Word Tokenization) ํ† ํฐ์˜ ๊ธฐ์ค€์„ ๋‹จ์–ด(word)๋กœ ํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹จ์–ด ํ† ํฐํ™”(word tokenization)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์—ฌ๊ธฐ์„œ ๋‹จ์–ด(word)๋Š” ๋‹จ์–ด ๋‹จ์œ„ ์™ธ์—๋„ ๋‹จ ์–ด๊ตฌ, ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š” ๋ฌธ์ž์—ด๋กœ๋„ ๊ฐ„์ฃผ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ๊ตฌ๋‘์ (punctuation)๊ณผ ๊ฐ™์€ ๋ฌธ์ž๋Š” ์ œ์™ธํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋‹จ์–ด ํ† ํฐํ™” ์ž‘์—…์„ ํ•ด๋ด…์‹œ๋‹ค. ๊ตฌ๋‘์ ์ด๋ž€ ๋งˆ์นจํ‘œ(.), ์ฝค๋งˆ(,), ๋ฌผ์Œํ‘œ(?), ์„ธ๋ฏธ์ฝœ๋ก (;), ๋Š๋‚Œํ‘œ(!) ๋“ฑ๊ณผ ๊ฐ™์€ ๊ธฐํ˜ธ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ: Time is an illusion. Lunchtime double so! ์ด๋Ÿฌํ•œ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ๊ตฌ๋‘์ ์„ ์ œ์™ธํ•œ ํ† ํฐํ™” ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ : "Time", "is", "an", "illustion", "Lunchtime", "double", "so" ์ด ์˜ˆ์ œ์—์„œ ํ† ํฐํ™” ์ž‘์—…์€ ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋‘์ ์„<NAME> ๋’ค์— ๋„์–ด์“ฐ๊ธฐ(whitespace)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž˜๋ผ๋ƒˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ์˜ˆ์ œ๋Š” ํ† ํฐํ™”์˜ ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ์ค€ ๊ฒƒ์— ๋ถˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ํ† ํฐํ™” ์ž‘์—…์€ ๋‹จ์ˆœํžˆ ๊ตฌ๋‘์ ์ด๋‚˜ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ „๋ถ€ ์ œ๊ฑฐํ•˜๋Š” ์ •์ œ(cleaning) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ํ•ด๊ฒฐ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ตฌ๋‘์ ์ด๋‚˜ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ „๋ถ€ ์ œ๊ฑฐํ•˜๋ฉด ํ† ํฐ์ด ์˜๋ฏธ๋ฅผ ์žƒ์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ์ž๋ฅด๋ฉด ์‚ฌ์‹ค์ƒ ๋‹จ์–ด ํ† ํฐ์ด ๊ตฌ๋ถ„๋˜๋Š” ์˜์–ด์™€ ๋‹ฌ๋ฆฌ, ํ•œ๊ตญ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๋งŒ์œผ๋กœ๋Š” ๋‹จ์–ด ํ† ํฐ์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋’ค์—์„œ ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ํ† ํฐํ™” ์ค‘ ์ƒ๊ธฐ๋Š” ์„ ํƒ์˜ ์ˆœ๊ฐ„ ํ† ํฐํ™”๋ฅผ ํ•˜๋‹ค ๋ณด๋ฉด, ์˜ˆ์ƒํ•˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด์„œ ํ† ํฐํ™”์˜ ๊ธฐ์ค€์„ ์ƒ๊ฐํ•ด ๋ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์ด๋Ÿฌํ•œ ์„ ํƒ์€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€์— ๋”ฐ๋ผ์„œ ๊ทธ ์šฉ๋„์— ์˜ํ–ฅ์ด ์—†๋Š” ๊ธฐ์ค€์œผ๋กœ ์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๋ฅผ(')๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๋‹จ์–ด๋Š” ์–ด๋–ป๊ฒŒ ํ† ํฐ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์„ ํƒ์˜ ๋ฌธ์ œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. Don't be fooled by the dark sounding name, Mr. Jone's Orphanage is as cheery as cheery goes for a pastry shop. ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๊ฐ€ ๋“ค์–ด๊ฐ„ ์ƒํ™ฉ์—์„œ Don't์™€ Jone's๋Š” ์–ด๋–ป๊ฒŒ ํ† ํฐํ™”ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ๋‹ค์–‘ํ•œ ์„ ํƒ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Don't Don t Dont Do n't Jone's Jone s Jone Jones ์ด ์ค‘ ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก ํ† ํฐํ™” ๋„๊ตฌ๋ฅผ ์ง์ ‘ ์„ค๊ณ„ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๊ธฐ์กด์— ๊ณต๊ฐœ๋œ ๋„๊ตฌ๋“ค์„ ์‚ฌ์šฉํ•˜์˜€์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์‚ฌ์šฉ์ž์˜ ๋ชฉ์ ๊ณผ ์ผ์น˜ํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. NLTK๋Š” ์˜์–ด ์ฝ”ํผ์Šค๋ฅผ ํ† ํฐํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ์ค‘ word_tokenize์™€ WordPunctTokenizer๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from nltk.tokenize import word_tokenize from nltk.tokenize import WordPunctTokenizer from tensorflow.keras.preprocessing.text import text_to_word_sequence ์šฐ์„  word_tokenize๋ฅผ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. print('๋‹จ์–ด ํ† ํฐํ™” 1 :',word_tokenize("Don't be fooled by the dark sounding name, Mr. Jone's Orphanage is as cheery as cheery goes for a pastry shop.")) ๋‹จ์–ด ํ† ํฐํ™” 1 : ['Do', "n't", 'be', 'fooled', 'by', 'the', 'dark', 'sounding', 'name', ',', 'Mr.', 'Jone', "'s", 'Orphanage', 'is', 'as', 'cheery', 'as', 'cheery', 'goes', 'for', 'a', 'pastry', 'shop', '.'] word_tokenize๋Š” Don't๋ฅผ Do์™€ n't๋กœ ๋ถ„๋ฆฌํ•˜์˜€์œผ๋ฉฐ, ๋ฐ˜๋ฉด Jone's๋Š” Jone๊ณผ 's๋กœ ๋ถ„๋ฆฌํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, wordPunctTokenizer๋Š” ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๊ฐ€ ๋“ค์–ด๊ฐ„ ์ฝ”ํผ์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ๊นŒ์š”? print('๋‹จ์–ด ํ† ํฐํ™” 2 :',WordPunctTokenizer().tokenize("Don't be fooled by the dark sounding name, Mr. Jone's Orphanage is as cheery as cheery goes for a pastry shop.")) ['Don', "'", 't', 'be', 'fooled', 'by', 'the', 'dark', 'sounding', 'name', ',', 'Mr', '.', 'Jone', "'", 's', 'Orphanage', 'is', 'as', 'cheery', 'as', 'cheery', 'goes', 'for', 'a', 'pastry', 'shop', '.'] WordPunctTokenizer๋Š” ๊ตฌ๋‘์ ์„ ๋ณ„๋„๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ํŠน์ง•์„ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์•ž์„œ ํ™•์ธํ–ˆ๋˜ word_tokenize์™€๋Š” ๋‹ฌ๋ฆฌ Don't๋ฅผ Don๊ณผ '์™€ t๋กœ ๋ถ„๋ฆฌํ•˜์˜€์œผ๋ฉฐ, ์ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Jone's๋ฅผ Jone๊ณผ '์™€ s๋กœ ๋ถ„๋ฆฌํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ€๋ผ์Šค ๋˜ํ•œ ํ† ํฐํ™” ๋„๊ตฌ๋กœ์„œ text_to_word_sequence๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. print('๋‹จ์–ด ํ† ํฐํ™” 3 :',text_to_word_sequence("Don't be fooled by the dark sounding name, Mr. Jone's Orphanage is as cheery as cheery goes for a pastry shop.")) ๋‹จ์–ด ํ† ํฐํ™” 3 : ["don't", 'be', 'fooled', 'by', 'the', 'dark', 'sounding', 'name', 'mr', "jone's", 'orphanage', 'is', 'as', 'cheery', 'as', 'cheery', 'goes', 'for', 'a', 'pastry', 'shop'] ์ผ€๋ผ์Šค์˜ text_to_word_sequence๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์„ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋ฉด์„œ ๋งˆ์นจํ‘œ๋‚˜ ์ฝค๋งˆ, ๋Š๋‚Œํ‘œ ๋“ฑ์˜ ๊ตฌ๋‘์ ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ don't๋‚˜ jone's์™€ ๊ฐ™์€ ๊ฒฝ์šฐ ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๋Š” ๋ณด์กดํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ํ† ํฐํ™”์—์„œ ๊ณ ๋ คํ•ด์•ผ ํ•  ์‚ฌํ•ญ ํ† ํฐํ™” ์ž‘์—…์„ ๋‹จ์ˆœํ•˜๊ฒŒ ์ฝ”ํผ์Šค์—์„œ ๊ตฌ๋‘์ ์„ ์ œ์™ธํ•˜๊ณ  ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ์ž˜๋ผ๋‚ด๋Š” ์ž‘์—…์ด๋ผ๊ณ  ๊ฐ„์ฃผํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ผ์€ ๋ณด๋‹ค ์„ฌ์„ธํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ•„์š”ํ•œ๋ฐ ๊ทธ ์ด์œ ๋ฅผ ์ •๋ฆฌํ•ด ๋ด…๋‹ˆ๋‹ค. 1) ๊ตฌ๋‘์ ์ด๋‚˜ ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ๋‹จ์ˆœ ์ œ์™ธํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ ๋‹จ์–ด๋“ค์„ ๊ฑธ๋Ÿฌ๋‚ผ ๋•Œ, ๊ตฌ๋‘์ ์ด๋‚˜ ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ๋‹จ์ˆœํžˆ ์ œ์™ธํ•˜๋Š” ๊ฒƒ์€ ์˜ณ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฝ”ํผ์Šค์— ๋Œ€ํ•œ ์ •์ œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋‹ค ๋ณด๋ฉด, ๊ตฌ๋‘์ ์กฐ์ฐจ๋„ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด์ž๋ฉด, ๋งˆ์นจํ‘œ(.)์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๋ฌธ์žฅ์˜ ๊ฒฝ๊ณ„๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋Š”๋ฐ ๋„์›€์ด ๋˜๋ฏ€๋กœ ๋‹จ์–ด๋ฅผ ๋ฝ‘์•„๋‚ผ ๋•Œ, ๋งˆ์นจํ‘œ(.)๋ฅผ ์ œ์™ธํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ ๋‹ค๋ฅธ ์˜ˆ๋กœ ๋‹จ์–ด ์ž์ฒด์— ๊ตฌ๋‘์ ์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋Š”๋ฐ, m.p.h๋‚˜ Ph.D๋‚˜ AT&T ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ ํŠน์ˆ˜ ๋ฌธ์ž์˜ ๋‹ฌ๋Ÿฌ๋‚˜ ์Šฌ๋ž˜์‹œ(/)๋กœ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋ฉด, $45.55์™€ ๊ฐ™์€ ๊ฐ€๊ฒฉ์„ ์˜๋ฏธํ•˜๊ธฐ๋„ ํ•˜๊ณ , 01/02/06์€ ๋‚ ์งœ๋ฅผ ์˜๋ฏธํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ์ด๋Ÿฐ ๊ฒฝ์šฐ 45.55๋ฅผ ํ•˜๋‚˜๋กœ ์ทจ๊ธ‰ํ•˜๊ณ  45์™€ 55๋กœ ๋”ฐ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ  ์‹ถ์ง€๋Š” ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆซ์ž ์‚ฌ์ด์— ์ฝค๋งˆ(,)๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต ์ˆ˜์น˜๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ๋Š” 123,456,789์™€ ๊ฐ™์ด ์„ธ ์ž๋ฆฌ ๋‹จ์œ„๋กœ ์ปด๋งˆ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ์ค„์ž„๋ง๊ณผ ๋‹จ์–ด ๋‚ด์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ. ํ† ํฐํ™” ์ž‘์—…์—์„œ ์ข…์ข… ์˜์–ด๊ถŒ ์–ธ์–ด์˜ ์•„ํฌ์ŠคํŠธ๋กœํ”ผ(')๋Š” ์••์ถ•๋œ ๋‹จ์–ด๋ฅผ ๋‹ค์‹œ ํŽผ์น˜๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด what're๋Š” what are์˜ ์ค„์ž„๋ง์ด๋ฉฐ, we're๋Š” we are์˜ ์ค„์ž„๋ง์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ re๋ฅผ ์ ‘์–ด(clitic)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋‹จ์–ด๊ฐ€ ์ค„์ž„๋ง๋กœ ์“ฐ์ผ ๋•Œ ์ƒ๊ธฐ๋Š” ํ˜•ํƒœ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น I am์„ ์ค„์ธ I'm์ด ์žˆ์„ ๋•Œ, m์„ ์ ‘์–ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. New York์ด๋ผ๋Š” ๋‹จ์–ด๋‚˜ rock 'n' roll์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๋ด…์‹œ๋‹ค. ์ด ๋‹จ์–ด๋“ค์€ ํ•˜๋‚˜์˜ ๋‹จ์–ด์ด์ง€๋งŒ ์ค‘๊ฐ„์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์šฉ๋„์— ๋”ฐ๋ผ์„œ, ํ•˜๋‚˜์˜ ๋‹จ์–ด ์‚ฌ์ด์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋„ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ๋ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํ† ํฐํ™” ์ž‘์—…์€ ์ €๋Ÿฌํ•œ ๋‹จ์–ด๋ฅผ ํ•˜๋‚˜๋กœ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ๋„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. 3) ํ‘œ์ค€ ํ† ํฐํ™” ์˜ˆ์ œ ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ํ‘œ์ค€์œผ๋กœ ์“ฐ์ด๊ณ  ์žˆ๋Š” ํ† ํฐํ™” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ธ Penn Treebank Tokenization์˜ ๊ทœ์น™์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•˜๊ณ , ํ† ํฐํ™”์˜ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทœ์น™ 1. ํ•˜์ดํ”ˆ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋‹จ์–ด๋Š” ํ•˜๋‚˜๋กœ<NAME>๋‹ค. ๊ทœ์น™ 2. doesn't์™€ ๊ฐ™์ด ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๋กœ '์ ‘์–ด'๊ฐ€ ํ•จ๊ป˜ํ•˜๋Š” ๋‹จ์–ด๋Š” ๋ถ„๋ฆฌํ•ด ์ค€๋‹ค. ํ•ด๋‹น ํ‘œ์ค€์— ์•„๋ž˜์˜ ๋ฌธ์žฅ์„ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด๋ด…๋‹ˆ๋‹ค. "Starting a home-based restaurant may be an ideal. it doesn't have a food chain or restaurant of their own." from nltk.tokenize import TreebankWordTokenizer tokenizer = TreebankWordTokenizer() text = "Starting a home-based restaurant may be an ideal. it doesn't have a food chain or restaurant of their own." print('ํŠธ๋ฆฌ ๋ฑ…ํฌ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ € :',tokenizer.tokenize(text)) ํŠธ๋ฆฌ ๋ฑ…ํฌ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ € : ['Starting', 'a', 'home-based', 'restaurant', 'may', 'be', 'an', 'ideal.', 'it', 'does', "n't", 'have', 'a', 'food', 'chain', 'or', 'restaurant', 'of', 'their', 'own', '.'] ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, ๊ฐ๊ฐ ๊ทœ์น™ 1๊ณผ ๊ทœ์น™ 2์— ๋”ฐ๋ผ์„œ home-based๋Š” ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ์ทจ๊ธ‰ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, dosen't์˜ ๊ฒฝ์šฐ does์™€ n't๋Š” ๋ถ„๋ฆฌ๋˜์—ˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. ๋ฌธ์žฅ ํ† ํฐํ™”(Sentence Tokenization) ์ด๋ฒˆ์—๋Š” ํ† ํฐ์˜ ๋‹จ์œ„๊ฐ€ ๋ฌธ์žฅ(sentence)์ผ ๊ฒฝ์šฐ๋ฅผ ๋…ผ์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค ๋‚ด์—์„œ ๋ฌธ์žฅ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ์ž‘์—…์œผ๋กœ ๋•Œ๋กœ๋Š” ๋ฌธ์žฅ ๋ถ„๋ฅ˜(sentence segmentation)๋ผ๊ณ ๋„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋ณดํ†ต ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค๊ฐ€ ์ •์ œ๋˜์ง€ ์•Š์€ ์ƒํƒœ๋ผ๋ฉด, ์ฝ”ํผ์Šค๋Š” ๋ฌธ์žฅ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์ง€ ์•Š์•„์„œ ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์šฉ๋„์— ๋งž๊ฒŒ ๋ฌธ์žฅ ํ† ํฐ ํ™”๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ์ฃผ์–ด์ง„ ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ ๋‹จ์œ„๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ง๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•ด ๋ดค์„ ๋•Œ๋Š”? ๋‚˜ ๋งˆ์นจํ‘œ(.)๋‚˜! ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์žฅ์„ ์ž˜๋ผ๋‚ด๋ฉด ๋˜์ง€ ์•Š์„๊นŒ๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ผญ ๊ทธ๋ ‡์ง€๋งŒ์€ ์•Š์Šต๋‹ˆ๋‹ค. !๋‚˜?๋Š” ๋ฌธ์žฅ์˜ ๊ตฌ๋ถ„์„ ์œ„ํ•œ ๊ฝค ๋ช…ํ™•ํ•œ ๊ตฌ๋ถ„์ž(boundary) ์—ญํ• ์„ ํ•˜์ง€๋งŒ ๋งˆ์นจํ‘œ๋Š” ๊ทธ๋ ‡์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋งˆ์นจํ‘œ๋Š” ๋ฌธ์žฅ์˜ ๋์ด ์•„๋‹ˆ๋”๋ผ๋„ ๋“ฑ์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. EX1) IP 192.168.56.31 ์„œ๋ฒ„์— ๋“ค์–ด๊ฐ€์„œ ๋กœ๊ทธ ํŒŒ์ผ ์ €์žฅํ•ด์„œ aaa@gmail.com๋กœ ๊ฒฐ๊ณผ ์ข€ ๋ณด๋‚ด์ค˜. ๊ทธ ํ›„ ์ ์‹ฌ ๋จน์œผ๋Ÿฌ ๊ฐ€์ž. EX2) Since I'm actively looking for Ph.D. students, I get the same question a dozen times every year. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์˜ˆ์ œ์— ๋งˆ์นจํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์žฅ ํ† ํฐํ™”๋ฅผ ์ ์šฉํ•ด ๋ณธ๋‹ค๋ฉด ์–ด๋–จ๊นŒ์š”? ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์—์„œ๋Š” ๋ณด๋‚ด์ค˜.์—์„œ ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ์—์„œ๋Š” year.์—์„œ ์ฒ˜์Œ์œผ๋กœ ๋ฌธ์žฅ์ด ๋๋‚œ ๊ฒƒ์œผ๋กœ ์ธ์‹ํ•˜๋Š” ๊ฒƒ์ด ์ œ๋Œ€๋กœ ๋ฌธ์žฅ์˜ ๋์„ ์˜ˆ์ธกํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์ˆœํžˆ ๋งˆ์นจํ‘œ(.)๋กœ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ ์ง“๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด, ๋ฌธ์žฅ์˜ ๋์ด ๋‚˜์˜ค๊ธฐ ์ „์— ์ด๋ฏธ ๋งˆ์นจํ‘œ๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋“ฑ์žฅํ•˜์—ฌ ์˜ˆ์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ์ฝ”ํผ์Šค๊ฐ€ ์–ด๋–ค ๊ตญ์ ์˜ ์–ธ์–ด์ธ์ง€, ๋˜๋Š” ํ•ด๋‹น ์ฝ”ํผ์Šค ๋‚ด์—์„œ ํŠน์ˆ˜๋ฌธ์ž๋“ค์ด ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š”์ง€์— ๋”ฐ๋ผ์„œ ์ง์ ‘ ๊ทœ์น™๋“ค์„ ์ •์˜ํ•ด ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. 100% ์ •ํ™•๋„๋ฅผ ์–ป๋Š” ์ผ์€ ์‰ฌ์šด ์ผ์ด ์•„๋‹Œ๋ฐ, ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ์— ์˜คํƒ€๋‚˜, ๋ฌธ์žฅ์˜ ๊ตฌ์„ฑ์ด ์—‰๋ง์ด๋ผ๋ฉด ์ •ํ•ด๋†“์€ ๊ทœ์น™์ด ์†Œ์šฉ์ด ์—†์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. NLTK์—์„œ๋Š” ์˜์–ด ๋ฌธ์žฅ์˜ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” sent_tokenize๋ฅผ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. NLTK๋ฅผ ํ†ตํ•ด ๋ฌธ์žฅ ํ† ํฐํ™”๋ฅผ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from nltk.tokenize import sent_tokenize text = "His barber kept his word. But keeping such a huge secret to himself was driving him crazy. Finally, the barber went up a mountain and almost to the edge of a cliff. He dug a hole in the midst of some reeds. He looked about, to make sure no one was near." print('๋ฌธ์žฅ ํ† ํฐํ™” 1 :',sent_tokenize(text)) ๋ฌธ์žฅ ํ† ํฐํ™” 1 : ['His barber kept his word.', 'But keeping such a huge secret to himself was driving him crazy.', 'Finally, the barber went up a mountain and almost to the edge of a cliff.', 'He dug a hole in the midst of some reeds.', 'He looked about, to make sure no one was near.'] ์œ„ ์ฝ”๋“œ๋Š” text์— ์ €์žฅ๋œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์žฅ๋“ค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์„ฑ๊ณต์ ์œผ๋กœ ๋ชจ๋“  ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•ด ๋‚ด์—ˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ฒˆ์—๋Š” ๋ฌธ์žฅ ์ค‘๊ฐ„์— ๋งˆ์นจํ‘œ๊ฐ€ ๋‹ค์ˆ˜ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ๋„ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. text = "I am actively looking for Ph.D. students. and you are a Ph.D student." print('๋ฌธ์žฅ ํ† ํฐํ™” 2 :',sent_tokenize(text)) ๋ฌธ์žฅ ํ† ํฐํ™” 2 : ['I am actively looking for Ph.D. students.', 'and you are a Ph.D student.'] NLTK๋Š” ๋‹จ์ˆœํžˆ ๋งˆ์นจํ‘œ๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ํ•˜์—ฌ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, Ph.D.๋ฅผ ๋ฌธ์žฅ ๋‚ด์˜ ๋‹จ์–ด๋กœ ์ธ์‹ํ•˜์—ฌ ์„ฑ๊ณต์ ์œผ๋กœ ์ธ์‹ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด์— ๋Œ€ํ•œ ๋ฌธ์žฅ ํ† ํฐํ™” ๋„๊ตฌ ๋˜ํ•œ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฐ•์ƒ๊ธธ ๋‹˜์ด ๊ฐœ๋ฐœํ•œ KSS(Korean Sentence Splitter)๋ฅผ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด KSS๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install kss KSS๋ฅผ ํ†ตํ•ด์„œ ๋ฌธ์žฅ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import kss text = '๋”ฅ ๋Ÿฌ๋‹ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๊ฐ€ ์žฌ๋ฏธ์žˆ๊ธฐ๋Š” ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ œ๋Š” ์˜์–ด๋ณด๋‹ค ํ•œ๊ตญ์–ด๋กœ ํ•  ๋•Œ ๋„ˆ๋ฌด ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ•ด๋ณด๋ฉด ์•Œ๊ฑธ์š”?' print('ํ•œ๊ตญ์–ด ๋ฌธ์žฅ ํ† ํฐํ™” :',kss.split_sentences(text)) ํ•œ๊ตญ์–ด ๋ฌธ์žฅ ํ† ํฐํ™” : ['๋”ฅ ๋Ÿฌ๋‹ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๊ฐ€ ์žฌ๋ฏธ์žˆ๊ธฐ๋Š” ํ•ฉ๋‹ˆ๋‹ค.', '๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ œ๋Š” ์˜์–ด๋ณด๋‹ค ํ•œ๊ตญ์–ด๋กœ ํ•  ๋•Œ ๋„ˆ๋ฌด ์–ด๋ ต์Šต๋‹ˆ๋‹ค.', '์ด์ œ ํ•ด๋ณด๋ฉด ์•Œ๊ฑธ์š”?'] ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์ •์ƒ์ ์œผ๋กœ ๋ชจ๋“  ๋ฌธ์žฅ์ด ๋ถ„๋ฆฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 5. ํ•œ๊ตญ์–ด์—์„œ์˜ ํ† ํฐํ™”์˜ ์–ด๋ ค์›€. ์˜์–ด๋Š” New York๊ณผ ๊ฐ™์€ ํ•ฉ์„ฑ์–ด๋‚˜ he's ์™€ ๊ฐ™์ด ์ค„์ž„๋ง์— ๋Œ€ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋งŒ ํ•œ๋‹ค๋ฉด, ๋„์–ด์“ฐ๊ธฐ(whitespace)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋Š” ๋„์–ด์“ฐ๊ธฐ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ด๋„ ๋‹จ์–ด ํ† ํฐ ํ™”๊ฐ€ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ๋‹จ์–ด ๋‹จ์œ„๋กœ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋„์–ด์“ฐ๊ธฐ ํ† ํฐํ™”์™€ ๋‹จ์–ด ํ† ํฐ ํ™”๊ฐ€ ๊ฑฐ์˜ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•œ๊ตญ์–ด๋Š” ์˜์–ด์™€๋Š” ๋‹ฌ๋ฆฌ ๋„์–ด์“ฐ๊ธฐ๋งŒ์œผ๋กœ๋Š” ํ† ํฐํ™”๋ฅผ ํ•˜๊ธฐ์— ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๊ฐ€ ๋˜๋Š” ๋‹จ์œ„๋ฅผ '์–ด์ ˆ'์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ ์–ด์ ˆ ํ† ํฐํ™”๋Š” ํ•œ๊ตญ์–ด NLP์—์„œ ์ง€์–‘๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด์ ˆ ํ† ํฐํ™”์™€ ๋‹จ์–ด ํ† ํฐํ™”๋Š” ๊ฐ™์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ ๊ทผ๋ณธ์ ์ธ ์ด์œ ๋Š” ํ•œ๊ตญ์–ด๊ฐ€ ์˜์–ด์™€๋Š” ๋‹ค๋ฅธ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋Š” ์–ธ์–ด์ธ ๊ต์ฐฉ์–ด๋ผ๋Š” ์ ์—์„œ ๊ธฐ์ธํ•ฉ๋‹ˆ๋‹ค. ๊ต์ฐฉ์–ด๋ž€ ์กฐ์‚ฌ, ์–ด๋ฏธ ๋“ฑ์„ ๋ถ™์—ฌ์„œ ๋ง์„ ๋งŒ๋“œ๋Š” ์–ธ์–ด๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. 1) ๊ต์ฐฉ์–ด์˜ ํŠน์„ฑ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. ์˜์–ด์™€๋Š” ๋‹ฌ๋ฆฌ ํ•œ๊ตญ์–ด์—๋Š” ์กฐ์‚ฌ๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•œ๊ตญ์–ด์— ๊ทธ(he/him)๋ผ๋Š” ์ฃผ์–ด๋‚˜ ๋ชฉ์ ์–ด๊ฐ€ ๋“ค์–ด๊ฐ„ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ, ๊ทธ๋ผ๋Š” ๋‹จ์–ด ํ•˜๋‚˜์—๋„ '๊ทธ๊ฐ€', '๊ทธ์—๊ฒŒ', '๊ทธ๋ฅผ', '๊ทธ์™€', '๊ทธ๋Š”'๊ณผ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ์กฐ์‚ฌ๊ฐ€ '๊ทธ'๋ผ๋Š” ๊ธ€์ž ๋’ค์— ๋„์–ด์“ฐ๊ธฐ ์—†์ด ๋ฐ”๋กœ ๋ถ™๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋‹ค ๋ณด๋ฉด ๊ฐ™์€ ๋‹จ์–ด์ž„์—๋„ ์„œ๋กœ ๋‹ค๋ฅธ ์กฐ์‚ฌ๊ฐ€ ๋ถ™์–ด์„œ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ์ธ์‹์ด ๋˜๋ฉด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๊ฐ€ ํž˜๋“ค๊ณ  ๋ฒˆ๊ฑฐ๋กœ์›Œ์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํ•œ๊ตญ์–ด NLP์—์„œ ์กฐ์‚ฌ๋Š” ๋ถ„๋ฆฌํ•ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๊ฐ€ ์˜์–ด์ฒ˜๋Ÿผ ๋…๋ฆฝ์ ์ธ ๋‹จ์–ด๋ผ๋ฉด ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ํ† ํฐํ™”๋ฅผ ํ•˜๋ฉด ๋˜๊ฒ ์ง€๋งŒ ํ•œ๊ตญ์–ด๋Š” ์–ด์ ˆ์ด ๋…๋ฆฝ์ ์ธ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์กฐ์‚ฌ ๋“ฑ์˜ ๋ฌด์–ธ๊ฐ€๊ฐ€ ๋ถ™์–ด์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„์„œ ์ด๋ฅผ ์ „๋ถ€ ๋ถ„๋ฆฌํ•ด ์ค˜์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ํ† ํฐํ™”์—์„œ๋Š” ํ˜•ํƒœ์†Œ(morpheme) ๋ž€ ๊ฐœ๋…์„ ๋ฐ˜๋“œ์‹œ ์ดํ•ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ˜•ํƒœ์†Œ(morpheme)๋ž€ ๋œป์„ ๊ฐ€์ง„ ๊ฐ€์žฅ ์ž‘์€ ๋ง์˜ ๋‹จ์œ„๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด ํ˜•ํƒœ์†Œ์—๋Š” ๋‘ ๊ฐ€์ง€ ํ˜•ํƒœ์†Œ๊ฐ€ ์žˆ๋Š”๋ฐ ์ž๋ฆฝ ํ˜•ํƒœ์†Œ์™€ ์˜์กด ํ˜•ํƒœ์†Œ์ž…๋‹ˆ๋‹ค. ์ž๋ฆฝ ํ˜•ํƒœ์†Œ : ์ ‘์‚ฌ, ์–ด๋ฏธ, ์กฐ์‚ฌ์™€ ์ƒ๊ด€์—†์ด ์ž๋ฆฝํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ์†Œ. ๊ทธ ์ž์ฒด๋กœ ๋‹จ์–ด๊ฐ€ ๋œ๋‹ค. ์ฒด์–ธ(๋ช…์‚ฌ, ๋Œ€๋ช…์‚ฌ, ์ˆ˜์‚ฌ), ์ˆ˜์‹์–ธ(๊ด€ํ˜•์‚ฌ, ๋ถ€์‚ฌ), ๊ฐํƒ„์‚ฌ ๋“ฑ์ด ์žˆ๋‹ค. ์˜์กด ํ˜•ํƒœ์†Œ : ๋‹ค๋ฅธ ํ˜•ํƒœ์†Œ์™€ ๊ฒฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉ๋˜๋Š” ํ˜•ํƒœ์†Œ. ์ ‘์‚ฌ, ์–ด๋ฏธ, ์กฐ์‚ฌ, ์–ด๊ฐ„์„ ๋งํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๋ฌธ์žฅ : ์—๋””๊ฐ€ ์ฑ…์„ ์ฝ์—ˆ๋‹ค ์ด ๋ฌธ์žฅ์„ ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ['์—๋””๊ฐ€', '์ฑ…์„', '์ฝ์—ˆ๋‹ค'] ํ•˜์ง€๋งŒ ์ด๋ฅผ ํ˜•ํƒœ์†Œ ๋‹จ์œ„๋กœ ๋ถ„ํ•ดํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž๋ฆฝ ํ˜•ํƒœ์†Œ : ์—๋””, ์ฑ… ์˜์กด ํ˜•ํƒœ์†Œ : -๊ฐ€, -์„, ์ฝ-, -์—ˆ, -๋‹ค '์—๋””'๋ผ๋Š” ์‚ฌ๋žŒ ์ด๋ฆ„๊ณผ '์ฑ…'์ด๋ผ๋Š” ๋ช…์‚ฌ๋ฅผ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ํ•œ๊ตญ์–ด์—์„œ ์˜์–ด์—์„œ์˜ ๋‹จ์–ด ํ† ํฐํ™”์™€ ์œ ์‚ฌํ•œ ํ˜•ํƒœ๋ฅผ ์–ป์œผ๋ ค๋ฉด ์–ด์ ˆ ํ† ํฐ ํ™”๊ฐ€ ์•„๋‹ˆ๋ผ ํ˜•ํƒœ์†Œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. 2) ํ•œ๊ตญ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์˜์–ด๋ณด๋‹ค ์ž˜ ์ง€์ผœ์ง€์ง€ ์•Š๋Š”๋‹ค. ์‚ฌ์šฉํ•˜๋Š” ํ•œ๊ตญ์–ด ์ฝ”ํผ์Šค๊ฐ€ ๋‰ด์Šค ๊ธฐ์‚ฌ์™€ ๊ฐ™์ด ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ฒ ์ €ํ•˜๊ฒŒ ์ง€ํ‚ค๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๋Š” ๊ธ€์ด๋ผ๋ฉด ์ข‹๊ฒ ์ง€๋งŒ, ๋งŽ์€ ๊ฒฝ์šฐ์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ํ‹€๋ ธ๊ฑฐ๋‚˜ ์ง€์ผœ์ง€์ง€ ์•Š๋Š” ์ฝ”ํผ์Šค๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด๋Š” ์˜์–ด๊ถŒ ์–ธ์–ด์™€ ๋น„๊ตํ•˜์—ฌ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์–ด๋ ต๊ณ  ์ž˜ ์ง€์ผœ์ง€์ง€ ์•Š๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์—ฌ๋Ÿฌ ๊ฒฌํ•ด๊ฐ€ ์žˆ์œผ๋‚˜, ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฒฌํ•ด๋Š” ํ•œ๊ตญ์–ด์˜ ๊ฒฝ์šฐ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ง€์ผœ์ง€์ง€ ์•Š์•„๋„ ๊ธ€์„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์—†๋˜ ํ•œ๊ตญ์–ด์— ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋ณดํŽธํ™”๋œ ๊ฒƒ๋„ ๊ทผ๋Œ€(1933๋…„, ํ•œ๊ธ€๋งž์ถค๋ฒ•ํ†ต์ผ์•ˆ)์˜ ์ผ์ž…๋‹ˆ๋‹ค. ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ „ํ˜€ ํ•˜์ง€ ์•Š์€ ํ•œ๊ตญ์–ด์™€ ์˜์–ด ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๋ด…์‹œ๋‹ค. EX1) ์ œ๊ฐ€ ์ด๋ ‡๊ฒŒ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ „ํ˜€ ํ•˜์ง€ ์•Š๊ณ  ๊ธ€์„ ์ผ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๊ธ€์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. EX2) Tobeornottobethatisthequestion ์˜์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด ์†์‰ฝ๊ฒŒ ์•Œ์•„๋ณด๊ธฐ ์–ด๋ ค์šด ๋ฌธ์žฅ๋“ค์ด ์ƒ๊น๋‹ˆ๋‹ค. ์ด๋Š” ํ•œ๊ตญ์–ด(๋ชจ์•„์“ฐ๊ธฐ ๋ฐฉ์‹)์™€ ์˜์–ด(ํ’€์–ด์“ฐ๊ธฐ ๋ฐฉ์‹)๋ผ๋Š” ์–ธ์–ด์  ํŠน์„ฑ์˜ ์ฐจ์ด์— ๊ธฐ์ธํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ๋ชจ์•„์“ฐ๊ธฐ์™€ ํ’€์–ด์“ฐ๊ธฐ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ํ•˜์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๊ฒฐ๋ก ์ ์œผ๋กœ ํ•œ๊ตญ์–ด๋Š” ์ˆ˜๋งŽ์€ ์ฝ”ํผ์Šค์—์„œ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋ฌด์‹œ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๊ฐ€ ์–ด๋ ค์›Œ์กŒ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 6. ํ’ˆ์‚ฌ ํƒœ๊น…(Part-of-speech tagging) ๋‹จ์–ด๋Š” ํ‘œ๊ธฐ๋Š” ๊ฐ™์ง€๋งŒ ํ’ˆ์‚ฌ์— ๋”ฐ๋ผ์„œ ๋‹จ์–ด์˜ ์˜๋ฏธ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์˜์–ด ๋‹จ์–ด 'fly'๋Š” ๋™์‚ฌ๋กœ๋Š” '๋‚ ๋‹ค'๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€๋งŒ, ๋ช…์‚ฌ๋กœ๋Š” 'ํŒŒ๋ฆฌ'๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. '๋ชป'์ด๋ผ๋Š” ๋‹จ์–ด๋Š” ๋ช…์‚ฌ๋กœ์„œ๋Š” ๋ง์น˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ชฉ์žฌ ๋”ฐ์œ„๋ฅผ ๊ณ ์ •ํ•˜๋Š” ๋ฌผ๊ฑด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ถ€์‚ฌ๋กœ์„œ์˜ '๋ชป'์€ '๋จน๋Š”๋‹ค', '๋‹ฌ๋ฆฐ๋‹ค'์™€ ๊ฐ™์€ ๋™์ž‘ ๋™์‚ฌ๋ฅผ ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์˜๋ฏธ๋กœ ์“ฐ์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ์ œ๋Œ€๋กœ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ์–ด๋–ค ํ’ˆ์‚ฌ๋กœ ์“ฐ์˜€๋Š”์ง€ ๋ณด๋Š” ๊ฒƒ์ด ์ฃผ์š” ์ง€ํ‘œ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์— ๋”ฐ๋ผ ๋‹จ์–ด ํ† ํฐํ™” ๊ณผ์ •์—์„œ ๊ฐ ๋‹จ์–ด๊ฐ€ ์–ด๋–ค ํ’ˆ์‚ฌ๋กœ ์“ฐ์˜€๋Š”์ง€๋ฅผ ๊ตฌ๋ถ„ํ•ด๋†“๊ธฐ๋„ ํ•˜๋Š”๋ฐ, ์ด ์ž‘์—…์„ ํ’ˆ์‚ฌ ํƒœ๊น…(part-of-speech tagging)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. NLTK์™€ KoNLPy๋ฅผ ํ†ตํ•ด ํ’ˆ์‚ฌ ํƒœ๊น… ์‹ค์Šต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 7. NLTK์™€ KoNLPy๋ฅผ ์ด์šฉํ•œ ์˜์–ด, ํ•œ๊ตญ์–ด ํ† ํฐํ™” ์‹ค์Šต NLTK์—์„œ๋Š” Penn Treebank POS Tags๋ผ๋Š” ๊ธฐ์ค€์„ ์‚ฌ์šฉํ•˜์—ฌ ํ’ˆ์‚ฌ๋ฅผ ํƒœ๊น… ํ•ฉ๋‹ˆ๋‹ค. from nltk.tokenize import word_tokenize from nltk.tag import pos_tag text = "I am actively looking for Ph.D. students. and you are a Ph.D. student." tokenized_sentence = word_tokenize(text) print('๋‹จ์–ด ํ† ํฐํ™” :',tokenized_sentence) print('ํ’ˆ์‚ฌ ํƒœ๊น… :',pos_tag(tokenized_sentence)) ๋‹จ์–ด ํ† ํฐํ™” : ['I', 'am', 'actively', 'looking', 'for', 'Ph.D.', 'students', '.', 'and', 'you', 'are', 'a', 'Ph.D.', 'student', '.'] ํ’ˆ์‚ฌ ํƒœ๊น… : [('I', 'PRP'), ('am', 'VBP'), ('actively', 'RB'), ('looking', 'VBG'), ('for', 'IN'), ('Ph.D.', 'NNP'), ('students', 'NNS'), ('.', '.'), ('and', 'CC'), ('you', 'PRP'), ('are', 'VBP'), ('a', 'DT'), ('Ph.D.', 'NNP'), ('student', 'NN'), ('.', '.')] ์˜์–ด ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ’ˆ์‚ฌ ํƒœ๊น…์„ ์ˆ˜ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Penn Treebank POG Tags์—์„œ PRP๋Š” ์ธ์นญ ๋Œ€๋ช…์‚ฌ, VBP๋Š” ๋™์‚ฌ, RB๋Š” ๋ถ€์‚ฌ, VBG๋Š” ํ˜„์žฌ ๋ถ€์‚ฌ, IN์€ ์ „์น˜์‚ฌ, NNP๋Š” ๊ณ ์œ  ๋ช…์‚ฌ, NNS๋Š” ๋ณต์ˆ˜ํ˜• ๋ช…์‚ฌ, CC๋Š” ์ ‘์†์‚ฌ, DT๋Š” ๊ด€์‚ฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ๋Š” KoNLPy(์ฝ”์—”์—˜ํŒŒ์ด)๋ผ๋Š” ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”์—”์—˜ํŒŒ์ด๋ฅผ ํ†ตํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋กœ Okt(Open Korea Text), ๋ฉ”์บ…(Mecab), ์ฝ”๋ชจ๋ž€(Komoran), ํ•œ ๋‚˜๋ˆ”(Hannanum), ๊ผฌ๊ผฌ๋งˆ(Kkma)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด NLP์—์„œ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์–ด ํ† ํฐํ™”. ๋” ์ •ํ™•ํžˆ๋Š” ํ˜•ํƒœ์†Œ ํ† ํฐํ™”(morpheme tokenization)๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” Okt์™€ ๊ผฌ๊ผฌ๋งˆ ๋‘ ๊ฐœ์˜ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. from konlpy.tag import Okt from konlpy.tag import Kkma okt = Okt() kkma = Kkma() print('OKT ํ˜•ํƒœ์†Œ ๋ถ„์„ :',okt.morphs("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) print('OKT ํ’ˆ์‚ฌ ํƒœ๊น… :',okt.pos("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) print('OKT ๋ช…์‚ฌ ์ถ”์ถœ :',okt.nouns("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) OKT ํ˜•ํƒœ์†Œ ๋ถ„์„ : ['์—ด์‹ฌํžˆ', '์ฝ”๋”ฉ', 'ํ•œ', '๋‹น์‹ ', ',', '์—ฐํœด', '์—๋Š”', '์—ฌํ–‰', '์„', '๊ฐ€๋ด์š”'] OKT ํ’ˆ์‚ฌ ํƒœ๊น… : [('์—ด์‹ฌํžˆ', 'Adverb'), ('์ฝ”๋”ฉ', 'Noun'), ('ํ•œ', 'Josa'), ('๋‹น์‹ ', 'Noun'), (',', 'Punctuation'), ('์—ฐํœด', 'Noun'), ('์—๋Š”', 'Josa'), ('์—ฌํ–‰', 'Noun'), ('์„', 'Josa'), ('๊ฐ€๋ด์š”', 'Verb')] OKT ๋ช…์‚ฌ ์ถ”์ถœ : ['์ฝ”๋”ฉ', '๋‹น์‹ ', '์—ฐํœด', '์—ฌํ–‰'] ์œ„์˜ ์˜ˆ์ œ๋Š” Okt ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋กœ ํ† ํฐํ™”๋ฅผ ์‹œ๋„ํ•ด ๋ณธ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ฉ”์„œ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 1) morphs : ํ˜•ํƒœ์†Œ ์ถ”์ถœ 2) pos : ํ’ˆ์‚ฌ ํƒœ๊น…(Part-of-speech tagging) 3) nouns : ๋ช…์‚ฌ ์ถ”์ถœ ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์ฝ”์—”์—˜ํŒŒ์ด์˜ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋“ค์€ ๊ณตํ†ต์ ์œผ๋กœ ์ด ๋ฉ”์„œ๋“œ๋“ค์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ ํ˜•ํƒœ์†Œ ์ถ”์ถœ๊ณผ ํ’ˆ์‚ฌ ํƒœ๊น… ๋ฉ”์„œ๋“œ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์กฐ์‚ฌ๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด NLP์—์„œ ์ „์ฒ˜๋ฆฌ์— ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ต‰์žฅํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ผฌ๊ผฌ๋งˆ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ™์€ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. print('๊ผฌ๊ผฌ๋งˆ ํ˜•ํƒœ์†Œ ๋ถ„์„ :',kkma.morphs("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) print('๊ผฌ๊ผฌ๋งˆ ํ’ˆ์‚ฌ ํƒœ๊น… :',kkma.pos("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) print('๊ผฌ๊ผฌ๋งˆ ๋ช…์‚ฌ ์ถ”์ถœ :',kkma.nouns("์—ด์‹ฌํžˆ ์ฝ”๋”ฉํ•œ ๋‹น์‹ , ์—ฐํœด์—๋Š” ์—ฌํ–‰์„ ๊ฐ€๋ด์š”")) ๊ผฌ๊ผฌ๋งˆ ํ˜•ํƒœ์†Œ ๋ถ„์„ : ['์—ด์‹ฌํžˆ', '์ฝ”๋”ฉ', 'ํ•˜', 'ใ„ด', '๋‹น์‹ ', ',', '์—ฐํœด', '์—', '๋Š”', '์—ฌํ–‰', '์„', '๊ฐ€๋ณด', '์•„์š”'] ๊ผฌ๊ผฌ๋งˆ ํ’ˆ์‚ฌ ํƒœ๊น… : [('์—ด์‹ฌํžˆ', 'MAG'), ('์ฝ”๋”ฉ', 'NNG'), ('ํ•˜', 'XSV'), ('ใ„ด', 'ETD'), ('๋‹น์‹ ', 'NP'), (',', 'SP'), ('์—ฐํœด', 'NNG'), ('์—', 'JKM'), ('๋Š”', 'JX'), ('์—ฌํ–‰', 'NNG'), ('์„', 'JKO'), ('๊ฐ€๋ณด', 'VV'), ('์•„์š”', 'EFN')] ๊ผฌ๊ผฌ๋งˆ ๋ช…์‚ฌ ์ถ”์ถœ : ['์ฝ”๋”ฉ', '๋‹น์‹ ', '์—ฐํœด', '์—ฌํ–‰'] ์•ž์„œ ์‚ฌ์šฉํ•œ Okt ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์™€ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋Š” ์„ฑ๋Šฅ๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์—, ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์˜ ์„ ํƒ์€ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ํ•„์š” ์šฉ๋„์— ์–ด๋–ค ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๊ฐ€ ๊ฐ€์žฅ ์ ์ ˆํ•œ์ง€๋ฅผ ํŒ๋‹จํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์†๋„๋ฅผ ์ค‘์‹œํ•œ๋‹ค๋ฉด ๋ฉ”์บ…์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 03-02 ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ •์ œ์™€ ์ •๊ทœํ™” ์ฝ”ํผ์Šค์—์„œ ์šฉ๋„์— ๋งž๊ฒŒ ํ† ํฐ์„ ๋ถ„๋ฅ˜ํ•˜๋Š” ์ž‘์—…์„ ํ† ํฐํ™”(tokenization)๋ผ๊ณ  ํ•˜๋ฉฐ, ํ† ํฐํ™” ์ž‘์—… ์ „, ํ›„์—๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์šฉ๋„์— ๋งž๊ฒŒ ์ •์ œ(cleaning) ๋ฐ ์ •๊ทœํ™”(normalization) ํ•˜๋Š” ์ผ์ด ํ•ญ์ƒ ํ•จ๊ป˜ํ•ฉ๋‹ˆ๋‹ค. ์ •์ œ ๋ฐ ์ •๊ทœํ™”์˜ ๋ชฉ์ ์€ ๊ฐ๊ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ •์ œ(cleaning) : ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. ์ •๊ทœํ™”(normalization) : ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์„ ํ†ตํ•ฉ์‹œ์ผœ์„œ ๊ฐ™์€ ๋‹จ์–ด๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค. ์ •์ œ ์ž‘์—…์€ ํ† ํฐํ™” ์ž‘์—…์— ๋ฐฉํ•ด๊ฐ€ ๋˜๋Š” ๋ถ€๋ถ„๋“ค์„ ๋ฐฐ์ œ์‹œํ‚ค๊ณ  ํ† ํฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ† ํฐํ™” ์ž‘์—…๋ณด๋‹ค ์•ž์„œ ์ด๋ฃจ์–ด์ง€๊ธฐ๋„ ํ•˜์ง€๋งŒ, ํ† ํฐํ™” ์ž‘์—… ์ดํ›„์—๋„ ์—ฌ์ „ํžˆ ๋‚จ์•„์žˆ๋Š” ๋…ธ์ด์ฆˆ๋“ค์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ์ง€์†์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์™„๋ฒฝํ•œ ์ •์ œ ์ž‘์—…์€ ์–ด๋ ค์šด ํŽธ์ด๋ผ์„œ, ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์ด ์ •๋„๋ฉด ๋๋‹ค.๋ผ๋Š” ์ผ์ข…์˜ ํ•ฉ์˜์ ์„ ์ฐพ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 1. ๊ทœ์น™์— ๊ธฐ๋ฐ˜ํ•œ ํ‘œ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์˜ ํ†ตํ•ฉ ํ•„์š”์— ๋”ฐ๋ผ ์ง์ ‘ ์ฝ”๋”ฉ์„ ํ†ตํ•ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๊ทœํ™” ๊ทœ์น™์˜ ์˜ˆ๋กœ์„œ ๊ฐ™์€ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Œ์—๋„, ํ‘œ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์„ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ์ •๊ทœํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, USA์™€ US๋Š” ๊ฐ™์€ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ์ •๊ทœํ™”ํ•ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. uh-huh์™€ uhhuh๋Š” ํ˜•ํƒœ๋Š” ๋‹ค๋ฅด์ง€๋งŒ ์—ฌ์ „ํžˆ ๊ฐ™์€ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ •๊ทœํ™”๋ฅผ ๊ฑฐ์น˜๊ฒŒ ๋˜๋ฉด, US๋ฅผ ์ฐพ์•„๋„ USA๋„ ํ•จ๊ป˜ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋’ค์—์„œ ํ‘œ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์„ ํ†ตํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ ์–ด๊ฐ„ ์ถ”์ถœ(stemming)๊ณผ ํ‘œ์ œ์–ด ์ถ”์ถœ(lemmatizaiton)์— ๋Œ€ํ•ด์„œ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. 2. ๋Œ€, ์†Œ๋ฌธ์ž ํ†ตํ•ฉ ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ ๋Œ€, ์†Œ๋ฌธ์ž๋ฅผ ํ†ตํ•ฉํ•˜๋Š” ๊ฒƒ์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ์ •๊ทœํ™” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ ๋Œ€๋ฌธ์ž๋Š” ๋ฌธ์žฅ์˜ ๋งจ ์•ž ๋“ฑ๊ณผ ๊ฐ™์€ ํŠน์ • ์ƒํ™ฉ์—์„œ๋งŒ ์“ฐ์ด๊ณ , ๋Œ€๋ถ€๋ถ„์˜ ๊ธ€์€ ์†Œ๋ฌธ์ž๋กœ ์ž‘์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€, ์†Œ๋ฌธ์ž ํ†ตํ•ฉ ์ž‘์—…์€ ๋Œ€๋ถ€๋ถ„ ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์ž‘์—…์œผ๋กœ ์ด๋ฃจ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์ด ์™œ ์œ ์šฉํ•œ์ง€ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, Automobile์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ๋ฌธ์žฅ์˜ ์ฒซ ๋‹จ์–ด์˜€๊ธฐ ๋•Œ๋ฌธ์— A๊ฐ€ ๋Œ€๋ฌธ์ž์˜€๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์— ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์„ ์‚ฌ์šฉํ•˜๋ฉด, automobile์„ ์ฐพ๋Š” ์งˆ์˜(query)์˜ ๊ฒฐ๊ณผ๋กœ์„œ Automobile๋„ ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰ ์—”์ง„์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ํŽ˜๋ผ๋ฆฌ ์ฐจ๋Ÿ‰์— ๊ด€์‹ฌ์ด ์žˆ์–ด์„œ ํŽ˜๋ผ๋ฆฌ๋ฅผ ๊ฒ€์ƒ‰ํ•ด ๋ณธ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์—„๋ฐ€ํžˆ ๋งํ•ด์„œ ์‚ฌ์‹ค ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฒฐ๊ณผ๋Š” a Ferrari car๋ผ๊ณ  ๋ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฒ€์ƒ‰ ์—”์ง„์€ ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์„ ์ ์šฉํ–ˆ์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ferrari๋งŒ ์ณ๋„ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๋ฌด์ž‘์ • ํ†ตํ•ฉํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ๊ตฌ๋ถ„๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น ๋ฏธ๊ตญ์„ ๋œปํ•˜๋Š” ๋‹จ์–ด US์™€ ์šฐ๋ฆฌ๋ฅผ ๋œปํ•˜๋Š” us๋Š” ๊ตฌ๋ถ„๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ ํšŒ์‚ฌ ์ด๋ฆ„(General Motors)๋‚˜, ์‚ฌ๋žŒ ์ด๋ฆ„(Bush) ๋“ฑ์€ ๋Œ€๋ฌธ์ž๋กœ ์œ ์ง€๋˜๋Š” ๊ฒƒ์ด ์˜ณ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ํ† ํฐ์„ ์†Œ๋ฌธ์ž๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ฌธ์ œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค๋ฉด, ๋˜ ๋‹ค๋ฅธ ๋Œ€์•ˆ์€ ์ผ๋ถ€๋งŒ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์ด๋Ÿฐ ๊ทœ์น™์€ ์–ด๋–จ๊นŒ์š”? ๋ฌธ์žฅ์˜ ๋งจ ์•ž์—์„œ ๋‚˜์˜ค๋Š” ๋‹จ์–ด์˜ ๋Œ€๋ฌธ์ž๋งŒ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๊ณ , ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์€ ์ „๋ถ€ ๋Œ€๋ฌธ์ž์ธ ์ƒํƒœ๋กœ ๋†”๋‘๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ž‘์—…์€ ๋” ๋งŽ์€ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์†Œ๋ฌธ์ž ๋ณ€ํ™˜์„ ์–ธ์ œ ์‚ฌ์šฉํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ์‹œํ€€์Šค ๋ชจ๋ธ๋กœ ๋” ์ •ํ™•ํ•˜๊ฒŒ ์ง„ํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ์˜ฌ๋ฐ”๋ฅธ ๋Œ€๋ฌธ์ž ๋‹จ์–ด๋ฅผ ์–ป๊ณ  ์‹ถ์€ ์ƒํ™ฉ์—์„œ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•˜๋Š” ์ฝ”ํผ์Šค๊ฐ€ ์‚ฌ์šฉ์ž๋“ค์ด ๋‹จ์–ด์˜ ๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž์˜ ์˜ฌ๋ฐ”๋ฅธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ ์ƒ๊ด€์—†์ด ์†Œ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ๋“ค๋กœ๋ถ€ํ„ฐ ๋‚˜์˜จ ๋ฐ์ดํ„ฐ๋ผ๋ฉด ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ• ๋˜ํ•œ ๊ทธ๋‹ค์ง€ ๋„์›€์ด ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ์—๋Š” ์˜ˆ์™ธ ์‚ฌํ•ญ์„ ํฌ๊ฒŒ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ , ๋ชจ๋“  ์ฝ”ํผ์Šค๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์ข…์ข… ๋” ์‹ค์šฉ์ ์ธ ํ•ด๊ฒฐ์ฑ…์ด ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 3. ๋ถˆํ•„์š”ํ•œ ๋‹จ์–ด์˜ ์ œ๊ฑฐ ์ •์ œ ์ž‘์—…์—์„œ ์ œ๊ฑฐํ•ด์•ผ ํ•˜๋Š” ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ(noise data)๋Š” ์ž์—ฐ์–ด๊ฐ€ ์•„๋‹ˆ๋ฉด์„œ ์•„๋ฌด ์˜๋ฏธ๋„ ๊ฐ–์ง€ ์•Š๋Š” ๊ธ€์ž๋“ค(ํŠน์ˆ˜ ๋ฌธ์ž ๋“ฑ)์„ ์˜๋ฏธํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ชฉ์ ์— ๋งž์ง€ ์•Š๋Š” ๋ถˆํ•„์š” ๋‹จ์–ด๋“ค์„ ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋ถˆํ•„์š” ๋‹จ์–ด๋“ค์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ์™€ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ์ ์€ ๋‹จ์–ด, ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋“ค์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ๋Š” ๋ถˆ์šฉ์–ด ์ฑ•ํ„ฐ์—์„œ ๋”์šฑ ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ธฐ๋กœ ํ•˜๊ณ , ์—ฌ๊ธฐ์„œ๋Š” ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ์ ์€ ๋‹จ์–ด์™€ ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ๊ฐ„๋žตํžˆ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. (1) ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ์ ์€ ๋‹จ์–ด ๋•Œ๋กœ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ๋„ˆ๋ฌด ์ ๊ฒŒ ๋“ฑ์žฅํ•ด์„œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ๋„์›€์ด ๋˜์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž…๋ ฅ๋œ ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ธ์ง€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ์„ค๊ณ„ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด 100,000๊ฐœ์˜ ๋ฉ”์ผ์„ ๊ฐ€์ง€๊ณ  ์ •์ƒ ๋ฉ”์ผ์—์„œ๋Š” ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ์ฃผ๋กœ ๋“ฑ์žฅํ•˜๊ณ , ์ŠคํŒธ ๋ฉ”์ผ์—์„œ๋Š” ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ์ฃผ๋กœ ๋“ฑ์žฅํ•˜๋Š”์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์„ค๊ณ„ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋•Œ 100,000๊ฐœ์˜ ๋ฉ”์ผ ๋ฐ์ดํ„ฐ์—์„œ ์ดํ•ฉ 5๋ฒˆ ๋ฐ–์— ๋“ฑ์žฅํ•˜์ง€ ์•Š์€ ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด ๋‹จ์–ด๋Š” ์ง๊ด€์ ์œผ๋กœ ๋ถ„๋ฅ˜์— ๊ฑฐ์˜ ๋„์›€์ด ๋˜์ง€ ์•Š์„ ๊ฒƒ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (2) ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ๋Š” ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ์–ด๋Š ์ •๋„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํฌ๊ฒŒ ์˜๋ฏธ๊ฐ€ ์—†๋Š” ๋‹จ์–ด๋“ค์„ ์ œ๊ฑฐํ•˜๋Š” ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์˜์–ด๊ถŒ ์–ธ์–ด์—์„œ ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๋ถˆ์šฉ์–ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” 2์ฐจ ์ด์œ ๋Š” ๊ธธ์ด๋ฅผ ์กฐ๊ฑด์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ์‚ญ์ œํ•˜๋ฉด์„œ ๋‹จ์–ด๊ฐ€ ์•„๋‹Œ ๊ตฌ๋‘์ ๋“ค๊นŒ์ง€๋„ ํ•œ๊บผ๋ฒˆ์— ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•จ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•œ๊ตญ์–ด์—์„œ๋Š” ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋ผ๊ณ  ์‚ญ์ œํ•˜๋Š” ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ํฌ๊ฒŒ ์œ ํšจํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ทธ ์ด์œ ์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์ •์ ์œผ๋กœ ๋งํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ, ์˜์–ด ๋‹จ์–ด์˜ ํ‰๊ท  ๊ธธ์ด๋Š” 6~7 ์ •๋„์ด๋ฉฐ, ํ•œ๊ตญ์–ด ๋‹จ์–ด์˜ ํ‰๊ท  ๊ธธ์ด๋Š” 2~3 ์ •๋„๋กœ ์ถ”์ •๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋‚˜๋ผ์˜ ๋‹จ์–ด ํ‰๊ท  ๊ธธ์ด๊ฐ€ ๋ช‡ ์ธ์ง€์— ๋Œ€ํ•ด์„œ๋Š” ํ™•์‹คํžˆ ๋งํ•˜๊ธฐ ์–ด๋ ต์ง€๋งŒ ๊ทธ๋Ÿผ์—๋„ ํ™•์‹คํ•œ ์‚ฌ์‹ค์€ ์˜์–ด ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ํ•œ๊ตญ์–ด ๋‹จ์–ด์˜ ๊ธธ์ด๋ณด๋‹ค๋Š” ํ‰๊ท ์ ์œผ๋กœ ๊ธธ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์˜์–ด ๋‹จ์–ด์™€ ํ•œ๊ตญ์–ด ๋‹จ์–ด์—์„œ ๊ฐ ํ•œ ๊ธ€์ž๊ฐ€ ๊ฐ€์ง„ ์˜๋ฏธ์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ์ ์—์„œ ๊ธฐ์ธํ•ฉ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ๋‹จ์–ด๋Š” ํ•œ์ž์–ด๊ฐ€ ๋งŽ๊ณ , ํ•œ ๊ธ€์ž๋งŒ์œผ๋กœ๋„ ์ด๋ฏธ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 'ํ•™๊ต'๋ผ๋Š” ํ•œ๊ตญ์–ด ๋‹จ์–ด๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด, ๋ฐฐ์šธ ํ•™(ๅญธ)๊ณผ ํ•™๊ต ๊ต(ๆ ก)๋กœ ๊ธ€์ž ํ•˜๋‚˜, ํ•˜๋‚˜๊ฐ€ ์ด๋ฏธ ํ•จ์ถ•์ ์ธ ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์–ด ๋‘ ๊ธ€์ž๋งŒ์œผ๋กœ ํ•™๊ต๋ผ๋Š” ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์˜์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ํ•™๊ต๋ผ๋Š” ๊ธ€์ž๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” s, c, h, o, o, l์ด๋ผ๋Š” ์ด 6๊ฐœ์˜ ๊ธ€์ž๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์˜ˆ๋กœ๋Š” ์ „์„ค ์† ๋™๋ฌผ์ธ ์šฉ()์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•œ๊ตญ์–ด๋กœ๋Š” ํ•œ ๊ธ€์ž ๋ฉด ์ถฉ๋ถ„ํ•˜์ง€๋งŒ, ์˜์–ด์—์„œ๋Š” d, r, a, g, o, n์ด๋ผ๋Š” ์ด 6๊ฐœ์˜ ๊ธ€์ž๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ์˜์–ด๋Š” ๊ธธ์ด๊ฐ€ 2~3 ์ดํ•˜์ธ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ํฌ๊ฒŒ ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ๋ชปํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์ค„์ด๋Š” ํšจ๊ณผ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฐ–๊ณ  ์žˆ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ๊ธธ์ด๊ฐ€ 1์ธ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋Œ€๋ถ€๋ถ„์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ๋ชปํ•˜๋Š” ๋‹จ์–ด์ธ ๊ด€์‚ฌ 'a'์™€ ์ฃผ์–ด๋กœ ์“ฐ์ด๋Š” 'I'๊ฐ€ ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ธธ์ด๊ฐ€ 2์ธ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค๊ณ  ํ•˜๋ฉด it, at, to, on, in, by ๋“ฑ๊ณผ ๊ฐ™์€ ๋Œ€๋ถ€๋ถ„ ๋ถˆ์šฉ์–ด์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด๋“ค์ด ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค. ํ•„์š”์— ๋”ฐ๋ผ์„œ๋Š” ๊ธธ์ด๊ฐ€ 3์ธ ๋‹จ์–ด๋„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด ๊ฒฝ์šฐ fox, dog, car ๋“ฑ ๊ธธ์ด๊ฐ€ 3์ธ ๋ช…์‚ฌ๋“ค์ด ์ œ๊ฑฐ๋˜๊ธฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ์—์„œ ํ•ด๋‹น ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋„ ๋˜๋Š”์ง€์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. import re text = "I was wondering if anyone out there could enlighten me on this car." # ๊ธธ์ด๊ฐ€ 1~2์ธ ๋‹จ์–ด๋“ค์„ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ์‚ญ์ œ shortword = re.compile(r'\W*\b\w{1,2}\b') print(shortword.sub('', text)) was wondering anyone out there could enlighten this car. 4. ์ •๊ทœ ํ‘œํ˜„์‹(Regular Expression) ์–ป์–ด๋‚ธ ์ฝ”ํผ์Šค์—์„œ ๋…ธ์ด์ฆˆ ๋ฐ์ดํ„ฐ์˜ ํŠน์ง•์„ ์žก์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ†ตํ•ด์„œ ์ด๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, HTML ๋ฌธ์„œ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜จ ์ฝ”ํผ์Šค๋ผ๋ฉด ๋ฌธ์„œ ์—ฌ๊ธฐ์ €๊ธฐ์— HTML ํƒœ๊ทธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‰ด์Šค ๊ธฐ์‚ฌ๋ฅผ ํฌ๋กค๋ง ํ–ˆ๋‹ค๋ฉด, ๊ธฐ์‚ฌ๋งˆ๋‹ค ๊ฒŒ์žฌ ์‹œ๊ฐ„์ด ์ ํ˜€์ ธ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์€ ์ด๋Ÿฌํ•œ ์ฝ”ํผ์Šค ๋‚ด์— ๊ณ„์†ํ•ด์„œ ๋“ฑ์žฅํ•˜๋Š” ๊ธ€์ž๋“ค์„ ๊ทœ์น™์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ํ•œ ๋ฒˆ์— ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ์„œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋„ ์ „์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ •๊ทœ ํ‘œํ˜„์‹์„ ์•ž์œผ๋กœ ์ข…์ข… ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๊ฒ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์—์„œ ๊ธธ์ด๊ฐ€ ์งง์€ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ๋„, ์ •๊ทœ ํ‘œํ˜„์‹์ด ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋’ค์—์„œ ์ข€ ๋” ์ƒ์„ธํ•˜๊ฒŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 03-03 ๋ถˆ์šฉ์–ด(Stopwords) ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์—์„œ ์œ ์˜๋ฏธํ•œ ๋‹จ์–ด ํ† ํฐ๋งŒ์„ ์„ ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํฐ ์˜๋ฏธ๊ฐ€ ์—†๋Š” ๋‹จ์–ด ํ† ํฐ์„ ์ œ๊ฑฐํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํฐ ์˜๋ฏธ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์€ ์ž์ฃผ ๋“ฑ์žฅํ•˜์ง€๋งŒ ๋ถ„์„์„ ํ•˜๋Š” ๊ฒƒ์— ์žˆ์–ด์„œ๋Š” ํฐ ๋„์›€์ด ๋˜์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, I, my, me, over, ์กฐ์‚ฌ, ์ ‘๋ฏธ์‚ฌ ๊ฐ™์€ ๋‹จ์–ด๋“ค์€ ๋ฌธ์žฅ์—์„œ๋Š” ์ž์ฃผ ๋“ฑ์žฅํ•˜์ง€๋งŒ ์‹ค์ œ ์˜๋ฏธ ๋ถ„์„์„ ํ•˜๋Š” ๋ฐ๋Š” ๊ฑฐ์˜ ๊ธฐ์—ฌํ•˜๋Š” ๋ฐ”๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋‹จ์–ด๋“ค์„ ๋ถˆ์šฉ์–ด(stopword)๋ผ๊ณ  ํ•˜๋ฉฐ, NLTK์—์„œ๋Š” ์œ„์™€ ๊ฐ™์€ 100์—ฌ ๊ฐœ ์ด์ƒ์˜ ์˜์–ด ๋‹จ์–ด๋“ค์„ ๋ถˆ์šฉ์–ด๋กœ ํŒจํ‚ค์ง€ ๋‚ด์—์„œ ๋ฏธ๋ฆฌ ์ •์˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋ถˆ์šฉ์–ด๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์˜์–ด ๋ฌธ์žฅ์—์„œ NLTK๊ฐ€ ์ •์˜ํ•œ ์˜์–ด ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์‹ค์Šต์„ ํ•˜๊ณ , ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์—์„œ ์ง์ ‘ ์ •์˜ํ•œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. NLTK ์‹ค์Šต์—์„œ๋Š” 1์ฑ•ํ„ฐ์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด NLTK Data๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ ์‹œ์—๋Š” nltk.download(ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ)๋ผ๋Š” ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from nltk.corpus import stopwords from nltk.tokenize import word_tokenize from konlpy.tag import Okt 1. NLTK์—์„œ ๋ถˆ์šฉ์–ด ํ™•์ธํ•˜๊ธฐ stop_words_list = stopwords.words('english') print('๋ถˆ์šฉ์–ด ๊ฐœ์ˆ˜ :', len(stop_words_list)) print('๋ถˆ์šฉ์–ด 10๊ฐœ ์ถœ๋ ฅ :',stop_words_list[:10]) ๋ถˆ์šฉ์–ด ๊ฐœ์ˆ˜ : 179 ๋ถˆ์šฉ์–ด 10๊ฐœ ์ถœ๋ ฅ : ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're"] stopwords.words("english")๋Š” NLTK๊ฐ€ ์ •์˜ํ•œ ์˜์–ด ๋ถˆ์šฉ์–ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ 100๊ฐœ ์ด์ƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํžˆ 10๊ฐœ๋งŒ ํ™•์ธํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. 'i', 'me', 'my'์™€ ๊ฐ™์€ ๋‹จ์–ด๋“ค์„ NLTK์—์„œ ๋ถˆ์šฉ์–ด๋กœ ์ •์˜ํ•˜๊ณ  ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. NLTK๋ฅผ ํ†ตํ•ด์„œ ๋ถˆ์šฉ์–ด ์ œ๊ฑฐํ•˜๊ธฐ example = "Family is not an important thing. It's everything." stop_words = set(stopwords.words('english')) word_tokens = word_tokenize(example) result = [] for word in word_tokens: if word not in stop_words: result.append(word) print('๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ์ „ :',word_tokens) print('๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ํ›„ :',result) ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ์ „ : ['Family', 'is', 'not', 'an', 'important', 'thing', '.', 'It', "'s", 'everything', '.'] ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ํ›„ : ['Family', 'important', 'thing', '.', 'It', "'s", 'everything', '.'] ์œ„ ์ฝ”๋“œ๋Š” "Family is not an important thing. It's everything."๋ผ๋Š” ์ž„์˜์˜ ๋ฌธ์žฅ์„ ์ •์˜ํ•˜๊ณ , NLTK์˜ word_tokenize๋ฅผ ํ†ตํ•ด์„œ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹จ์–ด ํ† ํฐํ™” ๊ฒฐ๊ณผ๋กœ๋ถ€ํ„ฐ NLTK๊ฐ€ ์ •์˜ํ•˜๊ณ  ์žˆ๋Š” ๋ถˆ์šฉ์–ด๋ฅผ ์ œ์™ธํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ 'is', 'not', 'an'๊ณผ ๊ฐ™์€ ๋‹จ์–ด๋“ค์ด ๋ฌธ์žฅ์—์„œ ์ œ๊ฑฐ๋˜์—ˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ํ•œ๊ตญ์–ด์—์„œ ๋ถˆ์šฉ์–ด ์ œ๊ฑฐํ•˜๊ธฐ ํ•œ๊ตญ์–ด์—์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ๋Š” ํ† ํฐํ™” ํ›„์— ์กฐ์‚ฌ, ์ ‘์†์‚ฌ ๋“ฑ์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋ ค๊ณ  ํ•˜๋‹ค ๋ณด๋ฉด ์กฐ์‚ฌ๋‚˜ ์ ‘์†์‚ฌ์™€ ๊ฐ™์€ ๋‹จ์–ด๋“ค๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ช…์‚ฌ, ํ˜•์šฉ์‚ฌ์™€ ๊ฐ™์€ ๋‹จ์–ด๋“ค ์ค‘์—์„œ ๋ถˆ์šฉ์–ด๋กœ์„œ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ์€ ๋‹จ์–ด๋“ค์ด ์ƒ๊ธฐ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ์—๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ๋ถˆ์šฉ์–ด ์‚ฌ์ „์„ ๋งŒ๋“ค๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ง์ ‘ ๋ถˆ์šฉ์–ด๋ฅผ ์ •์˜ํ•ด ๋ณด๊ณ , ์ฃผ์–ด์ง„ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ๋ถˆ์šฉ์–ด ์‚ฌ์ „์œผ๋กœ๋ถ€ํ„ฐ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋ถˆ์šฉ์–ด๋Š” ์ž„์˜ ์„ ์ •ํ•œ ๊ฒƒ์œผ๋กœ ์‹ค์ œ ์˜๋ฏธ ์žˆ๋Š” ์„ ์ • ๊ธฐ์ค€์ด ์•„๋‹™๋‹ˆ๋‹ค. okt = Okt() example = "๊ณ ๊ธฐ๋ฅผ ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ๊ตฌ์šฐ๋ ค๊ณ  ํ•˜๋ฉด ์•ˆ ๋ผ. ๊ณ ๊ธฐ๋ผ๊ณ  ๋‹ค ๊ฐ™์€ ๊ฒŒ ์•„๋‹ˆ๊ฑฐ๋“ . ์˜ˆ์ปจ๋Œ€ ์‚ผ๊ฒน์‚ด์„ ๊ตฌ์šธ ๋•Œ๋Š” ์ค‘์š”ํ•œ ๊ฒŒ ์žˆ์ง€." stop_words = "๋ฅผ ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ๊ตฌ ์šฐ๋ ค ๊ณ  ์•ˆ ๋ผ ๊ฐ™์€ ๊ฒŒ ๊ตฌ์šธ ๋•Œ๋Š”" stop_words = set(stop_words.split(' ')) word_tokens = okt.morphs(example) result = [word for word in word_tokens if not word in stop_words] print('๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ์ „ :',word_tokens) print('๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ํ›„ :',result) ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ์ „ : ['๊ณ ๊ธฐ', '๋ฅผ', '์•„๋ฌด๋ ‡๊ฒŒ๋‚˜', '๊ตฌ', '์šฐ๋ ค', '๊ณ ', 'ํ•˜๋ฉด', '์•ˆ', '๋ผ', '.', '๊ณ ๊ธฐ', '๋ผ๊ณ ', '๋‹ค', '๊ฐ™์€', '๊ฒŒ', '์•„๋‹ˆ๊ฑฐ๋“ ', '.', '์˜ˆ์ปจ๋Œ€', '์‚ผ๊ฒน์‚ด', '์„', '๊ตฌ์šธ', '๋•Œ', '๋Š”', '์ค‘์š”ํ•œ', '๊ฒŒ', '์žˆ์ง€', '.'] ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ํ›„ : ['๊ณ ๊ธฐ', 'ํ•˜๋ฉด', '.', '๊ณ ๊ธฐ', '๋ผ๊ณ ', '๋‹ค', '์•„๋‹ˆ๊ฑฐ๋“ ', '.', '์˜ˆ์ปจ๋Œ€', '์‚ผ๊ฒน์‚ด', '์„', '์ค‘์š”ํ•œ', '์žˆ์ง€', '.'] ์•„๋ž˜์˜ ๋งํฌ๋Š” ๋ณดํŽธ์ ์œผ๋กœ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ๊ตญ์–ด ๋ถˆ์šฉ์–ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ ์ ˆ๋Œ€์ ์ธ ๊ธฐ์ค€์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋งํฌ : https://www.ranks.nl/stopwords/korean ๋ถˆ์šฉ์–ด๊ฐ€ ๋งŽ์€ ๊ฒฝ์šฐ์—๋Š” ์ฝ”๋“œ ๋‚ด์—์„œ ์ง์ ‘ ์ •์˜ํ•˜์ง€ ์•Š๊ณ  txt ํŒŒ์ผ์ด๋‚˜ csv ํŒŒ์ผ๋กœ ์ •๋ฆฌํ•ด๋†“๊ณ  ์ด๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 03-04 ์ •๊ทœ ํ‘œํ˜„์‹ ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ์—์„œ ์ •๊ทœ ํ‘œํ˜„์‹์€ ์•„์ฃผ ์œ ์šฉํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ํŒŒ์ด์ฌ์—์„œ ์ง€์›ํ•˜๊ณ  ์žˆ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ re์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ NLTK๋ฅผ ํ†ตํ•œ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ด์šฉํ•œ ํ† ํฐํ™”์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. 1. ์ •๊ทœ ํ‘œํ˜„์‹ ๋ฌธ๋ฒ•๊ณผ ๋ชจ๋“ˆ ํ•จ์ˆ˜ ํŒŒ์ด์ฌ์—์„œ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ re์„ ์ง€์›ํ•˜๋ฏ€๋กœ, ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด ํŠน์ • ๊ทœ์น™์ด ์žˆ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ •์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํŠน์ˆ˜ ๋ฌธ์ž์™€ ๋ชจ๋“ˆ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ํ‘œ๋ฅผ ํ†ตํ•ด ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ‘œ๋งŒ์œผ๋กœ๋Š” ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ํ‘œ ์•„๋ž˜์˜ ์‹ค์Šต๊ณผ ํ‘œ๋ฅผ ๋ณ‘ํ–‰ํ•˜์—ฌ ์ดํ•ดํ•˜์‹œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. 1) ์ •๊ทœ ํ‘œํ˜„์‹ ๋ฌธ๋ฒ• ์ •๊ทœ ํ‘œํ˜„์‹์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ๋ฒ• ์ค‘ ํŠน์ˆ˜ ๋ฌธ์ž๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํŠน์ˆ˜ ๋ฌธ์ž ์„ค๋ช… . ํ•œ ๊ฐœ์˜ ์ž„์˜์˜ ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. (์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž์ธ \n๋Š” ์ œ์™ธ) ? ์•ž์˜ ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. (๋ฌธ์ž๊ฐ€ 0๊ฐœ ๋˜๋Š” 1๊ฐœ) * ์•ž์˜ ๋ฌธ์ž๊ฐ€ ๋ฌดํ•œ๊ฐœ๋กœ ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. (๋ฌธ์ž๊ฐ€ 0๊ฐœ ์ด์ƒ) + ์•ž์˜ ๋ฌธ์ž๊ฐ€ ์ตœ์†Œ ํ•œ ๊ฐœ ์ด์ƒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. (๋ฌธ์ž๊ฐ€ 1๊ฐœ ์ด์ƒ) ^ ๋’ค์˜ ๋ฌธ์ž์—ด๋กœ ๋ฌธ์ž์—ด์ด ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค. $ ์•ž์˜ ๋ฌธ์ž์—ด๋กœ ๋ฌธ์ž์—ด์ด ๋๋‚ฉ๋‹ˆ๋‹ค. {์ˆซ์ž} ์ˆซ์ž๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. {์ˆซ์ž 1, ์ˆซ์ž 2} ์ˆซ์ž 1 ์ด์ƒ ์ˆซ์ž 2 ์ดํ•˜๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ?, *, +๋ฅผ ์ด๊ฒƒ์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. {์ˆซ์ž,} ์ˆซ์ž ์ด์ƒ๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. [ ] ๋Œ€๊ด„ํ˜ธ ์•ˆ์˜ ๋ฌธ์ž๋“ค ์ค‘ ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์™€ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. [amk]๋ผ๊ณ  ํ•œ๋‹ค๋ฉด a ๋˜๋Š” m ๋˜๋Š” k ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์กด์žฌํ•˜๋ฉด ๋งค์น˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [a-z]์™€ ๊ฐ™์ด ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. [a-zA-Z]๋Š” ์•ŒํŒŒ๋ฒณ ์ „์ฒด๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ฒ”์œ„์ด๋ฉฐ, ๋ฌธ์ž์—ด์— ์•ŒํŒŒ๋ฒณ์ด ์กด์žฌํ•˜๋ฉด ๋งค์น˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [^๋ฌธ์ž] ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋ฌธ์ž๋ฅผ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. l AlB์™€ ๊ฐ™์ด ์“ฐ์ด๋ฉฐ A ๋˜๋Š” B์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹ ๋ฌธ๋ฒ•์—๋Š” ์—ญ ์Šฌ๋ž˜์‰ฌ(\)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž์ฃผ ์“ฐ์ด๋Š” ๋ฌธ์ž ๊ทœ์น™๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž ๊ทœ์น™ ์„ค๋ช… \\\ ์—ญ ์Šฌ๋ž˜์‰ฌ ๋ฌธ์ž ์ž์ฒด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค \\d ๋ชจ๋“  ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [0-9]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. \\D ์ˆซ์ž๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [^0-9]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. \\s ๊ณต๋ฐฑ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [ \t\n\r\f\v]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. \\S ๊ณต๋ฐฑ์„ ์ œ์™ธํ•œ ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [^ \t\n\r\f\v]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. \\w ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [a-zA-Z0-9]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. \\W ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. [^a-zA-Z0-9]์™€ ์˜๋ฏธ๊ฐ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. 2) ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ ํ•จ์ˆ˜ ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ์—์„œ ์ง€์›ํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ชจ๋“ˆ ํ•จ์ˆ˜ ์„ค๋ช… re.compile() ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ปดํŒŒ์ผํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ํŒŒ์ด์ฌ์—๊ฒŒ ์ „ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ฐพ๊ณ ์ž ํ•˜๋Š” ํŒจํ„ด์ด ๋นˆ๋ฒˆํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ฏธ๋ฆฌ ์ปดํŒŒ์ผํ•ด๋†“๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ์†๋„์™€ ํŽธ ์˜์„ฑ๋ฉด์—์„œ ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. re.search() ๋ฌธ์ž์—ด ์ „์ฒด์— ๋Œ€ํ•ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋˜๋Š”์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. re.match() ๋ฌธ์ž์—ด์˜ ์ฒ˜์Œ์ด ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋˜๋Š”์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. re.split() ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. re.findall() ๋ฌธ์ž์—ด์—์„œ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ๋ฌธ์ž์—ด์„ ์ฐพ์•„์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋งค์น˜๋˜๋Š” ๋ฌธ์ž์—ด์ด ์—†๋‹ค๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค. re.finditer() ๋ฌธ์ž์—ด์—์„œ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์ดํ„ฐ ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. re.sub() ๋ฌธ์ž์—ด์—์„œ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ง„ํ–‰๋  ์‹ค์Šต์—์„œ๋Š” re.compile()์— ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ปดํŒŒ์ผํ•˜๊ณ , re.search()๋ฅผ ํ†ตํ•ด์„œ ํ•ด๋‹น ์ •๊ทœ ํ‘œํ˜„์‹์ด ์ž…๋ ฅ ํ…์ŠคํŠธ์™€ ๋งค์น˜๋˜๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋ฉด์„œ ๊ฐ ์ •๊ทœ ํ‘œํ˜„์‹์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. re.search()๋Š” ๋งค์น˜๋œ๋‹ค๋ฉด Match Object๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ๋งค์น˜๋˜์ง€ ์•Š์œผ๋ฉด ์•„๋ฌด๋Ÿฐ ๊ฐ’๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 2. ์ •๊ทœ ํ‘œํ˜„์‹ ์‹ค์Šต ์•ž์„œ ํ‘œ๋กœ ๋ดค๋˜ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import re 1) . ๊ธฐํ˜ธ .์€ ํ•œ ๊ฐœ์˜ ์ž„์˜์˜ ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด a.c๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. a์™€ c ์‚ฌ์ด์—๋Š” ์–ด๋–ค 1๊ฐœ์˜ ๋ฌธ์ž๋ผ๋„ ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. akc, azc, avc, a5c, a! c์™€ ๊ฐ™์€ ํ˜•ํƒœ๋Š” ๋ชจ๋‘ a.c์˜ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋ฉ๋‹ˆ๋‹ค. r = re.compile("a.c") r.search("kkk") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("abc") <_sre.SRE_Match object; span=(0, 3), match='abc'> .์€ ์–ด๋–ค ๋ฌธ์ž๋กœ๋„ ์ธ์‹๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— abc๋ผ๋Š” ๋ฌธ์ž์—ด์€ a.c๋ผ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ํŒจํ„ด์œผ๋กœ ๋งค์น˜๋ฉ๋‹ˆ๋‹ค. 2) ?๊ธฐํ˜ธ ?๋Š”?์•ž์˜ ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ๊ณ  ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด ab? c๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ ์ด ์ •๊ทœ ํ‘œํ˜„์‹์—์„œ์˜ b๋Š” ์žˆ๋‹ค๊ณ  ์ทจ๊ธ‰ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์—†๋‹ค๊ณ  ์ทจ๊ธ‰ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, abc์™€ ac ๋ชจ๋‘ ๋งค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. r = re.compile("ab? c") r.search("abbc") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("abc") <_sre.SRE_Match object; span=(0, 3), match='abc'> b๊ฐ€ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•˜์—ฌ abc๋ฅผ ๋งค์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค. r.search("ac") <_sre.SRE_Match object; span=(0, 2), match='ac'> b๊ฐ€ ์—†๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•˜์—ฌ ac๋ฅผ ๋งค์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค. 3) *๊ธฐํ˜ธ *์€ ๋ฐ”๋กœ ์•ž์˜ ๋ฌธ์ž๊ฐ€ 0๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์•ž์˜ ๋ฌธ์ž๋Š” ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์ด ab*c๋ผ๋ฉด ac, abc, abbc, abbbc ๋“ฑ๊ณผ ๋งค์น˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ b์˜ ๊ฐœ์ˆ˜๋Š” ๋ฌด์ˆ˜ํžˆ ๋งŽ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. r = re.compile("ab*c") r.search("a") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("ac") <_sre.SRE_Match object; span=(0, 2), match='ac'> r.search("abc") <_sre.SRE_Match object; span=(0, 3), match='abc'> r.search("abbbbc") <_sre.SRE_Match object; span=(0, 6), match='abbbbc'> 4) +๊ธฐํ˜ธ +๋Š” *์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ ์€ ์•ž์˜ ๋ฌธ์ž๊ฐ€ ์ตœ์†Œ 1๊ฐœ ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์ด ab+c๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ac๋Š” ๋งค์น˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ abc, abbc, abbbc ๋“ฑ๊ณผ ๋งค์น˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ b์˜ ๊ฐœ์ˆ˜๋Š” ๋ฌด์ˆ˜ํžˆ ๋งŽ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. r = re.compile("ab+c") r.search("ac") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("abc") <_sre.SRE_Match object; span=(0, 3), match='abc'> r.search("abbbbc") <_sre.SRE_Match object; span=(0, 6), match='abbbbc'> 5) ^๊ธฐํ˜ธ ^๋Š” ์‹œ์ž‘๋˜๋Š” ๋ฌธ์ž์—ด์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์ด ^ab๋ผ๋ฉด ๋ฌธ์ž์—ด ab๋กœ ์‹œ์ž‘๋˜๋Š” ๊ฒฝ์šฐ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. r = re.compile("^ab") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("bbc") r.search("zab") r.search("abz") <re.Match object; span=(0, 2), match='ab'> 6) {์ˆซ์ž} ๊ธฐํ˜ธ ๋ฌธ์ž์— ํ•ด๋‹น ๊ธฐํ˜ธ๋ฅผ ๋ถ™์ด๋ฉด, ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋งŒํผ ๋ฐ˜๋ณตํ•œ ๊ฒƒ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด ab{2}c๋ผ๋ฉด a์™€ c ์‚ฌ์ด์— b๊ฐ€ ์กด์žฌํ•˜๋ฉด์„œ b๊ฐ€ 2๊ฐœ์ธ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. r = re.compile("ab{2}c") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("ac") r.search("abc") r.search("abbbbbc") r.search("abbc") <_sre.SRE_Match object; span=(0, 4), match='abbc'> 7) {์ˆซ์ž 1, ์ˆซ์ž 2} ๊ธฐํ˜ธ ๋ฌธ์ž์— ํ•ด๋‹น ๊ธฐํ˜ธ๋ฅผ ๋ถ™์ด๋ฉด, ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ˆซ์ž 1 ์ด์ƒ ์ˆซ์ž 2 ์ดํ•˜๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด ab{2,8}c๋ผ๋ฉด a์™€ c ์‚ฌ์ด์— b๊ฐ€ ์กด์žฌํ•˜๋ฉด์„œ b๋Š” 2๊ฐœ ์ด์ƒ 8๊ฐœ ์ดํ•˜์ธ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. r = re.compile("ab{2,8}c") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("ac") r.search("abc") r.search("abbbbbbbbbc") r.search("abbc") <_sre.SRE_Match object; span=(0, 4), match='abbc'> r.search("abbbbbbbbc") <_sre.SRE_Match object; span=(0, 10), match='abbbbbbbbc'> 8) {์ˆซ์ž,} ๊ธฐํ˜ธ ๋ฌธ์ž์— ํ•ด๋‹น ๊ธฐํ˜ธ๋ฅผ ๋ถ™์ด๋ฉด ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ˆซ์ž ์ด์ƒ๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด a{2, }bc๋ผ๋ฉด ๋’ค์— bc๊ฐ€ ๋ถ™์œผ๋ฉด์„œ a์˜ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ์ธ ๋ฌธ์ž์—ด๊ณผ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋งŒ์•ฝ {0, }์„ ์“ด๋‹ค๋ฉด *์™€ ๋™์ผํ•œ ์˜๋ฏธ๊ฐ€ ๋˜๋ฉฐ, {1, }์„ ์“ด๋‹ค๋ฉด +์™€ ๋™์ผํ•œ ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. r = re.compile("a{2, }bc") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("bc") r.search("aa") r.search("aabc") <_sre.SRE_Match object; span=(0, 4), match='aabc'> r.search("aaaaaaaabc") <_sre.SRE_Match object; span=(0, 10), match='aaaaaaaabc'> 9) [ ] ๊ธฐํ˜ธ [ ] ์•ˆ์— ๋ฌธ์ž๋“ค์„ ๋„ฃ์œผ๋ฉด ๊ทธ ๋ฌธ์ž๋“ค ์ค‘ ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์™€ ๋งค์น˜๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์ด [abc] ๋ผ๋ฉด, a ๋˜๋Š” b ๋˜๋Š” c๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๋ฌธ์ž์—ด๊ณผ ๋งค์น˜๋ฉ๋‹ˆ๋‹ค. ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. [a-zA-Z]๋Š” ์•ŒํŒŒ๋ฒณ ์ „๋ถ€๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, [0-9]๋Š” ์ˆซ์ž ์ „๋ถ€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. r = re.compile("[abc]") # [abc]๋Š” [a-c]์™€ ๊ฐ™๋‹ค. r.search("zzz") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("a") <_sre.SRE_Match object; span=(0, 1), match='a'> r.search("aaaaaaa") <_sre.SRE_Match object; span=(0, 1), match='a'> r.search("baac") <_sre.SRE_Match object; span=(0, 1), match='b'> ์ด๋ฒˆ์—๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ ๋ฒ”์œ„ ์ง€์ •ํ•˜์—ฌ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๋งŒ๋“ค์–ด๋ณด๊ณ  ๋ฌธ์ž์—ด๊ณผ ๋งค์น˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. r = re.compile("[a-z]") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("AAA") r.search("111") r.search("aBC") <_sre.SRE_Match object; span=(0, 1), match='a'> 10) [^๋ฌธ์ž] ๊ธฐํ˜ธ [^๋ฌธ์ž]๋Š” ^๊ธฐํ˜ธ ๋’ค์— ๋ถ™์€ ๋ฌธ์ž๋“ค์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ๋งค์น˜ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ [^abc]๋ผ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์ด ์žˆ๋‹ค๋ฉด, a ๋˜๋Š” b ๋˜๋Š” c๊ฐ€ ๋“ค์–ด๊ฐ„ ๋ฌธ์ž์—ด์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋งค์น˜ํ•ฉ๋‹ˆ๋‹ค. r = re.compile("[^abc]") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("a") r.search("ab") r.search("b") r.search("d") <_sre.SRE_Match object; span=(0, 1), match='d'> r.search("1") <_sre.SRE_Match object; span=(0, 1), match='1'> 3. ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ ํ•จ์ˆ˜ ์˜ˆ์ œ ์•ž์„œ re.compile()๊ณผ re.search()๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ •๊ทœ ํ‘œํ˜„์‹ ๋ชจ๋“ˆ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (1) re.match() ์™€ re.search()์˜ ์ฐจ์ด search()๊ฐ€ ์ •๊ทœ ํ‘œํ˜„์‹ ์ „์ฒด์— ๋Œ€ํ•ด์„œ ๋ฌธ์ž์—ด์ด ๋งค์น˜ํ•˜๋Š”์ง€๋ฅผ ๋ณธ๋‹ค๋ฉด, match()๋Š” ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜ํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ์ค‘๊ฐ„์— ์ฐพ์„ ํŒจํ„ด์ด ์žˆ๋”๋ผ๋„ match ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์—์„œ ํŒจํ„ด์ด ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ์ฐพ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. r = re.compile("ab.") r.match("kkkabc") # ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. r.search("kkkabc") <_sre.SRE_Match object; span=(3, 6), match='abc'> r.match("abckkk") <_sre.SRE_Match object; span=(0, 3), match='abc'> ์œ„ ๊ฒฝ์šฐ ์ •๊ทœ ํ‘œํ˜„์‹์ด ab. ์ด๊ธฐ ๋•Œ๋ฌธ์—, ab ๋‹ค์Œ์—๋Š” ์–ด๋–ค ํ•œ ๊ธ€์ž๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ํŒจํ„ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. search ๋ชจ๋“ˆ ํ•จ์ˆ˜์— kkkabc๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด ๋งค์น˜๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค๋ฉด abc๋ผ๋Š” ๋ฌธ์ž์—ด์—์„œ ๋งค์น˜๋˜์–ด Match object๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ match ๋ชจ๋“ˆ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ์•ž ๋ถ€๋ถ„์ด ab. ์™€ ๋งค์น˜๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ˜๋Œ€๋กœ abckkk๋กœ ๋งค์น˜๋ฅผ ์‹œ๋„ํ•ด ๋ณด๋ฉด, ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ํŒจํ„ด๊ณผ ๋งค์น˜๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •์ƒ์ ์œผ๋กœ Match object๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. (2) re.split() split() ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๋œ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด๋“ค์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ํ† ํฐํ™”์— ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด ๋ถ„๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋กœ์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•ด๋ด…์‹œ๋‹ค. # ๊ณต๋ฐฑ ๊ธฐ์ค€ ๋ถ„๋ฆฌ text = "์‚ฌ๊ณผ ๋”ธ๊ธฐ ์ˆ˜๋ฐ• ๋ฉœ๋ก  ๋ฐ”๋‚˜๋‚˜" re.split(" ", text) ['์‚ฌ๊ณผ', '๋”ธ๊ธฐ', '์ˆ˜๋ฐ•', '๋ฉœ๋ก ', '๋ฐ”๋‚˜๋‚˜'] ์ด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ค„๋ฐ”๊ฟˆ์ด๋‚˜ ๋‹ค๋ฅธ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ธฐ์ค€์œผ๋กœ ํ…์ŠคํŠธ๋ฅผ ๋ถ„๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. # ์ค„๋ฐ”๊ฟˆ ๊ธฐ์ค€ ๋ถ„๋ฆฌ text = """์‚ฌ๊ณผ ๋”ธ๊ธฐ ์ˆ˜๋ฐ• ๋ฉœ๋ก  ๋ฐ”๋‚˜๋‚˜""" re.split("\n", text) ['์‚ฌ๊ณผ', '๋”ธ๊ธฐ', '์ˆ˜๋ฐ•', '๋ฉœ๋ก ', '๋ฐ”๋‚˜๋‚˜'] # '+'๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌ text = "์‚ฌ๊ณผ+๋”ธ๊ธฐ+์ˆ˜๋ฐ•+๋ฉœ๋ก +๋ฐ”๋‚˜๋‚˜" re.split("\+", text) ['์‚ฌ๊ณผ', '๋”ธ๊ธฐ', '์ˆ˜๋ฐ•', '๋ฉœ๋ก ', '๋ฐ”๋‚˜๋‚˜'] (3) re.findall() findall() ํ•จ์ˆ˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ๋งค์น˜๋˜๋Š” ๋ฌธ์ž์—ด์ด ์—†๋‹ค๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ž„์˜์˜ ํ…์ŠคํŠธ์— ์ •๊ทœ ํ‘œํ˜„์‹์œผ๋กœ ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ทœ์น™์œผ๋กœ findall()์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ „์ฒด ํ…์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ์ˆซ์ž๋งŒ ์ฐพ์•„๋‚ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. text = """์ด๋ฆ„ : ๊น€์ฒ ์ˆ˜ ์ „ํ™”๋ฒˆํ˜ธ : 010 - 1234 - 1234 ๋‚˜์ด : 30 ์„ฑ๋ณ„ : ๋‚จ""" re.findall("\d+", text) ['010', '1234', '1234', '30'] ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ์ž…๋ ฅ ํ…์ŠคํŠธ์— ์ˆซ์ž๊ฐ€ ์—†๋‹ค๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. re.findall("\d+", "๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.") [] (4) re.sub() sub() ํ•จ์ˆ˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ์•„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ์ •์ œ ์ž‘์—…์— ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ, ์˜์–ด ๋ฌธ์žฅ์— ๊ฐ์ฃผ ๋“ฑ๊ณผ ๊ฐ™์€ ์ด์œ ๋กœ ํŠน์ˆ˜ ๋ฌธ์ž๊ฐ€ ์„ž์—ฌ์žˆ๋Š” ๊ฒฝ์šฐ์— ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•ŒํŒŒ๋ฒณ ์™ธ์˜ ๋ฌธ์ž๋Š” ๊ณต๋ฐฑ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋“ฑ์˜ ์šฉ๋„๋กœ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. text = "Regular expression : A regular expression, regex or regexp[1] (sometimes called a rational expression)[2][3] is, in theoretical computer science and formal language theory, a sequence of characters that define a search pattern." preprocessed_text = re.sub('[^a-zA-Z]', ' ', text) print(preprocessed_text) 'Regular expression A regular expression regex or regexp sometimes called a rational expression is in theoretical computer science and formal language theory a sequence of characters that define a search pattern ' 4. ์ •๊ทœ ํ‘œํ˜„์‹ ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ ์˜ˆ์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž„์˜์˜ ํ…์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ํ…Œ์ด๋ธ”<NAME>์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ…์ŠคํŠธ์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. text = """100 John PROF 101 James STUD 102 Mac STUD""" \s+๋Š” ๊ณต๋ฐฑ์„ ์ฐพ์•„๋‚ด๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์ž…๋‹ˆ๋‹ค. ๋’ค์— ๋ถ™๋Š” +๋Š” ์ตœ์†Œ 1๊ฐœ ์ด์ƒ์˜ ํŒจํ„ด์„ ์ฐพ์•„๋‚ธ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. s๋Š” ๊ณต๋ฐฑ์„ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์†Œ 1๊ฐœ ์ด์ƒ์˜ ๊ณต๋ฐฑ์ธ ํŒจํ„ด์„ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. split์€ ์ฃผ์–ด์ง„ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋ฏ€๋กœ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. re.split('\s+', text) ['100', 'John', 'PROF', '101', 'James', 'STUD', '102', 'Mac', 'STUD'] ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ’์ด ๊ตฌ๋ถ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ์ˆซ์ž๋งŒ์„ ๋ฝ‘์•„์˜จ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์—ฌ๊ธฐ์„œ \d๋Š” ์ˆซ์ž์— ํ•ด๋‹น๋˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์ž…๋‹ˆ๋‹ค. +๋ฅผ ๋ถ™์ด๋ฉด ์ตœ์†Œ 1๊ฐœ ์ด์ƒ์˜ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. findall()์€ ํ•ด๋‹น ํ‘œํ˜„์‹์— ์ผ์น˜ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. re.findall('\d+',text) ['100', '101', '102] ์ด๋ฒˆ์—๋Š” ํ…์ŠคํŠธ๋กœ๋ถ€ํ„ฐ ๋Œ€๋ฌธ์ž์ธ ํ–‰์˜ ๊ฐ’๋งŒ ๊ฐ€์ ธ์™€๋ด…์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ ์ •๊ทœ ํ‘œํ˜„์‹์— ๋Œ€๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋งค์น˜์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ •๊ทœ ํ‘œํ˜„์‹์— ๋Œ€๋ฌธ์ž๋ผ๋Š” ๊ธฐ์ค€๋งŒ์„ ๋„ฃ์„ ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์ž์—ด์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ชจ๋“  ๋Œ€๋ฌธ์ž ๊ฐ๊ฐ์„ ๊ฐ–๊ณ  ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. re.findall('[A-Z]',text) ['J', 'P', 'R', 'O', 'F', 'J', 'S', 'T', 'U', 'D', 'M', 'S', 'T', 'U', 'D'] ๋Œ€๋ฌธ์ž๊ฐ€ ์—ฐ์†์ ์œผ๋กœ ๋„ค ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋Š” ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. re.findall('[A-Z]{4}',text) ['PROF', 'STUD', 'STUD'] ๋Œ€๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด๋“ค์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ด๋ฆ„์˜ ๊ฒฝ์šฐ์—๋Š” ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ์„ž์—ฌ์žˆ๋Š” ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„์— ๋Œ€ํ•œ ํ–‰์˜ ๊ฐ’์„ ๊ฐ–๊ณ  ์˜ค๊ณ  ์‹ถ๋‹ค๋ฉด ์ฒ˜์Œ์— ๋Œ€๋ฌธ์ž๊ฐ€ ๋“ฑ์žฅํ•œ ํ›„์— ์†Œ๋ฌธ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ์— ๋งค์น˜ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. re.findall('[A-Z][a-z]+',text) ['John', 'James', 'Mac'] 5. ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ด์šฉํ•œ ํ† ํฐํ™” NLTK์—์„œ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด์„œ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” RegexpTokenizer๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. RegexpTokenizer()์—์„œ ๊ด„ํ˜ธ ์•ˆ์— ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ๊ทœ์ •ํ•˜๊ธฐ๋ฅผ ์›ํ•˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ๋„ฃ์–ด์„œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. tokenizer1์— ์‚ฌ์šฉํ•œ \w+๋Š” ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž๊ฐ€ 1๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. tokenizer2์—์„œ๋Š” ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ํ† ํฐํ™”ํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค. gaps=true๋Š” ํ•ด๋‹น ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ† ํฐ์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•œ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ gaps=True๋ผ๋Š” ๋ถ€๋ถ„์„ ๊ธฐ์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ํ† ํฐํ™”์˜ ๊ฒฐ๊ณผ๋Š” ๊ณต๋ฐฑ๋“ค๋งŒ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. tokenizer2์˜ ๊ฒฐ๊ณผ๋Š” ์œ„์˜ tokenizer1์˜ ๊ฒฐ๊ณผ์™€๋Š” ๋‹ฌ๋ฆฌ ์•„ํฌ์ŠคํŠธ๋กœํ”ผ๋‚˜ ์˜จ์ ์„ ์ œ์™ธํ•˜์ง€ ์•Š๊ณ  ํ† ํฐ ํ™”๊ฐ€ ์ˆ˜ํ–‰๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from nltk.tokenize import RegexpTokenizer text = "Don't be fooled by the dark sounding name, Mr. Jone's Orphanage is as cheery as cheery goes for a pastry shop" tokenizer1 = RegexpTokenizer("[\w]+") tokenizer2 = RegexpTokenizer("\s+", gaps=True) print(tokenizer1.tokenize(text)) print(tokenizer2.tokenize(text)) ['Don', 't', 'be', 'fooled', 'by', 'the', 'dark', 'sounding', 'name', 'Mr', 'Jone', 's', 'Orphanage', 'is', 'as', 'cheery', 'as', 'cheery', 'goes', 'for', 'a', 'pastry', 'shop'] ["Don't", 'be', 'fooled', 'by', 'the', 'dark', 'sounding', 'name,', 'Mr.', "Jone's", 'Orphanage', 'is', 'as', 'cheery', 'as', 'cheery', 'goes', 'for', 'a', 'pastry', 'shop'] 03-05 ๋”ฅ ๋Ÿฌ๋‹์„ ์œ„ํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ „์ฒ˜๋ฆฌ ์‹ค์Šต ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ† ํฐํ™”, ๋‹จ์–ด ์ง‘ํ•ฉ ์ƒ์„ฑ, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ, ํŒจ๋”ฉ, ๋ฒกํ„ฐํ™”์˜ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ์ „๋ฐ˜์ ์ธ ๊ณผ์ •์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 1. ํ† ํฐํ™”(Tokenization) ์ฃผ์–ด์ง„ ํ…์ŠคํŠธ๋ฅผ ๋‹จ์–ด ๋˜๋Š” ๋ฌธ์ž ๋‹จ์œ„๋กœ ์ž๋ฅด๋Š” ๊ฒƒ์„ ํ† ํฐํ™”๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ฃผ์–ด์ง„ ๋ฌธ์žฅ์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์˜์–ด์˜ ๊ฒฝ์šฐ ํ† ํฐํ™”๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋„๊ตฌ๋กœ์„œ ๋Œ€ํ‘œ์ ์œผ๋กœ spaCy์™€ NLTK๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก , ํŒŒ์ด์ฌ ๊ธฐ๋ณธ ํ•จ์ˆ˜์ธ split์œผ๋กœ ํ† ํฐํ™”๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์˜์–ด์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™” ์‹ค์Šต์„ ํ•ด๋ด…์‹œ๋‹ค. en_text = "A Dog Run back corner near spare bedrooms" 1. spaCy ์‚ฌ์šฉํ•˜๊ธฐ import spacy spacy_en = spacy.load('en') def tokenize(en_text): return [tok.text for tok in spacy_en.tokenizer(en_text)] print(tokenize(en_text)) ['A', 'Dog', 'Run', 'back', 'corner', 'near', 'spare', 'bedrooms'] 2. NLTK ์‚ฌ์šฉํ•˜๊ธฐ !pip install nltk import nltk nltk.download('punkt') from nltk.tokenize import word_tokenize print(word_tokenize(en_text)) ['A', 'Dog', 'Run', 'back', 'corner', 'near', 'spare', 'bedrooms'] 3. ๋„์–ด์“ฐ๊ธฐ๋กœ ํ† ํฐํ™” print(en_text.split()) ['A', 'Dog', 'Run', 'back', 'corner', 'near', 'spare', 'bedrooms'] ์‚ฌ์‹ค ์˜์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ํ† ํฐํ™”๋ฅผ ํ•ด๋„ ๋‹จ์–ด๋“ค ๊ฐ„ ๊ตฌ๋ถ„์ด ๊ฝค๋‚˜ ๋ช…ํ™•ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ† ํฐํ™” ์ž‘์—…์ด ์ˆ˜์›”ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•œ๊ตญ์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ํ† ํฐํ™” ์ž‘์—…์ด ํ›จ์”ฌ ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ํ•œ๊ตญ์–ด๋Š” ์กฐ์‚ฌ, ์ ‘์‚ฌ ๋“ฑ์œผ๋กœ ์ธํ•ด ๋‹จ์ˆœ ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋ฉด ๊ฐ™์€ ๋‹จ์–ด๊ฐ€ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ์ธ์‹๋˜์–ด์„œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์ปค์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ(vocabuary)์ด๋ž€ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ํ…์ŠคํŠธ์˜ ์ด ๋‹จ์–ด์˜ ์ง‘ํ•ฉ(set)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์–ด '์‚ฌ๊ณผ'๊ฐ€ ๋งŽ์ด ๋“ค์–ด๊ฐ„ ์–ด๋–ค ๋ฌธ์žฅ์— ๋„์–ด์“ฐ๊ธฐ ํ† ํฐํ™”๋ฅผ ํ•œ๋‹ค๋ฉด '์‚ฌ๊ณผ๊ฐ€', '์‚ฌ๊ณผ๋ฅผ', '์‚ฌ๊ณผ์˜', '์‚ฌ๊ณผ์™€', '์‚ฌ๊ณผ๋Š”'๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ ๊ฐ™์€ ๋‹จ์–ด์ž„์—๋„ ์กฐ์‚ฌ๊ฐ€ ๋ถ™์–ด์„œ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ์ธ์‹๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ํ†ตํ•ด ๊ตฌ์ฒด์ ์œผ๋กœ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 4. ํ•œ๊ตญ์–ด ๋„์–ด์“ฐ๊ธฐ ํ† ํฐํ™” kor_text = "์‚ฌ๊ณผ์˜ ๋†€๋ผ์šด ํšจ๋Šฅ์ด๋ผ๋Š” ๊ธ€์„ ๋ดค์–ด. ๊ทธ๋ž˜์„œ ์˜ค๋Š˜ ์‚ฌ๊ณผ๋ฅผ ๋จน์œผ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์‚ฌ๊ณผ๊ฐ€ ์ฉ์–ด์„œ ์Šˆํผ์— ๊ฐ€์„œ ์‚ฌ๊ณผ๋ž‘ ์˜ค๋ Œ์ง€ ์‚ฌ ์™”์–ด" print(kor_text.split()) ['์‚ฌ๊ณผ์˜', '๋†€๋ผ์šด', 'ํšจ๋Šฅ์ด๋ผ๋Š”', '๊ธ€์„', '๋ดค์–ด.', '๊ทธ๋ž˜์„œ', '์˜ค๋Š˜', '์‚ฌ๊ณผ๋ฅผ', '๋จน์œผ๋ ค๊ณ ', 'ํ–ˆ๋Š”๋ฐ', '์‚ฌ๊ณผ๊ฐ€', '์ฉ์–ด์„œ', '์Šˆํผ์—', '๊ฐ€์„œ', '์‚ฌ๊ณผ๋ž‘', '์˜ค๋ Œ์ง€', '์‚ฌ ์™”์–ด'] ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” '์‚ฌ๊ณผ'๋ž€ ๋‹จ์–ด๊ฐ€ ์ด 4๋ฒˆ ๋“ฑ์žฅํ–ˆ๋Š”๋ฐ ๋ชจ๋‘ '์˜', '๋ฅผ', '๊ฐ€', '๋ž‘' ๋“ฑ์ด ๋ถ™์–ด์žˆ์–ด ์ด๋ฅผ ์ œ๊ฑฐํ•ด ์ฃผ์ง€ ์•Š์œผ๋ฉด ๊ธฐ๊ณ„๋Š” ์ „๋ถ€ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ์ธ์‹ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 5. ํ˜•ํƒœ์†Œ ํ† ํฐํ™” ์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•œ๊ตญ์–ด๋Š” ๋ณดํŽธ์ ์œผ๋กœ 'ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ'๋กœ ํ† ํฐํ™”๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ ์ค‘์—์„œ mecab์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ปค๋งจ๋“œ๋กœ colab์—์„œ mecab์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. !pip install konlpy !pip install mecab-python !bash <(curl -s https://raw.githubusercontent.com/konlpy/konlpy/master/scripts/mecab.sh) from konlpy.tag import Mecab tokenizer = Mecab() print(tokenizer.morphs(kor_text)) ['์‚ฌ๊ณผ', '์˜', '๋†€๋ผ์šด', 'ํšจ๋Šฅ', '์ด', '๋ผ๋Š”', '๊ธ€', '์„', '๋ดค', '์–ด', '.', '๊ทธ๋ž˜์„œ', '์˜ค๋Š˜', '์‚ฌ๊ณผ', '๋ฅผ', '๋จน', '์œผ๋ ค๊ณ ', 'ํ–ˆ', '๋Š”๋ฐ', '์‚ฌ๊ณผ', '๊ฐ€', '์ฉ', '์–ด์„œ', '์Šˆํผ', '์—', '๊ฐ€', '์„œ', '์‚ฌ๊ณผ', '๋ž‘', '์˜ค๋ Œ์ง€', '์‚ฌ', '์™”', '์–ด'] ์•ž์„  ์˜ˆ์™€ ๋‹ค๋ฅด๊ฒŒ '์˜', '๋ฅผ', '๊ฐ€', '๋ž‘' ๋“ฑ์ด ์ „๋ถ€ ๋ถ„๋ฆฌ๋˜์–ด ๊ธฐ๊ณ„๋Š” '์‚ฌ๊ณผ'๋ผ๋Š” ๋‹จ์–ด๋ฅผ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” ๋‹จ์–ด ๋˜๋Š” ํ˜•ํƒœ์†Œ ๋‹จ์œ„๋กœ ํ† ํฐํ™”๋ฅผ ํ–ˆ์ง€๋งŒ ์ด๋ณด๋‹ค๋„ ๋” ์ž‘์€ ๋‹จ์œ„์ธ ๋ฌธ์ž ๋‹จ์œ„๋กœ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 6. ๋ฌธ์ž ํ† ํฐํ™” print(list(en_text)) ['A', ' ', 'D', 'o', 'g', ' ', 'R', 'u', 'n', ' ', 'b', 'a', 'c', 'k', ' ', 'c', 'o', 'r', 'n', 'e', 'r', ' ', 'n', 'e', 'a', 'r', ' ', 's', 'p', 'a', 'r', 'e', ' ', 'b', 'e', 'd', 'r', 'o', 'o', 'm', 's'] ๊ฐ„๋‹จํžˆ ํ† ํฐํ™”์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ๋Š” ์ข€ ๋” ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary) ์ƒ์„ฑ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabuary)์ด๋ž€ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ํ…์ŠคํŠธ์˜ ์ด ๋‹จ์–ด์˜ ์ง‘ํ•ฉ(set)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„ , ์‹ค์Šต์„ ์œ„ํ•ด์„œ ๊นƒํ—ˆ๋ธŒ์—์„œ '๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ' ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋Š” ์ด 20๋งŒ ๊ฐœ์˜ ์˜ํ™” ๋ฆฌ๋ทฐ๋ฅผ ๊ธ์ • 1, ๋ถ€์ • 0์œผ๋กœ ๋ ˆ์ด๋ธ”๋งํ•œ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. import urllib.request import pandas as pd from konlpy.tag import Mecab from nltk import FreqDist import numpy as np import matplotlib.pyplot as plt urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings.txt", filename="ratings.txt") data = pd.read_table('ratings.txt') # ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅ data[:10] print('์ „์ฒด ์ƒ˜ํ”Œ์˜ ์ˆ˜ : {}'.format(len(data))) ์ „์ฒด ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 200000 sample_data = data[:100] # ์ž„์˜๋กœ 100๊ฐœ๋งŒ ์ €์žฅ ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์ œํ•ฉ๋‹ˆ๋‹ค. sample_data['document'] = sample_data['document'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") # ํ•œ๊ธ€๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐ sample_data[:10] ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ† ํฐํ™” ๊ณผ์ •์—์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ๋ถˆ์šฉ์–ด๋ฅผ ์šฐ์„  ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. # ๋ถˆ์šฉ์–ด ์ •์˜ stopwords=['์˜','๊ฐ€','์ด','์€','๋“ค','๋Š”','์ข€','์ž˜','๊ทธ๋ƒฅ','๊ณผ','๋„','๋ฅผ','์œผ๋กœ','์ž','์—','์™€','ํ•œ','ํ•˜๋‹ค'] ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋Š” mecab์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. tokenizer = Mecab() tokenized=[] for sentence in sample_data['document']: temp = tokenizer.morphs(sentence) # ํ† ํฐํ™” temp = [word for word in temp if not word in stopwords] # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ tokenized.append(temp) print(tokenized[:10]) [['์–ด๋ฆด', '๋•Œ', '๋ณด', '๊ณ ', '์ง€๊ธˆ', '๋‹ค์‹œ', '๋ด๋„', '์žฌ๋ฐŒ', '์–ด์š”', 'ใ…‹ใ…‹'], ['๋””์ž์ธ', '์„', '๋ฐฐ์šฐ', 'ํ•™์ƒ', '์™ธ๊ตญ', '๋””์ž์ด๋„ˆ', '๊ทธ', '์ผ๊ตฐ', '์ „ํ†ต', '์„', 'ํ†ตํ•ด', '๋ฐœ์ „', 'ํ•ด', '๋ฌธํ™”', '์‚ฐ์—…', '๋ถ€๋Ÿฌ์› ', '๋Š”๋ฐ', '์‚ฌ์‹ค', '์šฐ๋ฆฌ', '๋‚˜๋ผ', '์—์„œ', '๊ทธ', '์–ด๋ ค์šด', '์‹œ์ ˆ', '๋', '๊นŒ์ง€', '์—ด์ •', '์„', '์ง€ํ‚จ', '๋…ธ๋ผ๋…ธ', '๊ฐ™', '์ „ํ†ต', '์žˆ', '์–ด', '์ €', '๊ฐ™', '์‚ฌ๋žŒ', '๊ฟˆ', '์„', '๊พธ', '๊ณ ', '์ด๋ค„๋‚˜๊ฐˆ', '์ˆ˜', '์žˆ', '๋‹ค๋Š”', '๊ฒƒ', '๊ฐ์‚ฌ', 'ํ•ฉ๋‹ˆ๋‹ค'], ['ํด๋ฆฌ์Šค', '์Šคํ† ๋ฆฌ', '์‹œ๋ฆฌ์ฆˆ', '๋ถ€ํ„ฐ', '๋‰ด', '๊นŒ์ง€', '๋ฒ„๋ฆด', '๊ป˜', 'ํ•˜๋‚˜', '์—†', '์Œ', '์ตœ๊ณ '], ['์—ฐ๊ธฐ', '์ง„์งœ', '๊ฐœ', '์ฉ”', '๊ตฌ๋‚˜', '์ง€๋ฃจ', 'ํ•  ๊ฑฐ', '๋ผ๊ณ ', '์ƒ๊ฐ', 'ํ–ˆ', '๋Š”๋ฐ', '๋ชฐ์ž…', 'ํ•ด์„œ', '๋ดค', '๋‹ค', '๊ทธ๋ž˜', '์ด๋Ÿฐ', '๊ฒŒ', '์ง„์งœ', '์˜ํ™”', '์ง€'], ['์•ˆ๊ฐœ', '์ž์šฑ', '๋ฐคํ•˜๋Š˜', '๋– ', '์žˆ', '์ดˆ์Šน๋‹ฌ', '๊ฐ™', '์˜ํ™”'], ['์‚ฌ๋ž‘', '์„', 'ํ•ด', '๋ณธ', '์‚ฌ๋žŒ', '๋ผ๋ฉด', '์ฒ˜์Œ', '๋ถ€ํ„ฐ', '๋', '๊นŒ์ง€', '์›ƒ', '์„', '์ˆ˜', '์žˆ', '์˜ํ™”'], ['์™„์ „', '๊ฐ๋™', '์ž…๋‹ˆ๋‹ค', '๋‹ค์‹œ', '๋ด๋„', '๊ฐ๋™'], ['๊ฐœ', '์ „์Ÿ', '๋‚˜์˜ค', '๋‚˜์š”', '๋‚˜์˜ค', '๋ฉด', '๋น ', '๋กœ', '๋ณด', '๊ณ ', '์‹ถ', '์Œ'], ['๊ตฟ'], ['๋ฐ”๋ณด', '์•„๋‹ˆ', '๋ผ', '๋ณ‘', '์‰ฐ', '์ธ', '๋“ฏ']] ์ด์ œ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. NLTK์—์„œ๋Š” ๋นˆ๋„์ˆ˜ ๊ณ„์‚ฐ ๋„๊ตฌ์ธ FreqDist()๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. vocab = FreqDist(np.hstack(tokenized)) print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(len(vocab))) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 663 ๋‹จ์–ด๋ฅผ ํ‚ค(key)๋กœ, ๋‹จ์–ด์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ’(value)์œผ๋กœ ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. vocab์— ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋นˆ๋„์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. vocab['์žฌ๋ฐŒ'] 10 '์žฌ๋ฐŒ'์ด๋ž€ ๋‹จ์–ด๊ฐ€ ์ด 10๋ฒˆ ๋“ฑ์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. most_common()๋Š” ์ƒ์œ„ ๋นˆ๋„์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ฃผ์–ด์ง„ ์ˆ˜์˜ ๋‹จ์–ด๋งŒ์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ๋‹จ์–ด๋“ค์„ ์›ํ•˜๋Š” ๊ฐœ์ˆ˜๋งŒํผ๋งŒ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 500๊ฐœ์˜ ๋‹จ์–ด๋งŒ ๋‹จ์–ด ์ง‘ํ•ฉ์œผ๋กœ ์ €์žฅํ•ด ๋ด…์‹œ๋‹ค. vocab_size = 500 # ์ƒ์œ„ vocab_size ๊ฐœ์˜ ๋‹จ์–ด๋งŒ ๋ณด์กด vocab = vocab.most_common(vocab_size) print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {}'.format(len(vocab))) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 500 ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ 500์œผ๋กœ ์ค„์–ด๋“  ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ๋ถ€์—ฌ enumerate()๋Š” ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ์ž๋ฃŒํ˜•(list, set, tuple, dictionary, string)์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์ธ๋ฑ์Šค๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ํ•จ๊ป˜ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค 0๊ณผ 1์€ ๋‹ค๋ฅธ ์šฉ๋„๋กœ ๋‚จ๊ฒจ๋‘๊ณ  ๋‚˜๋จธ์ง€ ๋‹จ์–ด๋“ค์€ 2๋ถ€ํ„ฐ 501๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•ด ๋ด…์‹œ๋‹ค. word_to_index = {word[0] : index + 2 for index, word in enumerate(vocab)} word_to_index['pad'] = 1 word_to_index['unk'] = 0 ์ด์ œ ๊ธฐ์กด์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ ๋‹จ์–ด๋ฅผ ๊ณ ์œ ํ•œ ์ •์ˆ˜๋กœ ๋ถ€์—ฌํ•˜๋Š” ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. encoded = [] for line in tokenized: #์ž…๋ ฅ ๋ฐ์ดํ„ฐ์—์„œ 1์ค„์”ฉ ๋ฌธ์žฅ์„ ์ฝ์Œ temp = [] for w in line: #๊ฐ ์ค„์—์„œ 1๊ฐœ์”ฉ ๊ธ€์ž๋ฅผ ์ฝ์Œ try: temp.append(word_to_index[w]) # ๊ธ€์ž๋ฅผ ํ•ด๋‹น๋˜๋Š” ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ except KeyError: # ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด์ผ ๊ฒฝ์šฐ unk๋กœ ๋Œ€์ฒด๋œ๋‹ค. temp.append(word_to_index['unk']) # unk์˜ ์ธ๋ฑ์Šค๋กœ ๋ณ€ํ™˜ encoded.append(temp) print(encoded[:10]) [[78, 27, 9, 4, 50, 41, 79, 16, 28, 29], [188, 5, 80, 189, 190, 191, 42, 192, 114, 5, 193, 194, 21, 115, 195, 196, 13, 51, 81, 116, 30, 42, 197, 117, 118, 31, 198, 5, 199, 200, 17, 114, 7, 82, 52, 17, 43, 201, 5, 202, 4, 203, 14, 7, 83, 32, 204, 84], [205, 119, 206, 53, 207, 31, 208, 209, 54, 10, 25, 11], [44, 33, 120, 210, 211, 212, 213, 68, 45, 34, 13, 214, 121, 15, 2, 215, 69, 8, 33, 3, 35], [216, 217, 218, 219, 7, 220, 17, 3], [122, 5, 21, 36, 43, 123, 124, 53, 118, 31, 85, 5, 14, 7, 3], [125, 37, 221, 41, 79, 37], [120, 222, 55, 223, 55, 86, 224, 46, 9, 4, 47, 25], [56], [225, 87, 88, 226, 227, 57, 89]] 4. ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๋ฌธ์žฅ๋“ค์„ ๋ชจ๋‘ ๋™์ผํ•œ ๊ธธ์ด๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ํŒจ๋”ฉ(padding) ์ด์ œ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๋ฆฌ๋ทฐ๋“ค์„ ๋ชจ๋‘ ๋™์ผํ•œ ๊ธธ์ด๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ํŒจ๋”ฉ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋‹จ์–ด ์ง‘ํ•ฉ์— ํŒจ๋”ฉ์„ ์œ„ํ•œ ํ† ํฐ์ธ 'pad'๋ฅผ ์ถ”๊ฐ€ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํŒจ๋”ฉ ์ž‘์—…์€ ์ •ํ•ด์ค€ ๊ธธ์ด๋กœ ๋ชจ๋“  ์ƒ˜ํ”Œ๋“ค์˜ ๊ธธ์ด๋ฅผ ๋งž์ถฐ์ฃผ๋˜, ๊ธธ์ด๊ฐ€ ์ •ํ•ด์ค€ ๊ธธ์ด๋ณด๋‹ค ์งง์€ ์ƒ˜ํ”Œ๋“ค์—๋Š” 'pad' ํ† ํฐ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ธธ์ด๋ฅผ ๋งž์ถฐ์ฃผ๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. max_len = max(len(l) for l in encoded) print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : %d' % max_len) print('๋ฆฌ๋ทฐ์˜ ์ตœ์†Œ ๊ธธ์ด : %d' % min(len(l) for l in encoded)) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : %f' % (sum(map(len, encoded))/len(encoded))) plt.hist([len(s) for s in encoded], bins=50) plt.xlabel('length of sample') plt.ylabel('number of sample') plt.show() ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋ฆฌ๋ทฐ์˜ ๊ธธ์ด๋Š” 63์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฆฌ๋ทฐ์˜ ๊ธธ์ด๋ฅผ 63์œผ๋กœ ํ†ต์ผ์‹œ์ผœ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. for line in encoded: if len(line) < max_len: # ํ˜„์žฌ ์ƒ˜ํ”Œ์ด ์ •ํ•ด์ค€ ๊ธธ์ด๋ณด๋‹ค ์งง์œผ๋ฉด line += [word_to_index['pad']] * (max_len - len(line)) # ๋‚˜๋จธ์ง€๋Š” ์ „๋ถ€ 'pad' ํ† ํฐ์œผ๋กœ ์ฑ„์šด๋‹ค. print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : %d' % max(len(l) for l in encoded)) print('๋ฆฌ๋ทฐ์˜ ์ตœ์†Œ ๊ธธ์ด : %d' % min(len(l) for l in encoded)) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : %f' % (sum(map(len, encoded))/len(encoded))) ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 63 ๋ฆฌ๋ทฐ์˜ ์ตœ์†Œ ๊ธธ์ด : 63 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 63.000000 ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ์ธํ•ด ์ƒ์œ„ 3๊ฐœ์˜ ์ƒ˜ํ”Œ๋“ค๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(encoded[:3]) [[78, 27, 9, 4, 50, 41, 79, 16, 28, 29, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [188, 5, 80, 189, 190, 191, 42, 192, 114, 5, 193, 194, 21, 115, 195, 196, 13, 51, 81, 116, 30, 42, 197, 117, 118, 31, 198, 5, 199, 200, 17, 114, 7, 82, 52, 17, 43, 201, 5, 202, 4, 203, 14, 7, 83, 32, 204, 84, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [205, 119, 206, 53, 207, 31, 208, 209, 54, 10, 25, 11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ์ด์ œ ๋‹จ์–ด๋“ค์„ ๊ณ ์œ ํ•œ ์ •์ˆ˜๋กœ ๋งคํ•‘ํ•˜์˜€์œผ๋‹ˆ, ๊ฐ ์ •์ˆ˜๋ฅผ ๊ณ ์œ ํ•œ ๋‹จ์–ด ๋ฒกํ„ฐ๋กœ ๋ฐ”๊พธ๋Š” ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ๊ณผ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด ์žˆ๋Š”๋ฐ, ์ฃผ๋กœ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ๊ณผ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•ด์„œ๋Š” 9์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 03-07 ํ•œ๊ตญ์–ด ์ „์ฒ˜๋ฆฌ ํŒจํ‚ค์ง€(Text Preprocessing Tools for Korean Text) ์œ ์šฉํ•œ ํ•œ๊ตญ์–ด ์ „์ฒ˜๋ฆฌ ํŒจํ‚ค์ง€๋ฅผ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. ์•ž์„œ ์†Œ๊ฐœํ•œ ํ˜•ํƒœ์†Œ์™€ ๋ฌธ์žฅ ํ† ํฌ๋‚˜์ด์ง• ๋„๊ตฌ๋“ค์ธ KoNLPy์™€ KSS(Korean Sentence Splitter)์™€ ํ•จ๊ป˜ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€๋“ค์ž…๋‹ˆ๋‹ค. 1. PyKoSpacing pip install git+https://github.com/haven-jeon/PyKoSpacing.git ์ „ํฌ ์›๋‹˜์ด ๊ฐœ๋ฐœํ•œ PyKoSpacing์€ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜์–ด์žˆ์ง€ ์•Š์€ ๋ฌธ์žฅ์„ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. PyKoSpacing์€ ๋Œ€์šฉ๋Ÿ‰ ์ฝ”ํผ์Šค๋ฅผ ํ•™์Šตํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง„ ๋„์–ด์“ฐ๊ธฐ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ๋กœ ์ค€์ˆ˜ํ•œ ์„ฑ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. sent = '๊น€์ฒ ์ˆ˜๋Š” ๊ทน์ค‘ ๋‘ ์ธ๊ฒฉ์˜ ์‚ฌ๋‚˜์ด ์ด๊ด‘์ˆ˜ ์—ญ์„ ๋งก์•˜๋‹ค. ์ฒ ์ˆ˜๋Š” ํ•œ๊ตญ ์œ ์ผ์˜ ํƒœ๊ถŒ๋„ ์ „์Šน์ž๋ฅผ ๊ฐ€๋ฆฌ๋Š” ๊ฒฐ์ „์˜ ๋‚ ์„ ์•ž๋‘๊ณ  10๋…„๊ฐ„ ํ•จ๊ป˜ ํ›ˆ๋ จํ•œ ์‚ฌํ˜•์ธ ์œ ์—ฐ์žฌ(๊น€๊ด‘์ˆ˜ ๋ถ„)๋ฅผ ์ฐพ์œผ๋Ÿฌ ์†์„ธ๋กœ ๋‚ด๋ ค์˜จ ์ธ๋ฌผ์ด๋‹ค.' ์ž„์˜์˜ ๋ฌธ์žฅ์„ ์ž„์˜๋กœ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์—†๋Š” ๋ฌธ์žฅ์œผ๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. new_sent = sent.replace(" ", '') # ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์—†๋Š” ๋ฌธ์žฅ ์ž„์˜๋กœ ๋งŒ๋“ค๊ธฐ print(new_sent) ๊น€์ฒ ์ˆ˜๋Š”๊ทน์ค‘๋‘์ธ๊ฒฉ์˜์‚ฌ๋‚˜์ด์ด๊ด‘์ˆ˜์—ญ์„๋งก์•˜๋‹ค.์ฒ ์ˆ˜๋Š”ํ•œ๊ตญ์œ ์ผ์˜ํƒœ๊ถŒ๋„์ „์Šน์ž๋ฅผ๊ฐ€๋ฆฌ๋Š”๊ฒฐ์ „์˜๋‚ ์„์•ž๋‘๊ณ 10๋…„๊ฐ„ํ•จ๊ป˜ํ›ˆ๋ จํ•œ์‚ฌํ˜•์ธ์œ ์—ฐ์žฌ(๊น€๊ด‘์ˆ˜๋ถ„)๋ฅผ์ฐพ์œผ๋Ÿฌ์†์„ธ๋กœ๋‚ด๋ ค์˜จ์ธ๋ฌผ์ด๋‹ค. ์ด๋ฅผ PyKoSpacing์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์› ๋ฌธ์žฅ๊ณผ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. from pykospacing import Spacing spacing = Spacing() kospacing_sent = spacing(new_sent) print(sent) print(kospacing_sent) ๊น€์ฒ ์ˆ˜๋Š” ๊ทน์ค‘ ๋‘ ์ธ๊ฒฉ์˜ ์‚ฌ๋‚˜์ด ์ด๊ด‘์ˆ˜ ์—ญ์„ ๋งก์•˜๋‹ค. ์ฒ ์ˆ˜๋Š” ํ•œ๊ตญ ์œ ์ผ์˜ ํƒœ๊ถŒ๋„ ์ „์Šน์ž๋ฅผ ๊ฐ€๋ฆฌ๋Š” ๊ฒฐ์ „์˜ ๋‚ ์„ ์•ž๋‘๊ณ  10๋…„๊ฐ„ ํ•จ๊ป˜ ํ›ˆ๋ จํ•œ ์‚ฌํ˜•์ธ ์œ ์—ฐ์žฌ(๊น€๊ด‘์ˆ˜ ๋ถ„)๋ฅผ ์ฐพ์œผ๋Ÿฌ ์†์„ธ๋กœ ๋‚ด๋ ค์˜จ ์ธ๋ฌผ์ด๋‹ค. ๊น€์ฒ ์ˆ˜๋Š” ๊ทน์ค‘ ๋‘ ์ธ๊ฒฉ์˜ ์‚ฌ๋‚˜์ด ์ด๊ด‘์ˆ˜ ์—ญ์„ ๋งก์•˜๋‹ค. ์ฒ ์ˆ˜๋Š” ํ•œ๊ตญ ์œ ์ผ์˜ ํƒœ๊ถŒ๋„ ์ „์Šน์ž๋ฅผ ๊ฐ€๋ฆฌ๋Š” ๊ฒฐ์ „์˜ ๋‚ ์„ ์•ž๋‘๊ณ  10๋…„๊ฐ„ ํ•จ๊ป˜ ํ›ˆ๋ จํ•œ ์‚ฌํ˜•์ธ ์œ ์—ฐ์žฌ(๊น€๊ด‘์ˆ˜ ๋ถ„)๋ฅผ ์ฐพ์œผ๋Ÿฌ ์†์„ธ๋กœ ๋‚ด๋ ค์˜จ ์ธ๋ฌผ์ด๋‹ค. ์ •ํ™•ํ•˜๊ฒŒ ๊ฒฐ๊ณผ๊ฐ€ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. 2. Py-Hanspell pip install git+https://github.com/ssut/py-hanspell.git Py-Hanspell์€ ๋„ค์ด๋ฒ„ ํ•œ๊ธ€ ๋งž์ถค๋ฒ• ๊ฒ€์‚ฌ๊ธฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. from hanspell import spell_checker sent = "๋งž์ถค๋ฒ• ํ‹€๋ฆฌ๋ฉด ์™œ ์•ˆ๋ผ? ์“ฐ๊ณ  ์‹ถ์€ ๋Œ€๋กœ ์“ฐ๋ฉด ๋˜์ง€ " spelled_sent = spell_checker.check(sent) hanspell_sent = spelled_sent.checked print(hanspell_sent) ๋งž์ถค๋ฒ• ํ‹€๋ฆฌ๋ฉด ์™œ ์•ˆ๋ผ? ์“ฐ๊ณ  ์‹ถ์€ ๋Œ€๋กœ ์“ฐ๋ฉด ๋˜์ง€ ์ด ํŒจํ‚ค์ง€๋Š” ๋„์–ด์“ฐ๊ธฐ ๋˜ํ•œ ๋ณด์ •ํ•ฉ๋‹ˆ๋‹ค. PyKoSpacing์— ์‚ฌ์šฉํ•œ ์˜ˆ์ œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค. spelled_sent = spell_checker.check(new_sent) hanspell_sent = spelled_sent.checked print(hanspell_sent) print(kospacing_sent) # ์•ž์„œ ์‚ฌ์šฉํ•œ kospacing ํŒจํ‚ค์ง€์—์„œ ์–ป์€ ๊ฒฐ๊ณผ ๊น€์ฒ ์ˆ˜๋Š” ๊ทน ์ค‘ ๋‘ ์ธ๊ฒฉ์˜ ์‚ฌ๋‚˜์ด ์ด๊ด‘์ˆ˜ ์—ญ์„ ๋งก์•˜๋‹ค. ์ฒ ์ˆ˜๋Š” ํ•œ๊ตญ ์œ ์ผ์˜ ํƒœ๊ถŒ๋„ ์ „์Šน์ž๋ฅผ ๊ฐ€๋ฆฌ๋Š” ๊ฒฐ์ „์˜ ๋‚ ์„ ์•ž๋‘๊ณ  10๋…„๊ฐ„ ํ•จ๊ป˜ ํ›ˆ๋ จํ•œ ์‚ฌํ˜•์ธ ์œ ์—ฐ์ œ(๊น€๊ด‘์ˆ˜ ๋ถ„)๋ฅผ ์ฐพ์œผ๋Ÿฌ ์†์„ธ๋กœ ๋‚ด๋ ค์˜จ ์ธ๋ฌผ์ด๋‹ค. ๊น€์ฒ ์ˆ˜๋Š” ๊ทน์ค‘ ๋‘ ์ธ๊ฒฉ์˜ ์‚ฌ๋‚˜์ด ์ด๊ด‘์ˆ˜ ์—ญ์„ ๋งก์•˜๋‹ค. ์ฒ ์ˆ˜๋Š” ํ•œ๊ตญ ์œ ์ผ์˜ ํƒœ๊ถŒ๋„ ์ „์Šน์ž๋ฅผ ๊ฐ€๋ฆฌ๋Š” ๊ฒฐ์ „์˜ ๋‚ ์„ ์•ž๋‘๊ณ  10๋…„๊ฐ„ ํ•จ๊ป˜ ํ›ˆ๋ จํ•œ ์‚ฌํ˜•์ธ ์œ ์—ฐ์žฌ(๊น€๊ด‘์ˆ˜ ๋ถ„)๋ฅผ ์ฐพ์œผ๋Ÿฌ ์†์„ธ๋กœ ๋‚ด๋ ค์˜จ ์ธ๋ฌผ์ด๋‹ค. PyKoSpacing๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๊ฑฐ์˜ ๋น„์Šทํ•˜์ง€๋งŒ ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. 3. SOYNLP๋ฅผ ์ด์šฉํ•œ ๋‹จ์–ด ํ† ํฐํ™” soynlp๋Š” ํ’ˆ์‚ฌ ํƒœ๊น…, ๋‹จ์–ด ํ† ํฐํ™” ๋“ฑ์„ ์ง€์›ํ•˜๋Š” ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €์ž…๋‹ˆ๋‹ค. ๋น„์ง€๋„ ํ•™์Šต์œผ๋กœ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ํ•œ๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ–๊ณ  ์žˆ์œผ๋ฉฐ, ๋ฐ์ดํ„ฐ์— ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋“ค์„ ๋‹จ์–ด๋กœ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค. soynlp ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋‹จ์–ด ์ ์ˆ˜ ํ‘œ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ ์ˆ˜๋Š” ์‘์ง‘ ํ™•๋ฅ (cohesion probability)๊ณผ ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ(branching entropy)๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. pip install soynlp 1. ์‹ ์กฐ์–ด ๋ฌธ์ œ soynlp๋ฅผ ์†Œ๊ฐœํ•˜๊ธฐ ์ „์— ๊ธฐ์กด์˜ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๊ฐ€ ๊ฐ€์ง„ ๋ฌธ์ œ๋Š” ๋ฌด์—‡์ด์—ˆ๋Š”์ง€, SOYNLP๊ฐ€ ์–ด๋–ค ์ ์—์„œ ์œ ์šฉํ•œ์ง€ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. ๊ธฐ์กด์˜ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋Š” ์‹ ์กฐ์–ด๋‚˜ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์— ๋“ฑ๋ก๋˜์ง€ ์•Š์€ ๋‹จ์–ด ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ œ๋Œ€๋กœ ๊ตฌ๋ถ„ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. from konlpy.tag import Okt tokenizer = Okt() print(tokenizer.morphs('์—์ด๋น„์‹์Šค<NAME> 1์›” ์ตœ์• ๋Œ ๊ธฐ๋ถ€ ์š”์ •')) ['์—์ด', '๋น„์‹์Šค', '์ด๋Œ€', 'ํœ˜', '1์›”', '์ตœ์• ', '๋Œ', '๊ธฐ๋ถ€', '์š”์ •'] ์—์ด๋น„ ์‹์Šค๋Š” ์•„์ด๋Œ์˜ ์ด๋ฆ„์ด๊ณ ,<NAME>๋Š” ์—์ด๋น„ ์‹์Šค์˜ ๋ฉค๋ฒ„์ด๋ฉฐ, ์ตœ์• ๋Œ์€ ์ตœ๊ณ ๋กœ ์• ์ • ํ•˜๋Š” ์บ๋ฆญํ„ฐ๋ผ๋Š” ๋œป์ด์ง€๋งŒ ์œ„์˜ ํ˜•ํƒœ์†Œ ๋ถ„์„ ๊ฒฐ๊ณผ์—์„œ๋Š” ์ „๋ถ€ ๋ถ„๋ฆฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ ํŠน์ • ๋ฌธ์ž ์‹œํ€€์Šค๊ฐ€ ํ•จ๊ป˜ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋นˆ๋„๊ฐ€ ๋†’๊ณ , ์•ž๋’ค๋กœ ์กฐ์‚ฌ ๋˜๋Š” ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด์„œ ํ•ด๋‹น ๋ฌธ์ž ์‹œํ€€์Šค๋ฅผ ํ˜•ํƒœ์†Œ๋ผ๊ณ  ํŒ๋‹จํ•˜๋Š” ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €๋ผ๋ฉด ์–ด๋–จ๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด ์—์ด๋น„ ์‹์Šค๋ผ๋Š” ๋ฌธ์ž์—ด์ด ์ž์ฃผ ์—ฐ๊ฒฐ๋˜์–ด ๋“ฑ์žฅํ•œ๋‹ค๋ฉด ํ•œ ๋‹จ์–ด๋ผ๊ณ  ํŒ๋‹จํ•˜๊ณ , ๋˜ํ•œ ์—์ด๋น„ ์‹์Šค๋ผ๋Š” ๋‹จ์–ด ์•ž, ๋’ค์— '์ตœ๊ณ ', '๊ฐ€์ˆ˜', '์‹ค๋ ฅ'๊ณผ ๊ฐ™์€ ๋…๋ฆฝ๋œ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์ด ๊ณ„์†ํ•ด์„œ ๋“ฑ์žฅํ•œ๋‹ค๋ฉด ์—์ด๋น„ ์‹์Šค๋ฅผ ํ•œ ๋‹จ์–ด๋กœ ํŒŒ์•…ํ•˜๋Š” ์‹์ด์ง€์š”. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ์•„์ด๋””์–ด๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ soynlp์ž…๋‹ˆ๋‹ค. 2. ํ•™์Šตํ•˜๊ธฐ soynlp๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•™์Šต์— ๊ธฐ๋ฐ˜ํ•œ ํ† ํฌ ๋‚˜์ด์ €์ด๋ฏ€๋กœ ํ•™์Šต์— ํ•„์š”ํ•œ ํ•œ๊ตญ์–ด ๋ฌธ์„œ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. import urllib.request from soynlp import DoublespaceLineCorpus from soynlp.word import WordExtractor urllib.request.urlretrieve("https://raw.githubusercontent.com/lovit/soynlp/master/tutorials/2016-10-20.txt", filename="2016-10-20.txt") ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์ˆ˜์˜ ๋ฌธ์„œ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์ˆ˜์˜ ๋ฌธ์„œ๋กœ ๋ถ„๋ฆฌ corpus = DoublespaceLineCorpus("2016-10-20.txt") len(corpus) 30091 ์ด 3๋งŒ 91๊ฐœ์˜ ๋ฌธ์„œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 3๊ฐœ์˜ ๋ฌธ์„œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ์ค‘๋žตํ•˜์˜€์Šต๋‹ˆ๋‹ค. i = 0 for document in corpus: if len(document) > 0: print(document) i = i+1 if i == 3: break 19 1990 52 1 22 ์˜คํŒจ์‚ฐ ํ„ฐ๋„ ์ด๊ฒฉ์ „ ์šฉ์˜์ž ๊ฒ€๊ฑฐ ์„œ์šธ ์—ฐํ•ฉ๋‰ด์Šค ๊ฒฝ์ฐฐ ๊ด€๊ณ„์ž๋“ค์ด 19์ผ ์˜คํ›„ ์„œ์šธ ๊ฐ•๋ถ๊ตฌ ์˜คํŒจ์‚ฐ ํ„ฐ๋„ ์ธ๊ทผ์—์„œ ์‚ฌ์ œ ์ด๊ธฐ๋ฅผ ๋ฐœ์‚ฌํ•ด ๊ฒฝ์ฐฐ์„ ์‚ดํ•ดํ•œ ์šฉ์˜์ž ์„ฑ๋ชจ ์”จ๋ฅผ ๊ฒ€๊ฑฐํ•˜๊ณ  ์žˆ๋‹ค ... ์ค‘๋žต ... ์ˆฒ์—์„œ ๋ฐœ๊ฒฌ๋๊ณ  ์ผ๋ถ€๋Š” ์„ฑ์”จ๊ฐ€ ์†Œ์ง€ํ•œ ๊ฐ€๋ฐฉ ์•ˆ์— ์žˆ์—ˆ๋‹ค ํ…Œํ—ค๋ž€ ์—ฐํ•ฉ๋‰ด์Šค ๊ฐ•ํ›ˆ์ƒ ํŠนํŒŒ์› ์ด์šฉ ์Šน๊ฐ์ˆ˜ ๊ธฐ์ค€ ์„ธ๊ณ„ ์ตœ๋Œ€ ๊ณตํ•ญ์ธ ์•„๋ž์—๋ฏธ๋ฆฌํŠธ ๋‘๋ฐ”์ด ๊ตญ์ œ๊ณตํ•ญ์€ 19์ผ ํ˜„์ง€์‹œ๊ฐ„ ์ด ๊ณตํ•ญ์„ ์ด๋ฅ™ํ•˜๋Š” ๋ชจ๋“  ํ•ญ๊ณต๊ธฐ์˜ ํƒ‘์Šน๊ฐ์€ ์‚ผ์„ฑ์ „์ž์˜ ๊ฐค๋Ÿญ์‹œ๋…ธํŠธ7์„ ํœด๋Œ€ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค๊ณ  ๋ฐํ˜”๋‹ค ... ์ค‘๋žต ... ์ด๋Ÿฐ ์กฐ์น˜๋Š” ๋‘๋ฐ”์ด ๊ตญ์ œ๊ณตํ•ญ๋ฟ ์•„๋‹ˆ๋ผ ์‹ ๊ณตํ•ญ์ธ ๋‘๋ฐ”์ด ์›”๋“œ์„ผํ„ฐ์—๋„ ์ ์šฉ๋œ๋‹ค ๋ฐฐํ„ฐ๋ฆฌ ํญ๋ฐœ ๋ฌธ์ œ๋กœ ํšŒ์ˆ˜๋œ ๊ฐค๋Ÿญ์‹œ๋…ธํŠธ7 ์—ฐํ•ฉ๋‰ด์Šค ์ž๋ฃŒ ์‚ฌ์ง„ ์ •์ƒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. soynlp๋Š” ํ•™์Šต ๊ธฐ๋ฐ˜์˜ ๋‹จ์–ด ํ† ํฌ ๋‚˜์ด์ €์ด๋ฏ€๋กœ ๊ธฐ์กด์˜ KoNLPy์—์„œ ์ œ๊ณตํ•˜๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋“ค๊ณผ๋Š” ๋‹ฌ๋ฆฌ ํ•™์Šต ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ „์ฒด ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ์‘์ง‘ ํ™•๋ฅ ๊ณผ ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ ๋‹จ์–ด ์ ์ˆ˜ํ‘œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. WordExtractor.extract()๋ฅผ ํ†ตํ•ด์„œ ์ „์ฒด ์ฝ”ํผ์Šค์— ๋Œ€ํ•ด ๋‹จ์–ด ์ ์ˆ˜ํ‘œ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. word_extractor = WordExtractor() word_extractor.train(corpus) word_score_table = word_extractor.extract() training was done. used memory 5.186 Gb all cohesion probabilities was computed. # words = 223348 all branching entropies was computed # words = 361598 all accessor variety was computed # words = 361598 ํ•™์Šต์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 3. SOYNLP์˜ ์‘์ง‘ ํ™•๋ฅ (cohesion probability) ์‘์ง‘ ํ™•๋ฅ ์€ ๋‚ด๋ถ€ ๋ฌธ์ž์—ด(substring)์ด ์–ผ๋งˆ๋‚˜ ์‘์ง‘ํ•˜์—ฌ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์ฒ™๋„์ž…๋‹ˆ๋‹ค. ์‘์ง‘ ํ™•๋ฅ ์€ ๋ฌธ์ž์—ด์„ ๋ฌธ์ž ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ๋‚ด๋ถ€ ๋ฌธ์ž์—ด์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •์—์„œ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ ๊ฐ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๊ทธ๋‹ค์Œ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜์—ฌ ๋ˆ„์  ๊ณฑ์„ ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ’์ด ๋†’์„์ˆ˜๋ก ์ „์ฒด ์ฝ”ํผ์Šค์—์„œ ์ด ๋ฌธ์ž์—ด ์‹œํ€€์Šค๋Š” ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ๋“ฑ์žฅํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์ˆ˜์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›์—'๋ผ๋Š” 7์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ ๋ฌธ์ž ์‹œํ€€์Šค์— ๋Œ€ํ•ด์„œ ๊ฐ ๋‚ด๋ถ€ ๋ฌธ์ž์—ด์˜ ์Šค์ฝ”์–ด๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹ค์Šต์„ ํ†ตํ•ด ์ง์ ‘ ์‘์ง‘ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. '๋ฐ˜ํฌํ•œ'์˜ ์‘์ง‘ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. word_score_table["๋ฐ˜ํฌํ•œ"].cohesion_forward 0.08838002913645132 ๊ทธ๋ ‡๋‹ค๋ฉด '๋ฐ˜ํฌ ํ•œ๊ฐ•'์˜ ์‘์ง‘ ํ™•๋ฅ ์€ '๋ฐ˜ํฌํ•œ'์˜ ์‘์ง‘ ํ™•๋ฅ ๋ณด๋‹ค ๋†’์„๊นŒ์š”? word_score_table["๋ฐ˜ํฌ ํ•œ๊ฐ•"].cohesion_forward 0.19841268168224552 '๋ฐ˜ํฌ ํ•œ๊ฐ•'์€ '๋ฐ˜ํฌํ•œ'๋ณด๋‹ค ์‘์ง‘ ํ™•๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด '๋ฐ˜ํฌํ•œ๊ฐ•๊ณต'์€ ์–ด๋–จ๊นŒ์š”? word_score_table["๋ฐ˜ํฌํ•œ๊ฐ•๊ณต"].cohesion_forward 0.2972877884078849 ์—ญ์‹œ๋‚˜ '๋ฐ˜ํฌ ํ•œ๊ฐ•'๋ณด๋‹ค ์‘์ง‘ ํ™•๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค. '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›'์€ ์–ด๋–จ๊นŒ์š”? word_score_table["๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›"].cohesion_forward 0.37891487632839754 '๋ฐ˜ํฌํ•œ๊ฐ•๊ณต'๋ณด๋‹ค ์‘์ง‘ ํ™•๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๋‹ค๊ฐ€ ์กฐ์‚ฌ '์—'๋ฅผ ๋ถ™์ธ '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›์—'๋Š” ์–ด๋–จ๊นŒ์š”? word_score_table["๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›์—"].cohesion_forward 0.33492963377557666 ์˜คํžˆ๋ ค '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›'๋ณด๋‹ค ์‘์ง‘๋„๊ฐ€ ๋‚ฎ์•„์ง‘๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๊ฒฐํ•ฉ ๋„๋Š” '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›'์ผ ๋•Œ๊ฐ€ ๊ฐ€์žฅ ๋†’์•˜์Šต๋‹ˆ๋‹ค. ์‘์ง‘๋„๋ฅผ ํ†ตํ•ด ํŒ๋‹จํ•˜๊ธฐ์— ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ํŒ๋‹จํ•˜๊ธฐ์— ๊ฐ€์žฅ ์ ํ•ฉํ•œ ๋ฌธ์ž์—ด์€ '๋ฐ˜ํฌ ํ•œ๊ฐ•๊ณต์›'์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. 4. SOYNLP์˜ ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ(branching entropy) Branching Entropy๋Š” ํ™•๋ฅ  ๋ถ„ํฌ์˜ ์—”ํŠธ๋กœํ”ผ ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ์–ผ๋งˆ๋‚˜ ๋‹ค์Œ ๋ฌธ์ž๊ฐ€ ๋“ฑ์žฅํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์ฒ™๋„์ž…๋‹ˆ๋‹ค. ํ€ด์ฆˆ๋ฅผ ํ•˜๋‚˜ ๋‚ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์–ด๋–ค ๋‹จ์–ด๋ฅผ ์ƒ๊ฐ ์ค‘์ธ๋ฐ, ํ•œ ๋ฌธ์ž์”ฉ ๋งํ•ด๋“œ๋ฆด ํ…Œ๋‹ˆ๊นŒ ๋งค๋ฒˆ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ๋งž์ถ”๋Š” ๊ฒƒ์ด ํ€ด์ฆˆ์ž…๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋Š” '๋””'์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ๋ฌธ์ž๋ฅผ ๋งž์ถฐ๋ณด์„ธ์š”. ์†”์งํžˆ ๊ฐ€๋Š ์ด ์ž˜ ์•ˆ ๊ฐ€์ง€์š”? '๋””'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ์–ด๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋งŽ์€๋ฐ์š”. ์ •๋‹ต์€ '์Šค'์ž…๋‹ˆ๋‹ค. ์ด์ œ '๋””์Šค'๊นŒ์ง€ ๋‚˜์™”๋„ค์š”. '๋””์Šค '๋‹ค์Œ ๋ฌธ์ž๋Š” ๋ญ˜๊นŒ์š”? '๋””์Šค์นด์šดํŠธ'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋‹ˆ๊นŒ '์นด'์ผ๊นŒ? ์•„๋‹ˆ๋ฉด '๋””์Šค์ฝ”๋“œ'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋‹ˆ๊นŒ '์ฝ”'์ธ๊ฐ€? ์ƒ๊ฐํ•ด ๋ณด๋‹ˆ '๋””์Šค์ฝ”'๊ฐ€ ์ •๋‹ต์ผ ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”. ๊ทธ๋Ÿฌ๋ฉด '์ฝ”'์ธ๊ฐ€? '๋””์Šค์•„๋„ˆ๋“œ'๋ผ๋Š” ๊ฒŒ์ž„์ด ์žˆ์œผ๋‹ˆ๊นŒ '์•„'? ์ด ๋‹จ์–ด๋“ค์„ ์ƒ๊ฐํ•˜์‹  ๋ถ„๋“ค์€ ์ „๋ถ€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค. ์ •๋‹ต์€ 'ํ”Œ'์ด์—ˆ์Šต๋‹ˆ๋‹ค. '๋””์Šคํ”Œ'๊นŒ์ง€ ์™”์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ฌธ์ž ๋งž์ถฐ๋ณด์„ธ์š”. ์ด์ œ ์ข€ ๋ช…๋ฐฑํ•ด์ง‘๋‹ˆ๋‹ค. ์ •๋‹ต์€ '๋ ˆ'์ž…๋‹ˆ๋‹ค. '๋””์Šคํ”Œ๋ ˆ์ด' ๋‹ค์Œ์—๋Š” ์–ด๋–ค ๋ฌธ์ž์ผ๊นŒ์š”? ์ •๋‹ต์€ '์ด'์ž…๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ƒ๊ฐํ•œ ๋‹จ์–ด๋Š” '๋””์Šคํ”Œ๋ ˆ์ด'์˜€์Šต๋‹ˆ๋‹ค. ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ๋ฅผ ์ฃผ์–ด์ง„ ๋ฌธ์ž ์‹œํ€€์Šค์—์„œ ๋‹ค์Œ ๋ฌธ์ž ์˜ˆ์ธก์„ ์œ„ํ•ด ํ—ท๊ฐˆ๋ฆฌ๋Š” ์ •๋„๋กœ ๋น„์œ ํ•ด ๋ด…์‹œ๋‹ค. ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ์˜ ๊ฐ’์€ ํ•˜๋‚˜์˜ ์™„์„ฑ๋œ ๋‹จ์–ด์— ๊ฐ€๊นŒ์›Œ์งˆ์ˆ˜๋ก ๋ฌธ๋งฅ์œผ๋กœ ์ธํ•ด ์ ์  ์ •ํ™•ํžˆ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋ฉด์„œ ์ ์  ์ค„์–ด๋“œ๋Š” ์–‘์ƒ์„ ๋ณด์ž…๋‹ˆ๋‹ค. word_score_table["๋””์Šค"].right_branching_entropy 1.6371694761537934 word_score_table["๋””์Šคํ”Œ"].right_branching_entropy -0.0 '๋””์Šค' ๋‹ค์Œ์—๋Š” ๋‹ค์–‘ํ•œ ๋ฌธ์ž๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ 1.63์ด๋ผ๋Š” ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐ˜๋ฉด, '๋””์Šคํ”Œ'์ด๋ผ๋Š” ๋ฌธ์ž์—ด ๋‹ค์Œ์—๋Š” ๋‹ค์Œ ๋ฌธ์ž๋กœ '๋ ˆ'๊ฐ€ ์˜ค๋Š” ๊ฒƒ์ด ๋„ˆ๋ฌด๋‚˜ ๋ช…๋ฐฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— 0์ด๋ž€ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. word_score_table["๋””์Šคํ”Œ๋ ˆ์ด"].right_branching_entropy -0.0 word_score_table["๋””์Šคํ”Œ๋ ˆ์ด"].right_branching_entropy 3.1400392861792916 ๊ฐ‘์ž๊ธฐ ๊ฐ’์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ฌธ์ž ์‹œํ€€์Šค '๋””์Šคํ”Œ๋ ˆ์ด'๋ผ๋Š” ๋ฌธ์ž ์‹œํ€€์Šค ๋‹ค์Œ์—๋Š” ์กฐ์‚ฌ๋‚˜ ๋‹ค๋ฅธ ๋‹จ์–ด์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ•˜๋‚˜์˜ ๋‹จ์–ด๊ฐ€ ๋๋‚˜๋ฉด ๊ทธ ๊ฒฝ๊ณ„ ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋‹ค์‹œ ๋ธŒ๋žœ์นญ ์—”ํŠธ๋กœํ”ผ ๊ฐ’์ด ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฐ’์œผ๋กœ ๋‹จ์–ด๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๊ฒ ์ฃ ? 5. SOYNLP์˜ L tokenizer ํ•œ๊ตญ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ๋‚˜๋ˆˆ ์–ด์ ˆ ํ† ํฐ์€ ์ฃผ๋กœ L ํ† ํฐ + R ํ† ํฐ์˜<NAME>์„ ๊ฐ€์งˆ ๋•Œ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ '๊ณต์›์—'๋Š” '๊ณต์› +์—'๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ฒ ์ง€์š”. ๋˜๋Š” '๊ณต๋ถ€ํ•˜๋Š”'์€ '๊ณต๋ถ€ + ํ•˜๋Š”'์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. L ํ† ํฌ ๋‚˜์ด์ €๋Š” L ํ† ํฐ + R ํ† ํฐ์œผ๋กœ ๋‚˜๋ˆ„๋˜, ๋ถ„๋ฆฌ ๊ธฐ์ค€์„ ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ L ํ† ํฐ์„ ์ฐพ์•„๋‚ด๋Š” ์›๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. from soynlp.tokenizer import LTokenizer scores = {word:score.cohesion_forward for word, score in word_score_table.items()} l_tokenizer = LTokenizer(scores=scores) l_tokenizer.tokenize("๊ตญ์ œ์‚ฌํšŒ์™€ ์šฐ๋ฆฌ์˜ ๋…ธ๋ ฅ๋“ค๋กœ ๋ฒ”์ฃ„๋ฅผ ์ฒ™๊ฒฐํ•˜์ž", flatten=False) [('๊ตญ์ œ์‚ฌํšŒ', '์™€'), ('์šฐ๋ฆฌ', '์˜'), ('๋…ธ๋ ฅ', '๋“ค๋กœ'), ('๋ฒ”์ฃ„', '๋ฅผ'), ('์ฒ™๊ฒฐ', 'ํ•˜์ž')] 6. ์ตœ๋Œ€ ์ ์ˆ˜ ํ† ํฌ ๋‚˜์ด์ € ์ตœ๋Œ€ ์ ์ˆ˜ ํ† ํฌ ๋‚˜์ด์ €๋Š” ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๋ฌธ์žฅ์—์„œ ์ ์ˆ˜๊ฐ€ ๋†’์€ ๊ธ€์ž ์‹œํ€€์Šค๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฐพ์•„๋‚ด๋Š” ํ† ํฌ ๋‚˜์ด์ €์ž…๋‹ˆ๋‹ค. ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๋ฌธ์žฅ์„ ๋„ฃ์–ด์„œ ์ ์ˆ˜๋ฅผ ํ†ตํ•ด ํ† ํฐํ™”๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. from soynlp.tokenizer import MaxScoreTokenizer maxscore_tokenizer = MaxScoreTokenizer(scores=scores) maxscore_tokenizer.tokenize("๊ตญ์ œ์‚ฌํšŒ์™€ ์šฐ๋ฆฌ์˜ ๋…ธ๋ ฅ๋“ค๋กœ ๋ฒ”์ฃ„๋ฅผ ์ฒ™๊ฒฐํ•˜์ž") ['๊ตญ์ œ์‚ฌํšŒ', '์™€', '์šฐ๋ฆฌ', '์˜', '๋…ธ๋ ฅ', '๋“ค๋กœ', '๋ฒ”์ฃ„', '๋ฅผ', '์ฒ™๊ฒฐ', 'ํ•˜์ž'] 4. SOYNLP๋ฅผ ์ด์šฉํ•œ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž ์ •์ œ SNS๋‚˜ ์ฑ„ํŒ… ๋ฐ์ดํ„ฐ์™€ ๊ฐ™์€ ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ์—๋Š” ใ…‹ใ…‹, ใ…Žใ…Ž ๋“ฑ์˜ ์ด๋ชจํ‹ฐ์ฝ˜์˜ ๊ฒฝ์šฐ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์—ฐ์†๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€๋ฐ ใ…‹ใ…‹, ใ…‹ใ…‹ใ…‹, ใ…‹ใ…‹ใ…‹ใ…‹์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ๋ถˆํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์€ ํ•˜๋‚˜๋กœ ์ •๊ทœํ™”์‹œ์ผœ์ค๋‹ˆ๋‹ค. from soynlp.normalizer import * print(emoticon_normalize('์•œใ…‹ใ…‹ใ…‹ใ…‹์ด์˜ํ™”์กด์žผ์“ฐใ… ใ… ใ… ใ… ใ… ', num_repeats=2)) print(emoticon_normalize('์•œใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹์ด์˜ํ™”์กด์žผ์“ฐใ… ใ… ใ… ใ… ', num_repeats=2)) print(emoticon_normalize('์•œใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹์ด์˜ํ™”์กด์žผ์“ฐใ… ใ… ใ… ใ… ใ… ใ… ', num_repeats=2)) print(emoticon_normalize('์•œใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹์ด์˜ํ™”์กด์žผ์“ฐใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ', num_repeats=2)) ์•„ใ…‹ใ…‹์˜ํ™”์กด์žผ์“ฐใ… ใ…  ์•„ใ…‹ใ…‹์˜ํ™”์กด์žผ์“ฐใ… ใ…  ์•„ใ…‹ใ…‹์˜ํ™”์กด์žผ์“ฐใ… ใ…  ์•„ใ…‹ใ…‹์˜ํ™”์กด์žผ์“ฐใ… ใ…  ์˜๋ฏธ ์—†๊ฒŒ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์€ ๋น„๋‹จ ์ด๋ชจํ‹ฐ์ฝ˜์— ํ•œ์ •๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. print(repeat_normalize('์™€ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•˜ํ•ซ', num_repeats=2)) print(repeat_normalize('์™€ํ•˜ํ•˜ ํ•˜ํ•˜ ํ•˜ํ•˜ ํ•ซ', num_repeats=2)) print(repeat_normalize('์™€ํ•˜ํ•˜ ํ•˜ํ•˜ ํ•ซ', num_repeats=2)) ์™€ํ•˜ํ•˜ ํ•ซ ์™€ํ•˜ํ•˜ ํ•ซ ์™€ํ•˜ํ•˜ ํ•ซ 5. Customized KoNLPy ์˜์–ด๊ถŒ ์–ธ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๋งŒ ํ•ด๋„ ๋‹จ์–ด๋“ค์ด ์ž˜ ๋ถ„๋ฆฌ๋˜์ง€๋งŒ, ํ•œ๊ตญ์–ด๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๊ณ  ์•ž์—์„œ ๋ช‡ ์ฐจ๋ก€ ์–ธ๊ธ‰ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋งŒํผ ์ด๋ฒˆ์—๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ์ด๋Ÿฐ ์ƒํ™ฉ์— ๋ด‰์ฐฉํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ํ˜•ํƒœ์†Œ ๋ถ„์„ ์ž…๋ ฅ : '์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.' ํ˜•ํƒœ์†Œ ๋ถ„์„ ๊ฒฐ๊ณผ : ['์€', '๊ฒฝ์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ์‚ฌ์‹ค ์œ„๋ฌธ์žฅ์—์„œ '์€๊ฒฝ์ด'๋Š” ์‚ฌ๋žŒ ์ด๋ฆ„์ด๋ฏ€๋กœ ์ œ๋Œ€๋กœ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” '์€', '๊ฒฝ์ด'์™€ ๊ฐ™์ด ๊ธ€์ž๊ฐ€ ๋ถ„๋ฆฌ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ '์€๊ฒฝ์ด' ๋˜๋Š” ์ตœ์†Œํ•œ '์€๊ฒฝ'์ด๋ผ๋Š” ๋‹จ์–ด ํ† ํฐ์„ ์–ป์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์— ์‚ฌ์šฉ์ž ์‚ฌ์ „์„ ์ถ”๊ฐ€ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. '์€๊ฒฝ์ด'๋Š” ํ•˜๋‚˜์˜ ๋‹จ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„๋ฆฌํ•˜์ง€ ๋ง๋ผ๊ณ  ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์— ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์‚ฌ์ „์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋งˆ๋‹ค ๋‹ค๋ฅธ๋ฐ, ์ƒ๊ฐ๋ณด๋‹ค ๋ณต์žกํ•œ ๊ฒฝ์šฐ๋“ค์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” Customized Konlpy๋ผ๋Š” ์‚ฌ์šฉ์ž ์‚ฌ์ „ ์ถ”๊ฐ€๊ฐ€ ๋งค์šฐ ์‰ฌ์šด ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. pip install customized_konlpy customized_konlpy์—์„œ ์ œ๊ณตํ•˜๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Twitter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ž์„œ ์†Œ๊ฐœํ–ˆ๋˜ ์˜ˆ๋ฌธ์„ ๋‹จ์–ด ํ† ํฐํ™”ํ•ด๋ด…์‹œ๋‹ค. from ckonlpy.tag import Twitter twitter = Twitter() twitter.morphs('์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.') ['์€', '๊ฒฝ์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] ์•ž์„œ ์†Œ๊ฐœํ•œ ์˜ˆ์‹œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ '์€๊ฒฝ์ด'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ '์€', '๊ฒฝ์ด'์™€ ๊ฐ™์ด ๋ถ„๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Twitter์— add_dictionary('๋‹จ์–ด', 'ํ’ˆ์‚ฌ')์™€ ๊ฐ™์€<NAME>์œผ๋กœ ์‚ฌ์ „ ์ถ”๊ฐ€๋ฅผ ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. twitter.add_dictionary('์€๊ฒฝ์ด', 'Noun') ์ œ๋Œ€๋กœ ๋ฐ˜์˜๋˜์—ˆ๋Š”์ง€ ๋™์ผํ•œ ์˜ˆ๋ฌธ์„ ๋‹ค์‹œ ํ˜•ํƒœ์†Œ ๋ถ„์„ํ•ด ๋ด…์‹œ๋‹ค. twitter.morphs('์€๊ฒฝ์ด๋Š” ์‚ฌ๋ฌด์‹ค๋กœ ๊ฐ”์Šต๋‹ˆ๋‹ค.') ['์€๊ฒฝ์ด', '๋Š”', '์‚ฌ๋ฌด์‹ค', '๋กœ', '๊ฐ”์Šต๋‹ˆ๋‹ค', '.'] '์€๊ฒฝ์ด'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ œ๋Œ€๋กœ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ์ธ์‹๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 10. [NLP ์ž…๋ฌธ ] - ์–ธ์–ด ๋ชจ๋ธ(Language Model)์ด๋ž€? ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ์„ ์œ„ํ•ด ํ•„์š”ํ•œ ๊ฐœ๋…์ธ ์–ธ์–ด ๋ชจ๋ธ, BoW, TF-IDF, ๊ทธ๋ฆฌ๊ณ  ๋ฒกํ„ฐ์˜ ์œ ์‚ฌ๋„์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 10-01 ์–ธ์–ด ๋ชจ๋ธ(Language Model)์ด๋ž€? ์–ธ์–ด ๋ชจ๋ธ(Language Model, LM)์€ ์–ธ์–ด๋ผ๋Š” ํ˜„์ƒ์„ ๋ชจ๋ธ๋งํ•˜๊ณ  ์ž ๋‹จ์–ด ์‹œํ€€์Šค(๋ฌธ์žฅ)์— ํ™•๋ฅ ์„ ํ• ๋‹น(assign) ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ๋Š” ํ†ต๊ณ„๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•๊ณผ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์—๋Š” ํ†ต๊ณ„๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•๋ณด๋‹ค๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์ด ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ ํ•ซํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ๊ธฐ์ˆ ์ธ GPT๋‚˜ BERT ๋˜ํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฐœ๋…๊ณผ ์–ธ์–ด ๋ชจ๋ธ์˜ ์ „ํ†ต์  ์ ‘๊ทผ ๋ฐฉ์‹์ธ ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. 1. ์–ธ์–ด ๋ชจ๋ธ(Language Model) ์–ธ์–ด ๋ชจ๋ธ์€ ๋‹จ์–ด ์‹œํ€€์Šค์— ํ™•๋ฅ ์„ ํ• ๋‹น(assign) ํ•˜๋Š” ์ผ์„ ํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์กฐ๊ธˆ ํ’€์–ด์„œ ์“ฐ๋ฉด, ์–ธ์–ด ๋ชจ๋ธ์€ ๊ฐ€์žฅ ์ž์—ฐ์Šค๋Ÿฌ์šด ๋‹จ์–ด ์‹œํ€€์Šค๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด ์‹œํ€€์Šค์— ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ€์žฅ ๋ณดํŽธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์€ ์–ธ์–ด ๋ชจ๋ธ์ด ์ด์ „ ๋‹จ์–ด๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์œ ํ˜•์˜ ์–ธ์–ด ๋ชจ๋ธ๋กœ๋Š” ์ฃผ์–ด์ง„ ์–‘์ชฝ์˜ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๊ฐ€์šด๋ฐ ๋น„์–ด์žˆ๋Š” ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ฌธ์žฅ์˜ ๊ฐ€์šด๋ฐ์— ์žˆ๋Š” ๋‹จ์–ด๋ฅผ ๋น„์›Œ๋†“๊ณ  ์–‘์ชฝ์˜ ๋ฌธ๋งฅ์„ ํ†ตํ•ด์„œ ๋นˆ์นธ์˜ ๋‹จ์–ด์ธ์ง€ ๋งž์ถ”๋Š” ๊ณ ๋“ฑํ•™๊ต ์ˆ˜ํ—˜ ์‹œํ—˜์˜ ๋นˆ์นธ ์ถ”๋ก  ๋ฌธ์ œ์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์ด ์œ ํ˜•์˜ ์–ธ์–ด ๋ชจ๋ธ์€ BERT ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃจ๊ฒŒ ๋  ์˜ˆ์ •์ด๊ณ , ๊ทธ๋•Œ๊นŒ์ง€๋Š” ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ์‹์—๋งŒ ์ง‘์ค‘ํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์— -ing๋ฅผ ๋ถ™์ธ ์–ธ์–ด ๋ชจ๋ธ๋ง(Language Modeling)์€ ์ฃผ์–ด์ง„ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ์•„์ง ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ž‘์—…์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์–ธ์–ด ๋ชจ๋ธ์ด ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ผ์€ ์–ธ์–ด ๋ชจ๋ธ๋ง์ž…๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋กœ ์œ ๋ช…ํ•œ ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™๊ต์—์„œ๋Š” ์–ธ์–ด ๋ชจ๋ธ์„ ๋ฌธ๋ฒ•(grammar)์ด๋ผ๊ณ  ๋น„์œ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์ด ๋‹จ์–ด๋“ค์˜ ์กฐํ•ฉ์ด ์–ผ๋งˆ๋‚˜ ์ ์ ˆํ•œ์ง€, ๋˜๋Š” ํ•ด๋‹น ๋ฌธ์žฅ์ด ์–ผ๋งˆ๋‚˜ ์ ํ•ฉํ•œ์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์ผ์„ ํ•˜๋Š” ๊ฒƒ์ด ๋งˆ์น˜ ๋ฌธ๋ฒ•์ด ํ•˜๋Š” ์ผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 2. ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ  ํ• ๋‹น ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋‹จ์–ด ์‹œํ€€์Šค์— ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๋Š” ์ผ์ด ์™œ ํ•„์š”ํ• ๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋Œ€๋ฌธ์ž P๋Š” ํ™•๋ฅ ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. a. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ(Machine Translation): P(๋‚˜๋Š” ๋ฒ„์Šค๋ฅผ ํƒ”๋‹ค) > P(๋‚˜๋Š” ๋ฒ„์Šค๋ฅผ ํƒœ์šด๋‹ค) : ์–ธ์–ด ๋ชจ๋ธ์€ ๋‘ ๋ฌธ์žฅ์„ ๋น„๊ตํ•˜์—ฌ ์ขŒ์ธก์˜ ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์ด ๋” ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. b. ์˜คํƒ€ ๊ต์ •(Spell Correction) ์„ ์ƒ๋‹˜์ด ๊ต์‹ค๋กœ ๋ถ€๋ฆฌ๋‚˜์ผ€ P(๋‹ฌ๋ ค๊ฐ”๋‹ค) > P(์ž˜๋ ค๊ฐ”๋‹ค) : ์–ธ์–ด ๋ชจ๋ธ์€ ๋‘ ๋ฌธ์žฅ์„ ๋น„๊ตํ•˜์—ฌ ์ขŒ์ธก์˜ ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์ด ๋” ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. c. ์Œ์„ฑ ์ธ์‹(Speech Recognition) P(๋‚˜๋Š” ๋ฉ”๋กฑ์„ ๋จน๋Š”๋‹ค) < P(๋‚˜๋Š” ๋ฉœ๋ก ์„ ๋จน๋Š”๋‹ค) : ์–ธ์–ด ๋ชจ๋ธ์€ ๋‘ ๋ฌธ์žฅ์„ ๋น„๊ตํ•˜์—ฌ ์šฐ์ธก์˜ ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์ด ๋” ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ์€ ์œ„์™€ ๊ฐ™์ด ํ™•๋ฅ ์„ ํ†ตํ•ด ๋ณด๋‹ค ์ ์ ˆํ•œ ๋ฌธ์žฅ์„ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. 3. ์ฃผ์–ด์ง„ ์ด์ „ ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด ์˜ˆ์ธกํ•˜๊ธฐ ์–ธ์–ด ๋ชจ๋ธ์€ ๋‹จ์–ด ์‹œํ€€์Šค์— ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹จ์–ด ์‹œํ€€์Šค์— ํ™•๋ฅ ์„ ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ€์žฅ ๋ณดํŽธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด์ „ ๋‹จ์–ด๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ๋กœ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. A. ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ  ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ, ๋‹จ์–ด ์‹œํ€€์Šค๋ฅผ ๋Œ€๋ฌธ์ž๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) P ( 1 w, 3 w, 5. . w) B. ๋‹ค์Œ ๋‹จ์–ด ๋“ฑ์žฅ ํ™•๋ฅ  ๋‹ค์Œ ๋‹จ์–ด ๋“ฑ์žฅ ํ™•๋ฅ ์„ ์‹์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. -1๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ๋‚˜์—ด๋œ ์ƒํƒœ์—์„œ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( n w, . , n 1 ) |์˜ ๊ธฐํ˜ธ๋Š” ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ (conditional probability)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์„ฏ ๋ฒˆ์งธ ๋‹จ์–ด์˜ ํ™•๋ฅ ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( 5 w, 2 w, 4 ) ์ „์ฒด ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ ์€ ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ์˜ˆ์ธก๋˜๊ณ  ๋‚˜์„œ์•ผ ์•Œ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋‹จ์–ด ์‹œํ€€์Šค์˜ ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( ) P ( 1 w, 3 w, 5. . n ) โˆ = n ( i w, . , 4. ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฐ„๋‹จํ•œ ์ง๊ด€ ๋น„ํ–‰๊ธฐ๋ฅผ ํƒ€๋ ค๊ณ  ๊ณตํ•ญ์— ๊ฐ”๋Š”๋ฐ ์ง€๊ฐ์„ ํ•˜๋Š” ๋ฐ”๋žŒ์— ๋น„ํ–‰๊ธฐ๋ฅผ [?]๋ผ๋Š” ๋ฌธ์žฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. '๋น„ํ–‰๊ธฐ๋ฅผ' ๋‹ค์Œ์— ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ์˜ค๊ฒŒ ๋ ์ง€ ์‚ฌ๋žŒ์€ ์‰ฝ๊ฒŒ '๋†“์ณค๋‹ค'๋ผ๊ณ  ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ ์ง€์‹์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋‹จ์–ด๋“ค์„ ํ›„๋ณด์— ๋†“๊ณ  ๋†“์ณค๋‹ค๋Š” ๋‹จ์–ด๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์ด ๊ฐ€์žฅ ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ธฐ๊ณ„์—๊ฒŒ ์œ„๋ฌธ์žฅ์„ ์ฃผ๊ณ , '๋น„ํ–‰๊ธฐ๋ฅผ' ๋‹ค์Œ์— ๋‚˜์˜ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ด ๋ณด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ๊ณผ์—ฐ ์–ด๋–ป๊ฒŒ ์ตœ๋Œ€ํ•œ ์ •ํ™•ํžˆ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ๊ธฐ๊ณ„๋„ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์•ž์— ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ๋‚˜์™”๋Š”์ง€ ๊ณ ๋ คํ•˜์—ฌ ํ›„๋ณด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋‹จ์–ด๋“ค์— ๋Œ€ํ•ด์„œ ํ™•๋ฅ ์„ ์˜ˆ์ธกํ•ด ๋ณด๊ณ  ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ๋‹จ์–ด๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์•ž์— ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ๋‚˜์™”๋Š”์ง€ ๊ณ ๋ คํ•˜์—ฌ ํ›„๋ณด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋‹จ์–ด๋“ค์— ๋Œ€ํ•ด์„œ ๋“ฑ์žฅ ํ™•๋ฅ ์„ ์ถ”์ •ํ•˜๊ณ  ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ๋‹จ์–ด๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. 5. ๊ฒ€์ƒ‰ ์—”์ง„์—์„œ์˜ ์–ธ์–ด ๋ชจ๋ธ์˜ ์˜ˆ ๊ฒ€์ƒ‰ ์—”์ง„์ด ์ž…๋ ฅ๋œ ๋‹จ์–ด๋“ค์˜ ๋‚˜์—ด์— ๋Œ€ํ•ด์„œ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 10-02 ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ(Statistical Language Model, SLM) ์–ธ์–ด ๋ชจ๋ธ์˜ ์ „ํ†ต์ ์ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์ธ ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ์ด ํ†ต๊ณ„์ ์ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์œผ๋กœ ์–ด๋–ป๊ฒŒ ์–ธ์–ด๋ฅผ ๋ชจ๋ธ๋ง ํ•˜๋Š”์ง€ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ(Statistical Language Model)์€ ์ค„์—ฌ์„œ SLM์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 1. ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ  ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์€ ๋‘ ํ™•๋ฅ  ( ) P ( ) ์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ด€๊ณ„๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ( | ) P ( , ) P ( ) ( , ) P ( ) ( | ) ๋” ๋งŽ์€ ํ™•๋ฅ ์— ๋Œ€ํ•ด์„œ ์ผ๋ฐ˜ํ™”ํ•ด๋ด…์‹œ๋‹ค. 4๊ฐœ์˜ ํ™•๋ฅ ์ด ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์งˆ ๋•Œ, ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( , , , ) P ( ) ( | ) ( | , ) ( | , , ) ์ด๋ฅผ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์˜ ์—ฐ์‡„ ๋ฒ•์น™(chain rule)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ๋Š” 4๊ฐœ๊ฐ€ ์•„๋‹Œ ๊ฐœ์— ๋Œ€ํ•ด์„œ ์ผ๋ฐ˜ํ™”๋ฅผ ํ•ด๋ด…์‹œ๋‹ค. ( 1 x, 3. x) P ( 1 ) ( 2 x) ( 3 x, 2 ) . P ( n x ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์— ๋Œ€ํ•œ ์ •์˜๋ฅผ ํ†ตํ•ด ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ๋ฌธ์žฅ์— ๋Œ€ํ•œ ํ™•๋ฅ  ๋ฌธ์žฅ 'An adorable little boy is spreading smiles'์˜ ํ™•๋ฅ  ( An adorable little boy is spreading smiles ) ๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ๊ฐ ๋‹จ์–ด๋Š” ๋ฌธ๋งฅ์ด๋ผ๋Š” ๊ด€๊ณ„๋กœ ์ธํ•ด ์ด์ „ ๋‹จ์–ด์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ ๋‚˜์˜จ ๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ๋‹จ์–ด๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์ด ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์„ ๊ตฌํ•˜๊ณ ์ž ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ•œ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์˜ ์ผ๋ฐ˜ํ™” ์‹์„ ๋ฌธ์žฅ์˜ ํ™•๋ฅ  ๊ด€์ ์—์„œ ๋‹ค์‹œ ์ ์–ด๋ณด๋ฉด ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์€ ๊ฐ ๋‹จ์–ด๋“ค์ด ์ด์ „ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋‹ค์Œ ๋‹จ์–ด๋กœ ๋“ฑ์žฅํ•  ํ™•๋ฅ ์˜ ๊ณฑ์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ( 1 w, 3 w, 5. . n ) โˆ = n ( n w, . , n 1 ) ์œ„์˜ ๋ฌธ์žฅ์— ํ•ด๋‹น ์‹์„ ์ ์šฉํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( An adorable little boy is spreading smiles ) P ( An ) P ( adorable|An ) P ( little|An adorable ) P ( boy|An adorable little ) P ( is|An adorable little boy ) P ( spreading|An adorable little boy is ) P ( smiles|An adorable little boy is spreading ) ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ์˜ˆ์ธก ํ™•๋ฅ ๋“ค์„ ๊ณฑํ•ฉ๋‹ˆ๋‹ค. 3. ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ ์ ‘๊ทผ ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ ๋‹จ์–ด์— ๋Œ€ํ•œ ์˜ˆ์ธก ํ™•๋ฅ ์„ ๋ชจ๋‘ ๊ณฑํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์•Œ์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด SLM์€ ์ด์ „ ๋‹จ์–ด๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ๋‹จ์–ด์— ๋Œ€ํ•œ ํ™•๋ฅ ์€ ์–ด๋–ป๊ฒŒ ๊ตฌํ• ๊นŒ์š”? ์ •๋‹ต์€ ์นด์šดํŠธ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. An adorable little boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ, is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์ธ ( is|An adorable little boy ) ๋ฅผ ๊ตฌํ•ด๋ด…์‹œ๋‹ค. (is|An adorable little boy ) count(An adorable little boy is ) count(An adorable little boy ) ๊ทธ ํ™•๋ฅ ์€ ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ธฐ๊ณ„๊ฐ€ ํ•™์Šตํ•œ ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ์—์„œ An adorable little boy๊ฐ€ 100๋ฒˆ ๋“ฑ์žฅํ–ˆ๋Š”๋ฐ ๊ทธ๋‹ค์Œ์— is๊ฐ€ ๋“ฑ์žฅํ•œ ๊ฒฝ์šฐ๋Š” 30๋ฒˆ์ด๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ ( is|An adorable little boy ) ๋Š” 30%์ž…๋‹ˆ๋‹ค. 4. ์นด์šดํŠธ ๊ธฐ๋ฐ˜ ์ ‘๊ทผ์˜ ํ•œ๊ณ„ - ํฌ์†Œ ๋ฌธ์ œ(Sparsity Problem) ์–ธ์–ด ๋ชจ๋ธ์€ ์‹ค์ƒํ™œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด์˜ ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ๊ทผ์‚ฌ ๋ชจ๋ธ๋ง ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ์•„๋ณผ ๋ฐฉ๋ฒ•์€ ์—†๊ฒ ์ง€๋งŒ ํ˜„์‹ค์—์„œ๋„ An adorable little boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์ด๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹ค์ œ ์ž์—ฐ์–ด์˜ ํ™•๋ฅ  ๋ถ„ํฌ, ํ˜„์‹ค์—์„œ์˜ ํ™•๋ฅ  ๋ถ„ํฌ๋ผ๊ณ  ๋ช…์นญ ํ•ฉ์‹œ๋‹ค. ๊ธฐ๊ณ„์—๊ฒŒ ๋งŽ์€ ์ฝ”ํผ์Šค๋ฅผ ํ›ˆ๋ จ์‹œ์ผœ์„œ ์–ธ์–ด ๋ชจ๋ธ์„ ํ†ตํ•ด ํ˜„์‹ค์—์„œ์˜ ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ๊ทผ์‚ฌํ•˜๋Š” ๊ฒƒ์ด ์–ธ์–ด ๋ชจ๋ธ์˜ ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์นด์šดํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ ‘๊ทผํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค(corpus). ์ฆ‰, ๋‹ค์‹œ ๋งํ•ด ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ์ •๋ง ๋ฐฉ๋Œ€ํ•œ ์–‘์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. (is|An adorable little boy ) count(An adorable little boy is ) count(An adorable little boy ) ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์™€ ๊ฐ™์ด (is|An adorable little boy ) ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ์—์„œ ๊ธฐ๊ณ„๊ฐ€ ํ›ˆ๋ จํ•œ ์ฝ”ํผ์Šค์— An adorable little boy is๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์—†์—ˆ๋‹ค๋ฉด ์ด ๋‹จ์–ด ์‹œํ€€์Šค์— ๋Œ€ํ•œ ํ™•๋ฅ ์€ 0์ด ๋ฉ๋‹ˆ๋‹ค. ๋˜๋Š” An adorable little boy๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์—†์—ˆ๋‹ค๋ฉด ๋ถ„๋ชจ๊ฐ€ 0์ด ๋˜์–ด ํ™•๋ฅ ์€ ์ •์˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ฝ”ํผ์Šค์— ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์—†๋‹ค๊ณ  ํ•ด์„œ ์ด ํ™•๋ฅ ์„ 0 ๋˜๋Š” ์ •์˜๋˜์ง€ ์•Š๋Š” ํ™•๋ฅ ์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์ด ์ •ํ™•ํ•œ ๋ชจ๋ธ๋ง ๋ฐฉ๋ฒ•์ผ๊นŒ์š”? ์•„๋‹™๋‹ˆ๋‹ค. ํ˜„์‹ค์—์„  An adorable little boy is๋ผ๋Š” ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์กด์žฌํ•˜๊ณ  ๋˜ ๋ฌธ๋ฒ•์—๋„ ์ ํ•ฉํ•˜๋ฏ€๋กœ ์ •๋‹ต์ผ ๊ฐ€๋Šฅ์„ฑ ๋˜ํ•œ ๋†’์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์ถฉ๋ถ„ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€์ธกํ•˜์ง€ ๋ชปํ•˜์—ฌ ์–ธ์–ด๋ฅผ ์ •ํ™•ํžˆ ๋ชจ๋ธ๋ง ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํฌ์†Œ ๋ฌธ์ œ(sparsity problem)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๋ฌธ์ œ๋ฅผ ์™„ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ”๋กœ ์ด์–ด์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” n-gram ์–ธ์–ด ๋ชจ๋ธ์ด๋‚˜ ์ด ์ฑ…์—์„œ ๋‹ค๋ฃจ์ง€๋Š” ์•Š์ง€๋งŒ ์Šค๋ฌด๋”ฉ์ด๋‚˜ ๋ฐฑ์˜คํ”„์™€ ๊ฐ™์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ผ๋ฐ˜ํ™”(generalization) ๊ธฐ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํฌ์†Œ ๋ฌธ์ œ์— ๋Œ€ํ•œ ๊ทผ๋ณธ์ ์ธ ํ•ด๊ฒฐ์ฑ…์€ ๋˜์ง€ ๋ชปํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด๋Ÿฌํ•œ ํ•œ๊ณ„๋กœ ์ธํ•ด ์–ธ์–ด ๋ชจ๋ธ์˜ ํŠธ๋ Œ๋“œ๋Š” ํ†ต๊ณ„์  ์–ธ์–ด ๋ชจ๋ธ์—์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง ์–ธ์–ด ๋ชจ๋ธ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 10-03 N-gram ์–ธ์–ด ๋ชจ๋ธ(N-gram Language Model) n-gram ์–ธ์–ด ๋ชจ๋ธ์€ ์—ฌ์ „ํžˆ ์นด์šดํŠธ์— ๊ธฐ๋ฐ˜ํ•œ ํ†ต๊ณ„์  ์ ‘๊ทผ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ SLM์˜ ์ผ์ข…์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์•ž์„œ ๋ฐฐ์šด ์–ธ์–ด ๋ชจ๋ธ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ด์ „์— ๋“ฑ์žฅํ•œ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ผ๋ถ€ ๋‹จ์–ด๋งŒ ๊ณ ๋ คํ•˜๋Š” ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋•Œ ์ผ๋ถ€ ๋‹จ์–ด๋ฅผ ๋ช‡ ๊ฐœ ๋ณด๋Š๋ƒ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š”๋ฐ ์ด๊ฒƒ์ด n-gram์—์„œ์˜ n์ด ๊ฐ€์ง€๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. 1. ์ฝ”ํผ์Šค์—์„œ ์นด์šดํŠธํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ์˜ ๊ฐ์†Œ. SLM์˜ ํ•œ๊ณ„๋Š” ํ›ˆ๋ จ ์ฝ”ํผ์Šค์— ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์žฅ์ด๋‚˜ ๋‹จ์–ด๊ฐ€ ์—†์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์žฅ์ด ๊ธธ์–ด์งˆ์ˆ˜๋ก ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ ๊ทธ ๋ฌธ์žฅ์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜๋ฉด ์นด์šดํŠธํ•  ์ˆ˜ ์—†์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐธ๊ณ ํ•˜๋Š” ๋‹จ์–ด๋“ค์„ ์ค„์ด๋ฉด ์นด์šดํŠธ๋ฅผ ํ•  ์ˆ˜ ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( is|An adorable little boy ) P ( is|boy ) ๊ฐ€๋ น, An adorable little boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์„ ๊ทธ๋ƒฅ boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ๋กœ ์ƒ๊ฐํ•ด ๋ณด๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”? ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์— An adorable little boy is๊ฐ€ ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ๋ณด๋‹ค๋Š” boy is๋ผ๋Š” ๋” ์งง์€ ๋‹จ์–ด ์‹œํ€€์Šค๊ฐ€ ์กด์žฌํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋” ๋†’์Šต๋‹ˆ๋‹ค. ์กฐ๊ธˆ ์ง€๋‚˜์นœ ์ผ๋ฐ˜ํ™”๋กœ ๋Š๊ปด์ง„๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด little boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ๋„ ๋Œ€์•ˆ์ž…๋‹ˆ๋‹ค. ( is|An adorable little boy ) P ( is|little boy ) ์ฆ‰, ์•ž์—์„œ๋Š” An adorable little boy๊ฐ€ ๋‚˜์™”์„ ๋•Œ is๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” An adorable little boy๊ฐ€ ๋‚˜์˜จ ํšŸ์ˆ˜์™€ An adorable little boy is๊ฐ€ ๋‚˜์˜จ ํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธํ•ด์•ผ๋งŒ ํ–ˆ์ง€๋งŒ, ์ด์ œ๋Š” ๋‹จ์–ด์˜ ํ™•๋ฅ ์„ ๊ตฌํ•˜๊ณ ์ž ๊ธฐ์ค€ ๋‹จ์–ด์˜ ์•ž ๋‹จ์–ด๋ฅผ ์ „๋ถ€ ํฌํ•จํ•ด์„œ ์นด์šดํŠธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์•ž ๋‹จ์–ด ์ค‘ ์ž„์˜์˜ ๊ฐœ์ˆ˜๋งŒ ํฌํ•จํ•ด์„œ ์นด์šดํŠธํ•˜์—ฌ ๊ทผ์‚ฌํ•˜์ž๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ ํ•ด๋‹น ๋‹จ์–ด์˜ ์‹œํ€€์Šค๋ฅผ ์นด์šดํŠธํ•  ํ™•๋ฅ ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค. 2. N-gram ์ด๋•Œ ์ž„์˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์ค€์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด n-gram์ž…๋‹ˆ๋‹ค. n-gram์€ n ๊ฐœ์˜ ์—ฐ์†์ ์ธ ๋‹จ์–ด ๋‚˜์—ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ n ๊ฐœ์˜ ๋‹จ์–ด ๋ญ‰์น˜ ๋‹จ์œ„๋กœ ๋Š์–ด์„œ ์ด๋ฅผ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋ฌธ์žฅ An adorable little boy is spreading smiles์ด ์žˆ์„ ๋•Œ, ๊ฐ n์— ๋Œ€ํ•ด์„œ n-gram์„ ์ „๋ถ€ ๊ตฌํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. unigrams : an, adorable, little, boy, is, spreading, smiles bigrams : an adorable, adorable little, little boy, boy is, is spreading, spreading smiles trigrams : an adorable little, adorable little boy, little boy is, boy is spreading, is spreading smiles 4-grams : an adorable little boy, adorable little boy is, little boy is spreading, boy is spreading smiles n-gram์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” n์ด 1์ผ ๋•Œ๋Š” ์œ ๋‹ˆ๊ทธ๋žจ(unigram), 2์ผ ๋•Œ๋Š” ๋ฐ”์ด ๊ทธ๋žจ(bigram), 3์ผ ๋•Œ๋Š” ํŠธ๋ผ์ด ๊ทธ๋žจ(trigram)์ด๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ณ  n์ด 4 ์ด์ƒ์ผ ๋•Œ๋Š” gram ์•ž์— ๊ทธ๋Œ€๋กœ ์ˆซ์ž๋ฅผ ๋ถ™์—ฌ์„œ ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ถœ์ฒ˜์— ๋”ฐ๋ผ์„œ๋Š” ์œ ๋‹ˆ๊ทธ๋žจ, ๋ฐ”์ด ๊ทธ๋žจ, ํŠธ๋ผ์ด ๊ทธ๋žจ ๋˜ํ•œ ๊ฐ๊ฐ 1-gram, 2-gram, 3-gram์ด๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. n-gram์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. n-gram์„ ํ†ตํ•œ ์–ธ์–ด ๋ชจ๋ธ์—์„œ๋Š” ๋‹ค์Œ์— ๋‚˜์˜ฌ ๋‹จ์–ด์˜ ์˜ˆ์ธก์€ ์˜ค์ง n-1๊ฐœ์˜ ๋‹จ์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 'An adorable little boy is spreading' ๋‹ค์Œ์— ๋‚˜์˜ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ํ•  ๋•Œ, n=4๋ผ๊ณ  ํ•œ 4-gram์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ๊ฒฝ์šฐ, spreading ๋‹ค์Œ์— ์˜ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์€ n-1์— ํ•ด๋‹น๋˜๋Š” ์•ž์˜ 3๊ฐœ์˜ ๋‹จ์–ด๋งŒ์„ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค. ( |boy is spreading ) count(boy is spreading w ) count(boy is spreading) ๋งŒ์•ฝ ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์—์„œ boy is spreading๊ฐ€ 1,000๋ฒˆ ๋“ฑ์žฅํ–ˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  boy is spreading insults๊ฐ€ 500๋ฒˆ ๋“ฑ์žฅํ–ˆ์œผ๋ฉฐ, boy is spreading smiles๊ฐ€ 200๋ฒˆ ๋“ฑ์žฅํ–ˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด boy is spreading ๋‹ค์Œ์— insults๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ ์€ 50%์ด๋ฉฐ, smiles๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ ์€ 20%์ž…๋‹ˆ๋‹ค. ํ™•๋ฅ ์  ์„ ํƒ์— ๋”ฐ๋ผ ์šฐ๋ฆฌ๋Š” insults๊ฐ€ ๋” ๋งž๋Š”๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ( insults|boy is spreading ) 0.500 ( smiles|boy is spreading ) 0.200 3. N-gram Language Model์˜ ํ•œ๊ณ„ ์•ž์„œ 4-gram์„ ํ†ตํ•œ ์–ธ์–ด ๋ชจ๋ธ์˜ ๋™์ž‘ ๋ฐฉ์‹์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์กฐ๊ธˆ ์˜๋ฌธ์ด ๋‚จ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ณธ 4-gram ์–ธ์–ด ๋ชจ๋ธ์€ ์ฃผ์–ด์ง„ ๋ฌธ์žฅ์—์„œ ์•ž์— ์žˆ๋˜ ๋‹จ์–ด์ธ '์ž‘๊ณ  ์‚ฌ๋ž‘์Šค๋Ÿฌ์šด(an adorable little)'์ด๋ผ๋Š” ์ˆ˜์‹์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋ฐ˜์˜ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ '์ž‘๊ณ  ์‚ฌ๋ž‘์Šค๋Ÿฌ์šด' ์ˆ˜์‹์–ด๊นŒ์ง€ ๋ชจ๋‘ ๊ณ ๋ คํ•˜์—ฌ ์ž‘๊ณ  ์‚ฌ๋ž‘ํ•˜๋Š” ์†Œ๋…„์ด ํ•˜๋Š” ํ–‰๋™์— ๋Œ€ํ•ด ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์–ธ์–ด ๋ชจ๋ธ์ด์—ˆ๋‹ค๋ฉด ๊ณผ์—ฐ '์ž‘๊ณ  ์‚ฌ๋ž‘์Šค๋Ÿฌ์šด ์†Œ๋…„์ด' '๋ชจ์š•์„ ํผํŠธ๋ ธ๋‹ค'๋ผ๋Š” ๋ถ€์ •์ ์ธ ๋‚ด์šฉ์ด '์›ƒ์Œ ์ง€์—ˆ๋‹ค'๋ผ๋Š” ๊ธ์ •์ ์ธ ๋‚ด์šฉ ๋Œ€์‹  ์„ ํƒ๋˜์—ˆ์„๊นŒ์š”? ๋ฌผ๋ก  ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ฐ€์ •ํ•˜๋Š๋ƒ์˜ ๋‚˜๋ฆ„์ด๊ณ , ์ „ํ˜€ ๋ง์ด ์•ˆ ๋˜๋Š” ๋ฌธ์žฅ์€ ์•„๋‹ˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ ์ง€์ ํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์€ n-gram์€ ์•ž์˜ ๋‹จ์–ด ๋ช‡ ๊ฐœ๋งŒ ๋ณด๋‹ค ๋ณด๋‹ˆ ์˜๋„ํ•˜๊ณ  ์‹ถ์€ ๋Œ€๋กœ ๋ฌธ์žฅ์„ ๋๋งบ์Œํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋ฌธ์žฅ์„ ์ฝ๋‹ค ๋ณด๋ฉด ์•ž ๋ถ€๋ถ„๊ณผ ๋’ท๋ถ€๋ถ„์˜ ๋ฌธ๋งฅ์ด ์ „ํ˜€ ์—ฐ๊ฒฐ ์•ˆ ๋˜๋Š” ๊ฒฝ์šฐ๋„ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๋ก ๋งŒ ๋งํ•˜์ž๋ฉด, ์ „์ฒด ๋ฌธ์žฅ์„ ๊ณ ๋ คํ•œ ์–ธ์–ด ๋ชจ๋ธ๋ณด๋‹ค๋Š” ์ •ํ™•๋„๊ฐ€ ๋–จ์–ด์งˆ ์ˆ˜๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ† ๋Œ€๋กœ n-gram ๋ชจ๋ธ์— ๋Œ€ํ•œ ํ•œ๊ณ„์ ์„ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (1) ํฌ์†Œ ๋ฌธ์ œ(Sparsity Problem) ๋ฌธ์žฅ์— ์กด์žฌํ•˜๋Š” ์•ž์— ๋‚˜์˜จ ๋‹จ์–ด๋ฅผ ๋ชจ๋‘ ๋ณด๋Š” ๊ฒƒ๋ณด๋‹ค ์ผ๋ถ€ ๋‹จ์–ด๋งŒ์„ ๋ณด๋Š” ๊ฒƒ์œผ๋กœ ํ˜„์‹ค์ ์œผ๋กœ ์ฝ”ํผ์Šค์—์„œ ์นด์šดํŠธํ•  ์ˆ˜ ์žˆ๋Š” ํ™•๋ฅ ์„ ๋†’์ผ ์ˆ˜๋Š” ์žˆ์—ˆ์ง€๋งŒ, n-gram ์–ธ์–ด ๋ชจ๋ธ๋„ ์—ฌ์ „ํžˆ n-gram์— ๋Œ€ํ•œ ํฌ์†Œ ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. (2) n์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์€ trade-off ๋ฌธ์ œ. ์•ž์—์„œ ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๋ณผ์ง€ n์„ ์ •ํ•˜๋Š” ๊ฒƒ์€ trade-off๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ž„์˜์˜ ๊ฐœ์ˆ˜์ธ n์„ 1๋ณด๋‹ค๋Š” 2๋กœ ์„ ํƒํ•˜๋Š” ๊ฒƒ์€ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ์—์„œ ์–ธ์–ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, spreading๋งŒ ๋ณด๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” is spreading์„ ๋ณด๊ณ  ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด ๋” ์ •ํ™•ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์ ˆํ•œ ๋ฐ์ดํ„ฐ์˜€๋‹ค๋ฉด ์–ธ์–ด ๋ชจ๋ธ์ด ์ ์–ด๋„ spreading ๋‹ค์Œ์— ๋™์‚ฌ๋ฅผ ๊ณ ๋ฅด์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. n์„ ํฌ๊ฒŒ ์„ ํƒํ•˜๋ฉด ์‹ค์ œ ํ›ˆ๋ จ ์ฝ”ํผ์Šค์—์„œ ํ•ด๋‹น n-gram์„ ์นด์šดํŠธํ•  ์ˆ˜ ์žˆ๋Š” ํ™•๋ฅ ์€ ์ ์–ด์ง€๋ฏ€๋กœ ํฌ์†Œ ๋ฌธ์ œ๋Š” ์ ์  ์‹ฌ๊ฐํ•ด์ง‘๋‹ˆ๋‹ค. ๋˜ํ•œ n์ด ์ปค์งˆ์ˆ˜๋ก ๋ชจ๋ธ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ปค์ง„๋‹ค๋Š” ๋ฌธ์ œ์ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฝ”ํผ์Šค์˜ ๋ชจ๋“  n-gram์— ๋Œ€ํ•ด์„œ ์นด์šดํŠธ๋ฅผ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. n์„ ์ž‘๊ฒŒ ์„ ํƒํ•˜๋ฉด ํ›ˆ๋ จ ์ฝ”ํผ์Šค์—์„œ ์นด์šดํŠธ๋Š” ์ž˜ ๋˜๊ฒ ์ง€๋งŒ ๊ทผ์‚ฌ์˜ ์ •ํ™•๋„๋Š” ํ˜„์‹ค์˜ ํ™•๋ฅ ๋ถ„ํฌ์™€ ๋ฉ€์–ด์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ ์ ˆํ•œ n์„ ์„ ํƒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ•œ trade-off ๋ฌธ์ œ๋กœ ์ธํ•ด ์ •ํ™•๋„๋ฅผ ๋†’์ด๋ ค๋ฉด n์€ ์ตœ๋Œ€ 5๋ฅผ ๋„˜๊ฒŒ ์žก์•„์„œ๋Š” ์•ˆ ๋œ๋‹ค๊ณ  ๊ถŒ์žฅ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. n์ด ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์œ ๋ช…ํ•œ ์˜ˆ์ œ ํ•˜๋‚˜๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™๊ต์˜ ๊ณต์œ  ์ž๋ฃŒ์— ๋”ฐ๋ฅด๋ฉด, ์›”์ŠคํŠธ๋ฆฌํŠธ ์ €๋„์—์„œ 3,800๋งŒ ๊ฐœ์˜ ๋‹จ์–ด ํ† ํฐ์— ๋Œ€ํ•˜์—ฌ n-gram ์–ธ์–ด ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ณ , 1,500๋งŒ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ–ˆ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ฑ๋Šฅ์ด ๋‚˜์™”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒ ์ง€๋งŒ, ํŽ„ ํ”Œ๋ ‰์„œํ‹ฐ(perplexity)๋Š” ์ˆ˜์น˜๊ฐ€ ๋‚ฎ์„์ˆ˜๋ก ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Unigram Bigram Trigram Perplexity 962 170 109 ์œ„์˜ ๊ฒฐ๊ณผ๋Š” n์„ 1์—์„œ 2, 2์—์„œ 3์œผ๋กœ ์˜ฌ๋ฆด ๋•Œ๋งˆ๋‹ค ์„ฑ๋Šฅ์ด ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 4. ์ ์šฉ ๋ถ„์•ผ(Domain)์— ๋งž๋Š” ์ฝ”ํผ์Šค์˜ ์ˆ˜์ง‘ ์–ด๋–ค ๋ถ„์•ผ์ธ์ง€, ์–ด๋–ค ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ธ์ง€์— ๋”ฐ๋ผ์„œ ํŠน์ • ๋‹จ์–ด๋“ค์˜ ํ™•๋ฅ  ๋ถ„ํฌ๋Š” ๋‹น์—ฐํžˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ฐ€๋ น, ๋งˆ์ผ€ํŒ… ๋ถ„์•ผ์—์„œ๋Š” ๋งˆ์ผ€ํŒ… ๋‹จ์–ด๊ฐ€ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋“ฑ์žฅํ•  ๊ฒƒ์ด๊ณ , ์˜๋ฃŒ ๋ถ„์•ผ์—์„œ๋Š” ์˜๋ฃŒ ๊ด€๋ จ ๋‹จ์–ด๊ฐ€ ๋‹น์—ฐํžˆ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์–ธ์–ด ๋ชจ๋ธ์— ์‚ฌ์šฉํ•˜๋Š” ์ฝ”ํผ์Šค๋ฅผ ํ•ด๋‹น ๋„๋ฉ”์ธ์˜ ์ฝ”ํผ์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋‹น์—ฐํžˆ ์–ธ์–ด ๋ชจ๋ธ์ด ์ œ๋Œ€๋กœ ๋œ ์–ธ์–ด ์ƒ์„ฑ์„ ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” ์ด๋ฅผ ์–ธ์–ด ๋ชจ๋ธ์˜ ์•ฝ์ ์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋Š”๋ฐ, ํ›ˆ๋ จ์— ์‚ฌ์šฉ๋œ ๋„๋ฉ”์ธ ์ฝ”ํผ์Šค๊ฐ€ ๋ฌด์—‡์ด๋ƒ์— ๋”ฐ๋ผ์„œ ์„ฑ๋Šฅ์ด ๋น„์•ฝ์ ์œผ๋กœ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. 5. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ(Neural Network Based Language Model) ์—ฌ๊ธฐ์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š๊ฒ ์ง€๋งŒ, N-gram Language Model์˜ ํ•œ๊ณ„์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ๋ถ„๋ชจ, ๋ถ„์ž์— ์ˆซ์ž๋ฅผ ๋”ํ•ด์„œ ์นด์šดํŠธํ–ˆ์„ ๋•Œ 0์ด ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๋“ฑ์˜ ์—ฌ๋Ÿฌ ์ผ๋ฐ˜ํ™”(generalization) ๋ฐฉ๋ฒ•๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋Ÿผ์—๋„ ๋ณธ์งˆ์ ์œผ๋กœ n-gram ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ทจ์•ฝ์ ์„ ์™„์ „ํžˆ ํ•ด๊ฒฐํ•˜์ง€๋Š” ๋ชปํ•˜์˜€๊ณ , ์ด๋ฅผ ์œ„ํ•œ ๋Œ€์•ˆ์œผ๋กœ N-gram Language Model๋ณด๋‹ค ๋Œ€์ฒด์ ์œผ๋กœ ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์ด ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 10-04 ํ•œ๊ตญ์–ด์—์„œ์˜ ์–ธ์–ด ๋ชจ๋ธ(Language Model for Korean Sentences) ์˜์–ด๋‚˜ ๊ธฐํƒ€ ์–ธ์–ด์— ๋น„ํ•ด์„œ ํ•œ๊ตญ์–ด๋Š” ์–ธ์–ด ๋ชจ๋ธ๋กœ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ๊ฐ€ ํ›จ์”ฌ ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. 1. ํ•œ๊ตญ์–ด๋Š” ์–ด์ˆœ์ด ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค. ํ•œ๊ตญ์–ด์—์„œ๋Š” ์–ด์ˆœ์ด ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด์ „ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‹ค์Œ ๋‹จ์–ด๊ฐ€ ๋‚˜ํƒ€๋‚  ํ™•๋ฅ ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š”๋ฐ ์–ด์ˆœ์ด ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์€ ๋‹ค์Œ ๋‹จ์–ด๋กœ ์–ด๋–ค ๋‹จ์–ด๋“  ๋“ฑ์žฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋‚˜๋Š” ์šด๋™์„ ํ•ฉ๋‹ˆ๋‹ค ์ฒด์œก๊ด€์—์„œ. 2. ๋‚˜๋Š” ์ฒด์œก๊ด€์—์„œ ์šด๋™์„ ํ•ฉ๋‹ˆ๋‹ค. 3. ์ฒด์œก๊ด€์—์„œ ์šด๋™์„ ํ•ฉ๋‹ˆ๋‹ค. 4. ๋‚˜๋Š” ์šด๋™์„ ์ฒด์œก๊ด€์—์„œ ํ•ฉ๋‹ˆ๋‹ค. 4๊ฐœ์˜ ๋ฌธ์žฅ์€ ์ „๋ถ€ ์˜๋ฏธ๊ฐ€ ํ†ตํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด '๋‚˜๋Š”'์ด๋ผ๋Š” ์ฃผ์–ด๋ฅผ ์ƒ๋žตํ•ด๋„ ๋ง์ด ๋ผ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‹จ์–ด ์ˆœ์„œ๋ฅผ ๋’ค์ฃฝ๋ฐ•์ฃฝ์œผ๋กœ ๋ฐ”๊พธ์–ด๋†”๋„ ํ•œ๊ตญ์–ด๋Š” ์˜๋ฏธ๊ฐ€ ์ „๋‹ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ™•๋ฅ ์— ๊ธฐ๋ฐ˜ํ•œ ์–ธ์–ด ๋ชจ๋ธ์ด ์ œ๋Œ€๋กœ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. 2. ํ•œ๊ตญ์–ด๋Š” ๊ต์ฐฉ์–ด์ด๋‹ค. ํ•œ๊ตญ์–ด๋Š” ๊ต์ฐฉ์–ด์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ•œ๊ตญ์–ด์—์„œ์˜ ์–ธ์–ด ๋ชจ๋ธ ์ž‘๋™์„ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„์ธ ์–ด์ ˆ ๋‹จ์œ„๋กœ ํ† ํฐํ™”๋ฅผ ํ•  ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์žฅ์—์„œ ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•œ ๋‹จ์–ด์˜ ์ˆ˜๊ฐ€ ๊ต‰์žฅํžˆ ๋Š˜์–ด๋‚ฉ๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋กœ ๊ต์ฐฉ์–ด์ธ ํ•œ๊ตญ์–ด์—๋Š” ์กฐ์‚ฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์กฐ์‚ฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•œ๊ตญ์–ด์—๋Š” ์–ด๋–ค ํ–‰๋™์„ ํ•˜๋Š” ๋™์‚ฌ์˜ ์ฃผ์–ด๋‚˜ ๋ชฉ์ ์–ด๋ฅผ ์œ„ํ•ด์„œ ์กฐ์‚ฌ๋ผ๋Š” ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น '๊ทธ๋…€'๋ผ๋Š” ๋‹จ์–ด ํ•˜๋‚˜๋งŒ ํ•ด๋„ ๊ทธ๋…€๊ฐ€, ๊ทธ๋…€๋ฅผ, ๊ทธ๋…€์˜, ๊ทธ๋…€์™€, ๊ทธ๋…€๋กœ, ๊ทธ๋…€๊ป˜์„œ, ๊ทธ๋…€์ฒ˜๋Ÿผ ๋“ฑ๊ณผ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ๊ตญ์–ด์—์„œ๋Š” ํ† ํฐํ™”๋ฅผ ํ†ตํ•ด ์ ‘์‚ฌ๋‚˜ ์กฐ์‚ฌ ๋“ฑ์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ์ค‘์š”ํ•œ ์ž‘์—…์ด ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 3. ํ•œ๊ตญ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ œ๋Œ€๋กœ ์ง€์ผœ์ง€์ง€ ์•Š๋Š”๋‹ค. ํ•œ๊ตญ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ œ๋Œ€๋กœ ํ•˜์ง€ ์•Š์•„๋„ ์˜๋ฏธ๊ฐ€ ์ „๋‹ฌ๋˜๋ฉฐ, ๋„์–ด์“ฐ๊ธฐ ๊ทœ์น™ ๋˜ํ•œ ์ƒ๋Œ€์ ์œผ๋กœ ๊นŒ๋‹ค๋กœ์šด ์–ธ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์— ์žˆ์–ด์„œ ํ•œ๊ตญ์–ด ์ฝ”ํผ์Šค๋Š” ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ œ๋Œ€๋กœ ์ง€์ผœ์ง€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ํ† ํฐ์ด ์ œ๋Œ€๋กœ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š๋Š” ์ฑ„ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉ๋œ๋‹ค๋ฉด ์–ธ์–ด ๋ชจ๋ธ์€ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 10-05 ํŽ„ ํ”Œ๋ ‰์„œํ‹ฐ(Perplexity, PPL) ๋‘ ๊ฐœ์˜ ๋ชจ๋ธ A, B๊ฐ€ ์žˆ์„ ๋•Œ ์ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์€ ์–ด๋–ป๊ฒŒ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ๋‘ ๊ฐœ์˜ ๋ชจ๋ธ์„ ์˜คํƒ€ ๊ต์ •, ๊ธฐ๊ณ„ ๋ฒˆ์—ญ ๋“ฑ์˜ ํ‰๊ฐ€์— ํˆฌ์ž…ํ•ด ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ชจ๋ธ์ด ํ•ด๋‹น ์—…๋ฌด์˜ ์„ฑ๋Šฅ์„ ๋ˆ„๊ฐ€ ๋” ์ž˜ํ–ˆ๋Š”์ง€๋ฅผ ๋น„๊ตํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‘ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ๋น„๊ตํ•˜๊ณ ์ž, ์ผ์ผ์ด ๋ชจ๋ธ๋“ค์— ๋Œ€ํ•ด์„œ ์‹ค์ œ ์ž‘์—…์„ ์‹œ์ผœ๋ณด๊ณ  ์ •ํ™•๋„๋ฅผ ๋น„๊ตํ•˜๋Š” ์ž‘์—…์€ ๊ณต์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์ด ๋“œ๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋น„๊ตํ•ด์•ผ ํ•˜๋Š” ๋ชจ๋ธ์ด ๋‘ ๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ ๊ทธ ์ด์ƒ์˜ ์ˆ˜๋ผ๋ฉด ์‹œ๊ฐ„์€ ๋น„๊ตํ•ด์•ผ ํ•˜๋Š” ๋ชจ๋ธ์˜ ์ˆ˜๋งŒํผ ๋ฐฐ๋กœ ๋Š˜์–ด๋‚  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ‰๊ฐ€๋ณด๋‹ค๋Š” ์–ด์ฉŒ๋ฉด ์กฐ๊ธˆ์€ ๋ถ€์ •ํ™•ํ•  ์ˆ˜๋Š” ์žˆ์–ด๋„ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋น ๋ฅด๊ฒŒ ์‹์œผ๋กœ ๊ณ„์‚ฐ๋˜๋Š” ๋” ๊ฐ„๋‹จํ•œ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๋ชจ๋ธ ๋‚ด์—์„œ ์ž์‹ ์˜ ์„ฑ๋Šฅ์„ ์ˆ˜์น˜ํ™”ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋†“๋Š” ํŽ„ ํ”Œ๋ ‰์„œํ‹ฐ(perplexity)์ž…๋‹ˆ๋‹ค. 1. ์–ธ์–ด ๋ชจ๋ธ์˜ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•(Evaluation metric) : PPL ํŽ„ ํ”Œ๋ ‰์„œํ‹ฐ(perplexity)๋Š” ์–ธ์–ด ๋ชจ๋ธ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ํ‰๊ฐ€ ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต ์ค„์—ฌ์„œ PPL์ด๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์™œ perplexity๋ผ๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ–ˆ์„๊นŒ์š”? ์˜์–ด์—์„œ 'perplexed'๋Š” 'ํ—ท๊ฐˆ๋ฆฌ๋Š”'๊ณผ ์œ ์‚ฌํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์—ฌ๊ธฐ์„œ PPL์€ 'ํ—ท๊ฐˆ๋ฆฌ๋Š” ์ •๋„'๋กœ ์ดํ•ดํ•ฉ์‹œ๋‹ค. PPL๋ฅผ ์ฒ˜์Œ ๋ฐฐ์šธ ๋•Œ ๋‹ค์†Œ ๋‚ฏ์„ค๊ฒŒ ๋Š๊ปด์งˆ ์ˆ˜ ์žˆ๋Š” ์ ์ด ์žˆ๋‹ค๋ฉด, PPL์€ ์ˆ˜์น˜๊ฐ€ ๋†’์œผ๋ฉด ์ข‹์€ ์„ฑ๋Šฅ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, '๋‚ฎ์„์ˆ˜๋ก' ์–ธ์–ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. PPL์€ ๋ฌธ์žฅ์˜ ๊ธธ์ด๋กœ ์ •๊ทœํ™”๋œ ๋ฌธ์žฅ ํ™•๋ฅ ์˜ ์—ญ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ ์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ์˜ PPL์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. P ( ) P ( 1 w, 3. . w) 1 = P ( 1 w, 3. . N ๋ฌธ์žฅ์˜ ํ™•๋ฅ ์— ์ฒด์ธ ๋ฃฐ(chain rule)์„ ์ ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. P ( ) 1 ( 1 w, 3. . w) = โˆ = N ( i w, 2 ์—ฌ๊ธฐ์— n-gram์„ ์ ์šฉํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด bigram ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ์—๋Š” ์‹์ด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. P ( ) 1 i 1 P ( i w โˆ’ ) 2. ๋ถ„๊ธฐ ๊ณ„์ˆ˜(Branching factor) PPL์€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋ถ„๊ธฐ ๊ณ„์ˆ˜(branching factor)์ž…๋‹ˆ๋‹ค. PPL์€ ์ด ์–ธ์–ด ๋ชจ๋ธ์ด ํŠน์ • ์‹œ์ ์—์„œ ํ‰๊ท ์ ์œผ๋กœ ๋ช‡ ๊ฐœ์˜ ์„ ํƒ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์–ธ์–ด ๋ชจ๋ธ์— ์–ด๋–ค ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์„ ์ฃผ๊ณ  ์ธก์ •ํ–ˆ๋”๋‹ˆ PPL์ด 10์ด ๋‚˜์™”๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ•ด๋‹น ์–ธ์–ด ๋ชจ๋ธ์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋“  ์‹œ์ (time step)๋งˆ๋‹ค ํ‰๊ท  10๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ๊ฒƒ์ด ์ •๋‹ต์ธ์ง€ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋‘ ์–ธ์–ด ๋ชจ๋ธ์˜ PPL์„ ๊ฐ๊ฐ ๊ณ„์‚ฐ ํ›„์— PPL์˜ ๊ฐ’์„ ๋น„๊ตํ•˜๋ฉด, ๋‘ ์–ธ์–ด ๋ชจ๋ธ ์ค‘ PPL์ด ๋” ๋‚ฎ์€ ์–ธ์–ด ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ๋” ์ข‹๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. P ( ) P ( 1 w, 3. . w) 1 = ( 10 ) 1 = 10 1 10 ๋‹จ, ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์— ์žˆ์–ด์„œ ์ฃผ์˜ํ•  ์ ์€ PPL์˜ ๊ฐ’์ด ๋‚ฎ๋‹ค๋Š” ๊ฒƒ์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์ƒ์—์„œ ๋†’์€ ์ •ํ™•๋„๋ฅผ ๋ณด์ธ๋‹ค๋Š” ๊ฒƒ์ด์ง€, ์‚ฌ๋žŒ์ด ์ง์ ‘ ๋Š๋ผ๊ธฐ์— ์ข‹์€ ์–ธ์–ด ๋ชจ๋ธ์ด๋ผ๋Š” ๊ฒƒ์„ ๋ฐ˜๋“œ์‹œ ์˜๋ฏธํ•˜์ง„ ์•Š๋Š”๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์–ธ์–ด ๋ชจ๋ธ์˜ PPL์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์˜์กดํ•˜๋ฏ€๋กœ ๋‘ ๊ฐœ ์ด์ƒ์˜ ์–ธ์–ด ๋ชจ๋ธ์„ ๋น„๊ตํ•  ๋•Œ๋Š” ์ •๋Ÿ‰์ ์œผ๋กœ ์–‘์ด ๋งŽ๊ณ , ๋˜ํ•œ ๋„๋ฉ”์ธ์— ์•Œ๋งž์€ ๋™์ผํ•œ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ์‹ ๋ขฐ๋„๊ฐ€ ๋†’๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 3. ๊ธฐ์กด ์–ธ์–ด ๋ชจ๋ธ Vs. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ. ํŽ˜์ด์Šค๋ถ AI ์—ฐ๊ตฌํŒ€์€ ์•ž์„œ ๋ฐฐ์šด n-gram ์–ธ์–ด ๋ชจ๋ธ๊ณผ ์ดํ›„ ๋ฐฐ์šฐ๊ฒŒ ๋  ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ PPL๋กœ ์„ฑ๋Šฅ ํ…Œ์ŠคํŠธ๋ฅผ ํ•œ ํ‘œ๋ฅผ ๊ณต๊ฐœํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://engineering.fb.com/2016/10/25/ml-applications/building-an-efficient-neural-language-model-over-a-billion-words/ ํ‘œ์—์„œ ๋งจ ์œ„์˜ ์ค„์˜ ์–ธ์–ด ๋ชจ๋ธ์ด n-gram์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ์ด๋ฉฐ PPL์ด 67.6์œผ๋กœ ์ธก์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 5-gram์„ ์‚ฌ์šฉํ•˜์˜€์œผ๋ฉฐ, 5-gram ์•ž์— Interpolated Kneser-Ney๋ผ๋Š” ์ด๋ฆ„์ด ๋ถ™์—ˆ๋Š”๋ฐ ์ด ์ฑ…์—์„œ๋Š” ๋ณ„๋„ ์„ค๋ช…์„ ์ƒ๋žตํ•˜๊ฒ ๋‹ค๊ณ  ํ–ˆ๋˜ ์ผ๋ฐ˜ํ™”(generalization) ๋ฐฉ๋ฒ•์ด ์‚ฌ์šฉ๋œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๊ทธ ์•„๋ž˜์˜ ๋ชจ๋ธ๋“ค์€ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ๋“ค๋กœ ํŽ˜์ด์Šค๋ถ AI ์—ฐ๊ตฌํŒ€์ด ์ž์‹ ๋“ค์˜ ์–ธ์–ด ๋ชจ๋ธ์„ ๋‹ค๋ฅธ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ๊ธฐ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•„์ง RNN๊ณผ LSTM ๋“ฑ์ด ๋ฌด์—‡์ธ์ง€ ๋ฐฐ์šฐ์ง€๋Š” ์•Š์•˜์ง€๋งŒ, ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ๋“ค์€ ๋Œ€๋ถ€๋ถ„ n-gram์„ ์ด์šฉํ•œ ์–ธ์–ด ๋ชจ๋ธ๋ณด๋‹ค ๋” ์ข‹์€ ์„ฑ๋Šฅ ํ‰๊ฐ€๋ฅผ ๋ฐ›์•˜์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 11. [NLP ์ž…๋ฌธ ] - ํ…์ŠคํŠธ์˜ ์œ ์‚ฌ๋„(Text similarity) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํ…์ŠคํŠธ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ทธ์ค‘ ์ •๋ณด ๊ฒ€์ƒ‰๊ณผ ํ…์ŠคํŠธ ๋งˆ์ด๋‹ ๋ถ„์•ผ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ ํ…์ŠคํŠธ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ธ DTM(Document Term Matrix)๊ณผ TF-IDF(Term Frequency-Inverse Document Frequency)์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋ฅผ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์น˜ํ™”๋ฅผ ํ•˜๊ณ  ๋‚˜๋ฉด, ํ†ต๊ณ„์ ์ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ๋ฌธ์„œ๋กœ ์ด๋ฃจ์–ด์ง„ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์„ ๋•Œ ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ํŠน์ • ๋ฌธ์„œ ๋‚ด์—์„œ ์–ผ๋งˆ๋‚˜ ์ค‘์š”ํ•œ ๊ฒƒ์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๊ฑฐ๋‚˜, ๋ฌธ์„œ์˜ ํ•ต์‹ฌ์–ด ์ถ”์ถœ, ๊ฒ€์ƒ‰ ์—”์ง„์—์„œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์˜ ์ˆœ์œ„ ๊ฒฐ์ •, ๋ฌธ์„œ๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋“ฑ์˜ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 11-01 ๋‹จ์–ด์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ• ์—ฌ๊ธฐ์„œ๋Š” ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ ๋‹จ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ• ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ๋‹จ์–ด์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์—๋Š” ์–ด๋–ค ๊ฒƒ์ด ์žˆ์œผ๋ฉฐ, ์•ž์œผ๋กœ ์ด ์ฑ…์—์„œ๋Š” ์–ด๋–ค ์ˆœ์„œ๋กœ ๋‹จ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•˜๊ฒŒ ๋  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 1. ๋‹จ์–ด์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ• ๋‹จ์–ด์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ ๊ตญ์†Œ ํ‘œํ˜„(Local Representation) ๋ฐฉ๋ฒ•๊ณผ ๋ถ„์‚ฐ ํ‘œํ˜„(Distributed Representation) ๋ฐฉ๋ฒ•์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ๊ตญ์†Œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น ๋‹จ์–ด ๊ทธ ์ž์ฒด๋งŒ ๋ณด๊ณ , ํŠน์ • ๊ฐ’์„ ๋งคํ•‘ํ•˜์—ฌ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ฉฐ, ๋ถ„์‚ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ๊ทธ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ณ ์ž ์ฃผ๋ณ€์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด puppy(๊ฐ•์•„์ง€), cute(๊ท€์—ฌ์šด), lovely(์‚ฌ๋ž‘์Šค๋Ÿฌ์šด)๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์„ ๋•Œ ๊ฐ ๋‹จ์–ด์— 1๋ฒˆ, 2๋ฒˆ, 3๋ฒˆ ๋“ฑ๊ณผ ๊ฐ™์€ ์ˆซ์ž๋ฅผ ๋งคํ•‘(mapping) ํ•˜์—ฌ ๋ถ€์—ฌํ•œ๋‹ค๋ฉด ์ด๋Š” ๊ตญ์†Œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๋ถ„์‚ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์˜ ์˜ˆ๋ฅผ ํ•˜๋‚˜ ๋“ค์–ด๋ณด๋ฉด ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์ฐธ๊ณ ํ•ฉ๋‹ˆ๋‹ค. puppy(๊ฐ•์•„์ง€)๋ผ๋Š” ๋‹จ์–ด ๊ทผ์ฒ˜์—๋Š” ์ฃผ๋กœ cute(๊ท€์—ฌ์šด), lovely(์‚ฌ๋ž‘์Šค๋Ÿฌ์šด)์ด๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ, puppy๋ผ๋Š” ๋‹จ์–ด๋Š” cute, lovely ํ•œ ๋Š๋‚Œ์ด ๋‹ค๋กœ ๋‹จ์–ด๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ด ๋‘ ๋ฐฉ๋ฒ•์˜ ์ฐจ์ด๋Š” ๊ตญ์†Œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ๋‹จ์–ด์˜ ์˜๋ฏธ, ๋‰˜์•™์Šค๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†์ง€๋งŒ, ๋ถ„์‚ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ๋‹จ์–ด์˜ ๋‰˜์•™์Šค๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋น„์Šทํ•œ ์˜๋ฏธ๋กœ ๊ตญ์†Œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•(Local Representation)์„ ์ด์‚ฐ ํ‘œํ˜„(Discrete Representation)์ด๋ผ๊ณ ๋„ ํ•˜๋ฉฐ, ๋ถ„์‚ฐ ํ‘œํ˜„(Distributed Representation)์„ ์—ฐ์† ํ‘œํ˜„(Continuous Represnetation)์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ์˜๊ฒฌ์œผ๋กœ ๊ตฌ๊ธ€์˜ ์—ฐ๊ตฌ์› ํ† ๋งˆ์Šค ๋ฏธ์ฝ” ๋กœ๋ธŒ(Tomas Mikolov)๋Š” 2016๋…„์— ํ•œ ๋ฐœํ‘œ์—์„œ ์ž ์žฌ ์˜๋ฏธ ๋ถ„์„(LSA)์ด๋‚˜ ์ž ์žฌ ๋””๋ฆฌํด๋ ˆ ํ• ๋‹น(LDA)๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•๋“ค์€ ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ์—ฐ์† ํ‘œํ˜„(Continuous Represnetation)์ด์ง€๋งŒ, ์—„๋ฐ€ํžˆ ๋งํ•ด์„œ ๋‹ค๋ฅธ ์ ‘๊ทผ์˜ ๋ฐฉ๋ฒ•๋ก ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์›Œ๋“œํˆฌ๋ฒกํ„ฐ(Word2vec)์™€ ๊ฐ™์€ ๋ถ„์‚ฐ ํ‘œํ˜„(Distributed Representation)์€ ์•„๋‹Œ ๊ฒƒ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์—ฐ์† ํ‘œํ˜„์„ ๋ถ„์‚ฐ ํ‘œํ˜„์„ ํฌ๊ด„ํ•˜๊ณ  ์žˆ๋Š” ๋” ํฐ ๊ฐœ๋…์œผ๋กœ ์„ค๋ช…ํ•˜๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค. 2. ๋‹จ์–ด ํ‘œํ˜„์˜ ์นดํ…Œ๊ณ ๋ฆฌํ™” ์ด ์ฑ…์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด ํ‘œํ˜„์„ ์นดํ…Œ๊ณ ๋ฆฌํ™”ํ•˜์—ฌ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์˜ Bag of Words๋Š” ๊ตญ์†Œ ํ‘œํ˜„์—(Local Representation)์— ์†ํ•˜๋ฉฐ, ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์นด์šดํŠธ(Count) ํ•˜์—ฌ ๋‹จ์–ด๋ฅผ ์ˆ˜์น˜ํ™”ํ•˜๋Š” ๋‹จ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด ์ฑ•ํ„ฐ์—์„œ๋Š” BoW์™€ ๊ทธ์˜ ํ™•์žฅ์ธ DTM(๋˜๋Š” TDM)์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜๊ณ , ์ด๋Ÿฌํ•œ ๋นˆ๋„์ˆ˜ ๊ธฐ๋ฐ˜ ๋‹จ์–ด ํ‘œํ˜„์— ๋‹จ์–ด์˜ ์ค‘์š”๋„์— ๋”ฐ๋ฅธ ๊ฐ€์ค‘์น˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋Š” TF-IDF์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ฑ•ํ„ฐ์—์„œ๋Š” ์—ฐ์† ํ‘œํ˜„(Continuous Representation)์— ์†ํ•˜๋ฉด์„œ, ์˜ˆ์ธก(prediction)์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹จ์–ด์˜ ๋‰˜์•™์Šค๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์›Œ๋“œํˆฌ๋ฒกํ„ฐ(Word2Vec)์™€ ๊ทธ์˜ ํ™•์žฅ์ธ ํŒจ์ŠคํŠธ ํ…์ŠคํŠธ(FastText)๋ฅผ ํ•™์Šตํ•˜๊ณ , ์˜ˆ์ธก๊ณผ ์นด์šดํŠธ๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ๋ชจ๋‘ ์‚ฌ์šฉ๋œ ๊ธ€๋กœ๋ธŒ(GloVe)์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 11-02๋ฐฑ ์˜ค๋ธŒ ์›Œ์ฆˆ(Bag of Words) ๋ฐฉ๋ฒ• ๋‹จ์–ด์˜ ๋“ฑ์žฅ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š” ๋นˆ๋„์ˆ˜ ๊ธฐ๋ฐ˜์˜ ๋‹จ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ธ Bag of Words์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. Bag of Words๋ž€? Bag of Words๋ž€ ๋‹จ์–ด๋“ค์˜ ์ˆœ์„œ๋Š” ์ „ํ˜€ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ , ๋‹จ์–ด๋“ค์˜ ์ถœํ˜„ ๋นˆ๋„(frequency)์—๋งŒ ์ง‘์ค‘ํ•˜๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์น˜ํ™” ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. Bag of Words๋ฅผ ์ง์—ญํ•˜๋ฉด ๋‹จ์–ด๋“ค์˜ ๊ฐ€๋ฐฉ์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด๋“ค์ด ๋“ค์–ด์žˆ๋Š” ๊ฐ€๋ฐฉ์„ ์ƒ์ƒํ•ด ๋ด…์‹œ๋‹ค. ๊ฐ–๊ณ  ์žˆ๋Š” ์–ด๋–ค ํ…์ŠคํŠธ ๋ฌธ์„œ์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ๊ฐ€๋ฐฉ์—๋‹ค๊ฐ€ ์ „๋ถ€ ๋„ฃ์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„์—๋Š” ์ด ๊ฐ€๋ฐฉ์„ ํ”๋“ค์–ด ๋‹จ์–ด๋“ค์„ ์„ž์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ํ•ด๋‹น ๋ฌธ์„œ ๋‚ด์—์„œ ํŠน์ • ๋‹จ์–ด๊ฐ€ N ๋ฒˆ ๋“ฑ์žฅํ–ˆ๋‹ค๋ฉด, ์ด ๊ฐ€๋ฐฉ์—๋Š” ๊ทธ ํŠน์ • ๋‹จ์–ด๊ฐ€ N ๊ฐœ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฐ€๋ฐฉ์„ ํ”๋“ค์–ด์„œ ๋‹จ์–ด๋ฅผ ์„ž์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋” ์ด์ƒ ๋‹จ์–ด์˜ ์ˆœ์„œ๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. BoW๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ์ด๋ ‡๊ฒŒ ๋‘ ๊ฐ€์ง€ ๊ณผ์ •์œผ๋กœ ์ƒ๊ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (1) ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. # ๋‹จ์–ด ์ง‘ํ•ฉ ์ƒ์„ฑ. (2) ๊ฐ ์ธ๋ฑ์Šค์˜ ์œ„์น˜์— ๋‹จ์–ด ํ† ํฐ์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ๊ธฐ๋กํ•œ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ BoW์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 1 : ์ •๋ถ€๊ฐ€ ๋ฐœํ‘œํ•˜๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ ์†Œ๋น„์ž๊ฐ€ ๋Š๋ผ๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ ๋‹ค๋ฅด๋‹ค. ๋ฌธ์„œ 1์— ๋Œ€ํ•ด์„œ BoW๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๋œ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocaburary)์„ ๋งŒ๋“ค์–ด ๊ฐ ๋‹จ์–ด์— ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ํ• ๋‹นํ•˜๊ณ , BoW๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. from konlpy.tag import Okt okt = Okt() def build_bag_of_words(document): # ์˜จ์  ์ œ๊ฑฐ ๋ฐ ํ˜•ํƒœ์†Œ ๋ถ„์„ document = document.replace('.', '') tokenized_document = okt.morphs(document) word_to_index = {} bow = [] for word in tokenized_document: if word not in word_to_index.keys(): word_to_index[word] = len(word_to_index) # BoW์— ์ „๋ถ€ ๊ธฐ๋ณธ๊ฐ’ 1์„ ๋„ฃ๋Š”๋‹ค. bow.insert(len(word_to_index) - 1, 1) else: # ์žฌ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค index = word_to_index.get(word) # ์žฌ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋Š” ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค์˜ ์œ„์น˜์— 1์„ ๋”ํ•œ๋‹ค. bow[index] = bow[index] + 1 return word_to_index, bow ํ•ด๋‹น ํ•จ์ˆ˜์— ๋ฌธ์„œ 1์„ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด๋ด…์‹œ๋‹ค. doc1 = "์ •๋ถ€๊ฐ€ ๋ฐœํ‘œํ•˜๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ ์†Œ๋น„์ž๊ฐ€ ๋Š๋ผ๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ ๋‹ค๋ฅด๋‹ค." vocab, bow = build_bag_of_words(doc1) print('vocabulary :', vocab) print('bag of words vector :', bow) vocabulary : {'์ •๋ถ€': 0, '๊ฐ€': 1, '๋ฐœํ‘œ': 2, 'ํ•˜๋Š”': 3, '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ': 4, '๊ณผ': 5, '์†Œ๋น„์ž': 6, '๋Š๋ผ๋Š”': 7, '์€': 8, '๋‹ค๋ฅด๋‹ค': 9} bag of words vector : [1, 2, 1, 1, 2, 1, 1, 1, 1, 1] ๋ฌธ์„œ 1์— ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•œ ๊ฒฐ๊ณผ๋Š” ์ฒซ ๋ฒˆ์งธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ๋ฌธ์„œ 1์˜ BoW๋Š” ๋‘ ๋ฒˆ์งธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, ์ธ๋ฑ์Šค 4์— ํ•ด๋‹นํ•˜๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ ๋‘ ๋ฒˆ ์–ธ๊ธ‰๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค 4์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด 2์ž…๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘๋จ์— ์ฃผ์˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ BoW์—์„œ ๋‹ค์„ฏ ๋ฒˆ์งธ ๊ฐ’์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ํ•œ๊ตญ์–ด์—์„œ ๋ถˆ์šฉ์–ด์— ํ•ด๋‹น๋˜๋Š” ์กฐ์‚ฌ๋“ค ๋˜ํ•œ ์ œ๊ฑฐํ•œ๋‹ค๋ฉด ๋” ์ •์ œ๋œ BoW๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 2. Bag of Words์˜ ๋‹ค๋ฅธ ์˜ˆ์ œ๋“ค ๋ฌธ์„œ 2 : ์†Œ๋น„์ž๋Š” ์ฃผ๋กœ ์†Œ๋น„ํ•˜๋Š” ์ƒํ’ˆ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์„ ๋Š๋‚€๋‹ค. ์œ„์˜ ํ•จ์ˆ˜์— ์ž„์˜์˜ ๋ฌธ์„œ 2๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. doc2 = '์†Œ๋น„์ž๋Š” ์ฃผ๋กœ ์†Œ๋น„ํ•˜๋Š” ์ƒํ’ˆ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์„ ๋Š๋‚€๋‹ค.' vocab, bow = build_bag_of_words(doc2) print('vocabulary :', vocab) print('bag of words vector :', bow) vocabulary : {'์†Œ๋น„์ž': 0, '๋Š”': 1, '์ฃผ๋กœ': 2, '์†Œ๋น„': 3, 'ํ•˜๋Š”': 4, '์ƒํ’ˆ': 5, '์„': 6, '๊ธฐ์ค€': 7, '์œผ๋กœ': 8, '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ': 9, '๋Š๋‚€๋‹ค': 10} bag of words vector : [1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1] ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2๋ฅผ ํ•ฉ์ณ์„œ ๋ฌธ์„œ 3์ด๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ณ , BoW๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 3: ์ •๋ถ€๊ฐ€ ๋ฐœํ‘œํ•˜๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ ์†Œ๋น„์ž๊ฐ€ ๋Š๋ผ๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ ๋‹ค๋ฅด๋‹ค. ์†Œ๋น„์ž๋Š” ์ฃผ๋กœ ์†Œ๋น„ํ•˜๋Š” ์ƒํ’ˆ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์„ ๋Š๋‚€๋‹ค. doc3 = doc1 + ' ' + doc2 vocab, bow = build_bag_of_words(doc3) print('vocabulary :', vocab) print('bag of words vector :', bow) vocabulary : {'์ •๋ถ€': 0, '๊ฐ€': 1, '๋ฐœํ‘œ': 2, 'ํ•˜๋Š”': 3, '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ': 4, '๊ณผ': 5, '์†Œ๋น„์ž': 6, '๋Š๋ผ๋Š”': 7, '์€': 8, '๋‹ค๋ฅด๋‹ค': 9, '๋Š”': 10, '์ฃผ๋กœ': 11, '์†Œ๋น„': 12, '์ƒํ’ˆ': 13, '์„': 14, '๊ธฐ์ค€': 15, '์œผ๋กœ': 16, '๋Š๋‚€๋‹ค': 17} bag of words vector : [1, 2, 1, 2, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1] ๋ฌธ์„œ 3์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์€ ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ๋‹จ์–ด๋“ค์„ ๋ชจ๋‘ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BoW๋Š” ์ข…์ข… ์—ฌ๋Ÿฌ ๋ฌธ์„œ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ํ•ฉ์นœ ๋’ค์—, ํ•ด๋‹น ๋‹จ์–ด ์ง‘ํ•ฉ์— ๋Œ€ํ•œ ๊ฐ ๋ฌธ์„œ์˜ BoW๋ฅผ ๊ตฌํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ๋ฌธ์„œ 3์— ๋Œ€ํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์„œ 1, ๋ฌธ์„œ 2์˜ BoW๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 3 ๋‹จ์–ด ์ง‘ํ•ฉ์— ๋Œ€ํ•œ ๋ฌธ์„œ 1 BoW : [1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0] ๋ฌธ์„œ 3 ๋‹จ์–ด ์ง‘ํ•ฉ์— ๋Œ€ํ•œ ๋ฌธ์„œ 2 BoW : [0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 2, 1, 1, 1] ๋ฌธ์„œ 3 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์ด๋ผ๋Š” ๋‹จ์–ด๋Š” ์ธ๋ฑ์Šค๊ฐ€ 4์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์ด๋ผ๋Š” ๋‹จ์–ด๋Š” ๋ฌธ์„œ 1์—์„œ๋Š” 2ํšŒ ๋“ฑ์žฅํ•˜๋ฉฐ, ๋ฌธ์„œ 2์—์„œ๋Š” 1ํšŒ ๋“ฑ์žฅํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ๋‘ BoW์˜ ์ธ๋ฑ์Šค 4์˜ ๊ฐ’์€ ๊ฐ๊ฐ 2์™€ 1์ด ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BoW๋Š” ๊ฐ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ ์ˆ˜์น˜ํ™”ํ•˜๋Š” ํ…์ŠคํŠธ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด๋ฏ€๋กœ ์ฃผ๋กœ ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋“ฑ์žฅํ–ˆ๋Š”์ง€๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์„œ๊ฐ€ ์–ด๋–ค ์„ฑ๊ฒฉ์˜ ๋ฌธ์„œ์ธ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์ž‘์—…์— ์“ฐ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋‚˜ ์—ฌ๋Ÿฌ ๋ฌธ์„œ ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์— ์ฃผ๋กœ ์“ฐ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น, '๋‹ฌ๋ฆฌ๊ธฐ', '์ฒด๋ ฅ', '๊ทผ๋ ฅ'๊ณผ ๊ฐ™์€ ๋‹จ์–ด๊ฐ€ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋ฉด ํ•ด๋‹น ๋ฌธ์„œ๋ฅผ ์ฒด์œก ๊ด€๋ จ ๋ฌธ์„œ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋ฉฐ, '๋ฏธ๋ถ„', '๋ฐฉ์ •์‹', '๋ถ€๋“ฑ์‹'๊ณผ ๊ฐ™์€ ๋‹จ์–ด๊ฐ€ ์ž์ฃผ ๋“ฑ์žฅํ•œ๋‹ค๋ฉด ์ˆ˜ํ•™ ๊ด€๋ จ ๋ฌธ์„œ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. CountVectorizer ํด๋ž˜์Šค๋กœ BoW ๋งŒ๋“ค๊ธฐ ์‚ฌ์ดํ‚ท ๋Ÿฐ์—์„œ๋Š” ๋‹จ์–ด์˜ ๋นˆ๋„๋ฅผ Count ํ•˜์—ฌ Vector๋กœ ๋งŒ๋“œ๋Š” CountVectorizer ํด๋ž˜์Šค๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด ์˜์–ด์— ๋Œ€ํ•ด์„œ๋Š” ์†์‰ฝ๊ฒŒ BoW๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CountVectorizer๋กœ ๊ฐ„๋‹จํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ BoW๋ฅผ ๋งŒ๋“œ๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. from sklearn.feature_extraction.text import CountVectorizer corpus = ['you know I want your love. because I love you.'] vector = CountVectorizer() # ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ๋ก print('bag of words vector :', vector.fit_transform(corpus).toarray()) # ๊ฐ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ถ€์—ฌ๋˜์—ˆ๋Š”์ง€๋ฅผ ์ถœ๋ ฅ print('vocabulary :',vector.vocabulary_) bag of words vector : [[1 1 2 1 2 1]] vocabulary : {'you': 4, 'know': 1, 'want': 3, 'your': 5, 'love': 2, 'because': 0} ์˜ˆ์ œ ๋ฌธ์žฅ์—์„œ you์™€ love๋Š” ๋‘ ๋ฒˆ์”ฉ ์–ธ๊ธ‰๋˜์—ˆ์œผ๋ฏ€๋กœ ๊ฐ๊ฐ ์ธ๋ฑ์Šค 2์™€ ์ธ๋ฑ์Šค 4์—์„œ 2์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, ๊ทธ ์™ธ์˜ ๊ฐ’์—์„œ๋Š” 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์•ŒํŒŒ๋ฒณ I๋Š” BoW๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์—์„œ ์‚ฌ๋ผ์กŒ๋Š”๋ฐ, ์ด๋Š” CountVectorizer๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ธธ์ด๊ฐ€ 2 ์ด์ƒ์ธ ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ๋งŒ ํ† ํฐ์œผ๋กœ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ •์ œ(Cleaning) ์ฑ•ํ„ฐ์—์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด, ์˜์–ด์—์„œ๋Š” ๊ธธ์ด๊ฐ€ ์งง์€ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ ๋˜ํ•œ ์ „์ฒ˜๋ฆฌ ์ž‘์—…์œผ๋กœ ๊ณ ๋ ค๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ๊ฒƒ์€ CountVectorizer๋Š” ๋‹จ์ง€ ๋„์–ด์“ฐ๊ธฐ๋งŒ์„ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด๋ฅผ ์ž๋ฅด๋Š” ๋‚ฎ์€ ์ˆ˜์ค€์˜ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  BoW๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์˜์–ด์˜ ๊ฒฝ์šฐ ๋„์–ด์“ฐ๊ธฐ๋งŒ์œผ๋กœ ํ† ํฐ ํ™”๊ฐ€ ์ˆ˜ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ ํ•œ๊ตญ์–ด์— CountVectorizer๋ฅผ ์ ์šฉํ•˜๋ฉด, ์กฐ์‚ฌ ๋“ฑ์˜ ์ด์œ ๋กœ ์ œ๋Œ€๋กœ BoW๊ฐ€ ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•ž์„œ BoW๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉํ–ˆ๋˜ '์ •๋ถ€๊ฐ€ ๋ฐœํ‘œํ•˜๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ ์†Œ๋น„์ž๊ฐ€ ๋Š๋ผ๋Š” ๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€ ๋‹ค๋ฅด๋‹ค.'๋ผ๋Š” ๋ฌธ์žฅ์„ CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ BoW๋กœ ๋งŒ๋“ค ๊ฒฝ์šฐ, CountVectorizer๋Š” '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ '์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. CountVectorizer๋Š” ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•œ ๋’ค์— '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ'์™€ '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€'์œผ๋กœ ์กฐ์‚ฌ๋ฅผ ํฌํ•จํ•ด์„œ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋กœ ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๋‹จ์–ด๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ๊ณผ'์™€ '๋ฌผ๊ฐ€ ์ƒ์Šน๋ฅ ์€'์ด ๊ฐ์ž ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์—์„œ 1์ด๋ผ๋Š” ๋นˆ๋„์˜ ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 4. ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•œ BoW ๋งŒ๋“ค๊ธฐ ์•ž์„œ ๋ถˆ์šฉ์–ด๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ณ„๋กœ ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. BoW๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ ๋ฌธ์„œ์—์„œ ๊ฐ ๋‹จ์–ด๊ฐ€ ์–ผ๋งˆ๋‚˜ ์ž์ฃผ ๋“ฑ์žฅํ–ˆ๋Š”์ง€๋ฅผ ๋ณด๊ฒ ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ๋นˆ๋„์ˆ˜๋ฅผ ์ˆ˜์น˜ํ™”ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์€ ๊ฒฐ๊ตญ ํ…์ŠคํŠธ ๋‚ด์—์„œ ์–ด๋–ค ๋‹จ์–ด๋“ค์ด ์ค‘์š”ํ•œ์ง€๋ฅผ ๋ณด๊ณ  ์‹ถ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ํ•จ์ถ•ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด BoW๋ฅผ ๋งŒ๋“ค ๋•Œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ์ผ์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์ •ํ™•๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ „์ฒ˜๋ฆฌ ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜์–ด์˜ BoW๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” CountVectorizer๋Š” ๋ถˆ์šฉ์–ด๋ฅผ ์ง€์ •ํ•˜๋ฉด, ๋ถˆ์šฉ์–ด๋Š” ์ œ์™ธํ•˜๊ณ  BoW๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. from sklearn.feature_extraction.text import CountVectorizer from nltk.corpus import stopwords (1) ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ •์˜ํ•œ ๋ถˆ์šฉ์–ด ์‚ฌ์šฉ text = ["Family is not an important thing. It's everything."] vect = CountVectorizer(stop_words=["the", "a", "an", "is", "not"]) print('bag of words vector :',vect.fit_transform(text).toarray()) print('vocabulary :',vect.vocabulary_) bag of words vector : [[1 1 1 1 1]] vocabulary : {'family': 1, 'important': 2, 'thing': 4, 'it': 3, 'everything': 0} (2) CountVectorizer์—์„œ ์ œ๊ณตํ•˜๋Š” ์ž์ฒด ๋ถˆ์šฉ์–ด ์‚ฌ์šฉ text = ["Family is not an important thing. It's everything."] vect = CountVectorizer(stop_words="english") print('bag of words vector :',vect.fit_transform(text).toarray()) print('vocabulary :',vect.vocabulary_) bag of words vector : [[1 1 1]] vocabulary : {'family': 0, 'important': 1, 'thing': 2} (3) NLTK์—์„œ ์ง€์›ํ•˜๋Š” ๋ถˆ์šฉ์–ด ์‚ฌ์šฉ text = ["Family is not an important thing. It's everything."] stop_words = stopwords.words("english") vect = CountVectorizer(stop_words=stop_words) print('bag of words vector :',vect.fit_transform(text).toarray()) print('vocabulary :',vect.vocabulary_) bag of words vector : [[1 1 1 1]] vocabulary : {'family': 1, 'important': 2, 'thing': 3, 'everything': 0} 11-03 ๋ฌธ์„œ์˜ ๋ฒกํ„ฐํ™”: ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix, DTM) ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์„œ๋“ค์˜ BoW๋“ค์„ ๊ฒฐํ•ฉํ•œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ธ ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix, DTM) ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ดํ•˜ DTM์ด๋ผ๊ณ  ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ํ–‰๊ณผ ์—ด์„ ๋ฐ˜๋Œ€๋กœ ์„ ํƒํ•˜๋ฉด TDM์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์„œ๋“ค์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 1. ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix, DTM)์˜ ํ‘œ๊ธฐ๋ฒ• ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix, DTM)์ด๋ž€ ๋‹ค์ˆ˜์˜ ๋ฌธ์„œ์—์„œ ๋“ฑ์žฅํ•˜๋Š” ๊ฐ ๋‹จ์–ด๋“ค์˜ ๋นˆ๋„๋ฅผ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ๊ฐ ๋ฌธ์„œ์— ๋Œ€ํ•œ BoW๋ฅผ ํ•˜๋‚˜์˜ ํ–‰๋ ฌ๋กœ ๋งŒ๋“  ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, BoW์™€ ๋‹ค๋ฅธ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋ผ BoW ํ‘œํ˜„์„ ๋‹ค์ˆ˜์˜ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ๋ถ€๋ฅด๋Š” ์šฉ์–ด์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์ด๋ ‡๊ฒŒ 4๊ฐœ์˜ ๋ฌธ์„œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๋ฌธ์„œ 1 : ๋จน๊ณ  ์‹ถ์€ ์‚ฌ๊ณผ ๋ฌธ์„œ 2 : ๋จน๊ณ  ์‹ถ์€ ๋ฐ”๋‚˜๋‚˜ ๋ฌธ์„œ 3 : ๊ธธ๊ณ  ๋…ธ๋ž€ ๋ฐ”๋‚˜๋‚˜ ๋ฐ”๋‚˜๋‚˜ ๋ฌธ์„œ 4 : ์ €๋Š” ๊ณผ์ผ์ด ์ข‹์•„์š” ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ณผ์ผ์ด ๊ธธ๊ณ  ๋…ธ๋ž€ ๋จน๊ณ  ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์‹ถ์€ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 0 0 0 1 0 1 1 0 0 ๋ฌธ์„œ 2 0 0 0 1 1 0 1 0 0 ๋ฌธ์„œ 3 0 1 1 0 2 0 0 0 0 ๋ฌธ์„œ 4 1 0 0 0 0 0 0 1 1 ๊ฐ ๋ฌธ์„œ์—์„œ ๋“ฑ์žฅํ•œ ๋‹จ์–ด์˜ ๋นˆ๋„๋ฅผ ํ–‰๋ ฌ์˜ ๊ฐ’์œผ๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์€ ๋ฌธ์„œ๋“ค์„ ์„œ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์น˜ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ์˜์˜๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•„์š”์— ๋”ฐ๋ผ์„œ๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋กœ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , ๋ถˆ์šฉ์–ด์— ํ•ด๋‹น๋˜๋Š” ์กฐ์‚ฌ๋“ค ๋˜ํ•œ ์ œ๊ฑฐํ•˜์—ฌ ๋” ์ •์ œ๋œ DTM์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 2. ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ(Document-Term Matrix)์˜ ํ•œ๊ณ„ DTM์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ณ  ๊ตฌํ˜„ํ•˜๊ธฐ๋„ ์‰ฝ์ง€๋งŒ, ๋ณธ์งˆ์ ์œผ๋กœ ๊ฐ€์ง€๋Š” ๋ช‡ ๊ฐ€์ง€ ํ•œ๊ณ„๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. 1) ํฌ์†Œ ํ‘œํ˜„(Sparse representation) ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋˜๊ณ  ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ’์ด 0์ด ๋˜๋Š” ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๊ณต๊ฐ„์  ๋‚ญ๋น„์™€ ๊ณ„์‚ฐ ๋ฆฌ์†Œ์Šค๋ฅผ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ๋‹จ์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. DTM๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. DTM์—์„œ์˜ ๊ฐ ํ–‰์„ ๋ฌธ์„œ ๋ฒกํ„ฐ๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ฐ ๋ฌธ์„œ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ „์ฒด ์ฝ”ํผ์Šค๊ฐ€ ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ผ๋ฉด ๋ฌธ์„œ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ ์ˆ˜๋งŒ ์ด์ƒ์˜ ์ฐจ์›์„ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋งŽ์€ ๋ฌธ์„œ ๋ฒกํ„ฐ๊ฐ€ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ’์ด 0์„ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์žฅ ์œ„์—์„œ ์˜ˆ๋กœ ๋“ค์—ˆ๋˜ ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์˜ ๋ชจ๋“  ํ–‰์ด 0์ด ์•„๋‹Œ ๊ฐ’๋ณด๋‹ค 0์˜ ๊ฐ’์ด ๋” ๋งŽ์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋‚˜ DTM๊ณผ ๊ฐ™์€ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ’์ด 0์ธ ํ‘œํ˜„์„ ํฌ์†Œ ๋ฒกํ„ฐ(sparse vector) ๋˜๋Š” ํฌ์†Œ ํ–‰๋ ฌ(sparse matrix)๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, ํฌ์†Œ ๋ฒกํ„ฐ๋Š” ๋งŽ์€ ์–‘์˜ ์ €์žฅ ๊ณต๊ฐ„๊ณผ ๋†’์€ ๊ณ„์‚ฐ ๋ณต์žก๋„๋ฅผ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š” ์ผ์€ BoW ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋ธ์—์„œ ์ค‘์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ๋‘์ , ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด, ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ์–ด๊ฐ„์ด๋‚˜ ํ‘œ์ œ์–ด ์ถ”์ถœ์„ ํ†ตํ•ด ๋‹จ์–ด๋ฅผ ์ •๊ทœํ™”ํ•˜์—ฌ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ๋‹จ์ˆœ ๋นˆ๋„ ์ˆ˜ ๊ธฐ๋ฐ˜ ์ ‘๊ทผ ์—ฌ๋Ÿฌ ๋ฌธ์„œ์— ๋“ฑ์žฅํ•˜๋Š” ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ๋นˆ๋„ ํ‘œ๊ธฐ๋ฅผ ํ•˜๋Š” ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์€ ๋•Œ๋กœ๋Š” ํ•œ๊ณ„๋ฅผ ๊ฐ€์ง€๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์˜์–ด์— ๋Œ€ํ•ด์„œ DTM์„ ๋งŒ๋“ค์—ˆ์„ ๋•Œ, ๋ถˆ์šฉ์–ด์ธ the๋Š” ์–ด๋–ค ๋ฌธ์„œ์ด๋“  ์ž์ฃผ ๋“ฑ์žฅํ•  ์ˆ˜๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œ ์‚ฌํ•œ ๋ฌธ์„œ์ธ์ง€ ๋น„๊ตํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์„œ 1, ๋ฌธ์„œ 2, ๋ฌธ์„œ 3์—์„œ ๋™์ผํ•˜๊ฒŒ the๊ฐ€ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’๋‹ค๊ณ  ํ•ด์„œ ์ด ๋ฌธ์„œ๋“ค์ด ์œ ์‚ฌํ•œ ๋ฌธ์„œ๋ผ๊ณ  ํŒ๋‹จํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ฌธ์„œ์—๋Š” ์ค‘์š”ํ•œ ๋‹จ์–ด์™€ ๋ถˆํ•„์š”ํ•œ ๋‹จ์–ด๋“ค์ด ํ˜ผ์žฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋ถˆ์šฉ์–ด(stopwords)์™€ ๊ฐ™์€ ๋‹จ์–ด๋“ค์€ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’๋”๋ผ๋„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ์žˆ์–ด ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ๋ชปํ•˜๋Š” ๋‹จ์–ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด DTM์— ๋ถˆ์šฉ์–ด์™€ ์ค‘์š”ํ•œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ๊ฐ€์ค‘์น˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ์š”? ์ด๋Ÿฌํ•œ ์•„์ด๋””์–ด๋ฅผ ์ ์šฉํ•œ TF-IDF๋ฅผ ์ด์–ด์„œ ํ•™์Šตํ•ด ๋ด…์‹œ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DTM์„ ๋งŒ๋“œ๋Š” ์‹ค์Šต ๋˜ํ•œ TF-IDF๋ฅผ ์„ค๋ช…ํ•˜๋ฉด์„œ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 11-04 TF-IDF(Term Frequency-Inverse Document Frequency) ์ด๋ฒˆ์—๋Š” DTM ๋‚ด์— ์žˆ๋Š” ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ ์ค‘์š”๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” TF-IDF ๊ฐ€์ค‘์น˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. TF-IDF๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ธฐ์กด์˜ DTM์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ณด๋‹ค ๋งŽ์€ ์ •๋ณด๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๋ฌธ์„œ๋“ค์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. TF-IDF๊ฐ€ DTM๋ณด๋‹ค ํ•ญ์ƒ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์žฅํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ๋งŽ์€ ๊ฒฝ์šฐ์—์„œ DTM๋ณด๋‹ค ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. TF-IDF(๋‹จ์–ด ๋นˆ๋„-์—ญ ๋ฌธ์„œ ๋นˆ๋„, Term Frequency-Inverse Document Frequency) TF-IDF(Term Frequency-Inverse Document Frequency)๋Š” ๋‹จ์–ด์˜ ๋นˆ๋„์™€ ์—ญ ๋ฌธ์„œ ๋นˆ๋„(๋ฌธ์„œ์˜ ๋นˆ๋„์— ํŠน์ • ์‹์„ ์ทจํ•จ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DTM ๋‚ด์˜ ๊ฐ ๋‹จ์–ด๋“ค๋งˆ๋‹ค ์ค‘์š”ํ•œ ์ •๋„๋ฅผ ๊ฐ€์ค‘์น˜๋กœ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์šฐ์„  DTM์„ ๋งŒ๋“  ํ›„, TF-IDF ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. TF-IDF๋Š” ์ฃผ๋กœ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ์ž‘์—…, ๊ฒ€์ƒ‰ ์‹œ์Šคํ…œ์—์„œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์˜ ์ค‘์š”๋„๋ฅผ ์ •ํ•˜๋Š” ์ž‘์—…, ๋ฌธ์„œ ๋‚ด์—์„œ ํŠน์ • ๋‹จ์–ด์˜ ์ค‘์š”๋„๋ฅผ ๊ตฌํ•˜๋Š” ์ž‘์—… ๋“ฑ์— ์“ฐ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. TF-IDF๋Š” TF์™€ IDF๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ์˜๋ฏธํ•˜๋Š” ๋ฐ ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ๋ฅผ d, ๋‹จ์–ด๋ฅผ t, ๋ฌธ์„œ์˜ ์ด๊ฐœ์ˆ˜๋ฅผ n์ด๋ผ๊ณ  ํ‘œํ˜„ํ•  ๋•Œ TF, DF, IDF๋Š” ๊ฐ๊ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (1) tf(d, t) : ํŠน์ • ๋ฌธ์„œ d์—์„œ์˜ ํŠน์ • ๋‹จ์–ด t์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜. ์ƒ์†Œํ•œ ๊ธ€์ž ๋•Œ๋ฌธ์— ์–ด๋ ค์›Œ ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ž˜ ์ƒ๊ฐํ•ด ๋ณด๋ฉด TF๋Š” ์ด๋ฏธ ์•ž์—์„œ ๊ตฌํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. TF๋Š” ์•ž์—์„œ ๋ฐฐ์šด DTM์˜ ์˜ˆ์ œ์—์„œ ๊ฐ ๋‹จ์–ด๋“ค์ด ๊ฐ€์ง„ ๊ฐ’๋“ค์ž…๋‹ˆ๋‹ค. DTM์ด ๊ฐ ๋ฌธ์„œ์—์„œ์˜ ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. (2) df(t) : ํŠน์ • ๋‹จ์–ด t๊ฐ€ ๋“ฑ์žฅํ•œ ๋ฌธ์„œ์˜ ์ˆ˜. ์—ฌ๊ธฐ์„œ ํŠน์ • ๋‹จ์–ด๊ฐ€ ๊ฐ ๋ฌธ์„œ, ๋˜๋Š” ๋ฌธ์„œ๋“ค์—์„œ ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ–ˆ๋Š”์ง€๋Š” ๊ด€์‹ฌ ๊ฐ€์ง€์ง€ ์•Š์œผ๋ฉฐ ์˜ค์ง ํŠน์ • ๋‹จ์–ด t๊ฐ€ ๋“ฑ์žฅํ•œ ๋ฌธ์„œ์˜ ์ˆ˜์—๋งŒ ๊ด€์‹ฌ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด DTM์—์„œ ๋ฐ”๋‚˜๋‚˜๋Š” ๋ฌธ์„œ 2์™€ ๋ฌธ์„œ 3์—์„œ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋ฐ”๋‚˜๋‚˜์˜ df๋Š” 2์ž…๋‹ˆ๋‹ค. ๋ฌธ์„œ 3์—์„œ ๋ฐ”๋‚˜๋‚˜๊ฐ€ ๋‘ ๋ฒˆ ๋“ฑ์žฅํ–ˆ์ง€๋งŒ, ๊ทธ๊ฒƒ์€ ์ค‘์š”ํ•œ ๊ฒŒ ์•„๋‹™๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ๋ฐ”๋‚˜๋‚˜๋ž€ ๋‹จ์–ด๊ฐ€ ๋ฌธ์„œ 2์—์„œ 100๋ฒˆ ๋“ฑ์žฅํ–ˆ๊ณ , ๋ฌธ์„œ 3์—์„œ 200๋ฒˆ ๋“ฑ์žฅํ–ˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๋ฐ”๋‚˜๋‚˜์˜ df๋Š” 2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. (3) idf(t) : df(t)์— ๋ฐ˜๋น„๋ก€ํ•˜๋Š” ์ˆ˜. d ( ) l g ( 1 d ( ) ) IDF๋ผ๋Š” ์ด๋ฆ„์„ ๋ณด๊ณ  DF์˜ ์—ญ์ˆ˜๊ฐ€ ์•„๋‹๊นŒ ์ƒ๊ฐํ–ˆ๋‹ค๋ฉด, IDF๋Š” DF์˜ ์—ญ์ˆ˜๋ฅผ ์ทจํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์ด ๋งž์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ log์™€ ๋ถ„๋ชจ์— 1์„ ๋”ํ•ด์ฃผ๋Š” ์‹์— ์˜์•„ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. log๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์„ ๋•Œ, IDF๋ฅผ DF์˜ ์—ญ์ˆ˜( d ( ) ๋ผ๋Š” ์‹)๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ด ๋ฌธ์„œ์˜ ์ˆ˜ n์ด ์ปค์งˆ์ˆ˜๋ก, IDF์˜ ๊ฐ’์€ ๊ธฐํ•˜๊ธ‰์ˆ˜์ ์œผ๋กœ ์ปค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— log๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์™œ log๊ฐ€ ํ•„์š”ํ•œ์ง€ n=1,000,000์ผ ๋•Œ์˜ ์˜ˆ๋ฅผ ๋“ค์–ด๋ด…์‹œ๋‹ค. log์˜ ๋ฐ‘์€ 10์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์˜€์„ ๋•Œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. d ( ) l g ( / f ( ) ) = , 000 000 ๋‹จ์–ด d ( ) d ( , ) word1 1 6 word2 100 4 word3 1,000 3 word4 10,000 2 word5 100,000 1 word6 1,000,000 0 ๊ทธ๋ ‡๋‹ค๋ฉด log๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด idf์˜ ๊ฐ’์ด ์–ด๋–ป๊ฒŒ ์ปค์ง€๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. d ( ) n d ( ) = , 000 000 ๋‹จ์–ด d ( ) d ( ) word1 1 1,000,000 word2 100 10,000 word3 1,000 1,000 word4 10,000 100 word5 100,000 10 word6 1,000,000 1 ๋˜ ๋‹ค๋ฅธ ์ง๊ด€์ ์ธ ์„ค๋ช…์€ ๋ถˆ์šฉ์–ด ๋“ฑ๊ณผ ๊ฐ™์ด ์ž์ฃผ ์“ฐ์ด๋Š” ๋‹จ์–ด๋“ค์€ ๋น„๊ต์  ์ž์ฃผ ์“ฐ์ด์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค๋ณด๋‹ค ์ตœ์†Œ ์ˆ˜์‹ญ ๋ฐฐ ์ž์ฃผ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋น„๊ต์  ์ž์ฃผ ์“ฐ์ด์ง€ ์•Š๋Š” ๋‹จ์–ด๋“ค์กฐ์ฐจ ํฌ๊ท€ ๋‹จ์–ด๋“ค๊ณผ ๋น„๊ตํ•˜๋ฉด ๋˜ ์ตœ์†Œ ์ˆ˜๋ฐฑ ๋ฐฐ๋Š” ๋” ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. ์ด ๋•Œ๋ฌธ์— log๋ฅผ ์”Œ์›Œ์ฃผ์ง€ ์•Š์œผ๋ฉด, ํฌ๊ท€ ๋‹จ์–ด๋“ค์— ์—„์ฒญ๋‚œ ๊ฐ€์ค‘์น˜๊ฐ€ ๋ถ€์—ฌ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ๊ทธ๋ฅผ ์”Œ์šฐ๋ฉด ์ด๋Ÿฐ ๊ฒฉ์ฐจ๋ฅผ ์ค„์ด๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. log ์•ˆ์˜ ์‹์—์„œ ๋ถ„๋ชจ์— 1์„ ๋”ํ•ด์ฃผ๋Š” ์ด์œ ๋Š” ์ฒซ ๋ฒˆ์งธ ์ด์œ ๋กœ๋Š” ํŠน์ • ๋‹จ์–ด๊ฐ€ ์ „์ฒด ๋ฌธ์„œ์—์„œ ๋“ฑ์žฅํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์— ๋ถ„๋ชจ๊ฐ€ 0์ด ๋˜๋Š” ์ƒํ™ฉ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. TF-IDF๋Š” ๋ชจ๋“  ๋ฌธ์„œ์—์„œ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋Š” ์ค‘์š”๋„๊ฐ€ ๋‚ฎ๋‹ค๊ณ  ํŒ๋‹จํ•˜๋ฉฐ, ํŠน์ • ๋ฌธ์„œ์—์„œ๋งŒ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋Š” ์ค‘์š”๋„๊ฐ€ ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. TF-IDF ๊ฐ’์ด ๋‚ฎ์œผ๋ฉด ์ค‘์š”๋„๊ฐ€ ๋‚ฎ์€ ๊ฒƒ์ด๋ฉฐ, TF-IDF ๊ฐ’์ด ํฌ๋ฉด ์ค‘์š”๋„๊ฐ€ ํฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, the๋‚˜ a์™€ ๊ฐ™์ด ๋ถˆ์šฉ์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋ชจ๋“  ๋ฌธ์„œ์— ์ž์ฃผ ๋“ฑ์žฅํ•˜๊ธฐ ๋งˆ๋ จ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ถˆ์šฉ์–ด์˜ TF-IDF์˜ ๊ฐ’์€ ๋‹ค๋ฅธ ๋‹จ์–ด์˜ TF-IDF์— ๋น„ํ•ด์„œ ๋‚ฎ์•„์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ณผ์ผ์ด ๊ธธ๊ณ  ๋…ธ๋ž€ ๋จน๊ณ  ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์‹ถ์€ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 0 0 0 1 0 1 1 0 0 ๋ฌธ์„œ 2 0 0 0 1 1 0 1 0 0 ๋ฌธ์„œ 3 0 1 1 0 2 0 0 0 0 ๋ฌธ์„œ 4 1 0 0 0 0 0 0 1 1 ์•ž์„œ DTM์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ๋“ค์—ˆ๋˜ ์œ„์˜ ์˜ˆ์ œ๋ฅผ ๊ฐ€์ง€๊ณ  TF-IDF์— ๋Œ€ํ•ด ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  TF๋Š” ์•ž์„œ ์‚ฌ์šฉํ•œ DTM์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด, ๊ทธ๊ฒƒ์ด ๊ฐ ๋ฌธ์„œ์—์„œ์˜ ๊ฐ ๋‹จ์–ด์˜ TF๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ ๊ตฌํ•ด์•ผ ํ•  ๊ฒƒ์€ TF์™€ ๊ณฑํ•ด์•ผ ํ•  ๊ฐ’์ธ IDF์ž…๋‹ˆ๋‹ค. ๋กœ๊ทธ๋Š” ์ž์—ฐ ๋กœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž์—ฐ ๋กœ๊ทธ๋Š” ๋กœ๊ทธ์˜ ๋ฐ‘์„ ์ž์—ฐ ์ƒ์ˆ˜ e(e=2.718281...)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋กœ๊ทธ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. IDF ๊ณ„์‚ฐ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋กœ๊ทธ์˜ ๋ฐ‘์€ TF-IDF๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ž„์˜๋กœ ์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ๋กœ๊ทธ๋Š” ๋งˆ์น˜ ๊ธฐ์กด์˜ ๊ฐ’์— ๊ณฑํ•˜์—ฌ ๊ฐ’์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ ˆํ•˜๋Š” ์ƒ์ˆ˜์˜ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ์ข… ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ํŒจํ‚ค์ง€๋กœ ์ง€์›ํ•˜๋Š” TF-IDF์˜ ๋กœ๊ทธ๋Š” ๋Œ€๋ถ€๋ถ„ ์ž์—ฐ ๋กœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ์ž์—ฐ ๋กœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž์—ฐ ๋กœ๊ทธ๋Š” ๋ณดํ†ต log๋ผ๊ณ  ํ‘œํ˜„ํ•˜์ง€ ์•Š๊ณ , ln์ด๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด IDF(์—ญ ๋ฌธ์„œ ๋นˆ๋„) ๊ณผ์ผ์ด ln(4/(1+1)) = 0.693147 ๊ธธ๊ณ  ln(4/(1+1)) = 0.693147 ๋…ธ๋ž€ ln(4/(1+1)) = 0.693147 ๋จน๊ณ  ln(4/(2+1)) = 0.287682 ๋ฐ”๋‚˜๋‚˜ ln(4/(2+1)) = 0.287682 ์‚ฌ๊ณผ ln(4/(1+1)) = 0.693147 ์‹ถ์€ ln(4/(2+1)) = 0.287682 ์ €๋Š” ln(4/(1+1)) = 0.693147 ์ข‹์•„์š” ln(4/(1+1)) = 0.693147 ๋ฌธ์„œ์˜ ์ด ์ˆ˜๋Š” 4์ด๊ธฐ ๋•Œ๋ฌธ์— ln ์•ˆ์—์„œ ๋ถ„์ž๋Š” ๋Š˜ 4๋กœ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋ถ„๋ชจ์˜ ๊ฒฝ์šฐ์—๋Š” ๊ฐ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ๋ฌธ์„œ์˜ ์ˆ˜(DF)๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด์„œ '๋จน๊ณ '์˜ ๊ฒฝ์šฐ์—๋Š” ์ด 2๊ฐœ์˜ ๋ฌธ์„œ(๋ฌธ์„œ 1, ๋ฌธ์„œ 2)์— ๋“ฑ์žฅํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— 2๋ผ๋Š” ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ IDF์˜ ๊ฐ’์„ ๋น„๊ตํ•ด ๋ณด๋ฉด ๋ฌธ์„œ 1๊ฐœ์—๋งŒ ๋“ฑ์žฅํ•œ ๋‹จ์–ด์™€ ๋ฌธ์„œ 2๊ฐœ์—๋งŒ ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋Š” ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. IDF๋Š” ์—ฌ๋Ÿฌ ๋ฌธ์„œ์—์„œ ๋“ฑ์žฅํ•œ ๋‹จ์–ด์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋‚ฎ์ถ”๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. TF-IDF๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์˜ TF๋Š” DTM์—์„œ์˜ ๊ฐ ๋‹จ์–ด์˜ ๊ฐ’๊ณผ ๊ฐ™์œผ๋ฏ€๋กœ, ์•ž์„œ ์‚ฌ์šฉํ•œ DTM์—์„œ ๋‹จ์–ด ๋ณ„๋กœ ์œ„์˜ IDF ๊ฐ’์„ ๊ณฑํ•ด์ฃผ๋ฉด TF-IDF ๊ฐ’์„ ์–ป์Šต๋‹ˆ๋‹ค. ๊ณผ์ผ์ด ๊ธธ๊ณ  ๋…ธ๋ž€ ๋จน๊ณ  ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์‹ถ์€ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 0 0 0 0.287682 0 0.693147 0.287682 0 0 ๋ฌธ์„œ 2 0 0 0 0.287682 0.287682 0 0.287682 0 0 ๋ฌธ์„œ 3 0 0.693147 0.693147 0 0.575364 0 0 0 0 ๋ฌธ์„œ 4 0.693147 0 0 0 0 0 0 0.693147 0.693147 ์‚ฌ์‹ค ์˜ˆ์ œ ๋ฌธ์„œ๊ฐ€ ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณ„์‚ฐ์€ ๋งค์šฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 3์—์„œ์˜ ๋ฐ”๋‚˜๋‚˜๋งŒ TF ๊ฐ’์ด 2์ด๋ฏ€๋กœ IDF์— 2๋ฅผ ๊ณฑํ•ด์ฃผ๊ณ , ๋‚˜๋จธ์ง„ TF ๊ฐ’์ด 1์ด๋ฏ€๋กœ ๊ทธ๋Œ€๋กœ IDF ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ 2์—์„œ์˜ ๋ฐ”๋‚˜๋‚˜์˜ TF-IDF ๊ฐ€์ค‘์น˜์™€ ๋ฌธ์„œ 3์—์„œ์˜ ๋ฐ”๋‚˜๋‚˜์˜ TF-IDF ๊ฐ€์ค‘์น˜๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜์‹์ ์œผ๋กœ ๋งํ•˜๋ฉด, TF๊ฐ€ ๊ฐ๊ฐ 1๊ณผ 2๋กœ ๋‹ฌ๋ž๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ TF-IDF์—์„œ์˜ ๊ด€์ ์—์„œ ๋ณด์ž๋ฉด TF-IDF๋Š” ํŠน์ • ๋ฌธ์„œ์—์„œ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋Š” ๊ทธ ๋ฌธ์„œ ๋‚ด์—์„œ ์ค‘์š”ํ•œ ๋‹จ์–ด๋กœ ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฌธ์„œ 2์—์„œ๋Š” ๋ฐ”๋‚˜๋‚˜๋ฅผ ํ•œ ๋ฒˆ ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ, ๋ฌธ์„œ 3์—์„œ๋Š” ๋ฐ”๋‚˜๋‚˜๋ฅผ ๋‘ ๋ฒˆ ์–ธ๊ธ‰ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์„œ 3์—์„œ์˜ ๋ฐ”๋‚˜๋‚˜๋ฅผ ๋”์šฑ ์ค‘์š”ํ•œ ๋‹จ์–ด๋ผ๊ณ  ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 2. ํŒŒ์ด์ฌ์œผ๋กœ TF-IDF ์ง์ ‘ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„์˜ ๊ณ„์‚ฐ ๊ณผ์ •์„ ํŒŒ์ด์ฌ์œผ๋กœ ์ง์ ‘ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์˜ ์„ค๋ช…์—์„œ ์‚ฌ์šฉํ•œ 4๊ฐœ์˜ ๋ฌธ์„œ๋ฅผ docs์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. import pandas as pd # ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์‚ฌ์šฉ์„ ์œ„ํ•ด from math import log # IDF ๊ณ„์‚ฐ์„ ์œ„ํ•ด docs = [ '๋จน๊ณ  ์‹ถ์€ ์‚ฌ๊ณผ', '๋จน๊ณ  ์‹ถ์€ ๋ฐ”๋‚˜๋‚˜', '๊ธธ๊ณ  ๋…ธ๋ž€ ๋ฐ”๋‚˜๋‚˜ ๋ฐ”๋‚˜๋‚˜', '์ €๋Š” ๊ณผ์ผ์ด ์ข‹์•„์š”' ] vocab = list(set(w for doc in docs for w in doc.split())) vocab.sort() TF, IDF, ๊ทธ๋ฆฌ๊ณ  TF-IDF ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. # ์ด ๋ฌธ์„œ์˜ ์ˆ˜ N = len(docs) def tf(t, d): return d.count(t) def idf(t): df = 0 for doc in docs: df += t in doc return log(N/(df+1)) def tfidf(t, d): return tf(t, d)* idf(t) TF๋ฅผ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด DTM์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•˜์—ฌ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. result = [] # ๊ฐ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ์•„๋ž˜ ์—ฐ์‚ฐ์„ ๋ฐ˜๋ณต for i in range(N): result.append([]) d = docs[i] for j in range(len(vocab)): t = vocab[j] result[-1].append(tf(t, d)) tf_ = pd.DataFrame(result, columns = vocab) ์ •์ƒ์ ์œผ๋กœ DTM์ด ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•œ IDF ๊ฐ’์„ ๊ตฌํ•ด๋ด…์‹œ๋‹ค. result = [] for j in range(len(vocab)): t = vocab[j] result.append(idf(t)) idf_ = pd.DataFrame(result, index=vocab, columns=["IDF"]) idf_ ์œ„์—์„œ ์ˆ˜๊ธฐ๋กœ ๊ตฌํ•œ IDF ๊ฐ’๋“ค๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. TF-IDF ํ–‰๋ ฌ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. result = [] for i in range(N): result.append([]) d = docs[i] for j in range(len(vocab)): t = vocab[j] result[-1].append(tfidf(t, d)) tfidf_ = pd.DataFrame(result, columns = vocab) tfidf_ TF-IDF์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์‹์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•˜๊ณ  ์‹ค์ œ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์‹ค์ œ TF-IDF ๊ตฌํ˜„์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š” ๋งŽ์€ ๋จธ์‹  ๋Ÿฌ๋‹ ํŒจํ‚ค์ง€๋“ค์€ ํŒจํ‚ค์ง€๋งˆ๋‹ค ์‹์ด ์กฐ๊ธˆ์”ฉ ์ƒ์ดํ•˜์ง€๋งŒ, ์œ„์—์„œ ๋ฐฐ์šด ์‹๊ณผ๋Š” ๋‹ค๋ฅธ ์กฐ์ •๋œ ์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์œ„์˜ ๊ธฐ๋ณธ์ ์ธ ์‹์„ ๋ฐ”ํƒ•์œผ๋กœ ํ•œ ๊ตฌํ˜„์—๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜ ์ด 4์ธ๋ฐ, f ( ) ์˜ ๊ฐ’์ด 3์ธ ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ค ์ผ์ด ๋ฒŒ์–ด์งˆ๊นŒ์š”? f ( ) ์— 1์ด ๋”ํ•ด์ง€๋ฉด์„œ log ํ•ญ์˜ ๋ถ„์ž์™€ ๋ถ„๋ชจ์˜ ๊ฐ’์ด ๊ฐ™์•„์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” o์˜ ์ง„์ˆ˜ ๊ฐ’์ด 1์ด ๋˜๋ฉด์„œ d ( , ) ์˜ ๊ฐ’์ด 0์ด ๋จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด d ( , ) l g ( / ( f ( ) 1 ) ) 0 ์ž…๋‹ˆ๋‹ค. IDF์˜ ๊ฐ’์ด 0์ด๋ผ๋ฉด ๋” ์ด์ƒ ๊ฐ€์ค‘์น˜์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์‹ค์Šตํ•  ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ TF-IDF ๊ตฌํ˜„์ฒด ๋˜ํ•œ ์œ„์˜ ์‹์—์„œ ์กฐ์ •๋œ ์‹์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 3. ์‚ฌ์ดํ‚ท๋Ÿฐ์„ ์ด์šฉํ•œ DTM๊ณผ TF-IDF ์‹ค์Šต ์‚ฌ์ดํ‚ท๋Ÿฐ์„ ํ†ตํ•ด DTM๊ณผ TF-IDF๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. BoW๋ฅผ ์„ค๋ช…ํ•˜๋ฉฐ ๋ฐฐ์šด CountVectorizer๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด DTM์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. from sklearn.feature_extraction.text import CountVectorizer corpus = [ 'you know I want your love', 'I like you', 'what should I do ', ] vector = CountVectorizer() # ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ๋ก print(vector.fit_transform(corpus).toarray()) # ๊ฐ ๋‹จ์–ด์™€ ๋งคํ•‘๋œ ์ธ๋ฑ์Šค ์ถœ๋ ฅ print(vector.vocabulary_) [[0 1 0 1 0 1 0 1 1] [0 0 1 0 0 0 0 1 0] [1 0 0 0 1 0 1 0 0]] {'you': 7, 'know': 1, 'want': 5, 'your': 8, 'love': 3, 'like': 2, 'what': 6, 'should': 4, 'do': 0} DTM์ด ์™„์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. DTM์—์„œ ๊ฐ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ถ€์—ฌ๋˜์—ˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด, ์ธ๋ฑ์Šค๋ฅผ ํ™•์ธํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์—ด์˜ ๊ฒฝ์šฐ์—๋Š” 0์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ do์ž…๋‹ˆ๋‹ค. do๋Š” ์„ธ ๋ฒˆ์งธ ๋ฌธ์„œ์—๋งŒ ๋“ฑ์žฅํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์„ธ ๋ฒˆ์งธ ํ–‰์—์„œ๋งŒ 1์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์—ด์˜ ๊ฒฝ์šฐ์—๋Š” 1์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ know์ž…๋‹ˆ๋‹ค. know๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ์—๋งŒ ๋“ฑ์žฅํ–ˆ์œผ๋ฏ€๋กœ ์ฒซ ๋ฒˆ์งธ ํ–‰์—์„œ๋งŒ 1์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์€ TF-IDF๋ฅผ ์ž๋™ ๊ณ„์‚ฐํ•ด ์ฃผ๋Š” TfidfVectorizer๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ TF-IDF๋Š” ์œ„์—์„œ ๋ฐฐ์› ๋˜ ๋ณดํŽธ์ ์ธ TF-IDF ๊ธฐ๋ณธ ์‹์—์„œ ์กฐ์ •๋œ ์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜์ž๋ฉด, IDF์˜ ๋กœ๊ทธ ํ•ญ์˜ ๋ถ„์ž์— 1์„ ๋”ํ•ด์ฃผ๋ฉฐ, ๋กœ๊ทธํ•ญ์— 1์„ ๋”ํ•ด์ฃผ๊ณ , TF-IDF์— L2 ์ •๊ทœํ™”๋ผ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ’์„ ์กฐ์ •ํ•˜๋Š” ๋“ฑ์˜ ์ฐจ์ด๋กœ TF-IDF๊ฐ€ ๊ฐ€์ง„ ์˜๋„๋Š” ์—ฌ์ „ํžˆ ๊ทธ๋Œ€๋กœ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. from sklearn.feature_extraction.text import TfidfVectorizer corpus = [ 'you know I want your love', 'I like you', 'what should I do ', ] tfidfv = TfidfVectorizer().fit(corpus) print(tfidfv.transform(corpus).toarray()) print(tfidfv.vocabulary_) [[0. 0.46735098 0. 0.46735098 0. 0.46735098 0. 0.35543247 0.46735098] [0. 0. 0.79596054 0. 0. 0. 0. 0.60534851 0. ] [0.57735027 0. 0. 0. 0.57735027 0. 0.57735027 0. 0. ]] {'you': 7, 'know': 1, 'want': 5, 'your': 8, 'love': 3, 'like': 2, 'what': 6, 'should': 4, 'do': 0} BoW, DTM, TF-IDF์— ๋Œ€ํ•ด์„œ ์ „๋ถ€ ํ•™์Šตํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ๋“ค ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์žฌ๋ฃŒ ์†์งˆํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šด ์…ˆ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ด๋ฅผ ์ด์šฉํ•œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ดํ‚ท๋Ÿฐ์˜ TF-IDF์˜ ์ˆ˜์‹์„ ์ดํ•ดํ•˜๊ณ  ์‹ถ์€ ๋ถ„๋“ค์„ ์œ„ํ•ด์„œ ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์— ๋Œ“๊ธ€๋กœ ์„ค๋ช…ํ•ด๋†จ์Šต๋‹ˆ๋‹ค. ๊ถ๊ธˆํ•˜์‹  ๋ถ„๋“ค์€ ์ฐธ๊ณ ํ•˜์„ธ์š”. 11-05 ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ์ด์šฉํ•œ ์ถ”์ฒœ ์‹œ์Šคํ…œ BoW์— ๊ธฐ๋ฐ˜ํ•œ ๋‹จ์–ด ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ธ DTM, TF-IDF, ๋˜๋Š” ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋  Word2Vec ๋“ฑ๊ณผ ๊ฐ™์ด ๋‹จ์–ด๋ฅผ ์ˆ˜์น˜ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ–ˆ๋‹ค๋ฉด ์ด๋Ÿฌํ•œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 1. ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„(Cosine Similarity) ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋Š” ๋‘ ๋ฒกํ„ฐ ๊ฐ„์˜ ์ฝ”์‚ฌ์ธ ๊ฐ๋„๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ์œ ์‚ฌ๋„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒกํ„ฐ์˜ ๋ฐฉํ–ฅ์ด ์™„์ „ํžˆ ๋™์ผํ•œ ๊ฒฝ์šฐ์—๋Š” 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, 90ยฐ์˜ ๊ฐ์„ ์ด๋ฃจ๋ฉด 0, 180ยฐ๋กœ ๋ฐ˜๋Œ€์˜ ๋ฐฉํ–ฅ์„ ๊ฐ€์ง€๋ฉด -1์˜ ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๊ฒฐ๊ตญ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋Š” -1 ์ด์ƒ 1 ์ดํ•˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ๊ฐ’์ด 1์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ์œ ์‚ฌ๋„๊ฐ€ ๋†’๋‹ค๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•˜๋ฉด ๋‘ ๋ฒกํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐฉํ–ฅ์ด ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ๊ฐ€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒกํ„ฐ A, B์— ๋Œ€ํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋Š” ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. i i a i y c s ( ) A B | | | B | โˆ‘ = n i B โˆ‘ = n ( i ) ร— i 1 ( ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์ด๋‚˜ TF-IDF ํ–‰๋ ฌ์„ ํ†ตํ•ด์„œ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์ด๋‚˜ TF-IDF ํ–‰๋ ฌ์ด ๊ฐ๊ฐ์˜ ํŠน์ง• ๋ฒกํ„ฐ A, B๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์— ๋Œ€ํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ด๋ด…์‹œ๋‹ค. ๋ฌธ์„œ 1 : ์ €๋Š” ์‚ฌ๊ณผ ์ข‹์•„์š” ๋ฌธ์„œ 2 : ์ €๋Š” ๋ฐ”๋‚˜๋‚˜ ์ข‹์•„์š” ๋ฌธ์„œ 3 : ์ €๋Š” ๋ฐ”๋‚˜๋‚˜ ์ข‹์•„์š” ์ €๋Š” ๋ฐ”๋‚˜๋‚˜ ์ข‹์•„์š” ๋„์–ด์“ฐ๊ธฐ ๊ธฐ์ค€ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ์œ„์˜ ์„ธ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ๋ฌธ์„œ ๋‹จ์–ด ํ–‰๋ ฌ์„ ๋งŒ๋“ค๋ฉด ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 0 1 1 1 ๋ฌธ์„œ 2 1 0 1 1 ๋ฌธ์„œ 3 2 0 2 2 Numpy๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ๊ฐ ๋ฌธ์„œ ๋ฒกํ„ฐ ๊ฐ„์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import numpy as np from numpy import dot from numpy.linalg import norm def cos_sim(A, B): return dot(A, B)/(norm(A)*norm(B)) doc1 = np.array([0,1,1,1]) doc2 = np.array([1,0,1,1]) doc3 = np.array([2,0,2,2]) print('๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ์œ ์‚ฌ๋„ :',cos_sim(doc1, doc2)) print('๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 3์˜ ์œ ์‚ฌ๋„ :',cos_sim(doc1, doc3)) print('๋ฌธ์„œ 2์™€ ๋ฌธ์„œ 3์˜ ์œ ์‚ฌ๋„ :',cos_sim(doc2, doc3)) ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ์œ ์‚ฌ๋„ : 0.67 ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 3์˜ ์œ ์‚ฌ๋„ : 0.67 ๋ฌธ์„œ 2๊ณผ ๋ฌธ์„œ 3์˜ ์œ ์‚ฌ๋„ : 1.00 ๋ˆˆ์—ฌ๊ฒจ๋ณผ ๋งŒํ•œ ์ ์€ ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„์™€ ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 3์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๊ฐ€ ๊ฐ™๋‹ค๋Š” ์ ๊ณผ ๋ฌธ์„œ 2์™€ ๋ฌธ์„œ 3์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๊ฐ€ 1์ด ๋‚˜์˜จ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•ž์„œ 1์€ ๋‘ ๋ฒกํ„ฐ์˜ ๋ฐฉํ–ฅ์ด ์™„์ „ํžˆ ๋™์ผํ•œ ๊ฒฝ์šฐ์— 1์ด ๋‚˜์˜ค๋ฉฐ, ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๊ด€์ ์—์„œ๋Š” ์œ ์‚ฌ๋„์˜ ๊ฐ’์ด ์ตœ๋Œ€์ž„์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 3์€ ๋ฌธ์„œ 2์—์„œ ๋‹จ์ง€ ๋ชจ๋“  ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๊ฐ€ 1์”ฉ ์ฆ๊ฐ€ํ–ˆ์„ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ•œ ๋ฌธ์„œ ๋‚ด์˜ ๋ชจ๋“  ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๊ฐ€ ๋™์ผํ•˜๊ฒŒ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ธฐ์กด์˜ ๋ฌธ์„œ์™€ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„์˜ ๊ฐ’์ด 1์ด๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์‹œ์‚ฌํ•˜๋Š” ์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ A์™€ B๊ฐ€ ๋™์ผํ•œ ์ฃผ์ œ์˜ ๋ฌธ์„œ. ๋ฌธ์„œ C๋Š” ๋‹ค๋ฅธ ์ฃผ์ œ์˜ ๋ฌธ์„œ๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์„œ A์™€ ๋ฌธ์„œ C์˜ ๋ฌธ์„œ์˜ ๊ธธ์ด๋Š” ๊ฑฐ์˜ ์ฐจ์ด๊ฐ€ ๋‚˜์ง€ ์•Š์ง€๋งŒ, ๋ฌธ์„œ B์˜ ๊ฒฝ์šฐ ๋ฌธ์„œ A์˜ ๊ธธ์ด๋ณด๋‹ค ๋‘ ๋ฐฐ์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๋กœ ์œ ์‚ฌ๋„๋ฅผ ์—ฐ์‚ฐํ•˜๋ฉด ๋ฌธ์„œ A๊ฐ€ ๋ฌธ์„œ B๋ณด๋‹ค ๋ฌธ์„œ C์™€ ์œ ์‚ฌ๋„๊ฐ€ ๋” ๋†’๊ฒŒ ๋‚˜์˜ค๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์œ ์‚ฌ๋„ ์—ฐ์‚ฐ์— ๋ฌธ์„œ์˜ ๊ธธ์ด๊ฐ€ ์˜ํ–ฅ์„ ๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ, ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๊ฐ€ ํ•ด๊ฒฐ์ฑ…์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋Š” ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ๋•Œ ๋ฒกํ„ฐ์˜ ๋ฐฉํ–ฅ(ํŒจํ„ด)์— ์ดˆ์ ์„ ๋‘๋ฏ€๋กœ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋Š” ๋ฌธ์„œ์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ์ƒํ™ฉ์—์„œ ๋น„๊ต์  ๊ณต์ •ํ•œ ๋น„๊ต๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค. 2. ์œ ์‚ฌ๋„๋ฅผ ์ด์šฉํ•œ ์ถ”์ฒœ ์‹œ์Šคํ…œ ๊ตฌํ˜„ํ•˜๊ธฐ ์บ๊ธ€์—์„œ ์‚ฌ์šฉ๋˜์—ˆ๋˜ ์˜ํ™” ๋ฐ์ดํ„ฐ ์…‹์„ ๊ฐ€์ง€๊ณ  ์˜ํ™” ์ถ”์ฒœ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. TF-IDF์™€ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋งŒ์œผ๋กœ ์˜ํ™”์˜ ์ค„๊ฑฐ๋ฆฌ์— ๊ธฐ๋ฐ˜ํ•ด์„œ ์˜ํ™”๋ฅผ ์ถ”์ฒœํ•˜๋Š” ์ถ”์ฒœ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://www.kaggle.com/rounakbanik/the-movies-dataset ์›๋ณธ ํŒŒ์ผ์€ ์œ„ ๋งํฌ์—์„œ movies_metadata.csv ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ์ด 24๊ฐœ์˜ ์—ด์„ ๊ฐ€์ง„ 45,466๊ฐœ์˜ ์ƒ˜ํ”Œ๋กœ ๊ตฌ์„ฑ๋œ ์˜ํ™” ์ •๋ณด ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. import pandas as pd from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import cosine_similarity data = pd.read_csv('movies_metadata.csv', low_memory=False) data.head(2) ๋‹ค์šด๋กœ๋“œํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ์ƒ์œ„ 2๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜<NAME>์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ... original_title overview ... title video vote_average vote_count 0 ... Toy Story Led by Woody, Andy's toys live happily in his ... ์ค‘๋žต ... ... Toy Story False 7.7 5415.0 1 ... Jumanji When siblings Judy and Peter discover an encha ... ์ค‘๋žต ... ... Jumanji False 6.9 2413.0 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ์ด 24๊ฐœ์˜ ์—ด์„ ๊ฐ–๊ณ  ์žˆ์œผ๋‚˜ ์ฑ…์˜ ์ง€๋ฉด์˜ ํ•œ๊ณ„๋กœ ์ผ๋ถ€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„์— ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ์˜ํ™” ์ œ๋ชฉ์— ํ•ด๋‹นํ•˜๋Š” title ์—ด๊ณผ ์ค„๊ฑฐ๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” overview ์—ด์ž…๋‹ˆ๋‹ค. ์ข‹์•„ํ•˜๋Š” ์˜ํ™”๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, ํ•ด๋‹น ์˜ํ™”์˜ ์ค„๊ฑฐ๋ฆฌ์™€ ์œ ์‚ฌํ•œ ์ค„๊ฑฐ๋ฆฌ์˜ ์˜ํ™”๋ฅผ ์ฐพ์•„์„œ ์ถ”์ฒœํ•˜๋Š” ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. # ์ƒ์œ„ 2๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์„ data์— ์ €์žฅ data = data.head(20000) ๋งŒ์•ฝ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์–‘์„ ์ค„์ด๊ณ  ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ค„์—ฌ์„œ ์žฌ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ƒ์œ„ 20,000๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. TF-IDF๋ฅผ ์—ฐ์‚ฐํ•  ๋•Œ ๋ฐ์ดํ„ฐ์— Null ๊ฐ’์ด ๋“ค์–ด์žˆ์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. TF-IDF์˜ ๋Œ€์ƒ์ด ๋˜๋Š” data์˜ overview ์—ด์— ๊ฒฐ์ธก๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” Null ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # overview ์—ด์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฒฐ์ธก๊ฐ’์„ ์ „๋ถ€ ์นด์šดํŠธํ•˜์—ฌ ์ถœ๋ ฅ print('overview ์—ด์˜ ๊ฒฐ์ธก๊ฐ’์˜ ์ˆ˜:',data['overview'].isnull().sum()) overview ์—ด์˜ ๊ฒฐ์ธก๊ฐ’์˜ ์ˆ˜: 135 135๊ฐœ์˜ Null ๊ฐ’์ด ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ฒฐ์ธก๊ฐ’์„ ๊ฐ€์ง„ ํ–‰์„ ์ œ๊ฑฐํ•˜๋Š” pandas์˜ dropna()๋‚˜ ๊ฒฐ์ธก๊ฐ’์ด ์žˆ๋˜ ํ–‰์— ํŠน์ • ๊ฐ’์œผ๋กœ ์ฑ„์›Œ ๋„ฃ๋Š” pandas์˜ fillna()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด„ํ˜ธ ์•ˆ์— Null ๋Œ€์‹  ๋„ฃ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๋˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ๋นˆ ๊ฐ’(empty value)์œผ๋กœ ๋Œ€์ฒดํ•˜์˜€์Šต๋‹ˆ๋‹ค. # ๊ฒฐ์ธก๊ฐ’์„ ๋นˆ ๊ฐ’์œผ๋กœ ๋Œ€์ฒด data['overview'] = data['overview'].fillna('') Null ๊ฐ’์„ ๋นˆ ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•˜์˜€์Šต๋‹ˆ๋‹ค. overview ์—ด์— ๋Œ€ํ•ด์„œ TF-IDF ํ–‰๋ ฌ์„ ๊ตฌํ•œ ํ›„ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. tfidf = TfidfVectorizer(stop_words='english') tfidf_matrix = tfidf.fit_transform(data['overview']) print('TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) :',tfidf_matrix.shape) TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ(shape) : (20000, 47487) TF-IDF ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋Š” 20,000์˜ ํ–‰์„ ๊ฐ€์ง€๊ณ  47,847์˜ ์—ด์„ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด 20,000๊ฐœ์˜ ์˜ํ™”๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด 47,487๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” 47,847์ฐจ์›์˜ ๋ฌธ์„œ ๋ฒกํ„ฐ๊ฐ€ 20,000๊ฐœ๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ ๋„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. ์ด์ œ 20,000๊ฐœ์˜ ๋ฌธ์„œ ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ์ƒํ˜ธ ๊ฐ„์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix) print('์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ :',cosine_sim.shape) ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ : (20000, 20000) ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋กœ๋Š” 20,000ํ–‰ 20,000์—ด์˜ ํ–‰๋ ฌ์„ ์–ป์Šต๋‹ˆ๋‹ค. ์ด๋Š” 20,000๊ฐœ์˜ ๊ฐ ๋ฌธ์„œ ๋ฒกํ„ฐ(์˜ํ™” ์ค„๊ฑฐ๋ฆฌ ๋ฒกํ„ฐ)์™€ ์ž๊ธฐ ์ž์‹ ์„ ํฌํ•จํ•œ 20,000๊ฐœ์˜ ๋ฌธ์„œ ๋ฒกํ„ฐ ๊ฐ„์˜ ์œ ์‚ฌ๋„๊ฐ€ ๊ธฐ๋ก๋œ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  20,000๊ฐœ ์˜ํ™”์˜ ์ƒํ˜ธ ์œ ์‚ฌ๋„๊ฐ€ ๊ธฐ๋ก๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ธฐ์กด ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ๋ถ€ํ„ฐ ์˜ํ™”์˜ ํƒ€์ดํ‹€์„ key, ์˜ํ™”์˜ ์ธ๋ฑ์Šค๋ฅผ value๋กœ ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ title_to_index๋ฅผ ๋งŒ๋“ค์–ด๋‘ก๋‹ˆ๋‹ค. title_to_index = dict(zip(data['title'], data.index)) # ์˜ํ™” ์ œ๋ชฉ Father of the Bride Part II์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ด idx = title_to_index['Father of the Bride Part II'] print(idx) ์„ ํƒํ•œ ์˜ํ™”์˜ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜๋ฉด ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ overview๊ฐ€ ์œ ์‚ฌํ•œ 10๊ฐœ์˜ ์˜ํ™”๋ฅผ ์ฐพ์•„๋‚ด๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. def get_recommendations(title, cosine_sim=cosine_sim): # ์„ ํƒํ•œ ์˜ํ™”์˜ ํƒ€์ดํ‹€๋กœ๋ถ€ํ„ฐ ํ•ด๋‹น ์˜ํ™”์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ›์•„์˜จ๋‹ค. idx = title_to_index[title] # ํ•ด๋‹น ์˜ํ™”์™€ ๋ชจ๋“  ์˜ํ™”์™€์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. sim_scores = list(enumerate(cosine_sim[idx])) # ์œ ์‚ฌ๋„์— ๋”ฐ๋ผ ์˜ํ™”๋“ค์„ ์ •๋ ฌํ•œ๋‹ค. sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True) # ๊ฐ€์žฅ ์œ ์‚ฌํ•œ 10๊ฐœ์˜ ์˜ํ™”๋ฅผ ๋ฐ›์•„์˜จ๋‹ค. sim_scores = sim_scores[1:11] # ๊ฐ€์žฅ ์œ ์‚ฌํ•œ 10๊ฐœ์˜ ์˜ํ™”์˜ ์ธ๋ฑ์Šค๋ฅผ ์–ป๋Š”๋‹ค. movie_indices = [idx[0] for idx in sim_scores] # ๊ฐ€์žฅ ์œ ์‚ฌํ•œ 10๊ฐœ์˜ ์˜ํ™”์˜ ์ œ๋ชฉ์„ ๋ฆฌํ„ดํ•œ๋‹ค. return data['title'].iloc[movie_indices] ์˜ํ™” ๋‹คํฌ ๋‚˜์ดํŠธ ๋ผ์ด์ฆˆ์™€ overview๊ฐ€ ์œ ์‚ฌํ•œ ์˜ํ™”๋“ค์„ ์ฐพ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. get_recommendations('The Dark Knight Rises') 12481 The Dark Knight 150 Batman Forever 1328 Batman Returns 15511 Batman: Under the Red Hood 585 Batman 9230 Batman Beyond: Return of the Joker 18035 Batman: Year One 19792 Batman: The Dark Knight Returns, Part 1 3095 Batman: Mask of the Phantasm 10122 Batman Begins Name: title, dtype: object ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์˜ํ™”๊ฐ€ ์ถœ๋ ฅ๋˜๋Š”๋ฐ, ์˜ํ™” ๋‹คํฌ ๋‚˜์ดํŠธ๊ฐ€ ์ฒซ ๋ฒˆ์งธ๊ณ , ๊ทธ ์™ธ์—๋„ ์ „๋ถ€ ๋ฐฐํŠธ๋งจ ์˜ํ™”๋ฅผ ์ฐพ์•„๋‚ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 11-06 ๋‹จ์–ด์™€ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ• ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ์™ธ์—๋„ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋“ค์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ(Euclidean distance) ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ(euclidean distance)๋Š” ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ๋•Œ ์ž์นด๋ฅด ์œ ์‚ฌ๋„๋‚˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋งŒํผ, ์œ ์šฉํ•œ ๋ฐฉ๋ฒ•์€ ์•„๋‹™๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•˜๊ณ , ์‹œ๋„ํ•ด ๋ณด๋Š” ๊ฒƒ ์ž์ฒด๋งŒ์œผ๋กœ ๋‹ค๋ฅธ ๊ฐœ๋…๋“ค์„ ์ดํ•ดํ•  ๋•Œ ๋„์›€์ด ๋˜๋ฏ€๋กœ ์˜๋ฏธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์ฐจ์› ๊ณต๊ฐ„์—์„œ ๋‘ ๊ฐœ์˜ ์  ์™€ ๊ฐ€ ๊ฐ๊ฐ = ( 1 p, 3. . p) q ( 1 q, 3. . q) ์˜ ์ขŒํ‘œ๋ฅผ ๊ฐ€์งˆ ๋•Œ ๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( 1 p) + ( 2 p) + . . + ( n p) = i 1 ( i p) ๋‹ค์ฐจ์› ๊ณต๊ฐ„์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด, ์ฒ˜์Œ ๋ณด๋Š” ์ž…์žฅ์—์„œ๋Š” ์‹์ด ๋„ˆ๋ฌด ๋ณต์žกํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. ์ข€ ๋” ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ 2์ฐจ์› ๊ณต๊ฐ„์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ขŒํ‘œ ํ‰๋ฉด ์ƒ์—์„œ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2์ฐจ์› ์ขŒํ‘œ ํ‰๋ฉด ์ƒ์—์„œ ๋‘ ์  ์™€ ์‚ฌ์ด์˜ ์ง์„ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ฒฝ์šฐ์—๋Š” ์ง๊ฐ ์‚ผ๊ฐํ˜•์œผ๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ์ค‘ํ•™๊ต ์ˆ˜ํ•™ ๊ณผ์ •์ธ ํ”ผํƒ€๊ณ ๋ผ์Šค์˜ ์ •๋ฆฌ๋ฅผ ํ†ตํ•ด ์™€ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, 2์ฐจ์› ์ขŒํ‘œ ํ‰๋ฉด์—์„œ ๋‘ ์  ์‚ฌ์ด์˜ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ ๊ณต์‹์€ ํ”ผํƒ€๊ณ ๋ผ์Šค์˜ ์ •๋ฆฌ๋ฅผ ํ†ตํ•ด ๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ์›์ ์œผ๋กœ ๋Œ์•„๊ฐ€์„œ ์—ฌ๋Ÿฌ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ณ ์ž ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ ๊ณต์‹์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์€, ์•ž์„œ ๋ณธ 2์ฐจ์›์„ ๋‹จ์–ด์˜ ์ด๊ฐœ์ˆ˜๋งŒํผ์˜ ์ฐจ์›์œผ๋กœ ํ™•์žฅํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์€ DTM์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ 1 2 3 0 1 ๋ฌธ์„œ 2 1 2 3 1 ๋ฌธ์„œ 3 2 1 2 2 ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ 4๊ฐœ์ด๋ฏ€๋กœ, ์ด๋Š” 4์ฐจ์› ๊ณต๊ฐ„์— ๋ฌธ์„œ 1, ๋ฌธ์„œ 2, ๋ฌธ์„œ 3์„ ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์„œ Q์— ๋Œ€ํ•ด์„œ ๋ฌธ์„œ 1, ๋ฌธ์„œ 2, ๋ฌธ์„œ 3 ์ค‘ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋‚ด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋‚˜๋‚˜ ์‚ฌ๊ณผ ์ €๋Š” ์ข‹์•„์š” ๋ฌธ์„œ Q 1 1 0 1 ์ด๋•Œ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๋ฅผ ํ†ตํ•ด ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด, ๋ฌธ์„œ Q ๋˜ํ•œ ๋‹ค๋ฅธ ๋ฌธ์„œ๋“ค์ฒ˜๋Ÿผ 4์ฐจ์› ๊ณต๊ฐ„์— ๋ฐฐ์น˜์‹œ์ผฐ๋‹ค๋Š” ๊ด€์ ์—์„œ 4์ฐจ์› ๊ณต๊ฐ„์—์„œ์˜ ๊ฐ๊ฐ์˜ ๋ฌธ์„œ๋“ค๊ณผ์˜ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import numpy as np def dist(x, y): return np.sqrt(np.sum((x-y)**2)) doc1 = np.array((2,3,0,1)) doc2 = np.array((1,2,3,1)) doc3 = np.array((2,1,2,2)) docQ = np.array((1,1,0,1)) print('๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ :',dist(doc1, docQ)) print('๋ฌธ์„œ 2๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ :',dist(doc2, docQ)) print('๋ฌธ์„œ 3๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ :',dist(doc3, docQ)) ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ : 2.23606797749979 ๋ฌธ์„œ 2๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ : 3.1622776601683795 ๋ฌธ์„œ 3๊ณผ ๋ฌธ์„œ Q์˜ ๊ฑฐ๋ฆฌ : 2.449489742783178 ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ์˜ ๊ฐ’์ด ๊ฐ€์žฅ ์ž‘๋‹ค๋Š” ๊ฒƒ์€ ๋ฌธ์„œ ๊ฐ„ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ๊ฐ€๊น๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฌธ์„œ 1์ด ๋ฌธ์„œ Q์™€ ๊ฐ€์žฅ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2. ์ž์นด๋ฅด ์œ ์‚ฌ๋„(Jaccard similarity) A์™€ B ๋‘ ๊ฐœ์˜ ์ง‘ํ•ฉ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋•Œ ๊ต์ง‘ํ•ฉ์€ ๋‘ ๊ฐœ์˜ ์ง‘ํ•ฉ์—์„œ ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์›์†Œ๋“ค์˜ ์ง‘ํ•ฉ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ•ฉ์ง‘ํ•ฉ์—์„œ ๊ต์ง‘ํ•ฉ์˜ ๋น„์œจ์„ ๊ตฌํ•œ๋‹ค๋ฉด ๋‘ ์ง‘ํ•ฉ A์™€ B์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ์ž์นด๋ฅด ์œ ์‚ฌ๋„(jaccard similarity)์˜ ์•„์ด๋””์–ด์ž…๋‹ˆ๋‹ค. ์ž์นด๋ฅด ์œ ์‚ฌ๋„๋Š” 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, ๋งŒ์•ฝ ๋‘ ์ง‘ํ•ฉ์ด ๋™์ผํ•˜๋‹ค๋ฉด 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ , ๋‘ ์ง‘ํ•ฉ์˜ ๊ณตํ†ต ์›์†Œ๊ฐ€ ์—†๋‹ค๋ฉด 0์˜ ๊ฐ’์„ ๊ฐ–์Šต๋‹ˆ๋‹ค. ์ž์นด๋ฅด ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ž์นด๋ฅด ์œ ์‚ฌ๋„ ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( , ) | โˆฉ | A B = A B | | | | | โˆฉ | ๋‘ ๊ฐœ์˜ ๋น„๊ตํ•  ๋ฌธ์„œ๋ฅผ ๊ฐ๊ฐ o 1 d c๋ผ๊ณ  ํ–ˆ์„ ๋•Œ o 1 d c์˜ ๋ฌธ์„œ์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์ž์นด๋ฅด ์œ ์‚ฌ๋„๋Š” ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( o 1 d c) d c โˆฉ o 2 o 1 d c ๋‘ ๋ฌธ์„œ o 1 d c ์‚ฌ์ด์˜ ์ž์นด๋ฅด ์œ ์‚ฌ๋„ ( o 1 d c) ๋Š” ๋‘ ์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ ํฌ๊ธฐ๋ฅผ ๋‘ ์ง‘ํ•ฉ์˜ ํ•ฉ์ง‘ํ•ฉ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์œผ๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ํ†ตํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. doc1 = "apple banana everyone like likey watch card holder" doc2 = "apple banana coupon passport love you" # ํ† ํฐํ™” tokenized_doc1 = doc1.split() tokenized_doc2 = doc2.split() print('๋ฌธ์„œ 1 :',tokenized_doc1) print('๋ฌธ์„œ 2 :',tokenized_doc2) ๋ฌธ์„œ 1 : ['apple', 'banana', 'everyone', 'like', 'likey', 'watch', 'card', 'holder'] ๋ฌธ์„œ 2 : ['apple', 'banana', 'coupon', 'passport', 'love', 'you'] ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ํ•ฉ์ง‘ํ•ฉ์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. union = set(tokenized_doc1).union(set(tokenized_doc2)) print('๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ํ•ฉ์ง‘ํ•ฉ :',union) ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ํ•ฉ์ง‘ํ•ฉ : {'you', 'passport', 'watch', 'card', 'love', 'everyone', 'apple', 'likey', 'like', 'banana', 'holder', 'coupon'} ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ํ•ฉ์ง‘ํ•ฉ์˜ ๋‹จ์–ด์˜ ์ด๊ฐœ์ˆ˜๋Š” 12๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์—์„œ ๋‘˜ ๋‹ค ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋ฅผ ์ฐพ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. intersection = set(tokenized_doc1).intersection(set(tokenized_doc2)) print('๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ๊ต์ง‘ํ•ฉ :',intersection) ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์˜ ๊ต์ง‘ํ•ฉ : {'apple', 'banana'} ๋ฌธ์„œ 1๊ณผ ๋ฌธ์„œ 2์—์„œ ๋‘˜ ๋‹ค ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋Š” banana์™€ apple ์ด 2๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๊ต์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ•ฉ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆ„๋ฉด ์ž์นด๋ฅด ์œ ์‚ฌ๋„๊ฐ€ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. print('์ž์นด๋ฅด ์œ ์‚ฌ๋„ :',len(intersection)/len(union)) ์ž์นด๋ฅด ์œ ์‚ฌ๋„ : 0.16666666666666666 12. [NLP ๊ธฐ๋ณธ ] - ํ…์ŠคํŠธ ์ž„๋ฒ ๋”ฉ(Embedding): ์‹ ๊ฒฝ๋ง ๊ธฐ๋ฐ˜์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ• ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์–ด์˜ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ธ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-hot encoding)๊ณผ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding). ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์„œ๋ฅผ ๋ฒกํ„ฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 12-01 NLP์—์„œ์˜ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-hot encoding) ์ปดํ“จํ„ฐ ๋˜๋Š” ๊ธฐ๊ณ„๋Š” ๋ฌธ์ž๋ณด๋‹ค๋Š” ์ˆซ์ž๋ฅผ ๋” ์ž˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ๋Š” ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ฐ”๊พธ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ๋ฒ•๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-hot encoding)์€ ๊ทธ ๋งŽ์€ ๊ธฐ๋ฒ• ์ค‘์—์„œ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด๋ฉฐ, ๋จธ์‹  ๋Ÿฌ๋‹, ๋”ฅ ๋Ÿฌ๋‹์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ๋ฐฐ์›Œ์•ผ ํ•˜๋Š” ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šฐ๊ธฐ์— ์•ž์„œ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์— ๋Œ€ํ•ด์„œ ์ •์˜ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ๋ถ„๋“ค์€ ์‚ฌ์ „(vocabulary)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅด์ง€๋งŒ, ์ €๋Š” ์ง‘ํ•ฉ์ด๋ผ๋Š” ํ‘œํ˜„์ด ๋ณด๋‹ค ๋ช…ํ™•ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ ์•ž์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์€ ์•ž์œผ๋กœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๊ณ„์† ๋‚˜์˜ค๋Š” ๊ฐœ๋…์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ ์ดํ•ดํ•˜๊ณ  ๊ฐ€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์€ ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค์˜ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ˜ผ๋™์ด ์—†๋„๋ก ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๋ผ๋Š” ์ •์˜์— ๋Œ€ํ•ด์„œ ์ข€ ๋” ์ฃผ๋ชฉํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ book๊ณผ books์™€ ๊ฐ™์ด ๋‹จ์–ด์˜ ๋ณ€ํ˜• ํ˜•ํƒœ๋„ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์•ž์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ๊ฐ€์ง€๊ณ , ๋ฌธ์ž๋ฅผ ์ˆซ์ž(๋” ๊ตฌ์ฒด์ ์œผ๋กœ๋Š” ๋ฒกํ„ฐ)๋กœ ๋ฐ”๊พธ๋Š” ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํฌํ•จํ•œ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์œ„ํ•ด์„œ ๋จผ์ € ํ•ด์•ผ ํ•  ์ผ์€ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“œ๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ชจ์•„๋†“์œผ๋ฉด ์ด๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‹จ์–ด ์ง‘ํ•ฉ์— ๊ณ ์œ ํ•œ ์ˆซ์ž๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ์— ๋‹จ์–ด๊ฐ€ ์ด 5,000๊ฐœ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 5,000์ž…๋‹ˆ๋‹ค. 5,000๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ๋Š” ์ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ๋‹จ์–ด๋“ค๋งˆ๋‹ค 1๋ฒˆ๋ถ€ํ„ฐ 5,000๋ฒˆ๊นŒ์ง€ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, book์€ 150๋ฒˆ, dog๋Š” 171๋ฒˆ, love๋Š” 192๋ฒˆ, books๋Š” 212๋ฒˆ๊ณผ ๊ฐ™์ด ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜์˜€๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ์ˆซ์ž๋กœ ๋ฐ”๋€ ๋‹จ์–ด๋“ค์„ ๋ฒกํ„ฐ๋กœ ๋‹ค๋ฃจ๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ๋ ๊นŒ์š”? 1. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-hot encoding)์ด๋ž€? ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์€ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๋ฒกํ„ฐ์˜ ์ฐจ์›์œผ๋กœ ํ•˜๊ณ , ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์€ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์— 1์˜ ๊ฐ’์„ ๋ถ€์—ฌํ•˜๊ณ , ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์—๋Š” 0์„ ๋ถ€์—ฌํ•˜๋Š” ๋‹จ์–ด์˜ ๋ฒกํ„ฐ ํ‘œํ˜„ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋œ ๋ฒกํ„ฐ๋ฅผ ์›-ํ•ซ ๋ฒกํ„ฐ(One-hot vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ๋‘ ๊ฐ€์ง€ ๊ณผ์ •์œผ๋กœ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (1) ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. (์ •์ˆ˜ ์ธ์ฝ”๋”ฉ) (2) ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์€ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์˜ ์œ„์น˜์— 1์„ ๋ถ€์—ฌํ•˜๊ณ , ๋‹ค๋ฅธ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์˜ ์œ„์น˜์—๋Š” 0์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด์„œ ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์„ ์˜ˆ์ œ๋กœ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„ , ํ•œ๊ตญ์–ด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ฝ”์—”์—˜ํŒŒ์ด ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install konlpy ๋ฌธ์žฅ : ๋‚˜๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ๋ฐฐ์šด๋‹ค ์œ„๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. from konlpy.tag import Okt okt = Okt() token = okt.morphs("๋‚˜๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ๋ฐฐ์šด๋‹ค") print(token) ['๋‚˜', '๋Š”', '์ž์—ฐ์–ด', '์ฒ˜๋ฆฌ', '๋ฅผ', '๋ฐฐ์šด๋‹ค'] ์ฝ”์—”์—˜ํŒŒ์ด์˜ Okt ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ํ†ตํ•ด์„œ ์šฐ์„  ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. word2index = {} for voca in token: if voca not in word2index.keys(): word2index[voca] = len(word2index) print(word2index) {'๋‚˜': 0, '๋Š”': 1, '์ž์—ฐ์–ด': 2, '์ฒ˜๋ฆฌ': 3, '๋ฅผ': 4, '๋ฐฐ์šด๋‹ค': 5} ๊ฐ ํ† ํฐ์— ๋Œ€ํ•ด์„œ ๊ณ ์œ ํ•œ ์ธ๋ฑ์Šค(index)๋ฅผ ๋ถ€์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ๋ฌธ์žฅ์ด ์งง๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š์ง€๋งŒ, ๋นˆ๋„์ˆ˜ ์ˆœ๋Œ€๋กœ ๋‹จ์–ด๋ฅผ ์ •๋ ฌํ•˜์—ฌ ๊ณ ์œ ํ•œ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ์ž‘์—…์ด ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. (์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ฑ•ํ„ฐ ์ฐธ๊ณ ) def one_hot_encoding(word, word2index): one_hot_vector = [0]*(len(word2index)) index = word2index[word] one_hot_vector[index] = 1 return one_hot_vector ํ† ํฐ์„ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ํ† ํฐ์— ๋Œ€ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. one_hot_encoding("์ž์—ฐ์–ด",word2index) [0, 0, 1, 0, 0, 0] ํ•ด๋‹น ํ•จ์ˆ˜์— '์ž์—ฐ์–ด'๋ผ๋Š” ํ† ํฐ์„ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด๋ดค๋”๋‹ˆ [0, 0, 1, 0, 0, 0]๋ผ๋Š” ๋ฒกํ„ฐ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ž์—ฐ์–ด๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์ธ๋ฑ์Šค๊ฐ€ 2์ด๋ฏ€๋กœ, ์ž์—ฐ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ์ธ๋ฑ์Šค 2์˜ ๊ฐ’์ด 1์ด๋ฉฐ, ๋‚˜๋จธ์ง€ ๊ฐ’์€ 0์ธ ๋ฒกํ„ฐ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. 2. ์›-ํ•ซ ์ธ์ฝ”๋”ฉ(One-hot encoding)์˜ ํ•œ๊ณ„ ์ด๋Ÿฌํ•œ ํ‘œํ˜„ ๋ฐฉ์‹์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚ ์ˆ˜๋ก, ๋ฒกํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๊ณต๊ฐ„์ด ๊ณ„์† ๋Š˜์–ด๋‚œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ง๋กœ๋Š” ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๊ณ„์† ๋Š˜์–ด๋‚œ๋‹ค๊ณ ๋„ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์› ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ๊ณง ๋ฒกํ„ฐ์˜ ์ฐจ์› ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ๋‹จ์–ด๊ฐ€ 1,000๊ฐœ์ธ ์ฝ”ํผ์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์› ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค๋ฉด, ๋ชจ๋“  ๋‹จ์–ด ๊ฐ๊ฐ์€ ๋ชจ๋‘ 1,000๊ฐœ์˜ ์ฐจ์›์„ ๊ฐ€์ง„ ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ชจ๋“  ๋‹จ์–ด ๊ฐ๊ฐ์€ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ 1์„ ๊ฐ€์ง€๊ณ , 999๊ฐœ์˜ ๊ฐ’์€ 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๊ฐ€ ๋˜๋Š”๋ฐ ์ด๋Š” ์ €์žฅ ๊ณต๊ฐ„ ์ธก๋ฉด์—์„œ๋Š” ๋งค์šฐ ๋น„ํšจ์œจ์ ์ธ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ํ‘œํ˜„ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋Š‘๋Œ€, ํ˜ธ๋ž‘์ด, ๊ฐ•์•„์ง€, ๊ณ ์–‘์ด๋ผ๋Š” 4๊ฐœ์˜ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•ด์„œ ๊ฐ๊ฐ, [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]์ด๋ผ๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋ถ€์—ฌ๋ฐ›์•˜๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋•Œ ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ๋Š” ๊ฐ•์•„์ง€์™€ ๋Š‘๋Œ€๊ฐ€ ์œ ์‚ฌํ•˜๊ณ , ํ˜ธ๋ž‘์ด์™€ ๊ณ ์–‘์ด๊ฐ€ ์œ ์‚ฌํ•˜๋‹ค๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ข€ ๋” ๊ทน๋‹จ์ ์œผ๋กœ๋Š” ๊ฐ•์•„์ง€, ๊ฐœ, ๋ƒ‰์žฅ๊ณ ๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์„ ๋•Œ ๊ฐ•์•„์ง€๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ๊ฐœ์™€ ๋ƒ‰์žฅ๊ณ ๋ผ๋Š” ๋‹จ์–ด ์ค‘ ์–ด๋–ค ๋‹จ์–ด์™€ ๋” ์œ ์‚ฌํ•œ์ง€๋„ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ์„ฑ์„ ์•Œ ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์€ ๊ฒ€์ƒ‰ ์‹œ์Šคํ…œ ๋“ฑ์—์„œ ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์—ฌํ–‰์„ ๊ฐ€๋ ค๊ณ  ์›น ๊ฒ€์ƒ‰์ฐฝ์— '์‚ฟํฌ๋กœ ์ˆ™์†Œ'๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ œ๋Œ€๋กœ ๋œ ๊ฒ€์ƒ‰ ์‹œ์Šคํ…œ์ด๋ผ๋ฉด, '์‚ฟํฌ๋กœ ์ˆ™์†Œ'๋ผ๋Š” ๊ฒ€์ƒ‰์–ด์— ๋Œ€ํ•ด์„œ '์‚ฟํฌ๋กœ ๊ฒŒ์ŠคํŠธ ํ•˜์šฐ์Šค', '์‚ฟํฌ๋กœ ๋ฃŒ์นธ', '์‚ฟํฌ๋กœ ํ˜ธํ…”'๊ณผ ๊ฐ™์€ ์œ ์‚ฌ ๋‹จ์–ด์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋„ ํ•จ๊ป˜ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ์„ฑ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†๋‹ค๋ฉด, '๊ฒŒ์ŠคํŠธ ํ•˜์šฐ์Šค'์™€ '๋ฃŒ์นธ'๊ณผ 'ํ˜ธํ…”'์ด๋ผ๋Š” ์—ฐ๊ด€ ๊ฒ€์ƒ‰์–ด๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 12-02 ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding) ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding)์€ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์€ ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ํ‘œํ˜„์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํฌ์†Œ ํ‘œํ˜„, ๋ฐ€์ง‘ ํ‘œํ˜„, ๊ทธ๋ฆฌ๊ณ  ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 1. ํฌ์†Œ ํ‘œํ˜„(Sparse Representation) ์•ž์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ†ตํ•ด์„œ ๋‚˜์˜จ ์›-ํ•ซ ๋ฒกํ„ฐ๋“ค์€ ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์˜ ๊ฐ’๋งŒ 1์ด๊ณ , ๋‚˜๋จธ์ง€ ์ธ๋ฑ์Šค์—๋Š” ์ „๋ถ€ 0์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฒกํ„ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฒกํ„ฐ ๋˜๋Š” ํ–‰๋ ฌ(matrix)์˜ ๊ฐ’์ด ๋Œ€๋ถ€๋ถ„์ด 0์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฐฉ๋ฒ•์„ ํฌ์†Œ ํ‘œํ˜„(sparse representation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ํฌ์†Œ ๋ฒกํ„ฐ(sparse vector)์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํฌ์†Œ ๋ฒกํ„ฐ์˜ ๋ฌธ์ œ์ ์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๋ฉด ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ํ•œ์—†์ด ์ปค์ง„๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•  ๋•Œ๋Š” ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์— ๋‹จ์–ด๊ฐ€ 10,000๊ฐœ์˜€๋‹ค๋ฉด ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 10,000์ด์–ด์•ผ๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ๊ทธ์ค‘์—์„œ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„๋งŒ 1์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” 0์˜ ๊ฐ’์„ ๊ฐ€์ ธ์•ผ๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์ด ํด์ˆ˜๋ก ๊ณ ์ฐจ์›์˜ ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์–ด๊ฐ€ 10,000๊ฐœ ์žˆ๊ณ  ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๋Š” 5์˜€๋‹ค๋ฉด ์› ํ•ซ ๋ฒกํ„ฐ๋Š” ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋˜์–ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. Ex) ๊ฐ•์•„์ง€ = [ 0 0 0 0 0 1 0 0 0 0 0 0 ... ์ค‘๋žต ... 0] # ์ด๋•Œ 1 ๋’ค์˜ 0์˜ ์ˆ˜๋Š” 9994๊ฐœ. ์ด๋Ÿฌํ•œ ๋ฒกํ„ฐ ํ‘œํ˜„์€ ๊ณต๊ฐ„์  ๋‚ญ๋น„๋ฅผ ๋ถˆ๋Ÿฌ์ผ์œผํ‚ต๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๋ฌธ์ œ์ ์€ ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. dog, cat, computer, netbook, book ์ด๋Ÿฌํ•œ ๋‹จ์–ด๊ฐ€ 5๊ฐœ์— ๋Œ€ํ•ด์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ•œ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์šฐ์„  ์ด ๋‹จ์–ด๋“ค์— 0, 1, 2, 3, 4๋ผ๋Š” ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ 5๊ฐœ์ด๋ฏ€๋กœ ๋ฒกํ„ฐ์˜ ์ฐจ์›์œผ๋กœ 5๋กœ ํ•˜๊ณ  ๋ถ€์—ฌ๋œ ๊ฐ ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ์ธ๋ฑ์Šค๋กœ ํ•˜์—ฌ ํ•ด๋‹น ์ธ๋ฑ์Šค์—๋Š” 1, ๋‚˜๋จธ์ง€๋Š” 0์˜ ๊ฐ’์„ ์ฑ„์›Œ ๋„ฃ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ฝ”๋“œ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. import torch # ์›-ํ•ซ ๋ฒกํ„ฐ ์ƒ์„ฑ dog = torch.FloatTensor([1, 0, 0, 0, 0]) cat = torch.FloatTensor([0, 1, 0, 0, 0]) computer = torch.FloatTensor([0, 0, 1, 0, 0]) netbook = torch.FloatTensor([0, 0, 0, 1, 0]) book = torch.FloatTensor([0, 0, 0, 0, 1]) ์ด๋Ÿฌํ•œ ์›-ํ•ซ ๋ฒกํ„ฐ ๊ฐ„ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(torch.cosine_similarity(dog, cat, dim=0)) print(torch.cosine_similarity(cat, computer, dim=0)) print(torch.cosine_similarity(computer, netbook, dim=0)) print(torch.cosine_similarity(netbook, book, dim=0)) tensor(0.) tensor(0.) tensor(0.) tensor(0.) ์‚ฌ๋žŒ์ด ์ƒ๊ฐํ•˜๊ธฐ์— ๊ฐ•์•„์ง€์™€ ๊ณ ์–‘์ด๋ผ๋Š” ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋Š” ๊ณ ์–‘์ด์™€ ์ปดํ“จํ„ฐ๋ผ๋Š” ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ณด๋‹ค ๋†’์„ ๊ฒƒ์ด๋ฉฐ, ์ปดํ“จํ„ฐ์™€ ๋„ท๋ถ์ด๋ผ๋Š” ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋Š” ๋„ท๋ถ๊ณผ ์ฑ…์ด๋ผ๋Š” ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ณด๋‹ค ๋†’์„ ๊ฒƒ ๊ฐ™์ง€๋งŒ ์–ด๋–ค ๋‹จ์–ด๋“ค์„ ์„ ํƒํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ด๋„ ์œ ์‚ฌ๋„๋Š” ์ „๋ถ€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด ๊ฐ„ ์˜๋ฏธ์  ์œ ์‚ฌ๋„๋ฅผ ๋ฐ˜์˜ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์น˜๋ช…์ ์ž…๋‹ˆ๋‹ค. 2. ๋ฐ€์ง‘ ํ‘œํ˜„(Dense Representation) ์ด๋Ÿฌํ•œ ํฌ์†Œ ํ‘œํ˜„๊ณผ ๋ฐ˜๋Œ€๋˜๋Š” ํ‘œํ˜„์ด ์žˆ์œผ๋‹ˆ, ์ด๋ฅผ ๋ฐ€์ง‘ ํ‘œํ˜„(dense representation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ€์ง‘ ํ‘œํ˜„์€ ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋กœ ์ƒ์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ๊ฐ’์œผ๋กœ ๋ชจ๋“  ๋‹จ์–ด์˜ ๋ฒกํ„ฐ ํ‘œํ˜„์˜ ์ฐจ์›์„ ๋งž์ถฅ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ด ๊ณผ์ •์—์„œ ๋” ์ด์ƒ 0๊ณผ 1๋งŒ ๊ฐ€์ง„ ๊ฐ’์ด ์•„๋‹ˆ๋ผ ์‹ค์ˆซ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ํฌ์†Œ ํ‘œํ˜„์˜ ์˜ˆ๋ฅผ ๊ฐ€์ ธ์™€๋ด…์‹œ๋‹ค. Ex) ๊ฐ•์•„์ง€ = [ 0 0 0 0 1 0 0 0 0 0 0 0 ... ์ค‘๋žต ... 0] # ์ด๋•Œ 1 ๋’ค์˜ 0์˜ ์ˆ˜๋Š” 9995๊ฐœ. ์ฐจ์›์€ 10,000 ์˜ˆ๋ฅผ ๋“ค์–ด 10,000๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์„ ๋•Œ ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์™€ ๊ฐ™์€ ํ‘œํ˜„์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ€์ง‘ ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ€์ง‘ ํ‘œํ˜„์˜ ์ฐจ์›์„ 128๋กœ ์„ค์ •ํ•œ๋‹ค๋ฉด, ๋ชจ๋“  ๋‹จ์–ด์˜ ๋ฒกํ„ฐ ํ‘œํ˜„์˜ ์ฐจ์›์€ 128๋กœ ๋ฐ”๋€Œ๋ฉด์„œ ๋ชจ๋“  ๊ฐ’์ด ์‹ค์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. Ex) ๊ฐ•์•„์ง€ = [0.2 1.8 1.1 -2.1 1.1 2.8 ... ์ค‘๋žต ...] # ์ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128 ์ด ๊ฒฝ์šฐ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ์กฐ๋ฐ€ํ•ด์กŒ๋‹ค๊ณ  ํ•˜์—ฌ ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding) ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)์˜ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(word embedding)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋ฅผ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๊ณผ์ •์„ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ผ๊ณ  ํ•˜์—ฌ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(embedding vector)๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์œผ๋กœ๋Š” LSA, Word2Vec, FastText, Glove ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜์—์„œ ์ œ๊ณตํ•˜๋Š” ๋„๊ตฌ์ธ nn.embedding()๋Š” ์•ž์„œ ์–ธ๊ธ‰ํ•œ ๋ฐฉ๋ฒ•๋“ค์„ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์ง€๋งŒ, ๋‹จ์–ด๋ฅผ ๋žœ๋ค ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค์—, ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ๊ฐ€์ค‘์น˜๋ฅผ ํ•™์Šตํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋Š” ์•ž์„œ ๋ฐฐ์šด ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ์ง€๊ธˆ ๋ฐฐ์šฐ๊ณ  ์žˆ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์ด๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. - ์›-ํ•ซ ๋ฒกํ„ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ์ฐจ์› ๊ณ ์ฐจ์›(๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ) ์ €์ฐจ์› ๋‹ค๋ฅธ ํ‘œํ˜„ ํฌ์†Œ ๋ฒกํ„ฐ์˜ ์ผ์ข… ๋ฐ€์ง‘ ๋ฒกํ„ฐ์˜ ์ผ์ข… ํ‘œํ˜„ ๋ฐฉ๋ฒ• ์ˆ˜๋™ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ํ•™์Šตํ•จ ๊ฐ’์˜ ํƒ€์ž… 1๊ณผ 0 ์‹ค์ˆ˜ 12-03 ์›Œ๋“œํˆฌ๋ฒกํ„ฐ(Word2Vec) ์•ž์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ์ฑ•ํ„ฐ์—์„œ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Œ์„ ์–ธ๊ธ‰ํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ๋ฒกํ„ฐํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฐฉ๋ฒ•์ด ์›Œ๋“œํˆฌ๋ฒกํ„ฐ(Word2Vec)์ž…๋‹ˆ๋‹ค. Word2Vec์˜ ๊ฐœ๋…์„ ์„ค๋ช…ํ•˜๊ธฐ์— ์•ž์„œ, Word2Vec๊ฐ€ ์–ด๋–ค ์ผ์„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋จผ์ € ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. http://w.elnn.kr/search/ ์œ„ ์‚ฌ์ดํŠธ๋Š” ํ•œ๊ตญ์–ด ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ๋ฒกํ„ฐ ์—ฐ์‚ฐ์„ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค. ์œ„ ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋‹จ์–ด๋“ค(์‹ค์ œ๋กœ๋Š” Word2Vec ๋ฒกํ„ฐ)๋กœ ๋”ํ•˜๊ธฐ, ๋นผ๊ธฐ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์˜ ์‹์—์„œ ์ขŒ๋ณ€์„ ์ง‘์–ด๋„ฃ์œผ๋ฉด, ์šฐ๋ณ€์˜ ๋‹ต๋“ค์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ๊ณ ์–‘์ด + ์• ๊ต = ๊ฐ•์•„์ง€ ํ•œ๊ตญ - ์„œ์šธ + ๋„์ฟ„ = ์ผ๋ณธ ๋ฐ•์ฐฌํ˜ธ - ์•ผ๊ตฌ + ์ถ•๊ตฌ = ํ˜ธ๋‚˜์šฐ๋‘ ์‹ ๊ธฐํ•˜๊ฒŒ๋„ ๋‹จ์–ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์–ด๋–ค ์˜๋ฏธ๋“ค์„ ๊ฐ€์ง€๊ณ  ์—ฐ์‚ฐ์„ ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” ๊ฐ ๋‹จ์–ด ๋ฒกํ„ฐ๊ฐ€ ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๋ฐ˜์˜ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ์ด๋Ÿฐ ์ผ์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ผ๊นŒ์š”? 1. ํฌ์†Œ ํ‘œํ˜„(Sparse Representation) ์•ž์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ†ตํ•ด์„œ ๋‚˜์˜จ ์›-ํ•ซ ๋ฒกํ„ฐ๋“ค์€ ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์˜ ๊ฐ’๋งŒ 1์ด๊ณ , ๋‚˜๋จธ์ง€ ์ธ๋ฑ์Šค์—๋Š” ์ „๋ถ€ 0์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฒกํ„ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฒกํ„ฐ ๋˜๋Š” ํ–‰๋ ฌ(matrix)์˜ ๊ฐ’์ด ๋Œ€๋ถ€๋ถ„์ด 0์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฐฉ๋ฒ•์„ ํฌ์†Œ ํ‘œํ˜„(sparse representation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ํฌ์†Œ ๋ฒกํ„ฐ(sparse vector)์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์€ ๊ฐ ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ์„ฑ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ๊ณ , ์ด๋ฅผ ์œ„ํ•œ ๋Œ€์•ˆ์œผ๋กœ ๋‹จ์–ด์˜ '์˜๋ฏธ'๋ฅผ ๋‹ค์ฐจ์› ๊ณต๊ฐ„์— ๋ฒกํ„ฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋Ÿฌํ•œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ๋ถ„์‚ฐ ํ‘œํ˜„(distributed representation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๋ถ„์‚ฐ ํ‘œํ˜„์„ ์ด์šฉํ•˜์—ฌ ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ๋ฒกํ„ฐํ™”ํ•˜๋Š” ์ž‘์—…์€ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(embedding) ์ž‘์—…์— ์†ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋œ ๋ฒกํ„ฐ ๋˜ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(embedding vector)๋ผ๊ณ  ํ•˜๋ฉฐ, ์ €์ฐจ์›์„ ๊ฐ€์ง€๋ฏ€๋กœ ๋ฐ”๋กœ ์•ž์˜ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šด ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)์—๋„ ์†ํ•ฉ๋‹ˆ๋‹ค. 2. ๋ถ„์‚ฐ ํ‘œํ˜„(Distributed Representation) ๋ถ„์‚ฐ ํ‘œํ˜„(distributed representation) ๋ฐฉ๋ฒ•์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ถ„ํฌ ๊ฐ€์„ค(distributional hypothesis)์ด๋ผ๋Š” ๊ฐ€์ • ํ•˜์— ๋งŒ๋“ค์–ด์ง„ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ€์ •์€ '๋น„์Šทํ•œ ์œ„์น˜์—์„œ ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๋“ค์€ ๋น„์Šทํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค'๋ผ๋Š” ๊ฐ€์ •์ž…๋‹ˆ๋‹ค. ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด๋Š” ๊ท€์—ฝ๋‹ค, ์˜ˆ์˜๋‹ค, ์• ๊ต ๋“ฑ์˜ ๋‹จ์–ด๊ฐ€ ์ฃผ๋กœ ํ•จ๊ป˜ ๋“ฑ์žฅํ•˜๋Š”๋ฐ ๋ถ„ํฌ ๊ฐ€์„ค์— ๋”ฐ๋ผ์„œ ์ €๋Ÿฐ ๋‚ด์šฉ์„ ๊ฐ€์ง„ ํ…์ŠคํŠธ๋ฅผ ๋ฒกํ„ฐํ™”ํ•œ๋‹ค๋ฉด ์ € ๋‹จ์–ด๋“ค์€ ์˜๋ฏธ์ ์œผ๋กœ ๊ฐ€๊นŒ์šด ๋‹จ์–ด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ถ„์‚ฐ ํ‘œํ˜„์€ ๋ถ„ํฌ ๊ฐ€์„ค์„ ์ด์šฉํ•˜์—ฌ ๋‹จ์–ด๋“ค์˜ ์…‹์„ ํ•™์Šตํ•˜๊ณ , ๋ฒกํ„ฐ์— ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ์—ฌ๋Ÿฌ ์ฐจ์›์— ๋ถ„์‚ฐํ•˜์—ฌ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋œ ๋ฒกํ„ฐ๋“ค์€ ์›-ํ•ซ ๋ฒกํ„ฐ์ฒ˜๋Ÿผ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ์ผ ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ์ƒ๋Œ€์ ์œผ๋กœ ์ €์ฐจ์›์œผ๋กœ ์ค„์–ด๋“ญ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์–ด๊ฐ€ 10,000๊ฐœ ์žˆ๊ณ  ์ธ๋ฑ์Šค๊ฐ€ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค๋Š” 5์˜€๋‹ค๋ฉด ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค. Ex) ๊ฐ•์•„์ง€ = [ 0 0 0 0 1 0 0 0 0 0 0 0 ... ์ค‘๋žต ... 0] 1์ด๋ž€ ๊ฐ’ ๋’ค์—๋Š” 0์ด 9,995๊ฐœ๊ฐ€ ์žˆ๋Š” ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Word2Vec๋กœ ์ž„๋ฒ ๋”ฉ ๋œ ๋ฒกํ„ฐ๋Š” ๊ตณ์ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ๋  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฐ•์•„์ง€๋ž€ ๋‹จ์–ด๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์ฐจ์›์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๊ฐ€ ๋˜๋ฉด์„œ ๊ฐ ์ฐจ์›์€ ์‹ค์ˆ˜ํ˜•์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. Ex) ๊ฐ•์•„์ง€ = [0.2 0.3 0.5 0.7 0.2 ... ์ค‘๋žต ... 0.2] ์š”์•ฝํ•˜๋ฉด ํฌ์†Œ ํ‘œํ˜„์ด ๊ณ ์ฐจ์›์— ๊ฐ ์ฐจ์›์ด ๋ถ„๋ฆฌ๋œ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค๋ฉด, ๋ถ„์‚ฐ ํ‘œํ˜„์€ ์ €์ฐจ์›์— ๋‹จ์–ด์˜ ์˜๋ฏธ๋ฅผ ์—ฌ๋Ÿฌ ์ฐจ์›์—๋‹ค๊ฐ€ ๋ถ„์‚ฐํ•˜์—ฌ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹จ์–ด ๊ฐ„ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ํ•™์Šต ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” NNLM, RNNLM ๋“ฑ์ด ์žˆ์œผ๋‚˜ ์š”์ฆ˜์—๋Š” ํ•ด๋‹น ๋ฐฉ๋ฒ•๋“ค์˜ ์†๋„๋ฅผ ๋Œ€ํญ ๊ฐœ์„ ํ•œ Word2Vec๊ฐ€ ๋งŽ์ด ์“ฐ์ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 3. CBOW(Continuous Bag of Words) Word2Vec์—๋Š” CBOW(Continuous Bag of Words)์™€ Skip-Gram ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ์Šต๋‹ˆ๋‹ค. CBOW๋Š” ์ฃผ๋ณ€์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ๊ฐ€์ง€๊ณ , ์ค‘๊ฐ„์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ, Skip-Gram์€ ์ค‘๊ฐ„์— ์žˆ๋Š” ๋‹จ์–ด๋กœ ์ฃผ๋ณ€ ๋‹จ์–ด๋“ค์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ž์ฒด๋Š” ๊ฑฐ์˜ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— CBOW๋ฅผ ์ดํ•ดํ•œ๋‹ค๋ฉด Skip-Gram๋„ ์†์‰ฝ๊ฒŒ ์ดํ•ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  CBOW์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ์œ„ํ•ด ๋งค์šฐ ๊ฐ„์†Œํ™”๋œ ํ˜•ํƒœ์˜ CBOW๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฌธ : "The fat cat sat on the mat" ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๊ฐ–๊ณ  ์žˆ๋Š” ์ฝ”ํผ์Šค์— ์œ„์™€ ๊ฐ™์€ ๋ฌธ์žฅ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ฐ€์šด๋ฐ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด CBOW๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. {"The", "fat", "cat", "on", "the", "mat"}์œผ๋กœ๋ถ€ํ„ฐ sat์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์€ CBOW๊ฐ€ ํ•˜๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ๋‹จ์–ด sat์„ ์ค‘์‹ฌ ๋‹จ์–ด(center word)๋ผ๊ณ  ํ•˜๊ณ , ์˜ˆ์ธก์— ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์–ด๋“ค์„ ์ฃผ๋ณ€ ๋‹จ์–ด(context word)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ž, ๋’ค๋กœ ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๋ณผ์ง€๋ฅผ ๊ฒฐ์ •ํ–ˆ๋‹ค๋ฉด ์ด ๋ฒ”์œ„๋ฅผ ์œˆ๋„(window)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ 2์ด๊ณ , ์˜ˆ์ธกํ•˜๊ณ ์ž ํ•˜๋Š” ์ค‘์‹ฌ ๋‹จ์–ด๊ฐ€ sat์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ์•ž์˜ ๋‘ ๋‹จ์–ด์ธ fat์™€ cat, ๊ทธ๋ฆฌ๊ณ  ๋’ค์˜ ๋‘ ๋‹จ์–ด์ธ on, the๋ฅผ ์ฐธ๊ณ ํ•ฉ๋‹ˆ๋‹ค. ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ n์ด๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ์‹ค์ œ ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ์ฐธ๊ณ ํ•˜๋ ค๊ณ  ํ•˜๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋Š” 2n์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œˆ๋„ ํฌ๊ธฐ๋ฅผ ์ •ํ–ˆ๋‹ค๋ฉด, ์œˆ๋„๋ฅผ ๊ณ„์† ์›€์ง์—ฌ์„œ ์ฃผ๋ณ€ ๋‹จ์–ด์™€ ์ค‘์‹ฌ ๋‹จ์–ด ์„ ํƒ์„ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ํ•™์Šต์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด ๋ฐฉ๋ฒ•์„ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„(sliding window)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ์ขŒ์ธก์˜ ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ๋ณ€ํ™”๋Š” ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ 2์ผ ๋•Œ, ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„๊ฐ€ ์–ด๋–ค ์‹์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋ฉด์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“œ๋Š”์ง€ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋˜ํ•œ Word2Vec์—์„œ ์ž…๋ ฅ์€ ๋ชจ๋‘ ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ, ์šฐ์ธก ๊ทธ๋ฆผ์€ ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์–ด๋–ป๊ฒŒ ์„ ํƒํ–ˆ์„ ๋•Œ์— ๋”ฐ๋ผ์„œ ๊ฐ๊ฐ ์–ด๋–ค ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ๋˜๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๊ฒฐ๊ตญ CBOW๋ฅผ ์œ„ํ•œ ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. CBOW์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๊ฐ„๋‹จํžˆ ๋„์‹ํ™”ํ•˜๋ฉด ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ์ธต(Input layer)์˜ ์ž…๋ ฅ์œผ๋กœ์„œ ์•ž, ๋’ค๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•œ ์œˆ๋„ ํฌ๊ธฐ ๋ฒ”์œ„ ์•ˆ์— ์žˆ๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด๋“ค์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๊ณ , ์ถœ๋ ฅ์ธต(Output layer)์—์„œ ์˜ˆ์ธกํ•˜๊ณ ์ž ํ•˜๋Š” ์ค‘๊ฐ„ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋’ค์—์„œ ์„ค๋ช…ํ•˜๊ฒ ์ง€๋งŒ, Word2Vec์˜ ํ•™์Šต์„ ์œ„ํ•ด์„œ ์ด ์ค‘๊ฐ„ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์œ„ ๊ทธ๋ฆผ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์‹ค์€, Word2Vec์€ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ(Deep Learning Model)์€ ์•„๋‹ˆ๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต ๋”ฅ ๋Ÿฌ๋‹์ด๋ผ ํ•จ์€, ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต ์‚ฌ์ด์˜ ์€๋‹‰์ธต์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ถฉ๋ถ„ํžˆ ์Œ“์ธ ์‹ ๊ฒฝ๋ง์„ ํ•™์Šตํ•  ๋•Œ๋ฅผ ๋งํ•˜๋Š”๋ฐ Word2Vec๋Š” ์ž…๋ ฅ์ธต๊ณผ ์ถœ๋ ฅ์ธต ์‚ฌ์ด์— ํ•˜๋‚˜์˜ ์€๋‹‰์ธต๋งŒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์€๋‹‰์ธต(hidden Layer)์ด 1๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์‹ฌ์ธต ์‹ ๊ฒฝ๋ง(Deep Neural Network)์ด ์•„๋‹ˆ๋ผ ์–•์€ ์‹ ๊ฒฝ๋ง(Shallow Neural Network)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋˜ํ•œ Word2Vec์˜ ์€๋‹‰์ธต์€ ์ผ๋ฐ˜์ ์ธ ์€๋‹‰์ธต๊ณผ๋Š” ๋‹ฌ๋ฆฌ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉฐ ๋ฃฉ์—… ํ…Œ์ด๋ธ”์ด๋ผ๋Š” ์—ฐ์‚ฐ์„ ๋‹ด๋‹นํ•˜๋Š” ์ธต์œผ๋กœ ์ผ๋ฐ˜์ ์ธ ์€๋‹‰์ธต๊ณผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ํˆฌ์‚ฌ์ธต(projection layer)์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. CBOW์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ข€ ๋” ํ™•๋Œ€ํ•˜์—ฌ, ๋™์ž‘ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ๋Œ€ํ•ด์„œ ์ƒ์„ธํ•˜๊ฒŒ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๊ทธ๋ฆผ์—์„œ ์ฃผ๋ชฉํ•ด์•ผ ํ•  ๊ฒƒ์€ ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ํˆฌ์‚ฌ์ธต์˜ ํฌ๊ธฐ๊ฐ€ M์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. CBOW์—์„œ ํˆฌ์‚ฌ์ธต์˜ ํฌ๊ธฐ M์€ ์ž„๋ฒ ๋”ฉํ•˜๊ณ  ๋‚œ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ์œ„์˜ ๊ทธ๋ฆผ์—์„œ ํˆฌ์‚ฌ์ธต์˜ ํฌ๊ธฐ๋Š” M=5์ด๊ธฐ ๋•Œ๋ฌธ์— CBOW๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚˜์„œ ์–ป๋Š” ๊ฐ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 5๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ์ž…๋ ฅ์ธต๊ณผ ํˆฌ์‚ฌ์ธต ์‚ฌ์ด์˜ ๊ฐ€์ค‘์น˜ W๋Š” V ร— M ํ–‰๋ ฌ์ด๋ฉฐ, ํˆฌ์‚ฌ์ธต์—์„œ ์ถœ๋ ฅ์ธต ์‚ฌ์ด์˜ ๊ฐ€์ค‘์น˜ W'๋Š” M ร— V ํ–‰๋ ฌ์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ V๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 7์ด๊ณ , M์€ 5๋ผ๋ฉด ๊ฐ€์ค‘์น˜ W๋Š” 7 ร— 5 ํ–‰๋ ฌ์ด๊ณ , W'๋Š” 5 ร— 7 ํ–‰๋ ฌ์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์ด ๋‘ ํ–‰๋ ฌ์€ ๋™์ผํ•œ ํ–‰๋ ฌ์„ ์ „์น˜(transpose) ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์„œ๋กœ ๋‹ค๋ฅธ ํ–‰๋ ฌ์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ›ˆ๋ จ ์ „์— ์ด ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W์™€ W'๋Š” ๋Œ€๊ฒŒ ๊ต‰์žฅํžˆ ์ž‘์€ ๋žœ๋ค ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. CBOW๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด๋กœ ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ๋” ์ •ํ™•ํžˆ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ๊ณ„์†ํ•ด์„œ ์ด W์™€ W'๋ฅผ ํ•™์Šตํ•ด๊ฐ€๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ๊ฐ€์ค‘์น˜ W ํ–‰๋ ฌ์˜ ๊ณฑ์ด ์–ด๋–ป๊ฒŒ ์ด๋ฃจ์–ด์ง€๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ๋Š” ๊ฐ ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ๋ฅผ ๋กœ ํ‘œ๊ธฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฒกํ„ฐ๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. i ๋ฒˆ์งธ ์ธ๋ฑ์Šค์— 1์ด๋ผ๋Š” ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๊ทธ ์™ธ์˜ 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ž…๋ ฅ ๋ฒกํ„ฐ์™€ ๊ฐ€์ค‘์น˜ W ํ–‰๋ ฌ์˜ ๊ณฑ์€ ์‚ฌ์‹ค W ํ–‰๋ ฌ์˜ i๋ฒˆ์งธ ํ–‰์„ ๊ทธ๋Œ€๋กœ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ๊ณผ(lookup) ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ์ž‘์—…์„ ๋ฃฉ์—… ํ…Œ์ด๋ธ”(lookup table)์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์•ž์„œ CBOW์˜ ๋ชฉ์ ์€ W์™€ W'๋ฅผ ์ž˜ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ์ ์ด ์žˆ๋Š”๋ฐ, ์‚ฌ์‹ค ๊ทธ ์ด์œ ๊ฐ€ ์—ฌ๊ธฐ์„œ lookup ํ•ด์˜จ W์˜ ๊ฐ ํ–‰๋ฒกํ„ฐ๊ฐ€ ์‚ฌ์‹ค Word2Vec์„ ์ˆ˜ํ–‰ํ•œ ํ›„์˜ ๊ฐ ๋‹จ์–ด์˜ M ์ฐจ์›์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ–๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ฐ ์ฃผ๋ณ€ ๋‹จ์–ด์˜ ์›-ํ•ซ ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ๊ฐ€์ค‘์น˜ W๊ฐ€ ๊ณฑํ•ด์„œ ์ƒ๊ธด ๊ฒฐ๊ณผ ๋ฒกํ„ฐ๋“ค์€ ํˆฌ์‚ฌ์ธต์—์„œ ๋งŒ๋‚˜ ์ด ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์ธ ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ 2๋ผ๋ฉด, ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ด๊ฐœ์ˆ˜๋Š” 2n์ด๋ฏ€๋กœ ์ค‘๊ฐ„ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด 4๊ฐœ๊ฐ€ ์ž…๋ ฅ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํ‰๊ท ์„ ๊ตฌํ•  ๋•Œ๋Š” 4๊ฐœ์˜ ๊ฒฐ๊ณผ ๋ฒกํ„ฐ์— ๋Œ€ํ•ด์„œ ํ‰๊ท ์„ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํˆฌ์‚ฌ์ธต์—์„œ ๋ฒกํ„ฐ์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ๋ถ€๋ถ„์€ CBOW๊ฐ€ Skip-Gram๊ณผ ๋‹ค๋ฅธ ์ฐจ์ด์ ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ณด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ, Skip-Gram์€ ์ž…๋ ฅ์ด ์ค‘์‹ฌ ๋‹จ์–ด ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์— ํˆฌ์‚ฌ์ธต์—์„œ ๋ฒกํ„ฐ์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ•ด์ง„ ํ‰๊ท  ๋ฒกํ„ฐ๋Š” ๋‘ ๋ฒˆ์งธ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ W'์™€ ๊ณฑํ•ด์ง‘๋‹ˆ๋‹ค. ๊ณฑ์…ˆ์˜ ๊ฒฐ๊ณผ๋กœ๋Š” ์›-ํ•ซ ๋ฒกํ„ฐ๋“ค๊ณผ ์ฐจ์›์ด V๋กœ ๋™์ผํ•œ ๋ฒกํ„ฐ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ž…๋ ฅ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 7์ด์—ˆ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ๋‚˜์˜ค๋Š” ๋ฒกํ„ฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ด ๋ฒกํ„ฐ์— CBOW๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ฅผ ์ทจํ•˜๋Š”๋ฐ, ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋กœ ์ธํ•œ ์ถœ๋ ฅ๊ฐ’์€ 0๊ณผ 1์‚ฌ์ด์˜ ์‹ค์ˆ˜๋กœ, ๊ฐ ์›์†Œ์˜ ์ดํ•ฉ์€ 1์ด ๋˜๋Š” ์ƒํƒœ๋กœ ๋ฐ”๋€๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‚˜์˜จ ๋ฒกํ„ฐ๋ฅผ ์Šค์ฝ”์–ด ๋ฒกํ„ฐ(score vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์Šค์ฝ”์–ด ๋ฒกํ„ฐ์˜ ๊ฐ ์ฐจ์› ์•ˆ์—์„œ์˜ ๊ฐ’์ด ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์Šค์ฝ”์–ด ๋ฒกํ„ฐ์˜ j ๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ€์ง„ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์€ j ๋ฒˆ์งธ ๋‹จ์–ด๊ฐ€ ์ค‘์‹ฌ ๋‹จ์–ด์ผ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์Šค์ฝ”์–ด ๋ฒกํ„ฐ๋Š” ์šฐ๋ฆฌ๊ฐ€ ์‹ค์ œ๋กœ ๊ฐ’์„ ์•Œ๊ณ  ์žˆ๋Š” ๋ฒกํ„ฐ์ธ ์ค‘์‹ฌ ๋‹จ์–ด ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ๊ฐ’์— ๊ฐ€๊นŒ์›Œ์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์Šค์ฝ”์–ด ๋ฒกํ„ฐ๋ฅผ ^ ๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ๋กœ ํ–ˆ์„ ๋•Œ, ์ด ๋‘ ๋ฒกํ„ฐ ๊ฐ’์˜ ์˜ค์ฐจ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด CBOW๋Š” ์†์‹ค ํ•จ์ˆ˜(loss function)๋กœ cross-entropy ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. cross-entropy ํ•จ์ˆ˜์— ์‹ค์ œ ์ค‘์‹ฌ ๋‹จ์–ด์ธ ์›-ํ•ซ ๋ฒกํ„ฐ์™€ ์Šค์ฝ”์–ด ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋„ฃ๊ณ , ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ y๊ฐ€ ์›-ํ•ซ ๋ฒกํ„ฐ๋ผ๋Š” ์ ์„ ๊ณ ๋ คํ•˜๋ฉด, ์ด ์‹์€ ์œ„์™€ ๊ฐ™์ด ๊ฐ„์†Œํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์‹์ด ์™œ loss function์œผ๋กœ ์ ํ•ฉํ•œ์ง€ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. c๋ฅผ ์ค‘์‹ฌ ๋‹จ์–ด์—์„œ 1์„ ๊ฐ€์ง„ ์ฐจ์›์˜ ๊ฐ’์˜ ์ธ๋ฑ์Šค๋ผ๊ณ  ํ•œ๋‹ค๋ฉด,๋Š” ^ y ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์— ๋Œ€์ž…ํ•ด ๋ณด๋ฉด -1 log(1) = 0์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฒฐ๊ณผ์ ์œผ๋กœ ^ y ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ์˜ cross-entropy์˜ ๊ฐ’์€ 0์ด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ด ๊ฐ’์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ํ•™์Šตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์—ญ์ „ํŒŒ(Back Propagation)๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด W์™€ W'๊ฐ€ ํ•™์Šต์ด ๋˜๋Š”๋ฐ, ํ•™์Šต์ด ๋‹ค ๋˜์—ˆ๋‹ค๋ฉด M ์ฐจ์›์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ–๋Š” W์˜ ํ–‰์ด๋‚˜ W'์˜ ์—ด๋กœ๋ถ€ํ„ฐ ์–ด๋–ค ๊ฒƒ์„ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” W์™€ W'์˜ ํ‰๊ท ์น˜๋ฅผ ๊ฐ€์ง€๊ณ  ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์„ ํƒํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 4. Skip-gram Skip-gram์€ CBOW๋ฅผ ์ดํ•ดํ–ˆ๋‹ค๋ฉด, ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ž์ฒด๋Š” ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ CBOW์—์„œ๋Š” ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ํ†ตํ•ด ์ค‘์‹ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ–ˆ๋‹ค๋ฉด, Skip-gram์€ ์ค‘์‹ฌ ๋‹จ์–ด์—์„œ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ•œ ๋™์ผํ•œ ์˜ˆ๋ฌธ์— ๋Œ€ํ•ด์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ๋„์‹ํ™”ํ•ด๋ณด๋ฉด ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ค‘์‹ฌ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํˆฌ์‚ฌ์ธต์—์„œ ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์€ ์—†์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋…ผ๋ฌธ์—์„œ ์„ฑ๋Šฅ ๋น„๊ต๋ฅผ ์ง„ํ–‰ํ–ˆ์„ ๋•Œ, ์ „๋ฐ˜์ ์œผ๋กœ Skip-gram์ด CBOW๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. 5. ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง(Negative Sampling) ๋Œ€์ฒด์ ์œผ๋กœ Word2Vec๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•˜๋ฉด SGNS(Skip-Gram with Negative Sampling)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Skip-gram์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง(Negative Sampling)์ด๋ž€ ๋ฐฉ๋ฒ•๊นŒ์ง€ ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. Skip-gram์„ ์ „์ œ๋กœ ๋„ค๊ฑฐํ‹ฐ๋ธŒ ์ƒ˜ํ”Œ๋ง์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค. ์œ„์—์„œ ๋ฐฐ์šด Word2Vec ๋ชจ๋ธ์—๋Š” ํ•œ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์†๋„์ž…๋‹ˆ๋‹ค. Word2Vec์˜ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋ฅผ ์ฃผ๋ชฉํ•ด ๋ด…์‹œ๋‹ค. ์ถœ๋ ฅ์ธต์— ์žˆ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ ํฌ๊ธฐ์˜ ๋ฒกํ„ฐ ๋‚ด์˜ ๋ชจ๋“  ๊ฐ’์„ 0๊ณผ 1์‚ฌ์ด์˜ ๊ฐ’์ด๋ฉด์„œ ๋ชจ๋‘ ๋”ํ•˜๋ฉด 1์ด ๋˜๋„๋ก ๋ฐ”๊พธ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์— ๋Œ€ํ•œ ์˜ค์ฐจ๋ฅผ ๊ตฌํ•˜๊ณ  ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•œ ์ž„๋ฒ ๋”ฉ์„ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋‹จ์–ด๊ฐ€ ์ค‘์‹ฌ ๋‹จ์–ด๋‚˜ ์ฃผ๋ณ€ ๋‹จ์–ด์™€ ์ „ํ˜€ ์ƒ๊ด€์—†๋Š” ๋‹จ์–ด๋ผ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ ์ˆ˜๋ฐฑ๋งŒ์— ๋‹ฌํ•œ๋‹ค๋ฉด ์ด ์ž‘์—…์€ ๊ต‰์žฅํžˆ ๋ฌด๊ฑฐ์šด ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฑด Word2Vec์ด ๋ชจ๋“  ๋‹จ์–ด ์ง‘ํ•ฉ์— ๋Œ€ํ•ด์„œ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , ์—ญ์ „ํŒŒ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์ฃผ๋ณ€ ๋‹จ์–ด์™€ ์ƒ๊ด€์—†๋Š” ๋ชจ๋“  ๋‹จ์–ด๊นŒ์ง€์˜ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์กฐ์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์—์„œ '๊ฐ•์•„์ง€'์™€ '๊ณ ์–‘์ด'์™€ ๊ฐ™์€ ๋‹จ์–ด์— ์ง‘์ค‘ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, Word2Vec์€ ์‚ฌ์‹ค '๋ˆ๊ฐ€์Šค'๋‚˜ '์ปดํ“จํ„ฐ'์™€ ๊ฐ™์€ ์—ฐ๊ด€ ๊ด€๊ณ„๊ฐ€ ์—†๋Š” ์ˆ˜๋งŽ์€ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ์„ ์กฐ์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์กฐ๊ธˆ ๋” ํšจ์œจ์ ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—†์„๊นŒ์š”? ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์ด ์•„๋‹ˆ๋ผ ์ผ๋ถ€ ๋‹จ์–ด ์ง‘ํ•ฉ์— ๋Œ€ํ•ด์„œ๋งŒ ๊ณ ๋ คํ•˜๋ฉด ์•ˆ ๋ ๊นŒ์š”? ์ด๋ ‡๊ฒŒ ์ผ๋ถ€ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. '๊ฐ•์•„์ง€', '๊ณ ์–‘์ด', '์• ๊ต'์™€ ๊ฐ™์€ ์ฃผ๋ณ€ ๋‹จ์–ด๋“ค์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์— '๋ˆ๊ฐ€์Šค', '์ปดํ“จํ„ฐ', 'ํšŒ์˜์‹ค'๊ณผ ๊ฐ™์€ ๋žœ๋ค์œผ๋กœ ์„ ํƒ๋œ ์ฃผ๋ณ€ ๋‹จ์–ด๊ฐ€ ์•„๋‹Œ ์ƒ๊ด€์—†๋Š” ๋‹จ์–ด๋“ค์„ ์ผ๋ถ€๋งŒ ๊ฐ–๊ณ  ์˜ต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ๋ณด๋‹ค ํ›จ์”ฌ ์ž‘์€ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด๋†“๊ณ  ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋ฅผ ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋กœ ๋ฐ”๊ฟ”๋ฒ„๋ฆฌ๋Š” ๊ฒ๋‹ˆ๋‹ค. ์ฆ‰, Word2Vec์€ ์ฃผ๋ณ€ ๋‹จ์–ด๋“ค์„ ๊ธ์ •(positive)์œผ๋กœ ๋‘๊ณ  ๋žœ๋ค์œผ๋กœ ์ƒ˜ํ”Œ๋ง ๋œ ๋‹จ์–ด๋“ค์„ ๋ถ€์ •(negative)์œผ๋กœ ๋‘” ๋‹ค์Œ์— ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ธฐ์กด์˜ ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋กœ ๋ฐ”๊พธ๋ฉด์„œ๋„ ์—ฐ์‚ฐ๋Ÿ‰์— ์žˆ์–ด์„œ ํ›จ์”ฌ ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ์˜์–ด์™€ ํ•œ๊ตญ์–ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ Word2Vec ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 12-04 ์˜์–ด/ํ•œ๊ตญ์–ด Word2Vec ํ•™์Šต์‹œํ‚ค๊ธฐ gensim ํŒจํ‚ค์ง€์—์„œ ์ œ๊ณตํ•˜๋Š” ์ด๋ฏธ ๊ตฌํ˜„๋œ Word2Vec์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜์–ด์™€ ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ์˜์–ด Word2Vec ๋งŒ๋“ค๊ธฐ ํŒŒ์ด์ฌ์˜ gensim ํŒจํ‚ค์ง€์—๋Š” Word2Vec์„ ์ง€์›ํ•˜๊ณ  ์žˆ์–ด, gensim ํŒจํ‚ค์ง€๋ฅผ ์ด์šฉํ•˜๋ฉด ์†์‰ฝ๊ฒŒ ๋‹จ์–ด๋ฅผ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด๋กœ ๋œ ์ฝ”ํผ์Šค๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ , ์ „์ฒ˜๋ฆฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ Word2Vec ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. import re import urllib.request import zipfile from lxml import etree from nltk.tokenize import word_tokenize, sent_tokenize 1) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์ดํ•ดํ•˜๊ธฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. # ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/09.%20Word%20Embedding/dataset/ted_en-20160408.xml", filename="ted_en-20160408.xml") ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์€ xml ๋ฌธ๋ฒ•์œผ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ์–ด ์ž์—ฐ์–ด๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ „์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์–ป๊ณ ์ž ํ•˜๋Š” ์‹ค์งˆ์  ๋ฐ์ดํ„ฐ๋Š” ์˜์–ด ๋ฌธ์žฅ์œผ๋กœ ๋งŒ ๊ตฌ์„ฑ๋œ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋Š” <content>์™€ </content> ์‚ฌ์ด์˜ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ ์ž‘์—…์„ ํ†ตํ•ด xml ๋ฌธ๋ฒ•๋“ค์€ ์ œ๊ฑฐํ•˜๊ณ , ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, <content>์™€ </content> ์‚ฌ์ด์˜ ๋‚ด์šฉ ์ค‘์—๋Š” (Laughter)๋‚˜ (Applause)์™€ ๊ฐ™์€ ๋ฐฐ๊ฒฝ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹จ์–ด๋„ ๋“ฑ์žฅํ•˜๋Š”๋ฐ ์ด ๋˜ํ•œ ์ œ๊ฑฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. <file id="1"> <head> <url>http://www.ted.com/talks/knut_haanaes_two_reasons_companies_fail_and_how_to_avoid_them</url> <pagesize>72832</pagesize> ... xml ๋ฌธ๋ฒ• ์ค‘๋žต ... <content> Here are two reasons companies fail: they only do more of the same, or they only do what's new. To me the real, real solution to quality growth is figuring out the balance between two activities: ... content ๋‚ด์šฉ ์ค‘๋žต ... To me, the irony about the Facit story is hearing about the Facit engineers, who had bought cheap, small electronic calculators in Japan that they used to double-check their calculators. (Laughter) ... content ๋‚ด์šฉ ์ค‘๋žต ... (Applause) </content> </file> <file id="2"> <head> <url>http://www.ted.com/talks/lisa_nip_how_humans_could_evolve_to_survive_in_space<url> ... ์ดํ•˜ ์ค‘๋žต ... 2) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ ์ „์ฒ˜๋ฆฌ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. targetXML = open('ted_en-20160408.xml', 'r', encoding='UTF8') target_text = etree.parse(targetXML) # xml ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ <content>์™€ </content> ์‚ฌ์ด์˜ ๋‚ด์šฉ๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค. parse_text = '\n'.join(target_text.xpath('//content/text()')) # ์ •๊ทœ ํ‘œํ˜„์‹์˜ sub ๋ชจ๋“ˆ์„ ํ†ตํ•ด content ์ค‘๊ฐ„์— ๋“ฑ์žฅํ•˜๋Š” (Audio), (Laughter) ๋“ฑ์˜ ๋ฐฐ๊ฒฝ์Œ ๋ถ€๋ถ„์„ ์ œ๊ฑฐ. # ํ•ด๋‹น ์ฝ”๋“œ๋Š” ๊ด„ํ˜ธ๋กœ ๊ตฌ์„ฑ๋œ ๋‚ด์šฉ์„ ์ œ๊ฑฐ. content_text = re.sub(r'\([^)]*\)', '', parse_text) # ์ž…๋ ฅ ์ฝ”ํผ์Šค์— ๋Œ€ํ•ด์„œ NLTK๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์žฅ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰. sent_text = sent_tokenize(content_text) # ๊ฐ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๊ตฌ๋‘์ ์„ ์ œ๊ฑฐํ•˜๊ณ , ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜. normalized_text = [] for string in sent_text: tokens = re.sub(r"[^a-z0-9]+", " ", string.lower()) normalized_text.append(tokens) # ๊ฐ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ NLTK๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰. result = [word_tokenize(sentence) for sentence in normalized_text] print('์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : {}'.format(len(result))) ์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 273424 ์ด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋Š” ์•ฝ 27๋งŒ 3์ฒœ ๊ฐœ์ž…๋‹ˆ๋‹ค. # ์ƒ˜ํ”Œ 3๊ฐœ๋งŒ ์ถœ๋ ฅ for line in result[:3]: print(line) ['here', 'are', 'two', 'reasons', 'companies', 'fail', 'they', 'only', 'do', 'more', 'of', 'the', 'same', 'or', 'they', 'only', 'do', 'what', 's', 'new'] ['to', 'me', 'the', 'real', 'real', 'solution', 'to', 'quality', 'growth', 'is', 'figuring', 'out', 'the', 'balance', 'between', 'two', 'activities', 'exploration', 'and', 'exploitation'] ['both', 'are', 'necessary', 'but', 'it', 'can', 'be', 'too', 'much', 'of', 'a', 'good', 'thing'] ์ƒ์œ„ 3๊ฐœ ๋ฌธ์žฅ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด์•˜๋Š”๋ฐ ํ† ํฐ ํ™”๊ฐ€ ์ˆ˜ํ–‰๋˜์—ˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Word2Vec ๋ชจ๋ธ์— ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ์‹œํ‚ต๋‹ˆ๋‹ค. 3) Word2Vec ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ from gensim.models import Word2Vec from gensim.models import KeyedVectors model = Word2Vec(sentences=result, vector_size=100, window=5, min_count=5, workers=4, sg=0) Word2Vec์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. vector_size = ์›Œ๋“œ ๋ฒกํ„ฐ์˜ ํŠน์ง• ๊ฐ’. ์ฆ‰, ์ž„๋ฒ ๋”ฉ ๋œ ๋ฒกํ„ฐ์˜ ์ฐจ์›. window = ์ปจํ…์ŠคํŠธ ์œˆ๋„ ํฌ๊ธฐ min_count = ๋‹จ์–ด ์ตœ์†Œ ๋นˆ๋„ ์ˆ˜ ์ œํ•œ (๋นˆ๋„๊ฐ€ ์ ์€ ๋‹จ์–ด๋“ค์€ ํ•™์Šตํ•˜์ง€ ์•Š๋Š”๋‹ค.) workers = ํ•™์Šต์„ ์œ„ํ•œ ํ”„๋กœ์„ธ์Šค ์ˆ˜ sg = 0์€ CBOW, 1์€ Skip-gram. Word2Vec์— ๋Œ€ํ•ด์„œ ํ•™์Šต์„ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Word2Vec๋Š” ์ž…๋ ฅํ•œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์„ ์ถœ๋ ฅํ•˜๋Š” model.wv.most_similar์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. man๊ณผ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์€ ์–ด๋–ค ๋‹จ์–ด๋“ค์ผ๊นŒ์š”? model_result = model.wv.most_similar("man") print(model_result) [('woman', 0.842622697353363), ('guy', 0.8178728818893433), ('boy', 0.7774451375007629), ('lady', 0.7767927646636963), ('girl', 0.7583760023117065), ('gentleman', 0.7437191009521484), ('soldier', 0.7413754463195801), ('poet', 0.7060446739196777), ('kid', 0.6925194263458252), ('friend', 0.6572611331939697)] man๊ณผ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋กœ woman, guy, boy, lady, girl, gentleman, soldier, kid ๋“ฑ์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Word2Vec๋ฅผ ํ†ตํ•ด ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 4) Word2Vec ๋ชจ๋ธ ์ €์žฅํ•˜๊ณ  ๋กœ๋“œํ•˜๊ธฐ ๊ณต๋“ค์—ฌ ํ•™์Šตํ•œ ๋ชจ๋ธ์„ ์–ธ์ œ๋“  ๋‚˜์ค‘์— ๋‹ค์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ปดํ“จํ„ฐ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ณ  ๋‹ค์‹œ ๋กœ๋“œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์„ ๊ฐ€์ง€๊ณ  ํ–ฅํ›„ ์‹œ๊ฐํ™”๋ฅผ ์ง„ํ–‰ํ•  ์˜ˆ์ •์ด๋ฏ€๋กœ ๊ผญ ์ €์žฅํ•ด ์ฃผ์„ธ์š”. model.wv.save_word2vec_format('eng_w2v') # ๋ชจ๋ธ ์ €์žฅ loaded_model = KeyedVectors.load_word2vec_format("eng_w2v") # ๋ชจ๋ธ ๋กœ๋“œ ๋กœ๋“œํ•œ ๋ชจ๋ธ์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ man๊ณผ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model_result = loaded_model.most_similar("man") print(model_result) [('woman', 0.842622697353363), ('guy', 0.8178728818893433), ('boy', 0.7774451375007629), ('lady', 0.7767927646636963), ('girl', 0.7583760023117065), ('gentleman', 0.7437191009521484), ('soldier', 0.7413754463195801), ('poet', 0.7060446739196777), ('kid', 0.6925194263458252), ('friend', 0.6572611331939697)] 2. ํ•œ๊ตญ์–ด Word2Vec ๋งŒ๋“ค๊ธฐ(๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ) ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋กœ ํ•œ๊ตญ์–ด Word2Vec์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. import pandas as pd import matplotlib.pyplot as plt import urllib.request from gensim.models.word2vec import Word2Vec from konlpy.tag import Okt ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings.txt", filename="ratings.txt") ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•˜๊ณ  ์ƒ์œ„ 5๊ฐœ์˜ ํ–‰์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. train_data = pd.read_table('ratings.txt') train_data[:5] # ์ƒ์œ„ 5๊ฐœ ์ถœ๋ ฅ ์ด ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(len(train_data)) # ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ 200000 ์ด 20๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜๋Š”๋ฐ, ๊ฒฐ์ธก๊ฐ’ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # NULL ๊ฐ’ ์กด์žฌ ์œ ๋ฌด print(train_data.isnull().values.any()) True ๊ฒฐ์ธก๊ฐ’์ด ์กด์žฌํ•˜๋ฏ€๋กœ ๊ฒฐ์ธก๊ฐ’์ด ์กด์žฌํ•˜๋Š” ํ–‰์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. train_data = train_data.dropna(how = 'any') # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ํ–‰ ์ œ๊ฑฐ print(train_data.isnull().values.any()) # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ False ๊ฒฐ์ธก๊ฐ’์ด ์‚ญ์ œ๋œ ํ›„์˜ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print(len(train_data)) # ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ 199992 ์ด 199,992๊ฐœ์˜ ๋ฆฌ๋ทฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ†ตํ•ด ํ•œ๊ธ€์ด ์•„๋‹Œ ๊ฒฝ์šฐ ์ œ๊ฑฐํ•˜๋Š” ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. # ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ†ตํ•œ ํ•œ๊ธ€ ์™ธ ๋ฌธ์ž ์ œ๊ฑฐ train_data['document'] = train_data['document'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") train_data[:5] # ์ƒ์œ„ 5๊ฐœ ์ถœ๋ ฅ ํ•™์Šต ์‹œ์— ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ๋‹จ์–ด๋“ค์ธ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Okt๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์ผ์ข…์˜ ๋‹จ์–ด ๋‚ด์ง€๋Š” ํ˜•ํƒœ์†Œ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š” ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์†Œ ์‹œ๊ฐ„์ด ์†Œ์š”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๋ถˆ์šฉ์–ด ์ •์˜ stopwords = ['์˜','๊ฐ€','์ด','์€','๋“ค','๋Š”','์ข€','์ž˜','๊ทธ๋ƒฅ','๊ณผ','๋„','๋ฅผ','์œผ๋กœ','์ž','์—','์™€','ํ•œ','ํ•˜๋‹ค'] # ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ OKT๋ฅผ ์‚ฌ์šฉํ•œ ํ† ํฐํ™” ์ž‘์—… (๋‹ค์†Œ ์‹œ๊ฐ„ ์†Œ์š”) okt = Okt() tokenized_data = [] for sentence in tqdm(train_data['document']): tokenized_sentence = okt.morphs(sentence, stem=True) # ํ† ํฐํ™” stopwords_removed_sentence = [word for word in tokenized_sentence if not word in stopwords] # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ tokenized_data.append(stopwords_removed_sentence) ํ† ํฐ ํ™”๊ฐ€ ๋œ ์ƒํƒœ์—์„œ๋Š” ๊ฐ ๋ฆฌ๋ทฐ์˜ ๊ธธ์ด ๋ถ„ํฌ ๋˜ํ•œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. # ๋ฆฌ๋ทฐ ๊ธธ์ด ๋ถ„ํฌ ํ™•์ธ print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max(len(review) for review in tokenized_data)) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :',sum(map(len, tokenized_data))/len(tokenized_data)) plt.hist([len(review) for review in tokenized_data], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 72 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 10.716703668146726 Word2Vec์œผ๋กœ ํ† ํฐํ™”๋œ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. from gensim.models import Word2Vec model = Word2Vec(sentences = tokenized_data, vector_size = 100, window = 5, min_count = 5, workers = 4, sg = 0) ํ•™์Šต์ด ๋‹ค ๋˜์—ˆ๋‹ค๋ฉด Word2Vec ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # ์™„์„ฑ๋œ ์ž„๋ฒ ๋”ฉ ๋งคํŠธ๋ฆญ์Šค์˜ ํฌ๊ธฐ ํ™•์ธ model.wv.vectors.shape (16477, 100) ์ด 16,477๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜๋ฉฐ ๊ฐ ๋‹จ์–ด๋Š” 100์ฐจ์›์œผ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. '์ตœ๋ฏผ์‹'๊ณผ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์„ ๋ฝ‘์•„๋ด…์‹œ๋‹ค. print(model.wv.most_similar("์ตœ๋ฏผ์‹")) [('ํ•œ์„๊ทœ', 0.8789200782775879), ('์•ˆ์„ฑ๊ธฐ', 0.8757420778274536), ('๊น€์ˆ˜ํ˜„', 0.855679452419281), ('์ด๋ฏผํ˜ธ', 0.854516863822937), ('๊น€๋ช…๋ฏผ', 0.8525030612945557), ('์ตœ๋ฏผ์ˆ˜', 0.8492398262023926), ('์ด์„ฑ์žฌ', 0.8478372097015381), ('์œค์ œ๋ฌธ', 0.8470626473426819), ('๊น€์ฐฝ์™„', 0.8456774950027466), ('์ด์ฃผ์Šน', 0.8442063927650452)] 'ํžˆ์–ด๋กœ'์™€ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์„ ๋ฝ‘์•„๋ด…์‹œ๋‹ค. print(model.wv.most_similar("ํžˆ์–ด๋กœ")) [('์Šฌ๋ž˜์…”', 0.8747539520263672), ('๋ˆ„์•„๋ฅด', 0.8666149377822876), ('๋ฌดํ˜‘', 0.8423701524734497), ('ํ˜ธ๋Ÿฌ', 0.8372749090194702), ('๋ฌผ์˜', 0.8365858793258667), ('๋ฌด๋น„', 0.8260530233383179), ('๋ฌผ', 0.8197994232177734), ('ํ™์ฝฉ', 0.8120777606964111), ('๋ธ”๋ก๋ฒ„์Šคํ„ฐ', 0.8021541833877563), ('๋ธ”๋ž™', 0.7880141139030457)] 3. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2Vec ์ž„๋ฒ ๋”ฉ(Pre-trained Word2Vec embedding) ์†Œ๊ฐœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์„ ํ•  ๋•Œ, ์ผ€๋ผ์Šค์˜ Embedding()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ–๊ณ  ์žˆ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ๋„ ํ•˜์ง€๋งŒ, ์œ„ํ‚คํ”ผ๋””์•„ ๋“ฑ์˜ ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์ „์— ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(pre-trained word embedding vector)๋ฅผ ๊ฐ€์ง€๊ณ  ์™€์„œ ํ•ด๋‹น ๋ฒกํ„ฐ๋“ค์˜ ๊ฐ’์„ ์›ํ•˜๋Š” ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ ์ž‘์—…์„ ํ•˜๋Š”๋ฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋ถ€์กฑํ•œ ์ƒํ™ฉ์ด๋ผ๋ฉด, ๋‹ค๋ฅธ ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ Word2Vec์ด๋‚˜ GloVe ๋“ฑ์œผ๋กœ ์‚ฌ์ „์— ํ•™์Šต์‹œ์ผœ๋†“์€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋•Œ๋กœ๋Š” ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ๊ฐ€์ ธ์™€์„œ ๊ฐ„๋‹จํžˆ ๋‹จ์–ด๋“ค์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ด๋ณด๋Š” ์‹ค์Šต์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋ชจ๋ธ์— ์ ์šฉํ•ด ๋ณด๋Š” ์‹ค์Šต์€ ํ–ฅํ›„์— ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๊ธ€์ด ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ(๋ฏธ๋ฆฌ ํ•™์Šต๋ผ ์žˆ๋Š”) Word2Vec ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ตฌ๊ธ€์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ 3๋ฐฑ๋งŒ ๊ฐœ์˜ Word2Vec ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 300์ž…๋‹ˆ๋‹ค. gensim์„ ํ†ตํ•ด์„œ ์ด ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฑด ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ๊ธฐ์žฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ ๋‹ค์šด๋กœ๋“œ ๊ฒฝ๋กœ : https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM/edit ์••์ถ• ํŒŒ์ผ์˜ ์šฉ๋Ÿ‰์€ ์•ฝ 1.5GB์ด์ง€๋งŒ, ํŒŒ์ผ์˜ ์••์ถ•์„ ํ’€๋ฉด ์•ฝ 3.3GB์˜ ํŒŒ์ผ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. import gensim import urllib.request # ๊ตฌ๊ธ€์˜ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2Vec ๋ชจ๋ธ์„ ๋กœ๋“œ. urllib.request.urlretrieve("https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz", \ filename="GoogleNews-vectors-negative300.bin.gz") word2vec_model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin.gz', binary=True) ๋ชจ๋ธ์˜ ํฌ๊ธฐ(shape)๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(word2vec_model.vectors.shape) (3000000, 300) ๋ชจ๋ธ์˜ ํฌ๊ธฐ๋Š” 3,000,000 x 300์ž…๋‹ˆ๋‹ค. ์ฆ‰, 3๋ฐฑ๋งŒ ๊ฐœ์˜ ๋‹จ์–ด์™€ ๊ฐ ๋‹จ์–ด์˜ ์ฐจ์›์€ 300์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ์˜ ํฌ๊ธฐ๊ฐ€ 3๊ธฐ๊ฐ€๊ฐ€ ๋„˜๋Š” ์ด์œ ๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 3 million words * 300 features * 4bytes/feature = ~3.35GB ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•ด ๋ด…์‹œ๋‹ค. print(word2vec_model.similarity('this', 'is')) print(word2vec_model.similarity('post', 'book')) 0.407970363878 0.0572043891977 ๋‹จ์–ด 'book'์˜ ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(word2vec_model['book']) [ 0.11279297 -0.02612305 -0.04492188 0.06982422 0.140625 0.03039551 -0.04370117 0.24511719 0.08740234 -0.05053711 0.23144531 -0.07470703 ... 300๊ฐœ์˜ ๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š” ๊ด€๊ณ„๋กœ ์ค‘๋žต ... 0.03637695 -0.16796875 -0.01483154 0.09667969 -0.05761719 -0.00515747] ์ฐธ๊ณ  : Word2vec ๋ชจ๋ธ์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์ด์ง€๋งŒ ์ตœ๊ทผ์— ๋“ค์–ด์„œ๋Š” ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ๋„˜์–ด์„œ ์ถ”์ฒœ ์‹œ์Šคํ…œ์—๋„ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์ ๋‹นํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜์—ดํ•ด ์ฃผ๋ฉด Word2vec์€ ์œ„์น˜๊ฐ€ ๊ทผ์ ‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์‚ฌ๋„๊ฐ€ ๋†’์€ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค๋Š” ์ ์—์„œ ์ฐฉ์•ˆ๋œ ์•„์ด๋””์–ด์ž…๋‹ˆ๋‹ค. ๊ด€์‹ฌ ์žˆ๋Š” ๋ถ„๋“ค์€ ๊ตฌ๊ธ€์— 'item2vec'์„ ๊ฒ€์ƒ‰ํ•ด ๋ณด์„ธ์š”. 12-05 ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์‹œ๊ฐํ™”(Embedding Visualization) ๊ตฌ๊ธ€์€ ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ(embedding projector)๋ผ๋Š” ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” ๋„๊ตฌ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šตํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ ๋…ผ๋ฌธ : https://arxiv.org/pdf/1611.05469v1.pdf 1. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ 2๊ฐœ์˜ tsv ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ•™์Šตํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ผญ Word2Vec ๋“ฑ์œผ๋กœ ํ•™์Šตํ•ด์•ผ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ •ํ•ด์ ธ์žˆ์ง€๋Š” ์—†๊ณ , GloVe ๋“ฑ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ›ˆ๋ จ๋˜์–ด ์žˆ์–ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•ด์„œ๋Š” ์ด๋ฏธ ๋ชจ๋ธ์„ ํ•™์Šตํ•˜๊ณ , ํŒŒ์ผ๋กœ ์ €์žฅ๋ผ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ €์žฅ๋ผ ์žˆ๋‹ค๋ฉด ์•„๋ž˜ ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ์‹œ๊ฐํ™”์— ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. !python -m gensim.scripts.word2vec2tensor --input ๋ชจ๋ธ ์ด๋ฆ„ --output ๋ชจ๋ธ ์ด๋ฆ„ ์—ฌ๊ธฐ์„œ๋Š” ํŽธ์˜๋ฅผ ์œ„ํ•ด ์ด์ „ ์ฑ•ํ„ฐ์—์„œ ํ•™์Šตํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ์‹ค์Šต๊นŒ์ง€ ์ง„ํ–‰ํ–ˆ๋˜ ์˜์–ด Word2Vec ๋ชจ๋ธ์ธ 'eng_w2v'๋ฅผ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. eng_w2v๋ผ๋Š” Word2Vec ๋ชจ๋ธ์ด ์ด๋ฏธ ์กด์žฌํ•œ๋‹ค๋Š” ๊ฐ€์ • ํ•˜์— ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์—์„œ ์•„๋ž˜ ์ปค๋งจ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. !python -m gensim.scripts.word2vec2tensor --input eng_w2v --output eng_w2v ์ปค๋งจ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์ด ์‹œ์ž‘๋˜๋Š” ๊ฒฝ๋กœ์— ๊ธฐ์กด์— ์žˆ๋˜ eng_w2v ์™ธ์—๋„ ๋‘ ๊ฐœ์˜ ํŒŒ์ผ์ด ์ƒ๊น๋‹ˆ๋‹ค. ์ƒˆ๋กœ ์ƒ๊ธด eng_w2v_metadata.tsv์™€ eng_w2v_tensor.tsv ์ด ๋‘ ๊ฐœ ํŒŒ์ผ์ด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•  ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ eng_w2v ๋ชจ๋ธ ํŒŒ์ผ์ด ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋ชจ๋ธ ํŒŒ์ผ ์ด๋ฆ„์œผ๋กœ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, '๋ชจ๋ธ ์ด๋ฆ„_ metadata.tsv'์™€ '๋ชจ๋ธ ์ด๋ฆ„_ tensor.tsv'๋ผ๋Š” ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๊ฐํ™”ํ•˜๊ธฐ ์ด์ œ ๊ตฌ๊ธ€์˜ ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ์„ ์‹œ๊ฐํ™”ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋งํฌ์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : https://projector.tensorflow.org/ ์‚ฌ์ดํŠธ์— ์ ‘์†ํ•ด์„œ ์ขŒ์ธก ์ƒ๋‹จ์„ ๋ณด๋ฉด Load๋ผ๋Š” ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Load๋ผ๋Š” ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฐฝ์ด ๋œจ๋Š”๋ฐ ์ด ๋‘ ๊ฐœ์˜ Choose file ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์— ์žˆ๋Š” Choose file ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  eng_w2v_tensor.tsv ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜๊ณ , ์•„๋ž˜์— ์žˆ๋Š” Choose file ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  eng_w2v_metadata.tsv ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๋‘ ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜๋ฉด ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ์— ํ•™์Šตํ–ˆ๋˜ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ์ด ์‹œ๊ฐํ™”๋ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„์—๋Š” ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ์˜ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž„๋ฒ ๋”ฉ ํ”„๋กœ์ ํ„ฐ๋Š” ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจ์›์„ ์ถ•์†Œํ•˜์—ฌ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” PCA, t-SNE ๋“ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ž์„ธํ•œ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ 'man'์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์„ ํƒํ•˜๊ณ , ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์ƒ์œ„ 10๊ฐœ ๋ฒกํ„ฐ๋“ค์„ ํ‘œ์‹œํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. 12-06 ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™๊ต์˜ ๊ธ€๋กœ๋ธŒ(GloVe) ๊ธ€๋กœ๋ธŒ(Global Vectors for Word Representation, GloVe)๋Š” ์นด์šดํŠธ ๊ธฐ๋ฐ˜๊ณผ ์˜ˆ์ธก ๊ธฐ๋ฐ˜์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋ก ์œผ๋กœ 2014๋…„์— ๋ฏธ๊ตญ ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™์—์„œ ๊ฐœ๋ฐœํ•œ ๋‹จ์–ด ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์ž…๋‹ˆ๋‹ค. ์•ž์„œ ํ•™์Šตํ•˜์˜€๋˜ ๊ธฐ์กด์˜ ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ LSA(Latent Semantic Analysis)์™€ ์˜ˆ์ธก ๊ธฐ๋ฐ˜์˜ Word2Vec์˜ ๋‹จ์ ์„ ์ง€์ ํ•˜๋ฉฐ ์ด๋ฅผ ๋ณด์™„ํ•œ๋‹ค๋Š” ๋ชฉ์ ์œผ๋กœ ๋‚˜์™”๊ณ , ์‹ค์ œ๋กœ๋„ Word2Vec ๋งŒํผ ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ˜„์žฌ๊นŒ์ง€์˜ ์—ฐ๊ตฌ์— ๋”ฐ๋ฅด๋ฉด ๋‹จ์ •์ ์œผ๋กœ Word2Vec์™€ GloVe ์ค‘์—์„œ ์–ด๋–ค ๊ฒƒ์ด ๋” ๋›ฐ์–ด๋‚˜๋‹ค๊ณ  ๋งํ•  ์ˆ˜๋Š” ์—†๊ณ , ์ด ๋‘ ๊ฐ€์ง€ ์ „๋ถ€๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๊ณ  ์„ฑ๋Šฅ์ด ๋” ์ข‹์€ ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. 1. ๊ธฐ์กด ๋ฐฉ๋ฒ•๋ก ์— ๋Œ€ํ•œ ๋น„ํŒ LSA๋Š” ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•œ ํ–‰๋ ฌ์ด๋ผ๋Š” ์ „์ฒด์ ์ธ ํ†ต๊ณ„ ์ •๋ณด๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์ฐจ์›์„ ์ถ•์†Œ(Truncated SVD) ํ•˜์—ฌ ์ž ์žฌ๋œ ์˜๋ฏธ๋ฅผ ๋Œ์–ด๋‚ด๋Š” ๋ฐฉ๋ฒ•๋ก ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, Word2Vec๋Š” ์‹ค์ œ ๊ฐ’๊ณผ ์˜ˆ์ธก๊ฐ’์— ๋Œ€ํ•œ ์˜ค์ฐจ๋ฅผ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ค„์—ฌ๋‚˜๊ฐ€๋ฉฐ ํ•™์Šตํ•˜๋Š” ์˜ˆ์ธก ๊ธฐ๋ฐ˜์˜ ๋ฐฉ๋ฒ•๋ก ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ์ด ๋‘ ๋ฐฉ๋ฒ•๋ก ์€ ๊ฐ๊ฐ ์žฅ, ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. LSA๋Š” ์นด์šดํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ์ฝ”ํผ์Šค์˜ ์ „์ฒด์ ์ธ ํ†ต๊ณ„ ์ •๋ณด๋ฅผ ๊ณ ๋ คํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ์™•:๋‚จ์ž = ์—ฌ์™•:? (์ •๋‹ต์€ ์—ฌ์ž)์™€ ๊ฐ™์€ ๋‹จ์–ด ์˜๋ฏธ์˜ ์œ ์ถ” ์ž‘์—…(Analogy task)์—๋Š” ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. Word2Vec๋Š” ์˜ˆ์ธก ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹จ์–ด ๊ฐ„ ์œ ์ถ” ์ž‘์—…์—๋Š” LSA๋ณด๋‹ค ๋›ฐ์–ด๋‚˜์ง€๋งŒ, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ์œˆ๋„ ํฌ๊ธฐ ๋‚ด์—์„œ๋งŒ ์ฃผ๋ณ€ ๋‹จ์–ด๋ฅผ ๊ณ ๋ คํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”ํผ์Šค์˜ ์ „์ฒด์ ์ธ ํ†ต๊ณ„ ์ •๋ณด๋ฅผ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. GloVe๋Š” ์ด๋Ÿฌํ•œ ๊ธฐ์กด ๋ฐฉ๋ฒ•๋ก ๋“ค์˜ ๊ฐ๊ฐ์˜ ํ•œ๊ณ„๋ฅผ ์ง€์ ํ•˜๋ฉฐ, LSA์˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด์—ˆ๋˜ ์นด์šดํŠธ ๊ธฐ๋ฐ˜์˜ ๋ฐฉ๋ฒ•๊ณผ Word2Vec์˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด์—ˆ๋˜ ์˜ˆ์ธก ๊ธฐ๋ฐ˜์˜ ๋ฐฉ๋ฒ•๋ก  ๋‘ ๊ฐ€์ง€๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 2. ์œˆ๋„ ๊ธฐ๋ฐ˜ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ(Window based Co-occurrence Matrix) ๋‹จ์–ด์˜ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์€ ํ–‰๊ณผ ์—ด์„ ์ „์ฒด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ๋‹จ์–ด๋“ค๋กœ ๊ตฌ์„ฑํ•˜๊ณ , i ๋‹จ์–ด์˜ ์œˆ๋„ ํฌ๊ธฐ(Window Size) ๋‚ด์—์„œ k ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ i ํ–‰ k ์—ด์— ๊ธฐ์žฌํ•œ ํ–‰๋ ฌ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ํ…์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. Ex) I like deep learning I like NLP I enjoy flying ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ N ์ผ ๋•Œ๋Š” ์ขŒ, ์šฐ์— ์กด์žฌํ•˜๋Š” N ๊ฐœ์˜ ๋‹จ์–ด๋งŒ ์ฐธ๊ณ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œˆ๋„ ํฌ๊ธฐ๊ฐ€ 1์ผ ๋•Œ, ์œ„์˜ ํ…์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ตฌ์„ฑํ•œ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์นด์šดํŠธ I like enjoy deep learning NLP flying I 0 2 1 0 0 0 0 like 2 0 0 1 0 1 0 enjoy 1 0 0 0 0 0 1 deep 0 1 0 0 1 0 0 learning 0 0 0 1 0 0 0 NLP 0 1 0 0 0 0 0 flying 0 0 1 0 0 0 0 ์œ„ ํ–‰๋ ฌ์€ ํ–‰๋ ฌ์„ ์ „์น˜(Transpose) ํ•ด๋„ ๋™์ผํ•œ ํ–‰๋ ฌ์ด ๋œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” i ๋‹จ์–ด์˜ ์œˆ๋„ ํฌ๊ธฐ ๋‚ด์—์„œ k ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ๋นˆ๋„๋Š” ๋ฐ˜๋Œ€๋กœ k ๋‹จ์–ด์˜ ์œˆ๋„ ํฌ๊ธฐ ๋‚ด์—์„œ i ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ๋นˆ๋„์™€ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ํ…Œ์ด๋ธ”์€ ์Šคํƒ ํผ๋“œ ๋Œ€ํ•™๊ต์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งํฌ : http://web.stanford.edu/class/cs224n/slides/cs224n-2019-lecture02-wordvecs2.pdf 3. ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ (Co-occurrence Probability) ์ด์ œ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ–ˆ์œผ๋‹ˆ, ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์•„๋ž˜์˜ ํ‘œ๋Š” ์–ด๋–ค ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์„ ๊ฐ€์ง€๊ณ  ์ •๋ฆฌํ•œ ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ (Co-occurrence Probability)์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”? ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ  ( | i ) ๋Š” ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ๋กœ๋ถ€ํ„ฐ ํŠน์ • ๋‹จ์–ด i์˜ ์ „์ฒด ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๊ณ , ํŠน์ • ๋‹จ์–ด i๊ฐ€ ๋“ฑ์žฅํ–ˆ์„ ๋•Œ ์–ด๋–ค ๋‹จ์–ด k๊ฐ€ ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜์—ฌ ๊ณ„์‚ฐํ•œ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค. ( | i ) ์—์„œ i๋ฅผ ์ค‘์‹ฌ ๋‹จ์–ด(Center Word), k๋ฅผ ์ฃผ๋ณ€ ๋‹จ์–ด(Context Word)๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ์œ„์—์„œ ๋ฐฐ์šด ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์—์„œ ์ค‘์‹ฌ ๋‹จ์–ด i์˜ ํ–‰์˜ ๋ชจ๋“  ๊ฐ’์„ ๋”ํ•œ ๊ฐ’์„ ๋ถ„๋ชจ๋กœ ํ•˜๊ณ  i ํ–‰ k ์—ด์˜ ๊ฐ’์„ ๋ถ„์ž๋กœ ํ•œ ๊ฐ’์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ GloVe์˜ ์ œ์•ˆ ๋…ผ๋ฌธ์—์„œ ๊ฐ€์ ธ์˜จ ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์„ ํ‘œ๋กœ ์ •๋ฆฌํ•œ ํ•˜๋‚˜์˜ ์˜ˆ์ž…๋‹ˆ๋‹ค. ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ๊ณผ ํฌ๊ธฐ ๊ด€๊ณ„ ๋น„(ratio) | k=solid | k=gas | k=water | k=fasion | ------|------|------|------| P(k l ice) | 0.00019 | 0.000066 | 0.003 | 0.000017 | P(k l steam) | 0.000022 | 0.00078 | 0.0022 | 0.000018 | P(k l ice) / P(k l steam) | 8.9 | 0.085 | 1.36 | 0.96 | ์œ„์˜ ํ‘œ๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์‹ค์€ ice๊ฐ€ ๋“ฑ์žฅํ–ˆ์„ ๋•Œ solid๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ  0.00019์€ steam์ด ๋“ฑ์žฅํ–ˆ์„ ๋•Œ solid๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ ์ธ 0.000022๋ณด๋‹ค ์•ฝ 8.9๋ฐฐ ํฌ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋„ ๊ทธ๋Ÿด ๊ฒƒ์ด solid๋Š” '๋‹จ๋‹จํ•œ'์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์กŒ์œผ๋‹ˆ๊นŒ '์ฆ๊ธฐ'๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š” steam๋ณด๋‹ค๋Š” ๋‹น์—ฐํžˆ '์–ผ์Œ'์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š” ice๋ผ๋Š” ๋‹จ์–ด์™€ ๋” ์ž์ฃผ ๋“ฑ์žฅํ•  ๊ฒ๋‹ˆ๋‹ค. ์ˆ˜์‹์ ์œผ๋กœ ๋‹ค์‹œ ์ •๋ฆฌํ•˜์—ฌ ์–ธ๊ธ‰ํ•˜๋ฉด k๊ฐ€ solid ์ผ ๋•Œ, P(solid l ice) / P(solid l steam)๋ฅผ ๊ณ„์‚ฐํ•œ ๊ฐ’์€ 8.9๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ 1๋ณด๋‹ค๋Š” ๋งค์šฐ ํฐ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์™œ๋ƒ๋ฉด P(solid | ice)์˜ ๊ฐ’์€ ํฌ๊ณ , P(solid | steam)์˜ ๊ฐ’์€ ์ž‘๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ k๋ฅผ solid๊ฐ€ ์•„๋‹ˆ๋ผ gas๋กœ ๋ฐ”๊พธ๋ฉด ์–˜๊ธฐ๋Š” ์™„์ „ํžˆ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. gas๋Š” ice๋ณด๋‹ค๋Š” steam๊ณผ ๋” ์ž์ฃผ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ, P(gas l ice) / P(gas l steam)๋ฅผ ๊ณ„์‚ฐํ•œ ๊ฐ’์€ 1๋ณด๋‹ค ํ›จ์”ฌ ์ž‘์€ ๊ฐ’์ธ 0.085๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, k๊ฐ€ water์ธ ๊ฒฝ์šฐ์—๋Š” solid์™€ steam ๋‘ ๋‹จ์–ด ๋ชจ๋‘์™€ ๋™์‹œ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋ฏ€๋กœ 1์— ๊ฐ€๊นŒ์šด ๊ฐ’์ด ๋‚˜์˜ค๊ณ , k๊ฐ€ fasion์ธ ๊ฒฝ์šฐ์—๋Š” solid์™€ steam ๋‘ ๋‹จ์–ด ๋ชจ๋‘์™€ ๋™์‹œ ๋“ฑ์žฅํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ ์œผ๋ฏ€๋กœ 1์— ๊ฐ€๊นŒ์šด ๊ฐ’์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋ณด๊ธฐ ์‰ฝ๋„๋ก ์กฐ๊ธˆ ๋‹จ์ˆœํ™”ํ•ด์„œ ํ‘œํ˜„ํ•œ ํ‘œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ๊ณผ ํฌ๊ธฐ ๊ด€๊ณ„ ๋น„(ratio) k=solid k=gas k=water k=fasion P(k l ice) ํฐ ๊ฐ’ ์ž‘์€ ๊ฐ’ ํฐ ๊ฐ’ ์ž‘์€ ๊ฐ’ P(k l steam) ์ž‘์€ ๊ฐ’ ํฐ ๊ฐ’ ํฐ ๊ฐ’ ์ž‘์€ ๊ฐ’ P(k l ice) / P(k l steam) ํฐ ๊ฐ’ ์ž‘์€ ๊ฐ’ 1์— ๊ฐ€๊นŒ์›€ 1์— ๊ฐ€๊นŒ์›€ ์ด์ œ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ๊ณผ ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์˜ ์ดํ•ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4. ์†์‹ค ํ•จ์ˆ˜(Loss function) ์šฐ์„  ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์ „์— ๊ฐ ์šฉ์–ด๋ฅผ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. : ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ(Co-occurrence Matrix) i : ์ค‘์‹ฌ ๋‹จ์–ด i๊ฐ€ ๋“ฑ์žฅํ–ˆ์„ ๋•Œ ์œˆ๋„ ๋‚ด ์ฃผ๋ณ€ ๋‹จ์–ด j๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ํšŸ์ˆ˜ i โˆ‘ X j : ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์—์„œ i ํ–‰์˜ ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’ i : ( | i ) X k i : ์ค‘์‹ฌ ๋‹จ์–ด i๊ฐ€ ๋“ฑ์žฅํ–ˆ์„ ๋•Œ ์œˆ๋„ ๋‚ด ์ฃผ๋ณ€ ๋‹จ์–ด k๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ  Ex) P(solid l ice) = ๋‹จ์–ด ice๊ฐ€ ๋“ฑ์žฅํ–ˆ์„ ๋•Œ ๋‹จ์–ด solid๊ฐ€ ๋“ฑ์žฅํ•  ํ™•๋ฅ  i P k P k P k ๋กœ ๋‚˜๋ˆ ์ค€ ๊ฐ’ Ex) P(solid l ice) / P(solid l steam) = 8.9 i : ์ค‘์‹ฌ ๋‹จ์–ด i์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ k : ์ฃผ๋ณ€ ๋‹จ์–ด k์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ GloVe์˜ ์•„์ด๋””์–ด๋ฅผ ํ•œ ์ค„๋กœ ์š”์•ฝํ•˜๋ฉด '์ž„๋ฒ ๋”ฉ ๋œ ์ค‘์‹ฌ ๋‹จ์–ด์™€ ์ฃผ๋ณ€ ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ๋‚ด์ ์ด ์ „์ฒด ์ฝ”ํผ์Šค์—์„œ์˜ ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์ด ๋˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ'์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ด๋ฅผ ๋งŒ์กฑํ•˜๋„๋ก ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o p o u t ( i w ~ ) P ( | i ) P k ๋’ค์—์„œ ๋ณด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ, ๋” ์ •ํ™•ํžˆ๋Š” GloVe๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋„๋ก ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. o p o u t ( i w ~ ) l g P ( | i ) l g P k ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์„ค๊ณ„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๋‹จ์–ด ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ž˜ ํ‘œํ˜„ํ•˜๋Š” ํ•จ์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์•ž์„œ ๋ฐฐ์šด ๊ฐœ๋…์ธ i / j๋ฅผ ์‹์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. GloVe์˜ ์—ฐ๊ตฌ์ง„๋“ค์€ ๋ฒกํ„ฐ i w, k๋ฅผ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด, i / j ๊ฐ€ ๋‚˜์˜จ๋‹ค๋Š” ์ดˆ๊ธฐ ์‹์œผ๋กœ๋ถ€ํ„ฐ ์ „๊ฐœ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ( i w , w ~ ) P k j ์•„์ง ์ด ํ•จ์ˆ˜ ๊ฐ€ ์–ด๋–ค ์‹์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€๋Š” ์ •ํ•ด์ง„ ๊ฒŒ ์—†์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋ชฉ์ ์— ๋งž๊ฒŒ ๊ทผ์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์‹์€ ๋ฌด์ˆ˜ํžˆ ๋งŽ๊ฒ ์œผ๋‚˜ ์ตœ์ ์˜ ์‹์— ๋‹ค๊ฐ€๊ฐ€๊ธฐ ์œ„ํ•ด์„œ ์ฐจ๊ทผ, ์ฐจ๊ทผ ๋””ํ…Œ์ผ์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋Š” ๋‘ ๋‹จ์–ด ์‚ฌ์ด์˜ ๋™์‹œ ๋“ฑ์žฅ ํ™•๋ฅ ์˜ ํฌ๊ธฐ ๊ด€๊ณ„ ๋น„(ratio) ์ •๋ณด๋ฅผ ๋ฒกํ„ฐ ๊ณต๊ฐ„์— ์ธ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด GloVe ์—ฐ๊ตฌ์ง„๋“ค์€ i w๋ผ๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ์ฐจ์ด๋ฅผ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค. ( i w , w ~ ) P k j ๊ทธ๋Ÿฐ๋ฐ ์šฐ๋ณ€์€ ์Šค์นผ๋ผ ๊ฐ’์ด๊ณ  ์ขŒ๋ณ€์€ ๋ฒกํ„ฐ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์„ฑ๋ฆฝํ•˜๊ธฐ ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ ํ•จ์ˆ˜์˜ ๋‘ ์ž…๋ ฅ์— ๋‚ด์ (Dot product)์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ( ( i w) w ~ ) P k j ์ •๋ฆฌํ•˜๋ฉด, ์„ ํ˜• ๊ณต๊ฐ„(Linear space)์—์„œ ๋‹จ์–ด์˜ ์˜๋ฏธ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋บ„์…ˆ๊ณผ ๋‚ด์ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•จ์ˆ˜ ๊ฐ€ ๋งŒ์กฑํ•ด์•ผ ํ•  ํ•„์ˆ˜ ์กฐ๊ฑด์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ค‘์‹ฌ ๋‹จ์–ด ์™€ ์ฃผ๋ณ€ ๋‹จ์–ด ~ ๋ผ๋Š” ์„ ํƒ ๊ธฐ์ค€์€ ์‹ค์ œ๋กœ๋Š” ๋ฌด์ž‘์œ„ ์„ ํƒ์ด๋ฏ€๋กœ ์ด ๋‘˜์˜ ๊ด€๊ณ„๋Š” ์ž์œ ๋กญ๊ฒŒ ๊ตํ™˜๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์„ฑ๋ฆฝ๋˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ GloVe ์—ฐ๊ตฌ์ง„์€ ํ•จ์ˆ˜ ๊ฐ€ ์‹ค์ˆ˜์˜ ๋ฅ์…ˆ๊ณผ ์–‘์ˆ˜์˜ ๊ณฑ์…ˆ์— ๋Œ€ํ•ด์„œ ์ค€๋™ํ˜•(Homomorphism)์„ ๋งŒ์กฑํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์ƒ์†Œํ•œ ์šฉ์–ด๋ผ์„œ ๋ง์ด ์–ด๋ ค์›Œ ๋ณด์ด๋Š”๋ฐ, ์ •๋ฆฌํ•˜๋ฉด ์™€์— ๋Œ€ํ•ด์„œ ํ•จ์ˆ˜ ๊ฐ€ ( + ) F ( ) ( ) ์™€ ๊ฐ™๋„๋ก ๋งŒ์กฑ์‹œ์ผœ์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( + ) F ( ) ( ) โˆ€, b R ์ด์ œ ์ด ์ค€๋™<NAME>์„ ํ˜„์žฌ ์ „๊ฐœํ•˜๋˜ GloVe ์‹์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์กฐ๊ธˆ์”ฉ ๋ฐ”๊ฟ”๋ณผ ๊ฒ๋‹ˆ๋‹ค. ์ „๊ฐœํ•˜๋˜ GloVe ์‹์— ๋”ฐ๋ฅด๋ฉด, ํ•จ์ˆ˜๋Š” ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ์Šค์นผ๋ผ ๊ฐ’( i P k )์ด ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ค€๋™<NAME>์—์„œ ์™€ ๊ฐ€ ๊ฐ๊ฐ ๋ฒกํ„ฐ ๊ฐ’์ด๋ผ๋ฉด ํ•จ์ˆ˜์˜ ๊ฒฐ๊ด๊ฐ’์œผ๋กœ๋Š” ์Šค์นผ๋ผ ๊ฐ’์ด ๋‚˜์˜ฌ ์ˆ˜ ์—†์ง€๋งŒ, ์™€ ๊ฐ€ ๊ฐ๊ฐ ์‚ฌ์‹ค ๋‘ ๋ฒกํ„ฐ์˜ ๋‚ด์  ๊ฐ’์ด๋ผ๊ณ  ํ•˜๋ฉด ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ์Šค์นผ๋ผ ๊ฐ’์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์œ„์˜ ์ค€๋™<NAME>์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊ฟ”๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ 1 v , v , v๋Š” ๊ฐ๊ฐ ๋ฒกํ„ฐ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๋Š” ๋ฒกํ„ฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ( 1 v + 3 v) F ( 1 v) ( 3 v) โˆ€ 1 v , v , v โˆˆ ๊ทธ๋Ÿฐ๋ฐ ์•ž์„œ ์ž‘์„ฑํ•œ GloVe ์‹์—์„œ๋Š” i w๋ผ๋Š” ๋‘ ๋ฒกํ„ฐ์˜ ์ฐจ์ด๋ฅผ ํ•จ์ˆ˜์˜ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. GloVe ์‹์— ๋ฐ”๋กœ ์ ์šฉ์„ ์œ„ํ•ด ์ค€๋™ํ˜• ์‹์„ ์ด๋ฅผ ๋บ„์…ˆ์— ๋Œ€ํ•œ ์ค€๋™<NAME>์œผ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๊ณฑ์…ˆ๋„ ๋‚˜๋ˆ—์…ˆ์œผ๋กœ ๋ฐ”๋€๋‹ˆ๋‹ค. ( 1 v โˆ’ 3 v) F ( 1 v) ( 3 v) โˆ€ 1 v , v , v โˆˆ ์ด์ œ ์ด ์ค€๋™ํ˜• ์‹์„ GloVe ์‹์— ์ ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„ , ํ•จ์ˆ˜์˜ ์šฐ๋ณ€์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๋€Œ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ( ( i w) w ~ ) F ( i w ~ ) ( j w ~ ) ๊ทธ๋Ÿฐ๋ฐ ์ด์ „์˜ ์‹์— ๋”ฐ๋ฅด๋ฉด ์šฐ๋ณ€์€ ๋ณธ๋ž˜ i P k ์˜€์œผ๋ฏ€๋กœ, ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. i P k F ( i w ~ ) ( j w ~ ) ( i w ~ ) P k X k i ์ขŒ๋ณ€์„ ํ’€์–ด์“ฐ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ( i w ~ โˆ’ w T k) F ( i w ~ ) ( j w ~ ) ์ด๋Š” ๋บ„์…ˆ์— ๋Œ€ํ•œ ์ค€๋™<NAME>์˜ ํ˜•ํƒœ์™€ ์ •ํ™•ํžˆ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ฅผ ๋งŒ์กฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ฐพ์•„์•ผ ํ•  ๋•Œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ๋งŒ์กฑ์‹œํ‚ค๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ ๋ฐ”๋กœ<NAME> ํ•จ์ˆ˜(Exponential function)์ž…๋‹ˆ๋‹ค.๋ฅผ<NAME> ํ•จ์ˆ˜ x๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. x ( i w ~ โˆ’ w T k) e p ( i w ~ ) x ( j w ~ ) x ( i w ~ ) P k X k i ์œ„์˜ ๋‘ ๋ฒˆ์งธ ์‹์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. i w ~ l g P k l g ( i X) l g X k l g X ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ์ƒ๊ธฐํ•ด์•ผ ํ•  ๊ฒƒ์€ ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด, ์‚ฌ์‹ค i w ~ ๋Š” ๋‘ ๊ฐ’์˜ ์œ„์น˜๋ฅผ ์„œ๋กœ ๋ฐ”๊พธ์–ด๋„ ์‹์ด ์„ฑ๋ฆฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. i์˜ ์ •์˜๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด k ์™€๋„ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฒŒ ์„ฑ๋ฆฝ๋˜๋ ค๋ฉด ์œ„์˜ ์‹์—์„œ o X ํ•ญ์ด ๊ฑธ๋ฆผ๋Œ์ž…๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„๋งŒ ์—†๋‹ค๋ฉด ์ด๋ฅผ ์„ฑ๋ฆฝ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ GloVe ์—ฐ๊ตฌํŒ€์€ ์ด o X ํ•ญ์„ i ์— ๋Œ€ํ•œ ํŽธํ–ฅ i ๋ผ๋Š” ์ƒ์ˆ˜ํ•ญ์œผ๋กœ ๋Œ€์ฒดํ•˜๊ธฐ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ์ด์œ ๋กœ k์— ๋Œ€ํ•œ ํŽธํ–ฅ k๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. i w ~ b + k = o X k ์ด ์‹์ด ์†์‹ค ํ•จ์ˆ˜์˜ ํ•ต์‹ฌ์ด ๋˜๋Š” ์‹์ž…๋‹ˆ๋‹ค. ์šฐ๋ณ€์˜ ๊ฐ’๊ณผ์˜ ์ฐจ์ด๋ฅผ ์ตœ์†Œํ™”๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ขŒ๋ณ€์˜ 4๊ฐœ์˜ ํ•ญ์€ ํ•™์Šต์„ ํ†ตํ•ด ๊ฐ’์ด ๋ฐ”๋€Œ๋Š” ๋ณ€์ˆ˜๋“ค์ด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์†์‹ค ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ผ๋ฐ˜ํ™”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. o s f n t o = m n 1 ( m w ~ b + n โˆ’ o X n ) ์—ฌ๊ธฐ์„œ๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์•„์ง ์ตœ์ ์˜ ์†์‹ค ํ•จ์ˆ˜๋ผ๊ธฐ์—๋Š” ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. GloVe ์—ฐ๊ตฌ์ง„์€ o X k ์—์„œ i ๊ฐ’์ด 0์ด ๋  ์ˆ˜ ์žˆ์Œ์„ ์ง€์ ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์•ˆ ์ค‘ ํ•˜๋‚˜๋Š” o X k ํ•ญ์„ o ( + i) ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•ด๋„ ์—ฌ์ „ํžˆ ํ•ด๊ฒฐ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ ๋Š” ๋งˆ์น˜ DTM์ฒ˜๋Ÿผ ํฌ์†Œ ํ–‰๋ ฌ(Sparse Matrix) ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ๋‹ค๋ถ„ํ•˜๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์—๋Š” ๋งŽ์€ ๊ฐ’์ด 0์ด๊ฑฐ๋‚˜, ๋™์‹œ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ์ ์–ด์„œ ๋งŽ์€ ๊ฐ’์ด ์ž‘์€ ์ˆ˜์น˜๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋นˆ๋„์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ€์ค‘์น˜๋ฅผ ์ฃผ๋Š” ๊ณ ๋ฏผ์„ ํ•˜๋Š” TF-IDF๋‚˜ LSA์™€ ๊ฐ™์€ ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•๋“ค์„ ๋ณธ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. GloVe์˜ ์—ฐ๊ตฌ์ง„์€ ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ์—์„œ ๋™์‹œ ๋“ฑ์žฅ ๋นˆ๋„์˜ ๊ฐ’ i ์ด ๊ต‰์žฅํžˆ ๋‚ฎ์€ ๊ฒฝ์šฐ์—๋Š” ์ •๋ณด์— ๊ฑฐ์˜ ๋„์›€์ด ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ์ฃผ๋Š” ๊ณ ๋ฏผ์„ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ GloVe ์—ฐ๊ตฌํŒ€์ด ์„ ํƒํ•œ ๊ฒƒ์€ ๋ฐ”๋กœ i์˜ ๊ฐ’์— ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๊ฐ€์ค‘์น˜ ํ•จ์ˆ˜(Weighting function) ( i) ๋ฅผ ์†์‹ค ํ•จ์ˆ˜์— ๋„์ž…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. GloVe์— ๋„์ž…๋˜๋Š” ( i) ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. i์˜ ๊ฐ’์ด ์ž‘์œผ๋ฉด ์ƒ๋Œ€์ ์œผ๋กœ ํ•จ์ˆ˜์˜ ๊ฐ’์€ ์ž‘๋„๋ก ํ•˜๊ณ , ๊ฐ’์ด ํฌ๋ฉด ํ•จ์ˆ˜์˜ ๊ฐ’์€ ์ƒ๋Œ€์ ์œผ๋กœ ํฌ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ i ๊ฐ€ ์ง€๋‚˜์น˜๊ฒŒ ๋†’๋‹ค๊ณ  ํ•ด์„œ ์ง€๋‚˜์นœ ๊ฐ€์ค‘์น˜๋ฅผ ์ฃผ์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ ๋˜ํ•œ ํ•จ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์ด ์ •ํ•ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. (์ตœ๋Œ“๊ฐ’์€ 1) ์˜ˆ๋ฅผ ๋“ค์–ด 'It is'์™€ ๊ฐ™์€ ๋ถˆ์šฉ์–ด์˜ ๋™์‹œ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’๋‹ค๊ณ  ํ•ด์„œ ์ง€๋‚˜์นœ ๊ฐ€์ค‘์„ ๋ฐ›์•„์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜์˜ ๊ฐ’์„ ์†์‹ค ํ•จ์ˆ˜์— ๊ณฑํ•ด์ฃผ๋ฉด ๊ฐ€์ค‘์น˜์˜ ์—ญํ• ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜ ( ) ์˜ ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. ( ) m n ( , ( / m x ) / ) ์ตœ์ข…์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ๋ฐ˜ํ™”๋œ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. o s f n t o = m n 1 f ( m) ( m w ~ b + n โˆ’ o X n ์ด์ œ GloVe ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ ๋ฐ ์‹ค์Šตํ•˜๊ณ  ํ›ˆ๋ จ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 5. GloVe ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ ์‹ค์Šต์„ ์œ„ํ•ด ํ”„๋กฌํ”„ํŠธ์—์„œ ์•„๋ž˜ ์ปค๋งจ๋“œ๋กœ GloVe ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install glove_python GloVe์˜ ์ž…๋ ฅ์ด ๋˜๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” '์˜์–ด์™€ ํ•œ๊ตญ์–ด Word2Vec ํ•™์Šตํ•˜๊ธฐ' ์ฑ•ํ„ฐ์—์„œ ์‚ฌ์šฉํ•œ ์˜์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋™์ผํ•œ ์ „์ฒ˜๋ฆฌ๋ฅผ ๋งˆ์น˜๊ณ  ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ result์— ๊ฒฐ๊ณผ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. from glove import Corpus, Glove corpus = Corpus() corpus.fit(result, window=5) # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ GloVe์—์„œ ์‚ฌ์šฉํ•  ๋™์‹œ ๋“ฑ์žฅ ํ–‰๋ ฌ ์ƒ์„ฑ glove = Glove(no_components=100, learning_rate=0.05) glove.fit(corpus.matrix, epochs=20, no_threads=4, verbose=True) glove.add_dictionary(corpus.dictionary) # ํ•™์Šต์— ์ด์šฉํ•  ์Šค๋ ˆ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” 4๋กœ ์„ค์ •, ์—ํฌํฌ๋Š” 20. ์ด์ œ ํ•™์Šต์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. glove.most_similar()๋Š” ์ž…๋ ฅ ๋‹จ์–ด์˜ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. model_result1=glove.most_similar("man") print(model_result1) [('woman', 0.9621753707315267), ('guy', 0.8860281455579162), ('girl', 0.8609057388487154), ('kid', 0.8383640509911114)] model_result2=glove.most_similar("boy") print(model_result2) [('girl', 0.9436601252235809), ('kid', 0.8400949618225224), ('woman', 0.8397250531245034), ('man', 0.8303093585541573)] model_result3=glove.most_similar("university") print(model_result3) [('harvard', 0.8690162017225468), ('cambridge', 0.8373272000675909), ('mit', 0.8288055170365777), ('stanford', 0.8212712738131419)] model_result4=glove.most_similar("water") print(model_result4) [('air', 0.838286550826724), ('clean', 0.8326093688298345), ('fresh', 0.8232884971285377), ('electricity', 0.8097066570385377)] model_result5=glove.most_similar("physics") print(model_result5) [('chemistry', 0.8379143027061764), ('biology', 0.827856517644139), ('economics', 0.775563255616767), ('finance', 0.7736692309034663)] model_result6=glove.most_similar("muscle") print(model_result6) [('skeletal', 0.7977490484723809), ('tissue', 0.7714119298512192), ('nerve', 0.7477850181231441), ('stem', 0.7222964725687838)] model_result7=glove.most_similar("clean") print(model_result7) [('water', 0.8264213732980569), ('fresh', 0.7850091074483321), ('wind', 0.7711854196846724), ('heat', 0.7646505765422197)] ์‚ฌ์ „ ํ›ˆ๋ จ๋œ GloVe๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์ฐธ๊ณ  ์ž๋ฃŒ https://towardsdatascience.com/light-on-math-ml-intuitive-guide-to-understanding-glove-embeddings-b13b4f19c010 12-07 ๋‚ด๋ถ€ ๋‹จ์–ด๋ฅผ ๊ณ ๋ คํ•˜๋Š” ํŒจ์ŠคํŠธ ํ…์ŠคํŠธ(FastText) ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ํŽ˜์ด์Šค๋ถ์—์„œ ๊ฐœ๋ฐœํ•œ FastText๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Word2Vec ์ดํ›„์— ๋‚˜์˜จ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ž์ฒด๋Š” Word2Vec์˜ ํ™•์žฅ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Word2Vec์™€ FastText์™€์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์ด๋ผ๋ฉด Word2Vec๋Š” ๋‹จ์–ด๋ฅผ ์ชผ๊ฐœ์งˆ ์ˆ˜ ์—†๋Š” ๋‹จ์œ„๋กœ ์ƒ๊ฐํ•œ๋‹ค๋ฉด, FastText๋Š” ํ•˜๋‚˜์˜ ๋‹จ์–ด ์•ˆ์—๋„ ์—ฌ๋Ÿฌ ๋‹จ์–ด๋“ค์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ๋‹จ์–ด. ์ฆ‰, ์„œ๋ธŒ ์›Œ๋“œ(subword)๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 1. ๋‚ด๋ถ€ ๋‹จ์–ด(subword)์˜ ํ•™์Šต FastText์—์„œ๋Š” ๊ฐ ๋‹จ์–ด๋Š” ๊ธ€์ž ๋‹จ์œ„ n-gram์˜ ๊ตฌ์„ฑ์œผ๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. n์„ ๋ช‡์œผ๋กœ ๊ฒฐ์ •ํ•˜๋Š”์ง€์— ๋”ฐ๋ผ์„œ ๋‹จ์–ด๋“ค์ด ์–ผ๋งˆ๋‚˜ ๋ถ„๋ฆฌ๋˜๋Š”์ง€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ n์„ 3์œผ๋กœ ์žก์€ ํŠธ๋ผ์ด ๊ทธ๋žจ(tri-gram)์˜ ๊ฒฝ์šฐ, apple์€ app, ppl, ple๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ์ด๋“ค์„ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํžˆ๋Š” ์‹œ์ž‘๊ณผ ๋์„ ์˜๋ฏธํ•˜๋Š” <, >๋ฅผ ๋„์ž…ํ•˜์—ฌ ์•„๋ž˜์˜ 5๊ฐœ ๋‚ด๋ถ€ ๋‹จ์–ด(subword) ํ† ํฐ์„ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. # n = 3์ธ ๊ฒฝ์šฐ <ap, app, ppl, ple, le> ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์— ์ถ”๊ฐ€์ ์œผ๋กœ ํ•˜๋‚˜๋ฅผ ๋” ๋ฒกํ„ฐํ™”ํ•˜๋Š”๋ฐ, ๊ธฐ์กด ๋‹จ์–ด์— <, ์™€ >๋ฅผ ๋ถ™์ธ ํ† ํฐ์ž…๋‹ˆ๋‹ค. # ํŠน๋ณ„ ํ† ํฐ <apple> ๋‹ค์‹œ ๋งํ•ด n = 3์ธ ๊ฒฝ์šฐ, FastText๋Š” ๋‹จ์–ด apple์— ๋Œ€ํ•ด์„œ ๋‹ค์Œ์˜ 6๊ฐœ์˜ ํ† ํฐ์„ ๋ฒกํ„ฐํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. # n = 3์ธ ๊ฒฝ์šฐ <ap, app, ppl, ple, le>, <apple> ๊ทธ๋Ÿฐ๋ฐ ์‹ค์ œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” n์˜ ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๋ฒ”์œ„๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ๋Š” ๊ฐ๊ฐ 3๊ณผ 6์œผ๋กœ ์„ค์ •๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ตœ์†Ÿ๊ฐ’ = 3, ์ตœ๋Œ“๊ฐ’ = 6์ธ ๊ฒฝ์šฐ๋ผ๋ฉด, ๋‹จ์–ด apple์— ๋Œ€ํ•ด์„œ FastText๋Š” ์•„๋ž˜ ๋‚ด๋ถ€ ๋‹จ์–ด๋“ค์„ ๋ฒกํ„ฐํ™”ํ•ฉ๋‹ˆ๋‹ค. # n = 3 ~ 6์ธ ๊ฒฝ์šฐ <ap, app, ppl, ppl, le>, <app, appl, pple, ple>, <appl, pple>, ..., <apple> ์—ฌ๊ธฐ์„œ ๋‚ด๋ถ€ ๋‹จ์–ด๋“ค์„ ๋ฒกํ„ฐํ™”ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋Š” ์ € ๋‹จ์–ด๋“ค์— ๋Œ€ํ•ด์„œ Word2Vec์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์ด ๋‚ด๋ถ€ ๋‹จ์–ด๋“ค์˜ ๋ฒกํ„ฐ ๊ฐ’์„ ์–ป์—ˆ๋‹ค๋ฉด, ๋‹จ์–ด apple์˜ ๋ฒกํ„ฐ ๊ฐ’์€ ์ € ์œ„ ๋ฒกํ„ฐ ๊ฐ’๋“ค์˜ ์ดํ•ฉ์œผ๋กœ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. apple = <ap + app + ppl + ppl + le> + <app + appl + pple + ple> + <appl + pple> + , ..., +<apple> ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์€ Word2Vec์—์„œ๋Š” ์–ป์„ ์ˆ˜ ์—†์—ˆ๋˜ ๊ฐ•์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 2. ๋ชจ๋ฅด๋Š” ๋‹จ์–ด(Out Of Vocabulary, OOV)์— ๋Œ€ํ•œ ๋Œ€์‘ FastText์˜ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ํ•™์Šตํ•œ ํ›„์—๋Š” ๋ฐ์ดํ„ฐ ์…‹์˜ ๋ชจ๋“  ๋‹จ์–ด์˜ ๊ฐ n-gram์— ๋Œ€ํ•ด์„œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์žฅ์ ์€ ๋ฐ์ดํ„ฐ ์…‹๋งŒ ์ถฉ๋ถ„ํ•œ๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์€ ๋‚ด๋ถ€ ๋‹จ์–ด(Subword)๋ฅผ ํ†ตํ•ด ๋ชจ๋ฅด๋Š” ๋‹จ์–ด(Out Of Vocabulary, OOV)์— ๋Œ€ํ•ด์„œ๋„ ๋‹ค๋ฅธ ๋‹จ์–ด์™€์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ น, FastText์—์„œ birthplace(์ถœ์ƒ์ง€)๋ž€ ๋‹จ์–ด๋ฅผ ํ•™์Šตํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๋‹จ์–ด์—์„œ birth์™€ place๋ผ๋Š” ๋‚ด๋ถ€ ๋‹จ์–ด๊ฐ€ ์žˆ์—ˆ๋‹ค๋ฉด, FastText๋Š” birthplace์˜ ๋ฒกํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ฅด๋Š” ๋‹จ์–ด์— ์ œ๋Œ€๋กœ ๋Œ€์ฒ˜ํ•  ์ˆ˜ ์—†๋Š” Word2Vec, GloVe์™€๋Š” ๋‹ค๋ฅธ ์ ์ž…๋‹ˆ๋‹ค. 3. ๋‹จ์–ด ์ง‘ํ•ฉ ๋‚ด ๋นˆ๋„ ์ˆ˜๊ฐ€ ์ ์—ˆ๋˜ ๋‹จ์–ด(Rare Word)์— ๋Œ€ํ•œ ๋Œ€์‘ Word2Vec์˜ ๊ฒฝ์šฐ์—๋Š” ๋“ฑ์žฅ ๋นˆ๋„ ์ˆ˜๊ฐ€ ์ ์€ ๋‹จ์–ด(rare word)์— ๋Œ€ํ•ด์„œ๋Š” ์ž„๋ฒ ๋”ฉ์˜ ์ •ํ™•๋„๊ฐ€ ๋†’์ง€ ์•Š๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์ ๋‹ค ๋ณด๋‹ˆ ์ •ํ™•ํ•˜๊ฒŒ ์ž„๋ฒ ๋”ฉ์ด ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ FastText์˜ ๊ฒฝ์šฐ, ๋งŒ์•ฝ ๋‹จ์–ด๊ฐ€ ํฌ๊ท€ ๋‹จ์–ด๋ผ๋„, ๊ทธ ๋‹จ์–ด์˜ n-gram์ด ๋‹ค๋ฅธ ๋‹จ์–ด์˜ n-gram๊ณผ ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด, Word2Vec๊ณผ ๋น„๊ตํ•˜์—ฌ ๋น„๊ต์  ๋†’์€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์„ ์–ป์Šต๋‹ˆ๋‹ค. FastText๊ฐ€ ๋…ธ์ด์ฆˆ๊ฐ€ ๋งŽ์€ ์ฝ”ํผ์Šค์—์„œ ๊ฐ•์ ์„ ๊ฐ€์ง„ ๊ฒƒ ๋˜ํ•œ ์ด์™€ ๊ฐ™์€ ์ด์œ ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ํ›ˆ๋ จ ์ฝ”ํผ์Šค์— ์˜คํƒ€(Typo)๋‚˜ ๋งž์ถค๋ฒ•์ด ํ‹€๋ฆฐ ๋‹จ์–ด๊ฐ€ ์—†์œผ๋ฉด ์ด์ƒ์ ์ด๊ฒ ์ง€๋งŒ, ์‹ค์ œ ๋งŽ์€ ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ์—๋Š” ์˜คํƒ€๊ฐ€ ์„ž์—ฌ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜คํƒ€๊ฐ€ ์„ž์ธ ๋‹จ์–ด๋Š” ๋‹น์—ฐํžˆ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋งค์šฐ ์ ์œผ๋ฏ€๋กœ ์ผ์ข…์˜ ํฌ๊ท€ ๋‹จ์–ด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, Word2Vec์—์„œ๋Š” ์˜คํƒ€๊ฐ€ ์„ž์ธ ๋‹จ์–ด๋Š” ์ž„๋ฒ ๋”ฉ์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์ง€๋งŒ FastText๋Š” ์ด์— ๋Œ€ํ•ด์„œ๋„ ์ผ์ • ์ˆ˜์ค€์˜ ์„ฑ๋Šฅ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์–ด apple๊ณผ ์˜คํƒ€๋กœ p๋ฅผ ํ•œ ๋ฒˆ ๋” ์ž…๋ ฅํ•œ appple์˜ ๊ฒฝ์šฐ์—๋Š” ์‹ค์ œ๋กœ ๋งŽ์€ ๊ฐœ์ˆ˜์˜ ๋™์ผํ•œ n-gram์„ ๊ฐ€์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 4. ์‹ค์Šต์œผ๋กœ ๋น„๊ตํ•˜๋Š” Word2Vec Vs. FastText ๊ฐ„๋‹จํ•œ ์‹ค์Šต์„ ํ†ตํ•ด Word2Vec์™€ FastText์˜ ์ฐจ์ด๋ฅผ ๋น„๊ตํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹จ, ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋Š” Word2Vec๋ฅผ ์‹ค์Šตํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ–ˆ๋˜ ์ด์ „ ์ฑ•ํ„ฐ์˜ ๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 1) Word2Vec ์šฐ์„ , ์ด์ „ Word2Vec์˜ ์‹ค์Šต( https://wikidocs.net/50739 )์˜ ์ „์ฒ˜๋ฆฌ ์ฝ”๋“œ์™€ Word2Vec ํ•™์Šต ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์ˆ˜ํ–‰ํ–ˆ์Œ์„ ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋ฅผ ์ฐพ์•„๋‚ด๋Š” ์ฝ”๋“œ์— ์ด๋ฒˆ์—๋Š” electrofishing์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. model.wv.most_similar("electrofishing") ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์ •์ƒ ์ž‘๋™ํ•˜์ง€ ์•Š๊ณ  ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. KeyError: "word 'electrofishing' not in vocabulary" ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์— electrofishing์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ Word2Vec๋Š” ํ•™์Šต ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋‹จ์–ด. ์ฆ‰, ๋ชจ๋ฅด๋Š” ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์–ด์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 2) FastText ์ด๋ฒˆ์—๋Š” ์ „์ฒ˜๋ฆฌ ์ฝ”๋“œ๋Š” ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ณ  Word2Vec ํ•™์Šต ์ฝ”๋“œ๋งŒ FastText ํ•™์Šต ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‹คํ–‰ํ•ด ๋ด…์‹œ๋‹ค. from gensim.models import FastText model = FastText(result, size=100, window=5, min_count=5, workers=4, sg=1) electrofishing์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌ ๋‹จ์–ด๋ฅผ ์ฐพ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. model.wv.most_similar("electrofishing") [('electrolux', 0.7934642434120178), ('electrolyte', 0.78279709815979), ('electro', 0.779127836227417), ('electric', 0.7753111720085144), ('airbus', 0.7648627758026123), ('fukushima', 0.7612422704696655), ('electrochemical', 0.7611693143844604), ('gastric', 0.7483425140380859), ('electroshock', 0.7477173805236816), ('overfishing', 0.7435552477836609)] Word2Vec๋Š” ํ•™์Šตํ•˜์ง€ ์•Š์€ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌํ•œ ๋‹จ์–ด๋ฅผ ์ฐพ์•„๋‚ด์ง€ ๋ชปํ–ˆ์ง€๋งŒ, FastText๋Š” ์œ ์‚ฌํ•œ ๋‹จ์–ด๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 5. ํ•œ๊ตญ์–ด์—์„œ์˜ FastText ํ•œ๊ตญ์–ด์˜ ๊ฒฝ์šฐ์—๋„ OOV ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด FastText๋ฅผ ์ ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์‹œ๋„๋“ค์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. (1) ์Œ์ ˆ ๋‹จ์œ„ ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์Œ์ ˆ ๋‹จ์œ„์˜ ์ž„๋ฒ ๋”ฉ์˜ ๊ฒฝ์šฐ์— n=3์ผ ๋•Œ โ€˜์ž์—ฐ์–ด ์ฒ˜๋ฆฌโ€™๋ผ๋Š” ๋‹จ์–ด์— ๋Œ€ํ•ด n-gram์„ ๋งŒ๋“ค์–ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. <์ž์—ฐ, ์ž์—ฐ์–ด, ์—ฐ์–ด์ฒ˜, ์–ด์ฒ˜๋ฆฌ, ์ฒ˜๋ฆฌ> (2) ์ž๋ชจ ๋‹จ์œ„ ์ด์ œ ๋” ๋‚˜์•„๊ฐ€ ์ž๋ชจ ๋‹จ์œ„(์ดˆ์„ฑ, ์ค‘์„ฑ, ์ข…์„ฑ ๋‹จ์œ„)๋กœ ์ž„๋ฒ ๋”ฉํ•˜๋Š” ์‹œ๋„ ๋˜ํ•œ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์Œ์ ˆ ๋‹จ์œ„๊ฐ€ ์•„๋‹ˆ๋ผ, ์ž๋ชจ ๋‹จ์œ„๋กœ ๊ฐ€๊ฒŒ ๋˜๋ฉด ์˜คํƒ€๋‚˜ ๋…ธ์ด์ฆˆ ์ธก๋ฉด์—์„œ ๋” ๊ฐ•ํ•œ ์ž„๋ฒ ๋”ฉ์„ ๊ธฐ๋Œ€ํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€˜์ž์—ฐ์–ด ์ฒ˜๋ฆฌโ€™๋ผ๋Š” ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ดˆ์„ฑ, ์ค‘์„ฑ, ์ข…์„ฑ์„ ๋ถ„๋ฆฌํ•˜๊ณ , ๋งŒ์•ฝ, ์ข…์„ฑ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด โ€˜_โ€™๋ผ๋Š” ํ† ํฐ์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค๋ฉด โ€˜์ž์—ฐ์–ด ์ฒ˜๋ฆฌโ€™๋ผ๋Š” ๋‹จ์–ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ถ„๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ถ„๋ฆฌ๋œ ๊ฒฐ๊ณผ : ใ…ˆ ใ… _ ใ…‡ ใ…• ใ„ด ใ…‡ ใ…“ _ ใ…Š ใ…“ _ ใ„น ใ…ฃ _ ๊ทธ๋ฆฌ๊ณ  ๋ถ„๋ฆฌ๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ n=3์ผ ๋•Œ, n-gram์„ ์ ์šฉํ•˜์—ฌ, ์ž„๋ฒ ๋”ฉ์„ ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. < ใ…ˆ ใ…, ใ…ˆ ใ… _, ใ… _ ใ…‡, ... ์ค‘๋žต> ์ด์–ด์„œ ์ž๋ชจ ๋‹จ์œ„ FastText๋ฅผ ์‹ค์Šตํ•ดํ•ด๋ด…์‹œ๋‹ค. 12-08 ์ž๋ชจ ๋‹จ์œ„ ํ•œ๊ตญ์–ด FastText ํ•™์Šตํ•˜๊ธฐ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ๋Š” ์œ„ํ‚ค๋…์Šค ์›น ์‚ฌ์ดํŠธ์—์„œ๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌ๋˜์–ด ๊ตฌํ˜„ ์ฝ”๋“œ์™€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ƒ์„ธํ•œ ์„ค๋ช…์€ ์œ ๋ฃŒ E-book์„ ๊ตฌ๋งคํ•˜์‹œ๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 12-09 ํŒŒ์ด ํ† ์น˜(PyTorch)์˜ nn.Embedding() ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์„ ๋งŒ๋“ค์–ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋ฏธ๋ฆฌ ์‚ฌ์ „์— ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(pre-trained word embedding)๋“ค์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ „์ž์— ํ•ด๋‹น๋˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” ์ด๋ฅผ nn.Embedding()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๋Œ€์กฐ๋˜๋Š” ๋ฐฉ๋ฒ•์ธ ์‚ฌ์ „์— ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(pre-trained word embedding)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค. 1. ์ž„๋ฒ ๋”ฉ ์ธต์€ ๋ฃฉ์—… ํ…Œ์ด๋ธ”์ด๋‹ค. ์ž„๋ฒ ๋”ฉ ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž…๋ ฅ ์‹œํ€€์Šค์˜ ๊ฐ ๋‹จ์–ด๋“ค์€ ๋ชจ๋‘ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ๋˜์–ด์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๋‹จ์–ด โ†’ ๋‹จ์–ด์— ๋ถ€์—ฌ๋œ ๊ณ ์œ ํ•œ ์ •์ˆซ๊ฐ’ โ†’ ์ž„๋ฒ ๋”ฉ ์ธต ํ†ต๊ณผ โ†’ ๋ฐ€์ง‘ ๋ฒกํ„ฐ ์ž„๋ฒ ๋”ฉ ์ธต์€ ์ž…๋ ฅ ์ •์ˆ˜์— ๋Œ€ํ•ด ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)๋กœ ๋งคํ•‘ํ•˜๊ณ  ์ด ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋Š” ์ธ๊ณต ์‹ ๊ฒฝ๋ง์˜ ํ•™์Šต ๊ณผ์ •์—์„œ ๊ฐ€์ค‘์น˜๊ฐ€ ํ•™์Šต๋˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํ›ˆ๋ จ๋ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ๋‹จ์–ด๋Š” ๋ชจ๋ธ์ด ํ’€๊ณ ์ž ํ•˜๋Š” ์ž‘์—…์— ๋งž๋Š” ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฐ€์ง‘ ๋ฒกํ„ฐ๋ฅผ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ •์ˆ˜๋ฅผ ๋ฐ€์ง‘ ๋ฒกํ„ฐ ๋˜๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋งคํ•‘ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์–ด๋–ค ์˜๋ฏธ์ผ๊นŒ์š”? ํŠน์ • ๋‹จ์–ด์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋ฅผ ์ธ๋ฑ์Šค๋กœ ๊ฐ€์ง€๋Š” ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฃฉ์—… ํ…Œ์ด๋ธ”์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ…Œ์ด๋ธ”์€ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒํผ์˜ ํ–‰์„ ๊ฐ€์ง€๋ฏ€๋กœ ๋ชจ๋“  ๋‹จ์–ด๋Š” ๊ณ ์œ ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๋‹จ์–ด great์ด ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ํ›„ ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ ํ•ด๋‹น ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๊บผ๋‚ด์˜ค๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด 4๋กœ ์„ค์ •๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹จ์–ด great์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ 1,918์˜ ์ •์ˆ˜๋กœ ์ธ์ฝ”๋”ฉ์ด ๋˜์—ˆ๊ณ  ๊ทธ์— ๋”ฐ๋ผ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒํผ์˜ ํ–‰์„ ๊ฐ€์ง€๋Š” ํ…Œ์ด๋ธ”์—์„œ ์ธ๋ฑ์Šค 1,918๋ฒˆ์— ์œ„์น˜ํ•œ ํ–‰์„ ๋‹จ์–ด great์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ๋ชจ๋ธ์˜ ์ž…๋ ฅ์ด ๋˜๊ณ , ์—ญ์ „ํŒŒ ๊ณผ์ •์—์„œ ๋‹จ์–ด great์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์ด ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ๋ฃฉ์—… ํ…Œ์ด๋ธ”์˜ ๊ฐœ๋…์„ ์ด๋ก ์ ์œผ๋กœ ์šฐ์„  ์ ‘ํ•˜๊ณ , ์ฒ˜์Œ ํŒŒ์ด ํ† ์น˜๋ฅผ ๋ฐฐ์šธ ๋•Œ ์–ด๋–ค ๋ถ„๋“ค์€ ์ž„๋ฒ ๋”ฉ ์ธต์˜ ์ž…๋ ฅ์ด ์›-ํ•ซ ๋ฒกํ„ฐ๊ฐ€ ์•„๋‹ˆ์–ด๋„ ๋™์ž‘ํ•œ๋‹ค๋Š” ์ ์— ํ—ท๊ฐˆ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜๋Š” ๋‹จ์–ด๋ฅผ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ๋ฐ”๊พธ๊ณ  ์›-ํ•ซ ๋ฒกํ„ฐ๋กœ ํ•œ ๋ฒˆ ๋” ๋ฐ”๊พธ๊ณ  ๋‚˜์„œ ์ž„๋ฒ ๋”ฉ ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋‹จ์–ด๋ฅผ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ๋งŒ ๋ฐ”๊พผ ์ฑ„๋กœ ์ž„๋ฒ ๋”ฉ ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ๋ฃฉ์—… ํ…Œ์ด๋ธ” ๋œ ๊ฒฐ๊ณผ์ธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ๋ฃฉ์—… ํ…Œ์ด๋ธ” ๊ณผ์ •์„ nn.Embedding()์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ตฌํ˜„ํ•ด ๋ณด๋ฉด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ž„์˜์˜ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค๊ณ  ๊ฐ ๋‹จ์–ด์— ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. train_data = 'you need to know how to code' # ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๋‹จ์–ด๋“ค์˜ ์ง‘ํ•ฉ์ธ ๋‹จ์–ด ์ง‘ํ•ฉ ์ƒ์„ฑ. word_set = set(train_data.split()) # ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ๋งคํ•‘. vocab = {word: i+2 for i, word in enumerate(word_set)} vocab['<unk>'] = 0 vocab['<pad>'] = 1 print(vocab) {'code': 2, 'you': 3, 'know': 4, 'to': 5, 'need': 6, 'how': 7, '<unk>': 0, '<pad>': 1} ์ด์ œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ–‰์œผ๋กœ ๊ฐ€์ง€๋Š” ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์—ฌ๊ธฐ์„œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 3์œผ๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. # ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒํผ์˜ ํ–‰์„ ๊ฐ€์ง€๋Š” ํ…Œ์ด๋ธ” ์ƒ์„ฑ. embedding_table = torch.FloatTensor([ [ 0.0, 0.0, 0.0], [ 0.0, 0.0, 0.0], [ 0.2, 0.9, 0.3], [ 0.1, 0.5, 0.7], [ 0.2, 0.1, 0.8], [ 0.4, 0.1, 0.1], [ 0.1, 0.8, 0.9], [ 0.6, 0.1, 0.1]]) ์ด์ œ ์ž„์˜์˜ ๋ฌธ์žฅ 'you need to run'์— ๋Œ€ํ•ด์„œ ๋ฃฉ์—… ํ…Œ์ด๋ธ”์„ ํ†ตํ•ด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ๊ฐ€์ ธ์™€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. sample = 'you need to run'.split() idxes = [] # ๊ฐ ๋‹จ์–ด๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ for word in sample: try: idxes.append(vocab[word]) # ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด์ผ ๊ฒฝ์šฐ <unk>๋กœ ๋Œ€์ฒด๋œ๋‹ค. except KeyError: idxes.append(vocab['<unk>']) idxes = torch.LongTensor(idxes) # ๊ฐ ์ •์ˆ˜๋ฅผ ์ธ๋ฑ์Šค๋กœ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์—์„œ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค. lookup_result = embedding_table[idxes, :] print(lookup_result) tensor([[0.1000, 0.5000, 0.7000], [0.1000, 0.8000, 0.9000], [0.4000, 0.1000, 0.1000], [0.0000, 0.0000, 0.0000]]) 2. ์ž„๋ฒ ๋”ฉ ์ธต ์‚ฌ์šฉํ•˜๊ธฐ ์ด์ œ nn.Embedding()์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ๋ฅผ ๋ด…์‹œ๋‹ค. ์šฐ์„  ์ „์ฒ˜๋ฆฌ๋Š” ๋™์ผํ•œ ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. train_data = 'you need to know how to code' # ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๋‹จ์–ด๋“ค์˜ ์ง‘ํ•ฉ์ธ ๋‹จ์–ด ์ง‘ํ•ฉ ์ƒ์„ฑ. word_set = set(train_data.split()) # ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ๋งคํ•‘. vocab = {tkn: i+2 for i, tkn in enumerate(word_set)} vocab['<unk>'] = 0 vocab['<pad>'] = 1 ์ด์ œ nn.Embedding()์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ๊ฐ€๋Šฅํ•œ ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. import torch.nn as nn embedding_layer = nn.Embedding(num_embeddings=len(vocab), embedding_dim=3, padding_idx=1) nn.Embedding์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋ฐ ๊ฐ๊ฐ num_embeddings๊ณผ embedding_dim์ž…๋‹ˆ๋‹ค. num_embeddings : ์ž„๋ฒ ๋”ฉ์„ ํ•  ๋‹จ์–ด๋“ค์˜ ๊ฐœ์ˆ˜. ๋‹ค์‹œ ๋งํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค. embedding_dim : ์ž„๋ฒ ๋”ฉ ํ•  ๋ฒกํ„ฐ์˜ ์ฐจ์›์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•ด์ฃผ๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์ž…๋‹ˆ๋‹ค. padding_idx : ์„ ํƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ธ์ž์ž…๋‹ˆ๋‹ค. ํŒจ๋”ฉ์„ ์œ„ํ•œ ํ† ํฐ์˜ ์ธ๋ฑ์Šค๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. print(embedding_layer.weight) Parameter containing: tensor([[-0.1778, -1.9974, -1.2478], [ 0.0000, 0.0000, 0.0000], [ 1.0921, 0.0416, -0.7896], [ 0.0960, -0.6029, 0.3721], [ 0.2780, -0.4300, -1.9770], [ 0.0727, 0.5782, -3.2617], [-0.0173, -0.7092, 0.9121], [-0.4817, -1.1222, 2.2774]], requires_grad=True) ์•ž์„  ์˜ˆ์ œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์˜ ํ–‰์„ ๊ฐ€์ง€๋Š” ์ž„๋ฒ ๋”ฉ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 12-10 ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Pre-trained Word Embedding) ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ ํŒŒ์ด ํ† ์น˜์˜ nn.Embedding()์„ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ๋•Œ๋กœ๋Š” ์ด๋ฏธ ํ›ˆ๋ จ๋ผ ์žˆ๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ๋ถˆ๋Ÿฌ์„œ ์ด๋ฅผ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ€์กฑํ•œ ์ƒํ™ฉ์ด๋ผ๋ฉด ๋ชจ๋ธ์— ํŒŒ์ด ํ† ์น˜์˜ nn.Embedding()์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋‹ค๋ฅธ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋˜์–ด ์žˆ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์ด ๋‚˜์€ ์„ ํƒ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ์ ๋‹ค๋ฉด ํŒŒ์ด ํ† ์น˜์˜ nn.Embedding()์œผ๋กœ ํ•ด๋‹น ๋ฌธ์ œ์— ์ถฉ๋ถ„ํžˆ ํŠนํ™”๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ํ•ด๋‹น ๋ฌธ์ œ์— ํŠนํ™”๋œ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ๋ณด๋‹ค ์ผ๋ฐ˜์ ์ด๊ณ  ๋ณด๋‹ค ๋งŽ์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ์ด๋ฏธ Word2Vec์ด๋‚˜ GloVe ๋“ฑ์œผ๋กœ ํ•™์Šต๋ผ ์žˆ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์˜ ๊ฐœ์„ ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. !pip install gensim 1. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ import numpy as np from collections import Counter import gensim ๋ฌธ์žฅ์˜ ๊ธ, ๋ถ€์ •์„ ํŒ๋‹จํ•˜๋Š” ๊ฐ์„ฑ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ๋ฌธ์žฅ๊ณผ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๊ธ์ •์ธ ๋ฌธ์žฅ์€ ๋ ˆ์ด๋ธ” 1, ๋ถ€์ •์ธ ๋ฌธ์žฅ์€ ๋ ˆ์ด๋ธ”์ด 0์ž…๋‹ˆ๋‹ค. sentences = ['nice great best amazing', 'stop lies', 'pitiful nerd', 'excellent work', 'supreme quality', 'bad', 'highly respectable'] y_train = [1, 0, 0, 1, 1, 0, 1] ๊ฐ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. tokenized_sentences = [sent.split() for sent in sentences] print('๋‹จ์–ด ํ† ํฐํ™”๋œ ๊ฒฐ๊ณผ :', tokenized_sentences) ๋‹จ์–ด ํ† ํฐํ™”๋œ ๊ฒฐ๊ณผ : [['nice', 'great', 'best', 'amazing'], ['stop', 'lies'], ['pitiful', 'nerd'], ['excellent', 'work'], ['supreme', 'quality'], ['bad'], ['highly', 'respectable']] ํ† ํฐํ™”๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ์šฐ์„  Counter() ๋ชจ๋“ˆ์„ ์ด์šฉํ•˜์—ฌ ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. word_list = [] for sent in tokenized_sentences: for word in sent: word_list.append(word) word_counts = Counter(word_list) print('์ด ๋‹จ์–ด ์ˆ˜ :', len(word_counts)) ์ด ๋‹จ์–ด ์ˆ˜ : 15 ํ˜„์žฌ ์กด์žฌํ•˜๋Š” ์ด ๋‹จ์–ด์˜ ์ˆ˜๋Š” 15๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด ๋‹จ์–ด๋“ค์„ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ๋†’์€ ์ˆœ์„œ๋ถ€ํ„ฐ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. # ๋“ฑ์žฅ ๋นˆ๋„์ˆœ์œผ๋กœ ์ •๋ ฌ vocab = sorted(word_counts, key=word_counts.get, reverse=True) print(vocab) ['nice', 'great', 'best', 'amazing', 'stop', 'lies', 'pitiful', 'nerd', 'excellent', 'work', 'supreme', 'quality', 'bad', 'highly', 'respectable'] nice๊ฐ€ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๋กœ ๊ฐ€์žฅ ๋†’์€ ๋‹จ์–ด์ด๊ณ , ๊ทธ๋‹ค์Œ์€ great, ๊ทธ๋‹ค์Œ์€ best๋กœ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ๋‹จ์–ด๊ฐ€ ์ •๋ ฌ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ด๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์™„์„ฑํ•ด ๋ด…์‹œ๋‹ค. 0๋ฒˆ์€ ํŒจ๋”ฉ ํ† ํฐ์„ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•˜๊ณ , 1๋ฒˆ์€ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” OOV(Out-Of-Vocabulary) ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์‚ฌ์šฉํ•˜๋Š” ์šฉ๋„๋กœ ๊ฐ๊ฐ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. word_to_index = {} word_to_index['<PAD>'] = 0 word_to_index['<UNK>'] = 1 for index, word in enumerate(vocab) : word_to_index[word] = index + 2 vocab_size = len(word_to_index) print('ํŒจ๋”ฉ ํ† ํฐ, UNK ํ† ํฐ์„ ๊ณ ๋ คํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', vocab_size) ํŒจ๋”ฉ ํ† ํฐ, UNK ํ† ํฐ์„ ๊ณ ๋ คํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 17 ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 17์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. print(word_to_index) {'<PAD>': 0, '<UNK>': 1, 'nice': 2, 'great': 3, 'best': 4, 'amazing': 5, 'stop': 6, 'lies': 7, 'pitiful': 8, 'nerd': 9, 'excellent': 10, 'work': 11, 'supreme': 12, 'quality': 13, 'bad': 14, 'highly': 15, 'respectable': 16} ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ด์šฉํ•˜์—ฌ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•  ๊ฒฝ์šฐ์—๋Š” ์ •์ˆ˜ 1์ด ํ• ๋‹น๋˜์ง€๋งŒ ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ•™์Šต ๋ฐ์ดํ„ฐ์— ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํ•ด๋‹น๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. def texts_to_sequences(tokenized_X_data, word_to_index): encoded_X_data = [] for sent in tokenized_X_data: index_sequences = [] for word in sent: try: index_sequences.append(word_to_index[word]) except KeyError: index_sequences.append(word_to_index['<UNK>']) encoded_X_data.append(index_sequences) return encoded_X_data X_encoded = texts_to_sequences(tokenized_sentences, word_to_index) print(X_encoded) [[2, 3, 4, 5], [6, 7], [8, 9], [10, 11], [12, 13], [14], [15, 16]] ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ธก์ •ํ•˜๊ณ , ํ•ด๋‹น ๊ธธ์ด๋กœ ํŒจ๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. max_len = max(len(l) for l in X_encoded) print('์ตœ๋Œ€ ๊ธธ์ด :',max_len) ์ตœ๋Œ€ ๊ธธ์ด : 4 def pad_sequences(sentences, max_len): features = np.zeros((len(sentences), max_len), dtype=int) for index, sentence in enumerate(sentences): if len(sentence) != 0: features[index, :len(sentence)] = np.array(sentence)[:max_len] return features X_train = pad_sequences(X_encoded, max_len=max_len) y_train = np.array(y_train) print('ํŒจ๋”ฉ ๊ฒฐ๊ณผ :') print(X_train) ํŒจ๋”ฉ ๊ฒฐ๊ณผ : [[ 2 3 4 5] [ 6 7 0 0] [ 8 9 0 0] [10 11 0 0] [12 13 0 0] [14 0 0 0] [15 16 0 0]] ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๊ฐ€ 4๋กœ ๋ณ€ํ™˜๋œ ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ nn.Embedding()๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn from torch.optim import Adam from torch.utils.data import DataLoader, TensorDataset class SimpleModel(nn.Module): def __init__(self, vocab_size, embedding_dim): super(SimpleModel, self).__init__() self.embedding = nn.Embedding(vocab_size, embedding_dim) self.flatten = nn.Flatten() self.fc = nn.Linear(embedding_dim * max_len, 1) self.sigmoid = nn.Sigmoid() def forward(self, x): # embedded.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ฌธ์žฅ์˜ ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›) embedded = self.embedding(x) # flattend.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ฌธ์žฅ์˜ ๊ธธ์ด ร— ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›) flattened = self.flatten(embedded) # output.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, 1) output = self.fc(flattened) return self.sigmoid(output) ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋Š” 100์œผ๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') embedding_dim = 100 simple_model = SimpleModel(vocab_size, embedding_dim).to(device) ์ถœ๋ ฅ์ธต์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์ด์šฉํ•œ ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ชจ๋ธ์ด๋ฏ€๋กœ ์†์‹ค ํ•จ์ˆ˜๋กœ๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” nn.BCELoss()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. criterion = nn.BCELoss() optimizer = Adam(simple_model.parameters()) ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์น˜ ํฌ๊ธฐ 2๋กœ ์„ค์ •ํ•œ ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. train_dataset = TensorDataset(torch.tensor(X_train, dtype=torch.long), torch.tensor(y_train, dtype=torch.float32)) train_dataloader = DataLoader(train_dataset, batch_size=2) ๋ฐ์ดํ„ฐ๊ฐ€ 7๊ฐœ์˜€์œผ๋ฏ€๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ 2๋กœ ๋ฌถ์œผ๋ฉด ์ด ๋ฌถ์Œ์€ 4๊ฐœ(2๊ฐœ, 2๊ฐœ, 2๊ฐœ, 1๊ฐœ)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. print(len(train_dataloader)) ์ด 10๋ฒˆ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. for epoch in range(10): for inputs, targets in train_dataloader: # inputs.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ฌธ์žฅ ๊ธธ์ด) # targets.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ) inputs, targets = inputs.to(device), targets.to(device) optimizer.zero_grad() # outputs.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ) outputs = simple_model(inputs).view(-1) loss = criterion(outputs, targets) loss.backward() optimizer.step() print(f"Epoch {epoch+1}, Loss: {loss.item()}") Epoch 1, Loss: 0.46478426456451416 Epoch 2, Loss: 0.4502693712711334 Epoch 3, Loss: 0.40378859639167786 Epoch 4, Loss: 0.3481767177581787 Epoch 5, Loss: 0.2946138083934784 Epoch 6, Loss: 0.24818778038024902 Epoch 7, Loss: 0.21027232706546783 Epoch 8, Loss: 0.18025405704975128 Epoch 9, Loss: 0.15673018991947174 Epoch 10, Loss: 0.13819283246994019 2. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๊ตฌ๊ธ€์—์„œ ์‚ฌ์ „ ํ•™์Šต์‹œํ‚จ Word2Vec ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ด…์‹œ๋‹ค. ์šฐ์„  ๊ตฌ๊ธ€์—์„œ ์‚ฌ์ „ ํ•™์Šต์‹œํ‚จ Word2Vec ๋ชจ๋ธ์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. !wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc? export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1Av37IVBQAAntSe1X3MOAl5gvowQzd2_j' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1Av37IVBQAAntSe1X3MOAl5gvowQzd2_j" -O GoogleNews-vectors-negative300.bin.gz && rm -rf /tmp/cookies.txt ๋จธ์‹  ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ gensim์„ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ชจ๋ธ์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. # ๊ตฌ๊ธ€์˜ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2vec ๋ชจ๋ธ์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. word2vec_model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin.gz', binary=True) ์œ„ ๋ชจ๋ธ์€ ๊ฐ ๋ฒกํ„ฐ๊ฐ€ 300์ฐจ์›์œผ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ ํฌ๊ธฐ์˜ ํ–‰๊ณผ 300๊ฐœ์˜ ์—ด์„ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ํ–‰๋ ฌ์˜ ๊ฐ’์€ ์ „๋ถ€ 0์œผ๋กœ ์ฑ„์›๋‹ˆ๋‹ค. ์ด ํ–‰๋ ฌ์— ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๊ฐ’์„ ๋„ฃ์–ด์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. embedding_matrix = np.zeros((vocab_size, 300)) print('์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ :', embedding_matrix.shape) ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์˜ ํฌ๊ธฐ : (17, 300) word2vec_model์—์„œ ํŠน์ • ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ๋ฆฌํ„ด ๋ฐ›์„ ํ…๋ฐ, ๋งŒ์•ฝ word2vec_model์— ํŠน์ • ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ์—†๋‹ค๋ฉด None์„ ๋ฆฌํ„ดํ•˜๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜ get_vector()๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. def get_vector(word): if word in word2vec_model: return word2vec_model[word] else: return None ๋‹จ์–ด ์ง‘ํ•ฉ์œผ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ 1๊ฐœ์”ฉ ํ˜ธ์ถœํ•˜์—ฌ word2vec_model์— ํ•ด๋‹น ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ None์ด ์•„๋‹ˆ๋ผ๋ฉด ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์— ํ•ด๋‹น ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค ์œ„์น˜์˜ ํ–‰์— ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. # <PAD>๋ฅผ ์œ„ํ•œ 0๋ฒˆ๊ณผ <UNK>๋ฅผ ์œ„ํ•œ 1๋ฒˆ์€ ์‹ค์ œ ๋‹จ์–ด๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ๋งคํ•‘์—์„œ ์ œ์™ธ for word, i in word_to_index.items(): if i > 2: temp = get_vector(word) if temp is not None: embedding_matrix[i] = temp ํ˜„์žฌ ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์˜ 17๊ฐœ์˜ ๋‹จ์–ด์™€ ๋งคํ•‘๋˜๋Š” ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์ด ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค. 0๋ฒˆ ๋‹จ์–ด๋Š” ํŒจ๋”ฉ์„ ์œ„ํ•œ ์šฉ๋„์ด๋ฏ€๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’์ด ๋ถˆํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ ์ดˆ๊นƒ๊ฐ’์ธ 0๋ฒกํ„ฐ๋กœ ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. embedding_matrix์˜ 0๋ฒˆ ์œ„์น˜์˜ ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # <PAD>๋‚˜ <UNK>์˜ ๊ฒฝ์šฐ๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์•„์„œ 0๋ฒกํ„ฐ์ž„ print(embedding_matrix[0]) [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] 0์ด 300๊ฐœ ์ฑ„์›Œ์ง„ ๋ฒกํ„ฐ์ž„์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋‹ค๋ฅธ ๋‹จ์–ด๋“ค๋„ ์ œ๋Œ€๋กœ ๋งคํ•‘์ด ๋๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ๊นŒ์š”? ๊ธฐ์กด์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋‹จ์–ด 'great'๊ฐ€ ์ •์ˆ˜๋กœ ๋ช‡ ๋ฒˆ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. word_to_index['great'] 3๋ฒˆ ์ž„์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ์„œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ word2vec_model์—์„œ์˜ 'great' ๋ฒกํ„ฐ์™€ ํ˜„์žฌ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ๋งคํ•‘๋œ embedding_matrix์˜ 3๋ฒˆ ๋ฒกํ„ฐ๊ฐ€ ๋™์ผํ•œ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # word2vec_model์—์„œ 'great'์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ # embedding_matrix[3]์ด ์ผ์น˜ํ•˜๋Š”์ง€ ์ฒดํฌ np.all(word2vec_model['great'] == embedding_matrix[3]) True ๋™์ผํ•œ ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ˜„์žฌ 3๋ฒˆ ์œ„์น˜์— ๋‹จ์–ด 'great' ๋ฒกํ„ฐ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ํ• ๋‹น๋˜์—ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์ด์šฉํ•œ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. class PretrainedEmbeddingModel(nn.Module): def __init__(self, vocab_size, embedding_dim): super(PretrainedEmbeddingModel, self).__init__() self.embedding = nn.Embedding(vocab_size, embedding_dim) self.embedding.weight = nn.Parameter(torch.tensor(embedding_matrix, dtype=torch.float32)) self.embedding.weight.requires_grad = True self.flatten = nn.Flatten() self.fc = nn.Linear(embedding_dim * max_len, 1) self.sigmoid = nn.Sigmoid() def forward(self, x): embedded = self.embedding(x) flattened = self.flatten(embedded) output = self.fc(flattened) return self.sigmoid(output) ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋Š” embedding_matrix์—์„œ ์ด๋ฏธ ์ •ํ•ด์ง„ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ธ 300์œผ๋กœ ํ•ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. pretraiend_embedding_model = PretrainedEmbeddingModel(vocab_size, 300).to(device) ์ถœ๋ ฅ์ธต์— ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ฅผ ์ด์šฉํ•œ ์ด์ง„ ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ชจ๋ธ์ด๋ฏ€๋กœ ์†์‹ค ํ•จ์ˆ˜๋กœ๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” nn.BCELoss()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. criterion = nn.BCELoss() optimizer = Adam(pretraiend_embedding_model.parameters()) ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์น˜ ํฌ๊ธฐ 2๋กœ ์„ค์ •ํ•œ ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. train_dataset = TensorDataset(torch.tensor(X_train, dtype=torch.long), torch.tensor(y_train, dtype=torch.float32)) train_dataloader = DataLoader(train_dataset, batch_size=2) ๋ฐ์ดํ„ฐ๊ฐ€ 7๊ฐœ์˜€์œผ๋ฏ€๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ 2๋กœ ๋ฌถ์œผ๋ฉด ์ด ๋ฌถ์Œ์€ 4๊ฐœ(2๊ฐœ, 2๊ฐœ, 2๊ฐœ, 1๊ฐœ)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. print(len(train_dataloader)) ์ด 10๋ฒˆ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. for epoch in range(10): for inputs, targets in train_dataloader: # inputs.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ฌธ์žฅ ๊ธธ์ด) # targets.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ) inputs, targets = inputs.to(device), targets.to(device) optimizer.zero_grad() # outputs.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ) outputs = pretraiend_embedding_model(inputs).view(-1) loss = criterion(outputs, targets) loss.backward() optimizer.step() print(f"Epoch {epoch+1}, Loss: {loss.item()}") Epoch 1, Loss: 0.6623443365097046 Epoch 2, Loss: 0.6018906235694885 Epoch 3, Loss: 0.5419147610664368 Epoch 4, Loss: 0.4856065809726715 Epoch 5, Loss: 0.43379512429237366 Epoch 6, Loss: 0.38664519786834717 Epoch 7, Loss: 0.3440646231174469 Epoch 8, Loss: 0.3058430850505829 Epoch 9, Loss: 0.2717098295688629 Epoch 10, Loss: 0.24136123061180115 ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์ด์šฉํ•˜์—ฌ ๊ธฐ์กด ๋ชจ๋ธ ๋Œ€๋น„ ๋” ๋†’์€ ์„ฑ๋Šฅ์„ ์–ป๋Š” ์˜ˆ์‹œ๋Š” '13-03 ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์ด์šฉํ•œ ์„ฑ๋Šฅ ์ƒ์Šน์‹œํ‚ค๊ธฐ(https://wikidocs.net/217099)' ์‹ค์Šต์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. 12-11 ์—˜๋ชจ(Embeddings from Language Model, ELMo) ๋…ผ๋ฌธ ๋งํฌ : https://aclweb.org/anthology/N18-1202 ELMo(Embeddings from Language Model)๋Š” 2018๋…„์— ์ œ์•ˆ๋œ ์ƒˆ๋กœ์šด ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์ž…๋‹ˆ๋‹ค. ELMo๋ผ๋Š” ์ด๋ฆ„์€ ์„ธ์„œ๋ฏธ ์ŠคํŠธ๋ฆฌํŠธ๋ผ๋Š” ๋ฏธ๊ตญ ์ธํ˜•๊ทน์˜ ์บ๋ฆญํ„ฐ ์ด๋ฆ„์ด๊ธฐ๋„ ํ•œ๋ฐ, ๋’ค์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” BERT๋‚˜ ์ตœ๊ทผ ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ๊ฐ€ ์‚ฌ์šฉํ•œ Big Bird๋ผ๋Š” NLP ๋ชจ๋ธ ๋˜ํ•œ ELMo์— ์ด์–ด ์„ธ์„œ๋ฏธ ์ŠคํŠธ๋ฆฌํŠธ์˜ ์บ๋ฆญํ„ฐ์˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ELMo๋Š” Embeddings from Language Model์˜ ์•ฝ์ž์ž…๋‹ˆ๋‹ค. ํ•ด์„ํ•˜๋ฉด '์–ธ์–ด ๋ชจ๋ธ๋กœ ํ•˜๋Š” ์ž„๋ฒ ๋”ฉ'์ž…๋‹ˆ๋‹ค. ELMo์˜ ๊ฐ€์žฅ ํฐ ํŠน์ง•์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ(Pre-trained language model)์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ELMo์˜ ์ด๋ฆ„์— LM์ด ๋“ค์–ด๊ฐ„ ์ด์œ ์ž…๋‹ˆ๋‹ค. 1. ELMo(Embeddings from Language Model) Bank๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. Bank Account(์€ํ–‰ ๊ณ„์ขŒ)์™€ River Bank(๊ฐ•๋‘‘)์—์„œ์˜ Bank๋Š” ์ „ํ˜€ ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ, Word2Vec์ด๋‚˜ GloVe ๋“ฑ์œผ๋กœ ํ‘œํ˜„๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋“ค์€ ์ด๋ฅผ ์ œ๋Œ€๋กœ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ Word2Vec์ด๋‚˜ GloVe ๋“ฑ์˜ ์ž„๋ฒ ๋”ฉ ๋ฐฉ๋ฒ•๋ก ์œผ๋กœ Bank๋ž€ ๋‹จ์–ด๋ฅผ [0.2 0.8 -1.2]๋ผ๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ์ž„๋ฒ ๋”ฉํ•˜์˜€๋‹ค๊ณ  ํ•˜๋ฉด, ์ด ๋‹จ์–ด๋Š” Bank Account(์€ํ–‰ ๊ณ„์ขŒ)์™€ River Bank(๊ฐ•๋‘‘)์—์„œ์˜ Bank๋Š” ์ „ํ˜€ ๋‹ค๋ฅธ ์˜๋ฏธ์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋‘ ๊ฐ€์ง€ ์ƒํ™ฉ ๋ชจ๋‘์—์„œ [0.2 0.8 -1.2]์˜ ๋ฒกํ„ฐ๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ํ‘œ๊ธฐ์˜ ๋‹จ์–ด๋ผ๋„ ๋ฌธ๋งฅ์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅด๊ฒŒ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์„ฑ๋Šฅ์„ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์‹œ ๋ฌธ๋งฅ์„ ๊ณ ๋ คํ•ด์„œ ์ž„๋ฒ ๋”ฉ์„ ํ•˜๊ฒ ๋‹ค๋Š” ์•„์ด๋””์–ด๊ฐ€ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Contextualized Word Embedding)์ž…๋‹ˆ๋‹ค. 2. biLM(Bidirectional Language Model)์˜ ์‚ฌ์ „ ํ›ˆ๋ จ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์ž‘์—…์ธ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์€๋‹‰์ธต์ด 2๊ฐœ์ธ ์ผ๋ฐ˜์ ์ธ ๋‹จ๋ฐฉํ–ฅ RNN ์–ธ์–ด ๋ชจ๋ธ์˜ ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. RNN ์–ธ์–ด ๋ชจ๋ธ์€ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด ๋‹จ์œ„๋กœ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋ฐ, RNN ๋‚ด๋ถ€์˜ ์€๋‹‰ ์ƒํƒœ t ๋Š” ์‹œ์ (time step)์ด ์ง€๋‚ ์ˆ˜๋ก ์ ์  ์—…๋ฐ์ดํŠธ๋ผ๊ฐ‘๋‹ˆ๋‹ค. ์ด๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ RNN์˜ t ์˜ ๊ฐ’์ด ๋ฌธ์žฅ์˜ ๋ฌธ๋งฅ ์ •๋ณด๋ฅผ ์ ์ฐจ์ ์œผ๋กœ ๋ฐ˜์˜ํ•œ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ELMo๋Š” ์œ„์˜ ๊ทธ๋ฆผ์˜ ์ˆœ๋ฐฉํ–ฅ RNN๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์œ„์˜ ๊ทธ๋ฆผ๊ณผ๋Š” ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฌธ์žฅ์„ ์Šค์บ”ํ•˜๋Š” ์—ญ๋ฐฉํ–ฅ RNN ๋˜ํ•œ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ELMo๋Š” ์–‘์ชฝ ๋ฐฉํ–ฅ์˜ ์–ธ์–ด ๋ชจ๋ธ์„ ๋‘˜ ๋‹ค ํ•™์Šตํ•˜์—ฌ ํ™œ์šฉํ•œ๋‹ค๊ณ  ํ•˜์—ฌ ์ด ์–ธ์–ด ๋ชจ๋ธ์„ biLM(Bidirectional Language Model)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ELMo์—์„œ ๋งํ•˜๋Š” biLM์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์ธต ๊ตฌ์กฐ(Multi-layer)๋ฅผ ์ „์ œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์€๋‹‰์ธต์ด ์ตœ์†Œ 2๊ฐœ ์ด์ƒ์ด๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ์€๋‹‰์ธต์ด 2๊ฐœ์ธ ์ˆœ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ์—ญ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋•Œ biLM์˜ ๊ฐ ์‹œ์ ์˜ ์ž…๋ ฅ์ด ๋˜๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ๋Š” ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ์„ค๋ช…ํ•œ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์„ ์‚ฌ์šฉํ•ด์„œ ์–ป์€ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ(character embedding)์„ ํ†ตํ•ด ์–ป์€ ๋‹จ์–ด ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•œ ์„ค๋ช…์€ 'NLP๋ฅผ ์œ„ํ•œ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง' ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃจ๋Š” ๋‚ด์šฉ์œผ๋กœ ์—ฌ๊ธฐ์„œ๋Š” ์ž„๋ฒ ๋”ฉ์ธต, Word2Vec ๋“ฑ ์™ธ์— ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ์‹๋„ ์žˆ๋‹ค๊ณ ๋งŒ ์•Œ์•„๋‘ก์‹œ๋‹ค. ๋ฌธ์ž ์ž„๋ฒ ๋”ฉ์€ ๋งˆ์น˜ ์„œ๋ธŒ ๋‹จ์–ด(subword)์˜ ์ •๋ณด๋ฅผ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ฌธ๋งฅ๊ณผ ์ƒ๊ด€์—†์ด dog๋ž€ ๋‹จ์–ด์™€ doggy๋ž€ ๋‹จ์–ด์˜ ์—ฐ๊ด€์„ฑ์„ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ๋ฐฉ๋ฒ•์€ OOV์—๋„ ๊ฒฌ๊ณ ํ•œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ ์•ž์„œ ์„ค๋ช…ํ•œ ์–‘๋ฐฉํ–ฅ RNN๊ณผ ELMo์—์„œ์˜ biLM์€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ RNN์€ ์ˆœ๋ฐฉํ–ฅ RNN์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์—ญ๋ฐฉํ–ฅ์˜ RNN์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ๋‹ค์Œ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, biLM์˜ ์ˆœ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ์—ญ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์ด๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์–ธ์–ด ๋ชจ๋ธ์„ ๋ณ„๊ฐœ์˜ ๋ชจ๋ธ๋กœ ๋ณด๊ณ  ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 3. biLM์˜ ํ™œ์šฉ biLM์ด ์–ธ์–ด ๋ชจ๋ธ๋ง์„ ํ†ตํ•ด ํ•™์Šต๋œ ํ›„ ELMo๊ฐ€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ biLM์„ ํ†ตํ•ด ์ž…๋ ฅ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ ์ž„๋ฒ ๋”ฉํ•˜๊ธฐ ์œ„ํ•œ ๊ณผ์ •์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” play๋ž€ ๋‹จ์–ด๊ฐ€ ์ž„๋ฒ ๋”ฉ์ด ๋˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฐ€์ • ํ•˜์— ELMo๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. play๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ž„๋ฒ ๋”ฉ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ELMo๋Š” ์œ„์˜ ์ ์„ ์˜ ์‚ฌ๊ฐํ˜• ๋‚ด๋ถ€์˜ ๊ฐ ์ธต์˜ ๊ฒฐ๊ด๊ฐ’์„ ์žฌ๋ฃŒ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ํ•ด๋‹น ์‹œ์ (time step)์˜ BiLM์˜ ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ˆœ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ๊ณผ ์—ญ๋ฐฉํ–ฅ ์–ธ์–ด ๋ชจ๋ธ์˜ ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์„ ์—ฐ๊ฒฐ(concatenate) ํ•˜๊ณ  ์ถ”๊ฐ€ ์ž‘์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์ด๋ž€ ์ฒซ ๋ฒˆ์งธ๋Š” ์ž„๋ฒ ๋”ฉ ์ธต์„ ๋งํ•˜๋ฉฐ, ๋‚˜๋จธ์ง€ ์ธต์€ ๊ฐ ์ธต์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ELMo์˜ ์ง๊ด€์ ์ธ ์•„์ด๋””์–ด๋Š” ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์ด ๊ฐ€์ง„ ์ •๋ณด๋Š” ์ „๋ถ€ ์„œ๋กœ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ์„ ๊ฒƒ์ด๋ฏ€๋กœ, ์ด๋“ค์„ ๋ชจ๋‘ ํ™œ์šฉํ•œ๋‹ค๋Š” ์ ์— ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ELMo๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์–ป๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 1) ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์„ ์—ฐ๊ฒฐ(concatenate) ํ•œ๋‹ค. 2) ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’ ๋ณ„๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ์ค€๋‹ค. ์ด ๊ฐ€์ค‘์น˜๋ฅผ ์—ฌ๊ธฐ์„œ๋Š” 1 s, 3 ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. 3) ๊ฐ ์ธต์˜ ์ถœ๋ ฅ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•œ๋‹ค. 2) ๋ฒˆ๊ณผ 3) ๋ฒˆ์˜ ๋‹จ๊ณ„๋ฅผ ์š”์•ฝํ•˜์—ฌ ๊ฐ€์ค‘ํ•ฉ(Weighted Sum)์„ ํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4) ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์Šค์นผ๋ผ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ณฑํ•œ๋‹ค. ์ด ์Šค์นผ๋ผ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์—ฌ๊ธฐ์„œ๋Š”์ด๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋ ‡๊ฒŒ ์™„์„ฑ๋œ ๋ฒกํ„ฐ๋ฅผ ELMo ํ‘œํ˜„(representation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” ELMo ํ‘œํ˜„์„ ์–ป๊ธฐ ์œ„ํ•œ ๊ณผ์ •์ด๊ณ  ์ด์ œ ELMo๋ฅผ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์€ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜, ์งˆ์˜์‘๋‹ต ์‹œ์Šคํ…œ ๋“ฑ์˜ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์„ ํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ELMo ํ‘œํ˜„์„ ์–ด๋–ป๊ฒŒ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ELMo ํ‘œํ˜„์„ ๊ธฐ์กด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์„ ์œ„ํ•ด์„œ GloVe์™€ ๊ฐ™์€ ๊ธฐ์กด์˜ ๋ฐฉ๋ฒ•๋ก ์„ ์‚ฌ์šฉํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์ค€๋น„ํ–ˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋•Œ, GloVe๋ฅผ ์‚ฌ์šฉํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋งŒ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ด๋ ‡๊ฒŒ ์ค€๋น„๋œ ELMo ํ‘œํ˜„์„ GloVe ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์™€ ์—ฐ๊ฒฐ(concatenate) ํ•ด์„œ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋•Œ biLM์˜ ๊ฐ€์ค‘์น˜๋Š” ๊ณ ์ •์‹œํ‚ค๊ณ , ์œ„์—์„œ ์‚ฌ์šฉํ•œ 1 s, 3 ฮณ ๋Š” ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ํ•™์Šต๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ELMo ํ‘œํ˜„์ด ๊ธฐ์กด์˜ GloVe ๋“ฑ๊ณผ ๊ฐ™์€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์™€ ํ•จ๊ป˜ NLP ํƒœ์Šคํฌ์˜ ์ž…๋ ฅ์ด ๋˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 12-12 ๋ฌธ์„œ ๋ฒกํ„ฐ๋ฅผ ์ด์šฉํ•œ ์ถ”์ฒœ ์‹œ์Šคํ…œ(Recommendation System using Document Embedding) ๋ฌธ์„œ๋“ค์„ ๊ณ ์ •๋œ ๊ธธ์ด์˜ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค๋ฉด ๋ฒกํ„ฐ ๊ฐ„ ๋น„๊ต๋กœ ๋ฌธ์„œ๋“ค์„ ์„œ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฌธ์„œ๋ฅผ ๋ฌธ์„œ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด๋ฏธ ๊ตฌํ˜„๋œ ํŒจํ‚ค์ง€์ธ Doc2Vec์ด๋‚˜ Sent2Vec ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šตํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์กด์žฌํ•˜์ง€๋งŒ, ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์–ป์€ ๋’ค ๋ฌธ์„œ์— ์กด์žฌํ•˜๋Š” ๋‹จ์–ด ๋ฒกํ„ฐ๋“ค์˜ ํ‰๊ท ์„ ๋ฌธ์„œ ๋ฒกํ„ฐ๋กœ ๊ฐ„์ฃผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฌธ์„œ ๋‚ด ๊ฐ ๋‹จ์–ด๋“ค์„ Word2Vec์„ ํ†ตํ•ด ๋‹จ์–ด ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ์ด๋“ค์˜ ํ‰๊ท ์œผ๋กœ ๋ฌธ์„œ ๋ฒกํ„ฐ๋ฅผ ์–ป์–ด ์„ ํ˜ธํ•˜๋Š” ๋„์„œ์™€ ์œ ์‚ฌํ•œ ๋„์„œ๋ฅผ ์ฐพ์•„์ฃผ๋Š” ๋„์„œ ์ถ”์ฒœ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ์ด๋ฒˆ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ์ฑ…์˜ ์ด๋ฏธ์ง€์™€ ์ฑ…์˜ ์ค„๊ฑฐ๋ฆฌ๋ฅผ ํฌ๋กค๋ง ํ•œ ๋ฐ์ดํ„ฐ๋กœ ์•„๋ž˜์˜ ๋งํฌ์—์„œ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://drive.google.com/file/d/15Q7DZ7xrJsI2Hji-WbkU9j1mwnODBd5A/view?usp=sharing # ํ˜„์žฌ ์ฝ”๋“œ๊ฐ€ gensim 3.6.0 ๋ฒ„์ „์—์„œ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. import urllib.request import pandas as pd import numpy as np import matplotlib.pyplot as plt import requests import re from PIL import Image from io import BytesIO from nltk.tokenize import RegexpTokenizer import nltk from gensim.models import Word2Vec from gensim.models import KeyedVectors from nltk.corpus import stopwords from sklearn.metrics.pairwise import cosine_similarity ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•˜๊ณ  ์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/09.%20Word%20Embedding/dataset/data.csv", filename="data.csv") df = pd.read_csv("data.csv") print('์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜ :',len(df)) ์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜ : 2382 ์ƒ์œ„ 5๊ฐœ์˜ ํ–‰๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. df[:5] ๋ถˆํ•„์š”ํ•œ ์—ด๋“ค์ด ์กด์žฌํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ์ค„๊ฑฐ๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” ์—ด์ธ 'Desc ์—ด'์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์—ด์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ Word2Vec์„ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์—ด์— ๋Œ€ํ•ด์„œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  'cleaned'๋ผ๋Š” ์—ด์— ์ €์žฅํ•ด ๋ด…์‹œ๋‹ค. def _removeNonAscii(s): return "".join(i for i in s if ord(i)<128) def make_lower_case(text): return text.lower() def remove_stop_words(text): text = text.split() stops = set(stopwords.words("english")) text = [w for w in text if not w in stops] text = " ".join(text) return text def remove_html(text): html_pattern = re.compile('<.*?>') return html_pattern.sub(r'', text) def remove_punctuation(text): tokenizer = RegexpTokenizer(r'[a-zA-Z]+') text = tokenizer.tokenize(text) text = " ".join(text) return text df['cleaned'] = df['Desc'].apply(_removeNonAscii) df['cleaned'] = df.cleaned.apply(make_lower_case) df['cleaned'] = df.cleaned.apply(remove_stop_words) df['cleaned'] = df.cleaned.apply(remove_punctuation) df['cleaned'] = df.cleaned.apply(remove_html) ์ƒ์œ„ 5๊ฐœ์˜ ํ–‰๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. df['cleaned'][:5] 0 know power shifting west east north south pres... 1 following success accidental billionaires mone... 2 tap power social software networks build busin... 3 william j bernstein american financial theoris... 4 amazing book joined steve jobs many akio morit... Name: cleaned, dtype: object ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ ๋นˆ ๊ฐ’์ด ์ƒ๊ธด ํ–‰์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋นˆ ๊ฐ’์ด ์ƒ๊ธด ํ–‰์ด ์žˆ๋‹ค๋ฉด, nan ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ ํ›„์— ํ•ด๋‹น ํ–‰์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. df['cleaned'].replace('', np.nan, inplace=True) df = df[df['cleaned'].notna()] print('์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜ :',len(df)) ์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜ : 2381 ์ „์ฒด ๋ฌธ์„œ์˜ ์ˆ˜๊ฐ€ 1๊ฐœ ์ค„์–ด๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ corpus๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ corpus๋ฅผ ํ†ตํ•ด Word2Vec์„ ํ›ˆ๋ จํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. corpus = [] for words in df['cleaned']: corpus.append(words.split()) 2. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์‚ฌ์šฉํ•˜๊ธฐ Word2Vec์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•™์Šตํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๋ฐ์ดํ„ฐ๊ฐ€ ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์€ ์ƒํ™ฉ์—์„œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ดˆ๊นƒ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2Vec์„ ๋กœ๋“œํ•˜๊ณ  ์ดˆ๊ธฐ ๋‹จ์–ด ๋ฒกํ„ฐ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz", \ filename="GoogleNews-vectors-negative300.bin.gz") word2vec_model = Word2Vec(size = 300, window=5, min_count = 2, workers = -1) word2vec_model.build_vocab(corpus) word2vec_model.intersect_word2vec_format('GoogleNews-vectors-negative300.bin.gz', lockf=1.0, binary=True) word2vec_model.train(corpus, total_examples = word2vec_model.corpus_count, epochs = 15) 3. ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ํ‰๊ท  ๊ตฌํ•˜๊ธฐ ๊ฐ ๋ฌธ์„œ์— ์กด์žฌํ•˜๋Š” ๋‹จ์–ด๋“ค์˜ ๋ฒกํ„ฐ ๊ฐ’์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ ํ•ด๋‹น ๋ฌธ์„œ์˜ ๋ฒกํ„ฐ ๊ฐ’์„ ์—ฐ์‚ฐํ•ด ๋ด…์‹œ๋‹ค. def get_document_vectors(document_list): document_embedding_list = [] # ๊ฐ ๋ฌธ์„œ์— ๋Œ€ํ•ด์„œ for line in document_list: doc2vec = None count = 0 for word in line.split(): if word in word2vec_model.wv.vocab: count += 1 # ํ•ด๋‹น ๋ฌธ์„œ์— ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋“ค์˜ ๋ฒกํ„ฐ ๊ฐ’์„ ๋”ํ•œ๋‹ค. if doc2vec is None: doc2vec = word2vec_model[word] else: doc2vec = doc2vec + word2vec_model[word] if doc2vec is not None: # ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๋ฒกํ„ฐ์˜ ๊ฐ’์„ ๋ฌธ์„œ ๊ธธ์ด๋กœ ๋‚˜๋ˆ ์ค€๋‹ค. doc2vec = doc2vec / count document_embedding_list.append(doc2vec) # ๊ฐ ๋ฌธ์„œ์— ๋Œ€ํ•œ ๋ฌธ์„œ ๋ฒกํ„ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ด return document_embedding_list document_embedding_list = get_document_vectors(df['cleaned']) print('๋ฌธ์„œ ๋ฒกํ„ฐ์˜ ์ˆ˜ :',len(document_embedding_list)) ๋ฌธ์„œ ๋ฒกํ„ฐ์˜ ์ˆ˜ : 2381 4. ์ถ”์ฒœ ์‹œ์Šคํ…œ ๊ตฌํ˜„ํ•˜๊ธฐ ๊ฐ ๋ฌธ์„œ ๋ฒกํ„ฐ ๊ฐ„์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. cosine_similarities = cosine_similarity(document_embedding_list, document_embedding_list) print('์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๋งคํŠธ๋ฆญ์Šค์˜ ํฌ๊ธฐ :',cosine_similarities.shape) ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๋งคํŠธ๋ฆญ์Šค์˜ ํฌ๊ธฐ : (2381, 2381) ์„ ํƒํ•œ ์ฑ…์— ๋Œ€ํ•ด์„œ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€์žฅ ์ค„๊ฑฐ๋ฆฌ๊ฐ€ ์œ ์‚ฌํ•œ 5๊ฐœ์˜ ์ฑ…์„ ์ฐพ์•„๋‚ด๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. def recommendations(title): books = df[['title', 'image_link']] # ์ฑ…์˜ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ์ œ๋ชฉ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ด ๋ฐ›์•„ idx์— ์ €์žฅ. indices = pd.Series(df.index, index = df['title']).drop_duplicates() idx = indices[title] # ์ž…๋ ฅ๋œ ์ฑ…๊ณผ ์ค„๊ฑฐ๋ฆฌ(document embedding)๊ฐ€ ์œ ์‚ฌํ•œ ์ฑ… 5๊ฐœ ์„ ์ •. sim_scores = list(enumerate(cosine_similarities[idx])) sim_scores = sorted(sim_scores, key = lambda x: x[1], reverse = True) sim_scores = sim_scores[1:6] # ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์ฑ… 5๊ถŒ์˜ ์ธ๋ฑ์Šค book_indices = [i[0] for i in sim_scores] # ์ „์ฒด ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์—์„œ ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ํ–‰๋งŒ ์ถ”์ถœ. 5๊ฐœ์˜ ํ–‰์„ ๊ฐ€์ง„๋‹ค. recommend = books.iloc[book_indices].reset_index(drop=True) fig = plt.figure(figsize=(20, 30)) # ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅ for index, row in recommend.iterrows(): response = requests.get(row['image_link']) img = Image.open(BytesIO(response.content)) fig.add_subplot(1, 5, index + 1) plt.imshow(img) plt.title(row['title']) ์ข‹์•„ํ•˜๋Š” ์ฑ… ์ œ๋ชฉ์„ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น ์ฑ…์˜ ๋ฌธ์„œ ๋ฒกํ„ฐ(์ค„๊ฑฐ๋ฆฌ ๋ฒกํ„ฐ)์™€ ์œ ์‚ฌํ•œ ๋ฌธ์„œ ๋ฒกํ„ฐ ๊ฐ’์„ ๊ฐ€์ง„ ์ฑ…๋“ค์„ ์ถ”์ฒœํ•ด ์ค๋‹ˆ๋‹ค. ์ฑ… ์ œ๋ชฉ๊ณผ ํ‘œ์ง€๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. recommendations("The Da Vinci Code") ๋‹ค๋นˆ์น˜ ์ฝ”๋“œ๋Š” ์ž‘๊ฐ€ ๋Œ„ ๋ธŒ๋ผ์šด์˜ ์ž‘ํ’ˆ์ž…๋‹ˆ๋‹ค. ์ถ”์ฒœ๋˜๋Š” ์ž‘ํ’ˆ๋“ค ๋˜ํ•œ 5๊ฐœ ์ค‘ 3๊ฐœ๊ฐ€ ๋Œ„ ๋ธŒ๋ผ์šด์˜ ์ž‘ํ’ˆ๋“ค์ด ์ถ”์ฒœ๋จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์•„๊ฐ€์‚ฌ ํฌ๋ฆฌ์Šคํ‹ฐ์˜ ์• ํฌ๋กœ์ด๋“œ ์‚ด์ธ์‚ฌ๊ฑด๊ณผ ์œ ์‚ฌํ•œ ๋„์„œ๋ฅผ ์ถ”์ฒœ๋ฐ›์•„ ๋ด…์‹œ๋‹ค. recommendations("The Murder of Roger Ackroyd") ์• ํฌ๋กœ์ด๋“œ ์‚ด์ธ์‚ฌ๊ฑด์€ ๋ฏธ์Šคํ„ฐ๋ฆฌ ์Šค๋ฆด๋Ÿฌ ์†Œ์„ค์ธ๋ฐ ์ด์™€ ์œ ์‚ฌํ•œ ์†Œ์„ค๋“ค์ด ์ถ”์ฒœ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 12-13 Doc2Vec์œผ๋กœ ๊ณต์‹œ ์‚ฌ์—…๋ณด๊ณ ์„œ ์œ ์‚ฌ๋„ ๊ณ„์‚ฐํ•˜๊ธฐ Word2Vec์€ ๋‹จ์–ด๋ฅผ ์ž„๋ฒ ๋”ฉํ•˜๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์—ˆ์Šต๋‹ˆ๋‹ค. Doc2Vec์€ Word2Vec์„ ๋ณ€ํ˜•ํ•˜์—ฌ ๋ฌธ์„œ์˜ ์ž„๋ฒ ๋”ฉ์„ ์–ป์„ ์ˆ˜ ์žˆ๋„๋ก ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฌธ ์ œ๋ชฉ๊ณผ ๋…ผ๋ฌธ์˜ ๋งํฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฌธ ์ œ๋ชฉ : Distributed Representations of Sentences and Documents ๋…ผ๋ฌธ ๋งํฌ : https://arxiv.org/abs/1405.4053 Word2Vec๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํŒŒ์ด์ฌ ๋จธ์‹  ๋Ÿฌ๋‹ ํŒจํ‚ค์ง€์ธ Gensim์„ ํ†ตํ•ด์„œ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์ €์ž๊ฐ€ ์ˆ˜์ง‘ํ•ด๋†“์€ ์ „์ž๊ณต์‹œ์‹œ์Šคํ…œ(Dart)์— ์˜ฌ๋ผ์™€ ์žˆ๋Š” ๊ฐ ํšŒ์‚ฌ์˜ ์‚ฌ์—…๋ณด๊ณ ์„œ๋ฅผ Doc2Vec์„ ํ†ตํ•ด์„œ ํ•™์Šต์‹œํ‚ค๊ณ , ํŠน์ • ํšŒ์‚ฌ์™€ ์‚ฌ์—… ๋ณด๊ณ ์„œ๊ฐ€ ์œ ์‚ฌํ•œ ํšŒ์‚ฌ๋“ค์„ ์ฐพ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๊ณต์‹œ ์‚ฌ์—… ๋ณด๊ณ ์„œ ๋กœ๋“œ ๋ฐ ์ „์ฒ˜๋ฆฌ ํ•ด๋‹น ์‹ค์Šต์€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์˜ ์›ํ™œํ•œ ์„ค์น˜๋ฅผ ์œ„ํ•ด์„œ ๊ตฌ๊ธ€์˜ Colab์—์„œ ์ง„ํ–‰ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„๋“ค์˜ ์ปดํ“จํ„ฐ์— Mecab์„ ์„ค์น˜ํ•˜์˜€๊ฑฐ๋‚˜, ๋‹ค๋ฅธ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Colab์—์„œ ํ•˜์ง€ ์•Š๋”๋ผ๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. # dart.csv ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ !wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc? export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1XS0UlE8gNNTRjnL6e64sMacOhtVERIqL' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1XS0UlE8gNNTRjnL6e64sMacOhtVERIqL" -O dart.csv && rm -rf /tmp/cookies.txt # ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab ์„ค์น˜ !pip install konlpy !pip install mecab-python !bash <(curl -s https://raw.githubusercontent.com/konlpy/konlpy/master/scripts/mecab.sh) import pandas as pd from konlpy.tag import Mecab from gensim.models.doc2vec import TaggedDocument from tqdm import tqdm dart.csv ํŒŒ์ผ์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฒฐ์ธก๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์„ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. df = pd.read_csv('/content/dart.csv', sep=',') df = df.dropna() df ์ด 2,295๊ฐœ์˜ ํ–‰๊ณผ 4๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์—ด์€ ์ข…๋ชฉ ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” code ์—ด, ๋‘ ๋ฒˆ์งธ ์—ด์€ ํ•ด๋‹น ์ข…๋ชฉ์ด KOSPI ์ธ์ง€ KOSDAQ ์ธ์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” market ์—ด, ์„ธ ๋ฒˆ์งธ ์—ด์€ ํšŒ์‚ฌ๋ช…์— ํ•ด๋‹นํ•˜๋Š” name ์—ด, ๊ทธ๋ฆฌ๊ณ  ๋„ค ๋ฒˆ์งธ business ์—ด์€ ์šฐ๋ฆฌ๊ฐ€ ํ•™์Šตํ•  ์‚ฌ์—… ๋ณด๊ณ ์„œ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ด์ œ business ์—ด์— ๋Œ€ํ•ด์„œ ํ˜•ํƒœ์†Œ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์ด์™€ ๋™์‹œ์— Doc2Vec ํ•™์Šต์„ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ<NAME>์œผ๋กœ ๋ฐ์ดํ„ฐ์˜<NAME>์„ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. Doc2Vec ํ•™์Šต์„ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ๋ฌธ์„œ์˜ '์ œ๋ชฉ'๊ณผ ๋‹จ์–ด ํ† ํฐ ํ™”๊ฐ€ ๋œ ์ƒํƒœ์˜ ํ•ด๋‹น ๋ฌธ์„œ์˜ '๋ณธ๋ฌธ' ๋‘ ๊ฐ€์ง€๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. TaggedDocument์˜ tags์— ํ•ด๋‹น ๋ฌธ์„œ์˜ '์ œ๋ชฉ'์„, ๊ทธ๋ฆฌ๊ณ  words์— ํ•ด๋‹น ๋ฌธ์„œ์˜ '๋ณธ๋ฌธ'์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด ํ† ํฐํ™” ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๊ณ , ์ด ๊ฒฐ๊ณผ๋ฅผ ์›์†Œ๋กœ ํ•˜๋Š” ํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ์ธ tagged_corpus_list๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. mecab = Mecab() tagged_corpus_list = [] for index, row in tqdm(df.iterrows(), total=len(df)): text = row['business'] tag = row['name'] tagged_corpus_list.append(TaggedDocument(tags=[tag], words=mecab.morphs(text))) print('๋ฌธ์„œ์˜ ์ˆ˜ :', len(tagged_corpus_list)) ๋ฌธ์„œ์˜ ์ˆ˜ : 2295 ์ฒซ ๋ฒˆ์งธ ๋ฌธ์„œ์˜ ์ „์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. tagged_corpus_list[0] TaggedDocument(words=['II', '.', '์‚ฌ์—…', '์˜', '๋‚ด์šฉ', '1', '.', '์‚ฌ์—…', '์˜', '๊ฐœ์š”', '๊ฐ€', '.', '์ผ๋ฐ˜', '์ ', '์ธ', '์‚ฌํ•ญ', '๊ธฐ์—…', 'ํšŒ๊ณ„', '๊ธฐ์ค€', '์„œ', '์ œ', '1110', 'ํ˜ธ', '"', '์—ฐ๊ฒฐ', '์žฌ๋ฌด์ œํ‘œ', '"', '์˜', '์˜ํ•˜', '์—ฌ', '2018', '๋…„', '12', '์›”', '17', '์ผ', '์—', '์„ค๋ฆฝ', 'ํ•œ', '๋™ํ™”', 'ํฌ๋ฆฝํ†ค', '๊ธฐ์—…๊ฐ€', '์ •์‹ ', '์ œ์ผ', 'ํ˜ธ', '์ฐฝ์—…', '๋ฒค์ฒ˜', '์ „๋ฌธ', '์‚ฌ๋ชจ', 'ํˆฌ์ž', 'ํ•ฉ์žํšŒ์‚ฌ', '๋ฅผ', '์ข…์†', 'ํšŒ์‚ฌ', '์—', 'ํŽธ์ž…', 'ํ•˜', '์˜€', '์Šต๋‹ˆ๋‹ค', ... ์ค‘๋žต ... '๋Œ€๊ธฐ', '๊ด€๋ฆฌ', '๊ถŒ', '์—ญ', '์˜', '๋Œ€๊ธฐ', 'ํ™˜๊ฒฝ', '๊ฐœ์„ ', '์—', '๊ด€ํ•œ', 'ํŠน๋ณ„๋ฒ•', '์„', '์ค€', '์ˆ˜', 'ํ•˜', '๊ณ ', '์žˆ', '์Šต๋‹ˆ๋‹ค', '.'], tags=['๋™ํ™”์•ฝํ’ˆ']) TaggedDocument ์•ˆ words์—๋Š” ํ† ํฐํ™”๋œ ์‚ฌ์—… ๋ณด๊ณ ์„œ, tags์—๋Š” ํ•ด๋‹น ๋ฌธ์„œ์˜ ์ œ๋ชฉ์ด ์ €์žฅ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. 2. Doc2Vec ํ•™์Šต ๋ฐ ํ…Œ์ŠคํŠธ ์ด์ œ ๋ชจ๋ธ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์—… ๋ณด๊ณ ์„œ๊ฐ€ ๊ฝค ๊ธด ๋ฌธ์„œ์ธ๋ฐ๋‹ค ํ† ํฐํ™” ์™ธ์— ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ ๋“ฑ ๋ณ„๋„ ์ถ”๊ฐ€ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ €์ž๊ฐ€ Colab์—์„œ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ, ์†Œ์š” ์‹œ๊ฐ„์ด ์ตœ์†Œ 1์‹œ๊ฐ„ ์ด์ƒ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. from gensim.models import doc2vec model = doc2vec.Doc2Vec(vector_size=300, alpha=0.025, min_alpha=0.025, workers=8, window=8) # Vocabulary ๋นŒ๋“œ model.build_vocab(tagged_corpus_list) print(f"Tag Size: {len(model.docvecs.doctags.keys())}", end=' / ') # Doc2Vec ํ•™์Šต model.train(tagged_corpus_list, total_examples=model.corpus_count, epochs=50) # ๋ชจ๋ธ ์ €์žฅ model.save('dart.doc2vec') ์ฝ”๋“œ๋ฅผ ๋‹ค ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚˜๋ฉด 3๊ฐœ์˜ ํŒŒ์ผ์ด ์ƒ๊น๋‹ˆ๋‹ค. dart.doc2vec dart.doc2vec.trainables.syn1neg.npy dart.doc2vec.wv.vectors.npy ์ด์ œ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•ด ๋ด…์‹œ๋‹ค. ํšŒ์‚ฌ ๋™ํ™”์•ฝํ’ˆ๊ณผ ์‚ฌ์—… ๋ณด๊ณ ์„œ๊ฐ€ ์œ ์‚ฌํ•œ ํšŒ์‚ฌ๋“ค์€ ์–ด๋””์ผ๊นŒ์š”? similar_doc = model.docvecs.most_similar('๋™ํ™”์•ฝํ’ˆ') print(similar_doc) [('์ข…๊ทผ๋‹น', 0.5310906171798706), ('์‚ผ์ผ์ œ์•ฝ', 0.5263979434967041), ('์ผ์–‘ ์•ฝํ’ˆ', 0.5260423421859741), ('์˜์ง„์•ฝํ’ˆ', 0.5254894495010376), ('์ œ์ผ์•ฝํ’ˆ', 0.5089458227157593), ('์œ ํ•œ์–‘ํ–‰', 0.5015101432800293), ('๊ตญ์ œ์•ฝํ’ˆ', 0.4985279440879822), ('์‚ผ์•„์ œ์•ฝ', 0.49677950143814087), ('๋™์•„์—์Šคํ‹ฐ', 0.49451446533203125), ('๋Œ€์›…์ œ์•ฝ', 0.48559868335723877)] ์ด ์™ธ์— ๋‹ค๋ฅธ ํ…Œ์ŠคํŠธ์—์„œ๋„ ๊ด€๋ จ ์—…์ข…์˜ ํšŒ์‚ฌ๋“ค์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. similar_doc = model.docvecs.most_similar('ํ•˜์ดํŠธ์ง„๋กœ') print(similar_doc) [('ํ•˜์ดํŠธ์ง„๋กœํ™€๋”ฉ์Šค', 0.8621307611465454), ('๋ฌดํ•™', 0.5214779376983643), ('๋ณดํ•ด์–‘์กฐ', 0.5100635290145874), ('๊ตญ์ˆœ๋‹น', 0.48447638750076294), ('๋กฏ๋ฐ์น ์„ฑ', 0.4617755115032196), ('๊ธˆ๋น„', 0.4156178832054138), ('์‚ผ์–‘ ํŒจํ‚ค์ง•', 0.40965551137924194), ('์‚ผ๊ด‘๊ธ€๋ผ์Šค', 0.40689679980278015), ('๊ฒฝ๋ฐฉ', 0.40154141187667847), ('์˜ค๋š์ด', 0.3958606719970703)] similar_doc = model.docvecs.most_similar('LG์ด๋…ธํ…') print(similar_doc) [('LG์ „์ž', 0.533338725566864), ('LG', 0.523799479007721), ('์‚ผ์„ฑ์ „๊ธฐ', 0.45796477794647217), ('LG๋””์Šคํ”Œ๋ ˆ์ด', 0.4485859274864197), ('์„œ์šธ๋ฐ˜๋„์ฒด', 0.42762115597724915), ('๋ฃจ๋ฉ˜์Šค', 0.42333459854125977), ('์‚ผ์„ฑSDI', 0.4111291170120239), ('ํ์— ์”จ', 0.409035325050354), ('์„œ์šธ๋ฐ”์ด์˜ค ์‹œ์Šค', 0.4087420105934143), ('์‚ผ์„ฑ๊ณต์กฐ', 0.4040142595767975)] similar_doc = model.docvecs.most_similar('๋ฉ”๋ฆฌ์ธ ํ™”์žฌ') print(similar_doc) [('๋ฉ”๋ฆฌ์ธ ๊ธˆ์œต ์ง€์ฃผ', 0.7080470323562622), ('ํ•œํ™”์†ํ•ด๋ณดํ—˜', 0.69782555103302), ('๋กฏ๋ฐ์†ํ•ด๋ณดํ—˜', 0.6945951581001282), ('DB์†ํ•ด๋ณดํ—˜', 0.6699072122573853), ('ํ•œํ™”์ƒ๋ช…', 0.665973961353302), ('ํฅ๊ตญํ™”์žฌ', 0.6471891403198242), ('ํ˜„๋Œ€ํ•ด์ƒ', 0.6267702579498291), ('์ฝ”๋ฆฌ์•ˆ๋ฆฌ', 0.5982924699783325), ('์‚ผ์„ฑํ™”์žฌ', 0.5873793959617615), ('๋™์–‘์ƒ๋ช…', 0.5722818970680237)] similar_doc = model.docvecs.most_similar('์นด์นด์˜ค') print(similar_doc) [('๋„ค์˜ค์œ„์ฆˆ', 0.5055375099182129), ('NAVER', 0.4846588373184204), ('๋„ค์˜ค์œ„์ฆˆ ํ™€๋”ฉ์Šค', 0.47819197177886963), ('ํ“จ์ฒ˜์ŠคํŠธ๋ฆผ๋„คํŠธ์›์Šค', 0.4654642939567566), ('์‹ ํ’์ œ์•ฝ ์šฐ', 0.46335992217063904), ('LG์ƒํ™œ๊ฑด๊ฐ• ์šฐ', 0.4604458212852478), ('๊ธˆํ˜ธ์„์œ ์šฐ', 0.4568769931793213), ('์ปดํˆฌ์Šค', 0.4565160274505615), ('์ฝ”๋ฆฌ์•„์จํ‚คํŠธ 2์šฐ B', 0.45594915747642517), ('์„ธ๋ฐฉ ์šฐ', 0.4553225636482239)] 12-14 ์‹ค์ „! ํ•œ๊ตญ์–ด ์œ„ํ‚คํ”ผ๋””์•„๋กœ Word2Vec ํ•™์Šตํ•˜๊ธฐ ์•„๋ž˜์˜ ์‹ค์Šต์€ ๊ตฌ๊ธ€์˜ Colab์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ™˜๊ฒฝ์—์„œ ์‹ค์Šตํ•˜์…”๋„ ์ƒ๊ด€์€ ์—†์ง€๋งŒ, ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์€ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค์น˜ํ•˜์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1. ์œ„ํ‚คํ”ผ๋””์•„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋ฐ ํ†ตํ•ฉ ์œ„ํ‚คํ”ผ๋””์•„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑ ํ•˜๊ธฐ ์œ„ํ•œ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€์ธ wikiextractor๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install wikiextractor ์œ„ํ‚คํ”ผ๋””์•„ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œ ํ•œ ํ›„์— ์ „์ฒ˜๋ฆฌ์—์„œ ์‚ฌ์šฉํ•  ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ์ธ Mecab์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. # Colab์— Mecab ์„ค์น˜ !git clone https://github.com/SOMJANG/Mecab-ko-for-Google-Colab.git %cd Mecab-ko-for-Google-Colab !bash install_mecab-ko_on_colab190912.sh ์œ„ํ‚คํ”ผ๋””์•„ ๋คํ”„(์œ„ํ‚คํ”ผ๋””์•„ ๋ฐ์ดํ„ฐ)๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. !wget https://dumps.wikimedia.org/kowiki/latest/kowiki-latest-pages-articles.xml.bz2 ์œ„ํ‚ค์ต์ŠคํŠธ๋ž™ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„ํ‚คํ”ผ๋””์•„ ๋คํ”„๋ฅผ ํŒŒ์‹ฑ ํ•ฉ๋‹ˆ๋‹ค. !python -m wikiextractor.WikiExtractor kowiki-latest-pages-articles.xml.bz2 ํ˜„์žฌ ๊ฒฝ๋กœ์— ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ํŒŒ์ผ๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์•„์˜ต๋‹ˆ๋‹ค. %ls images/ LICENSE install_mecab-ko_on_colab190912.sh README.md install_mecab-ko_on_colab_light_210108.sh text/ kowiki-latest-pages-articles.xml.bz2 text๋ผ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์—๋Š” ๋˜ ์–ด๋–ค ๋””๋ ‰ํ„ฐ๋ฆฌ๋“ค์ด ์žˆ๋Š”์ง€ ํŒŒ์ด์ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. import os import re os.listdir('text') ['AG', 'AI', 'AH', 'AC', 'AE', 'AB', 'AA', 'AD', 'AF'] AA๋ผ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ํŒŒ์ผ๋“ค์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. %ls text/AA wiki_00 wiki_12 wiki_24 wiki_36 wiki_48 wiki_60 wiki_72 wiki_84 wiki_96 wiki_01 wiki_13 wiki_25 wiki_37 wiki_49 wiki_61 wiki_73 wiki_85 wiki_97 wiki_02 wiki_14 wiki_26 wiki_38 wiki_50 wiki_62 wiki_74 wiki_86 wiki_98 wiki_03 wiki_15 wiki_27 wiki_39 wiki_51 wiki_63 wiki_75 wiki_87 wiki_99 wiki_04 wiki_16 wiki_28 wiki_40 wiki_52 wiki_64 wiki_76 wiki_88 wiki_05 wiki_17 wiki_29 wiki_41 wiki_53 wiki_65 wiki_77 wiki_89 wiki_06 wiki_18 wiki_30 wiki_42 wiki_54 wiki_66 wiki_78 wiki_90 wiki_07 wiki_19 wiki_31 wiki_43 wiki_55 wiki_67 wiki_79 wiki_91 wiki_08 wiki_20 wiki_32 wiki_44 wiki_56 wiki_68 wiki_80 wiki_92 wiki_09 wiki_21 wiki_33 wiki_45 wiki_57 wiki_69 wiki_81 wiki_93 wiki_10 wiki_22 wiki_34 wiki_46 wiki_58 wiki_70 wiki_82 wiki_94 wiki_11 wiki_23 wiki_35 wiki_47 wiki_59 wiki_71 wiki_83 wiki_95 ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ๋ณ€ํ™˜๋œ ์œ„ํ‚คํ”ผ๋””์•„ ํ•œ๊ตญ์–ด ๋คํ”„๋Š” ์ด 6๊ฐœ์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. AA ~ AF์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋กœ ๊ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‚ด์—๋Š” 'wiki_00 ~ wiki_์•ฝ 90๋‚ด์™ธ์˜ ์ˆซ์ž'์˜ ํŒŒ์ผ๋“ค์ด ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ์—๋Š” ์•ฝ 90์—ฌ ๊ฐœ์˜ ํŒŒ์ผ๋“ค์ด ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ํŒŒ์ผ๋“ค์„ ์—ด์–ด๋ณด๋ฉด ์ด์™€ ๊ฐ™์€ ๊ตฌ์„ฑ์ด ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. <doc id="๋ฌธ์„œ ๋ฒˆํ˜ธ" url="์‹ค์ œ ์œ„ํ‚คํ”ผ๋””์•„ ๋ฌธ์„œ ์ฃผ์†Œ" title="๋ฌธ์„œ ์ œ๋ชฉ"> ๋‚ด์šฉ </doc> ์˜ˆ๋ฅผ ๋“ค์–ด์„œ AA ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ wiki_00 ํŒŒ์ผ์„ ์ฝ์–ด๋ณด๋ฉด, ์ง€๋ฏธ ์นดํ„ฐ์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. <doc id="5" url="https://ko.wikipedia.org/wiki?curid=5" title="์ง€๋ฏธ ์นดํ„ฐ"> ์ง€๋ฏธ ์นดํ„ฐ ์ œ์ž„์Šค ์–ผ "์ง€๋ฏธ" ์นดํ„ฐ ์ฃผ๋‹ˆ์–ด(, 1924๋…„ 10์›” 1์ผ ~ )๋Š” ๋ฏผ์ฃผ๋‹น ์ถœ์‹  ๋ฏธ๊ตญ 39๋ฒˆ์งธ ๋Œ€ํ†ต๋ น(1977๋…„ ~ 1981๋…„)์ด๋‹ค. ์ง€๋ฏธ ์นดํ„ฐ๋Š” ์กฐ์ง€์•„ ์ฃผ ์„ฌํ„ฐ ์นด์šดํ‹ฐ ํ”Œ๋ ˆ์ธ์Šค ๋งˆ์„์—์„œ ํƒœ์–ด๋‚ฌ๋‹ค. ์กฐ์ง€์•„ ๊ณต๊ณผ๋Œ€ํ•™๊ต๋ฅผ ์กธ์—…ํ•˜์˜€๋‹ค. ๊ทธ ํ›„ ํ•ด๊ตฐ์— ๋“ค์–ด๊ฐ€ ์ „ํ•จยท์›์ž๋ ฅยท์ž ์ˆ˜ํ•จ์˜ ์Šน๋ฌด์›์œผ๋กœ ์ผํ•˜์˜€๋‹ค. 1953๋…„ ๋ฏธ๊ตญ ํ•ด๊ตฐ ๋Œ€ ์œ„๋กœ ์˜ˆํŽธํ•˜์˜€๊ณ  ์ดํ›„ ๋•…์ฝฉยท๋ฉดํ™” ๋“ฑ์„ ๊ฐ€๊ฟ” ๋งŽ์€ ๋ˆ์„ ๋ฒŒ์—ˆ๋‹ค. ... ์ดํ•˜ ์ค‘๋žต... </doc> ์ด์ œ ์ด 6๊ฐœ AA ~ AF ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์˜ wiki ์ˆซ์ž ํ˜•ํƒœ์˜ ์ˆ˜๋งŽ์€ ํŒŒ์ผ๋“ค์„ ํ•˜๋‚˜๋กœ ํ†ตํ•ฉํ•˜๋Š” ๊ณผ์ •์„ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. AA ~ AF ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์˜ ๋ชจ๋“  ํŒŒ์ผ๋“ค์˜ ๊ฒฝ๋กœ๋ฅผ ํŒŒ์ด์ฌ์˜ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. def list_wiki(dirname): filepaths = [] filenames = os.listdir(dirname) for filename in filenames: filepath = os.path.join(dirname, filename) if os.path.isdir(filepath): # ์žฌ๊ท€ ํ•จ์ˆ˜ filepaths.extend(list_wiki(filepath)) else: find = re.findall(r"wiki_[0-9][0-9]", filepath) if 0 < len(find): filepaths.append(filepath) return sorted(filepaths) filepaths = list_wiki('text') ์ด ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. len(filepaths) 850 ์ด ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๋Š” 850๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด์ œ output_file.txt๋ผ๋Š” ํŒŒ์ผ์— 850๊ฐœ์˜ ํŒŒ์ผ์„ ์ „๋ถ€ ํ•˜๋‚˜๋กœ ํ•ฉ์นฉ๋‹ˆ๋‹ค. with open("output_file.txt", "w") as outfile: for filename in filepaths: with open(filename) as infile: contents = infile.read() outfile.write(contents) ํŒŒ์ผ์„ ์ฝ๊ณ  10์ค„๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. f = open('output_file.txt', encoding="utf8") i = 0 while True: line = f.readline() if line != '\n': i = i+1 print("%d ๋ฒˆ์งธ ์ค„ :"%i + line) if i==10: break f.close() 1๋ฒˆ์งธ ์ค„ :<doc id="5" url="https://ko.wikipedia.org/wiki? curid=5" title="์ง€๋ฏธ ์นดํ„ฐ"> 2๋ฒˆ์งธ ์ค„ :์ง€๋ฏธ ์นดํ„ฐ 3๋ฒˆ์งธ ์ค„ :์ œ์ž„์Šค ์–ผ ์นดํ„ฐ ์ฃผ๋‹ˆ์–ด(, 1924๋…„ 10์›” 1์ผ ~ )๋Š” ๋ฏผ์ฃผ๋‹น ์ถœ์‹  ๋ฏธ๊ตญ 39๋Œ€ ๋Œ€ํ†ต๋ น (1977๋…„ ~ 1981๋…„)์ด๋‹ค. 4๋ฒˆ์งธ ์ค„ :์ƒ์• . 5๋ฒˆ์งธ ์ค„ :์–ด๋ฆฐ ์‹œ์ ˆ. 6๋ฒˆ์งธ ์ค„ :์ง€๋ฏธ ์นดํ„ฐ๋Š” ์กฐ์ง€์•„์ฃผ ์„ฌํ„ฐ ์นด์šดํ‹ฐ ํ”Œ๋ ˆ์ธ์Šค ๋งˆ์„์—์„œ ํƒœ์–ด๋‚ฌ๋‹ค. 7๋ฒˆ์งธ ์ค„ :์กฐ์ง€์•„ ๊ณต๊ณผ๋Œ€ํ•™๊ต๋ฅผ ์กธ์—…ํ•˜์˜€๋‹ค. ๊ทธ ํ›„ ํ•ด๊ตฐ์— ๋“ค์–ด๊ฐ€ ์ „ํ•จยท์›์ž๋ ฅยท์ž ์ˆ˜ํ•จ์˜ ์Šน๋ฌด์›์œผ๋กœ ์ผํ•˜์˜€๋‹ค. 1953๋…„ ๋ฏธ๊ตญ ํ•ด๊ตฐ ๋Œ€์œ„๋กœ ์˜ˆํŽธํ•˜์˜€๊ณ  ์ดํ›„ ๋•…์ฝฉยท๋ฉดํ™” ๋“ฑ์„ ๊ฐ€๊ฟ” ๋งŽ์€ ๋ˆ์„ ๋ฒŒ์—ˆ๋‹ค. ๊ทธ์˜ ๋ณ„๋ช…์ด "๋•…์ฝฉ ๋†๋ถ€" (Peanut Farmer)๋กœ ์•Œ๋ ค์กŒ๋‹ค. 8๋ฒˆ์งธ ์ค„ :์ •๊ณ„ ์ž…๋ฌธ. 9๋ฒˆ์งธ ์ค„ :1962๋…„ ์กฐ์ง€์•„ ์ฃผ<NAME> ์˜์› ์„ ๊ฑฐ์—์„œ ๋‚™์„ ํ•˜๋‚˜ ๊ทธ ์„ ๊ฑฐ๊ฐ€ ๋ถ€์ •์„ ๊ฑฐ์˜€์Œ์„ ์ž…์ฆํ•˜๊ฒŒ ๋˜์–ด ๋‹น์„ ๋˜๊ณ , 1966๋…„ ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ ์„ ๊ฑฐ์— ๋‚™์„ ํ•˜์ง€๋งŒ, 1970๋…„ ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ๋ฅผ ์—ญ์ž„ํ–ˆ๋‹ค. ๋Œ€ํ†ต๋ น์ด ๋˜๊ธฐ ์ „ ์กฐ์ง€์•„์ฃผ<NAME> ์˜์›์„ ๋‘ ๋ฒˆ ์—ฐ์ž„ํ–ˆ์œผ๋ฉฐ, 1971๋…„๋ถ€ํ„ฐ 1975๋…„๊นŒ์ง€ ์กฐ์ง€์•„ ์ง€์‚ฌ๋กœ ๊ทผ๋ฌดํ–ˆ๋‹ค. ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ๋กœ ์ง€๋‚ด๋ฉด์„œ, ๋ฏธ๊ตญ์— ์‚ฌ๋Š” ํ‘์ธ ๋“ฑ์šฉ ๋ฒ•์„ ๋‚ด์„ธ์› ๋‹ค. 10๋ฒˆ์งธ ์ค„ :๋Œ€ํ†ต๋ น ์žฌ์ž„. 2. ํ˜•ํƒœ์†Œ ๋ถ„์„ from tqdm import tqdm from konlpy.tag import Mecab ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ Mecab์„ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. mecab = Mecab() ์šฐ์„  output_file์—๋Š” ์ด ๋ช‡ ์ค„์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. f = open('output_file.txt', encoding="utf8") lines = f.read().splitlines() print(len(lines)) 9718793 9,718,793๊ฐœ์˜ ์ค„์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 10๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. lines[:10] ['<doc id="5" url="https://ko.wikipedia.org/wiki? curid=5" title="์ง€๋ฏธ ์นดํ„ฐ">', '์ง€๋ฏธ ์นดํ„ฐ', '', '์ œ์ž„์Šค ์–ผ ์นดํ„ฐ ์ฃผ๋‹ˆ์–ด(, 1924๋…„ 10์›” 1์ผ ~ )๋Š” ๋ฏผ์ฃผ๋‹น ์ถœ์‹  ๋ฏธ๊ตญ 39๋Œ€ ๋Œ€ํ†ต๋ น (1977๋…„ ~ 1981๋…„)์ด๋‹ค.', '์ƒ์• .', '์–ด๋ฆฐ ์‹œ์ ˆ.', '์ง€๋ฏธ ์นดํ„ฐ๋Š” ์กฐ์ง€์•„์ฃผ ์„ฌํ„ฐ ์นด์šดํ‹ฐ ํ”Œ๋ ˆ์ธ์Šค ๋งˆ์„์—์„œ ํƒœ์–ด๋‚ฌ๋‹ค.', '์กฐ์ง€์•„ ๊ณต๊ณผ๋Œ€ํ•™๊ต๋ฅผ ์กธ์—…ํ•˜์˜€๋‹ค. ๊ทธ ํ›„ ํ•ด๊ตฐ์— ๋“ค์–ด๊ฐ€ ์ „ํ•จยท์›์ž๋ ฅยท์ž ์ˆ˜ํ•จ์˜ ์Šน๋ฌด์›์œผ๋กœ ์ผํ•˜์˜€๋‹ค. 1953๋…„ ๋ฏธ๊ตญ ํ•ด๊ตฐ ๋Œ€์œ„๋กœ ์˜ˆํŽธํ•˜์˜€๊ณ  ์ดํ›„ ๋•…์ฝฉยท๋ฉดํ™” ๋“ฑ์„ ๊ฐ€๊ฟ” ๋งŽ์€ ๋ˆ์„ ๋ฒŒ์—ˆ๋‹ค. ๊ทธ์˜ ๋ณ„๋ช…์ด "๋•…์ฝฉ ๋†๋ถ€" (Peanut Farmer)๋กœ ์•Œ๋ ค์กŒ๋‹ค.', '์ •๊ณ„ ์ž…๋ฌธ.', '1962๋…„ ์กฐ์ง€์•„ ์ฃผ<NAME> ์˜์› ์„ ๊ฑฐ์—์„œ ๋‚™์„ ํ•˜๋‚˜ ๊ทธ ์„ ๊ฑฐ๊ฐ€ ๋ถ€์ •์„ ๊ฑฐ์˜€์Œ์„ ์ž…์ฆํ•˜๊ฒŒ ๋˜์–ด ๋‹น์„ ๋˜๊ณ , 1966๋…„ ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ ์„ ๊ฑฐ์— ๋‚™์„ ํ•˜์ง€๋งŒ, 1970๋…„ ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ๋ฅผ ์—ญ์ž„ํ–ˆ๋‹ค. ๋Œ€ํ†ต๋ น์ด ๋˜๊ธฐ ์ „ ์กฐ์ง€์•„์ฃผ<NAME> ์˜์›์„ ๋‘ ๋ฒˆ ์—ฐ์ž„ํ–ˆ์œผ๋ฉฐ, 1971๋…„๋ถ€ํ„ฐ 1975๋…„๊นŒ์ง€ ์กฐ์ง€์•„ ์ง€์‚ฌ๋กœ ๊ทผ๋ฌดํ–ˆ๋‹ค. ์กฐ์ง€์•„ ์ฃผ์ง€์‚ฌ๋กœ ์ง€๋‚ด๋ฉด์„œ, ๋ฏธ๊ตญ์— ์‚ฌ๋Š” ํ‘์ธ ๋“ฑ์šฉ ๋ฒ•์„ ๋‚ด์„ธ์› ๋‹ค.'] ๋‘ ๋ฒˆ์งธ ์ค„์„ ๋ณด๋ฉด ์•„๋ฌด๋Ÿฐ ๋‹จ์–ด๋„ ๋“ค์–ด์žˆ์ง€ ์•Š์€ ''์™€ ๊ฐ™์€ ์ค„๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฌธ์ž์—ด์€ ํ˜•ํƒœ์†Œ ๋ถ„์„์—์„œ ์ œ์™ธํ•˜๋„๋ก ํ•˜๊ณ  ํ˜•ํƒœ์†Œ ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. result = [] for line in tqdm(lines): # ๋นˆ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ ์ˆ˜ํ–‰ if line: result.append(mecab.morphs(line)) 100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 9718793/9718793 [15:27<00:00, 10478.61it/s] ๋นˆ ๋ฌธ์ž์—ด์€ ์ œ์™ธํ•˜๊ณ  ํ˜•ํƒœ์†Œ ๋ถ„์„์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ช‡ ๊ฐœ์˜ ์ค„. ์ฆ‰, ๋ช‡ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. len(result) 6559314 6,559,314๊ฐœ๋กœ ๋ฌธ์žฅ์˜ ์ˆ˜๊ฐ€ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. 3. Word2Vec ํ•™์Šต ํ˜•ํƒœ์†Œ ๋ถ„์„์„ ํ†ตํ•ด์„œ ํ† ํฐ ํ™”๊ฐ€ ์ง„ํ–‰๋œ ์ƒํƒœ์ด๋ฏ€๋กœ Word2Vec์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. from gensim.models import Word2Vec model = Word2Vec(result, size=100, window=5, min_count=5, workers=4, sg=0) model_result1 = model.wv.most_similar("๋Œ€ํ•œ๋ฏผ๊ตญ") print(model_result1) [('ํ•œ๊ตญ', 0.7382678389549255), ('๋ฏธ๊ตญ', 0.6731516122817993), ('์ผ๋ณธ', 0.6541135907173157), ('๋ถ€์‚ฐ', 0.5798133611679077), ('ํ™์ฝฉ', 0.5752249360084534), ('์„œ์šธ', 0.5541036128997803), ('์˜ค์ŠคํŠธ๋ ˆ์ผ๋ฆฌ์•„', 0.5531408786773682), ('ํƒœ๊ตญ', 0.548468828201294), ('๊ฒฝ์ƒ๋‚จ๋„', 0.5462549924850464), ('์ œ์ฃผํŠน๋ณ„์ž์น˜๋„', 0.5385439395904541)] model_result2 = model.wv.most_similar("์–ด๋ฒค์ €์Šค") print(model_result2) [('์ŠคํŒŒ์ด๋”๋งจ', 0.80271977186203), ('ํŠธ๋žœ์Šคํฌ๋จธ', 0.773989200592041), ('์•„์ด์–ธ๋งจ', 0.7648921012878418), ('์Šคํƒ€ํŠธ๋ ‰', 0.7645636796951294), ('์–ด๋ฒค์ €์Šค', 0.7626765966415405), ('์—‘์Šค๋งจ', 0.7586475610733032), ('ใ€Šใ€‹,', 0.7560415267944336), ('ํŠธ์™€์ผ๋ผ์ž‡', 0.7518032789230347), ('ํผ๋‹ˆ์…”', 0.7391209602355957), ('ํ…Œ์ผ์ฆˆ', 0.7386105060577393)] model_result3 = model.wv.most_similar("๋ฐ˜๋„์ฒด") print(model_result3) [('์ง‘์ ํšŒ๋กœ', 0.7714468836784363), ('์—ฐ๋ฃŒ์ „์ง€', 0.7699108719825745), ('์ „์ž', 0.7606919407844543), ('์›จ์ดํผ', 0.745188295841217), ('์‹ค๋ฆฌ์ฝ˜', 0.743209958076477), ('ํŠธ๋žœ์ง€์Šคํ„ฐ', 0.7398351430892944), ('PCB', 0.7275883555412292), ('TSMC', 0.7156406044960022), ('๊ฐ€์†๊ธฐ', 0.6962155699729919), ('๊ด‘์ „์ž', 0.6957612037658691)] 12-15 ๋‹จ์–ด ๋‹จ์œ„ RNN - ์ž„๋ฒ ๋”ฉ ์‚ฌ์šฉ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ด์ „์˜ RNN ์ฑ•ํ„ฐ์—์„œ ๋ฌธ์ž ๋‹จ์œ„ RNN์„ ๊ตฌํ˜„ํ–ˆ๋˜ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ RNN์˜ ์ž…๋ ฅ ๋‹จ์œ„๋ฅผ ๋‹จ์–ด ๋‹จ์œ„๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹จ์–ด ๋‹จ์œ„๋ฅผ ์‚ฌ์šฉํ•จ์— ๋”ฐ๋ผ์„œ Pytorch์—์„œ ์ œ๊ณตํ•˜๋Š” ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์šฐ์„  ์‹ค์Šต์„ ์œ„ํ•ด ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.optim as optim ์‹ค์Šต์„ ์œ„ํ•ด ์ž„์˜์˜ ๋ฌธ์žฅ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. sentence = "Repeat is the best medicine for memory".split() ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค RNN์€ 'Repeat is the best medicine for'์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด 'is the best medicine for memory'๋ฅผ ์ถœ๋ ฅํ•˜๋Š” RNN์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์ž„์˜์˜ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด์žฅ(vocabulary)์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. vocab = list(set(sentence)) print(vocab) ['best', 'memory', 'the', 'is', 'for', 'medicine', 'Repeat'] ์ด์ œ ๋‹จ์–ด์žฅ์˜ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ์™€ ๋™์‹œ์— ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๋ฅผ ์˜๋ฏธํ•˜๋Š” UNK ํ† ํฐ๋„ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. word2index = {tkn: i for i, tkn in enumerate(vocab, 1)} # ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜ ๋ถ€์—ฌ word2index['<unk>']=0 print(word2index) {'best': 1, 'memory': 2, 'the': 3, 'is': 4, 'for': 5, 'medicine': 6, 'Repeat': 7, '<unk>': 0} ์ด์ œ word2index๊ฐ€ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ์ตœ์ข… ๋‹จ์–ด์žฅ์ธ ์…ˆ์ž…๋‹ˆ๋‹ค. word2index์— ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. print(word2index['memory']) ๋‹จ์–ด 'memory'์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋Š” 2์ž…๋‹ˆ๋‹ค. ์˜ˆ์ธก ๋‹จ๊ณ„์—์„œ ์˜ˆ์ธกํ•œ ๋ฌธ์žฅ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด idx2word๋„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. # ์ˆ˜์น˜ํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹จ์–ด๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•œ ์‚ฌ์ „ index2word = {v: k for k, v in word2index.items()} print(index2word) {1: 'best', 2: 'memory', 3: 'the', 4: 'is', 5: 'for', 6: 'medicine', 7: 'Repeat', 0: '<unk>'} idx2word๋Š” ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜ 2๋ฅผ ๋„ฃ์–ด๋ด…์‹œ๋‹ค. print(index2word[2]) memory ์ •์ˆ˜ 2์™€ ๋งคํ•‘๋˜๋Š” ๋‹จ์–ด๋Š” memory์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ฐ์ดํ„ฐ์˜ ๊ฐ ๋‹จ์–ด๋ฅผ ์ •์ˆ˜๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋™์‹œ์—, ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” build_data๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. def build_data(sentence, word2index): encoded = [word2index[token] for token in sentence] # ๊ฐ ๋ฌธ์ž๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜. input_seq, label_seq = encoded[:-1], encoded[1:] # ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ๋ ˆ์ด๋ธ” ์‹œํ€€์Šค๋ฅผ ๋ถ„๋ฆฌ input_seq = torch.LongTensor(input_seq).unsqueeze(0) # ๋ฐฐ์น˜ ์ฐจ์› ์ถ”๊ฐ€ label_seq = torch.LongTensor(label_seq).unsqueeze(0) # ๋ฐฐ์น˜ ์ฐจ์› ์ถ”๊ฐ€ return input_seq, label_seq ๋งŒ๋“ค์–ด์ง„ ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. X, Y = build_data(sentence, word2index) ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(X) print(Y) tensor([[7, 4, 3, 1, 6, 5]]) # Repeat is the best medicine for์„ ์˜๋ฏธ tensor([[4, 3, 1, 6, 5, 2]]) # is the best medicine for memory์„ ์˜๋ฏธ 2. ๋ชจ๋ธ ๊ตฌํ˜„ํ•˜๊ธฐ ์ด์ œ ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ๋ชจ๋ธ๋“ค๊ณผ ๋‹ฌ๋ผ์ง„ ์ ์€ ์ž„๋ฒ ๋”ฉ ์ธต์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜์—์„œ๋Š” nn.Embedding()์„ ์‚ฌ์šฉํ•ด์„œ ์ž„๋ฒ ๋”ฉ ์ธต์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ์ธต์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ๋‹จ์–ด์žฅ์˜ ํฌ๊ธฐ์ด๋ฉฐ, ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ž…๋‹ˆ๋‹ค. class Net(nn.Module): def __init__(self, vocab_size, input_size, hidden_size, batch_first=True): super(Net, self).__init__() self.embedding_layer = nn.Embedding(num_embeddings=vocab_size, # ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ embedding_dim=input_size) self.rnn_layer = nn.RNN(input_size, hidden_size, # ์ž…๋ ฅ ์ฐจ์›, ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ ์ •์˜ batch_first=batch_first) self.linear = nn.Linear(hidden_size, vocab_size) # ์ถœ๋ ฅ์€ ์›-ํ•ซ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•จ. ๋˜๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋งŒํผ ๊ฐ€์ ธ์•ผ ํ•จ. def forward(self, x): # 1. ์ž„๋ฒ ๋”ฉ ์ธต # ํฌ๊ธฐ ๋ณ€ํ™”: (๋ฐฐ์น˜ ํฌ๊ธฐ, ์‹œํ€€์Šค ๊ธธ์ด) => (๋ฐฐ์น˜ ํฌ๊ธฐ, ์‹œํ€€์Šค ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ์ฐจ์›) output = self.embedding_layer(x) # 2. RNN ์ธต # ํฌ๊ธฐ ๋ณ€ํ™”: (๋ฐฐ์น˜ ํฌ๊ธฐ, ์‹œํ€€์Šค ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ์ฐจ์›) # => output (๋ฐฐ์น˜ ํฌ๊ธฐ, ์‹œํ€€์Šค ๊ธธ์ด, ์€๋‹‰์ธต ํฌ๊ธฐ), hidden (1, ๋ฐฐ์น˜ ํฌ๊ธฐ, ์€๋‹‰์ธต ํฌ๊ธฐ) output, hidden = self.rnn_layer(output) # 3. ์ตœ์ข… ์ถœ๋ ฅ์ธต # ํฌ๊ธฐ ๋ณ€ํ™”: (๋ฐฐ์น˜ ํฌ๊ธฐ, ์‹œํ€€์Šค ๊ธธ์ด, ์€๋‹‰์ธต ํฌ๊ธฐ) => (๋ฐฐ์น˜ ํฌ๊ธฐ, ์‹œํ€€์Šค ๊ธธ์ด, ๋‹จ์–ด์žฅ ํฌ๊ธฐ) output = self.linear(output) # 4. view๋ฅผ ํ†ตํ•ด์„œ ๋ฐฐ์น˜ ์ฐจ์› ์ œ๊ฑฐ # ํฌ๊ธฐ ๋ณ€ํ™”: (๋ฐฐ์น˜ ํฌ๊ธฐ, ์‹œํ€€์Šค ๊ธธ์ด, ๋‹จ์–ด์žฅ ํฌ๊ธฐ) => (๋ฐฐ์น˜ ํฌ๊ธฐ*์‹œํ€€์Šค ๊ธธ์ด, ๋‹จ์–ด์žฅ ํฌ๊ธฐ) return output.view(-1, output.size(2)) ์ด์ œ ๋ชจ๋ธ์„ ์œ„ํ•ด ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. # ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ vocab_size = len(word2index) # ๋‹จ์–ด์žฅ์˜ ํฌ๊ธฐ๋Š” ์ž„๋ฒ ๋”ฉ ์ธต, ์ตœ์ข… ์ถœ๋ ฅ์ธต์— ์‚ฌ์šฉ๋œ๋‹ค. <unk> ํ† ํฐ์„ ํฌ๊ธฐ์— ํฌํ•จํ•œ๋‹ค. input_size = 5 # ์ž„๋ฒ ๋”ฉ ๋œ ์ฐจ์›์˜ ํฌ๊ธฐ ๋ฐ RNN ์ธต ์ž…๋ ฅ ์ฐจ์›์˜ ํฌ๊ธฐ hidden_size = 20 # RNN์˜ ์€๋‹‰์ธต ํฌ๊ธฐ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋ธ ์ƒ์„ฑ model = Net(vocab_size, input_size, hidden_size, batch_first=True) # ์†์‹ค ํ•จ์ˆ˜ ์ •์˜ loss_function = nn.CrossEntropyLoss() # ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜ ํฌํ•จ์ด๋ฉฐ ์‹ค์ œ ๊ฐ’์€ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ์•ˆ ํ•ด๋„ ๋จ. # ์˜ตํ‹ฐ๋งˆ์ด์ € ์ •์˜ optimizer = optim.Adam(params=model.parameters()) ๋ชจ๋ธ์— ์ž…๋ ฅ์„ ๋„ฃ์–ด์„œ ์ถœ๋ ฅ์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. # ์ž„์˜๋กœ ์˜ˆ์ธกํ•ด ๋ณด๊ธฐ. ๊ฐ€์ค‘์น˜๋Š” ์ „๋ถ€ ๋žœ๋ค ์ดˆ๊ธฐํ™”๋œ ์ƒํƒœ์ด๋‹ค. output = model(X) print(output) tensor([[ 0.1198, 0.0473, 0.1735, 0.6194, 0.2807, -0.2106, 0.0770, -0.4386], [ 0.0374, -0.0778, 0.2033, 0.3874, -0.0493, -0.0961, 0.0201, -0.4601], [ 0.0167, -0.0092, 0.0669, 0.2091, -0.0390, -0.0250, 0.1512, -0.2769], [-0.0784, -0.0491, 0.1702, 0.2962, 0.0476, -0.1790, -0.3025, -0.2063], [ 0.1245, 0.1390, 0.2189, 0.3938, 0.2040, -0.1574, -0.2011, -0.1248], [ 0.1940, 0.0897, 0.3987, 0.3072, 0.2123, -0.0825, 0.1198, -0.2285]], grad_fn=<ViewBackward>) ๋ชจ๋ธ์ด ์–ด๋–ค ์˜ˆ์ธก๊ฐ’์„ ๋‚ด๋†“๊ธฐ๋Š” ํ•˜์ง€๋งŒ ํ˜„์žฌ ๊ฐ€์ค‘์น˜๋Š” ๋žœ๋ค ์ดˆ๊ธฐํ™”๋˜์–ด ์žˆ์–ด ์˜๋ฏธ ์žˆ๋Š” ์˜ˆ์ธก๊ฐ’์€ ์•„๋‹™๋‹ˆ๋‹ค. ์˜ˆ์ธก๊ฐ’์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(output.shape) torch.Size([6, 8]) ์˜ˆ์ธก๊ฐ’์˜ ํฌ๊ธฐ๋Š” (6, 8)์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ๊ฐ (์‹œํ€€์Šค์˜ ๊ธธ์ด, ์€๋‹‰์ธต์˜ ํฌ๊ธฐ)์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ ์ „์— ์˜ˆ์ธก์„ ์ œ๋Œ€๋กœ ํ•˜๊ณ  ์žˆ๋Š”์ง€ ์˜ˆ์ธก๋œ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ๋‹ค์‹œ ๋‹จ์–ด ์‹œํ€€์Šค๋กœ ๋ฐ”๊พธ๋Š” decode ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. # ์ˆ˜์น˜ํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹จ์–ด๋กœ ์ „ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ decode = lambda y: [index2word.get(x) for x in y] ์•ฝ 200 ์—ํฌํฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. # ํ›ˆ๋ จ ์‹œ์ž‘ for step in range(201): # ๊ฒฝ์‚ฌ ์ดˆ๊ธฐํ™” optimizer.zero_grad() # ์ˆœ๋ฐฉํ–ฅ ์ „ํŒŒ output = model(X) # ์†์‹ค ๊ฐ’ ๊ณ„์‚ฐ loss = loss_function(output, Y.view(-1)) # ์—ญ๋ฐฉํ–ฅ ์ „ํŒŒ loss.backward() # ๋งค๊ฐœ๋ณ€์ˆ˜ ์—…๋ฐ์ดํŠธ optimizer.step() # ๊ธฐ๋ก if step % 40 == 0: print("[{:02d}/201] {:.4f} ".format(step+1, loss)) pred = output.softmax(-1).argmax(-1).tolist() print(" ".join(["Repeat"] + decode(pred))) print() [01/201] 2.0184 Repeat the the the the medicine best [41/201] 1.3917 Repeat is the best medicine for memory [81/201] 0.7013 Repeat is the best medicine for memory [121/201] 0.2992 Repeat is the best medicine for memory [161/201] 0.1552 Repeat is the best medicine for memory [201/201] 0.0964 Repeat is the best medicine for memory 13. [NLP ๊ธฐ๋ณธ ] RNN๊ณผ CNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋‹ค๋Œ€์ผ RNN๊ณผ CNN์„ ์ด์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 13-01 RNN์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜(Text classification using PyTorch) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํŒŒ์ด ํ† ์น˜(PyTorch)๋กœ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์‹ค์Šตํ•ฉ๋‹ˆ๋‹ค. ์‹ค์Šต์— ์•ž์„œ ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•ด์„œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๊ฐ€ ์ˆ˜ํ–‰๋  ๋•Œ, ์–ด๋–ค ์ž‘์—…๊ณผ ๊ตฌ์„ฑ์œผ๋กœ ์ง„ํ–‰๋˜๋Š”์ง€ ๊ฐ„๋‹จํžˆ ๋ฏธ๋ฆฌ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด ์•ž์œผ๋กœ ๋ฐฐ์šฐ๊ฒŒ ๋  ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…์€ ์ง€๋„ ํ•™์Šต(Supervised Learning)์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์ง€๋„ ํ•™์Šต์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” ๋ ˆ์ด๋ธ”์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฏธ๋ฆฌ ์ •๋‹ต์ด ์ ํ˜€์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ๋น„์œ ํ•˜๋ฉด, ๊ธฐ๊ณ„๋Š” ์ •๋‹ต์ด ์ ํ˜€์ ธ ์žˆ๋Š” ๋ฌธ์ œ์ง€๋ฅผ ์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ•˜๊ณ , ํ–ฅํ›„์— ์ •๋‹ต์ด ์—†๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ๋„ ์ •๋‹ต์„ ์˜ˆ์ธกํ•ด์„œ ๋Œ€๋‹ตํ•˜๊ฒŒ ๋˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜๊ธฐ์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋ฉ”์ผ์˜ ๋‚ด์šฉ๊ณผ ํ•ด๋‹น ๋ฉ”์ผ์ด ์ •์ƒ ๋ฉ”์ผ์ธ์ง€, ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€ ์ ํ˜€์žˆ๋Š” ๋ ˆ์ด๋ธ”๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€<NAME>์˜ ๋ฉ”์ผ ์ƒ˜ํ”Œ์ด ์•ฝ 20,000๊ฐœ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ํ…์ŠคํŠธ(๋ฉ”์ผ์˜ ๋‚ด์šฉ) ๋ ˆ์ด๋ธ”(์ŠคํŒธ ์—ฌ๋ถ€) ๋‹น์‹ ์—๊ฒŒ ๋“œ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰ ํ˜œํƒ! ... ์ŠคํŒธ ๋ฉ”์ผ ๋‚ด์ผ ๋ต ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธ ๋ถ€ํƒ... ์ •์ƒ ๋ฉ”์ผ ์‰ฟ! ํ˜ผ์ž ๋ณด์„ธ์š”... ์ŠคํŒธ ๋ฉ”์ผ ์–ธ์ œ๊นŒ์ง€ ๋‹ต์žฅ ๊ฐ€๋Šฅํ• ... ์ •์ƒ ๋ฉ”์ผ ... ... (๊ด‘๊ณ ) ๋ฉ‹์žˆ์–ด์งˆ ์ˆ˜ ์žˆ๋Š”... ์ŠคํŒธ ๋ฉ”์ผ 20,000๊ฐœ์˜ ๋ฉ”์ผ ์ƒ˜ํ”Œ์„ ๊ฐ€์ง„ ์ด ๋ฐ์ดํ„ฐ๋Š” ๋ฉ”์ผ์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์™€ ์ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ŠคํŒธ ๋ฉ”์ผ์ธ์ง€ ์•„๋‹Œ์ง€๊ฐ€ ์ ํ˜€์žˆ๋Š” ๋ ˆ์ด๋ธ”. ๋‘ ๊ฐ€์ง€ ์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ์ด 20,000๊ฐœ์˜ ๋ฉ”์ผ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ๊ฐ€ ๊น”๋”ํ•˜๊ณ  ๋ชจ๋ธ ๋˜ํ•œ ์ž˜ ์„ค๊ณ„๋ผ ์žˆ๋‹ค๋ฉด ํ•™์Šต์ด ๋‹ค ๋œ ์ด ๋ชจ๋ธ์€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ๋Š” ์—†์—ˆ๋˜ ์–ด๋–ค ๋ฉ”์ผ ํ…์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ ˆ์ด๋ธ”์„ ์˜ˆ์ธกํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 2. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์œ„์—์„œ๋Š” 20,000๊ฐœ์˜ ๋ฉ”์ผ ์ƒ˜ํ”Œ์„ ์ „๋ถ€ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ–ˆ์ง€๋งŒ ์‚ฌ์‹ค ๊ฐ–๊ณ  ์žˆ๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋ถ€ ํ›ˆ๋ จ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ์ผ๋ถ€๋Š” ๋‚จ๊ฒจ๋†“๋Š” ๊ฒƒ์œผ๋กœ ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ 20,000๊ฐœ์˜ ์ƒ˜ํ”Œ ์ค‘์—์„œ 18,000๊ฐœ์˜ ์ƒ˜ํ”Œ์€ ํ›ˆ๋ จ์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , 2,000๊ฐœ์˜ ์ƒ˜ํ”Œ์€ ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ ๋ณด๋ฅ˜ํ•œ ์ฑ„ ํ›ˆ๋ จ์„ ์‹œํ‚ฌ ๋•Œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ 18,000๊ฐœ์˜ ์ƒ˜ํ”Œ๋กœ ๋ชจ๋ธ์ด ํ›ˆ๋ จ์ด ๋‹ค ๋˜๋ฉด, ์ด์ œ ๋ณด๋ฅ˜ํ•ด๋‘์—ˆ๋˜ 2,000๊ฐœ์˜ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์—์„œ ๋ ˆ์ด๋ธ”์€ ๋ณด์—ฌ์ฃผ์ง€ ์•Š๊ณ  ๋ชจ๋ธ์—๊ฒŒ ๋งž์ถฐ๋ณด๋ผ๊ณ  ์š”๊ตฌํ•œ ๋’ค, ์ •ํ™•๋„๋ฅผ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2,000๊ฐœ์˜ ์ƒ˜ํ”Œ์—๋„ ๋ ˆ์ด๋ธ”์ด ์žˆ์œผ๋ฏ€๋กœ ๋ชจ๋ธ์ด ์‹ค์ œ๋กœ ์ •๋‹ต์„ ์–ผ๋งˆ๋‚˜ ๋งž์ถ”๋Š”์ง€ ์ •๋‹ต๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋’ค์— ๋‚˜์˜ค๊ฒŒ ๋  ์˜ˆ์ œ์—์„œ๋Š” ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์—์„œ ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์—ด์„ X, ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ์˜ ์—ด์„ y๋ผ๊ณ  ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ(X_train, y_train)์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ(X_test, y_test)๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ์€ X_train๊ณผ y_train์„ ํ•™์Šตํ•˜๊ณ , X_test์— ๋Œ€ํ•ด์„œ ๋ ˆ์ด๋ธ”์„ ์˜ˆ์ธกํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋ธ์ด ์˜ˆ์ธกํ•œ ๋ ˆ์ด๋ธ”๊ณผ y_test๋ฅผ ๋น„๊ตํ•ด์„œ ์ •๋‹ต๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 3. ๋‹จ์–ด์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค ๋ถ€์—ฌ ์•ž์„œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ฑ•ํ„ฐ์—์„œ ๋‹จ์–ด๋ฅผ ๋ฐ€์ง‘ ๋ฒกํ„ฐ(dense vector)๋กœ ๋ฐ”๊พธ๋Š” ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šด ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. 8์ฑ•ํ„ฐ์™€ 9์ฑ•ํ„ฐ์—์„œ ์„ค๋ช…ํ•˜์˜€์ง€๋งŒ, ํŒŒ์ด ํ† ์น˜(PyTorch)์˜ nn.Embedding()์€ ๋‹จ์–ด ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์ •์ˆ˜๊ฐ€ ๋งคํ•‘๋œ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์ž„๋ฒ ๋”ฉ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ๋‹จ์–ด ๊ฐ๊ฐ์— ์ˆซ์ž ๋งคํ•‘, ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ฑ•ํ„ฐ์—์„œ์™€ ๊ฐ™์ด ๋‹จ์–ด๋ฅผ ๋นˆ๋„์ˆ˜ ์ˆœ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ณ  ์ˆœ์ฐจ์ ์œผ๋กœ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ์ดํ„ฐ ๋‰ด์Šค ๋ถ„๋ฅ˜ํ•˜๊ธฐ์™€ IMDB ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„๋ฅ˜ํ•˜๊ธฐ ์ฑ•ํ„ฐ์—์„œ๋„ ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์˜€์œผ๋ฉฐ, ํ•ด๋‹น ์ฑ•ํ„ฐ์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋“ค์€ ์ด๋ฏธ ์ด ์ž‘์—…์ด ๋๋‚œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„ ์ˆœ๋Œ€๋กœ ๋‹จ์–ด๋ฅผ ์ •๋ ฌํ•˜์—ฌ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ•˜์˜€์„ ๋•Œ์˜ ์žฅ์ ์€ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ์ ์€ ๋‹จ์–ด์˜ ์ œ๊ฑฐ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ 25,000๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ˆœ๊ฐ€ ๋†’์€ ์ˆœ์„œ๋กœ 1๋ถ€ํ„ฐ 25,000๊นŒ์ง€ ์ธ๋ฑ์Šค๋ฅผ ๋ถ€์—ฌํ–ˆ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ˆœ๋Œ€๋กœ ๋“ฑ์ˆ˜๊ฐ€ ๋ถ€์—ฌ๋œ ๊ฒƒ๊ณผ ๋‹ค๋ฆ„์—†๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒ˜๋ฆฌ ์ž‘์—…์—์„œ 1,000์„ ๋„˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด๋“ค์„ ์ œ๊ฑฐ์‹œ์ผœ๋ฒ„๋ฆฌ๋ฉด ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 1,000๊ฐœ์˜ ๋‹จ์–ด๋งŒ ๋‚จ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. RNN์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ธฐ # ์‹ค์ œ RNN ์€๋‹‰์ธต์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ. nn.RNN(input_size, hidden_size, batch_first=True) ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๊ด€์ ์—์„œ ์•ž์„œ ๋ฐฐ์šด RNN ์ฝ”๋“œ์˜ timesteps์™€ input_dim, ๊ทธ๋ฆฌ๊ณ  hidden_size๋ฅผ ํ•ด์„ํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ๋ฐ”๋‹๋ผ RNN์„ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, RNN์˜ ๋ณ€ํ˜•์ธ LSTM์ด๋‚˜ GRU๋„ ์•„๋ž˜์˜ ์‚ฌํ•ญ์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.) hidden_size = ์ถœ๋ ฅ์˜ ํฌ๊ธฐ(output_dim). timesteps = ์‹œ์ ์˜ ์ˆ˜ = ๊ฐ ๋ฌธ์„œ์—์„œ์˜ ๋‹จ์–ด ์ˆ˜. input_size = ์ž…๋ ฅ์˜ ํฌ๊ธฐ = ๊ฐ ๋‹จ์–ด์˜ ๋ฒกํ„ฐ ํ‘œํ˜„์˜ ์ฐจ์› ์ˆ˜. 5. RNN์˜ ๋‹ค-๋Œ€-์ผ(Many-to-One) ๋ฌธ์ œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋Š” RNN์˜ ๋‹ค-๋Œ€-์ผ(Many-to-One) ๋ฌธ์ œ์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋Š” ๋ชจ๋“  ์‹œ์ (time step)์— ๋Œ€ํ•ด์„œ ์ž…๋ ฅ์„ ๋ฐ›์ง€๋งŒ ์ตœ์ข… ์‹œ์ ์˜ RNN ์…€ ๋งŒ์ด ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์ด๊ฒƒ์ด ์ถœ๋ ฅ์ธต์œผ๋กœ ๊ฐ€์„œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ •๋‹ต์„ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋‘ ๊ฐœ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ์ •๋‹ต๋ฅผ ๊ณ ๋ฅด๋Š” ์ด์ง„ ๋ถ„๋ฅ˜(Binary Classification) ๋ฌธ์ œ๋ผ๊ณ  ํ•˜๋ฉฐ, ์„ธ ๊ฐœ ์ด์ƒ์˜ ์„ ํƒ์ง€ ์ค‘์—์„œ ์ •๋‹ต์„ ๊ณ ๋ฅด๋Š” ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜(Multi-Class Classification) ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๋ฌธ์ œ์—์„œ๋Š” ๊ฐ๊ฐ ๋ฌธ์ œ์— ๋งž๋Š” ๋‹ค๋ฅธ ํ™œ์„ฑํ™” ํ•จ์ˆ˜์™€ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ง„ ๋ถ„๋ฅ˜์˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ์ถœ๋ ฅ์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๋ฅผ, ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ฌธ์ œ๋ผ๋ฉด ์ถœ๋ ฅ์ธต์˜ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ์—๋Š” ํด๋ž˜์Šค๊ฐ€ N ๊ฐœ๋ผ๋ฉด ์ถœ๋ ฅ์ธต์— ํ•ด๋‹น๋˜๋Š” ๋ฐ€์ง‘์ธต(dense layer)์˜ ํฌ๊ธฐ๋Š” N์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ถœ๋ ฅ์ธต์˜ ๋‰ด๋Ÿฐ์˜ ์ˆ˜๋Š” N ๊ฐœ์ž…๋‹ˆ๋‹ค. (ํ•˜์ง€๋งŒ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋กœ ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์— ๋‰ด๋Ÿฐ์„ 2๊ฐœ๋กœ ๋ฐฐ์น˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.) 13-02 LSTM์„ ์ด์šฉํ•œ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ ์ด๋ฒˆ์— ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์ด 200,000๊ฐœ ๋ฆฌ๋ทฐ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋กœ ์˜ํ™” ๋ฆฌ๋ทฐ์— ๋Œ€ํ•œ ํ…์ŠคํŠธ์™€ ํ•ด๋‹น ๋ฆฌ๋ทฐ๊ฐ€ ๊ธ์ •์ธ ๊ฒฝ์šฐ 1, ๋ถ€์ •์ธ ๊ฒฝ์šฐ 0์„ ํ‘œ์‹œํ•œ ๋ ˆ์ด๋ธ”๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด ๊ฐ์„ฑ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์ „์ฒ˜๋ฆฌ ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://github.com/e9t/nsmc/ import pickle import pandas as pd import numpy as np import matplotlib.pyplot as plt import re import urllib.request from konlpy.tag import Mecab from tqdm import tqdm from sklearn.model_selection import train_test_split from collections import Counter 1) ๋ฐ์ดํ„ฐ ๋กœ๋“œํ•˜๊ธฐ ์œ„ ๋งํฌ๋กœ๋ถ€ํ„ฐ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ํ•ด๋‹นํ•˜๋Š” ratings_train.txt์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ํ•ด๋‹นํ•˜๋Š” ratings_test.txt๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings_train.txt", filename="ratings_train.txt") urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings_test.txt", filename="ratings_test.txt") pandas๋ฅผ ์ด์šฉํ•˜์—ฌ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” train_data์— ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” test_data์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. train_data = pd.read_table('ratings_train.txt') test_data = pd.read_table('ratings_test.txt') train_data์— ์กด์žฌํ•˜๋Š” ์˜ํ™” ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(train_data)) # ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ํ›ˆ๋ จ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 150000 train_data๋Š” ์ด 150,000๊ฐœ์˜ ๋ฆฌ๋ทฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. train_data[:5] # ์ƒ์œ„ 5๊ฐœ ์ถœ๋ ฅ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” id, document, label ์ด 3๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. id๋Š” ๊ฐ์„ฑ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์•ž์œผ๋กœ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด ๋ชจ๋ธ์€ ๋ฆฌ๋ทฐ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋Š” document์™€ ํ•ด๋‹น ๋ฆฌ๋ทฐ๊ฐ€ ๊ธ์ •(1), ๋ถ€์ •(0)์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” label ๋‘ ๊ฐœ์˜ ์—ด์„ ํ•™์Šตํ•˜๋Š” ๋ชจ๋ธ์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‹จ์ง€ ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด์•˜์ง€๋งŒ ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ์™€ ์˜์–ด ๋ฐ์ดํ„ฐ์˜ ์ฐจ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ธ๋ฑ์Šค 2๋ฒˆ ์ƒ˜ํ”Œ์€ ๋„์–ด์“ฐ๊ธฐ๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๊ธ€์„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ•œ๊ตญ์–ด์˜ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋˜์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. test_data์˜ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜์™€ ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(test_data)) # ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ํ…Œ์ŠคํŠธ์šฉ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 50000 test_data๋Š” ์ด 50,000๊ฐœ์˜ ์˜ํ™” ๋ฆฌ๋ทฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. test_data[:5] test_data๋„ train_data์™€ ๋™์ผํ•œ<NAME>์œผ๋กœ id, document, label 3๊ฐœ์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. 2) ๋ฐ์ดํ„ฐ ์ •์ œํ•˜๊ธฐ train_data์˜ ๋ฐ์ดํ„ฐ ์ค‘๋ณต ์œ ๋ฌด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. # document ์—ด๊ณผ label ์—ด์˜ ์ค‘๋ณต์„ ์ œ์™ธํ•œ ๊ฐ’์˜ ๊ฐœ์ˆ˜ train_data['document'].nunique(), train_data['label'].nunique() (146182, 2) ์ด 150,000๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜๋Š”๋ฐ document ์—ด์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 146,182๊ฐœ๋ผ๋Š” ๊ฒƒ์€ ์•ฝ 4,000๊ฐœ์˜ ์ค‘๋ณต ์ƒ˜ํ”Œ์ด ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. label ์—ด์€ 0 ๋˜๋Š” 1์˜ ๋‘ ๊ฐ€์ง€ ๊ฐ’๋งŒ์„ ๊ฐ€์ง€๋ฏ€๋กœ 2๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ค‘๋ณต ์ƒ˜ํ”Œ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. # document ์—ด์˜ ์ค‘๋ณต ์ œ๊ฑฐ train_data.drop_duplicates(subset=['document'], inplace=True) ์ค‘๋ณต ์ƒ˜ํ”Œ์„ ์ œ๊ฑฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ค‘๋ณต์ด ์ œ๊ฑฐ๋˜์—ˆ๋Š”์ง€ ์ „์ฒด ์ƒ˜ํ”Œ ์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ :',len(train_data)) ์ด ์ƒ˜ํ”Œ์˜ ์ˆ˜ : 146183 ์ค‘๋ณต ์ƒ˜ํ”Œ์ด ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. train_data์—์„œ ํ•ด๋‹น ๋ฆฌ๋ทฐ์˜ ๊ธ, ๋ถ€์ • ์œ ๋ฌด๊ฐ€ ๊ธฐ์žฌ๋˜์–ด ์žˆ๋Š” ๋ ˆ์ด๋ธ”(label) ๊ฐ’์˜ ๋ถ„ํฌ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. train_data['label'].value_counts().plot(kind = 'bar') ์•ž์„œ ํ™•์ธํ•˜์˜€๋“ฏ์ด ์•ฝ 146,000๊ฐœ์˜ ์˜ํ™” ๋ฆฌ๋ทฐ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜๋Š”๋ฐ ๊ทธ๋ž˜ํ”„ ์ƒ์œผ๋กœ ๊ธ์ •๊ณผ ๋ถ€์ • ๋‘˜ ๋‹ค ์•ฝ 72,000๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜์—ฌ ๋ ˆ์ด๋ธ”์˜ ๋ถ„ํฌ๊ฐ€ ๊ท ์ผํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ •ํ™•ํ•˜๊ฒŒ ๋ช‡ ๊ฐœ์ธ์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(train_data.groupby('label').size().reset_index(name = 'count')) label count 0 0 73342 1 1 72841 ๋ ˆ์ด๋ธ”์ด 0์ธ ๋ฆฌ๋ทฐ๊ฐ€ ๊ทผ์†Œํ•˜๊ฒŒ ๋งŽ์Šต๋‹ˆ๋‹ค. ๋ฆฌ๋ทฐ ์ค‘์— Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print(train_data.isnull().values.any()) True True๊ฐ€ ๋‚˜์™”๋‹ค๋ฉด ๋ฐ์ดํ„ฐ ์ค‘์— Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์–ด๋–ค ์—ด์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(train_data.isnull().sum()) id 0 document 1 label 0 dtype: int64 ๋ฆฌ๋ทฐ๊ฐ€ ์ ํ˜€์žˆ๋Š” document ์—ด์—์„œ Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์ด 1๊ฐœ๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด document ์—ด์—์„œ Null ๊ฐ’์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์กฐ๊ฑด์œผ๋กœ Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์–ด๋Š ์ธ๋ฑ์Šค์˜ ์œ„์น˜์— ์กด์žฌํ•˜๋Š”์ง€ ํ•œ ๋ฒˆ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. train_data.loc[train_data.document.isnull()] ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์„ ์ œ๊ฑฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. train_data = train_data.dropna(how = 'any') # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ํ–‰ ์ œ๊ฑฐ print(train_data.isnull().values.any()) # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ False Null ๊ฐ’์„ ๊ฐ€์ง„ ์ƒ˜ํ”Œ์ด ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ 1๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์ œ๊ฑฐ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(len(train_data)) 146182 ๋ฐ์ดํ„ฐ์˜ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ train_data์™€ test_data์—์„œ ์˜จ์ (.)์ด๋‚˜? ์™€ ๊ฐ™์€ ๊ฐ์ข… ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ์‚ฌ์šฉ๋œ ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. train_data๋กœ๋ถ€ํ„ฐ ํ•œ๊ธ€๋งŒ ๋‚จ๊ธฐ๊ณ  ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์˜์–ด๋ฅผ ์˜ˆ์‹œ๋กœ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜์–ด์˜ ์•ŒํŒŒ๋ฒณ๋“ค์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์€ [a-zA-Z]์ž…๋‹ˆ๋‹ค. ์ด ์ •๊ทœ ํ‘œํ˜„์‹์€ ์˜์–ด์˜ ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž๋“ค์„ ๋ชจ๋‘ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์œผ๋กœ ์ด๋ฅผ ์‘์šฉํ•˜๋ฉด ์˜์–ด์— ์†ํ•˜์ง€ ์•Š๋Š” ๊ตฌ๋‘์ ์ด๋‚˜ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•ŒํŒŒ๋ฒณ๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐํ•˜๋Š” ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์˜ˆ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. #์•ŒํŒŒ๋ฒณ๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐ eng_text = 'do!!! you expect... people~ to~ read~ the FAQ, etc. and actually accept hard~! atheism?@@' print(re.sub(r'[^a-zA-Z ]', '', eng_text)) 'do you expect people to read the FAQ etc and actually accept hard atheism' ์œ„์™€ ๊ฐ™์€ ์›๋ฆฌ๋ฅผ ํ•œ๊ตญ์–ด ๋ฐ์ดํ„ฐ์— ์ ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์šฐ์„  ํ•œ๊ธ€์„ ๋ฒ”์œ„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ฐพ์•„๋‚ด๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ž์Œ๊ณผ ๋ชจ์Œ์— ๋Œ€ํ•œ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ž์Œ์˜ ๋ฒ”์œ„๋Š” ใ„ฑ ~ ใ…Ž, ๋ชจ์Œ์˜ ๋ฒ”์œ„๋Š” ใ… ~ ใ…ฃ์™€ ๊ฐ™์ด ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์— ์–ด๋–ค ์ž์Œ๊ณผ ๋ชจ์Œ์ด ์†ํ•˜๋Š”์ง€ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋งํฌ : https://www.unicode.org/charts/PDF/U3130.pdf ใ„ฑ ~ ใ…Ž: 3131 ~ 314E ใ… ~ ใ…ฃ: 314F ~ 3163 ์™„์„ฑํ˜• ํ•œ๊ธ€์˜ ๋ฒ”์œ„๋Š” ๊ฐ€ ~ ํžฃ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์— ํฌํ•จ๋œ ์Œ์ ˆ๋“ค์€ ์•„๋ž˜์˜ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://www.unicode.org/charts/PDF/UAC00.pdf ์œ„ ๋ฒ”์œ„ ์ง€์ •์„ ๋ชจ๋‘ ๋ฐ˜์˜ํ•˜์—ฌ train_data์— ํ•œ๊ธ€๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐํ•˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. # ํ•œ๊ธ€๊ณผ ๊ณต๋ฐฑ์„ ์ œ์™ธํ•˜๊ณ  ๋ชจ๋‘ ์ œ๊ฑฐ train_data['document'] = train_data['document'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") train_data[:5] ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ๋‹ค์‹œ ์ถœ๋ ฅํ•ด ๋ณด์•˜๋Š”๋ฐ, ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ˆ˜ํ–‰ํ•˜์ž ๊ธฐ์กด์˜ ๊ณต๋ฐฑ. ์ฆ‰, ๋„์–ด์“ฐ๊ธฐ๋Š” ์œ ์ง€๋˜๋ฉด์„œ ์˜จ์ ๊ณผ ๊ฐ™์€ ๊ตฌ๋‘์  ๋“ฑ์€ ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ๋Š” ํ•œ๊ธ€์ด ์•„๋‹ˆ๋”๋ผ๋„ ์˜์–ด, ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž๋กœ๋„ ๋ฆฌ๋ทฐ๋ฅผ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ธฐ์กด์— ํ•œ๊ธ€์ด ์—†๋Š” ๋ฆฌ๋ทฐ์˜€๋‹ค๋ฉด ๋” ์ด์ƒ ์•„๋ฌด๋Ÿฐ ๊ฐ’๋„ ์—†๋Š” ๋นˆ(empty) ๊ฐ’์ด ๋˜์—ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. train_data์— ๊ณต๋ฐฑ(whitespace)๋งŒ ์žˆ๊ฑฐ๋‚˜ ๋นˆ ๊ฐ’์„ ๊ฐ€์ง„ ํ–‰์ด ์žˆ๋‹ค๋ฉด Null ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋„๋ก ํ•˜๊ณ , Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. train_data['document'] = train_data['document'].str.replace('^ +', "") # white space ๋ฐ์ดํ„ฐ๋ฅผ empty value๋กœ ๋ณ€๊ฒฝ train_data['document'].replace('', np.nan, inplace=True) print(train_data.isnull().sum()) id 0 document 789 label 0 dtype: int64 Null ๊ฐ’์ด 789๊ฐœ๋‚˜ ์ƒˆ๋กœ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. Null ๊ฐ’์ด ์žˆ๋Š” ํ–‰์„ 5๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ณผ๊นŒ์š”? train_data.loc[train_data.document.isnull()][:5] Null ์ƒ˜ํ”Œ๋“ค์€ ๋ ˆ์ด๋ธ”์ด ๊ธ์ •์ผ ์ˆ˜๋„ ์žˆ๊ณ , ๋ถ€์ •์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๋Ÿฐ ์˜๋ฏธ๋„ ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฏ€๋กœ ์ œ๊ฑฐํ•ด ์ค๋‹ˆ๋‹ค. train_data = train_data.dropna(how = 'any') print(len(train_data)) 145393 ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜๊ฐ€ ๋˜๋‹ค์‹œ ์ค„์–ด์„œ 145,393๊ฐœ๊ฐ€ ๋‚จ์•˜์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ์•ž์„œ ์ง„ํ–‰ํ•œ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์„ ๋™์ผํ•˜๊ฒŒ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. test_data.drop_duplicates(subset = ['document'], inplace=True) # document ์—ด์—์„œ ์ค‘๋ณต์ธ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ์ค‘๋ณต ์ œ๊ฑฐ test_data['document'] = test_data['document'].str.replace("[^ใ„ฑ-ใ…Žใ…-ใ…ฃ๊ฐ€-ํžฃ ]","") # ์ •๊ทœ ํ‘œํ˜„์‹ ์ˆ˜ํ–‰ test_data['document'] = test_data['document'].str.replace('^ +', "") # ๊ณต๋ฐฑ์€ empty ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ test_data['document'].replace('', np.nan, inplace=True) # ๊ณต๋ฐฑ์€ Null ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ test_data = test_data.dropna(how='any') # Null ๊ฐ’ ์ œ๊ฑฐ print('์ „์ฒ˜๋ฆฌ ํ›„ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ :',len(test_data)) ์ „์ฒ˜๋ฆฌ ํ›„ ํ…Œ์ŠคํŠธ์šฉ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ : 48852 3) ํ† ํฐํ™” ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ํ† ํฐํ™” ๊ณผ์ •์—์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ถˆ์šฉ์–ด๋Š” ์ •์˜ํ•˜๊ธฐ ๋‚˜๋ฆ„์ธ๋ฐ, ํ•œ๊ตญ์–ด์˜ ์กฐ์‚ฌ, ์ ‘์†์‚ฌ ๋“ฑ์˜ ๋ณดํŽธ์ ์ธ ๋ถˆ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ๊ฒฐ๊ตญ ํ’€๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์† ๊ฒ€ํ† ํ•˜๋ฉด์„œ ๊ณ„์†ํ•ด์„œ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ ๋˜ํ•œ ๋งŽ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ํ˜„์—…์ธ ์ƒํ™ฉ์ด๋ผ๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ์•„๋ž˜์˜ ๋ถˆ์šฉ์–ด๋ณด๋‹ค ๋” ๋งŽ์€ ๋ถˆ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. stopwords = ['๋„', '๋Š”', '๋‹ค', '์˜', '๊ฐ€', '์ด', '์€', 'ํ•œ', '์—', 'ํ•˜', '๊ณ ', '์„', '๋ฅผ', '์ธ', '๋“ฏ', '๊ณผ', '์™€', '๋„ค', '๋“ค', '๋“ฏ', '์ง€', '์ž„', '๊ฒŒ'] ์—ฌ๊ธฐ์„œ๋Š” ์œ„ ์ •๋„๋กœ๋งŒ ๋ถˆ์šฉ์–ด๋ฅผ ์ •์˜ํ•˜๊ณ , ํ† ํฐํ™”๋ฅผ ์œ„ํ•œ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋Š” KoNLPy์˜ Mecab์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Mecab์„ ๋ณต์Šตํ•ด ๋ด…์‹œ๋‹ค. mecab = Mecab() mecab.morphs('์™€ ์ด๋Ÿฐ ๊ฒƒ๋„ ์˜ํ™”๋ผ๊ณ  ์ฐจ๋ผ๋ฆฌ ๋ฎค์ง๋น„๋””์˜ค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒŒ ๋‚˜์„ ๋ป”') ['์™€', '์ด๋Ÿฐ', '๊ฒƒ', '๋„', '์˜ํ™”', '๋ผ๊ณ ', '์ฐจ๋ผ๋ฆฌ', '๋ฎค์ง', '๋น„๋””์˜ค', '๋ฅผ', '๋งŒ๋“œ', '๋Š”', '๊ฒŒ', '๋‚˜์„', '๋ป”' ํ•œ๊ตญ์–ด์„ ํ† ํฐํ™”ํ•  ๋•Œ๋Š” ์˜์–ด์ฒ˜๋Ÿผ ๋„์–ด์“ฐ๊ธฐ ๊ธฐ์ค€์œผ๋กœ ํ† ํฐํ™”๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ฃผ๋กœ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. train_data์— ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™”๋ฅผ ํ•˜๋ฉด์„œ ๋ถˆ์šฉ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ X_train์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. X_train = [] for sentence in tqdm(train_data['document']): tokenized_sentence = mecab.morphs(sentence) # ํ† ํฐํ™” stopwords_removed_sentence = [word for word in tokenized_sentence if not word in stopwords] # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ X_train.append(stopwords_removed_sentence) ์ƒ์œ„ 3๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print(X_train[:3]) [['์•„', '๋”', '๋น™', '์ง„์งœ', '์งœ์ฆ', '๋‚˜', '๋„ค์š”', '๋ชฉ์†Œ๋ฆฌ'], ['ํ ', 'ํฌ์Šคํ„ฐ', '๋ณด๊ณ ', '์ดˆ๋“ฑํ•™์ƒ', '์˜ํ™”', '์ค„', '์˜ค๋ฒ„', '์—ฐ๊ธฐ', '์กฐ์ฐจ', '๊ฐ€๋ณ', '์•ˆ', '๊ตฌ๋‚˜'], ['๋„ˆ๋ฌด', '์žฌ', '๋ฐ“์—ˆ๋‹ค๊ทธ๋ž˜์„œ๋ณด๋Š”๊ฒƒ์„์ถ”์ฒœํ•œ๋‹ค']] ํ† ํฐ ํ™”๊ฐ€ ์ง„ํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ๋™์ผํ•˜๊ฒŒ ํ† ํฐํ™”๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค. X_test = [] for sentence in tqdm(test_data['document']): tokenized_sentence = okt.morphs(sentence, stem=True) # ํ† ํฐํ™” stopwords_removed_sentence = [word for word in tokenized_sentence if not word in stopwords] # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ X_test.append(stopwords_removed_sentence) ์ง€๊ธˆ๊นŒ์ง€ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ•™์Šต ๋ฐ์ดํ„ฐ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4) ํ•™์Šต ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์ด๋ฏธ ํ•™์Šต ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” ์ค€๋น„๋˜์—ˆ์ง€๋งŒ ํ•™์Šตํ•˜๋Š” ๋™์•ˆ์˜ ์„ฑ๋Šฅ ํ‰๊ฐ€๋ฅผ ์ง„ํ–‰ํ•  ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€๋กœ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ๋ ˆ์ด๋ธ” ์—ด์„ ๋ณ„๋„๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ y_train๊ณผ y_test๋กœ ์ €์žฅํ•ด ์ค๋‹ˆ๋‹ค. ์ด์ œ ํ•™์Šต ๋ฐ์ดํ„ฐ๋Š” X_train, y_train์— ์ €์žฅ๋˜๊ณ , ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” X_test, y_test์— ์ €์žฅ์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•™์Šต ๋ฐ์ดํ„ฐ ์ค‘์—์„œ 20%๋ฅผ ๋ถ„ํ• ํ•˜์—ฌ ์ถ”๊ฐ€๋กœ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ๋จธ์‹  ๋Ÿฌ๋‹ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ, ๋ฐ์ดํ„ฐ์˜ ๋ถ„๋ฆฌ๋Š” ์ฃผ๋กœ ์‚ฌ์ดํ‚ท๋Ÿฐ์—์„œ ์ œ๊ณตํ•˜๋Š” train_test_split์„ ์‚ฌ์šฉํ•ด ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. test_size์— ๋น„์œจ์„ ๋„ฃ์–ด์ฃผ๋ฉด ๊ธฐ์กด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ•ด๋‹น ๋น„์œจ๋งŒํผ ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„ํ• ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋žœ๋ค์œผ๋กœ ๋ถ„ํ• ํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ ˆ์ด๋ธ” ๋ถˆ๊ท ํ˜•์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก, ๋ ˆ์ด๋ธ”์˜ ๊ท ํ˜• ๋น„์œจ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ถ„ํ• ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋ถ„ํ•  ์‹œ ๊ธฐ์กด ๋ฐ์ดํ„ฐ์˜ y ๋ฐ์ดํ„ฐ๋ฅผ stratify์˜ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. y_train = np.array(train_data['label']) y_test = np.array(test_data['label']) X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=0.2, random_state=0, stratify=y_train) ์‹ค์ œ๋กœ ๋น„์œจ์ด ์ž˜ ์œ ์ง€๋˜๋ฉด์„œ ๋ถ„ํ• ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('--------ํ•™์Šต ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'๋ถ€์ • ๋ฆฌ๋ทฐ = {round(np.sum(y_train==0)/len(y_train) * 100,3)}%') print(f'๊ธ์ • ๋ฆฌ๋ทฐ = {round(np.count_nonzero(y_train)/len(y_train) * 100,3)}%') print('--------๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'๋ถ€์ • ๋ฆฌ๋ทฐ = {round(np.sum(y_valid==0)/len(y_valid) * 100,3)}%') print(f'๊ธ์ • ๋ฆฌ๋ทฐ = {round(np.count_nonzero(y_valid)/len(y_valid) * 100,3)}%') print('--------ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'๋ถ€์ • ๋ฆฌ๋ทฐ = {round(np.sum(y_test==0)/len(y_test) * 100,3)}%') print(f'๊ธ์ • ๋ฆฌ๋ทฐ = {round(np.count_nonzero(y_test)/len(y_test) * 100,3)}%') --------ํ•™์Šต ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ๋ถ€์ • ๋ฆฌ๋ทฐ = 50.238% ๊ธ์ • ๋ฆฌ๋ทฐ = 49.762% --------๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ๋ถ€์ • ๋ฆฌ๋ทฐ = 50.239% ๊ธ์ • ๋ฆฌ๋ทฐ = 49.761% --------ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ๋ถ€์ • ๋ฆฌ๋ทฐ = 49.808% ๊ธ์ • ๋ฆฌ๋ทฐ = 50.192% ๋ถ„ํ•  ํ›„์—๋„ ํ•™์Šต ๋ฐ์ดํ„ฐ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ” ๋น„์œจ์ด ๋™์ผํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 5) ๋‹จ์–ด ์ง‘ํ•ฉ ๋งŒ๋“ค๊ธฐ word_list = [] for sent in X_train: for word in sent: word_list.append(word) word_counts = Counter(word_list) print('์ด ๋‹จ์–ด ์ˆ˜ :', len(word_counts)) ์ด ๋‹จ์–ด ์ˆ˜ : 45296 print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด ์˜ํ™”์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ :', word_counts['์˜ํ™”']) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด ๊ณต๊ฐ์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ :', word_counts['๊ณต๊ฐ']) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด ์˜ํ™”์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ : 45791 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด ๊ณต๊ฐ์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ : 756 vocab = sorted(word_counts, key=word_counts.get, reverse=True) print('๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 10๊ฐœ ๋‹จ์–ด') print(vocab[:10]) ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 10๊ฐœ ๋‹จ์–ด ['์˜ํ™”', '๋ณด', '์žˆ', '์—†', '์ข‹', '๋‚˜', '์—ˆ', '๋งŒ', '๋Š”๋ฐ', '๋„ˆ๋ฌด'] threshold = 3 total_cnt = len(word_counts) # ๋‹จ์–ด์˜ ์ˆ˜ rare_cnt = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ total_freq = 0 # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๋‹จ์–ด ๋นˆ๋„์ˆ˜ ์ดํ•ฉ rare_freq = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜์˜ ์ดํ•ฉ # ๋‹จ์–ด์™€ ๋นˆ๋„์ˆ˜์˜ ์Œ(pair)์„ key์™€ value๋กœ ๋ฐ›๋Š”๋‹ค. for key, value in word_counts.items(): total_freq = total_freq + value # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์œผ๋ฉด if(value < threshold): rare_cnt = rare_cnt + 1 rare_freq = rare_freq + value print('๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ :',total_cnt) print('๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ %s ๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: %s'%(threshold - 1, rare_cnt)) print("๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ:", (rare_cnt / total_cnt)*100) print("์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ:", (rare_freq / total_freq)*100) ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ : 45296 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 2๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: 26105 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ: 57.63202048746025 ์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ: 2.2769635638286716 # ์ „์ฒด ๋‹จ์–ด ๊ฐœ์ˆ˜ ์ค‘ ๋นˆ๋„์ˆ˜ 2์ดํ•˜์ธ ๋‹จ์–ด๋Š” ์ œ๊ฑฐ. vocab_size = total_cnt - rare_cnt vocab = vocab[:vocab_size] print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', len(vocab)) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 19191 word_to_index = {} word_to_index['<PAD>'] = 0 word_to_index['<UNK>'] = 1 for index, word in enumerate(vocab) : word_to_index[word] = index + 2 vocab_size = len(word_to_index) print('ํŒจ๋”ฉ ํ† ํฐ๊ณผ UNK ํ† ํฐ์„ ๊ณ ๋ คํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', vocab_size) ํŒจ๋”ฉ ํ† ํฐ๊ณผ UNK ํ† ํฐ์„ ๊ณ ๋ คํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 19193 print('๋‹จ์–ด <PAD>์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ :', word_to_index['<PAD>']) print('๋‹จ์–ด <UNK>์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ :', word_to_index['<UNK>']) print('๋‹จ์–ด ์˜ํ™”์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ :', word_to_index['์˜ํ™”']) ๋‹จ์–ด <PAD>์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ : 0 ๋‹จ์–ด <UNK>์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ : 1 ๋‹จ์–ด ์˜ํ™”์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ : 2 6) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ def texts_to_sequences(tokenized_X_data, word_to_index): encoded_X_data = [] for sent in tokenized_X_data: index_sequences = [] for word in sent: try: index_sequences.append(word_to_index[word]) except KeyError: index_sequences.append(word_to_index['<UNK>']) encoded_X_data.append(index_sequences) return encoded_X_data encoded_X_train = texts_to_sequences(X_train, word_to_index) encoded_X_valid = texts_to_sequences(X_valid, word_to_index) encoded_X_test = texts_to_sequences(X_test, word_to_index) # ์ƒ์œ„ ์ƒ˜ํ”Œ 2๊ฐœ ์ถœ๋ ฅ for sent in encoded_X_train[:2]: print(sent) [924, 1866, 128, 7, 80, 48, 34] [2415, 3138, 4, 2095, 422, 87, 5768, 19, 307] index_to_word = {} for key, value in word_to_index.items(): index_to_word[value] = key decoded_sample = [index_to_word[word] for word in encoded_X_train[0]] print('๊ธฐ์กด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :', X_train[0]) print('๋ณต์›๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :', decoded_sample) ๊ธฐ์กด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : ['์ด์•ผ', '์–ด์ฉœ', '์ด๋ ‡๊ฒŒ', '๋‚˜', '์ง€๋ฃจ', 'ํ• ', '์ˆ˜'] ๋ณต์›๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : ['์ด์•ผ', '์–ด์ฉœ', '์ด๋ ‡๊ฒŒ', '๋‚˜', '์ง€๋ฃจ', 'ํ• ', '์ˆ˜'] 7) ํŒจ๋”ฉ print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max(len(review) for review in encoded_X_train)) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :',sum(map(len, encoded_X_train))/len(encoded_X_train)) plt.hist([len(review) for review in encoded_X_train], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 74 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 12.296731261928917 ๊ทธ๋ฆผ def below_threshold_len(max_len, nested_list): count = 0 for sentence in nested_list: if(len(sentence) <= max_len): count = count + 1 print('์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ %s ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: %s'%(max_len, (count / len(nested_list))*100)) max_len = 30 below_threshold_len(max_len, X_train) ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ 30 ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: 92.49703389101914 def pad_sequences(sentences, max_len): features = np.zeros((len(sentences), max_len), dtype=int) for index, sentence in enumerate(sentences): if len(sentence) != 0: features[index, :len(sentence)] = np.array(sentence)[:max_len] return features padded_X_train = pad_sequences(encoded_X_train, max_len=max_len) padded_X_valid = pad_sequences(encoded_X_valid, max_len=max_len) padded_X_test = pad_sequences(encoded_X_test, max_len=max_len) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_train.shape) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_valid.shape) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_test.shape) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (116314, 30) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (29079, 30) ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (48852, 30) print('์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด :', len(padded_X_train[0])) print('์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :', padded_X_train[0]) ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด : 30 ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : [ 924 1866 128 7 80 48 34 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 2. LSTM์„ ์ด์šฉํ•œ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ ๋ชจ๋ธ ์ด์ œ ๋”ฅ ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ PyTorch๋ฅผ ์ด์šฉํ•˜์—ฌ LSTM ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F ํ˜„์žฌ ์‹ค์Šต ํ™˜๊ฒฝ์—์„œ GPU๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. USE_CUDA = torch.cuda.is_available() device = torch.device("cuda" if USE_CUDA else "cpu") print("cpu์™€ cuda ์ค‘ ๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•จ:", device) cpu์™€ cuda ์ค‘ ๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•จ: cuda ์ €์ž์˜ ๊ฒฝ์šฐ Colab์—์„œ GPU๋ฅผ ์„ ํƒํ•˜์—ฌ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜์—ฌ cuda๋ผ๋Š” ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด ํ† ์น˜์˜ ํ…์„œ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ 5๊ฐœ์˜ ๋ ˆ์ด๋ธ”์„ ์ถœ๋ ฅํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. train_label_tensor = torch.tensor(np.array(y_train)) valid_label_tensor = torch.tensor(np.array(y_valid)) test_label_tensor = torch.tensor(np.array(y_test)) print(train_label_tensor[:5]) tensor([1, 1, 0, 0, 0]) ์ด์ œ LSTM ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ๊ฐ ์ธต์„ ์ง€๋‚  ๋•Œ๋งˆ๋‹ค ๊ฐ ์ธต์˜ ์ถœ๋ ฅ์˜ ํฌ๊ธฐ๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž…๋ ฅ์€ (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ฌธ์žฅ ๊ธธ์ด)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํ…์„œ์ž…๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ์ธต์„ ์ง€๋‚˜๊ณ  ๋‚˜๋ฉด ๊ฐ ๋‹จ์–ด๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜๋˜๋ฉด์„œ (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ฌธ์žฅ ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›)์œผ๋กœ ํ…์„œ์˜ ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ดํ›„ LSTM์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ(hidden state) ๊ฐ’์„ ์ถœ๋ ฅ์ธต๊ณผ ์—ฐ๊ฒฐ์‹œํ‚ค๋Š” ์ž‘์—…์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ LSTM์ด ์ถœ๋ ฅ์ธต์œผ๋กœ ๋ณด๋Š” ๊ฒฐ๊ด๊ฐ’์˜ ์ฐจ์›์€ (๋ฐฐ์น˜ ํฌ๊ธฐ, ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›)์„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’๋งŒ ์ „๋‹ฌํ•˜๋ฏ€๋กœ ์€๋‹‰ ์ƒํƒœ๋Š” ๋ชจ๋“  ์‹œ์ (๋ฌธ์žฅ ๊ธธ์ด) ๋งŒํผ ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹จ ํ•˜๋‚˜๋งŒ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์€ ์ง€๋‚œ ๊ฒฐ๊ณผ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์ˆ˜)์˜ ์ฐจ์›์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ ๋ฌถ์Œ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•„์ง ๋ชจ๋ธ์„ ๋งŒ๋“ค์ง€๋Š” ์•Š์•˜์ง€๋งŒ, ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ 100, ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 32, ๋ฌธ์žฅ ๊ธธ์ด๋ฅผ 500(ํŒจ๋”ฉ ํ›„), LSTM์˜ ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›์„ 128๋กœ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. - ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์› = 100 - ๋ฌธ์žฅ ๊ธธ์ด = 500 - ๋ฐฐ์น˜ ํฌ๊ธฐ = 32 - ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜ = 2๋งŒ - LSTM์˜ ์€๋‹‰์ธต์˜ ํฌ๊ธฐ = 128 - ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ๊ฐœ์ˆ˜ = 2๊ฐœ ์œ„์˜ ์ •๋ณด๋“ค์„ ๊ณ ๋ คํ•˜์˜€์„ ๋•Œ ๋ชจ๋ธ ๋‚ด๋ถ€์—์„œ ๋ฐ์ดํ„ฐ์˜ ๋ณ€ํ™”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (32, 500) => ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ํ˜•ํƒœ => ์ž„๋ฒ ๋”ฉ ์ธต ํ†ต๊ณผ ํ›„ => (32, 500, 100) => LSTM ํ†ต๊ณผ ํ›„ => (32, 128) => Softmax ์ถœ๋ ฅ์ธต ํ†ต๊ณผ ํ›„ => (32, 2) class TextClassifier(nn.Module): def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim): super(TextClassifier, self).__init__() self.embedding = nn.Embedding(vocab_size, embedding_dim) self.lstm = nn.LSTM(embedding_dim, hidden_dim, batch_first=True) self.fc = nn.Linear(hidden_dim, output_dim) def forward(self, x): # x: (batch_size, seq_length) embedded = self.embedding(x) # (batch_size, seq_length, embedding_dim) # LSTM์€ (hidden state, cell state)์˜ ํŠœํ”Œ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค lstm_out, (hidden, cell) = self.lstm(embedded) # lstm_out: (batch_size, seq_length, hidden_dim), hidden: (1, batch_size, hidden_dim) last_hidden = hidden.squeeze(0) # (batch_size, hidden_dim) logits = self.fc(last_hidden) # (batch_size, output_dim) return logits ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํŒŒ์ด ํ† ์น˜ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ฐฐ์น˜ ๋‹จ์œ„ ์—ฐ์‚ฐ์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. encoded_train = torch.tensor(padded_X_train).to(torch.int64) train_dataset = torch.utils.data.TensorDataset(encoded_train, train_label_tensor) train_dataloader = torch.utils.data.DataLoader(train_dataset, shuffle=True, batch_size=32) encoded_test = torch.tensor(padded_X_test).to(torch.int64) test_dataset = torch.utils.data.TensorDataset(encoded_test, test_label_tensor) test_dataloader = torch.utils.data.DataLoader(test_dataset, shuffle=True, batch_size=1) encoded_valid = torch.tensor(padded_X_valid).to(torch.int64) valid_dataset = torch.utils.data.TensorDataset(encoded_valid, valid_label_tensor) valid_dataloader = torch.utils.data.DataLoader(valid_dataset, shuffle=True, batch_size=1) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜๊ฐ€ 116,314๊ฐœ์˜€์œผ๋ฏ€๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 32๋กœ ํ•  ๊ฒฝ์šฐ์—๋Š” 116,324/32=3,635 ๋‹ค์‹œ ๋งํ•ด 32๊ฐœ์”ฉ ๋ฌถ์ธ ๋ฐ์ดํ„ฐ ๋ฌถ์Œ์ด 3,635๊ฐœ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต ์‹œ์—๋Š” 32๊ฐœ์”ฉ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. total_batch = len(train_dataloader) print('์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : {}'.format(total_batch)) ์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : 3635 ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. embedding_dim = 100 hidden_dim = 128 output_dim = 2 learning_rate = 0.01 num_epochs = 10 model = TextClassifier(vocab_size, embedding_dim, hidden_dim, output_dim) model.to(device) ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128, ์ถœ๋ ฅ์ธต์˜ ํฌ๊ธฐ(๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ฐœ์ˆ˜)๋Š” 2๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•ด์ฃผ๋Š” ๊ฐ’์ด๋ฉด์„œ ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๊ฐ’๋“ค์„ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ํ†ตํ•ด ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ง„ํ–‰ํ•˜๋ฏ€๋กœ ์†์‹ค ํ•จ์ˆ˜๋Š” nn.CrossEntropyLoss()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜๋กœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ์†์‹ค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘ ํ•˜๋‚˜์ธ ํ•™์Šต๋ฅ (learning rate)๋Š” 0.001๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) 3. ํ‰๊ฐ€ ์ฝ”๋“œ ์ž‘์„ฑ ์ดํ›„ ํ‰๊ฐ€๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ชจ๋ธ์˜ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•˜๋Š” ํ•จ์ˆ˜ calculate_accuracy()๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. def calculate_accuracy(logits, labels): # _, predicted = torch.max(logits, 1) predicted = torch.argmax(logits, dim=1) correct = (predicted == labels).sum().item() total = labels.size(0) accuracy = correct / total return accuracy ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ evaluate()๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ•จ์ˆ˜์—์„œ model.eval()๊ณผ with torch.no_grad()๋ฅผ ์งš์–ด๋ด…์‹œ๋‹ค. ์ด ๋‘ ๊ฐœ๋Š” ๋ชจ๋ธ ํ‰๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์˜๋ฏธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. model.eval(): ๋ชจ๋ธ์„ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋ธ ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๋ ˆ์ด์–ด์— ๋Œ€ํ•ด ํ‰๊ฐ€ ๋ชจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ ˆ์ด์–ด, ์˜ˆ๋ฅผ ๋“ค์–ด ๋“œ๋กญ์•„์›ƒ์ด๋‚˜ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ํ•™์Šต๊ณผ ํ‰๊ฐ€ ์‹œ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ์„ค์ •์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ชจ๋“œ๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์œผ๋ฉด, ์ด๋Ÿฌํ•œ ๋ ˆ์ด์–ด์˜ ๋™์ž‘์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋กœ ์ธํ•ด ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ์ œ๋Œ€๋กœ ๋‚˜์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with torch.no_grad(): ์ด ๋ฌธ์žฅ์€ ์ž๋™ ๋ฏธ๋ถ„ ์—”์ง„์—์„œ ๊ทธ๋ž˜๋”” ์–ธํŠธ ๊ณ„์‚ฐ์„ ๋น„ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ์ค‘์—๋Š” ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๊ณ  ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ์„ค์ •์ด ์ ์šฉ๋˜์ง€ ์•Š์œผ๋ฉด, ํ‰๊ฐ€ ๊ณผ์ •์—์„œ ๊ทธ๋ž˜๋”” ์–ธํŠธ๊ฐ€ ๊ณ„์‚ฐ๋˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ‰๊ฐ€ ๊ฒฐ๊ณผ ์ž์ฒด์—๋Š” ์ง์ ‘์ ์ธ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ model.eval()์€ ํ‰๊ฐ€ ์‹œ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋‚˜์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with torch.no_grad():๋Š” ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ์™€ ์†๋„ ์ธก๋ฉด์—์„œ ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. def evaluate(model, valid_dataloader, criterion, device): val_loss = 0 val_correct = 0 val_total = 0 model.eval() with torch.no_grad(): # ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ๋ถ€ํ„ฐ ๋ฐฐ์น˜ ํฌ๊ธฐ๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ์†์œผ๋กœ ๋กœ๋“œ for batch_X, batch_y in valid_dataloader: batch_X, batch_y = batch_X.to(device), batch_y.to(device) # ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’ logits = model(batch_X) # ์†์‹ค์„ ๊ณ„์‚ฐ loss = criterion(logits, batch_y) # ์ •ํ™•๋„์™€ ์†์‹ค์„ ๊ณ„์‚ฐํ•จ val_loss += loss.item() val_correct += calculate_accuracy(logits, batch_y) * batch_y.size(0) val_total += batch_y.size(0) val_accuracy = val_correct / val_total val_loss /= len(valid_dataloader) return val_loss, val_accuracy 4. ํ•™์Šต num_epochs = 5 # Training loop best_val_loss = float('inf') # Training loop for epoch in range(num_epochs): # Training train_loss = 0 train_correct = 0 train_total = 0 model.train() for batch_X, batch_y in train_dataloader: # Forward pass batch_X, batch_y = batch_X.to(device), batch_y.to(device) # batch_X.shape == (batch_size, max_len) logits = model(batch_X) # Compute loss loss = criterion(logits, batch_y) # Backward pass and optimization optimizer.zero_grad() loss.backward() optimizer.step() # Calculate training accuracy and loss train_loss += loss.item() train_correct += calculate_accuracy(logits, batch_y) * batch_y.size(0) train_total += batch_y.size(0) train_accuracy = train_correct / train_total train_loss /= len(train_dataloader) # Validation val_loss, val_accuracy = evaluate(model, valid_dataloader, criterion, device) print(f'Epoch {epoch+1}/{num_epochs}:') print(f'Train Loss: {train_loss:.4f}, Train Accuracy: {train_accuracy:.4f}') print(f'Validation Loss: {val_loss:.4f}, Validation Accuracy: {val_accuracy:.4f}') # ๊ฒ€์ฆ ์†์‹ค์ด ์ตœ์†Œ์ผ ๋•Œ ์ฒดํฌํฌ์ธํŠธ ์ €์žฅ if val_loss < best_val_loss: print(f'Validation loss improved from {best_val_loss:.4f} to {val_loss:.4f}. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.') best_val_loss = val_loss torch.save(model.state_dict(), 'best_model_checkpoint.pth') 5. ๋ชจ๋ธ ๋กœ๋“œ ๋ฐ ํ‰๊ฐ€ # ๋ชจ๋ธ ๋กœ๋“œ model.load_state_dict(torch.load('best_model_checkpoint.pth')) # ๋ชจ๋ธ์„ device์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. model.to(device) # ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ val_loss, val_accuracy = evaluate(model, valid_dataloader, criterion, device) print(f'Best model validation loss: {val_loss:.4f}') print(f'Best model validation accuracy: {val_accuracy:.4f}') Best model validation loss: 0.3392 Best model validation accuracy: 0.8490 # ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ test_loss, test_accuracy = evaluate(model, test_dataloader, criterion, device) print(f'Best model test loss: {test_loss:.4f}') print(f'Best model test accuracy: {test_accuracy:.4f}') Best model test loss: 0.3435 Best model test accuracy: 0.8492 6. ๋ชจ๋ธ ํ…Œ์ŠคํŠธ index_to_tag = {0 : '๋ถ€์ •', 1 : '๊ธ์ •'} def predict(text, model, word_to_index, index_to_tag): # Set the model to evaluation mode model.eval() # Tokenize the input text tokens = mecab.morphs(text) # ํ† ํฐํ™” tokens = [word for word in tokens if not word in stopwords] # ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ token_indices = [word_to_index.get(token, 1) for token in tokens] # Convert tokens to tensor input_tensor = torch.tensor([token_indices], dtype=torch.long).to(device) # (1, seq_length) # Pass the input tensor through the model with torch.no_grad(): logits = model(input_tensor) # (1, output_dim) # Get the predicted class index predicted_index = torch.argmax(logits, dim=1) # Convert the predicted index to its corresponding tag predicted_tag = index_to_tag[predicted_index.item()] return predicted_tag test_input = "์ด ์˜ํ™” ๊ฐœ๊ฟ€ ์žผ ใ…‹ใ…‹ใ…‹" predict(test_input, model, word_to_index, index_to_tag) ๊ธ์ • test_input = "์ด๋”ด ๊ฒŒ ์˜ํ™”๋ƒ ใ…‰ใ…‰" predict(test_input, model, word_to_index, index_to_tag) ๋ถ€์ • test_input = "๊ฐ๋… ๋ญ ํ•˜๋Š” ๋†ˆ์ด๋ƒ?" predict(test_input, model, word_to_index, index_to_tag) ๋ถ€์ • test_input = "์™€ ๊ฐœ์ฉ๋‹ค ์ •๋ง ์„ธ๊ณ„๊ด€ ์ตœ๊ฐ•์ž๋“ค์˜ ์˜ํ™”๋‹ค" predict(test_input, model, word_to_index, index_to_tag) ๊ธ์ • 13-03 GRU๋ฅผ ์ด์šฉํ•œ IMDB ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ํ•˜๊ธฐ 1D CNN์„ ์ด์šฉํ•˜์—ฌ IMDB ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฅ˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ๋‹จ์–ด ํ† ํฐํ™” import pandas as pd import numpy as np import matplotlib.pyplot as plt import nltk import torch import urllib.request from tqdm import tqdm from collections import Counter from nltk.tokenize import word_tokenize from sklearn.model_selection import train_test_split nltk.download('punkt') urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/pytorch-nlp-tutorial/main/10.%20RNN%20Text%20Classification/dataset/IMDB%20Dataset.csv", filename="IMDB Dataset.csv") ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์ธ IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. df = pd.read_csv('IMDB Dataset.csv') df ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋Š” ์ด 5๋งŒ ๊ฐœ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์— ๊ฒฐ์ธก๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” info()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 50000 entries, 0 to 49999 Data columns (total 2 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 review 50000 non-null object 1 sentiment 50000 non-null object dtypes: object(2) memory usage: 781.4+ KB review ์—ด๊ณผ sentiment ์—ด ๋ชจ๋‘ non-null(๊ฒฐ์ธก๊ฐ’์ด ์•„๋‹Œ) ๋ฐ์ดํ„ฐ๊ฐ€ 5๋งŒ ๊ฐœ๋กœ ํ™•์ธ๋˜๋ฏ€๋กœ ๊ฒฐ์ธก๊ฐ’์€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฒฐ์ธก๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ธ. isnull().values.any()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐ์ธก๊ฐ’ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('๊ฒฐ์ธก๊ฐ’ ์—ฌ๋ถ€ :',df.isnull().values.any()) ๊ฒฐ์ธก๊ฐ’ ์—ฌ๋ถ€ : False False๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค๋ฉด ๊ฒฐ์ธก๊ฐ’์€ ์—†๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์ด ๊ท ๋“ฑํ•œ์ง€ Bar Chart๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. df['sentiment'].value_counts().plot(kind='bar') ๋ ˆ์ด๋ธ”์˜ ์‹ค์ œ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('๋ ˆ์ด๋ธ” ๊ฐœ์ˆ˜') print(df.groupby('sentiment').size().reset_index(name='count')) ๋ ˆ์ด๋ธ” ๊ฐœ์ˆ˜ sentiment count 0 negative 25000 1 positive 25000 ๋‘ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์ด ํ˜„์žฌ 'positive'์™€ 'negative'๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์œผ๋ฏ€๋กœ ๊ฐ๊ฐ 1, 0์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์ •์ƒ ๋ณ€ํ™˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒ์œ„ 5๊ฐœ์˜ ํ–‰์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. df['sentiment'] = df['sentiment'].replace(['positive','negative'],[1, 0]) df.head() ๊ธ์ • ๋ ˆ์ด๋ธ”์€ 1, ๋ถ€์ • ๋ ˆ์ด๋ธ”์€ 0์œผ๋กœ ๋ณ€ํ™˜๋œ ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. 'review' ์—ด์€ X_data, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” 'sentiment' ์—ด์€ y_data์— ์ €์žฅ ํ›„ ์ •์ƒ ๋ณ€ํ™˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. X_data = df['review'] y_data = df['sentiment'] print('์˜ํ™” ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜: {}'.format(len(X_data))) print('๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜: {}'.format(len(y_data))) ์˜ํ™” ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜: 50000 ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜: 50000 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ์šฐ์„  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 5:5 ๋น„์œจ๋กœ ๋‚˜๋ˆ„๊ณ , ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ 8:2 ๋น„์œจ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. sklearn์˜ train_test_split์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆŒ ๋•Œ ๊ต‰์žฅํžˆ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋„๊ตฌ์ด๋ฏ€๋กœ ๊ผญ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆŒ ๋•Œ ๋ ˆ์ด๋ธ”์˜ ๋น„์œจ์„ ์œ ์ง€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ stratify์— ๋ช…์‹œํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.5, random_state=0, stratify=y_data) X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=.2, random_state=0, stratify=y_train) print('--------ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'๋ถ€์ • ๋ฆฌ๋ทฐ = {round(y_train.value_counts()[0]/len(y_train) * 100,3)}%') print(f'๊ธ์ • ๋ฆฌ๋ทฐ = {round(y_train.value_counts()[1]/len(y_train) * 100,3)}%') print('--------๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'๋ถ€์ • ๋ฆฌ๋ทฐ = {round(y_valid.value_counts()[0]/len(y_valid) * 100,3)}%') print(f'๊ธ์ • ๋ฆฌ๋ทฐ = {round(y_valid.value_counts()[1]/len(y_valid) * 100,3)}%') print('--------ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'๋ถ€์ • ๋ฆฌ๋ทฐ = {round(y_test.value_counts()[0]/len(y_test) * 100,3)}%') print(f'๊ธ์ • ๋ฆฌ๋ทฐ = {round(y_test.value_counts()[1]/len(y_test) * 100,3)}%') --------ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ๋ถ€์ • ๋ฆฌ๋ทฐ = 50.0% ๊ธ์ • ๋ฆฌ๋ทฐ = 50.0% --------๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ๋ถ€์ • ๋ฆฌ๋ทฐ = 50.0% ๊ธ์ • ๋ฆฌ๋ทฐ = 50.0% --------ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ๋ถ€์ • ๋ฆฌ๋ทฐ = 50.0% ๊ธ์ • ๋ฆฌ๋ทฐ = 50.0% ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์„ธ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ๋ชจ๋‘ ๊ธ์ • ๋ ˆ์ด๋ธ”๊ณผ ๋ถ€์ • ๋ ˆ์ด๋ธ” ๋ชจ๋‘ 50:50์œผ๋กœ ๋ ˆ์ด๋ธ”์ด ๊ท ๋“ฑํ•˜๊ฒŒ ์œ ์ง€๋œ ์ฑ„ ๋ถ„ํ• ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ํ† ํฐํ™”๋ฅผ ์œ„ํ•ด ํ† ํฐํ™” ํ•จ์ˆ˜ tokenize()๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ† ํฐํ™” ์ง„ํ–‰ ์‹œ์— ์„ ํƒ์ ์œผ๋กœ ์†Œ๋ฌธ์žํ™”๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์†Œ๋ฌธ์žํ™”๋„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ๋ฌธ์ž์—ด์—. lower()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋ชจ๋‘ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. def tokenize(sentences): tokenized_sentences = [] for sent in tqdm(sentences): tokenized_sent = word_tokenize(sent) tokenized_sent = [word.lower() for word in tokenized_sent] tokenized_sentences.append(tokenized_sent) return tokenized_sentences tokenized_X_train = tokenize(X_train) tokenized_X_valid = tokenize(X_valid) tokenized_X_test = tokenize(X_test) ํ† ํฐ ํ™”๊ฐ€ ์ง„ํ–‰๋œ ํ›„์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ 2๊ฐœ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด๋ด…์‹œ๋‹ค. # ์ƒ์œ„ ์ƒ˜ํ”Œ 2๊ฐœ ์ถœ๋ ฅ for sent in tokenized_X_train[:2]: print(sent) ['have', 'you', 'ever', ',', 'or', 'do', 'you', 'have', ',', 'a', 'pet', 'who', "'s", 'been', 'with', 'you', 'through', 'thick', 'and', 'thin', ',', 'who', 'you', "'d", 'be', 'lost', 'without', ',', 'and', 'who', 'you', 'love', 'no', 'matter', 'what', '?', 'betcha', 'never', 'thought', 'they', 'feel', 'the', 'same', 'way', 'about', 'you', '!', '<', 'br', '/', '>', '<', 'br', '/', '>', 'wonderful', ... ์ค‘๋žต ...] ['i', 'hate', 'football', '!', '!', 'i', 'hate', 'football', 'fans', '!', 'i', 'hate', 'cars', '!', 'but', 'this', 'film', 'was', 'the', 'funniest', 'thing', 'i', 'have', 'seen', 'in', 'quite', 'some', 'time', '.', '<', 'br', '/', '>', '<', 'br', '/', '>', 'i', 'was', 'given', 'the', 'great', 'opportunity', 'to', 'see', 'this', 'film', 'at', 'the', 'weekend', ',', 'and', 'all', 'i', 'have', 'to', 'say', 'is', 'i', ... ์ค‘๋žต ...] ์ •์ƒ์ ์œผ๋กœ ํ† ํฐํ™”๋œ ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด ์ฑ…์˜ ์ง€๋ฉด์—์„œ๋Š” ์ค‘๋žตํ–ˆ์Šต๋‹ˆ๋‹ค. 2. Vocab ๋งŒ๋“ค๊ธฐ ์ด์ œ ํ† ํฐํ™”๋œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. Counter ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ํ˜„์žฌ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜๋Š” ๋‹จ์–ด ์ข…๋ฅ˜์˜ ์ด๊ฐœ์ˆ˜์™€ ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ๋“ฑ์žฅ ๋นˆ๋„๋ฅผ ์นด์šดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. word_list = [] for sent in tokenized_X_train: for word in sent: word_list.append(word) word_counts = Counter(word_list) print('์ด ๋‹จ์–ด ์ˆ˜ :', len(word_counts)) ์ด ๋‹จ์–ด ์ˆ˜ : 100586 Counter ๋ชจ๋“ˆ์„ ํ†ตํ•ด ํ™•์ธํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜๋Š” ์ด ๋‹จ์–ด ์ˆ˜๋Š” 100,586๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด 100,586๋Š” ๋‹จ์–ด๋“ค์˜ ์ง‘ํ•ฉ(set)์—์„œ์˜ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋ฏ€๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜๋Š” ์ด ๋‹จ์–ด์˜ ์ข…๋ฅ˜์˜ ๊ฐœ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, ํ˜„์žฌ word_counts์—๋Š” ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ธฐ๋ก๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜๋‹จ์–ด 'the'์™€ 'love'์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด the์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ :', word_counts['the']) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด love์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ :', word_counts['love']) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด the์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ : 265697 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด love์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ : 4984 word_counts์—๋Š” ๋‹จ์–ด์™€ ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ธฐ๋ก๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์ •๋ณด๊ฐ€ ์ด 100,586๊ฐœ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜์—ฌ vocab์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•œ ํ›„ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์ƒ์œ„ 10๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. vocab = sorted(word_counts, key=word_counts.get, reverse=True) print('๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 10๊ฐœ ๋‹จ์–ด') print(vocab[:10]) ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 10๊ฐœ ๋‹จ์–ด ['the', ',', '.', 'a', 'and', 'of', 'to', 'is', '/', '>'] ์—ฌ๊ธฐ์„œ๋Š” ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ฐฐ์ œํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 3ํšŒ ๋ฏธ๋งŒ์ธ ๋‹จ์–ด๋“ค์ด ์ด ๋ฐ์ดํ„ฐ์—์„œ ์–ผ๋งˆํผ์˜ ๋น„์ค‘์„ ์ฐจ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. threshold = 3 total_cnt = len(word_counts) # ๋‹จ์–ด์˜ ์ˆ˜ rare_cnt = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ total_freq = 0 # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๋‹จ์–ด ๋นˆ๋„์ˆ˜ ์ดํ•ฉ rare_freq = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜์˜ ์ดํ•ฉ # ๋‹จ์–ด์™€ ๋นˆ๋„์ˆ˜์˜ ์Œ(pair)์„ key์™€ value๋กœ ๋ฐ›๋Š”๋‹ค. for key, value in word_counts.items(): total_freq = total_freq + value # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์œผ๋ฉด if(value < threshold): rare_cnt = rare_cnt + 1 rare_freq = rare_freq + value print('๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ :',total_cnt) print('๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ %s ๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: %s'%(threshold - 1, rare_cnt)) print("๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ:", (rare_cnt / total_cnt)*100) print("์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ:", (rare_freq / total_freq)*100) ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ : 100586 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 2๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: 61877 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ: 61.51651323245779 ์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ: 1.3294254426463437 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ threshold ๊ฐ’์ธ 3ํšŒ ๋ฏธ๋งŒ. ์ฆ‰, 2ํšŒ ์ดํ•˜์ธ ๋‹จ์–ด๋“ค์€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋ฌด๋ ค ์ ˆ๋ฐ˜ ์ด์ƒ์„ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„๋กœ ์ฐจ์ง€ํ•˜๋Š” ๋น„์ค‘์€ ์ƒ๋Œ€์ ์œผ๋กœ ๋งค์šฐ ์ ์€ ์ˆ˜์น˜์ธ 1.32%๋ฐ–์— ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๋ž˜๋„ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 2ํšŒ ์ดํ•˜์ธ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ณ„๋กœ ์ค‘์š”ํ•˜์ง€ ์•Š์„ ๋“ฏํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๋‹จ์–ด๋“ค์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ ๋ฐฐ์ œ์‹œํ‚ค๊ฒ ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 2์ดํ•˜์ธ ๋‹จ์–ด๋“ค์˜ ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋กœ ์ œํ•œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ „์ฒด ๋‹จ์–ด ๊ฐœ์ˆ˜ ์ค‘ ๋นˆ๋„์ˆ˜ 2์ดํ•˜์ธ ๋‹จ์–ด๋Š” ์ œ๊ฑฐ. vocab_size = total_cnt - rare_cnt vocab = vocab[:vocab_size] print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', len(vocab)) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 38709 ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 2๋ฒˆ ์ดํ•˜์ธ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์ž ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ 100,586๊ฐœ์—์„œ 38,709๊ฐœ๋กœ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. ์•„์ง ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ์ž‘์—…์„ ์ง„ํ–‰ํ•˜์ง€๋Š” ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ธฐ์— ์•ž์„œ ์ •์ˆ˜ 0๊ณผ ์ •์ˆ˜ 1์—๋Š” ํŠน๋ณ„ํ•œ ์šฉ๋„์˜ ๋‹จ์–ด๋ฅผ ๋ถ€์—ฌํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜ 0์€ ํŒจ๋”ฉ์„ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ํŒจ๋”ฉ ํ† ํฐ์ธ <PAD>๋ฅผ ํ• ๋‹นํ•˜๊ณ , ์ •์ˆ˜ 1์€ OOV(Out-Of-Vocabulary) ๋ฌธ์ œ ๋ฐœ์ƒ ์‹œ์— ๋ชจ๋ฅด๋Š” ๋‹จ์–ด์— ์ •์ˆ˜ 1์„ ํ• ๋‹นํ•˜๋Š” ์šฉ๋„์ธ <UNK>๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. word_to_index = {} word_to_index['<PAD>'] = 0 word_to_index['<UNK>'] = 1 for index, word in enumerate(vocab) : word_to_index[word] = index + 2 vocab_size = len(word_to_index) print('ํŒจ๋”ฉ ํ† ํฐ๊ณผ UNK ํ† ํฐ์„ ๊ณ ๋ คํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', vocab_size) ํŒจ๋”ฉ ํ† ํฐ๊ณผ UNK ํ† ํฐ์„ ๊ณ ๋ คํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 38711 3. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ตœ์ข… ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์ธ word_to_index๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜์ธ texts_to_sequences()๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ ๋‹จ์–ด๋ฅผ word_to_index์— ๋งคํ•‘๋œ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ word_to_index์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ๊ฒฝ์šฐ์—๋Š” ์ •์ˆ˜ 1์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. def texts_to_sequences(tokenized_X_data, word_to_index): encoded_X_data = [] for sent in tokenized_X_data: index_sequences = [] for word in sent: try: index_sequences.append(word_to_index[word]) except KeyError: index_sequences.append(word_to_index['<UNK>']) encoded_X_data.append(index_sequences) return encoded_X_data encoded_X_train = texts_to_sequences(tokenized_X_train, word_to_index) encoded_X_valid = texts_to_sequences(tokenized_X_valid, word_to_index) encoded_X_test = texts_to_sequences(tokenized_X_test, word_to_index) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ง„ํ–‰๋œ ํ•™์Šต ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ 2๊ฐœ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # ์ƒ์œ„ ์ƒ˜ํ”Œ 2๊ฐœ ์ถœ๋ ฅ for sent in encoded_X_train[:2]: print(sent) [38, 29, 140, 3, 52, 54, 29, 38, 3, 5, 3406, 47, 19, 95, 22, 29, 161, 4059, 6, 1741, 3, 47, 29, 293, 39, 469, 218, 3, 6, 47, 29, 134, 71, 532, 61, 59, 25184, 130, 214, 44, 249, 2, 189, 114, 58, 29, 41, 12, 13, 10, 11, 12, 13, 10, 11, 384, 3, 384, 253, 26, 4, 57, 29, 38, 5, 2280, 1587, 23, 1477, 3, 17, 9, 5775, 8, 111, 29, 1440, 71, 532, 141, 677, 4, 16, 343, 8, 126, 17, 24, 43, 2, 75, 63, 16, 20 ... ์ค‘๋žต ...] [16, 735, 2344, 41, 41, 16, 735, 2344, 467, 41, 16, 735, 1903, 41, 25, 17, 26, 20, 2, 1588, 165, 16, 38, 128, 15, 198, 62, 75, 4, 12, 13, 10, 11, 12, 13, 10, 11, 16, 20, 360, 2, 100, 1359, 8, 77, 17, 26, 42, 2, 2394, 3, 6, 43, 16, 38, 8, 147, 9, 16, 1445, 2395, 16, 3268, 3, 6, 63, 9, 14, 184, 8, 39, 1320, 15, 2, 2382, 6, 9728, 4, 520, 3, 17, 9, 40, 2344, 26, 29, 97, 354, 8, 77, 3, 109, 604, 41, 12, 13, 10, 11 ... ์ค‘๋žต ...] ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์—ญ์œผ๋กœ ๋ณต์›ํ•ด ๋ณด๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ๋‹จ์–ด์— ์ •์ˆ˜๊ฐ€ ๋งคํ•‘๋œ word_to_index๋ฅผ ๋ฐ˜๋Œ€๋กœ ๋งŒ๋“  index_to_word๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ณ  ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋ณต์›ํ•ด ๋ด…์‹œ๋‹ค. index_to_word = {} for key, value in word_to_index.items(): index_to_word[value] = key decoded_sample = [index_to_word[word] for word in encoded_X_train[0]] print('๊ธฐ์กด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :', tokenized_X_train[0]) print('๋ณต์›๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :', decoded_sample) ๊ธฐ์กด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : ['have', 'you', 'ever', ',', 'or', 'do', ... ์ค‘๋žต ... 'heart-swelling', 'feeling', '.', 'i', 'give', 'this', '9/10', '.', 'to', 'be', 'compared', 'to', '(', 'and', 'even', 'rated', 'better', 'than', ')', 'cats', 'and', 'dogs', 'and', 'babe', '.'] ๋ณต์›๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : ['have', 'you', 'ever', ',', 'or', 'do', ... ์ค‘๋žต ... '<UNK>', 'feeling', '.', 'i', 'give', 'this', '9/10', '.', 'to', 'be', 'compared', 'to', '(', 'and', 'even', 'rated', 'better', 'than', ')', 'cats', 'and', 'dogs', 'and', 'babe', '.'] ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๊ธธ์–ด์„œ ์ค‘๋žตํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„ ๋‹ค์‹œ์—ญ์œผ๋กœ ๋ณต์›ํ•œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์€ ์ค‘๊ฐ„์— <UNK>์ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. ํŒจ๋”ฉ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธธ์ด์˜ ๋ฐ์ดํ„ฐ๋“ค์„ ๋™์ผํ•œ ๊ธธ์ด๋กœ ์ผ์น˜์‹œ์ผœ์ฃผ๋Š” ํŒจ๋”ฉ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด, ํ‰๊ท  ๊ธธ์ด, ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max(len(review) for review in encoded_X_train)) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :',sum(map(len, encoded_X_train))/len(encoded_X_train)) plt.hist([len(review) for review in encoded_X_train], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 2818 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 279.1958 ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” 2,818์ด๋ฉฐ, ๊ทธ๋ž˜ํ”„๋ฅผ ๋ดค์„ ๋•Œ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋Š” ๋Œ€์ฒด์ ์œผ๋กœ ์•ฝ 1,000๋‚ด์™ธ์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก encoded_X_train๊ณผ encoded_X_test์˜ ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ํŠน์ • ๊ธธ์ด๋กœ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ๊ธธ์ด ๋ณ€์ˆ˜๋ฅผ max_len์œผ๋กœ ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ฆฌ๋ทฐ๊ฐ€ ๋‚ด์šฉ์ด ์ž˜๋ฆฌ์ง€ ์•Š๋„๋ก ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์ ์˜ max_len์˜ ๊ฐ’์€ ๋ช‡์ผ๊นŒ์š”? ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ max_len ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ์ด ๋ช‡ % ์ธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. def below_threshold_len(max_len, nested_list): count = 0 for sentence in nested_list: if(len(sentence) <= max_len): count = count + 1 print('์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ %s ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: %s'%(max_len, (count / len(nested_list))*100)) ์ตœ๋Œ€ ๊ธธ์ด 2,818๋กœ ๋ชจ๋“  ์ƒ˜ํ”Œ์„ ํŒจ๋”ฉ ํ•˜๋Š” ๊ฒƒ์€ ์กฐ๊ธˆ ๊ณผํ•œ ์ฒ˜์‚ฌ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 500์œผ๋กœ ํ•  ๊ฒฝ์šฐ ๋ช‡ ๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์†์ƒ์‹œํ‚ค์ง€ ์•Š๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. max_len = 500 below_threshold_len(max_len, encoded_X_train) ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ 500 ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: 87.795 500์œผ๋กœ ํŒจ๋”ฉ ํ•  ๊ฒฝ์šฐ ์•ฝ 88%์˜ ์ƒ˜ํ”Œ์€ ๊ทธ๋Œ€๋กœ ๋ณด์กด๋ฉ๋‹ˆ๋‹ค. ๋” ๋งŽ์€ ์ƒ˜ํ”Œ์„ ๋ณด์กดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 500๋ณด๋‹ค ๋” ํฐ ๊ธธ์ด๋กœ ํŒจ๋”ฉ ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” 500์œผ๋กœ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํŒจ๋”ฉ์„ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ pad_sequences()๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ •ํ•˜๋ฉด ํ•ด๋‹น ๊ธธ์ด๋ณด๋‹ค ๊ธด ๋ฐ์ดํ„ฐ๋Š” ๋’ท๋ถ€๋ถ„์„ ์ž˜๋ผ์„œ ํ•ด๋‹น ๊ธธ์ด๋กœ ๋งž์ถ”๊ณ , ํ•ด๋‹น ๊ธธ์ด๋ณด๋‹ค ์งง์€ ๋ฐ์ดํ„ฐ๋Š” ๋’ค์— 0์„ ์ฑ„์›Œ์„œ ํ•ด๋‹น ๊ธธ์ด์˜ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ธธ์ด 500์œผ๋กœ ํŒจ๋”ฉ์„ ํ•˜๋ฉด ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋Š” 500์ด ๋ฉ๋‹ˆ๋‹ค. def pad_sequences(sentences, max_len): features = np.zeros((len(sentences), max_len), dtype=int) for index, sentence in enumerate(sentences): if len(sentence) != 0: features[index, :len(sentence)] = np.array(sentence)[:max_len] return features padded_X_train = pad_sequences(encoded_X_train, max_len=max_len) padded_X_valid = pad_sequences(encoded_X_valid, max_len=max_len) padded_X_test = pad_sequences(encoded_X_test, max_len=max_len) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_train.shape) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_valid.shape) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_test.shape) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (20000, 500) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (5000, 500) ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (25000, 500) 5. ๋ชจ๋ธ๋ง ์ด์ œ ๋”ฅ ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ PyTorch๋ฅผ ์ด์šฉํ•˜์—ฌ GRU ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F ํ˜„์žฌ ์‹ค์Šต ํ™˜๊ฒฝ์—์„œ GPU๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. USE_CUDA = torch.cuda.is_available() device = torch.device("cuda" if USE_CUDA else "cpu") print("cpu์™€ cuda ์ค‘ ๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•จ:", device) cpu์™€ cuda ์ค‘ ๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•จ: cuda ์ €์ž์˜ ๊ฒฝ์šฐ Colab์—์„œ GPU๋ฅผ ์„ ํƒํ•˜์—ฌ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜์—ฌ cuda๋ผ๋Š” ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด ํ† ์น˜์˜ ํ…์„œ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ 5๊ฐœ์˜ ๋ ˆ์ด๋ธ”์„ ์ถœ๋ ฅํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. train_label_tensor = torch.tensor(np.array(y_train)) valid_label_tensor = torch.tensor(np.array(y_valid)) test_label_tensor = torch.tensor(np.array(y_test)) print(train_label_tensor[:5]) tensor([1, 1, 0, 0, 0]) GRU ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. ๊ฐ ์ธต์„ ์ง€๋‚  ๋•Œ๋งˆ๋‹ค ๊ฐ ์ธต์˜ ์ถœ๋ ฅ์˜ ํฌ๊ธฐ๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ž…๋ ฅ์€ (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ฌธ์žฅ ๊ธธ์ด)์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ํ…์„œ์ž…๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ์ธต์„ ์ง€๋‚˜๊ณ  ๋‚˜๋ฉด ๊ฐ ๋‹จ์–ด๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜๋˜๋ฉด์„œ (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ฌธ์žฅ ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›)์œผ๋กœ ํ…์„œ์˜ ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ดํ›„ GRU์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ(hidden state) ๊ฐ’์„ ์ถœ๋ ฅ์ธต๊ณผ ์—ฐ๊ฒฐ์‹œํ‚ค๋Š” ์ž‘์—…์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ GRU๊ฐ€ ์ถœ๋ ฅ์ธต์œผ๋กœ ๋ณด๋Š” ๊ฒฐ๊ด๊ฐ’์˜ ์ฐจ์›์€ (๋ฐฐ์น˜ ํฌ๊ธฐ, ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›)์„ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’๋งŒ ์ „๋‹ฌํ•˜๋ฏ€๋กœ ์€๋‹‰ ์ƒํƒœ๋Š” ๋ชจ๋“  ์‹œ์ (๋ฌธ์žฅ ๊ธธ์ด) ๋งŒํผ ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹จ ํ•˜๋‚˜๋งŒ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ์ธต์€ ์ง€๋‚œ ๊ฒฐ๊ณผ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์ˆ˜)์˜ ์ฐจ์›์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ ๋ฌถ์Œ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•„์ง ๋ชจ๋ธ์„ ๋งŒ๋“ค์ง€๋Š” ์•Š์•˜์ง€๋งŒ, ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์›์„ 100, ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 32, ๋ฌธ์žฅ ๊ธธ์ด๋ฅผ 500(ํŒจ๋”ฉ ํ›„), GRU์˜ ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›์„ 128๋กœ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. - ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์› = 100 - ๋ฌธ์žฅ ๊ธธ์ด = 500 - ๋ฐฐ์น˜ ํฌ๊ธฐ = 32 - ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜ = 2๋งŒ - GRU์˜ ์€๋‹‰์ธต์˜ ํฌ๊ธฐ = 128 - ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ๊ฐœ์ˆ˜ = 2๊ฐœ ์œ„์˜ ์ •๋ณด๋“ค์„ ๊ณ ๋ คํ•˜์˜€์„ ๋•Œ ๋ชจ๋ธ ๋‚ด๋ถ€์—์„œ ๋ฐ์ดํ„ฐ์˜ ๋ณ€ํ™”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (32, 500) => ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ํ˜•ํƒœ => ์ž„๋ฒ ๋”ฉ ์ธต ํ†ต๊ณผ ํ›„ => (32, 500, 100) => GRU ํ†ต๊ณผ ํ›„ => (32, 128) => Softmax ์ถœ๋ ฅ์ธต ํ†ต๊ณผ ํ›„ => (32, 2) class TextClassifier(nn.Module): def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim): super(TextClassifier, self).__init__() self.embedding = nn.Embedding(vocab_size, embedding_dim) self.gru = nn.GRU(embedding_dim, hidden_dim, batch_first=True) self.fc = nn.Linear(hidden_dim, output_dim) # output_dim = ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ฐœ์ˆ˜ def forward(self, x): # x: (batch_size, seq_length) == (32, 500) embedded = self.embedding(x) # (batch_size, seq_length, embedding_dim) == (32, 500, 100) == (๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜, ๋ฌธ์žฅ ๊ธธ์ด, ๋‹จ์–ด ๋ฒกํ„ฐ์˜ ์ฐจ์›) gru_out, hidden = self.gru(embedded) # gru_out: (batch_size, seq_length, hidden_dim), hidden: (1, batch_size, hidden_dim) last_hidden = hidden.squeeze(0) # (batch_size, hidden_dim) logits = self.fc(last_hidden) # (batch_size, output_dim) return logits ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํŒŒ์ด ํ† ์น˜ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ฐฐ์น˜ ๋‹จ์œ„ ์—ฐ์‚ฐ์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. encoded_train = torch.tensor(padded_X_train).to(torch.int64) train_dataset = torch.utils.data.TensorDataset(encoded_train, train_label_tensor) train_dataloader = torch.utils.data.DataLoader(train_dataset, shuffle=True, batch_size=32) encoded_test = torch.tensor(padded_X_test).to(torch.int64) test_dataset = torch.utils.data.TensorDataset(encoded_test, test_label_tensor) test_dataloader = torch.utils.data.DataLoader(test_dataset, shuffle=True, batch_size=1) encoded_valid = torch.tensor(padded_X_valid).to(torch.int64) valid_dataset = torch.utils.data.TensorDataset(encoded_valid, valid_label_tensor) valid_dataloader = torch.utils.data.DataLoader(valid_dataset, shuffle=True, batch_size=1) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜๊ฐ€ 20,000๊ฐœ์˜€์œผ๋ฏ€๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 32๋กœ ํ•  ๊ฒฝ์šฐ์—๋Š” 20000/32=625 ๋‹ค์‹œ ๋งํ•ด 32๊ฐœ์”ฉ ๋ฌถ์ธ ๋ฐ์ดํ„ฐ ๋ฌถ์Œ์ด 625๊ฐœ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต ์‹œ์—๋Š” 32๊ฐœ์”ฉ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. total_batch = len(train_dataloader) print('์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : {}'.format(total_batch)) ์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : 625 ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. embedding_dim = 100 hidden_dim = 128 output_dim = 2 learning_rate = 0.01 num_epochs = 10 model = TextClassifier(vocab_size, embedding_dim, hidden_dim, output_dim) model.to(device) ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128, ์ถœ๋ ฅ์ธต์˜ ํฌ๊ธฐ(๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ฐœ์ˆ˜)๋Š” 2๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•ด์ฃผ๋Š” ๊ฐ’์ด๋ฉด์„œ ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๊ฐ’๋“ค์„ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ํ†ตํ•ด ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ง„ํ–‰ํ•˜๋ฏ€๋กœ ์†์‹ค ํ•จ์ˆ˜๋Š” nn.CrossEntropyLoss()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜๋กœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ์†์‹ค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘ ํ•˜๋‚˜์ธ ํ•™์Šต๋ฅ (learning rate)๋Š” 0.001๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) 6. ํ‰๊ฐ€ ์ฝ”๋“œ ์ž‘์„ฑ ์ดํ›„ ํ‰๊ฐ€๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ชจ๋ธ์˜ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•˜๋Š” ํ•จ์ˆ˜ calculate_accuracy()๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. def calculate_accuracy(logits, labels): # _, predicted = torch.max(logits, 1) predicted = torch.argmax(logits, dim=1) correct = (predicted == labels).sum().item() total = labels.size(0) accuracy = correct / total return accuracy ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ evaluate()๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ•จ์ˆ˜์—์„œ model.eval()๊ณผ with torch.no_grad()๋ฅผ ์งš์–ด๋ด…์‹œ๋‹ค. ์ด ๋‘ ๊ฐœ๋Š” ๋ชจ๋ธ ํ‰๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์˜๋ฏธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. model.eval(): ๋ชจ๋ธ์„ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋ธ ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๋ ˆ์ด์–ด์— ๋Œ€ํ•ด ํ‰๊ฐ€ ๋ชจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ ˆ์ด์–ด, ์˜ˆ๋ฅผ ๋“ค์–ด ๋“œ๋กญ์•„์›ƒ์ด๋‚˜ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ํ•™์Šต๊ณผ ํ‰๊ฐ€ ์‹œ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ์„ค์ •์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ชจ๋“œ๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์œผ๋ฉด, ์ด๋Ÿฌํ•œ ๋ ˆ์ด์–ด์˜ ๋™์ž‘์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋กœ ์ธํ•ด ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ์ œ๋Œ€๋กœ ๋‚˜์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with torch.no_grad(): ์ด ๋ฌธ์žฅ์€ ์ž๋™ ๋ฏธ๋ถ„ ์—”์ง„์—์„œ ๊ทธ๋ž˜๋”” ์–ธํŠธ ๊ณ„์‚ฐ์„ ๋น„ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ์ค‘์—๋Š” ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๊ณ  ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ์„ค์ •์ด ์ ์šฉ๋˜์ง€ ์•Š์œผ๋ฉด, ํ‰๊ฐ€ ๊ณผ์ •์—์„œ ๊ทธ๋ž˜๋”” ์–ธํŠธ๊ฐ€ ๊ณ„์‚ฐ๋˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ‰๊ฐ€ ๊ฒฐ๊ณผ ์ž์ฒด์—๋Š” ์ง์ ‘์ ์ธ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ model.eval()์€ ํ‰๊ฐ€ ์‹œ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋‚˜์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with torch.no_grad():๋Š” ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ์™€ ์†๋„ ์ธก๋ฉด์—์„œ ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. def evaluate(model, valid_dataloader, criterion, device): val_loss = 0 val_correct = 0 val_total = 0 model.eval() with torch.no_grad(): # ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ๋ถ€ํ„ฐ ๋ฐฐ์น˜ ํฌ๊ธฐ๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ์†์œผ๋กœ ๋กœ๋“œ for batch_X, batch_y in valid_dataloader: batch_X, batch_y = batch_X.to(device), batch_y.to(device) # ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’ logits = model(batch_X) # ์†์‹ค์„ ๊ณ„์‚ฐ loss = criterion(logits, batch_y) # ์ •ํ™•๋„์™€ ์†์‹ค์„ ๊ณ„์‚ฐํ•จ val_loss += loss.item() val_correct += calculate_accuracy(logits, batch_y) * batch_y.size(0) val_total += batch_y.size(0) val_accuracy = val_correct / val_total val_loss /= len(valid_dataloader) return val_loss, val_accuracy 7. ํ•™์Šต num_epochs = 5 # Training loop best_val_loss = float('inf') # Training loop for epoch in range(num_epochs): # Training train_loss = 0 train_correct = 0 train_total = 0 model.train() for batch_X, batch_y in train_dataloader: # Forward pass batch_X, batch_y = batch_X.to(device), batch_y.to(device) # batch_X.shape == (batch_size, max_len) logits = model(batch_X) # Compute loss loss = criterion(logits, batch_y) # Backward pass and optimization optimizer.zero_grad() loss.backward() optimizer.step() # Calculate training accuracy and loss train_loss += loss.item() train_correct += calculate_accuracy(logits, batch_y) * batch_y.size(0) train_total += batch_y.size(0) train_accuracy = train_correct / train_total train_loss /= len(train_dataloader) # Validation val_loss, val_accuracy = evaluate(model, valid_dataloader, criterion, device) print(f'Epoch {epoch+1}/{num_epochs}:') print(f'Train Loss: {train_loss:.4f}, Train Accuracy: {train_accuracy:.4f}') print(f'Validation Loss: {val_loss:.4f}, Validation Accuracy: {val_accuracy:.4f}') # ๊ฒ€์ฆ ์†์‹ค์ด ์ตœ์†Œ์ผ ๋•Œ ์ฒดํฌํฌ์ธํŠธ ์ €์žฅ if val_loss < best_val_loss: print(f'Validation loss improved from {best_val_loss:.4f} to {val_loss:.4f}. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.') best_val_loss = val_loss torch.save(model.state_dict(), 'best_model_checkpoint.pth') 8. ๋ชจ๋ธ ๋กœ๋“œ ๋ฐ ํ‰๊ฐ€ # ๋ชจ๋ธ ๋กœ๋“œ model.load_state_dict(torch.load('best_model_checkpoint.pth')) # ๋ชจ๋ธ์„ device์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. model.to(device) # ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ val_loss, val_accuracy = evaluate(model, valid_dataloader, criterion, device) print(f'Best model validation loss: {val_loss:.4f}') print(f'Best model validation accuracy: {val_accuracy:.4f}') Best model validation loss: 0.3153 Best model validation accuracy: 0.8678 # ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ test_loss, test_accuracy = evaluate(model, test_dataloader, criterion, device) print(f'Best model test loss: {test_loss:.4f}') print(f'Best model test accuracy: {test_accuracy:.4f}') Best model test loss: 0.3245 Best model test accuracy: 0.8641 9. ๋ชจ๋ธ ํ…Œ์ŠคํŠธ index_to_tag = {0 : '๋ถ€์ •', 1 : '๊ธ์ •'} def predict(text, model, word_to_index, index_to_tag): # ๋ชจ๋ธ ํ‰๊ฐ€ ๋ชจ๋“œ model.eval() # ํ† ํฐํ™” ๋ฐ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ. OOV ๋ฌธ์ œ ๋ฐœ์ƒ ์‹œ <UNK> ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค 1 ํ• ๋‹น tokens = word_tokenize(text) token_indices = [word_to_index.get(token.lower(), 1) for token in tokens] # ๋ฆฌ์ŠคํŠธ๋ฅผ ํ…์„œ๋กœ ๋ณ€๊ฒฝ input_tensor = torch.tensor([token_indices], dtype=torch.long).to(device) # (1, seq_length) # ๋ชจ๋ธ์˜ ์˜ˆ์ธก with torch.no_grad(): logits = model(input_tensor) # (1, output_dim) # ๋ ˆ์ด๋ธ” ์ธ๋ฑ์Šค ์˜ˆ์ธก _, predicted_index = torch.max(logits, dim=1) # (1, ) # ์ธ๋ฑ์Šค์™€ ๋งค์นญ๋˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ๋ฌธ์ž์—ด๋กœ ๋ณ€๊ฒฝ predicted_tag = index_to_tag[predicted_index.item()] return predicted_tag test_input = "This movie was just way too overrated. The fighting was not professional and in slow motion. I was expecting more from a 200 million budget movie. The little sister of T.Challa was just trying too hard to be funny. The story was really dumb as well. Don't watch this movie if you are going because others say its great unless you are a Black Panther fan or Marvels fan." predict(test_input, model, word_to_index, index_to_tag) ๋ถ€์ • test_input = " I was lucky enough to be included in the group to see the advanced screening in Melbourne on the 15th of April, 2012. And, firstly, I need to say a big thank-you to Disney and Marvel Studios. Now, the film... how can I even begin to explain how I feel about this film? It is, as the title of this review says a 'comic book triumph'. I went into the film with very, very high expectations and I was not disappointed. Seeing Joss Whedon's direction and envisioning of the film come to life on the big screen is perfect. The script is amazingly detailed and laced with sharp wit a humor. The special effects are literally mind-blowing and the action scenes are both hard-hitting and beautifully choreographed." predict(test_input, model, word_to_index, index_to_tag) ๊ธ์ • 13-04 ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ 1D CNN(1D Convolutional Neural Networks) ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ 1D CNN์„ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. 2D ํ•ฉ์„ฑ๊ณฑ(2D Convolutions) ์•ž์„œ ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์„ ์„ค๋ช…ํ•˜๋ฉฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์ด๋ž€ ์ปค๋„(kernel) ๋˜๋Š” ํ•„ํ„ฐ(filter)๋ผ๋Š” n ร— m ํฌ๊ธฐ์˜ ํ–‰๋ ฌ๋กœ ๋†’์ด(height) ร— ๋„ˆ๋น„(width) ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๊ฒน์น˜๋ฉฐ ํ›‘์œผ๋ฉด์„œ n ร— m ํฌ๊ธฐ์˜ ๊ฒน์ณ์ง€๋Š” ๋ถ€๋ถ„์˜ ๊ฐ ์ด๋ฏธ์ง€์™€ ์ปค๋„์˜ ์›์†Œ์˜ ๊ฐ’์„ ๊ณฑํ•ด์„œ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ์ด๋ฏธ์ง€์˜ ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„๋ถ€ํ„ฐ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ํ›‘์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์—์„œ์˜ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ 2D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 2. 1D ํ•ฉ์„ฑ๊ณฑ(1D Convolutions) ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉ๋˜๋Š” 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์ •๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. LSTM์„ ์ด์šฉํ•œ ์—ฌ๋Ÿฌ ์‹ค์Šต์„ ์ƒ๊ธฐํ•ด ๋ณด๋ฉด, ๊ฐ ๋ฌธ์žฅ์€ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์„ ์ง€๋‚˜์„œ ๊ฐ ๋‹จ์–ด๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๊ฐ€ ๋œ ์ƒํƒœ๋กœ LSTM์˜ ์ž…๋ ฅ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ๋„ ์ž…๋ ฅ์ด ๋˜๋Š” ๊ฒƒ์€ ๊ฐ ๋‹จ์–ด๊ฐ€ ๋ฒกํ„ฐ๋กœ ๋ณ€ํ™˜๋œ ๋ฌธ์žฅ ํ–‰๋ ฌ๋กœ LSTM๊ณผ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ํ˜•ํƒœ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. 'wait for the video and don't rent it'์ด๋ผ๋Š” ๋ฌธ์žฅ์ด ์žˆ์„ ๋•Œ, ์ด ๋ฌธ์žฅ์ด ํ† ํฐํ™”, ํŒจ๋”ฉ, ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)์„ ๊ฑฐ์นœ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ ํ˜•ํƒœ์˜ ํ–‰๋ ฌ๋กœ ๋ณ€ํ™˜๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ์€ ๋ฌธ์žฅ์˜ ๊ธธ์ด,๋Š” ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ–‰๋ ฌ์ด ๋งŒ์•ฝ LSTM์˜ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค๋ฉด, LSTM์€ ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์—๋Š” ์ฒซ ๋ฒˆ์งธ ํ–‰์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ณ , ๋‘ ๋ฒˆ์งธ ์‹œ์ ์—๋Š” ๋‘ ๋ฒˆ์งธ ํ–‰์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์œผ๋ฉฐ ์ˆœ์ฐจ์ ์œผ๋กœ ๋‹จ์–ด๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฝ์šฐ์—๋Š” ์ € ํ–‰๋ ฌ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ๊นŒ์š”? 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ ์ปค๋„์˜ ๋„ˆ๋น„๋Š” ๋ฌธ์žฅ ํ–‰๋ ฌ์—์„œ์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ ๋™์ผํ•˜๊ฒŒ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ๋Š” ์ปค๋„์˜ ๋†’์ด๋งŒ์œผ๋กœ ํ•ด๋‹น ์ปค๋„์˜ ํฌ๊ธฐ๋ผ๊ณ  ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ 2์ธ ๊ฒฝ์šฐ์—๋Š” ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋†’์ด๊ฐ€ 2, ๋„ˆ๋น„๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ธ ์ปค๋„์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ปค๋„์˜ ๋„ˆ๋น„๊ฐ€ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด๋ผ๋Š” ์˜๋ฏธ๋Š” ์ปค๋„์ด 2D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ๋•Œ์™€๋Š” ๋‹ฌ๋ฆฌ ๋„ˆ๋น„ ๋ฐฉํ–ฅ์œผ๋กœ๋Š” ๋” ์ด์ƒ ์›€์ง์ผ ๊ณณ์ด ์—†๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ๋Š” ์ปค๋„์ด ๋ฌธ์žฅ ํ–‰๋ ฌ์˜ ๋†’์ด ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์›€์ง์ด๊ฒŒ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด, ์œ„ ๊ทธ๋ฆผ์—์„œ ์ปค๋„์€ 2D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ ๋•Œ์™€๋Š” ๋‹ฌ๋ฆฌ ์˜ค๋ฅธ์ชฝ์œผ๋กœ๋Š” ์›€์ง์ผ ๊ณต๊ฐ„์ด ์—†์œผ๋ฏ€๋กœ, ์•„๋ž˜์ชฝ์œผ๋กœ๋งŒ ์ด๋™ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ 1 ์Šคํ…(step)์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๋„ค ๋ฒˆ์งธ ์Šคํ…๊นŒ์ง€ ํ‘œํ˜„ํ•œ ์ด๋ฏธ์ง€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํฌ๊ธฐ๊ฐ€ 2์ธ ์ปค๋„์€ ์ฒ˜์Œ์—๋Š” 'wait for'์— ๋Œ€ํ•ด์„œ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์Šคํ…์—๋Š” 'for the'์— ๋Œ€ํ•ด์„œ ์—ฐ์‚ฐ์„, ์„ธ ๋ฒˆ์งธ ์Šคํ…์—๋Š” 'the video'์— ๋Œ€ํ•ด์„œ ์—ฐ์‚ฐ์„, ๋„ค ๋ฒˆ์งธ ์Šคํ…์—์„œ๋Š” 'video and'์— ๋Œ€ํ•ด์„œ ์—ฐ์‚ฐ์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿ ๋ฒˆ์งธ ์Šคํ…๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜์˜€์„ ๋•Œ, ๊ฒฐ๊ณผ์ ์œผ๋กœ๋Š” ์šฐ์ธก์˜ 8์ฐจ์› ๋ฒกํ„ฐ๋ฅผ 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋กœ์„œ ์–ป๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ ๊ผญ 2์ผ ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ์š”? 2D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ 3 ร— 3 ๋˜๋Š” 5 ร— 5 ๋˜๋Š” ๋“ฑ๋“ฑ์˜ ์—ฌ๋Ÿฌ ํฌ๊ธฐ์˜ ์ปค๋„์„ ์ž์œ ์ž์žฌ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋“ฏ์ด, 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์—์„œ๋„ ์ปค๋„์˜ ํฌ๊ธฐ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์ปค๋„์˜ ํฌ๊ธฐ๋ฅผ 3์œผ๋กœ ํ•œ๋‹ค๋ฉด, ๋„ค ๋ฒˆ์งธ ์Šคํ…์—์„œ์˜ ์—ฐ์‚ฐ์€ ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค๋Š” ๊ฒƒ์€ ์–ด๋–ค ์˜๋ฏธ๊ฐ€ ์žˆ์„๊นŒ์š”? CNN์—์„œ์˜ ์ปค๋„์€ ์‹ ๊ฒฝ๋ง ๊ด€์ ์—์„œ๋Š” ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์ด๋ฏ€๋กœ ์ปค๋„์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ํ•™์Šตํ•˜๊ฒŒ ๋˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ˆ˜๋Š” ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ๊ณผ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ด€์ ์—์„œ๋Š” ์ปค๋„์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ์„œ ์ฐธ๊ณ ํ•˜๋Š” ๋‹จ์–ด์˜ ๋ฌถ์Œ์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด๋Š” ์ฐธ๊ณ ํ•˜๋Š” n-gram์ด ๋‹ฌ๋ผ์ง„๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ 2๋ผ๋ฉด ๊ฐ ์—ฐ์‚ฐ์˜ ์Šคํ…์—์„œ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์€ bigram์ž…๋‹ˆ๋‹ค. ์ปค๋„์˜ ํฌ๊ธฐ๊ฐ€ 3์ด๋ผ๋ฉด ๊ฐ ์—ฐ์‚ฐ์˜ ์Šคํ…์—์„œ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์€ trigram์ž…๋‹ˆ๋‹ค. 3. ๋งฅ์Šค ํ’€๋ง(Max-pooling) ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ์—์„œ์˜ CNN์—์„œ ๊ทธ๋žฌ๋“ฏ์ด, ์ผ๋ฐ˜์ ์œผ๋กœ 1D ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜๋Š” 1D CNN์—์„œ๋„ ํ•ฉ์„ฑ๊ณฑ ์ธต(ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ + ํ™œ์„ฑํ™” ํ•จ์ˆ˜) ๋‹ค์Œ์—๋Š” ํ’€๋ง ์ธต์„ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ์ค‘ ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ๋งฅ์Šค ํ’€๋ง(Max-pooling)์ž…๋‹ˆ๋‹ค. ๋งฅ์Šค ํ’€๋ง์€ ๊ฐ ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์œผ๋กœ๋ถ€ํ„ฐ ์–ป์€ ๊ฒฐ๊ณผ ๋ฒกํ„ฐ์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ฐ€์ง„ ์Šค์นผ๋ผ ๊ฐ’์„ ๋นผ๋‚ด๋Š” ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ํฌ๊ธฐ๊ฐ€ 2์ธ ์ปค๋„๊ณผ ํฌ๊ธฐ๊ฐ€ 3์ธ ์ปค๋„ ๋‘ ๊ฐœ์˜ ์ปค๋„๋กœ๋ถ€ํ„ฐ ๊ฐ๊ฐ ๊ฒฐ๊ณผ ๋ฒกํ„ฐ๋ฅผ ์–ป๊ณ , ๊ฐ ๋ฒกํ„ฐ์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊บผ๋‚ด์˜ค๋Š” ๋งฅ์Šค ํ’€๋ง ์—ฐ์‚ฐ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. 4. ์‹ ๊ฒฝ๋ง ์„ค๊ณ„ํ•˜๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๊ฐœ๋…๋“ค์„ ๊ฐ€์ง€๊ณ  ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ CNN์„ ์„ค๊ณ„ํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„ , ์„ค๊ณ„ํ•˜๊ณ ์ž ํ•˜๋Š” ์‹ ๊ฒฝ๋ง์€ ์ด์ง„ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ ์‹ ๊ฒฝ๋ง์ž…๋‹ˆ๋‹ค. ๋‹จ, ์‹œ๊ทธ๋ชจ์ด๋“œ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์ถœ๋ ฅ์ธต์—์„œ ๋‰ด๋Ÿฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 2์ธ ์‹ ๊ฒฝ๋ง์„ ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ์ปค๋„์€ ํฌ๊ธฐ๊ฐ€ 4์ธ ์ปค๋„ 2๊ฐœ, 3์ธ ์ปค๋„ 2๊ฐœ, 2์ธ ์ปค๋„ 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ 9์ธ ๊ฒฝ์šฐ, ํ•ฉ์„ฑ๊ณฑ ์—ฐ์‚ฐ์„ ํ•œ ํ›„์—๋Š” ๊ฐ๊ฐ 6์ฐจ์› ๋ฒกํ„ฐ 2๊ฐœ, 7์ฐจ์› ๋ฒกํ„ฐ 2๊ฐœ, 8์ฐจ์› ๋ฒกํ„ฐ 2๊ฐœ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋ฒกํ„ฐ๊ฐ€ 6๊ฐœ๋ฏ€๋กœ ๋งฅ์Šค ํ’€๋ง์„ ํ•œ ํ›„์—๋Š” 6๊ฐœ์˜ ์Šค์นผ๋ผ ๊ฐ’์„ ์–ป๋Š”๋ฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋ ‡๊ฒŒ ์–ป์€ ์Šค์นผ๋ผ ๊ฐ’๋“ค์€ ์ „๋ถ€ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์–ป์€ ๋ฒกํ„ฐ๋Š” 1D CNN์„ ํ†ตํ•ด์„œ ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์–ป์€ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๋‰ด๋Ÿฐ์ด 2๊ฐœ์ธ ์ถœ๋ ฅ์ธต์— ์™„์ „ ์—ฐ๊ฒฐ์‹œํ‚ค๋ฏ€๋กœ์„œ(nn.Linear()๋ฅผ ์‚ฌ์šฉ) ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 13-05 1D CNN์„ ์ด์šฉํ•œ IMDB ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜ 1D CNN์„ ์ด์šฉํ•˜์—ฌ IMDB ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฅ˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ๋‹จ์–ด ํ† ํฐํ™” import pandas as pd import numpy as np import matplotlib.pyplot as plt import nltk import torch import urllib.request from tqdm import tqdm from collections import Counter from nltk.tokenize import word_tokenize from sklearn.model_selection import train_test_split nltk.download('punkt') urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/pytorch-nlp-tutorial/main/10.%20RNN%20Text%20Classification/dataset/IMDB%20Dataset.csv", filename="IMDB Dataset.csv") ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์ธ IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. df = pd.read_csv('IMDB Dataset.csv') df ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋Š” ์ด 5๋งŒ ๊ฐœ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์— ๊ฒฐ์ธก๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” info()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 50000 entries, 0 to 49999 Data columns (total 2 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 review 50000 non-null object 1 sentiment 50000 non-null object dtypes: object(2) memory usage: 781.4+ KB review ์—ด๊ณผ sentiment ์—ด ๋ชจ๋‘ non-null(๊ฒฐ์ธก๊ฐ’์ด ์•„๋‹Œ) ๋ฐ์ดํ„ฐ๊ฐ€ 5๋งŒ ๊ฐœ๋กœ ํ™•์ธ๋˜๋ฏ€๋กœ ๊ฒฐ์ธก๊ฐ’์€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฒฐ์ธก๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ธ. isnull().values.any()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐ์ธก๊ฐ’ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('๊ฒฐ์ธก๊ฐ’ ์—ฌ๋ถ€ :',df.isnull().values.any()) ๊ฒฐ์ธก๊ฐ’ ์—ฌ๋ถ€ : False False๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค๋ฉด ๊ฒฐ์ธก๊ฐ’์€ ์—†๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์ด ๊ท ๋“ฑํ•œ์ง€ Bar Chart๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. df['sentiment'].value_counts().plot(kind='bar') ๋ ˆ์ด๋ธ”์˜ ์‹ค์ œ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('๋ ˆ์ด๋ธ” ๊ฐœ์ˆ˜') print(df.groupby('sentiment').size().reset_index(name='count')) ๋ ˆ์ด๋ธ” ๊ฐœ์ˆ˜ sentiment count 0 negative 25000 1 positive 25000 ๋‘ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์ด ํ˜„์žฌ 'positive'์™€ 'negative'๋กœ ๊ตฌ์„ฑ๋ผ ์žˆ์œผ๋ฏ€๋กœ ๊ฐ๊ฐ 1, 0์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์ •์ƒ ๋ณ€ํ™˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ƒ์œ„ 5๊ฐœ์˜ ํ–‰์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. df['sentiment'] = df['sentiment'].replace(['positive','negative'],[1, 0]) df.head() ๊ธ์ • ๋ ˆ์ด๋ธ”์€ 1, ๋ถ€์ • ๋ ˆ์ด๋ธ”์€ 0์œผ๋กœ ๋ณ€ํ™˜๋œ ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. 'review' ์—ด์€ X_data, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹นํ•˜๋Š” 'sentiment' ์—ด์€ y_data์— ์ €์žฅ ํ›„ ์ •์ƒ ๋ณ€ํ™˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. X_data = df['review'] y_data = df['sentiment'] print('์˜ํ™” ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜: {}'.format(len(X_data))) print('๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜: {}'.format(len(y_data))) ์˜ํ™” ๋ฆฌ๋ทฐ์˜ ๊ฐœ์ˆ˜: 50000 ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜: 50000 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ์šฐ์„  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ 5:5 ๋น„์œจ๋กœ ๋‚˜๋ˆ„๊ณ , ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ 8:2 ๋น„์œจ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. sklearn์˜ train_test_split์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆŒ ๋•Œ ๊ต‰์žฅํžˆ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋„๊ตฌ์ด๋ฏ€๋กœ ๊ผญ ๊ธฐ์–ตํ•ด๋‘ก์‹œ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆŒ ๋•Œ ๋ ˆ์ด๋ธ”์˜ ๋น„์œจ์„ ์œ ์ง€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ stratify์— ๋ช…์‹œํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.5, random_state=0, stratify=y_data) X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=.2, random_state=0, stratify=y_train) print('--------ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'๋ถ€์ • ๋ฆฌ๋ทฐ = {round(y_train.value_counts()[0]/len(y_train) * 100,3)}%') print(f'๊ธ์ • ๋ฆฌ๋ทฐ = {round(y_train.value_counts()[1]/len(y_train) * 100,3)}%') print('--------๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'๋ถ€์ • ๋ฆฌ๋ทฐ = {round(y_valid.value_counts()[0]/len(y_valid) * 100,3)}%') print(f'๊ธ์ • ๋ฆฌ๋ทฐ = {round(y_valid.value_counts()[1]/len(y_valid) * 100,3)}%') print('--------ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ-----------') print(f'๋ถ€์ • ๋ฆฌ๋ทฐ = {round(y_test.value_counts()[0]/len(y_test) * 100,3)}%') print(f'๊ธ์ • ๋ฆฌ๋ทฐ = {round(y_test.value_counts()[1]/len(y_test) * 100,3)}%') --------ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ๋ถ€์ • ๋ฆฌ๋ทฐ = 50.0% ๊ธ์ • ๋ฆฌ๋ทฐ = 50.0% --------๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ๋ถ€์ • ๋ฆฌ๋ทฐ = 50.0% ๊ธ์ • ๋ฆฌ๋ทฐ = 50.0% --------ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋น„์œจ----------- ๋ถ€์ • ๋ฆฌ๋ทฐ = 50.0% ๊ธ์ • ๋ฆฌ๋ทฐ = 50.0% ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์„ธ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ๋ชจ๋‘ ๊ธ์ • ๋ ˆ์ด๋ธ”๊ณผ ๋ถ€์ • ๋ ˆ์ด๋ธ” ๋ชจ๋‘ 50:50์œผ๋กœ ๋ ˆ์ด๋ธ”์ด ๊ท ๋“ฑํ•˜๊ฒŒ ์œ ์ง€๋œ ์ฑ„ ๋ถ„ํ• ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ํ† ํฐํ™”๋ฅผ ์œ„ํ•ด ํ† ํฐํ™” ํ•จ์ˆ˜ tokenize()๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ† ํฐํ™” ์ง„ํ–‰ ์‹œ์— ์„ ํƒ์ ์œผ๋กœ ์†Œ๋ฌธ์žํ™”๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์†Œ๋ฌธ์žํ™”๋„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ ๋ฌธ์ž์—ด์—. lower()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ๋ชจ๋‘ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. def tokenize(sentences): tokenized_sentences = [] for sent in tqdm(sentences): tokenized_sent = word_tokenize(sent) tokenized_sent = [word.lower() for word in tokenized_sent] tokenized_sentences.append(tokenized_sent) return tokenized_sentences tokenized_X_train = tokenize(X_train) tokenized_X_valid = tokenize(X_valid) tokenized_X_test = tokenize(X_test) ํ† ํฐ ํ™”๊ฐ€ ์ง„ํ–‰๋œ ํ›„์˜ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ 2๊ฐœ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด๋ด…์‹œ๋‹ค. # ์ƒ์œ„ ์ƒ˜ํ”Œ 2๊ฐœ ์ถœ๋ ฅ for sent in tokenized_X_train[:2]: print(sent) ['have', 'you', 'ever', ',', 'or', 'do', 'you', 'have', ',', 'a', 'pet', 'who', "'s", 'been', 'with', 'you', 'through', 'thick', 'and', 'thin', ',', 'who', 'you', "'d", 'be', 'lost', 'without', ',', 'and', 'who', 'you', 'love', 'no', 'matter', 'what', '?', 'betcha', 'never', 'thought', 'they', 'feel', 'the', 'same', 'way', 'about', 'you', '!', '<', 'br', '/', '>', '<', 'br', '/', '>', 'wonderful', ... ์ค‘๋žต ...] ['i', 'hate', 'football', '!', '!', 'i', 'hate', 'football', 'fans', '!', 'i', 'hate', 'cars', '!', 'but', 'this', 'film', 'was', 'the', 'funniest', 'thing', 'i', 'have', 'seen', 'in', 'quite', 'some', 'time', '.', '<', 'br', '/', '>', '<', 'br', '/', '>', 'i', 'was', 'given', 'the', 'great', 'opportunity', 'to', 'see', 'this', 'film', 'at', 'the', 'weekend', ',', 'and', 'all', 'i', 'have', 'to', 'say', 'is', 'i', ... ์ค‘๋žต ...] ์ •์ƒ์ ์œผ๋กœ ํ† ํฐํ™”๋œ ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด ์ฑ…์˜ ์ง€๋ฉด์—์„œ๋Š” ์ค‘๋žตํ–ˆ์Šต๋‹ˆ๋‹ค. 2. Vocab ๋งŒ๋“ค๊ธฐ ์ด์ œ ํ† ํฐํ™”๋œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. Counter ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ํ˜„์žฌ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜๋Š” ๋‹จ์–ด ์ข…๋ฅ˜์˜ ์ด๊ฐœ์ˆ˜์™€ ๊ฐ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ๋“ฑ์žฅ ๋นˆ๋„๋ฅผ ์นด์šดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. word_list = [] for sent in tokenized_X_train: for word in sent: word_list.append(word) word_counts = Counter(word_list) print('์ด ๋‹จ์–ด ์ˆ˜ :', len(word_counts)) ์ด ๋‹จ์–ด ์ˆ˜ : 100586 Counter ๋ชจ๋“ˆ์„ ํ†ตํ•ด ํ™•์ธํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜๋Š” ์ด ๋‹จ์–ด ์ˆ˜๋Š” 100,586๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด 100,586๋Š” ๋‹จ์–ด๋“ค์˜ ์ง‘ํ•ฉ(set)์—์„œ์˜ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋ฏ€๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์กด์žฌํ•˜๋Š” ์ด ๋‹จ์–ด์˜ ์ข…๋ฅ˜์˜ ๊ฐœ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, ํ˜„์žฌ word_counts์—๋Š” ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ธฐ๋ก๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜๋‹จ์–ด 'the'์™€ 'love'์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด the์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ :', word_counts['the']) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด love์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ :', word_counts['love']) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด the์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ : 265697 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด love์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ : 4984 word_counts์—๋Š” ๋‹จ์–ด์™€ ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ธฐ๋ก๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์ •๋ณด๊ฐ€ ์ด 100,586๊ฐœ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜์—ฌ vocab์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•œ ํ›„ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์ƒ์œ„ 10๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. vocab = sorted(word_counts, key=word_counts.get, reverse=True) print('๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 10๊ฐœ ๋‹จ์–ด') print(vocab[:10]) ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 10๊ฐœ ๋‹จ์–ด ['the', ',', '.', 'a', 'and', 'of', 'to', 'is', '/', '>'] ์—ฌ๊ธฐ์„œ๋Š” ๋นˆ๋„์ˆ˜๊ฐ€ ๋‚ฎ์€ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ฐฐ์ œํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 3ํšŒ ๋ฏธ๋งŒ์ธ ๋‹จ์–ด๋“ค์ด ์ด ๋ฐ์ดํ„ฐ์—์„œ ์–ผ๋งˆํผ์˜ ๋น„์ค‘์„ ์ฐจ์ง€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. threshold = 3 total_cnt = len(word_counts) # ๋‹จ์–ด์˜ ์ˆ˜ rare_cnt = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ total_freq = 0 # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด ๋‹จ์–ด ๋นˆ๋„์ˆ˜ ์ดํ•ฉ rare_freq = 0 # ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์€ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜์˜ ์ดํ•ฉ # ๋‹จ์–ด์™€ ๋นˆ๋„์ˆ˜์˜ ์Œ(pair)์„ key์™€ value๋กœ ๋ฐ›๋Š”๋‹ค. for key, value in word_counts.items(): total_freq = total_freq + value # ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ threshold๋ณด๋‹ค ์ž‘์œผ๋ฉด if(value < threshold): rare_cnt = rare_cnt + 1 rare_freq = rare_freq + value print('๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ :',total_cnt) print('๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ %s ๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: %s'%(threshold - 1, rare_cnt)) print("๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ:", (rare_cnt / total_cnt)*100) print("์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ:", (rare_freq / total_freq)*100) ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์˜ ํฌ๊ธฐ : 100586 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 2๋ฒˆ ์ดํ•˜์ธ ํฌ๊ท€ ๋‹จ์–ด์˜ ์ˆ˜: 61877 ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ํฌ๊ท€ ๋‹จ์–ด์˜ ๋น„์œจ: 61.51651323245779 ์ „์ฒด ๋“ฑ์žฅ ๋นˆ๋„์—์„œ ํฌ๊ท€ ๋‹จ์–ด ๋“ฑ์žฅ ๋นˆ๋„ ๋น„์œจ: 1.3294254426463437 ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ threshold ๊ฐ’์ธ 3ํšŒ ๋ฏธ๋งŒ. ์ฆ‰, 2ํšŒ ์ดํ•˜์ธ ๋‹จ์–ด๋“ค์€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ๋ฌด๋ ค ์ ˆ๋ฐ˜ ์ด์ƒ์„ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‹ค์ œ๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ ๋“ฑ์žฅ ๋นˆ๋„๋กœ ์ฐจ์ง€ํ•˜๋Š” ๋น„์ค‘์€ ์ƒ๋Œ€์ ์œผ๋กœ ๋งค์šฐ ์ ์€ ์ˆ˜์น˜์ธ 1.32%๋ฐ–์— ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๋ž˜๋„ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ 2ํšŒ ์ดํ•˜์ธ ๋‹จ์–ด๋“ค์€ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ๋ณ„๋กœ ์ค‘์š”ํ•˜์ง€ ์•Š์„ ๋“ฏํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๋‹จ์–ด๋“ค์€ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์—์„œ ๋ฐฐ์ œ์‹œํ‚ค๊ฒ ์Šต๋‹ˆ๋‹ค. ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 2์ดํ•˜์ธ ๋‹จ์–ด๋“ค์˜ ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋กœ ์ œํ•œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ „์ฒด ๋‹จ์–ด ๊ฐœ์ˆ˜ ์ค‘ ๋นˆ๋„์ˆ˜ 2์ดํ•˜์ธ ๋‹จ์–ด๋Š” ์ œ๊ฑฐ. vocab_size = total_cnt - rare_cnt vocab = vocab[:vocab_size] print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', len(vocab)) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 38709 ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ 2๋ฒˆ ์ดํ•˜์ธ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์ž ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๊ฐ€ 100,586๊ฐœ์—์„œ 38,709๊ฐœ๋กœ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค. ์•„์ง ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ์ž‘์—…์„ ์ง„ํ–‰ํ•˜์ง€๋Š” ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ธฐ์— ์•ž์„œ ์ •์ˆ˜ 0๊ณผ ์ •์ˆ˜ 1์—๋Š” ํŠน๋ณ„ํ•œ ์šฉ๋„์˜ ๋‹จ์–ด๋ฅผ ๋ถ€์—ฌํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜ 0์€ ํŒจ๋”ฉ์„ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ํŒจ๋”ฉ ํ† ํฐ์ธ <PAD>๋ฅผ ํ• ๋‹นํ•˜๊ณ , ์ •์ˆ˜ 1์€ OOV(Out-Of-Vocabulary) ๋ฌธ์ œ ๋ฐœ์ƒ ์‹œ์— ๋ชจ๋ฅด๋Š” ๋‹จ์–ด์— ์ •์ˆ˜ 1์„ ํ• ๋‹นํ•˜๋Š” ์šฉ๋„์ธ <UNK>๋ฅผ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. word_to_index = {} word_to_index['<PAD>'] = 0 word_to_index['<UNK>'] = 1 for index, word in enumerate(vocab) : word_to_index[word] = index + 2 vocab_size = len(word_to_index) print('ํŒจ๋”ฉ ํ† ํฐ๊ณผ UNK ํ† ํฐ์„ ๊ณ ๋ คํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', vocab_size) ํŒจ๋”ฉ ํ† ํฐ๊ณผ UNK ํ† ํฐ์„ ๊ณ ๋ คํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 38711 3. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ตœ์ข… ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์ธ word_to_index๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜์ธ texts_to_sequences()๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ ๋‹จ์–ด๋ฅผ word_to_index์— ๋งคํ•‘๋œ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ word_to_index์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ ๊ฒฝ์šฐ์—๋Š” ์ •์ˆ˜ 1์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. def texts_to_sequences(tokenized_X_data, word_to_index): encoded_X_data = [] for sent in tokenized_X_data: index_sequences = [] for word in sent: try: index_sequences.append(word_to_index[word]) except KeyError: index_sequences.append(word_to_index['<UNK>']) encoded_X_data.append(index_sequences) return encoded_X_data encoded_X_train = texts_to_sequences(tokenized_X_train, word_to_index) encoded_X_valid = texts_to_sequences(tokenized_X_valid, word_to_index) encoded_X_test = texts_to_sequences(tokenized_X_test, word_to_index) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ง„ํ–‰๋œ ํ•™์Šต ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ 2๊ฐœ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # ์ƒ์œ„ ์ƒ˜ํ”Œ 2๊ฐœ ์ถœ๋ ฅ for sent in encoded_X_train[:2]: print(sent) [38, 29, 140, 3, 52, 54, 29, 38, 3, 5, 3406, 47, 19, 95, 22, 29, 161, 4059, 6, 1741, 3, 47, 29, 293, 39, 469, 218, 3, 6, 47, 29, 134, 71, 532, 61, 59, 25184, 130, 214, 44, 249, 2, 189, 114, 58, 29, 41, 12, 13, 10, 11, 12, 13, 10, 11, 384, 3, 384, 253, 26, 4, 57, 29, 38, 5, 2280, 1587, 23, 1477, 3, 17, 9, 5775, 8, 111, 29, 1440, 71, 532, 141, 677, 4, 16, 343, 8, 126, 17, 24, 43, 2, 75, 63, 16, 20 ... ์ค‘๋žต ...] [16, 735, 2344, 41, 41, 16, 735, 2344, 467, 41, 16, 735, 1903, 41, 25, 17, 26, 20, 2, 1588, 165, 16, 38, 128, 15, 198, 62, 75, 4, 12, 13, 10, 11, 12, 13, 10, 11, 16, 20, 360, 2, 100, 1359, 8, 77, 17, 26, 42, 2, 2394, 3, 6, 43, 16, 38, 8, 147, 9, 16, 1445, 2395, 16, 3268, 3, 6, 63, 9, 14, 184, 8, 39, 1320, 15, 2, 2382, 6, 9728, 4, 520, 3, 17, 9, 40, 2344, 26, 29, 97, 354, 8, 77, 3, 109, 604, 41, 12, 13, 10, 11 ... ์ค‘๋žต ...] ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์—ญ์œผ๋กœ ๋ณต์›ํ•ด ๋ณด๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ๋‹จ์–ด์— ์ •์ˆ˜๊ฐ€ ๋งคํ•‘๋œ word_to_index๋ฅผ ๋ฐ˜๋Œ€๋กœ ๋งŒ๋“  index_to_word๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ณ  ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋ณต์›ํ•ด ๋ด…์‹œ๋‹ค. index_to_word = {} for key, value in word_to_index.items(): index_to_word[value] = key decoded_sample = [index_to_word[word] for word in encoded_X_train[0]] print('๊ธฐ์กด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :', tokenized_X_train[0]) print('๋ณต์›๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :', decoded_sample) ๊ธฐ์กด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : ['have', 'you', 'ever', ',', 'or', 'do', ... ์ค‘๋žต ... 'heart-swelling', 'feeling', '.', 'i', 'give', 'this', '9/10', '.', 'to', 'be', 'compared', 'to', '(', 'and', 'even', 'rated', 'better', 'than', ')', 'cats', 'and', 'dogs', 'and', 'babe', '.'] ๋ณต์›๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : ['have', 'you', 'ever', ',', 'or', 'do', ... ์ค‘๋žต ... '<UNK>', 'feeling', '.', 'i', 'give', 'this', '9/10', '.', 'to', 'be', 'compared', 'to', '(', 'and', 'even', 'rated', 'better', 'than', ')', 'cats', 'and', 'dogs', 'and', 'babe', '.'] ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๊ธธ์–ด์„œ ์ค‘๋žตํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„ ๋‹ค์‹œ์—ญ์œผ๋กœ ๋ณต์›ํ•œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์€ ์ค‘๊ฐ„์— <UNK>์ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4. ํŒจ๋”ฉ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธธ์ด์˜ ๋ฐ์ดํ„ฐ๋“ค์„ ๋™์ผํ•œ ๊ธธ์ด๋กœ ์ผ์น˜์‹œ์ผœ์ฃผ๋Š” ํŒจ๋”ฉ ์ž‘์—…์„ ์ง„ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด, ํ‰๊ท  ๊ธธ์ด, ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด :',max(len(review) for review in encoded_X_train)) print('๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด :',sum(map(len, encoded_X_train))/len(encoded_X_train)) plt.hist([len(review) for review in encoded_X_train], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ๋ฆฌ๋ทฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 2818 ๋ฆฌ๋ทฐ์˜ ํ‰๊ท  ๊ธธ์ด : 279.1958 ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” 2,818์ด๋ฉฐ, ๊ทธ๋ž˜ํ”„๋ฅผ ๋ดค์„ ๋•Œ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋Š” ๋Œ€์ฒด์ ์œผ๋กœ ์•ฝ 1,000๋‚ด์™ธ์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก encoded_X_train๊ณผ encoded_X_test์˜ ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ํŠน์ • ๊ธธ์ด๋กœ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ๊ธธ์ด ๋ณ€์ˆ˜๋ฅผ max_len์œผ๋กœ ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ฆฌ๋ทฐ๊ฐ€ ๋‚ด์šฉ์ด ์ž˜๋ฆฌ์ง€ ์•Š๋„๋ก ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์ ์˜ max_len์˜ ๊ฐ’์€ ๋ช‡์ผ๊นŒ์š”? ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ max_len ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ์ด ๋ช‡ % ์ธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. def below_threshold_len(max_len, nested_list): count = 0 for sentence in nested_list: if(len(sentence) <= max_len): count = count + 1 print('์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ %s ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: %s'%(max_len, (count / len(nested_list))*100)) ์ตœ๋Œ€ ๊ธธ์ด 2,818๋กœ ๋ชจ๋“  ์ƒ˜ํ”Œ์„ ํŒจ๋”ฉ ํ•˜๋Š” ๊ฒƒ์€ ์กฐ๊ธˆ ๊ณผํ•œ ์ฒ˜์‚ฌ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 500์œผ๋กœ ํ•  ๊ฒฝ์šฐ ๋ช‡ ๊ฐœ์˜ ์ƒ˜ํ”Œ์„ ์†์ƒ์‹œํ‚ค์ง€ ์•Š๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. max_len = 500 below_threshold_len(max_len, encoded_X_train) ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ 500 ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: 87.795 500์œผ๋กœ ํŒจ๋”ฉ ํ•  ๊ฒฝ์šฐ ์•ฝ 88%์˜ ์ƒ˜ํ”Œ์€ ๊ทธ๋Œ€๋กœ ๋ณด์กด๋ฉ๋‹ˆ๋‹ค. ๋” ๋งŽ์€ ์ƒ˜ํ”Œ์„ ๋ณด์กดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 500๋ณด๋‹ค ๋” ํฐ ๊ธธ์ด๋กœ ํŒจ๋”ฉ ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” 500์œผ๋กœ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํŒจ๋”ฉ์„ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ pad_sequences()๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ •ํ•˜๋ฉด ํ•ด๋‹น ๊ธธ์ด๋ณด๋‹ค ๊ธด ๋ฐ์ดํ„ฐ๋Š” ๋’ท๋ถ€๋ถ„์„ ์ž˜๋ผ์„œ ํ•ด๋‹น ๊ธธ์ด๋กœ ๋งž์ถ”๊ณ , ํ•ด๋‹น ๊ธธ์ด๋ณด๋‹ค ์งง์€ ๋ฐ์ดํ„ฐ๋Š” ๋’ค์— 0์„ ์ฑ„์›Œ์„œ ํ•ด๋‹น ๊ธธ์ด์˜ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ธธ์ด 500์œผ๋กœ ํŒจ๋”ฉ์„ ํ•˜๋ฉด ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋Š” 500์ด ๋ฉ๋‹ˆ๋‹ค. def pad_sequences(sentences, max_len): features = np.zeros((len(sentences), max_len), dtype=int) for index, sentence in enumerate(sentences): if len(sentence) != 0: features[index, :len(sentence)] = np.array(sentence)[:max_len] return features padded_X_train = pad_sequences(encoded_X_train, max_len=max_len) padded_X_valid = pad_sequences(encoded_X_valid, max_len=max_len) padded_X_test = pad_sequences(encoded_X_test, max_len=max_len) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_train.shape) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_valid.shape) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_test.shape) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (20000, 500) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (5000, 500) ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (25000, 500) 5. ๋ชจ๋ธ๋ง ์ด์ œ ๋”ฅ ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ PyTorch๋ฅผ ์ด์šฉํ•˜์—ฌ 1D CNN ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F ํ˜„์žฌ ์‹ค์Šต ํ™˜๊ฒฝ์—์„œ GPU๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. USE_CUDA = torch.cuda.is_available() device = torch.device("cuda" if USE_CUDA else "cpu") print("cpu์™€ cuda ์ค‘ ๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•จ:", device) cpu์™€ cuda ์ค‘ ๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•จ: cuda ์ €์ž์˜ ๊ฒฝ์šฐ Colab์—์„œ GPU๋ฅผ ์„ ํƒํ•˜์—ฌ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜์—ฌ cuda๋ผ๋Š” ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด ํ† ์น˜์˜ ํ…์„œ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ 5๊ฐœ์˜ ๋ ˆ์ด๋ธ”์„ ์ถœ๋ ฅํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. train_label_tensor = torch.tensor(np.array(y_train)) valid_label_tensor = torch.tensor(np.array(y_valid)) test_label_tensor = torch.tensor(np.array(y_test)) print(train_label_tensor[:5]) tensor([1, 1, 0, 0, 0]) ์•„๋ž˜๋Š” ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์˜ ๋™์ž‘ ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. # input.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›, ๋ฌธ์žฅ ๊ธธ์ด) input = torch.randn(32, 16, 50) # ์„ ์–ธ ์‹œ nn.Conv1d(์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›, ์ปค๋„์˜ ๊ฐœ์ˆ˜, ์ปค๋„ ์‚ฌ์ด์ฆˆ) m = nn.Conv1d(16, 33, 3, stride=1) # output.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ์ปค๋„์˜ ๊ฐœ์ˆ˜, ์ปจ๋ณผ๋ฃจ์…˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ ๋ฒกํ„ฐ) output = m(input) print(output.shape) torch.Size([32, 33, 48]) CNN ๋ชจ๋ธ์„ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. class CNN(torch.nn.Module): def __init__(self, vocab_size, num_labels): super(CNN, self).__init__() # ์˜ค์ง ํ•˜๋‚˜์˜ ์ข…๋ฅ˜์˜ ํ•„ํ„ฐ๋งŒ ์‚ฌ์šฉํ•จ. self.num_filter_sizes = 1 # ์œˆ๋„ 5์งœ๋ฆฌ 1๊ฐœ๋งŒ ์‚ฌ์šฉ self.num_filters = 256 self.word_embed = torch.nn.Embedding(num_embeddings=vocab_size, embedding_dim=128, padding_idx=0) # ์œˆ๋„ 5์งœ๋ฆฌ 1๊ฐœ๋งŒ ์‚ฌ์šฉ self.conv1 = torch.nn.Conv1d(128, self.num_filters, 5, stride=1) self.dropout = torch.nn.Dropout(0.5) self.fc1 = torch.nn.Linear(1 * self.num_filters, num_labels, bias=True) def forward(self, inputs): # word_embed(inputs).shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ฌธ์žฅ ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›) # word_embed(inputs).permute(0, 2, 1).shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›, ๋ฌธ์žฅ ๊ธธ์ด) embedded = self.word_embed(inputs).permute(0, 2, 1) # max๋ฅผ ์ด์šฉํ•œ maxpooling # conv1(embedded).shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ์ปค๋„ ๊ฐœ์ˆ˜, ์ปจ๋ณผ๋ฃจ์…˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ) == ex) 32, 256, 496 # conv1(embedded).permute(0, 2, 1).shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ์ปจ๋ณผ๋ฃจ์…˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ, ์ปค๋„ ๊ฐœ์ˆ˜) # conv1(embedded).permute(0, 2, 1).max(1)[0]).shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ์ปค๋„ ๊ฐœ์ˆ˜) x = F.relu(self.conv1(embedded).permute(0, 2, 1).max(1)[0]) # y_pred.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ถ„๋ฅ˜ํ•  ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์ˆ˜) y_pred = self.fc1(self.dropout(x)) return y_pred ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํŒŒ์ด ํ† ์น˜ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ฐฐ์น˜ ๋‹จ์œ„ ์—ฐ์‚ฐ์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. encoded_train = torch.tensor(padded_X_train).to(torch.int64) train_dataset = torch.utils.data.TensorDataset(encoded_train, train_label_tensor) train_dataloader = torch.utils.data.DataLoader(train_dataset, shuffle=True, batch_size=32) encoded_test = torch.tensor(padded_X_test).to(torch.int64) test_dataset = torch.utils.data.TensorDataset(encoded_test, test_label_tensor) test_dataloader = torch.utils.data.DataLoader(test_dataset, shuffle=True, batch_size=1) encoded_valid = torch.tensor(padded_X_valid).to(torch.int64) valid_dataset = torch.utils.data.TensorDataset(encoded_valid, valid_label_tensor) valid_dataloader = torch.utils.data.DataLoader(valid_dataset, shuffle=True, batch_size=1) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜๊ฐ€ 20,000๊ฐœ์˜€์œผ๋ฏ€๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 32๋กœ ํ•  ๊ฒฝ์šฐ์—๋Š” 20000/32=625 ๋‹ค์‹œ ๋งํ•ด 32๊ฐœ์”ฉ ๋ฌถ์ธ ๋ฐ์ดํ„ฐ ๋ฌถ์Œ์ด 625๊ฐœ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต ์‹œ์—๋Š” 32๊ฐœ์”ฉ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. total_batch = len(train_dataloader) print('์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : {}'.format(total_batch)) ์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : 625 ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. model = CNN(vocab_size, num_labels = len(set(y_train))) model.to(device) ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 128, ์ถœ๋ ฅ์ธต์˜ ํฌ๊ธฐ(๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ฐœ์ˆ˜)๋Š” 2๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•ด์ฃผ๋Š” ๊ฐ’์ด๋ฉด์„œ ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๊ฐ’๋“ค์„ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ํ†ตํ•ด ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ง„ํ–‰ํ•˜๋ฏ€๋กœ ์†์‹ค ํ•จ์ˆ˜๋Š” nn.CrossEntropyLoss()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜๋กœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ์†์‹ค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘ ํ•˜๋‚˜์ธ ํ•™์Šต๋ฅ (learning rate)๋Š” 0.001๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) 6. ํ‰๊ฐ€ ์ฝ”๋“œ ์ž‘์„ฑ ์ดํ›„ ํ‰๊ฐ€๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ชจ๋ธ์˜ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•˜๋Š” ํ•จ์ˆ˜ calculate_accuracy()๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. def calculate_accuracy(logits, labels): # _, predicted = torch.max(logits, 1) predicted = torch.argmax(logits, dim=1) correct = (predicted == labels).sum().item() total = labels.size(0) accuracy = correct / total return accuracy ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ evaluate()๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ•จ์ˆ˜์—์„œ model.eval()๊ณผ with torch.no_grad()๋ฅผ ์งš์–ด๋ด…์‹œ๋‹ค. ์ด ๋‘ ๊ฐœ๋Š” ๋ชจ๋ธ ํ‰๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์˜๋ฏธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. model.eval(): ๋ชจ๋ธ์„ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋ธ ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๋ ˆ์ด์–ด์— ๋Œ€ํ•ด ํ‰๊ฐ€ ๋ชจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ ˆ์ด์–ด, ์˜ˆ๋ฅผ ๋“ค์–ด ๋“œ๋กญ์•„์›ƒ์ด๋‚˜ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ํ•™์Šต๊ณผ ํ‰๊ฐ€ ์‹œ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ์„ค์ •์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ชจ๋“œ๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์œผ๋ฉด, ์ด๋Ÿฌํ•œ ๋ ˆ์ด์–ด์˜ ๋™์ž‘์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋กœ ์ธํ•ด ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ์ œ๋Œ€๋กœ ๋‚˜์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with torch.no_grad(): ์ด ๋ฌธ์žฅ์€ ์ž๋™ ๋ฏธ๋ถ„ ์—”์ง„์—์„œ ๊ทธ๋ž˜๋”” ์–ธํŠธ ๊ณ„์‚ฐ์„ ๋น„ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ์ค‘์—๋Š” ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๊ณ  ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ์„ค์ •์ด ์ ์šฉ๋˜์ง€ ์•Š์œผ๋ฉด, ํ‰๊ฐ€ ๊ณผ์ •์—์„œ ๊ทธ๋ž˜๋”” ์–ธํŠธ๊ฐ€ ๊ณ„์‚ฐ๋˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ‰๊ฐ€ ๊ฒฐ๊ณผ ์ž์ฒด์—๋Š” ์ง์ ‘์ ์ธ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ model.eval()์€ ํ‰๊ฐ€ ์‹œ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋‚˜์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with torch.no_grad():๋Š” ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ์™€ ์†๋„ ์ธก๋ฉด์—์„œ ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. def evaluate(model, valid_dataloader, criterion, device): val_loss = 0 val_correct = 0 val_total = 0 model.eval() with torch.no_grad(): # ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ๋ถ€ํ„ฐ ๋ฐฐ์น˜ ํฌ๊ธฐ๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ์†์œผ๋กœ ๋กœ๋“œ for batch_X, batch_y in valid_dataloader: batch_X, batch_y = batch_X.to(device), batch_y.to(device) # ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’ logits = model(batch_X) # ์†์‹ค์„ ๊ณ„์‚ฐ loss = criterion(logits, batch_y) # ์ •ํ™•๋„์™€ ์†์‹ค์„ ๊ณ„์‚ฐํ•จ val_loss += loss.item() val_correct += calculate_accuracy(logits, batch_y) * batch_y.size(0) val_total += batch_y.size(0) val_accuracy = val_correct / val_total val_loss /= len(valid_dataloader) return val_loss, val_accuracy 7. ํ•™์Šต num_epochs = 5 # Training loop best_val_loss = float('inf') # Training loop for epoch in range(num_epochs): # Training train_loss = 0 train_correct = 0 train_total = 0 model.train() for batch_X, batch_y in train_dataloader: # Forward pass batch_X, batch_y = batch_X.to(device), batch_y.to(device) # batch_X.shape == (batch_size, max_len) logits = model(batch_X) # Compute loss loss = criterion(logits, batch_y) # Backward pass and optimization optimizer.zero_grad() loss.backward() optimizer.step() # Calculate training accuracy and loss train_loss += loss.item() train_correct += calculate_accuracy(logits, batch_y) * batch_y.size(0) train_total += batch_y.size(0) train_accuracy = train_correct / train_total train_loss /= len(train_dataloader) # Validation val_loss, val_accuracy = evaluate(model, valid_dataloader, criterion, device) print(f'Epoch {epoch+1}/{num_epochs}:') print(f'Train Loss: {train_loss:.4f}, Train Accuracy: {train_accuracy:.4f}') print(f'Validation Loss: {val_loss:.4f}, Validation Accuracy: {val_accuracy:.4f}') # ๊ฒ€์ฆ ์†์‹ค์ด ์ตœ์†Œ์ผ ๋•Œ ์ฒดํฌํฌ์ธํŠธ ์ €์žฅ if val_loss < best_val_loss: print(f'Validation loss improved from {best_val_loss:.4f} to {val_loss:.4f}. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.') best_val_loss = val_loss torch.save(model.state_dict(), 'best_model_checkpoint.pth') 8. ๋ชจ๋ธ ๋กœ๋“œ ๋ฐ ํ‰๊ฐ€ ํ•™์Šต ๊ณผ์ •์—์„œ ๊ฒ€์ฆ ์†์‹ค์ด ์ตœ์†Œ ์ผ ๋•Œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ฒ ์ŠคํŠธ ๋ชจ๋ธ๋กœ ํŒ๋‹จํ•˜๊ณ  ํ•ด๋‹น ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋กœ๋“œํ•˜์—ฌ ๋ชจ๋ธ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋ธ ๋กœ๋“œ model.load_state_dict(torch.load('best_model_checkpoint.pth')) # ๋ชจ๋ธ์„ device์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. model.to(device) # ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ val_loss, val_accuracy = evaluate(model, valid_dataloader, criterion, device) print(f'Best model validation loss: {val_loss:.4f}') print(f'Best model validation accuracy: {val_accuracy:.4f}') Best model validation loss: 0.2951 Best model validation accuracy: 0.8816 ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์—์„œ์˜ ํ‰๊ฐ€ ์‹œ ์ •ํ™•๋„๋Š” 88.16%, ์†์‹ค์€ 0.2951์ž…๋‹ˆ๋‹ค. # ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ test_loss, test_accuracy = evaluate(model, test_dataloader, criterion, device) print(f'Best model test loss: {test_loss:.4f}') print(f'Best model test accuracy: {test_accuracy:.4f}') Best model test loss: 0.3060 Best model test accuracy: 0.8728 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์—์„œ์˜ ํ‰๊ฐ€ ์‹œ ์ •ํ™•๋„๋Š” 87.28%, ์†์‹ค์€ 0.3060์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ 13-03 ์‹ค์Šต์—์„œ ์ด ์ •ํ™•๋„์™€ ์†์‹ค์„ ์ข€ ๋” ๋†’์—ฌ๋ณด๋Š” ์‹ค์Šต์„ ์ง„ํ–‰ํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. 9. ๋ชจ๋ธ ํ…Œ์ŠคํŠธ ์ด์ œ ์ž„์˜์˜ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์˜ˆ์ธกํ•ด ๋ณด๋Š” ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜ predict() ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ํ•ด๋‹น ํ•จ์ˆ˜์— ์ „์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด ์žˆ์ง€ ์•Š์€ ์˜ํ™” ๋ฆฌ๋ทฐ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด์„œ ๋ชจ๋ธ์˜ ์˜ˆ์ธก์„ ์–ป์–ด๋ด…์‹œ๋‹ค. index_to_tag = {0 : '๋ถ€์ •', 1 : '๊ธ์ •'} def predict(text, model, word_to_index, index_to_tag): # ๋ชจ๋ธ ํ‰๊ฐ€ ๋ชจ๋“œ model.eval() # ํ† ํฐํ™” ๋ฐ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ. OOV ๋ฌธ์ œ ๋ฐœ์ƒ ์‹œ <UNK> ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค 1 ํ• ๋‹น tokens = word_tokenize(text) token_indices = [word_to_index.get(token.lower(), 1) for token in tokens] # ๋ฆฌ์ŠคํŠธ๋ฅผ ํ…์„œ๋กœ ๋ณ€๊ฒฝ input_tensor = torch.tensor([token_indices], dtype=torch.long).to(device) # (1, seq_length) # ๋ชจ๋ธ์˜ ์˜ˆ์ธก with torch.no_grad(): logits = model(input_tensor) # (1, output_dim) # ๋ ˆ์ด๋ธ” ์ธ๋ฑ์Šค ์˜ˆ์ธก _, predicted_index = torch.max(logits, dim=1) # (1, ) # ์ธ๋ฑ์Šค์™€ ๋งค์นญ๋˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ๋ฌธ์ž์—ด๋กœ ๋ณ€๊ฒฝ predicted_tag = index_to_tag[predicted_index.item()] return predicted_tag ๋ถ€์ •์ ์ธ ์˜ํ™” ๋ฆฌ๋ทฐ๋ฅผ ๋„ฃ์–ด ๋ชจ๋ธ์ด ๋ถ€์ •์ด๋ผ๊ณ  ์ž˜ ์˜ˆ์ธกํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค. test_input = "This movie was just way too overrated. The fighting was not professional and in slow motion. I was expecting more from a 200 million budget movie. The little sister of T.Challa was just trying too hard to be funny. The story was really dumb as well. Don't watch this movie if you are going because others say its great unless you are a Black Panther fan or Marvels fan." predict(test_input, model, word_to_index, index_to_tag) ๋ถ€์ • ๊ธ์ •์ ์ธ ์˜ํ™” ๋ฆฌ๋ทฐ๋ฅผ ๋„ฃ์–ด ๋ชจ๋ธ์ด ๋ถ€์ •์ด๋ผ๊ณ  ์ž˜ ์˜ˆ์ธกํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค. test_input = " I was lucky enough to be included in the group to see the advanced screening in Melbourne on the 15th of April, 2012. And, firstly, I need to say a big thank-you to Disney and Marvel Studios. Now, the film... how can I even begin to explain how I feel about this film? It is, as the title of this review says a 'comic book triumph'. I went into the film with very, very high expectations and I was not disappointed. Seeing Joss Whedon's direction and envisioning of the film come to life on the big screen is perfect. The script is amazingly detailed and laced with sharp wit a humor. The special effects are literally mind-blowing and the action scenes are both hard-hitting and beautifully choreographed." predict(test_input, model, word_to_index, index_to_tag) ๊ธ์ • 13-06 ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์ด์šฉํ•œ ์„ฑ๋Šฅ ์ƒ์Šน์‹œํ‚ค๊ธฐ ์ด์ „ ์‹ค์Šต์ธ '12-04 1D CNN์„ ์ด์šฉํ•˜์—ฌ IMDB ์˜ํ™” ๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜' ์‹ค์Šต์— ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์ด์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ข€ ๋” ๋†’์€ ์„ฑ๋Šฅ์„ ์–ป์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ๋‹จ์–ด ํ† ํฐํ™” ~ 4. ํŒจ๋”ฉ 12-04์™€ ๋ชจ๋“  ๊ณผ์ •์ด ๋™์ผํ•œ ๊ณผ์ •์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 5. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จธ์‹  ๋Ÿฌ๋‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ gensim์˜ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. !pip install gensim ๊ตฌ๊ธ€์ด ์ด๋ฏธ ํ•™์Šตํ•ด๋†“์€ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. !wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc? export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1Av37IVBQAAntSe1X3MOAl5gvowQzd2_j' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1Av37IVBQAAntSe1X3MOAl5gvowQzd2_j" -O GoogleNews-vectors-negative300.bin.gz && rm -rf /tmp/cookies.txt ๊ตฌ๊ธ€์˜ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2vec ๋ชจ๋ธ์„ gensim์„ ํ†ตํ•ด ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. word2vec_model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin.gz', binary=True) ๊ตฌ๊ธ€์˜ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์€ ํ•™์Šต๋˜์—ˆ์„ ๋‹น์‹œ ๊ฐ ๋‹จ์–ด๊ฐ€ 300์ฐจ์›์œผ๋กœ ํ•™์Šต๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  vocab_size ๋งŒํผ์˜ ํ–‰์„ ๊ฐ€์ง€๊ณ , 300์ฐจ์›์˜ ์—ด์„ ๊ฐ€์ง€๋Š” ํ–‰๋ ฌ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. embedding_matrix = np.zeros((vocab_size, 300)) ๊ทธ ํ›„ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  embedding_matrix์— ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์šฐ๋ฆฌ๊ฐ€ ์•ž์„œ ๋งŒ๋“  ํ† ํฌ ๋‚˜์ด์ € ๊ธฐ์ค€์œผ๋กœ 36๋ฒˆ์ด ๋‹จ์–ด '์‚ฌ๊ณผ'๋ผ๋ฉด, embedding_matrix์˜ 36๋ฒˆ ํ–‰์— ๊ตฌ๊ธ€์—์„œ ๋งŒ๋“  ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ๊ฐ’์ด '์‚ฌ๊ณผ'์ธ ๋ฒกํ„ฐ๋ฅผ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค. def get_vector(word): if word in word2vec_model: return word2vec_model[word] else: return None # <PAD>๋ฅผ ์œ„ํ•œ 0๋ฒˆ๊ณผ <UNK>๋ฅผ ์œ„ํ•œ 1๋ฒˆ์€ ์‹ค์ œ ๋‹จ์–ด๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ๋งคํ•‘์—์„œ ์ œ์™ธ for word, i in word_to_index.items(): if i > 2: temp = get_vector(word) if temp is not None: embedding_matrix[i] = temp 0๋ฒˆ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # <PAD>๋‚˜ <UNK>์˜ ๊ฒฝ์šฐ๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์•„์„œ 0๋ฒกํ„ฐ์ž„ embedding_matrix[0] array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) ์›์†Œ์˜ ๊ฐ’์ด ์ „๋ถ€ 0์ธ 0๋ฒกํ„ฐ์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ 'apple'์€ ๋ช‡ ๋ฒˆ ์ •์ˆ˜๋กœ ๋งคํ•‘๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. word_to_index['apple'] 8053 ๊ตฌ๊ธ€์˜ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ Word2Vec์—์„œ์˜ 'apple'์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๊ฐ’(word2vec_model['apple'])๊ณผ ํ˜„์žฌ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์˜ 8053๋ฒˆ์˜ ๋ฒกํ„ฐ๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ๊ฐ’์ด ์šฐ๋ฆฌ์˜ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์— ์ •ํ™•ํ•˜๊ฒŒ ๋งคํ•‘๋˜์—ˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. # word2vec_model์—์„œ 'apple'์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ # embedding_matrix[8053]์ด ์ผ์น˜ํ•˜๋Š”์ง€ ์ฒดํฌ np.all(word2vec_model['apple'] == embedding_matrix[8053]) True 6. ๋ชจ๋ธ๋ง ์œ„์˜ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์ด์šฉํ•˜์—ฌ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F GPU๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ™˜๊ฒฝ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. USE_CUDA = torch.cuda.is_available() device = torch.device("cuda" if USE_CUDA else "cpu") print("cpu์™€ cuda ์ค‘ ๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•จ:", device) cpu์™€ cuda ์ค‘ ๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•จ: cuda ์ €์ž์˜ ๊ฒฝ์šฐ Colab์—์„œ GPU๋ฅผ ์„ ํƒ ํ›„ ์‹ค์Šตํ•˜๊ณ  ์žˆ์–ด 'cuda'๋ผ๊ณ  ์ถœ๋ ฅ๋˜์—ˆ์œผ๋ฉฐ GPU๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” CNN ๋ชจ๋ธ์˜ ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ ์ธต์— ์œ„์—์„œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์ž„๋ฒ ๋”ฉ์œผ๋กœ ๋งŒ๋“ค์–ด๋†“์€ ์ž„๋ฒ ๋”ฉ ํ–‰๋ ฌ์ธ embedding_matrix๋ฅผ ๋งคํ•‘ํ•˜๊ณ  ์žˆ๋Š” ์ฝ”๋“œ์— ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. class CNN(torch.nn.Module): def __init__(self, vocab_size, num_labels): super(CNN, self).__init__() # ์˜ค์ง ํ•˜๋‚˜์˜ ์ข…๋ฅ˜์˜ ํ•„ํ„ฐ๋งŒ ์‚ฌ์šฉํ•จ. self.num_filter_sizes = 1 # ์œˆ๋„ 5์งœ๋ฆฌ 1๊ฐœ๋งŒ ์‚ฌ์šฉ self.num_filters = 256 # ์ฃผ์„ ์ฒ˜๋ฆฌ๋œ ์ฝ”๋“œ๋Š” ๊ธฐ์กด์˜ ์ž„๋ฒ ๋”ฉ ์ธต์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ # self.word_embed = nn.Embedding(num_embeddings=vocab_size, embedding_dim=128, padding_idx=0) self.word_embed = nn.Embedding(num_embeddings=vocab_size, embedding_dim=300) self.word_embed.weight = nn.Parameter(torch.tensor(embedding_matrix, dtype=torch.float32)) self.word_embed.weight.requires_grad = True # ์œˆ๋„ 5์งœ๋ฆฌ 1๊ฐœ๋งŒ ์‚ฌ์šฉ self.conv1 = torch.nn.Conv1d(300, self.num_filters, 5, stride=1) self.dropout = torch.nn.Dropout(0.5) self.fc1 = torch.nn.Linear(1 * self.num_filters, num_labels, bias=True) def forward(self, inputs): # word_embed(inputs).shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ฌธ์žฅ ๊ธธ์ด, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›) # word_embed(inputs).permute(0, 2, 1).shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›, ๋ฌธ์žฅ ๊ธธ์ด) embedded = self.word_embed(inputs).permute(0, 2, 1) # max๋ฅผ ์ด์šฉํ•œ maxpooling # conv1(embedded).shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ์ปค๋„ ๊ฐœ์ˆ˜, ์ปจ๋ณผ๋ฃจ์…˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ) == ex) 32, 256, 496 # conv1(embedded).permute(0, 2, 1).shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ์ปจ๋ณผ๋ฃจ์…˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ, ์ปค๋„ ๊ฐœ์ˆ˜) # conv1(embedded).permute(0, 2, 1).max(1)[0]).shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ์ปค๋„ ๊ฐœ์ˆ˜) x = F.relu(self.conv1(embedded).permute(0, 2, 1).max(1)[0]) # y_pred.shape == (๋ฐฐ์น˜ ํฌ๊ธฐ, ๋ถ„๋ฅ˜ํ•  ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์ˆ˜) y_pred = self.fc1(self.dropout(x)) return y_pred ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ํŒŒ์ด ํ† ์น˜ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ฐฐ์น˜ ๋‹จ์œ„ ์—ฐ์‚ฐ์„ ์œ„ํ•ด ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ์•„๋ž˜์˜ ๋ชจ๋“  ์ฝ”๋“œ๋Š” 13-02์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. encoded_train = torch.tensor(padded_X_train).to(torch.int64) train_dataset = torch.utils.data.TensorDataset(encoded_train, train_label_tensor) train_dataloader = torch.utils.data.DataLoader(train_dataset, shuffle=True, batch_size=32) encoded_test = torch.tensor(padded_X_test).to(torch.int64) test_dataset = torch.utils.data.TensorDataset(encoded_test, test_label_tensor) test_dataloader = torch.utils.data.DataLoader(test_dataset, shuffle=True, batch_size=1) encoded_valid = torch.tensor(padded_X_valid).to(torch.int64) valid_dataset = torch.utils.data.TensorDataset(encoded_valid, valid_label_tensor) valid_dataloader = torch.utils.data.DataLoader(valid_dataset, shuffle=True, batch_size=1) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜๊ฐ€ 20,000๊ฐœ์˜€์œผ๋ฏ€๋กœ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ 32๋กœ ํ•  ๊ฒฝ์šฐ์—๋Š” 20000/32=625 ๋‹ค์‹œ ๋งํ•ด 32๊ฐœ์”ฉ ๋ฌถ์ธ ๋ฐ์ดํ„ฐ ๋ฌถ์Œ์ด 625๊ฐœ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์Šต ์‹œ์—๋Š” 32๊ฐœ์”ฉ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. total_batch = len(train_dataloader) print('์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : {}'.format(total_batch)) ์ด ๋ฐฐ์น˜์˜ ์ˆ˜ : 625 ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. model = CNN(vocab_size, num_labels = len(set(y_train))) model.to(device) ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 300, ์ถœ๋ ฅ์ธต์˜ ํฌ๊ธฐ(๋ถ„๋ฅ˜ํ•ด์•ผ ํ•  ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ฐœ์ˆ˜)๋Š” 2๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•ด์ฃผ๋Š” ๊ฐ’์ด๋ฉด์„œ ๋ชจ๋ธ์˜ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๊ฐ’๋“ค์„ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ํ†ตํ•ด ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ์ง„ํ–‰ํ•˜๋ฏ€๋กœ ์†์‹ค ํ•จ์ˆ˜๋Š” nn.CrossEntropyLoss()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด ํ† ์น˜๋กœ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ์†์‹ค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘ ํ•˜๋‚˜์ธ ํ•™์Šต๋ฅ (learning rate)๋Š” 0.001๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) 7. ํ‰๊ฐ€ ์ฝ”๋“œ ์ž‘์„ฑ ์ดํ›„ ํ‰๊ฐ€๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ชจ๋ธ์˜ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•˜๋Š” ํ•จ์ˆ˜ calculate_accuracy()๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. def calculate_accuracy(logits, labels): # _, predicted = torch.max(logits, 1) predicted = torch.argmax(logits, dim=1) correct = (predicted == labels).sum().item() total = labels.size(0) accuracy = correct / total return accuracy ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ evaluate()๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํ•จ์ˆ˜์—์„œ model.eval()๊ณผ with torch.no_grad()๋ฅผ ์งš์–ด๋ด…์‹œ๋‹ค. ์ด ๋‘ ๊ฐœ๋Š” ๋ชจ๋ธ ํ‰๊ฐ€๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์˜๋ฏธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. model.eval(): ๋ชจ๋ธ์„ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋ธ ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๋ ˆ์ด์–ด์— ๋Œ€ํ•ด ํ‰๊ฐ€ ๋ชจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ ˆ์ด์–ด, ์˜ˆ๋ฅผ ๋“ค์–ด ๋“œ๋กญ์•„์›ƒ์ด๋‚˜ ๋ฐฐ์น˜ ์ •๊ทœํ™”๋Š” ํ•™์Šต๊ณผ ํ‰๊ฐ€ ์‹œ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ์„ค์ •์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ๋ชจ๋“œ๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์œผ๋ฉด, ์ด๋Ÿฌํ•œ ๋ ˆ์ด์–ด์˜ ๋™์ž‘์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋กœ ์ธํ•ด ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ์ œ๋Œ€๋กœ ๋‚˜์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with torch.no_grad(): ์ด ๋ฌธ์žฅ์€ ์ž๋™ ๋ฏธ๋ถ„ ์—”์ง„์—์„œ ๊ทธ๋ž˜๋”” ์–ธํŠธ ๊ณ„์‚ฐ์„ ๋น„ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ์ค‘์—๋Š” ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•˜๊ณ  ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ์„ค์ •์ด ์ ์šฉ๋˜์ง€ ์•Š์œผ๋ฉด, ํ‰๊ฐ€ ๊ณผ์ •์—์„œ ๊ทธ๋ž˜๋”” ์–ธํŠธ๊ฐ€ ๊ณ„์‚ฐ๋˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ‰๊ฐ€ ๊ฒฐ๊ณผ ์ž์ฒด์—๋Š” ์ง์ ‘์ ์ธ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ model.eval()์€ ํ‰๊ฐ€ ์‹œ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ํ‰๊ฐ€ ๊ฒฐ๊ณผ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋‚˜์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. with torch.no_grad():๋Š” ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ์™€ ์†๋„ ์ธก๋ฉด์—์„œ ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. def evaluate(model, valid_dataloader, criterion, device): val_loss = 0 val_correct = 0 val_total = 0 model.eval() with torch.no_grad(): # ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ๋ถ€ํ„ฐ ๋ฐฐ์น˜ ํฌ๊ธฐ๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ์†์œผ๋กœ ๋กœ๋“œ for batch_X, batch_y in valid_dataloader: batch_X, batch_y = batch_X.to(device), batch_y.to(device) # ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’ logits = model(batch_X) # ์†์‹ค์„ ๊ณ„์‚ฐ loss = criterion(logits, batch_y) # ์ •ํ™•๋„์™€ ์†์‹ค์„ ๊ณ„์‚ฐํ•จ val_loss += loss.item() val_correct += calculate_accuracy(logits, batch_y) * batch_y.size(0) val_total += batch_y.size(0) val_accuracy = val_correct / val_total val_loss /= len(valid_dataloader) return val_loss, val_accuracy 8. ํ•™์Šต num_epochs = 5 # Training loop best_val_loss = float('inf') # Training loop for epoch in range(num_epochs): # Training train_loss = 0 train_correct = 0 train_total = 0 model.train() for batch_X, batch_y in train_dataloader: # Forward pass batch_X, batch_y = batch_X.to(device), batch_y.to(device) # batch_X.shape == (batch_size, max_len) logits = model(batch_X) # Compute loss loss = criterion(logits, batch_y) # Backward pass and optimization optimizer.zero_grad() loss.backward() optimizer.step() # Calculate training accuracy and loss train_loss += loss.item() train_correct += calculate_accuracy(logits, batch_y) * batch_y.size(0) train_total += batch_y.size(0) train_accuracy = train_correct / train_total train_loss /= len(train_dataloader) # Validation val_loss, val_accuracy = evaluate(model, valid_dataloader, criterion, device) print(f'Epoch {epoch+1}/{num_epochs}:') print(f'Train Loss: {train_loss:.4f}, Train Accuracy: {train_accuracy:.4f}') print(f'Validation Loss: {val_loss:.4f}, Validation Accuracy: {val_accuracy:.4f}') # ๊ฒ€์ฆ ์†์‹ค์ด ์ตœ์†Œ์ผ ๋•Œ ์ฒดํฌํฌ์ธํŠธ ์ €์žฅ if val_loss < best_val_loss: print(f'Validation loss improved from {best_val_loss:.4f} to {val_loss:.4f}. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.') best_val_loss = val_loss torch.save(model.state_dict(), 'best_model_checkpoint.pth') 9. ๋ชจ๋ธ ๋กœ๋“œ ๋ฐ ํ‰๊ฐ€ # ๋ชจ๋ธ ๋กœ๋“œ model.load_state_dict(torch.load('best_model_checkpoint.pth')) # ๋ชจ๋ธ์„ device์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. model.to(device) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ „๋ฐ˜์ ์œผ๋กœ 13-02๋ณด๋‹ค ๋” ๋†’์€ ์„ฑ๋Šฅ์„ ์–ป๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ val_loss, val_accuracy = evaluate(model, valid_dataloader, criterion, device) print(f'Best model validation loss: {val_loss:.4f}') print(f'Best model validation accuracy: {val_accuracy:.4f}') Best model validation loss: 0.2523 Best model validation accuracy: 0.8996 # ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ test_loss, test_accuracy = evaluate(model, test_dataloader, criterion, device) print(f'Best model test loss: {test_loss:.4f}') print(f'Best model test accuracy: {test_accuracy:.4f}') Best model test loss: 0.2503 Best model test accuracy: 0.8983 10. ๋ชจ๋ธ ํ…Œ์ŠคํŠธ index_to_tag = {0 : '๋ถ€์ •', 1 : '๊ธ์ •'} def predict(text, model, word_to_index, index_to_tag): # ๋ชจ๋ธ ํ‰๊ฐ€ ๋ชจ๋“œ model.eval() # ํ† ํฐํ™” ๋ฐ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ. OOV ๋ฌธ์ œ ๋ฐœ์ƒ ์‹œ <UNK> ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค 1 ํ• ๋‹น tokens = word_tokenize(text) token_indices = [word_to_index.get(token.lower(), 1) for token in tokens] # ๋ฆฌ์ŠคํŠธ๋ฅผ ํ…์„œ๋กœ ๋ณ€๊ฒฝ input_tensor = torch.tensor([token_indices], dtype=torch.long).to(device) # (1, seq_length) # ๋ชจ๋ธ์˜ ์˜ˆ์ธก with torch.no_grad(): logits = model(input_tensor) # (1, output_dim) # ๋ ˆ์ด๋ธ” ์ธ๋ฑ์Šค ์˜ˆ์ธก _, predicted_index = torch.max(logits, dim=1) # (1, ) # ์ธ๋ฑ์Šค์™€ ๋งค์นญ๋˜๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ๋ฌธ์ž์—ด๋กœ ๋ณ€๊ฒฝ predicted_tag = index_to_tag[predicted_index.item()] return predicted_tag test_input = "This movie was just way too overrated. The fighting was not professional and in slow motion. I was expecting more from a 200 million budget movie. The little sister of T.Challa was just trying too hard to be funny. The story was really dumb as well. Don't watch this movie if you are going because others say its great unless you are a Black Panther fan or Marvels fan." predict(test_input, model, word_to_index, index_to_tag) ๋ถ€์ • test_input = " I was lucky enough to be included in the group to see the advanced screening in Melbourne on the 15th of April, 2012. And, firstly, I need to say a big thank-you to Disney and Marvel Studios. Now, the film... how can I even begin to explain how I feel about this film? It is, as the title of this review says a 'comic book triumph'. I went into the film with very, very high expectations and I was not disappointed. Seeing Joss Whedon's direction and envisioning of the film come to life on the big screen is perfect. The script is amazingly detailed and laced with sharp wit a humor. The special effects are literally mind-blowing and the action scenes are both hard-hitting and beautifully choreographed." predict(test_input, model, word_to_index, index_to_tag) ๊ธ์ • 14. [NLP ๊ธฐ๋ณธ ] ์‹œํ€€์Šค ๋ ˆ์ด๋ธ”๋ง(Sequence Labeling) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋‹ค๋Œ€๋‹ค RNN์„ ์ด์šฉํ•œ ์‹œํ€€์Šค ๋ ˆ์ด๋ธ”๋ง(Sequence Labeling)์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 14-01 ์‹œํ€€์Šค ๋ ˆ์ด๋ธ”๋ง(Sequence Labeling) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ํŒŒ์ด ํ† ์น˜(PyTorch)๋กœ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•˜์—ฌ ํƒœ๊น… ์ž‘์—…์„ ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ฐœ์ฒด๋ช… ์ธ์‹ ๊ธฐ์™€ ํ’ˆ์‚ฌ ํƒœ๊ฑฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ, ์ด๋Ÿฌํ•œ ๋‘ ์ž‘์—…์˜ ๊ณตํ†ต์ ์€ RNN์˜ ๋‹ค-๋Œ€-๋‹ค(Many-to-Many) ์ž‘์—…์ด๋ฉด์„œ ๋˜ํ•œ ์•ž, ๋’ค ์‹œ์ ์˜ ์ž…๋ ฅ์„ ๋ชจ๋‘ ์ฐธ๊ณ ํ•˜๋Š” ์–‘๋ฐฉํ–ฅ RNN(Bidirectional RNN)์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์‹ค์Šต ์ฑ•ํ„ฐ๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— ์ „์ฒด์ ์œผ๋กœ ์‹ค์Šต์ด ์–ด๋–ป๊ฒŒ ์ง„ํ–‰๋˜๋Š”์ง€ ์ •๋ฆฌํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ๊ฐœ์š” ์ฑ•ํ„ฐ์™€ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ์š”์•ฝํ•˜์—ฌ ์„ค๋ช…ํ•˜๋ฏ€๋กœ, ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์ฑ•ํ„ฐ๋ฅผ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. 1. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ดํ•ด ํƒœ๊น… ์ž‘์—…์€ ์•ž์„œ ๋ฐฐ์šด ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ž‘์—…๊ณผ ๋™์ผํ•˜๊ฒŒ ์ง€๋„ ํ•™์Šต(Supervised Learning)์— ์†ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ•ํ„ฐ์—์„œ๋Š” ํƒœ๊น…์„ ํ•ด์•ผ ํ•˜๋Š” ๋‹จ์–ด ๋ฐ์ดํ„ฐ๋ฅผ X, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” ํƒœ๊น… ์ •๋ณด ๋ฐ์ดํ„ฐ๋Š” y๋ผ๊ณ  ์ด๋ฆ„์„ ๋ถ™์˜€์Šต๋‹ˆ๋‹ค. X์— ๋Œ€ํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” X_train, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” X_test๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ณ  y์— ๋Œ€ํ•œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋Š” y_train, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” y_test๋ผ๊ณ  ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ X์™€ y ๋ฐ์ดํ„ฐ์˜ ์Œ(pair)์€ ๋ณ‘๋ ฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. X์™€ y์˜ ๊ฐ ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ’ˆ์‚ฌ ํƒœ๊น… ์ž‘์—…์„ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  X_train์™€ y_train์˜ ๋ฐ์ดํ„ฐ ์ค‘ 4๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ ํ™•์ธํ•ด ๋ณธ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. X_train y_train ๊ธธ์ด 0 ['EU', 'rejects', 'German', 'call', 'to', 'boycott', 'British', 'lamb'] ['B-ORG', 'O', 'B-MISC', 'O', 'O', 'O', 'B-MISC', 'O'] 1 ['peter', 'blackburn'] ['B-PER', 'I-PER'] 2 ['brussels', '1996-08-22' ] ['B-LOC', 'O'] 3 ['The', 'European', 'Commission'] ['O', 'B-ORG', 'I-ORG'] ๊ฐ€๋ น, X_train[3]์˜ 'The'์™€ y_train[3]์˜ 'O'๋Š” ํ•˜๋‚˜์˜ ์Œ(pair)์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, X_train[3]์˜ 'European'๊ณผ y_train[3]์˜ 'B-ORG'๋Š” ์Œ์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋ฉฐ, X_train[3]์˜ 'Commision'๊ณผ y_train[3]์˜ 'I-ORG'๋Š” ์Œ์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณ‘๋ ฌ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋Š” ๊ฐ ๋ฐ์ดํ„ฐ๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„, ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ฃผ๊ธฐ ์œ„ํ•œ ํŒจ๋”ฉ(Padding) ์ž‘์—…์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. 2. ์‹œํ€€์Šค ๋ ˆ์ด๋ธ”๋ง(Sequence Labeling) ์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅ ์‹œํ€€์Šค X = [ 1 x, 3 , ..., n ]์— ๋Œ€ํ•˜์—ฌ ๋ ˆ์ด๋ธ” ์‹œํ€€์Šค y = [ 1 y, 3 , ..., n ]๋ฅผ ๊ฐ๊ฐ ๋ถ€์—ฌํ•˜๋Š” ์ž‘์—…์„ ์‹œํ€€์Šค ๋ ˆ์ด๋ธ”๋ง ์ž‘์—…(Sequence Labeling Task)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํƒœ๊น… ์ž‘์—…์€ ๋Œ€ํ‘œ์ ์ธ ์‹œํ€€์Šค ๋ ˆ์ด๋ธ”๋ง ์ž‘์—…์ž…๋‹ˆ๋‹ค. 3. ์–‘๋ฐฉํ–ฅ RNN(Bidirectional RNN) nn.RNN(input_size = input_size, hidden_size = hidden_size, num_layers = 1, batch_first=True, bidirectional = True) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋„ ๋ฐ”๋‹๋ผ RNN์ด ์•„๋‹ˆ๋ผ ์„ฑ๋Šฅ์ด ๊ฐœ์„ ๋œ RNN์ธ LSTM์ด๋‚˜ GRU ๋“ฑ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋‹จ๋ฐฉํ–ฅ RNN์„ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์–‘๋ฐฉํ–ฅ RNN์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์‹œ์ ์˜ ๋‹จ์–ด ์ •๋ณด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๋‹ค์Œ ์‹œ์ ์˜ ๋‹จ์–ด ์ •๋ณด๋„ ์ฐธ๊ณ ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ์–‘๋ฐฉํ–ฅ์€ ๊ธฐ์กด์˜ ๋‹จ๋ฐฉํ–ฅ nn.RNN()์—์„œ bidirectional ์ธ์ž์˜ ๊ฐ’์œผ๋กœ True๋ฅผ ๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 4. RNN์˜ ๋‹ค-๋Œ€-๋‹ค(Many-to-Many) ๋ฌธ์ œ ์ด์ œ RNN์ด ์–ด๋–ป๊ฒŒ ์„ค๊ณ„๋˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์—์„œ ์„ค๋ช…ํ•œ ๋ฐ์ดํ„ฐ ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ์— ํ•ด๋‹น๋˜๋Š” X_train[0]๋ฅผ ๊ฐ€์ง€๊ณ  4๋ฒˆ์˜ ์‹œ์ (time steps)๊นŒ์ง€ RNN์„ ์ง„ํ–‰ํ•˜์˜€์„ ๋•Œ์˜ ๊ทธ๋ฆผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์–‘๋ฐฉํ–ฅ RNN์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 14-02 ๊ฐœ์ฒด๋ช… ์ธ์‹ ์ดํ•ดํ•˜๊ธฐ ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ ๊ฐœ์ฒด(entity)์˜ ์œ ํ˜•์„ ์ธ์‹ํ•˜๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition)์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”ํผ์Šค๋กœ๋ถ€ํ„ฐ ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ์‚ฌ๋žŒ, ์žฅ์†Œ, ์กฐ์ง ๋“ฑ์„ ์˜๋ฏธํ•˜๋Š” ๋‹จ์–ด์ธ์ง€๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition)์ด๋ž€? ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition)์ด๋ž€ ๋ง ๊ทธ๋Œ€๋กœ ์ด๋ฆ„์„ ๊ฐ€์ง„ ๊ฐœ์ฒด(named entity)๋ฅผ ์ธ์‹ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ข€ ๋” ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด, ์–ด๋–ค ์ด๋ฆ„์„ ์˜๋ฏธํ•˜๋Š” ๋‹จ์–ด๋ฅผ ๋ณด๊ณ ๋Š” ๊ทธ ๋‹จ์–ด๊ฐ€ ์–ด๋–ค ์œ ํ˜•์ธ์ง€๋ฅผ ์ธ์‹ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ ์ •์ด๋Š” 2018๋…„์— ๊ณจ๋“œ๋งŒ์‚ญ์Šค์— ์ž…์‚ฌํ–ˆ๋‹ค.๋ผ๋Š” ๋ฌธ์žฅ์ด ์žˆ์„ ๋•Œ, ์‚ฌ๋žŒ(person), ์กฐ์ง(organization), ์‹œ๊ฐ„(time)์— ๋Œ€ํ•ด ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์ด๋ผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์œ ์ • - ์‚ฌ๋žŒ 2018๋…„ - ์‹œ๊ฐ„ ๊ณจ๋“œ๋งŒ์‚ญ์Šค - ์กฐ์ง 2. NLTK๋ฅผ ์ด์šฉํ•œ ๊ฐœ์ฒด๋ช… ์ธ์‹(Named Entity Recognition using NTLK) NLTK์—์„œ๋Š” ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ(NER chunker)๋ฅผ ์ง€์›ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ๋ณ„๋„ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•  ํ•„์š” ์—†์ด NLTK๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐœ์ฒด๋ช… ์ธ์‹์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์•„๋ž˜์˜ ์‹ค์Šต์—์„œ nltk.download('maxent_ne_chunker'), nltk.download('words') ๋“ฑ์˜ ์„ค์น˜๋ฅผ ์š”๊ตฌํ•˜๋Š” ์—๋Ÿฌ ๋ฌธ๊ตฌ๊ฐ€ ๋œฌ๋‹ค๋ฉด, ์ง€์‹œํ•˜๋Š” ๋Œ€๋กœ ์„ค์น˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. from nltk import word_tokenize, pos_tag, ne_chunk sentence = "James is working at Disney in London" # ํ† ํฐํ™” ํ›„ ํ’ˆ์‚ฌ ํƒœ๊น… tokenized_sentence = pos_tag(word_tokenize(sentence)) print(tokenized_sentence) [('James', 'NNP'), ('is', 'VBZ'), ('working', 'VBG'), ('at', 'IN'), ('Disney', 'NNP'), ('in', 'IN'), ('London', 'NNP')] # ๊ฐœ์ฒด๋ช… ์ธ์‹ ner_sentence = ne_chunk(tokenized_sentence) print(ner_sentence) (S (PERSON James/NNP) is/VBZ working/VBG at/IN (ORGANIZATION Disney/NNP) in/IN (GPE London/NNP)) ne_chunk๋Š” ๊ฐœ์ฒด๋ช…์„ ํƒœ๊น… ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ž์„œ ํ’ˆ์‚ฌ ํƒœ๊น…(pos_tag)์ด ์ˆ˜ํ–‰๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ฒฐ๊ณผ์—์„œ James๋Š” PERSON(์‚ฌ๋žŒ), Disney๋Š” ์กฐ์ง(ORGANIZATION), London์€ ์œ„์น˜(GPE)๋ผ๊ณ  ์ •์ƒ์ ์œผ๋กœ ๊ฐœ์ฒด๋ช… ์ธ์‹์ด ์ˆ˜ํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์˜ ์‹ค์Šต์—์„œ ์ธ๊ณต ์‹ ๊ฒฝ๋ง์„ ์ด์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 14-03 ์–‘๋ฐฉํ–ฅ LSTM์„ ์ด์šฉํ•œ ๊ฐœ์ฒด ๋ช…์ธ์‹ PyTorch์˜ ์–‘๋ฐฉํ–ฅ LSTM(Bidirectional LSTM)์„ ์ด์šฉํ•˜์—ฌ ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ๋‹จ์–ด ํ† ํฐํ™” import urllib.request import numpy as np from tqdm import tqdm import re from collections import Counter import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split NLTK๋ฅผ ์ด์šฉํ•˜๋ฉด ์˜์–ด ์ฝ”ํผ์Šค์— ํ† ํฐํ™”์™€ ํ’ˆ์‚ฌ ํƒœ๊น… ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•œ ๋ฌธ์žฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ํ›ˆ๋ จ์‹œ์ผœ ํ’ˆ์‚ฌ ํƒœ๊น…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ €์ž์˜ ๊นƒํ—ˆ๋ธŒ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/ukairia777/tensorflow-nlp-tutorial/main/12.%20RNN%20Sequence%20Labeling/dataset/train.txt", filename="train.txt") ์ „์ฒ˜๋ฆฌ ํ›„ ์ „์ฒด ๋ฌธ์žฅ ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. f = open('train.txt', 'r') tagged_sentences = [] sentence = [] for line in f: if len(line)==0 or line.startswith('-DOCSTART') or line[0]=="\n": if len(sentence) > 0: tagged_sentences.append(sentence) sentence = [] continue splits = line.split(' ') # ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ์†์„ฑ์„ ๊ตฌ๋ถ„ํ•œ๋‹ค. splits[-1] = re.sub(r'\n', '', splits[-1]) # ์ค„๋ฐ”๊ฟˆ ํ‘œ์‹œ \n์„ ์ œ๊ฑฐํ•œ๋‹ค. word = splits[0].lower() # ๋‹จ์–ด๋“ค์€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ์ €์žฅํ•œ๋‹ค. sentence.append([word, splits[-1]]) # ๋‹จ์–ด์™€ ๊ฐœ์ฒด๋ช… ํƒœ๊น…๋งŒ ๊ธฐ๋กํ•œ๋‹ค. print("์ „์ฒด ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜: ", len(tagged_sentences)) # ์ „์ฒด ์ƒ˜ํ”Œ์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ์ „์ฒด ์ƒ˜ํ”Œ ๊ฐœ์ˆ˜: 14041 ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(tagged_sentences[0]) # ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ ์ถœ๋ ฅ [['eu', 'B-ORG'], ['rejects', 'O'], ['german', 'B-MISC'], ['call', 'O'], ['to', 'O'], ['boycott', 'O'], ['british', 'B-MISC'], ['lamb', 'O'], ['.', 'O']] ๊ฐ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด๋Š” sentences์— ํƒœ๊น… ์ •๋ณด๋Š” pos_tags์— ์ €์žฅํ•˜๊ณ  ์ฒซ ๋ฒˆ์งธ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. sentences, ner_tags = [], [] for tagged_sentence in tagged_sentences: # 14,041๊ฐœ์˜ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ 1๊ฐœ์”ฉ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. sentence, tag_info = zip(*tagged_sentence) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด๋“ค์€ sentence์— ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋“ค์€ tag_info์— ์ €์žฅ. sentences.append(list(sentence)) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๋‹จ์–ด ์ •๋ณด๋งŒ ์ €์žฅํ•œ๋‹ค. ner_tags.append(list(tag_info)) # ๊ฐ ์ƒ˜ํ”Œ์—์„œ ๊ฐœ์ฒด๋ช… ํƒœ๊น… ์ •๋ณด๋งŒ ์ €์žฅํ•œ๋‹ค. print(sentences[0]) print(ner_tags[0]) ['eu', 'rejects', 'german', 'call', 'to', 'boycott', 'british', 'lamb', '.'] ['B-ORG', 'O', 'B-MISC', 'O', 'O', 'O', 'B-MISC', 'O', 'O'] ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ sentences[0]์—, ํ’ˆ์‚ฌ์— ๋Œ€ํ•ด์„œ๋งŒ pos_tags[0]์— ์ €์žฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์—์„œ ๋ณด๊ฒ ์ง€๋งŒ, sentences๋Š” ์˜ˆ์ธก์„ ์œ„ํ•œ X์— ํ•ด๋‹น๋˜๋ฉฐ pos_tags๋Š” ์˜ˆ์ธก ๋Œ€์ƒ์ธ y์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ž„์˜๋กœ 12๋ฒˆ ์ธ๋ฑ์Šค ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ๋„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print(sentences[12]) print(ner_tags[12]) ['only', 'france', 'and', 'britain', 'backed', 'fischler', "'s", 'proposal', '.'] ['O', 'B-LOC', 'O', 'B-LOC', 'O', 'B-PER', 'O', 'O', 'O'] ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ๋งŒ sentences[12]์—, ๋˜ํ•œ ํ’ˆ์‚ฌ์— ๋Œ€ํ•ด์„œ๋งŒ pos_tags[12]์— ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๊ณผ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•ด ๋ด…์‹œ๋‹ค. X_train, X_test, y_train, y_test = train_test_split(sentences, ner_tags, test_size=.2, random_state=777) ํ•™์Šต์ด ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ ์„ฑ๋Šฅ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ๋˜ํ•œ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=.2, random_state=777) ํ•™์Šต ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :', len(X_train)) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :', len(X_valid)) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :', len(X_test)) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ :', len(X_train)) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ :', len(X_valid)) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ :', len(X_test)) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 8985 ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 2247 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 2809 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ : 8985 ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ : 2247 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ ˆ์ด๋ธ”์˜ ๊ฐœ์ˆ˜ : 2809 ํ•™์Šต ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ 2๊ฐœ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. ํ˜„์žฌ ๋ฐ์ดํ„ฐ๋Š” ๋‹จ์–ด ํ† ํฐ ํ™”๊ฐ€ ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. # ์ƒ์œ„ ์ƒ˜ํ”Œ 2๊ฐœ ์ถœ๋ ฅ for sent in X_train[:2]: print(sent) ['young', 'boys', '9', '1', '0', '8', '6', '19', '3'] ['hentgen', '(', '17-7', ')', 'surrendered', 'just', 'three', 'doubles', 'and', 'a', 'pair', 'of', 'singles', 'in', 'tossing', 'his', 'major-league', 'leading', 'ninth', 'complete', 'game', '.'] 2. Vocab ๋งŒ๋“ค๊ธฐ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ๊ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„๋ฅผ ์นด์šดํŠธํ•ด ์ฃผ๋Š” Counter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋‹จ์–ด๋ณ„ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ธฐ๋ก๋œ ๋‹จ์–ด์˜ ์ด ์ข…๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ์ด ๋‹จ์–ด ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. word_list = [] for sent in X_train: for word in sent: word_list.append(word) word_counts = Counter(word_list) print('์ด ๋‹จ์–ด ์ˆ˜ :', len(word_counts)) ์ด ๋‹จ์–ด ์ˆ˜ : 16742 ๋‹จ์–ด ์ˆ˜๋Š” 16,742๊ฐœ์ž…๋‹ˆ๋‹ค. ์ž„์˜๋กœ ์˜๋‹จ์–ด the์™€ love์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด the์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ :', word_counts['the']) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด love์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ :', word_counts['love']) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด the์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ : 5410 ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋‹จ์–ด love์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ : 7 ์˜๋‹จ์–ด the์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋Š” 5,410ํšŒ์ด๋ฉฐ, ์˜๋‹จ์–ด love์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋Š” 7ํšŒ์ž…๋‹ˆ๋‹ค. word_counts๋ฅผ ์ •๋ ฌํ•˜๊ณ  ๋“ฑ์žฅ ๋นˆ๋„ ์ƒ์œ„ 10๊ฐœ ๋‹จ์–ด๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. vocab = sorted(word_counts, key=word_counts.get, reverse=True) print('๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 10๊ฐœ ๋‹จ์–ด') print(vocab[:10]) ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ ์ƒ์œ„ 10๊ฐœ ๋‹จ์–ด ['the', ',', '.', 'of', 'in', 'to', 'a', ')', '(', 'and'] ์ด์ œ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ํŒจ๋”ฉ์„ ์œ„ํ•œ ํ† ํฐ, ๊ทธ๋ฆฌ๊ณ  OOV ๋ฌธ์ œ(Out-Of-Vocabulary) ๋ฐœ์ƒ ์‹œ์— ์‚ฌ์šฉํ•˜๋Š” UNK ํ† ํฐ์„ ์œ„ํ•œ ์ •์ˆ˜ 0๊ณผ 1์„ ๊ฐ๊ฐ ๋‹จ์–ด ์ง‘ํ•ฉ์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. word_to_index = {} word_to_index['<PAD>'] = 0 word_to_index['<UNK>'] = 1 for index, word in enumerate(vocab) : word_to_index[word] = index + 2 vocab_size = len(word_to_index) print('ํŒจ๋”ฉ ํ† ํฐ๊ณผ UNK ํ† ํฐ์„ ๊ณ ๋ คํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', vocab_size) ํŒจ๋”ฉ ํ† ํฐ๊ณผ UNK ํ† ํฐ์„ ๊ณ ๋ คํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 16744 print('๋‹จ์–ด <PAD>์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ :', word_to_index['<PAD>']) print('๋‹จ์–ด <UNK>์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ :', word_to_index['<UNK>']) print('๋‹จ์–ด the์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ :', word_to_index['the']) ๋‹จ์–ด <PAD>์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ : 0 ๋‹จ์–ด <UNK>์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ : 1 ๋‹จ์–ด the์™€ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜ : 2 3. ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ…์ŠคํŠธ๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋Š” OOV ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ๋‹จ์–ด๋Š” ํ† ํฐ๊ณผ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜์ธ 1๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. def texts_to_sequences(tokenized_X_data, word_to_index): encoded_X_data = [] for sent in tokenized_X_data: index_sequences = [] for word in sent: try: index_sequences.append(word_to_index[word]) except KeyError: index_sequences.append(word_to_index['<UNK>']) encoded_X_data.append(index_sequences) return encoded_X_data ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. encoded_X_train = texts_to_sequences(X_train, word_to_index) encoded_X_valid = texts_to_sequences(X_valid, word_to_index) encoded_X_test = texts_to_sequences(X_test, word_to_index) ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ์ƒ์œ„ ์ƒ˜ํ”Œ 2๊ฐœ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. # ์ƒ์œ„ ์ƒ˜ํ”Œ 2๊ฐœ ์ถœ๋ ฅ for sent in encoded_X_train[:2]: print(sent) [1260, 3215, 117, 17, 21, 123, 56, 539, 23] [5456, 10, 8229, 9, 8230, 186, 84, 1815, 11, 8, 1073, 5, 421, 6, 8231, 35, 2043, 291, 790, 957, 267, 4] ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” word_to_index์˜ key์™€ value๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ €์žฅํ•˜์—ฌ index_to_word๋ฅผ ๋งŒ๋“ค๊ณ , ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ๋ณต์›ํ•ด ๋ด…์‹œ๋‹ค. index_to_word = {} for key, value in word_to_index.items(): index_to_word[value] = key decoded_sample = [index_to_word[word] for word in encoded_X_train[0]] print('๊ธฐ์กด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :', X_train[0]) print('๋ณต์›๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ :', decoded_sample) ๊ธฐ์กด์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : ['young', 'boys', '9', '1', '0', '8', '6', '19', '3'] ๋ณต์›๋œ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ : ['young', 'boys', '9', '1', '0', '8', '6', '19', '3'] ์ด์ œ ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•ด์„œ๋„ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ ˆ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋‹จ์–ด๋“ค์˜ ์ง‘ํ•ฉ์„ ๊ตฌํ•ด๋ด…์‹œ๋‹ค. # y_train์œผ๋กœ๋ถ€ํ„ฐ ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ํƒœ๊ทธ๋“ค์˜ ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ flatten_tags = [tag for sent in y_train for tag in sent] tag_vocab = list(set(flatten_tags)) print('ํƒœ๊ทธ ์ง‘ํ•ฉ :', tag_vocab) print('ํƒœ๊ทธ ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', len(tag_vocab)) ํƒœ๊ทธ ์ง‘ํ•ฉ : ['B-PER', 'I-MISC', 'B-ORG', 'I-PER', 'B-LOC', 'I-LOC', 'I-ORG', 'O', 'B-MISC'] ํƒœ๊ทธ ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 9 ๋ ˆ์ด๋ธ”์˜ ๊ฐ ๋‹จ์–ด์— ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•˜์—ฌ ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. tag_to_index = {} tag_to_index['<PAD>'] = 0 for index, word in enumerate(tag_vocab) : tag_to_index[word] = index + 1 tag_vocab_size = len(tag_to_index) # print('ํŒจ๋”ฉ ํ† ํฐ๊นŒ์ง€ ํฌํ•จ๋œ ํƒœ๊ทธ ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ :', tag_vocab_size) print('ํƒœ๊ทธ ์ง‘ํ•ฉ :', tag_to_index) ํƒœ๊ทธ ์ง‘ํ•ฉ : {'<PAD>': 0, 'B-PER': 1, 'I-MISC': 2, 'B-ORG': 3, 'I-PER': 4, 'B-LOC': 5, 'I-LOC': 6, 'I-ORG': 7, 'O': 8, 'B-MISC': 9} many-to-many ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ ˆ์ด๋ธ”๋„ ์‹œํ€€์Šค ๋ฐ์ดํ„ฐ๊ฐ€ ๋˜๋ฏ€๋กœ ๊ฐ ๋ ˆ์ด๋ธ”์„ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ด ์ค๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๋ ˆ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด tag_to_index๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ ˆ์ด๋ธ”์˜ ๊ฐ ๋‹จ์–ด๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ธ encoding_label() ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. def encoding_label(sequence, tag_to_index): label_sequence = [] for seq in sequence: label_sequence.append([tag_to_index[tag] for tag in seq]) return label_sequence ์ƒ์œ„ 2๊ฐœ์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print('X ๋ฐ์ดํ„ฐ ์ƒ์œ„ 2๊ฐœ') print(encoded_X_train[:2]) print('-' * 50) print('y ๋ฐ์ดํ„ฐ ์ƒ์œ„ 2๊ฐœ') print(encoded_y_train[:2]) print('-' * 50) print('์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๊ณผ ๋ ˆ์ด๋ธ”์˜ ๊ธธ์ด') print(len(encoded_X_train[0])) print(len(encoded_y_train[0])) X ๋ฐ์ดํ„ฐ ์ƒ์œ„ 2๊ฐœ [[1260, 3215, 117, 17, 21, 123, 56, 539, 23], [5456, 10, 8229, 9, 8230, 186, 84, 1815, 11, 8, 1073, 5, 421, 6, 8231, 35, 2043, 291, 790, 957, 267, 4]] -------------------------------------------------- y ๋ฐ์ดํ„ฐ ์ƒ์œ„ 2๊ฐœ [[3, 7, 8, 8, 8, 8, 8, 8, 8], [1, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8]] -------------------------------------------------- ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ๊ณผ ๋ ˆ์ด๋ธ”์˜ ๊ธธ์ด 9 4. ํŒจ๋”ฉ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ฃผ๋Š” ์ž‘์—…์ธ ํŒจ๋”ฉ์„ ์œ„ํ•ด์„œ๋Š” ๊ฐ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. print('์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : %d' % max(len(l) for l in encoded_X_train)) print('์ƒ˜ํ”Œ์˜ ํ‰๊ท  ๊ธธ์ด : %f' % (sum(map(len, encoded_X_train))/len(encoded_X_train))) plt.hist([len(s) for s in encoded_X_train], bins=50) plt.xlabel('length of samples') plt.ylabel('number of samples') plt.show() ์ƒ˜ํ”Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด : 78 ์ƒ˜ํ”Œ์˜ ํ‰๊ท  ๊ธธ์ด : 14.518420 ๊ฐ€์žฅ ๊ธด ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋Š” 78์ด๋ฉฐ, ๊ทธ๋ž˜ํ”„๋ฅผ ๋ดค์„ ๋•Œ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ๋ถ„ํฌ๋Š” ๋Œ€์ฒด์ ์œผ๋กœ ์•ฝ 50๋‚ด์™ธ์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก encoded_X_train๊ณผ encoded_X_test์˜ ๋ชจ๋“  ์ƒ˜ํ”Œ์˜ ๊ธธ์ด๋ฅผ ํŠน์ • ๊ธธ์ด๋กœ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ๊ธธ์ด ๋ณ€์ˆ˜๋ฅผ max_len์œผ๋กœ ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ฆฌ๋ทฐ๊ฐ€ ๋‚ด์šฉ์ด ์ž˜๋ฆฌ์ง€ ์•Š๋„๋ก ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์ ์˜ max_len์˜ ๊ฐ’์€ ๋ช‡์ผ๊นŒ์š”? ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ max_len ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ์ด ๋ช‡ % ์ธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. def below_threshold_len(max_len, nested_list): count = 0 for sentence in nested_list: if(len(sentence) <= max_len): count = count + 1 print('์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ %s ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: %s'%(max_len, (count / len(nested_list))*100)) ์‚ฌ์‹ค ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 78์ด๋ฏ€๋กœ 78๋กœ ํŒจ๋”ฉ ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 80 ์ •๋„๋กœ ํŒจ๋”ฉ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. max_len = 80 below_threshold_len(max_len, encoded_X_train) ์ „์ฒด ์ƒ˜ํ”Œ ์ค‘ ๊ธธ์ด๊ฐ€ 80 ์ดํ•˜์ธ ์ƒ˜ํ”Œ์˜ ๋น„์œจ: 100.0 ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋ฅผ 80์œผ๋กœ ํŒจ๋”ฉ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. max_len์„ ์ธ์ž๋กœ ์ž…๋ ฅ๋ฐ›์•„์„œ max_len๋ณด๋‹ค ์งง์€ ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ์—๋Š” ๋’ค์— 0์„ ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜์ธ pad_sequences()๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. def pad_sequences(sentences, max_len): features = np.zeros((len(sentences), max_len), dtype=int) for index, sentence in enumerate(sentences): if len(sentence) != 0: features[index, :len(sentence)] = np.array(sentence)[:max_len] return features ํ•จ์ˆ˜ pad_sequences()๋กœ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ, ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ํŒจ๋”ฉ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ณผ ๊ฐ™์€ Many-to-Many ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ ˆ์ด๋ธ”๋„ ํŒจ๋”ฉ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŒจ๋”ฉ ํ›„์— ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๊ธธ์ด๊ฐ€ 80์œผ๋กœ ํŒจ๋”ฉ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. padded_X_train = pad_sequences(encoded_X_train, max_len=max_len) padded_X_valid = pad_sequences(encoded_X_valid, max_len=max_len) padded_X_test = pad_sequences(encoded_X_test, max_len=max_len) padded_y_train = pad_sequences(encoded_y_train, max_len=max_len) padded_y_valid = pad_sequences(encoded_y_valid, max_len=max_len) padded_y_test = pad_sequences(encoded_y_test, max_len=max_len) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_train.shape) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_valid.shape) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :', padded_X_test.shape) print('-' * 30) print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ” :', padded_y_train.shape) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ” :', padded_y_valid.shape) print('ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ” :', padded_y_test.shape) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (8985, 80) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (2247, 80) ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (2809, 80) ------------------------------ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ” : (8985, 80) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ” : (2247, 80) ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋ ˆ์ด๋ธ” : (2809, 80) ํŒจ๋”ฉ ํ›„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. print('ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ ์ƒ˜ํ”Œ 2๊ฐœ') print(padded_X_train[:2]) print('-' * 5 + '๋ ˆ์ด๋ธ”' + '-' * 5) print(padded_y_train[:2]) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ์ƒ์œ„ ์ƒ˜ํ”Œ 2๊ฐœ [[1260 3215 117 17 21 123 56 539 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [5456 10 8229 9 8230 186 84 1815 11 8 1073 5 421 6 8231 35 2043 291 790 957 267 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]] -----๋ ˆ์ด๋ธ”----- [[3 7 8 8 8 8 8 8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [1 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]] 5. ๋ชจ๋ธ๋ง ์ด์ œ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F GPU๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ™˜๊ฒฝ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. USE_CUDA = torch.cuda.is_available() device = torch.device("cuda" if USE_CUDA else "cpu") print("cpu์™€ cuda ์ค‘ ๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•จ:", device) cpu์™€ cuda ์ค‘ ๋‹ค์Œ ๊ธฐ๊ธฐ๋กœ ํ•™์Šตํ•จ: cuda cuda๋ผ๊ณ  ์ถœ๋ ฅ๋œ๋‹ค๋ฉด GPU๋ฅผ ํ™œ์šฉ ๊ฐ€๋Šฅํ•œ ํ™˜๊ฒฝ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, Colab์—์„œ ์‹ค์Šต ์ค‘์ด๊ณ  cuda๊ฐ€ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด Colab ํ™”๋ฉด ์ƒ๋‹จ์—์„œ ๋Ÿฐํƒ€์ž„ > ๋Ÿฐํƒ€์ž„ ์œ ํ˜• ๋ณ€๊ฒฝ > ํ•˜๋“œ์›จ์–ด ๊ฐ€์†๊ธฐ > GPU ์žฅ๋น„ ์„ ํƒ๋ฅผ ์ด์šฉํ•˜์—ฌ GPU ์žฅ๋น„๋ฅผ ์„ ํƒํ•˜์‹  ํ›„์— ์‹ค์Šตํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ด์ œ ๊ฐœ์ฒด๋ช… ์ธ์‹ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ๋งŒ์•ฝ, ๋‹จ๋ฐฉํ–ฅ GRU๋ฅผ ๋ชจ๋ธ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. class NERTagger(nn.Module): def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim): super(NERTagger, self).__init__() self.embedding = nn.Embedding(vocab_size, embedding_dim) self.gru = nn.GRU(embedding_dim, hidden_dim, batch_first=True) self.fc = nn.Linear(hidden_dim, output_dim) def forward(self, x): # x: (batch_size, seq_length) embedded = self.embedding(x) # (batch_size, seq_length, embedding_dim) gru_out, _ = self.gru(embedded) # (batch_size, seq_length, hidden_dim) logits = self.fc(gru_out) # (batch_size, seq_length, output_dim) return logits ํ•˜์ง€๋งŒ ์œ„์˜ GRU๋ฅผ ์–‘๋ฐฉํ–ฅ LSTM(Bidirectional LSTM) 2์ธต์งœ๋ฆฌ๋กœ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. class NERTagger(nn.Module): def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, num_layers=2): super(NERTagger, self).__init__() self.embedding = nn.Embedding(vocab_size, embedding_dim) self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=num_layers, batch_first=True, bidirectional=True) self.fc = nn.Linear(hidden_dim*2, output_dim) def forward(self, x): # x: (batch_size, seq_length) embedded = self.embedding(x) # (batch_size, seq_length, embedding_dim) lstm_out, _ = self.lstm(embedded) # (batch_size, seq_length, hidden_dim*2) logits = self.fc(lstm_out) # (batch_size, seq_length, output_dim) return logits ์œ„์—์„œ ์ž‘์„ฑํ•œ GRU ์ฝ”๋“œ์™€ ์–‘๋ฐฉํ–ฅ LSTM ์ฝ”๋“œ์˜ ์ฐจ์ด์ ์„ ๋ด…์‹œ๋‹ค. nn.GRU๋ฅผ nn.LSTM์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค. num_layers ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์ด๋ฅผ nn.LSTM ์ƒ์„ฑ์ž์— ์ „๋‹ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 2์ž…๋‹ˆ๋‹ค. bidirectional=True๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์–‘๋ฐฉํ–ฅ LSTM์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. nn.Linear์˜ ์ž…๋ ฅ ์ฐจ์›์„ hidden_dim*2๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์–‘๋ฐฉํ–ฅ LSTM์˜ ์ถœ๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด ํ† ์น˜์˜ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ๋ฐฐ์น˜ ๋‹จ์œ„ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. X_train_tensor = torch.tensor(padded_X_train, dtype=torch.long) y_train_tensor = torch.tensor(padded_y_train, dtype=torch.long) X_valid_tensor = torch.tensor(padded_X_valid, dtype=torch.long) y_valid_tensor = torch.tensor(padded_y_valid, dtype=torch.long) X_test_tensor = torch.tensor(padded_X_test, dtype=torch.long) y_test_tensor = torch.tensor(padded_y_test, dtype=torch.long) train_dataset = torch.utils.data.TensorDataset(X_train_tensor, y_train_tensor) train_dataloader = torch.utils.data.DataLoader(train_dataset, shuffle=True, batch_size=32) valid_dataset = torch.utils.data.TensorDataset(X_valid_tensor, y_valid_tensor) valid_dataloader = torch.utils.data.DataLoader(valid_dataset, shuffle=False, batch_size=32) test_dataset = torch.utils.data.TensorDataset(X_test_tensor, y_test_tensor) test_dataloader = torch.utils.data.DataLoader(test_dataset, shuffle=False, batch_size=32) ์ด์ œ ์œ„์—์„œ ์„ ์–ธํ•œ NERTagger ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ํ˜„์žฌ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. print('๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ:', vocab_size) ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ: 16744 ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๊ธฐ ์œ„ํ•œ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 100, LSTM์˜ ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›์€ 256, ์ถœ๋ ฅ์ธต์˜ ์ฐจ์›์€ tag_vocab_size์ด๋ฉฐ ์•ž์—์„œ ํ™•์ธํ•œ ๋ฐ”์™€ ๊ฐ™์ด 10์ด๋ฉฐ, ํ•™์Šต๋ฅ (learning rate)๋Š” 0.01, ํ•™์Šต ํšŸ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ์—ํฌํฌ๋Š” 10, LSTM์˜ ์€๋‹‰์ธต ์ˆ˜๋Š” 2๋กœ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. embedding_dim = 100 hidden_dim = 256 output_dim = tag_vocab_size learning_rate = 0.01 num_epochs = 10 num_layers = 2 ์ด๋กœ๋ถ€ํ„ฐ ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. # Model, loss, optimizer model = NERTagger(vocab_size, embedding_dim, hidden_dim, output_dim, num_layers) model.to(device) ์•ž์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๋น„์šฉํ•จ์ˆ˜์ธ nn.CrossEntropyLoss์—์„œ๋Š” ignore_index๋ฅผ ํ†ตํ•ด์„œ ํŠน์ • ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ loss๋ฅผ ๊ตฌํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ignore_index=0์„ ์‚ฌ์šฉํ•˜๋ฉด ํŒจ๋”ฉ ์œ„์น˜์— ๋Œ€ํ•ด์„œ๋Š” loss๋ฅผ ๊ตฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. criterion = nn.CrossEntropyLoss(ignore_index=0) optimizer = optim.Adam(model.parameters(), lr=learning_rate) 6. ํ‰๊ฐ€ ์ฝ”๋“œ ์ž‘์„ฑ ํ•™์Šตํ•˜๋Š” ๋™์•ˆ ํ•™์Šต ๋ฐ์ดํ„ฐ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ loss๋ฅผ ๊ตฌํ•  ๊ฒƒ์ด๋ฏ€๋กœ ํ•™์Šตํ•˜๊ธฐ ์ „์— ํ‰๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ ์ •ํ™•๋„๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜์ธ calculate_accuracy()๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ ๊ณ ๋ คํ•ด์•ผ ํ•  ์ ์€ ํŒจ๋”ฉ ํ† ํฐ์ด ์žˆ๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ๊ณ„์‚ฐ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. def calculate_accuracy(logits, labels, ignore_index=0): # ์˜ˆ์ธก ๋ ˆ์ด๋ธ”์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. predicted = torch.argmax(logits, dim=1) # ํŒจ๋”ฉ ํ† ํฐ์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. mask = (labels != ignore_index) # ์ •๋‹ต์„ ๋งžํžŒ ๊ฒฝ์šฐ๋ฅผ ์ง‘๊ณ„ํ•ฉ๋‹ˆ๋‹ค. correct = (predicted == labels).masked_select(mask).sum().item() total = mask.sum().item() accuracy = correct / total return accuracy ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ๋ถ€ํ„ฐ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๋Š” evaluate() ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. evaluate() ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋Š” ์œ„์—์„œ ์ž‘์„ฑํ•œ calculate_accuracy()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. def evaluate(model, valid_dataloader, criterion, device): val_loss = 0 val_correct = 0 val_total = 0 model.eval() with torch.no_grad(): for batch_X, batch_y in valid_dataloader: batch_X, batch_y = batch_X.to(device), batch_y.to(device) # Forward pass logits = model(batch_X) # Compute loss loss = criterion(logits.view(-1, output_dim), batch_y.view(-1)) # Calculate validation accuracy and loss val_loss += loss.item() val_correct += calculate_accuracy(logits.view(-1, output_dim), batch_y.view(-1)) * batch_y.size(0) val_total += batch_y.size(0) val_accuracy = val_correct / val_total val_loss /= len(valid_dataloader) return val_loss, val_accuracy 7. ๋ชจ๋ธ ํ•™์Šตํ•˜๊ธฐ # Training loop best_val_loss = float('inf') for epoch in range(num_epochs): # Training train_loss = 0 train_correct = 0 train_total = 0 model.train() for batch_X, batch_y in train_dataloader: # Forward pass batch_X, batch_y = batch_X.to(device), batch_y.to(device) logits = model(batch_X) # Compute loss loss = criterion(logits.view(-1, output_dim), batch_y.view(-1)) # Backward pass and optimization optimizer.zero_grad() loss.backward() optimizer.step() # Calculate training accuracy and loss train_loss += loss.item() train_correct += calculate_accuracy(logits.view(-1, output_dim), batch_y.view(-1)) * batch_y.size(0) train_total += batch_y.size(0) train_accuracy = train_correct / train_total train_loss /= len(train_dataloader) # Validation val_loss, val_accuracy = evaluate(model, valid_dataloader, criterion, device) print(f'Epoch {epoch+1}/{num_epochs}:') print(f'Train Loss: {train_loss:.4f}, Train Accuracy: {train_accuracy:.4f}') print(f'Validation Loss: {val_loss:.4f}, Validation Accuracy: {val_accuracy:.4f}') # ๊ฒ€์ฆ ์†์‹ค์ด ์ตœ์†Œ์ผ ๋•Œ ์ฒดํฌํฌ์ธํŠธ ์ €์žฅ if val_loss < best_val_loss: print(f'Validation loss improved from {best_val_loss:.4f} to {val_loss:.4f}. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.') best_val_loss = val_loss torch.save(model.state_dict(), 'best_model_checkpoint.pth') 8. ๋ชจ๋ธ ๋กœ๋“œ ๋ฐ ํ‰๊ฐ€ ์œ„์—์„œ ์ €์žฅํ•ด๋‘” Best ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜์—ฌ ์ •์ƒ ๋กœ๋“œ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค์„ ์ถœ๋ ฅํ•˜๊ณ , ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ํ‰๊ฐ€๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. # ๋ชจ๋ธ ๋กœ๋“œ model.load_state_dict(torch.load('best_model_checkpoint.pth')) # ๋ชจ๋ธ์„ device์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. model.to(device) # ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„(accuracy)์™€ ์†์‹ค(loss) ๊ณ„์‚ฐ val_loss, val_accuracy = evaluate(model, valid_dataloader, criterion, device) print(f'Best model validation loss: {val_loss:.4f}') print(f'Best model validation accuracy: {val_accuracy:.4f}') Best model validation loss: 0.1606 Best model validation accuracy: 0.9560 ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ์ •ํ™•๋„์™€ ์†์‹ค์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. # ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ test_loss, test_accuracy = evaluate(model, test_dataloader, criterion, device) print(f'Best model test loss: {test_loss:.4f}') print(f'Best model test accuracy: {test_accuracy:.4f}') Best model test loss: 0.1609 Best model test accuracy: 0.9566 9. ์ธํผ๋Ÿฐ์Šค ๋ฐ ํ…Œ์ŠคํŠธ ๋ชจ๋ธ์„ ์„œ๋น„์Šค์— ์ ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ „์ฒ˜๋ฆฌ๊ฐ€ ์ „ํ˜€ ๋˜์–ด์žˆ์ง€ ์•Š์€ ์ž„์˜์˜ ํ…์ŠคํŠธ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ๋™์ž‘ํ•ด์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ž„์˜์˜ ํ…์ŠคํŠธ ์ž…๋ ฅ์— ๋Œ€ํ•ด์„œ ์˜ˆ์ธก ๋ ˆ์ด๋ธ”์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. index_to_tag = {} for key, value in tag_to_index.items(): index_to_tag[value] = key def predict_labels(text, model, word_to_ix, index_to_tag, max_len=150): # ๋‹จ์–ด ํ† ํฐํ™” tokens = text.split() # ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ token_indices = [word_to_ix.get(token, 1) for token in tokens] # ํŒจ๋”ฉ token_indices_padded = np.zeros(max_len, dtype=int) token_indices_padded[:len(token_indices)] = token_indices[:max_len] # ํ…์„œ๋กœ ๋ณ€ํ™˜ input_tensor = torch.tensor(token_indices_padded, dtype=torch.long).unsqueeze(0).to(device) # ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์˜ˆ์ธก๊ฐ’ ๋ฆฌํ„ด model.eval() with torch.no_grad(): logits = model(input_tensor) # ๊ฐ€์žฅ ๊ฐ’์ด ๋†’์€ ์ธ๋ฑ์Šค๋ฅผ ์˜ˆ์ธก๊ฐ’์œผ๋กœ ์„ ํƒ predicted_indices = torch.argmax(logits, dim=-1).squeeze(0).tolist() # ํŒจ๋”ฉ ํ† ํฐ ์ œ๊ฑฐ predicted_indices_no_pad = predicted_indices[:len(tokens)] # ํŒจ๋”ฉ ํ† ํฐ์„ ์ œ์™ธํ•˜๊ณ  ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ์˜ˆ์ธก ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ predicted_tags = [index_to_tag[index] for index in predicted_indices_no_pad] return predicted_tags ํ•™์Šต์— ์‚ฌ์šฉ๋˜์ง€ ์•Š์€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ์ฒซ ๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ด์šฉํ•ด ๋ด…์‹œ๋‹ค. ํ˜„์žฌ ์ด ๋ฐ์ดํ„ฐ๋Š” ์ด๋ฏธ ๋‹จ์–ด ํ† ํฐ ํ™”๊ฐ€ ๋œ ์ƒํƒœ๋ผ์„œ ๋‹จ์–ด ํ† ํฐํ™” ์ด์ „ ์ƒํƒœ๋กœ ๋˜๋Œ๋ ค ์ „์ฒ˜๋ฆฌ๊ฐ€ ์ „ํ˜€ ๋˜์–ด์žˆ์ง€ ์•Š์€ ์ž…๋ ฅ์„ ๊ฐ€์ •ํ•˜๊ณ  ํ•จ์ˆ˜์— ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. print(X_test[0]) ['feyenoord', 'rotterdam', 'suffered', 'an', 'early', 'shock', 'when', 'they', 'went', '1-0', 'down', 'after', 'four', 'minutes', 'against', 'de', 'graafschap', 'doetinchem', '.'] ํ† ํฐํ™” ์ด์ „ ์ƒํƒœ๋กœ ๋Œ๋ฆฐ ํ›„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. sample = ' '.join(X_test[0]) print(sample) feyenoord rotterdam suffered an early shock when they went 1-0 down after four minutes against de graafschap doetinchem . ์‹ค์ œ ๋ ˆ์ด๋ธ”๊ณผ ์˜ˆ์ธก๊ฐ’์„ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. predicted_tags = predict_labels(sample, model, word_to_index, index_to_tag) print('์˜ˆ์ธก :', predicted_tags) print('์‹ค ์ œ๊ฐ’ :', y_test[0]) ์˜ˆ์ธก : ['B-ORG', 'I-ORG', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORG', 'I-ORG', 'I-ORG', 'O'] ์‹ค์ œ ๊ฐ’ : ['B-ORG', 'I-ORG', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORG', 'I-ORG', 'O'] 15. [NLP ๊ณ ๊ธ‰ ] ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €(Subword Tokenizer) ๊ธฐ๊ณ„์—๊ฒŒ ์•„๋ฌด๋ฆฌ ๋งŽ์€ ๋‹จ์–ด๋ฅผ ํ•™์Šต์‹œ์ผœ๋„, ์„ธ์ƒ์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์•Œ๋ ค์ค„ ์ˆ˜๋Š” ์—†๋Š” ๋…ธ๋ฆ‡์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๊ธฐ๊ณ„๊ฐ€ ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด ๊ทธ ๋‹จ์–ด๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๋ž€ ์˜๋ฏธ์—์„œ OOV(Out-Of-Vocabulary) ๋˜๋Š” UNK(Unknown Token)๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ, ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด (์‚ฌ๋žŒ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ง€๋งŒ) ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ๊นŒ๋‹ค๋กœ์›Œ์ง‘๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๋กœ ์ธํ•ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ๊นŒ๋‹ค๋กœ์›Œ์ง€๋Š” ์ƒํ™ฉ์„ OOV ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ(Subword segmenation) ์ž‘์—…์€ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋Š” ๋” ์ž‘์€ ๋‹จ์œ„์˜ ์˜๋ฏธ ์žˆ๋Š” ์—ฌ๋Ÿฌ ์„œ๋ธŒ ์›Œ๋“œ๋“ค(Ex) birthplace = birth + place)์˜ ์กฐํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—, ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ ์—ฌ๋Ÿฌ ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ๋‹จ์–ด๋ฅผ ์ธ์ฝ”๋”ฉ ๋ฐ ์ž„๋ฒ ๋”ฉํ•˜๊ฒ ๋‹ค๋Š” ์˜๋„๋ฅผ ๊ฐ€์ง„ ์ „์ฒ˜๋ฆฌ ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด OOV๋‚˜ ํฌ๊ท€ ๋‹จ์–ด, ์‹ ์กฐ์–ด์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ์™„ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์–ธ์–ด์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ ์˜์–ด๊ถŒ ์–ธ์–ด๋‚˜ ํ•œ๊ตญ์–ด๋Š” ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ๋ฅผ ์‹œ๋„ํ–ˆ์„ ๋•Œ ์–ด๋Š ์ •๋„ ์˜๋ฏธ ์žˆ๋Š” ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์ด๋Ÿฐ ์ž‘์—…์„ ํ•˜๋Š” ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €๋ผ๊ณ  ๋ช…๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €์˜ ์ฃผ์š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๋ฐ”์ดํŠธ ํŽ˜์–ด ์ธ์ฝ”๋”ฉ๊ณผ ์‹ค์ œ ์‹ค๋ฌด์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ € ๊ตฌํ˜„์ฒด์ธ SentencePiece์™€ Huggingface์˜ Tokenizers์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 15-01 ๋ฐ”์ดํŠธ ํŽ˜์–ด ์ธ์ฝ”๋”ฉ(Byte Pair Encoding, BPE) ๊ธฐ๊ณ„์—๊ฒŒ ์•„๋ฌด๋ฆฌ ๋งŽ์€ ๋‹จ์–ด๋ฅผ ํ•™์Šต์‹œ์ผœ๋„ ์„ธ์ƒ์˜ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์•Œ๋ ค์ค„ ์ˆ˜๋Š” ์—†๋Š” ๋…ธ๋ฆ‡์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ธฐ๊ณ„๊ฐ€ ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด ๊ทธ ๋‹จ์–ด๋ฅผ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์—†๋Š” ๋‹จ์–ด๋ž€ ์˜๋ฏธ์—์„œ ํ•ด๋‹น ํ† ํฐ์„ UNK(Unknown Token)๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๊ฐ€ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด (์‚ฌ๋žŒ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ง€๋งŒ) ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ๊นŒ๋‹ค๋กœ์›Œ์ง‘๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ๋ชจ๋ฅด๋Š” ๋‹จ์–ด๋กœ ์ธํ•ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ๊นŒ๋‹ค๋กœ์›Œ์ง€๋Š” ์ƒํ™ฉ์„ OOV(Out-Of-Vocabulary) ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ(Subword segmenation) ์ž‘์—…์€ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋Š” ๋” ์ž‘์€ ๋‹จ์œ„์˜ ์˜๋ฏธ ์žˆ๋Š” ์—ฌ๋Ÿฌ ์„œ๋ธŒ ์›Œ๋“œ๋“ค(Ex) birthplace = birth + place)์˜ ์กฐํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—, ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ ์—ฌ๋Ÿฌ ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ๋‹จ์–ด๋ฅผ ์ธ์ฝ”๋”ฉ ๋ฐ ์ž„๋ฒ ๋”ฉํ•˜๊ฒ ๋‹ค๋Š” ์˜๋„๋ฅผ ๊ฐ€์ง„ ์ „์ฒ˜๋ฆฌ ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด OOV๋‚˜ ํฌ๊ท€ ๋‹จ์–ด, ์‹ ์กฐ์–ด์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ์™„ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์–ธ์–ด์˜ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ์˜์–ด๋‚˜ ํ•œ๊ตญ์–ด๋Š” ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ๋ฅผ ์‹œ๋„ํ–ˆ์„ ๋•Œ ์–ด๋Š ์ •๋„ ์˜๋ฏธ ์žˆ๋Š” ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ๋Š” ์ด๋Ÿฐ ์ž‘์—…์„ ํ•˜๋Š” ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €๋ผ๊ณ  ๋ช…๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” OOV(Out-Of-Vocabulary) ๋ฌธ์ œ๋ฅผ ์™„ํ™”ํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ BPE(Byte Pair Encoding) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 1. BPE(Byte Pair Encoding) BPE(Byte pair encoding) ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ 1994๋…„์— ์ œ์•ˆ๋œ ๋ฐ์ดํ„ฐ ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ›„์— ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์˜ ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์‘์šฉ๋˜์—ˆ๋Š”๋ฐ ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์— ์–ธ๊ธ‰ํ•˜๋„๋ก ํ•˜๊ณ , ์šฐ์„  ๊ธฐ์กด์˜ BPE์˜ ์ž‘๋™ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ BPE์„ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. aaabdaaabac BPE์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์—ฐ์†์ ์œผ๋กœ ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•œ ๊ธ€์ž์˜ ์Œ์„ ์ฐพ์•„์„œ ํ•˜๋‚˜์˜ ๊ธ€์ž๋กœ ๋ณ‘ํ•ฉํ•˜๋Š” ๋ฐฉ์‹์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํƒœ์ƒ์ด ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๋งŒํผ, ์—ฌ๊ธฐ์„œ๋Š” ๊ธ€์ž ๋Œ€์‹  ๋ฐ”์ดํŠธ(byte)๋ผ๋Š” ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ์ž์ฃผ ๋“ฑ์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฐ”์ดํŠธ์˜ ์Œ(byte pair)์€ 'aa'์ž…๋‹ˆ๋‹ค. ์ด 'aa'๋ผ๋Š” ๋ฐ”์ดํŠธ์˜ ์Œ์„ ํ•˜๋‚˜์˜ ๋ฐ”์ดํŠธ์ธ 'Z'๋กœ ์น˜ํ™˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ZabdZabac Z=aa ์œ„ ๋ฌธ์ž์—ด ์ค‘์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฐ”์ดํŠธ์˜ ์Œ์€ 'ab'์ž…๋‹ˆ๋‹ค. ์ด 'ab'๋ฅผ 'Y'๋กœ ์น˜ํ™˜ํ•ด ๋ด…์‹œ๋‹ค. ZYdZYac Y=ab Z=aa ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฐ”์ดํŠธ์˜ ์Œ์€ 'ZY'์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ 'X'๋กœ ์น˜ํ™˜ํ•ด ๋ด…์‹œ๋‹ค. XdXac X=ZY Y=ab Z=aa ๋” ์ด์ƒ ๋ณ‘ํ•ฉํ•  ๋ฐ”์ดํŠธ์˜ ์Œ์€ ์—†์œผ๋ฏ€๋กœ BPE๋Š” ์œ„์˜ ๊ฒฐ๊ณผ๋ฅผ ์ตœ์ข… ๊ฒฐ๊ณผ๋กœ ํ•˜์—ฌ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. 2. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ์˜ BPE(Byte Pair Encoding) ๋…ผ๋ฌธ : https://arxiv.org/pdf/1508.07909.pdf ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ์˜ BPE๋Š” ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ(subword segmentation) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด์— ์žˆ๋˜ ๋‹จ์–ด๋ฅผ ๋ถ„๋ฆฌํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. BPE์„ ์š”์•ฝํ•˜๋ฉด, ๊ธ€์ž(charcter) ๋‹จ์œ„์—์„œ ์ ์ฐจ์ ์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ๋งŒ๋“ค์–ด ๋‚ด๋Š” Bottom up ๋ฐฉ์‹์˜ ์ ‘๊ทผ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ๋ชจ๋“  ๊ธ€์ž(chracters) ๋˜๋Š” ์œ ๋‹ˆ์ฝ”๋“œ(unicode) ๋‹จ์œ„๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)๋ฅผ ๋งŒ๋“ค๊ณ , ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•˜๋Š” ์œ ๋‹ˆ๊ทธ๋žจ์„ ํ•˜๋‚˜์˜ ์œ ๋‹ˆ๊ทธ๋žจ์œผ๋กœ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. BPE์„ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ œ์•ˆํ•œ ๋…ผ๋ฌธ(Sennrich et al. (2016))์—์„œ ์ด๋ฏธ BPE์˜ ์ฝ”๋“œ๋ฅผ ๊ณต๊ฐœํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ”๋กœ ํŒŒ์ด์ฌ ์‹ค์Šต์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— ์œก์•ˆ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ๊ธฐ์กด์˜ ์ ‘๊ทผ ์–ด๋–ค ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๊ฐ ๋‹จ์–ด๋“ค์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์นด์šดํŠธํ–ˆ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋‹จ์–ด์™€ ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ธฐ๋ก๋ผ ์žˆ๋Š” ํ•ด๋‹น ๊ฒฐ๊ณผ๋Š” ์ž„์˜๋กœ ๋”•์…”๋„ˆ๋ฆฌ(dictionary)๋ž€ ์ด๋ฆ„์„ ๋ถ™์˜€์Šต๋‹ˆ๋‹ค. # dictionary # ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ์žˆ๋Š” ๋‹จ์–ด์™€ ๋“ฑ์žฅ ๋นˆ๋„์ˆ˜ low : 5, lower : 2, newest : 6, widest : 3 ์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์—๋Š” 'low'๋ž€ ๋‹จ์–ด๊ฐ€ 5ํšŒ ๋“ฑ์žฅํ•˜์˜€๊ณ , 'lower'๋ž€ ๋‹จ์–ด๋Š” 2ํšŒ ๋“ฑ์žฅํ•˜์˜€์œผ๋ฉฐ, 'newest'๋ž€ ๋‹จ์–ด๋Š” 6ํšŒ, 'widest'๋ž€ ๋‹จ์–ด๋Š” 3ํšŒ ๋“ฑ์žฅํ•˜์˜€๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋”•์…”๋„ˆ๋ฆฌ๋กœ๋ถ€ํ„ฐ ์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ์–ป๋Š” ๊ฒƒ์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. # vocabulary low, lower, newest, widest ๋‹จ์–ด ์ง‘ํ•ฉ์€ ์ค‘๋ณต์„ ๋ฐฐ์ œํ•œ ๋‹จ์–ด๋“ค์˜ ์ง‘ํ•ฉ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ ๊ธฐ์กด์— ๋ฐฐ์šด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ์ •์˜๋ผ๋ฉด, ์ด ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์—๋Š” 'low', 'lower', 'newest', 'widest'๋ผ๋Š” 4๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฒฝ์šฐ ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ 'lowest'๋ž€ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค๋ฉด ๊ธฐ๊ณ„๋Š” ์ด ๋‹จ์–ด๋ฅผ ํ•™์Šตํ•œ ์ ์ด ์—†์œผ๋ฏ€๋กœ ํ•ด๋‹น ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ œ๋Œ€๋กœ ๋Œ€์‘ํ•˜์ง€ ๋ชปํ•˜๋Š” OOV ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด BPE๋ฅผ ์ ์šฉํ•œ๋‹ค๋ฉด ์–ด๋–จ๊นŒ์š”? 2) BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ์œ„์˜ ๋”•์…”๋„ˆ๋ฆฌ์— BPE๋ฅผ ์ ์šฉํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ๋‹จ์–ด๋“ค์„ ๊ธ€์ž(chracter) ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ž์‹  ๋˜ํ•œ ์—…๋ฐ์ดํŠธ๋˜๋ฉฐ ์•ž์œผ๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด ์ง€์†์ ์œผ๋กœ ์ฐธ๊ณ ๋˜๋Š” ์ฐธ๊ณ  ์ž๋ฃŒ์˜ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. # dictionary l o w : 5, l o w e r : 2, n e w e s t : 6, w i d e s t : 3 ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ฐธ๊ณ ๋กœ ํ•œ ์ดˆ๊ธฐ ๋‹จ์–ด ์ง‘ํ•ฉ(vocabulary)์„ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ๋งํ•ด ์ดˆ๊ธฐ ๊ตฌ์„ฑ์€ ๊ธ€์ž ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. # vocabulary l, o, w, e, r, n, s, t, i, d BPE์˜ ํŠน์ง•์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋™์ž‘์„ ๋ช‡ ํšŒ ๋ฐ˜๋ณต(iteration) ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์‚ฌ์šฉ์ž๊ฐ€ ์ •ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ด 10ํšŒ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ฐ€์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์Œ์„ ํ•˜๋‚˜์˜ ์œ ๋‹ˆ๊ทธ๋žจ์œผ๋กœ ํ†ตํ•ฉํ•˜๋Š” ๊ณผ์ •์„ ์ด 10ํšŒ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๋”•์…”๋„ˆ๋ฆฌ์— ๋”ฐ๋ฅด๋ฉด ๋นˆ๋„์ˆ˜๊ฐ€ ํ˜„์žฌ ๊ฐ€์žฅ ๋†’์€ ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์Œ์€ (e, s)์ž…๋‹ˆ๋‹ค. 1ํšŒ - ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ฐธ๊ณ ๋กœ ํ•˜์˜€์„ ๋•Œ ๋นˆ๋„์ˆ˜๊ฐ€ 9๋กœ ๊ฐ€์žฅ ๋†’์€ (e, s)์˜ ์Œ์„ es๋กœ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. # dictionary update! l o w : 5, l o w e r : 2, n e w es t : 6, w i d es t : 3 # vocabulary update! l, o, w, e, r, n, s, t, i, d, es 2ํšŒ - ๋นˆ๋„์ˆ˜๊ฐ€ 9๋กœ ๊ฐ€์žฅ ๋†’์€ (es, t)์˜ ์Œ์„ est๋กœ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. # dictionary update! l o w : 5, l o w e r : 2, n e w est : 6, w i d est : 3 # vocabulary update! l, o, w, e, r, n, s, t, i, d, es, est 3ํšŒ - ๋นˆ๋„์ˆ˜๊ฐ€ 7๋กœ ๊ฐ€์žฅ ๋†’์€ (l, o)์˜ ์Œ์„ lo๋กœ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. # dictionary update! lo w : 5, lo w e r : 2, n e w est : 6, w i d est : 3 # vocabulary update! l, o, w, e, r, n, s, t, i, d, es, est, lo ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ด 10ํšŒ ๋ฐ˜๋ณตํ•˜์˜€์„ ๋•Œ ์–ป์€ ๋”•์…”๋„ˆ๋ฆฌ์™€ ๋‹จ์–ด ์ง‘ํ•ฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. # dictionary update! low : 5, low e r : 2, newest : 6, widest : 3 # vocabulary update! l, o, w, e, r, n, s, t, i, d, es, est, lo, low, ne, new, newest, wi, wid, widest ์ด ๊ฒฝ์šฐ ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ 'lowest'๋ž€ ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•œ๋‹ค๋ฉด, ๊ธฐ์กด์—๋Š” OOV์— ํ•ด๋‹น๋˜๋Š” ๋‹จ์–ด๊ฐ€ ๋˜์—ˆ๊ฒ ์ง€๋งŒ BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ ์œ„์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ๋Š” ๋” ์ด์ƒ 'lowest'๋Š” OOV๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ์šฐ์„  'lowest'๋ฅผ ์ „๋ถ€ ๊ธ€์ž ๋‹จ์œ„๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, 'l, o, w, e, s, t'๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ธฐ๊ณ„๋Š” ์œ„์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ฐธ๊ณ ๋กœ ํ•˜์—ฌ 'low'์™€ 'est'๋ฅผ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค. ์ฆ‰, 'lowest'๋ฅผ ๊ธฐ๊ณ„๋Š” 'low'์™€ 'est' ๋‘ ๋‹จ์–ด๋กœ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋‘ ๋‹จ์–ด๋Š” ๋‘˜ ๋‹ค ๋‹จ์–ด ์ง‘ํ•ฉ์— ์žˆ๋Š” ๋‹จ์–ด์ด๋ฏ€๋กœ OOV๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์ด ๋™์ž‘ ๊ณผ์ •์„ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 3) ์ฝ”๋“œ ์‹ค์Šตํ•˜๊ธฐ ์•„๋ž˜ ์ฝ”๋“œ๋Š” ์› ๋…ผ๋ฌธ์—์„œ ๊ณต๊ฐœํ•œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ๋กœ ํ•˜์—ฌ ์ˆ˜์ •ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ํ•„์š”ํ•œ ๋„๊ตฌ๋“ค์„ ์ž„ํฌํŠธ ํ•ฉ๋‹ˆ๋‹ค. import re, collections from IPython.display import display, Markdown, Latex BPE์„ ๋ช‡ ํšŒ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 10ํšŒ๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. num_merges = 10 BPE์— ์‚ฌ์šฉํ•  ๋‹จ์–ด๊ฐ€ low, lower, newest, widest ์ผ ๋•Œ, BPE์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์‹ค์ œ ๋‹จ์–ด ์ง‘ํ•ฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. </w>๋Š” ๋‹จ์–ด์˜ ๋งจ ๋์— ๋ถ™์ด๋Š” ํŠน์ˆ˜ ๋ฌธ์ž์ด๋ฉฐ, ๊ฐ ๋‹จ์–ด๋Š” ๊ธ€์ž(character) ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. dictionary = {'l o w </w>' : 5, 'l o w e r </w>' : 2, 'n e w e s t </w>':6, 'w i d e s t </w>':3 } BPE์˜ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์œ„์—์„œ ์„ค๋ช…ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ๊ฐ€์žฅ ๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์Œ์„ ํ•˜๋‚˜์˜ ์œ ๋‹ˆ๊ทธ๋žจ์œผ๋กœ ํ†ตํ•ฉํ•˜๋Š” ๊ณผ์ •์œผ๋กœ num_merges ํšŒ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. def get_stats(dictionary): # ์œ ๋‹ˆ๊ทธ๋žจ์˜ pair๋“ค์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์นด์šดํŠธ pairs = collections.defaultdict(int) for word, freq in dictionary.items(): symbols = word.split() for i in range(len(symbols)-1): pairs[symbols[i],symbols[i+1]] += freq print('ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ :', dict(pairs)) return pairs def merge_dictionary(pair, v_in): v_out = {} bigram = re.escape(' '.join(pair)) p = re.compile(r'(?<!\S)' + bigram + r'(?!\S)') for word in v_in: w_out = p.sub(''.join(pair), word) v_out[w_out] = v_in[word] return v_out bpe_codes = {} bpe_codes_reverse = {} for i in range(num_merges): display(Markdown("### Iteration {}".format(i + 1))) pairs = get_stats(dictionary) best = max(pairs, key=pairs.get) dictionary = merge_dictionary(best, dictionary) bpe_codes[best] = i bpe_codes_reverse[best[0] + best[1]] = best print("new merge: {}".format(best)) print("dictionary: {}".format(dictionary)) ์ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์œผ๋ฉฐ ์ด๋Š” ๊ธ€์ž๋“ค์˜ ํ†ตํ•ฉ ๊ณผ์ •์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Iteration 1 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('l', 'o'): 7, ('o', 'w'): 7, ('w', '</w>'): 5, ('w', 'e'): 8, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('e', 's'): 9, ('s', 't'): 9, ('t', '</w>'): 9, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'e'): 3} new merge: ('e', 's') dictionary: {'l o w </w>': 5, 'l o w e r </w>': 2, 'n e w es t </w>': 6, 'w i d es t </w>': 3} Iteration 2 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('l', 'o'): 7, ('o', 'w'): 7, ('w', '</w>'): 5, ('w', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('w', 'es'): 6, ('es', 't'): 9, ('t', '</w>'): 9, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'es'): 3} new merge: ('es', 't') dictionary: {'l o w </w>': 5, 'l o w e r </w>': 2, 'n e w est </w>': 6, 'w i d est </w>': 3} Iteration 3 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('l', 'o'): 7, ('o', 'w'): 7, ('w', '</w>'): 5, ('w', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('w', 'est'): 6, ('est', '</w>'): 9, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est'): 3} new merge: ('est', '</w>') dictionary: {'l o w </w>': 5, 'l o w e r </w>': 2, 'n e w est</w>': 6, 'w i d est</w>': 3} Iteration 4 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('l', 'o'): 7, ('o', 'w'): 7, ('w', '</w>'): 5, ('w', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('w', 'est</w>'): 6, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('l', 'o') dictionary: {'lo w </w>': 5, 'lo w e r </w>': 2, 'n e w est</w>': 6, 'w i d est</w>': 3} Iteration 5 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('lo', 'w'): 7, ('w', '</w>'): 5, ('w', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('w', 'est</w>'): 6, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('lo', 'w') dictionary: {'low </w>': 5, 'low e r </w>': 2, 'n e w est</w>': 6, 'w i d est</w>': 3} Iteration 6 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('low', '</w>'): 5, ('low', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('n', 'e'): 6, ('e', 'w'): 6, ('w', 'est</w>'): 6, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('n', 'e') dictionary: {'low </w>': 5, 'low e r </w>': 2, 'ne w est</w>': 6, 'w i d est</w>': 3} Iteration 7 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('low', '</w>'): 5, ('low', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('ne', 'w'): 6, ('w', 'est</w>'): 6, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('ne', 'w') dictionary: {'low </w>': 5, 'low e r </w>': 2, 'new est</w>': 6, 'w i d est</w>': 3} Iteration 8 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('low', '</w>'): 5, ('low', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('new', 'est</w>'): 6, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('new', 'est</w>') dictionary: {'low </w>': 5, 'low e r </w>': 2, 'newest</w>': 6, 'w i d est</w>': 3} Iteration 9 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('low', '</w>'): 5, ('low', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('low', '</w>') dictionary: {'low</w>': 5, 'low e r </w>': 2, 'newest</w>': 6, 'w i d est</w>': 3} Iteration 10 ํ˜„์žฌ pair๋“ค์˜ ๋นˆ๋„์ˆ˜ : {('low', 'e'): 2, ('e', 'r'): 2, ('r', '</w>'): 2, ('w', 'i'): 3, ('i', 'd'): 3, ('d', 'est</w>'): 3} new merge: ('w', 'i') dictionary: {'low</w>': 5, 'low e r </w>': 2, 'newest</w>': 6, 'wi d est</w>': 3} e์™€ s์˜ ์Œ์€ ์ดˆ๊ธฐ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์ด 9ํšŒ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— es๋กœ ํ†ตํ•ฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์œผ๋กœ๋Š” es์™€ t์˜ ์Œ์„, ๊ทธ๋‹ค์Œ์œผ๋กœ๋Š” est์™€ </w>์˜ ์Œ์„ ํ†ตํ•ฉ์‹œํ‚ต๋‹ˆ๋‹ค. ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ํ†ตํ•ฉํ•˜๋Š” ์ด ๊ณผ์ •์„ ์ด num_merges ํšŒ ๋ฐ˜๋ณตํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. bpe_codes๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด merge ํ–ˆ๋˜ ๊ธฐ๋ก์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. print(bpe_codes) {('e', 's'): 0, ('es', 't'): 1, ('est', '</w>'): 2, ('l', 'o'): 3, ('lo', 'w'): 4, ('n', 'e'): 5, ('ne', 'w'): 6, ('new', 'est</w>'): 7, ('low', '</w>'): 8, ('w', 'i'): 9} ์ด ๊ธฐ๋ก์€ ์ƒˆ๋กœ์šด ๋‹จ์–ด๊ฐ€ ๋“ฑ์žฅํ•˜์˜€์„ ๋•Œ, ํ˜„์žฌ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„œ๋ธŒ ์›Œ๋“œ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์˜๊ฑฐํ•˜์—ฌ ๋ถ„๋ฆฌํ•˜๋Š” ์ผ์— ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 4) OOV์— ๋Œ€์ฒ˜ํ•˜๊ธฐ def get_pairs(word): """Return set of symbol pairs in a word. Word is represented as a tuple of symbols (symbols being variable-length strings). """ pairs = set() prev_char = word[0] for char in word[1:]: pairs.add((prev_char, char)) prev_char = char return pairs def encode(orig): """Encode word based on list of BPE merge operations, which are applied consecutively""" word = tuple(orig) + ('</w>',) display(Markdown("__word split into characters:__ <tt>{}</tt>".format(word))) pairs = get_pairs(word) if not pairs: return orig iteration = 0 while True: iteration += 1 display(Markdown("__Iteration {}:__".format(iteration))) print("bigrams in the word: {}".format(pairs)) bigram = min(pairs, key = lambda pair: bpe_codes.get(pair, float('inf'))) print("candidate for merging: {}".format(bigram)) if bigram not in bpe_codes: display(Markdown("__Candidate not in BPE merges, algorithm stops.__")) break first, second = bigram new_word = [] i = 0 while i < len(word): try: j = word.index(first, i) new_word.extend(word[i:j]) i = j except: new_word.extend(word[i:]) break if word[i] == first and i < len(word)-1 and word[i+1] == second: new_word.append(first+second) i += 2 else: new_word.append(word[i]) i += 1 new_word = tuple(new_word) word = new_word print("word after merging: {}".format(word)) if len(word) == 1: break else: pairs = get_pairs(word) # ํŠน๋ณ„ ํ† ํฐ์ธ </w>๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. if word[-1] == '</w>': word = word[:-1] elif word[-1].endswith('</w>'): word = word[:-1] + (word[-1].replace('</w>',''),) return word ๋‹จ์–ด 'loki'๊ฐ€ ๋“ค์–ด์˜ค๋ฉด BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ์–ด๋–ป๊ฒŒ ๋ถ„๋ฆฌํ• ๊นŒ์š”? encode("loki") word split into characters: ('l', 'o', 'k', 'i', '') Iteration 1: bigrams in the word: {('i', '</w>'), ('o', 'k'), ('l', 'o'), ('k', 'i')} candidate for merging: ('l', 'o') word after merging: ('lo', 'k', 'i', '</w>') Iteration 2: bigrams in the word: {('i', '</w>'), ('k', 'i'), ('lo', 'k')} candidate for merging: ('i', '</w>') Candidate not in BPE merges, algorithm stops. ('lo', 'k', 'i') ํ˜„์žฌ ์„œ๋ธŒ ์›Œ๋“œ ๋‹จ์–ด ์ง‘ํ•ฉ์—๋Š” 'lo'๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ, 'lo'๋Š” ์œ ์ง€ํ•˜๊ณ  'k'์™€ 'i'๋Š” ๋ถ„๋ฆฌ์‹œํ‚ต๋‹ˆ๋‹ค. ๋‹จ์–ด 'lowest'์— ๋Œ€ํ•ด์„œ๋„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. encode("lowest") word split into characters: ('l', 'o', 'w', 'e', 's', 't', '') Iteration 1: bigrams in the word: {('e', 's'), ('s', 't'), ('t', '</w>'), ('o', 'w'), ('w', 'e'), ('l', 'o')} candidate for merging: ('e', 's') word after merging: ('l', 'o', 'w', 'es', 't', '</w>') Iteration 2: bigrams in the word: {('w', 'es'), ('es', 't'), ('t', '</w>'), ('o', 'w'), ('l', 'o')} candidate for merging: ('es', 't') word after merging: ('l', 'o', 'w', 'est', '</w>') Iteration 3: bigrams in the word: {('o', 'w'), ('l', 'o'), ('est', '</w>'), ('w', 'est')} candidate for merging: ('est', '</w>') word after merging: ('l', 'o', 'w', 'est</w>') Iteration 4: bigrams in the word: {('o', 'w'), ('l', 'o'), ('w', 'est</w>')} candidate for merging: ('l', 'o') word after merging: ('lo', 'w', 'est</w>') Iteration 5: bigrams in the word: {('lo', 'w'), ('w', 'est</w>')} candidate for merging: ('lo', 'w') word after merging: ('low', 'est</w>') Iteration 6: bigrams in the word: {('low', 'est</w>')} candidate for merging: ('low', 'est</w>') Candidate not in BPE merges, algorithm stops. ('low', 'est') ํ˜„์žฌ ์„œ๋ธŒ ์›Œ๋“œ ๋‹จ์–ด ์ง‘ํ•ฉ์— 'low'์™€ 'est'๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ, 'low'์™€ 'est'๋ฅผ ๋ถ„๋ฆฌ์‹œํ‚ต๋‹ˆ๋‹ค. ๋‹จ์–ด 'lowing'์— ๋Œ€ํ•ด์„œ๋„ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. encode("lowing") word split into characters: ('l', 'o', 'w', 'i', 'n', 'g', '') Iteration 1: bigrams in the word: {('n', 'g'), ('w', 'i'), ('g', '</w>'), ('i', 'n'), ('o', 'w'), ('l', 'o')} candidate for merging: ('l', 'o') word after merging: ('lo', 'w', 'i', 'n', 'g', '</w>') Iteration 2: bigrams in the word: {('lo', 'w'), ('n', 'g'), ('w', 'i'), ('g', '</w>'), ('i', 'n')} candidate for merging: ('lo', 'w') word after merging: ('low', 'i', 'n', 'g', '</w>') Iteration 3: bigrams in the word: {('n', 'g'), ('g', '</w>'), ('i', 'n'), ('low', 'i')} candidate for merging: ('n', 'g') Candidate not in BPE merges, algorithm stops. ('low', 'i', 'n', 'g') ํ˜„์žฌ ์„œ๋ธŒ ์›Œ๋“œ ๋‹จ์–ด ์ง‘ํ•ฉ์— 'low'๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ, 'i', 'n', 'g'์˜ ๋ฐ”์ด ๊ทธ๋žจ ์กฐํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์„œ๋ธŒ ์›Œ๋“œ๋Š” ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ 'i', 'n', 'g'๋กœ ์ „๋ถ€ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ๋œ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์–ด๋–ค ์„œ๋ธŒ ์›Œ๋“œ๋„ ์กด์žฌํ•˜์ง€ ์•Š๋Š” 'highing'์€ ์–ด๋–จ๊นŒ์š”? encode("highing") word split into characters: ('h', 'i', 'g', 'h', 'i', 'n', 'g', '') Iteration 1: bigrams in the word: {('n', 'g'), ('g', 'h'), ('h', 'i'), ('g', '</w>'), ('i', 'n'), ('i', 'g')} candidate for merging: ('n', 'g') Candidate not in BPE merges, algorithm stops. ('h', 'i', 'g', 'h', 'i', 'n', 'g') ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์ด ๋ถ„๋ฆฌ๋ฉ๋‹ˆ๋‹ค. BPE ์™ธ์—๋„ BPE๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง„ Wordpiece Tokenizer๋‚˜ Unigram Language Model Tokenizer์™€ ๊ฐ™์€ ์„œ๋ธŒ ์›Œ๋“œ ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ๋Š” ๊ฐ„๋žตํžˆ ์ด๋Ÿฐ ๊ฒƒ๋“ค์ด ์กด์žฌํ•œ๋‹ค ์ •๋„๋กœ๋งŒ ์–ธ๊ธ‰ํ•˜๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. 3. WordPiece Tokenizer ๋…ผ๋ฌธ : https://static.googleusercontent.com/media/research.google.com/ko//pubs/archive/37842.pdf ๊ตฌ๊ธ€์ด ์œ„ WordPiece Tokenizer๋ฅผ ๋ณ€ํ˜•ํ•˜์—ฌ ๋ฒˆ์—ญ๊ธฐ์— ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ๋…ผ๋ฌธ : https://arxiv.org/pdf/1609.08144.pdf WordPiece Tokenizer์€ BPE์˜ ๋ณ€ํ˜• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ BPE๊ฐ€ ๋นˆ๋„์ˆ˜์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•œ ์Œ์„ ๋ณ‘ํ•ฉํ•˜๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ, ๋ณ‘ํ•ฉ๋˜์—ˆ์„ ๋•Œ ์ฝ”ํผ์Šค์˜ ์šฐ๋„(Likelihood)๋ฅผ ๊ฐ€์žฅ ๋†’์ด๋Š” ์Œ์„ ๋ณ‘ํ•ฉํ•ฉ๋‹ˆ๋‹ค. 2016๋…„์˜ ์œ„ ๋…ผ๋ฌธ์—์„œ ๊ตฌ๊ธ€์€ ๊ตฌ๊ธ€ ๋ฒˆ์—ญ๊ธฐ์—์„œ WordPiece Tokenizer๊ฐ€ ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ ๊ธฐ์ˆ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ด์ „์˜ ๋ฌธ์žฅ: Jet makers feud over seat width with big orders at stake WordPiece Tokenizer๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ(wordpieces): _J et _makers _fe ud _over _seat _width _with _big _orders _at _stake Jet๋Š” J์™€ et๋กœ ๋‚˜๋ˆ„์–ด์กŒ์œผ๋ฉฐ, feud๋Š” fe์™€ ud๋กœ ๋‚˜๋ˆ„์–ด์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. WordPiece Tokenizer๋Š” ๋ชจ๋“  ๋‹จ์–ด์˜ ๋งจ ์•ž์— _๋ฅผ ๋ถ™์ด๊ณ , ๋‹จ์–ด๋Š” ์„œ๋ธŒ ์›Œ๋“œ(subword)๋กœ ํ†ต๊ณ„์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๋„์–ด์“ฐ๊ธฐ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์–ธ๋”๋ฐ” _๋Š” ๋ฌธ์žฅ ๋ณต์›์„ ์œ„ํ•œ ์žฅ์น˜์ž…๋‹ˆ๋‹ค. ์˜ˆ์ปจ๋Œ€, WordPiece Tokenizer์˜ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ๋ฌธ์žฅ์„ ๋ณด๋ฉด, Jet โ†’ _J et์™€ ๊ฐ™์ด ๊ธฐ์กด์— ์—†๋˜ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ์„œ๋ธŒ ์›Œ๋“œ(subwords)๋“ค์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ตฌ๋ถ„์ž ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ธฐ์กด์— ์žˆ๋˜ ๋„์–ด์“ฐ๊ธฐ์™€ ๊ตฌ๋ถ„์ž ์—ญํ• ์˜ ๋„์–ด์“ฐ๊ธฐ๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌ๋ณ„ํ• ๊นŒ์š”? ์ด ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋‹จ์–ด๋“ค ์•ž์— ๋ถ™์€ ์–ธ๋”๋ฐ” _์ž…๋‹ˆ๋‹ค. WordPiece Tokenizer์ด ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ๋กœ๋ถ€ํ„ฐ ๋‹ค์‹œ ์ˆ˜ํ–‰ ์ „์˜ ๊ฒฐ๊ณผ๋กœ ๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์€ ํ˜„์žฌ ์žˆ๋Š” ๋ชจ๋“  ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ „๋ถ€ ์ œ๊ฑฐํ•˜๊ณ , ์–ธ๋”๋ฐ”๋ฅผ ๋„์–ด์“ฐ๊ธฐ๋กœ ๋ฐ”๊พธ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์œ ๋ช… ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ BERT๋ฅผ ํ›ˆ๋ จํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 4. Unigram Language Model Tokenizer ๋…ผ๋ฌธ : https://arxiv.org/pdf/1804.10959.pdf ์œ ๋‹ˆ๊ทธ๋žจ ์–ธ์–ด ๋ชจ๋ธ ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ฐ๊ฐ์˜ ์„œ๋ธŒ ์›Œ๋“œ๋“ค์— ๋Œ€ํ•ด์„œ ์†์‹ค(loss)์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์„œ๋ธŒ ๋‹จ์–ด์˜ ์†์‹ค์ด๋ผ๋Š” ๊ฒƒ์€ ํ•ด๋‹น ์„œ๋ธŒ ์›Œ๋“œ๊ฐ€ ๋‹จ์–ด ์ง‘ํ•ฉ์—์„œ ์ œ๊ฑฐ๋˜์—ˆ์„ ๊ฒฝ์šฐ, ์ฝ”ํผ์Šค์˜ ์šฐ๋„(Likelihood)๊ฐ€ ๊ฐ์†Œํ•˜๋Š” ์ •๋„๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ธก์ •๋œ ์„œ๋ธŒ ์›Œ๋“œ๋“ค์„ ์†์‹ค์˜ ์ •๋„๋กœ ์ •๋ ฌํ•˜์—ฌ, ์ตœ์•…์˜ ์˜ํ–ฅ์„ ์ฃผ๋Š” 10~20%์˜ ํ† ํฐ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์›ํ•˜๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ๋‚˜์ด์ง• ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด์–ด์„œ ์ด๋ฅผ ์‹ค๋ฌด์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํŒจํ‚ค์ง€์ธ ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece)๋‚˜ ํ† ํฌ ๋‚˜์ด ์ €์Šค(tokenizers)์˜ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 15-02 ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece) ์•ž์„œ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฐํ™”๋ฅผ ์œ„ํ•œ BPE(Byte Pair Encoding) ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ทธ ์™ธ BPE์˜ ๋ณ€ํ˜• ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์–ธ๊ธ‰ํ–ˆ์Šต๋‹ˆ๋‹ค. BPE๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ธฐํƒ€ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ๋‚˜์ด์ง• ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์„ ๋‚ด์žฅํ•œ ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece)๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์‹ค๋ฌด์—์„œ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์„ ์˜ ์„ ํƒ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. 1. ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece) ๋…ผ๋ฌธ : https://arxiv.org/pdf/1808.06226.pdf ์„ผํ…์Šค ํ”ผ์Šค ๊นƒํ—ˆ๋ธŒ : https://github.com/google/sentencepiece ๋‚ด๋ถ€ ๋‹จ์–ด ๋ถ„๋ฆฌ๋ฅผ ์œ„ํ•œ ์œ ์šฉํ•œ ํŒจํ‚ค์ง€๋กœ ๊ตฌ๊ธ€์˜ ์„ผํ…์Šค ํ”ผ์Šค(Sentencepiece)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ๊ธ€์€ BPE ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ Unigram Language Model Tokenizer๋ฅผ ๊ตฌํ˜„ํ•œ ์„ผํ…์Šค ํ”ผ์Šค๋ฅผ ๊นƒํ—ˆ๋ธŒ์— ๊ณต๊ฐœํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ๋‹จ์–ด ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ, ๋ฐ์ดํ„ฐ์— ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ๋จผ์ € ์ง„ํ–‰ํ•œ ์ƒํƒœ์—ฌ์•ผ ํ•œ๋‹ค๋ฉด ์ด ๋‹จ์–ด ๋ถ„๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ชจ๋“  ์–ธ์–ด์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜์–ด์™€ ๋‹ฌ๋ฆฌ ํ•œ๊ตญ์–ด์™€ ๊ฐ™์€ ์–ธ์–ด๋Š” ๋‹จ์–ด ํ† ํฐํ™”๋ถ€ํ„ฐ๊ฐ€ ์‰ฝ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ์ด๋Ÿฐ ์‚ฌ์ „ ํ† ํฐํ™” ์ž‘์—…(pretokenization) ์—†์ด ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ(raw data)์— ๋ฐ”๋กœ ๋‹จ์–ด ๋ถ„๋ฆฌ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์ด ํ† ํฌ ๋‚˜์ด์ €๋Š” ๊ทธ ์–ด๋–ค ์–ธ์–ด์—๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์„ผํ…์Šค ํ”ผ์Šค๋Š” ์ด ์ด์ ์„ ์‚ด๋ ค์„œ ๊ตฌํ˜„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์„ผํ…์Šค ํ”ผ์Šค๋Š” ์‚ฌ์ „ ํ† ํฐํ™” ์ž‘์—… ์—†์ด ๋‹จ์–ด ๋ถ„๋ฆฌ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์–ธ์–ด์— ์ข…์†๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. pip install sentencepiece 2. IMDB ๋ฆฌ๋ทฐ ํ† ํฐํ™”ํ•˜๊ธฐ import sentencepiece as spm import pandas as pd import urllib.request import csv IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์ด๋ฅผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/LawrenceDuan/IMDb-Review-Analysis/master/IMDb_Reviews.csv", filename="IMDb_Reviews.csv") train_df = pd.read_csv('IMDb_Reviews.csv') train_df['review'] 0 My family and I normally do not watch local mo... 1 Believe it or not, this was at one time the wo... 2 After some internet surfing, I found the "Home... 3 One of the most unheralded great works of anim... 4 It was the Sixties, and anyone with long hair ... ... 49995 the people who came up with this are SICK AND ... 49996 The script is so so laughable... this in turn,... 49997 "So there's this bride, you see, and she gets ... 49998 Your mind will not be satisfied by this noย—bud... 49999 The chaser's war on everything is a weekly sho... Name: review, Length: 50000, dtype: object print('๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(train_df)) # ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 50000 ์ด 5๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์„ผํ…์Šค ํ”ผ์Šค์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ txt ํŒŒ์ผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. with open('imdb_review.txt', 'w', encoding='utf8') as f: f.write('\n'.join(train_df['review'])) ์„ผํ…์Šค ํ”ผ์Šค๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ๊ณผ ๊ฐ ๋‹จ์–ด์— ๊ณ ์œ ํ•œ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. spm.SentencePieceTrainer.Train('--input=imdb_review.txt --model_prefix=imdb --vocab_size=5000 --model_type=bpe --max_sentence_length=9999') ๊ฐ ์ธ์ž๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. input : ํ•™์Šต์‹œํ‚ฌ ํŒŒ์ผ model_prefix : ๋งŒ๋“ค์–ด์งˆ ๋ชจ๋ธ ์ด๋ฆ„ vocab_size : ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ model_type : ์‚ฌ์šฉํ•  ๋ชจ๋ธ (unigram(default), bpe, char, word) max_sentence_length: ๋ฌธ์žฅ์˜ ์ตœ๋Œ€ ๊ธธ์ด pad_id, pad_piece: pad token id, ๊ฐ’ unk_id, unk_piece: unknown token id, ๊ฐ’ bos_id, bos_piece: begin of sentence token id, ๊ฐ’ eos_id, eos_piece: end of sequence token id, ๊ฐ’ user_defined_symbols: ์‚ฌ์šฉ์ž ์ •์˜ ํ† ํฐ vocab ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด imdb.model, imdb.vocab ํŒŒ์ผ ๋‘ ๊ฐœ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. vocab ํŒŒ์ผ์—์„œ ํ•™์Šต๋œ ์„œ๋ธŒ ์›Œ๋“œ๋“ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด vocab ํŒŒ์ผ์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ด ๋ด…์‹œ๋‹ค. vocab_list = pd.read_csv('imdb.vocab', sep='\t', header=None, quoting=csv.QUOTE_NONE) vocab_list.sample(10) ์œ„์—์„œ vocab_size์˜ ์ธ์ž๋ฅผ ํ†ตํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ 5,000๊ฐœ๋กœ ์ œํ•œํ•˜์˜€์œผ๋ฏ€๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” 5,000๊ฐœ์ž…๋‹ˆ๋‹ค. len(vocab_list) 5000 model ํŒŒ์ผ์„ ๋กœ๋“œํ•˜์—ฌ ๋‹จ์–ด ์‹œํ€€์Šค๋ฅผ ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ฐ”๊พธ๋Š” ์ธ์ฝ”๋”ฉ ์ž‘์—…์ด๋‚˜ ๋ฐ˜๋Œ€๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋””์ฝ”๋”ฉ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. sp = spm.SentencePieceProcessor() vocab_file = "imdb.model" sp.load(vocab_file) True ์•„๋ž˜์˜ ๋‘ ๊ฐ€์ง€ ๋„๊ตฌ๋ฅผ ํ…Œ์ŠคํŠธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. encode_as_pieces : ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด ์„œ๋ธŒ ์›Œ๋“œ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. encode_as_ids : ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. lines = [ "I didn't at all think of it this way.", "I have waited a long time for someone to film" ] for line in lines: print(line) print(sp.encode_as_pieces(line)) print(sp.encode_as_ids(line)) print() I didn't at all think of it this way. ['โ– I', 'โ– didn', "'", 't', 'โ– at', 'โ– all', 'โ– think', 'โ– of', 'โ– it', 'โ– this', 'โ– way', '.'] [41, 623, 4950, 4926, 138, 169, 378, 30, 58, 73, 413, 4945] I have waited a long time for someone to film ['โ– I', 'โ– have', 'โ– wa', 'ited', 'โ– a', 'โ– long', 'โ– time', 'โ– for', 'โ– someone', 'โ– to', 'โ– film'] [41, 141, 1364, 1120, 4, 666, 285, 92, 1078, 33, 91] GetPieceSize() : ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. sp.GetPieceSize() 5000 idToPiece : ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋งคํ•‘๋˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.IdToPiece(430) โ– character PieceToId : ์„œ๋ธŒ ์›Œ๋“œ๋กœ๋ถ€ํ„ฐ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.PieceToId('โ– character') 430 DecodeIds : ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.DecodeIds([41, 141, 1364, 1120, 4, 666, 285, 92, 1078, 33, 91]) DecodePieces : ์„œ๋ธŒ ์›Œ๋“œ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. I have waited a long time for someone to film sp.DecodePieces(['โ– I', 'โ– have', 'โ– wa', 'ited', 'โ– a', 'โ– long', 'โ– time', 'โ– for', 'โ– someone', 'โ– to', 'โ– film']) encode : ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์ธ์ž ๊ฐ’์— ๋”ฐ๋ผ์„œ ์ •์ˆ˜ ์‹œํ€€์Šค ๋˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. I have waited a long time for someone to film print(sp.encode('I have waited a long time for someone to film', out_type=str)) print(sp.encode('I have waited a long time for someone to film', out_type=int)) ['โ– I', 'โ– have', 'โ– wa', 'ited', 'โ– a', 'โ– long', 'โ– time', 'โ– for', 'โ– someone', 'โ– to', 'โ– film'] [41, 141, 1364, 1120, 4, 666, 285, 92, 1078, 33, 91] 3. ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ํ† ํฐํ™”ํ•˜๊ธฐ ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์œ„์˜ IMDB ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์™€ ๋™์ผํ•œ ๊ณผ์ •์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import pandas as pd import sentencepiece as spm import urllib.request import csv ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings.txt", filename="ratings.txt") naver_df = pd.read_table('ratings.txt') naver_df[:5] ์ด 20๋งŒ ๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. print('๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(naver_df)) # ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 200000 ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ Null ๊ฐ’์ด ์กด์žฌํ•˜๋ฏ€๋กœ ์ด๋ฅผ ์ œ๊ฑฐํ•œ ํ›„์— ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. print(naver_df.isnull().values.any()) True naver_df = naver_df.dropna(how = 'any') # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ํ–‰ ์ œ๊ฑฐ print(naver_df.isnull().values.any()) # Null ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ False print('๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ :',len(naver_df)) # ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ : 199992 ์ตœ์ข…์ ์œผ๋กœ 199,992๊ฐœ์˜ ์ƒ˜ํ”Œ์„ naver_review.txt ํŒŒ์ผ์— ์ €์žฅํ•œ ํ›„์— ์„ผํ…์Šค ํ”ผ์Šค๋ฅผ ํ†ตํ•ด ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. with open('naver_review.txt', 'w', encoding='utf8') as f: f.write('\n'.join(naver_df['document'])) spm.SentencePieceTrainer.Train('--input=naver_review.txt --model_prefix=naver --vocab_size=5000 --model_type=bpe --max_sentence_length=9999') vocab ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด naver.model, naver.vocab ํŒŒ์ผ ๋‘ ๊ฐœ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. .vocab์—์„œ ํ•™์Šต๋œ subwords๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. vocab_list = pd.read_csv('naver.vocab', sep='\t', header=None, quoting=csv.QUOTE_NONE) vocab_list[:10] vocab_list.sample(10) Vocabulary์—๋Š” unknown, ๋ฌธ์žฅ์˜ ์‹œ์ž‘, ๋ฌธ์žฅ์˜ ๋์„ ์˜๋ฏธํ•˜๋Š” special token์ด 0, 1, 2์— ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. len(vocab_list) 5000 ์„ค์ •ํ•œ ๋Œ€๋กœ 5000๊ฐœ์˜ ์„œ๋ธŒ ์›Œ๋“œ๊ฐ€ ๋‹จ์–ด ์ง‘ํ•ฉ์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. sp = spm.SentencePieceProcessor() vocab_file = "naver.model" sp.load(vocab_file) True lines = [ "๋ญ ์ด๋”ด ๊ฒƒ๋„ ์˜ํ™”๋ƒ.", "์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹", ] for line in lines: print(line) print(sp.encode_as_pieces(line)) print(sp.encode_as_ids(line)) print() ๋ญ ์ด๋”ด ๊ฒƒ๋„ ์˜ํ™”๋ƒ. ['โ–๋ญ', 'โ–์ด๋”ด', 'โ–๊ฒƒ๋„', 'โ–์˜ํ™”๋ƒ', '.'] [132, 966, 1296, 2590, 3276] ์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹ ['โ–์ง„์งœ', 'โ–์ตœ๊ณ ์˜', 'โ–์˜ํ™”์ž…๋‹ˆ๋‹ค', 'โ–แ„แ„'] [54, 200, 821, 85] GetPieceSize() : ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. sp.GetPieceSize() 5000 idToPiece : ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋งคํ•‘๋˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.IdToPiece(4) '์˜ํ™”' PieceToId : ์„œ๋ธŒ ์›Œ๋“œ๋กœ๋ถ€ํ„ฐ ๋งคํ•‘๋˜๋Š” ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.PieceToId('์˜ํ™”') DecodeIds : ์ •์ˆ˜ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.DecodeIds([54, 200, 821, 85]) ์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค แ„แ„ DecodePieces : ์„œ๋ธŒ ์›Œ๋“œ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sp.DecodePieces(['โ–์ง„์งœ', 'โ–์ตœ๊ณ ์˜', 'โ–์˜ํ™”์ž…๋‹ˆ๋‹ค', 'โ–แ„แ„']) ์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค แ„แ„ encode : ๋ฌธ์žฅ์œผ๋กœ๋ถ€ํ„ฐ ์ธ์ž ๊ฐ’์— ๋”ฐ๋ผ์„œ ์ •์ˆ˜ ์‹œํ€€์Šค ๋˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. print(sp.encode('์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹', out_type=str)) print(sp.encode('์ง„์งœ ์ตœ๊ณ ์˜ ์˜ํ™”์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹', out_type=int)) ['โ–์ง„์งœ', 'โ–์ตœ๊ณ ์˜', 'โ–์˜ํ™”์ž…๋‹ˆ๋‹ค', 'โ–แ„แ„'] [54, 200, 821, 85] 15-03 ํ—ˆ๊น… ํŽ˜์ด์Šค ํ† ํฌ ๋‚˜์ด์ €(Huggingface Tokenizer) *์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์Šคํƒ€ํŠธ์—… ํ—ˆ๊น… ํŽ˜์ด์Šค๊ฐ€ ๊ฐœ๋ฐœํ•œ ํŒจํ‚ค์ง€ tokenizers๋Š” ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ์„œ๋ธŒ ์›Œ๋“œ๋“ค์„ ํ•˜๋‚˜์˜ ํ† ํฐ์œผ๋กœ ์ทจ๊ธ‰ํ•˜๋Š” ๋‹ค์–‘ํ•œ ์„œ๋ธŒ ์›Œ๋“œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์ด ์ค‘์—์„œ WordPiece Tokenizer๋ฅผ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์Šต์„ ์œ„ํ•ด ์šฐ์„  tokenizers๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. pip install tokenizers 1. BERT์˜ ์›Œ๋“œ ํ”ผ์Šค ํ† ํฌ ๋‚˜์ด์ €(BertWordPieceTokenizer) ๊ตฌ๊ธ€์ด ๊ณต๊ฐœํ•œ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ BERT์—๋Š” WordPiece Tokenizer๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ—ˆ๊น… ํŽ˜์ด์Šค๋Š” ํ•ด๋‹น ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜์—ฌ tokenizers๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ํ†ตํ•ด ๋ฒ„ํŠธ ์›Œ๋“œ ํ”ผ์Šค ํ† ํฌ ๋‚˜์ด์ €(BertWordPieceTokenizer)๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด๋‹น ํ† ํฌ ๋‚˜์ด์ €์— ํ•™์Šต์‹œํ‚ค๊ณ , ์ด๋กœ๋ถ€ํ„ฐ ์„œ๋ธŒ ์›Œ๋“œ์˜ ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์„ ์–ป์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž„์˜์˜ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ํ•™์Šต๋œ ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. import pandas as pd import urllib.request from tokenizers import BertWordPieceTokenizer urllib.request.urlretrieve("https://raw.githubusercontent.com/e9t/nsmc/master/ratings.txt", filename="ratings.txt") ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece) ์‹ค์Šต์—์„œ ์ง„ํ–‰ํ–ˆ๋˜ ์ „์ฒ˜๋ฆฌ์™€ ๋™์ผํ•œ ๊ณผ์ •์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ratings.txt๋ผ๋Š” ํŒŒ์ผ์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•œ ํ›„, ๊ฒฐ์ธก๊ฐ’์„ ์ œ๊ฑฐํ•˜๊ณ , ์‹ค์งˆ์ ์ธ ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ์ธ document ์—ด์— ๋Œ€ํ•ด์„œ naverreview.txt๋ผ๋Š” ํŒŒ์ผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. naverdf = pd.readtable('ratings.txt') naverdf = naverdf.dropna(how='any') with open('naverreview.txt', 'w', encoding='utf8') as f: f.write('\n'.join(naverdf['document'])) ๋ฒ„ํŠธ ์›Œ๋“œ ํ”ผ์Šค ํ† ํฌ ๋‚˜์ด์ €๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. tokenizer = BertWordPieceTokenizer(lowercase=False, tripaccents=False) ๊ฐ ์ธ์ž๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. lowercase : ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ ์—ฌ๋ถ€. True์ผ ๊ฒฝ์šฐ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Œ. stripaccents : True์ผ ๊ฒฝ์šฐ ์•…์„ผํŠธ ์ œ๊ฑฐ. ex) รฉ โ†’ e, รด โ†’ o ๋„ค์ด๋ฒ„ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šตํ•˜์—ฌ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์–ป์–ด๋ด…์‹œ๋‹ค. datafile = 'naverreview.txt' vocabsize = 30000 limitalphabet = 6000 minfrequency = 5 tokenizer.train(files=datafile, vocabsize=vocabsize, limitalphabet=limitalphabet, minfrequency=minfrequency) ๊ฐ ์ธ์ž๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. files : ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์–ป๊ธฐ ์œ„ํ•ด ํ•™์Šตํ•  ๋ฐ์ดํ„ฐ vocabsize : ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ limitalphabet : ๋ณ‘ํ•ฉ ์ „์˜ ์ดˆ๊ธฐ ํ† ํฐ์˜ ํ—ˆ์šฉ ๊ฐœ์ˆ˜. minfrequency : ์ตœ์†Œ ํ•ด๋‹น ํšŸ์ˆ˜๋งŒํผ ๋“ฑ์žฅํ•œ ์Œ(pair)์˜ ๊ฒฝ์šฐ์—๋งŒ ๋ณ‘ํ•ฉ ๋Œ€์ƒ์ด ๋œ๋‹ค. ํ•™์Šต์ด ๋‹ค ๋˜์—ˆ๋‹ค๋ฉด vocab์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ๋Š” ํ˜„์žฌ ๊ฒฝ๋กœ์— ์ €์žฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # vocab ์ €์žฅ tokenizer.savemodel('./') vocab์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์œผ๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. # vocab ๋กœ๋“œ df = pd.readfwf('vocab.txt', header=None) df ์ด 30,000๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ 30,000์œผ๋กœ ์ง€์ •ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. encoded = tokenizer.encode('์•„ ๋ฐฐ๊ณ ํ”ˆ๋ฐ ์งœ์žฅ๋ฉด ๋จน๊ณ  ์‹ถ๋‹ค') print('ํ† ํฐํ™” ๊ฒฐ๊ณผ :',encoded.tokens) print('์ •์ˆ˜ ์ธ์ฝ”๋”ฉ :',encoded.ids) print('๋””์ฝ”๋”ฉ :',tokenizer.decode(encoded.ids)) ํ† ํฐํ™” ๊ฒฐ๊ณผ : ['์•„', '๋ฐฐ๊ณ ', '##ํ”ˆ', '##๋ฐ', '์งœ์žฅ๋ฉด', '##๋จน๊ณ ', '##์‹ถ๋‹ค'] ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [2111, 20629, 3979, 3244, 24682, 7871, 7379] ๋””์ฝ”๋”ฉ : ์•„ ๋ฐฐ๊ณ ํ”ˆ๋ฐ ์งœ์žฅ๋ฉด ๋จน๊ณ  ์‹ถ๋‹ค .ids๋Š” ์‹ค์งˆ์ ์ธ ๋”ฅ ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. tokens๋Š” ํ•ด๋‹น ํ† ํฌ ๋‚˜์ด์ €๊ฐ€ ์–ด๋–ป๊ฒŒ ํ† ํฐํ™”๋ฅผ ์ง„ํ–‰ํ–ˆ๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. decode()๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณต์›ํ•ฉ๋‹ˆ๋‹ค. encoded = tokenizer.encode('์ปคํ”ผ ํ•œ ์ž”์˜ ์—ฌ์œ ๋ฅผ ์ฆ๊ธฐ๋‹ค') print('ํ† ํฐํ™” ๊ฒฐ๊ณผ :',encoded.tokens) print('์ •์ˆ˜ ์ธ์ฝ”๋”ฉ :',encoded.ids) print('๋””์ฝ”๋”ฉ :',tokenizer.decode(encoded.ids)) ํ† ํฐํ™” ๊ฒฐ๊ณผ : ['์ปคํ”ผ', 'ํ•œ ์ž”', '##์˜', '์—ฌ์œ ', '##๋ฅผ', '์ฆ๊ธฐ', '##๋‹ค'] ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ : [12825, 25641, 3435, 12696, 3419, 10784, 3260] ๋””์ฝ”๋”ฉ : ์ปคํ”ผ ํ•œ ์ž”์˜ ์—ฌ์œ ๋ฅผ ์ฆ๊ธฐ๋‹ค 2. ๊ธฐํƒ€ ํ† ํฌ ๋‚˜์ด์ € ์ด ์™ธ ByteLevelBPETokenizer, CharBPETokenizer, SentencePieceBPETokenizer ๋“ฑ์ด ์กด์žฌํ•˜๋ฉฐ ์„ ํƒ์— ๋”ฐ๋ผ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BertWordPieceTokenizer : BERT์—์„œ ์‚ฌ์šฉ๋œ ์›Œ๋“œ ํ”ผ์Šค ํ† ํฌ ๋‚˜์ด์ €(WordPiece Tokenizer) CharBPETokenizer : ์˜ค๋ฆฌ์ง€๋„ BPE ByteLevelBPETokenizer : BPE์˜ ๋ฐ”์ดํŠธ ๋ ˆ๋ฒจ ๋ฒ„์ „ SentencePieceBPETokenizer : ์•ž์„œ ๋ณธ ํŒจํ‚ค์ง€ ์„ผํ…์Šค ํ”ผ์Šค(SentencePiece)์™€ ํ˜ธํ™˜๋˜๋Š” BPE ๊ตฌํ˜„์ฒด from tokenizers import ByteLevelBPETokenizer, CharBPETokenizer, SentencePieceBPETokenizer tokenizer = SentencePieceBPETokenizer() tokenizer.train('naverreview.txt', vocabsize=10000, minfrequency=5) encoded = tokenizer.encode("์ด ์˜ํ™”๋Š” ์ •๋ง ์žฌ๋ฏธ์žˆ์Šต๋‹ˆ๋‹ค.") print(encoded.tokens) python ['โ–์ด', 'โ–์˜ํ™”๋Š”', 'โ–์ •๋ง', 'โ–์žฌ๋ฏธ์žˆ', '์Šต๋‹ˆ๋‹ค.']* 16. [NLP ๊ณ ๊ธ‰ ] ์‹œํ€€์Šค ํˆฌ ์‹œํ€€์Šค(Sequence-to-Sequence, seq2seq) ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋‘ ๊ฐœ์˜ RNN ์•„ํ‚คํ…์ฒ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋งŒ๋“œ๋Š” ์‹œํ€€์Šค ํˆฌ ์‹œํ€€์Šค ๊ตฌ์กฐ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. 16-01 ์‹œํ€€์Šค ํˆฌ ์‹œํ€€์Šค(Sequence-to-Sequence, seq2seq) ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค(Sequence-to-Sequence)๋Š” ์ž…๋ ฅ๋œ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ๋„๋ฉ”์ธ์˜ ์‹œํ€€์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ฑ—๋ด‡(Chatbot)๊ณผ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ(Machine Translation)์ด ๊ทธ๋Ÿฌํ•œ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์ธ๋ฐ, ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ๊ฐ๊ฐ ์งˆ๋ฌธ๊ณผ ๋Œ€๋‹ต์œผ๋กœ ๊ตฌ์„ฑํ•˜๋ฉด ์ฑ—๋ด‡์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ๊ฐ๊ฐ ์ž…๋ ฅ ๋ฌธ์žฅ๊ณผ ๋ฒˆ์—ญ ๋ฌธ์žฅ์œผ๋กœ ๋งŒ๋“ค๋ฉด ๋ฒˆ์—ญ๊ธฐ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋„ ๋‚ด์šฉ ์š”์•ฝ(Text Summarization), STT(Speech to Text) ๋“ฑ์—์„œ ์“ฐ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ธฐ๊ณ„ ๋ฒˆ์—ญ์„ ์˜ˆ์ œ๋กœ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ๋Š” ์ค„์—ฌ์„œ seq2seq์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. seq2seq์— ๋Œ€ํ•œ ๊ตฌ์กฐ๋ฅผ ์ดํ•ดํ•˜๊ณ , ํŒŒ์ด ํ† ์น˜(PyTorch)๋ฅผ ํ†ตํ•ด ์ง์ ‘ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. 1. ๋ชจ๋ธ์˜ ๊ฐœ์š”(Overview) seq2seq๋Š” ๋ฒˆ์—ญ๊ธฐ์—์„œ ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ์„ค๋ช… ๋ฐฉ์‹์€ ๋‚ด๋ถ€๊ฐ€ ๋ณด์ด์ง€ ์•Š๋Š” ์ปค๋‹ค๋ž€ ๋ธ”๋ž™๋ฐ•์Šค์—์„œ ์ ์ฐจ์ ์œผ๋กœ ํ™•๋Œ€ํ•ด๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ์—ฌ๊ธฐ์„œ ์„ค๋ช…ํ•˜๋Š” ๋‚ด์šฉ์˜ ๋Œ€๋ถ€๋ถ„์€ RNN ์ฑ•ํ„ฐ์—์„œ ์–ธ๊ธ‰ํ•œ ๋‚ด์šฉ๋“ค์ž…๋‹ˆ๋‹ค. ๋‹จ์ง€ ์ด๊ฒƒ์„ ๊ฐ€์ง€๊ณ  ์–ด๋–ป๊ฒŒ ์กฐ๋ฆฝํ–ˆ๋Š๋ƒ์— ๋”ฐ๋ผ์„œ seq2seq๋ผ๋Š” ๊ตฌ์กฐ๊ฐ€ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ seq2seq ๋ชจ๋ธ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋ฒˆ์—ญ๊ธฐ๊ฐ€ 'I am a student'๋ผ๋Š” ์˜์–ด ๋ฌธ์žฅ์„ ์ž…๋ ฅ๋ฐ›์•„์„œ, 'je suis รฉtudiant'๋ผ๋Š” ํ”„๋ž‘์Šค ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, seq2seq ๋ชจ๋ธ ๋‚ด๋ถ€์˜ ๋ชจ์Šต์€ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์—ˆ์„๊นŒ์š”? seq2seq๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐœ๋กœ ๊ตฌ์„ฑ๋œ ์•„ํ‚คํ…์ฒ˜๋กœ ๊ตฌ์„ฑ๋˜๋Š”๋ฐ, ๋ฐ”๋กœ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์ž…๋‹ˆ๋‹ค. ์ธ์ฝ”๋”๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ž…๋ ฅ๋ฐ›์€ ๋’ค์— ๋งˆ์ง€๋ง‰์— ์ด ๋ชจ๋“  ๋‹จ์–ด ์ •๋ณด๋“ค์„ ์••์ถ•ํ•ด์„œ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š”๋ฐ, ์ด๋ฅผ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ(context vector)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…์ŠคํŠธ๋ž€ ํ•œ๊ตญ์–ด๋กœ๋Š” '๋ฌธ๋งฅ'์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ์ •๋ณด๊ฐ€ ํ•˜๋‚˜์˜ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋กœ ๋ชจ๋‘ ์••์ถ•๋˜๋ฉด ์ธ์ฝ”๋”๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๋””์ฝ”๋”๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ๋ฒˆ์—ญ๋œ ๋‹จ์–ด๋ฅผ ํ•œ ๊ฐœ์”ฉ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ 4์˜ ์‚ฌ์ด์ฆˆ๋กœ ํ‘œํ˜„ํ•˜์˜€์ง€๋งŒ, ์‹ค์ œ ํ˜„์—…์—์„œ ์‚ฌ์šฉ๋˜๋Š” seq2seq ๋ชจ๋ธ์—์„œ๋Š” ๋ณดํ†ต ์ˆ˜๋ฐฑ ์ด์ƒ์˜ ์ฐจ์›์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ๋‚ด๋ถ€๋ฅผ ์ข€ ๋” ํ™•๋Œ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 2. seq2seq์˜ ๋™์ž‘ ๊ณผ์ • ์ธ์ฝ”๋” ์•„ํ‚คํ…์ฒ˜์™€ ๋””์ฝ”๋” ์•„ํ‚คํ…์ฒ˜์˜ ๋‚ด๋ถ€๋Š” ์‚ฌ์‹ค ๋‘ ๊ฐœ์˜ RNN ์•„ํ‚คํ…์ฒ˜์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฌธ์žฅ์„ ๋ฐ›๋Š” RNN ์…€์„ ์ธ์ฝ”๋”๋ผ๊ณ  ํ•˜๊ณ , ์ถœ๋ ฅ ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋Š” RNN ์…€์„ ๋””์ฝ”๋”๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ธ์ฝ”๋”์˜ RNN ์…€์„ ์ฃผํ™ฉ์ƒ‰์œผ๋กœ, ๋””์ฝ”๋”์˜ RNN ์…€์„ ์ดˆ๋ก์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก , ์„ฑ๋Šฅ ๋ฌธ์ œ๋กœ ์ธํ•ด ์‹ค์ œ๋กœ๋Š” ๋ฐ”๋‹๋ผ RNN์ด ์•„๋‹ˆ๋ผ LSTM ์…€ ๋˜๋Š” GRU ์…€๋“ค๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์šฐ์„  ์ธ์ฝ”๋”๋ฅผ ์ž์„ธํžˆ ๋ณด๋ฉด, ์ž…๋ ฅ ๋ฌธ์žฅ์€ ๋‹จ์–ด ํ† ํฐํ™”๋ฅผ ํ†ตํ•ด์„œ ๋‹จ์–ด ๋‹จ์œ„๋กœ ์ชผ๊ฐœ์ง€๊ณ  ๋‹จ์–ด ํ† ํฐ ๊ฐ๊ฐ์€ RNN ์…€์˜ ๊ฐ ์‹œ์ ์˜ ์ž…๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋” RNN ์…€์€ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋’ค์— ์ธ์ฝ”๋” RNN ์…€์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๋””์ฝ”๋” RNN ์…€๋กœ ๋„˜๊ฒจ์ฃผ๋Š”๋ฐ ์ด๋ฅผ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋Š” ๋””์ฝ”๋” RNN ์…€์˜ ์ฒซ ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. 1. ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„ ๋””์ฝ”๋”๋Š” ์ดˆ๊ธฐ ์ž…๋ ฅ์œผ๋กœ ๋ฌธ์žฅ์˜ ์‹œ์ž‘์„ ์˜๋ฏธํ•˜๋Š” ์‹ฌ๋ฒŒ <sos>๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” <sos>๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด, ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ํ™•๋ฅ ์ด ๋†’์€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์‹œ์ (time step)์˜ ๋””์ฝ”๋” RNN ์…€์€ ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ๋‹จ์–ด๋กœ je๋ฅผ ์˜ˆ์ธกํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์˜ ๋””์ฝ”๋” RNN ์…€์€ ์˜ˆ์ธก๋œ ๋‹จ์–ด je๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ RNN ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์‹œ์ ์˜ ๋””์ฝ”๋” RNN ์…€์€ ์ž…๋ ฅ๋œ ๋‹จ์–ด je๋กœ๋ถ€ํ„ฐ ๋‹ค์‹œ ๋‹ค์Œ์— ์˜ฌ ๋‹จ์–ด์ธ suis๋ฅผ ์˜ˆ์ธกํ•˜๊ณ , ๋˜๋‹ค์‹œ ์ด๊ฒƒ์„ ๋‹ค์Œ ์‹œ์ ์˜ RNN ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์Œ์— ์˜ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ณ , ๊ทธ ์˜ˆ์ธกํ•œ ๋‹จ์–ด๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ RNN ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ๋Š” ํ–‰์œ„๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ํ–‰์œ„๋Š” ๋ฌธ์žฅ์˜ ๋์„ ์˜๋ฏธํ•˜๋Š” ์‹ฌ๋ฒŒ์ธ <eos>๊ฐ€ ๋‹ค์Œ ๋‹จ์–ด๋กœ ์˜ˆ์ธก๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์€ ํ…Œ์ŠคํŠธ ๊ณผ์ • ๋™์•ˆ์˜ ์ด์•ผ๊ธฐ์ž…๋‹ˆ๋‹ค. 2. ํ›ˆ๋ จ ๋‹จ๊ณ„์™€ ๊ต์‚ฌ ๊ฐ•์š” seq2seq๋Š” ํ›ˆ๋ จ ๊ณผ์ •๊ณผ ํ…Œ์ŠคํŠธ ๊ณผ์ •(๋˜๋Š” ์‹ค์ œ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ์‚ฌ๋žŒ์ด ์“ธ ๋•Œ)์˜ ์ž‘๋™ ๋ฐฉ์‹์ด ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ๋””์ฝ”๋”์—๊ฒŒ ์ธ์ฝ”๋”๊ฐ€ ๋ณด๋‚ธ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์™€ ์‹ค์ œ ์ •๋‹ต์ธ ์ƒํ™ฉ์ธ <sos> je suis รฉtudiant๋ฅผ ์ž…๋ ฅ๋ฐ›์•˜์„ ๋•Œ, je suis รฉtudiant <eos>๊ฐ€ ๋‚˜์™€์•ผ ๋œ๋‹ค๊ณ  ์ •๋‹ต์„ ์•Œ๋ ค์ฃผ๋ฉด์„œ ํ›ˆ๋ จํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ต์‚ฌ ๊ฐ•์š”(teacher forcing)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ๋Š” ์•ž์„œ ์„ค๋ช…ํ•œ ๊ณผ์ •๊ณผ ๊ฐ™์ด ๋””์ฝ”๋”๋Š” ์˜ค์ง ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์™€ <go>๋งŒ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์€ ํ›„์— ๋‹ค์Œ์— ์˜ฌ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ณ , ๊ทธ ๋‹จ์–ด๋ฅผ ๋‹ค์Œ ์‹œ์ ์˜ RNN ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ๋Š” ํ–‰์œ„๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. 3. ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer) - ์ด๋ฏธ ๋ฐฐ์šด ๋‚ด์šฉ ๊ธฐ๊ณ„๋Š” ํ…์ŠคํŠธ๋ณด๋‹ค ์ˆซ์ž๋ฅผ ์ž˜ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํ…์ŠคํŠธ๋ฅผ ๋ฒกํ„ฐ๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(9์ฑ•ํ„ฐ ์ฐธ๊ณ )์ด ์‚ฌ์šฉ๋œ๋‹ค๊ณ  ์„ค๋ช…ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, seq2seq์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“  ๋‹จ์–ด๋“ค์€ ์›Œ๋“œ ์ž„๋ฒ ๋”ฉ์„ ํ†ตํ•ด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋กœ์„œ ํ‘œํ˜„๋œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๋ชจ๋“  ๋‹จ์–ด์— ๋Œ€ํ•ด์„œ ์ž„๋ฒ ๋”ฉ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ฒŒ ํ•˜๋Š” ๋‹จ๊ณ„์ธ ์ž„๋ฒ ๋”ฉ ์ธต(embedding layer)์˜ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด I, am, a, student๋ผ๋Š” ๋‹จ์–ด๋“ค์— ๋Œ€ํ•œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ์œ„์™€ ๊ฐ™์€ ๋ชจ์Šต์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ ์ž ์‚ฌ์ด์ฆˆ๋ฅผ 4๋กœ ํ•˜์˜€์ง€๋งŒ, ๋ณดํ†ต ์‹ค์ œ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋Š” ์ˆ˜๋ฐฑ ๊ฐœ์˜ ์ฐจ์›์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ RNN ์…€์— ๋Œ€ํ•ด์„œ ํ™•๋Œ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4. RNN ์…€ - ์ด๋ฏธ ๋ฐฐ์šด ๋‚ด์šฉ ์ด๋ฏธ RNN์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šด ์ ์ด ์žˆ์ง€๋งŒ, ๋‹ค์‹œ ๋ณต์Šต์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ RNN ์…€์€ ๊ฐ ์‹œ์ (time step)๋งˆ๋‹ค ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์‹œ์ (time step)์„ t๋ผ๊ณ  ํ•  ๋•Œ, RNN ์…€์€ t-1์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ์™€ t์—์„œ์˜ ์ž…๋ ฅ ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ณ , t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋•Œ t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋Š” ๋ฐ”๋กœ ์œ„์— ๋˜ ๋‹ค๋ฅธ ์€๋‹‰์ธต์ด๋‚˜ ์ถœ๋ ฅ์ธต์ด ์กด์žฌํ•  ๊ฒฝ์šฐ์—๋Š” ์œ„์˜ ์ธต์œผ๋กœ ๋ณด๋‚ด๊ฑฐ๋‚˜, ํ•„์š” ์—†์œผ๋ฉด ๊ฐ’์„ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  RNN ์…€์€ ๋‹ค์Œ ์‹œ์ ์— ํ•ด๋‹นํ•˜๋Š” t+1์˜ RNN ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ํ˜„์žฌ t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. RNN ์ฑ•ํ„ฐ์—์„œ๋„ ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ, ์ด๋Ÿฐ ๊ตฌ์กฐ์—์„œ ํ˜„์žฌ ์‹œ์  t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋Š” ๊ณผ๊ฑฐ ์‹œ์ ์˜ ๋™์ผํ•œ RNN ์…€์—์„œ์˜ ๋ชจ๋“  ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’๋“ค์˜ ์˜ํ–ฅ์„ ๋ˆ„์ ํ•ด์„œ ๋ฐ›์•„์˜จ ๊ฐ’์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์•ž์„œ ์šฐ๋ฆฌ๊ฐ€ ์–ธ๊ธ‰ํ–ˆ๋˜ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋Š” ์‚ฌ์‹ค ์ธ์ฝ”๋”์—์„œ์˜ ๋งˆ์ง€๋ง‰ RNN ์…€์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ’์„ ๋งํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ์ด๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์˜ ๋ชจ๋“  ๋‹จ์–ด ํ† ํฐ๋“ค์˜ ์ •๋ณด๋ฅผ ์š”์•ฝํ•ด์„œ ๋‹ด๊ณ  ์žˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 5. ๋””์ฝ”๋” ๋””์ฝ”๋”๋Š” ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ RNN ์…€์˜ ์€๋‹‰ ์ƒํƒœ์ธ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ์ฒซ ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ RNN ์…€์€ ์ด ์ฒซ ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ์˜ ๊ฐ’๊ณผ, ํ˜„์žฌ t์—์„œ์˜ ์ž…๋ ฅ๊ฐ’์ธ <sos>๋กœ๋ถ€ํ„ฐ, ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์˜ˆ์ธก๋œ ๋‹จ์–ด๋Š” ๋‹ค์Œ ์‹œ์ ์ธ t+1 RNN์—์„œ์˜ ์ž…๋ ฅ๊ฐ’์ด ๋˜๊ณ , ์ด t+1์—์„œ์˜ RNN ๋˜ํ•œ ์ด ์ž…๋ ฅ๊ฐ’๊ณผ t์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ๋กœ๋ถ€ํ„ฐ t+1์—์„œ์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ. ์ฆ‰, ๋˜๋‹ค์‹œ ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋””์ฝ”๋”๊ฐ€ ๋‹ค์Œ์— ๋“ฑ์žฅํ•  ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ถ€๋ถ„์„ ํ™•๋Œ€ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ถœ๋ ฅ ๋‹จ์–ด๋กœ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๋“ค์€ ๋‹ค์–‘ํ•œ ๋‹จ์–ด๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. seq2seq ๋ชจ๋ธ์€ ์„ ํƒ๋  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋‹จ์–ด๋“ค๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ๋‹จ์–ด๋ฅผ ๊ณจ๋ผ์„œ ์˜ˆ์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ์“ธ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋กœ๋Š” ๋ญ๊ฐ€ ์žˆ์„๊นŒ์š”? ๋ฐ”๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋””์ฝ”๋”์—์„œ ๊ฐ ์‹œ์ (time step)์˜ RNN ์…€์—์„œ ์ถœ๋ ฅ ๋ฒกํ„ฐ๊ฐ€ ๋‚˜์˜ค๋ฉด, ํ•ด๋‹น ๋ฒกํ„ฐ๋Š” ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅ ์‹œํ€€์Šค์˜ ๊ฐ ๋‹จ์–ด๋ณ„ ํ™•๋ฅ  ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๋””์ฝ”๋”๋Š” ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. 3. ๋‹ค์–‘ํ•œ ๋ณ€ํ˜•๋“ค ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ seq2seq์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›Œ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค seq2seq๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ์„œ ์ถฉ๋ถ„ํžˆ ๋” ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๋””์ฝ”๋”์˜ ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋กœ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๊ฑฐ๊ธฐ์„œ ๋” ๋‚˜์•„๊ฐ€ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๋””์ฝ”๋”๊ฐ€ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋งค ์‹œ์ ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ ๊ฑฐ๊ธฐ์„œ ๋” ๋‚˜์•„๊ฐ€๋ฉด ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์ง€๊ธˆ ์•Œ๊ณ  ์žˆ๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ณด๋‹ค ๋”์šฑ ๋ฌธ๋งฅ์„ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•˜์—ฌ ๋งค ์‹œ์ ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์›๋‹ˆ๋‹ค. 16-02 Seq2Seq๋ฅผ ์ด์šฉํ•œ ๋ฒˆ์—ญ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ seq2seq๋ฅผ ์ด์šฉํ•ด์„œ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ์„œ๋น„์Šค์— ์‚ฌ์šฉ๋˜๋Š” ๋ฒˆ์—ญ๊ธฐ๋Š” ๋’ค์˜ ์ฑ•ํ„ฐ์—์„œ ๋ฐฐ์šฐ๊ฒŒ ๋  ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ณ , ์ตœ์†Œ ์ˆ˜๋ฐฑ๋งŒ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋Ÿผ์—๋„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฐ„๋‹จํ•œ ํ† ์ด ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ seq2seq ๊ตฌ์กฐ์™€ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์˜ ์—ญํ• ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ์ „์ฒ˜๋ฆฌ ์‹ค์ œ ์„ฑ๋Šฅ์ด ์ข‹์€ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋Š” seq2seq๋ฅผ ๊ฐ„๋‹จํžˆ ์‹ค์Šตํ•ด ๋ณด๋Š” ์ˆ˜์ค€์˜ ๊ฐ„๋‹จํ•œ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ํ›ˆ๋ จ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋กœ ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค(parallel corpus)๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค๋ž€, ๋‘ ๊ฐœ ์ด์ƒ์˜ ์–ธ์–ด๊ฐ€ ๋ณ‘๋ ฌ์ ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ฝ”ํผ์Šค๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋งํฌ : http://www.manythings.org/anki ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ํ”„๋ž‘์Šค์–ด-์˜์–ด ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค์ธ fra-eng.zip ํŒŒ์ผ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๋งํฌ์—์„œ ํ•ด๋‹น ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•œ ํ›„ ์••์ถ•์„ ํ’€๋ฉด fra.txt๋ผ๋Š” ํŒŒ์ผ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ํ•ด๋‹น ํŒŒ์ผ์„ ์ด ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ ์ฝ”ํผ์Šค ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ๋ผ๊ณ  ํ•˜๋ฉด ์•ž์„œ ์ˆ˜ํ–‰ํ•œ ํƒœ๊น… ์ž‘์—… ์ฑ•ํ„ฐ์˜ ๊ฐœ์ฒด๋ช… ์ธ์‹๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์•ž์„œ ์ˆ˜ํ–‰ํ•œ ํƒœ๊น… ์ž‘์—…์˜ ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ์™€ seq2seq๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ๋Š” ์„ฑ๊ฒฉ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํƒœ๊น… ์ž‘์—…์˜ ๋ณ‘๋ ฌ ๋ฐ์ดํ„ฐ๋Š” ์Œ์ด ๋˜๋Š” ๋ฐ์ดํ„ฐ์™€ ๋ ˆ์ด๋ธ”์ด ๊ธธ์ด๊ฐ€ ๋™์ผํ•˜์˜€์œผ๋‚˜ ์—ฌ๊ธฐ์„œ๋Š” ์Œ์ด ๋œ๋‹ค๊ณ  ํ•ด์„œ ๋ฐ˜๋“œ์‹œ ๊ธธ์ด๊ฐ€ ๊ฐ™์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์‹ค์ œ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ๊ตฌ๊ธ€ ๋ฒˆ์—ญ๊ธฐ์— '๋‚˜๋Š” ํ•™์ƒ์ด๋‹ค.'๋ผ๋Š” ํ† ํฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 2์ธ ๋ฌธ์žฅ์„ ๋„ฃ์—ˆ์„ ๋•Œ 'I am a student.'๋ผ๋Š” ํ† ํฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 4์ธ ๋ฌธ์žฅ์ด ๋‚˜์˜ค๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์ด์น˜์ž…๋‹ˆ๋‹ค. seq2seq๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์ถœ๋ ฅ ์‹œํ€€์Šค์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ ๊ตฌํ˜„ ์˜ˆ์ œ๋Š” ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ์ด์ง€๋งŒ seq2seq๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ์˜ˆ์ œ์ธ ์ฑ—๋ด‡์„ ๋งŒ๋“ ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๋ฉด, ๋Œ€๋‹ต์˜ ๊ธธ์ด๊ฐ€ ์งˆ๋ฌธ์˜ ๊ธธ์ด์™€ ํ•ญ์ƒ ๋˜‘๊ฐ™์•„์•ผ ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๊ทธ ๋˜ํ•œ ์ด์ƒํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•  fra.txt ๋ฐ์ดํ„ฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์™ผ์ชฝ์˜ ์˜์–ด ๋ฌธ์žฅ๊ณผ ์˜ค๋ฅธ์ชฝ์˜ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ ์‚ฌ์ด์— ํƒญ์œผ๋กœ ๊ตฌ๋ถ„๋˜๋Š”<NAME>์ด ํ•˜๋‚˜์˜ ์ƒ˜ํ”Œ์ž…๋‹ˆ๋‹ค. Watch me. Regardez-moi ! ๋ฐ์ดํ„ฐ๋Š” ์œ„์™€ ๋™์ผํ•œ<NAME>์˜ ์•ฝ 19๋งŒ ๊ฐœ์˜ ๋ณ‘๋ ฌ ๋ฌธ์žฅ ์ƒ˜ํ”Œ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์ „์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ์˜ ์ฝ”๋“œ์—์„œ src๋Š” source์˜ ์ค„์ž„๋ง๋กœ ์ž…๋ ฅ ๋ฌธ์žฅ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, tar๋Š” target์˜ ์ค„์ž„๋ง๋กœ ๋ฒˆ์—ญํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์žฅ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. import re import os import unicodedata import urllib3 import zipfile import shutil import numpy as np import pandas as pd import torch from collections import Counter from tqdm import tqdm from torch.utils.data import DataLoader, TensorDataset ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์•ฝ 19๋งŒ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์ค‘ 33,000๊ฐœ์˜ ์ƒ˜ํ”Œ๋งŒ์„ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. num_samples = 33000 fra-eng.zip ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์••์ถ•์„ ํ’€๊ฒ ์Šต๋‹ˆ๋‹ค. !wget -c http://www.manythings.org/anki/fra-eng.zip && unzip -o fra-eng.zip ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋“ค์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋‘์  ๋“ฑ์„ ์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜ ๋‹จ์–ด์™€ ๊ตฌ๋ถ„ํ•ด ์ฃผ๊ธฐ ์œ„ํ•œ ์ „์ฒ˜๋ฆฌ์ž…๋‹ˆ๋‹ค. def unicode_to_ascii(s): # ํ”„๋ž‘์Šค์–ด ์•…์„ผํŠธ(accent) ์‚ญ์ œ # ์˜ˆ์‹œ : 'dรฉjร  dinรฉ' -> deja dine return ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn') def preprocess_sentence(sent): # ์•…์„ผํŠธ ์‚ญ์ œ ํ•จ์ˆ˜ ํ˜ธ์ถœ sent = unicode_to_ascii(sent.lower()) # ๋‹จ์–ด์™€ ๊ตฌ๋‘์  ์‚ฌ์ด์— ๊ณต๋ฐฑ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. # Ex) "he is a boy." => "he is a boy ." sent = re.sub(r"([?.!,ยฟ])", r" \1", sent) # (a-z, A-Z, ".", "?", "!", ",") ์ด๋“ค์„ ์ œ์™ธํ•˜๊ณ ๋Š” ์ „๋ถ€ ๊ณต๋ฐฑ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. sent = re.sub(r"[^a-zA-Z!.?]+", r" ", sent) # ๋‹ค์ˆ˜ ๊ฐœ์˜ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ์น˜ํ™˜ sent = re.sub(r"\s+", " ", sent) return sent def load_preprocessed_data(): encoder_input, decoder_input, decoder_target = [], [], [] with open("fra.txt", "r") as lines: for i, line in enumerate(lines): # source ๋ฐ์ดํ„ฐ์™€ target ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌ src_line, tar_line, _ = line.strip().split('\t') # source ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ src_line = [w for w in preprocess_sentence(src_line).split()] # target ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ tar_line = preprocess_sentence(tar_line) tar_line_in = [w for w in ("<sos> " + tar_line).split()] tar_line_out = [w for w in (tar_line + " <eos>").split()] encoder_input.append(src_line) decoder_input.append(tar_line_in) decoder_target.append(tar_line_out) if i == num_samples - 1: break return encoder_input, decoder_input, decoder_target ๊ตฌํ˜„ํ•œ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋“ค์„ ์ž„์˜์˜ ๋ฌธ์žฅ์„ ์ž…๋ ฅ์œผ๋กœ ํ…Œ์ŠคํŠธํ•ด ๋ด…์‹œ๋‹ค. # ์ „์ฒ˜๋ฆฌ ํ…Œ์ŠคํŠธ en_sent = u"Have you had dinner?" fr_sent = u"Avez-vous dรฉjร  dinรฉ?" print('์ „์ฒ˜๋ฆฌ ์ „ ์˜์–ด ๋ฌธ์žฅ :', en_sent) print('์ „์ฒ˜๋ฆฌ ํ›„ ์˜์–ด ๋ฌธ์žฅ :',preprocess_sentence(en_sent)) print('์ „์ฒ˜๋ฆฌ ์ „ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ :', fr_sent) print('์ „์ฒ˜๋ฆฌ ํ›„ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ :', preprocess_sentence(fr_sent)) ์ „์ฒ˜๋ฆฌ ์ „ ์˜์–ด ๋ฌธ์žฅ : Have you had dinner? ์ „์ฒ˜๋ฆฌ ํ›„ ์˜์–ด ๋ฌธ์žฅ : have you had dinner ? ์ „์ฒ˜๋ฆฌ ์ „ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ : Avez-vous dรฉjร  dinรฉ? ์ „์ฒ˜๋ฆฌ ํ›„ ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ : avez vous deja dine ? sents_en_in, sents_fra_in, sents_fra_out = load_preprocessed_data() ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ 33,000๊ฐœ์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ ๊ต์‚ฌ ๊ฐ•์š”(Teacher Forcing)์„ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋ฏ€๋กœ, ํ›ˆ๋ จ ์‹œ ์‚ฌ์šฉํ•  ๋””์ฝ”๋”์˜ ์ž…๋ ฅ ์‹œํ€€์Šค์™€ ์‹ค์ œ ๊ฐ’. ์ฆ‰, ๋ ˆ์ด๋ธ”์— ํ•ด๋‹น๋˜๋Š” ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ์‹œํ€€์Šค์—๋Š” ์‹œ์ž‘์„ ์˜๋ฏธํ•˜๋Š” ํ† ํฐ์ธ ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์ถœ๋ ฅ ์‹œํ€€์Šค์—๋Š” ์ข…๋ฃŒ๋ฅผ ์˜๋ฏธํ•˜๋Š” ํ† ํฐ์ธ ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์–ป์€ 3๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์…‹ ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ, ๋””์ฝ”๋”์˜ ์ž…๋ ฅ, ๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ”์„ ์ƒ์œ„ 5๊ฐœ ์ƒ˜ํ”Œ๋งŒ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. sents_en_in, sents_fra_in, sents_fra_out = load_preprocessed_data() print('์ธ์ฝ”๋”์˜ ์ž…๋ ฅ :',sents_en_in[:5]) print('๋””์ฝ”๋”์˜ ์ž…๋ ฅ :',sents_fra_in[:5]) print('๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ” :',sents_fra_out[:5]) ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ : [['go', '.'], ['go', '.'], ['go', '.'], ['hi', '.'], ['hi', '.']] ๋””์ฝ”๋”์˜ ์ž…๋ ฅ : [['<sos>', 'va', '!'], ['<sos>', 'marche', '.'], ['<sos>', 'bouge', '!'], ['<sos>', 'salut', '!'], ['<sos>', 'salut', '.']] ๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ” : [['va', '!', '<eos>'], ['marche', '.', '<eos>'], ['bouge', '!', '<eos>'], ['salut', '!', '<eos>'], ['salut', '.', '<eos>']] ๋ชจ๋ธ์„ ์„ค๊ณ„ํ•˜๊ธฐ ์ „ ์˜์•„ํ•œ ์ ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์€ ์˜ค์ง ์ด์ „ ๋””์ฝ”๋” ์…€์˜ ์ถœ๋ ฅ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š”๋‹ค๊ณ  ์„ค๋ช…ํ•˜์˜€๋Š”๋ฐ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ์ธ sents_fra_in์ด ์™œ ํ•„์š”ํ• ๊นŒ์š”? ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ์ด์ „ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ถœ๋ ฅ์„ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ๋„ฃ์–ด์ฃผ์ง€ ์•Š๊ณ , ์ด์ „ ์‹œ์ ์˜ ์‹ค์ œ ๊ฐ’์„ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ๊ฒ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ด์ „ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์˜ˆ์ธก์ด ํ‹€๋ ธ๋Š”๋ฐ ์ด๋ฅผ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์˜ˆ์ธก๋„ ์ž˜๋ชป๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๊ณ  ์ด๋Š” ์—ฐ์‡„ ์ž‘์šฉ์œผ๋กœ ๋””์ฝ”๋” ์ „์ฒด์˜ ์˜ˆ์ธก์„ ์–ด๋ ต๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์ด ๋ฐ˜๋ณต๋˜๋ฉด ํ›ˆ๋ จ ์‹œ๊ฐ„์ด ๋Š๋ ค์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ์ƒํ™ฉ์„ ์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ด์ „ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์˜ˆ์ธก๊ฐ’ ๋Œ€์‹  ์‹ค์ œ ๊ฐ’์„ ํ˜„์žฌ ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์ด RNN์˜ ๋ชจ๋“  ์‹œ์ ์— ๋Œ€ํ•ด์„œ ์ด์ „ ์‹œ์ ์˜ ์˜ˆ์ธก๊ฐ’ ๋Œ€์‹  ์‹ค์ œ ๊ฐ’์„ ์ž…๋ ฅ์œผ๋กœ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ๊ต์‚ฌ ๊ฐ•์š”๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜๋ฅผ ์–ป๋Š” ๋”•์…”๋„ˆ๋ฆฌ. ์ฆ‰, ๋‹จ์–ด ์ง‘ํ•ฉ(Vocabulary)์„ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜๋กœ build_vocab()์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. build_vocab์€ ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด์˜ ๋“ฑ์žฅ ๋นˆ๋„์ˆœ์œผ๋กœ ์ •๋ ฌ ํ›„์— ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ๋†’์€ ์ˆœ์„œ์ผ์ˆ˜๋ก ๋‚ฎ์€ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ํŒจ๋”ฉ ํ† ํฐ์„ ์œ„ํ•œ <PAD> ํ† ํฐ์€ 0๋ฒˆ, OOV์— ๋Œ€์‘ํ•˜๊ธฐ ์œ„ํ•œ <UNK> ํ† ํฐ์€ 1๋ฒˆ์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๋‹จ์–ด๋Š” ์ •์ˆ˜๊ฐ€ 2๋ฒˆ, ๋นˆ๋„์ˆ˜๊ฐ€ ๋‘ ๋ฒˆ์งธ๋กœ ๋งŽ์€ ๋‹จ์–ด๋Š” ์ •์ˆ˜ 3๋ฒˆ์ด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. def build_vocab(sents): word_list = [] for sent in sents: for word in sent: word_list.append(word) # ๊ฐ ๋‹จ์–ด๋ณ„ ๋“ฑ์žฅ ๋นˆ๋„๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ๋†’์€ ์ˆœ์„œ๋กœ ์ •๋ ฌ word_counts = Counter(word_list) vocab = sorted(word_counts, key=word_counts.get, reverse=True) word_to_index = {} word_to_index['<PAD>'] = 0 word_to_index['<UNK>'] = 1 # ๋“ฑ์žฅ ๋นˆ๋„๊ฐ€ ๋†’์€ ๋‹จ์–ด์ผ์ˆ˜๋ก ๋‚ฎ์€ ์ •์ˆ˜๋ฅผ ๋ถ€์—ฌ for index, word in enumerate(vocab) : word_to_index[word] = index + 2 return word_to_index ์˜์–ด๋ฅผ ์œ„ํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ src_vocab๊ณผ ํ”„๋ž‘์Šค์–ด๋ฅผ ์ด์šฉํ•œ ๋‹จ์–ด ์ง‘ํ•ฉ tar_vocab๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค. ๊ตฌํ˜„ ๋ฐฉ์‹์— ๋”ฐ๋ผ์„œ๋Š” ํ•˜๋‚˜์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“ค์–ด๋„ ์ƒ๊ด€์—†์œผ๋ฉฐ ์ด๋Š” ์„ ํƒ์˜ ์ฐจ์ด์ž…๋‹ˆ๋‹ค. src_vocab = build_vocab(sents_en_in) tar_vocab = build_vocab(sents_fra_in + sents_fra_out) src_vocab_size = len(src_vocab) tar_vocab_size = len(tar_vocab) print("์˜์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {:d}, ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : {:d}".format(src_vocab_size, tar_vocab_size)) ์˜์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 4517, ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ : 7908 ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ๋‹จ์–ด๋ฅผ ์–ป๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๊ฐ๊ฐ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ด๋“ค์€ ํ›ˆ๋ จ์„ ๋งˆ์น˜๊ณ  ์˜ˆ์ธก๊ฐ’๊ณผ ์‹ค์ œ ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๋‹จ๊ณ„์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. index_to_src = {v: k for k, v in src_vocab.items()} index_to_tar = {v: k for k, v in tar_vocab.items()} def texts_to_sequences(sents, word_to_index): encoded_X_data = [] for sent in tqdm(sents): index_sequences = [] for word in sent: try: index_sequences.append(word_to_index[word]) except KeyError: index_sequences.append(word_to_index['<UNK>']) encoded_X_data.append(index_sequences) return encoded_X_data encoder_input = texts_to_sequences(sents_en_in, src_vocab) decoder_input = texts_to_sequences(sents_fra_in, tar_vocab) decoder_target = texts_to_sequences(sents_fra_out, tar_vocab) # ์ƒ์œ„ 5๊ฐœ์˜ ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด์„œ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ „, ํ›„ ๋ฌธ์žฅ ์ถœ๋ ฅ # ์ธ์ฝ”๋” ์ž…๋ ฅ์ด๋ฏ€๋กœ <sos>๋‚˜ <eos>๊ฐ€ ์—†์Œ for i, (item1, item2) in zip(range(5), zip(sents_en_in, encoder_input)): print(f"Index: {i}, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ „: {item1}, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„: {item2}") Index: 0, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ „: ['go', '.'], ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„: [28, 2] Index: 1, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ „: ['go', '.'], ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„: [28, 2] Index: 2, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ „: ['go', '.'], ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„: [28, 2] Index: 3, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ „: ['go', '.'], ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„: [28, 2] Index: 4, ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ์ „: ['hi', '.'], ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ ํ›„: [746, 2] def pad_sequences(sentences, max_len=None): # ์ตœ๋Œ€ ๊ธธ์ด ๊ฐ’์ด ์ฃผ์–ด์ง€์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ๋‚ด ์ตœ๋Œ€ ๊ธธ์ด๋กœ ํŒจ๋”ฉ if max_len is None: max_len = max([len(sentence) for sentence in sentences]) features = np.zeros((len(sentences), max_len), dtype=int) for index, sentence in enumerate(sentences): if len(sentence) != 0: features[index, :len(sentence)] = np.array(sentence)[:max_len] return features encoder_input = pad_sequences(encoder_input) decoder_input = pad_sequences(decoder_input) decoder_target = pad_sequences(decoder_target) ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape)๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. print('์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape) :',encoder_input.shape) print('๋””์ฝ”๋”์˜ ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape) :',decoder_input.shape) print('๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ(shape) :',decoder_target.shape) ์ธ์ฝ”๋”์˜ ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape) : (33000, 7) ๋””์ฝ”๋”์˜ ์ž…๋ ฅ์˜ ํฌ๊ธฐ(shape) : (33000, 16) ๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ(shape) : (33000, 16) ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ ์ „ ๋ฐ์ดํ„ฐ๋ฅผ ์„ž์–ด์ค๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ์ˆœ์„œ๊ฐ€ ์„ž์ธ ์ •์ˆ˜ ์‹œํ€€์Šค ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. indices = np.arange(encoder_input.shape[0]) np.random.shuffle(indices) print('๋žœ๋ค ์‹œํ€€์Šค :',indices) ๋žœ๋ค ์‹œํ€€์Šค : [29443 12274 30297 ... 24517 9984 32323] ์ด๋ฅผ ๋ฐ์ดํ„ฐ ์…‹์˜ ์ˆœ์„œ๋กœ ์ง€์ •ํ•ด ์ฃผ๋ฉด ์ƒ˜ํ”Œ๋“ค์ด ๊ธฐ์กด ์ˆœ์„œ์™€ ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ์„ž์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. encoder_input = encoder_input[indices] decoder_input = decoder_input[indices] decoder_target = decoder_target[indices] ์ž„์˜๋กœ 30,997๋ฒˆ์งธ ์ƒ˜ํ”Œ์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. ์ด๋•Œ decoder_input๊ณผ decoder_target์€ ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ์ƒ์œผ๋กœ ์•ž์— ๋ถ™์€ <sos> ํ† ํฐ๊ณผ ๋’ค์— ๋ถ™์€ <eos>์„ ์ œ์™ธํ•˜๋ฉด ๋™์ผํ•œ ์‹œํ€€์Šค๋ฅผ ๊ฐ€์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. print([index_to_src[word] for word in encoder_input[30997]]) print([index_to_tar[word] for word in decoder_input[30997]]) print([index_to_tar[word] for word in decoder_target[30997]]) ['give', 'me', 'the', 'phone', '.', '<PAD>', '<PAD>'] ['<sos>', 'donne', 'moi', 'le', 'telephone', '.', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>'] ['donne', 'moi', 'le', 'telephone', '.', '<eos>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>', '<PAD>'] 33,000๊ฐœ์˜ 10%์— ํ•ด๋‹น๋˜๋Š” 3,300๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. n_of_val = int(33000*0.1) print('๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ :',n_of_val) ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ : 3300 encoder_input_train = encoder_input[:-n_of_val] decoder_input_train = decoder_input[:-n_of_val] decoder_target_train = decoder_target[:-n_of_val] encoder_input_test = encoder_input[-n_of_val:] decoder_input_test = decoder_input[-n_of_val:] decoder_target_test = decoder_target[-n_of_val:] array([ 74, 4, 438, 5, 0, 0, 0]) array([ 3, 80, 19, 2172, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) array([ 80, 19, 2172, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์™€ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ(shape)๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print('ํ›ˆ๋ จ source ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',encoder_input_train.shape) print('ํ›ˆ๋ จ target ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',decoder_input_train.shape) print('ํ›ˆ๋ จ target ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ :',decoder_target_train.shape) print('ํ…Œ์ŠคํŠธ source ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',encoder_input_test.shape) print('ํ…Œ์ŠคํŠธ target ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ :',decoder_input_test.shape) print('ํ…Œ์ŠคํŠธ target ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ :',decoder_target_test.shape) ํ›ˆ๋ จ source ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (29700, 7) ํ›ˆ๋ จ target ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (29700, 16) ํ›ˆ๋ จ target ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (29700, 16) ํ…Œ์ŠคํŠธ source ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (3300, 7) ํ…Œ์ŠคํŠธ target ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ : (3300, 16) ํ…Œ์ŠคํŠธ target ๋ ˆ์ด๋ธ”์˜ ํฌ๊ธฐ : (3300, 16) 2. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ ๋งŒ๋“ค๊ธฐ import torch import torch.nn as nn import torch.optim as optim embedding_dim = 256 hidden_units = 256 class Encoder(nn.Module): def __init__(self, src_vocab_size, embedding_dim, hidden_units): super(Encoder, self).__init__() self.embedding = nn.Embedding(src_vocab_size, embedding_dim, padding_idx=0) self.lstm = nn.LSTM(embedding_dim, hidden_units, batch_first=True) def forward(self, x): # x.shape == (batch_size, seq_len, embedding_dim) x = self.embedding(x) # hidden.shape == (1, batch_size, hidden_units), cell.shape == (1, batch_size, hidden_units) _, (hidden, cell) = self.lstm(x) # ์ธ์ฝ”๋”์˜ ์ถœ๋ ฅ์€ hidden state, cell state return hidden, cell class Decoder(nn.Module): def __init__(self, tar_vocab_size, embedding_dim, hidden_units): super(Decoder, self).__init__() self.embedding = nn.Embedding(tar_vocab_size, embedding_dim, padding_idx=0) self.lstm = nn.LSTM(embedding_dim, hidden_units, batch_first=True) self.fc = nn.Linear(hidden_units, tar_vocab_size) def forward(self, x, hidden, cell): # x.shape == (batch_size, seq_len, embedding_dim) x = self.embedding(x) # ๋””์ฝ”๋”์˜ LSTM์œผ๋กœ ์ธ์ฝ”๋”์˜ hidden state, cell state๋ฅผ ์ „๋‹ฌ. # output.shape == (batch_size, seq_len, hidden_units) # hidden.shape == (1, batch_size, hidden_units) # cell.shape == (1, batch_size, hidden_units) output, (hidden, cell) = self.lstm(x, (hidden, cell)) # output.shape: (batch_size, seq_len, tar_vocab_size) output = self.fc(output) # ๋””์ฝ”๋”์˜ ์ถœ๋ ฅ์€ ์˜ˆ์ธก๊ฐ’, hidden state, cell state return output, hidden, cell class Seq2Seq(nn.Module): def __init__(self, encoder, decoder): super(Seq2Seq, self).__init__() self.encoder = encoder self.decoder = decoder def forward(self, src, trg): hidden, cell = self.encoder(src) # ํ›ˆ๋ จ ์ค‘์—๋Š” ๋””์ฝ”๋”์˜ ์ถœ๋ ฅ ์ค‘ ์˜ค์ง output๋งŒ ์‚ฌ์šฉํ•œ๋‹ค. output, _, _ = self.decoder(trg, hidden, cell) return output encoder = Encoder(src_vocab_size, embedding_dim, hidden_units) decoder = Decoder(tar_vocab_size, embedding_dim, hidden_units) model = Seq2Seq(encoder, decoder) loss_function = nn.CrossEntropyLoss(ignore_index=0) optimizer = optim.Adam(model.parameters()) ๋ชจ๋ธ์˜ ๊ตฌ์กฐ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(model) Seq2Seq( (encoder): Encoder( (embedding): Embedding(4517, 256, padding_idx=0) (lstm): LSTM(256, 256, batch_first=True) ) (decoder): Decoder( (embedding): Embedding(7908, 256, padding_idx=0) (lstm): LSTM(256, 256, batch_first=True) (fc): Linear(in_features=256, out_features=7908, bias=True) ) ) ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›๊ณผ LSTM์˜ ์€๋‹‰ ์ƒํƒœ์˜ ํฌ๊ธฐ๋ฅผ 256๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋Š” ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋กœ๋ถ€ํ„ฐ ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋„ ์€๋‹‰ ์ƒํƒœ, ์…€ ์ƒํƒœ๋ฅผ ๋ฆฌํ„ดํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. seq2seq์˜ ๋””์ฝ”๋”๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ ์‹œ์ ๋งˆ๋‹ค ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งค ์‹œ์ ๋งˆ๋‹ค ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ(tar_vocab_size)์˜ ์„ ํƒ์ง€์—์„œ ๋‹จ์–ด๋ฅผ 1๊ฐœ ์„ ํƒํ•˜์—ฌ ์ด๋ฅผ ์ด๋ฒˆ ์‹œ์ ์—์„œ ์˜ˆ์ธกํ•œ ๋‹จ์–ด๋กœ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ด๋ฏ€๋กœ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. def evaluation(model, dataloader, loss_function, device): model.eval() total_loss = 0.0 total_correct = 0 total_count = 0 with torch.no_grad(): for encoder_inputs, decoder_inputs, decoder_targets in dataloader: encoder_inputs = encoder_inputs.to(device) decoder_inputs = decoder_inputs.to(device) decoder_targets = decoder_targets.to(device) # ์ˆœ๋ฐฉํ–ฅ ์ „ํŒŒ # outputs.shape == (batch_size, seq_len, tar_vocab_size) outputs = model(encoder_inputs, decoder_inputs) # ์†์‹ค ๊ณ„์‚ฐ # outputs.view(-1, outputs.size(-1))์˜ shape๋Š” (batch_size * seq_len, tar_vocab_size) # decoder_targets.view(-1)์˜ shape๋Š” (batch_size * seq_len) loss = loss_function(outputs.view(-1, outputs.size(-1)), decoder_targets.view(-1)) total_loss += loss.item() # ์ •ํ™•๋„ ๊ณ„์‚ฐ (ํŒจ๋”ฉ ํ† ํฐ ์ œ์™ธ) mask = decoder_targets != 0 total_correct += ((outputs.argmax(dim=-1) == decoder_targets) * mask).sum().item() total_count += mask.sum().item() return total_loss / len(dataloader), total_correct / total_count encoder_input_train_tensor = torch.tensor(encoder_input_train, dtype=torch.long) decoder_input_train_tensor = torch.tensor(decoder_input_train, dtype=torch.long) decoder_target_train_tensor = torch.tensor(decoder_target_train, dtype=torch.long) encoder_input_test_tensor = torch.tensor(encoder_input_test, dtype=torch.long) decoder_input_test_tensor = torch.tensor(decoder_input_test, dtype=torch.long) decoder_target_test_tensor = torch.tensor(decoder_target_test, dtype=torch.long) # ๋ฐ์ดํ„ฐ ์…‹ ๋ฐ ๋ฐ์ดํ„ฐ ๋กœ๋” ์ƒ์„ฑ batch_size = 128 train_dataset = TensorDataset(encoder_input_train_tensor, decoder_input_train_tensor, decoder_target_train_tensor) train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) valid_dataset = TensorDataset(encoder_input_test_tensor, decoder_input_test_tensor, decoder_target_test_tensor) valid_dataloader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False) # ํ•™์Šต ์„ค์ • num_epochs = 30 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•ฉ๋‹ˆ๋‹ค. 128๊ฐœ์˜ ๋ฐฐ์น˜ ํฌ๊ธฐ(128๊ฐœ์”ฉ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ‘๋ ฌ๋กœ ํ•™์Šต)๋กœ ์ด 50 ์—ํฌํฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ํ›ˆ๋ จ์ด ์ œ๋Œ€๋กœ ๋˜๊ณ  ์žˆ๋Š”์ง€ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # Training loop best_val_loss = float('inf') for epoch in range(num_epochs): # ํ›ˆ๋ จ ๋ชจ๋“œ model.train() for encoder_inputs, decoder_inputs, decoder_targets in train_dataloader: encoder_inputs = encoder_inputs.to(device) decoder_inputs = decoder_inputs.to(device) decoder_targets = decoder_targets.to(device) # ๊ธฐ์šธ๊ธฐ ์ดˆ๊ธฐํ™” optimizer.zero_grad() # ์ˆœ๋ฐฉํ–ฅ ์ „ํŒŒ # outputs.shape == (batch_size, seq_len, tar_vocab_size) outputs = model(encoder_inputs, decoder_inputs) # ์†์‹ค ๊ณ„์‚ฐ ๋ฐ ์—ญ๋ฐฉํ–ฅ ์ „ํŒŒ # outputs.view(-1, outputs.size(-1))์˜ shape๋Š” (batch_size * seq_len, tar_vocab_size) # decoder_targets.view(-1)์˜ shape๋Š” (batch_size * seq_len) loss = loss_function(outputs.view(-1, outputs.size(-1)), decoder_targets.view(-1)) loss.backward() # ๊ฐ€์ค‘์น˜ ์—…๋ฐ์ดํŠธ optimizer.step() train_loss, train_acc = evaluation(model, train_dataloader, loss_function, device) valid_loss, valid_acc = evaluation(model, valid_dataloader, loss_function, device) print(f'Epoch: {epoch+1}/{num_epochs} | Train Loss: {train_loss:.4f} | Train Acc: {train_acc:.4f} | Valid Loss: {valid_loss:.4f} | Valid Acc: {valid_acc:.4f}') # ๊ฒ€์ฆ ์†์‹ค์ด ์ตœ์†Œ์ผ ๋•Œ ์ฒดํฌํฌ์ธํŠธ ์ €์žฅ if valid_loss < best_val_loss: print(f'Validation loss improved from {best_val_loss:.4f} to {valid_loss:.4f}. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.') best_val_loss = valid_loss torch.save(model.state_dict(), 'best_model_checkpoint.pth') ์ €์ž์˜ ํ•™์Šต ๊ธฐ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Epoch: 1/30 | Train Loss: 2.9014 | Train Acc: 0.5312 | Valid Loss: 3.0343 | Valid Acc: 0.5257 Validation loss improved from inf to 3.0343. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 2/30 | Train Loss: 2.2466 | Train Acc: 0.6030 | Valid Loss: 2.5037 | Valid Acc: 0.5886 Validation loss improved from 3.0343 to 2.5037. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 3/30 | Train Loss: 1.8302 | Train Acc: 0.6487 | Valid Loss: 2.2069 | Valid Acc: 0.6181 Validation loss improved from 2.5037 to 2.2069. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 4/30 | Train Loss: 1.5223 | Train Acc: 0.6869 | Valid Loss: 2.0138 | Valid Acc: 0.6424 Validation loss improved from 2.2069 to 2.0138. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 5/30 | Train Loss: 1.2775 | Train Acc: 0.7241 | Valid Loss: 1.8763 | Valid Acc: 0.6582 Validation loss improved from 2.0138 to 1.8763. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 6/30 | Train Loss: 1.0680 | Train Acc: 0.7643 | Valid Loss: 1.7626 | Valid Acc: 0.6766 Validation loss improved from 1.8763 to 1.7626. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 7/30 | Train Loss: 0.8900 | Train Acc: 0.7895 | Valid Loss: 1.6930 | Valid Acc: 0.6852 Validation loss improved from 1.7626 to 1.6930. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 8/30 | Train Loss: 0.7457 | Train Acc: 0.8253 | Valid Loss: 1.6228 | Valid Acc: 0.6969 Validation loss improved from 1.6930 to 1.6228. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 9/30 | Train Loss: 0.6195 | Train Acc: 0.8557 | Valid Loss: 1.5719 | Valid Acc: 0.7071 Validation loss improved from 1.6228 to 1.5719. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 10/30 | Train Loss: 0.5193 | Train Acc: 0.8748 | Valid Loss: 1.5415 | Valid Acc: 0.7120 Validation loss improved from 1.5719 to 1.5415. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 11/30 | Train Loss: 0.4450 | Train Acc: 0.8905 | Valid Loss: 1.5235 | Valid Acc: 0.7165 Validation loss improved from 1.5415 to 1.5235. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 12/30 | Train Loss: 0.3804 | Train Acc: 0.9010 | Valid Loss: 1.5223 | Valid Acc: 0.7142 Validation loss improved from 1.5235 to 1.5223. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 13/30 | Train Loss: 0.3334 | Train Acc: 0.9090 | Valid Loss: 1.5129 | Valid Acc: 0.7180 Validation loss improved from 1.5223 to 1.5129. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 14/30 | Train Loss: 0.2965 | Train Acc: 0.9146 | Valid Loss: 1.5222 | Valid Acc: 0.7213 ... ์ค‘๋žต ... ์ดํ›„ validation_loss๋Š” ๊ณ„์† ์ฆ๊ฐ€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค์ด ๊ฐ€์žฅ ์ตœ์†Œ์ผ ๋•Œ์˜ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ๋‹ค์‹œ ์žฌํ‰๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. # ๋ชจ๋ธ ๋กœ๋“œ model.load_state_dict(torch.load('best_model_checkpoint.pth')) # ๋ชจ๋ธ์„ device์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. model.to(device) # ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ val_loss, val_accuracy = evaluation(model, valid_dataloader, loss_function, device) print(f'Best model validation loss: {val_loss:.4f}') print(f'Best model validation accuracy: {val_accuracy:.4f}') Best model validation loss: 1.5129 Best model validation accuracy: 0.7180 ๋กœ๋“œ ํ›„ ์žฌํ‰๊ฐ€๋ฅผ ์ง„ํ–‰ํ•˜์˜€๋”๋‹ˆ, ์ €์žฅํ•  ๋‹น์‹œ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์†์‹ค๊ณผ ์ •ํ™•๋„๊ฐ€ ๋™์ผํ•˜๋ฏ€๋กœ ์ €์žฅ ๋ฐ ๋กœ๋“œ๊ฐ€ ์›ํ™œํžˆ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. <sos>์™€ <eos> ํ† ํฐ์˜ ์ •์ˆ˜๋Š” ๊ฐ๊ฐ 3๊ณผ 4์ž…๋‹ˆ๋‹ค. print(tar_vocab['<sos>']) print(tar_vocab['<eos>']) 4 3. seq2seq ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ ๋™์ž‘์‹œํ‚ค๊ธฐ seq2seq๋Š” ํ›ˆ๋ จ ๊ณผ์ •(๊ต์‚ฌ ๊ฐ•์š”)๊ณผ ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ์˜ ๋™์ž‘ ๋ฐฉ์‹์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ…Œ์ŠคํŠธ ๊ณผ์ •์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋‹ค์‹œ ์„ค๊ณ„ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ๋””์ฝ”๋”๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฒˆ์—ญ ๋‹จ๊ณ„๋ฅผ ์œ„ํ•ด ๋ชจ๋ธ์„ ์ˆ˜์ •ํ•˜๊ณ  ๋™์ž‘์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด์ ์ธ ๋ฒˆ์—ญ ๋‹จ๊ณ„๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1) ๋ฒˆ์—ญํ•˜๊ณ ์ž ํ•˜๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์ด ์ธ์ฝ”๋”๋กœ ์ž…๋ ฅ๋˜์–ด ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. 2) ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ, ๊ทธ๋ฆฌ๊ณ  ํ† ํฐ <sos>๋ฅผ ๋””์ฝ”๋”๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. 3) ๋””์ฝ”๋”๊ฐ€ ํ† ํฐ <eos>๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ํ–‰๋™์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. index_to_src = {v: k for k, v in src_vocab.items()} index_to_tar = {v: k for k, v in tar_vocab.items()} # ์›๋ฌธ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ def seq_to_src(input_seq): sentence = '' for encoded_word in input_seq: if(encoded_word != 0): sentence = sentence + index_to_src[encoded_word] + ' ' return sentence # ๋ฒˆ์—ญ๋ฌธ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ def seq_to_tar(input_seq): sentence = '' for encoded_word in input_seq: if(encoded_word != 0 and encoded_word != tar_vocab['<sos>'] and encoded_word != tar_vocab['<eos>']): sentence = sentence + index_to_tar[encoded_word] + ' ' return sentence print(encoder_input_test[25]) print(decoder_input_test[25]) print(decoder_target_test[25]) array([ 4, 22, 931, 2, 0, 0, 0]) array([ 3, 19, 36, 2007, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) array([ 19, 36, 2007, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) decode_sequence() ํ•จ์ˆ˜๋ฅผ ๋ด…์‹œ๋‹ค. ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„์—์„œ๋Š” ๋””์ฝ”๋”๋ฅผ ๋งค ์‹œ์  ๋ณ„๋กœ ์ปจํŠธ๋กคํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์‹œ์ ์„ for ๋ฌธ์„ ํ†ตํ•ด์„œ ์ปจํŠธ๋กคํ•˜๊ฒŒ ๋˜๋ฉฐ, ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก์€ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋  ๋ณ€์ˆ˜๋Š” decoder_input์ž…๋‹ˆ๋‹ค. def decode_sequence(input_seq, model, src_vocab_size, tar_vocab_size, max_output_len, int_to_src_token, int_to_tar_token): encoder_inputs = torch.tensor(input_seq, dtype=torch.long).unsqueeze(0).to(device) # ์ธ์ฝ”๋”์˜ ์ดˆ๊ธฐ ์ƒํƒœ ์„ค์ • hidden, cell = model.encoder(encoder_inputs) # ์‹œ์ž‘ ํ† ํฐ <sos>์„ ๋””์ฝ”๋”์˜ ์ฒซ ์ž…๋ ฅ์œผ๋กœ ์„ค์ • # unsqueeze(0)๋Š” ๋ฐฐ์น˜ ์ฐจ์›์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•จ. decoder_input = torch.tensor([3], dtype=torch.long).unsqueeze(0).to(device) decoded_tokens = [] # for ๋ฌธ์„ ๋„๋Š” ๊ฒƒ == ๋””์ฝ”๋”์˜ ๊ฐ ์‹œ์  for _ in range(max_output_len): output, hidden, cell = model.decoder(decoder_input, hidden, cell) # ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์ˆ˜ํ–‰. ์˜ˆ์ธก ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค output_token = output.argmax(dim=-1).item() # ์ข…๋ฃŒ ํ† ํฐ <eos> if output_token == 4: break # ๊ฐ ์‹œ์ ์˜ ๋‹จ์–ด(์ •์ˆ˜)๋Š” decoded_tokens์— ๋ˆ„์ ํ•˜์˜€๋‹ค๊ฐ€ ์ตœ์ข… ๋ฒˆ์—ญ ์‹œํ€€์Šค๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. decoded_tokens.append(output_token) # ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก. ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. decoder_input = torch.tensor([output_token], dtype=torch.long).unsqueeze(0).to(device) return ' '.join(int_to_tar_token[token] for token in decoded_tokens) ๊ฒฐ๊ณผ ํ™•์ธ์„ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. seq_to_src ํ•จ์ˆ˜๋Š” ์˜์–ด ๋ฌธ์žฅ์— ํ•ด๋‹นํ•˜๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ์˜์–ด ๋‹จ์–ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_src๋ฅผ ํ†ตํ•ด ์˜์–ด ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. seq_to_tar์€ ํ”„๋ž‘์Šค์–ด์— ํ•ด๋‹นํ•˜๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_tar์„ ํ†ตํ•ด ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ž„์˜๋กœ ์„ ํƒํ•œ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. for seq_index in [3, 50, 100, 300, 1001]: input_seq = encoder_input_train[seq_index] translated_text = decode_sequence(input_seq, model, src_vocab_size, tar_vocab_size, 20, index_to_src, index_to_tar) print("์ž…๋ ฅ ๋ฌธ์žฅ :",seq_to_src(encoder_input_train[seq_index])) print("์ •๋‹ต ๋ฌธ์žฅ :",seq_to_tar(decoder_input_train[seq_index])) print("๋ฒˆ์—ญ ๋ฌธ์žฅ :",translated_text) print("-"*50) ์ž…๋ ฅ ๋ฌธ์žฅ : you re fortunate . ์ •๋‹ต ๋ฌธ์žฅ : tu es chanceux . ๋ฒˆ์—ญ ๋ฌธ์žฅ : tu es chanceuse . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : run for it ! ์ •๋‹ต ๋ฌธ์žฅ : taillez vous ! ๋ฒˆ์—ญ ๋ฌธ์žฅ : sauvez vous ! -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : pass me the water . ์ •๋‹ต ๋ฌธ์žฅ : passe moi l eau . ๋ฒˆ์—ญ ๋ฌธ์žฅ : passe moi l eau . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : i couldn t fight . ์ •๋‹ต ๋ฌธ์žฅ : je ne pourrais pas me battre . ๋ฒˆ์—ญ ๋ฌธ์žฅ : je ne pourrais pas me battre . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : get real ! ์ •๋‹ต ๋ฌธ์žฅ : sois realiste ! ๋ฒˆ์—ญ ๋ฌธ์žฅ : sois realiste ! -------------------------------------------------- ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ž„์˜๋กœ ์„ ํƒํ•œ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. for seq_index in [3, 50, 100, 300, 1001]: input_seq = encoder_input_test[seq_index] translated_text = decode_sequence(input_seq, model, src_vocab_size, tar_vocab_size, 20, index_to_src, index_to_tar) print("์ž…๋ ฅ ๋ฌธ์žฅ :",seq_to_src(encoder_input_test[seq_index])) print("์ •๋‹ต ๋ฌธ์žฅ :",seq_to_tar(decoder_input_test[seq_index])) print("๋ฒˆ์—ญ ๋ฌธ์žฅ :",translated_text) print("-"*50) ์ž…๋ ฅ ๋ฌธ์žฅ : you re good . ์ •๋‹ต ๋ฌธ์žฅ : tu es bonne . ๋ฒˆ์—ญ ๋ฌธ์žฅ : vous etes bon . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : you cheated . ์ •๋‹ต ๋ฌธ์žฅ : tu as triche . ๋ฒˆ์—ญ ๋ฌธ์žฅ : vous avez triche . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : put it there . ์ •๋‹ต ๋ฌธ์žฅ : mettez le la . ๋ฒˆ์—ญ ๋ฌธ์žฅ : mets le la . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : get your gear . ์ •๋‹ต ๋ฌธ์žฅ : allez chercher votre materiel ! ๋ฒˆ์—ญ ๋ฌธ์žฅ : va chercher tes affaires ! -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : i m a stutterer . ์ •๋‹ต ๋ฌธ์žฅ : je suis begue . ๋ฒˆ์—ญ ๋ฌธ์žฅ : je suis un coureur . -------------------------------------------------- ๋ฒˆ์—ญ๊ธฐ๋ฅผ ํ†ตํ•ด์„œ ์ž…๋ ฅ ๋ฌธ์žฅ๊ณผ ์ •๋‹ต ๋ฌธ์žฅ ๋ฒˆ์—ญ ๋ฌธ์žฅ์˜ ์‹ค์ œ ๋‚ด์šฉ์„ ํ™•์ธํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฌธ์žฅ (์˜์–ด) : you re good . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋„ˆ ์ •๋ง ์ž˜ํ•˜๋„ค. ์ •๋‹ต ๋ฌธ์žฅ (ํ”„๋ž‘์Šค์–ด) : tu es bonne . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋„ˆ ์ •๋ง ์ž˜ํ•˜๋„ค. ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ”„๋ž‘์Šค์–ด) : vous etes bon . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋‹น์‹ ์€ ์ข‹์Šต๋‹ˆ๋‹ค. --- ์ž…๋ ฅ ๋ฌธ์žฅ (์˜์–ด) : you cheated . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋„ˆ ์†์˜€์–ด. ์ •๋‹ต ๋ฌธ์žฅ (ํ”„๋ž‘์Šค์–ด) : tu as triche . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋„ˆ ์†์˜€์–ด. ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ”„๋ž‘์Šค์–ด) : vous avez triche . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋‹น์‹ ์€ ์†์˜€์–ด. --- ์ž…๋ ฅ ๋ฌธ์žฅ (์˜์–ด) : put it there . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๊ฑฐ๊ธฐ์— ๋†“์œผ์„ธ์š”. ์ •๋‹ต ๋ฌธ์žฅ (ํ”„๋ž‘์Šค์–ด) : mettez le la . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๊ฑฐ๊ธฐ์— ๋†“์œผ์„ธ์š”. ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ”„๋ž‘์Šค์–ด) : mets le la . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๊ฑฐ๊ธฐ์— ๋†“์•„. --- ์ž…๋ ฅ ๋ฌธ์žฅ (์˜์–ด) : get your gear . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋„ค ๋ฌผ๊ฑด ๊ฐ€์ ธ์™€. ์ •๋‹ต ๋ฌธ์žฅ (ํ”„๋ž‘์Šค์–ด) : allez chercher votre materiel ! ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋„ค ๋ฌผ๊ฑด ๊ฐ€์ ธ์™€! ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ”„๋ž‘์Šค์–ด) : va chercher tes affaires ! ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋„ค ๋ฌผ๊ฑด ๊ฐ€์ ธ์™€! --- ์ž…๋ ฅ ๋ฌธ์žฅ (์˜์–ด) : i m a stutterer . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋‚˜๋Š” ๋ง๋”๋“ฌ์ด์•ผ. ์ •๋‹ต ๋ฌธ์žฅ (ํ”„๋ž‘์Šค์–ด) : je suis begue . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋‚˜๋Š” ๋ง๋”๋“ฌ์ด์•ผ. ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ”„๋ž‘์Šค์–ด) : je suis un coureur . ๋ฒˆ์—ญ ๋ฌธ์žฅ (ํ•œ๊ตญ์–ด) : ๋‚˜๋Š” ๋‹ฌ๋ฆฌ๊ธฐ ์„ ์ˆ˜์ž…๋‹ˆ๋‹ค. 15-03์—์„œ ์ด ๋ฒˆ์—ญ๊ธฐ๋ฅผ ์ข€ ๋” ๊ฐœ์„ ํ•œ ๋ฒ„์ „์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. 16-03 BLEU Score(Bilingual Evaluation Understudy Score) ์•ž์„œ ์–ธ์–ด ๋ชจ๋ธ(Language Model)์˜ ์„ฑ๋Šฅ ์ธก์ •์„ ์œ„ํ•œ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์œผ๋กœ ํŽ„ ํ”Œ๋ ‰์„œํ‹ฐ(perplexity, PPL)๋ฅผ ์†Œ๊ฐœํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ์—๋„ PPL์„ ํ‰๊ฐ€์— ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, PPL์€ ๋ฒˆ์—ญ์˜ ์„ฑ๋Šฅ์„ ์ง์ ‘์ ์œผ๋กœ ๋ฐ˜์˜ํ•˜๋Š” ์ˆ˜์น˜๋ผ ๋ณด๊ธฐ์—” ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ๋Š” ๊ทธ ์™ธ์—๋„ ์ˆ˜๋งŽ์€ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•๋“ค์ด ์กด์žฌํ•˜๋Š”๋ฐ, ๊ธฐ๊ณ„ ๋ฒˆ์—ญ์˜ ์„ฑ๋Šฅ์ด ์–ผ๋งˆ๋‚˜ ๋›ฐ์–ด๋‚œ๊ฐ€๋ฅผ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฐฉ๋ฒ•์ธ BLEU(Bilingual Evaluation Understudy) ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ์ง„ํ–‰๋˜๋Š” ์„ค๋ช…์€ ๋…ผ๋ฌธ BLEU: a Method for Automatic Evaluation of Machine Translation๋ฅผ ์ฐธ๊ณ ๋กœ ํ•˜์—ฌ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. import numpy as np from collections import Counter from nltk import ngrams 1. BLEU(Bilingual Evaluation Understudy) BLEU๋Š” ๊ธฐ๊ณ„ ๋ฒˆ์—ญ ๊ฒฐ๊ณผ์™€ ์‚ฌ๋žŒ์ด ์ง์ ‘ ๋ฒˆ์—ญํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€ ๋น„๊ตํ•˜์—ฌ ๋ฒˆ์—ญ์— ๋Œ€ํ•œ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ธก์ • ๊ธฐ์ค€์€ n-gram์— ๊ธฐ๋ฐ˜ํ•ฉ๋‹ˆ๋‹ค. n-gram์˜ ์ •์˜๋Š” ์–ธ์–ด ๋ชจ๋ธ ์ฑ•ํ„ฐ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. BLEU๋Š” ์™„๋ฒฝํ•œ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ ๋Š” ํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ ๋ช‡ ๊ฐ€์ง€ ์ด์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์–ธ์–ด์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ณ„์‚ฐ ์†๋„๊ฐ€ ๋น ๋ฆ…๋‹ˆ๋‹ค. BLEU๋Š” PPL ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๋†’์„์ˆ˜๋ก ์„ฑ๋Šฅ์ด ๋” ์ข‹์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. BLEU๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๊ณ„ ๋ฒˆ์—ญ ์„ฑ๋Šฅ ํ‰๊ฐ€๋ฅผ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ์ง๊ด€์ ์ธ ๋ฐฉ๋ฒ•์„ ๋จผ์ € ์ œ์‹œํ•˜๊ณ , ๋ฌธ์ œ์ ์„ ๋ณด์™„ํ•ด๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 1) ๋‹จ์–ด ๊ฐœ์ˆ˜ ์นด์šดํŠธ๋กœ ์ธก์ •ํ•˜๊ธฐ(Unigram Precision) ํ•œ๊ตญ์–ด-์˜์–ด ๋ฒˆ์—ญ๊ธฐ์˜ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. ๋‘ ๊ฐœ์˜ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ๊ฐ€ ์กด์žฌํ•˜๊ณ  ๋‘ ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ์— ๊ฐ™์€ ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์—ฌ ๋ฒˆ์—ญ๋œ ์˜์–ด ๋ฌธ์žฅ์˜ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋ฒˆ์—ญ๋œ ๋ฌธ์žฅ์„ ๊ฐ๊ฐ Candidate1, 2๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด ๋ฌธ์žฅ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ •๋‹ต์œผ๋กœ ๋น„๊ต๋˜๋Š” ๋ฌธ์žฅ์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์„ธ ๋ช…์˜ ์‚ฌ๋žŒ์—๊ฒŒ ํ•œ๊ตญ์–ด๋ฅผ ๋ณด๊ณ  ์˜์ž‘ํ•ด ๋ณด๋ผ๊ณ  ํ•˜์—ฌ ์„ธ ๊ฐœ์˜ ๋ฒˆ์—ญ ๋ฌธ์žฅ์„ ๋งŒ๋“ค์–ด๋ƒˆ์Šต๋‹ˆ๋‹ค. ์ด ์„ธ ๋ฌธ์žฅ์„ ๊ฐ๊ฐ Reference1, 2, 3๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. Example 1 Candidate1 : It is a guide to action which ensures that the military always obeys the commands of the party. Candidate2 : It is to insure the troops forever hearing the activity guidebook that party direct. Reference1 : It is a guide to action that ensures that the military will forever heed Party commands. Reference2 : It is the guiding principle which guarantees the military forces always being under the command of the Party. Reference3 : It is the practical guide for the army always to heed the directions of the party. ํŽธ์˜์ƒ Candidate๋ฅผ Ca๋กœ, Reference๋ฅผ Ref๋กœ ์ถ•์•ฝํ•˜์—ฌ ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. Ca 1, 2๋ฅผ Ref 1, 2, 3๊ณผ ๋น„๊ตํ•˜์—ฌ ์„ฑ๋Šฅ์„ ์ธก์ •ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ง๊ด€์ ์ธ ์„ฑ๋Šฅ ํ‰๊ฐ€ ๋ฐฉ๋ฒ•์€ Ref 1, 2, 3 ์ค‘ ์–ด๋Š ํ•œ ๋ฌธ์žฅ์ด๋ผ๋„ ๋“ฑ์žฅํ•œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ Ca์—์„œ ์„ธ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ํ›„์— Ca์˜ ๋ชจ๋“  ๋‹จ์–ด์˜ ์นด์šดํŠธ์˜ ํ•ฉ. ์ฆ‰, Ca์—์„œ์˜ ์ด ๋‹จ์–ด์˜ ์ˆ˜์œผ๋กœ ๋‚˜๋ˆ ์ค๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ธก์ • ๋ฐฉ๋ฒ•์„ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„(Unigram Precision)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋“ค ์ค‘์—์„œ ์กด์žฌํ•˜๋Š”์˜ ๋‹จ์–ด์˜ ์ˆ˜ ์˜์ด ๋‹จ์–ด ์ˆ˜ Unigram Precision = Ref๋“ค ์ค‘์—์„œ ์กด์žฌํ•˜๋Š” Ca์˜ ๋‹จ์–ด์˜ ์ˆ˜ Ca์˜ ์ด ๋‹จ์–ด ์ˆ˜ the number of Ca words(unigrams) which occur in any Ref the total number of words in the Ca Ca1์˜ ๋‹จ์–ด๋“ค์€ ์–ผ์ถ” ํ›‘์–ด๋งŒ ๋ด๋„ Ref1, Ref2, Ref3์—์„œ ์ „๋ฐ˜์ ์œผ๋กœ ๋“ฑ์žฅํ•˜๋Š” ๋ฐ˜๋ฉด, Ca2๋Š” ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” Ca1์ด Ca2๋ณด๋‹ค ๋” ์ข‹์€ ๋ฒˆ์—ญ ๋ฌธ์žฅ์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Ca1์˜ It is a guide to action์€ Ref1์—์„œ, which๋Š” Ref2์—์„œ, ensures that the militrary๋Š” Ref1์—์„œ, always๋Š” Ref2์™€ Ref3์—์„œ, commands๋Š” Ref1์—์„œ, of the party๋Š” Ref2์—์„œ ๋“ฑ์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. (๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์€ ์—†๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค.) Ca1์— ์žˆ๋Š” ๋‹จ์–ด ์ค‘ Ref1, Ref2, Ref3 ์–ด๋””์—๋„ ๋“ฑ์žฅํ•˜์ง€ ์•Š์€ ๋‹จ์–ด๋Š” obeys๋ฟ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, Ca2๋Š” Ca1๊ณผ ๋น„๊ตํ•˜์—ฌ ์ƒ๋Œ€์ ์œผ๋กœ Ref1, 2, 3์— ๋“ฑ์žฅํ•œ ๋‹จ์–ด๋“ค์ด ์ ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์— ๋”ฐ๋ฅด๋ฉด Ca1๊ณผ Ca2์˜ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” ๊ฐ๊ฐ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. Ca1 Unigram Precision = 17 18 Ca2 Unigram Precision = 14 ์ด์ œ๋ถ€ํ„ฐ๋Š” ๋‹จ์–ด๋ผ๋Š” ํ‘œํ˜„๋ณด๋‹ค๋Š” ์œ ๋‹ˆ๊ทธ๋žจ์ด๋ผ๋Š” ์šฉ์–ด๋กœ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” ๋‚˜๋ฆ„ ์˜๋ฏธ ์žˆ๋Š” ์ธก์ • ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณด์ด์ง€๋งŒ ์‚ฌ์‹ค ํ—ˆ์ˆ ํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒˆ๋กœ์šด ์˜ˆ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ด…์‹œ๋‹ค. 2) ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜์—ฌ ๋ณด์ •ํ•˜๊ธฐ(Modified Unigram Precision) Example 2 Candidate : the the the the the the the Reference1 : the cat is on the mat Reference2 : there is a cat on the mat ์œ„์˜ Ca๋Š” the๋งŒ 7๊ฐœ๊ฐ€ ๋“ฑ์žฅํ•œ ํ„ฐ๋ฌด๋‹ˆ์—†๋Š” ๋ฒˆ์—ญ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๋ฒˆ์—ญ์€ ์•ž์„œ ๋ฐฐ์šด ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์— ๋”ฐ๋ฅด๋ฉด 7 1 ์ด๋ผ๋Š” ์ตœ๊ณ ์˜ ์„ฑ๋Šฅ ํ‰๊ฐ€๋ฅผ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์— ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ๋‹ค์†Œ ๋ณด์ •ํ•  ํ•„์š”๋ฅผ ๋Š๋‚๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ณด์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ •๋ฐ€๋„์˜ ๋ถ„์ž๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด Ref์™€ ๋งค์นญํ•˜๋ฉฐ ์นด์šดํŠธํ•˜๋Š” ๊ณผ์ •์—์„œ Ca์˜ ์œ ๋‹ˆ๊ทธ๋žจ์ด ์ด๋ฏธ Ref์—์„œ ๋งค์นญ๋œ ์ ์ด ์žˆ์—ˆ๋Š”์ง€๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋“ค๊ณผ๋ฅผ ๊ณ ๋ คํ•œ ์ƒˆ๋กœ์šด ์นด์šดํŠธ ๋ฐฉ๋ฒ•์ด ํ•„์š” ์˜์ด ์œ ๋‹ˆ ๊ทธ๋žจ ์ˆ˜ Unigram Precision = Ref ๋“ค๊ณผ Ca๋ฅผ ๊ณ ๋ คํ•œ ์ƒˆ๋กœ์šด ์นด์šดํŠธ ๋ฐฉ๋ฒ•์ด ํ•„์š”! Ca์˜ ์ด ์œ ๋‹ˆ๊ทธ๋žจ ์ˆ˜ ์ •๋ฐ€๋„์˜ ๋ถ„์ž๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์นด์šดํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ฉ์‹œ๋‹ค. ์šฐ์„ , ์œ ๋‹ˆ๊ทธ๋žจ์ด ํ•˜๋‚˜์˜ Ref์—์„œ ์ตœ๋Œ€ ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ–ˆ๋Š”์ง€๋ฅผ ์นด์šดํŠธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์„ maximum reference count๋ฅผ ์ค„์ธ ์˜๋ฏธ์—์„œ Max_Ref_Count๋ผ๊ณ  ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. Max_Ref_Count๊ฐ€ ๊ธฐ์กด์˜ ๋‹จ์ˆœ ์นด์šดํŠธํ•œ ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ์—๋Š” ์ด ๊ฐ’์„ ์ตœ์ข… ์นด์šดํŠธ ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฐ€๋„์˜ ๋ถ„์ž ๊ณ„์‚ฐ์„ ์œ„ํ•œ ์ƒˆ๋กœ์šด ์นด์šดํŠธ ๋ฐฉ์‹์„ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o n c i = m n ( o n , M x R f C u t ) ์œ„์˜ ์นด์šดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„์ž๋ฅผ ๊ณ„์‚ฐํ•œ ์ •๋ฐ€๋„๋ฅผ ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„(Modified Unigram Precision)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜๊ฐ์œ ๋‹ˆ๊ทธ๋žจ์—๋Œ€ํ•ด์„์ˆ˜ํ–‰ํ•œ๊ฐ’์˜์ดํ•ฉ ์˜์ด ์œ ๋‹ˆ ๊ทธ๋žจ ์ˆ˜ Modified Unigram Precision = Ca์˜ ๊ฐ ์œ ๋‹ˆ๊ทธ๋žจ์— ๋Œ€ํ•ด o n c i ์„ ์ˆ˜ํ–‰ํ•œ ๊ฐ’์˜ ์ดํ•ฉ Ca์˜ ์ด ์œ ๋‹ˆ๊ทธ๋žจ ์ˆ˜ ๋ถ„๋ชจ์˜ ๊ฒฝ์šฐ์—๋Š” ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ Ca์˜ ๋ชจ๋“  ์œ ๋‹ˆ๊ทธ๋žจ์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ o n ํ•˜๊ณ  ๋ชจ๋‘ ํ•ฉํ•œ ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. Example 2๋ฅผ ๋ณผ๊นŒ์š”? the์˜ ๊ฒฝ์šฐ์—๋Š” Ref1์—์„œ ์ด ๋‘ ๋ฒˆ ๋“ฑ์žฅํ•˜์˜€์œผ๋ฏ€๋กœ, the์˜ ์นด์šดํŠธ๋Š” 2๋กœ ๋ณด์ •๋ฉ๋‹ˆ๋‹ค. Ca์˜ ๊ธฐ์กด ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 7 1 ์ด์—ˆ์œผ๋‚˜ ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 7 ์™€ ๊ฐ™์ด ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์˜ˆ๋กœ Example 1์—์„œ์˜ Ca1์˜ ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๋ฉด ๋ณด์ •๋˜๊ธฐ ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ 17 18 ์ด์ง€๋งŒ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ๊ณผ์ •์€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. Ca1์—์„œ the๋Š” 3๋ฒˆ ๋“ฑ์žฅํ•˜์ง€๋งŒ, Re2์™€ Ref3์—์„œ the๊ฐ€ 4๋ฒˆ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ 3์ด 4๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ the๋Š” 3์œผ๋กœ ์นด์šดํŠธ๋ฉ๋‹ˆ๋‹ค. the ์™ธ์— Ca1์˜ ๋ชจ๋“  ์œ ๋‹ˆ๊ทธ๋žจ์€ ์ „๋ถ€ 1๊ฐœ์”ฉ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ ๋ณด์ • ์ „๊ณผ ๋™์ผํ•˜๊ฒŒ ์นด์šดํŠธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ณด์ • ์ด์ „์˜ ์ •๋ฐ€๋„์™€ ๋™์ผํ•˜๊ฒŒ 17 18 ์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 3) ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„ (Modified Unigram Precision) ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ํŒŒ์ด์ฌ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ ๋‹ˆ๊ทธ๋žจ์„ ์นด์šดํŠธํ•˜๋Š” o n ํ•จ์ˆ˜์™€ o n c i ํ•จ์ˆ˜ ๋‘ ๊ฐ€์ง€ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ถ„๋ชจ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ o n ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋ถ„์ž๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ o n c i ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์œ ๋‹ˆ๊ทธ๋žจ์„ ๋‹จ์ˆœํžˆ o n ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ simple_count๋ผ๋Š” ์ด๋ฆ„์˜ ์•„๋ž˜ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. # ํ† ํฐํ™”๋œ ๋ฌธ์žฅ(tokens)์—์„œ n-gram์„ ์นด์šดํŠธ def simple_count(tokens, n): return Counter(ngrams(tokens, n)) ์œ„ ํ•จ์ˆ˜๋Š” ํ† ํฐํ™”๋œ ๋ฌธ์žฅ์„ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋ฌธ์žฅ ๋‚ด์˜ n-gram์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์€ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์ด๋ฏ€๋กœ ์นด์šดํŠธํ•˜๊ณ ์ž ํ•˜๋Š” n-gram์˜ ๋‹จ์œ„๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” simple_count ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ n์˜ ๊ฐ’์„ 1๋กœ ํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Example 1์˜ Ca1๋ฅผ ๊ฐ€์ ธ์™€ ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ค ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. candidate = "It is a guide to action which ensures that the military always obeys the commands of the party." tokens = candidate.split() # ํ† ํฐํ™” result = simple_count(tokens, 1) # n = 1์€ ์œ ๋‹ˆ๊ทธ๋žจ print('์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ :',result) ์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ : Counter({('the',): 3, ('It',): 1, ('is',): 1, ('a',): 1, ('guide',): 1, ('to',): 1, ('action',): 1, ('which',): 1, ('ensures',): 1, ('that',): 1, ('military',): 1, ('always',): 1, ('obeys',): 1, ('commands',): 1, ('of',): 1, ('party.',): 1}) ์œ„์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋ชจ๋“  ์œ ๋‹ˆ๊ทธ๋žจ์„ ์นด์šดํŠธํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์œ ๋‹ˆ๊ทธ๋žจ์ด 1๊ฐœ์”ฉ ์นด์šดํŠธ๋˜์—ˆ์œผ๋‚˜ ์œ ๋‹ˆ๊ทธ๋žจ the๋Š” ๋ฌธ์žฅ์—์„œ 3๋ฒˆ ๋“ฑ์žฅํ•˜์˜€์œผ๋ฏ€๋กœ ์œ ์ผํ•˜๊ฒŒ 3์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” Example 2์˜ Ca๋ฅผ ๊ฐ€์ง€๊ณ  ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•ด ๋ด…์‹œ๋‹ค. candidate = 'the the the the the the the' tokens = candidate.split() # ํ† ํฐํ™” result = simple_count(tokens, 1) # n = 1์€ ์œ ๋‹ˆ๊ทธ๋žจ print('์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ :',result) ์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ : Counter({('the',): 7}) simple_count ํ•จ์ˆ˜๋Š” ๋‹จ์ˆœ ์นด์šดํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ the์— ๋Œ€ํ•ด์„œ 7์ด๋ผ๋Š” ์นด์šดํŠธ ๊ฐ’์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. o n์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์œผ๋‹ˆ ์ด๋ฒˆ์—๋Š” o n c i ์„ ์•„๋ž˜์˜ count_clip ์ด๋ฆ„์„ ๊ฐ€์ง„ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. def count_clip(candidate, reference_list, n): # Ca ๋ฌธ์žฅ์—์„œ n-gram ์นด์šดํŠธ ca_cnt = simple_count(candidate, n) max_ref_cnt_dict = dict() for ref in reference_list: # Ref ๋ฌธ์žฅ์—์„œ n-gram ์นด์šดํŠธ ref_cnt = simple_count(ref, n) # ๊ฐ Ref ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋น„๊ตํ•˜์—ฌ n-gram์˜ ์ตœ๋Œ€ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ๊ณ„์‚ฐ. for n_gram in ref_cnt: if n_gram in max_ref_cnt_dict: max_ref_cnt_dict[n_gram] = max(ref_cnt[n_gram], max_ref_cnt_dict[n_gram]) else: max_ref_cnt_dict[n_gram] = ref_cnt[n_gram] return { # count_clip = min(count, max_ref_count) n_gram: min(ca_cnt.get(n_gram, 0), max_ref_cnt_dict.get(n_gram, 0)) for n_gram in ca_cnt } count_clip ํ•จ์ˆ˜๋Š” candidate ๋ฌธ์žฅ๊ณผ reference ๋ฌธ์žฅ๋“ค, ๊ทธ๋ฆฌ๊ณ  ์นด์šดํŠธ ๋‹จ์œ„๊ฐ€ ๋˜๋Š” n-gram์—์„œ์˜ n์˜ ๊ฐ’ ์ด ์„ธ ๊ฐ€์ง€๋ฅผ ์ธ์ž๋กœ ์ž…๋ ฅ๋ฐ›์•„์„œ o n c i ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์—ญ์‹œ๋‚˜ n=1๋กœ ํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ count_clip ํ•จ์ˆ˜ ๋‚ด๋ถ€์—๋Š” ๊ธฐ์กด์— ๊ตฌํ˜„ํ–ˆ๋˜ simple_count ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. o n c i ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” a _ e _ o n ๊ฐ’๊ณผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด o n ๊ฐ’์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. Example 2๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜๊ฐ€ ์ •์ƒ ์ž‘๋™๋˜๋Š”์ง€ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค. candidate = 'the the the the the the the' references = [ 'the cat is on the mat', 'there is a cat on the mat' ] result = count_clip(candidate.split(),list(map(lambda ref: ref.split(), references)),1) print('๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ :',result) ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์นด์šดํŠธ : {('the',): 2} ๋™์ผํ•œ ์˜ˆ์ œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ์œ„์˜ simple_count ํ•จ์ˆ˜๋Š” the๊ฐ€ 7๊ฐœ๋กœ ์นด์šดํŠธ๋˜์—ˆ๋˜ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ด๋ฒˆ์—๋Š” 2๊ฐœ๋กœ ์นด์šดํŠธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋‘ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ œ ๋ฌธ์žฅ์— ๋Œ€ํ•ด์„œ ๋ณด์ •๋œ ์ •๋ฐ€๋„๋ฅผ ์—ฐ์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ modified_precision๋ž€ ์ด๋ฆ„์˜ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. def modified_precision(candidate, reference_list, n): clip_cnt = count_clip(candidate, reference_list, n) total_clip_cnt = sum(clip_cnt.values()) # ๋ถ„์ž cnt = simple_count(candidate, n) total_cnt = sum(cnt.values()) # ๋ถ„๋ชจ # ๋ถ„๋ชจ๊ฐ€ 0์ด ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ if total_cnt == 0: total_cnt = 1 # ๋ถ„์ž : count_clip์˜ ํ•ฉ, ๋ถ„๋ชจ : ๋‹จ์ˆœ count์˜ ํ•ฉ ==> ๋ณด์ •๋œ ์ •๋ฐ€๋„ return (total_clip_cnt / total_cnt) result = modified_precision(candidate.split(), list(map(lambda ref: ref.split(), references)), n=1) print('๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„ :',result) ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„ : 0.2857142857142857 ์†Œ์ˆ˜ ๊ฐ’์ด ๋‚˜์˜ค๋Š”๋ฐ ์ด๋Š” 7 ์˜ ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์•ž์„œ ์œก์•ˆ์œผ๋กœ ๊ณ„์‚ฐํ–ˆ๋˜ Example 2์—์„œ Ca์˜ ๋ณด์ •๋œ ์ •๋ฐ€๋„์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜๊ณ , ์ง์ ‘ ๊ตฌํ˜„๊นŒ์ง€ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ์„ค๋ช…์—์„œ ์–ธ๊ธ‰ํ•˜๋Š” '์ •๋ฐ€๋„'๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณด์ •๋œ ์ •๋ฐ€๋„(Modified Precision)๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฐ€๋„๋ฅผ ๋ณด์ •ํ•จ์œผ๋กœ์จ Ca์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋‹จ์–ด ์ค‘๋ณต์— ๋Œ€ํ•œ ๋ฌธ์ œ์ ์€ ํ•ด๊ฒฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๊ฐ€ ๊ฐ€์ง€๋Š” ๋ณธ์งˆ์ ์ธ ๋ฌธ์ œ์ ์ด ์žˆ๊ธฐ์— ์œ ๋‹ˆ๊ทธ๋žจ์„ ๋„˜์–ด ๋ฐ”์ด ๊ทธ๋žจ, ํŠธ๋ผ์ด ๊ทธ๋žจ ๋“ฑ๊ณผ ๊ฐ™์ด n-gram์œผ๋กœ ํ™•์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ œ์ ์ด ๋ฌด์—‡์ธ์ง€ ์ดํ•ดํ•˜๊ณ , ์–ด๋–ป๊ฒŒ n-gram์œผ๋กœ ํ™•์žฅํ•˜๋Š”์ง€ ํ•™์Šตํ•ด ๋ด…์‹œ๋‹ค. 4) ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜๊ธฐ ์œ„ํ•ด์„œ n-gram์œผ๋กœ ํ™•์žฅํ•˜๊ธฐ BoW ํ‘œํ˜„๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ, ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์™€ ๊ฐ™์ด ๊ฐ ๋‹จ์–ด์˜ ๋นˆ๋„์ˆ˜๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฒฐ๊ตญ ๋‹จ์–ด์˜ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. Example 1์— Ca3์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์žฅ์„ ์ถ”๊ฐ€ํ•ด ๋ณด๊ณ  ๊ธฐ์กด์˜ Ca1๊ณผ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. Example 1 Candidate1 : It is a guide to action which ensures that the military always obeys the commands of the party. Candidate2 : It is to insure the troops forever hearing the activity guidebook that party direct. Candidate3 : the that military a is It guide ensures which to commands the of action obeys always party the. Reference1 : It is a guide to action that ensures that the military will forever heed Party commands. Reference2 : It is the guiding principle which guarantees the military forces always being under the command of the Party. Reference3 : It is the practical guide for the army always to heed the directions of the party. Ca3์€ ์‚ฌ์‹ค Ca1์—์„œ ๋ชจ๋“  ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์ˆœ์„œ๋ฅผ ๋žœ๋ค์œผ๋กœ ์„ž์€ ์‹ค์ œ ์˜์–ด ๋ฌธ๋ฒ•์— ๋งž์ง€ ์•Š์€ ๋ฌธ์žฅ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Ref 1, 2, 3๊ณผ ๋น„๊ตํ•˜์—ฌ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋ฅผ ์ ์šฉํ•˜๋ฉด Ca1๊ณผ Ca3์˜ ๋‘ ์ •๋ฐ€๋„๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” ์œ ๋‹ˆ๊ทธ๋žจ์˜ ์ˆœ์„œ๋ฅผ ์ „ํ˜€ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ๋Œ€์•ˆ์œผ๋กœ ๊ฐœ๋ณ„์ ์ธ ์œ ๋‹ˆ๊ทธ๋žจ/๋‹จ์–ด๋กœ์„œ ์นด์šดํŠธํ•˜๋Š” ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์—์„œ ๋‹ค์Œ์— ๋“ฑ์žฅํ•œ ๋‹จ์–ด๊นŒ์ง€ ํ•จ๊ป˜ ๊ณ ๋ คํ•˜์—ฌ ์นด์šดํŠธํ•˜๋„๋ก ์œ ๋‹ˆ๊ทธ๋žจ ์™ธ์—๋„ Bigram, Trigram, 4-gram ๋‹จ์œ„ ๋“ฑ์œผ๋กœ ๊ณ„์‚ฐํ•œ ์ •๋ฐ€๋„. ์ฆ‰, n-gram์„ ์ด์šฉํ•œ ์ •๋ฐ€๋„๋ฅผ ๋„์ž…ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด๋“ค ๊ฐ๊ฐ์€ ์นด์šดํŠธ ๋‹จ์œ„๋ฅผ 2๊ฐœ, 3๊ฐœ, 4๊ฐœ๋กœ ๋ณด๋Š๋ƒ์˜ ์ฐจ์ด๋กœ 2-gram Precision, 3-gram Precision, 4-gram Precision์ด๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ์˜๋ฏธ์ธ์ง€ ๋ฐ”์ด ๊ทธ๋žจ(Bigram) ๋‹จ์œ„๋กœ ์นด์šดํŠธํ•˜์—ฌ Example 1, 2์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„(Bigram Precision)๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ์ข€ ๋” ์‰ฌ์šด Example 2๋ถ€ํ„ฐ ๋ณผ๊นŒ์š”? Example 2 Candidate1 : the the the the the the the Candidate2 : the cat the cat on the mat Reference1 : the cat is on the mat Reference2 : there is a cat on the mat ์ดํ•ด๋ฅผ ๋•๊ณ ์ž Example 2์— Ca2๋ฅผ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. Ca2 ๋ฐ”์ด ๊ทธ๋žจ์˜ o n ์™€ o n c i ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฐ”์ด ๊ทธ๋žจ the cat cat the cat on on the the mat SUM o n 2 1 1 1 1 6 o n c i 1 0 1 1 1 4 ๊ฒฐ๊ณผ์ ์œผ๋กœ Ca2์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 6 ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๋‹น์—ฐํ•˜๊ฒŒ๋„ Ca1์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 0์ž…๋‹ˆ๋‹ค. Example 1์€ ์–ด๋–จ๊นŒ์š”? Example 1์—์„œ Ca1์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 10 17 ์ด๋ฉฐ, Ca2์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” 13 ์ž…๋‹ˆ๋‹ค. Ca1์—์„œ ๋‹จ์–ด์˜ ์ˆœ์„œ๋ฅผ ๋’ค์„ž์€ Ca3์˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๋Š” ๋…์ž๋ถ„๋“ค์˜ ์ˆ™์ œ๋กœ ๋‚จ๊น๋‹ˆ๋‹ค. ๋ณด์ •๋œ ์ •๋ฐ€๋„๋ฅผ ์‹์œผ๋กœ ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. n ์—์„œ ์€ n-gram์—์„œ์˜ ์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, ์•ž์„œ ๋ฐฐ์šด ๋ณด์ •๋œ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์˜ ์‹์„ ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. 1 โˆ‘ n g a โˆˆ a d d t C u t l p ( n g a) u i r m C n i a e C u t ( n g a) ์ด๋ฅผ n-gram์œผ๋กœ ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. n โˆ‘ - r m C n i a e C u t l p ( - r m ) n g a โˆˆ a d d t C u t ( - r m ) ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„์—์„œ๋Š” ์ด 1์ด๋ฏ€๋กœ 1 ๋กœ ํ‘œํ˜„ํ•˜์˜€์œผ๋‚˜, ์ผ๋ฐ˜ํ™”๋œ ์‹์—์„œ๋Š” n ์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๋ณด์ •๋œ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„ 2 , ๋ณด์ •๋œ ํŠธ๋ผ์ด ๊ทธ๋žจ ์ •๋ฐ€๋„ 3 ๋“ฑ์— ๋Œ€ํ•œ ํŒŒ์ด์ฌ ์‹ค์Šต์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค n ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ ๋ณ„๋„๋กœ ๋‹ค์‹œ ๊ตฌํ˜„ํ•  ํ•„์š”๋Š” ์—†๋Š”๋ฐ, ์•ž์„œ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜ simple_count, count_clip, modified_precision์€ ๋ชจ๋‘ n-gram์˜ n์„ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋ฐ›์œผ๋ฏ€๋กœ, n์„ 1๋Œ€์‹  ๋‹ค๋ฅธ ๊ฐ’์„ ๋„ฃ์–ด์„œ ์‹ค์Šตํ•ด ๋ณด๋ฉด ๋ฐ”์ด ๊ทธ๋žจ, ํŠธ๋ผ์ด ๊ทธ๋žจ ๋“ฑ์— ๋Œ€ํ•ด์„œ๋„ ๋ณด์ •๋œ ์ •๋ฐ€๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. n-gram ์ •๋ฐ€๋„ ์‹์„ ์ดํ•ดํ•˜์˜€๋‹ค๋ฉด BLEU์˜ ์ตœ์ข… ์‹๊นŒ์ง€ ๋‹ค ์™”์Šต๋‹ˆ๋‹ค. BLEU๋Š” ๋ณด์ •๋œ ์ •๋ฐ€๋„ 1 p, . , n ๋ฅผ ๋ชจ๋‘ ์กฐํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ชจ๋‘ ์กฐํ•ฉํ•œ BLEU์˜ ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. L U e p ( n 1 w log p) n : ๊ฐ gram์˜ ๋ณด์ •๋œ ์ •๋ฐ€๋„์ž…๋‹ˆ๋‹ค. : n-gram์—์„œ์˜ ์ตœ๋Œ€ ์ˆซ์ž์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต์€ 4์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด 4๋ผ๋Š” ๊ฒƒ์€ 1 p, 3 p๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. n : ๊ฐ gram์˜ ๋ณด์ •๋œ ์ •๋ฐ€๋„์— ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ€์ค‘์น˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€์ค‘์น˜์˜ ํ•ฉ์€ 1๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด 4๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, 1 p, 3 p์— ๋Œ€ํ•ด์„œ ๋™์ผํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ์ฃผ๊ณ ์ž ํ•œ๋‹ค๋ฉด ๋ชจ๋‘ 0.25๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BLEU์˜ ์ตœ์ข…์‹์— ๊ฑฐ์˜ ๋‹ค ๋„๋‹ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์—ฌ์ „ํžˆ ์œ„์˜ BLEU ์‹์—๋„ ๋ฌธ์ œ์ ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 5) ์งง์€ ๋ฌธ์žฅ ๊ธธ์ด์— ๋Œ€ํ•œ ํŽ˜๋„ํ‹ฐ(Brevity Penalty) n-gram์œผ๋กœ ๋‹จ์–ด์˜ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ์—ฌ์ „ํžˆ ๋‚จ์•„์žˆ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”๋ฐ, ๋ฐ”๋กœ Ca์˜ ๊ธธ์ด์— BLEU์˜ ์ ์ˆ˜๊ฐ€ ๊ณผํ•œ ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด Example 1์— ๋‹ค์Œ์˜ Ca๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Example 1 Candidate4 : it is ์ด ๋ฌธ์žฅ์€ ์œ ๋‹ˆ๊ทธ๋žจ ์ •๋ฐ€๋„๋‚˜ ๋ฐ”์ด ๊ทธ๋žจ ์ •๋ฐ€๋„๊ฐ€ ๊ฐ๊ฐ 2 1๋กœ ๋‘ ์ •๋ฐ€๋„ ๋ชจ๋‘ 1์ด๋ผ๋Š” ๋†’์€ ์ •๋ฐ€๋„๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ด๊ณผ๊ฐ™์ด ์ œ๋Œ€๋กœ ๋œ ๋ฒˆ์—ญ์ด ์•„๋‹˜์—๋„ ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ ์งง๋‹ค๋Š” ์ด์œ ๋กœ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์€ ์ด์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ Ca๊ฐ€ Ref๋ณด๋‹ค ๋ฌธ์žฅ์˜ ๊ธธ์ด๊ฐ€ ์งง์€ ๊ฒฝ์šฐ์—๋Š” ์ ์ˆ˜์— ํŽ˜๋„ํ‹ฐ๋ฅผ ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ(Brevity Penalty)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. (์ง์—ญํ•˜๋ฉด ์งง์Œ ํŽ˜๋„ํ‹ฐ) ์ด์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šฐ๊ธฐ ์ „์—, ๋งŒ์•ฝ ๋ฐ˜๋Œ€๋กœ Ca์˜ ๊ธธ์ด๊ฐ€ Ref๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ์—๋„ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š”์ง€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Example 3 Candidate 1: I always invariably perpetually do. Candidate 2: I always do. Reference 1: I always do. Reference 2: I invariably do. Reference 3: I perpetually do. Example 3์—์„œ Ca1์€ ๊ฐ€์žฅ ๋งŽ์€ ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ Ca2๋ณด๋‹ค ์ข‹์ง€ ๋ชปํ•œ ๋ฒˆ์—ญ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด Ref์˜ ๋‹จ์–ด๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•œ ๊ฒƒ์ด ๊ผญ ์ข‹์€ ๋ฒˆ์—ญ์ด๋ผ๋Š” ์˜๋ฏธ๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‹คํ–‰ํžˆ๋„ ์œ„์™€ ๊ฐ™์ด Ca์˜ ๊ธธ์ด๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ Ref๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ์—๋Š” BLEU ์ˆ˜์‹์—์„œ ์ •๋ฐ€๋„๋ฅผ n-gram์œผ๋กœ ํ™•์žฅํ•˜์—ฌ ๋ฐ”์ด ๊ทธ๋žจ, ํŠธ๋ผ์ด ๊ทธ๋žจ ์ •๋ฐ€๋„ ๋“ฑ์„ ๋ชจ๋‘ ๊ณ„์‚ฐ์— ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ์ด๋ฏธ ํŽ˜๋„ํ‹ฐ๋ฅผ ๋ฐ›๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ, ์ด ๊ฒฝ์šฐ๊นŒ์ง€ ๊ณ ๋ คํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ Ref๋ณด๋‹ค Ca์˜ ๊ธธ์ด๊ฐ€ ์งง์„ ๊ฒฝ์šฐ์— ํŽ˜๋„ํ‹ฐ๋ฅผ ์ฃผ๋Š” ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ์˜ ์ด์•ผ๊ธฐ๋กœ ๋Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ๋Š” ์•ž์„œ ๋ฐฐ์šด BLEU์˜ ์‹์— ๊ณฑํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ๋ฅผ ์ค„์—ฌ์„œ P ๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ, ์ตœ์ข… BLEU์˜ ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. L U B ร— x ( n 1 w log p) ์œ„์˜ ์ˆ˜์‹์€ ํŽ˜๋„ํ‹ฐ๋ฅผ ์ค„ ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” P ์˜ ๊ฐ’์ด 1์ด์–ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐ˜์˜ํ•œ P ์˜ ์ˆ˜์‹์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. P { if c r ( โˆ’ / ) if c r : Candidate์˜ ๊ธธ์ด : Candidate์™€ ๊ฐ€์žฅ ๊ธธ์ด ์ฐจ์ด๊ฐ€ ์ž‘์€ Reference์˜ ๊ธธ์ด Ref๊ฐ€ 1๊ฐœ๋ผ๋ฉด Ca์™€ Ref์˜ ๋‘ ๋ฌธ์žฅ์˜ ๊ธธ์ด๋งŒ์„ ๊ฐ€์ง€๊ณ  ๊ณ„์‚ฐํ•˜๋ฉด ๋˜๊ฒ ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” Ref๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์€ ๋ชจ๋“  Ref๋“ค ์ค‘์—์„œ Ca์™€ ๊ฐ€์žฅ ๊ธธ์ด ์ฐจ์ด๊ฐ€ ์ž‘์€ Ref์˜ ๊ธธ์ด๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์„ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. # Ca ๊ธธ์ด์™€ ๊ฐ€์žฅ ๊ทผ์ ‘ํ•œ Ref์˜ ๊ธธ์ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜ def closest_ref_length(candidate, reference_list): ca_len = len(candidate) # ca ๊ธธ์ด ref_lens = (len(ref) for ref in reference_list) # Ref๋“ค์˜ ๊ธธ์ด # ๊ธธ์ด ์ฐจ์ด๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” Ref๋ฅผ ์ฐพ์•„์„œ Ref์˜ ๊ธธ์ด๋ฅผ ๋ฆฌํ„ด closest_ref_len = min(ref_lens, key=lambda ref_len: (abs(ref_len - ca_len), ref_len)) return closest_ref_len ๋งŒ์•ฝ Ca์™€ ๊ธธ์ด๊ฐ€ ์ •ํ™•ํžˆ ๋™์ผํ•œ Ref๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ธธ์ด ์ฐจ์ด๊ฐ€ 0์ธ ์ตœ๊ณ  ์ˆ˜์ค€์˜ ๋งค์น˜(best match length)์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋งŒ์•ฝ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธธ์ด์˜ Ref์ด์ง€๋งŒ Ca์™€ ๊ธธ์ด ์ฐจ์ด๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ์—๋Š” ๋” ์ž‘์€ ๊ธธ์ด์˜ Ref๋ฅผ ํƒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Ca๊ฐ€ ๊ธธ์ด๊ฐ€ 10์ธ๋ฐ, Ref 1, 2๊ฐ€ ๊ฐ๊ฐ 9์™€ 11์ด๋ผ๋ฉด ๊ธธ์ด ์ฐจ์ด๋Š” ๋™์ผํ•˜๊ฒŒ 1๋ฐ–์— ๋‚˜์ง€ ์•Š์ง€๋งŒ 9๋ฅผ ํƒํ•ฉ๋‹ˆ๋‹ค. closest_ref_length ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์„ ๊ตฌํ–ˆ๋‹ค๋ฉด, P ๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜ brevity_penalty๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. def brevity_penalty(candidate, reference_list): ca_len = len(candidate) ref_len = closest_ref_length(candidate, reference_list) if ca_len > ref_len: return 1 # candidate๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด BP = 0 โ†’ BLEU = 0.0 elif ca_len == 0 : return 0 else: return np.exp(1 - ref_len/ca_len) ์œ„ ํ•จ์ˆ˜๋Š” ์•ž์„œ ๋ฐฐ์šด P ์˜ ์ˆ˜์‹์ฒ˜๋Ÿผ ๊ฐ€ ๋ณด๋‹ค ํด ๊ฒฝ์šฐ์—๋Š” 1์„ ๋ฆฌํ„ดํ•˜๊ณ , ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” 1 r c ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ BLEU ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜ bleu_score๋ฅผ ๊ตฌํ˜„ํ•ด ๋ด…์‹œ๋‹ค. def bleu_score(candidate, reference_list, weights=[0.25, 0.25, 0.25, 0.25]): bp = brevity_penalty(candidate, reference_list) # ๋ธŒ๋ ˆ ๋ฒ„ํ‹ฐ ํŽ˜๋„ํ‹ฐ, BP p_n = [modified_precision(candidate, reference_list, n=n) for n, _ in enumerate(weights, start=1)] # p1, p2, p3, ..., pn score = np.sum([w_i * np.log(p_i) if p_i != 0 else 0 for w_i, p_i in zip(weights, p_n)]) return bp * np.exp(score) ์œ„์˜ bleu_score ํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ์ด 4์— ๊ฐ gram์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๋Š” ๋™์ผํ•˜๊ฒŒ 0.25๋ผ ์ฃผ์–ด์ง„๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•จ์ˆ˜ ๋‚ด์—์„œ๋Š” P ๋ฅผ ๊ตฌํ•˜๊ณ  bp์—, 1 p, . , n ๋ฅผ ๊ตฌํ•˜์—ฌ p_n์— ์ €์žฅํ•˜๋„๋ก ๊ตฌํ˜„๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•ž์„œ ๋ฐฐ์šด BLEU์˜ ์‹์— ๋”ฐ๋ผ ์ถ”๊ฐ€ ์—ฐ์‚ฐํ•˜์—ฌ ์ตœ์ข… ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์œ„ ํ•จ์ˆ˜๊ฐ€ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•ž์„œ ๊ตฌํ˜„ํ•œ simple_count, count_clip, modified_precision, brevity_penalty 4๊ฐœ์˜ ํ•จ์ˆ˜ ๋˜ํ•œ ๋ชจ๋‘ ๊ตฌํ˜„๋ผ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๊ตฌํ˜„ํ•œ BLEU ์ฝ”๋“œ๋กœ ๊ณ„์‚ฐ๋œ ์ ์ˆ˜์™€ NLTK ํŒจํ‚ค์ง€์— ์ด๋ฏธ ๊ตฌํ˜„๋ผ ์žˆ๋Š” BLEU ์ฝ”๋“œ๋กœ ๊ณ„์‚ฐ๋œ ์ ์ˆ˜๋ฅผ ๋น„๊ตํ•ด ๋ด…์‹œ๋‹ค. 2. NLTK๋ฅผ ์‚ฌ์šฉํ•œ BLEU ์ธก์ •ํ•˜๊ธฐ ํŒŒ์ด์ฌ์—์„œ๋Š” NLTK ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ BLEU๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. import nltk.translate.bleu_score as bleu candidate = 'It is a guide to action which ensures that the military always obeys the commands of the party' references = [ 'It is a guide to action that ensures that the military will forever heed Party commands', 'It is the guiding principle which guarantees the military forces always being under the command of the Party', 'It is the practical guide for the army always to heed the directions of the party' ] print('์‹ค์Šต ์ฝ”๋“œ์˜ BLEU :',bleu_score(candidate.split(),list(map(lambda ref: ref.split(), references)))) print('ํŒจํ‚ค์ง€ NLTK์˜ BLEU :',bleu.sentence_bleu(list(map(lambda ref: ref.split(), references)),candidate.split())) ์‹ค์Šต ์ฝ”๋“œ์˜ BLEU : 0.5045666840058485 ํŒจํ‚ค์ง€ NLTK์˜ BLEU : 0.5045666840058485 17. [NLP ๊ณ ๊ธ‰ ] - ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์‹ ๊ฒฝ๋ง๋“ค์˜ ์„ฑ๋Šฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด์ž, ์ด์ œ๋Š” AI ๋ถ„์•ผ์—์„œ ๋Œ€์„ธ ๋ชจ๋“ˆ๋กœ์„œ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ๋Œ€ํ•ด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. 17-01 ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜ (Attention Mechanism) ์•ž์„œ ๋ฐฐ์šด seq2seq ๋ชจ๋ธ์€ ์ธ์ฝ”๋”์—์„œ ์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ผ๋Š” ํ•˜๋‚˜์˜ ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ ๋ฒกํ„ฐ ํ‘œํ˜„์œผ๋กœ ์••์ถ•ํ•˜๊ณ , ๋””์ฝ”๋”๋Š” ์ด ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ์ถœ๋ ฅ ์‹œํ€€์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ƒˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ RNN์— ๊ธฐ๋ฐ˜ํ•œ seq2seq ๋ชจ๋ธ์—๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ์งธ, ํ•˜๋‚˜์˜ ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ ๋ฒกํ„ฐ์— ๋ชจ๋“  ์ •๋ณด๋ฅผ ์••์ถ•ํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ ์ •๋ณด ์†์‹ค์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋‘˜์งธ, RNN์˜ ๊ณ ์งˆ์ ์ธ ๋ฌธ์ œ์ธ ๊ธฐ์šธ๊ธฐ ์†Œ์‹ค(vanishing gradient) ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ด๋Š” ๊ธฐ๊ณ„ ๋ฒˆ์—ญ ๋ถ„์•ผ์—์„œ ์ž…๋ ฅ ๋ฌธ์žฅ์ด ๊ธธ๋ฉด ๋ฒˆ์—ญ ํ’ˆ์งˆ์ด ๋–จ์–ด์ง€๋Š” ํ˜„์ƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•œ ๋Œ€์•ˆ์œผ๋กœ ์ž…๋ ฅ ์‹œํ€€์Šค๊ฐ€ ๊ธธ์–ด์ง€๋ฉด ์ถœ๋ ฅ ์‹œํ€€์Šค์˜ ์ •ํ™•๋„๊ฐ€ ๋–จ์–ด์ง€๋Š” ๊ฒƒ์„ ๋ณด์ •ํ•ด ์ฃผ๊ธฐ ์œ„ํ•œ ๋“ฑ์žฅํ•œ ๊ธฐ๋ฒ•์ธ ์–ดํ…์…˜(attention)์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 1. ์–ดํ…์…˜(Attention)์˜ ์•„์ด๋””์–ด ์–ดํ…์…˜์˜ ๊ธฐ๋ณธ ์•„์ด๋””์–ด๋Š” ๋””์ฝ”๋”์—์„œ ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋งค ์‹œ์ (time step)๋งˆ๋‹ค, ์ธ์ฝ”๋”์—์„œ์˜ ์ „์ฒด ์ž…๋ ฅ ๋ฌธ์žฅ์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ฐธ๊ณ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋‹จ, ์ „์ฒด ์ž…๋ ฅ ๋ฌธ์žฅ์„ ์ „๋ถ€ ๋‹ค ๋™์ผํ•œ ๋น„์œจ๋กœ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํ•ด๋‹น ์‹œ์ ์—์„œ ์˜ˆ์ธกํ•ด์•ผ ํ•  ๋‹จ์–ด์™€ ์—ฐ๊ด€์ด ์žˆ๋Š” ์ž…๋ ฅ ๋‹จ์–ด ๋ถ€๋ถ„์„ ์ข€ ๋” ์ง‘์ค‘(attention) ํ•ด์„œ ๋ณด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 2. ์–ดํ…์…˜ ํ•จ์ˆ˜(Attention Function) ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์–ธ๊ธ‰ํ•˜๊ธฐ ์ „์— ์ปดํ“จํ„ฐ๊ณตํ•™์˜ ๋งŽ์€ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” Key-Value๋กœ ๊ตฌ์„ฑ๋˜๋Š” ์ž๋ฃŒํ˜•์— ๋Œ€ํ•ด์„œ ์ž ๊น ์–ธ๊ธ‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น, ์ด ์ฑ…์˜ ์ฃผ ์–ธ์–ด๋กœ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ด์ฌ์—๋„ Key-Value๋กœ ๊ตฌ์„ฑ๋˜๋Š” ์ž๋ฃŒํ˜•์ธ ๋”•์…”๋„ˆ๋ฆฌ(Dict) ์ž๋ฃŒํ˜•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์€ ํ‚ค(Key)์™€ ๊ฐ’(Value)์ด๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š”๋ฐ, ํ‚ค๋ฅผ ํ†ตํ•ด์„œ ๋งคํ•‘๋œ ๊ฐ’์„ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. # ํŒŒ์ด์ฌ์˜ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์„ ์„ ์–ธ # ํ‚ค(Key) : ๊ฐ’(value)์˜<NAME>์œผ๋กœ ํ‚ค์™€ ๊ฐ’์˜ ์Œ(Pair)์„ ์„ ์–ธํ•œ๋‹ค. dict = {"2017" : "Transformer", "2018" : "BERT"} ์œ„์˜ ์ž๋ฃŒํ˜•์—์„œ 2017์€ ํ‚ค์— ํ•ด๋‹น๋˜๋ฉฐ, Transformer๋Š” 2017์˜ ํ‚ค์™€ ๋งคํ•‘๋˜๋Š” ๊ฐ’์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๊ทธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 2018์€ ํ‚ค์— ํ•ด๋‹น๋˜๋ฉฐ, BERT๋Š” 2018์ด๋ผ๋Š” ํ‚ค์™€ ๋งคํ•‘๋˜๋Š” ๊ฐ’์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. print(dict["2017"]) #2017์ด๋ผ๋Š” ํ‚ค์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์„ ์ถœ๋ ฅ Transformer print(dict["2018"]) #2018์ด๋ผ๋Š” ํ‚ค์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์„ ์ถœ๋ ฅ BERT Key-Value ์ž๋ฃŒํ˜•์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ๊ฐ€์ง€๊ณ  ์–ดํ…์…˜ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜์„ ํ•จ์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์ฃผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. Attention(Q, K, V) = Attention Value ์–ดํ…์…˜ ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ '์ฟผ๋ฆฌ(Query)'์— ๋Œ€ํ•ด์„œ ๋ชจ๋“  'ํ‚ค(Key)'์™€์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ตฌํ•ด๋‚ธ ์ด ์œ ์‚ฌ๋„๋ฅผ ํ‚ค์™€ ๋งคํ•‘๋˜์–ด์žˆ๋Š” ๊ฐ๊ฐ์˜ '๊ฐ’(Value)'์— ๋ฐ˜์˜ํ•ด ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ ์‚ฌ๋„๊ฐ€ ๋ฐ˜์˜๋œ '๊ฐ’(Value)'์„ ๋ชจ๋‘ ๋”ํ•ด์„œ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ด๋ฅผ ์–ดํ…์…˜ ๊ฐ’(Attention Value)์ด๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ๋ฐฐ์šฐ๊ฒŒ ๋˜๋Š” seq2seq + ์–ดํ…์…˜ ๋ชจ๋ธ์—์„œ Q, K, V์— ํ•ด๋‹น๋˜๋Š” ๊ฐ๊ฐ์˜ Query, Keys, Values๋Š” ๊ฐ๊ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Q = Query : t ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ K = Keys : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค V = Values : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค ๊ฐ„๋‹จํ•œ ์–ดํ…์…˜ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์–ดํ…์…˜์„ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 3. ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜(Dot-Product Attention) ์–ดํ…์…˜์€ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋Š”๋ฐ ๊ทธ์ค‘์—์„œ๋„ ๊ฐ€์žฅ ์ˆ˜์‹์ ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์ˆ˜์‹์„ ์ ์šฉํ•œ ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜(Dot-Product Attention)์„ ํ†ตํ•ด ์–ดํ…์…˜์„ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. seq2seq์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์–ดํ…์…˜ ์ค‘์—์„œ ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜๊ณผ ๋‹ค๋ฅธ ์–ดํ…์…˜์˜ ์ฐจ์ด๋Š” ์ฃผ๋กœ ์ค‘๊ฐ„ ์ˆ˜์‹์˜ ์ฐจ์ด๋กœ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ž์ฒด๋Š” ๊ฑฐ์˜ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ๋””์ฝ”๋”์˜ ์„ธ ๋ฒˆ์งธ LSTM ์…€์—์„œ ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•  ๋•Œ, ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ LSTM ์…€์€ ์ด๋ฏธ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ํ†ตํ•ด je์™€ suis๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณค๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ๋Œ€ํ•ด ์ƒ์„ธํžˆ ์„ค๋ช…ํ•˜๊ธฐ ์ „์— ์œ„์˜ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์ „์ฒด์ ์ธ ๊ฐœ์š”๋งŒ ์ดํ•ดํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์„ธ ๋ฒˆ์งธ LSTM ์…€์€ ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ธ์ฝ”๋”์˜ ๋ชจ๋“  ์ž…๋ ฅ ๋‹จ์–ด๋“ค์˜ ์ •๋ณด๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ฐธ๊ณ ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ค‘๊ฐ„ ๊ณผ์ •์— ๋Œ€ํ•œ ์„ค๋ช…์€ ํ˜„์žฌ๋Š” ์ƒ๋žตํ•˜๊ณ  ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•  ๊ฒƒ์€ ์ธ์ฝ”๋”์˜ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‚˜์˜จ ๊ฒฐ๊ด๊ฐ’์€ I, am, a, student ๋‹จ์–ด ๊ฐ๊ฐ์ด ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•  ๋•Œ ์–ผ๋งˆ๋‚˜ ๋„์›€์ด ๋˜๋Š”์ง€์˜ ์ •๋„๋ฅผ ์ˆ˜์น˜ํ™”ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ๋นจ๊ฐ„ ์ง์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๋กœ ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜์˜ ๊ฒฐ๊ด๊ฐ’์˜ ํฌ๊ธฐ๋ฅผ ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ง์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๊ฐ€ ํด์ˆ˜๋ก ๋„์›€์ด ๋˜๋Š” ์ •๋„์˜ ํฌ๊ธฐ๊ฐ€ ํฝ๋‹ˆ๋‹ค. ๊ฐ ์ž…๋ ฅ ๋‹จ์–ด๊ฐ€ ๋””์ฝ”๋”์˜ ์˜ˆ์ธก์— ๋„์›€์ด ๋˜๋Š” ์ •๋„๊ฐ€ ์ˆ˜์น˜ํ™”ํ•˜์—ฌ ์ธก์ •๋˜๋ฉด ์ด๋ฅผ ํ•˜๋‚˜์˜ ์ •๋ณด๋กœ ๋‹ด์•„์„œ ๋””์ฝ”๋”๋กœ ์ „์†ก๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ๋Š” ์ดˆ๋ก์ƒ‰ ์‚ผ๊ฐํ˜•์ด ์ด์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ, ๋””์ฝ”๋”๋Š” ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ๋” ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•  ํ™•๋ฅ ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค. ์ข€ ๋” ์ƒ์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1) ์–ดํ…์…˜ ์Šค์ฝ”์–ด(Attention Score)๋ฅผ ๊ตฌํ•œ๋‹ค. ์ธ์ฝ”๋”์˜ ์‹œ์ (time step)์„ ๊ฐ๊ฐ 1, 2, ... N์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ ๊ฐ๊ฐ 1 h, ... N ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๋””์ฝ”๋”์˜ ํ˜„์žฌ ์‹œ์ (time step) t์—์„œ์˜ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ t ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๋˜ํ•œ ์—ฌ๊ธฐ์„œ๋Š” ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›์ด ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์˜ ๊ฒฝ์šฐ์—๋Š” ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ๋™์ผํ•˜๊ฒŒ ์ฐจ์›์ด 4์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ์ฒซ๊ฑธ์Œ์ธ ์–ดํ…์…˜ ์Šค์ฝ”์–ด(Attention score)์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šฐ๊ธฐ ์ „์—, ์ด์ „ ์ฑ•ํ„ฐ ๋ฐฐ์› ๋˜ ๋””์ฝ”๋”์˜ ํ˜„์žฌ ์‹œ์  t์—์„œ ํ•„์š”ํ•œ ์ž…๋ ฅ๊ฐ’์„ ๋‹ค์‹œ ์ƒ๊ธฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹œ์  t์—์„œ ์ถœ๋ ฅ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋””์ฝ”๋”์˜ ์…€์€ ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’์„ ํ•„์š”๋กœ ํ•˜๋Š”๋ฐ, ๋ฐ”๋กœ ์ด์ „ ์‹œ์ ์ธ t-1์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์ด์ „ ์‹œ์  t-1์— ๋‚˜์˜จ ์ถœ๋ ฅ ๋‹จ์–ด์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ๋Š” ์ถœ๋ ฅ ๋‹จ์–ด ์˜ˆ์ธก์— ๋˜ ๋‹ค๋ฅธ ๊ฐ’์„ ํ•„์š”๋กœ ํ•˜๋Š”๋ฐ ๋ฐ”๋กœ ์–ดํ…์…˜ ๊ฐ’(Attention Value)์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐ’์ž…๋‹ˆ๋‹ค. t ๋ฒˆ์งธ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•œ ์–ดํ…์…˜ ๊ฐ’์„ t ์ด๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๊ฐ’์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐœ๋…์ด ๋“ฑ์žฅํ•œ ๋งŒํผ, ์–ดํ…์…˜ ๊ฐ’์ด ํ˜„์žฌ ์‹œ์  t์—์„œ์˜ ์ถœ๋ ฅ ์˜ˆ์ธก์— ๊ตฌ์ฒด์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๋ฐ˜์˜๋˜๋Š”์ง€๋Š” ๋’ค์—์„œ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ๋ฐฐ์šฐ๋Š” ๋ชจ๋“  ๊ณผ์ •์€ t ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ์ •์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์—ฌ์ •์˜ ์ฒซ๊ฑธ์Œ์€ ๋ฐ”๋กœ ์–ดํ…์…˜ ์Šค์ฝ”์–ด(Attention Score)๋ฅผ ๊ตฌํ•˜๋Š” ์ผ์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ž€ ํ˜„์žฌ ๋””์ฝ”๋”์˜ ์‹œ์  t์—์„œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด, ์ธ์ฝ”๋”์˜ ๋ชจ๋“  ์€๋‹‰ ์ƒํƒœ ๊ฐ๊ฐ์ด ๋””์ฝ”๋”์˜ ํ˜„์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ t ์™€ ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์Šค์ฝ”์–ด ๊ฐ’์ž…๋‹ˆ๋‹ค. ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์—์„œ๋Š” ์ด ์Šค์ฝ”์–ด ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด t ๋ฅผ ์ „์น˜(transpose) ํ•˜๊ณ  ๊ฐ ์€๋‹‰ ์ƒํƒœ์™€ ๋‚ด์ (dot product)์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ชจ๋“  ์–ดํ…์…˜ ์Šค์ฝ”์–ด ๊ฐ’์€ ์Šค์นผ๋ผ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด t ๊ณผ ์ธ์ฝ”๋”์˜ i ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด์˜ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. c r ( t h) s T i t ์™€ ์ธ์ฝ”๋”์˜ ๋ชจ๋“  ์€๋‹‰ ์ƒํƒœ์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด์˜ ๋ชจ์Œ ๊ฐ’์„ t ๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. t ์˜ ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. t [ t h, . , t h ] 2) ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์–ดํ…์…˜ ๋ถ„ํฌ(Attention Distribution)๋ฅผ ๊ตฌํ•œ๋‹ค. t ์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ, ๋ชจ๋“  ๊ฐ’์„ ํ•ฉํ•˜๋ฉด 1์ด ๋˜๋Š” ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ์–ป์–ด๋ƒ…๋‹ˆ๋‹ค. ์ด๋ฅผ ์–ดํ…์…˜ ๋ถ„ํฌ(Attention Distribution)๋ผ๊ณ  ํ•˜๋ฉฐ, ๊ฐ๊ฐ์˜ ๊ฐ’์€ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜(Attention Weight)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ์–ป์€ ์ถœ๋ ฅ๊ฐ’์ธ I, am, a, student์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ๊ฐ 0.1, 0.4, 0.1, 0.4๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด๋“ค์˜ ํ•ฉ์€ 1์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์€ ๊ฐ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์—์„œ์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์˜ ํฌ๊ธฐ๋ฅผ ์ง์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๋ฅผ ํ†ตํ•ด ์‹œ๊ฐํ™”ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜๊ฐ€ ํด์ˆ˜๋ก ์ง์‚ฌ๊ฐํ˜•์ด ํฝ๋‹ˆ๋‹ค. ๋””์ฝ”๋”์˜ ์‹œ์  t์—์„œ์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์˜ ๋ชจ์Œ ๊ฐ’์ธ ์–ดํ…์…˜ ๋ถ„ํฌ๋ฅผ t ์ด๋ผ๊ณ  ํ•  ๋•Œ, t ์„ ์‹์œผ๋กœ ์ •์˜ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. t s f m x ( t ) 3) ๊ฐ ์ธ์ฝ”๋”์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์™€ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ฐ€์ค‘ ํ•ฉํ•˜์—ฌ ์–ดํ…์…˜ ๊ฐ’(Attention Value)์„ ๊ตฌํ•œ๋‹ค. ์ด์ œ ์ง€๊ธˆ๊นŒ์ง€ ์ค€๋น„ํ•ด์˜จ ์ •๋ณด๋“ค์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜์˜ ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜ ๊ฐ’๋“ค์„ ๊ณฑํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ ๋ชจ๋‘ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด ๊ฐ€์ค‘ํ•ฉ(Weighted Sum)์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์–ดํ…์…˜์˜ ์ตœ์ข… ๊ฒฐ๊ณผ. ์ฆ‰, ์–ดํ…์…˜ ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ๊ฐ’์ธ ์–ดํ…์…˜ ๊ฐ’(Attention Value) t ์— ๋Œ€ํ•œ ์‹์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. t โˆ‘ = N i h ์ด๋Ÿฌํ•œ ์–ดํ…์…˜ ๊ฐ’ t ์€ ์ข…์ข… ์ธ์ฝ”๋”์˜ ๋ฌธ๋งฅ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜์—ฌ, ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ(context vector)๋ผ๊ณ ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. ์•ž์„œ ๋ฐฐ์šด ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ seq2seq์—์„œ๋Š” ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๊ฒƒ๊ณผ ๋Œ€์กฐ๋ฉ๋‹ˆ๋‹ค. 4) ์–ดํ…์…˜ ๊ฐ’๊ณผ ๋””์ฝ”๋”์˜ t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.(Concatenate) ์–ดํ…์…˜ ํ•จ์ˆ˜์˜ ์ตœ์ข… ๊ฐ’์ธ ์–ดํ…์…˜ ๊ฐ’ t ์„ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์–ดํ…์…˜ ๊ฐ’์ด ๊ตฌํ•ด์ง€๋ฉด ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ t s ์™€ ๊ฒฐํ•ฉ(concatenate) ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ t ๋ผ๊ณ  ์ •์˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด t y ์˜ˆ์ธก ์—ฐ์‚ฐ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ธ์ฝ”๋”๋กœ๋ถ€ํ„ฐ ์–ป์€ ์ •๋ณด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ^ ๋ฅผ ์ข€ ๋” ์ž˜ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. 5) ์ถœ๋ ฅ์ธต ์—ฐ์‚ฐ์˜ ์ž…๋ ฅ์ด ๋˜๋Š” ~๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ๋…ผ๋ฌธ์—์„œ๋Š” t ๋ฅผ ๋ฐ”๋กœ ์ถœ๋ ฅ์ธต์œผ๋กœ ๋ณด๋‚ด๊ธฐ ์ „์— ์‹ ๊ฒฝ๋ง ์—ฐ์‚ฐ์„ ํ•œ ๋ฒˆ ๋” ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ๊ณผ ๊ณฑํ•œ ํ›„์— ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋„๋ก ํ•˜์—ฌ ์ถœ๋ ฅ์ธต ์—ฐ์‚ฐ์„ ์œ„ํ•œ ์ƒˆ๋กœ์šด ๋ฒกํ„ฐ์ธ ~๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” seq2seq์—์„œ๋Š” ์ถœ๋ ฅ์ธต์˜ ์ž…๋ ฅ์ด t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ธ t ์˜€๋˜ ๋ฐ˜๋ฉด, ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ๋Š” ์ถœ๋ ฅ์ธต์˜ ์ž…๋ ฅ์ด ~ ๊ฐ€ ๋˜๋Š” ์…ˆ์ž…๋‹ˆ๋‹ค. ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. c ๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ, c ๋Š” ํŽธํ–ฅ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ ํŽธํ–ฅ์€ ์ƒ๋žตํ–ˆ์Šต๋‹ˆ๋‹ค. ~ = tanh ( c [ t s ] b) 6) ~๋ฅผ ์ถœ๋ ฅ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ~๋ฅผ ์ถœ๋ ฅ์ธต์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ธก ๋ฒกํ„ฐ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ^ = Softmax ( y ~ + y ) 4. ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์–ดํ…์…˜(Attention) ์•ž์„œ seq2seq + ์–ดํ…์…˜(attention) ๋ชจ๋ธ์— ์“ฐ์ผ ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์–ดํ…์…˜ ์ข…๋ฅ˜๊ฐ€ ์žˆ์ง€๋งŒ, ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜๊ณผ ๋‹ค๋ฅธ ์–ดํ…์…˜๋“ค์˜ ์ฐจ์ด๋Š” ์ค‘๊ฐ„ ์ˆ˜์‹์˜ ์ฐจ์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ•œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” ์ค‘๊ฐ„ ์ˆ˜์‹์€ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ฐฐ์šด ์–ดํ…์…˜์ด ๋‹ท-ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์ธ ์ด์œ ๋Š” ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‚ด์ ์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์ œ์‹œ๋˜์–ด ์žˆ์œผ๋ฉฐ, ํ˜„์žฌ ์ œ์‹œ๋œ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฆ„ ์Šค์ฝ”์–ด ํ•จ์ˆ˜ Defined by o s o e ( t h) s T i Luong et al. (2015) c l d o s o e ( t h) s T i Vaswani et al. (2017) e e a s o e ( t h) s T a i // ๋‹จ, a ๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ Luong et al. (2015) o c t c r ( t h) W T t n ( b [ t h ] ) s o e ( t h) W T t n ( b t W h) Bahdanau et al. (2015) o a i n b s ฮฑ = o t a ( a t ) // t ์‚ฐ์ถœ ์‹œ์— t ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•. Luong et al. (2015) ์œ„์—์„œ t ๋Š” Query, i ๋Š” Keys, a W๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„์ด dot์ด๋ผ๊ณ  ๋ถ™์—ฌ์ง„ ์Šค์ฝ”์–ด ํ•จ์ˆ˜๊ฐ€ ์ด๋ฒˆ์— ๋ฐฐ์šด ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜์ž…๋‹ˆ๋‹ค. ์ด ์–ดํ…์…˜์€ ์ œ์•ˆํ•œ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ๋ฃจ ์˜น(Luong) ์–ดํ…์…˜์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ œ์•ˆํ•œ ์ด๋“ค์˜ ์ด๋ฆ„์€ ์œ„ ํ…Œ์ด๋ธ”์—์„œ Defined By์— ์ ํ˜€์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. concat์ด๋ผ๋Š” ์ด๋ฆ„์˜ ์–ดํ…์…˜์€ ๋งŒ๋“  ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ๋ฐ”๋‹ค๋‚˜ ์šฐ(Bahdanau) ์–ดํ…์…˜์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅด๋ฉฐ ๋’ค์—์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ seq2seq์—์„œ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œ์ผœ์ฃผ๊ธฐ ์œ„ํ•œ ๊ธฐ๋ฒ•์ธ ์–ดํ…์…˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜์€ ์ฒ˜์Œ์—๋Š” RNN ๊ธฐ๋ฐ˜์˜ seq2seq์˜ ์„ฑ๋Šฅ์„ ๋ณด์ •ํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์†Œ๊ฐœ๋˜์—ˆ์ง€๋งŒ, ํ˜„์žฌ์— ์ด๋ฅด๋Ÿฌ์„œ๋Š” ์–ดํ…์…˜ ์Šค์Šค๋กœ๊ฐ€ ๊ธฐ์กด์˜ seq2seq๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋˜์–ด๊ฐ€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ ์ฑ•ํ„ฐ์ธ ํŠธ๋žœ์Šคํฌ๋จธ(Transformer) ์ฑ•ํ„ฐ์—์„œ ๋” ์ž์„ธํžˆ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 17-02 ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜(Bahdanau Attention) ์•ž์„œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ๋ชฉ์ ๊ณผ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ์ผ์ข…์ธ ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜(๋ฃจ ์˜น ์–ดํ…์…˜)์˜ ์ „์ฒด์ ์ธ ๊ฐœ์š”๋ฅผ ์‚ดํŽด๋ณด๊ณ , ๋งˆ์ง€๋ง‰์— ํ‘œ๋ฅผ ํ†ตํ•ด ๊ทธ ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์กด์žฌํ•œ๋‹ค๊ณ  ์†Œ๊ฐœํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋‹ท ํ”„๋กœ๋•ํŠธ ์–ดํ…์…˜๋ณด๋‹ค๋Š” ์กฐ๊ธˆ ๋” ๋ณต์žกํ•˜๊ฒŒ ์„ค๊ณ„๋œ ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 1. ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜ ํ•จ์ˆ˜(Bahdanau Attention Function) ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ํ•จ์ˆ˜ Attention()์œผ๋กœ ์ •์˜ํ•˜์˜€์„ ๋•Œ, ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜ ํ•จ์ˆ˜์˜ ์ž…, ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์˜ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Attention(Q, K, V) = Attention Value t = ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์ˆ˜ํ–‰๋˜๋Š” ๋””์ฝ”๋” ์…€์˜ ํ˜„์žฌ ์‹œ์ ์„ ์˜๋ฏธ. Q = Query : t-1 ์‹œ์ ์˜ ๋””์ฝ”๋” ์…€์—์„œ์˜ ์€๋‹‰ ์ƒํƒœ K = Keys : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค V = Values : ๋ชจ๋“  ์‹œ์ ์˜ ์ธ์ฝ”๋” ์…€์˜ ์€๋‹‰ ์ƒํƒœ๋“ค ์—ฌ๊ธฐ์„œ๋Š” ์–ดํ…์…˜ ํ•จ์ˆ˜์˜ Query๊ฐ€ ๋””์ฝ”๋” ์…€์˜ t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ t-1 ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์ž„์„ ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. 2. ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜(Bahdanau Attention) ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜์˜ ์—ฐ์‚ฐ ์ˆœ์„œ๋ฅผ ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. 1) ์–ดํ…์…˜ ์Šค์ฝ”์–ด(Attention Score)๋ฅผ ๊ตฌํ•œ๋‹ค. ์ธ์ฝ”๋”์˜ ์‹œ์ (time step)์„ ๊ฐ๊ฐ 1, 2, ... N์ด๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ ๊ฐ๊ฐ 1 h, ... N ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๋””์ฝ”๋”์˜ ํ˜„์žฌ ์‹œ์ (time step) t์—์„œ์˜ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ(hidden state)๋ฅผ t ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๋˜ํ•œ ์—ฌ๊ธฐ์„œ๋Š” ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์›์ด ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์˜ ๊ฒฝ์šฐ์—๋Š” ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ๊ฐ€ ๋™์ผํ•˜๊ฒŒ ์ฐจ์›์ด 4์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋ฃจ ์˜น ์–ดํ…์…˜์—์„œ๋Š” Query๋กœ ๋””์ฝ”๋”์˜ t ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ด๋ฒˆ์—๋Š” t-1 ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ t 1 ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ํ•จ์ˆ˜. ์ฆ‰, t 1 ๊ณผ ์ธ์ฝ”๋”์˜ i ๋ฒˆ์งธ ์€๋‹‰ ์ƒํƒœ์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. c r ( t 1 h) W T t n ( b t 1 W h) ๋‹จ, a W, c ๋Š” ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. t 1 h, 2 h, 4 ์˜ ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ณ‘๋ ฌ ์—ฐ์‚ฐ์„ ์œ„ํ•ด 1 h, 3 h๋ฅผ ํ•˜๋‚˜์˜ ํ–‰๋ ฌ๋กœ ๋‘๊ฒ ์Šต๋‹ˆ๋‹ค. ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค. c r ( t 1 H ) W T t n ( b t 1 W H ) ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์ดํ•ดํ•ด ๋ด…์‹œ๋‹ค. ์šฐ์„  b t 1 W H ๋ฅผ ๊ฐ๊ฐ ๊ตฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋“ค์„ ๋”ํ•œ ํ›„, ํ•˜์ดํผ๋ณผ๋ฆญํƒ„์  ํŠธ ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ง„ํ–‰๋œ ์—ฐ์‚ฐ์˜ ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. a h ( b t 1 W H ) ์ด์ œ a ์™€ ๊ณฑํ•˜์—ฌ t 1 h, 2 h, 4 ์˜ ์œ ์‚ฌ๋„๊ฐ€ ๊ธฐ๋ก๋œ ์–ดํ…์…˜ ์Šค์ฝ”์–ด ๋ฒกํ„ฐ t ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. t W T t n ( b t 1 W H ) 2) ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์–ดํ…์…˜ ๋ถ„ํฌ(Attention Distribution)๋ฅผ ๊ตฌํ•œ๋‹ค. t ์— ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ, ๋ชจ๋“  ๊ฐ’์„ ํ•ฉํ•˜๋ฉด 1์ด ๋˜๋Š” ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ์–ป์–ด๋ƒ…๋‹ˆ๋‹ค. ์ด๋ฅผ ์–ดํ…์…˜ ๋ถ„ํฌ(Attention Distribution)๋ผ๊ณ  ํ•˜๋ฉฐ, ๊ฐ๊ฐ์˜ ๊ฐ’์€ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜(Attention Weight)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3) ๊ฐ ์ธ์ฝ”๋”์˜ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜์™€ ์€๋‹‰ ์ƒํƒœ๋ฅผ ๊ฐ€์ค‘ ํ•ฉํ•˜์—ฌ ์–ดํ…์…˜ ๊ฐ’(Attention Value)์„ ๊ตฌํ•œ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ค€๋น„ํ•ด์˜จ ์ •๋ณด๋“ค์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์–ดํ…์…˜์˜ ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜ ๊ฐ’๋“ค์„ ๊ณฑํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ ๋ชจ๋‘ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด ๊ฐ€์ค‘ํ•ฉ(Weighted Sum)์„ ํ•œ๋‹ค๊ณ  ๋งํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๋ฒกํ„ฐ๋Š” ์ธ์ฝ”๋”์˜ ๋ฌธ๋งฅ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜์—ฌ, ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ(context vector)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. 4) ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋กœ๋ถ€ํ„ฐ t ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ LSTM์ด t ๋ฅผ ๊ตฌํ•  ๋•Œ๋ฅผ ์•„๋ž˜ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์ƒ๊ธฐํ•ด ๋ด…์‹œ๋‹ค. ๊ธฐ์กด์˜ LSTM์€ ์ด์ „ ์‹œ์ ์˜ ์…€๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์€๋‹‰ ์ƒํƒœ t 1 ์™€ ํ˜„์žฌ ์‹œ์ ์˜ ์ž…๋ ฅ t ๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ์‚ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ LSTM์€ seq2seq์˜ ๋””์ฝ”๋”์ด๋ฉฐ ํ˜„์žฌ ์‹œ์ ์˜ ์ž…๋ ฅ t ๋Š” ์ž„๋ฒ ๋”ฉ๋œ ๋‹จ์–ด ๋ฒกํ„ฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ๋Š” ์–ด๋–จ๊นŒ์š”? ์•„๋ž˜์˜ ๊ทธ๋ฆผ์€ ๋ฐ”๋‹ค๋‚˜ ์šฐ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ๋Š” ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์™€ ํ˜„์žฌ ์‹œ์ ์˜ ์ž…๋ ฅ์ธ ๋‹จ์–ด์˜ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ๋ฅผ ์—ฐ๊ฒฐ(concatenate) ํ•˜๊ณ , ํ˜„์žฌ ์‹œ์ ์˜ ์ƒˆ๋กœ์šด ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ „ ์‹œ์ ์˜ ์…€๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์€๋‹‰ ์ƒํƒœ t 1 ์™€ ํ˜„์žฌ ์‹œ์ ์˜ ์ƒˆ๋กœ์šด ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ t ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ LSTM์ด ์ž„๋ฒ ๋”ฉ๋œ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์—์„œ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ์™€ ์ž„๋ฒ ๋”ฉ๋œ ๋‹จ์–ด ๋ฒกํ„ฐ๋ฅผ ์—ฐ๊ฒฐ(concatenate) ํ•˜์—ฌ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋‹ฌ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. ์ดํ›„์—๋Š” ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. t ๋Š” ์ถœ๋ ฅ์ธต์œผ๋กœ ์ „๋‹ฌ๋˜์–ด ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก๊ฐ’์„ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 17-03 ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ด์šฉํ•œ ๋ฒˆ์—ญ๊ธฐ ๊ตฌํ˜„ ์ด๋ฒˆ์—๋Š” 15-02 ์‹ค์Šต์—์„œ ๋งŒ๋“  Seq2Seq๋ฅผ ์ด์šฉํ•œ ๋ฒˆ์—ญ๊ธฐ์— ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ์ „์ฒ˜๋ฆฌ ์ด ๊ณผ์ •์€ 15-02์—์„œ ์ง„ํ–‰ํ•œ 1. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ์ „์ฒ˜๋ฆฌ ์ฝ”๋“œ์™€ ๋ชจ๋“  ๊ณผ์ •์ด ๋™์ผํ•ฉ๋‹ˆ๋‹ค. 2. ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ ๋งŒ๋“ค๊ธฐ 15-02์™€ ๋‹ฌ๋ผ์ง„ ๊ฒƒ์€ ๋ชจ๋ธ ํด๋ž˜์Šค ์ฝ”๋“œ์™€ decode_sequence ํ•จ์ˆ˜์˜ ์ฝ”๋“œ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ 256, ์€๋‹‰ ์ƒํƒœ์˜ ์ฐจ์› ๋˜ํ•œ 256์œผ๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.optim as optim embedding_dim = 256 hidden_units = 256 ์ธ์ฝ”๋” ํด๋ž˜์Šค๋ฅผ ๋ณด๋ฉด ๊ตฌ์กฐ ์ž์ฒด๋Š” 15-02์—์„œ ๊ตฌํ˜„ํ•œ Seq2Seq์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ž„๋ฒ ๋”ฉ ์ธต(Embedding layer)๊ณผ LSMT ์ธต์œผ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ ์ž…๋ ฅ ๋ฌธ์žฅ์€ ์ž„๋ฒ ๋”ฉ ์ธต์„ ํ†ตํ•ด ๊ฐ ๋‹จ์–ด๊ฐ€ ์ž„๋ฒ ๋”ฉ๋˜๊ณ , ์ธ์ฝ”๋”์˜ LSTM์„ ํ†ต๊ณผํ•ฉ๋‹ˆ๋‹ค. class Encoder(nn.Module): def __init__(self, src_vocab_size, embedding_dim, hidden_units): super(Encoder, self).__init__() self.embedding = nn.Embedding(src_vocab_size, embedding_dim, padding_idx=0) self.lstm = nn.LSTM(embedding_dim, hidden_units, batch_first=True) def forward(self, x): # x.shape == (batch_size, seq_len, embedding_dim) x = self.embedding(x) # hidden.shape == (1, batch_size, hidden_units), cell.shape == (1, batch_size, hidden_units) outputs, (hidden, cell) = self.lstm(x) return outputs, hidden, cell ๋””์ฝ”๋” ํด๋ž˜์Šค๋ฅผ ๋ณด๋ฉด 15-02์™€๋Š” ์™„์ „ํžˆ ๋‹ฌ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ž์ฒด๊ฐ€ ๋””์ฝ”๋” ๋‹จ์—์„œ ์‹œ์ž‘๋˜๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ, ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ์–ป๊ธฐ ์œ„ํ•œ ์—ฐ์‚ฐ ๊ณผ์ • ์ž์ฒด๋Š” 15-01์—์„œ ์„ค๋ช…ํ•œ ์ˆœ์„œ๋Œ€๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋””์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์ธ์ฝ”๋”์˜ ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ ๊ฐ„์˜ ๋‚ด์ (dot product)๋ฅผ ํ†ตํ•ด์„œ ์–ดํ…์…˜ ์Šค์ฝ”์–ด(attention_scores)๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ ์–ดํ…์…˜ ์Šค์ฝ”์–ด๋ฅผ ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผ์‹œ์ผœ ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜(attention_weights)๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์–ดํ…์…˜ ๊ฐ€์ค‘์น˜๋Š” ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์—์„œ Value์— ํ•ด๋‹นํ•˜๋Š” ์ธ์ฝ”๋”์˜ ๋ชจ๋“  ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์™€ ๋‹ค์‹œ ๊ฐ๊ฐ ๊ณฑํ•ด์ง€๊ณ  ์ด๋ฅผ ๋ชจ๋‘ ๋”ํ•˜์—ฌ ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ(context_vector)๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ์ด ์ปจํ…์ŠคํŠธ ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๊ตฌํ˜„ํ•˜๊ธฐ ๋‚˜๋ฆ„์ธ๋ฐ, ์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ๋Š” 15-02์—์„œ์˜ ์„ค๋ช…๊ณผ ๊ฐ™์ด ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ(์•„๋ž˜์—์„œ๋Š” ๋ณ€์ˆ˜ x)์™€ ์—ฐ๊ฒฐ๋˜์–ด ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. class Decoder(nn.Module): def __init__(self, tar_vocab_size, embedding_dim, hidden_units): super(Decoder, self).__init__() self.embedding = nn.Embedding(tar_vocab_size, embedding_dim, padding_idx=0) self.lstm = nn.LSTM(embedding_dim + hidden_units, hidden_units, batch_first=True) self.fc = nn.Linear(hidden_units, tar_vocab_size) self.softmax = nn.Softmax(dim=2) def forward(self, x, encoder_outputs, hidden, cell): x = self.embedding(x) # Dot product attention # attention_scores.shape: (batch_size, source_seq_len, 1) attention_scores = torch.bmm(encoder_outputs, hidden.transpose(0, 1).transpose(1, 2)) # attention_weights.shape: (batch_size, source_seq_len, 1) attention_weights = self.softmax(attention_scores) # context_vector.shape: (batch_size, 1, hidden_units) context_vector = torch.bmm(attention_weights.transpose(1, 2), encoder_outputs) # Repeat context_vector to match seq_len # context_vector_repeated.shape: (batch_size, target_seq_len, hidden_units) seq_len = x.shape[1] context_vector_repeated = context_vector.repeat(1, seq_len, 1) # Concatenate context vector and embedded input # x.shape: (batch_size, target_seq_len, embedding_dim + hidden_units) x = torch.cat((x, context_vector_repeated), dim=2) # output.shape: (batch_size, target_seq_len, hidden_units) # hidden.shape: (1, batch_size, hidden_units) # cell.shape: (1, batch_size, hidden_units) output, (hidden, cell) = self.lstm(x, (hidden, cell)) # output.shape: (batch_size, target_seq_len, tar_vocab_size) output = self.fc(output) return output, hidden, cell ์•„๋ž˜์˜ Seq2Seq ํด๋ž˜์Šค๋Š” ๊ตฌ์กฐ ์ž์ฒด๋Š” 15-02์—์„œ ๊ตฌํ˜„ํ•œ Seq2Seq์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ Seq2Seq ๋ชจ๋ธ์„ ์™„์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ž์ฒด๊ฐ€ ์ •์˜๋˜๋Š” ๊ฒƒ์€ ๋””์ฝ”๋” ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. class Seq2Seq(nn.Module): def __init__(self, encoder, decoder): super(Seq2Seq, self).__init__() self.encoder = encoder self.decoder = decoder def forward(self, src, trg): encoder_outputs, hidden, cell = self.encoder(src) output, _, _ = self.decoder(trg, encoder_outputs, hidden, cell) return output encoder = Encoder(src_vocab_size, embedding_dim, hidden_units) decoder = Decoder(tar_vocab_size, embedding_dim, hidden_units) model = Seq2Seq(encoder, decoder) loss_function = nn.CrossEntropyLoss(ignore_index=0) optimizer = optim.Adam(model.parameters()) ๋””์ฝ”๋”๋Š” ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์€๋‹‰ ์ƒํƒœ๋กœ๋ถ€ํ„ฐ ์ดˆ๊ธฐ ์€๋‹‰ ์ƒํƒœ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋„ ์€๋‹‰ ์ƒํƒœ, ์…€ ์ƒํƒœ๋ฅผ ๋ฆฌํ„ดํ•˜๊ธฐ๋Š” ํ•˜์ง€๋งŒ ํ›ˆ๋ จ ๊ณผ์ •์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. seq2seq์˜ ๋””์ฝ”๋”๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ ์‹œ์ ๋งˆ๋‹ค ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งค ์‹œ์ ๋งˆ๋‹ค ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ(tar_vocab_size)์˜ ์„ ํƒ์ง€์—์„œ ๋‹จ์–ด๋ฅผ 1๊ฐœ ์„ ํƒํ•˜์—ฌ ์ด๋ฅผ ์ด๋ฒˆ ์‹œ์ ์—์„œ ์˜ˆ์ธกํ•œ ๋‹จ์–ด๋กœ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํด๋ž˜์Šค ๋ถ„๋ฅ˜ ๋ฌธ์ œ์ด๋ฏ€๋กœ ์†์‹ค ํ•จ์ˆ˜๋ฅผ ํฌ๋กœ์Šค ์—”ํŠธ๋กœํ”ผ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ฐ€ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋Š” 15-02์—์„œ ์‚ฌ์šฉํ•œ evaluation()์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. def evaluation(model, dataloader, loss_function, device): model.eval() total_loss = 0.0 total_correct = 0 total_count = 0 with torch.no_grad(): for encoder_inputs, decoder_inputs, decoder_targets in dataloader: encoder_inputs = encoder_inputs.to(device) decoder_inputs = decoder_inputs.to(device) decoder_targets = decoder_targets.to(device) # ์ˆœ๋ฐฉํ–ฅ ์ „ํŒŒ # outputs.shape == (batch_size, seq_len, tar_vocab_size) outputs = model(encoder_inputs, decoder_inputs) # ์†์‹ค ๊ณ„์‚ฐ # outputs.view(-1, outputs.size(-1))์˜ shape๋Š” (batch_size * seq_len, tar_vocab_size) # decoder_targets.view(-1)์˜ shape๋Š” (batch_size * seq_len) loss = loss_function(outputs.view(-1, outputs.size(-1)), decoder_targets.view(-1)) total_loss += loss.item() # ์ •ํ™•๋„ ๊ณ„์‚ฐ (ํŒจ๋”ฉ ํ† ํฐ ์ œ์™ธ) mask = decoder_targets != 0 total_correct += ((outputs.argmax(dim=-1) == decoder_targets) * mask).sum().item() total_count += mask.sum().item() return total_loss / len(dataloader), total_correct / total_count ๊ฐ๊ฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด ํ† ์น˜ ํ…์„œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ฐฐ์น˜ ํฌ๊ธฐ 128๋กœ ๋ฐ์ดํ„ฐ ๋กœ๋”๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ ํ•™์Šต ์—ํฌํฌ๋ฅผ 30์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ ์—ญ์‹œ 15-02์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. encoder_input_train_tensor = torch.tensor(encoder_input_train, dtype=torch.long) decoder_input_train_tensor = torch.tensor(decoder_input_train, dtype=torch.long) decoder_target_train_tensor = torch.tensor(decoder_target_train, dtype=torch.long) encoder_input_test_tensor = torch.tensor(encoder_input_test, dtype=torch.long) decoder_input_test_tensor = torch.tensor(decoder_input_test, dtype=torch.long) decoder_target_test_tensor = torch.tensor(decoder_target_test, dtype=torch.long) # ๋ฐ์ดํ„ฐ ์…‹ ๋ฐ ๋ฐ์ดํ„ฐ ๋กœ๋” ์ƒ์„ฑ batch_size = 128 train_dataset = TensorDataset(encoder_input_train_tensor, decoder_input_train_tensor, decoder_target_train_tensor) train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) valid_dataset = TensorDataset(encoder_input_test_tensor, decoder_input_test_tensor, decoder_target_test_tensor) valid_dataloader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False) # ํ•™์Šต ์„ค์ • num_epochs = 30 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) ๋ชจ๋ธ์„ ํ›ˆ๋ จํ•ฉ๋‹ˆ๋‹ค. 128๊ฐœ์˜ ๋ฐฐ์น˜ ํฌ๊ธฐ(128๊ฐœ์”ฉ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ‘๋ ฌ๋กœ ํ•™์Šต)๋กœ ์ด 50 ์—ํฌํฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ํ›ˆ๋ จ์ด ์ œ๋Œ€๋กœ ๋˜๊ณ  ์žˆ๋Š”์ง€ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ ๋˜ํ•œ 15-02์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. # Training loop best_val_loss = float('inf') for epoch in range(num_epochs): # ํ›ˆ๋ จ ๋ชจ๋“œ model.train() for encoder_inputs, decoder_inputs, decoder_targets in train_dataloader: encoder_inputs = encoder_inputs.to(device) decoder_inputs = decoder_inputs.to(device) decoder_targets = decoder_targets.to(device) # ๊ธฐ์šธ๊ธฐ ์ดˆ๊ธฐํ™” optimizer.zero_grad() # ์ˆœ๋ฐฉํ–ฅ ์ „ํŒŒ # outputs.shape == (batch_size, seq_len, tar_vocab_size) outputs = model(encoder_inputs, decoder_inputs) # ์†์‹ค ๊ณ„์‚ฐ ๋ฐ ์—ญ๋ฐฉํ–ฅ ์ „ํŒŒ # outputs.view(-1, outputs.size(-1))์˜ shape๋Š” (batch_size * seq_len, tar_vocab_size) # decoder_targets.view(-1)์˜ shape๋Š” (batch_size * seq_len) loss = loss_function(outputs.view(-1, outputs.size(-1)), decoder_targets.view(-1)) loss.backward() # ๊ฐ€์ค‘์น˜ ์—…๋ฐ์ดํŠธ optimizer.step() train_loss, train_acc = evaluation(model, train_dataloader, loss_function, device) valid_loss, valid_acc = evaluation(model, valid_dataloader, loss_function, device) print(f'Epoch: {epoch+1}/{num_epochs} | Train Loss: {train_loss:.4f} | Train Acc: {train_acc:.4f} | Valid Loss: {valid_loss:.4f} | Valid Acc: {valid_acc:.4f}') # ๊ฒ€์ฆ ์†์‹ค์ด ์ตœ์†Œ์ผ ๋•Œ ์ฒดํฌํฌ์ธํŠธ ์ €์žฅ if valid_loss < best_val_loss: print(f'Validation loss improved from {best_val_loss:.4f} to {valid_loss:.4f}. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.') best_val_loss = valid_loss torch.save(model.state_dict(), 'best_model_checkpoint.pth') ์ €์ž์˜ ํ•™์Šต ๊ธฐ๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Epoch: 1/30 | Train Loss: 2.9015 | Train Acc: 0.5423 | Valid Loss: 3.0118 | Valid Acc: 0.5413 Validation loss improved from inf to 3.0118. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 2/30 | Train Loss: 2.1927 | Train Acc: 0.6178 | Valid Loss: 2.4254 | Valid Acc: 0.6028 Validation loss improved from 3.0118 to 2.4254. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 3/30 | Train Loss: 1.7281 | Train Acc: 0.6661 | Valid Loss: 2.0946 | Valid Acc: 0.6399 Validation loss improved from 2.4254 to 2.0946. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 4/30 | Train Loss: 1.3687 | Train Acc: 0.7195 | Valid Loss: 1.8610 | Valid Acc: 0.6683 Validation loss improved from 2.0946 to 1.8610. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 5/30 | Train Loss: 1.0871 | Train Acc: 0.7652 | Valid Loss: 1.6995 | Valid Acc: 0.6886 Validation loss improved from 1.8610 to 1.6995. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 6/30 | Train Loss: 0.8613 | Train Acc: 0.8050 | Valid Loss: 1.5855 | Valid Acc: 0.7022 Validation loss improved from 1.6995 to 1.5855. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 7/30 | Train Loss: 0.6870 | Train Acc: 0.8404 | Valid Loss: 1.5009 | Valid Acc: 0.7157 Validation loss improved from 1.5855 to 1.5009. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 8/30 | Train Loss: 0.5525 | Train Acc: 0.8674 | Valid Loss: 1.4467 | Valid Acc: 0.7218 Validation loss improved from 1.5009 to 1.4467. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 9/30 | Train Loss: 0.4515 | Train Acc: 0.8874 | Valid Loss: 1.4147 | Valid Acc: 0.7243 Validation loss improved from 1.4467 to 1.4147. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 10/30 | Train Loss: 0.3790 | Train Acc: 0.8997 | Valid Loss: 1.3990 | Valid Acc: 0.7287 Validation loss improved from 1.4147 to 1.3990. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. Epoch: 11/30 | Train Loss: 0.3285 | Train Acc: 0.9086 | Valid Loss: 1.3973 | Valid Acc: 0.7334 Validation loss improved from 1.3990 to 1.3973. ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” 15-02์—์„œ ํ™•์ธํ•œ ๊ธฐ๋ก๊ณผ ๋น„๊ตํ•˜๋ฉด ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์†์‹ค์ด ์ข€ ๋” ์ž‘์œผ๋ฉฐ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•๋„๋Š” ๋” ํฐ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์†์‹ค์ด ๊ฐ€์žฅ ์ตœ์†Œ์ผ ๋•Œ์˜ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ๋‹ค์‹œ ์žฌํ‰๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค. # ๋ชจ๋ธ ๋กœ๋“œ model.load_state_dict(torch.load('best_model_checkpoint.pth')) # ๋ชจ๋ธ์„ device์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. model.to(device) # ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์™€ ์†์‹ค ๊ณ„์‚ฐ val_loss, val_accuracy = evaluation(model, valid_dataloader, loss_function, device) print(f'Best model validation loss: {val_loss:.4f}') print(f'Best model validation accuracy: {val_accuracy:.4f}') Best model validation loss: 1.3973 Best model validation accuracy: 0.7334 ๋กœ๋“œ ํ›„ ์žฌํ‰๊ฐ€๋ฅผ ์ง„ํ–‰ํ•˜์˜€๋”๋‹ˆ, ์ €์žฅํ•  ๋‹น์‹œ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์˜ ์†์‹ค๊ณผ ์ •ํ™•๋„๊ฐ€ ๋™์ผํ•˜๋ฏ€๋กœ ์ €์žฅ ๋ฐ ๋กœ๋“œ๊ฐ€ ์›ํ™œํžˆ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. <sos>์™€ <eos> ํ† ํฐ์˜ ์ •์ˆ˜๋Š” ๊ฐ๊ฐ 3๊ณผ 4์ž…๋‹ˆ๋‹ค. print(tar_vocab['<sos>']) print(tar_vocab['<eos>']) 4 3. seq2seq ๊ธฐ๊ณ„ ๋ฒˆ์—ญ๊ธฐ ๋™์ž‘์‹œํ‚ค๊ธฐ seq2seq๋Š” ํ›ˆ๋ จ ๊ณผ์ •(๊ต์‚ฌ ๊ฐ•์š”)๊ณผ ํ…Œ์ŠคํŠธ ๊ณผ์ •์—์„œ์˜ ๋™์ž‘ ๋ฐฉ์‹์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ…Œ์ŠคํŠธ ๊ณผ์ •์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ๋‹ค์‹œ ์„ค๊ณ„ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ๋””์ฝ”๋”๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฒˆ์—ญ ๋‹จ๊ณ„๋ฅผ ์œ„ํ•ด ๋ชจ๋ธ์„ ์ˆ˜์ •ํ•˜๊ณ  ๋™์ž‘์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด์ ์ธ ๋ฒˆ์—ญ ๋‹จ๊ณ„๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 1) ๋ฒˆ์—ญํ•˜๊ณ ์ž ํ•˜๋Š” ์ž…๋ ฅ ๋ฌธ์žฅ์ด ์ธ์ฝ”๋”๋กœ ์ž…๋ ฅ๋˜์–ด ์ธ์ฝ”๋”์˜ ๋งˆ์ง€๋ง‰ ์‹œ์ ์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. 2) ์ธ์ฝ”๋”์˜ ์€๋‹‰ ์ƒํƒœ์™€ ์…€ ์ƒํƒœ, ๊ทธ๋ฆฌ๊ณ  ํ† ํฐ <sos>๋ฅผ ๋””์ฝ”๋”๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. 3) ๋””์ฝ”๋”๊ฐ€ ํ† ํฐ <eos>๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๋‹จ์–ด๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ํ–‰๋™์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ ํ™•์ธ์„ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. seq_to_src ํ•จ์ˆ˜๋Š” ์˜์–ด ๋ฌธ์žฅ์— ํ•ด๋‹นํ•˜๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ์˜์–ด ๋‹จ์–ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_src๋ฅผ ํ†ตํ•ด ์˜์–ด ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. seq_to_tar์€ ํ”„๋ž‘์Šค์–ด์— ํ•ด๋‹นํ•˜๋Š” ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ •์ˆ˜๋กœ๋ถ€ํ„ฐ ํ”„๋ž‘์Šค์–ด ๋‹จ์–ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” index_to_tar์„ ํ†ตํ•ด ํ”„๋ž‘์Šค์–ด ๋ฌธ์žฅ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. index_to_src = {v: k for k, v in src_vocab.items()} index_to_tar = {v: k for k, v in tar_vocab.items()} # ์›๋ฌธ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ def seq_to_src(input_seq): sentence = '' for encoded_word in input_seq: if(encoded_word != 0): sentence = sentence + index_to_src[encoded_word] + ' ' return sentence # ๋ฒˆ์—ญ๋ฌธ์˜ ์ •์ˆ˜ ์‹œํ€€์Šค๋ฅผ ํ…์ŠคํŠธ ์‹œํ€€์Šค๋กœ ๋ณ€ํ™˜ def seq_to_tar(input_seq): sentence = '' for encoded_word in input_seq: if(encoded_word != 0 and encoded_word != tar_vocab['<sos>'] and encoded_word != tar_vocab['<eos>']): sentence = sentence + index_to_tar[encoded_word] + ' ' return sentence 25๋ฒˆ ์ƒ˜ํ”Œ์˜ ์ •์ˆ˜ ์ธ์ฝ”๋”ฉ์ด ์ง„ํ–‰๋œ ์ธ์ฝ”๋” ์ž…๋ ฅ, ๋””์ฝ”๋” ์ž…๋ ฅ, ๊ทธ๋ฆฌ๊ณ  ๋””์ฝ”๋”์˜ ๋ ˆ์ด๋ธ”์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. print(encoder_input_test[25]) print(decoder_input_test[25]) print(decoder_target_test[25]) array([ 3, 267, 1748, 2, 0, 0, 0]) array([ 3, 12, 15, 188, 53, 1889, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0]) array([ 12, 15, 188, 53, 1889, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0]) decode_sequence() ํ•จ์ˆ˜๋ฅผ ๋ด…์‹œ๋‹ค. ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„์—์„œ๋Š” ๋””์ฝ”๋”๋ฅผ ๋งค ์‹œ์  ๋ณ„๋กœ ์ปจํŠธ๋กคํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์‹œ์ ์„ for ๋ฌธ์„ ํ†ตํ•ด์„œ ์ปจํŠธ๋กคํ•˜๊ฒŒ ๋˜๋ฉฐ, ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก์€ ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋  ๋ณ€์ˆ˜๋Š” decoder_input์ž…๋‹ˆ๋‹ค. def decode_sequence(input_seq, model, src_vocab_size, tar_vocab_size, max_output_len, int_to_src_token, int_to_tar_token): encoder_inputs = torch.tensor(input_seq, dtype=torch.long).unsqueeze(0).to(device) # ์ธ์ฝ”๋”์˜ ์ดˆ๊ธฐ ์ƒํƒœ ์„ค์ • encoder_outputs, hidden, cell = model.encoder(encoder_inputs) # ์‹œ์ž‘ ํ† ํฐ <sos>์„ ๋””์ฝ”๋”์˜ ์ฒซ ์ž…๋ ฅ์œผ๋กœ ์„ค์ • # unsqueeze(0)๋Š” ๋ฐฐ์น˜ ์ฐจ์›์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•จ. decoder_input = torch.tensor([3], dtype=torch.long).unsqueeze(0).to(device) decoded_tokens = [] # for ๋ฌธ์„ ๋„๋Š” ๊ฒƒ == ๋””์ฝ”๋”์˜ ๊ฐ ์‹œ์  for _ in range(max_output_len): output, hidden, cell = model.decoder(decoder_input, encoder_outputs, hidden, cell) # ์†Œํ”„ํŠธ๋งฅ์Šค ํšŒ๊ท€๋ฅผ ์ˆ˜ํ–‰. ์˜ˆ์ธก ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค output_token = output.argmax(dim=-1).item() # ์ข…๋ฃŒ ํ† ํฐ <eos> if output_token == 4: break # ๊ฐ ์‹œ์ ์˜ ๋‹จ์–ด(์ •์ˆ˜)๋Š” decoded_tokens์— ๋ˆ„์ ํ•˜์˜€๋‹ค๊ฐ€ ์ตœ์ข… ๋ฒˆ์—ญ ์‹œํ€€์Šค๋กœ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. decoded_tokens.append(output_token) # ํ˜„์žฌ ์‹œ์ ์˜ ์˜ˆ์ธก. ๋‹ค์Œ ์‹œ์ ์˜ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. decoder_input = torch.tensor([output_token], dtype=torch.long).unsqueeze(0).to(device) return ' '.join(int_to_tar_token[token] for token in decoded_tokens) ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ž„์˜๋กœ ์„ ํƒํ•œ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. for seq_index in [3, 50, 100, 300, 1001]: input_seq = encoder_input_train[seq_index] translated_text = decode_sequence(input_seq, model, src_vocab_size, tar_vocab_size, 20, index_to_src, index_to_tar) print("์ž…๋ ฅ ๋ฌธ์žฅ :",seq_to_src(encoder_input_train[seq_index])) print("์ •๋‹ต ๋ฌธ์žฅ :",seq_to_tar(decoder_input_train[seq_index])) print("๋ฒˆ์—ญ ๋ฌธ์žฅ :",translated_text) print("-"*50) ์ž…๋ ฅ ๋ฌธ์žฅ : let s keep it . ์ •๋‹ต ๋ฌธ์žฅ : gardons cela comme ca . ๋ฒˆ์—ญ ๋ฌธ์žฅ : gardons le . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : how was the trip ? ์ •๋‹ต ๋ฌธ์žฅ : comment fut votre voyage ? ๋ฒˆ์—ญ ๋ฌธ์žฅ : comment etait le voyage ? -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : is it dangerous ? ์ •๋‹ต ๋ฌธ์žฅ : est ce dangereux ? ๋ฒˆ์—ญ ๋ฌธ์žฅ : est ce dangereux ? -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : am i seeing things ? ์ •๋‹ต ๋ฌธ์žฅ : ai je des visions ? ๋ฒˆ์—ญ ๋ฌธ์žฅ : ai je des visions ? -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : this is a bomb . ์ •๋‹ต ๋ฌธ์žฅ : c est une bombe . ๋ฒˆ์—ญ ๋ฌธ์žฅ : c est une bombe . -------------------------------------------------- ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์ž„์˜๋กœ ์„ ํƒํ•œ ์ธ๋ฑ์Šค์˜ ์ƒ˜ํ”Œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. for seq_index in [3, 50, 100, 300, 1001]: input_seq = encoder_input_test[seq_index] translated_text = decode_sequence(input_seq, model, src_vocab_size, tar_vocab_size, 20, index_to_src, index_to_tar) print("์ž…๋ ฅ ๋ฌธ์žฅ :",seq_to_src(encoder_input_test[seq_index])) print("์ •๋‹ต ๋ฌธ์žฅ :",seq_to_tar(decoder_input_test[seq_index])) print("๋ฒˆ์—ญ ๋ฌธ์žฅ :",translated_text) print("-"*50) ์ž…๋ ฅ ๋ฌธ์žฅ : that s no good . ์ •๋‹ต ๋ฌธ์žฅ : ce n est pas bon . ๋ฒˆ์—ญ ๋ฌธ์žฅ : ce n est pas bon . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : i want you back . ์ •๋‹ต ๋ฌธ์žฅ : je veux que vous soyez revenue . ๋ฒˆ์—ญ ๋ฌธ์žฅ : je veux que vous soyez revenues . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : nobody was home . ์ •๋‹ต ๋ฌธ์žฅ : personne n etait chez moi . ๋ฒˆ์—ญ ๋ฌธ์žฅ : personne n etait chez nous . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : it s raining here . ์ •๋‹ต ๋ฌธ์žฅ : il pleut ici . ๋ฒˆ์—ญ ๋ฌธ์žฅ : il pleut ici . -------------------------------------------------- ์ž…๋ ฅ ๋ฌธ์žฅ : look out ! ์ •๋‹ต ๋ฌธ์žฅ : soyez prudente ! ๋ฒˆ์—ญ ๋ฌธ์žฅ : attention ! --------------------------------------------------<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: Machine Learning ๊ฐ•์˜๋…ธํŠธ ### ๋ณธ๋ฌธ: Andrew Ng ๊ต์ˆ˜๋‹˜ Coursera ๊ฐ•์˜ ๋‚ด์šฉ ์ •๋ฆฌ ๋…ธํŠธ์ž…๋‹ˆ๋‹ค. ๋ณธ๋ž˜ ๊ฐœ์ธ์ ์œผ๋กœ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์  ์–ด์„œ ๊ฐ•์˜ ๋‚ด์šฉ์„ ๋ชจ๋‘ ํฌํ•จํ•˜์ง€๋Š” ์•Š์œผ๋ฉฐ, ๊ฐ•์˜์— ์—†๋Š” ๋‚ด์šฉ์ด๋ผ๋„ ํ•„์š”ํ•œ ์„ค๋ช…์€ ๋ณด์ถฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด๊ฐ•์˜๊ฐ€ ์ต์ˆ™ํ•˜์ง€ ์•Š์œผ์‹  ๋ถ„๋“ค๊ป˜์„œ ๊ตญ๋ฌธ ๋ณด์กฐ๊ต์žฌ๋กœ ์ฐธ๊ณ ํ•˜์‹ค ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์„œ ๊ณต๊ฐœํ•˜์˜€์Šต๋‹ˆ๋‹ค. Tensorflow๋กœ ๋”ฅ๋Ÿฌ๋‹ ์‹œ์Šคํ…œ ๊ตฌํ˜„์„ ์›ํ•˜์‹œ๋Š” ๋ถ„์€ Sung Kim ๊ต์ˆ˜๋‹˜ "๋ชจ๋‘๋ฅผ ์œ„ํ•œ ๋”ฅ๋Ÿฌ๋‹" ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์…”๋„ ์ข‹์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ•์˜๋ฅผ ๋ณธ ๋…ธํŠธ์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ณ„ํš์€ ์ค‘๋‹จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋Œ€์‹ , PyTorch๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. (2020๋…„ 3์›” ํ˜„์žฌ) ๋‚ด์šฉ ์ƒ ์˜ค๋ฅ˜/ํ”ผ๋“œ๋ฐฑ ๋˜๋Š” ์งˆ๋ฌธ์€ ๋Œ“๊ธ€๋กœ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. #๋จธ์‹ ๋Ÿฌ๋‹ #๊ธฐ๊ณ„ํ•™์Šต #machinelearning #machine_learning #Python #TensorFlow 01. Introduction What is Machine Learning? Examples Formal Definition Example: Playing Checkers Supervised Learning Regression Classification Unsupervised Learning Clustering Non-clustering Further Reading What is Machine Learning? ์ „ํ†ต์ ์œผ๋กœ programming์ด๋ผ๊ณ  ํ•˜๋ฉด, "A ์กฐ๊ฑด์—์„œ๋Š” B ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜๊ณ , C ์กฐ๊ฑด์—์„œ๋Š” D ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜๋ผ"๋ผ๊ณ  ์‚ฌ๋žŒ์ด ๊ตฌ์ฒด์ ์œผ๋กœ ๊ทœ์น™์„ ์ •ํ•ด์ฃผ๋Š” ๊ณผ์ •์ด์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ explicit programming์—๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒธ ๋ฉ”์ผ์„ ๊ฑฐ๋ฅด๋Š” ํ•„ํ„ฐ๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ํ•˜์ž. ์ œ๋ชฉ์— "๊ด‘๊ณ "๋ผ๋Š” ๋ฌธ๊ตฌ๊ฐ€ ๋ฒ„์ “์ด ๊ฑธ๋ ค์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ๋งŽ์ง€ ์•Š๋‹ค. ์ŠคํŒธ ํ•„ํ„ฐ์— ๊ฑธ๋ฆฌ์ง€ ์•Š์œผ๋ ค๊ณ  ์˜๋„์ ์œผ๋กœ ์˜คํƒ€๋ฅผ ๋‚ด๊ฑฐ๋‚˜ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ด์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ๋“ค์„ ์ผ์ผ์ด ๊ฑธ๋Ÿฌ๋‚ด์ž๋ฉด ๋„ˆ๋ฌด ๋งŽ์€ ๊ทœ์น™์„ ์ •ํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ, ์ž์œจ์ฃผํ–‰ ์ฐจ๋Ÿ‰์„ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ์šด์ „ ์ค‘์—๋Š” ์ˆ˜๋งŽ์€ ๋Œ๋ฐœ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์˜ˆ์ƒํ•  ์ˆ˜ ์—†์„ ์ •๋„๋กœ ๋งŽ์€ ๋ณ€์น™์— ๋Œ€๋น„ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ์ด์™€ ๊ฐ™์ด ๊ธฐ์กด์˜ explicit programming์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด machine learning, ์ฆ‰ ๊ธฐ๊ณ„๊ฐ€ ์Šค์Šค๋กœ ์–ด๋–ค ํŒจํ„ด์„ 'ํ•™์Šต'ํ•˜๋„๋ก ํ•˜๋Š” ์ ‘๊ทผ๋ฒ•์ด ๋“ฑ์žฅํ•˜์˜€๋‹ค. ์ตœ๊ทผ ๊ฐ๊ด‘๋ฐ›๊ณ  ์žˆ๊ธด ํ•˜์ง€๋งŒ machine learning ์ž์ฒด๋Š” ๊ต‰์žฅํžˆ ์˜ค๋ž˜๋œ ๊ฐœ๋…์ด๋‹ค. Arthur Samuel์€ 1959๋…„์— machine learning์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•˜์˜€๋‹ค. Field of study that gives computers the ability to learn without being explicitly programmed. ์š”์ปจ๋Œ€ ๋จธ์‹ ๋Ÿฌ๋‹์ด๋ž€, ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ง์ ‘ ์ˆ˜๋งŽ์€ ๊ทœ์น™์„ ๋ฏธ๋ฆฌ ์ •ํ•ด์ฃผ๋Š” ๋Œ€์‹  ํ”„๋กœ๊ทธ๋žจ ์ž์ฒด๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ์Šค์Šค๋กœ ํ•™์Šตํ•˜๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. AI ์—ฐ๊ตฌ๊ฐ€ ์ง„ํ–‰๋จ์— ๋”ฐ๋ผ machine learning ๊ธฐ๋ฒ•๋„ ๋น„์•ฝ์ ์œผ๋กœ ๋ฐœ์ „ํ•˜์˜€๊ณ , ์ด์ œ๋Š” ์ „์ž์ œํ’ˆ์˜ ์ฃผ์š”ํ•œ ๊ธฐ๋Šฅ์œผ๋กœ ์ƒˆ๋กญ๊ฒŒ ๋ฐ›์•„๋“ค์—ฌ์ง€๊ณ  ์žˆ๋‹ค. Examples Database mining: large datasets from growth of automation/web Applications can't program by hand Self-customizing programs Understanding human learning (brain, real AI) Formal Definition Arthur Samuel์˜ ์ •์˜๋Š” ๋‹ค์†Œ ์˜ค๋ž˜๋˜์—ˆ๊ณ , ์ถ”์ƒ์ ์ธ ์ธก๋ฉด์ด ์žˆ์–ด Tom Mitchell์ด ์ข€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ์ •์˜ํ•˜์˜€๋‹ค. A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E. Example: Playing Checkers E = the experience of playing many games of checkers T = the task of playing checkers. P = the probability that the program will win the next game. ์ผ๋ฐ˜์ ์œผ๋กœ, machine learning ๋ฌธ์ œ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฒ”์ฃผ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค: supervised learning๊ณผ unsupervised learning์ด ๊ทธ๊ฒƒ์ด๋‹ค. Supervised Learning Supervised learning ์ด๋ž€, ํŠน์ • input์— ๋Œ€ํ•ด "์ •๋‹ต (label)" output์ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์…‹์ด ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์€ ์ด ์ •๋ณด๋กœ๋ถ€ํ„ฐ input๊ณผ output์˜ ๊ด€๊ณ„๋ฅผ ์œ ์ถ”ํ•˜๊ฒŒ ๋œ๋‹ค. ๋Œ€๋‹ค์ˆ˜์˜ machine learning ๋ฌธ์ œ๋Š” ์ด ํ˜•ํƒœ๋ฅผ ๋ค๋‹ค. Supervised learning์˜ ์„ธ๋ถ€ ๋ถ„๋ฅ˜๋กœ๋Š” "regression"๊ณผ "classification"์ด ์žˆ๋‹ค. Regression Regression์˜ output์€ continuous ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค. ์ฆ‰, ์ฃผ์–ด์ง„ input ๋ณ€์ˆ˜๋ฅผ ouput ๋ณ€์ˆ˜์— ๋Œ€์‘์‹œํ‚ค๋Š” ์–ด๋–ค ์—ฐ์†ํ•จ์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ณผ์ •์ด ๋ฐ”๋กœ regression์ด๋‹ค. ์ง‘์˜ ๋„“์ด์— ํ•ด๋‹นํ•˜๋Š” ์ ์ ˆํ•œ ์ง‘๊ฐ’์„ ์ถ”์ •ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๊ทธ ํ•œ ์˜ˆ์ด๋‹ค. ์ด๋•Œ input์€ ์ง‘์˜ ๋„“์ด์ด๋ฉฐ, output์€ ์ง‘๊ฐ’์ด๋‹ค. ์‚ฌ์ „์— ์ง‘์˜ ๋„“์ด์™€ ๊ทธ ์ง‘์˜ ๊ฐ€๊ฒฉ์„ ์กฐ์‚ฌํ•ด์„œ ๊ทธ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ supervised learning์ด๊ณ  output์— ํ•ด๋‹นํ•˜๋Š” ์ง‘๊ฐ’์€ continuous ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ ์ด๋Š” regression ๋ฌธ์ œ์ด๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ linear regression section์—์„œ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ๋‹ค. Classification ๋ฐ˜๋ฉด, classification ๋ฌธ์ œ์˜ output์€ discrete ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค. ์ฆ‰, classification์˜ ๋ชฉ์ ์€ ์ฃผ์–ด์ง„ input ๋ณ€์ˆ˜๊ฐ€ ์–ด๋Š discrete category์— ์†ํ•˜๋Š”์ง€ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ์ข…์–‘์ด ์•…์„ฑ์ธ์ง€ ์–‘์„ฑ์ธ์ง€ ์ง„๋‹จํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ์ด๋ฒˆ์—๋„ ์‚ฌ์ „์— ์ข…์–‘์˜ ํฌ๊ธฐ์™€ ๊ทธ ์ข…์–‘์˜ ์ง„๋‹จ ๊ฒฐ๊ณผ (์•…์„ฑ/์–‘์„ฑ) ์ •๋ณด๋ฅผ ์ด์šฉํ•˜๋ฏ€๋กœ supervised learning์ด์ง€๋งŒ, output์— ํ•ด๋‹นํ•˜๋Š” ์ง„๋‹จ ๊ฒฐ๊ณผ๊ฐ€ ์•…์„ฑ/์–‘์„ฑ์œผ๋กœ discrete category์ด๋ฏ€๋กœ classification ๋ฌธ์ œ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ์ข…์–‘ ํฌ๊ธฐ๋งŒ์œผ๋กœ ์ง„๋‹จ์„ ๋‚ด๋ฆฌ๊ธฐ์—๋Š” ๋ถ€์กฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ™์€ ํฌ๊ธฐ์˜ ์ข…์–‘์ด๋ผ๋„ ๋‚˜์ด๊ฐ€ ๋งŽ์œผ๋ฉด ๋” ์œ„ํ—˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ข…์–‘ ํฌ๊ธฐ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ™˜์ž์˜ ๋‚˜์ด ์—ญ์‹œ input์œผ๋กœ ํ™œ์šฉํ•˜์—ฌ ๋ณด๋‹ค ๋ณตํ•ฉ์ ์ธ ํŒ๋‹จ์„ ๋‚ด๋ฆด ์ˆ˜๋„ ์žˆ๋‹ค. ์ด ์™ธ์—๋„ clump thickness, uniformity of cell size, uniformity of cell shape ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์š”์†Œ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์•…์„ฑ/์–‘์„ฑ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. Unsupervised Learning Unsupervised learning์€ "์ •๋‹ต (label)" output์„ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ ์…‹์ด ์—†๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋”ฐ๋ผ์„œ prediction result์— ๋Œ€ํ•œ feedback์ด ์—†๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ์ž˜๋ชป๋œ prediction ์„ ํ•˜๋”๋ผ๋„ ๊ต์ •ํ•ด ์ค„ "์„ ์ƒ๋‹˜"์ด ์—†๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ๋ณ€์ˆ˜๋“ค ๊ฐ„์˜ ๊ด€๊ณ„์— ๊ธฐ๋ฐ˜ํ•œ clustering์œผ๋กœ ์–ด๋–ค ๊ตฌ์กฐ๋ฅผ ๋„์ถœํ•ด๋‚ธ๋‹ค. Clustering ๋ฏธ๊ตญ ๊ฒฝ์ œ์— ๊ด€ํ•œ ๋…ผ๋ฌธ 1000๊ฐœ๋ฅผ ๊ฐ€์ ธ๋‹ค๊ฐ€ ์ž๋™์œผ๋กœ ์ด ๋…ผ๋ฌธ๋“ค์„ ๋น„์Šทํ•œ ๊ฒƒ๋ผ๋ฆฌ ๋ฌถ์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด word frequency, sentence length, page count ๋“ฑ์˜ ์ •๋ณด๋ฅผ ์ด์šฉํ•œ๋‹ค. Non-clustering eg. Cocktail party problem Further Reading Quora: "What is the difference between supervised and unsupervised learning algorithms?" 02. Linear Regression Linear regression์€ ์–ด๋–ค input์— ๋Œ€ํ•œ ์‹ค์ˆซ๊ฐ’์˜ output์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ด ์žฅ์—์„œ๋Š” "์ง‘๊ฐ’ ์ถ”์ •" ์˜ˆ์‹œ๋ฅผ ํ†ตํ•˜์—ฌ cost function ๊ฐœ๋…๊ณผ gradient descent ํ•™์Šต๋ฒ•์„ ๋‹ค๋ฃฌ๋‹ค. 1) Univariate Linear Regression Model Representation The Hypothesis Function Cost function Intuition I Intuition II Model Representation ๊ฐœ๋˜ฅ์ด๋Š” ์ง‘์„ ์‚ฌ๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ฐœ๋˜ฅ์ด๋Š” ์‚ด๊ณ ์ž ํ•˜๋Š” ๋™๋„ค์—์„œ ์ž๊ธฐ๊ฐ€ ์‚ด๊ณ  ์‹ถ์€ ํฌ๊ธฐ์™€ ๋น„์Šทํ•œ ์ง‘๋“ค์€ ์–ผ๋งˆ๋‚˜ ๋น„์‹ผ์ง€ ์ด๋ฆฌ์ €๋ฆฌ ๋ฌผ์–ด๋ณด๋Ÿฌ ๋‹ค๋‹ ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ์— ๋ง๋˜ฅ์ด๊ฐ€ ์‚ฌ๋Š” 1500์ œ๊ณฑํ”ผํŠธ ์งœ๋ฆฌ ์ง‘์ด 24๋งŒ ๋ถˆ์ด๊ณ  ์†Œ๋˜ฅ์ด๊ฐ€ ์‚ฌ๋Š” 1000์ œ๊ณฑํ”ผํŠธ ์งœ๋ฆฌ ์ง‘์ด 20๋งŒ ๋ถˆ์ด๋ผ๋ฉด ๊ฐœ๋˜ฅ์ด๊ฐ€ ์‚ฌ๊ณ ์ž ํ•˜๋Š” 1250์ œ๊ณฑํ”ผํŠธ ์ง‘์€ ๊ทธ ์ค‘๊ฐ„์ธ 22๋งŒ ๋ถˆ ์ •๋„๊ฐ€ ์ ๋‹นํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•  ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐœ๋˜ฅ์ด๊ฐ€ ์›ํ•˜๋Š” ๋„“์ด์˜ ์ง‘์˜ ์ ์ • ๊ฐ€๊ฒฉ์„ ์ถ”์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์ „์— ๋ช‡ ๊ตฐ๋ฐ ์ง‘์˜ ๋„“์ด์™€ ๊ทธ ๊ฐ€๊ฒฉ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ–ˆ๋‹ค. Target set of housing price size[feet ] ( ) price [1000 ] ( ) 2104 460 1416 232 1534 315 852 178 โ‹ฎ ์ด ๊ฒฝ์šฐ, ์‚ฌ์ „์— ์ˆ˜์ง‘ํ•œ ์ง‘๊ฐ’ ์ •๋ณด๋ผ๋Š” "์ •๋‹ต"์ด ์กด์žฌํ•˜๋ฏ€๋กœ "supervised learning"์— ํ•ด๋‹นํ•˜๋ฉฐ, ์ถ”์ •ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ์‹ค์ˆ˜๊ฐ’์ด๋ฏ€๋กœ regression problem์ด๋‹ค. ์—ฌ๊ธฐ์— ์ง‘์˜ ๋„“์ด์™€ ๊ฐ€๊ฒฉ ์‚ฌ์ด์— ์„ ํ˜•์˜ ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด linear regression์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ์ˆ˜์ง‘ํ•œ ์ง‘ ๋„“์ด์™€ ์ง‘๊ฐ’์„ ์ขŒํ‘œํ‰๋ฉด์— ๋‚˜ํƒ€๋‚ด์–ด ๋ณด์•˜๋‹ค. ์ˆ˜์ง‘ํ•œ ๋ฐ์ดํ„ฐ๋Š” ์•„๋ž˜ ๊ทธ๋ž˜ํ”„ ์ƒ์˜ ํŒŒ๋ž€ x ์ ๋“ค์ด๋‹ค. ์ด ์ ๋“ค์„ ๊ฐ€์žฅ '์ž˜ ๋‚˜ํƒ€๋‚ด๋Š”' ์ง์„ ์ธ ๋…น์ƒ‰ ์„ ์„ ๊ตฌํ•œ๋‹ค. ์ด ์„ ์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ•˜๋Š”์ง€๋Š” ์ฐจ์ฐจ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ์ผ๋‹จ ์ด ์ง์„ ์„ ๊ตฌํ•˜๋ฉด ๋‚ด๊ฐ€ ๊ด€์‹ฌ ์žˆ๋Š” 1250 ์ œ๊ณฑํ”ผํŠธ ์งœ๋ฆฌ ์ง‘์— ํ•ด๋‹นํ•˜๋Š” ์ง€์ ์„ ๋…น์ƒ‰ ์„ ์—์„œ ์ฐพ๊ณ , ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ€๊ฒฉ์„ y ์ถ•์—์„œ ์ฐพ์•„๋‚ด์–ด 22๋งŒ ๋ถˆ์ด ์ ์ • ๊ฐ€๊ฒฉ์ด๋ผ๊ณ  ์‰ฝ๊ฒŒ ๊ฒฐ๋ก  ๋‚ด๋ฆด ์ˆ˜ ์žˆ๋‹ค. Linear regression์€ ๋ฐ”๋กœ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ตœ์ ์˜ ์ง์„ ์„ ์ฐพ์•„๋ƒ„์œผ๋กœ์จ input (x)์™€ output (y) ์‚ฌ์ด์˜ ๊ด€๊ณ„๋ฅผ ๋„์ถœํ•ด ๋‚ด๋Š” ๊ณผ์ •์ด๋‹ค. ์ด ๊ณผ์ •์„ ์ˆ˜ํ•™์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด notation์„ ์ •๋ฆฌํ•ด ๋ณด์ž. : # training examples 's: "input" varilable, or features 's: "output" variable, or "target" variable ( , ) : one training example ( ( ) y ( ) ) i -th training example The Hypothesis Function Hypothesis ๋ž€, input (feature)๊ณผ output (target)์˜ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•จ์ˆ˜์ด๋‹ค. Output ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ํ•˜๋Š” '์ง„์งœ' ๋ณ€์ˆ˜๋“ค๊ณผ ๊ทธ ๋ณ€์ˆ˜์™€ output ์‚ฌ์ด์˜ ๊ด€๊ณ„์‹์„ ์ •์˜ํ•˜๋Š” '์ง„์งœ' ๊ด€๊ณ„์‹์„ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฐ€๋ น ์ง‘๊ฐ’์„ ๊ฒฐ์ •ํ•˜๋Š” ์š”์ธ๋งŒ ํ•˜๋”๋ผ๋„, ๋‹จ์ˆœํžˆ ์ง‘์˜ ํ‰์ˆ˜๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐฉ์ด ๋ช‡ ๊ฐœ์ธ์ง€, ์–ผ๋งˆ๋‚˜ ์˜ค๋ž˜๋œ ์ง‘์ธ์ง€, ์—ญ์„ธ๊ถŒ์ธ์ง€, ํ•™๊ตฐ์ด ์ข‹์€์ง€, ์ฃผ๋ณ€์— ํŽธ์˜์‹œ์„ค์ด ์–ผ๋งˆ๋‚˜ ์žˆ๋Š”์ง€, ๋‚˜์•„๊ฐ€ ๊ตญ์ œ ์œ ๊ฐ€ ์ „๋ง๊ณผ ์ฃผ์‹ ์‹œ์žฅ ๋ถ„์œ„๊ธฐ, ์ฐจ๊ธฐ ๋Œ€๊ถŒ ์ฃผ์ž๋“ค์˜ ํ–‰๋ณด, ๋Œ€๋งˆ์ดˆ ํ•ฉ๋ฒ•ํ™” ๋…ผ๋ž€, ์ƒˆ๋กœ์šด ์†Œํ–‰์„ฑ ๋ฐœ๊ฒฌ ๋“ฑ๋“ฑ ์ „ํ˜€ ๊ด€๋ จ ์—†์–ด ๋ณด์ด๋Š” ์š”์ธ๋“ค์ด ์ง๊ฐ„์ ‘์ ์œผ๋กœ ์ง‘๊ฐ’์— ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋ ‡๊ฒŒ ์–ด๋งˆ์–ด๋งˆํ•œ ๋ณ€์ˆ˜๋“ค์„ ๋ชจ๋‘ ๊ณ ๋ คํ•˜๊ณ  ๊ทธ ๋ณ€์ˆ˜๋“ค ๊ฐ„์˜ ๋ณต์žกํ•œ ๋ฐฉ์ •์‹์„ ์ฐพ๋Š” ๋Œ€์‹  "์ฃผ๋กœ ์ด๋Ÿฌ์ด๋Ÿฌํ•œ ๋ณ€์ˆ˜๋“ค์ด output์— ์˜ํ–ฅ์„ ๋ฏธ์น  ๊ฑฐ์•ผ"๋ผ๊ณ  ์ถ”์ •ํ•˜๊ณ  "์ด๋Ÿฌ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋กœ ๋ณ€์ˆ˜๋“ค๊ณผ output์˜ ๊ด€๊ณ„๋ฅผ ์–ผ์ถ” ๋‚˜ํƒ€๋‚ด๋ณผ ์ˆ˜ ์žˆ์„ ๊ฑฐ์•ผ"๋ผ๊ณ  ์ผ์ข…์˜ ๊ฐ€์„ค์„ ์„ธ์šฐ๊ธฐ ๋•Œ๋ฌธ์— hypothesis๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ ์‹œ์ž‘ํ•œ ๊ฒƒ์œผ๋กœ ์—ฌ๊ฒจ์ง„๋‹ค. Hypothesis๋Š” ์–ด๋–ค ํ•จ์ˆ˜์˜ ํ˜•ํƒœ๋“  ์ทจํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ ํ˜• (linear) ํ•จ์ˆ˜๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•œ๋‹ค. ฮธ ( ) ฮธ + 1 ์ด ๋•Œ์˜ ๋Š” ์‹ค์ˆซ๊ฐ’์„ ์ทจํ•˜๋Š” 1์ฐจ์› ๋žœ๋ค ๋ณ€์ˆ˜์ด๋‹ค. ์ด์™€ ๊ฐ™์ด ํ•œ ๊ฐ€์ง€ feature๋ฅผ ์ด์šฉํ•œ linear regression์„ "univariate linear regression"์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค. ์ด ๋ฐฉ์ •์‹์ด '์ง์„ ' ํ˜•ํƒœ๋ฅผ ๋ค๋‹ค๋Š” ์ ์— ์ฃผ๋ชฉํ•˜์ž. ์ด์ œ๋ถ€ํ„ฐ ฮธ ( ) ฮธ ๊ณผ 1 ๊ฐ’์„ ์ง€์ •ํ•˜์—ฌ output ^ ๊ฐ’์„ ๊ตฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ์ฃผ์–ด์ง„ input data( )๋ฅผ output data ( )์— ๋Œ€์‘์‹œํ‚ค๋Š” ํ•จ์ˆ˜ ฮธ ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๋„๋ก ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์€ training data๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. input output 0 4 1 7 2 7 3 8 ์šฐ์„ ์€ ์•„๋ฌด ์ˆซ์ž๋‚˜ ๋„ฃ์–ด๋ณด์ž. ๊ฐ€๋ น 0 2 ฮธ = . ์ด๋•Œ์˜ hypothesis function์€ ฮธ ( ) 2 2 ๊ฐ€ ๋œ๋‹ค. Input์— 1์„ ๋Œ€์ž…ํ•˜๋ฉด ^ 4 ๊ฐ€ ๋˜๋ฉฐ ์ฃผ์–ด์ง„ '์ •๋‹ต'์ธ =์—์„œ 3๋งŒํผ ๋น—๋‚˜๊ฐ„ ๊ฒƒ์ด ๋œ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชฉํ‘œ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ 0 ฮธ ์„ ์‹œ๋„ํ•˜์—ฌ x-y ํ‰๋ฉด ์ƒ์—์„œ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ ์ขŒํ‘œ๋“ค์— ๊ฐ€์žฅ ์ž˜ ๋งž๋Š” (fit), ํ˜น์€ ๊ทธ ์ ๋“ค์„ ๊ฐ€์žฅ ์ž˜ ๋Œ€ํ‘œํ•˜๋Š” ์ง์„ ์„ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. Cost function Hypothesis ฮธ ( ) ฮธ + 1 h ( ) ๋ฅผ ๊ฐ„๋žตํžˆ ( ) ๋กœ ํ‘œ๊ธฐํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์ด๋•Œ์˜ i ๋“ค์€ parameter๋ผ๊ณ  ํ•œ๋‹ค. ์šฐ๋ฆฌ์˜ ์„ ํ˜• hypothesis์—์„œ๋Š” parameter์— ์–ด๋–ค ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋Š๋ƒ์— ๋”ฐ๋ผ ( ) y ์ ˆํŽธ๊ณผ ๊ธฐ์šธ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ตœ์ ์˜ i ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฐพ์„ ๊ฒƒ์ธ๊ฐ€? ์˜ˆ. ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ๊ฐ€์žฅ ์ž˜ '๋งž๋Š”' ์ง์„ ์„ ์„ ํƒํ•˜๋ ค๋ฉด ์ผ์ •ํ•œ ๊ธฐ์ค€์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์šฐ๋ฆฌ์˜ hypothesis function์˜ ์ •ํ™•๋„๋ฅผ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•ด cost function์„ ์ด์šฉํ•  ๊ฒƒ์ด๋‹ค. ( 0 ฮธ) 1 m i 1 ( ^ ( ) y ( ) ) = 2 โˆ‘ = m ( ฮธ ( ( ) ) y ( ) ) Training example๋“ค์ด parameter ์ถ”์ •์— ์ด์šฉ๋œ๋‹ค. ์ƒ์‹์ ์œผ๋กœ ฮธ ( ) y ์™€ ๋น„์Šทํ•ด์ง€๋Š” 0 ฮธ ์„ ๊ณ ๋ฅด๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ์ด๋‹ค. ์กฐ๊ธˆ ๋” ์ˆ˜ํ•™์ ์œผ๋กœ ํ‘œํ˜„ํ•˜์ž๋ฉด, error h ( ) y ๊ฐ€ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ–๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด error ๊ฐ’์€ ์–‘์ˆ˜ ๊ฐ’์ผ ์ˆ˜๋„ ์žˆ๊ณ  ์Œ์ˆ˜ ๊ฐ’์ผ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— error ๊ฐ’์˜ ํ•ฉ์„ ๋ฐ”๋กœ ๊ตฌํ•˜๋Š” ๊ฒƒ์€ ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค. ๋Œ€์‹ , ์ด๋“ค์˜ ์ œ๊ณฑ ๊ฐ’์˜ ํ•ฉ์„ ๊ตฌํ•˜์—ฌ ๊ทธ ํ•ฉ์ด ์ตœ์†Œ๊ฐ€ ๋˜๋Š” parameter๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ์ด๋ฅผ LSE (least squared error) criterion์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ด cost funciton ์€ mean-squared-error (MSE)์ด๋‹ค. ์ฆ‰, ( error )์˜ ํ‰๊ท ์ด cost ๊ฐ’์ด ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ error๋ž€, ์ถ”์ •ํ•œ ๊ฐ’ ( ^ )๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๋งํ•œ๋‹ค. ๋‹ค๋งŒ, ํ‰๊ท ์ด๋ผ๋ฉด data ๊ฐœ ์ˆ˜์ธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์•ผ ํ•˜๋Š”๋ฐ m ์œผ๋กœ ๋‚˜๋ˆ„์—ˆ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. ์ด๋•Œ์˜ 2๋Š” ๊ณ„์‚ฐ์ƒ์˜ ํŽธ์˜๋ฅผ ์œ„ํ•œ ๊ฒƒ์œผ๋กœ ๋‚˜์ค‘์— ์•ฝ๋ถ„๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ์–ด์ฐจํ”ผ ์šฐ๋ฆฌ๋Š” cost ๊ฐ’์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๊ณ , ๊ทธ๋Ÿฌํ•œ ๋Š” cost๋ฅผ ์–ด๋–ค ์–‘์˜ ์ƒ์ˆ˜๋กœ ๋‚˜๋ˆ„๋“  ์–ด์ฐจํ”ผ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋ฏ€๋กœ ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์„ ์œ„ํ•ด 2๋กœ ๋ฏธ๋ฆฌ ๋‚˜๋ˆ„์–ด์ฃผ๋Š” ๋ฐฉ์‹์ด ์ž์ฃผ ์ด์šฉ๋œ๋‹ค. Intuition I ์šฐ์„ , ์ง๊ด€์ ์œผ๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด hypothesis๋ฅผ ๋‹จ์ˆœํ™”ํ•ด๋ณด์ž. 0 0 ์œผ๋กœ ์„ค์ •ํ•˜๋ฉด hypothesis๋Š” ์›์ ์„ ์ง€๋‚˜๋Š” ์ง์„ ์ด ๋œ๋‹ค. Training data ๋„ ๋‹จ์ˆœํ•œ ๊ฑธ ์‚ฌ์šฉํ•˜์ž. ( , ) ( , ) ( , ) 1 1 ์ธ ๊ฒฝ์šฐ, ฮธ ( ) x ๊ฐ€ ๋œ๋‹ค. ์ด ๋ชจ๋ธ์€ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ์™„๋ฒฝํ•˜๊ฒŒ ๋งž์•„๋–จ์–ด์ง€๋ฏ€๋กœ ์ด๋•Œ์˜ ( 1 ) ๋Š” 0์ด ๋œ๋‹ค. ์ด์ œ ์˜ค๋ฅธ์ชฝ์˜ ( ) ๊ทธ๋ž˜ํ”„๋กœ ์ด๋™ํ•˜์—ฌ ( , ) ์— ์ ์„ ์ฐ๋Š”๋‹ค. ํ•œํŽธ, 1 0.5 ์ธ ๊ฒฝ์šฐ, ( 1 ) 0.58 ์ด๋‹ค. ๋”ฐ๋ผ์„œ ( ) ๊ทธ๋ž˜ํ”„์˜ ( 0.5 0.58 ) ์— ์ ์„ ์ฐ๋Š”๋‹ค. ์ด์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ 1 ๊ฐ’์— ๋Œ€ํ•˜์—ฌ ( 1 ) ์„ ๊ตฌํ•ด ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋‚˜๊ฐ€๋‹ค ๋ณด๋ฉด ํฌ๋ฌผ์„  ํ˜•ํƒœ๊ฐ€ ๋˜๋ฉฐ, ์ด ํฌ๋ฌผ์„ ์ด ์ตœ์†Ÿ๊ฐ’(0)์„ ๊ฐ–๋„๋ก ํ•˜๋Š” parameter ( 1 1 ) ์ด ์ตœ์ ์˜ ๊ฐ’์ด๋‹ค. Intuition II ์ด์ œ ๋‹ค์‹œ 0 ํ•ญ์ด ์กด์žฌํ•˜๋Š” univariate linear regression ์ผ๋ฐ˜ํ˜•์„ ์ƒ๊ฐํ•ด ๋ณด์ž. ์ด๋•Œ์—๋„ ๋น„์Šทํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ( ) ๊ทธ๋ž˜ํ”„๋ฅผ ์™„์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ, ์ด๋ฒˆ์—๋Š” ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ด๋ฏ€๋กœ 0 ฮธ์— ๋”ฐ๋ผ ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ•˜๋Š” 3์ฐจ์› ๊ทธ๋ž˜ํ”„๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ์ด๋•Œ์˜ ํฌ๊ธฐ๋ฅผ ์ƒ‰์ƒ์œผ๋กœ ํ‘œํ˜„ํ•˜์—ฌ 2์ฐจ์› ํ‰๋ฉด์— ๋‚˜ํƒ€๋‚ด๋ฉด ์˜ค๋ฅธ์ชฝ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ๋“ฑ๊ณ ์„  ๋ชจ์–‘์ด ๋œ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด cost function ( ) ๊ฐ€ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ–๋„๋ก ํ•˜๋Š” parameter๋ฅผ ๊ตฌํ•˜๊ณ  ์‹ถ๋‹ค. ์ด๋ฅผ ์ž๋™์œผ๋กœ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ ์žฅ์—์„œ ๋‹ค๋ฃฌ๋‹ค. a. Pytorch Example Data generation Linear Regression with Numpy Converting to Tensor Tensor Loading Data, Devices, and CUDA Creating Parameters References Data generation = + x import numpy as np # Data Generation np.random.seed(42) x = np.random.rand(100, 1) y = 1 + 2 * x + .1 * np.random.randn(100, 1) # Shuffles the indices idx = np.arange(100) np.random.shuffle(idx) # Uses first 80 random indices for train train_idx = idx[:80] # Uses the remaining indices for validation val_idx = idx[80:] # Generates train and validation sets x_train, y_train = x[train_idx], y[train_idx] x_val, y_val = x[val_idx], y[val_idx] # Plot fig, ax = plt.subplots() ax.scatter(x_train, y_train, color='C0', label='train', alpha=0.5) ax.scatter(x_val, y_val, color = 'C1', label='validation', alpha=0.5) ax.legend() ax.grid(True) fig.show() Linear Regression with Numpy np.random.seed(42) a = np.random.randn(1) b = np.random.randn(1) print(a, b) # Sets learning rate lr = 1e-1 # Defines number of epochs n_epochs = 1000 for epoch in range(n_epochs): # Computes our model's predicted output yhat = a + b * x_train # How wrong is our model? That's the error! error = (y_train - yhat) # It is a regression, so it computes mean squared error (MSE) loss = (error ** 2).mean() # Computes gradients for both "a" and "b" parameters a_grad = -2 * error.mean() b_grad = -2 * (x_train * error).mean() # Updates parameters using gradients and the learning rate a = a - lr * a_grad b = b - lr * b_grad print(a, b) # Sanity Check: do we get the same results as our gradient descent? from sklearn.linear_model import LinearRegression linr = LinearRegression() linr.fit(x_train, y_train) print(linr.intercept_, linr.coef_[0]) Converting to Tensor Tensor PyTorch ํ…์„œ๋Š” numpy array์™€ ์‚ฌ์‹ค์ƒ ๋‹ค๋ฅผ ๊ฒŒ ์—†๋‹ค. Numpy array์™€์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ CPU์™€ GPU์—์„œ ๋ชจ๋‘ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค. Loading Data, Devices, and CUDA Numpy array๋ฅผ PyTorch tensor๋กœ ๋ณ€ํ™˜: from_numpy ์€ CPU tensor๋ฅผ return GPU๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด to() (cuda or cuda:0) GPU ์“ธ ์ˆ˜ ์žˆ๋Š”์ง€ ํŒ๋ณ„: cuda.is_available() import torch device = 'cuda' if torch.cuda.is_available() else 'cpu' x_train_tensor = torch.from_numpy(x_train).float().to(device) y_train_tensor = torch.from_numpy(y_train).float().to(device) ๋‹ค์‹œ numpy array๋กœ ๋ณ€ํ™˜: numpy ๊ทธ๋Ÿฌ๋‚˜ numpy๋Š” GPU tensor๋ฅผ ์ง์ ‘ ๋‹ค๋ฃฐ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋จผ์ € `cpu()' x_train_numpy = x_train_tensor.cpu().numpy() Creating Parameters Data ์šฉ tensor (๋ฐฉ๊ธˆ ๋งŒ๋“  ๊ฒƒ) ์™€ parameter/weight ์šฉ tensor๋Š” ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ๊ฐ€? ํ›„์ž๋Š” gradient ๊ณ„์‚ฐ์„ ํ•ด์„œ ๊ฐ’์„ update ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ requires_grad=True argument๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. ์ฃผ์˜ํ•  ์ ์€, ๋จผ์ € device๋กœ ๋ณด๋‚ด๊ณ  requires_grad_() method๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. Bad example a = torch.randn(1, requires_grad=True, dtype=torch.float).to(device) b = torch.randn(1, requires_grad=True, dtype=torch.float).to(device) print(a, b) Working example a = torch.randn(1, dtype=torch.float).to(device) b = torch.randn(1, dtype=torch.float).to(device) # and THEN set them as requiring gradients... a.requires_grad_() b.requires_grad_() print(a, b) tensor๋ฅผ ๋งŒ๋“ค๋ฉด์„œ ๋™์‹œ์— device์— ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹๋‹ค. Good example a = torch.randn(1, requires_grad=True, dtype=torch.float, device=device) b = torch.randn(1, requires_grad=True, dtype=torch.float, device=device) References Understanding PyTorch with an example: a step-by-step tutorial A comprehensive overview of PyTorch b. TensorFlow Example Recap Lab Build Graph Run/Update the Graph Using Placeholder Predict ์•„๋ž˜ ์˜ˆ์‹œ๋Š” Sung Kim ๋‹˜์˜ ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค Recap ์ด๋ฒˆ์—๋Š” ์‹œํ—˜๊ณต๋ถ€ํ•œ ์‹œ๊ฐ„(x)๋กœ๋ถ€ํ„ฐ ์‹œํ—˜ ์„ฑ์ (y)์„ ์˜ˆ์ธกํ•˜๋Š” ์˜ˆ์‹œ๋ฅผ ์•Œ์•„๋ณด๊ฒ ๋‹ค. ๋‹ค๋ฅธ ํ•™์ƒ๋“ค์˜ ๊ณต๋ถ€ ์‹œ๊ฐ„๊ณผ ์„ฑ์ ์œผ๋กœ๋ถ€ํ„ฐ ๋จผ์ € ํ•™์Šต์„ ํ•ด์•ผ ํ•œ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ training data๋ผ๊ณ  ํ•œ๋‹ค. x(hours) y(score) 10 90 9 80 3 50 2 30 ๊ทธ๋ ‡๋‹ค๋ฉด 7์‹œ๊ฐ„ ๊ณต๋ถ€ํ•œ ํ•™์ƒ์ด ๋ช‡ ์ ์„ ๋ฐ›์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ง๊ด€์ ์œผ๋กœ 65์  ์ •๋„ ๋ฐ›์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ธกํ•  ๊ฒƒ์ด๋‹ค. ๋ชจ๋ธ์„ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์„ ๊ฐ€์„ค(hypothesis)์„ ์„ธ์šด๋‹ค๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์ด๋•Œ์˜ hypothesis๊ฐ€ ์„ ํ˜•์ด๋ฉด linear hypothesis์ด๋‹ค. ( ) w + ์šฐ๋ฆฌ๋Š” ์ฃผ์–ด์ง„ ํ•™์Šต ๋ฐ์ดํ„ฐ์— ์ตœ์ ํ™”๋œ ์ง์„ ์˜ ๊ธฐ์šธ๊ธฐ์™€ y-์ ˆํŽธ์„ ๊ตฌํ•˜๋ ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ฐ€๋ น ( ) 0.5 + , ( ) x h ( ) 0.1 10 ๋“ฑ์˜ hypothesis๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ, ๊ทธ์ค‘ ๊ฐ€์žฅ ์ ์ ˆํ•œ ๊ฒƒ์ด ๋ฌด์—‡์ธ๊ฐ€๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์–ผ๋งˆ๋‚˜ '์ ์ ˆํ•œ' ๊ฐ€์„ค์ธ๊ฐ€๋ฅผ ์–‘์ ์œผ๋กœ ํŒ๋‹จํ•˜๋Š” ๊ธฐ์ค€์ด ๋ฐ”๋กœ cost function (loss function)์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ squared-error๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ( ( ) y ) ์ฃผ์–ด์ง„ training data set์˜ ๋ชจ๋“  example์— ๋Œ€ํ•œ ํ‰๊ท ์„ ๊ตฌํ•œ ๊ฒƒ์ด ์šฐ๋ฆฌ์˜ cost function์ด ๋œ๋‹ค. cost 1 โˆ‘ = m ( ( ( ) ) y ( ) ) ์ด๋•Œ ์€ example ๊ฐœ์ˆ˜์ด๋ฉฐ ( ) ๋Š” i ๋ฒˆ์งธ example์ด๋‹ค. cost function์€ w ์™€ b์— ๋Œ€ํ•œ ํ•จ์ˆ˜์ด๋ฉฐ, cost๋ฅผ ๊ฐ€์žฅ ์ž‘๊ฒŒ ๋งŒ๋“œ๋Š” w์™€ b๋ฅผ ์ฐพ๊ณ ์ž ํ•œ๋‹ค. min , cost ( , ) Lab Tensorflow๋ฅผ ํ†ตํ•ด linear regression์„ ๊ตฌํ˜„ํ•ด ๋ณด์ž Build Graph ์šฐ์„  graph๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋‹จ์ˆœํ™”ํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ training data๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. x y 1 1 2 2 3 3 #X and y data (w=1, b=0) x_train = [1,2,3] y_train = [1,2,3] ์šฐ๋ฆฌ๊ฐ€ ์ฐพ๊ณ  ์‹ถ์€ ๊ฐ’์€ model paramter ์™€์ด๋‹ค. ์ด๋“ค์„ Variable๋กœ ์ƒ์„ฑํ•œ๋‹ค. w = tf.Variable(tf.random_normal([1]), name = 'weight') b = tf.Variable(tf.random_normal([1]), name = 'bias') ์ด์ œ hypothesis์™€ cost function์„ ์ •์˜ํ•œ๋‹ค. # Hypothesis Xw+b hypothesis = x_train*w+b # Cost function cost = tf.reduce_mean( tf.square(hypothesis - y_train)) ์ •์˜ํ•œ cost function์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ gradient-descent๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. ์ด์— ๊ด€ํ•ด์„œ๋Š” ๋‚˜์ค‘์— ์ž์„ธํžˆ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ˆ ์ง€๊ธˆ์€ ๋งˆ๋ฒ•(?)์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž. # Minimize cost (Gradient descent) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) train = optimizer.minimize(cost) ์ด์™€ ๊ฐ™์ด train์ด๋ผ๋Š” node๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์œผ๋กœ ์™„์„ฑ๋œ๋‹ค. ์ด node๋Š” cost node์— ์—ฐ๊ฒฐ๋˜๊ณ , cost node๋Š” hypothesis์™€ y_train์—, ๊ทธ๋ฆฌ๊ณ  hypothesis๋Š” x_train, w, b์— ์—ฐ๊ฒฐ๋˜๋Š” ๊ตฌ์กฐ์ด๋‹ค. Run/Update the Graph Graph๋ฅผ ์ƒ์„ฑํ•˜์˜€์œผ๋ฉด ์ด์ œ ์ด ๊ทธ๋ž˜ํ”„๋ฅผ '์‹คํ–‰'์‹œํ‚ฌ ๊ฒƒ์ด๋‹ค. ์ฆ‰, ์ตœ์ ์˜ model paramter w์™€ b๋ฅผ ์ฐพ๋Š” ๊ณผ์ •์„ ์ง„ํ–‰ํ•  ๊ฒƒ์ด๋‹ค. ๋จผ์ € ์„ธ์…˜์„ ์ƒ์„ฑํ•œ๋‹ค. # Launch the graph in a session sess = tf.Session() ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ž˜ํ”„์˜ Variable๋“ค์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. sess.run(tf.global_variables_initializer()) # for w and b 'Variables' ์ด์ œ ์ค€๋น„๊ฐ€ ๋‹ค ๋˜์—ˆ๋‹ค. ์ค€๋น„๋œ ๊ทธ๋ž˜ํ”„ train๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ํ•™์Šต์ด ์ž˜ ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ค‘๊ฐ„์ค‘๊ฐ„ cost์™€ paramter๋“ค(w, b)์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ์—์„œ๋Š” 50ํšŒ iteration๋งˆ๋‹ค ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜์˜€๋‹ค. for step in range(2001): sess.run(train) if step %50 == 0: print(step, sess.run(cost), sess.run(w), sess.run(b)) ์ถœ๋ ฅ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด w์™€ b๊ฐ€ ๊ฐ๊ฐ 1๊ณผ 0์— ์ ์ฐจ ์ˆ˜๋ ดํ•ด๊ฐ€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ƒํ•œ ๋Œ€๋กœ ํ•™์Šต์ด ์ž˜ ์ด๋ฃจ์–ด์ง€๊ณ  ์žˆ๋‹ค. Using Placeholder ํ•œํŽธ, ์• ์ดˆ์— x_train ๊ณผ y_train ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๊ณ ์ •ํ•˜๋Š” ๋Œ€์‹  placeholder๋กœ ์ง€์ •ํ•˜์—ฌ ๋‚˜์ค‘์— data๋ฅผ ์ง‘์–ด๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค. # Now we can use X and y in place of x_train and y_train # Placeholders for a tensor taht will be always fed using feed_dict X = tf.placeholder(tf.float32, shape=[None]) y = tf.placeholder(tf.float32, shape=[None]) ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์—์„œ๋Š” ๊ตฌ์ฒด์ ์ธ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์ง€ ์•Š๊ณ  ์‹ค์ œ ์‹คํ–‰์‹œํ‚ค๋Š” ๋‹จ๊ณ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. ์ดํ›„ ์ฝ”๋“œ์˜ x_train, y_train ๋ณ€์ˆ˜๋ฅผ X, y๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง์ž. ์ด์ œ ๊ทธ๋ž˜ํ”„ ์‹คํ–‰ ๋‹จ๊ณ„์—์„œ feed_dict๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค. # Fit the line for step in range(2001): cost_val, w_val, b_val, _ = \ sess.run([cost, w, b, train], feed_dict = {X:[1,2,3], y:[1,2,3]}) if step %50 == 0: print(step, cost_val, w_val, b_val) Predict ์ด๋ ‡๊ฒŒ ํ•™์Šต๋œ ๊ทธ๋ž˜ํ”„๋ฅผ ์ด์šฉํ•ด ์ƒˆ๋กœ์šด ์ž…๋ ฅ๊ฐ’์— ๋Œ€ํ•œ ์ถœ๋ ฅ๊ฐ’์„ ์ถ”์ •ํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค. ๊ฐ€๋ น X=5์ธ ๊ฒฝ์šฐ, print(sess.run(hypothesis, feed_dict ={X:[5]})) ์˜ˆ์ƒ๋˜๋Š” y ๊ฐ’์€ 5๋กœ, ๋งค์šฐ ๊ทผ์ ‘ํ•˜๊ฒŒ ์ถ”์ •์ด ๋˜์—ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ ์ž…๋ ฅ์„ ๋™์‹œ์— ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค. print(sess.run(hypothesis, feed_dict ={X:[1.5, 3.5]})) 2) Parameter Learning (Gradient Descent) Gradient Descent Gradient Descent Algorithm Intuition Gradient Descent for Linear Regression Algorithm Batch Gradient Descent Further Reading Gradient Descent ์ง€๊ธˆ๊นŒ์ง€ hypothesis function์„ ์ •์˜ํ•˜๊ณ  ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ์–ผ๋งˆ๋‚˜ ์ž˜ ๋งž๋Š”์ง€ cost function์„ ํ†ตํ•ด ์ธก์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์•˜๋‹ค. ์ด์ œ๋ถ€ํ„ฐ hypothesis function์˜ ์ตœ์ ์˜ parameter๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์„ ๊ณต๋ถ€ํ•˜๋„๋ก ํ•œ๋‹ค. Gradient descent๋Š” cost function์„ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ด๋ฉฐ, cost function ๋ง๊ณ ๋„ ๊ฐ์ข… optimization์— ์ด์šฉ๋˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ฐ€๋ น, ์–ด๋–ค ํ•จ์ˆ˜ ( 0 ฮธ) ๊ฐ€ ์žˆ์„ ๋•Œ, ๊ทธ ํ•จ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’๊ณผ ๊ทธ๋•Œ์˜ parameter 0 ฮธ ์„ ์ฐพ๊ณ ์ž ํ•œ๋‹ค. min 0 ฮธ J ( 0 ฮธ) Gradient descent์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ „๋žต์„ ์ทจํ•œ๋‹ค. Gradient Descent Outline start with some 0 ฮธ (say 0 0 ฮธ = ) Keep changing 0 ฮธ to reduce ( 0 ฮธ) until we hopefully end up at minimum Gradient Descent Algorithm Gradient descent algorithm์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰๋œ๋‹ค. repeat until convergence{ ฮธ := j ฮฑ โˆ‚ j ( 0 ฮธ) for = , = } := | "assignment" operator | learning rate | feature index number, should be updated simultaneously ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ parameter๋“ค์„ ํ•œ ๋ฒˆ์— ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค. ๋งŒ์•ฝ 0 ์„ ๋จผ์ € ์—…๋ฐ์ดํŠธํ•˜์—ฌ hypothesis๊ฐ€ ๋ฐ”๋€Œ๊ณ , ๊ทธ hypothesis์— ์ˆซ์ž๋ฅผ ๋Œ€์ž…ํ•ด 1 ์„ ๊ตฌํ•˜๋ฉด ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. Intuition repeat until convergence{ ฮธ := j ฮฑ โˆ‚ j ( 0 ฮธ) for = , = } ์ƒ์ˆ˜ >๋Š” learning rate์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ด ํฌ๊ธฐ๊ฐ€ ํด์ˆ˜๋ก ํ•œ ๋ฒˆ์— ๋” ๋งŽ์ด ์›€์ง์ด๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ํŽธ๋ฏธ๋ถ„ํ•ญ โˆ‚ j ( 0 ฮธ) ๋Š” ๋‹ค์Œ์— ์ด๋™ํ•  ๋ฐฉํ–ฅ๊ณผ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. ๊ธฐ์šธ๊ธฐ์˜ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์ด๋Š”๋ฐ, ๊ธฐ์šธ๊ธฐ๊ฐ€ ํด์ˆ˜๋ก ๋” ๋งŽ์ด ์›€์ง์ธ๋‹ค. ๋‹ค์Œ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ํŽธ๋ฏธ๋ถ„ํ•ญ์ด ์–‘์ˆ˜์ด๋ฉด ์™ผ์ชฝ์œผ๋กœ, ์Œ์ˆ˜์ด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์›€์ง์—ฌ ์ตœ์†Ÿ๊ฐ’์— ์„œ์„œํžˆ ๊ฐ€๊นŒ์›Œ์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ learning rate ๊ฐ€ ๋„ˆ๋ฌด ์ž‘์œผ๋ฉด ์ˆ˜๋ ดํ•˜๋Š” ๋ฐ์— ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๊ณ , ๋„ˆ๋ฌด ํฌ๋ฉด ์ตœ์†Ÿ๊ฐ’์— ์ด๋ฅด์ง€ ๋ชปํ•ด ์ˆ˜๋ ดํ•˜์ง€ ๋ชปํ•˜๊ฑฐ๋‚˜ ์‹ฌ์ง€์–ด ๋ฐœ์‚ฐํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ ์ ˆํ•œ learning rate์„ ๊ณ ๋ฅด๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. Gradient descent๊ฐ€ local optima (slope=0)์— ์ด๋ฅด๋ฉด ํŽธ๋ฏธ๋ถ„ํ•ญ์ด 0์ด ๋˜๋ฏ€๋กœ ๋” ์ด์ƒ ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์ตœ์ ๊ฐ’์— ์ˆ˜๋ ดํ• ์ˆ˜๋ก ํŽธ๋ฏธ๋ถ„ ํ•ญ์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์•„์ ธ์„œ ์กฐ๊ธˆ์”ฉ ์—…๋ฐ์ดํŠธ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ์ˆ˜๋™์œผ๋กœ ์กฐ์ ˆํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. Gradient Descent for Linear Regression ์ด์ œ gradient descent๋ฅผ linear regression์— ์ ์šฉํ•ด ๋ณด์ž. Linear regression์—์„œ ์ •์˜ํ•œ cost function์„ ( 0 ฮธ) ์— ๋Œ€์ž…ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. โˆ‚ j ( 0 ฮธ) โˆ‚ ฮธ [ 2 โˆ‘ = m ( ฮธ ( ( ) ) y ( ) ) ] โˆ‚ ฮธ [ 2 โˆ‘ = m ( 0 ฮธ x ( ) y ( ) ) ] Algorithm ์ด๋ฅผ pseudo-code๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ด๋•Œ ์€ training set size์ด๊ณ , ( ) y ( ) ๋Š” training set์˜ ๋ฐ์ดํ„ฐ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ j ๊ฐ€ ๊ฐ๊ฐ ๋”ฐ๋กœ ๋–จ์–ด์ ธ 0 ฮธ์˜ ์—…๋ฐ์ดํŠธ ์‹์ด ๋”ฐ๋กœ ์ •์˜๋œ ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. ๋˜ํ•œ 1 ์„ ๊ตฌํ•  ๋•Œ์—๋Š” ( ) ๊ฐ€ ๊ณฑํ•ด์ง„๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค๋Š” ์ ์— ์ฃผ์˜ํ•˜์ž. ์ด ํ•ญ์€ ํŽธ๋ฏธ๋ถ„์—์„œ ๋‚˜์˜จ ๊ฒƒ์ด๋‹ค. ์ฒ˜์Œ์—๋Š” ์ž„์˜๋กœ ์„ค์ •ํ•œ parameter์—์„œ ์ถœ๋ฐœํ•˜์—ฌ iteration์ด ๊ฑฐ๋“ญ๋ ์ˆ˜๋ก ์ ์  ์ •ํ™•ํ•œ hypothesis๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ, Linear regression cost function์€ convex์ด๋ฏ€๋กœ ํ•ญ์ƒ global optima์— ์ˆ˜๋ ดํ•œ๋‹ค. Batch Gradient Descent "Batch": Gradient descent์˜ ๋งค ๋‹จ๊ณ„์—์„œ ๋ชจ๋“  training example์„ ์‚ฌ์šฉํ•œ๋‹ค. Further Reading Youtube Video "Lienar Regression (2): Gradient descent" a. Pytorch example Graident-Descent Impementing with Numpy Pytorch: Autograd Optimizer Loss References Graident-Descent Impementing with Numpy np.random.seed(42) a = np.random.randn(1) b = np.random.randn(1) print(a, b) # Sets learning rate lr = 1e-1 # Defines number of epochs n_epochs = 1000 for epoch in range(n_epochs): # Computes our model's predicted output yhat = a + b * x_train # How wrong is our model? That's the error! error = (y_train - yhat) # It is a regression, so it computes mean squared error (MSE) loss = (error ** 2).mean() # Computes gradients for both "a" and "b" parameters a_grad = -2 * error.mean() b_grad = -2 * (x_train * error).mean() # Updates parameters using gradients and the learning rate a = a - lr * a_grad b = b - lr * b_grad print(a, b) # Sanity Check: do we get the same results as our gradient descent? from sklearn.linear_model import LinearRegression linr = LinearRegression() linr.fit(x_train, y_train) print(linr.intercept_, linr.coef_[0]) Pytorch: Autograd Autograd๋Š” ์ž๋™์œผ๋กœ ๋ฏธ๋ถ„์„ ๊ตฌํ•ด์ฃผ๋Š” ํŒจํ‚ค์ง€์ด๋‹ค. ๋ชจ๋“  gradient๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด backward() method๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. Gradient ๊ณ„์‚ฐ์˜ ์ถœ๋ฐœ์ ์€ loss์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ parameter์— ๋Œ€ํ•œ ํŽธ๋ฏธ๋ถ„์„ ๊ตฌํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ, loss.backward() ์™€ ๊ฐ™์ด ํ•ด๋‹น ๋ณ€์ˆ˜๋กœ๋ถ€ํ„ฐ backward() method๋ฅผ invoke ํ•œ๋‹ค. Gradient์˜ ๊ฐ’์„ ํ™•์ธํ•˜๊ณ  ์‹ถ์œผ๋ฉด tensor์˜ grad attribute๋ฅผ ๋ณด๋ฉด ๋œ๋‹ค. Gradient๋Š” accumulate ๋˜๋ฏ€๋กœ, parameter ์—…๋ฐ์ดํŠธ์— gradient๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋‚˜๋ฉด ํ•ญ์ƒ 0์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์ด๋•Œ zero_() method๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. tip In PyTorch, every method that ends with an underscore (_) makes changes in-place, meaning, they will modify the underlying variable. PyTorch์˜ dynamic computation graph์™€ ํ˜ผ๋™๋˜์ง€ ์•Š๊ณ  ์ผ๋ฐ˜ Python ์—ฐ์‚ฐ์„ tensor์— ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก torch.no_grad()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. lr = 1e-1 n_epochs = 1000 for epoch in range(n_epochs): yhat = a + b * x_train_tensor error = y_train_tensor - yhat loss = (error ** 2).mean() with torch.no_grad(): a -= lr * a.grad b -= lr * b.grad a.grad.zero_() b.grad.zero_() Optimizer ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ง์ ‘ parameter๋ฅผ ์—…๋ฐ์ดํŠธํ–ˆ๋‹ค. ๋‘ ๊ฐœ๋ฟ์ด๋ผ ํ•  ๋งŒํ–ˆ์ง€๋งŒ ๋งŒ์•ฝ parameter ๊ฐœ์ˆ˜๊ฐ€ ์•„์ฃผ ๋งŽ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ธ๊ฐ€? Pytorch์˜ SGD๋‚˜ Adam๊ณผ ๊ฐ™์€ optimizer๋ฅผ ์จ์„œ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. Optimzer๋Š” updateํ•  parameter์™€ learning rate ๋ฐ ์—ฌ๋Ÿฌ ๋‹ค๋ฅธ hyper-parameter๋ฅผ ๋ฐ›์•„ step() method๋ฅผ ํ†ตํ•ด ์—…๋ฐ์ดํŠธํ•œ๋‹ค. Gradient๋ฅผ 0์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ๋„ optimizer์˜ zero_grad() method ๊ฐ€ ํ•ด๊ฒฐํ•ด ์ค€๋‹ค. import torch.optim as optim optimizer = optim.SGD([a, b], lr=lr) for epoch in range(n_epochs): yhat = a + b * x_train_tensor error = y_train_tensor - yhat loss = (error ** 2).mean() loss.backward() optimizer.step() optimizer.zero_grad() print(a, b) Loss ์ด๋ฒˆ์—” loss ๊ณ„์‚ฐ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์ž. Pytorch๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ loss function ์„ ์ œ๊ณตํ•œ๋‹ค. ์ง€๊ธˆ์€ regrssion ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ์žˆ์œผ๋ฏ€๋กœ mean-squared error (MSE) loss ๊ฐ€ ์ ์ ˆํ•˜๋‹ค. tip Notice that nn.MSELoss actually creates a loss function for us โ€” it is NOT the loss function itself. Moreover, you can specify a reduction method to be applied, that is, how do you want to aggregate the results for individual points โ€” you can average them (reduction=โ€™meanโ€™) or simply sum them up (reduction=โ€™sumโ€™). import torch.nn as nn loss_fn = nn.MSELoss(reduction='mean') optimizer = optim.SGD([a, b], lr=lr) for epoch in range(n_epochs): yhat = a + b * x_train_tensor loss = loss_fn(y_train_tensor, yhat) loss.backward() optimizer.step() optimizer.zero_grad() References Understanding PyTorch with an example: a step-by-step tutorial A comprehensive overview of PyTorch b. TensorFlow example Recap Gradient descent algorithm Lab Plotting Cost Function against a Model Parameter Implementing Gradient Descent Setting Manual Gradient ์•„๋ž˜ ์˜ˆ์‹œ๋Š” Sung Kim ๋‹˜์˜ ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค Recap ์šฐ์„ , bias term์„ ์—†์• ๊ณ  hypothesis๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹จ์ˆœํ•˜๊ฒŒ ์ •์˜ํ•ด ๋ณด์ž. ( ) w ๊ทธ๋Ÿฌ๋ฉด cost function์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„๋œ๋‹ค. cost 1 โˆ‘ = m ( ( ( ) ) y ( ) ) = m i 1 ( x ( ) y ( ) ) ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹จ์ˆœํ•œ training data๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. x(hours) y(score) 1 1 2 2 3 3 ์ด ๊ฒฝ์šฐ, = ์ด๋ฉด cost( )๋Š” ์–ผ๋งˆ์ผ๊นŒ? ์ด๋•Œ์—๋Š” ๋ชจ๋“  ํ•ญ์˜ error term์ด 0์ด๋ฏ€๋กœ ์ด๋ฅผ ์ œ๊ณฑํ•ด์„œ ํ‰๊ท ์„ ๋‚ด๋”๋ผ๋„ ๊ทธ๋Œ€๋กœ 0์ด ๋œ๋‹ค. = , ์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ๋„ ๊ตฌํ•ด๋ณด์ž. = : 3 ( ( ร— โˆ’ ) + ( ร— โˆ’ ) + ( ร— โˆ’ ) ) 0 = : 3 ( ( ร— โˆ’ ) + ( ร— โˆ’ ) + ( ร— โˆ’ ) ) 4.67 = : 3 ( ( ร— โˆ’ ) + ( ร— โˆ’ ) + ( ร— โˆ’ ) ) 4.67 ์ด์™€ ๊ฐ™์ด์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋ฅผ ๊ทธ๋ ค๋ณด๋ฉด ํฌ๋ฌผ์„  ํ˜•ํƒœ๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชฉํ‘œ๋Š” ์ด ํ•จ์ˆซ๊ฐ’์ด ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ์ ์„ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. min , cost ( , ) ์ด ์˜ˆ์‹œ์—์„œ๋Š” ์‰ฝ๊ฒŒ ํ•ด๋‹นํ•˜๋Š” ์ ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ์ข€ ๋” ์ผ๋ฐ˜์ ์ธ cost function ์—๊นŒ์ง€ ํ™•์žฅํ•˜๊ธฐ ์œ„ํ•ด gradient descent algorithm์„ ์ด์šฉํ•˜๊ฒ ๋‹ค. ์ง์—ญํ•˜์ž๋ฉด '๊ฒฝ์‚ฌ๋ฅผ ๋”ฐ๋ผ ๋‚ด๋ ค๊ฐ€๋Š”' ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. Gradient descent algorithm Minimize cost function Used in many minimization problems For a given cost function, cost(w, b), it will find , to minimize cost It can be applied to more general functions: cost(w1, w2, ...) ๊ฐ€๋ น ๋†’์€ ๊ณณ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๊ฐ€์žฅ ๋‚ฎ์€ ๊ณณ์œผ๋กœ ์ด๋™ํ•˜๊ณ ์ž ํ•˜์ž. ๋ฐ”๋กœ ์ฃผ๋ณ€์„ ๋‘˜๋Ÿฌ๋ด์„œ ๊ฐ€์žฅ ๊ฒฝ์‚ฌ๊ฐ€ ๊ธ‰ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ๋ฐœ์ž๊ตญ ๊ฐ„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐ”๋กœ ๊ทธ ์ง€์ ์—์„œ ๋‹ค์‹œ ์ฃผ๋ณ€์„ ๋‘˜๋Ÿฌ๋ด์„œ ๊ฐ€์žฅ ๊ฒฝ์‚ฌ๊ฐ€ ๊ธ‰ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๋‹ค์‹œ ํ•œ ๋ฐœ์ž๊ตญ ๊ฐ„๋‹ค. ์ด๋ฅผ ๋ฐ˜๋ณตํ•˜๋‹ค ๋ณด๋ฉด ๊ฐ€์žฅ ๋‚ฎ์€ ๊ณณ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. Gradient descent๋Š” ์ด๋Ÿฌํ•œ ์›๋ฆฌ๋ฅผ ์ด์šฉํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. How it works ์•„๋ฌด ๋ฐ์„œ๋‚˜ ์‹œ์ž‘ํ•œ๋‹ค. Keep chaning w and b a little bit to reduce cost(w, b) -- Each time chaning the paramters, select the gradient which reduces cost(w, b) as much as possible Repeat until converge to a local minimum ์‹œ์ž‘์ ์„ ์–ด๋””๋กœ ์žก๋Š๋ƒ์— ๋”ฐ๋ผ ์–ด๋Š local minimum์— ์ˆ˜๋ ดํ•˜๋Š๋ƒ๊ฐ€ ์ •ํ•ด์ง„๋‹ค. -- Cost function์ด convex ์ด๋ฉด ํ•ญ์ƒ global minimum์— ์ˆ˜๋ ดํ•œ๋‹ค. Formal Definition cost 1 โˆ‘ = m ( x ( ) y ( ) ) w๋Š” ์–ด๋–ป๊ฒŒ ์—…๋ฐ์ดํŠธํ•˜๋Š”๊ฐ€? learning rate์— cost function์„ w์— ๋Œ€ํ•˜์—ฌ ๋ฏธ๋ถ„ํ•œ ๊ฐ’, ์ฆ‰ ๊ธฐ์šธ๊ธฐ๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ๋นผ๋Š” ๊ฒƒ์ด๋‹ค. := โˆ’ โˆ‚ w o t ( ) w ฮฑ โˆ‚ 1 m i 1 ( x ( ) y ( ) ) = โˆ’ 1 m i 1 2 ( x ( ) y ( ) ) ( ) w ฮฑ m i 1 ( x ( ) y ( ) ) ( ) ์•„๋ฌด w์—์„œ ์‹œ์ž‘ํ•˜๋”๋ผ๋„ ์ด๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์‹คํ–‰ํ•˜๋‹ค ๋ณด๋ฉด ์ตœ์ ์˜ ๊ฐ€ ๊ตฌํ•ด์ง€๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. Lab Plotting Cost Function against a Model Parameter ์šฐ์„  model parameter์— ๋”ฐ๋ผ cost function์„ ๊ทธ๋ ค๋ณด๊ฒ ๋‹ค. ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ import ํ•œ๋‹ค. ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•ด pyplot๋„ import ํ•œ๋‹ค. import tensorflow as tf import matplotlib.pyplot as plt ์‰ฌ์šด ์˜ˆ์‹œ๋ฅผ ์œ„ํ•ด ๊ฐ„๋‹จํ•œ training data๋ฅผ ์ด์šฉํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค. X = [1, 2, 3] y = [1, 2, 3] ๋ณ€์ˆ˜ w๋ฅผ placeholder๋กœ ์ •์˜ํ•˜๊ณ  hypothesis์™€ cost function์„ ์ •์˜ํ•œ๋‹ค. w = tf.placeholder(tf.float32) # Hypothesis for linear model X*w hypothesis = X*w # Cost/loss function cost = tf.reduce_mean(tf.square(hypothesis - y)) ์ด์ œ ์ƒ์„ฑํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ์‹คํ–‰ํ•ด ๋ณธ๋‹ค. # Launch the graph in a session sess = tf.Session() # Initialize global variables in the graph sess.run(tf.global_variables_initializer()) # Variables for plotting cost function w_val = [] cost_val = [] for i in range(-30, 50): feed_w = i *0.1 curr_cost, curr_w = sess.run( [cost, w], feed_dict={w: feed_w}) w_val.append(curr_w) cost_val.append(curr_cost) ์ด์ œ ์ด ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ๋‚˜ํƒ€๋‚ด์–ด๋ณด์ž. # Show the cost function plt.plot(w_val, cost_val) plt.xlabel('w') plt.ylabel('cost') plt.show() Implementing Gradient Descent ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ Tensorflow๋ฅผ ํ†ตํ•ด gradient descent๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด์ž. ์ด๋ฒˆ์—๋„ ๊ฐ™์€ training data๋ฅผ ์ด์šฉํ•  ๊ฒƒ์ด๋‹ค. x_data = [1, 2, 3] y_data = [1, 2, 3] X = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32) ์ด๋ฒˆ์—๋Š” model parameter๊ฐ€ Tensorflow๊ฐ€ updateํ•  ๋ณ€์ˆ˜์ด๋ฏ€๋กœ Variable๋กœ ์„ ์–ธํ•œ๋‹ค. w = tf.Variable(tf.random_normal([1]), name='weight') # Minimize: Gradient descent using derivative: # w-= learning_rate * deriavative learning_rate = 0.1 gradient = tf.reduce_mean( ( w*X -y)* X) descent = w - learning_rate *gradient update = w.assign(descent) # Launch the graph in a session sess = tf.Session() # Initialize global variables in the graph sess.run(tf.global_variables_initializer()) for step in range(21): sess.run(update, feed_dict = {X:x_data, y:y_data}) print(step, sess.run(cost, feed_dict={X:x_data, y:y_data}), sess.run(w)) ํ˜น์€ Tensorflow์— ์ด๋ฏธ ์ •์˜๋œ gradient descent optimizer๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค. # Minimize: Gradient descent magic # No need to find derivative optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.1) train = optimizer.minimize(cost) # Launch the graph in a session sess = tf.Session() # Initialize global variables in the graph sess.run(tf.global_variables_initializer()) for step in range(21): sess.run(train, feed_dict = {X:x_data, y:y_data}) print(step, sess.run(w)) Setting Manual Gradient ์ž์œ ์ž์žฌ๋กœ gradient function์„ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•œ๋‹ค. # Manual gradient gradient = tf.reduce_mean( (w*X - y) * X) *2 # Cost function cost = tf.reduce_mean(tf.square( hypothesis - y)) optimizer = tf.train.GradientDescentOptimizer(learning_rate= 0.1) # Get gradients gvs = optimizer.compute_gradients(cost) # Apply gradients apply_gradients = optimizer.apply_gradients(gvs) # Launch the graph in a session sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(100): sess.run(apply_gradients, feed_dict = {X:x_data, y:y_data}) print( step, sess.run([gradient, w, gvs], feed_dict = {X:x_data, y:y_data})) 3) Multivariate Linear Regression Multiple features Cost Function Gradient Descent for Multiple Variables Matrix Notation tip Ng ๊ต์ˆ˜๋‹˜์€ ์—ฌ๋Ÿฌ ๊ฐœ intdependent variable๋กœ ํ•˜๋‚˜์˜ dependent variable์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฌธ์ œ๋ฅผ multivariate linear regression์ด๋ผ๊ณ  ๋ถˆ๋ €์ง€๋งŒ ํ†ต๊ณ„ํ•™ ์ชฝ์—์„œ๋Š” multiple (linear) regression์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ณ  multivariate regression ์€ ๋‹ค๋ฅธ ๋ฌธ์ œ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ๋ณด์ถฉ ์„ค๋ช…ํ•ด ์ฃผ์‹ค ์ˆ˜ ์žˆ๋Š” ๋ถ„์€ ์•„๋ž˜ ๋Œ“๊ธ€์ด๋‚˜ ํ”ผ๋“œ๋ฐฑ์„ ํ†ตํ•ด์„œ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Multiple features ์ง€๊ธˆ๊นŒ์ง€๋Š” ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•œ univariate regression์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด์•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ ์ƒํ™ฉ์—์„œ๋Š” ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋งŒ์œผ๋กœ ์˜ˆ์ธกํ•˜๊ธฐ ์–ด๋ ค์šด ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•œ multivariate linear regression์„ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž. ๋‹ค์‹œ ์ง‘๊ฐ’ ์ถ”์ • ๋ฌธ์ œ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ๊ฐœ๋˜ฅ์ด๋Š” ์›๋ž˜ ์ง‘์˜ ๋„“์ด๋งŒ์„ ๊ณ ๋ คํ–ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ถ€๋™์‚ฐ ์—…์ž ๋ง๋˜ฅ์ด๊ฐ€ ํ‰์ˆ˜ ์™ธ์˜ ์š”์†Œ๋“ค๋„ ๊ณ ๋ คํ•˜๋ฉด ๋” ์ •๊ตํ•˜๊ฒŒ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ์„ ๊ฑฐ๋ผ๊ณ  ์กฐ์–ธํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋ฒˆ์—๋Š” ์ง‘์˜ ๋„“์ด( 1 ) ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐฉ์˜ ๊ฐœ์ˆ˜( 2 ), ์ธต์ˆ˜( 3 ), ๊ทธ๋ฆฌ๊ณ  ๊ฑด๋ฌผ์˜ ์—ฐ๋ น( 4 )์„ ๊ณ ๋ คํ•ด์„œ ์ง‘๊ฐ’( )๋ฅผ ์ถ”์ •ํ•ด ๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. Notation = x ( ) : the number of features ( = ) : the number of training examples ( = 47 ) ( ) : input (features) of -th training example ( ( ) [ 1416 3 2 40 ] โˆˆ 4 ) j ( ) : value of feature in -th training example ( 3 ( ) 2 ) ์ด์ œ hypothesis function์„ multivariate ๊ผด๋กœ ๋‹ค์‹œ ์ •์˜ํ•ด ๋ณด์ž. ๋ณ€์ˆ˜๊ฐ€ ํ•˜๋‚˜์ผ ๋•Œ์—๋Š” ฮธ ( ) ฮธ + 1์˜€์œผ๋‚˜ h ( ) ฮธ + 1 1 ฮธ x + . + n n ๊ฐ€๋ น, ์ฃผ์–ด์ง„ ์ง‘๊ฐ’ ์ถ”์ • ๋ฌธ์ œ์—์„œ hypothesis๊ฐ€ ฮธ ( ) 80 0.1 1 0.01 2 3 3 2 4 ์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ด๊ณ  ์ง๊ด€์ ์œผ๋กœ ํ•ด์„ํ•ด ๋ณด์ž. 0 80 ์€ ๊ธฐ๋ณธ ์ง‘๊ฐ’์ด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๊ณ , 1 0.1 ์€ ์ œ๊ณฑ๋ฏธํ„ฐ๋‹น ๊ฐ€๊ฒฉ, 2 0.01 ์€ ์ธต๋‹น ๊ฐ€๊ฒฉ ๋“ฑ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Matrix multiplication์„ ํ†ตํ•ด hypothesis function์„ ๊ฐ„๋žตํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ผ ์ˆ˜๋„ ์žˆ๋‹ค. ํŽธ์˜์ƒ 0 ( ) 1 ( โˆˆ, . , ) ๋กœ ์ •์˜ํ•˜๊ณ  ์™€ ๊ฐ€ ๋ชจ๋‘ + ์ฐจ์›์ด ๋˜๋„๋ก ๋งž์ถ”๊ฒ ๋‹ค. ฮธ ( ) [ 0 1. ฮธ ] [ 0 1 x ] ฮธ x ์ด๊ฒƒ์€ ํ•œ ๊ฐœ training example์— ๋Œ€ํ•œ hypothesis function์˜ vectorization์ด๋‹ค. ๊ฐ training example์„ row-vector๋กœ ํ•˜์—ฌ matrix์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด '์Œ“๋Š”๋‹ค'. = [ 0 ( ) 1 ( ) 0 ( ) 1 ( ) 0 ( ) 1 ( ) ] ฮธ [ 0 1 ] ๊ทธ๋Ÿฌ๋ฉด hypothesis๋ฅผ ร— column vector๋กœ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ฮธ ( ) X Cost Function Parameter vector โˆˆ n 1 ์— ๋Œ€ํ•˜์—ฌ cost function์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ด์–ด์ง„๋‹ค. ( ) 1 m i 1 ( ฮธ ( ( ) ) y ( ) ) Vectorize ํ•˜์—ฌ ํ‘œํ˜„ํ•˜๋ฉด, ( ) 1 m ( ฮธ y ) ( ฮธ y ) ์ด๋•Œ์˜ ๋Š” ๋ชจ๋“  ( ) ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ๋ฒกํ„ฐ์ด๋‹ค. Gradient Descent for Multiple Variables ์ง€๊ธˆ๊นŒ์ง€ ๋‹ค๋ฃฌ ๋‚ด์šฉ์„ ์š”์•ฝํ•ด ๋ณด์ž. Hypothesis ฮธ ( ) ฮธ x ฮธ x + 1 1. . ฮธ x where 0 1 Parameters = [ 0 ฮธ, . , n ] โˆˆ n 1 Cost function ( ) J ( 0 ฮธ, . , n ) 1 m i 1 ( ฮธ ( ( ) ) y ( ) ) Gradient descent๋Š” ๋ณ€์ˆ˜๊ฐ€ ํ•˜๋‚˜์ผ ๋•Œ์™€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ™์€ ๊ผด์ด์ง€๋งŒ ๊ฐœ feature์— ๋Œ€ํ•˜์—ฌ ๋ฐ˜๋ณตํ•œ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅผ ๋ฟ์ด๋‹ค. n 1 ์ธ ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด update ํ•˜์˜€๋‹ค. Repeat until convergence{ ฮธ := 0 ฮฑ โˆ‚ 0 ( 0 ฮธ) ฮธ โˆ’ 1 โˆ‘ = m ( ฮธ ( ( ) ) y ( ) ) ฮธ := 1 ฮฑ โˆ‚ 1 ( 0 ฮธ) ฮธ โˆ’ 1 โˆ‘ = m ( ฮธ ( ( ) ) y ( ) ) ( ) } simultaneously update 0 ฮธ ์ด์ œ โ‰ฅ ์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•˜์—ฌ ์ ์–ด ๋ณด์ž. Repeat until convergence{ ฮธ := j ฮฑ โˆ‚ j ( 0. . ฮธ) ฮธ โˆ’ 1 โˆ‘ = m ( ฮธ ( ( ) ) y ( ) ) j ( ) } simultaneously update for every = , . , ์ด๋ฅผ ๊ฐ parameter ๋ณ„๋กœ ํ’€์–ด์“ฐ๋ฉด, Repeat until convergence{ ฮธ := 0 ฮฑ m i 1 ( ฮธ ( ( ) ) y ( ) ) 0 ( ) ฮธ := 1 ฮฑ m i 1 ( ฮธ ( ( ) ) y ( ) ) 1 ( ) . . ฮธ := n ฮฑ m i 1 ( ฮธ ( ( ) ) y ( ) ) n ( ) } simultaneously update for every = , . , Matrix Notation The gradient descent rule can be expressed as: := โˆ’ โˆ‡ ( ) where J ( ) is a column vector of the form: J ( ) [ J ( ) ฮธ โˆ‚ ( ) ฮธ โ‹ฎ J ( ) ฮธ ] The j-th component of the gradient is the summation of the product of two terms J ( ) ฮธ = m i 1 ( ฮธ ( ( ) ) y ( ) ) x ( ) 1 โˆ‘ = m j ( ) ( ฮธ ( ( ) ) y ( ) ) Sometimes, the summation of the product of two terms can be expressed as the product of two vectors. Here, j ( ) , for = , . , represents the elements of the j-th column, j of the training set . The other term ( ฮธ ( ( ) ) y ( ) ) is the vector of the deviations between the predictions ฮธ ( ( ) ) and the true values ( ) . Re-writing J ( ) ฮธ , we have: J ( ) ฮธ = m j T ( ฮธ y) J ( ) 1 X ( ฮธ y) Finally, the matrix notation (vectorized) of the gradient descent rule is: := โˆ’ m T ( ฮธ y) a. Pytorch Example Model Nested Models Sequential Models Training Step References Model ์ง€๊ธˆ๊นŒ์ง€ line์„ fit ํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃจ์—ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ๋Š” prediction์— ๊ด€ํ•ด ๋‹ค๋ฃจ์–ด ๋ณด์ž. PyTorch์—์„œ model์€ Module ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†๋œ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ ๋‹ค. ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‘ ๊ฐœ method๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. __init__(self): model์„ ๊ตฌ์„ฑํ•  ๋ถ€๋ถ„๋“ค์„ ์ •์˜ํ•œ๋‹ค. ์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ, a ์™€ b 2๊ฐœ์˜ paramter์ด๋‹ค. forward(self, x): ์‹ค์ œ ๊ณ„์‚ฐ์„ ํ–‰ํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ ์ฃผ์–ด์ง„ x์— ๋Œ€ํ•œ prediction์„ output์œผ๋กœ ๋‚ธ๋‹ค. tip forward(x) method๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค. model(x)์™€ ๊ฐ™์ด ์ „์ฒด model์„ ํ˜ธ์ถœํ•œ๋‹ค. class ManualLinearRegressor(nn.Module): def __init__(self): super().__init__() self.a = nn.Parameter(torch.rand(1, requires_grad=True, dtype=torch.float)) self.b = nn.Parameter(torch.rand(1, requires_grad=True, dtype=torch.float)) def foward(self, x): return self.a + self.b*x model์€ data์™€ ๊ฐ™์€ device์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ, model์—๋Š” train() method๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด๋Š” training์„ ์ง์ ‘ ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ๋Œ€์‹  model์„ training mode๋กœ ์„ค์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๊ฐ€๋ น, Dropout์ด ์žˆ๋Š” ๊ฒฝ์šฐ training๊ณผ evaluation ์ด ๊ฐ๊ฐ ๋‹ค๋ฅธ behavior๋ฅผ ๋ณด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. model = ManualLinearRegressor().to(device) print(model.state_dict()) loss_fn = nn.MSELoss(reduction='mean') optimizer = optim.SGD(model.parameters(), lr=lr) for epoch in range(n_epochs): model.train() yhat = model(x_train_tensor) loss = loss_fn(yhat, y_train_tensor) loss.backward() optimizer.step() optimizer.zero_grad() Nested Models ์ˆ˜๋™์œผ๋กœ linear regression parameter๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋Œ€์‹  PyTorch์˜ Linear model์„ ๋งŒ๋“ค์–ด nested model์„ ์ƒ์„ฑํ•ด ๋ณด์ž. class LayerLinearRegressor(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(in_features=1, out_features=1) def forward(self, x): return self.linear(x) Sequential Models Sequaltial model ์„ ์ด์šฉํ•˜๋ฉด class๋ฅผ ์ƒ์„ฑํ•  ํ•„์š”์กฐ์ฐจ ์—†๋‹ค. model=nn.Sequential(nn.Linear(in_features=1, out_features=1)).to(device) Training Step ์ง€๊ธˆ๊นŒ์ง€ optimizer, loss function, model ์„ ์ •์˜ํ–ˆ๋‹ค. loop๋ฅผ ์กฐ๊ธˆ ๋” ์ผ๋ฐ˜ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„๊นŒ? def make_train_step(model, loss_fn, optimizer): def train_step(x, y): # Sets model to train mode model.train() # Make predictions yhat = model(x) # Compute loss loss = loss_fn(yhat, y) # Compute gradients loss.backward() # Update parameters and zero gradients optimizer.step() optimizer.zero_grad() # Return the loss return loss.item() return train_step model=nn.Sequential(nn.Linear(in_features=1, out_features=1)).to(device) loss_fn = nn.MSELoss(reduction='mean') optimizer = optim.SGD(model.parameters(), lr=lr) train_step=make_train_step(model, loss_fn, optimizer) losses = [] for epoch in range(n_epochs): loss = train_step(x_train_tensor, y_train_tensor) losses.append(loss) References Understanding PyTorch with an example: a step-by-step tutorial A comprehensive overview of PyTorch 4) Gradient Descent in Practice Feature Scaling Mean normalization Learning Rate Debugging Gradient Descent Automatic Convergence Test Feature Scaling ๋ชจ๋“  feature๊ฐ€ ๋น„์Šทํ•œ ๋ฒ”์œ„์— ์žˆ์œผ๋ฉด gradient descent๊ฐ€ ๋” ๋น ๋ฅด๊ฒŒ ์ˆ˜๋ ดํ•˜๋Š” ๋ฐ์— ๋„์›€์ด ๋œ๋‹ค. More generally, get every features into approximately same range. eg. 1 x โ‰ค, 3 x โ‰ค ์•„์ฃผ ๋˜‘๊ฐ™์€ ๋ฒ”์œ„์ผ ํ•„์š”๋Š” ์—†๋‹ค. ๋Œ€๋žต ๋น„์Šทํ•œ ์ •๋„๋ฉด ์ถฉ๋ถ„. Mean normalization Replace i with i ฮผ to make features have approximately zero mean (Do not apply to 0 1 ) Learning Rate ( ) ๋Š” ๋งค iteration๋งˆ๋‹ค ๊ฐ์†Œํ•ด์•ผ ํ•œ๋‹ค. Example automatic convergence test: Declare convergence if ( ) decreases by less than in one iteration eg. = 10 3 Gradient descent ๊ฐ€ ์ œ๋Œ€๋กœ ์ž‘๋™์„ ์•ˆ ํ•œ๋‹ค๋ฉด, ๊ฐ’์„ ์ค„์ด์ž. ์ถฉ๋ถ„ํžˆ ์ž‘์€ ๊ฐ’์„ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด, ( ) ๋Š” ๋งค iteration๋งˆ๋‹ค ๊ฐ์†Œํ•˜๋Š” ๊ฒƒ์ด ์ˆ˜ํ•™์ ์œผ๋กœ ์ฆ๋ช…๋˜์–ด ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ฐ’์ด ๋„ˆ๋ฌด ์ž‘์œผ๋ฉด gradient descent ๊ฐ€ ์ˆ˜๋ ดํ•˜๋Š” ๋ฐ์— ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. Andrew Ng๋Š” ๊ฐ’์„ 3์”ฉ ๊ณฑํ•˜๊ฑฐ๋‚˜ ๋‚˜๋ˆ„๋ฉฐ ์กฐ์ ˆํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•˜์˜€๋‹ค. Debugging Gradient Descent Make a plot with number of iterations on the x-axis. Now plot the cost function, ( ) over the number of iterations of gradient descent. If ( ) ever increases, then you probably need to decrease . Automatic Convergence Test Declare convergence if ( ) decreases by less than E in one iteration, where E is some small value such as 10 3 . However in practice it's difficult to choose this threshold value. a. Pytorch Example: Dataset Dataset DataLoader Random Split References Dataset Pytorch์—์„œ dataset์€ Datset class์—์„œ ์ƒ์†๋œ๋‹ค. ์ผ์ข…์˜ tuple ๋“ค์˜ list๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๊ณ , ๊ฐ tuple์€ ํ•˜๋‚˜์˜ data point (features label) ์Œ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. from torch.utils.data import Dataset, TensorDataset ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•  method๋“ค์€ __init__(self): list of tuple์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ argument๋ฅผ ๋ฐ›๋Š”๋‹ค. CSV file์˜ ์ด๋ฆ„, ๋‘ tensor (ํ•˜๋‚˜๋Š” feature, ํ•˜๋‚˜๋Š” label), ๋“ฑ๋“ฑ __getitem__(self, index): dataset์ด index๋˜์–ด list์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๋„๋ก (dataset[i]) ํ•œ๋‹ค. ์š”์ฒญ๋œ data point์˜ Tuple (features, label)์„ return ํ•ด์•ผ ํ•œ๋‹ค. pre-loaded dataset or tensor์˜ ํ•ด๋‹นํ•˜๋Š” slice๋ฅผ return ํ•˜๊ฑฐ๋‚˜ on demand๋กœ return ํ•œ๋‹ค. __len__(self): ์ „์ฒด dataset์˜ size๋ฅผ return ํ•œ๋‹ค. tip __init__์—์„œ ์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์„ ๋ถˆ๋Ÿฌ์˜ค์ง€ ์•Š์•„๋„ ๋œ๋‹ค. dataset์ด ํฌ๋‹ค๋ฉด ํ•œ ๋ฒˆ์— ์ฝ์–ด๋“ค์ด๋Š” ๊ฒƒ์€ memory ํšจ์œจ์ด ๋–จ์–ด์ง„๋‹ค. ์ด ๊ฒฝ์šฐ, on demand๋กœ __getitem__์„ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ๋ถ€๋ฅด๋Š” ํŽธ์„ ์ถ”์ฒœํ•œ๋‹ค. class CustomDataset(Dataset): def __init__(self, x_tensor, y_tensor): self.x = x_tensor self.y = y_tensor def __getitem__(self, index): return (self.x[index], self.y[index]) def __len__(self): return len(self.x) x_train_tensor = torch.from_numpy(x_train).float() y_train_tensor = torch.from_numpy(y_train).float() train_data = CustomDataset(x_train_tensor, y_train_tensor) print(train_data[0]) ์ด๋•Œ training tensor๋ฅผ device๋กœ ๋ณด๋‚ด์ง€ ์•Š์€ ์ ์— ์œ ์˜ํ•˜์ž. training data ์ „์ฒด๋ฅผ GPU tensor๋กœ ์ฝ์–ด์„œ ์†Œ์ค‘ํ•œ ๊ทธ๋ž˜ํ”ฝ ์นด๋“œ RAM์˜ ์ž๋ฆฌ๋ฅผ ๊ณผ๋„ํ•˜๊ฒŒ ์ฐจ์ง€ํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. ํ•œํŽธ, Dataset์ด tensor ๋ช‡ ๊ฐœ์— ๋ถˆ๊ณผํ•˜๋‹ค๋ฉด ์ƒˆ class๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ณผ์ • ์—†์ด TensorDataset class๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ด ๋” ๊ฐ„๋‹จํ•˜๋‹ค. train_data = TensorDataset(x_train_tensor, y_train_tensor) print(train_data[0]) DataLoader ์ง€๊ธˆ๊นŒ์ง€๋Š” ์ „์ฒด training data๋ฅผ ๋งค training step๋งˆ๋‹ค ์ด์šฉํ–ˆ๋‹ค. ์ฆ‰, batch gradient descent์˜€๋‹ค. ์ž‘์€ dataset์œผ๋กœ๋Š” ๊ดœ์ฐฎ์ง€๋งŒ dataset์ด ์ปค์ง€๋ฉด mini-batch gradient descent๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. Pytorch DataLoader class๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด dataset slice๋ฅผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. from torch.utils.data import DataLoader train_loader = DataLoader(dataset=train_data, batch_size=16, shuffle=True) Sample mini-batch ํ•˜๋‚˜๋ฅผ ์–ป์œผ๋ ค๋ฉด ์•„๋ž˜ command๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. next(iter(train_loader)) for epoch in range(n_epochs): for x_batch, y_batch in train_loader: x_batch = x_batch.to(device) y_batch = y_batch.to(device) loss = train_step(x_batch, y_batch) losses.append(loss) ํ•œ ๋ฒˆ์— ํ•œ mini-batch๋งŒ device๋กœ ๋ณด๋‚ธ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ฐ™์€ ๊ณผ์ •์„ validation data์— split์„ ์ด์šฉํ•ด ์ ์šฉํ•ด ๋ณด์ž. Random Split Pytorch์˜ random_split() method๋Š” training-validation split์„ ์šฉ์ดํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ „์ฒด dataset์— ์ ์šฉํ•ด์•ผ ํ•จ์„ ๊ธฐ์–ตํ•˜์ž. from torch.utils.data.dataset import random_split x_tensor = torch.from_numpy(x).float() y_tensor = torch.from_numpy(y).float() dataset = TensorDataset(x_tensor, y_tensor) train_dataset, val_dataset = random_split(dataset, [80,20]) train_loader = DataLoader(dataset=train_dataset, batch_size=16) val_loader = DataLoader(dataset=val_dataset, batch_size=20) References Understanding PyTorch with an example: a step-by-step tutorial A comprehensive overview of PyTorch 5) Features and Polynomial Regression New Feature Polynomial Regression ๋ช‡ ๊ฐ€์ง€ feature ์™€ hypothesis function ๊ฐœ์„  ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. New Feature ์—ฌ๋Ÿฌ ๊ฐœ feature๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์„œ ์ƒˆ๋กœ์šด feature๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋ น, ๊ฐœ๋˜ฅ์ด๊ฐ€ ์ง‘๊ฐ’์„ ์ถ”์ •ํ•  ๋•Œ ์ง‘์˜ frontage์™€ depth๋ฅผ feature๋กœ ์ด์šฉํ–ˆ๋‹ค๊ณ  ํ•˜์ž. ์ด ๋‘ feature๋ณด๋‹ค๋Š” ๊ทธ ๋‘˜์„ ๊ณฑํ•œ ๊ฐ’, ์ฆ‰ ์ง‘์˜ ๋„“์ด ์—ญ์‹œ ์ง‘๊ฐ’์„ ์ถ”์ •ํ•˜๋Š” ๋ฐ์— ๋„์›€์ด ๋˜๋Š” feature์ด๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒˆ๋กœ์šด featre 1 ๋ฅผ ๊ธฐ์กด์˜ feature๋“ค์˜ ๊ณฑ์˜ ํ˜•ํƒœ๋กœ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ฮธ ( ) ฮธ + 1 ( frontage ) ฮธ ร— ( depth ) 1 ( area ) ( frontage ) ( depth ) Polynomial Regression Hypothesis function์ด ๋ฐ˜๋“œ์‹œ linear (์ง์„ )์ด์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์— ์ž˜ fit ํ•˜๋Š” ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด 2์ฐจ ํ•จ์ˆ˜๋‚˜ 3์ฐจ ํ•จ์ˆ˜ ๊ณก์„ , ๋˜๋Š” ์ œ๊ณฑ๊ทผ ํ•จ์ˆ˜ ๋“ฑ์˜ ํ˜•ํƒœ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋ น, hypothesis function์ด ฮธ ( ) ฮธ + 1 1 ์ผ ๋•Œ, 1 ์— ์—ฐ๊ด€๋œ feature๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ 2์ฐจ ํ•จ์ˆ˜ ๊ผด๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ฮธ ( ) ฮธ + 1 1 ฮธ x 2 ๊ทธ๋Ÿฐ๋ฐ 2์ฐจ ํ•จ์ˆ˜ ํฌ๋ฌผ์„ ์€ ์ตœ๊ณ ์ ์„ ์ฐ๊ณ  ๊ฐ์†Œํ•˜๋Š” ํ˜•ํƒœ์ด๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ์ง‘์˜ ํฌ๊ธฐ๊ฐ€ ์ผ์ • ์ˆ˜์ค€ ์ด์ƒ์œผ๋กœ ์ปค์ง€๋ฉด ์ง‘์ด ํด์ˆ˜๋ก ์ง‘๊ฐ’์ด ๊ฐ์†Œํ•˜๋Š” ๊ธฐ์ดํ•œ ๋ชจ๋ธ์ด ๋œ๋‹ค. ์ด๋Š” ์šฐ๋ฆฌ์˜ ์ง‘๊ฐ’ ์ถ”์ • ๋ฐ์ดํ„ฐ์™€ ์ž˜ ๋งž์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ์ด๋ฒˆ์—๋Š” 3์ฐจ ํ•จ์ˆ˜๋ฅผ ๊ณ ๋ คํ•ด ๋ณด์ž. ฮธ ( ) ฮธ + 1 1 ฮธ x 3 ฮธ x 3 ์ด๋ ‡๊ฒŒ 3์ฐจ ํ•จ์ˆ˜ ๊ผด๋กœ ๋‚˜ํƒ€๋‚ด์—ˆ์„ ๋•Œ, ์ƒˆ๋กœ์šด feature 2 x ๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. 2 x 2 x = 1. ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ ์ œ๊ณฑ๊ทผ ํ˜•ํƒœ์˜ feature๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ฮธ ( ) ฮธ + 1 1 ฮธ x ์ฃผ์˜ํ•  ์ ์€, ์ด๋ ‡๊ฒŒ feature๋ฅผ ๋งŒ๋“ค๋ฉด feature scaling์ด ๋”์šฑ ์ค‘์š”ํ•ด์ง„๋‹ค๋Š” ์ ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 1 ์˜ ์›๋ž˜ ๋ฒ”์œ„๊ฐ€ 1~1000์ด๋ผ๋ฉด, 1์˜ ๋ฒ”์œ„๋Š” 1~1000000์ด ๋˜๊ณ , 1์˜ ๋ฒ”์œ„๋Š” 1~1000000000์ด ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค. a. Pytorch Example: Evaluation Evaluation References Evaluation ์ด์ œ validation loss๋ฅผ ๊ณ„์‚ฐํ•  ์ฐจ๋ก€์ด๋‹ค. ๋‹ค์Œ ์‚ฌํ•ญ์„ ๊ณ ๋ คํ•˜์ž. torch.no_grad(): ์šฐ๋ฆฌ์˜ ์ž‘์€ model์—์„œ๋Š” ์ฐจ์ด๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์ง€๋งŒ, validation inner loop๋ฅผ ์ด context manager๋กœ ๊ฐ์‹ธ์„œ gradient calculation์„ ๋ฐฉ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. eval(): model์„ evaluation mode๋กœ ์„ค์ •ํ•œ๋‹ค. for epoch in range(n_epochs): for x_batch, y_batch in train_loader: x_batch = x_batch.to(device) y_batch = y_batch.to(device) loss = train_step(x_batch, y_batch) losses.append(loss) with torch.no_grad(): for x_val, y_val in val_loader: x_val = x_val.to(device) y_val = y_val.to(device) model.eval() yhat = model(x_val) val_loss = loss_fn(yhat, y_val) val_losses.append(val_loss.item()) References Understanding PyTorch with an example: a step-by-step tutorial A comprehensive overview of PyTorch 6) Computing Parameters Analytically Normal Equation Intuition Normal Equation Noninvertibility Further Reading Normal Equation Model parameter์˜ analytical solution์„ ์ง์ ‘ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. Gradient-descent ๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ iteration์„ ๋Œ์•„์•ผ ํ•˜๋Š” ๊ฒƒ๊ณผ ๋Œ€์กฐ์ ์œผ๋กœ ํ•œ ๋ฒˆ์— ์ตœ์ ์˜ ํ•ด๋ฅผ ์ฐพ๋Š”๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค. Intuition 1D case ( โˆˆ ) Feature ๊ฐ€ 1๊ฐœ์ธ ๊ฒฝ์šฐ, ( ) ๋Š” 2์ฐจ ๋ฐฉ์ •์‹์˜ ๊ผด์ด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ์— ๋Œ€ํ•˜์—ฌ ๋ฏธ๋ถ„ํ•œ ๊ฒฐ๊ณผ๊ฐ€ 0์ด ๋˜๋„๋ก ์„ค์ •ํ•˜์—ฌ ํ’€๋ฉด ๋œ๋‹ค. Multiple feature case โˆˆ n 1 ์—ฌ๋Ÿฌ ๊ฐœ feature๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ๋ณ€์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ด๋ฏ€๋กœ ์ด๋ฒˆ์—๋Š” ๊ฐ j ์— ๋Œ€ํ•˜์—ฌ ํŽธ ๋ฏธ๋ถ„ํ•˜์—ฌ ํ‘ผ๋‹ค. Example: = m examples ( ( ) y ( ) ) . . ( ( ) y ( ) ) n features Feature scaling ์ด ๊ตณ์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. Gradient-descent Normal Equation โ–ณ Need to choose โ–ฝ No need to choose โ–ฝ Need many iterations โ–ณ No need to iterate โ–ณ Works well when is large โ–ฝ Slow if very large ( n) ( 3 ) to calculate ( T) 1 eg. Use gd if โ‰ฅ 10 000 Normal Equation Noninvertibility What if T non-invertible? (singular/ degenerate) Common reasons Redundant features (linearly dependent) eg. 1 ( i e n ) ( e t ) , 1 ( i e n ) ( ) ==> Remove one Too many features (eg โ‰ค ) ==> Delete some features or use regularization Further Reading Proofs are available at these links for those who are interested: https://en.wikipedia.org/wiki/Linear_least_squares_(mathematics) http://eli.thegreenplace.net/2014/derivation-of-the-normal-equation-for-linear-regression 03. Logistic Regression Classification Binary Classification Linear Regression for Classification? Classification Linear regression ์ด ์ฃผ์–ด์ง„ feature์— ๋”ฐ๋ผ continous ํ•œ target ๊ฐ’์„ ์ถ”์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• (eg. ์ง‘๊ฐ’ ์ถ”์ •) ์ด์—ˆ๋‹ค๋ฉด, classification์€ ์ฃผ์–ด์ง„ feature์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฅผ discrete ํ•œ class์— ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋ฐ›์€ e-mail์ด ์ŠคํŒธ์ธ์ง€ ์•„๋‹Œ์ง€, ์–ด๋–ค ์ข…์–‘์ด ์–‘์„ฑ์ธ์ง€ ์•…์„ฑ์ธ์ง€ ๋“ฑ์„ ํŒ๋ณ„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋‚˜์ค‘์— ์—ฌ๋Ÿฌ ๊ฐœ class๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” multi-class classification์— ๋Œ€ํ•ด์„œ๋„ ๋‹ค๋ฃจ๊ฒ ์ง€๋งŒ, ์šฐ์„ ์€ ๋‘ ๊ฐœ class๋กœ๋งŒ ๋ถ„๋ฅ˜ํ•˜๋Š” binary classification์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. Binary Classification Linear regression์€ output vector ๊ฐ€ ์—ฐ์†์  ๊ฐ’์„ ๊ฐ–์ง€๋งŒ binary classification์—์„œ๋Š” ๊ฐ€ 0 ๋˜๋Š” 1์˜ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค. ์ฆ‰, โˆˆ, . ์ผ๋ฐ˜์ ์œผ๋กœ, 0์€ "negative class"๋กœ, 1์€ "positive class"๋ผ๊ณ  ๋ถˆ๋ฆฌ์ง€๋งŒ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ•˜๋“  ๊ด€๊ณ„์—†๋‹ค. ์ด์™€ ๊ฐ™์ด 2๊ฐœ์˜ class๋กœ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฌธ์ œ๋Š” "binary classification problem"์ด๋ผ๊ณ  ํ•œ๋‹ค. ๊ฐ class์˜ ์ด๋ฆ„์€ ํŽธ์˜์ƒ '0' ๊ณผ '1'์ด๋ผ๊ณ  ํ•˜์ž. ์–ด๋–ค ์ด๋ฆ„์„ ์“ฐ๋“  ์ƒ๊ด€์—†์ง€๋งŒ ๊ณ„์‚ฐ์ƒ์˜ ์ด์ ๋„ ์žˆ๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ์ด์™€ ๊ฐ™์ด ํ‘œ๊ธฐํ•˜๋ฏ€๋กœ ํŠน๋ณ„ํ•œ ์ด์œ ๊ฐ€ ์—†๋‹ค๋ฉด ์ด ๋ฐฉ์‹์„ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ํŽธ๋ฆฌํ•˜๋‹ค. โˆˆ { , } Class 0 ( = ): "Negative class" Class 1 ( = ): "Positive class" Linear Regression for Classification? ์–ด์ฉŒ๋ฉด linear regression์„ classification ๋ฌธ์ œ์—๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ? ์˜์‚ฌ์ธ ๊ฐœ๋˜ฅ์ด๋Š” ์ง‘๊ฐ’์„ ์ถ”์ •ํ•˜๋Š” ๋ฐ์— linear regression์„ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐฉ๋ฒ•์„ ์ง„๋‹จ์„ ๋‚ด๋ฆฌ๋Š” ๋ฐ์—๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์„์ง€ ๊ถ๊ธˆํ•ด์กŒ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ข…์–‘ ํฌ๊ธฐ์— ๋”ฐ๋ผ ์•…์„ฑ์ธ์ง€ ์–‘์„ฑ์ธ์ง€ ๊ตฌ๋ถ„๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์‹คํ—˜์„ ํ•ด๋ณด์•˜๋‹ค. Linear regression์„ ์ด์šฉํ–ˆ๋”๋‹ˆ ๋…น์ƒ‰ ์„ ์œผ๋กœ ์ด ๋ฐ์ดํ„ฐ๊ฐ€ ํ‘œํ˜„๋˜์—ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๊ตฌํ•œ hypothesis function ์ด '0.5' ๊ฐ€ ๋˜๋Š” ์ข…์–‘ ํฌ๊ธฐ๋ฅผ threshold๋กœ ์žก์•„์„œ ๊ทธ๋ณด๋‹ค ํฌ๋ฉด '์•…์„ฑ(malignant), ๊ทธ๋ณด๋‹ค ์ž‘์œผ๋ฉด '์–‘์„ฑ(benign)'์ด๋ผ๊ณ  ํŒ๋ณ„ํ•ด ๋ณด์•˜๋‹ค. ๊ทธ๋žฌ๋”๋‹ˆ classification์ด ์ œ๋ฒ• ์ž˜ ๋˜์—ˆ๋‹ค! ์ด๋•Œ์—๋Š” linear regression์„ ํ†ตํ•œ classification๋„ ์ œ๋ฒ• ์“ธ๋งŒํ•œ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ํ•ญ์ƒ ๊ทธ๋Ÿด๊นŒ? If ฮธ ( ) 0.5 , predict " = " If ฮธ ( ) 0.5 , predict " = " ํ˜ธ๊ธฐ์‹ฌ ๋งŽ์€ ๊ฐœ๋˜ฅ์ด๋Š” ์ด๋ฒˆ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋กœ ์‹œํ—˜ํ•ด ๋ณด์•˜๋‹ค. ์ด๋ฒˆ์—๋Š” ์•„์ฃผ ํฐ ์ข…์–‘์ด ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. ์ด๋•Œ linear regression ๊ฒฐ๊ณผ๋Š” ํŒŒ๋ž€ ์„ ์ด ๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํŒŒ๋ž€ ์„ ์„ ๊ธฐ์ค€์œผ๋กœ threshold๋ฅผ ์žก์„ ๊ฒฝ์šฐ classification์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด๊ฒƒ์€ linear regression์ด ์‹คํŒจํ•˜๋Š” ํŠน์ˆ˜ํ•œ ์˜ˆ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ linear regression์„ classification์— ์‚ฌ์šฉํ•˜๋ฉด ์ž˜๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์ด ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์–ด๋–ค feature (์ข…์–‘ ํฌ๊ธฐ)๊ฐ€ class (์•…์„ฑ/์–‘์„ฑ)์„ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ๋„ linear regression์„ ์ด์šฉํ•˜๋Š” classification์€ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ์˜ํ•˜์ž. ๋˜ ํ•œ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๊ด€์‹ฌ ์žˆ๋Š” classification ๋ฌธ์ œ์˜ target๋Š” 0 ๋˜๋Š” 1์˜ ๊ฐ’๋งŒ ๊ฐ–๋Š”๋‹ค. ๊ทธ๋Ÿฐ๋ฐ linear regression์˜ ฮธ ( ) ๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ 0๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„ ๋‚ด๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ logistic regression์€ hypothesis function์ด 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’๋งŒ ๋‚ด๋ณด๋‚ด๋„๋ก ํ•œ๋‹ค. Logistic regression์€ ์ด๋ฆ„์— regression์ด ๋“ค์–ด์žˆ๊ธด ํ•˜์ง€๋งŒ classifier๋ผ๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜๋„๋ก ํ•˜์ž. 1) Hypothesis Representation Logistic Regression Model Interpretation of Hypothesis Output Logistic Regression Model Logistic regression์˜ hypothesis function์€ 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’๋งŒ ๋‚ด๋ณด๋‚ด๋Š” ํ˜•ํƒœ๊ฐ€ ๋˜๋„๋ก ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ฆ‰, โ‰ค ฮธ ( ) 1 ๋‹ค์Œ์€ ์ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉฐ ๋ฏธ๋ถ„ ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜์˜ ์˜ˆ์ด๋‹ค. ( ) 1 + โˆ’ ์ด ํ•จ์ˆ˜๋Š” 'sigmoid function' ๋˜๋Š” 'logistic function'์ด๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค. ์ด ๋•Œ ์— ์— ๊ด€ํ•œ ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด hypothesis๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. Linear regression hypothesis์ธ T๋ฅผ ๋Œ€์ž…ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•ด ๋ณด์ž. ฮธ ( ) 1 + โˆ’ T ์ด์™€ ๊ฐ™์ด Logistic function์„ hypothesis๋กœ ์“ฐ๊ธฐ ๋•Œ๋ฌธ์— logistic regression์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋‹ค์Œ ๋งํฌ์—์„œ logistic hypothesis์˜ parameter๋ฅผ ์ง์ ‘ ๋ฐ”๊พธ์–ด๊ฐ€๋ฉฐ ํ•จ์ˆ˜์˜ ๋ชจ์–‘์ด ์–ด๋–ป๊ฒŒ ๋‹ฌ๋ผ์ง€๋Š”์ง€ ๊ด€์ฐฐํ•ด ๋ณด์ž. https://www.desmos.com/calculator/bgontvxotm Interpretation of Hypothesis Output Hypothesis function์˜ ์ถœ๋ ฅ๊ฐ’์€ '์ฃผ์–ด์ง„ feature ๊ฐ€๋ผ๋Š” ๊ฐ’์„ ๊ฐ€์งˆ ๋•Œ class 1์— ๋“ค์–ด๊ฐˆ ํ™•๋ฅ '์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š”๋‹ค. ฮธ ( ) P ( = | ; ) : feature : model parameter = (class 1) ์ผ ํ™•๋ฅ ๊ณผ = (class 0) ์ผ ํ™•๋ฅ ์€ ํ•ฉ์ด 1์ด ๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊ธฐ์–ตํ•ด๋‘์ž. ( = | ; ) P ( = | ; ) 1 ์˜ˆ๋ฅผ ๋“ค์–ด, ฮธ ( ) 0.7 ์ด๋ž€, output์ด 1์ผ ํ™•๋ฅ ์ด 70%์ด๋ฉฐ output์ด 0์ผ ํ™•๋ฅ ์€ 30%๋ผ๋Š” ๋œป์ด๋‹ค. 2) Decision Boundary Decision Boundary Non-Linear Decision Boundaries Logistic regression์˜ hypothesis function์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค๊ณ  ํ•˜์˜€๋‹ค. ์ด๋•Œ ์šฐ๋ฆฌ๋Š” feature ๊ฐ€ ์–ด๋–ค ๊ฐ’์„ ๊ฐ€์ง€๋ฉด Class 1์— ๋“ค์–ด๊ฐ€๋Š”์ง€ ( = ), ํ˜น์€ Class 0์— ๋“ค์–ด๊ฐ€๋Š”์ง€ ( = ) ์•Œ๊ณ ์ž ํ•œ๋‹ค. ๊ทœ์น™์€ ๊ฐ„๋‹จํ•˜๋‹ค. Hypothesis function์ด 0.5๊ฐ€ ๋„˜์œผ๋ฉด Class 1์—, 0.5 ๊ฐ€ ๋˜์ง€ ์•Š์œผ๋ฉด Class 0์— ๋„ฃ๊ธฐ๋กœ ํ•˜์ž. ์ฆ‰, ๋‹ค์Œ ๊ทœ์น™์— ๋”ฐ๋ผ y ๊ฐ’์„ predict ํ•œ๋‹ค. = { if ฮธ ( ) 0.5 0 if ฮธ ( ) 0.5 ๊ทธ๋Ÿฐ๋ฐ hypothesis function ฮธ ( ) g ( ) ์ด 0.5 ์ด์ƒ์ด ๋˜๋Š” ๊ฒฝ์šฐ๋Š” = T โ‰ฅ ์ผ ๋•Œ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  0.5 ์ดํ•˜๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๋Š” T < ์ผ ๋•Œ์ด๋‹ค. ๋”ฐ๋ผ์„œ, T = ์ผ ๋•Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ class ๊ฐ€ ๋‚˜๋‰˜๊ฒŒ ๋œ๋‹ค. = { if T โ‰ฅ 0 if T < Decision Boundary Decision boundary๋Š” = ๊ณผ = ์„ ๊ฐ€๋ฅด๋Š” ๊ฒฝ๊ณ„์„ ์„ ๋งํ•˜๋ฉฐ, hypothesis function์— ์˜ํ•ด ๊ฒฐ์ •๋œ๋‹ค. Decision boundary: ฮธ ( ) 0.5 , i.e. T = ๊ฐ€๋ น ๋‹ค์Œ๊ณผ ๊ฐ™์€ data๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๋•Œ, ๋ชจ์ข…์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์ตœ์ ์˜ parameter๋ฅผ ์ฐพ์•˜๋‹ค๊ณ  ํ•˜์ž. (Parameter๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์€ ๋‚˜์ค‘์— ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค.) ์ด ์ตœ์ ์˜ parameter๋ฅผ ๊ฐ€์ง€๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๋ก ์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ๋‹ค: T = 3 x + 2 0 ์ด๋ฉด Class 1 ( = )์— ๋„ฃ๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด Class 0 ( = )์— ๋„ฃ๋Š”๋‹ค. Predict = if 3 x + 2 ฮธ x 0 i.e. 1 x โ‰ฅ ์ด๋•Œ์˜ decision boundary๋Š” 3 x + 2 0 ์ด๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ๋…น์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ์„ ์ด๋‹ค. ์ด์ œ training set์— ๋“ค์–ด์žˆ์ง€ ์•Š์•˜๋˜, ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋ฉด 1 ๊ฐ’๊ณผ 2 ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ์ขŒํ‘œ๋ฅผ ์ฐ์–ด๋ณด๊ณ , decision boundary๋ณด๋‹ค ์œ„์ชฝ์— ์ฐํžˆ๋ฉด class 1์—, ์•„๋ž˜์ชฝ์— ์ฐํžˆ๋ฉด class 0์— ๋„ฃ์œผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋•Œ, decision boundary๋Š”์— ์˜ํ•ด ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ์ž„์„ ๊ธฐ์–ตํ•˜์ž. Training data๋Š” parameter๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐ์— ์ด์šฉ๋  ๋ฟ, decision boundary์— ์ง์ ‘์ ์œผ๋กœ ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€๋Š” ์•Š๋Š”๋‹ค. Non-Linear Decision Boundaries ์•ž์˜ ์˜ˆ๋Š” linearly separable ํ•œ ๋ฐ์ดํ„ฐ์˜€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ง์„ ์œผ๋กœ class๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์›ํ˜•์˜ decision boundary๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ polynomial ํ•˜๊ฒŒ feature dimension์„ ๋†’์ด๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด non-linear decision boundary๋„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์˜ˆ์‹œ์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” sigmoid function์˜ input์„ ์›์˜ ๋ฐฉ์ •์‹์œผ๋กœ ๋Œ€์ฒดํ•˜์—ฌ ๋ฐ์ดํ„ฐ์— ์ ์ ˆํ•œ hypothesis๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. 3) Cost Function & Gradient Descent Logistic Regression Cost Function Gradient Descent Appendix: Partial derivative of ( ) Linear regression์—์„œ๋Š” LSE criterion์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. Linear regression: ( ) 1 โˆ‘ = m 2 ( ฮธ ( ( ) y ( ) ) ) โŸ cost ( ฮธ ( ) y ) ์ด criterion์„ logistic regression์— ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ? ฮธ ( ) 1 + โˆ’ T๋ฅผ ๋Œ€์ž…ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ทธ ๊ฒฝ์šฐ, cost function์ด non-convex function์ด ๋œ๋‹ค. Non-convex cost function์€ local optima์— ๋น ์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ logistic regression์˜ cost function์€ ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ์ •์˜ํ•œ๋‹ค. Logistic Regression Cost Function Logistic regression์˜ cost function์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค. cost ( ฮธ ( ) y ) { log ( ฮธ ( ) ) if = โˆ’ log ( โˆ’ ฮธ ( ) ) if = ์ฆ‰, ์•„๋ž˜ ๊ทธ๋ž˜ํ”„์™€ ๊ฐ™์ด = ์ผ ๋•Œ์™€ = ์ผ ๋•Œ ์„œ๋กœ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋ฅด๊ฒŒ ๋œ๋‹ค. = ์ผ ๋•Œ cost function์˜ ํŠน์ง•์€, cost 0 when ฮธ ( ) 1 cost โˆž as ฮธ ( ) 0 ์ด๋Š” = and ฮธ ( ) 0 ์ฆ‰, ( = | ; ) 0 ์ธ ๊ฒฝ์šฐ learning algorithm์— ํฐ penalty๋ฅผ ์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ์ง๊ด€์— ๋ถ€ํ•ฉํ•œ๋‹ค. ์ •๋ฆฌํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Logistic regression: ( ) 1 โˆ‘ = m cost ( ฮธ ( ( ) ) y ( ) ) where cost ( ฮธ ( ) y ) { log ( ฮธ ( ) ) if = โˆ’ log ( โˆ’ ฮธ ( ) ) if = ๊ทธ๋Ÿฐ๋ฐ๋Š” ์˜ค์ง 1 ๋˜๋Š” 0์ด๋ผ๋Š” ๊ฐ’๋งŒ ๊ฐ€์ง€๋ฏ€๋กœ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. cost ( ฮธ ( ) y ) โˆ’ log ( ฮธ ( ) ) ( โˆ’ ) log ( โˆ’ ฮธ ( ) ) if = , cost ( , ) โˆ’ log ( ฮธ ( ) ) if = , cost ( , ) โˆ’ log ( โˆ’ ฮธ ( ) ) ์ตœ์ข…์ ์œผ๋กœ, logistic regression์˜ cost function์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•œ๋‹ค. Logistic regression: ( ) โˆ’ m i 1 [ ( ) log h ( ) ( โˆ’ ( ) ) log ( โˆ’ ฮธ ( ) ) ] ์ด cost function์˜ ํŠน์ง•์€, Maximum likelihood estimation criterion Convex ์ด์ œ ์ตœ์ ์˜ parameter๋ฅผ ์ฐพ์œผ๋ ค๋ฉด min J ( ) ๋ฅผ ๊ตฌํ•˜๊ณ , ๊ทธ ํ›„์— ์ฃผ์–ด์ง„ ์ƒˆ๋กœ์šด ๋ฅผ ์–ด๋–ค class์— ๋„ฃ์„์ง€ ํŒ๋‹จํ•˜๋ ค๋ฉด ฮธ ( ) 1 + exp ( ฮธ x ) ๊ฐ€ 0.5๋ณด๋‹ค ํฐ์ง€ ์ž‘์€์ง€๋ฅผ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค. Gradient Descent ( ) โˆ’ m i 1 [ ( ) log h ( ) ( โˆ’ ( ) ) log ( โˆ’ ฮธ ( ) ) ] ์ด cost function์„ ์ตœ์†Œํ™”ํ•˜๋Š” parameter๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋‹ค. ( ) ๊ฐ€ convex์ด๋ฏ€๋กœ, gradient descent์— ์˜ํ•ด optimal๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, Repeat{ ฮธ := j ฮฑ โˆ‚ j ( ) } simultaneously update all j ์ด๋•Œ โˆ‚ j ( ) 1 โˆ‘ = m ( ฮธ ( ( ) ) y ( ) ) j ( ) ์ด ์ˆ˜์‹๋งŒ ๋ณด๋ฉด linear regression์˜ gradient descent์™€ ๋™์ผํ•˜๋‹ค. ์œ ์ผํ•œ ์ฐจ์ด์ ์€ hypothesis function์ด ฮธ ( ) 1 + exp ( ฮธ x ) ๋กœ ๋ฐ”๋€ ๊ฒƒ์ด๋‹ค. Appendix: Partial derivative of ( ) ์šฐ์„  logistic funtion์˜ ๋ฏธ๋ถ„์„ ๊ตฌํ•ด๋‘์ž. ( ) = ( 1 e x ) = ( + โˆ’ ) ( + โˆ’ ) = 1 โˆ’ ( โˆ’ ) ( + โˆ’ ) = โˆ’ ( x ) ( โˆ’ ( + โˆ’ ) = ( 1 e x ) ( โˆ’ 1 e x ) ฯƒ ( ) ( 1 1 e x + โˆ’ ) ฯƒ ( ) ( + โˆ’ + โˆ’ ์ด์ œ ํŽธ๋ฏธ๋ถ„์„ ๊ตฌํ•ด๋ณด์ž. โˆ‚ j ( ) โˆ‚ ฮธ โˆ’ m i 1 [ ( ) log ( ฮธ ( ( ) ) ) ( โˆ’ ( ) โˆ’ m i 1 [ ( ) โˆ‚ j log ( ฮธ ( ( ) ) ) ( โˆ’ ( ) ) โˆ’ m i 1 [ ( ) โˆ‚ j ฮธ ( ( ) ) ฮธ ( ( ) ) ( โˆ’ ( ) ) โˆ’ โˆ’ m i 1 [ ( ) โˆ‚ j ( T ( ) ) ฮธ ( ( ) ) ( โˆ’ ( ) ) โˆ’ โˆ’ m i 1 [ ( ) ( T ( ) ) ( โˆ’ ( T ( ) ) ) โˆ‚ j T ( ) ฮธ ( ( ) ) โˆ’ ( โˆ’ = 1 โˆ‘ = m [ ( ) ฮธ ( ( ) ) ( โˆ’ ฮธ ( ( ) ) ) โˆ‚ j T ( ) ฮธ ( ( ) ) ( โˆ’ ( โˆ’ m i 1 [ ( ) ( โˆ’ ฮธ ( ( ) ) ) j ( ) ( โˆ’ โˆ’ m i 1 [ ( ) ( โˆ’ ฮธ ( ( ) ) ) ( โˆ’ = 1 โˆ‘ = m [ ( ) y ( ) ฮธ ( ( ) ) h ( ( ) ) โˆ’ m i 1 [ ( ) h ( ( ) ) 1 โˆ‘ = m [ ฮธ ( ( ) ) y ( ) ] 4) Advanced Optimization Gradient descent ์™ธ์—๋„ optimization algorithm์ด ์—ฌ๋Ÿฟ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, conjugate gradient, BFGS, L-BFGS ๋“ฑ์ด๋‹ค. ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์žฅ์ ์€ learning rate๋ฅผ ์ž๋™์œผ๋กœ ๊ณจ๋ผ์ฃผ๋ฉฐ, gradient descent๋ณด๋‹ค ๋น ๋ฅธ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค๋Š” ์ ์ธ๋ฐ, ๋ณต์žกํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ์ด ๊ฐ•์˜์—์„œ๋Š” ์ด๋Ÿฌํ•œ optimization ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์— ๊ด€ํ•ด ์ž์„ธํžˆ ๋‹ค๋ฃจ์ง€๋Š” ์•Š๊ณ , ๊ฐ„๋žตํžˆ ์†Œ๊ฐœ๋งŒ ํ•ด์ฃผ์—ˆ๋‹ค. 5) Multi-Class Classification One-vs-All (One-vs-Rest) ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฐ›์€ e-mail์„ ์ž๋™์œผ๋กœ ๋ถ„๋ฅ˜ํ•œ๋‹ค๊ณ  ํ•˜์ž. E.g. Email foldering/tagging: work y 1 friends y 2 family y 3 hobby y 4 ์ด ๊ฒฝ์šฐ binary classifier๋กœ๋Š” ๋ถˆ์ถฉ๋ถ„ํ•˜๋‹ค. ์ด์ฒ˜๋Ÿผ class ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ผ ๋•Œ์—๋Š” ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ๋ ๊นŒ? One-vs-All (One-vs-Rest) ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ๊ฐ class ๋ณ„๋กœ ํ•ด๋‹น class vs ๋‚˜๋จธ์ง€ class๋กœ binary decision์„ ๋‚ด๋ฆฌ๋„๋ก ๋งŒ๋“ค๊ณ  hypothesis function ๊ฐ’์ด ๊ฐ€์žฅ ํฐ ๊ฒƒ์„ ๊ณ ๋ฅด๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ class๋งˆ๋‹ค hypothesis function์„ train ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ hypothesis function์ด ๋‚ด๋ณด๋‚ด๋Š” ๊ฐ’์€ '์ฃผ์–ด์ง„ ๊ฐ€ class 1์— ๋“ค์–ด๊ฐˆ ํ™•๋ฅ '๋กœ์„œ ํ•ด์„๋œ๋‹ค. ๋”ฐ๋ผ์„œ, hypothesis function ์ถœ๋ ฅ์ด ๊ฐ€์žฅ ํฌ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ class์— ๋“ค์–ด๊ฐˆ ํ™•๋ฅ ์ด ๊ฐ€์žฅ ํฌ๋‹ค๋Š” ๋œป์ด๋‹ค. ฮธ ( ) ( ) P ( = | ; ) i { , , } ์ •๋ฆฌํ•˜์ž๋ฉด, ๊ฐ class์— ๋“ค์–ด๊ฐˆ ํ™•๋ฅ  ( = ) ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด regression classifier ฮธ ( ) ( ) ๋ฅผ train ํ•œ๋‹ค. ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์œผ๋ฉด, ฮธ ( ) ( ) ๊ฐ€ ์ตœ๋Œ€์ธ class๋ฅผ ์„ ํƒํ•œ๋‹ค. ์ฆ‰, arg max h ( ) ( ) 04. Regularization The Problem of Overfitting Adressing Overfitting Reduce the number of features Regularization The Problem of Overfitting Feature๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„๋„ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค. Hypothesis function ๋„ˆ๋ฌด ๋ณต์žกํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ๋ณต์žกํ•œ ํ•จ์ˆ˜๋Š” training set์˜ ๋ฐ์ดํ„ฐ ๋ถ„ํฌ๋ฅผ ๊ฑฐ์˜ ๋˜‘๊ฐ™์ด ๋ชจ๋ธ๋ง ํ•  ์ˆ˜๋Š” ์žˆ์„ ๊ฒƒ์ด๋‹ค. ( ( ) 0 ) ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์€ training set๊ณผ ์™„๋ฒฝํ•˜๊ฒŒ ๋˜‘๊ฐ™์€ ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ training set์— ์—†๋Š” ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ target์„ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ training data์— ์ง€๋‚˜์น˜๊ฒŒ ๋งž์ถฐ์ง„ ๋ชจ๋ธ์€ ์˜คํžˆ๋ ค ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฐ์—๋Š” ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์™€ ๊ฐ™์ด training data์— ์ง€๋‚˜์น˜๊ฒŒ(over) fit ๋˜์–ด ์ผ๋ฐ˜์ ์ธ ์ถ”์„ธ๋ฅผ ํ‘œํ˜„ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๋ฅผ overfitting์ด๋ผ๊ณ  ํ•œ๋‹ค. Adressing Overfitting ๊ทธ๋ ‡๋‹ค๋ฉด overfitting์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ป๊ฒŒ ํ•  ์ˆ˜ ์žˆ์„๊นŒ. Reduce the number of features ์ผ๋‹จ์€ feature๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์‚ฌ์šฉํ•  feature๋ฅผ ์ˆ˜๋™์œผ๋กœ ๊ณ ๋ฅธ๋‹ค. Model selection algorithm (later in course) ๊ทธ๋Ÿฌ๋‚˜ feature๋ฅผ ๋ฒ„๋ฆฌ๊ธฐ ์•„๊นŒ์šธ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ฐ€๋ น, ๋ชจ๋“  feature๊ฐ€ ์กฐ๊ธˆ์”ฉ์€ traget ์˜ˆ์ธก์— ๋„์›€์ด ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋•Œ์—๋Š” feature ๊ฐœ์ˆ˜๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ overfitting์„ ํ”ผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์“ด๋‹ค. Regularization์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ์ด ๋ฐฉ๋ฒ•์€ ๋ชจ๋ธ์ด ๋„ˆ๋ฌด ๋ณต์žกํ•ด์ง€์ง€ ์•Š๋„๋ก ์•ฝ๊ฐ„์˜ ์ œ์•ฝ์„ ๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. Regularization ๋ชจ๋“  feature๋ฅผ ์œ ์ง€ํ•˜๋˜, parameter j ์˜ ํฌ๊ธฐ๋ฅผ ์ž‘๊ฒŒ<NAME>๋‹ค. ๋งŽ์€ feature ๊ฐ€๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฐ์— ์กฐ๊ธˆ์”ฉ ๊ธฐ์—ฌํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋‹ค. 1) Cost Function Intuition Regularization Intuition ์ด๋•Œ, 3 ฮธ์˜ ๊ฐ’์ด ์ปค์งˆ์ˆ˜๋ก penalty๋ฅผ ์ฃผ์–ด๋ณด์ž. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด ๊ฐ’๋“ค์— 1000์„ ๊ณฑํ•œ ๊ฐ’์„ cost function์— ๋”ํ•ด๋ณธ๋‹ค. ์ด๋กœ์จ ์ด parameter๋“ค์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘๊ฒŒ ์œ ์ง€๋œ๋‹ค. min 1 m i 1 ( ฮธ ( ( ) ) y ( ) ) + 1000 3 + 1000 4 โ‡’ 3 0 ฮธ โ‰ˆ ์ด์™€ ๊ฐ™์ด 4์ฐจ ํ•จ์ˆ˜ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ 2์ฐจ ํ•จ์ˆ˜ ๋ชจ๋ธ์— ๊ฑฐ์˜ ๊ทผ์ ‘ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. Regularization Parameters 0 ฮธ, 2. . ฮธ ์ด ์ž‘์€ ๊ฐ’์„ ๊ฐ–๋„๋ก ํ•œ๋‹ค. "๋‹จ์ˆœํ•œ" hypothesis Overfitting ๊ฐ€๋Šฅ์„ฑ์ด ์ ์–ด์ง eg. Housing features: 1 x, . , 100 (We don't know which feature is important) parameters: 0 ฮธ, . , 100 ( ) 1 m i 1 ( ฮธ ( ( ) ) y ( ) ) + โˆ‘ = 100 j min J ( ) ์ผ์ข…์˜ penalty๋ฅผ ๊ณฑํ•ด์ฃผ๋Š” ๊ฒƒ์œผ๋กœ parameter์˜ ํฌ๊ธฐ๋ฅผ ์ž‘๊ฒŒ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋งŒ์•ฝ ๊ฐ€ ๋„ˆ๋ฌด ํฌ๋ฉด (eg. = 10 10 ), j 0 ๊ฐ€ ๋˜์–ด ์ด๋ฒˆ์—๋Š” underfit ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ ์ ˆํ•œ ๊ฐ’์„ ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. 2) Regularized Linear Regression Regularized Linear Regression Gradient Descent Normal Equation Non-Invertibility ์ด๋ฒˆ์—๋Š” regularization์„ linear regression์— ์ ์šฉํ•˜์—ฌ ๋ณด์ž. Regularized Linear Regression ( ) 1 m [ i 1 ( ฮธ ( ( ) ) y ( ) ) + โˆ‘ = n j ] min J ( ) Linear regression์˜ ์ตœ์ ์˜ parameter๋ฅผ ์ฐพ๋Š” ๋ฐ์—๋Š” gradient descent์™€ normal equation์„ ์ด์šฉํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. Gradient Descent Normal Equation ํ•œํŽธ, regularization์€ feature ๊ฐœ์ˆ˜์— ๋น„ํ•ด data ์ˆ˜๊ฐ€ ๋ถ€์กฑํ•  ๋•Œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” non-invertibility ๋ฌธ์ œ๋„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ์— ๋„์›€์ด ๋œ๋‹ค. Non-Invertibility 3) Regularized Logistic Regression Cost Function Gradient Descent ์ด๋ฒˆ์—๋Š” logistic regression์— regularization์„ ์ ์šฉํ•œ๋‹ค. Cost Function ( ) โˆ’ [ m i 1 y ( ) log h ( ( ) ) ( โˆ’ ( ) ) log ( โˆ’ ฮธ ( ( ) ) ) ] ฮป m Gradient Descent 05. Neural Networks Non-linear Hypotheses Neural Networks Neurons and the Brain: The "One Learning Algorithm" Hypothesis Non-linear Hypotheses ์ง€๊ธˆ๊นŒ์ง€ ์•Œ์•„๋ณธ linear regression์ด๋‚˜ logistic regression์œผ๋กœ๋Š” ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ์— ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค. ๊ฐ€๋ น, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ„ํฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์—๋Š” non-linear decision boundary๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๋ฌผ๋ก , logistic regression์„ ์ด์šฉํ•ด์„œ non-linear boundary๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๊ธด ํ•˜๋‹ค. ๊ฐ€๋ น, ๋‹ค์Œ๊ณผ ๊ฐ™์ด 3๊ฐœ feature๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š” 2์ฐจ ํ•ญ๋“ค์„ ํฌํ•จํ•˜๋Š” hypothesis๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ํ•˜์ž. ( 0 ฮธ x 2 ฮธ x x ฮธ x x + 4 2 + 5 2 3 ฮธ x 2 ) ์ด ๊ฒฝ์šฐ, ์ƒˆ๋กœ์šด 6๊ฐœ feature๋ฅผ ์ด์šฉํ•œ ๊ฒƒ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ์›๋ž˜ 3๊ฐœ feature๋ฅผ ๋‘ ๊ฐœ์”ฉ ๋ฌถ๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ( + โˆ’ ) 2 ( โˆ’ ) = ์ด ์ƒˆ feature ๊ฐœ์ˆ˜๊ฐ€ ๋œ๋‹ค (๊ณ„์‚ฐ๋ฒ•์€ ์—ฌ๊ธฐ ์ฐธ๊ณ ). ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ์›๋ž˜ feature ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ๋‹ค๋ฉด ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•˜๋Š” ๋ฐ์—๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ง‘๊ฐ’ ์˜ˆ์ธก ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ์— 100๊ฐœ feature๊ฐ€ ํ•„์š”ํ•˜๋‹ค๊ณ  ํ•˜์ž. ์ด ๊ฒฝ์šฐ 5,050๊ฐœ๋ผ๋Š” ์–ด๋งˆ์–ด๋งˆํ•œ ์–‘์˜ ์ƒˆ feature๊ฐ€ ์ƒ๊ฒจ๋‚œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์‚ฌ์‹ค์ƒ์˜ feature ๊ฐœ์ˆ˜๊ฐ€ ๊ธฐํ•˜๊ธ‰์ˆ˜์ ์œผ๋กœ ์ฆ๊ฐ€ํ•ด์„œ ์—ฐ์‚ฐ๋Ÿ‰์ด ๋Š˜์–ด๋‚  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ overfitting์˜ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง„๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด non-linear boundary classification์„ ํ•  ๋•Œ ์ข€ ๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด ์—†์„๊นŒ. Neural Networks Neural net์€ ์šฐ๋ฆฌ์˜ ๋‘๋‡Œ๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๋ฐฉ์‹์„ ์ œํ•œ์ ์œผ๋กœ ๋ชจ๋ฐฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ, ๋‡Œ์˜ ์‹ ๊ฒฝ์„ธํฌ๋ฅผ ๋ชจ๋ฐฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ๋ฐœํ•˜๋Š” ๊ณผ์ •์—์„œ ๋“ฑ์žฅํ–ˆ๋‹ค. 80๋…„๋Œ€~90๋…„๋Œ€ ์ดˆ๋ฐ˜์— ๋„๋ฆฌ ์ด์šฉ๋˜๋‹ค๊ฐ€ 90๋…„๋Œ€ ํ›„๋ฐ˜ ์ธ๊ธฐ๊ฐ€ ์‹œ๋“ค์—ˆ์œผ๋‚˜, ์ตœ๊ทผ ํ•˜๋“œ์›จ์–ด ๋ฐœ์ „์— ํž˜์ž…์–ด ์žฌ์กฐ๋ช… ๋ฐ›๊ณ  ์žˆ๋‹ค. ํ˜„์žฌ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ state-of-the-art ๊ธฐ์ˆ ์ด ๋˜๊ณ  ์žˆ๋‹ค. Neurons and the Brain: The "One Learning Algorithm" Hypothesis (๋™๋ฌผ์˜) ๊ท€์™€ auditory cortex ์‚ฌ์ด๋ฅผ ์ ˆ๋‹จํ•ด์„œ ๊ฑฐ๊ธฐ์— ์‹œ์‹ ๊ฒฝ์„ ์—ฐ๊ฒฐํ•˜๋ฉด ๋ณด๋Š” ๋ฒ•์„ ๋ฐฐ์šด๋‹ค๊ณ  ํ•œ๋‹ค. ์ฆ‰, ๋‡Œ๊ฐ€ ํ•™์Šตํ•˜๋Š” ๊ธฐ์ž‘์€ ๋‹จ ํ•œ ๊ฐ€์ง€๋ฟ์ด๊ณ , ์–ด๋–ค ๊ฐ๊ฐ๊ธฐ๊ด€์—์„œ ๋ฐ›์€ ์‹ ํ˜ธ์ด๋“  ๊ทธ '์•Œ๊ณ ๋ฆฌ์ฆ˜'์— ๋”ฐ๋ผ ์ง€๊ฐํ•œ๋‹ค๋Š” ๊ฐ€์„ค์„ ๋’ท๋ฐ›์นจํ•˜๋Š” ์ฆ๊ฑฐ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ด๋Ÿฌํ•œ ์„ฑ์งˆ์„ "neuroplasticity"๋ผ๊ณ  ํ•œ๋‹ค. 1) Model Representation Neurons in the Brain Neuron Model: Logistic Unit Neural Network Forward propagation: Vectored Implementation Examples and Intuitions I Examples and Intuitions II Neural Network Learning its Own Features Neurons in the Brain ์‹ ๊ฒฝ์„ธํฌ๋ฅผ ๋‹จ์ˆœํ™”ํ•ด ํ•˜๋‚˜์˜ computational unit์œผ๋กœ ์ƒ๊ฐํ•ด ๋ณด์ž. ๋‰ด๋Ÿฐ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž…๋ ฅ๋‹จ(dendrites)์— ์ „๊ธฐ ์‹ ํ˜ธ (spikes)๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•ด ์ถœ๋ ฅ๋‹จ(axon)์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Neuron Model: Logistic Unit ์ƒ๋ฌผํ•™์  '๋‰ด๋Ÿฐ'์„ ์ˆ˜ํ•™์  ๋ชจ๋ธ๋กœ ๋‚˜ํƒ€๋‚ด์–ด ๋ณด์ž. Dendrite์€ input feature 1. . x์œผ๋กœ, axon์€ hypothesis function์˜ output์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค. ์ด ๋ชจ๋ธ์—์„œ 0 ์€ "bias unit"์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋ฉฐ ํ•ญ์ƒ 1์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ ๊ตณ์ด ๋‚˜ํƒ€๋‚ด์ง€ ์•Š๊ธฐ๋„ ํ•œ๋‹ค. NN์€ classification์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ logistic function ( 1 exp ( ฮธ x ) )์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, sigmoid (logistic) activation function์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค. ์ด ๋•Œ์˜ ๋Š” "weights"๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€๋Š” "single neuron"์— ๊ด€ํ•œ ๋ชจ๋ธ์ด๋‹ค. ์ด์ œ ๋‰ด๋Ÿฐ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ ค๋ณด์ž. Neural Network Neural net์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ด์–ด ๋ณด์ž. [ 0 1 2 ] [ ] h ( ) ์šฐ๋ฆฌ์˜ ์ฒซ ๋ฒˆ์งธ node (layer 1) ์€ ๋‹ค๋ฅธ node (layer 2)๋กœ ์—ฐ๊ฒฐ๋˜๊ณ , ๋งˆ์ง€๋ง‰์—” hypothesis function์„ ํ†ตํ•ด output์ด ๊ณ„์‚ฐ๋œ๋‹ค. ์ฒซ ๋ฒˆ์งธ layer๋Š” "input layer"๋ผ๊ณ  ํ•˜๋ฉฐ ๋งˆ์ง€๋ง‰ layer๋Š” "output layer"๋ผ๊ณ  ํ•œ๋‹ค. ์ด output layer๊ฐ€ ์ตœ์ข… ๊ณ„์‚ฐ๋œ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. input๊ณผ output ์ค‘๊ฐ„์˜ layer๋“ค์€ "hidden layer"๋ผ๊ณ  ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ค‘๊ฐ„, ํ˜น์€ "hidden" layer์˜ node๋“ค์„ 0 ( ) . . a ( ) ๋กœ ๋‚˜ํƒ€๋‚ด๊ณ  "activation unit"์ด๋ผ ๋ถ€๋ฅธ๋‹ค. Hidden layer๊ฐ€ ํ•˜๋‚˜๋ผ๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. [ 0 1 2 3 ] [ 1 ( ) 2 ( ) 3 ( ) ] h ( ) i ( ) : "activation" of unit in layer ฮ˜ ( ) : matrix of weights controlling function mapping from layer to layer + ๊ฐ๊ฐ์˜ activation node์˜ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐํ•œ๋‹ค. ์ฆ‰, ๊ฐ activation node๋Š” 3x4 matrix์˜ paramter์— ์˜ํ•ด ๊ณ„์‚ฐ๋œ๋‹ค. ๊ฐ ํ–‰์˜ paramter๋ฅผ input์— ๊ณฑํ•˜์—ฌ ํ•œ activation node์˜ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค. hypothesis output์€ activation node ๊ฐ’์˜ ํ•ฉ์— logistic funciton์„ ์ ์šฉํ•˜์—ฌ ๊ตฌํ•˜๋ฉฐ, ์ด๋Š” ๋‹ค์‹œ ๋‹ค๋ฅธ parameter matrix ( ) , ๋‘ ๋ฒˆ์งธ ๋ ˆ์ด์–ด์˜ weights'์— ํ•ด๋‹นํ•˜๋Š”,์— ๊ณฑํ•ด์ง„๋‹ค. ๊ฐ ๋ ˆ์ด์–ด๋Š” ๊ณ ์œ ํ•œ weight matrix ( ) ๋ฅผ ๊ฐ–๋Š”๋‹ค. Network๊ฐ€ layer์— j ๊ฐœ unit์„ ๊ฐ–๊ณ , layer +์— j 1 unit์„ ๊ฐ–๋Š”๋‹ค๋ฉด, ( ) ์˜ dimension์€ j 1 ( j 1 ) ์ด๋‹ค. "+1" ์€ "bias node" 0 ฮ˜ ( ) ๋•Œ๋ฌธ์— ์ƒ๊ธด๋‹ค. ์ฆ‰, output node๋Š” bias node๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๋ฐ˜๋ฉด input์—๋Š” ํฌํ•จ๋œ๋‹ค. Forward propagation: Vectored Implementation ์ด๋ฒˆ์—๋Š” ์•ž์˜ ํ•จ์ˆ˜๋“ค์„ vector๋กœ ํ‘œํ˜„ํ•ด ๋ณด๋„๋ก ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ํ•ด ๋ณ€์ˆ˜ k ( ) ๋ฅผ ์ •์˜ํ•œ๋‹ค. ์ด ๋ณ€์ˆ˜๋Š” ( ) ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜๋“ค์„ ๋ฐ˜์˜ํ•œ๋‹ค. ์•ž์˜ ์˜ˆ์‹œ๋ฅผ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๋‹ค์‹œ ๋‚˜ํƒ€๋‚ด์–ด ๋ณด์ž. ์ด์ œ ์ž…๋ ฅ๊ฐ’๋“ค์„ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ด์–ด ๋ณด์ž. = [ 0 1 x ] z ( ) [ 1 ( ) 2 ( ) z ( ) ] ์ด๋•Œ = ( ) ์ด๋ผ ๋‚˜ํƒ€๋‚ด๋ฉด, ๊ด€๊ณ„์‹์„ ๋”์šฑ ๊ฐ„๋‹จํžˆ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, layer 2์—์„œ, ( ) [ 1 ( ) 2 ( ) 3 ( ) ] ฮ˜ ( ) = ( ) ( ) ์ด๋•Œ, ( ) g ( ( ) ) R์ด์ง€๋งŒ, bias unit 0 ( ) 1 ์„ ์ถ”๊ฐ€ํ•ด์„œ ์ •์˜ํ•˜๋Š” ํŽธ์ด ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ์— ๋” ์ข‹๋‹ค. ์ด์ œ, ( ) R ๊ฐ€ ๋˜๋ฉฐ, ( ) ฮ˜ ( ) ( ) ์ผ๋ฐ˜ layer์— ๋Œ€ํ•˜์—ฌ ๋‚˜ํƒ€๋‚ด๋ฉด, ( ) ฮ˜ ( โˆ’ ) ( โˆ’ ) ์ตœ์ข… vector๋Š” ( โˆ’ ) ๋‹ค์Œ ํ–‰๋ ฌ๊ณผ ๋ชจ๋“  activation node์—์„œ์˜ ๊ฐ’์˜ ๊ณฑ์œผ๋กœ ๊ตฌํ•œ๋‹ค. ์ตœ์ข… theta matrix ( ) ๋Š” ๋‹จ ํ•˜๋‚˜์˜ ํ–‰์„ ๊ฐ€์ง€๋ฏ€๋กœ, ๊ทธ ๊ฒฐ๊ณผ๋Š” single number๊ฐ€ ๋œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•ด์ง„๋‹ค. ฮ˜ ( ) a ( + ) g ( ( + ) ) ์ด ์˜ˆ์‹œ์—์„œ๋Š” ฮ˜ ( ) a ( ) g ( ( ) ) ์ด ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์—์„œ, layer ์™€ layer + ์‚ฌ์ด์—์„œ, logistic regression์—์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ์ •ํ™•ํžˆ ๊ฐ™์€ ๋ฐฉ์‹์ด ์“ฐ์ด๋Š” ์ ์„ ์ฃผ๋ชฉํ•˜์ž. ์ด๋ ‡๊ฒŒ ์ค‘๊ฐ„ layer ๋“ค์„ ์Œ“์•„ ์˜ฌ๋ฆผ์œผ๋กœ์จ ๋”์šฑ ๋ชฉ์žกํ•˜๊ณ  ํฅ๋ฏธ๋กœ์šด non-linear hypothesis๋ฅผ ์œ ๋ คํ•˜๊ฒŒ ์ƒ์„ฑํ•ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค. Examples and Intuitions I 1 AND 2 ๋ฅผ ์˜ˆ์ธกํ•˜๋Š” neural net ์˜ˆ์‹œ๋ฅผ ์•Œ์•„๋ณด์ž. ์—ฌ๊ธฐ์„œ AND๋Š” 1 x ๊ฐ€ ๋ชจ๋‘ ์ฐธ์ผ ๋•Œ์—๋งŒ '์ฐธ'์„ ์ถœ๋ ฅํ•˜๋Š” ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž์ด๋‹ค. [ 0 1 2 ] [ ( ( ) ) ] h ( ) ์ด๋•Œ์˜ 0 ์€ bias์ด๋ฉฐ ํ•ญ์ƒ 1๊ฐ’์„ ๊ฐ–๋Š”๋‹ค๋Š” ์ ์„ ์—ผ๋‘์— ๋‘ ์ž. theta matrix๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ์ž: ( ) [ 30 20 20 ] ์ด๋Š” ์šฐ๋ฆฌ์˜ hypothesis๊ฐ€ 1 x ๊ฐ€ ๋ชจ๋‘ 1์ผ ๋•Œ์—๋งŒ ์–‘์ˆ˜ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•  ๊ฒƒ์ด๋‹ค. ์ฆ‰, ฮ˜ ( ) g ( 30 20 1 20 2 ) 1 0 and 2 0 then ( 30 ) x = and 2 1 then ( 10 ) x = and 2 0 then ( 10 ) x = and 2 1 then ( 10 ) 1 ์ด์™€ ๊ฐ™์ด, ์‹ค์ œ AND gate๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹  ์ž‘์€ neural net์œผ๋กœ ๊ธฐ๋ณธ ์ปดํ“จํ„ฐ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. Neural net์€ ๋‹ค๋ฅธ ๋…ผ๋ฆฌ ๊ฒŒ์ดํŠธ๋ฅผ simulate ํ•˜๋Š” ๋ฐ์—๋„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Examples and Intuitions II AND, NOR, OR ์—ฐ์‚ฐ์„ ์œ„ํ•œ ( ) ํ–‰๋ ฌ์€, N : ( ) [ 30 20 20 ] O : ( ) [ 10 20 20 ] R ฮ˜ ( ) [ 10 20 20 ] ์ด๋“ค์„ ์กฐํ•ฉํ•˜์—ฌ 1 x ๊ฐ€ ๋ชจ๋‘ 0 ์ด๊ฑฐ๋‚˜ ๋ชจ๋‘ 1์ผ ๋•Œ์—๋งŒ '์ฐธ'์„ ์ถœ๋ ฅํ•˜๋Š” XNOR ์—ฐ์‚ฐ์ž๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. [ 0 1 2 ] [ 1 ( ) 2 ( ) ] [ ( ) ] h ( ) ์ฒซ ๋ฒˆ์งธ์™€ ๋‘ ๋ฒˆ์งธ ๋ ˆ์ด์–ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ( ) ์€ AND์™€ NOR์˜ ์กฐํ•ฉ์„ ์ด์šฉํ•œ๋‹ค. ( ) [ 30 20 20 10 20 20 ] ( ) ๋Š” OR๋ฅผ ์ด์šฉํ•œ๋‹ค. ( ) [ 10 20 20 ] ๊ฐ node์—์„œ์˜ ๊ฐ’์€ ( ) g ( ( ) x ) ( ) g ( ( ) a ( ) ) ฮ˜ ( ) a ( ) ์ด๋ ‡๊ฒŒ hidden layer 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด XNOR ์—ฐ์‚ฐ์ž๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. Neural Network Learning its Own Features ( ) ์ด๋ผ๋Š” hypothesis์— ์˜ํ•ด ( ) ๋ผ๋Š” ์ƒˆ feature๋ฅผ ์ƒ์„ฑํ•œ ๊ฒƒ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. 2) Cost Function Neural Network (Multiclass Classification) Cost Function Neural Network (Multiclass Classification) Multi-class classification์„ ํ•˜๋ ค๋ฉด output unit์„ class ๊ฐœ์ˆ˜๋งŒํผ ๋‘๋ฉด ๋œ๋‹ค. ๊ฐ€๋ น, ๋ฐ์ดํ„ฐ๋ฅผ 4๊ฐœ ํด๋ž˜์Šค๋กœ ๋‚˜๋ˆ„๊ณ ์ž ํ•œ๋‹ค๋ฉด, [ 0 1 2 x ] [ 0 ( ) 1 ( ) 2 ( ) ] [ 0 ( ) 1 ( ) 2 ( ) ] โ‹ฏ [ ฮ˜ ( ) h ( ) h ( ) h ( ) ] ๋งˆ์ง€๋ง‰ layer๋Š”, theta matrix์— ๊ณฑํ•ด์ ธ์„œ ์ƒˆ๋กœ์šด vector๊ฐ€ ๋  ๊ฒƒ์ธ๋ฐ ์ด vector๋Š” ( ) logistic function์„ ์ ์šฉํ•˜์—ฌ hypothesis ๊ฐ’์„ ๊ฐ–๋Š” vector์ด๋‹ค. ํ•œ input์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ hypothesis์˜ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ฮ˜ ( ) [ 0 0 ] ์ด๋•Œ classification ๊ฒฐ๊ณผ๋Š” 3๋ฒˆ์งธ ํด๋ž˜์Šค, ํ˜น์€ ฮ˜ ( )์— ํ•ด๋‹นํ•œ๋‹ค. ๊ฐ input์— ๋Œ€ํ•œ hypothesis์˜ ์ตœ์ข… ๊ฐ’์€ y์˜ ์›์†Œ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. Cost Function ์•ž์„œ ๊ณต๋ถ€ํ–ˆ๋˜ Logistic regression์„ ๋‹ค์‹œ ๋– ์˜ฌ๋ ค ๋ณด์ž. Logistic regression์€ binary classification์„ ์œ„ํ•ด logistic function์„ hypothesis๋กœ ์ทจํ–ˆ๋‹ค. NN ์—ญ์‹œ "activation" ๊ณผ์ •์—์„œ binary output์„ ๋‚ด๊ธฐ ์œ„ํ•ด logistic function (or sigmoid function)์„ ์ทจํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด Logistic regression์˜ cost function๊ณผ NN์˜ cost function์€ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅผ๊นŒ? Logistic regression ( ) โˆ’ m i 1 [ ( ) log h ( ( ) ) ( โˆ’ ( ) ) log ( โˆ’ ฮธ ( ( ) ) ) ] 2 NN์˜ cost function์„ ์ •์˜ํ•˜๊ธฐ์— ์•ž์„œ, ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋“ค์„ ์ •์˜ํ•ด ๋ณด์ž. : total number of layers in the network l : number of units (not including the bias unit) in layer K : number of output units/classes NN์€ ์—ฌ๋Ÿฌ ๊ฐœ output node๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ์„ ์—ผ๋‘์— ๋‘ ์ž. ฮ˜ ( )๋ฅผ ๋ฒˆ์งธ output์˜ hypothesis๋ผ๊ณ  ํ•˜์ž. ฮ˜ ( ) R , ( ฮ˜ ( ) ) = th output ์ด์ œ NN์€ logistic function์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ, ์ข€ ๋” ์ผ๋ฐ˜ํ™”๋œ version์ด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ( ) โˆ’ m [ i 1 โˆ‘ = K k ( ) log ( ฮธ ( ( ) ) ) + ( โˆ’ k ( ) ) log ( โˆ’ ฮ˜ ( ( ) ) ์ด ์‹์—๋Š” multiple output nodes๋ฅผ ๊ณ ๋ คํ•˜๊ธฐ ์œ„ํ•ด ๋ช‡ ๊ฐœ์˜ nested summation์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. ๋ฐฉ์ •์‹์˜ ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์ธ ๋Œ€๊ด„ํ˜ธ([]) ์‚ฌ์ด์—๋Š”, ouput node ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋˜๋Š” nested summation์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. Regularization ํ•ญ (๋Œ€๊ด„ํ˜ธ ๋‹ค์Œ ๋ถ€๋ถ„)์—๋Š” ๋ณต์ˆ˜์˜ ํ–‰๋ ฌ์„ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. ํ˜„์žฌ ํ–‰๋ ฌ์˜ ์—ด ๊ฐœ์ˆ˜๋Š” ํ˜„์žฌ layer์˜ node ๊ฐœ์ˆ˜ (bias unit ํฌํ•จ)์™€ ๊ฐ™๋‹ค. ํ–‰๋ ฌ์˜ ํ–‰ ๊ฐœ์ˆ˜๋Š” ๋‹ค์Œ layer์˜ node ๊ฐœ์ˆ˜ (bias unit ์ œ์™ธ)์™€ ๊ฐ™๋‹ค. Logistic regression๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋“  ํ•ญ์— ์ œ๊ณฑ์„ ์ทจํ•œ๋‹ค. Note the double sum simply adds up the logistic regression costs calculated for each cell in the output layer; and the triple sum simply adds up the squares of all the individual ฮ˜ s in the entire network. the i in the triple sum does not refer to training example i 3) Back-Propagation Algorithm I Gradient Computation Back-Propagation Backpropagation Algorithm Back-propagation์€ cost funciton์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•œ NN ํŠน์œ ์˜ ๋ฐฉ๋ฒ•์ด๋‹ค. Linear regression๊ณผ logistic regression์—์„œ gradient descent๊ฐ€ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. Gradient Computation ๋Š˜ ๊ทธ๋ ‡๋“ฏ์ด ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์€ cost function์„ ์ตœ์†Œํ™”ํ•˜๋Š” ์ตœ์ ์˜ parameter๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค. arg min J ( ) ์ด๋ฅผ ์œ„ํ•ด cost function ( ) ๋ฅผ ์ •์˜ํ•ด์•ผ ํ•˜๊ณ , ๊ทธ ํŽธ๋ฏธ๋ถ„ โˆ‚ i j ( ) ( ) ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ํ•œ๋‹ค. ( ) ๋Š” ์ง€๋‚œ๋ฒˆ์— ์ •์˜ํ–ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํŽธ๋ฏธ๋ถ„ ํ•จ์ˆ˜๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌํ• ๊นŒ? Back-Propagation Input layer์—์„œ ์ถœ๋ฐœํ•ด์„œ output์„ ๊ตฌํ•˜๋Š” forward propagation ๊ณผ๋Š” ๋ฐ˜๋Œ€๋กœ, BP๋Š” output layer์—์„œ ์‹œ์ž‘ํ•œ๋‹ค. ์ฆ‰, ๋งˆ์ง€๋ง‰ ๊ฒฐ๊ณผ์˜ 'error'๋ฅผ ๋จผ์ € ๊ตฌํ•˜๊ณ , ํ•ด๋‹น error ๊ฐ’์„ ์ด์šฉํ•ด ๊ฐ๊ฐ์˜ node์—์„œ์˜ error๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. j ( ) "error" of node in layer ์ด๋•Œ j ( ) ์€ layer์˜ activation node ์ž„์„ ๊ธฐ์–ตํ•˜์ž. ์œ„ ์˜ˆ์™€ ๊ฐ™์ด layer ๊ฐœ์ˆ˜๊ฐ€ 4๊ฐœ์ธ ๊ฒฝ์šฐ, ๋งˆ์ง€๋ง‰ layer ( ๋ฒˆ์งธ layer)์˜ ( ) ์€ ๋‹จ์ˆœํžˆ ๊ณ„์‚ฐ๋œ L ๊ฐ’๊ณผ label ๊ฐ’์˜ ์ฐจ๊ฐ€ ๋œ๋‹ค. ( ) a ( ) y where is the total number of layers and ( ) is the vector of ouptuts of the activation units for the last layer. ์ด์ „ layer์˜ ( ) ๊ฐ’์€ (์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๋Š”) ๋‹ค์Œ ๊ณต์‹์— ์˜ํ•ด ๊ตฌํ•œ๋‹ค. ( ) ( ( ( ) ) ฮด ( + ) ) โˆ— โ€ฒ ( ( ) ) ์ด๋•Œ โˆ— ์—ฐ์‚ฐ์€ ๋ฒกํ„ฐ์˜ element-by-element ๊ณฑ์…ˆ์„ ๋œปํ•˜๊ณ , โ€ฒ ( ) z ์— ๋Œ€ํ•ด ( ) ๋ฅผ ๋ฏธ๋ถ„ํ•œ ํ•จ์ˆ˜์ด๋‹ค. ํ•œํŽธ, โ€ฒ ( ( ) ) g ( ( ) ) a ( ) โˆ— ( โˆ’ ( ) ) ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค 1. ์ฆ‰, ( ) ( ( ( ) ) ฮด ( + ) ) โˆ— โ€ฒ ( ( ) ) ( ( ( ) ) ฮด ( + ) ) โˆ— ( ) โˆ— ( โˆ’ ( ) ) Backpropagation Algorithm ์œ„ ์ˆ˜์‹์—์„œ โ‰ ์˜ ๊ฒฝ์šฐ i j ( ) := m ( i j ( ) ฮป i j ( ) ) ์ด๋‹ค. ๊ด„ํ˜ธ๊ฐ€ ๋น ์ง„ ๊ฒƒ์€ ์˜คํƒ€์ด๋‹ค. ์—ฌ๊ธฐ์„œ matrix๋Š” "accumulator"๋กœ์„œ ๋™์ž‘ํ•œ๋‹ค. ์ฆ‰, ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•œ ๊ฐ’๋“ค์„ ๋”ํ•˜๊ณ , ๊ถ๊ทน์ ์œผ๋กœ ํŽธ๋ฏธ๋ถ„์„ ๊ตฌํ•˜๋Š” ๋ฐ ์ด์šฉ๋œ๋‹ค. i j ( ) โˆ‚ ( ) ฮ˜, ( ) (...๋ผ๊ณ  ํ•˜๋Š”๋ฐ ์†”์งํžˆ ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ ๋˜๋Š” ๊ฐ•์˜์˜€๋‹ค. ๋”ฐ๋กœ ๋ณด์ถฉ์ž๋ฃŒ๋ฅผ ์ฐพ์•„๋ด์•ผ๊ฒ ๋‹ค.) g ( ) z โˆ’ ( 1 e z ) โˆ‚ z ( + โˆ’ ) โˆ’ ( 1 e z ) e z ( 1 ) ( 1 e z ) ( 1 e z ) ( โˆ’ ) ( 1 e z ) ( โˆ’ 1 e z ) g ( ) ( โˆ’ ( ) ) โ†ฉ 4) Back-Propagation Algorithm II Intuition Forward Propagation What is BP doing? External Resources Intuition Forward Propagation What is BP doing? External Resources BP with numerical example 5) Implementation Notes Unrolling Parameters Gradient Checking Numerical Estimation of Gradients Parameter Vector Random Initialization Zero Initialization Random Initialization: Symmetric Breaking Unrolling Parameters Gradient Checking Train์ด ์ œ๋Œ€๋กœ ๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ํ•˜๋‚˜์ด๋‹ค. Numerical Estimation of Gradients ๋ฏธ๋ถ„์˜ ์ •์˜ ์ž์ฒด๊ฐ€ โ†’ ์ผ ๋•Œ ํ•จ์ˆซ๊ฐ’์˜ ๋ณ€ํ™”๋Ÿ‰์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด๋‹ˆ,์— ์•„์ฃผ ์ž‘์€ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค๋ฉด gradient, ์ฆ‰, ( ) ฮธ ์—์„œ์˜ ์ ‘์„ ์€ ๋…น์ƒ‰ ์„ ์˜ ๊ธฐ์šธ๊ธฐ์™€ ๋น„์Šทํ•ด์•ผ ํ•œ๋‹ค. Parameter Vector โˆ‚ ฮธ ( ) ๊ฐ€ numerial estimation ๊ณผ ๋น„์Šทํ•œ์ง€ ํ™•์ธํ•œ๋‹ค. ์ผ๋‹จ train algorithm์ด ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ๋‚˜๋ฉด, ์‹ค์ œ training ์ „์— gradient checking ๋ถ€๋ถ„์€ disable ํ•œ๋‹ค. ๊ทธ๋Ÿฌ์ง€ ์•Š์œผ๋ฉด ๊ต‰์žฅํžˆ ๋Š๋ ค์ง„๋‹ค. Random Initialization Gradient descent ๋˜๋Š” advanced optimization ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์—๋Š” ์–ด๋–ค ๊ฐ’์œผ๋กœ๋“  ์ดˆ๊ธฐํ™”ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. Zero Initialization ๋งŒ์•ฝ ๋ชจ๋“  weight์— 0 ๊ฐ’์„ ์ดˆ๊นƒ๊ฐ’์œผ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ. ํ•œ ๋ฒˆ weight๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ™์€ ์ž…๋ ฅ๋‹จ์—์„œ ๋‚˜์˜จ weight ๋“ค์—๋Š” ๊ฐ™์€ ๊ฐ’์ด ๋“ค์–ด๊ฐ„๋‹ค. (์ฆ‰, ๊ฐ™์€ ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ node์—๋Š” ๊ณ„์† ๊ฐ™์€ ๊ฐ’์˜ weight๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.) Random Initialization: Symmetric Breaking Initialize each i ( ) to a random value in [ ฯต ฯต ] 6) Put it Together Training a Neural Network Training a Neural Network Randomly initialize weights Implement forward propagation to get ฮธ ( ( ) ) for any ( ) Implement code to compute the cost function ( ) Implement backprop to compute partial derivatives โˆ‚ j ( ) ( ) Use gradient checking to compare โˆ‚ J ( ) computed using backprop. vs. using numerical estimate of gradient of ( ) . Then disable gradient checking code. Use gd or advanced optimization method with bp to try to minimize ( ) as a function of parameters . 06. Advice for Applying Machine Learning Evaluating a Learning Algorithm Debugging a Learning Algorithm Evaluating a Learning Algorithm Debugging a Learning Algorithm ์˜ˆ๋ฅผ ๋“ค์–ด, housing price๋ฅผ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด regularized linear regression ์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•˜์ž. ๊ทธ๋•Œ์˜ cost function์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด hypothesis๋ฅผ ์ƒˆ ๋ฐ์ดํ„ฐ์— ์ ์šฉํ•˜์ž ์˜ˆ์ธก ์—๋Ÿฌ๊ฐ€ ๊ต‰์žฅํžˆ ์ปธ๋‹ค. ์ด๋•Œ ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์จ์„œ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ. ์‹œ๋„ํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Training example ์„ ๋Š˜๋ฆฐ๋‹ค. Feature ๊ฐœ์ˆ˜๋ฅผ ์ค„์ธ๋‹ค. Polynomial feature๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.๋ฅผ ๋Š˜๋ฆฐ๋‹ค / ์ค„์ธ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ๋ฒ•๋“ค์„ ๋งˆ๊ตฌ์žก์ด๋กœ ์‹œ๋„ํ•˜๊ธฐ๋ณด๋‹ค๋Š” ํ˜„์žฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์–ด๋Š ๋ถ€๋ถ„์—์„œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”์ง€ ์ง„๋‹จํ•ด์„œ ์ฒด๊ณ„์ ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ์„ ํ•˜๋Š” ํŽธ์ด ํ›จ์”ฌ ํšจ์œจ์ ์ผ ๊ฒƒ์ด๋‹ค. 1) Machine Learning Diagnostic Evaluating a Hypothesis Model Selection and Training/ Validation/ Test Sets Model Selection Machine Learning Diagnostic A test that you can run to gain insights what is/isn't working with a learning algorithm and gain guidance as to how best to improve its performance. Diagnostic ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ์—๋Š” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ ์ถฉ๋ถ„ํžˆ ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š” ์ผ์ด๋‹ค. Evaluating a Hypothesis ์•„๋ž˜์™€ ๊ฐ™์ด 4th-order polynomial ์„ ์ด์šฉํ•ด์„œ housing price๋ฅผ ์˜ˆ์ธกํ•œ๋‹ค๊ณ  ํ•ด๋ณด์ž. ์ด๋•Œ ์ฃผ์–ด์ง„ training example์— ๋Œ€ํ•ด์„œ๋Š” ๊ฑฐ์˜ ์™„๋ฒฝํ•œ modeling์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๊นŒ์ง€ ์ผ๋ฐ˜ํ™”๋Š” ํ•˜์ง€ ๋ชปํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ training example๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ผ๋ถ€๋Š” training์—, ์ผ๋ถ€๋Š” test์— ์‚ฌ์šฉํ•ด์„œ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•œ๋‹ค. Training/ testing procedure for linear regression Learn parameter from training data (min. training error ( ) ) Compute test set error Model Selection and Training/ Validation/ Test Sets Over-fitting example Training set์— ๋Œ€ํ•ด์„œ ์ •ํ•œ parameters 0 ฮธ, . ฮธ๋ฅผ ์ด์šฉํ•ด์„œ ๊ณ„์‚ฐํ•œ ์—๋Ÿฌ (training error ( ) )๋Š” ์‹ค์ œ ์—๋Ÿฌ์œจ๋ณด๋‹ค ์ž‘์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋งŽ๋‹ค. Model Selection ์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐ๋œ ์ผ๋ จ์˜ test ( ) d 5 ์ผ ๋•Œ์˜ test error๊ฐ€ ๊ฐ€์žฅ ์ž‘์•„์„œ 0. . ฮธ x๋ฅผ ์„ ํƒํ–ˆ๋‹ค๊ณ  ํ•˜์ž. ์ด๋•Œ test ( ( ) ) ๊ฐ€ model์˜ ์ผ๋ฐ˜ํ™” ์„ฑ๋Šฅ์„ ์ž˜ ๋ฐ˜์˜ํ•œ ๊ฒƒ์ผ๊นŒ? ์‚ฌ์‹ค test ( ( ) ) ๋Š” generalization error์˜ optimistic estimate ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค. ์™œ๋ƒํ•˜๋ฉด extra parameter๋Š” test set์— ๋Œ€ํ•ด์„œ fit ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹จ์ˆœํžˆ training/test๋กœ ๋‚˜๋ˆŒ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฅผ fit ํ•˜๊ธฐ ์œ„ํ•œ set ์—ญ์‹œ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. Training ๊ณผ test set์˜ ๋น„์œจ์„ ์•ฝ๊ฐ„ ์ค„์—ฌ์„œ cross validation set์„ ๋งŒ๋“ ๋‹ค. ๊ฐ set์—์„œ์˜ error๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค. Training error train ( ) 1 m i 1 [ ฮธ ( ( ) ) y ( ) ] Cross-validation error cv ( ) 1 m cv i 1 cv [ ฮธ ( cv ( ) ) y cv ( ) ] Test error test ( ) 1 m test i 1 test [ ฮธ ( test ( ) ) y test ( ) ] ์กฐ๊ธˆ ์ „์˜๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฌธ์ œ๋Š” ์ด์ œ cross validation set์„ ์ด์šฉํ•ด์„œ ํ‘ผ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ฐ€๋ น 0. . ฮธ x๋ฅผ ์„ ํƒํ–ˆ๋‹ค๋ฉด, generalization error๋Š” test ( ( ) ) ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•œ๋‹ค. 2) Bias vs. Variance Diagnosing Bias vs. Variance Regularization and Bias/Variance Choosing the Regularization Parameter Diagnosing Bias vs. Variance Training error์™€ cross validation error๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•˜์˜€๋‹ค. Training error: train ( ) 1 m i 1 [ ฮธ ( ( ) ) y ( ) ] Cross-validation error: cv ( ) 1 m cv i 1 cv [ ฮธ ( cv ( ) ) y cv ( ) ] Polynomial order์— ๋Œ€ํ•˜์—ฌ cv ( ) J train ( ) ๋ฅผ ๊ทธ๋ ค๋ณด๋ฉด training error๋Š” ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ์ค„์–ด๋“ค์ง€๋งŒ cross validation error๋Š” =์—์„œ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค. ์–ด๋–ค learning algorithm์˜ ์„ฑ๋Šฅ์ด ๊ธฐ๋Œ€ํ•œ ๋งŒํผ ๋‚˜์˜ค์ง€ ์•Š์„ ๋•Œ, ์ฆ‰ cv ( ) ๋˜๋Š” test ( ) ๊ฐ€ ํด ๋•Œ, ๋ฌธ์ œ๊ฐ€ bias ์ธ์ง€ variance ์ธ์ง€ ์–ด๋–ป๊ฒŒ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์„๊นŒ? High bias (underfit)๋Š” train ( ) ์ด ํฌ๋‹ค. ์ฆ‰ cv ( ) J train ( ) ์ธ ํ˜•ํƒœ๋ฅผ ๋ค๋‹ค. ๋ฐ˜๋ฉด high variance (overfit)๋Š” train ( ) ์€ ์ž‘์€๋ฐ cv ( ) ์€ ํฐ ๊ฒƒ, ์ฆ‰ cv ( ) J train ( ) ์ธ ๊ฒƒ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. Regularization and Bias/Variance Linear regression with regularization์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์ž. Model: ฮธ ( ) ฮธ + 1 + 2 2 ฮธ x + 4 4 ( ) 1 โˆ‘ = m [ ฮธ ( ( ) ) y ( ) ] + 2 โˆ‘ = n j ฮป ๋ฅผ ํฌํ•จํ•œ ๋‘ ๋ฒˆ์งธ ํ•ญ์ด model์˜ bias์™€ variance ์‚ฌ์ด์˜ ๊ท ํ˜•์„ ์กฐ์ ˆํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๊ฒฝ์šฐ์ฒ˜๋Ÿผ ๊ฐ€ ๋„ˆ๋ฌด ํฌ๋ฉด ๋‘ ๋ฒˆ์งธ ํ•ญ์ด dominate ํ•˜๊ฒŒ ๋˜์–ด underfit ๋˜๊ณ , ๋งˆ์ง€๋ง‰ ๊ฒฝ์šฐ์ฒ˜๋Ÿผ ๊ฐ€ ๋„ˆ๋ฌด ์ž‘์œผ๋ฉด regularization ํ•ญ์ด ๊ฑฐ์˜ ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ๋ชปํ•ด overfit ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ€์šด๋ฐ ๊ฒฝ์šฐ์™€ ๊ฐ™์ด ์ ์ ˆํ•œ ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋Š” ์–ด๋–ป๊ฒŒ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์„๊นŒ? Choosing the Regularization Parameter Cross-validation set์„ ์ด์šฉํ•ด์„œ๋ฅผ ์„ ํƒํ•œ๋‹ค. Model: ฮธ ( ) ฮธ + 1 + 2 2 ฮธ x + 4 4 ( ) 1 โˆ‘ = m [ ฮธ ( ( ) ) y ( ) ] + 2 โˆ‘ = n j ๊ฐ€๋ น ( ) ๋ฅผ ์„ ํƒํ–ˆ๋‹ค๊ณ  ํ•˜๋ฉด test error๋Š” test ( ( ) ) ๊ฐ€ ๋œ๋‹ค. 3) Learning Curves High Bias High Variance Training example ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ error ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋ณด๋Š” ๊ฒƒ์œผ๋กœ overfit/underfit์„ ์ง„๋‹จํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ learning curve๋ผ๊ณ  ํ•œ๋‹ค. High Bias High bias์˜ ํŠน์ง•์€, training error์™€ cv error๊ฐ€ ๋ชจ๋‘ ํฌ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. Learning algorithm์— high bias ๋ฌธ์ œ๊ฐ€ ์žˆ์œผ๋ฉด, ๋‹จ์ˆœํžˆ training data๋ฅผ ๋” ์ง‘์–ด๋„ฃ๋Š” ๊ฒƒ์€ ํฐ ๋„์›€์ด ๋˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. High Variance High variance ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ์—๋Š” training error์™€ cv error์˜ ์ฐจ์ด๊ฐ€ ํฌ๋‹ค. Learning algorithm์— high variance ๋ฌธ์ œ๊ฐ€ ์žˆ์œผ๋ฉด, training data๋ฅผ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์ด ๋„์›€์ด ๋  ๊ฐ€๋Šฅ์„ฑ์ด ํฌ๋‹ค. 4) Deciding What to do Next (Revisited) Neural Networks and Overfitting Housing price๋ฅผ ์˜ˆ์ธกํ•˜๋Š” regularized linear regression์ด ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด generalize ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋ฉด, ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ•  ๊ฒƒ์ธ๊ฐ€? Training example ์„ ๋Š˜๋ฆฐ๋‹ค. -> fix high variance Feature ๊ฐœ์ˆ˜๋ฅผ ์ค„์ธ๋‹ค. -> fix high variance Feature๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. -> fix high bias Polynomial feature๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. -> fix high bias๋ฅผ ๋Š˜๋ฆฐ๋‹ค. -> fix high variance๋ฅผ ์ค„์ธ๋‹ค. -> fix high bias Neural Networks and Overfitting 07. Machine Learning System Design ์ด ์žฅ์—์„œ๋Š” ์ŠคํŒธ ๋ฉ”์ผ ๋ถ„๋ฅ˜์˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์‹ค์ œ ์‹œ์Šคํ…œ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ์— ์žˆ์–ด ๊ณ ๋ คํ•ด์•ผ ํ•  ์‚ฌํ•ญ์„ ์‚ดํŽด๋ณธ๋‹ค. ๋˜ํ•œ ๋ฐ์ดํ„ฐ์˜ Class ํฌ๊ธฐ๊ฐ€ ๊ท ๋“ฑํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์— ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์™€ ์ด๋•Œ ์‹œ์Šคํ…œ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์•Œ์•„๋ณธ๋‹ค. 1) Building a Spam Classifier Prioritizing What to Work on Error Analysis The Importance of Numerical Evaluation Prioritizing What to Work on Supervised learning : Features of email : Spam (1) or non-spam (0) label ์˜ˆ๋ฅผ ๋“ค๋ฉด, spam/non-spam ์„ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด 100๊ฐœ๋ฅผ ์ถ”๋ ค์„œ feature๋Š” ๊ทธ๋Ÿฌํ•œ ๋‹จ์–ด๊ฐ€ email์— ํฌํ•จ๋˜์—ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” vector๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ๊ทธ๋Ÿฌํ•œ ๋‹จ์–ด๊ฐ€ deal, buy, Andrew (์ˆ˜์‹ ์ž ์ด๋ฆ„), now,... ๋“ฑ์ด๋ผ๋ฉด, x = [ 1 1 0 1 ...] ์‹ค์ œ๋กœ๋Š” ์ˆ˜๋™์œผ๋กœ 100๊ฐœ ๋‹จ์–ด๋ฅผ ๋ฝ‘๊ธฐ๋ณด๋‹ค๋Š” training set์—์„œ ๊ฐ€์žฅ ์ž์ฃผ ๋‚˜ํƒ€๋‚˜๋Š” ๋‹จ์–ด n ๊ฐœ (10,000~50,000)๋ฅผ ์„ ํƒํ•œ๋‹ค. ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ํšจ์œจ์ ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ์„๊นŒ. ๋ฐ์ดํ„ฐ๋ฅผ ๋งŽ์ด ๋ชจ์€๋‹ค (์˜ˆ: "Honey pot" project) Email routing information (from email header)์— ๊ธฐ๋ฐ˜ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ์„ ํ•œ๋‹ค. Email ๋ณธ๋ฌธ์— ๊ธฐ๋ฐ˜ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ์„ ํ•œ๋‹ค. ์˜๋„์ ์œผ๋กœ ์ฒ ์ž๋ฅผ ํ‹€๋ฆฌ๊ฒŒ ์“ด ๋‹จ์–ด๋ฅผ ๊ฒ€์ถœํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ์„ ํ•œ๋‹ค. (์˜ˆ: m0rgage, med1cine, w4tches...) Error Analysis ์ถ”์ฒœํ•˜๋Š” ๋ฐฉ๋ฒ• ๋น ๋ฅด๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์‹œ์ž‘ํ•˜์ž. ๊ตฌํ˜„ํ•œ ํ›„ cross-validation data๋กœ ํ…Œ์ŠคํŠธํ•œ๋‹ค. Learning curve๋ฅผ ๊ทธ๋ ค์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜, feature๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜, ํ˜น์€ ๋‹ค๋ฅธ ์–ด๋–ค ๋ฐฉ๋ฒ•์ด ๋„์›€์ด ๋ ์ง€ ๊ฒฐ์ •ํ•œ๋‹ค. Error analysis: ์—๋Ÿฌ๋ฅผ ์ผ์œผํ‚ค๋Š” ๋ฐ์ดํ„ฐ(in cross-validation set)๋ฅผ ์ˆ˜๋™์œผ๋กœ ๋ถ„์„ํ•œ๋‹ค. ์–ด๋–ค ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ผ์ •ํ•œ ๊ฒฝํ–ฅ์˜ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š”์ง€ ๊ด€์ฐฐํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 500๊ฐœ cross-validation data ์ค‘ 100๊ฐœ email์ด ์ž˜๋ชป ๋ถ„๋ฅ˜๋˜์—ˆ๋‹ค๊ณ  ํ•˜์ž. ์ด 100๊ฐ€์ง€ ์—๋Ÿฌ๋ฅผ ํ™•์ธํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธฐ์ค€์— ์˜ํ•ด ๋ถ„๋ฅ˜ํ•œ๋‹ค. (i) What type of email it is (ii) What cues (features) you think would helped the algorithm classify them correctly (i)์˜ ๊ฒฝ์šฐ, ์•ฝํŒ” ์ด: 12๊ฐœ ์งํ‰: 4๊ฐœ ํ”ผ์‹ฑ: 53๊ฐœ ๊ธฐํƒ€: 31๊ฐœ ์˜€๋‹ค๋ฉด, 'ํ”ผ์‹ฑ'์œผ๋กœ ๋ถ„๋ฅ˜๋œ ๋ฉ”์ผ์— ๋Œ€ํ•œ ์„ฑ๋Šฅ์„ ๋†’์ด๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ํšจ๊ณผ์ ์ผ ๊ฒƒ์ด๋‹ค. (ii)์˜ ๊ฒฝ์šฐ, ์˜๋„์ ์ธ ์ฒ ์ž๋ฒ• ์˜ค๋ฅ˜: 5๊ฑด unusual email routing: 16๊ฑด unusual punctuation: 32๊ฑด ์ด์—ˆ๋‹ค๋ฉด unusual punctuation ์„ ๊ฒ€์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ๋Š” ๊ฒƒ์ด ํšจ๊ณผ์ ์ผ ๊ฒƒ์ด๋‹ค. The Importance of Numerical Evaluation discount/discounts/discounted/discounting ๋“ฑ์€ ๊ฐ™์€ ๋‹จ์–ด๋กœ ์ทจ๊ธ‰ํ•ด์•ผ ํ• ๊นŒ? "stemming" software (e.g. "Porter stemmer")๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ทธ๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ œ๋Š” ์ด๋Ÿฌํ•œ ์†Œํ”„ํŠธ์›จ์–ด๋Š” ์™„๋ฒฝํ•˜์ง€ ์•Š๊ณ  universe์™€ university๋ฅผ ๊ฐ™์€ ๋‹จ์–ด๋กœ ์ทจ๊ธ‰ํ•˜๋Š” ๋“ฑ์˜ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. Error analysis๋งŒ์œผ๋กœ๋Š” stemming์„ ํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ ํ–ฅ์ƒ์— ๋„์›€์ด ๋ ์ง€ ์•Œ๊ธฐ ์–ด๋ ต๋‹ค. ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ์ผ๋‹จ ์‹œ๋„ํ•ด ๋ณด๊ณ  ๋˜๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋•Œ, numerical evaluation (์˜ˆ: cross-validation error)์ด ํ•„์š”ํ•˜๋‹ค. ์ฆ‰, stemming์„ ์ด์šฉํ•  ๋•Œ์™€ ์ด์šฉํ•˜์ง€ ์•Š์„ ๋•Œ์˜ ์—๋Ÿฌ์œจ์„ ๋น„๊ตํ•˜์—ฌ ๊ฒฐ์ •์„ ๋‚ด๋ฆฌ๋ฉด ๋œ๋‹ค. 2) Handling Skewed Data Precision/ Recall Trading Off Precision and Recall 1 Score (F Score) Arithmetic and harmonic means (optional) Derivation of the F-measure (optional) References Logistic Regression ๊ฐ•์˜์—์„œ ์–ธ๊ธ‰ํ–ˆ๋˜ cancer classification์˜ ์˜ˆ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. Logistic regression model ฮธ ( ) ๋ฅผ ์ด์šฉํ•˜์—ฌ ์•”์ด๋ฉด = , ์•”์ด ์•„๋‹ˆ๋ฉด =์œผ๋กœ ๊ฒฐ์ •์„ ๋‚ด๋ฆฌ๋„๋ก ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค์—ˆ๋‹ค. ์„ฑ๋Šฅ์„ ํ…Œ์ŠคํŠธํ•ด๋ดค๋”๋‹ˆ test set์—์„œ์˜ error๊ฐ€ 1%์˜€๋‹ค. ์ •ํ™•๋„๊ฐ€ 99% ์ด๋‹ˆ, ๊ต‰์žฅํžˆ ์ž˜ ์ž‘๋™ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•จ์ •์ด ์žˆ๋‹ค. ์‚ฌ์‹ค ํ™˜์ž์˜ 0.5%๋งŒ์ด ์‹ค์ œ๋กœ ์•”์ด๋ผ๋ฉด? ํ•ญ์ƒ =์œผ๋กœ ๊ฒฐ์ •ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๋งŒ์œผ๋กœ๋„ 0.5% ์—๋Ÿฌ์œจ์„ ๋‹ฌ์„ฑํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ๊ฒฐ๊ตญ 1% ์—๋Ÿฌ์œจ์ด ๋”ฑํžˆ ์ข‹์€ ์„ฑ๋Šฅ์ด ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๋ง์ด๋‹ค. ์ด๋ ‡๊ฒŒ class ๋ณ„ ๋ฐ์ดํ„ฐ ์ˆ˜ ์ฐจ์ด๊ฐ€ ๋งŽ์ด ๋‚˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ skewed data๋ผ๊ณ  ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ skewed data์˜ ๊ฒฝ์šฐ, ์—๋Ÿฌ์œจ๋งŒ์œผ๋กœ๋Š” ์„ฑ๋Šฅ์„ ์ •ํ™•ํ•˜๊ฒŒ ํ‰๊ฐ€ํ•˜๊ธฐ ์–ด๋ ต๋‹ค. Precision/ Recall ์—๋Ÿฌ์œจ ๋Œ€์‹  ์‹œ์Šคํ…œ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” metric์—๋Š” precision๊ณผ recall์ด ์žˆ๋‹ค. ๋“œ๋ฌธ class๋ฅผ ์ผ๋ฐ˜์ ์œผ๋กœ =๋กœ ๋‘”๋‹ค. Precision =๋กœ ๊ฒฐ์ •๋œ ํ™˜์ž๋“ค ์ค‘ (positive), ์–ผ๋งˆ๋งŒํผ์ด ์ •๋ง๋กœ ์•”์ธ๊ฐ€? Recall ์‹ค์ œ๋กœ ์•”์ธ ํ™˜์ž๋“ค ์ค‘, ์–ผ๋งˆ๋งŒํผ์„ ์ฐพ์•„๋‚ด์—ˆ๋Š”๊ฐ€? Trading Off Precision and Recall Suppose we want to predict = (cancer) only if very confident Higher precision, lower recall Suppose we want to avoid missing too many cases of cancer Higher recall, lower precision ์ตœ์ ์˜ threshold๋ฅผ ์–ด๋–ป๊ฒŒ ๊ฒฐ์ •ํ• ๊นŒ? 1 Score (F Score) ์ด์ œ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ์— 2๊ฐœ ์ˆซ์ž๊ฐ€ ๋“ฑ์žฅํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ตœ์ ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณ ๋ฅด๋ ค๋ฉด ํ•˜๋‚˜์˜ ์ˆซ์ž๋กœ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ํŽธํ•˜๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ์ด ๋‘ ์ˆซ์ž์˜ ํ‰๊ท ์„ ๋‚ผ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ด๋‹ค. Average: + 2 ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ๋ฒ•์€ ์“ฐ์ด์ง€ ์•Š๋Š”๋‹ค. ์ด์ „์— ๋ณธ ์˜ˆ์™€ ๊ฐ™์ด, =๋กœ ํ•ญ์ƒ ๊ฒฐ์ •ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ์ด ์ ์ˆ˜๋Š” ๋†’์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (Algorithm 3) ๋Œ€์‹  F-score๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ด์šฉ๋œ๋‹ค. 1 score: P P R ๋งŒ์•ฝ = ๋˜๋Š” = ์ด๋ฉด F-score๋Š” 0์ด ๋˜๊ณ , ์™„๋ฒฝํ•œ ์‹œ์Šคํ…œ์˜ ๊ฒฝ์šฐ = , = ์ด ๋˜์–ด F-score๊ฐ€ 1์ด ๋œ๋‹ค. F-score๋ฅผ ์ด์šฉํ•˜๋ฉด Algorithm 1์ด ์ตœ์ ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์„ ํƒ๋œ๋‹ค. ์ดํ•˜ ๋‚ด์šฉ์€ Ng ๊ต์ˆ˜๋‹˜ ๊ฐ•์˜์—์„œ ๋‹ค๋ฃจ์–ด์ง€์ง€ ์•Š์€ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. Arithmetic and harmonic means (optional) F-measure๋Š” precision (P) ๊ณผ recall (R)์˜ harmonic mean์ด๋‹ค. = P P R Arithmetic mean A: ์ผ๋ฐ˜์ ์œผ๋กœ ๋งํ•˜๋Š” ํ‰๊ท  = n i 1 x Harmonic mean H: = โˆ‘ = n x ๋น„์œจ์— ๊ด€ํ•ด์„œ๋Š” harmonic mean์ด arithmetic mean ๋ณด๋‹ค ์ง๊ด€์ ์ด๋‹ค. ๊ฐ€๋ น, ํ•œ ์‹œ์Šคํ…œ์˜ precision recall์ด ๊ฐ๊ฐ 1.0 ๊ณผ 0.2๋ผ๊ณ  ํ•˜์ž. ์ง๊ด€์ ์œผ๋กœ, ์ด ์‹œ์Šคํ…œ์˜ ์„ฑ๋Šฅ์€ ๋‚ฎ๊ฒŒ ํ‰๊ฐ€๋˜์–ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. ์ด ๋‘ measure์˜ arithmetic mean์€ 0.6์ด๊ณ , harmonic mean์€ 0.3์ด๋‹ค. = ร— 1.0 0.2 1.0 0.2 0.333 Derivation of the F-measure (optional) ์•ž์„œ F-measure๋Š” F1-measure๋ผ๊ณ ๋„ ํ•œ๋‹ค๊ณ  ํ–ˆ๋‹ค. ์—ฌ๊ธฐ์„œ "1"์€ ๋ฌด์Šจ ์˜๋ฏธ์ผ๊นŒ? F-measure์˜ full definition์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค (Chinchor, 1992) ฮฒ ( 2 1 ) R 2 + , โ‰ค < ์—ฌ๊ธฐ์„œ eta๋Š” P์™€ R ์‚ฌ์ด์˜ ๋ฐธ๋Ÿฐ์Šค๋ฅผ ์กฐ์ ˆํ•œ๋‹ค. = : 1 ์€ P์™€ R์˜ harmonic mean๊ณผ ๊ฐ™๋‹ค. > : more recall-oriented < : more precision-oriented (e.g., 0 P ) References Sasaki, Y. (2007). "The trueth of the F-measure" [Chinchor, 1992] Nancy Chinchor, MUC-4 Evaluation Metrics, in Proc. of the Fourth Message Understanding Conference, pp. 22โ€“29, 1992. http://www.aclweb.org/anthology-new/M/M 92/M92-1002.pdf 3) Using Large Data Sets Data for Machine Learning Large Data Rationale 1 Large Data Rationale 2 Data for Machine Learning Designing a high accuracy learning system ์˜ˆ: Classify between confusable words (? and ?, 2001) {to, two, too}, {then, than}, ... For breakfast I ate ______ eggs. ์—ฌ๋Ÿฌ algorithm์„ ์‹œ๋„ํ•ด ๋ณด์•˜๋‹ค. ๊ฑฐ์˜ ๋ชจ๋“  ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด training data๊ฐ€ ๋Š˜์–ด๋‚ ์ˆ˜๋ก ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋˜์–ด "์•ฝํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜"๋„ ๋ฐ์ดํ„ฐ๋งŒ ๋งŽ์œผ๋ฉด "๊ฐ•ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜"๋งŒํผ์˜ ์„ฑ๋Šฅ์„ ๋‚ด๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ–ˆ๋‹ค. "It's not who has the best algorithm that wins. It's who has the most data" Large Data Rationale 1 Assume feature โˆˆ n 1 has sufficient information to predict accurately ์˜ˆ: For breakfast I ate ______ eggs. ๋ฐ˜๋ก€: Housing price๋ฅผ ํ‰์ˆ˜๋งŒ์œผ๋กœ ์˜ˆ์ธกํ•˜๊ธฐ (์•„๋ฌด๋ฆฌ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„๋„ ์ž…์ง€, ๋ฐฉ ๊ฐœ์ˆ˜ ๋“ฑ์˜ ์กฐ๊ฑด ์—†์ด ํ‰์ˆ˜๋งŒ์œผ๋กœ ์ง‘๊ฐ’์„ ์ •ํ™•ํ•˜๊ฒŒ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์€ ์–ด๋ ต๋‹ค.) ์ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์œ ์šฉํ•œ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” ์‚ฌ๋žŒ(์ „๋ฌธ๊ฐ€)์ด ์ฃผ์–ด ์ง„๋กœ๋ฅผ ๋ฏฟ์„ ๋งŒํ•˜๊ฒŒ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€๋ฅผ ๋”ฐ์ ธ๋ณด๋Š” ๊ฒƒ์ด๋‹ค. Large Data Rationale 2 Use a learning algorithm with many parameters. (์˜ˆ: Logistic/Linear regression with many features; Neural networks with many hidden units) 08. Support Vector Machines Alternative View of Logistic Regression Cost Function SVM Hypothesis Support vector machine (SVM) ์€ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ supervised learning algorithm์œผ๋กœ, logistic regression๋ณด๋‹ค ๊ฐ•๋ ฅํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. Alternative View of Logistic Regression Logistic regression์˜ hypothesis function์„ ๋‹ค์‹œ ๋– ์˜ฌ๋ ค๋ณด์ž. If = , then ฮธ ( ) 1 . i.e., T โ‰ซ If = , then ฮธ ( ) 0 . i.e., T โ‰ช ์ฆ‰, = ์ผ ๋•Œ T โ‰ซ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ , = ์ผ ๋•Œ T โ‰ช์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ฅผ ์ฐพ๊ณ ์ž ํ–ˆ๋‹ค. ํ•œ ๊ฐœ training example์— ํ•ด๋‹นํ•˜๋Š” cost๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. Logistic regression์€ log๋ฅผ ์ด์šฉํ•ด์„œ cost function์„ ์ •์˜ํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ SVM์€ ์ด์™€ ๋น„์Šทํ•˜์ง€๋งŒ piecewise-linear ํ•œ ํ•จ์ˆ˜๋ฅผ ๋Œ€์‹  ์ด์šฉํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ hinge loss function์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. Cost Function ๋”ฐ๋ผ์„œ logistic regression cost function์— ์ƒˆ๋กญ๊ฒŒ ์ •์˜ํ•œ cost ( ) cost ( ) ๋ฅผ ๋Œ€์ž…ํ•˜๋ฉด SVM cost function์ด ๋œ๋‹ค. ๊ทธ ์™ธ์—๋„ ์ผ๋ฐ˜์ ์ธ notation์„ ๋”ฐ๋ฅด์ž๋ฉด, exmple ๋‹น cost์˜ ํ‰๊ท  ๋Œ€์‹  ํ•ฉ์„ ์‚ฌ์šฉํ•œ๋‹ค. ( m ํƒˆ๋ฝ) ๋˜ํ•œ regularization trade-off๋ฅผ ์กฐ์ •ํ•˜๋Š” ๊ณ„์ˆ˜ ๋Œ€์‹  = ฮป ๋ฅผ ์‚ฌ์šฉํ•ด trade-off๋ฅผ ์กฐ์ •ํ•œ๋‹ค. SVM Hypothesis SVM์˜ hypothesis๋Š” ๊ฐ€ 1์ด๊ฑฐ๋‚˜ 0์ผ ํ™•๋ฅ ๋กœ ํ•ด์„ํ•˜์ง€ ์•Š๋Š”๋‹ค. Hypothesis function์˜ output ์ž์ฒด๋Š” ๊ทธ๋ƒฅ 1 ๋˜๋Š” 0์ด๋‹ค. 1) Decision Boundary: Large Margin SVM Decision Boundary Mathematics behind Large Margin Classification Vector Inner Product Optimal Decision Boundary Large Margin Classifier in Presence of Outliers SVM ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ optimization problem์ด๋‹ค. T ๊ฐ€ 0์ผ ๋•Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ classification์„ ํ•ด๋„ ๋  ํ…๋ฐ ๊ตณ์ด +1 ์ด์ƒ์ผ ๋•Œ์™€ -1 ์ดํ•˜์ผ ๋•Œ decision์„ ๋‚ด๋ฆฌ๋„๋ก ํ–ˆ๋‹ค. ๋ฐ”๋กœ ์ด ๋ถ€๋ถ„์ด SVM์ด margin์„ ์ตœ๋Œ€ํ™”ํ•˜๋„๋ก ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. Decision boundary์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ๋„ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ 2๊ฐœ class๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์ง€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ‘œ์‹œ๋œ ์„ ์ด ์„ ํƒ๋œ ์ด์œ ๋Š” ๋ฐ”๋กœ ๊ทธ ์„ ์ด ๋‘ class ์‚ฌ์ด์˜ margin์„ ์ตœ๋Œ€ํ™”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ์žฅ์—์„œ๋Š” ์–ด๋–ค ์›๋ฆฌ๋กœ ์ตœ๋Œ€ margin์„ ๊ฐ–๋Š” boundary๋ฅผ ์ฐพ๋Š”์ง€ ์•Œ์•„๋ณธ๋‹ค. SVM Decision Boundary ๊ด„ํ˜ธ ์•ˆ์˜ ์ˆ˜์‹์ด 0์ด ๋˜๋Š” ๋ถ€๋ถ„์ด ๋ฐ”๋กœ decision boundary๊ฐ€ ๋œ๋‹ค. ๊ทธ ๋ถ€๋ถ„์— 0์„ ๋„ฃ๊ณ  ๋‚˜๋ฉด ์šฐ๋ฆฌ์˜ ์ตœ์ ํ™” ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹จ์ˆœํ™”๋œ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ๋ฌธ์ œ๊ฐ€ ์š”๊ตฌํ•˜๋Š” ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1. ์–ด๋–ค ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๋Š”์˜ Euclidean norm ์„ ์ตœ์†Œํ™”ํ•  ๊ฒƒ. 2. ๊ทธ ์กฐ๊ฑด์ด๋ž€, ( ) 1 ์ผ ๋•Œ์—๋Š” T ( ) + ์ด ๋˜๊ฒŒ ํ•˜๊ณ  ( ) 0 ์ผ ๋•Œ์—๋Š” T ( ) โˆ’ ์ด ๋˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ. Mathematics behind Large Margin Classification ์šฐ์„  vector์˜ ๋‚ด์ ์„ ์ž ์‹œ ๋ณต์Šตํ•˜์ž. Vector Inner Product ์ฆ‰, ๋‘ ๋ฒกํ„ฐ์˜ ๋‚ด์  T๋Š”, ๋ฒกํ„ฐ ๋ฅผ ์— project ํ•œ ๋ฒกํ„ฐ์˜ ๊ธธ์ด์— ๋ฒกํ„ฐ์˜ ๊ธธ์ด๋ฅผ ๊ณฑํ•œ ๊ฒƒ์ด๋‹ค. ์ด๋•Œ๋Š” scalar์ด์ง€๋งŒ ์Œ์ˆ˜๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๋‹ค. Optimal Decision Boundary ์•ž์—์„œ ์•Œ์•„๋ณธ ๋ฐ”์™€ ๊ฐ™์ด SVM์˜ decision boundary๋ฅผ ์ฐพ๋Š” ๊ฒƒ์€ ๋‹ค์Œ์˜ optimization problem์„ ํ‘ธ๋Š” ๊ฒƒ์ด๋‹ค. ๋ฌธ์ œ๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์œ„ํ•ด bias 0 ์€ 0์œผ๋กœ ๋†“๊ณ , 2-dimension feature (feature๊ฐ€ 2๊ฐœ)์ธ ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ •ํ•˜๋„๋ก ํ•˜์ž. Decision boundary๋Š” ๋ฅผ normal vector๋กœ ๊ฐ–๋Š” hyperplane์ด๋ฏ€๋กœ decision boundary์™€๋Š” ์„œ๋กœ orthogonal ํ•˜๋‹ค. ๋งŒ์•ฝ ๋‚ด๊ฐ€ ์ฐพ์€ ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์ž‘๋‹ค๋ฉด ( ) | | โ‰ฅ 1 ์„ ๋งŒ์กฑ์‹œํ‚ค๋Š”์˜ ๊ธธ์ด๋Š” ์ปค์ ธ์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์•ž์„œ ๊ธฐ์ˆ ํ•œ optimization problem์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ•œ ๊ฐ€์žฅ ์ž‘์€ | |๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋ผ๊ณ  ํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” ์ตœ๋Œ€ margin์˜ decision boundary๊ฐ€ ๋œ๋‹ค. Large Margin Classifier in Presence of Outliers ๊ฐ€ ์•„์ฃผ ํฌ๋ฉด outlier์— ๋ฏผ๊ฐํ•ด์ง„๋‹ค. ๋…น์ƒ‰ ์„ ๋ณด๋‹ค๋Š” ํšŒ์ƒ‰ ์„ ์ด prediction์— ์žˆ์–ด ๋” ์œ ๋ฆฌํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋„ˆ๋ฌด ํฌ์ง€ ์•Š์€ ๊ฐ’์„ ์ ์ ˆํžˆ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. 2) Kernels Non-Linear Decision Boundary Kernel Choosing the Landmarks SVM with Kernels Non-Linear Decision Boundary ๋‹ค์Œ๊ณผ ๊ฐ™์€ data์—๋Š” non-linear decision boundary๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์—ฌ๊ธฐ์„œ SVM classifier๊ฐ€ ๋ชฉํ‘œํ•˜๋Š” ๊ฒƒ์€ 0 ฮธ x + 2 2. . ฮธ x 2 ฮธ x 2. . 0 ์ผ ๋•Œ = ์ด ๋˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋•Œ 1 x, 1 , 2 , . ์™€ ๊ฐ™์€ feature๋“ค์„ ์ข€ ๋” ์ผ๋ฐ˜์ ์ธ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด 1 f, .๋กœ ๊ณ ์ณ ์จ๋ณด์ž. 0 ฮธ f + 2 2 ฮธ f + . ์ด์™€ ๊ฐ™์€ x ๋“ค์€ ๊ธฐ์กด์˜ feature๋“ค ( 1 x, . )์„ ๋ชจ์ข…์˜ ๊ณผ์ •์„ ํ†ตํ•ด ๋ณ€ํ™˜ํ•œ ์ƒˆ๋กœ์šด feature๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด์ œ ์ด x ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ•˜๋ฉด non-linear decision boundary๋ฅผ ๋‚˜ํƒ€๋‚ด๊ฒŒ ๋˜๋Š”์ง€ ์•Œ์•„๋ณธ๋‹ค. Kernel ์šฐ๋ฆฌ์˜ feature space์— ์ž„์˜์˜ landmark ( ) l ( ) l ( ) ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. ์ƒˆ๋กœ์šด feature x ๋“ค์€ ์›๋ž˜ feature๊ฐ€ ๊ฐ landmark์— ์–ผ๋งˆ๋‚˜ ๊ฐ€๊นŒ์šด๊ฐ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์œผ๋กœ ์ •์˜ํ•œ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” Gaussian ํ•จ์ˆ˜๋ฅผ ์˜ˆ๋กœ ๋“ค์—ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ •๊ทœ๋ถ„ํฌ ํ•จ์ˆ˜์™€ ๊ฐ™์ด bell-curve ํ˜•ํƒœ๋กœ landmark์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ํฐ ๊ฐ’์„, ๋ฉ€์ˆ˜๋ก ์ž‘์€ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ example ๊ฐ€ ์ฒซ ๋ฒˆ์งธ landmark์— ๊ฐ€๊น๋‹ค๋ฉด 1 1 ์ด ๋  ๊ฒƒ์ด๊ณ , ๊ทธ landmark์—์„œ ๋ฉ€๋‹ค๋ฉด 1 0 ์ด ๋  ๊ฒƒ์ด๋‹ค. SVM: Predict "1" when 0 ฮธ f + 2 2 ฮธ f โ‰ฅ ๊ฐ€๋ น, 0 โˆ’ 0.5 ฮธ = , 2 1 ฮธ =์ด๋ผ๊ณ  ํ•˜์ž.๋Š” ( ) ์— ๊ฐ€๊น๊ณ  ( ) l ( ) ์— ๋ฉ€๊ธฐ ๋•Œ๋ฌธ์— 1 1 f โ‰ˆ, 3 0 ์ด๋‹ค. 0 ฮธ โ‹… + 2 0 ฮธ โ‹… = 0.5 0 ์ด๋ฏ€๋กœ ํ•ด๋‹นํ•˜๋Š” ์ ์€ = ์˜์—ญ์— ํฌํ•จ๋œ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์—ฌ๋Ÿฌ ์œ„์น˜์˜ ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์–ด๋–ค ๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š”์ง€ ๊ณ„์‚ฐํ•˜๋ฉด decision boundary๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์˜ˆ์‹œ์—์„œ์˜ decision boundary๋Š” ๋…ธ๋ž€์ƒ‰ ์„ , ์ฆ‰ non-linear decision boundary๊ฐ€ ๋œ๋‹ค. Choosing the Landmarks ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ landmark๋ฅผ ๊ฐ training example๊ณผ ๊ฐ™์€ ์œ„์น˜์— ๋†“๋Š” ๊ฒƒ์ด๋‹ค. SVM with Kernels kernel์„ ๋„์ž…ํ•œ ์ƒˆ cost function์€ ๋‹จ์ˆœํžˆ ( ) ์ž๋ฆฌ์— ( ) ๋ฅผ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค. ๊ผญ SVM์ด ์•„๋‹ˆ๋ผ๋„ kernel ๊ฐœ๋…์„ ๋„์ž…ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ SVM ์™ธ์˜ classifier์—์„œ๋Š” ์—ฐ์‚ฐ์†๋„๊ฐ€ ๋งค์šฐ ๋Š๋ ค์ง„๋‹ค. ๊ทธ๋ž˜์„œ kernel์€ ๊ฑฐ์˜ SVM์—๋งŒ ์‚ฌ์šฉ๋œ๋‹ค. 3) Using an SVM SVM Paramters Using SVM Packages Multi-Class Classification Logistic Regression vs. SVMs SVM Paramters Using SVM Packages SVM์€ ์ด๋ฏธ ์ž˜ ์•Œ๋ ค์ง„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๊ณ  ์ž˜ ๊ตฌํ˜„๋œ library๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค๋Š” ๊ธฐ์กด์— ๊ตฌ์ถ•๋œ package๋ฅผ ์ด์šฉํ•˜๋Š” ํŽธ์ด ๋‚ซ๋‹ค. e.g. liblinear, libsvm, ... ๋ช…์‹œํ•ด์•ผ ํ•  ๊ฒƒ์€ ๊ฐ’๊ณผ kernel์˜ ์ข…๋ฅ˜์ด๋‹ค. NOTE| ๋ชจ๋“  similarity function sim ( , ) ์„ kernel๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. "Mercer Theorem"์ด๋ผ๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผ SVM package์˜ optimization ๊ณผ์ •์ด ๋ฐœ์‚ฐํ•˜์ง€ ์•Š๊ณ  ์ œ๋Œ€๋กœ ๋™์ž‘ํ•œ๋‹ค. Multi-Class Classification Many SVM packages already have built-in multi-class classification functionality. Otherwise, use one-vs-all method. (Train SVMs, one to distinguish = from the rest, for = , , . , , get ( ) = , ( ) = , . , ( ) = . Pick class with largest ( ( ) ) x Logistic Regression vs. SVMs 09. Unsupervised Learning Unsupervised Learning Unsupervised Learning ์ง€๊ธˆ๊นŒ์ง€๋Š” supervised learning, ์ฆ‰, training set์˜ label์ด ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด์•˜๋‹ค. ๋ฐ˜๋ฉด, unsupervised learning ์€ ์ฃผ์–ด์ง„ label์ด ์—†์ด training ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ๋น„์Šทํ•œ ํŠน์„ฑ์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋ผ๋ฆฌ ๋ฌถ๋Š” clustering์ด ์žˆ๋‹ค. 1) K-Means Algorithm Intuition Algorithm K-means for non-separated clusters Intuition ์ฃผ์–ด์ง„ training ๋ฐ์ดํ„ฐ (๊ฒ€์€์ƒ‰ ์ )๋ฅผ K-means algorithm ์„ ์ด์šฉํ•ด์„œ clustering ํ•˜๋Š” ๊ณผ์ •์„ ์•Œ์•„๋ณด๋„๋ก ํ•œ๋‹ค. ์šฐ์„  ์ž„์˜์˜ ์œ„์น˜์— cluster centroids (๋นจ๊ฐ„ ์ , ํŒŒ๋ž€ ์ )๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด centroid์— ํ•ด๋‹นํ•˜๋Š” cluster์— ๋ฐฐ์ •ํ•œ๋‹ค. ๋ฐฐ์ •๋œ ์ ๋“ค์€ ์ƒ‰๊น”๋กœ ๊ตฌ๋ถ„ํ•˜์˜€๋‹ค. ๊ทธ๋‹ค์Œ ๊ฐ cluster์˜ ๋ฐ์ดํ„ฐ ํ‰๊ท ์„ ๊ตฌํ•ด centroids๋ฅผ ์ด๋™ํ•œ๋‹ค. ๊ฐ™์€ ๊ณผ์ •์„ ์ƒˆ centroids๋กœ ๋ฐ˜๋ณตํ•œ๋‹ค. ๋” ์ด์ƒ ๋ณ€ํ™”๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜์—ฌ clustering์„ ๋งˆ๋ฌด๋ฆฌํ•œ๋‹ค. Algorithm ์œ„ ๊ณผ์ •์„ pseudo-code๋กœ ์ž‘์„ฑํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Input: K (number of clusters) training set { ( ) x ( ) . . x ( ) } where ( ) R (drop 0 1 by convention) Procedure: Randomly initialize cluster centroids 1 ฮผ, . , m R Repeat{ for = to (cluster assignment step) c ( ) := index (from to) of cluster centroid closest to ( ) arg min | x ( ) ฮผ | 2 for = to (move centroid step) ฮผ := average (mean) of points assigned to cluster } ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋‹ค ๋ณด๋ฉด ์•„๋ฌด ๋ฐ์ดํ„ฐ๋„ ์—†๋Š” cluster๊ฐ€ ์ƒ๊ธฐ๊ธฐ๋„ ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ๊ทธ cluster๋ฅผ ์—†์• ๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„๋‹จํžˆ ํ•ด๊ฒฐํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ, โˆ’ ๊ฐœ cluster๊ฐ€ ๋œ๋‹ค. ๋งŒ์•ฝ ๋ฐ˜๋“œ์‹œ ๊ฐœ cluster๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” ์ƒˆ๋กœ random centroid๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. K-means for non-separated clusters ์–ด๋–ค ๋ฐ์ดํ„ฐ๋Š” cluster๋ฅผ ๋‚˜๋ˆ„๊ธฐ ์• ๋งคํ•˜๊ธฐ๋„ ํ•˜๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ์—๋„ ์–ด์จŒ๋“  clustering์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜๋ฅ˜ํšŒ์‚ฌ์—์„œ ์ถœ์‹œํ•  t-shirt ์‚ฌ์ด์ฆˆ๋ฅผ ์ •ํ•  ๋•Œ๋ฅผ ์˜ˆ๋กœ ๋“ค ์ˆ˜ ์žˆ๋‹ค. 2) Optimization Objective K-means Optimization Objective K-means Procedure K-means algorithm ๋„ ์ง€๊ธˆ๊นŒ์ง€ ๊ณต๋ถ€ํ•œ ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ objective๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ( ) : index of cluster to which example ( ) is currently assigned k : cluster centroid ( k R) c ( ) : cluster centroid of cluster to which example ( ) has been assigned K-means Optimization Objective ์ด๋•Œ ํ•จ์ˆ˜๋ฅผ cost function, ๋˜๋Š” distortion์ด๋ผ๊ณ  ํ•œ๋‹ค. K-means Procedure Cluster assignment step์—์„œ ์ตœ์ ์˜ ( ) . . c ( ) ์„ ์ฐพ๋Š” ๊ฒƒ์€ ๊ฐ example ( ) ๊ฐ€ ์ตœ์ ์˜ cluster์— ์žฌ๋ฐฐ์ •๋˜๋Š” ๊ณผ์ •์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•œํŽธ, move centroid step์—์„œ ( ) . . ฮผ ( ) ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์€ ๊ฐ cluster๋ฅผ ๋Œ€ํ‘œํ•˜๋Š” ์ตœ์ ๊ฐ’ (์—ฌ๊ธฐ์„œ๋Š” ํ‰๊ท ๊ฐ’)์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. 3) Random Initialization Local Optima ์ด๋ฒˆ์—๋Š” ํšจ๊ณผ์ ์ธ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. ๋‹น์—ฐํ•œ ์ด์•ผ๊ธฐ์ด์ง€๋งŒ, cluster ๊ฐœ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜ ๋ณด๋‹ค ์ž‘์•„์•ผ ํ•œ๋‹ค( < ). ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€, ์ฃผ์–ด์ง„ training example ์ค‘ K ๊ฐœ๋ฅผ centroid๋กœ ์ž„์˜๋กœ ์„ ํƒํ•˜๊ณ  1. . ฮผ๋ฅผ ๊ทธ ๊ฐœ example๊ณผ ๊ฐ™๊ฒŒ ๋‘๋Š” ๊ฒƒ์ด๋‹ค. ์šด์ด ์ข‹๋‹ค๋ฉด ์ ๋‹นํ•œ centroid๋ฅผ ๊ณ ๋ฅด๊ฒŒ ๋˜๊ฒ ์ง€๋งŒ, ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ์ œ๋Œ€๋กœ clustering์ด ๋˜์ง€ ๋ชปํ•˜๋Š” ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๊ฒŒ ๋œ๋‹ค. K-means ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ดˆ๊ธฐํ™”์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋‹ค. ๋‚˜์œ ๊ฒฝ์šฐ, local optima์— ๋น ์ง„๋‹ค. Local Optima ์ด ๋ฌธ์ œ๋Š” random initialization์„ ์—ฌ๋Ÿฌ ๋ฒˆ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ•ด๊ฒฐํ•œ๋‹ค. ์ด ์ค‘ cost๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ clustering์„ ์„ ํƒํ•œ๋‹ค. Cluster ๊ฐœ์ˆ˜๊ฐ€ ์ ์„์ˆ˜๋ก local optima์— ๋น ์งˆ ํ™•๋ฅ ์ด ํฌ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ, cluster ๊ฐœ์ˆ˜๊ฐ€ ์ ๋‹ค๋ฉด ์—ฌ๋Ÿฌ ๋ฒˆ ์ดˆ๊ธฐํ™”ํ•ด์„œ ๊ณ ๋ฅด๋Š” ๊ณผ์ •์„ ๊ฑฐ์น  ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. 4) Choosing the Number of Clusters Elbow Method K-means for a Later Purpose Cluster ๊ฐœ์ˆ˜๋Š” ์ฃผ๋กœ ์‚ฌ๋žŒ์ด ์ง์ ‘ ๋ฐ์ดํ„ฐ ๋ถ„ํฌ๋ฅผ ๋ณด๊ณ  manually ๊ฒฐ์ •ํ•œ๋‹ค. ์‚ฌ์‹ค ์–ด๋–ค K๊ฐ€ ์ ์ ˆํ•œ์ง€์— ๋Œ€ํ•œ ๋ช…ํ™•ํ•œ ๋‹ต์ด ์—†๊ธฐ๋„ ํ•˜๋‹ค. ๋‹ค์Œ ์˜ˆ์—์„œ cluster๋ฅผ 2๊ฐœ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒŒ ์ ๋‹นํ•œ๊ฐ€, ์•„๋‹ˆ๋ฉด 4๊ฐœ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒŒ ์ ๋‹นํ•œ๊ฐ€? Elbow Method ๊ฐ€์žฅ ์ ์ ˆํ•œ K๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ธด ํ•˜๋‹ค. K ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ cost function๋ฅผ ๊ทธ๋ ค๋ณด์•˜์„ ๋•Œ, ํŠน์ • K ์ดํ›„ cost๊ฐ€ ๊ฑฐ์˜ ๋ณ€ํ•˜์ง€ ์•Š๋Š” elbow point ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ K๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ํ•ฉ๋ฆฌ์ ์ผ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ๋ฒ•์ด ํ•ญ์ƒ ํ†ตํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™”๊ฐ€ ๋Œ€์ฒด๋กœ ๋ถ€๋“œ๋Ÿฝ๋‹ค๋ฉด, elbow๋ผ๊ณ  ๋ถ€๋ฅผ๋งŒํ•œ ์ง€์ ์ด ์—†๋‹ค. ์ด ๊ฒฝ์šฐ, elbow method๋กœ K๋ฅผ ์ •ํ•˜๋Š” ๋ฐ์—๋Š” ๋ฌด๋ฆฌ๊ฐ€ ์žˆ๋‹ค. K-means for a Later Purpose K-means๋ฅผ ์ดํ›„ ๋‹ค๋ฅธ application์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด, ํ•ด๋‹นํ•˜๋Š” ๋ชฉ์ ์„ ์ž˜ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ‰๊ฐ€ํ•˜๋Š” metric์„ ํ†ตํ•ด K๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด t-shirt ์‚ฌ์ด์ฆˆ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒฝ์šฐ, ์˜ˆ์ƒ๋˜๋Š” ์ด์ต์ด ๊ทน๋Œ€ํ™”๋˜๋Š” ์‚ฌ์ด์ฆˆ ์ข…๋ฅ˜ ์ˆ˜๋ฅผ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค. 10. Dimensionality Reduction Motivation Data Compression Visualization Motivation Data Compression Feature dimension ์ด ์“ธ๋ฐ์—†์ด ๋„ˆ๋ฌด ๋†’์•„ redundancy๊ฐ€ ํฐ ๊ฒฝ์šฐ, dimension์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. Correlation์ด ๋†’์€ feature๋ฅผ ์ฐพ๊ณ , ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค์„œ ๋‘ feature๋ฅผ ๋™์‹œ์— ๋Œ€ํ‘œํ•  ์ˆ˜ ์žˆ๋Š” ์ƒˆ๋กœ์šด line์„ ์ฐพ์•„๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค. Dimensionality reduction์œผ๋กœ ์ปดํ“จํ„ฐ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๋Š” ๋ฐ์ดํ„ฐ์–‘์„ ์ค„์ด๊ณ  learning algorithm ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค. NOTE: Dimensionality reduction ์ด๋ž€ feature์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ด์ง€ example ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์ฆ‰, ์€ ๊ฐ™์€ ํฌ๊ธฐ์ด๋ฉฐ, ๊ฐ example์˜ feature ๊ฐœ์ˆ˜ ์ด ์ค„์–ด๋“ ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 2-dim data๋ฅผ 1-dim data๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. Visualization ๋ฐ์ดํ„ฐ ์ฐจ์›์ด 3์ฐจ์›์„ ๋„˜์œผ๋ฉด visualization์ด ์–ด๋ ต๋‹ค. ๊ณ ์ฐจ์› ๋ฐ์ดํ„ฐ๋ฅผ 2-dim ๋˜๋Š” 3-dim์œผ๋กœ ์ค„์—ฌ ๋ˆˆ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฐ์—๋„ ์‚ฌ์šฉ๋œ๋‹ค. ์ฆ‰, ๊ธฐ์กด feature๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์š”์•ฝํ•ด ์ฃผ๋Š” ์ƒˆ๋กœ์šด feature 1 z (and perhaps 3 )๋ฅผ ์ฐพ๋Š”๋‹ค. 1) Principal Component Analysis Problem Formulation Relationship to Linear Regression PCA Algorithm Data Pre-processing Algorithm ๊ฐ€์žฅ ๋„๋ฆฌ ์ด์šฉ๋˜๋Š” dimensionality reduction algorithm ์€ Principal Component Analysis (PCA)์ด๋‹ค. Problem Formulation ๋‘ feature 1 x ๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ณด์ž. ์šฐ๋ฆฌ๋Š” ์ด ๋‘ feature๋ฅผ ๋ชจ๋‘ ํšจ๊ณผ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋‚˜์˜ ์„ ์„ ์ฐพ๊ณ  ์‹ถ๋‹ค. ์ด ๊ธฐ์กด์˜ feature๋“ค์„ ๊ทธ ์ƒˆ๋กœ์šด ์„ ์— map ํ•ด์„œ ์ƒˆ๋กœ์šด ๋‹จ ํ•˜๋‚˜์˜ feature๋ฅผ ์ฐพ์œผ๋ ค ํ•œ๋‹ค. Reduce from 2-dim to 1-dim: Data๋ฅผ project ํ–ˆ์„ ๋•Œ ๊ทธ projection error๋ฅผ ์ตœ์†Œ๋กœ ๋งŒ๋“œ๋Š” ๋ฒกํ„ฐ ( ( ) R)๋ฅผ ์ฐพ๋Š”๋‹ค. ๊ฐ™์€ ๊ณผ์ •์„ 3๊ฐœ feature๋กœ๋„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์„  ๋Œ€์‹  ํ‰๋ฉด์— map ํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ์ด์™€ ๊ฐ™์ด ์ž„์˜์˜ n ๊ฐœ feature๋ฅผ k-dimensional feature๋กœ ์ค„์ด๋Š” ๊ฒƒ์œผ๋กœ ์ผ๋ฐ˜ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. Reduce from n-dim to k-dim: Data๋ฅผ project ํ–ˆ์„ ๋•Œ ๊ทธ projection error๋ฅผ ์ตœ์†Œ๋กœ ๋งŒ๋“œ๋Š” ๊ฐœ ๋ฒกํ„ฐ ( ( ) u ( ) . . u ( ) )๋ฅผ ์ฐพ๋Š”๋‹ค. i.e. project the data onto the linear subspace spanned by the vectors Relationship to Linear Regression PCA์™€ lin. reg.๋Š” ๋น„์Šทํ•ด ๋ณด์ด์ง€๋งŒ ์ „ํ˜€ ๋‹ค๋ฅด๋‹ค. linear regression์—์„œ๋Š” ๊ฐ ๋ฐ์ดํ„ฐ์ ๊ณผ prediction line ๊ฐ„์˜ squared error (vertical distance)๋ฅผ ์ตœ์†Œํ™”ํ•œ๋‹ค. PCA์—์„œ๋Š” ๊ฐ ๋ฐ์ดํ„ฐ์ ๊ณผ projection line ๊ฐ„์˜ shortest distance (or orthogonal distance)๋ฅผ ์ตœ์†Œํ™”ํ•œ๋‹ค. ์กฐ๊ธˆ ๋” ์ผ๋ฐ˜ํ™”ํ•ด์„œ ๋งํ•˜์ž๋ฉด, linear regression์—์„œ๋Š” ๋ฅผ predict ํ•˜๊ธฐ ์œ„ํ•ด์˜ ๋ชจ๋“  example์„ ํƒํ•ด์„œ์˜ parameters๋ฅผ ์ ์šฉํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, PCA์—์„œ๋Š” feature 1 x, . , n ๋ฅผ ํƒํ•ด์„œ ๊ทธ๋“ค ์‚ฌ์ด์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ณตํ†ต์ ์ธ ๋ฐ์ดํ„ฐ ์…‹์„ ์ฐพ๋Š”๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์–ด๋–ค ๊ฒฐ๊ณผ๋ฅผ predict ํ•˜์ง€๋„, feature์— h t weight์„ ์ ์šฉํ•˜์ง€๋„ ์•Š๋Š”๋‹ค. ๋˜ํ•œ, lin regression์˜๋Š” ์˜ˆ์ธกํ•ด์•ผ ํ•˜๋Š” ํŠน๋ณ„ํ•œ ๋ณ€์ˆ˜์ด์ง€๋งŒ PCA์˜ 1 x ๋“ฑ ์‚ฌ์ด์—๋Š” ๊ทธ๋Ÿฌํ•œ ๊ตฌ๋ณ„์ด ์—†๋‹ค. PCA Algorithm Data Pre-processing PCA ํ•˜๊ธฐ ์ „์— data pre-processing์ด ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋‹ค. Given training set: ( ) x ( ) . . x ( ) Preprocess (Feature scaling/ Mean normalization): j 1 โˆ‘ = m j ( ) Replace each j ( ) with j ( ) ฮผ If different features on different scales (e.g. 1 = size of house, 2 = number of bedrooms), scale features to have comparable range of values. i.e. j ( ) x ( ) ฮผ s where j is scaling factor We can define specifically what it means to reduce from 2d to 1d data as follows: = m i 1 ( ( ) ) ( ( ) ) The values are all real numbers and are the projections of our features onto ( ) So, PCA has two tasks: Figure out ( ) u ( ) . . ( ) Find 1 z, . , m The mathematical proof for the following procedure is complicated and beyond the scope of this course. Algorithm Reduce data from n-dim to k-dim Compute "covariance matrix" Sigma = (1/m) * X' * X; % compute the covariacne matrix Covraince matrix๋ฅผ๋กœ ํ‘œํ˜„ํ•˜์˜€๋Š”๋ฐ, summation๊ณผ ํ˜ผ๋™ํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•˜์ž. ์—ฌ๊ธฐ์„œ ( ) n 1 vector์ด๊ณ , ( ( ) )๋Š” ร— vector์ด๋ฉฐ๋Š” ร— matrix์ด๋‹ค. ์ด ํ–‰๋ ฌ์˜ ๊ณฑ ์—ฐ์‚ฐ์€ ร— ํ–‰๋ ฌ์ด ๋œ๋‹ค. Compute "eigenvectors" of covariance matrix Covariance matirx๋Š” positive definite์ด๊ธฐ ๋•Œ๋ฌธ์— eig = svd [U, S, V] = svd(Sigma); % compute our projected directions SVD๋Š” singular value decomposition์„ ์˜๋ฏธํ•œ๋‹ค. SVD ๊ณผ์ •์„ ํ†ตํ•ด covariance matrix์˜ โˆˆ n n ํ–‰๋ ฌ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋‹ค.๋Š” ( ) u ( ) . . ( ) ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ ๋ฐ”๋กœ ์ด ๋ฒกํ„ฐ๋“ค์ด ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Take the first columns of the matrix and compute ์ด์ œ ํ–‰๋ ฌ์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐœ ์—ด์„ ๋ณ€์ˆ˜ 'Ureduce'์— assign ํ•˜์ž. ์ด๊ฒƒ์€ ร— matrix๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ์ด ํ–‰๋ ฌ๋กœ ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ( ) U reduce โ‹… ( ) r d c T : kxn ( ) : nx1 r d c T x ( ) : kx1 Ureduce = U(:,1:k) % take the first k directions; Z=X*Ureduce; % compute the projected data points 2) Reconstruction from Compressed Representation PCA๋กœ ์••์ถ•ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ์›๋ž˜๋Œ€๋กœ ๋Œ๋ฆด ์ˆ˜ ์žˆ์„๊นŒ? 1-dim์—์„œ 2-dim์œผ๋กœ ํ™•์žฅํ•˜๋ ค๋ฉด โˆˆ โ†’ โˆˆ 2 ์ด ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์ •์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค: approx ( ) U reduce z ( ) ์ด๋ ‡๊ฒŒ ํ•ด์„œ ์–ป์–ด์ง€๋Š” ๋ฐ์ดํ„ฐ๋Š” ์›๋ž˜ ๋ฐ์ดํ„ฐ์˜ ๊ทผ์‚ฟ๊ฐ’์ž„์— ์ฃผ์˜ํ•˜์ž. NOTE: ํ–‰๋ ฌ ๋Š” unitary matrix๋ผ๋Š” ํŠน๋ณ„ํ•œ ์„ฑ์งˆ์„ ๊ฐ–๋Š”๋‹ค. Unitary matrix๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ฑ์งˆ์„ ๊ฐ–๋Š”๋‹ค. โˆ’ = โˆ— where indicates "conjugate transpose". ์šฐ๋ฆฌ๋Š” ์‹ค์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ํ–‰๋ ฌ์„ ๋‹ค๋ฃจ๊ณ  ์žˆ์œผ๋ฏ€๋กœ, โˆ’ = T ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด ์„ฑ์งˆ์„ ์ด์šฉํ•˜์—ฌ ์—ญํ–‰๋ ฌ์„ ๊ตฌํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธด ํ•˜์ง€๋งŒ, ์‹œ๊ฐ„๊ณผ ์ž์›์˜ ๋‚ญ๋น„์ด๋‹ค... 3) Choosing the Number of Principal Components Criterion Algorithm (Not recommended) Algorithm (Using SVD) Number of principal component ์ฆ‰,๋Š” ์–ด๋–ป๊ฒŒ ์ •ํ•  ๊ฒƒ์ธ๊ฐ€?๋Š” ๋ชฉํ‘œ๋กœ ํ•˜๋Š” ์ฐจ์› ์ˆ˜ ์ž„์„ ๊ธฐ์–ตํ•˜์ž. Criterion ๋‹ค์Œ ๊ณต์‹์„ ๋”ฐ๋ผ์„œ๋ฅผ ์„ ํƒํ•œ๋‹ค. Averaged squared projection error: m i 1 | x ( ) x approx ( ) | Total variation in the data: m i 1 | x ( ) | ๋‹ค์Œ์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฒƒ์„ ํƒํ•œ๋‹ค. m i 1 | x ( ) x approx ( ) | 1 โˆ‘ = m | ( ) | โ‰ค 0.01 ( % ) ์ด์™€ ๊ฐ™์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ variance์˜ 99% ๊ฐ€ ๋ณด์กด๋œ๋‹ค๊ณ  ๋ณธ๋‹ค. ํ•„์š”์— ๋”ฐ๋ผ ์œ„ ์ˆ˜์‹์—์„œ 0.01 ๋Œ€์‹  0.05, 0.10 ๋“ฑ ๋‹ค๋ฅธ ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ ๊ฒฝ์šฐ ๋ณด์กด๋˜๋Š” variance๋Š” 95%, 90% ๋“ฑ์ด๋‹ค. Algorithm (Not recommended) k๋ฅผ ์ ์ฐจ ๋Š˜๋ ค๊ฐ€๋ฉฐ PCA๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์œ„ ratio๊ฐ€ 0.01๋ณด๋‹ค ์ž‘์€์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ์‹์€ ๋Œ€๋‹จํžˆ ๋น„ํšจ์œจ์ ์ด๋‹ค. ํ–‰๋ ฌ ์—ฐ์‚ฐ์„ ์ด์šฉํ•˜๋ฉด ์ด๋ฅผ ํ›จ์”ฌ ํšจ์œจ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. Algorithm (Using SVD) SVD (Singular Value Decomposition)์„ ์ด์šฉํ•˜๋ฉด k๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์šฉ์ดํ•˜๋‹ค. ๋จผ์ € covariance matrix Sigma๋ฅผ ๊ตฌํ•œ ๋‹ค์Œ, Sigma์— ๋Œ€ํ•˜์—ฌ SVD๋ฅผ ํ•œ๋‹ค. [U, S, V] = svd(Sigma) ์ด๋•Œ, matrix๋ฅผ ์ด์šฉํ•˜๋ฉด ์–ด๋Š์—์„œ retained variance๊ฐ€ 99% ์ด์ƒ์ด ๋˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์„ ๋” ํšจ๊ณผ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ๋‹ค์Œ ๋ถ€๋“ฑ์‹์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ€์žฅ ์ž‘์€ ๋ฅผ ์ฐพ๋Š”๋‹ค. i 1 S i i 1 S i 0.99 4) Advice for Applying PCA Supervised Learning Speedup Application of PCA Bad Use of PCA To Prevent Overfitting Unnecessary Dim Reduction Supervised Learning Speedup Feature ์ˆ˜๊ฐ€ ์•„์ฃผ ๋งŽ์€ supervised learning์˜ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž. ์˜ˆ๋ฅผ ๋“ค์–ด, feature dimension์ด 10,000 ์ธ ๊ฒฝ์šฐ, ์—ฐ์‚ฐ ์†๋„๊ฐ€ ์•„์ฃผ ๋Š๋ฆด ์ˆ˜๋ฐ–์— ์—†์„ ๊ฒƒ์ด๋‹ค. Dataset: ์ด๋•Œ PCA๋ฅผ ์ด์šฉํ•ด์„œ feature dimension์„ 1,000์œผ๋กœ ์ค„์ด๋ฉด ํ›จ์”ฌ ๋น ๋ฅด๊ฒŒ ์—ฐ์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. Extract inputs New training set Prediction Note | Mapping ( ) U reduced ( ) should be defined by running PCA only on the training set. This mapping can be applied as well to the examples cv ( ) and test ( ) in the cross validation and test sets. Application of PCA Compression Reduce memory/disk needed to store data Speed up learning algorithm -> Choose by % of variance retain Visualization -> = or = Bad Use of PCA To Prevent Overfitting ( ) ๋Œ€์‹  ( ) ์„ ์‚ฌ์šฉํ•˜๋ฉด feature dimension ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค( < ). ๊ทธ๋Ÿฌ๋ฉด fitํ•  paramter ์ˆ˜๊ฐ€ ์ค„์–ด๋“œ๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ overfitting์„ ํ”ผํ•˜๋Š” ๋ฐ์—๋„ ์ข‹์ง€ ์•Š์„๊นŒ? ๊ทธ๋Ÿญ์ €๋Ÿญ ๋™์ž‘ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ overfitting ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ํ•ด๊ฒฐํ•˜๊ธฐ์— ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ๋‹ค. Overfitting์„ ํ”ผํ•˜๋ ค๋ฉด ๋Œ€์‹  regularization์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. PCA๋Š” ์ •๋ณด์˜ ์–‘์„ ๋งŽ์ด ์ค„์ด๋Š”๋ฐ, ํŠนํžˆ์— ์ƒ๊ด€์—†์ด ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์žƒ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๋ฐ˜๋ฉด regularization์€ label ์„ ๋ฐ˜์˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์œผ๋กœ ๋” ๋‚˜์€ ๊ฒฐ๊ณผ๋ฅผ ๋‚ธ๋‹ค. Unnecessary Dim Reduction Machine learning system ์„ ์„ค๊ณ„ํ•  ๋•Œ์— PCA๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ตณ์ด ์ฒ˜์Œ๋ถ€ํ„ฐ PCA๋ฅผ ์จ์•ผ ํ• ๊นŒ? PCA๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์ „์—, ์šฐ์„  ์›๋ž˜ ๋ฐ์ดํ„ฐ ( ) ๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹œ๋„ํ•ด ๋ณด์ž. ์›ํ•˜๋Š” ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ทธ๋•Œ ๊ฐ€์„œ PCA์™€ ( ) ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•˜๋„๋ก ํ•˜์ž. 5) Factor Analysis Introduction Types of Factor Anlaysis Exploratory Confirmatory Rotation Orthogonal Rotation Oblique Rotation Formulation Fundamental Equations for Factor Analysis Commonly Encountered Matrices in Factor Analysis Some Important Issues Interpretation of Factors Limitations Theoretical Issues Practial Issues Sample size and missing data Normality Linearity Absense of outliers among cases Absense of Multicollinearity and Singularity Factorability of R Absense of outliers among variables References tip ์ด ๋ฌธ์„œ๋Š” Ng ๊ต์ˆ˜๋‹˜ ๊ฐ•์˜์— ํฌํ•จ๋˜์ง€ ์•Š์€ ๋‚ด์šฉ์„ ๋‹ค๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Introduction PCA ์™€ ๋น„์Šทํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ factor analysis (FA)๊ฐ€ ์žˆ๋‹ค. ์ด ๊ธฐ๋ฒ•์€ feature์˜ subset์ด ์„œ๋กœ independent ํ•œ coherent subset์„ ๊ตฌ์„ฑํ•˜๋„๋ก ํ•˜๋Š” ๋ฐ์— ์ด์šฉ๋œ๋‹ค. ์ด๋•Œ ๊ฐ subset์— ์†ํ•˜๋Š” feature๋“ค์€ ์„œ๋กœ correlated ๋˜์–ด ์žˆ์ง€๋งŒ ๋‹ค๋ฅธ subset์˜ feature๋“ค๊ณผ๋Š” independent ํ•˜๋„๋ก ๊ตฌ์„ฑํ•˜๊ณ , ๊ฐ subset์˜ feature๋Š” ์–ด๋–ค "factor"๋กœ ํ•ฉ์ณ์ง„๋‹ค. ๊ฐ factor๋Š” feature๋“ค์˜ linear combination์œผ๋กœ ํ‘œํ˜„๋˜๋ฉฐ ๊ทธ ์™ธ์—๋„ PCA์˜ compoenent์™€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ณตํ†ต์ ์ด ์žˆ์ง€๋งŒ, factor๋Š” ๊ฐ feature๋ฅผ ์ผ์œผํ‚จ ์ผ์ข…์˜ ์›์ธ์œผ๋กœ ๊ฐ„์ฃผ๋œ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋ฉฐ ๊ทธ์— ๋”ฐ๋ผ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์—๋„ ์ฐจ์ด์ ์ด ์žˆ๋‹ค. ๊ฐ€์žฅ ๊ทผ๋ณธ์ ์ธ ์ฐจ์ด์ ์€ ๋ถ„์„๋˜๋Š” covariance์— ์žˆ๋‹ค. PCA FA All the variances in the observed variables are analyzed Only shared variance is analyzed Types of Factor Anlaysis Exploratory Correlate๋œ ๋ณ€์ˆ˜๋“ค์„ ๋ชจ์•„์„œ ์š”์•ฝ -> theory development Confirmatory Latent process์— ๊ด€ํ•œ ๊ฐ€์„ค์„ ๊ฒ€์ฆ -> theory testing Rotation Orthogonal Rotation i.e. factors are uncorrelated A loading matrix is produced + factor-socre coefficient matrix Oblique Rotation Loding matrix -> structure matrix, patterm matrix + factor-score coefficient matrix + factor correlation matrix Formulation Dataset = 1 x, . , n . ์ด ๋ฐ์ดํ„ฐ ์…‹์˜ ํ•œ ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ๋ฅผ latent variable์„ ์ด์šฉํ•ด์„œ ์ˆ˜ํ•™์ ์œผ๋กœ ํ‘œํ˜„ํ•ด ๋ณด์ž. i ฮผ W i ฯต ์ด๋•Œ, ๋ฒกํ„ฐ i ๋Š” ์ง์ ‘ ๊ด€์ฐฐ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ "latent"๋ผ๊ณ  ํ•˜๋ฉฐ, ์€ zero-mean, covariance์˜ Gaussian distribution์„ ๋”ฐ๋ฅด๋Š” noise์ด๋‹ค.๋Š” ์–ด๋–ค ์ž„์˜์˜ offset vector์ด๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์€ "generative"๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š”๋ฐ, i h๋กœ๋ถ€ํ„ฐ ์–ด๋–ป๊ฒŒ generate ๋˜๋Š”์ง€ ๊ธฐ์ˆ ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. i ๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๋ฉด, ์œ„์˜ ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™•๋ฅ  ๊ด€๊ณ„๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค. ( i h) N ( h + , ) ํ™•๋ฅ  ๋ชจ๋ธ์„ ์™„์„ฑํ•˜๋ ค๋ฉด latent variable h์˜ prior distribution ๋„ ์•Œ์•„์•ผ ํ•œ๋‹ค. N ( , ) ๋ผ๊ณ  ๊ฐ€์ •ํ•ด ๋ณด์ž. ์ด๋Š” x์˜ marginal distribution ์„ Gaussian ์ด ๋˜๊ฒŒ ํ•œ๋‹ค. ( ) N ( , W + ) Latent variable h ๊ฐ€ superflous (๋ฌด์Šจ ๋œป์ด์ง€)๋ผ๋Š” ๊ฐ€์ •์„ ํ•  ํ•„์š” ์—†์ด, x๋Š” mean ๊ณผ covariance๋กœ ์™„์ „ํžˆ model ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋‘ ํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘ ํ•˜๋‚˜์— ์ข€ ๋” ํŠน์ •ํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ •ํ•˜์ž. Error covarinace์— ๋Œ€ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ—์€ ๊ฐ€์ •์„ ํ•ด๋ณด์ž. = 2 : PCA์˜ ํ™•๋ฅ  ๋ชจ๋ธ์ด ๋œ๋‹ค. = i g ( 1 ฯˆ, . , n ) : Factor Analysis๊ฐ€ ๋œ๋‹ค. Matrix W๋Š” "factor loading matrix"๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค. ๋‘ ๋ชจ๋ธ ๋ชจ๋‘ low-rank cov. matix๋กœ estimate ํ•œ๋‹ค. ์ด์ œ x ์™€ h์˜ ๊ด€๊ณ„์‹์„ ์ „์ฒด ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด matrix ๊ผด๋กœ ํ™•์žฅํ•ด ๋ณด์ž. = + H E ์ด๋•Œ, ์šฐ๋ฆฌ๋Š” matrix๋ฅผ decompose ํ•˜๋Š” ๊ฒƒ์ด ๋œ๋‹ค. Fundamental Equations for Factor Analysis Commonly Encountered Matrices in Factor Analysis : Number of variables : Number of subjects : Number of factors (or components) Label Name Rotation Size Description Correlation matrix Orthogonal, oblique ร— Matrix of correlations between variables Factor loading matrix / Pattern matrix Orthogonal / oblique ร— Matrix of regression-like weights used to estimate the unique contribution of each factor to the variance in a variable. If orthogonal, also correlations between variables and factors Factor-score coefficient matrix Orthogonal, oblique ร— Matrix of regression-like weights used to generate factor scores from variables = โˆ’ A Some Important Issues Interpretation of Factors Loading ( )์˜ ์˜๋ฏธ๋Š” rotation์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค. Orthogonal rotation์˜ ๊ฒฝ์šฐ, loading matrix์˜ ๊ฐ’์€ variable๊ณผ factor ์‚ฌ์ด์˜ correlation์ด๋‹ค. ์˜๋ฏธ ์žˆ๋Š” correlation์„ ๊ฒฐ์ •ํ•ด์„œ (์ผ๋ฐ˜์ ์œผ๋กœ 0.32 ์ด์ƒ) ํ•œ factor์— ๋ชจ์ธ variable์„ ๊ณ ๋ฅด๊ณ  ๊ทธ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐœ๋…์„ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Oblique rotation์˜ ๊ฒฝ์šฐ์—๋„ ๊ฐ™์€ ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€๋งŒ์˜ ๊ฐ’์— ๋Œ€ํ•œ ํ•ด์„์€ ๊ทธ๋งŒํผ ๋ถ„๋ช…ํ•˜์ง€ ์•Š๋‹ค. ์ด๋•Œ์˜ ๊ฐ’์€ correlation ์ด ์•„๋‹ˆ๋ผ factor์™€ variable ์‚ฌ์ด์˜ ์œ ์ผํ•œ ๊ด€๊ณ„์— ๋Œ€ํ•œ ์ฒ™๋„์ด๋‹ค. Factor๋“ค์ด correlate๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— variable๊ณผ factor ์‚ฌ์ด์˜ correlation (arailable in the structure matrix) ์ด factor๋“ค ๊ฐ„์˜ overlap์œผ๋กœ ์ธํ•ด ๊ณผ์žฅ๋˜์–ด ์žˆ๋‹ค. Limitations Theoretical Issues Factor์˜ ๊ฐœ์ˆ˜, rotational scheme ์€ theoretical criteria๋ณด๋‹ค๋Š” practical criteria๋ฅผ ๋”ฐ๋ฅด๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Œ Complexity: # factors with which a variable correlates ... Practial Issues Sample size and missing data Corr coeff๋Š” small sample๋กœ๋ถ€ํ„ฐ ์ถ”์ •๋˜๋ฉด less reliable ์ถฉ๋ถ„ํžˆ ๋งŽ์€ ๋ฐ์ดํ„ฐ ํ•„์š”ํ•˜๋ฉฐ, ๊ทธ ์–‘์€ population correlation์˜ ํฌ๊ธฐ์™€ factor ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ์ •ํ•ด์ง„๋‹ค. ๋งŒ์•ฝ correlation์ด ํฌ๊ณ  ์ ์€ ์ˆ˜์˜ distinct factor๊ฐ€ ์žˆ๋‹ค๋ฉด sample size๊ฐ€ ๋น„๊ต์  ์ ์–ด๋„ ๋œ๋‹ค. 100-200 acceptable with well-determined factors (i.e. most factors defined by many indicators, i.e. marker variables with loadings >.8) and communalities (squared multiple correlations among variables) in the range of .5 At least 300 cases with low communalities, a small number of factors, and just three or four indicators for each factor. over 500 are required under the worst conditions of low communalities and a larger number of weakly determined factors. Missing data๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, missing value๊ฐ€ ์ถ”์ •๋˜๊ฑฐ๋‚˜ case๊ฐ€ ์‚ญ์ œ๋œ๋‹ค. [Ch 4๋Š” missing value ์ถ”์ • ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃฌ๋‹ค]. Missing value estimation procedure ๊ฐ€ data๋ฅผ overfitํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด correlation์ด ๋„ˆ๋ฌด ๋†’์•„์งˆ ์ˆ˜ ์žˆ์Œ์— ์œ ์˜ํ•˜์ž. ์ด ๊ณผ์ • ์ž์ฒด๊ฐ€ factor๋ฅผ "create"ํ•  ์ˆ˜๋„ ์žˆ๋Š” ์…ˆ. Normality Normality๊ฐ€ ์–ด๋Š ์ •๋„ ์ง€์ผœ์ง€์ง€ ์•Š๋”๋ผ๋„ solution์ด ์–ด๋Š ์ •๋„ degrade ๋˜์ง€๋งŒ ์—ฌ์ „ํžˆ worthwhile. Linearity Absense of outliers among cases Absense of Multicollinearity and Singularity Factorability of R Absense of outliers among variables References Tabachnick and Fidell, "Using Multivariate Statistics" Ch. 13 Scikit: Factor Analysis 6) Linear Discriminant Analysis Introduction PCA vs. LDA References tip ์ด ๋ฌธ์„œ๋Š” Ng ๊ต์ˆ˜๋‹˜ ๊ฐ•์˜์— ํฌํ•จ๋˜์ง€ ์•Š์€ ๋‚ด์šฉ์„ ๋‹ค๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Introduction Linear discriminant analysis (LDA)๋Š” ๋งŽ์€ machine learning์—์„œ pre-processing์œผ๋กœ ์ด์šฉ๋˜๋Š” dimensionality reduction ๊ธฐ๋ฒ•์ด๋‹ค. ์ด๋Š” class-separability๋ฅผ ๋†’๊ฒŒ ์œ ์ง€ํ•˜๋ฉด์„œ ๋‚ฎ์€ ์ฐจ์›์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ project ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•œ๋‹ค. ์ด ๊ธฐ๋ฒ•์€ Ronald A. Fisher์— ์˜ํ•ด ์ •๋ฆฝ๋˜์—ˆ๋‹ค (Fisher, 1936). ์ฒ˜์Œ์—๋Š” 2๊ฐœ class๋ฅผ ๋‚˜๋ˆ„๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•ด ์ ์šฉ๋˜์—ˆ์œผ๋‚˜ ๋‚˜์ค‘์— ์—ฌ๋Ÿฌ class์— ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ™•์žฅ๋˜์—ˆ๋‹ค (Rao, 1948) LDA๋Š” PCA์™€ ๋น„์Šทํ•˜์ง€๋งŒ, PCA๋Š” ๋ฐ์ดํ„ฐ์˜ ๋ถ„์‚ฐ์„ ์ตœ๋Œ€๋กœ ํ•˜๋Š” ์ถ•์„ ์ฐพ๋Š” ๋ฐ ๋ฐ˜ํ•ด LDA๋Š” class ์‚ฌ์ด์˜ separation์„ ์ตœ๋Œ€๋กœ ํ•˜๋Š” ์ถ•์„ ์ฐพ๋Š”๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. ๋‹ค์‹œ ๋งํ•ด, LDA๋„ PCA์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•œ feature space๋ฅผ ๋” ์ž‘์€ k-dimensional subspace์— project ํ•˜๋Š” linear transform ๋ฐ ์ด๋ฒˆ์—๋Š” class-discriminatory information์„ ๋ณด์กดํ•˜๋ฉด์„œ ์ค„์ธ๋‹ค. PCA vs. LDA ๊ฐ„๋‹จํžˆ ๋งํ•˜์ž๋ฉด PCA๋Š” class label์„ ๋ฌด์‹œํ•˜๊ณ  ๋ฐ์ดํ„ฐ ์…‹ ์ „์ฒด์— ๋Œ€ํ•ด ์ ์šฉํ•˜๋Š” "unsupervised" ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๊ณ  LDA๋Š” class label ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ class๋ฅผ ์ตœ๋Œ€ํ•œ ๋ถ„๋ฆฌํ•ด ๋‚ด๋Š” "supervised" ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋งํ•˜๋ฉด LDA๊ฐ€ PCA๋ณด๋‹ค ๋” ๋‚˜์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋“ค๋ฆฌ๊ฒ ์ง€๋งŒ, ํ•ญ์ƒ ๊ทธ๋Ÿฐ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, image ์ธ์‹์—์„œ LDA์™€ PCA๋ฅผ ๊ฐ๊ฐ ์‚ฌ์šฉํ•œ ๋ฐฉ์‹์˜ classification accuracy๋ฅผ ์ธก์ •ํ•˜์˜€์„ ๋•Œ, ๊ฐ class์˜ sample ๊ฐœ์ˆ˜๊ฐ€ ์ ์œผ๋ฉด PCA๋ฅผ ์‚ฌ์šฉํ•˜์˜€์„ ๋•Œ์˜ ์„ฑ๋Šฅ์ด ๋” ์ข‹์•˜๋‹ค. (Martinez et al., 2001) (Sebastian Raschka) References Fisher, 1936 Rao, 1948 Martinez et al., 2001 Sebastian Raschka 11. Anomaly Detection ๋น„ํ–‰๊ธฐ ์—”์ง„์„ ์ œ์ž‘ํ•˜๋Š” ๊ณต์žฅ์—์„œ ๋ถˆ๋Ÿ‰ํ’ˆ์„ ๊ฑธ๋Ÿฌ๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ๊ฐœ๋ฐœํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ์—”์ง„์€ ๋ฐœ์—ด์ด๋‚˜ ์ง„๋™ ์ •๋„๊ฐ€ ๋น„์ •์ƒ์ ์œผ๋กœ ํฌ๊ฑฐ๋‚˜ ์ž‘๋‹ค๋ฉด, ์ด๋“ค์„ ์ธก์ •ํ•จ์œผ๋กœ์จ ๋ถˆ๋Ÿ‰ํ’ˆ์„ ๊ฒ€์ถœํ•ด๋‚ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ฆ‰, ๋ฐœ์—ด๊ณผ ์ง„๋™ ์„ธ๊ธฐ๋ฅผ feature๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์ •์ƒ ์ž‘๋™ํ•˜๋Š” ๋‹ค์ˆ˜์˜ ์—”์ง„๋“ค(training data)๋กœ๋ถ€ํ„ฐ ์ธก์ •ํ•œ feature ๊ฐ’๋“ค(๋นจ๊ฐ„ ์ )๊ณผ test ํ•˜๊ณ  ์‹ถ์€ ์—”์ง„์—์„œ ์ธก์ •ํ•œ feature ๊ฐ’(ํŒŒ๋ž€ ์ )์„ ๋น„๊ตํ•˜์—ฌ ๋ถˆ๋Ÿ‰ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์žฅ์—์„œ๋Š” ์ด์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ anomaly๋ฅผ ๊ฒ€์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณธ๋‹ค. 1) Density Estimation Examples Gaussian Distribution Parameter Estimation Density Estimation Anomaly Detection Algorithm ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์ „์— ์ˆ˜์ง‘ํ•˜์˜€๋‹ค. ์•ž์„œ ์‚ดํŽด๋ณธ ์˜ˆ์™€ ๊ฐ™์ด ๋น„ํ–‰๊ธฐ ์—”์ง„ ์ •์ƒ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•ด ๋ฐœ์—ด๊ณผ ์ง„๋™ ์ •๋„๋ฅผ ์ธก์ •ํ•˜์˜€๋‹ค๋ฉด ๊ฐ ๋ฐ์ดํ„ฐ ํฌ์ธํŠธ์˜ features๋Š” 2-dim vector์˜ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. Dataset: ( ) x ( ) . . x ( ) ์ด๋•Œ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ test ๊ฐ€ anomalous ํ•œ ์ง€ ์–ด๋–ป๊ฒŒ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์„๊นŒ? ์ˆ˜์ง‘ํ•œ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ feature์˜ ํ™•๋ฅ ๋ถ„ํฌ ( ) ๋ฅผ ๋ชจ๋ธ๋ง ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ test ๊ฐ€ ํ•ด๋‹น ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅผ likelihood๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์ด likelihood๊ฐ€ ํŠน์ •ํ•œ threshold ๋ณด๋‹ค ํฌ๋ฉด ์ •์ƒ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋น„์ •์ƒ์œผ๋กœ ํŒ๋‹จํ•œ๋‹ค. Examples Fraud detection ( ) : ์‚ฌ์šฉ์ž์˜ ํ–‰๋™์–‘์‹์„ ๊ธฐ๋กํ•˜๋Š” features ๋‹ค์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ( ) ๋ฅผ ๋ชจ๋ธ๋ง ํ•œ๋‹ค. ๊ฐ ์‚ฌ์šฉ์ž๋“ค์ด ( ) ฯต ์ธ์ง€ ํŒ๋ณ„ํ•˜์—ฌ ์ด์ƒํ–‰๋™์„ ๋ณด์ด๋Š” ์‚ฌ์šฉ์ž๋ฅผ ๊ตฌ๋ณ„ํ•œ๋‹ค. Manufacturing Monitoring computers in a data center ( ) : ๊ธฐ๊ธฐ์— ๊ด€ํ•œ features e.g. 1 : memory use, 2 : # disk accesses/sec, 3 : CPU load, 4 : CPU load/network traffic,... Gaussian Distribution Gaussian (normal) distribution ๊ฐ€๋ น ๊ฐ€ ์‹ค์ˆ˜์ด๊ณ , ํ‰๊ท , ๋ถ„์‚ฐ 2 ์ธ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ํ•˜์ž. ์ด๋Ÿฌํ•œ ๋žœ๋ค ๋ณ€์ˆ˜๋ฅผ โˆผ ( , 2 ) ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. Parameter Estimation ์ฃผ์–ด์ง„ dataset { ( ) x ( ) . . x ( ) }์˜ ( ) R ๊ฐ€ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ์ด๋•Œ์˜ parameter ์™€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”์ •ํ•œ๋‹ค. Density Estimation Anomaly Detection Algorithm Anomalous example์„ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์œผ๋กœ ์—ฌ๊ฒจ์ง€๋Š” features i 1 i n ๋ฅผ ๊ณ ๋ฅธ๋‹ค. ์ด๋•Œ ์€ feature dimension, ํ˜น์€ ์‚ฌ์šฉํ•  feature์˜ ๊ฐ€์ง“์ˆ˜์ด๋‹ค. Parameters 1. . ฮผ, 1 , . , n๋ฅผ ๊ตฌํ•œ๋‹ค. j 1 โˆ‘ = m j ( ) j = m i 1 ( j ( ) ฮผ) ์ƒˆ example์— ๋Œ€ํ•˜์—ฌ ( ) ๋ฅผ ๊ตฌํ•œ๋‹ค. ( ) โˆ = n ( j ฮผ, j) โˆ = n 2 ฯƒ exp ( ( j ฮผ) 2 j) ( ) ฯต ์ด๋ฉด anomaly 2) Building an Anomaly Detection System The Importance of Real Number Evaluation Aircraft Engines Example Algorithm Evaluation Anomaly Detection vs. Supervised Learning The Importance of Real Number Evaluation Learning algorithm์„ ๊ฐœ๋ฐœํ•  ๋•Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์–‘์ ์œผ๋กœ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์œผ๋ฉด feature๋ฅผ ์„ ํƒํ•˜๋Š” ๋“ฑ์˜ ๊ฒฐ์ •์„ ๋‚ด๋ฆฌ๊ธฐ๊ฐ€<NAME> ์ˆ˜์›”ํ•˜๋‹ค. Anomalous/non-anomalous label ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. ์ฆ‰, ์ •์ƒ์ด๋ฉด = , ๋น„์ •์ƒ์ด๋ฉด =์ด๋ผ๊ณ  ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค. Aircraft Engines Example ๊ฐ€๋ น, ์ •์ƒ ์—”์ง„ ๋ฐ์ดํ„ฐ๋ฅผ 10,000๊ฐœ, ๋ถˆ๋Ÿ‰ ์—”์ง„ ๋ฐ์ดํ„ฐ๋ฅผ 20๊ฐœ ๊ฐ–๊ณ  ์žˆ๋‹ค๊ณ  ํ•˜์ž. Algorithm Evaluation Fit model ( ) on training set ( ) . . x ( ) On a cross-validation/test example , predict Possible evaluation metrics: Note this is skewed data! True positive, false positive, false negative, true negative Precision/recall F-score Cross-validation set ์„ ์ด์šฉํ•˜์—ฌ parameter ์„ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. Anomaly Detection vs. Supervised Learning Label์ด ์žˆ๋‹ค๋ฉด supervised learning ๋„ ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์„๊นŒ? 3) Choosing what Features to Use Non-Gaussian Features Error Analysis for Anomaly Detection Example: Monitoring Computers in a Data Center Non-Gaussian Features Feature ๊ฐ€ Gaussian distribution์„ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์ด ์ด์ƒ์ ์ด๊ฒ ์ง€๋งŒ, ์‚ฌ์‹ค ๊ทธ๋ ‡์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ๋‹ค. Non-Gaussian feature๋ฅผ ์ด์šฉํ•  ๋•Œ์—๋Š” feature๋ฅผ ์•ฝ๊ฐ„ ๋งˆ์‚ฌ์ง€(?) ํ•ด์„œ Gaussian distribution์„ ๋”ฐ๋ฅด๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์€ ๋ถ„ํฌ๋ฅผ ๋ณด์ด๋Š” feature๋ฅผ ์ด์šฉํ•  ๊ฒฝ์šฐ log ๋ฅผ ์ทจํ•˜๋ฉด Gaussian์— ๊ฐ€๊นŒ์šด ๋ถ„ํฌ๋กœ transform ํ•  ์ˆ˜ ์žˆ๋‹ค. Logarithm ์™ธ์—๋„ polynomial ๋“ฑ์„ ์ด์šฉํ•˜์—ฌ feature transformation์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. Error Analysis for Anomaly Detection Anomaly detection์„ ํ•  ๋•Œ ๊ฐ€์žฅ ์ด์ƒ์ ์ธ ๊ฒฝ์šฐ๋Š” ์ •์ƒ ์‚ฌ๋ก€ normal p ( normal ) ๋Š” ํฌ๊ณ  ๋น„์ •์ƒ ์‚ฌ๋ก€ anomalous p ( anomalous ) ๋Š” ์ž‘์€ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ ์ƒํ™ฉ์—์„œ๋Š” ์ •์ƒ๊ณผ ๋น„์ •์ƒ ์‚ฌ๋ก€์˜ ( ) ๊ฐ€ ๋ชจ๋‘ ๋น„์Šทํ•œ ๋ฒ”์œ„์— ์กด์žฌ, ์ฆ‰ ๋‘˜ ๋‹ค ํฐ ๊ฒฝ์šฐ๊ฐ€ ํ”ํžˆ ๋ฐœ์ƒํ•œ๋‹ค. ์ด๋•Œ์—๋Š” anomalous ํ•˜์ง€๋งŒ ( ) ๊ฐ€ ํฐ ๊ฒฝ์šฐ๋ฅผ ๋”ฐ๋กœ ๋ฝ‘์•„ ์ž์„ธํžˆ ๋ถ„์„ํ•˜์—ฌ ์ƒˆ feature๋ฅผ ๊ณ ์•ˆํ•ด ๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ•ด ๋ณธ๋‹ค. Example: Monitoring Computers in a Data Center Choose features that might take on unusually large or small values in the event of an anomaly . 1 = memory use of computer 2 = # disk accesses/sec 3 = CPU load 4 = network traffic 5 = CPU load/network traffic 12. Recommender Systems Problem Formulation Problem Formulation ๋ง๋˜ฅ์ด๋Š” Netflix๋‚˜ Watcha์—์„œ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค์™€ ๊ฐ™์ด ์˜ํ™” ๋ณ„์ ์„ ์˜ˆ์ƒํ•˜์—ฌ ์ถ”์ฒœํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. ๊ฐ ์‚ฌ์šฉ์ž๊ฐ€ ๋ช‡ ํŽธ์˜ ์˜ํ™”์— ๋Œ€ํ•ด 0์ ์—์„œ 5์  ์‚ฌ์ด์˜ ๋ณ„์ ์„ ๋งค๊ธฐ๋ฉด ๊ทธ์— ๊ธฐ๋ฐ˜ํ•ด ์ƒˆ๋กœ์šด ์˜ํ™”๋ฅผ ์ถ”์ฒœํ•ด ์ฃผ๋Š” ์‹œ์Šคํ…œ์ด๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ๋‹ค์Œ ํ‘œ์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ ์‚ฌ์šฉ์ž๊ฐ€ ํ‰๊ฐ€ํ•˜์ง€ ์•Š์€ ์˜ํ™”, ์ฆ‰ '?' ์ž๋ฆฌ์— ๋“ค์–ด๊ฐˆ ์ ์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ด๋‹ค. u = ์‚ฌ์šฉ์ž ์ˆ˜ = 4 m = ์˜ํ™” ์ˆ˜ = 5 ( , ) 1 if user has rated movie y ( , ) = rating given by user to movie ( ( , ) 1 ์ธ ๊ฒฝ์šฐ์—๋งŒ ์ •์˜๋จ) ์ง€๊ธˆ๊นŒ์ง€ ๋‹ค๋ฃฌ linear regression ๋“ฑ์˜ ๊ธฐ๋ฒ•์„ ์‘์šฉํ•ด์„œ ๋ง๋˜ฅ์ด๊ฐ€ ์‹œ๋„ํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค์„ ์ด์šฉํ•ด ๋ณด์ž. 1) Content Based Recommendation Problem Formulation Obtimization Objective Gradient Descent Update ๋ง๋˜ฅ์ด๋Š” ๋จผ์ € ๊ฐ ์˜ํ™” ๋ณ„๋กœ ์–ด๋–ค ๋‚ด์šฉ์ด ๋‹ด๊ฒจ์žˆ๋Š”์ง€๋ฅผ feature๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋– ์˜ฌ๋ ธ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์˜ํ™”๊ฐ€ ๋กœ๋งจํ‹ฑํ• ์ˆ˜๋ก ์ข‹์•„ํ•˜๋Š” ์‚ฌ์šฉ์ž๋“ค์ด ์žˆ๊ณ  ์•ก์…˜ ์žฅ๋ฉด์ด ๋งŽ์„์ˆ˜๋ก ์ข‹์•„ํ•˜๋Š” ์‚ฌ์šฉ์ž๋“ค์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์˜ํ™”๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋กœ๋งจํ‹ฑํ•œ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” feature 1 ๊ณผ ์–ผ๋งˆ๋‚˜ ์•ก์…˜ ์žฅ๋ฉด์ด ์ž˜ ๋งŒ๋“ค์–ด์กŒ๋Š”์ง€ ๋‚˜ํƒ€๋‚ด๋Š” feature 2 ๋ฅผ ์ด์šฉํ•ด์„œ ๋ณ„์ ์„ ์˜ˆ์ธกํ•  ์ˆ˜๋„ ์žˆ์ง€ ์•Š์„๊นŒ. ์ข€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ, ๊ฐ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•˜์—ฌ parameter ( ) R๋ฅผ ๊ฒฐ์ •ํ•˜๊ธฐ๋กœ ํ•œ๋‹ค. Predict user as rating movie with ( ( ) ) x ( ) starts Problem Formulation ( , ) 1 if user has rated movie (0 otherwise) ( , ) = rating by user on movie (if defined) ( ) = parameter vector for user x ( ) = feature vector for movie ์‚ฌ์šฉ์ž ๊ฐ€ ์˜ํ™”์— ์ค„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ๋ณ„์ : ( ( ) ) x ( ) ( ) = # movies rated by user ๊ธฐ๋ณธ์ ์œผ๋กœ lin. regression ๋ฌธ์ œ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ( ) R + Obtimization Objective Gradient Descent Update 2) Collaborative Filtering Problem Motivation Optimization Criteria Collaborative Filtering Optimization Objective Collaborative Filtering Algorithm Problem Motivation ์ด์ „์— ์–ธ๊ธ‰ํ•œ ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™์ด ์˜ํ™” ๋‚ด์šฉ์„ ๋‚˜ํƒ€๋‚ด๋Š” feature๋ฅผ ํ†ตํ•ด ๋ณ„์ ์„ ์˜ˆ์ƒํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์ œ๋Š”, ๊ทธ๋Ÿฌํ•œ feature๋ฅผ ์ฐพ๊ธฐ๋„ ์–ด๋ ค์šธ๋ฟ๋”๋Ÿฌ ์˜ํ™”๊ฐ€ ์–ผ๋งˆํผ ๋กœ๋งจํ‹ฑํ•œ์ง€ ๋“ฑ์„ ์ˆ˜์น˜ํ™”ํ•˜๋Š” ๊ฒƒ๋„ ์–ด๋ ต๋‹ค. ๋ง๋˜ฅ์ด๋Š” ๋‹ค๋ฅธ ์•„์ด๋””์–ด๋ฅผ ๋‚ธ๋‹ค. ๋‹ค๋ฆ„ ์•„๋‹Œ feature๋ฅผ ์ž๋™์œผ๋กœ ์ฐพ์•„๋‚ด๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด feature๋ฅผ ์ž๋™์œผ๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ์„๊นŒ? ์ด๋ฒˆ์—๋Š” ( ) ๊ฐ’์„ ๋ฏธ๋ฆฌ ์•Œ๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด์ž. ๊ทธ๋Ÿฌ๋ฉด ( ) ๊ฐ’๋“ค๋กœ๋ถ€ํ„ฐ ๊ฐ ์˜ํ™”์˜ 1 x๋ฅผ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ( ) ๊ฐ’์€ ์–ด๋–ป๊ฒŒ ๊ตฌํ• ๊นŒ. ๋‹ต์€ ์˜์™ธ๋กœ ๊ฐ„๋‹จํ•˜๋‹ค. ์ž„์˜์˜ ๊ฐ’์„ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค. ( ) ๋ฅผ ํ†ตํ•ด 1 x, . ์„ ๊ตฌํ•˜๊ณ , ๊ทธ 1 x, .๋กœ๋ถ€ํ„ฐ ์ตœ์ ์˜ ( ) ๋ฅผ ๋‹ค์‹œ ๊ตฌํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋‹ค ๋ณด๋ฉด ์ œ๋ฒ• ์“ธ๋งŒํ•œ feature๋“ค์„ ์–ป๊ฒŒ ๋œ๋‹ค. Optimization Criteria Collaborative Filtering Random initialization ํ•œ ํ›„ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉฐ ์ตœ์ ํ™”ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. Optimization Objective ๋‘ optimization์„ ๋™์‹œ์— ํ•˜๋ฉด ํ›จ์”ฌ ํšจ์œจ์ ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” bias unit 0 1 ์ด ์ œ์™ธ๋˜์—ˆ๋‹ค. ์–ด์ฐจํ”ผ feature ์ž์ฒด๋„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์Šค์Šค๋กœ ๊ฒฐ์ •ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. Collaborative Filtering Algorithm 3) Low Rank Matrix Factorization Vectorization Low Rank Matrix Factorization Finding related movies Vectorization Collaborative filtering์„ matrix ๊ผด๋กœ ๋‚˜ํƒ€๋‚ด์–ด ๋ณด์ž. Low Rank Matrix Factorization Finding related movies 4) Implementational detail Implementational Detail: Mean Normalization Implementational Detail: Mean Normalization = , ( ) R ๋ณ„์ ์„ ํ•˜๋‚˜๋„ ๋งค๊ธฐ์ง€ ์•Š์€ ์‚ฌ๋žŒ์€ ๋ชจ๋“  ์˜ํ™”์— '0์ '์„ ์ค„ ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์€ ๋ณ„๋กœ ์œ ์šฉํ•˜์ง€ ์•Š๋‹ค. 13. Large Scale Machine Learning ์ด์ „์— ๋“ค์—ˆ๋˜ ์˜ˆ์‹œ Classify between confusable words eg. {to, two, too}, {then, than}, ... For breakfast I ate ______ eggs. --> Low-bias algorithm & large data set ์ฆ‰, ์ •๊ตํ•œ classifier๋ฅผ ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ ์ด์ƒ์œผ๋กœ ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ํšจ๊ณผ์ ์ด๋‹ค. It's not who has the best algorithm that wins. It's who has the most data ๊ทธ๋ ‡๋‹ค๋ฉด ๊ทธ๋Ÿฌํ•œ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•œ ํ•™์Šต์€ ์–ด๋–ป๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ผ๊นŒ. 1) Gradient Descent with Large Datasets Learning with Large Datasets Stochastic Gradient Descent Linear Regression with Gradient Descent Batch Gradient Descent vs. Stochastic Gradient Descent Minibatch Gradient Descent Stochastic Gradient Descent Convergence Checking for Convergence Learning with Large Datasets ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด high variance์ธ ๊ฒฝ์šฐ์—๋Š” ํฐ dataset์ด ๋„์›€์ด ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ high bias์ธ ๊ฒฝ์šฐ์—๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์•„๋ฌด ๋„์›€์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค. Dataset์€ 100,000,000 ๊ฐœ example์— ์ด๋ฅผ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋•Œ, gradient descent๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด 1๋ฒˆ iteration ๋Œ ๋•Œ๋งˆ๋‹ค 100,000,000 ๋ฒˆ์˜ ๋ง์…ˆ์„ ํ•ด์•ผ ํ•œ๋‹ค. = 100 000 000 Gradient descent: j := j ฮฑ m i 1 ( ฮธ ( ( ) ) y ( ) ) j ( ) ์ด๋Š” ์–ด๋งˆ์–ด๋งˆํ•œ ์—ฐ์‚ฐ๋Ÿ‰์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ ์ด๋ ‡๊ฒŒ ๋งŽ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ณผ์—ฐ ๊ผญ ํ•„์š”ํ•œ์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ฐ€๋ น, ์ž„์˜๋กœ ๋ฝ‘์€ = , 000 subset ๋งŒ์œผ๋กœ ์ถฉ๋ถ„ํžˆ ํ•™์Šต์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ๊ตณ์ด ๊ทธ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ์ด๋Š” learning curve๋ฅผ ๊ทธ๋ ค๋ด„์œผ๋กœ์จ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋” ๋„ฃ์–ด์ฃผ์–ด๋„ ๋” ์ด์ƒ ์„ฑ๋Šฅ ๊ฐœ์„ ์ด ์—†๋Š” high bias ํ˜•ํƒœ๋ผ๋ฉด ์ ๋‹น๋Ÿ‰ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค. Stochastic Gradient Descent Stochastic gradient descent๋Š” classic (or batch) gradient descent๋ฅผ ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋ฉฐ ํฐ dataset๋กœ scalable ํ•œ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค. Linear Regression with Gradient Descent Hypothesis h ( ) โˆ‘ = n j j Cost function J train ( ) 1 m i 1 ( ฮธ ( ( ) ) y ( ) ) Gradient Descent Repeat{ j := j ฮฑ m i 1 ( ฮธ ( ( ) ) y ( ) ) j ( ) (for every = , , ) } ์ด ํฌ๋ฉด ๋งค iteration๋งˆ๋‹ค ๊ณ„์‚ฐ๋Ÿ‰์ด ํฌ๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ์žก์•„๋จน๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค. Batch Gradient Descent vs. Stochastic Gradient Descent Batch gradient descent Stochastic descent cost ( , ( ( ) y ( ) ) ) 1 ( ฮธ ( ( ) ) y ( ) ) How well my hypothesis doing on a single example ( ( ) y ( ) ) train ( ) 1 m i 1 ( ฮธ ( ( ) ) y ( ) ) J train ( ) 1 โˆ‘ = m cost ( , ( ( ) y ( ) ) ) Repeat{ Missing open brace for subscript Missing open brace for subscript (for every = , , ) } 1. Randomly shuffle dataset 2. Repeat{ e.g., 10 times for = , , { Missing open brace for subscript Missing open brace for subscript (for = , , ) } } Minibatch Gradient Descent Batch gd: Use all examples in each iteration Stochastic gd: Use 1 example in each iteration Mini-batch gd: Use examples in each iteration where < is the mini-batch size (eg. = 10 ) Stochastic Gradient Descent Convergence To make sure its convergence To choose the learning rate Checking for Convergence Batch Gradient Descent: Plot train ( ) as a function of the number of iterations of gradient descent train ( ) 1 m i 1 ( ฮธ ( ( ) ) y ( ) ) Stochastic Gradient Descent: cost ( , ( ( ) y ( ) ) ) 1 ( ฮธ ( ( ) ) y ( ) ) During learning, compute cost ( , ( ( ) y ( ) ) ) before updating using ( ( ) y ( ) ) Every 1,000 iterations (say), plot cost ( , ( ( ) y ( ) ) ) averaged over the last 1,000 examples processed by algorithm. 2) Advanced Topics Online Learning Other Online Learning Example Map Reduce and Data Parallelism Map Reduce Map-Reduce and Summation over the Training Set Multi-Core Machines Online Learning ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•œ ํƒ๋ฐฐ ํšŒ์‚ฌ ์›น์‚ฌ์ดํŠธ์—์„œ ์ถœ๋ฐœ์ง€์™€ ๋ฐฐ์†ก์ง€๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฐฐ์†ก๋น„ ๊ฒฌ์ ์„ ์ œ์‹œํ•ด ์ค€๋‹ค๊ณ  ํ•˜์ž. ์‚ฌ์šฉ์ž๋“ค์€ ํ•ด๋‹น ํƒ๋ฐฐ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ  ( = ) ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค ( = ). Feature ๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด, ์ถœ๋ฐœ์ง€, ๋ฐฐ์†ก์ง€, ๊ทธ๋ฆฌ๊ณ  ๊ฒฌ์ ๊ฐ€ ๋“ฑ์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ, ์ตœ์ ์˜ ๊ฐ€๊ฒฉ์„ ์•Œ์•„๋‚ด๊ธฐ ์œ„ํ•ด ( = | ; ) ๋ฅผ ๊ตฌํ•˜๊ณ ์ž ํ•œ๋‹ค. Logistic regression์„ ์ด์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ผ๋ฐ˜์ ์ธ logistic regression๊ณผ ๋‹ค๋ฅธ ์ ์ด๋ผ๋ฉด ํ•˜๋‚˜์˜ example ๋งˆ๋‹ค๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ํ•œ ๋ฒˆ ์‚ฌ์šฉํ•œ example์€ ๋ฒ„๋ฆฐ๋‹ค๋Š” ์ ์ด๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ€ํ™”ํ•˜๋Š” ์‚ฌ์šฉ์ž ์„ฑํ–ฅ์— ๋งž์ถฐ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. Other Online Learning Example Product search (learning to search) : User searches for "Android phone 1080p camera" Have 100 phones in store. Will return 10 results. : feature of phone, how many words in user query match name of phone, how many words in query match description of phone, etc, ... : 1 if user clicks on link, 0 otherwise Learn ( = | ; ) Predicted click-through rate (CTR) Use to show user the 10 phones they're most likely to click on Other examples: Choosing special offer to show user Customized selection of news articles Product recommendation ... Online learning์€ stochastic gd์™€ ๋น„์Šทํ•˜์ง€๋งŒ, fixed set์ด ์•„๋‹Œ continuous stream์œผ๋กœ data ๋ฐ›์œผ๋ฉฐ ๊ฐ example ํ•œ ๋ฒˆ ์“ฐ๊ณ  ๋ฒ„๋ฆฐ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. Map Reduce and Data Parallelism Map Reduce Batch gradient descent j := j ฮฑ 400 i 1 400 ( ฮธ ( ( ) ) y ( ) ) j ( ) Map-Reduce and Summation over the Training Set Many learning algorithms can be expressed as comuting sums of functions over the training set eg. For advanced optimization, with logistic regression, need train ( ) โˆ’ m i 1 ( ( ) log h ( ( ) ) ( โˆ’ ( ) ) log ( โˆ’ ฮธ ( ( ) ) ) โˆ‚ j train ( ) 1 โˆ‘ = m ( ฮธ ( ( ) ) y ( ) ) j ( ) Multi-Core Machines 14. Application Example: Photo OCR A-1. Probability and Likelihood Distinguishing Likelihood from Probability Using the Same Function "Forwards" and "Backwards" Bayesian Inference ์ฐธ๊ณ  ์ž๋ฃŒ ๋จธ์‹ ๋Ÿฌ๋‹์„ ๊ณต๋ถ€ํ•˜๋‹ค ๋ณด๋ฉด probability์™€ likelihood๋ผ๋Š” ๋ง์ด ๋งŽ์ด ๋‚˜์˜จ๋‹ค. ์ผ์ƒ ์–ธ์–ด์—์„œ๋Š” ๋น„์Šทํ•œ ๋œป์„ ์ง€๋‹ˆ์ง€๋งŒ ์ „๋ฌธ์šฉ์–ด๋กœ ์“ฐ์ผ ๋•Œ์—๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. Distinguishing Likelihood from Probability Probability๋Š” possible result์— ๊ฒฐ๋ถ€๋œ ๊ฒƒ์ด๊ณ  likelihood๋Š” hypothesis์— ๊ฒฐ๋ถ€๋œ ๊ฒƒ์ด๋‹ค. Possible result๋Š” mutually exclusinve ํ•˜๊ณ  exhaustive ํ•˜๋‹ค. ๋™์ „ ๋˜์ง€๊ธฐ๋ฅผ 10๋ฒˆ ํ•ด์„œ ์–ด๋Š ๋ฉด์ด ๋‚˜์˜ฌ์ง€ ์ถ”์ธกํ•œ๋‹ค๊ณ  ํ•ด๋ณด์ž. ๋‹จ 11๊ฐ€์ง€์˜ possible results๊ฐ€ ์žˆ๋‹ค (0~10 ๋ฒˆ์˜ ๋งž๋Š” ์˜ˆ์ธก). Actual result๋Š” possible result ์ค‘ ๋‹จ ํ•˜๋‚˜์˜ ๊ฒฝ์šฐ์ด๋‹ค. ์ฆ‰, possible results์— ๊ฒฐ๋ถ€๋œ probability๋Š” ์–ธ์ œ๋‚˜ ํ•ฉ์ด 1์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. Hypothesis๋Š” result์™€๋Š” ๋‹ค๋ฅด๊ฒŒ, mutually exclusive ํ•˜์ง€๋„ ์•Š๊ณ  exhaustive ํ•˜์ง€๋„ ์•Š๋‹ค. ๊ฐ€๋ น, ์ฒซ ๋ฒˆ์งธ ์‹คํ—˜์ž๊ฐ€ 10๋ฒˆ ์ค‘ 7๋ฒˆ ๋งž์•˜๋‹ค๊ณ  ํ•˜์ž. ์ด๋ฅผ ์‹คํ—˜์ž๊ฐ€ ๊ทธ์ € ์ฐ์—ˆ๋‹ค๊ณ  hypothesis๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๊ณ  ์‹คํ—˜์ž์—๊ฒŒ ์ผ์ข…์˜ ์ดˆ์ž์—ฐ์ ์ธ ๋Šฅ๋ ฅ์ด ์žˆ์–ด ๋‹จ์ˆœํžˆ ์ฐ๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์กฐ๊ธˆ ๋” ์ •ํ™•๋„๊ฐ€ ๋†’๋‹ค๊ณ  hypothesis๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. ํ˜น์€ ์‹คํ—˜์ž์˜ ์ดˆ์ž์—ฐ์  ๋Šฅ๋ ฅ์ด ์‹คํ—˜ ๊ฒฐ๊ณผ์— ๋‚˜ํƒ€๋‚œ ๊ฒƒ๋ณด๋‹ค ๋” ํฌ๋‹ค๋Š” ๊ฐ€์„ค์„ ์„ธ์šธ ์ˆ˜๋„ ์žˆ๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ€์„ค๋“ค์ด์ง€๋งŒ mutually exclusive ํ•˜์ง€ ์•Š๋‹ค. ์–ด๋–ค ๊ฐ€์„ค์ด ๋‹ค๋ฅธ ๊ฐ€์„ค์„ ํฌํ•จํ•  ์ˆ˜๋„ ์žˆ๋‹ค. Likelihood์™€ ๊ฒฐ๋ถ€์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” hypothesis์˜ ๊ฐ€์ง“์ˆ˜๋Š” ์šฐ๋ฆฌ์˜ ์ƒ์ƒ๋ ฅ์— ๋‹ฌ๋ ค์žˆ์„ ๋ฟ์ด๋‹ค. ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฐ€์„ค์„ ์„ธ์›Œ๋ณด์•˜๋‹ค๊ณ  ํ™•์‹ ํ•˜๊ธฐ๋Š” ์–ด๋ ต๋‹ค. ๊ทธ๋ณด๋‹ค๋Š” ์‹คํ—˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ ๊ฐ€์„ค์˜ ์ƒ๋Œ€์ ์ธ likelihood์— ์–ผ๋งˆ๋งŒํผ์˜ ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”๊ฐ€๊ฐ€ ๊ด€์‹ฌ์‚ฌ์ด๋‹ค. Hypothesis์— ๊ฒฐ๋ถ€๋˜๋Š” likelihood๋Š” ๊ทธ ์ž์ฒด๋กœ ์•„๋ฌด ์˜๋ฏธ๊ฐ€ ์—†๊ณ  ์ƒ๋Œ€์ ์ธ ๊ฐ’, ์ฆ‰ ํ•œ likelihood์˜ ๋‹ค๋ฅธ ํ•˜๋‚˜์— ๋Œ€ํ•œ ๋น„์œจ์— ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค. Using the Same Function "Forwards" and "Backwards" Probability์™€ likelihood์˜ ์ฐจ์ด๋Š” probability distribution function์„ ์ด์šฉํ•˜๋ฉด ๋” ๋ช…ํ™•ํ•ด์ง„๋‹ค. ๋™์ „์„ ๋˜์ง€๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ์„ฑ๊ณต/์‹คํŒจ๋กœ ์ด๋ถ„๋ฒ•์  ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” binomial ์‹œํ–‰์„ ์˜ˆ๋กœ ๋“ค์–ด๋ณด๊ฒ ๋‹ค. ์ด๋•Œ, ๊ฐ€๋Šฅํ•œ '์„ฑ๊ณต' ํšŸ์ˆ˜๋ฅผ ๋ณ€์ˆ˜๋กœ ํ•˜์—ฌ probability๋ฅผ ๊ตฌํ•˜๊ฒŒ ๋œ๋‹ค. ์‹œํ–‰ํ•  ํšŸ์ˆ˜ (๋™์ „์„ ๋ช‡ ๋ฒˆ ๋˜์งˆ ๊ฒƒ์ธ๊ฐ€)์™€ ์„ฑ๊ณต ํ™•๋ฅ  (์˜ˆ์ธกํ•œ ๋™์ „์˜ ๋ฉด์ด ๋‚˜์˜ฌ ํ™•๋ฅ ์ด ์–ผ๋งˆ์ธ๊ฐ€)๋Š” ํ™•๋ฅ ๋ถ„ํฌ์˜ parameter๋กœ, ์ด parameter ๊ฐ’๋“ค์— ๋ฌด๊ด€ํ•˜๊ฒŒ ๊ฐ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ probability์˜ ํ•ฉ์€ 1์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. ์ด parameter๊ฐ€ ๊ณ ์ •๋˜์—ˆ์„ ๋•Œ, possible number of success ๋ณ„๋กœ ๊ทธ probability์„ ๊ณ„์‚ฐํ•œ๋‹ค. (๊ทธ๋ฆผ 1 ์ƒ๋‹จ) ๋ฐ˜๋ฉด, likelihood๋ฅผ ๊ตฌํ•  ๋•Œ ์ฃผ์–ด์ง€๋Š” ๊ฒƒ์€ '์„ฑ๊ณต'ํ•œ ํšŸ์ˆ˜ (์˜ˆ์ธกํ•œ ๋ฉด์ด ๋‚˜์˜จ ํšŸ์ˆ˜)์™€ ์‹œํ–‰ํ•œ ํšŸ์ˆ˜ (๋™์ „์„ ๋ช‡ ๋ฒˆ ๋˜์กŒ๋Š”๊ฐ€)์ด๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์ด๋ฒˆ์—๋Š” result๊ฐ€ parameter๋กœ ์—ฌ๊ฒจ์ง€๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฒˆ์—๋Š” ์˜ˆ์ƒ๋˜๋Š” ์„ฑ๊ณต ํšŸ์ˆ˜๊ฐ€ ๋ณ€์ˆ˜๊ฐ€ ๋˜๋Š” ๋Œ€์‹  ์„ฑ๊ณตํ•  ํ™•๋ฅ ์ด ๋ณ€์ˆ˜๊ฐ€ ๋œ๋‹ค. ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด probability๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์˜ ์—ญ๋ฐฉํ–ฅ ์—ฐ์‚ฐ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค. (๊ทธ๋ฆผ 1 ํ•˜๋‹จ) (๊ทธ๋ฆผ 1. Figure from R. Gallistel. ) ์ด๋Ÿฌํ•œ binomial likelihood function์€ ๋Œ€๋‹จํžˆ ์ง๊ด€์ ์ด๋‹ค. ๋งŒ์•ฝ 10๋ฒˆ ์‹œํ–‰์—์„œ 7๋ฒˆ ์„ฑ๊ณตํ•˜์˜€๋‹ค๋ฉด, binomial distribution์—์„œ์˜ probability parameter (the distribuion of succesful predictions from this subject)๊ฐ€ 0.1์ด ๋˜๊ธฐ๋Š” ์•„์ฃผ ์–ด๋ ต๋‹ค. 0.7์ผ ๊ฐ€๋Šฅ์„ฑ์ด ๋” ๋†’์ง€๋งŒ 0.5๋„ ์™„์ „ํžˆ ๊ฐ€๋Šฅ์„ฑ์ด ์—†์ง€๋Š” ์•Š๋‹ค. = 0.7 ์ผ likelihood๋Š” 0.27์ด๊ณ , = 0.5 ์ผ likelihood๋Š” 0.12์ธ๋ฐ, ์ด ๋น„์œจ์€ ๊ฒจ์šฐ 2.28์ด๋‹ค. ์ฆ‰, ์ฃผ์–ด์ง„ ์‹คํ—˜ ๊ฒฐ๊ณผ์— ๋น„์ถ”์–ด ํ•ด๋‹น ์‹คํ—˜์ž์˜ long-term ์„ฑ๊ณต๋ฅ ์ด 0.5์ด๋ผ๋Š” ๊ฐ€์„ค๋ณด๋‹ค 0.7์ด๋ผ๋Š” ๊ฐ€์„ค์ด ๋” ๋งž์„ ๊ฐ€๋Šฅ์„ฑ์€ 2๋ฐฐ ์ •๋„์ด๋‹ค. Data science์—์„œ hypothesis๋Š”, ์œ„ ์˜ˆ์—์„œ ๋ณธ ๋ฐ”์™€ ๊ฐ™์ด, ํŠน์ • ๋ถ„ํฌ์˜ ํ‰๊ท ๊ฐ’์˜ ๊ฐ€๋Šฅํ•œ ๊ฐ’, ํ˜น์€ ๊ทธ ๋ฒ”์œ„๋ฅผ ๋งํ•œ๋‹ค. Bayesian Inference posterior likelihood prior model evidence ( | ) P ( | ) ( ) ( ) : Hypothesis. Data (or evidence) ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๊ฐ€์„ค ๋ฌด์—‡์ด๋“ . ์ผ๋ฐ˜์ ์œผ๋กœ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฒฝํ•ฉํ•˜๋Š” ๊ฐ€์„ค์ด ์กด์žฌํ•˜๊ณ  ๊ทธ์ค‘ ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ๊ฒƒ์„ ๊ณ ๋ฅธ๋‹ค. : Evidence or data. Prior probability๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์ด์šฉ๋˜์ง€ ์•Š์€ ์ƒˆ observation. ( ) : Prior probability. Current evidence๋ฅผ ๊ด€์ฐฐํ•˜๊ธฐ ์ „์ด ๋ฏธ๋ฆฌ ๊ณ„์‚ฐ๋œ hypothesis H์˜ ์ถ”์ • ๊ฐ’. ( | ) : Posterior probability. Observed data E๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ์˜ hypothesis H ์ผ ํ™•๋ฅ . ์ด ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋‹ค. ( | ) : Likelihood. H๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ E๋ฅผ ๊ด€์ฐฐํ•  ํ™•๋ฅ . H๊ฐ€ ๊ณ ์ •๋˜์—ˆ์„ ๋•Œ์˜ E์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋กœ, ์ฃผ์–ด์ง„ ๊ฐ€์„ค์— ๋Œ€ํ•œ evidence์˜ compatibility๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. Posterior probability๊ฐ€ H์— ๋Œ€ํ•œ ํ•จ์ˆ˜์ธ ๋ฐ˜๋ฉด likelihood๋Š” E์— ๋Œ€ํ•œ ํ•จ์ˆ˜์ด๋‹ค. ( ) : Marginal likelihood or model evidence. ๋ชจ๋“  ๊ฐ€์„ค์„ ๊ณ ๋ คํ•ด์„œ ๊ณ„์‚ฐํ•œ ํ™•๋ฅ ๋กœ, ์–ด๋–ค ๊ฐ€์„ค์—๋“  ๋™์ผํ•˜๊ฒŒ ์ ์šฉ๋˜๋ฏ€๋กœ ๊ฐ€์„ค ๊ฐ„์˜ ๋น„๊ต์—๋Š” ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค. ( ) โˆ‘ P ( | k ) ( k ) ( | , ) P ( | , ) ( | ) P ( | , ) ( | ) ฮธ Likelihood๋Š” probability๊ฐ€ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•œ๋‹ค. Assume fixed, chosen model m, P ( | , ) X 1 In this case, probability density Bayesian inference์—์„œ๋Š” ๋ณ€ํ•˜๊ณ , ์ด ๊ณ ์ •. ์ฆ‰, ( | , ) P ( | , ) โˆ’ โˆž ( | , ) ฮธ ( โˆž + ) ์ฐธ๊ณ  ์ž๋ฃŒ "Bayes for Beginners: Probability and Likelihood" by C. RANDY GALLISTEL https://en.wikipedia.org/wiki/Bayesian_inference https://www.youtube.com/watch?v=sm60vapz2jQ https://towardsdatascience.com/probability-concepts-explained-bayesian-inference-for-parameter-estimation-90e8930e5348 A-2. AdaBoost ์ฐธ๊ณ  ์ž๋ฃŒ Wikipedia: AdaBoost "Boosting and AdaBoost for Machine Learning" A-3. Gaussian Mixture Model Gaussian mixture model (GMM)์€ ์ž„์˜์˜ ๋ถ„ํฌ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ Gaussian ๋ถ„ํฌ์˜ weighted sum์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. A-4. Feature Selection<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: ์œ„ํ‚ค๋…์Šค ### ๋ณธ๋ฌธ: Since 2008 ์œ„ํ‚ค๋…์Šค๋Š” ์˜จ๋ผ์ธ ์ฑ…์„ ์ œ์ž‘<NAME>๋Š” ํ”Œ๋žซํผ ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค. ๋ˆ„๊ตฌ๋‚˜ ์œ„ํ‚ค๋…์Šค์—์„œ ์ฑ…์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์š” ์ด๋ ฅ ์•„๋ฌด๊ฑฐ๋‚˜ ์งˆ๋ฌธ ์œ„ํ‚ค๋…์Šค ๋””์Šค์ฝ”๋“œ https://discord.gg/zPERvk7pza 01 ์œ„ํ‚ค๋…์Šค์˜ ํŠน์ง• ์œ„ํ‚ค๋…์Šค๋Š” ์ผ๋ฐ˜ ์ฑ…๋“ค๊ณผ๋Š” ๋‹ค๋ฅธ ๋งŽ์€ ํŠน์ง•๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๊ฒฐํ•จ ์‚ด์•„์žˆ๋Š” ์ฑ… ๋ชฉ์ฐจ ์ €์ž์™€์˜ ๊ต๋ฅ˜ ๊ณ ์œ  URL ์•Œ๋ฆผ ๊ธฐ๋Šฅ ๊ณต๋™์ž‘์—… ๊ธฐ๋Šฅ ๋ฐฑ์—… ๋ณ€๊ฒฝ ์ด๋ ฅ ์ˆ˜์ต ์•ฑ (App) ๊ฐ„๊ฒฐํ•จ ์„œ๋น„์Šค ์ „๋ฐ˜์— ๊ฑธ์ณ ๊ฐ„๊ฒฐํ•จ์„ ์—ผ๋‘์— ๋‘๊ณ  ์ œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋…์ž๊ฐ€ ๋ฌธ์„œ ์ฝ๊ธฐ์— ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์‚ด์•„์žˆ๋Š” ์ฑ… ์ข…์ด์ฑ…์€ ํ•œ๋ฒˆ ์ธ์‡„๋˜๋ฉด ๊ทธ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์œ„ํ‚ค๋…์Šค ์ฑ…์€ ๋…์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋ฉฐ ๋ฐœ์ „ํ•ฉ๋‹ˆ๋‹ค. ์ฑ…์˜ ๋‚ด์šฉ์€ ์‹ค์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ๋˜๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ๋…์ž๋Š” ํ•ญ์ƒ ์ตœ์‹  ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชฉ์ฐจ ์œ„ํ‚ค๋…์Šค ์ฑ…์€ ๋ชฉ์ฐจ๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ํŽ˜์ด์ง€์—์„œ๋„ ๋ชฉ์ฐจ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ•ด๋‹น ํŽ˜์ด์ง€๋กœ์˜ ์ด๋™์ด ์‰ฝ์Šต๋‹ˆ๋‹ค. ์ €์ž์™€์˜ ๊ต๋ฅ˜ ์œ„ํ‚ค๋…์Šค ์ฑ…์€ ํŽ˜์ด์ง€๋งˆ๋‹ค ํ•˜๋‹จ์— ํ”ผ๋“œ๋ฐฑ ๋˜๋Š” ๋Œ“๊ธ€์„ ๋‹ฌ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ”ผ๋“œ๋ฐฑ๊ณผ ๋Œ“๊ธ€์„ ํ†ตํ•ด ์ž‘๊ฐ€์™€ ๋…์ž๋Š” ์ƒํ˜ธ ์˜์‚ฌ์†Œํ†ตํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ๋”์šฑ ์ข‹๊ฒŒ ๋ฐœ์ „์‹œ์ผœ ๋‚˜๊ฐ‘๋‹ˆ๋‹ค. ๊ณ ์œ  URL ์œ„ํ‚ค๋…์Šค ์ฑ…์€ ํŽ˜์ด์ง€๋ณ„๋กœ ๊ณ ์œ ํ•œ URL์ด ์žˆ์–ด ๋‹ค๋ฅธ ์‚ฌ๋žŒ ๋˜๋Š” ์ต๋ช…์˜ ๋…์ž์—๊ฒŒ ๋งํฌ๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค. ์•Œ๋ฆผ ๊ธฐ๋Šฅ ๋…์ž๊ฐ€ ๋Œ“๊ธ€ ๋˜๋Š” ํ”ผ๋“œ๋ฐฑ์„ ์ž‘์„ฑํ•  ๊ฒฝ์šฐ ์ €์ž์—๊ฒŒ ์ด๋ฉ”์ผ๋กœ ํ•ด๋‹น ๋‚ด์šฉ์ด ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๊ณต๋™์ž‘์—… ๊ธฐ๋Šฅ ์ฑ… ๋‹จ์œ„๋กœ ๊ณต๋™ ์ €์ž๋ฅผ ์„ค์ •ํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ช…์ด ํ•จ๊ป˜ ์ฑ…์„ ๋งŒ๋“ค์–ด ๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฑ์—… "์ฑ… ๋‹ค์šด๋กœ๋“œ" ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜์—ฌ ์ฑ… ์ „์ฒด๋ฅผ ๋ฐฑ์—… ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ์ด๋ ฅ "๋ณ€๊ฒฝ ์ด๋ ฅ" ํ™”๋ฉด์—์„œ ํŽ˜์ด์ง€์˜ ํŠน์ • ๋ฒ„์ „์œผ๋กœ ๋˜๋Œ๋ฆฌ๊ธฐ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜์ต ์ €์ž๋Š” ์ „์ž์ฑ…์„ ์ถœ๊ฐ„ํ•˜๊ฑฐ๋‚˜ ์ฑ…์— ๊ด‘๊ณ ๋ฅผ ๊ฒŒ์‹œํ•˜์—ฌ ์ˆ˜์ต์„ ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ฑ (App) ์•ˆ๋“œ๋กœ์ด๋“œ, ์•„์ดํฐ์˜ ์œ„ํ‚ค๋…์Šค ์•ฑ์„ ์ด์šฉํ•˜๋ฉด ๋ชจ๋ฐ”์ผ ํ™˜๊ฒฝ์—์„œ ๋”์šฑ ํŽธ๋ฆฌํ•˜๊ฒŒ ์œ„ํ‚ค๋…์Šค ์ฑ…์„ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 02 ์œ„ํ‚ค๋…์Šค FAQ ์ €์ž๊ฐ€ ๋˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋‚˜์š”? ๋ชฉ์ฐจ ํŽ˜์ด์ง€ ์ˆœ์„œ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐ”๊พธ๋‚˜์š”? ์ œ๋ชฉ์„ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‚˜์š”? ์œ„ํ‚ค๋…์Šค์—์„œ ์ž‘์„ฑํ•œ ์ฑ…์„ ๋‹ค๋ฅธ ์ถœํŒ์‚ฌ์—์„œ "์ข…์ด์ฑ…"์œผ๋กœ ์ถœํŒํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์˜ ์ˆ˜์ต๋ชจ๋ธ์€ ๋ฌด์—‡์ธ๊ฐ€์š”? ๋“ฑ๋กํ•œ ๊ด‘๊ณ ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ด‘๊ณ ๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ํ‹ฐ์Šคํ† ๋ฆฌ์—์„œ ์• ๋“œ์„ผ์Šค ๊ณ„์ •์„ ์Šน์ธ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์Šน์ธ๋œ ์• ๋“œ์„ผ์Šค ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ €์žฅํ•ด๋„ ๊ด‘๊ณ ๊ฐ€ ํ‘œ์‹œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์—์„œ ์ž‘์„ฑํ•œ ์ฑ…์„ ์ถœํŒํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ, ์ถœํŒ์‚ฌ์—์„œ ์œ„ํ‚ค๋…์Šค์˜ ๊ธ€์„ ๋ชจ๋‘ ๋‚ด๋ฆฌ๊ธฐ๋ฅผ ์š”๊ตฌํ•˜๋Š” ์ƒํ™ฉ์ด ์˜ค๋ฉด ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”? ์œ„ํ‚ค๋…์Šค์—์„œ ์ „์ž์ฑ…์„ ํŒ๋งคํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์•ฑ์ด ์žˆ๋‚˜์š”? ์œ„ํ‚ค๋…์Šค์—๋Š” OpenAPI๊ฐ€ ์žˆ๋‚˜์š”? ์ฑ… ํ‘œ์ง€ ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋‚˜์š”? ์œ„ํ‚ค๋…์Šค์—์„œ ์ „์ž์ฑ…์„ ๊ตฌ์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‚ด๋ ค๋ฐ›์€ ์ „์ž์ฑ…์„ ๋ถ„์‹คํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌด๋ฃŒ๋กœ ์ „์ž์ฑ… ์žฌ๋ฐœ์†ก์ด ๊ฐ€๋Šฅํ•œ๊ฐ€์š”? ์œ„ํ‚ค๋…์Šค ๊ณ„์ • ํƒˆํ‡ด๋ฅผ ์›ํ•ฉ๋‹ˆ๋‹ค. ์ฑ…์„ ๊ณต๊ฐœํ•ด๋„ ํ‘œ์‹œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ €์ž๊ฐ€ ๋˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋‚˜์š”? ์œ„ํ‚ค๋…์Šค์—์„œ ์ €์ž๊ฐ€ ๋˜๊ธฐ ์œ„ํ•œ ํŠน๋ณ„ํ•œ ์ ˆ์ฐจ๋Š” ์—†์Šต๋‹ˆ๋‹ค. ํšŒ์›๊ฐ€์ž… ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ฑ…์„ ์ž‘์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค -> ๋กœ๊ทธ์ธ -> ๊ณ„์ • ์„ค์ • -> ๋‚˜์˜ ์ฑ… -> ์ƒˆ ์ฑ… ๋งŒ๋“ค๊ธฐ ๋ชฉ์ฐจ ํŽ˜์ด์ง€ ์ˆœ์„œ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐ”๊พธ๋‚˜์š”? ์œ„ํ‚ค๋…์Šค์˜ ๋ชฉ์ฐจ๋Š” ์ด๋ฆ„์ˆœ์œผ๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชฉ์ฐจ๋ฅผ ์›ํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๋ ค๋ฉด ์ œ๋ชฉ ์•ž์— ์ˆซ์ž ๋“ฑ์„ ์‚ฝ์ž…ํ•˜์—ฌ ์ •๋ ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œˆ๋„(Windows) ํƒ์ƒ‰๊ธฐ์˜ ์ •๋ ฌ ๋ฐฉ์‹๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ œ๋ชฉ์„ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‚˜์š”? ์ œ๋ชฉ์„ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ญ์ˆœ ์ •๋ ฌ ๊ธฐ๋Šฅ์€ ๋ณดํ†ต ๋ธ”๋กœ๊ทธ ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•  ๊ฒฝ์šฐ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ œ๋ชฉ์˜ ์•ž๋ถ€๋ถ„์— ๋‚ ์งœ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ๋‚ ์งœ์˜ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. [์ฑ… ์ˆ˜์ • -> ๊ธฐํƒ€ -> ์—ญ์ˆœ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๊นŒ?] ํ•ญ๋ชฉ์„ "์˜ˆ"๋กœ ํ™œ์„ฑํ™”ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์—์„œ ์ž‘์„ฑํ•œ ์ฑ…์„ ๋‹ค๋ฅธ ์ถœํŒ์‚ฌ์—์„œ "์ข…์ด์ฑ…"์œผ๋กœ ์ถœํŒํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์—์„œ ์ž‘์„ฑ๋œ ์ฑ…์˜ ์ €์ž‘๊ถŒ์€ ์œ„ํ‚ค๋…์Šค๊ฐ€ ์•„๋‹Œ ์ €์ž์—๊ฒŒ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ €์ž๋Š” ์›ํ•  ๊ฒฝ์šฐ ๋‹ค๋ฅธ ์ถœํŒ์‚ฌ์— ์œ„ํ‚ค๋…์Šค์— ์˜ฌ๋ฆฐ ๊ธ€์„ ์ฑ…์œผ๋กœ ์ถœํŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ž์ฑ… ์ถœ๊ฐ„์„ ์›ํ•˜์‹ค ๊ฒฝ์šฐ์—๋Š” ์œ„ํ‚ค๋…์Šค์—์„œ ์ „์ž์ฑ…์„ ์ถœ๊ฐ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์ „์ž์ฑ… ์ถœ๊ฐ„ : https://wikidocs.net/150 ์œ„ํ‚ค๋…์Šค์˜ ์ˆ˜์ต๋ชจ๋ธ์€ ๋ฌด์—‡์ธ๊ฐ€์š”? ์œ„ํ‚ค๋…์Šค๋Š” ์ฑ…์— ๊ตฌ๊ธ€ ๊ด‘๊ณ ๋ฅผ ๊ฒŒ์‹œํ•˜์—ฌ ์ˆ˜์ต์„ ๋ƒ…๋‹ˆ๋‹ค. (๊ด‘๊ณ ์˜ ํ‘œ์‹œ ๋น„์œจ์€ ์ €์ž์˜ ๊ด‘๊ณ  90%, ์œ„ํ‚ค๋…์Šค ๊ด‘๊ณ  10%๋กœ ๋ฐฐ๋ถ„๋ฉ๋‹ˆ๋‹ค. ํ™œ์„ฑํ™”๋œ ์• ๋“œ์„ผ์Šค ๊ณ„์ •์ด ์—†๋Š” ์ €์ž์˜ ๊ฒฝ์šฐ ์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜์ต์„ ์ฐฝ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) ์ „์ž์ฑ…์„ ํŒ๋งคํ•˜์—ฌ ์ˆ˜์ต์„ ๋ƒ…๋‹ˆ๋‹ค. (์ „์ž์ฑ…์˜ ์ˆ˜์ต์€ ์ €์ž 80%, ์œ„ํ‚ค๋…์Šค 20%๋กœ ๋ฐฐ๋ถ„๋ฉ๋‹ˆ๋‹ค.) ๋“ฑ๋กํ•œ ๊ด‘๊ณ ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ด‘๊ณ ๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ €์ž๊ฐ€ ๋“ฑ๋กํ•œ ๊ด‘๊ณ ์™€ ์œ„ํ‚ค๋…์Šค์˜ ๊ด‘๊ณ ๊ฐ€ 9 ๋Œ€ 1์˜ ๋น„์œจ๋กœ ๋ฒˆ๊ฐˆ์•„ ๊ฐ€๋ฉด์„œ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. (2017๋…„ 5์›”๋ถ€ํ„ฐ) ํ‹ฐ์Šคํ† ๋ฆฌ์—์„œ ์• ๋“œ์„ผ์Šค ๊ณ„์ •์„ ์Šน์ธ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์Šน์ธ๋œ ์• ๋“œ์„ผ์Šค ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ €์žฅํ•ด๋„ ๊ด‘๊ณ ๊ฐ€ ํ‘œ์‹œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•˜์—ฌ ์• ๋“œ์„ผ์Šค ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”. https://wikidocs.net/151142 ์œ„ํ‚ค๋…์Šค์—์„œ ์ž‘์„ฑํ•œ ์ฑ…์„ ์ถœํŒํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ, ์ถœํŒ์‚ฌ์—์„œ ์œ„ํ‚ค๋…์Šค์˜ ๊ธ€์„ ๋ชจ๋‘ ๋‚ด๋ฆฌ๊ธฐ๋ฅผ ์š”๊ตฌํ•˜๋Š” ์ƒํ™ฉ์ด ์˜ค๋ฉด ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”? ์œ„ํ‚ค๋…์Šค์— ๊ฒŒ์‹œ๋œ ๊ธ€์€ ์œ„ํ‚ค๋…์Šค์— ์–ด๋– ํ•œ ์†Œ์œ ๊ถŒ์ด๋‚˜ ์ €์ž‘๊ถŒ๋„ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ €์ž๋Š” ์œ„ํ‚ค๋…์Šค์˜ ๊ธ€์„ ์‚ญ์ œ ๋˜๋Š” ๋น„๊ณต๊ฐœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์—์„œ ์ „์ž์ฑ…์„ ํŒ๋งคํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ํ˜„์žฌ ๋ช‡ ๊ฐ€์ง€ ๊ธฐ์ˆ ์ ์ธ ๋ฌธ์ œ๋กœ ์‹ ๊ทœ ์ „์ž์ฑ… ๋ฐœ๊ฐ„์„ ์ค‘์ง€ํ•œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜๋ฉด ๊ณต์ง€๋ฅผ ํ†ตํ•ด ๋‹ค์‹œ ์•Œ๋ ค๋“œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ํŽ˜์ด์ง€๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”. https://wikidocs.net/150 ์œ„ํ‚ค๋…์Šค ์•ฑ์ด ์žˆ๋‚˜์š”? ์•ˆ๋“œ๋กœ์ด๋“œ, ์•„์ดํฐ ์•ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. (๊ฐค๋Ÿญ์‹œ ํƒญ์ด๋‚˜ ์•„์ดํŒจ๋“œ ๋“ฑ์—์„œ๋„ ์ž˜ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.) ์œ„ํ‚ค๋…์Šค ์•ฑ ์ž์„ธํžˆ ๋ณด๊ธฐ ์œ„ํ‚ค๋…์Šค์—๋Š” OpenAPI๊ฐ€ ์žˆ๋‚˜์š”? ๋„ค, ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค API ์ž์„ธํžˆ ๋ณด๊ธฐ ์ฑ… ํ‘œ์ง€ ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋‚˜์š”? ์ฑ… ํ‘œ์ง€์˜ ์ด๋ฏธ์ง€๋Š” 100 x 130 ๋น„์œจ๋กœ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”. ์œ„ํ‚ค๋…์Šค์—์„œ ์ „์ž์ฑ…์„ ๊ตฌ์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‚ด๋ ค๋ฐ›์€ ์ „์ž์ฑ…์„ ๋ถ„์‹คํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌด๋ฃŒ๋กœ ์ „์ž์ฑ… ์žฌ๋ฐœ์†ก์ด ๊ฐ€๋Šฅํ•œ๊ฐ€์š”? ์œ„ํ‚ค๋…์Šค์—์„œ ํŒ๋งคํ•œ ์ „์ž์ฑ…์€ ์ตœ๋Œ€ 3ํšŒ๊นŒ์ง€ ๋ฌด๋ฃŒ๋กœ ์žฌ๋ฐœ์†ก ํ•ด๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ URL์—์„œ ์ง์ ‘ ์žฌ๋ฐœ์†ก ์‹ ์ฒญ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. https://wikidocs.net/buy/gbook/ ์œ„ํ‚ค๋…์Šค ๊ณ„์ • ํƒˆํ‡ด๋ฅผ ์›ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ๊ณ„์ • ํƒˆํ‡ด๋Š” ํ˜„์žฌ ์‚ฌ์šฉ์ž ๊ธฐ๋Šฅ(ํƒˆํ‡ด ๋ฉ”๋‰ด ๋“ฑ)์œผ๋กœ ์ œ๊ณตํ•˜๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ์ด๋ฉ”์ผ๋กœ ํƒˆํ‡ด๋ฅผ ํฌ๋งํ•œ๋‹ค๋Š” ๋ฉ”์ผ์„ ์ฃผ์‹œ๋ฉด 24์‹œ๊ฐ„ ๋‚ด๋กœ ํƒˆํ‡ด ์ฒ˜๋ฆฌํ•ด ๋“œ๋ฆฝ๋‹ˆ๋‹ค. <EMAIL> ์ฑ…์„ ๊ณต๊ฐœํ•ด๋„ ํ‘œ์‹œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ํ™ˆ์˜ "๊ณต๊ฐœ" ํƒญ์—๋Š” ๊ณต๊ฐœ ์ฑ…๋“ค์ด ๋…ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ตœ์†Œ 5ํŽ˜์ด์ง€ ์ด์ƒ์˜ ๋ถ„๋Ÿ‰์„ ์ง€๋‹Œ ์ฑ…๋งŒ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ŠคํŒธ ์ €์ž(๊ด‘๊ณ  ๋ฐ ์‹ ๊ณ  ์ ‘์ˆ˜ ๋“ฑ)๋กœ ๋“ฑ๋ก๋œ ์ €์ž์˜ ์ฑ…์ธ ๊ฒฝ์šฐ์—๋„ ํ‘œ์‹œ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™ธ์˜ ๊ฒฝ์šฐ์— ๋ณธ์ธ์˜ ์ฑ…์ด ๊ณต๊ฐœ ์ฑ…์— ๋…ธ์ถœ๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ <EMAIL>์œผ๋กœ ์—ฐ๋ฝ ์ฃผ์‹œ๋ฉด ์กฐ์น˜ํ•ด ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. 03 ์œ„ํ‚ค๋…์Šค ๊ฐ€์ด๋“œ ์œ„ํ‚ค๋…์Šค์˜ ์ €์ž๊ฐ€ ๋˜์–ด ์ฑ…์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•๊ณผ ํŽธ์ง‘๊ธฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 03-1 ์ฑ… ๋งŒ๋“ค๊ธฐ ์œ„ํ‚ค๋…์Šค์—์„œ ์ฑ…์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. 01) ๋กœ๊ทธ์ธ (ํšŒ์›๊ฐ€์ž…) ๋กœ๊ทธ์ธ ํšŒ์›๊ฐ€์ž… ๋กœ๊ทธ์ธ ์œ„ํ‚ค๋…์Šค ํ™ˆํŽ˜์ด์ง€์— ์ ‘์†ํ•˜์—ฌ ์šฐ์ธก ์ƒ๋‹จ์˜ ๋กœ๊ทธ์ธ ๋งํฌ๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธ ํ™”๋ฉด์—์„œ ๊ตฌ๊ธ€ ๋˜๋Š” ํŽ˜์ด์Šค๋ถ ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธํ•ฉ๋‹ˆ๋‹ค. ํšŒ์›๊ฐ€์ž… ๋˜๋Š” "๊ณ„์ •์„ ๋งŒ๋“œ์„ธ์š”"๋ฅผ ํด๋ฆญํ•˜์—ฌ "์ด๋ฉ”์ผ + ๋น„๋ฐ€๋ฒˆํ˜ธ" ํ˜•ํƒœ์˜ ๊ณ„์ •์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณ„์ •์„ ์‹ ๊ทœ๋กœ ์ƒ์„ฑํ•  ๊ฒฝ์šฐ, ๊ตฌ๊ธ€์ด๋‚˜ ํŽ˜์ด์Šค๋ถ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฉ”์ผ ์ฃผ์†Œ๋กœ ๊ฐ€์ž…ํ–ˆ๋‹ค๋ฉด ๊ตฌ๊ธ€ ๋˜๋Š” ํŽ˜์ด์Šค๋ถ์œผ๋กœ ๋กœ๊ทธ์ธ ์‹œ ์„œ๋กœ ๋‹ค๋ฅธ ๊ณ„์ •์ด ์•„๋‹Œ ๋‹จ์ผ ๊ณ„์ •์œผ๋กœ ์ทจ๊ธ‰๋œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•ด ์ฃผ์„ธ์š”. (๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๋กœ๊ทธ์ธํ•˜๋”๋ผ๋„ ๋™์ผํ•œ ์‚ฌ์šฉ์ž๋กœ ์ ‘์†๋จ) 02) ์ƒˆ ์ฑ… ๋งŒ๋“ค๊ธฐ ์ด๋ฒˆ์—๋Š” ์œ„ํ‚ค๋…์Šค์—์„œ ์ƒˆ ์ฑ…์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ณ„์ • ์„ค์ • ์ƒˆ ์ฑ… ๋งŒ๋“ค๊ธฐ ํŽธ์ง‘ ํ™”๋ฉด ๊ณ„์ • ์„ค์ • ์œ„ํ‚ค๋…์Šค์— ๋กœ๊ทธ์ธํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒ๋‹จ ๋ฉ”๋‰ด๊ฐ€ ๋ณด์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ "๊ณ„์ • ์„ค์ •" ๋งํฌ๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ ์ฑ… ๋งŒ๋“ค๊ธฐ "๋‚˜์˜ ์ฑ…" -> "์ƒˆ ์ฑ… ๋งŒ๋“ค๊ธฐ"๋ฅผ ์„ ํƒํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ฑ…์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํŽธ์ง‘ ํ™”๋ฉด ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒˆ๋กœ์šด ์ฑ…์ด ์ƒ์„ฑ๋˜๊ณ  ์ฑ…์„ ํŽธ์ง‘ํ•˜๋Š” ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. 03) ์ฑ… ์ˆ˜์ •, ํŽ˜์ด์ง€ ์ˆ˜์ • ์œ„ํ‚ค๋…์Šค์—์„œ ์ฑ…์„ ์ˆ˜์ •ํ•  ๋•Œ๋Š” "์ฑ… ์ˆ˜์ •", "ํŽ˜์ด์ง€ ์ˆ˜์ •" ๋‘ ๊ฐ€์ง€ ํŽธ์ง‘๋ชจ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐ€์ง€ ํŽธ์ง‘ ๋ชจ๋“œ์— ๋Œ€ํ•ด์„œ ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฑ… ์ˆ˜์ • ๊ธฐ๋ณธ ์ €์ž‘๊ถŒ ๊ณต๋™ ์ €์ž ๊ด‘๊ณ  ๊ธฐํƒ€ ์ฑ… ๋ฐฑ์—… ์ฑ… ์‚ญ์ œ ํŽ˜์ด์ง€ ์ˆ˜์ • ์ฑ… ์ˆ˜์ • ์ฑ… ์ˆ˜์ •์€ ์ฑ…์— ๋Œ€ํ•ด ์„ค์ •ํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ํŽธ์ง‘๊ธฐ ํ™”๋ฉด์—์„œ ์ขŒ์ธก์˜ ๋ชฉ์ฐจ ์ค‘ ๊ฐ€์žฅ ์ƒ๋‹จ์˜ ์ฑ… ์ œ๋ชฉ์— ํ•ด๋‹นํ•˜๋Š” ํ•ญ๋ชฉ์„ ์„ ํƒํ•˜๋ฉด ์œ„์™€ ๊ฐ™์€ ์ฑ… ์ˆ˜์ • ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ๋ชฉ์ฐจ ์ค‘ ๊ฐ€์žฅ ์ƒ๋‹จ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ํ•ญ๋ชฉ์„ ์„ ํƒํ•  ๊ฒฝ์šฐ์—๋Š” ํŽ˜์ด์ง€ ์ˆ˜์ • ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๊ธฐ๋ณธ ํƒญ์€ ์ฑ…์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌํ•ญ์„ ์„ค์ •ํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ์ฑ… ์ˆ˜์ •์˜ ๊ธฐ๋ณธ ํƒญ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•ญ๋ชฉ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ… ์ œ๋ชฉ : ์ฑ… ์ œ๋ชฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฑ… ์ด๋ฏธ์ง€ : ์ฑ…์˜ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ์—…๋กœ๋“œํ•˜์—ฌ ์ €์žฅํ•œ ํ›„์—๋Š” ๋„ค๋ชจ ๋ฐ•์Šค๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€์˜ ํŠน์ • ์˜์—ญ์„ ์„ ํƒํ•˜์—ฌ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณต๊ฐœ/๋น„๊ณต๊ฐœ : ์ฑ…์„ ๊ณต๊ฐœ ๋˜๋Š” ๋น„๊ณต๊ฐœ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ฑ… ์š”์•ฝ : ์ฑ…์— ๋Œ€ํ•œ ์„ค๋ช…๊ณผ ๊ฐœ์š”๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ… ์š”์•ฝ์˜ ๋‚ด์šฉ์€ ๋งˆํฌ๋‹ค์šด ๋ฌธ๋ฒ•์œผ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋งˆํฌ๋‹ค์šด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋งˆํฌ๋‹ค์šด ํŽ˜์ด์ง€๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. ์ €์ž‘๊ถŒ ์ €์ž‘๊ถŒ ํƒญ์€ ์ด ์ฑ…์˜ ์ €์ž‘๊ถŒ์„ ์„ค์ •ํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ์ €์ž‘๊ถŒ์€ CCL(Creative Commons License) ๋ฐฉ์‹๊ณผ ์ง์ ‘ ์ž…๋ ฅ ๋ฐฉ์‹์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CCL ๋ฐฉ์‹์€ ์œ„ ํ™”๋ฉด๊ณผ ๊ฐ™์ด ์„ ํƒํ•˜์—ฌ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง์ ‘ ์ž…๋ ฅ ๋ฐฉ์‹์€ ๋ผ์ด์„ ์Šค ๋ช…๊ณผ ๋ผ์ด์„ ์Šค์— ๋Œ€ํ•ด์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” URL์„ ์ž…๋ ฅํ•˜์—ฌ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณต๋™ ์ €์ž ๊ณต๋™ ์ €์ž ํƒญ์€ ๊ณต๋™ ์ €์ž๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ๋กœ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๊ณต๋™ ์ €์ž๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋นจ๊ฐ„์ƒ‰์€ ์ด ์ฑ…์˜ ๋ฉ”์ธ ์ €์ž๋ฅผ ์˜๋ฏธํ•˜๊ณ  ํŒŒ๋ž€์ƒ‰์€ ์ด ์ฑ…์˜ ๊ณต๋™ ์ €์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ณต๋™ ์ €์ž๋Š” ์—ฌ๋Ÿฌ ๋ช…์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ํ•œ ๊ณต๋™ ์ €์ž๋ฅผ ์ทจ์†Œํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ํŒŒ๋ž€์ƒ‰์œผ๋กœ ์ถ”๊ฐ€๋œ ๊ณต๋™ ์ €์ž์˜ ์šฐ์ธก์˜ "x" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ๊ณต๋™ ์ €์ž๋ฅผ ์ทจ์†Œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด‘๊ณ  ๊ด‘๊ณ  ํƒญ์€ ๊ด‘๊ณ ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•ญ๋ชฉ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด‘๊ณ ๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๊นŒ? : ๊ด‘๊ณ ๋ฅผ ํ‘œ์‹œํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๊นŒ? : ์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ ๋ฅผ ์‚ฌ์šฉํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ด‘๊ณ  ๋‚ด์šฉ(PC, ํ•˜๋‹จ) : ์œ„ํ‚ค๋…์Šค๋ฅผ PC๋กœ ์ ‘์†ํ–ˆ์„ ๊ฒฝ์šฐ ํ•˜๋‹จ์— ํ‘œ์‹œํ•  ๊ด‘๊ณ  ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ ๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค. ๊ด‘๊ณ  ๋‚ด์šฉ(PC, ์šฐ์ƒ๋‹จ ๊ณ ์ •) : ์œ„ํ‚ค๋…์Šค๋ฅผ PC๋กœ ์ ‘์†ํ–ˆ์„ ๊ฒฝ์šฐ ์šฐ์ธก ์ƒ๋‹จ์— ํ‘œ์‹œํ•  ๊ด‘๊ณ  ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ ๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค(์ด ๊ด‘๊ณ ๋Š” ํ•ญ์ƒ ๊ณ ์ •๋˜์–ด ํ‘œ์‹œ๋˜๋Š” ๊ด‘๊ณ ์ž…๋‹ˆ๋‹ค). ๊ด‘๊ณ  ๋‚ด์šฉ(๋ชจ๋ฐ”์ผ) : ์œ„ํ‚ค๋…์Šค๋ฅผ ์Šค๋งˆํŠธํฐ์œผ๋กœ ์ ‘์†ํ–ˆ์„ ๊ฒฝ์šฐ์— ํ‘œ์‹œํ•  ๊ด‘๊ณ  ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ ๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค(ํŽ˜์ด์ง€ ํ•˜๋‹จ์— ๊ด‘๊ณ ๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค). ๊ด‘๊ณ ์— ๋Œ€ํ•œ ๋ณด๋‹ค ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ๊ด‘๊ณ  - https://wikidocs.net/9346 ๊ธฐํƒ€ ๊ธฐํƒ€ ํƒญ์€ ์—”ํ„ฐํ‚ค ์ค„ ๋ฐ”๊ฟˆ, ์—ญ์ˆœ ์ •๋ ฌ ๋“ฑ์„ ์„ค์ •ํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•ญ๋ชฉ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—”ํ„ฐํ‚ค ์ค„ ๋ฐ”๊ฟˆ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๊นŒ? : ์œ„ํ‚ค๋…์Šค๋Š” ๊ธ€ ์ž‘์„ฑ ์‹œ ์—”ํ„ฐํ‚ค๋ฅผ ์ž…๋ ฅํ•˜๋”๋ผ๋„ ์ค„ ๋ฐ”๊ฟˆ์ด ์ด๋ฃจ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งˆํฌ๋‹ค์šด์€ ์ค„ ๋ฐ”๊ฟˆ์„ ์œ„ํ•ด ์ค„์˜ ๋งˆ์ง€๋ง‰์— ์ŠคํŽ˜์ด์Šค ํ‚ค๋ฅผ 2๋ฒˆ ๊ฐ•์ œ๋กœ ์ž…๋ ฅํ•ด์•ผ๋งŒ ์ค„ ๋ฐ”๊ฟˆ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ํ•ญ๋ชฉ์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•˜๋ฉด ์—”ํ„ฐํ‚ค๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์ž๋™์œผ๋กœ ์ค„ ๋ฐ”๊ฟˆ์ด ๋ฉ๋‹ˆ๋‹ค. ์—ญ์ˆœ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๊นŒ? : ์œ„ํ‚ค๋…์Šค์˜ ๋ชฉ์ฐจ๋Š” ์ œ๋ชฉ์„ ๊ธฐ์ค€์œผ๋กœ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ์ด ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ํ•ญ๋ชฉ์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•˜๋ฉด ์ œ๋ชฉ์˜ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ๋ธ”๋กœ๊ทธ์™€ ๊ฐ™์ด ์‹œ๊ฐ„์˜ ์—ญ์ˆœ์œผ๋กœ ํ‘œ์‹œํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์„œ์˜ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ… ๋ฐฑ์—… ์ฑ… ๋ฐฑ์—… ํƒญ์€ ์ฑ…์„ ๋ฐฑ์—…ํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ์ฑ…์˜ ๋‚ด์šฉ๊ณผ ์ด๋ฏธ์ง€๋ฅผ ์ „๋ถ€ ๋‚ด๋ ค๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๋ ค๋ฐ›์€ ํŒŒ์ผ์€ zip์œผ๋กœ ์••์ถ•๋˜์–ด ์žˆ์œผ๋ฉฐ ์ฑ…์˜ ๋‚ด์šฉ์€ ๋งˆํฌ๋‹ค์šด ํ˜•ํƒœ๋กœ, ์ด๋ฏธ์ง€๋Š” ํ•ด๋‹น ํŽ˜์ด์ง€๋ณ„๋กœ ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ… ์‚ญ์ œ ์ฑ… ์‚ญ์ œ ํƒญ์€ ์ฑ…์„ ์‚ญ์ œํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ์ฑ…์„ ์‚ญ์ œํ•  ๋•Œ๋Š” ์ฑ…์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฑ… ์ด๋ฆ„์ด ์ผ์น˜ํ•  ๊ฒฝ์šฐ์—๋งŒ ์ฑ…์ด ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค. ์ฑ…์€ ํ•œ๋ฒˆ ์‚ญ์ œํ•˜๋ฉด ๋˜๋Œ๋ฆด ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฑ…์„ ์‚ญ์ œํ•˜๊ธฐ ์ „์— ์ฑ…์„ ๋ฐฑ์—…ํ•œ ํ›„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ์ˆ˜์ • ํŽธ์ง‘ ํ™”๋ฉด์˜ ๋ชฉ์ฐจ์—์„œ ๊ฐ€์žฅ ์ƒ๋‹จ์„ ์ œ์™ธํ•œ ๋ชฉ์ฐจ๋Š” ๋ชจ๋‘ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. ๋ชฉ์ฐจ ์ค‘ ํŽ˜์ด์ง€๋ฅผ ์„ ํƒํ•  ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ์ˆ˜์ • ํ™”๋ฉด์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•ญ๋ชฉ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ์ œ๋ชฉ : ํŽ˜์ด์ง€์˜ ์ œ๋ชฉ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ชฉ์ฐจ๋Š” ํŽ˜์ด์ง€์˜ ์ œ๋ชฉ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ผ์ •ํ•œ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํŽ˜์ด์ง€์˜ ์ œ๋ชฉ์„ ์ˆซ์ž๋‚˜ ๊ธฐํ˜ธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒŒ ํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ถ€๋ชจ ํŽ˜์ด์ง€ : ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ๋ถ€๋ชจ ํŽ˜์ด์ง€๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ถ€๋ชจ ํŽ˜์ด์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋ถ€๋ชจ ํŽ˜์ด์ง€์˜ ํ•˜์œ„ ๋ชฉ์ฐจ๋กœ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ๋ถ€๋ชจ ํŽ˜์ด์ง€๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ์ตœ์ƒ๋‹จ์˜ ๋ชฉ์ฐจ๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๊ณต๊ฐœ/๋น„๊ณต๊ฐœ : ์ด ํŽ˜์ด์ง€์˜ ๊ณต๊ฐœ ์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•˜์œ„ ํŽ˜์ด์ง€๊ฐ€ ์žˆ์„ ๋•Œ, ๋น„๊ณต๊ฐœ๋กœ ์„ค์ •ํ•˜๋ฉด ํ•˜์œ„ ํŽ˜์ด์ง€๋“ค๋„ ๋ชจ๋‘ ๋น„๊ณต๊ฐœ๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ : ์ด ํŽ˜์ด์ง€์— ์‚ฝ์ž…ํ•  ์ด๋ฏธ์ง€๋ฅผ ์ฒจ๋ถ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋“œ๋ž˜๊ทธ ์•ค๋“œ ๋“œ๋กญ๊ณผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ์„ ๋™์‹œ์— ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ํ›„์— ๋“ฑ๋ก๋œ ์ด๋ฏธ์ง€์˜ ์šฐ์ธก์— ํ‘œ์‹œ๋œ Action ์•„์ด์ฝ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ญ์ œ, ๋ณต์‚ฌ, ์ ์šฉ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ๋‚ด์šฉ : ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค. ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์€ ๋งˆํฌ๋‹ค์šด ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋งˆํฌ๋‹ค์šด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋งˆํฌ๋‹ค์šด ํŽ˜์ด์ง€์—์„œ ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ์ˆ˜์ • ํ™”๋ฉด์˜ ๊ฐ€์žฅ ํ•˜๋‹จ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์žฅ : ํŽ˜์ด์ง€๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋ณต์ œ : ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ํŽ˜์ด์ง€๋ฅผ ์‹ ๊ทœ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์ œ๋ชฉ์—๋Š” "Copy"๋ผ๋Š” ๋ฌธ์žฅ์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์‚ญ์ œ : ํŽ˜์ด์ง€๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ์‚ญ์ œํ•œ ํŽ˜์ด์ง€๋Š” ๋ณต๊ตฌํ•  ์ˆ˜ ์—†์œผ๋‹ˆ ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 04) ์ฑ… ๊ฐ€์ ธ์˜ค๊ธฐ ๋งˆํฌ๋‹ค์šด ํŒŒ์ผ์„ ํฌํ•จํ•˜๋Š” zip ํŒŒ์ผ์„ ์˜ฌ๋ ค ์ฑ…์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. zip ํŒŒ์ผ ๋‚ด์—๋Š” ๋ฐ˜๋“œ์‹œ. md ํŒŒ์ผ์ด ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋Œ€ํ‘œ์ ์ธ ํŒŒ์ผ์˜ ์ข…๋ฅ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์—์„œ ๋ฐฑ์—…ํ•œ ์ฑ… ํŒŒ์ผ ๊นƒํ—ˆ๋ธŒ์—์„œ ๋‚ด๋ ค๋ฐ›์€ ํŒŒ์ผ ๋…ธ์…˜์—์„œ ๋งˆํฌ๋‹ค์šด์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ ํ•œ ํŒŒ์ผ ์œ„ํ‚ค๋…์Šค์— ๋กœ๊ทธ์ธํ•œ ํ›„ [๊ณ„์ • ์„ค์ • -> ์ฑ… ๊ฐ€์ ธ์˜ค๊ธฐ] ๋ฉ”๋‰ด์—์„œ ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฆด ํŒŒ์ผ์„ ์„ ํƒํ•˜๊ณ  ์ฑ… ์ œ๋ชฉ์„ ์ž…๋ ฅํ•œ ํ›„ "์ฑ… ๊ฐ€์ ธ์˜ค๊ธฐ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฉ๋‹ˆ๋‹ค. 03-2 ๋งˆํฌ๋‹ค์šด ์œ„ํ‚ค๋…์Šค๋Š” ๋งˆํฌ๋‹ค์šด์„ ๊ธฐ๋ณธ ํŽธ์ง‘๊ธฐ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋งˆํฌ๋‹ค์šด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ URL์„ ์ฐธ์กฐํ•˜์„ธ์š”. http://daringfireball.net/projects/markdown/ http://en.wikipedia.org/wiki/Markdown ์—ฌ๊ธฐ์„œ๋Š” ์œ„ํ‚ค๋…์Šค ํŽธ์ง‘์„ ์œ„ํ•œ ๋งˆํฌ๋‹ค์šด์˜ ๊ธฐ๋ณธ์ ์ธ ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 01) ํ—ค๋”ฉ ํƒœ๊ทธ ๋ฌธ์„œ์— ์†Œ์ œ๋ชฉ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ—ค๋”ฉ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: # H1 ## H2 ### H3 #### H4 ##### H5 ###### H6 ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: H1 H2 H3 H4 H5 H6 ์œ„ํ‚ค๋…์Šค์—์„œ ํ—ค๋”ฉ ํƒœ๊ทธ ์‚ฌ์šฉ ์‹œ H2 ํ—ค๋”ฉ ํƒœ๊ทธ๋ถ€ํ„ฐ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ „์ž์ฑ… ์ƒ์„ฑ ์‹œ H1 ํ—ค๋”ฉ ํƒœ๊ทธ๋Š” ์ฑ•ํ„ฐ๋กœ ์ธ์‹๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ „์ž์ฑ… ํŒ๋งค๋ฅผ ์—ผ๋‘์— ๋‘๊ณ  ๊ณ„์‹ ๋‹ค๋ฉด H2 ํƒœ๊ทธ๋ถ€ํ„ฐ ์‚ฌ์šฉํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. โ€ป H2 ํƒœ๊ทธ์—๋Š” ๋ฐ‘์ค„์„ ๊ธ‹๋„๋ก ๋””์ž์ธํ–ˆ์Šต๋‹ˆ๋‹ค. 02) ์ธ์šฉ๊ตฌ ์ธ์šฉ๋ฌธ์€ ๋‹ค์Œ์ฒ˜๋Ÿผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค(โ€ป ์ธ์šฉ๋ฌธ์€ HTML blockquote ์—˜๋ฆฌ๋จผํŠธ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.) ์ž‘์„ฑ ์˜ˆ: > ์ฒ ์ˆ˜๋Š” ์˜ํฌ๊ฐ€ ์ด์˜๋‹ค๊ณ  ๋งํ–ˆ๋‹ค. ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ์ฒ ์ˆ˜๋Š” ์˜ํฌ๊ฐ€ ์ด์˜๋‹ค๊ณ  ๋งํ–ˆ๋‹ค. 03) ๋ฆฌ์ŠคํŠธ ๋ฆฌ์ŠคํŠธ์—๋Š” ๋‘ ๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆœ์ฐจ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ๊ณผ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒƒ ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ˆœ์ฐจ ํ‘œ์‹œ ์ž‘์„ฑ ์˜ˆ: 1. ํ•˜๋‚˜ 1. ๋‘˜ 1. ์…‹ ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ํ•˜๋‚˜ ์…‹ ์ˆœ์ฐจ ํ‘œ์‹œ ์—†๋Š” ๊ฒƒ ์ž‘์„ฑ ์˜ˆ: * ํ™๊ธธ๋™ * ๊น€์ฒ ์ˆ˜ * ๊น€์˜ํฌ ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ํ™๊ธธ๋™ ๊น€์ฒ ์ˆ˜ ๊น€์˜ํฌ ์ค‘์ฒฉ ํ‘œ์‹œ ์ค‘์ฒฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋“ค์—ฌ ์“ฐ๊ธฐ(์ŠคํŽ˜์ด์Šค 4๊ฐœ)๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: * ์ปคํ”ผ์˜ ์žฅ์  * ๋ง›์žˆ๋‹ค. * ์ž ์„ ๊นจ์šด๋‹ค. * ์ปคํ”ผ์˜ ๋‹จ์  * ์ž ์ด ์•ˆ ์˜จ๋‹ค. * ๊ฑด๊ฐ•์— ํ•ด๋กญ๋‹ค. ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ์ปคํ”ผ์˜ ์žฅ์  ๋ง›์žˆ๋‹ค. ์ž ์„ ๊นจ์šด๋‹ค. ์ปคํ”ผ์˜ ๋‹จ์  ์ž ์ด ์•ˆ ์˜จ๋‹ค. ๊ฑด๊ฐ•์— ํ•ด๋กญ๋‹ค. 04) ๊ฐ•์กฐ ๊ตฌ๋ฌธ ๊ฐ•์กฐ ๊ตฌ๋ฌธ์—๋„ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง„ํ•˜๊ฒŒ ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ๊ณผ ๊ธฐ์šธ์—ฌ์„œ ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ ๋‘ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ง„ํ•˜๊ฒŒ ํ‘œ์‹œ (Bold) ์ž‘์„ฑ ์˜ˆ: ์ „ ๋งค์šฐ **์ฐฉํ•˜๊ฒŒ** ์‚ด๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ์ „ ๋งค์šฐ ์ฐฉํ•˜๊ฒŒ ์‚ด๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์šธ์—ฌ ํ‘œ์‹œ (Italic) ์ž‘์„ฑ ์˜ˆ: ์ „ ๋งค์šฐ *์ฐฉํ•˜๊ฒŒ* ์‚ด๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. (๋˜๋Š” _์ฐฉํ•˜๊ฒŒ_) ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ์ „ ๋งค์šฐ ์ฐฉํ•˜๊ฒŒ ์‚ด๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. (๋˜๋Š” ์ฐฉํ•˜๊ฒŒ) 05) ๋งํฌ์™€ ์ด๋ฏธ์ง€ ๋งํฌ ์ด๋ฏธ์ง€ ํ‘œ์‹œ ์ด๋ฏธ์ง€ ์ •๋ ฌ๊ณผ ์‚ฝ์ž… ์™ผ์ชฝ ์ •๋ ฌ ์™ผ์ชฝ ์‚ฝ์ž… ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ ์˜ค๋ฅธ์ชฝ ์‚ฝ์ž… ๊ฐ€์šด๋ฐ ์ •๋ ฌ ๋งํฌ [๋งํฌ ๋ฌธ๊ตฌ](๋งํฌ ์ฃผ์†Œ) ์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ๋งํฌ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: [์œ„ํ‚ค๋…์Šค](http://wikidocs.net) ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ์œ„ํ‚ค๋…์Šค ์ด๋ฏธ์ง€ ํ‘œ์‹œ ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•˜๋ ค๋ฉด ๋งํฌ ์ฃผ์†Œ ๋Œ€์‹  ์ด๋ฏธ์ง€ ์ฃผ์†Œ๋ฅผ ๊ธฐ์žฌํ•˜๊ณ  ๋งจ ์•ž์—! ๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: ![์œ„ํ‚ค๋…์Šค ๋กœ๊ณ ](http://wikidocs.net/images/book/wikidocs.png) ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ์ด๋ฏธ์ง€ ์ •๋ ฌ๊ณผ ์‚ฝ์ž… ์ด๋ฏธ์ง€๋ฅผ ์ •๋ ฌ ๋˜๋Š” ์‚ฝ์ž…ํ•˜๋ ค๋ฉด img ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์™ผ์ชฝ ์ •๋ ฌ <p align="left"> <img src="https://picsum.photos/100/100"> </p> ์ด๋ฏธ์ง€๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์™€ ๊ฐ™์ด p ํƒœ๊ทธ์™€ img ํƒœ๊ทธ๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์™ผ์ชฝ ์‚ฝ์ž… <img align="left" src="https://picsum.photos/100/100" style="margin-right:10px"> ์ด๋ฏธ์ง€๋ฅผ ํ…์ŠคํŠธ ๋ณธ๋ฌธ ์™ผ์ชฝ์— ์‚ฝ์ž…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์™€ ๊ฐ™์ด img ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. align="left" ์†์„ฑ์„ ์ฃผ๊ณ  ์ด๋ฏธ์ง€ ์šฐ์ธก์—๋Š” ๋งˆ์ง„์„ ์ฃผ์–ด์•ผ ๊น”๋”ํ•˜๊ฒŒ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์šฐ์ธก์œผ๋กœ ์ž‘์„ฑ๋˜๋Š” ํ…์ŠคํŠธ๋Š” ์ด๋ฏธ์ง€์˜ ์„ธ๋กœ ๊ธธ์ด๋งŒํผ ๊ณ„์† ๋“ค์—ฌ์“ฐ๊ธฐ ๋˜์–ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ <p align="right"> <img src="https://picsum.photos/100/100"> </p> ์ด๋ฏธ์ง€๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์™€ ๊ฐ™์ด p ํƒœ๊ทธ์™€ img ํƒœ๊ทธ๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ ์‚ฝ์ž… <img align="right" src="https://picsum.photos/100/100" style="margin-left:10px"> ์ด๋ฏธ์ง€๋ฅผ ํ…์ŠคํŠธ ๋ณธ๋ฌธ ์˜ค๋ฅธ์ชฝ์— ์‚ฝ์ž…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์™€ ๊ฐ™์ด img ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. align="right" ์†์„ฑ์„ ์ฃผ๊ณ  ์ด๋ฏธ์ง€ ์ขŒ์ธก์—๋Š” ๋งˆ์ง„์„ ์ฃผ์–ด์•ผ ๊น”๋”ํ•˜๊ฒŒ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์ขŒ์ธก์œผ๋กœ ์ž‘์„ฑ๋˜๋Š” ํ…์ŠคํŠธ๋Š” ์ด๋ฏธ์ง€์˜ ์„ธ๋กœ ๊ธธ์ด๋งŒํผ ๊ณ„์† ๋“ค์—ฌ์“ฐ๊ธฐ ๋˜์–ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€์šด๋ฐ ์ •๋ ฌ <p align="center"> <img src="https://picsum.photos/460/300"> </p> ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์šด๋ฐ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์™€ ๊ฐ™์ด p ํƒœ๊ทธ์™€ img ํƒœ๊ทธ๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 06) ๊ฐ•์ œ ์ค„ ๋ฐ”๊ฟˆ ๋งˆํฌ๋‹ค์šด ์—๋””ํ„ฐ์—์„œ ์—”ํ„ฐํ‚ค๋ฅผ ์ด์šฉํ•˜์—ฌ ์ค„ ๋ฐ”๊ฟˆ์„ ํ•˜๋”๋ผ๋„ ์‹ค์ œ ๋ณด์ด๋Š” ํ™”๋ฉด์—์„œ๋Š” ์ค„ ๋ฐ”๊ฟˆ์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ•์ œ๋กœ ์ค„ ๋ฐ”๊ฟˆ์„ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์ค„ ๋์— ๊ณต๋ฐฑ(space)์„ ๋‘ ๊ฐœ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฑ… ์ˆ˜์ • ํ™”๋ฉด์—์„œ "์—”ํ„ฐํ‚ค ์ค„ ๋ฐ”๊ฟˆ" ๊ธฐ๋Šฅ์„ "์‚ฌ์šฉํ•จ"์œผ๋กœ ์„ค์ •ํ•œ ๊ฒฝ์šฐ์—๋Š” ์—”ํ„ฐํ‚ค๋กœ๋„ ์ค„ ๋ฐ”๊ฟˆ์ด ๋ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: ("์ฒซ ๋ฒˆ์งธ ์ค„" ๋’ค์— ์ŠคํŽ˜์ด์Šค 2๊ฐœ๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ์žˆ์Œ) ์ฒซ ๋ฒˆ์งธ ์ค„ ๋‘ ๋ฒˆ์งธ ์ค„ ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ์ฒซ ๋ฒˆ์งธ ์ค„ ๋‘ ๋ฒˆ์งธ ์ค„ 07) ์ฝ”๋“œ ๋ธ”๋ก ์ฝ”๋“œ ๋ธ”๋ก์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‚ฝ์ž…๋œ ์ฝ”๋“œ๋Š” ๋ณด๊ธฐ ์ข‹๊ฒŒ(Syntax Highlighting) ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ ๋ธ”๋ก Back Quote(`) ๋ฌธ์ž ์„ธ ๊ฐœ(```)๋ฅผ ์ฝ”๋“œ์˜ ์œ„์•„๋ž˜์— ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: ``` def sum(a, b): return a+b ``` ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: def sum(a, b): return a+b ์ž๋™์œผ๋กœ ์–ธ์–ด๋ฅผ ํŒ๋ณ„ํ•˜์—ฌ ์‹ ํƒ์Šค ๊ฐ•์กฐ๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์–ธ์–ด ์ง€์ • ์ฝ”๋“œ ๋ธ”๋ก (์ถ”์ฒœ) ๋˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ์— ํŠน์ • ์–ธ์–ด(์˜ˆ:python)๋ฅผ ์ง€์ •ํ•˜์—ฌ ์ฝ”๋“œ ๋ธ”๋ก์„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Back Quote(`) ๋ฌธ์ž ์„ธ ๊ฐœ์™€ ์ฝ”๋“œ์˜ ์–ธ์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. (์˜ˆ:```python) ๋ธ”๋ก์„ ๋‹ซ์„ ๋•Œ๋Š” ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Back Quote(`) ๋ฌธ์ž ์„ธ ๊ฐœ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. python์˜ ์˜ˆ ```python def sum(a, b): return a+b ``` ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: def sum(a, b): return a+b java์˜ ์˜ˆ ```java class Test { public static void main(String[] args) { System.out.pritnln("hello world"); } } ``` ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: class Test { public static void main(String[] args) { System.out.pritnln("hello world"); } } python, java์™€ ๊ฐ™์€ ๊ฐ’์„ ์ง€์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ ์–ธ์–ด๊ฐ€ ์„ ํƒ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ Syntax Highlighting์ด ์ง€์›๋˜๋Š” ์–ธ์–ด(Language)๋“ค์ž…๋‹ˆ๋‹ค. ๋งˆํฌ๋‹ค์šด ์—๋””ํ„ฐ์—์„œ๋Š” ๊ด„ํ˜ธ ์•ˆ์˜ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Bash (bash) C# (cs) C++ (cpp) CSS (css) Diff (diff) HTML, XML (html) HTTP (http) Ini (ini) JSON (json) Java (java) JavaScript (javascript) PHP (php) Perl (perl) Python (python) Ruby (ruby) SQL (sql) Dart (dart) ๋” ๋งŽ์€ ์–ธ์–ด ์ฝ”๋“œ ๋งŒ์•ฝ ์œ„์— ์–ธ๊ธ‰ํ•œ ์–ธ์–ด ์™ธ์— ์ฝ”๋“œ ๋ธ”๋ก์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŽ˜์ด์ง€ ์ƒ๋‹จ์— ํ•ด๋‹น ์–ธ์–ด์— ๋Œ€ํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [์˜ˆ: verilog ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ] <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/languages/verilog.min.js"></script> ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ```verilog for ( int count = 0; count < 3; count++ ) value = value +((a[count]) * (count+1)); for ( int count = 0, done = 0, j = 0; j * count < 125; j++, count++) $display("Value j = %d\n", j); ``` ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: for ( int count = 0; count < 3; count++ ) value = value +((a[count]) * (count+1)); for ( int count = 0, done = 0, j = 0; j * count < 125; j++, count++) $display("Value j = %d\n", j); ๋” ๋งŽ์€ ์–ธ์–ด ์ฝ”๋“œ๋Š” ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md ์œ„ ์‚ฌ์ดํŠธ์˜ ์–ธ์–ด ์ฝ”๋“œ ํ‘œ์—์„œ aliases์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/languages/{alias}.min.js"></script> plaintext ์ฝ”๋“œ ๋ธ”๋ก ๋งŒ์•ฝ ์ฝ”๋“œ ๋ธ”๋ก์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€๋งŒ ์‹ ํƒ์Šค ๊ฐ•์กฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด plaintext๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ```plaintext class Test { public static void main(String[] args) { System.out.pritnln("hello world"); } } ``` ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: class Test { public static void main(String[] args) { System.out.pritnln("hello world"); } } ์ธ๋ผ์ธ ์ฝ”๋“œ ๋ธ”๋ก ์ฝ”๋“œ๋Š” ๋Œ€๋ถ€๋ถ„ 1์ค„ ์ด์ƒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์ง€๋งŒ ์งค๋ง‰ํ•œ ์ฝ”๋“œ ๋ฌธ์žฅ์„ ํ•œ ๋ฌธ์žฅ ๋‚ด์— ์‚ฝ์ž…ํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ๋„ ์žˆ๊ฒ ์ง€์š”? ์ด๋Ÿฐ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ธ๋ผ์ธ ์ฝ”๋“œ ๋ฌธ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ฒ˜๋Ÿผ ์ฝ”๋“œ ๋ถ€๋ถ„์„ ` (back quote) ๋ฌธ์ž๋กœ ๊ฐ์‹ธ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: ํ”„๋กœ๊ทธ๋žจ ์ˆ˜ํ–‰ ์ค‘ `return a+b`๋ผ๋Š” ๋ฌธ์žฅ์„ ๋งŒ๋‚˜๋ฉด ๊ฒฐ๊ด๊ฐ’์ด ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค. ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ํ”„๋กœ๊ทธ๋žจ ์ˆ˜ํ–‰ ์ค‘ return a+b๋ผ๋Š” ๋ฌธ์žฅ์„ ๋งŒ๋‚˜๋ฉด ๊ฒฐ๊ด๊ฐ’์ด ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค. 08) ๋งˆํฌ๋‹ค์šด extension ์œ„ํ‚ค๋…์Šค๋Š” ์ผ๋ฐ˜ ๋งˆํฌ๋‹ค์šด ๊ธฐ๋Šฅ์— ์ถ”๊ฐ€๋กœ ๋ช‡ ๊ฐ€์ง€ ๊ธฐ๋Šฅ๋“ค์„ ์ถ”๊ฐ€๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. โ€ป ๋‹ค๋ฅธ ๋งˆํฌ๋‹ค์šด ์—๋””ํ„ฐ์—์„œ๋Š” ์•„๋ž˜์˜ ๊ธฐ๋Šฅ์ด ๋™์ž‘ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” ์ž‘์„ฑ ์˜ˆ: head1 | head2 ------|------- hello | foo hi | bar ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: head1 head2 hello foo hi bar (โ€ป ํ…Œ์ด๋ธ” ๋‚ด์— ํŒŒ์ดํ”„ ๋ฌธ์ž ํ‘œ์‹œํ•˜๋ ค๋ฉด |๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.) ํ…Œ์ด๋ธ” ์ •๋ ฌ ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ์„ ์ขŒ์ธก, ์šฐ์ธก ๋˜๋Š” ๊ฐ€์šด๋ฐ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ขŒ์ธก ์ •๋ ฌ์ธ ๊ฒฝ์šฐ ํ…Œ์ด๋ธ”์˜ ํ—ค๋”์™€ ๋ฐ์ดํ„ฐ์˜ ๊ตฌ๋ถ„์„ ์„ :------ ์™€ ๊ฐ™์ด : ์„ ๊ตฌ๋ถ„ ์„ ์˜ ์ขŒ์ธก์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์ธก ์ •๋ ฌ์ธ ๊ฒฝ์šฐ์—๋Š” ------:, ๊ฐ€์šด๋ฐ ์ •๋ ฌ์ธ ๊ฒฝ์šฐ์—๋Š” :------:๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: ์ขŒ์ธก ์ •๋ ฌ | ์šฐ์ธก ์ •๋ ฌ | ๊ฐ€์šด๋ฐ ์ •๋ ฌ :------ |------:|:------: ์—ฐํ•„ | 500 | Yes ์ปคํ”ผ | 9000 | No ํฌ๋ ˆํŒŒ์Šค | 15000 | Yes ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ์ขŒ์ธก ์ •๋ ฌ ์šฐ์ธก ์ •๋ ฌ ๊ฐ€์šด๋ฐ ์ •๋ ฌ ์—ฐํ•„ 500 Yes ์ปคํ”ผ 9000 No ํฌ๋ ˆํŒŒ์Šค 15000 Yes ๊ฐ์ฃผ ๊ฐ์ฃผ๋ž€ ๋ณธ๋ฌธ์˜ ์–ด๋–ค ๋ถ€๋ถ„์„ ์„ค๋ช…ํ•˜๊ฑฐ๋‚˜ ๋ณด์ถฉํ•˜๊ธฐ ์œ„ํ•ด ๋ณธ๋ฌธ ์•„๋ž˜์ชฝ์— ๋ณ„๋„๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์„ค๋ช…๋ฌธ์œผ๋กœ์„œ ์ฃผ๋กœ ๋‚ด์šฉ์˜ ์ถœ์ฒ˜๋ฅผ ๋ฐํž ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: ์—๋ฆญ ๋ ˆ์ด๋จผ๋“œ๋Š” ํŒŒ์ด์ฌ์„ ๋ฐฐ์šด์ง€ ํ•˜๋ฃจ ๋งŒ์— ์›ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค. [^myfootnote] [^myfootnote]: ์—๋ฆญ ๋ ˆ์ด๋จผ๋“œ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฒฝํ—˜์ด ๋งŽ์€ ๊ตฌ๋ฃจ ํ”„๋กœ๊ทธ๋ž˜๋จธ์ด๋‹ค. ๋ณดํ†ต ์‚ฌ๋žŒ์€ ํŒŒ์ด์ฌ์„ ๋ฐฐ์šฐ๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐ 1์ฃผ์ผ ์ •๋„์˜ ์ ์‘ ์‹œ๊ฐ„์ด ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค. ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ์—๋ฆญ ๋ ˆ์ด๋จผ๋“œ๋Š” ํŒŒ์ด์ฌ์„ ๋ฐฐ์šด์ง€ ํ•˜๋ฃจ ๋งŒ์— ์›ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค. 1 โ€ป ๊ฐ ์ฃผ๋ช…(์˜ˆ:myfootnote)์€ ๋งˆ์Œ๋Œ€๋กœ ๋ช…๋ช…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ ๋‚ด ๋ชฉ์ฐจ (Table Of Contents) ๋ฌธ์„œ ๋‚ด์— ์‚ฌ์šฉ๋œ ํ—ค๋”ฉ ํƒœ๊ทธ๋“ค์„ ์ด์šฉํ•˜์—ฌ [TOC] ์ž…๋ ฅ ์‹œ ๊ทธ ๋ถ€๋ถ„์— ๋ชฉ์ฐจ๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: [TOC] ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ํ…Œ์ด๋ธ” ๊ฐ์ฃผ ๋ฌธ์„œ ๋‚ด ๋ชฉ์ฐจ (Table Of Contents) ์—๋ฆญ ๋ ˆ์ด๋จผ๋“œ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฒฝํ—˜์ด ๋งŽ์€ ๊ตฌ๋ฃจ ํ”„๋กœ๊ทธ๋ž˜๋จธ์ด๋‹ค. ๋ณดํ†ต ์‚ฌ๋žŒ์€ ํŒŒ์ด์ฌ์„ ๋ฐฐ์šฐ๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐ 1์ฃผ์ผ ์ •๋„์˜ ์ ์‘ ์‹œ๊ฐ„์ด ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค. โ†ฉ 09) ์œ„ํ‚ค๋…์Šค extension ์•„๋ž˜๋Š” ๋งˆํฌ๋‹ค์šด ๋ฌธ๋ฒ•์ด ์•„๋‹Œ ์œ„ํ‚ค๋…์Šค์—์„œ๋งŒ ์ง€์›๋˜๋Š” ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค. ํŒ ๋ธ”๋ก ์ฝ”๋“œ ๋งˆ์ปค ์šฉ์–ด ๋งํฌ ํŒ ๋ธ”๋ก ๋„์›€๋ง ๋“ฑ์„ ํŒ ๋ธ”๋ก์œผ๋กœ ํ‘œ์‹œํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌธ์žฅ์„ [[TIP]]๊ณผ [[/TIP]]์œผ๋กœ ๊ฐ์‹ธ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: [[TIP]] ** ํŒ ๋ธ”๋ก์— ๋Œ€ํ•˜์—ฌ ** ๋„์›€๋ง์ด๋‚˜ ํŒ์„ ์„ค๋ช…ํ•˜๋Š” ๋ธ”๋ก์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [[/TIP]] ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: tip ํŒ ๋ธ”๋ก์— ๋Œ€ํ•˜์—ฌ ๋„์›€๋ง์ด๋‚˜ ํŒ์„ ์„ค๋ช…ํ•˜๋Š” ๋ธ”๋ก์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒ ๋ธ”๋ก์— "tip" ๋Œ€์‹  ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ๋„ฃ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. [[TIP("์•Œ์•„๋‘๊ธฐ")]] ** ํŒ ๋ธ”๋ก์— ๋Œ€ํ•˜์—ฌ ** ๋„์›€๋ง์ด๋‚˜ ํŒ์„ ์„ค๋ช…ํ•˜๋Š” ๋ธ”๋ก์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [[/TIP]] ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: ์•Œ์•„๋‘๊ธฐ ํŒ ๋ธ”๋ก์— ๋Œ€ํ•˜์—ฌ ๋„์›€๋ง์ด๋‚˜ ํŒ์„ ์„ค๋ช…ํ•˜๋Š” ๋ธ”๋ก์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ ๋งˆ์ปค ์ฝ”๋“œ์˜ ์ผ๋ถ€๋ถ„์„ ๊ฐ•์กฐ ํ‘œ์‹œํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ•์กฐํ•˜๊ณ  ์‹ถ์€ ๋ถ€๋ถ„์„ [[MARK]]์™€ [[/MARK]]๋กœ ๊ฐ์‹ธ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: def sum(a, b): return a+b ์ฝ”๋“œ์˜ ์‚ญ์ œ๋œ ๋ถ€๋ถ„์„ ํ‘œ์‹œํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ญ์ œ ๋ถ€๋ถ„์„ [[SMARK]]์™€ [[/SMARK]]๋กœ ๊ฐ์‹ธ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ ์˜ˆ: ๋ณ€ํ™˜ ๊ฒฐ๊ณผ: def sum(a, b): return a+b ์ฝ”๋“œ ๋งˆ์ปค๋Š” ํŽธ์ง‘๊ธฐ์—์„œ ๋งˆ์ปค๋ฅผ ์ ์šฉํ•  ๋ถ€๋ถ„์„ ๋งˆ์šฐ์Šค๋กœ ์„ ํƒํ•œ ์ƒํƒœ์—์„œ ์•„๋ž˜ ์•„์ด์ฝ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฉ์–ด ๋งํฌ ์šฉ์–ด ๋งํฌ๋Š” ์œ„ํ‚ค๋…์Šค์˜ ์ž๋งค ์„œ๋น„์Šค์ธ ์œ„ํ‚ค๋…์Šค ์šฉ์–ด ์‚ฌ์ „์˜ ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์„ ํŒ์—…์œผ๋กœ ๋ฏธ๋ฆฌ ๋ณด๊ธฐ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. (๊ฐ์ฃผ๋‚˜ ํŒ ๋Œ€์‹  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) ์œ„ํ‚ค๋…์Šค ์šฉ์–ด ์‚ฌ์ „: https://wikidocs.net/wiki/ ์šฉ์–ด ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์šฉ์–ด ์‚ฌ์ „์˜ ์šฉ์–ด๋ช… ์ขŒ์šฐ๋ฅผ [["์™€ "]]๋กœ ๊ฐ์‹ธ์„œ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. [["์œ„ํ‚ค์œ„ํ‚ค"]] ์‹คํ–‰ ๊ฒฐ๊ณผ ๋ณด๊ธฐ: ์œ„ํ‚ค์œ„ํ‚ค (๋งํฌ๋ฅผ ํด๋ฆญํ•ด ๋ณด์„ธ์š”) ์šฉ์–ด ๋งํฌ๋Š” ์œ„์™€ ๊ฐ™์ด ๊ฐˆ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋˜๋ฉฐ ํ•ด๋‹น ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด ์šฉ์–ด์˜ ๋‚ด์šฉ์„ ํŒ์—…์œผ๋กœ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํŒ์—…์„ ๋‹ซ์œผ๋ ค๋ฉด ์šฉ์–ด ๋งํฌ๋ฅผ ๋‹ค์‹œ ํด๋ฆญํ•˜๊ฑฐ๋‚˜ ํŒ์—…์ฐฝ์˜ x ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—๋””ํ„ฐ์˜ ์•„์ด์ฝ˜์„ ํ†ตํ•ด์„œ๋„ ์‰ฝ๊ฒŒ ์šฉ์–ด ๋งํฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์„ ํƒํ•œ ํ›„ ์—๋””ํ„ฐ์˜ ๋‹ค์Œ ์•„์ด์ฝ˜์„ ๋ˆ„๋ฅด๋ฉด ๋ฉ๋‹ˆ๋‹ค. 03-3 ์ˆ˜์‹ ์ž…๋ ฅ ์œ„ํ‚ค๋…์Šค์˜ ์ˆ˜์‹์€ mathjax๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜์‹ ์ž…๋ ฅ inline ๋ชจ๋“œ ์ˆ˜์‹ ์ž…๋ ฅ ์ˆ˜์‹ ์ž…๋ ฅ ํฌ๋งท ์ˆ˜์‹ ์ž…๋ ฅ ๋ฌธ์ž ํšŒํ”ผ ํ–‰๋ ฌ์‹ ์ฃผ์˜์‚ฌํ•ญ ์•ฑ ์ฃผ์˜์‚ฌํ•ญ ์ˆ˜์‹ ์ž…๋ ฅ ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด, (์ˆ˜์‹์˜ ์ขŒ์šฐ๋ฅผ $$ ๋ฌธ์ž๋กœ ๊ฐ์‹ธ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.) $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$$ ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆ˜์‹์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. = b b โˆ’ a 2 ์ˆ˜์‹ ์ž…๋ ฅ์˜ ๋ฐฉ๋ฒ•์€ LaTeX ๋ฌธ์„œ ์ž‘์„ฑ ์‹œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. inline ๋ชจ๋“œ ์ˆ˜์‹ ์ž…๋ ฅ ๋งŒ์•ฝ, inline ๋ชจ๋“œ(๊ฐ™์€ ์ค„์— ํ‘œ์‹œ)๋กœ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์œผ๋ฉด $$ ๋Œ€์‹  $๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ์ค„์— ํ‘œ์‹œํ•˜๋ ค๋ฉด ์ด๋ ‡๊ฒŒ $x = {-b \pm \sqrt{b^2-4ac} \over 2a}$ `$`๋ฅผ ํ•œ ๊ฐœ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ์ค„์— ํ‘œ์‹œํ•˜๋ ค๋ฉด ์ด๋ ‡๊ฒŒ = b b โˆ’ a 2 $๋ฅผ ํ•œ ๊ฐœ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜์‹ ์ž…๋ ฅ ํฌ๋งท ๋‹ค์Œ์˜ ๋ฌธ์„œ(LaTeX ํฌ๋งท)๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์›ํ•˜๋Š” ์ˆ˜์‹์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. http://en.wikipedia.org/wiki/Help:Displaying_a_formula#Formatting_using_TeX ์ˆ˜์‹ ์ž…๋ ฅ ๋ฌธ์ž ํšŒํ”ผ ์˜ˆ๋ฅผ ๋“ค์–ด $foo, $bar๋ผ๋Š” ๋ฌธ์žฅ์€ $ ์‚ฌ์ด์˜ ๋ฌธ์žฅ์ด ์ˆ˜์‹์œผ๋กœ ์ธ์‹๋˜๋ฏ€๋กœ $ ์ˆ˜์‹์„ ํ”ผํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. <span class="tex2jax_ignore">$foo, $bar</span> ํ–‰๋ ฌ์‹ ์ฃผ์˜์‚ฌํ•ญ ์ˆ˜์‹ ๋‚ด์— \\ ๊ธฐํ˜ธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” (์˜ˆ: ํ–‰๋ ฌ์‹) \\ ๋Œ€์‹  \\\\ ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ) $ \begin{bmatrix} a & b \\\\ c & d \end{bmatrix}$ ๊ฒฐ๊ด๊ฐ’: [ b d ] ์•ฑ ์ฃผ์˜์‚ฌํ•ญ ์ˆ˜์‹์€ ์ค„ ๋ฐ”๊ฟˆ(word-break)์ด ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์•ฑ์—์„œ ๋„ˆ๋ฌด ๊ธด ์ˆ˜์‹์„ ๋ณผ ๊ฒฝ์šฐ ๊ฐ€๋กœ ์Šคํฌ๋กค์ด ์ƒ๊ฒจ์„œ ๋ถˆํŽธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 03-4 ๊ฒ€์ƒ‰ ์œ„ํ‚ค๋…์Šค ํŽธ์ง‘ํ™”๋ฉด์—์„œ ๋ธŒ๋ผ์šฐ์ €์˜ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์œผ๋กœ๋Š” ํŽธ์ง‘๊ธฐ ๋‚ด์˜ ๊ธ€๋“ค์ด ์ •์ƒ์ ์œผ๋กœ ๊ฒ€์ƒ‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŽธ์ง‘๊ธฐ ๋‚ด๋ถ€์˜ ๊ธ€๋“ค์„ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„ํ‚ค๋…์Šค ํŽธ์ง‘๊ธฐ์— ๋งˆ์šฐ์Šค ํฌ์ปค์Šค๊ฐ€ ์žˆ๋Š” ์ƒํƒœ๋กœ Ctrl+F ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํŽธ์ง‘๊ธฐ ๋‚ด์— ๊ฒ€์ƒ‰์ฐฝ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด๊ณณ์— ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์—ฌ์„œ ๊ฒ€์ƒ‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰๋œ ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ Ctrl+G๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๋‹ค์Œ ๊ฒ€์ƒ‰ ์œ„์น˜๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 03-5 ์ฑ… ๋ฐฑ์—…๊ณผ ํŽ˜์ด์ง€ ๋ณต๊ตฌ ์œ„ํ‚ค๋…์Šค ์ฑ…์„ ๋ฐฑ์—…ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ํŽ˜์ด์ง€๋ฅผ ์ด์ „ ๋ฒ„์ „์œผ๋กœ ๋ณต๊ตฌํ•˜๋Š” ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฑ… ์ „์ฒด ๋ฐฑ์—…ํ•˜๊ธฐ ํŽ˜์ด์ง€ ๋ณต๊ตฌํ•˜๊ธฐ ๋ณ€๊ฒฝ ์ด๋ ฅ ํ™”๋ฉด ๊ฐœ๋ณ„ ๋ฒ„์ „ ๋‹ค์šด๋กœ๋“œ ์ฑ… ์ „์ฒด ๋ฐฑ์—…ํ•˜๊ธฐ ์ฑ…์˜ ์ˆ˜์ • ํ™”๋ฉด์—์„œ <์ฑ… ๋‹ค์šด๋กœ๋“œ> ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด zip ํŒŒ์ผ์ด ๋‹ค์šด๋กœ๋“œ ๋ฉ๋‹ˆ๋‹ค. zip ํŒŒ์ผ์—๋Š” ์ฑ…์˜ ๋ชจ๋“  ํŽ˜์ด์ง€๊ฐ€ ๋“ค์–ด ์žˆ์Šต๋‹ˆ๋‹ค. (์—…๋กœ๋“œํ•œ ์ด๋ฏธ์ง€ ํฌํ•จ) ํŽ˜์ด์ง€ ๋ณต๊ตฌํ•˜๊ธฐ ํŠน์ • ์‹œ์ ์˜ ํŽ˜์ด์ง€๋ฅผ ๋‚ด๋ ค๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์–ด๋–ค ํŽ˜์ด์ง€๋Š” 5๋ฒˆ์„ ์ˆ˜์ •ํ–ˆ์„ ๊ฒฝ์šฐ 5๊ฐœ์˜ ๋ฒ„์ „์„ ๊ฐ๊ฐ ๋‚ด๋ ค๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ์ด๋ ฅ ํ™”๋ฉด ๋ฐฑ์—…์„ ์›ํ•˜๋Š” ํŽ˜์ด์ง€ ํ•˜๋‹จ์˜ "๋งˆ์ง€๋ง‰ ํŽธ์ง‘ ์ผ์‹œ" ๋งํฌ๋ฅผ ๋ˆŒ๋Ÿฌ ๋ณ€๊ฒฝ ์ด๋ ฅ ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ๋ณ„ ๋ฒ„์ „ ๋‹ค์šด๋กœ๋“œ ๋ณ€๊ฒฝ ์ด๋ ฅ ํ™”๋ฉด์—์„œ ๋ฐฑ์—…์„ ์›ํ•˜๋Š” ๋ฒ„์ „์˜ <download> ๋งํฌ๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋‚ด๋ ค๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๋ ค๋ฐ›์€ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ „ ๋ฒ„์ „์œผ๋กœ ์‰ฝ๊ฒŒ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 04 ์œ„ํ‚ค๋…์Šค ๊ด‘๊ณ  ์ €์ž๋Š” ๋ณธ์ธ์ด ์ž‘์„ฑํ•œ ์ฑ…์— ๊ตฌ๊ธ€ ๊ด‘๊ณ ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ์ˆ˜์ต์„ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜์ต์„ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ด‘๊ณ ์˜ ํ˜•ํƒœ๋Š” ์ด 3๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๋ณธ์ธ์—๊ฒŒ ์ ํ•ฉํ•œ ๊ด‘๊ณ ๋ฅผ ์ž˜ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ €์ž์˜ ์• ๋“œ์„ผ์Šค ์‚ฌ์šฉ (์ž์„ธํžˆ ๋ณด๊ธฐ) ํ˜ธ์ŠคํŠธ ํŒŒํŠธ๋„ˆ(์˜ˆ: ํ‹ฐ์Šคํ† ๋ฆฌ)์˜ ์• ๋“œ์„ผ์Šค ์‚ฌ์šฉ (์ž์„ธํžˆ ๋ณด๊ธฐ) ์œ„ํ‚ค๋…์Šค์˜ ํฌ์ธํŠธ ๊ด‘๊ณ  ์‚ฌ์šฉ (์ž์„ธํžˆ ๋ณด๊ธฐ) ๊ณต์ง€์‚ฌํ•ญ ์ˆ˜์ต ๋ฐฐ๋ถ„ ์œ„์˜ 1, 2๋ฒˆ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ 10% ๋น„์œจ๋กœ ์œ„ํ‚ค๋…์Šค ์• ๋“œ์„ผ์Šค๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. (์ €์ž์˜ ๊ด‘๊ณ ์™€ ์œ„ํ‚ค๋…์Šค์˜ ๊ด‘๊ณ ๊ฐ€ 9 ๋Œ€ 1์˜ ๋น„์œจ๋กœ ๊ฒŒ์‹œ. ) ๊ด‘๊ณ ๋ฅผ ํ‘œ์‹œํ•˜์ง€ ์•Š๋Š” ์ฑ…์˜ ๊ฒฝ์šฐ์—๋Š” ์œ„ํ‚ค๋…์Šค์˜ ์• ๋“œ์„ผ์Šค๋งŒ 10% ๋น„์œจ๋กœ ๊ฒŒ์‹œ๋ฉ๋‹ˆ๋‹ค. ์• ๋“œ์„ผ์Šค 1๋ฒˆ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ™œ์„ฑํ™”๋œ ์• ๋“œ์„ผ์Šค ๊ณ„์ •์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๊ณ„์ • ์ƒ์„ฑ ๋ฐ ํ™œ์„ฑํ™”๋ฅผ ์œ„ํ•ด ์œ„ํ‚ค๋…์Šค ์‚ฌ์ดํŠธ๋ฅผ ์ด์šฉํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. (์• ๋“œ์„ผ์Šค ๊ณ„์ • ํ™œ์„ฑํ™”๋ฅผ ์œ„ํ•ด์„œ๋Š” ์‚ฌ์ดํŠธ ์†Œ์œ ๊ถŒํ•œ์ด ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.) ๋งŒ์•ฝ ์• ๋“œ์„ผ์Šค ํ˜ธ์ŠคํŠธ ํŒŒํŠธ๋„ˆ(์˜ˆ: ํ‹ฐ์Šคํ† ๋ฆฌ, Blogger ๋“ฑ)๋ฅผ ๊ฒฝ์œ ํ•˜์—ฌ ์• ๋“œ์„ผ์Šค ์Šน์ธ์„ ๋ฐ›์€ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. ํ˜ธ์ŠคํŠธ ํŒŒํŠธ๋„ˆ์˜ ์• ๋“œ์„ผ์Šค ๊ฒŒ์‹œํ•˜๊ธฐ 04-1 ์ €์ž์˜ ์• ๋“œ์„ผ์Šค ๊ฒŒ์‹œํ•˜๊ธฐ ํ™œ์„ฑํ™”๋œ ์• ๋“œ์„ผ์Šค ๊ณ„์ •์„ ์†Œ์œ ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ๊ธ€์˜ ๊ด‘๊ณ  ์Šคํฌ๋ฆฝํŠธ๋ฅผ "์ฑ… ์ˆ˜์ •" ํ™”๋ฉด์— ์ž…๋ ฅํ•˜์—ฌ ๊ตฌ๊ธ€ ๊ด‘๊ณ ๋ฅผ ๊ฒŒ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์Šคํฌํ†ฑ ๊ด‘๊ณ  [์ถ”์ฒœ] ๋ฐ•์Šค ํ˜•ํƒœ์˜ ๊ด‘๊ณ  ์˜ˆ์‹œ(336x280, 2๊ฐœ ๊ฒŒ์‹œ) ๋ฐ•์Šค ํ˜•ํƒœ์˜ ๊ด‘๊ณ  ์˜ˆ์‹œ(336x280, 1๊ฐœ ๊ฒŒ์‹œ) ๋ชจ๋ฐ”์ผ ๊ด‘๊ณ  ads.txt ๋ฐ์Šคํฌํ†ฑ ๊ด‘๊ณ  ์ €์ž์˜ "์ฑ… ์ˆ˜์ •" ํ™”๋ฉด์—์„œ ๊ด‘๊ณ  ๋‚ด์šฉ(ํƒ์ƒ์šฉ ์ปดํ“จํ„ฐ)์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์˜ HTML ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜์„ธ์š”. [์ถ”์ฒœ] ๋ฐ•์Šค ํ˜•ํƒœ์˜ ๊ด‘๊ณ  ์˜ˆ์‹œ(336x280, 2๊ฐœ ๊ฒŒ์‹œ) ๊ตฌ๊ธ€ ๊ด‘๊ณ  2๊ฐœ๋ฅผ ๊ฒŒ์‹œํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์˜ HTML ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜์„ธ์š”. ๋‘ ๊ฐœ์˜ ๊ตฌ๊ธ€ ๊ด‘๊ณ  ๊ณ„์ •์€ ์„œ๋กœ ๋‹ฌ๋ผ๋„ ๋ฉ๋‹ˆ๋‹ค. (๊ณต๋™ ์ €์ž์˜ ๊ฒฝ์šฐ ์ €์ž๋ณ„๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ๊ตฌ๊ธ€ ๊ด‘๊ณ  ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) <div class="clearfix"> <div class="pull-left"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-9470517771012578" data-ad-slot="3437110462"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="pull-left" style="margin-left:30px"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-9470517771012578" data-ad-slot="3437110462"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> ๋…ธ๋ž€์ƒ‰์œผ๋กœ ๋งˆํ‚น๋˜์–ด ์žˆ๋Š” ๋ถ€๋ถ„์€ ๋ณธ์ธ์˜ ๊ฒƒ์œผ๋กœ ์ˆ˜์ •ํ•ด์•ผ ํ•จ ๋ฐ•์Šค ํ˜•ํƒœ์˜ ๊ด‘๊ณ  ์˜ˆ์‹œ(336x280, 1๊ฐœ ๊ฒŒ์‹œ) <div class="row"> <div class="col-sm-6"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-9470517771012578" data-ad-slot="3437110462"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> ๋…ธ๋ž€์ƒ‰์œผ๋กœ ๋งˆํ‚น๋˜์–ด ์žˆ๋Š” ๋ถ€๋ถ„์€ ๋ณธ์ธ์˜ ๊ฒƒ์œผ๋กœ ์ˆ˜์ •ํ•ด์•ผ ํ•จ ๋ชจ๋ฐ”์ผ ๊ด‘๊ณ  ๋ชจ๋ฐ”์ผ ๊ด‘๊ณ ๋Š” ๊ฐ€๊ธ‰์  ๋ฐ˜์‘ํ˜•(responsive) ํ˜•ํƒœ์˜ ๊ด‘๊ณ ๋ฅผ ๊ฒŒ์‹œํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. PC์šฉ์˜ ๊ด‘๊ณ ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์œ„ํ‚ค๋…์Šค ์•ฑ์— ๊ฐ€๋กœ ์Šคํฌ๋กค์ด ์ƒ๊ฒจ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. [๋ชจ๋ฐ”์ผ ๊ด‘๊ณ ์˜ ์˜ˆ] <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- ์œ„ํ‚ค๋…์Šค๋ชจ๋ฐ”์ผ2020 --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-9470517771012578" data-ad-slot="4465552259" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> ads.txt ์ €์ž์˜ ๊ตฌ๊ธ€ ๊ด‘๊ณ  ์•„์ด๋””๊ฐ€ ์•„๋ž˜ URL์— ๋ฐ˜๋“œ์‹œ ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ๋งŒ ๊ตฌ๊ธ€ ๊ด‘๊ณ ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์•„๋ž˜ URL์— ์ €์ž์˜ ๊ตฌ๊ธ€ ๊ด‘๊ณ  ์•„์ด๋””๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด ๋Œ“๊ธ€์ด๋‚˜ <EMAIL>์œผ๋กœ ์—ฐ๋ฝํ•ด ์ฃผ์‹œ๋ฉด ์ถ”๊ฐ€ํ•ด ๋“œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. https://wikidocs.net/ads.txt 04-2 ํ˜ธ์ŠคํŠธ ํŒŒํŠธ๋„ˆ์˜ ์• ๋“œ์„ผ์Šค ๊ฒŒ์‹œํ•˜๊ธฐ ๋ณธ์ธ์˜ ํ‹ฐ์Šคํ† ๋ฆฌ ๊ตฌ๊ธ€ ๊ด‘๊ณ ๋ฅผ ์œ„ํ‚ค๋…์Šค์— ๊ฒŒ์‹œํ•˜๋ ค๋ฉด ์•„๋ž˜์— ์„ค๋ช…ํ•œ ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ฑ… ์ˆ˜์ • > ๊ด‘๊ณ  ๋‚ด์šฉ์— ์ €์žฅํ•˜์„ธ์š”. ํ‹ฐ์Šคํ† ๋ฆฌ์—์„œ ์• ๋“œ์„ผ์Šค ๊ณ„์ •์„ ์Šน์ธ๋ฐ›์€ ๊ฒฝ์šฐ ๋‘ ๊ฐœ์˜ ์• ๋“œ์„ผ์Šค๋ฅผ ํ‘œ์‹œํ•˜๋ ค๋ฉด ํ‹ฐ์Šคํ† ๋ฆฌ์—์„œ ์• ๋“œ์„ผ์Šค ๊ณ„์ •์„ ์Šน์ธ๋ฐ›์€ ๊ฒฝ์šฐ ์ฃผ์˜์‚ฌํ•ญ iframe ์ •์ฑ… ์œ„๋ฐ˜์— ๊ด€ํ•˜์—ฌ ํ‹ฐ์Šคํ† ๋ฆฌ, ๋ธ”๋กœ๊ทธ ์Šคํฟ(๋ธ”๋กœ๊ฑฐ) ๋“ฑ์€ ์• ๋“œ์„ผ์Šค ํ˜ธ์ŠคํŠธ ํŒŒํŠธ๋„ˆ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์• ๋“œ์„ผ์Šค ํ˜ธ์ŠคํŠธ ํŒŒํŠธ๋„ˆ๋ฅผ ํ†ตํ•ด ์• ๋“œ์„ผ์Šค ์Šน์ธ์„ ๋ฐ›์€ ๊ฒฝ์šฐ์—๋Š” ์œ„ํ‚ค๋…์Šค์—์„œ iframe์„ ์ด์šฉํ•œ ๋ฐฉ์‹์œผ๋กœ๋งŒ ๊ด‘๊ณ  ๊ฒŒ์‹œ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ iframe์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ •์ฑ… ์œ„๋ฐ˜์ด ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ๊ณต์ง€๋ฅผ ํ™•์ธํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. iframe ๊ด€๋ จ ์• ๋“œ์„ผ์Šค FAQ ๋งŒ์•ฝ ์• ๋“œ์„ผ์Šค ํ˜ธ์ŠคํŠธ ํŒŒํŠธ๋„ˆ(์˜ˆ: ํ‹ฐ์Šคํ† ๋ฆฌ, ๋ธ”๋กœ๊ฑฐ ๋“ฑ)๋ฅผ ๊ฒฝ์œ ํ•˜์—ฌ ์• ๋“œ์„ผ์Šค ์Šน์ธ์„ ๋ฐ›์€ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ๋งŒ ๊ตฌ๊ธ€ ๊ด‘๊ณ  ๊ฒŒ์‹œ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. โ€ป ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์„ ์•„์‹œ๋Š” ๋ถ„์€ ์—ฌ๊ธฐ ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. [ํ‹ฐ์Šคํ† ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ๊ธ€ ๊ด‘๊ณ ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ• ์˜ˆ์‹œ] <iframe name="adsense_iframe" width="336" height="280" frameborder="0"></iframe> <form name="adsense_form" method="post" target="adsense_iframe" action="https://googleads.g.doubleclick.net/pagead/ads"> <input type="hidden" name="client" value="ca-pub-1176584478877240"> <input type="hidden" name="host" value="ca-host-pub-9691043933427338"> <input type="hidden" name="format" value="336x280"> <input type="hidden" name="url" value="https://dextto.tistory.com/225"> </form> <script>document.adsense_form.submit();</script> ๋…ธ๋ž€์ƒ‰์œผ๋กœ ๋งˆํ‚น ํ•œ ๋ถ€๋ถ„์€ ๋ณธ์ธ์—๊ฒŒ ๋งž๋Š” ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ hidden input ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค. client - ์Šน์ธ๋ฐ›์€ ์• ๋“œ์„ผ์Šค ๊ณ„์ • ์ฝ”๋“œ host - ํ˜ธ์ŠคํŠธ ํŒŒํŠธ๋„ˆ์˜ ์• ๋“œ์„ผ์Šค ๊ณ„์ • ์ฝ”๋“œ (ํ‹ฐ์Šคํ† ๋ฆฌ์˜ ๊ฒฝ์šฐ: ca-host-pub-9691043933427338) format - ๊ด‘๊ณ ์˜ ํฌ๊ธฐ(๊ฐ€๋กœ x ์„ธ๋กœ), iframe์˜ width, height์™€ ์„œ๋กœ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. url - ์• ๋“œ์„ผ์Šค ๊ณ„์ •์„ ์Šน์ธ๋ฐ›์€ ํ‹ฐ์Šคํ† ๋ฆฌ์˜ URL ๋‘ ๊ฐœ์˜ ์• ๋“œ์„ผ์Šค๋ฅผ ํ‘œ์‹œํ•˜๋ ค๋ฉด ํ•œ ํŽ˜์ด์ง€์— 336x280 ํฌ๊ธฐ์˜ ๊ด‘๊ณ  ๋‘ ๊ฐœ๋ฅผ ํ‘œ์‹œํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. <div class="clearfix"> <div class="pull-left" style="margin-right:30px"> <iframe name="adsense_iframe1" width="336" height="280" frameborder="0"></iframe> <form name="adsense_form1" method="post" target="adsense_iframe1" action="https://googleads.g.doubleclick.net/pagead/ads"> <input type="hidden" name="client" value="ca-pub-1176584478877240"> <input type="hidden" name="host" value="ca-host-pub-9691043933427338"> <input type="hidden" name="format" value="336x280"> <input type="hidden" name="url" value="https://dextto.tistory.com/225"> </form> <script>document.adsense_form1.submit();</script> </div> <div class="pull-left"> <iframe name="adsense_iframe2" width="336" height="280" frameborder="0"></iframe> <form name="adsense_form2" method="post" target="adsense_iframe2" action="https://googleads.g.doubleclick.net/pagead/ads"> <input type="hidden" name="client" value="ca-pub-1176584478877240"> <input type="hidden" name="host" value="ca-host-pub-9691043933427338"> <input type="hidden" name="format" value="336x280"> <input type="hidden" name="url" value="https://dextto.tistory.com/225"> </form> <script>document.adsense_form2.submit();</script> </div> </div> ๋…ธ๋ž€์ƒ‰์œผ๋กœ ๋งˆํ‚น ํ•œ ๋ถ€๋ถ„์€ ๋ณธ์ธ์—๊ฒŒ ๋งž๋Š” ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 04-3 ์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ํ™œ์„ฑํ™”๋œ ์• ๋“œ์„ผ์Šค ๊ณ„์ •์ด ์—†์œผ๋ฉด ์œ„ํ‚ค๋…์Šค์˜ ํฌ์ธํŠธ ๊ด‘๊ณ ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜์ต์„ ์ฐฝ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ ๋ž€? ์ˆ˜์ต๋ถ„๋ฐฐ ๋ฐฉ์นจ ์‚ฌ์šฉ ๋ฐฉ๋ฒ• ํฌ์ธํŠธ ๋ฐ ์ˆ˜์ต ์กฐํšŒ ์ˆ˜์ต ์ง€๊ธ‰ ์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ ๋ž€? ์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ ๋Š” ์• ๋“œ์„ผ์Šค ๊ณ„์ •์ด ์—†๋Š” ์ €์ž๋ฅผ ์œ„ํ•ด ์ €์ž๊ฐ€ ์ž‘์„ฑํ•œ ์ฑ…์— ์œ„ํ‚ค๋…์Šค์˜ ์• ๋“œ์„ผ์Šค๋ฅผ ๊ฒŒ์‹œํ•˜์—ฌ ๋ฐœ์ƒํ•˜๋Š” ์ˆ˜์ต์„ ์ €์ž์—๊ฒŒ ๋ถ„๋ฐฐํ•˜๋Š” ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค. ์ˆ˜์ต๋ถ„๋ฐฐ ๋ฐฉ์นจ ๊ด‘๊ณ  ์ˆ˜์ต์˜ 70%๋ฅผ ์ €์ž์—๊ฒŒ ๋ถ„๋ฐฐํ•ฉ๋‹ˆ๋‹ค. ํฌ์ธํŠธ๋Š” ์ฑ…๋ณ„๋กœ ์›ํ™”๋กœ ํ™˜์‚ฐํ•˜์—ฌ 10๋งŒ ์› ์ด์ƒ ์ ๋ฆฝ๋˜๋ฉด ์ €์ž์—๊ฒŒ ์ง€๊ธ‰๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๋ฐฉ๋ฒ• ์œ„ํ‚ค๋…์Šค์˜ ์ฑ… ์ˆ˜์ • ํ™”๋ฉด์—์„œ "๊ด‘๊ณ ๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๊นŒ?"์™€ "์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๊นŒ?"๋ฅผ "์˜ˆ"๋กœ ์„ค์ •ํ•˜๊ณ  ์ €์žฅํ•˜๋ฉด ํ•ด๋‹น ์ฑ…์— ์œ„ํ‚ค๋…์Šค์˜ ์• ๋“œ์„ผ์Šค๊ฐ€ ๊ฒŒ์‹œ๋˜๊ณ  ํฌ์ธํŠธ๊ฐ€ ์ ๋ฆฝ๋˜๊ธฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ํฌ์ธํŠธ ๋ฐ ์ˆ˜์ต ์กฐํšŒ ์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ„ํ‚ค๋…์Šค ๋ฉ”์ธ ํŽ˜์ด์ง€์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ "๊ด‘๊ณ  ์ˆ˜์ต" ๋ฉ”๋‰ด๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๊ด‘๊ณ  ์ˆ˜์ต ๋ฉ”๋‰ด๋ฅผ ํ†ตํ•ด ํฌ์ธํŠธ์™€ ์ˆ˜์ต์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ˆ˜์ต์€ ๋งค์›” ์ดˆ์— ์ „์›”์น˜๋ฅผ ์ง‘๊ณ„ํ•ฉ๋‹ˆ๋‹ค.) ์ˆ˜์ต ์ง€๊ธ‰ ์›ํ™”๋กœ ํ™˜์‚ฐ๋œ ๊ธˆ์•ก์ด 10๋งŒ ์› ์ด์ƒ์ผ ๊ฒฝ์šฐ ์ €์ž๊ฐ€ ๋“ฑ๋กํ•œ ๊ณ„์ขŒ์— ์ž…๊ธˆ๋ฉ๋‹ˆ๋‹ค. ์ˆ˜์ต์„ ์ง€๊ธ‰๋ฐ›์„ ๊ณ„์ขŒ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธ‰ ์‹œ๊ธฐ๋Š” ๋งค์›” ์ค‘์ˆœ ~ ๋ง ์‚ฌ์ด์ž…๋‹ˆ๋‹ค. (๊ณ„์ • ์„ค์ • -> ๊ธฐ๋ณธ ์ •๋ณด -> ์ˆ˜์ต ๊ณ„์ขŒ) 04-4 ๊ณ ์ •ํ˜• ์šฐ์ƒ๋‹จ ๊ด‘๊ณ  ๊ณ ์ •ํ˜• ์šฐ์ƒ๋‹จ ๊ด‘๊ณ  ๊ฒŒ์‹œ ๋ฐฉ๋ฒ• ์ฃผ์˜ํ•  ์ ๊ณผ ๊ด‘๊ณ ์˜ ์˜ˆ์‹œ ๊ณ ์ •ํ˜• ์šฐ์ƒ๋‹จ ๊ด‘๊ณ  ๋‹ค์Œ์ฒ˜๋Ÿผ ํ™”๋ฉด ์šฐ์ƒ๋‹จ์— ๊ตฌ๊ธ€ ๊ด‘๊ณ ๋ฅผ ๊ฒŒ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ, ์šฐ์ธก ๊ณ ์ •ํ˜• ๊ด‘๊ณ ๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ ๊ฐ€๋กœ ์‚ฌ์ด์ฆˆ๊ฐ€ 1650px ๋ณด๋‹ค ์ž‘์•„์ง€๋ฉด ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ๊ฒŒ์‹œ ๋ฐฉ๋ฒ• "์ฑ… ์ˆ˜์ •" ํ™”๋ฉด์— ์ง„์ž…ํ•œ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด "๊ด‘๊ณ  ๋‚ด์šฉ(PC, ์šฐ์ƒ๋‹จ)" ํ•ญ๋ชฉ์— ๊ฒŒ์‹œํ•  ๊ตฌ๊ธ€ ๊ด‘๊ณ ๋ฅผ ๋“ฑ๋กํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฃผ์˜ํ•  ์ ๊ณผ ๊ด‘๊ณ ์˜ ์˜ˆ์‹œ ๋‹จ, ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์ด ํ•œ ๊ฐ€์ง€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ๊ธ€ ๊ด‘๊ณ ๋Š” ๋ฐ˜๋“œ์‹œ ๋‹ค์Œ์ฒ˜๋Ÿผ ๋ฐ˜์‘ํ˜•์ด ์•„๋‹Œ ํฌ๊ธฐ ๊ณ ์ •ํ˜•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9470517771012578" crossorigin="anonymous"></script> <!-- wikidocs_sidebar --> <ins class="adsbygoogle" style="display:inline-block;width:200px;height:500px" data-ad-client="ca-pub-9470517771012578" data-ad-slot="7263258217"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> ํฌ๊ธฐ๋Š” ์œ„์˜ ์˜ˆ์‹œ๋Œ€๋กœ 200 x 500์ฒ˜๋Ÿผ ๊ฐ€๋กœ ํฌ๊ธฐ๋ฅผ 200 ์ดํ•˜๋กœ ์ง€์ •ํ•ด์•ผ ๊ด‘๊ณ ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. 04-9 ๊ด‘๊ณ  ์ •์ฑ… ๋ณ€๊ฒฝ ์ด๋ ฅ 2023๋…„ 2์›” 20์ผ 2022๋…„ 3์›” 25์ผ 2019๋…„ 10์›” 05์ผ 2017๋…„ 5์›” 15์ผ 2023๋…„ 2์›” 20์ผ ์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ  ๊ธฐ๋Šฅ ์ถ”๊ฐ€ 2022๋…„ 3์›” 25์ผ ์œ„ํ‚ค๋…์Šค ํŽ˜์ด์ง€ ์šฐ์ƒ๋‹จ ์˜์—ญ์— ๊ณ ์ • ๊ด‘๊ณ  ์ถ”๊ฐ€ ๊ณ ์ •ํ˜• ์šฐ์ธก ์‚ฌ์ด๋“œ ๊ด‘๊ณ  2019๋…„ 10์›” 05์ผ ๊ตฌ๊ธ€ ๊ด‘๊ณ  ์ •์ฑ… ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์ €์ž์˜ ๊ตฌ๊ธ€ ๊ด‘๊ณ  ์•„์ด๋””๊ฐ€ ์•„๋ž˜ URL์— ๋ฐ˜๋“œ์‹œ ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ๋งŒ ๊ตฌ๊ธ€ ๊ด‘๊ณ ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” ์œ„ํ‚ค๋…์Šค์— ๋“ฑ๋ก๋œ ์ €์ž์˜ ๊ตฌ๊ธ€ ๊ด‘๊ณ ๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ž๋™ ๋“ฑ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์•„๋ž˜ URL์— ์ €์ž์˜ ๊ตฌ๊ธ€ ๊ด‘๊ณ  ์•„์ด๋””๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด ๋Œ“๊ธ€์ด๋‚˜ <EMAIL>์œผ๋กœ ์—ฐ๋ฝํ•ด ์ฃผ์‹œ๋ฉด ์ถ”๊ฐ€ํ•ด ๋“œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. https://wikidocs.net/ads.txt 2017๋…„ 5์›” 15์ผ ์œ„ํ‚ค๋…์Šค์˜ ์ง€์†์ ์ด๊ณ  ์•ˆ์ •์ ์ธ ์„œ๋น„์Šค ์šด์˜์„ ์œ„ํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ด‘๊ณ  ์ •์ฑ…์ด ๋ณ€๊ฒฝ๋˜์—ˆ์Œ์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ์ „ : ๊ด‘๊ณ  ์—†์Œ. (๋‹จ, ์ €์ž๊ฐ€ ๊ด‘๊ณ ๋ฅผ ๋“ฑ๋กํ•œ ๊ฒฝ์šฐ ์ €์ž์˜ ๊ด‘๊ณ ๊ฐ€ ํ‘œ์‹œ.) ๋ณ€๊ฒฝ ํ›„ : 10% ๋น„์œจ๋กœ ์œ„ํ‚ค๋…์Šค ๊ด‘๊ณ  ํ‘œ์‹œ (์ €์ž์˜ ๊ด‘๊ณ ์™€ ์œ„ํ‚ค๋…์Šค์˜ ๊ด‘๊ณ ๊ฐ€ 9 ๋Œ€ 1์˜ ๋น„์œจ๋กœ ๊ฒŒ์‹œ. ๋‹จ, ์ €์ž์˜ ๊ด‘๊ณ ๊ฐ€ ๋“ฑ๋ก๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ์œ„ํ‚ค๋…์Šค์˜ ๊ด‘๊ณ ๋งŒ 10% ๋น„์œจ๋กœ ๊ฒŒ์‹œ.) ์ €์ž๋ถ„๋“ค์˜ ๋งŽ์€ ์–‘ํ•ด๋ฅผ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. 05 ์ „์ž์ฑ…(e-book) ์œ„ํ‚ค๋…์Šค์— ์ž‘์„ฑํ•œ ์ฑ…์„ ์ „์ž์ฑ…์œผ๋กœ ํŒ๋งคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2022๋…„ 11์›”๋ถ€ํ„ฐ ์ „์ž์ฑ… ์„œ๋น„์Šค๋ฅผ ์žฌ๊ฐœํ•ฉ๋‹ˆ๋‹ค. 05-1 ์ „์ž์ฑ… ์ž‘์„ฑ๊ณผ ํŒ๋งค ์ง„ํ–‰ ์ˆœ์„œ ๋‹ค์Œ์˜ ์ˆœ์„œ๋กœ ์ „์ž์ฑ…์„ ์ž‘์„ฑํ•˜๊ณ  ํŒ๋งคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1. ์ฑ… ์ž‘์„ฑ (์ €์ž) 2. ์ฑ… ํŒ๋งค ์š”์ฒญ (์ €์ž) 3. ์ฑ… ํŒ๋งค๋ฅผ ์œ„ํ•œ ์ค€๋น„ (์œ„ํ‚ค๋…์Šค) 4. ํŒ๋งค ๊ฐœ์‹œ (์œ„ํ‚ค๋…์Šค) 5. ์ต์›”์— ์ธ์„ธ ์ •์‚ฐ 1. ์ฑ… ์ž‘์„ฑ (์ €์ž) ์ „์ž์ฑ… ํŒ๋งค๋ฅผ ์œ„ํ•œ ์ฑ…์„ ์œ„ํ‚ค๋…์Šค์—์„œ ์ €์ž๊ฐ€ ์ง์ ‘ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. 2. ์ฑ… ํŒ๋งค ์š”์ฒญ (์ €์ž) ์ฑ…์ด ์™„์„ฑ๋˜๋ฉด ์ €์ž๊ฐ€ ์•„๋ž˜์˜ ์ด๋ฉ”์ผ๋กœ ์ „์ž์ฑ… ํŒ๋งค ์š”์ฒญ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฉ”์ผ ์š”์ฒญ (<EMAIL>) 3. ์ฑ… ํŒ๋งค๋ฅผ ์œ„ํ•œ ์ค€๋น„ (์œ„ํ‚ค๋…์Šค) ์œ„ํ‚ค๋…์Šค๋Š” ์ „์ž์ฑ… ํŒ๋งค๋ฅผ ์œ„ํ•œ ์ค€๋น„๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. PDF ์ƒ์„ฑ ํ…Œ์ŠคํŠธ ์˜ค๋ฅ˜ ๊ฒ€์ฆ 4. ํŒ๋งค ๊ฐœ์‹œ (์œ„ํ‚ค๋…์Šค) ์œ„ํ‚ค๋…์Šค๋Š” ์ „์ž์ฑ… ํŒ๋งค๋ฅผ ๊ฐœ์‹œํ•ฉ๋‹ˆ๋‹ค. (ํŒ๋งค๊ฐ€ ์‹œ์ž‘๋˜๋ฉด ์œ„ํ‚ค๋…์Šค ๋ฉ”์ธ ํŽ˜์ด์ง€์˜ "์ „์ž์ฑ…" ํƒญ์— ํ•ด๋‹น ์ฑ…์ด ๋…ธ์ถœ๋ฉ๋‹ˆ๋‹ค.) ์ €์ž์™€ ์ „์ž์ฑ… ๊ฐ€๊ฒฉ ํ˜‘์˜ ISBN ๋ฐœํ–‰ 5. ์ต์›”์— ์ธ์„ธ ์ •์‚ฐ ์ „์ž์ฑ… ์ •์‚ฐ์€ ์ต์›” ์ค‘์ˆœ ~ ๋ง์— ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด 2023๋…„ 4์›”์— ํŒ๋งค๋œ ์ „์ž์ฑ…์€ 5์›” ์ค‘์ˆœ์— ์ •์‚ฐํ•ด ๋“œ๋ฆฝ๋‹ˆ๋‹ค.) 05-2 ์ „์ž์ฑ… ์ƒ˜ํ”Œ ์œ„ํ‚ค๋…์Šค์—์„œ ๋งŒ๋“ค์–ด์ง€๋Š” ์ „์ž์ฑ…์€ ๋ชจ๋‘ ์•„๋ž˜์˜ ์ƒ˜ํ”Œ๊ณผ ๋™์ผํ•œ ํฌ๋งท์œผ๋กœ ์ œ์ž‘๋ฉ๋‹ˆ๋‹ค. (์ฑ… ํ‘œ์ง€๋Š” ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) [์ ํ”„ ํˆฌ ํŒŒ์ด์ฌ] 05-3 ์ „์ž์ฑ… ๋ณด์•ˆ ์œ„ํ‚ค๋…์Šค์—์„œ ํŒ๋งคํ•˜๋Š” ์ „์ž์ฑ…(PDF)์—๋Š” DRM๊ณผ ๊ฐ™์€ ๋ณด์•ˆ ๊ธฐ์ˆ ์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ํŽ˜์ด์ง€ ํ•˜๋‹จ์— ๊ตฌ๋งค์ž ์ •๋ณด(์ด๋ฆ„, ์ด๋ฉ”์ผ)๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์›Œํ„ฐ๋งˆํฌ ํ‘œ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ €์ž๊ฐ€ ์ด๋Ÿฌํ•œ ์›Œํ„ฐ๋งˆํฌ ํ‘œ๊ธฐ ๋ฐฉ์‹์— ๋™์˜ํ•  ๊ฒฝ์šฐ์—๋งŒ ์ „์ž์ฑ… ํŒ๋งค๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. DRM๊ณผ ์›Œํ„ฐ๋งˆํฌ์˜ ์ฐจ์ด DRM ๋ฐฉ์‹ DRM ๋ฐฉ์‹์œผ๋กœ ์•”ํ˜ธํ™”๋œ ๋ฌธ์„œ๋ฅผ ์ฝ๊ธฐ ์œ„ํ•ด์„œ๋Š” DRM ์ „์šฉ ํด๋ผ์ด์–ธํŠธ ํ”„๋กœ๊ทธ๋žจ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” DRM ํด๋ผ์ด์–ธํŠธ ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•˜์—ฌ DRM ์„œ๋ฒ„์— ์ธ์ฆ์„ ํš๋“ํ•œ ํ›„์—์•ผ ๊ตฌ๋งคํ•œ ๋ฌธ์„œ๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ ๋งค์šฐ ๋ณต์žกํ•˜๊ณ  ์‚ฌ์šฉ์ž PC ์ƒํ™ฉ์— ๋”ฐ๋ผ ์˜ค๋ฅ˜๋„ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ DRM ๋ฌธ์„œ๋Š” ํŽธ์ง‘์ด ๋ถˆ๊ฐ€๋Šฅํ•˜์—ฌ ๋ฌธ์„œ ์ž‘์—…์— ์ƒ๋‹นํ•œ ๋ถˆํŽธํ•จ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์›Œํ„ฐ๋งˆํฌ ๋ฐฉ์‹ ์›Œํ„ฐ๋งˆํฌ๋Š” ์ฝ˜ํ…์ธ ์˜ ๋ถˆ๋ฒ• ๋ณต์ œ์™€ ์ €์ž‘๊ถŒ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๋งคํ•œ PDF ๋ฌธ์„œ์˜ ๋ชจ๋“  ํŽ˜์ด์ง€ ํ•˜๋‹จ์— ๊ตฌ๋งค์ž์˜ ์ •๋ณด๋ฅผ ๊ธฐ์žฌํ•˜๋Š” ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ „์ž์ฑ…์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ œ์ž‘ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. ์ „์ž์ฑ…์„ ๊ตฌ๋งคํ•œ ์‚ฌ์šฉ์ž๋Š” ์ €์ž‘๊ถŒ๋ฒ•์˜ ํ—ˆ์šฉ ๋ฒ”์œ„ ๋‚ด์—์„œ ์ž์œ ๋กญ๊ฒŒ ๋ฌธ์„œ๋ฅผ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ํŽธ์ง‘ํ•˜์—ฌ ๊ฐœ์ธ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 05-4 ์ „์ž์ฑ… ๋‹จ๊ฐ€์™€ ์ˆ˜์ต ๋ฐฐ๋ถ„ ์ „์ž์ฑ…์˜ ๋‹จ๊ฐ€๋Š” ์ €์ž์™€ ์œ„ํ‚ค๋…์Šค๊ฐ€ ํ˜‘์˜ํ•˜์—ฌ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ์œ„ํ‚ค๋…์Šค๋Š” 4,000์›๋ถ€ํ„ฐ 33,000์›๊นŒ์ง€์˜ ์ „์ž์ฑ…์„ ํŒ๋งคํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. [์ˆ˜์ต ๋ฐฐ๋ถ„ ํ˜•ํƒœ] (๊ธฐ๋ณธ) ์ „์ž์ฑ… ์ˆ˜์ž…์˜ 80%๋Š” ์ €์ž, 20%๋Š” ์œ„ํ‚ค๋…์Šค์—๊ฒŒ ๋ฐฐ๋ถ„๋ฉ๋‹ˆ๋‹ค. (์›์ฒœ์„ธ ์ œ์™ธ) ์ „์ž์ฑ… ์ˆ˜์ž…์˜ 100%๊ฐ€ ์ €์ž์—๊ฒŒ ๋ฐฐ๋ถ„๋ฉ๋‹ˆ๋‹ค. (PG์‚ฌ ์ˆ˜์ˆ˜๋ฃŒ ๋ฐ ์›์ฒœ์„ธ ์ œ์™ธ) ๋‹จ, ํ•ด๋‹น ์ฑ…(์ „์ž์ฑ… ์•„๋‹Œ ์œ„ํ‚ค๋…์Šค ์ฑ…)์— ์œ„ํ‚ค๋…์Šค์˜ ๊ตฌ๊ธ€ ๊ด‘๊ณ ๋ฅผ ๊ฒŒ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ „์ž์ฑ…๊ณผ ์˜จ๋ผ์ธ ์ฝ˜ํ…์ธ ๊ฐ€ 100% ๋™์ผํ•œ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. (์ „์ž์ฑ…์—๋งŒ ์ˆ˜๋ก๋˜๋Š” ๋น„๊ณต๊ฐœ ์ฝ˜ํ…์ธ ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.) ํ™œ์„ฑํ™”๋œ ๊ตฌ๊ธ€ ์• ๋“œ์„ผ์Šค ๊ณ„์ •์ด ์—†์œผ๋ฉด ์‚ฌ์šฉํ•˜์„ธ์š”. ํ•œ ๋‹ฌ์— ํ•œ ๋ฒˆ ์ธ์„ธ๊ฐ€ ์ง€๊ธ‰๋ฉ๋‹ˆ๋‹ค. (์นด๋“œ์‚ฌ ์ •์‚ฐ์ด ๋งˆ๋ฌด๋ฆฌ๋œ ์‹œ์ ์ธ ์ต์›” ์ค‘์ˆœ ~ ๋ง๊ฒฝ์— ์ง€๊ธ‰) 05-5 ์ „์ž์ฑ… ์ž‘์„ฑ ์‹œ ์ฃผ์˜ํ•  ์  ์ „์ž์ฑ… ์ž‘์„ฑ ์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค. ํ—ค๋”ฉ ํƒœ๊ทธ ์—”ํ„ฐํ‚ค ์ค„ ๋ฐ”๊ฟˆ ๋ชจ๋“œ ํ•ด์ œ ํŒ ๋ธ”๋ก ๊ฒฝ๋กœ๋ช… ๊ฐ ์ฃผ๋ช… ํ—ค๋”ฉ ํƒœ๊ทธ ํ—ค๋”ฉ ํƒœ๊ทธ๋Š” 2๋ ˆ๋ฒจ(##)๋ถ€ํ„ฐ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1๋ ˆ๋ฒจ(#) ํ—ค๋”ฉ ํƒœ๊ทธ๋Š” ์ฑ…์˜ ์ฑ•ํ„ฐ๋กœ ๋ถ„๋ฅ˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชฉ์ฐจ๊ฐ€ ์œ„ํ‚ค๋…์Šค์˜ ๋ชฉ์ฐจ์™€ ๋™์ผํ•˜๊ฒŒ ์ƒ์„ฑ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์—”ํ„ฐํ‚ค ์ค„ ๋ฐ”๊ฟˆ ๋ชจ๋“œ ํ•ด์ œ ์œ„ํ‚ค๋…์Šค ์ฑ…์˜ ์„ค์ • ์ค‘์— "์—”ํ„ฐํ‚ค ์ค„ ๋ฐ”๊ฟˆ ๋ชจ๋“œ"๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ „์ž์ฑ… ์ƒ์„ฑ ์‹œ์—๋Š” ์ด ์˜ต์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ „์ž์ฑ…์„ ๋ชฉ์ ์œผ๋กœ ํ•˜๋Š” ์ฑ…์€ ์ด ์˜ต์…˜์„ ํ•ด์ œํ•˜๊ณ  ๋ณธ๋ฌธ์˜ ๋‚ด์šฉ์„ ์žฌ์กฐ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (์ค„ ๋ฐ”๊ฟˆ์ด ์ •์ƒ์ ์œผ๋กœ ํ‘œ์‹œ๋˜๋Š”์ง€ ํ™•์ธ ํ•„์š”) ์—”ํ„ฐํ‚ค ์ค„ ๋ฐ”๊ฟˆ ๋ชจ๋“œ ์„ค์ •์„ ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ์ƒ๊ด€์ด ์—†์Šต๋‹ˆ๋‹ค. (์œ„ํ‚ค๋…์Šค ์ฑ…์˜ ์—”ํ„ฐํ‚ค ์ค„ ๋ฐ”๊ฟˆ ๋ชจ๋“œ์˜ ๊ธฐ๋ณธ ์„ค์ •์€ Off์ž…๋‹ˆ๋‹ค.) ํŒ ๋ธ”๋ก ๋ณธ๋ฌธ์— ํŒ(TIP) ๋ธ”๋ก์„ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด ํŒ ๋ธ”๋ก์˜ ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๊ธธ์ง€ ์•Š์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํŒ ๋ธ”๋ก์˜ ๋‚ด์šฉ์ด ํ•œ ํŽ˜์ด์ง€ ์ด์ƒ์ด๋ฉด ํŒ ๋ธ”๋ก์ด ์ „๋ถ€ ํ‘œ์‹œ๋˜์ง€ ์•Š๊ณ  ์ž˜๋ฆฝ๋‹ˆ๋‹ค. ํŒ ๋ธ”๋ก - https://wikidocs.net/141889 ๊ฒฝ๋กœ๋ช… ์•„๋ž˜์™€ ๊ฐ™์ด ์—ญ์Šฌ๋ž˜์‹œ \ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•œ ๊ฒฝ๋กœ๋ช…์€ ์ฝ”๋“œ ๋ธ”๋ก์œผ๋กœ ์ง€์ •ํ•˜๋“ ๊ฐ€ ์•„๋‹ˆ๋ฉด /์ฒ˜๋Ÿผ ์Šฌ๋ž˜์‹œ ๋ฌธ์ž๋กœ ๋ฐ”๊พธ์–ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ํŒŒ์ผ๋ช… ์˜ˆ์‹œ - C:\mydir\abc.txt, C:/mydir/abc.txt, C:/mydir/abc.txt ์ž˜๋ชป๋œ ํŒŒ์ผ๋ช… ์˜ˆ์‹œ - C:\mydir\abc.txt ๊ฐ ์ฃผ๋ช… ๋งŒ์•ฝ ์ฑ…์—์„œ ๊ฐ์ฃผ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๊ฐ์ฃผ ๋ช…์€ ๋ชจ๋‘ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”. ์ „์ž์ฑ… ์ƒ์„ฑ ์‹œ์—๋Š” ์ฑ…์— ์žˆ๋Š” ๋ชจ๋“  ํŽ˜์ด์ง€๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์„œ๋กœ ํ†ตํ•ฉํ•˜์—ฌ ์ž‘์—…ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋™์ผํ•œ ์ด๋ฆ„์˜ ๊ฐ์ฃผ๊ฐ€ ํ•˜๋‚˜์˜ ๋ฌธ์„œ์— ์—ฌ๋Ÿฌ ๊ฐœ ์กด์žฌํ•  ๊ฒฝ์šฐ ์—‰๋šฑํ•œ ๊ฐ์ฃผ๊ฐ€ ์ฐธ์กฐ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 05-6 ์ „์ž์ฑ… FAQ ์œ„ํ‚ค๋…์Šค ์ „์ž์ฑ…๋งŒ์˜ ํŠน๋ณ„ํ•จ์ด ์žˆ๋‚˜์š”? ์œ„ํ‚ค๋…์Šค๋Š” ์ถœํŒ์‚ฌ์ธ๊ฐ€์š”? ์œ„ํ‚ค๋…์Šค์—์„œ ์ „์ž์ฑ…์„ ๋งŒ๋“ค๋ ค๋ฉด ๋น„์šฉ์ด ๋“œ๋‚˜์š”? ์ œ์ž‘๋˜๋Š” ์ „์ž์ฑ…(e-book)์˜ ์ข…๋ฅ˜๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”? ํŒ๋งคํ•˜๋Š” ์ „์ž์ฑ…์— ISBN์ด ๋ฐœ๊ธ‰๋˜๋‚˜์š”? PDF์— ์•”ํ˜ธํ™”๊ฐ€ ์ ์šฉ๋˜๋‚˜์š”? ์ฑ…(e-book) ํŒ๋งค๋ฅผ ๋ชฉ์ ์œผ๋กœ ์ž‘์„ฑํ•œ ์ฑ…์˜ ๋‚ด์šฉ ์ „์ฒด๋ฅผ ์œ„ํ‚ค๋…์Šค์— ๊ณต๊ฐœํ•ด์•ผ ํ•˜๋‚˜์š”? ์œ„ํ‚ค๋…์Šค ์ „์ž์ฑ…๋งŒ์˜ ํŠน๋ณ„ํ•จ์ด ์žˆ๋‚˜์š”? ์œ„ํ‚ค๋…์Šค์˜ ์ „์ž์ฑ…์€ ๊ตฌ๋งค ์‹œ์ ์— ์œ„ํ‚ค๋…์Šค์˜ ์ฑ… ๋‚ด์šฉ ๊ทธ๋Œ€๋กœ ์‹ค์‹œ๊ฐ„ ์ œ์ž‘๋˜์–ด ๋ฐœ์†ก๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ด์•„์žˆ๋Š” ์ „์ž์ฑ…์ด ๋ฉ๋‹ˆ๋‹ค. ์ฑ…์˜ ๋‚ด์šฉ์ด ๊ณ ์ •๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜์˜ ์ˆ˜์ •, ๋‚ด์šฉ์˜ ์ถ”๊ฐ€ ๋“ฑ์— ์™„์ „ํžˆ ์ž์œ ๋กญ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์ถœํŒ์‚ฌ์ธ๊ฐ€์š”? ๋„ค, ์ •์‹ ์ถœํŒ์‚ฌ์ž…๋‹ˆ๋‹ค. (์ถœํŒ์‚ฌ ์‹ ๊ณ ๋ฒˆํ˜ธ : 251002022000013, ์ƒํ˜ธ: ์œ„ํ‚ค๋…์Šค) ํ˜„์žฌ๋Š” ์ „์ž์ฑ…๋งŒ ์ทจ๊ธ‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์—์„œ ์ „์ž์ฑ…์„ ๋งŒ๋“ค๋ ค๋ฉด ๋น„์šฉ์ด ๋“œ๋‚˜์š”? ์•„๋‹ˆ์š”, ๋น„์šฉ์ด ๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฌด๋ฃŒ๋กœ ์ „์ž์ฑ…์„ ๋งŒ๋“ค์–ด ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ „์ž์ฑ…์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ ๋‹นํ•œ ๋ถ„๋Ÿ‰๊ณผ ์–‘์งˆ์˜ ์ฝ˜ํ…์ธ ๋ฅผ ๊ฐ–์ถ”๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ œ์ž‘๋˜๋Š” ์ „์ž์ฑ…(e-book)์˜ ์ข…๋ฅ˜๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”? PDF ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค. (PC์—์„œ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ํ”„๋ฆฐํ„ฐ๋กœ ์ธ์‡„, ๋˜๋Š” ์•„์ดํŒจ๋“œ, ๊ฐค๋Ÿญ์‹œํƒญ ๋“ฑ์˜ ๋ชจ๋ฐ”์ผ ๊ธฐ๊ธฐ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) ํŒ๋งคํ•˜๋Š” ์ „์ž์ฑ…์— ISBN์ด ๋ฐœ๊ธ‰๋˜๋‚˜์š”? ๋„ค, ์œ„ํ‚ค๋…์Šค์—์„œ ํŒ๋งคํ•˜๋Š” ์ „์ž์ฑ…์€ ๋ชจ๋‘ ISBN์ด ๋ฌด๋ฃŒ๋กœ ๋ฐœ๊ธ‰๋ฉ๋‹ˆ๋‹ค. ISBN ์ด๋ž€? PDF์— ์•”ํ˜ธํ™”๊ฐ€ ์ ์šฉ๋˜๋‚˜์š”? ์œ„ํ‚ค๋…์Šค๋Š” DRM๊ณผ ๊ฐ™์€ ์•”ํ˜ธํ™”๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹จ, ํŒ๋งค๋˜๋Š” ์ „์ž ๋ฌธ์„œ์˜ ๋ชจ๋“  ํŽ˜์ด์ง€ ํ•˜๋‹จ์— ๊ตฌ๋งค์ž ์ด๋ฆ„๊ณผ ์ด๋ฉ”์ผ์„ ์›Œํ„ฐ๋งˆํฌ์‹์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ฑ…(e-book) ํŒ๋งค๋ฅผ ๋ชฉ์ ์œผ๋กœ ์ž‘์„ฑํ•œ ์ฑ…์˜ ๋‚ด์šฉ ์ „์ฒด๋ฅผ ์œ„ํ‚ค๋…์Šค์— ๊ณต๊ฐœํ•ด์•ผ ํ•˜๋‚˜์š”? ์œ„ํ‚ค๋…์Šค์— ๋น„๊ณต๊ฐœ ์ฝ˜ํ…์ธ ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  PDF์—๋งŒ ๋น„๊ณต๊ฐœ ์ฝ˜ํ…์ธ ๋ฅผ ๊ณต๊ฐœํ•˜์—ฌ ํŒ๋งคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 06 ์œ„ํ‚ค๋…์Šค ์•ฑ(App) ์œ„ํ‚ค๋…์Šค ์•ˆ๋“œ๋กœ์ด๋“œ, ์•„์ดํฐ ์•ฑ์ด ์ถœ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. (2021๋…„ 10์›”) ์ด์ œ ๋ชจ๋ฐ”์ผ ํ™˜๊ฒฝ์—์„œ ์œ„ํ‚ค๋…์Šค ์ฑ…์„ ๋ณด๋‹ค ํŽธ๋ฆฌํ•˜๊ฒŒ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (๊ฐค๋Ÿญ์‹œ ํƒญ์ด๋‚˜ ์•„์ดํŒจ๋“œ ๋“ฑ์—์„œ๋„ ์ž˜ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.) ์Šคํฌ๋ฆฐ์ˆ [์ฑ… ๊ฒ€์ƒ‰ํ•˜๊ธฐ] [์ฑ… ์ฝ๊ธฐ] [๋ชฉ์ฐจ ๋ณด๊ธฐ] [๋Œ“๊ธ€ ๋ณด๊ธฐ] [์ฑ…๊ฐˆํ”ผ] 06-1 ์•ฑ ๋‹ค์šด๋กœ๋“œ ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ์•„์ดํฐ ์•ฑ ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๋‹ค์Œ์˜ QR ์ฝ”๋“œ๋กœ ์•ฑ์„ ๋‚ด๋ ค๋ฐ›์œผ์„ธ์š”. ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๋‹ค์šด๋กœ๋“œ ์ฃผ์†Œ: ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๋‹ค์šด๋กœ๋“œ ์•„์ดํฐ ์•ฑ App Store์—์„œ "์œ„ํ‚ค๋…์Šค"๋กœ ๊ฒ€์ƒ‰ ๋˜๋Š” ๋‹ค์Œ์˜ QR ์ฝ”๋“œ๋กœ ์•ฑ์„ ๋‚ด๋ ค๋ฐ›์œผ์„ธ์š”. ์•„์ดํฐ ์•ฑ ๋‹ค์šด๋กœ๋“œ ์ฃผ์†Œ: ์•„์ดํฐ ์•ฑ ๋‹ค์šด๋กœ๋“œ 06-2 ์•ฑ ๊ฐ€์ด๋“œ ์ฑ… ์กฐํšŒ ์ฑ… ๋ณด๊ธฐ ๋ชฉ์ฐจ ์ฑ…๊ฐˆํ”ผ ๊ธ€์ž ํฌ๊ธฐ ์กฐ์ ˆ ์ถ”์ฒœ ๋Œ“๊ธ€ ๋Œ“๊ธ€ ์กฐํšŒ ๋Œ“๊ธ€ ์ถ”๊ฐ€ ๋Œ“๊ธ€ ์ˆ˜์ •๊ณผ ์‚ญ์ œ ๋กœ๊ทธ์ธ๊ณผ ๋กœ๊ทธ์•„์›ƒ ์œ„ํ‚ค๋…์Šค ์•ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฑ… ์กฐํšŒ ์œ„ํ‚ค๋…์Šค ์•ฑ์„ ์Šค๋งˆํŠธํฐ์— ์„ค์น˜ํ•œ ํ›„ ์•ฑ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ์ƒ๋‹จ์˜ ์ฝค๋ณด ๋ฐ•์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ "์ถ”์ฒœ์ˆœ, ์ธ๊ธฐ์ˆœ, ์ตœ์‹ ์ˆœ"์œผ๋กœ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ๋‹จ์˜ "์ฑ… ๊ฒ€์ƒ‰" ํ…์ŠคํŠธ ์ฐฝ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฑ…์„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ… ์ œ๋ชฉ๊ณผ ์ €์ž๋ช…์œผ๋กœ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ํŽ˜์ด์ง€์— 50๊ฐœ์”ฉ ์ถœ๋ ฅ๋˜๋ฉด 50๊ฐœ๊ฐ€ ๋„˜๋Š” ๊ฒฝ์šฐ ์Šคํฌ๋กค์ด ๊ฐ€์žฅ ํ•˜๋‹จ์— ์œ„์น˜ํ•˜๊ฒŒ ๋˜๋ฉด ์ž๋™์œผ๋กœ ๋‹ค์Œ 50๊ฑด์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. ์ฑ… ๋ณด๊ธฐ ์œ„ํ‚ค๋…์Šค์—์„œ ์ฑ…์„ ์กฐํšŒํ•œ ํ›„์— ์ฑ…์„ ์„ ํƒํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. (์•„๋ž˜๋Š” "์ ํ”„ ํˆฌ ํŒŒ์ด์ฌ" ์ฑ…์„ ์„ ํƒํ•œ ํ™”๋ฉด์ž…๋‹ˆ๋‹ค.) ์ฑ…์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ์„ธ ํ™”๋ฉด์ž…๋‹ˆ๋‹ค. ํ•˜๋‹จ์˜ "๋‹ค์Œ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์ฑ…์˜ ์ฒซ ๋ฒˆ์งธ ํŽ˜์ด์ง€๊ฐ€ ์กฐํšŒ๋ฉ๋‹ˆ๋‹ค. ํ•˜๋‹จ์˜ "์ด์ „", "๋‹ค์Œ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ด์ „ ํŽ˜์ด์ง€์™€ ๋‹ค์Œ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชฉ์ฐจ ์ฑ… ๋ณด๊ธฐ ํ™”๋ฉด์—์„œ ๋‹ค์Œ์ฒ˜๋Ÿผ ์šฐ์ธก ํ•˜๋‹จ์˜ ์•„์ด์ฝ˜์„ ๋ˆŒ๋Ÿฌ ๋ณด์„ธ์š”. ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ชฉ์ฐจ๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ๋ชฉ์ฐจ๋ฅผ ์œ„์•„๋ž˜๋กœ ์›€์ง์—ฌ ์›ํ•˜๋Š” ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ…๊ฐˆํ”ผ ์ฑ…๊ฐˆํ”ผ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ์œ„ํ‚ค๋…์Šค์˜ ํŠน์ • ์ฑ…์ด๋‚˜ ํŽ˜์ด์ง€๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ…๊ฐˆํ”ผ ๊ธฐ๋Šฅ์€ "์ฑ… ์กฐํšŒ", "์ฑ… ๋ณด๊ธฐ" ํ™”๋ฉด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ… ๋ณด๊ธฐ ํ™”๋ฉด์—์„œ ๋‹ค์Œ์ฒ˜๋Ÿผ ์šฐ์ธก ์ƒ๋‹จ์˜ ์•„์ด์ฝ˜์„ ๋ˆŒ๋Ÿฌ ๋ณด์„ธ์š”. ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ์ด ํ™”๋ฉด์—์„œ "์ถ”๊ฐ€" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ˜„์žฌ ํŽ˜์ด์ง€ ๋˜๋Š” ํ˜„์žฌ ์ฑ…์ด ์ฑ…๊ฐˆํ”ผ์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ฑ…๊ฐˆํ”ผ๊ฐ€ ์ถ”๊ฐ€๋œ ํ™”๋ฉด์ž…๋‹ˆ๋‹ค. ์ถ”๊ฐ€๋œ ์ฑ…๊ฐˆํ”ผ๋ฅผ ์„ ํƒํ•˜๋ฉด ํ•ด๋‹น ์ฑ… ๋˜๋Š” ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์ฑ…๊ฐˆํ”ผ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” ์ฑ…๊ฐˆํ”ผ์˜ ์ขŒ์ธก ์ฒดํฌ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ฒดํฌํ•œ ํ›„ "์‚ญ์ œ" ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ํ•œ๊บผ๋ฒˆ์— ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธ€์ž ํฌ๊ธฐ ์กฐ์ ˆ "์ฑ… ๋ณด๊ธฐ" ํ™”๋ฉด์˜ ๊ธ€์ž ํฌ๊ธฐ๋ฅผ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ํ™ˆ ํ™”๋ฉด("์ฑ… ์กฐํšŒ")์œผ๋กœ ์ด๋™ํ•œ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด "์„ค์ •" ์•„์ด์ฝ˜์„ ๋ˆŒ๋Ÿฌ๋ณด์„ธ์š”. ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ์Šค๋งˆํŠธํฐ ๋˜๋Š” ์Šค๋งˆํŠธ ํŒจ๋“œ์— ์–ด์šธ๋ฆฌ๋Š” ๊ธ€์ž ํฌ๊ธฐ๋ฅผ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”์ฒœ ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” ์ฑ…์„ ์ถ”์ฒœํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด "์ฑ… ํ™”๋ฉด"์—์„œ ์šฐ์ธก ์ƒ๋‹จ์˜ ๋‹ค์Œ ์•„์ด์ฝ˜์„ ๋ˆŒ๋Ÿฌ ์ถ”์ฒœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ถ”์ฒœ ์•„์ด์ฝ˜์€ "์ฑ… ํ™”๋ฉด"์—๋งŒ ๋…ธ์ถœ๋˜๊ณ  "ํŽ˜์ด์ง€ ํ™”๋ฉด"์—๋Š” ๋…ธ์ถœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.) ๋Œ“๊ธ€ ํŽ˜์ด์ง€์˜ ๋Œ“๊ธ€์„ ์ฝ๊ฑฐ๋‚˜ ๋Œ“๊ธ€์„ ์ž‘์„ฑ, ์ˆ˜์ •, ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. "ํŽ˜์ด์ง€ ํ™”๋ฉด"์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์•„์ด์ฝ˜์„ ๋ˆ„๋ฅด๋ฉด ์ž‘์„ฑ๋œ ๋Œ“๊ธ€์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ“๊ธ€ ์กฐํšŒ ๋‹ค์Œ์€ ๋Œ“๊ธ€ ์กฐํšŒ ํ™”๋ฉด์ž…๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ณธ์ธ์ด ์ž‘์„ฑํ•œ ๋Œ“๊ธ€์€ "๋…ธ๋ž€์ƒ‰" ๋ฐฐ๊ฒฝ์œผ๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋Œ“๊ธ€ ์ถ”๊ฐ€ ๋Œ“๊ธ€ ์กฐํšŒ ํ™”๋ฉด์—์„œ ์šฐ์ธก ํ•˜๋‹จ์˜ "๋Œ“๊ธ€ ์ถ”๊ฐ€" ์•„์ด์ฝ˜์„ ๋ˆ„๋ฅด๋ฉด ๋Œ“๊ธ€์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ“๊ธ€ ์ž‘์„ฑ์„ ์œ„ํ•ด์„œ๋Š” ์œ„ํ‚ค๋…์Šค ๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋กœ๊ทธ์ธ๋˜์–ด ์žˆ์ง€ ์•Š์€ ์ƒํƒœ๋ผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋กœ๊ทธ์ธ ํ™”๋ฉด์ด ๋จผ์ € ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์œ„ํ‚ค๋…์Šค ๊ณ„์ •์ด ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ํšŒ์› ๊ฐ€์ž… ํ›„์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธ์ด ์™„๋ฃŒ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณธ์ธ์ด ์ž‘์„ฑํ•œ ๋Œ“๊ธ€์€ ๋…ธ๋ž€์ƒ‰ ๋ฐฐ๊ฒฝ์œผ๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋Œ“๊ธ€ ์ž‘์„ฑ์„ ์œ„ํ•ด ๋‹ค์‹œ "๋Œ“๊ธ€ ์ž‘์„ฑ" ์•„์ด์ฝ˜์„ ๋ˆ„๋ฅด๋ฉด ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ๋Œ“๊ธ€์„ ์ž…๋ ฅ ํ›„ ์ €์žฅํ•˜๋ฉด ๋Œ“๊ธ€์ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋Œ“๊ธ€ ์ˆ˜์ •๊ณผ ์‚ญ์ œ ์ž‘์„ฑํ•œ ๋Œ“๊ธ€์„ ์ˆ˜์ •ํ•˜๋ ค๋ฉด ๋ณธ์ธ์ด ์ž‘์„ฑํ•œ ๋Œ“๊ธ€์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋Œ“๊ธ€์„ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋Š” ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ๋Œ“๊ธ€์„ ์ˆ˜์ •ํ•˜๋ ค๋ฉด ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•œ ํ›„ "์ €์žฅ" ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ €์žฅํ•˜๊ณ , ๋Œ“๊ธ€์„ ์‚ญ์ œํ•˜๋ ค๋ฉด "์‚ญ์ œ" ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธ๊ณผ ๋กœ๊ทธ์•„์›ƒ ๋Œ“๊ธ€ ์กฐํšŒ ํ™”๋ฉด์—์„œ ์šฐ์ธก ์ƒ๋‹จ์˜ ์•„์ด์ฝ˜์„ ํ†ตํ•ด ์œ„ํ‚ค๋…์Šค ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธ์ด ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋กœ๊ทธ์•„์›ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋กœ๊ทธ์•„์›ƒ ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. 06-3 ์•ฑ ์ฃผ์˜ ์‚ฌํ•ญ ์•ฑ ๊ฐ€๋กœ ์Šคํฌ๋กค ๋ฌธ์ œ ์•ฑ ๊ฐ€๋กœ ์Šคํฌ๋กค ๋ฌธ์ œ ์œ„ํ‚ค๋…์Šค ์•ฑ์œผ๋กœ ์ฑ…์„ ๋ณผ ๋•Œ ๊ฐ€๋กœ ์Šคํฌ๋กค์ด ์ƒ๊ธฐ๋ฉด ๊ฐ€๋…์„ฑ์ด ๋งค์šฐ ๋–จ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฐ€๋กœ ์Šคํฌ๋กค์ด ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ ๋‹ค์Œ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. mathjax๋ฅผ ์ด์šฉํ•œ ๊ธด ์ˆ˜์‹ ์‚ฌ์šฉ ๊ฐ€๋กœ ์‚ฌ์ด์ฆˆ๊ฐ€ ํฌ๊ฒŒ ์ •ํ•ด์ ธ ์žˆ๋Š” ๊ด‘๊ณ ๋ฅผ ๊ฒŒ์‹œ ์ˆ˜์‹์˜ ๊ฒฝ์šฐ๋Š” ๊ฐ€๊ธ‰์  ์ด๋ฏธ์ง€ ์บก์ฒ˜ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ชจ๋ฐ”์ผ ๊ด‘๊ณ ๋Š” ๋ฐ˜์‘ํ˜•์œผ๋กœ ๋ฐ”๊พธ๋ฉด ์œ„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 06-9 ์•ฑ ๋ณ€๊ฒฝ ์ด๋ ฅ 1.1.0 (2023๋…„ 5์›”) 1.0.3 (2021๋…„ 10์›”) 1.1.0 (2023๋…„ 5์›”) ์œ„ํ‚ค๋…์Šค API ๋ณ€๊ฒฝ์— ๋”ฐ๋ฅธ ์ˆ˜์ • ํ”Œ๋Ÿฌํ„ฐ ๋ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ์ ์šฉ ๋™์˜์ƒ ํ˜•ํƒœ์˜ ์• ๋“œ์„ผ์Šค ๋ณด์ด์ง€ ์•Š๋˜ ๋ฌธ์ œ ํ•ด๊ฒฐ 1.0.3 (2021๋…„ 10์›”) ์œ„ํ‚ค๋…์Šค ์•ฑ (์œ„ํ‚ค๋…์Šค ๋ฆฌ๋”) ์ถœ์‹œ 07 ์œ„ํ‚ค๋…์Šค API ์œ„ํ‚ค๋…์Šค๋Š” Open API๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” ์‹œํ—˜ ๊ธฐ๊ฐ„์œผ๋กœ ์ฑ— GPT ์šฉ API์™€ ์กฐํšŒ ์„œ๋น„์Šค๋งŒ ์šด์˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ URL์—์„œ API๋ฅผ ํ™•์ธํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค API ์ŠคํŽ™ - https://wikidocs.net/api/v1/docs ์œ„ํ‚ค๋…์Šค API ์ŠคํŽ™ (Readonly) - https://wikidocs.net/api/v1/redoc 08 ์œ„ํ‚ค๋…์Šค์™€ ์ฑ— GPT ์œ„ํ‚ค๋…์Šค์™€ ์ฑ— GPT์˜ ์—ฐ๊ณ„ ๋ฐฉ๋ฒ•์— ๊ด€ํ•ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ฑ— GPT์—์„œ ์œ„ํ‚ค๋…์Šค ํ”Œ๋Ÿฌ๊ทธ์ธ ์‚ฌ์šฉํ•˜๊ธฐ ์ฑ— GPT๋กœ ์œ„ํ‚ค๋…์Šค ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ ์ธ๊ณต์ง€๋Šฅ์œผ๋กœ ์ฑ… ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ 08-1 ์ฑ— GPT์—์„œ ์œ„ํ‚ค๋…์Šค ํ”Œ๋Ÿฌ๊ทธ์ธ ์‚ฌ์šฉํ•˜๊ธฐ ์ฑ— GPT์— ์œ„ํ‚ค๋…์Šค ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ— GPT์—์„œ ํ”Œ๋Ÿฌ๊ทธ์ธ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ‚ค๋…์Šค ํ”Œ๋Ÿฌ๊ทธ์ธ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ‚ค๋…์Šค ํ”Œ๋Ÿฌ๊ทธ์ธ ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ ์œ„ํ‚ค๋…์Šค ์ฑ… ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ‚ค๋…์Šค ์ฑ… ๋งŒ๋“ค๊ธฐ ๋‚ด ์ฑ…์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์ฑ— GPT์—์„œ ํ”Œ๋Ÿฌ๊ทธ์ธ ์‚ฌ์šฉํ•˜๊ธฐ ์ฑ— GPT๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”Œ๋Ÿฌ๊ทธ์ธ ๋“ฑ๋ก์€ ํ˜„์žฌ ์ฑ— GPT PLUS(์œ ๋ฃŒ) ์‚ฌ์šฉ์ž๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ํ”Œ๋Ÿฌ๊ทธ์ธ ๋“ฑ๋กํ•˜๊ธฐ ์—ฌ๊ธฐ์„œ "No plugin enabled"๋ฅผ ๋ˆŒ๋Ÿฌ ํ”Œ๋Ÿฌ๊ทธ์ธ ๋“ฑ๋ก ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ํ”Œ๋Ÿฌ๊ทธ์ธ ์Šคํ† ์–ด์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด "wikidocs"๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. "Install" ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์œ„ํ‚ค๋…์Šค ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ํ”Œ๋Ÿฌ๊ทธ์ธ ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ ์œ„ํ‚ค๋…์Šค ์ฑ… ๊ฒ€์ƒ‰ํ•˜๊ธฐ ๊ทธ๋Ÿฌ๋ฉด ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์งˆ๋ฌธํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ— GPT๋Š” ์œ„ํ‚ค๋…์Šค์˜ ์–ด๋–ค API๋ฅผ ์จ์•ผ ํ•˜๋Š”์ง€ ์ž์ฒด์ ์œผ๋กœ ํŒ๋‹จํ•˜์—ฌ ์ž˜ ํ˜ธ์ถœํ•ด ์ค๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์ฑ… ๋งŒ๋“ค๊ธฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฑ…์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฑ…์„ ์ƒ์„ฑํ•œ ํ›„์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŽ˜์ด์ง€๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž‘์„ฑํ•œ ํŽ˜์ด์ง€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‚ด ์ฑ…์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์ž‘์„ฑํ•œ ์ฑ…์˜ ์ €์ž๋Š” ChatGPT์ž…๋‹ˆ๋‹ค. ์ฑ— GPT๊ฐ€ ๋งŒ๋“  ์ฑ…์„ ๋‚ด ์ฑ…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋ฉด ์ฑ— GPT๊ฐ€ ๋งŒ๋“  ์œ„ํ‚ค๋…์Šค ์ฑ…์œผ๋กœ ์ด๋™ํ•œ ํ›„ "๋‚ด ์ฑ…์œผ๋กœ ์ „ํ™˜ํ•˜๊ธฐ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. (๋‹จ, ์œ„ํ‚ค๋…์Šค์— ๋กœ๊ทธ์ธํ•œ ํ›„์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.) ์ฑ— GPT๊ฐ€ ๋งŒ๋“  ์ฑ…์€ 1์‹œ๊ฐ„ ๋™์•ˆ ๋ณ€๊ฒฝ์ด ์—†์œผ๋ฉด ์ž๋™์œผ๋กœ ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ญ์ œ๋˜๊ธฐ ์ „์— "๋‚ด ์ฑ…์œผ๋กœ ์ „ํ™˜ํ•˜๊ธฐ"๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ด ์ฑ…์œผ๋กœ ๋ฐ”๊พธ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ๋‚ด ์ฑ…์œผ๋กœ ์ „ํ™˜๋œ ํ›„์—๋Š” ์ฑ— GPT๋กœ ์ฑ…์˜ ํŽ˜์ด์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 08-2 ์ฑ— GPT๋กœ ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ (์‹œํ—˜ ์ค‘) ์œ„ํ‚ค๋…์Šค์—์„œ ์ฑ— GPT๋ฅผ ์ด์šฉํ•˜์—ฌ ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ํ˜„์žฌ ์‹œํ—˜ ์ค‘์ž…๋‹ˆ๋‹ค. ์ฑ— GPT๋ฅผ ์ด์šฉํ•˜์—ฌ ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฑ… ์ˆ˜์ • ํ™”๋ฉด์˜ ์ขŒ์ธก ๋ชฉ์ฐจ์—์„œ "AI" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ์ œ๋ชฉ, ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•œ ํ›„ "์ƒ์„ฑํ•˜๊ธฐ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ์ƒ์„ฑ์—๋Š” ๋ณดํ†ต 30์ดˆ ์ด์ƒ์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค. 08-3 ์ธ๊ณต์ง€๋Šฅ์œผ๋กœ ์ฑ… ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ (์‹œํ—˜ ์ค‘) ์œ„ํ‚ค๋…์Šค์—์„œ ์ธ๊ณต์ง€๋Šฅ(DALL ยท E)์„ ์ด์šฉํ•˜์—ฌ ์ฑ… ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ํ˜„์žฌ ์‹œํ—˜ ์ค‘์ž…๋‹ˆ๋‹ค. ์ธ๊ณต์ง€๋Šฅ์„ ์ด์šฉํ•˜์—ฌ ์ฑ… ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฑ… ์ˆ˜์ • ํ™”๋ฉด์—์„œ "์ด๋ฏธ์ง€ ์ƒ์„ฑ(AI)" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์ƒ์„ฑ์„ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•œ ํ›„ "๋งŒ๋“ค๊ธฐ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ์ƒ์„ฑ์—๋Š” ๋ณดํ†ต 5์ดˆ ์ด์ƒ์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค. 09 ์œ„ํ‚ค๋…์Šค ์šฉ์–ด ์‚ฌ์ „ ์œ„ํ‚ค๋…์Šค ์šฉ์–ด ์‚ฌ์ „์ด๋ž€? ์šฉ์–ด ๋งํฌ ๋งŒ๋“ค๊ธฐ ์—๋””ํ„ฐ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ‚ค๋…์Šค ์šฉ์–ด ์‚ฌ์ „์ด๋ž€? ์œ„ํ‚ค๋…์Šค ์šฉ์–ด ์‚ฌ์ „์€ ์œ„ํ‚ค๋…์Šค์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์šฉ์–ด๋ฅผ ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์šฉ์–ด ์‚ฌ์ „: https://wikidocs.net/wiki/ ์šฉ์–ด ๋งํฌ ๋งŒ๋“ค๊ธฐ ์œ„ํ‚ค๋…์Šค์—์„œ ์šฉ์–ด ์‚ฌ์ „์˜ ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŽ˜์ด์ง€ ํŽธ์ง‘ ์‹œ ์šฉ์–ด์˜ ์ขŒ์šฐ๋ฅผ [[" ์™€ "]]๋กœ ๊ฐ์‹ธ์„œ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ: [["์›Œ๋“œ ์ปค๋‹ํ–„"]] ์‹คํ–‰ ๊ฒฐ๊ณผ: ์›Œ๋“œ ์ปค๋‹ํ–„ (๋งํฌ๋ฅผ ํด๋ฆญํ•ด ๋ณด์„ธ์š”) ์šฉ์–ด ๋งํฌ๋Š” ์œ„์™€ ๊ฐ™์ด ๊ฐˆ์ƒ‰ ๋งํฌ๋กœ ํ‘œ์‹œ๋˜๋ฉฐ ํ•ด๋‹น ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด ์šฉ์–ด ์‚ฌ์ „์— ๋“ฑ๋ก๋œ ๋‚ด์šฉ์„ ํŒ์—…์œผ๋กœ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํŒ์—… ์ƒ๋‹จ์˜ ์šฉ์–ด ๋งํฌ๋ฅผ ๋ˆ„๋ฅด๋ฉด ์šฉ์–ด ์‚ฌ์ „์˜ ํ•ด๋‹น ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์ด๋™ํ•œ ํ›„ ํ•„์š”ํ•œ ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒ์—…์„ ๋‹ซ์œผ๋ ค๋ฉด ๋‹ค์‹œ ์šฉ์–ด ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๊ฑฐ๋‚˜ ํŒ์—…์ฐฝ์˜ x ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์šฉ์–ด ์‚ฌ์ „์— ์—†๋Š” ์šฉ์–ด์ธ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์šฉ์–ด ์‚ฌ์ „์œผ๋กœ ์ด๋™ํ•˜์—ฌ ์‹ ๊ทœ ์šฉ์–ด๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—๋””ํ„ฐ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์—๋””ํ„ฐ์˜ ์•„์ด์ฝ˜์„ ํ†ตํ•ด์„œ๋„ ์‰ฝ๊ฒŒ ์šฉ์–ด ๋งํฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์„ ํƒํ•œ ํ›„ ์—๋””ํ„ฐ์˜ ๋‹ค์Œ ์•„์ด์ฝ˜์„ ๋ˆ„๋ฅด๋ฉด ๋‹จ์–ด์˜ ์ขŒ์šฐ์— [[" ์™€ "]] ์„ ์ž๋™์œผ๋กœ ๋„ฃ์–ด ์ค๋‹ˆ๋‹ค. 98 ์•„๋ฌด๊ฑฐ๋‚˜ ์งˆ๋ฌธ ์œ„ํ‚ค๋…์Šค์˜ ๊ถ๊ธˆํ•œ ์‚ฌํ•ญ์— ๋Œ€ํ•ด์„œ ์งˆ๋ฌธ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด๊ณณ์— ๋Œ“๊ธ€๋กœ ์งˆ๋ฌธ์„ ํ•ด ์ฃผ์„ธ์š”. ๋˜๋Š” ๋‹ค์Œ ์œ„ํ‚ค๋…์Šค ๋””์Šค์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์„ธ์š”. https://discord.gg/zPERvk7pza 99 ๊ธฐํƒ€ ์œ„ํ‚ค๋…์Šค์˜ ๊ธฐํƒ€ ์‚ฌํ•ญ 99-1 ์ €์ž๋‹˜์„ ๋ชจ์‹ญ๋‹ˆ๋‹ค ์ €์ž๋‹˜, ์œ„ํ‚ค๋…์Šค๊ฐ€ ์ €์ž๋‹˜์„ ์• ํƒ€๊ฒŒ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€์†์ ์ธ ์ง‘ํ•„ ๊ด€์‹ฌ ์žˆ๋Š” ์ฃผ์ œ์— ๋Œ€ํ•˜์—ฌ ์˜ค๋žœ ์‹œ๊ฐ„์„ ๋‘๊ณ  ์ง€์†์ ์œผ๋กœ ์ง‘ํ•„ํ•˜์‹ค ์ €์ž๋ถ„์„ ๋ชจ์‹ญ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์—์„œ ๋งŒ๋“œ๋Š” ์ฑ…์€ ๊ณ„์†ํ•ด์„œ ๋‹ค๋“ฌ๊ณ  ๋ฐœ์ „์‹œ์ผœ ๋‚˜๊ฐ€๋Š” ์ฑ…์ž…๋‹ˆ๋‹ค. ์†Œ์…œ ์œ„ํ‚ค๋…์Šค์—์„œ ์ฑ…์„ ์“ฐ๋ฉด ์žฌ๋ฏธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ“๊ธ€๊ณผ ํ”ผ๋“œ๋ฐฑ์„ ํ†ตํ•ด ์ €์ž์™€ ๋…์ž ๊ฐ„ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฑ…์˜ ๋‚ด์šฉ์ด ์ €์ž์™€ ๋…์ž ๊ฐ„ ์ƒํ˜ธ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์„ ํ†ตํ•ด ์ ์  ๋” ์ข‹์€ ๋ฐฉํ–ฅ์œผ๋กœ ์ˆ˜๋ ดํ•ด ๋‚˜๊ฐ‘๋‹ˆ๋‹ค. ๊ด‘๊ณ  ์ˆ˜์ต ์ž‘์„ฑํ•œ ์ฑ…์— ์ €์ž์˜ ๊ด‘๊ณ (์˜ˆ:๊ตฌ๊ธ€ ๊ด‘๊ณ ) ๋˜๋Š” ์œ„ํ‚ค๋…์Šค์˜ ํฌ์ธํŠธ ๊ด‘๊ณ ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ์ˆ˜์ต์„ ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์˜: <NAME> (<EMAIL>) 99-2 ์œ„ํ‚ค๋…์Šค ์žฅ์• ์ผ์ง€ 2023๋…„ 6์›” 16์ผ 2023๋…„ 6์›” 4์ผ 2022๋…„ 11์›” 12์ผ 2021๋…„ 03์›” 26์ผ 2021๋…„ 03์›” 24์ผ 2021๋…„ 03์›” 17์ผ 2021๋…„ 02์›” 25์ผ 2021๋…„ 02์›” 03์ผ 2023๋…„ 6์›” 16์ผ 16์‹œ 40๋ถ„ ~ 16์‹œ 50๋ถ„, ์„œ๋ฒ„ ๋‹ค์šด ์›์ธ ํŒŒ์•… ์•ˆ ๋จ (AWS ์ง€ํ‘œ์ƒ ์•„๋ฌด ๋ฌธ์ œ ์—†์Œ) ์ธ์Šคํ„ด์Šค ์žฌ์‹œ์ž‘์œผ๋กœ ํ•ด๊ฒฐ ์„œ๋ฒ„ ์—…๊ทธ๋ ˆ์ด๋“œ ์˜ˆ์ • 2023๋…„ 6์›” 4์ผ 15์‹œ~19์‹œ 15๋ถ„, ์„œ๋ฒ„ ๋‹ค์šด ์›์ธ ํŒŒ์•… ์•ˆ ๋จ (AWS ์ง€ํ‘œ์ƒ ์•„๋ฌด ๋ฌธ์ œ ์—†์Œ) ์ธ์Šคํ„ด์Šค ์žฌ์‹œ์ž‘์œผ๋กœ ํ•ด๊ฒฐ ์บ์‹œ๋กœ redis๋ฅผ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์œผ๋กœ ์˜์‹ฌ ์ค‘.. 2022๋…„ 11์›” 12์ผ 19~20์‹œ, 1์‹œ๊ฐ„ ์ •๋„ ์„œ๋ฒ„ ๋‹ค์šด ๋„ ์ปค ์ปจํ…Œ์ด๋„ˆ ์กฐ์ž‘ ์‹ค์ˆ˜ (๊ด€๋ฆฌ์ž ์‹ค์ˆ˜๋กœ ์ธํ•œ ๋ฌธ์ œ) 2021๋…„ 03์›” 26์ผ AWS Lightsail RDS - Postgresql ๋ฌด์‘๋‹ต ํ˜„์ƒ AWS ๊ธฐ์ˆ  ์ง€์› ์˜๋ขฐ ํ˜„์ƒ ์ง€์† ์‹œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—…๊ทธ๋ ˆ์ด๋“œ ํ•„์š” (2021๋…„ 3์›” 31์ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—…๊ทธ๋ ˆ์ด๋“œ ์™„๋ฃŒ) 2021๋…„ 03์›” 24์ผ AWS Lightsail RDS - Postgresql ๋ฌด์‘๋‹ต ํ˜„์ƒ 18์‹œ 30๋ถ„๋ถ€ํ„ฐ ๋ฐœ์ƒ RDS ์ค‘์ง€, ์‹œ์ž‘์œผ๋กœ ํ•ด๊ฒฐ AWS ๊ธฐ์ˆ  ์ง€์› ์š”์ฒญ 2021๋…„ 03์›” 17์ผ AWS Lightsail RDS - Postgresql ๋ฌด์‘๋‹ต ํ˜„์ƒ 22์‹œ 30๋ถ„๋ถ€ํ„ฐ ๋ฐœ์ƒ RDS ์ค‘์ง€, ์‹œ์ž‘์œผ๋กœ ํ•ด๊ฒฐ (์ค‘์ง€, ์‹œ์ž‘ ์‹œ 25๋ถ„๊ฐ€๋Ÿ‰ ์†Œ์š”๋จ, 23์‹œ 05๋ถ„ ~ 23์‹œ 27๋ถ„) ์žฌ๋ถ€ํŒ…์€ ํ•ด๊ฒฐ๋˜์ง€ ์•Š์Œ 2021๋…„ 02์›” 25์ผ AWS ์ธ์Šคํ„ด์Šค ์˜ˆ๊ณ  ์—†์ด ์ค‘์ง€ ํ˜„์ƒ (์›์ธ ํŒŒ์•… ์ค‘) ์ธ์Šคํ„ด์Šค ์‹œ์ž‘์œผ๋กœ ํ•ด๊ฒฐ 2021๋…„ 02์›” 03์ผ AWS Lightsail RDS - Postgresql ๋ฌด์‘๋‹ต ํ˜„์ƒ 30๋ถ„๊ฐ€๋Ÿ‰ RDS ๋ฆฌ๋ถ€ํŒ…์œผ๋กœ ํ•ด๊ฒฐ (๋ฆฌ ๋ถ€ํŒ… ์‹œ 20๋ถ„๊ฐ€๋Ÿ‰ ์†Œ์š”๋จ) 99-3 ์œ„ํ‚ค๋…์Šค ๊ธฐ์ˆ  ์Šคํƒ ์ž์„ธํ•œ ๋ฒ„์ „ ์ •๋ณด๋Š” ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. (๊ฐ€๊ธ‰์  ์ตœ์‹  ๋ฒ„์ „์„ ์œ ์ง€ํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค.) ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด Python 3 ํ”„๋ ˆ์ž„์›Œํฌ Django FastAPI ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค PostgreSQL ์ž‘์—… ํ Celery Redis ํ”„๋ŸฐํŠธ์—”๋“œ JQuery Bootstrap 3 OS Docker Ubuntu ์›น์„œ๋ฒ„ Nginx ๋ฏธ๋“ค์›จ์–ด Gunicorn Uvicorn ํ˜ธ์ŠคํŒ… AWS 99-4 ์œ„ํ‚ค๋…์Šค ์ฃผ์š” ์ด๋ ฅ ์œ„ํ‚ค๋…์Šค History. 2023 RSS ํ”ผ๋“œ ์ œ๊ณต (2023.10.31) ์ฑ… ์ˆ˜์ • ์‹œ reCAPTCHA ์ ์šฉ (2023.10.25) ๋””์Šค์ฝ”๋“œ ์šด์˜ (2023.10.18) ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ๊ฐœ์„  (2023.09.04) ์šฉ์–ด ๋งํฌ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ (2023.08.25) ์œ„ํ‚ค๋…์Šค ์šฉ์–ด ์‚ฌ์ „ ์„œ๋น„์Šค ์ถœ์‹œ (2023.08.24) ๋งค์›” ์ฃผ๋ง๋งˆ๋‹ค ๋„์„œ ์ฆ์ • ์ด๋ฒคํŠธ ์‹ค์‹œ (2023.08.04) ์™ธ๋ถ€ ๋งํฌ์ธ ๊ฒฝ์šฐ ์ƒˆ ์ฐฝ์œผ๋กœ ์—ด๋„๋ก ์ˆ˜์ • (2023.08.04) ๊ณต๊ฐœ ์ฑ… ํ•„ํ„ฐ๋ง(์ตœ์†Œ 5ํŽ˜์ด์ง€ ์ด์ƒ, ์ŠคํŒธ ์ €์ž ๋ฐฉ์ง€) ๊ธฐ๋Šฅ ์ถ”๊ฐ€ (2023.07.12) ์ฑ… ๊ฐ€์ ธ์˜ค๊ธฐ, ์†Œ์Šค์ฝ”๋“œ Copy ๊ธฐ๋Šฅ ์ถ”๊ฐ€ (2023.06.23) AWS ์„œ๋ฒ„ ์—…๊ทธ๋ ˆ์ด๋“œ (2023.06.17) Cloudflare ์ ์šฉ (2023.06.17) ์ฑ— GPT ํ”Œ๋Ÿฌ๊ทธ์ธ ์Šน์ธ (2023.06.11) ๋Œ“๊ธ€ ์‹ ๊ณ  ๊ธฐ๋Šฅ ์ถ”๊ฐ€ (2023.06.01) ์œ„ํ‚ค๋…์Šค ์˜๋ฌธ ํŽ˜์ด์ง€ ์ œ๊ณต (2023.05.17) ์ฑ— GPT์™€์˜ ์—ฐ๊ณ„ (2023.05.14) ์ฑ… ๋ฐฑ์—… ์‹œ ์ด๋ฏธ์ง€๋„ ํ•จ๊ป˜ ๋ฐฑ์—… (2023.04.27) ์ œ๋ชฉ ์—ญ์ˆœ ์ •๋ ฌ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ (2023.04.26) ์ฑ… ํ‘œ์ง€ ์ด๋ฏธ์ง€ ์ €์žฅ ์‹œ ์˜์—ญ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ˆ˜์ • (2023.04.24) ์• ํ”Œ ํŽ˜์ด ๊ฒฐ์ œ ์ถ”๊ฐ€ (2023.03.25) ์œ„ํ‚ค๋…์Šค ํฌ์ธํŠธ ๊ด‘๊ณ  ์„œ๋น„์Šค ์ถœ์‹œ (2023.02.20) 2022 ISBN ๋ฐœ๊ธ‰์„ ์œ„ํ•œ ์ถœํŒ์‚ฌ ์‹ ๊ณ  (2022.11.29) ๋”ฅ ๋Ÿฌ๋‹์„ ์ด์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž…๋ฌธ - ์ „์ž์ฑ… ํŒ๋งค ์‹œ์ž‘ (2022.11.15) ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ๋„๊ตฌ ๋ณ€๊ฒฝ (2022.11.11) ์œ„ํ‚ค๋…์Šค ์ „์ž์ฑ… ํŒ๋งค ์žฌ๊ฐœ (20022.11.01) ์œ„ํ‚ค๋…์Šค API Open (2022.11.01) ๋„ ์ปค ๊ธฐ๋ฐ˜์œผ๋กœ ์šด์˜ (2022.10.29) ์ด๋ฏธ์ง€ ์ €์žฅ์†Œ, ์Šคํ† ๋ฆฌ์ง€ ๋ถ„๋ฆฌ (2022.10.29) ํŒŒ์ด์ฌ 3.10.6, ์žฅ๊ณ  4.1.2 ์—…๊ทธ๋ ˆ์ด๋“œ (2022.10.29) ์œ„ํ‚ค๋…์Šค extension MARK, SMARK ์ˆ˜์ • (2022.04.20) highlight.js : 11.5.1 ๋ฒ„์ „์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ (2022.04.20) ์ฑ… ๊ธ€์ž ํฌ๊ธฐ ์„ค์ • ๊ธฐ๋Šฅ ์ถ”๊ฐ€ (2022.04.19) ์ฑ… ์ˆ˜์ • ๊ธฐ๋Šฅ ์—…๋ฐ์ดํŠธ, ์—”ํ„ฐํ‚ค ์ค„ ๋ฐ”๊ฟˆ ์ถ”๊ฐ€ (2022.04.14) ์šฐ์ƒ๋‹จ ๊ณ ์ • ๊ด‘๊ณ  ์ถ”๊ฐ€ (2022.03.25) ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ํ›„ ์—๋””ํ„ฐ์— ๋ถ™์—ฌ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” "๋ณต์‚ฌ" ๋งํฌ ์ถ”๊ฐ€ (2022.02.06) 2021 ์œ„ํ‚ค๋…์Šค ์•ฑ ์ถœ์‹œ (2021.10.04) ์ปค์Šคํ…€ ๋ผ์ด์„ ์Šค ๋“ฑ๋กํ•˜๊ธฐ (๋ผ์ด์„ผ์Šค๋ช…๊ณผ URL์„ ์ˆ˜๋™์œผ๋กœ ์ž…๋ ฅ) (2021.09.03) ๋ถ€๋ชจ ๋ชฉ์ฐจ๊ฐ€ ๋น„๊ณต๊ฐœ์ด๋ฉด ์ž์‹ ๋ชฉ์ฐจ ์•ˆ ๋ณด์ด๋„๋ก (2021.08) ํŽ˜์ด์Šค๋ถ ๋“ฑ์—์„œ ์œ„ํ‚ค๋…์Šค ๋งํฌ ์‹œ ์ด๋ฏธ์ง€ ๋งํฌ๊ฐ€ "์ฑ… ํ‘œ์ง€"๋กœ ๋‚˜์˜ค๋„๋ก ์ˆ˜์ •ํ•˜๊ธฐ (2021.08) Mathjax ๋ฒ„์ „ 2์—์„œ 3์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ (2021.05.23) AWS RDS ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—…๊ทธ๋ ˆ์ด๋“œ (2021.03.31) ์ €์ž ํ”„๋กœํ•„ ํŽ˜์ด์ง€ ์ถ”๊ฐ€ (2021.01.05) ์ตœ๊ทผ ๋Œ“๊ธ€ 10๊ฐœ, ์ „์ฒด ๋Œ“๊ธ€ ๋ณด๊ธฐ ๋งํฌ ์ถ”๊ฐ€ (2021.06.01) ๋งˆํฌ๋‹ค์šด ์œ„ํ‚ค๋…์Šค Extension ์ฝ”๋“œ ๋งˆ์ปค ์ถ”๊ฐ€ (2021.07.17) 2020 AWS EC2 ์„œ๋ฒ„ ์—…๊ทธ๋ ˆ์ด๋“œ (2020.04.26) 2019 StackEditor ๋„์ž… ๋ฐ ํ™”๋ฉด ๋””์ž์ธ ์ˆ˜์ • (2019.11.08) ๊ตฌ๊ธ€ ๊ด‘๊ณ  ํ‘œ์‹œ ์˜ค๋ฅ˜์ˆ˜์ • (2019.10.05) 2018 ํ•œ๊ธ€ ๋ชฉ์ฐจ ์ •๋ ฌ ๋ฒ„๊ทธ(PostgreSQL ํ•œ๊ธ€ ์ •๋ ฌ ๋ฌธ์ œ) ์ˆ˜์ • (2018.03.10) 2017 ๋ชฉ์ฐจ ์ •๋ ฌ ๋ฒ„๊ทธ ์ˆ˜์ • (2017.05.23) ๊ด‘๊ณ  ์ •์ฑ… ๋ณ€๊ฒฝ (2017.05.12) ํ—ค๋” ๋ฐ ๋ชฉ์ฐจ ๊ณ ์ • (2017.04.20) ๋งˆํฌ๋‹ค์šด ์—๋””ํ„ฐ ๋ณ€๊ฒฝ (2017.03.04) ์ฑ… ๋‹ค์šด๋กœ๋“œ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ (2017.02.26) ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ (2017.02.25) ํŒŒ์ด์ฌ 3.6 + Django 1.10.5 Upgrade (2017.01.22) 2014 ์ˆ˜์‹ ์ž…๋ ฅ ์ง€์› (2014.09.30) 2013 ๋ชจ๋ฐ”์ผ ์›น ์ง€์› (2013.08.24) ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์ง€์› (2013.07.24) ์œ„ํ‚ค๋…์Šค Version 2 Open (2013.07.01) 2008 ์œ„ํ‚ค๋…์Šค Open A. ๊ฐœ์ธ์ •๋ณด์ฒ˜๋ฆฌ ๋ฐฉ์นจ ์ตœ์ข… ์ˆ˜์ •์ผ: 2013๋…„ 6์›” 30์ผ '์œ„ํ‚ค๋…์Šค' ๋Š” (์ดํ•˜ 'ํšŒ์‚ฌ'๋Š”) ๊ณ ๊ฐ๋‹˜์˜ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์ค‘์š”์‹œํ•˜๋ฉฐ, "์ •๋ณดํ†ต์‹ ๋ง ์ด์šฉ ์ด‰์ง„ ๋ฐ ์ •๋ณด๋ณดํ˜ธ"์— ๊ด€ํ•œ ๋ฒ•๋ฅ ์„ ์ค€์ˆ˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํšŒ์‚ฌ๋Š” ๊ฐœ์ธ์ •๋ณด ์ทจ๊ธ‰ ๋ฐฉ์นจ์„ ํ†ตํ•˜์—ฌ ๊ณ ๊ฐ๋‹˜๊ป˜์„œ ์ œ๊ณตํ•˜์‹œ๋Š” ๊ฐœ์ธ์ •๋ณด๊ฐ€ ์–ด๋– ํ•œ ์šฉ๋„์™€ ๋ฐฉ์‹์œผ๋กœ ์ด์šฉ๋˜๊ณ  ์žˆ์œผ๋ฉฐ, ๊ฐœ์ธ์ •๋ณด๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด ์–ด๋– ํ•œ ์กฐ์น˜๊ฐ€ ์ทจํ•ด์ง€๊ณ  ์žˆ๋Š”์ง€ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค. ํšŒ์‚ฌ๋Š” ๊ฐœ์ธ์ •๋ณด ์ทจ๊ธ‰ ๋ฐฉ์นจ์„ ๊ฐœ์ •ํ•˜๋Š” ๊ฒฝ์šฐ ์›น์‚ฌ์ดํŠธ ๊ณต์ง€์‚ฌํ•ญ(๋˜๋Š” ๊ฐœ๋ณ„ ๊ณต์ง€)์„ ํ†ตํ•˜์—ฌ ๊ณต์ง€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ˆ˜์ง‘ํ•˜๋Š” ๊ฐœ์ธ์ •๋ณด์˜ ํ•ญ๋ชฉ ๊ฐœ์ธ์ •๋ณด์˜ ์ˆ˜์ง‘ ๋ฐ ์ด์šฉ๋ชฉ์  ๊ฐœ์ธ์ •๋ณด์˜ ๋ณด์œ  ๋ฐ ์ด์šฉ ๊ธฐ๊ฐ„ ๊ฐœ์ธ์ •๋ณด์˜ ํŒŒ๊ธฐ ์ ˆ์ฐจ ๋ฐ ๋ฐฉ๋ฒ• ๊ฐœ์ธ์ •๋ณด ์ œ๊ณต ์—…๋ฌด ์œ„ํƒ ์ด์šฉ์ž ๋ฐ ๋ฒ•์ •๋Œ€๋ฆฌ์ธ์˜ ๊ถŒ๋ฆฌ์™€ ๊ทธ ํ–‰์‚ฌ ๋ฐฉ๋ฒ• ๊ฐœ์ธ์ •๋ณด ์ž๋™ ์ˆ˜์ง‘ ์žฅ์น˜์˜ ์„ค์น˜, ์šด์˜ ๋ฐ ๊ทธ ๊ฑฐ๋ถ€์— ๊ด€ํ•œ ์‚ฌํ•ญ ๊ฐœ์ธ ์ •๋ณด์— ๊ด€ํ•œ ๋ฏผ์› ์„œ๋น„์Šค ๊ธฐํƒ€ ๊ฐœ์ธ์ •๋ณด ์นจํ•ด์— ๋Œ€ํ•œ ์‹ ๊ณ ๋‚˜ ์ƒ๋‹ด ์ˆ˜์ง‘ํ•˜๋Š” ๊ฐœ์ธ์ •๋ณด์˜ ํ•ญ๋ชฉ ๋ณธ ๋ฐฉ์นจ์€ : 2013๋…„ 06 ์›” 30์ผ๋ถ€ํ„ฐ ์‹œํ–‰๋ฉ๋‹ˆ๋‹ค. ํšŒ์‚ฌ๋Š” ํšŒ์›๊ฐ€์ž…, ์ƒ๋‹ด, ์„œ๋น„์Šค ์‹ ์ฒญ ๋“ฑ๋“ฑ์„ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜์ง‘ ํ•ญ๋ชฉ : ์ด๋ฆ„, ์ด๋ฉ”์ผ ๊ฐœ์ธ์ •๋ณด ์ˆ˜์ง‘ ๋ฐฉ๋ฒ• : ํ™ˆํŽ˜์ด์ง€ (ํšŒ์›๊ฐ€์ž…) ๊ฐœ์ธ์ •๋ณด์˜ ์ˆ˜์ง‘ ๋ฐ ์ด์šฉ๋ชฉ์  ํšŒ์‚ฌ๋Š” ์ˆ˜์ง‘ํ•œ ๊ฐœ์ธ์ •๋ณด๋ฅผ ๋‹ค์Œ์˜ ๋ชฉ์ ์„ ์œ„ํ•ด ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ์„œ๋น„์Šค ์ œ๊ณต์— ๊ด€ํ•œ ๊ณ„์•ฝ ์ดํ–‰ ๋ฐ ์„œ๋น„์Šค ์ œ๊ณต์— ๋”ฐ๋ฅธ ์š”๊ธˆ ์ •์‚ฐ ์ฝ˜ํ…์ธ  ์ œ๊ณต ํšŒ์› ๊ด€๋ฆฌ: ํšŒ์›์ œ ์„œ๋น„์Šค ์ด์šฉ์— ๋”ฐ๋ฅธ ๋ณธ์ธํ™•์ธ, ๊ฐœ์ธ ์‹๋ณ„, ๋ถˆ๋Ÿ‰ ํšŒ์›์˜ ๋ถ€์ • ์ด์šฉ ๋ฐฉ์ง€์™€ ๋น„์ธ๊ฐ€ ์‚ฌ์šฉ ๋ฐฉ์ง€, ๊ฐ€์ž… ์˜์‚ฌ ํ™•์ธ, ๊ณ ์ง€์‚ฌํ•ญ ์ „๋‹ฌ ๊ฐœ์ธ์ •๋ณด์˜ ๋ณด์œ  ๋ฐ ์ด์šฉ ๊ธฐ๊ฐ„ ํšŒ์‚ฌ๋Š” ๊ฐœ์ธ์ •๋ณด ์ˆ˜์ง‘ ๋ฐ ์ด์šฉ๋ชฉ์ ์ด ๋‹ฌ์„ฑ๋œ ํ›„์—๋Š” ์˜ˆ์™ธ ์—†์ด ํ•ด๋‹น ์ •๋ณด๋ฅผ ์ง€์ฒด ์—†์ด ํŒŒ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ์ธ์ •๋ณด์˜ ํŒŒ๊ธฐ ์ ˆ์ฐจ ๋ฐ ๋ฐฉ๋ฒ• ํšŒ์‚ฌ๋Š” ์›์น™์ ์œผ๋กœ ๊ฐœ์ธ์ •๋ณด ์ˆ˜์ง‘ ๋ฐ ์ด์šฉ๋ชฉ์ ์ด ๋‹ฌ์„ฑ๋œ ํ›„์—๋Š” ํ•ด๋‹น ์ •๋ณด๋ฅผ ๋ฐ”๋กœ ํŒŒ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ํŒŒ๊ธฐ ์ ˆ์ฐจ ๋ฐ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํŒŒ๊ธฐ ์ ˆ์ฐจ ํšŒ์›๋‹˜์ด ํšŒ์›๊ฐ€์ž… ๋“ฑ์„ ์œ„ํ•ด ์ž…๋ ฅํ•˜์‹  ์ •๋ณด๋Š” ๋ชฉ์ ์ด ๋‹ฌ์„ฑ๋œ ํ›„ ๋ณ„๋„์˜ DB๋กœ ์˜ฎ๊ฒจ์ ธ(์ข…์ด์˜ ๊ฒฝ์šฐ ๋ณ„๋„์˜ ์„œ๋ฅ˜ํ•จ) ๋‚ด๋ถ€ ๋ฐฉ์นจ ๋ฐ ๊ธฐํƒ€ ๊ด€๋ จ ๋ฒ•๋ น์— ์˜ํ•œ ์ •๋ณด๋ณดํ˜ธ ์‚ฌ์œ ์— ๋”ฐ๋ผ(๋ณด์œ  ๋ฐ ์ด์šฉ ๊ธฐ๊ฐ„ ์ฐธ์กฐ) ์ผ์ • ๊ธฐ๊ฐ„ ์ €์žฅ๋œ ํ›„ ํŒŒ๊ธฐ๋ฉ๋‹ˆ๋‹ค. ๋ณ„๋„ DB๋กœ ์˜ฎ๊ฒจ์ง„ ๊ฐœ์ธ์ •๋ณด๋Š” ๋ฒ•๋ฅ ์— ์˜ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๊ณ ์„œ๋Š” ๋ณด์œ ๋˜๋Š” ์ด์™ธ์˜ ๋‹ค๋ฅธ ๋ชฉ์ ์œผ๋กœ ์ด์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŒŒ๊ธฐ ๋ฐฉ๋ฒ• ์ „์ž์  ํŒŒ์ผ ํ˜•ํƒœ๋กœ ์ €์žฅ๋œ ๊ฐœ์ธ์ •๋ณด๋Š” ๊ธฐ๋ก์„ ์žฌ์ƒํ•  ์ˆ˜ ์—†๋Š” ๊ธฐ์ˆ ์  ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ์ธ์ •๋ณด ์ œ๊ณต ํšŒ์‚ฌ๋Š” ์ด์šฉ์ž์˜ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์›์น™์ ์œผ๋กœ ์™ธ๋ถ€์— ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์•„๋ž˜์˜ ๊ฒฝ์šฐ์—๋Š” ์˜ˆ์™ธ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด์šฉ์ž๋“ค์ด ์‚ฌ์ „์— ๋™์˜ํ•œ ๊ฒฝ์šฐ ๋ฒ•๋ น์˜ ๊ทœ์ •์— ๋”ฐ๋ฅด๊ฑฐ๋‚˜, ์ˆ˜์‚ฌ ๋ชฉ์ ์œผ๋กœ ๋ฒ•๋ น์— ์ •ํ•ด์ง„ ์ ˆ์ฐจ์™€ ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ์ˆ˜์‚ฌ๊ธฐ๊ด€์˜ ์š”๊ตฌ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์—…๋ฌด ์œ„ํƒ ํšŒ์‚ฌ๋Š” ๊ณ ๊ฐ๋‹˜์˜ ๋™์˜ ์—†์ด ๊ณ ๊ฐ๋‹˜์˜ ์ •๋ณด๋ฅผ ์™ธ๋ถ€ ์—…์ฒด์— ์œ„ํƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ–ฅํ›„ ๊ทธ๋Ÿฌํ•œ ํ•„์š”๊ฐ€ ์ƒ๊ธธ ๊ฒฝ์šฐ, ์œ„ํƒ ๋Œ€์ƒ์ž์™€ ์œ„ํƒ ์—…๋ฌด ๋‚ด์šฉ์— ๋Œ€ํ•ด ๊ณ ๊ฐ๋‹˜์—๊ฒŒ ํ†ต์ง€ํ•˜๊ณ  ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์ „ ๋™์˜๋ฅผ ๋ฐ›๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์šฉ์ž ๋ฐ ๋ฒ•์ •๋Œ€๋ฆฌ์ธ์˜ ๊ถŒ๋ฆฌ์™€ ๊ทธ ํ–‰์‚ฌ ๋ฐฉ๋ฒ• ์ด์šฉ์ž ๋ฐ ๋ฒ•์ • ๋Œ€๋ฆฌ์ธ์€ ์–ธ์ œ๋“ ์ง€ ๋“ฑ๋ก๋œ ์ž์‹  ํ˜น์€ ๋‹นํ•ด ๋งŒ 14์„ธ ๋ฏธ๋งŒ ์•„๋™์˜ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฐ€์ž… ํ•ด์ง€๋ฅผ ์š”์ฒญํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์šฉ์ž ํ˜น์€ ๋งŒ 14์„ธ ๋ฏธ๋งŒ ์•„๋™์˜ ๊ฐœ์ธ์ •๋ณด ์กฐํšŒ? ์ˆ˜์ •์„ ์œ„ํ•ด์„œ๋Š” โ€˜๊ฐœ์ธ์ •๋ณด๋ณ€ ๊ฒฝโ€™(๋˜๋Š” โ€˜ํšŒ์› ์ •๋ณด ์ˆ˜์ •โ€™ ๋“ฑ)์„ ๊ฐ€์ž…ํ•ด ์ง€(๋™์˜ ์ฒ ํšŒ)๋ฅผ ์œ„ํ•ด์„œ๋Š” โ€œํšŒ์› ํƒˆํ‡ดโ€๋ฅผ ํด๋ฆญํ•˜์—ฌ ๋ณธ์ธ ํ™•์ธ ์ ˆ์ฐจ๋ฅผ ๊ฑฐ์น˜์‹  ํ›„ ์ง์ ‘ ์—ด๋žŒ, ์ •์ • ๋˜๋Š” ํƒˆํ‡ด๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ˜น์€ ๊ฐœ์ธ ์ •๋ณด๊ด€๋ฆฌ ์ฑ…์ž„์ž์—๊ฒŒ ์„œ๋ฉด, ์ „ํ™” ๋˜๋Š” ์ด๋ฉ”์ผ๋กœ ์—ฐ๋ฝํ•˜์‹œ๋ฉด ๋ฐ”๋กœ ์กฐ์น˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ท€ํ•˜๊ฐ€ ๊ฐœ์ธ์ •๋ณด์˜ ์˜ค๋ฅ˜์— ๋Œ€ํ•œ ์ •์ •์„ ์š”์ฒญํ•˜์‹  ๊ฒฝ์šฐ์—๋Š” ์ •์ •์„ ์™„๋ฃŒํ•˜๊ธฐ ์ „๊นŒ์ง€ ๋‹นํ•ด ๊ฐœ์ธ์ •๋ณด๋ฅผ ์ด์šฉ ๋˜๋Š” ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ž˜๋ชป๋œ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์ œ3์ž์—๊ฒŒ ์ด๋ฏธ ์ œ๊ณตํ•œ ๊ฒฝ์šฐ์—๋Š” ์ •์ • ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ์ œ์‚ผ์ž์—๊ฒŒ ๋ฐ”๋กœ ํ†ต์ง€ํ•˜์—ฌ ์ •์ •์ด ์ด๋ฃจ์–ด์ง€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํšŒ์‚ฌ๋Š” ์ด์šฉ์ž ํ˜น์€ ๋ฒ•์ • ๋Œ€๋ฆฌ์ธ์˜ ์š”์ฒญ์œผ๋กœ ํ•ด์ง€ ๋˜๋Š” ์‚ญ์ œ๋œ ๊ฐœ์ธ์ •๋ณด๋Š” โ€œํšŒ์‚ฌ๊ฐ€ ์ˆ˜์ง‘ํ•˜๋Š” ๊ฐœ์ธ์ •๋ณด์˜ ๋ณด์œ  ๋ฐ ์ด์šฉ ๊ธฐ๊ฐ„โ€์— ๋ช…์‹œ๋œ ๋ฐ”์— ๋”ฐ๋ผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ทธ ์™ธ์˜ ์šฉ๋„๋กœ ์—ด๋žŒ ๋˜๋Š” ์ด์šฉํ•  ์ˆ˜ ์—†๋„๋ก ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ์ธ์ •๋ณด ์ž๋™ ์ˆ˜์ง‘ ์žฅ์น˜์˜ ์„ค์น˜, ์šด์˜ ๋ฐ ๊ทธ ๊ฑฐ๋ถ€์— ๊ด€ํ•œ ์‚ฌํ•ญ ์ฟ ํ‚ค ๋“ฑ ์ธํ„ฐ๋„ท ์„œ๋น„์Šค ์ด์šฉ ์‹œ ์ž๋™ ์ƒ์„ฑ๋˜๋Š” ๊ฐœ์ธ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” ์žฅ์น˜๋ฅผ ์šด์˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐœ์ธ ์ •๋ณด์— ๊ด€ํ•œ ๋ฏผ์› ์„œ๋น„์Šค ํšŒ์‚ฌ๋Š” ๊ณ ๊ฐ์˜ ๊ฐœ์ธ์ •๋ณด๋ฅผ ๋ณดํ˜ธํ•˜๊ณ  ๊ฐœ์ธ ์ •๋ณด์™€ ๊ด€๋ จํ•œ ๋ถˆ๋งŒ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ด€๋ จ ๋ถ€์„œ ๋ฐ ๊ฐœ์ธ์ •๋ณด ๊ด€๋ฆฌ ์ฑ…์ž„์ž๋ฅผ ์ง€์ •ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ์ธ์ •๋ณด ๊ด€๋ฆฌ ์ฑ…์ž„์ž ์„ฑ๋ช… : <NAME> <EMAIL> ๊ธฐํƒ€ ๊ฐœ์ธ์ •๋ณด ์นจํ•ด์— ๋Œ€ํ•œ ์‹ ๊ณ ๋‚˜ ์ƒ๋‹ด ๊ท€ํ•˜๊ป˜์„œ๋Š” ํšŒ์‚ฌ์˜ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜์‹œ๋ฉฐ ๋ฐœ์ƒํ•˜๋Š” ๋ชจ๋“  ๊ฐœ์ธ์ •๋ณด๋ณดํ˜ธ ๊ด€๋ จ ๋ฏผ์›์„ ๊ฐœ์ธ์ •๋ณด ๊ด€๋ฆฌ ์ฑ…์ž„์ž ํ˜น์€ ๋‹ด๋‹น ๋ถ€์„œ๋กœ ์‹ ๊ณ ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํšŒ์‚ฌ๋Š” ์ด์šฉ์ž๋“ค๊ณผ ๊ด€๋ จํ•œ ์‹ ๊ณ ์‚ฌํ•ญ์— ๋Œ€ํ•ด ์‹ ์†ํ•˜๊ฒŒ ์ถฉ๋ถ„ํ•œ ๋‹ต๋ณ€์„ ๋“œ๋ฆด ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ธฐํƒ€ ๊ฐœ์ธ์ •๋ณด ์นจํ•ด์— ๋Œ€ํ•œ ์‹ ๊ณ ๋‚˜ ์ƒ๋‹ด์ด ํ•„์š”ํ•˜์‹  ๊ฒฝ์šฐ์—๋Š” ์•„๋ž˜ ๊ธฐ๊ด€์— ๋ฌธ์˜ํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ฐœ์ธ ์ •๋ณด ์นจํ•ด์‹ ๊ณ ์„ผํ„ฐ (www.1336.or.kr/๊ตญ๋ฒˆ ์—†์ด 118) ์ •๋ณด๋ณดํ˜ธ ๋งˆํฌ ์ธ์ฆ์œ„์›ํšŒ (www.eprivacy.or.kr/02-580-0533~4) ๋Œ€๊ฒ€์ฐฐ์ฒญ ์ธํ„ฐ๋„ท ๋ฒ”์ฃ„ ์ˆ˜์‚ฌ์„ผํ„ฐ (http://icic.sppo.go.kr/02-3480-3600) ๊ฒฝ์ฐฐ์ฒญ ์‚ฌ์ด๋ฒ„ํ…Œ๋Ÿฌ๋Œ€์‘์„ผํ„ฐ (www.ctrc.go.kr/02-392-0330) B. ์„œ๋น„์Šค ์•ฝ๊ด€ ์ตœ์ข… ์ˆ˜์ •์ผ: 2022๋…„ 11์›” 7์ผ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ๋ฐ ์ œํ’ˆ(์ดํ•˜ โ€˜์„œ๋น„์Šคโ€™)์„ ์ด์šฉํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋ณธ ์•ฝ๊ด€์€ ๋‹ค์–‘ํ•œ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์˜ ์ด์šฉ๊ณผ ๊ด€๋ จํ•˜์—ฌ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ์œ„ํ‚ค๋…์Šค์™€ ์ด๋ฅผ ์ด์šฉํ•˜๋Š” ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ํšŒ์›(์ดํ•˜ โ€˜ํšŒ์›โ€™) ๋˜๋Š” ๋น„ํšŒ์›๊ณผ์˜ ๊ด€๊ณ„๋ฅผ ์„ค๋ช…ํ•˜๋ฉฐ, ์•„์šธ๋Ÿฌ ์—ฌ๋Ÿฌ๋ถ„์˜ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ์— ๋„์›€์ด ๋  ์ˆ˜ ์žˆ๋Š” ์œ ์ตํ•œ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜์‹œ๊ฑฐ๋‚˜ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ํšŒ์›์œผ๋กœ ๊ฐ€์ž…ํ•˜์‹ค ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ๋ถ„์€ ๋ณธ ์•ฝ๊ด€ ๋ฐ ๊ด€๋ จ ์šด์˜ ์ •์ฑ…์„ ํ™•์ธํ•˜๊ฑฐ๋‚˜ ๋™์˜ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, ์ž ์‹œ ์‹œ๊ฐ„์„ ๋‚ด์‹œ์–ด ์ฃผ์˜ ๊นŠ๊ฒŒ ์‚ดํŽด๋ด ์ฃผ์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค ๋‹ค์–‘ํ•œ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ฆ๊ฒจ๋ณด์„ธ์š”. ์—ฌ๋Ÿฌ๋ถ„์ด ์ œ๊ณตํ•œ ์ฝ˜ํ…์ธ ๋ฅผ ์†Œ์ค‘ํžˆ ๋‹ค๋ฃฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์˜ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์†Œ์ค‘ํžˆ ๋ณดํ˜ธํ•ฉ๋‹ˆ๋‹ค. ํƒ€์ธ์˜ ๊ถŒ๋ฆฌ๋ฅผ ์กด์ค‘ํ•ด ์ฃผ์„ธ์š”. ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ๊ณผ ๊ด€๋ จํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€ ์ฃผ์˜์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถ€๋“์ด ์„œ๋น„์Šค ์ด์šฉ์„ ์ œํ•œํ•  ๊ฒฝ์šฐ ํ•ฉ๋ฆฌ์ ์ธ ์ ˆ์ฐจ๋ฅผ ์ค€์ˆ˜ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์˜ ์ž˜๋ชป์€ ์œ„ํ‚ค๋…์Šค๊ฐ€ ์ฑ…์ž„์ง‘๋‹ˆ๋‹ค. ์–ธ์ œ๋“ ์ง€ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ๊ณ„์•ฝ์„ ํ•ด์ง€ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋ถ€ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์—๋Š” ๊ด‘๊ณ ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์„œ๋น„์Šค ์ค‘๋‹จ ๋˜๋Š” ๋ณ€๊ฒฝ ์‹œ ๊ผญ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผ์š” ์‚ฌํ•ญ์„ ์ž˜ ์•ˆ๋‚ดํ•˜๊ณ  ์—ฌ๋Ÿฌ๋ถ„์˜ ์†Œ์ค‘ํ•œ ์˜๊ฒฌ์— ๊ท€ ๊ธฐ์šธ์ด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์ „์ž์ฑ… ํŒ๋งค์™€ ๊ด€๋ จํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€ ์ฃผ์˜์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ์•ฝ๊ด€ ๋ฐ ์šด์˜์ •์ฑ…์„ ๊ฒŒ์‹œํ•˜๋ฉฐ ์‚ฌ์ „ ๊ณต์ง€ ํ›„ ๊ฐœ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ฆ๊ฒจ๋ณด์„ธ์š”. ์œ„ํ‚ค๋…์Šค๋Š” wikidocs.net์„ ๋น„๋กฏํ•œ ์œ„ํ‚ค๋…์Šค ๋„๋ฉ”์ธ์˜ ์›น์‚ฌ์ดํŠธ ๋ฐ ์‘์šฉํ”„๋กœ๊ทธ๋žจ(์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ์•ฑ)์„ ํ†ตํ•ด ์ •๋ณด ๊ฒ€์ƒ‰, ๋‹ค๋ฅธ ์ด์šฉ์ž์™€์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜, ์ฝ˜ํ…์ธ  ์ œ๊ณต, PDF ๊ตฌ์ž… ๋“ฑ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ƒํ™œ์— ํŽธ๋ฆฌํ•จ์„ ๋”ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ PC, ํœด๋Œ€์ „ํ™” ๋“ฑ ์ธํ„ฐ๋„ท ์ด์šฉ์ด ๊ฐ€๋Šฅํ•œ ๊ฐ์ข… ๋‹จ๋ง๊ธฐ๋ฅผ ํ†ตํ•ด ๊ฐ์–‘๊ฐ์ƒ‰์˜ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์ด์šฉํ•˜์‹ค ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐœ๋ณ„ ์„œ๋น„์Šค๋“ค์˜ ๊ตฌ์ฒด์ ์ธ ๋‚ด์šฉ์€ ๊ฐ ์„œ๋น„์Šค ์ƒ์˜ ์•ˆ๋‚ด, ๊ณต์ง€์‚ฌํ•ญ, ์œ„ํ‚ค๋…์Šค ์›น ๊ณ ๊ฐ์„ผํ„ฐ(์ดํ•˜โ€˜๊ณ ๊ฐ์„ผํ„ฐโ€™) ๋„์›€๋ง ๋“ฑ์—์„œ ์‰ฝ๊ฒŒ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํšŒ์›์œผ๋กœ ๊ฐ€์ž…ํ•˜์‹œ๋ฉด ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ๋”์šฑ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ๋ณธ ์•ฝ๊ด€์„ ์ฝ๊ณ  ๋™์˜ํ•˜์‹  ํ›„ ํšŒ์› ๊ฐ€์ž…์„ ์‹ ์ฒญํ•˜์‹ค ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์œ„ํ‚ค๋…์Šค๋Š” ์ด์— ๋Œ€ํ•œ ์Šน๋‚™์„ ํ†ตํ•ด ํšŒ์› ๊ฐ€์ž… ์ ˆ์ฐจ๋ฅผ ์™„๋ฃŒํ•˜๊ณ  ์—ฌ๋Ÿฌ๋ถ„๊ป˜ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ ๊ณ„์ •(์ดํ•˜ โ€˜๊ณ„์ •โ€™)์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ๊ณ„์ •์ด๋ž€ ํšŒ์›์ด ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์— ๋กœ๊ทธ์ธํ•œ ์ดํ›„ ์ด์šฉํ•˜๋Š” ๊ฐ์ข… ์„œ๋น„์Šค ์ด์šฉ ์ด๋ ฅ์„ ํšŒ์›๋ณ„๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์„ค์ •ํ•œ ํšŒ์› ์‹๋ณ„ ๋‹จ์œ„๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ํšŒ์›์€ ์ž์‹ ์˜ ๊ณ„์ •์„ ํ†ตํ•ด ์ข€ ๋” ๋‹ค์–‘ํ•œ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ๋”์šฑ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์ œ๊ณตํ•œ ์ฝ˜ํ…์ธ ๋ฅผ ์†Œ์ค‘ํžˆ ๋‹ค๋ฃฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฒŒ์žฌํ•œ ๊ฒŒ์‹œ๋ฌผ์ด ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ์ด์šฉ์ž๋“ค์—๊ฒŒ ์ „๋‹ฌ๋˜์–ด ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ์‚ถ์„ ๋”์šฑ ํ’์š”๋กญ๊ฒŒ ํ•ด์ค„ ๊ฒƒ์„ ๊ธฐ๋Œ€ํ•ฉ๋‹ˆ๋‹ค. ๊ฒŒ์‹œ๋ฌผ์€ ์—ฌ๋Ÿฌ๋ถ„์ด ํƒ€์ธ ๋˜๋Š” ์ž์‹ ์ด ๋ณด๊ฒŒ ํ•  ๋ชฉ์ ์œผ๋กœ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์ƒ์— ๊ฒŒ์žฌํ•œ ๋ถ€ํ˜ธ, ๋ฌธ์ž, ์Œ์„ฑ, ์Œํ–ฅ, ๊ทธ๋ฆผ, ์‚ฌ์ง„,<NAME>์ƒ, ๋งํฌ ๋“ฑ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฐ์ข… ์ฝ˜ํ…์ธ  ์ž์ฒด ๋˜๋Š” ํŒŒ์ผ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ์ƒ๊ฐ๊ณผ ๊ฐ์ •์ด ํ‘œํ˜„๋œ ์ฝ˜ํ…์ธ ๋ฅผ ์†Œ์ค‘ํžˆ ๋ณดํ˜ธํ•  ๊ฒƒ์„ ์•ฝ์†๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์ œ์ž‘ํ•˜์—ฌ ๊ฒŒ์žฌํ•œ ๊ฒŒ์‹œ๋ฌผ์— ๋Œ€ํ•œ ์ง€์‹ ์žฌ์‚ฐ๊ถŒ ๋“ฑ์˜ ๊ถŒ๋ฆฌ๋Š” ๋‹น์—ฐํžˆ ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œํŽธ, ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฒŒ์žฌํ•œ ๊ฒŒ์‹œ๋ฌผ์„ ์ ๋ฒ•ํ•˜๊ฒŒ ์ œ๊ณตํ•˜๋ ค๋ฉด ํ•ด๋‹น ์ฝ˜ํ…์ธ ์— ๋Œ€ํ•œ ์ €์žฅ, ๋ณต์ œ, ์ˆ˜์ •, ๊ณต์ค‘ ์†ก์‹ , ์ „์‹œ, ๋ฐฐํฌ, ์ด์ฐจ์  ์ €์ž‘๋ฌผ ์ž‘์„ฑ(๋‹จ, ๋ฒˆ์—ญ์— ํ•œํ•จ) ๋“ฑ์˜ ์ด์šฉ ๊ถŒํ•œ(๊ธฐํ•œ๊ณผ ์ง€์—ญ ์ œํ•œ์— ์ •ํ•จ์ด ์—†์œผ๋ฉฐ, ๋ณ„๋„ ๋Œ€๊ฐ€ ์ง€๊ธ‰์ด ์—†๋Š” ๋ผ์ด์„ ์Šค)์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฒŒ์‹œ๋ฌผ ๊ฒŒ์žฌ๋กœ ์—ฌ๋Ÿฌ๋ถ„์€ ์œ„ํ‚ค๋…์Šค์—๊ฒŒ ๊ทธ๋Ÿฌํ•œ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, ์—ฌ๋Ÿฌ๋ถ„์€ ์ด์— ํ•„์š”ํ•œ ๊ถŒ๋ฆฌ๋ฅผ ๋ณด์œ ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ๋ถ€์—ฌํ•ด ์ฃผ์‹  ์ฝ˜ํ…์ธ  ์ด์šฉ ๊ถŒํ•œ์„ ์ €์ž‘๊ถŒ๋ฒ• ๋“ฑ ๊ด€๋ จ ๋ฒ•๋ น์—์„œ ์ •ํ•˜๋Š” ๋ฐ”์— ๋”ฐ๋ผ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ๋‚ด ๋…ธ์ถœ, ์„œ๋น„์Šค ํ™๋ณด๋ฅผ ์œ„ํ•œ ํ™œ์šฉ, ์„œ๋น„์Šค ์šด์˜, ๊ฐœ์„  ๋ฐ ์ƒˆ๋กœ์šด ์„œ๋น„์Šค ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ์—ฐ๊ตฌ, ์›น ์ ‘๊ทผ์„ฑ ๋“ฑ ๋ฒ•๋ฅ ์ƒ ์˜๋ฌด ์ค€์ˆ˜, ์™ธ๋ถ€ ์‚ฌ์ดํŠธ์—์„œ์˜ ๊ฒ€์ƒ‰, ์ˆ˜์ง‘ ๋ฐ ๋งํฌ ํ—ˆ์šฉ์„ ์œ„ํ•ด์„œ๋งŒ ์ œํ•œ์ ์œผ๋กœ ํ–‰์‚ฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๊ทธ ๋ฐ–์˜ ๋ชฉ์ ์„ ์œ„ํ•ด ๋ถ€๋“์ด ์—ฌ๋Ÿฌ๋ถ„์˜ ์ฝ˜ํ…์ธ ๋ฅผ ์ด์šฉํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ์—” ์‚ฌ์ „์— ์—ฌ๋Ÿฌ๋ถ„๊ป˜ ์„ค๋ช…์„ ํ•ด๋“œ๋ฆฌ๊ณ  ๋™์˜ ๋ฐ›๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์ž์‹ ์ด ์ œ๊ณตํ•œ ์ฝ˜ํ…์ธ ์— ๋Œ€ํ•œ ์œ„ํ‚ค๋…์Šค ๋˜๋Š” ๋‹ค๋ฅธ ์ด์šฉ์ž๋“ค์˜ ์ด์šฉ ๋˜๋Š” ์ ‘๊ทผ์„ ๋”์šฑ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‹ค์–‘ํ•œ ์ˆ˜๋‹จ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ๋‚ด์— ์ฝ˜ํ…์ธ  ์‚ญ์ œ, ๋น„๊ณต๊ฐœ ๋“ฑ์˜ ๊ด€๋ฆฌ๊ธฐ๋Šฅ์ด ์ œ๊ณต๋˜๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ํ†ตํ•ด ์ง์ ‘ ํƒ€์ธ์˜ ์ด์šฉ ๋˜๋Š” ์ ‘๊ทผ์„ ํ†ต์ œํ•  ์ˆ˜ ์žˆ๊ณ , ๊ณ ๊ฐ์„ผํ„ฐ๋ฅผ ํ†ตํ•ด์„œ๋„ ์ฝ˜ํ…์ธ ์˜ ์‚ญ์ œ, ๋น„๊ณต๊ฐœ, ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ œ์™ธ ๋“ฑ์˜ ์กฐ์น˜๋ฅผ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ผ๋ถ€ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์˜ ๊ฒฝ์šฐ ์‚ญ์ œ, ๋น„๊ณต๊ฐœ ๋“ฑ์˜ ์ฒ˜๋ฆฌ๊ฐ€ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Ÿฌํ•œ ๋‚ด์šฉ์€ ๊ฐ ์„œ๋น„์Šค ์ƒ์˜ ์•ˆ๋‚ด, ๊ณต์ง€์‚ฌํ•ญ, ๋„์›€๋ง ๋“ฑ์—์„œ ํ™•์ธํ•ด ์ฃผ์‹œ๊ธธ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์˜ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์†Œ์ค‘ํžˆ ๋ณดํ˜ธํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์„œ๋น„์Šค์˜ ์›ํ™œํ•œ ์ œ๊ณต์„ ์œ„ํ•˜์—ฌ ํšŒ์›์ด ๋™์˜ํ•œ ๋ชฉ์ ๊ณผ ๋ฒ”์œ„ ๋‚ด์—์„œ๋งŒ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ยท์ด์šฉํ•˜๋ฉฐ, ๊ฐœ์ธ์ •๋ณด ๋ณดํ˜ธ ๊ด€๋ จ ๋ฒ•๋ น์— ๋”ฐ๋ผ ์•ˆ์ „ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๊ฐ€ ์ด์šฉ์ž ๋ฐ ํšŒ์›์— ๋Œ€ํ•ด ๊ด€๋ จ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๊ธฐ์šธ์ด๋Š” ๋…ธ๋ ฅ์ด๋‚˜ ๊ธฐํƒ€ ์ƒ์„ธํ•œ ์‚ฌํ•ญ์€ ๊ฐœ์ธ์ •๋ณด ์ฒ˜๋ฆฌ ๋ฐฉ์นจ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ผ์ • ๊ธฐ๊ฐ„ ๋กœ๊ทธ์ธ ํ˜น์€ ์ ‘์†ํ•œ ๊ธฐ๋ก์ด ์—†๋Š” ๊ฒฝ์šฐ, ์ „์ž๋ฉ”์ผ, ์„œ๋น„์Šค ๋‚ด ์•Œ๋ฆผ ๋˜๋Š” ๊ธฐํƒ€ ์ ์ ˆํ•œ ์ „์ž์  ์ˆ˜๋‹จ์„ ํ†ตํ•ด ์‚ฌ์ „์— ์•ˆ๋‚ดํ•ด ๋“œ๋ฆฐ ํ›„ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ •๋ณด๋ฅผ ํŒŒ๊ธฐํ•˜๊ฑฐ๋‚˜ ๋ถ„๋ฆฌ ๋ณด๊ด€ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋งŒ์•ฝ ์ด์— ๋”ฐ๋ผ ์„œ๋น„์Šค ์ œ๊ณต์„ ์œ„ํ•ด ํ•„์ˆ˜์ ์ธ ์ •๋ณด๊ฐ€ ๋ถ€์กฑํ•ด์งˆ ๊ฒฝ์šฐ ๋ถ€๋“์ด ๊ด€๋ จ ์„œ๋น„์Šค ์ด์šฉ๊ณ„์•ฝ์„ ํ•ด์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํƒ€์ธ์˜ ๊ถŒ๋ฆฌ๋ฅผ ์กด์ค‘ํ•ด ์ฃผ์„ธ์š”. ์—ฌ๋Ÿฌ๋ถ„์ด ๋ฌด์‹ฌ์ฝ” ๊ฒŒ์žฌํ•œ ๊ฒŒ์‹œ๋ฌผ๋กœ ์ธํ•ด ํƒ€์ธ์˜ ์ €์ž‘๊ถŒ์ด ์นจํ•ด๋˜๊ฑฐ๋‚˜ ๋ช…์˜ˆํ›ผ์† ๋“ฑ ๊ถŒ๋ฆฌ ์นจํ•ด๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์ด์— ๋Œ€ํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•ด โ€˜์ •๋ณดํ†ต์‹ ๋ง ์ด์šฉ ์ด‰์ง„ ๋ฐ ์ •๋ณด๋ณดํ˜ธ ๋“ฑ์— ๊ด€ํ•œ ๋ฒ•๋ฅ โ€™ ๋ฐ โ€˜์ €์ž‘๊ถŒ๋ฒ•โ€™ ๋“ฑ์„ ๊ทผ๊ฑฐ๋กœ ๊ถŒ๋ฆฌ์นจํ•ด ์ฃผ์žฅ์ž์˜ ์š”์ฒญ์— ๋”ฐ๋ฅธ ๊ฒŒ์‹œ๋ฌผ ๊ฒŒ์‹œ ์ค‘๋‹จ, ์› ๊ฒŒ์‹œ์ž์˜ ์ด์˜์‹ ์ฒญ์— ๋”ฐ๋ฅธ ํ•ด๋‹น ๊ฒŒ์‹œ๋ฌผ ๊ฒŒ์‹œ ์žฌ๊ฐœ ๋“ฑ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“  ์„œ๋น„์Šค์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๊ฒŒ์‹œ๋ฌผ์— ๋Œ€ํ•ด ์ž„์‹œ๋กœ ๊ฒŒ์‹œ ์ค‘๋‹จ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ์ธ์˜ ์ €์ž‘๋ฌผ์„ ๋ฌด๋‹จ์œผ๋กœ ๋„์šฉ๋‹นํ•œ ๊ฒฝ์šฐ ๋ณธ์ธ์˜ ๋ช…์˜ˆ๋ฅผ ํ›ผ์†๋‹นํ•œ ๊ฒฝ์šฐ ๋ณธ์ธ์˜ ์ดˆ์ƒ๊ถŒ ๋ฐ ์‚ฌ์ƒํ™œ์„ ์นจํ•ด๋‹นํ•œ ๊ฒฝ์šฐ ์œ„ํ‚ค๋…์Šค๋Š” ์ €์ž‘๋ฌผ์˜ ๊ถŒ๋ฆฌ ์นจํ•ด ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ๊ฒŒ์‹œ๋ฌผ์ด ์‹ค์ œ๋กœ ๊ถŒ๋ฆฌ๋ฅผ ์นจํ•ดํ–ˆ๋Š”์ง€ ์ž„์˜๋กœ ํŒ๋‹จํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ถŒ๋ฆฌ ์นจํ•ด์— ๋Œ€ํ•œ ํŒ๋‹จ ๋ฐ ๊ฒŒ์‹œ๋ฌผ ์กฐ์น˜์— ๋Œ€ํ•œ ๊ฒฐ์ •์€ ์ ๋ฒ•ํ•œ ์ž๊ฒฉ์„ ๊ฐ–์ถ˜ ๊ด€๋ จ ๊ธฐ๊ด€์„ ํ†ตํ•ด ๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ด€๋ จ ๊ธฐ๊ด€์˜ ํŒ๋‹จ๊นŒ์ง€ ์‹œ๊ฐ„์ด ์†Œ์š”๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฒŒ์‹œ์ค‘๋‹จ์„ ํ†ตํ•ด ์šฐ์„  ์ž„์‹œ๋กœ ์กฐ์น˜ํ•ด ๋“œ๋ฆฌ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ž„์‹œ ์กฐ์น˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทผ๋ณธ์ ์ธ ๋ฌธ์ œ ํ•ด๊ฒฐ์€ ๋ฐ˜๋“œ์‹œ ๊ด€๋ จ ๊ธฐ๊ด€์„ ํ†ตํ•œ ๋ฒ•๋ฅ  ์ƒ๋‹ด ๋ฐ ๊ตฌ์ œ ์ ˆ์ฐจ๋ฅผ ํ†ตํ•ด ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ฐฐ์ฒญ: ๊ตญ๋ฒˆ ์—†์ด 1301 (ํœด๋Œ€์ „ํ™”: ์ง€์—ญ๋ฒˆํ˜ธ 1301)- ํ•ดํ‚น, ์ปดํ“จํ„ฐ๋ฐ”์ด๋Ÿฌ์Šค ์œ ํฌ, ์ „์ž์ƒ๊ฑฐ๋ž˜ ์‚ฌ๊ธฐ, ๊ฐœ์ธ ์ •๋ณด ์นจํ•ด ๊ด€๋ จ ์ˆ˜์‚ฌ ๊ฒฝ์ฐฐ์ฒญ ์‚ฌ์ด๋ฒ„์•ˆ์ „๊ตญ: ๊ตญ๋ฒˆ ์—†์ด 182 - ํ•ดํ‚น, ๋ฐ”์ด๋Ÿฌ์Šค, ๊ฐœ์ธ ์ •๋ณด ๋„์šฉ, ๊ฒŒ์ž„ ์‚ฌ๊ธฐ, ์ธํ„ฐ๋„ท ์‚ฌ๊ธฐ ๋“ฑ ๊ฐ์ข… ์‚ฌ์ด๋ฒ„ ๋ฒ”์ฃ„ ์‹ ๊ณ  ํ•œ๊ตญ์ €์ž‘๊ถŒ์œ„์›ํšŒ: 02-2660-0000 - ์ €์ž‘๊ถŒ ๋“ฑ๋ก, ์‹ฌ์˜์™€ ๋ถ„์Ÿ ์กฐ์ • ๋ฐ ์ƒ๋‹ด ๋ฐฉ์†กํ†ต์‹ ์‹ฌ์˜์œ„์›ํšŒ: 02-3219-5114,5333 - ์ •๋ณด ํ†ต์‹ ์ƒ์—์„œ์˜ ๊ฑด์ „ํ•œ ๋ฌธํ™” ์ฐฝ๋‹ฌ ๋ฐ ์ •๋ณด ํ†ต์‹ ์— ์˜ฌ๋ฐ”๋ฅธ ์ด์šฉ ํ™˜๊ฒฝ ์กฐ์„ฑ ๋ช…์˜ˆํ›ผ์† ๋ถ„์Ÿ์กฐ์ •๋ถ€: ๊ตญ๋ฒˆ ์—†์ด 1377 - ์ •๋ณดํ†ต์‹ ๋ง์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ช…์˜ˆํ›ผ์†๊ณผ ์„ฑํญ๋ ฅ ๋“ฑ ์‚ฌ์ด๋ฒ„ ๊ถŒ๋ฆฌ์นจํ•ด์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ƒ๋‹ด๊ณผ ๋ถ„์Ÿ ์กฐ์ • ํ•œ๊ตญ ์ธํ„ฐ๋„ท์ง„ํฅ์›: 02-405-4118,5118 - ๊ฐœ์ธ ์ •๋ณด ๋ณดํ˜ธ์™€ ๊ด€๋ จํ•œ ์ƒ๋‹ด ๋ฐ ๊ฐœ์ธ ์ •๋ณด ์นจํ•ด ์‹ ๊ณ  ํ•œํŽธ, ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ํ†ตํ•ด ํƒ€์ธ์˜ ์ฝ˜ํ…์ธ ๋ฅผ ์ด์šฉํ•œ๋‹ค๊ณ  ํ•˜์—ฌ ์—ฌ๋Ÿฌ๋ถ„์ด ํ•ด๋‹น ์ฝ˜ํ…์ธ ์— ๋Œ€ํ•œ ์ง€์‹ ์žฌ์‚ฐ๊ถŒ์„ ๋ณด์œ ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ํ•ด๋‹น ์ฝ˜ํ…์ธ ๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ทธ ์ด์šฉ์ด ์ €์ž‘๊ถŒ๋ฒ• ๋“ฑ ๊ด€๋ จ ๋ฒ•๋ฅ ์— ๋”ฐ๋ผ ํ—ˆ์šฉ๋˜๋Š” ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ฑฐ๋‚˜, ํ•ด๋‹น ์ฝ˜ํ…์ธ ์˜ ์ง€์‹ ์žฌ์‚ฐ๊ถŒ์ž๋กœ๋ถ€ํ„ฐ ๋ณ„๋„์˜ ์ด์šฉ ํ—ˆ๋ฝ์„ ๋ฐ›์•„์•ผ ํ•˜๋ฏ€๋กœ ๊ฐ๋ณ„ํ•œ ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ๋งˆ์Œ๊ป ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฌ๋Ÿฌ๋ถ„๊ป˜ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์— ์ˆ˜๋ฐ˜๋˜๋Š” ๊ด€๋ จ ์†Œํ”„ํŠธ์›จ์–ด ์‚ฌ์šฉ์— ๊ด€ํ•œ ์ด์šฉ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ž์œ ๋กœ์šด ์ด์šฉ์€ ์œ„ํ‚ค๋…์Šค๊ฐ€ ์ œ์‹œํ•˜๋Š” ์ด์šฉ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๋ฒ”์œ„ ๋‚ด์—์„œ๋งŒ ํ—ˆ์šฉ๋˜๊ณ , ์ด๋Ÿฌํ•œ ๊ถŒํ•œ์€ ์–‘๋„๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋น„๋…์ ์  ์กฐ๊ฑด ๋ฐ ๋ฒ•์  ๊ณ ์ง€๊ฐ€ ์ ์šฉ๋œ๋‹ค๋Š” ์ ์„ ์œ ์˜ํ•ด ์ฃผ์„ธ์š”. ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ๊ณผ ๊ด€๋ จํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€ ์ฃผ์˜์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ž์œ ๋กญ๊ณ  ํŽธ๋ฆฌํ•˜๊ฒŒ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ตœ์„ ์„ ๋‹คํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์—ฌ๋Ÿฌ๋ถ„์ด ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ๋”์šฑ ์•ˆ์ „ํ•˜๊ฒŒ ์ด์šฉํ•˜๊ณ  ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์—์„œ ์—ฌ๋Ÿฌ๋ถ„๊ณผ ํƒ€์ธ์˜ ๊ถŒ๋ฆฌ๊ฐ€ ์„œ๋กœ ์กด์ค‘๋˜๊ณ  ๋ณดํ˜ธ๋ฐ›์œผ๋ ค๋ฉด ์—ฌ๋Ÿฌ๋ถ„์˜ ๋„์›€๊ณผ ํ˜‘์กฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์˜ ์•ˆ์ „ํ•œ ์„œ๋น„์Šค ์ด์šฉ๊ณผ ๊ถŒ๋ฆฌ ๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด ๋ถ€๋“์ด ์•„๋ž˜์˜ ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ๋ถ„์˜ ๊ฒŒ์‹œ๋ฌผ ๊ฒŒ์žฌ๋‚˜ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ์ด ์ œํ•œ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ด์— ๋Œ€ํ•œ ํ™•์ธ ๋ฐ ์ค€์ˆ˜๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค. ํšŒ์› ๊ฐ€์ž… ์‹œ ์ด๋ฉ”์ผ ๋“ฑ์˜ ์ •๋ณด๋ฅผ ํ—ˆ์œ„๋กœ ๊ธฐ์žฌํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ํšŒ์› ๊ณ„์ •์— ๋“ฑ๋ก๋œ ์ •๋ณด๋Š” ํ•ญ์ƒ ์ •ํ™•ํ•œ ์ตœ์‹  ์ •๋ณด๊ฐ€ ์œ ์ง€๋  ์ˆ˜ ์žˆ๋„๋ก ๊ด€๋ฆฌํ•ด ์ฃผ์„ธ์š”. ์ž์‹ ์˜ ๊ณ„์ •์„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์—๊ฒŒ ํŒ๋งค, ์–‘๋„, ๋Œ€์—ฌ ๋˜๋Š” ๋‹ด๋ณด๋กœ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์—๊ฒŒ ๊ทธ ์‚ฌ์šฉ์„ ํ—ˆ๋ฝํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์•„์šธ๋Ÿฌ ์ž์‹ ์˜ ๊ณ„์ •์ด ์•„๋‹Œ ํƒ€์ธ์˜ ๊ณ„์ •์„ ๋ฌด๋‹จ์œผ๋กœ ์‚ฌ์šฉํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ํƒ€์ธ์— ๋Œ€ํ•ด ์ง์ ‘์ ์ด๊ณ  ๋ช…๋ฐฑํ•œ ์‹ ์ฒด์  ์œ„ํ˜‘์„ ๊ฐ€ํ•˜๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ, ํƒ€์ธ์˜ ์žํ•ด ํ–‰์œ„ ๋˜๋Š” ์ž์‚ด์„ ๋ถ€์ถ”๊ธฐ๊ฑฐ๋‚˜ ๊ถŒ์žฅํ•˜๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ, ํƒ€์ธ์˜ ์‹ ์ƒ์ •๋ณด, ์‚ฌ์ƒํ™œ ๋“ฑ ๋น„๊ณต๊ฐœ ๊ฐœ์ธ์ •๋ณด๋ฅผ ๋“œ๋Ÿฌ๋‚ด๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ, ํƒ€์ธ์„ ์ง€์†์ ์œผ๋กœ ๋”ฐ๋Œ๋ฆฌ๊ฑฐ๋‚˜ ๊ดด๋กญํžˆ๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ, ์„ฑ๋งค๋งค๋ฅผ ์ œ์•ˆ, ์•Œ์„ , ์œ ์ธ ๋˜๋Š” ๊ฐ•์š”ํ•˜๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ, ๊ณต๊ณต ์•ˆ์ „์— ๋Œ€ํ•ด ์ง์ ‘์ ์ด๊ณ  ์‹ฌ๊ฐํ•œ ์œ„ํ˜‘์„ ๊ฐ€ํ•˜๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ์€ ์ œํ•œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ด€๋ จ ๋ฒ•๋ น์ƒ ๊ธˆ์ง€๋˜๊ฑฐ๋‚˜ ํ˜•์‚ฌ์ฒ˜๋ฒŒ์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ํ–‰์œ„๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฑฐ๋‚˜ ์ด๋ฅผ ๊ต์‚ฌ ๋˜๋Š” ๋ฐฉ์กฐํ•˜๋Š” ๋“ฑ์˜ ๋ฒ”์ฃ„ ๊ด€๋ จ ์ง์ ‘์ ์ธ ์œ„ํ—˜์ด ํ™•์ธ๋œ ๊ฒŒ์‹œ๋ฌผ, ๊ด€๋ จ ๋ฒ•๋ น์—์„œ ํ™๋ณด, ๊ด‘๊ณ , ํŒ๋งค ๋“ฑ์„ ๊ธˆ์ง€ํ•˜๊ณ  ์žˆ๋Š” ๋ฌผ๊ฑด ๋˜๋Š” ์„œ๋น„์Šค๋ฅผ ํ™๋ณด, ๊ด‘๊ณ , ํŒ๋งคํ•˜๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ, ํƒ€์ธ์˜ ์ง€์‹ ์žฌ์‚ฐ๊ถŒ ๋“ฑ์„ ์นจํ•ดํ•˜๊ฑฐ๋‚˜ ๋ชจ์š•, ์‚ฌ์ƒํ™œ ์นจํ•ด ๋˜๋Š” ๋ช…์˜ˆํ›ผ์† ๋“ฑ ํƒ€์ธ์˜ ๊ถŒ๋ฆฌ๋ฅผ ์นจํ•ดํ•˜๋Š” ๋‚ด์šฉ์ด ํ™•์ธ๋œ ๊ฒŒ์‹œ๋ฌผ์€ ์ œํ•œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž๊ทน์ ์ด๊ณ  ๋…ธ๊ณจ์ ์ธ ์„ฑํ–‰์œ„๋ฅผ ๋ฌ˜์‚ฌํ•˜๋Š” ๋“ฑ ํƒ€์ธ์—๊ฒŒ ์„ฑ์  ์ˆ˜์น˜์‹ฌ์„ ์œ ๋ฐœํ•˜๊ฑฐ๋‚˜ ์™œ๊ณก๋œ ์„ฑ ์˜์‹ ๋“ฑ์„ ์•ผ๊ธฐํ•  ์ˆ˜ ์žˆ๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ, ํƒ€์ธ์—๊ฒŒ ์ž”ํ˜น๊ฐ ๋˜๋Š” ํ˜์˜ค๊ฐ์„ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋Š” ํญ๋ ฅ์ ์ด๊ณ  ์ž๊ทน์ ์ธ ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ, ๋ณธ์ธ ์ด์™ธ์˜ ์ž๋ฅผ ์‚ฌ์นญํ•˜๊ฑฐ๋‚˜ ํ—ˆ์œ„ ์‚ฌ์‹ค์„ ์ฃผ์žฅํ•˜๋Š” ๋“ฑ ํƒ€์ธ์„ ๊ธฐ๋งŒํ•˜๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ, ๊ณผ๋„ํ•œ ์š•์„ค, ๋น„์†์–ด ๋“ฑ์„ ๊ณ„์†ํ•˜์—ฌ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์‹ฌํ•œ ํ˜์˜ค๊ฐ ๋˜๋Š” ๋ถˆ์พŒ๊ฐ์„ ์ผ์œผํ‚ค๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ์€ ์ œํ•œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž๋™ํ™”๋œ ์ˆ˜๋‹จ์„ ํ™œ์šฉํ•˜๋Š” ๋“ฑ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์˜ ๊ธฐ๋Šฅ์„ ๋น„์ •์ƒ์ ์œผ๋กœ ์ด์šฉํ•˜์—ฌ ๊ฒŒ์žฌ๋œ ๊ฒŒ์‹œ๋ฌผ, ์œ„ํ‚ค๋…์Šค ๊ฐœ๋ณ„ ์„œ๋น„์Šค์˜ ์ œ๊ณต ์ทจ์ง€์™€ ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š” ๋‚ด์šฉ์˜ ๊ฒŒ์‹œ๋ฌผ์€ ๋‹ค๋ฅธ ์ด์šฉ์ž๋“ค์˜ ์ •์ƒ์ ์ธ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ์— ๋ถˆํŽธ์„ ์ดˆ๋ž˜ํ•˜๊ณ  ๋” ๋‚˜์•„๊ฐ€ ์œ„ํ‚ค๋…์Šค์˜ ์›ํ™œํ•œ ์„œ๋น„์Šค ์ œ๊ณต์„ ๋ฐฉํ•ดํ•˜๋ฏ€๋กœ ์—ญ์‹œ ์ œํ•œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์˜ ์‚ฌ์ „ ํ—ˆ๋ฝ ์—†์ด ์ž๋™ํ™”๋œ ์ˆ˜๋‹จ(์˜ˆ: ๋งคํฌ๋กœ ํ”„๋กœ๊ทธ๋žจ, ๋กœ๋ด‡(๋ด‡), ์ŠคํŒŒ์ด๋”, ์Šคํฌ๋ ˆ์ดํผ ๋“ฑ)์„ ์ด์šฉํ•˜์—ฌ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ํšŒ์›์œผ๋กœ ๊ฐ€์ž…์„ ์‹œ๋„ ๋˜๋Š” ๊ฐ€์ž…ํ•˜๊ฑฐ๋‚˜, ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์— ๋กœ๊ทธ์ธ์„ ์‹œ๋„ ๋˜๋Š” ๋กœ๊ทธ์ธํ•˜๊ฑฐ๋‚˜, ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์ƒ์— ๊ฒŒ์‹œ๋ฌผ์„ ๊ฒŒ์žฌํ•˜๊ฑฐ๋‚˜, ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ํ†ตํ•ด ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ํ•˜๊ฑฐ๋‚˜(์˜ˆ: ์ „์ž๋ฉ”์ผ, ์ชฝ์ง€ ๋“ฑ), ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์— ๊ฒŒ์žฌ๋œ ํšŒ์›์˜ ์•„์ด๋””(ID), ๊ฒŒ์‹œ๋ฌผ ๋“ฑ์„ ์ˆ˜์ง‘ํ•˜๊ฑฐ๋‚˜, ์œ„ํ‚ค๋…์Šค ๊ฒ€์ƒ‰ ์„œ๋น„์Šค์—์„œ ํŠน์ • ์งˆ์˜์–ด๋กœ ๊ฒ€์ƒ‰ํ•˜๊ฑฐ๋‚˜ ํ˜น์€ ๊ทธ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์—์„œ ํŠน์ • ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์„ ํƒ(์ด๋ฅธ๋ฐ” โ€˜ํด๋ฆญโ€™) ํ•˜๋Š” ๋“ฑ ์ด์šฉ์ž(์‚ฌ๋žŒ)์˜ ์‹ค์ œ ์ด์šฉ์„ ์ „์ œ๋กœ ํ•˜๋Š” ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์˜ ์ œ๊ณต ์ทจ์ง€์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š” ๋ฐฉ์‹์œผ๋กœ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๊ฑฐ๋‚˜ ์ด์™€ ๊ฐ™์€ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์— ๋Œ€ํ•œ ์–ด๋ทฐ์ง•(๋‚จ์šฉ) ํ–‰์œ„๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•œ ์œ„ํ‚ค๋…์Šค์˜ ๊ธฐ์ˆ ์  ์กฐ์น˜๋ฅผ ๋ฌด๋ ฅํ™”ํ•˜๋ ค๋Š” ๋ชจ๋“  ํ–‰์œ„(์˜ˆ: IP๋ฅผ ์ง€์†์ ์œผ๋กœ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ์ ‘์†ํ•˜๋Š” ํ–‰์œ„, Captcha๋ฅผ ์™ธ๋ถ€ ์„ค๋ฃจ์…˜ ๋“ฑ์„ ํ†ตํ•ด ์šฐํšŒํ•˜๊ฑฐ๋‚˜ ๋ฌด๋ ฅํ™”ํ•˜๋Š” ํ–‰์œ„ ๋“ฑ)๋ฅผ ์‹œ๋„ํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์˜ ๋™์˜ ์—†์ด ์ž๋™ํ™”๋œ ์ˆ˜๋‹จ์— ์˜ํ•ด ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์ƒ์— ๊ด‘๊ณ ๊ฐ€ ๊ฒŒ์žฌ๋˜๋Š” ์˜์—ญ ๋˜๋Š” ๊ทธ ๋ฐ–์˜ ์˜์—ญ์— ๋ถ€ํ˜ธ, ๋ฌธ์ž, ์Œ์„ฑ, ์Œํ–ฅ, ๊ทธ๋ฆผ, ์‚ฌ์ง„,<NAME>์ƒ, ๋งํฌ ๋“ฑ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฐ์ข… ์ฝ˜ํ…์ธ  ์ž์ฒด ๋˜๋Š” ํŒŒ์ผ์„ ์‚ฝ์ž…ํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ๋˜๋Š” ์ด์— ํฌํ•จ๋œ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ๋ณต์‚ฌ, ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ์ด๋ฅผ ํŒ๋งค, ์–‘๋„, ๋Œ€์—ฌ ๋˜๋Š” ๋‹ด๋ณด๋กœ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜ ํƒ€์ธ์—๊ฒŒ ๊ทธ ์ด์šฉ์„ ํ—ˆ๋ฝํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์— ํฌํ•จ๋œ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ์—ญ ์„ค๊ณ„, ์†Œ์Šค ์ฝ”๋“œ ์ถ”์ถœ ์‹œ๋„, ๋ณต์ œ, ๋ถ„ํ•ด, ๋ชจ๋ฐฉ, ๊ธฐํƒ€ ๋ณ€ํ˜•ํ•˜๋Š” ๋“ฑ์˜ ํ–‰์œ„๋„ ๊ธˆ์ง€๋ฉ๋‹ˆ๋‹ค(๋‹ค๋งŒ, ์˜คํ”ˆ์†Œ์Šค์— ํ•ด๋‹น๋˜๋Š” ๊ฒฝ์šฐ ๊ทธ ์ž์ฒด ์กฐ๊ฑด์— ๋”ฐ๋ฆ…๋‹ˆ๋‹ค). ๊ทธ ๋ฐ–์— ๋ฐ”์ด๋Ÿฌ์Šค๋‚˜ ๊ธฐํƒ€ ์•…์„ฑ์ฝ”๋“œ๋ฅผ ์—…๋กœ๋“œํ•˜๊ฑฐ๋‚˜ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์˜ ์›ํ™œํ•œ ์šด์˜์„ ๋ฐฉํ•ดํ•  ๋ชฉ์ ์œผ๋กœ ์„œ๋น„์Šค ๊ธฐ๋Šฅ์„ ๋น„์ •์ƒ์ ์œผ๋กœ ์ด์šฉํ•˜๋Š” ํ–‰์œ„ ์—ญ์‹œ ๊ธˆ์ง€๋ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ๋ณธ ์•ฝ๊ด€์˜ ๋ฒ”์œ„ ๋‚ด์—์„œ ๊ฒŒ์‹œ๋ฌผ ์šด์˜์ •์ฑ…, ๊ฐœ๋ณ„ ์„œ๋น„์Šค์—์„œ์˜ ์•ฝ๊ด€ ๋˜๋Š” ์šด์˜์ •์ฑ…, ๊ฐ ์„œ๋น„์Šค ์ƒ์˜ ์•ˆ๋‚ด, ๊ณต์ง€์‚ฌํ•ญ, ๊ณ ๊ฐ์„ผํ„ฐ ๋„์›€๋ง ๋“ฑ์„ ๋‘์–ด, ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ์•ˆ์ •์ ์ด๊ณ  ์›ํ™œํ•œ ์„œ๋น„์Šค ์ด์šฉ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์„ธ๋ถ€ ์ •์ฑ…์—๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณด๋‹ค ๊ตฌ์ฒด์ ์ธ ์œ ์˜ ์‚ฌํ•ญ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋‹ˆ, ๋ณธ ์•ฝ๊ด€ ๋ณธ๋ฌธ ๋ฐ ๊ตฌ์„ฑ ํŽ˜์ด์ง€ ์ƒ์˜ ๋งํฌ ๋“ฑ์„ ํ†ตํ•ด ์ด๋ฅผ ํ™•์ธํ•ด ์ฃผ์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋ถ€๋“์ด ์„œ๋น„์Šค ์ด์šฉ์„ ์ œํ•œํ•  ๊ฒฝ์šฐ ํ•ฉ๋ฆฌ์ ์ธ ์ ˆ์ฐจ๋ฅผ ์ค€์ˆ˜ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ๋‹ค์–‘ํ•œ ์ •๋ณด์™€ ์˜๊ฒฌ์ด ๋‹ด๊ธด ์—ฌ๋Ÿฌ๋ถ„์˜ ์ฝ˜ํ…์ธ ๋ฅผ ์†Œ์ค‘ํžˆ ๋‹ค๋ฃฐ ๊ฒƒ์„ ์•ฝ์†๋“œ๋ฆฝ๋‹ˆ๋‹ค๋งŒ, ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฒŒ์žฌํ•œ ๊ฒŒ์‹œ๋ฌผ์ด ๊ด€๋ จ ๋ฒ•๋ น, ๋ณธ ์•ฝ๊ด€, ๊ฒŒ์‹œ๋ฌผ ์šด์˜์ •์ฑ…, ๊ฐœ๋ณ„ ์„œ๋น„์Šค์—์„œ์˜ ์•ฝ๊ด€, ์šด์˜์ •์ฑ… ๋“ฑ์— ์œ„๋ฐฐ๋˜๋Š” ๊ฒฝ์šฐ, ๋ถ€๋“์ด ์ด๋ฅผ ๋น„๊ณต๊ฐœ ๋˜๋Š” ์‚ญ์ œ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜ ๊ฒŒ์žฌ๋ฅผ ๊ฑฐ๋ถ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ด๊ฒƒ์ด ์œ„ํ‚ค๋…์Šค๊ฐ€ ๋ชจ๋“  ์ฝ˜ํ…์ธ ๋ฅผ ๊ฒ€ํ† ํ•  ์˜๋ฌด๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์—ฌ๋Ÿฌ๋ถ„์ด ๊ด€๋ จ ๋ฒ•๋ น, ๋ณธ ์•ฝ๊ด€, ๊ณ„์ • ๋ฐ ๊ฒŒ์‹œ๋ฌผ ์šด์˜์ •์ฑ…, ๊ฐœ๋ณ„ ์„œ๋น„์Šค์—์„œ์˜ ์•ฝ๊ด€, ์šด์˜์ •์ฑ… ๋“ฑ์„ ์ค€์ˆ˜ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ, ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ๊ด€๋ จ ํ–‰์œ„ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ ํ™•์ธ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ์— ๋Œ€ํ•œ ์ฃผ์˜๋ฅผ ๋‹น๋ถ€ํ•˜๊ฑฐ๋‚˜, ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ์„ ์ผ๋ถ€ ๋˜๋Š” ์ „๋ถ€, ์ผ์‹œ ๋˜๋Š” ์˜๊ตฌํžˆ ์ •์ง€์‹œํ‚ค๋Š” ๋“ฑ ๊ทธ ์ด์šฉ์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œํŽธ, ์ด๋Ÿฌํ•œ ์ด์šฉ ์ œํ•œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋” ์ด์ƒ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ๊ณ„์•ฝ์˜ ์˜จ์ „ํ•œ ์œ ์ง€๋ฅผ ๊ธฐ๋Œ€ํ•˜๊ธฐ ์–ด๋ ค์šด ๊ฒฝ์šฐ์—” ๋ถ€๋“์ด ์—ฌ๋Ÿฌ๋ถ„๊ณผ์˜ ์ด์šฉ๊ณ„์•ฝ์„ ํ•ด์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถ€๋“์ด ์—ฌ๋Ÿฌ๋ถ„์˜ ์„œ๋น„์Šค ์ด์šฉ์„ ์ œํ•œํ•ด์•ผ ํ•  ๊ฒฝ์šฐ ๋ช…๋ฐฑํ•œ ๋ฒ•๋ น ์œ„๋ฐ˜์ด๋‚˜ ํƒ€์ธ์˜ ๊ถŒ๋ฆฌ์นจํ•ด๋กœ์„œ ๊ธด๊ธ‰ํ•œ ์œ„ํ—˜ ๋˜๋Š” ํ”ผํ•ด ์ฐจ๋‹จ์ด ์š”๊ตฌ๋˜๋Š” ์‚ฌ์•ˆ ์™ธ์—๋Š” ์œ„์™€ ๊ฐ™์€ ๋‹จ๊ณ„์  ์„œ๋น„์Šค ์ด์šฉ ์ œํ•œ ์›์น™์„ ์ค€์ˆ˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ฐฑํ•œ ๋ฒ•๋ น ์œ„๋ฐ˜ ๋“ฑ์„ ์ด์œ ๋กœ ๋ถ€๋“์ด ์„œ๋น„์Šค ์ด์šฉ์„ ์ฆ‰์‹œ ์˜๊ตฌ ์ •์ง€์‹œํ‚ค๋Š” ๊ฒฝ์šฐ ์„œ๋น„์Šค ์ด์šฉ์„ ํ†ตํ•ด ํš๋“ํ•œ ํฌ์ธํŠธ ๋ฐ ๊ธฐํƒ€ ํ˜œํƒ ๋“ฑ์€ ๋ชจ๋‘ ์†Œ๋ฉธํ•˜๊ณ  ์ด์— ๋Œ€ํ•ด ๋ณ„๋„๋กœ ๋ณด์ƒํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์œ ์˜ํ•ด ์ฃผ์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์„œ๋น„์Šค ์ด์šฉ ์ œํ•œ์˜ ์กฐ๊ฑด, ์„ธ๋ถ€ ๋‚ด์šฉ ๋“ฑ์€ ๊ณ„์ • ์šด์˜์ •์ฑ… ๋ฐ ๊ฐœ๋ณ„ ์„œ๋น„์Šค์—์„œ์˜ ์šด์˜์ •์ฑ…์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์˜ ์ž˜๋ชป์€ ์œ„ํ‚ค๋…์Šค๊ฐ€ ์ฑ…์ž„์ง‘๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐ ์œ„ํ‚ค๋…์Šค์˜ ๊ณ ์˜ ๋˜๋Š” ๊ณผ์‹ค๋กœ ์ธํ•˜์—ฌ ์†ํ•ด๋ฅผ ์ž…๊ฒŒ ๋  ๊ฒฝ์šฐ ๊ด€๋ จ ๋ฒ•๋ น์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ๋ถ„์˜ ์†ํ•ด๋ฅผ ๋ฐฐ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ฒœ์žฌ์ง€๋ณ€ ๋˜๋Š” ์ด์—<NAME>๋Š” ๋ถˆ๊ฐ€ํ•ญ๋ ฅ์œผ๋กœ ์ธํ•˜์—ฌ ์œ„ํ‚ค๋…์Šค๊ฐ€ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์—†๊ฑฐ๋‚˜ ์ด์šฉ์ž์˜ ๊ณ ์˜ ๋˜๋Š” ๊ณผ์‹ค๋กœ ์ธํ•˜์—ฌ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์—†์–ด ๋ฐœ์ƒํ•œ ์†ํ•ด์— ๋Œ€ํ•ด์„œ ์œ„ํ‚ค๋…์Šค๋Š” ์ฑ…์ž„์„ ๋ถ€๋‹ดํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„ํ‚ค๋…์Šค๊ฐ€ ์†ํ•ด๋ฐฐ์ƒ์ฑ…์ž„์„ ๋ถ€๋‹ดํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ํ†ต์ƒ์ ์œผ๋กœ ์˜ˆ๊ฒฌ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฑฐ๋‚˜ ํŠน๋ณ„ํ•œ ์‚ฌ์ •์œผ๋กœ ์ธํ•œ ํŠน๋ณ„ ์†ํ•ด ๋˜๋Š” ๊ฐ„์ ‘ ์†ํ•ด, ๊ธฐํƒ€ ์ง•๋ฒŒ์  ์†ํ•ด์— ๋Œ€ํ•ด์„œ๋Š” ๊ด€๋ จ ๋ฒ•๋ น์— ํŠน๋ณ„ํ•œ ๊ทœ์ •์ด ์—†๋Š” ํ•œ ์ฑ…์ž„์„ ๋ถ€๋‹ดํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•œํŽธ, ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ๋งค๊ฐœ๋กœ ํ•œ ์—ฌ๋Ÿฌ๋ถ„๊ณผ ๋‹ค๋ฅธ ํšŒ์› ๊ฐ„ ๋˜๋Š” ์—ฌ๋Ÿฌ๋ถ„๊ณผ ๋น„ํšŒ์› ๊ฐ„์˜ ์˜๊ฒฌ ๊ตํ™˜, ๊ฑฐ๋ž˜ ๋“ฑ์—์„œ ๋ฐœ์ƒํ•œ ์†ํ•ด๋‚˜ ์—ฌ๋Ÿฌ๋ถ„์ด ์„œ๋น„์Šค ์ƒ์— ๊ฒŒ์žฌ๋œ ํƒ€์ธ์˜ ๊ฒŒ์‹œ๋ฌผ ๋“ฑ์˜ ์ฝ˜ํ…์ธ ๋ฅผ ์‹ ๋ขฐํ•จ์œผ๋กœ์จ ๋ฐœ์ƒํ•œ ์†ํ•ด์— ๋Œ€ํ•ด์„œ๋„ ์œ„ํ‚ค๋…์Šค๋Š” ํŠน๋ณ„ํ•œ ์‚ฌ์ •์ด ์—†์œผ๋ฉด ์ด์— ๋Œ€ํ•ด ์ฑ…์ž„์„ ๋ถ€๋‹ดํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์–ธ์ œ๋“ ์ง€ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ๊ณ„์•ฝ์„ ํ•ด์ง€ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์—๊ฒŒ๋Š” ์ฐธ ์•ˆํƒ€๊นŒ์šด ์ผ์ž…๋‹ˆ๋‹ค๋งŒ, ํšŒ์›์€ ์–ธ์ œ๋“ ์ง€ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ๊ณ„์•ฝ ํ•ด์ง€๋ฅผ ์‹ ์ฒญํ•˜์—ฌ ํšŒ์›์—์„œ ํƒˆํ‡ดํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ๊ฒฝ์šฐ ์œ„ํ‚ค๋…์Šค๋Š” ๊ด€๋ จ ๋ฒ•๋ น ๋“ฑ์ด ์ •ํ•˜๋Š” ๋ฐ”์— ๋”ฐ๋ผ ์ด๋ฅผ ์ง€์ฒด ์—†์ด ์ฒ˜๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค ์ด์šฉ๊ณ„์•ฝ์ด ํ•ด์ง€๋˜๋ฉด, ๊ด€๋ จ ๋ฒ•๋ น ๋ฐ ๊ฐœ์ธ์ •๋ณด ์ฒ˜๋ฆฌ ๋ฐฉ์นจ์— ๋”ฐ๋ผ ์œ„ํ‚ค๋…์Šค๊ฐ€ ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด๋ฅผ ๋ณด์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ , ํ•ด๋‹น ํšŒ์› ๊ณ„์ •์— ๋ถ€์†๋œ ๊ฒŒ์‹œ๋ฌผ ์ผ์ฒด๋ฅผ ํฌํ•จํ•œ ํšŒ์›์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋Š” ์†Œ๋ฉธํ•จ๊ณผ ๋™์‹œ์— ๋ณต๊ตฌํ•  ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ด ๊ฒฝ์šฐ์—๋„ ๋‹ค๋ฅธ ํšŒ์›์ด ๋ณ„๋„๋กœ ๋‹ด์•„ ๊ฐ”๊ฑฐ๋‚˜ ์Šคํฌ๋žฉํ•œ ๊ฒŒ์‹œ๋ฌผ๊ณผ ๊ณต์šฉ ๊ฒŒ์‹œํŒ์— ๋“ฑ๋กํ•œ ๋Œ“๊ธ€ ๋“ฑ์˜ ๊ฒŒ์‹œ๋ฌผ์€ ์‚ญ์ œ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ํ•ด์ง€ ์‹ ์ฒญ ์ด์ „์— ์‚ญ์ œํ•˜์‹  ํ›„ ํƒˆํ‡ดํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ผ๋ถ€ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์—๋Š” ๊ด‘๊ณ ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ๋‹ค์–‘ํ•œ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๋‹ค ๋ณด๋ฉด ๊ฐ„ํ˜น ์ผ๋ถ€ ๊ฐœ๋ณ„ ์„œ๋น„์Šค์— ๊ด‘๊ณ ๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๋ฉด์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ํ†ต์‹  ์š”๊ธˆ์€ ๊ฐ€์ž…ํ•˜์‹  ํ†ต์‹  ์‚ฌ์—…์ž์™€์˜ ์ด์šฉ๊ณ„์•ฝ์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ๋ถ„์ด ๋ถ€๋‹ดํ•˜๋ฉฐ, ํฌํ•จ๋œ ๊ด‘๊ณ  ์—ด๋žŒ์œผ๋กœ ์ธํ•ด ์ถ”๊ฐ€๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋น„์šฉ ์—ญ์‹œ ์—ฌ๋Ÿฌ๋ถ„์ด ๋ถ€๋‹ดํ•ฉ๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด ์›ํ•˜์ง€ ์•Š๋Š” ๊ด‘๊ณ ๋ฅผ ๋ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ์ œ๊ณตํ•˜๋Š” ๋‹ค์–‘ํ•œ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค๋ฅผ ์›์น™์ ์œผ๋กœ ๋ฌด๋ฃŒ๋กœ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋ฐ ๊ธฐ์—ฌํ•˜๋ฉฐ, ๋” ๋‚˜์•„๊ฐ€ ์œ„ํ‚ค๋…์Šค๊ฐ€ ์—ฐ๊ตฌ ๊ฐœ๋ฐœ์— ํˆฌ์žํ•˜์—ฌ ๋” ๋‚˜์€ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ฐ˜์ด ๋ฉ๋‹ˆ๋‹ค. ์ตœ๊ทผ ํƒ€์‚ฌ์˜ ์ผ๋ถ€ ์„œ๋น„์Šค๋“ค์ด ๊ด‘๊ณ  ์—†๋Š” ์„œ๋น„์Šค ์ด์šฉ์„ ๊ฐ•์กฐํ•˜๋ฉฐ ์ฃผ๋œ ์„œ๋น„์Šค๋ฅผ ์œ ๋ฃŒ๋กœ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š” ๊ด€ํ–‰์ด ์ด๋ฅผ ๋’ท๋ฐ›์นจํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ณธ์˜ ์•„๋‹Œ ๋ถˆํŽธ์ด๋‚˜ ๋ถ€๋‹ด์ด ์ตœ์†Œํ™”๋  ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ํ•ญ์ƒ ๊ณ ๋ฏผํ•˜๊ณ  ๊ฐœ์„ ํ•ด ๋‚˜๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. ์„œ๋น„์Šค ์ค‘๋‹จ ๋˜๋Š” ๋ณ€๊ฒฝ ์‹œ ๊ผญ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฐ์ค‘๋ฌดํœด, 1์ผ 24์‹œ๊ฐ„ ์•ˆ์ •์ ์œผ๋กœ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์„ ์„ ๋‹คํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค๋งŒ, ์ปดํ“จํ„ฐ, ์„œ๋ฒ„ ๋“ฑ ์ •๋ณดํ†ต์‹  ์„ค๋น„์˜ ๋ณด์ˆ˜ ์ ๊ฒ€, ๊ต์ฒด ๋˜๋Š” ๊ณ ์žฅ, ํ†ต์‹  ๋‘์ ˆ ๋“ฑ ์šด์˜์ƒ ํƒ€๋‹นํ•œ ์ด์œ ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋ถ€๋“์ด ์„œ๋น„์Šค์˜ ์ „๋ถ€ ๋˜๋Š” ์ผ๋ถ€๋ฅผ ์ค‘๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œํŽธ, ์œ„ํ‚ค๋…์Šค๋Š” ์„œ๋น„์Šค ์šด์˜ ๋˜๋Š” ๊ฐœ์„ ์„ ์œ„ํ•ด ์ƒ๋‹นํ•œ ํ•„์š”์„ฑ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์„œ๋น„์Šค์˜ ์ „๋ถ€ ๋˜๋Š” ์ผ๋ถ€๋ฅผ ์ˆ˜์ •, ๋ณ€๊ฒฝ ๋˜๋Š” ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌด๋ฃŒ๋กœ ์ œ๊ณต๋˜๋Š” ์„œ๋น„์Šค์˜ ์ „๋ถ€ ๋˜๋Š” ์ผ๋ถ€๋ฅผ ์ˆ˜์ •, ๋ณ€๊ฒฝ ๋˜๋Š” ์ข…๋ฃŒํ•˜๊ฒŒ ๋œ ๊ฒฝ์šฐ ๊ด€๋ จ ๋ฒ•๋ น์— ํŠน๋ณ„ํ•œ ๊ทœ์ •์ด ์—†๋Š” ํ•œ ๋ณ„๋„์˜ ๋ณด์ƒ์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์œ„ํ‚ค๋…์Šค๋Š” ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์ƒ๋‹น ๊ธฐ๊ฐ„ ์ „์— ์ด๋ฅผ ์•ˆ๋‚ดํ•˜๋ฉฐ, ๋งŒ์•ฝ ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ผ๋ฉด ์‚ฌํ›„ ์ง€์ฒด ์—†์ด ์ƒ์„ธํžˆ ์„ค๋ช…ํ•˜๊ณ  ์•ˆ๋‚ดํ•ด ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์„œ๋น„์Šค ์ค‘๋‹จ์˜ ๊ฒฝ์šฐ์—๋Š” ์—ฌ๋Ÿฌ๋ถ„ ์ž์‹ ์˜ ์ฝ˜ํ…์ธ ๋ฅผ ๋ฐฑ์—…ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋ฆฌ์ ์ด๊ณ  ์ถฉ๋ถ„ํ•œ ๊ธฐํšŒ๋ฅผ ์ œ๊ณตํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผ์š” ์‚ฌํ•ญ์„ ์ž˜ ์•ˆ๋‚ดํ•˜๊ณ  ์—ฌ๋Ÿฌ๋ถ„์˜ ์†Œ์ค‘ํ•œ ์˜๊ฒฌ์— ๊ท€ ๊ธฐ์šธ์ด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์„œ๋น„์Šค ์ด์šฉ์— ํ•„์š”ํ•œ ์ฃผ์š” ์‚ฌํ•ญ์„ ์ ์‹œ์— ์ž˜ ์•ˆ๋‚ดํ•ด ๋“œ๋ฆด ์ˆ˜ ์žˆ๋„๋ก ํž˜์“ฐ๊ฒ ์Šต๋‹ˆ๋‹ค. ํšŒ์›์—๊ฒŒ ํ†ต์ง€ํ•˜๋Š” ๊ฒฝ์šฐ ์ „์ž๋ฉ”์ผ, ์„œ๋น„์Šค ๋‚ด ์•Œ๋ฆผ ๋˜๋Š” ๊ธฐํƒ€ ์ ์ ˆํ•œ ์ „์ž์  ์ˆ˜๋‹จ์„ ํ†ตํ•ด ๊ฐœ๋ณ„์ ์œผ๋กœ ์•Œ๋ ค ๋“œ๋ฆด ๊ฒƒ์ด๋ฉฐ, ๋‹ค๋งŒ ํšŒ์› ์ „์ฒด์— ๋Œ€ํ•œ ํ†ต์ง€๊ฐ€ ํ•„์š”ํ•  ๊ฒฝ์šฐ์—” 7์ผ ์ด์ƒ wikidocs.net์„ ๋น„๋กฏํ•œ ์œ„ํ‚ค๋…์Šค ๋„๋ฉ”์ธ์˜ ์›น์‚ฌ์ดํŠธ ๋ฐ ์‘์šฉํ”„๋กœ๊ทธ๋žจ(์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ์•ฑ) ์ดˆ๊ธฐ ํ™”๋ฉด ๋˜๋Š” ๊ณต์ง€์‚ฌํ•ญ ๋“ฑ์— ๊ด€๋ จ ๋‚ด์šฉ์„ ๊ฒŒ์‹œํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ์†Œ์ค‘ํ•œ ์˜๊ฒฌ์— ๊ท€ ๊ธฐ์šธ์ด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ์–ธ์ œ๋“ ์ง€ ๊ณ ๊ฐ์„ผํ„ฐ๋ฅผ ํ†ตํ•ด ์„œ๋น„์Šค ์ด์šฉ๊ณผ ๊ด€๋ จ๋œ ์˜๊ฒฌ์ด๋‚˜ ๊ฐœ์„ ์‚ฌํ•ญ์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์œ„ํ‚ค๋…์Šค๋Š” ํ•ฉ๋ฆฌ์  ๋ฒ”์œ„ ๋‚ด์—์„œ ๊ฐ€๋Šฅํ•œ ๊ทธ ์ฒ˜๋ฆฌ ๊ณผ์ • ๋ฐ ๊ฒฐ๊ณผ๋ฅผ ์—ฌ๋Ÿฌ๋ถ„๊ป˜ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์ „์ž์ฑ… ํŒ๋งค์™€ ๊ด€๋ จํ•˜์—ฌ ๋ช‡ ๊ฐ€์ง€ ์ฃผ์˜์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค์—์„œ ์ „์ž์ฑ…์„ ํŒ๋งคํ•˜๋Š” ํšŒ์›์€ ๋‹ค์Œ์˜ ๋‚ด์šฉ์— ๋Œ€ํ•œ ํ™•์ธ ๋ฐ ์ค€์ˆ˜๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค. ์ „์ž์ฑ… ํŒ๋งค์˜ ํ๋ฆ„ (์—ญํ•  ๋ถ„๋‹ด) ์œ„ํ‚ค๋…์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ˆ์ฐจ์™€ ์—ญํ• ์— ๋”ฐ๋ผ ์ „์ž์ฑ…์„ ํŒ๋งคํ•ฉ๋‹ˆ๋‹ค. ํšŒ์›์ด ์˜จ๋ผ์ธ์—์„œ ์ฑ…์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ํšŒ์›์ด ์œ„ํ‚ค๋…์Šค ๊ด€๋ฆฌ์ž์—๊ฒŒ ์ฑ… ํŒ๋งค๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์ž์ฒด ์‹ฌ์‚ฌ ๊ณผ์ •์„ ํ†ตํ•ด ์ฑ… ํŒ๋งค ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ํšŒ์›์˜ ์ „์ž์ฑ…(PDF)์„ ์œ„ํ‚ค๋…์Šค๊ฐ€ ํŒ๋งคํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์ต์›”์— ํ•ด๋‹น ํšŒ์›์—๊ฒŒ ์ธ์„ธ๋ฅผ ์ •์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ „์ž์ฑ… ํŒ๋งค์— ๋Œ€ํ•œ ์‹ฌ์‚ฌ ์ ˆ์ฐจ ์ „์ž์ฑ… ํŒ๋งค์˜ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋Š” ์œ„ํ‚ค๋…์Šค๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ๋“ค์„ ๊ณ ๋ คํ•˜์—ฌ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ฑ…์˜ ํ’ˆ์งˆ ์ฑ…์„ ์ž‘์„ฑํ•œ ๊ธฐ๊ฐ„ ๋…์ž๋“ค์˜ ํ”ผ๋“œ๋ฐฑ ์ฑ…์˜ ๋ถ„๋Ÿ‰ ์ฑ…์˜ ๋‹จ๊ฐ€ ์ €์ž‘๊ถŒ ์นจํ•ด ์—ฌ๋ถ€ (๋‚ด์šฉ ๋ฐ ์ฒจ๋ถ€ ์ด๋ฏธ์ง€ ๋“ฑ) ์ €์ž‘๊ถŒ ์นจํ•ด ์ €์ž‘๊ถŒ ์นจํ•ด์— ๋Œ€ํ•œ ํŒ๋‹จ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ๊ด€๋ฆฌ์ž๊ฐ€ ์ฑ…์„ ์ˆ˜๋™์œผ๋กœ ๊ฒ€ํ†  ํ•ด๋‹น ์ฑ…์˜ ๋Œ“๊ธ€, ํ”ผ๋“œ๋ฐฑ์„ ํ†ตํ•œ ์‹ ๊ณ  ์ ‘์ˆ˜ ์ด๋ฉ”์ผ์„ ํ†ตํ•œ ๋…์ž๋“ค์˜ ์‹ ๊ณ  ์ ‘์ˆ˜ ๋งŒ์•ฝ, ์ €์ž‘๊ถŒ ์นจํ•ด๊ฐ€ ์ธ์ •๋œ๋‹ค๊ณ  ํŒ๋‹จ๋  ๊ฒฝ์šฐ ์œ„ํ‚ค๋…์Šค์—์„œ ํ•ด๋‹น ์ฑ…์˜ ํŒ๋งค ์ค‘์ง€์™€ ๋”๋ถˆ์–ด ์ €์ž‘๊ถŒ ์นจํ•ด๊ฐ€ ์žˆ๋Š” ํŽ˜์ด์ง€๋ฅผ ๋น„๊ณต๊ฐœ ํŽ˜์ด์ง€๋กœ ์ „ํ™˜ํ•œ ํ›„์— ์ €์ž์—๊ฒŒ ํ†ต์ง€ํ•˜์—ฌ ํ›„์† ์กฐ์น˜๋ฅผ ์ทจํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €์ž‘๊ถŒ ์นจํ•ด์— ๋Œ€ํ•œ ๋ณด๋‹ค ์ž์„ธํ•œ ๋‚ด์šฉ์€ "ํƒ€์ธ์˜ ๊ถŒ๋ฆฌ๋ฅผ ์กด์ค‘ํ•ด ์ฃผ์„ธ์š”" ํ•ญ๋ชฉ์„ ์ฐธ์กฐํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ์•ฝ๊ด€ ๋ฐ ์šด์˜์ •์ฑ…์„ ๊ฒŒ์‹œํ•˜๋ฉฐ ์‚ฌ์ „ ๊ณต์ง€ ํ›„ ๊ฐœ์ •ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ๋ณธ ์•ฝ๊ด€์˜ ๋‚ด์šฉ์„ ์—ฌ๋Ÿฌ๋ถ„์ด ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ์„œ๋น„์Šค ์ดˆ๊ธฐ ํ™”๋ฉด์— ๊ฒŒ์‹œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ์ˆ˜์‹œ๋กœ ๋ณธ ์•ฝ๊ด€, ๊ณ„์ • ๋ฐ ๊ฒŒ์‹œ๋ฌผ ์šด์˜์ •์ฑ…์„ ๊ฐœ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค๋งŒ, ๊ด€๋ จ ๋ฒ•๋ น์„ ์œ„๋ฐฐํ•˜์ง€ ์•Š๋Š” ๋ฒ”์œ„ ๋‚ด์—์„œ ๊ฐœ์ •ํ•  ๊ฒƒ์ด๋ฉฐ, ์‚ฌ์ „์— ๊ทธ ๊ฐœ์ • ์ด์œ ์™€ ์ ์šฉ ์ผ์ž๋ฅผ ์„œ๋น„์Šค ๋‚ด์— ์•Œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ๋ถˆ๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ค‘๋Œ€ํ•œ ๋‚ด์šฉ์˜ ์•ฝ๊ด€ ๋ณ€๊ฒฝ์˜ ๊ฒฝ์šฐ์—๋Š” ์ตœ์†Œ 30์ผ ์ด์ „์— ํ•ด๋‹น ์„œ๋น„์Šค ๋‚ด ๊ณต์ง€ํ•˜๊ณ  ๋ณ„๋„์˜ ์ „์ž์  ์ˆ˜๋‹จ(์ „์ž๋ฉ”์ผ, ์„œ๋น„์Šค ๋‚ด ์•Œ๋ฆผ ๋“ฑ)์„ ํ†ตํ•ด ๊ฐœ๋ณ„์ ์œผ๋กœ ์•Œ๋ฆด ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋Š” ๋ณ€๊ฒฝ๋œ ์•ฝ๊ด€์„ ๊ฒŒ์‹œํ•œ ๋‚ ๋กœ๋ถ€ํ„ฐ ํšจ๋ ฅ์ด ๋ฐœ์ƒํ•˜๋Š” ๋‚ ๊นŒ์ง€ ์•ฝ๊ด€ ๋ณ€๊ฒฝ์— ๋Œ€ํ•œ ์—ฌ๋Ÿฌ๋ถ„์˜ ์˜๊ฒฌ์„ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค. ์œ„ ๊ธฐ๊ฐ„์ด ์ง€๋‚˜๋„๋ก ์—ฌ๋Ÿฌ๋ถ„์˜ ์˜๊ฒฌ์ด ์œ„ํ‚ค๋…์Šค์— ์ ‘์ˆ˜๋˜์ง€ ์•Š์œผ๋ฉด, ์—ฌ๋Ÿฌ๋ถ„์ด ๋ณ€๊ฒฝ๋œ ์•ฝ๊ด€์— ๋”ฐ๋ผ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐ์— ๋™์˜ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค๋กœ์„œ๋Š” ๋งค์šฐ ์•ˆํƒ€๊นŒ์šด ์ผ์ด์ง€๋งŒ, ์—ฌ๋Ÿฌ๋ถ„์ด ๋ณ€๊ฒฝ๋œ ์•ฝ๊ด€์— ๋™์˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋ณ€๊ฒฝ๋œ ์•ฝ๊ด€์˜ ์ ์šฉ์„ ๋ฐ›๋Š” ํ•ด๋‹น ์„œ๋น„์Šค์˜ ์ œ๊ณต์ด ๋” ์ด์ƒ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณธ ์•ฝ๊ด€์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค๋งŒ, ๋ถ€๋“์ด ๊ฐ ๊ฐœ๋ณ„ ์„œ๋น„์Šค์˜ ๊ณ ์œ ํ•œ ํŠน์„ฑ์„ ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด ๋ณธ ์•ฝ๊ด€ ์™ธ ๋ณ„๋„์˜ ์•ฝ๊ด€, ์šด์˜์ •์ฑ…์ด ์ถ”๊ฐ€๋กœ ์ ์šฉ๋  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ณ„๋„์˜ ์•ฝ๊ด€, ์šด์˜์ •์ฑ…์—์„œ ๊ทธ ๊ฐœ๋ณ„ ์„œ๋น„์Šค ์ œ๊ณต์— ๊ด€ํ•˜์—ฌ ๋ณธ ์•ฝ๊ด€, ๊ณ„์ • ๋ฐ ๊ฒŒ์‹œ๋ฌผ ์šด์˜์ •์ฑ…๊ณผ ๋‹ค๋ฅด๊ฒŒ ์ •ํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ณ„๋„์˜ ์•ฝ๊ด€, ์šด์˜์ •์ฑ…์ด ์šฐ์„ ํ•˜์—ฌ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋‚ด์šฉ์€ ๊ฐ๊ฐ์˜ ๊ฐœ๋ณ„ ์„œ๋น„์Šค ์ดˆ๊ธฐ ํ™”๋ฉด์—์„œ ํ™•์ธํ•ด ์ฃผ์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋ณธ ์•ฝ๊ด€์€ ํ•œ๊ตญ์–ด๋ฅผ ์ •๋ณธ์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๋ณธ ์•ฝ๊ด€ ๋˜๋Š” ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์™€ ๊ด€๋ จ๋œ ์—ฌ๋Ÿฌ๋ถ„๊ณผ ์œ„ํ‚ค๋…์Šค์™€์˜ ๊ด€๊ณ„์—๋Š” ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ๋ฒ•๋ น์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ณธ ์•ฝ๊ด€ ๋˜๋Š” ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์™€ ๊ด€๋ จํ•˜์—ฌ ์—ฌ๋Ÿฌ๋ถ„๊ณผ ์œ„ํ‚ค๋…์Šค ์‚ฌ์ด์— ๋ถ„์Ÿ์ด ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ, ๊ทธ ๋ถ„์Ÿ์˜ ์ฒ˜๋ฆฌ๋Š” ๋Œ€ํ•œ๋ฏผ๊ตญ '๋ฏผ์‚ฌ์†Œ์†ก๋ฒ•'์—์„œ ์ •ํ•œ ์ ˆ์ฐจ๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ์ ์šฉ ์ผ์ž: 2022๋…„ 11์›” 07์ผ ์œ„ํ‚ค๋…์Šค ์„œ๋น„์Šค์™€ ๊ด€๋ จํ•˜์—ฌ ๊ถ๊ธˆํ•˜์‹  ์‚ฌํ•ญ์ด ์žˆ์œผ์‹œ๋ฉด <EMAIL>์œผ๋กœ ๋ฌธ์˜ํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค.<|endoftext|>
์ „๋ฌธ๋ถ„์•ผ ํ…์ŠคํŠธ ### ์ œ๋ชฉ: Must Learning with R (๊ฐœ์ •ํŒ) ### ๋ณธ๋ฌธ: MustLearning with R ๊ฐœ์ •ํŒ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด ์ œ์ž‘ํ•œ ์ฑ…์—์„œ ๋‹ค์‹œ ๋งŒ๋“ค๋ ค๊ณ  ํ–ˆ์œผ๋‚˜, ์ฑ…์˜ ๊ตฌ์„ฑ์ด ์–ด๋Š ์ •๋„ ๋ฐ”๋€ ๋ถ€๋ถ„๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ์ƒˆ๋กญ๊ฒŒ ๊ตฌ์„ฑ์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์€ ๋‚ด์šฉ์ด ๋น„์Šทํ•˜์ง€๋งŒ, ์ถ”๊ฐ€๋œ ๋‚ด์šฉ๋„ ๋งŽ์Šต๋‹ˆ๋‹ค. ๋ธ”๋กœ๊ทธ ์ฃผ์†Œ : https://mustlearning.tistory.com/ ================================================ MustLearning With R(MLR)์— ๋งŽ์€ ๊ด€์‹ฌ์„ ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. MLR์—์„œ ๋” ์ข‹์€ ๋‚ด์šฉ์œผ๋กœ ์ƒˆ๋กœ์šด ์ฑ…์ด ์ถœ๊ฐ„์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋งŽ์€ ๊ด€์‹ฌ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ฒต ์ œ๋ชฉ: ์‹ค๋ฌด ํ”„๋กœ์ ํŠธ๋กœ ๋ฐฐ์šฐ๋Š” ๋ฐ์ดํ„ฐ ๋ถ„์„ with R (์—…๋ฌด์— ๊ณง๋ฐ”๋กœ ์จ๋จน๋Š” R ์‹ค์ „ ํ™œ์šฉ๋ฒ•) yes24: http://www.yes24.com/Product/Goods/101441297 ================================================ ์˜จ๋ผ์ธ ๊ฐ•์˜ ๋ก ์นญ: https://class101.page.link/pDWB ํด๋ž˜์Šค 101์—์„œ ์˜จ๋ผ์ธ ๊ฐ•์˜๋ฅผ ๋ก ์นญํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋งŽ์€ ๊ด€์‹ฌ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค ๋ฐ์ดํ„ฐ ๋ถ„์„์— ์ƒ์†Œํ•œ ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•œ R ๋ฐ์ดํ„ฐ ๋ถ„์„ ๊ธฐ๋ณธ์„œ ์ด ์ž๋ฃŒ๋ฅผ ํ†ตํ•ด, ๋งŽ์€ ๋ถ„๋“ค์ด ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ์ง„์ž… ์žฅ๋ฒฝ์„ ๊ทน๋ณตํ•˜์…จ์œผ๋ฉด ํ•˜๋Š” ๋ฐ”๋žŒ์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ž‘์„ฑ์ž : KK.Park, HJ.Oh, NY.Kim ์ด์ œ๋Š” R์„ R ์•„์•ผ ํ•  ๋•Œ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ์—‘์…€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑํ•˜๊ฑฐ๋‚˜, SPSS๋ฅผ ํ†ตํ•ด ๋…ผ๋ฌธ์„ ๋ถ„์„ํ•œ๋‹ค๊ฑฐ๋‚˜ ๊ทธ๋Ÿฐ ์‹œ๋Œ€๋Š” ๋๋‚˜๊ฐ€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŽ์€ ๊ธฐ์—…, ์—ฐ๊ตฌ๋ถ„์•ผ์—์„œ R, python ๋“ฑ์„ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, R ํ”„๋กœ๊ทธ๋žจ์— ๋Œ€ํ•œ ์–ด๋ ค์›€๊ณผ ๋™์‹œ์— ํ†ต๊ณ„ํ•™์— ๋Œ€ํ•œ ์ง„์ž…์žฅ๋ฒฝ์ด ์ƒ๋‹นํžˆ ๋†’์•„, ๋งŽ์€ ๋ถ„๋“ค์ด ์–ด๋ ค์›€์„ ๊ฒช๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ฑ…์€ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด R๊ณผ ํ†ต๊ณ„ํ•™์„ ๊ณต๋ถ€ํ•˜๋Š”๋ฐ, ์ง„์ž…์žฅ๋ฒฝ์„ ํ—ˆ๋ฌผ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋ชฉํ‘œ๋กœ ์ œ์ž‘๋œ ์ฑ…์ž…๋‹ˆ๋‹ค. ์ฑ…์˜ ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. R์— ์นœ์ˆ™ํ•ด์ง€๊ธฐ ํ•„์ˆ˜ ํ†ต๊ณ„ํ•™ ์ง€์‹ ์ตํžˆ๊ธฐ ํ•„์ˆ˜ ํ†ต๊ณ„ ๋ชจํ˜• ์ง€์‹ ์ตํžˆ๊ธฐ ๊ธฐ๊ณ„ํ•™์Šต ๊ฐœ๋ก  ํ†ต๊ณ„ํ•™์€ ๋งค์šฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. R๋„ ์ฒ˜์Œ ํ•˜๋Š” ์‚ฌ๋žŒ์—๊ฒŒ ์‰ฝ์ง€๋งŒ์€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋จผ์ € R์— ๋Œ€ํ•ด ์นœ์ˆ™ํ•ด์ง€๋Š” ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„, ํ†ต๊ณ„ํ•™์— ๋Œ€ํ•ด ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๋” ๋‚˜์•„๊ฐ€, ๊ธฐ๊ณ„ํ•™์Šต ์ž…๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ๋„ ๋‹ค๋ฃจ๋ฉด์„œ ์ด ์ฑ…์„ ๋ณด์‹œ๋Š” ๋ถ„๋“ค์ด ๋ฐ์ดํ„ฐ ๋ถ„์„์— ์กฐ๊ธˆ ๋” ์นœ์ˆ™ํ•จ์„ ๋Š๋ผ์…จ์œผ๋ฉด ํ•ฉ๋‹ˆ๋‹ค. ์˜คํƒ€, ์˜ค๋ฅ˜ ์ˆ˜์ • : <EMAIL> Ch1. Intro Ch1์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์™œ ํ†ต๊ณ„ํ•™์„ ๊ณต๋ถ€ํ•ด์•ผ ํ•˜๋Š”์ง€, ์™œ R๊ณผ Python ๋“ฑ ์–ด๋ ค์šด ์–ธ์–ด๋ฅผ ๊ณต๋ถ€๋ฅผ ํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ๊ธฐ๋ณธ์ ์ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. A1. ์„œ๋ก  1. ์„œ๋ก  ๋ฐ์ดํ„ฐ ๋ถ„์„์— ๋Œ€ํ•ด ์ƒ๊ฐ์„ ํ•ด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ง€๊ณ  ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ ์“ฐ๋Š” ๊ธ€์€ ๋ฌด์กฐ๊ฑด์ ์ธ ์‚ฌ์‹ค์ด๊ธฐ๋ณด๋‹ค๋Š”, ํ‰์†Œ์— ์ œ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ƒ๊ฐ์„ ์“ฐ๋Š” ๊ธ€์ด๊ธฐ์— ๊ฐ€๋ณ๊ฒŒ ์ฝ์–ด์ฃผ์…จ์œผ๋ฉด ํ•ฉ๋‹ˆ๋‹ค. ์š”์ฆ˜ Big data, Data Scientist, Analyst ๋“ฑ์˜ ๋‹จ์–ด๋ฅผ ์ž์ฃผ ์ ‘ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ธฐ์ˆ ์˜ ๋ฐœ์ „์œผ๋กœ ์ธํ•ด ๋ฐฉ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๊ณ , ์˜ค๋žœ ์‹œ๊ฐ„ ๊ธฐ๋ก๋˜๋ฉด์„œ ๋ˆ„์ ๋˜๊ธฐ๋งŒ์„ ๋ฐ˜๋ณตํ–ˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ์ƒˆ๋กœ์šด ๊ธฐ์ˆ ์„ ๊ฐœ๋ฐœํ•˜๊ณ , ์ธ์‚ฌ์ดํŠธ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ธฐ์ˆ ์˜ ๋ฐœ์ „์€ ์ปดํ“จํ„ฐ ์žฅ๋น„๋“ค์˜ ๋ฐœ์ „์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์›๋ž˜ ๋จธ์‹ ๋Ÿฌ๋‹, ๋”ฅ๋Ÿฌ๋‹ ๋“ฑ ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„ ๊ธฐ์ˆ ์˜ ์ด๋ก ๊ณผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์˜›๋‚ ๋ถ€ํ„ฐ ์™„์„ฑ๋˜์–ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์žฅ๋น„๋“ค์ด ๋’ท๋ฐ›์นจ์„ ํ•˜์ง€ ๋ชปํ•ด ๊ตฌํ˜„์„ ๋ชปํ•˜๊ณ  ์žˆ์—ˆ์„ ๋ฟ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ธฐ์ˆ ์ด ๋ฐœ์ „๋œ ์ง€๊ธˆ, ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„๋ฐฉ๋ฒ•๋ก ๋“ค์€ ์—ฌ๋Ÿฌ ๋ถ„์•ผ์—์„œ ๋น ๋ฅธ ์†๋„๋กœ ์ ์šฉ๋˜๋ฉฐ, ๋„์ž…๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ œ๊ฐ€ ํ•˜๊ณ  ์‹ถ์€ ๋ง์€ ์ด๋ ‡๊ฒŒ ์š”์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„์ด๋ผ๊ณ  ์ƒˆ๋กญ๊ณ  ์–ด๋ ค์šด ์‹ ๊ธฐ์ˆ ์ด ์•„๋‹Œ, ๊ธฐ์กด์— ์ •๋ฆฝ๋˜์–ด ์žˆ์—ˆ๋˜ ํ†ต๊ณ„ํ•™์˜ ์—ฐ์žฅ์„ ์ผ ๋ฟ์ด๋‹ค. ๋˜ํ•œ, ์š”์ฆ˜ ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„๋ฐฉ๋ฒ•๋ก  ํ•˜๋ฉด์„œ, ์—ฌ๋Ÿฌ ๋ถ„์„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ตํžˆ๋ฉด์„œ ๊ณต๋ถ€ํ•˜์‹œ๋Š” ๋ถ„๋“ค์„ ๋งŽ์ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ „ ๊ทธ๋Ÿฐ ๊ณต๋ถ€๋ฒ•์ด ๊ทธ๋ฆฌ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŽ์ด ์•ˆ๋‹ค๊ณ  ํ•ด์„œ ํ•ด๊ฒฐ๋˜๋Š” ๋ฌธ์ œ๋“ค์€ ํ˜„์‹ค์—์„œ ๋งŽ์ด ์—†๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋ฐ์ดํ„ฐ ๋ถ„์„ ํ•™์Šต์„ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผ ๋ ๊นŒ์š”? ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์Šต๋“ํ•˜๊ธฐ ์ „์— ์„ ํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ์‚ฌํ•ญ๋“ค์€ ๋‹ค์Œ 3๊ฐ€์ง€๋กœ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ๋ชฉ์ ๊ณผ ๊ฒฝ์Ÿ ์ƒ๋Œ€ ๋จผ์ €, ๋ถ„์„์˜ ๋ชฉ์ ์€ ๋‹น์—ฐํžˆ ๋ˆ์ž…๋‹ˆ๋‹ค. ๋จน๊ณ ์‚ด๋ ค๊ณ  ๊ณต๋ถ€ํ•˜๊ณ , ์ผํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ํ™œ์šฉํ•˜์—ฌ ๋ˆ์„ ๋” ๋ฒŒ์–ด์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ์˜ˆ์ธก ๋ถ„์„์„ ์ ์šฉํ•œ๋‹ค๋“ ์ง€, ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ฆฌํ•˜๊ณ , ๋ถ„์„ํ•˜์—ฌ ์‹ค๋ฌด ํ”„๋กœ์„ธ์Šค์—์„œ์˜ ๊ตฌ๋ฉ์„ ๋ฐœ๊ฒฌํ•˜๊ณ , ๋น„์šฉ์„ ์ค„์ธ๋‹ค ๋“ฑ์˜ ํšจ๊ณผ๋ฅผ ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ง์ด์•ผ ์‰ฝ๊ณ  ๋ฉ‹์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ด๊ฑธ ์‹ค์ œ๋กœ ์„ฑ๊ณต์‹œํ‚ค๊ธฐ์—๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๋งŒ ์ ์šฉ์‹œํ‚จ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒŒ ์•„๋‹™๋‹ˆ๋‹ค. ๋ˆ์ด ๋˜๋Š” ๋ถ„์„์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ˜„์žฅ์—์„œ ์ˆ˜๋…„, ์ˆ˜์‹ญ ๋…„๊ฐ„ ์Œ“์ธ ๋…ธํ•˜์šฐ์™€ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ด๊ธฐ๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ธ๊ฐ„๋“ค์ด ์ด์ œ๊นŒ์ง€ ์Œ“์•„ ์˜ฌ๋ ค ์ตœ์ ํ™”์‹œ์ผœ๋‘” ์‹ค๋ฌด ํ”„๋กœ์„ธ์Šค๋“ค์€ ์–ธ๋œป ๋ณด๊ธฐ์—๋Š” ๋น„ํšจ์œจ์ฒ˜๋Ÿผ ๋ณด์—ฌ๋„, ์ƒ๊ฐ๋ณด๋‹ค ๊ฒฐ๊ณผ๋Š” ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. ์ฆ‰ ๋ฐ์ดํ„ฐ ๋ถ„์„์œผ๋กœ ์ด๊ฒจ๋‚ด๊ธฐ์—๋Š” ๋งŒ๋งŒํ•œ ์ƒ๋Œ€๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ์ฆ‰, ํ•œ๋งˆ๋””๋กœ ์ •๋ฆฌํ•˜์ž๋ฉด, ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ๊ฒฝ์Ÿ ์ƒ๋Œ€๋Š” ์ด์ œ๊นŒ์ง€ ์‹ค๋ฌด์— ์ ์šฉ๋˜์–ด ์žˆ๋Š” ๋…ธํ•˜์šฐ์™€ ํ”„๋กœ์„ธ์Šค์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ๊ทน๋ณตํ•˜์ง€ ๋ชปํ•˜๋ฉด, ๋ฐ์ดํ„ฐ ๋ถ„์„์€ ์•ˆ ํ•˜๋‹ˆ๋งŒ์„ ๋ชปํ•œ ์ƒํ™ฉ์ด ๋˜๊ธฐ ๋งˆ๋ จ์ž…๋‹ˆ๋‹ค. ๋„๋ฉ”์ธ์— ๋Œ€ํ•œ ์ถฉ๋ถ„ํ•œ ์ดํ•ด ๊ทธ๋ ‡๊ธฐ์— ์‹ค๋ฌด(ํ˜„์žฅ, ๋„๋ฉ”์ธ)์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ์™„๋ฒฝํ•œ ์ƒํƒœ์—์„œ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์ ์šฉ์‹œ์ผœ์•ผ ์˜๋ฏธ๊ฐ€ ์žˆ๋Š” ๋ถ„์„์ด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด๋Ÿฐ ์ƒํ™ฉ์ด๋ผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์š”์ฆ˜ ๋ฐ์ดํ„ฐ ๋ถ„์„์— ๋Œ€ํ•œ ๊ด€์‹ฌ์ด ๋†’์€ ์ƒํ™ฉ์—, ๋งŽ์€ ๋Œ€ํ•™๊ต์—์„œ ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„ํ•™ ์ปค๋ฆฌํ˜๋Ÿผ์„ ๊ตฌ์„ฑํ•˜์—ฌ, ๋Œ€ํ•™์› ์ฝ”์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์—ฐํžˆ ์ด๋Ÿฐ ๋Œ€ํ•™์› ํ•™๊ณผ์— ๊ด€์‹ฌ์ด ๊ฐ€๋Š” ์‚ฌ๋žŒ๋“ค๋„ ๋งŽ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‹ ์ƒํ•™๊ณผ์ด๊ธฐ๋„ ํ•˜๊ณ , ์ •๋ณด๋„ ๋ณ„๋กœ ์—†์–ด ์˜ํ˜น์€ ์ƒ๊ธฐ๊ณ , ์ง„ํ•™ ์ „์— ๊ณ ๋ฏผ์„ ๊ฐ€์ง€๋Š” ์‚ฌ๋žŒ์„ ๋งŽ์ด ๋ดค์Šต๋‹ˆ๋‹ค. ์ „ ์ด๋Ÿฐ ๋ถ„๋“ค๊ป˜ ์ด๋ ‡๊ฒŒ ๋งํ•ด๋“œ๋ฆฌ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๊ธˆ์œต๊ถŒ์—์„œ ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„์„ ํ•˜๊ณ  ์‹ถ๋‹ค. ๊ทธ๋Ÿผ ๋น…๋ฐ์ดํ„ฐ ์œตํ•ฉ ๋Œ€ํ•™์›์ด ์•„๋‹Œ, ๊ฒฝ์˜, ๊ฒฝ์ œ, ์‚ฐ์—…๊ณตํ•™๊ณผ์—์„œ ๊ธˆ์œต์„ ์—ฐ๊ตฌํ•˜์‹œ๋Š” ๊ต์ˆ˜๋‹˜ํ•œํ…Œ ๊ฐ€์„œ, ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„์„ ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋„๋ฉ”์ธ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ•œ ์ƒํ™ฉ์—์„œ์˜ ๋ถ„์„์€ ๋ง ๊ทธ๋Œ€๋กœ ์‚ฝ์งˆํ•˜๋Š” ๊ฑฐ๋ž‘ ๋‹ค๋ฅผ ๊ฒŒ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„, ํŒŒ์ด ์ฐจํŠธ ๋“ฑ ๊ทธ๋ ค์ฃผ๋ฉด์„œ ํ‰๊ท ๊ฐ’์ด ๋ช‡์ž…๋‹ˆ๋‹ค.๋ผ๊ณ  ๋ณด๊ณ ํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ๊ณผ๊ฐ€ ์ข‹์„ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๊ผญ ์ผํ•˜๊ณ  ์‹ถ์€ ๋ถ„์•ผ์—์„œ์˜ ๋„๋ฉ”์ธ์„ ์ถฉ๋ถ„ํžˆ ์ตํžˆ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ํ†ต๊ณ„ํ•™์— ๋Œ€ํ•œ ์ถฉ๋ถ„ํ•œ ์ดํ•ด ๋จธ์‹ ๋Ÿฌ๋‹์ด๋‹ˆ ๋”ฅ๋Ÿฌ๋‹์ด๋‹ˆ ๋ญ๋‹ˆ ํ•ด๋„, ์–ด์ฐจํ”ผ ๋‹ค ํ™•๋ฅ ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ํ†ต๊ณ„๋ถ„์„์˜ ์—ฐ์žฅ์„ ์ž…๋‹ˆ๋‹ค. ํ†ต๊ณ„ํ•™์— ๋Œ€ํ•œ ์ถฉ๋ถ„ํ•œ ์ดํ•ด ์—†์ด๋Š” ๋จธ์‹ ๋Ÿฌ๋‹ ๋ฐ ๋”ฅ๋Ÿฌ๋‹์„ ๊ณต๋ถ€ํ•œ๋‹ค ํ•ด๋„ ๊ทธ๊ฑด 'ํ‰๋‚ด' ๋‚ด๋Š” ๊ฑฐ์ง€, ์•ˆ๋‹ค๊ณ  ํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด๋Ÿฐ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ๋”ฅ๋Ÿฌ๋‹์„ ๊ณต๋ถ€ํ•˜์‹œ๋Š” ๋ถ„๋“ค์ด๋ผ๋ฉด, ์‹œ๊ทธ๋ชจ์ด๋“œ(sigmoid)๋ผ๋Š” ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ ๋“ฃ๊ฑฐ๋‚˜ ์•Œ๊ณ  ๊ณ„์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ํ™œ์„ฑํ™” ํ•จ์ˆ˜๊ฐ€ ํ†ต๊ณ„ํ•™์—์„œ์˜ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„, ์ฆ‰ Odds Ratio์—์„œ ๊ธฐ๋ฐ˜ํ•œ ๊ฒƒ์€ ๋ชจ๋ฅด๋Š” ๋ถ„๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฑด ์‹œ๊ทธ๋ชจ์ด๋“œ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์–ด์กŒ๋Š”์ง€๋Š” ๊ด€์‹ฌ๋„ ์—†๊ณ , ๊ทธ๋ƒฅ ๋‚จ๋“ค์ด ๊ทธ๋ ‡๋‹ค ํ•˜๋‹ˆ๊น ๊ทธ๋ ‡๊ตฌ๋‚˜ ํ•œ ๊ฒƒ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„ํ•™์„ ๊ณต๋ถ€ํ•˜๊ธฐ ์ „์— ํ†ต๊ณ„ํ•™์„ ๊นŠ์ด ์žˆ๊ฒŒ ๊ณต๋ถ€ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์–ด์ฐจํ”ผ ํ†ต๊ณ„ํ•™์— ๋Œ€ํ•œ ์ถฉ๋ถ„ํ•œ ์ดํ•ด ์—†์ด๋Š” ๋จธ์‹ ๋Ÿฌ๋‹, ๋”ฅ๋Ÿฌ๋‹์„ ์•„๋ฌด๋ฆฌ ๊ณต๋ถ€ํ•ด๋„ ์ž˜ ์ดํ•ด๊ฐ€ ๋˜์ง€๋Š” ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐ์ดํ„ฐ ๋ถ„์„์ด ๊ฐ€๋œฉ์ด๋‚˜ ์–ด๋ ค์šด๋ฐ, ํšจ๊ณผ๋ฅผ ๋ณด๊ธฐ๋„ ํž˜๋“ค์–ด? ๊ทธ๋Ÿผ ๊ณต๋ถ€๋ฅผ ํ•ด? ๋ง์•„? ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชฐ๋ผ์„œ ๋ชปํ•˜๋Š” ๊ฒƒ์ด๋ž‘, ์•Œ๋ฉด์„œ ์•ˆ ํ•˜๋Š” ๊ฒƒ์ด๋ž‘์€ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋ฌด์—‡๋ณด๋‹ค ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ฐฉ๋ฒ•๋ก ๋“ค์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ, ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋Š” ์‹œ๊ฐ์„ ์ตํžˆ๋Š” ๊ฒƒ์ด ๋” ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋” "์„ธ์ƒ ์‚ฌ๋Š”๋ฐ ๋ˆ ๊ณ„์‚ฐ๋งŒ ์ž˜ํ•˜๋ฉด ๋˜์ง€, ๊ทธ ์–ด๋ ค์šด ์ˆ˜ํ•™์€ ์™œ ๊ณต๋ถ€์‹œํ‚ค๋Š” ๊ฑฐ์•ผ. "๋ผ๊ณ  ์กฐ๊ธˆ์€ ๋…ผ๋ž€์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ง์„ ํ•˜์‹œ๋Š” ๋ถ„๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„๋“ค๋„ ์•„์‹œ๋‹ค์‹œํ”ผ, ์ˆ˜ํ•™์„ ๊ณต๋ถ€ํ•˜๋Š” ์ด์œ ๋Š” ๋ˆ ๊ณ„์‚ฐ์ด ์•„๋‹ˆ๋ผ, ๋…ผ๋ฆฌ์— ๋”ฐ๋ผ ์ƒ๊ฐํ•˜๋Š” ํž˜์„ ํ‚ค์šฐ๋Š” ๊ฒƒ์— ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถ„์„๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ์–ด๋Š ๋ถ„์•ผ๋ฅผ ๋ง‰๋ก ํ•˜๊ณ  ํ•ญ์ƒ ๋งˆ์ฃผํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ˆˆ์— ๋ณด์ด๋Š” ๋Œ€๋กœ ๊ฒฐ๋ก ๋‚ด๊ณ  ๊ทธ ์ˆ˜์ค€์—์„œ ๋ฉˆ์ถœ ๊ฒƒ์ธ์ง€, ์•„๋‹˜ ๋” ๋‚˜์•„๊ฐ€ ๋ฐ์ดํ„ฐ์—์„œ ์ƒˆ๋กœ์šด ์ธ์‚ฌ์ดํŠธ๋ฅผ ๋„์ถœํ•ด๋‚ผ ๊ฒƒ์ธ์ง€. ๊ทธ๊ฒƒ์€ ๋ฐ์ดํ„ฐ ๋ถ„์„๋ฐฉ๋ฒ•๋ก ์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ํ„ฐ๋“ํ•œ ์‚ฌ๊ณ ๋ ฅ๊ณผ ์„ผ์Šค๊ฐ€ ๋’ท๋ฐ›์นจ๋˜์–ด์•ผ ๊ฐ€๋Šฅํ•œ ์ผ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๊ผญ ๊ณต๋ถ€ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ, ์ด ์ฑ…์—์„œ๋Š” ์œ„ 3๊ฐ€์ง€ ์‚ฌํ•ญ์— ๋Œ€ํ•œ ๋‹ต์„ ๋‹ค ์•Œ๋ ค์ฃผ์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ์„ธ์ƒ์€ ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ณ  ๋‹ค์–‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ท€๋‚ฉ๋ฒ•์œผ๋กœ ์ •๋ฆฌํ•˜๊ธฐ์—๋Š” ์–ด๋ ค์šด ๋ถ€๋ถ„์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ €๋„ ์•„์ง ๊ณต๋ถ€ํ•ด์•ผ ๋  ๊ฒƒ์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งˆ์ง€๋ง‰์— ์–ธ๊ธ‰ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ณ  ์‚ฌ๊ณ ๋ฅผ ํ•ด๋‚ด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ๋Š” ์ œ๋Œ€๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๋ถ€๋ถ„์„ ์ง‘์ค‘ ์‚ผ์•„ ์ฑ…์„ ์ž‘์„ฑํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A2. R์— ๋Œ€ํ•œ ์†Œ๊ฐœ 2. R์— ๋Œ€ํ•œ ์†Œ๊ฐœ R์„ ์™œ ๊ณต๋ถ€ํ•ด์•ผ ํ•˜๋Š”๊ฐ€? R์€ ์ฒ ์ €ํžˆ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•œ ๋ฌด๋ฃŒ ๋ถ„์„ Tool์ž…๋‹ˆ๋‹ค. ๋งŽ์€ ํšŒ์‚ฌ์—์„œ๋Š” ์—‘์…€์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ •๋ฆฌ๋ฅผ ํ•˜๊ณ  ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์—‘์…€์€ ๋งค์šฐ ๋›ฐ์–ด๋‚œ ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ๋‚ดํฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์€ '๊นŠ์ด'๋Š” ๋–จ์–ด์ง„๋‹ค๋Š” ์†Œ๋ฆฌ์™€ ๋งฅ๋ฝ์ด ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ผ๋ฐ˜์ ์ธ ์—…๋ฌด์—์„œ๋Š” R์ด ํฌ๊ฒŒ ํ•„์š”ํ•œ ์ƒํ™ฉ์€ ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, 'ํ†ต๊ณ„์ '์œผ๋กœ ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•ด๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด, ํ™•์‹คํ•œ ๊ฒƒ์€ ์—‘์…€๋กœ๋Š” ํ•œ๊ณ„์ ์— ๋ถ€๋”ชํžˆ๊ธธ ๋งˆ๋ จ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ R์ด๋˜ Python์ด๋˜ ์–ด๋–ค ์–ธ์–ด๋ฅผ ํ†ตํ•ด ํ†ต๊ณ„์ ์ธ ๊ธฐ๋ฒ•์„ ์ ์šฉ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—‘์…€์˜ ๋‹จ์  ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ฒ˜๋ฆฌ๊ฐ€ ํž˜๋“ญ๋‹ˆ๋‹ค. ์—‘์…€์˜ ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ์—‘์…€์„ ํ‚ค๋Š” ๊ฒƒ๋งŒ ํ•ด๋„ ๋งค์šฐ ํฐ ์‹œ๊ฐ„์ด ์†Œ๋น„๋˜๋ฉฐ, ์ž˜๋ชป ๊ฑด๋“œ๋ฆฌ๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ๋จนํ†ต์ด ๋˜์–ด๋ฒ„๋ฆฌ๋Š” ํ˜„์ƒ๊นŒ์ง€ ์‹ฌ์‹ฌ์น˜ ์•Š๊ฒŒ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์—‘์…€๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๋ถ„์„์€ ์ œํ•œ์ ์ด๋‹ค. ๊ฐ„๋‹จํ•œ ๋นˆ๋„ ๋ถ„์„์„ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์ƒ๊ด€์ด ์—†์ง€๋งŒ, ์กฐ๊ธˆ ๋” ๊ณ ๊ธ‰์ ์ธ ๋ถ„์„๊ณผ ์‹œ๊ฐํ™”๋ฅผ ํ•˜๊ธฐ์— ์—‘์…€์€ ๋งค์šฐ ํ•œ์ •์ ์ธ ๊ธฐ๋Šฅ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. R์˜ ์žฅ์  ์—‘์…€์— ๋น„๊ตํ•˜์—ฌ ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๊ฐ€ ๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋Š” ์‚ฌ์‹ค SQL์„ ์ด์šฉํ•ด์„œ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ํ•˜๋ ค๋ฉด R, Python, Spss, SAS ๋“ฑ์˜ ๋ถ„์„ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. SQL์€ DB ์ž‘์—… ์šฉ์ด์ง€, ๋ฐ์ดํ„ฐ ๋ถ„์„์šฉ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋ช…๋ น์–ด ์ฒด๊ณ„๊ฐ€ ๋งค์šฐ ์ž์œ ๋กญ๋‹ค R์˜ ์žฅ์ ์ด์ž ๋‹จ์ ์ž…๋‹ˆ๋‹ค. ๋ฌธ๋ฒ•์ด ๋งค์šฐ ์ž์œ ๋กœ์šฐ๋ฉฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ํ‘œ์ค€ ๊ตญ์–ด์‚ฌ์ „์„ ๋”ฐ๋ฅธ๋‹ค๋ฉด, R์€ ๋ถ€์‚ฐ ์‚ฌํˆฌ๋ฆฌ๋„ ์•Œ์•„๋“ฃ๋Š” ์ˆ˜์ค€์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ํŽธํ•ฉ๋‹ˆ๋‹ค. ์˜คํ”ˆ ์†Œ์Šค(๋ฌด๋ฃŒ) ๋ฐ ๊ฐ€๋ฒผ์šด ํ”„๋กœ๊ทธ๋žจ ๋Œ€๋ถ€๋ถ„์˜ ํ•จ์ˆ˜๋Š” ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋ฅผ ํ†ตํ•ด ๋Œ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ํƒ€ ํ”„๋กœ๊ทธ๋žจ์— ๋น„ํ•ด ๋งค์šฐ ๊ฐ€๋ฒผ์šฐ๋ฉฐ, ํ”„๋กœ๊ทธ๋žจ ์šด์˜ ์ƒ์—์„œ์˜ ์˜ค๋ฅ˜(ํ™˜๊ฒฝ ์„ค์ •, ์„ค์น˜ ์ƒ์˜ ์˜ค๋ฅ˜ ๋“ฑ) ๋ฐœ์ƒ ํ™•๋ฅ ์ด ๊ทนํžˆ ๋‚ฎ์Šต๋‹ˆ๋‹ค. Python ๋ฒ„์ „์ด ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค, ์œˆ๋„ ๋ฒ„์ „์ด ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ, ํ•„์š”ํ•œ Module๋“ค์˜ ์ •์ƒ์  ๊ธฐ๋Šฅ์ด ๊ฐ€๋Šฅํ•œ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•œ๊ฐ€๋Š” ํ•ญ์ƒ ๊ทน์‹ฌํ•œ ์ŠคํŠธ๋ ˆ์Šค๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. R์€ ๊ทธ๋Ÿฐ ๊ฒŒ ์—†์Šต๋‹ˆ๋‹ค. R์˜ ๋‹จ์  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ์žก์•„๋จน์Šต๋‹ˆ๋‹ค. R์„ ๋Œ๋ฆด ๋•Œ ์ปดํ“จํ„ฐ์˜ Ram ์šฉ๋Ÿ‰์€ ๊ฝค๋‚˜ ์ค‘์š”ํ•œ ์š”์†Œ์ž…๋‹ˆ๋‹ค. ์—ฐ์‚ฐ์†๋„๊ฐ€ ๋น ๋ฅธ ํŽธ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์ž๋™๋ฌธ์„ ์ ์šฉํ•  ๋•Œ, R์ด ํƒ€ ํ”„๋กœ๊ทธ๋žจ์— ๋น„ํ•ด ๋น ๋ฅด๊ฒŒ ๊ณ„์‚ฐํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ์—ด์‹ฌํžˆ ํŒจํ‚ค์ง€ ์„ค๋ช…์„œ๋ฅผ ์ฝ์–ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ๊ณต๋ถ€ํ•ด์•ผ ๋ ๊นŒ? ๋ฐ์ดํ„ฐ ๋ถ„์„ ๊ด€๋ จ ์ง๋ฌด๋ฅผ ์›ํ•˜์‹œ๋Š” ๊ฒฝ์šฐ, SQL์€ ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค. ๋ถ„์„์„ ํ•˜๋ ค๋ฉด ์ผ๋‹จ ๋ฐ์ดํ„ฐ๋ฅผ ์›ํ•˜๋Š” ์กฐ๊ฑด์œผ๋กœ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŽ์€ ๋ฐ์ดํ„ฐ๋Š” SQL ์„œ๋ฒ„์— ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ํ•˜๋ ค๋ฉด ์œ ๋ฃŒ ํˆด์„ ์ œ์™ธํ•˜๊ณ ์„œ๋Š” R, Python ์ค‘ 1๊ฐœ๋ฅผ ํƒํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค ํ•˜๋ฉด ์ข‹์ง€๋งŒ, ์ด ๊ธ€์„ ๋ณด์‹œ๋Š” ๋ถ„๋“ค์€ ์•„์ง 2๊ฐœ๋ฅผ ๋ชจ๋‘ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๋‹จ๊ณ„๊ฐ€ ์•„๋‹ ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ž์‹ ์ด ํ•˜๊ณ  ์‹ถ์–ด ํ•˜๋Š” ๊ฒƒ์— ๋” ๋ถ€ํ•ฉํ•˜๋Š” ์–ธ์–ด๋ถ€ํ„ฐ ํ•˜๋Š” ๊ฒƒ์ด ๋งž์Šต๋‹ˆ๋‹ค. R : ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง ๋ฐ ํ†ต๊ณ„ ๋ชจ๋ธ๋ง, ๊ธฐ๊ณ„ํ•™์Šต & ์‹œ๊ฐํ™” Python : ๊ธฐ๊ณ„ํ•™์Šต, ๋”ฅ๋Ÿฌ๋‹, ๊ฐ•ํ™” ํ•™์Šต ๋“ฑ ๊ฐœ๋ฐœ ๊ฐ€๋ณ๊ฒŒ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์‹ค๋ฌด์— ์ ์šฉ์‹œํ‚ค๊ธฐ์—๋Š” R์ด ๋” ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ๋ถ„์„์„ ํ•œ๋‹ค๊ณ  ์ณค์„ ๋•Œ, ํ†ต๊ณ„๋ถ„์„์— ํ•œํ•ด์„œ๋Š” R์ด Python๋ณด๋‹ค ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์งค ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€์‹ , ๊ฐœ๋ฐœ ์ชฝ์œผ๋กœ ๋„˜์–ด๊ฐ€๋ฉด R๋ณด๋‹ค๋Š” Python์ด ๋” ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. (์‚ฌ์‹ค Python์„ ํ†ตํ•œ ๊ฐœ๋ฐœ์€ ์œˆ๋„ ์šด์˜์ฒด์ œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ๊ฐ€ ์—๋Ÿฌ ์‚ฌํ•ญ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌ๋ˆ…์Šค ์šด์˜์ฒด์ œ์˜ ๊ฐ€์ƒ ๋จธ์‹ ์„ ์„ค์น˜ํ•ด์•ผ ๋˜๋Š” ๋“ฑ ๋จธ๋ฆฌ๊ฐ€ ๋งŽ์ด ์•„ํ”•๋‹ˆ๋‹ค. ์ฆ‰, ์ž…๋ฌธ์ž ์ž…์žฅ์—์„œ๋Š” ์ œ๋Œ€๋กœ ๋ฌด์—‡์„ ํ•ด๋ณด๊ธฐ ์ „์— ๋จธ๋ฆฌ๊ฐ€ ์—„์ฒญ ์•„ํ”•๋‹ˆ๋‹ค.) ์ •๋ฆฌํ•˜์ž๋ฉด, ํ†ต๊ณ„๋ถ„์„์„ ์ž…๋ฌธํ•˜๋Š” ์ž…์žฅ์—์„œ๋Š” Python๋ณด๋‹ค๋Š” R์„ ์ถ”์ฒœํ•˜๋Š” ์ž…์žฅ์ž…๋‹ˆ๋‹ค. ํ†ต๊ณ„ํ•™๋„ ์–ด๋ ค์šด๋ฐ, ์ฝ”๋“œ ๋ฐ ์™ธ๋ถ€ ์š”๊ฑด๋„ ์–ด๋ ค์šฐ๋ฉด ๊ณต๋ถ€ํ•˜๋Š” ๋ฐ์— ์žˆ์–ด, ๋‚œ๊ด€์ด ๋งค์šฐ ๋งŽ์Šต๋‹ˆ๋‹ค. ํฅ๋ฏธ๋ฅผ ๋ถ™์ผ ๋•Œ๊นŒ์ง€๋Š” R์„ ํ™œ์šฉํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ ๋ถ„์„์— ์ต์ˆ™ํ•ด์ง€๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. A3. ์ผ์ƒ์ƒํ™œ ์†์—์„œ ์ถ•์ฒ™๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ทธ๋ฆฌ๊ณ  ํ†ต๊ณ„ํ•™ 3. ์ผ์ƒ์ƒํ™œ ์†์—์„œ ์ถ•์ฒ™๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ทธ๋ฆฌ๊ณ  ํ†ต๊ณ„ํ•™ ๋ณธ๊ฒฉ์ ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ, ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด ํ†ต๊ณ„ํ•™์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” ํ™•๋ฅ ์— ๋Œ€ํ•ด ๋‹ค๋ฃจ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ‘์ž๊ธฐ ๋“ฑ์žฅํ•œ ์ˆ˜์‹์— ์–ด๋ ต๊ณ  ๋‹นํ™ฉ์Šค๋Ÿฌ์šธ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ด ์ •๋„๋Š” ์ˆ™์ง€๋ฅผ ํ•˜์ง€๊ณ  ๊ณ„์…”์•ผ, ๋‹ค์Œ์— ํ†ต๊ณ„ํ•™ ์ด๋ก ์ด ๋“ฑ์žฅํ•˜์˜€์„ ๋•Œ ์–ด๋ ต์ง€ ์•Š๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ตœ๊ทผ ํšŒ์‚ฌ์— ์ง€๊ฐํ•˜๋Š” ์ผ์ด ์žฆ์•„์ง„ "A"๋Š” ์‚ฌ์ˆ˜ํ•œํ…Œ์„œ ์ผ์ฐ ๋‹ค๋‹ˆ๋ผ๋Š” ์ž”์†Œ๋ฆฌ๋ฅผ ๋ฐ›์•˜๋‹ค. ๊ทธ๋‚  ํ‡ด๊ทผ ํ›„ "A"๋Š” ๋‹ฌ๋ ฅ์„ ํ‚ค๊ณ  ๊ธฐ์–ต์„ ๋”๋“ฌ์–ด๊ฐ€๋ฉฐ, ์ถœ๊ทผ ๋‚ด์—ญ์„ ๊ธฐ๋กํ•ด ๋ณด์•˜๋‹ค. ๊ธฐ๋ก์ด ๋๋‚œ "A"๋Š” ๋ณธ์ธ๋„ ๋†€๋ž„ ๋งŒํผ์˜ ๋นˆ๋„๋กœ ์ง€๊ฐ์„ ์ž์ฃผ ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์ ์„ ๊นจ๋‹ฌ์•˜๋‹ค. ์ด๋ž˜์„œ๋Š” ์•ˆ๋˜๊ฒ ๋‹ค๊ณ  ๋‹ค์ง์„ ํ•œ "A"๋Š” ๋งค์ผ๋งค์ผ ์ž์‹ ์˜ ์ถœ๊ทผ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•˜๊ธฐ ์‹œ์ž‘ํ•˜์˜€๋‹ค. ๊ฐ€์žฅ ๋จผ์ € ์‹œ์ž‘ํ•œ ์ž‘์—…์€ ๊ธฐ๋กํ•  ์‚ฌํ•ญ(์š”์ธ)๋“ค์„ ์ •ํ•˜๋Š” ์ž‘์—…์ด์—ˆ๋‹ค. "A"๋Š” ์ž์‹ ์˜ ๊ฒฝํ—˜์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ๋‹ค์Œ์˜ ์‚ฌํ•ญ(์š”์ธ)์„ ํ•œ ๋‹ฌ ๋™์•ˆ ๊พธ์ค€ํžˆ ๊ธฐ๋กํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. (์—ฌ๊ธฐ์„œ, ์–ด์ œ์˜ ์ถœ๊ทผ ์ƒํ™ฉ์€ ์˜ค๋Š˜์˜ ์ถœ๊ทผ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์œผ๋ฉฐ, ์˜ค๋Š˜์˜ ์ถœ๊ทผ์€ ๋‚ด์ผ์˜ ์ถœ๊ทผ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š” ๋…๋ฆฝ์„ฑ์„ ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ๊ฐ ๊ตํ†ต ํŽธ์˜ ์„ ํƒ, ๋‚ ์”จ, ์•Œ๋žŒ ๊ธฐ์ƒ์‹œ๊ฐ„ ๋“ฑ์€ ๋ชจ๋‘ ๋…๋ฆฝ์ ์œผ๋กœ ์ ์šฉ๋จ์„ ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.) ๋ฐ์ดํ„ฐ(์ž๋ฃŒ)์˜ ์ˆ˜์ง‘ ๊ณผ์ • ๋‚ ์งœ : ๋…„/์›”/์ผ ๊ธฐ์ƒ์‹œ๊ฐ„ : ์•Œ๋žŒ ์‹œ๊ฐ„ ์ค€์ˆ˜ ์—ฌ๋ถ€ ๊ตํ†ตํŽธ : ์ง€ํ•˜์ฒ /๋ฒ„์Šค/ํƒ์‹œ ๋‚ ์”จ : ๋ง‘์Œ/๋น„ ๋ˆˆ ์ง€๊ฐ ์—ฌ๋ถ€ : O/X ํ™•๋ฅ ์˜ ๊ณ„์‚ฐ "A"๋Š” ๋งค์ผ๋งค์ผ ๋น ์ง์—†์ด ์ž์‹ ์˜ ์ถœ๊ทผ ๊ธฐ๋ก์„ ์ž…๋ ฅํ–ˆ์œผ๋ฉฐ, 1๋‹ฌ์ด ์ง€๋‚œ ๋‹ค์Œ 30์ผ๊ฐ„์˜ ์ถœ๊ทผ ๊ธฐ๋ก์„ ๋ถ„์„ํ•ด ๋ณด๊ธฐ๋กœ ๋งˆ์Œ์„ ๋จน์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด์ œ๊นŒ์ง€ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์ œ๋Œ€๋กœ ํ•ด๋ณธ ์ ์ด ์—†๋Š” "A"๋Š” ๋ฐ์ดํ„ฐ๋Š” ์žˆ์œผ๋‚˜, ๋ฌด์—‡๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์•ผ ํ• ์ง€๋„ ๋ง‰๋ง‰ํ•ด์กŒ๋‹ค. ์—‘์…€๋กœ ์ง€๊ฐ์„ ๋ช‡ ๋ฒˆ ํ–ˆ๋Š”์ง€ ๊ตฌํ•ด๋ณด๋Š” ๊ฒƒ์ด ์ „๋ถ€์˜€๋‹ค. ๊ฒฐ๊ตญ ์ฃผ๋ณ€์— ์˜ํ•™ํ†ต๊ณ„๋ถ„์„ ์—ฐ๊ตฌ์— ๋งค์ง„ํ•˜๊ณ  ์žˆ๋Š” "H"์—๊ฒŒ ๋„์›€์„ ์š”์ฒญํ•˜์˜€๋‹ค. "H"๋Š” "A"์—๊ฒŒ ๋จผ์ € ํ™•๋ฅ ๋ถ€ํ„ฐ ๊ตฌํ•ด๋ณด์ž๊ณ  ํ•˜์˜€๋‹ค. ํ™•๋ฅ ์€ ์ˆ˜์ง‘๋œ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๋Œ€๋น„ ๊ด€์‹ฌ์‚ฌํ•ญ(์ง€๊ฐ ์—ฌ๋ถ€)๊ฐ€ ์–ผ๋งˆํผ ๊ธฐ๋ก๋˜์—ˆ๋Š” ์ง€๋กœ ๊ณ„์‚ฐ์„ ํ•ฉ๋‹ˆ๋‹ค. (์ง€๊ฐ ์—ฌ๋ถ€(Event)๋ฅผ ํŽธํ•˜๊ฒŒ๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค.) ์ง€๊ฐ ํšŸ์ˆ˜ ๋ฐ์ดํ„ฐ์ˆ˜์ง‘์ผ ๋Š”๋ฅผ์˜๋ฏธ์€๊ฐฏ์ˆ˜๋ฅผ์˜๋ฏธ [ = ] ์ง€ ํšŸ ๋ฐ ํ„ฐ ์ˆ˜ ์ผ n ( = ) ( = o E 1 ) P [ = ] P [ = ] 1 P ์ด๋Ÿฌํ•œ ํ™•๋ฅ ๋“ค์„ ์šฐ๋ฆฌ๋Š” ์‚ฌ์ „ ํ™•๋ฅ ์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ˆ˜์ง‘๋œ ๋ฐ์ดํ‹ฐ์—์„œ ๊ณ„์‚ฐ์„ ์ง„ํ–‰ํ•ด ๋ณธ ๊ฒฐ๊ณผ, 23 ์œผ๋กœ, ํ‰์ƒ์‹œ์— ์ง€๊ฐ์„ ํ•  ํ™•๋ฅ ์€ 34.7 ์ •๋„์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ด ์ •๋„์˜ ์ •๋ณด๋Š” "A"๋„ ์—‘์„ธ๋กœ ์ถฉ๋ถ„ํžˆ ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ "A"๋Š” ์ด๋Ÿฐ ๊ฐ„๋‹จํ•œ ๊ฐ’ ๋ง๊ณ  ์ข€ ๋ญ”๊ฐ€ ๋” ์•Œ์•„๋‚ผ ์ˆ˜ ์—†์„๊นŒ๋ผ๋Š” ์˜๋ฌธ์„ ๊ฐ€์ง€๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. "H"๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ, ์ง€๊ฐํ•  ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•ด ๋ณด์ž๊ณ  ์ œ์•ˆํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์„ ํƒํ•œ ๊ตํ†ตํŽธ์— ๋”ฐ๋ฅธ ์ง€๊ฐ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ ํ™•๋ฅ  ๊ณ„์‚ฐ์„ '์กฐ๊ฑด์ด ์ฃผ์–ด์ง„ ์ƒํ™ฉ์—์„œ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ํ™•๋ฅ '์ด๋ผ๊ณ  ํ•ด์„œ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. [ = | = u ] P [ = โˆฉ = u ] [ = u ] [ = , = u ] ๋Š” ๋ฒ„์Šค๋ฅผ ํƒ”์„ ๋•Œ ์ง€๊ฐ์„ ํ•œ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ํ™•๋ฅ ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ™•๋ฅ ์„ ๊ฒฐํ•ฉ ํ™•๋ฅ ์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค. [ = โˆฉ = u ] n ( = โˆฉ = u) ( = โˆช = ) 3 23 ์—ฌ๊ธฐ์„œ ๊ตฌํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋ฒ„์Šค๋ฅผ ์„ ํƒํ•  ํ™•๋ฅ  ( [ = u ] ) ๋ฅผ ๋‚˜๋ˆ„์–ด ์ฃผ๋ฉด, ํŠน์ • ์กฐ๊ฑด ํ•˜์—์„œ ์‚ฌ๊ฑด์ด ๋ฐœ์ƒํ•˜๋Š” ํ™•๋ฅ ์„ ๊ตฌํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‘ ์‚ฌ๊ฑด(์ง€๊ฐ ์—ฌ๋ถ€, ๊ตํ†ตํŽธ)์ด ๋…๋ฆฝ์ด๋ผ๋ฉด ๊ฒฐํ•ฉ ํ™•๋ฅ ์„ ๊ตฌํ•˜๋Š” ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. [ โˆฉ ] P [ ] [ ] ํ•˜์ง€๋งŒ ๋‘ ์‚ฌ๊ฑด์ด ๋…๋ฆฝ์ด ์•„๋‹ ๋•Œ๋Š”, ๊ฒฐํ•ฉ ํ™•๋ฅ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. [ โˆฉ ] P [ | ] [ ] P [ | ] [ ] [ = | = u ] P [ = โˆฉ = u ] [ = u ] 3 23 / 23 3 ์ด๊ฒฝ์šฐ, ๊ตํ†ตํŽธ์—์„œ ๋ฒ„์Šค๋ฅผ ์„ ํƒ(๊ด€์ธก) ํ•œ ํ›„, ์ง€๊ฐ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜์˜€๋‹ค๋Š” ์˜๋ฏธ๋กœ ์‚ฌํ›„ ํ™•๋ฅ ์ด๋ผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ H๋Š” ์ง€๊ฐ์„ ํ–ˆ๋Š”๋ฐ ๋ฒ„์Šค๋ฅผ ํƒ”์„ ๊ฐ€๋Šฅ์„ฑ์— ๋Œ€ํ•ด ๊ณ„์‚ฐ์„ ํ•ด๋ณด์ž๊ณ  ์ œ์•ˆ์„ ํ•œ๋‹ค. [ = u | = ] P [ = u โˆฉ = ] [ = ] = [ = | = u P [ = $$ \frac{3}{7}\frac{3}{23}/ \frac{8}{23} = \frac{0.06}{0.34}=0.17 $$ ์ด๋ ‡๊ฒŒ ์‚ฌ๊ฑด์ด ๋ฐœ์ƒํ•˜์˜€์„ ๋•Œ, ๊ฒฝ์šฐ์— ๋”ฐ๋ฅธ ๊ฐ€๋Šฅ์„ฑ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด๋ฉด ์ด๋ฅผ ๊ฐ€๋Šฅ๋„(์šฐ๋„)*๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€ ๋‹ค๋ฃฌ ํ™•๋ฅ ๋“ค์˜ ์ข…๋ฅ˜๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ ํ‘œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฒฝ์šฐ ๋ช…์นญ ๊ณ„์‚ฐ์‹ ์ง€๊ฐํ•  ํ™•๋ฅ  ์‚ฌ์ „ ํ™•๋ฅ  [ = ] ๋ฒ„์Šค๋ฅผ ํƒ€๊ณ  ์ง€๊ฐํ•  ํ™•๋ฅ  ๊ฒฐํ•ฉ ํ™•๋ฅ  [ = โˆฉ = u ] ๋ฒ„์Šค๋ฅผ ํƒ”์„ ๋•Œ ์ง€๊ฐํ•  ํ™•๋ฅ  ์‚ฌํ›„ ํ™•๋ฅ  [ = โˆฉ = u ] [ = u ] ์ง€๊ฐ์„ ํ–ˆ์„ ๋•Œ ๋ฒ„์Šค๋ฅผ ํƒ”์„ ๊ฐ€๋Šฅ์„ฑ ๊ฐ€๋Šฅ๋„ [ = U โˆฉ = ] [ = ] ํ™•๋ฅ ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ํ†ต๊ณ„ํ•™์€ ์ด๋ ‡๊ฒŒ ์ผ์ƒ์ƒํ™œ ์†์— ๋…น์•„์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ์ ์„ ํ•ญ์ƒ ์œ ๋…ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ํ†ต๊ณ„ํ•™์„ ๊ณต๋ถ€ํ•ด์•ผ ๋˜๋ฉฐ, ์›Œ๋ฐ์—…์„ ์œ„ํ•˜์—ฌ ์œ„ ์ผ€์ด์Šค๋ฅผ ์‚ด์ง ๋‹ค๋ฃจ์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ•์กฐํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ผ ํ• ์ง€๋ผ๋„, ํ†ต๊ณ„ํ•™ ์ง€์‹์˜ ์ˆ˜์ค€์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๋ณด์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. A4. ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ํ”„๋กœ์„ธ์Šค 4. ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ํ”„๋กœ์„ธ์Šค ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ๊ณผ์ •์€ ์š”๋ฆฌ์˜ ๊ณผ์ •๊ณผ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ์ง„ํ–‰ ๋ฐฉ์‹์— ๋Œ€ํ•ด ๋‹ค๋ค„๋ณผ๊นŒ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถ„์„์€ ํ”ํžˆ '์š”๋ฆฌ'์™€ ๋น„๊ตํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์•Œ๊ณ  ๊ณ„์‹œ๋Š” ์š”๋ฆฌ์˜ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์š”์•ฝํ•˜์ž๋ฉด, '์žฌ๋ฃŒ ๊ณต์ˆ˜', '์žฌ๋ฃŒ ํŒŒ์•…', '์žฌ๋ฃŒ ์†์งˆ', '์š”๋ฆฌ', '์‹œ์‹'์˜ ๊ณผ์ •์œผ๋กœ ์ •์˜๋ฅผ ๋‚ด๋ฆด ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์š”๋ฆฌ์— ์“ฐ์ผ ์žฌ๋ฃŒ์˜ ํŠน์„ฑ์„ ์ •ํ™•ํ•˜๊ฒŒ ํŒŒ์•…ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ์žฌ๋ฃŒ์—๋Š” ๊ทธ์— ์•Œ๋งž์€ ์†์งˆ๋ฒ•๊ณผ ๊ฐ€๊ณต๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ทธ ๋งค๋‰ด์–ผ๋งŒ ์ž˜ ๋”ฐ๋ฅด๋ฉด ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ฒŒ ๋˜๋ฉด, ๋ฐ”๋กœ ๋ถ„์„๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ด ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค ๊ณผ์ •์„ ํ†ตํ•ด ์ˆ˜์ง‘์ด ๋˜์—ˆ๋Š”์ง€, ์–ด๋–ค ๋„๋ฉ”์ธ ํŠน์„ฑ์„ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š”์ง€๋ถ€ํ„ฐ ์ •ํ™•ํ•˜๊ฒŒ ํŒŒ์•…์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ธˆ ๋” ๊ณผ์žฅํ•˜์ž๋ฉด, ์„ฑ๊ณต์ ์ธ ๋ฐ์ดํ„ฐ ๋ถ„์„์€ ๋ฐ”๋กœ ์ด ์ฒซ ๋‹จ๊ณ„์—์„œ ๊ฐˆ๋ฆฐ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ด๋„ ๋ฌด๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์€ ๋งค์šฐ ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ํ•ต์‹ฌ์ธ IoT ์„ผ์„œ ๋ฐ์ดํ„ฐ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ, ๋งˆ์ผ€ํŒ…์„ ์œ„ํ•œ ๊ณ ๊ฐ ์ •๋ณด ๋ฐ์ดํ„ฐ, ๋„๋กœ ํ˜ผ์žก๋„ ์˜ˆ์ธก์„ ์œ„ํ•œ ๊ตํ†ต ๋ฐ์ดํ„ฐ ๋“ฑ ๋งค์šฐ ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ 80 ~ 90%๋Š” ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์ด ํ•ด๋‹น์ด ๋œ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ํŠน์„ฑ์„ ํŒŒ์•…ํ•˜์…จ๋‹ค๋ฉด ๋‹ค์Œ์œผ๋กœ ํ™•์ธํ•ด์•ผ ๋˜๋Š” ๋ถ€๋ถ„์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค ๋ณ€์ˆ˜๋“ค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ์„ ํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ณ€์ˆ˜๋“ค์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง ๋ฐฉ๋ฒ•์ด ๊ฒฐ์ •์ด ๋˜๋ฉฐ, ๋” ๋‚˜์•„๊ฐ€ ๊ฒฐ๋ก  ๋„์ถœ์„ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ค ๋ถ„์„ ๋ชจ๋ธ์„ ์ ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€๋„ ๋ฐ”๋กœ ์—ฐ๊ฒฐ์ด ๋  ์ •๋„๋กœ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ํŠน์„ฑ์„ ํŒŒ์•…ํ•˜๊ณ  ์–ด๋–ค ๋ถ„์„ ๋ชจํ˜•๊นŒ์ง€ ์ ์šฉํ• ์ง€ ๊ฒฐ์ •์ด ๋˜์—ˆ๋‹ค๋ฉด, ๊ทธ ๋’ค ์ž‘์—…๋“ค์€ ์ผ์‚ฌ์ฒœ๋ฆฌ๋กœ ๋๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ •ํ•ด์ ธ์žˆ๋Š” ๋งค๋‰ด์–ผ์— ๋”ฐ๋ผ ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๋ฉด ๋˜๊ธฐ์—, ์˜คํžˆ๋ ค ์•ž๋‹จ์—์„œ ์ง„ํ–‰ํ–ˆ๋˜ ํ•ธ๋“ค๋ง(๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ) ์ž‘์—…๋ณด๋‹ค ๋” ์ˆ˜์›”ํ•˜๊ฒŒ ๋๋‚ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ช…์‹ฌํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๋ถ„์„์˜ ์—…๋ฌด๋Ÿ‰์„ 100์œผ๋กœ ์žก์•˜์„ ๋•Œ, ๊ทธ์ค‘ 80 ~ 90์€ ๋ฐ”๋กœ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ ์ž‘์—…์ด ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๋ถ„์„์ด ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ๋ง์น˜๋Š” ์š”์ธ์ผ๊นŒ? ๋ฐ์ดํ„ฐ ๋ถ„์„์ด ๋งํ•˜๋Š” ์ด์œ ์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๊ฒ ์ง€๋งŒ, ์ œ๊ฐ€ ์ƒ๊ฐํ–ˆ์„ ๋•Œ๋Š” '๋ฌป์ง€ ๋งˆ ๋ถ„์„'์ž…๋‹ˆ๋‹ค. ํ”ํžˆ๋“ค ์‚ฌ๋žŒ๋“ค์ด ๊ฐ€์žฅ ๋งŽ์ด ์ €์ง€๋ฅด๋Š” ์ž˜๋ชป์€ ์ผ๋‹จ ๋ฐ์ดํ„ฐ์— ์ƒ๊ด€๋ถ„์„๊ณผ ํšŒ๊ท€๋ถ„์„์„ ๋•Œ๋ ค ๋„ฃ๊ณ  ๊ฒฐ๊ด๊ฐ’์„ ๋ƒ…๋‹ˆ๋‹ค. ํšŒ๊ท€๋ถ„์„์—์„œ ์š”๊ตฌ๋˜๋Š” ๊ธฐ๋ณธ ๊ฐ€์ •๋“ค์€ ๊นก๊ทธ๋ฆฌ ๋ฌด์‹œํ•œ ์ฑ„, ์ผ๋‹จ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๊ณ  ๋ฐœํ‘œ๋ฅผ ๋‚ด๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ง์”€๋“œ๋ฆฌ๋ฉด, ๋ถ„์„ ๋ชจํ˜•์€ ์šฐ๋ฆฌ๊ฐ€ ์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋ฐ์ดํ„ฐ์˜ ํŠน์„ฑ์ด ์•Œ์•„์„œ ์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๋ถ„์„์„ ํ•˜๊ธฐ ์ „์— ์—ด์‹ฌํžˆ ๋ฐ์ดํ„ฐ๋ถ€ํ„ฐ ๊ณต๋ถ€๋ฅผ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ๋„๋ฉ”์ธ, ๋ณ€์ˆ˜๋“ค์˜ ๋ถ„ํฌ, ์ „์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ธ์ง€.. ๊ทธ ๋ชจ๋“  ๊ฒƒ์ด ํฌํ•จ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€๋”, ๊ณต๋ถ€๋ฅผ ์—ด์‹ฌํžˆ ํ•˜์‹  ๋ถ„๋“ค์ด ์–ด๋ ต๊ฒŒ ๊ณต๋ถ€ํ•œ ํ†ต๊ณ„ ๋ชจํ˜•, ๊ธฐ๊ณ„ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋“ฑ์„ ํ™œ์šฉํ•˜๊ณ  ์‹ถ์–ด, ๋ถ„์„ ๋ชจํ˜•๋ถ€ํ„ฐ ์ •ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ์ง€๋Š” ๊ฒฝ์šฐ๋„ ์‹ฌ์‹ฌ์น˜ ์•Š๊ฒŒ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐ์ดํ„ฐ ๋ถ„์„์ด ๋งํ•˜๋Š” ์ง€๋ฆ„๊ธธ์ž…๋‹ˆ๋‹ค. ์ข€ ๋งŽ์ด ๊ท€์ฐฎ๋”๋ผ๋„ ์—ฌ๋Ÿฌ๋ถ„๋“ค์€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ถฉ๋ถ„ํ•œ ํ•™์Šต์„ ์ง„ํ–‰ํ•œ ํ›„, ๋ถ„์„ ๋ชจํ˜•์„ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ ๊ฐ„๊ฒฐํ•˜๋ฉด์„œ๋„ ์˜๋ฏธ๊ฐ€ ์žˆ๋Š” ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ๋ฝ‘์•„๋‚ผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Ch2. R ๊ธฐ๋ณธ ๋ฌธ๋ฒ• 1๋‹จ๊ณ„ ๊ธฐ๋ณธ์ ์œผ๋กœ Rstudio ํŽธ์ง‘๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. Rstudio๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ, ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ Tip์„ ๋“œ๋ฆฌ์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ๋ถ„๋“ค์ด ๊ฐ€์žฅ ๋งŽ์ด ํ‹€๋ฆฌ๋Š” ๋ถ€๋ถ„์€ ์˜คํƒ€์ž…๋‹ˆ๋‹ค. ์†Œ, ๋Œ€๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜คํƒ€๊ฐ€ ํŠนํžˆ ๋งŽ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ 'No such file or directory'๋ผ๋Š” Error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ์˜คํƒ€๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜, ์ €์žฅ์„ ์•ˆํ•œ ๊ฒƒ์ด๋‹ˆ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ํ™•์ธํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ž‘์„ฑํ•˜๋‹ค๊ฐ€ Tab ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด, ์ž๋™์™„์„ฑ์ฐฝ์ด ๋œน๋‹ˆ๋‹ค. ์ ๊ทน ํ™œ์šฉํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์šฐ์ธก ์ƒ๋‹จ์˜ Environment ์ฐฝ์—์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ๋ถˆ๋Ÿฌ์™€์ง€๊ณ  ์ €์žฅ๋˜๊ณ  ์žˆ๋Š”์ง€ ๊พธ์ค€ํžˆ ํ™•์ธํ•˜๋ฉด์„œ ์ง„ํ–‰ํ•˜๋ฉด, ์˜ค๋ฅ˜ ๋ฐœ์ƒ์„ ์ค„์ผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Rstudio๋Š” ํ•œ๊ธ€์— ์นœํ™”์ ์ด์ง€ ์•Š์œผ๋‹ˆ ์ตœ๋Œ€ํ•œ ์˜์–ด๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. A1. ํ• ๋‹น ๋ฐ ๋…ผ๋ฆฌ๋ฌธ 1. ํ• ๋‹น ๋ฐ ๋…ผ๋ฆฌ๋ฌธ ํ• ๋‹น ์ง€์ •ํ•ด ์ฃผ๋Š” ๋ช…์นญ(์ €์žฅ์†Œ)์— ๊ฐ’์„ ์ €์žฅํ•ด ์ฃผ๋Š” ๋งค์šฐ ๊ธฐ๋ณธ์ ์ธ ์ปดํ“จํ„ฐ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. = : ~๋ฅผ ~์— ์ €์žฅํ•˜์—ฌ๋ผ A = 2 print(A) [1] 2 ์‹คํ–‰ ๊ฒฐ๊ณผ, A์— 2๊ฐ€ ์ €์žฅ๋˜์—ˆ์œผ๋ฉฐ, ์ถœ๋ ฅํ•˜๋ฉด 2๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋…ผ๋ฆฌ๋ฌธ ๊ฐ’์ด ์ง€์ •ํ•ด ์ค€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ์ฃผ๋Š” ๊ธฐ๋ณธ์ ์ธ ์ปดํ“จํ„ฐ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. 'TRUE' ํ˜น์€ 'FALSE'๊ฐ’์„ ์ถœ๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. 'TRUE'๋Š” ์กฐ๊ฑด์„ ์ถฉ์กฑํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฉฐ, 'FALSE'๋Š” ๊ทธ ๋ฐ˜๋Œ€์ž…๋‹ˆ๋‹ค. == : ์ธ์ง€ ํŒ๋‹จํ•˜์—ฌ๋ผ A == 2 [1] TRUE == : ์ธ์ง€ ํŒ๋‹จํ•˜์—ฌ๋ผ A != 2 [1] FALSE A2. c()์˜ ํ™œ์šฉ 2. c()์˜ ํ™œ์šฉ ๋ฒกํ„ฐ์˜ ์ƒ์„ฑ c()๋Š” Combind์˜ ์•ฝ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ช…๋ น์–ด๋กœ, R์—์„œ ๋งค์šฐ ์ž์ฃผ ์“ฐ์ด๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. c()๋Š” ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ฒกํ„ฐ๋ผ๊ณ  ํ•˜๋ฉด, ๋ฐ์ดํ„ฐ์—์„œ ํ•˜๋‚˜์˜ โ€™์—ด(Column)โ€™์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฐ์ดํ„ฐ๊ฐ€ ์„ธ๋กœ๋กœ ์ €์žฅ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. c() : ๋ฒกํ„ฐ์˜ ์ƒ์„ฑ B = c(2,3,4,5) print(B) [1] 2 3 4 5 A3. rep(), seq()์„ ํ†ตํ•œ ๋ฒกํ„ฐ ์ƒ์„ฑ 3. rep(), sep()์„ ํ†ตํ•œ ๋ฒกํ„ฐ ์ƒ์„ฑ ์ˆœ์ฐจ์ ์ธ ์ˆ˜์—ด ์ƒ์„ฑ sequence์˜ ์ค„์ž„๋ง๋กœ ์ˆœ์ฐจ์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์“ฐ์ž…๋‹ˆ๋‹ค. seq() : seq(from = ์‹œ์ž‘ ์ˆซ์ž, to = ๋งˆ์ง€๋ง‰ ์ˆซ์ž, by = ์ฆ๊ฐ€๋ฒ”์œ„) # 1 ~ 10๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜์—ด ์ƒ์„ฑ x1 = c(1:10) x1_2 = seq(from = 1, to = 10, by = 1) # 1 ~ 10๊นŒ์ง€ 2์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜์—ด ์ƒ์„ฑ x2 = seq(from = 1, to = 10, by = 2) print(x1) [1] 1 2 3 4 5 6 7 8 9 10 print(x1_2) [1] 1 2 3 4 5 6 7 8 9 10 print(x2) [1] 1 3 5 7 9 ๋ฐ˜๋ณต์ ์ธ ์ˆ˜์—ด ์ƒ์„ฑ rep()๋Š” repeat์˜ ์ค„์ž„๋ง๋กœ ๋ฐ˜๋ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์“ฐ์ž…๋‹ˆ๋‹ค. rep() : rep(๋ฐ˜๋ณตํ•  ๊ฐ’, ๋ฐ˜๋ณตํ•  ํšŸ์ˆ˜) # 1์„ 10๋ฒˆ ๋ฐ˜๋ณต y = rep(1,10) print(y) [1] 1 1 1 1 1 1 1 1 1 1 y2 = rep(c(1,10), 2) print(y2) [1] 1 10 1 10 y3 = rep(c(1,10), c(2,2)) print(y3) [1] 1 1 10 10 A4. matrix(), data.frame()์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์…‹ ๋งŒ๋“ค๊ธฐ 4. matrix(), data.frame()์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์…‹ ๋งŒ๋“ค๊ธฐ matrix์˜ ์ƒ์„ฑ R์—์„œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ ์…‹์„ matrix ํ˜•ํƒœ ํ˜น์€ data.frame ํ˜•ํƒœ๋กœ ์ •๋ฆฌํ•œ ํ›„, ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. matrix(data = ๋ฐ์ดํ„ฐ, nrow = ํ–‰์˜ ์ˆ˜, ncol = ์—ด์˜ ์ˆ˜, byrow = ํ–‰/์—ด ๊ธฐ์ค€) MATRIX_R = matrix( data = x1, nrow = 5 ) print(MATRIX_R) [,1] [,2] [1, ] 1 6 [2, ] 2 7 [3, ] 3 8 [4, ] 4 9 [5, ] 5 10 MATRIX_C = matrix( data = x1, ncol = 5 ) print(MATRIX_C) [,1] [,2] [,3] [,4] [,5] [1, ] 1 3 5 7 9 [2, ] 2 4 6 8 10 dataframe์˜ ์ƒ์„ฑ DATA_SET = data.frame( X1 = x1, # ๋ณ€์ˆ˜๋ช… = ๋ฒกํ„ฐ ๊ฐ’, X1_2 = x1_2, X2 = x2, y = y ) ๋ฐ์ดํ„ฐ ์ƒ์œ„๋‹จ ๋ณด๊ธฐ head()๋Š” ๋ฐ์ดํ„ฐ์˜ ์ƒ๋‹จ๋ถ€๋ถ„์„ ์ง€์ •ํ•ด ์ฃผ๋Š” ํ–‰๋งŒํผ ์ถœ๋ ฅํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ 5๋กœ ์„ค์ •ํ•˜์˜€์œผ๋‹ˆ 1 ~ 5ํ–‰์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. head(๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„) print(head(DATA_SET, 5)) A5. length(), dim()์„ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ํ˜•ํƒœ ํŒŒ์•…ํ•˜๊ธฐ 5. length(), dim()์„ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ํ˜•ํƒœ ํŒŒ์•…ํ•˜๊ธฐ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ํ˜•ํƒœ๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์€, ๋ฐ์ดํ„ฐ ๋ถ„์„ ์ง„ํ–‰ ์ƒํ™ฉ ์ค‘์— ํ‹ˆํ‹ˆ์ด ํ™•์ธ์„ ํ•ด์•ผ ํ•˜๋Š” ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. 1์ฐจ์› ๋ฒกํ„ฐ์ธ ๊ฒฝ์šฐ, length()๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. length(1์ฐจ์› ๋ฒกํ„ฐ) # ๋ฒกํ„ฐ์— ์†ํ•œ ์›์†Œ์˜ ๊ฐœ์ˆ˜ length(x1) [1] 10 2์ฐจ์› ํ–‰๋ ฌ, ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์ธ ๊ฒฝ์šฐ์—๋Š” dim()์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. dim(ํ–‰๋ ฌ ํ˜น์€ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„) # ํ–‰, ์—ด dim(MATRIX_R) [1] 5 2 dim(DATA_SET) [1] 10 4 A6. ๊ด„ํ˜ธ์˜ ํ™œ์šฉ 6. ๊ด„ํ˜ธ์˜ ํ™œ์šฉ ๊ด„ํ˜ธ์—๋Š” (), {}, []๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ์ปดํ“จํ„ฐ ์–ธ์–ด์—์„œ๋Š” ๊ฐ ๊ด„ํ˜ธ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ์“ฐ์ด๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. R์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ( ) ()๋Š” ์‹คํ–‰ ํ•จ์ˆ˜(function)๊ณผ ํ•จ๊ป˜ ์“ฐ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, c()์—์„œ c()๋Š” ๋“ค์–ด์˜ค๋Š” ๊ฐ’๋“ค์„ ๋ฌถ์–ด ํ•˜๋‚˜์˜ ๋ฒกํ„ฐ๋กœ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. () ์•ˆ์—๋Š” ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ์›์†Œ(element) ๊ฐ’๋“ค์ด ์ž…๋ ฅ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # ( ) A=c(1,2,3,4,5) print(A) [1] 1 2 3 4 5 { } { }๋Š” for, if ๋ฌธ ๋“ฑ์—์„œ ์กฐ๊ฑด์‹์„ ์‚ฝ์ž…ํ•  ๋•Œ ์“ฐ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, A ๋ฒกํ„ฐ์— ์†ํ•ด ์žˆ๋Š” ๊ฐ’๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # { } for(i in A){ print(i) # {} ์•ˆ์— ์‹คํ–‰ ํ•จ์ˆ˜๋ฅผ ์‚ฝ์ž… } [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 ์ด๋ฒˆ์—๋Š” ๋นˆ ๊ณต๊ฐ„์„ ๊ฐ€์ง€๋Š” ๋ฒกํ„ฐ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“  ๋‹ค์Œ, ๋นˆ ๋ฒกํ„ฐ์— ๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ์‚ฝ์ž…ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. B = c() # ๋นˆ ๊ณต๊ฐ„์˜ ๋ฒกํ„ฐ ์ƒ์„ฑ for(k in seq(from = 1, to = 10, by = 1)){ B = c(B, k) } print(B) [1] 1 2 3 4 5 6 7 8 9 10 [] []๋Š” Index๋ฅผ ์ž…๋ ฅํ•ด์•ผ ๋  ๋•Œ ์“ฐ์ž…๋‹ˆ๋‹ค. ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ๋ถ„๋“ค์ด ๊ฐ€์žฅ ํ‹€๋ฆฌ๋Š” ๊ณณ์ด ๋Œ€๊ด„ํ˜ธ์˜ ํ™œ์šฉ์ž…๋‹ˆ๋‹ค. ์–ธ๋œป ๋ณด๊ธฐ์—๋Š” ์‰ฝ์ง€๋งŒ, ๋‚˜์ค‘์— ์ƒ๋‹น์ˆ˜์˜ Error๋Š” ์—ฌ๊ธฐ์„œ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 1์ฐจ์› ๋ฒกํ„ฐ์˜ ๊ฒฝ์šฐ, ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณด๊ณ  ์‹ถ์€ ์œ„์น˜๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋˜์ง€๋งŒ, 2์ฐจ์› ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ ์…‹์—์„œ๋Š” ๋ณด๊ณ  ์‹ถ์€ [ํ–‰, ์—ด]์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1์ฐจ์› ๋ฐ์ดํ„ฐ์˜ ๊ฒฝ์šฐ # 2๋ฒˆ์งธ ๊ฐ’ A[2] [1] 2 # 1,2๋ฒˆ์งธ ๊ฐ’ A[1:2] [1] 1 2 # 3๋ฒˆ์งธ ๊ฐ’ ๋นผ๊ณ  A[-3] [1] 1 2 4 5 # 1,2,4,5๋ฒˆ์งธ ๊ฐ’ A[c(1,2,4,5)] [1] 1 2 4 5 2์ฐจ์› data.frame() ํ˜•ํƒœ์˜ ๊ฒฝ์šฐ # 1ํ–‰ ์ „๋ถ€ DATA_SET[1, ] # 1ํ–‰ ์ „๋ถ€ X1 X1_2 X2 y 1 1 1 1 1 # 1์—ด ์ „๋ถ€ DATA_SET[,1] [1] 1 2 3 4 5 6 7 8 9 10 # 1,2,3 ํ–‰ & 2์—ด ๋นผ๊ณ  ๋‚˜๋จธ์ง€ print(DATA_SET[c(1,2,3),-2]) X1 X2 y 1 1 1 1 2 2 3 1 3 3 5 1 A7. ๋ณ€์ˆ˜ ํ˜•ํƒœ ์ดํ•ดํ•˜๊ธฐ 7. ๋ณ€์ˆ˜ ํ˜•ํƒœ ์ดํ•ดํ•˜๊ธฐ R์—์„œ๋Š” ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์„ ๋‹ค์Œ์œผ๋กœ ์ •๋ฆฌ๋ฅผ ํ•˜๋ฉฐ, ๋ณดํ†ต โ€™Stringsโ€™๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. Strings ํŒŒ์•…์ด ์ค‘์š”ํ•œ ์ด์œ  Strings์— ๋”ฐ๋ผ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ˆœ์„œํ˜• ๋ณ€์ˆ˜๋ฅผ ๋ช…๋ชฉํ˜• ๋ณ€์ˆ˜๋กœ ์ทจ๊ธ‰ํ•˜๋Š” ๊ฒฝ์šฐ, ์ˆœ์„œํ˜•์— ์†ํ•œ ์„œ์—ด ์ •๋ณด๋ฅผ ํฌ๊ธฐํ•˜์—ฌ ๋ถ„์„ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ช…๋ น์–ด์— ๋”ฐ๋ผ Error๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ฐ’์ด ๊ดด์ƒํ•˜๊ฒŒ ๋ณ€๊ฒฝ๋˜๋Š” ์ƒํ™ฉ๋„ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฌด์—‡๋ณด๋‹ค ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ด์œ ๋Š” ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ณ€์ˆ˜๋“ค์˜ Strings์— ๋”ฐ๋ผ ๋ถ„์„๋ฐฉ๋ฒ•๋ก ์ด ์ •ํ•ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ํŠน์„ฑ์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜์ง€๋งŒ, ๋ถ„์„ ๊ณผ์ •์—์„œ๋Š” ๋งŽ์€ ์‹ค์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐœ์ธ์ ์œผ๋กœ๋Š” ๋ถ„์„๊ฐ€์˜ ์„ผ์Šค๊ฐ€ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๊ฒŒ ์ ์šฉ๋˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. Discrete(์ด์‚ฐํ˜•): ํ•˜๋‚˜, ๋‘˜, ์…‹, ๋„ท ๋“ฑ ์…€ ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜ ๋ช…๋ชฉํ˜• ๋ณ€์ˆ˜: ํŠน์„ฑ์— ๋”ฐ๋ผ ๋ช…์นญ์„ ์ฃผ์–ด ๊ตฌ๋ถ„์„ ์ง€์–ด์ฃผ๋Š” ๋ณ€์ˆ˜, ๋ณ€์ˆซ๊ฐ’์— ๋”ฐ๋ผ ์„œ์—ด ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ณ  ๋ชจ๋‘ ๋™๋“ฑํ•œ ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ์„œ์—ดํ˜• ๋ณ€์ˆ˜: ๋ช…๋ชฉํ˜• ๋ณ€์ˆ˜์—์„œ ์„œ์—ด ์ •๋ณด๊ฐ€ ์ฃผ์ž…๋œ ๋ณ€์ˆ˜, ๊ทธ์— ๋”ฐ๋ผ ๋ช…๋ชฉํ˜• ๋ณ€์ˆ˜์— ๋น„ํ•ด ์ •๋ณด๋Ÿ‰์„ ๋” ๋งŽ์ด ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. Continuous(์—ฐ์†ํ˜•): ์…€ ์ˆ˜ ์—†๊ณ  ๊ตฌ๊ฐ„์œผ๋กœ ์ •์˜๋œ ๋ณ€์ˆ˜ ์ •๋ณด๋Ÿ‰์„ ๊ฐ€์žฅ ๋งŽ์ด ํ’ˆ๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ์ฒ™๋„์— ๋”ฐ๋ฅธ ์ •๋ณด๋Ÿ‰ ๋Œ€ํ•™์ƒ ์„ฑ์ ์„ ์˜ˆ์‹œ๋กœ ์‚ผ์•˜์„ ๋•Œ, ๋ช…๋ชฉํ˜•์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ ์ทจ๊ธ‰ํ•  ๊ฒฝ์šฐ, ํ•™์ƒ์˜ ํ•™์ ์ด ์ˆ˜์—ฌ ๋ถ€๋งŒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์„œ์—ดํ˜•์œผ๋กœ ์ทจ๊ธ‰ํ•  ๋•Œ๋Š” ํ•™์  ์ด์ˆ˜๋ฅผ ํ•œ ํ•™์ƒ ์ค‘์—์„œ D0 ~ A+์˜ ์„ฑ์ ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ๋‚˜์•„๊ฐ€, ์—ฐ์†ํ˜•์œผ๋กœ ์ทจ๊ธ‰ํ•  ๊ฒฝ์šฐ์—๋Š” ๊ฐ™์€ A+๋‚ด์—์„œ๋„ ๋ฐฑ๋ถ„์œ„๋ฅผ ํ†ตํ•ด ์„œ์—ด์„ ์•Œ์•„๋‚ผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ •๋ณด๋Ÿ‰์€ ๋ช…๋ชฉํ˜•์—์„œ ์—ฐ์†ํ˜•์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ๋” ๋งŽ์•„์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ๋ณ€ํ™˜ ์ •๋ณด๋Ÿ‰์ด ํ’๋ถ€ํ•œ ์—ฐ์†ํ˜•์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ˆœ์„œ, ๋ช…๋ชฉํ˜•์œผ๋กœ ๋ณ€ํ™˜์ด ๋˜๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋ช…๋ชฉํ˜•์ด ์ˆœ์„œ ํ˜น์€ ์—ฐ์†ํ˜• ๋ฐ์ดํ„ฐ๋กœ, ์ˆœ์„œํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ์†ํ˜•์œผ๋กœ ์ทจ๊ธ‰ํ•˜๊ธฐ์—๋Š” ์ •๋ณด๋Ÿ‰์ด ๋ถ€์กฑํ•˜๊ธฐ์— ๋ณ€ํ™˜์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž… ํ™•์ธ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋“ค์˜ ํƒ€์ž…์€ str() ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. str(๋ฒกํ„ฐ, ํ–‰๋ ฌ, ๋ฐ์ดํ„ฐ ๋“ฑ ๋ชจ๋“  ์ €์žฅ ๊ฐ’) Numeric_Vector = c(1:20) Chr_Vector = c("A","B","C") str(Numeric_Vector) int [1:20] 1 2 3 4 5 6 7 8 9 10 ... str(Chr_Vector) chr [1:3] "A" "B" "C" A8. ์‹œ๊ฐ„(๋‚ ์งœ) ํ˜•ํƒœ์˜ ๋ณ€์ˆ˜ ๋‹ค๋ฃจ๊ธฐ 8. ์‹œ๊ฐ„(๋‚ ์งœ) ํ˜•ํƒœ์˜ ๋ณ€์ˆ˜ ๋‹ค๋ฃจ๊ธฐ R์—์„œ ์‹œ๊ฐ„(๋‚ ์งœ) ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. 1. as.Date()๋ฅผ ํ™œ์šฉํ•˜์—ฌ โ€˜๋…„-์›”-์ผโ€™ ํ˜•ํƒœ๋กœ ๋‹ค๋ฃจ๊ธฐ 2. as.POSIXct()๋ฅผ ํ™œ์šฉํ•˜์—ฌ โ€˜๋…„-์›”-์ผ ์‹œ:๋ถ„:์ดˆโ€™ ํ˜•ํƒœ๋กœ ๋‹ค๋ฃจ๊ธฐ 3. lubridate ํŒจํ‚ค์ง€๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‚ ์งœ ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ lubridate ํŒจํ‚ค์ง€๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃจ์ง€ ์•Š๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. as.Date()๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋‚ ์งœ๊ฐ€ ์ž…๋ ฅ๋œ ํฌ๋งท์— ๋งž์ถฐ ์˜ต์…˜ ๊ฐ’์— ์„ค์ •์„ ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. as.Date(๋ณ€์ˆ˜, format = โ€œ๋‚ ์งœ<NAME>โ€) ์ฒ˜์Œ ์ž…๋ ฅ๋˜์—ˆ์„ ๋•Œ๋Š”, strings๊ฐ€ character์˜€์ง€๋งŒ, as.Date๋ฅผ ํ†ตํ•ด strings๊ฐ€ Date๋กœ ๋ณ€ํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. DATE_O = "2018-01-02" DATE_C = as.Date(DATE_O, format = "%Y-%m-%d") str(DATE_O) chr "2018-01-02" str(DATE_C) Date[1:1], format: "2018-01-02" as.POSIXct()๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒฝ์šฐ POSIXct๋Š” ์‹œ๊ฐ„:๋ถ„:์ดˆ๊นŒ์ง€ ํ™œ์šฉํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. as.POSIXct(๋‚ ์งœ, format = โ€œ๋‚ ์งœ<NAME>โ€) DATE_O2 = "2015-02-04 23:13:23" DATE_P = as.POSIXct(DATE_O2, format = "%Y-%m-%d %H:%M:%S") str(DATE_P) POSIXct[1:1], format: "2015-02-04 23:13:23" format()์˜ ํ™œ์šฉ ๋‚ ์งœ ์ •๋ณด๋ฅผ ๋ฝ‘์•„ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. format(๋‚ ์งœ ๋ณ€์ˆ˜, โ€œํ˜•์‹โ€) format(DATE_P,"%A") [1] "์ˆ˜์š”์ผ" format(DATE_P,"%S") [1] "23" format(DATE_P,"%M") [1] "13" format(DATE_P,"%Y") [1] "2015" A9. as & is๋ฅผ ํ†ตํ•ด strings ํ™•์ธ ๋ฐ ๋ณ€๊ฒฝํ•˜๊ธฐ 9. as & is๋ฅผ ํ†ตํ•ด strings ํ™•์ธ ๋ฐ ๋ณ€๊ฒฝํ•˜๊ธฐ as() as๋Š” โ€œ๋ณ€์ˆ˜ x๋ฅผ ~๋กœ ์ทจ๊ธ‰ํ•˜๊ฒ ๋‹ค.โ€๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋˜ํ•œ summary()๋Š” ๋งค์šฐ ์ž์ฃผ ์“ฐ์ด๋Š” ๋ช…๋ น์–ด์ด๋ฉฐ, ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์— ๋”ฐ๋ผ ๊ฒฐ๊ด๊ฐ’์ด ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ต๋‹ˆ๋‹ค. x=c(1,2,3,4,5,6,7,8,9,10) x1 = as.integer(x) x2 = as.numeric(x) x3 = as.factor(x) x4 = as.character(x) str(x1) int [1:10] 1 2 3 4 5 6 7 8 9 10 summary(x1) Min. 1st Qu. Median Mean 3rd Qu. Max. 1.00 3.25 5.50 5.50 7.75 10.00 str(x2) num [1:10] 1 2 3 4 5 6 7 8 9 10 summary(x2) Min. 1st Qu. Median Mean 3rd Qu. Max. 1.00 3.25 5.50 5.50 7.75 10.00 str(x3) Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 summary(x4) Length Class Mode 10 character character str(x4) chr [1:10] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" is() is๋Š” ๋…ผ๋ฆฌ๋ฌธ์œผ๋กœ์จ ๋ณ€์ˆ˜ x๊ฐ€ ~์ธ์ง€ ํŒ๋‹จํ•˜์—ฌ๋ผ.๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. str()์€ ๋‹จ์ˆœํžˆ Stirngs๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด์ง€, ๋…ผ๋ฆฌ๋ฌธ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด ์ฃผ์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋…ผ๋ฆฌ ์กฐ๊ฑด์ด ํ•„์š”ํ•  ๋•Œ๋Š” is.()๋ฅผ ์จ์•ผ ํ•ฉ๋‹ˆ๋‹ค. x=c(1,2,3,4,5,6,7,8,9,10) y=c("str",'str2',"str3","str4") is.integer(x) [1] FALSE is.numeric(x) [1] TRUE is.factor(y) [1] FALSE is.character(y) [1] TRUE B1. sample()์„ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ๋ฌด์ž‘์œ„ ์ถ”์ถœํ•˜๊ธฐ 10. sample()์„ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ๋ฌด์ž‘์œ„ ์ถ”์ถœํ•˜๊ธฐ ๋ฐ์ดํ„ฐ๊ฐ€ ๋„ˆ๋ฌด ๋ฐฉ๋Œ€ํ•œ ๊ฒฝ์šฐ, ํ•„์š” ์—†์ด ๊ธด ์—ฐ์‚ฐ์„ ํ•ด์•ผ ๋  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ๋ฌด์ž‘์œ„ ์ถ”์ถœ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ์˜ ํŠน์„ฑ์€ ์‚ด๋ฆฌ๋ฉด์„œ, ์—ฐ์‚ฐ์†๋„๋ฅผ ๋‚ฎ์ถ”๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. replace = FALSE๋Š” ๋น„๋ณต์› ์ถ”์ถœ์„ ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. sample(๋ฐ์ดํ„ฐ ์ถ”์ถœ ๋ฒ”์œ„, ๋ฐ์ดํ„ฐ ์ถ”์ถœ ๊ฐœ์ˆ˜, replace = โ€œFALSE ORโ€TRUE") # ๋กœ๋˜ ๋ฒˆํ˜ธ ์ถ”์ฒจ S1 = sample(1:45, 6, replace = FALSE) # 1 ~ 45 ์ค‘์— 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋น„๋ณต์› ์ถ”์ถœํ•˜๊ฒ ๋‹ค. print(S1) [1] 36 28 5 16 26 13 sample() ๊ฐ™์€ ๋ฌด์ž‘์œ„ ๊ฐ’์„ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋Š” ์ฝ”๋“œ๋Š” ๋ฌด์ž‘์œ„ ๊ฒฐ๊ด๊ฐ’(์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋Š”)์„ ๊ณ ์ •์‹œ์ผœ์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ, set.seed()๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒฐ๊ด๊ฐ’์„ ๊ณ ์ •ํ•ฉ๋‹ˆ๋‹ค. set.seed(1234) S2 = sample(1:45, 6, replace = FALSE) print(S2) [1] 6 28 27 43 36 26 set.seed()์˜ ๊ด„ํ˜ธ ์•ˆ์—๋Š” ์•„๋ฌด ์ˆซ์ž๋ฅผ ์ €์žฅํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ์ˆซ์ž์— ํ•จ๊ป˜ ์‹คํ–‰๋œ ๋ฌด์ž‘์œ„ ๊ฒฐ๊ด๊ฐ’์ด ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ๊ธฐ๋Šฅ ํ™•์ธ์„ ์œ„ํ•ด์„œ๋Š” set.seed()๋ฅผ ํฌํ•จํ•˜์—ฌ sample()์„ ๋Œ๋ ค๋ณธ ๋‹ค์Œ, sample()๋งŒ ๋Œ๋ ค๋ณด์‹œ๋ฉด ๋ฐ”๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. B2. ์กฐ๊ฑด๋ฌธ(if) ํ™œ์šฉํ•˜๊ธฐ 11. ์กฐ๊ฑด๋ฌธ(if) ํ™œ์šฉํ•˜๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜๋‹ค ๋ณด๋ฉด, ๋ฐ˜๋ณต์ž‘์—…์„ ์ง„ํ–‰ํ•ด์•ผ ๋˜๊ฑฐ๋‚˜, case by case ๋ณ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ฆฌํ•ด์•ผ ๋  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ, ํ•˜๋‚˜์”ฉ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” for ๋ฌธ๊ณผ if ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์ผ์ด ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. for ๋ฌธ์€ ์•ž์„œ ์ค‘๊ด„ํ˜ธ ํ™œ์šฉ ๋•Œ ๋‹ค๋ค„๋ดค์œผ๋‹ˆ, if ๋ฌธ์„ ํ™œ์šฉํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A = c(1,2,3,4,5) if( 7 %in% A){ # %in% A์— ์†ํ•ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋…ผ๋ฆฌ๋ฌธ print("TRUE") } else{ print("FALSE") } [1] "FALSE" B3. function()์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ 12. function()์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ ๋Œ€๋ถ€๋ถ„์˜ ํ†ต๊ณ„ ๋ถ„์„์€ R์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” ํ•จ์ˆ˜์™€ ํŒจํ‚ค์ง€๋“ค์„ ์ถ”๊ฐ€๋กœ ๋‹ค์šด๋กœ๋“œํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ€๋” ์ง์ ‘ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์•ผ ๋  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. function()์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ž…๋ ฅ ๊ฐ’์— +1์„ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ธฐ Plus_One = function(x){ y = x+1 return(y) } Plus_One(3) [1] 4 B4. R ํŒจํ‚ค์ง€ ์„ค์น˜ํ•˜๊ธฐ 13. R ํŒจํ‚ค์ง€ ์„ค์น˜ํ•˜๊ธฐ R์€ ํ”„๋กœ๊ทธ๋žจ์ด ๊ฐ€๋ฒผ์šด ๋Œ€์‹ , ํ•„์š”ํ•œ ํ•จ์ˆ˜๋Š” ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ ํ›„์—๋Š” ํ•ญ์ƒ ํŒจํ‚ค์ง€๋ฅผ R์— ๋ถ€์ฐฉ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. install.packages("ggplot2") # ggplot2๋ผ๋Š” ํŒจํ‚ค์ง€ ์„ค์น˜ library(ggplot2) # ggplot2 ํŒจํ‚ค์ง€ ๋ถ€์ฐฉ B4. ์—ฐ์Šต๋ฌธ์ œ 14. ์—ฐ์Šต๋ฌธ์ œ sample()์„ ํ™œ์šฉํ•ด์„œ ๋กœ๋˜๋ฒˆํ˜ธ(1 ~ 45, 6๊ฐœ)๋ฅผ ์ถ”์ฒจํ•˜์—ฌ๋ผ. ๋‹ค์Œ์˜ ์ˆ˜์—ด๋กœ ๊ตฌ์„ฑ๋œ ๋ฒกํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์‹œ์˜ค. V ( , , , , , , 99 ) V ( , , , , , , , , , ) ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ–‰๋ ฌ์„ ์ƒ์„ฑํ•˜์‹œ์˜ค. = [ 1 3 4 6 7 9 ] (์œ„ํ‚ค๋…์Šค ๋ฌธ๋ฒ•์ƒ ๋งคํŠธ๋ฆญ์Šค๊ฐ€ 1ํ–‰์œผ๋กœ ํ‘œํ˜„์ด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ๋“ค์–ด์•ผ ๋˜๋Š” ๋งคํŠธ๋ฆญ์Šค๋Š” 3ํ–‰ 3์—ด์ž…๋‹ˆ๋‹ค.) funtion()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ์˜ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“œ์‹œ์˜ค. u d a i ( , ) x + + 10 for() ๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ๊ตฌ๊ตฌ๋‹จ์„ ๋งŒ๋“œ์‹œ์˜ค. Ch3. R ๊ธฐ๋ณธ ๋ฌธ๋ฒ• 2๋‹จ๊ณ„ ์ด๋ฒˆ Chapter์—์„œ๋Š” ์‰ฌ์–ด๊ฐ€๋Š” ๋Š๋‚Œ์œผ๋กœ, ๊ฐ„๋‹จํ•˜๊ฒŒ ํ†ต๊ณ„ ๊ฐ’(Statistics)์„ ๋ฝ‘์•„๋‚ด๋Š” ๋ฐฉ๋ฒ•๋“ค์„ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A1. ์—ฐ์Šต ๋ฐ์ดํ„ฐ ์„ค๋ช… 1. ์—ฐ์Šต ๋ฐ์ดํ„ฐ ์„ค๋ช… ์ด๋ฒˆ์— ๋‹ค๋ฃฐ ๋ฐ์ดํ„ฐ๋Š” ์–ด๋–ค ํšŒ์‚ฌ์˜ โ€˜HR(Human Resource, ์ธ์‚ฌ๊ด€๋ฆฌ)โ€™ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ๋งํฌ : https://www.drop box.com/sh/xx1w2syi768kfU0/AACZgxgo1fcxyDMgv9U-iTz8a?dl=0 ๋ณ€์ˆ˜ ์„ค๋ช… satisfaction_level : ์ง๋ฌด ๋งŒ์กฑ๋„ last_evaluation : ๋งˆ์ง€๋ง‰ ํ‰๊ฐ€์ ์ˆ˜ number_project : ์ง„ํ–‰ ํ”„๋กœ์ ํŠธ ์ˆ˜ average_monthly_hours : ์›”ํ‰๊ท  ๊ทผ๋ฌด์‹œ๊ฐ„ time_spend_company : ๊ทผ์†์—ฐ์ˆ˜ work_accident : ์‚ฌ๊ฑด์‚ฌ๊ณ  ์—ฌ๋ถ€(0: ์—†์Œ, 1: ์žˆ์Œ, ๋ช…๋ชฉํ˜•) left : ์ด์ง ์—ฌ๋ถ€(0: ์ž”๋ฅ˜, 1: ์ด์ง, ๋ช…๋ชฉํ˜•) promotion_last_5years: ์ตœ๊ทผ 5๋…„๊ฐ„ ์Šน์ง„ ์—ฌ๋ถ€(0: ์Šน์ง„ x, 1: ์Šน์ง„, ๋ช…๋ชฉํ˜•) sales : ๋ถ€์„œ salary : ์ž„๊ธˆ ์ˆ˜์ค€ A2. ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋ฐ Strings ํ™•์ธ 2. ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋ฐ Strings ํ™•์ธ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋Š” ํŒŒ์ผ์ด ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ๋ณต์‚ฌํ•œ ๋‹ค์Œ, ํŒŒ์ผ ๊ฒฝ๋กœ/ํŒŒ์ผ๋ช…. csv๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. HR = read.csv('D:/Drop box/DATA SET/HR_comma_sep.csv') # /๋กœ ๊ฒฝ๋กœ ๊ตฌ๋ถ„ HR = read.csv('D:\Drop box\DATA SET\\HR_comma_sep.csv') # \\๋กœ ๊ฒฝ๋กœ ๊ตฌ๋ถ„ ๋ฐ์ดํ„ฐ ํŒŒ์•…ํ•˜๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ๋‹ค์Œ์—๋Š” ๋‹น์—ฐํžˆ ๋ฐ์ดํ„ฐ๊ฐ€ ์ œ๋Œ€๋กœ ๋ถˆ๋Ÿฌ์™€์กŒ๋Š”์ง€ ํ™•์ธ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ช…๋ น์–ด๋Š” head(), str(), summary()๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋‹ด์œผ๋กœ ์ด ์ค‘์—์„œ ์ œ๊ฐ€ ๊ฐœ์ธ์ ์œผ๋กœ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด๋Š” str()์ž…๋‹ˆ๋‹ค. head(), ๋ฐ์ดํ„ฐ ์œ—๋ถ€๋ถ„์„ ์ถœ๋ ฅํ•˜๋Š” ๋ช…๋ น์–ด # ๋ฐ์ดํ„ฐ ์œ—๋ถ€๋ถ„ ๋„์šฐ๊ธฐ head(HR, n = 3) satisfaction_level last_evaluation number_project average_montly_hours 1 0.38 0.53 2 157 2 0.80 0.86 5 262 3 0.11 0.88 7 272 time_spend_company Work_accident left promotion_last_5years sales salary 1 3 0 1 0 sales low 2 6 0 1 0 sales medium 3 4 0 1 0 sales medium str(), ๋ฐ์ดํ„ฐ์˜ shape, strings ํŒŒ์•… # ๋ฐ์ดํ„ฐ strings ํŒŒ์•… str(HR) 'data.frame': 14999 obs. of 10 variables: $ satisfaction_level : num 0.38 0.8 0.11 0.72 0.37 0.41 0.1 0.92 0.89 0.42 ... $ last_evaluation : num 0.53 0.86 0.88 0.87 0.52 0.5 0.77 0.85 1 0.53 ... $ number_project : int 2 5 7 5 2 2 6 5 5 2 ... $ average_montly_hours : int 157 262 272 223 159 153 247 259 224 142 ... $ time_spend_company : int 3 6 4 5 3 3 4 5 5 3 ... $ Work_accident : int 0 0 0 0 0 0 0 0 0 0 ... $ left : int 1 1 1 1 1 1 1 1 1 1 ... $ promotion_last_5years: int 0 0 0 0 0 0 0 0 0 0 ... $ sales : Factor w/ 10 levels "accounting","hr",..: 8 8 8 8 8 8 8 8 8 8 ... $ salary : Factor w/ 3 levels "high","low","medium": 2 3 3 2 2 2 2 2 2 2 ... ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๊ฐ ๋ณ€์ˆ˜๋“ค์ด ์–ด๋–ค strings๋ฅผ ๊ฐ€์ง€๋Š”์ง€ ํ™•์ธ์„ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. num, int, Factor ๋“ฑ ์–ด๋–ค ๋ณ€์ˆ˜๋“ค์ด ์–ด๋–ค strings๋กœ ์ €์žฅ์ด ๋˜์—ˆ๋Š”์ง€๋Š” R์ด ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์ธ์‹ํ•˜๊ณ  ์žˆ๋Š”๊ฐ€์™€ ๊ฐ™์€ ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์š”์•ฝํ•ด์„œ ๋ณด๊ธฐ # ์š”์•ฝ๋œ ๋ฐ์ดํ„ฐ ์‚ดํŽด๋ณด๊ธฐ summary(HR) # ์š”์•ฝ๋œ ๋ฐ์ดํ„ฐ ์‚ดํŽด๋ณด๊ธฐ satisfaction_level last_evaluation number_project average_montly_hours Min. :0.0900 Min. :0.3600 Min. :2.000 Min. : 96.0 1st Qu.:0.4400 1st Qu.:0.5600 1st Qu.:3.000 1st Qu.:156.0 Median :0.6400 Median :0.7200 Median :4.000 Median :200.0 Mean :0.6128 Mean :0.7161 Mean :3.803 Mean :201.1 3rd Qu.:0.8200 3rd Qu.:0.8700 3rd Qu.:5.000 3rd Qu.:245.0 Max. :1.0000 Max. :1.0000 Max. :7.000 Max. :310.0 time_spend_company Work_accident left Min. : 2.000 Min. :0.0000 Min. :0.0000 1st Qu.: 3.000 1st Qu.:0.0000 1st Qu.:0.0000 Median : 3.000 Median :0.0000 Median :0.0000 Mean : 3.498 Mean :0.1446 Mean :0.2381 3rd Qu.: 4.000 3rd Qu.:0.0000 3rd Qu.:0.0000 Max. :10.000 Max. :1.0000 Max. :1.0000 promotion_last_5years sales salary Min. :0.00000 sales :4140 high :1237 1st Qu.:0.00000 technical :2720 low :7316 Median :0.00000 support :2229 medium:6446 Mean :0.02127 IT :1227 3rd Qu.:0.00000 product_mng: 902 Max. :1.00000 marketing : 858 (Other) :2923 summary(DataSet)์€ ๋ฐ์ดํ„ฐ์— ํฌํ•จ๋œ ๋ชจ๋“  ๋ณ€์ˆ˜๋“ค์— ๋Œ€ํ•œ ์š” ์•ฝ ๊ฐ’์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. num ๋ฐ int๋Š” ๋ณ€์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’, ํ‰๊ท , ์ค‘์œ„์ˆ˜ ๋“ฑ์„ ๋‚˜ํƒ€๋‚ด๊ณ , Factor ํ˜•ํƒœ๋Š” ๊ฐœ์ˆ˜๋ฅผ ์ง‘๊ณ„ ๋‚ด์ฃผ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ strings ๋ณ€๊ฒฝ summary(HR$left) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.0000 0.0000 0.0000 0.2381 0.0000 1.0000 Work_accident, left, promotion_last_5years๋Š” ๋ช…๋ชฉํ˜• ๋ณ€์ˆ˜๋ผ R์—์„œ Factor ํ˜•ํƒœ๋กœ ๋“ค์–ด์™€์•ผ ํ•˜์ง€๋งŒ, R์—์„œ๋Š” ์•„์ง ํ•ด๋‹น ๋ณ€์ˆ˜๋“ค์„ integer๋กœ ์ธ์‹ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฑด ์›๋ž˜ ๋ฐ์ดํ„ฐ์˜ ํŠน์„ฑ์ด ์•„์ง R์— ๋ฐ˜์˜์ด ๋˜์ง€ ์•Š์€ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— strings ๋ณ€ํ™˜์„ ํ†ตํ•ด ์•Œ๋งž๊ฒŒ ๋ณ€ํ™˜์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. HR$Work_accident=as.factor(HR$Work_accident) HR$left=as.factor(HR$left) HR$promotion_last_5years=as.factor(HR$promotion_last_5years) summary(HR$left) 0 1 11428 3571 left ๋ณ€์ˆ˜๊ฐ€ numeric์œผ๋กœ ๋˜์–ด ์žˆ์„ ๋•Œ์™€ Factor๋กœ ๋˜์–ด ์žˆ์„ ๋•Œ, ์š” ์•ฝ ๊ฐ’์ด ๋‹ค๋ฅด๊ฒŒ ํ‘œ์‹œ๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋ช…๋ชฉํ˜•, ์ˆœ์„œํ˜•, ์—ฐ์†ํ˜•์ธ์ง€ ์ œ๋Œ€๋กœ ํŒŒ์•…์„ ํ•ด์•ผ ํ•˜๋Š” ์ž‘์—…์€ ์•„๋ฌด๋ฆฌ ๊ฐ•์กฐํ•ด๋„ ๋ชจ์ž๋ž๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ํŠน์„ฑ์€ ํ›„์— ๋ถ„์„ ๋ฐฉ๋ฒ•๋ก ์„ ์ •ํ•ด์ฃผ๋ฉฐ, ์ž˜๋ชป๋œ ๋ณ€์ˆ˜ ์ฒ™๋„์˜ ์ดํ•ด๋Š” ์ž˜๋ชป๋œ ๋ถ„์„์˜ ์‹œ์ž‘์ด ๋ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ์ฒ™๋„์— ๋Œ€ํ•œ ํ™•์ธ์ด ๋๋‚œ ๋‹ค์Œ์—๋Š” ๋ณ€์ˆ˜์˜ ๋ถ„ํฌ๋ฅผ ์ง์ ‘ ํ™•์ธ์„ ํ•ด๋ณด๋Š” ์ž‘์—…์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ๋ถ„ํฌ ํ™•์ธ์ด ์ค‘์š”ํ•œ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋จผ์ €, ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ณ€์ˆ˜์˜ ๋ถ„ํฌ๋ฅผ ๋ณด๊ณ  ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง ๋ฐฉํ–ฅ์„ ์„ค์ •ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์•ž์œผ๋กœ ๋‹ค๋ฃจ๊ฒŒ ๋  ๋ถ„์„์—์„œ ์“ฐ์ด๋Š” ์„ ํ˜•๋ชจํ˜•๋“ค์€ ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๋ถ„ํฌ๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๋ถ„ํฌ๊ฐ€ ๊ฐ€์ •๋˜์–ด ์žˆ๋Š” ๋ถ„ํฌ์™€ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ๋ณ€ํ™˜์„ ํ†ตํ•ด ๋ถ„ํฌ๋ฅผ ๋งž์ถ”์–ด ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ถ„ํฌ๋ฅผ ๊ผผ๊ผผํžˆ ํŒŒ์•…ํ•ด์•ผ ๋ฐ์ดํ„ฐ์—์„œ ์ธ์‚ฌ์ดํŠธ๋ฅผ ๋ฐœ๊ตดํ•ด ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜๋ฏธ ์—†๋Š” ๋ถ„ํฌ๋ฅผ ๊ฐ€์ง„ ๋ณ€์ˆ˜๋Š” ๊ณผ๊ฐํ•˜๊ฒŒ ๋ฒ„๋ฆฌ๊ณ , ์˜๋ฏธ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๋Š” ๋ฐ์— ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๋Š” ์—ด์‡ ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. A3. ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ ๊ฐ€๊ณตํ•˜๊ธฐ 3. ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ ๊ฐ€๊ณตํ•˜๊ธฐ ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง์€ ๋ถ„์„์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์„ ์ฐจ์ง€ํ•˜๋ฉฐ, ์‹ค์ œ ๋ถ„์„์— ๋“ค์ด๋Š” ์‹œ๊ฐ„์˜ 80 ~ 90%๋Š” ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง์ด ์ฐจ์ง€ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๋จผ์ €, ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง์„ ํ•  ๋•Œ ํ•„์ˆ˜๋กœ ์•Œ์•„์•ผ ํ•˜๋Š” ๋‚ด์šฉ์— ๋Œ€ํ•ด ์„ค๋ช…๋“œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ Raw ๋ฐ์ดํ„ฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ Raw ๋ฐ์ดํ„ฐ์—์„œ ๋ฐ”๋กœ ๋ชจ๋ธ๋ง์„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค. Raw ๋ฐ์ดํ„ฐ๋กœ ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๋‹ค, ์ค‘๊ฐ„์— ๋ถ„์„์ด ํ‹€์–ด์กŒ์„ ๊ฒฝ์šฐ ๋‹ค์‹œ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ ์ด ์กด์žฌํ•˜๋ฉฐ, ์ด๋Š” ์ƒ๊ฐ๋ณด๋‹ค ๋งค์šฐ ํฐ ์‹œ๊ฐ„์„ ์†Œ๋ชจํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฝ‘์•„์•ผ ๋  ๋•Œ๋„ ์žˆ๊ณ , ์—ฐ์†ํ˜• ๋ณ€์ˆ˜๋ฅผ ์ด์‚ฐํ˜• ๋ณ€์ˆ˜๋กœ ๋ฌถ์–ด์ค˜์•ผ ํ•  ๋•Œ๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ชฉ์ , ์ •๋ณด์— ๋”ฐ๋ผ ๊ณผ๊ฐํ•˜๊ฒŒ ์ˆ˜์ •์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ๊ฐ‡ํ˜€์žˆ์ง€ ์•Š์œผ๋ฉฐ, ์ฃผ์–ด์ง„ ์ •๋ณด๋ฅผ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ˜•์‹œํ‚ค๋Š” ๊ฒƒ, ๊ทธ๊ฒƒ์ด ๋ฐ์ดํ„ฐ ๋ถ„์„๊ฐ€์—๊ฒŒ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋Šฅ๋ ฅ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ๊ฑด์— ๋งž๋Š” ๊ฐ’ ํ• ๋‹นํ•˜๊ธฐ ifelse ifelse()๋Š” R์—์„œ ๋งค์šฐ ์“ฐ์ž„์ด ๋งŽ์€ ์กฐ๊ฑด ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. R์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—‘์…€์—์„œ์˜ ์“ฐ์ž„๊ณผ ๋งค์šฐ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. satisfaction_level์ด 0.5๋ณด๋‹ค ํฌ๋‹ค๋ฉด โ€˜Highโ€™, ํฌ์ง€ ์•Š๋‹ค๋ฉด โ€˜Lowโ€™ ๋ถ€์—ฌ. ifelse(์กฐ๊ฑด, TRUE, FALSE) HR$satisfaction_level_group_1 = ifelse(HR$satisfaction_level > 0.5, 'High', 'Low') HR$satisfaction_level_group_1 = as.factor(HR$satisfaction_level_group_1) summary(HR$satisfaction_level_group_1) High Low 10187 4812 satisfaction_level์ด 0.8๋ณด๋‹ค ํฌ๋‹ค๋ฉด โ€˜Highโ€™, 0.5 ~ 0.8์ด๋ฉด โ€˜Midโ€™, ๋‚˜๋จธ์ง€๋Š” โ€˜Lowโ€™ ์กฐ๊ฑด์ด ์ถ”๊ฐ€๊ฐ€ ๋˜์—ˆ์„ ๊ฒฝ์šฐ์—๋Š” ifelse(ifelse()) ํ˜•ํƒœ๋กœ ifelse ์•ˆ์— ifelse ๋ฌธ์„ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. HR$satisfaction_level_group_2 = ifelse(HR$satisfaction_level > 0.8, 'High', ifelse(HR$satisfaction_level > 0.5, 'Mid','Low')) HR$satisfaction_level_group_2 = as.factor(HR$satisfaction_level_group_2) summary(HR$satisfaction_level_group_2) High Low Mid 4002 4812 6185 ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๊ธฐ subset subset() ํ•จ์ˆ˜๋Š” ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. subset(๋ฐ์ดํ„ฐ, ์ถ”์ถœ ์กฐ๊ฑด) salary๊ฐ€ high์ธ ์ง์›๋“ค๋งŒ ์ถ”์ถœํ•˜์—ฌ HR_High๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ์…‹์„ ์ƒ์„ฑ HR_High = subset(HR, salary == 'high') summary(HR_High$salary) high low medium 1237 0 0 salary๊ฐ€ high์ด๋ฉด์„œ, sales๊ฐ€ IT์ธ ์ง์›๋“ค๋งŒ ์ถ”์ถœํ•˜์—ฌ HR_High_IT ์ƒ์„ฑ (๊ต์ง‘ํ•ฉ) HR_High_IT = subset(HR, salary == 'high' & sales == 'IT') print(xtabs(~ HR_High_IT$sales + HR_High_IT$salary)) HR_High_IT$salary HR_High_IT$sales high low medium accounting 0 0 0 hr 0 0 0 IT 83 0 0 management 0 0 0 marketing 0 0 0 product_mng 0 0 0 RandD 0 0 0 sales 0 0 0 support 0 0 0 technical 0 0 0 salary๊ฐ€ high์ด๊ฑฐ๋‚˜, sales๊ฐ€ IT์ธ ์ง์›๋“ค์„ ์ถ”์ถœํ•˜์—ฌ HR_High_IT2 ์ƒ์„ฑ (ํ•ฉ์ง‘ํ•ฉ) HR_High_IT2 = subset(HR, salary == 'high' | sales == 'IT') print(xtabs(~ HR_High_IT2$sales + HR_High_IT2$salary)) HR_High_IT2$salary HR_High_IT2$sales high low medium accounting 74 0 0 hr 45 0 0 IT 83 609 535 management 225 0 0 marketing 80 0 0 product_mng 68 0 0 RandD 51 0 0 sales 269 0 0 support 141 0 0 technical 201 0 0 A4. ์กฐ๊ฑด์— ๋งž๋Š” ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ ๋งŒ๋“ค๊ธฐ 4. ์กฐ๊ฑด์— ๋งž๋Š” ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ ๋งŒ๋“ค๊ธฐ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ƒˆ๋กœ์šด ์ง‘๊ณ„๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๋ถ„์„ํ•  ๋•Œ ๋งค์šฐ ์ค‘์š”ํ•œ ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค. ์—‘์…€์„ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๋ถ„๋“ค์ด๋ผ๋ฉด, ํ”ผ๋ฒ—ํ…Œ์ด๋ธ”์„ ์ž์ฃผ ๋งŒ๋“œ์‹ค ๊ฒ๋‹ˆ๋‹ค. R์—์„œ๋Š” ๋น„์Šทํ•œ ๊ธฐ๋Šฅ์„ โ€˜plyrโ€™ ํŒจํ‚ค์ง€๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒจํ‚ค์ง€ ์„ค์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ํŽธํ•˜๊ฒŒ ์ง‘๊ณ„ ๋‚ด๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ์˜ ํŒจํ‚ค์ง€๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. install.packages("plyr") library(plyr) ddply๋ฅผ ํ™œ์šฉํ•œ ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ ๋งŒ๋“ค๊ธฐ ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ์ง€๊ธˆ์€ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ddply()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด์˜ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ถ„์„ํ•  ๋ฐ์ดํ„ฐ ์„ค์ • ์ง‘๊ณ„ ๊ธฐ์ค€ ๋ณ€์ˆ˜ ์„ค์ • ์ง‘๊ณ„ ๊ฐ’์„ ์ €์žฅํ•  ์นผ๋Ÿผ๋ช… ๋ฐ ๊ณ„์‚ฐ ํ•จ์ˆ˜ ์„ค์ • ddply(๋ฐ์ดํ„ฐ, ์ง‘๊ณ„ ๊ธฐ์ค€, summarise, ์š”์•ฝ ๋ณ€์ˆ˜) SS=ddply(HR, # ๋ถ„์„ํ•  Data Set ์„ค์ • c("sales","salary"),summarise, # ์ง‘๊ณ„ ๊ธฐ์ค€ ๋ณ€์ˆ˜ ์„ค์ • M_SF = mean(satisfaction_level), # ์นผ๋Ÿผ๋ช… ๋ฐ ๊ณ„์‚ฐ ํ•จ์ˆ˜ ์„ค์ • COUNT =length(sales), M_WH = round(mean(average_montly_hours),2)) ๋ช…๋ น์–ด๊ฐ€ ์ œ๋Œ€๋กœ ์‹คํ–‰๋๋‹ค๋ฉด, SS๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A5. ggplot2 ๊ธฐ๋ณธ ์‹œ๊ฐํ™” 5. ggplot2 ๊ธฐ๋ณธ ์‹œ๊ฐํ™” ggplot2 ํŒจํ‚ค์ง€๋Š” R, python์—์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ์‰ฝ๊ฒŒ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ๋ฐฉ์‹์€ ๋Œ€๋ถ€๋ถ„ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ข€ ๊ทธ๋ฆฌ๋‹ค ๋ณด๋ฉด, ์›ํ•˜์‹œ๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ์ž์œ ์ž์žฌ๋กœ ๊ทธ๋ฆด ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ggplot2 ๊ธฐ๋ณธ ๋ฌธ๋ฒ•๊ตฌ์กฐ ggplot2๋ฅผ ์ด์šฉํ•ด ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ์ผ์€ ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ๊ณผ์ •๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์†์œผ๋กœ ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์„ ๋”ฐ๋ฅผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ถ•์„ ๊ทธ๋ฆฐ๋‹ค. ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฐ๋‹ค. ๋ฒ”๋ก€, ์ œ๋ชฉ, ๊ธ€์”จ ๋“ฑ ๊ธฐํƒ€ ์˜ต์…˜์„ ์ˆ˜์ •ํ•œ๋‹ค. ggplot2 ์—ญ์‹œ ๋งˆ์ฐฌ๊ฐ€์ง€๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ถ•์„ ๊ทธ๋ฆฐ๋‹ค โ†’ ggplot(๋ฐ์ดํ„ฐ๋ช…,aes(x=๋ณ€์ˆ˜1,y=๋ณ€์ˆ˜2)) (x์ถ•, y ์ถ•์„ ์ •ํ•ด์ค€๋‹ค.) ggplot์€ ggplot2์˜ ์‹œ์ž‘ ๋ช…๋ น์–ด์ด๋ฉฐ, ์—ฌ๊ธฐ์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ๋ฐ์ดํ„ฐ์™€, ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. aes๋Š” aesthetic์˜ ์•ฝ์ž๋กœ, ๊ทธ๋ž˜ํ”„์— ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด ์ค„ ๋•Œ๋Š” ๋ฌด์กฐ๊ฑด aes ์•ˆ์— ๋“ค์–ด๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฐ๋‹ค geom_bar( ) , ๋ง‰๋Œ€๋„ํ‘œ๋ฅผ ๊ทธ๋ฆฌ๊ฒ ๋‹ค. geom_histogram( ) , ํžˆ์Šคํ† ๊ทธ๋žจ์„ ๊ทธ๋ฆฌ๊ฒ ๋‹ค. geom_boxplot( ), ๋ฐ•์Šค ํ”Œ๋กฏ์„ ๊ทธ๋ฆฌ๊ฒ ๋‹ค. geom_line( ), ์„  ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๊ฒ ๋‹ค. ๊ธฐํƒ€ ์˜ต์…˜์„ ์ˆ˜์ •ํ•˜์—ฌ, ๊ทธ๋ž˜ํ”„๋ฅผ ์ •๊ตํ•˜๊ฒŒ ๊ทธ๋ฆฐ๋‹ค. labs( ) , ๋ฒ”๋ก€ ์ œ๋ชฉ ์ˆ˜์ • ggtitle( ), ์ œ๋ชฉ ์ˆ˜์ • xlabs( ), ylabs( ), x์ถ• y ์ถ• ์ด๋ฆ„ ์ˆ˜์ • ๋‹ค์Œ ์˜ˆ์‹œ์—์„œ ์–ด๋–ป๊ฒŒ ๊ทธ๋ฆฌ๋Š”์ง€ ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. library(ggplot2) # ํŒจํ‚ค์ง€ ๋ถ€์ฐฉ library(ggthemes) HR$salary = factor(HR$salary, levels = c("low","medium","high")) ggplot(HR) ggplot(HR, aes(x = salary)) ๋จผ์ €, ggplot๋งŒ ์ž…๋ ฅ์„ ํ•ด๋ณด๋ฉด, plot ์ฐฝ์—์„œ ํšŒ์ƒ‰ ๋ฐ”ํƒ• ๋ฉด์ด ์ƒ๊ธฐ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ aes()์™€ ํ•จ๊ป˜ ์ถ•์„ค์ •์„ ํ•ด์ฃผ๋ฉด, x์ถ•์— ๋ณ€์ˆ˜๋ช…์ด ์ƒ๊ธฐ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ aes()์ž…๋‹ˆ๋‹ค. aesthetic(๋ฏธ์ )์˜ ์ค„์ž„๋ง๋กœ, ggplot2์—์„œ ๊ฐ€์žฅ ํ•ต์‹ฌ์ ์ธ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. aes์˜ ์šฉ๋„๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ggplot2์— ๋“ค์–ด๊ฐˆ โ€™๋ณ€์ˆ˜โ€™๋“ค์€ ๋ชจ๋‘ aes() ์•ˆ์— ๋“ค์–ด๊ฐ€๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฐ’์€ aes()์— ์„ค์ •๊ฐ’์œผ๋กœ ์ž…๋ ฅ๋  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๊ณ  ํŒ๋‹จํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ggplot(HR, aes(x=salary)) + geom_bar() ggplot(HR, aes(x=salary)) + geom_bar(fill = 'royalblue') ggplot(HR, aes(x=salary)) + geom_bar(aes(fill=salary)) ggplot()์œผ๋กœ ๋งŒ๋“  ๋ฐ”ํƒ•์ƒ‰์— + geom_bar()์„ ํ•ด์ฃผ๋ฉด ๋ฐ”ํƒ•์ƒ‰ ์œ„์— ๋ง‰๋Œ€๋„ํ‘œ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์—ฌ๊ธฐ์„œ ๋ง‰๋Œ€๋„ํ‘œ์— ์ƒ‰์„ ์ฃผ๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋ง‰๋Œ€๋„ํ‘œ์— โ€™royalblueโ€™๋ผ๋Š” ์ƒ‰์„ ์ฑ„์›Œ์ฃผ๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๊ฐ„๋‹จํ•˜๊ฒŒ fill = โ€™royalblueโ€™๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ๋ฉด, ๋ง‰๋Œ€๋„ํ‘œ๊ฐ€ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ์ฑ„์›Œ์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ง‰๋Œ€ ๋„ํ‘œ์˜ ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ์ƒ‰์„ ๋‹ค๋ฅด๊ฒŒ ์ฃผ๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ, โ€™aes(fill = salary)โ€™๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฐจ์ด์ ์„ ์•„์‹œ๊ฒ ๋‚˜์š”? royalblue๋Š” ๊ทธ๋ƒฅ ์ผ๋ฐ˜์ ์ธ ์„ค์ •๊ฐ’์ด์ง€๋งŒ, salary๋Š” ์‹œ๊ฐํ™” ๋Œ€์ƒ ๋ฐ์ดํ„ฐ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ณ€์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— aes() ์•ˆ์— ๋“ค์–ด๊ฐ€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ฐจ์ด๋งŒ ์ž˜ ์ดํ•ดํ•˜์‹œ๋ฉด ggplot2๋ฅผ ํ™œ์šฉํ•˜๋Š”๋ฐ ํฐ ๋ฌธ์ œ๋Š” ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ggplot2๋Š” ์ด๋ ‡๊ฒŒ ํ•˜๋‚˜์”ฉ ๊ทธ๋ฆฌ๊ณ  ์‹ถ์€ ์กฐ๊ฑด๋“ค์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด์„œ ๊ทธ๋ฆฌ๋ฉด ํŽธํ•˜๊ฒŒ ๊ทธ๋ฆด ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ง‰๋Œ€ ๋„ํ‘œ(Bar plot) ๋ง‰๋Œ€ ๋„ํ‘œ : ์ด์‚ฐํ˜• ๋ณ€์ˆ˜ ํ•˜๋‚˜๋ฅผ ์ง‘๊ณ„ ๋‚ด๋Š” ๊ทธ๋ž˜ํ”„, 1์ฐจ์› ์ž์ฃผ ๋ดค์„ ๋ง‰๋Œ€ ๋„ํ‘œ์ž…๋‹ˆ๋‹ค. ๋ง‰๋Œ€ ๋„ํ‘œ๋Š” ์ด์‚ฐํ˜• ๋ณ€์ˆ˜๋ฅผ x์ถ•์œผ๋กœ ๋‘๊ณ , y ์ถ•์€ ์ž๋™์œผ๋กœ ์ง‘๊ณ„(Counting) ๋œ ๊ฐ’๋“ค์„ ํ‘œํ˜„ํ•˜๋Š” ๋„ํ‘œ์ž…๋‹ˆ๋‹ค. library(ggplot2) # ํŒจํ‚ค์ง€ ๋ถ€์ฐฉ x์ถ•์— salary ์„ค์ •, y ์ถ•์€ ์ž๋™์œผ๋กœ ์ง‘๊ณ„๋œ ๊ฐ’์ด ํ‘œํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์— ์„ค์ •์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. # ์ƒ‰ ์„ค์ •_1 ggplot(HR, aes(x=salary)) + geom_bar(fill = 'royalblue') # royalblue ์ƒ‰์œผ๋กœ ์ƒ‰ ์ฑ„์šฐ๊ธฐ # ์ƒ‰ ์„ค์ •_2 ggplot(HR, aes(x=salary)) + geom_bar(aes(fill=left)) # left ๊ฐ’์— ๋”ฐ๋ผ ์ƒ‰ ์ฑ„์šฐ๊ธฐ ggplot2์—์„œ๋Š” col = , fill = ์˜ต์…˜์„ ์คŒ์œผ๋กœ์จ ๊ทธ๋ž˜ํ”„์— ์ƒ‰์„ ๋”ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ , ์„ ๊ณผ์ฒ˜๋Ÿผ ๋ฉด์ ์ด ์—†๋Š” ๊ทธ๋ž˜ํ”„๋Š” col ์˜ต์…˜์„ ํ†ตํ•ด ์ƒ‰์„ ๋ฐ”๊ฟ”์ฃผ๋ฉฐ, ๋ฉด์ ์ด ์žˆ๋Š” ๊ทธ๋ž˜ํ”„๋“ค์€ fill ์˜ต์…˜์„ ํ†ตํ•ด ์ƒ‰์„ ๋ณ€๊ฒฝํ•ด ์ค๋‹ˆ๋‹ค. ํžˆ์Šคํ† ๊ทธ๋žจ(Histogram) ํžˆ์Šคํ† ๊ทธ๋žจ : ์—ฐ์†ํ˜• ๋ณ€์ˆ˜ ํ•˜๋‚˜๋ฅผ ์ง‘๊ณ„ ๋‚ด๋Š” ๊ทธ๋ž˜ํ”„, 1์ฐจ์› ํžˆ์Šคํ† ๊ทธ๋žจ์€ ์—ฐ์†ํ˜• ๋ณ€์ˆ˜๋ฅผ ์ผ์ • ๋ฒ”์œ„๋กœ ๊ตฌ๊ฐ„์„ ๋งŒ๋“ค์–ด, x์ถ•์œผ๋กœ ์„ค์ •ํ•˜๊ณ  y ์ถ•์€ ์ง‘๊ณ„๋œ ๊ฐ’(Counting)์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. # ๊ธฐ๋ณธ ggplot(HR, aes(x=satisfaction_level))+ geom_histogram() # ๊ตฌ๊ฐ„ ์ˆ˜์ • ๋ฐ ์ƒ‰ ์ž…ํžˆ๊ธฐ ggplot(HR, aes(x=satisfaction_level))+ geom_histogram(binwidth = 0.01, col='red',fill='royalblue') col์€ ํ…Œ๋‘๋ฆฌ ์ƒ‰์„, fill์€ ์ฑ„์›Œ์ง€๋Š” ์ƒ‰์„ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. ๋ฐ€๋„ ๊ทธ๋ž˜ํ”„(Density Plot) ๋ฐ€๋„ ๊ทธ๋ž˜ํ”„ : ์—ฐ์†ํ˜• ๋ณ€์ˆ˜ ํ•˜๋‚˜๋ฅผ ์ง‘๊ณ„ ๋‚ด๋Š” ๊ทธ๋ž˜ํ”„, 1์ฐจ์› ๋ฐ€๋„ ๊ทธ๋ž˜ํ”„๋Š” ์—ฐ์†ํ˜• ๋ณ€์ˆ˜๋ฅผ ์ผ์ • ๋ฒ”์œ„๋กœ ๊ตฌ๊ฐ„์„ ๋งŒ๋“ค์–ด, x์ถ•์œผ๋กœ ์„ค์ •ํ•˜๊ณ  y ์ถ•์€ ์ง‘๊ณ„๋œ ๊ฐ’(percentage)์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. # ๊ธฐ๋ณธ ggplot(HR, aes(x=satisfaction_level))+ geom_density() # ์ƒ‰ ์ž…ํžˆ๊ธฐ ggplot(HR, aes(x=satisfaction_level))+ geom_density(col='red',fill='royalblue') # col์€ ํ…Œ๋‘๋ฆฌ, fill์€ ์ฑ„์šฐ๊ธฐ ๋ฐ•์Šค ํ”Œ๋กฏ ๋ฐ•์Šค ํ”Œ๋กฏ(Boxplot) ๋ฐ•์Šค ํ”Œ๋กฏ : ์ด์‚ฐํ˜• ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ์—ฐ์†ํ˜• ๋ณ€์ˆ˜์˜ ๋ถ„ํฌ ์ฐจ์ด๋ฅผ ํ‘œํ˜„ํ•ด ์ฃผ๋Š” 2์ฐจ์› ๊ทธ๋ž˜ํ”„ ๋ฐ•์Šค ํ”Œ๋กฏ์€ ์ด์‚ฐํ˜• ๋ณ€์ˆ˜์™€ ์—ฐ์†ํ˜• ๋ณ€์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์— ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํƒ์ƒ‰๊ณผ์ •(EDA)์—์„œ ๋งค์šฐ ์ค‘์š”ํ•˜๊ฒŒ ์“ฐ์ด๋Š” ํ”Œ๋กฏ ์ค‘ ํ•˜๋‚˜์ด๋‹ˆ, ๊ผญ ๊ธฐ์–ตํ•ด๋‘์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. # ๊ธฐ๋ณธ ggplot(HR, aes(x=left, y=satisfaction_level)) + geom_boxplot(aes(fill = left)) + xlab("์ด์ง ์—ฌ๋ถ€") + ylab("๋งŒ์กฑ๋„") + ggtitle("Boxplot") + labs(fill = "์ด์ง ์—ฌ๋ถ€") alpha๋Š” ์ƒ‰ ๋ช…๋„ ์กฐ์ ˆ ๊ธฐ๋Šฅ์œผ๋กœ, 0 ~ 1 ์‚ฌ์ด ๊ฐ’์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. # ๊ธฐ๋ณธ์—์„œ ์กฐ๊ธˆ ๋” ์ถ”๊ฐ€ ggplot(HR, aes(x=left, y=satisfaction_level)) + geom_boxplot(aes(fill = left),alpha = I(0.4)) + geom_jitter(aes(col = left),alpha = I(0.4)) + xlab("์ด์ง ์—ฌ๋ถ€") + ylab("๋งŒ์กฑ๋„") + ggtitle("Boxplot") + labs(fill = "์ด์ง ์—ฌ๋ถ€", col = "์ด์ง ์—ฌ๋ถ€") # ๊ธฐ๋ณธ์—์„œ ์กฐ๊ธˆ ๋” ์ถ”๊ฐ€์—์„œ ์กฐ๊ธˆ ๋ณ€๊ฒฝ ggplot(HR, aes(x=left, y=satisfaction_level)) + geom_boxplot(aes(fill = salary),alpha = I(0.4),outlier.colour = 'red') + xlab("์ด์ง ์—ฌ๋ถ€") + ylab("๋งŒ์กฑ๋„") + ggtitle("Boxplot") + labs(fill = "์ž„๊ธˆ ์ˆ˜์ค€") ์‚ฐ์ ๋„(Scatter Plot) ์‚ฐ์ ๋„ : ๋‘ ์—ฐ์†ํ˜• ๋ณ€์ˆ˜์˜ ์ƒ๊ด€๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•ด ์ฃผ๋Š” 2์ฐจ์› ๊ทธ๋ž˜ํ”„ ์‚ฐ์ ๋„๋Š” ๋ถ„์„์—์„œ ๊ฐ€์žฅ ํ™œ๋ฐœํžˆ ์“ฐ์ด๋Š” ๊ทธ๋ž˜ํ”„ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ํŠนํžˆ ์‚ฐ์ ๋„์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ๋ง ์ „์— ๋ณ€์ˆ˜๋“ค ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๋ฐ ์žˆ์–ด, ๊ฐ€์žฅ ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค. # ๊ธฐ๋ณธ ggplot(HR, aes(x=average_montly_hours, y=satisfaction_level))+ geom_point() # ๊ฐ„๋‹จํ•œ ์ƒ‰์น ๋กœ ์ธ์‚ฌ์ดํŠธ ๋ฐœ๊ตดํ•˜๊ธฐ ggplot(HR, aes(x=average_montly_hours, y=satisfaction_level))+ geom_point(aes(col = left)) + labs(col = '์ด์ง ์—ฌ๋ถ€') + xlab("ํ‰๊ท  ๊ทผ๋ฌด์‹œ๊ฐ„") + ylab("๋งŒ์กฑ๋„") A6. ์—ฐ์Šต๋ฌธ์ œ 6. ์—ฐ์Šต๋ฌธ์ œ HR ๋ฐ์ดํ„ฐ์˜ ํ–‰์˜ ์ˆ˜, ์—ด์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜์‹œ์˜ค. salary ๋ณ€์ˆ˜์˜ strings์— ๋Œ€ํ•ด ๋‹ตํ•˜์‹œ์˜ค. salary ๋ณ€์ˆ˜์— ๋Œ€ํ•˜์—ฌ low๋Š” 1, medium์€ 2, high๋Š” 3์˜ ๊ฐ’์„ ๊ฐ€์ ธ ์„œ์—ด ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ฒŒ ํ•˜๋Š” salary_New ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ์‹œ์˜ค. Salary_New ๊ฐ’์ด 2์ด๋ฉด์„œ left๋Š” 1์ธ ์ง์›๋“ค๋งŒ ๋ฝ‘์•„ Medium_Left๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ๋งŒ๋“œ์‹œ์˜ค. Medium_Left ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด sales ๋ณ€์ˆ˜ ๋ณ„๋กœ time_spend_company์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ, Time_spend_Mean์ด Ch4. R ๊ธฐ๋ณธ ๋ฌธ๋ฒ• 3๋‹จ๊ณ„ ์ด๋ฒˆ Chapter์—์„œ๋Š” ์‰ฌ์–ด๊ฐ€๋Š” ๋Š๋‚Œ์œผ๋กœ, ๊ฐ„๋‹จํ•˜๊ฒŒ ํ†ต๊ณ„ ๊ฐ’(Statistics)์„ ๋ฝ‘์•„๋‚ด๋Š” ๋ฐฉ๋ฒ•๋“ค์„ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A1. ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์š”์•ฝ ๊ฐ’ ์‚ดํŽด๋ณด๊ธฐ 1. ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์š”์•ฝ ๊ฐ’ ์‚ดํŽด๋ณด๊ธฐ ๋ณ€์ˆ˜๊ฐ€ Factor ํ˜•ํƒœ์ผ ๋•Œ๋Š” ๊ฐ level(Low, Mid, High)์— ํ•ด๋‹นํ•˜๋Š” ์ง‘๊ณ„ Count๋ฅผ ๋‚˜ํƒ€๋‚ด์ฃผ๋ฉฐ, Numeric ํ˜•ํƒœ์ผ ๋•Œ๋Š” ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’, ํ‰๊ท  ๋ฐ ๊ฐ ๋ถ„์œ„์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด์ค๋‹ˆ๋‹ค. HR = read.csv('F:\Drop box\DATA SET\\HR_comma_sep.csv') summary(HR$salary) high low medium 1237 7316 6446 summary(HR$satisfaction_level) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.0900 0.4400 0.6400 0.6128 0.8200 1.0000 A2. ๋ถ„์œ„์ˆ˜ ๊ณ„์‚ฐ 2. ๋ถ„์œ„์ˆ˜ ๊ณ„์‚ฐ ๋ถ„์œ„์ˆ˜(quantile)์ด๋ž€ ๋ณ€์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ฆฌํ•˜์˜€์„ ๋•Œ, ํŠน์ • % ์œ„์น˜์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Q1์€ 1๋ถ„์œ„ ์ˆ˜๋กœ ํ•˜์œ„ 25%์— ํ•ด๋‹น๋˜๋Š” ์ง์›์˜ satisfaction_level ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด์ค๋‹ˆ๋‹ค. Median(์ค‘์œ„์ˆ˜)๋Š” ์ค‘๊ฐ„(50%)์— ํ•ด๋‹น๋˜๋Š” ์ง์›์˜ satisfaction_level์„ ๋‚˜ํƒ€๋‚ด์ฃผ๋ฉฐ, Q3( ํ•˜์œ„ 75%, ์ƒ์œ„ 25%)์˜ ๊ธฐ์ค€์— ํ•ด๋‹น๋˜๋Š” ์ง์›์˜ satisfaction_level์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. # 10%, 30%, 60%, 90%์— ํ•ด๋‹นํ•˜๋Š” ๋ถ„์œ„์ˆ˜ ๋ฝ‘์•„๋‚ด๊ธฐ quantile(HR$satisfaction_level, probs = c(0.1,0.3,0.6,0.9)) 10% 30% 60% 90% 0.21 0.49 0.72 0.92 A3. ํ•ฉ, ํ‰๊ท , ํ‘œ์ค€ํŽธ์ฐจ ๊ตฌํ•˜๊ธฐ 3. ํ•ฉ, ํ‰๊ท , ํ‘œ์ค€ํŽธ์ฐจ ๊ตฌํ•˜๊ธฐ ๋‹จ์ผ ๋ณ€์ˆ˜์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ sum(HR$satisfaction_level) [1] 9191.89 ๋‹จ์ผ ๋ณ€์ˆ˜์˜ ํ‰๊ท  ๊ตฌํ•˜๊ธฐ mean(HR$last_evaluation) [1] 0.7161017 ๋‹จ์ผ ๋ณ€์ˆ˜์˜ ํ‘œ์ค€ํŽธ์ฐจ ๊ตฌํ•˜๊ธฐ sd(HR$satisfaction_level) [1] 0.2486307 ๋‹ค์ค‘ ๋ณ€์ˆ˜์˜ ํ•ฉ, ํ‰๊ท  ๊ตฌํ•˜๊ธฐ obs(ํ–‰) ๋ณ„๋กœ ํ•ฉ, ํ‰๊ท  ๊ตฌํ•  ์‹œ์—๋Š” rowSums, rowMeans ํ™œ์šฉ colMeans(HR[1:5]) satisfaction_level last_evaluation number_project 0.6128335 0.7161017 3.8030535 average_montly_hours time_spend_company 201.0503367 3.4982332 colSums(HR[1:5]) satisfaction_level last_evaluation number_project 9191.89 10740.81 57042.00 average_montly_hours time_spend_company 3015554.00 52470.00 A4. ๋นˆ๋„ ํ…Œ์ด๋ธ” ์ž‘์„ฑํ•˜๊ธฐ 4. ๋นˆ๋„ ํ…Œ์ด๋ธ” ์ž‘์„ฑํ•˜๊ธฐ 1์ฐจ์› ๋นˆ๋„ ํ…Œ์ด๋ธ” TABLE = as.data.frame(table(HR$sales)) 2์ฐจ์› ํ…Œ์ด๋ธ” TABLE2 = as.data.frame(xtabs(~ HR$salary + HR$sales)) A5. ์—ฐ์Šต๋ฌธ์ œ 5. ์—ฐ์Šต๋ฌธ์ œ HR ๋ฐ์ดํ„ฐ์—์„œ last_evaluation์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์‹œ์˜ค. HR ๋ฐ์ดํ„ฐ์—์„œ last_evaluation์˜ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ตฌํ•˜์‹œ์˜ค. HR ๋ฐ์ดํ„ฐ์—์„œ sales์— ๋Œ€ํ•œ ๋นˆ๋„ํ‘œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค. HR ๋ฐ์ดํ„ฐ์—์„œ left, salary์— ๋Œ€ํ•œ ๊ต์ฐจ ํ‘œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค. Ch5. R ๊ธฐ๋ณธ ๋ฌธ๋ฒ• 4๋‹จ๊ณ„ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ด์ „์— ํ–ˆ๋˜ ๋ฐ์ดํ„ฐ์— ๋น„ํ•ด์„œ๋Š” ์ข€ ๋” ์–ด๋ ค์šด ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด R์„ ์ตํ˜€๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A1. ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋ฐ ๋ฐ์ดํ„ฐ ์„ค๋ช… 1. ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋ฐ ๋ฐ์ดํ„ฐ ์„ค๋ช… ๋ฐ์ดํ„ฐ ์ถœ์ฒ˜๋Š” Kaggle์ด๋ฉฐ, ํ•ด๋‹น ๋“œ๋กญ๋ฐ•์Šค ๋งํฌ์—์„œ ๋ฐ›์œผ์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งํฌ : https://www.drop box.com/sh/xx1w2syi768kfU0/AACZgxgo1fcxyDMgv9U-iTz8a?dl=0 # ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ IMDB = read.csv("D:\Drop box\DATA SET(Drop box)\IMDB-Movie-Data.csv") ๋ณ€์ˆ˜ ์„ค๋ช… Rank Title : ์˜ํ™” ์ œ๋ชฉ Genre : ์˜ํ™” ์žฅ๋ฅด Description : ์˜ํ™” ์„ค๋ช… Director : ๊ฐ๋…๋ช… Actors : ๋ฐฐ์šฐ Year : ์˜ํ™” ์ƒ์˜ ์—ฐ๋„ Runtime.. Minutes : ์ƒ์˜์‹œ๊ฐ„ Rating : Rating ์ ์ˆ˜ Votes : ๊ด€๊ฐ ์ˆ˜ Revenue.. Millions : ์ˆ˜์ต Metascore : ๋ฉ”ํƒ€ ์Šค์ฝ”์–ด A2. ๊ฒฐ์ธก์น˜(Missing Value) 2. ๊ฒฐ์ธก์น˜(Missing Value) ๊ฒฐ์ธก์น˜์— ๋Œ€ํ•œ ์ •์˜ ๊ฒฐ์ธก์น˜(Missing Value)๋Š” ๋ง ๊ทธ๋Œ€๋กœ ๋ฐ์ดํ„ฐ์— ๊ฐ’์ด ์—†๋Š” ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. ์ค„์—ฌ์„œ โ€™NAโ€™๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•˜๊ณ , ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋Š” Null ์ด๋ž€ ํ‘œํ˜„์„ ๋งŽ์ด ์”๋‹ˆ๋‹ค. ๊ฒฐ์ธก์น˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜๋Š” ๋ฐ์— ์žˆ์–ด์„œ ๋งค์šฐ ๋ฐฉํ•ด๊ฐ€ ๋˜๋Š” ์กด์žฌ์ž…๋‹ˆ๋‹ค. ๊ฒฐ์ธก์น˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ์•ผ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € ๊ฒฐ์ธก์น˜๋ฅผ ๋‹ค ์ œ๊ฑฐํ•ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ, ๊ฒฐ์ธก์น˜์˜ ๋น„์œจ์— ๋”ฐ๋ผ์„œ ๋ง‰๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์†์‹ค์„ ๋ถˆ๋Ÿฌ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ๋Š” ๊ฒฐ์ธก์น˜๋ฅผ ์ž˜๋ชป ๋Œ€์ฒดํ•  ๊ฒฝ์šฐ, ๋ฐ์ดํ„ฐ์—์„œ ํŽธํ–ฅ(bias)์ด ์ƒ๊ธธ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฒฐ์ธก์น˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ์— ์žˆ์–ด ๋ถ„์„๊ฐ€์˜ ๊ฒฌํ•ด๊ฐ€ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ˜์˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€์ฒด๊ฐ€ ์ž˜๋ชป๋  ๊ฒฝ์šฐ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ๋งค์šฐ ํ‹€์–ด์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๋ก ์€ ๊ฒฐ์ธก์น˜๋ฅผ ์ž์„ธํžˆ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‹œ๊ฐ„์ด ๋งŽ์ด ํˆฌ์ž๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌด์—‡๋ณด๋‹ค, ๋ฐ์ดํ„ฐ์— ๊ธฐ๋ฐ˜ํ•œ ๊ฒฐ์ธก์น˜ ์ฒ˜๋ฆฌ๊ฐ€ ์ง„ํ–‰๋˜์–ด์•ผ ๋ถ„์„์„ ์ •ํ™•ํ•˜๊ฒŒ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A3. R์„ ํ†ตํ•œ ๊ฒฐ์ธก์น˜ ์ฒ˜๋ฆฌ 3. R์„ ํ†ตํ•œ ๊ฒฐ์ธก์น˜ ์ฒ˜๋ฆฌ ๊ฒฐ์ธก์น˜ ํ™•์ธ # Metascore ๋ณ€์ˆ˜ ๋‚ด์—์„œ ๊ฒฐ์ธก์น˜ ๋…ผ๋ฆฌ๋ฌธ ํŒ๋‹จ (TRUE, FALSE) is.na(IMDB$Metascore)[1:20] [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE # Metascore ๋ณ€์ˆ˜ ๋‚ด์— ๊ฒฐ์ธก์น˜ ๊ฐœ์ˆ˜ sum(is.na(IMDB$Metascore)) [1] 64 # IMDB ๋‚ด ๋ชจ๋“  ๋ณ€์ˆ˜๋ณ„ ๊ฒฐ์ธก์น˜ ๊ฐœ์ˆ˜ colSums(is.na(IMDB)) Rank Title Genre 0 0 0 Description Director Actors 0 0 0 Year Runtime.. Minutes. Rating 0 0 0 Votes Revenue.. Millions. Metascore 0 128 64 ๊ฒฐ์ธก์น˜ ์ œ๊ฑฐ ์ „๋ถ€ ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ, ๊ฐ€์žฅ ๊ทน๋‹จ์ ์ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.(๊ฒฐ์ธก์น˜๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ํฌํ•จ๋œ obs(ํ–‰)์€ ์‚ญ์ œ) IMDB2 = na.omit(IMDB) colSums(is.na(IMDB2)) Rank Title Genre 0 0 0 Description Director Actors 0 0 0 Year Runtime.. Minutes. Rating 0 0 0 Votes Revenue.. Millions. Metascore 0 0 0 ํŠน์ • ๋ณ€์ˆ˜์— ๊ฒฐ์ธก์น˜๊ฐ€ ์กด์žฌํ•˜๋Š” ํ–‰๋งŒ ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ # 12๋ฒˆ์งธ ์—ด์— ๊ฒฐ์ธก์น˜๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์‚ญ์ œ IMDB3 = IMDB[complete.cases(IMDB[ ,12]),] colSums(is.na(IMDB3)) Rank Title Genre 0 0 0 Description Director Actors 0 0 0 Year Runtime.. Minutes. Rating 0 0 0 Votes Revenue.. Millions. Metascore 0 98 0 ๊ฒฐ์ธก์น˜๋ฅผ ํŠน์ • ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•  ๊ฒฝ์šฐ is.na๊ฐ€ True์ธ ๊ฐ’(๊ฒฐ์ธก์น˜)๋“ค์— ๋Œ€ํ•ด 58.99 ์ง€์ • # Rawdata๋ฅผ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜์— ๋ณต์‚ฌ IMDB$Metascore2 = IMDB$Metascore # ๊ฒฐ์ธก์น˜ ๋Œ€์ฒด IMDB$Metascore2[is.na(IMDB$Metascore2)]=58.99 ๊ฒฐ์ธก์น˜ ์ƒ๋žตํ•˜๊ณ  ๊ณ„์‚ฐํ•  ๊ฒฝ์šฐ mean(IMDB$Revenue.. Millions.) # NA ์ƒ์„ฑ [1] NA mean(IMDB$Revenue.. Millions.,na.rm = TRUE) # NA ์ƒ๋žตํ•˜๊ณ  ๊ณ„์‚ฐ [1] 82.95638 A4. ๊ฒฐ์ธก์น˜ ์ฒ˜๋ฆฌ ์‹œ ์ฃผ์˜ํ•  ์  4. ๊ฒฐ์ธก์น˜ ์ฒ˜๋ฆฌ ์‹œ ์ฃผ์˜ํ•  ์  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ, ๊ธฐ๋ณธ์ ์œผ๋กœ raw ๋ฐ์ดํ„ฐ๋Š” ์ ˆ๋Œ€ ๊ฑด๋“œ๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋ณดํ†ต์˜ ๊ฒฝ์šฐ, ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ ๋ณต์‚ฌ๋ฅผ ํ•ด๋‘๊ณ  ์ง„ํ–‰์„ ํ•˜๋Š” ๊ฒƒ์ด ๋‚˜์ค‘์„ ์œ„ํ•ด ๋งค์šฐ ์ข‹์€ ์Šต๊ด€์ด ๋ฉ๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” ๋‚˜๋จธ์ง€ ์ •๋ณด๋ฅผ ์žƒ์ง€ ์•Š๊ธฐ ์œ„ํ•˜์—ฌ ์ตœ๋Œ€ํ•œ ํ•ฉ๋ฆฌ์ ์œผ๋กœ ๊ฒฐ์ธก์น˜๋ฅผ ๋Œ€์ฒดํ•ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฐ์†ํ˜• ๋ณ€์ˆ˜ : ํ‰๊ท ์œผ๋กœ ๋Œ€์ฒด ์ด์‚ฐํ˜• ๋ณ€์ˆ˜ : ์ตœ๋นˆ๊ฐ’์œผ๋กœ ๋Œ€์ฒด ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋ฌดํ„ฑ๋Œ€๊ณ  ๊ฒฐ์ธก์น˜๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์‹ฌํ•˜๊ฒŒ ์ ๋ฆด ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ์ธก์น˜๋ฅผ ๋Œ€์ฒดํ•  ๋•Œ๋Š” ํ•ญ์ƒ ๋‹ค์Œ์˜ ์‚ฌํ•ญ๋“ค์„ ํ™•์ธํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ์ธก์น˜์˜ ๋น„์œจ ๋งŒ์•ฝ, ๊ฒฐ์ธก์น˜์˜ ๋น„์œจ์ด ์ƒ๋‹นํ•œ ๊ฒฝ์šฐ, ์ผ๋‹จ<NAME>๊ณ  ๋ณด๋Š” ๋ฐฉ์‹์€ ํฌ๋‚˜ํฐ ์ •๋ณด ์†์‹ค์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•  ๋•Œ, ํ•ญ์ƒ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ๋ฅผ ํ™•์ธํ•˜๊ณ  ์ง„ํ–‰์„ ํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ํ‰๊ท ์„ ์ค‘์‹ฌ์œผ๋กœ ๊ท ํ˜• ์žˆ๊ฒŒ ํผ์ ธ์žˆ๋Š” โ€™์ •๊ทœ๋ถ„ํฌโ€™ํ˜•ํƒœ๋ฅผ ๋ ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, ํ˜„์‹ค์˜ ๋Œ€๋ถ€๋ถ„ ๋ฐ์ดํ„ฐ๋Š” ๊ทธ๋ ‡๊ฒŒ ์ด์ƒ์ ์ด์ง€๋Š” ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๋ฉด์„œ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ณ€์ˆ˜์™€์˜ ๊ด€๊ณ„๊ฐ€ ์žˆ๋Š”์ง€ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์™€์˜ ๊ด€๊ณ„๋ฅผ ํŒŒ์•…ํ•˜์—ฌ, ๊ฒฐ์ธก์น˜๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ „๋ฌธ์ ์ธ ์šฉ์–ด๋กœ๋Š” โ€™Mssing value Imputationโ€™์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ํ•ด๋‹น ๋ฐฉ๋ฒ•๋ก ์€ ์ด ์ฑ…์˜ ์ˆ˜์ค€์„ ๋ฒ—์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฃจ์ง€ ์•Š๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A5. ๊ฒฐ์ธก์น˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ ํƒ์ƒ‰ 5. ๊ฒฐ์ธก์น˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ ํƒ์ƒ‰ ๊ฒฐ์ธก์น˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ, Revenue.. Millions.์˜ ๋ถ„ํฌ์— ๋Œ€ํ•ด ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. library(ggplot2) ggplot(IMDB, aes(x=Revenue.. Millions.)) + geom_histogram(fill='royalblue', alpha = 0.4) + ylab('') + xlab("Revenue_Millions") + theme_classic() ggplot(IMDB, aes(x = "",y=Revenue.. Millions.)) + geom_boxplot(fill='red', alpha = 0.4, outlier.color = 'red') + xlab('') + ylab("Revenue_Millions") + theme_classic() summary(IMDB$Revenue.. Millions.) Min. 1st Qu. Median Mean 3rd Qu. Max. NA's 0.00 13.27 47.98 82.96 113.72 936.63 128 Revenu_Millions๋ฅผ ๋ณผ ๋•Œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•œ์ชฝ์œผ๋กœ ๋งค์šฐ ์น˜์šฐ์ณ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ถ„ํฌ๋Š” ํ•ญ์ƒ ํ‰๊ท ๊ณผ ์ค‘์œ„ ์ˆ˜์˜ ์ฐจ์ด๋ฅผ ํ™•์ธํ•ด ๋ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (ํ‰๊ท : 82.96, ์ค‘์œ„์ˆ˜: 47.98) ๋”ฐ๋ฆฌ์„œ, ์ด๋Ÿฐ ๋ถ„ํฌ์˜ ๋ฐ์ดํ„ฐ์˜ ๊ฒฐ์ธก์น˜๋Š” ํ‰๊ท ์œผ๋กœ ๋Œ€์ฒดํ•˜๋ฉด ๋งค์šฐ ์œ„ํ—˜ํ•ฉ๋‹ˆ๋‹ค. ํ‰๊ท ์€ ๊ทน๋‹จ๊ฐ’(Outlier, ์ด์ƒ์น˜)์— ์˜ํ–ฅ์„ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทน๋‹จ๊ฐ’์€ ํŒจํ„ด์„ ๋ฒ—์–ด๋‚œ ํŠน์ˆ˜ํ•œ ์ƒํ™ฉ์ด์ง€, ๊ฒฐ์ฝ” ์ผ๋ฐ˜์ ์ธ ์ƒํ™ฉ์„ ๋Œ€๋ณ€ํ•ด ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ํ‰๊ท ๋ณด๋‹ค๋Š” ์ค‘์œ„์ˆ˜๊ฐ€ ์•ˆ์ „ํ•ฉ๋‹ˆ๋‹ค. ์ค‘์œ„์ˆ˜๋Š” ๊ทน๋‹จ๊ฐ’(Outlier)์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ค‘์œ„ ์ˆ˜๋กœ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ์€ ์ฐจ์„ ์ฑ…์ด์ง€ ์™„๋ฒฝํ•œ ๋ฐฉ๋ฒ•์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋“ค๊ณผ์˜ ๊ด€๊ณ„๋ฅผ ๋ณด๋ฉด์„œ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ์€ ๋งŽ์€ ์‹œ๊ฐ„์„ ์š”๊ตฌํ•˜์ง€๋งŒ, ๋” ์ •๊ตํ•œ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A6. ์ด์ƒ์น˜(Outlier) ๋ฝ‘์•„๋‚ด๊ธฐ 6. ์ด์ƒ์น˜(Outlier) ๋ฝ‘์•„๋‚ด๊ธฐ ์ด์ƒ์น˜(Outlier)๋Š” โ€˜ํŒจํ„ด์—์„œ ๋ฒ—์–ด๋‚œ ๊ฐ’โ€™์œผ๋กœ ์ •์˜๋ฅผ ๋‚ด๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” โ€™์ค‘์‹ฌ์—์„œ ์ข€ ๋งŽ์ด ๋–จ์–ด์ ธ ์žˆ๋Š” ๊ฐ’โ€™์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ƒ์น˜๋Š” ํ‰๊ท ์— ๋ง‰๋Œ€ํ•œ ์˜ํ–ฅ์„ ๋ฏธ์นฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด [1,2,3,4,5]์˜ ํ‰๊ท ์€ 3์ด์ง€๋งŒ, [1,2,3,4,100]์˜ ํ‰๊ท ์€ 22์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ค‘์œ„์ˆ˜๋Š” 3์œผ๋กœ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ข…์ข… ํ‰๊ท ์œผ๋กœ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๊ธฐ๋ณด๋‹ค๋Š” ์ค‘์œ„ ์ˆ˜๋กœ ์š” ์•ฝ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ด ๋” ํ˜„์‹ค์„ ๋ฐ˜์˜ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด์ƒ์น˜ ์—ฌ๋ถ€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ ๊ณ„์‚ฐ์„ ํ•ฉ๋‹ˆ๋‹ค. ggplot(IMDB, aes(x=as.factor(Year),y=Revenue.. Millions.))+ geom_boxplot(aes(fill=as.factor(Year)),outlier.colour = 'red',alpha=I(0.4))+ xlab("์—ฐ๋„") + ylab("์ˆ˜์ต") + guides(fill = FALSE) + theme_bw() + theme(axis.text.x = element_text(angle = 90)) ๋ฐ•์Šค ํ”Œ๋กฏ์„ ๊ทธ๋ฆฐ ์ด์œ ๋Š” ์ด์ƒ์น˜ ํƒ์ƒ‰์„ ๊ฐ€์žฅ ํ•˜๊ธฐ ์ข‹์€ ํ”Œ๋กฏ์ด ๋ฐ•์Šค ํ”Œ๋กฏ์ด ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฐ•์Šค ํ”Œ๋กฏ์˜ ๊ตฌ์„ฑ์„ ์‚ดํŽด๋ณด์‹œ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ƒ์ž ์•ˆ์— ๊ทธ๋ ค์ ธ ์žˆ๋Š” ์ง์„ ์€ ์ค‘์œ„์ˆ˜(Median)์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ƒ์ž์˜ ๋ฐ‘๋ณ€์€ 1๋ถ„ ์œ„์ˆ˜๋ฅผ ๋‚˜ํƒ€์• ๋ฉฐ, ์œ—๋ณ€์€ 3๋ถ„ ์œ„์ˆ˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ƒ์ž ํ…Œ๋‘๋ฆฌ ์™ธ๋ถ€์— ์ง์„ ์ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ง์„ ์€ ์šธํƒ€๋ฆฌ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ƒ์ž๋กœ๋ถ€ํ„ฐ ์•„๋ž˜ ์šธํƒ€๋ฆฌ์˜ ๊ณ„์‚ฐ์‹ : Q1 - 1.5 * (Q3 - Q1) ์ƒ์ž๋กœ๋ถ€ํ„ฐ ์œ„ ์šธํƒ€๋ฆฌ์˜ ๊ณ„์‚ฐ์‹ : Q3 + 1.5 *(Q3 - Q1) ์ด ์šธํƒ€๋ฆฌ๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฐ’๋“ค์„ Outlier๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. Outlier๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ†ต๊ณ„ ์ถ”์ •์— ์žˆ์–ด์„œ ๋ฐฉํ•ด๊ฐ€ ๋˜๊ณ ๋Š” ํ•ฉ๋‹ˆ๋‹ค. ํ†ต๊ณ„๋ถ„์„์€ ์ „๋ถ€ ๊ท€๋‚ฉ๋ฒ•์ธ๋ฐ, ์ด์ƒ์น˜ ๊ฐ™์€ ํŠน์ˆ˜ ์ผ€์ด์Šค๊ฐ€ ๊ทœ์น™์„ ๋งŒ๋“œ๋Š”๋ฐ ๋ฐฉํ•ด๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. Outlier์˜ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ• ์ œ๊ฑฐ๋ฅผ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์“ฐ์ด๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ๊ฐœ์ธ์ ์œผ๋กœ ์ข‹์•„ํ•˜๋Š” ๋ฐฉ์‹์€ ์•„๋‹™๋‹ˆ๋‹ค. ์–ด์ฐŒ ๋๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฒ„๋ฆฌ๋Š” ๊ฑฐ๋‹ˆ๊น์š”. ๋ฐ์ดํ„ฐ ๋ณ€ํ˜•์„ ํ†ตํ•ด Outlier ๋ฌธ์ œ๋ฅผ ์ค„์—ฌ์ค๋‹ˆ๋‹ค. ํ†ต๊ณ„ ์ถ”์ •์— ์„ธ๋Š” ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋งž์ถ”์–ด ์ฃผ๋Š” ๊ฒƒ์ด ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต Outlier๋กœ ์ธํ•ด ํ•œ ์ชฝ์œผ๋กœ ์น˜์šฐ์นœ ๋ถ„ํฌ๋Š” log ๋ณ€ํ™˜์„ ํ†ตํ•ด<NAME>์„ ๋งž์ถ”์–ด์ฃผ๊ณ ๋Š” ํ•ฉ๋‹ˆ๋‹ค. (์ด ๋ถ€๋ถ„์€ ํ›„์— ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.) # Outlier์ธ ๋ฐ์ดํ„ฐ ์ œ๊ฑฐํ•˜๊ธฐ # 1๋ถ„ ์œ„์ˆ˜ ๊ณ„์‚ฐ Q1 = quantile(IMDB$Revenue.. Millions.,probs = c(0.25),na.rm = TRUE) # 3๋ถ„ ์œ„์ˆ˜ ๊ณ„์‚ฐ Q3 = quantile(IMDB$Revenue.. Millions.,probs = c(0.75),na.rm = TRUE) LC = Q1 - 1.5 * (Q3 - Q1) # ์•„๋ž˜ ์šธํƒ€๋ฆฌ UC = Q3 + 1.5 * (Q3 - Q1) # ์œ„ ์šธํƒ€๋ฆฌ IMDB2 = subset(IMDB, Revenue.. Millions. > LC & Revenue.. Millions. < UC) A7. ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ 1ํŽธ 7. ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ 1ํŽธ ์ด๋ฒˆ์—๋Š” ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃฐ ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ˆ™์ง€ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•˜๋Š” ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ๋Œ€์ฒด : gsub() ๋ฌธ์ž์—ด ๋ถ„๋ฆฌ : strsplit() ๋ฌธ์ž์—ด ํ•ฉ์น˜๊ธฐ : paste() ๋ฌธ์ž์—ด ์ถ”์ถœ : substr() ํ…์ŠคํŠธ ๋งˆ์ด๋‹ ํ•จ์ˆ˜: Corpus() & tm_map(), & tdm() ๋ฌธ์ž์—ด ์ถ”์ถœ substr(IMDB$Actors[1],1,5) [1] "Chris" ์ฒซ ๋ฒˆ์งธ obs์˜ Actors ๋ณ€์ˆ˜์—์„œ 1 ~ 5๋ฒˆ์งธ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž์—ด ์ถ”์ถœ ๋ฌธ์ž์—ด ๋ถ™์ด๊ธฐ paste(IMDB$Actors[1],"_",'A') # ์ฒซ ๋ฒˆ์งธ obs์˜ Actors ๋ณ€์ˆ˜์—์„œ _ A ๋ถ™์ด๊ธฐ [1] "Chris Pratt, Vin Diesel, Bradley Cooper, Zoe Saldana _ A" paste(IMDB$Actors[1],"_",'A',sep="") # ๋„์–ด์“ฐ๊ธฐ ์—†์ด ๋ถ™์ด๊ธฐ [1] "Chris Pratt, Vin Diesel, Bradley Cooper, Zoe Saldana_A" paste(IMDB$Actors[1],"_","Example",sep="|") # |๋กœ ๋ถ™์ด๊ธฐ [1] "Chris Pratt, Vin Diesel, Bradley Cooper, Zoe Saldana|_|Example" paste()๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ถ™์ด๋Š” ๋ฌธ์ž์—ด ์‚ฌ์ด์— " โ€œ(ํ•œ ์นธ ๋นˆ์นธ)์ด ๊ธฐ๋ณธ ์„ค์ •์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” sep =โ€"์˜ต์…˜์„ ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ๋ถ„๋ฆฌ strsplit(as.character(IMDB$Actors[1]), split= ",") [[1]] [1] "Chris Pratt" " Vin Diesel" " Bradley Cooper" " Zoe Saldana" ๋ฌธ์ž์—ด ๋Œ€์ฒด IMDB$Genre2=gsub(","," ",IMDB$Genre) # ,๋ฅผ ๋„์–ด์“ฐ๊ธฐ๋กœ ๋Œ€์ฒด gsub์€ ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง์—์„œ ๋งค์šฐ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ช…๋ น์–ด์ด๊ธฐ์— ๊ผญ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. A8. ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ 2ํŽธ(R ํ…์ŠคํŠธ ๋งˆ์ด๋‹) 8. ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ 2ํŽธ ํ…์ŠคํŠธ ๋งˆ์ด๋‹ 1 ํ…์ŠคํŠธ ๋งˆ์ด๋‹์˜ ์ ˆ์ฐจ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฝ”ํผ์Šค(๋ง๋ญ‰์น˜) ์ƒ์„ฑ TDM(๋ฌธ์„œ ํ–‰๋ ฌ) ์ƒ์„ฑ ๋ฌธ์ž ์ฒ˜๋ฆฌ(ํŠน์ˆ˜๋ฌธ์ž ์ œ๊ฑฐ, ์กฐ์‚ฌ ์ œ๊ฑฐ, ์ˆซ์ž ์ œ๊ฑฐ ๋“ฑ..) ๋ฌธ์ž์—ด ๋ณ€์ˆ˜ ์ƒ์„ฑ Genre ๋ณ€์ˆ˜๋Š” ์˜ํ™”์— ๋Œ€ํ•œ ์žฅ๋ฅด๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋“  ์˜ํ™”๊ฐ€ ํ•˜๋‚˜์˜ ์žฅ๋ฅด์—๋งŒ ํ•ด๋‹น๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์—ฌ๋Ÿฌ ์žฅ๋ฅด์— ํ•ด๋‹น๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ ์˜ํ™”๊ฐ€ ์–ด๋Š ์žฅ๋ฅด์— ํ•ด๋‹น๋˜๋Š”์ง€ ๋‚˜ํƒ€๋‚ด์ค„ ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ํ…์ŠคํŠธ ๋งˆ์ด๋‹ ๊ธฐ๋ฒ•์„ ์ ์šฉ์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1๋‹จ๊ณ„ : ์ฝ”ํผ์Šค ์ƒ์„ฑ ์˜์–ด์˜ ๊ฒฝ์šฐ, ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅธ ๊ธ€์ž๋กœ ์ธ์‹๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๊ฟ”์ฃผ๋Š” ์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. library(tm) # tm ํŒจํ‚ค์ง€ ์„ค์น˜ ํ•„์š” CORPUS = Corpus(VectorSource(IMDB$Genre2)) # ์ฝ”ํผ์Šค ์ƒ์„ฑ CORPUS_TM = tm_map(CORPUS, removePunctuation) # ํŠน์ˆ˜๋ฌธ์ž ์ œ๊ฑฐ CORPUS_TM = tm_map(CORPUS_TM, removeNumbers) # ์ˆซ์ž ์ œ๊ฑฐ CORPUS_TM = tm_map(CORPUS_TM, tolower) # ์•ŒํŒŒ๋ฒณ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๊ธฐ Corpus๋Š” ๋ง๋ญ‰์น˜๋ผ๋Š” ์˜๋ฏธ๋กœ, ํ…์ŠคํŠธ ๋งˆ์ด๋‹์„ ํ•˜๊ธฐ ์ „์— ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ๊ณผ์ •์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 2๋‹จ๊ณ„ : ๋ฌธ์„œ ํ–‰๋ ฌ ์ƒ์„ฑ ๋ฌธ์„œ ํ–‰๋ ฌ์„ ๋งŒ๋“œ๋Š” ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์œ ์ž…๋‹ˆ๋‹ค. ํŠน์ • ๋‹จ์–ด๋ฅผ ๋ณ€์ˆ˜๋กœ ๋งŒ๋“ค์–ด, ๋ถ„์„์— ์‚ฌ์šฉํ•˜๋ ค๋Š” ๋ชฉ์  ํŠน์ • ๋‹จ์–ด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๋”ฐ๋กœ ์ถ”์ถœํ•˜๊ฑฐ๋‚˜ ํŠน์ • ๋‹จ์–ด๊ฐ€ ๋งŽ์ด ๋“ฑ์žฅํ•˜์˜€์„ ๋•Œ, ์ด๊ฒƒ์ด ๋‹ค๋ฅธ ๋ฌด์–ธ๊ฐ€์™€ ์ƒ๊ด€์„ฑ์ด ์žˆ๋Š”์ง€ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์  ์ฆ‰, ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ํ†ต๊ณ„์ ์ธ ๋ถ„์„์„ ํ•˜๊ธฐ ์œ„ํ•œ ์ค€๋น„๊ณผ์ •์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. TDM=DocumentTermMatrix(CORPUS_TM) # ๋ฌธ์„œ ํ–‰๋ ฌ ์ƒ์„ฑ inspect(TDM) <<DocumentTermMatrix (documents: 1000, terms: 20)>> Non-/sparse entries: 2555/17445 Sparsity : 87% Maximal term length: 9 Weighting : term frequency (tf) Sample : Terms Docs action adventure comedy crime drama horror mystery romance scifi 1 1 1 0 0 0 0 0 0 1 10 0 1 0 0 1 0 0 1 0 11 0 1 0 0 0 0 0 0 0 12 0 0 0 0 1 0 0 0 0 2 0 1 0 0 0 0 1 0 1 4 0 0 1 0 0 0 0 0 0 5 1 1 0 0 0 0 0 0 0 6 1 1 0 0 0 0 0 0 0 7 0 0 1 0 1 0 0 0 0 9 1 1 0 0 0 0 0 0 0 Terms Docs thriller 1 0 10 0 11 0 12 0 2 0 4 0 5 0 6 0 7 0 9 0 TDM = as.data.frame(as.matrix(TDM)) # ๋ฌธ์„œ ํ–‰๋ ฌ์„ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ. head(TDM) action adventure scifi mystery horror thriller animation comedy family 1 1 1 1 0 0 0 0 0 0 2 0 1 1 1 0 0 0 0 0 3 0 0 0 0 1 1 0 0 0 4 0 0 0 0 0 0 1 1 1 5 1 1 0 0 0 0 0 0 0 fantasy drama music biography romance history crime western war musical 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 5 1 0 0 0 0 0 0 0 0 0 sport 1 0 2 0 3 0 4 0 5 0 [ reached 'max' / getOption("max.print") -- omitted 1 rows ] 3๋‹จ๊ณ„ : ๊ธฐ์กด ๋ฐ์ดํ„ฐ์™€ ๊ฒฐํ•ฉํ•˜๊ธฐ IMDB_GENRE = cbind(IMDB, TDM) ๊ธฐ์กด์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ์™€, ์žฅ๋ฅด ๋ณ€์ˆ˜๋“ค๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ฉ์ณ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ์ณ์•ผ ํ•  ๋‘ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ™์€ ํ–‰์„ ๊ฐ€์ง€๊ณ  ์ˆœ์„œ๋„ ๊ฐ™๋‹ค๋ฉด, cbind(Column bind) ๋ช…๋ น์–ด๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋ฐ˜๋Œ€๋กœ ํ•ฉ์ณ์•ผ ํ•  ๋‘ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ™์€ ์—ด์„ ๊ฐ€์ง€๊ณ , ์ˆœ์„œ๋„ ๊ฐ™์€๋ฐ, ํ–‰์„ ํ•ฉ์ณ์•ผ ํ•œ๋‹ค๋ฉด rbind(row bind) ๋ช…๋ น์–ด๋ฅผ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๊ฒฐํ•ฉํ•˜๊ธฐ ๋ช…๋ น์–ด cbind : ํ–‰์ด ๋™์ผํ•˜๊ณ , ์ˆœ์„œ๋„ ๊ฐ™์„ ๋•Œ ์˜†์œผ๋กœ(๋ณ€์ˆ˜) ํ•ฉ์น˜๊ธฐ rbind : ์—ด์ด ๋™์ผํ•˜๊ณ , ์ˆœ์„œ๋„ ๊ฐ™์„ ๋•Œ ์•„๋ž˜๋กœ(obs) ํ•ฉ์น˜๊ธฐ merge : ์—ด๊ณผ ํ–‰์ด ๋‹ค๋ฅธ ๋‘ ๋ฐ์ดํ„ฐ ์…‹์„ ํ•˜๋‚˜์˜ ๊ธฐ์ค€์„ ์žก๊ณ  ํ•ฉ์น˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ Genre ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋‹ค๋ฃฐ์–ด๋ดค๋‹ค๋ฉด, ์ด๋ฒˆ์—๋Š” Description ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๋ณต์Šต๋„ ํ•  ๊ฒธ ๋‹ค๋ฃจ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Description ๋ณ€์ˆ˜๋Š” Genre ๋ณ€์ˆ˜์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ, ๋‹ค์Œ์˜ ์ฐจ์ด์ ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ์ค‘๋ณต ๋“ฑ์žฅ ์กฐ์‚ฌ, ๋™์‚ฌ, ๋ช…์‚ฌ ๋“ฑ ๋“ฑ์žฅ 1๋‹จ๊ณ„ : stopwords๋ฅผ ์ด์šฉํ•œ ๋‹จ์–ด ์ œ๊ฑฐ library(tm) CORPUS=Corpus(VectorSource(IMDB$Description)) CORPUS_TM = tm_map(CORPUS, stripWhitespace) CORPUS_TM = tm_map(CORPUS_TM, removePunctuation) CORPUS_TM = tm_map(CORPUS_TM, removeNumbers) CORPUS_TM = tm_map(CORPUS_TM, tolower) DTM = DocumentTermMatrix(CORPUS_TM) inspect(DTM) <<DocumentTermMatrix (documents: 1000, terms: 5960)>> Non-/sparse entries: 20465/5939535 Sparsity : 100% Maximal term length: 23 Weighting : term frequency (tf) Sample : Terms Docs and for from her his that the their who with 155 2 1 1 0 0 0 4 0 0 2 232 1 0 0 0 1 0 2 0 1 2 323 1 2 0 0 0 0 3 0 0 0 324 1 0 0 0 0 1 5 0 0 1 704 0 0 1 1 0 0 2 0 1 1 764 4 0 1 0 0 0 1 2 0 0 774 0 0 3 2 2 2 3 0 1 1 836 2 0 0 0 0 0 5 1 0 0 863 2 0 1 0 3 0 0 1 1 1 960 2 0 0 0 1 0 5 0 0 0 and, for, from, with ์ด๋Ÿฐ ๋‹จ์–ด๋“ค์€ ์ž์ฃผ ์“ฐ์ด๋Š” ๋‹จ์–ด์ด์ง€๋งŒ, ์‹ค์ œ๋กœ ์˜๋ฏธ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋‹จ์–ด๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํ…์ŠคํŠธ ๋งˆ์ด๋‹ ์‹œ์—๋Š” ์ด๋Ÿฐ ๋‹จ์–ด๋“ค์„ ์ œ๊ฑฐํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ๋” ์›ํ™œํ•œ ๋ถ„์„์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CORPUS_TM = tm_map(CORPUS_TM, removeWords, c(stopwords("english"),"my","custom","words")) stopwords ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด and, his ๊ฐ™์€ ๋‹จ์–ด๋“ค์„ ๋ชจ๋‘ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ๋‚˜์•„๊ฐ€ ์ถ”๊ฐ€๋กœ ์‚ญ์ œํ•˜๊ณ  ์‹ถ์€ ๋‹จ์–ด๋Š” c( ) ๋ช…๋ น์–ด ์•ˆ์— ๋„ฃ์–ด์ฃผ๋ฉด ์‚ญ์ œ๋ฅผ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” โ€˜myโ€™, โ€˜customโ€™,โ€˜wordsโ€™ ์ด 3๊ฐ€์ง€ ๋‹จ์–ด๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ญ์ œ์‹œํ‚จ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. 2๋‹จ๊ณ„ : ์ค‘๋ณต ๋“ฑ์žฅ ๋‹จ์–ด ์ฒ˜๋ฆฌ ๊ฒฐ์ • ๋ฌธ์žฅ์„ ๋ถ„ํ•ดํ•œ ๊ฒฝ์šฐ, ์ค‘๋ณต ๋‹จ์–ด ์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•˜๋Š๋ƒ๋„ ๊ฒฐ์ •ํ•ด์•ผ ๋  ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. 1์•ˆ : ํŠน์ • ๋‹จ์–ด๊ฐ€ ๋ฌธ์žฅ์— ํฌํ•จ๋˜์–ด ์žˆ๋ƒ ์—†๋ƒ๋กœ ํ‘œ์‹œ -> 0 , 1๋กœ ์ฝ”๋”ฉ (0: ํฌํ•จ x, 1: ํฌํ•จ o) convert_count = function(x) { y <- ifelse(x > 0,1,0) y = as.numeric(y) } 2์•ˆ : ํŠน์ • ๋‹จ์–ด๊ฐ€ ๋ฌธ์žฅ์—์„œ ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ–ˆ๋‚˜๋ฅผ ํ‘œ์‹œ -> ๋“ฑ์žฅ ๋นˆ๋„๋กœ ์ฝ”๋”ฉ convert_count = function(x) { y <- ifelse(x > 0, x, 0) y = as.numeric(y) } ์‚ฌ์šฉ์ž ํ•จ์ˆ˜ ์ ์šฉ ๋งคํŠธ๋ฆญ์Šค ํ˜•ํƒœ์ธ TDM์— convert_count๋ฅผ ํ•˜๋‚˜์”ฉ ์ ์šฉํ•˜์—ฌ ๊ฐ’์„ ๋ฐฐ์ถœ DESCRIPT_IMDB=apply(DTM, MARGIN=2, convert_count) DESCRIPT_IMDB=as.data.frame(DESCRIPT_IMDB) 3๋‹จ๊ณ„ : ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” # Temr Document Matrix ์ƒ์„ฑ TDM = TermDocumentMatrix(CORPUS_TM) # ์›Œ๋“œ ํด๋ผ์šฐ๋“œ ์ƒ์„ฑ m = as.matrix(TDM) v = sort(rowSums(m),decreasing=TRUE) # ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ d = data.frame(word = names(v),freq=v) library("SnowballC") library("wordcloud") library("RColorBrewer") # min.freq -> ์ตœ์†Œ 5๋ฒˆ ์ด์ƒ ์“ฐ์ธ ๋‹จ์–ด๋งŒ ๋„์šฐ๊ธฐ # max.words -> ์ตœ๋Œ€ 200๊ฐœ๋งŒ ๋„์šฐ๊ธฐ # random.order -> ๋‹จ์–ด ์œ„์น˜ ๋žœ๋ค ์—ฌ๋ถ€ wordcloud(words = d$word, freq = d$freq, min.freq = 5, max.words=200, random.order=FALSE, colors=brewer.pal(8, "Dark2")) # ๋‹จ์–ด ๋นˆ๋„ ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ ggplot(d[1:10, ]) + geom_bar(aes(x = reorder(word, freq), y = freq), stat = 'identity') + coord_flip()+ xlab("word") + ylab("freq") + theme_bw() ํ…์ŠคํŠธ ๋งˆ์ด๋‹์€ ์‚ฌ์‹ค ํ†ต๊ณ„์ ์ธ ์ด์šฉ๋ณด๋‹ค๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๊ธฐ๋ฐ˜ํ•œ Computer Science ์˜์—ญ์— ๊ฐ€๊น์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ํ…์ŠคํŠธ๋ฅผ ์ œ๋Œ€๋กœ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ˜•ํƒœ์†Œ ๋ถ„์„์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉ์‹œ์ผœ์•ผ ๊ทธ๋‚˜๋งˆ ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” R์„ ์ด์šฉํ•˜์—ฌ ๋น„์ •ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ฐ€๊ณตํ•˜๋ฉฐ, ์ด๋ฅผ ๋ถ„์„์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•˜๋Š”์ง€์— ์ง‘์ค‘์„ ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐํšŒ๊ฐ€ ๋œ๋‹ค๋ฉด ํ›„์— ๋” ์‹ฌํ™”๋œ ๋‚ด์šฉ์„ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A9. ์—ฐ์Šต๋ฌธ์ œ 9. ์—ฐ์Šต๋ฌธ์ œ IMDB ๋ฐ์ดํ„ฐ ์…‹์˜ Revenue Millions ๋ณ€์ˆ˜์— ์กด์žฌํ•˜๋Š” ๊ฒฐ์ธก์น˜๋ฅผ ๋ชจ๋‘ 0์œผ๋กœ ์ „ํ™˜์‹œ์ผœ Revenue_NonNA๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ์‹œ์˜ค. Revenue Millions์˜ ์ด์ƒ์น˜ ๋ฒ”์œ„๋ฅผ ๊ณ„์‚ฐํ•ด ๋ณด์„ธ์š” Ch6. R ์ค‘๊ธ‰๋ฌธ๋ฒ• 1๋‹จ๊ณ„ R ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง์—์„œ ๋งค์šฐ ์ค‘์š”ํ•˜๊ฒŒ ์“ฐ์ด๋Š” apply์™€ dplyr์— ๋Œ€ํ•ด ๋‹ค๋ฃน๋‹ˆ๋‹ค. A1. ํšจ๊ณผ์ ์ธ ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง์„ ์œ„ํ•œ apply & dplyr ์†Œ๊ฐœ 1. ํšจ๊ณผ์ ์ธ ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง์„ ์œ„ํ•œ apply & dplyr ์†Œ๊ฐœ R์—๋Š” ๋งค์šฐ ๋งŽ์€ ๋ช…๋ น์–ด๊ฐ€ ์กด์žฌํ•˜๋ฉฐ, ๊ทธ์ค‘ ๋‹ค์ˆ˜์˜ ์ฝ”๋“œ๋Š” ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜์ง€๋งŒ ๋ช…๋ น์–ด๋งŒ ๋‹ค๋ฅผ ๋ฟ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์—, ์ธํ„ฐ๋„ท์— ์˜ฌ๋ผ์™€ ์žˆ๋Š” R ์ฝ”๋“œ๋“ค์„ ์‚ดํŽด๋ณด๋ฉด ์ž‘์„ฑ์ž์˜ ๊ฐœ์„ฑ์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•˜๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ณง R์˜ ๊ฐ€์žฅ ํฐ ์žฅ์ ์ด์ž ๊ฐ€์žฅ ํฐ ๋‹จ์ ์œผ๋กœ ์ž‘์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๋‹ค์–‘ํ•˜๊ณ  ํŽธํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํŽธ๋ฆฌ์™€ ๋‹ค์–‘์„ฑ์ด ๋ณด์žฅ๋œ ๋ฐ˜๋ฉด, ์ฒ˜์Œ ์ ‘ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์—๊ฒŒ๋Š” ํ˜ผ๋ž€์„ ์•ผ๊ธฐํ•˜๊ธฐ ๋งค์šฐ ์ข‹์œผ๋ฉฐ, ์ •๋ˆ๋˜์–ด ์žˆ์ง€๊ฐ€ ์•Š์Šต๋‹ˆ๋‹ค. python์€ numpy, pandas ๋“ฑ์˜ ๋‹จ์ผ ํŒจํ‚ค์ง€๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ์ ๊ณผ ๋น„๊ตํ•˜๋ฉด R์€ ์ง€๋‚˜์น˜๊ฒŒ ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๋‹ค๊ณ  ํŒŒ์ด์ฌ์ด R๋ณด๋‹ค ์‰ฝ๋‹ค๋Š” ์˜๋ฏธ๋กœ ์ง๊ฒฐ๋˜์ง€๋Š” ์•Š์œผ๋‹ˆ, ํŽธํ•œ ๋งˆ์Œ์œผ๋กœ ํ•™์Šต์„ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์˜ ๋ชฉ์ ์€ ํ†ต๊ณ„ ๊ฐ’์„ ๋ฝ‘์•„๋‚ผ ๋•Œ ์ž์ฃผ ์“ฐ์ด๋Š” ๋ช…๋ น์–ด๋“ค์„ ์ •๋ฆฌํ•˜๋ฉด์„œ, ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ์ฝ”๋“œ๋“ค๋„ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋งˆ, ์—ฌ๊ธฐ๊นŒ์ง€ ๋”ฐ๋ผ์˜ค์…จ๋‹ค๋ฉด ์ด์ œ R ์ฝ”๋“œ์˜ ๊ตฌ์กฐ๋Š” ๋‹ค ์ดํ•ดํ•˜์‹ค ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ ์ฃผ๋กœ ๋‹ค๋ฃฐ ํŒจํ‚ค์ง€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. library(dplyr) library(reshape) library(plyr) dplyr ๋ฐ reshape ํŒจํ‚ค์ง€๋Š” ๋งค์šฐ ์ž์ฃผ ์“ฐ์ด๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ ์ด ํŒจํ‚ค์ง€๋“ค์„ ์œ„์ฃผ๋กœ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A2. ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ 2. ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ HR = read.csv('D:/Drop box/DATA SET/HR_comma_sep.csv') A3. apply ํ•จ์ˆ˜์™€ dplyr ํŒจํ‚ค์ง€ ์†Œ๊ฐœ 3. apply ํ•จ์ˆ˜์™€ dplyr ํŒจํ‚ค์ง€ ์†Œ๊ฐœ apply ํ•จ์ˆ˜ ์†Œ๊ฐœ apply๋ผ๋Š” ํ•จ์ˆ˜๋Š” ๋งŽ์€ ์ฝ”๋“œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ €ํฌ๋Š” ์ด์ „์— for ๋ฌธ์„ ํ†ตํ•ด ๋ฐ˜๋ณต๋ฌธ์„ ๋งŒ๋“ค์–ด ๋ณธ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. for ๋ฌธ์€ ๋งค์šฐ ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์ด์ง€๋งŒ, ๋งŒ๋Šฅ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ํ•˜๋‚˜์˜ ์—ด(Column)์— ๋Œ€ํ•ด ์ž‘๋™์„ ํ•  ๋ฟ, ๋™์‹œ์— ์—ฌ๋Ÿฌ column ํ˜น์€ row์— ๋Œ€ํ•ด์„œ ๊ณ„์‚ฐ์„ ์‹คํ–‰ํ•  ์ˆ˜๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. apply๋Š” ์ด๋ฅผ ๋™์‹œ์— ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ํ›Œ๋ฅญํ•œ ๊ธฐ๋Šฅ์˜ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋™์ผํ•œ ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด์„œ apply ๋ฌธ๊ณผ for ๋ฌธ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ๋ช…๋ น์–ด๊ฐ€ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ์ด ๋˜๋Š”์ง€ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ : HR ๋ฐ์ดํ„ฐ ์…‹์˜ 1,2์—ด ํ‰๊ท ์„ ๊ตฌํ•˜๊ณ ์ž ํ•œ๋‹ค. for ๋ฌธ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ for(i in 1:2){ print(paste(colnames(HR)[i],":",mean(HR[,i]))) } [1] "satisfaction_level : 0.612833522234816" [1] "last_evaluation : 0.716101740116008" apply๋ฅผ ํ™œ์šฉํ•  ๊ฒฝ์šฐ apply(HR[,1:2],2, mean) satisfaction_level last_evaluation 0.6128335 0.7161017 ๋จผ์ €, apply ๋ฌธ์˜ ๊ฒฝ์šฐ (๋ฐ์ดํ„ฐ, ๊ณ„์‚ฐ ๊ธฐ์ค€(1 ํ˜น์€ 2), ํ•จ์ˆ˜)๋ฅผ ์ž…๋ ฅํ•ด ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ โ€™๊ณ„์‚ฐ ๊ธฐ์ค€(1 ํ˜น์€ 2)โ€™๋Š” ํ–‰/์—ด ์ค‘ ์–ด๋–ค ๊ฒƒ์„ ๊ธฐ์ค€์œผ๋กœ ์—ฐ์‚ฐ์„ ํ• ์ง€ ์ •ํ•ด์ฃผ๋Š” ์„ค์ •๊ฐ’์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ 1์„ ์ž…๋ ฅํ•˜๋ฉด, mean์„ ๊ฐ ํ–‰(row) ๋ณ„๋กœ ๊ณ„์‚ฐ์„ ํ•  ๊ฒƒ์ด๊ณ , 2๋ฅผ ์ž…๋ ฅํ•˜๋ฉด mean์„ ๊ฐ ์—ด(column) ๋ณ„๋กœ ๊ณ„์‚ฐ์„ ์ง„ํ–‰ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„ ๋ช…๋ น์–ด๋Š” 2๋ฅผ ์ž…๋ ฅํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ ์—ด(column)์˜ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. colMeans๋ฅผ ํ™œ์šฉํ•  ๊ฒฝ์šฐ colMeans(HR[,1:2]) satisfaction_level last_evaluation 0.6128335 0.7161017 colMeans๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐ”๋กœ ๊ฐ ๋ณ€์ˆ˜์˜ ํ‰๊ท ์„ ๊ตฌํ•ด์ฃผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์ •๋ง ๊ฐ„ํŽธํ•˜์ง€์š”? ๊ทผ๋ฐ ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ ์™œ ๊ตณ์ด apply ๋ฌธ์„ ํ™œ์šฉํ•ด์•ผ ๋˜๋Š”์ง€ ์˜๋ฌธ์‚ฌํ•ญ์ด ์ƒ๊ธธ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ท ์ฒ˜๋Ÿผ ๋ช…๋ น์–ด๊ฐ€ ๋งŒ๋“ค์–ด์ ธ ์žˆ๋Š” ์ƒํ™ฉ์ด๋ฉด ๊ตณ์ด apply ๋ฌธ์„ ํ™œ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? ์ง€๊ธˆ ๋‹น์žฅ๋งŒ ํ•ด๋„ ์ฃผ๋กœ ํ‰๊ท ๊ณผ ๊ฐ™์ด ๊ตฌํ•˜๋Š” ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ฐ ๋ณ€์ˆ˜๋งˆ๋‹ค ๊ณ„์‚ฐํ•ด ์ฃผ๋Š” ๊ฐ„๋‹จํ•œ ๋ช…๋ น์–ด๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด apply์— ์ ์šฉ์„ ์‹œ์ผœ์ค˜์•ผ ํŽธํ•˜๊ฒŒ ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. apply(HR[,1:2],2, sd) satisfaction_level last_evaluation 0.2486307 0.1711691 ๋ฐ”๋กœ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ง์ด์ง€์š”. ๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ์— ๊ฒฐ์ธก์น˜๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด? ์—ฌ๋Ÿฌ๋ถ„๋„ ์•„์‹œ๋‹ค์‹œํ”ผ, ๊ฒฐ์ธก์น˜๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ R์—์„œ ์—ฐ์‚ฐ ํ•จ์ˆ˜๋Š” na.rm = TRUE ์˜ต์…˜์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด, ๋ชจ๋“  ๊ฒฐ๊ด๊ฐ’์ด NA๋กœ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ณ€์ˆ˜์˜ ํ‘œ์ค€ํŽธ์ฐจ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ• D = c(1,2,3,4, NA) E = c(1,2,3,4,5) DF = data.frame( D = D, E = E ) ๋จผ์ €, 2๊ฐœ์˜ ์—ด(column)์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ apply ๋ฌธ์œผ๋กœ ๊ฐ ๋ณ€์ˆ˜์˜ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ตฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. apply(DF, 2, sd) D E NA 1.581139 ๋ณด์‹œ๋‹ค์‹œํ”ผ ๊ฒฐ์ธก์น˜(NA)๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” โ€™Dโ€™๋ณ€์ˆ˜์˜ ํ‘œ์ค€ํŽธ์ฐจ๋Š” NA๋กœ ๊ฐ’์ด ๊ณ„์‚ฐ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ, NA๋ฅผ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ๋Š” ํ‘œ์ค€ํŽธ์ฐจ ๊ณ„์‚ฐ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. colSd = function(x){ y = sd(x, na.rm = TRUE) return(y) } ๊ฐ„๋‹จํ•˜๊ฒŒ y๋Š” x์˜ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๊ตฌํ•˜๋ฉฐ NA๋ฅผ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ๋„๋ก na.rm = TRUE ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ด์ œ ๋‹ค์‹œ, apply ๋ฌธ์„ ํ™œ์šฉํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. apply(DF, 2, colSd) D E 1.290994 1.581139 ๋ฐฉ๊ธˆ ๋งŒ๋“  ColSd๋ฅผ ํ™œ์šฉํ•ด ์ฃผ๋‹ˆ ๋ฌธ์ œ๊ฐ€ ์—†์ด ๊ณ„์‚ฐ์ด ์ž˜ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ, ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ๋ช…๋ น์–ด๋„ ์กด์žฌํ•˜์ง€๋งŒ, ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ๋Š” ๋ช…๋ น์–ด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํ•ญ์ƒ ์œ ๋…ํ•˜๊ณ  ์žˆ์œผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ๋Š” ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ apply ํ•จ์ˆ˜๋ฅผ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. apply ๊ณ„์—ด ํ•จ์ˆ˜ ์†Œ๊ฐœ ๊ทธ๋ฃน ๊ฐ„ ํ‰๊ท ์„ ๊ตฌํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ๊ธฐ์ดˆ ํ†ต๊ณ„๋ถ„์„์„ ์ง„ํ–‰ํ•  ๊ฒฝ์šฐ, ๊ทธ๋ฃน ๊ฐ„ ํ†ต๊ณ„ ๊ฐ’(ํ‰๊ท , ํ‘œ์ค€ํŽธ์ฐจ) ๋“ฑ์„ ๊ตฌํ•˜๋Š” ์ƒํ™ฉ์€ ๋งค์šฐ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ tapply๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. tapply(HR$satisfaction_level, HR$left, mean) 0 1 0.6668096 0.4400980 tapply ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ (๋ฐ์ดํ„ฐ, ๊ทธ๋ฃน, ์—ฐ์‚ฐ ํ•จ์ˆ˜)๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ทธ๋ฃน ๊ฐ„ ํ†ต๊ณ„ ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋“ค์— ๋Œ€ํ•ด ๋™์ผ ์กฐ๊ฑด์„ ์ฃผ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์ด๋Ÿฐ ๊ฒฝ์šฐ lappy ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋น„๊ต์  ํŽธํ•˜๊ฒŒ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜์— ๋Œ€ํ•˜์—ฌ ๋™์ผ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์— ์ ์šฉ์‹œํ‚ฌ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„์—์„œ ๋งŒ๋“ค์—ˆ๋˜ DF ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•˜์—ฌ 1์— ํ•ด๋‹น๋˜๋ฉด โ€œAโ€๋ฅผ ์ฃผ๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ฐ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ๋ณ€ํ™˜ ํ•จ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์–ด์•ผ ํ•˜์ง€๋งŒ, lapply๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋จผ์ €, ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด ์ด๋ ‡๊ฒŒ ๋ช…๋ น์–ด๋ฅผ ๊ตฌ์„ฑํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. DF$D2 = gsub(1, "A",DF$D) DF$E2 = gsub(1, "A",DF$E) ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ๋„ ๋‚˜์˜์ง€๋Š” ์•Š์ง€๋งŒ, ๋งŒ์•ฝ ์ ์šฉํ•ด์•ผ ํ•˜๋Š” ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ 10๊ฐœ, 20๊ฐœ์ธ ๊ฒฝ์šฐ๋Š” ๋งŽ์ด ๋‚œ๊ฐํ•ด์ง€๋Š” ์ƒํ™ฉ์ด ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ด์ œ lappy๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•œ ๋ฒˆ์— ๋ณ€๊ฒฝ์„ ์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. DF2 = DF[,1:2] DF3 = lapply(DF2, function(x) gsub(1, "A",x)) DF3 = as.data.frame(DF3) lapply ํ•œ ์ค„๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ apply ๊ณ„์—ด์€ ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง์—์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์„ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ฒ˜์Œ์— apply ๊ณ„์—ด ํ•จ์ˆ˜๋ฅผ ์ ‘ํ•˜๋ฉด ๋งค์šฐ ์–ด๋ ต์ง€๋งŒ, ์‚ฌ์šฉํ•˜์‹œ๋‹ค ๋ณด๋ฉด ์ต์ˆ™ํ•ด์งˆ ๊ฒƒ์ด๊ณ , ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ๋ฅผ ๋จธ๋ฆฟ์†์— ํ•ญ์ƒ ๊ทธ๋ฆฌ๊ณ  ๊ณ„์‹œ๋ฉด ์–ด๋ ต์ง€ ์•Š๊ฒŒ apply ๊ณ„์—ด ๋ช…๋ น์–ด๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์œผ์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. dplyr ํŒจํ‚ค์ง€ ์†Œ๊ฐœ dplyr ํŒจํ‚ค์ง€๋Š” R ์‚ฌ์šฉ์ž ์‚ฌ์ด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์ด๋Š” ํŒจํ‚ค์ง€ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ณต์žกํ•˜๊ณ  ์—ฐ์‡„์ ์ธ ์—ฐ์‚ฐ์„ ์ง๊ด€์ ์œผ๋กœ ์ž…๋ ฅํ•˜๊ณ  ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ์‹œ๋ฅผ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. head(rowMeans(HR[,1:2])) [1] 0.455 0.830 0.495 0.795 0.445 0.455 HR[,1:2] %>% rowMeans() %>% head() [1] 0.455 0.830 0.495 0.795 0.445 0.455 ์œ„ ๋‘ ๋ช…๋ น์–ด๋Š” ํ˜•ํƒœ๋Š” ๋‹ค๋ฅด์ง€๋งŒ, ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 2๋ฒˆ์งธ์— ํ•ด๋‹นํ•˜๋Š” ๋ช…๋ น์–ด๊ฐ€ dplyr ํŒจํ‚ค์ง€์˜ %>%๋ฅผ ํ™œ์šฉํ•œ ๋ช…๋ น์–ด ๊ตฌ์„ฑ์ž…๋‹ˆ๋‹ค. ์ด ๋งค์šฐ ์ด์งˆ์ ์ด๊ฒŒ ์ƒ๊ธด ์ฝ”๋“œ๋Š” dplyr ํŒจํ‚ค์ง€์—์„œ ํ•ต์‹ฌ์ ์ธ ์—ญํ• ์„ ์ง€๋‹ˆ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ €, ์ฒซ ๋ฒˆ์งธ ๋ช…๋ น์–ด์˜ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. head(rowMeans(HR[,1:2]))์˜ ๋ช…๋ น์–ด ์‹คํ–‰ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. HR[,1:2] ๊ณ„์‚ฐ rowMeans(HR[,1:2]) ๊ณ„์‚ฐ head(rowMeans(HR[,1:2])) ๊ณ„์‚ฐ ์ˆœ์„œ๋กœ ์—ฐ์‚ฐ์ด ์ง„ํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ์ •๋„์•ผ ํ•œ ์ค„๋กœ ์ •๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋งŒ์•ฝ ๋ช…๋ น์–ด๋ฅผ ๋” ํ™œ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ๊ตฌ์„ฑ์ด ๋งค์šฐ ๋ณต์žกํ•ด์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. dplyr ํŒจํ‚ค์ง€์˜ %>%๋Š” ๋ช…๋ น์–ด๊ฐ€ ๋ณต์žกํ•ด์ง€์ง€ ์•Š๊ณ  ์ง๊ด€์ ์œผ๋กœ ๊ตฌ์„ฑ์ด ๋  ์ˆ˜ ์žˆ๋„๋ก ์ค‘๊ฐ„๋‹ค๋ฆฌ ์—ญํ• ์„ ํ•ด์ค๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ช…๋ น์–ด์˜ ๊ตฌ์„ฑ์„ ์‚ดํŽด๋ณด์‹œ๋ฉด, ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์˜๋„ํ•œ ์—ฐ์‚ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ๋ฅผ ํ†ตํ•ด ๋” ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. apply(HR[,1:5],2, mean) satisfaction_level last_evaluation number_project 0.6128335 0.7161017 3.8030535 average_montly_hours time_spend_company 201.0503367 3.4982332 colMeans(HR[,1:5]) satisfaction_level last_evaluation number_project 0.6128335 0.7161017 3.8030535 average_montly_hours time_spend_company 201.0503367 3.4982332 HR[,1:5] %>% colMeans() satisfaction_level last_evaluation number_project 0.6128335 0.7161017 3.8030535 average_montly_hours time_spend_company 201.0503367 3.4982332 ์ด์ œ %>%์˜ ๋ชฉ์ ์ด ์–ด๋–ค ๊ฒƒ์ธ์ง€ ์•„์‹œ๊ฒ ๋‚˜์š”?? dplyr์˜ %>%๋Š” ์–ธ๋œป ๋ณด๋ฉด ๋งค์šฐ ์–ด๋ ค์›Œ ๋ณด์ด์ง€๋งŒ, ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๊ณ„์† ๊ธฐ์กด R ์ฝ”๋“œ์™€ %>%๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์˜ ์ฐจ์ด์ ์„ ๋น„๊ตํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ง‘๊ณ„ ๋‚ด๊ธฐ # Summarise summarise(HR, MEAN = mean(satisfaction_level), N = length(satisfaction_level)) MEAN N 1 0.6128335 14999 HR %>% summarise(MEAN = mean(satisfaction_level), N = length(satisfaction_level)) MEAN N 1 0.6128335 14999 subset ํ›„ ddply๋ฅผ ์ ์šฉํ–ˆ์„ ๋•Œ %>% ํ™œ์šฉ๋ฒ• # library(plyr) HR2_O = ddply(subset(HR, left == 1),c("sales"), summarise, MEAN = mean(satisfaction_level), N = length(satisfaction_level)) HR2_D = HR %>% subset(left == 1) %>% group_by(sales) %>% dplyr::summarise(MEAN = mean(satisfaction_level), N = length(satisfaction_level)) ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ HR3_D = HR2_D %>% mutate(percent = MEAN / N) dplyr์™€ ggplot2์˜ ์กฐํ•ฉ library(ggplot2) HR2_D %>% ggplot() + geom_bar(aes(x=sales, y=MEAN, fill=sales), stat="identity") + geom_text(aes(x=sales, y= MEAN+0.05, label=round(MEAN, 2))) + theme_bw() + xlab("๋ถ€์„œ") + ylab("ํ‰๊ท  ๋งŒ์กฑ๋„") + guides(fill = FALSE) + theme(axis.text.x = element_text(angle = 45, size = 8.5, color = "black", face = "plain", vjust = 1, hjust = 1)) A4. ์ค‘๋ณต ๋ฐ์ดํ„ฐ ์ œ๊ฑฐํ•˜๊ธฐ ๋ฐ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ •๋ ฌ 4. ์ค‘๋ณต ๋ฐ์ดํ„ฐ ์ œ๊ฑฐํ•˜๊ธฐ ๋ฐ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ •๋ ฌ ํ”ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ, ์ค‘๋ณต์œผ๋กœ ์ž…๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ ์…‹์„ ๋งˆ์ฃผ์น˜๋Š” ์ผ์ด ์ƒ๊ธฐ๊ธฐ ๋งˆ๋ จ์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋Š” ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ๋‹จ๊ณ„์—์„œ ๋งŽ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์—‘์…€๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ํ•œ๊ณ„๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, R์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃจ์–ด ๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. 1์ฐจ์› ๋ฒกํ„ฐ, ๋ฆฌ์ŠคํŠธ์—์„œ์˜ ์ค‘๋ณต ์ œ๊ฑฐ A = rep(1:10, each = 2) # 1 ~ 10๊นŒ์ง€ 2๋ฒˆ์”ฉ ๋ฐ˜๋ณต print(A) [1] 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 # ์ค‘๋ณต ์ œ๊ฑฐ unique(A) [1] 1 2 3 4 5 6 7 8 9 10 1์ฐจ์› ๋ฒกํ„ฐ์˜ ๊ฒฝ์šฐ, unique ๋ช…๋ น์–ด๋ฅผ ํ™œ์šฉํ•˜๋ฉด, ์†์‰ฝ๊ฒŒ ์ค‘๋ณต ๊ฐ’์„ ์ œ๊ฑฐํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์—์„œ์˜ ์ค‘๋ณต ์ œ๊ฑฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ์˜ˆ์‹œ๋กœ ์‚ผ๊ฒ ์Šต๋‹ˆ๋‹ค. # ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ DUPLICATE = read.csv("D:\Drop box\DATA SET(Drop box)\DUPLICATED.csv") ๋ณ€์ˆ˜์˜ ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. OBS : ๋ฒˆํ˜ธ NAME : ํ™˜์ž ์ด๋ฆ„ ID : ํ™˜์ž ๊ณ ์œ ๋ฒˆํ˜ธ DATE : ๊ฒ€์‚ฌ ๋‚ ์งœ BTW : Body total water ๋จผ์ € ํ™˜์ž ์ด๋ฆ„์ด ์žˆ๊ณ , ๊ทธ ํ™˜์ž์˜ ๊ณ ์œ  ID๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™๋ช…์ด์ธ์€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ํ•ญ์ƒ ๊ณ ์œ  ID๋ฅผ ๊ธฐ๋กํ•ด๋‘๊ธฐ ๋งˆ๋ จ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ์ค‘๋ณต ์ œ๊ฑฐ DUPLICATED3_1 = DUPLICATE[-which(duplicated(DUPLICATE)),] ํ•˜๋‚˜๋ผ๋„ ์ค‘๋ณต์ด ๋˜๋ฉด ์ „๋ถ€ ์ง€์›Œ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ๋ณ„๋กœ ์ถ”์ฒœ๋“œ๋ฆฌ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ํ•œ ๊ฐœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ค‘๋ณต ์ œ๊ฑฐ # NAME ์ด ๊ฐ™์€ ๋ณ€์ˆ˜๋“ค ์ค‘๋ณต ์ œ๊ฑฐ DUPLICATED3_2 = DUPLICATE[-which(duplicated(DUPLICATE$NAME)),] ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•ด์•ผ ๋  ๋ถ€๋ถ„์€ ์ค‘๋ณต ๊ฐ’์€ ์ œ๊ฑฐ๋ฅผ ํ•˜๊ณ  ํ•˜๋‚˜๋งŒ ๋‚จ๊ธฐ๊ฒŒ ๋˜๋Š”๋ฐ, ๊ทธ ๊ธฐ์ค€์€ Row ๋ฒˆํ˜ธ๊ฐ€ ๋น ๋ฅธ, ์ฆ‰ ์œ„์— ์žˆ๋Š” ํ–‰์„ ๋‚จ๊ธฐ๊ณ  ๋’ค์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๊ฐ’๋“ค์„ ์‚ญ์ œ ์‹œํ‚ต๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ณ€์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ค‘๋ณต ์ œ๊ฑฐ #NAME, ID ๋‘ ๊ฐœ์˜ ๊ฐ’์ด ๊ฐ™์€ ์ค‘๋ณต ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ # ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์ œ๊ฑฐ DUPLICATED3_3 = DUPLICATE[!duplicated(DUPLICATE[,c('NAME','ID')]),] # ๋ณ€์ˆ˜ ์ธ๋ฑ์Šค๋กœ ์ œ๊ฑฐ DUPLICATED3_4 = DUPLICATE[!duplicated(DUPLICATE[,c(2,3)]),] ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋“ค์„ ๊ธฐ์ค€์œผ๋กœ ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์‹ , ํ–‰(row) ์ธ๋ฑ์Šค์— ์ค‘๋ณต ์ œ๊ฑฐ ์กฐ๊ฑด์„ ์ฃผ์–ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์ด ์กฐ๊ธˆ ๋ณต์žกํ•ด ๋ณด์ผ ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, ์ฒœ์ฒœํžˆ ์‚ดํŽด๋ณด์‹œ๋ฉด ํฌ๊ฒŒ ์–ด๋ ต์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ๊ฐ€๋” ๋ณ‘์›์—์„œ๋Š” ๋™์ผ ํ™˜์ž๊ฐ€ ๊ฐ™์€ ๊ฒ€์‚ฌ๋ฅผ ๋‚ ์งœ๋ฅผ ๋‹ฌ๋ฆฌํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ›๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์–ด๋–ค ๊ฐ’์„ ๋‚จ๊ฒจ์•ผ ํ• ์ง€๋Š” ์ƒํ™ฉ๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒ ์ง€๋งŒ, ์ง€๊ธˆ์€ โ€™๋งˆ์ง€๋ง‰ ๊ฒ€์‚ฌ ๊ธฐ์ค€ ๋ฐ์ดํ„ฐโ€™๋ฅผ ๋‚จ๊ธฐ๋Š” ๊ฒƒ์œผ๋กœ ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ค‘๋ณต ์ œ๊ฑฐ๋Š” ๋งจ ์ฒ˜์Œ ๊ฐ’๋งŒ ๋‚จ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์—, ์ตœ๊ทผ ๊ฒ€์‚ฌ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งจ ์œ„๋กœ ์˜ฌ๋ผ์˜ค๋„๋ก ์ •๋ ฌ(sort)์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ •๋ ฌํ•˜๊ธฐ # ๋‚ ์งœ ๋ณ€์ˆ˜ ์„ค์ •ํ•˜๊ธฐ DUPLICATE$DATE = as.Date(DUPLICATE$DATE,"%Y-%m-%d") summary(DUPLICATE$DATE) Min. 1st Qu. Median Mean 3rd Qu. "2018-11-25" "2018-11-27" "2018-11-28" "2018-11-28" "2018-11-29" Max. "2018-11-30" 0๋จผ์ €, ๋‚ ์งœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” DATE ๋ณ€์ˆ˜๊ฐ€ ๋‚ ์งœ๋กœ ์ธ์‹์ด ๋˜์–ด ๋Œ€์†Œ ๋น„๊ต๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณ€์ˆ˜์˜ strings๊ฐ€ ๋ณ€๊ฒฝ์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‚ ์งœ ๋ณ€์ˆ˜๋ฅผ ๋‚ ์งœ<NAME>์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋ช…๋ น์–ด๋Š” as.Date, as.Posixct๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต โ€™๋…„-์›”-์ผโ€™๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉด as.Date๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  โ€™๋…„-์›”-์ผ ์‹œ:๋ถ„:์ดˆโ€™๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉด as.Posixct๋ฅผ ์“ฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ โ€™๋…„-์›”-์ผโ€™์ด๊ธฐ ๋•Œ๋ฌธ์— as.Date๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # DATE ๋ณ€์ˆ˜ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ DUPLICATE_SORT = DUPLICATE[order(DUPLICATE[,'DATE'],decreasing = TRUE), ] decreasing ์˜ต์…˜์€ ์˜ค๋ฆ„์ฐจ์ˆœ ํ˜น์€ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ํ•  ๊ฒƒ์ธ์ง€ ์ •์˜ํ•ด ์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. decreasing = TRUE์€ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ง„ํ–‰ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ค‘๋ณต์ œ๊ฑฐ๋Š” ์œ„์—์„œ ์ง„ํ–‰ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์ง„ํ–‰ํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณ‘์›์—์„œ ๊ฐ™์€ ํ™˜์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฒ€์‚ฌ๋ฅผ ๋ฐ›๋Š” ๊ฒฝ์šฐ๋Š” ๋งค์šฐ ํ”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๊ฒ€์‚ฌ ์ˆ˜์น˜๋ฅผ ๊ฐ€์ง€๊ณ  ๋ถ„์„์„ ํ•ด์•ผ ๋˜์ง€์š”. ํ•˜์ง€๋งŒ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘์ด ์กฐ๊ธˆ์€ ์• ๋งคํ•˜๊ฒŒ ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์ด๋Ÿฐ ๊ฒฝ์šฐ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. TEST ๋ณ€์ˆ˜๋Š” ๊ฒ€์‚ฌ ์ข…๋ฅ˜๋ผ๊ณ  ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ˆ˜์ง‘ ์ฒด๊ณ„๊ฐ€ ์ž˜๋ชป๋œ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ๊ฐ ๊ฒ€์‚ฌ ์ˆ˜์น˜๋ฅผ ๋ชจ๋ธ๋ง์—์„œ ์จ๋จน์œผ๋ ค๋ฉด, TEST์˜ ๊ฐ ์ˆ˜์ค€(levels, T1, T2, T3, T4)๋Š” ํ•˜๋‚˜์˜ TEST ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฐ๊ฐ์˜ T1, T2, T3, T4 ๋ณ€์ˆ˜๋กœ ์žกํ˜€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ฐ ๊ฒ€์‚ฌ ์ˆ˜์น˜๊ฐ€ ๋ณ€์ˆ˜๊ฐ€ ๋˜์–ด์•ผ ํ›„์— ๋ชจ๋ธ๋ง์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์ฝ”๋“œ๋Š” ์ด๋ ‡๊ฒŒ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. library(reshape) RESHAPE = read.csv("D:\Drop box\DATA SET(Drop box)\RESHAPE.csv") CAST_DATA = cast(RESHAPE, OBS + NAME + ID + DATE ~ TEST) reshape ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์˜ ํ˜•ํƒœ๋ฅผ ๋ฐ”๊พธ๊ณ  ์‹ถ์„ ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ TEST์˜ ๊ฐ ์ˆ˜์ค€(levels)์ด ๋ณ€์ˆ˜๊ฐ€ ๋˜์—ˆ์„ ๋•Œ, ์ด๋Ÿฐ ํ˜•ํƒœ๋ฅผ Wide Form์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ, ๊ธฐ์กด์— ํ•˜๋‚˜์˜ TEST ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ์ด ๋˜์—ˆ์„ ๊ฒฝ์šฐ Long Form ํ˜•ํƒœ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ์›๋ž˜๋Œ€๋กœ ๋Œ์•„๊ฐ€๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. MELT_DATA = melt(CAST_DATA, ID=c("OBS","NAME","ID","DATE")) MELT_DATA = na.omit(MELT_DATA) melt ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด, ๋‹ค์‹œ ์›๋ž˜๋Œ€๋กœ ๋Œ์•„์˜ฌ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Wide & Long Form์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์€ ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง ๋‹จ๊ณ„์—์„œ ๋งค์šฐ ์ค‘์š”ํ•˜๋ฏ€๋กœ ์ถฉ๋ถ„ํ•œ ์—ฐ์Šต์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์ถ”๊ฐ€๋กœ ๋‘ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ ๋ชจ๋‘ ์“ฐ์ž„์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. CAST_DATA์˜ ๊ฒฝ์šฐ, ๊ทธ๋ž˜ํ”„ ์‹œ๊ฐํ™”ํ•  ๋•Œ์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. MELT_DATA์˜ ๊ฒฝ์šฐ, ๋ชจ๋ธ๋ง ํ•  ๋•Œ์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. A5. ๋ฐ์ดํ„ฐ ํ•ฉ๋ณ‘ํ•˜๊ธฐ(merge) 5. ๋ฐ์ดํ„ฐ ํ•ฉ๋ณ‘ํ•˜๊ธฐ(merge) ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜๋‹ค ๋ณด๋ฉด, ํŠน์ • KEY ๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ฉ๋ณ‘ํ•ด์•ผ ๋  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ### ์ด์ „ ๋ฐ์ดํ„ฐ ์ž‘์—… ์ฝ”๋“œ DUPLICATE = read.csv("D:\Drop box\DATA SET(Drop box)\DUPLICATED.csv") DUPLICATED3_3 = DUPLICATE[!duplicated(DUPLICATE[,c('NAME','ID')]),] RESHAPE = read.csv("D:\Drop box\DATA SET(Drop box)\RESHAPE.csv") CAST_DATA = cast(RESHAPE, OBS + NAME + ID + DATE ~ TEST) ### ๋ฐ์ดํ„ฐ ํ•ฉ๋ณ‘ MERGE = merge(DUPLICATED3_3, CAST_DATA[,c(-1, -2, -4)] , by = "ID", all.x = TRUE) A6. ์—ฐ์Šต๋ฌธ์ œ 6. ์—ฐ์Šต๋ฌธ์ œ Event_Time.csv๋ฅผ ๋ถˆ๋Ÿฌ์˜ค์‹œ์˜ค. ๋งํฌ : https://www.drop box.com/sh/xx1w2syi768kfU0/AACZgxgo1fcxyDMgv9U-iTz8a? dl=0 colSums๋ฅผ ํ™œ์šฉํ•˜์—ฌ Alarm, Event ๋ณ€์ˆ˜์˜ ํ•ฉ์„ ๊ฐ๊ฐ ๊ตฌํ•˜์‹œ์˜ค. reshape ํŒจํ‚ค์ง€์˜ ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์Œ์˜ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“ค์–ด๋ณด์‹œ์˜ค. Ch7. R ์ค‘๊ธ‰๋ฌธ๋ฒ• 2๋‹จ๊ณ„ ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ์ด์ „์— ๋‹ค๋ฃจ์—ˆ๋˜ dplyr๋ฅผ ์กฐ๊ธˆ ๋” ์‹ฌ๋„ ์žˆ๊ฒŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค. A0. ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ 0. ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://www.drop box.com/sh/vtqlvrgdts2yfez/AAD_cd49dBcvgBNdz-C-A6TFA?dl=0 library(dplyr) STOCK = read.csv("D:\Drop box\DATA SET(Drop box)\Uniqlo_stocks2012-2016.csv") STOCK$Date = as.Date(STOCK$Date) STOCK$Year = as.factor(format(STOCK$Date,"%Y")) STOCK$Day = as.factor(format(STOCK$Date,"%a")) str(STOCK) 'data.frame': 1226 obs. of 9 variables: $ Date : Date, format: "2016-12-30" "2016-12-29" ... $ Open : int 42120 43000 43940 43140 43310 43660 43900 42910 42790 43350 ... $ High : int 42330 43220 43970 43700 43660 43840 44370 43630 43150 43550 ... $ Low : int 41700 42540 43270 43140 43090 43190 43610 42860 42740 42810 ... $ Close : int 41830 42660 43270 43620 43340 43480 44000 43620 43130 43130 ... $ Volume : int 610000 448400 339900 400100 358200 381600 658900 499400 358700 542000 ... $ Stock.Trading: num 2.56e+10 1.92e+10 1.48e+10 1.74e+10 1.55e+10 ... $ Year : Factor w/ 5 levels "2012","2013",..: 5 5 5 5 5 5 5 5 5 5 ... $ Day : Factor w/ 5 levels "๊ธˆ","๋ชฉ","์ˆ˜",..: 1 2 3 5 4 2 3 5 4 1 ... A1. ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ ๋งŒ๋“ค๊ธฐ 1. ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ ๋งŒ๋“ค๊ธฐ ์›ํ•˜๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋ ค๊ณ  ํ•˜๋‹ค ๋ณด๋ฉด, ๊ฐ€๋” ๋ฐ์ดํ„ฐ์˜ ์ง‘๊ณ„๋œ ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ด ๊ทธ๋ž˜ํ”„๋ฅผ ์ž‘์„ฑํ•ด์•ผ ๋  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. dplyr์„ ํ™œ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์ง‘๊ณ„ ๊ณผ์ •๊ณผ ์‹œ๊ฐํ™” ๊ณผ์ •์„ ํ•จ๊ป˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹œ๊ฐํ™”๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์ „์—, ์ž์ฃผ ์“ฐ์ด๋Š” dplyr ๋ช…๋ น์–ด๋“ค์„ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. group_by ์ง‘๊ณ„ ๊ธฐ์ค€ ๋ณ€์ˆ˜๋ฅผ ์ •ํ•ด์ฃผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. summarise ์ง‘๊ณ„ ๊ธฐ์ค€ ๋ณ€์ˆ˜ ๋ฐ ๋ช…๋ น์–ด์— ๋”ฐ๋ผ ์š” ์•ฝ ๊ฐ’์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. Group_Data = STOCK %>% group_by(Year, Day) %>% summarise(Mean = round(mean(Open)), Median = round(median(Open)), Max = round(max(Open)), Counts = length(Open)) ungroup group์œผ๋กœ ๋ฌถ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃน ํ•ด์ œ ์‹œ์ผœ์ฃผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ungroup์„ ํ•ด์ฃผ๋Š” ์ด์œ  group_by๋ฅผ ํ†ตํ•ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” error ๋ฐฉ์ง€๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. Error in mutate_impl(.data, dots) : Column Class canโ€™t be modified because itโ€™s a grouping variable group_by ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ต๋‹ˆ๋‹ค. [] ์•ˆ์— ์กฐ๊ฑด์„ ์ค„ ๋•Œ, ํ•จ์ˆ˜๊ฐ€ ์•ˆ ๋จนํžˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. Ungroup_Data = Group_Data %>% ungroup() str(Group_Data) Classes 'grouped_df', 'tbl_df', 'tbl' and 'data.frame': 25 obs. of 6 variables: $ Year : Factor w/ 5 levels "2012","2013",..: 1 1 1 1 1 2 2 2 2 2 ... $ Day : Factor w/ 5 levels "๊ธˆ","๋ชฉ","์ˆ˜",..: 1 2 3 4 5 1 2 3 4 5 ... $ Mean : num 17179 17161 17125 17099 17099 ... $ Median: num 17280 17220 17215 17200 17225 ... $ Max : num 21480 21300 21040 20320 20610 ... $ Counts: int 50 51 52 45 50 51 51 50 42 51 ... - attr(*, "groups")=Classes 'tbl_df', 'tbl' and 'data.frame': 5 obs. of 2 variables: ..$ Year : Factor w/ 5 levels "2012","2013",..: 1 2 3 4 5 ..$ .rows:List of 5 .. ..$ : int 1 2 3 4 5 .. ..$ : int 6 7 8 9 10 .. ..$ : int 11 12 13 14 15 .. ..$ : int 16 17 18 19 20 .. ..$ : int 21 22 23 24 25 ..- attr(*, ".drop")= logi TRUE str(Ungroup_Data) Classes 'tbl_df', 'tbl' and 'data.frame': 25 obs. of 6 variables: $ Year : Factor w/ 5 levels "2012","2013",..: 1 1 1 1 1 2 2 2 2 2 ... $ Day : Factor w/ 5 levels "๊ธˆ","๋ชฉ","์ˆ˜",..: 1 2 3 4 5 1 2 3 4 5 ... $ Mean : num 17179 17161 17125 17099 17099 ... $ Median: num 17280 17220 17215 17200 17225 ... $ Max : num 21480 21300 21040 20320 20610 ... $ Counts: int 50 51 52 45 50 51 51 50 42 51 ... str์„ ํ†ตํ•ด Group_Data์™€ Ungroup_Data๋ฅผ ๋น„๊ตํ•ด ๋ณด๋ฉด, ์ƒ๊ธด ๊ฒƒ์€ ๋˜‘๊ฐ™์ง€๋งŒ ๋‚ด๋ถ€ ํƒ€์ž…์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ธฐ๋ณธ์ ์œผ๋กœ data.frame ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. R์„ ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ์ž…์žฅ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ์‚ฌ์†Œํ•œ ์ฐจ์ด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜ ๋•Œ๋ฌธ์— ํฌ๊ฒŒ ๊ณ ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ž์ฃผ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๋ถ„์„์„ ์‹œ์ž‘ํ•  ๋•Œ๋Š” as.data.frame์„ ํ†ตํ•ด ๊ผญ data.frame ํ˜•ํƒœ์˜ strings๋กœ ๋ณ€๊ฒฝํ•ด ์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. count ์ง‘๊ณ„ ๊ธฐ์ค€์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ์˜ row ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด ์ค๋‹ˆ๋‹ค. summarise ๋‚ด์—์„œ ์“ฐ์ธ length์™€ ๋น„์Šทํ•œ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. Count_Data = STOCK %>% group_by(Year, Day) %>% count() A2. ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๊ธฐ 2. ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๊ธฐ ์›ํ•˜๋Š” ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. filter(), subset()์„ ํ™œ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๋ช…๋ น์–ด ๋ชจ๋‘ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋‹ˆ ํŽธํ•˜์‹  ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Subseted_Data = Group_Data %>% filter(Year == "2012") head(Subseted_Data) # A tibble: 5 x 6 # Groups: Year [1] Year Day Mean Median Max Counts <fct> <fct> <dbl> <dbl> <dbl> <int> 1 2012 ๊ธˆ 17179 17280 21480 50 2 2012 ๋ชฉ 17161 17220 21300 51 3 2012 ์ˆ˜ 17125 17215 21040 52 4 2012 ์›” 17099 17200 20320 45 5 2012 ํ™” 17099 17225 20610 50 A3. ๋ฐ์ดํ„ฐ ์ค‘๋ณต ์ œ๊ฑฐํ•˜๊ธฐ 3. ๋ฐ์ดํ„ฐ ์ค‘๋ณต ์ œ๊ฑฐํ•˜๊ธฐ ๋ฐ์ดํ„ฐ์— ๋‚ด์— ์กด์žฌํ•˜๋Š” ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๋ช…๋ น์–ด๋Š” distinct()๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. # ์ค‘๋ณต ๋ฐ์ดํ„ฐ ์ƒ์„ฑ SL = sample(1:nrow(Group_Data),500, replace = TRUE) Duplicated_Data = Group_Data[SL,] Group_Data๋Š” 25๊ฐœ์˜ ํ–‰(row)๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋‚˜, ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์—ฌ 500๊ฐœ๋กœ ์ฆ๊ฐ€์‹œ์ผœ๋ฒ„๋ฆฌ๋ฉด ์ค‘๋ณต ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌด์กฐ๊ฑด ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Duplicated_Data2 = Duplicated_Data %>% distinct(Year, Day, Mean, Median, Max, Counts) A4. ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ๋ฌด์ž‘์œ„ ์ถ”์ถœ 4. ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ๋ฌด์ž‘์œ„ ์ถ”์ถœ ๋ฐ์ดํ„ฐ์˜ ํ–‰์ด ๋„ˆ๋ฌด ๋งŽ์€ ๊ฒฝ์šฐ, ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ๋•Œ ์—ฐ์‚ฐ์†๋„๊ฐ€ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋ฉฐ, R์ด ๋‹ค์šด๋  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ, ๋ฌด์ž‘์œ„๋กœ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„, ๊ฐ€๋ณ๊ฒŒ ์‹œ๊ฐํ™”๋ฅผ ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ช…๋ น์–ด๋Š” sample_frac(), sample_n()์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. frac์€ size์—์„œ ๋น„์œจ(0 ~ 1)์„, n์€ ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ํ•  ๋ถ€๋ถ„์€ group ์ง€์ • ์—ฌ๋ถ€์— ๋”ฐ๋ผ sample ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. sample_frac() - ๊ทธ๋ฃน์ด ์ง€์ •๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ Sample_Frac_Gr = Group_Data %>% sample_frac(size = 0.4, replace = FALSE) ๊ฐ ์—ฐ๋„์—์„œ 2๊ฐœ์”ฉ ๊ท ํ˜• ์žˆ๊ฒŒ sampling ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฃน์ด ํ•ด์ œ๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ Sample_Frac_Un = Ungroup_Data %>% sample_frac(size = 0.4, replace = FALSE) sample_n() Sample_N_Gr = Group_Data %>% sample_n(size = 5, replace = FALSE) ๊ฐ ์—ฐ๋„๋ณ„ 2๊ฐœ์”ฉ ๋ฝ‘ํžŒ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. group์ด ์ ์šฉ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ํ˜„์žฌ ๋ฐ์ดํ„ฐ์—์„œ๋Š” size ๊ฐ’์„ 5๋ณด๋‹ค ํฐ ์ˆซ์ž๋ฅผ ์„ค์ •ํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. (๊ฐ ์—ฐ๋„๋ณ„ 5๊ฐœ์”ฉ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ) Sample_N_Un = Ungroup_Data %>% sample_n(size = 10, replace = FALSE) ์ œ๊ฐ€ ์ž…๋ ฅํ•œ size ๊ฐ’์— ๋”ฐ๋ผ์„œ ๋ฌด์ž‘์œ„๋กœ 10๊ฐœ๊ฐ€ ๋ฝ‘ํžŒ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ํ•œ ํ›„, ๋ฐ์ดํ„ฐ์˜ ํ–‰(row)์ด 25๋กœ ๋Œ์•„์˜จ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A5. ์ •ํ•ด์ง„ Index์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๊ธฐ 5. ์ •ํ•ด์ง„ Index์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๊ธฐ ๋ฌด์ž‘์œ„ ์ถ”์ถœ์ด ์•„๋‹Œ, ์ˆœ์„œ๋Œ€๋กœ ๋ฝ‘๊ฑฐ๋‚˜ ์›ํ•˜๋Š” ๊ตฌ๊ฐ„๋งŒ ์„ค์ •ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„๋‚ด๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ช…๋ น์–ด๋Š” slice(), top_n()์ด ์žˆ์Šต๋‹ˆ๋‹ค. slice() slice()๋Š” Index๋ฅผ ์ง์ ‘ ์„ค์ •ํ•จ์œผ๋กœ, ์›ํ•˜๋Š” ๊ตฌ๊ฐ„๋งŒ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ Dataset์€ ungroup()์ด ๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ ์ง„ํ–‰ํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. Slice_Data = Ungroup_Data %>% slice(1:10) top_n() top_n()์€ ์„ค์ •ํ•ด ์ค€ ๋ณ€์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์žฅ ๊ฐ’์ด ๋†’์€ n ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. Top_n_Data = Ungroup_Data %>% top_n(5, Mean) # Mean์ด ๊ฐ€์žฅ ๋†’์€ 5๊ฐœ ๋ฐ์ดํ„ฐ ์ถ”์ถœ A6. ๋ฐ์ดํ„ฐ ์ •๋ ฌํ•˜๊ธฐ 6. ๋ฐ์ดํ„ฐ ์ •๋ ฌํ•˜๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ๋ณ€์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. arrange()๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ungroup()์ด ์„ค์ •๋œ ๋ฐ์ดํ„ฐ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ group_by()๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ๊ฒฝ์šฐ, Year ๋ณ„๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋ฆ„์ฐจ์ˆœ # ungroup์œผ๋กœ ๊ทธ๋ฃน ์ง€์ •์„ ํ•ด์ œํ•œ ๋ฐ์ดํ„ฐ Asce_Data = Ungroup_Data %>% arrange(Mean) # Mean์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ์—ฐ๋„์— ์ƒ๊ด€์—†์ด ์ •๋ ฌ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๋ฆผ์ฐจ์ˆœ ๋‚ด๋ฆผ์ฐจ์ˆœ์€ ๋ณ€์ˆ˜์— โ€™-โ€™๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. Desc_Data = Ungroup_Data %>% arrange(-Mean) A7. ์›ํ•˜๋Š” ๋ณ€์ˆ˜(Colomn)๋งŒ ๋ฝ‘์•„๋‚ด๊ธฐ 7. ์›ํ•˜๋Š” ๋ณ€์ˆ˜(Colomn)๋งŒ ๋ฝ‘์•„๋‚ด๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ธ๋“ค๋ง ํ•  ๋•Œ, ๋ชจ๋“  ๋ณ€์ˆ˜๋“ค์„ ๊ฐ€์ ธ๊ฐˆ ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ select(), select_if()๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ๋ณ€์ˆ˜๋“ค์„ ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. select() select()๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ๋ฝ‘์•„๋„ ๋˜๊ณ , ๋ณ€์ˆ˜๋ช…์„ ์ž…๋ ฅํ•ด์„œ ๋ฝ‘์•„๋‚ด๋„ ๋ฉ๋‹ˆ๋‹ค. ํŽธํ•˜์‹  ๋ฐฉ๋ฒ•๋Œ€๋กœ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. # Index ํ™œ์šฉ Select_Data = Group_Data %>% select(1:2) # Column ๋ช… ํ™œ์šฉ Select_Data = Group_Data %>% select(Year, Day) select_if() select_if()๋ฅผ ํ†ตํ•ด ๋ฝ‘๋Š” ์กฐ๊ฑด์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋”ฐ๋ผ ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # Factor ๋ณ€์ˆ˜๋งŒ ๋ฝ‘๊ธฐ Select_if_Data1 = Group_Data %>% select_if(is.factor) ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด Factor์ธ ๋ณ€์ˆ˜๋งŒ ๋ฝ‘์•„์„œ ๋ฐ์ดํ„ฐ ์…‹์„ ๋งŒ๋“  ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # integer ๋ณ€์ˆ˜๋งŒ ๋ฝ‘๊ธฐ Select_if_Data2 = Group_Data %>% select_if(is.integer) A8. ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜ ๋งŒ๋“ค๊ธฐ ํ˜น์€ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๊ธฐ 8. ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜ ๋งŒ๋“ค๊ธฐ ํ˜น์€ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง ๊ณผ์ •์—์„œ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜(Column)๋ฅผ ๋งŒ๋“ค๊ณ ์ž ํ•  ๋•Œ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ๋Š” mutate(), mutate_if(), mutate_at()์ด ์žˆ์Šต๋‹ˆ๋‹ค. mutate() mutate()๋Š” ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋ฅผ ๋ช…๋ น์–ด์— ๋”ฐ๋ผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. Mutate_Data = STOCK %>% mutate(Divided = round(High/Low, 2)) %>% select(Date, High, Low, Divided) mutate_if() mutate_if()์€ ์ง€์ •ํ•ด ์ค€ ๋ชจ๋“  ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ๊ณ„์‚ฐ์‹์„ ์ ์šฉ์‹œ์ผœ ์ค๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ, as.factor() ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ํ•˜๋‚˜์”ฉ ์ ์šฉ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์€ ์‹œ๊ฐ„์„ ๋งŽ์ด ์žก์•„๋จน๋Š” ๋ง‰๋…ธ๋™ ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ, mutate_if()๋ฅผ ํ†ตํ•ด ํ•œ ๋ฒˆ์— ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ณ€๊ฒฝ์‹œ์ผœ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. # integer ํƒ€์ž… ๋ณ€์ˆ˜๋ฅผ ๋ชจ๋‘ numeric์œผ๋กœ ๋ณ€๊ฒฝ Mutate_If_Data = STOCK %>% mutate_if(is.integer, as.numeric) mutate_at() mutate_at()์€ ์ง€์ •ํ•œ ๋ณ€์ˆ˜๋“ค์— ๋Œ€ํ•ด ๊ณ„์‚ฐ์‹์„ ์ ์šฉ์‹œํ‚ค๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. Mutate_At_Data = STOCK %>% mutate_at(vars(-Date,-Year,-Day),log) %>% select_if(is.numeric) Date, Year, Day ๋ณ€์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ๋ณ€์ˆ˜๋“ค์€ ๋ชจ๋‘ log ๋ณ€ํ™˜์ด ์ ์šฉ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€ dplyr์˜ ํ™œ์šฉ๋ฒ•์„ ๋‹ค๋ฃจ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ๋‹ค๋ฃฌ ๊ฒƒ์€ ์•„๋‹ˆ๊ณ , ์ œ ๊ฒฝํ—˜์ƒ ๋งŽ์ด ์ผ๋˜ ๊ธฐ๋Šฅ๋“ค์„ ์œ„์ฃผ๋กœ ์„ ๋ณ„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. dplyr๋กœ ํ•ธ๋“ค๋ง ํ•œ ํ›„, %>%๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ”๋กœ ggplot() ๋ช…๋ น์–ด์™€ ์—ฐ๊ณ„ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งค์šฐ ํšจ์œจ์ ์œผ๋กœ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A9. ์—ฐ์Šต๋ฌธ์ œ 9. ์—ฐ์Šต๋ฌธ์ œ HR ๋ฐ์ดํ„ฐ์— Factor ๋ณ€์ˆ˜๋“ค๋งŒ ๋ฝ‘์•„, HR_Factor๋ผ๋Š” ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„, Numeric ๋ณ€์ˆ˜๋“ค๋งŒ ๋ฝ‘์•„ HR_Numeric ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์‹œ์˜ค. HR ๋ฐ์ดํ„ฐ์˜ sales, left ๋ณ€์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ satisfaction_level, last_evaluation์˜ ํ‰๊ท , ์ค‘์œ„์ˆ˜, ํ‘œ์ค€ํŽธ์ฐจ ๊ฐ’์„ ๊ณ„์‚ฐํ•ด์„œ HR_summarise๋ผ๋Š” ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ๋งŒ๋“œ์‹œ์˜ค. Ch8. R ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” R์—๋Š” ggplot2๋ผ๋Š” ๊ฐ•๋ ฅํ•œ ์‹œ๊ฐํ™” ํŒจํ‚ค์ง€๊ฐ€ ์žˆ์œผ๋ฉฐ, ์š”์ฆ˜ ์—ฌ๋Ÿฌ ํ•ด์™ธ ์ €๋„์—์„œ๋Š” ggplot2๋ฅผ ์ด์šฉํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ์‹œ๊ฐํ™” ๋„๊ตฌ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ด ggplot2๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์‹œ๊ฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋‹ค๋ฃจ๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. A1. ggplot2 ํ…Œ๋งˆ ์ˆ˜์ • 1. ggplot2 ํ…Œ๋งˆ ์ˆ˜์ • ggplot2()๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํšŒ์ƒ‰ ๋ฐ”ํƒ•์— ํฐ์ƒ‰ ๊ฒฉ์ž์„ ์ด ๊ธฐ๋ณธ ๋ฐฐ๊ฒฝ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ด๋ฅผ ์‹ซ์–ดํ•˜๋Š” ๋ถ„๋“ค๋„ ๋ถ„๋ช… ๊ณ„์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ํ…Œ๋งˆ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋ฉด ์›ํ•˜์‹œ๋Š” ์Šคํƒ€์ผ๋กœ ๊ทธ๋ฆด ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…Œ๋งˆ์˜ ์ข…๋ฅ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. library(ggplot2) library(ggthemes) HR = read.csv('F:/Drop box/DATA SET/HR_comma_sep.csv') HR$left = as.factor(HR$left) HR$salary = factor(HR$salary, levels = c("low","medium","high")) # Classic Theme ggplot(HR, aes(x=salary)) + geom_bar(aes(fill=salary)) + theme_classic() # BW Theme ggplot(HR, aes(x=salary)) + geom_bar(aes(fill=salary)) + theme_bw() ์ œ๊ฐ€ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํ…Œ๋งˆ 2๊ฐœ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋‹ค๋ฅธ ๋‹ค์–‘ํ•œ ํ…Œ๋งˆ๋„ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์œผ๋‹ˆ, ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ggthemes ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๋ฉด ๋” ๋งŽ์€ ํ…Œ๋งˆ๋ฅผ ์ ์šฉ์‹œํ‚ฌ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Graph = ggplot(HR, aes(x=salary)) + geom_bar(aes(fill=salary)) Graph + theme_bw() + ggtitle("Theme_bw") Graph + theme_classic() + ggtitle("Theme_classic") Graph + theme_dark() + ggtitle("Theme_dark") Graph + theme_light() + ggtitle("Theme_light") Graph + theme_linedraw() + ggtitle("Theme_linedraw") Graph + theme_minimal() + ggtitle("Theme_minimal") Graph + theme_test() + ggtitle("Theme_test") Graph + theme_void() + ggtitle("Theme_vold") A2. ๋ฒ”๋ก€ ์ œ๋ชฉ ์ˆ˜์ • 2. ๋ฒ”๋ก€ ์ œ๋ชฉ ์ˆ˜์ • ๋ฒ”๋ก€๋Š” ์ˆ˜์ •ํ•˜๊ธฐ๋Š” ๊ท€์ฐฎ์ง€๋งŒ, ๊ทธ๋ž˜ํ”„๋ฅผ ์ฒ˜์Œ ๋ณด๋Š” ์‚ฌ๋žŒ์ด ๊ฐ€์žฅ ๋จผ์ € ํ™•์ธํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์‹ ๊ฒฝ์„ ์ข€ ์จ์ค˜์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒ”๋ก€๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฒ”๋ก€ ์ œ๋ชฉ ์ˆ˜์ • ggplot(HR, aes(x=salary)) + geom_bar(aes(fill=salary)) + theme_bw() + labs(fill = "๋ฒ”๋ก€ ์ œ๋ชฉ ์ˆ˜์ •(fill)") ggplot(HR, aes(x = salary)) + geom_bar(aes(col = salary)) + theme_bw() + labs(col = "๋ฒ”๋ก€ ์ œ๋ชฉ ์ˆ˜์ •(col)") ๋จผ์ € ๋ฒ”๋ก€ ์ œ๋ชฉ์„ ์ˆ˜์ •ํ•˜๊ธฐ์— ์•ž์„œ ์ƒ‰์„ ์ง€์ •ํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ดํ•ด๋ฅผ ํ•˜์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” fill, ๋‚˜๋จธ์ง€ ํ•˜๋‚˜๋Š” col์ž…๋‹ˆ๋‹ค. fill์€ ๋ง ๊ทธ๋Œ€๋กœ โ€˜์ƒ‰ ์ฑ„์šฐ๊ธฐโ€™ ๊ฐœ๋…์œผ๋กœ ๋ฉด์ ์ด ์žˆ๋Š” ๋„ํ˜•์˜ ์ƒ‰์„ ๋ฐ”๊ฟ€ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ col์€ ์ƒ‰์„ ๋ฐ”๊ฟ”๋งŒ ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ์ , ๊ธ€์”จ, ์„  ๋“ฑ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์œ„ 2๊ทธ๋ž˜ํ”„๋ฅผ ์‚ดํŽด๋ณด์‹œ๋ฉด ํ•˜๋‚˜๋Š” fill์ด๊ณ , ๋‚˜๋จธ์ง€ ํ•˜๋‚˜๋Š” col๋กœ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๋ˆˆ์— ๋ด๋„ ์ฐจ์ด์ ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒ”๋ก€ ์ œ๋ชฉ์„ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ์€ labs()๋ฅผ ์ด์šฉํ•˜๋ฉด ๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒ”๋ก€ ์œ„์น˜ ์ˆ˜์ • ๋ฒ”๋ก€ ์œ„์น˜๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ theme() ์˜ต์…˜์—์„œ legend.positon์„ ํ†ตํ•ด ์œ„์น˜๋ฅผ ์กฐ์ •์‹œ์ผœ์ค„ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Graph + theme(legend.position = "top") Graph + theme(legend.position = "bottom") Graph + theme(legend.position = c(0.9,0.7)) Graph + theme(legend.position = 'none') ๋ฒ”๋ก€ ํ…Œ๋‘๋ฆฌ ์„ค์ • Graph + theme(legend.position = "bottom") Graph + theme(legend.position = "bottom", legend.box.background = element_rect(), legend.box.margin = margin(1, 1, 1, 1)) ๋ฒ”๋ก€ ํ…Œ๋‘๋ฆฌ๋ฅผ ์„ค์ •ํ•˜๋ฉด ๊น”๋”ํ•˜๊ฒŒ ๋ฒ”๋ก€๋ฅผ ์ •๋ฆฌํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. A3. ์ถ• ๋ณ€๊ฒฝ 3. ์ถ• ๋ณ€๊ฒฝ ์ถ• ๋ณ€๊ฒฝ์˜ ๊ฒฝ์šฐ, ๊ฐ€์žฅ ๋จผ์ € ์‹ ๊ฒฝ ์จ์•ผ ํ•  ๋ถ€๋ถ„์€ ์ถ•์— ์„ค์ •๋œ ๋ณ€์ˆ˜๊ฐ€ Discrete ์ธ์ง€ Continuous ์ธ์ง€ ํ™•์ธ๋ถ€ํ„ฐ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์•ž์„œ ๋งํ–ˆ๋“ฏ์ด ๋ณ€์ˆ˜์˜ ์ฒ™๋„์— ๋Œ€ํ•œ ์ดํ•ด๋Š” ๋ถ„์„๋ฟ ์•„๋‹ˆ๋ผ ์‹œ๊ฐํ™”์—์„œ๋„ ํฐ ๋ถ€๋ถ„์„ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰ ์‚ฌ์‹ค์ƒ ๋ถ„์„ ์ „์ฒด์— ํ•ด๋‹น๋˜๋Š” ๊ฒฝ์šฐ์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋จผ์ € ์ •๋ฆฌํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ggplot(HR, aes(x = salary)) + geom_bar(aes(fill = salary)) + theme_bw() + scale_x_discrete(expand = c(0,0), labels = c("ํ•˜","์ค‘","์ƒ")) + scale_y_continuous(expand = c(0,0),breaks = seq(0,8000, by = 1000)) ggplot(HR, aes(x = salary)) + geom_bar(aes(fill = salary)) + theme_bw() + scale_x_discrete(expand = c(0,0), labels = c("ํ•˜","์ค‘","์ƒ")) + scale_y_continuous(expand = c(0,0),breaks = seq(0,8000, by = 1000)) + scale_fill_discrete(labels = c("ํ•˜","์ค‘","์ƒ")) ์œ„ ๊ทธ๋ž˜ํ”„์—์„œ x์ถ•์€ ์ด์‚ฐํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์—, scale_x_discrete๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ถ• ์ˆ˜์ •์„ ํ•ด์ฃผ๋ฉฐ, y ์ถ•์€ ์ง‘๊ณ„ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ์—ฐ์†ํ˜• ์ˆ˜์น˜์ด๊ธฐ ๋•Œ๋ฌธ์— scale_y_continuous๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ˆ˜์ •์„ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ expand ์˜ต์…˜์€ ๊ทธ๋ž˜ํ”„๋ฅผ ํ…Œ๋‘๋ฆฌ์— ๋งž์ถ”์–ด ๊ทธ๋ฆฌ๊ฒŒ ํ•ด์ฃผ๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ ๊ทธ๋ ธ๋˜ ๊ทธ๋ž˜ํ”„๋“ค์„ ์‚ดํŽด๋ณด๋ฉด ์กฐ๊ธˆ์”ฉ ์ถ•(ํ…Œ๋‘๋ฆฌ)๋กœ๋ถ€ํ„ฐ ๋ถ• ๋– ์žˆ๋‹ค๋Š” ์ ์„ ๋ฐœ๊ฒฌํ•˜์‹ค ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์„ ์˜ต์…˜์œผ๋กœ ์ˆ˜์ •์„ ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด์‚ฐํ˜•(Discrete)์—์„œ๋Š” labels ์˜ต์…˜์„ ํ†ตํ•ด ์ถ•์„ ๋ณ€๊ฒฝ์‹œ์ผœ์ค„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์—ฐ์†ํ˜•(Continuous)์€ breaks๋ฅผ ํ†ตํ•ด ์ถ•์„ ๋ณ€๊ฒฝ์‹œ์ผœ ์ค„ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒ”๋ก€์—์„œ์˜ ํ‘œ์‹œ๋Š” labels๋ฅผ ๋ฐ”๊พธ์–ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์€ scale_fill_discrete()๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ƒ‰์ด col ์˜ต์…˜์œผ๋กœ ๊ตฌ๋ถ„์ด ๋˜์—ˆ๋‹ค๋ฉด, scale_color_discrete()๋ฅผ ํ†ตํ•ด ๋ฐ”๊ฟ”์ค„ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ•๋ฒ”์œ„ ์„ค์ • ์ถ• ๋ฒ”์œ„ ์„ค์ •์€ ๊ทธ๋ž˜ํ”„๋ฅผ ํ‘œํ˜„ํ•  ๋ฒ”์œ„๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ggplot(HR, aes(x = salary)) + geom_bar(aes(fill = salary)) + theme_bw() + ylim(0,5000) ggplot(HR, aes(x = salary)) + geom_bar(aes(fill = salary)) + theme_bw() + ylim(0,13000) y ์ถ•์— ylim(0,5000)์„ ์ฃผ๋ฉด y ์ถ• ๊ธฐ์ค€ 0 ~ 5000๊นŒ์ง€๋งŒ ๋‚˜ํƒ€๋‚ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. low, medium์€ 5000๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ž˜ํ”„๊ฐ€ ์ž˜๋ ค์„œ ๋‚˜์˜ค์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ‰ ๋ณ€๊ฒฝ R์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ‰ ๋ถ„๋ฅ˜๊ฐ€ ์ง€์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ€๋” ์ƒ‰์„ ์ง์ ‘ ๋ณ€๊ฒฝ์„ ํ•ด์ฃผ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. Discrete ๋ณ€์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ‰์„ ์ง€์ •ํ•  ๋•Œ๋Š” scale_fill_manual() ํ˜น์€ scale_color_manual()์„ ํ™œ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ์ƒ‰ ๋ฐฐ์น˜๋ฅผ ๋ฐฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ggplot(HR, aes(x = salary)) + geom_bar(aes(fill = salary)) + theme_bw() + scale_fill_manual(values = c('red','royalblue','tan')) ggplot(HR, aes(x = salary)) + geom_bar(aes(fill = salary), alpha = 0.4) + theme_bw() + scale_fill_manual(values = c('red','royalblue','tan')) scale_fill_manual์—์„œ ์›ํ•˜๋Š” ์ƒ‰์ƒ์„ ๋ฐ”๊ฟ”์ค„ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. alpha๋ฅผ ์ด์šฉํ•˜๋ฉด ์ƒ‰์„ ์—ฐํ•˜๊ฒŒ ์น ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„ ๋ถ„ํ•  ๋ฐ ๋Œ€์นญ ์ด๋™ ๊ทธ๋ž˜ํ”„ ๋ถ„ํ•  ๋ฐ ๋Œ€์นญ์ด๋™์€ ๋‹ค์Œ์˜ ์˜ต์…˜ ์ถ”๊ฐ€๋ฅผ ํ†ตํ•ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ggplot(HR, aes(x = salary)) + geom_bar(aes(fill = salary), alpha = 0.4) + theme_bw() + scale_fill_manual(values = c('red','royalblue','tan')) + coord_flip() ggplot(HR, aes(x = salary)) + geom_bar(aes(fill = salary)) + theme_bw() + scale_fill_manual(values = c('red','royalblue','tan')) + guides(fill = FALSE) + facet_wrap(~left, ncol = 1) ์ฐธ๊ณ ๋กœ guides()๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋ฒ”๋ก€๋ฅผ ์—†์• ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. coord_flip()์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด ๊ทธ๋ž˜ํ”„๊ฐ€ x์ถ•, y ์ถ•์ด ๋Œ€๊ฐ์„  ๋Œ€์นญ์ด ๋˜์–ด ๊ทธ๋ž˜ํ”„๊ฐ€ ์ž‘์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค. facet_wrap()์€ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ถ„ํ• ํ•˜๊ณ  ์‹ถ์€ ๋ณ€์ˆ˜์˜ ๊ธฐ์ค€์„ ์ž…๋ ฅํ•ด ์ค€ ๋‹ค์Œ, ncol ์˜ต์…˜์„ ํ†ตํ•ด์„œ ๋ช‡ ๊ฐœ์˜ ์—ด๋กœ ํ‘œํ˜„ํ• ์ง€ ์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ๋Š” ncol = 1์„ ์ฃผ์—ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๊ทธ๋ž˜ํ”„๋ฅผ ์—ด 1๊ฐœ๋กœ ๋‚˜์—ดํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ธ€์ž ํฌ๊ธฐ, ๊ฐ๋„ ์ˆ˜์ • ggplot(HR, aes(x = salary)) + geom_bar(aes(fill = salary)) + theme_bw() + scale_fill_manual(values = c('red','royalblue','tan')) + coord_flip() + theme(legend.position = 'none', axis.text.x = element_text(size = 15, angle = 90), axis.text.y = element_text(size = 15), legend.text = element_text(size = 15)) Ch9. ggplot2๋ฅผ ํ™œ์šฉํ•œ ๋‹ค์–‘ํ•œ ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ggplot2๋กœ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„๋“ค์˜ ์ข…๋ฅ˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. library(ggplot2) library(dplyr) STOCK = read.csv("D:\Drop box\DATA SET(Drop box)\uniqlo.csv") STOCK$Date = as.Date(STOCK$Date) STOCK$Year = as.factor(format(STOCK$Date,"%Y")) STOCK$Day = as.factor(format(STOCK$Date,"%a")) Group_Data = STOCK %>% group_by(Year, Day) %>% dplyr::summarise(Mean = round(mean(Open)), Median = round(median(Open)), Max = round(max(Open)), Counts = length(Open)) A1. Bar Chart ๋ง‰๋Œ€๋„ํ‘œ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ด์‚ฐํ˜• ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ์‹œ๊ฐํ™”๋ฅผ ํ•˜๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ y ์ถ•์€ ๋”ฐ๋กœ ์„ค์ •ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์ด์‚ฐํ˜• ๋ณ€์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ x์ถ• ๋ณ€์ˆ˜ 1๊ฐœ๋กœ๋งŒ ๊ทธ๋ฆฌ๋Š” ๊ฒฝ์šฐ ggplot(Group_Data) + geom_bar(aes(x = as.factor(Counts),fill = .. count..)) + xlab("") + ylab("") + scale_fill_gradient(low = "#CCE5FF", high = "#FF00FF") + theme_classic() + ggtitle("Continuous Color") ggplot(Group_Data) + geom_bar(aes(x = as.factor(Counts),fill = Day),alpha = 0.4) + xlab("") + ylab("") + theme_classic() + ggtitle("Discrete Color") ์ƒ‰ ๊ตฌ๋ถ„ ํฌ์ง€์…˜์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ggplot(Group_Data) + geom_bar(aes(x = as.factor(Counts),fill = Day), alpha = 0.4, position = "dodge") + xlab("") + ylab("") + theme_classic() + ggtitle("Discrete Color\n position Dodge") x์ถ•, y ์ถ• 1๊ฐœ์”ฉ ์ด ๋ณ€์ˆ˜ 2๊ฐœ๋กœ ๊ทธ๋ฆฌ๋Š” ๊ฒฝ์šฐ, stat = โ€˜identityโ€™ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ggplot(Group_Data) + geom_bar(aes(x = Year, y = Mean, fill = Day), stat = 'identity') + scale_fill_manual(values = c("#C2DAEF","#C2EFDD","#BBAAE9", "#E9F298","#FABDB3")) + theme_classic() A2. Histogram ํžˆ์Šคํ† ๊ทธ๋žจ์€ ๋ง‰๋Œ€๋„ํ‘œ์™€ ๋งค์šฐ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒผ์ง€๋งŒ, ์—ฐ์†ํ˜• ๋ณ€์ˆ˜๋ฅผ ์‹œ๊ฐํ™”ํ•˜๋Š” ์ ์—์„œ ๋ง‰๋Œ€๋„ํ‘œ์™€ ํฐ ์ฐจ์ด์ ์„ ์ง€๋‹ˆ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฐ์†ํ˜• ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ๊ตฌ๊ฐ„์„ ๋‚˜๋ˆ„์–ด, ๋ง‰๋Œ€๋„ํ‘œ์ฒ˜๋Ÿผ ์ง‘๊ณ„๋œ ๊ฐ’์„ ์ถœ๋ ฅํ•˜์—ฌ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํžˆ์Šคํ† ๊ทธ๋žจ์˜ ๋ณ€์ˆ˜ ๊ตฌ๊ฐ„์„ ์กฐ์ •ํ•˜๋Š” ๋ช…๋ น์–ด๋Š” binwidth๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ggplot(STOCK) + geom_histogram(aes(x = High , fill = .. x.. ), binwidth = 1000) + scale_fill_gradient(low = "#CCE5FF", high = "#FF00FF") + theme_classic() + labs(fill = "Labels Name") ggplot(STOCK) + geom_histogram(aes(x = High , fill = Day), binwidth = 1000, alpha = 0.4) + theme_classic() + labs(fill = "Labels Name") ggplot(STOCK) + geom_histogram(aes(x = High , fill = Day), binwidth = 1000, alpha = 0.4, position = "dodge") + theme_classic() + labs(fill = "Labels Name") A3. Density plot ํžˆ์Šคํ† ๊ทธ๋žจ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„ ๋ฉด์ ์ด ๊ฐ๊ฐ์˜ ๋น„์œจ์ด ๋˜๋„๋ก ๋งž์ถ”์–ด์ค๋‹ˆ๋‹ค. ggplot(STOCK) + geom_density(aes(x = High)) + theme_classic() + labs(fill = "Labels Name") ggplot(STOCK) + geom_density(aes(x = High , fill = Day),alpha = 0.4) + theme_classic() + labs(fill = "Labels Name") + theme(axis.text.x = element_text(size = 9, angle = 45, hjust = 1)) A4. Boxplot & Jitter plot ๋ฐ•์Šค ํ”Œ๋กฏ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์•ฝํ•˜๋Š” ๋ฐ์— ์žˆ์–ด ๋งค์šฐ ์œ ์šฉํ•œ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ๋จผ์ €, ๋ฐ•์Šค ํ”Œ๋กฏ์— ๋Œ€ํ•ด ์ดํ•ด๋ฅผ ํ•˜์‹ค ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ•์Šค ํ”Œ๋กฏ์€ ๋ถ„์œ„์ˆ˜(quantile)๋กœ ์ž‘์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค. ์ƒ์ž ๋‚ด๋ถ€์— ์œ„์น˜ํ•œ ์ค„์€ ์ค‘์œ„์ˆ˜(Median)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ƒ์ž์˜ ๋ฐ‘๋ณ€, ์œ—๋ณ€์€ ๊ฐ๊ฐ 1, 3๋ถ„ ์œ„์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ƒ์ž๋ฅผ ๊ฐ์‹ธ๋Š” ํ…Œ๋‘๋ฆฌ๋Š” ์šธํƒ€๋ฆฌ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„ ์„ ๋ฒ—์–ด๋‚˜๋ฉด, ์ฃผ๋กœ ์ด์ƒ์น˜(Outlier)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ•์Šค ํ”Œ๋กฏ์„ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ x์ถ•์€ Descrete ๋ณ€์ˆ˜๋ฅผ, y ์ถ•์—๋Š” Continuous ๋ณ€์ˆ˜๋ฅผ ๋ฐฐ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ggplot(STOCK) + geom_boxplot(aes(x = Day, y = Volume, fill = Day), alpha = 0.4, outlier.color = 'red') + theme_bw() ggplot(STOCK) + geom_boxplot(aes(x = Day, y = Volume, fill = Day), alpha = 0.2, outlier.color = 'red') + geom_jitter(aes(x= Day, y= Volume, col = Day),alpha = 0.1) + theme_bw() A5. Violin plot ๋ฐ•์Šค ํ”Œ๋กฏ๊ณผ ๋น„์Šทํ•œ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ggplot(STOCK) + geom_violin(aes(x = Day, y = Volume, fill = Day), alpha = 0.4) + theme_bw() A6. Scatter plot ์‚ฐ์ ๋„๋Š” ๋ฐ์ดํ„ฐ์˜ ์ƒ๊ด€๊ด€๊ณ„๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ์— ๋งค์šฐ ์œ ์šฉํ•œ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„์—์„œ shape, size ๋“ฑ์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ggplot(STOCK) + geom_point(aes(x = Open, y = Stock.Trading, col = High, size = log(Volume), shape = Year)) + scale_color_gradient(low = "#CCE5FF", high = "#FF00FF") + scale_shape_manual(values = c(19,20,21,22,23)) + labs( col = "Color", shape = "Shape", size = "Size" ) + theme_bw() + theme(axis.text.x = element_blank()) A7. Smooth plot geom_smooth๋Š” ํšŒ๊ท€์„ ์„ ๊ทธ๋ ค์ค๋‹ˆ๋‹ค. ggplot(STOCK) + geom_smooth(aes(x = Open, y = Stock.Trading), method = 'lm', col = '#8A8585') + theme_bw() ggplot(STOCK) + geom_point(aes(x = Open, y = Stock.Trading, ),col = 'royalblue', alpha = 0.2) + geom_smooth(aes(x = Open, y = Stock.Trading), method = 'lm', col = '#8A8585') + theme_bw() A8. abline, vline, h line ๊ทธ๋ž˜ํ”„์— ํ‰ํ–‰์„ , ์ˆ˜์ง์„ , ๋Œ€๊ฐ์„ ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ggplot(NULL) + geom_vline(xintercept = 10, linetype = 'dashed', col = 'royalblue', size = 3) + geom_h line(yintercept = 10, linetype = 'dashed', col = 'royalblue', size = 3) + geom_abline(intercept = 0, slope = 1, col = 'red', size = 3) + theme_bw() A9. Step plot ๊ณ„๋‹จ<NAME>์˜ ๊ทธ๋ž˜ํ”„๋กœ, ๊ฐ’์˜ ์ฆ๊ฐ€๋Ÿ‰์„ ๋‚˜ํƒ€๋‚ผ ๋•Œ ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค. Hazard_Ratio = c(0.1,0.3,0.4,0.45,0.49,0.52,0.6,0.65,0.75,0.8,0.95) Survival_Time = c(1,2,3,4,5,6,7,8,9,10,11) ggplot(NULL) + geom_step(aes(x = Survival_Time, y = Hazard_Ratio),col = 'red') + scale_x_continuous(breaks = Survival_Time) + theme_classic() B1. Density 2d plot ๋ฐ€๋„ ๊ทธ๋ž˜ํ”„๋ฅผ 2๊ฐœ์˜ ์ฐจ์›์œผ๋กœ ๊ทธ๋ฆฌ๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ggplot(STOCK) + geom_point(aes(x= log(Stock.Trading), y = Open, col = Open)) + geom_density2d(aes(x= log(Stock.Trading), y = Open)) + scale_color_gradient(low = "#E93061", high = "#574449") + theme_bw() B2. Text plot ์‚ฐ์ ๋„์™€ ๊ฑฐ์˜ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๊ทธ๋ƒฅ ์ ์ด ์•„๋‹Œ ์ง€์ •ํ•œ ๊ธ€์ž๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. SL = sample(1:nrow(STOCK), 200, replace = FALSE) ggplot(STOCK[SL,]) + geom_text(aes(x = Date , y= Open, label = Open, col = Open), size = 2) + scale_color_gradient(low = "#CCE5FF", high = "#0080FF") + theme_bw() B3. Line plot & Timeseries plot ์„  ๊ทธ๋ž˜ํ”„์—์„œ๋Š” group์ด๋ผ๋Š” ์˜ต์…˜์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ggplot(STOCK) + geom_line(aes(x= Year, y = Open),group = 1) + theme_bw() ์ด๋Ÿฐ ๊ฒฝ์šฐ ์„  ๊ทธ๋ž˜ํ”„๋Š” ์ž˜๋ชป ๊ทธ๋ ค์ง„ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์„ ์€ ๋™์ผํ•œ x์—์„œ ํ•˜๋‚˜์˜ y ๊ฐ’๋งŒ์„ ๊ฐ€์ ธ์•ผ ํ•˜๋Š” ํŠน์ง•์„ ์ง€๋‹ˆ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์„  ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๊ธฐ ์ „์—๋Š” ํ•ญ์ƒ ์ด ์ ์„ ์œ ์˜ํ•ด์„œ ์š” ์•ฝ ๊ฐ’์„ ๋งŒ๋“ค์–ด ์ค€ ํ›„, ๊ทธ๋ž˜ํ”„๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ggplot(STOCK) + geom_line(aes(x = Date, y = Open), group = 1) + theme_bw() ggplot(STOCK) + geom_line(aes(x = Date, y = Open, col = Year, group = Year)) + theme_bw() B4. Error bar plot ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ๋ฒ”์œ„๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์˜ค์ฐจ ๋ฒ”์œ„ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ํ•™์ˆ  ์ €๋„์—์„œ๋Š” ๊ฝค๋‚˜ ์ž˜ ์“ฐ์ด๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ point, boxplot๊ณผ ํ˜ผํ•ฉํ•  ๋•Œ ์ฃผ๋กœ ์“ฐ์ž…๋‹ˆ๋‹ค. DF = STOCK %>% group_by(Year) %>% summarise(Min = min(High), Max = max(High)) ggplot(NULL) + geom_boxplot(data = STOCK, aes(x = Year, y = High, fill = Year)) + geom_errorbar(data = DF, aes(x = Year, ymin = Min, ymax = Max)) + theme_classic() B5. Corrplot ์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ํžˆํŠธ๋งต<NAME>์œผ๋กœ ํ•œ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. Cor_matrix = cor(iris[,1:4]) # iris๋Š” R ๊ธฐ๋ณธ ๋‚ด์žฅ ๋ฐ์ดํ„ฐ library(corrplot) corrplot(Cor_matrix , method = "color", outline = T, addgrid.col = "darkgray", order="hclust", addrect = 4, rect.col = "black", rect.lwd = 5, cl.pos = "b", tl.col = "indianred4", tl.cex = 1, cl.cex = 1, addCoef.col = "white", number.digits = 2, number.cex = 1, col = colorRampPalette(c("darkred","white","midnightblue"))(100)) B6. Heatmap ์œ„ Corrplot๊ณผ ๋น„์Šทํ•œ ํ˜•ํƒœ์˜ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ƒ‰์˜ ์˜จ๋„๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ggplot(Group_Data) + geom_tile(aes(x = Year, y = Day, fill = Counts),alpha = 0.6) + scale_fill_gradient(low = "#C2DAEF", high = "#8A8585") + theme_classic() B7. Ribbon plot ๋ฒ”์œ„๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์„  ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ggplot(STOCK) + geom_ribbon(aes(x= Date, ymin = log(Low) - 0.5, ymax = log(High) + 0.5),fill = 'royalblue' , alpha = 0.2) + theme_classic() ggplot(STOCK) + geom_ribbon(aes(x= Date, ymin = log(Low) - 0.5, ymax = log(High) + 0.5),fill = 'royalblue' , alpha = 0.2) + geom_point(aes(x= Date, y = log(Low) - 0.5), col = '#8A8585', alpha = 0.8) + geom_point(aes(x= Date, y = log(High) + 0.5), col = '#8A8585', alpha = 0.8) + geom_line(aes(x = Date, y = log(Open)),group =1 , col = '#C2DAEF' , linetype = 'dashed', size = 0.1) + geom_point(aes(x = Date, y = log(Open)),col = 'red', alpha = 0.4) + theme_classic() + ylab("") + xlab("") B8. Ridge plot density plot์„ ์—ฌ๋Ÿฌ ๊ฐœ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. library(ggridges) ggplot(STOCK) + geom_density_ridges_gradient(aes(x = log(High) + 0.2 , y= Year, fill = .. x..),gradient_lwd = 1.) + theme_ridges(grid = FALSE) + scale_fill_gradient(low= "#8A8585", high= "#C2DAEF") + theme(legend.position='none') + xlab("") + ylab("") B9. Area plot ๋ˆ„์ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ์˜์—ญ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ggplot(Group_Data) + geom_area(aes(x= as.numeric(as.character(Year)), y = Mean , fill = Day),alpha = 0.4) + theme_classic() + xlab("") C1. Polygon plot ๊ทธ๋ž˜ํ”„ ๋ถ„ํฌ์˜ ๋ฒ”์œ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. geom_polygon()์ด๋ผ๋Š” ๋ช…๋ น์–ด๊ฐ€ ์žˆ์ง€๋งŒ, ํ•ด๋‹น ๋ช…๋ น์–ด๋Š” ์“ฐ๊ธฐ๊ฐ€ ์กฐ๊ธˆ ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. ์š”๊ตฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ๋ฐฉ์‹์ด ๊ฐ„๋‹จํ•˜์ง€๊ฐ€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— stat_ellipse() ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ polygon ํ”Œ๋ž์„ ๊ทธ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. stat_ellipse()์ฒ˜๋Ÿผ stat์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ช…๋ น์–ด๋Š” ์˜ต์…˜์— geom = โ€˜polygonโ€™์ฒ˜๋Ÿผ ์–ด๋–ป๊ฒŒ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ๊ฒƒ์ธ์ง€ ์˜ต์…˜์„ ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ggplot(STOCK) + stat_ellipse(geom = 'polygon', aes(x = Volume, y = Stock.Trading, fill = Year), alpha = 0.2) + geom_point(aes(x = Volume, y = Stock.Trading, col = Year), alpha = 0.2) + theme_classic() + # ๊ทธ๋ž˜ํ”„ ๊ฐ€์‹œ์„ฑ์„ ์œ„ํ•ด ์ถ• ๋ฒ”์œ„ ์กฐ์ ˆ xlim(0,1000000) + ylim(0,50000000000) + theme(axis.text.x = element_blank()) C2. Rect plot ์‚ฌ๊ฐํ˜• ํ˜•ํƒœ์˜ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ waterfall ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š”๋ฐ ํ™œ์šฉ์ด ๋ฉ๋‹ˆ๋‹ค. ggplot(Group_Data) + geom_rect(aes(xmin = as.numeric(as.character(Year)) - 0.5 , xmax = as.numeric(as.character(Year)) + 0.5, ymin = Median, ymax = Max, fill = Year), alpha = 0.4) + geom_h line(yintercept = mean(Group_Data$Median), linetype = 'dashed', col = 'red') + theme_classic() + facet_wrap(~Day, nrow = 1) + theme(axis.text.x = element_blank()) ChB1. ๊ธฐ์ดˆํ†ต๊ณ„ ์ด๋ก  1๋‹จ๊ณ„ ์—‘์…€ ํ†ต๊ณ„์™€ R ํ†ต๊ณ„๊ฐ€ ๋‹ค๋ฅธ ๋ถ€๋ถ„ ํ”ํžˆ, ํ†ต๊ณ„๋ฅผ ์ „๊ณตํ•˜์ง€ ์•Š์€ ์‚ฌ๋žŒ๋“ค์€ ์—‘์…€์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ”ผ๋ฒ—ํ…Œ์ด๋ธ”์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ง‘๊ณ„ ๋‚ด๊ณ , ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์„ ๊ตฌํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ธฐ์ดˆํ†ต๊ณ„ ๋ถ„์„์€ R๋กœ๋„ ํ•  ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉฐ, ์–ธ๋œป ๋ณด๊ธฐ์—๋Š” ํฌ๊ฒŒ ๋‹ค๋ฅธ ์ ์ด ์—†์–ด ๋ณด์ž…๋‹ˆ๋‹ค. ์˜คํžˆ๋ ค ํ”„๋กœ๊ทธ๋žจ์ด ๋” ์ง๊ด€์ ์œผ๋กœ ๊ตฌ์„ฑ์ด ๋˜์–ด ์žˆ๋Š” ์—‘์…€์ด ํŽธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด R์„ ์™œ ํ•ด์•ผ ๋˜๋Š” ๊ฒƒ์ผ๊นŒ์š”? ๋ฐ”๋กœ โ€™ํ†ต๊ณ„ํ•™โ€™์— ๊ธฐ๋ฐ˜ํ•œ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํ†ต๊ณ„ํ•™์ด ์ผ๋ฐ˜์ ์ธ ๊ธฐ์ดˆ๋ถ„์„๊ณผ ๋‹ค๋ฅธ ์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”? ๊ทธ ๋ถ€๋ถ„์€ ๋ฐ”๋กœ ๊ฐ™์€ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•  ๋•Œ, ๊ธฐ์ดˆ ํ†ต๊ณ„๋Š” ์ •๋ง ํ‰๊ท ๋งŒ์„ ๋ฝ‘์•„๋‚ด์ง€๋งŒ ํ†ต๊ณ„ํ•™์—์„œ๋Š” ํ‰๊ท ๊ณผ ํ•จ๊ป˜ โ€™๋ถ„์‚ฐโ€™์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ถ„์‚ฐ์ด๋ž€, ๋ณ€๋™์˜ ๊ฐœ๋…์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ค‘์‹ฌ์œผ๋กœ๋ถ€ํ„ฐ ์–ผ๋งˆ๋‚˜ ์‚ฐํฌํ–ˆ๋Š”์ง€ ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํ†ต๊ณ„ํ•™์—์„œ๋Š” ๊ฐ™์€ ํ‰๊ท ์„ ๋ณด๋”๋ผ๋„, ๋ถ„์‚ฐ์„ ํ•จ๊ป˜ ๊ณ ๋ คํ•˜์—ฌ ํ•ด๋‹น ํ‰๊ท ์˜ ๊ฐ’์ด ๋งž๋Š”์ง€ ํ‹€๋ฆฐ ์ง€ ๊ฒ€์ •๊ณผ ์ถ”์ •์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‚˜์ค‘์— ์ ์ถ”์ •๊ณผ ๊ตฌ๊ฐ„์ถ”์ •์—์„œ ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. A1. ํ‘œ๋ณธ๊ณต๊ฐ„๊ณผ ํ™•๋ฅ ๋ณ€์ˆ˜ ํ™•๋ฅ  ์‹คํ—˜๊ณผ ํ‘œ๋ณธ๊ณต๊ฐ„ ํ™•๋ฅ  ์‹คํ—˜ : ๊ฐ™์€ ์กฐ๊ฑด ํ•˜์—์„œ ์‹คํ—˜์„ ๋ฐ˜๋ณตํ•  ๋•Œ, ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅํ•œ ์‹คํ—˜ ํ‘œ๋ณธ ๊ณต๊ฐ„ : ํ™•๋ฅ  ์‹คํ—˜์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๋“ค์˜ ์ง‘ํ•ฉ ํ†ต๊ณ„ ์ด๋ก ์„ ๊ณต๋ถ€ํ•  ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ์•Œ์•„์•ผ ๋  ์šฉ์–ด๋Š” ํ™•๋ฅ  ์‹คํ—˜์ž…๋‹ˆ๋‹ค. ํ™•๋ฅ  ์‹คํ—˜์€ ์‰ฝ๊ฒŒ๋Š” ์ฃผ์‚ฌ์œ„ ๋˜์ง€๊ธฐ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ, ์˜ค๋Š˜ ์ง€๊ฐ์„ ํ• ์ง€ ์•ˆ ํ• ์ง€ ์‹คํ—˜ํ•˜๋Š” ๊ฒƒ๊นŒ์ง€ ๋ชจ๋“  ์ผ์ƒ์ƒํ™œ์„ ํ™•๋ฅ  ์‹คํ—˜์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ™•๋ฅ  ์‹คํ—˜์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๋“ค์˜ ์ง‘ํ•ฉ์„ ํ‘œ๋ณธ๊ณต๊ฐ„(Sample Sapce)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋™์ „์„ ๋˜์ ธ ์•ž, ๋’ท๋ฉด์ด ๋‚˜์˜ค๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ™•๋ฅ  ์‹คํ—˜์„ ์ง„ํ–‰ํ•˜์˜€์„ ๊ฒฝ์šฐ, ํ‘œ๋ณธ๊ณต๊ฐ„์€ {์•ž, ๋’ค}๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ™•๋ฅ ๋ณ€์ˆ˜(Random Variable) ํ™•๋ฅ ๋ณ€์ˆ˜ : ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ์˜ ์‹ค์ˆซ๊ฐ’์„ ๋Œ€์ž…ํ•ด ์ฃผ๋Š” ํ•˜๋‚˜์˜ ํ•จ์ˆ˜ ๋‹ค์Œ์œผ๋กœ๋Š” ํ†ต๊ณ„ํ•™์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜(Random Variable)๊ฐ€ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ํ‘œ๋ณธ๊ณต๊ฐ„์˜ ๊ฐ ์›์†Œ ํ•˜๋‚˜ํ•˜๋‚˜์— ์›ํ•˜๋Š” ๋ชฉ์ ์— ๋”ฐ๋ผ ๊ทธ์— ๊ฑธ๋งž์€ ์‹ค์ˆ˜๋ฅผ ๋Œ€์ž…ํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ™•๋ฅ ๋ณ€์ˆ˜๋Š” ๋ณ€์ˆ˜์˜ ์ฒ™๋„(์ด์‚ฐํ˜•, ์—ฐ์†ํ˜•)์— ๋”ฐ๋ผ 2๊ฐ€์ง€๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค. ์ด์‚ฐํ˜• ํ™•๋ฅ ๋ณ€์ˆ˜(Discrete Random Variable) : ์–ด๋–ค ๊ฐ’์„ ๊ฐ€์งˆ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜ ์—ฐ์†ํ˜• ํ™•๋ฅ ๋ณ€์ˆ˜(Conituous Random Variable) : ์–ด๋–ค ๊ตฌ๊ฐ„ ๋‚ด์— ํฌํ•จ๋  ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜ ์˜ˆ์‹œ 1) ์„ฑ๋ณ„ ์–ด๋–ค ํŒ€์—์„œ ํ•œ ๋ช…์„ ๋ฆฌ๋”๋กœ ์„ ์ถœํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ ์„ฑ๋ณ„์— ๊ด€์‹ฌ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํ‘œ๋ณธ๊ณต๊ฐ„์€ (๋‚จ์ž, ์—ฌ์ž) ๋‘ ๊ฐ€์ง€๋กœ ์ด๋ฃจ์–ด์ง„ ์ง‘ํ•ฉ์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ํ™•๋ฅ ๋ณ€์ˆ˜๋ฅผ '์—ฌ์ž๋Š” 0 ๋‚จ์ž๋Š” 1'์ด๋ผ๊ณ  ์ •์˜ํ•ด ๋ด…์‹œ๋‹ค. ๋ฐ˜๋Œ€๋กœ ํ•ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฆฌ๋”๊ฐ€ ์„ ์ถœ๋˜์—ˆ์„ ๋•Œ 0 ๋˜๋Š” 1์ด๋ผ๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” (๋‚จ์ž, ์—ฌ์ž)๋ผ๋Š” ํ‘œ๋ณธ๊ณต๊ฐ„์—์„œ ๊ฐ ์›์†Œ๋“ค์„ 0๊ณผ 1์ด๋ผ๋Š” ์‹ค์ˆ˜๋กœ ์ „ํ™”ํ•ด ์ฃผ๋Š” ๋ณ€ํ™˜ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์˜ˆ์‹œ 2) ์ฃผ์‚ฌ์œ„ ๋ˆˆ์ด 3๊นŒ์ง€ ์žˆ๋Š” ์ฃผ์‚ฌ์œ„ ๋‘ ๊ฐœ๋ฅผ ๊ตด๋ฆฌ๋Š” ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ํ™•๋ฅ ๋ณ€์ˆ˜๋ฅผ '๋‘ ์ฃผ์‚ฌ์œ„ ๋ˆˆ์˜ ํ•ฉ'์ด๋ผ๊ณ  ์ •์˜ํ•ด ๋ด…์‹œ๋‹ค. ๋ณ€์ˆ˜๋ผ๊ณ  ํ•˜๋ฉด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋Š” ์ด 9๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ‘œ๋ณธ๊ณต๊ฐ„์ด 9๊ฐœ์˜ ์›์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ง‘ํ•ฉ์ด๋ผ๋Š” ๋œป์ด๊ณ  '๋‘ ๋ˆˆ์˜ ํ•ฉ'์ด๋ผ๋Š” ํ•จ์ˆ˜์ธ ํ™•๋ฅ ๋ณ€์ˆ˜๋Š” ์ด 5๊ฐœ์˜ ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ„ ํ‘œ์—์„œ ๋ณด๋ฉด ํ‘œ๋ณธ๊ณต๊ฐ„์€ ์™ผ์ชฝ ๋ถ€๋ถ„์ด ๋˜๊ฒ ๊ณ  ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์‹ค์ˆ˜๋Š” ์˜ค๋ฅธ์ชฝ์ด๊ฒ ์ง€์š”. ์ด๋ ‡๊ฒŒ ํ‘œ๋ณธ๊ณต๊ฐ„์—์„œ ์‹ค์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ๋Š” ๋ณ€ํ™˜ ํ•จ์ˆ˜๊ฐ€ ํ™•๋ฅ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ํŠน์ง•์€ ๊ฐ ๊ฒฝ์šฐ๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์ด ์•Œ๋ ค์ ธ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ํ™•๋ฅ ๋ณ€์ˆ˜๋Š” ๊ทธ ์ž์ฒด๋กœ๋„ ํ•จ์ˆ˜๋ผ๋Š” ์‚ฌ์‹ค์ž…๋‹ˆ๋‹ค. ํ”ํžˆ ๋งŽ์€ ๋ถ„๋“ค์ด ํ™•๋ฅ ๋ณ€์ˆ˜์™€ ํ™•๋ฅ ํ•จ์ˆ˜๋ฅผ ํ—ท๊ฐˆ๋ฆฌ์‹œ๋Š”๋ฐ, ํ™•๋ฅ ๋ณ€์ˆ˜๋Š” ํ‘œ๋ณธ๊ณต๊ฐ„์—์„œ ์‹ค์ˆ˜๋กœ ๊ฐ€๋Š” ํ•จ์ˆ˜์ด๊ณ  ํ™•๋ฅ ํ•จ์ˆ˜๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ค ๊ฐ’์„ ๊ฐ€์งˆ ๋•Œ(ํ˜น์€ ์–ด๋–ค ๋ฒ”์œ„ ๋‚ด์— ํฌํ•จ๋  ๋•Œ)์˜ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์œ„ ํ‘œ์—์„œ๋Š” ์„ธ ๋ฒˆ์งธ ์—ด์ด ํ™•๋ฅ ํ•จ์ˆ˜ ๊ฐ’์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A2. ์ด์‚ฐํ˜• ํ™•๋ฅ ๋ถ„ํฌ 2. ์ด์‚ฐํ˜• ํ™•๋ฅ ๋ถ„ํฌ ์ฒซ ๋ฒˆ์งธ ์ฑ•ํ„ฐ์—์„œ ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ๊ฐœ๋…์„ ๋ง์”€๋“œ๋ฆฌ๋ฉด์„œ ํ™•๋ฅ ๋ณ€์ˆ˜๋Š” ๊ฐ€๋Šฅํ•œ ๊ฐ’๋“ค์— ๋Œ€ํ•œ ํ™•๋ฅ ์ด ์•Œ๋ ค์ ธ ์žˆ๊ณ  ๊ทธ๊ฒƒ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ™•๋ฅ ํ•จ์ˆ˜(Probability Function)๋ผ๋Š” ๊ฒƒ์„ ๋ง์”€๋“œ๋ ธ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋Ÿฌํ•œ ํ™•๋ฅ ๋“ค์€ ์–ด๋–ป๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ์š”? ๊ทธ ํ™•๋ฅ ๋ณ€์ˆ˜๋“ค์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ™•๋ฅ ์˜ ๊ตฌ์กฐ๋ฅผ ์•Œ์•„์•ผ ํ•˜๋ฉฐ ์ด ํ™•๋ฅ  ๊ตฌ์กฐ๋ฅผ ํ”ํžˆ ํ™•๋ฅ ๋ถ„ํฌ(Probability Distribution)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์‚ฐํ˜• ํ™•๋ฅ ๋ณ€์ˆ˜ => ์ด์‚ฐํ˜• ํ™•๋ฅ ๋ถ„ํฌ => ํ™•๋ฅ  ์งˆ๋Ÿ‰ ํ•จ์ˆ˜(Probability Mass Function, m) ์—ฐ์†ํ˜• ํ™•๋ฅ ๋ณ€์ˆ˜ => ์—ฐ์†ํ˜• ํ™•๋ฅ ๋ถ„ํฌ => ํ™•๋ฅ ๋ฐ€๋„ํ•จ์ˆ˜(Probability Density Function, d) ์ผ๋ฐ˜์ ์œผ๋กœ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๋ถ„์„๋“ค์€ ์ด ํ™•๋ฅ ๋ถ„ํฌ์™€ ๊ทธ์— ๋”ฐ๋ฅธ ํ™•๋ฅ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ๋ถ„์„๋“ค์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ๋ฐฐ์šฐ๊ฒŒ ๋  ์ถ”์ •๊ณผ ๊ฒ€์ •๊ณผ ๊ฐ™์€ ํ†ต๊ณ„๋ถ„์„ ์—ญ์‹œ ์ด ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ํ†ตํ•ด์„œ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์‹ค์ œ ๋ฐ์ดํ„ฐ์—์„œ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋Š” ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๊ณ ์•ˆํ•ด ๋‚ด๊ธฐ๋Š” ์‰ฌ์šด ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด์‚ฐํ˜• ํ™•๋ฅ ๋ถ„ํฌ๋Š” ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์ง‘ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ฒฐ์ •๋  ์ˆ˜ ์žˆ์œผ๋‚˜ ์—ฐ์†ํ˜•์˜ ๊ฒฝ์šฐ๋Š” ์‚ฌ์‹ค ๋šœ๋ ทํ•œ ๋ฐฉ๋ฒ•์ด ์—†๊ธฐ์— ์–ป์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ถ”์ธกํ•˜๋Š” ๊ฒƒ์ด ๋Œ€๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์‚ฌ๋žŒ๋“ค์ด ์‹คํ—˜๊ณผ ์—ฐ๊ตฌ๋ฅผ ํ•˜๋‹ค ๋ณด๋‹ˆ, ์ˆ˜๋งŽ์€ ํ™•๋ฅ ๋ถ„ํฌ์—์„œ ํŠน์ •ํ•œ ํŒจํ„ด์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถ„ํฌ๋“ค์„ ๋ฐœ๊ฒฌํ•˜์˜€๊ณ , ์ด๋ฅผ ์ •๋ฆฌํ•˜์—ฌ ์ด๋ก ์„ ์„ฑ๋ฆฝํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ ํ•ด๋‹น ํ™•๋ฅ ๋ถ„ํฌ๋“ค์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์‚ฐํ˜• ํ™•๋ฅ ๋ถ„ํฌ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ˆ˜์ง‘๋˜๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ฒฐ์ •๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ง์€ ๊ณง ์–ด๋–ค ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๋ƒ ํ˜น์€ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋ƒ์— ๋”ฐ๋ผ ์šฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ„ํฌ์˜ ์ข…๋ฅ˜๊ฐ€ ๋ฐ”๋€” ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ํ™•๋ฅ  ์งˆ๋Ÿ‰ ํ•จ์ˆ˜( m)๋Š” ๋‹ค์Œ์˜ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. x ( ) P [ = ] ์ˆ˜์‹์˜ ํ•ด์„์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.์— ํ•ด๋‹นํ•  ํ™•๋ฅ ์„ ๊ตฌํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. A3. ์ดํ•ญ๋ถ„ํฌ(Binomial distribution) 3. ์ดํ•ญ๋ถ„ํฌ(Binomial distribution) ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰ : ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐฐํƒ€์ ์ธ ๋‘ ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ํ™•๋ฅ  ์‹คํ—˜ ์ดํ•ญ๋ถ„ํฌ : ์„ฑ๊ณต ํ™•๋ฅ ์ด ์ธ ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰์„ ๋…๋ฆฝ์ ์œผ๋กœ ๋ฒˆ ์‹คํ–‰ํ•˜์˜€์„ ๋•Œ, ์„ฑ๊ณต์˜ ์ˆ˜๋ฅผ ํ™•๋ฅ ๋ณ€์ˆ˜๋กœ ์ •์˜ํ•˜๋Š” ๋ถ„ํฌ ์ดํ•ญ๋ถ„ํฌ๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ๋ฐฐํƒ€์ ์ธ ๋‘ ๊ฐ€์ง€ ๋ฒ”์ฃผ๋ฅผ ๊ฐ–๊ณ  ๊ฐ ์‹œํ–‰์€ ๋…๋ฆฝ์ ์ธ ๊ฒฝ์šฐ์—์„œ์˜ ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. ์ด ์‹œํ–‰์„ ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋…๋ฆฝ์ ์ด๋ผ๋Š” ๊ฒƒ์€ ๊ฐ ์‹œํ–‰์ด ๋‹ค๋ฅธ ์‹œํ–‰์— ์ „ํ˜€ ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ๋กœ ํŒ€์› ์ค‘ 10์ฃผ ๋™์•ˆ ๋ฌด์ž‘์œ„๋กœ ๋Œ์•„๊ฐ€๋ฉด์„œ ๋‹น์ง์„ ์„œ๋Š”๋ฐ ๋‚จ์ž๊ฐ€ ๋‹น์‹์„ ์„œ๋Š” ํšŸ์ˆ˜์— ๊ด€์‹ฌ์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ์„ฑ๋ณ„์€ ๋‚จ์ž, ์—ฌ์ž ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋งŒ ์ƒํ˜ธ ๋ฐฐํƒ€์ ์œผ๋กœ ์กด์žฌํ•˜๋ฏ€๋กœ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ํ•œ ๋ฒˆ ๋ฝ‘ํžŒ ์‚ฌ๋žŒ์„ ๋‹ค์Œ ๋‹น์ง ๋•Œ ํ›„๋ณด์—์„œ ์ œ์™ธํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ์ด๋Š” ๊ฐ ์‹œํ–‰์ด ๋…๋ฆฝ์ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๊ณ  ๊ณ„์† ๋™์ผํ•œ ํ›„๋ณด๊ตฐ์—์„œ ๋ฌด์ž‘์œ„๋กœ ๋ฝ‘๊ฒŒ ๋œ๋‹ค๋ฉด ๊ฐ ์‹œํ–‰์€ ๋…๋ฆฝ์ ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์กฐ๊ธˆ ๋” ์ƒํ™ฉ์„ ์ผ๋ฐ˜ํ™”์‹œ์ผœ ๋ด…์‹œ๋‹ค. ์‹œํ–‰์€ ์ด ๋ฒˆ์˜ ๋…๋ฆฝ์ ์ธ ๋ฒ ๋ฅด๋ˆ„์ด ์‹œํ–‰์—์„œ ๊ด€์‹ฌ ์žˆ๋Š” ๋ฒ”์ฃผ๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์ด๋ผ๊ณ  ํ•ด๋ด…์‹œ๋‹ค. ์ด๋Ÿฐ ์กฐ๊ฑด๋“ค์ด ์ดํ•ญ๋ถ„ํฌ๋ฅผ ๊ฒฐ์ •์ง“๋Š” '์ƒํ™ฉ'์ด๋ฉฐ, ์—ฌ๊ธฐ์„œ ์ดํ•ญ๋ถ„ํฌ๋Š” ๊ด€์‹ฌ ์žˆ๋Š” ๋ฒ”์ฃผ๊ฐ€ ๋‚˜์˜ค๋Š” ํšŸ์ˆ˜๋ฅผ ํ™•๋ฅ ๋ณ€์ˆ˜๋กœ ํ•˜๋Š” ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ดํ•ญ๋ถ„ํฌ์˜ ํ™•๋ฅ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ฑ๊ณต ํ™•๋ฅ ์ด 0.6์ธ ์ดํ•ญ๋ถ„ํฌ ์ƒ์„ฑ library(ggplot2) # ๋‚œ์ˆ˜ ์ƒ์„ฑ RB = rbinom(n = 400 , size = 1, prob = 0.6) ggplot(NULL) + geom_bar(aes(x = as.factor(RB), fill = as.factor(RB))) + theme_bw() + xlab("") + ylab("") + scale_x_discrete(labels = c("์‹คํŒจ","์„ฑ๊ณต")) + theme(legend.position = 'none') ์ด๋ฉด โˆผ i ( , ) ์ด, ( = ) ( y ) y ( โˆ’ ) โˆ’ , = , , โ‹ฏ ์ด pmfpmf๋ฅผ ํ†ตํ•ด์„œ ๊ด€์‹ฌ ์žˆ๋Š” ๋ฒ”์ฃผ๊ฐ€(ํŽธ์˜์ƒ ์„ฑ๊ณต์ด๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.) nn ๊ฐœ์ค‘ ํ•˜๋‚˜๋„ ๋‚˜์˜ค์ง€ ์•Š์„ ํ™•๋ฅ ๋ถ€ํ„ฐ nn ๊ฐœ ์ค‘ nn ๊ฐœ๋ฅผ ์„ฑ๊ณตํ•  ํ™•๋ฅ ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด๋Ÿฌํ•œ ํ˜•ํƒœ์˜ ์ดํ•ญ๋ถ„ํฌ์˜ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์€ ๊ฐ๊ฐ npnp, np(1โˆ’p) np(1โˆ’p)์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. [ ] n V [ ] n ( โˆ’ ) ์˜ˆ์‹œ ์œ„์˜ ์ˆ˜์‹์„ R๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. library(ggplot2) # ๋‚œ์ˆ˜ ์ƒ์„ฑ X = c() P = c() for(k in 1:10){ RDB = dbinom(x = k, size = 10, prob = 0.4) X = c(X, k) P = c(P, RDB) } ggplot(NULL) + geom_bar(aes(x = X, y = P),stat = 'identity') + theme_bw() + scale_x_continuous(breaks = seq(1,10)) + xlab("์„ฑ๊ณต ํšŸ์ˆ˜") + ylab("ํ™•๋ฅ ") X = c() P = c() for(k in 1:10){ RDB = dbinom(x = k, size = 10, prob = 0.8) X = c(X, k) P = c(P, RDB) } ggplot(NULL) + geom_bar(aes(x = X, y = P),stat = 'identity') + theme_bw() + scale_x_continuous(breaks = seq(1,10)) + xlab("์„ฑ๊ณต ํšŸ์ˆ˜") + ylab("ํ™•๋ฅ ") ์ด 10๋ฒˆ ์‹คํ—˜์„ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์˜€์„ ๋•Œ, ์™ผ์ชฝ์€ ์„ฑ๊ณต ํ™•๋ฅ ์ด 0.4์ผ ๋•Œ, ์„ฑ๊ณต ํšŸ์ˆ˜์— ๋”ฐ๋ฅธ ์„ฑ๊ณต ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์šฐ์ธก์€ ์„ฑ๊ณต ํ™•๋ฅ ์ด 0.8์ผ ๋•Œ, ์„ฑ๊ณต ํšŸ์ˆ˜์— ๋”ฐ๋ฅธ ์„ฑ๊ณต ํ™•๋ฅ ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. A4. ๋‹คํ•ญ๋ถ„ํฌ(multinomial distribution) 4. ๋‹คํ•ญ๋ถ„ํฌ(multinomial distribution) ๋‹คํ•ญ๋ถ„ํฌ : ์‹คํ—˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐœ์ธ ํ™•๋ฅ  ์‹คํ—˜์„ ๋ฒˆ ๋ฐ˜๋ณตํ•˜์˜€์„ ๋•Œ, ๊ฐ ๋ฒ”์ฃผ์— ์†ํ•˜๋Š” ํšŸ์ˆ˜๋ฅผ ํ™•๋ฅ ๋ณ€์ˆ˜๋กœ ํ•˜๋Š” ๋ถ„ํฌ ๋‹คํ•ญ๋ถ„ํฌ๋Š” ์ดํ•ญ๋ถ„ํฌ์˜ ํ™•์žฅ์ž…๋‹ˆ๋‹ค. ์ดํ•ญ๋ถ„ํฌ๊ฐ€ ๋ฒˆ ์‹œํ–‰์—์„œ ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋ฒ”์ฃผ๊ฐ€ ์„ฑ๊ณต/์‹คํŒจ ๋‘ ๊ฐ€์ง€์˜€๋‹ค๋ฉด, ๋‹คํ•ญ๋ถ„ํฌ์—์„œ๋Š” ๋ฒˆ ์‹œํ–‰์—์„œ ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋ฒ”์ฃผ๊ฐ€ ๊ฐ€์ง€๋กœ ํ™•์žฅ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ = ์ธ ๊ฒฝ์šฐ, ์ดํ•ญ๋ถ„ํฌ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. = ์ธ ๊ฒฝ์šฐ์ธ ๋‹ค์Œ ํ‘œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. RM = as.data.frame(t(rmultinom(n = 1, size = 10, prob = c(0.2,0.5,0.3)))) RM = colSums(RM) ggplot(NULL) + geom_bar(aes(x = names(RM), y= RM, fill = names(RM)),stat = 'identity') + theme_bw() + theme(legend.position = 'none') + scale_x_discrete(labels = c("1","2","3")) + xlab("") + ylab("") ์œ„์˜ ๊ฒฝ์šฐ๋Š” ์ด ๊ฐœ์˜ ๋…๋ฆฝ์ ์ธ ์‹œํ–‰ ์ค‘ ๋ฒ”์ฃผ 1์ด ๊ฐœ ๋ฒ”์ฃผ 2๊ฐ€ ๊ฐœ ๋‚˜์˜ฌ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹คํ•ญ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. ๋ฒˆ์˜ ์‹œํ–‰์ด๋‹ˆ ๋‹น์—ฐํžˆ ๋งˆ์ง€๋ง‰ ๋ฒ”์ฃผ๋Š” โˆ’ โˆ’ ๊ฐœ๊ฐ€ ๋  ๊ฒƒ์ด๊ณ  ํ™•๋ฅ ์€ โˆ’ 1 p ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ž…๋‹ˆ๋‹ค. ํ•œ ๋ถ„ํฌ์— ๊ผญ ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ํ•œ ๊ฐœ๋งŒ ์žˆ์œผ๋ฆฌ๋ž€ ๋ฒ•์€ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ™•์žฅํ•ด์„œ ๋ฒ”์ฃผ๊ฐ€ ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ์ƒํ•˜๋ฉด โˆ’ ๊ฐœ์˜ ํ™•๋ฅ ๋ณ€์ˆ˜๋ฅผ ๊ฐ–๋Š” ํ™•๋ฅ ๋ถ„ํฌ๊ฐ€ ๋จ์„ ์ง์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ๋ฒ”์ฃผ๋Š” ์œ„ ํ‘œ์ฒ˜๋Ÿผ ๋‚˜๋จธ์ง€ ๋ฒ”์ฃผ์— ์ข…์†๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฒ”์ฃผ๊ฐ€ ๊ฐœ์ธ ๋‹คํ•ญ๋ถ„ํฌ์˜ ํ™•๋ฅ ํ•จ์ˆ˜๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ( 1 X, , k 1 ) M l i ( , 1 p, , k 1 ) ( 1 x, 2 x, X โˆ’ = k 1 ) n x! 2 โ‹ฏ k 1 x x = ( โˆ’ 1 x โˆ’ โˆ’ k 1 ) , p = ( โˆ’ 1 p โˆ’ โˆ’ k ์‹์€ ์กฐ๊ธˆ ๋ณต์žกํ•˜์ง€๋งŒ ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•˜์‹ค ํ•„์š” ์—†์ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋…ผ๋ฆฌ๋ฅผ ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒ”์ฃผ ํ™•๋ฅ  ์ฒซ ๋ฒ”์ฃผ ๊ฐœ์ˆ˜ ๋‹ค์Œ ๋ฒ”์ฃผ ํ™•๋ฅ  ๋‹ค์Œ ๋ฒ”์ฃผ ๊ฐœ์ˆ˜ ๋งˆ์ง€๋ง‰ ๋ฒ”์ฃผ ํ™•๋ฅ  ๋งˆ์ง€๋ง‰ ๋ฒ”์ฃผ ๊ฐœ์ˆ˜ ( ๋ฒ” ํ™• ) ๋ฒ” ๊ฐฏ ร— ( ์Œ ์ฃผ ๋ฅ  ) ์Œ ์ฃผ ์ˆ˜ โ‹ฏ ร— ( ์ง€ ๋ฒ” ํ™• ) ์ง€ ๋ฒ” ๊ฐฏ ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋ฒ”์ฃผ์˜ ์กฐํ•ฉ์„ ๊ณฑํ•ด์ฃผ์–ด์„œ ํ•ด๋‹น ํ™•๋ฅ ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด์ฃ . ์ด ์—ญ์‹œ ๋ฐ์ดํ„ฐ์˜ ์ƒํ™ฉ์ด ๋ถ„ํฌ๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ๋‹คํ•ญ๋ถ„ํฌ ๋ฌธ์ œ๋ฅผ ๋‹ค๋ฃจ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ ๋ˆˆ์ด 3๊นŒ์ง€ ์žˆ๋Š” ์ฃผ์‚ฌ์œ„๋ฅผ 10ํšŒ ๋˜์กŒ์„ ๋•Œ, ์œ„ ๊ฒฝ์šฐ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜๋Š” ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # p.m.f ๊ณ„์‚ฐ n_F = factorial(10) x_F = factorial(5) * factorial(3) * factorial(2) Prob = (n_F / x_F) * (1/3)^5 * (1/3)^3 * (1/3)^2 Prob [1] 0.04267642 # ๋ช…๋ น์–ด ํ™œ์šฉ dmultinom(c(5,3,2),prob = c(1/3,1/3,1/3)) [1] 0.04267642 [ 1 5 x = , 3 2 ] 0.04 A5. ํฌ์•„์†ก๋ถ„ํฌ(Poisson Distribution) 5. ํฌ์•„์†ก๋ถ„ํฌ(Poisson Distribution) ํฌ์•„์†ก๋ถ„ํฌ : ์ผ์ • ๋‹จ์œ„์—์„œ ํ‰๊ท  ์„ฑ๊ณต ์ˆ˜๊ฐ€ ์ผ ๋•Œ ์„ฑ๊ณต ํšŸ์ˆ˜๋ฅผ ํ™•๋ฅ ๋ณ€์ˆ˜๋กœ ํ•˜๋Š” ๋ถ„ํฌ ํฌ์•„์†ก ๋ถ„ํฌ๋Š” ์ด ์ถฉ๋ถ„ํžˆ ํฌ๊ณ  ์„ฑ๊ณต ํ™•๋ฅ  ๊ฐ€ ๋งค์šฐ ์ž‘์„ ๋•Œ, ์ดํ•ญ๋ถ„ํฌ์— ๋Œ€ํ•œ ๊ทผ์‚ฌ๋กœ ํ™œ์šฉ์ด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์–ด๋–ค ๊ณต์žฅ์—์„œ 10์‹œ๊ฐ„(์ผ์ • ๋‹จ์œ„)๋งˆ๋‹ค ํ‰๊ท ์ ์œผ๋กœ 2๊ฐœ์˜ ๋ถˆ๋Ÿ‰ํ’ˆ(ํ‰๊ท  ์„ฑ๊ณต ์ˆ˜ )์ด ๋ฐœ์ƒ๋œ๋‹ค๋ฉด ๋ถˆ๋Ÿ‰ํ’ˆ์ด ํ•˜๋‚˜๋„ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ํ™•๋ฅ ๋ถ€ํ„ฐ ์ˆ˜์‹ญ, ์ˆ˜๋ฐฑ ๊ฐœ๊ฐ€ ๋ฐœ์ƒํ•  ํ™•๋ฅ ๊นŒ์ง€ ์„ฑ๊ณต ํšŸ์ˆ˜์— ๋”ฐ๋ฅธ ํ™•๋ฅ ์„ ๋‹ค๋ฃจ๋Š” ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. RP = rpois(n = 100 ,lambda = 2) ggplot(NULL) + geom_bar(aes(x = as.factor(RP),fill = as.factor(RP))) + theme_bw() + xlab("์„ฑ๊ณต ํšŸ์ˆ˜") + ylab("๋นˆ๋„") + theme(legend.position = 'none') ํฌ์•„์†ก๋ถ„ํฌ๋Š”์šฐ๋ฆฌ ์‹ค์ƒํ™œ์— ์ •๋ง ๋งŽ์ด ์ ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. ๋นˆ๋„๋กœ ์กฐ์‚ฌ๋œ ๋ฐ์ดํ„ฐ๋Š” ์ „๋ถ€ ํฌ์•„์†ก๋ถ„ํฌ๋ฅผ ์ ์šฉํ•˜์—ฌ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๋˜ํ•œ ํฌ์•„์†ก๋ถ„ํฌ๋Š” โ€™nn ๋ฒˆ ์ค‘ ์„ฑ๊ณต ํšŸ์ˆ˜โ€™์˜ ๋ถ„ํฌ์ธ ์ดํ•ญ๋ถ„ํฌ์™€ ๋งค์šฐ ๋ฐ€์ ‘ํ•œ ๊ด€๋ จ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ•ญ๋ถ„ํฌ์˜ ํ‰๊ท ์€ npnp์ด๊ณ  ์ด๋Š” ๊ณง โ€™ํ‰๊ท  ์„ฑ๊ณต ์ˆ˜โ€™์˜ ๊ด€์ ์œผ๋กœ ๋ฐ”๋ผ๋ณผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํฌ์•„์†ก๋ถ„ํฌ์˜ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ™•๋ฅ ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์œ„ ๊ทธ๋ž˜ํ”„์—์„œ ์„ฑ๊ณต ํšŸ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ• ์ˆ˜๋ก ๋นˆ๋„ ์ˆ˜๊ฐ€ ๊ธ‰๊ฒฉํžˆ ์ค„์–ด๋“œ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฉด โˆผ o s o ( ) ์ด, ( = ) e ฮป y! y 0 1 2 โ‹ฏ ( ) ฮป ( ) ฮป ํฌ์•„์†ก๋ถ„ํฌ๋Š” ํŠน์ดํ•˜๊ฒŒ๋„ ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์ด ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋นˆ๋„ ๋ฐ์ดํ„ฐ์— ์ ์šฉํ•˜๊ธฐ๊ฐ€ ์ ์ ˆํ•˜์ฃ . ํ‰๊ท  ๋นˆ๋„๊ฐ€ ๋†’๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ๋งŒํผ ๋ฐ”์šด๋”๋ฆฌ๊ฐ€ ์ปค์ง„๋‹ค๋Š” ๊ฒƒ์ด๊ณ  ๋ฐ”์šด๋”๋ฆฌ๊ฐ€ ์ปค์ง„๋‹ค๋Š” ๊ฒƒ์€ ๋ถ„์‚ฐ์ด ํฌ๋‹ค๊ณ  ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ A ๋„๋กœ์˜ 1์‹œ๊ฐ„๋‹น ํ†ต๊ณผ ์ฐจ๋Ÿ‰ ์ˆ˜๊ฐ€ = 20 ์ธ ํฌ์•„์†ก ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅผ ๊ฒฝ์šฐ, 15๋Œ€ ์ดํ•˜์˜ ์ฐจ๋Ÿ‰์ด ํ†ต๊ณผํ•  ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ppois(q = 15, lambda = 20, lower.tail = TRUE) [ โ‰ค 15 ] 0.15 A6. ์—ฐ์†ํ˜• ํ™•๋ฅ ๋ถ„ํฌ 6. ์—ฐ์†ํ˜• ํ™•๋ฅ ๋ถ„ํฌ ์—ฐ์†ํ˜• ํ™•๋ฅ ๋ถ„ํฌ๋Š” ์ด์‚ฐํ˜•๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์ƒํ™ฉ์ด ๋ถ„ํฌ๋ฅผ ๊ฒฐ์ •์ง“์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋“ค์˜ ํ˜•ํƒœ๋ฅผ ๋ณด๊ณ  ์ถ”์ธกํ•˜๋Š” ์ •๋„๊ฐ€ ์ „๋ถ€์ž…๋‹ˆ๋‹ค. ํ†ต๊ณ„์—์„œ๋Š” ๊ทธ๊ฒƒ์„ ๋ถ„ํฌ ๊ฐ€์ •์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋ฉฐ ์‹ค์งˆ์ ์œผ๋กœ ์ €ํฌ๊ฐ€ ๋ถ„์„ํ•˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ฒƒ๋“ค์€ ์ด ๋ถ„ํฌ ๊ฐ€์ •์„ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์—ฐ์†ํ˜• ํ™•๋ฅ ๋ณ€์ˆ˜๋Š” ์ด์‚ฐํ˜• ํ™•๋ฅ ๋ณ€์ˆ˜์™€๋Š” ๋‹ค๋ฅด๊ฒŒ, ๊ตฌ๊ฐ„์œผ๋กœ ์ •์˜์—ญ์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. [ < < ] โˆซ b ( ) x ๋˜ํ•œ ๋ˆ„์ ๋œ ํ™•๋ฅ  ๋ถ„ํฌ๋ฅผ ๋ˆ„์  ํ™•๋ฅ  ๋ฐ€๋„ ํ•จ์ˆ˜(cumulative probability density function, d)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. d๋Š” d๋ฅผ ์ ๋ถ„ํ•œ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. R ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. [ ] โˆซ โˆž f ( ) x R = rnorm(n = 100000, mean = 0, sd = 1) ggplot(NULL) + geom_histogram(aes(x = R, y= .. density..),binwidth = 0.2, fill = "white",col = 'black') + geom_density(aes(x = R), col = 'red', size = 1) + scale_y_continuous(expand = c(0,0),limits = c(0,0.5)) + scale_x_continuous(limits = c(-3,3)) + xlab("") + theme_bw() ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ถ„ํฌ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์˜€์„ ๋•Œ, ์œ„ ๋ถ„ํฌ์— ๋Œ€ํ•œ ๋ˆ„์  ํ™•๋ฅ ๋ถ„ํฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. CR = ecdf(R) # CDF ๊ณ„์‚ฐ x = seq(from = -3, to = 3, by = 0.2) CP = CR(x) ggplot(NULL) + geom_line(aes(x = x, y = CP)) + geom_area(aes(x = x, y = CP), fill = 'royalblue', alpha = 0.4) + theme_bw() ์œ„ ๊ทธ๋ž˜ํ”„๋ฅผ ํ™•์ธํ•˜์‹œ๋ฉด, x ๊ฐ’์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ๊ทธ์— ๋”ฐ๋ฅธ ๋ˆ„์  ํ™•๋ฅ ๋„ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, [ < ] 1 ์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. A7. ์ •๊ทœ๋ถ„ํฌ(Normal Distribution) 7. ์ •๊ทœ๋ถ„ํฌ(Normal Distribution) ์ •๊ทœ๋ถ„ํฌ๋Š” ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์˜ ํ˜•ํƒœ๊ฐ€ ์‚ฐ๋ด‰์šฐ๋ฆฌ์ผ ๋•Œ ๊ฐ€์ •๋˜๋Š” ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. ํ”ํžˆ ' ์ข… ๋ชจ์–‘'์— ๋น„์œ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ข… ๋ชจ์–‘์ด๋ผ ํ•จ์€ ์ค‘์‹ฌ์— ๋งŽ์€ ๋ฐ์ดํ„ฐ๋“ค์ด ๋ชจ์—ฌ์žˆ๊ณ  ์ค‘์‹ฌ์—์„œ ๋ฉ€์–ด์งˆ์ˆ˜๋ก ์ ์€ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ค‘์‹ฌ์„ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์šฐ๊ฐ€ ๋Œ€์นญ์ ์ธ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ณง ๋ฐ์ดํ„ฐ์˜ ์ค‘์‹ฌ๊ณผ ์‚ฐ์ˆ ์ ์ธ ํ‰๊ท ์ด ๋™์ผํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ณผ๊ฑฐ์˜ ํ•™์ž๋“ค์€ ๋งŽ์€ ๋ถ„์•ผ์˜ ์—ฐ์†ํ˜• ๋ฐ์ดํ„ฐ๋“ค์ด ์ด๋Ÿฐ ํ˜•ํƒœ๋ฅผ ๋ค๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•˜์˜€๊ณ  ๊ทธ์— ์ฐฉ์•ˆํ•˜์—ฌ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๊ณ ์•ˆํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ์–ธ๊ธ‰ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์—ฐ์†ํ˜• ์ž๋ฃŒ๋Š” ์ˆ˜์ง‘ ์ƒํ™ฉ์ด ๋ถ„ํฌ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  ๋ฐ์ดํ„ฐ์˜ ํ˜•ํƒœ๋ฅผ ๋ณด๊ณ  ๊ทธ์— ์•Œ๋งž์€ ๋ถ„ํฌ๋ฅผ ์„ ํƒํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•˜๋ฉด ์ง€๊ทนํžˆ ์ƒ์‹์ ์ธ ๊ด€์ ์—์„œ ๋งŒ๋“ค์–ด์ง„ ๋ถ„ํฌ๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด ์‚ฐ๋ด‰์šฐ๋ฆฌ ๋ถ„ํฌ๋Š” ์–ด๋–ค ์‹์œผ๋กœ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ์„๊นŒ์š”? ๋‘ ๊ฐ€์ง€๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์‚ฐ๋ด‰์šฐ๋ฆฌ ์ •์ƒ์„ ๋‚˜ํƒ€๋‚ด๋Š” '๋ฐ์ดํ„ฐ์˜ ์ค‘์‹ฌ' : ํ‰๊ท  ์‚ฐ์˜ ๊ฒฝ์‚ฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” '๋ฐ์ดํ„ฐ์˜ ํผ์ง ์ •๋„' : ๋ถ„์‚ฐ ํ‰๊ท ์— ๋”ฐ๋ผ ์‚ฐ์€ ์ขŒ์šฐ๋กœ ์ด๋™์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ ๋ถ„์‚ฐ์— ๋”ฐ๋ผ ์‚ฐ์˜ ๊ฒฝ์‚ฌ๊ฐ€ ์™„๋งŒํ•œ์ง€ ๊ฐ€ํŒŒ๋ฅธ์ง€๋ฅผ ๊ฐ€๋Š ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์™„๋งŒํ•˜๋‹ค๊ณ  ํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋Š” ๋น„๊ต์  ์ค‘์‹ฌ์— ๋œ ๋ชจ์—ฌ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋ถ„์‚ฐ์€ ๋†’๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ๊ฒฝ์‚ฌ๊ฐ€ ๊ฐ€ํŒŒ๋ฅด๋‹ค๋ฉด ๋ฐ์ดํ„ฐ๋“ค์€ ์ค‘์‹ฌ์—์„œ ๋ฐ€๋„๊ฐ€ ๋†’๊ณ  ์ด ๊ฒฝ์šฐ ๋ถ„์‚ฐ์€ ๋‚ฎ์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ๊ฐ„๋‹จํ•œ ๊ทธ๋ฆผ ํ•˜๋‚˜๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. library(reshape) library(dplyr) k1 = c() p1 = c() for(k in seq(-15,15, by = 0.01)){ p = dnorm(x = k, mean = 0, sd = 3) k1 = c(k1,k) p1 = c(p1,p) } k2 = c() p2 = c() for(k in seq(-15,15, by = 0.01)){ p = dnorm(x = k, mean = 0, sd = 5) k2 = c(k2,k) p2 = c(p2,p) } DF = data.frame( k = k1, p1 = p1, p2 = p2 ) DF %>% melt(id.vars = c("k")) %>% ggplot() + geom_line(aes(x = k, y = value, col = as.factor(variable))) + geom_vline(xintercept = 0, linetype = 'dashed') + theme_bw() + theme(legend.position = 'none') + xlab("") + ylab("") + scale_y_continuous(expand = c(0,0)) ์œ„ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด ๋‘ ๊ฐ€์ง€์˜ ์ •๊ทœ๋ถ„ํฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ์ค‘์‹ฌ์ด์ž ํ‰๊ท ์€ ๋™์ผํ•˜๊ณ  ๋ถ„์‚ฐ์€ ํŒŒ๋ž€์ƒ‰์ด ๋ถ‰์€์ƒ‰๋ณด๋‹ค ๋†’์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์‚ฐ๋ด‰์šฐ๋ฆฌ ํ˜•ํƒœ์˜ ๊ฒฝ์‚ฌ๊ฐ€ ํŒŒ๋ž€์ƒ‰์ด ๋” ์™„๋งŒํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ๋ถ„ํฌ์˜ ํ˜•ํƒœ๋ฅผ ๊ฒฐ์ •ํ•ด ์ฃผ๋Š” ํ‰๊ท ๊ณผ ๋ถ„์‚ฐ์€ ๊ฐ๊ฐ ์™€ 2 ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฉด โˆผ ( , 2 ) ์ด, ( ) 1 ฯ€ ฯƒ โˆ’ 2 ( โˆ’ ฯƒ ) , โˆž y โˆž [ ] ฮผ [ ] ฯƒ ์ •๊ทœ๋ถ„ํฌ์—์„œ๋Š” ๋ถ„์‚ฐ 2 ์˜ ์–‘์˜ ์ œ๊ณฑ๊ทผ์ธ ๋ฅผ ํ‘œ์ค€ํŽธ์ฐจ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ด ์—ญ์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ์–ผ๋งˆ๋‚˜ ํผ์ ธ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ฒ™๋„๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถ„์‚ฐ์ด ์žˆ๋Š”๋ฐ๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๊ตณ์ด ํ‘œ์ค€ํŽธ์ฐจ๊ฐ€ ์“ฐ์ด๋Š” ์ด์œ ๋Š” ๋‹จ์œ„(์Šค์ผ€์ผ)์˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋ถ„์‚ฐ์ด๋ผ๋Š” ๊ฒƒ์€ ์‹ค์ œ ์ž๋ฃŒ์˜ ์ œ๊ณฑ์„ ์ด์šฉํ•ด์„œ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋ถ„์‚ฐ์€ ์‹ค์ œ ์ž๋ฃŒ์—์„œ ์ œ๊ณฑ๋œ ๋‹จ์œ„(์Šค์ผ€์ผ)์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ์–‘์˜ ์ œ๊ณฑ๊ทผ์„ ํ†ตํ•ด ๊ตฌํ•ด์ง„ ๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๋“ค๊ณผ ๊ฐ™์€ ๋‹จ์œ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ๋ถ„ํฌ์˜ ์ค‘์š”ํ•œ ํŠน์ง• ์ค‘ ํ•˜๋‚˜๋Š” ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ผ๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜์˜ ์„ ํ˜• ์กฐํ•ฉ ์—ญ์‹œ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๋Š” ์‚ฌ์‹ค์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ‰๊ท ์ด 10์ธ ์ •๊ทœ ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์ •๋œ ๋ฐ์ดํ„ฐ์— ๋ชจ๋‘ -10์”ฉ ํ•ด์ฃผ๋ฉด ๊ทธ๋“ค ์—ญ์‹œ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ณ  ํ‰๊ท ์€ 0์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ™์€ ์‹์œผ๋กœ ๋ถ„์‚ฐ์ด 100์ธ ์ •๊ทœ ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์ •๋œ ๋ฐ์ดํ„ฐ์— ๋ชจ๋‘ 10์”ฉ ๋‚˜๋ˆ„์–ด ์ฃผ๋ฉด ๊ทธ๋“ค ์—ญ์‹œ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ณ  ๋ถ„์‚ฐ์€ 1์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ณง ์ค‘์‹ฌ๊ณผ ๋‹จ์œ„(scale)๋ฅผ ์ž์œ ์ž์žฌ๋กœ ๋ฐ”๊พธ์–ด ์ค„ ์ˆ˜ ์žˆ๋Š” ์œ ์—ฐ์„ฑ์„ ๊ฐ€์กŒ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ด ์„ฑ์งˆ์„ ์ด์šฉํ•ด ์ •๊ทœ๋ถ„ํฌ ๋ฐ์ดํ„ฐ์˜ ๋‹จ์œ„๋ฅผ ๋งž์ถ”์–ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฐ์ดํ„ฐ์—์„œ ํ‰๊ท ์„ ๋นผ๊ณ  ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๋‚˜๋ˆ„์–ด ์ฃผ๋ฉด ์–ด๋–ค ์ •๊ทœ ํ™•๋ฅ  ๋ณ€์ˆ˜๋“  ํ‰๊ท ์ด 0, ๋ถ„์‚ฐ์ด 1์ธ ๋™์ผํ•œ ๋‹จ์œ„๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ •๊ทœํ™”ํ•œ๋‹ค๊ณ  ํ‘œํ˜„ํ•˜๋ฉฐ ์ •๊ทœํ™”๋œ ๋ถ„ํฌ๋ฅผ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ(standard normal distribution)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ผ ๋•Œ โˆผ ( , 2 ) ๋•Œ ( โˆ’ ฯƒ ) N ( , ) ์ •๊ทœํ™”์˜ ์‹ค์งˆ์  ์˜๋ฏธ๋Š” ๊ฐ ๋ฐ์ดํ„ฐ๋“ค์˜ ๋‹จ์œ„๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ์„œ๋กœ ๋‹ค๋ฅธ ์ง‘๋‹จ๋ผ๋ฆฌ๋„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋น„๊ต ๊ฒ€์ •์€ ๋‹ค ์ด๋Ÿฐ ์ฝ˜์…‰ํŠธ๋ฅผ ๊ทผ๊ฐ„์— ๋‘๊ณ  ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ •๊ทœ๋ถ„ํฌ๊ฐ€ ์ค‘์š”ํ•œ ์ด์œ ๋Š” ๋น„๋‹จ ๋งŽ์€ ์—ฐ์†ํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ๋งŒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ํ‘œ๋ณธ๋“ค์„ ๋ฝ‘์•„์„œ ํ‘œ๋ณธ๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•ด๋ณด๋ฉด ๊ทธ๊ฒƒ๋“ค์ด ๋”ฐ๋ฅด๋Š” ๋ถ„ํฌ๊ฐ€ ์ •๊ทœ๋ถ„ํฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋”์šฑ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ‘œ๋ณธ๋ถ„ํฌ(ํ†ต๊ณ„๋Ÿ‰์˜ ํ™•๋ฅ ๋ถ„ํฌ)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ โˆผ ( 20 5 ) ์ธ ํ™•๋ฅ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ํ‘œ์ค€ํ™”๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. x1 = rnorm(n = 1000, mean = 20, sd = 5) x2 = scale(x1) DF = data.frame( x1 = x1, x2 = x2 ) DF %>% melt() %>% mutate(variable = ifelse(variable == "x1", "๋น„ํ‘œ์ค€ํ™”","ํ‘œ์ค€ํ™”")) %>% ggplot() + geom_density(aes(x = value, fill = variable), alpha = 0.4) + theme_bw() + theme(legend.position = c(0.8,0.6)) + xlab("") + ylab("") + labs(fill = "") ๋‹ค์Œ์œผ๋กœ๋Š” ์ˆ˜๋ฆฌ์˜์—ญ ๋ชจ์˜๊ณ ์‚ฌ ๋ฌธ์ œ๋ฅผ R๋กœ ํ•œ๋ฒˆ ํ’€์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ) ์–ด๋Š ์‹คํ—˜์‹ค์˜ ์—ฐ๊ตฌ์›์ด ์–ด๋–ค ์‹๋ฌผ๋กœ๋ถ€ํ„ฐ ํ•˜๋ฃจ ๋™์•ˆ ์ถ”์ถœํ•˜๋Š” ํ˜ธ๋ฅด๋ชฌ์˜ ์–‘์€ ํ‰๊ท ์ด 30.2 g , ํ‘œ์ค€ํŽธ์ฐจ๊ฐ€ 0.6 g ์ธ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ํ•œ๋‹ค. ์–ด๋Š ๋‚  ์ด ์—ฐ๊ตฌ์›์ด ํ•˜๋ฃจ ๋™์•ˆ ์ถ”์ถœํ•œ ํ˜ธ๋ฅด๋ชฌ์˜ ์–‘์ด 29.6 g ์ด์ƒ์ด๊ณ  31.4 g ์ดํ•˜์ผ ํ™•๋ฅ ์„ ์˜ค๋ฅธ์ชฝ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ ํ‘œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ•œ ๊ฒƒ์€?(2016๋…„ 9์›” ๋ชจ์˜๊ณ ์‚ฌ ๊ฐ€ํ˜• 10๋ฒˆ) ์ผ ๋•Œ โˆผ ( 30.2 0.6 ) ์ผ, P [ 29.6 Y 31.4 ] ? 1 29.6 30.2 0.6 Z = 31.4 30.2 0.6 Z1 = (29.6-30.2) / 0.6 Z2 = (31.4-30.2) / 0.6 print(paste("Z1 :",round(Z1),",","Z2 :", Z2)) [ 1 Z 2 ] P [ โ‰ค ] P [ โ‰ค 1 ] k1 = c() p1 = c() for(k in seq(-5,5, by = 0.01)){ p = dnorm(x = k, mean = 0, sd = 1) k1 = c(k1,k) p1 = c(p1,p) } ggplot(NULL) + geom_line(aes(x = k1, y = p1)) + geom_area(aes(x = ifelse(k1 > -1 & k1 < 2, k1, 0), y = p1),fill = 'royalblue', alpha = 0.4) + theme_bw() + scale_x_continuous(breaks = seq(-5,5, by = 1)) + scale_y_continuous(expand = c(0,0),limits = c(0,0.45)) + xlab("") + ylab("") # pnorm => ๋ˆ„์  ํ™•๋ฅ  ๊ตฌํ•˜๊ธฐ Answer = pnorm(q = 2, mean = 0, sd = 1, lower.tail = TRUE) - pnorm(q = -1, mean = 0, sd = 1, lower.tail = TRUE) print(paste("Answer : ", round(Answer, 5))) [1] "Answer : 0.81859" ๋‹ค์Œ ๋ถ„ํฌ๋ฅผ ๋ฐฐ์šฐ๊ธฐ ์ „์— ์ด ๋ชจ์ˆ˜์™€ ํ†ต๊ณ„๋Ÿ‰, ๊ทธ๋ฆฌ๊ณ  ํ‰๊ท ์˜ ํ‘œ๋ณธ๋ถ„ํฌ์— ๋Œ€ํ•ด ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A8. ๋ชจ์ˆ˜(parameter)์™€ ํ†ต๊ณ„๋Ÿ‰(statistic) 8. ๋ชจ์ˆ˜(parameter)์™€ ํ†ต๊ณ„๋Ÿ‰(statistic) ๋ชจ์ˆ˜ : ํ†ต๊ณ„์  ์ถ”๋ก ์—์„œ ๋ถ„์„์ž์˜ ์ตœ์ข… ๋ชฉํ‘œ์ด์ž ๋ชจ์ง‘๋‹จ(population)์˜ ํŠน์„ฑ ๋ชจ์ง‘๋‹จ : ๋ถ„์„ ๋Œ€์ƒ์ด ๋˜๋Š” ์ง‘๋‹จ์˜ ์ „์ฒด ํ†ต๊ณ„๋Ÿ‰ : ํ•ด๋‹น ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœํ•œ ํ‘œ๋ณธ(sample)์„ ์ด์šฉํ•ด ๋งŒ๋“  ๊ฒƒ์œผ๋กœ ํ‘œ๋ณธ๋“ค์˜ ํ•จ์ˆ˜ ํ‘œ๋ณธ : ๋ชจ์ง‘๋‹จ์œผ๋กœ๋ถ€ํ„ฐ ๋ฌด์ž‘์œ„ ์ถ”์ถœ์ด ๋˜์—ˆ์œผ๋ฉฐ, ๋ชจ์ง‘๋‹จ์„ ๋Œ€ํ‘œํ•˜๋Š” ๋ถ„์„ ๋Œ€์ƒ ๊ฒ€์ • : ์ฃผ์žฅํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ€์„ค์ด ๋งž๋Š”์ง€ ํ‹€๋ฆฐ ์ง€์— ๋Œ€ํ•œ ์˜์‚ฌ๊ฒฐ์ • ๊ณผ์ • ์ถ”์ • : ํ‘œ๋ณธ(ํ†ต๊ณ„๋Ÿ‰)์„ ์ด์šฉํ•˜์—ฌ ๋ชจ์ง‘๋‹จ(๋ชจ์ˆ˜)๋ฅผ ์ถ”์ •ํ•˜๋Š” ๊ณผ์ • ์ ์ถ”์ •๋Ÿ‰ : ํ•˜๋‚˜์˜ ๊ฐ’(์ )์œผ๋กœ ๋ชจ์ˆ˜๋ฅผ ์ถ”์ •ํ•œ ๊ฐ’ ๊ตฌ๊ฐ„์ถ”์ •๋Ÿ‰ : ์‹ ๋ขฐ๊ตฌ๊ฐ„์„ ํ†ตํ•ด ๋ชจ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์„ ๋ฒ”์œ„๋ฅผ ์ถ”์ •ํ•œ ๊ฐ’๋“ค์˜ ๋ฒ”์œ„ ๋•Œ๋กœ๋Š” ํ†ต๊ณ„์  ๊ฒ€์ •์„ ์œ„ํ•ด ํŠน์ˆ˜ํ•œ ํ†ต๊ณ„๋Ÿ‰์„ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜๊ณ  ๋ชจ์ˆ˜๋ฅผ ์ถ”์ •ํ•˜๊ธฐ ์œ„ํ•ด ํ†ต๊ณ„๋Ÿ‰์„ ๊ตฌํ•ด๋ณด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ „์ž์˜ ๊ฒฝ์šฐ๋Š” ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰(test statistic)์ด๋ผ ๋ถ€๋ฅด๊ณ  ํ›„์ž์˜ ๊ฒฝ์šฐ๋Š” ํŠน๋ณ„ํžˆ ์ถ”์ •๋Ÿ‰(estimator)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ํ•˜๋‚˜ ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์ •๋ณด๋Š” ๋Œ€ํ•œ๋ฏผ๊ตญ ๋‚จ์„ฑ์˜ ํ‰๊ท  ํ‚ค์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ์„ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด์„œ ๋ฌด์ž‘์œ„๋กœ 100๋ช…์˜ ๋‚จ์„ฑ์„ ๋ฝ‘์•„์„œ ๊ทธ๋“ค์˜ ํ‚ค๋ฅผ ํ‰๊ท  ๋‚ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋ชจ์ง‘๋‹จ์€ ๋Œ€ํ•œ๋ฏผ๊ตญ ๋‚จ์„ฑ์ด๋ฉฐ ํ‘œ๋ณธ์€ ๋ฝ‘์€ 100๋ช…์˜ ๋‚จ์ž์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์šฐ๋ฆฌ์˜ ์ตœ์ข… ๋ชฉํ‘œ์ด์ž ๋ชจ์ง‘๋‹จ์˜ ํŠน์„ฑ์ธ ๋ชจ์ˆ˜๋Š” ๋Œ€ํ•œ๋ฏผ๊ตญ ๋‚จ์„ฑ์˜ ํ‰๊ท  ํ‚ค๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๊ณ  ํ‘œ๋ณธ๋“ค์„ ํ†ตํ•ด ๊ตฌํ•œ ํ‘œ๋ณธ๋“ค์˜ ํ‰๊ท  ํ‚ค๋Š” ํ†ต๊ณ„๋Ÿ‰์ด์ž ๋ชจ์ˆ˜๋ฅผ ์ถ”์ •ํ•˜๋Š” ์ถ”์ •๋Ÿ‰์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋งŽ์€ ๋ถ„๋“ค์ด ์ฐฉ๊ฐํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ, ํ†ต๊ณ„๋Ÿ‰์€ ๊ผญ ํ‘œ๋ณธํ‰๊ท , ํ‘œ๋ณธ๋ถ„์‚ฐ๊ณผ ๊ฐ™์ด ๋ชจ์ˆ˜๋ฅผ ์ถ”์ •ํ•˜๋Š” ๊ฒƒ๋“ค๋งŒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ํ‘œ๋ณธ๋“ค์˜ ํ•จ์ˆ˜๋Š” ์ „๋ถ€ ํ†ต๊ณ„๋Ÿ‰์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ‘œ๋ณธํ‰๊ท ๊ณผ ํ‘œ๋ณธ๋ถ„์‚ฐ์ด ๋งค์šฐ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ†ต๊ณ„๋Ÿ‰์ผ๋ฟ์ž…๋‹ˆ๋‹ค. โ€• 1 โˆ‘ = n i 2 1 โˆ’ โˆ‘ = n ( i x) ๊ตณ์ด ์ด๋Ÿฐ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋”๋ผ๋„ ํ‘œ๋ณธ๋“ค์„ ์ด์šฉํ•ด์„œ ๋งŒ๋“  ๋ชจ๋“  ๊ฐ’๋“ค์€ ํ†ต๊ณ„๋Ÿ‰์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ‘œ๋ณธ๋“ค ์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜, ํ‘œ๋ณธ ์ค‘ ํ™€์ˆ˜ ๋ฒˆ์งธ ํ‘œ๋ณธ๋งŒ ๋”ํ•œ ๊ฐ’ ๋“ฑ๋„ ์‚ฌ์šฉํ•  ์ผ์€ ๋ณ„๋กœ ์—†์„ ์ˆ˜ ์žˆ์ง€๋งŒ ํ•˜๋‚˜์˜ ํ†ต๊ณ„๋Ÿ‰์ž…๋‹ˆ๋‹ค. ์ž์œ ๋„ ๋Œ€ํ‘œ์ ์ธ ์  ์ถ”์ •๋Ÿ‰์ธ ํ‘œ๋ณธํ‰๊ท ( โ€• )๊ณผ ํ‘œ๋ณธ๋ถ„์‚ฐ( 2 )์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•ด์ง‘๋‹ˆ๋‹ค. โ€• 1 โˆ‘ = n i 2 1 โˆ’ โˆ‘ = n ( i X) ๋ถ„๋ช… ๋‘ ์  ์ถ”์ •๋Ÿ‰ ๋ชจ๋‘ ๊ฐœ์˜ ์ „์ฒด ์ž๋ฃŒ๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ ๊ตฌํ•˜๋Š”๋ฐ, ํ‘œ๋ณธํ‰๊ท ์€ ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ฃผ๋Š” ๊ฒƒ์— ๋ฐ˜์— ํ‘œ๋ณธ๋ถ„์‚ฐ์€ โˆ’๋กœ ๋‚˜๋ˆ„์–ด์ค๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํ‘œ๋ณธ๋ถ„์‚ฐ์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์—์„œ ํ‘œ๋ณธํ‰๊ท ์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์ž์œ ๋„์™€ ๊ด€๋ จ๋œ ๊ฒƒ์œผ๋กœ, ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ์™€ ํ•จ๊ป˜ ์•Œ์•„๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค. ์ž์œ ๋„(degree of freedom)๋Š” ์ž์œ ๋กญ๊ฒŒ ๊ฐ’์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋กœ, ํ†ต๊ณ„๋Ÿ‰์˜ ๊ด€์ ์—์„œ ๋ดค์„ ๋•Œ, ์˜จ์ „ํžˆ ํ•ด๋‹น ํ†ต๊ณ„๋Ÿ‰์˜ ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ์— ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฃŒ ์ˆ˜๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์•„์ด๋””์–ด๋ฅผ ๋– ์˜ฌ๋ฆด ๋งŒํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ํ•˜๋‚˜ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 4๋ช…์˜ ์•„์ด๋“ค์—๊ฒŒ ์•„์ด์Šคํฌ๋ฆผ์„ ์‚ฌ์ฃผ๋ ค๊ณ  ํ•˜๋Š” ์ƒํ™ฉ์„ ๋– ์˜ฌ๋ ค ๋ด…์‹œ๋‹ค. ๋งˆ์ผ“์—์„œ ์•„์ด์Šคํฌ๋ฆผ์€ ์ข…๋ฅ˜๊ฐ€ ๋‹ค๋ฅธ 4๊ฐœ์˜ ์•„์ด์Šคํฌ๋ฆผ๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋กœ ๊ณจ๋ผ๊ฐ€๋Š” ์•„์ด๋Š” 4๊ฐœ ์ค‘์— ๋จน๊ณ  ์‹ถ์€ ๊ฒƒ์„ ๊ณ ๋ฅผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ ๊ณจ๋ผ๊ฐ€๋Š” ์•„์ด๋Š” 3๊ฐœ ์ค‘์— ๊ณจ๋ผ๊ฐ€๊ณ , ์„ธ ๋ฒˆ์งธ ์•„์ด๋Š” 2๊ฐœ ์ค‘์— ๊ณจ๋ผ๊ฐ‘๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์•„์ด๋Š” ๋‚จ๋Š” ๊ฒƒ์„ ๋จน์–ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์•„์ด์Šคํฌ๋ฆผ์„ ์ž์œ ๋กญ๊ฒŒ ์„ ํƒํ•œ ์•„์ด๋Š” ์ด 3๋ช…, ์ž์œ ๋„๋Š” 3์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ์ƒํ™ฉ์„ ํ‘œ๋ณธ๋ถ„์‚ฐ์— ์ ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  โ€• ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ์  ์ถ”์ •๋Ÿ‰์ธ โ€• ์€ ํ™•๋ฅ ๋ณ€์ˆ˜์ด์ง€๋งŒ, ์‹ค์ œ ํ‘œ๋ณธ๋ถ„์‚ฐ์€ ๊ตฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ ๊ฐ’์€ ์กฐ์‚ฌ๋œ ํ‘œ๋ณธ์—์„œ ์–ป์–ด์ง„ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๊ฐ’์ด ๊ณ ์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆœ์ฐจ์ ์œผ๋กœ ์ƒ๊ฐํ•˜๋ฉด, 1 ์€ ๊ฐ’์„ ์ž์œ ๋กญ๊ฒŒ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2 X ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ์ญ‰ ์ง„ํ–‰๋˜์–ด์„œ n 1 ๊นŒ์ง€, ํ™•๋ฅ ๋ณ€์ˆ˜ i ๋Š” ์ž์œ ๋กญ๊ฒŒ ๊ฐ’์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งˆ์ง€๋ง‰์— ํ•ด๋‹น๋˜๋Š” n ์€ ์ž์œ ๋กญ๊ฒŒ ๊ฐ’์„ ์„ ํƒํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๊ณ ์ •๋˜์–ด ์žˆ๋Š” ๊ฐ’, ํ‘œ๋ณธ์„ ํ†ตํ•ด ๊ตฌํ•ด์ง„ ํ‰๊ท ( โ€• )์„ ๋งž์ถ”์–ด ์ฃผ๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ์ด์œ ๋กœ ํ‘œ๋ณธํ‰๊ท ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ์‹œ, ์ž์œ ๋„๋Š” n์ด ์•„๋‹Œ n-1๋กœ ๊ณ„์‚ฐ์ด ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด โˆ’ ์€ ์˜จ์ „ํžˆ ํ‘œ๋ณธ๋ถ„์‚ฐ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ ์ž๋ฃŒ์˜ ๊ฐœ์ˆ˜์ž…๋‹ˆ๋‹ค. 1๊ฐœ์˜ ์ž๋ฃŒ๋Š” ์œ„์™€ ๊ฐ™์€ ๋…ผ๋ฆฌ๋กœ โ€• ๋ผ๋Š” ํ‘œ๋ณธํ‰๊ท ์„ ๋งž์ถฐ์ฃผ์–ด, 2 ์„ ์„ฑ๋ฆฝํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ–ˆ์œผ๋‚˜ 2 ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํผ์ง์˜ ์ •๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •๋ณด๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ๋Š” ์•„๋ฌด ๊ธฐ์—ฌ๋ฅผ ํ•˜์ง€ ๋ชปํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. B1. t ๋ถ„ํฌ(student's t-distribution) ๋ถ„ํฌ๋ฅผ ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๋ ค๋ฉด, ์ด ๋ถ„ํฌ๋Š” ์ˆœ์ „ํžˆ ํ‰๊ท  ๊ฒ€์ •์„ ํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋˜์—ˆ๋‹ค๋Š” ์ ์„ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ชจ์ง‘๋‹จ์ด ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ํ•˜๋ฉด, ํ‘œ๋ณธํ‰๊ท ์€ ( , 2 ) ์„ ๋”ฐ๋ฅธ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์‹ค ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ณผ๊ฑฐ์—๋Š” ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‰๊ท  ๊ฒ€์ •์„ ํ•ด์™”๋Š”๋ฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ์šฐ๋ฆฌ๋Š” ๋ชจ๋ถ„์‚ฐ์ธ 2 ์„ ์•Œ ๊ธธ์ด ์—†์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ๋งค์šฐ ํฌ๋‹ค๋ฉด ํ‘œ๋ณธํ‰๊ท ์€ ๋”์šฑ ์ •ํ™•ํžˆ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅผ ๊ฒƒ์ด๊ณ , ํ‘œ๋ณธํ‰๊ท ์˜ ๋ถ„์‚ฐ ์—ญ์‹œ 0์œผ๋กœ ์ ์ฐจ ์ˆ˜๋ ดํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์‚ฌ์‹ค์ƒ 2 ์˜ ์˜ํ–ฅ์ด ๋ฏธ๋ฏธํ•˜๊ฒŒ ๋˜์–ด ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ํ‘œ๋ณธ ์ˆ˜๊ฐ€ ์ž‘์„ ๋•Œ๋Š” ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ถ„์‚ฐ 2 ์„ ์ •ํ™•ํžˆ ์•Œ ์ˆ˜ ์—†์„ ๋ฟ ์•„๋‹ˆ๋ผ, ๊ทธ ๊ฐ’์— ๋”ฐ๋ผ ์ •๊ทœ๋ถ„ํฌ์˜ ๋ชจ์–‘์ด ํฌ๊ฒŒ ์ขŒ์ง€์šฐ์ง€๋˜์–ด, ์ •๊ทœ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•œ ๊ฒ€์ •์ด ๊ทธ ์‹ ๋ขฐ์„ฑ์„ ์žƒ๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ์˜ ๊ฒ€์ •์„ ์œ„ํ•ด, ์ •๊ทœ๋ถ„ํฌ์™€ ํ˜•ํƒœ๋Š” ๋น„์Šทํ•˜์ง€๋งŒ ๋ชจ๋ถ„์‚ฐ ํ•ญ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์ง€ ์•Š๊ณ , ๋Œ€์‹  ํ‘œ๋ถ„๋ถ„์‚ฐ์„ ์ด์šฉํ•œ ๋ถ„ํฌ๋ฅผ ๊ณ ์•ˆํ•ด ๋‚ด๋Š”๋ฐ, ๊ทธ๊ฒƒ์ด ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. ๋ถ„ํฌ๋Š” ์ •๊ทœ๋ถ„ํฌ์™€ ๊ฐ™์ด ์ค‘์‹ฌ์„ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์šฐ ๋Œ€์นญ์ด๊ณ  ์ข… ๋ชจ์–‘์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ–๊ณ  ์ค‘์‹ฌ์€ 0์œผ๋กœ ๊ณ ์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์œ„์—์„œ ๋‹ค๋ฃฌ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์™€ ์ค‘์‹ฌ์ด ๊ฐ™๊ณ  ์ž์œ ๋„(degree of freedom, df)์— ๋”ฐ๋ผ ์ข…์˜ ํ˜•ํƒœ๊ฐ€ ์กฐ๊ธˆ์”ฉ ๋ณ€ํ™”ํ•ฉ๋‹ˆ๋‹ค. df๋Š” ํ‘œ๋ณธ ์ˆ˜์™€ ๊ด€๋ จ์ด ์žˆ๋Š” ๊ฐœ๋…์œผ๋กœ, ํ‘œ๋ณธ์ด ๋งŽ์•„์ง€๋ฉด ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์™€ ๊ฑฐ์˜ ๋™์ผํ•œ ํ˜•ํƒœ๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. ์ด๋ฉด โˆผ ( ) ๋ฉด f ( ) ฮ“ ( + 2 ) ( 2 ) ฯ€ โ‹… ( y + ) + 2 โˆ’ < < E [ ] 0 [ ] n โˆ’ t ๋ถ„ํฌ์˜ ๋˜ ๋‹ค๋ฅธ ํŠน์ง•์€ ํ‘œ๋ณธ ์ˆ˜๊ฐ€ ์ ์œผ๋ฉด ์ ์„์ˆ˜๋ก ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์— ๋น„ํ•ด ์–‘์ชฝ ๊ผฌ๋ฆฌ๊ฐ€ ๋” ๋‘๊ป๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ์ดํ„ฐ๋“ค์ด ๊ทธ๋งŒํผ ์ค‘์‹ฌ์— ๋œ ๋ชจ์—ฌ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ •๊ทœ๋ถ„ํฌ์™€ ๋ถ„ํฌ๋Š” ๊ฒ€์ •์˜ ๊ด€์ ์—์„œ ๋ฐ”๋ผ๋ณผ ํ•„์š”๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•œ ๊ฒ€์ •์˜ ๊ฒฝ์šฐ, ์ค‘์‹ฌ์—์„œ ๋น„๊ต์  ์กฐ๊ธˆ๋งŒ ๋ฒ—์–ด๋‚˜๋„ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ด์ง€๋งŒ ๋ถ„ํฌ๋Š” ์ •๊ทœ๋ถ„ํฌ์— ๋น„ํ•ด ์ƒ๋Œ€์ ์œผ๋กœ ๋” ๋ฒ—์–ด๋‚˜๋„ ๊ฐ™๋‹ค๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ค๋‹ˆ๋‹ค. ์ด๋Š” ํ‘œ๋ณธ ์ˆ˜๊ฐ€ ์ ์€ ๋ฐ์„œ ๋‚˜์˜ค๋Š” ์šฐ์—ฐ์— ์˜ํ•œ ๊ทน๋‹จ์ ์ธ ๊ฐ’์— ๋Œ€ํ•ด์„œ๋„ ์–ด๋Š ์ •๋„ ์œ ์—ฐํ•œ ๊ฒ€์ • ๊ฒฐ๊ณผ๋ฅผ ์ค€๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ด ์—ญ์‹œ ํ‘œ๋ณธ์ด ์ ์„ ๋•Œ์˜ ๊ฒฝ์šฐ์ด๊ณ  ํ‘œ๋ณธ์ด ๋งŽ์•„์ง„๋‹ค๋ฉด ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์™€ ๊ฑฐ์˜ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค. ๊ทน๋‹จ์ ์œผ๋กœ ํ‘œ๋ณธ์ด ๋ฌดํ•œ์— ๊ฐ€๊นŒ์›Œ์ง„๋‹ค๋ฉด ๋ถ„ํฌ๋Š” ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์™€ ๋™์ผํ•œ ํ™•๋ฅ  ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์™€ ๋ถ„ํฌ์˜ ์ฐจ์ด๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ทธ๋ฆผ์œผ๋กœ ๋ถ„ํฌ์˜ ๊ฒฝ์šฐ ์ž์œ ๋„๊ฐ€ 3์ธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ๊ฐ ๊ทธ๋ž˜ํ”„์˜ ์ƒ‰์น ํ•œ ๋ถ€๋ถ„์€ ์ขŒ์šฐ ๊ฐ๊ฐ ๋ฐ์ดํ„ฐ์˜ 2.5%์”ฉ, ํ•ฉ์ณ์„œ 5%์˜ ์˜์—ญ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋“ฏ, ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋ณด๋‹ค๋Š” ๋ถ„ํฌ๊ฐ€ ํ›จ์”ฌ ๋” ์ค‘์‹ฌ์—์„œ ๋งŽ์ด ํผ์ ธ์žˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด๋Œ€๋กœ ๊ฒ€์ •์„ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋Š” ์ค‘์‹ฌ๊ณผ 2 ์ •๋„๋งŒ ์ฐจ์ด๋‚˜๋„ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ฃผ๊ฒ ์ง€๋งŒ, t ๋ถ„ํฌ๋Š” ์ค‘์‹ฌ๊ณผ 3์ด ์ฐจ์ด ๋‚œ๋‹ค๊ณ  ํ•˜์—ฌ๋„ ๊ฐ™๋‹ค๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ '๋ณด์ˆ˜์ ์ด๋‹ค' ํ˜น์€ '๋ณด์ˆ˜์ ์ธ ๊ฒ€์ •์ด๋‹ค'๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋Š”๋ฐ ์›ฌ๋งŒํผ ํ™•์‹ ์ด ์—†์œผ๋ฉด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ฃผ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ ‡๊ฒŒ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. ์ฆ‰, ํ‰๊ท ์— ๋Œ€ํ•œ ๊ฒ€์ •์—์„œ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•œ ๊ฒ€์ •๋ณด๋‹ค ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•œ ๊ฒ€์ •์ด ๋” ๋ณด์ˆ˜์ ์ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๊ฒƒ์€ ๊ฒ€์ • ํŒŒํŠธ์—์„œ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ๋‹ค์‹œ ๋‹ค๋ฃจ๊ฒ ์ง€๋งŒ ์„ธ ๊ฐ€์ง€ ์ •๋„๋Š” ๊ธฐ์–ตํ•ด๋‘๊ณ  ๊ฐ€๋„๋ก ํ•ฉ์‹œ๋‹ค. ์ •๊ทœ๋ถ„ํฌ์™€ ๋‹ค๋ฅด๊ฒŒ ๋ถ„ํฌ๋Š” ๋ชจ๋ถ„์‚ฐ 2 ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์ง€ ์•Š๋‹ค. ์ด๋Š” ํ‘œ๋ณธ ์ˆ˜๊ฐ€ ์ ์„ ๋•Œ ์‹ ๋ขฐ์„ฑ์„ ๋”ํ•ด์ค€๋‹ค. ํ‘œ๋ณธ ์ˆ˜๊ฐ€ ์ ์„ ๋•Œ, ๋ถ„ํฌ๋Š” ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ๋ณด๋‹ค ์–‘์ชฝ ๊ผฌ๋ฆฌ๊ฐ€ ๋” ๋‘ํ…๋‹ค. ์ด๋Š” ๋ณด๋‹ค ๋ณด์ˆ˜์ ์ธ ๊ฒ€์ •์„ ํ•˜๊ฒŒ ํ•ด์ค€๋‹ค. ํ‘œ๋ณธ ์ˆ˜๊ฐ€ ๋งŽ์•„์ง€๋ฉด ๋ถ„ํฌ์™€ ํ‘œ์ค€์ •๊ทœ๋ถ„ํฌ์— ๊ทผ์‚ฌํ•œ๋‹ค. B2. ์นด์ด์ œ๊ณฑ๋ถ„ํฌ์™€ F ๋ถ„ํฌ (Chi-square distribution and F-distribution) 11. 2 ๋ถ„ํฌ์™€ ๋ถ„ํฌ (Chi-square distribution and F-distribution) ํ†ต๊ณ„์—์„œ๋Š” ๋ณ€๋™(๋ถ„์‚ฐ)์€ ๋น„๊ต, ๊ด€๊ณ„ ๋“ฑ ๋ชจ๋“  ๋ถ„์„์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋ณ€๋™์€ ๋‹จ์ˆœํžˆ ํ•ด๋‹น ๋ณ€์ˆ˜์˜ ํผ์ง ์ •๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ๋ฟ ์•„๋‹ˆ๋ผ ๋น„๊ต์˜ ์‹ ๋ขฐ์„ฑ์„ ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ํฐ ์ฐจ์ด๊ฐ€ ์—†์–ด๋„ ๊ทธ ํ‰๊ท ์˜ ๋ณ€๋™์ด ํฌ๋‹ค๋ฉด ์‹ ๋ขฐํ•˜๊ธฐ ํž˜๋“ญ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ณ€๋™์€ ๋ณผ๋ฅจ์„ ๋œปํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. A๋ผ๋Š” ๋ณ€๋™๊ณผ B๋ผ๋Š” ๋ณ€๋™์ด ์žˆ์„ ๋•Œ, ๋น„๊ต๋ฅผ ํ†ตํ•˜์—ฌ ๋” ํฐ ๋ณผ๋ฅจ(ํŒŒ์›Œ, ํ™•์žฅ์„ฑ)์„ ๊ฐ€์ง„ ๋ณ€๋™์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ๋ณ€๋™์„ ์ธก์ •ํ•˜๋Š” ๋„๊ตฌ๋กœ ์ œ๊ณฑํ•ฉ ๊ตฌ์กฐ ( a) ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ณ€๋™์„ ์ธก์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€๋™์€ ์–ด๋–ค ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ ๊ด€์ฐฐ ๊ฐ’๋“ค์ด ๊ทธ ๊ธฐ์ค€๊ฐ’๊ณผ ์–ผ๋งˆํผ ๋–จ์–ด์ ธ ์žˆ๋Š”์ง€ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ธก์ •ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ํ‰๊ท ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋‹จ์ˆœํžˆ ํ‰๊ท ๊ณผ ๊ด€์ฐฐ ๊ฐ’๋“ค์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ทธ ํ•ฉ์ด 0์ด ๋˜์–ด ์˜๋ฏธ๊ฐ€ ์—†์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ์ด ๊ตฌ์กฐ์ ์ธ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ ์ž ๊ธฐ์ค€๊ฐ’๊ณผ์˜ ๊ฑฐ๋ฆฌ์˜ ์ œ๊ณฑ์„ ์ด์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋ณ€๋™์€ ์˜ค๋กœ์ง€ ์–‘์ ์ธ ๊ฐ’์œผ๋กœ๋งŒ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‘ ์ง‘๋‹จ์˜ ๋ณ€๋™์„ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋‘ ๋ณ€๋™์˜ ์ฐจ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ณค๋ž€ํ•ฉ๋‹ˆ๋‹ค. ๋น„๊ตํ•˜๋ ค๋Š” ์ง‘๋‹จ์˜ ๋‹จ์œ„๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์„๋ฟ๋”๋Ÿฌ ๊ฐ ์ง‘๋‹จ์˜ ์ˆ˜๋ฅผ ๋ฐ˜์˜ํ•ด ์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์˜ˆ์ปจ๋Œ€, ํ•œ ์ง‘๋‹จ์€ cm ๋‹จ์œ„๋กœ ์กฐ์‚ฌ๋˜์—ˆ๊ณ  ๋‹ค๋ฅธ ์ง‘๋‹จ์€ m ๋‹จ์œ„๋กœ ํ‘œํ˜„๋˜์–ด ์žˆ๋Š” ๊ธธ์ด๋ฅผ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ๊ฐ™์€ ์ˆ˜์ค€์˜ ๋ณ€๋™์„ ๊ฐ€์กŒ๋‹ค๊ณ  ํ•ด๋„ ๋ง‰์ƒ ์ œ๊ณฑํ•ฉ์„ ๊ตฌํ•ด๋ณด๋ฉด cm๋กœ ์กฐ์‚ฌ๋œ ์ง‘๋‹จ์˜ ๋” ํฌ๊ฒŒ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹จ์œ„๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๋˜ํ•œ 10๊ฐœ ๋ฐ์ดํ„ฐ์—์„œ์™€ 100๊ฐœ ๋ฐ์ดํ„ฐ์—์„œ์˜ ๋ถ„์‚ฐ์„ ๋˜‘๊ฐ™์ด ๋ณด๋ฉด ๊ณค๋ž€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” '์ œ๊ณฑ์˜ ํ•ฉ' ๊ผด๋กœ ํ‘œํ˜„๋˜๋ฏ€๋กœ ์ž๋ฃŒ ์ˆ˜๊ฐ€ ๋งŽ์œผ๋ฉด ๋งŽ์„์ˆ˜๋ก ๋”์šฑ ์ปค์งˆ ์ˆ˜๋ฐ–์— ์—†๋Š” ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ์ด๋Ÿฌํ•œ ํŠน์„ฑ๋“ค ๋•Œ๋ฌธ์—, ๋ณ€๋™์„ ์ด์šฉํ•˜์—ฌ ๊ฒ€์ •, ์ถ”์ •์„ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ๋“ค์„ ์ „๋ถ€ ๊ณ ๋ คํ•œ ํ™•๋ฅ  ๊ตฌ์กฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€๋™์˜ '๋‹จ์œ„' ์™€ '์ž๋ฃŒ ์ˆ˜'๋ฅผ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ณ ์•ˆ๋œ ๋ถ„ํฌ๊ฐ€ ๋ฐ”๋กœ 2 ๋ถ„ํฌ์ด๊ณ  2 ๋“ค์˜ ๋น„(ratio)๊ฐ€ ๋”ฐ๋ผ๋Š” ๋ถ„ํฌ๊ฐ€ ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. 2 ๋“ค์˜ ๋น„๋Š” ๋‘ ๋ณ€๋™์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์œผ๋กœ, ์ด ์—ญ์‹œ ์ž๋ฃŒ ์ˆ˜์™€ ๊ด€๋ จ๋œ ์ž์œ ๋„๊ฐ€ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. Chi_2 = rchisq(n = 100, df = 2) Chi_3 = rchisq(n = 100, df = 3) Chi_10 = rchisq(n = 100, df = 10) Chi_30 = rchisq(n = 100, df = 30) DF_Chi = data.frame( `df=2` = Chi_2, `df=3` = Chi_3, `df=10` = Chi_10, `df=30` = Chi_30 ) DF_Chi %>% melt() %>% ggplot() + geom_density(aes(x = value, fill = variable),alpha = 0.4) + theme_bw() + xlab("") + ylab("") + labs(fill = "") + theme(legend.position = "bottom") + ggtitle("์นด์ด์ œ๊ณฑ ๋ถ„ํฌ") F_11 = rf(n = 100, df1 = 1, df2 = 1) F_21 = rf(n = 100, df1 = 2, df2 = 1) F_52 = rf(n = 100, df1 = 5, df2 = 2) F_101= rf(n = 100, df1 = 10, df2 = 1) DF_F = data.frame( `df=1,1` = F_11, `df=2,1` = F_21, `df=5,5` = F_52, `df=10,1` = F_101 ) DF_F %>% melt() %>% ggplot() + geom_density(aes(x = value, fill = variable),alpha = 0.05) + theme_bw() + xlab("") + ylab("") + labs(fill = "") + theme(legend.position = "bottom") + xlim(0,5) + ggtitle("F ๋ถ„ํฌ") 2 ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ์•„์ด๋””์–ด๋Š” ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋Š” ๋ณ€์ˆ˜์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์„  ๋‹จ์œ„๋ฅผ ์ผ๋ฐ˜ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํ‘œ์ค€ํ™”๋ฅผ ํ•œ ํ›„ ์ œ๊ณฑ์„ ํ•˜๋ฉด ์ž์œ ๋„ 1์ธ 2 ๋ณ€์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ 2 ๋ณ€์ˆ˜๋Š” ๊ฐ€๋ฒ•์„ฑ์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ์„ฑ์งˆ์ด ์žˆ์–ด, ๋…๋ฆฝ์ ์ธ 2 ๋ณ€์ˆ˜๋ผ๋ฆฌ ๋”ํ•ด๋„ 2 ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ผ๋ฆฌ๋Š” ์„œ๋กœ ๋…๋ฆฝ โˆผ ( , ) โ‡’ 2 ฯ‡ ( f 1 ) โ‡’ i 1 Z 2 ฯ‡ ( f n ) ์ด๊ฒƒ์„ ์กฐ๊ธˆ ํ’€์–ด์„œ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. iid๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ๊ฐ™์€ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. i i d ( , 2 ) โ‡’ ( i ฮผ) Z โˆผ i d ( , ) โ‡’ ( ์œ„์™€ ๊ฐ™์ด ์ •๊ทœ๋ถ„ํฌ์˜ ์ œ๊ณฑํ•ฉ์€ 2 ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ถ„ํฌ๋Š” ๋…๋ฆฝ์ ์ธ 2 ๋ณ€์ˆ˜์˜ ๋น„๊ฐ€ ๋”ฐ๋ฅด๋Š” ๋ถ„ํฌ๋ผ๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ณผ๋Š” ๋…๋ฆฝ 1 ฯ‡ ( 1 ) Q โˆผ 2 ( 2 ) โ‡’ 1 n Q / 2 F ( 1 n) F ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ž์œ ๋„๋ฅผ ๋ฐ˜์˜ํ•œ ๋น„๊ต๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํ›„์— ๋ถ„์‚ฐ๋ถ„์„ ๋“ฑ์—์„œ ์‹ค์Šตํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. B3. ํ†ต๊ณ„์  ์ถ”์ •๊ณผ ๊ฒ€์ • 12. ํ†ต๊ณ„์  ์ถ”์ •๊ณผ ๊ฒ€์ • ์„ ๊ฑฐ์ฒ ์ด ๋‹ค๊ฐ€์˜ฌ ๊ฒฝ์šฐ, ์—ฌ๋Ÿฌ๋ถ„๋“ค์€ ์ˆ˜์—†์ด ๋งŽ์€ ํ‘œ๋ณธ์กฐ์‚ฌ ๊ฒฐ๊ณผ๋ฅผ ์—ฌ๋Ÿฌ ๋งค์ฒด๋ฅผ ํ†ตํ•ด ์ ‘ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. '์–ด๋Š ํ›„๋ณด์˜ ์ง€์ง€์œจ์ด OO%์ด๋ฉฐ ์‹ ๋ขฐ์ˆ˜์ค€ 95%์—์„œ ์กฐ์‚ฌ๊ฐ€ ๋˜์—ˆ๋‹ค.' ์ด๋Ÿฐ ๋ฌธ๊ตฌ๋Š” ๋งค์šฐ ์ต์ˆ™ํ•˜์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ†ต๊ณ„ ๋ถ„์„์˜ ๋ชฉ์ ์€ ๋ชจ์ง‘๋‹จ์„ ๋Œ€ํ‘œํ•  ์ˆ˜ ์žˆ๋Š” ํ‘œ๋ณธ์„ ์ˆ˜์ง‘ํ•œ ๋’ค, ๋ชจ์ง‘๋‹จ์— ๋Œ€ํ•ด ์ถ”์ •์„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ถ”์ •์€ ์  ์ถ”์ •๋Ÿ‰๊ณผ ๊ตฌ๊ฐ„์ถ”์ •๋Ÿ‰์œผ๋กœ ๋‚˜๋‰˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ถ”์ •๋Ÿ‰์ด๋ผ๊ณ  ํ•˜๋ฉด ์  ์ถ”์ •๋Ÿ‰์„ ์˜๋ฏธํ•˜๋ฉฐ ์ด๋Š” ๋ชจ์ˆ˜๋ฅผ ๋‹จ ํ•˜๋‚˜์˜ ์ ์œผ๋กœ ์ถ”์ธกํ•˜๋Š” ํ†ต๊ณ„๋Ÿ‰์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์šฐ๋ฆฌ๊ฐ€ A ์นดํŽ˜์— ๋ฐฉ๋ฌธํ•œ ๊ณ ๊ฐ๋“ค์˜ ์—ฐ๋ น๋Œ€๋ฅผ ์กฐ์‚ฌํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๋ชจ๋“  ๊ณ ๊ฐ์˜ ๋‚˜์ด ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•  ์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์ฃผ์–ด์ง„ ๊ณ ๊ฐ๋“ค์˜ ๋‚˜์ด ์ •๋ณด๋ฅผ ํ†ตํ•ด ์ „์ฒด ๊ณ ๊ฐ์˜ ํ‰๊ท  ๋‚˜์ด๋ฅผ ์†Œ์ˆ˜์ ๊นŒ์ง€ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์„ '์  ์ถ”์ •'์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ํ•œ์ •๋œ ์ •๋ณด๋งŒ์œผ๋กœ ์ „์ฒด ์ง‘๋‹จ(๋ชจ์ง‘๋‹จ)์˜ ์ •๋ณด๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ๋งž์ถ”๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์šฐ๋ฆฌ๋Š” ๋ชจ์ˆ˜(๊ณ ๊ฐ ์—ฐ๋ น์˜ ํ‰๊ท )๋ฅผ ํฌํ•จํ•˜๋Š” ์‹ ๋ขฐ๊ตฌ๊ฐ„์„ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ตฌ๊ฐ„์ถ”์ •์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. "ํ†ต๊ณ„ํ•™์ด ๋ฌด์—‡์„ ํ•˜๋Š” ํ•™๋ฌธ์ธ๊ฐ€?"๋ผ๋Š” ์งˆ๋ฌธ์ด ์ฃผ์–ด์ง€๋ฉด ํ†ต๊ณ„ํ•™์€ '์ถ”์ •'์„ ํ•˜๋Š” ํ•™๋ฌธ์ด๋ผ๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…์„ ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒ€์ •์ด๋ž€ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ๋งŽ์ด ๋“ค์–ด๋ณด์…จ์„๋ฒ•ํ•œ '๊ฐ€์„ค๊ฒ€์ •'์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ๊ฐ€์„ค์ด ํ†ต๊ณ„์ ์œผ๋กœ ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€ ๋ฐํ˜€๋‚ด๊ธฐ ์œ„ํ•œ ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ๋“ฑ์žฅํ•˜๋Š” ๋ชจ๋“  ๋ถ„์„ ๋ชจํ˜•์€ ์ถ”์ •๊ณผ ๊ฐ€์„ค๊ฒ€์ •์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์˜ˆ์™ธ์˜ ๊ฒฝ์šฐ๋„ ์žˆ์œผ๋‚˜ ๊ทธ ์˜ˆ์™ธ์˜ ๊ฒฝ์šฐ๋“ค์— ๋Œ€ํ•ด์„œ ์ด ์ฑ…์—์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. B4. ์  ์ถ”์ • 13. ์  ์ถ”์ • ์ถ”์ •๋Ÿ‰์€ ์šฐ๋ฆฌ๊ฐ€ ์•Œ๊ณ  ์‹ถ์–ด ํ•˜๋Š” ๋ชจ์ˆ˜๋ฅผ ํ‘œ๋ณธ๋“ค์„ ์ด์šฉํ•˜์—ฌ ๋‹จ ํ•˜๋‚˜์˜ ์ ์œผ๋กœ ์ถ”์ธกํ•˜๋Š” ํ†ต๊ณ„๋Ÿ‰์ž…๋‹ˆ๋‹ค. ๊ทธ ๊ณผ์ •์„ ์  ์ถ”์ •(Point estimation)์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ๊ทธ๋ ‡๊ฒŒ ์–ป์–ด์ง„ ํ†ต๊ณ„๋Ÿ‰์„ ์ ์ฃผ ์ฒญ๋Ÿ‰(Point estimator)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์  ์ถ”์ •๋Ÿ‰์€ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจํ‰๊ท ์„ ์ถ”์ •ํ•˜๊ธฐ ์œ„ํ•œ ํ‘œ๋ณธํ‰๊ท , ๋ชจ๋ถ„์‚ฐ์„ ์ถ”์ •ํ•˜๊ธฐ ์œ„ํ•œ ํ‘œ๋ณธํ‰๊ท  ๋“ฑ์ด ๋Œ€ํ‘œ์ ์ธ ์  ์ถ”์ •๋Ÿ‰์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ํ•˜๋‚˜์˜ ๋ชจ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์ถ”์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋์˜ ์ผ์ • ๋ถ€๋ถ„์”ฉ์€ ๋ฌด์‹œํ•˜๊ณ  ๋‚˜๋จธ์ง€ ํ‘œ๋ณธ๋“ค์˜ ํ‰๊ท  ๊ณ„์‚ฐ(์ ˆ์‚ญ ํ‰๊ท , Trimmed Mean) ์—ญ์‹œ ๋ชจํ‰๊ท ์„ ์ถ”์ •ํ•˜๋Š” ํ•˜๋‚˜์˜ ์  ์ถ”์ •๋Ÿ‰์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ญ์‹œ ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ๋Š” ์ฒ™๋„๋Š” ํ‘œ๋ณธํ‰๊ท ์ž…๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ˆ˜๋ฆฌ์ ์ธ ํ™•์žฅ์„ฑ๊ณผ ํ‘œ๋ณธํ‰๊ท ์˜ ๋ถ„ํฌ๋ฅผ ๋น„๊ต์  ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ด๋Ÿฐ ์  ์ถ”์ •์—๋„ ๋ช‡ ๊ฐ€์ง€์˜ ์žฅ์ ๊ณผ ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ ์ถ”์ •์˜ ์žฅ์  ์  ์ถ”์ •๋Ÿ‰์€ ์ง€๊ทนํžˆ ์ง๊ด€์ ์ด๋‹ค. ํ†ต๊ณ„๋ฅผ ๋ชจ๋ฅด๋Š” ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ํ•œ๊ตญ์˜ 30๋Œ€ ์—ฌ์„ฑ์˜ ํ‰๊ท  ์ˆ˜์ž…์„ ๋ฌป๋Š”๋‹ค๋ฉด ์  ์ถ”์ •๋Ÿ‰์œผ๋กœ ์ฆ‰๊ฐ์ ์ธ ๋‹ต์„ ์ค„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์  ์ถ”์ •๋Ÿ‰์€ ๋งค์šฐ ์ง๊ด€์ ์ด๋ฉฐ ํ•ฉ๋ฆฌ์ ์ž…๋‹ˆ๋‹ค. ์  ์ถ”์ •๋Ÿ‰์€ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์ˆ˜์น˜๋ฅผ ๋Œ€์ฒดํ•  ๊ตฌ์ฒด์ ์ธ ๊ฐ’์„ ์ œ์‹œํ•ด ์ค€๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ํ†ต๊ณ„์ ์ธ ๋ชจ๋ธ๋ง ํ˜น์€ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด 30๋Œ€ ์—ฌ์„ฑ ์ˆ˜์ž…์˜ ํ‰๊ท ์น˜๊ฐ€ ํ•„์š”ํ•˜๋‚˜ ๋ชจํ‰๊ท ์„ ์•Œ ์ˆ˜ ์—†์„ ๋•Œ, ์  ์ถ”์ •๋Ÿ‰์œผ๋กœ ๊ฐ„๋‹จํžˆ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค์ƒ ์ด๋Š” ๋Œ€๋ถ€๋ถ„์— ํ†ต๊ณ„ ์ด๋ก ์„ ์ „๊ฐœํ•˜๋Š”๋ฐ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ๋ง์”€๋“œ๋ฆฌ์ž๋ฉด ๋ชจ๋ถ„์‚ฐ์„ ์ถ”์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ‰๊ท ์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ‘œ๋ณธํ‰๊ท ์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ถ„์‚ฐ์€ ๊ฐ ๊ฐœ๋ณ„ ๊ฐ’๋“ค์ด ํ‰๊ท ์—์„œ ์–ผ๋งˆํผ ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ฒ™๋„์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์šฐ๋ฆฌ๋Š” '์ง„์งœ ํ‰๊ท '์„ ์•Œ ์ˆ˜ ์—†์œผ๋‹ˆ ํ‘œ๋ณธ๋“ค์˜ ํ‰๊ท ์œผ๋กœ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์  ์ถ”์ •๋Ÿ‰์€ ์ˆ˜๋ฆฌ์  ์—ฐ์‚ฐ์ด๋‚˜ ๋น„๊ต์— ์œ ์—ฐํ•˜๋‹ค. ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ๋Š” ๋‹ค๋ฅธ ์ง‘๋‹จ๊ณผ์˜ ๊ฒฐํ•ฉ์ด๋‚˜ ๋‹ค๋ฅธ ์ง‘๋‹จ์œผ๋กœ๋ถ€ํ„ฐ์˜ ๋ถ„๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฟ ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ๊ฐ€ ์ˆ˜์ •๋˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€๋˜์—ˆ์„ ๋•Œ๋„ ํ”ํžˆ ๋งŒ๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋„ ์  ์ถ”์ •๋Ÿ‰์€ ์œ ์—ฐํžˆ ๋Œ€์ฒ˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์  ์ถ”์ •๋Ÿ‰์˜ ๋‹จ์  ์  ์ถ”์ •๋Ÿ‰์€ ๊ทธ์ € ํ•˜๋‚˜์˜ ์ ์ž…๋‹ˆ๋‹ค. ์  ์ถ”์ •๋Ÿ‰์€ ๊ทธ ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ํ•˜๋‚˜์˜ ์ ์œผ๋กœ ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์žฅ์ ์ด๋ฉด์„œ ๋‹จ์ ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง๊ด€์ ์ด์ง€๋งŒ ํ•˜๋‚˜์˜ ์ ๋งŒ ๋ฏฟ๊ณ  ๋‹ค์Œ ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๊ธฐ์—๋Š” ๋„ˆ๋ฌด ๋ฆฌ์Šคํฌ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์  ์ถ”์ •๋Ÿ‰์ด ์ •ํ™•ํ•˜๊ฒŒ ๋ชจ์ˆ˜๋ฅผ ์ถ”์ •ํ•˜์ง€ ์•Š๊ณ  ๋™๋–จ์–ด์ง„ ๊ฐ’์„ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์„๋ฟ๋”๋Ÿฌ, ์„ค๋ น ์ •ํ™•ํ•˜๊ฒŒ ๋ชจ์ˆ˜์— ๊ทผ์ ‘ํ–ˆ๋‹ค๊ณ  ํ•˜์—ฌ๋„ ์ ์ถ”์ •๋Ÿ‰ ํ•˜๋‚˜๋งŒ ๋ณด๊ณ ์„œ๋Š” ๊ทธ ์‚ฌ์‹ค์„ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฐ™์€ ๋งฅ๋ฝ์œผ๋กœ, ์  ์ถ”์ •๋Ÿ‰์„ ์ด์šฉํ•ด ๋งŒ๋“  ๋‹ค๋ฅธ ์ถ”์ •๋Ÿ‰ ๋ฐ ํ†ต๊ณ„ ๋ชจํ˜•๋“ค์€ ๋ฌด์šฉ์ง€๋ฌผ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์  ์ถ”์ •๋Ÿ‰์€ ๋ณ€๋™์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ํ†ต๊ณ„๋Ÿ‰ ์—ญ์‹œ ํ™•๋ฅ ๋ณ€์ˆ˜์ด๋ฉฐ, ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๊ฐ€์ง€๋Š” ํŠน์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์  ์ถ”์ •๋Ÿ‰์€ ์ •ํ•ด์ง„ ๊ฐ’์ด ์•„๋‹Œ ๋ณ€๋™(๋ถ„์‚ฐ)์„ ๊ฐ–๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ปจ๋Œ€, '๋‚จ์„ฑ ํ‚ค'๋ผ๋Š” ๋ชจํ‰๊ท ์„ ์ถ”์ •ํ•˜๊ธฐ ์œ„ํ•ด ์ƒ˜ํ”Œ๋ง์„ ํ†ตํ•ด ํ‘œ๋ณธํ‰๊ท ์„ ๊ตฌํ•ด๋ณด๋‹ˆ 170์ด ๋‚˜์˜จ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. ์ด 170์€ ๋‹จ์ง€ ์ด๋ฒˆ ์กฐ์‚ฌ์—์„œ ๋‚˜์˜จ ๊ฐ’์— ๋ถˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์กฐ์‚ฌ์—์„œ๋Š” 168์ด ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ๊ณ  172๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์ฒจ์ถ”์ •๋Ÿ‰์˜ ๋ณ€๋™(๋ถ„์‚ฐ)์ด๊ณ  ์ด๋Š” ์  ์ถ”์ •๋Ÿ‰์˜ ๋ถ„ํฌ์™€ ํ‘œ๋ณธ ์ˆ˜ n์— ์˜ํ•˜์—ฌ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ ์ถ”์ •๋Ÿ‰ ํ•˜๋‚˜๋งŒ ๋ณด๊ณ ๋Š” ์ด ์  ์ถ”์ •๋Ÿ‰์ด ์–ผ๋งˆ๋‚˜ ๋†’์€ ๋ณ€๋™์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ถ”์ •๋Ÿ‰์˜ ๋ณ€๋™์ด ๋„ˆ๋ฌด ํฌ๋‹ค๋ฉด ํ•ด๋‹น ์ถ”์ •๋Ÿ‰์€ ์‹ ๋ขฐํ•  ์ˆ˜ ์—†๊ณ , ๋‹ค์‹œ ์กฐ์‚ฌํ•˜๋ฉด ์ง€๊ธˆ ๊ฐ’๊ณผ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๊ฐ’์ด ๋‚˜์˜ฌ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๊ทธ๋ ‡๊ธฐ์— ์ถ”์ •๋Ÿ‰์€ ๊ผญ ์ถ”์ •๋Ÿ‰์˜ ๋ณ€๋™๊ณผ ํ•จ๊ป˜ ๋ณด์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๊ตฌ๊ฐ„์ถ”์ •์ด์ž ๊ฐ€์„ค๊ฒ€์ •์˜ ์›๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๊ฒƒ์€ ๋‹ค์Œ ์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋„ˆ๋ฌด ์ ์€ ํ‘œ๋ณธ์—์„œ์˜ ์  ์ถ”์ •๋Ÿ‰์€ ์˜๋ฏธ๊ฐ€ ์—†๋‹ค. ์ด๋Š” 2๋ฒˆ๊ณผ ๊ทผ๋ณธ์ ์œผ๋กœ ๊ฐ™์€ ์ด์œ ์ง€๋งŒ ํ‘œ๋ณธ ์ˆ˜๋ผ๋Š” ๊ด€์ ์—์„œ ๋ฐ”๋ผ๋ณด๊ธฐ ์œ„ํ•ด ์ผ๋ถ€๋Ÿฌ ๋ถ„๋ฅ˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ‘œ๋ณธ ์ˆ˜๊ฐ€ ํฌ์ง€ ์•Š๋‹ค๋ฉด, ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜๋“ , ์ ์ถ”์ •์€ ์ด์ƒ์น˜์— ๋งค์šฐ ์ทจ์•ฝํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ์™€๋Š” ์ „ํ˜€ ์ƒ๊ด€์—†๋Š” ๊ฐ’์ด ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ถ”์ • ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ์ƒ๋Œ€์ ์œผ๋กœ ์ด์ƒ์น˜์˜ ์˜ํ–ฅ์„ ๋œ ๋ฐ›๋Š” ์ถ”์ •๋Ÿ‰๋„ ์žˆ๊ธฐ๋Š” ํ•ฉ๋‹ˆ๋‹ค๋งŒ, ๊ทธ ์–ด๋–ค ๊ฒƒ๋„ ์™„์ „ํžˆ ์ž์œ ๋กœ์šธ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ถ”์ •๋Ÿ‰์˜ ๋Œ€๋ถ€๋ถ„์€ ํ‰๊ท ์— ๊ธฐ์ธํ•œ ๊ฒƒ๋“ค์ธ๋ฐ, ํ‰๊ท ์€ ๋ชจ๋“  ์ž๋ฃŒ๊ฐ€ ๋™๋“ฑํ•˜๊ฒŒ ๋ฐ˜์˜๋˜๋ฏ€๋กœ ์ด์ƒ์น˜์— ์ทจ์•ฝํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋•Œ๋•Œ๋กœ ๋งค์šฐ ์ ์€ ์–‘์˜ ํ‘œ๋ณธ๋งŒ์„ ์ด์šฉํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์  ์ถ”์ •๋Ÿ‰์„ ํ•ด์„ํ•˜๊ธฐ ์–ด๋ ค์šด ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. B5. ๊ตฌ๊ฐ„์ถ”์ • 14. ๊ตฌ๊ฐ„์ถ”์ • ๊ตฌ๊ฐ„์ถ”์ •(interval estimation)์€ ์  ์ถ”์ •๋Ÿ‰์˜ ๋ณ€๋™์„ ์ด์šฉํ•ด์„œ ์–ด๋Š ์ •๋„์˜ ๊ตฌ๊ฐ„์„ ์ถ”์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ทธ ๊ฒฐ๊ณผ๋ฌผ์ด ๋งŽ์ด ๋“ค์–ด๋ณด์…จ์„ ์‹ ๋ขฐ๊ตฌ๊ฐ„(confidence interval, C.I)์ž…๋‹ˆ๋‹ค. ์‹ ๋ขฐ๊ตฌ๊ฐ„์˜ ๊ธธ์ด๋Š” ๋‹น์—ฐํžˆ ์งง์œผ๋ฉด ์งง์„์ˆ˜๋ก ์ข‹๊ณ  ๊ธธ๋ฉด ์‚ฌ์‹ค์ƒ ์‹ค๋ฌด์ ์œผ๋กœ ์•„๋ฌด ์˜๋ฏธ๊ฐ€ ์—†์„ ๊ฐ€๋Šฅ์„ฑ์ด ํฝ๋‹ˆ๋‹ค. ๊ตฌ๊ฐ„์ถ”์ •์˜ ๋ฐฉ๋ฒ•์€ ์ถ”์ •๋Ÿ‰์˜ ๋ถ„ํฌ๋ฅผ ๊ทผ์‚ฌ ํ˜น์€ ๊ฐ€์ •ํ•˜์—ฌ ๊ทธ ํ™•๋ฅ  ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋ถ€ํ„ฐ ์—ฌ๋Ÿฌ ๋ฒˆ์˜ ์žฌ์ถ”์ถœ์„ ํ†ตํ•ด ๊ฒฝํ—˜์ ์ธ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ๊นŒ์ง€ ๋‹ค์–‘ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ์ „์ž๋งŒ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ตฌ๊ฐ„์ถ”์ •์˜ ๊ธฐ๋ณธ์ ์ธ ์›๋ฆฌ๋Š” ์  ์ถ”์ • ๊ฐ’์— ๊ทธ ์  ์ถ”์ •๋Ÿ‰์˜ ํ™•๋ฅ  ๊ตฌ์กฐ์™€ ์  ์ถ”์ •๋Ÿ‰์˜ ๋ณ€๋™ ์ •๋„๋ฅผ ๊ฐ€๋ฏธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ตฌ๊ฐ„์ถ”์ •์˜ ๊ธฐ๋ณธ ์š”์†Œ๋Š” ๋‹ค์Œ 3๊ฐ€์ง€๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ ์ถ”์ •๊ฐ’ ์ ์ถ”์ •์„ ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจํ‰๊ท ์˜ ์‹ ๋ขฐ๊ตฌ๊ฐ„์„ ๊ตฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ํ‘œ๋ณธํ‰๊ท  ์ด์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์  ์ถ”์ •๋Ÿ‰์˜ ํ™•๋ฅ  ๊ตฌ์กฐ ๊ตฌ๊ฐ„์ถ”์ •์€ ์ ์ถ”์ •์˜ ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ์•Œ๊ณ  ์žˆ์„ ๋•Œ ์ด๋ฅผ ํ†ตํ•ด ๊ตฌํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„์„ ์ •ํ™•ํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„(exact confidence interval)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์‹ค ์‹ค์ œ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์ด๋ ‡๊ฒŒ ์ •ํ™•ํ•˜๊ฒŒ ๋ถ„์„์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์€ ๋งŽ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํ†ต๊ณ„์ ์ธ ๋ฐฉ๋ฒ•์„ ํ†ตํ•˜์—ฌ ์ถ”์ •๋Ÿ‰์˜ ๊ทผ์‚ฌ์ ์ธ ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์ด ๊ฒฝ์šฐ, ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ์ ๊ทผ์  ์‹ ๋ขฐ๊ตฌ๊ฐ„(asymptotic confidence interval) ํ˜น์€ ๊ทผ์‚ฌ์  ์‹ ๋ขฐ๊ตฌ๊ฐ„(approximate confidence interval)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ์ด ์ ๊ทผ์  ์‹ ๋ขฐ๊ตฌ๊ฐ„์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด ํ™•๋ฅ  ๊ตฌ์กฐ๋ฅผ ์–ด๋–ค ์‹์œผ๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์–ด๋–ค ํ™•๋ฅ  ํ•˜์—์„œ, ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ๋ฐ˜์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„, 90% ์‹ ๋ขฐ๊ตฌ๊ฐ„ ๋“ฑ, ์•ž์— ์‹ ๋ขฐ ์ •๋„๋ฅผ ํ™•๋ฅ ๋กœ ๋‚˜ํƒ€๋‚ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ์ถ”์ •๋Ÿ‰์˜ ๋ถ„ํฌ๋ฅผ ํ†ตํ•ด ํ—ˆ์šฉํ•  ์ˆ˜์ค€์„ ๊ฒฐ์ •ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์ง€์š”. ๋งŒ์•ฝ ์‹ ๋ขฐ์„ฑ์„ ๋†’์ธ๋‹ค๋ฉด ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ๊ธธ์–ด์งˆ ๊ฒƒ์ด๊ณ  ์•ฝ๊ฐ„ ๋Š์Šจํ•œ ์‹ ๋ขฐ์„ฑ์„ ์š”๊ตฌํ•˜๋ฉด ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ์งง์•„์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ถ”์ •๋Ÿ‰์˜ ํ‘œ์ค€ํŽธ์ฐจ(๋ณ€๋™์˜ ์ฒ™๋„) ๋งˆ์ง€๋ง‰์œผ๋กœ ์ถ”์ •๋Ÿ‰์˜ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ถ”์ •๋Ÿ‰์˜ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ํ”ํžˆ ํ‘œ์ค€์˜ค์ฐจ(standard error)๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ณ  ์ด๋Š” ๋‹จ์ˆœํžˆ ๋ชจ ํ‘œ์ค€ํŽธ์ฐจ์ฒ˜๋Ÿผ ๊ฐ ์ ๋“ค์ด ํ‰๊ท ๊ณผ ์–ผ๋งˆ๋‚˜ ๋–จ์–ด์ ธ ์žˆ๋ƒ์— ๋Œ€ํ•œ ๊ฒƒ ๋ง๊ณ ๋„ ํ•ด๋‹น ์ถ”์ •๋Ÿ‰์˜ ๋ณ€๋™ ์ •๋„๋กœ, ์‹ ๋ขฐ์„ฑ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ณ€๋™์ด ์ ์œผ๋ฉด ์‹ ๋ขฐํ•  ๋งŒํ•œ ์ถ”์ •๋Ÿ‰์ด๊ณ  ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์‹ ๋ขฐํ•˜๊ธฐ ํž˜๋“  ์ถ”์ •๋Ÿ‰์ด ๋˜๊ฒ ์ง€์š”. ๋งŒ์•ฝ ํ‘œ์ค€์˜ค์ฐจ๊ฐ€ ํฌ๋‹ค๋ฉด ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ๊ธธ์–ด์งˆ ๊ฒƒ์ด๊ณ  ํฌ์ง€ ์•Š๋‹ค๋ฉด ์งง์•„์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ถ„ํฌ๋ผ๋Š” ๊ฒƒ์— ๋ณ€๋™์ด ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 2๋ฒˆ๊ณผ 3๋ฒˆ์€ ์‚ฌ์‹ค ๊ฐ™์€ ๋‚ด์šฉ์ž…๋‹ˆ๋งŒ ํ‘œ์ค€์˜ค์ฐจ์— ๋Œ€ํ•œ ๊ฐœ๋… ์„ค๋ช…์„ ์œ„ํ•ด ๋ถ„๋ฆฌํ•ด ๋‘์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๋‚ด์šฉ์€ ์ฒ˜์Œ ์ฝ๊ณ ๋Š” ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์œผ๋‹ˆ ๊ผญ ๋ฐ‘์˜ ์˜ˆ์™€ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๊ณ  ๋‹ค์‹œ ์ฝ์–ด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์‹ ๋ขฐ๊ตฌ๊ฐ„์˜ ๊ธธ์ด๋Š” ๊ณง ์šฐ๋ฆฌ๊ฐ€ ์ด ๊ตฌ๊ฐ„์„ ์˜๋ฏธ ์žˆ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š”๊ฐ€์— ๋Œ€ํ•œ ์ฒ™๋„์ธ ๋™์‹œ์— ๊ฐ€์„ค๊ฒ€์ •์„ ํ–ˆ์„ ๋•Œ์˜ ๊ธฐ๊ฐํ•  ๋ฒ”์œ„๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๊ตฌ๊ฐ„์ถ”์ •์˜ ์›๋ฆฌ๋Š” ๊ฐ€์„ค๊ฒ€์ •์˜ ์›๋ฆฌ์™€ ์ •ํ™•ํžˆ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์„ค๊ฒ€์ • ์—ญ์‹œ ์  ์ถ”์ •๋Ÿ‰๊ณผ ๊ทธ ํ™•๋ฅ ๋ถ„ํฌ, ๋ณ€๋™ ์ •๋„๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ ์‹ค์‹œํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๊ฐ€์„ค๊ฒ€์ •์€ ํ•œ ์ ์— ๋Œ€ํ•œ ๊ฒ€์ • ๊ฒฐ๊ณผ๊ณ  ๊ตฌ๊ฐ„์ถ”์ •์€ ์ „์ฒด์ ์ธ ์‹ ๋ขฐํ•  ๋งŒํ•œ ๋ฒ”์œ„๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๋‹น์—ฐํžˆ ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด ์ •๋ณด๋ ฅ์ด ํ›จ์”ฌ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๊ตฌ์ฒด์ ์ธ ์˜ˆ๋ฅผ ๋“ค์–ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ ๋‚จ์„ฑ์˜ ํ‚ค๋Š” ์ •๊ทœ๋ชจ์ง‘๋‹จ์„ ๋”ฐ๋ฅธ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ 100๋ช…์˜ ํ‘œ๋ณธ์„ ๋ฝ‘์•„ ํ‰๊ท  ํ‚ค๋ฅผ ๊ตฌ๊ฐ„์ถ”์ •ํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ƒ˜ํ”Œ๋ง ๊ฒฐ๊ณผ ํ‘œ๋ณธ ํ‰๊ท ์€ 170, ํ‘œ๋ณธ ํ‘œ์ค€ํŽธ์ฐจ๋Š” 10์ด ๋‚˜์™”๊ณ  ์ด๋ฅผ ์ด์šฉํ•ด 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์šฐ๋ฆฌ๋Š” ์ด ํ‘œ๋ณธํ‰๊ท ๊ณผ ๊ด€๋ จ๋œ ํ™•๋ฅ  ๊ตฌ์กฐ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์œ ๋ช…ํ•œ ์ •๋ฆฌ๋ฅผ ํ•˜๋‚˜ ์‚ฌ์šฉํ•ด ๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค. ๋ชจ์ง‘๋‹จ์ด ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ๋„ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ŠคํŠœ๋˜ํŠธ ์ •๋ฆฌ์— ์˜ํ•ด ๋‹ค์Œ์ด ์„ฑ๋ฆฝํ•ฉ๋‹ˆ๋‹ค. = ( โ€• ฮผ / ) t ( โˆ’ ) ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์˜ ๋ถ„ํฌ๋ฅผ ๋„์ถœํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์—, t ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰๊ณผ ๊ด€๋ จ๋œ ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. [ X โˆ’ s n โ‰ค 0.975 ( โˆ’ ) ] 0.95 ๋งŒ์•ฝ ๊ฐ€ ์ •ํ™•ํ•˜๊ฒŒ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๋ฉด ์œ„ ์‹์€ ์ž๋ช…ํ•œ ์‚ฌ์‹ค์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ’€์–ด์„œ ์ƒ๊ฐํ•ด ๋ณด๋ฉด ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค.๋Š” ์ž์œ ๋„๊ฐ€ โˆ’ ์ธ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋ฏ€๋กœ ์ขŒ์šฐ ๋์— 2.5%์”ฉ ์ œ์™ธํ•œ ์˜์—ญ์— ํฌํ•จ๋  ํ™•๋ฅ ์ด 95%์ž…๋‹ˆ๋‹ค. ๋ถ„ํฌ๋Š” 0์„ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์šฐ๊ฐ€ ๋™์ผํ•œ ๋Œ€์นญ๋ถ„ํฌ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ขŒ์šฐ 2.5%์˜ ์˜์—ญ์„ ๋‚˜๋ˆ„๋Š” ๊ธฐ์ค€ ๊ฐ’์€ ๋ถ€ํ˜ธ๋งŒ ๋‹ค๋ฅผ ๋ฟ, ์ ˆ๋Œ“๊ฐ’์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. 0.975 ( โˆ’ ) ๋Š” ์ž์œ ๋„๊ฐ€ โˆ’ ์ธ ๋ถ„ํฌ์—์„œ 97.5%๊ฐ€ ๋ˆ„์ ๋˜์—ˆ์„ ๋•Œ์˜ ๊ฐ’์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  0.025 ( โˆ’ ) ๊ณผ ๋ถ€ํ˜ธ๋งŒ ๋‹ค๋ฅผ ๋ฟ, ์ ˆ๋Œ“๊ฐ’์€ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์—ญ ํ™•๋ฅ  ๊ฐ’์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” 100๋ช…์˜ ํ‘œ๋ณธ์„ ๋ฝ‘์•˜์œผ๋‹ˆ ๊ฐ’์€ ์ž์œ ๋„๊ฐ€ 99์ธ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ฒŒ ๋˜๊ณ , ์–‘์ชฝ ๊ทน๋‹จ์— 2.5%์”ฉ ๋‚จ๊ฒจ๋‘” ์—ญ ํ™•๋ฅ  ๊ฐ’์„ ๊ทธ๋ฆผ์œผ๋ฃŒ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. k1 = c() p1 = c() for(k in seq(-5,5, by = 0.01)){ p = dt(x = k, df = 99) k1 = c(k1,k) p1 = c(p1,p) } DF = data.frame( k1 = k1, p1 = p1 ) ggplot(DF) + geom_line(aes(x = k1, y = p1)) + geom_area(aes(x = ifelse(k1 > qt(p = 0.025, df = 99) & k1 < qt(p = 0.975, df = 99), k1, 0), y = p1), fill = 'red', alpha = 0.2) + geom_text(aes(x = 0, y = 0.2), label = "95%") + theme_bw() + scale_x_continuous(breaks = seq(-4,4, by = 1)) + scale_y_continuous(expand = c(0,0),limits = c(0,0.45)) + xlab("") + ylab("") B6. ๊ฐ€์„ค๊ฒ€์ • 15. ๊ฐ€์„ค๊ฒ€์ • ํ†ต๊ณ„์  ๊ฐ€์„ค๊ฒ€์ •์€ ์–ด๋–ค ๊ฐ€์„ค์„ ์„ธ์šฐ๊ณ  ๊ทธ ๊ฐ€์„ค์ด ์‚ฌ์‹ค์ผ ๊ฐ€๋Šฅ์„ฑ๊ณผ ์‚ฌ์‹ค์ด ์•„๋‹ ๊ฐ€๋Šฅ์„ฑ์„ ๋น„๊ตํ•ด์„œ ์˜์‚ฌ๊ฒฐ์ •์„ ํ•˜๋Š” ํ•˜๋‚˜์˜ ์ถ”๋ก  ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์–ธ์ œ๋‚˜ ๊ฐ•์กฐํ•˜์ง€๋งŒ, ๊ฐ€๋Šฅ์„ฑ์€ ๊ณง ํ™•๋ฅ ๋กœ ํ‘œํ˜„๋˜๊ณ  ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ™•๋ฅ ๋ถ„ํฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์ถ”์ •๋Ÿ‰์˜ ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๊ฐ€์ •ํ•˜๊ฑฐ๋‚˜ ๊ทผ์‚ฌ ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌ๊ฐ„์ถ”์ •๊ณผ ๋™์ผํ•œ ์•„์ด๋””์–ด, ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ฐ€์„ค๊ฒ€์ •์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ช‡ ๊ฐ€์ง€ ์šฉ์–ด์— ๋Œ€ํ•œ ํ•™์Šต์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฆฌํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค(Null Hypothesis, 0 ) ๊ท€๋ฌด๊ฐ€์„ค์€ ๋ถ„์„์ž๊ฐ€ ๊ฒ€์ •ํ•˜๊ธฐ๋ฅผ ์›ํ•˜๋Š” ๊ฐ€์„ค์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ํ†ต๊ณ„์  ๊ฐ€์„ค๊ฒ€์ •์˜ ์‹œ์ž‘์ด์ž ๋์ž…๋‹ˆ๋‹ค. ์™œ ๊ทธ๋Ÿฐ์ง€๋Š” ๊ฐ€์„ค๊ฒ€์ •์˜ ๊ณผ์ •์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์š”์•ฝํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์–ป์–ด์ง„ ํ‘œ๋ณธ์„ ์ด์šฉํ•ด ๊ท€๋ฌด๊ฐ€์„ค ํ•˜์—์„œ ๊ฐ€์ •๋˜๋Š” ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค ํ•˜์—์„œ ๊ฐ€์ •๋˜๋Š” ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๊ฒ€ํ† ํ•ด์„œ ์˜์‚ฌ๊ฒฐ์ •์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ผ ๊ฒƒ ๊ฐ™์œผ๋ฉด '๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•˜์ง€ ๋ชปํ•œ๋‹ค'๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋ฉฐ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด '๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•œ๋‹ค'๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ํ‘œํ˜„์„ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ๋ชจ๋“  ๊ฐ€์„ค๊ฒ€์ •์€ ๊ท€๋ฌด๊ฐ€์„ค ์ค‘์‹ฌ์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋Š” ํ†ต๊ณ„์ ์œผ๋กœ ๋งค์šฐ ์ค‘์š”ํ•œ ๊ด€์ ์ž…๋‹ˆ๋‹ค. ๊ฒ€์ •์˜ ๋ชจ๋“  ๊ณผ์ •์€ ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ด๋ผ๋Š” ๊ฐ€์ •ํ•˜์— ์ด๋ฃจ์–ด์ง€๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ์šฐ๋ฆฌ์˜ ๋ชฉํ‘œ๋Š” ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ์ฆ๋ช…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•˜์ง€ ๋ชปํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ด๋ผ๊ณ  ํ•ด์„ํ•˜๊ธฐ๋ณด๋‹ค๋Š” '๊ท€๋ฌด๊ฐ€์„ค์ด ํ‹€๋ ธ๋‹ค๋Š” ํ™•์‹คํ•œ ์ฆ๊ฑฐ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค' ์ •๋„๋กœ ์ƒ๊ฐํ•˜์‹œ๋Š” ๊ฒŒ ์ •ํ™•ํ•œ ์‹œ๊ฐ์ž…๋‹ˆ๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค(Alternative hypothesis, 1 ) ๋Œ€๋ฆฝ๊ฐ€์„ค์€ ๊ท€๋ฌด๊ฐ€์„ค์ด ๊ธฐ๊ฐ๋˜์—ˆ์„ ๋•Œ ๋ฐ›์•„๋“ค์—ฌ์ง€๋Š” ๊ฐ€์„ค์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋‹น์—ฐํ•˜๊ฒŒ๋„ ๊ท€๋ฌด๊ฐ€์„ค๊ณผ ์ˆ˜ํ•™์ ์œผ๋กœ ๋ฐฐํƒ€์ ์ž…๋‹ˆ๋‹ค(exclusive). ๋‘ ๊ฐ€์„ค์˜ ๊ต์ง‘ํ•ฉ์€ ์—†๊ณ  ๋‘ ๊ฐ€์„ค์˜ ํ•ฉ์ง‘ํ•ฉ์€ ์ „์ฒด์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. ๋Œ€๋ฆฝ๊ฐ€์„ค์€ ๊ฒ€์ •์˜ ๋Œ€์ƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์„ ํƒํ•œ ๊ท€๋ฌด๊ฐ€์„ค์— ๋ฐ˜ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์‹œ ์„ ํƒ๋˜๋Š” ๊ฒƒ๋ฟ์ด์ฃ . ๊ทธ๋ž˜์„œ ๋Œ€๋ถ€๋ถ„ ๋Œ€๋ฆฝ๊ฐ€์„ค์€ ๊ท€๋ฌด๊ฐ€์„ค์€ ์‚ฌ์‹ค์ด ์•„๋‹ˆ๋‹ค( 1 n t 0 )๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค. ์ œ1์ข… ์˜ค๋ฅ˜(type 1 error)์™€ ์ œ2์ข… ์˜ค๋ฅ˜(type 2 error) ๊ทธ๋ฆฌ๊ณ  ์œ ์˜์ˆ˜์ค€(significance level) 1์ข… ์˜ค๋ฅ˜๋Š” ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ผ ๋•Œ ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•  ํ™•๋ฅ ์ด๊ณ , ์ผ๋ฐ˜์ ์œผ๋กœ ๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. 2์ข… ์˜ค๋ฅ˜๋Š” ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ด ์•„๋‹ ๋•Œ, ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•˜์ง€ ์•Š์„ ํ™•๋ฅ ์ด๊ณ ๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ ๋ถ„์„์ž๊ฐ€ ์ž˜๋ชป๋œ ์„ ํƒ์„ ํ•˜๊ฒŒ ๋  ํ™•๋ฅ (์˜ค๋ฅ˜์œจ)์ž…๋‹ˆ๋‹ค. ์˜ค๋ฅ˜(Error)๋Š” ์–ด๋–ค ์˜ค๋ฅ˜๋ฅผ ๋ง‰๋ก ํ•˜๊ณ  ์ผ๋‹จ์€ ๋‚ฎ์€ ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ™์€ ํ‘œ๋ณธ ํ•˜์—์„œ ์šฐ๋ฆฌ๋Š” ์ด ๋‘ ๊ฐ€์ง€์˜ ์˜ค๋ฅ˜๋ฅผ ๋™์‹œ์— ์ค„์ด์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ํ”ํžˆ Trade - off ๊ด€๊ณ„๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.๋ฅผ ์ค„์ด๋ ค๊ณ  ํ•˜๋ฉด ๊ฐ€ ๋Š˜์–ด๋‚˜๋ฉฐ, ๋ฐ˜๋Œ€๋กœ ๋ฅผ ์ค„์ด๋ ค๊ณ  ํ•˜๋ฉด ๊ฐ€ ๋Š˜์–ด๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฒฝ์šฐ๋ฅผ ๋™์‹œ์— ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์€ ์˜ค๋กœ์ง€ ํ‘œ๋ณธ ์ˆ˜ n์„ ๋Š˜๋ ค๊ฐ€๋Š” ๋ฐฉ๋ฒ•๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๊ณ ์ •๋œ ํ‘œ๋ณธ ์ˆ˜ ํ•˜์—์„œ ์šฐ๋ฆฌ๋Š” ํ•˜๋‚˜์˜ ์˜ค๋ฅ˜์œจ์„ ๊ณ ์ •์‹œํ‚ค๊ณ  ๋ถ„์„์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๊ด€์‹ฌ ์žˆ๋Š” ๊ฒƒ์€ ์–ธ์ œ๋‚˜ ๊ท€๋ฌด๊ฐ€์„ค์ด๋ฏ€๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ผ ๋•Œ ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•  ํ™•๋ฅ , ์ฆ‰ 1์ข… ์˜ค๋ฅ˜๋ฅผ ๊ณ ์ •์‹œํ‚ต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ณ ์ •์‹œํ‚จ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ ํ•œ๊ณ„์น˜๋ฅผ ์ •ํ•ด๋‘”๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ฐ€์„ค๊ฒ€์ •์„ ํ•  ๋•Œ = 0.05 ํ˜น์€ = 0.1 ๊ณผ ๊ฐ™์€ ๋ฌธ๊ตฌ๋“ค์„ ๋ณด์•˜์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์•ŒํŒŒ๊ฐ€ ๋ฐ”๋กœ ์ œ1์ข… ์˜ค๋ฅ˜์˜ ํ—ˆ์šฉ์น˜์ž…๋‹ˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ผ ๋•Œ ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•  ํ™•๋ฅ ์˜ ํ•œ๊ณ„์น˜๋ฅผ 5% ํ˜น์€ 10%๋กœ ๊ณ ์ •์‹œํ‚ค๊ณ  ๋ถ„์„์„ ํ•˜๋Š” ๊ฒƒ์ด์ฃ . ์ด ์ตœ๋Œ€ ํ—ˆ์šฉ์น˜๋ฅผ ์œ ์˜์ˆ˜์ค€์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์œ ์˜์ˆ˜์ค€๊ณผ 1์ข… ์˜ค๋ฅ˜๋Š” ๊ฐ™์ง€๋งŒ ์ด์‚ฐํ˜•๊ณผ ๊ฐ™์ด ํ™•๋ฅ ์„ ์—ฐ์†์ ์œผ๋กœ ์ปจํŠธ๋กคํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ์œ ์˜์ˆ˜์ค€๋ณด๋‹ค 1์ข… ์˜ค๋ฅ˜๊ฐ€ ์ž‘์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ๋Œ€๋ถ€๋ถ„ ์—ฐ์†ํ˜• ๊ฒ€์ •์„ ๋‹ค๋ฃจ๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋ถ€๋ถ„ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰(test statistic) ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ๊ฒ€์ •์— ์‚ฌ์šฉ๋˜๋Š” ํ†ต๊ณ„๋Ÿ‰์ž…๋‹ˆ๋‹ค. ์ด ์—ญ์‹œ ํ•˜๋‚˜์˜ ํ†ต๊ณ„๋Ÿ‰์ด๋ฏ€๋กœ ํ‘œ๋ณธ๋“ค์˜ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ‘œ๋ณธ๋“ค์˜ ํ•จ์ˆ˜๋ผ๋Š” ๋ง์„ ์กฐ๊ธˆ ๊ฐ€๋ณ๊ฒŒ ํ‘œํ˜„ํ•˜๋ฉด ํ‘œ๋ณธ๋“ค์„ ์ด๋ฆฌ์ €๋ฆฌ ์กฐํ•ฉํ•ด์„œ ๋งŒ๋“ ๋‹ค๋Š” ๋ง์ด์ฃ . ๊ทธ๋ ‡๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ํ•˜๋‚˜์˜ ๊ถ๊ธˆ์ฆ์ด ์ƒ๊ธฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. '์ด๋ฆฌ์ €๋ฆฌ ์กฐํ•ฉ'ํ•˜๋Š” ๊ธฐ์ค€์€ ๋ญ˜๊นŒ์š”? ์ฆ‰, ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์˜ ํ˜•ํƒœ์— ๋Œ€ํ•œ ๊ถ๊ธˆ์ฆ์ž…๋‹ˆ๋‹ค. ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•˜์‹œ์ง€ ๋งˆ์‹œ๊ณ  ์œ„์—์„œ ํ•™์Šตํ•œ ๊ตฌ๊ฐ„์ถ”์ •๊ณผ ๊ฐ™์€ ์•„์ด๋””์–ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ํŽธํ•ฉ๋‹ˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค ํ•˜์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋„๋ก ์กฐํ•ฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์กฐํ•ฉ์˜ ๊ฒฐ๊ณผ๋ฌผ์ด ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ธ ๊ฒƒ์ด์ฃ . ์ด๋Š” ํ™•๋ฅ ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ํ•ต์‹ฌ ์•„์ด๋””์–ด์ž…๋‹ˆ๋‹ค. ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ๊ฒ€์ •ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ€์„ค์˜ ๋ถ„ํฌ์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•˜๊ฒŒ ์กฐํ•ฉ์ด ๋ฉ๋‹ˆ๋‹ค. ํ‰๊ท  ๊ฒ€์ •์„ ํ•˜๋Š” ๊ฒฝ์šฐ ์šฐ๋ฆฌ๋Š” ๋ถ„ํฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ t ๋ถ„ํฌ์˜ ์ŠคํŠœ๋˜ํŠธ ์ •๋ฆฌ๋ฅด ํ™œ์šฉํ•˜์—ฌ ๊ณ„์‚ฐ์ด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ถ„์‚ฐ์„ ๊ฒ€์ •ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ 2 ๋ถ„ํฌ ํ˜น์€ ๋ถ„ํฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ณ„์‚ฐ์ด ๋ฉ๋‹ˆ๋‹ค. ์œ ์˜ ํ™•๋ฅ (significance probability, p-value) ์œ ์˜ ํ™•๋ฅ ์€ ๊ท€๋ฌด๊ฐ€์„ค ํ•˜์—์„œ ๊ณ„์‚ฐ๋œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ ๊ฐ’๋ณด๋‹ค ๋” ๊ทน๋‹จ์ ์ธ ๊ฐ’์ด ๋‚˜์˜ฌ ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค. ํ”ํžˆ โˆ’ a u ํ˜น์€ ๊ฐ’์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ ๊ฐ€์„ค๊ฒ€์ •์—์„œ ์˜์‚ฌ ์„ ํƒ์„ ํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ์ค‘์š”ํ•œ ์ง€ํ‘œ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค ํ•˜์—์„œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ๋ถ„์„์ž๊ฐ€ ์˜๋„ํ–ˆ๋˜ ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๊ฐ€ ๊ณ„์‚ฐํ•œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ๊ทธ ํ™•๋ฅ  ๊ตฌ์กฐ์—์„œ ์–ด๋Š ์˜์—ญ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰๋ณด๋‹ค ๋” ๊ทน๋‹จ์ ์ธ ์˜์—ญ์— ์žˆ์„ ํ™•๋ฅ ์ด ๋ฐ”๋กœ ์œ ์˜ ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค. (๋งŒ์•ฝ ์˜ค๋ฅธ์ชฝ ๊ฒ€์ •์„ ํ•œ๋‹ค๋ฉด ๋” ์˜ค๋ฅธ ์ชฝ์— ์žˆ์„ ํ™•๋ฅ ์ด๊ณ  ์–‘์ธก ๊ฒ€์ •์„ ํ•œ๋‹ค๋ฉด ์–‘์ชฝ ๊ทน๋‹จ์— ์žˆ์„ ํ™•๋ฅ ์ผ ๊ฒ๋‹ˆ๋‹ค.) ์œ„์—์„œ ์‚ฌ์šฉํ•˜์˜€๋˜ ๋‚จ์„ฑ ํ‚ค๋ฅผ ์˜ˆ๋กœ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ๋ชจ์ง‘๋‹จ์—์„œ 100๋ช…์˜ ํ‘œ๋ณธ์„ ๋ฝ‘์•„์„œ ๋ชจํ‰๊ท ์ด 167.5๊ฐ€ ๋งž๋Š”์ง€ ๊ฒ€์ •์„ ํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. (์ฐธ๊ณ ๋กœ ์ด๋Š” ์ผํ‘œ๋ณธ ๊ฒ€์ •์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ๊ฐ€์„ค๊ฒ€์ •์ž…๋‹ˆ๋‹ค.) ํ‘œ๋ณธํ‰๊ท ์€ 170, ํ‘œ๋ณธํ‘œ์ค€ํŽธ์ฐจ๋Š” 10์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. = 100 X = 170 s 10 0 ฮผ 167.5 1 ฮผ 167.5 = 0.05 ๊ท€๋ฌด๊ฐ€์„ค์€ ๋ชจํ‰๊ท ์ด 167.5์ธ ๊ฒฝ์šฐ์ด๊ณ  ๋Œ€๋ฆฝ๊ฐ€์„ค์€ 167.5๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋Œ€๋ฆฝ๊ฐ€์„ค์€ 167.5๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ๋„ 167.5๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ๋„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. (์ด๋ฅผ ์–‘์ธก ๊ฒ€์ •์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.) ์œ„์—์„œ ๋ง์”€๋“œ๋ ธ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ๊ฐ€์„ค๊ฒ€์ •์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ™•๋ฅ  ๊ตฌ์กฐ๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๊ฐ€ ์•„๋Š” ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋„๋ก ํ‘œ๋ณธ๋“ค์„ ์กฐํ•ฉํ•ด์„œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ™•๋ฅ  ๊ตฌ์กฐ๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ๊ตฌ๊ฐ„์ถ”์ •์˜ ๊ฒฝ์šฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ŠคํŠœ๋˜ํŠธ ์ •๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์„ ๊ตฌํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ( โ€• ฮผ / ) t ( โˆ’ ) ๋งŒ์•ฝ ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ด๋ผ๋ฉด, = 167.5 ์ผ ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋‹ค์Œ์˜ ์‹์ด ์„ฑ๋ฆฝํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ( โ€• 167.5 / ) t ( โˆ’ ) ์—ฌ๊ธฐ์„œ n = 100์ด๋ฏ€๋กœ, ์œ„์˜ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ์ž์œ ๋„๊ฐ€ 99์ธ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ( 170 167.5 10 100 ) 2.5 ๊ณ„์‚ฐ๋œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ ๊ฐ’์„ ( 99 ) ์˜ ๊ทธ๋ž˜ํ”„ ์ƒ์— ์ด๋ฅผ ํ‘œํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. xbar = 170 sd = 10 n = 100 mu = 167.5 T_Statistics = (xbar - mu) / (sd / sqrt(n)) 1 - pt(q = T_Statistics, df = n-1) [1] 0.007031298 ggplot(DF) + geom_line(aes(x = k1, y = p1)) + geom_area(aes(x = ifelse(k1 > qt(p = 0.025, df = 99) & k1 < qt(p = 0.975, df = 99), k1, 0), y = p1), fill = 'red', alpha = 0.2) + geom_text(aes(x = 0, y = 0.2), label = "95%") + geom_vline(xintercept = qt(p = 1-0.007, df = 99),linetype = 'dashed') + geom_area(aes(x = ifelse(k1 < qt(p = 1-0.007, df = 99),0, k1), y = p1),fill = 'royalblue') + theme_bw() + scale_x_continuous(breaks = seq(-4,4, by = 1)) + scale_y_continuous(expand = c(0,0),limits = c(0,0.45)) + xlab("") + ylab("") ๋ณด์‹œ๋Š” ๋ฐ”์™€ ๊ฐ™์ด ( 99 ) ์˜ ๋ถ„ํฌ์—์„œ 2.5๋ณด๋‹ค ๋” ๊ทน๋‹จ์ ์ธ ๊ฐ’์„ ๋‚˜์˜ฌ ํ™•๋ฅ ์€ ์˜ค๋ฅธ์ชฝ์—์„œ 0.007์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์–‘์ธก๊ฒ€์ •์„ ํ•˜์˜€์œผ๋‹ˆ ๋ฐ˜๋Œ€์ชฝ๋„ ๊ณ„์‚ฐํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1 ฮผ 167.5 ์ด๋ž€ ๋ง์€ ๊ณง 1 ฮผ 167.5 r < 167 5 ์„ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ถ„ํฌ๋Š” ๋Œ€์นญ๋ถ„ํฌ์ด๋‹ˆ ๊ฐ™์€ ํ™•๋ฅ ์„ ๋ณด์ด๋ฉฐ, ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•œ ๊ฐ’๋ณด๋‹ค ๋” ๊ทน๋‹จ์ ์ธ ๊ฐ’์ด ๋‚˜์˜ฌ ํ™•๋ฅ (์œ ์˜ ํ™•๋ฅ , โˆ’ a u)๋Š” 0.014 ์ฆ‰, 1.4%๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์กฐ๊ธˆ ๋„“์€ ์˜๋ฏธ์—์„œ ํ•ด์„ํ•ด ๋ณด์ž๋ฉด, ๊ฐ’์€ (๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ผ ๋•Œ) ์šฐ๋ฆฌ๊ฐ€ ์–ป์€ ํ‘œ๋ณธ๋“ค์ด ์ผ๋ฐ˜์ ์œผ๋กœ ๋‚˜์˜ฌ ๋ฒ•ํ•œ ํ‘œ๋ณธ๋“ค์ด๋ƒ ์•„๋‹ˆ๋ƒ๋ฅผ ๋งํ•ด์ฃผ๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋ชจํ‰๊ท ์ด ์ •๋ง 167.5์ผ ๋•Œ, ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•œ 170์€ ๊ทธ๊ฒƒ๋ณด๋‹ค ๊ทน๋‹จ์ ์ธ ๊ฐ’์ด ๋‚˜์˜ฌ ํ™•๋ฅ ์ด 1.4% ์ •๋„ ๋ฐ–์— ์•ˆ๋  ์ •๋„๋กœ ๊ฝค๋‚˜ ์ด์ƒํ•œ ๊ฐ’์ด๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ด์–ด๋„ ์šฐ์—ฐํžˆ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์ด๊ธด ํ•˜์ง€๋งŒ ๊ทธ ํ™•๋ฅ ์ด 1.4% ์ •๋„ ๋ฐ–์— ์•ˆ๋œ๋‹ค๋Š” ์˜๋ฏธ๋กœ ํ•ด์„ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ผ ๋•Œ, ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•  ํ™•๋ฅ ์˜ ํ—ˆ์šฉํ•œ๊ณ„๋ฅผ 0.05๋กœ ๊ณ ์ •ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ์ด ์˜ˆ์—์„œ์˜ ๊ฐ’ 0.014๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ—ˆ์šฉํ•  ๋งŒํ•œ ์ˆ˜์ค€์ด ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒฐ๋ก ์„ ๋‚ด๋ฆฌ๊ณ  ๊ธฐ๊ฐํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ’์ด ํฐ ๊ฐ’์ด ๋‚˜์™”๋‹ค๋ฉด ๊ท€๋ฌด๊ฐ€์„ค ํ•˜์—์„œ ์ถฉ๋ถ„ํžˆ ๋‚˜์˜ฌ ๋งŒํ•œ ๊ฐ’์ด๋ผ๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•˜์ง€ ์•Š๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ€์„ค๊ฒ€์ •์€ ๊ฒฐ๊ตญ ๋‹จ์ˆœ ๋น„๊ต๋ฅผ ํ†ตํ•ด ๋งž๋ƒ ์•„๋‹ˆ๋ƒ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ถ”์ •๋Ÿ‰์˜ ๋ถ„ํฌ๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ ๋ณ€๋™์„ ํฌํ•จํ•ด๋„ ๋งž์„ ์ˆ˜ ์žˆ๋ƒ ์•„๋‹ˆ๋ƒ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ถ”์ •๋Ÿ‰๊ณผ ๊ทธ ์ถ”์ •๋Ÿ‰์˜ ๋ถ„ํฌ๋ฅผ ๋ฐ˜์˜ํ•˜๋Š” ๊ตฌ๊ฐ„์ถ”์ •๊ณผ ์•„์ด๋””์–ด์˜ ๋ฐฉํ–ฅ์ด ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์ด ๋‘˜์€ ๊ฐ™์€ ๋ถ„ํฌ๋ฅผ ์ด์šฉํ–ˆ๋‹ค๋ฉด ์ •ํ™•ํžˆ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๊ตฌ๊ฐ„์ถ”์ •์—์„œ ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฐ’์€ (์ฃผ์–ด์ง„ ์‹ ๋ขฐ๋„ ํ•˜์—์„œ) ๋ถ„ํฌ๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ๋„ ์ถฉ๋ถ„ํžˆ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์ด๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ฐ€์„ค๊ฒ€์ •๊ณผ ๊ฐ™์€ ์˜๋ฏธ์ฃ . ๊ทธ๋ž˜์„œ ์‹ ๋ขฐ๊ตฌ๊ฐ„์— ํฌํ•จ๋œ ๊ฐ’๋“ค์€ ๊ฐ€์„ค๊ฒ€์ •์„ ํ•˜์—ฌ๋„ ์ „๋ถ€ ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๊ฐ’๋“ค์€ ๊ฐ€์„ค๊ฒ€์ •์—๋„ ๊ธฐ๊ฐ๋  ๊ฐ’๋“ค์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํžˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ํ‘œ๋ณธ๋ถ„ํฌ(์ž์œ ๋„ 99์˜ t ๋ถ„ํฌ)๋ฅผ ํ™œ์šฉํ•œ ๊ตฌ๊ฐ„์ถ”์ •์˜ ๊ฒฝ์šฐ ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด [168.016, 171,984]์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ด๋ฒˆ์— ๊ฒ€์ •ํ•œ ๊ฐ’์€ 167.5์˜€์ฃ . ๊ตฌ๊ฐ„์— ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š๊ณ  ๊ทธ๋ ‡๊ธฐ์— ๋‹น์—ฐํžˆ = 167.5 ๋ผ๋Š” ๊ท€๋ฌด๊ฐ€์„ค์€ ๊ธฐ๊ฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค๋ฅธ ์ˆ˜๋ฅผ ๋„ฃ์–ด๋ด๋„ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ผ ๊ฒ๋‹ˆ๋‹ค. ๊ณ„์‚ฐ์ด ๊ทธ๋ฆฌ ๋ณต์žกํ•˜๋‹ˆ ์•Š์œผ๋‹ˆ ๋ช‡ ๊ฐ€์ง€๋ฅผ ๋„ฃ์–ด์„œ ํ™•์ธํ•ด ๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. 97.5%์˜ ๋ˆ„์  ์—ญ ํ™•๋ฅ  ๊ฐ’์€ 1.984์ด๊ณ  2.5%์˜ ๋ˆ„์  ์—ญ ํ™•๋ฅ  ๊ฐ’์€ -1.984์ž…๋‹ˆ๋‹ค.๋Š” ์ž์œ ๋„๊ฐ€ 99์ธ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋ฏ€๋กœ ๊ฐ€ -1.984์™€ 1.984์‚ฌ์ด์— ํฌํ•จ๋  ํ™•๋ฅ ์€ 95%๋ผ๋Š” ๊ฒƒ์ด ์„ฑ๋ฆฝํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์ € ์‚ฌ์‹ค์„ ๊ธฐ์ดˆํ•˜์—ฌ ์‹์„ ์กฐ๊ธˆ ๋ฐ”๊ฟ” ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 0.975 ( โˆ’ ) ์€ ์ž์œ ๋„ โˆ’์˜ ๋ถ„ํฌ์—์„œ ๋ˆ„์  ํ™•๋ฅ  0.975์˜ ์—ญ ํ™•๋ฅ  ๊ฐ’์ž…๋‹ˆ๋‹ค. ๊ต์žฌ์— ๋”ฐ๋ผ ์—ญ ํ™•๋ฅ  ๊ฐ’์˜ ํ‘œํ˜„์„ ๋’ค๋ถ€ํ„ฐ ๋ˆ„์ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋‹ˆ ์ฃผ์˜ ๋ฐ”๋ž๋‹ˆ๋‹ค. [ X โˆ’ s n โ‰ค 0.975 ( โˆ’ ) ] 0.95 P [ t 0.975 ( โˆ’ ) X โˆ’ s n t 0.975 ( โˆ’ ) ] 0.95 P [ โ€• t 0.975 ( โˆ’ ) s โ‰ค โ‰ค โ€• t 0.975 ( โˆ’ ) s ] 0.95 ์ด๋Ÿฐ ์‹์œผ๋กœ ์ •๋ฆฌํ•˜๋ฉด ์ฃผ์–ด์ง„ ์กฐ๊ฑด์—์„œ ๋ชจํ‰๊ท  ๊ฐ€ ์ € ๊ตฌ๊ฐ„์— ํฌํ•จ๋  ํ™•๋ฅ ์ด 95%๋ผ๋Š” ์‹์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๊ตฌ๊ฐ„์ถ”์ •์˜ ์›๋ฆฌ์ด์ž 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„์ž…๋‹ˆ๋‹ค. ์œ„์˜ ํ•œ๊ตญ ๋‚จ์„ฑ ํ‚ค๋ฅผ ๊ทธ๋Œ€๋กœ ๋Œ€์ž…ํ•ด ๋ณด๋ฉด โ€• ๋Š” 170,๋Š” 10, ์€ 100์ด๋ฏ€๋กœ, ํ•œ๊ตญ ๋‚จ์„ฑ ํ‚ค์˜ 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ [ 168.016 171.984 ] ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ฐ•์กฐํ•˜์ง€๋งŒ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ถ”์ •๋Ÿ‰์€ ํ™•๋ฅ  ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜๋ผ๋Š” ๊ฒƒ์ด๊ณ  ์ด๋Š” ๊ตฌ๊ฐ„์ถ”์ •์—๋„ ๊ทธ๋Œ€๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ๋งค ์กฐ์‚ฌ๋งˆ๋‹ค ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์œ„์—์„œ ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•œ [ 168.016 171 984 ] ๋Š” ์ ˆ๋Œ€์ ์ธ ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด ์•„๋‹Œ ๊ทธ๋ƒฅ ์ด๋ฒˆ ์กฐ์‚ฌ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•œ ํ•˜๋‚˜์˜ ์‹ ๋ขฐ๊ตฌ๊ฐ„์— ๋ถˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ ๋” ์กฐ์‚ฌํ•˜๋ฉด ๋ถ„๋ช…ํžˆ ๋ณ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์—„๋ฐ€ํžˆ ๋”ฐ์ง€๋ฉด [๋ชจ์ˆ˜๊ฐ€ ํ•ด๋‹น ์‹ ๋ขฐ๊ตฌ๊ฐ„ ์•ˆ์— ์žˆ์„ ํ™•๋ฅ ์ด 95%๋‹ค]๋ผ๋Š” ๋ง์€ ์ •ํ™•ํ•œ ๋ง์ด ์•„๋‹™๋‹ˆ๋‹ค. [ํ•ด๋‹น ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด ๋ชจ์ˆ˜๋ฅผ ํฌํ•จํ•  ํ™•๋ฅ ์ด 95%๋‹ค]๋ผ๋Š” ๋ง์ด ์ •ํ™•ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ์ˆ˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹Œ ์ด๋ฏธ ์ •ํ•ด์ง„ ์ˆ˜์ด๊ณ  ๋ฐ”๋€Œ๋Š” ๊ฑด ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๋” ์ •ํ™•ํžˆ ์ด์•ผ๊ธฐํ•˜๋ฉด [100๋ฒˆ ์กฐ์‚ฌ๋ฅผ ํ•ด์„œ 100๊ฐœ์˜ ์‹ ๋ขฐ๊ตฌ๊ฐ„์„ ์–ป์—ˆ์„ ๋•Œ ๊ทธ์ค‘ ํ‰๊ท ์ ์œผ๋กœ 95๊ฐœ์˜ ์‹ ๋ขฐ๊ตฌ๊ฐ„์€ ๋ชจ์ˆ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค] ์ •๋„๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ถ„์„์„ ์ผํ‘œ๋ณธ ๊ฒ€์ •(One Sample Test)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Height = rnorm(n = 98, mean = 170, sd = 10) print(paste("ํ‰๊ท  : ", round(mean(Height),2))) [1] "ํ‰๊ท  : 169.51" print(paste("ํ‘œ์ค€ํŽธ์ฐจ : ", round(sd(Height),2))) [1] "ํ‘œ์ค€ํŽธ์ฐจ : 9.87" t.test(x = Height, mu = 167.5, alternative = "two.sided") One Sample t-test data: Height t = 2.0176, df = 97, p-value = 0.0464 alternative hypothesis: true mean is not equal to 167.5 95 percent confidence interval: 167.5328 171.4891 sample estimates: mean of x 169.5109 ์œ„ ๊ฒฐ๊ณผ๋Š” ๋‚œ์ˆ˜ ๋ฐœ์ƒ์„ ํ†ตํ•œ ๋ถ„์„ ๊ฒฐ๊ณผ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๊ฐ€ 100% ์ผ์น˜ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค๋Š” ์ ์„ ์œ ์˜ํ•ด ์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ChB2. ๊ธฐ์ดˆํ†ต๊ณ„ ์ด๋ก  2๋‹จ๊ณ„ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋ณธ๊ฒฉ์ ์œผ๋กœ ํ†ต๊ณ„์  ์„ ํ˜•๋ชจํ˜•์— ๋Œ€ํ•ด ๋‹ค๋ฃน๋‹ˆ๋‹ค. A1. ํ†ต๊ณ„ ๋ชจํ˜• Preview 1. ํ†ต๊ณ„ ๋ชจํ˜• Preview ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋ชจ๋ธ๋ง์„ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ ๊ฐ„๋‹จํ•œ ์ฃผ์˜์‚ฌํ•ญ ๋ฐ ๋ถ„์„ ๋ชจํ˜•์— ๋Œ€ํ•œ ์†Œ๊ฐœ๋ฅผ ํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ”ํžˆ ์‚ฌ๋žŒ๋“ค์ด ๋ถ„์„ ๊ณผ์ •์—์„œ ์‹ค์ˆ˜ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ํž˜๋“ค๊ฒŒ ์–ด๋ ค์›Œ ๋ณด์˜€๋˜ ์˜ˆ์ธก ๋ชจํ˜• ๋ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต๋ถ€ํ•˜๊ณ  ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋ฐ”๋กœ ๋ชจ๋ธ์— ์ ํ•ฉ์‹œํ‚ค๋ ค๊ณ  ํ•˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋ณต์žกํ•œ ๋ชจํ˜•์„ ์ ์šฉ์‹œํ‚ค๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‚˜์˜ฌ ๊ฑฐ ๊ฐ™๊ณ  ๋ฉ‹๋„ ์žˆ์–ด ๋ณด์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ „ํ˜€ ๊ทธ๋ ‡์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ถ„์„์€ ์š”๋ฆฌ์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ์š”๋ฆฌ๋ฅผ ํ• ์ง€ ๊ฒฐ์ •ํ•˜๊ณ , ๊ฐ€์ ธ์˜จ ์žฌ๋ฃŒ๋ฅผ ๋ณด์ง€๋Š” ์•Š์ฃ . ๋ฐ์ดํ„ฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๋ถ„์„ ๋ชจํ˜•์€ ๋ฐ์ดํ„ฐ์— ๋งž๋Š” ๋ถ„์„ ๋ชจํ˜•์„ ์ ์šฉ์‹œ์ผœ์•ผ์ง€, ๋ฌด์ž‘์ • ์–ด๋ ค์šด ๋ถ„์„ ๋ชจํ˜• ์ ์šฉ์‹œํ‚จ๋‹ค๊ณ  ํ•ด์„œ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค ๋‚˜์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํ•ญ์ƒ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ํŠน์„ฑ์„ ์ถฉ๋ถ„ํžˆ ์ดํ•ดํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ์ด๋Ÿฐ ์ƒํ™ฉ์—๋Š” ์–ด๋–ค ๋ถ„์„ ๋ชจํ˜•์„ ์ ์šฉ์‹œ์ผœ์•ผ ํ•˜๋Š”์ง€ ๋ฐ”๋กœ ์ƒ๊ฐ์ด ๋– ์˜ฌ๋ผ์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ถ€๋ถ„์˜ ์ดํ•ด๋ฅผ ์œ„ํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ†ต๊ณ„ ๋ชจํ˜•๋“ค์— ๋Œ€ํ•˜์—ฌ ๋‹ค๋ฃจ๊ณ  ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A2. ๋ถ„์„ ๋ชจํ˜•์„ ์„ ํƒํ•˜๋Š” ๊ธฐ์ค€ 2. ๋ถ„์„ ๋ชจํ˜•์„ ์„ ํƒํ•˜๋Š” ๊ธฐ์ค€ ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ”๋ฅผ ๋ช…ํ™•ํžˆ ๊ทœ๋ช…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ€์„ค์„ ์„ธ์šฐ๋Š” ๊ณผ์ •๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ€์„ค์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋“ค์— ๋Œ€ํ•œ ๋ณ€์ˆ˜ ์ฒ™๋„ ๊ตฌ๋ถ„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ์ฒ™๋„์— ๋”ฐ๋ผ ์ ์šฉํ•ด์•ผ ๋˜๋Š” ๋ชจํ˜•์ด ์ •ํ•ด์ง‘๋‹ˆ๋‹ค. ๋ชจํ˜•์€ ์šฐ๋ฆฌ๊ฐ€ ์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ๋ฐ์ดํ„ฐ๊ฐ€ ์ •ํ•ด์ค๋‹ˆ๋‹ค. ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ์ฃผ์ œ๊ฐ€ โ€™์ฐจ์ดโ€™๋ฅผ ๊ฒ€์ •ํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์ธ์ง€, โ€™๊ด€๊ณ„โ€™๋ฅผ ๊ฒ€์ •ํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์ธ์ง€์— ๋”ฐ๋ผ ๊ฐˆ๋ฆฝ๋‹ˆ๋‹ค. ์ฐจ์ด๋ฅผ ๋ณด๋Š” ๊ฒ€์ •์€ ํ”ํžˆ ์ง‘๋‹จ ๊ฐ„์— ํ‰๊ท  ์ฐจ์ด๋ฅผ ๊ฒ€์ •ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ธ์‚ฌ๊ด€๋ฆฌ ๋ฐ์ดํ„ฐ์—์„œ ์ด์ง ์—ฌ๋ถ€(0: ์•ˆ ํ•จ, 1: ์ด์ง)์— ๋”ฐ๋ผ ์ง๋ฌด ๋งŒ์กฑ๋„๊ฐ€ ๋‹ค๋ฅธ์ง€ ๊ฒ€์ •ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ๊ด€๊ณ„๋ฅผ ๋ณด๊ณ ์ž ํ•˜๋Š” ๊ฒ€์ •์€ ๋ณ€์ˆ˜ ๊ฐ„์˜ ํ•จ์ˆ˜์  ๊ด€๊ณ„๋ฅผ ๋ณด๊ณ ์ž ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋งˆ์ผ€ํŒ… ํˆฌ์ž๋น„์šฉ์ด ๋งˆ์ผ€ํŒ… ํšจ๊ณผ์— ๋ฏธ์น˜๋Š” ์˜ํ–ฅ์„ ๊ฒ€์ •ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ๋Š” Response Variable(์ข…์† ๋ณ€์ˆ˜)๊ฐ€ ์—ฐ์†ํ˜•์ด๋ƒ, ์ด์‚ฐํ˜•์ด๋ƒ์— ๋”ฐ๋ผ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์ข…์† ๋ณ€์ˆ˜๊ฐ€ ์—ฐ์†ํ˜•์ผ ๋•Œ ์ฐจ์ด๋ฅผ ๋ณด๊ณ ์ž ํ•  ๋•Œ : T ๊ฒ€์ •(T-test), ๋ถ„์‚ฐ๋ถ„์„(Anova) ๊ด€๊ณ„๋ฅผ ๋ณด๊ณ ์ž ํ•  ๋•Œ : ํšŒ๊ท€๋ถ„์„(Regression) ์ข…์† ๋ณ€์ˆ˜๊ฐ€ ์ด์‚ฐํ˜•์ผ ๋•Œ ์—ฐ๊ด€์„ ๋ณด๊ณ ์ž ํ•  ๋•Œ : ์นด์ด ์ œ๊ณฑ ๋…๋ฆฝ์„ฑ ๊ฒ€์ •(Chi square Independent Test) ๊ด€๊ณ„๋ฅผ ๋ณด๊ณ ์ž ํ•  ๋•Œ : ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„(Logisitc Regression) A3. ๋ถ„์„ ๋ชจํ˜•๋ณ„ ๊ฐ€์„ค ๊ฒ€์ • 3. ๋ถ„์„ ๋ชจํ˜•๋ณ„ ๊ฐ€์„ค ๊ฒ€์ • ํ†ต๊ณ„ ๋ถ„์„์—์„œ ์ฐจ์ด๊ฐ€ ์—†๋Š”(์ฆ‰ ์˜๋ฏธ๊ฐ€ ์—†๋Š”) ์‚ฌ์‹ค์€ ์•Œ์•„๋‚ด๊ณ  ์‹ถ์€ ์‚ฌ์‹ค์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ฐจ์ด๊ฐ€ ์žˆ๋Š”(์ฆ‰ ์˜๋ฏธ๊ฐ€ ์žˆ๋Š”, ์œ ์˜ํ•˜๋‹ค) ์‚ฌ์‹ค์— ๊ด€์‹ฌ์ด ์žˆ์œผ๋ฉฐ, ์˜๋ฏธ๊ฐ€ ์žˆ๋Š” ์‚ฌ์‹ค์„ ๋Œ€๋ฆฝ๊ฐ€์„ค์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. A4. t ๊ฒ€์ • 4. ๊ฒ€์ • ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฒ€์ •์ด๋ผ ํ•˜๋ฉด ๋…๋ฆฝ ํ‘œ๋ณธ ๊ฒ€์ •์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋…๋ฆฝ์ ์ธ ๋‘ ์ง‘๋‹จ์—์„œ ์ถ”์ถœ๋œ ํ‘œ๋ณธ๋“ค์˜ ํ‰๊ท ์ด ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์‹ค์‹œ๋˜๋ฉฐ ํ˜น์‹œ ๋‘ ์ง‘๋‹จ์ด ์ƒํ™ฉ์ ์œผ๋กœ ๋…๋ฆฝ์ด ์•„๋‹Œ ์ง‘๋‹จ์ด๋ผ๋ฉด ๋Œ€์‘ ํ‘œ๋ณธ ๊ฒ€์ •(paired t-test) ๋“ฑ์„ ํฌํ•จํ•œ ๋‹ค๋ฅธ ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์›๋ฆฌ๋Š” ์œ„์—์„œ ๊ฐ€์„ค๊ฒ€์ • ํŒŒํŠธ์—์„œ ํ–ˆ๋˜ ์ผํ‘œ๋ณธ ๊ฒ€์ •๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ ๋…๋ฆฝ์ ์ธ ์ •๊ทœ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœ๋œ ํ‘œ๋ณธ์„ ํ†ตํ•ด ๊ณ„์‚ฐ๋œ ๋‘ ์ง‘๋‹จ์˜ ํ‘œ๋ณธํ‰๊ท  ์ฐจ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‘ ํ‘œ๋ณธํ‰๊ท  ์ฐจ์˜ ๋ถ„ํฌ๋ฅผ ํ™•์ธ ํ›„ ์šฐ๋ฆฌ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ‘œ๋ณธ์—์„œ ๊ณ„์‚ฐ๋œ ์ฐจ์ด๊ฐ€ ๋‘ ํ‰๊ท ์ด ๊ฐ™๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ(๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ผ ๋•Œ) ์ถฉ๋ถ„ํžˆ ๋‚˜์˜ฌ ๋ฒ•ํ•œ ์ฐจ์ด์ธ๊ฐ€๋ฅผ ๊ฒ€์ •ํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ๋…๋ฆฝ ํ‘œ๋ณธ t ๊ฒ€์ •์˜ ํŠน์„ฑ ๋ถ„์„์˜ ๋ชฉ์ ์€ ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ถ„์„์˜ ๋Œ€์ƒ์€ ๋…๋ฆฝ์ ์ธ ๋‘ ์ •๊ทœ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœ๋œ ํ‘œ๋ณธ๋“ค์ด๋‹ค. ๋ถ„์„์˜ ์ˆ˜๋‹จ์€ ๋ถ„ํฌ๋‹ค. ๋ถ„์„์˜ ์›๋ฆฌ๋Š” ์ถ”์ถœ๋œ ํ‘œ๋ณธ์œผ๋กœ ๊ณ„์‚ฐ๋œ ์ฐจ์ด๊ฐ€ ์šฐ์—ฐํžˆ ๋‚˜์˜ฌ๋งŒํ•œ ์ฐจ์ด์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋ถ„ํฌ(๋‘ ํ‘œ๋ณธํ‰๊ท  ์ฐจ์˜ ๋ถ„ํฌ)๋ฅผ ์ด์šฉํ•˜์—ฌ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ ํ•˜๋‚˜์˜ ๋ฌธ์ œ๊ฐ€ ์ƒ๊น๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๋ถ„์‚ฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ์ž…๋‹ˆ๋‹ค. ๊ฐ€์„ค๊ฒ€์ • ํŒŒํŠธ์—์„œ ํ–ˆ๋˜ ์ผํ‘œ๋ณธ ๊ฒ€์ •์˜ ๊ฒฝ์šฐ ์ง‘๋‹จ์ด ํ•˜๋‚˜๋ฐ–์— ์—†์œผ๋‹ˆ ์ƒ๊ด€์—†์ง€๋งŒ ์ง‘๋‹จ์ด ๋‘ ๊ฐœ์ธ ์ด ๊ฒฝ์šฐ์—์„œ๋Š” ๋ถ„์‚ฐ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ• ๊นŒ์š”? ๋…๋ฆฝ ํ‘œ๋ณธ ๊ฒ€์ •์—์„œ๋Š” ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์ด ๊ฐ™์€ ๊ฒฝ์šฐ์™€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ„์–ด ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ”ํžˆ ๋“ฑ ๋ถ„์‚ฐ ๊ฐ€์ •์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฒ€์ •์—์„œ๋Š” ๋‘ ์ง‘๋‹จ์˜ ๋“ฑ ๋ถ„์‚ฐ์ด ๊ฐ™์€ ๊ฒฝ์šฐ, ์ฆ‰, ๋“ฑ ๋ถ„์‚ฐ ๊ฐ€์„ฑ์ด ์„ฑ๋ฆฝ๋˜์—ˆ์„ ๋•Œ๋Š” ์ •ํ™•ํ•˜๊ฒŒ(exact) ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” ๊ทผ์‚ฌ์ ์ธ(approximate) ๊ฒ€์ •์„ ์‹ค์‹œํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ด€๊ณ„๋กœ, ๊ฒ€์ • ์ „์—๋Š” ๋ฐ˜๋“œ์‹œ ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์ด ๊ฐ™์€์ง€์— ๋Œ€ํ•œ ๊ฒ€์ •์„ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ๋“ฑ ๋ถ„์‚ฐ์„ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ F ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•œ Levene ๋“ฑ ๋ถ„์‚ฐ ๊ฒ€์ •(๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์€ ๋™์ผํ•˜๋‹ค 0 ๋‘ ๋‹จ ๋ถ„ ์€ ์ผ ๋‹ค )์„ ๋น„๋กฏํ•œ ๋ช‡ ๊ฐ€์ง€์˜ ๊ฒ€์ •์ด ์žˆ์œผ๋‚˜ ์—ฌ๊ธฐ์„œ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜์ง„ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‘ ์ง‘๋‹จ์˜ ๋“ฑ ๋ถ„์‚ฐ ๊ฐ€์ •์ด ์„ฑ๋ฆฝํ•œ๋‹ค๊ณ  ํŒ๋‹จ์ด ๋˜๋ฉด ์ง‘๋‹จ์— ์ƒ๊ด€์—†์ด ์ „์ฒด์˜ ๋ถ„์‚ฐ์„ ๊ณ„์‚ฐํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ฉ๋™ ํ‘œ๋ณธ๋ถ„์‚ฐ(pooled sample variance)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ 1 ์€ X ํ‘œ๋ณธ(์ง‘๋‹จ 1)์—์„œ์˜ ํ‘œ๋ณธ ์ˆ˜๊ณ  2 ๋Š” Y ํ‘œ๋ณธ(์ง‘๋‹จ 2)์—์„œ์˜ ํ‘œ๋ณธ ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•ฉ๋™ ํ‘œ๋ณธ๋ถ„์‚ฐ์˜ ํ˜•ํƒœ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‘ ์ง‘๋‹จ์˜์—์„œ์˜ ๋ณ€๋™์„ ๊ฐ๊ฐ ๊ณ„์‚ฐํ•˜๊ณ  ๋”ํ•œ ํ›„ ์ „์ฒด ์ž์œ ๋„๋กœ ๋‚˜๋ˆ„์–ด์ค€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์œ ๋„๋Š” ์—ญ์‹œ ๊ธฐ์กด ๋‹ค๋ฅธ ์ถ”์ •๋Ÿ‰๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•ฉ๋™ ํ‘œ๋ณธ๋ถ„์‚ฐ์„ ์ถ”์ •ํ•˜๋Š”๋ฐ ์˜จ์ „ํžˆ ์‚ฌ์šฉ๋œ ๋ฐ์ดํ„ฐ ์ˆ˜๋กœ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๊ฒƒ์„ pooling ํ•œ๋‹ค๊ณ  ํ‘œํ˜„ํ•˜๋ฉฐ ๋‹จ์ผ ์ง‘๋‹จ์—์„œ์˜ ํ‘œ๋ณธํ‰๊ท ๊ณผ ๋น„๊ตํ•ด ๋ณด๋ฉด pooling์ด ์–ด๋–ค ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ง€๋Š” ๊ฒƒ์ธ์ง€ ์–ด๋ ต์ง€ ์•Š๊ฒŒ ์ดํ•ดํ•˜์‹ค ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹จ์ผ ์ง‘๋‹จ์—์„œ์˜ ํ‘œ๋ณธํ‰๊ท  ( ์ผ ๋‹จ ์„œ ํ‘œ ํ‰ ) 2 1 โˆ’ โˆ‘ = n ( i X) ์ด์ œ ์ด๋ฅผ ์ด์šฉํ•ด ๋‘ ์ง‘๋‹จ์˜ ํ‘œ๋ณธํ‰๊ท  ์ฐจ์— ๋Œ€ํ•œ ๋ถ„ํฌ๋ฅผ ๋„์ถœํ•ด ๋ด…์‹œ๋‹ค. ๋‘ ๋…๋ฆฝ์ ์ธ ์ •๊ทœ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœ๋œ ํ‘œ๋ณธ์œผ๋กœ ๊ณ„์‚ฐ๋œ ํ‘œ๋ณธํ‰๊ท ๋“ค์€ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ •๊ทœ๋ถ„ํฌ ํŒŒํŠธ์—์„œ ํ•™์Šตํ•œ ๋Œ€๋กœ ๊ทธ๋“ค์˜ ์„ ํ˜•๊ฒฐํ•ฉ ( โ€• Y) ์—ญ์‹œ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์— ์ฐฉ์•ˆํ•˜์—ฌ ์ผํ‘œ๋ณธ ๊ฒ€์ •์—์„œ์™€ ๊ฐ™์ด ์ŠคํŠœ๋˜ํŠธ ์ •๋ฆฌ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ( โ€• Y) ( 1 ฮผ) p n + p n โˆผ ( 1 n โˆ’ ) ์—ฌ๊ธฐ์„œ 1 ฮผ๋Š” ๊ฐ ๋‘ ์ง‘๋‹จ์œผ ๋ชจํ‰๊ท ์ด๊ณ  ๊ทธ ์ฐจ์ด์ธ ( 1 ฮผ) ์ด ์šฐ๋ฆฌ์˜ ์ตœ์ข… ๋ชฉ์ ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‘ ์ง‘๋‹จ์˜ ์‚ฌ์ด์˜ ์œ ์˜ํ•œ ์ฐจ์ด๊ฐ€ ์—†๋‹ค๋ฉด 0์ด ๋  ๊ฒƒ์ด๊ณ  ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์ถฉ๋ถ„ํžˆ ์œ ์˜ํ•œ ์ฐจ์ด๋ฅผ ๋ณด์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์—ญ์‹œ ์ผํ‘œ๋ณธ ๊ฒ€์ •์—์„œ์˜ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰๊ณผ ๋น„๊ตํ•ด ๋ณด์‹œ๋ฉด ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ๋ ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์ผ ์ง‘๋‹จ์—์„œ์˜ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ ( ์ผ ๋‹จ ์„œ ๊ฒ€ ํ†ต ๋Ÿ‰ ) โ€• ฮผ / = โ€• ฮผ 2 โˆผ ( โˆ’ ) ์ด์ œ ์ด ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์„ ์ด์šฉํ•˜์—ฌ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒ€์ •์„ ์‹ค์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ง์”€๋“œ๋ฆฐ ๋Œ€๋กœ ์šฐ๋ฆฌ์˜ ์ตœ์ข… ๋ชฉ์ ์€ ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๊ฐ™์€์ง€ ๋‹ค๋ฅธ์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ท€๋ฌด๊ฐ€์„ค๊ณผ ๋Œ€๋ฆฝ๊ฐ€์„ค๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 0 ฮผ = 2 H : 1 ฮผ ๋งŒ์•ฝ ๊ท€๋ฌด๊ฐ€์„ค์ด ์‚ฌ์‹ค์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ์œ„ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์—์„œ ( 1 ฮผ) ์€ 0์ด ๋˜๋ฏ€๋กœ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ์ข€ ๋” ๊ฐ„์†Œํ™”๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๊ท€๋ฌด๊ฐ€์„ค ํ•˜์—์„œ์˜ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด๋‹ˆ 0 ๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 0 ( โ€• Y) p n + p n โˆผ ( 1 n โˆ’ ) ํŒ๋‹จ์€ ์›๋ฆฌ๋Š” ์ด 0 ๋ฅผ ์ด์šฉํ•˜์—ฌ, ์šฐ๋ฆฌ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ‘œ๋ณธ๋“ค๋กœ ๊ณ„์‚ฐ๋œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ด ๊ณผ์—ฐ (์ฃผ์–ด์ง„ ๋ถ„ํฌํ•˜์—์„œ) ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋‚˜์˜ฌ๋งŒํ•œ ๊ฐ’์ธ์ง€, ์•„๋‹ˆ๋ฉด ์šฐ์—ฐ์ด๋ผ๊ณ  ๋ณด๊ธฐ์—๋Š” ๋„ˆ๋ฌด ๊ทน๋‹จ์— ๊ฐ€๊นŒ์šด ๊ฐ’์ธ์ง€ ํ™•๋ฅ ๋กœ์จ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ ๋ฐฉ์‹์€ ์ผํ‘œ๋ณธ ๊ฒ€์ •๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ณ„์‚ฐ๋œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰๋ณด๋‹ค ๊ทน๋‹จ์ ์ธ ๊ฐ’์ด ๋‚˜์˜ฌ ํ™•๋ฅ (์œ ์˜ ํ™•๋ฅ )์ด ์‚ฌ์ „์— ์šฐ๋ฆฌ๊ฐ€ ์ •ํ•œ ์œ ์˜์ˆ˜์ค€ ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๊ธฐ๊ฐํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ๋Š” ์ผ๋ฐ˜์ ์ธ ๋“ฑ ๋ถ„์‚ฐ ๊ฒ€์ •์—์„œ์˜ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰๊ณผ ํ˜•ํƒœ๋Š” ๊ฐ™๊ฒŒ ํ•˜๋˜ ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์„ ๋”ฐ๋กœ ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ 1 ๊ณผ 2๋Š” ๊ฐ๊ฐ ์ง‘๋‹จ 1๊ณผ ์ง‘๋‹จ 2์˜ ํ‘œ๋ณธ๋ถ„์‚ฐ์ž…๋‹ˆ๋‹ค. ( โ€• Y) ( 1 ฮผ) 1 n + 2 n โˆผ ( e r e f r e o) ๊ทธ๋Ÿฐ๋ฐ ์ด ๊ฒฝ์šฐ ๋ถ„ํฌ์˜ ์ž์œ ๋„๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ๊ฐ€ ์• ๋งคํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ๋Š” ์„ธํ„ฐ์Šค์›จ์ดํŠธ ๊ณต์‹๊ณผ ๊ฐ™์€ ์ž์œ ๋„์˜ ๊ทผ์‚ฌ์น˜๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. R์„ ๋น„๋กฏํ•œ ํ†ต๊ณ„ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ์ž๋™์œผ๋กœ ์ž์œ ๋„ ๊ทผ์‚ฌ์น˜๋ฅผ ์–ป์–ด ๊ฒ€์ •์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. A5. t ๊ฒ€์ •(R Code) 5. ๊ฒ€์ •(R Code) ๋ฐ์ดํ„ฐ๋Š” ์•ž๋‹จ์—์„œ ๋‹ค๋ฃจ์—ˆ๋˜ HR ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด์ง ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ง์›๋“ค์˜ ์ง๋ฌด๋งŒ์กฑ๋„์— ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์ •์„ ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ์ด์ง ์—ฌ๋ถ€(left)๋Š” 0 : ์ด์ง ์•ˆ ํ•จ, 1 : ์ด์ง์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด์ง ์—ฌ๋ถ€๋Š” 2๊ฐœ์˜ ์ˆ˜์ค€์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ช…๋ชฉํ˜• ๋ณ€์ˆ˜์ด๊ณ  ์ง๋ฌด๋งŒ์กฑ๋„(satisfaction_level)๋Š” 0 ~ 1 ์‚ฌ์ด์— ์žˆ๋Š” ์—ฐ์†ํ˜• ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์—ฐ์†ํ˜• ๋ณ€์ˆ˜๋ฅผ ๋‘ ์ˆ˜์ค€์„ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š” ๋ช…๋ชฉํ˜• ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์ •ํ•˜๊ณ  ์‹ถ๊ธฐ์— T ๊ฒ€์ •์„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ ํ•ฉํ•œ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. T ๊ฒ€์ •์„ R์—์„œ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋“ฑ ๋ถ„์‚ฐ ๊ฒ€์ • ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ๋‘ ์žก๋‹จ์˜ ๋ถ„์‚ฐ์ด ๊ฐ™์€์ง€ ๊ฒ€์ •ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์ด ๋™์ผํ•˜๋‹ค ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์ด ๋‹ค๋ฅด๋‹ค 0 ๋‘ ๋‹จ ๋ถ„ ์ด ์ผ ๋‹ค H : ์ง‘์˜ ์‚ฐ ๋‹ค ๋‹ค ์—ฌ๊ธฐ์„œ ๋‘ ์ง‘๋‹จ์€ left ๋ณ€์ˆ˜์˜ ์ˆ˜์ค€์ธ 0(์ด์ง ์•ˆ ํ•จ)๊ณผ 1(์ด์ง)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. # ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ library(car) # ๋“ฑ ๋ถ„์‚ฐ ๊ฒ€์ • ์‹คํ–‰ HR = read.csv('F:/Drop box/DATA SET/HR_comma_sep.csv') HR$left = as.factor(HR$left) leveneTest(satisfaction_level ~ left , data = HR) Levene's Test for Homogeneity of Variance (center = median) Df F value Pr(>F) group 1 122.4 < 2.2e-16 *** 14997 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Levene's ๋“ฑ ๋ถ„์‚ฐ ๊ฒ€์ •์€ ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ๋น„๋ฅผ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— F ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. R ์‹คํ–‰ ๊ฒฐ๊ด๊ฐ’์— ๋‚˜์˜ค๋Š” v l e 122.4 ๋Š” ์œ„ ๋ถ„ํฌ์—์„œ์˜ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ž…๋‹ˆ๋‹ค. ์•ž์— ๊ฐ€์„ค๊ฒ€์ • ๋‹จ๊ณ„์—์„œ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์„ ๊ธฐ์ค€์œผ๋กœ ๊ณ„์‚ฐ๋œ ์œ ์˜ ํ™•๋ฅ ์„ ํ†ตํ•ด ๊ท€๋ฌด๊ฐ€์„ค ๊ธฐ๊ฐ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ์œ ์˜ ํ™•๋ฅ ์„ ๋‚˜ํƒ€๋‚ด๋Š” r ( F ) < 2.2 โˆ’ 16 ์œผ๋กœ ํ‘œ์‹œ๊ฐ€ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. R์—์„œ โˆ’ 16 1 10 16 ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์œ ์˜ ํ™•๋ฅ ์€ 0์— ๋งค์šฐ ๊ทผ์ ‘ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์„ ๋œปํ•˜๋ฉฐ ์œ ์˜์ˆ˜์ค€ = 0.05 ๋ณด๋‹ค ํ›จ์”ฌ ์ž‘๊ธฐ์— ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์ด ๋™์ผํ•˜๋‹ค.๋ผ๋Š” ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ด์ง์„ ํ•œ ์ง์›๋“ค๊ณผ ์ด์ง์„ ํ•˜์ง€ ์•Š์€ ์ง์›๋“ค ๊ฐ„์˜ ์ง๋ฌด๋งŒ์กฑ๋„์˜ ๋ถ„์‚ฐ์€ ๋™์ผํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒ€์ • ์•ž์„œ ๋“ฑ ๋ถ„์‚ฐ ๊ฒ€์ •์˜ ๊ฒฐ๊ณผ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋‚˜์™”๋Š”๊ฐ€์— ๋”ฐ๋ผ์„œ t ๊ฒ€์ •์˜ ์˜ต์…˜์ด ๋ณ€ํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ •์˜ ๊ท€๋ฌด๊ฐ€์„ค๊ณผ ๋Œ€๋ฆฝ๊ฐ€์„ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๊ฐ™๋‹ค ๋‘ ์ง‘๋‹จ์˜ ํ‰๊ท  ์ด ๋‹ค๋ฅด๋‹ค 0 ๋‘ ๋‹จ ํ‰ ์ด ๋‹ค H : ์ง‘์˜ ๊ท  ๋‹ค ๋‹ค # ๋“ฑ ๋ถ„์‚ฐ์ด ๋™์ผํ•  ๊ฒฝ์šฐ t.test(satisfaction_level ~ left , data = HR, var.equal = TRUE) Two Sample t-test data: satisfaction_level by left t = 51.613, df = 14997, p-value < 2.2e-16 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: 0.2181017 0.2353215 sample estimates: mean in group 0 mean in group 1 0.6668096 0.4400980 # ๋“ฑ ๋ถ„์‚ฐ์ด ๋™์ผํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ t.test(satisfaction_level ~ left , data = HR, var.equal = FALSE) Welch Two Sample t-test data: satisfaction_level by left t = 46.636, df = 5167, p-value < 2.2e-16 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: 0.2171815 0.2362417 sample estimates: mean in group 0 mean in group 1 0.6668096 0.4400980 ํ˜„์žฌ ๋ฐ์ดํ„ฐ๋Š” ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์ด ๋™์ผํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ t.test์—์„œ ์˜ต์…˜์„ var.equal = FALSE๋กœ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. ๋ถ„์„ ๊ฒฐ๊ณผ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ v l e 46.636 ์ด๋ฉฐ ์œ ์˜ ํ™•๋ฅ ( โˆ’ a u)์€ 2.2 โˆ’ 16 ์œผ๋กœ 0๊ณผ ๋งค์šฐ ๊ฐ€๊น์Šต๋‹ˆ๋‹ค. ์ด๋กœ์จ ๋‘ ์ง‘๋‹จ(์ด์ง ์—ฌ๋ถ€)์˜ ํ‰๊ท ์ด ๊ฐ™๋‹ค๋Š” ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด์ง ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ง๋ฌด๋งŒ์กฑ๋„์˜ ํ‰๊ท  ์ฐจ์ด๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. R ๊ฒฐ๊ณผํ‘œ์— ๋”ฐ๋ฅด๋ฉด ์ด์ง์„ ํ•˜์ง€ ์•Š์€ ์ง‘๋‹จ์˜ ํ‰๊ท (mean in group 0)์€ 0.66์ด๊ณ  ์ด์ง์„ ํ•œ ์ง‘๋‹จ์˜ ํ‰๊ท (mean in group 1)์€ 0.44๋กœ ์ด์ง์„ ํ•˜์ง€ ์•Š์€ ์ง‘๋‹จ์˜ ์ง๋ฌด๋งŒ์กฑ๋„๊ฐ€ ๋” ๋†’๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A6. ๋ถ„์‚ฐ๋ถ„์„ 6. ๋ถ„์‚ฐ๋ถ„์„ ๋ถ„์‚ฐ๋ถ„์„์€ ๊ฒ€์ •๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฐจ์ด๋ฅผ ๋ณด๋Š” ๋ถ„์„์ž…๋‹ˆ๋‹ค. ๊ฒ€์ •๊ณผ ๋‹ค๋ฅธ ์ ์€ ๋ถ„์‚ฐ๋ถ„์„์€ ๋‘ ์ง‘๋‹จ์€ ๋ฌผ๋ก ์ด๊ณ  ์„ธ ์ง‘๋‹จ ์ด์ƒ์—์„œ๋„ ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋‘ ์ง‘๋‹จ์—์„œ ์ง„ํ–‰ํ•˜๋ฉด ๊ฒ€์ •๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์ค๋‹ˆ๋‹ค. ANOVA(Analysis of Variance)์˜ ์˜๋ฏธ๋ฅผ ์กฐ๊ธˆ ํ’€์–ด์„œ ํ•ด์„ํ•ด ๋ณด์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ถ„์‚ฐ(๋ณ€๋™)์„ ๋ถ„์„ํ•˜์—ฌ ํ‰๊ท ์„ ๋น„๊ตํ•œ๋‹ค. ์ •๋„๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต ์ฐจ์ด๋ฅผ ๋น„๊ตํ•  ๋•Œ, 3๊ฐœ ์ด์ƒ์˜ ๊ฐœ์ฒด์— ๋Œ€ํ•œ ๋™์‹œ ๋น„๊ต๋Š” ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ๊ณ  ์‹ถ์€ ์ œํ’ˆ์ด 3๊ฐœ๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ทธ์ค‘ 1๊ฐœ๋งŒ ์‚ด ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด ๋ณธ๋Šฅ์ ์œผ๋กœ 3๊ฐœ ์ค‘์— 2๊ฐœ๋ฅผ ๋จผ์ € ๋น„๊ตํ•˜๊ณ , ๊ทธ์ค‘ ์„ ํƒ๋œ 1๊ฐœ๊ฐ€ ๋‚˜๋จธ์ง€ 1๊ฐœ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ตฌ๋งคํ•  ์ œํ’ˆ์„ ์„ ํƒํ•˜๋Š” ๊ณผ์ •๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„์€ ์ด๋Ÿฌํ•œ ํ•œ๊ณ„์ ์„ ๊ทน๋ณตํ•˜๊ณ ์ž ์ง์ ‘์ ์œผ๋กœ ํ‰๊ท ์„ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ๋ถ„์‚ฐ์„ ์ด์šฉํ•˜์—ฌ ํ‰๊ท ์„ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ช…์นญ๋„ '๋ถ„์‚ฐ์„ ๋ถ„์„ํ•œ๋‹ค'์˜ ์˜๋ฏธ๋กœ Analysis of Variance๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด HR ๋ฐ์ดํ„ฐ ์…‹์—์„œ satisfaction_level(์ง๋ฌด ๋งŒ์กฑ๋„) ๋ณ€์ˆ˜๋ฅผ salary(์—ฐ๋ด‰ ์ˆ˜์ค€, Low, Mid, High 3๊ฐœ์˜ ์ˆ˜์ค€์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ˆœ์„œํ˜• ๋ณ€์ˆ˜) ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ํ‰๊ท  ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ†ต๊ณ„์ ์œผ๋กœ ๊ฒ€์ •ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์—ฐ์†ํ˜• ๋ณ€์ˆ˜๋ฅผ 3 ์ง‘๋‹จ ๊ฐ„์˜ ํ‰๊ท  ์ฐจ์ด๋ฅผ ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด๋‹ˆ, ๊ฒ€์ •์ด ์•„๋‹Œ ๋ถ„์‚ฐ๋ถ„์„์„ ์ ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„์˜ ์•„์ด๋””์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์ „์ฒด ์ง‘๋‹จ์˜ ํ‰๊ท ( โ€• โ‹… ) , ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์ง‘๋‹จ์˜ ํ‰๊ท ( โ€• โ‹… )์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ ์ง‘๋‹จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.(salary ๋ณ€์ˆ˜์˜ low, mid, high) ์ž„์˜์˜ ๋ฐ์ดํ„ฐ( i)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ด ๋ณ€๋™์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ณ€๋™ : โ€• โ‹… X j ๊ณ„์‚ฐ๋œ ์ด ๋ณ€๋™์„ ์ง‘๋‹จ ๊ฐ„(between) ๋ณ€๋™๊ณผ ์ง‘๋‹จ ๋‚ด(within) ๋ณ€๋™์œผ๋กœ ๋ถ„๋ฆฌ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ์ง‘๋‹จ ๊ฐ„(between) ๋ณ€๋™ : โ€• โ‹… X i ์ง‘๋‹จ ๋‚ด(within) ๋ณ€๋™ : โ€• โ‹… X j ์ด ๋ณ€๋™์„ ์ง‘๋‹จ ๊ฐ„ ๋ฐ ์ง‘๋‹จ ๋‚ด๋กœ ๋ถ„๋ฆฌํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์ด ๋ณ€๋™ ์ค‘์— ์ง‘๋‹จ ๊ฐ„(between) ๋ณ€๋™์ด ์ง‘๋‹จ ๋‚ด(within) ๋ณ€๋™์— ๋น„ํ•ด ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๋ถ€๋ถ„์„ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ๋Š”๊ฐ€๋ฅผ ๋ณด๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ์ž„์˜์˜ ๋ฐ์ดํ„ฐ( i)์— ๋Œ€ํ•ด์„œ ๋ณ€๋™์˜ ์ œ๊ณฑํ•ฉ์„ ๊ตฌํ•ด ์ค๋‹ˆ๋‹ค. ์ œ๊ณฑํ•ฉ์„ ๊ตฌํ•ด์ฃผ๋Š” ์ด์œ ๋Š” ์ž„์˜์˜ ๋ฐ์ดํ„ฐ( i) ์œ„์น˜์— ๋”ฐ๋ผ์„œ ์ด ๋ณ€๋™์˜ ๊ฐ’์ด ์–‘์ˆ˜(+)๊ฐ€ ๊ณ„์‚ฐ๋  ์ˆ˜๊ฐ€ ์žˆ๊ณ  ํ˜น์€ ์Œ์ˆ˜(-)๊ฐ€ ๊ณ„์‚ฐ๋  ์ˆ˜๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ถ€ํ˜ธ ๊ฐ„ ๋ง์…ˆ์„ ํ•˜๋ฉด ๋ณ€๋™์˜ ํ•ฉ์ด ์ƒ์‡„๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ œ๊ณฑํ•ฉ์„ ํ•จ์œผ๋กœ์จ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด ์ฃผ๊ณ ์ž ํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ณ„์‚ฐ๋œ ์ œ๊ณฑํ•ฉ์„ ํ‰๊ท  ์ œ๊ณฑํ•ฉ์œผ๋กœ ๋ณด์ •์„ ํ•ด์ค€ ๋‹ค์Œ, F ํ†ต๊ณ„๋Ÿ‰์„ ๊ตฌํ•ด ์œ ์˜ ํ™•๋ฅ (p-value)์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์ง‘๋‹จ ๊ฐ„ ๋ณ€๋™์ด ์ง‘๋‹จ ๋‚ด ๋ณ€๋™์— ๋น„ํ•ด ์œ ์˜ํ•œ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด, ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ, ๋ถ„์‚ฐ๋ถ„์„์˜ ์›๋ฆฌ์™€ ๊ด€๋ จ๋œ ๋ช‡ ๊ฐ€์ง€ ํ‚ค ์•„์ด๋””์–ด๋ฅผ ๋ง์”€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์ „์ฒด ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ์ด์œ ๋Š” 3์ง‘๋‹จ ์ด์ƒ์˜ ๋ณ€๋™์„ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์ค€์„ ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„์˜ ํ•ต์‹ฌ์€ ์ด ๋ณ€๋™ ์ค‘์—์„œ ์ง‘๋‹จ ๊ฐ„(between) ๋ณ€๋™์ด ์ง‘๋‹จ ๋‚ด(within) ๋ณ€๋™์— ๋น„ํ•ด ์–ผ๋งˆ๋‚˜ ํฐ์ง€ ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ง‘๋‹จ ๊ฐ„ ๋ณ€๋™์— ์ง‘์ค‘ํ•˜๋Š” ์ด์œ ๋Š” ๋ถ„์‚ฐ๋ถ„์„์—์„œ ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด ์ „์ฒด ์ง‘๋‹จ์˜ ํ‰๊ท ๊ณผ ํŠน์ • ์ง‘๋‹จ์˜ ํ‰๊ท ์˜ ์ฐจ์ด ์ฆ‰, ๋‹ค๋ฅธ ์ง‘๋‹จ ๊ฐ„์˜ ํ‰๊ท  ์ฐจ์ด์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ์ง‘๋‹จ ๋‚ด(within) ๋ณ€๋™์€ ๋™์ผ ์ง‘๋‹จ ๋‚ด ์ฐจ์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ์ง‘๋‹จ ๊ฐ„ ํ‰๊ท  ์ฐจ์ด๋ฅผ ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ๋ถ„์‚ฐ๋ถ„์„์—์„œ๋Š” Error ์ทจ๊ธ‰์„ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ง‘๋‹จ ๋‚ด ์ฐจ์ด๊ฐ€ ํฌ๋ฉด ์ด ๋ณ€๋™ ๋‚ด์—์„œ ์ง‘๋‹จ ๊ฐ„ ์ฐจ์ด๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ๋ถ€๋ถ„์ด ์ค„์–ด๋“ค๊ธฐ ๋•Œ๋ฌธ์—, ์ด ๋ณ€๋™์ด ๊ณ ์ •๋œ ์ƒํƒœ์—์„œ Error๊ฐ€ ์ž‘๋‹ค๋Š” ๊ฒƒ์€ ๊ณง ์ง‘๋‹จ ๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ํฌ๋‹ค๋Š” ๊ฒƒ์œผ๋กœ ์˜๋ฏธ๊ฐ€ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ด์ œ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์„ธ ๊ฐ€์ง€ ๋ณ€๋™์„ ๊ด€์ฐฐ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ์ถ”์ •ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋ณ€๋™์„ ํ‘œํ˜„ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” SS๋Š” ์ œ๊ณฑํ•ฉ์ด๋ž€ ์˜๋ฏธ๋กœ Sum of Squares๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ณ€๋™ ์ง‘๋‹จ์˜ ์ฒจ์ž ํ‘œ๋ณธ์˜ ์ฒจ์ž ๋ณ€ : S = i 1 โˆ‘ = n ( i โˆ’ โ€• โ‹… ) i ์ง‘์˜ ์ž = ๋ณธ ์ฒจ S T ๋Š” ๋ณด์‹œ๋Š” ๋ฐ”์™€ ๊ฐ™์ด ์–ด๋–ค ๊ทธ๋ฃน์ด๋“  ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š๊ณ  ์ „๋ถ€ ํŽธ์ฐจ์˜ ์ œ๊ณฑํ•ฉ์„ ์‹œ์ผœ ๋”ํ•ฉ๋‹ˆ๋‹ค. โ€• โ‹… ์—ญ์‹œ ๊ทธ๋ฃน๊ณผ ์ƒ๊ด€์—†์ด ๋ชจ๋“  ํ‘œ๋ณธ๋“ค์˜ ์ „์ฒด ํ‰๊ท ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์œ„์˜ ์˜ˆ์ฒ˜๋Ÿผ ์ง‘๋‹จ ๋ณ€์ˆ˜(salary ๋ณ€์ˆ˜)๊ฐ€ low, mid, high ์„ธ ๊ทธ๋ฃน์ด๋ผ๋ฉด ๋Š” 3์ด ๋˜๊ฒ ๊ณ  i ๋“ค์€ low, mid, high ๊ฐ ์ง‘๋‹จ์—์„œ์˜ ํ‘œ๋ณธ ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ฐ ์ง‘๋‹จ์—์„œ์˜ ํ‘œ๋ณธ ์ˆ˜๋Š” ๋‹ฌ๋ผ๋„ ์ƒ๊ด€์€ ์—†์œผ๋‚˜ ๋˜๋„๋ก ๋น„์Šทํ•œ ๊ฒƒ์ด ๋ถ„์„์˜ ์‹ ๋ขฐ๋ฅผ ๋†’์—ฌ์ค๋‹ˆ๋‹ค. ์ง‘๋‹จ ๊ฐ„ ๋ณ€๋™ ๋‹จ ( e w e) ๋™ S G โˆ‘ = k j 1 i ( โ€• โ‹… X โ‹… ) = i 1 n ( โ€• โ‹… ์ง‘๋‹จ ๊ฐ„ ์ œ๊ณฑํ•ฉ์€ ์ฒ˜๋ฆฌ์ œ๊ณฑํ•ฉ์ด๋ผ๊ณ ๋„ ๋งŽ์ด ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ i ์€ ๊ทธ๋ฃน์˜ ํ‰๊ท ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด โ€• โ‹… ์€ salary ๋ณ€์ˆ˜์—์„œ mid์— ์†ํ•˜๋Š” ํ‘œ๋ณธ๋งŒ์˜ ํ‰๊ท ์ด๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ „์ฒด ํ‰๊ท ๊ณผ ๊ทธ๋ฃน ํ‰๊ท ์˜ ์ฐจ์ด์— ๋Œ€ํ•œ ์ œ๊ณฑํ•ฉ์ด์ฃ . ์ด๊ฒƒ์ด ๊ทธ๋ฃน์˜ ํšจ๊ณผ์ž…๋‹ˆ๋‹ค. S ๊ฐ€ ๊ทธ๋ฃน๊ณผ ์ƒ๊ด€์—†๋Š” ์ „์ฒด ๋ณ€๋™์ด๋ผ๋ฉด ์ด๋Š” ๊ทธ๋ฃน๋“ค์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ณ€๋™์ธ ๊ฒƒ์ด์ฃ . ์ง‘๋‹จ ๋‚ด ๋ณ€๋™ ๋‹จ ( i h n ) ๋™ S E โˆ‘ = k j 1 i ( i โˆ’ โ€• โ‹… ) ๋งˆ์ง€๋ง‰์œผ๋กœ S๋Š” ๊ทธ๋ฃน ๋‚ด ๋ณ€๋™์„ ํ‘œํ˜„ํ•˜๋Š” ์ œ๊ณฑํ•ฉ์ž…๋‹ˆ๋‹ค. ํ˜•ํƒœ๋ฅผ ๋ณด์‹œ๋ฉด ๊ฐœ๋ณ„ ๊ด€์ฐฐ ๊ฐ’๊ณผ ๊ทธ ๊ด€์ธก ๊ฐ’์ด ์†ํ•œ ๊ทธ๋ฃน์˜ ๊ทธ๋ฃน ํ‰๊ท ์˜ ์ฐจ์ด๋ฅผ ์ด์šฉํ•ด ์ œ๊ณฑํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ์•ž์—์„œ ์ง‘๋‹จ ๋‚ด ๋ณ€๋™์€ ๋ณด๊ณ ์ž ํ•˜๋Š” ์ง‘๋‹จ ๊ฐ„์˜ ์ฐจ์ด์™€๋Š” ๋ฐ˜๋Œ€๋˜๋Š” ์„ฑํ–ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Error๋ผ๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด S ๊ฐ€ S์— ๋น„ํ•ด ์ƒ๋Œ€์ ์œผ๋กœ ์ž‘๋‹ค๋ฉด ์šฐ๋ฆฌ๋Š” ๊ทธ๋ฃน์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํšจ๊ณผ๊ฐ€ ์œ ์˜ํ•˜๋‹ค๊ณ  ํŒ๋‹จํ•˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค. i 1 โˆ‘ = n ( i โˆ’ โ€• โ‹… ) = i 1 n ( โ€• โ‹… X โ‹… ) + i 1 โˆ‘ = n ( i โˆ’ โ€• S = S + S ๋งŒ์•ฝ ๊ธฐ๋ณธ์ ์ธ ๊ฐ€์ •๋“ค์ด ์„ฑ๋ฆฝํ•œ๋‹ค๋ฉด ์ˆ˜๋ฆฌ์ ์œผ๋กœ ์ด ๋‘ ์ œ๊ณฑํ•ฉ( S, S)์€ ์ง๊ต ๋ถ„ํ•ด(orthogonal decomposition) ๋˜์—ˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง๊ต ๋ถ„ํ•ด๋Š” ๊ต์ฐจํ•ญ์ด ์—†์ด ๊ฐ ๋ณ€๋™์„ ํ‘œํ˜„ํ•˜๋Š” ์ œ๊ณฑํ•ฉ ํ•ญ๋งŒ์œผ๋กœ ๋ถ„ํ•ด๋˜๋Š” ๊ฒƒ์ด๋ฉฐ ๊ทธ ์˜๋ฏธ๋Š” ๋‘ ์ œ๊ณฑํ•ฉ์ด ํ†ต๊ณ„์ ์œผ๋กœ ๋…๋ฆฝ์ด๋ผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ณ€๋™์˜ ์ถ”์ •์น˜์ธ ์ œ๊ณฑํ•ฉ ์—ญ์‹œ ํ‘œ๋ณธ์œผ๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ํ†ต๊ณ„๋Ÿ‰์ด๋ฏ€๋กœ ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์šฐ๋ฆฌ๋Š” ์„œ๋กœ ๋…๋ฆฝ์ ์ธ ์ง‘๋‹จ ๊ฐ„ ์ œ๊ณฑํ•ฉ๊ณผ ์ง‘๋‹จ ๋‚ด ์ œ๊ณฑํ•ฉ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘˜์„ ์กฐํ•ฉํ•˜์—ฌ ์ƒ๋Œ€์  ๋น„๊ต๊ฐ€ ๊ฐ€๋Šฅํ•œ F ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋Š” ์ƒˆ๋กœ์šด ํ†ต๊ณ„๋Ÿ‰์„ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ฃผ์–ด์ง„ ํ‘œ๋ณธ๋“ค์„ ์ด์šฉํ•ด ์ด ํ†ต๊ณ„๋Ÿ‰์„ ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ํ”ํžˆ F ๊ฐ’์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค) ๋ถ„์‚ฐ๋ถ„์„์—์„œ์˜ ๊ฐ€์„ค๊ฒ€์ • ์ ˆ์ฐจ ์˜์ž ์œ ๋„ ์˜์ž ์œ ๋„ ์˜ ์ž์œ ๋„์˜ ์ž์œ ๋„ S / ( S์˜ ์œ  ) S / ( S์˜ ์œ  ) F ( S์˜ ์œ , S์˜ ์œ  ) S ์™€ S์˜ ์ž์œ ๋„ ์—ญ์‹œ ์˜ˆ์ „์— ํ–ˆ๋˜ ์ž์œ ๋„์˜ ๋…ผ๋ฆฌ์™€ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. S๋Š” K ๊ฐœ์˜ ๊ทธ๋ฃน์˜ ํ‰๊ท ( โ€• โ‹… )์ด ์ œ๊ณฑํ•ฉ์„ ๋งŒ๋“œ๋Š”๋ฐ ์„ ํƒ๋  ์ˆ˜ ์žˆ๊ณ , ๋งŒ๋“œ๋Š” ๊ณผ์ •์—์„œ ์ „์ฒด ํ‰๊ท ( โ€• โ‹… ) ํ•˜๋‚˜๋ฅผ ์ถ”์ •ํ•ด์„œ ์‚ฌ์šฉํ•˜์˜€์œผ๋‹ˆ 1๊ฐœ์˜ ์ž์œ ๋„๋ฅผ ์žƒ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ S์˜ ์ž์œ ๋„๋Š” โˆ’ ์ด ๋ฉ๋‹ˆ๋‹ค. S์˜ ๊ฒฝ์šฐ, ์ „์ฒด ํ‘œ๋ณธ ์ˆ˜๊ฐ€ (์—ฌ๊ธฐ์„œ๋Š” N์ด๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค) ์ œ๊ณฑํ•ฉ์„ ๋งŒ๋“œ๋Š”๋ฐ ์„ ํƒ๋  ์ˆ˜ ์žˆ๊ณ  ๋งŒ๋“œ๋Š” ๊ณผ์ •์—์„œ ๊ฐœ์˜ ๊ทธ๋ฃน ํ‰๊ท ( โ€• โ‹… ) ์„ ์ถ”์ •ํ•ด์„œ ์‚ฌ์šฉํ•˜์˜€์œผ๋‹ˆ k ๊ฐœ์˜ ์ž์œ ๋„๋ฅผ ์žƒ์Šต๋‹ˆ๋‹ค. ์ฆ‰, S์˜ ์ž์œ ๋„๋Š” โˆ’์ž…๋‹ˆ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„์˜ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์„ ๊ฐ€์ง€๊ณ  ์œ ์˜ ํ™•๋ฅ (p-value)์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. = S / f S E d E F ( f, f) = u b r f r u, i n m e o s m l s o e f = โˆ’ d E N k ์ด๋ ‡๊ฒŒ ์ง„ํ–‰๋œ ๋ถ„์‚ฐ๋ถ„์„์˜ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ฆฌํ•œ ๊ฒƒ์„ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ(Anova Table)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„์˜ ๊ฒฐ๊ณผ ํ•ด์„์€ ํ•ญ์ƒ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด์„ํ•˜๋‹ˆ ๊ผญ ๊ธฐ์–ตํ•ด๋‘์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค i = i ฯต j ฯต i d ( , 2 ) Y j ฮผ ฮฑ + i, i โˆผ i N ( , 2 ) ๊ฐ ์ง‘๋‹จ = , , , ( ์ง‘ ) ๊ฐ ์ง‘๋‹จ์—์„œ์˜ ๊ฐœ๋ณ„๊ด€์ฐฐ์น˜ = , , , i ( ์ง‘ ์—์˜ ๋ณ„ ์ฐฐ ) ๊ฐ ์ง‘๋‹จ์˜ ํšจ๊ณผ i ฮผ โˆ’ ( ์ง‘์˜ ๊ณผ ) ์—ฌ๊ธฐ์„œ ๊ฐ ์ง‘๋‹จ์˜ ํšจ๊ณผ i ๋Š” ๊ฐ ์ง‘๋‹จ์˜ ํ‰๊ท ๊ณผ ์ „์ฒด ํ‰๊ท ์˜ ์ฐจ์ด์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์œ„์—์„œ ๋ณ€๋™์„ ์ด์•ผ๊ธฐํ•˜๋ฉด์„œ ์‚ฌ์šฉํ–ˆ๋˜ โ€• โ‹… X โ‹… ์ด ์ถ”์ •ํ•˜๋ ค๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ์ด ๊ฐ ์ง‘๋‹จ์˜ ํšจ๊ณผ i ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ i ๋ž€ independent and identically distributed์˜ ์ค„์ž„๋ง๋กœ, ์–ด๋–ค ํ™•๋ฅ ๋ณ€์ˆ˜๊ฐ€ ๋…๋ฆฝ์ ์ด๋ฉฐ ๋™์ผํ•œ ๋ถ„ํฌ๋ฅผ ๋ณด์ธ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์œ„ ๋ชจํ˜•์—์„œ๋Š” ์˜ค์ฐจํ•ญ ์ด ๋…๋ฆฝ์ ์œผ๋กœ ํ‰๊ท ์ด 0์ด๊ณ  ๋ถ„์‚ฐ์ด 2 ์ธ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅธ๋‹ค๋Š” ๊ฒƒ์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค์ด ๊ธฐ๊ฐ์ด ๋˜์—ˆ์„ ๋•Œ, ์ฆ‰ ํ‰๊ท ์ด ๊ฐ™์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ถ„์‚ฐ๋ถ„์„์—์„œ ๋Œ€๋ฆฝ๊ฐ€์„ค์˜ ๋œป์€ '๋ชจ๋“  ์ง‘๋‹จ๋ณ„ ํ‰๊ท ์€ ๊ฐ™์ง€ ์•Š๋‹ค' ๊ฐ€ ์•„๋‹Œ '์ ์–ด๋„ ํ•˜๋‚˜์˜ ์ง‘๋‹จ ํ‰๊ท ์€ ๋‹ค๋ฅด๋‹ค'์ด๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค. โˆ’ ๊ฐœ์˜ ์ง‘๋‹จ๋ณ„ ํ‰๊ท ์ด ๊ฐ™๋‹ค๊ณ  ํ•ด๋„ ํ•˜๋‚˜๋งŒ ์œ ์˜ํ•˜๊ฒŒ ๋‹ค๋ฅด๋‹ค๋ฉด ๊ท€๋ฌด๊ฐ€์„ค์€ ๊ธฐ๊ฐ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋ถ„์‚ฐ๋ถ„์„์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ตฌ์กฐ์  ํ•œ๊ณ„์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์–ธ๊ธ‰ํ–ˆ๋˜ ๊ฒ€์ • ์›๋ฆฌ๋ฅผ ๋ณด์•„๋„ ์•„๋…ธ๋ฐ”๋Š” ์ง‘๋‹จ ๊ฐ„ ๋ณ€๋™์˜ ์ดํ•ฉ์„ ์ด์šฉํ•œ ๊ฒ€์ •์„ ํ•˜๊ณ , ๊ทธ ๋ง์€ ํŠน์ • ์ง‘๋‹จ์˜ ๊ฐœ๋ณ„ ํšจ๊ณผ๋ฅผ ์ „ํ˜€ ํ™•์ธํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด์„œ ๊ท€๋ฌด๊ฐ€์„ค์ด ๊ธฐ๊ฐ๋˜์—ˆ์„ ๋•Œ, ๊ฒ€์ •์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๊ฐ ์ง‘๋‹จ๋ณ„ ํ‰๊ท ์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์€ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ ์–ด๋–ค ์ง‘๋‹จ์ด ์–ผ๋งˆํผ ํฐ์ง€๋Š” ์•Œ ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด์ฃ . ๊ทธ๋ž˜์„œ ์ด ๊ฒฝ์šฐ ๋Œ€๊ฒŒ ์‚ฌํ›„ ๊ฒ€์ •(post hoc test)๋ผ ๋ถ€๋ฅด๋Š” ์ง‘๋‹จ๋ณ„ ๋น„๊ต ๊ฒ€์ •์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์›๋ฆฌ๋Š” ์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ low, mid, high ์„ธ ์ง‘๋‹จ์˜ ํšจ๊ณผ๊ฐ€ ๊ฐ™์€์ง€์— ๋Œ€ํ•œ ๋ถ„์‚ฐ๋ถ„์„ ๊ฒฐ๊ณผ ๊ท€๋ฌด๊ฐ€์„ค์ด ๊ธฐ๊ฐ๋˜์–ด ์„ธ ์ง‘๋‹จ์˜ ํšจ๊ณผ๊ฐ€ ๋ชจ๋‘ ๊ฐ™์ง€๋Š” ์•Š๋‹ค๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์—ˆ๋‹ค๋ฉด ์‚ฌํ›„ ๊ฒ€์ •์œผ๋กœ (low-medium), (low-high), (medium-high) ์ด๋ ‡๊ฒŒ ๋‘ ๊ฐœ์”ฉ ๋น„๊ตํ•˜๋ฉด์„œ ๊ฐœ๋ณ„ ๋น„๊ต๋ฅผ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ ๋ถ„์„ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์œ„์—์„œ ๋ฐฐ์šด ๋…๋ฆฝ ๊ฒ€์ •์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๊ฒ ๊ณ  ๊ฐ™์€ ์›๋ฆฌ๋ฅผ<NAME>๋Š” Duncan์ด๋‚˜ Scheffe๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ฐœ๋ณ„ ๋น„๊ต๋ฒ•์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. A7. ๋ถ„์‚ฐ๋ถ„์„(R Code) 7. ๋ถ„์‚ฐ๋ถ„์„(R Code) ๋ถ„์‚ฐ๋ถ„์„์„ R์—์„œ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์€ satisfaction_level(์ง๋ฌด ๋งŒ์กฑ๋„)์˜ ํ‰๊ท ์ด salary(์—ฐ๋ด‰ ์ˆ˜์ค€, low, medium, high) ์ง‘๋‹จ์— ๋”ฐ๋ผ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ†ต๊ณ„์ ์œผ๋กœ ๊ฒ€์ •ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฐ๋ด‰ ์ˆ˜์ค€๋ณ„๋กœ ์ง๋ฌด๋งŒ์กฑ๋„์˜ ํ‰๊ท ์ด ๊ฐ™์„ ๊ฒƒ์ด๋‹ค 0 s l r ( ๋ด‰ ) ์ค€๋กœ a i f c i n e e ( ๋ฌด ์กฑ ) ํ‰ ์ด ์„ ์ด. 1 n t 0 ANOVA = aov(satisfaction_level ~ salary, data = HR) summary(ANOVA) Df Sum Sq Mean Sq F value Pr(>F) salary 2 2.3 1.1693 18.96 5.97e-09 *** Residuals 14996 924.8 0.0617 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์‹œ๋ฉด ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. Df๋Š” ์ž์œ ๋„, Sum sq๋Š” ์ œ๊ณฑํ•ฉ์„ Mean Sq๋Š” ํ‰๊ท  ์ œ๊ณฑํ•ฉ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ์–ธ๊ธ‰ํ–ˆ๋˜ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์™€์„œ ๊ฐ’์„ ์ฑ„์›Œ ๋„ฃ์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ •ํ•ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ์ œ๊ณฑํ•ฉ๊ณผ ์ž์œ ๋„๋ฅผ ๊ตฌํ•œ ํ›„ ํ‰๊ท ์ œ๊ณฑ์„ ๊ณ„์‚ฐํ•œ ๋’ค ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์ธ F value๋ฅผ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์œ ์˜ ํ™•๋ฅ  p-value๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. p-value๋Š” 0๊ณผ ๋งค์šฐ ๊ฐ€๊นŒ์šด ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹น์—ฐํžˆ ์œ ์˜ ์ˆ˜์ค€ 0.05๋ณด๋‹ค ์ž‘์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ท€๋ฌด๊ฐ€์„ค 0 ๋ฅผ ๊ธฐ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰ ์—ฐ๋ด‰ ์ˆ˜์ค€๋ณ„๋กœ ์ง๋ฌด๋งŒ์กฑ๋„์˜ ํ‰๊ท ์€ ๋™์ผํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ์ƒํƒœ๋กœ๋Š” ์œ„์—์„œ ๋งํ–ˆ๋“ฏ์ด 'ํ‰๊ท ์ด ๊ฐ™์ง€ ์•Š๋‹ค.'๋งŒ์„ ์•Œ ์ˆ˜ ์žˆ์ง€, ์ •ํ™•ํžˆ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌํ›„ ๊ฒ€์ •์„ ์ง„ํ–‰ํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. TUKEY = TukeyHSD(ANOVA) TUKEY Tukey multiple comparisons of means 95% family-wise confidence level Fit: aov(formula = satisfaction_level ~ salary, data = HR) $salary diff lwr upr p adj low-high -0.03671654 -0.05461098 -0.018822102 0.0000046 medium-high -0.01565305 -0.03372130 0.002415191 0.1049520 medium-low 0.02106349 0.01111999 0.031006988 0.0000021 plot(TUKEY) ์‚ฌํ›„ ๊ฒ€์ •์€ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•์€ Tukey ์‚ฌํ›„ ๊ฒ€์ •์ž…๋‹ˆ๋‹ค. ํ•ด์„์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 3๊ฐœ์˜ ์ง‘๋‹จ์„ 2๊ฐœ์”ฉ ๋”ฐ๋กœ ๋ถ„์„์„ ํ•œ ๊ฒƒ์ด๋ฉฐ, ๋น„๊ตํ•˜๋Š” ๋‘ ์ง‘๋‹จ ๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ๊ฐ™์€์ง€ ๋‹ค๋ฅธ์ง€ ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ฒ€์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. plot์€ ์‚ฌํ›„ ๊ฒ€์ • ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋ฉฐ ๋ณด๋Š” ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์‹ ๋ขฐ๊ตฌ๊ฐ„ ์•ˆ์— 0์ด ํฌํ•จ๋˜๋Š”์ง€ ์•„๋‹Œ์ง€๋งŒ ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. low - high : low ์ง‘๋‹จ์˜ satisfaction_level์˜ ํ‰๊ท ๊ณผ high ์ง‘๋‹จ์˜ satisfaction_level์˜ ํ‰๊ท  ์ฐจ์ด๋ฅผ ๋น„๊ตํ•˜๋‹ˆ ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด [ 0.054 ( w) โˆ’ 0.018 ( p) ] ์‚ฌ์ด์— ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ตฌ๊ฐ„ ์•ˆ์— 0์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉฐ, 0๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์— low ์ง‘๋‹จ์˜ satisfaction_level ํ‰๊ท ์€ high ์ง‘๋‹จ์˜ satisfaction_level์— ๋น„ํ•ด ์ž‘๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. medium - high : medium ์ง‘๋‹จ๊ณผ high ์ง‘๋‹จ์˜ satisfaction_level์˜ ๋น„๊ต๋ฅผ ์ง„ํ–‰ํ•œ ๊ฒฐ๊ณผ ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด [ 0.033 0.0024 ] ์ž…๋‹ˆ๋‹ค. 0์„ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— medium ์ง‘๋‹จ๊ณผ high ์ง‘๋‹จ์˜ satisfaction_level์˜ ํ‰๊ท ์€ ๊ฐ™๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. medium - low : ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด 0์„ ํฌํ•จํ•˜์ง€ ์•Š๊ณ  0๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— medium์ด low ์ง‘๋‹จ๋ณด๋‹ค satisfaction_level์˜ ํ‰๊ท ์ด ๋†’๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๋ฆฌ๋ฅผ ํ•˜๋ฉด satisfaction_level์˜ ํ‰๊ท ์€ o < e i m h g ์ˆœ์„œ์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰ low ์ง‘๋‹จ์˜ ํ‰๊ท ์ด ๋‚ฎ๊ธฐ ๋•Œ๋ฌธ์— ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•˜๊ฒŒ ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ง„ํ–‰ํ•œ ์ด๋ก ๊ณผ ๋ถ„์„์„ ์ •ํ™•ํžˆ๋Š” ์ผ์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„(One way Anova)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ‰๊ท ์„ ๋น„๊ตํ•  ๋•Œ ํ•˜๋‚˜์˜ ์š”์ธ๋งŒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์š”์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„์„์„ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ 2๊ฐœ์˜ ์š”์ธ์„ ํ†ตํ•ด ํ‰๊ท ์„ ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋ฉด ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„(Two way Anova)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. T_ANOVA = aov(satisfaction_level ~ salary + left + salary:left, data = HR) summary(T_ANOVA) Df Sum Sq Mean Sq F value Pr(>F) salary 2 2.3 1.17 22.276 2.19e-10 *** left 1 137.8 137.79 2624.960 < 2e-16 *** salary:left 2 0.0 0.01 0.155 0.856 Residuals 14993 787.0 0.05 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ์š”์ธ์„ ํ•˜๋‚˜ ๋” ๋„ฃ์–ด์ฃผ๋ฉด ๋ฐ”๋กœ ์ด์› ๋ฐฐ์น˜ ๋ถ„์‚ฐ๋ถ„์„์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์‹ ๊ฒฝ ์จ์•ผ ํ•  ๋ถ€๋ถ„์€ ์š”์ธ์„ ๋‘ ๊ฐœ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ๊ฐ ์š”์ธ ๊ฐ„์˜ ๊ตํ˜ธ์ž‘์šฉ์ด ์žˆ๋Š”์ง€ ํ™•์ธ์„ ํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ์œ„ ๋ถ„์„ ๊ฒฐ๊ณผ์—์„œ๋Š” ๋‹จ์ผ ํšจ๊ณผ(salary, left)๋Š” ์œ ์˜ํ•œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ ๊ตํ˜ธ์ž‘์šฉํšจ๊ณผ(salary:left)๋Š” ์œ ์˜ํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ๊ตํ˜ธ์ž‘์šฉํ•ญ์„ ์ œ๊ฑฐํ•˜๊ณ  ๋ชจํ˜•์„ ๋‹ค์‹œ ๋ถ„์„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. A8. ์ƒ๊ด€๋ถ„์„ \ ๋งŒ์•ฝ ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜ ๊ฐ„ ๊ด€๊ณ„๋ฅผ ๋ถ„์„ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ, ์šฐ๋ฆฌ๋Š” ์ข…์ข… ์ƒ๊ด€๊ณ„์ˆ˜(correlation)์ด๋ž€ ๊ฒƒ์„ ๊ตฌํ•˜๊ณ ๋Š” ํ•ฉ๋‹ˆ๋‹ค. ๋„ˆ๋ฌด ์œ ๋ช…ํ•œ ์šฉ์–ด๋ผ์„œ, ์ƒ๊ด€๊ณ„์ˆ˜์˜ ์ •ํ™•ํ•œ ์˜๋ฏธ๋Š” ์•Œ์ง€ ๋ชปํ•˜๋”๋ผ๋„, ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ ๋Œ€์ถฉ ์–ด๋–ค ๊ฒƒ์ธ์ง€๋Š” ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ๊ฐœ๋…์„ ์ •๋ฆฝํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ €, ์ƒ๊ด€๋ถ„์„์ด๋ž€ ๋‘ ๋ณ€์ˆ˜์˜ ๊ด€๊ณ„์—์„œ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด, ๋‹ค๋ฅธ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋„ ์ฆ๊ฐ€ํ•˜๋Š”์ง€ ํ˜น์€ ๊ฐ์†Œํ•˜๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋Š”์ง€ ํ™•์ธ์„ ํ•˜๊ธฐ ์œ„ํ•ด ๋ถ„์„์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ทธ๋Ÿฌํ•œ ๊ฒฝํ–ฅ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๊ณต๋ถ„์‚ฐ(Covariance)์ด๋ผ๋Š” ๊ฐ’์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ๊ณต๋ถ„์‚ฐ๊ณผ ์ƒ๊ด€๊ณ„์ˆ˜ O [ , ] E [ ( โˆ’ โ€• ) ( โˆ’ โ€• ) ] ์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐ์„ ํ•˜๋ฉด, X์™€ Y์˜ ์ƒ๊ด€๊ด€๊ณ„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ํ•œ ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ณต๋ถ„์‚ฐ์€ ๋ณ€์ˆ˜์˜ ๋‹จ์œ„์— ๋”ฐ๋ผ ๋ฒ”์œ„๊ฐ€ ๋ฌดํ•œ๋Œ€๊นŒ์ง€ ํ™•์žฅ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ณ€์ˆ˜๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๊ณต๋ถ„์‚ฐ์˜ ๋‹จ์œ„๋„ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ๋น„๊ตํ•˜๋Š” ๊ฐ’์œผ๋กœ ํ™•์ธํ•˜๊ธฐ์—๋Š” ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๊ณต๋ถ„์‚ฐ์— ๋‘ ๋ณ€์ˆ˜์˜ ๋ถ„์‚ฐ์„ ๋‚˜๋ˆ„์–ด์ค๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ณต๋ถ„์‚ฐ์€ -1 ~ 1์˜ ๋ณ€์ˆ˜์˜ ๋‹จ์œ„์— ์ƒ๊ด€์—†์ด ์ผ์ •ํ•œ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๋Š” ์ƒ๊ด€๊ณ„์ˆ˜(Correlation)๋กœ ๋ณ€ํ™˜์ด ๋ฉ๋‹ˆ๋‹ค. o r [ , ] C V [ , ] A [ ] V R [ ] โˆ’ โ‰ค o r [ , ] 1 ํ•ด์„ ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ 1์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ๊ฐ•ํ•œ ๊ธ์ • ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๊ณ  -1์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ๊ฐ•ํ•œ ๋ถ€์ • ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ 0์— ๊ฐ€๊นŒ์šธ ๊ฒฝ์šฐ, ๋‘ ๋ณ€์ˆ˜๋Š” ๊ด€๊ณ„๊ฐ€ ์—†๋‹ค๊ณ  ํ•ด์„ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ๊ด€๋ถ„์„ ์ƒ๊ด€๋ถ„์„์ด๋ž€, ๋‘ ๋ณ€์ˆ˜ ๊ฐ„์˜ ์ƒ๊ด€๊ด€๊ณ„๊ฐ€ 0์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธ์„ ํ•˜๋Š” ํ†ต๊ณ„์  ๊ฒ€์ •๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ท€๋ฌด๊ฐ€์„ค๊ณผ ๋Œ€๋ฆฝ๊ฐ€์„ค์€ ๋‹ค์Œ์ฒ˜๋Ÿผ ์„ธ์šธ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 0 ฯ 0 H : โ‰  A9. ์ƒ๊ด€๋ถ„์„ (R code) ์ƒ๊ด€๋ถ„์„์—์„œ ์ฃผ์˜ํ•  ์ ์€ ์ƒ๊ด€๋ถ„์„์€ ๋‹จ์ˆœํžˆ ๋‘ ๋ณ€์ˆ˜ ๊ฐ„์˜ ์„ ํ˜• ๊ด€๊ณ„๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ๋ฟ์ž…๋‹ˆ๋‹ค. ์ฆ‰ ์ด ๋ง์€ ๋น„์„ ํ˜• ๊ด€๊ณ„๋Š” ์ƒ๊ด€๊ณ„์ˆ˜๋กœ ์žก์•„๋‚ด๊ธฐ ํž˜๋“ค ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. library(ggplot2) x1 = runif(n = 100, min = -10, max = 10) y = x1 * 10 + rnorm(n = 100, mean = 3, sd = 5) ggplot() + geom_point(aes(x = x1, y= y),size = 3) + geom_text(aes(x = 5, y = -30),label = round(cor(x1,y),4)) + theme_bw() ์œ„ ๋‘ ๋ณ€์ˆ˜๋Š” ์‚ฐ์ ๋„๋กœ ๋ณด๋‚˜, ์ƒ๊ด€๊ณ„์ˆ˜๋กœ ๋ณด๋‚˜ ๊ฑฐ์˜ 1์— ๊ฐ€๊นŒ์šด ์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋‘ ๋ณ€์ˆ˜์˜ ๊ด€๊ณ„๋ฅผ ํ•˜๋‚˜์˜ ์„ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ƒ๊ด€๋ถ„์„ ๊ฒ€์ •์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. cor.test(x1,y) Pearson's product-moment correlation data: x1 and y t = 113.5, df = 98, p-value < 2.2e-16 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.9943742 0.9974582 sample estimates: cor 0.9962179 pโˆ’valuepโˆ’value๊ฐ€ ๋งค์šฐ ๋‚ฎ์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋‘ ๋ณ€์ˆ˜์˜ ์ƒ๊ด€๊ณ„์ˆ˜๋Š” 0์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒฐ๋ก ์„ ๋‚ด๋ฆด ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. y = x1^2 + x1 * 10 + rnorm(n = 100, mean = 3, sd = 5) ggplot() + geom_point(aes(x = x1, y= y),size = 3) + geom_text(aes(x = 0, y = 100),label = round(cor(x1,y),4)) + theme_bw() ๋‘ ๋ฒˆ์งธ ์‚ฐ์ ๋„์—์„œ๋Š” ์„ ํ˜•๋ณด๋‹ค๋Š” 2์ฐจ ํ•จ์ˆ˜ ๊ผด์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ 0.8 ~ 0.9๋กœ ๋‚ฎ์•„์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ์ „ํžˆ ๋†’์€ ๊ฐ’์ธ ๊ฒƒ์€ ๋งž์ง€๋งŒ ์„ ํ˜• ๊ด€๊ณ„๊ฐ€ ์•„๋‹ˆ๊ธฐ์— ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ ๊ฐ์†Œ๋ฅผ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๊ทธ๋ ‡๋‹ค๊ณ  ์ € ๋‘ ๋ณ€์ˆ˜์˜ ์ƒ๊ด€๋„๊ฐ€ ๋‚ฎ์•„์กŒ๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ธฐ๋Š” ํž˜๋“ญ๋‹ˆ๋‹ค. ์„ ํ˜• ๊ด€๊ณ„๊ฐ€ ์•„๋‹ ๋ฟ์ด์ง€, ๋น„์„ ํ˜• ๊ด€๊ณ„๋Š” ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. y = sin(x1) + rnorm(n = 100, mean = 3, sd = 0.3) ggplot() + geom_point(aes(x = x1, y= y),size = 3) + geom_text(aes(x = 0, y = 5),label = round(cor(x1,y),4)) + theme_bw() ์ด๋ฒˆ ์‚ฐ์ ๋„๋Š” ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ ๋งค์šฐ ๋‚ฎ๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ, ๊ทธ๋ ‡๋‹ค๊ณ  ์ƒ๊ด€์ด ์—†๋‹ค๊ณ  ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์‚ฐ์ ๋„์—๋Š” ๋šœ๋ ท์ด ์‚ผ๊ฐํ•จ์ˆ˜ sin(x) ๊ผด์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ๊ด€๋ถ„์„์€ ์ •๋ง ํŽธํ•œ ๋ถ„์„์ด์ง€๋งŒ, ๊ทธ๋ ‡๋‹ค๊ณ  ๋งŒ๋Šฅ์ธ ๋ถ„์„์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ €๋Š” ๊ฐœ์ธ์ ์œผ๋กœ ์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ๋ณด๋‹ค๋Š” ์‚ฐ์ ๋„๋ฅผ ์ง์ ‘ ๋ด„์œผ๋กœ์จ ๋ณ€์ˆ˜์˜ ์ƒ๊ด€ ์ •๋„๋ฅผ ํŒŒ์•…ํ•˜๊ณ , ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์ด ๋” ์ •ํ™•ํ•ฉ๋‹ˆ๋‹ค. B1. ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€๋ถ„์„์˜ ์ถ”์ • 10. ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€๋ถ„์„์˜ ์ถ”์ • ํšŒ๊ท€๋ถ„์„ : ์ธ๊ณผ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋‘ ๋ณ€์ˆ˜ ๊ฐ„์˜ ํ•จ์ˆ˜๊ด€๊ณ„๋ฅผ ํ†ต๊ณ„์ ์œผ๋กœ ๊ทœ๋ช…ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ถ„์„ ๋ถ„์‚ฐ๋ถ„์„๊ณผ ํšŒ๊ท€๋ถ„์„์€ ์„ ํ˜•๋ชจํ˜•์ด๋ผ๋Š” ํฐ ์ค„๊ธฐ์—์„œ ๊ฐ™์€ ๋ฐฉ๋ฒ•๋ก ์ด๋ผ๋Š” ๋ง์”€์„ ๋“œ๋ฆฐ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ชจํ˜• ๋ชจ๋‘ ์˜ˆ์ธก์ž(predictor)์— ๋”ฐ๋ฅธ ํ‰๊ท  ๋ฐ˜์‘ ๊ฐ’์„ ์ถ”์ • ํ˜น์€ ์˜ˆ์ธกํ•˜๋Š” ๋ชจํ˜•์œผ๋กœ, ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ์„ ํ˜• ๋ชจํ˜•์„ ์„ค์ •ํ•˜๊ณ  ์ƒˆ๋กœ์šด ๊ฐ’์— ๋Œ€ํ•œ ๋ฐ˜์‘ ๊ฐ’์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์— ๊ทธ ๋ชฉ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ธก ์ž๋ž€ ๋ฐ˜์‘ ๊ฐ’์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์œผ๋กœ ์„ค๋ช… ๋ณ€์ˆ˜(explanatory variable)์™€ ํ˜ผ์šฉ๋˜๋Š” ๊ฐœ๋…์œผ๋กœ ์ดํ•ดํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์•„๋…ธ๋ฐ”์™€ ํšŒ๊ท€ ๋ชจํ˜•์˜ ์ฐจ์ด์ ์ด ์žˆ๋‹ค๋ฉด ์•„๋…ธ๋ฐ”์™€ ๋‹ฌ๋ฆฌ ํšŒ๊ท€๋ถ„์„์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์—ฐ์†ํ˜• ์˜ˆ์ธก์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ํšŒ๊ท€๋ถ„์„์€ ์„ค๋ช… ์˜ˆ์ธก์ž์™€ ๋ฐ˜์‘ ๊ฐ’์˜ ์ˆ˜์— ๋”ฐ๋ผ ๋‹จ์ˆœ ํšŒ๊ท€๋ถ„์„, ๋‹ค์ค‘ ํšŒ๊ท€๋ถ„์„ ๋“ฑ์œผ๋กœ ๋ถ„๋ฅ˜๋˜๋ฉฐ ์„ค๋ช… ์˜ˆ์ธก์ž์™€ ๋ฐ˜์‘ ๊ฐ’์˜ ๊ด€๊ณ„์— ๋”ฐ๋ผ ์„ ํ˜• ํšŒ๊ท€๋ถ„์„, ๋‹คํ•ญ ํšŒ๊ท€๋ถ„์„(๋น„์„ ํ˜• ํšŒ๊ท€๋ถ„์„) ๋“ฑ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง‘๋‹ˆ๋‹ค. ๋จผ์ € ์„ ํ˜• ํšŒ๊ท€๋ถ„์„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ํšŒ๊ท€๋ถ„์„์˜ ๊ฐ€์žฅ ํ•ต์‹ฌ ๊ด€์ ์€ ๋‘ ๋ณ€์ˆ˜ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์„ ํ˜•์ ์œผ๋กœ ๋ฐ”๋ผ๋ณด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๊ด€๊ณ„๋ฅผ ํ™•์ธํ•˜๋ ค๋Š” ๋‘ ๋ณ€์ˆ˜๊ฐ€ ์„ ํ˜•์ ์ด์ง€ ์•Š๋‹ค๋ฉด ์• ์ดˆ์— ์„ฑ๋ฆฝ๋  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด ํšŒ๊ท€๋ถ„์„์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์„ ํ˜•์„ฑ์€ ๋ถ„์„ ์ „์— ์‚ฐ์ ๋„์™€ ๊ฐ™์€ ๊ทธ๋ž˜ํ”„๋ฅผ ํ†ตํ•ด ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋ฉฐ ์„ ํ˜• ํšŒ๊ท€๋ถ„์„ ์ „์ฒด๋ฅผ ์•„์šฐ๋ฅด๋Š” ์ค‘์š”ํ•œ ๊ฐ€์ •์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํšŒ๊ท€๋ถ„์„์˜ ์ตœ์ข… ๋ชฉํ‘œ๋Š” ๋‘ ๋ณ€์ˆ˜ ์‚ฌ์ด์˜ ์„ ํ˜•์„ฑ์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ๊ทธ ์„ ํ˜• ๊ด€๊ณ„๋ฅผ ๋Œ€ํ‘œํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜๋‚˜์˜ ์ง์„ ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Y์™€ X๋ผ๋Š” ๋‘ ๋ณ€์ˆ˜ ์‚ฌ์ด์˜ ๊ด€๊ณ„๊ฐ€ = + X ๋ผ๋Š” ์ง์„ ์œผ๋กœ ๋Œ€ํ‘œ๋  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ๋‘ ๋ณ€์ˆ˜ ์‚ฌ์ด์˜ ๊ด€๊ณ„๋ฅผ ํ•œ๋ˆˆ์— ์•Œ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ๋ฌผ๋ก ์ด๊ณ  ์ƒˆ๋กœ์šด ๊ฐ’์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๊ฐ’์„ ์†์‰ฝ๊ฒŒ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ณ ๋ฏผํ•ด ๋ณด์•„์•ผ ํ•˜๋Š” ๊ฒƒ์€, ๊ด€์ฐฐ์น˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ทธ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์™€๋ฅผ ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ ์ถ”์ •ํ•ด์•ผ ๊ฐ€์žฅ ํšจ์œจ์ ์ด๊ณ  ๋‘ ๋ณ€์ˆ˜ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ •ํ™•ํžˆ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์„๊นŒ์— ๋Œ€ํ•œ ๋ฐฉ๋ฒ•๋ก ์  ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ํšŒ๊ท€๋ถ„์„์—์„œ๋Š” ์ž”์ฐจ์˜ ์ œ๊ณฑํ•ฉ์„ ์ตœ์†Œ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์™€๋ฅผ ์ถ”์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํƒํ•˜์˜€๊ณ  ์ด๋ฅผ ์ตœ์†Œ ์ œ๊ณฑ ์ถ”์ • ๋ฒ•(method of least squares estimation)์ด๋ผ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ž”์ฐจ(residual)๋Š” ์‹ค์ œ ๊ด€์ธก ๊ฐ’๊ณผ ์˜ˆ์ธก๋œ ๊ฐ’์˜ ์ฐจ์ด๋ฅผ ๋งํ•˜๋ฉฐ ๊ทธ ์ œ๊ณฑํ•ฉ์„ ์ตœ์†Œ๋กœ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํ•ด๋‹น ์„ ๊ณผ ์‹ค์ œ ๊ด€์ธก ๊ฐ’์˜ ์ฐจ์ด์˜ ์ด๋Ÿ‰์„ ์ตœ์†Œ๋กœ ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์„ ์ฐธ๊ณ ํ•˜๋ฉด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ์ดํ•ดํ•˜์‹ค ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํŒŒ๋ž€์ƒ‰ ์ง์„ ์ด ์ ํ•ฉ๋œ ํšŒ๊ท€์„ ์ด๊ณ  ๊ฒ€์€ ์ ๋“ค์ด ๊ฐ ๊ด€์ฐฐ์น˜์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์‹ค์ œ ๊ด€์ฐฐ์น˜์™€ ์ ํ•ฉ๋œ ํšŒ๊ท€์„ ์˜ ์ฐจ์ด๊ฐ€ ๋ฐ”๋กœ ์ž”์ฐจ์ด๊ณ  ๊ทธ ์ œ๊ณฑ์€ ์œ„์™€ ๊ฐ™์ด ์‚ฌ๊ฒฉํ˜•์˜ ๋ฉด์ ์œผ๋กœ ํ‘œํ˜„๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ž”์ฐจ๋ฅผ ๊ทธ๋ƒฅ ํ•ฉํ•˜๋ฉด + / - ๊ฐ€ ์ƒ์‡„๋˜์–ด ์˜๋ฏธ๊ฐ€ ์—†์–ด์ง€์ง€๋งŒ, ์ด๋Ÿฐ ์‹์œผ๋กœ ์ œ๊ณฑ ํ›„์— ์ดํ•ฉ ์ด์šฉํ•˜๋ฉด ์‹ค์ œ ๊ด€์ฐฐ์น˜์™€ ์ ํ•ฉ๋œ ํšŒ๊ท€์„  ์‚ฌ์ด์˜ ์ฐจ์ด๋ฅผ ํ‘œํ˜„ํ•ด ์ค„ ์ˆ˜ ์žˆ๊ณ  ์ด๊ฒƒ์€ ํšŒ๊ท€์„ ์ด ์–ผ๋งˆ๋‚˜ ๊ด€์ฐฐ์น˜๋“ค์„ ์ž˜ ๋Œ€ํ‘œํ•˜๊ณ  ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ฒ™๋„๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ด€์ ์—์„œ ๋ดค์„ ๋•Œ ์ตœ์†Œ์ œ๊ณฑ๋ฒ•, ์ฆ‰, ์ด ์ž”์ฐจ ์ œ๊ณฑํ•ฉ์„ ์ตœ์†Œํ™”ํ•˜๋Š” ํšŒ๊ท€์„ ์€ ์ถฉ๋ถ„ํžˆ ํ•ฉ๋ฆฌ์ ์ธ ํšŒ๊ท€์„ ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํšŒ๊ท€์„ ์˜ ํ‰๊ฐ€ ์ตœ์†Œ์ œ๊ณฑ๋ฒ•์„ ์ด์šฉํ•˜๋ฉด ์–ด๋–ค ์ž๋ฃŒ์—์„œ๋“  ํšŒ๊ท€์„ ์„ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ๊ณ , ์ตœ์†Œ์ œ๊ณฑ๋ฒ•์ด ์•„๋ฌด๋ฆฌ ํ•ฉ๋ฆฌ์ ์ธ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•˜์—ฌ๋„ ๊ด€์ฐฐ์น˜๋“ค์„ ์ ˆ๋Œ€์ ์œผ๋กœ ์ž˜ ๋Œ€ํ‘œํ•˜๋Š” ์ง์„ ์„ ํ•ญ์ƒ ์ œ์‹œํ•˜์ง€๋Š” ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํšŒ๊ท€์„ ์„ ์ ํ•ฉ์‹œํ‚จ ํ›„์—๋Š” ๊ผญ ๊ทธ ํšŒ๊ท€์„ ์ด ์–ผ๋งˆ๋‚˜ ์ •ํ™•ํ•œ์ง€์— ๋Œ€ํ•œ ๊ฒ€์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํšŒ๊ท€์„ ์˜ ์ ํ•ฉ๋„ ํ˜น์€ ์ •๋„๋ฅผ ํ‰๊ฐ€ํ•œ๋‹ค๊ณ  ํ•˜๋ฉฐ ๋ถ„์‚ฐ๋ถ„์„์˜ ์•„์ด๋””์–ด๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์™€์„œ ์ง„ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ด€์ฐฐ์น˜ i ( ์ฐฐ ) Y (๊ด€์ฐฐ์น˜์˜ ํ‰๊ท ), i (ํšŒ๊ท€์„ ์— ์˜ํ•œ ์ถ”์ • ๊ฐ’)์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด ํŽธ์ฐจ( i Y)๋ฅผ ํšŒ๊ท€์„ ์„ ๊ธฐ์ค€์œผ๋กœ ๋‘ ์˜์—ญ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ค€๋‹ค. ํšŒ๊ท€์„ ์— ์˜ํ•ด ์„ค๋ช…์ด ๊ฐ€๋Šฅํ•œ ์˜์—ญ : i โˆ’ โ€• ํšŒ๊ท€์„ ์— ์˜ํ•ด ์„ค๋ช…์ด ๋˜์ง€ ์•Š์€ ์˜์—ญ : i Y ^ ๋ชจ๋“  ๊ด€์ฐฐ์น˜์— ๋Œ€ํ•ด ์ œ๊ณฑํ•ฉ์„ ๊ณ„์‚ฐํ•œ ํ›„, ์ง๊ต ๋ถ„ํ•ด๋ฅผ ํ•œ๋‹ค. i 1 ( i Y) = i 1 ( ^ โˆ’ โ€• ) + i 1 ( i Y i ) S T S R S E ๋ถ„์‚ฐ๋ถ„์„๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ํšŒ๊ท€์„ ์˜ ์œ ์˜์„ฑ ๊ฒ€์ •์„ ํ•œ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํšŒ๊ท€์„ ์œผ๋กœ ์„ค๋ช…ํ•˜๋Š” ์ƒ๋Œ€์ ์ธ ๋น„์ค‘์„ ๋‚˜ํƒ€๋‚ด๋Š” S ์ด ๋ชจํ˜•์˜ ์ž”์ฐจ(Error)๋ฅผ ์„ค๋ช…ํ•˜๋Š” S ๋ณด๋‹ค ์ƒ๋Œ€์ ์œผ๋กœ ์ปค์•ผ์ง€ ํšŒ๊ท€์„ ์ด ์œ ์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ œ๊ณฑํ•ฉ์„ ๋น„๊ตํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งŽ์€ ๋ถ„๋“ค์ด SSE์˜ ์ž์œ ๋„๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์›Œํ•˜์‹ญ๋‹ˆ๋‹ค. ์ด๋Š” ๊ฒฐ์ • ๊ณ„์ˆ˜( 2 )์™€๋„ ๊ด€๋ จ์ด ์žˆ๋Š” ๋ถ€๋ถ„์ธ๋ฐ, ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๊ณ  ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž์œ ๋„๋Š” ์ž์œ ๋กญ๊ฒŒ ์„ ํƒ๋  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ ์ˆ˜๋กœ, ์˜จ์ „ํžˆ ํ•ด๋‹น ํ†ต๊ณ„๋Ÿ‰์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฃŒ ์ˆ˜๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ด€์ฐฐ์น˜๊ฐ€ 2๊ฐœ๋ฐ–์— ์—†๋‹ค๋ฉด ํšŒ๊ท€์„ ์€ ๋ฌด์กฐ๊ฑด ๊ทธ ๋‘ ์ ์„ ์ง€๋‚˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ ์ง์„ ์ด ๋งŒ๋“ค์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋‘ ์„ ์„ ์ง€๋‚˜๋Š” ํšŒ๊ท€์„ ์„ ์ถ”์ •ํ•˜๊ธฐ๋งŒ ํ•  ๋ฟ, ๊ทธ ํšŒ๊ท€์„ ์„ ํ‰๊ฐ€ํ•  ์ž์œ ๋„๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ๊ด€์ ์œผ๋กœ ๊ด€์ฐฐ์น˜๊ฐ€ 3๊ฐœ๋ผ๋ฉด 2๊ฐœ๋Š” ์œ„์™€ ๊ฐ™์€ ๋…ผ๋ฆฌ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ํšŒ๊ท€์„ ์˜ ์ •๋„๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ์—๋Š” ํ•˜๋‚˜์˜ ๊ด€์ฐฐ์น˜๋งŒ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๊ฒ๋‹ˆ๋‹ค. ์ฆ‰, ๊ธฐ๋ณธ์ ์œผ๋กœ ํšŒ๊ท€์„ ์€ 2๊ฐœ์˜ ์ž์œ ๋„๋ฅผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํšŒ๊ท€์„  ์ •๋„ ํ‰๊ฐ€๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” S๋Š” โˆ’์˜ ์ž์œ ๋„๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์„ค๋ช… ์˜ˆ์ธก์ž๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๋‹ค์ค‘ํšŒ๊ท€๋ถ„์„์—๋„ ๊ทธ๋Œ€๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 2๊ฐœ์ธ ๊ฒฝ์šฐ ํšŒ๊ท€์‹์€ 3์ฐจ์›์˜ ์ขŒํ‘œ ๊ณต๊ฐ„์—์„œ ์–ด๋–ค ํ‰ํ‰ํ•œ ๋ฉด์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉด์€ ์ตœ์†Œํ•œ 3๊ฐœ์˜ ์ ์ด ์žˆ์–ด์•ผ ํ˜•์„ฑ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ด€์ฐฐ์น˜๊ฐ€ 3๊ฐœ์ธ ๊ฒฝ์šฐ, ์ ํ•ฉ๋œ ํšŒ๊ท€์‹์„ ๋ฌด์กฐ๊ฑด ๊ทธ ์„ธ ์ ์„ ์ง€๋‚˜๊ฐˆ ๊ฒƒ์ด๋ฉฐ, ํšŒ๊ท€์‹์˜ ์ •๋„๋ฅผ ํŒŒ์•…ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ตœ์†Œํ•œ 4๊ฐœ ์ด์ƒ์˜ ๊ด€์ฐฐ์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.(3๊ฐœ์˜ ์ ๋งŒ ์žˆ๋‹ค๋ฉด ํšŒ๊ท€ ๋ฉด์€ ๋ชจ๋“  ์ ์„ ์ง€๋‚˜๊ฐˆ ํ…Œ๊ณ  ์ด๋Š” ํšŒ๊ท€์„  ํ‰๊ฐ€์— ๊ด€ํ•ด์„œ ์•„๋ฌด ์˜๋ฏธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.) ์ฆ‰, ์ด ๊ฒฝ์šฐ ํšŒ๊ท€์‹์€ 3์˜ ์ž์œ ๋„๋ฅผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํšŒ๊ท€์‹ ํ‰๊ฐ€๋ฅผ ์œ„ํ•œ S๋Š” n-3์˜ ์ž์œ ๋„๋ฅผ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด SSE๋Š” n-์˜ˆ์ธก์ž ์ˆ˜-1 ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์˜ˆ์ธก์ž ์ˆ˜๋ฅผ k๋กœ ํ‘œํ˜„ํ•˜๋‹ˆ, n-k-1์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ์ด์œ ๋กœ ํšŒ๊ท€์„ ์„ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์„ค๊ณผ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถ”์ •๋œ ํšŒ๊ท€์‹์€ ์œ ์˜ํ•˜์ง€ ์•Š๋‹ค 0 ์ถ” ๋œ ๊ท€ ์€์˜ ์ง€ ๋‹ค ์ถ”์ •๋œ ํšŒ๊ท€์‹์€ ์œ ์˜ํ•˜๋‹ค 1 ์ถ” ๋œ ๊ท€ ์€์˜ ๋‹ค 0 S R d R S / f โˆผ ( f, f) d R k f = โˆ’ โˆ’ n n m e o o s r a i n ๋งŒ์•ฝ MSE์— ๋น„ํ•ด MSR์ด ์ƒ๋Œ€์ ์œผ๋กœ ํฐ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด 0 ๋Š” ๋†’์€ ๊ฐ’์„ ๊ฐ–๊ฒ ๊ณ , ์ด๋Š” ํšŒ๊ท€์‹์œผ๋กœ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€๋™์ด ๊ทธ๋ ‡์ง€ ์•Š์€ ๋ณ€๋™์— ๋น„ํ•ด ํฌ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•ฉ๋‹ˆ๋‹ค. 2 ๊ณ„์‚ฐ ํšŒ๊ท€์„ ์ด ์ „์ฒด ๋ณ€๋™ ์ค‘ ์„ค๋ช…ํ•˜๋Š” ๋น„์ค‘์„ ํšŒ๊ท€์„ ์˜ ์„ค๋ช…๋ ฅ, 2 ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 2 S R S = โˆ’ S S T ์ด๋Š” ๊ฒฐ์ • ๊ณ„์ˆ˜(coefficient of determination, 2 )๋ผ๋Š” ์ฒ™๋„๋กœ ์ „์ฒด ๋ณ€๋™์ค‘ ํšŒ๊ท€์„ ์— ์˜ํ•ด ์„ค๋ช…๋˜๋Š” ๋ณ€๋™์ด ์ฐจ์ง€ํ•˜๋Š” ๋น„์ค‘์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ 'ํšŒ๊ท€์‹์˜ ๊ธฐ์—ฌ์œจ'์ด๋ผ๋Š” ๊ด€์ ์œผ๋กœ ํ•ด์„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์—ญ์‹œ ์œ„์˜ F ๊ฒ€์ •๊ณผ ๊ฐ™์€ ์›๋ฆฌ์ด๋ฉฐ, ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํšŒ๊ท€์„ ์„ ํ‰๊ฐ€ํ•˜๋Š” ์ฒ™๋„์ž…๋‹ˆ๋‹ค. ํšŒ๊ท€ ๋ชจํ˜• ์„ค์ • ํšŒ๊ท€ ๋ชจํ˜•์„ ์„ค์ •ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ’์€ ์ฃผ์–ด์ง„ ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰ํ•˜๋ฏ€๋กœ ์˜ค์ฐจ ํ•ญ์˜ ๋ถ„ํฌ๋Š” ๊ณง ๋ฐ˜์‘ ๊ฐ’์˜ ๋ถ„ํฌ์ž…๋‹ˆ๋‹ค. i ฮฒ + 1 i ฯต, i i d ( , 2 ) ๋ถ„์‚ฐ๋ถ„์„์—์„œ์™€ ๊ฐ™์ด ์˜ค์ฐจํ•ญ์— ๋Œ€ํ•œ ๊ฐ€์ •์€ ๊ณง ํšŒ๊ท€๋ถ„์„์„ ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ€์ •์ด๊ณ  i N ์— ๋”ฐ๋ผ ๋…๋ฆฝ์„ฑ,<NAME>, ๋“ฑ ๋ถ„์‚ฐ์„ฑ ์„ธ ๊ฐ€์ง€ ์„ฑ์งˆ์„ ์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„ธ ๊ฐ€์ •์€ ์˜ค์ฐจ ํ•ญ์˜ ์ถ”์ •๋Ÿ‰์ธ ์ž”์ฐจ๋ฅผ ํ†ตํ•ด ์ง„๋‹จ๋˜๋ฉฐ ์ž”์ฐจ๊ฐ€ ๊ด€์ฐฐ์น˜์— ๋”ฐ๋ผ ํŠน์ •ํ•œ ํŒจํ„ด์„ ๋ณด์ด๊ฑฐ๋‚˜ ์ปค์กŒ๋‹ค ์ž‘์•„์กŒ๋‹ค ํ•˜๋Š” ์–‘์ƒ์„ ๋ณด์ธ๋‹ค๋ฉด ๋…๋ฆฝ์„ฑ, ๋“ฑ ๋ถ„์‚ฐ์„ฑ์„ ์˜์‹ฌํ•ด ๋ณด์•„์•ผ ํ•˜๋ฉฐ ์ž”์ฐจ์˜ ํžˆ์Šคํ† ๊ทธ๋žจ์„ ๊ทธ๋ ค๋ณด๊ฑฐ๋‚˜ ๊ด€๋ จ ๊ฒ€์ •์„ ํ†ตํ•ด<NAME>์„ ๋งŒ์กฑํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ํ™•์ธํ•ด ๋ณด์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์ง€๊ณ  ์ถ”์ •ํ•œ ํšŒ๊ท€์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ^ = 0 b X ์ ˆํŽธ 0 ๋Š” ์˜ˆ์ธก์ž๊ฐ€ 0์˜ ๊ฐ’์„ ๊ฐ€์งˆ ๋•Œ์˜ ๋ฐ˜์‘ ๊ฐ’์„ ์˜๋ฏธํ•˜๋ฉฐ ์ดˆ๊นƒ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ 1 ์€ ์˜ˆ์ธก์ž๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ˜์‘ ๊ฐ’์— ๋Œ€ํ•œ ๋‹จ์œ„ ๋‹น ์˜ํ–ฅ๋„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ๊ฐ€ ์–‘์˜ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๋‹ค๋ฉด ์˜ˆ์ธก์ž๊ฐ€ ์ฆ๊ฐ€ํ• ์ˆ˜๋ก ๋ฐ˜์‘ ๊ฐ’ ์—ญ์‹œ ์ฆ๊ฐ€ํ•  ๊ฒƒ์ด๋ฉฐ ์Œ์˜ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค๋ฉด ์˜ˆ์ธก์ž์˜ ์ฆ๊ฐ€์— ๋”ฐ๋ผ ๋ฐ˜์‘ ๊ฐ’์€ ๊ฐ์†Œํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์ ˆ๋Œ“๊ฐ’์ด ํด์ˆ˜๋ก ๊ทธ ์˜ํ–ฅ์ด ํฌ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ์˜ํ–ฅ๋„ ์—ญ์‹œ ์œ ์˜ํ•œ์ง€ ๊ฒ€์ •์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํšŒ๊ท€๊ณ„์ˆ˜์˜ ๊ฒ€์ • ํ˜น์€ ํšŒ๊ท€์‹์˜ ๊ธฐ์šธ๊ธฐ ๊ฒ€์ •์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์šธ๊ธฐ ๊ฒ€์ •์€ ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ<NAME>์˜ ๊ฐ€์ •์ด ๋งŒ์กฑํ•˜๋ฉด, ํšŒ๊ท€๊ณ„์ˆ˜๋Š” ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ํšŒ๊ท€๊ณ„์ˆ˜๊ฐ€๊ฐ€ 0์ธ๊ฐ€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒ€์ •์€ ํ•ด๋‹น ์„ค๋ช…๋ณ€์ˆ˜๊ฐ€ ๋ฐ˜์‘ ๊ฐ’์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ์„ค๋ช…๋ณ€์ˆ˜๊ฐ€ ํฌํ•จ๋œ ๋ชจํ˜•์—์„œ๋„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ ๋ณ€์ˆ˜์˜ ๋…๋ฆฝ์ ์ธ ์˜ํ–ฅ๋ ฅ์„ ํ‰๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํšŒ๊ท€๊ณ„์ˆ˜๋Š”์ด๋‹ค 0 ฮฒ = ํšŒ ๊ณ„๋Š” ์ด. ํšŒ๊ท€๊ณ„์ˆ˜๋Š” ์ด ์•„๋‹ˆ๋‹ค 1 ฮฒ โ‰  ํšŒ ๊ณ„๋Š” ์ด ๋„ค. B2. ํšŒ๊ท€๋ถ„์„(R Code) 11. ํšŒ๊ท€๋ถ„์„(R Code) ํšŒ๊ท€๋ถ„์„์€ ์ œ๊ฐ€ ๋งŒ๋“ค์–ด ๋‘” ๋ฐ์ดํ„ฐ๋กœ ์ง„ํ–‰์„ ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://www.drop box.com/sh/vtqlvrgdts2yfez/AAD_cd49dBcvgBNdz-C-A6TFA? dl=0 # ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ Regression = read.csv("F:\Drop box\DATA SET(Drop box)/Regression.csv") ์‚ฐ์ ๋„ ํšŒ๊ท€๋ถ„์„์€ ์šฐ์„ ์ ์œผ๋กœ ์‚ฐ์ ๋„๋ฅผ ๊ทธ๋ ค๋ณด๊ณ  ์„ ํ˜•์„ฑ์„ ํŒ๋‹จํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. library(ggplot2) ggplot(Regression, aes(x = X, y = y)) + geom_point() + geom_smooth(method = 'lm') + theme_classic() ์‚ฐ์ ๋„๋ฅผ ๊ทธ๋ ค๋ณธ ๊ฒฐ๊ณผ X์™€ y๋Š” ์„ ํ˜• ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ๋‹จ์ˆœ ์„ ํ˜• ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํšŒ๊ท€๋ถ„์„ Reg = lm(y ~ X, data = Regression) anova(Reg) Analysis of Variance Table Response: y Df Sum Sq Mean Sq F value Pr(>F) X 1 143757 143757 3882.2 < 2.2e-16 *** Residuals 148 5480 37 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 summary(Reg) Call: lm(formula = y ~ X, data = Regression) Residuals: Min 1Q Median 3Q Max -9.5537 -5.7116 0.2738 5.0961 10.2835 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.9839 0.9975 1.989 0.0486 * X 10.0924 0.1620 62.308 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 6.085 on 148 degrees of freedom Multiple R-squared: 0.9633, Adjusted R-squared: 0.963 F-statistic: 3882 on 1 and 148 DF, p-value: < 2.2e-16 โˆ’ a u๋Š” 0๊ณผ ๋งค์šฐ ๊ฐ€๊นŒ์šด ๊ฐ’์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํšŒ๊ท€๋ถ„์„์˜ ๊ท€๋ฌด๊ฐ€์„ค(ํšŒ๊ท€์‹์€ ์œ ์˜ํ•˜์ง€ ์•Š๋‹ค 0 ํšŒ ์‹ ์œ  ํ•˜ ์•ˆ )์„ ๊ธฐ๊ฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํšŒ๊ท€์„ ์€ ์œ ์˜ํ•ฉ๋‹ˆ๋‹ค. R ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์ž์„ธํžˆ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Residuals๋Š” i y ^ ์˜ ์š”์•ฝ ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Coefficients๋Š” ์ถ”์ •๋œ ํšŒ๊ท€์‹์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Estimate๋Š” ํšŒ๊ท€์‹์˜ ์ ˆํŽธ๊ณผ ๊ธฐ์šธ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. (Intercept) : 0 ์˜ ๊ฐ’์€ 1.9839์ž…๋‹ˆ๋‹ค. : 1 ์˜ ๊ฐ’์€ 10.0924์ž…๋‹ˆ๋‹ค. Pr(>|t|)๋Š” ๊ฐ๊ฐ 0 b์— ๋Œ€ํ•œ ๊ฐ€์„ค๊ฒ€์ • ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์„ค๋ช… ๋ณ€์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค์ค‘ ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•  ๊ฒฝ์šฐ, ๊ฐ ์„ค๋ช… ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๊ฐ€์„ค ๊ฒ€์ •์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. Multiple R-squared๋Š” ํšŒ๊ท€ ๋ชจํ˜•์˜ 2 ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Adjusted R-squared๋Š” ์กฐ๊ธˆ์€ ๋‹ค๋ฅธ 2 ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํšŒ๊ท€ ๋ชจํ˜•์€ ์„ค๋ช… ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์„์ˆ˜๋ก ์„ค๋ช…๋ ฅ์€ ๋†’์•„์ง€๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ๋œ ๋‘ ํšŒ๊ท€ ๋ชจํ˜•์˜ ์„ค๋ช…๋ ฅ์„ ๋น„๊ตํ•˜๊ณ ์ž ํ•  ๋•Œ Adjusted R-squared๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. o e 1 y ^ b + 1 1 + 2 2 M d l : i = 0 + 1 1 + ์˜ˆ๋ฅผ ๋“ค์–ด ํšŒ๊ท€์‹์ด 2๊ฐœ๊ฐ€ ์žˆ๊ณ  ๋‘ ๋ชจํ˜• ์ค‘ 2 ๊ฐ€ ๋” ๋†’์€ ๋ชจํ˜•์„ ํƒํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. o e 2 ๋Š” 3๊ฐœ์˜ ์„ค๋ช… ๋ณ€์ˆ˜๊ฐ€ ํˆฌ์ž…๋˜์—ˆ๊ธฐ์— ์„ค๋ช… ๋ณ€์ˆ˜๋ฅผ 2๊ฐœ๋ฅผ ํˆฌ์ž…ํ•œ o e 1 ์— ๋น„ํ•ด ์šฐ์œ„์— ์žˆ์Šต๋‹ˆ๋‹ค. 2 ์˜ ๊ตฌ์กฐ์ƒ ์„ค๋ช…๋ณ€์ˆ˜๊ฐ€ ๋งŽ์•„์ง€๋ฉด ํ•ญ์ƒ ์˜ค๋ฅผ ์ˆ˜๋ฐ–์— ์—†๋Š” ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์„ค๋ช… ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์€ ๊ฒƒ์€ ํ•ญ์ƒ ๋ฐ”๋žŒ์งํ•œ ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋งŒํผ ๋งŽ์€ ๊ณ„์ˆ˜๋ฅผ ์ถ”์ •ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๊ทธ๋ ‡๊ธฐ์— ์„ค๋ช… ๋ณ€์ˆ˜๊ฐ€ ๋” ๋งŽ์ด ํˆฌ์ž…๋œ ๋ถ€๋ถ„์„ ๋ณด์ •ํ•ด ์ฃผ์–ด ๋” ๋ฐ”๋žŒ์งํ•œ ๋ชจํ˜•์˜ ๊ธฐ์ค€์„ ์ œ์‹œํ•ด ์ฃผ๋Š” 2 ๊ฐ’์ด Adjusted R-square์ž…๋‹ˆ๋‹ค. p-value : ํšŒ๊ท€ ๋ชจํ˜•์— ๋Œ€ํ•œ ์œ ์˜์„ฑ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํšŒ๊ท€๋ถ„์„์˜ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ ํ•ฉ๋œ ํšŒ๊ท€์„ ์€ i = 1.9839 10.0924 i ์ž…๋‹ˆ๋‹ค. i ๊ฐ€ ํ•œ ๋‹จ์œ„ ์ฆ๊ฐ€ํ•˜๋ฉด i๋Š” 10.0924๋งŒํผ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. 2 ๋Š” 0.963์œผ๋กœ 96%์˜ ์„ค๋ช…๋ ฅ์„ ๊ฐ€์ง„๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž”์ฐจ ์ง„๋‹จ ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•œ ํ›„์—๋Š” ํ•ญ์ƒ ๊ฐ€์ • ๊ฒ€ํ† ๋ฅผ ์œ„ํ•ด ์ž”์ฐจ ์ง„๋‹จ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํšŒ๊ท€๋ถ„์„์—์„œ์˜ ๊ฐ€์ •์€<NAME>, ๋“ฑ ๋ถ„์‚ฐ์„ฑ, ๋…๋ฆฝ์„ฑ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋…๋ฆฝ์„ฑ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ˆ˜์ง‘๋˜๋Š” ๊ณผ์ •์—์„œ ํ™•์ธํ•˜์—ฌ์•ผ ํ•  ๊ฐ€์ •์ด๋ฏ€๋กœ ์ˆ˜์ง‘๋œ ๋ฐ์ดํ„ฐ๋กœ ๋ถ„์„์ด ์ง„ํ–‰๋œ ์ƒํ™ฉ์—์„œ๋Š” ํ™•์ธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์—ฌ๊ธฐ์„œ๋Š”<NAME>, ๋“ฑ ๋ถ„์‚ฐ์„ฑ์„ ๋‹ค๋ฃจ๊ณ , ์ถ”๊ฐ€์ ์œผ๋กœ ์˜ํ–ฅ์ ์ด๋ผ๋Š” ๊ฒƒ์„ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. plot(Reg) ์ •๊ทœ์„ฑ qqnorm(Reg$residuals) ํšŒ๊ท€๋ถ„์„์—์„œ ์ž”์ฐจ์˜<NAME>์„ ์ง„๋‹จํ•˜๋Š” ์ด์œ ๋Š” ์‹ ๋ขฐ๊ตฌ๊ฐ„ ์ถ”์ •๊ณผ ๊ฐ€์„ค ๊ฒ€์„ ์ •ํ™•ํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์ธก ์ƒ๋‹จ์— ๋ฐฐ์น˜๋˜์–ด ์žˆ๋Š” Normal Q-Q๋ฅผ ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. x์ถ•์€ Theoretical Quantiles์ด๋ฉฐ, y ์ถ•์€ Standardized Residuals์ž…๋‹ˆ๋‹ค. QQ plot์€ ์—ญ ํ™•๋ฅ ์„ ์ด์šฉํ•˜์—ฌ ๋‘ ๋ถ„ํฌ๋ฅผ ๋น„๊ตํ•˜๋Š” plot์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ •๊ทœ๋ถ„ํฌ์™€ ์ž”์ฐจ์˜ ๋ถ„ํฌ๋ฅผ ๋น„๊ตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์„ ์ด ๋Œ€๊ฐ์„ ์„ ๋”ฐ๋ผ๊ฐ€๋Š” ์ผ์ง์„ ์ด๋ผ๋ฉด ๊ทธ๋งŒํผ ์ž”์ฐจ์˜ ๋ถ„ํฌ๋Š” ์ •๊ทœ๋ถ„ํฌ์™€ ๋น„์Šทํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ์„ ์ด ๋Œ€๊ฐ์„ ๊ณผ๋Š” ๋ฉ€์–ด์ง€๋Š” ๊ณก์„  ํ˜•ํƒœ๋ผ๋ฉด ๊ทธ๋งŒํผ ์ž”์ฐจ์˜ ๋ถ„ํฌ๋Š” ์ •๊ทœ๋ถ„ํฌ์™€ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋“ฑ ๋ถ„์‚ฐ์„ฑ ๋“ฑ ๋ถ„์‚ฐ์„ฑ์€ ํšŒ๊ท€๋ถ„์„์—์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ๊ฐ€์ • ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋“ฑ ๋ถ„์‚ฐ์˜ ์ฃผ์ฒด๋Š” ์˜ค์ฐจ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์‹ค์ œ ์˜ค์ฐจ๋ฅผ ์ •๋Ÿ‰ํ™”ํ•  ์ˆ˜ ์—†์œผ๋‹ˆ ์˜ค์ฐจ์˜ ์ถ”์ •์น˜๋กœ์จ ์ž”์ฐจ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ž”์ฐจ๋Š” ์ถ”์ •๋œ ํšŒ๊ท€์„ ๊ณผ ์‹ค์ œ ๊ฐ’์˜ ์ฐจ์ด์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋“ฑ๋ถ„์„ฑ์„ ๋ณด๋Š” ๊ฒƒ์€ ์„ ๊ณผ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ํŒจํ„ด์ด ์—†์ด ์ผ์ •ํ•œ๊ฐ€๋ฅผ ๋ณด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์กฐ๊ธˆ ๊ทน๋‹จ์ ์ธ ์˜ˆ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ขŒ์ธก ์‚ฐ์ ๋„์™€ ์šฐ์ธก ์‚ฐ์ ๋„๊ฐ€ ์žˆ์„ ๋•Œ, ๋‘ ์‚ฐ์ ๋„์— ํšŒ๊ท€์„ ์„ ์ ํ•ฉ์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์ธก์˜ ํšŒ๊ท€์„ ์€ ์ง๊ด€์ ์œผ๋กœ ํŒ๋‹จํ•ด๋„ ํšŒ๊ท€์„ ์— ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ขŒ์ธก ํšŒ๊ท€์„ ์€ ๊ทธ๋Ÿฌ์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ํšŒ๊ท€์„ ๊ณผ ๋ฐ์ดํ„ฐ์˜ ์ฐจ์ด(์ž”์ฐจ)๊ฐ€ x๊ฐ€ ์ปค์ง€๋ฉด์„œ ๊ฐ™์ด ๋Š˜์–ด๋‚˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋“ฑ ๋ถ„์‚ฐ์„ฑ์„ ๋งŒ์กฑํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋•Œ๋Š”, ํšŒ๊ท€์„ ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜ ์„ค๋ช…ํ•œ๋‹ค๊ณ  ๋ณด๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ํšŒ๊ท€๋ถ„์„์—์„œ ๋“ฑ ๋ถ„์‚ฐ์„ฑ์ด ์œ„๋ฐฐ๋˜๋ฉด ํšŒ๊ท€๋ถ„์„์€ ๋ฐ์ดํ„ฐ๋ฅผ ์„ค๋ช…ํ•˜์ง€ ๋ชปํ•œ๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ข‹์€ ํšŒ๊ท€์„ ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ขŒ์ธก์˜ ํšŒ๊ท€์„ ์— ๋Œ€ํ•œ ์ž”์ฐจ์˜ ๋“ฑ ๋ถ„์‚ฐ ์ง„๋‹จ ๊ทธ๋ž˜ํ”„๋กœ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ”Œ๋กฏ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. x์ถ•์€ Fitted value์ž…๋‹ˆ๋‹ค. ์ฆ‰, i๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. y ์ถ•์€ Resiuals( i y ^ )์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ž”์ฐจ๊ฐ€ ์ถ”์ • ๊ฐ’์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์ฆ๊ฐ€ํ•˜๋Š” ํŒจํ„ด์„ ๋ณด์ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ง์”€๋“œ๋ฆฐ ๊ฒƒ์ฒ˜๋Ÿผ ๋“ฑ ๋ถ„์‚ฐ์„ฑ์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋ฐ์ดํ„ฐ๋กœ ๋Œ์•„์™€์„œ ์œ„์—์„œ ๊ทธ๋ฆฐ ์ž”์ฐจ ์ง„๋‹จ ๊ทธ๋ž˜ํ”„๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋“ฑ ๋ถ„์‚ฐ์„ฑ์€ ์ขŒ์ธก ์ƒ, ํ•˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ขŒ์ธก์€ y ์ถ•์ด Residuals, ์šฐ์ธก์€ y ์ถ•์ด ํ‘œ์ค€ํ™” ์ž”์ฐจ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋˜ํ•œ ์„ค๋ช…๋ณ€์ˆ˜์™€ ๋ฐ˜์‘ ๊ฐ’๊ณผ์˜ ๊ด€๊ณ„๋ฅผ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํŒจํ„ด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ƒ๊ด€์—†์ง€๋งŒ Fitted value์— ๋”ฐ๋ผ ์ž”์ฐจ๊ฐ€ ํŠน์ •ํ•œ ํŒจํ„ด์„ ๋ณด์ธ๋‹ค๋ฉด ๋‹ค๋ฅธ ๋ถ„์„์„ ๊ณ ๋ คํ•˜๊ฑฐ๋‚˜ ์„ค๋ช…๋ณ€์ˆ˜์˜ ๋ณ€ํ™˜์„ ์ƒ๊ฐํ•ด ๋ณด์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋…๋ฆฝ์„ฑ ggplot(NULL) + geom_point(aes(x = 1:nrow(Regression), y = Reg$residuals)) + geom_h line(yintercept = 0, linetype = "dashed",col = 'red') + xlab("Index") + ylab("Residuals") + theme_bw() ์ž”์ฐจ์˜ ๋…๋ฆฝ์„ฑ์ด๋ž€, ์ž”์ฐจ๊ฐ€ โ€™์ž๊ธฐ์ƒ๊ด€(Auto correlation)โ€™์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ™•๋ฅ ๋ณ€์ˆ˜ ๋ฐ ํ™•๋ฅ ๋ถ„ํฌ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ, ๋ชจ๋“  ํ™•๋ฅ  ์‹คํ—˜์€ ๋…๋ฆฝ์‹œํ–‰์„ ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด ๋ง์€ ์ „ ์‹œ์ ์—์„œ์˜ ํ™•๋ฅ  ์‹คํ—˜์ด ํ˜„์žฌ์˜ ํ™•๋ฅ  ์‹คํ—˜์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ๋ชปํ•˜๋ฉฐ, ์ง€๊ธˆ์˜ ํ™•๋ฅ  ์‹คํ—˜์€ ๋ฏธ๋ž˜์˜ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ๋ชปํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ โ€™์ž๊ธฐ์ƒ๊ด€โ€™์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ์ฃผ์‹, ๋‚ ์”จ ๋“ฑ์ด ํ•ด๋‹น์ด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋…๋ฆฝ์„ฑ์ด ์œ„๋ฐฐ๊ฐ€ ๋œ๋‹ค๋ฉด ์ด ๋•Œ๋Š” ์‹œ๊ณ„์—ด ๋ถ„์„(Time Series) ๋ฐฉ๋ฒ•๋ก ์„ ์ด์šฉํ•˜์—ฌ ํšŒ๊ท€๋ถ„์„์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ •๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. Durbin-Watson ๊ฒ€์ •์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ, ์ž”์ฐจ๋“ค์„ ์‹œ์  ์ˆœ์„œ๋Œ€๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฐ๋‹ค ๋‹ค์Œ, ํŒจํ„ด์ด ์—†๋‹ค๋ฉด ๋…๋ฆฝ์„ฑ์„ ์ถฉ์กฑํ•œ๋‹ค๊ณ  ํŒ๋‹จํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ํ–ฅ์  ํšŒ๊ท€๋ถ„์„๊ณผ ๊ฐ™์€ ํ†ต๊ณ„ ๋ชจํ˜•์—๋Š” ์˜ํ–ฅ์ ์ด๋ผ๋Š” ๊ฒƒ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์˜ํ–ฅ์ ์€ ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜๋ฉด ํšŒ๊ท€์„ ์„ ์ž๊ธฐ ์ž์‹ ํ•œํ…Œ ๋‹น๊ฒจ์˜ค๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์ฆ‰ ํšŒ๊ท€์„ ์˜ ๋ฌด๊ฒŒ์ค‘์‹ฌ๊ณผ ๋ฉ€๋ฆฌ ์žˆ์–ด, ์ด์— ์˜ํ–ฅ์„ ์ค€๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Cookโ€™s distance๋ผ๋Š” ๊ฐ’์„ ๊ณ„์‚ฐํ•˜์—ฌ ์˜ํ–ฅ์ ์„ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. plot์— ์ฐํžŒ 92, 134, 144๋ฒˆ์งธ ๋ฐ์ดํ„ฐ๊ฐ€ ํšŒ๊ท€์„ ์— ์˜ํ–ฅ์„ ์ฃผ๊ณ  ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ ๋“ค์€ ํšŒ๊ท€์„ ์„ ๊ธฐ์šธ๊ธฐ์— ํฌ๊ฒŒ ์˜ํ–ฅ์„ ์ฃผ์–ด ์˜ˆ์ธก์— ์™œ๊ณก์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ํ–ฅ์ ์„ ๋ฐœ๊ฒฌํ•˜์˜€์„ ๊ฒฝ์šฐ, ๋จผ์ € ์˜ํ–ฅ์ ์— ๋Œ€ํ•œ ํ™•์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์˜ํ–ฅ์ ์€ ์ผ๋ฐ˜์ ์ธ x์™€ ๋จผ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์œผ๋ฉด์„œ ๋†’์€ ์ž”์ฐจ๋ฅผ ๋ณด์ด๋ฏ€๋กœ ์šฐ์„ ์ ์œผ๋กœ x ๊ฐ’์ด ์ •๋‹นํ•œ ๊ฐ’์ธ๊ฐ€๋ฅผ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ธก์ • ์ž์ฒด๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์˜ˆ์ธก์— ๋ชฉ์ ์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. B3. ๋‹ค์ค‘ ํšŒ๊ท€๋ถ„์„(Multiple Regression) 12. ๋‹ค์ค‘ ํšŒ๊ท€๋ถ„์„(Multiple Regression) ๋‹ค์ค‘ ํšŒ๊ท€๋ถ„์„ : ๋‹จ์ˆœ ์„ ํ˜•ํšŒ๊ท€๋ถ„์„์˜ ํ™•์žฅํŒ์œผ๋กœ ์˜ˆ์ธก์ž๊ฐ€ 2๊ฐœ ์ด์ƒ ์“ฐ์ด๋Š” ๊ฒฝ์šฐ ๋‹ค์ค‘ ํšŒ๊ท€๋ถ„์„์€ ์˜ˆ์ธก์ž๋ฅผ 2๊ฐœ ์ด์ƒ ์“ฐ๋Š” ๊ฒฝ์šฐ๋กœ, ํšŒ๊ท€๋ถ„์„๊ณผ ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ ํ‘œํ˜„์€ ํ–‰๋ ฌ์‹์„ ์ด์šฉํ•ด ํ‘œํ˜„์„ ํ•˜๋Š”๋ฐ, ์ด ์ฑ…์˜ ์ทจ์ง€์™€๋Š” ๋งž์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋‹ค์ค‘ ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•  ๋•Œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ๋“ค์— ๋Œ€ํ•ด ๋‹ค๋ฃจ๋ฉด์„œ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. i = 0 b x i b x i ํšŒ๊ท€์‹์ด ์œ„ ์‹์ฒ˜๋Ÿผ ๊ตฌํ•ด์ ธ ์žˆ์„ ๋•Œ, ํšŒ๊ท€์‹์˜ ํ•ด์„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 1 ๊ฐ€ 1 ๋‹จ์œ„ ์ฆ๊ฐ€ํ•˜๋ฉด i๋Š” 1 ๋งŒํผ ๋ณ€ํ•œ๋‹ค.(๋‹จ, 2๋Š” ๊ณ ์ •) 2 ๊ฐ€ 1 ๋‹จ์œ„ ์ฆ๊ฐ€ํ•˜๋ฉด i๋Š” 2 ๋งŒํผ ๋ณ€ํ•œ๋‹ค.(๋‹จ, 1 ์€ ๊ณ ์ •) ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€ ์„œ๋กœ ๋‹ค๋ฅธ ์˜ˆ์ธก ์ž๋Š” ์ƒ๊ด€์„ฑ์ด ์ ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ 1 x์˜ ์ƒ๊ด€์„ฑ์ด ๋†’๋‹ค๋ฉด, 1 ์ด ์ฆ๊ฐ€ํ•˜์˜€์„ ๋•Œ, 2 ๋Š” ๊ณ ์ •๋˜์ง€ ๋ชปํ•˜๊ณ  ํ•จ๊ป˜ ์ฆ๊ฐ€ํ•ด๋ฒ„๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ด๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์„ ๋‹ค์ค‘๊ณต ์„ ์„ฑ(Multicolinearity)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํšŒ๊ท€์‹์— ๋‹ค์ค‘๊ณต ์„ ์„ฑ์ด ์กด์žฌํ•  ๊ฒฝ์šฐ, ํšŒ๊ท€์‹์˜ ๋ถ„์‚ฐ์ด ํŒฝ์ฐฝ์„ ํ•˜๊ฒŒ ๋˜๋ฉฐ, 1 b์— ๋Œ€ํ•œ ์ถ”์ •์ด ๋ถˆํ™•์‹คํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋‹ค์ค‘๊ณต ์„ ์„ฑ์ด ์กด์žฌํ•  ๊ฒฝ์šฐ, ํšŒ๊ท€์‹์— ํˆฌ์ž…๋˜๋Š” ์˜ˆ์ธก์ž๋ฅผ ๋‹ค์‹œ ์กฐ์ •ํ•˜์—ฌ ํšŒ๊ท€์‹์„ ๊ตฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ํ›„์— ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์—์„œ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. x1 = runif(n = 100, min = -10, max = 10) x2 = 0.7*x1 + rnorm(n = 100, mean = 0, sd = 1) y = 1.3*x1 + rnorm(n = 100, mean = 6, sd = 3) DF = data.frame( x1 = x1, x2 = x2, y = y ) library(GGally) ggpairs(DF) ์œ„ ๊ทธ๋ฆผ์„ ๋ณด์‹œ๋ฉด x1x1, x2x2์™€์˜ ์ƒ๊ด€๊ด€๊ณ„๊ฐ€ ๋งค์šฐ ๋†’์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ, ํšŒ๊ท€๋ถ„์„์„ ๊ตฌํ–ˆ์„ ๋•Œ, ๋‹ค์ค‘๊ณต ์„ ์„ฑ์ด ๋งค์šฐ ์˜์‹ฌ๋˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ค‘ ํšŒ๊ท€๋ถ„์„ M_Reg = lm(y ~ x1 + x2) summary(M_Reg) Call: lm(formula = y ~ x1 + x2) Residuals: Min 1Q Median 3Q Max -7.6871 -1.8648 0.2094 1.6262 6.5584 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 6.2825 0.2965 21.192 < 2e-16 *** x1 1.1548 0.2202 5.244 9.18e-07 *** x2 0.2368 0.3083 0.768 0.444 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 2.937 on 97 degrees of freedom Multiple R-squared: 0.8793, Adjusted R-squared: 0.8768 F-statistic: 353.4 on 2 and 97 DF, p-value: < 2.2e-16 ์–ธ๋œป ๋ณด๊ธฐ์—๋Š” ํšŒ๊ท€๋ถ„์„์— ํฐ ๋ฌธ์ œ๋Š” ์—†์–ด ๋ณด์ž…๋‹ˆ๋‹ค. ๋‹ค์ค‘ํšŒ๊ท€๋ถ„์„์—์„œ๋Š” x1x1, x2x2์˜ ๊ธฐ์šธ๊ธฐ b1b1, b2b2์— ๋Œ€ํ•œ ๊ฐ€์„ค ๊ฒ€์ •์ด ์ง„ํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. x1x1์˜ ํšŒ๊ท€๊ณ„์ˆ˜ b1b1์€ ์œ ์˜ํ•œ ๊ฒƒ์œผ๋กœ ๋‚˜์™”์ง€๋งŒ, x2x2์˜ ํšŒ๊ท€๊ณ„์ˆ˜ b2b2๋Š” ์œ ์˜ํ•˜์ง€ ์•Š์€ ๊ฒƒ์œผ๋กœ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ๋‹ค์ค‘๊ณต ์„ ์„ฑ ์ง„๋‹จ library(car) vif(M_Reg) x1 x2 19.66709 19.66709 ๋‹ค์ค‘๊ณต ์„ ์„ฑ์€ ๋ถ„์‚ฐ ํŒฝ์ฐฝ์ง€์ˆ˜(VIF)๋ฅผ ์ธก์ •ํ•จ์œผ๋กœ์จ ํŒ๋‹จ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ 5๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋‹ค์ค‘๊ณต ์„ ์„ฑ์ด ์—†๋‹ค๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ๋‹ค์ค‘๊ณต ์„ ์„ฑ์ด ๋งค์šฐ ๋†’์€ ์ƒํ™ฉ์ด๋ฏ€๋กœ ๋‹ค์ค‘๊ณต ์„ ์„ฑ์ด ์กด์žฌํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. B4. ๋‹คํ•ญ ํšŒ๊ท€๋ถ„์„(Polynomial Regression) 13. ๋‹คํ•ญ ํšŒ๊ท€๋ถ„์„(Polynomial Regression) ๋‹คํ•ญ ํšŒ๊ท€๋ถ„์„ : ์˜ˆ์ธก์ž๋“ค์ด 1์ฐจ ํ•ญ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ฒƒ์ด ์•„๋‹Œ, 2์ฐจ ํ•ญ, 3์ฐจ ํ•ญ ๋“ฑ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ํšŒ๊ท€์‹ ^ b + 1 i b x 2 โ‹ฏ b x p \ ๋‹คํ•ญ ํšŒ๊ท€๋ถ„์„์€ ์œ„ ์‹์ฒ˜๋Ÿผ ๊ตฌ์„ฑ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹คํ•ญํšŒ๊ท€๋ถ„์„์—์„œ๋Š” ๋งค์šฐ ์ค‘์š”ํ•œ ๊ฐœ๋…์ด ํ•˜๋‚˜ ๋”ฐ๋ผ์˜ค๋Š”๋ฐ, ์ด๋ฅผ ํ™•์ธํ•˜๊ณ  ๋‹คํ•ญ ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ถ„์‚ฐ-ํŽธ์ฐจ์˜ Trade off ๊ด€๊ณ„ Trade off : ๋‘ ๊ฐœ์˜ ๋ชฉํ‘œ ์ค‘์—์„œ ํ•˜๋‚˜๋ฅผ ๋‹ฌ์„ฑํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋‹ค๋ฅธ ๋ชฉํ‘œ๊ฐ€ ํฌ์ƒ๋˜์–ด์•ผ ํ•˜๋Š” ๊ด€๊ณ„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ํ•™์Šต์—์„œ ์˜ˆ์ธก ๋ชจํ˜•์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ํ•ญ์ƒ Trade off ๊ด€๊ณ„๋ฅผ ์ƒ๊ฐํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ํ†ต๊ณ„ํ•™์—์„œ๋Š” ๋ชจํ˜•์˜ Target Variable(์ข…์† ๋ณ€์ˆ˜)์ด ์—ฐ์†ํ˜•(Continuous)์ผ ๋•Œ๋Š” MSE ์™€ Bias์— ์ฃผ๋ชฉํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ Target Variable์ด ๋ฒ”์ฃผํ˜•(Categorical)์ผ ๊ฒฝ์šฐ์—๋Š” ๋ชจํ˜•์˜ Error Rate์— ์ฃผ๋ชฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ชจํ˜•์˜ ์ •ํ™•์„ฑ์€ MSE ํ˜น์€ Bias๊ฐ€ ์–ผ๋งˆ๋‚˜ ์ž‘์€์ง€์— ๋”ฐ๋ผ ๊ฒฐ์ •๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. MSE(Mean Squared Error) ์•ž์„œ ๋‹จ์ˆœ ์„ ํ˜•ํšŒ๊ท€์—์„œ MSE๋ฅผ ๋‹ค๋ฃจ์—ˆ์ง€๋งŒ, ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ณต์Šตํ•˜๋ฉด์„œ ์žฌ์ฐจ ๋‹ค๋ฃจ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. S๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„ ๊ทธ๋ฆผ์˜ ์˜๋ฏธ๋ฅผ ์ œ๋Œ€๋กœ ์ˆ™์ง€ํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ ๊ด€์ธก ๊ฐ’ i ์‹ค ๊ด€ ๊ฐ’ ์˜ˆ์ธก๊ฐ’ i = ์ธก ํ‰๊ท  โ€• ํ‰ ์—ฌ๊ธฐ์„œ ์˜ˆ์ธก๊ฐ’ i ์€ ์ถ”์ •๋œ ํšŒ๊ท€์‹ i = 0 ฮฒ X์œผ๋กœ๋ถ€ํ„ฐ ์ถ”์ •๋œ ์˜ˆ์ธก ๊ฐ’์ž…๋‹ˆ๋‹ค. ํ‰๊ท ๊ฐ’ โ€• Y = n ( i ) ํ‰๊ท  ์‚ฐ์ˆ ์‹์œผ๋กœ๋ถ€ํ„ฐ ๊ณ„์‚ฐ๋œ ํ‘œ๋ณธํ‰๊ท ์ž…๋‹ˆ๋‹ค. ๋ณด๋ผ์ƒ‰ ๊ฐ„๊ฒฉ์— ํ•ด๋‹น๋˜๋Š” i โˆ’ โ€• ๋Š” ๊ฐ„๊ฒฉ์˜ ์ฐจ์ด๋ฅผ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—( 0 ฮฒ X โˆ’ n ( i ) ) ์ถ”์ •๋œ ํšŒ๊ท€์‹์ด ์„ค๋ช…์ด ๊ฐ€๋Šฅํ•œ ์˜์—ญ์ด ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ดˆ๋ก์ƒ‰ ๊ฐ„๊ฒฉ์— ํ•ด๋‹น๋˜๋Š” i Y ^ ๋Š” ์‹ค์ œ๋กœ ๊ด€์ธก๋œ i ๊ฐ’์ด ์™œ ์ €๊ธฐ์— ์ฐํ˜”๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋Š” ์„ค๋ช…์„ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ด€๊ณ„๋กœ ํ•ด๋‹น ์˜์—ญ์„ ์„ค๋ช…์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ์˜์—ญ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ชจํ˜•์€ ์„ค๋ช…๋ ฅ์ด ๋†’์œผ๋ฉฐ (ํ˜น์€ ์„ค๋ช… ๋ชปํ•˜๋Š” ์˜์—ญ์ด ์ ์€) ์˜ˆ์ธก์ด ์ž˜ ๋˜๋Š” ๋ชจํ˜•์ด ์ข‹์€ ๋ชจํ˜•์ž…๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๋ชจํ˜•์˜ ๊ฒฐํ•จ์€ ์„ค๋ช…์„ ํ•˜์ง€ ๋ชปํ•˜๋Š” i Y ^ ์€ ์˜ค์ฐจ๋กœ ๊ณ„์‚ฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ž”์ฐจ(Residuals, ํ˜น์€ Error)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ถ”์ •๋œ ํšŒ๊ท€์‹ i = 0 ฮฒ X ์ด ๋ณ€์ˆ˜๋“ค ๊ฐ„์˜ ์ธ๊ณผ๊ด€๊ณ„๋ฅผ ์ œ๋Œ€๋กœ ์„ค๋ช…ํ•˜๋Š”์ง€ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์ž”์ฐจ์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ž”์ฐจ๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ์–‘์ˆ˜๊ฐ€ ๋  ์ˆ˜๋„, ์Œ์ˆ˜๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋ถ€ํ˜ธ ๊ฐ„ ๊ณ„์‚ฐ์œผ๋กœ ์ž”์ฐจ์˜ ํ•ฉ์ด ์ƒ์‡„๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์ž”์ฐจ๋ฅผ ์ œ๊ณฑํ•˜์—ฌ ํ•ฉ์„ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์˜ค์ฐจ์˜ ์ œ๊ณฑํ•ฉ(Sum Squred Error, S)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ณ„์‚ฐ๋œ S๋ฅผ ๋ณด์ •ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ S๋ฅผ ์˜ค์ฐจ์˜ ์ž์œ ๋„( f)๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๊ณ„์‚ฐ๋œ ๊ฐ’์„ ์˜ค์ฐจ ํ‰๊ท  ์ œ๊ณฑํ•ฉ(Mean Squared Error, S)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. S = ( i Y ^ ) M E 1 f ฮฃ ( i Y ^ ) SSE๋ฅผ ์˜ค์ฐจ์˜ ์ž์œ ๋„๋กœ ๋‚˜๋ˆ„์–ด์ฃผ๋Š” ์ด์œ  ์ œ๊ณฑ ๋œ ๊ฐ’์€ ํ•ญ์ƒ ์–‘์ˆ˜์ž…๋‹ˆ๋‹ค ์–‘์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•˜๊ฒŒ ๋˜๋ฉด, ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์„์ˆ˜๋ก ๊ฐ’์€ ์ปค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ์˜๋ฏธ๋Š” SSE ์ž์ฒด๊ฐ€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์„์ˆ˜๋ก ๋‹จ์ˆœํžˆ ์ปค์ง€๋Š” ์˜๋ฏธ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ •๋ง ์˜ค์ฐจ๊ฐ€ ๋†’์€๊ฐ€?์— ๋Œ€ํ•œ ํ‰๊ฐ€ ๊ธฐ์ค€์ด ์ž˜๋ชป ํ•ด์„๋  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ž์œ ๋„๋กœ ๋‚˜๋ˆ”์œผ๋กœ์จ ํ‰๊ท ์ด ๊ณ„์‚ฐ๋˜๊ณ , ๋ณด์ •๋œ ํ‰๊ท ์˜ค์ฐจ๋ฅผ ๋ชจํ˜•์˜ Error ์ˆ˜์ค€์œผ๋กœ ํŒ๋‹จํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๊ณ„์‚ฐ์œผ๋กœ ํšŒ๊ท€์‹์ด ์„ค๋ช… ๊ฐ€๋Šฅํ•œ ์˜์—ญ์ธ, i โˆ’ โ€• ์€ ๊ฐ๊ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์‚ฐ์ด ๋ฉ๋‹ˆ๋‹ค. S = ( i โˆ’ โ€• ) M R 1 f ฮฃ ( i โˆ’ โ€• ) ๊ทธ๋ ‡๋‹ค๋ฉด ์ถ”์ •๋˜๋Š” ํšŒ๊ท€์‹์ด ๋ฐ์ดํ„ฐ์˜ ์ธ๊ณผ๊ด€๊ณ„๋ฅผ ์–ผ๋งˆ๋‚˜ ์ž˜ ์„ค๋ช…ํ•˜๋Š”์ง€ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ์„ค๋ช…์„ ํ•˜์ง€ ๋ชปํ•˜๋Š” ์˜์—ญ ๋Œ€๋น„, ์„ค๋ช…์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ์„ ๋น„๊ตํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. v l e M R S = d R ( i โˆ’ โ€• ) 1 f ฮฃ ( i Y ^ ) M R S๋Š” ๋‘ ์ง‘๋‹จ์˜ ๋ถ„์‚ฐ์„ ๋น„๊ตํ•˜๋Š” F ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ถ”์ •๋œ ํšŒ๊ท€์‹์˜ ์„ค๋ช…ํ•˜๋Š” ์˜์—ญ์ด ์„ค๋ช…ํ•˜์ง€ ๋ชปํ•˜๋Š” ์˜์—ญ์— ๋น„ํ•ด ์–ผ๋งˆ๋‚˜ ํฐ์ง€ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰(Test Statistics) v l e ๋Š” ๊ฐ’์ด ํด์ˆ˜๋ก ํšŒ๊ท€์‹์˜ ๊ท€๋ฌด๊ฐ€์„ค(ํšŒ๊ท€์‹์˜ ๊ธฐ์šธ๊ธฐ๊ฐ€์ด๋‹ค 0 ํšŒ ์‹ ๊ธฐ ๊ธฐ 0 ๋‹ค )๋ฅผ ๊ธฐ๊ฐํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์ปค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด v l e ๊ฐ’์ด ์ปค์ง€๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. S ์ด ์ฆ๊ฐ€ S ๊ฐ€ ๊ฐ์†Œ ๋ถ„์„ ๋ชจํ˜•์˜ ์„ฑ๋Šฅ ํ‰๊ฐ€๋ฅผ S๋กœ ํ•˜๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค. S ๊ฐ€ ์ž‘์€ ๋ชจํ˜•์ผ์ˆ˜๋ก ํšŒ๊ท€์‹์˜ ์˜ค์ฐจ๊ฐ€ ์ค„๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋งŒํผ ํ˜„์ƒ์„ ์ž˜ ์„ค๋ช…ํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Variation & Bias ํšŒ๊ท€์‹์œผ๋กœ ์ถ”์ •๋œ i๋Š” ์–ผํ• ๋ณด๋ฉด ๋‹จ์ผ ๊ฐ’์ธ ์  ์ถ”์ •(Point Estimation)์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ชจ๋“  ํ†ต๊ณ„๋ถ„์„ ๋ชจํ˜•์€ ๊ตฌ๊ฐ„ ์ถ”์ •(Interval Estimation)์ž…๋‹ˆ๋‹ค. ๊ตฌ๊ฐ„ ์ถ”์ •์ด๋ž€ ์†Œ๋ฆฌ๋Š” ์ถ”์ • ๊ฐ’์— ๋Œ€ํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„์„ ๊ณ„์‚ฐํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์‹ ๋ขฐ๊ตฌ๊ฐ„์˜ ์˜๋ฏธ๋Š” ๋˜‘๊ฐ™์€ ๊ฐ’์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ถ”์ • ๊ฐ’ ^ ฮฒ + 1์˜ ๊ฐ’์ด [ ^ ฮฑ y + ] ์˜ ๋ฒ”์œ„์— ์†ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ถ„์‚ฐ์ด ํฌ๋‹ค๋ฉด, ์ด ์‹ ๋ขฐ๊ตฌ๊ฐ„์˜ ๊ธธ์ด๋Š” ๊ธธ์–ด์ง€๊ฒŒ ๋˜๊ณ , ์ถ”์ •์˜ ์‹ ๋ขฐ์„ฑ์ด ๋–จ์–ด์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ํŽธ์˜(Bias)๋Š” ์ถ”์ •๋œ ๊ฐ’์ด ๋ชจ์ง‘๋‹จ์˜ ํŠน์„ฑ, ์ฆ‰ ๋ชจ์ˆ˜๋ฅผ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์„ ๋ณด์‹œ๋ฉด ๋ถ„์‚ฐ๊ณผ ํŽธํ–ฅ์ด ํฌ๊ณ  ์ž‘์„ ๋•Œ์— ๋”ฐ๋ผ ๋ชจํ˜•์˜ ์ •ํ™•์„ฑ์ด ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ์ˆ˜์˜ True Value๊ฐ€ ์› ์ •์ค‘์•™์— ์žˆ๋‹ค๊ณ  ํ•˜์˜€์„ ๋•Œ, Variance ๊ฐ€ ํฌ๋‹ค๋Š” ๊ฒƒ์€ ์ถ”์ • ๊ฐ’์˜ ๋ฒ”์œ„๊ฐ€ ๋„“์€ ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ณ , Bias๊ฐ€ ํฌ๋‹ค๋Š” ๊ฒƒ์€ ์˜์ ์กฐ์ค€ ์‚ฌ๊ฒฉ ํ›ˆ๋ จ ๋•Œ ํƒ„์ง‘๊ตฐ์€ ์ƒ๊ฒผ์ง€๋งŒ ์˜์ ์ด ์ž˜๋ชป ์žกํ˜”๋‹ค ์™€ ๋น„์Šทํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์„ ํ˜• & ๋น„์„ ํ˜• Modeling Linear Regression(์„ ํ˜• ํšŒ๊ท€๋ถ„์„)๊ณผ Non - Linear Regression(๋น„์„ ํ˜• ํšŒ๊ท€๋ถ„์„)์„ ์ž ๊น ๋‹ค๋ฃจ๊ณ  ๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. library(ggplot2) ggplot(Regression) + geom_point(aes(x = X, y = y),col = 'royalblue',alpha = 0.4) + geom_smooth(aes(x = X, y = y),col = 'red') + theme_bw() + xlab("") + ylab("") ggplot(Regression) + geom_point(aes(x = X, y = y2),col = 'royalblue',alpha = 0.4) + geom_smooth(aes(x = X, y = y2),col = 'red') + theme_bw() + xlab("") + ylab("") ์‚ฌ๋žŒ๋“ค์ด ํšŒ๊ท€๋ถ„์„์„ ๋Œ๋ฆด ๋•Œ, ๊ฐ€์žฅ ์‹ค์ˆ˜ํ•˜๋Š” ๋ถ€๋ถ„์€ ๋‹จ์ˆœํ•˜๊ฒŒ ๋ณ€์ˆ˜๋“ค ๊ฐ„์˜ ์ƒ๊ด€๊ด€๊ณ„๋งŒ์„ ํŒŒ์•…ํ•ด์„œ ๋ถ„์„ํ•˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ƒ๊ด€๊ด€๊ณ„๋Š” ๋‘ ๋ณ€์ˆ˜์˜ ๊ด€๊ณ„๊ฐ€ ์„ ํ˜•์„ฑ์„ ๋„๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ผ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋น„์„ ํ˜• ๊ด€๊ณ„์— ์žˆ์„ ๊ฒฝ์šฐ, ์ƒ๊ด€๊ด€๊ณ„๋Š” ๋‚ฎ๊ฒŒ ์žกํž ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ƒ๊ด€๊ณ„์ˆ˜๊ฐ€ ๋‚ฎ๊ฒŒ ์žกํžŒ๋‹ค๊ณ  ํ•ด์„œ ์ด ๋‘ ๋ณ€์ˆ˜ ๊ฐ„์— ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋น„์„ ํ˜•์œผ๋กœ ํšŒ๊ท€์‹์„ ์žก์œผ๋ฉด ์ถฉ๋ถ„ํžˆ ๊ด€๊ณ„๋ฅผ ์„ค๋ช…ํ•  ์ˆ˜๊ฐ€ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ggplot(Regression) + geom_point(aes(x = X, y = y3),col = 'royalblue',alpha = 0.4) + geom_smooth(aes(x = X, y = y3),col = 'red') + theme_bw() + xlab("") + ylab("") ํŠนํžˆ ์ด๋Ÿฐ ๊ฒฝ์šฐ, ๋ฐ์ดํ„ฐ์˜ ์ƒ๊ด€๊ด€๊ณ„ ์ˆ˜๊ฐ€ ๋งค์šฐ ๋‚ฎ๊ฒŒ ๊ณ„์‚ฐ์ด ๋ฉ๋‹ˆ๋‹ค. ์ƒ๊ด€๊ณ„์ˆ˜๋งŒ ๋ณด๋ฉด ๋งค์šฐ ๋‚ฎ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋ธ๋ง ํ•  ์ƒ๊ฐ๋ถ€ํ„ฐ ์•ˆ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ถ„์„ ๋ชจํ˜•์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๋น„์„ ํ˜• ๊ด€๊ณ„๋“ค๋กœ ๊ด€๊ณ„์‹์„ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ ํ˜• ํšŒ๊ท€๋ถ„์„ ๋‘ ๋ณ€์ˆ˜์˜ ๊ด€๊ณ„๊ฐ€ ์„ ํ˜•์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ํšŒ๊ท€๋ถ„์„์„ ์ถ”์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. LINEAR = lm(y ~ X, data = Regression) summary(LINEAR) Call: lm(formula = y ~ X, data = Regression) Residuals: Min 1Q Median 3Q Max -9.5537 -5.7116 0.2738 5.0961 10.2835 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.9839 0.9975 1.989 0.0486 * X 10.0924 0.1620 62.308 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 6.085 on 148 degrees of freedom Multiple R-squared: 0.9633, Adjusted R-squared: 0.963 F-statistic: 3882 on 1 and 148 DF, p-value: < 2.2e-16 ggplot(Regression) + geom_smooth(aes(x = X, y = predict(LINEAR, newdata = Regression)),col = "red", method = 'lm') + geom_point(aes(x = X, y = y),col = 'royalblue') + ylab("") + xlab("") + ggtitle("Linear Regression") + theme_bw() ์ผ๋ฐ˜์ ์ธ ์„ ํ˜•ํšŒ๊ท€๋ถ„์„, ์ฆ‰ ์„ ํ˜•์„ ์™„๋ฒฝํ•˜๊ฒŒ ๋„๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜ ๊ฐ„์˜ ๊ด€๊ณ„๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ ํ˜•์œผ๋กœ ์ ํ•ฉ์‹œํ‚ค๋ฉด ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. Polynomial Regression ๋ณ€์ˆ˜ ๊ฐ„์˜ ๊ด€๊ณ„๊ฐ€ y=x2y=x2 ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด 2์ฐจ ํ•ญ ํšŒ๊ท€๋ถ„์„(๋‹คํ•ญ ํšŒ๊ท€๋ถ„์„)์„ ์ ์šฉ์‹œ์ผœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # ์ œ๊ณฑ ๊ผด ๊ด€๊ณ„๋ฅผ ์„ ํ˜•์œผ๋กœ ์ ํ•ฉ NonLinear = lm(y2 ~ X, data = Regression) summary(NonLinear) Call: lm(formula = y2 ~ X, data = Regression) Residuals: Min 1Q Median 3Q Max -154.19 -84.79 -46.28 46.35 446.95 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -208.872 21.225 -9.841 <2e-16 *** X 103.788 3.447 30.113 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 129.5 on 148 degrees of freedom Multiple R-squared: 0.8597, Adjusted R-squared: 0.8587 F-statistic: 906.8 on 1 and 148 DF, p-value: < 2.2e-16 2์ฐจ ํ•ญ์˜ ๊ด€๊ณ„๋ฅผ ์„ ํ˜•์œผ๋กœ ์ ํ•ฉํ•˜์˜€์„ ๋•Œ์˜ ์„ค๋ช…๋ ฅ์€ 85 ~ 86%๊ฐ€ ๋‚˜์˜จ ๊ฒƒ์„ ์•Œ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ๋‹คํ•ญ ํšŒ๊ท€๋ถ„์„(2์ฐจ ํ•ญ)์„ ์ ์šฉ์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. NonLinear2 = lm(y2 ~ poly(X, 2), data = Regression) summary(NonLinear2) Call: lm(formula = y2 ~ poly(X, 2), data = Regression) Residuals: Min 1Q Median 3Q Max -65.180 -25.879 5.213 29.693 39.436 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 345.341 2.582 133.74 <2e-16 *** poly(X, 2) 1 3899.149 31.625 123.29 <2e-16 *** poly(X, 2) 2 1527.866 31.625 48.31 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 31.63 on 147 degrees of freedom Multiple R-squared: 0.9917, Adjusted R-squared: 0.9916 F-statistic: 8767 on 2 and 147 DF, p-value: < 2.2e-16 ํšŒ๊ท€์‹์„ i = 0 ฮฒ x + 2 i ํ˜•ํƒœ๋กœ ์ ํ•ฉํ•œ ๊ฒฐ๊ณผ ์„ค๋ช…๋ ฅ์€ 99%๋กœ ์ƒ์Šนํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ggplot(Regression) + geom_smooth(aes(x = X, y = predict(NonLinear2, newdata = Regression)),col = "red") + geom_point(aes(x = X, y = y2),col = 'royalblue') + ylab("") + xlab("") + ggtitle("Polynomial Regression") + theme_bw() ์œ ์—ฐ์„ฑ์ด ์žˆ๋Š” ํšŒ๊ท€๋ถ„์„ ๋‹ค์Œ ํšŒ๊ท€๋ถ„์„์€ = i ( ) ๊ผด์„ ๊ฐ€์ง€๋Š” ๋‘ ๋ณ€์ˆ˜ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ํšŒ๊ท€์‹์œผ๋กœ ์ถ”์ •ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์›Œ๋‚™ ํ˜•ํƒœ๊ฐ€ ๊ดด์ดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ช‡ ์ฐจ ํ•ญ์„ ์ ํ•ฉ์‹œ์ผœ์•ผ ํ• ์ง€ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌํ•˜๋‹ˆ ๋ณ€์ˆ˜ ํ•ญ์˜ ์ฐจ์ˆ˜(Degree of Polynomial)์„ 2 ~ 10๊นŒ์ง€ ์ฃผ๊ณ  Testing์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. # Train Set & Test Set ํ˜•์„ฑ TRAIN = Regression[1:100, ] TEST = Regression[101:150, ] # ์ €์žฅ ๊ณต๊ฐ„ ์ƒ์„ฑ DEGREE = c() TEST_MSE = c() TRAIN_MSE = c() Adj_R = c() TEST_VAR = c() TRAIN_VAR = c() # ์ ํ•ฉ ๋ชจ๋ธ ์ฐพ๊ธฐ for( degree in 2:10){ FLEXIBLE_MODEL = lm(y3 ~ poly(X, degree),data = TRAIN) ## Summary Save SUMMARY = summary(FLEXIBLE_MODEL) ANOVA = anova(FLEXIBLE_MODEL) DEGREE = c(DEGREE, DEGREE) ## R_SQUARE Adj_R = c(Adj_R, SUMMARY$Adj.R.squared) ## Train Set TRAIN_MSE = c(TRAIN_MSE, ANOVA$`Mean Sq`[2]) TRAIN_VAR = c(TRAIN_VAR, var(FLEXIBLE_MODEL$fitted.values)) ## Test Set Pred = predict(FLEXIBLE_MODEL, newdata = TEST) TEST_RESIDUALS = (Pred - TEST$y3) TEST_MSE_VALUE = sum(TEST_RESIDUALS^2)/(nrow(TEST)) TEST_MSE = c(TEST_MSE, TEST_MSE_VALUE) TEST_VAR = c(TEST_VAR, var(Pred)) } ## Test ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ์ƒ์„ฑ F_DATA = data.frame( DEGREE = DEGREE, Adj_R = Adj_R, TRAIN_MSE = TRAIN_MSE, TRAIN_VAR = TRAIN_VAR, TEST_MSE = TEST_MSE, TEST_VAR = TEST_VAR ) ํ•ญ์ฐจ๋ฅผ 2์ฐจ ํ•ญ๋ถ€ํ„ฐ 10์ฐจ ํ•ญ๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ ์ถ”์ •ํ•ด ๋ณธ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. library(dplyr) library(reshape) ggplot(TRAIN) + geom_point(aes(x= X, y = y3) , col = 'royalblue', alpha = 0.8) + geom_smooth(aes(x = X, y = predict(FLEXIBLE_MODEL, newdata = TRAIN)),col = 'red') + xlab("") + ylab("") + ggtitle("Flexible Regression") + theme_bw() ggplot(F_DATA) + geom_point(aes(x = DEGREE, y = Adj_R * 100)) + geom_line(aes(x = DEGREE, y = Adj_R * 100)) + geom_text(aes(x = DEGREE, y = Adj_R * 100 + 5, label = paste(round(Adj_R*100,2),"%",sep="")),size = 3)+ scale_x_continuous(breaks = seq(2,10, by = 1)) + ylab("Adj_R2") + theme_bw() F_DATA %>% select(DEGREE, TRAIN_MSE, TEST_MSE) %>% melt(id.vars = c("DEGREE")) %>% ggplot() + geom_point(aes(x = DEGREE, y = value, col = variable)) + geom_line(aes(x = DEGREE, y = value, col = variable)) + labs(col = "") + theme_bw() + theme(legend.position = "bottom") + xlab("Degree") + ylab("MSE") F_DATA %>% select(DEGREE, TRAIN_VAR, TEST_VAR) %>% melt(id.vars = c("DEGREE")) %>% ggplot() + geom_point(aes(x = DEGREE, y = value, col = variable)) + geom_line(aes(x = DEGREE, y = value, col = variable)) + labs(col = "") + theme_bw() + theme(legend.position = "bottom") + xlab("Degree") + ylab("Variation") 2 ๋Š” 6์ฐจ ํ•ญ๋ถ€ํ„ฐ ๊ธ‰๊ฒฉํ•˜๊ฒŒ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 6์ฐจ ํ•ญ์€ ๋˜์–ด์•ผ = i ( ) ํ˜•ํƒœ์˜ ๊ด€๊ณ„๋ฅผ ์ž˜ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ํŽธ์ด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. S๋Š” Train Set๊ณผ Test Set์— ๋”ฐ๋ผ ์ถ”์„ธ๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ฐจ์ˆ˜๊ฐ€ ๋†’์•„์งˆ์ˆ˜๋ก Train Set์˜ S๋Š” ๊ฐ์†Œํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Test Set์˜ MSE๋Š” ๊ฐ์†Œํ•˜๋‹ค๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” Train Set์€ ๊ธฐ๊ฐ€ ๋ง‰ํžˆ๊ฒŒ ์ž˜ ๋งž์ถ”์ง€๋งŒ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์ธ Test Set์€ ๋งž์ถ”์ง€ ๋ชปํ•˜๋Š” OverFitting์ด ๋ฐœ์ƒํ•˜์˜€๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. a i n e ๋Š” ํ•ญ์ฐจ๊ฐ€ ์˜ฌ๋ผ๊ฐˆ์ˆ˜๋ก ๋Œ€์ฒด๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ถ”์„ธ์— ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ณ ์ฐจ ํ•ญ์˜ ํšŒ๊ท€ ๋ชจํ˜•์˜ ๋‹จ์ ์ด ์ œ๋Œ€๋กœ ๋“œ๋Ÿฌ๋‚ฉ๋‹ˆ๋‹ค. ๋ถ„์„ ๋ชจํ˜•์ด ์œ ์—ฐํ• ์ˆ˜๋ก(ํ•ญ์ฐจ๊ฐ€ ๋†’์„์ˆ˜๋ก) ํšŒ๊ท€์ถ”์ •๊ฐ’์˜ ๋ถ„์‚ฐ์€ ๋†’๊ฒŒ ๋›ฐ๊ธฐ ๋งˆ๋ จ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํšŒ๊ท€์‹์— ์˜ํ•œ ์ถ”์ • ๊ฐ’์— ๋Œ€ํ•œ ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด ๊ธธ์–ด์ง„๋‹ค๋Š” ์˜๋ฏธ์ด๋ฉฐ, ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์‹ ๋ขฐ๋„๊ฐ€ ๋–จ์–ด์ง„๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€ ๋น„์„ ํ˜• ํšŒ๊ท€๋ถ„์„์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ChB3. ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋ถ„์„ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋ถ„์„์€ ๋ณ€์ˆ˜๋“ค์ด ์ด์‚ฐํ˜• ๋ณ€์ˆ˜์ผ ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ถ„์„์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‘ ์ œํ’ˆ ๊ฐ„์˜ ์„ ํ˜ธ๋„๊ฐ€ ์„ฑ๋ณ„์— ๋”ฐ๋ผ ์—ฐ๊ด€์ด ์žˆ๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ, ๊ฐ ์ง‘๋‹จ ๊ฐ„์˜ ๋น„์œจ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๊ฒฝ์šฐ ๋“ฑ์— ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜๋ฅผ ๋‹ค๋ฃฐ ๋•Œ์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ทธ ๋นˆ๋„๋ฅผ ์„ธ์„œ ํ‘œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‘ ๋ณ€์ˆ˜์˜ ๋ฒ”์ฃผ๊ฐ€ ๊ต์ฐจ๋˜์–ด ์žˆ๋‹ค๋ฉด ์ด ํ‘œ๋ฅผ ๋ถ„ํ• ํ‘œ(contingency table)๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์‚ฌ์‹ค ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜๋ฅผ ์š”์•ฝํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด๋Ÿฌํ•œ ๋ถ„ํ• ํ‘œ ๋ง๊ณ ๋Š” ์ ๋‹นํ•œ ๊ฒƒ์ด ์—†์Šต๋‹ˆ๋‹ค. ๋ถ„ํ• ํ‘œ๋ฅผ ํ†ตํ•ด์„œ ๋ฒ”์ฃผ ๋ณ„ ๋น„๊ต๋ฅผ ํ•˜๊ณ  ๋ถ„ํ• ํ‘œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜์˜ ๋…๋ฆฝ์„ฑ, ๋™์งˆ์„ฑ ๊ฒ€์ • ๋“ฑ์˜ ์นด์ด์ œ๊ณฑ ๊ฒ€์ •์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋งŒํผ ๋ถ„ํ• ํ‘œ๋Š” ์‰ฝ์ง€๋งŒ ์ค‘์š”ํ•œ ๊ฐœ๋…์ด๋ฉฐ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜• ๋“ฑ์œผ๋กœ ๋Œ€ํ‘œ๋˜๋Š” ์ผ๋ฐ˜ํ™” ์„ ํ˜•๋ชจํ˜•์„ ํ•ด์„ํ•˜๋Š” ๊ณผ์ •์—์„œ๋„ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. A1. ๋น„์œจ์˜ ๋น„๊ต 1. ๋น„์œจ์˜ ๋น„๊ต 2x2 ๋ถ„ํ• ํ‘œ๋ฅผ ํ†ตํ•œ ๋น„์œจ์˜ ๋น„๊ต์— ๋Œ€ํ•ด ๋‹ค๋ค„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜ ๋ชจ๋‘ ๋‘ ๊ฐ€์ง€ ๋ฒ”์ฃผ๋งŒ์„ ๊ฐ–๋Š” ์ด ํ•ญ๋ณ€ ์ˆ˜์ผ ๋•Œ, ๊ฐ ๋ฒ”์ฃผ์˜ ๋นˆ๋„๋ฅผ ์ด์šฉํ•ด 2x2 ๋ถ„ํ• ํ‘œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ๋ถ„ํ• ํ‘œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์˜ ๋ฒ”์ฃผ๋ณ„ ๋น„๊ต๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์ˆœํ•˜๊ฒŒ ๋นˆ๋„์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•  ์ˆ˜๊ฐ€ ์žˆ๊ณ  ํ•œ ๋ณ€์ˆ˜์˜ ๊ฐ ๋ฒ”์ฃผ ๋ณ„๋‹ค๋ฅธ ๋ณ€์ˆ˜์˜ ๋น„์œจ์„ ๋น„๊ตํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด ์ค‘ ๋Œ€ํ‘œ์ ์œผ๋กœ ์ƒ๋Œ€์œ„ํ—˜๋„์™€ ์˜ค์ฆˆ๋น„๋ผ๋Š” ๋‘ ๊ฐœ์˜ ๋น„์œจ ๋น„๊ต ์ฒ™๋„๋ฅผ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋ฉฐ ์ด ๋‘ ์ฒ™๋„๋Š” ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜๋ฅผ ๋ถ„์„ํ•  ๋•Œ ์–ธ์ œ๋‚˜ ์–ธ๊ธ‰๋˜๋Š” ์Šคํ…Œ๋””์…€๋Ÿฌ์ž…๋‹ˆ๋‹ค. ์ƒ๋Œ€์œ„ํ—˜๋„(relative risk) ํ”ํžˆ r.r์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ์ƒ๋Œ€์œ„ํ—˜๋„๋Š” ํ•œ ๋ณ€์ˆ˜์˜ ๋ฒ”์ฃผ ๋ณ„๋‹ค๋ฅธ ๋ณ€์ˆ˜์˜ ๋น„์œจ์„ ์ƒ๋Œ€์ ์œผ๋กœ ๋น„๊ตํ•  ๋•Œ ์“ฐ์ด๋ฉฐ ์ฒซ ๋ฒ”์ฃผ์— ์†ํ•  ํ™•๋ฅ  ์ถ”์ •๋Ÿ‰๊ณผ ๋‘ ๋ฒˆ์งธ ๋ฒ”์ฃผ์•  ์†ํ•  ํ™•๋ฅ  ์ถ”์ •๋Ÿ‰์˜ ๋น„๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ™•๋ฅ ์€ ๊ฐ ๋ฒ”์ฃผ์˜ ๋น„์œจ๋กœ ์ถ”์ •๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ฒซ ๋ฒˆ์งธ ๋ณ€์ˆ˜ ๊ฐ€ ์–ด๋–ค ์‹œํ—˜์˜ ๊ฒฐ๊ณผ(PASS, FAIL)์ด๊ณ  ํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ ๋ณ€์ˆ˜ ๊ฐ€ ํ•™๋ ฅ(๊ณ ์กธ, ๋Œ€์กธ)์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ‘œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€์กธ ์ค‘ ์‹œํ—˜์— ํ•ฉ๊ฒฉํ•œ ๋น„์œจ์€ 30 50 ์ž…๋‹ˆ๋‹ค. ๊ณ ์กธ ์ค‘ ์‹œํ—˜์— ํ•ฉ๊ฒฉํ•œ ๋น„์œจ์€ 15 40 ์ž…๋‹ˆ๋‹ค. ๋‘ ๊ทธ๋ฃน์˜ ์ƒ๋Œ€์œ„ํ—™๋„ ์ถ”์ •๋Ÿ‰( . ^ )์€ 30 50 15 40 ๋กœ 1.6์ž…๋‹ˆ๋‹ค. ํ•ด์„์„ ํ•˜๋ฉด ๋Œ€์กธ์˜ ๊ฒฝ์šฐ๊ฐ€ ๊ณ ์กธ์˜ ๊ฒฝ์šฐ๋ณด๋‹ค ์‹œํ—˜์— ํ•ฉ๊ฒฉํ•œ ๋น„์œจ์ด 60% ๋†’๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ถ”์ •์˜ ๊ด€์ ์—์„œ ํ•ด์„ํ•˜๋ฉด ๋Œ€์กธ์ž์˜ ํ•ฉ๊ฒฉ ํ™•๋ฅ ์ด ๊ณ ์กธ์˜ 1.6๋ฐฐ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ƒ๋Œ€์œ„ํ—˜๋„๊ฐ€ 1์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ๋‘ ๋ณ€์ˆ˜ ์‚ฌ์ด์— ์—ฐ๊ด€์„ฑ์ด ์—†๋‹ค๋Š” ๋œป์ด๋ฉฐ 0์— ๊ฐ€๊นŒ์›Œ์ง€๊ฑฐ๋‚˜ ์ปค์งˆ์ˆ˜๋ก ์Œ ํ˜น์€ ์–‘์˜ ๊ด€๊ณ„๋ฅผ ๊ฐ–๋Š”๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ƒ๋Œ€์œ„ํ—˜๋„๋Š” ๋งค์šฐ ์ง๊ด€์ ์ธ ๋น„๊ต ์ง€ํ‘œ๋กœ ๋ˆ„๊ตฌ๋‚˜ ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ณ  ์‰ฝ๊ฒŒ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ํฐ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค์ฆˆ๋น„(odds ratio) ์ƒ๋Œ€์œ„ํ—˜๋„์™€ ๋”๋ถˆ์–ด ๋‘ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜์˜ ๋ฒ”์ฃผ ๋ณ„ ๋น„๊ต์— ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ฒ™๋„๋กœ ์˜ค์ฆˆ๋น„๋ผ๋Š” ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค์ฆˆ๋น„๋Š” ์˜ค์ฆˆ(์„ฑ๊ณต ํ™•๋ฅ /์‹คํŒจ ํ™•๋ฅ )์˜ ๊ฐ ๋ฒ”์ฃผ ๋ณ„ ๋น„๋กœ ์ •์˜๋˜๋ฉฐ ์ƒ๋Œ€์œ„ํ—˜๋„๋ณด๋‹ค ์กฐ๊ธˆ ๋” ์œ ์—ฐํ•œ ํŠน์ง•์„ ๋ณด์ž…๋‹ˆ๋‹ค. ์„ฑ๊ณต ํ™•๋ฅ (๊ด€์‹ฌ ๋ฒ”์ฃผ์— ์†ํ•  ํ™•๋ฅ )์„๋ผ๊ณ  ํ–ˆ์„ ๋•Œ ์˜ค์ฆˆ์™€ ์˜ค์ฆˆ๋น„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. d s ฯ€ โˆ’ o r ( 1 โˆ’ 1 ) ( 2 โˆ’ 2 ) ์ƒ๋Œ€์œ„ํ—˜๋„๋Š” ๋งค์šฐ ์ง๊ด€์ ์€ ์ฒ™๋„์ง€๋งŒ ํ•œ ๋ณ€์ˆ˜์˜ ์ˆ˜๋ฅผ ๊ณ ์ •์‹œํ‚จ ์กฐ์‚ฌ์—์„œ๋Š” ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค์ฆˆ๋น„๋Š” ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์—๋„ ๋ฌธ์ œ์—†์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋Œ€์นญ์ ์œผ๋กœ ๊ตฌํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜์‘ ๋ณ€์ˆ˜์™€ ์„ค๋ช…๋ณ€์ˆ˜์˜ ๊ตฌ๋ณ„ ์—†์ด ๊ฐ™์€ ๊ฐ’์„ ์ œ์‹œํ•ด ์ค๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•Œ์ฝ”์˜ฌ์ค‘๋…๊ณผ ์‹ฌ์žฅ์งˆํ™˜์˜ ์—ฐ๊ด€์„ฑ์„ ๋ณด๊ธฐ ์œ„ํ•˜์—ฌ ์‹ฌ์žฅ์งˆํ™˜์„ ๊ฐ–๊ณ  ์žˆ๋Š” ์‚ฌ๋žŒ๊ณผ ๊ทธ๋ ‡์ง€ ์•Š์€ ์‚ฌ๋žŒ์„ ๊ฐ๊ฐ 50๋ช…, 100๋ช…์”ฉ ์„ ์ •ํ•˜์—ฌ ์•Œ์ฝ”์˜ฌ์ค‘๋… ์—ฌ๋ถ€์™€ ๋น„๊ตํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ๋ถ„์„ ์ž๋Š” ์ƒ๋Œ€์œ„ํ—˜๋„๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ฐ ๊ด€์ธก์น˜๊ฐ€ ๋…๋ฆฝ์ ์œผ๋กœ ๋žœ๋คํ•˜๊ฒŒ ์„ ํƒ๋œ ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  ์‹ฌ์žฅ์งˆํ™˜ ์—ฌ๋ถ€์— ์˜ํ•ด ์ •ํ•ด์ง„ ๋น„์œจ ํ˜น์€ ์ˆซ์ž์— ๋”ฐ๋ผ ์„ ์ •๋œ ์ง‘๋‹จ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹จ์ ์œผ๋กœ ์ „์ฒด ํ‘œ๋ณธ ์ค‘ ์‹ฌ์žฅ์งˆํ™˜์ž์˜ ๋น„์œจ์ด 3 ์ธ ๊ฒƒ์€ ๋ฏธ๋ฆฌ ๊ทธ๋ ‡๊ฒŒ ์ •ํ•ด ๋†“์•˜๊ธฐ ๋•Œ๋ฌธ์ด์ง€, ์‹ค์ œ๋กœ 3๋ช… ์ค‘ 1๋ช…์ด ์‹ฌ์žฅ์งˆํ™˜์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ ์ถ”์ •๋œ ํ™•๋ฅ ์€ ์˜๋ฏธ๊ฐ€ ์—†๊ฒŒ ๋˜๊ณ  ์ถ”์ •๋œ ํ™•๋ฅ ์˜ ๋น„๋ฅผ ์ด์šฉํ•˜๋Š” ์ƒ๋Œ€์œ„ํ—˜๋„ ์—ญ์‹œ ์˜๋ฏธ๋ฅผ ์žƒ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์˜ค์ฆˆ๋น„๋Š” ์ด๋Ÿฌํ•œ ์ œ์•ฝ์— ์œ ์—ฐํ•ฉ๋‹ˆ๋‹ค. ์•Œ์ฝ”์˜ฌ์ค‘๋…์ž ์ค‘ ์‹ฌ์žฅ์งˆํ™˜์˜ ์ถ”์ •๋œ ์˜ค์ฆˆ ( d s x 1 ) 4 6 / = 2 ์ž…๋‹ˆ๋‹ค. ๋น„์•Œ์ฝœ ์ค‘๋…์ž ์ค‘ ์‹ฌ์žฅ์งˆํ™˜์˜ ์ถ”์ •๋œ ์˜ค์ฆˆ( d s x 0 )๋Š” 46 144 98 144 46 98 ์ž…๋‹ˆ๋‹ค. ์˜ค์ฆˆ๋น„ ( . ^ )๋Š” d s x 1 d s x 0 4 2 46 98 98 23 ์ž…๋‹ˆ๋‹ค. ์‹ฌ์žฅ์งˆํ™˜์ด ์žˆ์„ ์˜ค์ฆˆ๋Š” ์•Œ์ฝ”์˜ฌ์ค‘๋…์ž ์ง‘๋‹จ์ด ๋น„์ค‘ ๋…์ž ์ง‘๋‹จ์˜ ์•ฝ 4.26๋ฐฐ์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์—๊ฒŒ ์ต์ˆ™ํ•œ ํ™•๋ฅ ์— ๋Œ€ํ•œ ์ง€ํ‘œ๊ฐ€ ์•„๋‹Œ ์˜ค์ฆˆ๋ผ๋Š” ์ƒ์†Œํ•œ ์ฒ™๋„๋ฅผ ์ด์šฉํ•ด์„œ ์กฐ๊ธˆ ๋‚ฏ์„ค ์ˆ˜๋Š” ์žˆ์œผ๋‚˜ ์˜ค์ฆˆ๋น„๋Š” ๋ถ„ํ• ํ‘œ์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์€ ๋ฌผ๋ก ์ด๊ณ  ํ™•๋ฅ ์„ ์„ ํ˜•ํ™”ํ•˜๋Š” ์—ฌ๋Ÿฌ ํ†ต๊ณ„ ๋ชจํ˜•์—์„œ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ ์ต์ˆ™ํ•ด์งˆ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ๋Œ€์œ„ํ—˜๋„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ์—ฐ๊ด€์ด ์—†๋‹ค๋ฉด ์˜ค์ฆˆ๋น„๋Š” 1์— ๊ฐ€๊นŒ์šธ ๊ฒƒ์ด๋ฉฐ 0์— ๊ฐ€๊นŒ์›Œ์ง€๊ฑฐ๋‚˜ ์ปค์งˆ์ˆ˜๋ก ๋ฐ€์ ‘ํ•œ ์—ฐ๊ด€์„ฑ์„ ๋ณด์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์‹คํŒจ์— ๋Œ€ํ•œ ์˜ค์ฆˆ๋น„๋Š” ์„ฑ๊ณต์— ๋Œ€ํ•œ ์˜ค์ฆˆ๋น„์˜ ์—ญ์ˆ˜๋กœ ํ‘œํ˜„๋˜๋ฉฐ ๊ฐ™์€ ์—ฐ๊ด€๋„๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ฆ‰, ์‹ฌ์žฅ์งˆํ™˜์ด ์žˆ์„ ์˜ค์ฆˆ๊ฐ€ ์•„๋‹Œ ์‹ฌ์žฅ์งˆํ™˜์ด ์—†์„ ์˜ค์ฆˆ๋ฅผ ์ด์šฉํ•˜์—ฌ ์˜ค์ฆˆ๋น„๋ฅผ ๊ตฌํ•˜๋ฉด 4.26์˜ ์—ญ์ˆ˜์ธ 0.2346์ด ๋‚˜์˜ฌ ๊ฒƒ์ด๊ณ  ์ด๋Š” ๊ฐ™์€ ์ˆ˜์ค€์˜ ์—ฐ๊ด€์„ฑ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. A2. ์นด์ด ์ œ๊ณฑ ๋…๋ฆฝ์„ฑ ๊ฒ€์ • 2. ์นด์ด ์ œ๊ณฑ ๋…๋ฆฝ์„ฑ ๊ฒ€์ • ์นด์ด ์ œ๊ณฑ ๋…๋ฆฝ์„ฑ ๊ฒ€์ •์€ ๋‘ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜๊ฐ€ ๋…๋ฆฝ์ ์œผ๋กœ ๋ถ„ํฌํ•˜๋Š”์ง€๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒ€์ •์ž…๋‹ˆ๋‹ค. ์ด ์—ญ์‹œ ๋ถ„ํ• ํ‘œ์—์„œ ์ง„ํ–‰๋˜๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ 2x2๊ฐ€ ์•„๋‹Œ ์—ฌ๋Ÿฌ ๋ฒ”์ฃผ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์นด์ด ์ œ๊ณฑ ๋…๋ฆฝ์„ฑ ๊ฒ€์ •์˜ ๊ธฐ๋ณธ์  ์•„์ด๋””์–ด๋Š” ๊ด€์ธก ๋นˆ๋„์™€ ๊ธฐ๋Œ€ ๋นˆ๋„(๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋…๋ฆฝ์ผ ๋•Œ์˜ ๋นˆ๋„)์˜ ์ฐจ์ด๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•๋ก ์„ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฒ”์ฃผ(์…€)์˜ ๊ธฐ๋Œ€ ๋นˆ๋„๊ฐ€ ๋†’๋‹ค๋ฉด(์ผ๋ฐ˜์ ์œผ๋กœ 5๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค), ์ •๊ทœ๋ถ„ํฌ ๊ทผ์‚ฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ •๊ทœ ๊ทผ์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉด ์ด๋ฅผ ์ด์šฉํ•ด ์นด์ด ์ œ๊ณฑ ํ†ต๊ณ„๋Ÿ‰์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (10์žฅ ์ฐธ๊ณ ) ์ด ์นด์ด ์ œ๊ณฑ ํ†ต๊ณ„๋Ÿ‰์€ ๊ด€์ธก ๋นˆ๋„์™€ ๊ธฐ๋Œ€ ๋นˆ๋„ ์ฐจ์ด์˜ ๋ณ€๋™์„ ์ •๋Ÿ‰ํ™”ํ•œ ํ†ต๊ณ„๋Ÿ‰์ž…๋‹ˆ๋‹ค. ์นด์ด ์ œ๊ณฑ ํ†ต๊ณ„๋Ÿ‰์ด ์ถฉ๋ถ„ํžˆ ๋†’๋‹ค๋ฉด ๊ด€์ธก ๋นˆ๋„์™€ ๊ธฐ๋Œ€ ๋นˆ๋„์˜ ์ฐจ์ด๋Š” ํฌ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ด€์ธก ๋นˆ๋„์™€ ๊ธฐ๋Œ€ ๋นˆ๋„์˜ ์ฐจ์ด๊ฐ€ ์ถฉ๋ถ„ํžˆ ํฌ๋ฉด, ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋…๋ฆฝ์ ์ด์ง€ ์•Š๋‹ค๋Š” ๊ฒฐ๋ก ์„ ๋‚ด๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ํ•˜๋‚˜ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋Œ€ ๋นˆ๋„๋Š” ๋‘ ๋ณ€์ˆ˜๊ฐ€ ํ†ต๊ณ„์ ์œผ๋กœ ๋…๋ฆฝ์ด๋ผ๋Š” ๊ท€๋ฌด๊ฐ€์„ค( 0 ) ํ•˜์— ๊ธฐ๋Œ€๋˜๋Š” ๋นˆ๋„์ด๊ณ  ์ด ๊ท€๋ฌด๊ฐ€์„ค์„ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด 0 ฯ€ j ฯ€ โ‹… ฯ€ j ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ํ‘œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด ์•„์ด๋””์–ด๋ฅผ ์ƒ๊ฐํ•ด ๋ด…์‹œ๋‹ค. i๋Š” ๊ฐ ์…€์— ์†ํ•  ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ง€์—ญ 3์ด๋ฉด์„œ B ๋‹น์„ ์ง€์ง€ํ•  ํ™•๋ฅ ์€ 23 ์ด ๋ฉ๋‹ˆ๋‹ค. i ์€ i ๋ฒˆ ์งธ ํ–‰์— ์†ํ•  ํ™•๋ฅ ๋กœ B ๋‹น์„ ์ง€์ง€ํ•  ํ™•๋ฅ ์€ 2์ž…๋‹ˆ๋‹ค. โ‹…๋Š” j ์งธ ์—ด์— ์†ํ•  ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค. ์ง€์—ญ 3์— ์†ํ•  ํ™•๋ฅ ์€ โ‹…์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ์ „ํ˜€ ์—ฐ๊ด€์ด ์—†๋‹ค๋ฉด ํ™•๋ฅ ์˜ ๊ณฑ๋ฒ•์น™์— ์˜ํ•ด ์ง€์—ญ 3์— ์†ํ•˜๋ฉด์„œ B ๋‹น์„ ์ง€์ง€ํ•  ํ™•๋ฅ ์€ ์ง€์—ญ 3์— ์†ํ•  ํ™•๋ฅ ๊ณผ B ๋‹น์„ ์ง€์ง€ํ•  ํ™•๋ฅ ์˜ ๊ณฑ์œผ๋กœ ํ‘œํ˜„๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๋ณ€์ˆ˜๊ฐ€ ํ†ต๊ณ„์ ์œผ๋กœ ๋…๋ฆฝ์ด๋ผ๋Š” ๊ฒƒ์€ i = i โ‹… โ‹… ๊ณผ ๋™์น˜์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ i ์€ i n ์œผ๋กœ ์ถ”์ •๋˜๊ณ  โ‹…๋Š” โ‹… n ์€์œผ๋กœ ์ถ”์ •๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๊ฐ ํ–‰๊ณผ ์—ด์˜ ๋นˆ๋„์™€ ์ „์ฒด ๋นˆ๋„์˜ ๋น„์œจ๋กœ ์ถ”์ •๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋Œ€ ๋นˆ๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถ”์ • i = โ‹… i = โ‹… i โ‹… โ‹… ( ์ • ) n ( i n ) ( โ‹… n ) n โ‹… n j ์ด๋ฅผ ์ด์šฉํ•ด ์ง€์—ญ 3์— ์†ํ•˜๋ฉด์„œ B ๋‹น์„ ์ง€์ง€ํ•  ๊ธฐ๋Œ€ ๋นˆ๋„๋ฅผ ๊ตฌํ•ด๋ณด๋ฉด ์•ฝ 161.44๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐ ์…€์— ๋Œ€ํ•œ ๊ธฐ๋Œ€ ๋นˆ๋„๋ฅผ ๊ตฌํ•ด ๊ด„ํ˜ธ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๊ณ  ๊ฐ ์…€์˜ ๊ธฐ๋Œ€ ๋นˆ๋„๋Š” ์ถฉ๋ถ„ํžˆ ์ปค์„œ ๊ทผ์‚ฌ ๊ฐ€์ •์„ ๋งŒ์กฑํ•˜๋ฏ€๋กœ ์นด์ด์ œ๊ณฑ ๊ฒ€์ •์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๋ง์”€๋“œ๋ ธ๋“ฏ์ด, ๊ธฐ๋Œ€ ๋นˆ๋„๋Š” ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋…๋ฆฝ์ด๋ผ๋Š” ๊ฐ€์ •ํ•˜์— ๊ตฌํ•ด์ง„ ๋นˆ๋„์ด๋ฏ€๋กœ ์‹ค์ œ ๊ด€์ธก ๋นˆ๋„์™€ ๊ธฐ๋Œ€ ๋นˆ๋„์˜ ์ฐจ์ด๊ฐ€ ํฌ๋‹ค๋Š” ๊ฒƒ์€ ๋‘ ๋ณ€์ˆ˜์˜ ์—ฐ๊ด€์„ฑ ์—ญ์‹œ ํฌ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ์•„์ด๋””์–ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐ ์…€์—์„œ์˜ ๊ด€์ธก ๋นˆ๋„์™€ ๊ธฐ๋Œ€ ๋นˆ๋„์˜ ์ด๋Ÿ‰์„ ์ด์šฉํ•˜๋ฉด ๋‘ ๋ณ€์ˆ˜์˜ ๋…๋ฆฝ์„ฑ์„ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹จ์ˆœํžˆ ์ฐจ์ด๋ฅผ ํ•ฉ์น˜๊ฒŒ ๋˜๋ฉด + / - ๊ฐ€ ์ƒ์‡„๋˜๋ฏ€๋กœ ์ œ๊ณฑ์„ ํ•ด์„œ ํ•ฉ์น˜๊ณ  ์ด๋Š” ์นด์ด์ œ๊ณฑ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์‹์„ ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. = i 1 โˆ‘ = b ( i โˆ’ i) E j ฯ‡ ( ( โˆ’ ) ( โˆ’ ) ) O o s r e f e u n i s : x e t d r q e c e a n m e o c t g r e f r o u n a i b e b n m ๊ตฌํ•ด์ง„ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰ Q๋Š” ์ž์œ ๋„๊ฐ€ (์—ด ๋ณ€์ˆ˜์˜ ๋ฒ”์ฃผ - 1) (ํ–‰ ๋ฒ”์ฃผ์˜ ๋ฒ”์ฃผ - 1) ์ธ ์นด์ด์ œ๊ณฑ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ๋งŒ์•ฝ Q๊ฐ€ ํฌ์ง€ ์•Š๋‹ค๋ฉด ์‹ค์ œ ๊ด€์ธก ๋นˆ๋„์™€ ๋…๋ฆฝ์ผ ๋•Œ์˜ ๊ธฐ๋Œ€ ๋นˆ๋„์˜ ์ฐจ๊ฐ€ ์ „์ฒด์ ์œผ๋กœ ํฌ์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ณ  ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋…๋ฆฝ์ด๋ผ๋Š” ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•˜์ง€ ๋ชปํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ Q ๊ฐ€ ๋งค์šฐ ํฌ๋‹ค๋ฉด ๋‘ ๋ณ€์ˆ˜๋Š” ์—ฐ๊ด€์„ฑ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ณ  ๊ท€๋ฌด๊ฐ€์„ค์„ ๊ธฐ๊ฐํ•˜๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ž์œ ๋„๊ฐ€ ์ €๋Ÿฐ ํ˜•ํƒœ๋ฅผ ๋ ๋Š” ์ด์œ ๋Š” ์ „์ฒด ํ‘œ๋ณธ ์ˆ˜ ์ด ๊ณ ์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. n์ด ๊ณ ์ •๋œ ์ƒํƒœ๋กœ ๊ธฐ๋Œ€ ๋นˆ๋„๋ฅผ ์ถ”์ •ํ•˜๋ฉด์„œ ํ–‰์˜ ํ•ฉ= , ์—ด์˜ ํ•ฉ=์ด๋ผ๋Š” ์ œ์•ฝ์‹์„ ๊ฐ–๊ฒŒ ๋˜๊ณ  ๊ทธ ์กฐํ•ฉ์œผ๋กœ ๊ตฌํ•ด์ง€๋Š” Q๋Š” (์—ด ๋ณ€์ˆ˜์˜ ๋ฒ”์ฃผ - 1) (ํ–‰ ๋ฒ”์ฃผ์˜ ๋ฒ”์ฃผ - 1)๋ผ๋Š” ์ž์œ ๋„๋ฅผ ๊ฐ–๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ์ง€์—ญ๊ณผ ์ง€์ง€ ์ •๋‹น์ด ๋…๋ฆฝ์ธ์ง€์— ๋Œ€ํ•œ ์นด์ด์ œ๊ณฑ ๊ฒ€์ •ํ†ต๊ณ„๋Ÿ‰์„ ๊ตฌํ•ด๋ณด๋ฉด Q๋Š” ์•ฝ 411.35๊ฐ€ ๋˜๊ณ  ์ž์œ ๋„๋Š” (3-1)(4-1) = 6 ์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ โˆ’ a u๋Š” ๋งค์šฐ ์ž‘์•„ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋…๋ฆฝ์ด๋ผ๋Š” ๊ท€๋ฌด๊ฐ€์„ค์€ ๊ธฐ๊ฐ๋ฉ๋‹ˆ๋‹ค. A4. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„ 4. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„(logistic regression analysis)์€ ์ผ๋ฐ˜ํ™” ์„ ํ˜•๋ชจํ˜•(generalized linear model, GLM)์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ํฐ ๋ฒ”์ฃผ์˜ ํ†ต๊ณ„ ๋ชจํ˜• ๋ชจ๋ธ๋ง ๋ฐฉ๋ฒ•์— ์†ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์šฐ์„  GLM์˜ ํŠน์ง•๋งŒ ๊ฐ„๋‹จํžˆ ํ›‘์–ด๋ณด๊ณ  ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•์— ๋Œ€ํ•ด ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค. GLM(Generalized Linear Model) GLM์€ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์„ ํ˜•์ ์ด์ง€ ์•Š์€ ๋Œ€์ƒ(๋น„์„ ํ˜•)์„ ์„ ํ˜•์ ์œผ๋กœ '์ผ๋ฐ˜ํ™”'์‹œํ‚จ ๋ชจํ˜•์ž…๋‹ˆ๋‹ค. ์„ ํ˜•ํ™” ์‹œํ‚ค๋Š” ์ด์œ ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์œผ๋กœ ์„ ํ˜•๋ชจํ˜•์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจํ˜•์˜ ํ•ด์„, ํ™•์žฅ, ์ˆ˜์ • ๋“ฑ์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค. ๋น„์„ ํ˜•๋ชจํ˜•์˜ ๊ฒฝ์šฐ๋Š” ๋ชจํ˜•์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์ด ๋งŽ์ด ์ œํ•œ๋  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์— ๋ฏผ๊ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„ ํ˜•๋ชจํ˜•์— ๋น„ํ•ด ๋œ ์„ ํ˜ธ๋˜๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„์—์„œ ์„ ํ˜•ํ™” ์‹œํ‚ค๋Š” ๋Œ€์ƒ์€ ๋ฌด์—‡์ผ๊นŒ์š”? ๋ฐ”๋กœ ๊ด€์‹ฌ ๋ฒ”์ฃผ์— ์†ํ•  ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค. ํ™•๋ฅ ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์˜ˆ์ธก์ž๋“ค์— ๋”ฐ๋ผ ๋น„์„ ํ˜• ํ•˜๊ฒŒ ๋ถ„ํฌ๋˜์–ด ์žˆ๊ณ , ํ˜•ํƒœ๋Š” S์ž ํ˜•ํƒœ๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ฒ˜์Œ๋ถ€ํ„ฐ ํ™•๋ฅ ์— ์„ ํ˜• ๋ผ์ธ์„ ์ ํ•ฉ์‹œ์ผœ ์„ ํ˜•ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ด๋Š” ๋ช‡ ๊ฐ€์ง€ ์ œ์•ฝ์ด ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. library(ggplot2) k = runif(n = 500, min = 2 , max = 4) K = c() P = c() P1 = c() for(i in k){ p1 = exp(-21.3 + 6.74*i) /(1 + exp(-21.3 + 6.74*i)) # error = runif(n = 1, min = -0.1, max = 0.1) error = 0 p = p1 + error p = ifelse(p < 0 ,0, p) p = ifelse(p > 1, 1, p) K = c(K, i) P = c(P, P) P1 = c(P1, P1) } ggplot(NULL) + geom_point(aes(x = K, y = P),col = 'royalblue', alpha = 0.2) + theme_bw() + scale_x_continuous(breaks = seq(2,4, by = 0.5)) + xlab("Predictor") + ylab("Prob") ์œ„ ๊ทธ๋ž˜ํ”„๋Š” ์–ด๋–ค ์—ฐ์†ํ˜• ์˜ˆ์ธก์ž์— ๋”ฐ๋ฅธ ๊ด€์‹ฌ ๋ฒ”์ฃผ์— ์†ํ•  ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•Œ์ฝ”์˜ฌ ์„ญ์ทจ๋Ÿ‰๊ณผ ๋น„๋งŒ์ผ ํ™•๋ฅ ์€ ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ถ„ํฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•Œ์ฝ”์˜ฌ ์„ญ์ทจ๊ฐ€ ๋งŽ์„์ˆ˜๋ก ๋น„๋งŒ์ผ ํ™•๋ฅ ์€ ๋†’์•„์ง€์ง€๋งŒ ์™„์ „ ์„ ํ˜•์ ์ด๋ผ๊ธฐ๋ณด๋‹ค๋Š” ์•ฝ๊ฐ„์˜ ์ปค๋ธŒ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ์„  ์—ฌ๊ธฐ์— ์ผ๋ฐ˜์ ์ธ ์„ ํ˜•ํšŒ๊ท€ ๋ผ์ธ์„ ์ ํ•ฉ์‹œ์ผœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ggplot(NULL) + geom_point(aes(x = K, y = P),col = 'royalblue', alpha = 0.2) + geom_smooth(aes(x = K, y = P),method = 'lm', col = 'grey20') + geom_h line(yintercept = c(0,1), linetype = 'dashed') + scale_x_continuous(breaks = seq(2,4, by = 0.5)) + theme_bw() + xlab("Predictor") + ylab("Prob") ์„ ํ˜• ํšŒ๊ท€์„ ์ด ์•„์ฃผ ๋น„ํ•ฉ๋ฆฌ์ ์ด์ง„ ์•Š์Šต๋‹ˆ๋‹ค. ์—ฐ์†ํ˜• ์˜ˆ์ธก์ž๊ฐ€ ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ํ™•๋ฅ  ์—ญ์‹œ ์ฆ๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๋˜ํ•œ ์ด๋Ÿฐ ์„ ํ˜•์‹์˜ ๊ฒฝ์šฐ ์œ„์—์„œ ๋ฐฐ์šด ๋ถ„์‚ฐ๋ถ„์„์„ ํ†ตํ•œ ๋ชจํ˜•์˜ ์ ํ•ฉ๋„ ๊ฒ€์ •๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๊ธฐ์กด์— ํšŒ๊ท€๋ถ„์„์—์„œ ์‚ฌ์šฉํ•˜๋˜ ๋ชจ๋“  ๋ถ„์„์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ๊ตฌ์กฐ์ ์ธ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋กœ ํ™•๋ฅ ์€ 0๊ณผ 1 ์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ–๋Š” ๋ฐ ๋ฐ˜ํ•ด ํšŒ๊ท€์„ ์€ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ์Œ์˜ ๊ฐ’์ด๋‚˜ 1์„ ์ดˆ๊ณผํ•˜๋Š” ์˜ˆ์ธก๊ฐ’๋ฅผ ์ œ์‹œํ•  ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜์—ญ์„ ํ‘œ์‹œํ•˜์ž๋ฉด ์•„๋ž˜ ๋™๊ทธ๋ผ๋ฏธ์™€ ๊ฐ™์€ ๊ตฌ๊ฐ„์ž…๋‹ˆ๋‹ค. ggplot(NULL) + geom_point(aes(x = K, y = P),col = 'royalblue', alpha = 0.2) + geom_smooth(aes(x = K, y = P),method = 'lm', col = 'grey20') + geom_h line(yintercept = c(0,1), linetype = 'dashed') + geom_point(aes(x = c(2.25,3.9), y = c(-0.05,1.05)),size = 10, shape = 1, col = 'red') + scale_x_continuous(breaks = seq(2,4, by = 0.5)) + theme_bw() + xlab("Predictor") + ylab("Prob") ์„ธ๋กœ์ถ•์€ ํ™•๋ฅ  ๊ฐ’์„ ํ‘œํ˜„ํ•œ ๊ฒƒ์ธ๋ฐ, ๋นจ๊ฐ„ ๋™๊ทธ๋ผ๋ฏธ๋Š” ํ™•๋ฅ ์ด ๊ฐ€์งˆ ์ˆ˜ ์—†๋Š” ๊ฐ’์„ ์ถ”์ •ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ๋ฐ์ดํ„ฐ์—์„œ๋Š” -4 ์ดํ•˜์˜ ์ ์—์„œ -ํ™•๋ฅ  ๊ฐ’์œผ๋กœ ์ถ”์ •๋˜๊ณ  ์žˆ์ง€๋งŒ, ์ด ํšŒ๊ท€์„ ์„ ์ด์šฉํ•˜์—ฌ ์ƒˆ ๋ฐ์ดํ„ฐ๋ฅผ ์˜ˆ์ธกํ•ด ๋ณด๋ฉด ์ด๋ณด๋‹ค ํ›จ์”ฌ ๋งŽ์€ ํฌ์ธํŠธ๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์œผ๋กœ ์ถ”์ •๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด๋Ÿฌํ•œ ๊ตฌ์กฐ์ ์ธ ๋ฌธ์ œ๋กœ ํ™•๋ฅ ์˜ ํŠน์„ฑ์„ ์ด์šฉํ•œ ๊ธฐํƒ€ ์ถ”๊ฐ€ ๋ถ„์„ ์—ญ์‹œ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ ๋น„์„ ํ˜•์ ์ธ ๋ชจํ˜•์„ ์ ํ•ฉ์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ž˜ํ”„๋Š” S์ž ํ˜•ํƒœ๋ฅผ ๋ ๋Š” ๋น„์„ ํ˜•์ ์ธ ํšŒ๊ท€๊ณก์„ ์ž…๋‹ˆ๋‹ค. ggplot(NULL) + geom_point(aes(x = K, y = P),col = 'royalblue', alpha = 0.2) + geom_line(aes(x = K, y = P1),col = 'red', size = 2.5, alpha = 0.5) + geom_h line(yintercept = c(0,1), linetype = 'dashed') + scale_x_continuous(breaks = seq(2,4, by = 0.5)) + theme_bw() + xlab("Predictor") + ylab("Prob") ํ•œ๋ˆˆ์— ๋ณด๊ธฐ์—๋„ ์„ ํ˜• ํšŒ๊ท€์„ ๋ณด๋‹ค๋Š” ํ›จ์”ฌ ๋ฐ์ดํ„ฐ์— ์ž˜ ์ ์šฉ๋จ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ  ๊ตฌ์กฐ์ ์ธ ๋ฌธ์ œ๋„ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์œ„์—์„œ ๋ง์”€๋“œ๋ฆฐ ๋Œ€๋กœ ๋น„์„ ํ˜• ๋ชจ๋ธ์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ถ”๊ฐ€ ๋ถ„์„์— ์ œ์•ฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ œ์•ฝ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ฝ๊ฐ„์˜ ๋ณ€ํ™˜์„ ํ†ตํ•ด ์ด๋ฅผ ์„ ํ˜•ํ™”ํ•˜๋Š” ๊ฒƒ์ด ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„์˜ ๊ธฐ๋ณธ ์•„์ด๋””์–ด์ž…๋‹ˆ๋‹ค. ์˜ˆ์ธก์ž๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ด€์‹ฌ ๋ฒ”์ฃผ์— ์†ํ•  ํ™•๋ฅ ์„ ์„ ํ˜•์œผ๋กœ ์˜ˆ์ธกํ•˜๊ธฐ ์ดํ•ด์„œ๋Š” โ‰ค x 1 ์˜ ์ œ์•ฝ์„ โˆž l g ( x โˆ’ x ) โˆž ์˜ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. x ฯ€ 1 ฯ€๋กœ ๋ณ€ํ™˜, ์ฆ‰ d s ๋กœ ๋ณ€ํ™˜์„ ํ•ด์ฃผ๋ฉด, x โˆ’ x 0 ฯ€ 1 ฯ€ <์˜ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„ o ๋ณ€ํ™˜์„ ์ทจํ•ด์ฃผ๋ฉด, ๋กœ๊ทธ ํ•จ์ˆ˜์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ โˆž l g ( x โˆ’ x ) โˆž ์˜ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ด€์‹ฌ ๋ฒ”์ฃผ์˜ ํ™•๋ฅ ์„ ์„ ํ˜•๋ชจํ˜•์œผ๋กœ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ์ˆœ์ˆ˜ x ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, o ( x โˆ’ x ) ๋ฅผ ํ™œ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ณ€ํ™˜์„ o i ๋ณ€ํ™˜์ด๋ผ ๋ถ€๋ฅด๋ฉฐ, o i ๋ณ€ํ™˜ ๊ฐ’์„ ๋ฐ˜์‘ ๋ณ€์ˆ˜ ๋กค ํ•˜๊ณ  ์„ ํ˜•๋ชจ๋ธ์„ ์ ํ•ฉ์‹œํ‚จ ๊ฒƒ์„ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์„ ํ˜•ํ™”๋ฅผ ์œ„ํ•ด ์•„๋ž˜์˜ ์‹์„ ๊ณ ๋ คํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ x ๋Š” ์„ค๋ช… ์˜ˆ์ธก์ž๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ์˜ ๊ด€์‹ฌ ๋ฒ”์ฃผ์— ์†ํ•  ํ™•๋ฅ ์ž…๋‹ˆ๋‹ค. x e p ( 0 ฮฒ x ) + x ( 0 ฮฒ x ) ์ด๋Š” ์ผ๋ฐ˜ ์„ ํ˜•ํšŒ๊ท€์—์„œ์˜ x ฮฒ + 1 ์™€ ๊ฐ™์ด ์ง์„ ์˜ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•จ์ˆ˜์— ๋Œ€์‘ํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ์„ ํ˜•์ ์ธ ๊ด€๊ณ„๊ฐ€ ์•„๋‹Œ ์„ฑ๊ณต ํ™•๋ฅ  x ์™€ ์„ค๋ช… ์˜ˆ์ธก์ž์™€์˜ ๊ด€๊ณ„๋ฅผ S์ž ๋ชจ์–‘์œผ๋กœ ํ‘œํ˜„ํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์„ ํ˜•ํ™”๋ฅผ ์œ„ํ•ด ์œ„์˜ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ ํ˜•์‹ ๋ชจ์–‘์œผ๋กœ ์ •๋ฆฌ๋ฅผ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o i ( x ) l g ( x โˆ’ x ) ฮฒ + 1 l g ( x โˆ’ x ) ์™€ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ x ์— ๋Œ€ํ•œ ๋กœ์ง“ํ•จ์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ o i ( x ) ์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋กœ์ง“ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์šฐ๋ณ€์„ ์„ ํ˜•์‹์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์ฒ˜์Œ์— ๋ง์”€๋“œ๋ ธ๋˜ ํ™•๋ฅ ๊ณผ ์˜ˆ์ธก์ž์˜ ๋น„์„ ํ˜•์  ๊ด€๊ณ„๋ฅผ ์„ ํ˜•์‹์œผ๋กœ ์ผ๋ฐ˜ํ™”ํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•๊ณผ ๊ฐ™์€ GLM์—์„œ๋Š” ์ ˆํŽธ๊ณผ ๊ธฐ์šธ๊ธฐ๋ฅผ ์ผ๋ฐ˜์ ์ธ ํšŒ๊ท€๋ถ„์„๊ณผ ๋‹ค๋ฅด๊ฒŒ ์ตœ์†Œ ์ œ๊ณฑ ์ถ”์ • ๋ฒ•์ด ์•„๋‹Œ ์ตœ๋Œ€ ๊ฐ€๋Šฅ ๋„๋„ ์ถ”์ • ๋ฒ•(method of maximum likelihood estimation)์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์— ์˜ํ•ด ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค์Œ ์žฅ์—์„œ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค ์ด์ œ ๊ตฌ์ฒด์ ์œผ๋กœ ๋กœ์ง€์Šคํ‹ฑ ๋ชจํ˜•์„ ์‚ดํŽด๋ด…์‹œ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ๋กœ์ง€์Šคํ‹ฑ ๋ชจํ˜•์€ ๋ฐ˜์‘ ๊ฐ’์ด ์ด ํ•ญ๋ณ€ ์ˆ˜์ž„์„ ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ž˜ ์ƒ๊ฐํ•ด ๋ณด๋ฉด, ์œ„์—์„œ ๊ณ„์† ๋‹ค๋ฃจ์—ˆ๋˜ ํ™•๋ฅ ์€ ์ฃผ์–ด์ง„ ๊ฐ’์ด ๋  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ๋– ์˜ฌ๋ฆฌ์‹ค ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. ํ™•๋ฅ ์€ ์ธก์ •๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ์„ค๋ช…์„ ์œ„ํ•ด ๋ณด์—ฌ๋“œ๋ฆฐ ๊ทธ๋ž˜ํ”„์˜ ํ™•๋ฅ  ๊ฐ’ ์—ญ์‹œ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ๋ชจํ˜•์—์„œ์˜ ํ™•๋ฅ ์€ ์ดํ•ญ ๋ฐ˜์‘ ๊ฐ’์œผ๋กœ ์ธก์ •๋œ ์งˆ์  ์ž๋ฃŒ๋ฅผ ์ด์šฉํ•ด ์ถ”์ •๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•Œ์ฝ”์˜ฌ ์„ญ์ทจ๋Ÿ‰๊ณผ ๋น„๋งŒ์ผ ํ™•๋ฅ ์˜ ๊ด€๊ณ„๋ฅผ ์•Œ๊ธฐ ์œ„ํ•œ ์กฐ์‚ฌ๋ฅผ ํ•  ๋•Œ, '๋น„๋งŒ์ผ ํ™•๋ฅ ' ์ž์ฒด๋Š” ์ธก์ •ํ•  ์ˆ˜ ์—†๊ณ  '๋น„๋งŒ ์—ฌ๋ถ€'๋ผ๋Š” ์ดํ•ญ ๋ฐ˜์‘ ๊ฐ’์„ ์ธก์ •ํ•ด ๊ทธ ํ™•๋ฅ ์„ ์ถ”์ •ํ•œ๋‹ค๋Š” ๊ฒƒ์ด์ฃ . ์ด๋Ÿฐ ์ดํ•ญ ๋ฐ˜์‘ ๊ฐ’(๊ด€์‹ฌ ๋ฒ”์ฃผ = 1)์™€ ์˜ˆ์ธก์ž๊ฐ„์˜ ์‚ฐ์ ๋„๋ฅผ ๊ทธ๋ฆฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. PG = ifelse(P1 < 0.5,0, 1) K1 = K + runif(n = length(K),min = -0.5, max = 0.5) ggplot(NULL) + geom_point(aes(x = K1, y = PG),col = 'royalblue') + geom_line(aes(x = K, y = P1),col = 'red', size = 2.5, alpha = 0.5) + theme_bw() + xlab("Predictor") + ylab("Prob") + scale_x_continuous(breaks = seq(2,4, by = 0.5)) ๊ด€์‹ฌ ๋ฒ”์ฃผ ์•„๋‹ˆ๋ฉด ๋น„ ๊ด€์‹ฌ ๋ฒ”์ฃผ๋กœ ์กฐ์‚ฌ๋œ ์„ธ๋กœ ์ถ• ๋ฐ˜์‘ ๋ณ€์ˆ˜๋Š” 0 ์•„๋‹ˆ๋ฉด 1์˜ ๊ฐ’๋งŒ ๊ฐ–์Šต๋‹ˆ๋‹ค. ๊ด€์‹ฌ ์žˆ๋Š” ๋ฒ”์ฃผ๋ฉด 1์„ ๋ถ€์—ฌํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 0์„ ๋ถ€์—ฌํ•ด ๊ตฌ๋ณ„ํ•ด ๋†“์€ ๊ฒƒ์ด์ง€์š”. ๊ทธ์— ๋น„ํ•ด ์˜ˆ์ธก ์ž๋Š” ์—ฐ์†์ ์œผ๋กœ ๋ถ„ํฌํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฐ์ ๋„๋ฅผ ๋ณด๋ฉด ์ง๊ด€์ ์œผ๋กœ ์˜ˆ์ธก์ž ๊ฐ’์ด ํฐ ๊ฐ’์„ ๊ฐ€์ง€๋ฉด ๊ด€์‹ฌ ๋ฒ”์ฃผ์— ์†ํ•  ํ™•๋ฅ ์ด ํฌ๊ณ  ์ž‘์œผ๋ฉด ๋น„ ๊ด€์‹ฌ ๋ฒ”์ฃผ์— ์†ํ•  ํ™•๋ฅ ์ด ํฌ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋Š” ์˜ˆ์ธก์ž๊ฐ€ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ํ•˜๋Š˜์ƒ‰ ์„ ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์žฅ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ์˜์—ญ์€ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์ด ์—†์–ด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ๋ฒ”์ฃผ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋‘ ๋ฒ”์ฃผ๊ฐ€ ๊ณต์กดํ•˜๋Š” ๊ฐ€์šด๋ฐ ๋ถ€๋ถ„์€ ์˜ˆ์ธก์ž๊ฐ€ ๊ฒน์น˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์ด ํ™•๋ฅ ์„ ์ถ”์ •ํ•˜์—ฌ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๊ณก์„ ์„ ๊ทธ๋ ธ์„ ๊ฒฝ์šฐ ๋ณ€๊ณก์ ์ด ์ƒ๊ธฐ๋Š” ์˜์—ญ์ด๊ณ  ์ถ”์ •๋œ ํ™•๋ฅ ์ด 0.5์— ๊ฐ€๊นŒ์šด ์• ๋งคํ•œ ๊ฐ’์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์˜ˆ์ธกํ•˜๊ธฐ ์–ด๋ ค์šด ์˜์—ญ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๊ณก์„ ์„ ์ ํ•ฉ์‹œ์ผœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ggplot(NULL) + geom_point(aes(x = K1, y = PG),col = 'royalblue') + geom_vline(xintercept = c(2.7,3.6),linetype = 'dashed', col = 'red') + geom_text(aes(x = c(2,4), y = c(0.5,0.5)),label = c("๋น„๊ด€์‹ฌ๋ฒ”์ฃผ","๊ด€์‹ฌ ๋ฒ”์ฃผ"),col = 'red', size = 5) + geom_text(aes(x = 3.1, y = 0.5), label = "๊ณต์กด", col = 'royalblue', size = 5) + theme_bw() + xlab("") + ylab("") + scale_x_continuous(breaks = seq(1,5, by = 0.5)) ์ ํ•ฉ๋œ ํšŒ๊ท€๊ณก์„ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ^ = x ( 21.3 6.74 ) + x ( 21.3 6.74 ) l g ( ^ 1 ฯ€ x ) โˆ’ 21.3 6.74 ์„ ํ˜•์ ์œผ๋กœ ์ถ”์ •๋œ o i ( ^ ) ๋Š” ๊ผญ ์—ฐ์†ํ˜• ์˜ˆ์ธก์ž๋ฟ ์•„๋‹ˆ๋ผ ๋ฒ”์ฃผํ˜• ์˜ˆ์ธก ์ž๋„ ๊ฐ€๋Šฅํ•˜๊ณ  ํ˜ผํ•ฉ๋œ ํ˜•ํƒœ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ชจํ˜•์— ๋Œ€ํ•œ ํ•ด์„์€ ์„ ํ˜•์ ์œผ๋กœ ๋ฐ”๋ผ๋ณด์•„๋„ ์ƒ๊ด€์€ ์—†์œผ๋‚˜ ๋กœ์ง“ ๊ฐ’์ด ์ง๊ด€์ ์ด์ง€ ์•Š์•„ ์ผ๋ฐ˜์ ์œผ๋กœ ์˜ค์ฆˆ๋น„๋ฅผ ํ†ตํ•ด ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ์‹์—์„œ ์˜ˆ์ธก์ž์˜ ๊ธฐ์šธ๊ธฐ 6.74๋Š” '์˜ˆ์ธก์ž๊ฐ€ ํ•œ ๋‹จ์œ„(1) ์ฆ๊ฐ€ํ–ˆ์„ ๋•Œ์˜ ์„ฑ๊ณตํ•  ์˜ค์ฆˆ '๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋‘ ๋ชจํ˜•์˜ ์ฐจ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ธก์ž๊ฐ€ ์ธ ๋ชจํ˜•๊ณผ ํ•œ ๋‹จ์œ„(1) ์ฆ๊ฐ€ํ•˜์—ฌ + ์ธ ๋ชจํ˜•์˜ ์ฐจ๋ฅผ ๋ด…์‹œ๋‹ค. ์•„๋ž˜ ์‹์—์„œ๋Š” ์ถ”์ •๋œ ํ™•๋ฅ ์˜ ๊ตฌ๋ณ„์„ ์œ„ํ•ด ์˜ˆ์ธก์ž๊ฐ€ ์ธ ๊ฒฝ์šฐ์—๋Š” x, + ์ธ ๊ฒฝ์šฐ๋Š” x 1๋กœ ํ‘œํ˜„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. [ 21.3 6.74 ( + ) ] [ 21.3 6.74 ( ) ] 6.74 โ‡” [ o ( ^ + 1 ฯ€ x 1 ) ] [ o ( ^ 1 ฯ€ x ) ] l g [ ( ^ + 1 ฯ€ x 1 ) ( ^ 1 ฯ€ x ์ฆ‰, ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•์—์„œ์˜ ๊ธฐ์šธ๊ธฐ๋Š” ์˜ˆ์ธก์ž๊ฐ€ 1 ์ฆ๊ฐ€ํ–ˆ์„ ๋•Œ์˜ ๋กœ๊ทธ ์˜ค์ฆˆ๋น„ ์ถ”์ •๋Ÿ‰์ด๊ณ  ๊ธฐ์šธ๊ธฐ x [ ์šธ ] ๋Š” ์˜ˆ์ธก์ž๊ฐ€ ํ•œ ๋‹จ์œ„ ์ฆ๊ฐ€ํ–ˆ์„ ๋•Œ์˜ ์˜ค์ฆˆ๋น„ ์ถ”์ •๋Ÿ‰์ž…๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ ์ ์šฉํ•ด ๋ณด๋ฉด ์˜ˆ์ธก์ž๊ฐ€ ํ•œ ๋‹จ์œ„ ์ฆ๊ฐ€ํ•  ๋•Œ ๊ด€์‹ฌ ๋ฒ”์ฃผ์— ์†ํ•  ์˜ค์ฆˆ๊ฐ€ x ( 6.74 ) 846 ๋ฐฐ๊ฐ€ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์‘์šฉํ•˜๋ฉด ๋‹จ์œ„๋ฅผ ์กฐ์ •ํ•ด์„œ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์—์„œ ๋‹ค๋ฃฌ ์˜ˆ์ œ๋Š” ์˜ˆ์ธก์ž์˜ ๋ฒ”์œ„๊ฐ€ ๋งค์šฐ ์ž‘์œผ๋ฏ€๋กœ 1์ด ์•„๋‹Œ 0.1์ด ์ฆ๊ฐ€ํ–ˆ์„ ๋•Œ์˜ ์˜ค์ฆˆ๋น„๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋กœ๊ทธ ์˜ค์ฆˆ๋น„๋Š” [ 21.3 6.74 ( + 0.1 ) ] [ 21.3 6.74 ( ) ] 0.674 ์ด ๋˜๊ฒ ๊ณ  ์˜ค์ฆˆ๋Š” x ( 0.674 ) 1.96 ์ด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์˜ˆ์ธก์ž๊ฐ€ 0.1 ์ฆ๊ฐ€ํ•˜๋ฉด ๊ด€์‹ฌ ๋ฒ”์ฃผ์— ์†ํ•  ์˜ค์ฆˆ๊ฐ€ 1.96๋ฐฐ ๋จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A5. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„(R Code) 5. ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„(R Code) ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„๋„ ๊ฒฐ๊ตญ์€ ํšŒ๊ท€๋ถ„์„์ด๊ธฐ์— ๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์€ ๋น„์Šทํ•˜์ง€๋งŒ ์ผ๋ฐ˜์ ์ธ ํšŒ๊ท€๋ถ„์„๋ณด๋‹ค๋Š” ์ข€ ๊นŒ๋‹ค๋กญ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ์—ญ์‹œ ์ธ์‚ฌ๊ด€๋ฆฌ ๋ฐ์ดํ„ฐ(HR)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง์›๋“ค์˜ ์ด์ง ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋ถ„๋ฅ˜ ๋ชจํ˜•์„ ๋งŒ๋“ค์–ด ํ†ต๊ณ„์ ์œผ๋กœ ๊ฒ€์ •ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด์ง์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๋ณ€์ˆ˜๋“ค์˜ ๊ธฐ์šธ๊ธฐ๋Š”์ด๋‹ค 0 ์ด์— ํ–ฅ ๋ฏธ๋Š” ์ˆ˜์˜ ์šธ ๋Š” ์ด. 1 n t 0 ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„ Logistic = glm(left ~ satisfaction_level + salary + time_spend_company, data = HR, family = binomial()) ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„์€ ์ผ๋ฐ˜ํ™” ์„ ํ˜• ๋ชจํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— glm() ๋ช…๋ น์–ด๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. family = binomial()์€ ์ผ๋ฐ˜ํ™” ์„ ํ˜• ๋ชจํ˜•์—์„œ๋Š” ๋‹ค์–‘ํ•œ ๋ถ„ํฌ์˜ ์ข…์† ๋ณ€์ˆ˜์— ์ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ข…์† ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ค ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ณ  ์žˆ๋Š”์ง€ ์˜ต์…˜์„ ์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. left๋Š” ์ด์ง ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ดํ•ญ ๋ณ€์ˆ˜์ด๋ฏ€๋กœ ์ดํ•ญ ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ binomial()์„ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. summary(Logistic) Call: glm(formula = left ~ satisfaction_level + salary + time_spend_company, family = binomial(), data = HR) Deviance Residuals: Min 1Q Median 3Q Max -1.8628 -0.6774 -0.4666 -0.1781 2.7644 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 0.48069 0.07532 6.382 1.75e-10 *** satisfaction_level -3.72386 0.08852 -42.069 < 2e-16 *** salarymedium -0.53427 0.04436 -12.044 < 2e-16 *** salaryhigh -1.98592 0.12461 -15.938 < 2e-16 *** time_spend_company 0.21159 0.01418 14.922 < 2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 16465 on 14998 degrees of freedom Residual deviance: 13597 on 14994 degrees of freedom AIC: 13607 Number of Fisher Scoring iterations: 5 ๊ฒฐ๊ณผํ‘œ ํ•ด์„์€ ํšŒ๊ท€๋ถ„์„์—์„œ ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ Deviance, AIC ๊ฐ’์€ ๋‹ค์Œ ์žฅ์—์„œ ์„ค๋ช…ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•ด์•ผ ๋˜๋Š” ์ ์€ Dummy variable(๊ฐ€๋ณ€ ์ˆ˜)๋กœ ๋ณ€ํ™˜๋˜์–ด ๋ชจํ˜•์— ํˆฌ์ž…๋œ salary ๋ณ€์ˆ˜์˜ ๊ธฐ์šธ๊ธฐ ํ•ด์„์ž…๋‹ˆ๋‹ค. Dummy variable์€ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜๊ฐ€ ํšŒ๊ท€ ๋ชจํ˜•์— ํˆฌ์ž…๋  ๋•Œ ๋ถ„์„์— ๋งž๊ฒŒ ๋ณ€ํ™˜ ๋œ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ์„ฑ๋ณ„(Male, Female) ๋ณ€์ˆ˜๋ฅผ ํšŒ๊ท€ ๋ชจํ˜•์— ํˆฌ์ž…ํ•˜๊ธฐ ์œ„ํ•ด Dummy variable๋กœ ๋ณ€ํ™˜์‹œ์ผœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋”๋ฏธ ๋ณ€์ˆ˜ ๋ณ€ํ™˜์€ ๋จผ์ € ํ•˜๋‚˜์˜ ์ˆ˜์ค€์„ ๊ธฐ์ค€์ (reference)์œผ๋กœ ์ •ํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ ์˜ˆ์‹œ์—์„œ๋Š” Male์„ ๊ธฐ์ค€์ ์œผ๋กœ ํ•ด์„œ Male ์ผ ๋•Œ๋Š” D_1์ด 0์„ ๊ฐ€์ง€๊ณ  Female์— ํ•ด๋‹น๋  ๋•Œ๋Š” 1์„ ๊ฐ€์ง€๋„๋ก ๋ณ€ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ณ€์ˆ˜๋ฅผ ๋ชจํ˜•์— ํˆฌ์ž…ํ•˜๋ฉด ๋‹ค์Œ์ฒ˜๋Ÿผ ํ•ด์„ํ•ฉ๋‹ˆ๋‹ค. i = 0 b D - Male์ผ ๊ฒฝ์šฐ : i = 0 - Female์ผ ๊ฒฝ์šฐ : i = 0 b Female์ผ ๊ฒฝ์šฐ Male ์ผ ๋•Œ๋ณด๋‹ค i ๊ฐ€ 1 ๋งŒํผ ์ฆ๊ฐ€ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ธฐ์ค€์ ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ด์„์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ๋Š” salary ๋ณ€์ˆ˜์ฒ˜๋Ÿผ 3๊ฐ€์ง€ ์ˆ˜์ค€์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ์ง„ํ–‰ํ•˜๋Š”์ง€ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. R์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Factor ๋ณ€์ˆ˜๊ฐ€ ๋ชจํ˜•์— ๋“ค์–ด์˜ค๋ฉด ์ž๋™์œผ๋กœ Dummy variable๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์ค€์ ์€ ์ฒซ ๋ฒˆ์งธ๋กœ ์ธ์‹๋œ ์ˆ˜์ค€์ž…๋‹ˆ๋‹ค. ๊ธฐ์ค€์„ ๋ฐ”๊ฟ”์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด factor ์ง€์ •์„ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. HR a a y f c o ( R salary , levels = c('low','medium','high')) ๋ถ„์„ ์ „์— ์œ„์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด salary ๋ณ€์ˆ˜์˜ level ์ˆœ์„œ๋ฅผ โ€˜lowโ€™, โ€˜mediumโ€™, โ€™highโ€™์ˆœ์œผ๋กœ ์ง€์ •ํ•ด ์คŒ์œผ๋กœ์จ low๊ฐ€ ์ž๋™์œผ๋กœ ๊ธฐ์ค€์ ์ด ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ถ„์„ ๊ฒฐ๊ณผ R์—์„œ ์ œ์‹œ๋œ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€์‹์„ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. o : o ( ^ 1 ฯ€ x ) 0.48 3.72 S t s a t o l v l 0 + 0.22 t m d u : o ( ^ 1 ฯ€ x ) 0.48 3.72 S t s a t o l v l 0.53 h g : o ( ^ 1 ฯ€ x ) 0.48 3.72 S t s a t o โˆ’ 1.98 + 0.22 t m ๊ธฐ์ค€์ ์ธ low ์ผ ๋•Œ๋Š” medium & high์— ํ•ด๋‹น๋˜๋Š” ํšŒ๊ท€ ๊ณ„์ˆ˜๋Š” ๋ชจ๋‘ 0์ด ๋ฉ๋‹ˆ๋‹ค. medium ์ผ ๋•Œ๋Š” salarymedium์˜ ํšŒ๊ท€ ๊ณ„์ˆ˜์ธ 0.53 ์ด ๋‚จ์•„ ์žˆ์–ด, logit ๊ฐ’์ด -0.53๋งŒํผ ๋‚ฎ์•„์ง‘๋‹ˆ๋‹ค. high ์ง‘๋‹จ์— ์†ํ•  ๋•Œ๋Š” low ์ง‘๋‹จ์— ๋น„ํ•ด ์ด์ง์„ ํ•  logit ๊ฐ’์ด 1.98 ๋งŒํผ ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€ satisfaction_level์€ ์ฆ๊ฐ€ํ• ์ˆ˜๋ก ์ด์งํ•  logit์€ ๋‚ด๋ ค๊ฐ€๊ฒŒ ๋˜๊ณ , i e s e d c m a y ๋Š” ์ฆ๊ฐ€ํ• ์ˆ˜๋ก ์ด์งํ•  logit์€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ณ€์ˆ˜์˜ ๊ณ„์ˆ˜์—<NAME> ๋ณ€ํ™˜์„ ํ•ด์ฃผ๋ฉด ํ•ด๋‹น ๋ณ€์ˆ˜์˜ ๋ณ€ํ™”์— ๋”ฐ๋ฅธ ์˜ค์ฆˆ๋น„ ์—ญ์‹œ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจํ˜•์˜ ์„ฑ๋Šฅ ํ‰๊ฐ€ ํšŒ๊ท€๋ถ„์„์—์„œ๋Š” 2 ๋ฅผ ํ†ตํ•ด ๋ชจํ˜•์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜์ง€๋งŒ, ์ผ๋ฐ˜์ ์œผ๋กœ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„์—์„œ๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ๋ฐฉ๋ฒ•์€ ๋ถ„๋ฅ˜ ๋ชจํ˜•์ด ์‹ค์ œ๋กœ ์–ผ๋งˆ๋‚˜ ๋งž์ท„๋Š”๊ฐ€๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งŒ๋“ค์–ด์ง„ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง‘์–ด๋„ฃ์–ด, ํ™•๋ฅ ์„ ์ถ”์ •ํ•˜๊ณ  ๊ทธ์— ๋”ฐ๋ฅธ ๋ถ„๋ฅ˜๋ฅผ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Log_odds = predict(Logistic, newdata = HR) Probability = predict(Logistic, newdata = HR, type = 'response') predict()๋Š” ๋งŒ๋“ค์–ด์ง„ ๋ชจํ˜•์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด ์ถ”์ • ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. type ์˜ต์…˜์ด ์—†์œผ๋ฉด predict์„ ํ†ตํ•ด o ( ^ 1 ฯ€ x ) ์ด ๊ณ„์‚ฐ์ด ๋ฉ๋‹ˆ๋‹ค. type = 'response' ์˜ต์…˜์„ ์ฃผ๋ฉด predict์„ ํ†ตํ•ด ^ ๊ฐ€ ๊ณ„์‚ฐ์ด ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์šฐ๋ฆฌ๊ฐ€ ๊ด€์‹ฌ์ด ์žˆ๋Š” ๊ฐ’์€ ์ง์›๋“ค์ด ์ด์ง์„ ํ•  ํ™•๋ฅ  ^ ์ด๊ธฐ์— ์˜ต์…˜์„ ์ค€ ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์œผ๋กœ๋Š” ๊ณ„์‚ฐ๋œ ^๋ฅผ ๊ฐ€์ง€๊ณ  ์ด์ง ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ด์งํ•  ํ™•๋ฅ ์ด 0.5๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์—๋Š” ์ด์ง์œผ๋กœ ํŒ๋‹จ, ๋‚˜๋จธ์ง€๋Š” ์ด์ง์„ ํ•˜์ง€ ์•Š๋Š” ์ง‘๋‹จ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ™•๋ฅ ์„ ๊ตฌ๋ถ„ ์ง“๋Š” ๊ฐ’์„ cut-off value๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. PREDICTED_C = ifelse(Probability > 0.5 , 1 , 0) PREDICTED_C = factor(PREDICTED_C, levels = c(1,0)) ๋‹ค์Œ์ฒ˜๋Ÿผ ifelse() ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ cut-off value์— ๋”ฐ๋ผ ๋ถ„๋ฅ˜๋ฅผ ์ง„ํ–‰ํ•ด ์ค๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ์œผ๋กœ๋Š” ์‹ค์ œ ๊ฐ’๊ณผ ๋ชจ๋ธ์— ์˜ํ•œ ๋ถ„๋ฅ˜ ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # install.packages(c("caret","e1071")) library(caret) HR$left = factor(HR$left, levels = c(1,0)) confusionMatrix(PREDICTED_C, HR$left) Confusion Matrix and Statistics Reference Prediction 1 0 1 947 872 0 2624 10556 Accuracy : 0.7669 95% CI : (0.7601, 0.7737) No Information Rate : 0.7619 P-Value [Acc > NIR] : 0.07635 Kappa : 0.2272 Mcnemar's Test P-Value : < 2e-16 Sensitivity : 0.26519 Specificity : 0.92370 Pos Pred Value : 0.52062 Neg Pred Value : 0.80091 Prevalence : 0.23808 Detection Rate : 0.06314 Detection Prevalence : 0.12127 Balanced Accuracy : 0.59444 'Positive' Class : 1 ์‹ค์ œ ๊ฐ’๊ณผ ๋ชจ๋ธ์— ์˜ํ•œ ๋ถ„๋ฅ˜ ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ํ…Œ์ด๋ธ”์„ Confusion Matrix๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. True Positive & True Negative๋Š” ๋ชจํ˜• ์˜ˆ์ธก๊ฐ’์ด ์‹ค์ œ ๊ฐ’์„ ๋งž์ถ˜ ๊ฒฝ์šฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. False Negative & False Positive๋Š” ๋ชจํ˜• ์˜ˆ์ธก๊ฐ’์ด ์‹ค์ œ ๊ฐ’์„ ๋งž์ถ”์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. Accuracy : P T T + P F + N : ์ „์ฒด ์ •ํ™•๋„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Sensitivity(๋ฏผ๊ฐ๋„) : P P F :์‹ค์ œ Positive ์ค‘์—์„œ ๋ชจํ˜•์ด Positive๋ฅผ ๋งž์ถ”์—ˆ๋Š”๊ฐ€์— ๋Œ€ํ•œ ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. Specificity(ํŠน์ด๋„) : P P T : ์‹ค์ œ Negative ์ค‘์—์„œ ๋ชจํ˜•์ด Negative๋ฅผ ๋งž์ถ”์—ˆ๋Š”๊ฐ€์— ๋Œ€ํ•œ ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. ์ „์ฒด ์ •ํ™•๋„๋งŒ ๋ณด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  ๋ฏผ๊ฐ๋„ ๋ฐ ํŠน์ด๋„๋ฅผ ๋ณด๋Š” ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทน๋‹จ์ ์ธ ๊ฒฐ๊ณผ์ง€๋งŒ, ๋ถ„์„์„ ์ž˜๋ชป ๋Œ๋ฆฐ ๊ฒฝ์šฐ ์ด๋Ÿฌํ•œ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊ฐ€ ์ข…์ข… ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ถ„์„ ๊ฒฐ๊ณผ์— ๋”ฐ๋ฅด๋ฉด Accuracy๋Š” 98%๋กœ ๋งค์šฐ ๋†’์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Specificiy๋Š” 0.1๋„ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ถ„์„ ๋ชจํ˜•์€ ํฌ๊ฒŒ ์˜๋ฏธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋ชจํ˜•์ด ๋ฐ์ดํ„ฐ๋ฅผ Neagtive๋กœ ๋ถ„๋ฅ˜ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ฐ ์‚ฐ์—… ๊ตฐ๋งˆ๋‹ค ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณ‘์›์—์„œ ํ™˜์ž์˜ ์งˆ๋ณ‘์„ ํŒ๋‹จํ•˜๋Š” ๋ถ„๋ฅ˜ ๋ชจํ˜•์„ ๋งŒ๋“ค์–ด ํ•ด๋‹น ๋ชจํ˜•์˜ ์ •ํ™•๋„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ์—, False Positive : ์‹ค์ œ๋กœ Negative์ธ๋ฐ Positive๋กœ ์˜ค ๋ถ„๋ฅ˜ํ•œ ๊ฒฝ์šฐ (๋ณ‘์ด ์—†๋Š”๋ฐ ๋ณ‘์ด ์žˆ๋‹ค๊ณ  ๋ถ„๋ฅ˜) False Negative : ์‹ค์ œ๋กœ Positive์ธ๋ฐ Negative๋กœ ์˜ค ๋ถ„๋ฅ˜ํ•œ ๊ฒฝ์šฐ (๋ณ‘์ด ์žˆ๋Š”๋ฐ ๋ณ‘์ด ์—†๋‹ค๊ณ  ๋ถ„๋ฅ˜) ์งˆ๋ณ‘์„ ๋ฐœ๊ฒฌ ๋ชปํ•˜๋Š” ์‹ค์ˆ˜๋Š” ๋Œ์ดํ‚ฌ ์ˆ˜๊ฐ€ ์—†๊ธฐ์—, ๋ณ‘์›์—์„œ๋Š” False Negative๊ฐ€ False Positive๊ฐ€ ๋” ์ค‘์š”ํ•œ ๊ฐ’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Confusion Matrix์˜ ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ด Accuracy, Sensitivity, Specificity์˜ ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•˜๋‚˜ ์ฃผ์˜ํ•  ์ ์€, ์œ„์—์„œ cut-off value๋ฅผ 0.5๋กœ ํ•˜์—ฌ ๋ถ„๋ฅ˜ํ–ˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด cut-off value๊ฐ€ ์ค‘์š”ํ•œ ์ด์œ ๋Š” ๊ทธ ๊ฐ’์— ๋”ฐ๋ผ ๋ถ„๋ฅ˜๊ฐ€ ์ฒœ์ฐจ๋งŒ๋ณ„์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋ชจ๋“  cut-off value๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๊ฒฐ๊ด๊ฐ’์˜ ๋ณ€ํ™”๋ฅผ ์‚ดํŽด๋ด์•ผ ํ•˜๋Š”๋ฐ, ๊ทธ ๋ฐฉ๋ฒ•์„ ROC curve๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Roc Curve library(pROC) HR$left = factor(HR$left, levels = c(0,1)) ROC = roc(HR$left, Probability) plot.roc(ROC, col="royalblue", print.auc=TRUE, max.auc.polygon=TRUE, print.thres=TRUE, print.thres.pch=19, print.thres.col = "red", auc.polygon=TRUE, auc.polygon.col="#A0A0A0") Roc Curve์˜ y ์ถ•์€ Sensitivity์ด๋ฉฐ, x์ถ•์€ Specificity์ž…๋‹ˆ๋‹ค. X์ถ•์ด 1 ~ 0 ์ˆœ์„œ๋กœ ๊ทธ๋ ค์ ธ ์žˆ๋Š” ๊ฒƒ์„ ์ฃผ์˜ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. Roc Curve๋Š” cut off value์˜ ๊ฐ’์— ๋”ฐ๋ผ Sensitivity์™€ Specificity์˜ ๋ณ€ํ™”๋Ÿ‰์„ ๋‚˜ํƒ€๋‚ธ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. AUC๋Š” Area under curve์˜ ๋ฏธ๋กœ, ๊ณก์„ ์— ํ•ด๋‹น๋˜๋Š” ๋ฉด์ ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. AUC ๊ฐ’์ด ๋†’์„์ˆ˜๋ก ๋ฐ”๋žŒ์งํ•œ ๋ชจํ˜•์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ข‹์€ ๊ฒฐ๊ด๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” cut off value ๊ฐ’ ๋ฐ sensitivity, specificity ์—ญ์‹œ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„์˜ ์ด๋ก  ๋ฐ ์‹ค์Šต๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃจ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜ ๋ชจํ˜•์€ ์ด๋Ÿฐ ๋กœ์ง€์Šคํ‹ฑ ๊ธฐ๋ฒ•์ด ๊ธฐ๋ณธ ์•„์ด๋””์–ด์ด๊ธด ํ•˜์ง€๋งŒ, ์ •ํ™•ํ•œ ๋ถ„์„์„ ์œ„ํ•ด์„œ๋Š” Train / Test SET์„ ํ†ตํ•œ ํƒ€๋‹น์„ฑ ๊ฒ€์ฆ, ์ข‹์€ ๋ณ€์ˆ˜ ์„ ํƒ๋ฒ• ๋ฐ ๊ธฐ๊ณ„ํ•™์Šต ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ์˜ ๋น„๊ต ๋ถ„์„ ๋“ฑ์ด ํ•จ๊ป˜ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ๋ถ€ํ„ฐ๋Š” ํ•ด๋‹น ๋‚ด์šฉ์„ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ChB4. ๊ธฐ์ดˆํ†ต๊ณ„ ์ด๋ก  3๋‹จ๊ณ„ ์ตœ์†Œ์ œ๊ณฑ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ์ผ๋ฐ˜์  ํšŒ๊ท€๋ถ„์„๊ณผ ๋‹ฌ๋ฆฌ GLM์—์„œ๋Š” ๊ฐ€์ •๋œ ๋ถ„ํฌํ•˜์—์„œ ๋ชจํ˜•์„ ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ตœ๋Œ€ ๊ฐ€๋Šฅ๋„ ๋ฒ•์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ ๊ฐ€๋Šฅ๋„(likelihood)๋ผ๋Š” ๊ฐ€๋Šฅ์„ฑ์˜ ๊ฐœ๋…์„ ์ด์šฉํ•œ ์ถ”์ • ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด ๊ฐ€๋Šฅ ๋„๋ผ๋Š” ๊ฐœ๋…์€ ๋ถ„ํฌ ๊ฐ€์ •๋งŒ ํ•ฉ๋ฆฌ์ ์ด๋ผ๋ฉด ๋งค์šฐ ํŒŒ์›Œํ’€ํ•˜๊ณ  ์œ ์šฉํ•œ ๊ฐœ๋…์œผ๋กœ ํ†ต๊ณ„ ์ „์ฒด๋ฅผ ์•„์šฐ๋ฅด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์žฅ์—์„œ๋Š” ๊ฐ€๋Šฅ๋„์— ๋Œ€ํ•œ ๊ฐœ๋…๊ณผ ๊ฐ€๋Šฅ๋„๋ฅผ ์ด์šฉํ•œ ๋ณ€์ˆ˜ ์„ ํƒ(๋ชจํ˜• ์„ ํƒ) ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค. A1. ๊ฐ€๋Šฅ ๋„์™€ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ 1. ๊ฐ€๋Šฅ ๋„์™€ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ€๋Šฅ๋„(likelihood)๋Š” ๊ฐ€๋Šฅ์„ฑ ํ˜น์€ ๊ณต์‚ฐ์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ๊ธˆ ๋” ํ’€์–ด์„œ ๋ง์”€๋“œ๋ฆฌ๋ฉด ๊ฐ€์ •๋œ ๋ถ„ํฌ์—์„œ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚˜์˜ฌ ๊ฐ€๋Šฅ์„ฑ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ( , 2 ) ๋ผ๋Š” ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅผ ๊ฒƒ์œผ๋กœ ๊ฐ€์ •๋˜๋Š” ๋ชจ์ง‘๋‹จ์—์„œ ์ถ”์ถœ๋œ ํ‘œ๋ณธ๋“ค์„ ์–ป์—ˆ์„ ๋•Œ, ๊ทธ ํ‘œ๋ณธ ๊ฐ’๋“ค๊ณผ ์ •๊ทœ๋ถ„ํฌ์˜ ํ™•๋ฅ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ํ‰๊ท ์ด์ด๊ณ  ๋ถ„์‚ฐ์ด 2 ๊ฐ€ ๋งž์„ ๊ฐ€๋Šฅ์„ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€๋Šฅ์„ฑ์ด๋ผ๋Š” ๊ฒƒ์€ ๊ฒฐ๊ตญ ์ฃผ์–ด์ง„ ํ‘œ๋ณธ๋“ค์—์„œ ํ™•๋ฅ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์‚ฐ์ถœ๋˜๋ฏ€๋กœ ์กฐ๊ฑด๋ถ€ ํ™•๋ฅ  ๊ฐ’์ด๋ผ๋Š” ๊ด€์ ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๊ณ  ๋ฐ์ดํ„ฐ๊ฐ€ ์ฃผ์–ด์ง„ ๋ชจ๋“  ํ™˜๊ฒฝ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋Šฅ ๋„๋ผ๋Š” ๊ฐœ๋…์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ™•๋ฅ ๊ณผ์˜ ์ฐจ์ด์ ์„ ์ดํ•ดํ•˜๋ฉด ์‰ฝ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ๋™์ „ ๋˜์ง€๊ธฐ ์‹คํ—˜์„ 10๋ฒˆ ์ง„ํ–‰ํ–ˆ์„ ๋•Œ ์•ž๋ฉด์ด 4๋ฒˆ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ •ํ–ˆ์„ ๋•Œ ํ™•๋ฅ  : ์•ž๋ฉด์ด ๋‚˜์˜ฌ ํ™•๋ฅ ์€ 0.4์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํ™•๋ฅ ์€ ํ™•๋ฅ  ์‹คํ—˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ง‘๊ณ„ํ•œ ๊ฒƒ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋Šฅ๋„ : ๋™์ „์„ 10๋ฒˆ ๋˜์กŒ์„ ๋•Œ ์•ž๋ฉด์ด ๋‚˜์˜ฌ ํ™•๋ฅ ์— ๋”ฐ๋ผ์„œ ์•ž๋ฉด์ด 4๋ฒˆ ๋‚˜์˜ฌ ๊ฐ€๋Šฅ์„ฑ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ€๋Šฅ๋„ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜(Likelihood function)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋™์ „ ๋˜์ง€๊ธฐ ์‹คํ—˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋™์ „ ๋˜์ง€๊ธฐ ์‹คํ—˜์€ ์ดํ•ญ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋‹ˆ ํ™•๋ฅ  ์งˆ๋Ÿ‰ ํ•จ์ˆ˜( m)๋Š” C p ( โˆ’ ) โˆ’์ž…๋‹ˆ๋‹ค. ์œ„์˜ ํ‘œ์ฒ˜๋Ÿผ ์•ž๋ฉด์ด ๋‚˜์˜ฌ ํ™•๋ฅ ์— ๋”ฐ๋ผ 10๋ฒˆ ์ค‘ 4๋ฒˆ์ด ์•ž๋ฉด์ด ๋‚˜์˜ฌ ๊ฐ€๋Šฅ์„ฑ์„ ๊ตฌํ•œ ๊ฒƒ์ด ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜(Likelihood function)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋Šฅ ํ•จ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ€์ •๋œ ๋ถ„ํฌ, ์กฐ์‚ฌ๋œ ํ‘œ๋ณธ์ด ํ•„์š”ํ•˜๊ณ  ์กฐ๊ฑด๋ถ€์  ์„ฑ๊ฒฉ์„ ์ œ์™ธํ•˜๋ฉด ํ™•๋ฅ ํ•จ์ˆ˜์™€ ์ •ํ™•ํžˆ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๊ด€์ ์„ ๋‹ฌ๋ฆฌ๋Š” ๊ฒƒ๋ฟ์ž…๋‹ˆ๋‹ค. ํ™•๋ฅ ํ•จ์ˆ˜์—์„œ ํ™•๋ฅ ๋ณ€์ˆ˜๋Š” ์ด๋ฏธ ์กฐ์‚ฌ๋˜์–ด ์ฃผ์–ด์ง„ ๊ฐ’์ด ๋˜๊ณ  ์ •ํ•ด์ ธ์žˆ๋˜ ๋ชจ์ˆ˜๊ฐ€ ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ชจ์ง‘๋‹จ์˜ ํ™•๋ฅ ํ•จ์ˆ˜๊ฐ€๋ผ๊ณ  ์•Œ๋ ค์ ธ ์žˆ๊ณ  ๊ทธ ํ™•๋ฅ ํ•จ์ˆ˜์˜ ๋ชจ์ˆ˜๋ฅผ ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด ํ™•๋ฅ ํ•จ์ˆ˜๋ฅผ ( ; ) ๋ผ๊ณ  ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ชจ์ง‘๋‹จ์—์„œ๋ผ๋Š” ํ‘œ๋ณธ์ด ๋‚˜์™”๋‹ค๊ณ  ํ•˜๋ฉด ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋ฉ๋‹ˆ๋‹ค. ( ; ) f ( | ) ์ด๋Š” ํ‘œ๋ณธ์ด ํ•œ ๊ฐœ๊ฐ€ ์•„๋‹ˆ์—ˆ์„ ๋•Œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๋…๋ฆฝ์ ์œผ๋กœ ์ถ”์ถœ๋œ ํ‘œ๋ณธ์ด 1 y, y์ด๋ผ๊ณ  ํ•˜๋ฉด ๊ฐ ํ‘œ๋ณธ๋“ค์˜ ๋ชจ์ง‘๋‹จ์˜ ํ™•๋ฅ ํ•จ์ˆ˜๋Š” ์ „๋ถ€ ( ; ) ๊ฐ€ ๋  ๊ฒƒ์ด๊ณ  ๋…๋ฆฝ์ ์œผ๋กœ ์ถ”์ถœ๋˜์—ˆ์œผ๋ฏ€๋กœ ํ™•๋ฅ ์˜ ๊ณฑ๋ฒ•์น™์— ์˜ํ•ด ๊ฒฐํ•ฉ ํ™•๋ฅ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, i f ( i ฮธ ) ์™€ ๊ฐ™์ด ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐœ์˜ ํ‘œ๋ณธ์—์„œ์˜ ์šฐ๋„ ํ•จ์ˆ˜๋„ ์–ด๋ ต์ง€ ์•Š๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( ; 1 y, , n ) โˆ = n ( | 1 y, , n ) ๋˜ํ•œ ์‹ค์ œ๋กœ ํ™œ์šฉํ•  ๋•Œ๋Š” ๊ณ„์‚ฐ๊ณผ ํŽธ์˜๋ฅผ ์œ„ํ•ด ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜์— ๋กœ๊ทธํ•จ์ˆ˜๋ฅผ ์”Œ์›Œ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜(log-likelihood function)๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ ์†Œ๋ฌธ์ž๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋กœ๊ทธํ•จ์ˆ˜๋Š” ๋‹จ์กฐ์ฆ๊ฐ€ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์ด ์ปค์ง€๋ฉด ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’๋„ ํ•ญ์ƒ ์ปค์ง‘๋‹ˆ๋‹ค. A2. ์ตœ๋Œ€ ๊ฐ€๋Šฅ๋„ ์ถ”์ •๋Ÿ‰(Maximum Likelihood Estimation, MLE) 2. ์ตœ๋Œ€ ๊ฐ€๋Šฅ๋„ ์ถ”์ •๋Ÿ‰(Maximum Likelihood Estimation, MLE) ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€ ๋ชจํ˜•์„ ๋‹ค๋ฃฐ ๋•Œ, ๋กœ์ง€์Šคํ‹ฑ ๋ชจํ˜•๊ณผ ๊ฐ™์€ GLM์€ ์ตœ์†Œ์ œ๊ณฑ๋ฒ•์ด ์•„๋‹Œ ์ตœ๋Œ€ ๊ฐ€๋Šฅ๋„ ์ถ”์ • ๋ฒ•์„ ์ด์šฉํ•œ๋‹ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ตœ๋Œ€ ๊ฐ€๋Šฅ๋„ ์ถ”์ •๋Ÿ‰์— ์˜ํ•ด ์ถ”์ •๋œ ์ถ”์ •๋Ÿ‰์„ ์ตœ๋Œ€ ๊ฐ€๋Šฅ๋„ ์ถ”์ •๋Ÿ‰(maximum likelihood estimator)์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ ํ”ํžˆ ์ค„์—ฌ์„œ MLE๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. MLE๋Š” ์—ญ์‹œ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜์˜ ๊ฐœ๋…๋งŒ ์ž˜ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ์–ด๋ ต์ง€ ์•Š์€ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๊ฐ€์ •๋œ ํ™•๋ฅ ๋ถ„ํฌ์˜ ๋ชจ์ˆ˜ ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋งŽ์€ ํ›„๋ณด๋“ค ์ค‘ ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜(ํ˜น์€ ์šฐ๋„ ํ•จ์ˆ˜)๋ฅผ ์ตœ๋Œ€๋กœ ํ•˜๋Š” ํ›„๋ณด๋ฅผ ๋ชจ์ˆ˜์˜ ์ถ”์ •๋Ÿ‰์œผ๋กœ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์ด ํฌ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ๋งŒํผ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ์ ํ•ฉํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์— MLE๋Š” ์ถฉ๋ถ„ํžˆ ํ•ฉ๋ฆฌ์ ์ธ ์ถ”์ •๋Ÿ‰์ž…๋‹ˆ๋‹ค. GLM์—์„œ๋„ ๊ธฐ์šธ๊ธฐ์™€ ์ ˆํŽธ ์—ญ์‹œ ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐ€์ •๋œ ๋ถ„ํฌ์™€ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์—์„œ ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜๋ฅผ ๊ฐ€์žฅ ํฌ๊ฒŒ ํ•˜๋Š” ๊ฐ’์œผ๋กœ ์ ํ•ฉ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ•ด์ง„ L๋Š” ์ด ๋งŽ์•„์ง์— ๋”ฐ๋ผ ํ†ต๊ณ„๊ฒ€์ •์— ์‚ฌ์šฉ๋˜๋Š” ์ •๊ทœ ๊ทผ์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉฐ ๊ทธ ์™ธ์—๋„ ํšจ์œจ์„ฑ, ๋ถˆํŽธ์„ฑ์ด๋ผ๋Š” ์ข‹์€ ์„ฑ์งˆ๋“ค์ด ์ƒ๊ธฐ๊ฒŒ ๋˜์–ด ๋งค์šฐ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์•ž์„œ ๋‹ค๋ฃฌ ๋™์ „ ๋˜์ง€๊ธฐ ์‹คํ—˜ ์˜ˆ์‹œ๋ฅผ ๋“ค๋ฉด 10๋ฒˆ ์ค‘ 4๋ฒˆ์ด ๋‚˜์˜ฌ ๊ฐ€๋Šฅ์„ฑ์ด ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์€ 0.4์ผ ๋•Œ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ L๋Š” 0.4๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. A3. ๊ฐ€๋Šฅ๋„์— ๋”ฐ๋ฅธ ๋ณ€์ˆ˜ ์„ ํƒ๋ฒ• 3. ๊ฐ€๋Šฅ๋„์— ๋”ฐ๋ฅธ ๋ณ€์ˆ˜ ์„ ํƒ๋ฒ• ์œ„์—์„œ ๋‹ค๋ค˜๋‹ค์‹œํ”ผ ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์€ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๊ฐ€๋Šฅ์„ฑ์˜ ์ฒ™๋„์ด๊ณ  '์ ํ•ฉ๋„(goodness of fit)'์˜ ๊ด€์ ์œผ๋กœ ๋ฐ”๋ผ๋ณด๋ฉด MLE๋Š” ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ ํ•ฉ๋„๋ฅผ ๋†’๊ฒŒ ํ•˜๋Š” ์ถ”์ •๋Ÿ‰์ด๋ผ๊ณ  ๋ง์”€๋“œ๋ ธ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจํ˜•์—๋„ ๊ทธ๋Œ€๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. GLM์€ ๋ชจํ˜•์˜ ๋ชจ์ˆ˜๋ฅผ ์ตœ๋Œ€ ๊ฐ€๋Šฅ๋„ ๋ฒ•์œผ๋กœ ๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ ํ•ฉ๋„๊ฐ€ ๋” ๋†’์„์ˆ˜๋ก ๋” ๋†’์€ ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹จ์ˆœํžˆ ๋งํ•ด์„œ ๋กœ๊ทธ ์šฐ๋„ ํ•จ์ˆ˜ ๊ฐ’์ด ๋†’์„์ˆ˜๋ก ์ถ”์ •๋œ ์ง์„  ํ˜น์€ ๊ณก์„ ์ด ๋ฐ์ดํ„ฐ๋“ค์„ ๊ฐ€๊น๊ฒŒ ์ง€๋‚˜๊ฐ„๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์„ ํƒ๋œ ๋ชจํ˜•๋งˆ๋‹ค ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ๊ฐ’์€ ๋‹ค ๋‹ค๋ฅผ ๊ฒƒ์ด๊ณ  ๋ชจํ˜• ์ ํ•ฉ์— ์‚ฌ์šฉ๋œ ์˜ˆ์ธก ์ž๋งˆ๋‹ค ๋„ ์ „๋ถ€ ๋‹ค๋ฅผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด ์–ด๋–ค ๋ชจํ˜•์„ ์ ์šฉํ• ์ง€ ์˜ˆ์ธก์ž์˜ ์กฐํ•ฉ๋ณ„๋กœ ๊ฐ€์žฅ ์ตœ์ ์˜ ๋ชจํ˜•์„ ์„ ํƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์ด ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ๋งŒ ๋ณด๊ณ  ๋ฌด์กฐ๊ฑด ์ข‹์€ ๋ชจํ˜•์ด๋ผ๊ณ  ํŒ๋‹จํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ฐ”๋กœ ๋ณ€์ˆ˜๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด ํ•ญ์ƒ ๋†’์•„์ง€๋Š” ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜์˜ ๊ตฌ์กฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜๋Š” ์˜ˆ์ธก์ž๊ฐ€ ๋Š˜์–ด๋‚ ์ˆ˜๋ก ๋‚ฎ์•„์ง€์ง€๋Š” ์•Š๊ณ  ํ•ญ์ƒ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์˜ํ–ฅ๋„์— ๋”ฐ๋ผ ๋”ํ•ด์ง€๋Š” ๊ฐ’์ด ๋‹ค๋ฅผ ๋ฟ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์˜ˆ์ธก์ž๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์„ ๋•Œ ์ฆ๊ฐ€๋˜๋Š” ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์ด ์œ ์˜๋ฏธํ•œ์ง€๋ฅผ ๋ณด์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์œ ์˜ํ•œ ์ฆ๊ฐ€๋ฅผ ๋ณด์—ฌ์ฃผ์ง€ ๋ชปํ•จ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์˜ˆ์ธก์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ๊ตณ์ด ํ•„์š” ์—†๋Š” ์ถ”์ •์„ ํ•œ ๋ฒˆ ๋” ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ถˆํ•„์š”ํ•œ ์ถ”์ •๊ณผ ๋‹ค๋ฅธ ์˜ˆ์ธก์ž์™€์˜ ๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ๋ชจํ˜•์€ ์ตœ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ชจํ˜•์˜ ๋ชจ์ˆ˜ ์ ˆ์•ฝ์˜ ์›์น™์ด๋ผ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ๋ชจํ˜•์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š” ์ฒ™๋„๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ์ดํƒˆ๋„(deviance)๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๋‘ ๋ชจํ˜•์˜ ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์˜ ์ฐจ์ด๊ฐ€ ์œ ์˜ํ•œ์ง€ ๋ณด๋Š” ๊ฒƒ๊ณผ ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’๊ณผ ๋ชจํ˜•์— ์‚ฌ์šฉ๋œ ๋ชจ์ˆ˜์˜ ์ˆ˜๋ฅผ ๋™์‹œ์— ๊ณ ๋ คํ•œ AIC๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ค‘์— ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” AIC์— ๋Œ€ํ•ด ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค. AIC๋Š” ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์ด ๋†’์œผ๋ฉด ๊ฐ€์‚ฐ์ ์„ ์ฃผ๊ณ  ๋ชจํ˜•์— ์‚ฌ์šฉ๋œ ๋ชจ์ˆ˜๊ฐ€ ๋งŽ์œผ๋ฉด ํŽ˜๋„ํ‹ฐ๋ฅผ ์ฃผ๋Š”<NAME>์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์ฒ™๋„๋กœ ์ž‘์œผ๋ฉด ์ž‘์„์ˆ˜๋ก ๋ฐ”๋žŒ์งํ•œ ๋ชจํ˜•์ด๋ผ๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. I = 2 ( o l k l h o โˆ’ u b r f a a e e s n o ์˜ˆ๋ฅผ ๋“ค์–ด, 2๊ฐœ์˜ ๋ชจํ˜• ๋ชจ์ˆ˜๋ฅผ ์ด์šฉํ•œ ๋ชจํ˜•์˜ ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์ด-16์ด๊ณ  4๊ฐœ์˜ ๋ชจ์ˆ˜๋งŒ์„ ์ด์šฉํ•œ ๋ชจํ˜•์˜ ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์€ -15์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ ๊ฐ ๋ชจํ˜•์˜ AIC๋Š” ๊ฐ๊ฐ 36๊ณผ 38์ž…๋‹ˆ๋‹ค. ๋น„๋ก ๋กœ๊ทธ ๊ฐ€๋Šฅ๋„ ํ•จ์ˆ˜ ๊ฐ’์€ ๋‘ ๋ฒˆ์งธ ๋ชจํ˜•์ด ๋” ์ปธ์ง€๋งŒ ๋ชจํ˜•์— ์‚ฌ์šฉ๋œ ๋ชจ์ˆ˜๊ฐ€ ๋” ๋งŽ์•„ ์ฒซ ๋ฒˆ์งธ ๋ชจํ˜•์•  ๋น„ํ•ด ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š๋‹ค๋Š” ํŒ๋‹จ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. AIC๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ์ ˆ๋Œ€์ ์ธ ๊ธฐ์ค€์€ ์—†์Šต๋‹ˆ๋‹ค. ์˜ค์ง ๋ชจํ˜• ๋น„๊ต ํ†ต์‹œ์—๋งŒ ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•œ ์ฒ™๋„์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ—Œ deviance ํ˜น์€ AIC๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ๊ฐ์˜ ์˜ˆ์ธก์ž๋ฅผ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ ๊ทธ ์˜ˆ์ธก์ž๊ฐ€ ๊ณผ์—ฐ ํ•„์š”ํ•œ ๊ฒƒ์ธ๊ฐ€๋ฅผ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ณ€์ˆ˜ ์„ ํƒ์ด๋ผ ํ•˜๋ฉฐ ๊ทธ ๋ฐฉ๋ฒ•์œผ๋กœ ํฌ๊ฒŒ 3๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „์ง„ ์„ ํƒ๋ฒ•(Forward) ์ „์ง„ ์„ ํƒ๋ฒ•์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” ํฌ์›Œ๋“œ ๋ฐฉ๋ฒ•์€ ๊ธฐ๋ณธ ๋ชจํ˜•(basic model)์—์„œ๋ถ€ํ„ฐ ๊ฐ€์žฅ ํฐ ์˜ํ–ฅ์„ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์ธก์ž๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋ชจํ˜•์— ๋„ฃ์–ด์„œ ๊ทธ ์œ ์˜๋ฏธํ•จ์„ ํŒ๋‹จํ•œ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„ ์ถ”๊ฐ€๋œ ์˜ˆ์ธก์ž๊ฐ€ ๋” ์ด์ƒ ์œ ์˜ํ•˜์ง€ ์•Š๋‹ค๋ฉด ์ œ๊ฑฐ ํ›„ ํฌ์›Œ๋“œ ๋ฐฉ๋ฒ•์„ ๋ฉˆ์ถ”๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ›„์ง„ ์ œ๊ฑฐ๋ฒ•(Backward) ํ›„์ง„ ์ œ๊ฑฐ๋ฒ•์ธ ๋ฐฑ ์›Œ๋“œ ๋ฐฉ๋ฒ•์€ ํฌ์›Œ๋“œ์™€๋Š” ๋ฐ˜๋Œ€๋กœ ๊ฐ€์žฅ ๋ณต์žกํ•œ ๋ชจํ˜•(full model)์—์„œ ์‹œ์ž‘์œผ๋กœ ๊ฐ€์žฅ ์œ ์˜๋ฏธํ•˜์ง€ ์•Š๋Š” ์˜ˆ์ธก ์ž๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ œ๊ฑฐํ•ด ๋‚˜๊ฐ‘๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„ ์ œ๊ฑฐ๋œ ์˜ˆ์ธก์ž๊ฐ€ ์œ ์˜๋ฏธํ•œ ๊ฒƒ์œผ๋กœ ํŒ๋‹จ๋˜๋ฉด ์ถ”๊ฐ€ ํ›„ ๋ฐฑ ์›Œ๋“œ ๋ฐฉ๋ฒ•์„ ๋ฉˆ์ถ”๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹จ๊ณ„๋ณ„ ์„ ํƒ๋ฒ•(Stepwise) ์Šคํ… ์™€์ด์ฆˆ๋Š” ๋‘ ๋ฐฉ๋ฒ•์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ, ์ „์ง„ ์„ ํƒ์„ ํ•˜๋ฉด์„œ ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค ํ›„์ง„ ์ œ๊ฑฐ๋ฅผ ํ•  ๊ฒƒ์ธ์ง€ ์ฒดํฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํฌ์›Œ๋“œ์™€ ๋ฐฑ ์›Œ๋“œ๋Š” ํ•œ ๋ฒˆ ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ์ œ๊ฑฐ๋œ ์˜ˆ์ธก์ž์— ๋Œ€ํ•ด์„œ๋Š” ๋” ์ด์ƒ ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š๋Š” ๋ฐ˜๋ฉด ์Šคํ… ์™€์ด์ฆˆ ๋ฐฉ๋ฒ•์€ ์ƒˆ๋กœ์šด ์˜ˆ์ธก์ž๊ฐ€ ๋“ค์–ด๊ฐ”์„ ๋•Œ ์ „ ๋‹จ๊ณ„์—์„œ ๋ชจํ˜•์— ํฌํ•จ๋œ ์˜ˆ์ธก์ž๊ฐ€ ๋ฌด์˜๋ฏธํ•ด์ง„๋‹ค๋ฉด ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์˜ˆ์ธก์ž ์‚ฌ์ด์˜ ๊ด€๊ณ„ ๋•Œ๋ฌธ์œผ๋กœ ์›๋ž˜๋Š” ์œ ์˜ํ–ˆ๋˜ ์˜ˆ์ธก์ž๊ฐ€ ๋‹ค๋ฅธ ์˜ˆ์ธก์ž๊ฐ€ ํฌํ•จ๋จ์— ๋”ฐ๋ผ ๊ทธ ์˜ํ–ฅ๋„๊ฐ€ ๋ฐ”๋€” ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฐ์— ์ฐฉ์•ˆํ•ด ๊ณ ์•ˆ๋œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ์„ ํƒ๋ฒ•์„ R์—์„œ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. x1 = runif(n = 1000, min = -10, max = 10) x2 = runif(n = 1000, min = -10, max = 10) x3 = runif(n = 1000, min = -10, max = 10) x4 = runif(n = 1000, min = -10, max = 10) x5 = runif(n = 1000, min = -10, max = 10) y = 0.1 * x1 - 0.7 *x3 + runif(n = 1000, min = -1, max = 1) Reg = step(lm(y ~ x1 + x2 + x3 + x4 + x5),direction = "backward") Start: AIC=-1046.68 y ~ x1 + x2 + x3 + x4 + x5 Df Sum of Sq RSS AIC - x4 1 0.2 347.1 -1048.14 - x2 1 0.4 347.3 -1047.52 - x5 1 0.6 347.6 -1046.83 <none> 346.9 -1046.68 - x1 1 314.5 661.4 -403.35 - x3 1 16126.2 16473.1 2811.73 Step: AIC=-1048.14 y ~ x1 + x2 + x3 + x5 Df Sum of Sq RSS AIC - x2 1 0.4 347.5 -1048.98 - x5 1 0.6 347.7 -1048.33 <none> 347.1 -1048.14 - x1 1 314.4 661.5 -405.31 - x3 1 16131.5 16478.6 2810.06 Step: AIC=-1048.98 y ~ x1 + x3 + x5 Df Sum of Sq RSS AIC - x5 1 0.6 348.1 -1049.16 <none> 347.5 -1048.98 - x1 1 316.3 663.8 -403.72 - x3 1 16147.5 16495.0 2809.06 Step: AIC=-1049.16 y ~ x1 + x3 Df Sum of Sq RSS AIC <none> 348.1 -1049.16 - x1 1 315.9 664.0 -405.49 - x3 1 16152.1 16500.2 2807.37 summary(Reg) Call: lm(formula = y ~ x1 + x3) Residuals: Min 1Q Median 3Q Max -1.02878 -0.51611 -0.01759 0.51527 1.02430 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.001265 0.018693 -0.068 0.946 x1 0.097125 0.003229 30.076 <2e-16 *** x3 -0.701708 0.003263 -215.074 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 0.5909 on 997 degrees of freedom Multiple R-squared: 0.979, Adjusted R-squared: 0.9789 F-statistic: 2.322e+04 on 2 and 997 DF, p-value: < 2.2e-16 lm() ๋ช…๋ น์–ด์— step()์„ ๋ฎ์–ด์ฃผ๋ฉด ์ž๋™์œผ๋กœ ๋ณ€์ˆ˜ ์„ ํƒ๋ฒ•์— ๋”ฐ๋ผ ๋ณ€์ˆ˜ ์„ ๋ณ„์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ์„ ํƒ๋ฒ•์€ ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋งŒ ๋ฝ‘์•„์ฃผ๋Š” ๋งค์šฐ ํšจ๊ณผ์ ์ธ ๋ฐฉ๋ฒ•์ด์ง€๋งŒ, ๋ณ€์ˆ˜๋“ค ๊ฐ„์˜ ์„ ํ˜• ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜๋Š” ๋‹ค์ค‘๊ณต ์„ ์„ฑ์„ ํ†ต์ œํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์žฅ์—์„œ๋Š” ๊ณ ์ฐจ์›์—์„œ ๋‹ค์ค‘๊ณต ์„ ์„ฑ์„ ์ œ์–ดํ•˜๋ฉฐ ์ฐจ์› ์ถ•์†Œ๋ฅผ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A4. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„ 4. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„ ๋Œ€๋ถ€๋ถ„์˜ ํ†ต๊ณ„๋ถ„์„์€ ์˜ˆ์ธก์ž๋“ค์ด ์„œ๋กœ ๋…๋ฆฝ์ด๋ผ๋Š” ๊ธฐ๋ณธ์ ์ธ ๊ฐ€์ •์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์ธก์ž๋ฅผ '๋…๋ฆฝ๋ณ€์ˆ˜'๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•˜๋ฉฐ ๊ฐ ์˜ˆ์ธก์ž๋“ค์˜ ํ†ต๊ณ„์ ์ธ ๋ถ„ํฌ๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์‹ค์ œ๋กœ๋Š” ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ์˜ˆ์ธก์ž๋“ค์€ ํฌ๊ณ  ์ž‘์€ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ๊ทธ ์ •๋„๊ฐ€ ์‹ฌํ•˜๋ฉด ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์‹ ๋ขฐํ•  ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์— ๋†“์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ๋‹ค์ค‘๊ณต ์„ ์„ฑ(multicollinearity)์ด๋ผ ๋ถ€๋ฅด๋ฉฐ ๋‹ค์ค‘๊ณต ์„ ์„ฑ์ด ์กด์žฌํ•จ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋ชจํ˜•์„ ์ ํ•ฉํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ ์˜ˆ์ธก์ž์˜ ๊ธฐ์šธ๊ธฐ์˜ ๋ถ„์‚ฐ์ด ๋น„์ •์ƒ์ ์œผ๋กœ ๋†’์•„์ง€๋Š” ๋“ฑ์˜ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์˜ˆ์ธก์ž๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ๋” ํฐ ๋ฌธ์ œ๋กœ ๋‹ค๊ฐ€์˜ค๋Š”๋ฐ, ์˜ˆ์ธก์ž๊ฐ€ ๋‘ ๊ฐœ์ธ ๊ฒฝ์šฐ, ์„œ๋กœ์— ๋Œ€ํ•œ ์ƒ๊ด€๊ด€๊ณ„๋งŒ์„ ๊ฐ–์ง€๋งŒ ์˜ˆ์ธก์ž๊ฐ€ ๋Š˜์–ด๋‚ ์ˆ˜๋ก ๊ทธ ์กฐํ•ฉ์— ๋”ฐ๋ผ ๋งค์šฐ ๋งŽ์€ ์ƒ๊ด€๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์ง€๋งŒ ๊ทธ์ค‘ ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ์ฃผ์„ฑ๋ถ„ ๋ถ„์„(principal component)์ž…๋‹ˆ๋‹ค. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์€ ์˜ˆ์ธก์ž์˜ ์„ ํ˜• ์กฐํ•ฉ์„ ํ†ตํ•˜์—ฌ ์„œ๋กœ ๋…๋ฆฝ์ ์ธ ์ธ๊ณต ๋ณ€์ˆ˜๋“ค์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, ํ†ต๊ณ„ ๋ถ„์„์— ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์žฅ ์ด์ƒ์ ์ธ ๋ณ€์ˆ˜๋ฅผ ์ƒ์‚ฐํ•ด๋‚ธ๋‹ค๋Š” ์ ์—์„œ ๋งค์šฐ ์œ ์šฉํ•œ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ๊ณผ์ •์—์„œ ๋ณ€์ˆ˜๋“ค์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํŒŒ์›Œ๋ฅผ ์†Œ์ˆ˜์˜ ์ธ๊ณต ๋ณ€์ˆ˜๋กœ ๋ชฐ์•„์ฃผ์–ด, ๋ถ„์„์— ์‚ฌ์šฉ๋˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์ค„์—ฌ์ฃผ๋Š” ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ '์ฐจ์› ์ถ•์†Œ'๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋ฉฐ ๋‹ค์ค‘๊ณต ์„ ์„ฑ๊ณผ ๋”๋ถˆ์–ด ํ†ต๊ณ„ ๋ถ„์„์—์„œ ์•„์ฃผ ์ค‘์š”ํ•˜๊ฒŒ ์ทจ๊ธ‰๋˜๋Š” ์ฃผ์ œ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์ด ๋‘ ๊ฐ€์ง€ ๊ด€์ ์—์„œ ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์˜ ๋ฐฉ๋ฒ•๋ก ์„ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์™ผ์ชฝ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๋ฉด 1 X๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋†’์€ ์ƒ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ  ์ด๋Ÿฌํ•œ ์ƒ๊ด€์„ฑ์ด ๋‹ค์ค‘๊ณต ์„ ์„ฑ์˜ ์ผ์ด ํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ƒ์ˆ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ํฌ์ธํŠธ๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ์ƒ๊ด€์„ฑ์„ ๋ฌด์‹œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ƒˆ๋กœ์šด ์ถ•์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์šฐ์ธก์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๋ฉด ์ƒˆ๋กœ์šด ์ถ• 1 P์—์„œ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ๊ด€์„ฑ์„ ์žƒ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์˜ ์ง๊ต ํšŒ์ „์€ ์› ๋ณ€์ˆ˜๋“ค์˜ ์„ ํ˜•๊ฒฐํ•ฉ์„ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง€๋ฉฐ, ์„ ํ˜•๊ฒฐํ•ฉ์˜ ๊ณ„์ˆ˜๋Š” ๋ณ€์ˆ˜๋“ค์˜ ๊ณต๋ถ„์‚ฐ(์ƒ๊ด€์„ฑ)์˜ ๊ตฌ์กฐ๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ์–ป์–ด์ง€๋Š” ๊ณ ์œ ๋ฒกํ„ฐ(eigen vectors) ๋“ค์— ์˜ํ•ด ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์ง๊ต ํšŒ์ „์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋‘ ๋ณ€์ˆ˜๋Š” ํ†ต๊ณ„์ ์œผ๋กœ ๋…๋ฆฝ์ด ๋˜๋ฉฐ ๋‹ค์ค‘๊ณต ์„ ์„ฑ์˜ ๋ฌธ์ œ๋ฅผ ์™„๋ฒฝํžˆ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์€ ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ง๊ตํ•˜๋Š” ์ƒˆ ์ถ•์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ž˜ํ”„๋Š” ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ถ• ํšŒ์ „์„ ํ‘œํ˜„ํ•˜์ง€๋งŒ ๋‘ ๊ฐœ๊ฐ€ ์•„๋‹Œ 3์ฐจ์› ํ˜น์€ 4์ฐจ์›์—์„œ๋„ ์ด๋Ÿฌํ•œ ์ง๊ต ํšŒ์ „์ด ๊ฐ€๋Šฅํ•˜๊ณ  ๊ทธ ๊ฒฝ์šฐ ๋ชจ๋“  ๋ณ€์ˆ˜๊ฐ€ ์„œ๋กœ ๋…๋ฆฝ์ด ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ƒˆ ์ฃผ์„ฑ๋ถ„ ์ถ•๋“ค์€ ๋ณ€์ˆ˜๋“ค์˜ ๋ณ€๋™์„ ์„ค๋ช…ํ•˜๋Š” ์–‘์— ๋”ฐ๋ผ ๋งŒ๋“ค์–ด์ง€๋Š” ์ˆœ์„œ๊ฐ€ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๋ฉด 2 ์ถ•์˜ ํ‰ํ–‰์ ์ธ ๋ฐฉํ–ฅ๋ณด๋‹ค๋Š” P1์ถ• ๋ฐฉํ–ฅ์—์„œ ๋ณ€์ˆ˜๋“ค์˜ ๋ณ€๋™์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋Š” 1 X ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๋ณ€๋™์„ 1 ์ถ•๋ณด๋‹ค 2 ์ถ•์ด ๋” ๋งŽ์ด ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋‹ค์Œ์˜ ๊ทธ๋ฆผ์„ ๋ด…์‹œ๋‹ค. ์œ„ ๊ทธ๋ž˜ํ”„๋Š” ๋ณ€์ˆ˜๋“ค์ด ๋ณด์ด๋Š” ๋ณ€๋™์„ ์‹œ๊ฐํ™”ํ•œ ๊ฒƒ์œผ๋กœ, ์ฃผ์„ฑ๋ถ„ ์ถ•๋“ค์€ ํƒ€์›์˜ ๋๊ณผ ๋์„ ์ด์–ด์ฃผ๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ ์ฃผ์„ฑ๋ถ„์ด ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€๋™์˜ ํฌ๊ธฐ๋Š” ์ฃผํ™ฉ์ƒ‰๊ณผ ๋นจ๊ฐ„์ƒ‰์˜ ๊ฐ ๊ธธ์ด๋กœ ํŒ๋‹จ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ๋ณ€์ˆ˜๋“ค์˜ ๊ณต๋ถ„์‚ฐ(์ƒ๊ด€์„ฑ)์˜ ๊ตฌ์กฐ๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ์–ป์–ด์ง€๋Š” ๊ณ ์œณ๊ฐ’(eigen value)์— ๋น„๋ก€ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋นจ๊ฐ„์ƒ‰ ๋ฐฉํ–ฅ์œผ๋กœ ๋จผ์ € 1๋ฒˆ ์ฃผ์„ฑ๋ถ„ ์ถ•์ด ์ƒ์„ฑ๋˜๋ฉฐ 1๋ฒˆ ์ฃผ์„ฑ๋ถ„ ์ถ•๊ณผ ์ง๊ตํ•˜๋ฉด์„œ ๊ทธ๋‹ค์Œ์œผ๋กœ ๋ณ€๋™์„ ๋งŽ์ด ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ์ฃผํ™ฉ์ƒ‰ ๋ฐฉํ–ฅ์œผ๋กœ 2๋ฒˆ ์ฃผ์„ฑ๋ถ„ ์ถ•์„ ์ƒ์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ํŠน์„ฑ์œผ๋กœ ์ธํ•ด ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•ฉ๋ฆฌ์ ์ธ ์ฐจ์› ์ถ•์†Œ๋ฅผ ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ์œ„ํ•ด ์กฐ๊ธˆ์€ ๊ทน๋‹จ์ ์ธ ์˜ˆ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ์˜ ํฌ์ธํŠธ๋“ค์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋นจ๊ฐ„์ƒ‰ ์„  ์œ„์˜ ์ ์„ ๋Œ€์‹  ์‚ฌ์šฉํ•ด๋„ ๋ฌด๋ฐฉํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋นจ๊ฐ„์ƒ‰ ์„  ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ํฐ ๋ณ€๋™์„ ๋ณด์ด๊ณ  ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ๋Š” ๊ฑฐ์˜ ๋ณ€๋™์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ์ฆ‰, ๊ตณ์ด 1 X ๋‘ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ € ์„ ์œ„์˜ ์ ๋“ค๋งŒ ์‚ฌ์šฉํ•˜๋ฉด 2๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋กœ ์ค„์ด๋Š” ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ณง 2์ฐจ์›์—์„œ 1์ฐจ์›์œผ๋กœ์˜ ์ฐจ์› ์ถ•์†Œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฐจ์›์˜ ๊ฒฝ์šฐ์—๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๋ฉด ๊ฐœ์˜ ์„œ๋กœ ์ง๊ตํ•˜๋Š” ์ƒˆ๋กœ์šด ์ถ•์„ ์ƒ์„ฑํ•˜์ง€๋งŒ ์ฒ˜์Œ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” 1๋ฒˆ ์ฃผ์„ฑ๋ถ„ ์ถ•์ด ๊ฐ€์žฅ ํฐ ๋ณ€๋™์„ ์„ค๋ช…ํ•˜๊ณ  ๋งˆ์ง€๋ง‰์— ์ƒ์„ฑ๋˜๋Š” ๋ฒˆ ์ฃผ์„ฑ๋ถ„ ์ถ•์ด ๊ฐ€์žฅ ์ ์€ ๋ณ€๋™์„ ์„ค๋ช…ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ชจ๋“  ์ถ•์ด ์„ค๋ช…ํ•˜๋Š” ๋ณ€๋™์€ ๊ฐœ์˜ ์›๋ž˜ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๋ณ€๋™์˜ ์ „๋ถ€๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ ์„ ๋ฏธ๋ฃจ์–ด ๋ณผ ๋•Œ, ๋ถ„์„ ์ž๋Š” ๊ฐœ์˜ ์ƒˆ๋กœ์šด ์ถ• ์ค‘ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์ค€์˜ ๋ณ€๋™์„ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋Š” ์†Œ์ˆ˜์˜ ์ถ•๋งŒ ์„ ํƒํ•˜๊ณ  ์†Œ๋Ÿ‰์˜ ๋ณ€๋™๋งŒ์„ ์„ค๋ช…ํ•˜๋Š” ์ถ•๋“ค์€ ๋ฒ„๋ฆผ์œผ๋กœ์จ ์ฐจ์›์„ ์ถ•์†Œํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์ฃ . 10๊ฐœ์˜ ๋ณ€์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ, ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์„ ์‹œํ–‰ํ•˜๋ฉด 10๊ฐœ์˜ ์ถ•์„ ์–ป์„ ์ˆ˜ ์žˆ๊ณ  ์ด ์ค‘ 3์˜ ์ถ•์ด ์„ค๋ช…ํ•˜๋Š” ๋ณ€๋™์ด ์ „์ฒด์˜ 80% ๋ผ๋ฉด, 3๋ฒˆ ์ฃผ์„ฑ๋ถ„ ์ถ•๊นŒ์ง€๋งŒ ํƒํ•˜๊ณ  ๋‚˜๋จธ์ง€ 7๊ฐœ ๋ฐฉํ–ฅ์˜ ๋ณ€๋™์€ ๋ฌด์‹œํ•จ์œผ๋กœ์จ 10์ฐจ์›์—์„œ 3์ฐจ์›์œผ๋กœ์˜ ์ฐจ์› ์ถ•์†Œ๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ์„ค๋ช… ๋น„์œจ์€ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๊ณ ์œณ๊ฐ’๊ณผ ๊ด€๋ จ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•๋“ค์€ ์ „๋ถ€ ์› ๋ณ€์ˆ˜๋“ค์˜ ์„ ํ˜•๊ฒฐํ•ฉ์œผ๋กœ ์ œ์‹œ๋˜๋Š” ์ƒˆ ์ฃผ์„ฑ๋ถ„ ์ธ๊ณต ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 1 X์˜ 5๊ฐœ์˜ ๋ณ€์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฃผ์„ฑ๋ถ„ ๋ณ€์ˆ˜์™€ ์ „์ฒด ๋ณ€๋™์˜ ์„ค๋ช… ๋น„์œจ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1 0.2 1 0.1 2 0.6 3 0.15 4 0.4 5 ์ „์ฒด ๋ณ€๋™ ์ค‘ 78% ์„ค๋ช… 2 โˆ’ 0.1 1 0.4 2 0.12 3 0.25 4 0.3 5 ์ „์ฒด ๋ณ€๋™ ์ค‘ 15% ์„ค๋ช… 3 0.31 1 0.13 2 0.19 3 0.22 4 0.39 5 ์ „์ฒด ๋ณ€๋™ ์ค‘ 4% ์„ค๋ช… 4 โˆ’ 0.14 1 0.57 2 0.66 3 0.05 4 0.1 5 ์ „์ฒด ๋ณ€๋™ ์ค‘ 2% ์„ค๋ช… 5 0.42 1 0.02 2 0.4 3 0.57 4 0.13 5 ์ „์ฒด ๋ณ€๋™ ์ค‘ 1% ์„ค๋ช… ์ด ๊ฒฝ์šฐ ๋‘ ์ธ๊ณต ๋ณ€์ˆ˜ 1 P๋งŒ ํƒํ•˜์—ฌ๋„ ์ „์ฒด ๋ณ€๋™์˜ ์•ฝ 93%๋ฅผ ์„ค๋ช…ํ•˜๋ฉด์„œ ์„œ๋กœ ๋…๋ฆฝ์ ์ธ 2๊ฐœ์˜ ์ธ๊ณต ๋ณ€์ˆ˜๋ฅผ ์–ป๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ์„ ํ˜•๊ฒฐํ•ฉ์„ ํ†ตํ•ด์„œ ๊ตฌํ•ด์ง„ ๊ฐ’์„ ์ฃผ์„ฑ๋ถ„ ์ ์ˆ˜(principal score)๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ ์› ๋ณ€์ˆ˜๋ฅผ ์ƒˆ ์ถ•์˜ ๊ด€์ ์—์„œ์˜ ๋ฐ”๋ผ๋ณด๋Š” ๊ฐ’์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฃผ์„ฑ๋ถ„ ์ ์ˆ˜์™€ ์› ๋ณ€์ˆ˜์˜ ์ƒ๊ด€๊ณ„์ˆ˜ ํ˜น์€ ์„ ํ˜• ๊ฒฐํ•ฉ์˜ ๊ณ„์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ ์ฃผ์„ฑ๋ถ„ ๋ณ€์ˆ˜์— ์ž‘์šฉํ•˜๋Š” ์› ๋ณ€์ˆ˜์˜ ์˜ํ–ฅ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ 3 P์˜ ์ƒ๊ด€์„ฑ์ด ๋†’์€ ์–‘์˜ ๊ฐ’์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ์ฃผ์„ฑ๋ถ„ 1 X ์ด ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ  ์ด๋Š” 1 ์„ ๋งŒ๋“œ๋Š”๋ฐ 1 ์˜ ์˜ํ–ฅ์ด ์ปธ์Œ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ์ฃผ์„ฑ๋ถ„ ์ ์ˆ˜ ๊ฐ’์€ ์™„์ „ ๋…๋ฆฝ์ ์ธ ์˜ˆ์ธก์ž๋กœ์จ, ํšŒ๊ท€๋ถ„์„์„ ํฌํ•จํ•œ ๋‹ค๋ฅธ ๊ธฐํƒ€ ํ†ต๊ณ„๋ถ„์„์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A5. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„(R Code) 5. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„(R Code) ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์€ FIFA 18 ๊ฒŒ์ž„ ๋ฐ์ดํ„ฐ ์…‹์„ ์ด์šฉํ•˜์—ฌ ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://www.drop box.com/sh/vtqlvrgdts2yfez/AAD_cd49dBcvgBNdz-C-A6TFA?dl=0 ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ ### Principal Component Analysis library(tidyr) library(data.table) FIFA = read.csv("F:\Drop box\DATA SET(Drop box)\CompleteDataSET.csv", header = TRUE, stringsAsFactors = FALSE) FIFA_FIELD = subset(FIFA, FIFA$Preferred.Positions != "GK ") library(purrr) FIFA_FIELD2 = FIFA_FIELD[1:100, c(2, 10:40, 42:48, 61:71)] FIFA_FIELD2 = FIFA_FIELD2 %>% map_if(is.character, as.numeric) FIFA_FIELD2 = FIFA_FIELD2 %>% map_if(is.integer, as.numeric) FIFA_FIELD2 = as.data.frame(FIFA_FIELD2) rownames(FIFA_FIELD2) = FIFA_FIELD$Name[1:100] ์„ ์ˆ˜๋“ค์˜ ๋Šฅ๋ ฅ์น˜๋ฅผ ์ถ”์ถœํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์„ ์ˆ˜๋“ค์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์œ„ 100๋ช…์— ๋Œ€ํ•ด ๋ถ„์„์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํ‘œ์ค€ํ™” ๋ฐ์ดํ„ฐ์˜ ๊ธฐํ•˜์ ์ธ ํŠน์„ฑ์„ ์ด์šฉํ•˜๋Š” ๋ถ„์„์€ ํ•ญ์ƒ ํ‘œ์ค€ํ™”(Scaling)๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ ์„ค๋ช…ํ–ˆ๋“ฏ์ด ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์€ ๋ฐ์ดํ„ฐ์˜ ๋ณ€๋™์„ ๊ธฐ์ค€์œผ๋กœ ์ฃผ์„ฑ๋ถ„์ด ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋‹จ์œ„๊ฐ€ ํฐ(๋ถ„์‚ฐ์ด ํฐ) ๋ณ€์ˆ˜์™€ ๋‹จ์œ„๊ฐ€ ์ž‘์€(๋ถ„์‚ฐ์ด ์ž‘์€) ๋ณ€์ˆ˜๊ฐ€ ๊ฐ™์ด ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์— ํˆฌ์ž…๋  ๊ฒฝ์šฐ, ๋‹จ์œ„๊ฐ€ ํฐ ๋ณ€์ˆ˜๊ฐ€ ๋‹จ์œ„๊ฐ€ ์ž‘์€ ๋ณ€์ˆ˜์˜ ๋ณ€๋™์„ ๋ฎ์–ด๋ฒ„๋ ค ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์—†๊ฒŒ ๊ฒฐ๊ด๊ฐ’์„ ์™œ๊ณกํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๊ธฐํ•˜ ์„ฑ์งˆ์„ ๋„๋Š” ๋ถ„์„์€ ๋‹จ์œ„๋ฅผ ํ†ต์ผ์‹œ์ผœ์ฃผ๋Š” ํ‘œ์ค€ํ™” ์ž‘์—…์„ ๋จผ์ € ์ง„ํ–‰ํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. SCALED=as.data.frame(scale(FIFA_FIELD2[,1:34])) scale()์„ ์“ฐ๋ฉด ์ˆ˜์น˜ํ˜•์œผ๋กœ ๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ๋ณ€์ˆ˜๋“ค์ด ํ‰๊ท ์€ 0, ๋ถ„์‚ฐ์€ 1์„ ๊ฐ€์ง€๋„๋ก ํ‘œ์ค€ํ™” ๋ณ€ํ™˜์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ๋ถ„์„์€ ํฌ์ง€์…˜๋ณ„ ๋Šฅ๋ ฅ์น˜๋ฅผ ์ œ์™ธํ•œ, ํ”ผ์ง€์ปฌ ๋ฐ ํ…Œํฌ๋‹‰ ๋Šฅ๋ ฅ์น˜๋“ค์„ ๋ถ„์„ ๋Œ€์ƒ์œผ๋กœ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„ ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์—, ๋จผ์ € ๋ณ€์ˆ˜๋“ค์ด ์„ ํ˜• ๊ด€๊ณ„๋ฅผ ์–ด๋Š ์ •๋„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€์— ๋Œ€ํ•ด์„œ ํƒ์ƒ‰์„ ํ•  ํ•„์š”๋Š” ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๋ณ€์ˆ˜ ๊ฐ„์˜ ์ƒ๊ด€๊ด€๊ณ„๊ฐ€ ์ ์€ ๊ฒฝ์šฐ, ๊ตณ์ด ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์„ ํ•  ์ด์œ ๋Š” ํฌ๊ฒŒ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ฐจ๋ผ๋ฆฌ ์ „์— ์–ธ๊ธ‰ํ•œ ๋ณ€์ˆ˜ ์„ ํƒ๋ฒ•์„ ํ†ตํ•ด ์ฐจ์›(๋ณ€์ˆ˜)์„ ์ค„์—ฌ๊ฐ€๋Š” ๊ฒƒ์ด ๋” ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค. library(corrplot) library(RColorBrewer) Corr_mat = cor(SCALED) corrplot(Corr_mat, method = "color", outline = T, addgrid.col = "darkgray", order = "hclust", addrect = 4, rect.col = "black", rect.lwd = 5, cl.pos = "b", tl.col = "indianred4", tl.cex = 0.5, cl.cex = 0.5, addCoef.col = "white", number.digits = 2, number.cex = 0.3, col = colorRampPalette(c("darkred", "white", "midnightblue"))(100)) cor()์€ ์ƒ๊ด€ ํ–‰๋ ฌ์„ ๋งŒ๋“œ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ๋ณ€์ˆ˜๋“ค ๊ฐ„์˜ ์„ ํ˜• ๊ด€๊ณ„๊ฐ€ ๊ฝค๋‚˜ ๋†’์€ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์„ ํ†ตํ•ด ์ฐจ์› ์ถ•์†Œ๋ฅผ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ช…๋ น์–ด ์ถœ์ฒ˜ : http://www.sthda.com/english/articles/31-principal-component-methods-in-r-practical-guide/112-pca-principal-component-analysis-essentials/ library(factoextra) library(FactoMineR) Principal_Component = PCA(SCALED, graph = FALSE) PCA()๋Š” ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์„ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. graph = FALSE ์˜ต์…˜์€ PCA() ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ž๋™์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ทธ๋ž˜ํ”„๊ฐ€ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋จผ์ € ํ™•์ธํ•ด์•ผ ๋˜๋Š” ๋ถ€๋ถ„์€ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ง„ ์ฃผ์„ฑ๋ถ„๋“ค์ด ๋ณ€์ˆ˜๋“ค์˜ ๋ณ€๋™์„ ์–ผ๋งˆํผ ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. Principal_Component$eig[1:5, ] eigenvalue percentage of variance cumulative percentage of variance comp 1 14.894127 43.806256 43.80626 comp 2 3.600039 10.588349 54.39461 comp 3 2.975834 8.752452 63.14706 comp 4 2.480377 7.295227 70.44228 comp 5 2.002131 5.888621 76.33090 comp1(์ œ1์ฃผ์„ฑ๋ถ„, PC1)์€ ์ „์ฒด ๋ณ€๋™์˜ 43.80%๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. comp2(์ œ2์ฃผ์„ฑ๋ถ„, PC 2)์€ ์ „์ฒด ๋ณ€๋™์˜ 10.58%๋ฅผ ์„ค๋ช…ํ•˜๋ฉฐ, ์ œ1์ฃผ์„ฑ๋ถ„์„ ํฌํ•จํ•œ ๋ˆ„์  ์„ค๋ช…๋ ฅ์€ 54.39%์ž…๋‹ˆ๋‹ค. Scree plot์€ ๊ฐ ์ฃผ์„ฑ๋ถ„์ด ์ „์ฒด ๋ณ€๋™์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๋Š” ๋น„์œจ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. fviz_screeplot(Principal_Component, addlabels = TRUE, ylim = c(0, 50)) PC1์ด ๊ฐ€์žฅ ๋งŽ์€ ๋ณ€๋™์„ ์„ค๋ช…ํ•˜๋ฉฐ, PC7๋ถ€ํ„ฐ๋Š” ์„ค๋ช…ํ•˜๋Š” ๋น„์œจ์˜ ๋ณ€ํ™”๊ฐ€ ๋งค์šฐ ์ž‘์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Scree Plot์„ ํ†ตํ•ด ๋ช‡ ๊ฐœ์˜ ์ฃผ์„ฑ๋ถ„๊นŒ์ง€ ๊ฐ€์ ธ๋‹ค ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ด ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค. ์ฃผ์„ฑ๋ถ„ ๊ฐ€์ค‘์น˜ ํ™•์ธ ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์˜ ํ•ด์„์€ ๊ฐ ์ฃผ์„ฑ๋ถ„์—์„œ ์–ด๋–ค ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์žฅ ํฐ ์˜ํ–ฅ๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. (+)๋ผ๊ณ  ์ข‹์€ ๊ฒƒ์ด ์•„๋‹ˆ๋ฉฐ, (-)๋ผ๊ณ  ๋‚˜์œ ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋ถ€ํ˜ธ๋Š” ๋ฐฉํ–ฅ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ํ–ฅ๋ ฅ ์ž์ฒด๋Š” ๊ณ„์ˆ˜์˜ ์ ˆ๋Œ“๊ฐ’ ํฌ๊ธฐ์— ์ง‘์ค‘์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Dim.1์€ ์ œ1์ฃผ์„ฑ๋ถ„(PC1)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Principal_Component$var$coord[1:34, ] Dim.1 Dim.2 Dim.3 Dim.4 Age -0.1391304 0.15188183 0.0002134079 0.43702597 Acceleration 0.6111358 -0.41547597 -0.0837865360 -0.44590706 Aggression -0.6583780 0.15278535 0.3071821747 0.17498731 Agility 0.8041455 -0.01336877 -0.0545868730 -0.36064461 Balance 0.6044207 0.15321777 -0.2446476197 -0.48091951 Ball.control 0.9140783 0.21037964 0.0194490653 -0.06052152 Composure 0.4190301 0.25451272 -0.0022204597 0.50070532 Crossing 0.8198600 0.25508545 -0.0058704866 -0.10857638 Curve 0.9155934 0.20091316 -0.0366767330 -0.02629993 Dribbling 0.9389829 0.04414586 -0.0944411585 -0.11187542 Finishing 0.9035608 -0.17998183 0.1005064965 0.18373436 Free.kick.accuracy 0.8181311 0.29463438 0.0920612901 0.06445744 GK.diving 0.1498721 -0.05724641 0.6802467984 -0.22159550 GK.handling 0.1079801 -0.05885318 0.6748930563 -0.16986119 GK.kicking 0.2176676 -0.08598534 0.7173101651 -0.16002126 GK.positioning 0.0916075 -0.13699776 0.6715493703 -0.11918837 GK.reflexes 0.2013511 -0.14807891 0.7759962774 -0.10869070 Heading.accuracy -0.5371602 -0.32424989 0.1744474398 0.59771117 Interceptions -0.7197588 0.58103933 0.1157715176 -0.07256358 Jumping -0.4587959 -0.35864751 0.0540112040 0.29396321 Dim.5 Age -0.080699184 Acceleration 0.380635869 Aggression 0.379845386 Agility 0.261068995 Balance 0.218730455 Ball.control 0.008509720 Composure 0.008980891 Crossing 0.169537864 Curve 0.024764215 Dribbling 0.039338751 Finishing -0.019721032 Free.kick.accuracy -0.012611936 GK.diving -0.123443113 GK.handling -0.257640855 GK.kicking 0.023624321 GK.positioning -0.086825626 GK.reflexes -0.153215026 Heading.accuracy 0.237293106 Interceptions 0.220703722 Jumping 0.558884296 [ getOption("max.print")์— ๋„๋‹ฌํ–ˆ์Šต๋‹ˆ๋‹ค -- 14 ํ–‰๋“ค์„ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค ] $$ PC1 =-0.14Age+0.61Acceleration-0.65Aggression+\ 0.8Agility+0.6Balance+0.91Ball.control + \cdots+0.89*Volleys $$ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ณ€์ˆ˜๋“ค์˜ ์„ ํ˜•๊ฒฐํ•ฉ ๊ผด๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ œ2์ฃผ์„ฑ๋ถ„(PC 2)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. $$ PC 2 =0.15Age-0.42Acceleration+0.15Aggression-\ 0.013Agility+0.15Balance+0.21Ball.control + \cdots-0.07*Volleys $$ ์ œ1์ฃผ์„ฑ๋ถ„์—์„œ์˜ ๋ณ€์ˆ˜๋“ค์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Ball.control, Curve, Dribbling, Finishing, Volleys ๋ณ€์ˆ˜์˜ ๊ฐ€์ค‘์น˜๊ฐ€ 0.9 ์ „ํ›„๋กœ ์–‘์˜ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€์ค‘์น˜๊ฐ€ ํฐ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Aggression, Interceptions, Marking, Sliding tackle, Standing.tackle ๋“ฑ์˜ ๋ณ€์ˆ˜๋Š” -0.7 ์ „ํ›„๋กœ ์Œ์˜ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€์ค‘์น˜๊ฐ€ ํฐ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถ„์„ํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ํ† ๋Œ€๋กœ ์ฃผ์„ฑ๋ถ„์— ๋Œ€ํ•ด ์ƒˆ๋กœ์šด ์ด๋ฆ„์„ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. PC1์ด ์–‘์˜ ๋ฐฉํ–ฅ์œผ๋กœ ํฐ ๊ฒฝ์šฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ณต์„ ๋‹ค๋ฃจ๋Š” ๋Šฅ๋ ฅ์น˜๋“ค์ด ์ข‹์€ ๊ฒฝ์šฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ PC1์ด ์Œ์˜ ๋ฐฉํ–ฅ์œผ๋กœ ํฐ ๊ฒฝ์šฐ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ˆ˜๋น„ ๋Šฅ๋ ฅ์น˜๊ฐ€ ๋†’์„ ๊ฒฝ์šฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, PC1์€ ์ˆ˜๋น„ ํฌ์ง€์…˜ ์—ฌ๋ถ€๋ผ๋Š” ์ƒˆ๋กœ์šด ์ด๋ฆ„์„ ์ง€์–ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ˆ˜๋งŽ์€ ๋ณ€์ˆ˜๋ฅผ ๋ถ„์„ํ•œ ์ฃผ์„ฑ๋ถ„ ๊ณ„์ˆ˜๋Š” ๋ˆˆ์œผ๋กœ ํ™•์ธํ•˜๊ธฐ ํž˜๋“  ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— Biplot์ด๋ผ๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค ํšจ์œจ์ ์œผ๋กœ ํ™•์ธํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. fviz_pca_var(Principal_Component, col.var = "contrib", gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"), repel = TRUE # Avoid text overlapping ) Biplot์˜ x์ถ•์€ Dim1(์ œ1์ฃผ์„ฑ๋ถ„), y ์ถ•์€ Dim2(์ œ2์ฃผ์„ฑ๋ถ„)์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด Long.passing ๋ณ€์ˆ˜์˜ ๊ฐ€์ค‘์น˜๋Š” ์ œ1์ฃผ์„ฑ๋ถ„์—์„œ 0.30์ž…๋‹ˆ๋‹ค. ์ œ2์ฃผ์„ฑ๋ถ„์—์„œ์˜ ๊ฐ€์ค‘์น˜๋Š” 0.82์ž…๋‹ˆ๋‹ค. Biplot์—์„œ Long.passing ๋ณ€์ˆ˜๋Š” (0.30,0.82)(0.30,0.82)์— ์ฐํžˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Biplot์—์„œ ๊ฐ™์€ ๋ฐฉํ–ฅ์œผ๋กœ ๋ป—์–ด๋‚˜๊ฐ€๋Š” ๋ณ€์ˆ˜์ผ์ˆ˜๋ก ๋น„์Šทํ•œ ๋ณ€์ˆ˜๋“ค์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Biplot์„ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ„์„ ๊ฒฐ๊ณผ๋Š” PC1์ด ํฌ๊ฑฐ๋‚˜ ์ž‘์„ ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ค ๋ณ€์ˆ˜๋“ค์ด ์˜ํ–ฅ์„ ๋งŽ์ด ๋ฏธ์น˜๋Š”์ง€, ํ˜น์€ PC 2๊ฐ€ ํด ๋•Œ, ์ž‘์„ ๋•Œ ์–ด๋–ค ๋ณ€์ˆ˜๋“ค์ด ์˜ํ–ฅ์„ ๋งŽ์ด ์ฃผ๋Š”์ง€ ๊ฐ€์ค‘์น˜๋ฅผ ํ•œ๋ˆˆ์— ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์€ ๊ณ ์ฐจ์›์˜ ๋ณ€์ˆ˜๋“ค์„ ๋‹จ 2์ฐจ์›์˜ ๊ทธ๋ž˜ํ”„๋กœ ์‹œ๊ฐํ™”ํ•ด์ฃผ๋Š” ๋›ฐ์–ด๋‚œ ํšจ์œจ์„ฑ์„ ์ง€๋‹ˆ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์€ ์‹œ๊ฐํ™” ๋ชฉ์ ์œผ๋กœ๋„ ์ž์ฃผ ์“ฐ์ด๊ณ ๋Š” ํ•ฉ๋‹ˆ๋‹ค. fviz_pca_biplot(Principal_Component, repel = FALSE) ์œ„์—์„œ ๊ทธ๋ ธ๋˜ Biplot ์œ„์—๋‹ค๊ฐ€ ์„ ์ˆ˜๋“ค์˜ ์‚ฐ์ ๋„๋ฅผ ๊ฐ™์ด ๊ทธ๋ ธ์Šต๋‹ˆ๋‹ค. ๊ฐ ์„ ์ˆ˜๋“ค ๋ณ„๋กœ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜(์ฃผ์„ฑ๋ถ„)์— ๋Œ€ํ•œ ๊ฐ’์ด ๊ณ„์‚ฐ์ด ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ํฌ๋ฆฌ์Šคํ‹ฐ์•„๋…ธ ํ˜ธ๋‚ ๋‘ ์„ ์ˆ˜๋Š” Biplot ์ƒ์—์„œ (4.95, -1.72)์— ์œ„์น˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์„ ์ˆ˜๋“ค์˜ ๋Šฅ๋ ฅ์น˜ ๋ถ„ํฌ์™€ ์ƒ๋Œ€์  ์œ„์น˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์€ ์ด๋ ‡๊ฒŒ ๊ธฐ์กด ๋ณ€์ˆ˜๋“ค์˜ ์„ ํ˜• ๊ฒฐํ•ฉ์„ ์ด์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ถ•(๋ณ€์ˆ˜)๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ƒˆ๋กœ์šด ๋ณ€์ˆซ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ๋ถ„์„์„ ์ƒˆ๋กœ ์ง„ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์ฃผ์„ฑ๋ถ„ ๊ฐ„์˜ ์ƒ๊ด€๊ณ„์ˆ˜๋Š” 0์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์„ฑ๋ถ„ ๊ฐ’์œผ๋กœ ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ, ๋‹ค์ค‘๊ณต ์„ ์„ฑ์„ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํšจ๊ณผ์ ์ธ ์ฐจ์› ์ถ•์†Œ๋กœ ๋ชจ๋ธ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์€ ๊ธฐ๊ณ„ํ•™์Šต์—์„œ๋„ ์ฃผ๋กœ ์“ฐ์ด๋Š” ์ฐจ์› ์ถ•์†Œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์„ ํ•œ ๋ฒˆ์— ์ดํ•ดํ•˜๊ธฐ๋Š” ๋งค์šฐ ํž˜๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ์— ๋งŽ์€ ํ•ด์„ ์—ฐ์Šต์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. A6. ๊ตฐ์ง‘๋ถ„์„ 6. ๊ตฐ์ง‘๋ถ„์„ ๊ตฐ์ง‘๋ถ„์„์ด๋ž€ ๋ฐ์ดํ„ฐ๋“ค์„ ์„œ๋กœ ๋น„์Šทํ•œ ๋ฐ์ดํ„ฐ๋“ค๋ผ๋ฆฌ ๋ฌถ์–ด์ฃผ์–ด, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ตฐ์ง‘์œผ๋กœ ๋ฌถ์–ด ์ฃผ๋Š” ๋ถ„์„์ž…๋‹ˆ๋‹ค. ์ด์ œ๊นŒ์ง€ ์ง„ํ–‰ํ–ˆ๋˜ ๋ถ„์„๋“ค์€ '์ง€๋„ ํ•™์Šต(Supervised Learning)'์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌํ•ด์•ผ ๋˜๋Š” ๊ฐ’์ด ๋ช…ํ™•ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ตฐ์ง‘๋ถ„์„์€ ๋ฐ˜๋Œ€๋กœ '๋น„์ง€๋„ ํ•™์Šต(Unsupervised Learning)'์ž…๋‹ˆ๋‹ค. ๊ตฌํ•ด์•ผ ๋˜๋Š” ๊ฐ’์ด ๋ช…ํ™•ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์นด๋“œ์‚ฌ์—์„œ ๊ณ ๊ฐ๋“ค์˜ ํŠน์„ฑ์„ ํŒŒ์•…ํ•˜์—ฌ ๋น„์Šทํ•œ ๊ณ ๊ฐ๋“ค์„ ๋Œ€์ƒ์œผ๋กœ ๊ตฐ์ง‘์„ ํ˜•์„ฑํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ์นด๋“œ์‚ฌ๋Š” ๊ณ ๊ฐ๋“ค์„ ์–ด๋–ป๊ฒŒ ๋ถ„๋ฅ˜ํ•ด์•ผ ๋˜๋Š”์ง€ ๊ธฐ์ค€์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌํ•ด์•ผ ๋˜๋Š” ๊ฐ’์ด ๋ช…ํ™•ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ ๊ตฐ์ง‘๋ถ„์„์€ ๋งค์šฐ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋จผ์ €, ๊ตฐ์ง‘๋ถ„์„์—์„œ๋Š” ์œ ์‚ฌ๋„(Similarity) ๊ฐœ๋…์ด ๊ณตํ†ต์ ์œผ๋กœ ์“ฐ์ž…๋‹ˆ๋‹ค. ์œ ์‚ฌ๋„(Similarity) ๋‹จ์–ด์˜ ์˜๋ฏธ ๊ทธ๋Œ€๋กœ '๋น„์Šทํ•œ ์ •๋„'๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์ด ์œ ์‚ฌ๋„๋ฅผ ์ธก์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๋งŽ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•๋“ค์— ๋Œ€ํ•ด๋งŒ ์–ธ๊ธ‰ํ•˜๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. ์œ ํด๋ฆฌ๋””์•ˆ ๊ฑฐ๋ฆฌ(Euclidean Distance) ์ขŒํ‘œ์ƒ์—์„œ ๋ฐ์ดํ„ฐ๋“ค ๊ฐ„์˜ ์ง์„ ๊ฑฐ๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„(Cosine Similarity) ์ขŒํ‘œ์ƒ์—์„œ ๋ฐ์ดํ„ฐ๋“ค ๊ฐ„์˜ Cosine ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ ํด๋ฆฌ๋””์•ˆ ๊ฑฐ๋ฆฌ์—์„œ๋Š” ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€๊นŒ์šธ์ˆ˜๋ก, ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„์—์„œ๋Š” ๊ฐ ๋ฐ์ดํ„ฐ ๊ฐ„์˜ ๊ฐ๋„๊ฐ€ ์ž‘์„์ˆ˜๋ก ๋ฐ์ดํ„ฐ๊ฐ€ ๋น„์Šทํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ตฐ์ง‘๋ถ„์„์—์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ๋ถ€๋ถ„์€ ๋ช‡ ๊ฐœ์˜ ๊ตฐ์ง‘์œผ๋กœ ๋‚˜๋ˆŒ์ง€๋Š” ๋ถ„์„๊ฐ€์˜ ํŒ๋‹จ์— ๋‹ฌ๋ ค ์žˆ์Šต๋‹ˆ๋‹ค. ์„ ํ˜• ๋ชจํ˜•์—์„œ์ฒ˜๋Ÿผ p-value๊ฐ€ ํ•ด๊ฒฐํ•ด ์ฃผ๋Š” ๋ถ€๋ถ„์ด ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋ถ„์„๊ฐ€์˜ ์ •ํ™•ํ•œ ํ•ด์„์ด ์š”๊ตฌ๋˜๋Š” ๋ถ„์„์ž…๋‹ˆ๋‹ค. ์ด๋Š” ์ฆ‰ ๊ตฐ์ง‘๋ถ„์„์˜ ๋‹จ์ ์œผ๋กœ ๊ท€๊ฒฐ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ตฐ์ง‘๋ถ„์„์€ ํฌ๊ฒŒ ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„๊ณผ ๋น„๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A7. ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„ 7. ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„ ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„์˜ ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋“ค ๊ฐ„์˜ ๊ฑฐ๋ฆฌํ–‰๋ ฌ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๊ฑฐ๋ฆฌํ–‰๋ ฌ์—์„œ ๊ทœ์น™์— ๋”ฐ๋ผ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๋ฐ์ดํ„ฐ๋“ค๋ถ€ํ„ฐ ๊ตฐ์ง‘์„ ํ˜•์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ƒ๋Œ€์ ์œผ๋กœ ์ž‘์€ ๋ฐ์ดํ„ฐ์— ์“ฐ๊ธฐ ์•Œ๋งž์Šต๋‹ˆ๋‹ค. ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„์„ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ์œ ์‚ฌ๋„๊ฐ€ ๊ณ„์‚ฐ์ด ๋˜์–ด์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. # ์œ ํด๋ฆฌ๋””์•ˆ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ Distance_Matrix = dist(x = Iris, method = "euclidean") Distance_Matrix 10 20 30 40 20 0.7280110 30 0.2236068 0.7211103 40 0.3605551 0.4000000 0.4472136 50 0.2236068 0.5099020 0.3162278 0.1414214 min(Distance_Matrix) [1] 0.1414214 ๊ฑฐ๋ฆฌ ํ–‰๋ ฌ์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์ด ์กด์žฌํ•˜๋Š” ๊ด€์ธก์น˜๋Š” 40, 50์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด 40, 50์€ ๊ฐ™์€ ๊ตฐ์ง‘์œผ๋กœ ๋ฌถ์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฒ•์€ ๊ตฐ์ง‘ ๋‚ด ๊ด€์ธก์น˜์™€ ๋‹ค๋ฅธ ๊ด€์ธก์น˜ ๊ฐ„ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ด€์ธก์น˜๋ฅผ ์„ ํƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ( 40 50 10 ) m n [ ( 40 10 ) d ( 50 10 ) ] m n ( 0.36 0.22 ) 0.22 ( 40 50 20 ) m n [ ( 40 20 ) d ( 50 20 ) ] m n ( 0.40 0.51 ) 0.40 ( 40 50 30 ) m n [ ( 40 30 ) d ( 50 30 ) ] m n ( 0.44 0.31 ) 0.31 ๊ทธ๋Ÿผ ์—ฌ๊ธฐ์„œ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด 10 ( 40 50 ) ๊ตฐ์ง‘์— ์†ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„์€ ์ด๋Ÿฐ ์‹์œผ๋กœ ์„œ๋กœ ์œ ์‚ฌ๋„๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ๊ตฐ์ง‘์„ ํ˜•์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ด€์ธก์น˜์— ๋Œ€ํ•ด์„œ ์œ ์‚ฌ๋„๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ‘œ๋ณธ์ด ํด ๊ฒฝ์šฐ์—๋Š” ์ ํ•ฉํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. A8. ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„(R Code) 8. ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„(R Code) ์ฃผ์„ฑ๋ถ„ ๋ถ„์„์—์„œ ์˜ˆ์ œ๋กœ ์ผ๋˜ ํ”ผํŒŒ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. library(cluster) library(factoextra) C = sample(1:nrow(SCALED), 40, replace = FALSE) # ๋žœ๋คํ•˜๊ฒŒ 40๊ฐœ๋งŒ ์ถ”์ถœ FIFA_SAMPLE = SCALED[C, ] 100๋ช…์˜ ์„ ์ˆ˜๋“ค ์ค‘ 40๋ช…์„ ๋žœ๋ค์œผ๋กœ ๋ฝ‘์•„ ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„์„ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ ์ถœ์ฒ˜ : http://www.sthda.com/english/wiki/print.php?id=234 res.hk = hkmeans(SCALED[C, ], 3) fviz_dend(res.hk, cex = 0.6, palette = "jco", rect = TRUE, rect_border = "jco", rect_fill = TRUE) hkmeans()๋Š” ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„ ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. 3์€ 3๊ฐœ์˜ ๊ตฐ์ง‘์œผ๋กœ ํ‘œ์‹œํ•˜๋ผ๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ 4๋ฅผ ์ฃผ๋ฉด 4๊ฐœ์˜ ๊ตฐ์ง‘์œผ๋กœ ์ƒ‰์„ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์—ฐ์‚ฐ๋Ÿ‰์ด ๋งŽ๊ธฐ์— ํฐ ํ‘œ๋ณธ์—์„œ๋Š” ์ถ”์ฒœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๊ฒฐ๊ด๊ฐ’์ด ๋ด๋“œ๋กœ๊ทธ๋žจ์„ ํ†ตํ•ด ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ๊ธฐ์—, ๋Œ€ํ‘œ๋ณธ์„ ๋Œ€์ƒ์œผ๋กœ๋Š” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋Œ€์‹  ๋Œ€ํ‘œ๋ณธ์„ ์†Œํ‘œ๋ณธ์œผ๋กœ ์ƒ˜ํ”Œ๋ง ํ•œ ํ›„, ๋น„๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„์„ ํ•˜๋ฉด ๋Œ€ํ‘œ๋ณธ์„ ๋Œ€์ƒ์œผ๋กœ ํ•˜๋Š” ๊ตฐ์ง‘๋ถ„์„์€ ๋ช‡ ๊ฐœ์˜ ๊ตฐ์ง‘์ด ์ ํ•ฉํ• ์ง€ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. A9. ๋น„๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„ 9. ๋น„๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„ ๋น„๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„์€ ๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„๊ณผ๋Š” ๋ฐ˜๋Œ€๋กœ ๊ฑฐ๋ฆฌํ–‰๋ ฌ์„ ์ด์šฉํ•˜์ง€ ์•Š๊ณ , ๋Œ€ํ‘œ๋ณธ์— ๋Œ€ํ•ด ์ ํ•ฉํ•œ ๋ถ„์„๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ K ํ‰๊ท  ๊ตฐ์ง‘๋ถ„์„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ‰๊ท  ๊ตฐ์ง‘๋ถ„์„์€ ๋‹ค์Œ์˜ ์ˆœ์„œ๋Œ€๋กœ ์ง„ํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. ๊ฐœ์˜ ๊ตฐ์ง‘ ์ˆ˜๋ฅผ ์‚ฌ์ „์— ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์ดˆ๊ธฐ์˜ ํ‰๊ท ๊ฐ’์€ ๋ฐ์ดํ„ฐ์—์„œ ๋ฌด์ž‘์œ„๋กœ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์„ ํƒ๋œ ๋ฐ์ดํ„ฐ๋Š” ๊ฐ์˜ ์ค‘์‹ฌ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ฐœ์˜ ์ค‘์‹ฌ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ ๋ฐ์ดํ„ฐ ๊ฐ„์˜ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ•˜๊ณ , ์œ ์‚ฌ๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๋ฐ์ดํ„ฐ๋“ค์„ ์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๊ตฐ์ง‘์˜ ์ค‘์‹ฌ์ ์„ ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜๊ณ  3 ~ 4์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. fviz_nbclust(SCALED, kmeans, method = "wss") + geom_vline(xintercept = 3, linetype = 5) ํ•ด๋‹น ๊ทธ๋ž˜ํ”„๋Š” ๊ตฐ์ง‘์˜ ๊ฐœ์ˆ˜(x์ถ•, Number of cluster k)์— ๋”ฐ๋ผ ๊ฐ ๊ตฐ์ง‘ ์•ˆ์—์„œ ๋ฐ์ดํ„ฐ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ ์ œ๊ณฑํ•ฉ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ’์ด ์ž‘์„์ˆ˜๋ก ๊ตฐ์ง‘์€ ์ž‘๊ฒŒ ํ˜•์„ฑ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๊ตฐ์ง‘์„ ๋„ˆ๋ฌด ๋งŽ์ด ๋‚˜๋ˆŒ ๊ฒฝ์šฐ์—๋Š” ํ•ด์„์ด ํž˜๋“ค์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ ๋‹นํ•œ ๊ฐœ๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. B1. ๋น„๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„(R Code) 10. ๋น„๊ณ„์ธต์  ๊ตฐ์ง‘๋ถ„์„(R Code) km.res = kmeans(SCALED, 3, nstart = 25) fviz_cluster(km.res, data = SCALED) + theme_bw() kmeans()๋Š” K ํ‰๊ท  ๊ตฐ์ง‘๋ถ„์„์„ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. 3์€ ๊ตฐ์ง‘์˜ ๊ฐœ์ˆ˜๋ฅผ 3๊ฐœ๋กœ ์„ค์ •ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. nstart ์˜ต์…˜์€ kmeans ๋ถ„์„์„ 25๋ฒˆ์„ ํ•˜์—ฌ, ๊ทธ์ค‘ ๋ถ„์‚ฐ์ด ๊ฐ€์žฅ ์ž‘๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ถ„์„์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋กœ ์ •ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด, K ํ‰๊ท  ๊ตฐ์ง‘๋ถ„์„์€ ์ž„์˜์ ์ธ ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ๊ฒฐ๊ณผ๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋Š” ๋ฌธ์ œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐํ™”๋ฅผ ์ฃผ์„ฑ๋ถ„์œผ๋กœ ์ฐจ์› ์ถ•์†Œ๋ฅผ ํ•˜๋Š” ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. plot(SCALED[, 1:5], col = km.res$cluster) ์ด๋ ‡๊ฒŒ ๊ฐ ๋ณ€์ˆ˜๋“ค ๊ฐ„์˜ ๊ฐœ๋ณ„ ์‚ฐ์ ๋„๋กœ ๊ตฐ์ง‘์ด ์ž˜ ๋ฌถ์˜€๋Š”์ง€ ์‹œ๊ฐํ™”ํ•˜๋Š” ๊ฒƒ์€ ๋„ˆ๋ฌด๋‚˜ ๋น„ํšจ์œจ์ ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ตฐ์ง‘ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜์‹œ๋ฉด ๊ณต๊ฒฉ์ˆ˜, ๋ฏธ๋“œํ•„๋”, ์ˆ˜๋น„์ˆ˜๋“ค์ด ๋”ฐ๋กœ๋”ฐ๋กœ ๊ตฐ์ง‘์ด ํ˜•์„ฑ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ค ๊ตฐ์ง‘์— ์†ํ–ˆ๋Š”์ง€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. km.res$cluster[1:10] Cristiano Ronaldo L. Messi Neymar L. Su์ฐผrez 2 2 2 2 R. Lewandowski E. Hazard T. Kroos G. Higua ์ฑ  n 2 2 3 2 Sergio Ramos K. De Bruyne 3 2 B2. KNN 11. KNN kNN(k nearest neighbor algorithm)์€ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๊ธฐ์กด ๋ฐ์ดํ„ฐ ๊ฐ€์šด๋ฐ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด k ๊ฐœ ์ด์›ƒ์˜ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์˜ ์ง‘๋‹จ์„ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•๋ก ์ž…๋‹ˆ๋‹ค. kNN์˜ ํŠน์ง•์„ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜ ๋Œ€์ƒ์ด ์ •ํ•ด์ ธ์žˆ๋Š” ์ง€๋„ํ•™์Šต์ž…๋‹ˆ๋‹ค. ๊ณ„์‚ฐ ๋น„์šฉ์ด ๋†’์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ํ˜•ํƒœ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š๊ธฐ์—, ๋ณ„๋„์˜ ๋ชจ๋ธ์„ ๊ตฌ์ถ•ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‘ ์  ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ธก์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ๊ฒฐ๊ด๊ฐ’ ๋„์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. k ๊ฐ’์ด ์ž‘์œผ๋ฉด noise(์ด์ƒ์น˜ ๋“ฑ)์˜ ์˜ํ–ฅ์„ ํฌ๊ฒŒ ๋ฐ›๊ณ , ๋ฐ˜๋Œ€๋กœ k๊ฐ€ ํฌ๊ฒŒ ๋˜๋ฉด ์ •์ƒ์ ์ธ ๋ถ„๋ฅ˜์— ์–ด๋ ค์›€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. kNN์—์„œ ๊ฐ€์žฅ ํ•ต์‹ฌ์€ k๋ฅผ ์–ด๋–ป๊ฒŒ ์„ค์ •ํ•˜๋ƒ์— ๋”ฐ๋ผ ๊ฒฐ๊ด๊ฐ’์ด ๋งค์šฐ ํฌ๊ฒŒ ์ฐจ์ด๊ฐ€ ๋‚  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ ์ ˆํ•œ k๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด kNN์˜ ํ•ต์‹ฌ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. B3. kNN(R code) 12. kNN(R code) library(caret) library(class) library(gmodels) library(ggplot2) data(iris) plot(iris$Sepal.Length, iris$Sepal.Width, col = iris$Species, pch = 19) legend("topright", legend = levels(iris$Species), bty = "n", pch = 19, col = palette()) normMinMax = function(x) { return((x - min(x))/max(x) - min(x)) } iris_norm = as.data.frame(lapply(iris[1:4], normMinMax)) normMinMax๋Š” kNN์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์—, ๊ฐ ํŠน์„ฑ ๊ฐ’๋“ค์˜ ๋ฒ”์œ„๋ฅผ 0~1๋กœ ์ •๊ทœํ™”๋ฅผ ์‹œ์ผœ์ฃผ๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค. ์ •๊ทœํ™”๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด ๋‹จ์œ„๊ฐ€ ๋™์ผํ•˜๊ฒŒ ๋˜๋ฉฐ ๋น„์œจ ๊ฐ’์œผ๋กœ ํ‘œ์‹œ๊ฐ€ ๋˜๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. # Train / Test ๋ฐ์ดํ„ฐ ๊ตฌ๋ถ„ ratio = 0.7 indexes = sample(2, nrow(iris), replace = TRUE, prob = c(ratio, 1 - ratio)) iris_train = iris[indexes == 1, 1:4] iris_test = iris[indexes == 2, 1:4] iris_train_labels = iris[indexes == 1, 5] iris_test_labels = iris[indexes == 2, 5] K = c() ACC = c() for(x in seq(1,80, by = 2)){ iris_mdl = knn(train=iris_train, test=iris_test, cl=iris_train_labels, k = x) CM = confusionMatrix(iris_test_labels, iris_mdl) accuracy = CM$overall[1] K = c(K, x) ACC = c(ACC, accuracy) } KNN_Result = data.frame( K = K, ACC = ACC ) ggplot(KNN_Result) + geom_point(aes(x = K, y = ACC)) + geom_line(aes(x = K, y = ACC),group = 1) + scale_x_continuous(breaks = seq(0,80, by = 10)) + theme_bw() + guides(col = FALSE) k๊ฐ€ ์ž‘์„ ๋•Œ๋Š” ๋น„๊ต์  ์ž˜ ๋งž์ถ”์ง€๋งŒ, k๊ฐ€ ๋งค์šฐ ์ปค์ง€๋‹ˆ ์ •ํ™•๋„๊ฐ€ ๋–จ์–ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ iris ๋ฐ์ดํ„ฐ๋Š” ์—ฐ์Šต์šฉ ๋ฐ์ดํ„ฐ์ด๊ธฐ์— ๋ถ„๋ฅ˜๊ฐ€ ๋งค์šฐ ์ž˜ ๋˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— kNN์ด ๋งค์šฐ ์ข‹๊ตฌ๋‚˜๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ธฐ์—๋Š” ๋ฌด๋ฆฌ๊ฐ€ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์…‹์ž…๋‹ˆ๋‹ค. ๊ตฐ์ง‘๋ถ„์„์€ ์‹ค์ œ ๋ถ„์„์—์„œ ๋Œ€๋‹จํžˆ ๋งŽ์ด ์“ฐ์ด๋Š” ๋ถ„์„์ž…๋‹ˆ๋‹ค. ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๋ชจ๋ธ๋ง์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋น„์Šทํ•œ ์œ ํ˜•๋ณ„๋กœ ๋ถ„๋ฆฌ์‹œํ‚จ ํ›„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜๋Š” ์ž‘์—…์ด ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ, ๋ฐ์ดํ„ฐ์˜ ์œ ์‚ฌ์„ฑ์— ๋”ฐ๋ฅธ ๊ตฐ์ง‘๋ถ„์„์€ ๋งค์šฐ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ChB5. ๊ธฐ๊ณ„ํ•™์Šต ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๊ธฐ๊ณ„ํ•™์Šต์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•˜๊ณ  ๋Œ€ํ‘œ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋งŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ 2ํŽธ์—์„œ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. A1. ๊ธฐ๊ณ„ํ•™์Šต์— ๋Œ€ํ•œ ์ •์˜ 1. ๊ธฐ๊ณ„ํ•™์Šต์— ๋Œ€ํ•œ ์ •์˜ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ํŒจํ„ด์„ ๋ฝ‘์•„๋‚ด๋Š” ์ž๋™ํ™” ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ธฐ๊ณ„ํ•™์Šต์ด๋ผ๊ณ  ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ํ•™์Šต์˜ ์ ์šฉ ๋ถ„์•ผ๋Š” ๋งค์šฐ ๋‹ค์–‘ํ•œ๋ฐ, ์˜ˆ์‹œ๋ฅผ ๋“ค์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ŠคํŒธ๋ฉ”์ผ ๋ถ„๋ฅ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณ ๊ฐ๋“ค์˜ ๊ตฌ๋งค ํŒจํ„ด ๋ถ„์„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ™˜์ž ์ง„๋‹จ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋น„์ „, ์Œ์„ฑ, ๋ฌธ์ž ๋“ฑ ๋งŽ์€ ๋ถ„์•ผ์— ์ ์šฉ์ด ๊ฐ€๋Šฅ ๊ธฐ๊ณ„ํ•™์Šต ๋ชจํ˜•์˜ ๊ฐ€์žฅ ํฐ ๋ชฉ์ ์€ ์˜ˆ์ธก ๋ชจํ˜•์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ข‹์€ ์˜ˆ์ธก ๋ชจํ˜•์ด๋ž€, ํ˜„์žฌ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ์˜ˆ์ธก๊ฐ’์„ ์ž˜ ๋งž์ถ”๋Š” ๋ชจํ˜•์„ ์ข‹์€ ๋ชจํ˜•์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ์ด๋Ÿฐ ๋ชจํ˜•์„ ์ผ๋ฐ˜ํ™”(generalize)๊ฐ€ ์ž˜ ๋˜์—ˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. A2. ๊ธฐ๊ณ„ํ•™์Šต์—์„œ ์ฃผ์˜ํ•  ์  2. ๊ธฐ๊ณ„ํ•™์Šต์—์„œ ์ฃผ์˜ํ•  ์  ๊ธฐ๊ณ„ํ•™์Šต์—์„œ ์˜ˆ์ธก ๋ชจํ˜•์˜ ์ •ํ™•์„ฑ์„ ๋–จ์–ดํŠธ๋ฆฌ๋Š” ๋ฌธ์ œ์ ์€ 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Underfitting Underfitting์€ Feature ๋ณ€์ˆ˜๊ฐ€ Response ๋ณ€์ˆ˜๋ฅผ ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. Overfitting Overfitting์€ Underfitting๊ณผ ๋Œ€์กฐ์ ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋„ˆ๋ฌด ๋ณต์žกํ•˜๊ณ  ๋ฐ์ดํ„ฐ์— ๋„ˆ๋ฌด ๋ฐ€์ ‘ํ•˜๊ฒŒ ์งœ์˜€์„ ๋•Œ, ์žก์Œ(noise)์— ์˜ˆ๋ฏผํ•ด์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. Over(Under) fitting์€ ์˜ˆ์‹œ๋ฅผ ๋“ค์ž๋ฉด, ์ด๋Ÿฐ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋Œ€ํ•™๊ต ํ•™๊ณผ์—์„œ ์ตœ์†Œ ๊ฐ•์˜ 1๊ฐœ ์ด์ƒ์€ ์‹œํ—˜ ์กฑ๋ณด๊ฐ€ ๋Œ์•„๋‹ค๋‹ˆ๊ธธ ๋งˆ๋ จ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ˜„๋ช…ํ•˜์‹  ๊ต์ˆ˜๋‹˜๋“ค๊ป˜์„œ๋Š” ์กฑ๋ณด ๊ทธ๋Œ€๋กœ ์‹œํ—˜๋ฌธ์ œ๋ฅผ ์ถœ์ œํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ, ์กฑ๋ณด์—์„œ 60%, ์•ˆ ๋‚˜์™”๋˜ ๋ฌธ์ œ 40%๋ฅผ ๋ฐฐ์น˜์‹œ์ผœ ๊ท ํ˜• ์žˆ๊ฒŒ ๋ฌธ์ œ๋ฅผ ๋‚ด์‹ญ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ, Underfitting์€ ๊ทธ๋ƒฅ ๊ณต๋ถ€๋ฅผ ์•ˆ ํ•ด์„œ ์‹œํ—˜์„ ๋ชป ๋ณด๋Š” ๊ฑฐ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Overfitting์€ ๊ณต๋ถ€๋Š” ์—ด์‹ฌํžˆ ํ–ˆ๋Š”๋ฐ ์กฑ๋ณด๋งŒ ๊ณต๋ถ€ํ•ด๊ฐ€์ง€๊ณ  ์ƒˆ๋กœ์šด ๋ฌธ์ œ๋ฅผ ๋ชป ํ’€์–ด ์‹œํ—˜์„ ๋ชป ๋ณด๋Š” ๊ฑฐ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Machine Learning์€ 'ํ•™์Šต'์ž…๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ํ•œํ…Œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šต์‹œํ‚ด์œผ๋กœ์จ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์™”์„ ๋•Œ, ๋ฌธ์ œ์—†์ด ์˜ˆ์ธก๊ฐ’์„ ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์žˆ๋Š๋ƒ๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ์ด์œ ๋กœ ํ•ญ์ƒ ์˜ˆ์ธก ๋ชจํ˜•์„ ํ•™์Šต์‹œํ‚ฌ ๋•Œ, Training Set๊ณผ Test Set์„ ๋ถ„๋ฆฌ์‹œ์ผœ ๋ชจํ˜•์˜ ์‹ ๋ขฐ๋„๋ฅผ ๋†’์ด๊ณ ์ž ํ•˜๋Š” ์•™์ƒ๋ธ” ๊ธฐ๋ฒ•์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. A3. ์•™์ƒ๋ธ”(Ensomble) ๊ธฐ๋ฒ• 3. ์•™์ƒ๋ธ”(Ensomble) ๊ธฐ๋ฒ• ๊ธฐ๊ณ„ํ•™์Šต์˜ ๋ชฉ์ ์€ ์ฃผ์–ด์ง„ ๊ณผ์ œ(๋ฐ์ดํ„ฐ)์— ๋Œ€ํ•ด์„œ ๊ฐ€์žฅ ์ •ํ™•ํ•œ ํ•˜๋‚˜์˜ ๋ชจํ˜•์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•™์ƒ๋ธ” ๊ธฐ๋ฒ•์€ ๋ชฉ์ ๊ณผ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅธ ์ ‘๊ทผ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ๋ชจํ˜•๋งŒ ๋งŒ๋“ค์ง€ ์•Š๊ณ , ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ชจํ˜•์„ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ ํ•œ ์„ธํŠธ๋กœ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ชจํ˜•๋“ค์˜ ์˜ˆ์ธก๊ฐ’์„ ์ง‘๊ณ„ ๋‚ด์„œ ๊ฒฐ๊ด๊ฐ’์„ ์‚ฐ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์„ Model ensemble์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•™์ƒ๋ธ” ๊ธฐ๋ฒ•์€ ์ „๋ฌธ๊ฐ€๋“ค์ด ๋ชจ์—ฌ ์•„์ด๋””์–ด๋ฅผ ํ•จ๊ป˜ ๋‚ด๋Š” ๊ฒƒ์ด ํ•œ ๋ช…์˜ ์ „๋ฌธ๊ฐ€๊ฐ€ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๊ฒฐ๊ณผ๊ฐ€ ๋” ์ข‹๋‹ค๋Š” ๊ฒฝํ—˜๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์‹œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ „๋ฌธ๊ฐ€๋“ค์ด ํ•จ๊ป˜ ์ผํ•  ๋•Œ์—ฌ๋„, ์ „๋ฌธ๊ฐ€๋Š” ๊ทธ๋ฃน์—์„œ์˜ ์ƒ๊ฐ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋…์ž์ ์œผ๋กœ ๊ฒฐ์ •์„ ๋‚ด๋ฆด ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•™์ƒ๋ธ” ๊ธฐ๋ฒ•๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์—ฌ๋Ÿฌ ๊ฐ€์ง€์˜ ๋ชจํ˜•์ด ์žˆ์–ด๋„ ๊ฐ ๋ชจํ˜•์ด ๋…๋ฆฝ์ ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋ฝ‘์•„๋ƒ…๋‹ˆ๋‹ค. ๊ทธ ํ›„, ๊ฐ ๋ชจํ˜•๋“ค์˜ ๊ฒฐ๊ด๊ฐ’์„ ์ง‘๊ณ„ ๋‚ด์–ด ์˜ˆ์ธก๊ฐ’์„ ์ •ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. Response ๋ณ€์ˆ˜๊ฐ€ Categorical ๋ณ€์ˆ˜ ์ผ ๋•Œ : ํˆฌํ‘œ<NAME>์œผ๋กœ ๊ฐ ๋ชจํ˜•์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ๋ฝ‘ํžŒ ๊ฐ’์ด ์˜ˆ์ธก ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. Response ๋ณ€์ˆ˜๊ฐ€ Numerical ๋ณ€์ˆ˜ ์ผ ๋•Œ : ํ‰๊ท  ํ˜น์€ ์ค‘์œ„์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•จ์œผ๋กœ์จ ์ค‘์‹ฌ ๊ฒฝํ–ฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์ด ์˜ˆ์ธก ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ๋Š” ์•™์ƒ๋ธ” ๊ธฐ๋ฒ•์—์„œ ๋Œ€ํ‘œ์ ์œผ๋กœ ์“ฐ์ด๋Š” 2๊ฐœ์˜ ์ ‘๊ทผ๋ฒ•(Bagging, Boosting)์— ๋Œ€ํ•ด ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Boosting Boosting ๊ธฐ๋ฒ•์€ ๊ฐ€์ค‘์น˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์•ฝํ•œ ํ•™์Šต๊ธฐ(Weak learners)๋ฅผ ๊ฐ•ํ•œ ํ•™์Šต๊ธฐ(Strong learners)๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ๋งํ•ฉ๋‹ˆ๋‹ค. Boosting ๊ธฐ๋ฒ•์€ ๋…๋ฆฝ์ ์ธ ๋ชจ๋ธ์ด ๋งŒ๋“ค์–ด์งˆ ๋•Œ๋งˆ๋‹ค ์ด์ „ ๋ชจ๋ธ์˜ ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜์˜ํ•œ ํ›„, ๋‹ค์Œ ๋ชจํ˜•์—๋Š” ์˜ค ๋ถ„๋ฅ˜ ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ์ฃผ๊ณ  ๋ชจํ˜•์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์ž๋ฉด, ํ‹€๋ฆฐ ์—ฐ์Šต ๋ฌธ์ œ์— ๋Œ€ํ•˜์—ฌ ๋‹ค์Œ ์‹œํ—˜ ๋•Œ๊นŒ์ง€ ํ‹€๋ฆฐ ์œ ํ˜•์˜ ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ๋” ๋งŽ์ด ํ’€๊ฒŒ ํ•จ์œผ๋กœ์จ(๊ฐ€์ค‘์น˜๋ฅผ ์˜ฌ๋ ค์คŒ์œผ๋กœ์จ) ์˜ค ๋ถ„๋ฅ˜๋˜์—ˆ๋˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ •๋ถ„๋ฅ˜๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •์„ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋จผ์ € Bootstrap ์ด๋ž€ ์šฉ์–ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Bootstrap ์ด๋ž€ ์˜ˆ๋ฅผ ๋“ค์–ด, '์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์—ฌ m ๊ฐœ๋ฅผ ๋ฝ‘๊ณ  ๊ทธ m ๊ฐœ์˜ ํ†ต๊ณ„ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ์ž‘์—…' ์ด ์ž‘์—…์„ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณตํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๊ฐ ์‹œํ–‰ ๋ณ„๋กœ ๊ณ„์‚ฐ๋œ ํ†ต๊ณ„ ๊ฐ’์ด ์Œ“์—ฌ ๋ถ„ํฌ๊ฐ€ ํ˜•์„ฑ์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. Bootsraping์€ ๋ณดํ†ต ์ƒ˜ํ”Œ ์ˆ˜๊ฐ€ ์ ์„ ๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ด๋ฅผ ๊ธฐ๊ณ„ํ•™์Šต์—์„œ ์–ด๋–ค ๋…ผ๋ฆฌ ์ˆœ์„œ๋กœ ์ง„ํ–‰๋˜๋Š”์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. N ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ, ๊ฐ ๊ฐœ๋ณ„ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฝ‘ํž ํ™•๋ฅ ์€ 1/N์ž…๋‹ˆ๋‹ค. 1/N์˜ ํ™•๋ฅ ๋กœ ๋ฝ‘ํžŒ ๋ฐ์ดํ„ฐ ์…‹(Training Set)์„ ๊ฐ€์ง€๊ณ  ์˜ˆ์ธก ๋ชจํ˜•์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋งŒ๋“ค์–ด์ง„ ์˜ˆ์ธก ๋ชจํ˜•์„ ํ‰๊ฐ€ํ•˜๊ณ  Error ๋ฐœ์ƒ ์ •๋„๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. Error, ์ฆ‰ ์˜ค ๋ถ„๋ฅ˜ ํŒ์ •์„ ๋ฐ›์€ ๋ฐ์ดํ„ฐ ์…‹๋“ค์— ๋Œ€ํ•˜์—ฌ ๋ฝ‘ํž ํ™•๋ฅ ์„ ์˜ฌ๋ ค ์ค€ ๋‹ค์Œ, Random Sampling์„ ๋‹ค์‹œ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ค ๋ถ„๋ฅ˜ ํŒ์ •์„ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋“ค์ด ๋ฝ‘ํž ํ™•๋ฅ ์˜ ๊ฐ€์ค‘์น˜ ์˜ฌ๋ฆด ๊ฒฝ์šฐ ๊ณ„์‚ฐ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. [ ] w [ ] 1 โˆ— ๋ฐ˜๋Œ€๋กœ ๋ฝ‘ํž ํ™•๋ฅ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋‚ด๋ฆด ๊ฒฝ์šฐ ๊ณ„์‚ฐ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. [ ] w [ ] 1 โˆ— ( โˆ’ ) ์˜ค ๋ถ„๋ฅ˜ ํŒ์ •์„ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋“ค์€ ๊ฐ€์ค‘์น˜๋ฅผ ๋” ๋ฐ›์Œ์œผ๋กœ์จ ๋‹ค์Œ Random Sampling ๋•Œ ๋ฝ‘ํž ํ™•๋ฅ ์ด ๋” ๋†’์•„์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ƒˆ๋กœ ๋ฝ‘ํžŒ Training Set์„ ๊ฐ€์ง€๊ณ  ์˜ˆ์ธก ๋ชจํ˜•์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์˜ค ๋ถ„๋ฅ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ์˜ค ๋ถ„๋ฅ˜ ๋ฐ์ดํ„ฐ๋“ค์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์กฐ์ •ํ•˜์—ฌ ๋‹ค์‹œ ๋ชจํ˜•์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์˜๋„ํ•œ ๋ชจํ˜• ๊ฐœ์ˆ˜์— ๋„๋‹ฌํ•˜๊ฒŒ ๋˜๋ฉด Bootstrap์ด ๋๋‚˜๊ฒŒ ๋˜๊ณ  ์ตœ์ข… ๊ฒฐ๊ด๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Boosting์€ ์ง„ํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ๋ฐ์ดํ„ฐ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋‹ฌ๋ฆฌํ•จ์œผ๋กœ์จ ์˜ˆ์ธก ๋ชจํ˜•์— ๋งŒ๋“ค์–ด์ง€๋Š” Training Set์˜ ๋ถ„ํฌ๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๋Š” ๊ฒƒ์ด ๊ทธ ๋ชฉ์ ์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ ์˜ค ๋ถ„๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ์—์„œ ํ”ํ•œ ๋ฌธ์ œ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๊ท ํ˜•์ด ์•ˆ ๋งž์ถฐ์ ธ ์žˆ์„ ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณ‘์›์—์„œ ํ™˜์ž๋“ค์˜ ์งˆ๋ณ‘ ์œ ๋ฌด๋ฅผ ํŒ๋‹จํ•˜๊ณ ์ž ์˜ˆ์ธก ๋ชจํ˜•์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•ด๋‹น ์งˆ๋ณ‘์— ๊ฑธ๋ฆฐ ์‚ฌ๋žŒ๋“ค์€ ์งˆ๋ณ‘์— ๊ฑธ๋ฆฌ์ง€ ์•Š์€ ์‚ฌ๋žŒ๋“ค๋ณด๋‹ค ์ ˆ๋Œ€์ ์œผ๋กœ ์ ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ์˜ ๋ถ„ํฌ๊ฐ€ ๊ท ํ˜• ์žˆ์ง€ ๋ชปํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ๊ธฐ๊ณ„ํ•™์Šต ๊ณผ์ •์—์„œ ์ƒ๋Œ€์ ์œผ๋กœ Minor ํ•œ ์งˆ๋ณ‘ ํ™˜์ž ๋ฐ์ดํ„ฐ๊ฐ€ ๊ด€์‹ฌ์„ ๊ฐ€์ง€์ง€ ๋ชปํ•˜๊ฒŒ ๋˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋Š” ์˜ˆ์ธก ๋ชจํ˜•์˜ ์˜ค ๋ถ„๋ฅ˜๋กœ ์ด์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด ์•™์ƒ๋ธ” ๊ธฐ๋ฒ•์—์„œ์˜ Boosting ์ ‘๊ทผ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. Bagging Bagging ๊ธฐ๋ฒ•์€ Boosting์— ๋น„ํ•ด์„œ๋Š” ์กฐ๊ธˆ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ชจํ˜•๋ณ„ ๋ณต์› ์ถ”์ถœ์„ ํ†ตํ•ด Training Set์„ ๋งŒ๋“ค๊ณ , ์˜ˆ์ธก ๋ชจํ˜•์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ๊ฐœ์ˆ˜๋งŒํผ์˜ ๋ชจํ˜•์„ ๋งŒ๋“ค์—ˆ์œผ๋ฉด, ๊ฐ ๋ชจํ˜•์˜ ๊ฒฐ๊ณผ๊ฐ’๋“ค์„ ์ง‘๊ณ„ํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฌผ์„ ๋ƒ…๋‹ˆ๋‹ค. ๊ฐ ๋ชจํ˜• ๋ณ„๋กœ Training Set์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์ธก ๋ชจํ˜•๋„ ๋‹น์—ฐํžˆ ๋ชจํ˜•๋ณ„๋กœ ์ฐจ์ด๊ฐ€ ์กด์žฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ •๋ง ์ด๊ฒŒ ๋์ž…๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•˜์ฃ ? ์ •๋ง์ž…๋‹ˆ๋‹ค. A4. Gradient Descent(๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•) Error Surface ํšŒ๊ท€๋ถ„์„์—์„œ์˜ Error๋Š” ์ด์ œ๊นŒ์ง€ ์–ธ๊ธ‰ํ•จ MSE๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. Error surface๋Š” ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฐ€์ค‘์น˜(์ผ๋ฐ˜์ ์œผ๋กœ ํ†ต๊ณ„ํ•™์—์„œ๋Š” ํšŒ๊ท€๊ณ„์ˆ˜ 0 ฮฒ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ , ํ•™์Šต ์ชฝ์—์„œ๋Š” ๊ฐ€์ค‘์ง€ [ ] w [ ] ๋กœ ํ‘œํ˜„ํ•˜๊ณ ๋Š” ํ•ฉ๋‹ˆ๋‹ค.) ๋“ค์— ๋Œ€ํ•œ ์กฐํ•ฉ์— ๋Œ€ํ•ด Error ๊ฐ’์„ ๊ณ„์‚ฐํ•˜์—ฌ ์‹œ๊ฐํ™”๋ฅผ ํ•˜๊ณ ์ž ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ†ต๊ณ„ํ•™์—์„œ๋Š” ์ตœ์†Œ์ œ๊ณฑ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ํšŒ๊ท€๊ณ„์ˆ˜๋ฅผ ์ถ”์ •ํ•˜์ง€๋งŒ, ๊ธฐ๊ณ„ํ•™์Šต์—์„œ์˜ ํšŒ๊ท€๊ณ„์ˆ˜ ์ถ”์ •์€ ์‚ด์ง ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ์ด Error Surface์—์„œ MSE๊ฐ€ ์ตœ์†Œ์ ์ด ๋˜๋Š” ๊ฐ€์ค‘์น˜์˜ ์กฐํ•ฉ์„ ์ฐพ๋Š” ๋ฐฉ์‹์œผ๋กœ ํšŒ๊ท€๊ณ„์ˆ˜๋ฅผ ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค. Error Surface๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋ณผ๋กํ•œ ๊ทธ๋ฆ‡ ๋ชจ์–‘์ž…๋‹ˆ๋‹ค. ๊ทธ ์˜๋ฏธ๋Š” MSE๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๋ฐ”๋‹ฅ์ด ์กด์žฌํ•  ๊ฒƒ์ด๋ฉฐ, ๊ทธ ๋ฐ”๋‹ฅ์„ Global minimum์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ ‘๊ทผ๋ฒ•์„ ์ €ํฌ๋Š” least squares optimization์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. Gradient Descent ์•ž์„œ ๋งํ–ˆ๋“ฏ์ด, ์„ ํ˜• ํšŒ๊ท€๋ถ„์„์—์„œ global minimum์„ ์ฐพ๊ธฐ ์œ„ํ•˜์—ฌ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฐ€์ค‘์น˜์˜ ์กฐํ•ฉ(ํšŒ๊ท€๊ณ„์ˆ˜์˜ ์กฐํ•ฉ)์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์€ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๊ฐ€ ๋ฐฉ๋Œ€ํ•  ๊ฒฝ์šฐ, ์—ฐ์‚ฐ ์†๋„๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์‹ค์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ์—๋Š” ์–ด๋ ค์šด ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, Error surface์—์„œ global minimum์ด ์กด์žฌํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊ธฐ๋ฐ˜์œผ๋กœ, ํšจ์œจ์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด gradient descent(๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•) ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Gradient Descent(๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•)์˜ ์›๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•ž์„ ๋ณด๊ธฐ ํž˜๋“ค ์ •๋„๋กœ, ์•ˆ๊ฐœ๊ฐ€ ๋‚€ ํ˜‘๊ณก์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ์•ž์ด ๋ณด์ด์ง€ ์•Š์•„, ํ˜‘๊ณก์„ ๋‚ด๋ ค๊ฐ€๋Š” ๊ธธ์„ ์ฐพ์„ ์ˆ˜๊ฐ€ ์—†์ง€๋งŒ, ๋ฐ”๋กœ ์•ž์—์„œ์˜ ํ˜‘๊ณก์˜ ๊ฒฝ์‚ฌ๋Š” ํ™•์ธํ•  ์ˆ˜๋Š” ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ๊ฐ€ ๋‚ด๋ ค๊ฐ€๋ฉด ํ•ด๋‹น ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€๊ณ , ๊ฒฝ์‚ฌ๊ฐ€ ์˜ฌ๋ผ๊ฐ€๋ฉด ๋‚ด๋ ค๊ฐ€๋Š” ๊ฒฝ์‚ฌ๋ฅผ ์ฐพ์•„ ๋ฐฉํ–ฅ์„ ์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊พธ์ค€ํžˆ ์กฐ์‹ฌํžˆ ๋‚ด๋ ค๊ฐ€๋‹ค ๋ณด๋ฉด, ์–ธ์  ๊ฐ€๋Š” ๋ฐ”๋‹ฅ์— ๋„์ฐฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ทธ๋Œ€๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์›๋ฆฌ์— ๋น„์œ ๋ฅผ ํ•˜๋ฉด, Gradient Descent๋Š” ์ž„์˜์˜ ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.(์œ„์น˜ : ๊ฐ€์ค‘์น˜, ํšŒ๊ท€๊ณ„์ˆ˜) ์ž„์˜๋กœ ์ฃผ์–ด์ง€๋Š” ๊ฐ€์ค‘์น˜๋Š” ๋ฒ”์œ„๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ „์ฒด ์กฐํ•ฉ์ด ์•„๋‹Œ, ํ•ด๋‹น ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ๋ฒ”์œ„๋กœ ์žก์Šต๋‹ˆ๋‹ค. S (Sum of Squared Error)์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. S๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์ง€์—ญ(๊ฐ€์ค‘์น˜, ํšŒ๊ท€๊ณ„์ˆ˜์˜ ๋ฒ”์œ„ ๋‚ด)์—์„œ์˜ Error Surface๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Error surface์—์„œ ๊ฒฝ์‚ฌ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ๊ฐ€ ๋‚ด๋ ค๊ฐ€๋Š” ๊ณณ์„ ๋ฐฉํ–ฅ์œผ๋กœ ์„ค์ •ํ•œ ํ›„, ์ƒˆ๋กœ์šด Error Surface๋กœ ์ด๋™์„ ํ•ฉ๋‹ˆ๋‹ค. ๊ณ„์‚ฐ์ด ๋ฌด์ˆ˜ํžˆ ๋ฐ˜๋ณต๋˜๋‹ค ๋ณด๋ฉด, global minumum์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜๋“ค์€ ๊ณ„์‚ฐ์ด ์ง„ํ–‰๋จ์— ๋”ฐ๋ผ์„œ ํ•œ๊ณณ์œผ๋กœ ์ˆ˜๋ ดํ•˜๊ฒŒ ๋˜๋Š” ๋ฐ, ํ•ด๋‹น ๋ถ€๋ถ„์ด Globar minimum์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ Gradient Descent๋Š” ๋‹ค์Œ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•œ๋‹ค๋ฉด ๋” ์ˆ˜์›”ํ•ด์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ, learning rate๋ผ๋Š” ๊ฒƒ์ด ๋“ฑ์žฅํ•˜๋Š”๋ฐ, ์ด๋Š” ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ๊ณ„๊ณก์—์„œ ํ•œ ๊ฑธ์Œ์”ฉ ๋‚ด๋ ค๊ฐˆ ๋•Œ ๋ณดํญ์„ ์–ด๋Š ์ •๋„์˜ ํฌ๋ฆฌ๊ณ  ์„ค์ •ํ•˜๋Š” ์ •๋„๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ณดํญ์ด ํฌ๋ฉด ์”ฉ์”ฉํ•˜๊ฒŒ ๊ฑธ์„ ์ˆ˜๋Š” ์žˆ์ง€๋งŒ, ๋ฐฉํ–ฅ์„ ์ œ๋Œ€๋กœ ์žก์ง€ ๋ชปํ•ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ž˜๋ชป ์ž‘๋™ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ ๋ณดํญ์„ ๋„ˆ๋ฌด ์งง๊ฒŒ ํ•˜๋ฉด ๋ฐฉํ–ฅ์€ ์ •ํ™•ํ•˜๊ฒŒ ์ •ํ•ด์„œ ๋‚ด๋ ค๊ฐˆ ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ์†๋„๊ฐ€ ๋„ˆ๋ฌด ๋Š๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ €ํฌ์˜ ํ‡ด๊ทผ์‹œ๊ฐ„์— ๋งค์šฐ ๋ฐฉํ•ด๋˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ด ์—ญ์‹œ๋„ learning rate๋ฅผ ์ž˜ ์„ค์ •ํ•˜๋Š” ์„ผ์Šค๊ฐ€ ์ค‘์š”ํ•œ ์˜์—ญ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ๊ทธ๋ž˜ํ”„๋Š” ๊ฐ€์ค‘์น˜๊ฐ€ ๋ณ€ํ•  ๋•Œ๋งˆ๋‹ค Error ๊ฐ’์ด ์–ด๋–ป๊ฒŒ ๋‚ฎ์•„์ง€๋Š”์ง€ ๋‚˜ํƒ€๋ƒˆ์Šต๋‹ˆ๋‹ค. ํ”ํžˆ, Cost function์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์™ผ์ชฝ์€ learning rate๊ฐ€ ๋„ˆ๋ฌด ํฌ๊ฒŒ ์žกํ˜”์„ ๊ฒฝ์šฐ, ๋ฐฉํ–ฅ์„ ์ž˜๋ชป ์žก์•„ ์˜คํžˆ๋ ค ์˜ฌ๋ผ๊ฐ€๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ์€ ๋ฐ˜๋Œ€๋กœ learning rate๊ฐ€ ๋„ˆ๋ฌด ๋‚ฎ๊ฒŒ ์žกํ˜€, ๋‚ด๋ ค๊ฐ€๋Š” ๊ณผ์ •์ด ๋งค์šฐ ๋ŽŒ์ง€๊ฒŒ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. learning rate๊ฐ€ ๋‚ด๋ ค๊ฐ€๋Š” ๋ณดํญ์„ ์ •ํ•œ๋‹ค๋ฉด, ๋ฐฉํ–ฅ์„ ์ •ํ•˜๋Š” ๊ฒƒ์€ Delta Function์ด ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ๊ณ„์‚ฐ ๋ฐฉ์‹์€ ํŽธ๋ฏธ๋ถ„์„ ํ†ตํ•ด ์ง„ํ–‰์ด ๋˜๋Š”๋ฐ, ์ˆ˜์‹์ด ์กฐ๊ธˆ ๋งŽ์ด ๋ณต์žกํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๊ฐ„๋‹จํ•˜๊ฒŒ ์›๋ฆฌ๋ฅผ ์„ค๋ช…ํ•ด ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„๋“ค๋„ ์•„์‹œ๋‹ค์‹œํ”ผ ๋ฏธ๋ถ„์„ ํ™œ์šฉํ•œ ์ตœ์†Œ ํ˜น์€ ์ตœ๋Œ€์ ์„ ์ฐพ๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. Delta Function์€ error surface๊ฐ€ ๋‚ฎ์•„์ง€๋Š” ๋ฐฉํ–ฅ์„ ์ œ์‹œํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. Error Loss function ์ •์˜ 2 E r r o s u c i n 2 ( w X ) 1 ฮฃ i 1 ( i ( โ‹… i ) ) ๋จผ์ € Error function์„ ์ •์˜ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ 2 ๋Š” SSE๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ( โ‹… i ) ๊ฐ€ ๋ฒกํ„ฐ ํ˜•ํƒœ๋กœ ํ‘œ์‹œ๋˜์–ด ์ด์งˆ๊ฐ์ด ๋“œ๋Š” ๊ฒƒ์ผ ๋ฟ, ํ’€์–ด์“ฐ๋ฉด [ ] w [ ] x [ ] ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. Error Surface์—์„œ ํŽธ๋ฏธ๋ถ„์„ ํ†ตํ•ด global minimum ์ฐพ๊ธฐ โˆ‚ [ ] 1 ฮฃ ( i ( [ ] w [ ] i [ ] ) ) = โˆ‚ w [ ] 1 ฮฃ ( i ( [ ] w [ ] i [ ] ) ) = Loss Function์„ ๊ฐ ๊ฐ€์ค‘์น˜(๊ธฐ์šธ๊ธฐ, ํšŒ๊ท€๊ณ„์ˆ˜)๋กœ ํŽธ๋ฏธ๋ถ„์„ ํ•˜์—ฌ 0 ์ด ๋˜๋Š” ์ง€์ ์„ ์ฐพ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํšŒ๊ท€๋ถ„์„์—์„œ ์ตœ์†Œ ์ œ๊ณฑ ๋ฒ•๊ณผ ๋น„์Šทํ•œ ๋ฐฉ์‹์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํšŒ๊ท€์‹์ด Multiple์ธ ๊ฒฝ์šฐ์—๋Š” ์‹์˜ ํ˜•ํƒœ๋Š” ์กฐ๊ธˆ ๋ณ€ํ•˜๊ฒ ์ง€๋งŒ ์›๋ฆฌ๋Š” ๋ฐ”๋€Œ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. โˆ‚ [ ] 2 ( w X ) ฮฃ = n ( ( i M ( i ) ) x [ ] ) r o D l a ( , [ ] ) โˆ’ โˆ‚ [ ] 2 ( w X ) = i 1 ( ( i M ( ๋ณต์žกํ•œ ํŽธ๋ฏธ๋ถ„์„ ๊ณ„์‚ฐํ•˜๋ฉด Delta Function์ด ๊ณ„์‚ฐ์ด ๋ฉ๋‹ˆ๋‹ค. ๋ฐฉํ–ฅ ์„ค์ • ๋ฐฉ๋ฒ• [ ] w [ ] ฮฑ i 1 ( ( i M ( i ) ) i [ ] ) ์ด๋ ‡๊ฒŒ learning rate ์™€ r o D l a ๋ฅผ ๊ณฑํ•ด์ค€ ๊ฐ’์„ ๋”ํ•ด์คŒ์œผ๋กœ์จ ๋ฐฉํ–ฅ๊ณผ ๋ณดํญ์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Learning rate ๋ฐ ์ดˆ๊ธฐ ๊ฐ€์ค‘์น˜ ์„ค์ • ๋ฐฉ๋ฒ• ๋งˆ์ง€๋ง‰์œผ๋กœ ์ตœ์ ์˜ learning rate ๋ฐ ์ดˆ๊ธฐ ๊ฐ€์ค‘์น˜ ์„ค์ • ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์ฃผ๋Š” ๊ทธ๋ ‡๋‹ค ํ•  ์ด๋ก ์€ ์—†๋‹ค๋Š” ๊ฒƒ์ด ํ•™๊ณ„์˜ ์ •์„ค์ž…๋‹ˆ๋‹ค. ์ดˆ๊ธฐ ๊ฐ€์ค‘์น˜ ์„ค์ •์€ ๋ณ€์ˆ˜๋ฅผ Normalizaton์„ ํ•œ ํ›„ ์ถ”์ •ํ•˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ Normalization ์ด๋ž€, o m l z t o ( ) x m n ( ) a ( ) m n ( ) ๋ณ€ํ™˜์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ณ€ํ™˜ ํ›„์— x ๊ฐ’์€ [0,1] ์‚ฌ์ด์˜ ์ƒ๋Œ€์ ์ธ ๋น„์œจ ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. Learning rate ๋ฐ ์ดˆ๊ธฐ ๊ฐ€์ค‘์น˜ ์„ค์ •์€ ๊ฒฝํ—˜์ ์ธ ํŒ๋‹จ์—์„œ ๊ฒฐ์ •์„ ๋‚ด๋ฆฌ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด ์—ด์‹ฌํžˆ ๋ฐ˜๋ณต๋ฌธ ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ•ด์„œ ์‹คํ—˜ํ•˜๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค. A5. ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•(R Code) 5. ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•(R Code) Gradient Descent๋ฅผ R code๋กœ ๊ตฌํ˜„ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ(๋‚œ์ˆ˜) x = runif(300, -10,10) # ๊ท ์ผ ๋ถ„ํฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ Noise = rnorm(n = 300, mean = 0 , sd = 3) y = x + Noise DF = data.frame(x = x, y = y) library(ggplot2) ggplot(DF, aes(x= x, y= y)) + geom_point(col = 'royalblue') + theme_bw() Learning Rate ์„ค์ • alpha = 0.01 ์ดˆ๊ธฐ ๊ฐ€์ค‘์น˜ ํ–‰๋ ฌ ์ƒ์„ฑ Weights = matrix(c(0,0),nrow = 2) Weights [,1] [1, ] 0 [2, ] 0 ํšŒ๊ท€์‹ ๊ณ„์‚ฐ์„ ์œ„ํ•œ ํ–‰๋ ฌ ์ƒ์„ฑ ์ด๋Ÿฌํ•œ ์‹์„ R์—์„œ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. # ํ–‰๋ ฌ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๊ธฐ X = matrix(x) X = cbind(1, X) colnames(X) = c("V1", "V2") # Error ๊ณ„์‚ฐ # %*%๋Š” ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Error = function(x, y, Weight){ sum(( y - x %*% Weight)^2) / (2*length(y)) } ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•™์Šต [ ] w [ ] ฮฑ i 1 ( ( i M ( i ) ) i [ ] ) ํ•™์Šต์„ ๋Œ๋ฆฌ๊ธฐ ์ „์—, Error(Cost) ๊ฐ’๊ณผ ๊ฐ€์ค‘์น˜(ํšŒ๊ท€๊ณ„์ˆ˜)๊ฐ€ ์ €์žฅ๋  ๋นˆ ๊ณต๊ฐ„์„ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Error_Surface = c() Weight_Value = list() for ( i in 1 : 300){ # X๋Š” (300,2) ํ–‰๋ ฌ # Weights๋Š” (2,1) ํ–‰๋ ฌ # X * Weights => (300,1) ํ–‰๋ ฌ[๊ฐ ๋ฐ์ดํ„ฐ์—์„œ์˜ Error ์—ฐ์‚ฐ] error = (X %*% Weights - y) # Delta Funtion ๊ณ„์‚ฐ Delta_function = t(X) %*% error / length(y) # ๊ฐ€์ค‘์น˜ ์ˆ˜์ • Weights = Weights - alpha * Delta_function Error_Surface[i] = Error(X, y, Weights) Weight_Value[[i]] = Weights } ์‹œ๊ฐํ™” p = ggplot(DF, aes(x = x, y = y)) + geom_point(col = "royalblue", alpha = 0.4) + theme_bw() for (i in 1:300) { p = p + geom_abline(slope = Weight_Value[[i]][2], intercept = Weight_Value[[i]][1], col = "red", alpha = 0.4) } ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ€์ค‘์น˜(ํšŒ๊ท€๊ณ„์ˆ˜)๊ฐ€ ์กฐ๊ธˆ์”ฉ ์›€์ง์ด๋ฉด์„œ ์ตœ์ ์˜ ํšŒ๊ท€์„ ์„ ์ฐพ์•„๊ฐ€๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. DF$num = 1:300 DF$Error_value = Error_Surface ggplot(DF) + geom_line(aes(x = num, y = Error_value), group = 1) + geom_point(aes(x = num, y = Error_value)) + theme_bw() + ggtitle("Error Function") + xlab("Num of iterations") Error ๊ฐ’ ๋˜ํ•œ ๊ฐ์†Œํ•˜๋ฉด์„œ ์ตœ์ ์˜ ๊ฐ€์ค‘์น˜(๊ธฐ์šธ๊ธฐ)์— ์ˆ˜๋ ดํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์†Œ์ œ๊ณฑ๋ฒ•์„ ์ด์šฉํ•œ ์„ ํ˜•ํšŒ๊ท€์‹๊ณผ์˜ ๋น„๊ต ์ผ๋ฐ˜ ์„ ํ˜•ํšŒ๊ท€(์ตœ์†Œ์ œ๊ณฑ๋ฒ•) REG = lm(y ~ x) A = summary(REG) print(paste("R Square :", round(A$r.squared, 4))) [1] "R Square : 0.7691" Gradient ๊ฒฝ์‚ฌํ•˜๊ฐ•๋ฒ•์œผ๋กœ ํšŒ๊ท€์‹์„ ์ถ”์ •ํ–ˆ์„ ๊ฒฝ์šฐ, 2 ๋ฅผ ๊ตฌํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ๊ณ„์‚ฐํ•œ ๊ฐ€์ค‘์น˜๋“ค์€ ์•ž์„œ ์ €์žฅ ๊ณต๊ฐ„์œผ๋กœ ๋งŒ๋“ค์–ด๋‘” weight_value์— ์ €์žฅ์ด ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. GR_MODEL = Weight_Value[[300]][1] + Weight_Value[[300]][2] * x actual = y rss = sum((GR_MODEL - actual)^2) tss = sum((actual - mean(actual))^2) rsq = 1 - rss/tss print(paste("R square :", round(rsq, 4))) [1] "R square : 0.7691" ์ตœ์†Œ ์ œ๊ณฑ ๋ฒ•๊ณผ ๋น„๊ตํ•˜์˜€์„ ๋•Œ, ๋น„์Šทํ•œ ๊ฐ’์ด ๊ณ„์‚ฐ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. A6. ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด 6. ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด \ ์‹ฌ๋ฆฌ ํ…Œ์ŠคํŠธ ์ฑ…์„ ๋ณด๋ฉด ๋ณต์žกํ•œ ์ˆœ์„œ๋„๋ฅผ ๋”ฐ๋ผ '๋‹น์‹ ์€ ์–ด๋–ค ์œ ํ˜•์˜ ์‚ฌ๋žŒ์ž…๋‹ˆ๋‹ค.'๋ผ๊ณ  ์•Œ๋ ค์ฃผ๋Š” ํŽ˜์ด์ง€๋ฅผ ๋ณธ ์ ์ด ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๋Š” ๋น„์Šทํ•œ ํ˜•ํƒœ์˜ ๋ถ„๋ฅ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‚จ์ž 100๋ช…, ์—ฌ์ž 100๋ช…์”ฉ ์ด 200๋ช…์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ์ง‘๋‹จ์—์„œ ๋‚จ๋…€๋ฅผ ๋ถ„๋ฆฌ์‹œํ‚ค๋Š” ๋ถ„๋ฅ˜ ๊ทœ์น™ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŒ๋“ค๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์ƒ๋ฌผํ•™์  ํŠน์„ฑ์€ ๋ถ„๋ฅ˜ ๊ทœ์น™์— ์‚ฌ์šฉํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜ ๊ทœ์น™ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŒ๋“ค๊ธฐ์— ์•ž์„œ, ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด์—์„œ ํ•„์ˆ˜์ ์œผ๋กœ ์•Œ์•„์•ผ ๋˜๋Š” ๊ฐœ๋…์€ ๋ถˆ์ˆœ๋„(Impurity)์ž…๋‹ˆ๋‹ค. ๋ถˆ์ˆœ๋„๋Š” ๋ฐ์ดํ„ฐ ๋‚ด์—์„œ ์—ฌ๋Ÿฌ ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์„ž์—ฌ ์žˆ์–ด, ์›ํ•˜๋Š” ํ‘œ๋ณธ์„ ๋ฝ‘์„ ํ™•๋ฅ ์ด ์ ์„ ์ˆ˜๋ก ์ปค์ง€๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ†ต๊ณ„ํ•™๊ณผ ํ•™๋ถ€์ƒ์„ ๋ฝ‘๊ณ  ์‹ถ์„ ๋•Œ, ๋‹ค๋ฅธ ํ•™๊ณผ์˜ ๋น„์œจ์ด ๋†’์•„์งˆ์ˆ˜๋ก, ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ†ต๊ณ„ํ•™๊ณผ๋ฅผ ๋ฝ‘์„ ํ™•๋ฅ ์€ ๊ฐ์†Œํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ˜๋Œ€๋กœ ๋ถˆ์ˆœ๋„๊ฐ€ ์ฆ๊ฐ€ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ถˆ์ˆœ๋„๋ฅผ ์ง€ํ‘œ๋กœ ๋งŒ๋“  ๊ฒƒ์„ ์—”ํŠธ๋กœํ”ผ(Entropy)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ( ) โˆ’ i 1 ( [ = ] l g ( [ = ] ) ) [ = ] P o a i i y f l m n t s h t p i ๋ถˆ์ˆœ๋„๊ฐ€ ๋†’์„์ˆ˜๋ก ๋ฐ์ดํ„ฐ์—์„œ ํŠน์ • ๋ ˆ๋ฒจ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฝ‘ํž ํ™•๋ฅ ์€ ๋‚ฎ์•„์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์—”ํŠธ๋กœํ”ผ๋Š” ๋งค์šฐ ์ž‘์€ ํ™•๋ฅ  ๊ฐ’์„ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๊ธฐ์—, log ๋ณ€ํ™˜์„ ํ•ด์คŒ์œผ๋กœ์จ ๊ฐ’์ด ํฐ ์Œ์ˆ˜๋ฅผ ๊ฐ€์ง€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์Œ์ˆ˜๋ฅผ ์ทจํ•ด์ฃผ๋ฉด ๋ถˆ์ˆœ๋„๊ฐ€ ๋†’์€ ๊ฐ’์€ ๋†’์€ ์—”ํŠธ๋กœํ”ผ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๋Š” ์—”ํŠธ๋กœํ”ผ๋ฅผ ์ตœ๋Œ€ํ•œ ๋‚ฎ์ถ”๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๋ถ„๋ฅ˜ ๊ทœ์น™์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ์†Œํ•œ ์—”ํŠธ๋กœํ”ผ๋ฅผ ์ •๋ณด ํš๋“(Information Gain)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ฒ˜์Œ์˜ ์˜ˆ์‹œ๋กœ ๋Œ์•„๊ฐ€์„œ, ๋‚จ๋…€ 200๋ช…์„ ๋ถ„๋ฅ˜ํ•˜๋Š” ๊ทœ์น™์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋‚จ/์—ฌ๋ฅผ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ๊ทœ์น™์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•˜์—ฌ, ์ €ํฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์„ฑ์„ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋จธ๋ฆฌ ๊ธธ์ด x(cm) ์ด์ƒ ์—ฌ๋ถ€ ๋ฐ˜์ง€ ์ฐฉ์šฉ ์—ฌ๋ถ€ ๊ท€๊ฑธ์ด ์ฐฉ์šฉ ์—ฌ๋ถ€ ์†ํ†ฑ ์ผ€์–ด ์—ฌ๋ถ€ ์•ˆ๊ฒฝ ์ฐฉ์šฉ ์—ฌ๋ถ€ ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด์—์„œ๋Š” ๊ฐ๊ฐ์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ •๋ณด ํš๋“์„ ์ธก์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ •๋ณด ํš๋“์ด ๊ฐ€์žฅ ํฐ ํŠน์„ฑ์ด ์ฒซ ๋ฒˆ์งธ ๊ฐ€์ง€์— ์ž๋ฆฌ๋ฅผ ์žก๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์†ํ†ฑ ์ผ€์–ด ์—ฌ๋ถ€๊ฐ€ ์ •๋ณด ํš๋“์ด ๊ฐ€์žฅ ๋†’์€ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ๋ถ„๋ฅ˜ ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. A7. ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ 7. ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ๋Š” ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๋ฅผ ๋งŽ์ด ์‹ฌ์–ด, ์ˆฒ์„ ๋งŒ๋“ ๋‹ค๋Š” ์˜๋ฏธ๋กœ Forest๋ผ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ Random์ด ๋ถ™์€ ์ด์œ ๋Š” ์ˆฒ์— ์‹ฌ๋Š” ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๋“ค์— ์“ฐ์ด๋Š” ํŠน์„ฑ๋“ค์„ ๋žœ๋คํ•˜๊ฒŒ ๋ฝ‘์•„ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ์—์„œ ํŠน์„ฑ๋“ค์„ ์ž„์˜๋กœ ์„ ํƒํ•˜์—ฌ ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๋ฅผ ๋งŒ๋“œ๋Š” ์ด์œ ๋Š” ๋ชจ๋“  ํŠน์„ฑ๋“ค์˜ ์กฐํ•ฉ์„ ๊ณ ๋ คํ•˜๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค. ๊ฐ™์€ ์กฐํ•ฉ์˜ ํŠน์„ฑ์œผ๋กœ ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๋ฅผ ๋งŒ๋“ค ๊ฒฝ์šฐ, ๋Œ€๋ถ€๋ถ„์˜ ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๋Š” ๋Š˜ ์‚ฌ์šฉํ•˜๋Š” ํŠน์„ฑ๋งŒ ์‚ฌ์šฉํ•  ํ™•๋ฅ ์ด ๋†’์Šต๋‹ˆ๋‹ค. ํŠน์„ฑ์„ ์ž„์˜์ ์œผ๋กœ ๋ฝ‘์•„ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•จ์œผ๋กœ์จ, ๋ชจ๋“  ์š”์†Œ๋ฅผ ๊ณ ๋ คํ•œ ์ˆฒ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ถ„๋ฅ˜ ๊ฒฐ๊ณผ ๋ฐ ์˜ˆ์ธก์€ ์ˆฒ์— ์žˆ๋Š” ๊ฐ ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๋“ค์ด ์ถœ๋ ฅํ•œ ๊ฒฐ๊ณผ๋“ค์„ ํˆฌํ‘œํ•˜์—ฌ ์ข…ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ˆฒ์— ๋‚˜๋ฌด๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก, ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ์˜ ๊ฒฐ๊ณผ๋Š” ๋” ์ •๊ตํ•˜๊ฒŒ ๋˜๋‚˜, ์–ด๋Š ์‹œ์ ์—์„œ๋ถ€ํ„ฐ๋Š” ์ •ํ™•๋„๊ฐ€ ์ˆ˜๋ ดํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. A8. ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด, ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ(R Code) 8. ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด, ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ(R Code) ํŒจํ‚ค์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ library(C50) library(caret) library(xtable) Train / Test Set ๋‚˜๋ˆ„๊ธฐ HR = read.csv("D:\Drop box\DATA SET\\HR_comma_sep.csv") SL = sample(1:nrow(HR), nrow(HR) * 0.7, replace = FALSE) HR$left = as.factor(HR$left) TRAIN = HR[SL, ] TEST = HR[-SL, ] FEATURE = TRAIN[, c(1:6, 8:10)] RESPONSE = TRAIN[, 7] ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด ๋ถ„๋ฅ˜ ๊ทœ์น™ ์ƒ์„ฑ tree = C5.0(FEATURE, RESPONSE, control = C5.0Control(noGlobalPruning = FALSE, minCases = 100), trials = 10) plot(tree) C5.0์€ Tree ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ช…๋ น์–ด ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. trials๋Š” boosting์„ ์‹คํ–‰ํ•˜๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค. minCasese ์˜ต์…˜์€ ์ตœ์ข… ๊ฒฐ๊ณผ ๋…ธ๋“œ์— ์ตœ์†Œ ๋ช‡ ๊ฐœ์˜ ๊ด€์ธก์น˜๋ฅผ ํฌํ•จํ•ด์•ผ ๋˜๋Š”์ง€ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค. ํšŒ์ƒ‰์€ 0(์ด์ง์„ ํ•˜์ง€ ์•Š์€ ์ง์›), ๊ฒ€์€์ƒ‰์€ 1(์ด์ง์„ ํ•œ ์ง์›)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ถ„๋ฅ˜ ๊ทœ์น™์— ๋”ฐ๋ผ ๋ถ„๋ฅ˜ ์˜ค์ฐจ์œจ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ mincases ์˜ต์…˜ ๊ฐ’์„ ๋†’๊ฒŒ ์กฐ์ •ํ•ด ์ฃผ๋ฉด, ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๊ฐ€ ๋‹จ์ˆœํ•ด์ง‘๋‹ˆ๋‹ค. tree = C5.0(FEATURE, RESPONSE, control = C5.0Control(noGlobalPruning = FALSE, minCases = 300),trials=10) plot(tree) TEST SET ๊ฒ€์ฆ y_pred = predict(tree, newdata = TEST) confusionMatrix(y_pred, TEST$left) Confusion Matrix and Statistics Reference Prediction 0 1 0 3435 87 1 18 960 Accuracy : 0.9767 95% CI : (0.9718, 0.9809) No Information Rate : 0.7673 P-Value [Acc > NIR] : < 2.2e-16 Kappa : 0.9331 Mcnemar's Test P-Value : 3.22e-11 Sensitivity : 0.9948 Specificity : 0.9169 Pos Pred Value : 0.9753 Neg Pred Value : 0.9816 Prevalence : 0.7673 Detection Rate : 0.7633 Detection Prevalence : 0.7827 Balanced Accuracy : 0.9558 'Positive' Class : 0 ์•ž์„ , ๋กœ์ง€์Šคํ‹ฑ ํšŒ๊ท€๋ถ„์„๋ณด๋‹ค ๋ถ„๋ฅ˜ ๊ฒฐ๊ณผ๊ฐ€ ๋” ์ข‹์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ ### Random Forest library(randomForest) rf.fit = randomForest(left ~ ., data = TRAIN, mtry = 3, ntree = 200, importance = T) y_pred = predict(rf.fit, TEST) confusionMatrix(y_pred, TEST$left) Confusion Matrix and Statistics Reference Prediction 0 1 0 3448 33 1 5 1014 Accuracy : 0.9916 95% CI : (0.9884, 0.994) No Information Rate : 0.7673 P-Value [Acc > NIR] : < 2.2e-16 Kappa : 0.9761 Mcnemar's Test P-Value : 1.187e-05 Sensitivity : 0.9986 Specificity : 0.9685 Pos Pred Value : 0.9905 Neg Pred Value : 0.9951 Prevalence : 0.7673 Detection Rate : 0.7662 Detection Prevalence : 0.7736 Balanced Accuracy : 0.9835 'Positive' Class : 0 mtry๋Š” ๋žœ๋ค์œผ๋กœ ํˆฌ์ž…ํ•  ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ntree๋Š” ๋ช‡ ๊ฐœ์˜ ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๋ฅผ ๋งŒ๋“ค์ง€ ์ •ํ•ด์ฃผ๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๋ณด๋‹ค ๋” ๋†’์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. varImpPlot(rf.fit, type = 2, pch = 19, col = 1, cex = 1, main = "") importance = T ์˜ต์…˜์„ ์คŒ์œผ๋กœ์จ, ํŠน์„ฑ์˜ ์ค‘์š”๋„ plot์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. plot(rf.fit$err.rate[, 1], col = "red") Random Forest๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋‚ด๋ถ€์—์„œ Train / Test Set์„ ๋‚˜๋ˆ ์„œ ๊ฒ€์ฆ์„ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ Train Set์— ํฌํ•จ๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋“ค์„ Out of Bag์ด๋ผ ํ•˜์—ฌ, ์˜ค๋ฅ˜์œจ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น plot์€ ๋žœ๋ค ํฌ๋ ˆ์ŠคํŠธ ๋‚ด๋ถ€์—์„œ ์˜์‚ฌ๊ฒฐ์ • ๋‚˜๋ฌด๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์˜ค๋ฅ˜์œจ์ด ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋ฅ˜์œจ์ด ์ˆ˜๋ ดํ•˜๋Š” ๊ฑฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ROC ์ปค๋ธŒ ๊ธฐ๊ณ„ํ•™์Šต ๋ฐฉ๋ฒ•๋ก ์„ ํ™œ์šฉํ•œ ๊ฒฐ๊ด๊ฐ’์— ๋Œ€ํ•ด์„œ๋„ ๋ชจํ˜•์˜ ์„ฑ๋Šฅ ํ‰๊ฐ€๋ฅผ ์œ„ํ•œ ROC ์ปค๋ธŒ๋ฅผ ๋งŒ๋“ค ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€์‹ ์— predict ๋‹จ๊ณ„์—์„œ ํ™•๋ฅ  ๊ฐ’์„ ์ถ”์ •ํ•˜๋„๋ก ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋น„๊ต๋ฅผ ์œ„ํ•˜์—ฌ ์ „์— ์ง„ํ–‰ํ–ˆ๋˜ ๋กœ์ง€์Šคํ‹ฑ ๋ชจํ˜•๋„ ๋‹ค์‹œ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. GLM = step(glm(left ~ ., data = TRAIN, family = binomial(link = "logit"))) Start: AIC=9038.94 left ~ satisfaction_level + last_evaluation + number_project + average_montly_hours + time_spend_company + Work_accident + promotion_last_5years + sales + salary Df Deviance AIC <none> 9000.9 9038.9 - sales 9 9027.9 9047.9 - last_evaluation 1 9012.4 9048.4 - promotion_last_5years 1 9036.2 9072.2 - average_montly_hours 1 9068.3 9104.3 - number_project 1 9174.9 9210.9 - time_spend_company 1 9208.0 9244.0 - Work_accident 1 9250.9 9286.9 - salary 2 9303.6 9337.6 - satisfaction_level 1 10576.9 10612.9 GLM_pred = predict(GLM, newdata = TEST, type = "response") Tree_pred = predict(tree, newdata = TEST, type = "prob") RF_pred = predict(rf.fit, newdata = TEST, type = "prob") library(pROC) GLM_ROC = roc(TEST$left, GLM_pred) Tree_ROC = roc(TEST$left, Tree_pred[, 2]) RF_ROC = roc(TEST$left, RF_pred[, 2]) ROC_DF = data.frame( SEN = c(GLM_ROC$sensitivities, Tree_ROC$sensitivities, RF_ROC$sensitivities), SPE = c(GLM_ROC$specificities, Tree_ROC$specificities, RF_ROC$specificities), Model = c(rep("GLM",length(GLM_ROC$sensitivities)), rep("Tree",length(Tree_ROC$sensitivities)), rep("RF",length(RF_ROC$sensitivities))) ) ggplot(ROC_DF) + geom_line(aes(x = 1-SPE, y = SEN, col = Model),size = 1.2) + xlab("1-specificity") + ylab("sensitivity") + theme_bw() + theme(legend.position = "bottom", legend.box.background = element_rect(), legend.box.margin = ggplot2::margin(2,2,2,2), text = element_text(size = 15)) ์œ„ ROC ์ปค๋ธŒ๋ฅผ ๋ณด์‹œ๋ฉด Random Forest๊ฐ€ ๊ฐ€์žฅ ์„ฑ๋Šฅ์ด ์ข‹์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ๊ธฐ๊ณ„ํ•™์Šต์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ข…๋ฅ˜๊ฐ€ ๋งค์šฐ ๋งŽ์œผ๋ฉฐ, ๋”ฅ๋Ÿฌ๋‹๊ณผ ํ•จ๊ป˜ ์„ฑ๋Šฅ์ด ํ›Œ๋ฅญํ•˜๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๊ทธ๋ ‡๋‹ค๊ณ  ๋งŒ๋Šฅ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ ์ ํ•ฉํ•œ ๊ธฐ๊ณ„ํ•™์Šต ๋ฐฉ๋ฒ•๋ก ์ด ์žˆ๋Š” ๊ฒƒ์ด๊ณ , ๋‹จ์ˆœํ•œ ํ†ต๊ณ„๋ถ„์„์ด ๋” ํšจ๊ณผ๊ฐ€ ์ข‹์„ ๋•Œ๊ฐ€ ์žˆ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ฐ์ดํ„ฐ ๋ถ„์„์€ ์ข‹์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์“ฐ๋ฉด ํ•ด๊ฒฐ์ด ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜ ์ •๋ฆดํ•œ ํ›„, ์ ์ ˆํ•œ ๋ถ„์„๋ฐฉ๋ฒ•์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ์„ ์œ ์˜ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. A9. ๊ต์ฐจ ํƒ€๋‹น์„ฑ(Cross Validation) Cross Validation ์ด๋ฒˆ์—๋Š” Cross Validation์— ๋Œ€ํ•ด ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Cross Validation ์ด๋ž€ Train, Test set์„ ๋‚˜๋ˆ„๋Š” ๊ณผ์ •์„ ํ”ผ์ž ์กฐ๊ฐ์ฒ˜๋Ÿผ ๋‚˜๋ˆ„์–ด ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ดํ•ดํ•˜์‹œ๋ฉด ํŽธํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์„ ๋ณด์‹œ๋ฉด Data๋ฅผ 4์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆˆ ๋‹ค์Œ์— ์„œ๋กœ ๋Œ์•„๊ฐ€๋ฉด์„œ Test Set์˜ ์—ญํ• ์„ ๋Œ์•„๊ฐ€๋ฉด์„œ ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰ ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด Train set์œผ๋กœ ๋ชจํ˜•์„ ๋งŒ๋“ค๊ณ  Test set์œผ๋กœ ๊ฒ€์ฆํ•˜๋Š” ๋‹จ๊ณ„๋ฅผ ์ชผ๊ฐ  ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ์ด์œ ๋Š” ๋ชจํ˜•์˜ ํƒ€๋‹น์„ฑ(Validation) ํ™•๋ณด์— ์žˆ์Šต๋‹ˆ๋‹ค. โ€™์ด๋ ‡๊ฒŒ ๋‚˜๋ˆ„๊ณ , ์ €๋ ‡๊ฒŒ ๋‚˜๋ˆ„์–ด๋„ ๋น„์Šทํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์ด ๋ชจํ˜•์€ ํƒ€๋‹นํ•˜๋‹ค.โ€™์˜ ์ฃผ์žฅ์„ ์ž…์ฆํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ณ„์‚ฐ๋˜๋Š” ๊ฐ’์€ Cross-Validateion Error(CVE)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. V = n k 1 n M E M E = n โˆ‘ โˆˆ k ( i y i [ k ] ) library(ggplot2) library(dplyr) library(reshape) library(factoextra) library(FactoMineR) AMES = read.csv("D:\Drop box\DATA SET(Drop box)\Ames_City.csv",stringsAsFactors = FALSE) ๋‹ค์Œ์˜ ๋ฐ์ดํ„ฐ๋กœ Cross Validation Error์„ ๊ณ„์‚ฐํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์กฐ๊ฐ์€ 6์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆ„๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ 4-CV๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. K = 4 Sample = sample(rep(1:K, length = nrow(AMES))) MSE_V = c() for(i in 1:K){ TRAIN = AMES[which(Sample != i),] TEST = AMES[which(Sample == i),] NonLinear = lm(SalePrice ~ poly(Overall.Qual, 2), data = TRAIN) NonLinear_Predicted = predict(NonLinear, newdata = TEST) NonLinear_E = TEST$SalePrice - NonLinear_Predicted MSE_V[i] = sum(NonLinear_E^2)/nrow(TEST) } ๊ณ„์‚ฐ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ 4์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆˆ ๋‹ค์Œ์— for ๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ์ฐจ๋ก€๋Œ€๋กœ Modeling ๋ฐ ๊ฒ€์ฆ์„ ๋ฐ˜๋ณตํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. names(MSE_V) = 1:K MSE_V 1 2 3 4 1872540857 1798261932 2149470897 2055542227 ์œ„ ๊ฒฐ๊ด๊ฐ’์„ ํ™•์ธํ•˜๋ฉด 4๋ฒˆ ๋ชจ๋‘ ๋น„์Šทํ•˜๋ฉด์„œ๋„ ๋‹ค๋ฅธ ๊ฐ’์ด ๋‚˜์˜จ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด ๊ฐ’๋“ค์— ๋Œ€ํ•ด ํ‰๊ท ์„ ์ทจํ•ด์ฃผ๋ฉด CVE๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค SalePrice์˜ ๊ฐ’์ด ๋งค์šฐ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์ข€ ๋น„๊ตํ•˜๊ธฐ๊ฐ€ ์• ๋งคํ•˜๊ธฐ๋Š” ํ•ฉ๋‹ˆ๋‹ค.(์‚ฌ์‹ค ์ด๋Ÿฐ ๋ฌธ์ œ์ ๋“ค์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด log ๋ณ€ํ™˜, Normalization, Scaling ๋“ฑ์„ ์ง„ํ–‰ํ•ด ์ค๋‹ˆ๋‹ค. ์ €๋„ ๋น„๊ตํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— log ๋ณ€ํ™˜์„ ํ•˜๊ณ  ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. K = 4 Sample = sample(rep(1:K, length = nrow(AMES))) MSE_V = c() for(i in 1:K){ TRAIN = AMES[which(Sample != i),] TEST = AMES[which(Sample == i),] NonLinear = lm(log(SalePrice) ~ poly(Overall.Qual, 2), data = TRAIN) NonLinear_Predicted = predict(NonLinear, newdata = TEST) NonLinear_E = log(TEST$SalePrice) - NonLinear_Predicted MSE_V[i] = sum(NonLinear_E^2)/nrow(TEST) } names(MSE_V) = 1:K MSE_V 1 2 3 4 0.04370626 0.05303571 0.06134567 0.05373700 log ๋ณ€ํ™˜์„ ํ•˜๊ณ  ํšŒ๊ท€๋ถ„์„์„ ํ•˜๋‹ˆ ๊ฒฐ๊ณผ๊ฐ€ ๋น„์Šทํ•˜๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. mean(MSE_V) [1] 0.05295616 CVE๋Š” 0.053 ์ •๋„๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ฌํ™”๋ฌธ์ œ ์‚ฌ์‹ค ์œ„ ๋ฌธ์ œ๋Š” ์–ด์ฐŒ ๋ณด๋ฉด ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์ด๋ฒˆ์—๋Š” ์ข€ ๋” ๋‚œํ•ดํ•œ ์‚ฐ์ ๋„๋ฅผ ๋‚œ์ˆ˜ ์ƒ์„ฑํ•œ ๋‹ค์Œ์— Cross Validation์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. set.seed(10000) X = runif(n = 1000, min = -10, max = 10) Y = sin(X) + rnorm(n = 1000, mean = 0, sd = 0.3) ggplot(NULL) + geom_point(aes(x = X, y = Y), col = 'royalblue', alpha = 0.5) + geom_smooth(aes(x = X, y = Y), se = FALSE, col = 'red') + theme_bw() + theme(text = element_text(size = 15, face = "bold")) ๋ณด๊ธฐ๋งŒ ํ•ด๋„ ์•„์ฐ”ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฑธ ํšŒ๊ท€์„ ์œผ๋กœ ํ•œ๋ฒˆ ์ ํ•ฉํ•ด ๋ณด๋Š” ์—ฐ์Šต์„ ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ €, ์ด ํšŒ๊ท€์„ ์— ๋Œ€ํ•ด ์šฐ๋ฆฌ๋Š” ๋ช‡ ์ฐจ ํ•ญ์„ ์ ์šฉ์‹œ์ผœ์•ผ ๋ ์ง€๋„ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ํ•ญ์ฐจ๋ฅผ ๋ช‡ ์ฐจ ํ•ญ์œผ๋กœ ์ฃผ์–ด์•ผ ํ• ์ง€๋„ ์‹คํ—˜์„ ํ†ตํ•ด ํ™•์ธํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ for ๋ฌธ์˜ parameter๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Replication : R(์‹คํ—˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜) Degree : D(๋น„์„ ํ˜• ํšŒ๊ท€์˜ ํ•ญ์ฐจ) Kfold : K(K-CV) ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์ง€๋งŒ ์–ด์ฐจํ”ผ ํ•ด์•ผ ๋˜๋Š” ๊ฒƒ์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ํ•ต์‹ฌ ์•Œ๋งน์ด๋Š” ์–ด์ฐจํ”ผ ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•˜๊ณ  MSE๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ ๋งŒ๋“ค์—ˆ๋˜ ์ฝ”๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์œ—๋‹จ์—๋Š” Replication, ์•„๋žซ๋‹จ์—๋Š” Degree์— ํ•ด๋‹น๋˜๋Š” for ๋ฌธ์„ ์ถ”๊ฐ€์‹œ์ผœ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ, for ๋ฌธ์—์„œ ์‚ฐ์ถœ๋˜๋Š” ๊ฐ’๋“ค์„ ์ €์žฅํ•ด์•ผ ๋  ๋ฐ์ดํ„ฐ ๊ณต๊ฐ„๋“ค์€ ํ—ท๊ฐˆ๋ฆด ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๋Œ€ํ•œ ์ฃผ์„์„ ๋‹ฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. set.seed(12123) R = 10 D = 20 K = 5 d = 5 MSE_R = list() VAR_R = list() for(r in 1:R){ Sample = sample(rep(1:K, length = length(X))) # Cross Validatiton ๊ฐ’ ์ €์žฅ ๊ณต๊ฐ„ MSE_CV = list() VAR_CV = list() for(k in 1:K){ # Degree์— ๋”ฐ๋ฅธ ๊ฐ’ ์ €์žฅ ๊ณต๊ฐ„ MSE_Degree = c() VAR_Degree = c() MSE_Degree[1] = NA VAR_Degree[1] = NA TRAIN_X = X[which(Sample != k)] TRAIN_Y = Y[which(Sample != k)] TRAIN_DF = data.frame( Y = TRAIN_Y, X = TRAIN_X ) TEST_X = X[which(Sample == k)] TEST_Y = Y[which(Sample == k)] TEST_DF = data.frame( Y = TEST_Y, X = TEST_X ) for(d in 2:D){ NonLinear = lm(Y ~ poly(X, d), data = TRAIN_DF) NonLinear_Predicted = predict(NonLinear, newdata = TEST_DF) NonLinear_E = TEST_Y - NonLinear_Predicted # Degree์— ๋”ฐ๋ฅธ MSE, VAR ๊ฐ’ ์ €์žฅ MSE_Degree[d] = sum(NonLinear_E^2)/nrow(TEST) VAR_Degree[d] = var(NonLinear_Predicted) } # CV์— ๋”ฐ๋ฅธ MSE, VAR ๊ฐ’ ์ €์žฅ MSE_CV[[k]] = MSE_Degree VAR_CV[[k]] = VAR_Degree } ### CV DATAFRAME CVE_DF = data.frame( DEGREE = 1:D ) for(i in 1:k){ CVE_DF = cbind(CVE_DF, MSE_CV[[i]]) } ### VAR DATAFRAME VAR_DF = data.frame( DEGREE = 1:D ) for(i in 1:k){ VAR_DF = cbind(VAR_DF, VAR_CV[[i]]) } ### CVE ๊ณ„์‚ฐ CVE = rowMeans(CVE_DF[,2:(k+1)],na.rm = TRUE) VAR_E = rowMeans(VAR_DF[,2:(k+1)],na.rm = TRUE) MSE_R[[r]] = CVE VAR_R[[r]] = VAR_E } ### ์ตœ์ข… ์ €์žฅ CVE_R = data.frame( DEGREE = 1:D ) for(i in 1:R){ CVE_R = cbind(CVE_R, MSE_R[[i]]) colnames(CVE_R)[i+1] = paste0("R",i) } ### VAR DATAFRAME VAR_RDF = data.frame( DEGREE = 1:D ) for(i in 1:R){ VAR_RDF = cbind(VAR_RDF, VAR_R[[i]]) colnames(VAR_RDF)[i+1] = paste0("R",i) } Replication ๋ฐ Degree์— ๋”ฐ๋ฅธ MSE ๊ฐ’ Replication ๋ฐ Degree์— ๋”ฐ๋ฅธ VAR ๊ฐ’ ๋ฐ˜๋ณต ํšŸ์ˆ˜, Degree, K-CV๋ฅผ ๋ชจ๋‘ ๊ณ ๋ คํ•œ ์‹คํ—˜ ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. CVE_R %>% melt(id.vars = c("DEGREE")) %>% na.omit() %>% ggplot() + geom_point(aes(x = DEGREE, y = value, col = variable)) + geom_line(aes(x = DEGREE, y = value, col = variable)) + labs(col = "Replications") + ylab("MSE(CV)") + theme_bw() + theme(text = element_text(size = 15, face = "bold"), legend.position = "top") MSE์˜ ๊ฒฝ์šฐ 10์ฐจ ํ•ญ ์ดํ›„๋ถ€ํ„ฐ๋Š” ํฌ๊ฒŒ ๋ณ€๋™์ด ์—†๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. VAR_RDF %>% melt(id.vars = c("DEGREE")) %>% na.omit() %>% ggplot() + geom_point(aes(x = DEGREE, y = value, col = variable)) + geom_line(aes(x = DEGREE, y = value, col = variable)) + labs(col = "Replications") + ylab("VAR(CV)") + theme_bw() + theme(text = element_text(size = 15, face = "bold"), legend.position = "top") Bias-Variance trade-off์˜ ๊ด€๊ณ„์— ๋”ฐ๋ผ์„œ Var๋„ ๋งค์šฐ ํญ๋“ฑํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋‘ ๊ทธ๋ž˜ํ”„๋ฅผ ๋™์‹œ์— ๋น„๊ตํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. rbind( CVE_R %>% melt(id.vars = c("DEGREE")) %>% mutate(G = "MSE") %>% na.omit(), VAR_RDF %>% melt(id.vars = c("DEGREE")) %>% mutate(G = "VAR") %>% na.omit() ) %>% group_by(DEGREE, G) %>% summarise(value = mean(value)) %>% ggplot() + geom_point(aes(x = DEGREE, y = value, col = G),size = 5) + geom_line(aes(x = DEGREE, y = value, col = G),size = 1.2) + labs(col = "Value") + ylab("MSE & VAR") + theme_bw() + theme(text = element_text(size = 15, face = "bold"), legend.position = "top") ChB6. Case Study ์ด๋ฒˆ ์žฅ์—์„œ๋Š” Case Study๋ฅผ ํ†ตํ•ด ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ค ๊ด€์ ์œผ๋กœ ๋ถ„์„ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A1. Case Study(EDA ํŽธ 1) Case Study 1 1. ํƒ์ƒ‰์  ์ž๋ฃŒ๋ถ„์„ ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ค‘๊ณ  ์ž๋™์ฐจ ๊ฐ€๊ฒฉ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด EDA(ํƒ์ƒ‰์  ์ž๋ฃŒ๋ถ„์„)๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํƒ์ƒ‰์  ์ž๋ฃŒ๋ถ„์„(EDA, Exploratory Data Analysis)์€ ๋ฐ์ดํ„ฐ ๋ถ„์„์— ์žˆ์–ด์„œ ๋งค์šฐ ์ค‘์š”ํ•œ step์ž…๋‹ˆ๋‹ค. ์ ˆ์ฐจ๋Š” ๋ณธ ๋ถ„์„์— ์ง„ํ–‰๋˜๊ธฐ์— ์•ž์„œ ์–ด๋–ป๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•  ๊ฑด์ง€ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‚ฌํšŒ์—์„œ ๊ฐ€์žฅ ํ†ต๊ณ„๋ถ„์„์„ ๋ชปํ•˜๋Š” ์œ ํ˜•์€ ํƒ์ƒ‰์  ๋ถ„์„ ์ ˆ์ฐจ๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋ชจ๋ธ๋ง๋ถ€ํ„ฐ ๋„์ „ํ•˜๋ ค๋Š” ์‚ฌ๋žŒ๋“ค์ž…๋‹ˆ๋‹ค. ์ด ๊ธ€์„ ์ฝ์œผ์‹œ๋Š” ๋ถ„๋“ค์€ ๊ทธ๋Ÿฐ ์‹ค์ˆ˜๋ฅผ ์•ˆ ํ•˜์‹œ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ํƒ์ƒ‰์  ์ž๋ฃŒ๋ถ„์„์€ ์ •๋‹ต์ด ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ์ € ์งง์€ ์‹œ๊ฐ„์„ ํˆฌ์žํ•ด ์ตœ๋Œ€ํ•œ์˜ ์ •๋ณด๋ฅผ ๋ฝ‘์•„, ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ดํ•ด๋ฅผ ํ•˜๋Š” ๋‹จ๊ณ„๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•๋“ค์ด ์กด์žฌํ•˜์ง€๋งŒ, ์ œ๊ฐ€ ์ฃผ๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ โ€™์‹œ๊ฐํ™”โ€™์ž…๋‹ˆ๋‹ค. ์‹œ๊ฐํ™”๋งŒํผ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”๋กœ ์ดํ•ดํ•˜๊ธฐ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์—†์Šต๋‹ˆ๋‹ค. ์‹œ๊ฐํ™”๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ํ•„์š”์— ๋”ฐ๋ผ ๊ฐ„๋‹จํ•˜๊ฒŒ Linear Model๋“ค ๋˜ํ•œ ํ™œ์šฉ์„ ํ•ฉ๋‹ˆ๋‹ค. Linear Model์ด๋ผ ํ•˜๋ฉด t-test, ๋ถ„์‚ฐ๋ถ„์„, ํšŒ๊ท€๋ถ„์„ ๋“ฑ์ด ํ•ด๋‹น์ด ๋ฉ๋‹ˆ๋‹ค. 2. ๋ฐ์ดํ„ฐ ๋ฐ ํŒจํ‚ค์ง€ ๋กœ๋”ฉ ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋งํฌ: https://www.kaggle.com/aryamonani/car-price-predictions library(ggplot2) library(dplyr) library(reshape) CAR = read.csv("D:\Drop box\DATA SET(Drop box)\car-price-predictions\1.04. Real-life example.csv") colnames(CAR)[1] = "Brand" str(CAR) 'data.frame': 4345 obs. of 9 variables: $ Brand : Factor w/ 7 levels "Audi","BMW","Mercedes-Benz",..: 2 3 3 1 6 3 2 1 5 7 ... $ Price : num 4200 7900 13300 23000 18300 ... $ Body : Factor w/ 6 levels "crossover","hatch",..: 4 6 4 1 1 1 4 5 5 3 ... $ Mileage : int 277 427 358 240 120 0 438 200 193 212 ... $ EngineV : num 2 2.9 5 4.2 2 5.5 2 2.7 1.5 1.8 ... $ Engine.Type : Factor w/ 4 levels "Diesel","Gas",..: 4 1 2 4 4 4 2 1 1 2 ... $ Registration: Factor w/ 2 levels "no","yes": 2 2 2 2 2 2 2 2 2 1 ... $ Year : int 1991 1999 2003 2007 2011 2016 1997 2006 2012 1999 ... $ Model : Factor w/ 312 levels "1 Series","100",..: 19 267 239 225 228 146 19 67 190 150 ... colSums(is.na(CAR)) Brand Price Body Mileage EngineV Engine.Type 0 172 0 0 150 0 Registration Year Model 0 0 0 CAR = na.omit(CAR) ๋ฐ์ดํ„ฐ๋Š” ๊ฝค ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์€ ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ์„ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜์ธ Price๋ฅผ ์ค‘์‹ฌ์œผ๋กœ EDA๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฒฐ์ธก์น˜๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ์ธก์น˜๊ฐ€ ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋Š” ์‚ญ์ œ๋ฅผ ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. 3. ๋ธŒ๋žœ๋“œ์— ๋”ฐ๋ฅธ ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ ์‹œ๊ฐํ™” ๋จผ์ € ๊ฐ€์žฅ ๊ด€์‹ฌ ์žˆ๋Š” ๋ธŒ๋žœ๋“œ์— ๋”ฐ๋ผ ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ์˜ ๋ถ„ํฌ๊ฐ€ ๋‹ค๋ฅธ์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ธŒ๋žœ๋“œ๋Š” ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ, ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ์€ ์—ฐ์†ํ˜• ์ž๋ฃŒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ด์— ์ ํ•ฉํ•œ ์‹œ๊ฐํ™” ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ, ๋ฐ•์Šค ํ”Œ๋กฏ์€ ํƒ์ƒ‰์  ๋ถ„์„์—์„œ ๋งค์šฐ ์œ ์šฉํ•œ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค. ggplot(CAR) + geom_boxplot(aes(x = Brand, y = Price), outlier.colour = "red") + theme_bw() + theme(text = element_text(size = 15, face = "bold"), axis.text.x = element_text(angle = 90)) ๋นจ๊ฐ„ ์ ์€ outlier(์ด์ƒ์ )์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๋ฒ”์œ„์—์„œ ๋ฒ—์–ด๋‚˜๋Š” ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ๋“ค์ž…๋‹ˆ๋‹ค ๋ฉ”๋ฅด์„ธ๋ฐ์Šค ๋ฒค์ธ ์˜ ๊ฒฝ์šฐ๊ฐ€ ์ƒ๋‹นํžˆ ์ด์ƒ์ ์ด ๋งŽ์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ggplot(CAR) + geom_boxplot(aes(x = Brand, y = Price), outlier.colour = "red") + theme_bw() + theme(text = element_text(size = 15, face = "bold"), axis.text.x = element_text(angle = 90)) + facet_wrap(~Body) ์ด๋ฒˆ์—๋Š” ์ฐจ๋Ÿ‰์˜ Body์— ๋”ฐ๋ผ ์–ด๋–ค์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค Body์˜ ์ฐจ๋Ÿ‰๋“ค์ด ๋งŽ์ด ๊ณ ๊ฐ€์ธ์ง€ ์‚ดํŽด๋ณด๋‹ˆ ์ฃผ๋กœ crossover, sedan, van, other ๋“ฑ์— ์œ„์น˜ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ, ์ „์ฒด์ ์œผ๋กœ ๊ฐ€๊ฒฉ์ด ๋†’์€ Body๋Š” crossover์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ Body๋“ค์€ ์ด์ƒ์ ์ด ๋†’์€ ๊ฑด๋ฐ, crossover์€ ๊ทธ๋ƒฅ ์ƒ์ž ์ž์ฒด๊ฐ€ ๋งค์šฐ ํฐ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•˜๋‚˜ ํ™•์ธํ•  ์ˆ˜๋Š” ์žˆ๋Š” ๊ฒƒ์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ์— ์ด์ƒ์ ์ด ๋งค์šฐ ๋งŽ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ƒ์ ์ด ๋งŽ์„ ๊ฒฝ์šฐ, ํ†ต๊ณ„์ ์ธ ๋ถ„์„์— ๋งŽ์€ error๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋‹ค์Œ ๋‹จ๊ณ„์—์„œ๋Š” Price์— ๋Œ€ํ•œ ๋ถ„ํฌ๋ฅผ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 4. ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ ๋ถ„ํฌ ํƒ์ƒ‰ ggplot(CAR) + geom_histogram(aes(x = Price, y = .. density..), fill = 'royalblue',col ='black',alpha = 0.4) + geom_density(aes(x = Price),col = 'red', size = 1.2) + xlab("Price") + ylab("Density") + scale_x_continuous(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) + theme_bw() + theme(text = element_text(size = 15, face = "bold")) ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ์— ๋Œ€ํ•œ ๋ถ„ํฌ๋ฅผ ์‚ดํŽด๋ณด์‹œ๋ฉด, ๋งค์šฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ผฌ๋ฆฌ๊ฐ€ ์น˜์šฐ์นœ ๋ถ„ํฌ์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐ์ดํ„ฐ๋Š” ์ •๊ทœ๋ถ„ํฌ์™€ ๋งค์šฐ ๊ฑฐ๋ฆฌ๊ฐ€ ๋จผ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๊ทœ๋ถ„ํฌ์ผ ํ•„์š”๋Š” ์—†์ง€๋งŒ, ์ ์–ด๋„ ๋ถ„์„ํ•˜๊ณ ์ž ํ•˜๋Š” Target ๋ณ€์ˆ˜๋Š” ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ผ์ฃผ๋Š” ๊ฒŒ ๋ถ„์„์˜ ํ”ผ๋กœ๋„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. qqnorm(CAR$Price) qqplot์„ ๊ทธ๋ ค๋ณด๋ฉด ์—ญ์‹œ๋‚˜, ์ •๊ทœ๋ถ„ํฌ์™€ ๊ฑฐ๋ฆฌ๊ฐ€ ๋ฉ€๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ ๋ถ„ํฌ๊ฐ€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ผฌ๋ฆฌ๊ฐ€ ์น˜์šฐ์นœ ๊ฒฝ์šฐ๋Š” ์‚ฌ์‹ค ๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. log ๋ณ€ํ™˜์„ ์ฃผ๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋งค์šฐ ์น˜์šฐ์นœ ๋ถ„ํฌ๋ฅผ ์ค‘์•™์œผ๋กœ ์ง‘๊ฒฐ์‹œํ‚ฌ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ## Log Transform ggplot(CAR) + geom_histogram(aes(x = log(Price), y = .. density..), fill = 'royalblue',col ='black',alpha = 0.4, binwidth = 0.1) + geom_density(aes(x = log(Price)),col = 'red', size = 1.2) + xlab("Price") + ylab("Density") + scale_x_continuous(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) + theme_bw() + theme(text = element_text(size = 15, face = "bold")) qqnorm(log(CAR$Price)) ๋กœ๊ทธ ๋ณ€ํ™˜ ํ•œ๋ฒˆ ํ–ˆ์„ ๋ฟ์ธ๋ฐ, ๋งค์šฐ ์ •๊ทœ๋ถ„ํฌ์™€ ์œ ์‚ฌํ•ด์กŒ์Šต๋‹ˆ๋‹ค. log ๋ณ€ํ™˜๋œ ๊ฐ’์ด๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ๋ถ„์„์„ ํ•  ๋•Œ ์ด log ๋ณ€ํ™˜ ๋œ ๊ฐ’์„ ์ค‘์‹ฌ์œผ๋กœ ๋ถ„์„์„ ํ•˜๋Š” ๊ฒƒ์ด ๋” ํ†ต๊ณ„์ ์œผ๋กœ ์œ ์˜ํ•œ ๊ฒฐ๊ณผ๋“ค์„ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค. ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง„๋‹ค๊ณ  ํ•˜๋Š” ์–˜๊ธฐ๋Š”, ํ†ต๊ณ„๋ถ„์„์—๋Š” ์ •ํ•ด์ง„ ์ •๋‹ต์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. โ€œ์ด ๋ฐฉ๋ฒ•์ด ์ œ์ผ ์ข‹๋‹คโ€๋ผ๋Š” ๊ฒƒ์€ ํ†ต๊ณ„๋ถ„์„์—์„œ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰ ์ƒํ™ฉ๋งˆ๋‹ค ์ ํ•ฉํ•œ ๋ถ„์„๋ฐฉ๋ฒ•์ด ๋ชจ๋‘ ์กด์žฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 5. Brand์™€ Body์˜ Price์— ๋Œ€ํ•œ Interaction effect ๋ถ„์„ ๊ตํ˜ธ ํšจ๊ณผ๋Š” ๋‘ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜๋“ค์˜ ์ˆ˜์ค€(levels) ์กฐํ•ฉ์— ๋”ฐ๋ผ Target ๋ณ€์ˆ˜์˜ ๊ธฐ๋Œ“๊ฐ’์ด ๋‹ฌ๋ผ์ง€๋Š”๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ํšจ๊ณผ์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ ํ•˜๋‚˜์˜ ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜ ์ˆ˜์ค€(levels)์— ๋”ฐ๋ผ Target ๋ณ€์ˆ˜๋“ค์˜ ๊ธฐ๋Œ“๊ฐ’ ์ฐจ์ด๋ฅผ ๋ณด๋Š”๋ฐ, ์ด๋Ÿด ๋•Œ๋Š” ์ฃผํšจ๊ณผ(Main Effect)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ตํ˜ธ ํšจ๊ณผ๋ฅผ ๋ณด๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋ณ€์ˆ˜์˜ ์กฐํ•ฉ์— ๋”ฐ๋ผ ๊ธฐ๋Œ“๊ฐ’(ํ‰๊ท )์˜ ๋ณ€ํ™”๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. # Log CAR %>% group_by(Brand, Body) %>% summarise(Mean = mean(log(Price))) %>% ggplot() + geom_point(aes(x = Brand, y = Mean, col = Body)) + geom_line(aes(x = Brand, y = Mean, col = Body, group = Body)) + theme_bw() + theme(text = element_text(size = 15, face = "bold"), axis.text.x = element_text(angle = 90)) ๊ตํ˜ธ ํšจ๊ณผ๋ฅผ ํ™•์ธํ•˜๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์„ ๋“ค์ด ๋งŒ๋‚˜๋Š”๊ฐ€ ์•ˆ ๋งŒ๋‚˜๋Š” ๊ฐ€์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์„ ๋“ค์ด ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ํ‰ํ–‰ ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•˜๊ณ  ์žˆ์œผ๋ฉด ๋”ฑํžˆ ๊ตํ˜ธ ํšจ๊ณผ๊ฐ€ ์—†๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด crossover, other์— ๋Œ€ํ•œ ์ˆ˜์ค€์€ Brand ๋ณ„๋กœ ๋น„์Šทํ•œ ํŒจํ„ด์„ ๋ณด์ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, hatch๋ฅผ ์‚ดํŽด๋ณด์‹œ๋ฉด ๋ฒค์ธ ์™€ ๋ฏธ์ธ  ๋น„์”จ์—์„œ ์•„์ฃผ ๊ผฌ๋ผ๋ฐ•๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ผฌ๋ผ๋ฐ•์œผ๋ฉด์„œ ๋‹ค๋ฅธ ์„ ๋“ค๊ณผ ๊ฒน์น˜๊ฒŒ ๋˜์ฃ . ์ด๋Ÿฐ ์ƒํ™ฉ์ด ๊ตํ˜ธ ํšจ๊ณผ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ํ•ฉ๋ฆฌ์  ์˜์‹ฌ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค. ๊ตํ˜ธ ํšจ๊ณผ๊ฐ€ ์ •๋ง ์žˆ๋Š”์ง€ ํ†ต๊ณ„์ ์œผ๋กœ ๊ฒ€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ถ„์‚ฐ๋ถ„์„์„ ํ™œ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ## Anova ANOVA = aov(log(Price) ~ Brand + Body + Brand:Body, data = CAR) summary(ANOVA) Df Sum Sq Mean Sq F value Pr(>F) Brand 6 331.4 55.23 93.232 <2e-16 *** Body 5 715.5 143.11 241.560 <2e-16 *** Brand:Body 29 96.7 3.33 5.628 <2e-16 *** Residuals 3984 2360.2 0.59 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Two Way ANOVA๋ฅผ ์ง„ํ–‰ํ•ด ๋ณธ ๊ฒฐ๊ณผ, ์—ญ์‹œ๋‚˜ ์ฃผํšจ๊ณผ์™€ ๊ตํ˜ธ ํšจ๊ณผ๊ฐ€ ๋ชจ๋‘ ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„ ํ‘œ์— ๋Œ€ํ•ด์„œ๋Š” ๋”ฐ๋กœ ์„ค๋ช…์„ ์ ์ง€๋Š” ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ถ„์‚ฐ๋ถ„์„์˜ ๊ฒฐ๊ณผ๊ฐ€ ์œ ์˜ํ•  ๊ฒฝ์šฐ ์‚ฌํ›„ ๊ฒ€์ •์„ ๊ฐ™์ด ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๊ตญ๋ฃฐ์ด์ง€๋งŒ, ์ง€๊ธˆ์€ ๋ฒ”์ฃผํ˜• ์ž๋ฃŒ๋“ค์˜ ์ˆ˜์ค€์ด ๋„ˆ๋ฌด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—, ํ•ด๋ดค์ž ๋จธ๋ฆฌ๋งŒ ์•„ํŒŒ์ง‘๋‹ˆ๋‹ค. ํšจ์œจ์ด ์—†์œผ๋‹ˆ ๋„˜์–ด๊ฐ€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. A2. Case Study(EDA ํŽธ 2) 6. ์ž๋™์ฐจ ๋ชจ๋ธ ๋ฐ ์ฃผํ–‰๊ฑฐ๋ฆฌ์— ๋”ฐ๋ฅธ Price ๋ถ„์„ 300์—ฌ ๊ฐœ์˜ ์ž๋™์ฐจ ๋ชจ๋ธ์— ๋Œ€ํ•ด 4025๊ฐœ์˜ ์ค‘๊ณ  ์ž๋™์ฐจ ๊ฐ€๊ฒฉ์ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ด€๊ณ„๋กœ ์šฐ๋ฆฌ๋Š” ์ž๋™์ฐจ ๋ชจ๋ธ์— ๋”ฐ๋ผ ๊ฐ’์„ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์š”์•ฝํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. CAR %>% group_by(Model, Brand) %>% summarise(Mean_Price = mean(log(Price)), Mean_Mile = mean(Mileage)) %>% ggplot() + geom_text(aes(x = Mean_Mile, y = Mean_Price, label = Model, col = Brand)) + theme_bw() + theme(text = element_text(size = 15, face = "bold"), legend.position = c(0.8,0.8)) ggplot(CAR) + geom_point(aes(x = Mileage, y = log(Price))) + geom_smooth(aes(x = Mileage, y = log(Price))) + theme_bw() + theme(text = element_text(size = 15, face = "bold")) ๋ณด์‹œ๋ฉด Mileage(์ฃผํ–‰๊ฑฐ๋ฆฌ)์— ๋”ฐ๋ผ ๊ฐ€๊ฒฉ์ด ํ•˜๋ฝํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์šฐ๋ฆฌ๋Š” ํšŒ๊ท€๋ถ„์„์„ ์ ์šฉํ•ด ๋ณผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ๋งŒ์•ฝ log ๋ณ€ํ™˜์„ ํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋กœ ์‚ฐ์ ๋„๋ฅผ ๊ทธ๋ ค๋ณผ ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. ggplot(CAR) + geom_point(aes(x = Mileage, y = Price)) + geom_smooth(aes(x = Mileage, y = Price)) + theme_bw() + theme(text = element_text(size = 15, face = "bold")) ๋งŒ์•ฝ ๋กœ๊ทธ ๋ณ€ํ™˜์„ ์•ˆ ํ–ˆ๋‹ค๋ฉด, ์œ ์˜ํ•œ ๊ด€๊ณ„๋ฅผ ์ฐพ๊ธฐ ํž˜๋“ค ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•˜์„ ๊ฒ๋‹ˆ๋‹ค. o ( r c) ฮฒ + 1 i e g l g ( r c) ฮฒ + 1 i e g + ๊ทธ๋ž˜ํ”„ ์ƒ์—์„œ ๋น„์„ ํ˜• ๊ด€๊ณ„๋„ ๋ณด์ด๋ฏ€๋กœ ์ผ๋ฐ˜ ์„ ํ˜•ํšŒ๊ท€์™€ ๋‹คํ•ญํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. # Simple Linear Reg = lm(log(Price) ~ Mileage, data = CAR) summary(Reg) Call: lm(formula = log(Price) ~ Mileage, data = CAR) Residuals: Min 1Q Median 3Q Max -3.8327 -0.4511 0.0226 0.4749 6.0869 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 10.2830105 0.0221983 463.23 <2e-16 *** Mileage -0.0053392 0.0001147 -46.54 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 0.7524 on 4023 degrees of freedom Multiple R-squared: 0.35, Adjusted R-squared: 0.3498 F-statistic: 2166 on 1 and 4023 DF, p-value: < 2.2e-16 2 34.98 ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ๋ณ€์ˆ˜ ํ•˜๋‚˜๋งŒ์„ ํˆฌ์ž…ํ–ˆ๋Š”๋ฐ ์ด ์ •๋„๋ฉด ๋‚˜์œ ๊ฒƒ์€ ์•„๋‹ˆ๋ผ๊ณ  ๋ด…๋‹ˆ๋‹ค. # Polynomial Poly_Reg = lm(log(Price) ~ poly(Mileage, 2), data = CAR) summary(Poly_Reg) Call: lm(formula = log(Price) ~ poly(Mileage, 2), data = CAR) Residuals: Min 1Q Median 3Q Max -4.0717 -0.4301 0.0498 0.4844 2.6382 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 9.40967 0.01146 821.01 <2e-16 *** poly(Mileage, 2) 1 -35.01901 0.72713 -48.16 <2e-16 *** poly(Mileage, 2) 2 12.29046 0.72713 16.90 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 0.7271 on 4022 degrees of freedom Multiple R-squared: 0.3931, Adjusted R-squared: 0.3928 F-statistic: 1303 on 2 and 4022 DF, p-value: < 2.2e-16 2 39.28 ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์—ญ์‹œ ์„ ํ˜•ํšŒ๊ท€๋ณด๋‹ค๋Š” ๋” ์ ํ•ฉ์„ ์ž˜ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹คํ•ญํšŒ๊ท€ ํ•ญ์ฐจ๋ฅผ ๋” ๋†’์—ฌ๋ณผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๊ทธ๋ž˜ํ”„ ์ƒ์—์„œ 2์ฐจํ•จ์ˆ˜ ๊ผด์„ ๋ณด์˜€์œผ๋‹ˆ ๊ตณ์ด ๋” ์˜ฌ๋ ค์„œ ๋ถ„์„ํ•ด ๋ณผ ํ•„์š”๋Š” ์—†์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์›ํ•˜์‹œ๋Š” ๋ถ„์€ ํ•œ๋ฒˆ ํ•ญ์ฐจ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ๊ฐ’์ด ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ํ™•์ธ์„ ํ•ด๋ณด์…”๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. A3. Case Study(EDA ํŽธ 3) 7. ์—”์ง„๊ณผ ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ ๋ถ„์„ ์ด๋ฒˆ์—๋Š” ์—”์ง„ ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•ด์„œ ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ์„ ๋ถ„์„ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ggplot(CAR) + geom_point(aes(x = EngineV, y = log(Price))) + theme_bw() + theme(text = element_text(size = 15, face = "bold")) EngineV์™€ Price ๊ฐ„์˜ ์‚ฐ์ ๋„์ธ๋ฐ, ๋งค์šฐ ๊ดด์ƒํ•˜๊ฒŒ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋‚˜๋ฉด ํ•œ ๊ฐ€์ง€ ํŒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. x ์ถ•์˜ ๋ฒ”์œ„๋ฅผ ์กฐ์ ˆํ•ด์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ๋‹ค์‹œ ๊ทธ๋ ค๋ณด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹คใ…ฃ. ๊ทธ ์ด์œ ๋Š” x ๋ฒ”์œ„๋Š” ๋งค์šฐ ๋„“์€ ๊ฒƒ์— ๋น„ํ•ด ๋Œ€๋ถ€๋ถ„์˜ ๋ฐ์ดํ„ฐ๋Š” ํ•œ์ชฝ์— ๋ชฐ๋ ค์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ggplot(CAR) + geom_point(aes(x = EngineV, y = log(Price))) + theme_bw() + theme(text = element_text(size = 15, face = "bold")) + scale_x_continuous(limits = c(0,10)) ๊ทธ๋ž˜ํ”„๋ฅผ ์ž˜๋ผ์„œ ๋ณด๋‹ˆ ๋‹ค๋ฅธ ๊ด€๊ณ„๊ฐ€ ๋ณด์ž…๋‹ˆ๋‹ค. ํƒ์ƒ‰์  ์ž๋ฃŒ๋ถ„์„์˜ ํ•ต์‹ฌ์€ ์ด๋Ÿฐ ๋ถ€๋ถ„์— ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ชผ๊ฐœ์„œ๋„ ๋ณด๊ณ  ํ•˜๋‚˜ํ•˜๋‚˜ ์‚ดํŽด๋ณด๋ฉฐ, ์–ด๋–ค ํ•จ์ •์ด ์ˆจ์–ด์žˆ๋Š”์ง€ ๋ฐœ๊ฒฌํ•˜๋Š” ๊ฒƒ, ์ด๊ฒŒ ๋ฐ”๋กœ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์ •ํ™•ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค๋ ฅ์˜ ์ง€๋ฆ„๊ธธ์ด ๋ฉ๋‹ˆ๋‹ค. ggplot(CAR) + geom_boxplot(aes(x = Engine.Type, y = log(Price), fill = Engine.Type), alpha = 0.4, outlier.colour = 'red') + geom_jitter(aes(x = Engine.Type, y = log(Price), col = Engine.Type), alpha = 0.4) + theme_bw() + theme(text = element_text(size = 15, face = "bold")) ๋‹ค์Œ์œผ๋กœ๋Š” ์—”์ง„ ํƒ€์ž…์— ๋Œ€ํ•ด ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ธ์Šต๋‹ˆ๋‹ค. ์—ญ์‹œ ์—”์ง„ ํƒ€์ž…์— ๋”ฐ๋ผ์„œ๋„ ๊ฐ€๊ฒฉ์ด ๋‹ค๋ฅธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜์˜ ์ˆ˜์ค€์— ๋”ฐ๋ฅธ ๊ธฐ๋Œ“๊ฐ’(ํ‰๊ท )์˜ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ถ„์‚ฐ๋ถ„์„์„ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ANOVA = aov(log(Price) ~ Engine.Type, data = CAR) summary(ANOVA) Df Sum Sq Mean Sq F value Pr(>F) Engine.Type 3 44 14.78 17.18 4.41e-11 *** Residuals 4021 3460 0.86 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ๋ถ„์‚ฐ๋ถ„์„ ๊ฒฐ๊ณผ ์ฐจ์ด๊ฐ€ ๋งค์šฐ ์œ ์˜ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์ˆ˜์ค€ ์ˆ˜๊ฐ€ 4๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌํ›„ ๊ฒ€์ •์„ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. TUKEY = TukeyHSD(ANOVA) TUKEY Tukey multiple comparisons of means 95% family-wise confidence level Fit: aov(formula = log(Price) ~ Engine.Type, data = CAR) $Engine.Type diff lwr upr p adj Gas-Diesel -0.24113612 -0.35376896 -0.12850328 0.0000003 Other-Diesel -0.25238630 -0.49043654 -0.01433606 0.0327292 Petrol-Diesel 0.05838048 -0.02483674 0.14159771 0.2719751 Other-Gas -0.01125018 -0.26273875 0.24023839 0.9994567 Petrol-Gas 0.29951661 0.18331123 0.41572198 0.0000000 Petrol-Other 0.31076679 0.07100555 0.55052802 0.0048353 plot(TUKEY) ์‚ฌํ›„ ๊ฒ€์ • ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ข€ ์ž์„ธํžˆ ์ •๋ฆฌํ•ด ๋ณผ ํ•„์š”๊ฐ€ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ ์ฐจ์ด์— ๋Œ€ํ•œ 95% ์‹ ๋ขฐ๊ตฌ๊ฐ„์ด 0์„ ํฌํ•จํ•˜๋Š”์ง€ ์•ˆ ํ•˜๋Š”์ง€๋งŒ ํ™•์ธํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ˜น์€ p-value๋ฅผ ํ†ตํ•ด ํ™•์ธํ•˜์…”๋„ ๋ฌธ์ œ๋Š” ์—†์Šต๋‹ˆ๋‹ค. a โˆ’ i s l 0 t e โˆ’ i s l 0 e r l D e e = O ์ด๋ฒˆ์—๋Š” ์—”์ง„๊ณผ ์—”์ง„ ํƒ€์ž…์„ ํฌํ•จํ•˜์—ฌ ๊ฐ€๋ณ€ ์ˆ˜ ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์—”์ง„๊ณผ ์—”์ง„ ํƒ€์ž…์„ ํฌํ•จํ•˜์—ฌ ๊ฐ€๋ณ€ ์ˆ˜ ํšŒ๊ท€๋ถ„์„์„ ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Reg2 = lm(log(Price) ~ Engine.Type + EngineV, data = CAR) summary(Reg2) Call: lm(formula = log(Price) ~ Engine.Type + EngineV, data = CAR) Residuals: Min 1Q Median 3Q Max -3.08079 -0.55506 -0.05422 0.57677 3.10458 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 9.408607 0.022757 413.431 < 2e-16 *** Engine.TypeGas -0.243758 0.043793 -5.566 2.77e-08 *** Engine.TypeOther -0.272324 0.092793 -2.935 0.00336 ** Engine.TypePetrol 0.055358 0.032366 1.710 0.08727. EngineV 0.008598 0.002969 2.896 0.00379 ** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 0.9267 on 4020 degrees of freedom Multiple R-squared: 0.01471, Adjusted R-squared: 0.01373 F-statistic: 15 on 4 and 4020 DF, p-value: 3.54e-12 ๋งŒ์•ฝ EngineV์— ๋ฐ์ดํ„ฐ ๋ฒ”์œ„๋ฅผ ์กฐ์ •ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ํšŒ๊ท€๋ถ„์„์˜ ๊ฒฐ๊ณผ๋Š” ์ •๋ง ์ฒ˜์ฐธํ•˜๊ฒŒ ๋‚˜์˜ต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— EngineV๋ฅผ ํ™œ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•ด ๋ณด๊ณ  ๋‹ค์‹œ ๋Œ๋ ค๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. CAR2 = subset(CAR, EngineV < 10) Reg2s = lm(log(Price) ~ Engine.Type + EngineV, data = CAR2) summary(Reg2s) Call: lm(formula = log(Price) ~ Engine.Type + EngineV, data = CAR2) Residuals: Min 1Q Median 3Q Max -3.11037 -0.47377 0.01656 0.53087 2.39005 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 8.40716 0.03649 230.402 <2e-16 *** Engine.TypeGas -0.39544 0.03916 -10.098 <2e-16 *** Engine.TypeOther -0.19139 0.08363 -2.289 0.0222 * Engine.TypePetrol -0.05580 0.02898 -1.925 0.0542. EngineV 0.44104 0.01336 33.008 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 0.8222 on 4000 degrees of freedom Multiple R-squared: 0.224, Adjusted R-squared: 0.2232 F-statistic: 288.6 on 4 and 4000 DF, p-value: < 2.2e-16 2 ๊ฐ€ ๋‚˜๋ฆ„ 22% ๊นŒ์ง€ ์ƒ์Šนํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ •๋„๋ฉด ์—”์ง„ ํƒ€์ž…๋„ ํšจ๊ณผ๋ฅผ ์ค€๋‹ค๊ณ  ๋ณผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Petrol Type์— ๋Œ€ํ•œ ํšŒ๊ท€๊ณ„์ˆ˜ ๊ฒ€์ •์˜ p-value๊ฐ€ 0.0542๋กœ 0.05๋ณด๋‹ค ๋†’์ง€๋งŒ, ์•„์ฃผ ๋†’์€ ๊ฒƒ๋„ ์•„๋‹ˆ๋ฉฐ, ํƒ์ƒ‰์  ์ž๋ฃŒ๋ถ„์„์€ Rough ํ•˜๊ฒŒ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ์ด ์ฃผ์š” point์ž…๋‹ˆ๋‹ค. ์ฒ˜์Œ๋ถ€ํ„ฐ ๋„ˆ๋ฌด ์ž”ํ˜นํ•˜๊ฒŒ ๋ณผ ํ•„์š”๋Š” ๋˜ ์—†๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ์—๊ฒŒ ์ฃผ์–ด์ง„ ๊ณผ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. EngineV๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ๋˜๋Š” ๊ฒƒ์ธ๊ฐ€? Mileage๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ธ๊ฐ€? EngineV๋Š” ๋ฐ”๋กœ ์œ„์—์„œ ์‚ฌ๋ก€๋ฅผ ๋ดค์œผ๋ฉฐ, Mileage๋Š” Mileage๊ฐ€ ํฐ ๊ฒฝ์šฐ์— ํ•œ ํ•ด ์„ ํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ๋น„์„ ํ˜•์œผ๋กœ ๊บพ์—ฌ๋ฒ„๋ฆฌ๋Š” ๋ฌธ์ œ์ ์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์€ ์‚ฌ์‹ค ๋ฐ์ดํ„ฐ๋ฅผ ์˜ˆ์ธก ๋ชจ๋ธ๋ง ํ˜น์€ ๊ด€๊ณ„๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•œ ๋ชจ๋ธ๋ง์„ ์ง„ํ–‰ํ•˜๋Š”๋ฐ ๋งค์šฐ ๋ฐฉํ•ด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ’์„ ์ž๋ฅด๊ณ  ๊ฐˆ ๊ฒƒ์ธ์ง€, ์•ˆ๊ณ  ๊ฐˆ ๊ฒƒ์ธ์ง€ ์„ ํƒ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฑด ๋ฐ์ดํ„ฐ ๋ถ„์„๊ฐ€๋“ค์˜ ์ƒ๊ฐ์— ๋”ฐ๋ผ ๊ฐˆ๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์ž์‹ ์ด ๋งž๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋Œ€์‹ ์— ๊ทธ ์ด์œ ๋Š” ์ถฉ๋ถ„ํžˆ ์„ค๋ช…ํ•  ์ˆ˜๋Š” ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค("๋ถ„์„์€ ์„ค๋“์ž…๋‹ˆ๋‹ค"). ์ด๋ ‡๊ฒŒ ์ฐจ๋Ÿ‰์˜ ํŠน์ง•๋“ค์ด ์ฐจ๋Ÿ‰ ๊ฐ€๊ฒฉ์— ์–ด๋–ค ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธ์„ ํ•˜๋Š” ํƒ์ƒ‰์  ์ž๋ฃŒ๋ถ„์„์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. A4. Case Study(Modeling ํŽธ 1) Case Study 2 ์ด๋ฒˆ ์ผ€์ด์Šค์Šคํ„ฐ๋””๋Š” ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ง„ํ–‰ํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์žฅ์—์„œ์˜ ๋ชฉํ‘œ๋Š” ํ•˜๋‚˜ํ•˜๋‚˜ ๊นŠ๊ฒŒ ํ•ด์„ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํšจ์œจ์ ์œผ๋กœ ๋น ๋ฅด๊ฒŒ ๊ฒฐ๊ณผ๋ฅผ ๋ฝ‘์•„๋‚ด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃจ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ๋ฐ์ดํ„ฐ ์„ค๋ช… ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ๋งํฌ : https://www.kaggle.com/pavansubhasht/ibm-hr-analytics-attrition-dataset Kaggle์— ์žˆ๋Š” IBM_HR ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด ์ฑ…์—์„œ ๊พธ์ค€ํžˆ ์˜ˆ์‹œ๋กœ ์‚ฌ์šฉํ•œ HR_comma_sep ๋ฐ์ดํ„ฐ์™€ ์œ ์‚ฌํ•œ ์„ฑ๊ฒฉ์˜ ์ธ์‚ฌ๊ด€๋ฆฌ ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค. ์ด ์นœ์ˆ™ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ํ†ต๊ณ„๋ถ„์„์„ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 2. ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ IBM = read.csv("D:\Drop box\DATA SET(Drop box)\IBM_HR.csv") colnames(IBM)[1] = "Age" str(IBM) 'data.frame': 1470 obs. of 35 variables: $ Age : int 41 49 37 33 27 32 59 30 38 36 ... $ Attrition : Factor w/ 2 levels "No","Yes": 2 1 2 1 1 1 1 1 1 1 ... $ BusinessTravel : Factor w/ 3 levels "Non-Travel","Travel_Frequently",..: 3 2 3 2 3 2 3 3 2 3 ... $ DailyRate : int 1102 279 1373 1392 591 1005 1324 1358 216 1299 ... $ Department : Factor w/ 3 levels "Human Resources",..: 3 2 2 2 2 2 2 2 2 2 ... $ DistanceFromHome : int 1 8 2 3 2 2 3 24 23 27 ... $ Education : int 2 1 2 4 1 2 3 1 3 3 ... $ EducationField : Factor w/ 6 levels "Human Resources",..: 2 2 5 2 4 2 4 2 2 4 ... $ EmployeeCount : int 1 1 1 1 1 1 1 1 1 1 ... $ EmployeeNumber : int 1 2 4 5 7 8 10 11 12 13 ... $ EnvironmentSatisfaction : int 2 3 4 4 1 4 3 4 4 3 ... $ Gender : Factor w/ 2 levels "Female","Male": 1 2 2 1 2 2 1 2 2 2 ... $ HourlyRate : int 94 61 92 56 40 79 81 67 44 94 ... $ JobInvolvement : int 3 2 2 3 3 3 4 3 2 3 ... $ JobLevel : int 2 2 1 1 1 1 1 1 3 2 ... $ JobRole : Factor w/ 9 levels "Healthcare Representative",..: 8 7 3 7 3 3 3 3 5 1 ... $ JobSatisfaction : int 4 2 3 3 2 4 1 3 3 3 ... $ MaritalStatus : Factor w/ 3 levels "Divorced","Married",..: 3 2 3 2 2 3 2 1 3 2 ... $ MonthlyIncome : int 5993 5130 2090 2909 3468 3068 2670 2693 9526 5237 ... $ MonthlyRate : int 19479 24907 2396 23159 16632 11864 9964 13335 8787 16577 ... $ NumCompaniesWorked : int 8 1 6 1 9 0 4 1 0 6 ... $ Over18 : Factor w/ 1 level "Y": 1 1 1 1 1 1 1 1 1 1 ... $ OverTime : Factor w/ 2 levels "No","Yes": 2 1 2 2 1 1 2 1 1 1 ... $ PercentSalaryHike : int 11 23 15 11 12 13 20 22 21 13 ... $ PerformanceRating : int 3 4 3 3 3 3 4 4 4 3 ... $ RelationshipSatisfaction: int 1 4 2 3 4 3 1 2 2 2 ... $ StandardHours : int 80 80 80 80 80 80 80 80 80 80 ... $ StockOptionLevel : int 0 1 0 0 1 0 3 1 0 2 ... $ TotalWorkingYears : int 8 10 7 8 6 8 12 1 10 17 ... $ TrainingTimesLastYear : int 0 3 3 3 3 2 3 2 2 3 ... $ WorkLifeBalance : int 1 3 3 3 3 2 2 3 3 2 ... $ YearsAtCompany : int 6 10 0 8 2 7 1 1 9 7 ... $ YearsInCurrentRole : int 4 7 0 7 2 7 0 0 7 7 ... $ YearsSinceLastPromotion : int 0 1 0 3 2 3 0 0 1 7 ... $ YearsWithCurrManager : int 5 7 0 0 2 6 0 0 8 7 ... Education : 1 โ€˜Below Collegeโ€™ 2 โ€˜Collegeโ€™ 3 โ€˜Bachelorโ€™ 4 โ€˜Masterโ€™ 5 โ€˜Doctorโ€™ EnvironmentSatisfaction : 1 โ€˜Lowโ€™ 2 โ€˜Mediumโ€™ 3 โ€˜Highโ€™ 4 โ€˜Very Highโ€™ JobInvolvement : 1 โ€˜Lowโ€™ 2 โ€˜Mediumโ€™ 3 โ€˜Highโ€™ 4 โ€˜Very Highโ€™ JobSatisfaction : 1 โ€˜Lowโ€™ 2 โ€˜Mediumโ€™ 3 โ€˜Highโ€™ 4 โ€˜Very Highโ€™ PerformanceRating : 1 โ€˜Lowโ€™ 2 โ€˜Goodโ€™ 3 โ€˜Excellentโ€™ 4 โ€˜Outstandingโ€™ RelationshipSatisfactionn1 : โ€˜Lowโ€™ 2 โ€˜Mediumโ€™ 3 โ€˜Highโ€™ 4 โ€˜Very Highโ€™ WorkLifeBalance 1 : โ€˜Badโ€™ 2 โ€˜Goodโ€™ 3 โ€˜Betterโ€™ 4 โ€˜Bestโ€™ ๋ฐ์ดํ„ฐ์˜ ํ–‰์€ 1470, ์—ด์€ 35๊ฐœ์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ˆ˜์น˜ํ˜• ๋ฐ์ดํ„ฐ์™€ ๋ช…๋ชฉํ˜• ๋ฐ์ดํ„ฐ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๋ฐ ์กฐ๊ธˆ์€ ๊นŒ๋‹ค๋กœ์šธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ๋ชจ๋“  ๋ณ€์ˆ˜๋“ค์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด ์ข‹์ง€๋งŒ, ์šฐ๋ฆฌ์—๊ฒ ๊ทธ๋ ‡๊ฒŒ ๋งŽ์€ ์‹œ๊ฐ„์ด ์ฃผ์–ด์ง€์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด ๋ณ€์ˆ˜๋“ค์„ ์ตœ๋Œ€ํ•œ ๋น ๋ฅด๊ณ  ํšจ์œจ์ ์œผ๋กœ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋“ค์— ๋Œ€ํ•ด ์ง„ํ–‰ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ์„œ โ€™Attritionโ€™๋ณ€์ˆ˜๋ฅผ Target ๋ณ€์ˆ˜๋กœ ์ •ํ•˜๊ณ  ๋ถ„์„์„ ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 3. ํŒจํ‚ค์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ R ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜์‹ค ๋•Œ, ์šฐ๋ฆฌ๋Š” ํ›—๋‚  ์ฝ”๋“œ๊ฐ€ ๋’ค์ฃฝ๋ฐ•์ฃฝ์œผ๋กœ ์„ž์ด๊ฒŒ ๋˜๋Š” ๋Œ€์ฐธ์‚ฌ๊ฐ€ ๋ฒŒ์–ด์ง€์ง€ ์•Š๋„๋ก ์‹ ๊ฒฝ์„ ์จ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ฒซ ๊ณผ์ •์€ ์ผ๋‹จ ํŒจํ‚ค์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ฝ”๋“œ๋Š” ๋ชจ๋‘ ์œ„์— ๋ฐฐ์น˜์‹œํ‚ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ ๋‚˜์ค‘์— ํŒจํ‚ค์ง€ ์•ˆ ๋ถˆ๋Ÿฌ์™€์„œ ์ฝ”๋“œ๊ฐ€ ์•ˆ ๋Œ์•„๊ฐ€๋Š” ์ฐธ์‚ฌ๊ฐ€ ๋ฒŒ์–ด์ง€์ง€๊ฐ€ ์•Š์Šต๋‹ˆ๋‹ค. library(ggplot2) library(dplyr) library(reshape) library(cowplot) library(gmodels) library(corrplot) library(RColorBrewer) library(DMwR) library(caret) library(C50) library(randomForest) library(class) library(nnet) library(ROCR) library(car) library(pROC) ๋ถˆ๋Ÿฌ์˜ค๋Š” ํŒจํ‚ค์ง€๊ฐ€ ๋งŽ์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„ ์•ž์—์„œ ๋‹ค๋ค˜๋˜ ํŒจํ‚ค์ง€๋“ค์ด๋‹ˆ ๋”ฐ๋กœ ์„ค๋ช…ํ•˜์ง€๋Š” ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. 4. ํƒ์ƒ‰์  ๋ฐ์ดํ„ฐ ๋ถ„์„ Attrition ๋ณ€์ˆ˜๋Š” โ€™Yesโ€™์™€ โ€™Noโ€™๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชฉํ‘œ๋Š” โ€™Attritionโ€™์— ๋”ฐ๋ผ ์–ด๋–ค ๋ณ€์ˆ˜๋“ค์ด ์œ ์˜ํ•˜๊ฒŒ ์ฐจ์ด๊ฐ€ ๋‚˜๋Š”์ง€ ํƒ์ƒ‰์ ์œผ๋กœ ๋ถ„์„์„ ํ•ด๋ณด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋จผ์ €, ๋น ๋ฅธ ๋ถ„์„์„ ์œ„ํ•ด์„œ๋Š” Numeric ๋ณ€์ˆ˜์™€ Factor ๋ณ€์ˆ˜๋ฅผ ๋ถ„๋ฆฌ์‹œ์ผœ์„œ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ์ด ํ•œ ๋ฒˆ์— ํ‰์ณ์„œ ๋ถ„์„์„ ์ง„ํ–‰ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ์ธก์น˜ ๋ถ„์„ ๋ถ„์„์„ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ, ๊ฒฐ์ธก์น˜๊ฐ€ ๋ฐ์ดํ„ฐ์— ์–ผ๋งˆ๋‚˜ ์กด์žฌํ•˜๋Š”์ง€ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์€ ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค. colSums(is.na(IBM)) Age Attrition BusinessTravel 0 0 0 DailyRate Department DistanceFromHome 0 0 0 Education EducationField EmployeeCount 0 0 0 EmployeeNumber EnvironmentSatisfaction Gender 0 0 0 HourlyRate JobInvolvement JobLevel 0 0 0 JobRole JobSatisfaction MaritalStatus 0 0 0 MonthlyIncome MonthlyRate NumCompaniesWorked 0 0 0 Over18 OverTime PercentSalaryHike 0 0 0 PerformanceRating RelationshipSatisfaction StandardHours 0 0 0 StockOptionLevel TotalWorkingYears TrainingTimesLastYear 0 0 0 WorkLifeBalance YearsAtCompany YearsInCurrentRole 0 0 0 YearsSinceLastPromotion YearsWithCurrManager 0 0 ๊ฒฐ์ธก์น˜๋Š” ์กด์žฌํ•˜์ง€ ์•Š์€ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํƒ€๊นƒ ๋ณ€์ˆ˜ ๋ถ„์„ ํƒ€๊นƒ ๋ณ€์ˆ˜ โ€™Attritionโ€™์ด ๋Œ€์ถฉ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ์„ ํ•ฉ๋‹ˆ๋‹ค. summary(IBM$Attrition) No Yes 1233 237 print(paste(round(summary(IBM$Attrition) / nrow(IBM),2) *100, "%",sep = "")) [1] "84%" "16%" โ€™Yesโ€™์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ โ€™Noโ€™์— ๋น„ํ•ด ๋งค์šฐ ์ ์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— โ€™Attritionโ€™์„ ํƒ€๊นƒ์œผ๋กœ ๋ถ„๋ฅ˜ ๋ชจํ˜•์„ ๋งŒ๋“ ๋‹ค๊ณ  ํ•  ๋•Œ, ์ด ๋ถˆ๊ท ํ˜•์„ ํ•ด๊ฒฐํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ์œผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ˆ˜์น˜ํ˜• ๋ณ€์ˆ˜ ๋ถ„์„ ์ˆ˜์น˜ํ˜• ๋ณ€์ˆ˜๋ฅผ ๋ถ„์„ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฒƒ์€ ํ‰๊ท , ํ‘œ์ค€ํŽธ์ฐจ, ์ค‘์œ„์ˆ˜ ๋“ฑ์„ โ€™Attritionโ€™์˜ ๊ทธ๋ฃน ๊ฐ„์— ๊ณ„์‚ฐ์„ ํ•ด๋ณด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋˜ ์ˆ˜์น˜ํ˜• ๋ณ€์ˆ˜๋“ค์€ ๋ชจ๋‘ ๋‹จ์œ„๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์œ„๊ฐ€ ๋‹ค๋ฅด๋ฉด ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋“ค ๊ฐ„์˜ ๋น„๊ต๋Š” ํ•ด์„์„ ์ œ๋Œ€๋กœ ์ง„ํ–‰ํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์šฐ๋ฆฌ๋Š” ๊ธฐ์กด์— ๋ณ€์ˆ˜์˜ ๋‹จ์œ„๋ฅผ ํฌ๊ธฐํ•˜๊ณ  ๋ชจ๋‘ ๊ฐ™์€ ๋‹จ์œ„๋ฅผ ๊ฐ€์ง€๋„๋ก ํ‘œ์ค€ํ™” ํ˜น์€ ์ •๊ทœํ™” ๋ณ€ํ™˜์„ ์ง„ํ–‰ํ•ด ์ค๋‹ˆ๋‹ค. ํ‘œ์ค€ํ™”๋Š” ์žฌ๋ฏธ๊ฐ€ ์—†์œผ๋‹ˆ ์ •๊ทœํ™”(0 ~ 1)๋ฅผ ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. o m l z t o ( ) x m n ( ) a ( ) m n ( ) # ์ •๊ทœํ™” ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ Normalization = function(x){ y = (x - min(x)) / (max(x)-min(x)) return(y) } # ์ˆ˜์น˜ํ˜• ๋ณ€์ˆ˜๋“ค๋งŒ ๊ณจ๋ผ๋‚ด๊ธฐ IBM_Attrition_Numeric = IBM[,c(-grep("BusinessTravel",colnames(IBM)), -grep("Attrition",colnames(IBM)), -grep("Department",colnames(IBM)), -grep("EducationField",colnames(IBM)), -grep("Gender",colnames(IBM)), -grep("JobRole",colnames(IBM)), -grep("MaritalStatus",colnames(IBM)), -grep("Over18",colnames(IBM)), -grep("OverTime",colnames(IBM)))] # ์ •๊ทœํ™” ํ•จ์ˆ˜ ์ ์šฉ IBM_Attrition_Numeric = lapply(IBM_Attrition_Numeric, FUN = Normalization) IBM_Attrition_Numeric = as.data.frame(IBM_Attrition_Numeric) ์—ฌ๊ธฐ๊นŒ์ง€ ์ง„ํ–‰ํ•˜๋ฉด, ์ˆ˜์น˜ํ˜• ๋ณ€์ˆ˜๋“ค๋งŒ ๋”ฐ๋กœ ๋ฝ‘์•„๋ƒˆ์œผ๋ฉฐ, ์ •๊ทœํ™” ๋ณ€ํ™˜์ด ์ง„ํ–‰๋œ ๋ฐ์ดํ„ฐ ์…‹์ด ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ์ด์ œ โ€™Attritionโ€™์— ๋”ฐ๋ผ ํ‰๊ท , ์ค‘์œ„์ˆ˜, ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ํ•œ ๋ฒˆ์— ๊ตฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. IBM_Attrition_Numeric$Attrition = IBM$Attrition IBM_Attrition_Numeric_Mean = IBM_Attrition_Numeric %>% group_by(Attrition) %>% summarise_all(.funs = mean) %>% as.data.frame() %>% melt(id.vars = "Attrition") IBM_Attrition_Numeric_Median = IBM_Attrition_Numeric %>% group_by(Attrition) %>% summarise_all(.funs = median) %>% as.data.frame() %>% melt(id.vars = "Attrition") IBM_Attrition_Numeric_Sd = IBM_Attrition_Numeric %>% group_by(Attrition) %>% summarise_all(.funs = sd) %>% as.data.frame() %>% melt(id.vars = "Attrition") IBM_Numeric_Sumamry = data.frame( Attrition = IBM_Attrition_Numeric_Mean$Attrition, Variable = IBM_Attrition_Numeric_Mean$variable, Mean = IBM_Attrition_Numeric_Mean$value, Median = IBM_Attrition_Numeric_Median$value, Sd = IBM_Attrition_Numeric_Sd$value ) dplyr ํŒจํ‚ค์ง€์™€ reshape ํŒจํ‚ค์ง€๋ฅผ ํ˜ผํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ ‡๊ฒŒ, Attrition์— ๋”ฐ๋ฅธ ์ˆ˜์น˜ํ˜• ๋ณ€์ˆ˜๋“ค์˜ ํ†ต๊ณ„๋Ÿ‰๋“ค์„ ๊ตฌํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋ชจ๋“  ๊ฐ’์„ ํ‘œ๋กœ ๋น„๊ตํ•˜๊ธฐ์—๋Š” ์•„์ง ์ข€ ์–ด๋ ค์šด ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์šฐ๋ฆฌ๋Š” ์ด ํ†ต๊ณ„๋Ÿ‰๋“ค์„ ๊ฐ€์ง€๊ณ  ํšจ๊ณผ์ ์ธ ์‹œ๊ฐํ™”๋ฅผ ์‹œ๋„ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹œ๊ฐํ™”๋„ ํ•˜๋‚˜์”ฉ ๊ทธ๋ฆฌ๋ฉด ๋„ˆ๋ฌด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ ๋ฒˆ์— ํ‰์ณ์„œ ๊ทธ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ‘œ๋„ ์ข€ ๋” ์ง๊ด€์ ์œผ๋กœ ํ‘œํ˜„์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ‰์†Œ์™€ ๊ฐ™์•˜์œผ๋ฉด ์—‘์…€์— ๋ณด๊ธฐ ์ข‹๊ฒŒ ํ‘œ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ๋ถ„๋“ค์ด ๋งŽ๊ฒ ์ง€๋งŒ, ์šฐ๋ฆฌ๋Š” R์„ ๊ณต๋ถ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ์ž‘์—…์„ R๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. IBM_Numeric_Sumamry %>% na.omit() %>% mutate(Mean = ifelse(Attrition == "No",Mean * -1, Mean), Attrition = factor(Attrition, levels = c("No","Yes"))) %>% ggplot() + geom_bar(aes(x = Variable, y = Mean, fill = Attrition), stat = 'identity') + geom_text(aes(x = Variable, y = rep(c(-0.7,0.7),24), fill = Attrition, label = round(abs(Mean),2))) + scale_y_continuous(labels = abs) + coord_flip() + theme_bw() + theme(text = element_text(size = 15), legend.position = "bottom") ๋งŒ์•ฝ ๊ทธ๋ž˜ํ”„๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ทธ๋ฆฐ๋‹ค๋ฉด, โ€˜Attritionโ€™ ๊ฐ’์— ๋”ฐ๋ฅธ ์ˆ˜์น˜ํ˜• ๋ณ€์ˆ˜๋“ค์˜ ์ฐจ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„๊ฐ€ ๋‚ด์ฃผ๋Š” ๋ฉ”์‹œ์ง€๋Š” ํ•œ๋ฒˆ ์ง์ ‘ ํ•ด์„์„ ํ•ด๋ณด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ dplyr์™€ ์•ฝ๊ฐ„์˜ for ๋ฌธ์„ ํ™œ์šฉํ•ด ์ฃผ๋ฉด ์ด์ œ ์ €ํฌ๊ฐ€ ์ž์ฃผ ๋ณด๋˜ ์–‘์‹์˜ ํ‘œ๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ‘œ์˜ ํ•ด์„์€ ์—ฌ๋Ÿฌ๋ถ„๋“ค์—๊ฒŒ ๋งก๊ธฐ๊ฒ ์Šต๋‹ˆ๋‹ค. IBM_Numeric_Sumamry2 %>% mutate(Group = ifelse(Diff > 0 , "Yes","No")) %>% ggplot() + geom_bar(aes(x = Variable, y = Diff, fill = Group), stat = 'identity') + geom_text(aes(x = Variable, y = Diff, fill = Group, label = round(Diff, 2))) + scale_y_continuous(labels = abs) + coord_flip() + theme_bw() + theme(text = element_text(size = 15)) ๊ทธ๋ž˜ํ”„๋„ ๋‘ ํ‰๊ท ์˜ ์ฐจ์ด ๊ฐ’์„ ํ‘œํ˜„ํ•˜๊ฒŒ ๊ทธ๋ฆด ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณด์‹œ๋ฉด โ€™Attritionโ€™์ด โ€™Yesโ€™์— ํ•ด๋‹น๋˜๋Š” ์ง์›๋“ค์€ DistanceFromHome, NumCompaniesWorked, MonthlyRate ๋“ฑ์˜ ๊ฐ’์ด โ€™Noโ€™์ธ ์‚ฌ๋žŒ๋“ค๋ณด๋‹ค ๋” ๋†’์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. A5. Case Study(Modeling ํŽธ 2) 5. ํƒ์ƒ‰์  ๋ฐ์ดํ„ฐ ๋ถ„์„ 2 ํƒ์ƒ‰์  ๋ฐ์ดํ„ฐ ๋ถ„์„์€ ์‹œ๊ฐํ™”๊ฐ€ ์ฃผ๋ฅผ ์ด๋ฃจ์ง€๋งŒ, ์‹œ๊ฐํ™”์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์กฐ๊ธˆ ๋” ํ†ต๊ณ„ํ•™์ ์œผ๋กœ ๊ฒ€์ฆํ•˜๊ธฐ ์œ„ํ•˜์—ฌ t-test ํ˜น์€ ANOVA ๋ถ„์„์„ ์ž์ฃผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ํ†ต๊ณ„์ ์ธ ์ด๋ก ์„ ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค ๋”ฐ์ ธ๊ฐ€๋ฉด ๋งŽ์ด ๊นŒ๋‹ค๋กœ์šธ ์ˆ˜ ์žˆ์œผ๋‚˜, ๋ณดํ†ต ํƒ์ƒ‰์  ๋ถ„์„์„ ์œ„ํ•ด์„œ๋Š” ์ ๋‹นํ•œ ์„ ์—์„œ ๋Œ๋ ค์„œ ๊ฒฐ๊ณผ๋ฅผ Rough ํ•˜๊ฒŒ ํ™•์ธํ•˜๋Š” ์ •๋„๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ๋งน์‹ ์„<NAME> ๋ง๊ณ  ์ฐธ๊ณ ํ•˜๋Š” ์ •๋„๋กœ ํ•ด์„ํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค. โ€™Attritionโ€™์€ ์ˆ˜์ค€์ด 2๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— t-test๋ฅผ ํ™œ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฐจ์ด๊ฐ€ ํฌ๊ฒŒ ๋‚˜๋Š” ๋ณ€์ˆ˜๋“ค์„ 2๊ฐœ๋ฅผ ๊ณจ๋ผ์„œ t-test๋ฅผ ์ง„ํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ฐจ์ด๊ฐ€ ํฐ ๋ณ€์ˆ˜๋Š” JobLevel์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ggplot(IBM) + geom_boxplot(aes(x = Attrition, y = JobLevel, fill = Attrition), alpha = 0.2, outlier.colour = "red") + geom_jitter(aes(x = Attrition, y = JobLevel, col = Attrition), alpha = 0.8) + theme_bw() + theme(text = element_text(size = 15), legend.position = "bottom", axis.title.x = element_blank()) # Job Level leveneTest(JobLevel ~ Attrition, data = IBM) Levene's Test for Homogeneity of Variance (center = median) Df F value Pr(>F) group 1 7.2855 0.007031 ** 1468 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 t.test(JobLevel ~ Attrition, data = IBM, var.equal = FALSE) Welch Two Sample t-test data: JobLevel by Attrition t = 7.3859, df = 376.25, p-value = 9.845e-13 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: 0.3733861 0.6443231 sample estimates: mean in group No mean in group Yes 2.145985 1.637131 ์ด๋ ‡๊ฒŒ t-test๋ฅผ ์ง„ํ–‰ํ•œ ๋‹ค์Œ, ํ•ด์„์€ ๋™์ผํ•˜๊ฒŒ ํ•˜๋ฉด ๋˜์ง€๋งŒ, ํ•˜๋‚˜์”ฉ ๋Œ๋ ค๋ณด๊ธฐ์—๋Š” ๋„ˆ๋ฌด ๋ฐ˜๋ณต์ž‘์—…์ด ๋งŽ๊ณ  ๊ท€์ฐฎ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋ชจ๋“  ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ํ•œ ๋ฒˆ์— ์ง„ํ–‰ํ•  ์ˆ˜ ์—†์„๊นŒ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ๋Š” for ๋ฌธ์„ ํ™œ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Numeric_Variable = IBM_Numeric_Sumamry2$ Variable Numeric_Variable = as.character(Numeric_Variable) P_value = c() for(k in 1:length(Numeric_Variable)){ LEVENE = leveneTest(IBM[,Numeric_Variable[k]] ~ Attrition, data = IBM) LEVENE_Pvalue = LEVENE$`Pr(>F)`[1] VAR_EQAUL = ifelse(LEVENE_Pvalue > 0.05, TRUE, FALSE) T_TEST = t.test(IBM[,Numeric_Variable[k]] ~ Attrition, data = IBM, var.equal = VAR_EQAUL) TTEST_Pvalue = T_TEST$p.value P_value[k] = TTEST_Pvalue } IBM_Numeric_Sumamry2$ P_value = P_value ์ด๋ ‡๊ฒŒ for ๋ฌธ์„ ์ ์ ˆํ•˜๊ฒŒ ํ™œ์šฉํ•ด ์ฃผ๋ฉด, ๋ชจ๋“  ๋ณ€์ˆ˜์— ๋Œ€ํ•œ t-test์™€ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฐ๊ณผ๋Š” ๋งน์‹ ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์•„๋‹ˆ๋ผ, ๋Œ€๋žต์ ์œผ๋กœ ์–ด๋–ค ๋ณ€์ˆ˜๊ฐ€ ์œ ์˜ํ•œ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋Š”์ง€ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ถ„์„ ๋‹จ๊ณ„๋ผ๋Š” ๊ฒƒ์„ ๋ช…์‹ฌํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. 5. ํƒ์ƒ‰์  ๋ฐ์ดํ„ฐ ๋ถ„์„ 3 ์ด๋ฒˆ์—๋Š” ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜๋“ค์„ ๋Œ€์ƒ์œผ๋กœ ๋ถ„์„์„ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜๋“ค์€ ์นด์ด ์ œ๊ณฑ ๋…๋ฆฝ์„ฑ๊ฒ€์ •์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๋ถ„์„์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Over18๋ณ€์ˆ˜๋Š” ๋ชจ๋‘ โ€™Yโ€™์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋ถ„์„ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. IBM_Attrition_Factor = IBM[,c(grep("Attrition",colnames(IBM)), grep("BusinessTravel",colnames(IBM)), grep("Department",colnames(IBM)), grep("EducationField",colnames(IBM)), grep("Gender",colnames(IBM)), grep("JobRole",colnames(IBM)), grep("MaritalStatus",colnames(IBM)), grep("OverTime",colnames(IBM)))] Cols = c() P_value_V = c() for(k in 2:ncol(IBM_Attrition_Factor)){ Table = table(IBM_Attrition_Factor$Attrition, IBM_Attrition_Factor[,k]) Chisq = chisq.test(Table) P_Value = Chisq$p.value Cols[k] = colnames(IBM_Attrition_Factor)[k] P_value_V[k] = P_Value } Cols = na.omit(Cols) P_value_V = na.omit(P_value_V) DF = data.frame( Cols = Cols, P_value = P_value_V ) ๊ฒฐ๊ณผ๋ฅผ ๋ณด์‹œ๋ฉด Gender์˜ ๊ฒฝ์šฐ p-value๊ฐ€ ๋งค์šฐ ๋†’์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋กœ์จ ์šฐ๋ฆฌ๋Š” Gender์€ Attrition์— ํฌ๊ฒŒ ์œ ์˜ํ•œ ๋ณ€์ˆ˜๋Š” ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์ „์— ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ํƒ์ƒ‰์ ์œผ๋กœ ๋ถ„์„ํ•˜๋Š” ๊ณผ์ •์„ ์ง„ํ–‰ํ–ˆ์œผ๋ฉฐ, ๋‹ค์Œ ๋ถ„์„์—์„œ๋Š” ์‹ค์ œ ์˜ˆ์ธก ๋ชจํ˜•์„ ์ ์šฉ์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. A6. Case Study(Modeling ํŽธ 3) 6. ์˜ˆ์ธก ๋ชจํ˜• ์šฐ๋ฆฌ๊ฐ€ ๊ด€์‹ฌ ์žˆ์–ด ํ•˜๋Š” Attrition ๋ณ€์ˆ˜๋Š” ๋ฒ”์ฃผํ˜• ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋ถ„๋ฅ˜ ๋ชจํ˜•์„ ๊ณ ๋ คํ•ด์•ผ ๋œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•œ ๊ฐ€์ง€ ์งš๊ณ  ๋„˜์–ด๊ฐˆ ๊ฒƒ์€ ๋ถ„๋ฅ˜ ๋ชจํ˜•์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ์— ์žˆ์–ด์„œ Target ๋ณ€์ˆ˜์˜ ๊ท ํ˜•์€ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. summary(IBM$Attrition) No Yes 1233 237 ๋ณด์‹œ๋ฉด Attrition์— No๊ฐ€ Yes์— ๋น„ํ•ด ๋งค์šฐ ๋งŽ์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ถ”์ •๋œ ๋ถ„๋ฅ˜ ๋ชจํ˜•์€ ํŽธํ–ฅ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋“ฑ์žฅํ•˜๋Š” ๊ฒƒ์ด Sampling ๋ฐฉ๋ฒ•๋“ค์ž…๋‹ˆ๋‹ค. ํฌ๊ฒŒ ๋ณด๋ฉด ๋ฟ”๋ฆฌ๋ƒ, ์ถ•์†Œํ•˜๋ƒ ๊ทธ ์ฐจ์ด์ž…๋‹ˆ๋‹ค. ์ƒ๋Œ€์ ์œผ๋กœ ์ž‘์€ Group์— ๋Œ€ํ•ด์„œ ์–‘์„ ๋ถ€ํ’€๋ฆฌ๋Š” ๊ฒƒ์„ Oversampling์ด๋ผ๊ณ  ํ•˜๊ณ , ์ƒ๋Œ€์ ์œผ๋กœ ํฐ Group์— ๋Œ€ํ•ด์„œ๋Š” ์ถ•์†Œ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์„ Undersampling์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ํ•™ํ†ต๊ณ„์—์„œ๋Š” ์„ฑํ–ฅ ์ ์ˆ˜ ๋งค์นญ(PSM) ๋ฐฉ๋ฒ•์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๊ณ„ํ•™์Šต ๋ถ„์•ผ์—์„œ๋„ SMOTE๋ผ๋“ ์ง€ ๋Œ€์ฑ…๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ƒ˜ํ”Œ๋ง ๋ฐฉ๋ฒ•์€ ๋” ์ •๋‹ต์ด ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ €๋ ‡๊ฒŒ ํ•ด๋ณด๋‹ค๊ฐ€ ๊ฐ€์žฅ ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‚˜์˜ค๋Š” ๊ฒƒ์ด ํ•ด๋‹น ๋ฐ์ดํ„ฐ์™€ ๋ชจํ˜•์— ๊ฐ€์žฅ ์ ํ•ฉํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ผ๋‹จ ์ด ์ฑ…์—์„œ๋Š” ์„ฑํ–ฅ ์ ์ˆ˜ ๋งค์นญ, SMOTE ๋“ฑ์˜ ๋ฐฉ๋ฒ•๋“ค์„ ๋‹ค๋ฃจ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๊ฐ„๋‹จํ•˜๊ฒŒ Original ํ•˜๊ฒŒ ๋ถ„์„ํ•˜์˜€์„ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  K-fold CV๋Š” ๋‹ค๋ฃจ์—ˆ์œผ๋‹ˆ, ๊ทธ ๋ถ€๋ถ„์„ ํ™œ์šฉํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์—์„œ๋Š” ์•ž์„œ ์ง„ํ–‰ํ•œ EDA์—์„œ ์œ ์˜ํ•˜๊ฒŒ ๋‚˜์˜จ ๋ณ€์ˆ˜๋“ค์„ ํ™œ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Original IBM_A = IBM %>% select(Age, DailyRate, EnvironmentSatisfaction, JobInvolvement, JobSatisfaction, MonthlyIncome, MonthlyRate, StockOptionLevel, TotalWorkingYears, TrainingTimesLastYear, WorkLifeBalance, YearsAtCompany, YearsInCurrentRole, YearsWithCurrManager, BusinessTravel, Department, EducationField, JobRole, MaritalStatus, OverTime, Attrition) %>% mutate(Attrition = as.factor(ifelse(Attrition == "Yes",1,0))) K = 4 set.seed(1234) Sample = sample(rep(1:K, length = nrow(IBM_A))) CV_LIST = data.frame() for(k in 1:K){ TRAIN = IBM_A[Sample!= k,] TEST = IBM_A[Sample == k,] # Logistic Regression GLM = step(glm(Attrition == 1 ~ . , data = TRAIN), direction = "backward",) GLM_Probs = predict(GLM, newdata = TEST, type = 'response') # Decision Tree FEATURE = TRAIN[,-grep("Attrition",colnames(TRAIN))] RESPONSE = TRAIN[,grep("Attrition",colnames(TRAIN))] tree = C5.0(FEATURE, RESPONSE, control = C5.0Control(noGlobalPruning = FALSE, minCases = 20), trials = 10) Tree_Probs = predict(tree, newdata = TEST, type = 'prob') # RandomForest RF = randomForest(Attrition ~ ., data = TRAIN, mtry = 5, ntree = 300, importance = T) RF_Probs = predict(RF, newdata = TEST, type = 'prob') GLM_ROC = roc(TEST$Attrition, GLM_Probs) Tree_ROC = roc(TEST$Attrition, Tree_Probs[, 2]) RF_ROC = roc(TEST$Attrition, RF_Probs[, 2]) ROC_DF = data.frame( SEN = c(GLM_ROC$sensitivities, Tree_ROC$sensitivities, RF_ROC$sensitivities), SPE = c(GLM_ROC$specificities, Tree_ROC$specificities, RF_ROC$specificities), Model = c(rep("GLM",length(GLM_ROC$sensitivities)), rep("Tree",length(Tree_ROC$sensitivities)), rep("RF",length(RF_ROC$sensitivities))), K = k ) CV_LIST = rbind(CV_LIST, ROC_DF) } Step: AIC=735.83 Attrition == 1 ~ Age + DailyRate + EnvironmentSatisfaction + JobInvolvement + JobSatisfaction + StockOptionLevel + TrainingTimesLastYear + WorkLifeBalance + YearsInCurrentRole + YearsWithCurrManager + BusinessTravel + EducationField + JobRole + MaritalStatus + OverTime Df Deviance AIC - YearsInCurrentRole 1 119.27 734.64 <none> 119.18 735.83 - StockOptionLevel 1 119.46 736.42 - TrainingTimesLastYear 1 119.52 736.94 - YearsWithCurrManager 1 119.59 737.64 - DailyRate 1 119.67 738.41 - EducationField 5 120.57 738.64 - MaritalStatus 2 120.05 739.84 - WorkLifeBalance 1 119.88 740.24 - EnvironmentSatisfaction 1 120.38 744.86 - JobRole 8 122.12 746.75 - Age 1 120.81 748.78 - JobInvolvement 1 120.99 750.42 - JobSatisfaction 1 121.10 751.49 - BusinessTravel 2 121.65 754.44 - OverTime 1 127.19 805.54 Step: AIC=734.64 Attrition == 1 ~ Age + DailyRate + EnvironmentSatisfaction + JobInvolvement + JobSatisfaction + StockOptionLevel + TrainingTimesLastYear + WorkLifeBalance + YearsWithCurrManager + BusinessTravel + EducationField + JobRole + MaritalStatus + OverTime Df Deviance AIC <none> 119.27 734.64 - StockOptionLevel 1 119.55 735.25 - TrainingTimesLastYear 1 119.60 735.75 - EducationField 5 120.63 737.17 - DailyRate 1 119.79 737.44 - MaritalStatus 2 120.17 739.01 - WorkLifeBalance 1 120.02 739.59 - EnvironmentSatisfaction 1 120.48 743.81 - YearsWithCurrManager 1 120.61 745.00 - JobRole 8 122.34 746.73 - Age 1 120.95 748.06 - JobInvolvement 1 121.06 749.07 - JobSatisfaction 1 121.21 750.47 - BusinessTravel 2 121.75 753.36 - OverTime 1 127.32 804.70 CV_LIST %>% ggplot() + geom_line(aes(x = 1-SPE, y = SEN, col = Model),size = 1.2) + xlab("1-specificity") + ylab("sensitivity") + theme_bw() + theme(legend.position = "bottom", legend.box.background = element_rect(), legend.box.margin = ggplot2::margin(2,2,2,2), text = element_text(size = 15)) + facet_wrap(~K) ๊ฒฐ๊ณผ๋ฅผ ์‚ดํŽด๋ณด๋ฉด 4๋ฒˆ์งธ CV์˜ ๊ฒฝ์šฐ GLM์ด ๊ฐ€์žฅ ROC ์ปค๋ธŒ๊ฐ€ ๋„“๊ฒŒ ๊ทธ๋ ค์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ผญ ๊ธฐ์–ตํ•ด์•ผ ๋˜๋Š” ์‚ฌ์‹ค์€ ๊ธฐ๊ณ„ํ•™์Šต์ด๋ผ๊ณ  ์ผ๋ฐ˜ ํ†ต๊ณ„ ๋ชจํ˜•๋ณด๋‹ค ์ข‹์€ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ํ•ญ์ƒ ์ƒํ™ฉ๊ณผ ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ธฐ ๋งˆ๋ จ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์ด ๊ธ€์„ ์ฝ์œผ์‹œ๋Š” ์—ฌ๋Ÿฌ๋ถ„๋“ค์€ ์ง์ ‘ ๋ถ„์„ ํ”„๋กœ์ ํŠธ์— ํˆฌ์ž…๋˜์…จ์„ ๋•Œ ํŠน์ • ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ๋†’์€ ์‹ ๋ขฐ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ChC. ์ถ”๊ฐ€ ์˜ˆ์ • ๋ชฉ๋ก ์ถ”๊ฐ€๋  ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. - ์บ๊ธ€ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•œ ํƒ์ƒ‰์  ์ž๋ฃŒ๋ถ„์„(์ถ”๊ฐ€ ์™„๋ฃŒ) - ์„ ํ˜•๋ชจํ˜• ์ ํ•ฉ(์ถ”๊ฐ€ ์™„๋ฃŒ) - ์ค‘์‹ฌ๊ทนํ•œ ์ •๋ฆฌ์˜ ์ˆ˜๋ฆฌํ†ต๊ณ„ํ•™์  ์ด๋ก  ๋ฐ์ดํ„ฐ๋งˆ์ด๋‹๊ณผ ๊ธฐ๊ณ„ํ•™์Šต์˜ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ MLR 2ํŽธ์—์„œ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ์ž‘์—… ์ค‘์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ œ ๊ฐœ์ธ ๋ธ”๋กœ๊ทธ ์ฃผ์†Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. https://mustlearning.tistory.com ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.<|endoftext|>